blob: 848286313dfb50aa305629c1ef330603ce81020b [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"
drh9a324642003-09-06 20:12:01 +000018#include <ctype.h>
19#include "vdbeInt.h"
20
21
drh46c99e02007-08-27 23:26:59 +000022
drh9a324642003-09-06 20:12:01 +000023/*
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;
drh17435752007-08-16 04:30:38 +000038 p = sqlite3DbMallocZero(db, sizeof(Vdbe) );
drh9a324642003-09-06 20:12:01 +000039 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/*
drhb900aaf2006-11-09 00:24:53 +000052** Remember the SQL string for a prepared statement.
53*/
54void sqlite3VdbeSetSql(Vdbe *p, const char *z, int n){
55 if( p==0 ) return;
56 assert( p->zSql==0 );
drh17435752007-08-16 04:30:38 +000057 p->zSql = sqlite3DbStrNDup(p->db, z, n);
drhb900aaf2006-11-09 00:24:53 +000058}
59
60/*
61** Return the SQL associated with a prepared statement
62*/
danielk1977d0e2a852007-11-14 06:48:48 +000063const char *sqlite3_sql(sqlite3_stmt *pStmt){
64 return ((Vdbe *)pStmt)->zSql;
drhb900aaf2006-11-09 00:24:53 +000065}
66
67/*
drhc5155252007-01-08 21:07:17 +000068** Swap all content between two VDBE structures.
drhb900aaf2006-11-09 00:24:53 +000069*/
drhc5155252007-01-08 21:07:17 +000070void sqlite3VdbeSwap(Vdbe *pA, Vdbe *pB){
71 Vdbe tmp, *pTmp;
72 char *zTmp;
73 int nTmp;
74 tmp = *pA;
75 *pA = *pB;
76 *pB = tmp;
77 pTmp = pA->pNext;
78 pA->pNext = pB->pNext;
79 pB->pNext = pTmp;
80 pTmp = pA->pPrev;
81 pA->pPrev = pB->pPrev;
82 pB->pPrev = pTmp;
83 zTmp = pA->zSql;
84 pA->zSql = pB->zSql;
85 pB->zSql = zTmp;
86 nTmp = pA->nSql;
87 pA->nSql = pB->nSql;
88 pB->nSql = nTmp;
drhb900aaf2006-11-09 00:24:53 +000089}
90
drhcf1023c2007-05-08 20:59:49 +000091#ifdef SQLITE_DEBUG
drhb900aaf2006-11-09 00:24:53 +000092/*
drh9a324642003-09-06 20:12:01 +000093** Turn tracing on or off
94*/
danielk19774adee202004-05-08 08:23:19 +000095void sqlite3VdbeTrace(Vdbe *p, FILE *trace){
drh9a324642003-09-06 20:12:01 +000096 p->trace = trace;
97}
drhcf1023c2007-05-08 20:59:49 +000098#endif
drh9a324642003-09-06 20:12:01 +000099
100/*
drh76ff3a02004-09-24 22:32:30 +0000101** Resize the Vdbe.aOp array so that it contains at least N
drha4e5d582007-10-20 15:41:57 +0000102** elements.
danielk1977ace3eb22006-01-26 10:35:04 +0000103**
104** If an out-of-memory error occurs while resizing the array,
105** Vdbe.aOp and Vdbe.nOpAlloc remain unchanged (this is so that
106** any opcodes already allocated can be correctly deallocated
107** along with the rest of the Vdbe).
drh76ff3a02004-09-24 22:32:30 +0000108*/
109static void resizeOpArray(Vdbe *p, int N){
drha4e5d582007-10-20 15:41:57 +0000110 VdbeOp *pNew;
111 int oldSize = p->nOpAlloc;
112 pNew = sqlite3DbRealloc(p->db, p->aOp, N*sizeof(Op));
113 if( pNew ){
114 p->nOpAlloc = N;
115 p->aOp = pNew;
116 if( N>oldSize ){
117 memset(&p->aOp[oldSize], 0, (N-oldSize)*sizeof(Op));
drh76ff3a02004-09-24 22:32:30 +0000118 }
119 }
120}
121
122/*
drh9a324642003-09-06 20:12:01 +0000123** Add a new instruction to the list of instructions current in the
124** VDBE. Return the address of the new instruction.
125**
126** Parameters:
127**
128** p Pointer to the VDBE
129**
130** op The opcode for this instruction
131**
drh66a51672008-01-03 00:01:23 +0000132** p1, p2, p3 Operands
drh9a324642003-09-06 20:12:01 +0000133**
danielk19774adee202004-05-08 08:23:19 +0000134** Use the sqlite3VdbeResolveLabel() function to fix an address and
drh66a51672008-01-03 00:01:23 +0000135** the sqlite3VdbeChangeP4() function to change the value of the P4
drh9a324642003-09-06 20:12:01 +0000136** operand.
137*/
drh66a51672008-01-03 00:01:23 +0000138int sqlite3VdbeAddOp3(Vdbe *p, int op, int p1, int p2, int p3){
drh9a324642003-09-06 20:12:01 +0000139 int i;
drh701a0ae2004-02-22 20:05:00 +0000140 VdbeOp *pOp;
drh9a324642003-09-06 20:12:01 +0000141
142 i = p->nOp;
drh9a324642003-09-06 20:12:01 +0000143 assert( p->magic==VDBE_MAGIC_INIT );
drhfd2d26b2006-03-15 22:44:36 +0000144 if( p->nOpAlloc<=i ){
drha4e5d582007-10-20 15:41:57 +0000145 resizeOpArray(p, p->nOpAlloc*2 + 100);
drh17435752007-08-16 04:30:38 +0000146 if( p->db->mallocFailed ){
drhfd2d26b2006-03-15 22:44:36 +0000147 return 0;
148 }
drh9a324642003-09-06 20:12:01 +0000149 }
danielk197701256832007-04-18 14:24:32 +0000150 p->nOp++;
drh701a0ae2004-02-22 20:05:00 +0000151 pOp = &p->aOp[i];
152 pOp->opcode = op;
153 pOp->p1 = p1;
drh701a0ae2004-02-22 20:05:00 +0000154 pOp->p2 = p2;
drh66a51672008-01-03 00:01:23 +0000155 pOp->p3 = p3;
156 pOp->p4.p = 0;
157 pOp->p4type = P4_NOTUSED;
drh55ef4d92005-08-14 01:20:37 +0000158 p->expired = 0;
danielk19778b60e0f2005-01-12 09:10:39 +0000159#ifdef SQLITE_DEBUG
danielk1977132872b2004-05-10 10:37:18 +0000160 if( sqlite3_vdbe_addop_trace ) sqlite3VdbePrintOp(0, i, &p->aOp[i]);
drh9a324642003-09-06 20:12:01 +0000161#endif
162 return i;
163}
drh66a51672008-01-03 00:01:23 +0000164int sqlite3VdbeAddOp0(Vdbe *p, int op){
165 return sqlite3VdbeAddOp3(p, op, 0, 0, 0);
166}
167int sqlite3VdbeAddOp1(Vdbe *p, int op, int p1){
168 return sqlite3VdbeAddOp3(p, op, p1, 0, 0);
169}
170int sqlite3VdbeAddOp2(Vdbe *p, int op, int p1, int p2){
171 return sqlite3VdbeAddOp3(p, op, p1, p2, 0);
drh701a0ae2004-02-22 20:05:00 +0000172}
173
drh66a51672008-01-03 00:01:23 +0000174
drh701a0ae2004-02-22 20:05:00 +0000175/*
drh66a51672008-01-03 00:01:23 +0000176** Add an opcode that includes the p4 value as a pointer.
drhd4e70eb2008-01-02 00:34:36 +0000177*/
drh66a51672008-01-03 00:01:23 +0000178int sqlite3VdbeAddOp4(
drhd4e70eb2008-01-02 00:34:36 +0000179 Vdbe *p, /* Add the opcode to this VM */
180 int op, /* The new opcode */
drh66a51672008-01-03 00:01:23 +0000181 int p1, /* The P1 operand */
182 int p2, /* The P2 operand */
183 int p3, /* The P3 operand */
184 const char *zP4, /* The P4 operand */
185 int p4type /* P4 operand type */
drhd4e70eb2008-01-02 00:34:36 +0000186){
drh66a51672008-01-03 00:01:23 +0000187 int addr = sqlite3VdbeAddOp3(p, op, p1, p2, p3);
188 sqlite3VdbeChangeP4(p, addr, zP4, p4type);
drhd4e70eb2008-01-02 00:34:36 +0000189 return addr;
190}
191
192/*
drh9a324642003-09-06 20:12:01 +0000193** Create a new symbolic label for an instruction that has yet to be
194** coded. The symbolic label is really just a negative number. The
195** label can be used as the P2 value of an operation. Later, when
196** the label is resolved to a specific address, the VDBE will scan
197** through its operation list and change all values of P2 which match
198** the label into the resolved address.
199**
200** The VDBE knows that a P2 value is a label because labels are
201** always negative and P2 values are suppose to be non-negative.
202** Hence, a negative P2 value is a label that has yet to be resolved.
danielk1977b5548a82004-06-26 13:51:33 +0000203**
204** Zero is returned if a malloc() fails.
drh9a324642003-09-06 20:12:01 +0000205*/
danielk19774adee202004-05-08 08:23:19 +0000206int sqlite3VdbeMakeLabel(Vdbe *p){
drh9a324642003-09-06 20:12:01 +0000207 int i;
208 i = p->nLabel++;
209 assert( p->magic==VDBE_MAGIC_INIT );
210 if( i>=p->nLabelAlloc ){
drh9a324642003-09-06 20:12:01 +0000211 p->nLabelAlloc = p->nLabelAlloc*2 + 10;
danielk19771e536952007-08-16 10:09:01 +0000212 p->aLabel = sqlite3DbReallocOrFree(p->db, p->aLabel,
drhcf643722007-03-27 13:36:37 +0000213 p->nLabelAlloc*sizeof(p->aLabel[0]));
drh9a324642003-09-06 20:12:01 +0000214 }
drh76ff3a02004-09-24 22:32:30 +0000215 if( p->aLabel ){
216 p->aLabel[i] = -1;
drh9a324642003-09-06 20:12:01 +0000217 }
drh9a324642003-09-06 20:12:01 +0000218 return -1-i;
219}
220
221/*
222** Resolve label "x" to be the address of the next instruction to
223** be inserted. The parameter "x" must have been obtained from
danielk19774adee202004-05-08 08:23:19 +0000224** a prior call to sqlite3VdbeMakeLabel().
drh9a324642003-09-06 20:12:01 +0000225*/
danielk19774adee202004-05-08 08:23:19 +0000226void sqlite3VdbeResolveLabel(Vdbe *p, int x){
drh76ff3a02004-09-24 22:32:30 +0000227 int j = -1-x;
drh9a324642003-09-06 20:12:01 +0000228 assert( p->magic==VDBE_MAGIC_INIT );
drh76ff3a02004-09-24 22:32:30 +0000229 assert( j>=0 && j<p->nLabel );
230 if( p->aLabel ){
231 p->aLabel[j] = p->nOp;
drh9a324642003-09-06 20:12:01 +0000232 }
233}
234
235/*
danielk1977bc04f852005-03-29 08:26:13 +0000236** Return non-zero if opcode 'op' is guarenteed not to push more values
237** onto the VDBE stack than it pops off.
238*/
danielk19777a5147c2005-03-29 13:07:00 +0000239static int opcodeNoPush(u8 op){
240 /* The 10 NOPUSH_MASK_n constants are defined in the automatically
danielk1977bc04f852005-03-29 08:26:13 +0000241 ** generated header file opcodes.h. Each is a 16-bit bitmask, one
242 ** bit corresponding to each opcode implemented by the virtual
danielk19777a5147c2005-03-29 13:07:00 +0000243 ** machine in vdbe.c. The bit is true if the word "no-push" appears
danielk1977bc04f852005-03-29 08:26:13 +0000244 ** in a comment on the same line as the "case OP_XXX:" in
245 ** sqlite3VdbeExec() in vdbe.c.
246 **
247 ** If the bit is true, then the corresponding opcode is guarenteed not
248 ** to grow the stack when it is executed. Otherwise, it may grow the
249 ** stack by at most one entry.
250 **
danielk19777a5147c2005-03-29 13:07:00 +0000251 ** NOPUSH_MASK_0 corresponds to opcodes 0 to 15. NOPUSH_MASK_1 contains
danielk1977bc04f852005-03-29 08:26:13 +0000252 ** one bit for opcodes 16 to 31, and so on.
253 **
254 ** 16-bit bitmasks (rather than 32-bit) are specified in opcodes.h
255 ** because the file is generated by an awk program. Awk manipulates
256 ** all numbers as floating-point and we don't want to risk a rounding
257 ** error if someone builds with an awk that uses (for example) 32-bit
258 ** IEEE floats.
259 */
drh9a7e6082005-03-31 22:26:19 +0000260 static const u32 masks[5] = {
drh580eeaf2006-02-24 03:09:37 +0000261 NOPUSH_MASK_0 + (((unsigned)NOPUSH_MASK_1)<<16),
262 NOPUSH_MASK_2 + (((unsigned)NOPUSH_MASK_3)<<16),
263 NOPUSH_MASK_4 + (((unsigned)NOPUSH_MASK_5)<<16),
264 NOPUSH_MASK_6 + (((unsigned)NOPUSH_MASK_7)<<16),
265 NOPUSH_MASK_8 + (((unsigned)NOPUSH_MASK_9)<<16)
danielk1977bc04f852005-03-29 08:26:13 +0000266 };
drh9cbe6352005-11-29 03:13:21 +0000267 assert( op<32*5 );
danielk1977bc04f852005-03-29 08:26:13 +0000268 return (masks[op>>5] & (1<<(op&0x1F)));
269}
270
271#ifndef NDEBUG
danielk19777a5147c2005-03-29 13:07:00 +0000272int sqlite3VdbeOpcodeNoPush(u8 op){
273 return opcodeNoPush(op);
danielk1977bc04f852005-03-29 08:26:13 +0000274}
275#endif
276
277/*
drh76ff3a02004-09-24 22:32:30 +0000278** Loop through the program looking for P2 values that are negative.
279** Each such value is a label. Resolve the label by setting the P2
280** value to its correct non-zero value.
281**
282** This routine is called once after all opcodes have been inserted.
danielk1977634f2982005-03-28 08:44:07 +0000283**
drh13449892005-09-07 21:22:45 +0000284** Variable *pMaxFuncArgs is set to the maximum value of any P2 argument
danielk1977399918f2006-06-14 13:03:23 +0000285** to an OP_Function, OP_AggStep or OP_VFilter opcode. This is used by
danielk1977634f2982005-03-28 08:44:07 +0000286** sqlite3VdbeMakeReady() to size the Vdbe.apArg[] array.
danielk1977bc04f852005-03-29 08:26:13 +0000287**
288** The integer *pMaxStack is set to the maximum number of vdbe stack
289** entries that static analysis reveals this program might need.
drh38449902005-06-07 01:43:41 +0000290**
291** This routine also does the following optimization: It scans for
drh77658e22007-12-04 16:54:52 +0000292** instructions that might cause a statement rollback. Such instructions
293** are:
294**
295** * OP_Halt with P1=SQLITE_CONSTRAINT and P2=OE_Abort.
296** * OP_Destroy
297** * OP_VUpdate
298** * OP_VRename
299**
300** If no such instruction is found, then every Statement instruction
301** is changed to a Noop. In this way, we avoid creating the statement
302** journal file unnecessarily.
drh76ff3a02004-09-24 22:32:30 +0000303*/
danielk1977bc04f852005-03-29 08:26:13 +0000304static void resolveP2Values(Vdbe *p, int *pMaxFuncArgs, int *pMaxStack){
drh76ff3a02004-09-24 22:32:30 +0000305 int i;
danielk1977bc04f852005-03-29 08:26:13 +0000306 int nMaxArgs = 0;
307 int nMaxStack = p->nOp;
drh76ff3a02004-09-24 22:32:30 +0000308 Op *pOp;
309 int *aLabel = p->aLabel;
drh38449902005-06-07 01:43:41 +0000310 int doesStatementRollback = 0;
311 int hasStatementBegin = 0;
drh76ff3a02004-09-24 22:32:30 +0000312 for(pOp=p->aOp, i=p->nOp-1; i>=0; i--, pOp++){
danielk1977634f2982005-03-28 08:44:07 +0000313 u8 opcode = pOp->opcode;
314
danielk19774b2688a2006-06-20 11:01:07 +0000315 if( opcode==OP_Function || opcode==OP_AggStep
danielk1977399918f2006-06-14 13:03:23 +0000316#ifndef SQLITE_OMIT_VIRTUALTABLE
danielk19774b2688a2006-06-20 11:01:07 +0000317 || opcode==OP_VUpdate
danielk1977399918f2006-06-14 13:03:23 +0000318#endif
319 ){
danielk1977bc04f852005-03-29 08:26:13 +0000320 if( pOp->p2>nMaxArgs ) nMaxArgs = pOp->p2;
danielk1977182c4ba2007-06-27 15:53:34 +0000321 }
322 if( opcode==OP_Halt ){
drh38449902005-06-07 01:43:41 +0000323 if( pOp->p1==SQLITE_CONSTRAINT && pOp->p2==OE_Abort ){
324 doesStatementRollback = 1;
325 }
drh38449902005-06-07 01:43:41 +0000326 }else if( opcode==OP_Statement ){
327 hasStatementBegin = 1;
drh77658e22007-12-04 16:54:52 +0000328 }else if( opcode==OP_Destroy ){
329 doesStatementRollback = 1;
danielk1977182c4ba2007-06-27 15:53:34 +0000330#ifndef SQLITE_OMIT_VIRTUALTABLE
331 }else if( opcode==OP_VUpdate || opcode==OP_VRename ){
332 doesStatementRollback = 1;
drh4be8b512006-06-13 23:51:34 +0000333 }else if( opcode==OP_VFilter ){
334 int n;
335 assert( p->nOp - i >= 3 );
336 assert( pOp[-2].opcode==OP_Integer );
337 n = pOp[-2].p1;
338 if( n>nMaxArgs ) nMaxArgs = n;
danielk1977182c4ba2007-06-27 15:53:34 +0000339#endif
danielk1977bc04f852005-03-29 08:26:13 +0000340 }
danielk19777a5147c2005-03-29 13:07:00 +0000341 if( opcodeNoPush(opcode) ){
danielk1977bc04f852005-03-29 08:26:13 +0000342 nMaxStack--;
danielk1977634f2982005-03-28 08:44:07 +0000343 }
344
drh76ff3a02004-09-24 22:32:30 +0000345 if( pOp->p2>=0 ) continue;
346 assert( -1-pOp->p2<p->nLabel );
347 pOp->p2 = aLabel[-1-pOp->p2];
348 }
drh17435752007-08-16 04:30:38 +0000349 sqlite3_free(p->aLabel);
drh76ff3a02004-09-24 22:32:30 +0000350 p->aLabel = 0;
danielk1977bc04f852005-03-29 08:26:13 +0000351
352 *pMaxFuncArgs = nMaxArgs;
353 *pMaxStack = nMaxStack;
drh38449902005-06-07 01:43:41 +0000354
355 /* If we never rollback a statement transaction, then statement
356 ** transactions are not needed. So change every OP_Statement
drh66560ad2006-01-06 14:32:19 +0000357 ** opcode into an OP_Noop. This avoid a call to sqlite3OsOpenExclusive()
drh38449902005-06-07 01:43:41 +0000358 ** which can be expensive on some platforms.
359 */
360 if( hasStatementBegin && !doesStatementRollback ){
361 for(pOp=p->aOp, i=p->nOp-1; i>=0; i--, pOp++){
362 if( pOp->opcode==OP_Statement ){
363 pOp->opcode = OP_Noop;
364 }
365 }
366 }
drh76ff3a02004-09-24 22:32:30 +0000367}
368
369/*
drh9a324642003-09-06 20:12:01 +0000370** Return the address of the next instruction to be inserted.
371*/
danielk19774adee202004-05-08 08:23:19 +0000372int sqlite3VdbeCurrentAddr(Vdbe *p){
drh9a324642003-09-06 20:12:01 +0000373 assert( p->magic==VDBE_MAGIC_INIT );
374 return p->nOp;
375}
376
377/*
378** Add a whole list of operations to the operation stack. Return the
379** address of the first operation added.
380*/
danielk19774adee202004-05-08 08:23:19 +0000381int sqlite3VdbeAddOpList(Vdbe *p, int nOp, VdbeOpList const *aOp){
drh9a324642003-09-06 20:12:01 +0000382 int addr;
383 assert( p->magic==VDBE_MAGIC_INIT );
drha4e5d582007-10-20 15:41:57 +0000384 if( p->nOp + nOp > p->nOpAlloc ){
385 resizeOpArray(p, p->nOp*2 + nOp);
386 }
drh17435752007-08-16 04:30:38 +0000387 if( p->db->mallocFailed ){
drh76ff3a02004-09-24 22:32:30 +0000388 return 0;
drh9a324642003-09-06 20:12:01 +0000389 }
390 addr = p->nOp;
391 if( nOp>0 ){
392 int i;
drh905793e2004-02-21 13:31:09 +0000393 VdbeOpList const *pIn = aOp;
394 for(i=0; i<nOp; i++, pIn++){
395 int p2 = pIn->p2;
396 VdbeOp *pOut = &p->aOp[i+addr];
397 pOut->opcode = pIn->opcode;
398 pOut->p1 = pIn->p1;
399 pOut->p2 = p2<0 ? addr + ADDR(p2) : p2;
drh24003452008-01-03 01:28:59 +0000400 pOut->p3 = pIn->p3;
401 pOut->p4type = P4_NOTUSED;
402 pOut->p4.p = 0;
403 pOut->p5 = 0;
danielk19778b60e0f2005-01-12 09:10:39 +0000404#ifdef SQLITE_DEBUG
danielk1977132872b2004-05-10 10:37:18 +0000405 if( sqlite3_vdbe_addop_trace ){
danielk19774adee202004-05-08 08:23:19 +0000406 sqlite3VdbePrintOp(0, i+addr, &p->aOp[i+addr]);
drh9a324642003-09-06 20:12:01 +0000407 }
408#endif
409 }
410 p->nOp += nOp;
411 }
412 return addr;
413}
414
415/*
416** Change the value of the P1 operand for a specific instruction.
417** This routine is useful when a large program is loaded from a
danielk19774adee202004-05-08 08:23:19 +0000418** static array using sqlite3VdbeAddOpList but we want to make a
drh9a324642003-09-06 20:12:01 +0000419** few minor changes to the program.
420*/
danielk19774adee202004-05-08 08:23:19 +0000421void sqlite3VdbeChangeP1(Vdbe *p, int addr, int val){
drh8aa34ae2006-03-13 12:54:09 +0000422 assert( p==0 || p->magic==VDBE_MAGIC_INIT );
drh9a324642003-09-06 20:12:01 +0000423 if( p && addr>=0 && p->nOp>addr && p->aOp ){
424 p->aOp[addr].p1 = val;
425 }
426}
427
428/*
429** Change the value of the P2 operand for a specific instruction.
430** This routine is useful for setting a jump destination.
431*/
danielk19774adee202004-05-08 08:23:19 +0000432void sqlite3VdbeChangeP2(Vdbe *p, int addr, int val){
drh9a324642003-09-06 20:12:01 +0000433 assert( val>=0 );
drh8aa34ae2006-03-13 12:54:09 +0000434 assert( p==0 || p->magic==VDBE_MAGIC_INIT );
drh9a324642003-09-06 20:12:01 +0000435 if( p && addr>=0 && p->nOp>addr && p->aOp ){
436 p->aOp[addr].p2 = val;
437 }
438}
439
drhd654be82005-09-20 17:42:23 +0000440/*
danielk19771f4aa332008-01-03 09:51:55 +0000441** Change the value of the P3 operand for a specific instruction.
danielk1977207872a2008-01-03 07:54:23 +0000442*/
443void sqlite3VdbeChangeP3(Vdbe *p, int addr, int val){
444 assert( p==0 || p->magic==VDBE_MAGIC_INIT );
445 if( p && addr>=0 && p->nOp>addr && p->aOp ){
446 p->aOp[addr].p3 = val;
447 }
448}
449
450/*
danielk19771f4aa332008-01-03 09:51:55 +0000451** Change the value of the P3 operand for a specific instruction.
452*/
453void sqlite3VdbeChangeP5(Vdbe *p, int addr, u8 val){
454 assert( p==0 || p->magic==VDBE_MAGIC_INIT );
455 if( p && addr>=0 && p->nOp>addr && p->aOp ){
456 p->aOp[addr].p5 = val;
457 }
458}
459
460/*
drhf8875402006-03-17 13:56:34 +0000461** Change the P2 operand of instruction addr so that it points to
drhd654be82005-09-20 17:42:23 +0000462** the address of the next instruction to be coded.
463*/
464void sqlite3VdbeJumpHere(Vdbe *p, int addr){
465 sqlite3VdbeChangeP2(p, addr, p->nOp);
466}
drhb38ad992005-09-16 00:27:01 +0000467
drhb7f6f682006-07-08 17:06:43 +0000468
469/*
470** If the input FuncDef structure is ephemeral, then free it. If
471** the FuncDef is not ephermal, then do nothing.
472*/
473static void freeEphemeralFunction(FuncDef *pDef){
474 if( pDef && (pDef->flags & SQLITE_FUNC_EPHEM)!=0 ){
drh17435752007-08-16 04:30:38 +0000475 sqlite3_free(pDef);
drhb7f6f682006-07-08 17:06:43 +0000476 }
477}
478
drhb38ad992005-09-16 00:27:01 +0000479/*
drh66a51672008-01-03 00:01:23 +0000480** Delete a P4 value if necessary.
drhb38ad992005-09-16 00:27:01 +0000481*/
drh66a51672008-01-03 00:01:23 +0000482static void freeP4(int p4type, void *p3){
drhb38ad992005-09-16 00:27:01 +0000483 if( p3 ){
drh66a51672008-01-03 00:01:23 +0000484 switch( p4type ){
485 case P4_REAL:
486 case P4_INT64:
487 case P4_MPRINTF:
488 case P4_DYNAMIC:
489 case P4_KEYINFO:
490 case P4_KEYINFO_HANDOFF: {
drh17435752007-08-16 04:30:38 +0000491 sqlite3_free(p3);
drhac1733d2005-09-17 17:58:22 +0000492 break;
493 }
drh66a51672008-01-03 00:01:23 +0000494 case P4_VDBEFUNC: {
drhac1733d2005-09-17 17:58:22 +0000495 VdbeFunc *pVdbeFunc = (VdbeFunc *)p3;
drhb7f6f682006-07-08 17:06:43 +0000496 freeEphemeralFunction(pVdbeFunc->pFunc);
drhac1733d2005-09-17 17:58:22 +0000497 sqlite3VdbeDeleteAuxData(pVdbeFunc, 0);
drh17435752007-08-16 04:30:38 +0000498 sqlite3_free(pVdbeFunc);
drhac1733d2005-09-17 17:58:22 +0000499 break;
500 }
drh66a51672008-01-03 00:01:23 +0000501 case P4_FUNCDEF: {
drhb7f6f682006-07-08 17:06:43 +0000502 freeEphemeralFunction((FuncDef*)p3);
503 break;
504 }
drh66a51672008-01-03 00:01:23 +0000505 case P4_MEM: {
drhac1733d2005-09-17 17:58:22 +0000506 sqlite3ValueFree((sqlite3_value*)p3);
507 break;
508 }
drhb38ad992005-09-16 00:27:01 +0000509 }
510 }
511}
512
513
drh9a324642003-09-06 20:12:01 +0000514/*
drhf8875402006-03-17 13:56:34 +0000515** Change N opcodes starting at addr to No-ops.
516*/
517void sqlite3VdbeChangeToNoop(Vdbe *p, int addr, int N){
danielk197792d4d7a2007-05-04 12:05:56 +0000518 if( p && p->aOp ){
519 VdbeOp *pOp = &p->aOp[addr];
520 while( N-- ){
drh66a51672008-01-03 00:01:23 +0000521 freeP4(pOp->p4type, pOp->p4.p);
danielk197792d4d7a2007-05-04 12:05:56 +0000522 memset(pOp, 0, sizeof(pOp[0]));
523 pOp->opcode = OP_Noop;
524 pOp++;
525 }
drhf8875402006-03-17 13:56:34 +0000526 }
527}
528
529/*
drh66a51672008-01-03 00:01:23 +0000530** Change the value of the P4 operand for a specific instruction.
drh9a324642003-09-06 20:12:01 +0000531** This routine is useful when a large program is loaded from a
danielk19774adee202004-05-08 08:23:19 +0000532** static array using sqlite3VdbeAddOpList but we want to make a
drh9a324642003-09-06 20:12:01 +0000533** few minor changes to the program.
534**
drh66a51672008-01-03 00:01:23 +0000535** If n>=0 then the P4 operand is dynamic, meaning that a copy of
drh17435752007-08-16 04:30:38 +0000536** the string is made into memory obtained from sqlite3_malloc().
drh66a51672008-01-03 00:01:23 +0000537** A value of n==0 means copy bytes of zP4 up to and including the
538** first null byte. If n>0 then copy n+1 bytes of zP4.
drh9a324642003-09-06 20:12:01 +0000539**
drh66a51672008-01-03 00:01:23 +0000540** If n==P4_KEYINFO it means that zP4 is a pointer to a KeyInfo structure.
danielk19771f55c052005-05-19 08:42:59 +0000541** A copy is made of the KeyInfo structure into memory obtained from
drh17435752007-08-16 04:30:38 +0000542** sqlite3_malloc, to be freed when the Vdbe is finalized.
drh66a51672008-01-03 00:01:23 +0000543** n==P4_KEYINFO_HANDOFF indicates that zP4 points to a KeyInfo structure
drh17435752007-08-16 04:30:38 +0000544** stored in memory that the caller has obtained from sqlite3_malloc. The
danielk19771f55c052005-05-19 08:42:59 +0000545** caller should not free the allocation, it will be freed when the Vdbe is
546** finalized.
547**
drh66a51672008-01-03 00:01:23 +0000548** Other values of n (P4_STATIC, P4_COLLSEQ etc.) indicate that zP4 points
danielk19771f55c052005-05-19 08:42:59 +0000549** to a string or structure that is guaranteed to exist for the lifetime of
550** the Vdbe. In these cases we can just copy the pointer.
drh9a324642003-09-06 20:12:01 +0000551**
drh66a51672008-01-03 00:01:23 +0000552** If addr<0 then change P4 on the most recently inserted instruction.
drh9a324642003-09-06 20:12:01 +0000553*/
drh66a51672008-01-03 00:01:23 +0000554void sqlite3VdbeChangeP4(Vdbe *p, int addr, const char *zP4, int n){
drh9a324642003-09-06 20:12:01 +0000555 Op *pOp;
drh8aa34ae2006-03-13 12:54:09 +0000556 assert( p==0 || p->magic==VDBE_MAGIC_INIT );
drh17435752007-08-16 04:30:38 +0000557 if( p==0 || p->aOp==0 || p->db->mallocFailed ){
drh66a51672008-01-03 00:01:23 +0000558 if (n != P4_KEYINFO) {
559 freeP4(n, (void*)*(char**)&zP4);
danielk1977261919c2005-12-06 12:52:59 +0000560 }
danielk1977d5d56522005-03-16 12:15:20 +0000561 return;
562 }
drh9a324642003-09-06 20:12:01 +0000563 if( addr<0 || addr>=p->nOp ){
564 addr = p->nOp - 1;
565 if( addr<0 ) return;
566 }
567 pOp = &p->aOp[addr];
drh66a51672008-01-03 00:01:23 +0000568 freeP4(pOp->p4type, pOp->p4.p);
569 pOp->p4.p = 0;
570 if( zP4==0 ){
571 pOp->p4.p = 0;
572 pOp->p4type = P4_NOTUSED;
573 }else if( n==P4_KEYINFO ){
drhd3d39e92004-05-20 22:16:29 +0000574 KeyInfo *pKeyInfo;
575 int nField, nByte;
drh4db38a72005-09-01 12:16:28 +0000576
drh66a51672008-01-03 00:01:23 +0000577 nField = ((KeyInfo*)zP4)->nField;
drhfdd6e852005-12-16 01:06:16 +0000578 nByte = sizeof(*pKeyInfo) + (nField-1)*sizeof(pKeyInfo->aColl[0]) + nField;
drh17435752007-08-16 04:30:38 +0000579 pKeyInfo = sqlite3_malloc( nByte );
danielk19772dca4ac2008-01-03 11:50:29 +0000580 pOp->p4.pKeyInfo = pKeyInfo;
drhd3d39e92004-05-20 22:16:29 +0000581 if( pKeyInfo ){
danielk1977bab45c62006-01-16 15:14:27 +0000582 unsigned char *aSortOrder;
drh66a51672008-01-03 00:01:23 +0000583 memcpy(pKeyInfo, zP4, nByte);
drhfdd6e852005-12-16 01:06:16 +0000584 aSortOrder = pKeyInfo->aSortOrder;
585 if( aSortOrder ){
danielk1977bab45c62006-01-16 15:14:27 +0000586 pKeyInfo->aSortOrder = (unsigned char*)&pKeyInfo->aColl[nField];
drhfdd6e852005-12-16 01:06:16 +0000587 memcpy(pKeyInfo->aSortOrder, aSortOrder, nField);
588 }
drh66a51672008-01-03 00:01:23 +0000589 pOp->p4type = P4_KEYINFO;
drhd3d39e92004-05-20 22:16:29 +0000590 }else{
drh17435752007-08-16 04:30:38 +0000591 p->db->mallocFailed = 1;
drh66a51672008-01-03 00:01:23 +0000592 pOp->p4type = P4_NOTUSED;
drhd3d39e92004-05-20 22:16:29 +0000593 }
drh66a51672008-01-03 00:01:23 +0000594 }else if( n==P4_KEYINFO_HANDOFF ){
danielk19772dca4ac2008-01-03 11:50:29 +0000595 pOp->p4.p = (void*)zP4;
drh66a51672008-01-03 00:01:23 +0000596 pOp->p4type = P4_KEYINFO;
drh9a324642003-09-06 20:12:01 +0000597 }else if( n<0 ){
danielk19772dca4ac2008-01-03 11:50:29 +0000598 pOp->p4.p = (void*)zP4;
drh66a51672008-01-03 00:01:23 +0000599 pOp->p4type = n;
drh9a324642003-09-06 20:12:01 +0000600 }else{
drh66a51672008-01-03 00:01:23 +0000601 if( n==0 ) n = strlen(zP4);
danielk19772dca4ac2008-01-03 11:50:29 +0000602 pOp->p4.z = sqlite3DbStrNDup(p->db, zP4, n);
drh66a51672008-01-03 00:01:23 +0000603 pOp->p4type = P4_DYNAMIC;
drh9a324642003-09-06 20:12:01 +0000604 }
605}
606
drhad6d9462004-09-19 02:15:24 +0000607#ifndef NDEBUG
608/*
drhd4e70eb2008-01-02 00:34:36 +0000609** Change the comment on the the most recently coded instruction.
drhad6d9462004-09-19 02:15:24 +0000610*/
611void sqlite3VdbeComment(Vdbe *p, const char *zFormat, ...){
612 va_list ap;
danielk197701256832007-04-18 14:24:32 +0000613 assert( p->nOp>0 || p->aOp==0 );
drhd4e70eb2008-01-02 00:34:36 +0000614 assert( p->aOp==0 || p->aOp[p->nOp-1].zComment==0 || p->db->mallocFailed );
drhad6d9462004-09-19 02:15:24 +0000615 va_start(ap, zFormat);
drhd4e70eb2008-01-02 00:34:36 +0000616 p->aOp[p->nOp-1].zComment = sqlite3VMPrintf(p->db, zFormat, ap);
drhad6d9462004-09-19 02:15:24 +0000617 va_end(ap);
618}
619#endif
620
drh9a324642003-09-06 20:12:01 +0000621/*
drh9a324642003-09-06 20:12:01 +0000622** Return the opcode for a given address.
623*/
danielk19774adee202004-05-08 08:23:19 +0000624VdbeOp *sqlite3VdbeGetOp(Vdbe *p, int addr){
drh9a324642003-09-06 20:12:01 +0000625 assert( p->magic==VDBE_MAGIC_INIT );
drh17435752007-08-16 04:30:38 +0000626 assert( (addr>=0 && addr<p->nOp) || p->db->mallocFailed );
danielk197701256832007-04-18 14:24:32 +0000627 return ((addr>=0 && addr<p->nOp)?(&p->aOp[addr]):0);
drh9a324642003-09-06 20:12:01 +0000628}
629
drhb7f91642004-10-31 02:22:47 +0000630#if !defined(SQLITE_OMIT_EXPLAIN) || !defined(NDEBUG) \
631 || defined(VDBE_PROFILE) || defined(SQLITE_DEBUG)
drh9a324642003-09-06 20:12:01 +0000632/*
drh66a51672008-01-03 00:01:23 +0000633** Compute a string that describes the P4 parameter for an opcode.
drhd3d39e92004-05-20 22:16:29 +0000634** Use zTemp for any required temporary buffer space.
635*/
drh66a51672008-01-03 00:01:23 +0000636static char *displayP4(Op *pOp, char *zTemp, int nTemp){
637 char *zP4 = zTemp;
drhd3d39e92004-05-20 22:16:29 +0000638 assert( nTemp>=20 );
drh66a51672008-01-03 00:01:23 +0000639 switch( pOp->p4type ){
640 case P4_KEYINFO: {
drhd3d39e92004-05-20 22:16:29 +0000641 int i, j;
danielk19772dca4ac2008-01-03 11:50:29 +0000642 KeyInfo *pKeyInfo = pOp->p4.pKeyInfo;
drh5bb3eb92007-05-04 13:15:55 +0000643 sqlite3_snprintf(nTemp, zTemp, "keyinfo(%d", pKeyInfo->nField);
drhd3d39e92004-05-20 22:16:29 +0000644 i = strlen(zTemp);
645 for(j=0; j<pKeyInfo->nField; j++){
646 CollSeq *pColl = pKeyInfo->aColl[j];
647 if( pColl ){
648 int n = strlen(pColl->zName);
649 if( i+n>nTemp-6 ){
drh5bb3eb92007-05-04 13:15:55 +0000650 memcpy(&zTemp[i],",...",4);
drhd3d39e92004-05-20 22:16:29 +0000651 break;
652 }
653 zTemp[i++] = ',';
drhffbc3082004-05-21 01:29:06 +0000654 if( pKeyInfo->aSortOrder && pKeyInfo->aSortOrder[j] ){
drhd3d39e92004-05-20 22:16:29 +0000655 zTemp[i++] = '-';
656 }
drh5bb3eb92007-05-04 13:15:55 +0000657 memcpy(&zTemp[i], pColl->zName,n+1);
drhd3d39e92004-05-20 22:16:29 +0000658 i += n;
659 }else if( i+4<nTemp-6 ){
drh5bb3eb92007-05-04 13:15:55 +0000660 memcpy(&zTemp[i],",nil",4);
drhd3d39e92004-05-20 22:16:29 +0000661 i += 4;
662 }
663 }
664 zTemp[i++] = ')';
665 zTemp[i] = 0;
666 assert( i<nTemp );
drhd3d39e92004-05-20 22:16:29 +0000667 break;
668 }
drh66a51672008-01-03 00:01:23 +0000669 case P4_COLLSEQ: {
danielk19772dca4ac2008-01-03 11:50:29 +0000670 CollSeq *pColl = pOp->p4.pColl;
drh5bb3eb92007-05-04 13:15:55 +0000671 sqlite3_snprintf(nTemp, zTemp, "collseq(%.20s)", pColl->zName);
drhd3d39e92004-05-20 22:16:29 +0000672 break;
673 }
drh66a51672008-01-03 00:01:23 +0000674 case P4_FUNCDEF: {
danielk19772dca4ac2008-01-03 11:50:29 +0000675 FuncDef *pDef = pOp->p4.pFunc;
drha967e882006-06-13 01:04:52 +0000676 sqlite3_snprintf(nTemp, zTemp, "%s(%d)", pDef->zName, pDef->nArg);
drhf9b596e2004-05-26 16:54:42 +0000677 break;
678 }
drh66a51672008-01-03 00:01:23 +0000679 case P4_INT64: {
danielk19772dca4ac2008-01-03 11:50:29 +0000680 sqlite3_snprintf(nTemp, zTemp, "%lld", *pOp->p4.pI64);
drhd4e70eb2008-01-02 00:34:36 +0000681 break;
682 }
drh66a51672008-01-03 00:01:23 +0000683 case P4_INT32: {
684 sqlite3_snprintf(nTemp, zTemp, "%d", pOp->p4.i);
drh598f1342007-10-23 15:39:45 +0000685 break;
686 }
drh66a51672008-01-03 00:01:23 +0000687 case P4_REAL: {
danielk19772dca4ac2008-01-03 11:50:29 +0000688 sqlite3_snprintf(nTemp, zTemp, "%.16g", *pOp->p4.pReal);
drhd4e70eb2008-01-02 00:34:36 +0000689 break;
690 }
drh66a51672008-01-03 00:01:23 +0000691 case P4_MEM: {
danielk19772dca4ac2008-01-03 11:50:29 +0000692 Mem *pMem = pOp->p4.pMem;
drhd4e70eb2008-01-02 00:34:36 +0000693 if( pMem->flags & MEM_Str ){
drh66a51672008-01-03 00:01:23 +0000694 zP4 = pMem->z;
drhd4e70eb2008-01-02 00:34:36 +0000695 }else if( pMem->flags & MEM_Int ){
696 sqlite3_snprintf(nTemp, zTemp, "%lld", pMem->u.i);
697 }else if( pMem->flags & MEM_Real ){
698 sqlite3_snprintf(nTemp, zTemp, "%.16g", pMem->r);
699 }else if( pMem->flags & MEM_Null ){
700 sqlite3_snprintf(nTemp, zTemp, "NULL");
701 }
drh598f1342007-10-23 15:39:45 +0000702 break;
703 }
drha967e882006-06-13 01:04:52 +0000704#ifndef SQLITE_OMIT_VIRTUALTABLE
drh66a51672008-01-03 00:01:23 +0000705 case P4_VTAB: {
danielk19772dca4ac2008-01-03 11:50:29 +0000706 sqlite3_vtab *pVtab = pOp->p4.pVtab;
drh19146192006-06-26 19:10:32 +0000707 sqlite3_snprintf(nTemp, zTemp, "vtab:%p:%p", pVtab, pVtab->pModule);
drha967e882006-06-13 01:04:52 +0000708 break;
709 }
710#endif
drhd3d39e92004-05-20 22:16:29 +0000711 default: {
danielk19772dca4ac2008-01-03 11:50:29 +0000712 zP4 = pOp->p4.z;
drh66a51672008-01-03 00:01:23 +0000713 if( zP4==0 || pOp->opcode==OP_Noop ){
714 zP4 = zTemp;
drhd4e70eb2008-01-02 00:34:36 +0000715 zTemp[0] = 0;
drhd3d39e92004-05-20 22:16:29 +0000716 }
717 }
718 }
drh66a51672008-01-03 00:01:23 +0000719 assert( zP4!=0 );
drh66a51672008-01-03 00:01:23 +0000720 return zP4;
drhd3d39e92004-05-20 22:16:29 +0000721}
drhb7f91642004-10-31 02:22:47 +0000722#endif
drhd3d39e92004-05-20 22:16:29 +0000723
drh900b31e2007-08-28 02:27:51 +0000724/*
drhd0679ed2007-08-28 22:24:34 +0000725** Declare to the Vdbe that the BTree object at db->aDb[i] is used.
726**
drh900b31e2007-08-28 02:27:51 +0000727*/
drhfb982642007-08-30 01:19:59 +0000728void sqlite3VdbeUsesBtree(Vdbe *p, int i){
729 int mask;
drhd0679ed2007-08-28 22:24:34 +0000730 assert( i>=0 && i<p->db->nDb );
731 assert( i<sizeof(p->btreeMask)*8 );
drhfb982642007-08-30 01:19:59 +0000732 mask = 1<<i;
733 if( (p->btreeMask & mask)==0 ){
734 p->btreeMask |= mask;
735 sqlite3BtreeMutexArrayInsert(&p->aMutex, p->db->aDb[i].pBt);
736 }
drh900b31e2007-08-28 02:27:51 +0000737}
738
drhd3d39e92004-05-20 22:16:29 +0000739
danielk19778b60e0f2005-01-12 09:10:39 +0000740#if defined(VDBE_PROFILE) || defined(SQLITE_DEBUG)
drh9a324642003-09-06 20:12:01 +0000741/*
742** Print a single opcode. This routine is used for debugging only.
743*/
danielk19774adee202004-05-08 08:23:19 +0000744void sqlite3VdbePrintOp(FILE *pOut, int pc, Op *pOp){
drh66a51672008-01-03 00:01:23 +0000745 char *zP4;
drhd3d39e92004-05-20 22:16:29 +0000746 char zPtr[50];
danielk197711641c12008-01-03 08:18:30 +0000747 static const char *zFormat1 = "%4d %-13s %4d %4d %4d %-4s %.2X\n";
drh9a324642003-09-06 20:12:01 +0000748 if( pOut==0 ) pOut = stdout;
drh66a51672008-01-03 00:01:23 +0000749 zP4 = displayP4(pOp, zPtr, sizeof(zPtr));
danielk197711641c12008-01-03 08:18:30 +0000750 fprintf(pOut, zFormat1, pc,
751 sqlite3OpcodeName(pOp->opcode), pOp->p1, pOp->p2, pOp->p3, zP4, pOp->p5);
drh9a324642003-09-06 20:12:01 +0000752 fflush(pOut);
753}
754#endif
755
756/*
drh76ff3a02004-09-24 22:32:30 +0000757** Release an array of N Mem elements
758*/
759static void releaseMemArray(Mem *p, int N){
760 if( p ){
761 while( N-->0 ){
drhb21c8cd2007-08-21 19:33:56 +0000762 assert( N<2 || p[0].db==p[1].db );
drh76ff3a02004-09-24 22:32:30 +0000763 sqlite3VdbeMemRelease(p++);
764 }
765 }
766}
767
drhb7f91642004-10-31 02:22:47 +0000768#ifndef SQLITE_OMIT_EXPLAIN
drh76ff3a02004-09-24 22:32:30 +0000769/*
drh9a324642003-09-06 20:12:01 +0000770** Give a listing of the program in the virtual machine.
771**
danielk19774adee202004-05-08 08:23:19 +0000772** The interface is the same as sqlite3VdbeExec(). But instead of
drh9a324642003-09-06 20:12:01 +0000773** running the code, it invokes the callback once for each instruction.
774** This feature is used to implement "EXPLAIN".
775*/
danielk19774adee202004-05-08 08:23:19 +0000776int sqlite3VdbeList(
drh9a324642003-09-06 20:12:01 +0000777 Vdbe *p /* The VDBE */
778){
drh9bb575f2004-09-06 17:24:11 +0000779 sqlite3 *db = p->db;
drh9a324642003-09-06 20:12:01 +0000780 int i;
drh826fb5a2004-02-14 23:59:57 +0000781 int rc = SQLITE_OK;
drh9a324642003-09-06 20:12:01 +0000782
drh9a324642003-09-06 20:12:01 +0000783 assert( p->explain );
drhc5cdca62005-01-11 16:54:14 +0000784 if( p->magic!=VDBE_MAGIC_RUN ) return SQLITE_MISUSE;
785 assert( db->magic==SQLITE_MAGIC_BUSY );
786 assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY );
danielk197718f41892004-05-22 07:27:46 +0000787
788 /* Even though this opcode does not put dynamic strings onto the
789 ** the stack, they may become dynamic if the user calls
drh4f26d6c2004-05-26 23:25:30 +0000790 ** sqlite3_column_text16(), causing a translation to UTF-16 encoding.
danielk197718f41892004-05-22 07:27:46 +0000791 */
drhd4e70eb2008-01-02 00:34:36 +0000792 if( p->pResultSet ){
793 releaseMemArray(p->pResultSet, 5);
794 p->pResultSet = 0;
danielk197718f41892004-05-22 07:27:46 +0000795 }
danielk197718f41892004-05-22 07:27:46 +0000796
drhecc92422005-09-10 16:46:12 +0000797 do{
798 i = p->pc++;
799 }while( i<p->nOp && p->explain==2 && p->aOp[i].opcode!=OP_Explain );
drh826fb5a2004-02-14 23:59:57 +0000800 if( i>=p->nOp ){
801 p->rc = SQLITE_OK;
802 rc = SQLITE_DONE;
drh881feaa2006-07-26 01:39:30 +0000803 }else if( db->u1.isInterrupted ){
drhc5cdca62005-01-11 16:54:14 +0000804 p->rc = SQLITE_INTERRUPT;
drh826fb5a2004-02-14 23:59:57 +0000805 rc = SQLITE_ERROR;
danielk1977f20b21c2004-05-31 23:56:42 +0000806 sqlite3SetString(&p->zErrMsg, sqlite3ErrStr(p->rc), (char*)0);
drh826fb5a2004-02-14 23:59:57 +0000807 }else{
drhd3d39e92004-05-20 22:16:29 +0000808 Op *pOp = &p->aOp[i];
drhd4e70eb2008-01-02 00:34:36 +0000809 Mem *pMem = p->pResultSet = p->aStack;
danielk19770d78bae2008-01-03 07:09:48 +0000810 if( p->explain==1 ){
811 pMem->flags = MEM_Int;
812 pMem->type = SQLITE_INTEGER;
813 pMem->u.i = i; /* Program counter */
814 pMem++;
815
816 pMem->flags = MEM_Static|MEM_Str|MEM_Term;
817 pMem->z = (char*)sqlite3OpcodeName(pOp->opcode); /* Opcode */
818 assert( pMem->z!=0 );
819 pMem->n = strlen(pMem->z);
820 pMem->type = SQLITE_TEXT;
821 pMem->enc = SQLITE_UTF8;
822 pMem++;
823 }
drheb2e1762004-05-27 01:53:56 +0000824
825 pMem->flags = MEM_Int;
drh3c024d62007-03-30 11:23:45 +0000826 pMem->u.i = pOp->p1; /* P1 */
drh9c054832004-05-31 18:51:57 +0000827 pMem->type = SQLITE_INTEGER;
drheb2e1762004-05-27 01:53:56 +0000828 pMem++;
829
830 pMem->flags = MEM_Int;
drh3c024d62007-03-30 11:23:45 +0000831 pMem->u.i = pOp->p2; /* P2 */
drh9c054832004-05-31 18:51:57 +0000832 pMem->type = SQLITE_INTEGER;
drheb2e1762004-05-27 01:53:56 +0000833 pMem++;
834
danielk19770d78bae2008-01-03 07:09:48 +0000835 if( p->explain==1 ){
836 pMem->flags = MEM_Int;
837 pMem->u.i = pOp->p3; /* P3 */
838 pMem->type = SQLITE_INTEGER;
839 pMem++;
840 }
841
drh66a51672008-01-03 00:01:23 +0000842 pMem->flags = MEM_Ephem|MEM_Str|MEM_Term; /* P4 */
843 pMem->z = displayP4(pOp, pMem->zShort, sizeof(pMem->zShort));
drhbdd88bd2006-06-15 13:22:22 +0000844 assert( pMem->z!=0 );
drhb8067982006-03-03 21:38:03 +0000845 pMem->n = strlen(pMem->z);
drh9c054832004-05-31 18:51:57 +0000846 pMem->type = SQLITE_TEXT;
danielk1977dc8453f2004-06-12 00:42:34 +0000847 pMem->enc = SQLITE_UTF8;
danielk19770d78bae2008-01-03 07:09:48 +0000848 pMem++;
drheb2e1762004-05-27 01:53:56 +0000849
danielk19770d78bae2008-01-03 07:09:48 +0000850 if( p->explain==1 ){
851 pMem->flags = MEM_Str|MEM_Term|MEM_Short;
852 pMem->n = sprintf(pMem->zShort, "%.2x", pOp->p5); /* P5 */
853 pMem->z = pMem->zShort;
854 pMem->type = SQLITE_TEXT;
855 pMem->enc = SQLITE_UTF8;
856 pMem++;
857
858 pMem->flags = MEM_Null; /* Comment */
859 if( pOp->zComment ){
860 pMem->flags = MEM_Str|MEM_Term;
861 pMem->z = pOp->zComment;
862 pMem->n = strlen(pMem->z);
863 pMem->enc = SQLITE_UTF8;
864 }
865 }
866
867 p->nResColumn = 8 - 5*(p->explain-1);
drheb2e1762004-05-27 01:53:56 +0000868 p->pTos = pMem;
drh826fb5a2004-02-14 23:59:57 +0000869 p->rc = SQLITE_OK;
870 rc = SQLITE_ROW;
drh9a324642003-09-06 20:12:01 +0000871 }
drh826fb5a2004-02-14 23:59:57 +0000872 return rc;
drh9a324642003-09-06 20:12:01 +0000873}
drhb7f91642004-10-31 02:22:47 +0000874#endif /* SQLITE_OMIT_EXPLAIN */
drh9a324642003-09-06 20:12:01 +0000875
drh7c4ac0c2007-04-05 11:25:58 +0000876#ifdef SQLITE_DEBUG
drh9a324642003-09-06 20:12:01 +0000877/*
drh3f7d4e42004-07-24 14:35:58 +0000878** Print the SQL that was used to generate a VDBE program.
879*/
880void sqlite3VdbePrintSql(Vdbe *p){
drh3f7d4e42004-07-24 14:35:58 +0000881 int nOp = p->nOp;
882 VdbeOp *pOp;
drhc16a03b2004-09-15 13:38:10 +0000883 if( nOp<1 ) return;
884 pOp = &p->aOp[nOp-1];
danielk19772dca4ac2008-01-03 11:50:29 +0000885 if( pOp->opcode==OP_Noop && pOp->p4.z!=0 ){
886 const char *z = pOp->p4.z;
drh4c755c02004-08-08 20:22:17 +0000887 while( isspace(*(u8*)z) ) z++;
drh3f7d4e42004-07-24 14:35:58 +0000888 printf("SQL: [%s]\n", z);
889 }
drh3f7d4e42004-07-24 14:35:58 +0000890}
drh7c4ac0c2007-04-05 11:25:58 +0000891#endif
drh3f7d4e42004-07-24 14:35:58 +0000892
drh602c2372007-03-01 00:29:13 +0000893#if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE)
894/*
895** Print an IOTRACE message showing SQL content.
896*/
897void sqlite3VdbeIOTraceSql(Vdbe *p){
898 int nOp = p->nOp;
899 VdbeOp *pOp;
900 if( sqlite3_io_trace==0 ) return;
901 if( nOp<1 ) return;
902 pOp = &p->aOp[nOp-1];
drh66a51672008-01-03 00:01:23 +0000903 if( pOp->opcode==OP_Noop && pOp->p4.p!=0 ){
drh602c2372007-03-01 00:29:13 +0000904 int i, j;
drh00a18e42007-08-13 11:10:34 +0000905 char z[1000];
drh66a51672008-01-03 00:01:23 +0000906 sqlite3_snprintf(sizeof(z), z, "%s", pOp->p4.p);
danielk19772be2be92007-05-16 17:50:45 +0000907 for(i=0; isspace((unsigned char)z[i]); i++){}
drh602c2372007-03-01 00:29:13 +0000908 for(j=0; z[i]; i++){
danielk19772be2be92007-05-16 17:50:45 +0000909 if( isspace((unsigned char)z[i]) ){
drh602c2372007-03-01 00:29:13 +0000910 if( z[i-1]!=' ' ){
911 z[j++] = ' ';
912 }
913 }else{
914 z[j++] = z[i];
915 }
916 }
917 z[j] = 0;
918 sqlite3_io_trace("SQL %s\n", z);
drh602c2372007-03-01 00:29:13 +0000919 }
920}
921#endif /* !SQLITE_OMIT_TRACE && SQLITE_ENABLE_IOTRACE */
922
923
drh3f7d4e42004-07-24 14:35:58 +0000924/*
drh9a324642003-09-06 20:12:01 +0000925** Prepare a virtual machine for execution. This involves things such
926** as allocating stack space and initializing the program counter.
927** After the VDBE has be prepped, it can be executed by one or more
danielk19774adee202004-05-08 08:23:19 +0000928** calls to sqlite3VdbeExec().
drh92f02c32004-09-02 14:57:08 +0000929**
930** This is the only way to move a VDBE from VDBE_MAGIC_INIT to
931** VDBE_MAGIC_RUN.
drh9a324642003-09-06 20:12:01 +0000932*/
danielk19774adee202004-05-08 08:23:19 +0000933void sqlite3VdbeMakeReady(
drh9a324642003-09-06 20:12:01 +0000934 Vdbe *p, /* The VDBE */
drh7c972de2003-09-06 22:18:07 +0000935 int nVar, /* Number of '?' see in the SQL statement */
drh290c1942004-08-21 17:54:45 +0000936 int nMem, /* Number of memory cells to allocate */
937 int nCursor, /* Number of cursors to allocate */
drh9a324642003-09-06 20:12:01 +0000938 int isExplain /* True if the EXPLAIN keywords is present */
939){
940 int n;
danielk19771e536952007-08-16 10:09:01 +0000941 sqlite3 *db = p->db;
drh9a324642003-09-06 20:12:01 +0000942
943 assert( p!=0 );
drh9a324642003-09-06 20:12:01 +0000944 assert( p->magic==VDBE_MAGIC_INIT );
945
drhc16a03b2004-09-15 13:38:10 +0000946 /* There should be at least one opcode.
drh9a324642003-09-06 20:12:01 +0000947 */
drhc16a03b2004-09-15 13:38:10 +0000948 assert( p->nOp>0 );
drh9a324642003-09-06 20:12:01 +0000949
danielk1977634f2982005-03-28 08:44:07 +0000950 /* Set the magic to VDBE_MAGIC_RUN sooner rather than later. This
951 * is because the call to resizeOpArray() below may shrink the
952 * p->aOp[] array to save memory if called when in VDBE_MAGIC_RUN
953 * state.
954 */
955 p->magic = VDBE_MAGIC_RUN;
956
drh9a324642003-09-06 20:12:01 +0000957 /* No instruction ever pushes more than a single element onto the
958 ** stack. And the stack never grows on successive executions of the
959 ** same loop. So the total number of instructions is an upper bound
drh38449902005-06-07 01:43:41 +0000960 ** on the maximum stack depth required. (Added later:) The
961 ** resolveP2Values() call computes a tighter upper bound on the
962 ** stack size.
drh9a324642003-09-06 20:12:01 +0000963 **
964 ** Allocation all the stack space we will ever need.
965 */
drh82a48512003-09-06 22:45:20 +0000966 if( p->aStack==0 ){
danielk1977634f2982005-03-28 08:44:07 +0000967 int nArg; /* Maximum number of args passed to a user function. */
danielk1977bc04f852005-03-29 08:26:13 +0000968 int nStack; /* Maximum number of stack entries required */
969 resolveP2Values(p, &nArg, &nStack);
danielk1977634f2982005-03-28 08:44:07 +0000970 resizeOpArray(p, p->nOp);
drh82a48512003-09-06 22:45:20 +0000971 assert( nVar>=0 );
danielk1977bc04f852005-03-29 08:26:13 +0000972 assert( nStack<p->nOp );
drh0f7eb612006-08-08 13:51:43 +0000973 if( isExplain ){
drh0a07c102008-01-03 18:03:08 +0000974 nStack = 16;
drh0f7eb612006-08-08 13:51:43 +0000975 }
danielk19771e536952007-08-16 10:09:01 +0000976 p->aStack = sqlite3DbMallocZero(db,
danielk1977bc04f852005-03-29 08:26:13 +0000977 nStack*sizeof(p->aStack[0]) /* aStack */
danielk1977634f2982005-03-28 08:44:07 +0000978 + nArg*sizeof(Mem*) /* apArg */
drh86f43302004-10-05 17:37:36 +0000979 + nVar*sizeof(Mem) /* aVar */
980 + nVar*sizeof(char*) /* azVar */
981 + nMem*sizeof(Mem) /* aMem */
drh0a07c102008-01-03 18:03:08 +0000982 + nCursor*sizeof(Cursor*) + 1 /* apCsr */
drh82a48512003-09-06 22:45:20 +0000983 );
drh17435752007-08-16 04:30:38 +0000984 if( !db->mallocFailed ){
drh0a07c102008-01-03 18:03:08 +0000985 p->aMem = &p->aStack[nStack-1]; /* aMem[] goes from 1..nMem */
986 p->nMem = nMem; /* not from 0..nMem-1 */
987 p->aVar = &p->aMem[nMem+1];
drh86f43302004-10-05 17:37:36 +0000988 p->nVar = nVar;
989 p->okVar = 0;
990 p->apArg = (Mem**)&p->aVar[nVar];
danielk1977634f2982005-03-28 08:44:07 +0000991 p->azVar = (char**)&p->apArg[nArg];
drh86f43302004-10-05 17:37:36 +0000992 p->apCsr = (Cursor**)&p->azVar[nVar];
drh290c1942004-08-21 17:54:45 +0000993 p->nCursor = nCursor;
994 for(n=0; n<nVar; n++){
995 p->aVar[n].flags = MEM_Null;
drhb21c8cd2007-08-21 19:33:56 +0000996 p->aVar[n].db = db;
997 }
998 for(n=0; n<nStack; n++){
999 p->aStack[n].db = db;
drh290c1942004-08-21 17:54:45 +00001000 }
danielk197754db47e2004-05-19 10:36:43 +00001001 }
drh82a48512003-09-06 22:45:20 +00001002 }
drh0a07c102008-01-03 18:03:08 +00001003 for(n=1; n<=p->nMem; n++){
danielk1977b3bce662005-01-29 08:32:43 +00001004 p->aMem[n].flags = MEM_Null;
drhb21c8cd2007-08-21 19:33:56 +00001005 p->aMem[n].db = db;
danielk1977b3bce662005-01-29 08:32:43 +00001006 }
drh9a324642003-09-06 20:12:01 +00001007
drh6810ce62004-01-31 19:22:56 +00001008 p->pTos = &p->aStack[-1];
danielk19771d850a72004-05-31 08:26:49 +00001009 p->pc = -1;
drh9a324642003-09-06 20:12:01 +00001010 p->rc = SQLITE_OK;
1011 p->uniqueCnt = 0;
1012 p->returnDepth = 0;
1013 p->errorAction = OE_Abort;
drh9a324642003-09-06 20:12:01 +00001014 p->popStack = 0;
1015 p->explain |= isExplain;
1016 p->magic = VDBE_MAGIC_RUN;
danielk1977b28af712004-06-21 06:50:26 +00001017 p->nChange = 0;
drh76873ab2006-01-07 18:48:26 +00001018 p->cacheCtr = 1;
drhd946db02005-12-29 19:23:06 +00001019 p->minWriteFileFormat = 255;
danielk1977182c4ba2007-06-27 15:53:34 +00001020 p->openedStatement = 0;
drh9a324642003-09-06 20:12:01 +00001021#ifdef VDBE_PROFILE
drhcf64d8b2003-12-31 17:57:10 +00001022 {
1023 int i;
1024 for(i=0; i<p->nOp; i++){
1025 p->aOp[i].cnt = 0;
1026 p->aOp[i].cycles = 0;
1027 }
drh9a324642003-09-06 20:12:01 +00001028 }
1029#endif
1030}
1031
drh9a324642003-09-06 20:12:01 +00001032/*
drhff0587c2007-08-29 17:43:19 +00001033** Close a VDBE cursor and release all the resources that cursor happens
drh9a324642003-09-06 20:12:01 +00001034** to hold.
1035*/
danielk1977be718892006-06-23 08:05:19 +00001036void sqlite3VdbeFreeCursor(Vdbe *p, Cursor *pCx){
drh4774b132004-06-12 20:12:51 +00001037 if( pCx==0 ){
1038 return;
1039 }
drh9a324642003-09-06 20:12:01 +00001040 if( pCx->pCursor ){
danielk19774adee202004-05-08 08:23:19 +00001041 sqlite3BtreeCloseCursor(pCx->pCursor);
drh9a324642003-09-06 20:12:01 +00001042 }
1043 if( pCx->pBt ){
danielk19774adee202004-05-08 08:23:19 +00001044 sqlite3BtreeClose(pCx->pBt);
drh9a324642003-09-06 20:12:01 +00001045 }
drh9eff6162006-06-12 21:59:13 +00001046#ifndef SQLITE_OMIT_VIRTUALTABLE
1047 if( pCx->pVtabCursor ){
1048 sqlite3_vtab_cursor *pVtabCursor = pCx->pVtabCursor;
danielk1977be718892006-06-23 08:05:19 +00001049 const sqlite3_module *pModule = pCx->pModule;
1050 p->inVtabMethod = 1;
danielk19775bd270b2006-07-25 15:14:52 +00001051 sqlite3SafetyOff(p->db);
drh9eff6162006-06-12 21:59:13 +00001052 pModule->xClose(pVtabCursor);
danielk19775bd270b2006-07-25 15:14:52 +00001053 sqlite3SafetyOn(p->db);
danielk1977be718892006-06-23 08:05:19 +00001054 p->inVtabMethod = 0;
drh9eff6162006-06-12 21:59:13 +00001055 }
1056#endif
drh17435752007-08-16 04:30:38 +00001057 sqlite3_free(pCx->pData);
1058 sqlite3_free(pCx->aType);
1059 sqlite3_free(pCx);
drh9a324642003-09-06 20:12:01 +00001060}
1061
1062/*
drhff0587c2007-08-29 17:43:19 +00001063** Close all cursors except for VTab cursors that are currently
1064** in use.
drh9a324642003-09-06 20:12:01 +00001065*/
drhff0587c2007-08-29 17:43:19 +00001066static void closeAllCursorsExceptActiveVtabs(Vdbe *p){
drh9a324642003-09-06 20:12:01 +00001067 int i;
drh290c1942004-08-21 17:54:45 +00001068 if( p->apCsr==0 ) return;
drh9a324642003-09-06 20:12:01 +00001069 for(i=0; i<p->nCursor; i++){
drhff0587c2007-08-29 17:43:19 +00001070 Cursor *pC = p->apCsr[i];
1071 if( pC && (!p->inVtabMethod || !pC->pVtabCursor) ){
1072 sqlite3VdbeFreeCursor(p, pC);
danielk1977b7a2f2e2006-06-23 11:34:54 +00001073 p->apCsr[i] = 0;
danielk1977be718892006-06-23 08:05:19 +00001074 }
drh9a324642003-09-06 20:12:01 +00001075 }
drh9a324642003-09-06 20:12:01 +00001076}
1077
1078/*
drh9a324642003-09-06 20:12:01 +00001079** Clean up the VM after execution.
1080**
1081** This routine will automatically close any cursors, lists, and/or
1082** sorters that were left open. It also deletes the values of
drh5a12e682004-05-19 11:24:25 +00001083** variables in the aVar[] array.
drh9a324642003-09-06 20:12:01 +00001084*/
1085static void Cleanup(Vdbe *p){
1086 int i;
drh6810ce62004-01-31 19:22:56 +00001087 if( p->aStack ){
drh76ff3a02004-09-24 22:32:30 +00001088 releaseMemArray(p->aStack, 1 + (p->pTos - p->aStack));
1089 p->pTos = &p->aStack[-1];
drh6810ce62004-01-31 19:22:56 +00001090 }
drhff0587c2007-08-29 17:43:19 +00001091 closeAllCursorsExceptActiveVtabs(p);
drh0a07c102008-01-03 18:03:08 +00001092 releaseMemArray(&p->aMem[1], p->nMem);
drha01f79d2005-07-08 13:07:59 +00001093 sqlite3VdbeFifoClear(&p->sFifo);
drh76ff3a02004-09-24 22:32:30 +00001094 if( p->contextStack ){
1095 for(i=0; i<p->contextStackTop; i++){
drha01f79d2005-07-08 13:07:59 +00001096 sqlite3VdbeFifoClear(&p->contextStack[i].sFifo);
drh76ff3a02004-09-24 22:32:30 +00001097 }
drh17435752007-08-16 04:30:38 +00001098 sqlite3_free(p->contextStack);
drh344737f2004-09-19 00:50:20 +00001099 }
drh5f968432004-02-21 19:02:30 +00001100 p->contextStack = 0;
drh344737f2004-09-19 00:50:20 +00001101 p->contextStackDepth = 0;
1102 p->contextStackTop = 0;
drh17435752007-08-16 04:30:38 +00001103 sqlite3_free(p->zErrMsg);
drh9a324642003-09-06 20:12:01 +00001104 p->zErrMsg = 0;
drhd4e70eb2008-01-02 00:34:36 +00001105 p->pResultSet = 0;
drh9a324642003-09-06 20:12:01 +00001106}
1107
1108/*
danielk197722322fd2004-05-25 23:35:17 +00001109** Set the number of result columns that will be returned by this SQL
1110** statement. This is now set at compile time, rather than during
1111** execution of the vdbe program so that sqlite3_column_count() can
1112** be called on an SQL statement before sqlite3_step().
1113*/
1114void sqlite3VdbeSetNumCols(Vdbe *p, int nResColumn){
drh76ff3a02004-09-24 22:32:30 +00001115 Mem *pColName;
1116 int n;
drh4a50aac2007-08-23 02:47:53 +00001117
danielk1977955de522006-02-10 02:27:42 +00001118 releaseMemArray(p->aColName, p->nResColumn*COLNAME_N);
drh17435752007-08-16 04:30:38 +00001119 sqlite3_free(p->aColName);
danielk1977955de522006-02-10 02:27:42 +00001120 n = nResColumn*COLNAME_N;
drhcc43cab2005-10-05 11:35:09 +00001121 p->nResColumn = nResColumn;
danielk19771e536952007-08-16 10:09:01 +00001122 p->aColName = pColName = (Mem*)sqlite3DbMallocZero(p->db, sizeof(Mem)*n );
drh76ff3a02004-09-24 22:32:30 +00001123 if( p->aColName==0 ) return;
1124 while( n-- > 0 ){
drh4a50aac2007-08-23 02:47:53 +00001125 pColName->flags = MEM_Null;
drh153c62c2007-08-24 03:51:33 +00001126 pColName->db = p->db;
drh4a50aac2007-08-23 02:47:53 +00001127 pColName++;
drh76ff3a02004-09-24 22:32:30 +00001128 }
danielk197722322fd2004-05-25 23:35:17 +00001129}
1130
1131/*
danielk19773cf86062004-05-26 10:11:05 +00001132** Set the name of the idx'th column to be returned by the SQL statement.
1133** zName must be a pointer to a nul terminated string.
1134**
1135** This call must be made after a call to sqlite3VdbeSetNumCols().
1136**
drh66a51672008-01-03 00:01:23 +00001137** If N==P4_STATIC it means that zName is a pointer to a constant static
1138** string and we can just copy the pointer. If it is P4_DYNAMIC, then
drh17435752007-08-16 04:30:38 +00001139** the string is freed using sqlite3_free() when the vdbe is finished with
danielk1977d8123362004-06-12 09:25:12 +00001140** it. Otherwise, N bytes of zName are copied.
danielk19773cf86062004-05-26 10:11:05 +00001141*/
danielk1977955de522006-02-10 02:27:42 +00001142int sqlite3VdbeSetColName(Vdbe *p, int idx, int var, const char *zName, int N){
danielk19773cf86062004-05-26 10:11:05 +00001143 int rc;
1144 Mem *pColName;
danielk1977955de522006-02-10 02:27:42 +00001145 assert( idx<p->nResColumn );
1146 assert( var<COLNAME_N );
drh17435752007-08-16 04:30:38 +00001147 if( p->db->mallocFailed ) return SQLITE_NOMEM;
drh76ff3a02004-09-24 22:32:30 +00001148 assert( p->aColName!=0 );
danielk1977955de522006-02-10 02:27:42 +00001149 pColName = &(p->aColName[idx+var*p->nResColumn]);
drh66a51672008-01-03 00:01:23 +00001150 if( N==P4_DYNAMIC || N==P4_STATIC ){
drhb21c8cd2007-08-21 19:33:56 +00001151 rc = sqlite3VdbeMemSetStr(pColName, zName, -1, SQLITE_UTF8, SQLITE_STATIC);
danielk19773cf86062004-05-26 10:11:05 +00001152 }else{
drhb21c8cd2007-08-21 19:33:56 +00001153 rc = sqlite3VdbeMemSetStr(pColName, zName, N, SQLITE_UTF8,SQLITE_TRANSIENT);
danielk19773cf86062004-05-26 10:11:05 +00001154 }
drh66a51672008-01-03 00:01:23 +00001155 if( rc==SQLITE_OK && N==P4_DYNAMIC ){
danielk19773cf86062004-05-26 10:11:05 +00001156 pColName->flags = (pColName->flags&(~MEM_Static))|MEM_Dyn;
danielk1977d8123362004-06-12 09:25:12 +00001157 pColName->xDel = 0;
danielk19773cf86062004-05-26 10:11:05 +00001158 }
1159 return rc;
1160}
1161
danielk197713adf8a2004-06-03 16:08:41 +00001162/*
1163** A read or write transaction may or may not be active on database handle
1164** db. If a transaction is active, commit it. If there is a
1165** write-transaction spanning more than one database file, this routine
1166** takes care of the master journal trickery.
1167*/
drh9bb575f2004-09-06 17:24:11 +00001168static int vdbeCommit(sqlite3 *db){
danielk197713adf8a2004-06-03 16:08:41 +00001169 int i;
1170 int nTrans = 0; /* Number of databases with an active write-transaction */
1171 int rc = SQLITE_OK;
1172 int needXcommit = 0;
1173
danielk19775bd270b2006-07-25 15:14:52 +00001174 /* Before doing anything else, call the xSync() callback for any
1175 ** virtual module tables written in this transaction. This has to
1176 ** be done before determining whether a master journal file is
1177 ** required, as an xSync() callback may add an attached database
1178 ** to the transaction.
1179 */
1180 rc = sqlite3VtabSync(db, rc);
1181 if( rc!=SQLITE_OK ){
1182 return rc;
1183 }
1184
1185 /* This loop determines (a) if the commit hook should be invoked and
1186 ** (b) how many database files have open write transactions, not
1187 ** including the temp database. (b) is important because if more than
1188 ** one database file has an open write transaction, a master journal
1189 ** file is required for an atomic commit.
1190 */
danielk197713adf8a2004-06-03 16:08:41 +00001191 for(i=0; i<db->nDb; i++){
1192 Btree *pBt = db->aDb[i].pBt;
drhd0679ed2007-08-28 22:24:34 +00001193 if( sqlite3BtreeIsInTrans(pBt) ){
danielk197713adf8a2004-06-03 16:08:41 +00001194 needXcommit = 1;
1195 if( i!=1 ) nTrans++;
1196 }
1197 }
1198
1199 /* If there are any write-transactions at all, invoke the commit hook */
1200 if( needXcommit && db->xCommitCallback ){
drh92f02c32004-09-02 14:57:08 +00001201 sqlite3SafetyOff(db);
1202 rc = db->xCommitCallback(db->pCommitArg);
1203 sqlite3SafetyOn(db);
1204 if( rc ){
danielk197713adf8a2004-06-03 16:08:41 +00001205 return SQLITE_CONSTRAINT;
1206 }
1207 }
1208
danielk197740b38dc2004-06-26 08:38:24 +00001209 /* The simple case - no more than one database file (not counting the
1210 ** TEMP database) has a transaction active. There is no need for the
drh2ac3ee92004-06-07 16:27:46 +00001211 ** master-journal.
drhc9e06862004-06-09 20:03:08 +00001212 **
danielk197740b38dc2004-06-26 08:38:24 +00001213 ** If the return value of sqlite3BtreeGetFilename() is a zero length
1214 ** string, it means the main database is :memory:. In that case we do
1215 ** not support atomic multi-file commits, so use the simple case then
drhc9e06862004-06-09 20:03:08 +00001216 ** too.
danielk197713adf8a2004-06-03 16:08:41 +00001217 */
danielk197740b38dc2004-06-26 08:38:24 +00001218 if( 0==strlen(sqlite3BtreeGetFilename(db->aDb[0].pBt)) || nTrans<=1 ){
drh2ac3ee92004-06-07 16:27:46 +00001219 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
danielk197713adf8a2004-06-03 16:08:41 +00001220 Btree *pBt = db->aDb[i].pBt;
1221 if( pBt ){
drh80e35f42007-03-30 14:06:34 +00001222 rc = sqlite3BtreeCommitPhaseOne(pBt, 0);
drh2ac3ee92004-06-07 16:27:46 +00001223 }
1224 }
1225
drh80e35f42007-03-30 14:06:34 +00001226 /* Do the commit only if all databases successfully complete phase 1.
1227 ** If one of the BtreeCommitPhaseOne() calls fails, this indicates an
1228 ** IO error while deleting or truncating a journal file. It is unlikely,
1229 ** but could happen. In this case abandon processing and return the error.
danielk1977979f38e2007-03-27 16:19:51 +00001230 */
1231 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
1232 Btree *pBt = db->aDb[i].pBt;
1233 if( pBt ){
drh80e35f42007-03-30 14:06:34 +00001234 rc = sqlite3BtreeCommitPhaseTwo(pBt);
danielk197713adf8a2004-06-03 16:08:41 +00001235 }
danielk1977979f38e2007-03-27 16:19:51 +00001236 }
1237 if( rc==SQLITE_OK ){
danielk1977f9e7dda2006-06-16 16:08:53 +00001238 sqlite3VtabCommit(db);
danielk197713adf8a2004-06-03 16:08:41 +00001239 }
1240 }
1241
1242 /* The complex case - There is a multi-file write-transaction active.
1243 ** This requires a master journal file to ensure the transaction is
1244 ** committed atomicly.
1245 */
danielk197744ee5bf2005-05-27 09:41:12 +00001246#ifndef SQLITE_OMIT_DISKIO
danielk197713adf8a2004-06-03 16:08:41 +00001247 else{
danielk1977b4b47412007-08-17 15:53:36 +00001248 sqlite3_vfs *pVfs = db->pVfs;
drh2c8997b2005-08-27 16:36:48 +00001249 int needSync = 0;
danielk197713adf8a2004-06-03 16:08:41 +00001250 char *zMaster = 0; /* File-name for the master journal */
1251 char const *zMainFile = sqlite3BtreeGetFilename(db->aDb[0].pBt);
danielk1977b4b47412007-08-17 15:53:36 +00001252 sqlite3_file *pMaster = 0;
danielk197762079062007-08-15 17:08:46 +00001253 i64 offset = 0;
danielk197713adf8a2004-06-03 16:08:41 +00001254
1255 /* Select a master journal file name */
1256 do {
drha6abd042004-06-09 17:37:22 +00001257 u32 random;
drh17435752007-08-16 04:30:38 +00001258 sqlite3_free(zMaster);
danielk197713adf8a2004-06-03 16:08:41 +00001259 sqlite3Randomness(sizeof(random), &random);
danielk19771e536952007-08-16 10:09:01 +00001260 zMaster = sqlite3MPrintf(db, "%s-mj%08X", zMainFile, random&0x7fffffff);
danielk197713adf8a2004-06-03 16:08:41 +00001261 if( !zMaster ){
1262 return SQLITE_NOMEM;
1263 }
danielk1977b4b47412007-08-17 15:53:36 +00001264 }while( sqlite3OsAccess(pVfs, zMaster, SQLITE_ACCESS_EXISTS) );
danielk197713adf8a2004-06-03 16:08:41 +00001265
1266 /* Open the master journal. */
danielk1977fee2d252007-08-18 10:59:19 +00001267 rc = sqlite3OsOpenMalloc(pVfs, zMaster, &pMaster,
1268 SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|
danielk1977967a4a12007-08-20 14:23:44 +00001269 SQLITE_OPEN_EXCLUSIVE|SQLITE_OPEN_MASTER_JOURNAL, 0
danielk1977fee2d252007-08-18 10:59:19 +00001270 );
danielk197713adf8a2004-06-03 16:08:41 +00001271 if( rc!=SQLITE_OK ){
drh17435752007-08-16 04:30:38 +00001272 sqlite3_free(zMaster);
danielk197713adf8a2004-06-03 16:08:41 +00001273 return rc;
1274 }
1275
1276 /* Write the name of each database file in the transaction into the new
1277 ** master journal file. If an error occurs at this point close
1278 ** and delete the master journal file. All the individual journal files
1279 ** still have 'null' as the master journal pointer, so they will roll
danielk1977aca790a2005-01-13 11:07:52 +00001280 ** back independently if a failure occurs.
danielk197713adf8a2004-06-03 16:08:41 +00001281 */
danielk19771e536952007-08-16 10:09:01 +00001282 for(i=0; i<db->nDb; i++){
danielk197713adf8a2004-06-03 16:08:41 +00001283 Btree *pBt = db->aDb[i].pBt;
drhc9e06862004-06-09 20:03:08 +00001284 if( i==1 ) continue; /* Ignore the TEMP database */
drhd0679ed2007-08-28 22:24:34 +00001285 if( sqlite3BtreeIsInTrans(pBt) ){
danielk19775865e3d2004-06-14 06:03:57 +00001286 char const *zFile = sqlite3BtreeGetJournalname(pBt);
drhc9e06862004-06-09 20:03:08 +00001287 if( zFile[0]==0 ) continue; /* Ignore :memory: databases */
drh2c8997b2005-08-27 16:36:48 +00001288 if( !needSync && !sqlite3BtreeSyncDisabled(pBt) ){
1289 needSync = 1;
1290 }
danielk1977b4b47412007-08-17 15:53:36 +00001291 rc = sqlite3OsWrite(pMaster, zFile, strlen(zFile)+1, offset);
danielk197762079062007-08-15 17:08:46 +00001292 offset += strlen(zFile)+1;
danielk197713adf8a2004-06-03 16:08:41 +00001293 if( rc!=SQLITE_OK ){
danielk1977fee2d252007-08-18 10:59:19 +00001294 sqlite3OsCloseFree(pMaster);
1295 sqlite3OsDelete(pVfs, zMaster, 0);
drh17435752007-08-16 04:30:38 +00001296 sqlite3_free(zMaster);
danielk197713adf8a2004-06-03 16:08:41 +00001297 return rc;
1298 }
1299 }
1300 }
1301
danielk19779663b8f2007-08-24 11:52:28 +00001302 /* Sync the master journal file. If the IOCAP_SEQUENTIAL device
1303 ** flag is set this is not required.
1304 */
danielk19775865e3d2004-06-14 06:03:57 +00001305 zMainFile = sqlite3BtreeGetDirname(db->aDb[0].pBt);
danielk1977f036aef2007-08-20 05:36:51 +00001306 if( (needSync
danielk19779663b8f2007-08-24 11:52:28 +00001307 && (0==(sqlite3OsDeviceCharacteristics(pMaster)&SQLITE_IOCAP_SEQUENTIAL))
danielk1977f036aef2007-08-20 05:36:51 +00001308 && (rc=sqlite3OsSync(pMaster, SQLITE_SYNC_NORMAL))!=SQLITE_OK) ){
danielk1977fee2d252007-08-18 10:59:19 +00001309 sqlite3OsCloseFree(pMaster);
1310 sqlite3OsDelete(pVfs, zMaster, 0);
drh17435752007-08-16 04:30:38 +00001311 sqlite3_free(zMaster);
danielk19775865e3d2004-06-14 06:03:57 +00001312 return rc;
1313 }
drhc9e06862004-06-09 20:03:08 +00001314
danielk197713adf8a2004-06-03 16:08:41 +00001315 /* Sync all the db files involved in the transaction. The same call
1316 ** sets the master journal pointer in each individual journal. If
1317 ** an error occurs here, do not delete the master journal file.
1318 **
drh80e35f42007-03-30 14:06:34 +00001319 ** If the error occurs during the first call to
1320 ** sqlite3BtreeCommitPhaseOne(), then there is a chance that the
1321 ** master journal file will be orphaned. But we cannot delete it,
1322 ** in case the master journal file name was written into the journal
1323 ** file before the failure occured.
danielk197713adf8a2004-06-03 16:08:41 +00001324 */
danielk19775bd270b2006-07-25 15:14:52 +00001325 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
danielk197713adf8a2004-06-03 16:08:41 +00001326 Btree *pBt = db->aDb[i].pBt;
drhd0679ed2007-08-28 22:24:34 +00001327 if( pBt ){
drh80e35f42007-03-30 14:06:34 +00001328 rc = sqlite3BtreeCommitPhaseOne(pBt, zMaster);
danielk197713adf8a2004-06-03 16:08:41 +00001329 }
1330 }
danielk1977fee2d252007-08-18 10:59:19 +00001331 sqlite3OsCloseFree(pMaster);
danielk19775bd270b2006-07-25 15:14:52 +00001332 if( rc!=SQLITE_OK ){
drh17435752007-08-16 04:30:38 +00001333 sqlite3_free(zMaster);
danielk19775bd270b2006-07-25 15:14:52 +00001334 return rc;
1335 }
danielk197713adf8a2004-06-03 16:08:41 +00001336
danielk1977962398d2004-06-14 09:35:16 +00001337 /* Delete the master journal file. This commits the transaction. After
1338 ** doing this the directory is synced again before any individual
1339 ** transaction files are deleted.
1340 */
danielk1977fee2d252007-08-18 10:59:19 +00001341 rc = sqlite3OsDelete(pVfs, zMaster, 1);
drh17435752007-08-16 04:30:38 +00001342 sqlite3_free(zMaster);
drhc416ba92007-03-30 18:42:55 +00001343 zMaster = 0;
drh29a01382006-08-13 19:04:18 +00001344 if( rc ){
1345 return rc;
1346 }
danielk197713adf8a2004-06-03 16:08:41 +00001347
1348 /* All files and directories have already been synced, so the following
drh80e35f42007-03-30 14:06:34 +00001349 ** calls to sqlite3BtreeCommitPhaseTwo() are only closing files and
1350 ** deleting or truncating journals. If something goes wrong while
1351 ** this is happening we don't really care. The integrity of the
1352 ** transaction is already guaranteed, but some stray 'cold' journals
1353 ** may be lying around. Returning an error code won't help matters.
danielk197713adf8a2004-06-03 16:08:41 +00001354 */
danielk1977979f38e2007-03-27 16:19:51 +00001355 disable_simulated_io_errors();
danielk197713adf8a2004-06-03 16:08:41 +00001356 for(i=0; i<db->nDb; i++){
1357 Btree *pBt = db->aDb[i].pBt;
1358 if( pBt ){
drh80e35f42007-03-30 14:06:34 +00001359 sqlite3BtreeCommitPhaseTwo(pBt);
danielk197713adf8a2004-06-03 16:08:41 +00001360 }
1361 }
danielk1977979f38e2007-03-27 16:19:51 +00001362 enable_simulated_io_errors();
1363
danielk1977f9e7dda2006-06-16 16:08:53 +00001364 sqlite3VtabCommit(db);
danielk197713adf8a2004-06-03 16:08:41 +00001365 }
danielk197744ee5bf2005-05-27 09:41:12 +00001366#endif
danielk1977026d2702004-06-14 13:14:59 +00001367
drh2ac3ee92004-06-07 16:27:46 +00001368 return rc;
danielk197713adf8a2004-06-03 16:08:41 +00001369}
1370
danielk19771d850a72004-05-31 08:26:49 +00001371/*
1372** This routine checks that the sqlite3.activeVdbeCnt count variable
1373** matches the number of vdbe's in the list sqlite3.pVdbe that are
1374** currently active. An assertion fails if the two counts do not match.
drh92f02c32004-09-02 14:57:08 +00001375** This is an internal self-check only - it is not an essential processing
1376** step.
danielk19771d850a72004-05-31 08:26:49 +00001377**
1378** This is a no-op if NDEBUG is defined.
1379*/
1380#ifndef NDEBUG
drh9bb575f2004-09-06 17:24:11 +00001381static void checkActiveVdbeCnt(sqlite3 *db){
danielk19771d850a72004-05-31 08:26:49 +00001382 Vdbe *p;
1383 int cnt = 0;
danielk19771d850a72004-05-31 08:26:49 +00001384 p = db->pVdbe;
1385 while( p ){
drh92f02c32004-09-02 14:57:08 +00001386 if( p->magic==VDBE_MAGIC_RUN && p->pc>=0 ){
danielk19771d850a72004-05-31 08:26:49 +00001387 cnt++;
1388 }
1389 p = p->pNext;
1390 }
danielk19771d850a72004-05-31 08:26:49 +00001391 assert( cnt==db->activeVdbeCnt );
1392}
1393#else
1394#define checkActiveVdbeCnt(x)
1395#endif
1396
danielk19773cf86062004-05-26 10:11:05 +00001397/*
drhfb982642007-08-30 01:19:59 +00001398** For every Btree that in database connection db which
1399** has been modified, "trip" or invalidate each cursor in
1400** that Btree might have been modified so that the cursor
1401** can never be used again. This happens when a rollback
1402*** occurs. We have to trip all the other cursors, even
1403** cursor from other VMs in different database connections,
1404** so that none of them try to use the data at which they
1405** were pointing and which now may have been changed due
1406** to the rollback.
1407**
1408** Remember that a rollback can delete tables complete and
1409** reorder rootpages. So it is not sufficient just to save
1410** the state of the cursor. We have to invalidate the cursor
1411** so that it is never used again.
danielk1977be718892006-06-23 08:05:19 +00001412*/
drhade6c9c2007-11-24 10:23:44 +00001413static void invalidateCursorsOnModifiedBtrees(sqlite3 *db){
drhfb982642007-08-30 01:19:59 +00001414 int i;
1415 for(i=0; i<db->nDb; i++){
1416 Btree *p = db->aDb[i].pBt;
1417 if( p && sqlite3BtreeIsInTrans(p) ){
1418 sqlite3BtreeTripAllCursors(p, SQLITE_ABORT);
1419 }
danielk1977be718892006-06-23 08:05:19 +00001420 }
1421}
1422
1423/*
drh92f02c32004-09-02 14:57:08 +00001424** This routine is called the when a VDBE tries to halt. If the VDBE
1425** has made changes and is in autocommit mode, then commit those
1426** changes. If a rollback is needed, then do the rollback.
drh9a324642003-09-06 20:12:01 +00001427**
drh92f02c32004-09-02 14:57:08 +00001428** This routine is the only way to move the state of a VM from
drhff0587c2007-08-29 17:43:19 +00001429** SQLITE_MAGIC_RUN to SQLITE_MAGIC_HALT. It is harmless to
1430** call this on a VM that is in the SQLITE_MAGIC_HALT state.
drh92f02c32004-09-02 14:57:08 +00001431**
1432** Return an error code. If the commit could not complete because of
1433** lock contention, return SQLITE_BUSY. If SQLITE_BUSY is returned, it
1434** means the close did not happen and needs to be repeated.
drh9a324642003-09-06 20:12:01 +00001435*/
drhff0587c2007-08-29 17:43:19 +00001436int sqlite3VdbeHalt(Vdbe *p){
drh9bb575f2004-09-06 17:24:11 +00001437 sqlite3 *db = p->db;
drh9a324642003-09-06 20:12:01 +00001438 int i;
danielk19771d850a72004-05-31 08:26:49 +00001439 int (*xFunc)(Btree *pBt) = 0; /* Function to call on each btree backend */
danielk197707cb5602006-01-20 10:55:05 +00001440 int isSpecialError; /* Set to true if SQLITE_NOMEM or IOERR */
1441
1442 /* This function contains the logic that determines if a statement or
1443 ** transaction will be committed or rolled back as a result of the
1444 ** execution of this virtual machine.
1445 **
drh71b890a2007-10-03 15:30:52 +00001446 ** If any of the following errors occur:
danielk197707cb5602006-01-20 10:55:05 +00001447 **
drh71b890a2007-10-03 15:30:52 +00001448 ** SQLITE_NOMEM
1449 ** SQLITE_IOERR
1450 ** SQLITE_FULL
1451 ** SQLITE_INTERRUPT
danielk197707cb5602006-01-20 10:55:05 +00001452 **
drh71b890a2007-10-03 15:30:52 +00001453 ** Then the internal cache might have been left in an inconsistent
1454 ** state. We need to rollback the statement transaction, if there is
1455 ** one, or the complete transaction if there is no statement transaction.
danielk197707cb5602006-01-20 10:55:05 +00001456 */
drh9a324642003-09-06 20:12:01 +00001457
drh17435752007-08-16 04:30:38 +00001458 if( p->db->mallocFailed ){
danielk1977261919c2005-12-06 12:52:59 +00001459 p->rc = SQLITE_NOMEM;
1460 }
drhff0587c2007-08-29 17:43:19 +00001461 closeAllCursorsExceptActiveVtabs(p);
drh92f02c32004-09-02 14:57:08 +00001462 if( p->magic!=VDBE_MAGIC_RUN ){
drh92f02c32004-09-02 14:57:08 +00001463 return SQLITE_OK;
drh9a324642003-09-06 20:12:01 +00001464 }
danielk19771d850a72004-05-31 08:26:49 +00001465 checkActiveVdbeCnt(db);
danielk1977261919c2005-12-06 12:52:59 +00001466
danielk197707cb5602006-01-20 10:55:05 +00001467 /* No commit or rollback needed if the program never started */
1468 if( p->pc>=0 ){
drhaac2f552006-09-23 21:44:23 +00001469 int mrc; /* Primary error code from p->rc */
drhff0587c2007-08-29 17:43:19 +00001470
1471 /* Lock all btrees used by the statement */
1472 sqlite3BtreeMutexArrayEnter(&p->aMutex);
1473
drh71b890a2007-10-03 15:30:52 +00001474 /* Check for one of the special errors */
drhaac2f552006-09-23 21:44:23 +00001475 mrc = p->rc & 0xff;
drh71b890a2007-10-03 15:30:52 +00001476 isSpecialError = mrc==SQLITE_NOMEM || mrc==SQLITE_IOERR
drh77658e22007-12-04 16:54:52 +00001477 || mrc==SQLITE_INTERRUPT || mrc==SQLITE_FULL;
danielk197707cb5602006-01-20 10:55:05 +00001478 if( isSpecialError ){
danielk1977261919c2005-12-06 12:52:59 +00001479 /* This loop does static analysis of the query to see which of the
1480 ** following three categories it falls into:
1481 **
1482 ** Read-only
danielk197707cb5602006-01-20 10:55:05 +00001483 ** Query with statement journal
1484 ** Query without statement journal
danielk1977261919c2005-12-06 12:52:59 +00001485 **
1486 ** We could do something more elegant than this static analysis (i.e.
1487 ** store the type of query as part of the compliation phase), but
danielk197707cb5602006-01-20 10:55:05 +00001488 ** handling malloc() or IO failure is a fairly obscure edge case so
1489 ** this is probably easier. Todo: Might be an opportunity to reduce
1490 ** code size a very small amount though...
danielk1977261919c2005-12-06 12:52:59 +00001491 */
drhd1817042007-10-03 18:45:04 +00001492 int notReadOnly = 0;
danielk1977261919c2005-12-06 12:52:59 +00001493 int isStatement = 0;
1494 assert(p->aOp || p->nOp==0);
1495 for(i=0; i<p->nOp; i++){
1496 switch( p->aOp[i].opcode ){
1497 case OP_Transaction:
drhd1817042007-10-03 18:45:04 +00001498 notReadOnly |= p->aOp[i].p2;
danielk1977261919c2005-12-06 12:52:59 +00001499 break;
1500 case OP_Statement:
1501 isStatement = 1;
1502 break;
1503 }
1504 }
drhff0587c2007-08-29 17:43:19 +00001505
1506
danielk197707cb5602006-01-20 10:55:05 +00001507 /* If the query was read-only, we need do no rollback at all. Otherwise,
1508 ** proceed with the special handling.
1509 */
drhd1817042007-10-03 18:45:04 +00001510 if( notReadOnly || mrc!=SQLITE_INTERRUPT ){
danielk1977e965ac72007-06-13 15:22:28 +00001511 if( p->rc==SQLITE_IOERR_BLOCKED && isStatement ){
1512 xFunc = sqlite3BtreeRollbackStmt;
1513 p->rc = SQLITE_BUSY;
drhd1817042007-10-03 18:45:04 +00001514 } else if( (mrc==SQLITE_NOMEM || mrc==SQLITE_FULL) && isStatement ){
danielk197707cb5602006-01-20 10:55:05 +00001515 xFunc = sqlite3BtreeRollbackStmt;
1516 }else{
1517 /* We are forced to roll back the active transaction. Before doing
1518 ** so, abort any other statements this handle currently has active.
1519 */
drhfb982642007-08-30 01:19:59 +00001520 invalidateCursorsOnModifiedBtrees(db);
danielk197797a227c2006-01-20 16:32:04 +00001521 sqlite3RollbackAll(db);
danielk197707cb5602006-01-20 10:55:05 +00001522 db->autoCommit = 1;
1523 }
danielk1977261919c2005-12-06 12:52:59 +00001524 }
1525 }
danielk197707cb5602006-01-20 10:55:05 +00001526
1527 /* If the auto-commit flag is set and this is the only active vdbe, then
1528 ** we do either a commit or rollback of the current transaction.
1529 **
1530 ** Note: This block also runs if one of the special errors handled
1531 ** above has occured.
1532 */
1533 if( db->autoCommit && db->activeVdbeCnt==1 ){
1534 if( p->rc==SQLITE_OK || (p->errorAction==OE_Fail && !isSpecialError) ){
drhfd131da2007-08-07 17:13:03 +00001535 /* The auto-commit flag is true, and the vdbe program was
danielk197707cb5602006-01-20 10:55:05 +00001536 ** successful or hit an 'OR FAIL' constraint. This means a commit
1537 ** is required.
1538 */
1539 int rc = vdbeCommit(db);
1540 if( rc==SQLITE_BUSY ){
drhff0587c2007-08-29 17:43:19 +00001541 sqlite3BtreeMutexArrayLeave(&p->aMutex);
danielk197707cb5602006-01-20 10:55:05 +00001542 return SQLITE_BUSY;
1543 }else if( rc!=SQLITE_OK ){
1544 p->rc = rc;
danielk197797a227c2006-01-20 16:32:04 +00001545 sqlite3RollbackAll(db);
danielk197707cb5602006-01-20 10:55:05 +00001546 }else{
1547 sqlite3CommitInternalChanges(db);
1548 }
1549 }else{
danielk197797a227c2006-01-20 16:32:04 +00001550 sqlite3RollbackAll(db);
danielk197707cb5602006-01-20 10:55:05 +00001551 }
1552 }else if( !xFunc ){
1553 if( p->rc==SQLITE_OK || p->errorAction==OE_Fail ){
danielk1977182c4ba2007-06-27 15:53:34 +00001554 if( p->openedStatement ){
1555 xFunc = sqlite3BtreeCommitStmt;
1556 }
danielk197707cb5602006-01-20 10:55:05 +00001557 }else if( p->errorAction==OE_Abort ){
1558 xFunc = sqlite3BtreeRollbackStmt;
1559 }else{
drhfb982642007-08-30 01:19:59 +00001560 invalidateCursorsOnModifiedBtrees(db);
danielk197797a227c2006-01-20 16:32:04 +00001561 sqlite3RollbackAll(db);
danielk197707cb5602006-01-20 10:55:05 +00001562 db->autoCommit = 1;
1563 }
danielk19771d850a72004-05-31 08:26:49 +00001564 }
danielk197707cb5602006-01-20 10:55:05 +00001565
1566 /* If xFunc is not NULL, then it is one of sqlite3BtreeRollbackStmt or
1567 ** sqlite3BtreeCommitStmt. Call it once on each backend. If an error occurs
1568 ** and the return code is still SQLITE_OK, set the return code to the new
1569 ** error value.
1570 */
1571 assert(!xFunc ||
1572 xFunc==sqlite3BtreeCommitStmt ||
1573 xFunc==sqlite3BtreeRollbackStmt
1574 );
1575 for(i=0; xFunc && i<db->nDb; i++){
1576 int rc;
1577 Btree *pBt = db->aDb[i].pBt;
1578 if( pBt ){
1579 rc = xFunc(pBt);
danielk19778a7aea32006-01-23 15:25:48 +00001580 if( rc && (p->rc==SQLITE_OK || p->rc==SQLITE_CONSTRAINT) ){
1581 p->rc = rc;
1582 sqlite3SetString(&p->zErrMsg, 0);
1583 }
danielk197707cb5602006-01-20 10:55:05 +00001584 }
danielk197777d83ba2004-05-31 10:08:14 +00001585 }
danielk197707cb5602006-01-20 10:55:05 +00001586
1587 /* If this was an INSERT, UPDATE or DELETE and the statement was committed,
1588 ** set the change counter.
1589 */
1590 if( p->changeCntOn && p->pc>=0 ){
1591 if( !xFunc || xFunc==sqlite3BtreeCommitStmt ){
1592 sqlite3VdbeSetChanges(db, p->nChange);
1593 }else{
1594 sqlite3VdbeSetChanges(db, 0);
1595 }
1596 p->nChange = 0;
danielk1977b28af712004-06-21 06:50:26 +00001597 }
danielk197707cb5602006-01-20 10:55:05 +00001598
1599 /* Rollback or commit any schema changes that occurred. */
1600 if( p->rc!=SQLITE_OK && db->flags&SQLITE_InternChanges ){
1601 sqlite3ResetInternalSchema(db, 0);
1602 db->flags = (db->flags | SQLITE_InternChanges);
1603 }
drhff0587c2007-08-29 17:43:19 +00001604
1605 /* Release the locks */
1606 sqlite3BtreeMutexArrayLeave(&p->aMutex);
drh9a324642003-09-06 20:12:01 +00001607 }
danielk19771d850a72004-05-31 08:26:49 +00001608
danielk197765fd59f2006-06-24 11:51:33 +00001609 /* We have successfully halted and closed the VM. Record this fact. */
1610 if( p->pc>=0 ){
danielk19771d850a72004-05-31 08:26:49 +00001611 db->activeVdbeCnt--;
drh9a324642003-09-06 20:12:01 +00001612 }
drh92f02c32004-09-02 14:57:08 +00001613 p->magic = VDBE_MAGIC_HALT;
1614 checkActiveVdbeCnt(db);
drhff0587c2007-08-29 17:43:19 +00001615 if( p->db->mallocFailed ){
1616 p->rc = SQLITE_NOMEM;
1617 }
1618 checkActiveVdbeCnt(db);
danielk19771d850a72004-05-31 08:26:49 +00001619
drh92f02c32004-09-02 14:57:08 +00001620 return SQLITE_OK;
1621}
drh4cf7c7f2007-08-28 23:28:07 +00001622
drh92f02c32004-09-02 14:57:08 +00001623
1624/*
drh3c23a882007-01-09 14:01:13 +00001625** Each VDBE holds the result of the most recent sqlite3_step() call
1626** in p->rc. This routine sets that result back to SQLITE_OK.
1627*/
1628void sqlite3VdbeResetStepResult(Vdbe *p){
1629 p->rc = SQLITE_OK;
1630}
1631
1632/*
drh92f02c32004-09-02 14:57:08 +00001633** Clean up a VDBE after execution but do not delete the VDBE just yet.
1634** Write any error messages into *pzErrMsg. Return the result code.
1635**
1636** After this routine is run, the VDBE should be ready to be executed
1637** again.
1638**
1639** To look at it another way, this routine resets the state of the
1640** virtual machine from VDBE_MAGIC_RUN or VDBE_MAGIC_HALT back to
1641** VDBE_MAGIC_INIT.
1642*/
1643int sqlite3VdbeReset(Vdbe *p){
drh4ac285a2006-09-15 07:28:50 +00001644 sqlite3 *db;
drh4ac285a2006-09-15 07:28:50 +00001645 db = p->db;
drh92f02c32004-09-02 14:57:08 +00001646
1647 /* If the VM did not run to completion or if it encountered an
1648 ** error, then it might not have been halted properly. So halt
1649 ** it now.
1650 */
drh4ac285a2006-09-15 07:28:50 +00001651 sqlite3SafetyOn(db);
drh92f02c32004-09-02 14:57:08 +00001652 sqlite3VdbeHalt(p);
drh4ac285a2006-09-15 07:28:50 +00001653 sqlite3SafetyOff(db);
drh92f02c32004-09-02 14:57:08 +00001654
drhfb7e7652005-01-24 00:28:42 +00001655 /* If the VDBE has be run even partially, then transfer the error code
1656 ** and error message from the VDBE into the main database structure. But
1657 ** if the VDBE has just been set to run but has not actually executed any
1658 ** instructions yet, leave the main database error information unchanged.
drh92f02c32004-09-02 14:57:08 +00001659 */
drhfb7e7652005-01-24 00:28:42 +00001660 if( p->pc>=0 ){
1661 if( p->zErrMsg ){
drhb21c8cd2007-08-21 19:33:56 +00001662 sqlite3ValueSetStr(db->pErr,-1,p->zErrMsg,SQLITE_UTF8,sqlite3_free);
danielk197797a227c2006-01-20 16:32:04 +00001663 db->errCode = p->rc;
drhfb7e7652005-01-24 00:28:42 +00001664 p->zErrMsg = 0;
1665 }else if( p->rc ){
drh4ac285a2006-09-15 07:28:50 +00001666 sqlite3Error(db, p->rc, 0);
drhfb7e7652005-01-24 00:28:42 +00001667 }else{
drh4ac285a2006-09-15 07:28:50 +00001668 sqlite3Error(db, SQLITE_OK, 0);
drhfb7e7652005-01-24 00:28:42 +00001669 }
danielk1977a21c6b62005-01-24 10:25:59 +00001670 }else if( p->rc && p->expired ){
1671 /* The expired flag was set on the VDBE before the first call
1672 ** to sqlite3_step(). For consistency (since sqlite3_step() was
1673 ** called), set the database error in this case as well.
1674 */
drh4ac285a2006-09-15 07:28:50 +00001675 sqlite3Error(db, p->rc, 0);
danielk19778e556522007-11-13 10:30:24 +00001676 sqlite3ValueSetStr(db->pErr, -1, p->zErrMsg, SQLITE_UTF8, sqlite3_free);
1677 p->zErrMsg = 0;
drh92f02c32004-09-02 14:57:08 +00001678 }
1679
1680 /* Reclaim all memory used by the VDBE
1681 */
1682 Cleanup(p);
1683
1684 /* Save profiling information from this VDBE run.
1685 */
danielk1977261919c2005-12-06 12:52:59 +00001686 assert( p->pTos<&p->aStack[p->pc<0?0:p->pc] || !p->aStack );
drh9a324642003-09-06 20:12:01 +00001687#ifdef VDBE_PROFILE
1688 {
1689 FILE *out = fopen("vdbe_profile.out", "a");
1690 if( out ){
1691 int i;
1692 fprintf(out, "---- ");
1693 for(i=0; i<p->nOp; i++){
1694 fprintf(out, "%02x", p->aOp[i].opcode);
1695 }
1696 fprintf(out, "\n");
1697 for(i=0; i<p->nOp; i++){
1698 fprintf(out, "%6d %10lld %8lld ",
1699 p->aOp[i].cnt,
1700 p->aOp[i].cycles,
1701 p->aOp[i].cnt>0 ? p->aOp[i].cycles/p->aOp[i].cnt : 0
1702 );
danielk19774adee202004-05-08 08:23:19 +00001703 sqlite3VdbePrintOp(out, i, &p->aOp[i]);
drh9a324642003-09-06 20:12:01 +00001704 }
1705 fclose(out);
1706 }
1707 }
1708#endif
1709 p->magic = VDBE_MAGIC_INIT;
drh91b48aa2004-06-30 11:14:18 +00001710 p->aborted = 0;
drh4ac285a2006-09-15 07:28:50 +00001711 return p->rc & db->errMask;
drh9a324642003-09-06 20:12:01 +00001712}
drh92f02c32004-09-02 14:57:08 +00001713
drh9a324642003-09-06 20:12:01 +00001714/*
1715** Clean up and delete a VDBE after execution. Return an integer which is
1716** the result code. Write any error message text into *pzErrMsg.
1717*/
danielk19779e6db7d2004-06-21 08:18:51 +00001718int sqlite3VdbeFinalize(Vdbe *p){
danielk1977b5548a82004-06-26 13:51:33 +00001719 int rc = SQLITE_OK;
danielk1977b5548a82004-06-26 13:51:33 +00001720 if( p->magic==VDBE_MAGIC_RUN || p->magic==VDBE_MAGIC_HALT ){
1721 rc = sqlite3VdbeReset(p);
drh4ac285a2006-09-15 07:28:50 +00001722 assert( (rc & p->db->errMask)==rc );
danielk1977b5548a82004-06-26 13:51:33 +00001723 }else if( p->magic!=VDBE_MAGIC_INIT ){
drh9a324642003-09-06 20:12:01 +00001724 return SQLITE_MISUSE;
1725 }
danielk19774adee202004-05-08 08:23:19 +00001726 sqlite3VdbeDelete(p);
drh9a324642003-09-06 20:12:01 +00001727 return rc;
1728}
1729
1730/*
drhf92c7ff2004-06-19 15:40:23 +00001731** Call the destructor for each auxdata entry in pVdbeFunc for which
danielk1977e159fdf2004-06-21 10:45:06 +00001732** the corresponding bit in mask is clear. Auxdata entries beyond 31
drhf92c7ff2004-06-19 15:40:23 +00001733** are always destroyed. To destroy all auxdata entries, call this
danielk1977e159fdf2004-06-21 10:45:06 +00001734** routine with mask==0.
drhf92c7ff2004-06-19 15:40:23 +00001735*/
1736void sqlite3VdbeDeleteAuxData(VdbeFunc *pVdbeFunc, int mask){
1737 int i;
1738 for(i=0; i<pVdbeFunc->nAux; i++){
1739 struct AuxData *pAux = &pVdbeFunc->apAux[i];
1740 if( (i>31 || !(mask&(1<<i))) && pAux->pAux ){
1741 if( pAux->xDelete ){
1742 pAux->xDelete(pAux->pAux);
1743 }
1744 pAux->pAux = 0;
1745 }
1746 }
1747}
1748
1749/*
drh9a324642003-09-06 20:12:01 +00001750** Delete an entire VDBE.
1751*/
danielk19774adee202004-05-08 08:23:19 +00001752void sqlite3VdbeDelete(Vdbe *p){
drh9a324642003-09-06 20:12:01 +00001753 int i;
1754 if( p==0 ) return;
1755 Cleanup(p);
1756 if( p->pPrev ){
1757 p->pPrev->pNext = p->pNext;
1758 }else{
1759 assert( p->db->pVdbe==p );
1760 p->db->pVdbe = p->pNext;
1761 }
1762 if( p->pNext ){
1763 p->pNext->pPrev = p->pPrev;
1764 }
drh76ff3a02004-09-24 22:32:30 +00001765 if( p->aOp ){
drhd4e70eb2008-01-02 00:34:36 +00001766 Op *pOp = p->aOp;
1767 for(i=0; i<p->nOp; i++, pOp++){
drh66a51672008-01-03 00:01:23 +00001768 freeP4(pOp->p4type, pOp->p4.p);
drhd4e70eb2008-01-02 00:34:36 +00001769#ifdef SQLITE_DEBUG
1770 sqlite3_free(pOp->zComment);
1771#endif
drh9a324642003-09-06 20:12:01 +00001772 }
drh17435752007-08-16 04:30:38 +00001773 sqlite3_free(p->aOp);
drh9a324642003-09-06 20:12:01 +00001774 }
drh76ff3a02004-09-24 22:32:30 +00001775 releaseMemArray(p->aVar, p->nVar);
drh17435752007-08-16 04:30:38 +00001776 sqlite3_free(p->aLabel);
1777 sqlite3_free(p->aStack);
danielk1977955de522006-02-10 02:27:42 +00001778 releaseMemArray(p->aColName, p->nResColumn*COLNAME_N);
drh17435752007-08-16 04:30:38 +00001779 sqlite3_free(p->aColName);
1780 sqlite3_free(p->zSql);
drh9a324642003-09-06 20:12:01 +00001781 p->magic = VDBE_MAGIC_DEAD;
drh17435752007-08-16 04:30:38 +00001782 sqlite3_free(p);
drh9a324642003-09-06 20:12:01 +00001783}
drha11846b2004-01-07 18:52:56 +00001784
1785/*
drha11846b2004-01-07 18:52:56 +00001786** If a MoveTo operation is pending on the given cursor, then do that
1787** MoveTo now. Return an error code. If no MoveTo is pending, this
1788** routine does nothing and returns SQLITE_OK.
1789*/
danielk19774adee202004-05-08 08:23:19 +00001790int sqlite3VdbeCursorMoveto(Cursor *p){
drha11846b2004-01-07 18:52:56 +00001791 if( p->deferredMoveto ){
drh536065a2005-01-26 21:55:31 +00001792 int res, rc;
adamd4fc93082006-09-14 16:57:19 +00001793#ifdef SQLITE_TEST
danielk1977132872b2004-05-10 10:37:18 +00001794 extern int sqlite3_search_count;
adamd4fc93082006-09-14 16:57:19 +00001795#endif
drhf0863fe2005-06-12 21:35:51 +00001796 assert( p->isTable );
drh5f9c1a22007-04-04 01:27:44 +00001797 rc = sqlite3BtreeMoveto(p->pCursor, 0, p->movetoTarget, 0, &res);
drh536065a2005-01-26 21:55:31 +00001798 if( rc ) return rc;
drhd3d39e92004-05-20 22:16:29 +00001799 *p->pIncrKey = 0;
drhf0863fe2005-06-12 21:35:51 +00001800 p->lastRowid = keyToInt(p->movetoTarget);
1801 p->rowidIsValid = res==0;
drha11846b2004-01-07 18:52:56 +00001802 if( res<0 ){
drh536065a2005-01-26 21:55:31 +00001803 rc = sqlite3BtreeNext(p->pCursor, &res);
1804 if( rc ) return rc;
drha11846b2004-01-07 18:52:56 +00001805 }
drh10cfdd52006-08-08 15:42:59 +00001806#ifdef SQLITE_TEST
danielk1977132872b2004-05-10 10:37:18 +00001807 sqlite3_search_count++;
drh10cfdd52006-08-08 15:42:59 +00001808#endif
drha11846b2004-01-07 18:52:56 +00001809 p->deferredMoveto = 0;
drh76873ab2006-01-07 18:48:26 +00001810 p->cacheStatus = CACHE_STALE;
drha11846b2004-01-07 18:52:56 +00001811 }
1812 return SQLITE_OK;
1813}
danielk19774adee202004-05-08 08:23:19 +00001814
drhab9f7f12004-05-08 10:56:11 +00001815/*
danielk1977cfcdaef2004-05-12 07:33:33 +00001816** The following functions:
danielk197790e4d952004-05-10 10:05:53 +00001817**
danielk1977cfcdaef2004-05-12 07:33:33 +00001818** sqlite3VdbeSerialType()
1819** sqlite3VdbeSerialTypeLen()
1820** sqlite3VdbeSerialRead()
danielk197790e4d952004-05-10 10:05:53 +00001821** sqlite3VdbeSerialLen()
danielk1977cfcdaef2004-05-12 07:33:33 +00001822** sqlite3VdbeSerialWrite()
danielk197790e4d952004-05-10 10:05:53 +00001823**
1824** encapsulate the code that serializes values for storage in SQLite
danielk1977cfcdaef2004-05-12 07:33:33 +00001825** data and index records. Each serialized value consists of a
1826** 'serial-type' and a blob of data. The serial type is an 8-byte unsigned
1827** integer, stored as a varint.
danielk197790e4d952004-05-10 10:05:53 +00001828**
danielk1977cfcdaef2004-05-12 07:33:33 +00001829** In an SQLite index record, the serial type is stored directly before
1830** the blob of data that it corresponds to. In a table record, all serial
1831** types are stored at the start of the record, and the blobs of data at
1832** the end. Hence these functions allow the caller to handle the
1833** serial-type and data blob seperately.
1834**
1835** The following table describes the various storage classes for data:
1836**
1837** serial type bytes of data type
danielk197790e4d952004-05-10 10:05:53 +00001838** -------------- --------------- ---------------
drha19b7752004-05-30 21:14:58 +00001839** 0 0 NULL
danielk197790e4d952004-05-10 10:05:53 +00001840** 1 1 signed integer
1841** 2 2 signed integer
drha19b7752004-05-30 21:14:58 +00001842** 3 3 signed integer
1843** 4 4 signed integer
1844** 5 6 signed integer
1845** 6 8 signed integer
1846** 7 8 IEEE float
drhd946db02005-12-29 19:23:06 +00001847** 8 0 Integer constant 0
1848** 9 0 Integer constant 1
1849** 10,11 reserved for expansion
danielk197790e4d952004-05-10 10:05:53 +00001850** N>=12 and even (N-12)/2 BLOB
1851** N>=13 and odd (N-13)/2 text
1852**
drh35a59652006-01-02 18:24:40 +00001853** The 8 and 9 types were added in 3.3.0, file format 4. Prior versions
1854** of SQLite will not understand those serial types.
danielk197790e4d952004-05-10 10:05:53 +00001855*/
1856
1857/*
danielk1977cfcdaef2004-05-12 07:33:33 +00001858** Return the serial-type for the value stored in pMem.
danielk1977192ac1d2004-05-10 07:17:30 +00001859*/
drhd946db02005-12-29 19:23:06 +00001860u32 sqlite3VdbeSerialType(Mem *pMem, int file_format){
danielk1977cfcdaef2004-05-12 07:33:33 +00001861 int flags = pMem->flags;
drhfdf972a2007-05-02 13:30:27 +00001862 int n;
danielk1977cfcdaef2004-05-12 07:33:33 +00001863
1864 if( flags&MEM_Null ){
drha19b7752004-05-30 21:14:58 +00001865 return 0;
danielk197790e4d952004-05-10 10:05:53 +00001866 }
danielk1977cfcdaef2004-05-12 07:33:33 +00001867 if( flags&MEM_Int ){
drhfe2093d2005-01-20 22:48:47 +00001868 /* Figure out whether to use 1, 2, 4, 6 or 8 bytes. */
drhb47d45c2005-04-15 12:04:34 +00001869# define MAX_6BYTE ((((i64)0x00001000)<<32)-1)
drh3c024d62007-03-30 11:23:45 +00001870 i64 i = pMem->u.i;
drhd946db02005-12-29 19:23:06 +00001871 u64 u;
1872 if( file_format>=4 && (i&1)==i ){
1873 return 8+i;
1874 }
1875 u = i<0 ? -i : i;
drh5742b632005-01-26 17:47:02 +00001876 if( u<=127 ) return 1;
1877 if( u<=32767 ) return 2;
1878 if( u<=8388607 ) return 3;
1879 if( u<=2147483647 ) return 4;
1880 if( u<=MAX_6BYTE ) return 5;
drha19b7752004-05-30 21:14:58 +00001881 return 6;
danielk197790e4d952004-05-10 10:05:53 +00001882 }
danielk1977cfcdaef2004-05-12 07:33:33 +00001883 if( flags&MEM_Real ){
drha19b7752004-05-30 21:14:58 +00001884 return 7;
danielk197790e4d952004-05-10 10:05:53 +00001885 }
drhfdf972a2007-05-02 13:30:27 +00001886 assert( flags&(MEM_Str|MEM_Blob) );
1887 n = pMem->n;
1888 if( flags & MEM_Zero ){
1889 n += pMem->u.i;
danielk197790e4d952004-05-10 10:05:53 +00001890 }
drhfdf972a2007-05-02 13:30:27 +00001891 assert( n>=0 );
1892 return ((n*2) + 12 + ((flags&MEM_Str)!=0));
danielk1977192ac1d2004-05-10 07:17:30 +00001893}
1894
1895/*
danielk1977cfcdaef2004-05-12 07:33:33 +00001896** Return the length of the data corresponding to the supplied serial-type.
danielk1977192ac1d2004-05-10 07:17:30 +00001897*/
drh25aa1b42004-05-28 01:39:01 +00001898int sqlite3VdbeSerialTypeLen(u32 serial_type){
drha19b7752004-05-30 21:14:58 +00001899 if( serial_type>=12 ){
drh51846b52004-05-28 16:00:21 +00001900 return (serial_type-12)/2;
1901 }else{
drh57196282004-10-06 15:41:16 +00001902 static const u8 aSize[] = { 0, 1, 2, 3, 4, 6, 8, 8, 0, 0, 0, 0 };
drh51846b52004-05-28 16:00:21 +00001903 return aSize[serial_type];
1904 }
danielk1977192ac1d2004-05-10 07:17:30 +00001905}
1906
1907/*
drh110daac2007-05-04 11:59:31 +00001908** If we are on an architecture with mixed-endian floating
drh7a4f5022007-05-23 07:20:08 +00001909** points (ex: ARM7) then swap the lower 4 bytes with the
drh110daac2007-05-04 11:59:31 +00001910** upper 4 bytes. Return the result.
1911**
drh7a4f5022007-05-23 07:20:08 +00001912** For most architectures, this is a no-op.
1913**
1914** (later): It is reported to me that the mixed-endian problem
1915** on ARM7 is an issue with GCC, not with the ARM7 chip. It seems
1916** that early versions of GCC stored the two words of a 64-bit
1917** float in the wrong order. And that error has been propagated
1918** ever since. The blame is not necessarily with GCC, though.
1919** GCC might have just copying the problem from a prior compiler.
1920** I am also told that newer versions of GCC that follow a different
1921** ABI get the byte order right.
1922**
1923** Developers using SQLite on an ARM7 should compile and run their
1924** application using -DSQLITE_DEBUG=1 at least once. With DEBUG
1925** enabled, some asserts below will ensure that the byte order of
1926** floating point values is correct.
drh60d09a72007-08-30 15:05:08 +00001927**
1928** (2007-08-30) Frank van Vugt has studied this problem closely
1929** and has send his findings to the SQLite developers. Frank
1930** writes that some Linux kernels offer floating point hardware
1931** emulation that uses only 32-bit mantissas instead of a full
1932** 48-bits as required by the IEEE standard. (This is the
1933** CONFIG_FPE_FASTFPE option.) On such systems, floating point
1934** byte swapping becomes very complicated. To avoid problems,
1935** the necessary byte swapping is carried out using a 64-bit integer
1936** rather than a 64-bit float. Frank assures us that the code here
1937** works for him. We, the developers, have no way to independently
1938** verify this, but Frank seems to know what he is talking about
1939** so we trust him.
drh110daac2007-05-04 11:59:31 +00001940*/
1941#ifdef SQLITE_MIXED_ENDIAN_64BIT_FLOAT
drh60d09a72007-08-30 15:05:08 +00001942static u64 floatSwap(u64 in){
drh110daac2007-05-04 11:59:31 +00001943 union {
drh60d09a72007-08-30 15:05:08 +00001944 u64 r;
drh110daac2007-05-04 11:59:31 +00001945 u32 i[2];
1946 } u;
1947 u32 t;
1948
1949 u.r = in;
1950 t = u.i[0];
1951 u.i[0] = u.i[1];
1952 u.i[1] = t;
1953 return u.r;
1954}
1955# define swapMixedEndianFloat(X) X = floatSwap(X)
1956#else
1957# define swapMixedEndianFloat(X)
1958#endif
1959
1960/*
danielk1977cfcdaef2004-05-12 07:33:33 +00001961** Write the serialized data blob for the value stored in pMem into
1962** buf. It is assumed that the caller has allocated sufficient space.
1963** Return the number of bytes written.
drhfdf972a2007-05-02 13:30:27 +00001964**
1965** nBuf is the amount of space left in buf[]. nBuf must always be
1966** large enough to hold the entire field. Except, if the field is
1967** a blob with a zero-filled tail, then buf[] might be just the right
1968** size to hold everything except for the zero-filled tail. If buf[]
1969** is only big enough to hold the non-zero prefix, then only write that
1970** prefix into buf[]. But if buf[] is large enough to hold both the
1971** prefix and the tail then write the prefix and set the tail to all
1972** zeros.
1973**
1974** Return the number of bytes actually written into buf[]. The number
1975** of bytes in the zero-filled tail is included in the return value only
1976** if those bytes were zeroed in buf[].
danielk1977cfcdaef2004-05-12 07:33:33 +00001977*/
drhfdf972a2007-05-02 13:30:27 +00001978int sqlite3VdbeSerialPut(u8 *buf, int nBuf, Mem *pMem, int file_format){
drhd946db02005-12-29 19:23:06 +00001979 u32 serial_type = sqlite3VdbeSerialType(pMem, file_format);
danielk1977cfcdaef2004-05-12 07:33:33 +00001980 int len;
danielk1977183f9f72004-05-13 05:20:26 +00001981
drh1483e142004-05-21 21:12:42 +00001982 /* Integer and Real */
drhd946db02005-12-29 19:23:06 +00001983 if( serial_type<=7 && serial_type>0 ){
drh1483e142004-05-21 21:12:42 +00001984 u64 v;
1985 int i;
drha19b7752004-05-30 21:14:58 +00001986 if( serial_type==7 ){
drh4f0c5872007-03-26 22:05:01 +00001987 assert( sizeof(v)==sizeof(pMem->r) );
1988 memcpy(&v, &pMem->r, sizeof(v));
drh60d09a72007-08-30 15:05:08 +00001989 swapMixedEndianFloat(v);
drh1483e142004-05-21 21:12:42 +00001990 }else{
drh3c024d62007-03-30 11:23:45 +00001991 v = pMem->u.i;
danielk1977cfcdaef2004-05-12 07:33:33 +00001992 }
drh1483e142004-05-21 21:12:42 +00001993 len = i = sqlite3VdbeSerialTypeLen(serial_type);
drhfdf972a2007-05-02 13:30:27 +00001994 assert( len<=nBuf );
drh1483e142004-05-21 21:12:42 +00001995 while( i-- ){
1996 buf[i] = (v&0xFF);
1997 v >>= 8;
1998 }
1999 return len;
danielk1977cfcdaef2004-05-12 07:33:33 +00002000 }
drhd946db02005-12-29 19:23:06 +00002001
danielk1977cfcdaef2004-05-12 07:33:33 +00002002 /* String or blob */
drhd946db02005-12-29 19:23:06 +00002003 if( serial_type>=12 ){
drhfdf972a2007-05-02 13:30:27 +00002004 assert( pMem->n + ((pMem->flags & MEM_Zero)?pMem->u.i:0)
2005 == sqlite3VdbeSerialTypeLen(serial_type) );
2006 assert( pMem->n<=nBuf );
2007 len = pMem->n;
drhd946db02005-12-29 19:23:06 +00002008 memcpy(buf, pMem->z, len);
drhfdf972a2007-05-02 13:30:27 +00002009 if( pMem->flags & MEM_Zero ){
2010 len += pMem->u.i;
2011 if( len>nBuf ){
2012 len = nBuf;
2013 }
2014 memset(&buf[pMem->n], 0, len-pMem->n);
2015 }
drhd946db02005-12-29 19:23:06 +00002016 return len;
2017 }
2018
2019 /* NULL or constants 0 or 1 */
2020 return 0;
danielk1977cfcdaef2004-05-12 07:33:33 +00002021}
2022
2023/*
2024** Deserialize the data blob pointed to by buf as serial type serial_type
2025** and store the result in pMem. Return the number of bytes read.
2026*/
danielk1977b1bc9532004-05-22 03:05:33 +00002027int sqlite3VdbeSerialGet(
danielk197793d46752004-05-23 13:30:58 +00002028 const unsigned char *buf, /* Buffer to deserialize from */
drh25aa1b42004-05-28 01:39:01 +00002029 u32 serial_type, /* Serial type to deserialize */
2030 Mem *pMem /* Memory cell to write value into */
danielk1977b1bc9532004-05-22 03:05:33 +00002031){
drh3c685822005-05-21 18:32:18 +00002032 switch( serial_type ){
drh3c685822005-05-21 18:32:18 +00002033 case 10: /* Reserved for future use */
2034 case 11: /* Reserved for future use */
2035 case 0: { /* NULL */
2036 pMem->flags = MEM_Null;
2037 break;
2038 }
2039 case 1: { /* 1-byte signed integer */
drh3c024d62007-03-30 11:23:45 +00002040 pMem->u.i = (signed char)buf[0];
drh1483e142004-05-21 21:12:42 +00002041 pMem->flags = MEM_Int;
drh3c685822005-05-21 18:32:18 +00002042 return 1;
drh1483e142004-05-21 21:12:42 +00002043 }
drh3c685822005-05-21 18:32:18 +00002044 case 2: { /* 2-byte signed integer */
drh3c024d62007-03-30 11:23:45 +00002045 pMem->u.i = (((signed char)buf[0])<<8) | buf[1];
drh3c685822005-05-21 18:32:18 +00002046 pMem->flags = MEM_Int;
2047 return 2;
2048 }
2049 case 3: { /* 3-byte signed integer */
drh3c024d62007-03-30 11:23:45 +00002050 pMem->u.i = (((signed char)buf[0])<<16) | (buf[1]<<8) | buf[2];
drh3c685822005-05-21 18:32:18 +00002051 pMem->flags = MEM_Int;
2052 return 3;
2053 }
2054 case 4: { /* 4-byte signed integer */
drh3c024d62007-03-30 11:23:45 +00002055 pMem->u.i = (buf[0]<<24) | (buf[1]<<16) | (buf[2]<<8) | buf[3];
drh3c685822005-05-21 18:32:18 +00002056 pMem->flags = MEM_Int;
2057 return 4;
2058 }
2059 case 5: { /* 6-byte signed integer */
2060 u64 x = (((signed char)buf[0])<<8) | buf[1];
2061 u32 y = (buf[2]<<24) | (buf[3]<<16) | (buf[4]<<8) | buf[5];
2062 x = (x<<32) | y;
drh3c024d62007-03-30 11:23:45 +00002063 pMem->u.i = *(i64*)&x;
drh3c685822005-05-21 18:32:18 +00002064 pMem->flags = MEM_Int;
2065 return 6;
2066 }
drh91124b32005-08-18 18:15:05 +00002067 case 6: /* 8-byte signed integer */
drh3c685822005-05-21 18:32:18 +00002068 case 7: { /* IEEE floating point */
drhd81bd4e2005-09-05 20:06:49 +00002069 u64 x;
2070 u32 y;
drh2a3e4a72006-01-23 21:44:53 +00002071#if !defined(NDEBUG) && !defined(SQLITE_OMIT_FLOATING_POINT)
drhde941c62005-08-28 01:34:21 +00002072 /* Verify that integers and floating point values use the same
drh110daac2007-05-04 11:59:31 +00002073 ** byte order. Or, that if SQLITE_MIXED_ENDIAN_64BIT_FLOAT is
2074 ** defined that 64-bit floating point values really are mixed
2075 ** endian.
drhbfd6b032005-08-28 01:38:44 +00002076 */
drhde941c62005-08-28 01:34:21 +00002077 static const u64 t1 = ((u64)0x3ff00000)<<32;
drh4f0c5872007-03-26 22:05:01 +00002078 static const double r1 = 1.0;
drh60d09a72007-08-30 15:05:08 +00002079 u64 t2 = t1;
2080 swapMixedEndianFloat(t2);
2081 assert( sizeof(r1)==sizeof(t2) && memcmp(&r1, &t2, sizeof(r1))==0 );
drhde941c62005-08-28 01:34:21 +00002082#endif
drhbfd6b032005-08-28 01:38:44 +00002083
drhd81bd4e2005-09-05 20:06:49 +00002084 x = (buf[0]<<24) | (buf[1]<<16) | (buf[2]<<8) | buf[3];
2085 y = (buf[4]<<24) | (buf[5]<<16) | (buf[6]<<8) | buf[7];
drh3c685822005-05-21 18:32:18 +00002086 x = (x<<32) | y;
2087 if( serial_type==6 ){
drh3c024d62007-03-30 11:23:45 +00002088 pMem->u.i = *(i64*)&x;
drh3c685822005-05-21 18:32:18 +00002089 pMem->flags = MEM_Int;
2090 }else{
drh4f0c5872007-03-26 22:05:01 +00002091 assert( sizeof(x)==8 && sizeof(pMem->r)==8 );
drh60d09a72007-08-30 15:05:08 +00002092 swapMixedEndianFloat(x);
drh4f0c5872007-03-26 22:05:01 +00002093 memcpy(&pMem->r, &x, sizeof(x));
drh3c685822005-05-21 18:32:18 +00002094 pMem->flags = MEM_Real;
2095 }
2096 return 8;
2097 }
drhd946db02005-12-29 19:23:06 +00002098 case 8: /* Integer 0 */
2099 case 9: { /* Integer 1 */
drh3c024d62007-03-30 11:23:45 +00002100 pMem->u.i = serial_type-8;
drhd946db02005-12-29 19:23:06 +00002101 pMem->flags = MEM_Int;
2102 return 0;
2103 }
drh3c685822005-05-21 18:32:18 +00002104 default: {
2105 int len = (serial_type-12)/2;
2106 pMem->z = (char *)buf;
2107 pMem->n = len;
2108 pMem->xDel = 0;
2109 if( serial_type&0x01 ){
2110 pMem->flags = MEM_Str | MEM_Ephem;
2111 }else{
2112 pMem->flags = MEM_Blob | MEM_Ephem;
2113 }
2114 return len;
drh696b32f2004-05-30 01:51:52 +00002115 }
danielk1977cfcdaef2004-05-12 07:33:33 +00002116 }
drh3c685822005-05-21 18:32:18 +00002117 return 0;
danielk1977192ac1d2004-05-10 07:17:30 +00002118}
2119
2120/*
drh0e6082e2006-01-12 20:28:35 +00002121** The header of a record consists of a sequence variable-length integers.
2122** These integers are almost always small and are encoded as a single byte.
2123** The following macro takes advantage this fact to provide a fast decode
2124** of the integers in a record header. It is faster for the common case
2125** where the integer is a single byte. It is a little slower when the
2126** integer is two or more bytes. But overall it is faster.
2127**
2128** The following expressions are equivalent:
2129**
2130** x = sqlite3GetVarint32( A, &B );
2131**
2132** x = GetVarint( A, B );
2133**
2134*/
2135#define GetVarint(A,B) ((B = *(A))<=0x7f ? 1 : sqlite3GetVarint32(A, &B))
2136
2137/*
drh7a224de2004-06-02 01:22:02 +00002138** This function compares the two table rows or index records specified by
danielk1977eb015e02004-05-18 01:31:14 +00002139** {nKey1, pKey1} and {nKey2, pKey2}, returning a negative, zero
2140** or positive integer if {nKey1, pKey1} is less than, equal to or
drh7a224de2004-06-02 01:22:02 +00002141** greater than {nKey2, pKey2}. Both Key1 and Key2 must be byte strings
2142** composed by the OP_MakeRecord opcode of the VDBE.
danielk1977eb015e02004-05-18 01:31:14 +00002143*/
drh7a224de2004-06-02 01:22:02 +00002144int sqlite3VdbeRecordCompare(
danielk1977eb015e02004-05-18 01:31:14 +00002145 void *userData,
2146 int nKey1, const void *pKey1,
2147 int nKey2, const void *pKey2
2148){
drhd3d39e92004-05-20 22:16:29 +00002149 KeyInfo *pKeyInfo = (KeyInfo*)userData;
drhd3194f52004-05-27 19:59:32 +00002150 u32 d1, d2; /* Offset into aKey[] of next data element */
2151 u32 idx1, idx2; /* Offset into aKey[] of next header element */
2152 u32 szHdr1, szHdr2; /* Number of bytes in header */
2153 int i = 0;
2154 int nField;
2155 int rc = 0;
danielk1977eb015e02004-05-18 01:31:14 +00002156 const unsigned char *aKey1 = (const unsigned char *)pKey1;
2157 const unsigned char *aKey2 = (const unsigned char *)pKey2;
danielk19770202b292004-06-09 09:55:16 +00002158
2159 Mem mem1;
2160 Mem mem2;
2161 mem1.enc = pKeyInfo->enc;
drhb21c8cd2007-08-21 19:33:56 +00002162 mem1.db = pKeyInfo->db;
danielk19770202b292004-06-09 09:55:16 +00002163 mem2.enc = pKeyInfo->enc;
drhb21c8cd2007-08-21 19:33:56 +00002164 mem2.db = pKeyInfo->db;
drhd3194f52004-05-27 19:59:32 +00002165
drh0e6082e2006-01-12 20:28:35 +00002166 idx1 = GetVarint(aKey1, szHdr1);
drhd3194f52004-05-27 19:59:32 +00002167 d1 = szHdr1;
drh0e6082e2006-01-12 20:28:35 +00002168 idx2 = GetVarint(aKey2, szHdr2);
drhd3194f52004-05-27 19:59:32 +00002169 d2 = szHdr2;
2170 nField = pKeyInfo->nField;
drhd5788202004-05-28 08:21:05 +00002171 while( idx1<szHdr1 && idx2<szHdr2 ){
drhd3194f52004-05-27 19:59:32 +00002172 u32 serial_type1;
2173 u32 serial_type2;
danielk197784ac9d02004-05-18 09:58:06 +00002174
2175 /* Read the serial types for the next element in each key. */
drh0e6082e2006-01-12 20:28:35 +00002176 idx1 += GetVarint( aKey1+idx1, serial_type1 );
drhd5788202004-05-28 08:21:05 +00002177 if( d1>=nKey1 && sqlite3VdbeSerialTypeLen(serial_type1)>0 ) break;
drh0e6082e2006-01-12 20:28:35 +00002178 idx2 += GetVarint( aKey2+idx2, serial_type2 );
drhd5788202004-05-28 08:21:05 +00002179 if( d2>=nKey2 && sqlite3VdbeSerialTypeLen(serial_type2)>0 ) break;
danielk197784ac9d02004-05-18 09:58:06 +00002180
drh0660e262006-10-27 14:06:57 +00002181 /* Extract the values to be compared.
danielk197784ac9d02004-05-18 09:58:06 +00002182 */
drh25aa1b42004-05-28 01:39:01 +00002183 d1 += sqlite3VdbeSerialGet(&aKey1[d1], serial_type1, &mem1);
2184 d2 += sqlite3VdbeSerialGet(&aKey2[d2], serial_type2, &mem2);
danielk197784ac9d02004-05-18 09:58:06 +00002185
drh0660e262006-10-27 14:06:57 +00002186 /* Do the comparison
2187 */
drhd5788202004-05-28 08:21:05 +00002188 rc = sqlite3MemCompare(&mem1, &mem2, i<nField ? pKeyInfo->aColl[i] : 0);
drh3c685822005-05-21 18:32:18 +00002189 if( mem1.flags & MEM_Dyn ) sqlite3VdbeMemRelease(&mem1);
2190 if( mem2.flags & MEM_Dyn ) sqlite3VdbeMemRelease(&mem2);
danielk197784ac9d02004-05-18 09:58:06 +00002191 if( rc!=0 ){
drhd3194f52004-05-27 19:59:32 +00002192 break;
2193 }
2194 i++;
2195 }
2196
2197 /* One of the keys ran out of fields, but all the fields up to that point
2198 ** were equal. If the incrKey flag is true, then the second key is
2199 ** treated as larger.
2200 */
2201 if( rc==0 ){
2202 if( pKeyInfo->incrKey ){
drhd3194f52004-05-27 19:59:32 +00002203 rc = -1;
danielk19779a96b662007-11-29 17:05:18 +00002204 }else if( !pKeyInfo->prefixIsEqual ){
2205 if( d1<nKey1 ){
2206 rc = 1;
2207 }else if( d2<nKey2 ){
2208 rc = -1;
2209 }
danielk197784ac9d02004-05-18 09:58:06 +00002210 }
drh0b2f3162005-12-21 18:36:45 +00002211 }else if( pKeyInfo->aSortOrder && i<pKeyInfo->nField
2212 && pKeyInfo->aSortOrder[i] ){
drhd3194f52004-05-27 19:59:32 +00002213 rc = -rc;
2214 }
2215
2216 return rc;
danielk1977eb015e02004-05-18 01:31:14 +00002217}
drhd5788202004-05-28 08:21:05 +00002218
2219/*
drh7a224de2004-06-02 01:22:02 +00002220** The argument is an index entry composed using the OP_MakeRecord opcode.
2221** The last entry in this record should be an integer (specifically
2222** an integer rowid). This routine returns the number of bytes in
2223** that integer.
drhd5788202004-05-28 08:21:05 +00002224*/
drh74161702006-02-24 02:53:49 +00002225int sqlite3VdbeIdxRowidLen(const u8 *aKey){
drhd5788202004-05-28 08:21:05 +00002226 u32 szHdr; /* Size of the header */
2227 u32 typeRowid; /* Serial type of the rowid */
2228
2229 sqlite3GetVarint32(aKey, &szHdr);
2230 sqlite3GetVarint32(&aKey[szHdr-1], &typeRowid);
2231 return sqlite3VdbeSerialTypeLen(typeRowid);
2232}
danielk1977eb015e02004-05-18 01:31:14 +00002233
2234
2235/*
drh7a224de2004-06-02 01:22:02 +00002236** pCur points at an index entry created using the OP_MakeRecord opcode.
2237** Read the rowid (the last field in the record) and store it in *rowid.
2238** Return SQLITE_OK if everything works, or an error code otherwise.
danielk1977183f9f72004-05-13 05:20:26 +00002239*/
drhb21c8cd2007-08-21 19:33:56 +00002240int sqlite3VdbeIdxRowid(BtCursor *pCur, i64 *rowid){
drh61fc5952007-04-01 23:49:51 +00002241 i64 nCellKey = 0;
danielk1977183f9f72004-05-13 05:20:26 +00002242 int rc;
drhd5788202004-05-28 08:21:05 +00002243 u32 szHdr; /* Size of the header */
2244 u32 typeRowid; /* Serial type of the rowid */
2245 u32 lenRowid; /* Size of the rowid */
2246 Mem m, v;
danielk1977183f9f72004-05-13 05:20:26 +00002247
drhd5788202004-05-28 08:21:05 +00002248 sqlite3BtreeKeySize(pCur, &nCellKey);
2249 if( nCellKey<=0 ){
drh49285702005-09-17 15:20:26 +00002250 return SQLITE_CORRUPT_BKPT;
drhd5788202004-05-28 08:21:05 +00002251 }
drhb21c8cd2007-08-21 19:33:56 +00002252 rc = sqlite3VdbeMemFromBtree(pCur, 0, nCellKey, 1, &m);
drhd5788202004-05-28 08:21:05 +00002253 if( rc ){
danielk1977183f9f72004-05-13 05:20:26 +00002254 return rc;
2255 }
drh2646da72005-12-09 20:02:05 +00002256 sqlite3GetVarint32((u8*)m.z, &szHdr);
2257 sqlite3GetVarint32((u8*)&m.z[szHdr-1], &typeRowid);
drhd5788202004-05-28 08:21:05 +00002258 lenRowid = sqlite3VdbeSerialTypeLen(typeRowid);
drh2646da72005-12-09 20:02:05 +00002259 sqlite3VdbeSerialGet((u8*)&m.z[m.n-lenRowid], typeRowid, &v);
drh3c024d62007-03-30 11:23:45 +00002260 *rowid = v.u.i;
danielk1977d8123362004-06-12 09:25:12 +00002261 sqlite3VdbeMemRelease(&m);
danielk1977183f9f72004-05-13 05:20:26 +00002262 return SQLITE_OK;
2263}
2264
drh7cf6e4d2004-05-19 14:56:55 +00002265/*
drhd3d39e92004-05-20 22:16:29 +00002266** Compare the key of the index entry that cursor pC is point to against
drh7cf6e4d2004-05-19 14:56:55 +00002267** the key string in pKey (of length nKey). Write into *pRes a number
2268** that is negative, zero, or positive if pC is less than, equal to,
2269** or greater than pKey. Return SQLITE_OK on success.
drhd3d39e92004-05-20 22:16:29 +00002270**
drhd5788202004-05-28 08:21:05 +00002271** pKey is either created without a rowid or is truncated so that it
2272** omits the rowid at the end. The rowid at the end of the index entry
2273** is ignored as well.
drh7cf6e4d2004-05-19 14:56:55 +00002274*/
danielk1977183f9f72004-05-13 05:20:26 +00002275int sqlite3VdbeIdxKeyCompare(
drh7cf6e4d2004-05-19 14:56:55 +00002276 Cursor *pC, /* The cursor to compare against */
2277 int nKey, const u8 *pKey, /* The key to compare */
2278 int *res /* Write the comparison result here */
danielk1977183f9f72004-05-13 05:20:26 +00002279){
drh61fc5952007-04-01 23:49:51 +00002280 i64 nCellKey = 0;
danielk1977183f9f72004-05-13 05:20:26 +00002281 int rc;
danielk19773d1bfea2004-05-14 11:00:53 +00002282 BtCursor *pCur = pC->pCursor;
drhd5788202004-05-28 08:21:05 +00002283 int lenRowid;
2284 Mem m;
danielk1977183f9f72004-05-13 05:20:26 +00002285
2286 sqlite3BtreeKeySize(pCur, &nCellKey);
2287 if( nCellKey<=0 ){
2288 *res = 0;
2289 return SQLITE_OK;
2290 }
drhb21c8cd2007-08-21 19:33:56 +00002291 rc = sqlite3VdbeMemFromBtree(pC->pCursor, 0, nCellKey, 1, &m);
drhd5788202004-05-28 08:21:05 +00002292 if( rc ){
2293 return rc;
danielk1977183f9f72004-05-13 05:20:26 +00002294 }
drh74161702006-02-24 02:53:49 +00002295 lenRowid = sqlite3VdbeIdxRowidLen((u8*)m.z);
drh7a224de2004-06-02 01:22:02 +00002296 *res = sqlite3VdbeRecordCompare(pC->pKeyInfo, m.n-lenRowid, m.z, nKey, pKey);
danielk1977d8123362004-06-12 09:25:12 +00002297 sqlite3VdbeMemRelease(&m);
danielk1977183f9f72004-05-13 05:20:26 +00002298 return SQLITE_OK;
2299}
danielk1977b28af712004-06-21 06:50:26 +00002300
2301/*
2302** This routine sets the value to be returned by subsequent calls to
2303** sqlite3_changes() on the database handle 'db'.
2304*/
2305void sqlite3VdbeSetChanges(sqlite3 *db, int nChange){
drhb21c8cd2007-08-21 19:33:56 +00002306 assert( sqlite3_mutex_held(db->mutex) );
danielk1977b28af712004-06-21 06:50:26 +00002307 db->nChange = nChange;
2308 db->nTotalChange += nChange;
2309}
2310
2311/*
2312** Set a flag in the vdbe to update the change counter when it is finalised
2313** or reset.
2314*/
drh4794f732004-11-05 17:17:50 +00002315void sqlite3VdbeCountChanges(Vdbe *v){
2316 v->changeCntOn = 1;
danielk1977b28af712004-06-21 06:50:26 +00002317}
drhd89bd002005-01-22 03:03:54 +00002318
2319/*
2320** Mark every prepared statement associated with a database connection
2321** as expired.
2322**
2323** An expired statement means that recompilation of the statement is
2324** recommend. Statements expire when things happen that make their
2325** programs obsolete. Removing user-defined functions or collating
2326** sequences, or changing an authorization function are the types of
2327** things that make prepared statements obsolete.
2328*/
2329void sqlite3ExpirePreparedStatements(sqlite3 *db){
2330 Vdbe *p;
2331 for(p = db->pVdbe; p; p=p->pNext){
2332 p->expired = 1;
2333 }
2334}
danielk1977aee18ef2005-03-09 12:26:50 +00002335
2336/*
2337** Return the database associated with the Vdbe.
2338*/
2339sqlite3 *sqlite3VdbeDb(Vdbe *v){
2340 return v->db;
2341}