blob: c15207b9bed216f7f79214eeb4cd16266f426327 [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/*
drh9cbf3422008-01-17 16:22:13 +0000236** Loop through the program looking for P2 values that are negative
237** on jump instructions. Each such value is a label. Resolve the
238** label by setting the P2 value to its correct non-zero value.
drh76ff3a02004-09-24 22:32:30 +0000239**
240** This routine is called once after all opcodes have been inserted.
danielk1977634f2982005-03-28 08:44:07 +0000241**
drh13449892005-09-07 21:22:45 +0000242** Variable *pMaxFuncArgs is set to the maximum value of any P2 argument
danielk1977399918f2006-06-14 13:03:23 +0000243** to an OP_Function, OP_AggStep or OP_VFilter opcode. This is used by
danielk1977634f2982005-03-28 08:44:07 +0000244** sqlite3VdbeMakeReady() to size the Vdbe.apArg[] array.
danielk1977bc04f852005-03-29 08:26:13 +0000245**
drh38449902005-06-07 01:43:41 +0000246** This routine also does the following optimization: It scans for
drh77658e22007-12-04 16:54:52 +0000247** instructions that might cause a statement rollback. Such instructions
248** are:
249**
250** * OP_Halt with P1=SQLITE_CONSTRAINT and P2=OE_Abort.
251** * OP_Destroy
252** * OP_VUpdate
253** * OP_VRename
254**
255** If no such instruction is found, then every Statement instruction
256** is changed to a Noop. In this way, we avoid creating the statement
257** journal file unnecessarily.
drh76ff3a02004-09-24 22:32:30 +0000258*/
drh9cbf3422008-01-17 16:22:13 +0000259static void resolveP2Values(Vdbe *p, int *pMaxFuncArgs){
drh76ff3a02004-09-24 22:32:30 +0000260 int i;
danielk1977bc04f852005-03-29 08:26:13 +0000261 int nMaxArgs = 0;
drh76ff3a02004-09-24 22:32:30 +0000262 Op *pOp;
263 int *aLabel = p->aLabel;
drh38449902005-06-07 01:43:41 +0000264 int doesStatementRollback = 0;
265 int hasStatementBegin = 0;
drh76ff3a02004-09-24 22:32:30 +0000266 for(pOp=p->aOp, i=p->nOp-1; i>=0; i--, pOp++){
danielk1977634f2982005-03-28 08:44:07 +0000267 u8 opcode = pOp->opcode;
268
drh98757152008-01-09 23:04:12 +0000269 if( opcode==OP_Function ){
270 if( pOp->p5>nMaxArgs ) nMaxArgs = pOp->p5;
271 }else if( opcode==OP_AggStep
danielk1977399918f2006-06-14 13:03:23 +0000272#ifndef SQLITE_OMIT_VIRTUALTABLE
danielk19774b2688a2006-06-20 11:01:07 +0000273 || opcode==OP_VUpdate
danielk1977399918f2006-06-14 13:03:23 +0000274#endif
275 ){
danielk1977bc04f852005-03-29 08:26:13 +0000276 if( pOp->p2>nMaxArgs ) nMaxArgs = pOp->p2;
danielk1977182c4ba2007-06-27 15:53:34 +0000277 }
278 if( opcode==OP_Halt ){
drh38449902005-06-07 01:43:41 +0000279 if( pOp->p1==SQLITE_CONSTRAINT && pOp->p2==OE_Abort ){
280 doesStatementRollback = 1;
281 }
drh38449902005-06-07 01:43:41 +0000282 }else if( opcode==OP_Statement ){
283 hasStatementBegin = 1;
drh77658e22007-12-04 16:54:52 +0000284 }else if( opcode==OP_Destroy ){
285 doesStatementRollback = 1;
danielk1977182c4ba2007-06-27 15:53:34 +0000286#ifndef SQLITE_OMIT_VIRTUALTABLE
287 }else if( opcode==OP_VUpdate || opcode==OP_VRename ){
288 doesStatementRollback = 1;
drh4be8b512006-06-13 23:51:34 +0000289 }else if( opcode==OP_VFilter ){
290 int n;
291 assert( p->nOp - i >= 3 );
drh4c583122008-01-04 22:01:03 +0000292 assert( pOp[-1].opcode==OP_Integer );
danielk19776dbee812008-01-03 18:39:41 +0000293 n = pOp[-1].p1;
drh4be8b512006-06-13 23:51:34 +0000294 if( n>nMaxArgs ) nMaxArgs = n;
danielk1977182c4ba2007-06-27 15:53:34 +0000295#endif
danielk1977bc04f852005-03-29 08:26:13 +0000296 }
danielk1977634f2982005-03-28 08:44:07 +0000297
drhd2981512008-01-04 19:33:49 +0000298 if( sqlite3VdbeOpcodeHasProperty(opcode, OPFLG_JUMP) && pOp->p2<0 ){
299 assert( -1-pOp->p2<p->nLabel );
300 pOp->p2 = aLabel[-1-pOp->p2];
301 }
drh76ff3a02004-09-24 22:32:30 +0000302 }
drh17435752007-08-16 04:30:38 +0000303 sqlite3_free(p->aLabel);
drh76ff3a02004-09-24 22:32:30 +0000304 p->aLabel = 0;
danielk1977bc04f852005-03-29 08:26:13 +0000305
306 *pMaxFuncArgs = nMaxArgs;
drh38449902005-06-07 01:43:41 +0000307
308 /* If we never rollback a statement transaction, then statement
309 ** transactions are not needed. So change every OP_Statement
drh66560ad2006-01-06 14:32:19 +0000310 ** opcode into an OP_Noop. This avoid a call to sqlite3OsOpenExclusive()
drh38449902005-06-07 01:43:41 +0000311 ** which can be expensive on some platforms.
312 */
313 if( hasStatementBegin && !doesStatementRollback ){
314 for(pOp=p->aOp, i=p->nOp-1; i>=0; i--, pOp++){
315 if( pOp->opcode==OP_Statement ){
316 pOp->opcode = OP_Noop;
317 }
318 }
319 }
drh76ff3a02004-09-24 22:32:30 +0000320}
321
322/*
drh9a324642003-09-06 20:12:01 +0000323** Return the address of the next instruction to be inserted.
324*/
danielk19774adee202004-05-08 08:23:19 +0000325int sqlite3VdbeCurrentAddr(Vdbe *p){
drh9a324642003-09-06 20:12:01 +0000326 assert( p->magic==VDBE_MAGIC_INIT );
327 return p->nOp;
328}
329
330/*
331** Add a whole list of operations to the operation stack. Return the
332** address of the first operation added.
333*/
danielk19774adee202004-05-08 08:23:19 +0000334int sqlite3VdbeAddOpList(Vdbe *p, int nOp, VdbeOpList const *aOp){
drh9a324642003-09-06 20:12:01 +0000335 int addr;
336 assert( p->magic==VDBE_MAGIC_INIT );
drha4e5d582007-10-20 15:41:57 +0000337 if( p->nOp + nOp > p->nOpAlloc ){
338 resizeOpArray(p, p->nOp*2 + nOp);
339 }
drh17435752007-08-16 04:30:38 +0000340 if( p->db->mallocFailed ){
drh76ff3a02004-09-24 22:32:30 +0000341 return 0;
drh9a324642003-09-06 20:12:01 +0000342 }
343 addr = p->nOp;
344 if( nOp>0 ){
345 int i;
drh905793e2004-02-21 13:31:09 +0000346 VdbeOpList const *pIn = aOp;
347 for(i=0; i<nOp; i++, pIn++){
348 int p2 = pIn->p2;
349 VdbeOp *pOut = &p->aOp[i+addr];
350 pOut->opcode = pIn->opcode;
351 pOut->p1 = pIn->p1;
drh8558cde2008-01-05 05:20:10 +0000352 if( p2<0 && sqlite3VdbeOpcodeHasProperty(pOut->opcode, OPFLG_JUMP) ){
353 pOut->p2 = addr + ADDR(p2);
354 }else{
355 pOut->p2 = p2;
356 }
drh24003452008-01-03 01:28:59 +0000357 pOut->p3 = pIn->p3;
358 pOut->p4type = P4_NOTUSED;
359 pOut->p4.p = 0;
360 pOut->p5 = 0;
danielk19778b60e0f2005-01-12 09:10:39 +0000361#ifdef SQLITE_DEBUG
danielk1977132872b2004-05-10 10:37:18 +0000362 if( sqlite3_vdbe_addop_trace ){
danielk19774adee202004-05-08 08:23:19 +0000363 sqlite3VdbePrintOp(0, i+addr, &p->aOp[i+addr]);
drh9a324642003-09-06 20:12:01 +0000364 }
365#endif
366 }
367 p->nOp += nOp;
368 }
369 return addr;
370}
371
372/*
373** Change the value of the P1 operand for a specific instruction.
374** This routine is useful when a large program is loaded from a
danielk19774adee202004-05-08 08:23:19 +0000375** static array using sqlite3VdbeAddOpList but we want to make a
drh9a324642003-09-06 20:12:01 +0000376** few minor changes to the program.
377*/
danielk19774adee202004-05-08 08:23:19 +0000378void sqlite3VdbeChangeP1(Vdbe *p, int addr, int val){
drh8aa34ae2006-03-13 12:54:09 +0000379 assert( p==0 || p->magic==VDBE_MAGIC_INIT );
drh9a324642003-09-06 20:12:01 +0000380 if( p && addr>=0 && p->nOp>addr && p->aOp ){
381 p->aOp[addr].p1 = val;
382 }
383}
384
385/*
386** Change the value of the P2 operand for a specific instruction.
387** This routine is useful for setting a jump destination.
388*/
danielk19774adee202004-05-08 08:23:19 +0000389void sqlite3VdbeChangeP2(Vdbe *p, int addr, int val){
drh8aa34ae2006-03-13 12:54:09 +0000390 assert( p==0 || p->magic==VDBE_MAGIC_INIT );
drh9a324642003-09-06 20:12:01 +0000391 if( p && addr>=0 && p->nOp>addr && p->aOp ){
392 p->aOp[addr].p2 = val;
393 }
394}
395
drhd654be82005-09-20 17:42:23 +0000396/*
danielk19771f4aa332008-01-03 09:51:55 +0000397** Change the value of the P3 operand for a specific instruction.
danielk1977207872a2008-01-03 07:54:23 +0000398*/
399void sqlite3VdbeChangeP3(Vdbe *p, int addr, int val){
400 assert( p==0 || p->magic==VDBE_MAGIC_INIT );
401 if( p && addr>=0 && p->nOp>addr && p->aOp ){
402 p->aOp[addr].p3 = val;
403 }
404}
405
406/*
drh35573352008-01-08 23:54:25 +0000407** Change the value of the P5 operand for the most recently
408** added operation.
danielk19771f4aa332008-01-03 09:51:55 +0000409*/
drh35573352008-01-08 23:54:25 +0000410void sqlite3VdbeChangeP5(Vdbe *p, u8 val){
danielk19771f4aa332008-01-03 09:51:55 +0000411 assert( p==0 || p->magic==VDBE_MAGIC_INIT );
drh35573352008-01-08 23:54:25 +0000412 if( p && p->aOp ){
413 assert( p->nOp>0 );
414 p->aOp[p->nOp-1].p5 = val;
danielk19771f4aa332008-01-03 09:51:55 +0000415 }
416}
417
418/*
drhf8875402006-03-17 13:56:34 +0000419** Change the P2 operand of instruction addr so that it points to
drhd654be82005-09-20 17:42:23 +0000420** the address of the next instruction to be coded.
421*/
422void sqlite3VdbeJumpHere(Vdbe *p, int addr){
423 sqlite3VdbeChangeP2(p, addr, p->nOp);
424}
drhb38ad992005-09-16 00:27:01 +0000425
drhb7f6f682006-07-08 17:06:43 +0000426
427/*
428** If the input FuncDef structure is ephemeral, then free it. If
429** the FuncDef is not ephermal, then do nothing.
430*/
431static void freeEphemeralFunction(FuncDef *pDef){
432 if( pDef && (pDef->flags & SQLITE_FUNC_EPHEM)!=0 ){
drh17435752007-08-16 04:30:38 +0000433 sqlite3_free(pDef);
drhb7f6f682006-07-08 17:06:43 +0000434 }
435}
436
drhb38ad992005-09-16 00:27:01 +0000437/*
drh66a51672008-01-03 00:01:23 +0000438** Delete a P4 value if necessary.
drhb38ad992005-09-16 00:27:01 +0000439*/
drh66a51672008-01-03 00:01:23 +0000440static void freeP4(int p4type, void *p3){
drhb38ad992005-09-16 00:27:01 +0000441 if( p3 ){
drh66a51672008-01-03 00:01:23 +0000442 switch( p4type ){
443 case P4_REAL:
444 case P4_INT64:
445 case P4_MPRINTF:
446 case P4_DYNAMIC:
447 case P4_KEYINFO:
448 case P4_KEYINFO_HANDOFF: {
drh17435752007-08-16 04:30:38 +0000449 sqlite3_free(p3);
drhac1733d2005-09-17 17:58:22 +0000450 break;
451 }
drh66a51672008-01-03 00:01:23 +0000452 case P4_VDBEFUNC: {
drhac1733d2005-09-17 17:58:22 +0000453 VdbeFunc *pVdbeFunc = (VdbeFunc *)p3;
drhb7f6f682006-07-08 17:06:43 +0000454 freeEphemeralFunction(pVdbeFunc->pFunc);
drhac1733d2005-09-17 17:58:22 +0000455 sqlite3VdbeDeleteAuxData(pVdbeFunc, 0);
drh17435752007-08-16 04:30:38 +0000456 sqlite3_free(pVdbeFunc);
drhac1733d2005-09-17 17:58:22 +0000457 break;
458 }
drh66a51672008-01-03 00:01:23 +0000459 case P4_FUNCDEF: {
drhb7f6f682006-07-08 17:06:43 +0000460 freeEphemeralFunction((FuncDef*)p3);
461 break;
462 }
drh66a51672008-01-03 00:01:23 +0000463 case P4_MEM: {
drhac1733d2005-09-17 17:58:22 +0000464 sqlite3ValueFree((sqlite3_value*)p3);
465 break;
466 }
drhb38ad992005-09-16 00:27:01 +0000467 }
468 }
469}
470
471
drh9a324642003-09-06 20:12:01 +0000472/*
drhf8875402006-03-17 13:56:34 +0000473** Change N opcodes starting at addr to No-ops.
474*/
475void sqlite3VdbeChangeToNoop(Vdbe *p, int addr, int N){
danielk197792d4d7a2007-05-04 12:05:56 +0000476 if( p && p->aOp ){
477 VdbeOp *pOp = &p->aOp[addr];
478 while( N-- ){
drh66a51672008-01-03 00:01:23 +0000479 freeP4(pOp->p4type, pOp->p4.p);
danielk197792d4d7a2007-05-04 12:05:56 +0000480 memset(pOp, 0, sizeof(pOp[0]));
481 pOp->opcode = OP_Noop;
482 pOp++;
483 }
drhf8875402006-03-17 13:56:34 +0000484 }
485}
486
487/*
drh66a51672008-01-03 00:01:23 +0000488** Change the value of the P4 operand for a specific instruction.
drh9a324642003-09-06 20:12:01 +0000489** This routine is useful when a large program is loaded from a
danielk19774adee202004-05-08 08:23:19 +0000490** static array using sqlite3VdbeAddOpList but we want to make a
drh9a324642003-09-06 20:12:01 +0000491** few minor changes to the program.
492**
drh66a51672008-01-03 00:01:23 +0000493** If n>=0 then the P4 operand is dynamic, meaning that a copy of
drh17435752007-08-16 04:30:38 +0000494** the string is made into memory obtained from sqlite3_malloc().
drh66a51672008-01-03 00:01:23 +0000495** A value of n==0 means copy bytes of zP4 up to and including the
496** first null byte. If n>0 then copy n+1 bytes of zP4.
drh9a324642003-09-06 20:12:01 +0000497**
drh66a51672008-01-03 00:01:23 +0000498** If n==P4_KEYINFO it means that zP4 is a pointer to a KeyInfo structure.
danielk19771f55c052005-05-19 08:42:59 +0000499** A copy is made of the KeyInfo structure into memory obtained from
drh17435752007-08-16 04:30:38 +0000500** sqlite3_malloc, to be freed when the Vdbe is finalized.
drh66a51672008-01-03 00:01:23 +0000501** n==P4_KEYINFO_HANDOFF indicates that zP4 points to a KeyInfo structure
drh17435752007-08-16 04:30:38 +0000502** stored in memory that the caller has obtained from sqlite3_malloc. The
danielk19771f55c052005-05-19 08:42:59 +0000503** caller should not free the allocation, it will be freed when the Vdbe is
504** finalized.
505**
drh66a51672008-01-03 00:01:23 +0000506** Other values of n (P4_STATIC, P4_COLLSEQ etc.) indicate that zP4 points
danielk19771f55c052005-05-19 08:42:59 +0000507** to a string or structure that is guaranteed to exist for the lifetime of
508** the Vdbe. In these cases we can just copy the pointer.
drh9a324642003-09-06 20:12:01 +0000509**
drh66a51672008-01-03 00:01:23 +0000510** If addr<0 then change P4 on the most recently inserted instruction.
drh9a324642003-09-06 20:12:01 +0000511*/
drh66a51672008-01-03 00:01:23 +0000512void sqlite3VdbeChangeP4(Vdbe *p, int addr, const char *zP4, int n){
drh9a324642003-09-06 20:12:01 +0000513 Op *pOp;
drh91fd4d42008-01-19 20:11:25 +0000514 assert( p!=0 );
515 assert( p->magic==VDBE_MAGIC_INIT );
516 if( p->aOp==0 || p->db->mallocFailed ){
drh66a51672008-01-03 00:01:23 +0000517 if (n != P4_KEYINFO) {
518 freeP4(n, (void*)*(char**)&zP4);
danielk1977261919c2005-12-06 12:52:59 +0000519 }
danielk1977d5d56522005-03-16 12:15:20 +0000520 return;
521 }
drh91fd4d42008-01-19 20:11:25 +0000522 assert( addr<p->nOp );
523 if( addr<0 ){
drh9a324642003-09-06 20:12:01 +0000524 addr = p->nOp - 1;
525 if( addr<0 ) return;
526 }
527 pOp = &p->aOp[addr];
drh66a51672008-01-03 00:01:23 +0000528 freeP4(pOp->p4type, pOp->p4.p);
529 pOp->p4.p = 0;
drh98757152008-01-09 23:04:12 +0000530 if( n==P4_INT32 ){
531 pOp->p4.i = (int)zP4;
532 pOp->p4type = n;
533 }else if( zP4==0 ){
drh66a51672008-01-03 00:01:23 +0000534 pOp->p4.p = 0;
535 pOp->p4type = P4_NOTUSED;
536 }else if( n==P4_KEYINFO ){
drhd3d39e92004-05-20 22:16:29 +0000537 KeyInfo *pKeyInfo;
538 int nField, nByte;
drh4db38a72005-09-01 12:16:28 +0000539
drh66a51672008-01-03 00:01:23 +0000540 nField = ((KeyInfo*)zP4)->nField;
drhfdd6e852005-12-16 01:06:16 +0000541 nByte = sizeof(*pKeyInfo) + (nField-1)*sizeof(pKeyInfo->aColl[0]) + nField;
drh17435752007-08-16 04:30:38 +0000542 pKeyInfo = sqlite3_malloc( nByte );
danielk19772dca4ac2008-01-03 11:50:29 +0000543 pOp->p4.pKeyInfo = pKeyInfo;
drhd3d39e92004-05-20 22:16:29 +0000544 if( pKeyInfo ){
drh66a51672008-01-03 00:01:23 +0000545 memcpy(pKeyInfo, zP4, nByte);
drh91fd4d42008-01-19 20:11:25 +0000546 /* In the current implementation, P4_KEYINFO is only ever used on
547 ** KeyInfo structures that have no aSortOrder component. Elements
548 ** with an aSortOrder always use P4_KEYINFO_HANDOFF. So we do not
549 ** need to bother with duplicating the aSortOrder. */
550 assert( pKeyInfo->aSortOrder==0 );
551#if 0
drhfdd6e852005-12-16 01:06:16 +0000552 aSortOrder = pKeyInfo->aSortOrder;
553 if( aSortOrder ){
danielk1977bab45c62006-01-16 15:14:27 +0000554 pKeyInfo->aSortOrder = (unsigned char*)&pKeyInfo->aColl[nField];
drhfdd6e852005-12-16 01:06:16 +0000555 memcpy(pKeyInfo->aSortOrder, aSortOrder, nField);
556 }
drh91fd4d42008-01-19 20:11:25 +0000557#endif
drh66a51672008-01-03 00:01:23 +0000558 pOp->p4type = P4_KEYINFO;
drhd3d39e92004-05-20 22:16:29 +0000559 }else{
drh17435752007-08-16 04:30:38 +0000560 p->db->mallocFailed = 1;
drh66a51672008-01-03 00:01:23 +0000561 pOp->p4type = P4_NOTUSED;
drhd3d39e92004-05-20 22:16:29 +0000562 }
drh66a51672008-01-03 00:01:23 +0000563 }else if( n==P4_KEYINFO_HANDOFF ){
danielk19772dca4ac2008-01-03 11:50:29 +0000564 pOp->p4.p = (void*)zP4;
drh66a51672008-01-03 00:01:23 +0000565 pOp->p4type = P4_KEYINFO;
drh9a324642003-09-06 20:12:01 +0000566 }else if( n<0 ){
danielk19772dca4ac2008-01-03 11:50:29 +0000567 pOp->p4.p = (void*)zP4;
drh66a51672008-01-03 00:01:23 +0000568 pOp->p4type = n;
drh9a324642003-09-06 20:12:01 +0000569 }else{
drh66a51672008-01-03 00:01:23 +0000570 if( n==0 ) n = strlen(zP4);
danielk19772dca4ac2008-01-03 11:50:29 +0000571 pOp->p4.z = sqlite3DbStrNDup(p->db, zP4, n);
drh66a51672008-01-03 00:01:23 +0000572 pOp->p4type = P4_DYNAMIC;
drh9a324642003-09-06 20:12:01 +0000573 }
574}
575
drhad6d9462004-09-19 02:15:24 +0000576#ifndef NDEBUG
577/*
drhd4e70eb2008-01-02 00:34:36 +0000578** Change the comment on the the most recently coded instruction.
drhad6d9462004-09-19 02:15:24 +0000579*/
580void sqlite3VdbeComment(Vdbe *p, const char *zFormat, ...){
581 va_list ap;
danielk197701256832007-04-18 14:24:32 +0000582 assert( p->nOp>0 || p->aOp==0 );
drhd4e70eb2008-01-02 00:34:36 +0000583 assert( p->aOp==0 || p->aOp[p->nOp-1].zComment==0 || p->db->mallocFailed );
danielk1977dba01372008-01-05 18:44:29 +0000584 if( p->nOp ){
drh8cc74322008-01-15 02:22:24 +0000585 char **pz = &p->aOp[p->nOp-1].zComment;
danielk1977dba01372008-01-05 18:44:29 +0000586 va_start(ap, zFormat);
drh8cc74322008-01-15 02:22:24 +0000587 sqlite3_free(*pz);
588 *pz = sqlite3VMPrintf(p->db, zFormat, ap);
danielk1977dba01372008-01-05 18:44:29 +0000589 va_end(ap);
590 }
drhad6d9462004-09-19 02:15:24 +0000591}
592#endif
593
drh9a324642003-09-06 20:12:01 +0000594/*
drh9a324642003-09-06 20:12:01 +0000595** Return the opcode for a given address.
596*/
danielk19774adee202004-05-08 08:23:19 +0000597VdbeOp *sqlite3VdbeGetOp(Vdbe *p, int addr){
drh9a324642003-09-06 20:12:01 +0000598 assert( p->magic==VDBE_MAGIC_INIT );
drh17435752007-08-16 04:30:38 +0000599 assert( (addr>=0 && addr<p->nOp) || p->db->mallocFailed );
danielk197701256832007-04-18 14:24:32 +0000600 return ((addr>=0 && addr<p->nOp)?(&p->aOp[addr]):0);
drh9a324642003-09-06 20:12:01 +0000601}
602
drhb7f91642004-10-31 02:22:47 +0000603#if !defined(SQLITE_OMIT_EXPLAIN) || !defined(NDEBUG) \
604 || defined(VDBE_PROFILE) || defined(SQLITE_DEBUG)
drh9a324642003-09-06 20:12:01 +0000605/*
drh66a51672008-01-03 00:01:23 +0000606** Compute a string that describes the P4 parameter for an opcode.
drhd3d39e92004-05-20 22:16:29 +0000607** Use zTemp for any required temporary buffer space.
608*/
drh66a51672008-01-03 00:01:23 +0000609static char *displayP4(Op *pOp, char *zTemp, int nTemp){
610 char *zP4 = zTemp;
drhd3d39e92004-05-20 22:16:29 +0000611 assert( nTemp>=20 );
drh66a51672008-01-03 00:01:23 +0000612 switch( pOp->p4type ){
613 case P4_KEYINFO: {
drhd3d39e92004-05-20 22:16:29 +0000614 int i, j;
danielk19772dca4ac2008-01-03 11:50:29 +0000615 KeyInfo *pKeyInfo = pOp->p4.pKeyInfo;
drh5bb3eb92007-05-04 13:15:55 +0000616 sqlite3_snprintf(nTemp, zTemp, "keyinfo(%d", pKeyInfo->nField);
drhd3d39e92004-05-20 22:16:29 +0000617 i = strlen(zTemp);
618 for(j=0; j<pKeyInfo->nField; j++){
619 CollSeq *pColl = pKeyInfo->aColl[j];
620 if( pColl ){
621 int n = strlen(pColl->zName);
622 if( i+n>nTemp-6 ){
drh5bb3eb92007-05-04 13:15:55 +0000623 memcpy(&zTemp[i],",...",4);
drhd3d39e92004-05-20 22:16:29 +0000624 break;
625 }
626 zTemp[i++] = ',';
drhffbc3082004-05-21 01:29:06 +0000627 if( pKeyInfo->aSortOrder && pKeyInfo->aSortOrder[j] ){
drhd3d39e92004-05-20 22:16:29 +0000628 zTemp[i++] = '-';
629 }
drh5bb3eb92007-05-04 13:15:55 +0000630 memcpy(&zTemp[i], pColl->zName,n+1);
drhd3d39e92004-05-20 22:16:29 +0000631 i += n;
632 }else if( i+4<nTemp-6 ){
drh5bb3eb92007-05-04 13:15:55 +0000633 memcpy(&zTemp[i],",nil",4);
drhd3d39e92004-05-20 22:16:29 +0000634 i += 4;
635 }
636 }
637 zTemp[i++] = ')';
638 zTemp[i] = 0;
639 assert( i<nTemp );
drhd3d39e92004-05-20 22:16:29 +0000640 break;
641 }
drh66a51672008-01-03 00:01:23 +0000642 case P4_COLLSEQ: {
danielk19772dca4ac2008-01-03 11:50:29 +0000643 CollSeq *pColl = pOp->p4.pColl;
drh5bb3eb92007-05-04 13:15:55 +0000644 sqlite3_snprintf(nTemp, zTemp, "collseq(%.20s)", pColl->zName);
drhd3d39e92004-05-20 22:16:29 +0000645 break;
646 }
drh66a51672008-01-03 00:01:23 +0000647 case P4_FUNCDEF: {
danielk19772dca4ac2008-01-03 11:50:29 +0000648 FuncDef *pDef = pOp->p4.pFunc;
drha967e882006-06-13 01:04:52 +0000649 sqlite3_snprintf(nTemp, zTemp, "%s(%d)", pDef->zName, pDef->nArg);
drhf9b596e2004-05-26 16:54:42 +0000650 break;
651 }
drh66a51672008-01-03 00:01:23 +0000652 case P4_INT64: {
danielk19772dca4ac2008-01-03 11:50:29 +0000653 sqlite3_snprintf(nTemp, zTemp, "%lld", *pOp->p4.pI64);
drhd4e70eb2008-01-02 00:34:36 +0000654 break;
655 }
drh66a51672008-01-03 00:01:23 +0000656 case P4_INT32: {
657 sqlite3_snprintf(nTemp, zTemp, "%d", pOp->p4.i);
drh598f1342007-10-23 15:39:45 +0000658 break;
659 }
drh66a51672008-01-03 00:01:23 +0000660 case P4_REAL: {
danielk19772dca4ac2008-01-03 11:50:29 +0000661 sqlite3_snprintf(nTemp, zTemp, "%.16g", *pOp->p4.pReal);
drhd4e70eb2008-01-02 00:34:36 +0000662 break;
663 }
drh66a51672008-01-03 00:01:23 +0000664 case P4_MEM: {
danielk19772dca4ac2008-01-03 11:50:29 +0000665 Mem *pMem = pOp->p4.pMem;
drhc4dd3fd2008-01-22 01:48:05 +0000666 assert( (pMem->flags & MEM_Null)==0 );
drhd4e70eb2008-01-02 00:34:36 +0000667 if( pMem->flags & MEM_Str ){
drh66a51672008-01-03 00:01:23 +0000668 zP4 = pMem->z;
drhd4e70eb2008-01-02 00:34:36 +0000669 }else if( pMem->flags & MEM_Int ){
670 sqlite3_snprintf(nTemp, zTemp, "%lld", pMem->u.i);
671 }else if( pMem->flags & MEM_Real ){
672 sqlite3_snprintf(nTemp, zTemp, "%.16g", pMem->r);
drhd4e70eb2008-01-02 00:34:36 +0000673 }
drh598f1342007-10-23 15:39:45 +0000674 break;
675 }
drha967e882006-06-13 01:04:52 +0000676#ifndef SQLITE_OMIT_VIRTUALTABLE
drh66a51672008-01-03 00:01:23 +0000677 case P4_VTAB: {
danielk19772dca4ac2008-01-03 11:50:29 +0000678 sqlite3_vtab *pVtab = pOp->p4.pVtab;
drh19146192006-06-26 19:10:32 +0000679 sqlite3_snprintf(nTemp, zTemp, "vtab:%p:%p", pVtab, pVtab->pModule);
drha967e882006-06-13 01:04:52 +0000680 break;
681 }
682#endif
drhd3d39e92004-05-20 22:16:29 +0000683 default: {
danielk19772dca4ac2008-01-03 11:50:29 +0000684 zP4 = pOp->p4.z;
drh949f9cd2008-01-12 21:35:57 +0000685 if( zP4==0 ){
drh66a51672008-01-03 00:01:23 +0000686 zP4 = zTemp;
drhd4e70eb2008-01-02 00:34:36 +0000687 zTemp[0] = 0;
drhd3d39e92004-05-20 22:16:29 +0000688 }
689 }
690 }
drh66a51672008-01-03 00:01:23 +0000691 assert( zP4!=0 );
drh66a51672008-01-03 00:01:23 +0000692 return zP4;
drhd3d39e92004-05-20 22:16:29 +0000693}
drhb7f91642004-10-31 02:22:47 +0000694#endif
drhd3d39e92004-05-20 22:16:29 +0000695
drh900b31e2007-08-28 02:27:51 +0000696/*
drhd0679ed2007-08-28 22:24:34 +0000697** Declare to the Vdbe that the BTree object at db->aDb[i] is used.
698**
drh900b31e2007-08-28 02:27:51 +0000699*/
drhfb982642007-08-30 01:19:59 +0000700void sqlite3VdbeUsesBtree(Vdbe *p, int i){
701 int mask;
drhd0679ed2007-08-28 22:24:34 +0000702 assert( i>=0 && i<p->db->nDb );
703 assert( i<sizeof(p->btreeMask)*8 );
drhfb982642007-08-30 01:19:59 +0000704 mask = 1<<i;
705 if( (p->btreeMask & mask)==0 ){
706 p->btreeMask |= mask;
707 sqlite3BtreeMutexArrayInsert(&p->aMutex, p->db->aDb[i].pBt);
708 }
drh900b31e2007-08-28 02:27:51 +0000709}
710
drhd3d39e92004-05-20 22:16:29 +0000711
danielk19778b60e0f2005-01-12 09:10:39 +0000712#if defined(VDBE_PROFILE) || defined(SQLITE_DEBUG)
drh9a324642003-09-06 20:12:01 +0000713/*
714** Print a single opcode. This routine is used for debugging only.
715*/
danielk19774adee202004-05-08 08:23:19 +0000716void sqlite3VdbePrintOp(FILE *pOut, int pc, Op *pOp){
drh66a51672008-01-03 00:01:23 +0000717 char *zP4;
drhd3d39e92004-05-20 22:16:29 +0000718 char zPtr[50];
drh1db639c2008-01-17 02:36:28 +0000719 static const char *zFormat1 = "%4d %-13s %4d %4d %4d %-4s %.2X %s\n";
drh9a324642003-09-06 20:12:01 +0000720 if( pOut==0 ) pOut = stdout;
drh66a51672008-01-03 00:01:23 +0000721 zP4 = displayP4(pOp, zPtr, sizeof(zPtr));
danielk197711641c12008-01-03 08:18:30 +0000722 fprintf(pOut, zFormat1, pc,
drh1db639c2008-01-17 02:36:28 +0000723 sqlite3OpcodeName(pOp->opcode), pOp->p1, pOp->p2, pOp->p3, zP4, pOp->p5,
724#ifdef SQLITE_DEBUG
725 pOp->zComment ? pOp->zComment : ""
726#else
727 ""
728#endif
729 );
drh9a324642003-09-06 20:12:01 +0000730 fflush(pOut);
731}
732#endif
733
734/*
drh76ff3a02004-09-24 22:32:30 +0000735** Release an array of N Mem elements
736*/
737static void releaseMemArray(Mem *p, int N){
738 if( p ){
739 while( N-->0 ){
drhb21c8cd2007-08-21 19:33:56 +0000740 assert( N<2 || p[0].db==p[1].db );
drh9cbf3422008-01-17 16:22:13 +0000741 sqlite3VdbeMemSetNull(p++);
drh76ff3a02004-09-24 22:32:30 +0000742 }
743 }
744}
745
drhb7f91642004-10-31 02:22:47 +0000746#ifndef SQLITE_OMIT_EXPLAIN
drh76ff3a02004-09-24 22:32:30 +0000747/*
drh9a324642003-09-06 20:12:01 +0000748** Give a listing of the program in the virtual machine.
749**
danielk19774adee202004-05-08 08:23:19 +0000750** The interface is the same as sqlite3VdbeExec(). But instead of
drh9a324642003-09-06 20:12:01 +0000751** running the code, it invokes the callback once for each instruction.
752** This feature is used to implement "EXPLAIN".
drh9cbf3422008-01-17 16:22:13 +0000753**
754** When p->explain==1, each instruction is listed. When
755** p->explain==2, only OP_Explain instructions are listed and these
756** are shown in a different format. p->explain==2 is used to implement
757** EXPLAIN QUERY PLAN.
drh9a324642003-09-06 20:12:01 +0000758*/
danielk19774adee202004-05-08 08:23:19 +0000759int sqlite3VdbeList(
drh9a324642003-09-06 20:12:01 +0000760 Vdbe *p /* The VDBE */
761){
drh9bb575f2004-09-06 17:24:11 +0000762 sqlite3 *db = p->db;
drh9a324642003-09-06 20:12:01 +0000763 int i;
drh826fb5a2004-02-14 23:59:57 +0000764 int rc = SQLITE_OK;
drh9cbf3422008-01-17 16:22:13 +0000765 Mem *pMem = p->pResultSet = &p->aMem[1];
drh9a324642003-09-06 20:12:01 +0000766
drh9a324642003-09-06 20:12:01 +0000767 assert( p->explain );
drhc5cdca62005-01-11 16:54:14 +0000768 if( p->magic!=VDBE_MAGIC_RUN ) return SQLITE_MISUSE;
769 assert( db->magic==SQLITE_MAGIC_BUSY );
770 assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY );
danielk197718f41892004-05-22 07:27:46 +0000771
drh9cbf3422008-01-17 16:22:13 +0000772 /* Even though this opcode does not use dynamic strings for
773 ** the result, result columns may become dynamic if the user calls
drh4f26d6c2004-05-26 23:25:30 +0000774 ** sqlite3_column_text16(), causing a translation to UTF-16 encoding.
danielk197718f41892004-05-22 07:27:46 +0000775 */
drh9cbf3422008-01-17 16:22:13 +0000776 releaseMemArray(pMem, p->nMem);
danielk197718f41892004-05-22 07:27:46 +0000777
drhecc92422005-09-10 16:46:12 +0000778 do{
779 i = p->pc++;
780 }while( i<p->nOp && p->explain==2 && p->aOp[i].opcode!=OP_Explain );
drh826fb5a2004-02-14 23:59:57 +0000781 if( i>=p->nOp ){
782 p->rc = SQLITE_OK;
783 rc = SQLITE_DONE;
drh881feaa2006-07-26 01:39:30 +0000784 }else if( db->u1.isInterrupted ){
drhc5cdca62005-01-11 16:54:14 +0000785 p->rc = SQLITE_INTERRUPT;
drh826fb5a2004-02-14 23:59:57 +0000786 rc = SQLITE_ERROR;
danielk1977f20b21c2004-05-31 23:56:42 +0000787 sqlite3SetString(&p->zErrMsg, sqlite3ErrStr(p->rc), (char*)0);
drh826fb5a2004-02-14 23:59:57 +0000788 }else{
drhd3d39e92004-05-20 22:16:29 +0000789 Op *pOp = &p->aOp[i];
danielk19770d78bae2008-01-03 07:09:48 +0000790 if( p->explain==1 ){
791 pMem->flags = MEM_Int;
792 pMem->type = SQLITE_INTEGER;
793 pMem->u.i = i; /* Program counter */
794 pMem++;
795
796 pMem->flags = MEM_Static|MEM_Str|MEM_Term;
797 pMem->z = (char*)sqlite3OpcodeName(pOp->opcode); /* Opcode */
798 assert( pMem->z!=0 );
799 pMem->n = strlen(pMem->z);
800 pMem->type = SQLITE_TEXT;
801 pMem->enc = SQLITE_UTF8;
802 pMem++;
803 }
drheb2e1762004-05-27 01:53:56 +0000804
805 pMem->flags = MEM_Int;
drh3c024d62007-03-30 11:23:45 +0000806 pMem->u.i = pOp->p1; /* P1 */
drh9c054832004-05-31 18:51:57 +0000807 pMem->type = SQLITE_INTEGER;
drheb2e1762004-05-27 01:53:56 +0000808 pMem++;
809
810 pMem->flags = MEM_Int;
drh3c024d62007-03-30 11:23:45 +0000811 pMem->u.i = pOp->p2; /* P2 */
drh9c054832004-05-31 18:51:57 +0000812 pMem->type = SQLITE_INTEGER;
drheb2e1762004-05-27 01:53:56 +0000813 pMem++;
814
danielk19770d78bae2008-01-03 07:09:48 +0000815 if( p->explain==1 ){
816 pMem->flags = MEM_Int;
817 pMem->u.i = pOp->p3; /* P3 */
818 pMem->type = SQLITE_INTEGER;
819 pMem++;
820 }
821
drh66a51672008-01-03 00:01:23 +0000822 pMem->flags = MEM_Ephem|MEM_Str|MEM_Term; /* P4 */
823 pMem->z = displayP4(pOp, pMem->zShort, sizeof(pMem->zShort));
drhbdd88bd2006-06-15 13:22:22 +0000824 assert( pMem->z!=0 );
drhb8067982006-03-03 21:38:03 +0000825 pMem->n = strlen(pMem->z);
drh9c054832004-05-31 18:51:57 +0000826 pMem->type = SQLITE_TEXT;
danielk1977dc8453f2004-06-12 00:42:34 +0000827 pMem->enc = SQLITE_UTF8;
danielk19770d78bae2008-01-03 07:09:48 +0000828 pMem++;
drheb2e1762004-05-27 01:53:56 +0000829
danielk19770d78bae2008-01-03 07:09:48 +0000830 if( p->explain==1 ){
831 pMem->flags = MEM_Str|MEM_Term|MEM_Short;
832 pMem->n = sprintf(pMem->zShort, "%.2x", pOp->p5); /* P5 */
833 pMem->z = pMem->zShort;
834 pMem->type = SQLITE_TEXT;
835 pMem->enc = SQLITE_UTF8;
836 pMem++;
837
838 pMem->flags = MEM_Null; /* Comment */
drhaa9b8962008-01-08 02:57:55 +0000839#ifdef SQLITE_DEBUG
danielk19770d78bae2008-01-03 07:09:48 +0000840 if( pOp->zComment ){
841 pMem->flags = MEM_Str|MEM_Term;
842 pMem->z = pOp->zComment;
843 pMem->n = strlen(pMem->z);
844 pMem->enc = SQLITE_UTF8;
845 }
drhaa9b8962008-01-08 02:57:55 +0000846#endif
danielk19770d78bae2008-01-03 07:09:48 +0000847 }
848
849 p->nResColumn = 8 - 5*(p->explain-1);
drh826fb5a2004-02-14 23:59:57 +0000850 p->rc = SQLITE_OK;
851 rc = SQLITE_ROW;
drh9a324642003-09-06 20:12:01 +0000852 }
drh826fb5a2004-02-14 23:59:57 +0000853 return rc;
drh9a324642003-09-06 20:12:01 +0000854}
drhb7f91642004-10-31 02:22:47 +0000855#endif /* SQLITE_OMIT_EXPLAIN */
drh9a324642003-09-06 20:12:01 +0000856
drh7c4ac0c2007-04-05 11:25:58 +0000857#ifdef SQLITE_DEBUG
drh9a324642003-09-06 20:12:01 +0000858/*
drh3f7d4e42004-07-24 14:35:58 +0000859** Print the SQL that was used to generate a VDBE program.
860*/
861void sqlite3VdbePrintSql(Vdbe *p){
drh3f7d4e42004-07-24 14:35:58 +0000862 int nOp = p->nOp;
863 VdbeOp *pOp;
drhc16a03b2004-09-15 13:38:10 +0000864 if( nOp<1 ) return;
drh949f9cd2008-01-12 21:35:57 +0000865 pOp = &p->aOp[0];
866 if( pOp->opcode==OP_Trace && pOp->p4.z!=0 ){
danielk19772dca4ac2008-01-03 11:50:29 +0000867 const char *z = pOp->p4.z;
drh4c755c02004-08-08 20:22:17 +0000868 while( isspace(*(u8*)z) ) z++;
drh3f7d4e42004-07-24 14:35:58 +0000869 printf("SQL: [%s]\n", z);
870 }
drh3f7d4e42004-07-24 14:35:58 +0000871}
drh7c4ac0c2007-04-05 11:25:58 +0000872#endif
drh3f7d4e42004-07-24 14:35:58 +0000873
drh602c2372007-03-01 00:29:13 +0000874#if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE)
875/*
876** Print an IOTRACE message showing SQL content.
877*/
878void sqlite3VdbeIOTraceSql(Vdbe *p){
879 int nOp = p->nOp;
880 VdbeOp *pOp;
881 if( sqlite3_io_trace==0 ) return;
882 if( nOp<1 ) return;
drh949f9cd2008-01-12 21:35:57 +0000883 pOp = &p->aOp[0];
884 if( pOp->opcode==OP_Trace && pOp->p4.z!=0 ){
drh602c2372007-03-01 00:29:13 +0000885 int i, j;
drh00a18e42007-08-13 11:10:34 +0000886 char z[1000];
drh949f9cd2008-01-12 21:35:57 +0000887 sqlite3_snprintf(sizeof(z), z, "%s", pOp->p4.z);
danielk19772be2be92007-05-16 17:50:45 +0000888 for(i=0; isspace((unsigned char)z[i]); i++){}
drh602c2372007-03-01 00:29:13 +0000889 for(j=0; z[i]; i++){
danielk19772be2be92007-05-16 17:50:45 +0000890 if( isspace((unsigned char)z[i]) ){
drh602c2372007-03-01 00:29:13 +0000891 if( z[i-1]!=' ' ){
892 z[j++] = ' ';
893 }
894 }else{
895 z[j++] = z[i];
896 }
897 }
898 z[j] = 0;
899 sqlite3_io_trace("SQL %s\n", z);
drh602c2372007-03-01 00:29:13 +0000900 }
901}
902#endif /* !SQLITE_OMIT_TRACE && SQLITE_ENABLE_IOTRACE */
903
904
drh3f7d4e42004-07-24 14:35:58 +0000905/*
drh9a324642003-09-06 20:12:01 +0000906** Prepare a virtual machine for execution. This involves things such
907** as allocating stack space and initializing the program counter.
908** After the VDBE has be prepped, it can be executed by one or more
danielk19774adee202004-05-08 08:23:19 +0000909** calls to sqlite3VdbeExec().
drh92f02c32004-09-02 14:57:08 +0000910**
911** This is the only way to move a VDBE from VDBE_MAGIC_INIT to
912** VDBE_MAGIC_RUN.
drh9a324642003-09-06 20:12:01 +0000913*/
danielk19774adee202004-05-08 08:23:19 +0000914void sqlite3VdbeMakeReady(
drh9a324642003-09-06 20:12:01 +0000915 Vdbe *p, /* The VDBE */
drh7c972de2003-09-06 22:18:07 +0000916 int nVar, /* Number of '?' see in the SQL statement */
drh290c1942004-08-21 17:54:45 +0000917 int nMem, /* Number of memory cells to allocate */
918 int nCursor, /* Number of cursors to allocate */
drh9a324642003-09-06 20:12:01 +0000919 int isExplain /* True if the EXPLAIN keywords is present */
920){
921 int n;
danielk19771e536952007-08-16 10:09:01 +0000922 sqlite3 *db = p->db;
drh9a324642003-09-06 20:12:01 +0000923
924 assert( p!=0 );
drh9a324642003-09-06 20:12:01 +0000925 assert( p->magic==VDBE_MAGIC_INIT );
926
drhc16a03b2004-09-15 13:38:10 +0000927 /* There should be at least one opcode.
drh9a324642003-09-06 20:12:01 +0000928 */
drhc16a03b2004-09-15 13:38:10 +0000929 assert( p->nOp>0 );
drh9a324642003-09-06 20:12:01 +0000930
danielk1977634f2982005-03-28 08:44:07 +0000931 /* Set the magic to VDBE_MAGIC_RUN sooner rather than later. This
932 * is because the call to resizeOpArray() below may shrink the
933 * p->aOp[] array to save memory if called when in VDBE_MAGIC_RUN
934 * state.
935 */
936 p->magic = VDBE_MAGIC_RUN;
937
drh9cbf3422008-01-17 16:22:13 +0000938 /*
939 ** Allocation space for registers.
drh9a324642003-09-06 20:12:01 +0000940 */
drh9cbf3422008-01-17 16:22:13 +0000941 if( p->aMem==0 ){
danielk1977634f2982005-03-28 08:44:07 +0000942 int nArg; /* Maximum number of args passed to a user function. */
drh9cbf3422008-01-17 16:22:13 +0000943 resolveP2Values(p, &nArg);
danielk1977634f2982005-03-28 08:44:07 +0000944 resizeOpArray(p, p->nOp);
drh82a48512003-09-06 22:45:20 +0000945 assert( nVar>=0 );
drh9cbf3422008-01-17 16:22:13 +0000946 if( isExplain && nMem<10 ){
947 p->nMem = nMem = 10;
drh0f7eb612006-08-08 13:51:43 +0000948 }
drh9cbf3422008-01-17 16:22:13 +0000949 p->aMem = sqlite3DbMallocZero(db,
950 nMem*sizeof(Mem) /* aMem */
drh86f43302004-10-05 17:37:36 +0000951 + nVar*sizeof(Mem) /* aVar */
drh9cbf3422008-01-17 16:22:13 +0000952 + nArg*sizeof(Mem*) /* apArg */
drh86f43302004-10-05 17:37:36 +0000953 + nVar*sizeof(char*) /* azVar */
drh0a07c102008-01-03 18:03:08 +0000954 + nCursor*sizeof(Cursor*) + 1 /* apCsr */
drh82a48512003-09-06 22:45:20 +0000955 );
drh17435752007-08-16 04:30:38 +0000956 if( !db->mallocFailed ){
drh9cbf3422008-01-17 16:22:13 +0000957 p->aMem--; /* aMem[] goes from 1..nMem */
958 p->nMem = nMem; /* not from 0..nMem-1 */
drh0a07c102008-01-03 18:03:08 +0000959 p->aVar = &p->aMem[nMem+1];
drh86f43302004-10-05 17:37:36 +0000960 p->nVar = nVar;
961 p->okVar = 0;
962 p->apArg = (Mem**)&p->aVar[nVar];
danielk1977634f2982005-03-28 08:44:07 +0000963 p->azVar = (char**)&p->apArg[nArg];
drh86f43302004-10-05 17:37:36 +0000964 p->apCsr = (Cursor**)&p->azVar[nVar];
drh290c1942004-08-21 17:54:45 +0000965 p->nCursor = nCursor;
966 for(n=0; n<nVar; n++){
967 p->aVar[n].flags = MEM_Null;
drhb21c8cd2007-08-21 19:33:56 +0000968 p->aVar[n].db = db;
969 }
drh9cbf3422008-01-17 16:22:13 +0000970 for(n=1; n<=nMem; n++){
971 p->aMem[n].flags = MEM_Null;
972 p->aMem[n].db = db;
drh290c1942004-08-21 17:54:45 +0000973 }
danielk197754db47e2004-05-19 10:36:43 +0000974 }
drh82a48512003-09-06 22:45:20 +0000975 }
drh9cbf3422008-01-17 16:22:13 +0000976#ifdef SQLITE_DEBUG
977 for(n=1; n<p->nMem; n++){
978 assert( p->aMem[n].db==db );
979 assert( p->aMem[n].flags==MEM_Null );
danielk1977b3bce662005-01-29 08:32:43 +0000980 }
drh9cbf3422008-01-17 16:22:13 +0000981#endif
drh9a324642003-09-06 20:12:01 +0000982
danielk19771d850a72004-05-31 08:26:49 +0000983 p->pc = -1;
drh9a324642003-09-06 20:12:01 +0000984 p->rc = SQLITE_OK;
985 p->uniqueCnt = 0;
986 p->returnDepth = 0;
987 p->errorAction = OE_Abort;
drh9a324642003-09-06 20:12:01 +0000988 p->explain |= isExplain;
989 p->magic = VDBE_MAGIC_RUN;
danielk1977b28af712004-06-21 06:50:26 +0000990 p->nChange = 0;
drh76873ab2006-01-07 18:48:26 +0000991 p->cacheCtr = 1;
drhd946db02005-12-29 19:23:06 +0000992 p->minWriteFileFormat = 255;
danielk1977182c4ba2007-06-27 15:53:34 +0000993 p->openedStatement = 0;
drh9a324642003-09-06 20:12:01 +0000994#ifdef VDBE_PROFILE
drhcf64d8b2003-12-31 17:57:10 +0000995 {
996 int i;
997 for(i=0; i<p->nOp; i++){
998 p->aOp[i].cnt = 0;
999 p->aOp[i].cycles = 0;
1000 }
drh9a324642003-09-06 20:12:01 +00001001 }
1002#endif
1003}
1004
drh9a324642003-09-06 20:12:01 +00001005/*
drhff0587c2007-08-29 17:43:19 +00001006** Close a VDBE cursor and release all the resources that cursor happens
drh9a324642003-09-06 20:12:01 +00001007** to hold.
1008*/
danielk1977be718892006-06-23 08:05:19 +00001009void sqlite3VdbeFreeCursor(Vdbe *p, Cursor *pCx){
drh4774b132004-06-12 20:12:51 +00001010 if( pCx==0 ){
1011 return;
1012 }
drh9a324642003-09-06 20:12:01 +00001013 if( pCx->pCursor ){
danielk19774adee202004-05-08 08:23:19 +00001014 sqlite3BtreeCloseCursor(pCx->pCursor);
drh9a324642003-09-06 20:12:01 +00001015 }
1016 if( pCx->pBt ){
danielk19774adee202004-05-08 08:23:19 +00001017 sqlite3BtreeClose(pCx->pBt);
drh9a324642003-09-06 20:12:01 +00001018 }
drh9eff6162006-06-12 21:59:13 +00001019#ifndef SQLITE_OMIT_VIRTUALTABLE
1020 if( pCx->pVtabCursor ){
1021 sqlite3_vtab_cursor *pVtabCursor = pCx->pVtabCursor;
danielk1977be718892006-06-23 08:05:19 +00001022 const sqlite3_module *pModule = pCx->pModule;
1023 p->inVtabMethod = 1;
drh7e8b8482008-01-23 03:03:05 +00001024 (void)sqlite3SafetyOff(p->db);
drh9eff6162006-06-12 21:59:13 +00001025 pModule->xClose(pVtabCursor);
drh7e8b8482008-01-23 03:03:05 +00001026 (void)sqlite3SafetyOn(p->db);
danielk1977be718892006-06-23 08:05:19 +00001027 p->inVtabMethod = 0;
drh9eff6162006-06-12 21:59:13 +00001028 }
1029#endif
drh17435752007-08-16 04:30:38 +00001030 sqlite3_free(pCx->pData);
1031 sqlite3_free(pCx->aType);
1032 sqlite3_free(pCx);
drh9a324642003-09-06 20:12:01 +00001033}
1034
1035/*
drhff0587c2007-08-29 17:43:19 +00001036** Close all cursors except for VTab cursors that are currently
1037** in use.
drh9a324642003-09-06 20:12:01 +00001038*/
drhff0587c2007-08-29 17:43:19 +00001039static void closeAllCursorsExceptActiveVtabs(Vdbe *p){
drh9a324642003-09-06 20:12:01 +00001040 int i;
drh290c1942004-08-21 17:54:45 +00001041 if( p->apCsr==0 ) return;
drh9a324642003-09-06 20:12:01 +00001042 for(i=0; i<p->nCursor; i++){
drhff0587c2007-08-29 17:43:19 +00001043 Cursor *pC = p->apCsr[i];
1044 if( pC && (!p->inVtabMethod || !pC->pVtabCursor) ){
1045 sqlite3VdbeFreeCursor(p, pC);
danielk1977b7a2f2e2006-06-23 11:34:54 +00001046 p->apCsr[i] = 0;
danielk1977be718892006-06-23 08:05:19 +00001047 }
drh9a324642003-09-06 20:12:01 +00001048 }
drh9a324642003-09-06 20:12:01 +00001049}
1050
1051/*
drh9a324642003-09-06 20:12:01 +00001052** Clean up the VM after execution.
1053**
1054** This routine will automatically close any cursors, lists, and/or
1055** sorters that were left open. It also deletes the values of
drh5a12e682004-05-19 11:24:25 +00001056** variables in the aVar[] array.
drh9a324642003-09-06 20:12:01 +00001057*/
1058static void Cleanup(Vdbe *p){
1059 int i;
drhff0587c2007-08-29 17:43:19 +00001060 closeAllCursorsExceptActiveVtabs(p);
drh0a07c102008-01-03 18:03:08 +00001061 releaseMemArray(&p->aMem[1], p->nMem);
drha01f79d2005-07-08 13:07:59 +00001062 sqlite3VdbeFifoClear(&p->sFifo);
drh76ff3a02004-09-24 22:32:30 +00001063 if( p->contextStack ){
1064 for(i=0; i<p->contextStackTop; i++){
drha01f79d2005-07-08 13:07:59 +00001065 sqlite3VdbeFifoClear(&p->contextStack[i].sFifo);
drh76ff3a02004-09-24 22:32:30 +00001066 }
drh17435752007-08-16 04:30:38 +00001067 sqlite3_free(p->contextStack);
drh344737f2004-09-19 00:50:20 +00001068 }
drh5f968432004-02-21 19:02:30 +00001069 p->contextStack = 0;
drh344737f2004-09-19 00:50:20 +00001070 p->contextStackDepth = 0;
1071 p->contextStackTop = 0;
drh17435752007-08-16 04:30:38 +00001072 sqlite3_free(p->zErrMsg);
drh9a324642003-09-06 20:12:01 +00001073 p->zErrMsg = 0;
drhd4e70eb2008-01-02 00:34:36 +00001074 p->pResultSet = 0;
drh9a324642003-09-06 20:12:01 +00001075}
1076
1077/*
danielk197722322fd2004-05-25 23:35:17 +00001078** Set the number of result columns that will be returned by this SQL
1079** statement. This is now set at compile time, rather than during
1080** execution of the vdbe program so that sqlite3_column_count() can
1081** be called on an SQL statement before sqlite3_step().
1082*/
1083void sqlite3VdbeSetNumCols(Vdbe *p, int nResColumn){
drh76ff3a02004-09-24 22:32:30 +00001084 Mem *pColName;
1085 int n;
drh4a50aac2007-08-23 02:47:53 +00001086
danielk1977955de522006-02-10 02:27:42 +00001087 releaseMemArray(p->aColName, p->nResColumn*COLNAME_N);
drh17435752007-08-16 04:30:38 +00001088 sqlite3_free(p->aColName);
danielk1977955de522006-02-10 02:27:42 +00001089 n = nResColumn*COLNAME_N;
drhcc43cab2005-10-05 11:35:09 +00001090 p->nResColumn = nResColumn;
danielk19771e536952007-08-16 10:09:01 +00001091 p->aColName = pColName = (Mem*)sqlite3DbMallocZero(p->db, sizeof(Mem)*n );
drh76ff3a02004-09-24 22:32:30 +00001092 if( p->aColName==0 ) return;
1093 while( n-- > 0 ){
drh4a50aac2007-08-23 02:47:53 +00001094 pColName->flags = MEM_Null;
drh153c62c2007-08-24 03:51:33 +00001095 pColName->db = p->db;
drh4a50aac2007-08-23 02:47:53 +00001096 pColName++;
drh76ff3a02004-09-24 22:32:30 +00001097 }
danielk197722322fd2004-05-25 23:35:17 +00001098}
1099
1100/*
danielk19773cf86062004-05-26 10:11:05 +00001101** Set the name of the idx'th column to be returned by the SQL statement.
1102** zName must be a pointer to a nul terminated string.
1103**
1104** This call must be made after a call to sqlite3VdbeSetNumCols().
1105**
drh66a51672008-01-03 00:01:23 +00001106** If N==P4_STATIC it means that zName is a pointer to a constant static
1107** string and we can just copy the pointer. If it is P4_DYNAMIC, then
drh17435752007-08-16 04:30:38 +00001108** the string is freed using sqlite3_free() when the vdbe is finished with
danielk1977d8123362004-06-12 09:25:12 +00001109** it. Otherwise, N bytes of zName are copied.
danielk19773cf86062004-05-26 10:11:05 +00001110*/
danielk1977955de522006-02-10 02:27:42 +00001111int sqlite3VdbeSetColName(Vdbe *p, int idx, int var, const char *zName, int N){
danielk19773cf86062004-05-26 10:11:05 +00001112 int rc;
1113 Mem *pColName;
danielk1977955de522006-02-10 02:27:42 +00001114 assert( idx<p->nResColumn );
1115 assert( var<COLNAME_N );
drh17435752007-08-16 04:30:38 +00001116 if( p->db->mallocFailed ) return SQLITE_NOMEM;
drh76ff3a02004-09-24 22:32:30 +00001117 assert( p->aColName!=0 );
danielk1977955de522006-02-10 02:27:42 +00001118 pColName = &(p->aColName[idx+var*p->nResColumn]);
drh66a51672008-01-03 00:01:23 +00001119 if( N==P4_DYNAMIC || N==P4_STATIC ){
drhb21c8cd2007-08-21 19:33:56 +00001120 rc = sqlite3VdbeMemSetStr(pColName, zName, -1, SQLITE_UTF8, SQLITE_STATIC);
danielk19773cf86062004-05-26 10:11:05 +00001121 }else{
drhb21c8cd2007-08-21 19:33:56 +00001122 rc = sqlite3VdbeMemSetStr(pColName, zName, N, SQLITE_UTF8,SQLITE_TRANSIENT);
danielk19773cf86062004-05-26 10:11:05 +00001123 }
drh66a51672008-01-03 00:01:23 +00001124 if( rc==SQLITE_OK && N==P4_DYNAMIC ){
danielk19773cf86062004-05-26 10:11:05 +00001125 pColName->flags = (pColName->flags&(~MEM_Static))|MEM_Dyn;
danielk1977d8123362004-06-12 09:25:12 +00001126 pColName->xDel = 0;
danielk19773cf86062004-05-26 10:11:05 +00001127 }
1128 return rc;
1129}
1130
danielk197713adf8a2004-06-03 16:08:41 +00001131/*
1132** A read or write transaction may or may not be active on database handle
1133** db. If a transaction is active, commit it. If there is a
1134** write-transaction spanning more than one database file, this routine
1135** takes care of the master journal trickery.
1136*/
drh9bb575f2004-09-06 17:24:11 +00001137static int vdbeCommit(sqlite3 *db){
danielk197713adf8a2004-06-03 16:08:41 +00001138 int i;
1139 int nTrans = 0; /* Number of databases with an active write-transaction */
1140 int rc = SQLITE_OK;
1141 int needXcommit = 0;
1142
danielk19775bd270b2006-07-25 15:14:52 +00001143 /* Before doing anything else, call the xSync() callback for any
1144 ** virtual module tables written in this transaction. This has to
1145 ** be done before determining whether a master journal file is
1146 ** required, as an xSync() callback may add an attached database
1147 ** to the transaction.
1148 */
1149 rc = sqlite3VtabSync(db, rc);
1150 if( rc!=SQLITE_OK ){
1151 return rc;
1152 }
1153
1154 /* This loop determines (a) if the commit hook should be invoked and
1155 ** (b) how many database files have open write transactions, not
1156 ** including the temp database. (b) is important because if more than
1157 ** one database file has an open write transaction, a master journal
1158 ** file is required for an atomic commit.
1159 */
danielk197713adf8a2004-06-03 16:08:41 +00001160 for(i=0; i<db->nDb; i++){
1161 Btree *pBt = db->aDb[i].pBt;
drhd0679ed2007-08-28 22:24:34 +00001162 if( sqlite3BtreeIsInTrans(pBt) ){
danielk197713adf8a2004-06-03 16:08:41 +00001163 needXcommit = 1;
1164 if( i!=1 ) nTrans++;
1165 }
1166 }
1167
1168 /* If there are any write-transactions at all, invoke the commit hook */
1169 if( needXcommit && db->xCommitCallback ){
drh7e8b8482008-01-23 03:03:05 +00001170 (void)sqlite3SafetyOff(db);
drh92f02c32004-09-02 14:57:08 +00001171 rc = db->xCommitCallback(db->pCommitArg);
drh7e8b8482008-01-23 03:03:05 +00001172 (void)sqlite3SafetyOn(db);
drh92f02c32004-09-02 14:57:08 +00001173 if( rc ){
danielk197713adf8a2004-06-03 16:08:41 +00001174 return SQLITE_CONSTRAINT;
1175 }
1176 }
1177
danielk197740b38dc2004-06-26 08:38:24 +00001178 /* The simple case - no more than one database file (not counting the
1179 ** TEMP database) has a transaction active. There is no need for the
drh2ac3ee92004-06-07 16:27:46 +00001180 ** master-journal.
drhc9e06862004-06-09 20:03:08 +00001181 **
danielk197740b38dc2004-06-26 08:38:24 +00001182 ** If the return value of sqlite3BtreeGetFilename() is a zero length
1183 ** string, it means the main database is :memory:. In that case we do
1184 ** not support atomic multi-file commits, so use the simple case then
drhc9e06862004-06-09 20:03:08 +00001185 ** too.
danielk197713adf8a2004-06-03 16:08:41 +00001186 */
danielk197740b38dc2004-06-26 08:38:24 +00001187 if( 0==strlen(sqlite3BtreeGetFilename(db->aDb[0].pBt)) || nTrans<=1 ){
drh2ac3ee92004-06-07 16:27:46 +00001188 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
danielk197713adf8a2004-06-03 16:08:41 +00001189 Btree *pBt = db->aDb[i].pBt;
1190 if( pBt ){
drh80e35f42007-03-30 14:06:34 +00001191 rc = sqlite3BtreeCommitPhaseOne(pBt, 0);
drh2ac3ee92004-06-07 16:27:46 +00001192 }
1193 }
1194
drh80e35f42007-03-30 14:06:34 +00001195 /* Do the commit only if all databases successfully complete phase 1.
1196 ** If one of the BtreeCommitPhaseOne() calls fails, this indicates an
1197 ** IO error while deleting or truncating a journal file. It is unlikely,
1198 ** but could happen. In this case abandon processing and return the error.
danielk1977979f38e2007-03-27 16:19:51 +00001199 */
1200 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
1201 Btree *pBt = db->aDb[i].pBt;
1202 if( pBt ){
drh80e35f42007-03-30 14:06:34 +00001203 rc = sqlite3BtreeCommitPhaseTwo(pBt);
danielk197713adf8a2004-06-03 16:08:41 +00001204 }
danielk1977979f38e2007-03-27 16:19:51 +00001205 }
1206 if( rc==SQLITE_OK ){
danielk1977f9e7dda2006-06-16 16:08:53 +00001207 sqlite3VtabCommit(db);
danielk197713adf8a2004-06-03 16:08:41 +00001208 }
1209 }
1210
1211 /* The complex case - There is a multi-file write-transaction active.
1212 ** This requires a master journal file to ensure the transaction is
1213 ** committed atomicly.
1214 */
danielk197744ee5bf2005-05-27 09:41:12 +00001215#ifndef SQLITE_OMIT_DISKIO
danielk197713adf8a2004-06-03 16:08:41 +00001216 else{
danielk1977b4b47412007-08-17 15:53:36 +00001217 sqlite3_vfs *pVfs = db->pVfs;
drh2c8997b2005-08-27 16:36:48 +00001218 int needSync = 0;
danielk197713adf8a2004-06-03 16:08:41 +00001219 char *zMaster = 0; /* File-name for the master journal */
1220 char const *zMainFile = sqlite3BtreeGetFilename(db->aDb[0].pBt);
danielk1977b4b47412007-08-17 15:53:36 +00001221 sqlite3_file *pMaster = 0;
danielk197762079062007-08-15 17:08:46 +00001222 i64 offset = 0;
danielk197713adf8a2004-06-03 16:08:41 +00001223
1224 /* Select a master journal file name */
1225 do {
drha6abd042004-06-09 17:37:22 +00001226 u32 random;
drh17435752007-08-16 04:30:38 +00001227 sqlite3_free(zMaster);
danielk197713adf8a2004-06-03 16:08:41 +00001228 sqlite3Randomness(sizeof(random), &random);
danielk19771e536952007-08-16 10:09:01 +00001229 zMaster = sqlite3MPrintf(db, "%s-mj%08X", zMainFile, random&0x7fffffff);
danielk197713adf8a2004-06-03 16:08:41 +00001230 if( !zMaster ){
1231 return SQLITE_NOMEM;
1232 }
danielk1977b4b47412007-08-17 15:53:36 +00001233 }while( sqlite3OsAccess(pVfs, zMaster, SQLITE_ACCESS_EXISTS) );
danielk197713adf8a2004-06-03 16:08:41 +00001234
1235 /* Open the master journal. */
danielk1977fee2d252007-08-18 10:59:19 +00001236 rc = sqlite3OsOpenMalloc(pVfs, zMaster, &pMaster,
1237 SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|
danielk1977967a4a12007-08-20 14:23:44 +00001238 SQLITE_OPEN_EXCLUSIVE|SQLITE_OPEN_MASTER_JOURNAL, 0
danielk1977fee2d252007-08-18 10:59:19 +00001239 );
danielk197713adf8a2004-06-03 16:08:41 +00001240 if( rc!=SQLITE_OK ){
drh17435752007-08-16 04:30:38 +00001241 sqlite3_free(zMaster);
danielk197713adf8a2004-06-03 16:08:41 +00001242 return rc;
1243 }
1244
1245 /* Write the name of each database file in the transaction into the new
1246 ** master journal file. If an error occurs at this point close
1247 ** and delete the master journal file. All the individual journal files
1248 ** still have 'null' as the master journal pointer, so they will roll
danielk1977aca790a2005-01-13 11:07:52 +00001249 ** back independently if a failure occurs.
danielk197713adf8a2004-06-03 16:08:41 +00001250 */
danielk19771e536952007-08-16 10:09:01 +00001251 for(i=0; i<db->nDb; i++){
danielk197713adf8a2004-06-03 16:08:41 +00001252 Btree *pBt = db->aDb[i].pBt;
drhc9e06862004-06-09 20:03:08 +00001253 if( i==1 ) continue; /* Ignore the TEMP database */
drhd0679ed2007-08-28 22:24:34 +00001254 if( sqlite3BtreeIsInTrans(pBt) ){
danielk19775865e3d2004-06-14 06:03:57 +00001255 char const *zFile = sqlite3BtreeGetJournalname(pBt);
drhc9e06862004-06-09 20:03:08 +00001256 if( zFile[0]==0 ) continue; /* Ignore :memory: databases */
drh2c8997b2005-08-27 16:36:48 +00001257 if( !needSync && !sqlite3BtreeSyncDisabled(pBt) ){
1258 needSync = 1;
1259 }
danielk1977b4b47412007-08-17 15:53:36 +00001260 rc = sqlite3OsWrite(pMaster, zFile, strlen(zFile)+1, offset);
danielk197762079062007-08-15 17:08:46 +00001261 offset += strlen(zFile)+1;
danielk197713adf8a2004-06-03 16:08:41 +00001262 if( rc!=SQLITE_OK ){
danielk1977fee2d252007-08-18 10:59:19 +00001263 sqlite3OsCloseFree(pMaster);
1264 sqlite3OsDelete(pVfs, zMaster, 0);
drh17435752007-08-16 04:30:38 +00001265 sqlite3_free(zMaster);
danielk197713adf8a2004-06-03 16:08:41 +00001266 return rc;
1267 }
1268 }
1269 }
1270
danielk19779663b8f2007-08-24 11:52:28 +00001271 /* Sync the master journal file. If the IOCAP_SEQUENTIAL device
1272 ** flag is set this is not required.
1273 */
danielk19775865e3d2004-06-14 06:03:57 +00001274 zMainFile = sqlite3BtreeGetDirname(db->aDb[0].pBt);
danielk1977f036aef2007-08-20 05:36:51 +00001275 if( (needSync
danielk19779663b8f2007-08-24 11:52:28 +00001276 && (0==(sqlite3OsDeviceCharacteristics(pMaster)&SQLITE_IOCAP_SEQUENTIAL))
danielk1977f036aef2007-08-20 05:36:51 +00001277 && (rc=sqlite3OsSync(pMaster, SQLITE_SYNC_NORMAL))!=SQLITE_OK) ){
danielk1977fee2d252007-08-18 10:59:19 +00001278 sqlite3OsCloseFree(pMaster);
1279 sqlite3OsDelete(pVfs, zMaster, 0);
drh17435752007-08-16 04:30:38 +00001280 sqlite3_free(zMaster);
danielk19775865e3d2004-06-14 06:03:57 +00001281 return rc;
1282 }
drhc9e06862004-06-09 20:03:08 +00001283
danielk197713adf8a2004-06-03 16:08:41 +00001284 /* Sync all the db files involved in the transaction. The same call
1285 ** sets the master journal pointer in each individual journal. If
1286 ** an error occurs here, do not delete the master journal file.
1287 **
drh80e35f42007-03-30 14:06:34 +00001288 ** If the error occurs during the first call to
1289 ** sqlite3BtreeCommitPhaseOne(), then there is a chance that the
1290 ** master journal file will be orphaned. But we cannot delete it,
1291 ** in case the master journal file name was written into the journal
1292 ** file before the failure occured.
danielk197713adf8a2004-06-03 16:08:41 +00001293 */
danielk19775bd270b2006-07-25 15:14:52 +00001294 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
danielk197713adf8a2004-06-03 16:08:41 +00001295 Btree *pBt = db->aDb[i].pBt;
drhd0679ed2007-08-28 22:24:34 +00001296 if( pBt ){
drh80e35f42007-03-30 14:06:34 +00001297 rc = sqlite3BtreeCommitPhaseOne(pBt, zMaster);
danielk197713adf8a2004-06-03 16:08:41 +00001298 }
1299 }
danielk1977fee2d252007-08-18 10:59:19 +00001300 sqlite3OsCloseFree(pMaster);
danielk19775bd270b2006-07-25 15:14:52 +00001301 if( rc!=SQLITE_OK ){
drh17435752007-08-16 04:30:38 +00001302 sqlite3_free(zMaster);
danielk19775bd270b2006-07-25 15:14:52 +00001303 return rc;
1304 }
danielk197713adf8a2004-06-03 16:08:41 +00001305
danielk1977962398d2004-06-14 09:35:16 +00001306 /* Delete the master journal file. This commits the transaction. After
1307 ** doing this the directory is synced again before any individual
1308 ** transaction files are deleted.
1309 */
danielk1977fee2d252007-08-18 10:59:19 +00001310 rc = sqlite3OsDelete(pVfs, zMaster, 1);
drh17435752007-08-16 04:30:38 +00001311 sqlite3_free(zMaster);
drhc416ba92007-03-30 18:42:55 +00001312 zMaster = 0;
drh29a01382006-08-13 19:04:18 +00001313 if( rc ){
1314 return rc;
1315 }
danielk197713adf8a2004-06-03 16:08:41 +00001316
1317 /* All files and directories have already been synced, so the following
drh80e35f42007-03-30 14:06:34 +00001318 ** calls to sqlite3BtreeCommitPhaseTwo() are only closing files and
1319 ** deleting or truncating journals. If something goes wrong while
1320 ** this is happening we don't really care. The integrity of the
1321 ** transaction is already guaranteed, but some stray 'cold' journals
1322 ** may be lying around. Returning an error code won't help matters.
danielk197713adf8a2004-06-03 16:08:41 +00001323 */
danielk1977979f38e2007-03-27 16:19:51 +00001324 disable_simulated_io_errors();
danielk197713adf8a2004-06-03 16:08:41 +00001325 for(i=0; i<db->nDb; i++){
1326 Btree *pBt = db->aDb[i].pBt;
1327 if( pBt ){
drh80e35f42007-03-30 14:06:34 +00001328 sqlite3BtreeCommitPhaseTwo(pBt);
danielk197713adf8a2004-06-03 16:08:41 +00001329 }
1330 }
danielk1977979f38e2007-03-27 16:19:51 +00001331 enable_simulated_io_errors();
1332
danielk1977f9e7dda2006-06-16 16:08:53 +00001333 sqlite3VtabCommit(db);
danielk197713adf8a2004-06-03 16:08:41 +00001334 }
danielk197744ee5bf2005-05-27 09:41:12 +00001335#endif
danielk1977026d2702004-06-14 13:14:59 +00001336
drh2ac3ee92004-06-07 16:27:46 +00001337 return rc;
danielk197713adf8a2004-06-03 16:08:41 +00001338}
1339
danielk19771d850a72004-05-31 08:26:49 +00001340/*
1341** This routine checks that the sqlite3.activeVdbeCnt count variable
1342** matches the number of vdbe's in the list sqlite3.pVdbe that are
1343** currently active. An assertion fails if the two counts do not match.
drh92f02c32004-09-02 14:57:08 +00001344** This is an internal self-check only - it is not an essential processing
1345** step.
danielk19771d850a72004-05-31 08:26:49 +00001346**
1347** This is a no-op if NDEBUG is defined.
1348*/
1349#ifndef NDEBUG
drh9bb575f2004-09-06 17:24:11 +00001350static void checkActiveVdbeCnt(sqlite3 *db){
danielk19771d850a72004-05-31 08:26:49 +00001351 Vdbe *p;
1352 int cnt = 0;
danielk19771d850a72004-05-31 08:26:49 +00001353 p = db->pVdbe;
1354 while( p ){
drh92f02c32004-09-02 14:57:08 +00001355 if( p->magic==VDBE_MAGIC_RUN && p->pc>=0 ){
danielk19771d850a72004-05-31 08:26:49 +00001356 cnt++;
1357 }
1358 p = p->pNext;
1359 }
danielk19771d850a72004-05-31 08:26:49 +00001360 assert( cnt==db->activeVdbeCnt );
1361}
1362#else
1363#define checkActiveVdbeCnt(x)
1364#endif
1365
danielk19773cf86062004-05-26 10:11:05 +00001366/*
drhfb982642007-08-30 01:19:59 +00001367** For every Btree that in database connection db which
1368** has been modified, "trip" or invalidate each cursor in
1369** that Btree might have been modified so that the cursor
1370** can never be used again. This happens when a rollback
1371*** occurs. We have to trip all the other cursors, even
1372** cursor from other VMs in different database connections,
1373** so that none of them try to use the data at which they
1374** were pointing and which now may have been changed due
1375** to the rollback.
1376**
1377** Remember that a rollback can delete tables complete and
1378** reorder rootpages. So it is not sufficient just to save
1379** the state of the cursor. We have to invalidate the cursor
1380** so that it is never used again.
danielk1977be718892006-06-23 08:05:19 +00001381*/
drhade6c9c2007-11-24 10:23:44 +00001382static void invalidateCursorsOnModifiedBtrees(sqlite3 *db){
drhfb982642007-08-30 01:19:59 +00001383 int i;
1384 for(i=0; i<db->nDb; i++){
1385 Btree *p = db->aDb[i].pBt;
1386 if( p && sqlite3BtreeIsInTrans(p) ){
1387 sqlite3BtreeTripAllCursors(p, SQLITE_ABORT);
1388 }
danielk1977be718892006-06-23 08:05:19 +00001389 }
1390}
1391
1392/*
drh92f02c32004-09-02 14:57:08 +00001393** This routine is called the when a VDBE tries to halt. If the VDBE
1394** has made changes and is in autocommit mode, then commit those
1395** changes. If a rollback is needed, then do the rollback.
drh9a324642003-09-06 20:12:01 +00001396**
drh92f02c32004-09-02 14:57:08 +00001397** This routine is the only way to move the state of a VM from
drhff0587c2007-08-29 17:43:19 +00001398** SQLITE_MAGIC_RUN to SQLITE_MAGIC_HALT. It is harmless to
1399** call this on a VM that is in the SQLITE_MAGIC_HALT state.
drh92f02c32004-09-02 14:57:08 +00001400**
1401** Return an error code. If the commit could not complete because of
1402** lock contention, return SQLITE_BUSY. If SQLITE_BUSY is returned, it
1403** means the close did not happen and needs to be repeated.
drh9a324642003-09-06 20:12:01 +00001404*/
drhff0587c2007-08-29 17:43:19 +00001405int sqlite3VdbeHalt(Vdbe *p){
drh9bb575f2004-09-06 17:24:11 +00001406 sqlite3 *db = p->db;
drh9a324642003-09-06 20:12:01 +00001407 int i;
danielk19771d850a72004-05-31 08:26:49 +00001408 int (*xFunc)(Btree *pBt) = 0; /* Function to call on each btree backend */
danielk197707cb5602006-01-20 10:55:05 +00001409 int isSpecialError; /* Set to true if SQLITE_NOMEM or IOERR */
1410
1411 /* This function contains the logic that determines if a statement or
1412 ** transaction will be committed or rolled back as a result of the
1413 ** execution of this virtual machine.
1414 **
drh71b890a2007-10-03 15:30:52 +00001415 ** If any of the following errors occur:
danielk197707cb5602006-01-20 10:55:05 +00001416 **
drh71b890a2007-10-03 15:30:52 +00001417 ** SQLITE_NOMEM
1418 ** SQLITE_IOERR
1419 ** SQLITE_FULL
1420 ** SQLITE_INTERRUPT
danielk197707cb5602006-01-20 10:55:05 +00001421 **
drh71b890a2007-10-03 15:30:52 +00001422 ** Then the internal cache might have been left in an inconsistent
1423 ** state. We need to rollback the statement transaction, if there is
1424 ** one, or the complete transaction if there is no statement transaction.
danielk197707cb5602006-01-20 10:55:05 +00001425 */
drh9a324642003-09-06 20:12:01 +00001426
drh17435752007-08-16 04:30:38 +00001427 if( p->db->mallocFailed ){
danielk1977261919c2005-12-06 12:52:59 +00001428 p->rc = SQLITE_NOMEM;
1429 }
drhff0587c2007-08-29 17:43:19 +00001430 closeAllCursorsExceptActiveVtabs(p);
drh92f02c32004-09-02 14:57:08 +00001431 if( p->magic!=VDBE_MAGIC_RUN ){
drh92f02c32004-09-02 14:57:08 +00001432 return SQLITE_OK;
drh9a324642003-09-06 20:12:01 +00001433 }
danielk19771d850a72004-05-31 08:26:49 +00001434 checkActiveVdbeCnt(db);
danielk1977261919c2005-12-06 12:52:59 +00001435
danielk197707cb5602006-01-20 10:55:05 +00001436 /* No commit or rollback needed if the program never started */
1437 if( p->pc>=0 ){
drhaac2f552006-09-23 21:44:23 +00001438 int mrc; /* Primary error code from p->rc */
drhff0587c2007-08-29 17:43:19 +00001439
1440 /* Lock all btrees used by the statement */
1441 sqlite3BtreeMutexArrayEnter(&p->aMutex);
1442
drh71b890a2007-10-03 15:30:52 +00001443 /* Check for one of the special errors */
drhaac2f552006-09-23 21:44:23 +00001444 mrc = p->rc & 0xff;
drh71b890a2007-10-03 15:30:52 +00001445 isSpecialError = mrc==SQLITE_NOMEM || mrc==SQLITE_IOERR
drh77658e22007-12-04 16:54:52 +00001446 || mrc==SQLITE_INTERRUPT || mrc==SQLITE_FULL;
danielk197707cb5602006-01-20 10:55:05 +00001447 if( isSpecialError ){
danielk1977261919c2005-12-06 12:52:59 +00001448 /* This loop does static analysis of the query to see which of the
1449 ** following three categories it falls into:
1450 **
1451 ** Read-only
danielk197707cb5602006-01-20 10:55:05 +00001452 ** Query with statement journal
1453 ** Query without statement journal
danielk1977261919c2005-12-06 12:52:59 +00001454 **
1455 ** We could do something more elegant than this static analysis (i.e.
1456 ** store the type of query as part of the compliation phase), but
danielk197707cb5602006-01-20 10:55:05 +00001457 ** handling malloc() or IO failure is a fairly obscure edge case so
1458 ** this is probably easier. Todo: Might be an opportunity to reduce
1459 ** code size a very small amount though...
danielk1977261919c2005-12-06 12:52:59 +00001460 */
drhd1817042007-10-03 18:45:04 +00001461 int notReadOnly = 0;
danielk1977261919c2005-12-06 12:52:59 +00001462 int isStatement = 0;
1463 assert(p->aOp || p->nOp==0);
1464 for(i=0; i<p->nOp; i++){
1465 switch( p->aOp[i].opcode ){
1466 case OP_Transaction:
drhd1817042007-10-03 18:45:04 +00001467 notReadOnly |= p->aOp[i].p2;
danielk1977261919c2005-12-06 12:52:59 +00001468 break;
1469 case OP_Statement:
1470 isStatement = 1;
1471 break;
1472 }
1473 }
drhff0587c2007-08-29 17:43:19 +00001474
1475
danielk197707cb5602006-01-20 10:55:05 +00001476 /* If the query was read-only, we need do no rollback at all. Otherwise,
1477 ** proceed with the special handling.
1478 */
drhd1817042007-10-03 18:45:04 +00001479 if( notReadOnly || mrc!=SQLITE_INTERRUPT ){
danielk1977e965ac72007-06-13 15:22:28 +00001480 if( p->rc==SQLITE_IOERR_BLOCKED && isStatement ){
1481 xFunc = sqlite3BtreeRollbackStmt;
1482 p->rc = SQLITE_BUSY;
drhd1817042007-10-03 18:45:04 +00001483 } else if( (mrc==SQLITE_NOMEM || mrc==SQLITE_FULL) && isStatement ){
danielk197707cb5602006-01-20 10:55:05 +00001484 xFunc = sqlite3BtreeRollbackStmt;
1485 }else{
1486 /* We are forced to roll back the active transaction. Before doing
1487 ** so, abort any other statements this handle currently has active.
1488 */
drhfb982642007-08-30 01:19:59 +00001489 invalidateCursorsOnModifiedBtrees(db);
danielk197797a227c2006-01-20 16:32:04 +00001490 sqlite3RollbackAll(db);
danielk197707cb5602006-01-20 10:55:05 +00001491 db->autoCommit = 1;
1492 }
danielk1977261919c2005-12-06 12:52:59 +00001493 }
1494 }
danielk197707cb5602006-01-20 10:55:05 +00001495
1496 /* If the auto-commit flag is set and this is the only active vdbe, then
1497 ** we do either a commit or rollback of the current transaction.
1498 **
1499 ** Note: This block also runs if one of the special errors handled
1500 ** above has occured.
1501 */
1502 if( db->autoCommit && db->activeVdbeCnt==1 ){
1503 if( p->rc==SQLITE_OK || (p->errorAction==OE_Fail && !isSpecialError) ){
drhfd131da2007-08-07 17:13:03 +00001504 /* The auto-commit flag is true, and the vdbe program was
danielk197707cb5602006-01-20 10:55:05 +00001505 ** successful or hit an 'OR FAIL' constraint. This means a commit
1506 ** is required.
1507 */
1508 int rc = vdbeCommit(db);
1509 if( rc==SQLITE_BUSY ){
drhff0587c2007-08-29 17:43:19 +00001510 sqlite3BtreeMutexArrayLeave(&p->aMutex);
danielk197707cb5602006-01-20 10:55:05 +00001511 return SQLITE_BUSY;
1512 }else if( rc!=SQLITE_OK ){
1513 p->rc = rc;
danielk197797a227c2006-01-20 16:32:04 +00001514 sqlite3RollbackAll(db);
danielk197707cb5602006-01-20 10:55:05 +00001515 }else{
1516 sqlite3CommitInternalChanges(db);
1517 }
1518 }else{
danielk197797a227c2006-01-20 16:32:04 +00001519 sqlite3RollbackAll(db);
danielk197707cb5602006-01-20 10:55:05 +00001520 }
1521 }else if( !xFunc ){
1522 if( p->rc==SQLITE_OK || p->errorAction==OE_Fail ){
danielk1977182c4ba2007-06-27 15:53:34 +00001523 if( p->openedStatement ){
1524 xFunc = sqlite3BtreeCommitStmt;
1525 }
danielk197707cb5602006-01-20 10:55:05 +00001526 }else if( p->errorAction==OE_Abort ){
1527 xFunc = sqlite3BtreeRollbackStmt;
1528 }else{
drhfb982642007-08-30 01:19:59 +00001529 invalidateCursorsOnModifiedBtrees(db);
danielk197797a227c2006-01-20 16:32:04 +00001530 sqlite3RollbackAll(db);
danielk197707cb5602006-01-20 10:55:05 +00001531 db->autoCommit = 1;
1532 }
danielk19771d850a72004-05-31 08:26:49 +00001533 }
danielk197707cb5602006-01-20 10:55:05 +00001534
1535 /* If xFunc is not NULL, then it is one of sqlite3BtreeRollbackStmt or
1536 ** sqlite3BtreeCommitStmt. Call it once on each backend. If an error occurs
1537 ** and the return code is still SQLITE_OK, set the return code to the new
1538 ** error value.
1539 */
1540 assert(!xFunc ||
1541 xFunc==sqlite3BtreeCommitStmt ||
1542 xFunc==sqlite3BtreeRollbackStmt
1543 );
1544 for(i=0; xFunc && i<db->nDb; i++){
1545 int rc;
1546 Btree *pBt = db->aDb[i].pBt;
1547 if( pBt ){
1548 rc = xFunc(pBt);
danielk19778a7aea32006-01-23 15:25:48 +00001549 if( rc && (p->rc==SQLITE_OK || p->rc==SQLITE_CONSTRAINT) ){
1550 p->rc = rc;
1551 sqlite3SetString(&p->zErrMsg, 0);
1552 }
danielk197707cb5602006-01-20 10:55:05 +00001553 }
danielk197777d83ba2004-05-31 10:08:14 +00001554 }
danielk197707cb5602006-01-20 10:55:05 +00001555
1556 /* If this was an INSERT, UPDATE or DELETE and the statement was committed,
1557 ** set the change counter.
1558 */
1559 if( p->changeCntOn && p->pc>=0 ){
1560 if( !xFunc || xFunc==sqlite3BtreeCommitStmt ){
1561 sqlite3VdbeSetChanges(db, p->nChange);
1562 }else{
1563 sqlite3VdbeSetChanges(db, 0);
1564 }
1565 p->nChange = 0;
danielk1977b28af712004-06-21 06:50:26 +00001566 }
danielk197707cb5602006-01-20 10:55:05 +00001567
1568 /* Rollback or commit any schema changes that occurred. */
1569 if( p->rc!=SQLITE_OK && db->flags&SQLITE_InternChanges ){
1570 sqlite3ResetInternalSchema(db, 0);
1571 db->flags = (db->flags | SQLITE_InternChanges);
1572 }
drhff0587c2007-08-29 17:43:19 +00001573
1574 /* Release the locks */
1575 sqlite3BtreeMutexArrayLeave(&p->aMutex);
drh9a324642003-09-06 20:12:01 +00001576 }
danielk19771d850a72004-05-31 08:26:49 +00001577
danielk197765fd59f2006-06-24 11:51:33 +00001578 /* We have successfully halted and closed the VM. Record this fact. */
1579 if( p->pc>=0 ){
danielk19771d850a72004-05-31 08:26:49 +00001580 db->activeVdbeCnt--;
drh9a324642003-09-06 20:12:01 +00001581 }
drh92f02c32004-09-02 14:57:08 +00001582 p->magic = VDBE_MAGIC_HALT;
1583 checkActiveVdbeCnt(db);
drhff0587c2007-08-29 17:43:19 +00001584 if( p->db->mallocFailed ){
1585 p->rc = SQLITE_NOMEM;
1586 }
1587 checkActiveVdbeCnt(db);
danielk19771d850a72004-05-31 08:26:49 +00001588
drh92f02c32004-09-02 14:57:08 +00001589 return SQLITE_OK;
1590}
drh4cf7c7f2007-08-28 23:28:07 +00001591
drh92f02c32004-09-02 14:57:08 +00001592
1593/*
drh3c23a882007-01-09 14:01:13 +00001594** Each VDBE holds the result of the most recent sqlite3_step() call
1595** in p->rc. This routine sets that result back to SQLITE_OK.
1596*/
1597void sqlite3VdbeResetStepResult(Vdbe *p){
1598 p->rc = SQLITE_OK;
1599}
1600
1601/*
drh92f02c32004-09-02 14:57:08 +00001602** Clean up a VDBE after execution but do not delete the VDBE just yet.
1603** Write any error messages into *pzErrMsg. Return the result code.
1604**
1605** After this routine is run, the VDBE should be ready to be executed
1606** again.
1607**
1608** To look at it another way, this routine resets the state of the
1609** virtual machine from VDBE_MAGIC_RUN or VDBE_MAGIC_HALT back to
1610** VDBE_MAGIC_INIT.
1611*/
1612int sqlite3VdbeReset(Vdbe *p){
drh4ac285a2006-09-15 07:28:50 +00001613 sqlite3 *db;
drh4ac285a2006-09-15 07:28:50 +00001614 db = p->db;
drh92f02c32004-09-02 14:57:08 +00001615
1616 /* If the VM did not run to completion or if it encountered an
1617 ** error, then it might not have been halted properly. So halt
1618 ** it now.
1619 */
drh7e8b8482008-01-23 03:03:05 +00001620 (void)sqlite3SafetyOn(db);
drh92f02c32004-09-02 14:57:08 +00001621 sqlite3VdbeHalt(p);
drh7e8b8482008-01-23 03:03:05 +00001622 (void)sqlite3SafetyOff(db);
drh92f02c32004-09-02 14:57:08 +00001623
drhfb7e7652005-01-24 00:28:42 +00001624 /* If the VDBE has be run even partially, then transfer the error code
1625 ** and error message from the VDBE into the main database structure. But
1626 ** if the VDBE has just been set to run but has not actually executed any
1627 ** instructions yet, leave the main database error information unchanged.
drh92f02c32004-09-02 14:57:08 +00001628 */
drhfb7e7652005-01-24 00:28:42 +00001629 if( p->pc>=0 ){
1630 if( p->zErrMsg ){
drhb21c8cd2007-08-21 19:33:56 +00001631 sqlite3ValueSetStr(db->pErr,-1,p->zErrMsg,SQLITE_UTF8,sqlite3_free);
danielk197797a227c2006-01-20 16:32:04 +00001632 db->errCode = p->rc;
drhfb7e7652005-01-24 00:28:42 +00001633 p->zErrMsg = 0;
1634 }else if( p->rc ){
drh4ac285a2006-09-15 07:28:50 +00001635 sqlite3Error(db, p->rc, 0);
drhfb7e7652005-01-24 00:28:42 +00001636 }else{
drh4ac285a2006-09-15 07:28:50 +00001637 sqlite3Error(db, SQLITE_OK, 0);
drhfb7e7652005-01-24 00:28:42 +00001638 }
danielk1977a21c6b62005-01-24 10:25:59 +00001639 }else if( p->rc && p->expired ){
1640 /* The expired flag was set on the VDBE before the first call
1641 ** to sqlite3_step(). For consistency (since sqlite3_step() was
1642 ** called), set the database error in this case as well.
1643 */
drh4ac285a2006-09-15 07:28:50 +00001644 sqlite3Error(db, p->rc, 0);
danielk19778e556522007-11-13 10:30:24 +00001645 sqlite3ValueSetStr(db->pErr, -1, p->zErrMsg, SQLITE_UTF8, sqlite3_free);
1646 p->zErrMsg = 0;
drh92f02c32004-09-02 14:57:08 +00001647 }
1648
1649 /* Reclaim all memory used by the VDBE
1650 */
1651 Cleanup(p);
1652
1653 /* Save profiling information from this VDBE run.
1654 */
drh9a324642003-09-06 20:12:01 +00001655#ifdef VDBE_PROFILE
1656 {
1657 FILE *out = fopen("vdbe_profile.out", "a");
1658 if( out ){
1659 int i;
1660 fprintf(out, "---- ");
1661 for(i=0; i<p->nOp; i++){
1662 fprintf(out, "%02x", p->aOp[i].opcode);
1663 }
1664 fprintf(out, "\n");
1665 for(i=0; i<p->nOp; i++){
1666 fprintf(out, "%6d %10lld %8lld ",
1667 p->aOp[i].cnt,
1668 p->aOp[i].cycles,
1669 p->aOp[i].cnt>0 ? p->aOp[i].cycles/p->aOp[i].cnt : 0
1670 );
danielk19774adee202004-05-08 08:23:19 +00001671 sqlite3VdbePrintOp(out, i, &p->aOp[i]);
drh9a324642003-09-06 20:12:01 +00001672 }
1673 fclose(out);
1674 }
1675 }
1676#endif
1677 p->magic = VDBE_MAGIC_INIT;
drh91b48aa2004-06-30 11:14:18 +00001678 p->aborted = 0;
drh4ac285a2006-09-15 07:28:50 +00001679 return p->rc & db->errMask;
drh9a324642003-09-06 20:12:01 +00001680}
drh92f02c32004-09-02 14:57:08 +00001681
drh9a324642003-09-06 20:12:01 +00001682/*
1683** Clean up and delete a VDBE after execution. Return an integer which is
1684** the result code. Write any error message text into *pzErrMsg.
1685*/
danielk19779e6db7d2004-06-21 08:18:51 +00001686int sqlite3VdbeFinalize(Vdbe *p){
danielk1977b5548a82004-06-26 13:51:33 +00001687 int rc = SQLITE_OK;
danielk1977b5548a82004-06-26 13:51:33 +00001688 if( p->magic==VDBE_MAGIC_RUN || p->magic==VDBE_MAGIC_HALT ){
1689 rc = sqlite3VdbeReset(p);
drh4ac285a2006-09-15 07:28:50 +00001690 assert( (rc & p->db->errMask)==rc );
danielk1977b5548a82004-06-26 13:51:33 +00001691 }else if( p->magic!=VDBE_MAGIC_INIT ){
drh9a324642003-09-06 20:12:01 +00001692 return SQLITE_MISUSE;
1693 }
danielk19774adee202004-05-08 08:23:19 +00001694 sqlite3VdbeDelete(p);
drh9a324642003-09-06 20:12:01 +00001695 return rc;
1696}
1697
1698/*
drhf92c7ff2004-06-19 15:40:23 +00001699** Call the destructor for each auxdata entry in pVdbeFunc for which
danielk1977e159fdf2004-06-21 10:45:06 +00001700** the corresponding bit in mask is clear. Auxdata entries beyond 31
drhf92c7ff2004-06-19 15:40:23 +00001701** are always destroyed. To destroy all auxdata entries, call this
danielk1977e159fdf2004-06-21 10:45:06 +00001702** routine with mask==0.
drhf92c7ff2004-06-19 15:40:23 +00001703*/
1704void sqlite3VdbeDeleteAuxData(VdbeFunc *pVdbeFunc, int mask){
1705 int i;
1706 for(i=0; i<pVdbeFunc->nAux; i++){
1707 struct AuxData *pAux = &pVdbeFunc->apAux[i];
1708 if( (i>31 || !(mask&(1<<i))) && pAux->pAux ){
1709 if( pAux->xDelete ){
1710 pAux->xDelete(pAux->pAux);
1711 }
1712 pAux->pAux = 0;
1713 }
1714 }
1715}
1716
1717/*
drh9a324642003-09-06 20:12:01 +00001718** Delete an entire VDBE.
1719*/
danielk19774adee202004-05-08 08:23:19 +00001720void sqlite3VdbeDelete(Vdbe *p){
drh9a324642003-09-06 20:12:01 +00001721 int i;
1722 if( p==0 ) return;
1723 Cleanup(p);
1724 if( p->pPrev ){
1725 p->pPrev->pNext = p->pNext;
1726 }else{
1727 assert( p->db->pVdbe==p );
1728 p->db->pVdbe = p->pNext;
1729 }
1730 if( p->pNext ){
1731 p->pNext->pPrev = p->pPrev;
1732 }
drh76ff3a02004-09-24 22:32:30 +00001733 if( p->aOp ){
drhd4e70eb2008-01-02 00:34:36 +00001734 Op *pOp = p->aOp;
1735 for(i=0; i<p->nOp; i++, pOp++){
drh66a51672008-01-03 00:01:23 +00001736 freeP4(pOp->p4type, pOp->p4.p);
drhd4e70eb2008-01-02 00:34:36 +00001737#ifdef SQLITE_DEBUG
1738 sqlite3_free(pOp->zComment);
1739#endif
drh9a324642003-09-06 20:12:01 +00001740 }
drh17435752007-08-16 04:30:38 +00001741 sqlite3_free(p->aOp);
drh9a324642003-09-06 20:12:01 +00001742 }
drh76ff3a02004-09-24 22:32:30 +00001743 releaseMemArray(p->aVar, p->nVar);
drh17435752007-08-16 04:30:38 +00001744 sqlite3_free(p->aLabel);
drh9cbf3422008-01-17 16:22:13 +00001745 if( p->aMem ){
1746 sqlite3_free(&p->aMem[1]);
1747 }
danielk1977955de522006-02-10 02:27:42 +00001748 releaseMemArray(p->aColName, p->nResColumn*COLNAME_N);
drh17435752007-08-16 04:30:38 +00001749 sqlite3_free(p->aColName);
1750 sqlite3_free(p->zSql);
drh9a324642003-09-06 20:12:01 +00001751 p->magic = VDBE_MAGIC_DEAD;
drh17435752007-08-16 04:30:38 +00001752 sqlite3_free(p);
drh9a324642003-09-06 20:12:01 +00001753}
drha11846b2004-01-07 18:52:56 +00001754
1755/*
drha11846b2004-01-07 18:52:56 +00001756** If a MoveTo operation is pending on the given cursor, then do that
1757** MoveTo now. Return an error code. If no MoveTo is pending, this
1758** routine does nothing and returns SQLITE_OK.
1759*/
danielk19774adee202004-05-08 08:23:19 +00001760int sqlite3VdbeCursorMoveto(Cursor *p){
drha11846b2004-01-07 18:52:56 +00001761 if( p->deferredMoveto ){
drh536065a2005-01-26 21:55:31 +00001762 int res, rc;
adamd4fc93082006-09-14 16:57:19 +00001763#ifdef SQLITE_TEST
danielk1977132872b2004-05-10 10:37:18 +00001764 extern int sqlite3_search_count;
adamd4fc93082006-09-14 16:57:19 +00001765#endif
drhf0863fe2005-06-12 21:35:51 +00001766 assert( p->isTable );
drh5f9c1a22007-04-04 01:27:44 +00001767 rc = sqlite3BtreeMoveto(p->pCursor, 0, p->movetoTarget, 0, &res);
drh536065a2005-01-26 21:55:31 +00001768 if( rc ) return rc;
drhd3d39e92004-05-20 22:16:29 +00001769 *p->pIncrKey = 0;
drhf0863fe2005-06-12 21:35:51 +00001770 p->lastRowid = keyToInt(p->movetoTarget);
1771 p->rowidIsValid = res==0;
drha11846b2004-01-07 18:52:56 +00001772 if( res<0 ){
drh536065a2005-01-26 21:55:31 +00001773 rc = sqlite3BtreeNext(p->pCursor, &res);
1774 if( rc ) return rc;
drha11846b2004-01-07 18:52:56 +00001775 }
drh10cfdd52006-08-08 15:42:59 +00001776#ifdef SQLITE_TEST
danielk1977132872b2004-05-10 10:37:18 +00001777 sqlite3_search_count++;
drh10cfdd52006-08-08 15:42:59 +00001778#endif
drha11846b2004-01-07 18:52:56 +00001779 p->deferredMoveto = 0;
drh76873ab2006-01-07 18:48:26 +00001780 p->cacheStatus = CACHE_STALE;
drha11846b2004-01-07 18:52:56 +00001781 }
1782 return SQLITE_OK;
1783}
danielk19774adee202004-05-08 08:23:19 +00001784
drhab9f7f12004-05-08 10:56:11 +00001785/*
danielk1977cfcdaef2004-05-12 07:33:33 +00001786** The following functions:
danielk197790e4d952004-05-10 10:05:53 +00001787**
danielk1977cfcdaef2004-05-12 07:33:33 +00001788** sqlite3VdbeSerialType()
1789** sqlite3VdbeSerialTypeLen()
1790** sqlite3VdbeSerialRead()
danielk197790e4d952004-05-10 10:05:53 +00001791** sqlite3VdbeSerialLen()
danielk1977cfcdaef2004-05-12 07:33:33 +00001792** sqlite3VdbeSerialWrite()
danielk197790e4d952004-05-10 10:05:53 +00001793**
1794** encapsulate the code that serializes values for storage in SQLite
danielk1977cfcdaef2004-05-12 07:33:33 +00001795** data and index records. Each serialized value consists of a
1796** 'serial-type' and a blob of data. The serial type is an 8-byte unsigned
1797** integer, stored as a varint.
danielk197790e4d952004-05-10 10:05:53 +00001798**
danielk1977cfcdaef2004-05-12 07:33:33 +00001799** In an SQLite index record, the serial type is stored directly before
1800** the blob of data that it corresponds to. In a table record, all serial
1801** types are stored at the start of the record, and the blobs of data at
1802** the end. Hence these functions allow the caller to handle the
1803** serial-type and data blob seperately.
1804**
1805** The following table describes the various storage classes for data:
1806**
1807** serial type bytes of data type
danielk197790e4d952004-05-10 10:05:53 +00001808** -------------- --------------- ---------------
drha19b7752004-05-30 21:14:58 +00001809** 0 0 NULL
danielk197790e4d952004-05-10 10:05:53 +00001810** 1 1 signed integer
1811** 2 2 signed integer
drha19b7752004-05-30 21:14:58 +00001812** 3 3 signed integer
1813** 4 4 signed integer
1814** 5 6 signed integer
1815** 6 8 signed integer
1816** 7 8 IEEE float
drhd946db02005-12-29 19:23:06 +00001817** 8 0 Integer constant 0
1818** 9 0 Integer constant 1
1819** 10,11 reserved for expansion
danielk197790e4d952004-05-10 10:05:53 +00001820** N>=12 and even (N-12)/2 BLOB
1821** N>=13 and odd (N-13)/2 text
1822**
drh35a59652006-01-02 18:24:40 +00001823** The 8 and 9 types were added in 3.3.0, file format 4. Prior versions
1824** of SQLite will not understand those serial types.
danielk197790e4d952004-05-10 10:05:53 +00001825*/
1826
1827/*
danielk1977cfcdaef2004-05-12 07:33:33 +00001828** Return the serial-type for the value stored in pMem.
danielk1977192ac1d2004-05-10 07:17:30 +00001829*/
drhd946db02005-12-29 19:23:06 +00001830u32 sqlite3VdbeSerialType(Mem *pMem, int file_format){
danielk1977cfcdaef2004-05-12 07:33:33 +00001831 int flags = pMem->flags;
drhfdf972a2007-05-02 13:30:27 +00001832 int n;
danielk1977cfcdaef2004-05-12 07:33:33 +00001833
1834 if( flags&MEM_Null ){
drha19b7752004-05-30 21:14:58 +00001835 return 0;
danielk197790e4d952004-05-10 10:05:53 +00001836 }
danielk1977cfcdaef2004-05-12 07:33:33 +00001837 if( flags&MEM_Int ){
drhfe2093d2005-01-20 22:48:47 +00001838 /* Figure out whether to use 1, 2, 4, 6 or 8 bytes. */
drhb47d45c2005-04-15 12:04:34 +00001839# define MAX_6BYTE ((((i64)0x00001000)<<32)-1)
drh3c024d62007-03-30 11:23:45 +00001840 i64 i = pMem->u.i;
drhd946db02005-12-29 19:23:06 +00001841 u64 u;
1842 if( file_format>=4 && (i&1)==i ){
1843 return 8+i;
1844 }
1845 u = i<0 ? -i : i;
drh5742b632005-01-26 17:47:02 +00001846 if( u<=127 ) return 1;
1847 if( u<=32767 ) return 2;
1848 if( u<=8388607 ) return 3;
1849 if( u<=2147483647 ) return 4;
1850 if( u<=MAX_6BYTE ) return 5;
drha19b7752004-05-30 21:14:58 +00001851 return 6;
danielk197790e4d952004-05-10 10:05:53 +00001852 }
danielk1977cfcdaef2004-05-12 07:33:33 +00001853 if( flags&MEM_Real ){
drha19b7752004-05-30 21:14:58 +00001854 return 7;
danielk197790e4d952004-05-10 10:05:53 +00001855 }
drhfdf972a2007-05-02 13:30:27 +00001856 assert( flags&(MEM_Str|MEM_Blob) );
1857 n = pMem->n;
1858 if( flags & MEM_Zero ){
1859 n += pMem->u.i;
danielk197790e4d952004-05-10 10:05:53 +00001860 }
drhfdf972a2007-05-02 13:30:27 +00001861 assert( n>=0 );
1862 return ((n*2) + 12 + ((flags&MEM_Str)!=0));
danielk1977192ac1d2004-05-10 07:17:30 +00001863}
1864
1865/*
danielk1977cfcdaef2004-05-12 07:33:33 +00001866** Return the length of the data corresponding to the supplied serial-type.
danielk1977192ac1d2004-05-10 07:17:30 +00001867*/
drh25aa1b42004-05-28 01:39:01 +00001868int sqlite3VdbeSerialTypeLen(u32 serial_type){
drha19b7752004-05-30 21:14:58 +00001869 if( serial_type>=12 ){
drh51846b52004-05-28 16:00:21 +00001870 return (serial_type-12)/2;
1871 }else{
drh57196282004-10-06 15:41:16 +00001872 static const u8 aSize[] = { 0, 1, 2, 3, 4, 6, 8, 8, 0, 0, 0, 0 };
drh51846b52004-05-28 16:00:21 +00001873 return aSize[serial_type];
1874 }
danielk1977192ac1d2004-05-10 07:17:30 +00001875}
1876
1877/*
drh110daac2007-05-04 11:59:31 +00001878** If we are on an architecture with mixed-endian floating
drh7a4f5022007-05-23 07:20:08 +00001879** points (ex: ARM7) then swap the lower 4 bytes with the
drh110daac2007-05-04 11:59:31 +00001880** upper 4 bytes. Return the result.
1881**
drh7a4f5022007-05-23 07:20:08 +00001882** For most architectures, this is a no-op.
1883**
1884** (later): It is reported to me that the mixed-endian problem
1885** on ARM7 is an issue with GCC, not with the ARM7 chip. It seems
1886** that early versions of GCC stored the two words of a 64-bit
1887** float in the wrong order. And that error has been propagated
1888** ever since. The blame is not necessarily with GCC, though.
1889** GCC might have just copying the problem from a prior compiler.
1890** I am also told that newer versions of GCC that follow a different
1891** ABI get the byte order right.
1892**
1893** Developers using SQLite on an ARM7 should compile and run their
1894** application using -DSQLITE_DEBUG=1 at least once. With DEBUG
1895** enabled, some asserts below will ensure that the byte order of
1896** floating point values is correct.
drh60d09a72007-08-30 15:05:08 +00001897**
1898** (2007-08-30) Frank van Vugt has studied this problem closely
1899** and has send his findings to the SQLite developers. Frank
1900** writes that some Linux kernels offer floating point hardware
1901** emulation that uses only 32-bit mantissas instead of a full
1902** 48-bits as required by the IEEE standard. (This is the
1903** CONFIG_FPE_FASTFPE option.) On such systems, floating point
1904** byte swapping becomes very complicated. To avoid problems,
1905** the necessary byte swapping is carried out using a 64-bit integer
1906** rather than a 64-bit float. Frank assures us that the code here
1907** works for him. We, the developers, have no way to independently
1908** verify this, but Frank seems to know what he is talking about
1909** so we trust him.
drh110daac2007-05-04 11:59:31 +00001910*/
1911#ifdef SQLITE_MIXED_ENDIAN_64BIT_FLOAT
drh60d09a72007-08-30 15:05:08 +00001912static u64 floatSwap(u64 in){
drh110daac2007-05-04 11:59:31 +00001913 union {
drh60d09a72007-08-30 15:05:08 +00001914 u64 r;
drh110daac2007-05-04 11:59:31 +00001915 u32 i[2];
1916 } u;
1917 u32 t;
1918
1919 u.r = in;
1920 t = u.i[0];
1921 u.i[0] = u.i[1];
1922 u.i[1] = t;
1923 return u.r;
1924}
1925# define swapMixedEndianFloat(X) X = floatSwap(X)
1926#else
1927# define swapMixedEndianFloat(X)
1928#endif
1929
1930/*
danielk1977cfcdaef2004-05-12 07:33:33 +00001931** Write the serialized data blob for the value stored in pMem into
1932** buf. It is assumed that the caller has allocated sufficient space.
1933** Return the number of bytes written.
drhfdf972a2007-05-02 13:30:27 +00001934**
1935** nBuf is the amount of space left in buf[]. nBuf must always be
1936** large enough to hold the entire field. Except, if the field is
1937** a blob with a zero-filled tail, then buf[] might be just the right
1938** size to hold everything except for the zero-filled tail. If buf[]
1939** is only big enough to hold the non-zero prefix, then only write that
1940** prefix into buf[]. But if buf[] is large enough to hold both the
1941** prefix and the tail then write the prefix and set the tail to all
1942** zeros.
1943**
1944** Return the number of bytes actually written into buf[]. The number
1945** of bytes in the zero-filled tail is included in the return value only
1946** if those bytes were zeroed in buf[].
danielk1977cfcdaef2004-05-12 07:33:33 +00001947*/
drhfdf972a2007-05-02 13:30:27 +00001948int sqlite3VdbeSerialPut(u8 *buf, int nBuf, Mem *pMem, int file_format){
drhd946db02005-12-29 19:23:06 +00001949 u32 serial_type = sqlite3VdbeSerialType(pMem, file_format);
danielk1977cfcdaef2004-05-12 07:33:33 +00001950 int len;
danielk1977183f9f72004-05-13 05:20:26 +00001951
drh1483e142004-05-21 21:12:42 +00001952 /* Integer and Real */
drhd946db02005-12-29 19:23:06 +00001953 if( serial_type<=7 && serial_type>0 ){
drh1483e142004-05-21 21:12:42 +00001954 u64 v;
1955 int i;
drha19b7752004-05-30 21:14:58 +00001956 if( serial_type==7 ){
drh4f0c5872007-03-26 22:05:01 +00001957 assert( sizeof(v)==sizeof(pMem->r) );
1958 memcpy(&v, &pMem->r, sizeof(v));
drh60d09a72007-08-30 15:05:08 +00001959 swapMixedEndianFloat(v);
drh1483e142004-05-21 21:12:42 +00001960 }else{
drh3c024d62007-03-30 11:23:45 +00001961 v = pMem->u.i;
danielk1977cfcdaef2004-05-12 07:33:33 +00001962 }
drh1483e142004-05-21 21:12:42 +00001963 len = i = sqlite3VdbeSerialTypeLen(serial_type);
drhfdf972a2007-05-02 13:30:27 +00001964 assert( len<=nBuf );
drh1483e142004-05-21 21:12:42 +00001965 while( i-- ){
1966 buf[i] = (v&0xFF);
1967 v >>= 8;
1968 }
1969 return len;
danielk1977cfcdaef2004-05-12 07:33:33 +00001970 }
drhd946db02005-12-29 19:23:06 +00001971
danielk1977cfcdaef2004-05-12 07:33:33 +00001972 /* String or blob */
drhd946db02005-12-29 19:23:06 +00001973 if( serial_type>=12 ){
drhfdf972a2007-05-02 13:30:27 +00001974 assert( pMem->n + ((pMem->flags & MEM_Zero)?pMem->u.i:0)
1975 == sqlite3VdbeSerialTypeLen(serial_type) );
1976 assert( pMem->n<=nBuf );
1977 len = pMem->n;
drhd946db02005-12-29 19:23:06 +00001978 memcpy(buf, pMem->z, len);
drhfdf972a2007-05-02 13:30:27 +00001979 if( pMem->flags & MEM_Zero ){
1980 len += pMem->u.i;
1981 if( len>nBuf ){
1982 len = nBuf;
1983 }
1984 memset(&buf[pMem->n], 0, len-pMem->n);
1985 }
drhd946db02005-12-29 19:23:06 +00001986 return len;
1987 }
1988
1989 /* NULL or constants 0 or 1 */
1990 return 0;
danielk1977cfcdaef2004-05-12 07:33:33 +00001991}
1992
1993/*
1994** Deserialize the data blob pointed to by buf as serial type serial_type
1995** and store the result in pMem. Return the number of bytes read.
1996*/
danielk1977b1bc9532004-05-22 03:05:33 +00001997int sqlite3VdbeSerialGet(
danielk197793d46752004-05-23 13:30:58 +00001998 const unsigned char *buf, /* Buffer to deserialize from */
drh25aa1b42004-05-28 01:39:01 +00001999 u32 serial_type, /* Serial type to deserialize */
2000 Mem *pMem /* Memory cell to write value into */
danielk1977b1bc9532004-05-22 03:05:33 +00002001){
drh3c685822005-05-21 18:32:18 +00002002 switch( serial_type ){
drh3c685822005-05-21 18:32:18 +00002003 case 10: /* Reserved for future use */
2004 case 11: /* Reserved for future use */
2005 case 0: { /* NULL */
2006 pMem->flags = MEM_Null;
2007 break;
2008 }
2009 case 1: { /* 1-byte signed integer */
drh3c024d62007-03-30 11:23:45 +00002010 pMem->u.i = (signed char)buf[0];
drh1483e142004-05-21 21:12:42 +00002011 pMem->flags = MEM_Int;
drh3c685822005-05-21 18:32:18 +00002012 return 1;
drh1483e142004-05-21 21:12:42 +00002013 }
drh3c685822005-05-21 18:32:18 +00002014 case 2: { /* 2-byte signed integer */
drh3c024d62007-03-30 11:23:45 +00002015 pMem->u.i = (((signed char)buf[0])<<8) | buf[1];
drh3c685822005-05-21 18:32:18 +00002016 pMem->flags = MEM_Int;
2017 return 2;
2018 }
2019 case 3: { /* 3-byte signed integer */
drh3c024d62007-03-30 11:23:45 +00002020 pMem->u.i = (((signed char)buf[0])<<16) | (buf[1]<<8) | buf[2];
drh3c685822005-05-21 18:32:18 +00002021 pMem->flags = MEM_Int;
2022 return 3;
2023 }
2024 case 4: { /* 4-byte signed integer */
drh3c024d62007-03-30 11:23:45 +00002025 pMem->u.i = (buf[0]<<24) | (buf[1]<<16) | (buf[2]<<8) | buf[3];
drh3c685822005-05-21 18:32:18 +00002026 pMem->flags = MEM_Int;
2027 return 4;
2028 }
2029 case 5: { /* 6-byte signed integer */
2030 u64 x = (((signed char)buf[0])<<8) | buf[1];
2031 u32 y = (buf[2]<<24) | (buf[3]<<16) | (buf[4]<<8) | buf[5];
2032 x = (x<<32) | y;
drh3c024d62007-03-30 11:23:45 +00002033 pMem->u.i = *(i64*)&x;
drh3c685822005-05-21 18:32:18 +00002034 pMem->flags = MEM_Int;
2035 return 6;
2036 }
drh91124b32005-08-18 18:15:05 +00002037 case 6: /* 8-byte signed integer */
drh3c685822005-05-21 18:32:18 +00002038 case 7: { /* IEEE floating point */
drhd81bd4e2005-09-05 20:06:49 +00002039 u64 x;
2040 u32 y;
drh2a3e4a72006-01-23 21:44:53 +00002041#if !defined(NDEBUG) && !defined(SQLITE_OMIT_FLOATING_POINT)
drhde941c62005-08-28 01:34:21 +00002042 /* Verify that integers and floating point values use the same
drh110daac2007-05-04 11:59:31 +00002043 ** byte order. Or, that if SQLITE_MIXED_ENDIAN_64BIT_FLOAT is
2044 ** defined that 64-bit floating point values really are mixed
2045 ** endian.
drhbfd6b032005-08-28 01:38:44 +00002046 */
drhde941c62005-08-28 01:34:21 +00002047 static const u64 t1 = ((u64)0x3ff00000)<<32;
drh4f0c5872007-03-26 22:05:01 +00002048 static const double r1 = 1.0;
drh60d09a72007-08-30 15:05:08 +00002049 u64 t2 = t1;
2050 swapMixedEndianFloat(t2);
2051 assert( sizeof(r1)==sizeof(t2) && memcmp(&r1, &t2, sizeof(r1))==0 );
drhde941c62005-08-28 01:34:21 +00002052#endif
drhbfd6b032005-08-28 01:38:44 +00002053
drhd81bd4e2005-09-05 20:06:49 +00002054 x = (buf[0]<<24) | (buf[1]<<16) | (buf[2]<<8) | buf[3];
2055 y = (buf[4]<<24) | (buf[5]<<16) | (buf[6]<<8) | buf[7];
drh3c685822005-05-21 18:32:18 +00002056 x = (x<<32) | y;
2057 if( serial_type==6 ){
drh3c024d62007-03-30 11:23:45 +00002058 pMem->u.i = *(i64*)&x;
drh3c685822005-05-21 18:32:18 +00002059 pMem->flags = MEM_Int;
2060 }else{
drh4f0c5872007-03-26 22:05:01 +00002061 assert( sizeof(x)==8 && sizeof(pMem->r)==8 );
drh60d09a72007-08-30 15:05:08 +00002062 swapMixedEndianFloat(x);
drh4f0c5872007-03-26 22:05:01 +00002063 memcpy(&pMem->r, &x, sizeof(x));
drh3c685822005-05-21 18:32:18 +00002064 pMem->flags = MEM_Real;
2065 }
2066 return 8;
2067 }
drhd946db02005-12-29 19:23:06 +00002068 case 8: /* Integer 0 */
2069 case 9: { /* Integer 1 */
drh3c024d62007-03-30 11:23:45 +00002070 pMem->u.i = serial_type-8;
drhd946db02005-12-29 19:23:06 +00002071 pMem->flags = MEM_Int;
2072 return 0;
2073 }
drh3c685822005-05-21 18:32:18 +00002074 default: {
2075 int len = (serial_type-12)/2;
2076 pMem->z = (char *)buf;
2077 pMem->n = len;
2078 pMem->xDel = 0;
2079 if( serial_type&0x01 ){
2080 pMem->flags = MEM_Str | MEM_Ephem;
2081 }else{
2082 pMem->flags = MEM_Blob | MEM_Ephem;
2083 }
2084 return len;
drh696b32f2004-05-30 01:51:52 +00002085 }
danielk1977cfcdaef2004-05-12 07:33:33 +00002086 }
drh3c685822005-05-21 18:32:18 +00002087 return 0;
danielk1977192ac1d2004-05-10 07:17:30 +00002088}
2089
2090/*
drh0e6082e2006-01-12 20:28:35 +00002091** The header of a record consists of a sequence variable-length integers.
2092** These integers are almost always small and are encoded as a single byte.
2093** The following macro takes advantage this fact to provide a fast decode
2094** of the integers in a record header. It is faster for the common case
2095** where the integer is a single byte. It is a little slower when the
2096** integer is two or more bytes. But overall it is faster.
2097**
2098** The following expressions are equivalent:
2099**
2100** x = sqlite3GetVarint32( A, &B );
2101**
2102** x = GetVarint( A, B );
2103**
2104*/
2105#define GetVarint(A,B) ((B = *(A))<=0x7f ? 1 : sqlite3GetVarint32(A, &B))
2106
2107/*
drh7a224de2004-06-02 01:22:02 +00002108** This function compares the two table rows or index records specified by
danielk1977eb015e02004-05-18 01:31:14 +00002109** {nKey1, pKey1} and {nKey2, pKey2}, returning a negative, zero
2110** or positive integer if {nKey1, pKey1} is less than, equal to or
drh7a224de2004-06-02 01:22:02 +00002111** greater than {nKey2, pKey2}. Both Key1 and Key2 must be byte strings
2112** composed by the OP_MakeRecord opcode of the VDBE.
drhc4dd3fd2008-01-22 01:48:05 +00002113**
2114** Key1 and Key2 do not have to contain the same number of fields.
2115** But if the lengths differ, Key2 must be the shorter of the two.
danielk1977eb015e02004-05-18 01:31:14 +00002116*/
drh7a224de2004-06-02 01:22:02 +00002117int sqlite3VdbeRecordCompare(
danielk1977eb015e02004-05-18 01:31:14 +00002118 void *userData,
2119 int nKey1, const void *pKey1,
2120 int nKey2, const void *pKey2
2121){
drhd3d39e92004-05-20 22:16:29 +00002122 KeyInfo *pKeyInfo = (KeyInfo*)userData;
drhd3194f52004-05-27 19:59:32 +00002123 u32 d1, d2; /* Offset into aKey[] of next data element */
2124 u32 idx1, idx2; /* Offset into aKey[] of next header element */
2125 u32 szHdr1, szHdr2; /* Number of bytes in header */
2126 int i = 0;
2127 int nField;
2128 int rc = 0;
danielk1977eb015e02004-05-18 01:31:14 +00002129 const unsigned char *aKey1 = (const unsigned char *)pKey1;
2130 const unsigned char *aKey2 = (const unsigned char *)pKey2;
danielk19770202b292004-06-09 09:55:16 +00002131
2132 Mem mem1;
2133 Mem mem2;
2134 mem1.enc = pKeyInfo->enc;
drhb21c8cd2007-08-21 19:33:56 +00002135 mem1.db = pKeyInfo->db;
danielk19770202b292004-06-09 09:55:16 +00002136 mem2.enc = pKeyInfo->enc;
drhb21c8cd2007-08-21 19:33:56 +00002137 mem2.db = pKeyInfo->db;
drhd3194f52004-05-27 19:59:32 +00002138
drh0e6082e2006-01-12 20:28:35 +00002139 idx1 = GetVarint(aKey1, szHdr1);
drhd3194f52004-05-27 19:59:32 +00002140 d1 = szHdr1;
drh0e6082e2006-01-12 20:28:35 +00002141 idx2 = GetVarint(aKey2, szHdr2);
drhd3194f52004-05-27 19:59:32 +00002142 d2 = szHdr2;
2143 nField = pKeyInfo->nField;
drhd5788202004-05-28 08:21:05 +00002144 while( idx1<szHdr1 && idx2<szHdr2 ){
drhd3194f52004-05-27 19:59:32 +00002145 u32 serial_type1;
2146 u32 serial_type2;
danielk197784ac9d02004-05-18 09:58:06 +00002147
2148 /* Read the serial types for the next element in each key. */
drh0e6082e2006-01-12 20:28:35 +00002149 idx1 += GetVarint( aKey1+idx1, serial_type1 );
drhd5788202004-05-28 08:21:05 +00002150 if( d1>=nKey1 && sqlite3VdbeSerialTypeLen(serial_type1)>0 ) break;
drh0e6082e2006-01-12 20:28:35 +00002151 idx2 += GetVarint( aKey2+idx2, serial_type2 );
drhd5788202004-05-28 08:21:05 +00002152 if( d2>=nKey2 && sqlite3VdbeSerialTypeLen(serial_type2)>0 ) break;
danielk197784ac9d02004-05-18 09:58:06 +00002153
drh0660e262006-10-27 14:06:57 +00002154 /* Extract the values to be compared.
danielk197784ac9d02004-05-18 09:58:06 +00002155 */
drh25aa1b42004-05-28 01:39:01 +00002156 d1 += sqlite3VdbeSerialGet(&aKey1[d1], serial_type1, &mem1);
2157 d2 += sqlite3VdbeSerialGet(&aKey2[d2], serial_type2, &mem2);
danielk197784ac9d02004-05-18 09:58:06 +00002158
drh0660e262006-10-27 14:06:57 +00002159 /* Do the comparison
2160 */
drhd5788202004-05-28 08:21:05 +00002161 rc = sqlite3MemCompare(&mem1, &mem2, i<nField ? pKeyInfo->aColl[i] : 0);
drh3c685822005-05-21 18:32:18 +00002162 if( mem1.flags & MEM_Dyn ) sqlite3VdbeMemRelease(&mem1);
2163 if( mem2.flags & MEM_Dyn ) sqlite3VdbeMemRelease(&mem2);
danielk197784ac9d02004-05-18 09:58:06 +00002164 if( rc!=0 ){
drhd3194f52004-05-27 19:59:32 +00002165 break;
2166 }
2167 i++;
2168 }
2169
2170 /* One of the keys ran out of fields, but all the fields up to that point
2171 ** were equal. If the incrKey flag is true, then the second key is
2172 ** treated as larger.
2173 */
2174 if( rc==0 ){
2175 if( pKeyInfo->incrKey ){
drhd3194f52004-05-27 19:59:32 +00002176 rc = -1;
danielk19779a96b662007-11-29 17:05:18 +00002177 }else if( !pKeyInfo->prefixIsEqual ){
2178 if( d1<nKey1 ){
2179 rc = 1;
2180 }else if( d2<nKey2 ){
drhc4dd3fd2008-01-22 01:48:05 +00002181 rc = -1; /* Only occurs on a corrupt database file */
danielk19779a96b662007-11-29 17:05:18 +00002182 }
danielk197784ac9d02004-05-18 09:58:06 +00002183 }
drh0b2f3162005-12-21 18:36:45 +00002184 }else if( pKeyInfo->aSortOrder && i<pKeyInfo->nField
2185 && pKeyInfo->aSortOrder[i] ){
drhd3194f52004-05-27 19:59:32 +00002186 rc = -rc;
2187 }
2188
2189 return rc;
danielk1977eb015e02004-05-18 01:31:14 +00002190}
drhd5788202004-05-28 08:21:05 +00002191
2192/*
drh7a224de2004-06-02 01:22:02 +00002193** The argument is an index entry composed using the OP_MakeRecord opcode.
2194** The last entry in this record should be an integer (specifically
2195** an integer rowid). This routine returns the number of bytes in
2196** that integer.
drhd5788202004-05-28 08:21:05 +00002197*/
drh74161702006-02-24 02:53:49 +00002198int sqlite3VdbeIdxRowidLen(const u8 *aKey){
drhd5788202004-05-28 08:21:05 +00002199 u32 szHdr; /* Size of the header */
2200 u32 typeRowid; /* Serial type of the rowid */
2201
2202 sqlite3GetVarint32(aKey, &szHdr);
2203 sqlite3GetVarint32(&aKey[szHdr-1], &typeRowid);
2204 return sqlite3VdbeSerialTypeLen(typeRowid);
2205}
danielk1977eb015e02004-05-18 01:31:14 +00002206
2207
2208/*
drh7a224de2004-06-02 01:22:02 +00002209** pCur points at an index entry created using the OP_MakeRecord opcode.
2210** Read the rowid (the last field in the record) and store it in *rowid.
2211** Return SQLITE_OK if everything works, or an error code otherwise.
danielk1977183f9f72004-05-13 05:20:26 +00002212*/
drhb21c8cd2007-08-21 19:33:56 +00002213int sqlite3VdbeIdxRowid(BtCursor *pCur, i64 *rowid){
drh61fc5952007-04-01 23:49:51 +00002214 i64 nCellKey = 0;
danielk1977183f9f72004-05-13 05:20:26 +00002215 int rc;
drhd5788202004-05-28 08:21:05 +00002216 u32 szHdr; /* Size of the header */
2217 u32 typeRowid; /* Serial type of the rowid */
2218 u32 lenRowid; /* Size of the rowid */
2219 Mem m, v;
danielk1977183f9f72004-05-13 05:20:26 +00002220
drhd5788202004-05-28 08:21:05 +00002221 sqlite3BtreeKeySize(pCur, &nCellKey);
2222 if( nCellKey<=0 ){
drh49285702005-09-17 15:20:26 +00002223 return SQLITE_CORRUPT_BKPT;
drhd5788202004-05-28 08:21:05 +00002224 }
drhb21c8cd2007-08-21 19:33:56 +00002225 rc = sqlite3VdbeMemFromBtree(pCur, 0, nCellKey, 1, &m);
drhd5788202004-05-28 08:21:05 +00002226 if( rc ){
danielk1977183f9f72004-05-13 05:20:26 +00002227 return rc;
2228 }
drh2646da72005-12-09 20:02:05 +00002229 sqlite3GetVarint32((u8*)m.z, &szHdr);
2230 sqlite3GetVarint32((u8*)&m.z[szHdr-1], &typeRowid);
drhd5788202004-05-28 08:21:05 +00002231 lenRowid = sqlite3VdbeSerialTypeLen(typeRowid);
drh2646da72005-12-09 20:02:05 +00002232 sqlite3VdbeSerialGet((u8*)&m.z[m.n-lenRowid], typeRowid, &v);
drh3c024d62007-03-30 11:23:45 +00002233 *rowid = v.u.i;
danielk1977d8123362004-06-12 09:25:12 +00002234 sqlite3VdbeMemRelease(&m);
danielk1977183f9f72004-05-13 05:20:26 +00002235 return SQLITE_OK;
2236}
2237
drh7cf6e4d2004-05-19 14:56:55 +00002238/*
drhd3d39e92004-05-20 22:16:29 +00002239** Compare the key of the index entry that cursor pC is point to against
drh7cf6e4d2004-05-19 14:56:55 +00002240** the key string in pKey (of length nKey). Write into *pRes a number
2241** that is negative, zero, or positive if pC is less than, equal to,
2242** or greater than pKey. Return SQLITE_OK on success.
drhd3d39e92004-05-20 22:16:29 +00002243**
drhd5788202004-05-28 08:21:05 +00002244** pKey is either created without a rowid or is truncated so that it
2245** omits the rowid at the end. The rowid at the end of the index entry
2246** is ignored as well.
drh7cf6e4d2004-05-19 14:56:55 +00002247*/
danielk1977183f9f72004-05-13 05:20:26 +00002248int sqlite3VdbeIdxKeyCompare(
drh7cf6e4d2004-05-19 14:56:55 +00002249 Cursor *pC, /* The cursor to compare against */
2250 int nKey, const u8 *pKey, /* The key to compare */
2251 int *res /* Write the comparison result here */
danielk1977183f9f72004-05-13 05:20:26 +00002252){
drh61fc5952007-04-01 23:49:51 +00002253 i64 nCellKey = 0;
danielk1977183f9f72004-05-13 05:20:26 +00002254 int rc;
danielk19773d1bfea2004-05-14 11:00:53 +00002255 BtCursor *pCur = pC->pCursor;
drhd5788202004-05-28 08:21:05 +00002256 int lenRowid;
2257 Mem m;
danielk1977183f9f72004-05-13 05:20:26 +00002258
2259 sqlite3BtreeKeySize(pCur, &nCellKey);
2260 if( nCellKey<=0 ){
2261 *res = 0;
2262 return SQLITE_OK;
2263 }
drhb21c8cd2007-08-21 19:33:56 +00002264 rc = sqlite3VdbeMemFromBtree(pC->pCursor, 0, nCellKey, 1, &m);
drhd5788202004-05-28 08:21:05 +00002265 if( rc ){
2266 return rc;
danielk1977183f9f72004-05-13 05:20:26 +00002267 }
drh74161702006-02-24 02:53:49 +00002268 lenRowid = sqlite3VdbeIdxRowidLen((u8*)m.z);
drh7a224de2004-06-02 01:22:02 +00002269 *res = sqlite3VdbeRecordCompare(pC->pKeyInfo, m.n-lenRowid, m.z, nKey, pKey);
danielk1977d8123362004-06-12 09:25:12 +00002270 sqlite3VdbeMemRelease(&m);
danielk1977183f9f72004-05-13 05:20:26 +00002271 return SQLITE_OK;
2272}
danielk1977b28af712004-06-21 06:50:26 +00002273
2274/*
2275** This routine sets the value to be returned by subsequent calls to
2276** sqlite3_changes() on the database handle 'db'.
2277*/
2278void sqlite3VdbeSetChanges(sqlite3 *db, int nChange){
drhb21c8cd2007-08-21 19:33:56 +00002279 assert( sqlite3_mutex_held(db->mutex) );
danielk1977b28af712004-06-21 06:50:26 +00002280 db->nChange = nChange;
2281 db->nTotalChange += nChange;
2282}
2283
2284/*
2285** Set a flag in the vdbe to update the change counter when it is finalised
2286** or reset.
2287*/
drh4794f732004-11-05 17:17:50 +00002288void sqlite3VdbeCountChanges(Vdbe *v){
2289 v->changeCntOn = 1;
danielk1977b28af712004-06-21 06:50:26 +00002290}
drhd89bd002005-01-22 03:03:54 +00002291
2292/*
2293** Mark every prepared statement associated with a database connection
2294** as expired.
2295**
2296** An expired statement means that recompilation of the statement is
2297** recommend. Statements expire when things happen that make their
2298** programs obsolete. Removing user-defined functions or collating
2299** sequences, or changing an authorization function are the types of
2300** things that make prepared statements obsolete.
2301*/
2302void sqlite3ExpirePreparedStatements(sqlite3 *db){
2303 Vdbe *p;
2304 for(p = db->pVdbe; p; p=p->pNext){
2305 p->expired = 1;
2306 }
2307}
danielk1977aee18ef2005-03-09 12:26:50 +00002308
2309/*
2310** Return the database associated with the Vdbe.
2311*/
2312sqlite3 *sqlite3VdbeDb(Vdbe *v){
2313 return v->db;
2314}