blob: 95d4818843108e305878cd530f1a49165633896e [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/*
drh76ff3a02004-09-24 22:32:30 +0000236** Loop through the program looking for P2 values that are negative.
237** Each such value is a label. Resolve the label by setting the P2
238** value to its correct non-zero value.
239**
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**
246** The integer *pMaxStack is set to the maximum number of vdbe stack
247** entries that static analysis reveals this program might need.
drh38449902005-06-07 01:43:41 +0000248**
249** This routine also does the following optimization: It scans for
drh77658e22007-12-04 16:54:52 +0000250** instructions that might cause a statement rollback. Such instructions
251** are:
252**
253** * OP_Halt with P1=SQLITE_CONSTRAINT and P2=OE_Abort.
254** * OP_Destroy
255** * OP_VUpdate
256** * OP_VRename
257**
258** If no such instruction is found, then every Statement instruction
259** is changed to a Noop. In this way, we avoid creating the statement
260** journal file unnecessarily.
drh76ff3a02004-09-24 22:32:30 +0000261*/
danielk1977bc04f852005-03-29 08:26:13 +0000262static void resolveP2Values(Vdbe *p, int *pMaxFuncArgs, int *pMaxStack){
drh76ff3a02004-09-24 22:32:30 +0000263 int i;
danielk1977bc04f852005-03-29 08:26:13 +0000264 int nMaxArgs = 0;
265 int nMaxStack = p->nOp;
drh76ff3a02004-09-24 22:32:30 +0000266 Op *pOp;
267 int *aLabel = p->aLabel;
drh38449902005-06-07 01:43:41 +0000268 int doesStatementRollback = 0;
269 int hasStatementBegin = 0;
drh76ff3a02004-09-24 22:32:30 +0000270 for(pOp=p->aOp, i=p->nOp-1; i>=0; i--, pOp++){
danielk1977634f2982005-03-28 08:44:07 +0000271 u8 opcode = pOp->opcode;
272
drh98757152008-01-09 23:04:12 +0000273 if( opcode==OP_Function ){
274 if( pOp->p5>nMaxArgs ) nMaxArgs = pOp->p5;
275 }else if( opcode==OP_AggStep
danielk1977399918f2006-06-14 13:03:23 +0000276#ifndef SQLITE_OMIT_VIRTUALTABLE
danielk19774b2688a2006-06-20 11:01:07 +0000277 || opcode==OP_VUpdate
danielk1977399918f2006-06-14 13:03:23 +0000278#endif
279 ){
danielk1977bc04f852005-03-29 08:26:13 +0000280 if( pOp->p2>nMaxArgs ) nMaxArgs = pOp->p2;
danielk1977182c4ba2007-06-27 15:53:34 +0000281 }
282 if( opcode==OP_Halt ){
drh38449902005-06-07 01:43:41 +0000283 if( pOp->p1==SQLITE_CONSTRAINT && pOp->p2==OE_Abort ){
284 doesStatementRollback = 1;
285 }
drh38449902005-06-07 01:43:41 +0000286 }else if( opcode==OP_Statement ){
287 hasStatementBegin = 1;
drh77658e22007-12-04 16:54:52 +0000288 }else if( opcode==OP_Destroy ){
289 doesStatementRollback = 1;
danielk1977182c4ba2007-06-27 15:53:34 +0000290#ifndef SQLITE_OMIT_VIRTUALTABLE
291 }else if( opcode==OP_VUpdate || opcode==OP_VRename ){
292 doesStatementRollback = 1;
drh4be8b512006-06-13 23:51:34 +0000293 }else if( opcode==OP_VFilter ){
294 int n;
295 assert( p->nOp - i >= 3 );
drh4c583122008-01-04 22:01:03 +0000296 assert( pOp[-1].opcode==OP_Integer );
danielk19776dbee812008-01-03 18:39:41 +0000297 n = pOp[-1].p1;
drh4be8b512006-06-13 23:51:34 +0000298 if( n>nMaxArgs ) nMaxArgs = n;
danielk1977182c4ba2007-06-27 15:53:34 +0000299#endif
danielk1977bc04f852005-03-29 08:26:13 +0000300 }
drh3a40f692008-01-04 16:50:09 +0000301 if( !sqlite3VdbeOpcodeHasProperty(opcode, OPFLG_PUSH) ){
danielk1977bc04f852005-03-29 08:26:13 +0000302 nMaxStack--;
danielk1977634f2982005-03-28 08:44:07 +0000303 }
304
drhd2981512008-01-04 19:33:49 +0000305 if( sqlite3VdbeOpcodeHasProperty(opcode, OPFLG_JUMP) && pOp->p2<0 ){
306 assert( -1-pOp->p2<p->nLabel );
307 pOp->p2 = aLabel[-1-pOp->p2];
308 }
drh76ff3a02004-09-24 22:32:30 +0000309 }
drh17435752007-08-16 04:30:38 +0000310 sqlite3_free(p->aLabel);
drh76ff3a02004-09-24 22:32:30 +0000311 p->aLabel = 0;
danielk1977bc04f852005-03-29 08:26:13 +0000312
313 *pMaxFuncArgs = nMaxArgs;
314 *pMaxStack = nMaxStack;
drh38449902005-06-07 01:43:41 +0000315
316 /* If we never rollback a statement transaction, then statement
317 ** transactions are not needed. So change every OP_Statement
drh66560ad2006-01-06 14:32:19 +0000318 ** opcode into an OP_Noop. This avoid a call to sqlite3OsOpenExclusive()
drh38449902005-06-07 01:43:41 +0000319 ** which can be expensive on some platforms.
320 */
321 if( hasStatementBegin && !doesStatementRollback ){
322 for(pOp=p->aOp, i=p->nOp-1; i>=0; i--, pOp++){
323 if( pOp->opcode==OP_Statement ){
324 pOp->opcode = OP_Noop;
325 }
326 }
327 }
drh76ff3a02004-09-24 22:32:30 +0000328}
329
330/*
drh9a324642003-09-06 20:12:01 +0000331** Return the address of the next instruction to be inserted.
332*/
danielk19774adee202004-05-08 08:23:19 +0000333int sqlite3VdbeCurrentAddr(Vdbe *p){
drh9a324642003-09-06 20:12:01 +0000334 assert( p->magic==VDBE_MAGIC_INIT );
335 return p->nOp;
336}
337
338/*
339** Add a whole list of operations to the operation stack. Return the
340** address of the first operation added.
341*/
danielk19774adee202004-05-08 08:23:19 +0000342int sqlite3VdbeAddOpList(Vdbe *p, int nOp, VdbeOpList const *aOp){
drh9a324642003-09-06 20:12:01 +0000343 int addr;
344 assert( p->magic==VDBE_MAGIC_INIT );
drha4e5d582007-10-20 15:41:57 +0000345 if( p->nOp + nOp > p->nOpAlloc ){
346 resizeOpArray(p, p->nOp*2 + nOp);
347 }
drh17435752007-08-16 04:30:38 +0000348 if( p->db->mallocFailed ){
drh76ff3a02004-09-24 22:32:30 +0000349 return 0;
drh9a324642003-09-06 20:12:01 +0000350 }
351 addr = p->nOp;
352 if( nOp>0 ){
353 int i;
drh905793e2004-02-21 13:31:09 +0000354 VdbeOpList const *pIn = aOp;
355 for(i=0; i<nOp; i++, pIn++){
356 int p2 = pIn->p2;
357 VdbeOp *pOut = &p->aOp[i+addr];
358 pOut->opcode = pIn->opcode;
359 pOut->p1 = pIn->p1;
drh8558cde2008-01-05 05:20:10 +0000360 if( p2<0 && sqlite3VdbeOpcodeHasProperty(pOut->opcode, OPFLG_JUMP) ){
361 pOut->p2 = addr + ADDR(p2);
362 }else{
363 pOut->p2 = p2;
364 }
drh24003452008-01-03 01:28:59 +0000365 pOut->p3 = pIn->p3;
366 pOut->p4type = P4_NOTUSED;
367 pOut->p4.p = 0;
368 pOut->p5 = 0;
danielk19778b60e0f2005-01-12 09:10:39 +0000369#ifdef SQLITE_DEBUG
danielk1977132872b2004-05-10 10:37:18 +0000370 if( sqlite3_vdbe_addop_trace ){
danielk19774adee202004-05-08 08:23:19 +0000371 sqlite3VdbePrintOp(0, i+addr, &p->aOp[i+addr]);
drh9a324642003-09-06 20:12:01 +0000372 }
373#endif
374 }
375 p->nOp += nOp;
376 }
377 return addr;
378}
379
380/*
381** Change the value of the P1 operand for a specific instruction.
382** This routine is useful when a large program is loaded from a
danielk19774adee202004-05-08 08:23:19 +0000383** static array using sqlite3VdbeAddOpList but we want to make a
drh9a324642003-09-06 20:12:01 +0000384** few minor changes to the program.
385*/
danielk19774adee202004-05-08 08:23:19 +0000386void sqlite3VdbeChangeP1(Vdbe *p, int addr, int val){
drh8aa34ae2006-03-13 12:54:09 +0000387 assert( p==0 || p->magic==VDBE_MAGIC_INIT );
drh9a324642003-09-06 20:12:01 +0000388 if( p && addr>=0 && p->nOp>addr && p->aOp ){
389 p->aOp[addr].p1 = val;
390 }
391}
392
393/*
394** Change the value of the P2 operand for a specific instruction.
395** This routine is useful for setting a jump destination.
396*/
danielk19774adee202004-05-08 08:23:19 +0000397void sqlite3VdbeChangeP2(Vdbe *p, int addr, int val){
drh8aa34ae2006-03-13 12:54:09 +0000398 assert( p==0 || p->magic==VDBE_MAGIC_INIT );
drh9a324642003-09-06 20:12:01 +0000399 if( p && addr>=0 && p->nOp>addr && p->aOp ){
400 p->aOp[addr].p2 = val;
401 }
402}
403
drhd654be82005-09-20 17:42:23 +0000404/*
danielk19771f4aa332008-01-03 09:51:55 +0000405** Change the value of the P3 operand for a specific instruction.
danielk1977207872a2008-01-03 07:54:23 +0000406*/
407void sqlite3VdbeChangeP3(Vdbe *p, int addr, int val){
408 assert( p==0 || p->magic==VDBE_MAGIC_INIT );
409 if( p && addr>=0 && p->nOp>addr && p->aOp ){
410 p->aOp[addr].p3 = val;
411 }
412}
413
414/*
drh35573352008-01-08 23:54:25 +0000415** Change the value of the P5 operand for the most recently
416** added operation.
danielk19771f4aa332008-01-03 09:51:55 +0000417*/
drh35573352008-01-08 23:54:25 +0000418void sqlite3VdbeChangeP5(Vdbe *p, u8 val){
danielk19771f4aa332008-01-03 09:51:55 +0000419 assert( p==0 || p->magic==VDBE_MAGIC_INIT );
drh35573352008-01-08 23:54:25 +0000420 if( p && p->aOp ){
421 assert( p->nOp>0 );
422 p->aOp[p->nOp-1].p5 = val;
danielk19771f4aa332008-01-03 09:51:55 +0000423 }
424}
425
426/*
drhf8875402006-03-17 13:56:34 +0000427** Change the P2 operand of instruction addr so that it points to
drhd654be82005-09-20 17:42:23 +0000428** the address of the next instruction to be coded.
429*/
430void sqlite3VdbeJumpHere(Vdbe *p, int addr){
431 sqlite3VdbeChangeP2(p, addr, p->nOp);
432}
drhb38ad992005-09-16 00:27:01 +0000433
drhb7f6f682006-07-08 17:06:43 +0000434
435/*
436** If the input FuncDef structure is ephemeral, then free it. If
437** the FuncDef is not ephermal, then do nothing.
438*/
439static void freeEphemeralFunction(FuncDef *pDef){
440 if( pDef && (pDef->flags & SQLITE_FUNC_EPHEM)!=0 ){
drh17435752007-08-16 04:30:38 +0000441 sqlite3_free(pDef);
drhb7f6f682006-07-08 17:06:43 +0000442 }
443}
444
drhb38ad992005-09-16 00:27:01 +0000445/*
drh66a51672008-01-03 00:01:23 +0000446** Delete a P4 value if necessary.
drhb38ad992005-09-16 00:27:01 +0000447*/
drh66a51672008-01-03 00:01:23 +0000448static void freeP4(int p4type, void *p3){
drhb38ad992005-09-16 00:27:01 +0000449 if( p3 ){
drh66a51672008-01-03 00:01:23 +0000450 switch( p4type ){
451 case P4_REAL:
452 case P4_INT64:
453 case P4_MPRINTF:
454 case P4_DYNAMIC:
455 case P4_KEYINFO:
456 case P4_KEYINFO_HANDOFF: {
drh17435752007-08-16 04:30:38 +0000457 sqlite3_free(p3);
drhac1733d2005-09-17 17:58:22 +0000458 break;
459 }
drh66a51672008-01-03 00:01:23 +0000460 case P4_VDBEFUNC: {
drhac1733d2005-09-17 17:58:22 +0000461 VdbeFunc *pVdbeFunc = (VdbeFunc *)p3;
drhb7f6f682006-07-08 17:06:43 +0000462 freeEphemeralFunction(pVdbeFunc->pFunc);
drhac1733d2005-09-17 17:58:22 +0000463 sqlite3VdbeDeleteAuxData(pVdbeFunc, 0);
drh17435752007-08-16 04:30:38 +0000464 sqlite3_free(pVdbeFunc);
drhac1733d2005-09-17 17:58:22 +0000465 break;
466 }
drh66a51672008-01-03 00:01:23 +0000467 case P4_FUNCDEF: {
drhb7f6f682006-07-08 17:06:43 +0000468 freeEphemeralFunction((FuncDef*)p3);
469 break;
470 }
drh66a51672008-01-03 00:01:23 +0000471 case P4_MEM: {
drhac1733d2005-09-17 17:58:22 +0000472 sqlite3ValueFree((sqlite3_value*)p3);
473 break;
474 }
drhb38ad992005-09-16 00:27:01 +0000475 }
476 }
477}
478
479
drh9a324642003-09-06 20:12:01 +0000480/*
drhf8875402006-03-17 13:56:34 +0000481** Change N opcodes starting at addr to No-ops.
482*/
483void sqlite3VdbeChangeToNoop(Vdbe *p, int addr, int N){
danielk197792d4d7a2007-05-04 12:05:56 +0000484 if( p && p->aOp ){
485 VdbeOp *pOp = &p->aOp[addr];
486 while( N-- ){
drh66a51672008-01-03 00:01:23 +0000487 freeP4(pOp->p4type, pOp->p4.p);
danielk197792d4d7a2007-05-04 12:05:56 +0000488 memset(pOp, 0, sizeof(pOp[0]));
489 pOp->opcode = OP_Noop;
490 pOp++;
491 }
drhf8875402006-03-17 13:56:34 +0000492 }
493}
494
495/*
drh66a51672008-01-03 00:01:23 +0000496** Change the value of the P4 operand for a specific instruction.
drh9a324642003-09-06 20:12:01 +0000497** This routine is useful when a large program is loaded from a
danielk19774adee202004-05-08 08:23:19 +0000498** static array using sqlite3VdbeAddOpList but we want to make a
drh9a324642003-09-06 20:12:01 +0000499** few minor changes to the program.
500**
drh66a51672008-01-03 00:01:23 +0000501** If n>=0 then the P4 operand is dynamic, meaning that a copy of
drh17435752007-08-16 04:30:38 +0000502** the string is made into memory obtained from sqlite3_malloc().
drh66a51672008-01-03 00:01:23 +0000503** A value of n==0 means copy bytes of zP4 up to and including the
504** first null byte. If n>0 then copy n+1 bytes of zP4.
drh9a324642003-09-06 20:12:01 +0000505**
drh66a51672008-01-03 00:01:23 +0000506** If n==P4_KEYINFO it means that zP4 is a pointer to a KeyInfo structure.
danielk19771f55c052005-05-19 08:42:59 +0000507** A copy is made of the KeyInfo structure into memory obtained from
drh17435752007-08-16 04:30:38 +0000508** sqlite3_malloc, to be freed when the Vdbe is finalized.
drh66a51672008-01-03 00:01:23 +0000509** n==P4_KEYINFO_HANDOFF indicates that zP4 points to a KeyInfo structure
drh17435752007-08-16 04:30:38 +0000510** stored in memory that the caller has obtained from sqlite3_malloc. The
danielk19771f55c052005-05-19 08:42:59 +0000511** caller should not free the allocation, it will be freed when the Vdbe is
512** finalized.
513**
drh66a51672008-01-03 00:01:23 +0000514** Other values of n (P4_STATIC, P4_COLLSEQ etc.) indicate that zP4 points
danielk19771f55c052005-05-19 08:42:59 +0000515** to a string or structure that is guaranteed to exist for the lifetime of
516** the Vdbe. In these cases we can just copy the pointer.
drh9a324642003-09-06 20:12:01 +0000517**
drh66a51672008-01-03 00:01:23 +0000518** If addr<0 then change P4 on the most recently inserted instruction.
drh9a324642003-09-06 20:12:01 +0000519*/
drh66a51672008-01-03 00:01:23 +0000520void sqlite3VdbeChangeP4(Vdbe *p, int addr, const char *zP4, int n){
drh9a324642003-09-06 20:12:01 +0000521 Op *pOp;
drh8aa34ae2006-03-13 12:54:09 +0000522 assert( p==0 || p->magic==VDBE_MAGIC_INIT );
drh17435752007-08-16 04:30:38 +0000523 if( p==0 || p->aOp==0 || p->db->mallocFailed ){
drh66a51672008-01-03 00:01:23 +0000524 if (n != P4_KEYINFO) {
525 freeP4(n, (void*)*(char**)&zP4);
danielk1977261919c2005-12-06 12:52:59 +0000526 }
danielk1977d5d56522005-03-16 12:15:20 +0000527 return;
528 }
drh9a324642003-09-06 20:12:01 +0000529 if( addr<0 || addr>=p->nOp ){
530 addr = p->nOp - 1;
531 if( addr<0 ) return;
532 }
533 pOp = &p->aOp[addr];
drh66a51672008-01-03 00:01:23 +0000534 freeP4(pOp->p4type, pOp->p4.p);
535 pOp->p4.p = 0;
drh98757152008-01-09 23:04:12 +0000536 if( n==P4_INT32 ){
537 pOp->p4.i = (int)zP4;
538 pOp->p4type = n;
539 }else if( zP4==0 ){
drh66a51672008-01-03 00:01:23 +0000540 pOp->p4.p = 0;
541 pOp->p4type = P4_NOTUSED;
542 }else if( n==P4_KEYINFO ){
drhd3d39e92004-05-20 22:16:29 +0000543 KeyInfo *pKeyInfo;
544 int nField, nByte;
drh4db38a72005-09-01 12:16:28 +0000545
drh66a51672008-01-03 00:01:23 +0000546 nField = ((KeyInfo*)zP4)->nField;
drhfdd6e852005-12-16 01:06:16 +0000547 nByte = sizeof(*pKeyInfo) + (nField-1)*sizeof(pKeyInfo->aColl[0]) + nField;
drh17435752007-08-16 04:30:38 +0000548 pKeyInfo = sqlite3_malloc( nByte );
danielk19772dca4ac2008-01-03 11:50:29 +0000549 pOp->p4.pKeyInfo = pKeyInfo;
drhd3d39e92004-05-20 22:16:29 +0000550 if( pKeyInfo ){
danielk1977bab45c62006-01-16 15:14:27 +0000551 unsigned char *aSortOrder;
drh66a51672008-01-03 00:01:23 +0000552 memcpy(pKeyInfo, zP4, nByte);
drhfdd6e852005-12-16 01:06:16 +0000553 aSortOrder = pKeyInfo->aSortOrder;
554 if( aSortOrder ){
danielk1977bab45c62006-01-16 15:14:27 +0000555 pKeyInfo->aSortOrder = (unsigned char*)&pKeyInfo->aColl[nField];
drhfdd6e852005-12-16 01:06:16 +0000556 memcpy(pKeyInfo->aSortOrder, aSortOrder, nField);
557 }
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;
drhd4e70eb2008-01-02 00:34:36 +0000666 if( pMem->flags & MEM_Str ){
drh66a51672008-01-03 00:01:23 +0000667 zP4 = pMem->z;
drhd4e70eb2008-01-02 00:34:36 +0000668 }else if( pMem->flags & MEM_Int ){
669 sqlite3_snprintf(nTemp, zTemp, "%lld", pMem->u.i);
670 }else if( pMem->flags & MEM_Real ){
671 sqlite3_snprintf(nTemp, zTemp, "%.16g", pMem->r);
672 }else if( pMem->flags & MEM_Null ){
673 sqlite3_snprintf(nTemp, zTemp, "NULL");
674 }
drh598f1342007-10-23 15:39:45 +0000675 break;
676 }
drha967e882006-06-13 01:04:52 +0000677#ifndef SQLITE_OMIT_VIRTUALTABLE
drh66a51672008-01-03 00:01:23 +0000678 case P4_VTAB: {
danielk19772dca4ac2008-01-03 11:50:29 +0000679 sqlite3_vtab *pVtab = pOp->p4.pVtab;
drh19146192006-06-26 19:10:32 +0000680 sqlite3_snprintf(nTemp, zTemp, "vtab:%p:%p", pVtab, pVtab->pModule);
drha967e882006-06-13 01:04:52 +0000681 break;
682 }
683#endif
drhd3d39e92004-05-20 22:16:29 +0000684 default: {
danielk19772dca4ac2008-01-03 11:50:29 +0000685 zP4 = pOp->p4.z;
drh949f9cd2008-01-12 21:35:57 +0000686 if( zP4==0 ){
drh66a51672008-01-03 00:01:23 +0000687 zP4 = zTemp;
drhd4e70eb2008-01-02 00:34:36 +0000688 zTemp[0] = 0;
drhd3d39e92004-05-20 22:16:29 +0000689 }
690 }
691 }
drh66a51672008-01-03 00:01:23 +0000692 assert( zP4!=0 );
drh66a51672008-01-03 00:01:23 +0000693 return zP4;
drhd3d39e92004-05-20 22:16:29 +0000694}
drhb7f91642004-10-31 02:22:47 +0000695#endif
drhd3d39e92004-05-20 22:16:29 +0000696
drh900b31e2007-08-28 02:27:51 +0000697/*
drhd0679ed2007-08-28 22:24:34 +0000698** Declare to the Vdbe that the BTree object at db->aDb[i] is used.
699**
drh900b31e2007-08-28 02:27:51 +0000700*/
drhfb982642007-08-30 01:19:59 +0000701void sqlite3VdbeUsesBtree(Vdbe *p, int i){
702 int mask;
drhd0679ed2007-08-28 22:24:34 +0000703 assert( i>=0 && i<p->db->nDb );
704 assert( i<sizeof(p->btreeMask)*8 );
drhfb982642007-08-30 01:19:59 +0000705 mask = 1<<i;
706 if( (p->btreeMask & mask)==0 ){
707 p->btreeMask |= mask;
708 sqlite3BtreeMutexArrayInsert(&p->aMutex, p->db->aDb[i].pBt);
709 }
drh900b31e2007-08-28 02:27:51 +0000710}
711
drhd3d39e92004-05-20 22:16:29 +0000712
danielk19778b60e0f2005-01-12 09:10:39 +0000713#if defined(VDBE_PROFILE) || defined(SQLITE_DEBUG)
drh9a324642003-09-06 20:12:01 +0000714/*
715** Print a single opcode. This routine is used for debugging only.
716*/
danielk19774adee202004-05-08 08:23:19 +0000717void sqlite3VdbePrintOp(FILE *pOut, int pc, Op *pOp){
drh66a51672008-01-03 00:01:23 +0000718 char *zP4;
drhd3d39e92004-05-20 22:16:29 +0000719 char zPtr[50];
drh1db639c2008-01-17 02:36:28 +0000720 static const char *zFormat1 = "%4d %-13s %4d %4d %4d %-4s %.2X %s\n";
drh9a324642003-09-06 20:12:01 +0000721 if( pOut==0 ) pOut = stdout;
drh66a51672008-01-03 00:01:23 +0000722 zP4 = displayP4(pOp, zPtr, sizeof(zPtr));
danielk197711641c12008-01-03 08:18:30 +0000723 fprintf(pOut, zFormat1, pc,
drh1db639c2008-01-17 02:36:28 +0000724 sqlite3OpcodeName(pOp->opcode), pOp->p1, pOp->p2, pOp->p3, zP4, pOp->p5,
725#ifdef SQLITE_DEBUG
726 pOp->zComment ? pOp->zComment : ""
727#else
728 ""
729#endif
730 );
drh9a324642003-09-06 20:12:01 +0000731 fflush(pOut);
732}
733#endif
734
735/*
drh76ff3a02004-09-24 22:32:30 +0000736** Release an array of N Mem elements
737*/
738static void releaseMemArray(Mem *p, int N){
739 if( p ){
740 while( N-->0 ){
drhb21c8cd2007-08-21 19:33:56 +0000741 assert( N<2 || p[0].db==p[1].db );
drh76ff3a02004-09-24 22:32:30 +0000742 sqlite3VdbeMemRelease(p++);
743 }
744 }
745}
746
drhb7f91642004-10-31 02:22:47 +0000747#ifndef SQLITE_OMIT_EXPLAIN
drh76ff3a02004-09-24 22:32:30 +0000748/*
drh9a324642003-09-06 20:12:01 +0000749** Give a listing of the program in the virtual machine.
750**
danielk19774adee202004-05-08 08:23:19 +0000751** The interface is the same as sqlite3VdbeExec(). But instead of
drh9a324642003-09-06 20:12:01 +0000752** running the code, it invokes the callback once for each instruction.
753** This feature is used to implement "EXPLAIN".
754*/
danielk19774adee202004-05-08 08:23:19 +0000755int sqlite3VdbeList(
drh9a324642003-09-06 20:12:01 +0000756 Vdbe *p /* The VDBE */
757){
drh9bb575f2004-09-06 17:24:11 +0000758 sqlite3 *db = p->db;
drh9a324642003-09-06 20:12:01 +0000759 int i;
drh826fb5a2004-02-14 23:59:57 +0000760 int rc = SQLITE_OK;
drh9a324642003-09-06 20:12:01 +0000761
drh9a324642003-09-06 20:12:01 +0000762 assert( p->explain );
drhc5cdca62005-01-11 16:54:14 +0000763 if( p->magic!=VDBE_MAGIC_RUN ) return SQLITE_MISUSE;
764 assert( db->magic==SQLITE_MAGIC_BUSY );
765 assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY );
danielk197718f41892004-05-22 07:27:46 +0000766
767 /* Even though this opcode does not put dynamic strings onto the
768 ** the stack, they may become dynamic if the user calls
drh4f26d6c2004-05-26 23:25:30 +0000769 ** sqlite3_column_text16(), causing a translation to UTF-16 encoding.
danielk197718f41892004-05-22 07:27:46 +0000770 */
drhd4e70eb2008-01-02 00:34:36 +0000771 if( p->pResultSet ){
772 releaseMemArray(p->pResultSet, 5);
773 p->pResultSet = 0;
danielk197718f41892004-05-22 07:27:46 +0000774 }
danielk197718f41892004-05-22 07:27:46 +0000775
drhecc92422005-09-10 16:46:12 +0000776 do{
777 i = p->pc++;
778 }while( i<p->nOp && p->explain==2 && p->aOp[i].opcode!=OP_Explain );
drh826fb5a2004-02-14 23:59:57 +0000779 if( i>=p->nOp ){
780 p->rc = SQLITE_OK;
781 rc = SQLITE_DONE;
drh881feaa2006-07-26 01:39:30 +0000782 }else if( db->u1.isInterrupted ){
drhc5cdca62005-01-11 16:54:14 +0000783 p->rc = SQLITE_INTERRUPT;
drh826fb5a2004-02-14 23:59:57 +0000784 rc = SQLITE_ERROR;
danielk1977f20b21c2004-05-31 23:56:42 +0000785 sqlite3SetString(&p->zErrMsg, sqlite3ErrStr(p->rc), (char*)0);
drh826fb5a2004-02-14 23:59:57 +0000786 }else{
drhd3d39e92004-05-20 22:16:29 +0000787 Op *pOp = &p->aOp[i];
drhd4e70eb2008-01-02 00:34:36 +0000788 Mem *pMem = p->pResultSet = p->aStack;
danielk19770d78bae2008-01-03 07:09:48 +0000789 if( p->explain==1 ){
790 pMem->flags = MEM_Int;
791 pMem->type = SQLITE_INTEGER;
792 pMem->u.i = i; /* Program counter */
793 pMem++;
794
795 pMem->flags = MEM_Static|MEM_Str|MEM_Term;
796 pMem->z = (char*)sqlite3OpcodeName(pOp->opcode); /* Opcode */
797 assert( pMem->z!=0 );
798 pMem->n = strlen(pMem->z);
799 pMem->type = SQLITE_TEXT;
800 pMem->enc = SQLITE_UTF8;
801 pMem++;
802 }
drheb2e1762004-05-27 01:53:56 +0000803
804 pMem->flags = MEM_Int;
drh3c024d62007-03-30 11:23:45 +0000805 pMem->u.i = pOp->p1; /* P1 */
drh9c054832004-05-31 18:51:57 +0000806 pMem->type = SQLITE_INTEGER;
drheb2e1762004-05-27 01:53:56 +0000807 pMem++;
808
809 pMem->flags = MEM_Int;
drh3c024d62007-03-30 11:23:45 +0000810 pMem->u.i = pOp->p2; /* P2 */
drh9c054832004-05-31 18:51:57 +0000811 pMem->type = SQLITE_INTEGER;
drheb2e1762004-05-27 01:53:56 +0000812 pMem++;
813
danielk19770d78bae2008-01-03 07:09:48 +0000814 if( p->explain==1 ){
815 pMem->flags = MEM_Int;
816 pMem->u.i = pOp->p3; /* P3 */
817 pMem->type = SQLITE_INTEGER;
818 pMem++;
819 }
820
drh66a51672008-01-03 00:01:23 +0000821 pMem->flags = MEM_Ephem|MEM_Str|MEM_Term; /* P4 */
822 pMem->z = displayP4(pOp, pMem->zShort, sizeof(pMem->zShort));
drhbdd88bd2006-06-15 13:22:22 +0000823 assert( pMem->z!=0 );
drhb8067982006-03-03 21:38:03 +0000824 pMem->n = strlen(pMem->z);
drh9c054832004-05-31 18:51:57 +0000825 pMem->type = SQLITE_TEXT;
danielk1977dc8453f2004-06-12 00:42:34 +0000826 pMem->enc = SQLITE_UTF8;
danielk19770d78bae2008-01-03 07:09:48 +0000827 pMem++;
drheb2e1762004-05-27 01:53:56 +0000828
danielk19770d78bae2008-01-03 07:09:48 +0000829 if( p->explain==1 ){
830 pMem->flags = MEM_Str|MEM_Term|MEM_Short;
831 pMem->n = sprintf(pMem->zShort, "%.2x", pOp->p5); /* P5 */
832 pMem->z = pMem->zShort;
833 pMem->type = SQLITE_TEXT;
834 pMem->enc = SQLITE_UTF8;
835 pMem++;
836
837 pMem->flags = MEM_Null; /* Comment */
drhaa9b8962008-01-08 02:57:55 +0000838#ifdef SQLITE_DEBUG
danielk19770d78bae2008-01-03 07:09:48 +0000839 if( pOp->zComment ){
840 pMem->flags = MEM_Str|MEM_Term;
841 pMem->z = pOp->zComment;
842 pMem->n = strlen(pMem->z);
843 pMem->enc = SQLITE_UTF8;
844 }
drhaa9b8962008-01-08 02:57:55 +0000845#endif
danielk19770d78bae2008-01-03 07:09:48 +0000846 }
847
848 p->nResColumn = 8 - 5*(p->explain-1);
drheb2e1762004-05-27 01:53:56 +0000849 p->pTos = pMem;
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
drh9a324642003-09-06 20:12:01 +0000938 /* No instruction ever pushes more than a single element onto the
939 ** stack. And the stack never grows on successive executions of the
940 ** same loop. So the total number of instructions is an upper bound
drh38449902005-06-07 01:43:41 +0000941 ** on the maximum stack depth required. (Added later:) The
942 ** resolveP2Values() call computes a tighter upper bound on the
943 ** stack size.
drh9a324642003-09-06 20:12:01 +0000944 **
945 ** Allocation all the stack space we will ever need.
946 */
drh82a48512003-09-06 22:45:20 +0000947 if( p->aStack==0 ){
danielk1977634f2982005-03-28 08:44:07 +0000948 int nArg; /* Maximum number of args passed to a user function. */
danielk1977bc04f852005-03-29 08:26:13 +0000949 int nStack; /* Maximum number of stack entries required */
950 resolveP2Values(p, &nArg, &nStack);
danielk1977634f2982005-03-28 08:44:07 +0000951 resizeOpArray(p, p->nOp);
drh82a48512003-09-06 22:45:20 +0000952 assert( nVar>=0 );
danielk1977bc04f852005-03-29 08:26:13 +0000953 assert( nStack<p->nOp );
drh0f7eb612006-08-08 13:51:43 +0000954 if( isExplain ){
drh0a07c102008-01-03 18:03:08 +0000955 nStack = 16;
drh0f7eb612006-08-08 13:51:43 +0000956 }
danielk19771e536952007-08-16 10:09:01 +0000957 p->aStack = sqlite3DbMallocZero(db,
danielk1977bc04f852005-03-29 08:26:13 +0000958 nStack*sizeof(p->aStack[0]) /* aStack */
danielk1977634f2982005-03-28 08:44:07 +0000959 + nArg*sizeof(Mem*) /* apArg */
drh86f43302004-10-05 17:37:36 +0000960 + nVar*sizeof(Mem) /* aVar */
961 + nVar*sizeof(char*) /* azVar */
962 + nMem*sizeof(Mem) /* aMem */
drh0a07c102008-01-03 18:03:08 +0000963 + nCursor*sizeof(Cursor*) + 1 /* apCsr */
drh82a48512003-09-06 22:45:20 +0000964 );
drh17435752007-08-16 04:30:38 +0000965 if( !db->mallocFailed ){
drh0a07c102008-01-03 18:03:08 +0000966 p->aMem = &p->aStack[nStack-1]; /* aMem[] goes from 1..nMem */
967 p->nMem = nMem; /* not from 0..nMem-1 */
968 p->aVar = &p->aMem[nMem+1];
drh86f43302004-10-05 17:37:36 +0000969 p->nVar = nVar;
970 p->okVar = 0;
971 p->apArg = (Mem**)&p->aVar[nVar];
danielk1977634f2982005-03-28 08:44:07 +0000972 p->azVar = (char**)&p->apArg[nArg];
drh86f43302004-10-05 17:37:36 +0000973 p->apCsr = (Cursor**)&p->azVar[nVar];
drh290c1942004-08-21 17:54:45 +0000974 p->nCursor = nCursor;
975 for(n=0; n<nVar; n++){
976 p->aVar[n].flags = MEM_Null;
drhb21c8cd2007-08-21 19:33:56 +0000977 p->aVar[n].db = db;
978 }
979 for(n=0; n<nStack; n++){
980 p->aStack[n].db = db;
drh290c1942004-08-21 17:54:45 +0000981 }
danielk197754db47e2004-05-19 10:36:43 +0000982 }
drh82a48512003-09-06 22:45:20 +0000983 }
drh0a07c102008-01-03 18:03:08 +0000984 for(n=1; n<=p->nMem; n++){
danielk1977b3bce662005-01-29 08:32:43 +0000985 p->aMem[n].flags = MEM_Null;
drhb21c8cd2007-08-21 19:33:56 +0000986 p->aMem[n].db = db;
danielk1977b3bce662005-01-29 08:32:43 +0000987 }
drh9a324642003-09-06 20:12:01 +0000988
drh6810ce62004-01-31 19:22:56 +0000989 p->pTos = &p->aStack[-1];
danielk19771d850a72004-05-31 08:26:49 +0000990 p->pc = -1;
drh9a324642003-09-06 20:12:01 +0000991 p->rc = SQLITE_OK;
992 p->uniqueCnt = 0;
993 p->returnDepth = 0;
994 p->errorAction = OE_Abort;
drh9a324642003-09-06 20:12:01 +0000995 p->popStack = 0;
996 p->explain |= isExplain;
997 p->magic = VDBE_MAGIC_RUN;
danielk1977b28af712004-06-21 06:50:26 +0000998 p->nChange = 0;
drh76873ab2006-01-07 18:48:26 +0000999 p->cacheCtr = 1;
drhd946db02005-12-29 19:23:06 +00001000 p->minWriteFileFormat = 255;
danielk1977182c4ba2007-06-27 15:53:34 +00001001 p->openedStatement = 0;
drh9a324642003-09-06 20:12:01 +00001002#ifdef VDBE_PROFILE
drhcf64d8b2003-12-31 17:57:10 +00001003 {
1004 int i;
1005 for(i=0; i<p->nOp; i++){
1006 p->aOp[i].cnt = 0;
1007 p->aOp[i].cycles = 0;
1008 }
drh9a324642003-09-06 20:12:01 +00001009 }
1010#endif
1011}
1012
drh9a324642003-09-06 20:12:01 +00001013/*
drhff0587c2007-08-29 17:43:19 +00001014** Close a VDBE cursor and release all the resources that cursor happens
drh9a324642003-09-06 20:12:01 +00001015** to hold.
1016*/
danielk1977be718892006-06-23 08:05:19 +00001017void sqlite3VdbeFreeCursor(Vdbe *p, Cursor *pCx){
drh4774b132004-06-12 20:12:51 +00001018 if( pCx==0 ){
1019 return;
1020 }
drh9a324642003-09-06 20:12:01 +00001021 if( pCx->pCursor ){
danielk19774adee202004-05-08 08:23:19 +00001022 sqlite3BtreeCloseCursor(pCx->pCursor);
drh9a324642003-09-06 20:12:01 +00001023 }
1024 if( pCx->pBt ){
danielk19774adee202004-05-08 08:23:19 +00001025 sqlite3BtreeClose(pCx->pBt);
drh9a324642003-09-06 20:12:01 +00001026 }
drh9eff6162006-06-12 21:59:13 +00001027#ifndef SQLITE_OMIT_VIRTUALTABLE
1028 if( pCx->pVtabCursor ){
1029 sqlite3_vtab_cursor *pVtabCursor = pCx->pVtabCursor;
danielk1977be718892006-06-23 08:05:19 +00001030 const sqlite3_module *pModule = pCx->pModule;
1031 p->inVtabMethod = 1;
danielk19775bd270b2006-07-25 15:14:52 +00001032 sqlite3SafetyOff(p->db);
drh9eff6162006-06-12 21:59:13 +00001033 pModule->xClose(pVtabCursor);
danielk19775bd270b2006-07-25 15:14:52 +00001034 sqlite3SafetyOn(p->db);
danielk1977be718892006-06-23 08:05:19 +00001035 p->inVtabMethod = 0;
drh9eff6162006-06-12 21:59:13 +00001036 }
1037#endif
drh17435752007-08-16 04:30:38 +00001038 sqlite3_free(pCx->pData);
1039 sqlite3_free(pCx->aType);
1040 sqlite3_free(pCx);
drh9a324642003-09-06 20:12:01 +00001041}
1042
1043/*
drhff0587c2007-08-29 17:43:19 +00001044** Close all cursors except for VTab cursors that are currently
1045** in use.
drh9a324642003-09-06 20:12:01 +00001046*/
drhff0587c2007-08-29 17:43:19 +00001047static void closeAllCursorsExceptActiveVtabs(Vdbe *p){
drh9a324642003-09-06 20:12:01 +00001048 int i;
drh290c1942004-08-21 17:54:45 +00001049 if( p->apCsr==0 ) return;
drh9a324642003-09-06 20:12:01 +00001050 for(i=0; i<p->nCursor; i++){
drhff0587c2007-08-29 17:43:19 +00001051 Cursor *pC = p->apCsr[i];
1052 if( pC && (!p->inVtabMethod || !pC->pVtabCursor) ){
1053 sqlite3VdbeFreeCursor(p, pC);
danielk1977b7a2f2e2006-06-23 11:34:54 +00001054 p->apCsr[i] = 0;
danielk1977be718892006-06-23 08:05:19 +00001055 }
drh9a324642003-09-06 20:12:01 +00001056 }
drh9a324642003-09-06 20:12:01 +00001057}
1058
1059/*
drh9a324642003-09-06 20:12:01 +00001060** Clean up the VM after execution.
1061**
1062** This routine will automatically close any cursors, lists, and/or
1063** sorters that were left open. It also deletes the values of
drh5a12e682004-05-19 11:24:25 +00001064** variables in the aVar[] array.
drh9a324642003-09-06 20:12:01 +00001065*/
1066static void Cleanup(Vdbe *p){
1067 int i;
drh6810ce62004-01-31 19:22:56 +00001068 if( p->aStack ){
drh76ff3a02004-09-24 22:32:30 +00001069 releaseMemArray(p->aStack, 1 + (p->pTos - p->aStack));
1070 p->pTos = &p->aStack[-1];
drh6810ce62004-01-31 19:22:56 +00001071 }
drhff0587c2007-08-29 17:43:19 +00001072 closeAllCursorsExceptActiveVtabs(p);
drh0a07c102008-01-03 18:03:08 +00001073 releaseMemArray(&p->aMem[1], p->nMem);
drha01f79d2005-07-08 13:07:59 +00001074 sqlite3VdbeFifoClear(&p->sFifo);
drh76ff3a02004-09-24 22:32:30 +00001075 if( p->contextStack ){
1076 for(i=0; i<p->contextStackTop; i++){
drha01f79d2005-07-08 13:07:59 +00001077 sqlite3VdbeFifoClear(&p->contextStack[i].sFifo);
drh76ff3a02004-09-24 22:32:30 +00001078 }
drh17435752007-08-16 04:30:38 +00001079 sqlite3_free(p->contextStack);
drh344737f2004-09-19 00:50:20 +00001080 }
drh5f968432004-02-21 19:02:30 +00001081 p->contextStack = 0;
drh344737f2004-09-19 00:50:20 +00001082 p->contextStackDepth = 0;
1083 p->contextStackTop = 0;
drh17435752007-08-16 04:30:38 +00001084 sqlite3_free(p->zErrMsg);
drh9a324642003-09-06 20:12:01 +00001085 p->zErrMsg = 0;
drhd4e70eb2008-01-02 00:34:36 +00001086 p->pResultSet = 0;
drh9a324642003-09-06 20:12:01 +00001087}
1088
1089/*
danielk197722322fd2004-05-25 23:35:17 +00001090** Set the number of result columns that will be returned by this SQL
1091** statement. This is now set at compile time, rather than during
1092** execution of the vdbe program so that sqlite3_column_count() can
1093** be called on an SQL statement before sqlite3_step().
1094*/
1095void sqlite3VdbeSetNumCols(Vdbe *p, int nResColumn){
drh76ff3a02004-09-24 22:32:30 +00001096 Mem *pColName;
1097 int n;
drh4a50aac2007-08-23 02:47:53 +00001098
danielk1977955de522006-02-10 02:27:42 +00001099 releaseMemArray(p->aColName, p->nResColumn*COLNAME_N);
drh17435752007-08-16 04:30:38 +00001100 sqlite3_free(p->aColName);
danielk1977955de522006-02-10 02:27:42 +00001101 n = nResColumn*COLNAME_N;
drhcc43cab2005-10-05 11:35:09 +00001102 p->nResColumn = nResColumn;
danielk19771e536952007-08-16 10:09:01 +00001103 p->aColName = pColName = (Mem*)sqlite3DbMallocZero(p->db, sizeof(Mem)*n );
drh76ff3a02004-09-24 22:32:30 +00001104 if( p->aColName==0 ) return;
1105 while( n-- > 0 ){
drh4a50aac2007-08-23 02:47:53 +00001106 pColName->flags = MEM_Null;
drh153c62c2007-08-24 03:51:33 +00001107 pColName->db = p->db;
drh4a50aac2007-08-23 02:47:53 +00001108 pColName++;
drh76ff3a02004-09-24 22:32:30 +00001109 }
danielk197722322fd2004-05-25 23:35:17 +00001110}
1111
1112/*
danielk19773cf86062004-05-26 10:11:05 +00001113** Set the name of the idx'th column to be returned by the SQL statement.
1114** zName must be a pointer to a nul terminated string.
1115**
1116** This call must be made after a call to sqlite3VdbeSetNumCols().
1117**
drh66a51672008-01-03 00:01:23 +00001118** If N==P4_STATIC it means that zName is a pointer to a constant static
1119** string and we can just copy the pointer. If it is P4_DYNAMIC, then
drh17435752007-08-16 04:30:38 +00001120** the string is freed using sqlite3_free() when the vdbe is finished with
danielk1977d8123362004-06-12 09:25:12 +00001121** it. Otherwise, N bytes of zName are copied.
danielk19773cf86062004-05-26 10:11:05 +00001122*/
danielk1977955de522006-02-10 02:27:42 +00001123int sqlite3VdbeSetColName(Vdbe *p, int idx, int var, const char *zName, int N){
danielk19773cf86062004-05-26 10:11:05 +00001124 int rc;
1125 Mem *pColName;
danielk1977955de522006-02-10 02:27:42 +00001126 assert( idx<p->nResColumn );
1127 assert( var<COLNAME_N );
drh17435752007-08-16 04:30:38 +00001128 if( p->db->mallocFailed ) return SQLITE_NOMEM;
drh76ff3a02004-09-24 22:32:30 +00001129 assert( p->aColName!=0 );
danielk1977955de522006-02-10 02:27:42 +00001130 pColName = &(p->aColName[idx+var*p->nResColumn]);
drh66a51672008-01-03 00:01:23 +00001131 if( N==P4_DYNAMIC || N==P4_STATIC ){
drhb21c8cd2007-08-21 19:33:56 +00001132 rc = sqlite3VdbeMemSetStr(pColName, zName, -1, SQLITE_UTF8, SQLITE_STATIC);
danielk19773cf86062004-05-26 10:11:05 +00001133 }else{
drhb21c8cd2007-08-21 19:33:56 +00001134 rc = sqlite3VdbeMemSetStr(pColName, zName, N, SQLITE_UTF8,SQLITE_TRANSIENT);
danielk19773cf86062004-05-26 10:11:05 +00001135 }
drh66a51672008-01-03 00:01:23 +00001136 if( rc==SQLITE_OK && N==P4_DYNAMIC ){
danielk19773cf86062004-05-26 10:11:05 +00001137 pColName->flags = (pColName->flags&(~MEM_Static))|MEM_Dyn;
danielk1977d8123362004-06-12 09:25:12 +00001138 pColName->xDel = 0;
danielk19773cf86062004-05-26 10:11:05 +00001139 }
1140 return rc;
1141}
1142
danielk197713adf8a2004-06-03 16:08:41 +00001143/*
1144** A read or write transaction may or may not be active on database handle
1145** db. If a transaction is active, commit it. If there is a
1146** write-transaction spanning more than one database file, this routine
1147** takes care of the master journal trickery.
1148*/
drh9bb575f2004-09-06 17:24:11 +00001149static int vdbeCommit(sqlite3 *db){
danielk197713adf8a2004-06-03 16:08:41 +00001150 int i;
1151 int nTrans = 0; /* Number of databases with an active write-transaction */
1152 int rc = SQLITE_OK;
1153 int needXcommit = 0;
1154
danielk19775bd270b2006-07-25 15:14:52 +00001155 /* Before doing anything else, call the xSync() callback for any
1156 ** virtual module tables written in this transaction. This has to
1157 ** be done before determining whether a master journal file is
1158 ** required, as an xSync() callback may add an attached database
1159 ** to the transaction.
1160 */
1161 rc = sqlite3VtabSync(db, rc);
1162 if( rc!=SQLITE_OK ){
1163 return rc;
1164 }
1165
1166 /* This loop determines (a) if the commit hook should be invoked and
1167 ** (b) how many database files have open write transactions, not
1168 ** including the temp database. (b) is important because if more than
1169 ** one database file has an open write transaction, a master journal
1170 ** file is required for an atomic commit.
1171 */
danielk197713adf8a2004-06-03 16:08:41 +00001172 for(i=0; i<db->nDb; i++){
1173 Btree *pBt = db->aDb[i].pBt;
drhd0679ed2007-08-28 22:24:34 +00001174 if( sqlite3BtreeIsInTrans(pBt) ){
danielk197713adf8a2004-06-03 16:08:41 +00001175 needXcommit = 1;
1176 if( i!=1 ) nTrans++;
1177 }
1178 }
1179
1180 /* If there are any write-transactions at all, invoke the commit hook */
1181 if( needXcommit && db->xCommitCallback ){
drh92f02c32004-09-02 14:57:08 +00001182 sqlite3SafetyOff(db);
1183 rc = db->xCommitCallback(db->pCommitArg);
1184 sqlite3SafetyOn(db);
1185 if( rc ){
danielk197713adf8a2004-06-03 16:08:41 +00001186 return SQLITE_CONSTRAINT;
1187 }
1188 }
1189
danielk197740b38dc2004-06-26 08:38:24 +00001190 /* The simple case - no more than one database file (not counting the
1191 ** TEMP database) has a transaction active. There is no need for the
drh2ac3ee92004-06-07 16:27:46 +00001192 ** master-journal.
drhc9e06862004-06-09 20:03:08 +00001193 **
danielk197740b38dc2004-06-26 08:38:24 +00001194 ** If the return value of sqlite3BtreeGetFilename() is a zero length
1195 ** string, it means the main database is :memory:. In that case we do
1196 ** not support atomic multi-file commits, so use the simple case then
drhc9e06862004-06-09 20:03:08 +00001197 ** too.
danielk197713adf8a2004-06-03 16:08:41 +00001198 */
danielk197740b38dc2004-06-26 08:38:24 +00001199 if( 0==strlen(sqlite3BtreeGetFilename(db->aDb[0].pBt)) || nTrans<=1 ){
drh2ac3ee92004-06-07 16:27:46 +00001200 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
danielk197713adf8a2004-06-03 16:08:41 +00001201 Btree *pBt = db->aDb[i].pBt;
1202 if( pBt ){
drh80e35f42007-03-30 14:06:34 +00001203 rc = sqlite3BtreeCommitPhaseOne(pBt, 0);
drh2ac3ee92004-06-07 16:27:46 +00001204 }
1205 }
1206
drh80e35f42007-03-30 14:06:34 +00001207 /* Do the commit only if all databases successfully complete phase 1.
1208 ** If one of the BtreeCommitPhaseOne() calls fails, this indicates an
1209 ** IO error while deleting or truncating a journal file. It is unlikely,
1210 ** but could happen. In this case abandon processing and return the error.
danielk1977979f38e2007-03-27 16:19:51 +00001211 */
1212 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
1213 Btree *pBt = db->aDb[i].pBt;
1214 if( pBt ){
drh80e35f42007-03-30 14:06:34 +00001215 rc = sqlite3BtreeCommitPhaseTwo(pBt);
danielk197713adf8a2004-06-03 16:08:41 +00001216 }
danielk1977979f38e2007-03-27 16:19:51 +00001217 }
1218 if( rc==SQLITE_OK ){
danielk1977f9e7dda2006-06-16 16:08:53 +00001219 sqlite3VtabCommit(db);
danielk197713adf8a2004-06-03 16:08:41 +00001220 }
1221 }
1222
1223 /* The complex case - There is a multi-file write-transaction active.
1224 ** This requires a master journal file to ensure the transaction is
1225 ** committed atomicly.
1226 */
danielk197744ee5bf2005-05-27 09:41:12 +00001227#ifndef SQLITE_OMIT_DISKIO
danielk197713adf8a2004-06-03 16:08:41 +00001228 else{
danielk1977b4b47412007-08-17 15:53:36 +00001229 sqlite3_vfs *pVfs = db->pVfs;
drh2c8997b2005-08-27 16:36:48 +00001230 int needSync = 0;
danielk197713adf8a2004-06-03 16:08:41 +00001231 char *zMaster = 0; /* File-name for the master journal */
1232 char const *zMainFile = sqlite3BtreeGetFilename(db->aDb[0].pBt);
danielk1977b4b47412007-08-17 15:53:36 +00001233 sqlite3_file *pMaster = 0;
danielk197762079062007-08-15 17:08:46 +00001234 i64 offset = 0;
danielk197713adf8a2004-06-03 16:08:41 +00001235
1236 /* Select a master journal file name */
1237 do {
drha6abd042004-06-09 17:37:22 +00001238 u32 random;
drh17435752007-08-16 04:30:38 +00001239 sqlite3_free(zMaster);
danielk197713adf8a2004-06-03 16:08:41 +00001240 sqlite3Randomness(sizeof(random), &random);
danielk19771e536952007-08-16 10:09:01 +00001241 zMaster = sqlite3MPrintf(db, "%s-mj%08X", zMainFile, random&0x7fffffff);
danielk197713adf8a2004-06-03 16:08:41 +00001242 if( !zMaster ){
1243 return SQLITE_NOMEM;
1244 }
danielk1977b4b47412007-08-17 15:53:36 +00001245 }while( sqlite3OsAccess(pVfs, zMaster, SQLITE_ACCESS_EXISTS) );
danielk197713adf8a2004-06-03 16:08:41 +00001246
1247 /* Open the master journal. */
danielk1977fee2d252007-08-18 10:59:19 +00001248 rc = sqlite3OsOpenMalloc(pVfs, zMaster, &pMaster,
1249 SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|
danielk1977967a4a12007-08-20 14:23:44 +00001250 SQLITE_OPEN_EXCLUSIVE|SQLITE_OPEN_MASTER_JOURNAL, 0
danielk1977fee2d252007-08-18 10:59:19 +00001251 );
danielk197713adf8a2004-06-03 16:08:41 +00001252 if( rc!=SQLITE_OK ){
drh17435752007-08-16 04:30:38 +00001253 sqlite3_free(zMaster);
danielk197713adf8a2004-06-03 16:08:41 +00001254 return rc;
1255 }
1256
1257 /* Write the name of each database file in the transaction into the new
1258 ** master journal file. If an error occurs at this point close
1259 ** and delete the master journal file. All the individual journal files
1260 ** still have 'null' as the master journal pointer, so they will roll
danielk1977aca790a2005-01-13 11:07:52 +00001261 ** back independently if a failure occurs.
danielk197713adf8a2004-06-03 16:08:41 +00001262 */
danielk19771e536952007-08-16 10:09:01 +00001263 for(i=0; i<db->nDb; i++){
danielk197713adf8a2004-06-03 16:08:41 +00001264 Btree *pBt = db->aDb[i].pBt;
drhc9e06862004-06-09 20:03:08 +00001265 if( i==1 ) continue; /* Ignore the TEMP database */
drhd0679ed2007-08-28 22:24:34 +00001266 if( sqlite3BtreeIsInTrans(pBt) ){
danielk19775865e3d2004-06-14 06:03:57 +00001267 char const *zFile = sqlite3BtreeGetJournalname(pBt);
drhc9e06862004-06-09 20:03:08 +00001268 if( zFile[0]==0 ) continue; /* Ignore :memory: databases */
drh2c8997b2005-08-27 16:36:48 +00001269 if( !needSync && !sqlite3BtreeSyncDisabled(pBt) ){
1270 needSync = 1;
1271 }
danielk1977b4b47412007-08-17 15:53:36 +00001272 rc = sqlite3OsWrite(pMaster, zFile, strlen(zFile)+1, offset);
danielk197762079062007-08-15 17:08:46 +00001273 offset += strlen(zFile)+1;
danielk197713adf8a2004-06-03 16:08:41 +00001274 if( rc!=SQLITE_OK ){
danielk1977fee2d252007-08-18 10:59:19 +00001275 sqlite3OsCloseFree(pMaster);
1276 sqlite3OsDelete(pVfs, zMaster, 0);
drh17435752007-08-16 04:30:38 +00001277 sqlite3_free(zMaster);
danielk197713adf8a2004-06-03 16:08:41 +00001278 return rc;
1279 }
1280 }
1281 }
1282
danielk19779663b8f2007-08-24 11:52:28 +00001283 /* Sync the master journal file. If the IOCAP_SEQUENTIAL device
1284 ** flag is set this is not required.
1285 */
danielk19775865e3d2004-06-14 06:03:57 +00001286 zMainFile = sqlite3BtreeGetDirname(db->aDb[0].pBt);
danielk1977f036aef2007-08-20 05:36:51 +00001287 if( (needSync
danielk19779663b8f2007-08-24 11:52:28 +00001288 && (0==(sqlite3OsDeviceCharacteristics(pMaster)&SQLITE_IOCAP_SEQUENTIAL))
danielk1977f036aef2007-08-20 05:36:51 +00001289 && (rc=sqlite3OsSync(pMaster, SQLITE_SYNC_NORMAL))!=SQLITE_OK) ){
danielk1977fee2d252007-08-18 10:59:19 +00001290 sqlite3OsCloseFree(pMaster);
1291 sqlite3OsDelete(pVfs, zMaster, 0);
drh17435752007-08-16 04:30:38 +00001292 sqlite3_free(zMaster);
danielk19775865e3d2004-06-14 06:03:57 +00001293 return rc;
1294 }
drhc9e06862004-06-09 20:03:08 +00001295
danielk197713adf8a2004-06-03 16:08:41 +00001296 /* Sync all the db files involved in the transaction. The same call
1297 ** sets the master journal pointer in each individual journal. If
1298 ** an error occurs here, do not delete the master journal file.
1299 **
drh80e35f42007-03-30 14:06:34 +00001300 ** If the error occurs during the first call to
1301 ** sqlite3BtreeCommitPhaseOne(), then there is a chance that the
1302 ** master journal file will be orphaned. But we cannot delete it,
1303 ** in case the master journal file name was written into the journal
1304 ** file before the failure occured.
danielk197713adf8a2004-06-03 16:08:41 +00001305 */
danielk19775bd270b2006-07-25 15:14:52 +00001306 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
danielk197713adf8a2004-06-03 16:08:41 +00001307 Btree *pBt = db->aDb[i].pBt;
drhd0679ed2007-08-28 22:24:34 +00001308 if( pBt ){
drh80e35f42007-03-30 14:06:34 +00001309 rc = sqlite3BtreeCommitPhaseOne(pBt, zMaster);
danielk197713adf8a2004-06-03 16:08:41 +00001310 }
1311 }
danielk1977fee2d252007-08-18 10:59:19 +00001312 sqlite3OsCloseFree(pMaster);
danielk19775bd270b2006-07-25 15:14:52 +00001313 if( rc!=SQLITE_OK ){
drh17435752007-08-16 04:30:38 +00001314 sqlite3_free(zMaster);
danielk19775bd270b2006-07-25 15:14:52 +00001315 return rc;
1316 }
danielk197713adf8a2004-06-03 16:08:41 +00001317
danielk1977962398d2004-06-14 09:35:16 +00001318 /* Delete the master journal file. This commits the transaction. After
1319 ** doing this the directory is synced again before any individual
1320 ** transaction files are deleted.
1321 */
danielk1977fee2d252007-08-18 10:59:19 +00001322 rc = sqlite3OsDelete(pVfs, zMaster, 1);
drh17435752007-08-16 04:30:38 +00001323 sqlite3_free(zMaster);
drhc416ba92007-03-30 18:42:55 +00001324 zMaster = 0;
drh29a01382006-08-13 19:04:18 +00001325 if( rc ){
1326 return rc;
1327 }
danielk197713adf8a2004-06-03 16:08:41 +00001328
1329 /* All files and directories have already been synced, so the following
drh80e35f42007-03-30 14:06:34 +00001330 ** calls to sqlite3BtreeCommitPhaseTwo() are only closing files and
1331 ** deleting or truncating journals. If something goes wrong while
1332 ** this is happening we don't really care. The integrity of the
1333 ** transaction is already guaranteed, but some stray 'cold' journals
1334 ** may be lying around. Returning an error code won't help matters.
danielk197713adf8a2004-06-03 16:08:41 +00001335 */
danielk1977979f38e2007-03-27 16:19:51 +00001336 disable_simulated_io_errors();
danielk197713adf8a2004-06-03 16:08:41 +00001337 for(i=0; i<db->nDb; i++){
1338 Btree *pBt = db->aDb[i].pBt;
1339 if( pBt ){
drh80e35f42007-03-30 14:06:34 +00001340 sqlite3BtreeCommitPhaseTwo(pBt);
danielk197713adf8a2004-06-03 16:08:41 +00001341 }
1342 }
danielk1977979f38e2007-03-27 16:19:51 +00001343 enable_simulated_io_errors();
1344
danielk1977f9e7dda2006-06-16 16:08:53 +00001345 sqlite3VtabCommit(db);
danielk197713adf8a2004-06-03 16:08:41 +00001346 }
danielk197744ee5bf2005-05-27 09:41:12 +00001347#endif
danielk1977026d2702004-06-14 13:14:59 +00001348
drh2ac3ee92004-06-07 16:27:46 +00001349 return rc;
danielk197713adf8a2004-06-03 16:08:41 +00001350}
1351
danielk19771d850a72004-05-31 08:26:49 +00001352/*
1353** This routine checks that the sqlite3.activeVdbeCnt count variable
1354** matches the number of vdbe's in the list sqlite3.pVdbe that are
1355** currently active. An assertion fails if the two counts do not match.
drh92f02c32004-09-02 14:57:08 +00001356** This is an internal self-check only - it is not an essential processing
1357** step.
danielk19771d850a72004-05-31 08:26:49 +00001358**
1359** This is a no-op if NDEBUG is defined.
1360*/
1361#ifndef NDEBUG
drh9bb575f2004-09-06 17:24:11 +00001362static void checkActiveVdbeCnt(sqlite3 *db){
danielk19771d850a72004-05-31 08:26:49 +00001363 Vdbe *p;
1364 int cnt = 0;
danielk19771d850a72004-05-31 08:26:49 +00001365 p = db->pVdbe;
1366 while( p ){
drh92f02c32004-09-02 14:57:08 +00001367 if( p->magic==VDBE_MAGIC_RUN && p->pc>=0 ){
danielk19771d850a72004-05-31 08:26:49 +00001368 cnt++;
1369 }
1370 p = p->pNext;
1371 }
danielk19771d850a72004-05-31 08:26:49 +00001372 assert( cnt==db->activeVdbeCnt );
1373}
1374#else
1375#define checkActiveVdbeCnt(x)
1376#endif
1377
danielk19773cf86062004-05-26 10:11:05 +00001378/*
drhfb982642007-08-30 01:19:59 +00001379** For every Btree that in database connection db which
1380** has been modified, "trip" or invalidate each cursor in
1381** that Btree might have been modified so that the cursor
1382** can never be used again. This happens when a rollback
1383*** occurs. We have to trip all the other cursors, even
1384** cursor from other VMs in different database connections,
1385** so that none of them try to use the data at which they
1386** were pointing and which now may have been changed due
1387** to the rollback.
1388**
1389** Remember that a rollback can delete tables complete and
1390** reorder rootpages. So it is not sufficient just to save
1391** the state of the cursor. We have to invalidate the cursor
1392** so that it is never used again.
danielk1977be718892006-06-23 08:05:19 +00001393*/
drhade6c9c2007-11-24 10:23:44 +00001394static void invalidateCursorsOnModifiedBtrees(sqlite3 *db){
drhfb982642007-08-30 01:19:59 +00001395 int i;
1396 for(i=0; i<db->nDb; i++){
1397 Btree *p = db->aDb[i].pBt;
1398 if( p && sqlite3BtreeIsInTrans(p) ){
1399 sqlite3BtreeTripAllCursors(p, SQLITE_ABORT);
1400 }
danielk1977be718892006-06-23 08:05:19 +00001401 }
1402}
1403
1404/*
drh92f02c32004-09-02 14:57:08 +00001405** This routine is called the when a VDBE tries to halt. If the VDBE
1406** has made changes and is in autocommit mode, then commit those
1407** changes. If a rollback is needed, then do the rollback.
drh9a324642003-09-06 20:12:01 +00001408**
drh92f02c32004-09-02 14:57:08 +00001409** This routine is the only way to move the state of a VM from
drhff0587c2007-08-29 17:43:19 +00001410** SQLITE_MAGIC_RUN to SQLITE_MAGIC_HALT. It is harmless to
1411** call this on a VM that is in the SQLITE_MAGIC_HALT state.
drh92f02c32004-09-02 14:57:08 +00001412**
1413** Return an error code. If the commit could not complete because of
1414** lock contention, return SQLITE_BUSY. If SQLITE_BUSY is returned, it
1415** means the close did not happen and needs to be repeated.
drh9a324642003-09-06 20:12:01 +00001416*/
drhff0587c2007-08-29 17:43:19 +00001417int sqlite3VdbeHalt(Vdbe *p){
drh9bb575f2004-09-06 17:24:11 +00001418 sqlite3 *db = p->db;
drh9a324642003-09-06 20:12:01 +00001419 int i;
danielk19771d850a72004-05-31 08:26:49 +00001420 int (*xFunc)(Btree *pBt) = 0; /* Function to call on each btree backend */
danielk197707cb5602006-01-20 10:55:05 +00001421 int isSpecialError; /* Set to true if SQLITE_NOMEM or IOERR */
1422
1423 /* This function contains the logic that determines if a statement or
1424 ** transaction will be committed or rolled back as a result of the
1425 ** execution of this virtual machine.
1426 **
drh71b890a2007-10-03 15:30:52 +00001427 ** If any of the following errors occur:
danielk197707cb5602006-01-20 10:55:05 +00001428 **
drh71b890a2007-10-03 15:30:52 +00001429 ** SQLITE_NOMEM
1430 ** SQLITE_IOERR
1431 ** SQLITE_FULL
1432 ** SQLITE_INTERRUPT
danielk197707cb5602006-01-20 10:55:05 +00001433 **
drh71b890a2007-10-03 15:30:52 +00001434 ** Then the internal cache might have been left in an inconsistent
1435 ** state. We need to rollback the statement transaction, if there is
1436 ** one, or the complete transaction if there is no statement transaction.
danielk197707cb5602006-01-20 10:55:05 +00001437 */
drh9a324642003-09-06 20:12:01 +00001438
drh17435752007-08-16 04:30:38 +00001439 if( p->db->mallocFailed ){
danielk1977261919c2005-12-06 12:52:59 +00001440 p->rc = SQLITE_NOMEM;
1441 }
drhff0587c2007-08-29 17:43:19 +00001442 closeAllCursorsExceptActiveVtabs(p);
drh92f02c32004-09-02 14:57:08 +00001443 if( p->magic!=VDBE_MAGIC_RUN ){
drh92f02c32004-09-02 14:57:08 +00001444 return SQLITE_OK;
drh9a324642003-09-06 20:12:01 +00001445 }
danielk19771d850a72004-05-31 08:26:49 +00001446 checkActiveVdbeCnt(db);
danielk1977261919c2005-12-06 12:52:59 +00001447
danielk197707cb5602006-01-20 10:55:05 +00001448 /* No commit or rollback needed if the program never started */
1449 if( p->pc>=0 ){
drhaac2f552006-09-23 21:44:23 +00001450 int mrc; /* Primary error code from p->rc */
drhff0587c2007-08-29 17:43:19 +00001451
1452 /* Lock all btrees used by the statement */
1453 sqlite3BtreeMutexArrayEnter(&p->aMutex);
1454
drh71b890a2007-10-03 15:30:52 +00001455 /* Check for one of the special errors */
drhaac2f552006-09-23 21:44:23 +00001456 mrc = p->rc & 0xff;
drh71b890a2007-10-03 15:30:52 +00001457 isSpecialError = mrc==SQLITE_NOMEM || mrc==SQLITE_IOERR
drh77658e22007-12-04 16:54:52 +00001458 || mrc==SQLITE_INTERRUPT || mrc==SQLITE_FULL;
danielk197707cb5602006-01-20 10:55:05 +00001459 if( isSpecialError ){
danielk1977261919c2005-12-06 12:52:59 +00001460 /* This loop does static analysis of the query to see which of the
1461 ** following three categories it falls into:
1462 **
1463 ** Read-only
danielk197707cb5602006-01-20 10:55:05 +00001464 ** Query with statement journal
1465 ** Query without statement journal
danielk1977261919c2005-12-06 12:52:59 +00001466 **
1467 ** We could do something more elegant than this static analysis (i.e.
1468 ** store the type of query as part of the compliation phase), but
danielk197707cb5602006-01-20 10:55:05 +00001469 ** handling malloc() or IO failure is a fairly obscure edge case so
1470 ** this is probably easier. Todo: Might be an opportunity to reduce
1471 ** code size a very small amount though...
danielk1977261919c2005-12-06 12:52:59 +00001472 */
drhd1817042007-10-03 18:45:04 +00001473 int notReadOnly = 0;
danielk1977261919c2005-12-06 12:52:59 +00001474 int isStatement = 0;
1475 assert(p->aOp || p->nOp==0);
1476 for(i=0; i<p->nOp; i++){
1477 switch( p->aOp[i].opcode ){
1478 case OP_Transaction:
drhd1817042007-10-03 18:45:04 +00001479 notReadOnly |= p->aOp[i].p2;
danielk1977261919c2005-12-06 12:52:59 +00001480 break;
1481 case OP_Statement:
1482 isStatement = 1;
1483 break;
1484 }
1485 }
drhff0587c2007-08-29 17:43:19 +00001486
1487
danielk197707cb5602006-01-20 10:55:05 +00001488 /* If the query was read-only, we need do no rollback at all. Otherwise,
1489 ** proceed with the special handling.
1490 */
drhd1817042007-10-03 18:45:04 +00001491 if( notReadOnly || mrc!=SQLITE_INTERRUPT ){
danielk1977e965ac72007-06-13 15:22:28 +00001492 if( p->rc==SQLITE_IOERR_BLOCKED && isStatement ){
1493 xFunc = sqlite3BtreeRollbackStmt;
1494 p->rc = SQLITE_BUSY;
drhd1817042007-10-03 18:45:04 +00001495 } else if( (mrc==SQLITE_NOMEM || mrc==SQLITE_FULL) && isStatement ){
danielk197707cb5602006-01-20 10:55:05 +00001496 xFunc = sqlite3BtreeRollbackStmt;
1497 }else{
1498 /* We are forced to roll back the active transaction. Before doing
1499 ** so, abort any other statements this handle currently has active.
1500 */
drhfb982642007-08-30 01:19:59 +00001501 invalidateCursorsOnModifiedBtrees(db);
danielk197797a227c2006-01-20 16:32:04 +00001502 sqlite3RollbackAll(db);
danielk197707cb5602006-01-20 10:55:05 +00001503 db->autoCommit = 1;
1504 }
danielk1977261919c2005-12-06 12:52:59 +00001505 }
1506 }
danielk197707cb5602006-01-20 10:55:05 +00001507
1508 /* If the auto-commit flag is set and this is the only active vdbe, then
1509 ** we do either a commit or rollback of the current transaction.
1510 **
1511 ** Note: This block also runs if one of the special errors handled
1512 ** above has occured.
1513 */
1514 if( db->autoCommit && db->activeVdbeCnt==1 ){
1515 if( p->rc==SQLITE_OK || (p->errorAction==OE_Fail && !isSpecialError) ){
drhfd131da2007-08-07 17:13:03 +00001516 /* The auto-commit flag is true, and the vdbe program was
danielk197707cb5602006-01-20 10:55:05 +00001517 ** successful or hit an 'OR FAIL' constraint. This means a commit
1518 ** is required.
1519 */
1520 int rc = vdbeCommit(db);
1521 if( rc==SQLITE_BUSY ){
drhff0587c2007-08-29 17:43:19 +00001522 sqlite3BtreeMutexArrayLeave(&p->aMutex);
danielk197707cb5602006-01-20 10:55:05 +00001523 return SQLITE_BUSY;
1524 }else if( rc!=SQLITE_OK ){
1525 p->rc = rc;
danielk197797a227c2006-01-20 16:32:04 +00001526 sqlite3RollbackAll(db);
danielk197707cb5602006-01-20 10:55:05 +00001527 }else{
1528 sqlite3CommitInternalChanges(db);
1529 }
1530 }else{
danielk197797a227c2006-01-20 16:32:04 +00001531 sqlite3RollbackAll(db);
danielk197707cb5602006-01-20 10:55:05 +00001532 }
1533 }else if( !xFunc ){
1534 if( p->rc==SQLITE_OK || p->errorAction==OE_Fail ){
danielk1977182c4ba2007-06-27 15:53:34 +00001535 if( p->openedStatement ){
1536 xFunc = sqlite3BtreeCommitStmt;
1537 }
danielk197707cb5602006-01-20 10:55:05 +00001538 }else if( p->errorAction==OE_Abort ){
1539 xFunc = sqlite3BtreeRollbackStmt;
1540 }else{
drhfb982642007-08-30 01:19:59 +00001541 invalidateCursorsOnModifiedBtrees(db);
danielk197797a227c2006-01-20 16:32:04 +00001542 sqlite3RollbackAll(db);
danielk197707cb5602006-01-20 10:55:05 +00001543 db->autoCommit = 1;
1544 }
danielk19771d850a72004-05-31 08:26:49 +00001545 }
danielk197707cb5602006-01-20 10:55:05 +00001546
1547 /* If xFunc is not NULL, then it is one of sqlite3BtreeRollbackStmt or
1548 ** sqlite3BtreeCommitStmt. Call it once on each backend. If an error occurs
1549 ** and the return code is still SQLITE_OK, set the return code to the new
1550 ** error value.
1551 */
1552 assert(!xFunc ||
1553 xFunc==sqlite3BtreeCommitStmt ||
1554 xFunc==sqlite3BtreeRollbackStmt
1555 );
1556 for(i=0; xFunc && i<db->nDb; i++){
1557 int rc;
1558 Btree *pBt = db->aDb[i].pBt;
1559 if( pBt ){
1560 rc = xFunc(pBt);
danielk19778a7aea32006-01-23 15:25:48 +00001561 if( rc && (p->rc==SQLITE_OK || p->rc==SQLITE_CONSTRAINT) ){
1562 p->rc = rc;
1563 sqlite3SetString(&p->zErrMsg, 0);
1564 }
danielk197707cb5602006-01-20 10:55:05 +00001565 }
danielk197777d83ba2004-05-31 10:08:14 +00001566 }
danielk197707cb5602006-01-20 10:55:05 +00001567
1568 /* If this was an INSERT, UPDATE or DELETE and the statement was committed,
1569 ** set the change counter.
1570 */
1571 if( p->changeCntOn && p->pc>=0 ){
1572 if( !xFunc || xFunc==sqlite3BtreeCommitStmt ){
1573 sqlite3VdbeSetChanges(db, p->nChange);
1574 }else{
1575 sqlite3VdbeSetChanges(db, 0);
1576 }
1577 p->nChange = 0;
danielk1977b28af712004-06-21 06:50:26 +00001578 }
danielk197707cb5602006-01-20 10:55:05 +00001579
1580 /* Rollback or commit any schema changes that occurred. */
1581 if( p->rc!=SQLITE_OK && db->flags&SQLITE_InternChanges ){
1582 sqlite3ResetInternalSchema(db, 0);
1583 db->flags = (db->flags | SQLITE_InternChanges);
1584 }
drhff0587c2007-08-29 17:43:19 +00001585
1586 /* Release the locks */
1587 sqlite3BtreeMutexArrayLeave(&p->aMutex);
drh9a324642003-09-06 20:12:01 +00001588 }
danielk19771d850a72004-05-31 08:26:49 +00001589
danielk197765fd59f2006-06-24 11:51:33 +00001590 /* We have successfully halted and closed the VM. Record this fact. */
1591 if( p->pc>=0 ){
danielk19771d850a72004-05-31 08:26:49 +00001592 db->activeVdbeCnt--;
drh9a324642003-09-06 20:12:01 +00001593 }
drh92f02c32004-09-02 14:57:08 +00001594 p->magic = VDBE_MAGIC_HALT;
1595 checkActiveVdbeCnt(db);
drhff0587c2007-08-29 17:43:19 +00001596 if( p->db->mallocFailed ){
1597 p->rc = SQLITE_NOMEM;
1598 }
1599 checkActiveVdbeCnt(db);
danielk19771d850a72004-05-31 08:26:49 +00001600
drh92f02c32004-09-02 14:57:08 +00001601 return SQLITE_OK;
1602}
drh4cf7c7f2007-08-28 23:28:07 +00001603
drh92f02c32004-09-02 14:57:08 +00001604
1605/*
drh3c23a882007-01-09 14:01:13 +00001606** Each VDBE holds the result of the most recent sqlite3_step() call
1607** in p->rc. This routine sets that result back to SQLITE_OK.
1608*/
1609void sqlite3VdbeResetStepResult(Vdbe *p){
1610 p->rc = SQLITE_OK;
1611}
1612
1613/*
drh92f02c32004-09-02 14:57:08 +00001614** Clean up a VDBE after execution but do not delete the VDBE just yet.
1615** Write any error messages into *pzErrMsg. Return the result code.
1616**
1617** After this routine is run, the VDBE should be ready to be executed
1618** again.
1619**
1620** To look at it another way, this routine resets the state of the
1621** virtual machine from VDBE_MAGIC_RUN or VDBE_MAGIC_HALT back to
1622** VDBE_MAGIC_INIT.
1623*/
1624int sqlite3VdbeReset(Vdbe *p){
drh4ac285a2006-09-15 07:28:50 +00001625 sqlite3 *db;
drh4ac285a2006-09-15 07:28:50 +00001626 db = p->db;
drh92f02c32004-09-02 14:57:08 +00001627
1628 /* If the VM did not run to completion or if it encountered an
1629 ** error, then it might not have been halted properly. So halt
1630 ** it now.
1631 */
drh4ac285a2006-09-15 07:28:50 +00001632 sqlite3SafetyOn(db);
drh92f02c32004-09-02 14:57:08 +00001633 sqlite3VdbeHalt(p);
drh4ac285a2006-09-15 07:28:50 +00001634 sqlite3SafetyOff(db);
drh92f02c32004-09-02 14:57:08 +00001635
drhfb7e7652005-01-24 00:28:42 +00001636 /* If the VDBE has be run even partially, then transfer the error code
1637 ** and error message from the VDBE into the main database structure. But
1638 ** if the VDBE has just been set to run but has not actually executed any
1639 ** instructions yet, leave the main database error information unchanged.
drh92f02c32004-09-02 14:57:08 +00001640 */
drhfb7e7652005-01-24 00:28:42 +00001641 if( p->pc>=0 ){
1642 if( p->zErrMsg ){
drhb21c8cd2007-08-21 19:33:56 +00001643 sqlite3ValueSetStr(db->pErr,-1,p->zErrMsg,SQLITE_UTF8,sqlite3_free);
danielk197797a227c2006-01-20 16:32:04 +00001644 db->errCode = p->rc;
drhfb7e7652005-01-24 00:28:42 +00001645 p->zErrMsg = 0;
1646 }else if( p->rc ){
drh4ac285a2006-09-15 07:28:50 +00001647 sqlite3Error(db, p->rc, 0);
drhfb7e7652005-01-24 00:28:42 +00001648 }else{
drh4ac285a2006-09-15 07:28:50 +00001649 sqlite3Error(db, SQLITE_OK, 0);
drhfb7e7652005-01-24 00:28:42 +00001650 }
danielk1977a21c6b62005-01-24 10:25:59 +00001651 }else if( p->rc && p->expired ){
1652 /* The expired flag was set on the VDBE before the first call
1653 ** to sqlite3_step(). For consistency (since sqlite3_step() was
1654 ** called), set the database error in this case as well.
1655 */
drh4ac285a2006-09-15 07:28:50 +00001656 sqlite3Error(db, p->rc, 0);
danielk19778e556522007-11-13 10:30:24 +00001657 sqlite3ValueSetStr(db->pErr, -1, p->zErrMsg, SQLITE_UTF8, sqlite3_free);
1658 p->zErrMsg = 0;
drh92f02c32004-09-02 14:57:08 +00001659 }
1660
1661 /* Reclaim all memory used by the VDBE
1662 */
1663 Cleanup(p);
1664
1665 /* Save profiling information from this VDBE run.
1666 */
danielk1977261919c2005-12-06 12:52:59 +00001667 assert( p->pTos<&p->aStack[p->pc<0?0:p->pc] || !p->aStack );
drh9a324642003-09-06 20:12:01 +00001668#ifdef VDBE_PROFILE
1669 {
1670 FILE *out = fopen("vdbe_profile.out", "a");
1671 if( out ){
1672 int i;
1673 fprintf(out, "---- ");
1674 for(i=0; i<p->nOp; i++){
1675 fprintf(out, "%02x", p->aOp[i].opcode);
1676 }
1677 fprintf(out, "\n");
1678 for(i=0; i<p->nOp; i++){
1679 fprintf(out, "%6d %10lld %8lld ",
1680 p->aOp[i].cnt,
1681 p->aOp[i].cycles,
1682 p->aOp[i].cnt>0 ? p->aOp[i].cycles/p->aOp[i].cnt : 0
1683 );
danielk19774adee202004-05-08 08:23:19 +00001684 sqlite3VdbePrintOp(out, i, &p->aOp[i]);
drh9a324642003-09-06 20:12:01 +00001685 }
1686 fclose(out);
1687 }
1688 }
1689#endif
1690 p->magic = VDBE_MAGIC_INIT;
drh91b48aa2004-06-30 11:14:18 +00001691 p->aborted = 0;
drh4ac285a2006-09-15 07:28:50 +00001692 return p->rc & db->errMask;
drh9a324642003-09-06 20:12:01 +00001693}
drh92f02c32004-09-02 14:57:08 +00001694
drh9a324642003-09-06 20:12:01 +00001695/*
1696** Clean up and delete a VDBE after execution. Return an integer which is
1697** the result code. Write any error message text into *pzErrMsg.
1698*/
danielk19779e6db7d2004-06-21 08:18:51 +00001699int sqlite3VdbeFinalize(Vdbe *p){
danielk1977b5548a82004-06-26 13:51:33 +00001700 int rc = SQLITE_OK;
danielk1977b5548a82004-06-26 13:51:33 +00001701 if( p->magic==VDBE_MAGIC_RUN || p->magic==VDBE_MAGIC_HALT ){
1702 rc = sqlite3VdbeReset(p);
drh4ac285a2006-09-15 07:28:50 +00001703 assert( (rc & p->db->errMask)==rc );
danielk1977b5548a82004-06-26 13:51:33 +00001704 }else if( p->magic!=VDBE_MAGIC_INIT ){
drh9a324642003-09-06 20:12:01 +00001705 return SQLITE_MISUSE;
1706 }
danielk19774adee202004-05-08 08:23:19 +00001707 sqlite3VdbeDelete(p);
drh9a324642003-09-06 20:12:01 +00001708 return rc;
1709}
1710
1711/*
drhf92c7ff2004-06-19 15:40:23 +00001712** Call the destructor for each auxdata entry in pVdbeFunc for which
danielk1977e159fdf2004-06-21 10:45:06 +00001713** the corresponding bit in mask is clear. Auxdata entries beyond 31
drhf92c7ff2004-06-19 15:40:23 +00001714** are always destroyed. To destroy all auxdata entries, call this
danielk1977e159fdf2004-06-21 10:45:06 +00001715** routine with mask==0.
drhf92c7ff2004-06-19 15:40:23 +00001716*/
1717void sqlite3VdbeDeleteAuxData(VdbeFunc *pVdbeFunc, int mask){
1718 int i;
1719 for(i=0; i<pVdbeFunc->nAux; i++){
1720 struct AuxData *pAux = &pVdbeFunc->apAux[i];
1721 if( (i>31 || !(mask&(1<<i))) && pAux->pAux ){
1722 if( pAux->xDelete ){
1723 pAux->xDelete(pAux->pAux);
1724 }
1725 pAux->pAux = 0;
1726 }
1727 }
1728}
1729
1730/*
drh9a324642003-09-06 20:12:01 +00001731** Delete an entire VDBE.
1732*/
danielk19774adee202004-05-08 08:23:19 +00001733void sqlite3VdbeDelete(Vdbe *p){
drh9a324642003-09-06 20:12:01 +00001734 int i;
1735 if( p==0 ) return;
1736 Cleanup(p);
1737 if( p->pPrev ){
1738 p->pPrev->pNext = p->pNext;
1739 }else{
1740 assert( p->db->pVdbe==p );
1741 p->db->pVdbe = p->pNext;
1742 }
1743 if( p->pNext ){
1744 p->pNext->pPrev = p->pPrev;
1745 }
drh76ff3a02004-09-24 22:32:30 +00001746 if( p->aOp ){
drhd4e70eb2008-01-02 00:34:36 +00001747 Op *pOp = p->aOp;
1748 for(i=0; i<p->nOp; i++, pOp++){
drh66a51672008-01-03 00:01:23 +00001749 freeP4(pOp->p4type, pOp->p4.p);
drhd4e70eb2008-01-02 00:34:36 +00001750#ifdef SQLITE_DEBUG
1751 sqlite3_free(pOp->zComment);
1752#endif
drh9a324642003-09-06 20:12:01 +00001753 }
drh17435752007-08-16 04:30:38 +00001754 sqlite3_free(p->aOp);
drh9a324642003-09-06 20:12:01 +00001755 }
drh76ff3a02004-09-24 22:32:30 +00001756 releaseMemArray(p->aVar, p->nVar);
drh17435752007-08-16 04:30:38 +00001757 sqlite3_free(p->aLabel);
1758 sqlite3_free(p->aStack);
danielk1977955de522006-02-10 02:27:42 +00001759 releaseMemArray(p->aColName, p->nResColumn*COLNAME_N);
drh17435752007-08-16 04:30:38 +00001760 sqlite3_free(p->aColName);
1761 sqlite3_free(p->zSql);
drh9a324642003-09-06 20:12:01 +00001762 p->magic = VDBE_MAGIC_DEAD;
drh17435752007-08-16 04:30:38 +00001763 sqlite3_free(p);
drh9a324642003-09-06 20:12:01 +00001764}
drha11846b2004-01-07 18:52:56 +00001765
1766/*
drha11846b2004-01-07 18:52:56 +00001767** If a MoveTo operation is pending on the given cursor, then do that
1768** MoveTo now. Return an error code. If no MoveTo is pending, this
1769** routine does nothing and returns SQLITE_OK.
1770*/
danielk19774adee202004-05-08 08:23:19 +00001771int sqlite3VdbeCursorMoveto(Cursor *p){
drha11846b2004-01-07 18:52:56 +00001772 if( p->deferredMoveto ){
drh536065a2005-01-26 21:55:31 +00001773 int res, rc;
adamd4fc93082006-09-14 16:57:19 +00001774#ifdef SQLITE_TEST
danielk1977132872b2004-05-10 10:37:18 +00001775 extern int sqlite3_search_count;
adamd4fc93082006-09-14 16:57:19 +00001776#endif
drhf0863fe2005-06-12 21:35:51 +00001777 assert( p->isTable );
drh5f9c1a22007-04-04 01:27:44 +00001778 rc = sqlite3BtreeMoveto(p->pCursor, 0, p->movetoTarget, 0, &res);
drh536065a2005-01-26 21:55:31 +00001779 if( rc ) return rc;
drhd3d39e92004-05-20 22:16:29 +00001780 *p->pIncrKey = 0;
drhf0863fe2005-06-12 21:35:51 +00001781 p->lastRowid = keyToInt(p->movetoTarget);
1782 p->rowidIsValid = res==0;
drha11846b2004-01-07 18:52:56 +00001783 if( res<0 ){
drh536065a2005-01-26 21:55:31 +00001784 rc = sqlite3BtreeNext(p->pCursor, &res);
1785 if( rc ) return rc;
drha11846b2004-01-07 18:52:56 +00001786 }
drh10cfdd52006-08-08 15:42:59 +00001787#ifdef SQLITE_TEST
danielk1977132872b2004-05-10 10:37:18 +00001788 sqlite3_search_count++;
drh10cfdd52006-08-08 15:42:59 +00001789#endif
drha11846b2004-01-07 18:52:56 +00001790 p->deferredMoveto = 0;
drh76873ab2006-01-07 18:48:26 +00001791 p->cacheStatus = CACHE_STALE;
drha11846b2004-01-07 18:52:56 +00001792 }
1793 return SQLITE_OK;
1794}
danielk19774adee202004-05-08 08:23:19 +00001795
drhab9f7f12004-05-08 10:56:11 +00001796/*
danielk1977cfcdaef2004-05-12 07:33:33 +00001797** The following functions:
danielk197790e4d952004-05-10 10:05:53 +00001798**
danielk1977cfcdaef2004-05-12 07:33:33 +00001799** sqlite3VdbeSerialType()
1800** sqlite3VdbeSerialTypeLen()
1801** sqlite3VdbeSerialRead()
danielk197790e4d952004-05-10 10:05:53 +00001802** sqlite3VdbeSerialLen()
danielk1977cfcdaef2004-05-12 07:33:33 +00001803** sqlite3VdbeSerialWrite()
danielk197790e4d952004-05-10 10:05:53 +00001804**
1805** encapsulate the code that serializes values for storage in SQLite
danielk1977cfcdaef2004-05-12 07:33:33 +00001806** data and index records. Each serialized value consists of a
1807** 'serial-type' and a blob of data. The serial type is an 8-byte unsigned
1808** integer, stored as a varint.
danielk197790e4d952004-05-10 10:05:53 +00001809**
danielk1977cfcdaef2004-05-12 07:33:33 +00001810** In an SQLite index record, the serial type is stored directly before
1811** the blob of data that it corresponds to. In a table record, all serial
1812** types are stored at the start of the record, and the blobs of data at
1813** the end. Hence these functions allow the caller to handle the
1814** serial-type and data blob seperately.
1815**
1816** The following table describes the various storage classes for data:
1817**
1818** serial type bytes of data type
danielk197790e4d952004-05-10 10:05:53 +00001819** -------------- --------------- ---------------
drha19b7752004-05-30 21:14:58 +00001820** 0 0 NULL
danielk197790e4d952004-05-10 10:05:53 +00001821** 1 1 signed integer
1822** 2 2 signed integer
drha19b7752004-05-30 21:14:58 +00001823** 3 3 signed integer
1824** 4 4 signed integer
1825** 5 6 signed integer
1826** 6 8 signed integer
1827** 7 8 IEEE float
drhd946db02005-12-29 19:23:06 +00001828** 8 0 Integer constant 0
1829** 9 0 Integer constant 1
1830** 10,11 reserved for expansion
danielk197790e4d952004-05-10 10:05:53 +00001831** N>=12 and even (N-12)/2 BLOB
1832** N>=13 and odd (N-13)/2 text
1833**
drh35a59652006-01-02 18:24:40 +00001834** The 8 and 9 types were added in 3.3.0, file format 4. Prior versions
1835** of SQLite will not understand those serial types.
danielk197790e4d952004-05-10 10:05:53 +00001836*/
1837
1838/*
danielk1977cfcdaef2004-05-12 07:33:33 +00001839** Return the serial-type for the value stored in pMem.
danielk1977192ac1d2004-05-10 07:17:30 +00001840*/
drhd946db02005-12-29 19:23:06 +00001841u32 sqlite3VdbeSerialType(Mem *pMem, int file_format){
danielk1977cfcdaef2004-05-12 07:33:33 +00001842 int flags = pMem->flags;
drhfdf972a2007-05-02 13:30:27 +00001843 int n;
danielk1977cfcdaef2004-05-12 07:33:33 +00001844
1845 if( flags&MEM_Null ){
drha19b7752004-05-30 21:14:58 +00001846 return 0;
danielk197790e4d952004-05-10 10:05:53 +00001847 }
danielk1977cfcdaef2004-05-12 07:33:33 +00001848 if( flags&MEM_Int ){
drhfe2093d2005-01-20 22:48:47 +00001849 /* Figure out whether to use 1, 2, 4, 6 or 8 bytes. */
drhb47d45c2005-04-15 12:04:34 +00001850# define MAX_6BYTE ((((i64)0x00001000)<<32)-1)
drh3c024d62007-03-30 11:23:45 +00001851 i64 i = pMem->u.i;
drhd946db02005-12-29 19:23:06 +00001852 u64 u;
1853 if( file_format>=4 && (i&1)==i ){
1854 return 8+i;
1855 }
1856 u = i<0 ? -i : i;
drh5742b632005-01-26 17:47:02 +00001857 if( u<=127 ) return 1;
1858 if( u<=32767 ) return 2;
1859 if( u<=8388607 ) return 3;
1860 if( u<=2147483647 ) return 4;
1861 if( u<=MAX_6BYTE ) return 5;
drha19b7752004-05-30 21:14:58 +00001862 return 6;
danielk197790e4d952004-05-10 10:05:53 +00001863 }
danielk1977cfcdaef2004-05-12 07:33:33 +00001864 if( flags&MEM_Real ){
drha19b7752004-05-30 21:14:58 +00001865 return 7;
danielk197790e4d952004-05-10 10:05:53 +00001866 }
drhfdf972a2007-05-02 13:30:27 +00001867 assert( flags&(MEM_Str|MEM_Blob) );
1868 n = pMem->n;
1869 if( flags & MEM_Zero ){
1870 n += pMem->u.i;
danielk197790e4d952004-05-10 10:05:53 +00001871 }
drhfdf972a2007-05-02 13:30:27 +00001872 assert( n>=0 );
1873 return ((n*2) + 12 + ((flags&MEM_Str)!=0));
danielk1977192ac1d2004-05-10 07:17:30 +00001874}
1875
1876/*
danielk1977cfcdaef2004-05-12 07:33:33 +00001877** Return the length of the data corresponding to the supplied serial-type.
danielk1977192ac1d2004-05-10 07:17:30 +00001878*/
drh25aa1b42004-05-28 01:39:01 +00001879int sqlite3VdbeSerialTypeLen(u32 serial_type){
drha19b7752004-05-30 21:14:58 +00001880 if( serial_type>=12 ){
drh51846b52004-05-28 16:00:21 +00001881 return (serial_type-12)/2;
1882 }else{
drh57196282004-10-06 15:41:16 +00001883 static const u8 aSize[] = { 0, 1, 2, 3, 4, 6, 8, 8, 0, 0, 0, 0 };
drh51846b52004-05-28 16:00:21 +00001884 return aSize[serial_type];
1885 }
danielk1977192ac1d2004-05-10 07:17:30 +00001886}
1887
1888/*
drh110daac2007-05-04 11:59:31 +00001889** If we are on an architecture with mixed-endian floating
drh7a4f5022007-05-23 07:20:08 +00001890** points (ex: ARM7) then swap the lower 4 bytes with the
drh110daac2007-05-04 11:59:31 +00001891** upper 4 bytes. Return the result.
1892**
drh7a4f5022007-05-23 07:20:08 +00001893** For most architectures, this is a no-op.
1894**
1895** (later): It is reported to me that the mixed-endian problem
1896** on ARM7 is an issue with GCC, not with the ARM7 chip. It seems
1897** that early versions of GCC stored the two words of a 64-bit
1898** float in the wrong order. And that error has been propagated
1899** ever since. The blame is not necessarily with GCC, though.
1900** GCC might have just copying the problem from a prior compiler.
1901** I am also told that newer versions of GCC that follow a different
1902** ABI get the byte order right.
1903**
1904** Developers using SQLite on an ARM7 should compile and run their
1905** application using -DSQLITE_DEBUG=1 at least once. With DEBUG
1906** enabled, some asserts below will ensure that the byte order of
1907** floating point values is correct.
drh60d09a72007-08-30 15:05:08 +00001908**
1909** (2007-08-30) Frank van Vugt has studied this problem closely
1910** and has send his findings to the SQLite developers. Frank
1911** writes that some Linux kernels offer floating point hardware
1912** emulation that uses only 32-bit mantissas instead of a full
1913** 48-bits as required by the IEEE standard. (This is the
1914** CONFIG_FPE_FASTFPE option.) On such systems, floating point
1915** byte swapping becomes very complicated. To avoid problems,
1916** the necessary byte swapping is carried out using a 64-bit integer
1917** rather than a 64-bit float. Frank assures us that the code here
1918** works for him. We, the developers, have no way to independently
1919** verify this, but Frank seems to know what he is talking about
1920** so we trust him.
drh110daac2007-05-04 11:59:31 +00001921*/
1922#ifdef SQLITE_MIXED_ENDIAN_64BIT_FLOAT
drh60d09a72007-08-30 15:05:08 +00001923static u64 floatSwap(u64 in){
drh110daac2007-05-04 11:59:31 +00001924 union {
drh60d09a72007-08-30 15:05:08 +00001925 u64 r;
drh110daac2007-05-04 11:59:31 +00001926 u32 i[2];
1927 } u;
1928 u32 t;
1929
1930 u.r = in;
1931 t = u.i[0];
1932 u.i[0] = u.i[1];
1933 u.i[1] = t;
1934 return u.r;
1935}
1936# define swapMixedEndianFloat(X) X = floatSwap(X)
1937#else
1938# define swapMixedEndianFloat(X)
1939#endif
1940
1941/*
danielk1977cfcdaef2004-05-12 07:33:33 +00001942** Write the serialized data blob for the value stored in pMem into
1943** buf. It is assumed that the caller has allocated sufficient space.
1944** Return the number of bytes written.
drhfdf972a2007-05-02 13:30:27 +00001945**
1946** nBuf is the amount of space left in buf[]. nBuf must always be
1947** large enough to hold the entire field. Except, if the field is
1948** a blob with a zero-filled tail, then buf[] might be just the right
1949** size to hold everything except for the zero-filled tail. If buf[]
1950** is only big enough to hold the non-zero prefix, then only write that
1951** prefix into buf[]. But if buf[] is large enough to hold both the
1952** prefix and the tail then write the prefix and set the tail to all
1953** zeros.
1954**
1955** Return the number of bytes actually written into buf[]. The number
1956** of bytes in the zero-filled tail is included in the return value only
1957** if those bytes were zeroed in buf[].
danielk1977cfcdaef2004-05-12 07:33:33 +00001958*/
drhfdf972a2007-05-02 13:30:27 +00001959int sqlite3VdbeSerialPut(u8 *buf, int nBuf, Mem *pMem, int file_format){
drhd946db02005-12-29 19:23:06 +00001960 u32 serial_type = sqlite3VdbeSerialType(pMem, file_format);
danielk1977cfcdaef2004-05-12 07:33:33 +00001961 int len;
danielk1977183f9f72004-05-13 05:20:26 +00001962
drh1483e142004-05-21 21:12:42 +00001963 /* Integer and Real */
drhd946db02005-12-29 19:23:06 +00001964 if( serial_type<=7 && serial_type>0 ){
drh1483e142004-05-21 21:12:42 +00001965 u64 v;
1966 int i;
drha19b7752004-05-30 21:14:58 +00001967 if( serial_type==7 ){
drh4f0c5872007-03-26 22:05:01 +00001968 assert( sizeof(v)==sizeof(pMem->r) );
1969 memcpy(&v, &pMem->r, sizeof(v));
drh60d09a72007-08-30 15:05:08 +00001970 swapMixedEndianFloat(v);
drh1483e142004-05-21 21:12:42 +00001971 }else{
drh3c024d62007-03-30 11:23:45 +00001972 v = pMem->u.i;
danielk1977cfcdaef2004-05-12 07:33:33 +00001973 }
drh1483e142004-05-21 21:12:42 +00001974 len = i = sqlite3VdbeSerialTypeLen(serial_type);
drhfdf972a2007-05-02 13:30:27 +00001975 assert( len<=nBuf );
drh1483e142004-05-21 21:12:42 +00001976 while( i-- ){
1977 buf[i] = (v&0xFF);
1978 v >>= 8;
1979 }
1980 return len;
danielk1977cfcdaef2004-05-12 07:33:33 +00001981 }
drhd946db02005-12-29 19:23:06 +00001982
danielk1977cfcdaef2004-05-12 07:33:33 +00001983 /* String or blob */
drhd946db02005-12-29 19:23:06 +00001984 if( serial_type>=12 ){
drhfdf972a2007-05-02 13:30:27 +00001985 assert( pMem->n + ((pMem->flags & MEM_Zero)?pMem->u.i:0)
1986 == sqlite3VdbeSerialTypeLen(serial_type) );
1987 assert( pMem->n<=nBuf );
1988 len = pMem->n;
drhd946db02005-12-29 19:23:06 +00001989 memcpy(buf, pMem->z, len);
drhfdf972a2007-05-02 13:30:27 +00001990 if( pMem->flags & MEM_Zero ){
1991 len += pMem->u.i;
1992 if( len>nBuf ){
1993 len = nBuf;
1994 }
1995 memset(&buf[pMem->n], 0, len-pMem->n);
1996 }
drhd946db02005-12-29 19:23:06 +00001997 return len;
1998 }
1999
2000 /* NULL or constants 0 or 1 */
2001 return 0;
danielk1977cfcdaef2004-05-12 07:33:33 +00002002}
2003
2004/*
2005** Deserialize the data blob pointed to by buf as serial type serial_type
2006** and store the result in pMem. Return the number of bytes read.
2007*/
danielk1977b1bc9532004-05-22 03:05:33 +00002008int sqlite3VdbeSerialGet(
danielk197793d46752004-05-23 13:30:58 +00002009 const unsigned char *buf, /* Buffer to deserialize from */
drh25aa1b42004-05-28 01:39:01 +00002010 u32 serial_type, /* Serial type to deserialize */
2011 Mem *pMem /* Memory cell to write value into */
danielk1977b1bc9532004-05-22 03:05:33 +00002012){
drh3c685822005-05-21 18:32:18 +00002013 switch( serial_type ){
drh3c685822005-05-21 18:32:18 +00002014 case 10: /* Reserved for future use */
2015 case 11: /* Reserved for future use */
2016 case 0: { /* NULL */
2017 pMem->flags = MEM_Null;
2018 break;
2019 }
2020 case 1: { /* 1-byte signed integer */
drh3c024d62007-03-30 11:23:45 +00002021 pMem->u.i = (signed char)buf[0];
drh1483e142004-05-21 21:12:42 +00002022 pMem->flags = MEM_Int;
drh3c685822005-05-21 18:32:18 +00002023 return 1;
drh1483e142004-05-21 21:12:42 +00002024 }
drh3c685822005-05-21 18:32:18 +00002025 case 2: { /* 2-byte signed integer */
drh3c024d62007-03-30 11:23:45 +00002026 pMem->u.i = (((signed char)buf[0])<<8) | buf[1];
drh3c685822005-05-21 18:32:18 +00002027 pMem->flags = MEM_Int;
2028 return 2;
2029 }
2030 case 3: { /* 3-byte signed integer */
drh3c024d62007-03-30 11:23:45 +00002031 pMem->u.i = (((signed char)buf[0])<<16) | (buf[1]<<8) | buf[2];
drh3c685822005-05-21 18:32:18 +00002032 pMem->flags = MEM_Int;
2033 return 3;
2034 }
2035 case 4: { /* 4-byte signed integer */
drh3c024d62007-03-30 11:23:45 +00002036 pMem->u.i = (buf[0]<<24) | (buf[1]<<16) | (buf[2]<<8) | buf[3];
drh3c685822005-05-21 18:32:18 +00002037 pMem->flags = MEM_Int;
2038 return 4;
2039 }
2040 case 5: { /* 6-byte signed integer */
2041 u64 x = (((signed char)buf[0])<<8) | buf[1];
2042 u32 y = (buf[2]<<24) | (buf[3]<<16) | (buf[4]<<8) | buf[5];
2043 x = (x<<32) | y;
drh3c024d62007-03-30 11:23:45 +00002044 pMem->u.i = *(i64*)&x;
drh3c685822005-05-21 18:32:18 +00002045 pMem->flags = MEM_Int;
2046 return 6;
2047 }
drh91124b32005-08-18 18:15:05 +00002048 case 6: /* 8-byte signed integer */
drh3c685822005-05-21 18:32:18 +00002049 case 7: { /* IEEE floating point */
drhd81bd4e2005-09-05 20:06:49 +00002050 u64 x;
2051 u32 y;
drh2a3e4a72006-01-23 21:44:53 +00002052#if !defined(NDEBUG) && !defined(SQLITE_OMIT_FLOATING_POINT)
drhde941c62005-08-28 01:34:21 +00002053 /* Verify that integers and floating point values use the same
drh110daac2007-05-04 11:59:31 +00002054 ** byte order. Or, that if SQLITE_MIXED_ENDIAN_64BIT_FLOAT is
2055 ** defined that 64-bit floating point values really are mixed
2056 ** endian.
drhbfd6b032005-08-28 01:38:44 +00002057 */
drhde941c62005-08-28 01:34:21 +00002058 static const u64 t1 = ((u64)0x3ff00000)<<32;
drh4f0c5872007-03-26 22:05:01 +00002059 static const double r1 = 1.0;
drh60d09a72007-08-30 15:05:08 +00002060 u64 t2 = t1;
2061 swapMixedEndianFloat(t2);
2062 assert( sizeof(r1)==sizeof(t2) && memcmp(&r1, &t2, sizeof(r1))==0 );
drhde941c62005-08-28 01:34:21 +00002063#endif
drhbfd6b032005-08-28 01:38:44 +00002064
drhd81bd4e2005-09-05 20:06:49 +00002065 x = (buf[0]<<24) | (buf[1]<<16) | (buf[2]<<8) | buf[3];
2066 y = (buf[4]<<24) | (buf[5]<<16) | (buf[6]<<8) | buf[7];
drh3c685822005-05-21 18:32:18 +00002067 x = (x<<32) | y;
2068 if( serial_type==6 ){
drh3c024d62007-03-30 11:23:45 +00002069 pMem->u.i = *(i64*)&x;
drh3c685822005-05-21 18:32:18 +00002070 pMem->flags = MEM_Int;
2071 }else{
drh4f0c5872007-03-26 22:05:01 +00002072 assert( sizeof(x)==8 && sizeof(pMem->r)==8 );
drh60d09a72007-08-30 15:05:08 +00002073 swapMixedEndianFloat(x);
drh4f0c5872007-03-26 22:05:01 +00002074 memcpy(&pMem->r, &x, sizeof(x));
drh3c685822005-05-21 18:32:18 +00002075 pMem->flags = MEM_Real;
2076 }
2077 return 8;
2078 }
drhd946db02005-12-29 19:23:06 +00002079 case 8: /* Integer 0 */
2080 case 9: { /* Integer 1 */
drh3c024d62007-03-30 11:23:45 +00002081 pMem->u.i = serial_type-8;
drhd946db02005-12-29 19:23:06 +00002082 pMem->flags = MEM_Int;
2083 return 0;
2084 }
drh3c685822005-05-21 18:32:18 +00002085 default: {
2086 int len = (serial_type-12)/2;
2087 pMem->z = (char *)buf;
2088 pMem->n = len;
2089 pMem->xDel = 0;
2090 if( serial_type&0x01 ){
2091 pMem->flags = MEM_Str | MEM_Ephem;
2092 }else{
2093 pMem->flags = MEM_Blob | MEM_Ephem;
2094 }
2095 return len;
drh696b32f2004-05-30 01:51:52 +00002096 }
danielk1977cfcdaef2004-05-12 07:33:33 +00002097 }
drh3c685822005-05-21 18:32:18 +00002098 return 0;
danielk1977192ac1d2004-05-10 07:17:30 +00002099}
2100
2101/*
drh0e6082e2006-01-12 20:28:35 +00002102** The header of a record consists of a sequence variable-length integers.
2103** These integers are almost always small and are encoded as a single byte.
2104** The following macro takes advantage this fact to provide a fast decode
2105** of the integers in a record header. It is faster for the common case
2106** where the integer is a single byte. It is a little slower when the
2107** integer is two or more bytes. But overall it is faster.
2108**
2109** The following expressions are equivalent:
2110**
2111** x = sqlite3GetVarint32( A, &B );
2112**
2113** x = GetVarint( A, B );
2114**
2115*/
2116#define GetVarint(A,B) ((B = *(A))<=0x7f ? 1 : sqlite3GetVarint32(A, &B))
2117
2118/*
drh7a224de2004-06-02 01:22:02 +00002119** This function compares the two table rows or index records specified by
danielk1977eb015e02004-05-18 01:31:14 +00002120** {nKey1, pKey1} and {nKey2, pKey2}, returning a negative, zero
2121** or positive integer if {nKey1, pKey1} is less than, equal to or
drh7a224de2004-06-02 01:22:02 +00002122** greater than {nKey2, pKey2}. Both Key1 and Key2 must be byte strings
2123** composed by the OP_MakeRecord opcode of the VDBE.
danielk1977eb015e02004-05-18 01:31:14 +00002124*/
drh7a224de2004-06-02 01:22:02 +00002125int sqlite3VdbeRecordCompare(
danielk1977eb015e02004-05-18 01:31:14 +00002126 void *userData,
2127 int nKey1, const void *pKey1,
2128 int nKey2, const void *pKey2
2129){
drhd3d39e92004-05-20 22:16:29 +00002130 KeyInfo *pKeyInfo = (KeyInfo*)userData;
drhd3194f52004-05-27 19:59:32 +00002131 u32 d1, d2; /* Offset into aKey[] of next data element */
2132 u32 idx1, idx2; /* Offset into aKey[] of next header element */
2133 u32 szHdr1, szHdr2; /* Number of bytes in header */
2134 int i = 0;
2135 int nField;
2136 int rc = 0;
danielk1977eb015e02004-05-18 01:31:14 +00002137 const unsigned char *aKey1 = (const unsigned char *)pKey1;
2138 const unsigned char *aKey2 = (const unsigned char *)pKey2;
danielk19770202b292004-06-09 09:55:16 +00002139
2140 Mem mem1;
2141 Mem mem2;
2142 mem1.enc = pKeyInfo->enc;
drhb21c8cd2007-08-21 19:33:56 +00002143 mem1.db = pKeyInfo->db;
danielk19770202b292004-06-09 09:55:16 +00002144 mem2.enc = pKeyInfo->enc;
drhb21c8cd2007-08-21 19:33:56 +00002145 mem2.db = pKeyInfo->db;
drhd3194f52004-05-27 19:59:32 +00002146
drh0e6082e2006-01-12 20:28:35 +00002147 idx1 = GetVarint(aKey1, szHdr1);
drhd3194f52004-05-27 19:59:32 +00002148 d1 = szHdr1;
drh0e6082e2006-01-12 20:28:35 +00002149 idx2 = GetVarint(aKey2, szHdr2);
drhd3194f52004-05-27 19:59:32 +00002150 d2 = szHdr2;
2151 nField = pKeyInfo->nField;
drhd5788202004-05-28 08:21:05 +00002152 while( idx1<szHdr1 && idx2<szHdr2 ){
drhd3194f52004-05-27 19:59:32 +00002153 u32 serial_type1;
2154 u32 serial_type2;
danielk197784ac9d02004-05-18 09:58:06 +00002155
2156 /* Read the serial types for the next element in each key. */
drh0e6082e2006-01-12 20:28:35 +00002157 idx1 += GetVarint( aKey1+idx1, serial_type1 );
drhd5788202004-05-28 08:21:05 +00002158 if( d1>=nKey1 && sqlite3VdbeSerialTypeLen(serial_type1)>0 ) break;
drh0e6082e2006-01-12 20:28:35 +00002159 idx2 += GetVarint( aKey2+idx2, serial_type2 );
drhd5788202004-05-28 08:21:05 +00002160 if( d2>=nKey2 && sqlite3VdbeSerialTypeLen(serial_type2)>0 ) break;
danielk197784ac9d02004-05-18 09:58:06 +00002161
drh0660e262006-10-27 14:06:57 +00002162 /* Extract the values to be compared.
danielk197784ac9d02004-05-18 09:58:06 +00002163 */
drh25aa1b42004-05-28 01:39:01 +00002164 d1 += sqlite3VdbeSerialGet(&aKey1[d1], serial_type1, &mem1);
2165 d2 += sqlite3VdbeSerialGet(&aKey2[d2], serial_type2, &mem2);
danielk197784ac9d02004-05-18 09:58:06 +00002166
drh0660e262006-10-27 14:06:57 +00002167 /* Do the comparison
2168 */
drhd5788202004-05-28 08:21:05 +00002169 rc = sqlite3MemCompare(&mem1, &mem2, i<nField ? pKeyInfo->aColl[i] : 0);
drh3c685822005-05-21 18:32:18 +00002170 if( mem1.flags & MEM_Dyn ) sqlite3VdbeMemRelease(&mem1);
2171 if( mem2.flags & MEM_Dyn ) sqlite3VdbeMemRelease(&mem2);
danielk197784ac9d02004-05-18 09:58:06 +00002172 if( rc!=0 ){
drhd3194f52004-05-27 19:59:32 +00002173 break;
2174 }
2175 i++;
2176 }
2177
2178 /* One of the keys ran out of fields, but all the fields up to that point
2179 ** were equal. If the incrKey flag is true, then the second key is
2180 ** treated as larger.
2181 */
2182 if( rc==0 ){
2183 if( pKeyInfo->incrKey ){
drhd3194f52004-05-27 19:59:32 +00002184 rc = -1;
danielk19779a96b662007-11-29 17:05:18 +00002185 }else if( !pKeyInfo->prefixIsEqual ){
2186 if( d1<nKey1 ){
2187 rc = 1;
2188 }else if( d2<nKey2 ){
2189 rc = -1;
2190 }
danielk197784ac9d02004-05-18 09:58:06 +00002191 }
drh0b2f3162005-12-21 18:36:45 +00002192 }else if( pKeyInfo->aSortOrder && i<pKeyInfo->nField
2193 && pKeyInfo->aSortOrder[i] ){
drhd3194f52004-05-27 19:59:32 +00002194 rc = -rc;
2195 }
2196
2197 return rc;
danielk1977eb015e02004-05-18 01:31:14 +00002198}
drhd5788202004-05-28 08:21:05 +00002199
2200/*
drh7a224de2004-06-02 01:22:02 +00002201** The argument is an index entry composed using the OP_MakeRecord opcode.
2202** The last entry in this record should be an integer (specifically
2203** an integer rowid). This routine returns the number of bytes in
2204** that integer.
drhd5788202004-05-28 08:21:05 +00002205*/
drh74161702006-02-24 02:53:49 +00002206int sqlite3VdbeIdxRowidLen(const u8 *aKey){
drhd5788202004-05-28 08:21:05 +00002207 u32 szHdr; /* Size of the header */
2208 u32 typeRowid; /* Serial type of the rowid */
2209
2210 sqlite3GetVarint32(aKey, &szHdr);
2211 sqlite3GetVarint32(&aKey[szHdr-1], &typeRowid);
2212 return sqlite3VdbeSerialTypeLen(typeRowid);
2213}
danielk1977eb015e02004-05-18 01:31:14 +00002214
2215
2216/*
drh7a224de2004-06-02 01:22:02 +00002217** pCur points at an index entry created using the OP_MakeRecord opcode.
2218** Read the rowid (the last field in the record) and store it in *rowid.
2219** Return SQLITE_OK if everything works, or an error code otherwise.
danielk1977183f9f72004-05-13 05:20:26 +00002220*/
drhb21c8cd2007-08-21 19:33:56 +00002221int sqlite3VdbeIdxRowid(BtCursor *pCur, i64 *rowid){
drh61fc5952007-04-01 23:49:51 +00002222 i64 nCellKey = 0;
danielk1977183f9f72004-05-13 05:20:26 +00002223 int rc;
drhd5788202004-05-28 08:21:05 +00002224 u32 szHdr; /* Size of the header */
2225 u32 typeRowid; /* Serial type of the rowid */
2226 u32 lenRowid; /* Size of the rowid */
2227 Mem m, v;
danielk1977183f9f72004-05-13 05:20:26 +00002228
drhd5788202004-05-28 08:21:05 +00002229 sqlite3BtreeKeySize(pCur, &nCellKey);
2230 if( nCellKey<=0 ){
drh49285702005-09-17 15:20:26 +00002231 return SQLITE_CORRUPT_BKPT;
drhd5788202004-05-28 08:21:05 +00002232 }
drhb21c8cd2007-08-21 19:33:56 +00002233 rc = sqlite3VdbeMemFromBtree(pCur, 0, nCellKey, 1, &m);
drhd5788202004-05-28 08:21:05 +00002234 if( rc ){
danielk1977183f9f72004-05-13 05:20:26 +00002235 return rc;
2236 }
drh2646da72005-12-09 20:02:05 +00002237 sqlite3GetVarint32((u8*)m.z, &szHdr);
2238 sqlite3GetVarint32((u8*)&m.z[szHdr-1], &typeRowid);
drhd5788202004-05-28 08:21:05 +00002239 lenRowid = sqlite3VdbeSerialTypeLen(typeRowid);
drh2646da72005-12-09 20:02:05 +00002240 sqlite3VdbeSerialGet((u8*)&m.z[m.n-lenRowid], typeRowid, &v);
drh3c024d62007-03-30 11:23:45 +00002241 *rowid = v.u.i;
danielk1977d8123362004-06-12 09:25:12 +00002242 sqlite3VdbeMemRelease(&m);
danielk1977183f9f72004-05-13 05:20:26 +00002243 return SQLITE_OK;
2244}
2245
drh7cf6e4d2004-05-19 14:56:55 +00002246/*
drhd3d39e92004-05-20 22:16:29 +00002247** Compare the key of the index entry that cursor pC is point to against
drh7cf6e4d2004-05-19 14:56:55 +00002248** the key string in pKey (of length nKey). Write into *pRes a number
2249** that is negative, zero, or positive if pC is less than, equal to,
2250** or greater than pKey. Return SQLITE_OK on success.
drhd3d39e92004-05-20 22:16:29 +00002251**
drhd5788202004-05-28 08:21:05 +00002252** pKey is either created without a rowid or is truncated so that it
2253** omits the rowid at the end. The rowid at the end of the index entry
2254** is ignored as well.
drh7cf6e4d2004-05-19 14:56:55 +00002255*/
danielk1977183f9f72004-05-13 05:20:26 +00002256int sqlite3VdbeIdxKeyCompare(
drh7cf6e4d2004-05-19 14:56:55 +00002257 Cursor *pC, /* The cursor to compare against */
2258 int nKey, const u8 *pKey, /* The key to compare */
2259 int *res /* Write the comparison result here */
danielk1977183f9f72004-05-13 05:20:26 +00002260){
drh61fc5952007-04-01 23:49:51 +00002261 i64 nCellKey = 0;
danielk1977183f9f72004-05-13 05:20:26 +00002262 int rc;
danielk19773d1bfea2004-05-14 11:00:53 +00002263 BtCursor *pCur = pC->pCursor;
drhd5788202004-05-28 08:21:05 +00002264 int lenRowid;
2265 Mem m;
danielk1977183f9f72004-05-13 05:20:26 +00002266
2267 sqlite3BtreeKeySize(pCur, &nCellKey);
2268 if( nCellKey<=0 ){
2269 *res = 0;
2270 return SQLITE_OK;
2271 }
drhb21c8cd2007-08-21 19:33:56 +00002272 rc = sqlite3VdbeMemFromBtree(pC->pCursor, 0, nCellKey, 1, &m);
drhd5788202004-05-28 08:21:05 +00002273 if( rc ){
2274 return rc;
danielk1977183f9f72004-05-13 05:20:26 +00002275 }
drh74161702006-02-24 02:53:49 +00002276 lenRowid = sqlite3VdbeIdxRowidLen((u8*)m.z);
drh7a224de2004-06-02 01:22:02 +00002277 *res = sqlite3VdbeRecordCompare(pC->pKeyInfo, m.n-lenRowid, m.z, nKey, pKey);
danielk1977d8123362004-06-12 09:25:12 +00002278 sqlite3VdbeMemRelease(&m);
danielk1977183f9f72004-05-13 05:20:26 +00002279 return SQLITE_OK;
2280}
danielk1977b28af712004-06-21 06:50:26 +00002281
2282/*
2283** This routine sets the value to be returned by subsequent calls to
2284** sqlite3_changes() on the database handle 'db'.
2285*/
2286void sqlite3VdbeSetChanges(sqlite3 *db, int nChange){
drhb21c8cd2007-08-21 19:33:56 +00002287 assert( sqlite3_mutex_held(db->mutex) );
danielk1977b28af712004-06-21 06:50:26 +00002288 db->nChange = nChange;
2289 db->nTotalChange += nChange;
2290}
2291
2292/*
2293** Set a flag in the vdbe to update the change counter when it is finalised
2294** or reset.
2295*/
drh4794f732004-11-05 17:17:50 +00002296void sqlite3VdbeCountChanges(Vdbe *v){
2297 v->changeCntOn = 1;
danielk1977b28af712004-06-21 06:50:26 +00002298}
drhd89bd002005-01-22 03:03:54 +00002299
2300/*
2301** Mark every prepared statement associated with a database connection
2302** as expired.
2303**
2304** An expired statement means that recompilation of the statement is
2305** recommend. Statements expire when things happen that make their
2306** programs obsolete. Removing user-defined functions or collating
2307** sequences, or changing an authorization function are the types of
2308** things that make prepared statements obsolete.
2309*/
2310void sqlite3ExpirePreparedStatements(sqlite3 *db){
2311 Vdbe *p;
2312 for(p = db->pVdbe; p; p=p->pNext){
2313 p->expired = 1;
2314 }
2315}
danielk1977aee18ef2005-03-09 12:26:50 +00002316
2317/*
2318** Return the database associated with the Vdbe.
2319*/
2320sqlite3 *sqlite3VdbeDb(Vdbe *v){
2321 return v->db;
2322}