1
1.1 (drh 06-Sep-03): /*
2
1.1 (drh 06-Sep-03): ** 2003 September 6
3
1.1 (drh 06-Sep-03): **
4
1.1 (drh 06-Sep-03): ** The author disclaims copyright to this source code. In place of
5
1.1 (drh 06-Sep-03): ** a legal notice, here is a blessing:
6
1.1 (drh 06-Sep-03): **
7
1.1 (drh 06-Sep-03): ** May you do good and not evil.
8
1.1 (drh 06-Sep-03): ** May you find forgiveness for yourself and forgive others.
9
1.1 (drh 06-Sep-03): ** May you share freely, never taking more than you give.
10
1.1 (drh 06-Sep-03): **
11
1.1 (drh 06-Sep-03): *************************************************************************
12
1.1 (drh 06-Sep-03): ** This file contains code used for creating, destroying, and populating
13
1.65 (danielk1 26-May-04): ** a VDBE (or an "sqlite3_stmt" as it is known to the outside world.) Prior
14
1.1 (drh 06-Sep-03): ** to version 2.8.7, all this code was combined into the vdbe.c source file.
15
1.1 (drh 06-Sep-03): ** But that file was getting too big so this subroutines were split out.
16
1.1 (drh 06-Sep-03): */
17
1.1 (drh 06-Sep-03): #include "sqliteInt.h"
18
1.1 (drh 06-Sep-03): #include "os.h"
19
1.1 (drh 06-Sep-03): #include <ctype.h>
20
1.1 (drh 06-Sep-03): #include "vdbeInt.h"
23
1.1 (drh 06-Sep-03): /*
24
1.1 (drh 06-Sep-03): ** When debugging the code generator in a symbolic debugger, one can
25
1.24 (danielk1 10-May-04): ** set the sqlite3_vdbe_addop_trace to 1 and all opcodes will be printed
26
1.1 (drh 06-Sep-03): ** as they are added to the instruction stream.
27
1.1 (drh 06-Sep-03): */
28
1.182 (drh 14-Jun-05): #ifdef SQLITE_DEBUG
29
1.24 (danielk1 10-May-04): int sqlite3_vdbe_addop_trace = 0;
30
1.1 (drh 06-Sep-03): #endif
33
1.1 (drh 06-Sep-03): /*
34
1.1 (drh 06-Sep-03): ** Create a new virtual database engine.
35
1.1 (drh 06-Sep-03): */
36
1.140 (drh 06-Sep-04): Vdbe *sqlite3VdbeCreate(sqlite3 *db){
37
1.1 (drh 06-Sep-03): Vdbe *p;
38
1.1 (drh 06-Sep-03): p = sqliteMalloc( sizeof(Vdbe) );
39
1.1 (drh 06-Sep-03): if( p==0 ) return 0;
40
1.1 (drh 06-Sep-03): p->db = db;
41
1.1 (drh 06-Sep-03): if( db->pVdbe ){
42
1.1 (drh 06-Sep-03): db->pVdbe->pPrev = p;
43
1.1 (drh 06-Sep-03): }
44
1.1 (drh 06-Sep-03): p->pNext = db->pVdbe;
45
1.1 (drh 06-Sep-03): p->pPrev = 0;
46
1.1 (drh 06-Sep-03): db->pVdbe = p;
47
1.1 (drh 06-Sep-03): p->magic = VDBE_MAGIC_INIT;
48
1.1 (drh 06-Sep-03): return p;
49
1.1 (drh 06-Sep-03): }
51
1.1 (drh 06-Sep-03): /*
52
1.268 (drh 09-Nov-06): ** Remember the SQL string for a prepared statement.
53
1.268 (drh 09-Nov-06): */
54
1.268 (drh 09-Nov-06): void sqlite3VdbeSetSql(Vdbe *p, const char *z, int n){
55
1.268 (drh 09-Nov-06): if( p==0 ) return;
56
1.268 (drh 09-Nov-06): assert( p->zSql==0 );
57
1.268 (drh 09-Nov-06): p->zSql = sqlite3StrNDup(z, n);
58
1.268 (drh 09-Nov-06): }
59
1.268 (drh 09-Nov-06):
60
1.268 (drh 09-Nov-06): /*
61
1.268 (drh 09-Nov-06): ** Return the SQL associated with a prepared statement
62
1.268 (drh 09-Nov-06): */
63
1.268 (drh 09-Nov-06): const char *sqlite3VdbeGetSql(Vdbe *p){
64
1.268 (drh 09-Nov-06): return p->zSql;
65
1.268 (drh 09-Nov-06): }
66
1.268 (drh 09-Nov-06):
67
1.268 (drh 09-Nov-06): /*
68
1.269 (drh 08-Jan-07): ** Swap all content between two VDBE structures.
69
1.268 (drh 09-Nov-06): */
70
1.269 (drh 08-Jan-07): void sqlite3VdbeSwap(Vdbe *pA, Vdbe *pB){
71
1.269 (drh 08-Jan-07): Vdbe tmp, *pTmp;
72
1.269 (drh 08-Jan-07): char *zTmp;
73
1.269 (drh 08-Jan-07): int nTmp;
74
1.269 (drh 08-Jan-07): tmp = *pA;
75
1.269 (drh 08-Jan-07): *pA = *pB;
76
1.269 (drh 08-Jan-07): *pB = tmp;
77
1.269 (drh 08-Jan-07): pTmp = pA->pNext;
78
1.269 (drh 08-Jan-07): pA->pNext = pB->pNext;
79
1.269 (drh 08-Jan-07): pB->pNext = pTmp;
80
1.269 (drh 08-Jan-07): pTmp = pA->pPrev;
81
1.269 (drh 08-Jan-07): pA->pPrev = pB->pPrev;
82
1.269 (drh 08-Jan-07): pB->pPrev = pTmp;
83
1.269 (drh 08-Jan-07): zTmp = pA->zSql;
84
1.269 (drh 08-Jan-07): pA->zSql = pB->zSql;
85
1.269 (drh 08-Jan-07): pB->zSql = zTmp;
86
1.269 (drh 08-Jan-07): nTmp = pA->nSql;
87
1.269 (drh 08-Jan-07): pA->nSql = pB->nSql;
88
1.269 (drh 08-Jan-07): pB->nSql = nTmp;
89
1.268 (drh 09-Nov-06): }
90
1.268 (drh 09-Nov-06):
91
1.268 (drh 09-Nov-06): /*
92
1.1 (drh 06-Sep-03): ** Turn tracing on or off
93
1.1 (drh 06-Sep-03): */
94
1.19 (danielk1 08-May-04): void sqlite3VdbeTrace(Vdbe *p, FILE *trace){
95
1.1 (drh 06-Sep-03): p->trace = trace;
96
1.1 (drh 06-Sep-03): }
98
1.1 (drh 06-Sep-03): /*
99
1.146 (drh 24-Sep-04): ** Resize the Vdbe.aOp array so that it contains at least N
100
1.170 (danielk1 28-Mar-05): ** elements. If the Vdbe is in VDBE_MAGIC_RUN state, then
101
1.235 (danielk1 26-Jan-06): ** the Vdbe.aOp array will be sized to contain exactly N
102
1.235 (danielk1 26-Jan-06): ** elements. Vdbe.nOpAlloc is set to reflect the new size of
103
1.235 (danielk1 26-Jan-06): ** the array.
104
1.235 (danielk1 26-Jan-06): **
105
1.235 (danielk1 26-Jan-06): ** If an out-of-memory error occurs while resizing the array,
106
1.235 (danielk1 26-Jan-06): ** Vdbe.aOp and Vdbe.nOpAlloc remain unchanged (this is so that
107
1.235 (danielk1 26-Jan-06): ** any opcodes already allocated can be correctly deallocated
108
1.235 (danielk1 26-Jan-06): ** along with the rest of the Vdbe).
109
1.146 (drh 24-Sep-04): */
110
1.146 (drh 24-Sep-04): static void resizeOpArray(Vdbe *p, int N){
111
1.199 (drh 16-Sep-05): int runMode = p->magic==VDBE_MAGIC_RUN;
112
1.199 (drh 16-Sep-05): if( runMode || p->nOpAlloc<N ){
113
1.199 (drh 16-Sep-05): VdbeOp *pNew;
114
1.199 (drh 16-Sep-05): int nNew = N + 100*(!runMode);
115
1.146 (drh 24-Sep-04): int oldSize = p->nOpAlloc;
116
1.199 (drh 16-Sep-05): pNew = sqliteRealloc(p->aOp, nNew*sizeof(Op));
117
1.198 (drh 16-Sep-05): if( pNew ){
118
1.199 (drh 16-Sep-05): p->nOpAlloc = nNew;
119
1.198 (drh 16-Sep-05): p->aOp = pNew;
120
1.199 (drh 16-Sep-05): if( nNew>oldSize ){
121
1.199 (drh 16-Sep-05): memset(&p->aOp[oldSize], 0, (nNew-oldSize)*sizeof(Op));
122
1.199 (drh 16-Sep-05): }
123
1.146 (drh 24-Sep-04): }
124
1.146 (drh 24-Sep-04): }
125
1.146 (drh 24-Sep-04): }
126
1.146 (drh 24-Sep-04):
127
1.146 (drh 24-Sep-04): /*
128
1.1 (drh 06-Sep-03): ** Add a new instruction to the list of instructions current in the
129
1.1 (drh 06-Sep-03): ** VDBE. Return the address of the new instruction.
130
1.1 (drh 06-Sep-03): **
131
1.1 (drh 06-Sep-03): ** Parameters:
132
1.1 (drh 06-Sep-03): **
133
1.1 (drh 06-Sep-03): ** p Pointer to the VDBE
134
1.1 (drh 06-Sep-03): **
135
1.1 (drh 06-Sep-03): ** op The opcode for this instruction
136
1.1 (drh 06-Sep-03): **
137
1.1 (drh 06-Sep-03): ** p1, p2 First two of the three possible operands.
138
1.1 (drh 06-Sep-03): **
139
1.19 (danielk1 08-May-04): ** Use the sqlite3VdbeResolveLabel() function to fix an address and
140
1.19 (danielk1 08-May-04): ** the sqlite3VdbeChangeP3() function to change the value of the P3
141
1.1 (drh 06-Sep-03): ** operand.
142
1.1 (drh 06-Sep-03): */
143
1.19 (danielk1 08-May-04): int sqlite3VdbeAddOp(Vdbe *p, int op, int p1, int p2){
144
1.1 (drh 06-Sep-03): int i;
145
1.18 (drh 22-Feb-04): VdbeOp *pOp;
147
1.1 (drh 06-Sep-03): i = p->nOp;
148
1.1 (drh 06-Sep-03): p->nOp++;
149
1.1 (drh 06-Sep-03): assert( p->magic==VDBE_MAGIC_INIT );
150
1.241 (drh 15-Mar-06): if( p->nOpAlloc<=i ){
151
1.241 (drh 15-Mar-06): resizeOpArray(p, i+1);
152
1.241 (drh 15-Mar-06): if( sqlite3MallocFailed() ){
153
1.241 (drh 15-Mar-06): return 0;
154
1.241 (drh 15-Mar-06): }
155
1.1 (drh 06-Sep-03): }
156
1.18 (drh 22-Feb-04): pOp = &p->aOp[i];
157
1.18 (drh 22-Feb-04): pOp->opcode = op;
158
1.18 (drh 22-Feb-04): pOp->p1 = p1;
159
1.18 (drh 22-Feb-04): pOp->p2 = p2;
160
1.18 (drh 22-Feb-04): pOp->p3 = 0;
161
1.18 (drh 22-Feb-04): pOp->p3type = P3_NOTUSED;
162
1.185 (drh 14-Aug-05): p->expired = 0;
163
1.156 (danielk1 12-Jan-05): #ifdef SQLITE_DEBUG
164
1.24 (danielk1 10-May-04): if( sqlite3_vdbe_addop_trace ) sqlite3VdbePrintOp(0, i, &p->aOp[i]);
165
1.1 (drh 06-Sep-03): #endif
166
1.1 (drh 06-Sep-03): return i;
167
1.1 (drh 06-Sep-03): }
168
1.18 (drh 22-Feb-04):
169
1.18 (drh 22-Feb-04): /*
170
1.18 (drh 22-Feb-04): ** Add an opcode that includes the p3 value.
171
1.18 (drh 22-Feb-04): */
172
1.69 (drh 27-May-04): int sqlite3VdbeOp3(Vdbe *p, int op, int p1, int p2, const char *zP3,int p3type){
173
1.19 (danielk1 08-May-04): int addr = sqlite3VdbeAddOp(p, op, p1, p2);
174
1.19 (danielk1 08-May-04): sqlite3VdbeChangeP3(p, addr, zP3, p3type);
175
1.18 (drh 22-Feb-04): return addr;
176
1.18 (drh 22-Feb-04): }
177
1.18 (drh 22-Feb-04):
178
1.18 (drh 22-Feb-04): /*
179
1.1 (drh 06-Sep-03): ** Create a new symbolic label for an instruction that has yet to be
180
1.1 (drh 06-Sep-03): ** coded. The symbolic label is really just a negative number. The
181
1.1 (drh 06-Sep-03): ** label can be used as the P2 value of an operation. Later, when
182
1.1 (drh 06-Sep-03): ** the label is resolved to a specific address, the VDBE will scan
183
1.1 (drh 06-Sep-03): ** through its operation list and change all values of P2 which match
184
1.1 (drh 06-Sep-03): ** the label into the resolved address.
185
1.1 (drh 06-Sep-03): **
186
1.1 (drh 06-Sep-03): ** The VDBE knows that a P2 value is a label because labels are
187
1.1 (drh 06-Sep-03): ** always negative and P2 values are suppose to be non-negative.
188
1.1 (drh 06-Sep-03): ** Hence, a negative P2 value is a label that has yet to be resolved.
189
1.129 (danielk1 26-Jun-04): **
190
1.129 (danielk1 26-Jun-04): ** Zero is returned if a malloc() fails.
191
1.1 (drh 06-Sep-03): */
192
1.19 (danielk1 08-May-04): int sqlite3VdbeMakeLabel(Vdbe *p){
193
1.1 (drh 06-Sep-03): int i;
194
1.1 (drh 06-Sep-03): i = p->nLabel++;
195
1.1 (drh 06-Sep-03): assert( p->magic==VDBE_MAGIC_INIT );
196
1.1 (drh 06-Sep-03): if( i>=p->nLabelAlloc ){
197
1.1 (drh 06-Sep-03): p->nLabelAlloc = p->nLabelAlloc*2 + 10;
198
1.224 (danielk1 13-Jan-06): sqliteReallocOrFree((void**)&p->aLabel,
199
1.199 (drh 16-Sep-05): p->nLabelAlloc*sizeof(p->aLabel[0]));
200
1.146 (drh 24-Sep-04): }
201
1.146 (drh 24-Sep-04): if( p->aLabel ){
202
1.146 (drh 24-Sep-04): p->aLabel[i] = -1;
203
1.1 (drh 06-Sep-03): }
204
1.1 (drh 06-Sep-03): return -1-i;
205
1.1 (drh 06-Sep-03): }
207
1.1 (drh 06-Sep-03): /*
208
1.1 (drh 06-Sep-03): ** Resolve label "x" to be the address of the next instruction to
209
1.1 (drh 06-Sep-03): ** be inserted. The parameter "x" must have been obtained from
210
1.19 (danielk1 08-May-04): ** a prior call to sqlite3VdbeMakeLabel().
211
1.1 (drh 06-Sep-03): */
212
1.19 (danielk1 08-May-04): void sqlite3VdbeResolveLabel(Vdbe *p, int x){
213
1.146 (drh 24-Sep-04): int j = -1-x;
214
1.1 (drh 06-Sep-03): assert( p->magic==VDBE_MAGIC_INIT );
215
1.146 (drh 24-Sep-04): assert( j>=0 && j<p->nLabel );
216
1.146 (drh 24-Sep-04): if( p->aLabel ){
217
1.146 (drh 24-Sep-04): p->aLabel[j] = p->nOp;
218
1.146 (drh 24-Sep-04): }
219
1.146 (drh 24-Sep-04): }
220
1.146 (drh 24-Sep-04):
221
1.146 (drh 24-Sep-04): /*
222
1.171 (danielk1 29-Mar-05): ** Return non-zero if opcode 'op' is guarenteed not to push more values
223
1.171 (danielk1 29-Mar-05): ** onto the VDBE stack than it pops off.
224
1.171 (danielk1 29-Mar-05): */
225
1.172 (danielk1 29-Mar-05): static int opcodeNoPush(u8 op){
226
1.172 (danielk1 29-Mar-05): /* The 10 NOPUSH_MASK_n constants are defined in the automatically
227
1.171 (danielk1 29-Mar-05): ** generated header file opcodes.h. Each is a 16-bit bitmask, one
228
1.171 (danielk1 29-Mar-05): ** bit corresponding to each opcode implemented by the virtual
229
1.172 (danielk1 29-Mar-05): ** machine in vdbe.c. The bit is true if the word "no-push" appears
230
1.171 (danielk1 29-Mar-05): ** in a comment on the same line as the "case OP_XXX:" in
231
1.171 (danielk1 29-Mar-05): ** sqlite3VdbeExec() in vdbe.c.
232
1.171 (danielk1 29-Mar-05): **
233
1.171 (danielk1 29-Mar-05): ** If the bit is true, then the corresponding opcode is guarenteed not
234
1.171 (danielk1 29-Mar-05): ** to grow the stack when it is executed. Otherwise, it may grow the
235
1.171 (danielk1 29-Mar-05): ** stack by at most one entry.
236
1.171 (danielk1 29-Mar-05): **
237
1.172 (danielk1 29-Mar-05): ** NOPUSH_MASK_0 corresponds to opcodes 0 to 15. NOPUSH_MASK_1 contains
238
1.171 (danielk1 29-Mar-05): ** one bit for opcodes 16 to 31, and so on.
239
1.171 (danielk1 29-Mar-05): **
240
1.171 (danielk1 29-Mar-05): ** 16-bit bitmasks (rather than 32-bit) are specified in opcodes.h
241
1.171 (danielk1 29-Mar-05): ** because the file is generated by an awk program. Awk manipulates
242
1.171 (danielk1 29-Mar-05): ** all numbers as floating-point and we don't want to risk a rounding
243
1.171 (danielk1 29-Mar-05): ** error if someone builds with an awk that uses (for example) 32-bit
244
1.171 (danielk1 29-Mar-05): ** IEEE floats.
245
1.171 (danielk1 29-Mar-05): */
246
1.173 (drh 31-Mar-05): static const u32 masks[5] = {
247
1.238 (drh 24-Feb-06): NOPUSH_MASK_0 + (((unsigned)NOPUSH_MASK_1)<<16),
248
1.238 (drh 24-Feb-06): NOPUSH_MASK_2 + (((unsigned)NOPUSH_MASK_3)<<16),
249
1.238 (drh 24-Feb-06): NOPUSH_MASK_4 + (((unsigned)NOPUSH_MASK_5)<<16),
250
1.238 (drh 24-Feb-06): NOPUSH_MASK_6 + (((unsigned)NOPUSH_MASK_7)<<16),
251
1.238 (drh 24-Feb-06): NOPUSH_MASK_8 + (((unsigned)NOPUSH_MASK_9)<<16)
252
1.171 (danielk1 29-Mar-05): };
253
1.206 (drh 29-Nov-05): assert( op<32*5 );
254
1.171 (danielk1 29-Mar-05): return (masks[op>>5] & (1<<(op&0x1F)));
255
1.171 (danielk1 29-Mar-05): }
256
1.171 (danielk1 29-Mar-05):
257
1.171 (danielk1 29-Mar-05): #ifndef NDEBUG
258
1.172 (danielk1 29-Mar-05): int sqlite3VdbeOpcodeNoPush(u8 op){
259
1.172 (danielk1 29-Mar-05): return opcodeNoPush(op);
260
1.171 (danielk1 29-Mar-05): }
261
1.171 (danielk1 29-Mar-05): #endif
262
1.171 (danielk1 29-Mar-05):
263
1.171 (danielk1 29-Mar-05): /*
264
1.146 (drh 24-Sep-04): ** Loop through the program looking for P2 values that are negative.
265
1.146 (drh 24-Sep-04): ** Each such value is a label. Resolve the label by setting the P2
266
1.146 (drh 24-Sep-04): ** value to its correct non-zero value.
267
1.146 (drh 24-Sep-04): **
268
1.146 (drh 24-Sep-04): ** This routine is called once after all opcodes have been inserted.
269
1.170 (danielk1 28-Mar-05): **
270
1.195 (drh 07-Sep-05): ** Variable *pMaxFuncArgs is set to the maximum value of any P2 argument
271
1.247 (danielk1 14-Jun-06): ** to an OP_Function, OP_AggStep or OP_VFilter opcode. This is used by
272
1.170 (danielk1 28-Mar-05): ** sqlite3VdbeMakeReady() to size the Vdbe.apArg[] array.
273
1.171 (danielk1 29-Mar-05): **
274
1.171 (danielk1 29-Mar-05): ** The integer *pMaxStack is set to the maximum number of vdbe stack
275
1.171 (danielk1 29-Mar-05): ** entries that static analysis reveals this program might need.
276
1.180 (drh 07-Jun-05): **
277
1.180 (drh 07-Jun-05): ** This routine also does the following optimization: It scans for
278
1.180 (drh 07-Jun-05): ** Halt instructions where P1==SQLITE_CONSTRAINT or P2==OE_Abort or for
279
1.181 (drh 12-Jun-05): ** IdxInsert instructions where P2!=0. If no such instruction is
280
1.180 (drh 07-Jun-05): ** found, then every Statement instruction is changed to a Noop. In
281
1.180 (drh 07-Jun-05): ** this way, we avoid creating the statement journal file unnecessarily.
282
1.146 (drh 24-Sep-04): */
283
1.171 (danielk1 29-Mar-05): static void resolveP2Values(Vdbe *p, int *pMaxFuncArgs, int *pMaxStack){
284
1.146 (drh 24-Sep-04): int i;
285
1.171 (danielk1 29-Mar-05): int nMaxArgs = 0;
286
1.171 (danielk1 29-Mar-05): int nMaxStack = p->nOp;
287
1.146 (drh 24-Sep-04): Op *pOp;
288
1.146 (drh 24-Sep-04): int *aLabel = p->aLabel;
289
1.180 (drh 07-Jun-05): int doesStatementRollback = 0;
290
1.180 (drh 07-Jun-05): int hasStatementBegin = 0;
291
1.146 (drh 24-Sep-04): for(pOp=p->aOp, i=p->nOp-1; i>=0; i--, pOp++){
292
1.170 (danielk1 28-Mar-05): u8 opcode = pOp->opcode;
293
1.170 (danielk1 28-Mar-05):
294
1.251 (danielk1 20-Jun-06): if( opcode==OP_Function || opcode==OP_AggStep
295
1.247 (danielk1 14-Jun-06): #ifndef SQLITE_OMIT_VIRTUALTABLE
296
1.251 (danielk1 20-Jun-06): || opcode==OP_VUpdate
297
1.247 (danielk1 14-Jun-06): #endif
298
1.247 (danielk1 14-Jun-06): ){
299
1.171 (danielk1 29-Mar-05): if( pOp->p2>nMaxArgs ) nMaxArgs = pOp->p2;
300
1.180 (drh 07-Jun-05): }else if( opcode==OP_Halt ){
301
1.180 (drh 07-Jun-05): if( pOp->p1==SQLITE_CONSTRAINT && pOp->p2==OE_Abort ){
302
1.180 (drh 07-Jun-05): doesStatementRollback = 1;
303
1.180 (drh 07-Jun-05): }
304
1.180 (drh 07-Jun-05): }else if( opcode==OP_Statement ){
305
1.180 (drh 07-Jun-05): hasStatementBegin = 1;
306
1.246 (drh 13-Jun-06): }else if( opcode==OP_VFilter ){
307
1.246 (drh 13-Jun-06): int n;
308
1.246 (drh 13-Jun-06): assert( p->nOp - i >= 3 );
309
1.246 (drh 13-Jun-06): assert( pOp[-2].opcode==OP_Integer );
310
1.246 (drh 13-Jun-06): n = pOp[-2].p1;
311
1.246 (drh 13-Jun-06): if( n>nMaxArgs ) nMaxArgs = n;
312
1.171 (danielk1 29-Mar-05): }
313
1.172 (danielk1 29-Mar-05): if( opcodeNoPush(opcode) ){
314
1.171 (danielk1 29-Mar-05): nMaxStack--;
315
1.170 (danielk1 28-Mar-05): }
316
1.170 (danielk1 28-Mar-05):
317
1.146 (drh 24-Sep-04): if( pOp->p2>=0 ) continue;
318
1.146 (drh 24-Sep-04): assert( -1-pOp->p2<p->nLabel );
319
1.146 (drh 24-Sep-04): pOp->p2 = aLabel[-1-pOp->p2];
320
1.1 (drh 06-Sep-03): }
321
1.146 (drh 24-Sep-04): sqliteFree(p->aLabel);
322
1.146 (drh 24-Sep-04): p->aLabel = 0;
323
1.171 (danielk1 29-Mar-05):
324
1.171 (danielk1 29-Mar-05): *pMaxFuncArgs = nMaxArgs;
325
1.171 (danielk1 29-Mar-05): *pMaxStack = nMaxStack;
326
1.180 (drh 07-Jun-05):
327
1.180 (drh 07-Jun-05): /* If we never rollback a statement transaction, then statement
328
1.180 (drh 07-Jun-05): ** transactions are not needed. So change every OP_Statement
329
1.218 (drh 06-Jan-06): ** opcode into an OP_Noop. This avoid a call to sqlite3OsOpenExclusive()
330
1.180 (drh 07-Jun-05): ** which can be expensive on some platforms.
331
1.180 (drh 07-Jun-05): */
332
1.180 (drh 07-Jun-05): if( hasStatementBegin && !doesStatementRollback ){
333
1.180 (drh 07-Jun-05): for(pOp=p->aOp, i=p->nOp-1; i>=0; i--, pOp++){
334
1.180 (drh 07-Jun-05): if( pOp->opcode==OP_Statement ){
335
1.180 (drh 07-Jun-05): pOp->opcode = OP_Noop;
336
1.180 (drh 07-Jun-05): }
337
1.180 (drh 07-Jun-05): }
338
1.180 (drh 07-Jun-05): }
339
1.1 (drh 06-Sep-03): }
341
1.1 (drh 06-Sep-03): /*
342
1.1 (drh 06-Sep-03): ** Return the address of the next instruction to be inserted.
343
1.1 (drh 06-Sep-03): */
344
1.19 (danielk1 08-May-04): int sqlite3VdbeCurrentAddr(Vdbe *p){
345
1.1 (drh 06-Sep-03): assert( p->magic==VDBE_MAGIC_INIT );
346
1.1 (drh 06-Sep-03): return p->nOp;
347
1.1 (drh 06-Sep-03): }
349
1.1 (drh 06-Sep-03): /*
350
1.1 (drh 06-Sep-03): ** Add a whole list of operations to the operation stack. Return the
351
1.1 (drh 06-Sep-03): ** address of the first operation added.
352
1.1 (drh 06-Sep-03): */
353
1.19 (danielk1 08-May-04): int sqlite3VdbeAddOpList(Vdbe *p, int nOp, VdbeOpList const *aOp){
354
1.1 (drh 06-Sep-03): int addr;
355
1.1 (drh 06-Sep-03): assert( p->magic==VDBE_MAGIC_INIT );
356
1.146 (drh 24-Sep-04): resizeOpArray(p, p->nOp + nOp);
357
1.227 (danielk1 18-Jan-06): if( sqlite3MallocFailed() ){
358
1.146 (drh 24-Sep-04): return 0;
359
1.1 (drh 06-Sep-03): }
360
1.1 (drh 06-Sep-03): addr = p->nOp;
361
1.1 (drh 06-Sep-03): if( nOp>0 ){
362
1.1 (drh 06-Sep-03): int i;
363
1.16 (drh 21-Feb-04): VdbeOpList const *pIn = aOp;
364
1.16 (drh 21-Feb-04): for(i=0; i<nOp; i++, pIn++){
365
1.16 (drh 21-Feb-04): int p2 = pIn->p2;
366
1.16 (drh 21-Feb-04): VdbeOp *pOut = &p->aOp[i+addr];
367
1.16 (drh 21-Feb-04): pOut->opcode = pIn->opcode;
368
1.16 (drh 21-Feb-04): pOut->p1 = pIn->p1;
369
1.16 (drh 21-Feb-04): pOut->p2 = p2<0 ? addr + ADDR(p2) : p2;
370
1.16 (drh 21-Feb-04): pOut->p3 = pIn->p3;
371
1.16 (drh 21-Feb-04): pOut->p3type = pIn->p3 ? P3_STATIC : P3_NOTUSED;
372
1.156 (danielk1 12-Jan-05): #ifdef SQLITE_DEBUG
373
1.24 (danielk1 10-May-04): if( sqlite3_vdbe_addop_trace ){
374
1.19 (danielk1 08-May-04): sqlite3VdbePrintOp(0, i+addr, &p->aOp[i+addr]);
375
1.1 (drh 06-Sep-03): }
376
1.1 (drh 06-Sep-03): #endif
377
1.1 (drh 06-Sep-03): }
378
1.1 (drh 06-Sep-03): p->nOp += nOp;
379
1.1 (drh 06-Sep-03): }
380
1.1 (drh 06-Sep-03): return addr;
381
1.1 (drh 06-Sep-03): }
383
1.1 (drh 06-Sep-03): /*
384
1.1 (drh 06-Sep-03): ** Change the value of the P1 operand for a specific instruction.
385
1.1 (drh 06-Sep-03): ** This routine is useful when a large program is loaded from a
386
1.19 (danielk1 08-May-04): ** static array using sqlite3VdbeAddOpList but we want to make a
387
1.1 (drh 06-Sep-03): ** few minor changes to the program.
388
1.1 (drh 06-Sep-03): */
389
1.19 (danielk1 08-May-04): void sqlite3VdbeChangeP1(Vdbe *p, int addr, int val){
390
1.240 (drh 13-Mar-06): assert( p==0 || p->magic==VDBE_MAGIC_INIT );
391
1.1 (drh 06-Sep-03): if( p && addr>=0 && p->nOp>addr && p->aOp ){
392
1.1 (drh 06-Sep-03): p->aOp[addr].p1 = val;
393
1.1 (drh 06-Sep-03): }
394
1.1 (drh 06-Sep-03): }
396
1.1 (drh 06-Sep-03): /*
397
1.1 (drh 06-Sep-03): ** Change the value of the P2 operand for a specific instruction.
398
1.1 (drh 06-Sep-03): ** This routine is useful for setting a jump destination.
399
1.1 (drh 06-Sep-03): */
400
1.19 (danielk1 08-May-04): void sqlite3VdbeChangeP2(Vdbe *p, int addr, int val){
401
1.1 (drh 06-Sep-03): assert( val>=0 );
402
1.240 (drh 13-Mar-06): assert( p==0 || p->magic==VDBE_MAGIC_INIT );
403
1.1 (drh 06-Sep-03): if( p && addr>=0 && p->nOp>addr && p->aOp ){
404
1.1 (drh 06-Sep-03): p->aOp[addr].p2 = val;
405
1.1 (drh 06-Sep-03): }
406
1.1 (drh 06-Sep-03): }
408
1.202 (drh 20-Sep-05): /*
409
1.242 (drh 17-Mar-06): ** Change the P2 operand of instruction addr so that it points to
410
1.202 (drh 20-Sep-05): ** the address of the next instruction to be coded.
411
1.202 (drh 20-Sep-05): */
412
1.202 (drh 20-Sep-05): void sqlite3VdbeJumpHere(Vdbe *p, int addr){
413
1.202 (drh 20-Sep-05): sqlite3VdbeChangeP2(p, addr, p->nOp);
414
1.202 (drh 20-Sep-05): }
415
1.198 (drh 16-Sep-05):
416
1.257 (drh 08-Jul-06):
417
1.257 (drh 08-Jul-06): /*
418
1.257 (drh 08-Jul-06): ** If the input FuncDef structure is ephemeral, then free it. If
419
1.257 (drh 08-Jul-06): ** the FuncDef is not ephermal, then do nothing.
420
1.257 (drh 08-Jul-06): */
421
1.257 (drh 08-Jul-06): static void freeEphemeralFunction(FuncDef *pDef){
422
1.257 (drh 08-Jul-06): if( pDef && (pDef->flags & SQLITE_FUNC_EPHEM)!=0 ){
423
1.257 (drh 08-Jul-06): sqliteFree(pDef);
424
1.257 (drh 08-Jul-06): }
425
1.257 (drh 08-Jul-06): }
426
1.257 (drh 08-Jul-06):
427
1.198 (drh 16-Sep-05): /*
428
1.198 (drh 16-Sep-05): ** Delete a P3 value if necessary.
429
1.198 (drh 16-Sep-05): */
430
1.198 (drh 16-Sep-05): static void freeP3(int p3type, void *p3){
431
1.198 (drh 16-Sep-05): if( p3 ){
432
1.201 (drh 17-Sep-05): switch( p3type ){
433
1.201 (drh 17-Sep-05): case P3_DYNAMIC:
434
1.201 (drh 17-Sep-05): case P3_KEYINFO:
435
1.201 (drh 17-Sep-05): case P3_KEYINFO_HANDOFF: {
436
1.201 (drh 17-Sep-05): sqliteFree(p3);
437
1.201 (drh 17-Sep-05): break;
438
1.201 (drh 17-Sep-05): }
439
1.246 (drh 13-Jun-06): case P3_MPRINTF: {
440
1.246 (drh 13-Jun-06): sqlite3_free(p3);
441
1.246 (drh 13-Jun-06): break;
442
1.246 (drh 13-Jun-06): }
443
1.201 (drh 17-Sep-05): case P3_VDBEFUNC: {
444
1.201 (drh 17-Sep-05): VdbeFunc *pVdbeFunc = (VdbeFunc *)p3;
445
1.257 (drh 08-Jul-06): freeEphemeralFunction(pVdbeFunc->pFunc);
446
1.201 (drh 17-Sep-05): sqlite3VdbeDeleteAuxData(pVdbeFunc, 0);
447
1.201 (drh 17-Sep-05): sqliteFree(pVdbeFunc);
448
1.201 (drh 17-Sep-05): break;
449
1.201 (drh 17-Sep-05): }
450
1.257 (drh 08-Jul-06): case P3_FUNCDEF: {
451
1.257 (drh 08-Jul-06): freeEphemeralFunction((FuncDef*)p3);
452
1.257 (drh 08-Jul-06): break;
453
1.257 (drh 08-Jul-06): }
454
1.201 (drh 17-Sep-05): case P3_MEM: {
455
1.201 (drh 17-Sep-05): sqlite3ValueFree((sqlite3_value*)p3);
456
1.201 (drh 17-Sep-05): break;
457
1.201 (drh 17-Sep-05): }
458
1.198 (drh 16-Sep-05): }
459
1.198 (drh 16-Sep-05): }
460
1.198 (drh 16-Sep-05): }
461
1.198 (drh 16-Sep-05):
462
1.198 (drh 16-Sep-05):
463
1.1 (drh 06-Sep-03): /*
464
1.242 (drh 17-Mar-06): ** Change N opcodes starting at addr to No-ops.
465
1.242 (drh 17-Mar-06): */
466
1.242 (drh 17-Mar-06): void sqlite3VdbeChangeToNoop(Vdbe *p, int addr, int N){
467
1.242 (drh 17-Mar-06): VdbeOp *pOp = &p->aOp[addr];
468
1.242 (drh 17-Mar-06): while( N-- ){
469
1.242 (drh 17-Mar-06): freeP3(pOp->p3type, pOp->p3);
470
1.242 (drh 17-Mar-06): memset(pOp, 0, sizeof(pOp[0]));
471
1.242 (drh 17-Mar-06): pOp->opcode = OP_Noop;
472
1.242 (drh 17-Mar-06): pOp++;
473
1.242 (drh 17-Mar-06): }
474
1.242 (drh 17-Mar-06): }
475
1.242 (drh 17-Mar-06):
476
1.242 (drh 17-Mar-06): /*
477
1.1 (drh 06-Sep-03): ** Change the value of the P3 operand for a specific instruction.
478
1.1 (drh 06-Sep-03): ** This routine is useful when a large program is loaded from a
479
1.19 (danielk1 08-May-04): ** static array using sqlite3VdbeAddOpList but we want to make a
480
1.1 (drh 06-Sep-03): ** few minor changes to the program.
481
1.1 (drh 06-Sep-03): **
482
1.1 (drh 06-Sep-03): ** If n>=0 then the P3 operand is dynamic, meaning that a copy of
483
1.1 (drh 06-Sep-03): ** the string is made into memory obtained from sqliteMalloc().
484
1.1 (drh 06-Sep-03): ** A value of n==0 means copy bytes of zP3 up to and including the
485
1.1 (drh 06-Sep-03): ** first null byte. If n>0 then copy n+1 bytes of zP3.
486
1.1 (drh 06-Sep-03): **
487
1.176 (danielk1 19-May-05): ** If n==P3_KEYINFO it means that zP3 is a pointer to a KeyInfo structure.
488
1.176 (danielk1 19-May-05): ** A copy is made of the KeyInfo structure into memory obtained from
489
1.176 (danielk1 19-May-05): ** sqliteMalloc, to be freed when the Vdbe is finalized.
490
1.176 (danielk1 19-May-05): ** n==P3_KEYINFO_HANDOFF indicates that zP3 points to a KeyInfo structure
491
1.176 (danielk1 19-May-05): ** stored in memory that the caller has obtained from sqliteMalloc. The
492
1.176 (danielk1 19-May-05): ** caller should not free the allocation, it will be freed when the Vdbe is
493
1.176 (danielk1 19-May-05): ** finalized.
494
1.176 (danielk1 19-May-05): **
495
1.176 (danielk1 19-May-05): ** Other values of n (P3_STATIC, P3_COLLSEQ etc.) indicate that zP3 points
496
1.176 (danielk1 19-May-05): ** to a string or structure that is guaranteed to exist for the lifetime of
497
1.176 (danielk1 19-May-05): ** the Vdbe. In these cases we can just copy the pointer.
498
1.1 (drh 06-Sep-03): **
499
1.1 (drh 06-Sep-03): ** If addr<0 then change P3 on the most recently inserted instruction.
500
1.1 (drh 06-Sep-03): */
501
1.19 (danielk1 08-May-04): void sqlite3VdbeChangeP3(Vdbe *p, int addr, const char *zP3, int n){
502
1.1 (drh 06-Sep-03): Op *pOp;
503
1.240 (drh 13-Mar-06): assert( p==0 || p->magic==VDBE_MAGIC_INIT );
504
1.227 (danielk1 18-Jan-06): if( p==0 || p->aOp==0 || sqlite3MallocFailed() ){
505
1.208 (danielk1 06-Dec-05): if (n != P3_KEYINFO) {
506
1.208 (danielk1 06-Dec-05): freeP3(n, (void*)*(char**)&zP3);
507
1.208 (danielk1 06-Dec-05): }
508
1.168 (danielk1 16-Mar-05): return;
509
1.168 (danielk1 16-Mar-05): }
510
1.1 (drh 06-Sep-03): if( addr<0 || addr>=p->nOp ){
511
1.1 (drh 06-Sep-03): addr = p->nOp - 1;
512
1.1 (drh 06-Sep-03): if( addr<0 ) return;
513
1.1 (drh 06-Sep-03): }
514
1.1 (drh 06-Sep-03): pOp = &p->aOp[addr];
515
1.198 (drh 16-Sep-05): freeP3(pOp->p3type, pOp->p3);
516
1.198 (drh 16-Sep-05): pOp->p3 = 0;
517
1.1 (drh 06-Sep-03): if( zP3==0 ){
518
1.1 (drh 06-Sep-03): pOp->p3 = 0;
519
1.1 (drh 06-Sep-03): pOp->p3type = P3_NOTUSED;
520
1.50 (drh 20-May-04): }else if( n==P3_KEYINFO ){
521
1.50 (drh 20-May-04): KeyInfo *pKeyInfo;
522
1.50 (drh 20-May-04): int nField, nByte;
523
1.192 (drh 01-Sep-05):
524
1.50 (drh 20-May-04): nField = ((KeyInfo*)zP3)->nField;
525
1.211 (drh 16-Dec-05): nByte = sizeof(*pKeyInfo) + (nField-1)*sizeof(pKeyInfo->aColl[0]) + nField;
526
1.108 (drh 13-Jun-04): pKeyInfo = sqliteMallocRaw( nByte );
527
1.50 (drh 20-May-04): pOp->p3 = (char*)pKeyInfo;
528
1.50 (drh 20-May-04): if( pKeyInfo ){
529
1.226 (danielk1 16-Jan-06): unsigned char *aSortOrder;
530
1.50 (drh 20-May-04): memcpy(pKeyInfo, zP3, nByte);
531
1.211 (drh 16-Dec-05): aSortOrder = pKeyInfo->aSortOrder;
532
1.211 (drh 16-Dec-05): if( aSortOrder ){
533
1.226 (danielk1 16-Jan-06): pKeyInfo->aSortOrder = (unsigned char*)&pKeyInfo->aColl[nField];
534
1.211 (drh 16-Dec-05): memcpy(pKeyInfo->aSortOrder, aSortOrder, nField);
535
1.211 (drh 16-Dec-05): }
536
1.50 (drh 20-May-04): pOp->p3type = P3_KEYINFO;
537
1.50 (drh 20-May-04): }else{
538
1.50 (drh 20-May-04): pOp->p3type = P3_NOTUSED;
539
1.50 (drh 20-May-04): }
540
1.51 (drh 21-May-04): }else if( n==P3_KEYINFO_HANDOFF ){
541
1.51 (drh 21-May-04): pOp->p3 = (char*)zP3;
542
1.51 (drh 21-May-04): pOp->p3type = P3_KEYINFO;
543
1.1 (drh 06-Sep-03): }else if( n<0 ){
544
1.1 (drh 06-Sep-03): pOp->p3 = (char*)zP3;
545
1.1 (drh 06-Sep-03): pOp->p3type = n;
546
1.1 (drh 06-Sep-03): }else{
547
1.147 (drh 25-Sep-04): if( n==0 ) n = strlen(zP3);
548
1.147 (drh 25-Sep-04): pOp->p3 = sqliteStrNDup(zP3, n);
549
1.1 (drh 06-Sep-03): pOp->p3type = P3_DYNAMIC;
550
1.1 (drh 06-Sep-03): }
551
1.1 (drh 06-Sep-03): }
553
1.144 (drh 19-Sep-04): #ifndef NDEBUG
554
1.144 (drh 19-Sep-04): /*
555
1.144 (drh 19-Sep-04): ** Replace the P3 field of the most recently coded instruction with
556
1.144 (drh 19-Sep-04): ** comment text.
557
1.144 (drh 19-Sep-04): */
558
1.144 (drh 19-Sep-04): void sqlite3VdbeComment(Vdbe *p, const char *zFormat, ...){
559
1.144 (drh 19-Sep-04): va_list ap;
560
1.144 (drh 19-Sep-04): assert( p->nOp>0 );
561
1.221 (drh 11-Jan-06): assert( p->aOp==0 || p->aOp[p->nOp-1].p3==0
562
1.227 (danielk1 18-Jan-06): || sqlite3MallocFailed() );
563
1.144 (drh 19-Sep-04): va_start(ap, zFormat);
564
1.144 (drh 19-Sep-04): sqlite3VdbeChangeP3(p, -1, sqlite3VMPrintf(zFormat, ap), P3_DYNAMIC);
565
1.144 (drh 19-Sep-04): va_end(ap);
566
1.144 (drh 19-Sep-04): }
567
1.144 (drh 19-Sep-04): #endif
568
1.144 (drh 19-Sep-04):
569
1.1 (drh 06-Sep-03): /*
570
1.1 (drh 06-Sep-03): ** Return the opcode for a given address.
571
1.1 (drh 06-Sep-03): */
572
1.19 (danielk1 08-May-04): VdbeOp *sqlite3VdbeGetOp(Vdbe *p, int addr){
573
1.1 (drh 06-Sep-03): assert( p->magic==VDBE_MAGIC_INIT );
574
1.1 (drh 06-Sep-03): assert( addr>=0 && addr<p->nOp );
575
1.1 (drh 06-Sep-03): return &p->aOp[addr];
576
1.1 (drh 06-Sep-03): }
578
1.151 (drh 31-Oct-04): #if !defined(SQLITE_OMIT_EXPLAIN) || !defined(NDEBUG) \
579
1.151 (drh 31-Oct-04): || defined(VDBE_PROFILE) || defined(SQLITE_DEBUG)
580
1.1 (drh 06-Sep-03): /*
581
1.50 (drh 20-May-04): ** Compute a string that describes the P3 parameter for an opcode.
582
1.50 (drh 20-May-04): ** Use zTemp for any required temporary buffer space.
583
1.50 (drh 20-May-04): */
584
1.50 (drh 20-May-04): static char *displayP3(Op *pOp, char *zTemp, int nTemp){
585
1.50 (drh 20-May-04): char *zP3;
586
1.50 (drh 20-May-04): assert( nTemp>=20 );
587
1.50 (drh 20-May-04): switch( pOp->p3type ){
588
1.50 (drh 20-May-04): case P3_KEYINFO: {
589
1.50 (drh 20-May-04): int i, j;
590
1.50 (drh 20-May-04): KeyInfo *pKeyInfo = (KeyInfo*)pOp->p3;
591
1.50 (drh 20-May-04): sprintf(zTemp, "keyinfo(%d", pKeyInfo->nField);
592
1.50 (drh 20-May-04): i = strlen(zTemp);
593
1.50 (drh 20-May-04): for(j=0; j<pKeyInfo->nField; j++){
594
1.50 (drh 20-May-04): CollSeq *pColl = pKeyInfo->aColl[j];
595
1.50 (drh 20-May-04): if( pColl ){
596
1.50 (drh 20-May-04): int n = strlen(pColl->zName);
597
1.50 (drh 20-May-04): if( i+n>nTemp-6 ){
598
1.50 (drh 20-May-04): strcpy(&zTemp[i],",...");
599
1.50 (drh 20-May-04): break;
600
1.50 (drh 20-May-04): }
601
1.50 (drh 20-May-04): zTemp[i++] = ',';
602
1.51 (drh 21-May-04): if( pKeyInfo->aSortOrder && pKeyInfo->aSortOrder[j] ){
603
1.50 (drh 20-May-04): zTemp[i++] = '-';
604
1.50 (drh 20-May-04): }
605
1.50 (drh 20-May-04): strcpy(&zTemp[i], pColl->zName);
606
1.50 (drh 20-May-04): i += n;
607
1.50 (drh 20-May-04): }else if( i+4<nTemp-6 ){
608
1.50 (drh 20-May-04): strcpy(&zTemp[i],",nil");
609
1.50 (drh 20-May-04): i += 4;
610
1.50 (drh 20-May-04): }
611
1.50 (drh 20-May-04): }
612
1.50 (drh 20-May-04): zTemp[i++] = ')';
613
1.50 (drh 20-May-04): zTemp[i] = 0;
614
1.50 (drh 20-May-04): assert( i<nTemp );
615
1.50 (drh 20-May-04): zP3 = zTemp;
616
1.50 (drh 20-May-04): break;
617
1.50 (drh 20-May-04): }
618
1.50 (drh 20-May-04): case P3_COLLSEQ: {
619
1.50 (drh 20-May-04): CollSeq *pColl = (CollSeq*)pOp->p3;
620
1.51 (drh 21-May-04): sprintf(zTemp, "collseq(%.20s)", pColl->zName);
621
1.50 (drh 20-May-04): zP3 = zTemp;
622
1.50 (drh 20-May-04): break;
623
1.50 (drh 20-May-04): }
624
1.67 (drh 26-May-04): case P3_FUNCDEF: {
625
1.67 (drh 26-May-04): FuncDef *pDef = (FuncDef*)pOp->p3;
626
1.244 (drh 13-Jun-06): sqlite3_snprintf(nTemp, zTemp, "%s(%d)", pDef->zName, pDef->nArg);
627
1.244 (drh 13-Jun-06): zP3 = zTemp;
628
1.244 (drh 13-Jun-06): break;
629
1.244 (drh 13-Jun-06): }
630
1.244 (drh 13-Jun-06): #ifndef SQLITE_OMIT_VIRTUALTABLE
631
1.244 (drh 13-Jun-06): case P3_VTAB: {
632
1.244 (drh 13-Jun-06): sqlite3_vtab *pVtab = (sqlite3_vtab*)pOp->p3;
633
1.256 (drh 26-Jun-06): sqlite3_snprintf(nTemp, zTemp, "vtab:%p:%p", pVtab, pVtab->pModule);
634
1.67 (drh 26-May-04): zP3 = zTemp;
635
1.67 (drh 26-May-04): break;
636
1.67 (drh 26-May-04): }
637
1.244 (drh 13-Jun-06): #endif
638
1.50 (drh 20-May-04): default: {
639
1.50 (drh 20-May-04): zP3 = pOp->p3;
640
1.135 (drh 24-Jul-04): if( zP3==0 || pOp->opcode==OP_Noop ){
641
1.50 (drh 20-May-04): zP3 = "";
642
1.50 (drh 20-May-04): }
643
1.50 (drh 20-May-04): }
644
1.50 (drh 20-May-04): }
645
1.249 (drh 15-Jun-06): assert( zP3!=0 );
646
1.50 (drh 20-May-04): return zP3;
647
1.50 (drh 20-May-04): }
648
1.151 (drh 31-Oct-04): #endif
649
1.50 (drh 20-May-04):
650
1.50 (drh 20-May-04):
651
1.156 (danielk1 12-Jan-05): #if defined(VDBE_PROFILE) || defined(SQLITE_DEBUG)
652
1.1 (drh 06-Sep-03): /*
653
1.1 (drh 06-Sep-03): ** Print a single opcode. This routine is used for debugging only.
654
1.1 (drh 06-Sep-03): */
655
1.19 (danielk1 08-May-04): void sqlite3VdbePrintOp(FILE *pOut, int pc, Op *pOp){
656
1.1 (drh 06-Sep-03): char *zP3;
657
1.50 (drh 20-May-04): char zPtr[50];
658
1.50 (drh 20-May-04): static const char *zFormat1 = "%4d %-13s %4d %4d %s\n";
659
1.1 (drh 06-Sep-03): if( pOut==0 ) pOut = stdout;
660
1.50 (drh 20-May-04): zP3 = displayP3(pOp, zPtr, sizeof(zPtr));
661
1.50 (drh 20-May-04): fprintf(pOut, zFormat1,
662
1.50 (drh 20-May-04): pc, sqlite3OpcodeNames[pOp->opcode], pOp->p1, pOp->p2, zP3);
663
1.1 (drh 06-Sep-03): fflush(pOut);
664
1.1 (drh 06-Sep-03): }
665
1.1 (drh 06-Sep-03): #endif
667
1.1 (drh 06-Sep-03): /*
668
1.146 (drh 24-Sep-04): ** Release an array of N Mem elements
669
1.146 (drh 24-Sep-04): */
670
1.146 (drh 24-Sep-04): static void releaseMemArray(Mem *p, int N){
671
1.146 (drh 24-Sep-04): if( p ){
672
1.146 (drh 24-Sep-04): while( N-->0 ){
673
1.146 (drh 24-Sep-04): sqlite3VdbeMemRelease(p++);
674
1.146 (drh 24-Sep-04): }
675
1.146 (drh 24-Sep-04): }
676
1.146 (drh 24-Sep-04): }
677
1.146 (drh 24-Sep-04):
678
1.151 (drh 31-Oct-04): #ifndef SQLITE_OMIT_EXPLAIN
679
1.146 (drh 24-Sep-04): /*
680
1.1 (drh 06-Sep-03): ** Give a listing of the program in the virtual machine.
681
1.1 (drh 06-Sep-03): **
682
1.19 (danielk1 08-May-04): ** The interface is the same as sqlite3VdbeExec(). But instead of
683
1.1 (drh 06-Sep-03): ** running the code, it invokes the callback once for each instruction.
684
1.1 (drh 06-Sep-03): ** This feature is used to implement "EXPLAIN".
685
1.1 (drh 06-Sep-03): */
686
1.19 (danielk1 08-May-04): int sqlite3VdbeList(
687
1.1 (drh 06-Sep-03): Vdbe *p /* The VDBE */
688
1.1 (drh 06-Sep-03): ){
689
1.140 (drh 06-Sep-04): sqlite3 *db = p->db;
690
1.1 (drh 06-Sep-03): int i;
691
1.14 (drh 14-Feb-04): int rc = SQLITE_OK;
693
1.1 (drh 06-Sep-03): assert( p->explain );
694
1.155 (drh 11-Jan-05): if( p->magic!=VDBE_MAGIC_RUN ) return SQLITE_MISUSE;
695
1.155 (drh 11-Jan-05): assert( db->magic==SQLITE_MAGIC_BUSY );
696
1.155 (drh 11-Jan-05): assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY );
697
1.56 (danielk1 22-May-04):
698
1.56 (danielk1 22-May-04): /* Even though this opcode does not put dynamic strings onto the
699
1.56 (danielk1 22-May-04): ** the stack, they may become dynamic if the user calls
700
1.68 (drh 26-May-04): ** sqlite3_column_text16(), causing a translation to UTF-16 encoding.
701
1.56 (danielk1 22-May-04): */
702
1.56 (danielk1 22-May-04): if( p->pTos==&p->aStack[4] ){
703
1.146 (drh 24-Sep-04): releaseMemArray(p->aStack, 5);
704
1.56 (danielk1 22-May-04): }
705
1.56 (danielk1 22-May-04): p->resOnStack = 0;
706
1.56 (danielk1 22-May-04):
707
1.197 (drh 10-Sep-05): do{
708
1.197 (drh 10-Sep-05): i = p->pc++;
709
1.197 (drh 10-Sep-05): }while( i<p->nOp && p->explain==2 && p->aOp[i].opcode!=OP_Explain );
710
1.14 (drh 14-Feb-04): if( i>=p->nOp ){
711
1.14 (drh 14-Feb-04): p->rc = SQLITE_OK;
712
1.14 (drh 14-Feb-04): rc = SQLITE_DONE;
713
1.259 (drh 26-Jul-06): }else if( db->u1.isInterrupted ){
714
1.155 (drh 11-Jan-05): p->rc = SQLITE_INTERRUPT;
715
1.14 (drh 14-Feb-04): rc = SQLITE_ERROR;
716
1.89 (danielk1 31-May-04): sqlite3SetString(&p->zErrMsg, sqlite3ErrStr(p->rc), (char*)0);
717
1.14 (drh 14-Feb-04): }else{
718
1.50 (drh 20-May-04): Op *pOp = &p->aOp[i];
719
1.69 (drh 27-May-04): Mem *pMem = p->aStack;
720
1.69 (drh 27-May-04): pMem->flags = MEM_Int;
721
1.88 (drh 31-May-04): pMem->type = SQLITE_INTEGER;
722
1.69 (drh 27-May-04): pMem->i = i; /* Program counter */
723
1.69 (drh 27-May-04): pMem++;
724
1.69 (drh 27-May-04):
725
1.69 (drh 27-May-04): pMem->flags = MEM_Static|MEM_Str|MEM_Term;
726
1.69 (drh 27-May-04): pMem->z = sqlite3OpcodeNames[pOp->opcode]; /* Opcode */
727
1.249 (drh 15-Jun-06): assert( pMem->z!=0 );
728
1.69 (drh 27-May-04): pMem->n = strlen(pMem->z);
729
1.88 (drh 31-May-04): pMem->type = SQLITE_TEXT;
730
1.104 (danielk1 12-Jun-04): pMem->enc = SQLITE_UTF8;
731
1.69 (drh 27-May-04): pMem++;
732
1.69 (drh 27-May-04):
733
1.69 (drh 27-May-04): pMem->flags = MEM_Int;
734
1.69 (drh 27-May-04): pMem->i = pOp->p1; /* P1 */
735
1.88 (drh 31-May-04): pMem->type = SQLITE_INTEGER;
736
1.69 (drh 27-May-04): pMem++;
737
1.69 (drh 27-May-04):
738
1.69 (drh 27-May-04): pMem->flags = MEM_Int;
739
1.69 (drh 27-May-04): pMem->i = pOp->p2; /* P2 */
740
1.88 (drh 31-May-04): pMem->type = SQLITE_INTEGER;
741
1.69 (drh 27-May-04): pMem++;
742
1.69 (drh 27-May-04):
743
1.239 (drh 03-Mar-06): pMem->flags = MEM_Ephem|MEM_Str|MEM_Term; /* P3 */
744
1.69 (drh 27-May-04): pMem->z = displayP3(pOp, pMem->zShort, sizeof(pMem->zShort));
745
1.249 (drh 15-Jun-06): assert( pMem->z!=0 );
746
1.239 (drh 03-Mar-06): pMem->n = strlen(pMem->z);
747
1.88 (drh 31-May-04): pMem->type = SQLITE_TEXT;
748
1.104 (danielk1 12-Jun-04): pMem->enc = SQLITE_UTF8;
749
1.69 (drh 27-May-04):
750
1.197 (drh 10-Sep-05): p->nResColumn = 5 - 2*(p->explain-1);
751
1.69 (drh 27-May-04): p->pTos = pMem;
752
1.14 (drh 14-Feb-04): p->rc = SQLITE_OK;
753
1.56 (danielk1 22-May-04): p->resOnStack = 1;
754
1.14 (drh 14-Feb-04): rc = SQLITE_ROW;
755
1.1 (drh 06-Sep-03): }
756
1.14 (drh 14-Feb-04): return rc;
757
1.1 (drh 06-Sep-03): }
758
1.151 (drh 31-Oct-04): #endif /* SQLITE_OMIT_EXPLAIN */
760
1.1 (drh 06-Sep-03): /*
761
1.135 (drh 24-Jul-04): ** Print the SQL that was used to generate a VDBE program.
762
1.135 (drh 24-Jul-04): */
763
1.135 (drh 24-Jul-04): void sqlite3VdbePrintSql(Vdbe *p){
764
1.135 (drh 24-Jul-04): #ifdef SQLITE_DEBUG
765
1.135 (drh 24-Jul-04): int nOp = p->nOp;
766
1.135 (drh 24-Jul-04): VdbeOp *pOp;
767
1.142 (drh 15-Sep-04): if( nOp<1 ) return;
768
1.142 (drh 15-Sep-04): pOp = &p->aOp[nOp-1];
769
1.135 (drh 24-Jul-04): if( pOp->opcode==OP_Noop && pOp->p3!=0 ){
770
1.135 (drh 24-Jul-04): const char *z = pOp->p3;
771
1.136 (drh 08-Aug-04): while( isspace(*(u8*)z) ) z++;
772
1.135 (drh 24-Jul-04): printf("SQL: [%s]\n", z);
773
1.135 (drh 24-Jul-04): }
774
1.135 (drh 24-Jul-04): #endif
775
1.135 (drh 24-Jul-04): }
776
1.135 (drh 24-Jul-04):
777
1.135 (drh 24-Jul-04): /*
778
1.1 (drh 06-Sep-03): ** Prepare a virtual machine for execution. This involves things such
779
1.1 (drh 06-Sep-03): ** as allocating stack space and initializing the program counter.
780
1.1 (drh 06-Sep-03): ** After the VDBE has be prepped, it can be executed by one or more
781
1.19 (danielk1 08-May-04): ** calls to sqlite3VdbeExec().
782
1.139 (drh 02-Sep-04): **
783
1.139 (drh 02-Sep-04): ** This is the only way to move a VDBE from VDBE_MAGIC_INIT to
784
1.139 (drh 02-Sep-04): ** VDBE_MAGIC_RUN.
785
1.1 (drh 06-Sep-03): */
786
1.19 (danielk1 08-May-04): void sqlite3VdbeMakeReady(
787
1.1 (drh 06-Sep-03): Vdbe *p, /* The VDBE */
788
1.2 (drh 06-Sep-03): int nVar, /* Number of '?' see in the SQL statement */
789
1.138 (drh 21-Aug-04): int nMem, /* Number of memory cells to allocate */
790
1.138 (drh 21-Aug-04): int nCursor, /* Number of cursors to allocate */
791
1.1 (drh 06-Sep-03): int isExplain /* True if the EXPLAIN keywords is present */
792
1.1 (drh 06-Sep-03): ){
793
1.1 (drh 06-Sep-03): int n;
795
1.1 (drh 06-Sep-03): assert( p!=0 );
796
1.1 (drh 06-Sep-03): assert( p->magic==VDBE_MAGIC_INIT );
798
1.142 (drh 15-Sep-04): /* There should be at least one opcode.
799
1.1 (drh 06-Sep-03): */
800
1.142 (drh 15-Sep-04): assert( p->nOp>0 );
802
1.170 (danielk1 28-Mar-05): /* Set the magic to VDBE_MAGIC_RUN sooner rather than later. This
803
1.170 (danielk1 28-Mar-05): * is because the call to resizeOpArray() below may shrink the
804
1.170 (danielk1 28-Mar-05): * p->aOp[] array to save memory if called when in VDBE_MAGIC_RUN
805
1.170 (danielk1 28-Mar-05): * state.
806
1.170 (danielk1 28-Mar-05): */
807
1.170 (danielk1 28-Mar-05): p->magic = VDBE_MAGIC_RUN;
808
1.170 (danielk1 28-Mar-05):
809
1.1 (drh 06-Sep-03): /* No instruction ever pushes more than a single element onto the
810
1.1 (drh 06-Sep-03): ** stack. And the stack never grows on successive executions of the
811
1.1 (drh 06-Sep-03): ** same loop. So the total number of instructions is an upper bound
812
1.180 (drh 07-Jun-05): ** on the maximum stack depth required. (Added later:) The
813
1.180 (drh 07-Jun-05): ** resolveP2Values() call computes a tighter upper bound on the
814
1.180 (drh 07-Jun-05): ** stack size.
815
1.1 (drh 06-Sep-03): **
816
1.1 (drh 06-Sep-03): ** Allocation all the stack space we will ever need.
817
1.1 (drh 06-Sep-03): */
818
1.3 (drh 06-Sep-03): if( p->aStack==0 ){
819
1.170 (danielk1 28-Mar-05): int nArg; /* Maximum number of args passed to a user function. */
820
1.171 (danielk1 29-Mar-05): int nStack; /* Maximum number of stack entries required */
821
1.171 (danielk1 29-Mar-05): resolveP2Values(p, &nArg, &nStack);
822
1.170 (danielk1 28-Mar-05): resizeOpArray(p, p->nOp);
823
1.3 (drh 06-Sep-03): assert( nVar>=0 );
824
1.171 (danielk1 29-Mar-05): assert( nStack<p->nOp );
825
1.261 (drh 08-Aug-06): if( isExplain ){
826
1.261 (drh 08-Aug-06): nStack = 10;
827
1.261 (drh 08-Aug-06): }
828
1.3 (drh 06-Sep-03): p->aStack = sqliteMalloc(
829
1.171 (danielk1 29-Mar-05): nStack*sizeof(p->aStack[0]) /* aStack */
830
1.170 (danielk1 28-Mar-05): + nArg*sizeof(Mem*) /* apArg */
831
1.149 (drh 05-Oct-04): + nVar*sizeof(Mem) /* aVar */
832
1.149 (drh 05-Oct-04): + nVar*sizeof(char*) /* azVar */
833
1.149 (drh 05-Oct-04): + nMem*sizeof(Mem) /* aMem */
834
1.149 (drh 05-Oct-04): + nCursor*sizeof(Cursor*) /* apCsr */
835
1.3 (drh 06-Sep-03): );
836
1.227 (danielk1 18-Jan-06): if( !sqlite3MallocFailed() ){
837
1.171 (danielk1 29-Mar-05): p->aMem = &p->aStack[nStack];
838
1.149 (drh 05-Oct-04): p->nMem = nMem;
839
1.149 (drh 05-Oct-04): p->aVar = &p->aMem[nMem];
840
1.149 (drh 05-Oct-04): p->nVar = nVar;
841
1.138 (drh 21-Aug-04): p->okVar = 0;
842
1.149 (drh 05-Oct-04): p->apArg = (Mem**)&p->aVar[nVar];
843
1.170 (danielk1 28-Mar-05): p->azVar = (char**)&p->apArg[nArg];
844
1.149 (drh 05-Oct-04): p->apCsr = (Cursor**)&p->azVar[nVar];
845
1.138 (drh 21-Aug-04): p->nCursor = nCursor;
846
1.138 (drh 21-Aug-04): for(n=0; n<nVar; n++){
847
1.138 (drh 21-Aug-04): p->aVar[n].flags = MEM_Null;
848
1.138 (drh 21-Aug-04): }
849
1.42 (danielk1 19-May-04): }
850
1.3 (drh 06-Sep-03): }
851
1.166 (danielk1 29-Jan-05): for(n=0; n<p->nMem; n++){
852
1.166 (danielk1 29-Jan-05): p->aMem[n].flags = MEM_Null;
853
1.166 (danielk1 29-Jan-05): }
855
1.10 (drh 31-Jan-04): p->pTos = &p->aStack[-1];
856
1.85 (danielk1 31-May-04): p->pc = -1;
857
1.1 (drh 06-Sep-03): p->rc = SQLITE_OK;
858
1.1 (drh 06-Sep-03): p->uniqueCnt = 0;
859
1.1 (drh 06-Sep-03): p->returnDepth = 0;
860
1.1 (drh 06-Sep-03): p->errorAction = OE_Abort;
861
1.1 (drh 06-Sep-03): p->popStack = 0;
862
1.1 (drh 06-Sep-03): p->explain |= isExplain;
863
1.1 (drh 06-Sep-03): p->magic = VDBE_MAGIC_RUN;
864
1.121 (danielk1 21-Jun-04): p->nChange = 0;
865
1.219 (drh 07-Jan-06): p->cacheCtr = 1;
866
1.215 (drh 29-Dec-05): p->minWriteFileFormat = 255;
867
1.1 (drh 06-Sep-03): #ifdef VDBE_PROFILE
868
1.6 (drh 31-Dec-03): {
869
1.6 (drh 31-Dec-03): int i;
870
1.6 (drh 31-Dec-03): for(i=0; i<p->nOp; i++){
871
1.6 (drh 31-Dec-03): p->aOp[i].cnt = 0;
872
1.6 (drh 31-Dec-03): p->aOp[i].cycles = 0;
873
1.6 (drh 31-Dec-03): }
874
1.1 (drh 06-Sep-03): }
875
1.1 (drh 06-Sep-03): #endif
876
1.1 (drh 06-Sep-03): }
878
1.1 (drh 06-Sep-03): /*
879
1.1 (drh 06-Sep-03): ** Close a cursor and release all the resources that cursor happens
880
1.1 (drh 06-Sep-03): ** to hold.
881
1.1 (drh 06-Sep-03): */
882
1.252 (danielk1 23-Jun-06): void sqlite3VdbeFreeCursor(Vdbe *p, Cursor *pCx){
883
1.106 (drh 12-Jun-04): if( pCx==0 ){
884
1.106 (drh 12-Jun-04): return;
885
1.106 (drh 12-Jun-04): }
886
1.1 (drh 06-Sep-03): if( pCx->pCursor ){
887
1.19 (danielk1 08-May-04): sqlite3BtreeCloseCursor(pCx->pCursor);
888
1.1 (drh 06-Sep-03): }
889
1.1 (drh 06-Sep-03): if( pCx->pBt ){
890
1.19 (danielk1 08-May-04): sqlite3BtreeClose(pCx->pBt);
891
1.1 (drh 06-Sep-03): }
892
1.243 (drh 12-Jun-06): #ifndef SQLITE_OMIT_VIRTUALTABLE
893
1.243 (drh 12-Jun-06): if( pCx->pVtabCursor ){
894
1.243 (drh 12-Jun-06): sqlite3_vtab_cursor *pVtabCursor = pCx->pVtabCursor;
895
1.252 (danielk1 23-Jun-06): const sqlite3_module *pModule = pCx->pModule;
896
1.252 (danielk1 23-Jun-06): p->inVtabMethod = 1;
897
1.258 (danielk1 25-Jul-06): sqlite3SafetyOff(p->db);
898
1.243 (drh 12-Jun-06): pModule->xClose(pVtabCursor);
899
1.258 (danielk1 25-Jul-06): sqlite3SafetyOn(p->db);
900
1.252 (danielk1 23-Jun-06): p->inVtabMethod = 0;
901
1.243 (drh 12-Jun-06): }
902
1.243 (drh 12-Jun-06): #endif
903
1.1 (drh 06-Sep-03): sqliteFree(pCx->pData);
904
1.34 (drh 14-May-04): sqliteFree(pCx->aType);
905
1.106 (drh 12-Jun-04): sqliteFree(pCx);
906
1.1 (drh 06-Sep-03): }
908
1.1 (drh 06-Sep-03): /*
909
1.1 (drh 06-Sep-03): ** Close all cursors
910
1.1 (drh 06-Sep-03): */
911
1.1 (drh 06-Sep-03): static void closeAllCursors(Vdbe *p){
912
1.1 (drh 06-Sep-03): int i;
913
1.138 (drh 21-Aug-04): if( p->apCsr==0 ) return;
914
1.1 (drh 06-Sep-03): for(i=0; i<p->nCursor; i++){
915
1.252 (danielk1 23-Jun-06): if( !p->inVtabMethod || (p->apCsr[i] && !p->apCsr[i]->pVtabCursor) ){
916
1.252 (danielk1 23-Jun-06): sqlite3VdbeFreeCursor(p, p->apCsr[i]);
917
1.253 (danielk1 23-Jun-06): p->apCsr[i] = 0;
918
1.252 (danielk1 23-Jun-06): }
919
1.1 (drh 06-Sep-03): }
920
1.1 (drh 06-Sep-03): }
922
1.1 (drh 06-Sep-03): /*
923
1.1 (drh 06-Sep-03): ** Clean up the VM after execution.
924
1.1 (drh 06-Sep-03): **
925
1.1 (drh 06-Sep-03): ** This routine will automatically close any cursors, lists, and/or
926
1.1 (drh 06-Sep-03): ** sorters that were left open. It also deletes the values of
927
1.43 (drh 19-May-04): ** variables in the aVar[] array.
928
1.1 (drh 06-Sep-03): */
929
1.1 (drh 06-Sep-03): static void Cleanup(Vdbe *p){
930
1.1 (drh 06-Sep-03): int i;
931
1.10 (drh 31-Jan-04): if( p->aStack ){
932
1.146 (drh 24-Sep-04): releaseMemArray(p->aStack, 1 + (p->pTos - p->aStack));
933
1.146 (drh 24-Sep-04): p->pTos = &p->aStack[-1];
934
1.10 (drh 31-Jan-04): }
935
1.1 (drh 06-Sep-03): closeAllCursors(p);
936
1.146 (drh 24-Sep-04): releaseMemArray(p->aMem, p->nMem);
937
1.183 (drh 08-Jul-05): sqlite3VdbeFifoClear(&p->sFifo);
938
1.146 (drh 24-Sep-04): if( p->contextStack ){
939
1.146 (drh 24-Sep-04): for(i=0; i<p->contextStackTop; i++){
940
1.183 (drh 08-Jul-05): sqlite3VdbeFifoClear(&p->contextStack[i].sFifo);
941
1.146 (drh 24-Sep-04): }
942
1.146 (drh 24-Sep-04): sqliteFree(p->contextStack);
943
1.143 (drh 19-Sep-04): }
944
1.17 (drh 21-Feb-04): p->contextStack = 0;
945
1.143 (drh 19-Sep-04): p->contextStackDepth = 0;
946
1.143 (drh 19-Sep-04): p->contextStackTop = 0;
947
1.1 (drh 06-Sep-03): sqliteFree(p->zErrMsg);
948
1.1 (drh 06-Sep-03): p->zErrMsg = 0;
949
1.1 (drh 06-Sep-03): }
951
1.1 (drh 06-Sep-03): /*
952
1.64 (danielk1 25-May-04): ** Set the number of result columns that will be returned by this SQL
953
1.64 (danielk1 25-May-04): ** statement. This is now set at compile time, rather than during
954
1.64 (danielk1 25-May-04): ** execution of the vdbe program so that sqlite3_column_count() can
955
1.64 (danielk1 25-May-04): ** be called on an SQL statement before sqlite3_step().
956
1.64 (danielk1 25-May-04): */
957
1.64 (danielk1 25-May-04): void sqlite3VdbeSetNumCols(Vdbe *p, int nResColumn){
958
1.146 (drh 24-Sep-04): Mem *pColName;
959
1.146 (drh 24-Sep-04): int n;
960
1.236 (danielk1 10-Feb-06): releaseMemArray(p->aColName, p->nResColumn*COLNAME_N);
961
1.203 (drh 05-Oct-05): sqliteFree(p->aColName);
962
1.236 (danielk1 10-Feb-06): n = nResColumn*COLNAME_N;
963
1.64 (danielk1 25-May-04): p->nResColumn = nResColumn;
964
1.146 (drh 24-Sep-04): p->aColName = pColName = (Mem*)sqliteMalloc( sizeof(Mem)*n );
965
1.146 (drh 24-Sep-04): if( p->aColName==0 ) return;
966
1.146 (drh 24-Sep-04): while( n-- > 0 ){
967
1.146 (drh 24-Sep-04): (pColName++)->flags = MEM_Null;
968
1.146 (drh 24-Sep-04): }
969
1.66 (danielk1 26-May-04): }
970
1.66 (danielk1 26-May-04):
971
1.66 (danielk1 26-May-04): /*
972
1.66 (danielk1 26-May-04): ** Set the name of the idx'th column to be returned by the SQL statement.
973
1.66 (danielk1 26-May-04): ** zName must be a pointer to a nul terminated string.
974
1.66 (danielk1 26-May-04): **
975
1.66 (danielk1 26-May-04): ** This call must be made after a call to sqlite3VdbeSetNumCols().
976
1.66 (danielk1 26-May-04): **
977
1.105 (danielk1 12-Jun-04): ** If N==P3_STATIC it means that zName is a pointer to a constant static
978
1.105 (danielk1 12-Jun-04): ** string and we can just copy the pointer. If it is P3_DYNAMIC, then
979
1.105 (danielk1 12-Jun-04): ** the string is freed using sqliteFree() when the vdbe is finished with
980
1.105 (danielk1 12-Jun-04): ** it. Otherwise, N bytes of zName are copied.
981
1.66 (danielk1 26-May-04): */
982
1.236 (danielk1 10-Feb-06): int sqlite3VdbeSetColName(Vdbe *p, int idx, int var, const char *zName, int N){
983
1.66 (danielk1 26-May-04): int rc;
984
1.66 (danielk1 26-May-04): Mem *pColName;
985
1.236 (danielk1 10-Feb-06): assert( idx<p->nResColumn );
986
1.236 (danielk1 10-Feb-06): assert( var<COLNAME_N );
987
1.227 (danielk1 18-Jan-06): if( sqlite3MallocFailed() ) return SQLITE_NOMEM;
988
1.146 (drh 24-Sep-04): assert( p->aColName!=0 );
989
1.236 (danielk1 10-Feb-06): pColName = &(p->aColName[idx+var*p->nResColumn]);
990
1.105 (danielk1 12-Jun-04): if( N==P3_DYNAMIC || N==P3_STATIC ){
991
1.105 (danielk1 12-Jun-04): rc = sqlite3VdbeMemSetStr(pColName, zName, -1, SQLITE_UTF8, SQLITE_STATIC);
992
1.66 (danielk1 26-May-04): }else{
993
1.105 (danielk1 12-Jun-04): rc = sqlite3VdbeMemSetStr(pColName, zName, N, SQLITE_UTF8,SQLITE_TRANSIENT);
994
1.66 (danielk1 26-May-04): }
995
1.66 (danielk1 26-May-04): if( rc==SQLITE_OK && N==P3_DYNAMIC ){
996
1.66 (danielk1 26-May-04): pColName->flags = (pColName->flags&(~MEM_Static))|MEM_Dyn;
997
1.105 (danielk1 12-Jun-04): pColName->xDel = 0;
998
1.66 (danielk1 26-May-04): }
999
1.66 (danielk1 26-May-04): return rc;
1000
1.64 (danielk1 25-May-04): }
1001
1.64 (danielk1 25-May-04):
1002
1.91 (danielk1 03-Jun-04): /*
1003
1.91 (danielk1 03-Jun-04): ** A read or write transaction may or may not be active on database handle
1004
1.91 (danielk1 03-Jun-04): ** db. If a transaction is active, commit it. If there is a
1005
1.91 (danielk1 03-Jun-04): ** write-transaction spanning more than one database file, this routine
1006
1.91 (danielk1 03-Jun-04): ** takes care of the master journal trickery.
1007
1.91 (danielk1 03-Jun-04): */
1008
1.140 (drh 06-Sep-04): static int vdbeCommit(sqlite3 *db){
1009
1.91 (danielk1 03-Jun-04): int i;
1010
1.91 (danielk1 03-Jun-04): int nTrans = 0; /* Number of databases with an active write-transaction */
1011
1.91 (danielk1 03-Jun-04): int rc = SQLITE_OK;
1012
1.91 (danielk1 03-Jun-04): int needXcommit = 0;
1013
1.91 (danielk1 03-Jun-04):
1014
1.258 (danielk1 25-Jul-06): /* Before doing anything else, call the xSync() callback for any
1015
1.258 (danielk1 25-Jul-06): ** virtual module tables written in this transaction. This has to
1016
1.258 (danielk1 25-Jul-06): ** be done before determining whether a master journal file is
1017
1.258 (danielk1 25-Jul-06): ** required, as an xSync() callback may add an attached database
1018
1.258 (danielk1 25-Jul-06): ** to the transaction.
1019
1.258 (danielk1 25-Jul-06): */
1020
1.258 (danielk1 25-Jul-06): rc = sqlite3VtabSync(db, rc);
1021
1.258 (danielk1 25-Jul-06): if( rc!=SQLITE_OK ){
1022
1.258 (danielk1 25-Jul-06): return rc;
1023
1.258 (danielk1 25-Jul-06): }
1024
1.258 (danielk1 25-Jul-06):
1025
1.258 (danielk1 25-Jul-06): /* This loop determines (a) if the commit hook should be invoked and
1026
1.258 (danielk1 25-Jul-06): ** (b) how many database files have open write transactions, not
1027
1.258 (danielk1 25-Jul-06): ** including the temp database. (b) is important because if more than
1028
1.258 (danielk1 25-Jul-06): ** one database file has an open write transaction, a master journal
1029
1.258 (danielk1 25-Jul-06): ** file is required for an atomic commit.
1030
1.258 (danielk1 25-Jul-06): */
1031
1.91 (danielk1 03-Jun-04): for(i=0; i<db->nDb; i++){
1032
1.91 (danielk1 03-Jun-04): Btree *pBt = db->aDb[i].pBt;
1033
1.91 (danielk1 03-Jun-04): if( pBt && sqlite3BtreeIsInTrans(pBt) ){
1034
1.91 (danielk1 03-Jun-04): needXcommit = 1;
1035
1.91 (danielk1 03-Jun-04): if( i!=1 ) nTrans++;
1036
1.91 (danielk1 03-Jun-04): }
1037
1.91 (danielk1 03-Jun-04): }
1038
1.91 (danielk1 03-Jun-04):
1039
1.91 (danielk1 03-Jun-04): /* If there are any write-transactions at all, invoke the commit hook */
1040
1.91 (danielk1 03-Jun-04): if( needXcommit && db->xCommitCallback ){
1041
1.139 (drh 02-Sep-04): sqlite3SafetyOff(db);
1042
1.139 (drh 02-Sep-04): rc = db->xCommitCallback(db->pCommitArg);
1043
1.139 (drh 02-Sep-04): sqlite3SafetyOn(db);
1044
1.139 (drh 02-Sep-04): if( rc ){
1045
1.91 (danielk1 03-Jun-04): return SQLITE_CONSTRAINT;
1046
1.91 (danielk1 03-Jun-04): }
1047
1.91 (danielk1 03-Jun-04): }
1048
1.91 (danielk1 03-Jun-04):
1049
1.128 (danielk1 26-Jun-04): /* The simple case - no more than one database file (not counting the
1050
1.128 (danielk1 26-Jun-04): ** TEMP database) has a transaction active. There is no need for the
1051
1.94 (drh 07-Jun-04): ** master-journal.
1052
1.99 (drh 09-Jun-04): **
1053
1.128 (danielk1 26-Jun-04): ** If the return value of sqlite3BtreeGetFilename() is a zero length
1054
1.128 (danielk1 26-Jun-04): ** string, it means the main database is :memory:. In that case we do
1055
1.128 (danielk1 26-Jun-04): ** not support atomic multi-file commits, so use the simple case then
1056
1.99 (drh 09-Jun-04): ** too.
1057
1.91 (danielk1 03-Jun-04): */
1058
1.128 (danielk1 26-Jun-04): if( 0==strlen(sqlite3BtreeGetFilename(db->aDb[0].pBt)) || nTrans<=1 ){
1059
1.94 (drh 07-Jun-04): for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
1060
1.91 (danielk1 03-Jun-04): Btree *pBt = db->aDb[i].pBt;
1061
1.91 (danielk1 03-Jun-04): if( pBt ){
1062
1.94 (drh 07-Jun-04): rc = sqlite3BtreeSync(pBt, 0);
1063
1.94 (drh 07-Jun-04): }
1064
1.94 (drh 07-Jun-04): }
1065
1.94 (drh 07-Jun-04):
1066
1.94 (drh 07-Jun-04): /* Do the commit only if all databases successfully synced */
1067
1.94 (drh 07-Jun-04): if( rc==SQLITE_OK ){
1068
1.94 (drh 07-Jun-04): for(i=0; i<db->nDb; i++){
1069
1.94 (drh 07-Jun-04): Btree *pBt = db->aDb[i].pBt;
1070
1.94 (drh 07-Jun-04): if( pBt ){
1071
1.94 (drh 07-Jun-04): sqlite3BtreeCommit(pBt);
1072
1.94 (drh 07-Jun-04): }
1073
1.91 (danielk1 03-Jun-04): }
1074
1.250 (danielk1 16-Jun-06): sqlite3VtabCommit(db);
1075
1.91 (danielk1 03-Jun-04): }
1076
1.91 (danielk1 03-Jun-04): }
1077
1.91 (danielk1 03-Jun-04):
1078
1.91 (danielk1 03-Jun-04): /* The complex case - There is a multi-file write-transaction active.
1079
1.91 (danielk1 03-Jun-04): ** This requires a master journal file to ensure the transaction is
1080
1.91 (danielk1 03-Jun-04): ** committed atomicly.
1081
1.91 (danielk1 03-Jun-04): */
1082
1.179 (danielk1 27-May-05): #ifndef SQLITE_OMIT_DISKIO
1083
1.91 (danielk1 03-Jun-04): else{
1084
1.188 (drh 27-Aug-05): int needSync = 0;
1085
1.91 (danielk1 03-Jun-04): char *zMaster = 0; /* File-name for the master journal */
1086
1.91 (danielk1 03-Jun-04): char const *zMainFile = sqlite3BtreeGetFilename(db->aDb[0].pBt);
1087
1.206 (drh 29-Nov-05): OsFile *master = 0;
1088
1.91 (danielk1 03-Jun-04):
1089
1.91 (danielk1 03-Jun-04): /* Select a master journal file name */
1090
1.91 (danielk1 03-Jun-04): do {
1091
1.98 (drh 09-Jun-04): u32 random;
1092
1.98 (drh 09-Jun-04): sqliteFree(zMaster);
1093
1.91 (danielk1 03-Jun-04): sqlite3Randomness(sizeof(random), &random);
1094
1.100 (drh 09-Jun-04): zMaster = sqlite3MPrintf("%s-mj%08X", zMainFile, random&0x7fffffff);
1095
1.91 (danielk1 03-Jun-04): if( !zMaster ){
1096
1.91 (danielk1 03-Jun-04): return SQLITE_NOMEM;
1097
1.91 (danielk1 03-Jun-04): }
1098
1.218 (drh 06-Jan-06): }while( sqlite3OsFileExists(zMaster) );
1099
1.91 (danielk1 03-Jun-04):
1100
1.91 (danielk1 03-Jun-04): /* Open the master journal. */
1101
1.218 (drh 06-Jan-06): rc = sqlite3OsOpenExclusive(zMaster, &master, 0);
1102
1.91 (danielk1 03-Jun-04): if( rc!=SQLITE_OK ){
1103
1.91 (danielk1 03-Jun-04): sqliteFree(zMaster);
1104
1.91 (danielk1 03-Jun-04): return rc;
1105
1.91 (danielk1 03-Jun-04): }
1106
1.91 (danielk1 03-Jun-04):
1107
1.91 (danielk1 03-Jun-04): /* Write the name of each database file in the transaction into the new
1108
1.91 (danielk1 03-Jun-04): ** master journal file. If an error occurs at this point close
1109
1.91 (danielk1 03-Jun-04): ** and delete the master journal file. All the individual journal files
1110
1.91 (danielk1 03-Jun-04): ** still have 'null' as the master journal pointer, so they will roll
1111
1.157 (danielk1 13-Jan-05): ** back independently if a failure occurs.
1112
1.91 (danielk1 03-Jun-04): */
1113
1.91 (danielk1 03-Jun-04): for(i=0; i<db->nDb; i++){
1114
1.91 (danielk1 03-Jun-04): Btree *pBt = db->aDb[i].pBt;
1115
1.99 (drh 09-Jun-04): if( i==1 ) continue; /* Ignore the TEMP database */
1116
1.91 (danielk1 03-Jun-04): if( pBt && sqlite3BtreeIsInTrans(pBt) ){
1117
1.109 (danielk1 14-Jun-04): char const *zFile = sqlite3BtreeGetJournalname(pBt);
1118
1.99 (drh 09-Jun-04): if( zFile[0]==0 ) continue; /* Ignore :memory: databases */
1119
1.188 (drh 27-Aug-05): if( !needSync && !sqlite3BtreeSyncDisabled(pBt) ){
1120
1.188 (drh 27-Aug-05): needSync = 1;
1121
1.188 (drh 27-Aug-05): }
1122
1.207 (drh 30-Nov-05): rc = sqlite3OsWrite(master, zFile, strlen(zFile)+1);
1123
1.91 (danielk1 03-Jun-04): if( rc!=SQLITE_OK ){
1124
1.207 (drh 30-Nov-05): sqlite3OsClose(&master);
1125
1.218 (drh 06-Jan-06): sqlite3OsDelete(zMaster);
1126
1.91 (danielk1 03-Jun-04): sqliteFree(zMaster);
1127
1.91 (danielk1 03-Jun-04): return rc;
1128
1.91 (danielk1 03-Jun-04): }
1129
1.91 (danielk1 03-Jun-04): }
1130
1.91 (danielk1 03-Jun-04): }
1131
1.91 (danielk1 03-Jun-04):
1132
1.109 (danielk1 14-Jun-04):
1133
1.109 (danielk1 14-Jun-04): /* Sync the master journal file. Before doing this, open the directory
1134
1.109 (danielk1 14-Jun-04): ** the master journal file is store in so that it gets synced too.
1135
1.109 (danielk1 14-Jun-04): */
1136
1.109 (danielk1 14-Jun-04): zMainFile = sqlite3BtreeGetDirname(db->aDb[0].pBt);
1137
1.207 (drh 30-Nov-05): rc = sqlite3OsOpenDirectory(master, zMainFile);
1138
1.196 (drh 08-Sep-05): if( rc!=SQLITE_OK ||
1139
1.207 (drh 30-Nov-05): (needSync && (rc=sqlite3OsSync(master,0))!=SQLITE_OK) ){
1140
1.207 (drh 30-Nov-05): sqlite3OsClose(&master);
1141
1.218 (drh 06-Jan-06): sqlite3OsDelete(zMaster);
1142
1.109 (danielk1 14-Jun-04): sqliteFree(zMaster);
1143
1.109 (danielk1 14-Jun-04): return rc;
1144
1.109 (danielk1 14-Jun-04): }
1145
1.91 (danielk1 03-Jun-04):
1146
1.91 (danielk1 03-Jun-04): /* Sync all the db files involved in the transaction. The same call
1147
1.91 (danielk1 03-Jun-04): ** sets the master journal pointer in each individual journal. If
1148
1.91 (danielk1 03-Jun-04): ** an error occurs here, do not delete the master journal file.
1149
1.91 (danielk1 03-Jun-04): **
1150
1.91 (danielk1 03-Jun-04): ** If the error occurs during the first call to sqlite3BtreeSync(),
1151
1.91 (danielk1 03-Jun-04): ** then there is a chance that the master journal file will be
1152
1.91 (danielk1 03-Jun-04): ** orphaned. But we cannot delete it, in case the master journal
1153
1.91 (danielk1 03-Jun-04): ** file name was written into the journal file before the failure
1154
1.91 (danielk1 03-Jun-04): ** occured.
1155
1.91 (danielk1 03-Jun-04): */
1156
1.258 (danielk1 25-Jul-06): for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
1157
1.91 (danielk1 03-Jun-04): Btree *pBt = db->aDb[i].pBt;
1158
1.91 (danielk1 03-Jun-04): if( pBt && sqlite3BtreeIsInTrans(pBt) ){
1159
1.91 (danielk1 03-Jun-04): rc = sqlite3BtreeSync(pBt, zMaster);
1160
1.91 (danielk1 03-Jun-04): }
1161
1.91 (danielk1 03-Jun-04): }
1162
1.207 (drh 30-Nov-05): sqlite3OsClose(&master);
1163
1.258 (danielk1 25-Jul-06): if( rc!=SQLITE_OK ){
1164
1.258 (danielk1 25-Jul-06): sqliteFree(zMaster);
1165
1.258 (danielk1 25-Jul-06): return rc;
1166
1.258 (danielk1 25-Jul-06): }
1167
1.91 (danielk1 03-Jun-04):
1168
1.110 (danielk1 14-Jun-04): /* Delete the master journal file. This commits the transaction. After
1169
1.110 (danielk1 14-Jun-04): ** doing this the directory is synced again before any individual
1170
1.110 (danielk1 14-Jun-04): ** transaction files are deleted.
1171
1.110 (danielk1 14-Jun-04): */
1172
1.218 (drh 06-Jan-06): rc = sqlite3OsDelete(zMaster);
1173
1.263 (drh 13-Aug-06): if( rc ){
1174
1.263 (drh 13-Aug-06): return rc;
1175
1.263 (drh 13-Aug-06): }
1176
1.111 (danielk1 14-Jun-04): sqliteFree(zMaster);
1177
1.111 (danielk1 14-Jun-04): zMaster = 0;
1178
1.218 (drh 06-Jan-06): rc = sqlite3OsSyncDirectory(zMainFile);
1179
1.110 (danielk1 14-Jun-04): if( rc!=SQLITE_OK ){
1180
1.110 (danielk1 14-Jun-04): /* This is not good. The master journal file has been deleted, but
1181
1.110 (danielk1 14-Jun-04): ** the directory sync failed. There is no completely safe course of
1182
1.110 (danielk1 14-Jun-04): ** action from here. The individual journals contain the name of the
1183
1.110 (danielk1 14-Jun-04): ** master journal file, but there is no way of knowing if that
1184
1.110 (danielk1 14-Jun-04): ** master journal exists now or if it will exist after the operating
1185
1.110 (danielk1 14-Jun-04): ** system crash that may follow the fsync() failure.
1186
1.110 (danielk1 14-Jun-04): */
1187
1.110 (danielk1 14-Jun-04): return rc;
1188
1.110 (danielk1 14-Jun-04): }
1189
1.91 (danielk1 03-Jun-04):
1190
1.91 (danielk1 03-Jun-04): /* All files and directories have already been synced, so the following
1191
1.91 (danielk1 03-Jun-04): ** calls to sqlite3BtreeCommit() are only closing files and deleting
1192
1.91 (danielk1 03-Jun-04): ** journals. If something goes wrong while this is happening we don't
1193
1.110 (danielk1 14-Jun-04): ** really care. The integrity of the transaction is already guaranteed,
1194
1.91 (danielk1 03-Jun-04): ** but some stray 'cold' journals may be lying around. Returning an
1195
1.91 (danielk1 03-Jun-04): ** error code won't help matters.
1196
1.91 (danielk1 03-Jun-04): */
1197
1.91 (danielk1 03-Jun-04): for(i=0; i<db->nDb; i++){
1198
1.91 (danielk1 03-Jun-04): Btree *pBt = db->aDb[i].pBt;
1199
1.91 (danielk1 03-Jun-04): if( pBt ){
1200
1.91 (danielk1 03-Jun-04): sqlite3BtreeCommit(pBt);
1201
1.91 (danielk1 03-Jun-04): }
1202
1.91 (danielk1 03-Jun-04): }
1203
1.250 (danielk1 16-Jun-06): sqlite3VtabCommit(db);
1204
1.91 (danielk1 03-Jun-04): }
1205
1.179 (danielk1 27-May-05): #endif
1206
1.112 (danielk1 14-Jun-04):
1207
1.94 (drh 07-Jun-04): return rc;
1208
1.91 (danielk1 03-Jun-04): }
1209
1.91 (danielk1 03-Jun-04):
1210
1.85 (danielk1 31-May-04): /*
1211
1.85 (danielk1 31-May-04): ** This routine checks that the sqlite3.activeVdbeCnt count variable
1212
1.85 (danielk1 31-May-04): ** matches the number of vdbe's in the list sqlite3.pVdbe that are
1213
1.85 (danielk1 31-May-04): ** currently active. An assertion fails if the two counts do not match.
1214
1.139 (drh 02-Sep-04): ** This is an internal self-check only - it is not an essential processing
1215
1.139 (drh 02-Sep-04): ** step.
1216
1.85 (danielk1 31-May-04): **
1217
1.85 (danielk1 31-May-04): ** This is a no-op if NDEBUG is defined.
1218
1.85 (danielk1 31-May-04): */
1219
1.85 (danielk1 31-May-04): #ifndef NDEBUG
1220
1.140 (drh 06-Sep-04): static void checkActiveVdbeCnt(sqlite3 *db){
1221
1.85 (danielk1 31-May-04): Vdbe *p;
1222
1.85 (danielk1 31-May-04): int cnt = 0;
1223
1.85 (danielk1 31-May-04): p = db->pVdbe;
1224
1.85 (danielk1 31-May-04): while( p ){
1225
1.139 (drh 02-Sep-04): if( p->magic==VDBE_MAGIC_RUN && p->pc>=0 ){
1226
1.85 (danielk1 31-May-04): cnt++;
1227
1.85 (danielk1 31-May-04): }
1228
1.85 (danielk1 31-May-04): p = p->pNext;
1229
1.85 (danielk1 31-May-04): }
1230
1.85 (danielk1 31-May-04): assert( cnt==db->activeVdbeCnt );
1231
1.85 (danielk1 31-May-04): }
1232
1.85 (danielk1 31-May-04): #else
1233
1.85 (danielk1 31-May-04): #define checkActiveVdbeCnt(x)
1234
1.85 (danielk1 31-May-04): #endif
1235
1.85 (danielk1 31-May-04):
1236
1.64 (danielk1 25-May-04): /*
1237
1.252 (danielk1 23-Jun-06): ** Find every active VM other than pVdbe and change its status to
1238
1.252 (danielk1 23-Jun-06): ** aborted. This happens when one VM causes a rollback due to an
1239
1.252 (danielk1 23-Jun-06): ** ON CONFLICT ROLLBACK clause (for example). The other VMs must be
1240
1.252 (danielk1 23-Jun-06): ** aborted so that they do not have data rolled out from underneath
1241
1.252 (danielk1 23-Jun-06): ** them leading to a segfault.
1242
1.252 (danielk1 23-Jun-06): */
1243
1.252 (danielk1 23-Jun-06): void sqlite3AbortOtherActiveVdbes(sqlite3 *db, Vdbe *pExcept){
1244
1.252 (danielk1 23-Jun-06): Vdbe *pOther;
1245
1.252 (danielk1 23-Jun-06): for(pOther=db->pVdbe; pOther; pOther=pOther->pNext){
1246
1.252 (danielk1 23-Jun-06): if( pOther==pExcept ) continue;
1247
1.252 (danielk1 23-Jun-06): if( pOther->magic!=VDBE_MAGIC_RUN || pOther->pc<0 ) continue;
1248
1.252 (danielk1 23-Jun-06): checkActiveVdbeCnt(db);
1249
1.252 (danielk1 23-Jun-06): closeAllCursors(pOther);
1250
1.252 (danielk1 23-Jun-06): checkActiveVdbeCnt(db);
1251
1.252 (danielk1 23-Jun-06): pOther->aborted = 1;
1252
1.252 (danielk1 23-Jun-06): }
1253
1.252 (danielk1 23-Jun-06): }
1254
1.252 (danielk1 23-Jun-06):
1255
1.252 (danielk1 23-Jun-06): /*
1256
1.139 (drh 02-Sep-04): ** This routine is called the when a VDBE tries to halt. If the VDBE
1257
1.139 (drh 02-Sep-04): ** has made changes and is in autocommit mode, then commit those
1258
1.139 (drh 02-Sep-04): ** changes. If a rollback is needed, then do the rollback.
1259
1.139 (drh 02-Sep-04): **
1260
1.139 (drh 02-Sep-04): ** This routine is the only way to move the state of a VM from
1261
1.139 (drh 02-Sep-04): ** SQLITE_MAGIC_RUN to SQLITE_MAGIC_HALT.
1262
1.1 (drh 06-Sep-03): **
1263
1.139 (drh 02-Sep-04): ** Return an error code. If the commit could not complete because of
1264
1.139 (drh 02-Sep-04): ** lock contention, return SQLITE_BUSY. If SQLITE_BUSY is returned, it
1265
1.139 (drh 02-Sep-04): ** means the close did not happen and needs to be repeated.
1266
1.1 (drh 06-Sep-03): */
1267
1.139 (drh 02-Sep-04): int sqlite3VdbeHalt(Vdbe *p){
1268
1.140 (drh 06-Sep-04): sqlite3 *db = p->db;
1269
1.1 (drh 06-Sep-03): int i;
1270
1.85 (danielk1 31-May-04): int (*xFunc)(Btree *pBt) = 0; /* Function to call on each btree backend */
1271
1.228 (danielk1 20-Jan-06): int isSpecialError; /* Set to true if SQLITE_NOMEM or IOERR */
1272
1.228 (danielk1 20-Jan-06):
1273
1.228 (danielk1 20-Jan-06): /* This function contains the logic that determines if a statement or
1274
1.228 (danielk1 20-Jan-06): ** transaction will be committed or rolled back as a result of the
1275
1.228 (danielk1 20-Jan-06): ** execution of this virtual machine.
1276
1.228 (danielk1 20-Jan-06): **
1277
1.228 (danielk1 20-Jan-06): ** Special errors:
1278
1.228 (danielk1 20-Jan-06): **
1279
1.228 (danielk1 20-Jan-06): ** If an SQLITE_NOMEM error has occured in a statement that writes to
1280
1.228 (danielk1 20-Jan-06): ** the database, then either a statement or transaction must be rolled
1281
1.228 (danielk1 20-Jan-06): ** back to ensure the tree-structures are in a consistent state. A
1282
1.228 (danielk1 20-Jan-06): ** statement transaction is rolled back if one is open, otherwise the
1283
1.228 (danielk1 20-Jan-06): ** entire transaction must be rolled back.
1284
1.228 (danielk1 20-Jan-06): **
1285
1.228 (danielk1 20-Jan-06): ** If an SQLITE_IOERR error has occured in a statement that writes to
1286
1.228 (danielk1 20-Jan-06): ** the database, then the entire transaction must be rolled back. The
1287
1.228 (danielk1 20-Jan-06): ** I/O error may have caused garbage to be written to the journal
1288
1.228 (danielk1 20-Jan-06): ** file. Were the transaction to continue and eventually be rolled
1289
1.228 (danielk1 20-Jan-06): ** back that garbage might end up in the database file.
1290
1.228 (danielk1 20-Jan-06): **
1291
1.228 (danielk1 20-Jan-06): ** In both of the above cases, the Vdbe.errorAction variable is
1292
1.228 (danielk1 20-Jan-06): ** ignored. If the sqlite3.autoCommit flag is false and a transaction
1293
1.228 (danielk1 20-Jan-06): ** is rolled back, it will be set to true.
1294
1.228 (danielk1 20-Jan-06): **
1295
1.228 (danielk1 20-Jan-06): ** Other errors:
1296
1.228 (danielk1 20-Jan-06): **
1297
1.228 (danielk1 20-Jan-06): ** No error:
1298
1.228 (danielk1 20-Jan-06): **
1299
1.228 (danielk1 20-Jan-06): */
1300
1.1 (drh 06-Sep-03):
1301
1.227 (danielk1 18-Jan-06): if( sqlite3MallocFailed() ){
1302
1.208 (danielk1 06-Dec-05): p->rc = SQLITE_NOMEM;
1303
1.208 (danielk1 06-Dec-05): }
1304
1.139 (drh 02-Sep-04): if( p->magic!=VDBE_MAGIC_RUN ){
1305
1.139 (drh 02-Sep-04): /* Already halted. Nothing to do. */
1306
1.139 (drh 02-Sep-04): assert( p->magic==VDBE_MAGIC_HALT );
1307
1.253 (danielk1 23-Jun-06): #ifndef SQLITE_OMIT_VIRTUALTABLE
1308
1.253 (danielk1 23-Jun-06): closeAllCursors(p);
1309
1.253 (danielk1 23-Jun-06): #endif
1310
1.139 (drh 02-Sep-04): return SQLITE_OK;
1311
1.1 (drh 06-Sep-03): }
1312
1.139 (drh 02-Sep-04): closeAllCursors(p);
1313
1.85 (danielk1 31-May-04): checkActiveVdbeCnt(db);
1314
1.208 (danielk1 06-Dec-05):
1315
1.228 (danielk1 20-Jan-06): /* No commit or rollback needed if the program never started */
1316
1.228 (danielk1 20-Jan-06): if( p->pc>=0 ){
1317
1.266 (drh 23-Sep-06): int mrc; /* Primary error code from p->rc */
1318
1.228 (danielk1 20-Jan-06): /* Check for one of the special errors - SQLITE_NOMEM or SQLITE_IOERR */
1319
1.266 (drh 23-Sep-06): mrc = p->rc & 0xff;
1320
1.266 (drh 23-Sep-06): isSpecialError = ((mrc==SQLITE_NOMEM || mrc==SQLITE_IOERR)?1:0);
1321
1.228 (danielk1 20-Jan-06): if( isSpecialError ){
1322
1.208 (danielk1 06-Dec-05): /* This loop does static analysis of the query to see which of the
1323
1.208 (danielk1 06-Dec-05): ** following three categories it falls into:
1324
1.208 (danielk1 06-Dec-05): **
1325
1.208 (danielk1 06-Dec-05): ** Read-only
1326
1.228 (danielk1 20-Jan-06): ** Query with statement journal
1327
1.228 (danielk1 20-Jan-06): ** Query without statement journal
1328
1.208 (danielk1 06-Dec-05): **
1329
1.208 (danielk1 06-Dec-05): ** We could do something more elegant than this static analysis (i.e.
1330
1.208 (danielk1 06-Dec-05): ** store the type of query as part of the compliation phase), but
1331
1.228 (danielk1 20-Jan-06): ** handling malloc() or IO failure is a fairly obscure edge case so
1332
1.228 (danielk1 20-Jan-06): ** this is probably easier. Todo: Might be an opportunity to reduce
1333
1.228 (danielk1 20-Jan-06): ** code size a very small amount though...
1334
1.208 (danielk1 06-Dec-05): */
1335
1.208 (danielk1 06-Dec-05): int isReadOnly = 1;
1336
1.208 (danielk1 06-Dec-05): int isStatement = 0;
1337
1.208 (danielk1 06-Dec-05): assert(p->aOp || p->nOp==0);
1338
1.208 (danielk1 06-Dec-05): for(i=0; i<p->nOp; i++){
1339
1.208 (danielk1 06-Dec-05): switch( p->aOp[i].opcode ){
1340
1.208 (danielk1 06-Dec-05): case OP_Transaction:
1341
1.208 (danielk1 06-Dec-05): isReadOnly = 0;
1342
1.208 (danielk1 06-Dec-05): break;
1343
1.208 (danielk1 06-Dec-05): case OP_Statement:
1344
1.208 (danielk1 06-Dec-05): isStatement = 1;
1345
1.208 (danielk1 06-Dec-05): break;
1346
1.208 (danielk1 06-Dec-05): }
1347
1.208 (danielk1 06-Dec-05): }
1348
1.228 (danielk1 20-Jan-06):
1349
1.228 (danielk1 20-Jan-06): /* If the query was read-only, we need do no rollback at all. Otherwise,
1350
1.228 (danielk1 20-Jan-06): ** proceed with the special handling.
1351
1.228 (danielk1 20-Jan-06): */
1352
1.228 (danielk1 20-Jan-06): if( !isReadOnly ){
1353
1.228 (danielk1 20-Jan-06): if( p->rc==SQLITE_NOMEM && isStatement ){
1354
1.228 (danielk1 20-Jan-06): xFunc = sqlite3BtreeRollbackStmt;
1355
1.228 (danielk1 20-Jan-06): }else{
1356
1.228 (danielk1 20-Jan-06): /* We are forced to roll back the active transaction. Before doing
1357
1.228 (danielk1 20-Jan-06): ** so, abort any other statements this handle currently has active.
1358
1.228 (danielk1 20-Jan-06): */
1359
1.233 (danielk1 24-Jan-06): sqlite3AbortOtherActiveVdbes(db, p);
1360
1.229 (danielk1 20-Jan-06): sqlite3RollbackAll(db);
1361
1.228 (danielk1 20-Jan-06): db->autoCommit = 1;
1362
1.228 (danielk1 20-Jan-06): }
1363
1.208 (danielk1 06-Dec-05): }
1364
1.208 (danielk1 06-Dec-05): }
1365
1.228 (danielk1 20-Jan-06):
1366
1.228 (danielk1 20-Jan-06): /* If the auto-commit flag is set and this is the only active vdbe, then
1367
1.228 (danielk1 20-Jan-06): ** we do either a commit or rollback of the current transaction.
1368
1.228 (danielk1 20-Jan-06): **
1369
1.228 (danielk1 20-Jan-06): ** Note: This block also runs if one of the special errors handled
1370
1.228 (danielk1 20-Jan-06): ** above has occured.
1371
1.228 (danielk1 20-Jan-06): */
1372
1.228 (danielk1 20-Jan-06): if( db->autoCommit && db->activeVdbeCnt==1 ){
1373
1.228 (danielk1 20-Jan-06): if( p->rc==SQLITE_OK || (p->errorAction==OE_Fail && !isSpecialError) ){
1374
1.228 (danielk1 20-Jan-06): /* The auto-commit flag is true, and the vdbe program was
1375
1.228 (danielk1 20-Jan-06): ** successful or hit an 'OR FAIL' constraint. This means a commit
1376
1.228 (danielk1 20-Jan-06): ** is required.
1377
1.228 (danielk1 20-Jan-06): */
1378
1.228 (danielk1 20-Jan-06): int rc = vdbeCommit(db);
1379
1.228 (danielk1 20-Jan-06): if( rc==SQLITE_BUSY ){
1380
1.228 (danielk1 20-Jan-06): return SQLITE_BUSY;
1381
1.228 (danielk1 20-Jan-06): }else if( rc!=SQLITE_OK ){
1382
1.228 (danielk1 20-Jan-06): p->rc = rc;
1383
1.229 (danielk1 20-Jan-06): sqlite3RollbackAll(db);
1384
1.228 (danielk1 20-Jan-06): }else{
1385
1.228 (danielk1 20-Jan-06): sqlite3CommitInternalChanges(db);
1386
1.228 (danielk1 20-Jan-06): }
1387
1.228 (danielk1 20-Jan-06): }else{
1388
1.229 (danielk1 20-Jan-06): sqlite3RollbackAll(db);
1389
1.228 (danielk1 20-Jan-06): }
1390
1.228 (danielk1 20-Jan-06): }else if( !xFunc ){
1391
1.228 (danielk1 20-Jan-06): if( p->rc==SQLITE_OK || p->errorAction==OE_Fail ){
1392
1.228 (danielk1 20-Jan-06): xFunc = sqlite3BtreeCommitStmt;
1393
1.228 (danielk1 20-Jan-06): }else if( p->errorAction==OE_Abort ){
1394
1.228 (danielk1 20-Jan-06): xFunc = sqlite3BtreeRollbackStmt;
1395
1.228 (danielk1 20-Jan-06): }else{
1396
1.233 (danielk1 24-Jan-06): sqlite3AbortOtherActiveVdbes(db, p);
1397
1.229 (danielk1 20-Jan-06): sqlite3RollbackAll(db);
1398
1.228 (danielk1 20-Jan-06): db->autoCommit = 1;
1399
1.228 (danielk1 20-Jan-06): }
1400
1.228 (danielk1 20-Jan-06): }
1401
1.228 (danielk1 20-Jan-06):
1402
1.228 (danielk1 20-Jan-06): /* If xFunc is not NULL, then it is one of sqlite3BtreeRollbackStmt or
1403
1.228 (danielk1 20-Jan-06): ** sqlite3BtreeCommitStmt. Call it once on each backend. If an error occurs
1404
1.228 (danielk1 20-Jan-06): ** and the return code is still SQLITE_OK, set the return code to the new
1405
1.228 (danielk1 20-Jan-06): ** error value.
1406
1.228 (danielk1 20-Jan-06): */
1407
1.228 (danielk1 20-Jan-06): assert(!xFunc ||
1408
1.228 (danielk1 20-Jan-06): xFunc==sqlite3BtreeCommitStmt ||
1409
1.228 (danielk1 20-Jan-06): xFunc==sqlite3BtreeRollbackStmt
1410
1.228 (danielk1 20-Jan-06): );
1411
1.228 (danielk1 20-Jan-06): for(i=0; xFunc && i<db->nDb; i++){
1412
1.228 (danielk1 20-Jan-06): int rc;
1413
1.228 (danielk1 20-Jan-06): Btree *pBt = db->aDb[i].pBt;
1414
1.228 (danielk1 20-Jan-06): if( pBt ){
1415
1.228 (danielk1 20-Jan-06): rc = xFunc(pBt);
1416
1.231 (danielk1 23-Jan-06): if( rc && (p->rc==SQLITE_OK || p->rc==SQLITE_CONSTRAINT) ){
1417
1.231 (danielk1 23-Jan-06): p->rc = rc;
1418
1.231 (danielk1 23-Jan-06): sqlite3SetString(&p->zErrMsg, 0);
1419
1.231 (danielk1 23-Jan-06): }
1420
1.228 (danielk1 20-Jan-06): }
1421
1.85 (danielk1 31-May-04): }
1422
1.228 (danielk1 20-Jan-06):
1423
1.228 (danielk1 20-Jan-06): /* If this was an INSERT, UPDATE or DELETE and the statement was committed,
1424
1.228 (danielk1 20-Jan-06): ** set the change counter.
1425
1.228 (danielk1 20-Jan-06): */
1426
1.228 (danielk1 20-Jan-06): if( p->changeCntOn && p->pc>=0 ){
1427
1.228 (danielk1 20-Jan-06): if( !xFunc || xFunc==sqlite3BtreeCommitStmt ){
1428
1.228 (danielk1 20-Jan-06): sqlite3VdbeSetChanges(db, p->nChange);
1429
1.228 (danielk1 20-Jan-06): }else{
1430
1.228 (danielk1 20-Jan-06): sqlite3VdbeSetChanges(db, 0);
1431
1.228 (danielk1 20-Jan-06): }
1432
1.228 (danielk1 20-Jan-06): p->nChange = 0;
1433
1.87 (danielk1 31-May-04): }
1434
1.228 (danielk1 20-Jan-06):
1435
1.228 (danielk1 20-Jan-06): /* Rollback or commit any schema changes that occurred. */
1436
1.228 (danielk1 20-Jan-06): if( p->rc!=SQLITE_OK && db->flags&SQLITE_InternChanges ){
1437
1.228 (danielk1 20-Jan-06): sqlite3ResetInternalSchema(db, 0);
1438
1.228 (danielk1 20-Jan-06): db->flags = (db->flags | SQLITE_InternChanges);
1439
1.121 (danielk1 21-Jun-04): }
1440
1.1 (drh 06-Sep-03): }
1441
1.85 (danielk1 31-May-04):
1442
1.254 (danielk1 24-Jun-06): /* We have successfully halted and closed the VM. Record this fact. */
1443
1.254 (danielk1 24-Jun-06): if( p->pc>=0 ){
1444
1.85 (danielk1 31-May-04): db->activeVdbeCnt--;
1445
1.1 (drh 06-Sep-03): }
1446
1.139 (drh 02-Sep-04): p->magic = VDBE_MAGIC_HALT;
1447
1.139 (drh 02-Sep-04): checkActiveVdbeCnt(db);
1448
1.139 (drh 02-Sep-04):
1449
1.139 (drh 02-Sep-04): return SQLITE_OK;
1450
1.139 (drh 02-Sep-04): }
1451
1.139 (drh 02-Sep-04):
1452
1.139 (drh 02-Sep-04): /*
1453
1.270 (drh 09-Jan-07): ** Each VDBE holds the result of the most recent sqlite3_step() call
1454
1.270 (drh 09-Jan-07): ** in p->rc. This routine sets that result back to SQLITE_OK.
1455
1.270 (drh 09-Jan-07): */
1456
1.270 (drh 09-Jan-07): void sqlite3VdbeResetStepResult(Vdbe *p){
1457
1.270 (drh 09-Jan-07): p->rc = SQLITE_OK;
1458
1.270 (drh 09-Jan-07): }
1459
1.270 (drh 09-Jan-07):
1460
1.270 (drh 09-Jan-07): /*
1461
1.139 (drh 02-Sep-04): ** Clean up a VDBE after execution but do not delete the VDBE just yet.
1462
1.139 (drh 02-Sep-04): ** Write any error messages into *pzErrMsg. Return the result code.
1463
1.139 (drh 02-Sep-04): **
1464
1.139 (drh 02-Sep-04): ** After this routine is run, the VDBE should be ready to be executed
1465
1.139 (drh 02-Sep-04): ** again.
1466
1.139 (drh 02-Sep-04): **
1467
1.139 (drh 02-Sep-04): ** To look at it another way, this routine resets the state of the
1468
1.139 (drh 02-Sep-04): ** virtual machine from VDBE_MAGIC_RUN or VDBE_MAGIC_HALT back to
1469
1.139 (drh 02-Sep-04): ** VDBE_MAGIC_INIT.
1470
1.139 (drh 02-Sep-04): */
1471
1.139 (drh 02-Sep-04): int sqlite3VdbeReset(Vdbe *p){
1472
1.265 (drh 15-Sep-06): sqlite3 *db;
1473
1.139 (drh 02-Sep-04): if( p->magic!=VDBE_MAGIC_RUN && p->magic!=VDBE_MAGIC_HALT ){
1474
1.148 (drh 30-Sep-04): sqlite3Error(p->db, SQLITE_MISUSE, 0);
1475
1.139 (drh 02-Sep-04): return SQLITE_MISUSE;
1476
1.139 (drh 02-Sep-04): }
1477
1.265 (drh 15-Sep-06): db = p->db;
1478
1.85 (danielk1 31-May-04):
1479
1.139 (drh 02-Sep-04): /* If the VM did not run to completion or if it encountered an
1480
1.139 (drh 02-Sep-04): ** error, then it might not have been halted properly. So halt
1481
1.139 (drh 02-Sep-04): ** it now.
1482
1.139 (drh 02-Sep-04): */
1483
1.265 (drh 15-Sep-06): sqlite3SafetyOn(db);
1484
1.139 (drh 02-Sep-04): sqlite3VdbeHalt(p);
1485
1.265 (drh 15-Sep-06): sqlite3SafetyOff(db);
1486
1.139 (drh 02-Sep-04):
1487
1.162 (drh 24-Jan-05): /* If the VDBE has be run even partially, then transfer the error code
1488
1.162 (drh 24-Jan-05): ** and error message from the VDBE into the main database structure. But
1489
1.162 (drh 24-Jan-05): ** if the VDBE has just been set to run but has not actually executed any
1490
1.162 (drh 24-Jan-05): ** instructions yet, leave the main database error information unchanged.
1491
1.139 (drh 02-Sep-04): */
1492
1.162 (drh 24-Jan-05): if( p->pc>=0 ){
1493
1.162 (drh 24-Jan-05): if( p->zErrMsg ){
1494
1.229 (danielk1 20-Jan-06): sqlite3ValueSetStr(db->pErr, -1, p->zErrMsg, SQLITE_UTF8, sqlite3FreeX);
1495
1.229 (danielk1 20-Jan-06): db->errCode = p->rc;
1496
1.162 (drh 24-Jan-05): p->zErrMsg = 0;
1497
1.162 (drh 24-Jan-05): }else if( p->rc ){
1498
1.265 (drh 15-Sep-06): sqlite3Error(db, p->rc, 0);
1499
1.162 (drh 24-Jan-05): }else{
1500
1.265 (drh 15-Sep-06): sqlite3Error(db, SQLITE_OK, 0);
1501
1.162 (drh 24-Jan-05): }
1502
1.163 (danielk1 24-Jan-05): }else if( p->rc && p->expired ){
1503
1.163 (danielk1 24-Jan-05): /* The expired flag was set on the VDBE before the first call
1504
1.163 (danielk1 24-Jan-05): ** to sqlite3_step(). For consistency (since sqlite3_step() was
1505
1.163 (danielk1 24-Jan-05): ** called), set the database error in this case as well.
1506
1.163 (danielk1 24-Jan-05): */
1507
1.265 (drh 15-Sep-06): sqlite3Error(db, p->rc, 0);
1508
1.139 (drh 02-Sep-04): }
1509
1.139 (drh 02-Sep-04):
1510
1.139 (drh 02-Sep-04): /* Reclaim all memory used by the VDBE
1511
1.139 (drh 02-Sep-04): */
1512
1.139 (drh 02-Sep-04): Cleanup(p);
1513
1.139 (drh 02-Sep-04):
1514
1.139 (drh 02-Sep-04): /* Save profiling information from this VDBE run.
1515
1.139 (drh 02-Sep-04): */
1516
1.208 (danielk1 06-Dec-05): assert( p->pTos<&p->aStack[p->pc<0?0:p->pc] || !p->aStack );
1517
1.1 (drh 06-Sep-03): #ifdef VDBE_PROFILE
1518
1.1 (drh 06-Sep-03): {
1519
1.1 (drh 06-Sep-03): FILE *out = fopen("vdbe_profile.out", "a");
1520
1.1 (drh 06-Sep-03): if( out ){
1521
1.1 (drh 06-Sep-03): int i;
1522
1.1 (drh 06-Sep-03): fprintf(out, "---- ");
1523
1.1 (drh 06-Sep-03): for(i=0; i<p->nOp; i++){
1524
1.1 (drh 06-Sep-03): fprintf(out, "%02x", p->aOp[i].opcode);
1525
1.1 (drh 06-Sep-03): }
1526
1.1 (drh 06-Sep-03): fprintf(out, "\n");
1527
1.1 (drh 06-Sep-03): for(i=0; i<p->nOp; i++){
1528
1.1 (drh 06-Sep-03): fprintf(out, "%6d %10lld %8lld ",
1529
1.1 (drh 06-Sep-03): p->aOp[i].cnt,
1530
1.1 (drh 06-Sep-03): p->aOp[i].cycles,
1531
1.1 (drh 06-Sep-03): p->aOp[i].cnt>0 ? p->aOp[i].cycles/p->aOp[i].cnt : 0
1532
1.1 (drh 06-Sep-03): );
1533
1.19 (danielk1 08-May-04): sqlite3VdbePrintOp(out, i, &p->aOp[i]);
1534
1.1 (drh 06-Sep-03): }
1535
1.1 (drh 06-Sep-03): fclose(out);
1536
1.1 (drh 06-Sep-03): }
1537
1.1 (drh 06-Sep-03): }
1538
1.1 (drh 06-Sep-03): #endif
1539
1.1 (drh 06-Sep-03): p->magic = VDBE_MAGIC_INIT;
1540
1.133 (drh 30-Jun-04): p->aborted = 0;
1541
1.160 (drh 23-Jan-05): if( p->rc==SQLITE_SCHEMA ){
1542
1.265 (drh 15-Sep-06): sqlite3ResetInternalSchema(db, 0);
1543
1.160 (drh 23-Jan-05): }
1544
1.265 (drh 15-Sep-06): return p->rc & db->errMask;
1545
1.1 (drh 06-Sep-03): }
1546
1.139 (drh 02-Sep-04):
1547
1.1 (drh 06-Sep-03): /*
1548
1.1 (drh 06-Sep-03): ** Clean up and delete a VDBE after execution. Return an integer which is
1549
1.1 (drh 06-Sep-03): ** the result code. Write any error message text into *pzErrMsg.
1550
1.1 (drh 06-Sep-03): */
1551
1.122 (danielk1 21-Jun-04): int sqlite3VdbeFinalize(Vdbe *p){
1552
1.129 (danielk1 26-Jun-04): int rc = SQLITE_OK;
1553
1.129 (danielk1 26-Jun-04): if( p->magic==VDBE_MAGIC_RUN || p->magic==VDBE_MAGIC_HALT ){
1554
1.129 (danielk1 26-Jun-04): rc = sqlite3VdbeReset(p);
1555
1.265 (drh 15-Sep-06): assert( (rc & p->db->errMask)==rc );
1556
1.129 (danielk1 26-Jun-04): }else if( p->magic!=VDBE_MAGIC_INIT ){
1557
1.1 (drh 06-Sep-03): return SQLITE_MISUSE;
1558
1.1 (drh 06-Sep-03): }
1559
1.19 (danielk1 08-May-04): sqlite3VdbeDelete(p);
1560
1.1 (drh 06-Sep-03): return rc;
1561
1.42 (danielk1 19-May-04): }
1562
1.1 (drh 06-Sep-03):
1563
1.1 (drh 06-Sep-03): /*
1564
1.120 (drh 19-Jun-04): ** Call the destructor for each auxdata entry in pVdbeFunc for which
1565
1.123 (danielk1 21-Jun-04): ** the corresponding bit in mask is clear. Auxdata entries beyond 31
1566
1.120 (drh 19-Jun-04): ** are always destroyed. To destroy all auxdata entries, call this
1567
1.123 (danielk1 21-Jun-04): ** routine with mask==0.
1568
1.120 (drh 19-Jun-04): */
1569
1.120 (drh 19-Jun-04): void sqlite3VdbeDeleteAuxData(VdbeFunc *pVdbeFunc, int mask){
1570
1.120 (drh 19-Jun-04): int i;
1571
1.120 (drh 19-Jun-04): for(i=0; i<pVdbeFunc->nAux; i++){
1572
1.120 (drh 19-Jun-04): struct AuxData *pAux = &pVdbeFunc->apAux[i];
1573
1.120 (drh 19-Jun-04): if( (i>31 || !(mask&(1<<i))) && pAux->pAux ){
1574
1.120 (drh 19-Jun-04): if( pAux->xDelete ){
1575
1.120 (drh 19-Jun-04): pAux->xDelete(pAux->pAux);
1576
1.120 (drh 19-Jun-04): }
1577
1.120 (drh 19-Jun-04): pAux->pAux = 0;
1578
1.120 (drh 19-Jun-04): }
1579
1.120 (drh 19-Jun-04): }
1580
1.120 (drh 19-Jun-04): }
1581
1.120 (drh 19-Jun-04):
1582
1.120 (drh 19-Jun-04): /*
1583
1.1 (drh 06-Sep-03): ** Delete an entire VDBE.
1584
1.1 (drh 06-Sep-03): */
1585
1.19 (danielk1 08-May-04): void sqlite3VdbeDelete(Vdbe *p){
1586
1.1 (drh 06-Sep-03): int i;
1587
1.1 (drh 06-Sep-03): if( p==0 ) return;
1588
1.1 (drh 06-Sep-03): Cleanup(p);
1589
1.1 (drh 06-Sep-03): if( p->pPrev ){
1590
1.1 (drh 06-Sep-03): p->pPrev->pNext = p->pNext;
1591
1.1 (drh 06-Sep-03): }else{
1592
1.1 (drh 06-Sep-03): assert( p->db->pVdbe==p );
1593
1.1 (drh 06-Sep-03): p->db->pVdbe = p->pNext;
1594
1.1 (drh 06-Sep-03): }
1595
1.1 (drh 06-Sep-03): if( p->pNext ){
1596
1.1 (drh 06-Sep-03): p->pNext->pPrev = p->pPrev;
1597
1.1 (drh 06-Sep-03): }
1598
1.146 (drh 24-Sep-04): if( p->aOp ){
1599
1.146 (drh 24-Sep-04): for(i=0; i<p->nOp; i++){
1600
1.146 (drh 24-Sep-04): Op *pOp = &p->aOp[i];
1601
1.198 (drh 16-Sep-05): freeP3(pOp->p3type, pOp->p3);
1602
1.92 (danielk1 05-Jun-04): }
1603
1.146 (drh 24-Sep-04): sqliteFree(p->aOp);
1604
1.2 (drh 06-Sep-03): }
1605
1.146 (drh 24-Sep-04): releaseMemArray(p->aVar, p->nVar);
1606
1.1 (drh 06-Sep-03): sqliteFree(p->aLabel);
1607
1.1 (drh 06-Sep-03): sqliteFree(p->aStack);
1608
1.236 (danielk1 10-Feb-06): releaseMemArray(p->aColName, p->nResColumn*COLNAME_N);
1609
1.146 (drh 24-Sep-04): sqliteFree(p->aColName);
1610
1.268 (drh 09-Nov-06): sqliteFree(p->zSql);
1611
1.1 (drh 06-Sep-03): p->magic = VDBE_MAGIC_DEAD;
1612
1.1 (drh 06-Sep-03): sqliteFree(p);
1613
1.1 (drh 06-Sep-03): }
1614
1.7 (drh 07-Jan-04):
1615
1.7 (drh 07-Jan-04): /*
1616
1.7 (drh 07-Jan-04): ** If a MoveTo operation is pending on the given cursor, then do that
1617
1.7 (drh 07-Jan-04): ** MoveTo now. Return an error code. If no MoveTo is pending, this
1618
1.7 (drh 07-Jan-04): ** routine does nothing and returns SQLITE_OK.
1619
1.7 (drh 07-Jan-04): */
1620
1.19 (danielk1 08-May-04): int sqlite3VdbeCursorMoveto(Cursor *p){
1621
1.7 (drh 07-Jan-04): if( p->deferredMoveto ){
1622
1.165 (drh 26-Jan-05): int res, rc;
1623
1.264 (adamd 14-Sep-06): #ifdef SQLITE_TEST
1624
1.24 (danielk1 10-May-04): extern int sqlite3_search_count;
1625
1.264 (adamd 14-Sep-06): #endif
1626
1.181 (drh 12-Jun-05): assert( p->isTable );
1627
1.181 (drh 12-Jun-05): if( p->isTable ){
1628
1.165 (drh 26-Jan-05): rc = sqlite3BtreeMoveto(p->pCursor, 0, p->movetoTarget, &res);
1629
1.26 (danielk1 11-May-04): }else{
1630
1.165 (drh 26-Jan-05): rc = sqlite3BtreeMoveto(p->pCursor,(char*)&p->movetoTarget,
1631
1.165 (drh 26-Jan-05): sizeof(i64),&res);
1632
1.26 (danielk1 11-May-04): }
1633
1.165 (drh 26-Jan-05): if( rc ) return rc;
1634
1.50 (drh 20-May-04): *p->pIncrKey = 0;
1635
1.181 (drh 12-Jun-05): p->lastRowid = keyToInt(p->movetoTarget);
1636
1.181 (drh 12-Jun-05): p->rowidIsValid = res==0;
1637
1.7 (drh 07-Jan-04): if( res<0 ){
1638
1.165 (drh 26-Jan-05): rc = sqlite3BtreeNext(p->pCursor, &res);
1639
1.165 (drh 26-Jan-05): if( rc ) return rc;
1640
1.7 (drh 07-Jan-04): }
1641
1.262 (drh 08-Aug-06): #ifdef SQLITE_TEST
1642
1.24 (danielk1 10-May-04): sqlite3_search_count++;
1643
1.262 (drh 08-Aug-06): #endif
1644
1.7 (drh 07-Jan-04): p->deferredMoveto = 0;
1645
1.219 (drh 07-Jan-06): p->cacheStatus = CACHE_STALE;
1646
1.7 (drh 07-Jan-04): }
1647
1.7 (drh 07-Jan-04): return SQLITE_OK;
1648
1.7 (drh 07-Jan-04): }
1649
1.19 (danielk1 08-May-04):
1650
1.20 (drh 08-May-04): /*
1651
1.28 (danielk1 12-May-04): ** The following functions:
1652
1.23 (danielk1 10-May-04): **
1653
1.28 (danielk1 12-May-04): ** sqlite3VdbeSerialType()
1654
1.28 (danielk1 12-May-04): ** sqlite3VdbeSerialTypeLen()
1655
1.28 (danielk1 12-May-04): ** sqlite3VdbeSerialRead()
1656
1.23 (danielk1 10-May-04): ** sqlite3VdbeSerialLen()
1657
1.28 (danielk1 12-May-04): ** sqlite3VdbeSerialWrite()
1658
1.23 (danielk1 10-May-04): **
1659
1.23 (danielk1 10-May-04): ** encapsulate the code that serializes values for storage in SQLite
1660
1.28 (danielk1 12-May-04): ** data and index records. Each serialized value consists of a
1661
1.28 (danielk1 12-May-04): ** 'serial-type' and a blob of data. The serial type is an 8-byte unsigned
1662
1.28 (danielk1 12-May-04): ** integer, stored as a varint.
1663
1.23 (danielk1 10-May-04): **
1664
1.28 (danielk1 12-May-04): ** In an SQLite index record, the serial type is stored directly before
1665
1.28 (danielk1 12-May-04): ** the blob of data that it corresponds to. In a table record, all serial
1666
1.28 (danielk1 12-May-04): ** types are stored at the start of the record, and the blobs of data at
1667
1.28 (danielk1 12-May-04): ** the end. Hence these functions allow the caller to handle the
1668
1.28 (danielk1 12-May-04): ** serial-type and data blob seperately.
1669
1.28 (danielk1 12-May-04): **
1670
1.28 (danielk1 12-May-04): ** The following table describes the various storage classes for data:
1671
1.28 (danielk1 12-May-04): **
1672
1.28 (danielk1 12-May-04): ** serial type bytes of data type
1673
1.23 (danielk1 10-May-04): ** -------------- --------------- ---------------
1674
1.84 (drh 30-May-04): ** 0 0 NULL
1675
1.23 (danielk1 10-May-04): ** 1 1 signed integer
1676
1.23 (danielk1 10-May-04): ** 2 2 signed integer
1677
1.84 (drh 30-May-04): ** 3 3 signed integer
1678
1.84 (drh 30-May-04): ** 4 4 signed integer
1679
1.84 (drh 30-May-04): ** 5 6 signed integer
1680
1.84 (drh 30-May-04): ** 6 8 signed integer
1681
1.84 (drh 30-May-04): ** 7 8 IEEE float
1682
1.215 (drh 29-Dec-05): ** 8 0 Integer constant 0
1683
1.215 (drh 29-Dec-05): ** 9 0 Integer constant 1
1684
1.215 (drh 29-Dec-05): ** 10,11 reserved for expansion
1685
1.23 (danielk1 10-May-04): ** N>=12 and even (N-12)/2 BLOB
1686
1.23 (danielk1 10-May-04): ** N>=13 and odd (N-13)/2 text
1687
1.23 (danielk1 10-May-04): **
1688
1.217 (drh 02-Jan-06): ** The 8 and 9 types were added in 3.3.0, file format 4. Prior versions
1689
1.217 (drh 02-Jan-06): ** of SQLite will not understand those serial types.
1690
1.23 (danielk1 10-May-04): */
1691
1.23 (danielk1 10-May-04):
1692
1.23 (danielk1 10-May-04): /*
1693
1.28 (danielk1 12-May-04): ** Return the serial-type for the value stored in pMem.
1694
1.22 (danielk1 10-May-04): */
1695
1.215 (drh 29-Dec-05): u32 sqlite3VdbeSerialType(Mem *pMem, int file_format){
1696
1.28 (danielk1 12-May-04): int flags = pMem->flags;
1697
1.28 (danielk1 12-May-04):
1698
1.28 (danielk1 12-May-04): if( flags&MEM_Null ){
1699
1.84 (drh 30-May-04): return 0;
1700
1.23 (danielk1 10-May-04): }
1701
1.28 (danielk1 12-May-04): if( flags&MEM_Int ){
1702
1.158 (drh 20-Jan-05): /* Figure out whether to use 1, 2, 4, 6 or 8 bytes. */
1703
1.175 (drh 15-Apr-05): # define MAX_6BYTE ((((i64)0x00001000)<<32)-1)
1704
1.28 (danielk1 12-May-04): i64 i = pMem->i;
1705
1.215 (drh 29-Dec-05): u64 u;
1706
1.215 (drh 29-Dec-05): if( file_format>=4 && (i&1)==i ){
1707
1.215 (drh 29-Dec-05): return 8+i;
1708
1.215 (drh 29-Dec-05): }
1709
1.215 (drh 29-Dec-05): u = i<0 ? -i : i;
1710
1.164 (drh 26-Jan-05): if( u<=127 ) return 1;
1711
1.164 (drh 26-Jan-05): if( u<=32767 ) return 2;
1712
1.164 (drh 26-Jan-05): if( u<=8388607 ) return 3;
1713
1.164 (drh 26-Jan-05): if( u<=2147483647 ) return 4;
1714
1.164 (drh 26-Jan-05): if( u<=MAX_6BYTE ) return 5;
1715
1.84 (drh 30-May-04): return 6;
1716
1.28 (danielk1 12-May-04): }
1717
1.28 (danielk1 12-May-04): if( flags&MEM_Real ){
1718
1.84 (drh 30-May-04): return 7;
1719
1.23 (danielk1 10-May-04): }
1720
1.28 (danielk1 12-May-04): if( flags&MEM_Str ){
1721
1.58 (danielk1 23-May-04): int n = pMem->n;
1722
1.58 (danielk1 23-May-04): assert( n>=0 );
1723
1.58 (danielk1 23-May-04): return ((n*2) + 13);
1724
1.23 (danielk1 10-May-04): }
1725
1.28 (danielk1 12-May-04): if( flags&MEM_Blob ){
1726
1.28 (danielk1 12-May-04): return (pMem->n*2 + 12);
1727
1.23 (danielk1 10-May-04): }
1728
1.28 (danielk1 12-May-04): return 0;
1729
1.22 (danielk1 10-May-04): }
1730
1.22 (danielk1 10-May-04):
1731
1.22 (danielk1 10-May-04): /*
1732
1.28 (danielk1 12-May-04): ** Return the length of the data corresponding to the supplied serial-type.
1733
1.22 (danielk1 10-May-04): */
1734
1.75 (drh 28-May-04): int sqlite3VdbeSerialTypeLen(u32 serial_type){
1735
1.84 (drh 30-May-04): if( serial_type>=12 ){
1736
1.79 (drh 28-May-04): return (serial_type-12)/2;
1737
1.79 (drh 28-May-04): }else{
1738
1.150 (drh 06-Oct-04): static const u8 aSize[] = { 0, 1, 2, 3, 4, 6, 8, 8, 0, 0, 0, 0 };
1739
1.79 (drh 28-May-04): return aSize[serial_type];
1740
1.79 (drh 28-May-04): }
1741
1.28 (danielk1 12-May-04): }
1742
1.23 (danielk1 10-May-04):
1743
1.28 (danielk1 12-May-04): /*
1744
1.28 (danielk1 12-May-04): ** Write the serialized data blob for the value stored in pMem into
1745
1.28 (danielk1 12-May-04): ** buf. It is assumed that the caller has allocated sufficient space.
1746
1.28 (danielk1 12-May-04): ** Return the number of bytes written.
1747
1.28 (danielk1 12-May-04): */
1748
1.215 (drh 29-Dec-05): int sqlite3VdbeSerialPut(unsigned char *buf, Mem *pMem, int file_format){
1749
1.215 (drh 29-Dec-05): u32 serial_type = sqlite3VdbeSerialType(pMem, file_format);
1750
1.28 (danielk1 12-May-04): int len;
1751
1.30 (danielk1 13-May-04):
1752
1.54 (drh 21-May-04): /* Integer and Real */
1753
1.215 (drh 29-Dec-05): if( serial_type<=7 && serial_type>0 ){
1754
1.54 (drh 21-May-04): u64 v;
1755
1.54 (drh 21-May-04): int i;
1756
1.84 (drh 30-May-04): if( serial_type==7 ){
1757
1.54 (drh 21-May-04): v = *(u64*)&pMem->r;
1758
1.54 (drh 21-May-04): }else{
1759
1.54 (drh 21-May-04): v = *(u64*)&pMem->i;
1760
1.54 (drh 21-May-04): }
1761
1.54 (drh 21-May-04): len = i = sqlite3VdbeSerialTypeLen(serial_type);
1762
1.54 (drh 21-May-04): while( i-- ){
1763
1.54 (drh 21-May-04): buf[i] = (v&0xFF);
1764
1.54 (drh 21-May-04): v >>= 8;
1765
1.54 (drh 21-May-04): }
1766
1.54 (drh 21-May-04): return len;
1767
1.28 (danielk1 12-May-04): }
1768
1.215 (drh 29-Dec-05):
1769
1.28 (danielk1 12-May-04): /* String or blob */
1770
1.215 (drh 29-Dec-05): if( serial_type>=12 ){
1771
1.215 (drh 29-Dec-05): len = sqlite3VdbeSerialTypeLen(serial_type);
1772
1.215 (drh 29-Dec-05): memcpy(buf, pMem->z, len);
1773
1.215 (drh 29-Dec-05): return len;
1774
1.215 (drh 29-Dec-05): }
1775
1.215 (drh 29-Dec-05):
1776
1.215 (drh 29-Dec-05): /* NULL or constants 0 or 1 */
1777
1.215 (drh 29-Dec-05): return 0;
1778
1.22 (danielk1 10-May-04): }
1779
1.22 (danielk1 10-May-04):
1780
1.22 (danielk1 10-May-04): /*
1781
1.28 (danielk1 12-May-04): ** Deserialize the data blob pointed to by buf as serial type serial_type
1782
1.28 (danielk1 12-May-04): ** and store the result in pMem. Return the number of bytes read.
1783
1.28 (danielk1 12-May-04): */
1784
1.55 (danielk1 22-May-04): int sqlite3VdbeSerialGet(
1785
1.58 (danielk1 23-May-04): const unsigned char *buf, /* Buffer to deserialize from */
1786
1.75 (drh 28-May-04): u32 serial_type, /* Serial type to deserialize */
1787
1.75 (drh 28-May-04): Mem *pMem /* Memory cell to write value into */
1788
1.55 (danielk1 22-May-04): ){
1789
1.177 (drh 21-May-05): switch( serial_type ){
1790
1.177 (drh 21-May-05): case 10: /* Reserved for future use */
1791
1.177 (drh 21-May-05): case 11: /* Reserved for future use */
1792
1.177 (drh 21-May-05): case 0: { /* NULL */
1793
1.177 (drh 21-May-05): pMem->flags = MEM_Null;
1794
1.177 (drh 21-May-05): break;
1795
1.177 (drh 21-May-05): }
1796
1.177 (drh 21-May-05): case 1: { /* 1-byte signed integer */
1797
1.177 (drh 21-May-05): pMem->i = (signed char)buf[0];
1798
1.177 (drh 21-May-05): pMem->flags = MEM_Int;
1799
1.177 (drh 21-May-05): return 1;
1800
1.177 (drh 21-May-05): }
1801
1.177 (drh 21-May-05): case 2: { /* 2-byte signed integer */
1802
1.177 (drh 21-May-05): pMem->i = (((signed char)buf[0])<<8) | buf[1];
1803
1.177 (drh 21-May-05): pMem->flags = MEM_Int;
1804
1.177 (drh 21-May-05): return 2;
1805
1.177 (drh 21-May-05): }
1806
1.177 (drh 21-May-05): case 3: { /* 3-byte signed integer */
1807
1.177 (drh 21-May-05): pMem->i = (((signed char)buf[0])<<16) | (buf[1]<<8) | buf[2];
1808
1.177 (drh 21-May-05): pMem->flags = MEM_Int;
1809
1.177 (drh 21-May-05): return 3;
1810
1.177 (drh 21-May-05): }
1811
1.177 (drh 21-May-05): case 4: { /* 4-byte signed integer */
1812
1.177 (drh 21-May-05): pMem->i = (buf[0]<<24) | (buf[1]<<16) | (buf[2]<<8) | buf[3];
1813
1.177 (drh 21-May-05): pMem->flags = MEM_Int;
1814
1.177 (drh 21-May-05): return 4;
1815
1.177 (drh 21-May-05): }
1816
1.177 (drh 21-May-05): case 5: { /* 6-byte signed integer */
1817
1.177 (drh 21-May-05): u64 x = (((signed char)buf[0])<<8) | buf[1];
1818
1.177 (drh 21-May-05): u32 y = (buf[2]<<24) | (buf[3]<<16) | (buf[4]<<8) | buf[5];
1819
1.177 (drh 21-May-05): x = (x<<32) | y;
1820
1.177 (drh 21-May-05): pMem->i = *(i64*)&x;
1821
1.83 (drh 30-May-04): pMem->flags = MEM_Int;
1822
1.177 (drh 21-May-05): return 6;
1823
1.177 (drh 21-May-05): }
1824
1.186 (drh 18-Aug-05): case 6: /* 8-byte signed integer */
1825
1.177 (drh 21-May-05): case 7: { /* IEEE floating point */
1826
1.193 (drh 05-Sep-05): u64 x;
1827
1.193 (drh 05-Sep-05): u32 y;
1828
1.232 (drh 23-Jan-06): #if !defined(NDEBUG) && !defined(SQLITE_OMIT_FLOATING_POINT)
1829
1.189 (drh 28-Aug-05): /* Verify that integers and floating point values use the same
1830
1.190 (drh 28-Aug-05): ** byte order. The byte order differs on some (broken) architectures.
1831
1.190 (drh 28-Aug-05): */
1832
1.189 (drh 28-Aug-05): static const u64 t1 = ((u64)0x3ff00000)<<32;
1833
1.189 (drh 28-Aug-05): assert( 1.0==*(double*)&t1 );
1834
1.189 (drh 28-Aug-05): #endif
1835
1.190 (drh 28-Aug-05):
1836
1.193 (drh 05-Sep-05): x = (buf[0]<<24) | (buf[1]<<16) | (buf[2]<<8) | buf[3];
1837
1.193 (drh 05-Sep-05): y = (buf[4]<<24) | (buf[5]<<16) | (buf[6]<<8) | buf[7];
1838
1.177 (drh 21-May-05): x = (x<<32) | y;
1839
1.177 (drh 21-May-05): if( serial_type==6 ){
1840
1.177 (drh 21-May-05): pMem->i = *(i64*)&x;
1841
1.177 (drh 21-May-05): pMem->flags = MEM_Int;
1842
1.177 (drh 21-May-05): }else{
1843
1.177 (drh 21-May-05): pMem->r = *(double*)&x;
1844
1.177 (drh 21-May-05): pMem->flags = MEM_Real;
1845
1.83 (drh 30-May-04): }
1846
1.177 (drh 21-May-05): return 8;
1847
1.177 (drh 21-May-05): }
1848
1.215 (drh 29-Dec-05): case 8: /* Integer 0 */
1849
1.215 (drh 29-Dec-05): case 9: { /* Integer 1 */
1850
1.215 (drh 29-Dec-05): pMem->i = serial_type-8;
1851
1.215 (drh 29-Dec-05): pMem->flags = MEM_Int;
1852
1.215 (drh 29-Dec-05): return 0;
1853
1.215 (drh 29-Dec-05): }
1854
1.177 (drh 21-May-05): default: {
1855
1.177 (drh 21-May-05): int len = (serial_type-12)/2;
1856
1.177 (drh 21-May-05): pMem->z = (char *)buf;
1857
1.177 (drh 21-May-05): pMem->n = len;
1858
1.177 (drh 21-May-05): pMem->xDel = 0;
1859
1.177 (drh 21-May-05): if( serial_type&0x01 ){
1860
1.177 (drh 21-May-05): pMem->flags = MEM_Str | MEM_Ephem;
1861
1.83 (drh 30-May-04): }else{
1862
1.177 (drh 21-May-05): pMem->flags = MEM_Blob | MEM_Ephem;
1863
1.83 (drh 30-May-04): }
1864
1.177 (drh 21-May-05): return len;
1865
1.81 (drh 30-May-04): }
1866
1.23 (danielk1 10-May-04): }
1867
1.177 (drh 21-May-05): return 0;
1868
1.22 (danielk1 10-May-04): }
1869
1.22 (danielk1 10-May-04):
1870
1.22 (danielk1 10-May-04): /*
1871
1.223 (drh 12-Jan-06): ** The header of a record consists of a sequence variable-length integers.
1872
1.223 (drh 12-Jan-06): ** These integers are almost always small and are encoded as a single byte.
1873
1.223 (drh 12-Jan-06): ** The following macro takes advantage this fact to provide a fast decode
1874
1.223 (drh 12-Jan-06): ** of the integers in a record header. It is faster for the common case
1875
1.223 (drh 12-Jan-06): ** where the integer is a single byte. It is a little slower when the
1876
1.223 (drh 12-Jan-06): ** integer is two or more bytes. But overall it is faster.
1877
1.223 (drh 12-Jan-06): **
1878
1.223 (drh 12-Jan-06): ** The following expressions are equivalent:
1879
1.223 (drh 12-Jan-06): **
1880
1.223 (drh 12-Jan-06): ** x = sqlite3GetVarint32( A, &B );
1881
1.223 (drh 12-Jan-06): **
1882
1.223 (drh 12-Jan-06): ** x = GetVarint( A, B );
1883
1.223 (drh 12-Jan-06): **
1884
1.223 (drh 12-Jan-06): */
1885
1.223 (drh 12-Jan-06): #define GetVarint(A,B) ((B = *(A))<=0x7f ? 1 : sqlite3GetVarint32(A, &B))
1886
1.223 (drh 12-Jan-06):
1887
1.223 (drh 12-Jan-06): /*
1888
1.90 (drh 02-Jun-04): ** This function compares the two table rows or index records specified by
1889
1.38 (danielk1 18-May-04): ** {nKey1, pKey1} and {nKey2, pKey2}, returning a negative, zero
1890
1.38 (danielk1 18-May-04): ** or positive integer if {nKey1, pKey1} is less than, equal to or
1891
1.90 (drh 02-Jun-04): ** greater than {nKey2, pKey2}. Both Key1 and Key2 must be byte strings
1892
1.90 (drh 02-Jun-04): ** composed by the OP_MakeRecord opcode of the VDBE.
1893
1.38 (danielk1 18-May-04): */
1894
1.90 (drh 02-Jun-04): int sqlite3VdbeRecordCompare(
1895
1.38 (danielk1 18-May-04): void *userData,
1896
1.38 (danielk1 18-May-04): int nKey1, const void *pKey1,
1897
1.38 (danielk1 18-May-04): int nKey2, const void *pKey2
1898
1.38 (danielk1 18-May-04): ){
1899
1.50 (drh 20-May-04): KeyInfo *pKeyInfo = (KeyInfo*)userData;
1900
1.73 (drh 27-May-04): u32 d1, d2; /* Offset into aKey[] of next data element */
1901
1.73 (drh 27-May-04): u32 idx1, idx2; /* Offset into aKey[] of next header element */
1902
1.73 (drh 27-May-04): u32 szHdr1, szHdr2; /* Number of bytes in header */
1903
1.73 (drh 27-May-04): int i = 0;
1904
1.73 (drh 27-May-04): int nField;
1905
1.73 (drh 27-May-04): int rc = 0;
1906
1.38 (danielk1 18-May-04): const unsigned char *aKey1 = (const unsigned char *)pKey1;
1907
1.38 (danielk1 18-May-04): const unsigned char *aKey2 = (const unsigned char *)pKey2;
1908
1.96 (danielk1 09-Jun-04):
1909
1.96 (danielk1 09-Jun-04): Mem mem1;
1910
1.96 (danielk1 09-Jun-04): Mem mem2;
1911
1.96 (danielk1 09-Jun-04): mem1.enc = pKeyInfo->enc;
1912
1.96 (danielk1 09-Jun-04): mem2.enc = pKeyInfo->enc;
1913
1.73 (drh 27-May-04):
1914
1.223 (drh 12-Jan-06): idx1 = GetVarint(aKey1, szHdr1);
1915
1.73 (drh 27-May-04): d1 = szHdr1;
1916
1.223 (drh 12-Jan-06): idx2 = GetVarint(aKey2, szHdr2);
1917
1.73 (drh 27-May-04): d2 = szHdr2;
1918
1.73 (drh 27-May-04): nField = pKeyInfo->nField;
1919
1.76 (drh 28-May-04): while( idx1<szHdr1 && idx2<szHdr2 ){
1920
1.73 (drh 27-May-04): u32 serial_type1;
1921
1.73 (drh 27-May-04): u32 serial_type2;
1922
1.39 (danielk1 18-May-04):
1923
1.39 (danielk1 18-May-04): /* Read the serial types for the next element in each key. */
1924
1.223 (drh 12-Jan-06): idx1 += GetVarint( aKey1+idx1, serial_type1 );
1925
1.76 (drh 28-May-04): if( d1>=nKey1 && sqlite3VdbeSerialTypeLen(serial_type1)>0 ) break;
1926
1.223 (drh 12-Jan-06): idx2 += GetVarint( aKey2+idx2, serial_type2 );
1927
1.76 (drh 28-May-04): if( d2>=nKey2 && sqlite3VdbeSerialTypeLen(serial_type2)>0 ) break;
1928
1.39 (danielk1 18-May-04):
1929
1.267 (drh 27-Oct-06): /* Extract the values to be compared.
1930
1.39 (danielk1 18-May-04): */
1931
1.75 (drh 28-May-04): d1 += sqlite3VdbeSerialGet(&aKey1[d1], serial_type1, &mem1);
1932
1.75 (drh 28-May-04): d2 += sqlite3VdbeSerialGet(&aKey2[d2], serial_type2, &mem2);
1933
1.39 (danielk1 18-May-04):
1934
1.267 (drh 27-Oct-06): /* Do the comparison
1935
1.267 (drh 27-Oct-06): */
1936
1.76 (drh 28-May-04): rc = sqlite3MemCompare(&mem1, &mem2, i<nField ? pKeyInfo->aColl[i] : 0);
1937
1.177 (drh 21-May-05): if( mem1.flags & MEM_Dyn ) sqlite3VdbeMemRelease(&mem1);
1938
1.177 (drh 21-May-05): if( mem2.flags & MEM_Dyn ) sqlite3VdbeMemRelease(&mem2);
1939
1.39 (danielk1 18-May-04): if( rc!=0 ){
1940
1.73 (drh 27-May-04): break;
1941
1.39 (danielk1 18-May-04): }
1942
1.73 (drh 27-May-04): i++;
1943
1.73 (drh 27-May-04): }
1944
1.73 (drh 27-May-04):
1945
1.73 (drh 27-May-04): /* One of the keys ran out of fields, but all the fields up to that point
1946
1.73 (drh 27-May-04): ** were equal. If the incrKey flag is true, then the second key is
1947
1.73 (drh 27-May-04): ** treated as larger.
1948
1.73 (drh 27-May-04): */
1949
1.73 (drh 27-May-04): if( rc==0 ){
1950
1.73 (drh 27-May-04): if( pKeyInfo->incrKey ){
1951
1.73 (drh 27-May-04): rc = -1;
1952
1.73 (drh 27-May-04): }else if( d1<nKey1 ){
1953
1.73 (drh 27-May-04): rc = 1;
1954
1.73 (drh 27-May-04): }else if( d2<nKey2 ){
1955
1.73 (drh 27-May-04): rc = -1;
1956
1.73 (drh 27-May-04): }
1957
1.214 (drh 21-Dec-05): }else if( pKeyInfo->aSortOrder && i<pKeyInfo->nField
1958
1.214 (drh 21-Dec-05): && pKeyInfo->aSortOrder[i] ){
1959
1.73 (drh 27-May-04): rc = -rc;
1960
1.39 (danielk1 18-May-04): }
1961
1.39 (danielk1 18-May-04):
1962
1.73 (drh 27-May-04): return rc;
1963
1.38 (danielk1 18-May-04): }
1964
1.76 (drh 28-May-04):
1965
1.76 (drh 28-May-04): /*
1966
1.90 (drh 02-Jun-04): ** The argument is an index entry composed using the OP_MakeRecord opcode.
1967
1.90 (drh 02-Jun-04): ** The last entry in this record should be an integer (specifically
1968
1.90 (drh 02-Jun-04): ** an integer rowid). This routine returns the number of bytes in
1969
1.90 (drh 02-Jun-04): ** that integer.
1970
1.76 (drh 28-May-04): */
1971
1.237 (drh 24-Feb-06): int sqlite3VdbeIdxRowidLen(const u8 *aKey){
1972
1.76 (drh 28-May-04): u32 szHdr; /* Size of the header */
1973
1.76 (drh 28-May-04): u32 typeRowid; /* Serial type of the rowid */
1974
1.76 (drh 28-May-04):
1975
1.76 (drh 28-May-04): sqlite3GetVarint32(aKey, &szHdr);
1976
1.76 (drh 28-May-04): sqlite3GetVarint32(&aKey[szHdr-1], &typeRowid);
1977
1.76 (drh 28-May-04): return sqlite3VdbeSerialTypeLen(typeRowid);
1978
1.76 (drh 28-May-04): }
1979
1.38 (danielk1 18-May-04):
1980
1.30 (danielk1 13-May-04):
1981
1.30 (danielk1 13-May-04): /*
1982
1.90 (drh 02-Jun-04): ** pCur points at an index entry created using the OP_MakeRecord opcode.
1983
1.90 (drh 02-Jun-04): ** Read the rowid (the last field in the record) and store it in *rowid.
1984
1.90 (drh 02-Jun-04): ** Return SQLITE_OK if everything works, or an error code otherwise.
1985
1.30 (danielk1 13-May-04): */
1986
1.30 (danielk1 13-May-04): int sqlite3VdbeIdxRowid(BtCursor *pCur, i64 *rowid){
1987
1.132 (danielk1 28-Jun-04): i64 nCellKey;
1988
1.30 (danielk1 13-May-04): int rc;
1989
1.76 (drh 28-May-04): u32 szHdr; /* Size of the header */
1990
1.76 (drh 28-May-04): u32 typeRowid; /* Serial type of the rowid */
1991
1.76 (drh 28-May-04): u32 lenRowid; /* Size of the rowid */
1992
1.76 (drh 28-May-04): Mem m, v;
1993
1.30 (danielk1 13-May-04):
1994
1.76 (drh 28-May-04): sqlite3BtreeKeySize(pCur, &nCellKey);
1995
1.76 (drh 28-May-04): if( nCellKey<=0 ){
1996
1.200 (drh 17-Sep-05): return SQLITE_CORRUPT_BKPT;
1997
1.76 (drh 28-May-04): }
1998
1.76 (drh 28-May-04): rc = sqlite3VdbeMemFromBtree(pCur, 0, nCellKey, 1, &m);
1999
1.76 (drh 28-May-04): if( rc ){
2000
1.30 (danielk1 13-May-04): return rc;
2001
1.30 (danielk1 13-May-04): }
2002
1.210 (drh 09-Dec-05): sqlite3GetVarint32((u8*)m.z, &szHdr);
2003
1.210 (drh 09-Dec-05): sqlite3GetVarint32((u8*)&m.z[szHdr-1], &typeRowid);
2004
1.76 (drh 28-May-04): lenRowid = sqlite3VdbeSerialTypeLen(typeRowid);
2005
1.210 (drh 09-Dec-05): sqlite3VdbeSerialGet((u8*)&m.z[m.n-lenRowid], typeRowid, &v);
2006
1.76 (drh 28-May-04): *rowid = v.i;
2007
1.105 (danielk1 12-Jun-04): sqlite3VdbeMemRelease(&m);
2008
1.30 (danielk1 13-May-04): return SQLITE_OK;
2009
1.30 (danielk1 13-May-04): }
2010
1.30 (danielk1 13-May-04):
2011
1.44 (drh 19-May-04): /*
2012
1.50 (drh 20-May-04): ** Compare the key of the index entry that cursor pC is point to against
2013
1.44 (drh 19-May-04): ** the key string in pKey (of length nKey). Write into *pRes a number
2014
1.44 (drh 19-May-04): ** that is negative, zero, or positive if pC is less than, equal to,
2015
1.44 (drh 19-May-04): ** or greater than pKey. Return SQLITE_OK on success.
2016
1.50 (drh 20-May-04): **
2017
1.76 (drh 28-May-04): ** pKey is either created without a rowid or is truncated so that it
2018
1.76 (drh 28-May-04): ** omits the rowid at the end. The rowid at the end of the index entry
2019
1.76 (drh 28-May-04): ** is ignored as well.
2020
1.44 (drh 19-May-04): */
2021
1.30 (danielk1 13-May-04): int sqlite3VdbeIdxKeyCompare(
2022
1.44 (drh 19-May-04): Cursor *pC, /* The cursor to compare against */
2023
1.44 (drh 19-May-04): int nKey, const u8 *pKey, /* The key to compare */
2024
1.44 (drh 19-May-04): int *res /* Write the comparison result here */
2025
1.30 (danielk1 13-May-04): ){
2026
1.132 (danielk1 28-Jun-04): i64 nCellKey;
2027
1.30 (danielk1 13-May-04): int rc;
2028
1.31 (danielk1 14-May-04): BtCursor *pCur = pC->pCursor;
2029
1.76 (drh 28-May-04): int lenRowid;
2030
1.76 (drh 28-May-04): Mem m;
2031
1.30 (danielk1 13-May-04):
2032
1.30 (danielk1 13-May-04): sqlite3BtreeKeySize(pCur, &nCellKey);
2033
1.30 (danielk1 13-May-04): if( nCellKey<=0 ){
2034
1.30 (danielk1 13-May-04): *res = 0;
2035
1.30 (danielk1 13-May-04): return SQLITE_OK;
2036
1.30 (danielk1 13-May-04): }
2037
1.76 (drh 28-May-04): rc = sqlite3VdbeMemFromBtree(pC->pCursor, 0, nCellKey, 1, &m);
2038
1.76 (drh 28-May-04): if( rc ){
2039
1.76 (drh 28-May-04): return rc;
2040
1.30 (danielk1 13-May-04): }
2041
1.237 (drh 24-Feb-06): lenRowid = sqlite3VdbeIdxRowidLen((u8*)m.z);
2042
1.90 (drh 02-Jun-04): *res = sqlite3VdbeRecordCompare(pC->pKeyInfo, m.n-lenRowid, m.z, nKey, pKey);
2043
1.105 (danielk1 12-Jun-04): sqlite3VdbeMemRelease(&m);
2044
1.30 (danielk1 13-May-04): return SQLITE_OK;
2045
1.62 (danielk1 25-May-04): }
2046
1.121 (danielk1 21-Jun-04):
2047
1.121 (danielk1 21-Jun-04): /*
2048
1.121 (danielk1 21-Jun-04): ** This routine sets the value to be returned by subsequent calls to
2049
1.121 (danielk1 21-Jun-04): ** sqlite3_changes() on the database handle 'db'.
2050
1.121 (danielk1 21-Jun-04): */
2051
1.121 (danielk1 21-Jun-04): void sqlite3VdbeSetChanges(sqlite3 *db, int nChange){
2052
1.121 (danielk1 21-Jun-04): db->nChange = nChange;
2053
1.121 (danielk1 21-Jun-04): db->nTotalChange += nChange;
2054
1.121 (danielk1 21-Jun-04): }
2055
1.121 (danielk1 21-Jun-04):
2056
1.121 (danielk1 21-Jun-04): /*
2057
1.121 (danielk1 21-Jun-04): ** Set a flag in the vdbe to update the change counter when it is finalised
2058
1.121 (danielk1 21-Jun-04): ** or reset.
2059
1.121 (danielk1 21-Jun-04): */
2060
1.152 (drh 05-Nov-04): void sqlite3VdbeCountChanges(Vdbe *v){
2061
1.152 (drh 05-Nov-04): v->changeCntOn = 1;
2062
1.121 (danielk1 21-Jun-04): }
2063
1.159 (drh 22-Jan-05):
2064
1.159 (drh 22-Jan-05): /*
2065
1.159 (drh 22-Jan-05): ** Mark every prepared statement associated with a database connection
2066
1.159 (drh 22-Jan-05): ** as expired.
2067
1.159 (drh 22-Jan-05): **
2068
1.159 (drh 22-Jan-05): ** An expired statement means that recompilation of the statement is
2069
1.159 (drh 22-Jan-05): ** recommend. Statements expire when things happen that make their
2070
1.159 (drh 22-Jan-05): ** programs obsolete. Removing user-defined functions or collating
2071
1.159 (drh 22-Jan-05): ** sequences, or changing an authorization function are the types of
2072
1.159 (drh 22-Jan-05): ** things that make prepared statements obsolete.
2073
1.159 (drh 22-Jan-05): */
2074
1.159 (drh 22-Jan-05): void sqlite3ExpirePreparedStatements(sqlite3 *db){
2075
1.159 (drh 22-Jan-05): Vdbe *p;
2076
1.159 (drh 22-Jan-05): for(p = db->pVdbe; p; p=p->pNext){
2077
1.159 (drh 22-Jan-05): p->expired = 1;
2078
1.159 (drh 22-Jan-05): }
2079
1.159 (drh 22-Jan-05): }
2080
1.167 (danielk1 09-Mar-05):
2081
1.167 (danielk1 09-Mar-05): /*
2082
1.167 (danielk1 09-Mar-05): ** Return the database associated with the Vdbe.
2083
1.167 (danielk1 09-Mar-05): */
2084
1.167 (danielk1 09-Mar-05): sqlite3 *sqlite3VdbeDb(Vdbe *v){
2085
1.167 (danielk1 09-Mar-05): return v->db;
2086
1.167 (danielk1 09-Mar-05): }