line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
/* |
2
|
|
|
|
|
|
|
** 2003 September 6 |
3
|
|
|
|
|
|
|
** |
4
|
|
|
|
|
|
|
** The author disclaims copyright to this source code. In place of |
5
|
|
|
|
|
|
|
** a legal notice, here is a blessing: |
6
|
|
|
|
|
|
|
** |
7
|
|
|
|
|
|
|
** May you do good and not evil. |
8
|
|
|
|
|
|
|
** May you find forgiveness for yourself and forgive others. |
9
|
|
|
|
|
|
|
** May you share freely, never taking more than you give. |
10
|
|
|
|
|
|
|
** |
11
|
|
|
|
|
|
|
************************************************************************* |
12
|
|
|
|
|
|
|
** This file contains code used for creating, destroying, and populating |
13
|
|
|
|
|
|
|
** a VDBE (or an "sqlite_vm" as it is known to the outside world.) Prior |
14
|
|
|
|
|
|
|
** to version 2.8.7, all this code was combined into the vdbe.c source file. |
15
|
|
|
|
|
|
|
** But that file was getting too big so this subroutines were split out. |
16
|
|
|
|
|
|
|
*/ |
17
|
|
|
|
|
|
|
#include "sqliteInt.h" |
18
|
|
|
|
|
|
|
#include "os.h" |
19
|
|
|
|
|
|
|
#include |
20
|
|
|
|
|
|
|
#include "vdbeInt.h" |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
/* |
24
|
|
|
|
|
|
|
** When debugging the code generator in a symbolic debugger, one can |
25
|
|
|
|
|
|
|
** set the sqlite_vdbe_addop_trace to 1 and all opcodes will be printed |
26
|
|
|
|
|
|
|
** as they are added to the instruction stream. |
27
|
|
|
|
|
|
|
*/ |
28
|
|
|
|
|
|
|
#ifndef NDEBUG |
29
|
|
|
|
|
|
|
int sqlite_vdbe_addop_trace = 0; |
30
|
|
|
|
|
|
|
#endif |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
/* |
34
|
|
|
|
|
|
|
** Create a new virtual database engine. |
35
|
|
|
|
|
|
|
*/ |
36
|
348
|
|
|
|
|
|
Vdbe *sqliteVdbeCreate(sqlite *db){ |
37
|
|
|
|
|
|
|
Vdbe *p; |
38
|
348
|
|
|
|
|
|
p = sqliteMalloc( sizeof(Vdbe) ); |
39
|
348
|
50
|
|
|
|
|
if( p==0 ) return 0; |
40
|
348
|
|
|
|
|
|
p->db = db; |
41
|
348
|
100
|
|
|
|
|
if( db->pVdbe ){ |
42
|
19
|
|
|
|
|
|
db->pVdbe->pPrev = p; |
43
|
|
|
|
|
|
|
} |
44
|
348
|
|
|
|
|
|
p->pNext = db->pVdbe; |
45
|
348
|
|
|
|
|
|
p->pPrev = 0; |
46
|
348
|
|
|
|
|
|
db->pVdbe = p; |
47
|
348
|
|
|
|
|
|
p->magic = VDBE_MAGIC_INIT; |
48
|
348
|
|
|
|
|
|
return p; |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
/* |
52
|
|
|
|
|
|
|
** Turn tracing on or off |
53
|
|
|
|
|
|
|
*/ |
54
|
342
|
|
|
|
|
|
void sqliteVdbeTrace(Vdbe *p, FILE *trace){ |
55
|
342
|
|
|
|
|
|
p->trace = trace; |
56
|
342
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
/* |
59
|
|
|
|
|
|
|
** Add a new instruction to the list of instructions current in the |
60
|
|
|
|
|
|
|
** VDBE. Return the address of the new instruction. |
61
|
|
|
|
|
|
|
** |
62
|
|
|
|
|
|
|
** Parameters: |
63
|
|
|
|
|
|
|
** |
64
|
|
|
|
|
|
|
** p Pointer to the VDBE |
65
|
|
|
|
|
|
|
** |
66
|
|
|
|
|
|
|
** op The opcode for this instruction |
67
|
|
|
|
|
|
|
** |
68
|
|
|
|
|
|
|
** p1, p2 First two of the three possible operands. |
69
|
|
|
|
|
|
|
** |
70
|
|
|
|
|
|
|
** Use the sqliteVdbeResolveLabel() function to fix an address and |
71
|
|
|
|
|
|
|
** the sqliteVdbeChangeP3() function to change the value of the P3 |
72
|
|
|
|
|
|
|
** operand. |
73
|
|
|
|
|
|
|
*/ |
74
|
4218
|
|
|
|
|
|
int sqliteVdbeAddOp(Vdbe *p, int op, int p1, int p2){ |
75
|
|
|
|
|
|
|
int i; |
76
|
|
|
|
|
|
|
VdbeOp *pOp; |
77
|
|
|
|
|
|
|
|
78
|
4218
|
|
|
|
|
|
i = p->nOp; |
79
|
4218
|
|
|
|
|
|
p->nOp++; |
80
|
|
|
|
|
|
|
assert( p->magic==VDBE_MAGIC_INIT ); |
81
|
4218
|
100
|
|
|
|
|
if( i>=p->nOpAlloc ){ |
82
|
348
|
|
|
|
|
|
int oldSize = p->nOpAlloc; |
83
|
|
|
|
|
|
|
Op *aNew; |
84
|
348
|
|
|
|
|
|
p->nOpAlloc = p->nOpAlloc*2 + 100; |
85
|
348
|
|
|
|
|
|
aNew = sqliteRealloc(p->aOp, p->nOpAlloc*sizeof(Op)); |
86
|
348
|
50
|
|
|
|
|
if( aNew==0 ){ |
87
|
0
|
|
|
|
|
|
p->nOpAlloc = oldSize; |
88
|
0
|
|
|
|
|
|
return 0; |
89
|
|
|
|
|
|
|
} |
90
|
348
|
|
|
|
|
|
p->aOp = aNew; |
91
|
348
|
|
|
|
|
|
memset(&p->aOp[oldSize], 0, (p->nOpAlloc-oldSize)*sizeof(Op)); |
92
|
|
|
|
|
|
|
} |
93
|
4218
|
|
|
|
|
|
pOp = &p->aOp[i]; |
94
|
4218
|
|
|
|
|
|
pOp->opcode = op; |
95
|
4218
|
|
|
|
|
|
pOp->p1 = p1; |
96
|
4218
|
100
|
|
|
|
|
if( p2<0 && (-1-p2)nLabel && p->aLabel[-1-p2]>=0 ){ |
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
97
|
0
|
|
|
|
|
|
p2 = p->aLabel[-1-p2]; |
98
|
|
|
|
|
|
|
} |
99
|
4218
|
|
|
|
|
|
pOp->p2 = p2; |
100
|
4218
|
|
|
|
|
|
pOp->p3 = 0; |
101
|
4218
|
|
|
|
|
|
pOp->p3type = P3_NOTUSED; |
102
|
|
|
|
|
|
|
#ifndef NDEBUG |
103
|
|
|
|
|
|
|
if( sqlite_vdbe_addop_trace ) sqliteVdbePrintOp(0, i, &p->aOp[i]); |
104
|
|
|
|
|
|
|
#endif |
105
|
4218
|
|
|
|
|
|
return i; |
106
|
|
|
|
|
|
|
} |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
/* |
109
|
|
|
|
|
|
|
** Add an opcode that includes the p3 value. |
110
|
|
|
|
|
|
|
*/ |
111
|
1141
|
|
|
|
|
|
int sqliteVdbeOp3(Vdbe *p, int op, int p1, int p2, const char *zP3, int p3type){ |
112
|
1141
|
|
|
|
|
|
int addr = sqliteVdbeAddOp(p, op, p1, p2); |
113
|
1141
|
|
|
|
|
|
sqliteVdbeChangeP3(p, addr, zP3, p3type); |
114
|
1141
|
|
|
|
|
|
return addr; |
115
|
|
|
|
|
|
|
} |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
/* |
118
|
|
|
|
|
|
|
** Add multiple opcodes. The list is terminated by an opcode of 0. |
119
|
|
|
|
|
|
|
*/ |
120
|
1
|
|
|
|
|
|
int sqliteVdbeCode(Vdbe *p, ...){ |
121
|
|
|
|
|
|
|
int addr; |
122
|
|
|
|
|
|
|
va_list ap; |
123
|
|
|
|
|
|
|
int opcode, p1, p2; |
124
|
1
|
|
|
|
|
|
va_start(ap, p); |
125
|
1
|
|
|
|
|
|
addr = p->nOp; |
126
|
4
|
100
|
|
|
|
|
while( (opcode = va_arg(ap,int))!=0 ){ |
|
|
100
|
|
|
|
|
|
127
|
3
|
100
|
|
|
|
|
p1 = va_arg(ap,int); |
128
|
3
|
100
|
|
|
|
|
p2 = va_arg(ap,int); |
129
|
3
|
|
|
|
|
|
sqliteVdbeAddOp(p, opcode, p1, p2); |
130
|
|
|
|
|
|
|
} |
131
|
1
|
|
|
|
|
|
va_end(ap); |
132
|
1
|
|
|
|
|
|
return addr; |
133
|
|
|
|
|
|
|
} |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
/* |
138
|
|
|
|
|
|
|
** Create a new symbolic label for an instruction that has yet to be |
139
|
|
|
|
|
|
|
** coded. The symbolic label is really just a negative number. The |
140
|
|
|
|
|
|
|
** label can be used as the P2 value of an operation. Later, when |
141
|
|
|
|
|
|
|
** the label is resolved to a specific address, the VDBE will scan |
142
|
|
|
|
|
|
|
** through its operation list and change all values of P2 which match |
143
|
|
|
|
|
|
|
** the label into the resolved address. |
144
|
|
|
|
|
|
|
** |
145
|
|
|
|
|
|
|
** The VDBE knows that a P2 value is a label because labels are |
146
|
|
|
|
|
|
|
** always negative and P2 values are suppose to be non-negative. |
147
|
|
|
|
|
|
|
** Hence, a negative P2 value is a label that has yet to be resolved. |
148
|
|
|
|
|
|
|
*/ |
149
|
457
|
|
|
|
|
|
int sqliteVdbeMakeLabel(Vdbe *p){ |
150
|
|
|
|
|
|
|
int i; |
151
|
457
|
|
|
|
|
|
i = p->nLabel++; |
152
|
|
|
|
|
|
|
assert( p->magic==VDBE_MAGIC_INIT ); |
153
|
457
|
100
|
|
|
|
|
if( i>=p->nLabelAlloc ){ |
154
|
|
|
|
|
|
|
int *aNew; |
155
|
177
|
|
|
|
|
|
p->nLabelAlloc = p->nLabelAlloc*2 + 10; |
156
|
177
|
|
|
|
|
|
aNew = sqliteRealloc( p->aLabel, p->nLabelAlloc*sizeof(p->aLabel[0])); |
157
|
177
|
50
|
|
|
|
|
if( aNew==0 ){ |
158
|
0
|
|
|
|
|
|
sqliteFree(p->aLabel); |
159
|
|
|
|
|
|
|
} |
160
|
177
|
|
|
|
|
|
p->aLabel = aNew; |
161
|
|
|
|
|
|
|
} |
162
|
457
|
50
|
|
|
|
|
if( p->aLabel==0 ){ |
163
|
0
|
|
|
|
|
|
p->nLabel = 0; |
164
|
0
|
|
|
|
|
|
p->nLabelAlloc = 0; |
165
|
0
|
|
|
|
|
|
return 0; |
166
|
|
|
|
|
|
|
} |
167
|
457
|
|
|
|
|
|
p->aLabel[i] = -1; |
168
|
457
|
|
|
|
|
|
return -1-i; |
169
|
|
|
|
|
|
|
} |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
/* |
172
|
|
|
|
|
|
|
** Resolve label "x" to be the address of the next instruction to |
173
|
|
|
|
|
|
|
** be inserted. The parameter "x" must have been obtained from |
174
|
|
|
|
|
|
|
** a prior call to sqliteVdbeMakeLabel(). |
175
|
|
|
|
|
|
|
*/ |
176
|
457
|
|
|
|
|
|
void sqliteVdbeResolveLabel(Vdbe *p, int x){ |
177
|
|
|
|
|
|
|
int j; |
178
|
|
|
|
|
|
|
assert( p->magic==VDBE_MAGIC_INIT ); |
179
|
457
|
50
|
|
|
|
|
if( x<0 && (-x)<=p->nLabel && p->aOp ){ |
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
180
|
457
|
50
|
|
|
|
|
if( p->aLabel[-1-x]==p->nOp ) return; |
181
|
|
|
|
|
|
|
assert( p->aLabel[-1-x]<0 ); |
182
|
457
|
|
|
|
|
|
p->aLabel[-1-x] = p->nOp; |
183
|
9531
|
100
|
|
|
|
|
for(j=0; jnOp; j++){ |
184
|
9074
|
100
|
|
|
|
|
if( p->aOp[j].p2==x ) p->aOp[j].p2 = p->nOp; |
185
|
|
|
|
|
|
|
} |
186
|
|
|
|
|
|
|
} |
187
|
|
|
|
|
|
|
} |
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
/* |
190
|
|
|
|
|
|
|
** Return the address of the next instruction to be inserted. |
191
|
|
|
|
|
|
|
*/ |
192
|
161
|
|
|
|
|
|
int sqliteVdbeCurrentAddr(Vdbe *p){ |
193
|
|
|
|
|
|
|
assert( p->magic==VDBE_MAGIC_INIT ); |
194
|
161
|
|
|
|
|
|
return p->nOp; |
195
|
|
|
|
|
|
|
} |
196
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
/* |
198
|
|
|
|
|
|
|
** Add a whole list of operations to the operation stack. Return the |
199
|
|
|
|
|
|
|
** address of the first operation added. |
200
|
|
|
|
|
|
|
*/ |
201
|
22
|
|
|
|
|
|
int sqliteVdbeAddOpList(Vdbe *p, int nOp, VdbeOpList const *aOp){ |
202
|
|
|
|
|
|
|
int addr; |
203
|
|
|
|
|
|
|
assert( p->magic==VDBE_MAGIC_INIT ); |
204
|
22
|
50
|
|
|
|
|
if( p->nOp + nOp >= p->nOpAlloc ){ |
205
|
0
|
|
|
|
|
|
int oldSize = p->nOpAlloc; |
206
|
|
|
|
|
|
|
Op *aNew; |
207
|
0
|
|
|
|
|
|
p->nOpAlloc = p->nOpAlloc*2 + nOp + 10; |
208
|
0
|
|
|
|
|
|
aNew = sqliteRealloc(p->aOp, p->nOpAlloc*sizeof(Op)); |
209
|
0
|
0
|
|
|
|
|
if( aNew==0 ){ |
210
|
0
|
|
|
|
|
|
p->nOpAlloc = oldSize; |
211
|
0
|
|
|
|
|
|
return 0; |
212
|
|
|
|
|
|
|
} |
213
|
0
|
|
|
|
|
|
p->aOp = aNew; |
214
|
0
|
|
|
|
|
|
memset(&p->aOp[oldSize], 0, (p->nOpAlloc-oldSize)*sizeof(Op)); |
215
|
|
|
|
|
|
|
} |
216
|
22
|
|
|
|
|
|
addr = p->nOp; |
217
|
22
|
50
|
|
|
|
|
if( nOp>0 ){ |
218
|
|
|
|
|
|
|
int i; |
219
|
22
|
|
|
|
|
|
VdbeOpList const *pIn = aOp; |
220
|
198
|
100
|
|
|
|
|
for(i=0; i
|
221
|
176
|
|
|
|
|
|
int p2 = pIn->p2; |
222
|
176
|
|
|
|
|
|
VdbeOp *pOut = &p->aOp[i+addr]; |
223
|
176
|
|
|
|
|
|
pOut->opcode = pIn->opcode; |
224
|
176
|
|
|
|
|
|
pOut->p1 = pIn->p1; |
225
|
176
|
100
|
|
|
|
|
pOut->p2 = p2<0 ? addr + ADDR(p2) : p2; |
226
|
176
|
|
|
|
|
|
pOut->p3 = pIn->p3; |
227
|
176
|
50
|
|
|
|
|
pOut->p3type = pIn->p3 ? P3_STATIC : P3_NOTUSED; |
228
|
|
|
|
|
|
|
#ifndef NDEBUG |
229
|
|
|
|
|
|
|
if( sqlite_vdbe_addop_trace ){ |
230
|
|
|
|
|
|
|
sqliteVdbePrintOp(0, i+addr, &p->aOp[i+addr]); |
231
|
|
|
|
|
|
|
} |
232
|
|
|
|
|
|
|
#endif |
233
|
|
|
|
|
|
|
} |
234
|
22
|
|
|
|
|
|
p->nOp += nOp; |
235
|
|
|
|
|
|
|
} |
236
|
22
|
|
|
|
|
|
return addr; |
237
|
|
|
|
|
|
|
} |
238
|
|
|
|
|
|
|
|
239
|
|
|
|
|
|
|
/* |
240
|
|
|
|
|
|
|
** Change the value of the P1 operand for a specific instruction. |
241
|
|
|
|
|
|
|
** This routine is useful when a large program is loaded from a |
242
|
|
|
|
|
|
|
** static array using sqliteVdbeAddOpList but we want to make a |
243
|
|
|
|
|
|
|
** few minor changes to the program. |
244
|
|
|
|
|
|
|
*/ |
245
|
0
|
|
|
|
|
|
void sqliteVdbeChangeP1(Vdbe *p, int addr, int val){ |
246
|
|
|
|
|
|
|
assert( p->magic==VDBE_MAGIC_INIT ); |
247
|
0
|
0
|
|
|
|
|
if( p && addr>=0 && p->nOp>addr && p->aOp ){ |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
248
|
0
|
|
|
|
|
|
p->aOp[addr].p1 = val; |
249
|
|
|
|
|
|
|
} |
250
|
0
|
|
|
|
|
|
} |
251
|
|
|
|
|
|
|
|
252
|
|
|
|
|
|
|
/* |
253
|
|
|
|
|
|
|
** Change the value of the P2 operand for a specific instruction. |
254
|
|
|
|
|
|
|
** This routine is useful for setting a jump destination. |
255
|
|
|
|
|
|
|
*/ |
256
|
38
|
|
|
|
|
|
void sqliteVdbeChangeP2(Vdbe *p, int addr, int val){ |
257
|
|
|
|
|
|
|
assert( val>=0 ); |
258
|
|
|
|
|
|
|
assert( p->magic==VDBE_MAGIC_INIT ); |
259
|
38
|
50
|
|
|
|
|
if( p && addr>=0 && p->nOp>addr && p->aOp ){ |
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
260
|
38
|
|
|
|
|
|
p->aOp[addr].p2 = val; |
261
|
|
|
|
|
|
|
} |
262
|
38
|
|
|
|
|
|
} |
263
|
|
|
|
|
|
|
|
264
|
|
|
|
|
|
|
/* |
265
|
|
|
|
|
|
|
** Change the value of the P3 operand for a specific instruction. |
266
|
|
|
|
|
|
|
** This routine is useful when a large program is loaded from a |
267
|
|
|
|
|
|
|
** static array using sqliteVdbeAddOpList but we want to make a |
268
|
|
|
|
|
|
|
** few minor changes to the program. |
269
|
|
|
|
|
|
|
** |
270
|
|
|
|
|
|
|
** If n>=0 then the P3 operand is dynamic, meaning that a copy of |
271
|
|
|
|
|
|
|
** the string is made into memory obtained from sqliteMalloc(). |
272
|
|
|
|
|
|
|
** A value of n==0 means copy bytes of zP3 up to and including the |
273
|
|
|
|
|
|
|
** first null byte. If n>0 then copy n+1 bytes of zP3. |
274
|
|
|
|
|
|
|
** |
275
|
|
|
|
|
|
|
** If n==P3_STATIC it means that zP3 is a pointer to a constant static |
276
|
|
|
|
|
|
|
** string and we can just copy the pointer. n==P3_POINTER means zP3 is |
277
|
|
|
|
|
|
|
** a pointer to some object other than a string. |
278
|
|
|
|
|
|
|
** |
279
|
|
|
|
|
|
|
** If addr<0 then change P3 on the most recently inserted instruction. |
280
|
|
|
|
|
|
|
*/ |
281
|
1416
|
|
|
|
|
|
void sqliteVdbeChangeP3(Vdbe *p, int addr, const char *zP3, int n){ |
282
|
|
|
|
|
|
|
Op *pOp; |
283
|
|
|
|
|
|
|
assert( p->magic==VDBE_MAGIC_INIT ); |
284
|
1416
|
50
|
|
|
|
|
if( p==0 || p->aOp==0 ) return; |
|
|
50
|
|
|
|
|
|
285
|
1416
|
100
|
|
|
|
|
if( addr<0 || addr>=p->nOp ){ |
|
|
50
|
|
|
|
|
|
286
|
252
|
|
|
|
|
|
addr = p->nOp - 1; |
287
|
252
|
50
|
|
|
|
|
if( addr<0 ) return; |
288
|
|
|
|
|
|
|
} |
289
|
1416
|
|
|
|
|
|
pOp = &p->aOp[addr]; |
290
|
1416
|
50
|
|
|
|
|
if( pOp->p3 && pOp->p3type==P3_DYNAMIC ){ |
|
|
0
|
|
|
|
|
|
291
|
0
|
|
|
|
|
|
sqliteFree(pOp->p3); |
292
|
0
|
|
|
|
|
|
pOp->p3 = 0; |
293
|
|
|
|
|
|
|
} |
294
|
1416
|
100
|
|
|
|
|
if( zP3==0 ){ |
295
|
21
|
|
|
|
|
|
pOp->p3 = 0; |
296
|
21
|
|
|
|
|
|
pOp->p3type = P3_NOTUSED; |
297
|
1395
|
100
|
|
|
|
|
}else if( n<0 ){ |
298
|
312
|
|
|
|
|
|
pOp->p3 = (char*)zP3; |
299
|
312
|
|
|
|
|
|
pOp->p3type = n; |
300
|
|
|
|
|
|
|
}else{ |
301
|
1083
|
|
|
|
|
|
sqliteSetNString(&pOp->p3, zP3, n, 0); |
302
|
1083
|
|
|
|
|
|
pOp->p3type = P3_DYNAMIC; |
303
|
|
|
|
|
|
|
} |
304
|
|
|
|
|
|
|
} |
305
|
|
|
|
|
|
|
|
306
|
|
|
|
|
|
|
/* |
307
|
|
|
|
|
|
|
** If the P3 operand to the specified instruction appears |
308
|
|
|
|
|
|
|
** to be a quoted string token, then this procedure removes |
309
|
|
|
|
|
|
|
** the quotes. |
310
|
|
|
|
|
|
|
** |
311
|
|
|
|
|
|
|
** The quoting operator can be either a grave ascent (ASCII 0x27) |
312
|
|
|
|
|
|
|
** or a double quote character (ASCII 0x22). Two quotes in a row |
313
|
|
|
|
|
|
|
** resolve to be a single actual quote character within the string. |
314
|
|
|
|
|
|
|
*/ |
315
|
209
|
|
|
|
|
|
void sqliteVdbeDequoteP3(Vdbe *p, int addr){ |
316
|
|
|
|
|
|
|
Op *pOp; |
317
|
|
|
|
|
|
|
assert( p->magic==VDBE_MAGIC_INIT ); |
318
|
209
|
50
|
|
|
|
|
if( p->aOp==0 ) return; |
319
|
209
|
100
|
|
|
|
|
if( addr<0 || addr>=p->nOp ){ |
|
|
50
|
|
|
|
|
|
320
|
197
|
|
|
|
|
|
addr = p->nOp - 1; |
321
|
197
|
50
|
|
|
|
|
if( addr<0 ) return; |
322
|
|
|
|
|
|
|
} |
323
|
209
|
|
|
|
|
|
pOp = &p->aOp[addr]; |
324
|
209
|
50
|
|
|
|
|
if( pOp->p3==0 || pOp->p3[0]==0 ) return; |
|
|
50
|
|
|
|
|
|
325
|
209
|
50
|
|
|
|
|
if( pOp->p3type==P3_POINTER ) return; |
326
|
209
|
50
|
|
|
|
|
if( pOp->p3type!=P3_DYNAMIC ){ |
327
|
0
|
|
|
|
|
|
pOp->p3 = sqliteStrDup(pOp->p3); |
328
|
0
|
|
|
|
|
|
pOp->p3type = P3_DYNAMIC; |
329
|
|
|
|
|
|
|
} |
330
|
209
|
|
|
|
|
|
sqliteDequote(pOp->p3); |
331
|
|
|
|
|
|
|
} |
332
|
|
|
|
|
|
|
|
333
|
|
|
|
|
|
|
/* |
334
|
|
|
|
|
|
|
** On the P3 argument of the given instruction, change all |
335
|
|
|
|
|
|
|
** strings of whitespace characters into a single space and |
336
|
|
|
|
|
|
|
** delete leading and trailing whitespace. |
337
|
|
|
|
|
|
|
*/ |
338
|
392
|
|
|
|
|
|
void sqliteVdbeCompressSpace(Vdbe *p, int addr){ |
339
|
|
|
|
|
|
|
unsigned char *z; |
340
|
|
|
|
|
|
|
int i, j; |
341
|
|
|
|
|
|
|
Op *pOp; |
342
|
|
|
|
|
|
|
assert( p->magic==VDBE_MAGIC_INIT ); |
343
|
392
|
50
|
|
|
|
|
if( p->aOp==0 || addr<0 || addr>=p->nOp ) return; |
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
344
|
392
|
|
|
|
|
|
pOp = &p->aOp[addr]; |
345
|
392
|
50
|
|
|
|
|
if( pOp->p3type==P3_POINTER ){ |
346
|
0
|
|
|
|
|
|
return; |
347
|
|
|
|
|
|
|
} |
348
|
392
|
50
|
|
|
|
|
if( pOp->p3type!=P3_DYNAMIC ){ |
349
|
0
|
|
|
|
|
|
pOp->p3 = sqliteStrDup(pOp->p3); |
350
|
0
|
|
|
|
|
|
pOp->p3type = P3_DYNAMIC; |
351
|
|
|
|
|
|
|
} |
352
|
392
|
|
|
|
|
|
z = (unsigned char*)pOp->p3; |
353
|
392
|
50
|
|
|
|
|
if( z==0 ) return; |
354
|
392
|
|
|
|
|
|
i = j = 0; |
355
|
392
|
50
|
|
|
|
|
while( isspace(z[i]) ){ i++; } |
356
|
2243
|
100
|
|
|
|
|
while( z[i] ){ |
357
|
1851
|
100
|
|
|
|
|
if( isspace(z[i]) ){ |
358
|
10
|
|
|
|
|
|
z[j++] = ' '; |
359
|
10
|
50
|
|
|
|
|
while( isspace(z[++i]) ){} |
360
|
|
|
|
|
|
|
}else{ |
361
|
1841
|
|
|
|
|
|
z[j++] = z[i++]; |
362
|
|
|
|
|
|
|
} |
363
|
|
|
|
|
|
|
} |
364
|
392
|
50
|
|
|
|
|
while( j>0 && isspace(z[j-1]) ){ j--; } |
|
|
50
|
|
|
|
|
|
365
|
392
|
|
|
|
|
|
z[j] = 0; |
366
|
|
|
|
|
|
|
} |
367
|
|
|
|
|
|
|
|
368
|
|
|
|
|
|
|
/* |
369
|
|
|
|
|
|
|
** Search for the current program for the given opcode and P2 |
370
|
|
|
|
|
|
|
** value. Return the address plus 1 if found and 0 if not found. |
371
|
|
|
|
|
|
|
*/ |
372
|
0
|
|
|
|
|
|
int sqliteVdbeFindOp(Vdbe *p, int op, int p2){ |
373
|
|
|
|
|
|
|
int i; |
374
|
|
|
|
|
|
|
assert( p->magic==VDBE_MAGIC_INIT ); |
375
|
0
|
0
|
|
|
|
|
for(i=0; inOp; i++){ |
376
|
0
|
0
|
|
|
|
|
if( p->aOp[i].opcode==op && p->aOp[i].p2==p2 ) return i+1; |
|
|
0
|
|
|
|
|
|
377
|
|
|
|
|
|
|
} |
378
|
0
|
|
|
|
|
|
return 0; |
379
|
|
|
|
|
|
|
} |
380
|
|
|
|
|
|
|
|
381
|
|
|
|
|
|
|
/* |
382
|
|
|
|
|
|
|
** Return the opcode for a given address. |
383
|
|
|
|
|
|
|
*/ |
384
|
0
|
|
|
|
|
|
VdbeOp *sqliteVdbeGetOp(Vdbe *p, int addr){ |
385
|
|
|
|
|
|
|
assert( p->magic==VDBE_MAGIC_INIT ); |
386
|
|
|
|
|
|
|
assert( addr>=0 && addrnOp ); |
387
|
0
|
|
|
|
|
|
return &p->aOp[addr]; |
388
|
|
|
|
|
|
|
} |
389
|
|
|
|
|
|
|
|
390
|
|
|
|
|
|
|
/* |
391
|
|
|
|
|
|
|
** The following group or routines are employed by installable functions |
392
|
|
|
|
|
|
|
** to return their results. |
393
|
|
|
|
|
|
|
** |
394
|
|
|
|
|
|
|
** The sqlite_set_result_string() routine can be used to return a string |
395
|
|
|
|
|
|
|
** value or to return a NULL. To return a NULL, pass in NULL for zResult. |
396
|
|
|
|
|
|
|
** A copy is made of the string before this routine returns so it is safe |
397
|
|
|
|
|
|
|
** to pass in an ephemeral string. |
398
|
|
|
|
|
|
|
** |
399
|
|
|
|
|
|
|
** sqlite_set_result_error() works like sqlite_set_result_string() except |
400
|
|
|
|
|
|
|
** that it signals a fatal error. The string argument, if any, is the |
401
|
|
|
|
|
|
|
** error message. If the argument is NULL a generic substitute error message |
402
|
|
|
|
|
|
|
** is used. |
403
|
|
|
|
|
|
|
** |
404
|
|
|
|
|
|
|
** The sqlite_set_result_int() and sqlite_set_result_double() set the return |
405
|
|
|
|
|
|
|
** value of the user function to an integer or a double. |
406
|
|
|
|
|
|
|
** |
407
|
|
|
|
|
|
|
** These routines are defined here in vdbe.c because they depend on knowing |
408
|
|
|
|
|
|
|
** the internals of the sqlite_func structure which is only defined in |
409
|
|
|
|
|
|
|
** this source file. |
410
|
|
|
|
|
|
|
*/ |
411
|
27
|
|
|
|
|
|
char *sqlite_set_result_string(sqlite_func *p, const char *zResult, int n){ |
412
|
|
|
|
|
|
|
assert( !p->isStep ); |
413
|
27
|
50
|
|
|
|
|
if( p->s.flags & MEM_Dyn ){ |
414
|
0
|
|
|
|
|
|
sqliteFree(p->s.z); |
415
|
|
|
|
|
|
|
} |
416
|
27
|
100
|
|
|
|
|
if( zResult==0 ){ |
417
|
3
|
|
|
|
|
|
p->s.flags = MEM_Null; |
418
|
3
|
|
|
|
|
|
n = 0; |
419
|
3
|
|
|
|
|
|
p->s.z = 0; |
420
|
3
|
|
|
|
|
|
p->s.n = 0; |
421
|
|
|
|
|
|
|
}else{ |
422
|
24
|
100
|
|
|
|
|
if( n<0 ) n = strlen(zResult); |
423
|
24
|
50
|
|
|
|
|
if( n
|
424
|
24
|
|
|
|
|
|
memcpy(p->s.zShort, zResult, n); |
425
|
24
|
|
|
|
|
|
p->s.zShort[n] = 0; |
426
|
24
|
|
|
|
|
|
p->s.flags = MEM_Str | MEM_Short; |
427
|
24
|
|
|
|
|
|
p->s.z = p->s.zShort; |
428
|
|
|
|
|
|
|
}else{ |
429
|
0
|
|
|
|
|
|
p->s.z = sqliteMallocRaw( n+1 ); |
430
|
0
|
0
|
|
|
|
|
if( p->s.z ){ |
431
|
0
|
|
|
|
|
|
memcpy(p->s.z, zResult, n); |
432
|
0
|
|
|
|
|
|
p->s.z[n] = 0; |
433
|
|
|
|
|
|
|
} |
434
|
0
|
|
|
|
|
|
p->s.flags = MEM_Str | MEM_Dyn; |
435
|
|
|
|
|
|
|
} |
436
|
24
|
|
|
|
|
|
p->s.n = n+1; |
437
|
|
|
|
|
|
|
} |
438
|
27
|
|
|
|
|
|
return p->s.z; |
439
|
|
|
|
|
|
|
} |
440
|
42
|
|
|
|
|
|
void sqlite_set_result_int(sqlite_func *p, int iResult){ |
441
|
|
|
|
|
|
|
assert( !p->isStep ); |
442
|
42
|
50
|
|
|
|
|
if( p->s.flags & MEM_Dyn ){ |
443
|
0
|
|
|
|
|
|
sqliteFree(p->s.z); |
444
|
|
|
|
|
|
|
} |
445
|
42
|
|
|
|
|
|
p->s.i = iResult; |
446
|
42
|
|
|
|
|
|
p->s.flags = MEM_Int; |
447
|
42
|
|
|
|
|
|
} |
448
|
2
|
|
|
|
|
|
void sqlite_set_result_double(sqlite_func *p, double rResult){ |
449
|
|
|
|
|
|
|
assert( !p->isStep ); |
450
|
2
|
50
|
|
|
|
|
if( p->s.flags & MEM_Dyn ){ |
451
|
0
|
|
|
|
|
|
sqliteFree(p->s.z); |
452
|
|
|
|
|
|
|
} |
453
|
2
|
|
|
|
|
|
p->s.r = rResult; |
454
|
2
|
|
|
|
|
|
p->s.flags = MEM_Real; |
455
|
2
|
|
|
|
|
|
} |
456
|
1
|
|
|
|
|
|
void sqlite_set_result_error(sqlite_func *p, const char *zMsg, int n){ |
457
|
|
|
|
|
|
|
assert( !p->isStep ); |
458
|
1
|
|
|
|
|
|
sqlite_set_result_string(p, zMsg, n); |
459
|
1
|
|
|
|
|
|
p->isError = 1; |
460
|
1
|
|
|
|
|
|
} |
461
|
|
|
|
|
|
|
|
462
|
|
|
|
|
|
|
/* |
463
|
|
|
|
|
|
|
** Extract the user data from a sqlite_func structure and return a |
464
|
|
|
|
|
|
|
** pointer to it. |
465
|
|
|
|
|
|
|
*/ |
466
|
29
|
|
|
|
|
|
void *sqlite_user_data(sqlite_func *p){ |
467
|
|
|
|
|
|
|
assert( p && p->pFunc ); |
468
|
29
|
|
|
|
|
|
return p->pFunc->pUserData; |
469
|
|
|
|
|
|
|
} |
470
|
|
|
|
|
|
|
|
471
|
|
|
|
|
|
|
/* |
472
|
|
|
|
|
|
|
** Allocate or return the aggregate context for a user function. A new |
473
|
|
|
|
|
|
|
** context is allocated on the first call. Subsequent calls return the |
474
|
|
|
|
|
|
|
** same context that was returned on prior calls. |
475
|
|
|
|
|
|
|
** |
476
|
|
|
|
|
|
|
** This routine is defined here in vdbe.c because it depends on knowing |
477
|
|
|
|
|
|
|
** the internals of the sqlite_func structure which is only defined in |
478
|
|
|
|
|
|
|
** this source file. |
479
|
|
|
|
|
|
|
*/ |
480
|
40
|
|
|
|
|
|
void *sqlite_aggregate_context(sqlite_func *p, int nByte){ |
481
|
|
|
|
|
|
|
assert( p && p->pFunc && p->pFunc->xStep ); |
482
|
40
|
100
|
|
|
|
|
if( p->pAgg==0 ){ |
483
|
16
|
50
|
|
|
|
|
if( nByte<=NBFS ){ |
484
|
16
|
|
|
|
|
|
p->pAgg = (void*)p->s.z; |
485
|
16
|
|
|
|
|
|
memset(p->pAgg, 0, nByte); |
486
|
|
|
|
|
|
|
}else{ |
487
|
0
|
|
|
|
|
|
p->pAgg = sqliteMalloc( nByte ); |
488
|
|
|
|
|
|
|
} |
489
|
|
|
|
|
|
|
} |
490
|
40
|
|
|
|
|
|
return p->pAgg; |
491
|
|
|
|
|
|
|
} |
492
|
|
|
|
|
|
|
|
493
|
|
|
|
|
|
|
/* |
494
|
|
|
|
|
|
|
** Return the number of times the Step function of a aggregate has been |
495
|
|
|
|
|
|
|
** called. |
496
|
|
|
|
|
|
|
** |
497
|
|
|
|
|
|
|
** This routine is defined here in vdbe.c because it depends on knowing |
498
|
|
|
|
|
|
|
** the internals of the sqlite_func structure which is only defined in |
499
|
|
|
|
|
|
|
** this source file. |
500
|
|
|
|
|
|
|
*/ |
501
|
0
|
|
|
|
|
|
int sqlite_aggregate_count(sqlite_func *p){ |
502
|
|
|
|
|
|
|
assert( p && p->pFunc && p->pFunc->xStep ); |
503
|
0
|
|
|
|
|
|
return p->cnt; |
504
|
|
|
|
|
|
|
} |
505
|
|
|
|
|
|
|
|
506
|
|
|
|
|
|
|
#if !defined(NDEBUG) || defined(VDBE_PROFILE) |
507
|
|
|
|
|
|
|
/* |
508
|
|
|
|
|
|
|
** Print a single opcode. This routine is used for debugging only. |
509
|
|
|
|
|
|
|
*/ |
510
|
|
|
|
|
|
|
void sqliteVdbePrintOp(FILE *pOut, int pc, Op *pOp){ |
511
|
|
|
|
|
|
|
char *zP3; |
512
|
|
|
|
|
|
|
char zPtr[40]; |
513
|
|
|
|
|
|
|
if( pOp->p3type==P3_POINTER ){ |
514
|
|
|
|
|
|
|
sprintf(zPtr, "ptr(%#lx)", (long)pOp->p3); |
515
|
|
|
|
|
|
|
zP3 = zPtr; |
516
|
|
|
|
|
|
|
}else{ |
517
|
|
|
|
|
|
|
zP3 = pOp->p3; |
518
|
|
|
|
|
|
|
} |
519
|
|
|
|
|
|
|
if( pOut==0 ) pOut = stdout; |
520
|
|
|
|
|
|
|
fprintf(pOut,"%4d %-12s %4d %4d %s\n", |
521
|
|
|
|
|
|
|
pc, sqliteOpcodeNames[pOp->opcode], pOp->p1, pOp->p2, zP3 ? zP3 : ""); |
522
|
|
|
|
|
|
|
fflush(pOut); |
523
|
|
|
|
|
|
|
} |
524
|
|
|
|
|
|
|
#endif |
525
|
|
|
|
|
|
|
|
526
|
|
|
|
|
|
|
/* |
527
|
|
|
|
|
|
|
** Give a listing of the program in the virtual machine. |
528
|
|
|
|
|
|
|
** |
529
|
|
|
|
|
|
|
** The interface is the same as sqliteVdbeExec(). But instead of |
530
|
|
|
|
|
|
|
** running the code, it invokes the callback once for each instruction. |
531
|
|
|
|
|
|
|
** This feature is used to implement "EXPLAIN". |
532
|
|
|
|
|
|
|
*/ |
533
|
0
|
|
|
|
|
|
int sqliteVdbeList( |
534
|
|
|
|
|
|
|
Vdbe *p /* The VDBE */ |
535
|
|
|
|
|
|
|
){ |
536
|
0
|
|
|
|
|
|
sqlite *db = p->db; |
537
|
|
|
|
|
|
|
int i; |
538
|
0
|
|
|
|
|
|
int rc = SQLITE_OK; |
539
|
|
|
|
|
|
|
static char *azColumnNames[] = { |
540
|
|
|
|
|
|
|
"addr", "opcode", "p1", "p2", "p3", |
541
|
|
|
|
|
|
|
"int", "text", "int", "int", "text", |
542
|
|
|
|
|
|
|
0 |
543
|
|
|
|
|
|
|
}; |
544
|
|
|
|
|
|
|
|
545
|
|
|
|
|
|
|
assert( p->popStack==0 ); |
546
|
|
|
|
|
|
|
assert( p->explain ); |
547
|
0
|
|
|
|
|
|
p->azColName = azColumnNames; |
548
|
0
|
|
|
|
|
|
p->azResColumn = p->zArgv; |
549
|
0
|
0
|
|
|
|
|
for(i=0; i<5; i++) p->zArgv[i] = p->aStack[i].zShort; |
550
|
0
|
|
|
|
|
|
i = p->pc; |
551
|
0
|
0
|
|
|
|
|
if( i>=p->nOp ){ |
552
|
0
|
|
|
|
|
|
p->rc = SQLITE_OK; |
553
|
0
|
|
|
|
|
|
rc = SQLITE_DONE; |
554
|
0
|
0
|
|
|
|
|
}else if( db->flags & SQLITE_Interrupt ){ |
555
|
0
|
|
|
|
|
|
db->flags &= ~SQLITE_Interrupt; |
556
|
0
|
0
|
|
|
|
|
if( db->magic!=SQLITE_MAGIC_BUSY ){ |
557
|
0
|
|
|
|
|
|
p->rc = SQLITE_MISUSE; |
558
|
|
|
|
|
|
|
}else{ |
559
|
0
|
|
|
|
|
|
p->rc = SQLITE_INTERRUPT; |
560
|
|
|
|
|
|
|
} |
561
|
0
|
|
|
|
|
|
rc = SQLITE_ERROR; |
562
|
0
|
|
|
|
|
|
sqliteSetString(&p->zErrMsg, sqlite_error_string(p->rc), (char*)0); |
563
|
|
|
|
|
|
|
}else{ |
564
|
0
|
|
|
|
|
|
sprintf(p->zArgv[0],"%d",i); |
565
|
0
|
|
|
|
|
|
sprintf(p->zArgv[2],"%d", p->aOp[i].p1); |
566
|
0
|
|
|
|
|
|
sprintf(p->zArgv[3],"%d", p->aOp[i].p2); |
567
|
0
|
0
|
|
|
|
|
if( p->aOp[i].p3type==P3_POINTER ){ |
568
|
0
|
|
|
|
|
|
sprintf(p->aStack[4].zShort, "ptr(%#lx)", (long)p->aOp[i].p3); |
569
|
0
|
|
|
|
|
|
p->zArgv[4] = p->aStack[4].zShort; |
570
|
|
|
|
|
|
|
}else{ |
571
|
0
|
|
|
|
|
|
p->zArgv[4] = p->aOp[i].p3; |
572
|
|
|
|
|
|
|
} |
573
|
0
|
|
|
|
|
|
p->zArgv[1] = sqliteOpcodeNames[p->aOp[i].opcode]; |
574
|
0
|
|
|
|
|
|
p->pc = i+1; |
575
|
0
|
|
|
|
|
|
p->azResColumn = p->zArgv; |
576
|
0
|
|
|
|
|
|
p->nResColumn = 5; |
577
|
0
|
|
|
|
|
|
p->rc = SQLITE_OK; |
578
|
0
|
|
|
|
|
|
rc = SQLITE_ROW; |
579
|
|
|
|
|
|
|
} |
580
|
0
|
|
|
|
|
|
return rc; |
581
|
|
|
|
|
|
|
} |
582
|
|
|
|
|
|
|
|
583
|
|
|
|
|
|
|
/* |
584
|
|
|
|
|
|
|
** Prepare a virtual machine for execution. This involves things such |
585
|
|
|
|
|
|
|
** as allocating stack space and initializing the program counter. |
586
|
|
|
|
|
|
|
** After the VDBE has be prepped, it can be executed by one or more |
587
|
|
|
|
|
|
|
** calls to sqliteVdbeExec(). |
588
|
|
|
|
|
|
|
*/ |
589
|
342
|
|
|
|
|
|
void sqliteVdbeMakeReady( |
590
|
|
|
|
|
|
|
Vdbe *p, /* The VDBE */ |
591
|
|
|
|
|
|
|
int nVar, /* Number of '?' see in the SQL statement */ |
592
|
|
|
|
|
|
|
int isExplain /* True if the EXPLAIN keywords is present */ |
593
|
|
|
|
|
|
|
){ |
594
|
|
|
|
|
|
|
int n; |
595
|
|
|
|
|
|
|
|
596
|
|
|
|
|
|
|
assert( p!=0 ); |
597
|
|
|
|
|
|
|
assert( p->magic==VDBE_MAGIC_INIT ); |
598
|
|
|
|
|
|
|
|
599
|
|
|
|
|
|
|
/* Add a HALT instruction to the very end of the program. |
600
|
|
|
|
|
|
|
*/ |
601
|
342
|
100
|
|
|
|
|
if( p->nOp==0 || (p->aOp && p->aOp[p->nOp-1].opcode!=OP_Halt) ){ |
|
|
50
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
602
|
269
|
|
|
|
|
|
sqliteVdbeAddOp(p, OP_Halt, 0, 0); |
603
|
|
|
|
|
|
|
} |
604
|
|
|
|
|
|
|
|
605
|
|
|
|
|
|
|
/* No instruction ever pushes more than a single element onto the |
606
|
|
|
|
|
|
|
** stack. And the stack never grows on successive executions of the |
607
|
|
|
|
|
|
|
** same loop. So the total number of instructions is an upper bound |
608
|
|
|
|
|
|
|
** on the maximum stack depth required. |
609
|
|
|
|
|
|
|
** |
610
|
|
|
|
|
|
|
** Allocation all the stack space we will ever need. |
611
|
|
|
|
|
|
|
*/ |
612
|
342
|
50
|
|
|
|
|
if( p->aStack==0 ){ |
613
|
342
|
|
|
|
|
|
p->nVar = nVar; |
614
|
|
|
|
|
|
|
assert( nVar>=0 ); |
615
|
342
|
50
|
|
|
|
|
n = isExplain ? 10 : p->nOp; |
616
|
342
|
|
|
|
|
|
p->aStack = sqliteMalloc( |
617
|
|
|
|
|
|
|
n*(sizeof(p->aStack[0]) + 2*sizeof(char*)) /* aStack and zArgv */ |
618
|
342
|
|
|
|
|
|
+ p->nVar*(sizeof(char*)+sizeof(int)+1) /* azVar, anVar, abVar */ |
619
|
|
|
|
|
|
|
); |
620
|
342
|
|
|
|
|
|
p->zArgv = (char**)&p->aStack[n]; |
621
|
342
|
|
|
|
|
|
p->azColName = (char**)&p->zArgv[n]; |
622
|
342
|
|
|
|
|
|
p->azVar = (char**)&p->azColName[n]; |
623
|
342
|
|
|
|
|
|
p->anVar = (int*)&p->azVar[p->nVar]; |
624
|
342
|
|
|
|
|
|
p->abVar = (u8*)&p->anVar[p->nVar]; |
625
|
|
|
|
|
|
|
} |
626
|
|
|
|
|
|
|
|
627
|
342
|
|
|
|
|
|
sqliteHashInit(&p->agg.hash, SQLITE_HASH_BINARY, 0); |
628
|
342
|
|
|
|
|
|
p->agg.pSearch = 0; |
629
|
|
|
|
|
|
|
#ifdef MEMORY_DEBUG |
630
|
|
|
|
|
|
|
if( sqliteOsFileExists("vdbe_trace") ){ |
631
|
|
|
|
|
|
|
p->trace = stdout; |
632
|
|
|
|
|
|
|
} |
633
|
|
|
|
|
|
|
#endif |
634
|
342
|
|
|
|
|
|
p->pTos = &p->aStack[-1]; |
635
|
342
|
|
|
|
|
|
p->pc = 0; |
636
|
342
|
|
|
|
|
|
p->rc = SQLITE_OK; |
637
|
342
|
|
|
|
|
|
p->uniqueCnt = 0; |
638
|
342
|
|
|
|
|
|
p->returnDepth = 0; |
639
|
342
|
|
|
|
|
|
p->errorAction = OE_Abort; |
640
|
342
|
|
|
|
|
|
p->undoTransOnError = 0; |
641
|
342
|
|
|
|
|
|
p->popStack = 0; |
642
|
342
|
|
|
|
|
|
p->explain |= isExplain; |
643
|
342
|
|
|
|
|
|
p->magic = VDBE_MAGIC_RUN; |
644
|
|
|
|
|
|
|
#ifdef VDBE_PROFILE |
645
|
|
|
|
|
|
|
{ |
646
|
|
|
|
|
|
|
int i; |
647
|
|
|
|
|
|
|
for(i=0; inOp; i++){ |
648
|
|
|
|
|
|
|
p->aOp[i].cnt = 0; |
649
|
|
|
|
|
|
|
p->aOp[i].cycles = 0; |
650
|
|
|
|
|
|
|
} |
651
|
|
|
|
|
|
|
} |
652
|
|
|
|
|
|
|
#endif |
653
|
342
|
|
|
|
|
|
} |
654
|
|
|
|
|
|
|
|
655
|
|
|
|
|
|
|
|
656
|
|
|
|
|
|
|
/* |
657
|
|
|
|
|
|
|
** Remove any elements that remain on the sorter for the VDBE given. |
658
|
|
|
|
|
|
|
*/ |
659
|
695
|
|
|
|
|
|
void sqliteVdbeSorterReset(Vdbe *p){ |
660
|
695
|
50
|
|
|
|
|
while( p->pSort ){ |
661
|
0
|
|
|
|
|
|
Sorter *pSorter = p->pSort; |
662
|
0
|
|
|
|
|
|
p->pSort = pSorter->pNext; |
663
|
0
|
|
|
|
|
|
sqliteFree(pSorter->zKey); |
664
|
0
|
|
|
|
|
|
sqliteFree(pSorter->pData); |
665
|
0
|
|
|
|
|
|
sqliteFree(pSorter); |
666
|
|
|
|
|
|
|
} |
667
|
695
|
|
|
|
|
|
} |
668
|
|
|
|
|
|
|
|
669
|
|
|
|
|
|
|
/* |
670
|
|
|
|
|
|
|
** Reset an Agg structure. Delete all its contents. |
671
|
|
|
|
|
|
|
** |
672
|
|
|
|
|
|
|
** For installable aggregate functions, if the step function has been |
673
|
|
|
|
|
|
|
** called, make sure the finalizer function has also been called. The |
674
|
|
|
|
|
|
|
** finalizer might need to free memory that was allocated as part of its |
675
|
|
|
|
|
|
|
** private context. If the finalizer has not been called yet, call it |
676
|
|
|
|
|
|
|
** now. |
677
|
|
|
|
|
|
|
*/ |
678
|
705
|
|
|
|
|
|
void sqliteVdbeAggReset(Agg *pAgg){ |
679
|
|
|
|
|
|
|
int i; |
680
|
|
|
|
|
|
|
HashElem *p; |
681
|
721
|
100
|
|
|
|
|
for(p = sqliteHashFirst(&pAgg->hash); p; p = sqliteHashNext(p)){ |
682
|
16
|
|
|
|
|
|
AggElem *pElem = sqliteHashData(p); |
683
|
|
|
|
|
|
|
assert( pAgg->apFunc!=0 ); |
684
|
35
|
100
|
|
|
|
|
for(i=0; inMem; i++){ |
685
|
19
|
|
|
|
|
|
Mem *pMem = &pElem->aMem[i]; |
686
|
19
|
100
|
|
|
|
|
if( pAgg->apFunc[i] && (pMem->flags & MEM_AggCtx)!=0 ){ |
|
|
50
|
|
|
|
|
|
687
|
|
|
|
|
|
|
sqlite_func ctx; |
688
|
0
|
|
|
|
|
|
ctx.pFunc = pAgg->apFunc[i]; |
689
|
0
|
|
|
|
|
|
ctx.s.flags = MEM_Null; |
690
|
0
|
|
|
|
|
|
ctx.pAgg = pMem->z; |
691
|
0
|
|
|
|
|
|
ctx.cnt = pMem->i; |
692
|
0
|
|
|
|
|
|
ctx.isStep = 0; |
693
|
0
|
|
|
|
|
|
ctx.isError = 0; |
694
|
0
|
|
|
|
|
|
(*pAgg->apFunc[i]->xFinalize)(&ctx); |
695
|
0
|
0
|
|
|
|
|
if( pMem->z!=0 && pMem->z!=pMem->zShort ){ |
|
|
0
|
|
|
|
|
|
696
|
0
|
|
|
|
|
|
sqliteFree(pMem->z); |
697
|
|
|
|
|
|
|
} |
698
|
0
|
0
|
|
|
|
|
if( ctx.s.flags & MEM_Dyn ){ |
699
|
0
|
|
|
|
|
|
sqliteFree(ctx.s.z); |
700
|
|
|
|
|
|
|
} |
701
|
19
|
50
|
|
|
|
|
}else if( pMem->flags & MEM_Dyn ){ |
702
|
0
|
|
|
|
|
|
sqliteFree(pMem->z); |
703
|
|
|
|
|
|
|
} |
704
|
|
|
|
|
|
|
} |
705
|
16
|
|
|
|
|
|
sqliteFree(pElem); |
706
|
|
|
|
|
|
|
} |
707
|
705
|
|
|
|
|
|
sqliteHashClear(&pAgg->hash); |
708
|
705
|
|
|
|
|
|
sqliteFree(pAgg->apFunc); |
709
|
705
|
|
|
|
|
|
pAgg->apFunc = 0; |
710
|
705
|
|
|
|
|
|
pAgg->pCurrent = 0; |
711
|
705
|
|
|
|
|
|
pAgg->pSearch = 0; |
712
|
705
|
|
|
|
|
|
pAgg->nMem = 0; |
713
|
705
|
|
|
|
|
|
} |
714
|
|
|
|
|
|
|
|
715
|
|
|
|
|
|
|
/* |
716
|
|
|
|
|
|
|
** Delete a keylist |
717
|
|
|
|
|
|
|
*/ |
718
|
0
|
|
|
|
|
|
void sqliteVdbeKeylistFree(Keylist *p){ |
719
|
0
|
0
|
|
|
|
|
while( p ){ |
720
|
0
|
|
|
|
|
|
Keylist *pNext = p->pNext; |
721
|
0
|
|
|
|
|
|
sqliteFree(p); |
722
|
0
|
|
|
|
|
|
p = pNext; |
723
|
|
|
|
|
|
|
} |
724
|
0
|
|
|
|
|
|
} |
725
|
|
|
|
|
|
|
|
726
|
|
|
|
|
|
|
/* |
727
|
|
|
|
|
|
|
** Close a cursor and release all the resources that cursor happens |
728
|
|
|
|
|
|
|
** to hold. |
729
|
|
|
|
|
|
|
*/ |
730
|
620
|
|
|
|
|
|
void sqliteVdbeCleanupCursor(Cursor *pCx){ |
731
|
620
|
100
|
|
|
|
|
if( pCx->pCursor ){ |
732
|
218
|
|
|
|
|
|
sqliteBtreeCloseCursor(pCx->pCursor); |
733
|
|
|
|
|
|
|
} |
734
|
620
|
100
|
|
|
|
|
if( pCx->pBt ){ |
735
|
3
|
|
|
|
|
|
sqliteBtreeClose(pCx->pBt); |
736
|
|
|
|
|
|
|
} |
737
|
620
|
|
|
|
|
|
sqliteFree(pCx->pData); |
738
|
620
|
|
|
|
|
|
memset(pCx, 0, sizeof(Cursor)); |
739
|
620
|
|
|
|
|
|
} |
740
|
|
|
|
|
|
|
|
741
|
|
|
|
|
|
|
/* |
742
|
|
|
|
|
|
|
** Close all cursors |
743
|
|
|
|
|
|
|
*/ |
744
|
690
|
|
|
|
|
|
static void closeAllCursors(Vdbe *p){ |
745
|
|
|
|
|
|
|
int i; |
746
|
894
|
100
|
|
|
|
|
for(i=0; inCursor; i++){ |
747
|
204
|
|
|
|
|
|
sqliteVdbeCleanupCursor(&p->aCsr[i]); |
748
|
|
|
|
|
|
|
} |
749
|
690
|
|
|
|
|
|
sqliteFree(p->aCsr); |
750
|
690
|
|
|
|
|
|
p->aCsr = 0; |
751
|
690
|
|
|
|
|
|
p->nCursor = 0; |
752
|
690
|
|
|
|
|
|
} |
753
|
|
|
|
|
|
|
|
754
|
|
|
|
|
|
|
/* |
755
|
|
|
|
|
|
|
** Clean up the VM after execution. |
756
|
|
|
|
|
|
|
** |
757
|
|
|
|
|
|
|
** This routine will automatically close any cursors, lists, and/or |
758
|
|
|
|
|
|
|
** sorters that were left open. It also deletes the values of |
759
|
|
|
|
|
|
|
** variables in the azVariable[] array. |
760
|
|
|
|
|
|
|
*/ |
761
|
690
|
|
|
|
|
|
static void Cleanup(Vdbe *p){ |
762
|
|
|
|
|
|
|
int i; |
763
|
690
|
100
|
|
|
|
|
if( p->aStack ){ |
764
|
684
|
|
|
|
|
|
Mem *pTos = p->pTos; |
765
|
720
|
100
|
|
|
|
|
while( pTos>=p->aStack ){ |
766
|
36
|
50
|
|
|
|
|
if( pTos->flags & MEM_Dyn ){ |
767
|
0
|
|
|
|
|
|
sqliteFree(pTos->z); |
768
|
|
|
|
|
|
|
} |
769
|
36
|
|
|
|
|
|
pTos--; |
770
|
|
|
|
|
|
|
} |
771
|
684
|
|
|
|
|
|
p->pTos = pTos; |
772
|
|
|
|
|
|
|
} |
773
|
690
|
|
|
|
|
|
closeAllCursors(p); |
774
|
690
|
100
|
|
|
|
|
if( p->aMem ){ |
775
|
77
|
100
|
|
|
|
|
for(i=0; inMem; i++){ |
776
|
66
|
50
|
|
|
|
|
if( p->aMem[i].flags & MEM_Dyn ){ |
777
|
0
|
|
|
|
|
|
sqliteFree(p->aMem[i].z); |
778
|
|
|
|
|
|
|
} |
779
|
|
|
|
|
|
|
} |
780
|
|
|
|
|
|
|
} |
781
|
690
|
|
|
|
|
|
sqliteFree(p->aMem); |
782
|
690
|
|
|
|
|
|
p->aMem = 0; |
783
|
690
|
|
|
|
|
|
p->nMem = 0; |
784
|
690
|
50
|
|
|
|
|
if( p->pList ){ |
785
|
0
|
|
|
|
|
|
sqliteVdbeKeylistFree(p->pList); |
786
|
0
|
|
|
|
|
|
p->pList = 0; |
787
|
|
|
|
|
|
|
} |
788
|
690
|
|
|
|
|
|
sqliteVdbeSorterReset(p); |
789
|
690
|
50
|
|
|
|
|
if( p->pFile ){ |
790
|
0
|
0
|
|
|
|
|
if( p->pFile!=stdin ) fclose(p->pFile); |
791
|
0
|
|
|
|
|
|
p->pFile = 0; |
792
|
|
|
|
|
|
|
} |
793
|
690
|
50
|
|
|
|
|
if( p->azField ){ |
794
|
0
|
|
|
|
|
|
sqliteFree(p->azField); |
795
|
0
|
|
|
|
|
|
p->azField = 0; |
796
|
|
|
|
|
|
|
} |
797
|
690
|
|
|
|
|
|
p->nField = 0; |
798
|
690
|
50
|
|
|
|
|
if( p->zLine ){ |
799
|
0
|
|
|
|
|
|
sqliteFree(p->zLine); |
800
|
0
|
|
|
|
|
|
p->zLine = 0; |
801
|
|
|
|
|
|
|
} |
802
|
690
|
|
|
|
|
|
p->nLineAlloc = 0; |
803
|
690
|
|
|
|
|
|
sqliteVdbeAggReset(&p->agg); |
804
|
690
|
100
|
|
|
|
|
if( p->aSet ){ |
805
|
9
|
100
|
|
|
|
|
for(i=0; inSet; i++){ |
806
|
6
|
|
|
|
|
|
sqliteHashClear(&p->aSet[i].hash); |
807
|
|
|
|
|
|
|
} |
808
|
|
|
|
|
|
|
} |
809
|
690
|
|
|
|
|
|
sqliteFree(p->aSet); |
810
|
690
|
|
|
|
|
|
p->aSet = 0; |
811
|
690
|
|
|
|
|
|
p->nSet = 0; |
812
|
690
|
50
|
|
|
|
|
if( p->keylistStack ){ |
813
|
|
|
|
|
|
|
int ii; |
814
|
0
|
0
|
|
|
|
|
for(ii = 0; ii < p->keylistStackDepth; ii++){ |
815
|
0
|
|
|
|
|
|
sqliteVdbeKeylistFree(p->keylistStack[ii]); |
816
|
|
|
|
|
|
|
} |
817
|
0
|
|
|
|
|
|
sqliteFree(p->keylistStack); |
818
|
0
|
|
|
|
|
|
p->keylistStackDepth = 0; |
819
|
0
|
|
|
|
|
|
p->keylistStack = 0; |
820
|
|
|
|
|
|
|
} |
821
|
690
|
|
|
|
|
|
sqliteFree(p->contextStack); |
822
|
690
|
|
|
|
|
|
p->contextStack = 0; |
823
|
690
|
|
|
|
|
|
sqliteFree(p->zErrMsg); |
824
|
690
|
|
|
|
|
|
p->zErrMsg = 0; |
825
|
690
|
|
|
|
|
|
} |
826
|
|
|
|
|
|
|
|
827
|
|
|
|
|
|
|
/* |
828
|
|
|
|
|
|
|
** Clean up a VDBE after execution but do not delete the VDBE just yet. |
829
|
|
|
|
|
|
|
** Write any error messages into *pzErrMsg. Return the result code. |
830
|
|
|
|
|
|
|
** |
831
|
|
|
|
|
|
|
** After this routine is run, the VDBE should be ready to be executed |
832
|
|
|
|
|
|
|
** again. |
833
|
|
|
|
|
|
|
*/ |
834
|
342
|
|
|
|
|
|
int sqliteVdbeReset(Vdbe *p, char **pzErrMsg){ |
835
|
342
|
|
|
|
|
|
sqlite *db = p->db; |
836
|
|
|
|
|
|
|
int i; |
837
|
|
|
|
|
|
|
|
838
|
342
|
100
|
|
|
|
|
if( p->magic!=VDBE_MAGIC_RUN && p->magic!=VDBE_MAGIC_HALT ){ |
|
|
50
|
|
|
|
|
|
839
|
0
|
|
|
|
|
|
sqliteSetString(pzErrMsg, sqlite_error_string(SQLITE_MISUSE), (char*)0); |
840
|
0
|
|
|
|
|
|
return SQLITE_MISUSE; |
841
|
|
|
|
|
|
|
} |
842
|
342
|
100
|
|
|
|
|
if( p->zErrMsg ){ |
843
|
2
|
50
|
|
|
|
|
if( pzErrMsg && *pzErrMsg==0 ){ |
|
|
50
|
|
|
|
|
|
844
|
2
|
|
|
|
|
|
*pzErrMsg = p->zErrMsg; |
845
|
|
|
|
|
|
|
}else{ |
846
|
0
|
|
|
|
|
|
sqliteFree(p->zErrMsg); |
847
|
|
|
|
|
|
|
} |
848
|
2
|
|
|
|
|
|
p->zErrMsg = 0; |
849
|
340
|
50
|
|
|
|
|
}else if( p->rc ){ |
850
|
0
|
|
|
|
|
|
sqliteSetString(pzErrMsg, sqlite_error_string(p->rc), (char*)0); |
851
|
|
|
|
|
|
|
} |
852
|
342
|
|
|
|
|
|
Cleanup(p); |
853
|
342
|
100
|
|
|
|
|
if( p->rc!=SQLITE_OK ){ |
854
|
2
|
|
|
|
|
|
switch( p->errorAction ){ |
855
|
|
|
|
|
|
|
case OE_Abort: { |
856
|
2
|
100
|
|
|
|
|
if( !p->undoTransOnError ){ |
857
|
3
|
100
|
|
|
|
|
for(i=0; inDb; i++){ |
858
|
2
|
50
|
|
|
|
|
if( db->aDb[i].pBt ){ |
859
|
2
|
|
|
|
|
|
sqliteBtreeRollbackCkpt(db->aDb[i].pBt); |
860
|
|
|
|
|
|
|
} |
861
|
|
|
|
|
|
|
} |
862
|
1
|
|
|
|
|
|
break; |
863
|
|
|
|
|
|
|
} |
864
|
|
|
|
|
|
|
/* Fall through to ROLLBACK */ |
865
|
|
|
|
|
|
|
} |
866
|
|
|
|
|
|
|
case OE_Rollback: { |
867
|
1
|
|
|
|
|
|
sqliteRollbackAll(db); |
868
|
1
|
|
|
|
|
|
db->flags &= ~SQLITE_InTrans; |
869
|
1
|
|
|
|
|
|
db->onError = OE_Default; |
870
|
1
|
|
|
|
|
|
break; |
871
|
|
|
|
|
|
|
} |
872
|
|
|
|
|
|
|
default: { |
873
|
0
|
0
|
|
|
|
|
if( p->undoTransOnError ){ |
874
|
0
|
|
|
|
|
|
sqliteRollbackAll(db); |
875
|
0
|
|
|
|
|
|
db->flags &= ~SQLITE_InTrans; |
876
|
0
|
|
|
|
|
|
db->onError = OE_Default; |
877
|
|
|
|
|
|
|
} |
878
|
0
|
|
|
|
|
|
break; |
879
|
|
|
|
|
|
|
} |
880
|
|
|
|
|
|
|
} |
881
|
2
|
|
|
|
|
|
sqliteRollbackInternalChanges(db); |
882
|
|
|
|
|
|
|
} |
883
|
1026
|
100
|
|
|
|
|
for(i=0; inDb; i++){ |
884
|
684
|
100
|
|
|
|
|
if( db->aDb[i].pBt && db->aDb[i].inTrans==2 ){ |
|
|
50
|
|
|
|
|
|
885
|
0
|
|
|
|
|
|
sqliteBtreeCommitCkpt(db->aDb[i].pBt); |
886
|
0
|
|
|
|
|
|
db->aDb[i].inTrans = 1; |
887
|
|
|
|
|
|
|
} |
888
|
|
|
|
|
|
|
} |
889
|
|
|
|
|
|
|
assert( p->pTos<&p->aStack[p->pc] || sqlite_malloc_failed==1 ); |
890
|
|
|
|
|
|
|
#ifdef VDBE_PROFILE |
891
|
|
|
|
|
|
|
{ |
892
|
|
|
|
|
|
|
FILE *out = fopen("vdbe_profile.out", "a"); |
893
|
|
|
|
|
|
|
if( out ){ |
894
|
|
|
|
|
|
|
int i; |
895
|
|
|
|
|
|
|
fprintf(out, "---- "); |
896
|
|
|
|
|
|
|
for(i=0; inOp; i++){ |
897
|
|
|
|
|
|
|
fprintf(out, "%02x", p->aOp[i].opcode); |
898
|
|
|
|
|
|
|
} |
899
|
|
|
|
|
|
|
fprintf(out, "\n"); |
900
|
|
|
|
|
|
|
for(i=0; inOp; i++){ |
901
|
|
|
|
|
|
|
fprintf(out, "%6d %10lld %8lld ", |
902
|
|
|
|
|
|
|
p->aOp[i].cnt, |
903
|
|
|
|
|
|
|
p->aOp[i].cycles, |
904
|
|
|
|
|
|
|
p->aOp[i].cnt>0 ? p->aOp[i].cycles/p->aOp[i].cnt : 0 |
905
|
|
|
|
|
|
|
); |
906
|
|
|
|
|
|
|
sqliteVdbePrintOp(out, i, &p->aOp[i]); |
907
|
|
|
|
|
|
|
} |
908
|
|
|
|
|
|
|
fclose(out); |
909
|
|
|
|
|
|
|
} |
910
|
|
|
|
|
|
|
} |
911
|
|
|
|
|
|
|
#endif |
912
|
342
|
|
|
|
|
|
p->magic = VDBE_MAGIC_INIT; |
913
|
342
|
|
|
|
|
|
return p->rc; |
914
|
|
|
|
|
|
|
} |
915
|
|
|
|
|
|
|
|
916
|
|
|
|
|
|
|
/* |
917
|
|
|
|
|
|
|
** Clean up and delete a VDBE after execution. Return an integer which is |
918
|
|
|
|
|
|
|
** the result code. Write any error message text into *pzErrMsg. |
919
|
|
|
|
|
|
|
*/ |
920
|
342
|
|
|
|
|
|
int sqliteVdbeFinalize(Vdbe *p, char **pzErrMsg){ |
921
|
|
|
|
|
|
|
int rc; |
922
|
|
|
|
|
|
|
sqlite *db; |
923
|
|
|
|
|
|
|
|
924
|
342
|
100
|
|
|
|
|
if( p->magic!=VDBE_MAGIC_RUN && p->magic!=VDBE_MAGIC_HALT ){ |
|
|
50
|
|
|
|
|
|
925
|
0
|
|
|
|
|
|
sqliteSetString(pzErrMsg, sqlite_error_string(SQLITE_MISUSE), (char*)0); |
926
|
0
|
|
|
|
|
|
return SQLITE_MISUSE; |
927
|
|
|
|
|
|
|
} |
928
|
342
|
|
|
|
|
|
db = p->db; |
929
|
342
|
|
|
|
|
|
rc = sqliteVdbeReset(p, pzErrMsg); |
930
|
342
|
|
|
|
|
|
sqliteVdbeDelete(p); |
931
|
342
|
50
|
|
|
|
|
if( db->want_to_close && db->pVdbe==0 ){ |
|
|
0
|
|
|
|
|
|
932
|
0
|
|
|
|
|
|
sqlite_close(db); |
933
|
|
|
|
|
|
|
} |
934
|
342
|
50
|
|
|
|
|
if( rc==SQLITE_SCHEMA ){ |
935
|
0
|
|
|
|
|
|
sqliteResetInternalSchema(db, 0); |
936
|
|
|
|
|
|
|
} |
937
|
342
|
|
|
|
|
|
return rc; |
938
|
|
|
|
|
|
|
} |
939
|
|
|
|
|
|
|
|
940
|
|
|
|
|
|
|
/* |
941
|
|
|
|
|
|
|
** Set the values of all variables. Variable $1 in the original SQL will |
942
|
|
|
|
|
|
|
** be the string azValue[0]. $2 will have the value azValue[1]. And |
943
|
|
|
|
|
|
|
** so forth. If a value is out of range (for example $3 when nValue==2) |
944
|
|
|
|
|
|
|
** then its value will be NULL. |
945
|
|
|
|
|
|
|
** |
946
|
|
|
|
|
|
|
** This routine overrides any prior call. |
947
|
|
|
|
|
|
|
*/ |
948
|
0
|
|
|
|
|
|
int sqlite_bind(sqlite_vm *pVm, int i, const char *zVal, int len, int copy){ |
949
|
0
|
|
|
|
|
|
Vdbe *p = (Vdbe*)pVm; |
950
|
0
|
0
|
|
|
|
|
if( p->magic!=VDBE_MAGIC_RUN || p->pc!=0 ){ |
|
|
0
|
|
|
|
|
|
951
|
0
|
|
|
|
|
|
return SQLITE_MISUSE; |
952
|
|
|
|
|
|
|
} |
953
|
0
|
0
|
|
|
|
|
if( i<1 || i>p->nVar ){ |
|
|
0
|
|
|
|
|
|
954
|
0
|
|
|
|
|
|
return SQLITE_RANGE; |
955
|
|
|
|
|
|
|
} |
956
|
0
|
|
|
|
|
|
i--; |
957
|
0
|
0
|
|
|
|
|
if( p->abVar[i] ){ |
958
|
0
|
|
|
|
|
|
sqliteFree(p->azVar[i]); |
959
|
|
|
|
|
|
|
} |
960
|
0
|
0
|
|
|
|
|
if( zVal==0 ){ |
961
|
0
|
|
|
|
|
|
copy = 0; |
962
|
0
|
|
|
|
|
|
len = 0; |
963
|
|
|
|
|
|
|
} |
964
|
0
|
0
|
|
|
|
|
if( len<0 ){ |
965
|
0
|
|
|
|
|
|
len = strlen(zVal)+1; |
966
|
|
|
|
|
|
|
} |
967
|
0
|
0
|
|
|
|
|
if( copy ){ |
968
|
0
|
|
|
|
|
|
p->azVar[i] = sqliteMalloc( len ); |
969
|
0
|
0
|
|
|
|
|
if( p->azVar[i] ) memcpy(p->azVar[i], zVal, len); |
970
|
|
|
|
|
|
|
}else{ |
971
|
0
|
|
|
|
|
|
p->azVar[i] = (char*)zVal; |
972
|
|
|
|
|
|
|
} |
973
|
0
|
|
|
|
|
|
p->abVar[i] = copy; |
974
|
0
|
|
|
|
|
|
p->anVar[i] = len; |
975
|
0
|
|
|
|
|
|
return SQLITE_OK; |
976
|
|
|
|
|
|
|
} |
977
|
|
|
|
|
|
|
|
978
|
|
|
|
|
|
|
|
979
|
|
|
|
|
|
|
/* |
980
|
|
|
|
|
|
|
** Delete an entire VDBE. |
981
|
|
|
|
|
|
|
*/ |
982
|
348
|
|
|
|
|
|
void sqliteVdbeDelete(Vdbe *p){ |
983
|
|
|
|
|
|
|
int i; |
984
|
348
|
50
|
|
|
|
|
if( p==0 ) return; |
985
|
348
|
|
|
|
|
|
Cleanup(p); |
986
|
348
|
50
|
|
|
|
|
if( p->pPrev ){ |
987
|
0
|
|
|
|
|
|
p->pPrev->pNext = p->pNext; |
988
|
|
|
|
|
|
|
}else{ |
989
|
|
|
|
|
|
|
assert( p->db->pVdbe==p ); |
990
|
348
|
|
|
|
|
|
p->db->pVdbe = p->pNext; |
991
|
|
|
|
|
|
|
} |
992
|
348
|
100
|
|
|
|
|
if( p->pNext ){ |
993
|
19
|
|
|
|
|
|
p->pNext->pPrev = p->pPrev; |
994
|
|
|
|
|
|
|
} |
995
|
348
|
|
|
|
|
|
p->pPrev = p->pNext = 0; |
996
|
348
|
50
|
|
|
|
|
if( p->nOpAlloc==0 ){ |
997
|
0
|
|
|
|
|
|
p->aOp = 0; |
998
|
0
|
|
|
|
|
|
p->nOp = 0; |
999
|
|
|
|
|
|
|
} |
1000
|
4742
|
100
|
|
|
|
|
for(i=0; inOp; i++){ |
1001
|
4394
|
100
|
|
|
|
|
if( p->aOp[i].p3type==P3_DYNAMIC ){ |
1002
|
1118
|
|
|
|
|
|
sqliteFree(p->aOp[i].p3); |
1003
|
|
|
|
|
|
|
} |
1004
|
|
|
|
|
|
|
} |
1005
|
348
|
50
|
|
|
|
|
for(i=0; inVar; i++){ |
1006
|
0
|
0
|
|
|
|
|
if( p->abVar[i] ) sqliteFree(p->azVar[i]); |
1007
|
|
|
|
|
|
|
} |
1008
|
348
|
|
|
|
|
|
sqliteFree(p->aOp); |
1009
|
348
|
|
|
|
|
|
sqliteFree(p->aLabel); |
1010
|
348
|
|
|
|
|
|
sqliteFree(p->aStack); |
1011
|
348
|
|
|
|
|
|
p->magic = VDBE_MAGIC_DEAD; |
1012
|
348
|
|
|
|
|
|
sqliteFree(p); |
1013
|
|
|
|
|
|
|
} |
1014
|
|
|
|
|
|
|
|
1015
|
|
|
|
|
|
|
/* |
1016
|
|
|
|
|
|
|
** Convert an integer in between the native integer format and |
1017
|
|
|
|
|
|
|
** the bigEndian format used as the record number for tables. |
1018
|
|
|
|
|
|
|
** |
1019
|
|
|
|
|
|
|
** The bigEndian format (most significant byte first) is used for |
1020
|
|
|
|
|
|
|
** record numbers so that records will sort into the correct order |
1021
|
|
|
|
|
|
|
** even though memcmp() is used to compare the keys. On machines |
1022
|
|
|
|
|
|
|
** whose native integer format is little endian (ex: i486) the |
1023
|
|
|
|
|
|
|
** order of bytes is reversed. On native big-endian machines |
1024
|
|
|
|
|
|
|
** (ex: Alpha, Sparc, Motorola) the byte order is the same. |
1025
|
|
|
|
|
|
|
** |
1026
|
|
|
|
|
|
|
** This function is its own inverse. In other words |
1027
|
|
|
|
|
|
|
** |
1028
|
|
|
|
|
|
|
** X == byteSwap(byteSwap(X)) |
1029
|
|
|
|
|
|
|
*/ |
1030
|
172
|
|
|
|
|
|
int sqliteVdbeByteSwap(int x){ |
1031
|
|
|
|
|
|
|
union { |
1032
|
|
|
|
|
|
|
char zBuf[sizeof(int)]; |
1033
|
|
|
|
|
|
|
int i; |
1034
|
|
|
|
|
|
|
} ux; |
1035
|
172
|
|
|
|
|
|
ux.zBuf[3] = x&0xff; |
1036
|
172
|
|
|
|
|
|
ux.zBuf[2] = (x>>8)&0xff; |
1037
|
172
|
|
|
|
|
|
ux.zBuf[1] = (x>>16)&0xff; |
1038
|
172
|
|
|
|
|
|
ux.zBuf[0] = (x>>24)&0xff; |
1039
|
172
|
|
|
|
|
|
return ux.i; |
1040
|
|
|
|
|
|
|
} |
1041
|
|
|
|
|
|
|
|
1042
|
|
|
|
|
|
|
/* |
1043
|
|
|
|
|
|
|
** If a MoveTo operation is pending on the given cursor, then do that |
1044
|
|
|
|
|
|
|
** MoveTo now. Return an error code. If no MoveTo is pending, this |
1045
|
|
|
|
|
|
|
** routine does nothing and returns SQLITE_OK. |
1046
|
|
|
|
|
|
|
*/ |
1047
|
411
|
|
|
|
|
|
int sqliteVdbeCursorMoveto(Cursor *p){ |
1048
|
411
|
50
|
|
|
|
|
if( p->deferredMoveto ){ |
1049
|
|
|
|
|
|
|
int res; |
1050
|
|
|
|
|
|
|
extern int sqlite_search_count; |
1051
|
0
|
|
|
|
|
|
sqliteBtreeMoveto(p->pCursor, (char*)&p->movetoTarget, sizeof(int), &res); |
1052
|
0
|
|
|
|
|
|
p->lastRecno = keyToInt(p->movetoTarget); |
1053
|
0
|
|
|
|
|
|
p->recnoIsValid = res==0; |
1054
|
0
|
0
|
|
|
|
|
if( res<0 ){ |
1055
|
0
|
|
|
|
|
|
sqliteBtreeNext(p->pCursor, &res); |
1056
|
|
|
|
|
|
|
} |
1057
|
0
|
|
|
|
|
|
sqlite_search_count++; |
1058
|
0
|
|
|
|
|
|
p->deferredMoveto = 0; |
1059
|
|
|
|
|
|
|
} |
1060
|
411
|
|
|
|
|
|
return SQLITE_OK; |
1061
|
|
|
|
|
|
|
} |