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