OLD | NEW |
1 /* | 1 /* |
2 ** 2003 September 6 | 2 ** 2003 September 6 |
3 ** | 3 ** |
4 ** The author disclaims copyright to this source code. In place of | 4 ** The author disclaims copyright to this source code. In place of |
5 ** a legal notice, here is a blessing: | 5 ** a legal notice, here is a blessing: |
6 ** | 6 ** |
7 ** May you do good and not evil. | 7 ** May you do good and not evil. |
8 ** May you find forgiveness for yourself and forgive others. | 8 ** May you find forgiveness for yourself and forgive others. |
9 ** May you share freely, never taking more than you give. | 9 ** May you share freely, never taking more than you give. |
10 ** | 10 ** |
11 ************************************************************************* | 11 ************************************************************************* |
12 ** This file contains code used for creating, destroying, and populating | 12 ** This file contains code used for creating, destroying, and populating |
13 ** a VDBE (or an "sqlite3_stmt" as it is known to the outside world.) Prior | 13 ** a VDBE (or an "sqlite3_stmt" as it is known to the outside world.) |
14 ** to version 2.8.7, all this code was combined into the vdbe.c source file. | |
15 ** But that file was getting too big so this subroutines were split out. | |
16 */ | 14 */ |
17 #include "sqliteInt.h" | 15 #include "sqliteInt.h" |
18 #include "vdbeInt.h" | 16 #include "vdbeInt.h" |
19 | 17 |
20 | |
21 | |
22 /* | |
23 ** When debugging the code generator in a symbolic debugger, one can | |
24 ** set the sqlite3VdbeAddopTrace to 1 and all opcodes will be printed | |
25 ** as they are added to the instruction stream. | |
26 */ | |
27 #ifdef SQLITE_DEBUG | |
28 int sqlite3VdbeAddopTrace = 0; | |
29 #endif | |
30 | |
31 | |
32 /* | 18 /* |
33 ** Create a new virtual database engine. | 19 ** Create a new virtual database engine. |
34 */ | 20 */ |
35 Vdbe *sqlite3VdbeCreate(sqlite3 *db){ | 21 Vdbe *sqlite3VdbeCreate(Parse *pParse){ |
| 22 sqlite3 *db = pParse->db; |
36 Vdbe *p; | 23 Vdbe *p; |
37 p = sqlite3DbMallocZero(db, sizeof(Vdbe) ); | 24 p = sqlite3DbMallocZero(db, sizeof(Vdbe) ); |
38 if( p==0 ) return 0; | 25 if( p==0 ) return 0; |
39 p->db = db; | 26 p->db = db; |
40 if( db->pVdbe ){ | 27 if( db->pVdbe ){ |
41 db->pVdbe->pPrev = p; | 28 db->pVdbe->pPrev = p; |
42 } | 29 } |
43 p->pNext = db->pVdbe; | 30 p->pNext = db->pVdbe; |
44 p->pPrev = 0; | 31 p->pPrev = 0; |
45 db->pVdbe = p; | 32 db->pVdbe = p; |
46 p->magic = VDBE_MAGIC_INIT; | 33 p->magic = VDBE_MAGIC_INIT; |
| 34 p->pParse = pParse; |
| 35 assert( pParse->aLabel==0 ); |
| 36 assert( pParse->nLabel==0 ); |
| 37 assert( pParse->nOpAlloc==0 ); |
47 return p; | 38 return p; |
48 } | 39 } |
49 | 40 |
50 /* | 41 /* |
51 ** Remember the SQL string for a prepared statement. | 42 ** Remember the SQL string for a prepared statement. |
52 */ | 43 */ |
53 void sqlite3VdbeSetSql(Vdbe *p, const char *z, int n, int isPrepareV2){ | 44 void sqlite3VdbeSetSql(Vdbe *p, const char *z, int n, int isPrepareV2){ |
54 assert( isPrepareV2==1 || isPrepareV2==0 ); | 45 assert( isPrepareV2==1 || isPrepareV2==0 ); |
55 if( p==0 ) return; | 46 if( p==0 ) return; |
56 #ifdef SQLITE_OMIT_TRACE | 47 #if defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_ENABLE_SQLLOG) |
57 if( !isPrepareV2 ) return; | 48 if( !isPrepareV2 ) return; |
58 #endif | 49 #endif |
59 assert( p->zSql==0 ); | 50 assert( p->zSql==0 ); |
60 p->zSql = sqlite3DbStrNDup(p->db, z, n); | 51 p->zSql = sqlite3DbStrNDup(p->db, z, n); |
61 p->isPrepareV2 = (u8)isPrepareV2; | 52 p->isPrepareV2 = (u8)isPrepareV2; |
62 } | 53 } |
63 | 54 |
64 /* | 55 /* |
65 ** Return the SQL associated with a prepared statement | 56 ** Return the SQL associated with a prepared statement |
66 */ | 57 */ |
(...skipping 16 matching lines...) Expand all Loading... |
83 pB->pNext = pTmp; | 74 pB->pNext = pTmp; |
84 pTmp = pA->pPrev; | 75 pTmp = pA->pPrev; |
85 pA->pPrev = pB->pPrev; | 76 pA->pPrev = pB->pPrev; |
86 pB->pPrev = pTmp; | 77 pB->pPrev = pTmp; |
87 zTmp = pA->zSql; | 78 zTmp = pA->zSql; |
88 pA->zSql = pB->zSql; | 79 pA->zSql = pB->zSql; |
89 pB->zSql = zTmp; | 80 pB->zSql = zTmp; |
90 pB->isPrepareV2 = pA->isPrepareV2; | 81 pB->isPrepareV2 = pA->isPrepareV2; |
91 } | 82 } |
92 | 83 |
| 84 /* |
| 85 ** Resize the Vdbe.aOp array so that it is at least nOp elements larger |
| 86 ** than its current size. nOp is guaranteed to be less than or equal |
| 87 ** to 1024/sizeof(Op). |
| 88 ** |
| 89 ** If an out-of-memory error occurs while resizing the array, return |
| 90 ** SQLITE_NOMEM. In this case Vdbe.aOp and Parse.nOpAlloc remain |
| 91 ** unchanged (this is so that any opcodes already allocated can be |
| 92 ** correctly deallocated along with the rest of the Vdbe). |
| 93 */ |
| 94 static int growOpArray(Vdbe *v, int nOp){ |
| 95 VdbeOp *pNew; |
| 96 Parse *p = v->pParse; |
| 97 |
| 98 /* The SQLITE_TEST_REALLOC_STRESS compile-time option is designed to force |
| 99 ** more frequent reallocs and hence provide more opportunities for |
| 100 ** simulated OOM faults. SQLITE_TEST_REALLOC_STRESS is generally used |
| 101 ** during testing only. With SQLITE_TEST_REALLOC_STRESS grow the op array |
| 102 ** by the minimum* amount required until the size reaches 512. Normal |
| 103 ** operation (without SQLITE_TEST_REALLOC_STRESS) is to double the current |
| 104 ** size of the op array or add 1KB of space, whichever is smaller. */ |
| 105 #ifdef SQLITE_TEST_REALLOC_STRESS |
| 106 int nNew = (p->nOpAlloc>=512 ? p->nOpAlloc*2 : p->nOpAlloc+nOp); |
| 107 #else |
| 108 int nNew = (p->nOpAlloc ? p->nOpAlloc*2 : (int)(1024/sizeof(Op))); |
| 109 UNUSED_PARAMETER(nOp); |
| 110 #endif |
| 111 |
| 112 assert( nOp<=(1024/sizeof(Op)) ); |
| 113 assert( nNew>=(p->nOpAlloc+nOp) ); |
| 114 pNew = sqlite3DbRealloc(p->db, v->aOp, nNew*sizeof(Op)); |
| 115 if( pNew ){ |
| 116 p->nOpAlloc = sqlite3DbMallocSize(p->db, pNew)/sizeof(Op); |
| 117 v->aOp = pNew; |
| 118 } |
| 119 return (pNew ? SQLITE_OK : SQLITE_NOMEM); |
| 120 } |
| 121 |
93 #ifdef SQLITE_DEBUG | 122 #ifdef SQLITE_DEBUG |
94 /* | 123 /* This routine is just a convenient place to set a breakpoint that will |
95 ** Turn tracing on or off | 124 ** fire after each opcode is inserted and displayed using |
| 125 ** "PRAGMA vdbe_addoptrace=on". |
96 */ | 126 */ |
97 void sqlite3VdbeTrace(Vdbe *p, FILE *trace){ | 127 static void test_addop_breakpoint(void){ |
98 p->trace = trace; | 128 static int n = 0; |
| 129 n++; |
99 } | 130 } |
100 #endif | 131 #endif |
101 | 132 |
102 /* | 133 /* |
103 ** Resize the Vdbe.aOp array so that it is at least one op larger than | |
104 ** it was. | |
105 ** | |
106 ** If an out-of-memory error occurs while resizing the array, return | |
107 ** SQLITE_NOMEM. In this case Vdbe.aOp and Vdbe.nOpAlloc remain | |
108 ** unchanged (this is so that any opcodes already allocated can be | |
109 ** correctly deallocated along with the rest of the Vdbe). | |
110 */ | |
111 static int growOpArray(Vdbe *p){ | |
112 VdbeOp *pNew; | |
113 int nNew = (p->nOpAlloc ? p->nOpAlloc*2 : (int)(1024/sizeof(Op))); | |
114 pNew = sqlite3DbRealloc(p->db, p->aOp, nNew*sizeof(Op)); | |
115 if( pNew ){ | |
116 p->nOpAlloc = sqlite3DbMallocSize(p->db, pNew)/sizeof(Op); | |
117 p->aOp = pNew; | |
118 } | |
119 return (pNew ? SQLITE_OK : SQLITE_NOMEM); | |
120 } | |
121 | |
122 /* | |
123 ** Add a new instruction to the list of instructions current in the | 134 ** Add a new instruction to the list of instructions current in the |
124 ** VDBE. Return the address of the new instruction. | 135 ** VDBE. Return the address of the new instruction. |
125 ** | 136 ** |
126 ** Parameters: | 137 ** Parameters: |
127 ** | 138 ** |
128 ** p Pointer to the VDBE | 139 ** p Pointer to the VDBE |
129 ** | 140 ** |
130 ** op The opcode for this instruction | 141 ** op The opcode for this instruction |
131 ** | 142 ** |
132 ** p1, p2, p3 Operands | 143 ** p1, p2, p3 Operands |
133 ** | 144 ** |
134 ** Use the sqlite3VdbeResolveLabel() function to fix an address and | 145 ** Use the sqlite3VdbeResolveLabel() function to fix an address and |
135 ** the sqlite3VdbeChangeP4() function to change the value of the P4 | 146 ** the sqlite3VdbeChangeP4() function to change the value of the P4 |
136 ** operand. | 147 ** operand. |
137 */ | 148 */ |
138 int sqlite3VdbeAddOp3(Vdbe *p, int op, int p1, int p2, int p3){ | 149 int sqlite3VdbeAddOp3(Vdbe *p, int op, int p1, int p2, int p3){ |
139 int i; | 150 int i; |
140 VdbeOp *pOp; | 151 VdbeOp *pOp; |
141 | 152 |
142 i = p->nOp; | 153 i = p->nOp; |
143 assert( p->magic==VDBE_MAGIC_INIT ); | 154 assert( p->magic==VDBE_MAGIC_INIT ); |
144 assert( op>0 && op<0xff ); | 155 assert( op>0 && op<0xff ); |
145 if( p->nOpAlloc<=i ){ | 156 if( p->pParse->nOpAlloc<=i ){ |
146 if( growOpArray(p) ){ | 157 if( growOpArray(p, 1) ){ |
147 return 1; | 158 return 1; |
148 } | 159 } |
149 } | 160 } |
150 p->nOp++; | 161 p->nOp++; |
151 pOp = &p->aOp[i]; | 162 pOp = &p->aOp[i]; |
152 pOp->opcode = (u8)op; | 163 pOp->opcode = (u8)op; |
153 pOp->p5 = 0; | 164 pOp->p5 = 0; |
154 pOp->p1 = p1; | 165 pOp->p1 = p1; |
155 pOp->p2 = p2; | 166 pOp->p2 = p2; |
156 pOp->p3 = p3; | 167 pOp->p3 = p3; |
157 pOp->p4.p = 0; | 168 pOp->p4.p = 0; |
158 pOp->p4type = P4_NOTUSED; | 169 pOp->p4type = P4_NOTUSED; |
159 p->expired = 0; | 170 #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS |
160 if( op==OP_ParseSchema ){ | 171 pOp->zComment = 0; |
161 /* Any program that uses the OP_ParseSchema opcode needs to lock | 172 #endif |
162 ** all btrees. */ | 173 #ifdef SQLITE_DEBUG |
163 int j; | 174 if( p->db->flags & SQLITE_VdbeAddopTrace ){ |
164 for(j=0; j<p->db->nDb; j++) sqlite3VdbeUsesBtree(p, j); | 175 int jj, kk; |
| 176 Parse *pParse = p->pParse; |
| 177 for(jj=kk=0; jj<SQLITE_N_COLCACHE; jj++){ |
| 178 struct yColCache *x = pParse->aColCache + jj; |
| 179 if( x->iLevel>pParse->iCacheLevel || x->iReg==0 ) continue; |
| 180 printf(" r[%d]={%d:%d}", x->iReg, x->iTable, x->iColumn); |
| 181 kk++; |
| 182 } |
| 183 if( kk ) printf("\n"); |
| 184 sqlite3VdbePrintOp(0, i, &p->aOp[i]); |
| 185 test_addop_breakpoint(); |
165 } | 186 } |
166 #ifdef SQLITE_DEBUG | |
167 pOp->zComment = 0; | |
168 if( sqlite3VdbeAddopTrace ) sqlite3VdbePrintOp(0, i, &p->aOp[i]); | |
169 #endif | 187 #endif |
170 #ifdef VDBE_PROFILE | 188 #ifdef VDBE_PROFILE |
171 pOp->cycles = 0; | 189 pOp->cycles = 0; |
172 pOp->cnt = 0; | 190 pOp->cnt = 0; |
173 #endif | 191 #endif |
| 192 #ifdef SQLITE_VDBE_COVERAGE |
| 193 pOp->iSrcLine = 0; |
| 194 #endif |
174 return i; | 195 return i; |
175 } | 196 } |
176 int sqlite3VdbeAddOp0(Vdbe *p, int op){ | 197 int sqlite3VdbeAddOp0(Vdbe *p, int op){ |
177 return sqlite3VdbeAddOp3(p, op, 0, 0, 0); | 198 return sqlite3VdbeAddOp3(p, op, 0, 0, 0); |
178 } | 199 } |
179 int sqlite3VdbeAddOp1(Vdbe *p, int op, int p1){ | 200 int sqlite3VdbeAddOp1(Vdbe *p, int op, int p1){ |
180 return sqlite3VdbeAddOp3(p, op, p1, 0, 0); | 201 return sqlite3VdbeAddOp3(p, op, p1, 0, 0); |
181 } | 202 } |
182 int sqlite3VdbeAddOp2(Vdbe *p, int op, int p1, int p2){ | 203 int sqlite3VdbeAddOp2(Vdbe *p, int op, int p1, int p2){ |
183 return sqlite3VdbeAddOp3(p, op, p1, p2, 0); | 204 return sqlite3VdbeAddOp3(p, op, p1, p2, 0); |
(...skipping 11 matching lines...) Expand all Loading... |
195 int p3, /* The P3 operand */ | 216 int p3, /* The P3 operand */ |
196 const char *zP4, /* The P4 operand */ | 217 const char *zP4, /* The P4 operand */ |
197 int p4type /* P4 operand type */ | 218 int p4type /* P4 operand type */ |
198 ){ | 219 ){ |
199 int addr = sqlite3VdbeAddOp3(p, op, p1, p2, p3); | 220 int addr = sqlite3VdbeAddOp3(p, op, p1, p2, p3); |
200 sqlite3VdbeChangeP4(p, addr, zP4, p4type); | 221 sqlite3VdbeChangeP4(p, addr, zP4, p4type); |
201 return addr; | 222 return addr; |
202 } | 223 } |
203 | 224 |
204 /* | 225 /* |
| 226 ** Add an OP_ParseSchema opcode. This routine is broken out from |
| 227 ** sqlite3VdbeAddOp4() since it needs to also needs to mark all btrees |
| 228 ** as having been used. |
| 229 ** |
| 230 ** The zWhere string must have been obtained from sqlite3_malloc(). |
| 231 ** This routine will take ownership of the allocated memory. |
| 232 */ |
| 233 void sqlite3VdbeAddParseSchemaOp(Vdbe *p, int iDb, char *zWhere){ |
| 234 int j; |
| 235 int addr = sqlite3VdbeAddOp3(p, OP_ParseSchema, iDb, 0, 0); |
| 236 sqlite3VdbeChangeP4(p, addr, zWhere, P4_DYNAMIC); |
| 237 for(j=0; j<p->db->nDb; j++) sqlite3VdbeUsesBtree(p, j); |
| 238 } |
| 239 |
| 240 /* |
205 ** Add an opcode that includes the p4 value as an integer. | 241 ** Add an opcode that includes the p4 value as an integer. |
206 */ | 242 */ |
207 int sqlite3VdbeAddOp4Int( | 243 int sqlite3VdbeAddOp4Int( |
208 Vdbe *p, /* Add the opcode to this VM */ | 244 Vdbe *p, /* Add the opcode to this VM */ |
209 int op, /* The new opcode */ | 245 int op, /* The new opcode */ |
210 int p1, /* The P1 operand */ | 246 int p1, /* The P1 operand */ |
211 int p2, /* The P2 operand */ | 247 int p2, /* The P2 operand */ |
212 int p3, /* The P3 operand */ | 248 int p3, /* The P3 operand */ |
213 int p4 /* The P4 operand as an integer */ | 249 int p4 /* The P4 operand as an integer */ |
214 ){ | 250 ){ |
215 int addr = sqlite3VdbeAddOp3(p, op, p1, p2, p3); | 251 int addr = sqlite3VdbeAddOp3(p, op, p1, p2, p3); |
216 sqlite3VdbeChangeP4(p, addr, SQLITE_INT_TO_PTR(p4), P4_INT32); | 252 sqlite3VdbeChangeP4(p, addr, SQLITE_INT_TO_PTR(p4), P4_INT32); |
217 return addr; | 253 return addr; |
218 } | 254 } |
219 | 255 |
220 /* | 256 /* |
221 ** Create a new symbolic label for an instruction that has yet to be | 257 ** Create a new symbolic label for an instruction that has yet to be |
222 ** coded. The symbolic label is really just a negative number. The | 258 ** coded. The symbolic label is really just a negative number. The |
223 ** label can be used as the P2 value of an operation. Later, when | 259 ** label can be used as the P2 value of an operation. Later, when |
224 ** the label is resolved to a specific address, the VDBE will scan | 260 ** the label is resolved to a specific address, the VDBE will scan |
225 ** through its operation list and change all values of P2 which match | 261 ** through its operation list and change all values of P2 which match |
226 ** the label into the resolved address. | 262 ** the label into the resolved address. |
227 ** | 263 ** |
228 ** The VDBE knows that a P2 value is a label because labels are | 264 ** The VDBE knows that a P2 value is a label because labels are |
229 ** always negative and P2 values are suppose to be non-negative. | 265 ** always negative and P2 values are suppose to be non-negative. |
230 ** Hence, a negative P2 value is a label that has yet to be resolved. | 266 ** Hence, a negative P2 value is a label that has yet to be resolved. |
231 ** | 267 ** |
232 ** Zero is returned if a malloc() fails. | 268 ** Zero is returned if a malloc() fails. |
233 */ | 269 */ |
234 int sqlite3VdbeMakeLabel(Vdbe *p){ | 270 int sqlite3VdbeMakeLabel(Vdbe *v){ |
235 int i; | 271 Parse *p = v->pParse; |
236 i = p->nLabel++; | 272 int i = p->nLabel++; |
237 assert( p->magic==VDBE_MAGIC_INIT ); | 273 assert( v->magic==VDBE_MAGIC_INIT ); |
238 if( i>=p->nLabelAlloc ){ | 274 if( (i & (i-1))==0 ){ |
239 int n = p->nLabelAlloc*2 + 5; | 275 p->aLabel = sqlite3DbReallocOrFree(p->db, p->aLabel, |
240 p->aLabel = sqlite3DbReallocOrFree(p->db, p->aLabel, | 276 (i*2+1)*sizeof(p->aLabel[0])); |
241 n*sizeof(p->aLabel[0])); | |
242 p->nLabelAlloc = sqlite3DbMallocSize(p->db, p->aLabel)/sizeof(p->aLabel[0]); | |
243 } | 277 } |
244 if( p->aLabel ){ | 278 if( p->aLabel ){ |
245 p->aLabel[i] = -1; | 279 p->aLabel[i] = -1; |
246 } | 280 } |
247 return -1-i; | 281 return -1-i; |
248 } | 282 } |
249 | 283 |
250 /* | 284 /* |
251 ** Resolve label "x" to be the address of the next instruction to | 285 ** Resolve label "x" to be the address of the next instruction to |
252 ** be inserted. The parameter "x" must have been obtained from | 286 ** be inserted. The parameter "x" must have been obtained from |
253 ** a prior call to sqlite3VdbeMakeLabel(). | 287 ** a prior call to sqlite3VdbeMakeLabel(). |
254 */ | 288 */ |
255 void sqlite3VdbeResolveLabel(Vdbe *p, int x){ | 289 void sqlite3VdbeResolveLabel(Vdbe *v, int x){ |
| 290 Parse *p = v->pParse; |
256 int j = -1-x; | 291 int j = -1-x; |
257 assert( p->magic==VDBE_MAGIC_INIT ); | 292 assert( v->magic==VDBE_MAGIC_INIT ); |
258 assert( j>=0 && j<p->nLabel ); | 293 assert( j<p->nLabel ); |
259 if( p->aLabel ){ | 294 if( ALWAYS(j>=0) && p->aLabel ){ |
260 p->aLabel[j] = p->nOp; | 295 p->aLabel[j] = v->nOp; |
261 } | 296 } |
| 297 p->iFixedOp = v->nOp - 1; |
262 } | 298 } |
263 | 299 |
264 /* | 300 /* |
265 ** Mark the VDBE as one that can only be run one time. | 301 ** Mark the VDBE as one that can only be run one time. |
266 */ | 302 */ |
267 void sqlite3VdbeRunOnlyOnce(Vdbe *p){ | 303 void sqlite3VdbeRunOnlyOnce(Vdbe *p){ |
268 p->runOnlyOnce = 1; | 304 p->runOnlyOnce = 1; |
269 } | 305 } |
270 | 306 |
271 #ifdef SQLITE_DEBUG /* sqlite3AssertMayAbort() logic */ | 307 #ifdef SQLITE_DEBUG /* sqlite3AssertMayAbort() logic */ |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
365 memset(&sIter, 0, sizeof(sIter)); | 401 memset(&sIter, 0, sizeof(sIter)); |
366 sIter.v = v; | 402 sIter.v = v; |
367 | 403 |
368 while( (pOp = opIterNext(&sIter))!=0 ){ | 404 while( (pOp = opIterNext(&sIter))!=0 ){ |
369 int opcode = pOp->opcode; | 405 int opcode = pOp->opcode; |
370 if( opcode==OP_Destroy || opcode==OP_VUpdate || opcode==OP_VRename | 406 if( opcode==OP_Destroy || opcode==OP_VUpdate || opcode==OP_VRename |
371 #ifndef SQLITE_OMIT_FOREIGN_KEY | 407 #ifndef SQLITE_OMIT_FOREIGN_KEY |
372 || (opcode==OP_FkCounter && pOp->p1==0 && pOp->p2==1) | 408 || (opcode==OP_FkCounter && pOp->p1==0 && pOp->p2==1) |
373 #endif | 409 #endif |
374 || ((opcode==OP_Halt || opcode==OP_HaltIfNull) | 410 || ((opcode==OP_Halt || opcode==OP_HaltIfNull) |
375 && (pOp->p1==SQLITE_CONSTRAINT && pOp->p2==OE_Abort)) | 411 && ((pOp->p1&0xff)==SQLITE_CONSTRAINT && pOp->p2==OE_Abort)) |
376 ){ | 412 ){ |
377 hasAbort = 1; | 413 hasAbort = 1; |
378 break; | 414 break; |
379 } | 415 } |
380 } | 416 } |
381 sqlite3DbFree(v->db, sIter.apSub); | 417 sqlite3DbFree(v->db, sIter.apSub); |
382 | 418 |
383 /* Return true if hasAbort==mayAbort. Or if a malloc failure occured. | 419 /* Return true if hasAbort==mayAbort. Or if a malloc failure occurred. |
384 ** If malloc failed, then the while() loop above may not have iterated | 420 ** If malloc failed, then the while() loop above may not have iterated |
385 ** through all opcodes and hasAbort may be set incorrectly. Return | 421 ** through all opcodes and hasAbort may be set incorrectly. Return |
386 ** true for this case to prevent the assert() in the callers frame | 422 ** true for this case to prevent the assert() in the callers frame |
387 ** from failing. */ | 423 ** from failing. */ |
388 return ( v->db->mallocFailed || hasAbort==mayAbort ); | 424 return ( v->db->mallocFailed || hasAbort==mayAbort ); |
389 } | 425 } |
390 #endif /* SQLITE_DEBUG - the sqlite3AssertMayAbort() function */ | 426 #endif /* SQLITE_DEBUG - the sqlite3AssertMayAbort() function */ |
391 | 427 |
392 /* | 428 /* |
393 ** Loop through the program looking for P2 values that are negative | 429 ** Loop through the program looking for P2 values that are negative |
394 ** on jump instructions. Each such value is a label. Resolve the | 430 ** on jump instructions. Each such value is a label. Resolve the |
395 ** label by setting the P2 value to its correct non-zero value. | 431 ** label by setting the P2 value to its correct non-zero value. |
396 ** | 432 ** |
397 ** This routine is called once after all opcodes have been inserted. | 433 ** This routine is called once after all opcodes have been inserted. |
398 ** | 434 ** |
399 ** Variable *pMaxFuncArgs is set to the maximum value of any P2 argument | 435 ** Variable *pMaxFuncArgs is set to the maximum value of any P2 argument |
400 ** to an OP_Function, OP_AggStep or OP_VFilter opcode. This is used by | 436 ** to an OP_Function, OP_AggStep or OP_VFilter opcode. This is used by |
401 ** sqlite3VdbeMakeReady() to size the Vdbe.apArg[] array. | 437 ** sqlite3VdbeMakeReady() to size the Vdbe.apArg[] array. |
402 ** | 438 ** |
403 ** The Op.opflags field is set on all opcodes. | 439 ** The Op.opflags field is set on all opcodes. |
404 */ | 440 */ |
405 static void resolveP2Values(Vdbe *p, int *pMaxFuncArgs){ | 441 static void resolveP2Values(Vdbe *p, int *pMaxFuncArgs){ |
406 int i; | 442 int i; |
407 int nMaxArgs = *pMaxFuncArgs; | 443 int nMaxArgs = *pMaxFuncArgs; |
408 Op *pOp; | 444 Op *pOp; |
409 int *aLabel = p->aLabel; | 445 Parse *pParse = p->pParse; |
| 446 int *aLabel = pParse->aLabel; |
410 p->readOnly = 1; | 447 p->readOnly = 1; |
| 448 p->bIsReader = 0; |
411 for(pOp=p->aOp, i=p->nOp-1; i>=0; i--, pOp++){ | 449 for(pOp=p->aOp, i=p->nOp-1; i>=0; i--, pOp++){ |
412 u8 opcode = pOp->opcode; | 450 u8 opcode = pOp->opcode; |
413 | 451 |
414 pOp->opflags = sqlite3OpcodeProperty[opcode]; | 452 /* NOTE: Be sure to update mkopcodeh.awk when adding or removing |
415 if( opcode==OP_Function || opcode==OP_AggStep ){ | 453 ** cases from this switch! */ |
416 if( pOp->p5>nMaxArgs ) nMaxArgs = pOp->p5; | 454 switch( opcode ){ |
417 }else if( (opcode==OP_Transaction && pOp->p2!=0) || opcode==OP_Vacuum ){ | 455 case OP_Function: |
418 p->readOnly = 0; | 456 case OP_AggStep: { |
| 457 if( pOp->p5>nMaxArgs ) nMaxArgs = pOp->p5; |
| 458 break; |
| 459 } |
| 460 case OP_Transaction: { |
| 461 if( pOp->p2!=0 ) p->readOnly = 0; |
| 462 /* fall thru */ |
| 463 } |
| 464 case OP_AutoCommit: |
| 465 case OP_Savepoint: { |
| 466 p->bIsReader = 1; |
| 467 break; |
| 468 } |
| 469 #ifndef SQLITE_OMIT_WAL |
| 470 case OP_Checkpoint: |
| 471 #endif |
| 472 case OP_Vacuum: |
| 473 case OP_JournalMode: { |
| 474 p->readOnly = 0; |
| 475 p->bIsReader = 1; |
| 476 break; |
| 477 } |
419 #ifndef SQLITE_OMIT_VIRTUALTABLE | 478 #ifndef SQLITE_OMIT_VIRTUALTABLE |
420 }else if( opcode==OP_VUpdate ){ | 479 case OP_VUpdate: { |
421 if( pOp->p2>nMaxArgs ) nMaxArgs = pOp->p2; | 480 if( pOp->p2>nMaxArgs ) nMaxArgs = pOp->p2; |
422 }else if( opcode==OP_VFilter ){ | 481 break; |
423 int n; | 482 } |
424 assert( p->nOp - i >= 3 ); | 483 case OP_VFilter: { |
425 assert( pOp[-1].opcode==OP_Integer ); | 484 int n; |
426 n = pOp[-1].p1; | 485 assert( p->nOp - i >= 3 ); |
427 if( n>nMaxArgs ) nMaxArgs = n; | 486 assert( pOp[-1].opcode==OP_Integer ); |
| 487 n = pOp[-1].p1; |
| 488 if( n>nMaxArgs ) nMaxArgs = n; |
| 489 break; |
| 490 } |
428 #endif | 491 #endif |
| 492 case OP_Next: |
| 493 case OP_NextIfOpen: |
| 494 case OP_SorterNext: { |
| 495 pOp->p4.xAdvance = sqlite3BtreeNext; |
| 496 pOp->p4type = P4_ADVANCE; |
| 497 break; |
| 498 } |
| 499 case OP_Prev: |
| 500 case OP_PrevIfOpen: { |
| 501 pOp->p4.xAdvance = sqlite3BtreePrevious; |
| 502 pOp->p4type = P4_ADVANCE; |
| 503 break; |
| 504 } |
429 } | 505 } |
430 | 506 |
| 507 pOp->opflags = sqlite3OpcodeProperty[opcode]; |
431 if( (pOp->opflags & OPFLG_JUMP)!=0 && pOp->p2<0 ){ | 508 if( (pOp->opflags & OPFLG_JUMP)!=0 && pOp->p2<0 ){ |
432 assert( -1-pOp->p2<p->nLabel ); | 509 assert( -1-pOp->p2<pParse->nLabel ); |
433 pOp->p2 = aLabel[-1-pOp->p2]; | 510 pOp->p2 = aLabel[-1-pOp->p2]; |
434 } | 511 } |
435 } | 512 } |
436 sqlite3DbFree(p->db, p->aLabel); | 513 sqlite3DbFree(p->db, pParse->aLabel); |
437 p->aLabel = 0; | 514 pParse->aLabel = 0; |
438 | 515 pParse->nLabel = 0; |
439 *pMaxFuncArgs = nMaxArgs; | 516 *pMaxFuncArgs = nMaxArgs; |
| 517 assert( p->bIsReader!=0 || DbMaskAllZero(p->btreeMask) ); |
440 } | 518 } |
441 | 519 |
442 /* | 520 /* |
443 ** Return the address of the next instruction to be inserted. | 521 ** Return the address of the next instruction to be inserted. |
444 */ | 522 */ |
445 int sqlite3VdbeCurrentAddr(Vdbe *p){ | 523 int sqlite3VdbeCurrentAddr(Vdbe *p){ |
446 assert( p->magic==VDBE_MAGIC_INIT ); | 524 assert( p->magic==VDBE_MAGIC_INIT ); |
447 return p->nOp; | 525 return p->nOp; |
448 } | 526 } |
449 | 527 |
450 /* | 528 /* |
451 ** This function returns a pointer to the array of opcodes associated with | 529 ** This function returns a pointer to the array of opcodes associated with |
452 ** the Vdbe passed as the first argument. It is the callers responsibility | 530 ** the Vdbe passed as the first argument. It is the callers responsibility |
453 ** to arrange for the returned array to be eventually freed using the | 531 ** to arrange for the returned array to be eventually freed using the |
454 ** vdbeFreeOpArray() function. | 532 ** vdbeFreeOpArray() function. |
455 ** | 533 ** |
456 ** Before returning, *pnOp is set to the number of entries in the returned | 534 ** Before returning, *pnOp is set to the number of entries in the returned |
457 ** array. Also, *pnMaxArg is set to the larger of its current value and | 535 ** array. Also, *pnMaxArg is set to the larger of its current value and |
458 ** the number of entries in the Vdbe.apArg[] array required to execute the | 536 ** the number of entries in the Vdbe.apArg[] array required to execute the |
459 ** returned program. | 537 ** returned program. |
460 */ | 538 */ |
461 VdbeOp *sqlite3VdbeTakeOpArray(Vdbe *p, int *pnOp, int *pnMaxArg){ | 539 VdbeOp *sqlite3VdbeTakeOpArray(Vdbe *p, int *pnOp, int *pnMaxArg){ |
462 VdbeOp *aOp = p->aOp; | 540 VdbeOp *aOp = p->aOp; |
463 assert( aOp && !p->db->mallocFailed ); | 541 assert( aOp && !p->db->mallocFailed ); |
464 | 542 |
465 /* Check that sqlite3VdbeUsesBtree() was not called on this VM */ | 543 /* Check that sqlite3VdbeUsesBtree() was not called on this VM */ |
466 assert( p->btreeMask==0 ); | 544 assert( DbMaskAllZero(p->btreeMask) ); |
467 | 545 |
468 resolveP2Values(p, pnMaxArg); | 546 resolveP2Values(p, pnMaxArg); |
469 *pnOp = p->nOp; | 547 *pnOp = p->nOp; |
470 p->aOp = 0; | 548 p->aOp = 0; |
471 return aOp; | 549 return aOp; |
472 } | 550 } |
473 | 551 |
474 /* | 552 /* |
475 ** Add a whole list of operations to the operation stack. Return the | 553 ** Add a whole list of operations to the operation stack. Return the |
476 ** address of the first operation added. | 554 ** address of the first operation added. |
477 */ | 555 */ |
478 int sqlite3VdbeAddOpList(Vdbe *p, int nOp, VdbeOpList const *aOp){ | 556 int sqlite3VdbeAddOpList(Vdbe *p, int nOp, VdbeOpList const *aOp, int iLineno){ |
479 int addr; | 557 int addr; |
480 assert( p->magic==VDBE_MAGIC_INIT ); | 558 assert( p->magic==VDBE_MAGIC_INIT ); |
481 if( p->nOp + nOp > p->nOpAlloc && growOpArray(p) ){ | 559 if( p->nOp + nOp > p->pParse->nOpAlloc && growOpArray(p, nOp) ){ |
482 return 0; | 560 return 0; |
483 } | 561 } |
484 addr = p->nOp; | 562 addr = p->nOp; |
485 if( ALWAYS(nOp>0) ){ | 563 if( ALWAYS(nOp>0) ){ |
486 int i; | 564 int i; |
487 VdbeOpList const *pIn = aOp; | 565 VdbeOpList const *pIn = aOp; |
488 for(i=0; i<nOp; i++, pIn++){ | 566 for(i=0; i<nOp; i++, pIn++){ |
489 int p2 = pIn->p2; | 567 int p2 = pIn->p2; |
490 VdbeOp *pOut = &p->aOp[i+addr]; | 568 VdbeOp *pOut = &p->aOp[i+addr]; |
491 pOut->opcode = pIn->opcode; | 569 pOut->opcode = pIn->opcode; |
492 pOut->p1 = pIn->p1; | 570 pOut->p1 = pIn->p1; |
493 if( p2<0 && (sqlite3OpcodeProperty[pOut->opcode] & OPFLG_JUMP)!=0 ){ | 571 if( p2<0 ){ |
| 572 assert( sqlite3OpcodeProperty[pOut->opcode] & OPFLG_JUMP ); |
494 pOut->p2 = addr + ADDR(p2); | 573 pOut->p2 = addr + ADDR(p2); |
495 }else{ | 574 }else{ |
496 pOut->p2 = p2; | 575 pOut->p2 = p2; |
497 } | 576 } |
498 pOut->p3 = pIn->p3; | 577 pOut->p3 = pIn->p3; |
499 pOut->p4type = P4_NOTUSED; | 578 pOut->p4type = P4_NOTUSED; |
500 pOut->p4.p = 0; | 579 pOut->p4.p = 0; |
501 pOut->p5 = 0; | 580 pOut->p5 = 0; |
| 581 #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS |
| 582 pOut->zComment = 0; |
| 583 #endif |
| 584 #ifdef SQLITE_VDBE_COVERAGE |
| 585 pOut->iSrcLine = iLineno+i; |
| 586 #else |
| 587 (void)iLineno; |
| 588 #endif |
502 #ifdef SQLITE_DEBUG | 589 #ifdef SQLITE_DEBUG |
503 pOut->zComment = 0; | 590 if( p->db->flags & SQLITE_VdbeAddopTrace ){ |
504 if( sqlite3VdbeAddopTrace ){ | |
505 sqlite3VdbePrintOp(0, i+addr, &p->aOp[i+addr]); | 591 sqlite3VdbePrintOp(0, i+addr, &p->aOp[i+addr]); |
506 } | 592 } |
507 #endif | 593 #endif |
508 } | 594 } |
509 p->nOp += nOp; | 595 p->nOp += nOp; |
510 } | 596 } |
511 return addr; | 597 return addr; |
512 } | 598 } |
513 | 599 |
514 /* | 600 /* |
515 ** Change the value of the P1 operand for a specific instruction. | 601 ** Change the value of the P1 operand for a specific instruction. |
516 ** This routine is useful when a large program is loaded from a | 602 ** This routine is useful when a large program is loaded from a |
517 ** static array using sqlite3VdbeAddOpList but we want to make a | 603 ** static array using sqlite3VdbeAddOpList but we want to make a |
518 ** few minor changes to the program. | 604 ** few minor changes to the program. |
519 */ | 605 */ |
520 void sqlite3VdbeChangeP1(Vdbe *p, int addr, int val){ | 606 void sqlite3VdbeChangeP1(Vdbe *p, u32 addr, int val){ |
521 assert( p!=0 ); | 607 assert( p!=0 ); |
522 assert( addr>=0 ); | 608 if( ((u32)p->nOp)>addr ){ |
523 if( p->nOp>addr ){ | |
524 p->aOp[addr].p1 = val; | 609 p->aOp[addr].p1 = val; |
525 } | 610 } |
526 } | 611 } |
527 | 612 |
528 /* | 613 /* |
529 ** Change the value of the P2 operand for a specific instruction. | 614 ** Change the value of the P2 operand for a specific instruction. |
530 ** This routine is useful for setting a jump destination. | 615 ** This routine is useful for setting a jump destination. |
531 */ | 616 */ |
532 void sqlite3VdbeChangeP2(Vdbe *p, int addr, int val){ | 617 void sqlite3VdbeChangeP2(Vdbe *p, u32 addr, int val){ |
533 assert( p!=0 ); | 618 assert( p!=0 ); |
534 assert( addr>=0 ); | 619 if( ((u32)p->nOp)>addr ){ |
535 if( p->nOp>addr ){ | |
536 p->aOp[addr].p2 = val; | 620 p->aOp[addr].p2 = val; |
537 } | 621 } |
538 } | 622 } |
539 | 623 |
540 /* | 624 /* |
541 ** Change the value of the P3 operand for a specific instruction. | 625 ** Change the value of the P3 operand for a specific instruction. |
542 */ | 626 */ |
543 void sqlite3VdbeChangeP3(Vdbe *p, int addr, int val){ | 627 void sqlite3VdbeChangeP3(Vdbe *p, u32 addr, int val){ |
544 assert( p!=0 ); | 628 assert( p!=0 ); |
545 assert( addr>=0 ); | 629 if( ((u32)p->nOp)>addr ){ |
546 if( p->nOp>addr ){ | |
547 p->aOp[addr].p3 = val; | 630 p->aOp[addr].p3 = val; |
548 } | 631 } |
549 } | 632 } |
550 | 633 |
551 /* | 634 /* |
552 ** Change the value of the P5 operand for the most recently | 635 ** Change the value of the P5 operand for the most recently |
553 ** added operation. | 636 ** added operation. |
554 */ | 637 */ |
555 void sqlite3VdbeChangeP5(Vdbe *p, u8 val){ | 638 void sqlite3VdbeChangeP5(Vdbe *p, u8 val){ |
556 assert( p!=0 ); | 639 assert( p!=0 ); |
557 if( p->aOp ){ | 640 if( p->aOp ){ |
558 assert( p->nOp>0 ); | 641 assert( p->nOp>0 ); |
559 p->aOp[p->nOp-1].p5 = val; | 642 p->aOp[p->nOp-1].p5 = val; |
560 } | 643 } |
561 } | 644 } |
562 | 645 |
563 /* | 646 /* |
564 ** Change the P2 operand of instruction addr so that it points to | 647 ** Change the P2 operand of instruction addr so that it points to |
565 ** the address of the next instruction to be coded. | 648 ** the address of the next instruction to be coded. |
566 */ | 649 */ |
567 void sqlite3VdbeJumpHere(Vdbe *p, int addr){ | 650 void sqlite3VdbeJumpHere(Vdbe *p, int addr){ |
568 assert( addr>=0 ); | |
569 sqlite3VdbeChangeP2(p, addr, p->nOp); | 651 sqlite3VdbeChangeP2(p, addr, p->nOp); |
| 652 p->pParse->iFixedOp = p->nOp - 1; |
570 } | 653 } |
571 | 654 |
572 | 655 |
573 /* | 656 /* |
574 ** If the input FuncDef structure is ephemeral, then free it. If | 657 ** If the input FuncDef structure is ephemeral, then free it. If |
575 ** the FuncDef is not ephermal, then do nothing. | 658 ** the FuncDef is not ephermal, then do nothing. |
576 */ | 659 */ |
577 static void freeEphemeralFunction(sqlite3 *db, FuncDef *pDef){ | 660 static void freeEphemeralFunction(sqlite3 *db, FuncDef *pDef){ |
578 if( ALWAYS(pDef) && (pDef->flags & SQLITE_FUNC_EPHEM)!=0 ){ | 661 if( ALWAYS(pDef) && (pDef->funcFlags & SQLITE_FUNC_EPHEM)!=0 ){ |
579 sqlite3DbFree(db, pDef); | 662 sqlite3DbFree(db, pDef); |
580 } | 663 } |
581 } | 664 } |
582 | 665 |
583 static void vdbeFreeOpArray(sqlite3 *, Op *, int); | 666 static void vdbeFreeOpArray(sqlite3 *, Op *, int); |
584 | 667 |
585 /* | 668 /* |
586 ** Delete a P4 value if necessary. | 669 ** Delete a P4 value if necessary. |
587 */ | 670 */ |
588 static void freeP4(sqlite3 *db, int p4type, void *p4){ | 671 static void freeP4(sqlite3 *db, int p4type, void *p4){ |
589 if( p4 ){ | 672 if( p4 ){ |
590 assert( db ); | 673 assert( db ); |
591 switch( p4type ){ | 674 switch( p4type ){ |
592 case P4_REAL: | 675 case P4_REAL: |
593 case P4_INT64: | 676 case P4_INT64: |
594 case P4_DYNAMIC: | 677 case P4_DYNAMIC: |
595 case P4_KEYINFO: | 678 case P4_INTARRAY: { |
596 case P4_INTARRAY: | |
597 case P4_KEYINFO_HANDOFF: { | |
598 sqlite3DbFree(db, p4); | 679 sqlite3DbFree(db, p4); |
599 break; | 680 break; |
600 } | 681 } |
| 682 case P4_KEYINFO: { |
| 683 if( db->pnBytesFreed==0 ) sqlite3KeyInfoUnref((KeyInfo*)p4); |
| 684 break; |
| 685 } |
601 case P4_MPRINTF: { | 686 case P4_MPRINTF: { |
602 if( db->pnBytesFreed==0 ) sqlite3_free(p4); | 687 if( db->pnBytesFreed==0 ) sqlite3_free(p4); |
603 break; | 688 break; |
604 } | 689 } |
605 case P4_VDBEFUNC: { | |
606 VdbeFunc *pVdbeFunc = (VdbeFunc *)p4; | |
607 freeEphemeralFunction(db, pVdbeFunc->pFunc); | |
608 if( db->pnBytesFreed==0 ) sqlite3VdbeDeleteAuxData(pVdbeFunc, 0); | |
609 sqlite3DbFree(db, pVdbeFunc); | |
610 break; | |
611 } | |
612 case P4_FUNCDEF: { | 690 case P4_FUNCDEF: { |
613 freeEphemeralFunction(db, (FuncDef*)p4); | 691 freeEphemeralFunction(db, (FuncDef*)p4); |
614 break; | 692 break; |
615 } | 693 } |
616 case P4_MEM: { | 694 case P4_MEM: { |
617 if( db->pnBytesFreed==0 ){ | 695 if( db->pnBytesFreed==0 ){ |
618 sqlite3ValueFree((sqlite3_value*)p4); | 696 sqlite3ValueFree((sqlite3_value*)p4); |
619 }else{ | 697 }else{ |
620 Mem *p = (Mem*)p4; | 698 Mem *p = (Mem*)p4; |
621 sqlite3DbFree(db, p->zMalloc); | 699 if( p->szMalloc ) sqlite3DbFree(db, p->zMalloc); |
622 sqlite3DbFree(db, p); | 700 sqlite3DbFree(db, p); |
623 } | 701 } |
624 break; | 702 break; |
625 } | 703 } |
626 case P4_VTAB : { | 704 case P4_VTAB : { |
627 if( db->pnBytesFreed==0 ) sqlite3VtabUnlock((VTable *)p4); | 705 if( db->pnBytesFreed==0 ) sqlite3VtabUnlock((VTable *)p4); |
628 break; | 706 break; |
629 } | 707 } |
630 } | 708 } |
631 } | 709 } |
632 } | 710 } |
633 | 711 |
634 /* | 712 /* |
635 ** Free the space allocated for aOp and any p4 values allocated for the | 713 ** Free the space allocated for aOp and any p4 values allocated for the |
636 ** opcodes contained within. If aOp is not NULL it is assumed to contain | 714 ** opcodes contained within. If aOp is not NULL it is assumed to contain |
637 ** nOp entries. | 715 ** nOp entries. |
638 */ | 716 */ |
639 static void vdbeFreeOpArray(sqlite3 *db, Op *aOp, int nOp){ | 717 static void vdbeFreeOpArray(sqlite3 *db, Op *aOp, int nOp){ |
640 if( aOp ){ | 718 if( aOp ){ |
641 Op *pOp; | 719 Op *pOp; |
642 for(pOp=aOp; pOp<&aOp[nOp]; pOp++){ | 720 for(pOp=aOp; pOp<&aOp[nOp]; pOp++){ |
643 freeP4(db, pOp->p4type, pOp->p4.p); | 721 freeP4(db, pOp->p4type, pOp->p4.p); |
644 #ifdef SQLITE_DEBUG | 722 #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS |
645 sqlite3DbFree(db, pOp->zComment); | 723 sqlite3DbFree(db, pOp->zComment); |
646 #endif | 724 #endif |
647 } | 725 } |
648 } | 726 } |
649 sqlite3DbFree(db, aOp); | 727 sqlite3DbFree(db, aOp); |
650 } | 728 } |
651 | 729 |
652 /* | 730 /* |
653 ** Link the SubProgram object passed as the second argument into the linked | 731 ** Link the SubProgram object passed as the second argument into the linked |
654 ** list at Vdbe.pSubProgram. This list is used to delete all sub-program | 732 ** list at Vdbe.pSubProgram. This list is used to delete all sub-program |
655 ** objects when the VM is no longer required. | 733 ** objects when the VM is no longer required. |
656 */ | 734 */ |
657 void sqlite3VdbeLinkSubProgram(Vdbe *pVdbe, SubProgram *p){ | 735 void sqlite3VdbeLinkSubProgram(Vdbe *pVdbe, SubProgram *p){ |
658 p->pNext = pVdbe->pProgram; | 736 p->pNext = pVdbe->pProgram; |
659 pVdbe->pProgram = p; | 737 pVdbe->pProgram = p; |
660 } | 738 } |
661 | 739 |
662 /* | 740 /* |
663 ** Change N opcodes starting at addr to No-ops. | 741 ** Change the opcode at addr into OP_Noop |
664 */ | 742 */ |
665 void sqlite3VdbeChangeToNoop(Vdbe *p, int addr, int N){ | 743 void sqlite3VdbeChangeToNoop(Vdbe *p, int addr){ |
666 if( p->aOp ){ | 744 if( addr<p->nOp ){ |
667 VdbeOp *pOp = &p->aOp[addr]; | 745 VdbeOp *pOp = &p->aOp[addr]; |
668 sqlite3 *db = p->db; | 746 sqlite3 *db = p->db; |
669 while( N-- ){ | 747 freeP4(db, pOp->p4type, pOp->p4.p); |
670 freeP4(db, pOp->p4type, pOp->p4.p); | 748 memset(pOp, 0, sizeof(pOp[0])); |
671 memset(pOp, 0, sizeof(pOp[0])); | 749 pOp->opcode = OP_Noop; |
672 pOp->opcode = OP_Noop; | 750 if( addr==p->nOp-1 ) p->nOp--; |
673 pOp++; | |
674 } | |
675 } | 751 } |
676 } | 752 } |
677 | 753 |
| 754 /* |
| 755 ** If the last opcode is "op" and it is not a jump destination, |
| 756 ** then remove it. Return true if and only if an opcode was removed. |
| 757 */ |
| 758 int sqlite3VdbeDeletePriorOpcode(Vdbe *p, u8 op){ |
| 759 if( (p->nOp-1)>(p->pParse->iFixedOp) && p->aOp[p->nOp-1].opcode==op ){ |
| 760 sqlite3VdbeChangeToNoop(p, p->nOp-1); |
| 761 return 1; |
| 762 }else{ |
| 763 return 0; |
| 764 } |
| 765 } |
| 766 |
678 /* | 767 /* |
679 ** Change the value of the P4 operand for a specific instruction. | 768 ** Change the value of the P4 operand for a specific instruction. |
680 ** This routine is useful when a large program is loaded from a | 769 ** This routine is useful when a large program is loaded from a |
681 ** static array using sqlite3VdbeAddOpList but we want to make a | 770 ** static array using sqlite3VdbeAddOpList but we want to make a |
682 ** few minor changes to the program. | 771 ** few minor changes to the program. |
683 ** | 772 ** |
684 ** If n>=0 then the P4 operand is dynamic, meaning that a copy of | 773 ** If n>=0 then the P4 operand is dynamic, meaning that a copy of |
685 ** the string is made into memory obtained from sqlite3_malloc(). | 774 ** the string is made into memory obtained from sqlite3_malloc(). |
686 ** A value of n==0 means copy bytes of zP4 up to and including the | 775 ** A value of n==0 means copy bytes of zP4 up to and including the |
687 ** first null byte. If n>0 then copy n+1 bytes of zP4. | 776 ** first null byte. If n>0 then copy n+1 bytes of zP4. |
688 ** | |
689 ** If n==P4_KEYINFO it means that zP4 is a pointer to a KeyInfo structure. | |
690 ** A copy is made of the KeyInfo structure into memory obtained from | |
691 ** sqlite3_malloc, to be freed when the Vdbe is finalized. | |
692 ** n==P4_KEYINFO_HANDOFF indicates that zP4 points to a KeyInfo structure | |
693 ** stored in memory that the caller has obtained from sqlite3_malloc. The | |
694 ** caller should not free the allocation, it will be freed when the Vdbe is | |
695 ** finalized. | |
696 ** | 777 ** |
697 ** Other values of n (P4_STATIC, P4_COLLSEQ etc.) indicate that zP4 points | 778 ** Other values of n (P4_STATIC, P4_COLLSEQ etc.) indicate that zP4 points |
698 ** to a string or structure that is guaranteed to exist for the lifetime of | 779 ** to a string or structure that is guaranteed to exist for the lifetime of |
699 ** the Vdbe. In these cases we can just copy the pointer. | 780 ** the Vdbe. In these cases we can just copy the pointer. |
700 ** | 781 ** |
701 ** If addr<0 then change P4 on the most recently inserted instruction. | 782 ** If addr<0 then change P4 on the most recently inserted instruction. |
702 */ | 783 */ |
703 void sqlite3VdbeChangeP4(Vdbe *p, int addr, const char *zP4, int n){ | 784 void sqlite3VdbeChangeP4(Vdbe *p, int addr, const char *zP4, int n){ |
704 Op *pOp; | 785 Op *pOp; |
705 sqlite3 *db; | 786 sqlite3 *db; |
706 assert( p!=0 ); | 787 assert( p!=0 ); |
707 db = p->db; | 788 db = p->db; |
708 assert( p->magic==VDBE_MAGIC_INIT ); | 789 assert( p->magic==VDBE_MAGIC_INIT ); |
709 if( p->aOp==0 || db->mallocFailed ){ | 790 if( p->aOp==0 || db->mallocFailed ){ |
710 if ( n!=P4_KEYINFO && n!=P4_VTAB ) { | 791 if( n!=P4_VTAB ){ |
711 freeP4(db, n, (void*)*(char**)&zP4); | 792 freeP4(db, n, (void*)*(char**)&zP4); |
712 } | 793 } |
713 return; | 794 return; |
714 } | 795 } |
715 assert( p->nOp>0 ); | 796 assert( p->nOp>0 ); |
716 assert( addr<p->nOp ); | 797 assert( addr<p->nOp ); |
717 if( addr<0 ){ | 798 if( addr<0 ){ |
718 addr = p->nOp - 1; | 799 addr = p->nOp - 1; |
719 } | 800 } |
720 pOp = &p->aOp[addr]; | 801 pOp = &p->aOp[addr]; |
| 802 assert( pOp->p4type==P4_NOTUSED |
| 803 || pOp->p4type==P4_INT32 |
| 804 || pOp->p4type==P4_KEYINFO ); |
721 freeP4(db, pOp->p4type, pOp->p4.p); | 805 freeP4(db, pOp->p4type, pOp->p4.p); |
722 pOp->p4.p = 0; | 806 pOp->p4.p = 0; |
723 if( n==P4_INT32 ){ | 807 if( n==P4_INT32 ){ |
724 /* Note: this cast is safe, because the origin data point was an int | 808 /* Note: this cast is safe, because the origin data point was an int |
725 ** that was cast to a (const char *). */ | 809 ** that was cast to a (const char *). */ |
726 pOp->p4.i = SQLITE_PTR_TO_INT(zP4); | 810 pOp->p4.i = SQLITE_PTR_TO_INT(zP4); |
727 pOp->p4type = P4_INT32; | 811 pOp->p4type = P4_INT32; |
728 }else if( zP4==0 ){ | 812 }else if( zP4==0 ){ |
729 pOp->p4.p = 0; | 813 pOp->p4.p = 0; |
730 pOp->p4type = P4_NOTUSED; | 814 pOp->p4type = P4_NOTUSED; |
731 }else if( n==P4_KEYINFO ){ | 815 }else if( n==P4_KEYINFO ){ |
732 KeyInfo *pKeyInfo; | |
733 int nField, nByte; | |
734 | |
735 nField = ((KeyInfo*)zP4)->nField; | |
736 nByte = sizeof(*pKeyInfo) + (nField-1)*sizeof(pKeyInfo->aColl[0]) + nField; | |
737 pKeyInfo = sqlite3DbMallocRaw(0, nByte); | |
738 pOp->p4.pKeyInfo = pKeyInfo; | |
739 if( pKeyInfo ){ | |
740 u8 *aSortOrder; | |
741 memcpy((char*)pKeyInfo, zP4, nByte - nField); | |
742 aSortOrder = pKeyInfo->aSortOrder; | |
743 if( aSortOrder ){ | |
744 pKeyInfo->aSortOrder = (unsigned char*)&pKeyInfo->aColl[nField]; | |
745 memcpy(pKeyInfo->aSortOrder, aSortOrder, nField); | |
746 } | |
747 pOp->p4type = P4_KEYINFO; | |
748 }else{ | |
749 p->db->mallocFailed = 1; | |
750 pOp->p4type = P4_NOTUSED; | |
751 } | |
752 }else if( n==P4_KEYINFO_HANDOFF ){ | |
753 pOp->p4.p = (void*)zP4; | 816 pOp->p4.p = (void*)zP4; |
754 pOp->p4type = P4_KEYINFO; | 817 pOp->p4type = P4_KEYINFO; |
755 }else if( n==P4_VTAB ){ | 818 }else if( n==P4_VTAB ){ |
756 pOp->p4.p = (void*)zP4; | 819 pOp->p4.p = (void*)zP4; |
757 pOp->p4type = P4_VTAB; | 820 pOp->p4type = P4_VTAB; |
758 sqlite3VtabLock((VTable *)zP4); | 821 sqlite3VtabLock((VTable *)zP4); |
759 assert( ((VTable *)zP4)->db==p->db ); | 822 assert( ((VTable *)zP4)->db==p->db ); |
760 }else if( n<0 ){ | 823 }else if( n<0 ){ |
761 pOp->p4.p = (void*)zP4; | 824 pOp->p4.p = (void*)zP4; |
762 pOp->p4type = (signed char)n; | 825 pOp->p4type = (signed char)n; |
763 }else{ | 826 }else{ |
764 if( n==0 ) n = sqlite3Strlen30(zP4); | 827 if( n==0 ) n = sqlite3Strlen30(zP4); |
765 pOp->p4.z = sqlite3DbStrNDup(p->db, zP4, n); | 828 pOp->p4.z = sqlite3DbStrNDup(p->db, zP4, n); |
766 pOp->p4type = P4_DYNAMIC; | 829 pOp->p4type = P4_DYNAMIC; |
767 } | 830 } |
768 } | 831 } |
769 | 832 |
770 #ifndef NDEBUG | |
771 /* | 833 /* |
772 ** Change the comment on the the most recently coded instruction. Or | 834 ** Set the P4 on the most recently added opcode to the KeyInfo for the |
| 835 ** index given. |
| 836 */ |
| 837 void sqlite3VdbeSetP4KeyInfo(Parse *pParse, Index *pIdx){ |
| 838 Vdbe *v = pParse->pVdbe; |
| 839 assert( v!=0 ); |
| 840 assert( pIdx!=0 ); |
| 841 sqlite3VdbeChangeP4(v, -1, (char*)sqlite3KeyInfoOfIndex(pParse, pIdx), |
| 842 P4_KEYINFO); |
| 843 } |
| 844 |
| 845 #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS |
| 846 /* |
| 847 ** Change the comment on the most recently coded instruction. Or |
773 ** insert a No-op and add the comment to that new instruction. This | 848 ** insert a No-op and add the comment to that new instruction. This |
774 ** makes the code easier to read during debugging. None of this happens | 849 ** makes the code easier to read during debugging. None of this happens |
775 ** in a production build. | 850 ** in a production build. |
776 */ | 851 */ |
777 void sqlite3VdbeComment(Vdbe *p, const char *zFormat, ...){ | 852 static void vdbeVComment(Vdbe *p, const char *zFormat, va_list ap){ |
778 va_list ap; | |
779 if( !p ) return; | |
780 assert( p->nOp>0 || p->aOp==0 ); | 853 assert( p->nOp>0 || p->aOp==0 ); |
781 assert( p->aOp==0 || p->aOp[p->nOp-1].zComment==0 || p->db->mallocFailed ); | 854 assert( p->aOp==0 || p->aOp[p->nOp-1].zComment==0 || p->db->mallocFailed ); |
782 if( p->nOp ){ | 855 if( p->nOp ){ |
783 char **pz = &p->aOp[p->nOp-1].zComment; | 856 assert( p->aOp ); |
| 857 sqlite3DbFree(p->db, p->aOp[p->nOp-1].zComment); |
| 858 p->aOp[p->nOp-1].zComment = sqlite3VMPrintf(p->db, zFormat, ap); |
| 859 } |
| 860 } |
| 861 void sqlite3VdbeComment(Vdbe *p, const char *zFormat, ...){ |
| 862 va_list ap; |
| 863 if( p ){ |
784 va_start(ap, zFormat); | 864 va_start(ap, zFormat); |
785 sqlite3DbFree(p->db, *pz); | 865 vdbeVComment(p, zFormat, ap); |
786 *pz = sqlite3VMPrintf(p->db, zFormat, ap); | |
787 va_end(ap); | 866 va_end(ap); |
788 } | 867 } |
789 } | 868 } |
790 void sqlite3VdbeNoopComment(Vdbe *p, const char *zFormat, ...){ | 869 void sqlite3VdbeNoopComment(Vdbe *p, const char *zFormat, ...){ |
791 va_list ap; | 870 va_list ap; |
792 if( !p ) return; | 871 if( p ){ |
793 sqlite3VdbeAddOp0(p, OP_Noop); | 872 sqlite3VdbeAddOp0(p, OP_Noop); |
794 assert( p->nOp>0 || p->aOp==0 ); | |
795 assert( p->aOp==0 || p->aOp[p->nOp-1].zComment==0 || p->db->mallocFailed ); | |
796 if( p->nOp ){ | |
797 char **pz = &p->aOp[p->nOp-1].zComment; | |
798 va_start(ap, zFormat); | 873 va_start(ap, zFormat); |
799 sqlite3DbFree(p->db, *pz); | 874 vdbeVComment(p, zFormat, ap); |
800 *pz = sqlite3VMPrintf(p->db, zFormat, ap); | |
801 va_end(ap); | 875 va_end(ap); |
802 } | 876 } |
803 } | 877 } |
804 #endif /* NDEBUG */ | 878 #endif /* NDEBUG */ |
805 | 879 |
| 880 #ifdef SQLITE_VDBE_COVERAGE |
| 881 /* |
| 882 ** Set the value if the iSrcLine field for the previously coded instruction. |
| 883 */ |
| 884 void sqlite3VdbeSetLineNumber(Vdbe *v, int iLine){ |
| 885 sqlite3VdbeGetOp(v,-1)->iSrcLine = iLine; |
| 886 } |
| 887 #endif /* SQLITE_VDBE_COVERAGE */ |
| 888 |
806 /* | 889 /* |
807 ** Return the opcode for a given address. If the address is -1, then | 890 ** Return the opcode for a given address. If the address is -1, then |
808 ** return the most recently inserted opcode. | 891 ** return the most recently inserted opcode. |
809 ** | 892 ** |
810 ** If a memory allocation error has occurred prior to the calling of this | 893 ** If a memory allocation error has occurred prior to the calling of this |
811 ** routine, then a pointer to a dummy VdbeOp will be returned. That opcode | 894 ** routine, then a pointer to a dummy VdbeOp will be returned. That opcode |
812 ** is readable but not writable, though it is cast to a writable value. | 895 ** is readable but not writable, though it is cast to a writable value. |
813 ** The return of a dummy opcode allows the call to continue functioning | 896 ** The return of a dummy opcode allows the call to continue functioning |
814 ** after a OOM fault without having to check to see if the return from | 897 ** after an OOM fault without having to check to see if the return from |
815 ** this routine is a valid pointer. But because the dummy.opcode is 0, | 898 ** this routine is a valid pointer. But because the dummy.opcode is 0, |
816 ** dummy will never be written to. This is verified by code inspection and | 899 ** dummy will never be written to. This is verified by code inspection and |
817 ** by running with Valgrind. | 900 ** by running with Valgrind. |
818 ** | |
819 ** About the #ifdef SQLITE_OMIT_TRACE: Normally, this routine is never called | |
820 ** unless p->nOp>0. This is because in the absense of SQLITE_OMIT_TRACE, | |
821 ** an OP_Trace instruction is always inserted by sqlite3VdbeGet() as soon as | |
822 ** a new VDBE is created. So we are free to set addr to p->nOp-1 without | |
823 ** having to double-check to make sure that the result is non-negative. But | |
824 ** if SQLITE_OMIT_TRACE is defined, the OP_Trace is omitted and we do need to | |
825 ** check the value of p->nOp-1 before continuing. | |
826 */ | 901 */ |
827 VdbeOp *sqlite3VdbeGetOp(Vdbe *p, int addr){ | 902 VdbeOp *sqlite3VdbeGetOp(Vdbe *p, int addr){ |
828 /* C89 specifies that the constant "dummy" will be initialized to all | 903 /* C89 specifies that the constant "dummy" will be initialized to all |
829 ** zeros, which is correct. MSVC generates a warning, nevertheless. */ | 904 ** zeros, which is correct. MSVC generates a warning, nevertheless. */ |
830 static const VdbeOp dummy; /* Ignore the MSVC warning about no initializer */ | 905 static VdbeOp dummy; /* Ignore the MSVC warning about no initializer */ |
831 assert( p->magic==VDBE_MAGIC_INIT ); | 906 assert( p->magic==VDBE_MAGIC_INIT ); |
832 if( addr<0 ){ | 907 if( addr<0 ){ |
833 #ifdef SQLITE_OMIT_TRACE | |
834 if( p->nOp==0 ) return (VdbeOp*)&dummy; | |
835 #endif | |
836 addr = p->nOp - 1; | 908 addr = p->nOp - 1; |
837 } | 909 } |
838 assert( (addr>=0 && addr<p->nOp) || p->db->mallocFailed ); | 910 assert( (addr>=0 && addr<p->nOp) || p->db->mallocFailed ); |
839 if( p->db->mallocFailed ){ | 911 if( p->db->mallocFailed ){ |
840 return (VdbeOp*)&dummy; | 912 return (VdbeOp*)&dummy; |
841 }else{ | 913 }else{ |
842 return &p->aOp[addr]; | 914 return &p->aOp[addr]; |
843 } | 915 } |
844 } | 916 } |
845 | 917 |
| 918 #if defined(SQLITE_ENABLE_EXPLAIN_COMMENTS) |
| 919 /* |
| 920 ** Return an integer value for one of the parameters to the opcode pOp |
| 921 ** determined by character c. |
| 922 */ |
| 923 static int translateP(char c, const Op *pOp){ |
| 924 if( c=='1' ) return pOp->p1; |
| 925 if( c=='2' ) return pOp->p2; |
| 926 if( c=='3' ) return pOp->p3; |
| 927 if( c=='4' ) return pOp->p4.i; |
| 928 return pOp->p5; |
| 929 } |
| 930 |
| 931 /* |
| 932 ** Compute a string for the "comment" field of a VDBE opcode listing. |
| 933 ** |
| 934 ** The Synopsis: field in comments in the vdbe.c source file gets converted |
| 935 ** to an extra string that is appended to the sqlite3OpcodeName(). In the |
| 936 ** absence of other comments, this synopsis becomes the comment on the opcode. |
| 937 ** Some translation occurs: |
| 938 ** |
| 939 ** "PX" -> "r[X]" |
| 940 ** "PX@PY" -> "r[X..X+Y-1]" or "r[x]" if y is 0 or 1 |
| 941 ** "PX@PY+1" -> "r[X..X+Y]" or "r[x]" if y is 0 |
| 942 ** "PY..PY" -> "r[X..Y]" or "r[x]" if y<=x |
| 943 */ |
| 944 static int displayComment( |
| 945 const Op *pOp, /* The opcode to be commented */ |
| 946 const char *zP4, /* Previously obtained value for P4 */ |
| 947 char *zTemp, /* Write result here */ |
| 948 int nTemp /* Space available in zTemp[] */ |
| 949 ){ |
| 950 const char *zOpName; |
| 951 const char *zSynopsis; |
| 952 int nOpName; |
| 953 int ii, jj; |
| 954 zOpName = sqlite3OpcodeName(pOp->opcode); |
| 955 nOpName = sqlite3Strlen30(zOpName); |
| 956 if( zOpName[nOpName+1] ){ |
| 957 int seenCom = 0; |
| 958 char c; |
| 959 zSynopsis = zOpName += nOpName + 1; |
| 960 for(ii=jj=0; jj<nTemp-1 && (c = zSynopsis[ii])!=0; ii++){ |
| 961 if( c=='P' ){ |
| 962 c = zSynopsis[++ii]; |
| 963 if( c=='4' ){ |
| 964 sqlite3_snprintf(nTemp-jj, zTemp+jj, "%s", zP4); |
| 965 }else if( c=='X' ){ |
| 966 sqlite3_snprintf(nTemp-jj, zTemp+jj, "%s", pOp->zComment); |
| 967 seenCom = 1; |
| 968 }else{ |
| 969 int v1 = translateP(c, pOp); |
| 970 int v2; |
| 971 sqlite3_snprintf(nTemp-jj, zTemp+jj, "%d", v1); |
| 972 if( strncmp(zSynopsis+ii+1, "@P", 2)==0 ){ |
| 973 ii += 3; |
| 974 jj += sqlite3Strlen30(zTemp+jj); |
| 975 v2 = translateP(zSynopsis[ii], pOp); |
| 976 if( strncmp(zSynopsis+ii+1,"+1",2)==0 ){ |
| 977 ii += 2; |
| 978 v2++; |
| 979 } |
| 980 if( v2>1 ){ |
| 981 sqlite3_snprintf(nTemp-jj, zTemp+jj, "..%d", v1+v2-1); |
| 982 } |
| 983 }else if( strncmp(zSynopsis+ii+1, "..P3", 4)==0 && pOp->p3==0 ){ |
| 984 ii += 4; |
| 985 } |
| 986 } |
| 987 jj += sqlite3Strlen30(zTemp+jj); |
| 988 }else{ |
| 989 zTemp[jj++] = c; |
| 990 } |
| 991 } |
| 992 if( !seenCom && jj<nTemp-5 && pOp->zComment ){ |
| 993 sqlite3_snprintf(nTemp-jj, zTemp+jj, "; %s", pOp->zComment); |
| 994 jj += sqlite3Strlen30(zTemp+jj); |
| 995 } |
| 996 if( jj<nTemp ) zTemp[jj] = 0; |
| 997 }else if( pOp->zComment ){ |
| 998 sqlite3_snprintf(nTemp, zTemp, "%s", pOp->zComment); |
| 999 jj = sqlite3Strlen30(zTemp); |
| 1000 }else{ |
| 1001 zTemp[0] = 0; |
| 1002 jj = 0; |
| 1003 } |
| 1004 return jj; |
| 1005 } |
| 1006 #endif /* SQLITE_DEBUG */ |
| 1007 |
| 1008 |
846 #if !defined(SQLITE_OMIT_EXPLAIN) || !defined(NDEBUG) \ | 1009 #if !defined(SQLITE_OMIT_EXPLAIN) || !defined(NDEBUG) \ |
847 || defined(VDBE_PROFILE) || defined(SQLITE_DEBUG) | 1010 || defined(VDBE_PROFILE) || defined(SQLITE_DEBUG) |
848 /* | 1011 /* |
849 ** Compute a string that describes the P4 parameter for an opcode. | 1012 ** Compute a string that describes the P4 parameter for an opcode. |
850 ** Use zTemp for any required temporary buffer space. | 1013 ** Use zTemp for any required temporary buffer space. |
851 */ | 1014 */ |
852 static char *displayP4(Op *pOp, char *zTemp, int nTemp){ | 1015 static char *displayP4(Op *pOp, char *zTemp, int nTemp){ |
853 char *zP4 = zTemp; | 1016 char *zP4 = zTemp; |
854 assert( nTemp>=20 ); | 1017 assert( nTemp>=20 ); |
855 switch( pOp->p4type ){ | 1018 switch( pOp->p4type ){ |
856 case P4_KEYINFO_STATIC: | |
857 case P4_KEYINFO: { | 1019 case P4_KEYINFO: { |
858 int i, j; | 1020 int i, j; |
859 KeyInfo *pKeyInfo = pOp->p4.pKeyInfo; | 1021 KeyInfo *pKeyInfo = pOp->p4.pKeyInfo; |
860 sqlite3_snprintf(nTemp, zTemp, "keyinfo(%d", pKeyInfo->nField); | 1022 assert( pKeyInfo->aSortOrder!=0 ); |
| 1023 sqlite3_snprintf(nTemp, zTemp, "k(%d", pKeyInfo->nField); |
861 i = sqlite3Strlen30(zTemp); | 1024 i = sqlite3Strlen30(zTemp); |
862 for(j=0; j<pKeyInfo->nField; j++){ | 1025 for(j=0; j<pKeyInfo->nField; j++){ |
863 CollSeq *pColl = pKeyInfo->aColl[j]; | 1026 CollSeq *pColl = pKeyInfo->aColl[j]; |
864 if( pColl ){ | 1027 const char *zColl = pColl ? pColl->zName : "nil"; |
865 int n = sqlite3Strlen30(pColl->zName); | 1028 int n = sqlite3Strlen30(zColl); |
866 if( i+n>nTemp-6 ){ | 1029 if( n==6 && memcmp(zColl,"BINARY",6)==0 ){ |
867 memcpy(&zTemp[i],",...",4); | 1030 zColl = "B"; |
868 break; | 1031 n = 1; |
869 } | |
870 zTemp[i++] = ','; | |
871 if( pKeyInfo->aSortOrder && pKeyInfo->aSortOrder[j] ){ | |
872 zTemp[i++] = '-'; | |
873 } | |
874 memcpy(&zTemp[i], pColl->zName,n+1); | |
875 i += n; | |
876 }else if( i+4<nTemp-6 ){ | |
877 memcpy(&zTemp[i],",nil",4); | |
878 i += 4; | |
879 } | 1032 } |
| 1033 if( i+n>nTemp-6 ){ |
| 1034 memcpy(&zTemp[i],",...",4); |
| 1035 break; |
| 1036 } |
| 1037 zTemp[i++] = ','; |
| 1038 if( pKeyInfo->aSortOrder[j] ){ |
| 1039 zTemp[i++] = '-'; |
| 1040 } |
| 1041 memcpy(&zTemp[i], zColl, n+1); |
| 1042 i += n; |
880 } | 1043 } |
881 zTemp[i++] = ')'; | 1044 zTemp[i++] = ')'; |
882 zTemp[i] = 0; | 1045 zTemp[i] = 0; |
883 assert( i<nTemp ); | 1046 assert( i<nTemp ); |
884 break; | 1047 break; |
885 } | 1048 } |
886 case P4_COLLSEQ: { | 1049 case P4_COLLSEQ: { |
887 CollSeq *pColl = pOp->p4.pColl; | 1050 CollSeq *pColl = pOp->p4.pColl; |
888 sqlite3_snprintf(nTemp, zTemp, "collseq(%.20s)", pColl->zName); | 1051 sqlite3_snprintf(nTemp, zTemp, "(%.20s)", pColl->zName); |
889 break; | 1052 break; |
890 } | 1053 } |
891 case P4_FUNCDEF: { | 1054 case P4_FUNCDEF: { |
892 FuncDef *pDef = pOp->p4.pFunc; | 1055 FuncDef *pDef = pOp->p4.pFunc; |
893 sqlite3_snprintf(nTemp, zTemp, "%s(%d)", pDef->zName, pDef->nArg); | 1056 sqlite3_snprintf(nTemp, zTemp, "%s(%d)", pDef->zName, pDef->nArg); |
894 break; | 1057 break; |
895 } | 1058 } |
896 case P4_INT64: { | 1059 case P4_INT64: { |
897 sqlite3_snprintf(nTemp, zTemp, "%lld", *pOp->p4.pI64); | 1060 sqlite3_snprintf(nTemp, zTemp, "%lld", *pOp->p4.pI64); |
898 break; | 1061 break; |
899 } | 1062 } |
900 case P4_INT32: { | 1063 case P4_INT32: { |
901 sqlite3_snprintf(nTemp, zTemp, "%d", pOp->p4.i); | 1064 sqlite3_snprintf(nTemp, zTemp, "%d", pOp->p4.i); |
902 break; | 1065 break; |
903 } | 1066 } |
904 case P4_REAL: { | 1067 case P4_REAL: { |
905 sqlite3_snprintf(nTemp, zTemp, "%.16g", *pOp->p4.pReal); | 1068 sqlite3_snprintf(nTemp, zTemp, "%.16g", *pOp->p4.pReal); |
906 break; | 1069 break; |
907 } | 1070 } |
908 case P4_MEM: { | 1071 case P4_MEM: { |
909 Mem *pMem = pOp->p4.pMem; | 1072 Mem *pMem = pOp->p4.pMem; |
910 assert( (pMem->flags & MEM_Null)==0 ); | |
911 if( pMem->flags & MEM_Str ){ | 1073 if( pMem->flags & MEM_Str ){ |
912 zP4 = pMem->z; | 1074 zP4 = pMem->z; |
913 }else if( pMem->flags & MEM_Int ){ | 1075 }else if( pMem->flags & MEM_Int ){ |
914 sqlite3_snprintf(nTemp, zTemp, "%lld", pMem->u.i); | 1076 sqlite3_snprintf(nTemp, zTemp, "%lld", pMem->u.i); |
915 }else if( pMem->flags & MEM_Real ){ | 1077 }else if( pMem->flags & MEM_Real ){ |
916 sqlite3_snprintf(nTemp, zTemp, "%.16g", pMem->r); | 1078 sqlite3_snprintf(nTemp, zTemp, "%.16g", pMem->u.r); |
| 1079 }else if( pMem->flags & MEM_Null ){ |
| 1080 sqlite3_snprintf(nTemp, zTemp, "NULL"); |
917 }else{ | 1081 }else{ |
918 assert( pMem->flags & MEM_Blob ); | 1082 assert( pMem->flags & MEM_Blob ); |
919 zP4 = "(blob)"; | 1083 zP4 = "(blob)"; |
920 } | 1084 } |
921 break; | 1085 break; |
922 } | 1086 } |
923 #ifndef SQLITE_OMIT_VIRTUALTABLE | 1087 #ifndef SQLITE_OMIT_VIRTUALTABLE |
924 case P4_VTAB: { | 1088 case P4_VTAB: { |
925 sqlite3_vtab *pVtab = pOp->p4.pVtab->pVtab; | 1089 sqlite3_vtab *pVtab = pOp->p4.pVtab->pVtab; |
926 sqlite3_snprintf(nTemp, zTemp, "vtab:%p:%p", pVtab, pVtab->pModule); | 1090 sqlite3_snprintf(nTemp, zTemp, "vtab:%p:%p", pVtab, pVtab->pModule); |
927 break; | 1091 break; |
928 } | 1092 } |
929 #endif | 1093 #endif |
930 case P4_INTARRAY: { | 1094 case P4_INTARRAY: { |
931 sqlite3_snprintf(nTemp, zTemp, "intarray"); | 1095 sqlite3_snprintf(nTemp, zTemp, "intarray"); |
932 break; | 1096 break; |
933 } | 1097 } |
934 case P4_SUBPROGRAM: { | 1098 case P4_SUBPROGRAM: { |
935 sqlite3_snprintf(nTemp, zTemp, "program"); | 1099 sqlite3_snprintf(nTemp, zTemp, "program"); |
936 break; | 1100 break; |
937 } | 1101 } |
| 1102 case P4_ADVANCE: { |
| 1103 zTemp[0] = 0; |
| 1104 break; |
| 1105 } |
938 default: { | 1106 default: { |
939 zP4 = pOp->p4.z; | 1107 zP4 = pOp->p4.z; |
940 if( zP4==0 ){ | 1108 if( zP4==0 ){ |
941 zP4 = zTemp; | 1109 zP4 = zTemp; |
942 zTemp[0] = 0; | 1110 zTemp[0] = 0; |
943 } | 1111 } |
944 } | 1112 } |
945 } | 1113 } |
946 assert( zP4!=0 ); | 1114 assert( zP4!=0 ); |
947 return zP4; | 1115 return zP4; |
948 } | 1116 } |
949 #endif | 1117 #endif |
950 | 1118 |
951 /* | 1119 /* |
952 ** Declare to the Vdbe that the BTree object at db->aDb[i] is used. | 1120 ** Declare to the Vdbe that the BTree object at db->aDb[i] is used. |
953 ** | 1121 ** |
954 ** The prepared statements need to know in advance the complete set of | 1122 ** The prepared statements need to know in advance the complete set of |
955 ** attached databases that they will be using. A mask of these databases | 1123 ** attached databases that will be use. A mask of these databases |
956 ** is maintained in p->btreeMask and is used for locking and other purposes. | 1124 ** is maintained in p->btreeMask. The p->lockMask value is the subset of |
| 1125 ** p->btreeMask of databases that will require a lock. |
957 */ | 1126 */ |
958 void sqlite3VdbeUsesBtree(Vdbe *p, int i){ | 1127 void sqlite3VdbeUsesBtree(Vdbe *p, int i){ |
959 assert( i>=0 && i<p->db->nDb && i<(int)sizeof(yDbMask)*8 ); | 1128 assert( i>=0 && i<p->db->nDb && i<(int)sizeof(yDbMask)*8 ); |
960 assert( i<(int)sizeof(p->btreeMask)*8 ); | 1129 assert( i<(int)sizeof(p->btreeMask)*8 ); |
961 p->btreeMask |= ((yDbMask)1)<<i; | 1130 DbMaskSet(p->btreeMask, i); |
962 if( i!=1 && sqlite3BtreeSharable(p->db->aDb[i].pBt) ){ | 1131 if( i!=1 && sqlite3BtreeSharable(p->db->aDb[i].pBt) ){ |
963 p->lockMask |= ((yDbMask)1)<<i; | 1132 DbMaskSet(p->lockMask, i); |
964 } | 1133 } |
965 } | 1134 } |
966 | 1135 |
967 #if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE>0 | 1136 #if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE>0 |
968 /* | 1137 /* |
969 ** If SQLite is compiled to support shared-cache mode and to be threadsafe, | 1138 ** If SQLite is compiled to support shared-cache mode and to be threadsafe, |
970 ** this routine obtains the mutex associated with each BtShared structure | 1139 ** this routine obtains the mutex associated with each BtShared structure |
971 ** that may be accessed by the VM passed as an argument. In doing so it also | 1140 ** that may be accessed by the VM passed as an argument. In doing so it also |
972 ** sets the BtShared.db member of each of the BtShared structures, ensuring | 1141 ** sets the BtShared.db member of each of the BtShared structures, ensuring |
973 ** that the correct busy-handler callback is invoked if required. | 1142 ** that the correct busy-handler callback is invoked if required. |
974 ** | 1143 ** |
975 ** If SQLite is not threadsafe but does support shared-cache mode, then | 1144 ** If SQLite is not threadsafe but does support shared-cache mode, then |
976 ** sqlite3BtreeEnter() is invoked to set the BtShared.db variables | 1145 ** sqlite3BtreeEnter() is invoked to set the BtShared.db variables |
977 ** of all of BtShared structures accessible via the database handle | 1146 ** of all of BtShared structures accessible via the database handle |
978 ** associated with the VM. | 1147 ** associated with the VM. |
979 ** | 1148 ** |
980 ** If SQLite is not threadsafe and does not support shared-cache mode, this | 1149 ** If SQLite is not threadsafe and does not support shared-cache mode, this |
981 ** function is a no-op. | 1150 ** function is a no-op. |
982 ** | 1151 ** |
983 ** The p->btreeMask field is a bitmask of all btrees that the prepared | 1152 ** The p->btreeMask field is a bitmask of all btrees that the prepared |
984 ** statement p will ever use. Let N be the number of bits in p->btreeMask | 1153 ** statement p will ever use. Let N be the number of bits in p->btreeMask |
985 ** corresponding to btrees that use shared cache. Then the runtime of | 1154 ** corresponding to btrees that use shared cache. Then the runtime of |
986 ** this routine is N*N. But as N is rarely more than 1, this should not | 1155 ** this routine is N*N. But as N is rarely more than 1, this should not |
987 ** be a problem. | 1156 ** be a problem. |
988 */ | 1157 */ |
989 void sqlite3VdbeEnter(Vdbe *p){ | 1158 void sqlite3VdbeEnter(Vdbe *p){ |
990 int i; | 1159 int i; |
991 yDbMask mask; | |
992 sqlite3 *db; | 1160 sqlite3 *db; |
993 Db *aDb; | 1161 Db *aDb; |
994 int nDb; | 1162 int nDb; |
995 if( p->lockMask==0 ) return; /* The common case */ | 1163 if( DbMaskAllZero(p->lockMask) ) return; /* The common case */ |
996 db = p->db; | 1164 db = p->db; |
997 aDb = db->aDb; | 1165 aDb = db->aDb; |
998 nDb = db->nDb; | 1166 nDb = db->nDb; |
999 for(i=0, mask=1; i<nDb; i++, mask += mask){ | 1167 for(i=0; i<nDb; i++){ |
1000 if( i!=1 && (mask & p->lockMask)!=0 && ALWAYS(aDb[i].pBt!=0) ){ | 1168 if( i!=1 && DbMaskTest(p->lockMask,i) && ALWAYS(aDb[i].pBt!=0) ){ |
1001 sqlite3BtreeEnter(aDb[i].pBt); | 1169 sqlite3BtreeEnter(aDb[i].pBt); |
1002 } | 1170 } |
1003 } | 1171 } |
1004 } | 1172 } |
1005 #endif | 1173 #endif |
1006 | 1174 |
1007 #if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE>0 | 1175 #if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE>0 |
1008 /* | 1176 /* |
1009 ** Unlock all of the btrees previously locked by a call to sqlite3VdbeEnter(). | 1177 ** Unlock all of the btrees previously locked by a call to sqlite3VdbeEnter(). |
1010 */ | 1178 */ |
1011 void sqlite3VdbeLeave(Vdbe *p){ | 1179 void sqlite3VdbeLeave(Vdbe *p){ |
1012 int i; | 1180 int i; |
1013 yDbMask mask; | |
1014 sqlite3 *db; | 1181 sqlite3 *db; |
1015 Db *aDb; | 1182 Db *aDb; |
1016 int nDb; | 1183 int nDb; |
1017 if( p->lockMask==0 ) return; /* The common case */ | 1184 if( DbMaskAllZero(p->lockMask) ) return; /* The common case */ |
1018 db = p->db; | 1185 db = p->db; |
1019 aDb = db->aDb; | 1186 aDb = db->aDb; |
1020 nDb = db->nDb; | 1187 nDb = db->nDb; |
1021 for(i=0, mask=1; i<nDb; i++, mask += mask){ | 1188 for(i=0; i<nDb; i++){ |
1022 if( i!=1 && (mask & p->lockMask)!=0 && ALWAYS(aDb[i].pBt!=0) ){ | 1189 if( i!=1 && DbMaskTest(p->lockMask,i) && ALWAYS(aDb[i].pBt!=0) ){ |
1023 sqlite3BtreeLeave(aDb[i].pBt); | 1190 sqlite3BtreeLeave(aDb[i].pBt); |
1024 } | 1191 } |
1025 } | 1192 } |
1026 } | 1193 } |
1027 #endif | 1194 #endif |
1028 | 1195 |
1029 #if defined(VDBE_PROFILE) || defined(SQLITE_DEBUG) | 1196 #if defined(VDBE_PROFILE) || defined(SQLITE_DEBUG) |
1030 /* | 1197 /* |
1031 ** Print a single opcode. This routine is used for debugging only. | 1198 ** Print a single opcode. This routine is used for debugging only. |
1032 */ | 1199 */ |
1033 void sqlite3VdbePrintOp(FILE *pOut, int pc, Op *pOp){ | 1200 void sqlite3VdbePrintOp(FILE *pOut, int pc, Op *pOp){ |
1034 char *zP4; | 1201 char *zP4; |
1035 char zPtr[50]; | 1202 char zPtr[50]; |
1036 static const char *zFormat1 = "%4d %-13s %4d %4d %4d %-4s %.2X %s\n"; | 1203 char zCom[100]; |
| 1204 static const char *zFormat1 = "%4d %-13s %4d %4d %4d %-13s %.2X %s\n"; |
1037 if( pOut==0 ) pOut = stdout; | 1205 if( pOut==0 ) pOut = stdout; |
1038 zP4 = displayP4(pOp, zPtr, sizeof(zPtr)); | 1206 zP4 = displayP4(pOp, zPtr, sizeof(zPtr)); |
| 1207 #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS |
| 1208 displayComment(pOp, zP4, zCom, sizeof(zCom)); |
| 1209 #else |
| 1210 zCom[0] = 0; |
| 1211 #endif |
| 1212 /* NB: The sqlite3OpcodeName() function is implemented by code created |
| 1213 ** by the mkopcodeh.awk and mkopcodec.awk scripts which extract the |
| 1214 ** information from the vdbe.c source text */ |
1039 fprintf(pOut, zFormat1, pc, | 1215 fprintf(pOut, zFormat1, pc, |
1040 sqlite3OpcodeName(pOp->opcode), pOp->p1, pOp->p2, pOp->p3, zP4, pOp->p5, | 1216 sqlite3OpcodeName(pOp->opcode), pOp->p1, pOp->p2, pOp->p3, zP4, pOp->p5, |
1041 #ifdef SQLITE_DEBUG | 1217 zCom |
1042 pOp->zComment ? pOp->zComment : "" | |
1043 #else | |
1044 "" | |
1045 #endif | |
1046 ); | 1218 ); |
1047 fflush(pOut); | 1219 fflush(pOut); |
1048 } | 1220 } |
1049 #endif | 1221 #endif |
1050 | 1222 |
1051 /* | 1223 /* |
1052 ** Release an array of N Mem elements | 1224 ** Release an array of N Mem elements |
1053 */ | 1225 */ |
1054 static void releaseMemArray(Mem *p, int N){ | 1226 static void releaseMemArray(Mem *p, int N){ |
1055 if( p && N ){ | 1227 if( p && N ){ |
1056 Mem *pEnd; | 1228 Mem *pEnd = &p[N]; |
1057 sqlite3 *db = p->db; | 1229 sqlite3 *db = p->db; |
1058 u8 malloc_failed = db->mallocFailed; | 1230 u8 malloc_failed = db->mallocFailed; |
1059 if( db->pnBytesFreed ){ | 1231 if( db->pnBytesFreed ){ |
1060 for(pEnd=&p[N]; p<pEnd; p++){ | 1232 do{ |
1061 sqlite3DbFree(db, p->zMalloc); | 1233 if( p->szMalloc ) sqlite3DbFree(db, p->zMalloc); |
1062 } | 1234 }while( (++p)<pEnd ); |
1063 return; | 1235 return; |
1064 } | 1236 } |
1065 for(pEnd=&p[N]; p<pEnd; p++){ | 1237 do{ |
1066 assert( (&p[1])==pEnd || p[0].db==p[1].db ); | 1238 assert( (&p[1])==pEnd || p[0].db==p[1].db ); |
| 1239 assert( sqlite3VdbeCheckMemInvariants(p) ); |
1067 | 1240 |
1068 /* This block is really an inlined version of sqlite3VdbeMemRelease() | 1241 /* This block is really an inlined version of sqlite3VdbeMemRelease() |
1069 ** that takes advantage of the fact that the memory cell value is | 1242 ** that takes advantage of the fact that the memory cell value is |
1070 ** being set to NULL after releasing any dynamic resources. | 1243 ** being set to NULL after releasing any dynamic resources. |
1071 ** | 1244 ** |
1072 ** The justification for duplicating code is that according to | 1245 ** The justification for duplicating code is that according to |
1073 ** callgrind, this causes a certain test case to hit the CPU 4.7 | 1246 ** callgrind, this causes a certain test case to hit the CPU 4.7 |
1074 ** percent less (x86 linux, gcc version 4.1.2, -O6) than if | 1247 ** percent less (x86 linux, gcc version 4.1.2, -O6) than if |
1075 ** sqlite3MemRelease() were called from here. With -O2, this jumps | 1248 ** sqlite3MemRelease() were called from here. With -O2, this jumps |
1076 ** to 6.6 percent. The test case is inserting 1000 rows into a table | 1249 ** to 6.6 percent. The test case is inserting 1000 rows into a table |
1077 ** with no indexes using a single prepared INSERT statement, bind() | 1250 ** with no indexes using a single prepared INSERT statement, bind() |
1078 ** and reset(). Inserts are grouped into a transaction. | 1251 ** and reset(). Inserts are grouped into a transaction. |
1079 */ | 1252 */ |
| 1253 testcase( p->flags & MEM_Agg ); |
| 1254 testcase( p->flags & MEM_Dyn ); |
| 1255 testcase( p->flags & MEM_Frame ); |
| 1256 testcase( p->flags & MEM_RowSet ); |
1080 if( p->flags&(MEM_Agg|MEM_Dyn|MEM_Frame|MEM_RowSet) ){ | 1257 if( p->flags&(MEM_Agg|MEM_Dyn|MEM_Frame|MEM_RowSet) ){ |
1081 sqlite3VdbeMemRelease(p); | 1258 sqlite3VdbeMemRelease(p); |
1082 }else if( p->zMalloc ){ | 1259 }else if( p->szMalloc ){ |
1083 sqlite3DbFree(db, p->zMalloc); | 1260 sqlite3DbFree(db, p->zMalloc); |
1084 p->zMalloc = 0; | 1261 p->szMalloc = 0; |
1085 } | 1262 } |
1086 | 1263 |
1087 p->flags = MEM_Null; | 1264 p->flags = MEM_Undefined; |
1088 } | 1265 }while( (++p)<pEnd ); |
1089 db->mallocFailed = malloc_failed; | 1266 db->mallocFailed = malloc_failed; |
1090 } | 1267 } |
1091 } | 1268 } |
1092 | 1269 |
1093 /* | 1270 /* |
1094 ** Delete a VdbeFrame object and its contents. VdbeFrame objects are | 1271 ** Delete a VdbeFrame object and its contents. VdbeFrame objects are |
1095 ** allocated by the OP_Program opcode in sqlite3VdbeExec(). | 1272 ** allocated by the OP_Program opcode in sqlite3VdbeExec(). |
1096 */ | 1273 */ |
1097 void sqlite3VdbeFrameDelete(VdbeFrame *p){ | 1274 void sqlite3VdbeFrameDelete(VdbeFrame *p){ |
1098 int i; | 1275 int i; |
(...skipping 25 matching lines...) Expand all Loading... |
1124 int sqlite3VdbeList( | 1301 int sqlite3VdbeList( |
1125 Vdbe *p /* The VDBE */ | 1302 Vdbe *p /* The VDBE */ |
1126 ){ | 1303 ){ |
1127 int nRow; /* Stop when row count reaches this */ | 1304 int nRow; /* Stop when row count reaches this */ |
1128 int nSub = 0; /* Number of sub-vdbes seen so far */ | 1305 int nSub = 0; /* Number of sub-vdbes seen so far */ |
1129 SubProgram **apSub = 0; /* Array of sub-vdbes */ | 1306 SubProgram **apSub = 0; /* Array of sub-vdbes */ |
1130 Mem *pSub = 0; /* Memory cell hold array of subprogs */ | 1307 Mem *pSub = 0; /* Memory cell hold array of subprogs */ |
1131 sqlite3 *db = p->db; /* The database connection */ | 1308 sqlite3 *db = p->db; /* The database connection */ |
1132 int i; /* Loop counter */ | 1309 int i; /* Loop counter */ |
1133 int rc = SQLITE_OK; /* Return code */ | 1310 int rc = SQLITE_OK; /* Return code */ |
1134 Mem *pMem = p->pResultSet = &p->aMem[1]; /* First Mem of result set */ | 1311 Mem *pMem = &p->aMem[1]; /* First Mem of result set */ |
1135 | 1312 |
1136 assert( p->explain ); | 1313 assert( p->explain ); |
1137 assert( p->magic==VDBE_MAGIC_RUN ); | 1314 assert( p->magic==VDBE_MAGIC_RUN ); |
1138 assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY || p->rc==SQLITE_NOMEM ); | 1315 assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY || p->rc==SQLITE_NOMEM ); |
1139 | 1316 |
1140 /* Even though this opcode does not use dynamic strings for | 1317 /* Even though this opcode does not use dynamic strings for |
1141 ** the result, result columns may become dynamic if the user calls | 1318 ** the result, result columns may become dynamic if the user calls |
1142 ** sqlite3_column_text16(), causing a translation to UTF-16 encoding. | 1319 ** sqlite3_column_text16(), causing a translation to UTF-16 encoding. |
1143 */ | 1320 */ |
1144 releaseMemArray(pMem, 8); | 1321 releaseMemArray(pMem, 8); |
| 1322 p->pResultSet = 0; |
1145 | 1323 |
1146 if( p->rc==SQLITE_NOMEM ){ | 1324 if( p->rc==SQLITE_NOMEM ){ |
1147 /* This happens if a malloc() inside a call to sqlite3_column_text() or | 1325 /* This happens if a malloc() inside a call to sqlite3_column_text() or |
1148 ** sqlite3_column_text16() failed. */ | 1326 ** sqlite3_column_text16() failed. */ |
1149 db->mallocFailed = 1; | 1327 db->mallocFailed = 1; |
1150 return SQLITE_ERROR; | 1328 return SQLITE_ERROR; |
1151 } | 1329 } |
1152 | 1330 |
1153 /* When the number of output rows reaches nRow, that means the | 1331 /* When the number of output rows reaches nRow, that means the |
1154 ** listing has finished and sqlite3_step() should return SQLITE_DONE. | 1332 ** listing has finished and sqlite3_step() should return SQLITE_DONE. |
(...skipping 25 matching lines...) Expand all Loading... |
1180 i = p->pc++; | 1358 i = p->pc++; |
1181 }while( i<nRow && p->explain==2 && p->aOp[i].opcode!=OP_Explain ); | 1359 }while( i<nRow && p->explain==2 && p->aOp[i].opcode!=OP_Explain ); |
1182 if( i>=nRow ){ | 1360 if( i>=nRow ){ |
1183 p->rc = SQLITE_OK; | 1361 p->rc = SQLITE_OK; |
1184 rc = SQLITE_DONE; | 1362 rc = SQLITE_DONE; |
1185 }else if( db->u1.isInterrupted ){ | 1363 }else if( db->u1.isInterrupted ){ |
1186 p->rc = SQLITE_INTERRUPT; | 1364 p->rc = SQLITE_INTERRUPT; |
1187 rc = SQLITE_ERROR; | 1365 rc = SQLITE_ERROR; |
1188 sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3ErrStr(p->rc)); | 1366 sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3ErrStr(p->rc)); |
1189 }else{ | 1367 }else{ |
1190 char *z; | 1368 char *zP4; |
1191 Op *pOp; | 1369 Op *pOp; |
1192 if( i<p->nOp ){ | 1370 if( i<p->nOp ){ |
1193 /* The output line number is small enough that we are still in the | 1371 /* The output line number is small enough that we are still in the |
1194 ** main program. */ | 1372 ** main program. */ |
1195 pOp = &p->aOp[i]; | 1373 pOp = &p->aOp[i]; |
1196 }else{ | 1374 }else{ |
1197 /* We are currently listing subprograms. Figure out which one and | 1375 /* We are currently listing subprograms. Figure out which one and |
1198 ** pick up the appropriate opcode. */ | 1376 ** pick up the appropriate opcode. */ |
1199 int j; | 1377 int j; |
1200 i -= p->nOp; | 1378 i -= p->nOp; |
1201 for(j=0; i>=apSub[j]->nOp; j++){ | 1379 for(j=0; i>=apSub[j]->nOp; j++){ |
1202 i -= apSub[j]->nOp; | 1380 i -= apSub[j]->nOp; |
1203 } | 1381 } |
1204 pOp = &apSub[j]->aOp[i]; | 1382 pOp = &apSub[j]->aOp[i]; |
1205 } | 1383 } |
1206 if( p->explain==1 ){ | 1384 if( p->explain==1 ){ |
1207 pMem->flags = MEM_Int; | 1385 pMem->flags = MEM_Int; |
1208 pMem->type = SQLITE_INTEGER; | |
1209 pMem->u.i = i; /* Program counter */ | 1386 pMem->u.i = i; /* Program counter */ |
1210 pMem++; | 1387 pMem++; |
1211 | 1388 |
1212 pMem->flags = MEM_Static|MEM_Str|MEM_Term; | 1389 pMem->flags = MEM_Static|MEM_Str|MEM_Term; |
1213 pMem->z = (char*)sqlite3OpcodeName(pOp->opcode); /* Opcode */ | 1390 pMem->z = (char*)sqlite3OpcodeName(pOp->opcode); /* Opcode */ |
1214 assert( pMem->z!=0 ); | 1391 assert( pMem->z!=0 ); |
1215 pMem->n = sqlite3Strlen30(pMem->z); | 1392 pMem->n = sqlite3Strlen30(pMem->z); |
1216 pMem->type = SQLITE_TEXT; | |
1217 pMem->enc = SQLITE_UTF8; | 1393 pMem->enc = SQLITE_UTF8; |
1218 pMem++; | 1394 pMem++; |
1219 | 1395 |
1220 /* When an OP_Program opcode is encounter (the only opcode that has | 1396 /* When an OP_Program opcode is encounter (the only opcode that has |
1221 ** a P4_SUBPROGRAM argument), expand the size of the array of subprograms | 1397 ** a P4_SUBPROGRAM argument), expand the size of the array of subprograms |
1222 ** kept in p->aMem[9].z to hold the new program - assuming this subprogram | 1398 ** kept in p->aMem[9].z to hold the new program - assuming this subprogram |
1223 ** has not already been seen. | 1399 ** has not already been seen. |
1224 */ | 1400 */ |
1225 if( pOp->p4type==P4_SUBPROGRAM ){ | 1401 if( pOp->p4type==P4_SUBPROGRAM ){ |
1226 int nByte = (nSub+1)*sizeof(SubProgram*); | 1402 int nByte = (nSub+1)*sizeof(SubProgram*); |
1227 int j; | 1403 int j; |
1228 for(j=0; j<nSub; j++){ | 1404 for(j=0; j<nSub; j++){ |
1229 if( apSub[j]==pOp->p4.pProgram ) break; | 1405 if( apSub[j]==pOp->p4.pProgram ) break; |
1230 } | 1406 } |
1231 if( j==nSub && SQLITE_OK==sqlite3VdbeMemGrow(pSub, nByte, 1) ){ | 1407 if( j==nSub && SQLITE_OK==sqlite3VdbeMemGrow(pSub, nByte, nSub!=0) ){ |
1232 apSub = (SubProgram **)pSub->z; | 1408 apSub = (SubProgram **)pSub->z; |
1233 apSub[nSub++] = pOp->p4.pProgram; | 1409 apSub[nSub++] = pOp->p4.pProgram; |
1234 pSub->flags |= MEM_Blob; | 1410 pSub->flags |= MEM_Blob; |
1235 pSub->n = nSub*sizeof(SubProgram*); | 1411 pSub->n = nSub*sizeof(SubProgram*); |
1236 } | 1412 } |
1237 } | 1413 } |
1238 } | 1414 } |
1239 | 1415 |
1240 pMem->flags = MEM_Int; | 1416 pMem->flags = MEM_Int; |
1241 pMem->u.i = pOp->p1; /* P1 */ | 1417 pMem->u.i = pOp->p1; /* P1 */ |
1242 pMem->type = SQLITE_INTEGER; | |
1243 pMem++; | 1418 pMem++; |
1244 | 1419 |
1245 pMem->flags = MEM_Int; | 1420 pMem->flags = MEM_Int; |
1246 pMem->u.i = pOp->p2; /* P2 */ | 1421 pMem->u.i = pOp->p2; /* P2 */ |
1247 pMem->type = SQLITE_INTEGER; | |
1248 pMem++; | 1422 pMem++; |
1249 | 1423 |
1250 pMem->flags = MEM_Int; | 1424 pMem->flags = MEM_Int; |
1251 pMem->u.i = pOp->p3; /* P3 */ | 1425 pMem->u.i = pOp->p3; /* P3 */ |
1252 pMem->type = SQLITE_INTEGER; | |
1253 pMem++; | 1426 pMem++; |
1254 | 1427 |
1255 if( sqlite3VdbeMemGrow(pMem, 32, 0) ){ /* P4 */ | 1428 if( sqlite3VdbeMemClearAndResize(pMem, 32) ){ /* P4 */ |
1256 assert( p->db->mallocFailed ); | 1429 assert( p->db->mallocFailed ); |
1257 return SQLITE_ERROR; | 1430 return SQLITE_ERROR; |
1258 } | 1431 } |
1259 pMem->flags = MEM_Dyn|MEM_Str|MEM_Term; | 1432 pMem->flags = MEM_Str|MEM_Term; |
1260 z = displayP4(pOp, pMem->z, 32); | 1433 zP4 = displayP4(pOp, pMem->z, 32); |
1261 if( z!=pMem->z ){ | 1434 if( zP4!=pMem->z ){ |
1262 sqlite3VdbeMemSetStr(pMem, z, -1, SQLITE_UTF8, 0); | 1435 sqlite3VdbeMemSetStr(pMem, zP4, -1, SQLITE_UTF8, 0); |
1263 }else{ | 1436 }else{ |
1264 assert( pMem->z!=0 ); | 1437 assert( pMem->z!=0 ); |
1265 pMem->n = sqlite3Strlen30(pMem->z); | 1438 pMem->n = sqlite3Strlen30(pMem->z); |
1266 pMem->enc = SQLITE_UTF8; | 1439 pMem->enc = SQLITE_UTF8; |
1267 } | 1440 } |
1268 pMem->type = SQLITE_TEXT; | |
1269 pMem++; | 1441 pMem++; |
1270 | 1442 |
1271 if( p->explain==1 ){ | 1443 if( p->explain==1 ){ |
1272 if( sqlite3VdbeMemGrow(pMem, 4, 0) ){ | 1444 if( sqlite3VdbeMemClearAndResize(pMem, 4) ){ |
1273 assert( p->db->mallocFailed ); | 1445 assert( p->db->mallocFailed ); |
1274 return SQLITE_ERROR; | 1446 return SQLITE_ERROR; |
1275 } | 1447 } |
1276 pMem->flags = MEM_Dyn|MEM_Str|MEM_Term; | 1448 pMem->flags = MEM_Str|MEM_Term; |
1277 pMem->n = 2; | 1449 pMem->n = 2; |
1278 sqlite3_snprintf(3, pMem->z, "%.2x", pOp->p5); /* P5 */ | 1450 sqlite3_snprintf(3, pMem->z, "%.2x", pOp->p5); /* P5 */ |
1279 pMem->type = SQLITE_TEXT; | |
1280 pMem->enc = SQLITE_UTF8; | 1451 pMem->enc = SQLITE_UTF8; |
1281 pMem++; | 1452 pMem++; |
1282 | 1453 |
1283 #ifdef SQLITE_DEBUG | 1454 #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS |
1284 if( pOp->zComment ){ | 1455 if( sqlite3VdbeMemClearAndResize(pMem, 500) ){ |
1285 pMem->flags = MEM_Str|MEM_Term; | 1456 assert( p->db->mallocFailed ); |
1286 pMem->z = pOp->zComment; | 1457 return SQLITE_ERROR; |
1287 pMem->n = sqlite3Strlen30(pMem->z); | 1458 } |
1288 pMem->enc = SQLITE_UTF8; | 1459 pMem->flags = MEM_Str|MEM_Term; |
1289 pMem->type = SQLITE_TEXT; | 1460 pMem->n = displayComment(pOp, zP4, pMem->z, 500); |
1290 }else | 1461 pMem->enc = SQLITE_UTF8; |
| 1462 #else |
| 1463 pMem->flags = MEM_Null; /* Comment */ |
1291 #endif | 1464 #endif |
1292 { | |
1293 pMem->flags = MEM_Null; /* Comment */ | |
1294 pMem->type = SQLITE_NULL; | |
1295 } | |
1296 } | 1465 } |
1297 | 1466 |
1298 p->nResColumn = 8 - 4*(p->explain-1); | 1467 p->nResColumn = 8 - 4*(p->explain-1); |
| 1468 p->pResultSet = &p->aMem[1]; |
1299 p->rc = SQLITE_OK; | 1469 p->rc = SQLITE_OK; |
1300 rc = SQLITE_ROW; | 1470 rc = SQLITE_ROW; |
1301 } | 1471 } |
1302 return rc; | 1472 return rc; |
1303 } | 1473 } |
1304 #endif /* SQLITE_OMIT_EXPLAIN */ | 1474 #endif /* SQLITE_OMIT_EXPLAIN */ |
1305 | 1475 |
1306 #ifdef SQLITE_DEBUG | 1476 #ifdef SQLITE_DEBUG |
1307 /* | 1477 /* |
1308 ** Print the SQL that was used to generate a VDBE program. | 1478 ** Print the SQL that was used to generate a VDBE program. |
1309 */ | 1479 */ |
1310 void sqlite3VdbePrintSql(Vdbe *p){ | 1480 void sqlite3VdbePrintSql(Vdbe *p){ |
1311 int nOp = p->nOp; | 1481 const char *z = 0; |
1312 VdbeOp *pOp; | 1482 if( p->zSql ){ |
1313 if( nOp<1 ) return; | 1483 z = p->zSql; |
1314 pOp = &p->aOp[0]; | 1484 }else if( p->nOp>=1 ){ |
1315 if( pOp->opcode==OP_Trace && pOp->p4.z!=0 ){ | 1485 const VdbeOp *pOp = &p->aOp[0]; |
1316 const char *z = pOp->p4.z; | 1486 if( pOp->opcode==OP_Init && pOp->p4.z!=0 ){ |
1317 while( sqlite3Isspace(*z) ) z++; | 1487 z = pOp->p4.z; |
1318 printf("SQL: [%s]\n", z); | 1488 while( sqlite3Isspace(*z) ) z++; |
| 1489 } |
1319 } | 1490 } |
| 1491 if( z ) printf("SQL: [%s]\n", z); |
1320 } | 1492 } |
1321 #endif | 1493 #endif |
1322 | 1494 |
1323 #if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE) | 1495 #if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE) |
1324 /* | 1496 /* |
1325 ** Print an IOTRACE message showing SQL content. | 1497 ** Print an IOTRACE message showing SQL content. |
1326 */ | 1498 */ |
1327 void sqlite3VdbeIOTraceSql(Vdbe *p){ | 1499 void sqlite3VdbeIOTraceSql(Vdbe *p){ |
1328 int nOp = p->nOp; | 1500 int nOp = p->nOp; |
1329 VdbeOp *pOp; | 1501 VdbeOp *pOp; |
1330 if( sqlite3IoTrace==0 ) return; | 1502 if( sqlite3IoTrace==0 ) return; |
1331 if( nOp<1 ) return; | 1503 if( nOp<1 ) return; |
1332 pOp = &p->aOp[0]; | 1504 pOp = &p->aOp[0]; |
1333 if( pOp->opcode==OP_Trace && pOp->p4.z!=0 ){ | 1505 if( pOp->opcode==OP_Init && pOp->p4.z!=0 ){ |
1334 int i, j; | 1506 int i, j; |
1335 char z[1000]; | 1507 char z[1000]; |
1336 sqlite3_snprintf(sizeof(z), z, "%s", pOp->p4.z); | 1508 sqlite3_snprintf(sizeof(z), z, "%s", pOp->p4.z); |
1337 for(i=0; sqlite3Isspace(z[i]); i++){} | 1509 for(i=0; sqlite3Isspace(z[i]); i++){} |
1338 for(j=0; z[i]; i++){ | 1510 for(j=0; z[i]; i++){ |
1339 if( sqlite3Isspace(z[i]) ){ | 1511 if( sqlite3Isspace(z[i]) ){ |
1340 if( z[i-1]!=' ' ){ | 1512 if( z[i-1]!=' ' ){ |
1341 z[j++] = ' '; | 1513 z[j++] = ' '; |
1342 } | 1514 } |
1343 }else{ | 1515 }else{ |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1384 if( &(*ppFrom)[nByte] <= pEnd ){ | 1556 if( &(*ppFrom)[nByte] <= pEnd ){ |
1385 pBuf = (void*)*ppFrom; | 1557 pBuf = (void*)*ppFrom; |
1386 *ppFrom += nByte; | 1558 *ppFrom += nByte; |
1387 }else{ | 1559 }else{ |
1388 *pnByte += nByte; | 1560 *pnByte += nByte; |
1389 } | 1561 } |
1390 return pBuf; | 1562 return pBuf; |
1391 } | 1563 } |
1392 | 1564 |
1393 /* | 1565 /* |
1394 ** Prepare a virtual machine for execution. This involves things such | 1566 ** Rewind the VDBE back to the beginning in preparation for |
1395 ** as allocating stack space and initializing the program counter. | 1567 ** running it. |
1396 ** After the VDBE has be prepped, it can be executed by one or more | |
1397 ** calls to sqlite3VdbeExec(). | |
1398 ** | |
1399 ** This is the only way to move a VDBE from VDBE_MAGIC_INIT to | |
1400 ** VDBE_MAGIC_RUN. | |
1401 ** | |
1402 ** This function may be called more than once on a single virtual machine. | |
1403 ** The first call is made while compiling the SQL statement. Subsequent | |
1404 ** calls are made as part of the process of resetting a statement to be | |
1405 ** re-executed (from a call to sqlite3_reset()). The nVar, nMem, nCursor | |
1406 ** and isExplain parameters are only passed correct values the first time | |
1407 ** the function is called. On subsequent calls, from sqlite3_reset(), nVar | |
1408 ** is passed -1 and nMem, nCursor and isExplain are all passed zero. | |
1409 */ | 1568 */ |
1410 void sqlite3VdbeMakeReady( | 1569 void sqlite3VdbeRewind(Vdbe *p){ |
1411 Vdbe *p, /* The VDBE */ | 1570 #if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE) |
1412 int nVar, /* Number of '?' see in the SQL statement */ | 1571 int i; |
1413 int nMem, /* Number of memory cells to allocate */ | 1572 #endif |
1414 int nCursor, /* Number of cursors to allocate */ | |
1415 int nArg, /* Maximum number of args in SubPrograms */ | |
1416 int isExplain, /* True if the EXPLAIN keywords is present */ | |
1417 int usesStmtJournal /* True to set Vdbe.usesStmtJournal */ | |
1418 ){ | |
1419 int n; | |
1420 sqlite3 *db = p->db; | |
1421 | |
1422 assert( p!=0 ); | 1573 assert( p!=0 ); |
1423 assert( p->magic==VDBE_MAGIC_INIT ); | 1574 assert( p->magic==VDBE_MAGIC_INIT ); |
1424 | 1575 |
1425 /* There should be at least one opcode. | 1576 /* There should be at least one opcode. |
1426 */ | 1577 */ |
1427 assert( p->nOp>0 ); | 1578 assert( p->nOp>0 ); |
1428 | 1579 |
1429 /* Set the magic to VDBE_MAGIC_RUN sooner rather than later. */ | 1580 /* Set the magic to VDBE_MAGIC_RUN sooner rather than later. */ |
1430 p->magic = VDBE_MAGIC_RUN; | 1581 p->magic = VDBE_MAGIC_RUN; |
1431 | 1582 |
| 1583 #ifdef SQLITE_DEBUG |
| 1584 for(i=1; i<p->nMem; i++){ |
| 1585 assert( p->aMem[i].db==p->db ); |
| 1586 } |
| 1587 #endif |
| 1588 p->pc = -1; |
| 1589 p->rc = SQLITE_OK; |
| 1590 p->errorAction = OE_Abort; |
| 1591 p->magic = VDBE_MAGIC_RUN; |
| 1592 p->nChange = 0; |
| 1593 p->cacheCtr = 1; |
| 1594 p->minWriteFileFormat = 255; |
| 1595 p->iStatement = 0; |
| 1596 p->nFkConstraint = 0; |
| 1597 #ifdef VDBE_PROFILE |
| 1598 for(i=0; i<p->nOp; i++){ |
| 1599 p->aOp[i].cnt = 0; |
| 1600 p->aOp[i].cycles = 0; |
| 1601 } |
| 1602 #endif |
| 1603 } |
| 1604 |
| 1605 /* |
| 1606 ** Prepare a virtual machine for execution for the first time after |
| 1607 ** creating the virtual machine. This involves things such |
| 1608 ** as allocating registers and initializing the program counter. |
| 1609 ** After the VDBE has be prepped, it can be executed by one or more |
| 1610 ** calls to sqlite3VdbeExec(). |
| 1611 ** |
| 1612 ** This function may be called exactly once on each virtual machine. |
| 1613 ** After this routine is called the VM has been "packaged" and is ready |
| 1614 ** to run. After this routine is called, further calls to |
| 1615 ** sqlite3VdbeAddOp() functions are prohibited. This routine disconnects |
| 1616 ** the Vdbe from the Parse object that helped generate it so that the |
| 1617 ** the Vdbe becomes an independent entity and the Parse object can be |
| 1618 ** destroyed. |
| 1619 ** |
| 1620 ** Use the sqlite3VdbeRewind() procedure to restore a virtual machine back |
| 1621 ** to its initial state after it has been run. |
| 1622 */ |
| 1623 void sqlite3VdbeMakeReady( |
| 1624 Vdbe *p, /* The VDBE */ |
| 1625 Parse *pParse /* Parsing context */ |
| 1626 ){ |
| 1627 sqlite3 *db; /* The database connection */ |
| 1628 int nVar; /* Number of parameters */ |
| 1629 int nMem; /* Number of VM memory registers */ |
| 1630 int nCursor; /* Number of cursors required */ |
| 1631 int nArg; /* Number of arguments in subprograms */ |
| 1632 int nOnce; /* Number of OP_Once instructions */ |
| 1633 int n; /* Loop counter */ |
| 1634 u8 *zCsr; /* Memory available for allocation */ |
| 1635 u8 *zEnd; /* First byte past allocated memory */ |
| 1636 int nByte; /* How much extra memory is needed */ |
| 1637 |
| 1638 assert( p!=0 ); |
| 1639 assert( p->nOp>0 ); |
| 1640 assert( pParse!=0 ); |
| 1641 assert( p->magic==VDBE_MAGIC_INIT ); |
| 1642 assert( pParse==p->pParse ); |
| 1643 db = p->db; |
| 1644 assert( db->mallocFailed==0 ); |
| 1645 nVar = pParse->nVar; |
| 1646 nMem = pParse->nMem; |
| 1647 nCursor = pParse->nTab; |
| 1648 nArg = pParse->nMaxArg; |
| 1649 nOnce = pParse->nOnce; |
| 1650 if( nOnce==0 ) nOnce = 1; /* Ensure at least one byte in p->aOnceFlag[] */ |
| 1651 |
1432 /* For each cursor required, also allocate a memory cell. Memory | 1652 /* For each cursor required, also allocate a memory cell. Memory |
1433 ** cells (nMem+1-nCursor)..nMem, inclusive, will never be used by | 1653 ** cells (nMem+1-nCursor)..nMem, inclusive, will never be used by |
1434 ** the vdbe program. Instead they are used to allocate space for | 1654 ** the vdbe program. Instead they are used to allocate space for |
1435 ** VdbeCursor/BtCursor structures. The blob of memory associated with | 1655 ** VdbeCursor/BtCursor structures. The blob of memory associated with |
1436 ** cursor 0 is stored in memory cell nMem. Memory cell (nMem-1) | 1656 ** cursor 0 is stored in memory cell nMem. Memory cell (nMem-1) |
1437 ** stores the blob of memory associated with cursor 1, etc. | 1657 ** stores the blob of memory associated with cursor 1, etc. |
1438 ** | 1658 ** |
1439 ** See also: allocateCursor(). | 1659 ** See also: allocateCursor(). |
1440 */ | 1660 */ |
1441 nMem += nCursor; | 1661 nMem += nCursor; |
1442 | 1662 |
1443 /* Allocate space for memory registers, SQL variables, VDBE cursors and | 1663 /* Allocate space for memory registers, SQL variables, VDBE cursors and |
1444 ** an array to marshal SQL function arguments in. This is only done the | 1664 ** an array to marshal SQL function arguments in. |
1445 ** first time this function is called for a given VDBE, not when it is | |
1446 ** being called from sqlite3_reset() to reset the virtual machine. | |
1447 */ | 1665 */ |
1448 if( nVar>=0 && ALWAYS(db->mallocFailed==0) ){ | 1666 zCsr = (u8*)&p->aOp[p->nOp]; /* Memory avaliable for allocation */ |
1449 u8 *zCsr = (u8 *)&p->aOp[p->nOp]; /* Memory avaliable for alloation */ | 1667 zEnd = (u8*)&p->aOp[pParse->nOpAlloc]; /* First byte past end of zCsr[] */ |
1450 u8 *zEnd = (u8 *)&p->aOp[p->nOpAlloc]; /* First byte past available mem */ | |
1451 int nByte; /* How much extra memory needed */ | |
1452 | 1668 |
1453 resolveP2Values(p, &nArg); | 1669 resolveP2Values(p, &nArg); |
1454 p->usesStmtJournal = (u8)usesStmtJournal; | 1670 p->usesStmtJournal = (u8)(pParse->isMultiWrite && pParse->mayAbort); |
1455 if( isExplain && nMem<10 ){ | 1671 if( pParse->explain && nMem<10 ){ |
1456 nMem = 10; | 1672 nMem = 10; |
| 1673 } |
| 1674 memset(zCsr, 0, zEnd-zCsr); |
| 1675 zCsr += (zCsr - (u8*)0)&7; |
| 1676 assert( EIGHT_BYTE_ALIGNMENT(zCsr) ); |
| 1677 p->expired = 0; |
| 1678 |
| 1679 /* Memory for registers, parameters, cursor, etc, is allocated in two |
| 1680 ** passes. On the first pass, we try to reuse unused space at the |
| 1681 ** end of the opcode array. If we are unable to satisfy all memory |
| 1682 ** requirements by reusing the opcode array tail, then the second |
| 1683 ** pass will fill in the rest using a fresh allocation. |
| 1684 ** |
| 1685 ** This two-pass approach that reuses as much memory as possible from |
| 1686 ** the leftover space at the end of the opcode array can significantly |
| 1687 ** reduce the amount of memory held by a prepared statement. |
| 1688 */ |
| 1689 do { |
| 1690 nByte = 0; |
| 1691 p->aMem = allocSpace(p->aMem, nMem*sizeof(Mem), &zCsr, zEnd, &nByte); |
| 1692 p->aVar = allocSpace(p->aVar, nVar*sizeof(Mem), &zCsr, zEnd, &nByte); |
| 1693 p->apArg = allocSpace(p->apArg, nArg*sizeof(Mem*), &zCsr, zEnd, &nByte); |
| 1694 p->azVar = allocSpace(p->azVar, nVar*sizeof(char*), &zCsr, zEnd, &nByte); |
| 1695 p->apCsr = allocSpace(p->apCsr, nCursor*sizeof(VdbeCursor*), |
| 1696 &zCsr, zEnd, &nByte); |
| 1697 p->aOnceFlag = allocSpace(p->aOnceFlag, nOnce, &zCsr, zEnd, &nByte); |
| 1698 if( nByte ){ |
| 1699 p->pFree = sqlite3DbMallocZero(db, nByte); |
1457 } | 1700 } |
1458 memset(zCsr, 0, zEnd-zCsr); | 1701 zCsr = p->pFree; |
1459 zCsr += (zCsr - (u8*)0)&7; | 1702 zEnd = &zCsr[nByte]; |
1460 assert( EIGHT_BYTE_ALIGNMENT(zCsr) ); | 1703 }while( nByte && !db->mallocFailed ); |
1461 | 1704 |
1462 /* Memory for registers, parameters, cursor, etc, is allocated in two | 1705 p->nCursor = nCursor; |
1463 ** passes. On the first pass, we try to reuse unused space at the | 1706 p->nOnceFlag = nOnce; |
1464 ** end of the opcode array. If we are unable to satisfy all memory | 1707 if( p->aVar ){ |
1465 ** requirements by reusing the opcode array tail, then the second | 1708 p->nVar = (ynVar)nVar; |
1466 ** pass will fill in the rest using a fresh allocation. | 1709 for(n=0; n<nVar; n++){ |
1467 ** | 1710 p->aVar[n].flags = MEM_Null; |
1468 ** This two-pass approach that reuses as much memory as possible from | 1711 p->aVar[n].db = db; |
1469 ** the leftover space at the end of the opcode array can significantly | |
1470 ** reduce the amount of memory held by a prepared statement. | |
1471 */ | |
1472 do { | |
1473 nByte = 0; | |
1474 p->aMem = allocSpace(p->aMem, nMem*sizeof(Mem), &zCsr, zEnd, &nByte); | |
1475 p->aVar = allocSpace(p->aVar, nVar*sizeof(Mem), &zCsr, zEnd, &nByte); | |
1476 p->apArg = allocSpace(p->apArg, nArg*sizeof(Mem*), &zCsr, zEnd, &nByte); | |
1477 p->azVar = allocSpace(p->azVar, nVar*sizeof(char*), &zCsr, zEnd, &nByte); | |
1478 p->apCsr = allocSpace(p->apCsr, nCursor*sizeof(VdbeCursor*), | |
1479 &zCsr, zEnd, &nByte); | |
1480 if( nByte ){ | |
1481 p->pFree = sqlite3DbMallocZero(db, nByte); | |
1482 } | |
1483 zCsr = p->pFree; | |
1484 zEnd = &zCsr[nByte]; | |
1485 }while( nByte && !db->mallocFailed ); | |
1486 | |
1487 p->nCursor = (u16)nCursor; | |
1488 if( p->aVar ){ | |
1489 p->nVar = (ynVar)nVar; | |
1490 for(n=0; n<nVar; n++){ | |
1491 p->aVar[n].flags = MEM_Null; | |
1492 p->aVar[n].db = db; | |
1493 } | |
1494 } | |
1495 if( p->aMem ){ | |
1496 p->aMem--; /* aMem[] goes from 1..nMem */ | |
1497 p->nMem = nMem; /* not from 0..nMem-1 */ | |
1498 for(n=1; n<=nMem; n++){ | |
1499 p->aMem[n].flags = MEM_Null; | |
1500 p->aMem[n].db = db; | |
1501 } | |
1502 } | 1712 } |
1503 } | 1713 } |
1504 #ifdef SQLITE_DEBUG | 1714 if( p->azVar ){ |
1505 for(n=1; n<p->nMem; n++){ | 1715 p->nzVar = pParse->nzVar; |
1506 assert( p->aMem[n].db==db ); | 1716 memcpy(p->azVar, pParse->azVar, p->nzVar*sizeof(p->azVar[0])); |
| 1717 memset(pParse->azVar, 0, pParse->nzVar*sizeof(pParse->azVar[0])); |
1507 } | 1718 } |
1508 #endif | 1719 if( p->aMem ){ |
1509 | 1720 p->aMem--; /* aMem[] goes from 1..nMem */ |
1510 p->pc = -1; | 1721 p->nMem = nMem; /* not from 0..nMem-1 */ |
1511 p->rc = SQLITE_OK; | 1722 for(n=1; n<=nMem; n++){ |
1512 p->errorAction = OE_Abort; | 1723 p->aMem[n].flags = MEM_Undefined; |
1513 p->explain |= isExplain; | 1724 p->aMem[n].db = db; |
1514 p->magic = VDBE_MAGIC_RUN; | |
1515 p->nChange = 0; | |
1516 p->cacheCtr = 1; | |
1517 p->minWriteFileFormat = 255; | |
1518 p->iStatement = 0; | |
1519 p->nFkConstraint = 0; | |
1520 #ifdef VDBE_PROFILE | |
1521 { | |
1522 int i; | |
1523 for(i=0; i<p->nOp; i++){ | |
1524 p->aOp[i].cnt = 0; | |
1525 p->aOp[i].cycles = 0; | |
1526 } | 1725 } |
1527 } | 1726 } |
1528 #endif | 1727 p->explain = pParse->explain; |
| 1728 sqlite3VdbeRewind(p); |
1529 } | 1729 } |
1530 | 1730 |
1531 /* | 1731 /* |
1532 ** Close a VDBE cursor and release all the resources that cursor | 1732 ** Close a VDBE cursor and release all the resources that cursor |
1533 ** happens to hold. | 1733 ** happens to hold. |
1534 */ | 1734 */ |
1535 void sqlite3VdbeFreeCursor(Vdbe *p, VdbeCursor *pCx){ | 1735 void sqlite3VdbeFreeCursor(Vdbe *p, VdbeCursor *pCx){ |
1536 if( pCx==0 ){ | 1736 if( pCx==0 ){ |
1537 return; | 1737 return; |
1538 } | 1738 } |
| 1739 sqlite3VdbeSorterClose(p->db, pCx); |
1539 if( pCx->pBt ){ | 1740 if( pCx->pBt ){ |
1540 sqlite3BtreeClose(pCx->pBt); | 1741 sqlite3BtreeClose(pCx->pBt); |
1541 /* The pCx->pCursor will be close automatically, if it exists, by | 1742 /* The pCx->pCursor will be close automatically, if it exists, by |
1542 ** the call above. */ | 1743 ** the call above. */ |
1543 }else if( pCx->pCursor ){ | 1744 }else if( pCx->pCursor ){ |
1544 sqlite3BtreeCloseCursor(pCx->pCursor); | 1745 sqlite3BtreeCloseCursor(pCx->pCursor); |
1545 } | 1746 } |
1546 #ifndef SQLITE_OMIT_VIRTUALTABLE | 1747 #ifndef SQLITE_OMIT_VIRTUALTABLE |
1547 if( pCx->pVtabCursor ){ | 1748 else if( pCx->pVtabCursor ){ |
1548 sqlite3_vtab_cursor *pVtabCursor = pCx->pVtabCursor; | 1749 sqlite3_vtab_cursor *pVtabCursor = pCx->pVtabCursor; |
1549 const sqlite3_module *pModule = pCx->pModule; | 1750 const sqlite3_module *pModule = pVtabCursor->pVtab->pModule; |
1550 p->inVtabMethod = 1; | 1751 p->inVtabMethod = 1; |
1551 pModule->xClose(pVtabCursor); | 1752 pModule->xClose(pVtabCursor); |
1552 p->inVtabMethod = 0; | 1753 p->inVtabMethod = 0; |
1553 } | 1754 } |
1554 #endif | 1755 #endif |
1555 } | 1756 } |
1556 | 1757 |
1557 /* | 1758 /* |
1558 ** Copy the values stored in the VdbeFrame structure to its Vdbe. This | 1759 ** Copy the values stored in the VdbeFrame structure to its Vdbe. This |
1559 ** is used, for example, when a trigger sub-program is halted to restore | 1760 ** is used, for example, when a trigger sub-program is halted to restore |
1560 ** control to the main program. | 1761 ** control to the main program. |
1561 */ | 1762 */ |
1562 int sqlite3VdbeFrameRestore(VdbeFrame *pFrame){ | 1763 int sqlite3VdbeFrameRestore(VdbeFrame *pFrame){ |
1563 Vdbe *v = pFrame->v; | 1764 Vdbe *v = pFrame->v; |
| 1765 v->aOnceFlag = pFrame->aOnceFlag; |
| 1766 v->nOnceFlag = pFrame->nOnceFlag; |
1564 v->aOp = pFrame->aOp; | 1767 v->aOp = pFrame->aOp; |
1565 v->nOp = pFrame->nOp; | 1768 v->nOp = pFrame->nOp; |
1566 v->aMem = pFrame->aMem; | 1769 v->aMem = pFrame->aMem; |
1567 v->nMem = pFrame->nMem; | 1770 v->nMem = pFrame->nMem; |
1568 v->apCsr = pFrame->apCsr; | 1771 v->apCsr = pFrame->apCsr; |
1569 v->nCursor = pFrame->nCursor; | 1772 v->nCursor = pFrame->nCursor; |
1570 v->db->lastRowid = pFrame->lastRowid; | 1773 v->db->lastRowid = pFrame->lastRowid; |
1571 v->nChange = pFrame->nChange; | 1774 v->nChange = pFrame->nChange; |
1572 return pFrame->pc; | 1775 return pFrame->pc; |
1573 } | 1776 } |
1574 | 1777 |
1575 /* | 1778 /* |
1576 ** Close all cursors. | 1779 ** Close all cursors. |
1577 ** | 1780 ** |
1578 ** Also release any dynamic memory held by the VM in the Vdbe.aMem memory | 1781 ** Also release any dynamic memory held by the VM in the Vdbe.aMem memory |
1579 ** cell array. This is necessary as the memory cell array may contain | 1782 ** cell array. This is necessary as the memory cell array may contain |
1580 ** pointers to VdbeFrame objects, which may in turn contain pointers to | 1783 ** pointers to VdbeFrame objects, which may in turn contain pointers to |
1581 ** open cursors. | 1784 ** open cursors. |
1582 */ | 1785 */ |
1583 static void closeAllCursors(Vdbe *p){ | 1786 static void closeAllCursors(Vdbe *p){ |
1584 if( p->pFrame ){ | 1787 if( p->pFrame ){ |
1585 VdbeFrame *pFrame; | 1788 VdbeFrame *pFrame; |
1586 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent); | 1789 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent); |
1587 sqlite3VdbeFrameRestore(pFrame); | 1790 sqlite3VdbeFrameRestore(pFrame); |
| 1791 p->pFrame = 0; |
| 1792 p->nFrame = 0; |
1588 } | 1793 } |
1589 p->pFrame = 0; | 1794 assert( p->nFrame==0 ); |
1590 p->nFrame = 0; | |
1591 | 1795 |
1592 if( p->apCsr ){ | 1796 if( p->apCsr ){ |
1593 int i; | 1797 int i; |
1594 for(i=0; i<p->nCursor; i++){ | 1798 for(i=0; i<p->nCursor; i++){ |
1595 VdbeCursor *pC = p->apCsr[i]; | 1799 VdbeCursor *pC = p->apCsr[i]; |
1596 if( pC ){ | 1800 if( pC ){ |
1597 sqlite3VdbeFreeCursor(p, pC); | 1801 sqlite3VdbeFreeCursor(p, pC); |
1598 p->apCsr[i] = 0; | 1802 p->apCsr[i] = 0; |
1599 } | 1803 } |
1600 } | 1804 } |
1601 } | 1805 } |
1602 if( p->aMem ){ | 1806 if( p->aMem ){ |
1603 releaseMemArray(&p->aMem[1], p->nMem); | 1807 releaseMemArray(&p->aMem[1], p->nMem); |
1604 } | 1808 } |
1605 while( p->pDelFrame ){ | 1809 while( p->pDelFrame ){ |
1606 VdbeFrame *pDel = p->pDelFrame; | 1810 VdbeFrame *pDel = p->pDelFrame; |
1607 p->pDelFrame = pDel->pParent; | 1811 p->pDelFrame = pDel->pParent; |
1608 sqlite3VdbeFrameDelete(pDel); | 1812 sqlite3VdbeFrameDelete(pDel); |
1609 } | 1813 } |
| 1814 |
| 1815 /* Delete any auxdata allocations made by the VM */ |
| 1816 if( p->pAuxData ) sqlite3VdbeDeleteAuxData(p, -1, 0); |
| 1817 assert( p->pAuxData==0 ); |
1610 } | 1818 } |
1611 | 1819 |
1612 /* | 1820 /* |
1613 ** Clean up the VM after execution. | 1821 ** Clean up the VM after a single run. |
1614 ** | |
1615 ** This routine will automatically close any cursors, lists, and/or | |
1616 ** sorters that were left open. It also deletes the values of | |
1617 ** variables in the aVar[] array. | |
1618 */ | 1822 */ |
1619 static void Cleanup(Vdbe *p){ | 1823 static void Cleanup(Vdbe *p){ |
1620 sqlite3 *db = p->db; | 1824 sqlite3 *db = p->db; |
1621 | 1825 |
1622 #ifdef SQLITE_DEBUG | 1826 #ifdef SQLITE_DEBUG |
1623 /* Execute assert() statements to ensure that the Vdbe.apCsr[] and | 1827 /* Execute assert() statements to ensure that the Vdbe.apCsr[] and |
1624 ** Vdbe.aMem[] arrays have already been cleaned up. */ | 1828 ** Vdbe.aMem[] arrays have already been cleaned up. */ |
1625 int i; | 1829 int i; |
1626 for(i=0; i<p->nCursor; i++) assert( p->apCsr==0 || p->apCsr[i]==0 ); | 1830 if( p->apCsr ) for(i=0; i<p->nCursor; i++) assert( p->apCsr[i]==0 ); |
1627 for(i=1; i<=p->nMem; i++) assert( p->aMem==0 || p->aMem[i].flags==MEM_Null ); | 1831 if( p->aMem ){ |
| 1832 for(i=1; i<=p->nMem; i++) assert( p->aMem[i].flags==MEM_Undefined ); |
| 1833 } |
1628 #endif | 1834 #endif |
1629 | 1835 |
1630 sqlite3DbFree(db, p->zErrMsg); | 1836 sqlite3DbFree(db, p->zErrMsg); |
1631 p->zErrMsg = 0; | 1837 p->zErrMsg = 0; |
1632 p->pResultSet = 0; | 1838 p->pResultSet = 0; |
1633 } | 1839 } |
1634 | 1840 |
1635 /* | 1841 /* |
1636 ** Set the number of result columns that will be returned by this SQL | 1842 ** Set the number of result columns that will be returned by this SQL |
1637 ** statement. This is now set at compile time, rather than during | 1843 ** statement. This is now set at compile time, rather than during |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1706 */ | 1912 */ |
1707 UNUSED_PARAMETER(p); | 1913 UNUSED_PARAMETER(p); |
1708 #endif | 1914 #endif |
1709 | 1915 |
1710 /* Before doing anything else, call the xSync() callback for any | 1916 /* Before doing anything else, call the xSync() callback for any |
1711 ** virtual module tables written in this transaction. This has to | 1917 ** virtual module tables written in this transaction. This has to |
1712 ** be done before determining whether a master journal file is | 1918 ** be done before determining whether a master journal file is |
1713 ** required, as an xSync() callback may add an attached database | 1919 ** required, as an xSync() callback may add an attached database |
1714 ** to the transaction. | 1920 ** to the transaction. |
1715 */ | 1921 */ |
1716 rc = sqlite3VtabSync(db, &p->zErrMsg); | 1922 rc = sqlite3VtabSync(db, p); |
1717 | 1923 |
1718 /* This loop determines (a) if the commit hook should be invoked and | 1924 /* This loop determines (a) if the commit hook should be invoked and |
1719 ** (b) how many database files have open write transactions, not | 1925 ** (b) how many database files have open write transactions, not |
1720 ** including the temp database. (b) is important because if more than | 1926 ** including the temp database. (b) is important because if more than |
1721 ** one database file has an open write transaction, a master journal | 1927 ** one database file has an open write transaction, a master journal |
1722 ** file is required for an atomic commit. | 1928 ** file is required for an atomic commit. |
1723 */ | 1929 */ |
1724 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){ | 1930 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){ |
1725 Btree *pBt = db->aDb[i].pBt; | 1931 Btree *pBt = db->aDb[i].pBt; |
1726 if( sqlite3BtreeIsInTrans(pBt) ){ | 1932 if( sqlite3BtreeIsInTrans(pBt) ){ |
1727 needXcommit = 1; | 1933 needXcommit = 1; |
1728 if( i!=1 ) nTrans++; | 1934 if( i!=1 ) nTrans++; |
| 1935 sqlite3BtreeEnter(pBt); |
1729 rc = sqlite3PagerExclusiveLock(sqlite3BtreePager(pBt)); | 1936 rc = sqlite3PagerExclusiveLock(sqlite3BtreePager(pBt)); |
| 1937 sqlite3BtreeLeave(pBt); |
1730 } | 1938 } |
1731 } | 1939 } |
1732 if( rc!=SQLITE_OK ){ | 1940 if( rc!=SQLITE_OK ){ |
1733 return rc; | 1941 return rc; |
1734 } | 1942 } |
1735 | 1943 |
1736 /* If there are any write-transactions at all, invoke the commit hook */ | 1944 /* If there are any write-transactions at all, invoke the commit hook */ |
1737 if( needXcommit && db->xCommitCallback ){ | 1945 if( needXcommit && db->xCommitCallback ){ |
1738 rc = db->xCommitCallback(db->pCommitArg); | 1946 rc = db->xCommitCallback(db->pCommitArg); |
1739 if( rc ){ | 1947 if( rc ){ |
1740 return SQLITE_CONSTRAINT; | 1948 return SQLITE_CONSTRAINT_COMMITHOOK; |
1741 } | 1949 } |
1742 } | 1950 } |
1743 | 1951 |
1744 /* The simple case - no more than one database file (not counting the | 1952 /* The simple case - no more than one database file (not counting the |
1745 ** TEMP database) has a transaction active. There is no need for the | 1953 ** TEMP database) has a transaction active. There is no need for the |
1746 ** master-journal. | 1954 ** master-journal. |
1747 ** | 1955 ** |
1748 ** If the return value of sqlite3BtreeGetFilename() is a zero length | 1956 ** If the return value of sqlite3BtreeGetFilename() is a zero length |
1749 ** string, it means the main database is :memory: or a temp file. In | 1957 ** string, it means the main database is :memory: or a temp file. In |
1750 ** that case we do not support atomic multi-file commits, so use the | 1958 ** that case we do not support atomic multi-file commits, so use the |
(...skipping 20 matching lines...) Expand all Loading... |
1771 rc = sqlite3BtreeCommitPhaseTwo(pBt, 0); | 1979 rc = sqlite3BtreeCommitPhaseTwo(pBt, 0); |
1772 } | 1980 } |
1773 } | 1981 } |
1774 if( rc==SQLITE_OK ){ | 1982 if( rc==SQLITE_OK ){ |
1775 sqlite3VtabCommit(db); | 1983 sqlite3VtabCommit(db); |
1776 } | 1984 } |
1777 } | 1985 } |
1778 | 1986 |
1779 /* The complex case - There is a multi-file write-transaction active. | 1987 /* The complex case - There is a multi-file write-transaction active. |
1780 ** This requires a master journal file to ensure the transaction is | 1988 ** This requires a master journal file to ensure the transaction is |
1781 ** committed atomicly. | 1989 ** committed atomically. |
1782 */ | 1990 */ |
1783 #ifndef SQLITE_OMIT_DISKIO | 1991 #ifndef SQLITE_OMIT_DISKIO |
1784 else{ | 1992 else{ |
1785 sqlite3_vfs *pVfs = db->pVfs; | 1993 sqlite3_vfs *pVfs = db->pVfs; |
1786 int needSync = 0; | 1994 int needSync = 0; |
1787 char *zMaster = 0; /* File-name for the master journal */ | 1995 char *zMaster = 0; /* File-name for the master journal */ |
1788 char const *zMainFile = sqlite3BtreeGetFilename(db->aDb[0].pBt); | 1996 char const *zMainFile = sqlite3BtreeGetFilename(db->aDb[0].pBt); |
1789 sqlite3_file *pMaster = 0; | 1997 sqlite3_file *pMaster = 0; |
1790 i64 offset = 0; | 1998 i64 offset = 0; |
1791 int res; | 1999 int res; |
| 2000 int retryCount = 0; |
| 2001 int nMainFile; |
1792 | 2002 |
1793 /* Select a master journal file name */ | 2003 /* Select a master journal file name */ |
| 2004 nMainFile = sqlite3Strlen30(zMainFile); |
| 2005 zMaster = sqlite3MPrintf(db, "%s-mjXXXXXX9XXz", zMainFile); |
| 2006 if( zMaster==0 ) return SQLITE_NOMEM; |
1794 do { | 2007 do { |
1795 u32 iRandom; | 2008 u32 iRandom; |
1796 sqlite3DbFree(db, zMaster); | 2009 if( retryCount ){ |
| 2010 if( retryCount>100 ){ |
| 2011 sqlite3_log(SQLITE_FULL, "MJ delete: %s", zMaster); |
| 2012 sqlite3OsDelete(pVfs, zMaster, 0); |
| 2013 break; |
| 2014 }else if( retryCount==1 ){ |
| 2015 sqlite3_log(SQLITE_FULL, "MJ collide: %s", zMaster); |
| 2016 } |
| 2017 } |
| 2018 retryCount++; |
1797 sqlite3_randomness(sizeof(iRandom), &iRandom); | 2019 sqlite3_randomness(sizeof(iRandom), &iRandom); |
1798 zMaster = sqlite3MPrintf(db, "%s-mj%08X", zMainFile, iRandom&0x7fffffff); | 2020 sqlite3_snprintf(13, &zMaster[nMainFile], "-mj%06X9%02X", |
1799 if( !zMaster ){ | 2021 (iRandom>>8)&0xffffff, iRandom&0xff); |
1800 return SQLITE_NOMEM; | 2022 /* The antipenultimate character of the master journal name must |
1801 } | 2023 ** be "9" to avoid name collisions when using 8+3 filenames. */ |
| 2024 assert( zMaster[sqlite3Strlen30(zMaster)-3]=='9' ); |
| 2025 sqlite3FileSuffix3(zMainFile, zMaster); |
1802 rc = sqlite3OsAccess(pVfs, zMaster, SQLITE_ACCESS_EXISTS, &res); | 2026 rc = sqlite3OsAccess(pVfs, zMaster, SQLITE_ACCESS_EXISTS, &res); |
1803 }while( rc==SQLITE_OK && res ); | 2027 }while( rc==SQLITE_OK && res ); |
1804 if( rc==SQLITE_OK ){ | 2028 if( rc==SQLITE_OK ){ |
1805 /* Open the master journal. */ | 2029 /* Open the master journal. */ |
1806 rc = sqlite3OsOpenMalloc(pVfs, zMaster, &pMaster, | 2030 rc = sqlite3OsOpenMalloc(pVfs, zMaster, &pMaster, |
1807 SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE| | 2031 SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE| |
1808 SQLITE_OPEN_EXCLUSIVE|SQLITE_OPEN_MASTER_JOURNAL, 0 | 2032 SQLITE_OPEN_EXCLUSIVE|SQLITE_OPEN_MASTER_JOURNAL, 0 |
1809 ); | 2033 ); |
1810 } | 2034 } |
1811 if( rc!=SQLITE_OK ){ | 2035 if( rc!=SQLITE_OK ){ |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1907 enable_simulated_io_errors(); | 2131 enable_simulated_io_errors(); |
1908 | 2132 |
1909 sqlite3VtabCommit(db); | 2133 sqlite3VtabCommit(db); |
1910 } | 2134 } |
1911 #endif | 2135 #endif |
1912 | 2136 |
1913 return rc; | 2137 return rc; |
1914 } | 2138 } |
1915 | 2139 |
1916 /* | 2140 /* |
1917 ** This routine checks that the sqlite3.activeVdbeCnt count variable | 2141 ** This routine checks that the sqlite3.nVdbeActive count variable |
1918 ** matches the number of vdbe's in the list sqlite3.pVdbe that are | 2142 ** matches the number of vdbe's in the list sqlite3.pVdbe that are |
1919 ** currently active. An assertion fails if the two counts do not match. | 2143 ** currently active. An assertion fails if the two counts do not match. |
1920 ** This is an internal self-check only - it is not an essential processing | 2144 ** This is an internal self-check only - it is not an essential processing |
1921 ** step. | 2145 ** step. |
1922 ** | 2146 ** |
1923 ** This is a no-op if NDEBUG is defined. | 2147 ** This is a no-op if NDEBUG is defined. |
1924 */ | 2148 */ |
1925 #ifndef NDEBUG | 2149 #ifndef NDEBUG |
1926 static void checkActiveVdbeCnt(sqlite3 *db){ | 2150 static void checkActiveVdbeCnt(sqlite3 *db){ |
1927 Vdbe *p; | 2151 Vdbe *p; |
1928 int cnt = 0; | 2152 int cnt = 0; |
1929 int nWrite = 0; | 2153 int nWrite = 0; |
| 2154 int nRead = 0; |
1930 p = db->pVdbe; | 2155 p = db->pVdbe; |
1931 while( p ){ | 2156 while( p ){ |
1932 if( p->magic==VDBE_MAGIC_RUN && p->pc>=0 ){ | 2157 if( sqlite3_stmt_busy((sqlite3_stmt*)p) ){ |
1933 cnt++; | 2158 cnt++; |
1934 if( p->readOnly==0 ) nWrite++; | 2159 if( p->readOnly==0 ) nWrite++; |
| 2160 if( p->bIsReader ) nRead++; |
1935 } | 2161 } |
1936 p = p->pNext; | 2162 p = p->pNext; |
1937 } | 2163 } |
1938 assert( cnt==db->activeVdbeCnt ); | 2164 assert( cnt==db->nVdbeActive ); |
1939 assert( nWrite==db->writeVdbeCnt ); | 2165 assert( nWrite==db->nVdbeWrite ); |
| 2166 assert( nRead==db->nVdbeRead ); |
1940 } | 2167 } |
1941 #else | 2168 #else |
1942 #define checkActiveVdbeCnt(x) | 2169 #define checkActiveVdbeCnt(x) |
1943 #endif | 2170 #endif |
1944 | 2171 |
1945 /* | 2172 /* |
1946 ** For every Btree that in database connection db which | |
1947 ** has been modified, "trip" or invalidate each cursor in | |
1948 ** that Btree might have been modified so that the cursor | |
1949 ** can never be used again. This happens when a rollback | |
1950 *** occurs. We have to trip all the other cursors, even | |
1951 ** cursor from other VMs in different database connections, | |
1952 ** so that none of them try to use the data at which they | |
1953 ** were pointing and which now may have been changed due | |
1954 ** to the rollback. | |
1955 ** | |
1956 ** Remember that a rollback can delete tables complete and | |
1957 ** reorder rootpages. So it is not sufficient just to save | |
1958 ** the state of the cursor. We have to invalidate the cursor | |
1959 ** so that it is never used again. | |
1960 */ | |
1961 static void invalidateCursorsOnModifiedBtrees(sqlite3 *db){ | |
1962 int i; | |
1963 for(i=0; i<db->nDb; i++){ | |
1964 Btree *p = db->aDb[i].pBt; | |
1965 if( p && sqlite3BtreeIsInTrans(p) ){ | |
1966 sqlite3BtreeTripAllCursors(p, SQLITE_ABORT); | |
1967 } | |
1968 } | |
1969 } | |
1970 | |
1971 /* | |
1972 ** If the Vdbe passed as the first argument opened a statement-transaction, | 2173 ** If the Vdbe passed as the first argument opened a statement-transaction, |
1973 ** close it now. Argument eOp must be either SAVEPOINT_ROLLBACK or | 2174 ** close it now. Argument eOp must be either SAVEPOINT_ROLLBACK or |
1974 ** SAVEPOINT_RELEASE. If it is SAVEPOINT_ROLLBACK, then the statement | 2175 ** SAVEPOINT_RELEASE. If it is SAVEPOINT_ROLLBACK, then the statement |
1975 ** transaction is rolled back. If eOp is SAVEPOINT_RELEASE, then the | 2176 ** transaction is rolled back. If eOp is SAVEPOINT_RELEASE, then the |
1976 ** statement transaction is commtted. | 2177 ** statement transaction is committed. |
1977 ** | 2178 ** |
1978 ** If an IO error occurs, an SQLITE_IOERR_XXX error code is returned. | 2179 ** If an IO error occurs, an SQLITE_IOERR_XXX error code is returned. |
1979 ** Otherwise SQLITE_OK. | 2180 ** Otherwise SQLITE_OK. |
1980 */ | 2181 */ |
1981 int sqlite3VdbeCloseStatement(Vdbe *p, int eOp){ | 2182 int sqlite3VdbeCloseStatement(Vdbe *p, int eOp){ |
1982 sqlite3 *const db = p->db; | 2183 sqlite3 *const db = p->db; |
1983 int rc = SQLITE_OK; | 2184 int rc = SQLITE_OK; |
1984 | 2185 |
1985 /* If p->iStatement is greater than zero, then this Vdbe opened a | 2186 /* If p->iStatement is greater than zero, then this Vdbe opened a |
1986 ** statement transaction that should be closed here. The only exception | 2187 ** statement transaction that should be closed here. The only exception |
1987 ** is that an IO error may have occured, causing an emergency rollback. | 2188 ** is that an IO error may have occurred, causing an emergency rollback. |
1988 ** In this case (db->nStatement==0), and there is nothing to do. | 2189 ** In this case (db->nStatement==0), and there is nothing to do. |
1989 */ | 2190 */ |
1990 if( db->nStatement && p->iStatement ){ | 2191 if( db->nStatement && p->iStatement ){ |
1991 int i; | 2192 int i; |
1992 const int iSavepoint = p->iStatement-1; | 2193 const int iSavepoint = p->iStatement-1; |
1993 | 2194 |
1994 assert( eOp==SAVEPOINT_ROLLBACK || eOp==SAVEPOINT_RELEASE); | 2195 assert( eOp==SAVEPOINT_ROLLBACK || eOp==SAVEPOINT_RELEASE); |
1995 assert( db->nStatement>0 ); | 2196 assert( db->nStatement>0 ); |
1996 assert( p->iStatement==(db->nStatement+db->nSavepoint) ); | 2197 assert( p->iStatement==(db->nStatement+db->nSavepoint) ); |
1997 | 2198 |
1998 for(i=0; i<db->nDb; i++){ | 2199 for(i=0; i<db->nDb; i++){ |
1999 int rc2 = SQLITE_OK; | 2200 int rc2 = SQLITE_OK; |
2000 Btree *pBt = db->aDb[i].pBt; | 2201 Btree *pBt = db->aDb[i].pBt; |
2001 if( pBt ){ | 2202 if( pBt ){ |
2002 if( eOp==SAVEPOINT_ROLLBACK ){ | 2203 if( eOp==SAVEPOINT_ROLLBACK ){ |
2003 rc2 = sqlite3BtreeSavepoint(pBt, SAVEPOINT_ROLLBACK, iSavepoint); | 2204 rc2 = sqlite3BtreeSavepoint(pBt, SAVEPOINT_ROLLBACK, iSavepoint); |
2004 } | 2205 } |
2005 if( rc2==SQLITE_OK ){ | 2206 if( rc2==SQLITE_OK ){ |
2006 rc2 = sqlite3BtreeSavepoint(pBt, SAVEPOINT_RELEASE, iSavepoint); | 2207 rc2 = sqlite3BtreeSavepoint(pBt, SAVEPOINT_RELEASE, iSavepoint); |
2007 } | 2208 } |
2008 if( rc==SQLITE_OK ){ | 2209 if( rc==SQLITE_OK ){ |
2009 rc = rc2; | 2210 rc = rc2; |
2010 } | 2211 } |
2011 } | 2212 } |
2012 } | 2213 } |
2013 db->nStatement--; | 2214 db->nStatement--; |
2014 p->iStatement = 0; | 2215 p->iStatement = 0; |
2015 | 2216 |
| 2217 if( rc==SQLITE_OK ){ |
| 2218 if( eOp==SAVEPOINT_ROLLBACK ){ |
| 2219 rc = sqlite3VtabSavepoint(db, SAVEPOINT_ROLLBACK, iSavepoint); |
| 2220 } |
| 2221 if( rc==SQLITE_OK ){ |
| 2222 rc = sqlite3VtabSavepoint(db, SAVEPOINT_RELEASE, iSavepoint); |
| 2223 } |
| 2224 } |
| 2225 |
2016 /* If the statement transaction is being rolled back, also restore the | 2226 /* If the statement transaction is being rolled back, also restore the |
2017 ** database handles deferred constraint counter to the value it had when | 2227 ** database handles deferred constraint counter to the value it had when |
2018 ** the statement transaction was opened. */ | 2228 ** the statement transaction was opened. */ |
2019 if( eOp==SAVEPOINT_ROLLBACK ){ | 2229 if( eOp==SAVEPOINT_ROLLBACK ){ |
2020 db->nDeferredCons = p->nStmtDefCons; | 2230 db->nDeferredCons = p->nStmtDefCons; |
| 2231 db->nDeferredImmCons = p->nStmtDefImmCons; |
2021 } | 2232 } |
2022 } | 2233 } |
2023 return rc; | 2234 return rc; |
2024 } | 2235 } |
2025 | 2236 |
2026 /* | 2237 /* |
2027 ** This function is called when a transaction opened by the database | 2238 ** This function is called when a transaction opened by the database |
2028 ** handle associated with the VM passed as an argument is about to be | 2239 ** handle associated with the VM passed as an argument is about to be |
2029 ** committed. If there are outstanding deferred foreign key constraint | 2240 ** committed. If there are outstanding deferred foreign key constraint |
2030 ** violations, return SQLITE_ERROR. Otherwise, SQLITE_OK. | 2241 ** violations, return SQLITE_ERROR. Otherwise, SQLITE_OK. |
2031 ** | 2242 ** |
2032 ** If there are outstanding FK violations and this function returns | 2243 ** If there are outstanding FK violations and this function returns |
2033 ** SQLITE_ERROR, set the result of the VM to SQLITE_CONSTRAINT and write | 2244 ** SQLITE_ERROR, set the result of the VM to SQLITE_CONSTRAINT_FOREIGNKEY |
2034 ** an error message to it. Then return SQLITE_ERROR. | 2245 ** and write an error message to it. Then return SQLITE_ERROR. |
2035 */ | 2246 */ |
2036 #ifndef SQLITE_OMIT_FOREIGN_KEY | 2247 #ifndef SQLITE_OMIT_FOREIGN_KEY |
2037 int sqlite3VdbeCheckFk(Vdbe *p, int deferred){ | 2248 int sqlite3VdbeCheckFk(Vdbe *p, int deferred){ |
2038 sqlite3 *db = p->db; | 2249 sqlite3 *db = p->db; |
2039 if( (deferred && db->nDeferredCons>0) || (!deferred && p->nFkConstraint>0) ){ | 2250 if( (deferred && (db->nDeferredCons+db->nDeferredImmCons)>0) |
2040 p->rc = SQLITE_CONSTRAINT; | 2251 || (!deferred && p->nFkConstraint>0) |
| 2252 ){ |
| 2253 p->rc = SQLITE_CONSTRAINT_FOREIGNKEY; |
2041 p->errorAction = OE_Abort; | 2254 p->errorAction = OE_Abort; |
2042 sqlite3SetString(&p->zErrMsg, db, "foreign key constraint failed"); | 2255 sqlite3SetString(&p->zErrMsg, db, "FOREIGN KEY constraint failed"); |
2043 return SQLITE_ERROR; | 2256 return SQLITE_ERROR; |
2044 } | 2257 } |
2045 return SQLITE_OK; | 2258 return SQLITE_OK; |
2046 } | 2259 } |
2047 #endif | 2260 #endif |
2048 | 2261 |
2049 /* | 2262 /* |
2050 ** This routine is called the when a VDBE tries to halt. If the VDBE | 2263 ** This routine is called the when a VDBE tries to halt. If the VDBE |
2051 ** has made changes and is in autocommit mode, then commit those | 2264 ** has made changes and is in autocommit mode, then commit those |
2052 ** changes. If a rollback is needed, then do the rollback. | 2265 ** changes. If a rollback is needed, then do the rollback. |
(...skipping 22 matching lines...) Expand all Loading... |
2075 ** SQLITE_INTERRUPT | 2288 ** SQLITE_INTERRUPT |
2076 ** | 2289 ** |
2077 ** Then the internal cache might have been left in an inconsistent | 2290 ** Then the internal cache might have been left in an inconsistent |
2078 ** state. We need to rollback the statement transaction, if there is | 2291 ** state. We need to rollback the statement transaction, if there is |
2079 ** one, or the complete transaction if there is no statement transaction. | 2292 ** one, or the complete transaction if there is no statement transaction. |
2080 */ | 2293 */ |
2081 | 2294 |
2082 if( p->db->mallocFailed ){ | 2295 if( p->db->mallocFailed ){ |
2083 p->rc = SQLITE_NOMEM; | 2296 p->rc = SQLITE_NOMEM; |
2084 } | 2297 } |
| 2298 if( p->aOnceFlag ) memset(p->aOnceFlag, 0, p->nOnceFlag); |
2085 closeAllCursors(p); | 2299 closeAllCursors(p); |
2086 if( p->magic!=VDBE_MAGIC_RUN ){ | 2300 if( p->magic!=VDBE_MAGIC_RUN ){ |
2087 return SQLITE_OK; | 2301 return SQLITE_OK; |
2088 } | 2302 } |
2089 checkActiveVdbeCnt(db); | 2303 checkActiveVdbeCnt(db); |
2090 | 2304 |
2091 /* No commit or rollback needed if the program never started */ | 2305 /* No commit or rollback needed if the program never started or if the |
2092 if( p->pc>=0 ){ | 2306 ** SQL statement does not read or write a database file. */ |
| 2307 if( p->pc>=0 && p->bIsReader ){ |
2093 int mrc; /* Primary error code from p->rc */ | 2308 int mrc; /* Primary error code from p->rc */ |
2094 int eStatementOp = 0; | 2309 int eStatementOp = 0; |
2095 int isSpecialError; /* Set to true if a 'special' error */ | 2310 int isSpecialError; /* Set to true if a 'special' error */ |
2096 | 2311 |
2097 /* Lock all btrees used by the statement */ | 2312 /* Lock all btrees used by the statement */ |
2098 sqlite3VdbeEnter(p); | 2313 sqlite3VdbeEnter(p); |
2099 | 2314 |
2100 /* Check for one of the special errors */ | 2315 /* Check for one of the special errors */ |
2101 mrc = p->rc & 0xff; | 2316 mrc = p->rc & 0xff; |
2102 assert( p->rc!=SQLITE_IOERR_BLOCKED ); /* This error no longer exists */ | |
2103 isSpecialError = mrc==SQLITE_NOMEM || mrc==SQLITE_IOERR | 2317 isSpecialError = mrc==SQLITE_NOMEM || mrc==SQLITE_IOERR |
2104 || mrc==SQLITE_INTERRUPT || mrc==SQLITE_FULL; | 2318 || mrc==SQLITE_INTERRUPT || mrc==SQLITE_FULL; |
2105 if( isSpecialError ){ | 2319 if( isSpecialError ){ |
2106 /* If the query was read-only and the error code is SQLITE_INTERRUPT, | 2320 /* If the query was read-only and the error code is SQLITE_INTERRUPT, |
2107 ** no rollback is necessary. Otherwise, at least a savepoint | 2321 ** no rollback is necessary. Otherwise, at least a savepoint |
2108 ** transaction must be rolled back to restore the database to a | 2322 ** transaction must be rolled back to restore the database to a |
2109 ** consistent state. | 2323 ** consistent state. |
2110 ** | 2324 ** |
2111 ** Even if the statement is read-only, it is important to perform | 2325 ** Even if the statement is read-only, it is important to perform |
2112 ** a statement or transaction rollback operation. If the error | 2326 ** a statement or transaction rollback operation. If the error |
2113 ** occured while writing to the journal, sub-journal or database | 2327 ** occurred while writing to the journal, sub-journal or database |
2114 ** file as part of an effort to free up cache space (see function | 2328 ** file as part of an effort to free up cache space (see function |
2115 ** pagerStress() in pager.c), the rollback is required to restore | 2329 ** pagerStress() in pager.c), the rollback is required to restore |
2116 ** the pager to a consistent state. | 2330 ** the pager to a consistent state. |
2117 */ | 2331 */ |
2118 if( !p->readOnly || mrc!=SQLITE_INTERRUPT ){ | 2332 if( !p->readOnly || mrc!=SQLITE_INTERRUPT ){ |
2119 if( (mrc==SQLITE_NOMEM || mrc==SQLITE_FULL) && p->usesStmtJournal ){ | 2333 if( (mrc==SQLITE_NOMEM || mrc==SQLITE_FULL) && p->usesStmtJournal ){ |
2120 eStatementOp = SAVEPOINT_ROLLBACK; | 2334 eStatementOp = SAVEPOINT_ROLLBACK; |
2121 }else{ | 2335 }else{ |
2122 /* We are forced to roll back the active transaction. Before doing | 2336 /* We are forced to roll back the active transaction. Before doing |
2123 ** so, abort any other statements this handle currently has active. | 2337 ** so, abort any other statements this handle currently has active. |
2124 */ | 2338 */ |
2125 invalidateCursorsOnModifiedBtrees(db); | 2339 sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK); |
2126 sqlite3RollbackAll(db); | |
2127 sqlite3CloseSavepoints(db); | 2340 sqlite3CloseSavepoints(db); |
2128 db->autoCommit = 1; | 2341 db->autoCommit = 1; |
2129 } | 2342 } |
2130 } | 2343 } |
2131 } | 2344 } |
2132 | 2345 |
2133 /* Check for immediate foreign key violations. */ | 2346 /* Check for immediate foreign key violations. */ |
2134 if( p->rc==SQLITE_OK ){ | 2347 if( p->rc==SQLITE_OK ){ |
2135 sqlite3VdbeCheckFk(p, 0); | 2348 sqlite3VdbeCheckFk(p, 0); |
2136 } | 2349 } |
2137 | 2350 |
2138 /* If the auto-commit flag is set and this is the only active writer | 2351 /* If the auto-commit flag is set and this is the only active writer |
2139 ** VM, then we do either a commit or rollback of the current transaction. | 2352 ** VM, then we do either a commit or rollback of the current transaction. |
2140 ** | 2353 ** |
2141 ** Note: This block also runs if one of the special errors handled | 2354 ** Note: This block also runs if one of the special errors handled |
2142 ** above has occurred. | 2355 ** above has occurred. |
2143 */ | 2356 */ |
2144 if( !sqlite3VtabInSync(db) | 2357 if( !sqlite3VtabInSync(db) |
2145 && db->autoCommit | 2358 && db->autoCommit |
2146 && db->writeVdbeCnt==(p->readOnly==0) | 2359 && db->nVdbeWrite==(p->readOnly==0) |
2147 ){ | 2360 ){ |
2148 if( p->rc==SQLITE_OK || (p->errorAction==OE_Fail && !isSpecialError) ){ | 2361 if( p->rc==SQLITE_OK || (p->errorAction==OE_Fail && !isSpecialError) ){ |
2149 rc = sqlite3VdbeCheckFk(p, 1); | 2362 rc = sqlite3VdbeCheckFk(p, 1); |
2150 if( rc!=SQLITE_OK ){ | 2363 if( rc!=SQLITE_OK ){ |
2151 if( NEVER(p->readOnly) ){ | 2364 if( NEVER(p->readOnly) ){ |
2152 sqlite3VdbeLeave(p); | 2365 sqlite3VdbeLeave(p); |
2153 return SQLITE_ERROR; | 2366 return SQLITE_ERROR; |
2154 } | 2367 } |
2155 rc = SQLITE_CONSTRAINT; | 2368 rc = SQLITE_CONSTRAINT_FOREIGNKEY; |
2156 }else{ | 2369 }else{ |
2157 /* The auto-commit flag is true, the vdbe program was successful | 2370 /* The auto-commit flag is true, the vdbe program was successful |
2158 ** or hit an 'OR FAIL' constraint and there are no deferred foreign | 2371 ** or hit an 'OR FAIL' constraint and there are no deferred foreign |
2159 ** key constraints to hold up the transaction. This means a commit | 2372 ** key constraints to hold up the transaction. This means a commit |
2160 ** is required. */ | 2373 ** is required. */ |
2161 rc = vdbeCommit(db, p); | 2374 rc = vdbeCommit(db, p); |
2162 } | 2375 } |
2163 if( rc==SQLITE_BUSY && p->readOnly ){ | 2376 if( rc==SQLITE_BUSY && p->readOnly ){ |
2164 sqlite3VdbeLeave(p); | 2377 sqlite3VdbeLeave(p); |
2165 return SQLITE_BUSY; | 2378 return SQLITE_BUSY; |
2166 }else if( rc!=SQLITE_OK ){ | 2379 }else if( rc!=SQLITE_OK ){ |
2167 p->rc = rc; | 2380 p->rc = rc; |
2168 sqlite3RollbackAll(db); | 2381 sqlite3RollbackAll(db, SQLITE_OK); |
2169 }else{ | 2382 }else{ |
2170 db->nDeferredCons = 0; | 2383 db->nDeferredCons = 0; |
| 2384 db->nDeferredImmCons = 0; |
| 2385 db->flags &= ~SQLITE_DeferFKs; |
2171 sqlite3CommitInternalChanges(db); | 2386 sqlite3CommitInternalChanges(db); |
2172 } | 2387 } |
2173 }else{ | 2388 }else{ |
2174 sqlite3RollbackAll(db); | 2389 sqlite3RollbackAll(db, SQLITE_OK); |
2175 } | 2390 } |
2176 db->nStatement = 0; | 2391 db->nStatement = 0; |
2177 }else if( eStatementOp==0 ){ | 2392 }else if( eStatementOp==0 ){ |
2178 if( p->rc==SQLITE_OK || p->errorAction==OE_Fail ){ | 2393 if( p->rc==SQLITE_OK || p->errorAction==OE_Fail ){ |
2179 eStatementOp = SAVEPOINT_RELEASE; | 2394 eStatementOp = SAVEPOINT_RELEASE; |
2180 }else if( p->errorAction==OE_Abort ){ | 2395 }else if( p->errorAction==OE_Abort ){ |
2181 eStatementOp = SAVEPOINT_ROLLBACK; | 2396 eStatementOp = SAVEPOINT_ROLLBACK; |
2182 }else{ | 2397 }else{ |
2183 invalidateCursorsOnModifiedBtrees(db); | 2398 sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK); |
2184 sqlite3RollbackAll(db); | |
2185 sqlite3CloseSavepoints(db); | 2399 sqlite3CloseSavepoints(db); |
2186 db->autoCommit = 1; | 2400 db->autoCommit = 1; |
2187 } | 2401 } |
2188 } | 2402 } |
2189 | 2403 |
2190 /* If eStatementOp is non-zero, then a statement transaction needs to | 2404 /* If eStatementOp is non-zero, then a statement transaction needs to |
2191 ** be committed or rolled back. Call sqlite3VdbeCloseStatement() to | 2405 ** be committed or rolled back. Call sqlite3VdbeCloseStatement() to |
2192 ** do so. If this operation returns an error, and the current statement | 2406 ** do so. If this operation returns an error, and the current statement |
2193 ** error code is SQLITE_OK or SQLITE_CONSTRAINT, then promote the | 2407 ** error code is SQLITE_OK or SQLITE_CONSTRAINT, then promote the |
2194 ** current statement error code. | 2408 ** current statement error code. |
2195 ** | |
2196 ** Note that sqlite3VdbeCloseStatement() can only fail if eStatementOp | |
2197 ** is SAVEPOINT_ROLLBACK. But if p->rc==SQLITE_OK then eStatementOp | |
2198 ** must be SAVEPOINT_RELEASE. Hence the NEVER(p->rc==SQLITE_OK) in | |
2199 ** the following code. | |
2200 */ | 2409 */ |
2201 if( eStatementOp ){ | 2410 if( eStatementOp ){ |
2202 rc = sqlite3VdbeCloseStatement(p, eStatementOp); | 2411 rc = sqlite3VdbeCloseStatement(p, eStatementOp); |
2203 if( rc ){ | 2412 if( rc ){ |
2204 assert( eStatementOp==SAVEPOINT_ROLLBACK ); | 2413 if( p->rc==SQLITE_OK || (p->rc&0xff)==SQLITE_CONSTRAINT ){ |
2205 if( NEVER(p->rc==SQLITE_OK) || p->rc==SQLITE_CONSTRAINT ){ | |
2206 p->rc = rc; | 2414 p->rc = rc; |
2207 sqlite3DbFree(db, p->zErrMsg); | 2415 sqlite3DbFree(db, p->zErrMsg); |
2208 p->zErrMsg = 0; | 2416 p->zErrMsg = 0; |
2209 } | 2417 } |
2210 invalidateCursorsOnModifiedBtrees(db); | 2418 sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK); |
2211 sqlite3RollbackAll(db); | |
2212 sqlite3CloseSavepoints(db); | 2419 sqlite3CloseSavepoints(db); |
2213 db->autoCommit = 1; | 2420 db->autoCommit = 1; |
2214 } | 2421 } |
2215 } | 2422 } |
2216 | 2423 |
2217 /* If this was an INSERT, UPDATE or DELETE and no statement transaction | 2424 /* If this was an INSERT, UPDATE or DELETE and no statement transaction |
2218 ** has been rolled back, update the database connection change-counter. | 2425 ** has been rolled back, update the database connection change-counter. |
2219 */ | 2426 */ |
2220 if( p->changeCntOn ){ | 2427 if( p->changeCntOn ){ |
2221 if( eStatementOp!=SAVEPOINT_ROLLBACK ){ | 2428 if( eStatementOp!=SAVEPOINT_ROLLBACK ){ |
2222 sqlite3VdbeSetChanges(db, p->nChange); | 2429 sqlite3VdbeSetChanges(db, p->nChange); |
2223 }else{ | 2430 }else{ |
2224 sqlite3VdbeSetChanges(db, 0); | 2431 sqlite3VdbeSetChanges(db, 0); |
2225 } | 2432 } |
2226 p->nChange = 0; | 2433 p->nChange = 0; |
2227 } | 2434 } |
2228 | |
2229 /* Rollback or commit any schema changes that occurred. */ | |
2230 if( p->rc!=SQLITE_OK && db->flags&SQLITE_InternChanges ){ | |
2231 sqlite3ResetInternalSchema(db, -1); | |
2232 db->flags = (db->flags | SQLITE_InternChanges); | |
2233 } | |
2234 | 2435 |
2235 /* Release the locks */ | 2436 /* Release the locks */ |
2236 sqlite3VdbeLeave(p); | 2437 sqlite3VdbeLeave(p); |
2237 } | 2438 } |
2238 | 2439 |
2239 /* We have successfully halted and closed the VM. Record this fact. */ | 2440 /* We have successfully halted and closed the VM. Record this fact. */ |
2240 if( p->pc>=0 ){ | 2441 if( p->pc>=0 ){ |
2241 db->activeVdbeCnt--; | 2442 db->nVdbeActive--; |
2242 if( !p->readOnly ){ | 2443 if( !p->readOnly ) db->nVdbeWrite--; |
2243 db->writeVdbeCnt--; | 2444 if( p->bIsReader ) db->nVdbeRead--; |
2244 } | 2445 assert( db->nVdbeActive>=db->nVdbeRead ); |
2245 assert( db->activeVdbeCnt>=db->writeVdbeCnt ); | 2446 assert( db->nVdbeRead>=db->nVdbeWrite ); |
| 2447 assert( db->nVdbeWrite>=0 ); |
2246 } | 2448 } |
2247 p->magic = VDBE_MAGIC_HALT; | 2449 p->magic = VDBE_MAGIC_HALT; |
2248 checkActiveVdbeCnt(db); | 2450 checkActiveVdbeCnt(db); |
2249 if( p->db->mallocFailed ){ | 2451 if( p->db->mallocFailed ){ |
2250 p->rc = SQLITE_NOMEM; | 2452 p->rc = SQLITE_NOMEM; |
2251 } | 2453 } |
2252 | 2454 |
2253 /* If the auto-commit flag is set to true, then any locks that were held | 2455 /* If the auto-commit flag is set to true, then any locks that were held |
2254 ** by connection db have now been released. Call sqlite3ConnectionUnlocked() | 2456 ** by connection db have now been released. Call sqlite3ConnectionUnlocked() |
2255 ** to invoke any required unlock-notify callbacks. | 2457 ** to invoke any required unlock-notify callbacks. |
2256 */ | 2458 */ |
2257 if( db->autoCommit ){ | 2459 if( db->autoCommit ){ |
2258 sqlite3ConnectionUnlocked(db); | 2460 sqlite3ConnectionUnlocked(db); |
2259 } | 2461 } |
2260 | 2462 |
2261 assert( db->activeVdbeCnt>0 || db->autoCommit==0 || db->nStatement==0 ); | 2463 assert( db->nVdbeActive>0 || db->autoCommit==0 || db->nStatement==0 ); |
2262 return (p->rc==SQLITE_BUSY ? SQLITE_BUSY : SQLITE_OK); | 2464 return (p->rc==SQLITE_BUSY ? SQLITE_BUSY : SQLITE_OK); |
2263 } | 2465 } |
2264 | 2466 |
2265 | 2467 |
2266 /* | 2468 /* |
2267 ** Each VDBE holds the result of the most recent sqlite3_step() call | 2469 ** Each VDBE holds the result of the most recent sqlite3_step() call |
2268 ** in p->rc. This routine sets that result back to SQLITE_OK. | 2470 ** in p->rc. This routine sets that result back to SQLITE_OK. |
2269 */ | 2471 */ |
2270 void sqlite3VdbeResetStepResult(Vdbe *p){ | 2472 void sqlite3VdbeResetStepResult(Vdbe *p){ |
2271 p->rc = SQLITE_OK; | 2473 p->rc = SQLITE_OK; |
2272 } | 2474 } |
2273 | 2475 |
2274 /* | 2476 /* |
| 2477 ** Copy the error code and error message belonging to the VDBE passed |
| 2478 ** as the first argument to its database handle (so that they will be |
| 2479 ** returned by calls to sqlite3_errcode() and sqlite3_errmsg()). |
| 2480 ** |
| 2481 ** This function does not clear the VDBE error code or message, just |
| 2482 ** copies them to the database handle. |
| 2483 */ |
| 2484 int sqlite3VdbeTransferError(Vdbe *p){ |
| 2485 sqlite3 *db = p->db; |
| 2486 int rc = p->rc; |
| 2487 if( p->zErrMsg ){ |
| 2488 u8 mallocFailed = db->mallocFailed; |
| 2489 sqlite3BeginBenignMalloc(); |
| 2490 if( db->pErr==0 ) db->pErr = sqlite3ValueNew(db); |
| 2491 sqlite3ValueSetStr(db->pErr, -1, p->zErrMsg, SQLITE_UTF8, SQLITE_TRANSIENT); |
| 2492 sqlite3EndBenignMalloc(); |
| 2493 db->mallocFailed = mallocFailed; |
| 2494 db->errCode = rc; |
| 2495 }else{ |
| 2496 sqlite3Error(db, rc); |
| 2497 } |
| 2498 return rc; |
| 2499 } |
| 2500 |
| 2501 #ifdef SQLITE_ENABLE_SQLLOG |
| 2502 /* |
| 2503 ** If an SQLITE_CONFIG_SQLLOG hook is registered and the VM has been run, |
| 2504 ** invoke it. |
| 2505 */ |
| 2506 static void vdbeInvokeSqllog(Vdbe *v){ |
| 2507 if( sqlite3GlobalConfig.xSqllog && v->rc==SQLITE_OK && v->zSql && v->pc>=0 ){ |
| 2508 char *zExpanded = sqlite3VdbeExpandSql(v, v->zSql); |
| 2509 assert( v->db->init.busy==0 ); |
| 2510 if( zExpanded ){ |
| 2511 sqlite3GlobalConfig.xSqllog( |
| 2512 sqlite3GlobalConfig.pSqllogArg, v->db, zExpanded, 1 |
| 2513 ); |
| 2514 sqlite3DbFree(v->db, zExpanded); |
| 2515 } |
| 2516 } |
| 2517 } |
| 2518 #else |
| 2519 # define vdbeInvokeSqllog(x) |
| 2520 #endif |
| 2521 |
| 2522 /* |
2275 ** Clean up a VDBE after execution but do not delete the VDBE just yet. | 2523 ** Clean up a VDBE after execution but do not delete the VDBE just yet. |
2276 ** Write any error messages into *pzErrMsg. Return the result code. | 2524 ** Write any error messages into *pzErrMsg. Return the result code. |
2277 ** | 2525 ** |
2278 ** After this routine is run, the VDBE should be ready to be executed | 2526 ** After this routine is run, the VDBE should be ready to be executed |
2279 ** again. | 2527 ** again. |
2280 ** | 2528 ** |
2281 ** To look at it another way, this routine resets the state of the | 2529 ** To look at it another way, this routine resets the state of the |
2282 ** virtual machine from VDBE_MAGIC_RUN or VDBE_MAGIC_HALT back to | 2530 ** virtual machine from VDBE_MAGIC_RUN or VDBE_MAGIC_HALT back to |
2283 ** VDBE_MAGIC_INIT. | 2531 ** VDBE_MAGIC_INIT. |
2284 */ | 2532 */ |
2285 int sqlite3VdbeReset(Vdbe *p){ | 2533 int sqlite3VdbeReset(Vdbe *p){ |
2286 sqlite3 *db; | 2534 sqlite3 *db; |
2287 db = p->db; | 2535 db = p->db; |
2288 | 2536 |
2289 /* If the VM did not run to completion or if it encountered an | 2537 /* If the VM did not run to completion or if it encountered an |
2290 ** error, then it might not have been halted properly. So halt | 2538 ** error, then it might not have been halted properly. So halt |
2291 ** it now. | 2539 ** it now. |
2292 */ | 2540 */ |
2293 sqlite3VdbeHalt(p); | 2541 sqlite3VdbeHalt(p); |
2294 | 2542 |
2295 /* If the VDBE has be run even partially, then transfer the error code | 2543 /* If the VDBE has be run even partially, then transfer the error code |
2296 ** and error message from the VDBE into the main database structure. But | 2544 ** and error message from the VDBE into the main database structure. But |
2297 ** if the VDBE has just been set to run but has not actually executed any | 2545 ** if the VDBE has just been set to run but has not actually executed any |
2298 ** instructions yet, leave the main database error information unchanged. | 2546 ** instructions yet, leave the main database error information unchanged. |
2299 */ | 2547 */ |
2300 if( p->pc>=0 ){ | 2548 if( p->pc>=0 ){ |
2301 if( p->zErrMsg ){ | 2549 vdbeInvokeSqllog(p); |
2302 sqlite3BeginBenignMalloc(); | 2550 sqlite3VdbeTransferError(p); |
2303 sqlite3ValueSetStr(db->pErr,-1,p->zErrMsg,SQLITE_UTF8,SQLITE_TRANSIENT); | 2551 sqlite3DbFree(db, p->zErrMsg); |
2304 sqlite3EndBenignMalloc(); | 2552 p->zErrMsg = 0; |
2305 db->errCode = p->rc; | |
2306 sqlite3DbFree(db, p->zErrMsg); | |
2307 p->zErrMsg = 0; | |
2308 }else if( p->rc ){ | |
2309 sqlite3Error(db, p->rc, 0); | |
2310 }else{ | |
2311 sqlite3Error(db, SQLITE_OK, 0); | |
2312 } | |
2313 if( p->runOnlyOnce ) p->expired = 1; | 2553 if( p->runOnlyOnce ) p->expired = 1; |
2314 }else if( p->rc && p->expired ){ | 2554 }else if( p->rc && p->expired ){ |
2315 /* The expired flag was set on the VDBE before the first call | 2555 /* The expired flag was set on the VDBE before the first call |
2316 ** to sqlite3_step(). For consistency (since sqlite3_step() was | 2556 ** to sqlite3_step(). For consistency (since sqlite3_step() was |
2317 ** called), set the database error in this case as well. | 2557 ** called), set the database error in this case as well. |
2318 */ | 2558 */ |
2319 sqlite3Error(db, p->rc, 0); | 2559 sqlite3ErrorWithMsg(db, p->rc, p->zErrMsg ? "%s" : 0, p->zErrMsg); |
2320 sqlite3ValueSetStr(db->pErr, -1, p->zErrMsg, SQLITE_UTF8, SQLITE_TRANSIENT); | |
2321 sqlite3DbFree(db, p->zErrMsg); | 2560 sqlite3DbFree(db, p->zErrMsg); |
2322 p->zErrMsg = 0; | 2561 p->zErrMsg = 0; |
2323 } | 2562 } |
2324 | 2563 |
2325 /* Reclaim all memory used by the VDBE | 2564 /* Reclaim all memory used by the VDBE |
2326 */ | 2565 */ |
2327 Cleanup(p); | 2566 Cleanup(p); |
2328 | 2567 |
2329 /* Save profiling information from this VDBE run. | 2568 /* Save profiling information from this VDBE run. |
2330 */ | 2569 */ |
2331 #ifdef VDBE_PROFILE | 2570 #ifdef VDBE_PROFILE |
2332 { | 2571 { |
2333 FILE *out = fopen("vdbe_profile.out", "a"); | 2572 FILE *out = fopen("vdbe_profile.out", "a"); |
2334 if( out ){ | 2573 if( out ){ |
2335 int i; | 2574 int i; |
2336 fprintf(out, "---- "); | 2575 fprintf(out, "---- "); |
2337 for(i=0; i<p->nOp; i++){ | 2576 for(i=0; i<p->nOp; i++){ |
2338 fprintf(out, "%02x", p->aOp[i].opcode); | 2577 fprintf(out, "%02x", p->aOp[i].opcode); |
2339 } | 2578 } |
2340 fprintf(out, "\n"); | 2579 fprintf(out, "\n"); |
| 2580 if( p->zSql ){ |
| 2581 char c, pc = 0; |
| 2582 fprintf(out, "-- "); |
| 2583 for(i=0; (c = p->zSql[i])!=0; i++){ |
| 2584 if( pc=='\n' ) fprintf(out, "-- "); |
| 2585 putc(c, out); |
| 2586 pc = c; |
| 2587 } |
| 2588 if( pc!='\n' ) fprintf(out, "\n"); |
| 2589 } |
2341 for(i=0; i<p->nOp; i++){ | 2590 for(i=0; i<p->nOp; i++){ |
2342 fprintf(out, "%6d %10lld %8lld ", | 2591 char zHdr[100]; |
| 2592 sqlite3_snprintf(sizeof(zHdr), zHdr, "%6u %12llu %8llu ", |
2343 p->aOp[i].cnt, | 2593 p->aOp[i].cnt, |
2344 p->aOp[i].cycles, | 2594 p->aOp[i].cycles, |
2345 p->aOp[i].cnt>0 ? p->aOp[i].cycles/p->aOp[i].cnt : 0 | 2595 p->aOp[i].cnt>0 ? p->aOp[i].cycles/p->aOp[i].cnt : 0 |
2346 ); | 2596 ); |
| 2597 fprintf(out, "%s", zHdr); |
2347 sqlite3VdbePrintOp(out, i, &p->aOp[i]); | 2598 sqlite3VdbePrintOp(out, i, &p->aOp[i]); |
2348 } | 2599 } |
2349 fclose(out); | 2600 fclose(out); |
2350 } | 2601 } |
2351 } | 2602 } |
2352 #endif | 2603 #endif |
| 2604 p->iCurrentTime = 0; |
2353 p->magic = VDBE_MAGIC_INIT; | 2605 p->magic = VDBE_MAGIC_INIT; |
2354 return p->rc & db->errMask; | 2606 return p->rc & db->errMask; |
2355 } | 2607 } |
2356 | 2608 |
2357 /* | 2609 /* |
2358 ** Clean up and delete a VDBE after execution. Return an integer which is | 2610 ** Clean up and delete a VDBE after execution. Return an integer which is |
2359 ** the result code. Write any error message text into *pzErrMsg. | 2611 ** the result code. Write any error message text into *pzErrMsg. |
2360 */ | 2612 */ |
2361 int sqlite3VdbeFinalize(Vdbe *p){ | 2613 int sqlite3VdbeFinalize(Vdbe *p){ |
2362 int rc = SQLITE_OK; | 2614 int rc = SQLITE_OK; |
2363 if( p->magic==VDBE_MAGIC_RUN || p->magic==VDBE_MAGIC_HALT ){ | 2615 if( p->magic==VDBE_MAGIC_RUN || p->magic==VDBE_MAGIC_HALT ){ |
2364 rc = sqlite3VdbeReset(p); | 2616 rc = sqlite3VdbeReset(p); |
2365 assert( (rc & p->db->errMask)==rc ); | 2617 assert( (rc & p->db->errMask)==rc ); |
2366 } | 2618 } |
2367 sqlite3VdbeDelete(p); | 2619 sqlite3VdbeDelete(p); |
2368 return rc; | 2620 return rc; |
2369 } | 2621 } |
2370 | 2622 |
2371 /* | 2623 /* |
2372 ** Call the destructor for each auxdata entry in pVdbeFunc for which | 2624 ** If parameter iOp is less than zero, then invoke the destructor for |
2373 ** the corresponding bit in mask is clear. Auxdata entries beyond 31 | 2625 ** all auxiliary data pointers currently cached by the VM passed as |
2374 ** are always destroyed. To destroy all auxdata entries, call this | 2626 ** the first argument. |
2375 ** routine with mask==0. | 2627 ** |
| 2628 ** Or, if iOp is greater than or equal to zero, then the destructor is |
| 2629 ** only invoked for those auxiliary data pointers created by the user |
| 2630 ** function invoked by the OP_Function opcode at instruction iOp of |
| 2631 ** VM pVdbe, and only then if: |
| 2632 ** |
| 2633 ** * the associated function parameter is the 32nd or later (counting |
| 2634 ** from left to right), or |
| 2635 ** |
| 2636 ** * the corresponding bit in argument mask is clear (where the first |
| 2637 ** function parameter corresponds to bit 0 etc.). |
2376 */ | 2638 */ |
2377 void sqlite3VdbeDeleteAuxData(VdbeFunc *pVdbeFunc, int mask){ | 2639 void sqlite3VdbeDeleteAuxData(Vdbe *pVdbe, int iOp, int mask){ |
2378 int i; | 2640 AuxData **pp = &pVdbe->pAuxData; |
2379 for(i=0; i<pVdbeFunc->nAux; i++){ | 2641 while( *pp ){ |
2380 struct AuxData *pAux = &pVdbeFunc->apAux[i]; | 2642 AuxData *pAux = *pp; |
2381 if( (i>31 || !(mask&(((u32)1)<<i))) && pAux->pAux ){ | 2643 if( (iOp<0) |
| 2644 || (pAux->iOp==iOp && (pAux->iArg>31 || !(mask & MASKBIT32(pAux->iArg)))) |
| 2645 ){ |
| 2646 testcase( pAux->iArg==31 ); |
2382 if( pAux->xDelete ){ | 2647 if( pAux->xDelete ){ |
2383 pAux->xDelete(pAux->pAux); | 2648 pAux->xDelete(pAux->pAux); |
2384 } | 2649 } |
2385 pAux->pAux = 0; | 2650 *pp = pAux->pNext; |
| 2651 sqlite3DbFree(pVdbe->db, pAux); |
| 2652 }else{ |
| 2653 pp= &pAux->pNext; |
2386 } | 2654 } |
2387 } | 2655 } |
2388 } | 2656 } |
2389 | 2657 |
2390 /* | 2658 /* |
2391 ** Free all memory associated with the Vdbe passed as the second argument. | 2659 ** Free all memory associated with the Vdbe passed as the second argument, |
| 2660 ** except for object itself, which is preserved. |
| 2661 ** |
2392 ** The difference between this function and sqlite3VdbeDelete() is that | 2662 ** The difference between this function and sqlite3VdbeDelete() is that |
2393 ** VdbeDelete() also unlinks the Vdbe from the list of VMs associated with | 2663 ** VdbeDelete() also unlinks the Vdbe from the list of VMs associated with |
2394 ** the database connection. | 2664 ** the database connection and frees the object itself. |
2395 */ | 2665 */ |
2396 void sqlite3VdbeDeleteObject(sqlite3 *db, Vdbe *p){ | 2666 void sqlite3VdbeClearObject(sqlite3 *db, Vdbe *p){ |
2397 SubProgram *pSub, *pNext; | 2667 SubProgram *pSub, *pNext; |
| 2668 int i; |
2398 assert( p->db==0 || p->db==db ); | 2669 assert( p->db==0 || p->db==db ); |
2399 releaseMemArray(p->aVar, p->nVar); | 2670 releaseMemArray(p->aVar, p->nVar); |
2400 releaseMemArray(p->aColName, p->nResColumn*COLNAME_N); | 2671 releaseMemArray(p->aColName, p->nResColumn*COLNAME_N); |
2401 for(pSub=p->pProgram; pSub; pSub=pNext){ | 2672 for(pSub=p->pProgram; pSub; pSub=pNext){ |
2402 pNext = pSub->pNext; | 2673 pNext = pSub->pNext; |
2403 vdbeFreeOpArray(db, pSub->aOp, pSub->nOp); | 2674 vdbeFreeOpArray(db, pSub->aOp, pSub->nOp); |
2404 sqlite3DbFree(db, pSub); | 2675 sqlite3DbFree(db, pSub); |
2405 } | 2676 } |
| 2677 for(i=p->nzVar-1; i>=0; i--) sqlite3DbFree(db, p->azVar[i]); |
2406 vdbeFreeOpArray(db, p->aOp, p->nOp); | 2678 vdbeFreeOpArray(db, p->aOp, p->nOp); |
2407 sqlite3DbFree(db, p->aLabel); | |
2408 sqlite3DbFree(db, p->aColName); | 2679 sqlite3DbFree(db, p->aColName); |
2409 sqlite3DbFree(db, p->zSql); | 2680 sqlite3DbFree(db, p->zSql); |
2410 sqlite3DbFree(db, p->pFree); | 2681 sqlite3DbFree(db, p->pFree); |
2411 sqlite3DbFree(db, p); | |
2412 } | 2682 } |
2413 | 2683 |
2414 /* | 2684 /* |
2415 ** Delete an entire VDBE. | 2685 ** Delete an entire VDBE. |
2416 */ | 2686 */ |
2417 void sqlite3VdbeDelete(Vdbe *p){ | 2687 void sqlite3VdbeDelete(Vdbe *p){ |
2418 sqlite3 *db; | 2688 sqlite3 *db; |
2419 | 2689 |
2420 if( NEVER(p==0) ) return; | 2690 if( NEVER(p==0) ) return; |
2421 db = p->db; | 2691 db = p->db; |
| 2692 assert( sqlite3_mutex_held(db->mutex) ); |
| 2693 sqlite3VdbeClearObject(db, p); |
2422 if( p->pPrev ){ | 2694 if( p->pPrev ){ |
2423 p->pPrev->pNext = p->pNext; | 2695 p->pPrev->pNext = p->pNext; |
2424 }else{ | 2696 }else{ |
2425 assert( db->pVdbe==p ); | 2697 assert( db->pVdbe==p ); |
2426 db->pVdbe = p->pNext; | 2698 db->pVdbe = p->pNext; |
2427 } | 2699 } |
2428 if( p->pNext ){ | 2700 if( p->pNext ){ |
2429 p->pNext->pPrev = p->pPrev; | 2701 p->pNext->pPrev = p->pPrev; |
2430 } | 2702 } |
2431 p->magic = VDBE_MAGIC_DEAD; | 2703 p->magic = VDBE_MAGIC_DEAD; |
2432 p->db = 0; | 2704 p->db = 0; |
2433 sqlite3VdbeDeleteObject(db, p); | 2705 sqlite3DbFree(db, p); |
2434 } | 2706 } |
2435 | 2707 |
2436 /* | 2708 /* |
| 2709 ** The cursor "p" has a pending seek operation that has not yet been |
| 2710 ** carried out. Seek the cursor now. If an error occurs, return |
| 2711 ** the appropriate error code. |
| 2712 */ |
| 2713 static int SQLITE_NOINLINE handleDeferredMoveto(VdbeCursor *p){ |
| 2714 int res, rc; |
| 2715 #ifdef SQLITE_TEST |
| 2716 extern int sqlite3_search_count; |
| 2717 #endif |
| 2718 assert( p->deferredMoveto ); |
| 2719 assert( p->isTable ); |
| 2720 rc = sqlite3BtreeMovetoUnpacked(p->pCursor, 0, p->movetoTarget, 0, &res); |
| 2721 if( rc ) return rc; |
| 2722 if( res!=0 ) return SQLITE_CORRUPT_BKPT; |
| 2723 #ifdef SQLITE_TEST |
| 2724 sqlite3_search_count++; |
| 2725 #endif |
| 2726 p->deferredMoveto = 0; |
| 2727 p->cacheStatus = CACHE_STALE; |
| 2728 return SQLITE_OK; |
| 2729 } |
| 2730 |
| 2731 /* |
| 2732 ** Something has moved cursor "p" out of place. Maybe the row it was |
| 2733 ** pointed to was deleted out from under it. Or maybe the btree was |
| 2734 ** rebalanced. Whatever the cause, try to restore "p" to the place it |
| 2735 ** is supposed to be pointing. If the row was deleted out from under the |
| 2736 ** cursor, set the cursor to point to a NULL row. |
| 2737 */ |
| 2738 static int SQLITE_NOINLINE handleMovedCursor(VdbeCursor *p){ |
| 2739 int isDifferentRow, rc; |
| 2740 assert( p->pCursor!=0 ); |
| 2741 assert( sqlite3BtreeCursorHasMoved(p->pCursor) ); |
| 2742 rc = sqlite3BtreeCursorRestore(p->pCursor, &isDifferentRow); |
| 2743 p->cacheStatus = CACHE_STALE; |
| 2744 if( isDifferentRow ) p->nullRow = 1; |
| 2745 return rc; |
| 2746 } |
| 2747 |
| 2748 /* |
| 2749 ** Check to ensure that the cursor is valid. Restore the cursor |
| 2750 ** if need be. Return any I/O error from the restore operation. |
| 2751 */ |
| 2752 int sqlite3VdbeCursorRestore(VdbeCursor *p){ |
| 2753 if( sqlite3BtreeCursorHasMoved(p->pCursor) ){ |
| 2754 return handleMovedCursor(p); |
| 2755 } |
| 2756 return SQLITE_OK; |
| 2757 } |
| 2758 |
| 2759 /* |
2437 ** Make sure the cursor p is ready to read or write the row to which it | 2760 ** Make sure the cursor p is ready to read or write the row to which it |
2438 ** was last positioned. Return an error code if an OOM fault or I/O error | 2761 ** was last positioned. Return an error code if an OOM fault or I/O error |
2439 ** prevents us from positioning the cursor to its correct position. | 2762 ** prevents us from positioning the cursor to its correct position. |
2440 ** | 2763 ** |
2441 ** If a MoveTo operation is pending on the given cursor, then do that | 2764 ** If a MoveTo operation is pending on the given cursor, then do that |
2442 ** MoveTo now. If no move is pending, check to see if the row has been | 2765 ** MoveTo now. If no move is pending, check to see if the row has been |
2443 ** deleted out from under the cursor and if it has, mark the row as | 2766 ** deleted out from under the cursor and if it has, mark the row as |
2444 ** a NULL row. | 2767 ** a NULL row. |
2445 ** | 2768 ** |
2446 ** If the cursor is already pointing to the correct row and that row has | 2769 ** If the cursor is already pointing to the correct row and that row has |
2447 ** not been deleted out from under the cursor, then this routine is a no-op. | 2770 ** not been deleted out from under the cursor, then this routine is a no-op. |
2448 */ | 2771 */ |
2449 int sqlite3VdbeCursorMoveto(VdbeCursor *p){ | 2772 int sqlite3VdbeCursorMoveto(VdbeCursor *p){ |
2450 if( p->deferredMoveto ){ | 2773 if( p->deferredMoveto ){ |
2451 int res, rc; | 2774 return handleDeferredMoveto(p); |
2452 #ifdef SQLITE_TEST | 2775 } |
2453 extern int sqlite3_search_count; | 2776 if( p->pCursor && sqlite3BtreeCursorHasMoved(p->pCursor) ){ |
2454 #endif | 2777 return handleMovedCursor(p); |
2455 assert( p->isTable ); | |
2456 rc = sqlite3BtreeMovetoUnpacked(p->pCursor, 0, p->movetoTarget, 0, &res); | |
2457 if( rc ) return rc; | |
2458 p->lastRowid = p->movetoTarget; | |
2459 if( res!=0 ) return SQLITE_CORRUPT_BKPT; | |
2460 p->rowidIsValid = 1; | |
2461 #ifdef SQLITE_TEST | |
2462 sqlite3_search_count++; | |
2463 #endif | |
2464 p->deferredMoveto = 0; | |
2465 p->cacheStatus = CACHE_STALE; | |
2466 }else if( ALWAYS(p->pCursor) ){ | |
2467 int hasMoved; | |
2468 int rc = sqlite3BtreeCursorHasMoved(p->pCursor, &hasMoved); | |
2469 if( rc ) return rc; | |
2470 if( hasMoved ){ | |
2471 p->cacheStatus = CACHE_STALE; | |
2472 p->nullRow = 1; | |
2473 } | |
2474 } | 2778 } |
2475 return SQLITE_OK; | 2779 return SQLITE_OK; |
2476 } | 2780 } |
2477 | 2781 |
2478 /* | 2782 /* |
2479 ** The following functions: | 2783 ** The following functions: |
2480 ** | 2784 ** |
2481 ** sqlite3VdbeSerialType() | 2785 ** sqlite3VdbeSerialType() |
2482 ** sqlite3VdbeSerialTypeLen() | 2786 ** sqlite3VdbeSerialTypeLen() |
2483 ** sqlite3VdbeSerialLen() | 2787 ** sqlite3VdbeSerialLen() |
2484 ** sqlite3VdbeSerialPut() | 2788 ** sqlite3VdbeSerialPut() |
2485 ** sqlite3VdbeSerialGet() | 2789 ** sqlite3VdbeSerialGet() |
2486 ** | 2790 ** |
2487 ** encapsulate the code that serializes values for storage in SQLite | 2791 ** encapsulate the code that serializes values for storage in SQLite |
2488 ** data and index records. Each serialized value consists of a | 2792 ** data and index records. Each serialized value consists of a |
2489 ** 'serial-type' and a blob of data. The serial type is an 8-byte unsigned | 2793 ** 'serial-type' and a blob of data. The serial type is an 8-byte unsigned |
2490 ** integer, stored as a varint. | 2794 ** integer, stored as a varint. |
2491 ** | 2795 ** |
2492 ** In an SQLite index record, the serial type is stored directly before | 2796 ** In an SQLite index record, the serial type is stored directly before |
2493 ** the blob of data that it corresponds to. In a table record, all serial | 2797 ** the blob of data that it corresponds to. In a table record, all serial |
2494 ** types are stored at the start of the record, and the blobs of data at | 2798 ** types are stored at the start of the record, and the blobs of data at |
2495 ** the end. Hence these functions allow the caller to handle the | 2799 ** the end. Hence these functions allow the caller to handle the |
2496 ** serial-type and data blob seperately. | 2800 ** serial-type and data blob separately. |
2497 ** | 2801 ** |
2498 ** The following table describes the various storage classes for data: | 2802 ** The following table describes the various storage classes for data: |
2499 ** | 2803 ** |
2500 ** serial type bytes of data type | 2804 ** serial type bytes of data type |
2501 ** -------------- --------------- --------------- | 2805 ** -------------- --------------- --------------- |
2502 ** 0 0 NULL | 2806 ** 0 0 NULL |
2503 ** 1 1 signed integer | 2807 ** 1 1 signed integer |
2504 ** 2 2 signed integer | 2808 ** 2 2 signed integer |
2505 ** 3 3 signed integer | 2809 ** 3 3 signed integer |
2506 ** 4 4 signed integer | 2810 ** 4 4 signed integer |
2507 ** 5 6 signed integer | 2811 ** 5 6 signed integer |
2508 ** 6 8 signed integer | 2812 ** 6 8 signed integer |
2509 ** 7 8 IEEE float | 2813 ** 7 8 IEEE float |
2510 ** 8 0 Integer constant 0 | 2814 ** 8 0 Integer constant 0 |
2511 ** 9 0 Integer constant 1 | 2815 ** 9 0 Integer constant 1 |
2512 ** 10,11 reserved for expansion | 2816 ** 10,11 reserved for expansion |
2513 ** N>=12 and even (N-12)/2 BLOB | 2817 ** N>=12 and even (N-12)/2 BLOB |
2514 ** N>=13 and odd (N-13)/2 text | 2818 ** N>=13 and odd (N-13)/2 text |
2515 ** | 2819 ** |
2516 ** The 8 and 9 types were added in 3.3.0, file format 4. Prior versions | 2820 ** The 8 and 9 types were added in 3.3.0, file format 4. Prior versions |
2517 ** of SQLite will not understand those serial types. | 2821 ** of SQLite will not understand those serial types. |
2518 */ | 2822 */ |
2519 | 2823 |
2520 /* | 2824 /* |
2521 ** Return the serial-type for the value stored in pMem. | 2825 ** Return the serial-type for the value stored in pMem. |
2522 */ | 2826 */ |
2523 u32 sqlite3VdbeSerialType(Mem *pMem, int file_format){ | 2827 u32 sqlite3VdbeSerialType(Mem *pMem, int file_format){ |
2524 int flags = pMem->flags; | 2828 int flags = pMem->flags; |
2525 int n; | 2829 u32 n; |
2526 | 2830 |
2527 if( flags&MEM_Null ){ | 2831 if( flags&MEM_Null ){ |
2528 return 0; | 2832 return 0; |
2529 } | 2833 } |
2530 if( flags&MEM_Int ){ | 2834 if( flags&MEM_Int ){ |
2531 /* Figure out whether to use 1, 2, 4, 6 or 8 bytes. */ | 2835 /* Figure out whether to use 1, 2, 4, 6 or 8 bytes. */ |
2532 # define MAX_6BYTE ((((i64)0x00008000)<<32)-1) | 2836 # define MAX_6BYTE ((((i64)0x00008000)<<32)-1) |
2533 i64 i = pMem->u.i; | 2837 i64 i = pMem->u.i; |
2534 u64 u; | 2838 u64 u; |
2535 if( file_format>=4 && (i&1)==i ){ | |
2536 return 8+(u32)i; | |
2537 } | |
2538 if( i<0 ){ | 2839 if( i<0 ){ |
2539 if( i<(-MAX_6BYTE) ) return 6; | 2840 if( i<(-MAX_6BYTE) ) return 6; |
2540 /* Previous test prevents: u = -(-9223372036854775808) */ | 2841 /* Previous test prevents: u = -(-9223372036854775808) */ |
2541 u = -i; | 2842 u = -i; |
2542 }else{ | 2843 }else{ |
2543 u = i; | 2844 u = i; |
2544 } | 2845 } |
2545 if( u<=127 ) return 1; | 2846 if( u<=127 ){ |
| 2847 return ((i&1)==i && file_format>=4) ? 8+(u32)u : 1; |
| 2848 } |
2546 if( u<=32767 ) return 2; | 2849 if( u<=32767 ) return 2; |
2547 if( u<=8388607 ) return 3; | 2850 if( u<=8388607 ) return 3; |
2548 if( u<=2147483647 ) return 4; | 2851 if( u<=2147483647 ) return 4; |
2549 if( u<=MAX_6BYTE ) return 5; | 2852 if( u<=MAX_6BYTE ) return 5; |
2550 return 6; | 2853 return 6; |
2551 } | 2854 } |
2552 if( flags&MEM_Real ){ | 2855 if( flags&MEM_Real ){ |
2553 return 7; | 2856 return 7; |
2554 } | 2857 } |
2555 assert( pMem->db->mallocFailed || flags&(MEM_Str|MEM_Blob) ); | 2858 assert( pMem->db->mallocFailed || flags&(MEM_Str|MEM_Blob) ); |
2556 n = pMem->n; | 2859 assert( pMem->n>=0 ); |
| 2860 n = (u32)pMem->n; |
2557 if( flags & MEM_Zero ){ | 2861 if( flags & MEM_Zero ){ |
2558 n += pMem->u.nZero; | 2862 n += pMem->u.nZero; |
2559 } | 2863 } |
2560 assert( n>=0 ); | |
2561 return ((n*2) + 12 + ((flags&MEM_Str)!=0)); | 2864 return ((n*2) + 12 + ((flags&MEM_Str)!=0)); |
2562 } | 2865 } |
2563 | 2866 |
2564 /* | 2867 /* |
2565 ** Return the length of the data corresponding to the supplied serial-type. | 2868 ** Return the length of the data corresponding to the supplied serial-type. |
2566 */ | 2869 */ |
2567 u32 sqlite3VdbeSerialTypeLen(u32 serial_type){ | 2870 u32 sqlite3VdbeSerialTypeLen(u32 serial_type){ |
2568 if( serial_type>=12 ){ | 2871 if( serial_type>=12 ){ |
2569 return (serial_type-12)/2; | 2872 return (serial_type-12)/2; |
2570 }else{ | 2873 }else{ |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2624 # define swapMixedEndianFloat(X) X = floatSwap(X) | 2927 # define swapMixedEndianFloat(X) X = floatSwap(X) |
2625 #else | 2928 #else |
2626 # define swapMixedEndianFloat(X) | 2929 # define swapMixedEndianFloat(X) |
2627 #endif | 2930 #endif |
2628 | 2931 |
2629 /* | 2932 /* |
2630 ** Write the serialized data blob for the value stored in pMem into | 2933 ** Write the serialized data blob for the value stored in pMem into |
2631 ** buf. It is assumed that the caller has allocated sufficient space. | 2934 ** buf. It is assumed that the caller has allocated sufficient space. |
2632 ** Return the number of bytes written. | 2935 ** Return the number of bytes written. |
2633 ** | 2936 ** |
2634 ** nBuf is the amount of space left in buf[]. nBuf must always be | 2937 ** nBuf is the amount of space left in buf[]. The caller is responsible |
2635 ** large enough to hold the entire field. Except, if the field is | 2938 ** for allocating enough space to buf[] to hold the entire field, exclusive |
2636 ** a blob with a zero-filled tail, then buf[] might be just the right | 2939 ** of the pMem->u.nZero bytes for a MEM_Zero value. |
2637 ** size to hold everything except for the zero-filled tail. If buf[] | |
2638 ** is only big enough to hold the non-zero prefix, then only write that | |
2639 ** prefix into buf[]. But if buf[] is large enough to hold both the | |
2640 ** prefix and the tail then write the prefix and set the tail to all | |
2641 ** zeros. | |
2642 ** | 2940 ** |
2643 ** Return the number of bytes actually written into buf[]. The number | 2941 ** Return the number of bytes actually written into buf[]. The number |
2644 ** of bytes in the zero-filled tail is included in the return value only | 2942 ** of bytes in the zero-filled tail is included in the return value only |
2645 ** if those bytes were zeroed in buf[]. | 2943 ** if those bytes were zeroed in buf[]. |
2646 */ | 2944 */ |
2647 u32 sqlite3VdbeSerialPut(u8 *buf, int nBuf, Mem *pMem, int file_format){ | 2945 u32 sqlite3VdbeSerialPut(u8 *buf, Mem *pMem, u32 serial_type){ |
2648 u32 serial_type = sqlite3VdbeSerialType(pMem, file_format); | |
2649 u32 len; | 2946 u32 len; |
2650 | 2947 |
2651 /* Integer and Real */ | 2948 /* Integer and Real */ |
2652 if( serial_type<=7 && serial_type>0 ){ | 2949 if( serial_type<=7 && serial_type>0 ){ |
2653 u64 v; | 2950 u64 v; |
2654 u32 i; | 2951 u32 i; |
2655 if( serial_type==7 ){ | 2952 if( serial_type==7 ){ |
2656 assert( sizeof(v)==sizeof(pMem->r) ); | 2953 assert( sizeof(v)==sizeof(pMem->u.r) ); |
2657 memcpy(&v, &pMem->r, sizeof(v)); | 2954 memcpy(&v, &pMem->u.r, sizeof(v)); |
2658 swapMixedEndianFloat(v); | 2955 swapMixedEndianFloat(v); |
2659 }else{ | 2956 }else{ |
2660 v = pMem->u.i; | 2957 v = pMem->u.i; |
2661 } | 2958 } |
2662 len = i = sqlite3VdbeSerialTypeLen(serial_type); | 2959 len = i = sqlite3VdbeSerialTypeLen(serial_type); |
2663 assert( len<=(u32)nBuf ); | 2960 assert( i>0 ); |
2664 while( i-- ){ | 2961 do{ |
2665 buf[i] = (u8)(v&0xFF); | 2962 buf[--i] = (u8)(v&0xFF); |
2666 v >>= 8; | 2963 v >>= 8; |
2667 } | 2964 }while( i ); |
2668 return len; | 2965 return len; |
2669 } | 2966 } |
2670 | 2967 |
2671 /* String or blob */ | 2968 /* String or blob */ |
2672 if( serial_type>=12 ){ | 2969 if( serial_type>=12 ){ |
2673 assert( pMem->n + ((pMem->flags & MEM_Zero)?pMem->u.nZero:0) | 2970 assert( pMem->n + ((pMem->flags & MEM_Zero)?pMem->u.nZero:0) |
2674 == (int)sqlite3VdbeSerialTypeLen(serial_type) ); | 2971 == (int)sqlite3VdbeSerialTypeLen(serial_type) ); |
2675 assert( pMem->n<=nBuf ); | |
2676 len = pMem->n; | 2972 len = pMem->n; |
2677 memcpy(buf, pMem->z, len); | 2973 memcpy(buf, pMem->z, len); |
2678 if( pMem->flags & MEM_Zero ){ | |
2679 len += pMem->u.nZero; | |
2680 assert( nBuf>=0 ); | |
2681 if( len > (u32)nBuf ){ | |
2682 len = (u32)nBuf; | |
2683 } | |
2684 memset(&buf[pMem->n], 0, len-pMem->n); | |
2685 } | |
2686 return len; | 2974 return len; |
2687 } | 2975 } |
2688 | 2976 |
2689 /* NULL or constants 0 or 1 */ | 2977 /* NULL or constants 0 or 1 */ |
2690 return 0; | 2978 return 0; |
2691 } | 2979 } |
2692 | 2980 |
| 2981 /* Input "x" is a sequence of unsigned characters that represent a |
| 2982 ** big-endian integer. Return the equivalent native integer |
| 2983 */ |
| 2984 #define ONE_BYTE_INT(x) ((i8)(x)[0]) |
| 2985 #define TWO_BYTE_INT(x) (256*(i8)((x)[0])|(x)[1]) |
| 2986 #define THREE_BYTE_INT(x) (65536*(i8)((x)[0])|((x)[1]<<8)|(x)[2]) |
| 2987 #define FOUR_BYTE_UINT(x) (((u32)(x)[0]<<24)|((x)[1]<<16)|((x)[2]<<8)|(x)[3]) |
| 2988 #define FOUR_BYTE_INT(x) (16777216*(i8)((x)[0])|((x)[1]<<16)|((x)[2]<<8)|(x)[3]) |
| 2989 |
2693 /* | 2990 /* |
2694 ** Deserialize the data blob pointed to by buf as serial type serial_type | 2991 ** Deserialize the data blob pointed to by buf as serial type serial_type |
2695 ** and store the result in pMem. Return the number of bytes read. | 2992 ** and store the result in pMem. Return the number of bytes read. |
| 2993 ** |
| 2994 ** This function is implemented as two separate routines for performance. |
| 2995 ** The few cases that require local variables are broken out into a separate |
| 2996 ** routine so that in most cases the overhead of moving the stack pointer |
| 2997 ** is avoided. |
2696 */ | 2998 */ |
| 2999 static u32 SQLITE_NOINLINE serialGet( |
| 3000 const unsigned char *buf, /* Buffer to deserialize from */ |
| 3001 u32 serial_type, /* Serial type to deserialize */ |
| 3002 Mem *pMem /* Memory cell to write value into */ |
| 3003 ){ |
| 3004 u64 x = FOUR_BYTE_UINT(buf); |
| 3005 u32 y = FOUR_BYTE_UINT(buf+4); |
| 3006 x = (x<<32) + y; |
| 3007 if( serial_type==6 ){ |
| 3008 pMem->u.i = *(i64*)&x; |
| 3009 pMem->flags = MEM_Int; |
| 3010 testcase( pMem->u.i<0 ); |
| 3011 }else{ |
| 3012 #if !defined(NDEBUG) && !defined(SQLITE_OMIT_FLOATING_POINT) |
| 3013 /* Verify that integers and floating point values use the same |
| 3014 ** byte order. Or, that if SQLITE_MIXED_ENDIAN_64BIT_FLOAT is |
| 3015 ** defined that 64-bit floating point values really are mixed |
| 3016 ** endian. |
| 3017 */ |
| 3018 static const u64 t1 = ((u64)0x3ff00000)<<32; |
| 3019 static const double r1 = 1.0; |
| 3020 u64 t2 = t1; |
| 3021 swapMixedEndianFloat(t2); |
| 3022 assert( sizeof(r1)==sizeof(t2) && memcmp(&r1, &t2, sizeof(r1))==0 ); |
| 3023 #endif |
| 3024 assert( sizeof(x)==8 && sizeof(pMem->u.r)==8 ); |
| 3025 swapMixedEndianFloat(x); |
| 3026 memcpy(&pMem->u.r, &x, sizeof(x)); |
| 3027 pMem->flags = sqlite3IsNaN(pMem->u.r) ? MEM_Null : MEM_Real; |
| 3028 } |
| 3029 return 8; |
| 3030 } |
2697 u32 sqlite3VdbeSerialGet( | 3031 u32 sqlite3VdbeSerialGet( |
2698 const unsigned char *buf, /* Buffer to deserialize from */ | 3032 const unsigned char *buf, /* Buffer to deserialize from */ |
2699 u32 serial_type, /* Serial type to deserialize */ | 3033 u32 serial_type, /* Serial type to deserialize */ |
2700 Mem *pMem /* Memory cell to write value into */ | 3034 Mem *pMem /* Memory cell to write value into */ |
2701 ){ | 3035 ){ |
2702 switch( serial_type ){ | 3036 switch( serial_type ){ |
2703 case 10: /* Reserved for future use */ | 3037 case 10: /* Reserved for future use */ |
2704 case 11: /* Reserved for future use */ | 3038 case 11: /* Reserved for future use */ |
2705 case 0: { /* NULL */ | 3039 case 0: { /* NULL */ |
2706 pMem->flags = MEM_Null; | 3040 pMem->flags = MEM_Null; |
2707 break; | 3041 break; |
2708 } | 3042 } |
2709 case 1: { /* 1-byte signed integer */ | 3043 case 1: { /* 1-byte signed integer */ |
2710 pMem->u.i = (signed char)buf[0]; | 3044 pMem->u.i = ONE_BYTE_INT(buf); |
2711 pMem->flags = MEM_Int; | 3045 pMem->flags = MEM_Int; |
| 3046 testcase( pMem->u.i<0 ); |
2712 return 1; | 3047 return 1; |
2713 } | 3048 } |
2714 case 2: { /* 2-byte signed integer */ | 3049 case 2: { /* 2-byte signed integer */ |
2715 pMem->u.i = (((signed char)buf[0])<<8) | buf[1]; | 3050 pMem->u.i = TWO_BYTE_INT(buf); |
2716 pMem->flags = MEM_Int; | 3051 pMem->flags = MEM_Int; |
| 3052 testcase( pMem->u.i<0 ); |
2717 return 2; | 3053 return 2; |
2718 } | 3054 } |
2719 case 3: { /* 3-byte signed integer */ | 3055 case 3: { /* 3-byte signed integer */ |
2720 pMem->u.i = (((signed char)buf[0])<<16) | (buf[1]<<8) | buf[2]; | 3056 pMem->u.i = THREE_BYTE_INT(buf); |
2721 pMem->flags = MEM_Int; | 3057 pMem->flags = MEM_Int; |
| 3058 testcase( pMem->u.i<0 ); |
2722 return 3; | 3059 return 3; |
2723 } | 3060 } |
2724 case 4: { /* 4-byte signed integer */ | 3061 case 4: { /* 4-byte signed integer */ |
2725 pMem->u.i = (buf[0]<<24) | (buf[1]<<16) | (buf[2]<<8) | buf[3]; | 3062 pMem->u.i = FOUR_BYTE_INT(buf); |
2726 pMem->flags = MEM_Int; | 3063 pMem->flags = MEM_Int; |
| 3064 testcase( pMem->u.i<0 ); |
2727 return 4; | 3065 return 4; |
2728 } | 3066 } |
2729 case 5: { /* 6-byte signed integer */ | 3067 case 5: { /* 6-byte signed integer */ |
2730 u64 x = (((signed char)buf[0])<<8) | buf[1]; | 3068 pMem->u.i = FOUR_BYTE_UINT(buf+2) + (((i64)1)<<32)*TWO_BYTE_INT(buf); |
2731 u32 y = (buf[2]<<24) | (buf[3]<<16) | (buf[4]<<8) | buf[5]; | |
2732 x = (x<<32) | y; | |
2733 pMem->u.i = *(i64*)&x; | |
2734 pMem->flags = MEM_Int; | 3069 pMem->flags = MEM_Int; |
| 3070 testcase( pMem->u.i<0 ); |
2735 return 6; | 3071 return 6; |
2736 } | 3072 } |
2737 case 6: /* 8-byte signed integer */ | 3073 case 6: /* 8-byte signed integer */ |
2738 case 7: { /* IEEE floating point */ | 3074 case 7: { /* IEEE floating point */ |
2739 u64 x; | 3075 /* These use local variables, so do them in a separate routine |
2740 u32 y; | 3076 ** to avoid having to move the frame pointer in the common case */ |
2741 #if !defined(NDEBUG) && !defined(SQLITE_OMIT_FLOATING_POINT) | 3077 return serialGet(buf,serial_type,pMem); |
2742 /* Verify that integers and floating point values use the same | |
2743 ** byte order. Or, that if SQLITE_MIXED_ENDIAN_64BIT_FLOAT is | |
2744 ** defined that 64-bit floating point values really are mixed | |
2745 ** endian. | |
2746 */ | |
2747 static const u64 t1 = ((u64)0x3ff00000)<<32; | |
2748 static const double r1 = 1.0; | |
2749 u64 t2 = t1; | |
2750 swapMixedEndianFloat(t2); | |
2751 assert( sizeof(r1)==sizeof(t2) && memcmp(&r1, &t2, sizeof(r1))==0 ); | |
2752 #endif | |
2753 | |
2754 x = (buf[0]<<24) | (buf[1]<<16) | (buf[2]<<8) | buf[3]; | |
2755 y = (buf[4]<<24) | (buf[5]<<16) | (buf[6]<<8) | buf[7]; | |
2756 x = (x<<32) | y; | |
2757 if( serial_type==6 ){ | |
2758 pMem->u.i = *(i64*)&x; | |
2759 pMem->flags = MEM_Int; | |
2760 }else{ | |
2761 assert( sizeof(x)==8 && sizeof(pMem->r)==8 ); | |
2762 swapMixedEndianFloat(x); | |
2763 memcpy(&pMem->r, &x, sizeof(x)); | |
2764 pMem->flags = sqlite3IsNaN(pMem->r) ? MEM_Null : MEM_Real; | |
2765 } | |
2766 return 8; | |
2767 } | 3078 } |
2768 case 8: /* Integer 0 */ | 3079 case 8: /* Integer 0 */ |
2769 case 9: { /* Integer 1 */ | 3080 case 9: { /* Integer 1 */ |
2770 pMem->u.i = serial_type-8; | 3081 pMem->u.i = serial_type-8; |
2771 pMem->flags = MEM_Int; | 3082 pMem->flags = MEM_Int; |
2772 return 0; | 3083 return 0; |
2773 } | 3084 } |
2774 default: { | 3085 default: { |
2775 u32 len = (serial_type-12)/2; | 3086 static const u16 aFlag[] = { MEM_Blob|MEM_Ephem, MEM_Str|MEM_Ephem }; |
2776 pMem->z = (char *)buf; | 3087 pMem->z = (char *)buf; |
2777 pMem->n = len; | 3088 pMem->n = (serial_type-12)/2; |
2778 pMem->xDel = 0; | 3089 pMem->flags = aFlag[serial_type&1]; |
2779 if( serial_type&0x01 ){ | 3090 return pMem->n; |
2780 pMem->flags = MEM_Str | MEM_Ephem; | |
2781 }else{ | |
2782 pMem->flags = MEM_Blob | MEM_Ephem; | |
2783 } | |
2784 return len; | |
2785 } | 3091 } |
2786 } | 3092 } |
2787 return 0; | 3093 return 0; |
2788 } | 3094 } |
| 3095 /* |
| 3096 ** This routine is used to allocate sufficient space for an UnpackedRecord |
| 3097 ** structure large enough to be used with sqlite3VdbeRecordUnpack() if |
| 3098 ** the first argument is a pointer to KeyInfo structure pKeyInfo. |
| 3099 ** |
| 3100 ** The space is either allocated using sqlite3DbMallocRaw() or from within |
| 3101 ** the unaligned buffer passed via the second and third arguments (presumably |
| 3102 ** stack space). If the former, then *ppFree is set to a pointer that should |
| 3103 ** be eventually freed by the caller using sqlite3DbFree(). Or, if the |
| 3104 ** allocation comes from the pSpace/szSpace buffer, *ppFree is set to NULL |
| 3105 ** before returning. |
| 3106 ** |
| 3107 ** If an OOM error occurs, NULL is returned. |
| 3108 */ |
| 3109 UnpackedRecord *sqlite3VdbeAllocUnpackedRecord( |
| 3110 KeyInfo *pKeyInfo, /* Description of the record */ |
| 3111 char *pSpace, /* Unaligned space available */ |
| 3112 int szSpace, /* Size of pSpace[] in bytes */ |
| 3113 char **ppFree /* OUT: Caller should free this pointer */ |
| 3114 ){ |
| 3115 UnpackedRecord *p; /* Unpacked record to return */ |
| 3116 int nOff; /* Increment pSpace by nOff to align it */ |
| 3117 int nByte; /* Number of bytes required for *p */ |
2789 | 3118 |
2790 | 3119 /* We want to shift the pointer pSpace up such that it is 8-byte aligned. |
2791 /* | |
2792 ** Given the nKey-byte encoding of a record in pKey[], parse the | |
2793 ** record into a UnpackedRecord structure. Return a pointer to | |
2794 ** that structure. | |
2795 ** | |
2796 ** The calling function might provide szSpace bytes of memory | |
2797 ** space at pSpace. This space can be used to hold the returned | |
2798 ** VDbeParsedRecord structure if it is large enough. If it is | |
2799 ** not big enough, space is obtained from sqlite3_malloc(). | |
2800 ** | |
2801 ** The returned structure should be closed by a call to | |
2802 ** sqlite3VdbeDeleteUnpackedRecord(). | |
2803 */ | |
2804 UnpackedRecord *sqlite3VdbeRecordUnpack( | |
2805 KeyInfo *pKeyInfo, /* Information about the record format */ | |
2806 int nKey, /* Size of the binary record */ | |
2807 const void *pKey, /* The binary record */ | |
2808 char *pSpace, /* Unaligned space available to hold the object */ | |
2809 int szSpace /* Size of pSpace[] in bytes */ | |
2810 ){ | |
2811 const unsigned char *aKey = (const unsigned char *)pKey; | |
2812 UnpackedRecord *p; /* The unpacked record that we will return */ | |
2813 int nByte; /* Memory space needed to hold p, in bytes */ | |
2814 int d; | |
2815 u32 idx; | |
2816 u16 u; /* Unsigned loop counter */ | |
2817 u32 szHdr; | |
2818 Mem *pMem; | |
2819 int nOff; /* Increase pSpace by this much to 8-byte align it */ | |
2820 | |
2821 /* | |
2822 ** We want to shift the pointer pSpace up such that it is 8-byte aligned. | |
2823 ** Thus, we need to calculate a value, nOff, between 0 and 7, to shift | 3120 ** Thus, we need to calculate a value, nOff, between 0 and 7, to shift |
2824 ** it by. If pSpace is already 8-byte aligned, nOff should be zero. | 3121 ** it by. If pSpace is already 8-byte aligned, nOff should be zero. |
2825 */ | 3122 */ |
2826 nOff = (8 - (SQLITE_PTR_TO_INT(pSpace) & 7)) & 7; | 3123 nOff = (8 - (SQLITE_PTR_TO_INT(pSpace) & 7)) & 7; |
2827 pSpace += nOff; | |
2828 szSpace -= nOff; | |
2829 nByte = ROUND8(sizeof(UnpackedRecord)) + sizeof(Mem)*(pKeyInfo->nField+1); | 3124 nByte = ROUND8(sizeof(UnpackedRecord)) + sizeof(Mem)*(pKeyInfo->nField+1); |
2830 if( nByte>szSpace ){ | 3125 if( nByte>szSpace+nOff ){ |
2831 p = sqlite3DbMallocRaw(pKeyInfo->db, nByte); | 3126 p = (UnpackedRecord *)sqlite3DbMallocRaw(pKeyInfo->db, nByte); |
2832 if( p==0 ) return 0; | 3127 *ppFree = (char *)p; |
2833 p->flags = UNPACKED_NEED_FREE | UNPACKED_NEED_DESTROY; | 3128 if( !p ) return 0; |
2834 }else{ | 3129 }else{ |
2835 p = (UnpackedRecord*)pSpace; | 3130 p = (UnpackedRecord*)&pSpace[nOff]; |
2836 p->flags = UNPACKED_NEED_DESTROY; | 3131 *ppFree = 0; |
2837 } | 3132 } |
| 3133 |
| 3134 p->aMem = (Mem*)&((char*)p)[ROUND8(sizeof(UnpackedRecord))]; |
| 3135 assert( pKeyInfo->aSortOrder!=0 ); |
2838 p->pKeyInfo = pKeyInfo; | 3136 p->pKeyInfo = pKeyInfo; |
2839 p->nField = pKeyInfo->nField + 1; | 3137 p->nField = pKeyInfo->nField + 1; |
2840 p->aMem = pMem = (Mem*)&((char*)p)[ROUND8(sizeof(UnpackedRecord))]; | 3138 return p; |
| 3139 } |
| 3140 |
| 3141 /* |
| 3142 ** Given the nKey-byte encoding of a record in pKey[], populate the |
| 3143 ** UnpackedRecord structure indicated by the fourth argument with the |
| 3144 ** contents of the decoded record. |
| 3145 */ |
| 3146 void sqlite3VdbeRecordUnpack( |
| 3147 KeyInfo *pKeyInfo, /* Information about the record format */ |
| 3148 int nKey, /* Size of the binary record */ |
| 3149 const void *pKey, /* The binary record */ |
| 3150 UnpackedRecord *p /* Populate this structure before returning. */ |
| 3151 ){ |
| 3152 const unsigned char *aKey = (const unsigned char *)pKey; |
| 3153 int d; |
| 3154 u32 idx; /* Offset in aKey[] to read from */ |
| 3155 u16 u; /* Unsigned loop counter */ |
| 3156 u32 szHdr; |
| 3157 Mem *pMem = p->aMem; |
| 3158 |
| 3159 p->default_rc = 0; |
2841 assert( EIGHT_BYTE_ALIGNMENT(pMem) ); | 3160 assert( EIGHT_BYTE_ALIGNMENT(pMem) ); |
2842 idx = getVarint32(aKey, szHdr); | 3161 idx = getVarint32(aKey, szHdr); |
2843 d = szHdr; | 3162 d = szHdr; |
2844 u = 0; | 3163 u = 0; |
2845 while( idx<szHdr && u<p->nField && d<=nKey ){ | 3164 while( idx<szHdr && d<=nKey ){ |
2846 u32 serial_type; | 3165 u32 serial_type; |
2847 | 3166 |
2848 idx += getVarint32(&aKey[idx], serial_type); | 3167 idx += getVarint32(&aKey[idx], serial_type); |
2849 pMem->enc = pKeyInfo->enc; | 3168 pMem->enc = pKeyInfo->enc; |
2850 pMem->db = pKeyInfo->db; | 3169 pMem->db = pKeyInfo->db; |
2851 pMem->flags = 0; | 3170 /* pMem->flags = 0; // sqlite3VdbeSerialGet() will set this for us */ |
2852 pMem->zMalloc = 0; | 3171 pMem->szMalloc = 0; |
2853 d += sqlite3VdbeSerialGet(&aKey[d], serial_type, pMem); | 3172 d += sqlite3VdbeSerialGet(&aKey[d], serial_type, pMem); |
2854 pMem++; | 3173 pMem++; |
2855 u++; | 3174 if( (++u)>=p->nField ) break; |
2856 } | 3175 } |
2857 assert( u<=pKeyInfo->nField + 1 ); | 3176 assert( u<=pKeyInfo->nField + 1 ); |
2858 p->nField = u; | 3177 p->nField = u; |
2859 return (void*)p; | |
2860 } | 3178 } |
2861 | 3179 |
| 3180 #if SQLITE_DEBUG |
2862 /* | 3181 /* |
2863 ** This routine destroys a UnpackedRecord object. | 3182 ** This function compares two index or table record keys in the same way |
| 3183 ** as the sqlite3VdbeRecordCompare() routine. Unlike VdbeRecordCompare(), |
| 3184 ** this function deserializes and compares values using the |
| 3185 ** sqlite3VdbeSerialGet() and sqlite3MemCompare() functions. It is used |
| 3186 ** in assert() statements to ensure that the optimized code in |
| 3187 ** sqlite3VdbeRecordCompare() returns results with these two primitives. |
| 3188 ** |
| 3189 ** Return true if the result of comparison is equivalent to desiredResult. |
| 3190 ** Return false if there is a disagreement. |
2864 */ | 3191 */ |
2865 void sqlite3VdbeDeleteUnpackedRecord(UnpackedRecord *p){ | 3192 static int vdbeRecordCompareDebug( |
2866 int i; | |
2867 Mem *pMem; | |
2868 | |
2869 assert( p!=0 ); | |
2870 assert( p->flags & UNPACKED_NEED_DESTROY ); | |
2871 for(i=0, pMem=p->aMem; i<p->nField; i++, pMem++){ | |
2872 /* The unpacked record is always constructed by the | |
2873 ** sqlite3VdbeUnpackRecord() function above, which makes all | |
2874 ** strings and blobs static. And none of the elements are | |
2875 ** ever transformed, so there is never anything to delete. | |
2876 */ | |
2877 if( NEVER(pMem->zMalloc) ) sqlite3VdbeMemRelease(pMem); | |
2878 } | |
2879 if( p->flags & UNPACKED_NEED_FREE ){ | |
2880 sqlite3DbFree(p->pKeyInfo->db, p); | |
2881 } | |
2882 } | |
2883 | |
2884 /* | |
2885 ** This function compares the two table rows or index records | |
2886 ** specified by {nKey1, pKey1} and pPKey2. It returns a negative, zero | |
2887 ** or positive integer if key1 is less than, equal to or | |
2888 ** greater than key2. The {nKey1, pKey1} key must be a blob | |
2889 ** created by th OP_MakeRecord opcode of the VDBE. The pPKey2 | |
2890 ** key must be a parsed key such as obtained from | |
2891 ** sqlite3VdbeParseRecord. | |
2892 ** | |
2893 ** Key1 and Key2 do not have to contain the same number of fields. | |
2894 ** The key with fewer fields is usually compares less than the | |
2895 ** longer key. However if the UNPACKED_INCRKEY flags in pPKey2 is set | |
2896 ** and the common prefixes are equal, then key1 is less than key2. | |
2897 ** Or if the UNPACKED_MATCH_PREFIX flag is set and the prefixes are | |
2898 ** equal, then the keys are considered to be equal and | |
2899 ** the parts beyond the common prefix are ignored. | |
2900 ** | |
2901 ** If the UNPACKED_IGNORE_ROWID flag is set, then the last byte of | |
2902 ** the header of pKey1 is ignored. It is assumed that pKey1 is | |
2903 ** an index key, and thus ends with a rowid value. The last byte | |
2904 ** of the header will therefore be the serial type of the rowid: | |
2905 ** one of 1, 2, 3, 4, 5, 6, 8, or 9 - the integer serial types. | |
2906 ** The serial type of the final rowid will always be a single byte. | |
2907 ** By ignoring this last byte of the header, we force the comparison | |
2908 ** to ignore the rowid at the end of key1. | |
2909 */ | |
2910 int sqlite3VdbeRecordCompare( | |
2911 int nKey1, const void *pKey1, /* Left key */ | 3193 int nKey1, const void *pKey1, /* Left key */ |
2912 UnpackedRecord *pPKey2 /* Right key */ | 3194 const UnpackedRecord *pPKey2, /* Right key */ |
| 3195 int desiredResult /* Correct answer */ |
2913 ){ | 3196 ){ |
2914 int d1; /* Offset into aKey[] of next data element */ | 3197 u32 d1; /* Offset into aKey[] of next data element */ |
2915 u32 idx1; /* Offset into aKey[] of next header element */ | 3198 u32 idx1; /* Offset into aKey[] of next header element */ |
2916 u32 szHdr1; /* Number of bytes in header */ | 3199 u32 szHdr1; /* Number of bytes in header */ |
2917 int i = 0; | 3200 int i = 0; |
2918 int nField; | |
2919 int rc = 0; | 3201 int rc = 0; |
2920 const unsigned char *aKey1 = (const unsigned char *)pKey1; | 3202 const unsigned char *aKey1 = (const unsigned char *)pKey1; |
2921 KeyInfo *pKeyInfo; | 3203 KeyInfo *pKeyInfo; |
2922 Mem mem1; | 3204 Mem mem1; |
2923 | 3205 |
2924 pKeyInfo = pPKey2->pKeyInfo; | 3206 pKeyInfo = pPKey2->pKeyInfo; |
| 3207 if( pKeyInfo->db==0 ) return 1; |
2925 mem1.enc = pKeyInfo->enc; | 3208 mem1.enc = pKeyInfo->enc; |
2926 mem1.db = pKeyInfo->db; | 3209 mem1.db = pKeyInfo->db; |
2927 /* mem1.flags = 0; // Will be initialized by sqlite3VdbeSerialGet() */ | 3210 /* mem1.flags = 0; // Will be initialized by sqlite3VdbeSerialGet() */ |
2928 VVA_ONLY( mem1.zMalloc = 0; ) /* Only needed by assert() statements */ | 3211 VVA_ONLY( mem1.szMalloc = 0; ) /* Only needed by assert() statements */ |
2929 | 3212 |
2930 /* Compilers may complain that mem1.u.i is potentially uninitialized. | 3213 /* Compilers may complain that mem1.u.i is potentially uninitialized. |
2931 ** We could initialize it, as shown here, to silence those complaints. | 3214 ** We could initialize it, as shown here, to silence those complaints. |
2932 ** But in fact, mem1.u.i will never actually be used initialized, and doing | 3215 ** But in fact, mem1.u.i will never actually be used uninitialized, and doing |
2933 ** the unnecessary initialization has a measurable negative performance | 3216 ** the unnecessary initialization has a measurable negative performance |
2934 ** impact, since this routine is a very high runner. And so, we choose | 3217 ** impact, since this routine is a very high runner. And so, we choose |
2935 ** to ignore the compiler warnings and leave this variable uninitialized. | 3218 ** to ignore the compiler warnings and leave this variable uninitialized. |
2936 */ | 3219 */ |
2937 /* mem1.u.i = 0; // not needed, here to silence compiler warning */ | 3220 /* mem1.u.i = 0; // not needed, here to silence compiler warning */ |
2938 | 3221 |
2939 idx1 = getVarint32(aKey1, szHdr1); | 3222 idx1 = getVarint32(aKey1, szHdr1); |
2940 d1 = szHdr1; | 3223 d1 = szHdr1; |
2941 if( pPKey2->flags & UNPACKED_IGNORE_ROWID ){ | 3224 assert( pKeyInfo->nField+pKeyInfo->nXField>=pPKey2->nField || CORRUPT_DB ); |
2942 szHdr1--; | 3225 assert( pKeyInfo->aSortOrder!=0 ); |
2943 } | 3226 assert( pKeyInfo->nField>0 ); |
2944 nField = pKeyInfo->nField; | 3227 assert( idx1<=szHdr1 || CORRUPT_DB ); |
2945 while( idx1<szHdr1 && i<pPKey2->nField ){ | 3228 do{ |
2946 u32 serial_type1; | 3229 u32 serial_type1; |
2947 | 3230 |
2948 /* Read the serial types for the next element in each key. */ | 3231 /* Read the serial types for the next element in each key. */ |
2949 idx1 += getVarint32( aKey1+idx1, serial_type1 ); | 3232 idx1 += getVarint32( aKey1+idx1, serial_type1 ); |
2950 if( d1>=nKey1 && sqlite3VdbeSerialTypeLen(serial_type1)>0 ) break; | 3233 |
| 3234 /* Verify that there is enough key space remaining to avoid |
| 3235 ** a buffer overread. The "d1+serial_type1+2" subexpression will |
| 3236 ** always be greater than or equal to the amount of required key space. |
| 3237 ** Use that approximation to avoid the more expensive call to |
| 3238 ** sqlite3VdbeSerialTypeLen() in the common case. |
| 3239 */ |
| 3240 if( d1+serial_type1+2>(u32)nKey1 |
| 3241 && d1+sqlite3VdbeSerialTypeLen(serial_type1)>(u32)nKey1 |
| 3242 ){ |
| 3243 break; |
| 3244 } |
2951 | 3245 |
2952 /* Extract the values to be compared. | 3246 /* Extract the values to be compared. |
2953 */ | 3247 */ |
2954 d1 += sqlite3VdbeSerialGet(&aKey1[d1], serial_type1, &mem1); | 3248 d1 += sqlite3VdbeSerialGet(&aKey1[d1], serial_type1, &mem1); |
2955 | 3249 |
2956 /* Do the comparison | 3250 /* Do the comparison |
2957 */ | 3251 */ |
2958 rc = sqlite3MemCompare(&mem1, &pPKey2->aMem[i], | 3252 rc = sqlite3MemCompare(&mem1, &pPKey2->aMem[i], pKeyInfo->aColl[i]); |
2959 i<nField ? pKeyInfo->aColl[i] : 0); | |
2960 if( rc!=0 ){ | 3253 if( rc!=0 ){ |
2961 assert( mem1.zMalloc==0 ); /* See comment below */ | 3254 assert( mem1.szMalloc==0 ); /* See comment below */ |
2962 | 3255 if( pKeyInfo->aSortOrder[i] ){ |
2963 /* Invert the result if we are using DESC sort order. */ | 3256 rc = -rc; /* Invert the result for DESC sort order. */ |
2964 if( pKeyInfo->aSortOrder && i<nField && pKeyInfo->aSortOrder[i] ){ | |
2965 rc = -rc; | |
2966 } | 3257 } |
2967 | 3258 goto debugCompareEnd; |
2968 /* If the PREFIX_SEARCH flag is set and all fields except the final | |
2969 ** rowid field were equal, then clear the PREFIX_SEARCH flag and set | |
2970 ** pPKey2->rowid to the value of the rowid field in (pKey1, nKey1). | |
2971 ** This is used by the OP_IsUnique opcode. | |
2972 */ | |
2973 if( (pPKey2->flags & UNPACKED_PREFIX_SEARCH) && i==(pPKey2->nField-1) ){ | |
2974 assert( idx1==szHdr1 && rc ); | |
2975 assert( mem1.flags & MEM_Int ); | |
2976 pPKey2->flags &= ~UNPACKED_PREFIX_SEARCH; | |
2977 pPKey2->rowid = mem1.u.i; | |
2978 } | |
2979 | |
2980 return rc; | |
2981 } | 3259 } |
2982 i++; | 3260 i++; |
2983 } | 3261 }while( idx1<szHdr1 && i<pPKey2->nField ); |
2984 | 3262 |
2985 /* No memory allocation is ever used on mem1. Prove this using | 3263 /* No memory allocation is ever used on mem1. Prove this using |
2986 ** the following assert(). If the assert() fails, it indicates a | 3264 ** the following assert(). If the assert() fails, it indicates a |
2987 ** memory leak and a need to call sqlite3VdbeMemRelease(&mem1). | 3265 ** memory leak and a need to call sqlite3VdbeMemRelease(&mem1). |
2988 */ | 3266 */ |
2989 assert( mem1.zMalloc==0 ); | 3267 assert( mem1.szMalloc==0 ); |
2990 | 3268 |
2991 /* rc==0 here means that one of the keys ran out of fields and | 3269 /* rc==0 here means that one of the keys ran out of fields and |
2992 ** all the fields up to that point were equal. If the UNPACKED_INCRKEY | 3270 ** all the fields up to that point were equal. Return the default_rc |
2993 ** flag is set, then break the tie by treating key2 as larger. | 3271 ** value. */ |
2994 ** If the UPACKED_PREFIX_MATCH flag is set, then keys with common prefixes | 3272 rc = pPKey2->default_rc; |
2995 ** are considered to be equal. Otherwise, the longer key is the | 3273 |
2996 ** larger. As it happens, the pPKey2 will always be the longer | 3274 debugCompareEnd: |
2997 ** if there is a difference. | 3275 if( desiredResult==0 && rc==0 ) return 1; |
| 3276 if( desiredResult<0 && rc<0 ) return 1; |
| 3277 if( desiredResult>0 && rc>0 ) return 1; |
| 3278 if( CORRUPT_DB ) return 1; |
| 3279 if( pKeyInfo->db->mallocFailed ) return 1; |
| 3280 return 0; |
| 3281 } |
| 3282 #endif |
| 3283 |
| 3284 /* |
| 3285 ** Both *pMem1 and *pMem2 contain string values. Compare the two values |
| 3286 ** using the collation sequence pColl. As usual, return a negative , zero |
| 3287 ** or positive value if *pMem1 is less than, equal to or greater than |
| 3288 ** *pMem2, respectively. Similar in spirit to "rc = (*pMem1) - (*pMem2);". |
| 3289 */ |
| 3290 static int vdbeCompareMemString( |
| 3291 const Mem *pMem1, |
| 3292 const Mem *pMem2, |
| 3293 const CollSeq *pColl, |
| 3294 u8 *prcErr /* If an OOM occurs, set to SQLITE_NOMEM */ |
| 3295 ){ |
| 3296 if( pMem1->enc==pColl->enc ){ |
| 3297 /* The strings are already in the correct encoding. Call the |
| 3298 ** comparison function directly */ |
| 3299 return pColl->xCmp(pColl->pUser,pMem1->n,pMem1->z,pMem2->n,pMem2->z); |
| 3300 }else{ |
| 3301 int rc; |
| 3302 const void *v1, *v2; |
| 3303 int n1, n2; |
| 3304 Mem c1; |
| 3305 Mem c2; |
| 3306 sqlite3VdbeMemInit(&c1, pMem1->db, MEM_Null); |
| 3307 sqlite3VdbeMemInit(&c2, pMem1->db, MEM_Null); |
| 3308 sqlite3VdbeMemShallowCopy(&c1, pMem1, MEM_Ephem); |
| 3309 sqlite3VdbeMemShallowCopy(&c2, pMem2, MEM_Ephem); |
| 3310 v1 = sqlite3ValueText((sqlite3_value*)&c1, pColl->enc); |
| 3311 n1 = v1==0 ? 0 : c1.n; |
| 3312 v2 = sqlite3ValueText((sqlite3_value*)&c2, pColl->enc); |
| 3313 n2 = v2==0 ? 0 : c2.n; |
| 3314 rc = pColl->xCmp(pColl->pUser, n1, v1, n2, v2); |
| 3315 sqlite3VdbeMemRelease(&c1); |
| 3316 sqlite3VdbeMemRelease(&c2); |
| 3317 if( (v1==0 || v2==0) && prcErr ) *prcErr = SQLITE_NOMEM; |
| 3318 return rc; |
| 3319 } |
| 3320 } |
| 3321 |
| 3322 /* |
| 3323 ** Compare two blobs. Return negative, zero, or positive if the first |
| 3324 ** is less than, equal to, or greater than the second, respectively. |
| 3325 ** If one blob is a prefix of the other, then the shorter is the lessor. |
| 3326 */ |
| 3327 static SQLITE_NOINLINE int sqlite3BlobCompare(const Mem *pB1, const Mem *pB2){ |
| 3328 int c = memcmp(pB1->z, pB2->z, pB1->n>pB2->n ? pB2->n : pB1->n); |
| 3329 if( c ) return c; |
| 3330 return pB1->n - pB2->n; |
| 3331 } |
| 3332 |
| 3333 |
| 3334 /* |
| 3335 ** Compare the values contained by the two memory cells, returning |
| 3336 ** negative, zero or positive if pMem1 is less than, equal to, or greater |
| 3337 ** than pMem2. Sorting order is NULL's first, followed by numbers (integers |
| 3338 ** and reals) sorted numerically, followed by text ordered by the collating |
| 3339 ** sequence pColl and finally blob's ordered by memcmp(). |
| 3340 ** |
| 3341 ** Two NULL values are considered equal by this function. |
| 3342 */ |
| 3343 int sqlite3MemCompare(const Mem *pMem1, const Mem *pMem2, const CollSeq *pColl){ |
| 3344 int f1, f2; |
| 3345 int combined_flags; |
| 3346 |
| 3347 f1 = pMem1->flags; |
| 3348 f2 = pMem2->flags; |
| 3349 combined_flags = f1|f2; |
| 3350 assert( (combined_flags & MEM_RowSet)==0 ); |
| 3351 |
| 3352 /* If one value is NULL, it is less than the other. If both values |
| 3353 ** are NULL, return 0. |
2998 */ | 3354 */ |
2999 assert( rc==0 ); | 3355 if( combined_flags&MEM_Null ){ |
3000 if( pPKey2->flags & UNPACKED_INCRKEY ){ | 3356 return (f2&MEM_Null) - (f1&MEM_Null); |
3001 rc = -1; | 3357 } |
3002 }else if( pPKey2->flags & UNPACKED_PREFIX_MATCH ){ | 3358 |
3003 /* Leave rc==0 */ | 3359 /* If one value is a number and the other is not, the number is less. |
3004 }else if( idx1<szHdr1 ){ | 3360 ** If both are numbers, compare as reals if one is a real, or as integers |
3005 rc = 1; | 3361 ** if both values are integers. |
3006 } | 3362 */ |
3007 return rc; | 3363 if( combined_flags&(MEM_Int|MEM_Real) ){ |
3008 } | 3364 double r1, r2; |
| 3365 if( (f1 & f2 & MEM_Int)!=0 ){ |
| 3366 if( pMem1->u.i < pMem2->u.i ) return -1; |
| 3367 if( pMem1->u.i > pMem2->u.i ) return 1; |
| 3368 return 0; |
| 3369 } |
| 3370 if( (f1&MEM_Real)!=0 ){ |
| 3371 r1 = pMem1->u.r; |
| 3372 }else if( (f1&MEM_Int)!=0 ){ |
| 3373 r1 = (double)pMem1->u.i; |
| 3374 }else{ |
| 3375 return 1; |
| 3376 } |
| 3377 if( (f2&MEM_Real)!=0 ){ |
| 3378 r2 = pMem2->u.r; |
| 3379 }else if( (f2&MEM_Int)!=0 ){ |
| 3380 r2 = (double)pMem2->u.i; |
| 3381 }else{ |
| 3382 return -1; |
| 3383 } |
| 3384 if( r1<r2 ) return -1; |
| 3385 if( r1>r2 ) return 1; |
| 3386 return 0; |
| 3387 } |
| 3388 |
| 3389 /* If one value is a string and the other is a blob, the string is less. |
| 3390 ** If both are strings, compare using the collating functions. |
| 3391 */ |
| 3392 if( combined_flags&MEM_Str ){ |
| 3393 if( (f1 & MEM_Str)==0 ){ |
| 3394 return 1; |
| 3395 } |
| 3396 if( (f2 & MEM_Str)==0 ){ |
| 3397 return -1; |
| 3398 } |
| 3399 |
| 3400 assert( pMem1->enc==pMem2->enc ); |
| 3401 assert( pMem1->enc==SQLITE_UTF8 || |
| 3402 pMem1->enc==SQLITE_UTF16LE || pMem1->enc==SQLITE_UTF16BE ); |
| 3403 |
| 3404 /* The collation sequence must be defined at this point, even if |
| 3405 ** the user deletes the collation sequence after the vdbe program is |
| 3406 ** compiled (this was not always the case). |
| 3407 */ |
| 3408 assert( !pColl || pColl->xCmp ); |
| 3409 |
| 3410 if( pColl ){ |
| 3411 return vdbeCompareMemString(pMem1, pMem2, pColl, 0); |
| 3412 } |
| 3413 /* If a NULL pointer was passed as the collate function, fall through |
| 3414 ** to the blob case and use memcmp(). */ |
| 3415 } |
3009 | 3416 |
| 3417 /* Both values must be blobs. Compare using memcmp(). */ |
| 3418 return sqlite3BlobCompare(pMem1, pMem2); |
| 3419 } |
| 3420 |
| 3421 |
| 3422 /* |
| 3423 ** The first argument passed to this function is a serial-type that |
| 3424 ** corresponds to an integer - all values between 1 and 9 inclusive |
| 3425 ** except 7. The second points to a buffer containing an integer value |
| 3426 ** serialized according to serial_type. This function deserializes |
| 3427 ** and returns the value. |
| 3428 */ |
| 3429 static i64 vdbeRecordDecodeInt(u32 serial_type, const u8 *aKey){ |
| 3430 u32 y; |
| 3431 assert( CORRUPT_DB || (serial_type>=1 && serial_type<=9 && serial_type!=7) ); |
| 3432 switch( serial_type ){ |
| 3433 case 0: |
| 3434 case 1: |
| 3435 testcase( aKey[0]&0x80 ); |
| 3436 return ONE_BYTE_INT(aKey); |
| 3437 case 2: |
| 3438 testcase( aKey[0]&0x80 ); |
| 3439 return TWO_BYTE_INT(aKey); |
| 3440 case 3: |
| 3441 testcase( aKey[0]&0x80 ); |
| 3442 return THREE_BYTE_INT(aKey); |
| 3443 case 4: { |
| 3444 testcase( aKey[0]&0x80 ); |
| 3445 y = FOUR_BYTE_UINT(aKey); |
| 3446 return (i64)*(int*)&y; |
| 3447 } |
| 3448 case 5: { |
| 3449 testcase( aKey[0]&0x80 ); |
| 3450 return FOUR_BYTE_UINT(aKey+2) + (((i64)1)<<32)*TWO_BYTE_INT(aKey); |
| 3451 } |
| 3452 case 6: { |
| 3453 u64 x = FOUR_BYTE_UINT(aKey); |
| 3454 testcase( aKey[0]&0x80 ); |
| 3455 x = (x<<32) | FOUR_BYTE_UINT(aKey+4); |
| 3456 return (i64)*(i64*)&x; |
| 3457 } |
| 3458 } |
| 3459 |
| 3460 return (serial_type - 8); |
| 3461 } |
| 3462 |
| 3463 /* |
| 3464 ** This function compares the two table rows or index records |
| 3465 ** specified by {nKey1, pKey1} and pPKey2. It returns a negative, zero |
| 3466 ** or positive integer if key1 is less than, equal to or |
| 3467 ** greater than key2. The {nKey1, pKey1} key must be a blob |
| 3468 ** created by the OP_MakeRecord opcode of the VDBE. The pPKey2 |
| 3469 ** key must be a parsed key such as obtained from |
| 3470 ** sqlite3VdbeParseRecord. |
| 3471 ** |
| 3472 ** If argument bSkip is non-zero, it is assumed that the caller has already |
| 3473 ** determined that the first fields of the keys are equal. |
| 3474 ** |
| 3475 ** Key1 and Key2 do not have to contain the same number of fields. If all |
| 3476 ** fields that appear in both keys are equal, then pPKey2->default_rc is |
| 3477 ** returned. |
| 3478 ** |
| 3479 ** If database corruption is discovered, set pPKey2->errCode to |
| 3480 ** SQLITE_CORRUPT and return 0. If an OOM error is encountered, |
| 3481 ** pPKey2->errCode is set to SQLITE_NOMEM and, if it is not NULL, the |
| 3482 ** malloc-failed flag set on database handle (pPKey2->pKeyInfo->db). |
| 3483 */ |
| 3484 static int vdbeRecordCompareWithSkip( |
| 3485 int nKey1, const void *pKey1, /* Left key */ |
| 3486 UnpackedRecord *pPKey2, /* Right key */ |
| 3487 int bSkip /* If true, skip the first field */ |
| 3488 ){ |
| 3489 u32 d1; /* Offset into aKey[] of next data element */ |
| 3490 int i; /* Index of next field to compare */ |
| 3491 u32 szHdr1; /* Size of record header in bytes */ |
| 3492 u32 idx1; /* Offset of first type in header */ |
| 3493 int rc = 0; /* Return value */ |
| 3494 Mem *pRhs = pPKey2->aMem; /* Next field of pPKey2 to compare */ |
| 3495 KeyInfo *pKeyInfo = pPKey2->pKeyInfo; |
| 3496 const unsigned char *aKey1 = (const unsigned char *)pKey1; |
| 3497 Mem mem1; |
| 3498 |
| 3499 /* If bSkip is true, then the caller has already determined that the first |
| 3500 ** two elements in the keys are equal. Fix the various stack variables so |
| 3501 ** that this routine begins comparing at the second field. */ |
| 3502 if( bSkip ){ |
| 3503 u32 s1; |
| 3504 idx1 = 1 + getVarint32(&aKey1[1], s1); |
| 3505 szHdr1 = aKey1[0]; |
| 3506 d1 = szHdr1 + sqlite3VdbeSerialTypeLen(s1); |
| 3507 i = 1; |
| 3508 pRhs++; |
| 3509 }else{ |
| 3510 idx1 = getVarint32(aKey1, szHdr1); |
| 3511 d1 = szHdr1; |
| 3512 if( d1>(unsigned)nKey1 ){ |
| 3513 pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT; |
| 3514 return 0; /* Corruption */ |
| 3515 } |
| 3516 i = 0; |
| 3517 } |
| 3518 |
| 3519 VVA_ONLY( mem1.szMalloc = 0; ) /* Only needed by assert() statements */ |
| 3520 assert( pPKey2->pKeyInfo->nField+pPKey2->pKeyInfo->nXField>=pPKey2->nField |
| 3521 || CORRUPT_DB ); |
| 3522 assert( pPKey2->pKeyInfo->aSortOrder!=0 ); |
| 3523 assert( pPKey2->pKeyInfo->nField>0 ); |
| 3524 assert( idx1<=szHdr1 || CORRUPT_DB ); |
| 3525 do{ |
| 3526 u32 serial_type; |
| 3527 |
| 3528 /* RHS is an integer */ |
| 3529 if( pRhs->flags & MEM_Int ){ |
| 3530 serial_type = aKey1[idx1]; |
| 3531 testcase( serial_type==12 ); |
| 3532 if( serial_type>=12 ){ |
| 3533 rc = +1; |
| 3534 }else if( serial_type==0 ){ |
| 3535 rc = -1; |
| 3536 }else if( serial_type==7 ){ |
| 3537 double rhs = (double)pRhs->u.i; |
| 3538 sqlite3VdbeSerialGet(&aKey1[d1], serial_type, &mem1); |
| 3539 if( mem1.u.r<rhs ){ |
| 3540 rc = -1; |
| 3541 }else if( mem1.u.r>rhs ){ |
| 3542 rc = +1; |
| 3543 } |
| 3544 }else{ |
| 3545 i64 lhs = vdbeRecordDecodeInt(serial_type, &aKey1[d1]); |
| 3546 i64 rhs = pRhs->u.i; |
| 3547 if( lhs<rhs ){ |
| 3548 rc = -1; |
| 3549 }else if( lhs>rhs ){ |
| 3550 rc = +1; |
| 3551 } |
| 3552 } |
| 3553 } |
| 3554 |
| 3555 /* RHS is real */ |
| 3556 else if( pRhs->flags & MEM_Real ){ |
| 3557 serial_type = aKey1[idx1]; |
| 3558 if( serial_type>=12 ){ |
| 3559 rc = +1; |
| 3560 }else if( serial_type==0 ){ |
| 3561 rc = -1; |
| 3562 }else{ |
| 3563 double rhs = pRhs->u.r; |
| 3564 double lhs; |
| 3565 sqlite3VdbeSerialGet(&aKey1[d1], serial_type, &mem1); |
| 3566 if( serial_type==7 ){ |
| 3567 lhs = mem1.u.r; |
| 3568 }else{ |
| 3569 lhs = (double)mem1.u.i; |
| 3570 } |
| 3571 if( lhs<rhs ){ |
| 3572 rc = -1; |
| 3573 }else if( lhs>rhs ){ |
| 3574 rc = +1; |
| 3575 } |
| 3576 } |
| 3577 } |
| 3578 |
| 3579 /* RHS is a string */ |
| 3580 else if( pRhs->flags & MEM_Str ){ |
| 3581 getVarint32(&aKey1[idx1], serial_type); |
| 3582 testcase( serial_type==12 ); |
| 3583 if( serial_type<12 ){ |
| 3584 rc = -1; |
| 3585 }else if( !(serial_type & 0x01) ){ |
| 3586 rc = +1; |
| 3587 }else{ |
| 3588 mem1.n = (serial_type - 12) / 2; |
| 3589 testcase( (d1+mem1.n)==(unsigned)nKey1 ); |
| 3590 testcase( (d1+mem1.n+1)==(unsigned)nKey1 ); |
| 3591 if( (d1+mem1.n) > (unsigned)nKey1 ){ |
| 3592 pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT; |
| 3593 return 0; /* Corruption */ |
| 3594 }else if( pKeyInfo->aColl[i] ){ |
| 3595 mem1.enc = pKeyInfo->enc; |
| 3596 mem1.db = pKeyInfo->db; |
| 3597 mem1.flags = MEM_Str; |
| 3598 mem1.z = (char*)&aKey1[d1]; |
| 3599 rc = vdbeCompareMemString( |
| 3600 &mem1, pRhs, pKeyInfo->aColl[i], &pPKey2->errCode |
| 3601 ); |
| 3602 }else{ |
| 3603 int nCmp = MIN(mem1.n, pRhs->n); |
| 3604 rc = memcmp(&aKey1[d1], pRhs->z, nCmp); |
| 3605 if( rc==0 ) rc = mem1.n - pRhs->n; |
| 3606 } |
| 3607 } |
| 3608 } |
| 3609 |
| 3610 /* RHS is a blob */ |
| 3611 else if( pRhs->flags & MEM_Blob ){ |
| 3612 getVarint32(&aKey1[idx1], serial_type); |
| 3613 testcase( serial_type==12 ); |
| 3614 if( serial_type<12 || (serial_type & 0x01) ){ |
| 3615 rc = -1; |
| 3616 }else{ |
| 3617 int nStr = (serial_type - 12) / 2; |
| 3618 testcase( (d1+nStr)==(unsigned)nKey1 ); |
| 3619 testcase( (d1+nStr+1)==(unsigned)nKey1 ); |
| 3620 if( (d1+nStr) > (unsigned)nKey1 ){ |
| 3621 pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT; |
| 3622 return 0; /* Corruption */ |
| 3623 }else{ |
| 3624 int nCmp = MIN(nStr, pRhs->n); |
| 3625 rc = memcmp(&aKey1[d1], pRhs->z, nCmp); |
| 3626 if( rc==0 ) rc = nStr - pRhs->n; |
| 3627 } |
| 3628 } |
| 3629 } |
| 3630 |
| 3631 /* RHS is null */ |
| 3632 else{ |
| 3633 serial_type = aKey1[idx1]; |
| 3634 rc = (serial_type!=0); |
| 3635 } |
| 3636 |
| 3637 if( rc!=0 ){ |
| 3638 if( pKeyInfo->aSortOrder[i] ){ |
| 3639 rc = -rc; |
| 3640 } |
| 3641 assert( vdbeRecordCompareDebug(nKey1, pKey1, pPKey2, rc) ); |
| 3642 assert( mem1.szMalloc==0 ); /* See comment below */ |
| 3643 return rc; |
| 3644 } |
| 3645 |
| 3646 i++; |
| 3647 pRhs++; |
| 3648 d1 += sqlite3VdbeSerialTypeLen(serial_type); |
| 3649 idx1 += sqlite3VarintLen(serial_type); |
| 3650 }while( idx1<(unsigned)szHdr1 && i<pPKey2->nField && d1<=(unsigned)nKey1 ); |
| 3651 |
| 3652 /* No memory allocation is ever used on mem1. Prove this using |
| 3653 ** the following assert(). If the assert() fails, it indicates a |
| 3654 ** memory leak and a need to call sqlite3VdbeMemRelease(&mem1). */ |
| 3655 assert( mem1.szMalloc==0 ); |
| 3656 |
| 3657 /* rc==0 here means that one or both of the keys ran out of fields and |
| 3658 ** all the fields up to that point were equal. Return the default_rc |
| 3659 ** value. */ |
| 3660 assert( CORRUPT_DB |
| 3661 || vdbeRecordCompareDebug(nKey1, pKey1, pPKey2, pPKey2->default_rc) |
| 3662 || pKeyInfo->db->mallocFailed |
| 3663 ); |
| 3664 return pPKey2->default_rc; |
| 3665 } |
| 3666 int sqlite3VdbeRecordCompare( |
| 3667 int nKey1, const void *pKey1, /* Left key */ |
| 3668 UnpackedRecord *pPKey2 /* Right key */ |
| 3669 ){ |
| 3670 return vdbeRecordCompareWithSkip(nKey1, pKey1, pPKey2, 0); |
| 3671 } |
| 3672 |
| 3673 |
| 3674 /* |
| 3675 ** This function is an optimized version of sqlite3VdbeRecordCompare() |
| 3676 ** that (a) the first field of pPKey2 is an integer, and (b) the |
| 3677 ** size-of-header varint at the start of (pKey1/nKey1) fits in a single |
| 3678 ** byte (i.e. is less than 128). |
| 3679 ** |
| 3680 ** To avoid concerns about buffer overreads, this routine is only used |
| 3681 ** on schemas where the maximum valid header size is 63 bytes or less. |
| 3682 */ |
| 3683 static int vdbeRecordCompareInt( |
| 3684 int nKey1, const void *pKey1, /* Left key */ |
| 3685 UnpackedRecord *pPKey2 /* Right key */ |
| 3686 ){ |
| 3687 const u8 *aKey = &((const u8*)pKey1)[*(const u8*)pKey1 & 0x3F]; |
| 3688 int serial_type = ((const u8*)pKey1)[1]; |
| 3689 int res; |
| 3690 u32 y; |
| 3691 u64 x; |
| 3692 i64 v = pPKey2->aMem[0].u.i; |
| 3693 i64 lhs; |
| 3694 |
| 3695 assert( (*(u8*)pKey1)<=0x3F || CORRUPT_DB ); |
| 3696 switch( serial_type ){ |
| 3697 case 1: { /* 1-byte signed integer */ |
| 3698 lhs = ONE_BYTE_INT(aKey); |
| 3699 testcase( lhs<0 ); |
| 3700 break; |
| 3701 } |
| 3702 case 2: { /* 2-byte signed integer */ |
| 3703 lhs = TWO_BYTE_INT(aKey); |
| 3704 testcase( lhs<0 ); |
| 3705 break; |
| 3706 } |
| 3707 case 3: { /* 3-byte signed integer */ |
| 3708 lhs = THREE_BYTE_INT(aKey); |
| 3709 testcase( lhs<0 ); |
| 3710 break; |
| 3711 } |
| 3712 case 4: { /* 4-byte signed integer */ |
| 3713 y = FOUR_BYTE_UINT(aKey); |
| 3714 lhs = (i64)*(int*)&y; |
| 3715 testcase( lhs<0 ); |
| 3716 break; |
| 3717 } |
| 3718 case 5: { /* 6-byte signed integer */ |
| 3719 lhs = FOUR_BYTE_UINT(aKey+2) + (((i64)1)<<32)*TWO_BYTE_INT(aKey); |
| 3720 testcase( lhs<0 ); |
| 3721 break; |
| 3722 } |
| 3723 case 6: { /* 8-byte signed integer */ |
| 3724 x = FOUR_BYTE_UINT(aKey); |
| 3725 x = (x<<32) | FOUR_BYTE_UINT(aKey+4); |
| 3726 lhs = *(i64*)&x; |
| 3727 testcase( lhs<0 ); |
| 3728 break; |
| 3729 } |
| 3730 case 8: |
| 3731 lhs = 0; |
| 3732 break; |
| 3733 case 9: |
| 3734 lhs = 1; |
| 3735 break; |
| 3736 |
| 3737 /* This case could be removed without changing the results of running |
| 3738 ** this code. Including it causes gcc to generate a faster switch |
| 3739 ** statement (since the range of switch targets now starts at zero and |
| 3740 ** is contiguous) but does not cause any duplicate code to be generated |
| 3741 ** (as gcc is clever enough to combine the two like cases). Other |
| 3742 ** compilers might be similar. */ |
| 3743 case 0: case 7: |
| 3744 return sqlite3VdbeRecordCompare(nKey1, pKey1, pPKey2); |
| 3745 |
| 3746 default: |
| 3747 return sqlite3VdbeRecordCompare(nKey1, pKey1, pPKey2); |
| 3748 } |
| 3749 |
| 3750 if( v>lhs ){ |
| 3751 res = pPKey2->r1; |
| 3752 }else if( v<lhs ){ |
| 3753 res = pPKey2->r2; |
| 3754 }else if( pPKey2->nField>1 ){ |
| 3755 /* The first fields of the two keys are equal. Compare the trailing |
| 3756 ** fields. */ |
| 3757 res = vdbeRecordCompareWithSkip(nKey1, pKey1, pPKey2, 1); |
| 3758 }else{ |
| 3759 /* The first fields of the two keys are equal and there are no trailing |
| 3760 ** fields. Return pPKey2->default_rc in this case. */ |
| 3761 res = pPKey2->default_rc; |
| 3762 } |
| 3763 |
| 3764 assert( vdbeRecordCompareDebug(nKey1, pKey1, pPKey2, res) ); |
| 3765 return res; |
| 3766 } |
| 3767 |
| 3768 /* |
| 3769 ** This function is an optimized version of sqlite3VdbeRecordCompare() |
| 3770 ** that (a) the first field of pPKey2 is a string, that (b) the first field |
| 3771 ** uses the collation sequence BINARY and (c) that the size-of-header varint |
| 3772 ** at the start of (pKey1/nKey1) fits in a single byte. |
| 3773 */ |
| 3774 static int vdbeRecordCompareString( |
| 3775 int nKey1, const void *pKey1, /* Left key */ |
| 3776 UnpackedRecord *pPKey2 /* Right key */ |
| 3777 ){ |
| 3778 const u8 *aKey1 = (const u8*)pKey1; |
| 3779 int serial_type; |
| 3780 int res; |
| 3781 |
| 3782 getVarint32(&aKey1[1], serial_type); |
| 3783 if( serial_type<12 ){ |
| 3784 res = pPKey2->r1; /* (pKey1/nKey1) is a number or a null */ |
| 3785 }else if( !(serial_type & 0x01) ){ |
| 3786 res = pPKey2->r2; /* (pKey1/nKey1) is a blob */ |
| 3787 }else{ |
| 3788 int nCmp; |
| 3789 int nStr; |
| 3790 int szHdr = aKey1[0]; |
| 3791 |
| 3792 nStr = (serial_type-12) / 2; |
| 3793 if( (szHdr + nStr) > nKey1 ){ |
| 3794 pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT; |
| 3795 return 0; /* Corruption */ |
| 3796 } |
| 3797 nCmp = MIN( pPKey2->aMem[0].n, nStr ); |
| 3798 res = memcmp(&aKey1[szHdr], pPKey2->aMem[0].z, nCmp); |
| 3799 |
| 3800 if( res==0 ){ |
| 3801 res = nStr - pPKey2->aMem[0].n; |
| 3802 if( res==0 ){ |
| 3803 if( pPKey2->nField>1 ){ |
| 3804 res = vdbeRecordCompareWithSkip(nKey1, pKey1, pPKey2, 1); |
| 3805 }else{ |
| 3806 res = pPKey2->default_rc; |
| 3807 } |
| 3808 }else if( res>0 ){ |
| 3809 res = pPKey2->r2; |
| 3810 }else{ |
| 3811 res = pPKey2->r1; |
| 3812 } |
| 3813 }else if( res>0 ){ |
| 3814 res = pPKey2->r2; |
| 3815 }else{ |
| 3816 res = pPKey2->r1; |
| 3817 } |
| 3818 } |
| 3819 |
| 3820 assert( vdbeRecordCompareDebug(nKey1, pKey1, pPKey2, res) |
| 3821 || CORRUPT_DB |
| 3822 || pPKey2->pKeyInfo->db->mallocFailed |
| 3823 ); |
| 3824 return res; |
| 3825 } |
| 3826 |
| 3827 /* |
| 3828 ** Return a pointer to an sqlite3VdbeRecordCompare() compatible function |
| 3829 ** suitable for comparing serialized records to the unpacked record passed |
| 3830 ** as the only argument. |
| 3831 */ |
| 3832 RecordCompare sqlite3VdbeFindCompare(UnpackedRecord *p){ |
| 3833 /* varintRecordCompareInt() and varintRecordCompareString() both assume |
| 3834 ** that the size-of-header varint that occurs at the start of each record |
| 3835 ** fits in a single byte (i.e. is 127 or less). varintRecordCompareInt() |
| 3836 ** also assumes that it is safe to overread a buffer by at least the |
| 3837 ** maximum possible legal header size plus 8 bytes. Because there is |
| 3838 ** guaranteed to be at least 74 (but not 136) bytes of padding following each |
| 3839 ** buffer passed to varintRecordCompareInt() this makes it convenient to |
| 3840 ** limit the size of the header to 64 bytes in cases where the first field |
| 3841 ** is an integer. |
| 3842 ** |
| 3843 ** The easiest way to enforce this limit is to consider only records with |
| 3844 ** 13 fields or less. If the first field is an integer, the maximum legal |
| 3845 ** header size is (12*5 + 1 + 1) bytes. */ |
| 3846 if( (p->pKeyInfo->nField + p->pKeyInfo->nXField)<=13 ){ |
| 3847 int flags = p->aMem[0].flags; |
| 3848 if( p->pKeyInfo->aSortOrder[0] ){ |
| 3849 p->r1 = 1; |
| 3850 p->r2 = -1; |
| 3851 }else{ |
| 3852 p->r1 = -1; |
| 3853 p->r2 = 1; |
| 3854 } |
| 3855 if( (flags & MEM_Int) ){ |
| 3856 return vdbeRecordCompareInt; |
| 3857 } |
| 3858 testcase( flags & MEM_Real ); |
| 3859 testcase( flags & MEM_Null ); |
| 3860 testcase( flags & MEM_Blob ); |
| 3861 if( (flags & (MEM_Real|MEM_Null|MEM_Blob))==0 && p->pKeyInfo->aColl[0]==0 ){ |
| 3862 assert( flags & MEM_Str ); |
| 3863 return vdbeRecordCompareString; |
| 3864 } |
| 3865 } |
| 3866 |
| 3867 return sqlite3VdbeRecordCompare; |
| 3868 } |
3010 | 3869 |
3011 /* | 3870 /* |
3012 ** pCur points at an index entry created using the OP_MakeRecord opcode. | 3871 ** pCur points at an index entry created using the OP_MakeRecord opcode. |
3013 ** Read the rowid (the last field in the record) and store it in *rowid. | 3872 ** Read the rowid (the last field in the record) and store it in *rowid. |
3014 ** Return SQLITE_OK if everything works, or an error code otherwise. | 3873 ** Return SQLITE_OK if everything works, or an error code otherwise. |
3015 ** | 3874 ** |
3016 ** pCur might be pointing to text obtained from a corrupt database file. | 3875 ** pCur might be pointing to text obtained from a corrupt database file. |
3017 ** So the content cannot be trusted. Do appropriate checks on the content. | 3876 ** So the content cannot be trusted. Do appropriate checks on the content. |
3018 */ | 3877 */ |
3019 int sqlite3VdbeIdxRowid(sqlite3 *db, BtCursor *pCur, i64 *rowid){ | 3878 int sqlite3VdbeIdxRowid(sqlite3 *db, BtCursor *pCur, i64 *rowid){ |
3020 i64 nCellKey = 0; | 3879 i64 nCellKey = 0; |
3021 int rc; | 3880 int rc; |
3022 u32 szHdr; /* Size of the header */ | 3881 u32 szHdr; /* Size of the header */ |
3023 u32 typeRowid; /* Serial type of the rowid */ | 3882 u32 typeRowid; /* Serial type of the rowid */ |
3024 u32 lenRowid; /* Size of the rowid */ | 3883 u32 lenRowid; /* Size of the rowid */ |
3025 Mem m, v; | 3884 Mem m, v; |
3026 | 3885 |
3027 UNUSED_PARAMETER(db); | |
3028 | |
3029 /* Get the size of the index entry. Only indices entries of less | 3886 /* Get the size of the index entry. Only indices entries of less |
3030 ** than 2GiB are support - anything large must be database corruption. | 3887 ** than 2GiB are support - anything large must be database corruption. |
3031 ** Any corruption is detected in sqlite3BtreeParseCellPtr(), though, so | 3888 ** Any corruption is detected in sqlite3BtreeParseCellPtr(), though, so |
3032 ** this code can safely assume that nCellKey is 32-bits | 3889 ** this code can safely assume that nCellKey is 32-bits |
3033 */ | 3890 */ |
3034 assert( sqlite3BtreeCursorIsValid(pCur) ); | 3891 assert( sqlite3BtreeCursorIsValid(pCur) ); |
3035 rc = sqlite3BtreeKeySize(pCur, &nCellKey); | 3892 VVA_ONLY(rc =) sqlite3BtreeKeySize(pCur, &nCellKey); |
3036 assert( rc==SQLITE_OK ); /* pCur is always valid so KeySize cannot fail */ | 3893 assert( rc==SQLITE_OK ); /* pCur is always valid so KeySize cannot fail */ |
3037 assert( (nCellKey & SQLITE_MAX_U32)==(u64)nCellKey ); | 3894 assert( (nCellKey & SQLITE_MAX_U32)==(u64)nCellKey ); |
3038 | 3895 |
3039 /* Read in the complete content of the index entry */ | 3896 /* Read in the complete content of the index entry */ |
3040 memset(&m, 0, sizeof(m)); | 3897 sqlite3VdbeMemInit(&m, db, 0); |
3041 rc = sqlite3VdbeMemFromBtree(pCur, 0, (int)nCellKey, 1, &m); | 3898 rc = sqlite3VdbeMemFromBtree(pCur, 0, (u32)nCellKey, 1, &m); |
3042 if( rc ){ | 3899 if( rc ){ |
3043 return rc; | 3900 return rc; |
3044 } | 3901 } |
3045 | 3902 |
3046 /* The index entry must begin with a header size */ | 3903 /* The index entry must begin with a header size */ |
3047 (void)getVarint32((u8*)m.z, szHdr); | 3904 (void)getVarint32((u8*)m.z, szHdr); |
3048 testcase( szHdr==3 ); | 3905 testcase( szHdr==3 ); |
3049 testcase( szHdr==m.n ); | 3906 testcase( szHdr==m.n ); |
3050 if( unlikely(szHdr<3 || (int)szHdr>m.n) ){ | 3907 if( unlikely(szHdr<3 || (int)szHdr>m.n) ){ |
3051 goto idx_rowid_corruption; | 3908 goto idx_rowid_corruption; |
(...skipping 21 matching lines...) Expand all Loading... |
3073 | 3930 |
3074 /* Fetch the integer off the end of the index record */ | 3931 /* Fetch the integer off the end of the index record */ |
3075 sqlite3VdbeSerialGet((u8*)&m.z[m.n-lenRowid], typeRowid, &v); | 3932 sqlite3VdbeSerialGet((u8*)&m.z[m.n-lenRowid], typeRowid, &v); |
3076 *rowid = v.u.i; | 3933 *rowid = v.u.i; |
3077 sqlite3VdbeMemRelease(&m); | 3934 sqlite3VdbeMemRelease(&m); |
3078 return SQLITE_OK; | 3935 return SQLITE_OK; |
3079 | 3936 |
3080 /* Jump here if database corruption is detected after m has been | 3937 /* Jump here if database corruption is detected after m has been |
3081 ** allocated. Free the m object and return SQLITE_CORRUPT. */ | 3938 ** allocated. Free the m object and return SQLITE_CORRUPT. */ |
3082 idx_rowid_corruption: | 3939 idx_rowid_corruption: |
3083 testcase( m.zMalloc!=0 ); | 3940 testcase( m.szMalloc!=0 ); |
3084 sqlite3VdbeMemRelease(&m); | 3941 sqlite3VdbeMemRelease(&m); |
3085 return SQLITE_CORRUPT_BKPT; | 3942 return SQLITE_CORRUPT_BKPT; |
3086 } | 3943 } |
3087 | 3944 |
3088 /* | 3945 /* |
3089 ** Compare the key of the index entry that cursor pC is pointing to against | 3946 ** Compare the key of the index entry that cursor pC is pointing to against |
3090 ** the key string in pUnpacked. Write into *pRes a number | 3947 ** the key string in pUnpacked. Write into *pRes a number |
3091 ** that is negative, zero, or positive if pC is less than, equal to, | 3948 ** that is negative, zero, or positive if pC is less than, equal to, |
3092 ** or greater than pUnpacked. Return SQLITE_OK on success. | 3949 ** or greater than pUnpacked. Return SQLITE_OK on success. |
3093 ** | 3950 ** |
3094 ** pUnpacked is either created without a rowid or is truncated so that it | 3951 ** pUnpacked is either created without a rowid or is truncated so that it |
3095 ** omits the rowid at the end. The rowid at the end of the index entry | 3952 ** omits the rowid at the end. The rowid at the end of the index entry |
3096 ** is ignored as well. Hence, this routine only compares the prefixes | 3953 ** is ignored as well. Hence, this routine only compares the prefixes |
3097 ** of the keys prior to the final rowid, not the entire key. | 3954 ** of the keys prior to the final rowid, not the entire key. |
3098 */ | 3955 */ |
3099 int sqlite3VdbeIdxKeyCompare( | 3956 int sqlite3VdbeIdxKeyCompare( |
3100 VdbeCursor *pC, /* The cursor to compare against */ | 3957 sqlite3 *db, /* Database connection */ |
3101 UnpackedRecord *pUnpacked, /* Unpacked version of key to compare against */ | 3958 VdbeCursor *pC, /* The cursor to compare against */ |
3102 int *res /* Write the comparison result here */ | 3959 UnpackedRecord *pUnpacked, /* Unpacked version of key */ |
| 3960 int *res /* Write the comparison result here */ |
3103 ){ | 3961 ){ |
3104 i64 nCellKey = 0; | 3962 i64 nCellKey = 0; |
3105 int rc; | 3963 int rc; |
3106 BtCursor *pCur = pC->pCursor; | 3964 BtCursor *pCur = pC->pCursor; |
3107 Mem m; | 3965 Mem m; |
3108 | 3966 |
3109 assert( sqlite3BtreeCursorIsValid(pCur) ); | 3967 assert( sqlite3BtreeCursorIsValid(pCur) ); |
3110 rc = sqlite3BtreeKeySize(pCur, &nCellKey); | 3968 VVA_ONLY(rc =) sqlite3BtreeKeySize(pCur, &nCellKey); |
3111 assert( rc==SQLITE_OK ); /* pCur is always valid so KeySize cannot fail */ | 3969 assert( rc==SQLITE_OK ); /* pCur is always valid so KeySize cannot fail */ |
3112 /* nCellKey will always be between 0 and 0xffffffff because of the say | 3970 /* nCellKey will always be between 0 and 0xffffffff because of the way |
3113 ** that btreeParseCellPtr() and sqlite3GetVarint32() are implemented */ | 3971 ** that btreeParseCellPtr() and sqlite3GetVarint32() are implemented */ |
3114 if( nCellKey<=0 || nCellKey>0x7fffffff ){ | 3972 if( nCellKey<=0 || nCellKey>0x7fffffff ){ |
3115 *res = 0; | 3973 *res = 0; |
3116 return SQLITE_CORRUPT_BKPT; | 3974 return SQLITE_CORRUPT_BKPT; |
3117 } | 3975 } |
3118 memset(&m, 0, sizeof(m)); | 3976 sqlite3VdbeMemInit(&m, db, 0); |
3119 rc = sqlite3VdbeMemFromBtree(pC->pCursor, 0, (int)nCellKey, 1, &m); | 3977 rc = sqlite3VdbeMemFromBtree(pC->pCursor, 0, (u32)nCellKey, 1, &m); |
3120 if( rc ){ | 3978 if( rc ){ |
3121 return rc; | 3979 return rc; |
3122 } | 3980 } |
3123 assert( pUnpacked->flags & UNPACKED_IGNORE_ROWID ); | |
3124 *res = sqlite3VdbeRecordCompare(m.n, m.z, pUnpacked); | 3981 *res = sqlite3VdbeRecordCompare(m.n, m.z, pUnpacked); |
3125 sqlite3VdbeMemRelease(&m); | 3982 sqlite3VdbeMemRelease(&m); |
3126 return SQLITE_OK; | 3983 return SQLITE_OK; |
3127 } | 3984 } |
3128 | 3985 |
3129 /* | 3986 /* |
3130 ** This routine sets the value to be returned by subsequent calls to | 3987 ** This routine sets the value to be returned by subsequent calls to |
3131 ** sqlite3_changes() on the database handle 'db'. | 3988 ** sqlite3_changes() on the database handle 'db'. |
3132 */ | 3989 */ |
3133 void sqlite3VdbeSetChanges(sqlite3 *db, int nChange){ | 3990 void sqlite3VdbeSetChanges(sqlite3 *db, int nChange){ |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3169 } | 4026 } |
3170 | 4027 |
3171 /* | 4028 /* |
3172 ** Return a pointer to an sqlite3_value structure containing the value bound | 4029 ** Return a pointer to an sqlite3_value structure containing the value bound |
3173 ** parameter iVar of VM v. Except, if the value is an SQL NULL, return | 4030 ** parameter iVar of VM v. Except, if the value is an SQL NULL, return |
3174 ** 0 instead. Unless it is NULL, apply affinity aff (one of the SQLITE_AFF_* | 4031 ** 0 instead. Unless it is NULL, apply affinity aff (one of the SQLITE_AFF_* |
3175 ** constants) to the value before returning it. | 4032 ** constants) to the value before returning it. |
3176 ** | 4033 ** |
3177 ** The returned value must be freed by the caller using sqlite3ValueFree(). | 4034 ** The returned value must be freed by the caller using sqlite3ValueFree(). |
3178 */ | 4035 */ |
3179 sqlite3_value *sqlite3VdbeGetValue(Vdbe *v, int iVar, u8 aff){ | 4036 sqlite3_value *sqlite3VdbeGetBoundValue(Vdbe *v, int iVar, u8 aff){ |
3180 assert( iVar>0 ); | 4037 assert( iVar>0 ); |
3181 if( v ){ | 4038 if( v ){ |
3182 Mem *pMem = &v->aVar[iVar-1]; | 4039 Mem *pMem = &v->aVar[iVar-1]; |
3183 if( 0==(pMem->flags & MEM_Null) ){ | 4040 if( 0==(pMem->flags & MEM_Null) ){ |
3184 sqlite3_value *pRet = sqlite3ValueNew(v->db); | 4041 sqlite3_value *pRet = sqlite3ValueNew(v->db); |
3185 if( pRet ){ | 4042 if( pRet ){ |
3186 sqlite3VdbeMemCopy((Mem *)pRet, pMem); | 4043 sqlite3VdbeMemCopy((Mem *)pRet, pMem); |
3187 sqlite3ValueApplyAffinity(pRet, aff, SQLITE_UTF8); | 4044 sqlite3ValueApplyAffinity(pRet, aff, SQLITE_UTF8); |
3188 sqlite3VdbeMemStoreType((Mem *)pRet); | |
3189 } | 4045 } |
3190 return pRet; | 4046 return pRet; |
3191 } | 4047 } |
3192 } | 4048 } |
3193 return 0; | 4049 return 0; |
3194 } | 4050 } |
3195 | 4051 |
3196 /* | 4052 /* |
3197 ** Configure SQL variable iVar so that binding a new value to it signals | 4053 ** Configure SQL variable iVar so that binding a new value to it signals |
3198 ** to sqlite3_reoptimize() that re-preparing the statement may result | 4054 ** to sqlite3_reoptimize() that re-preparing the statement may result |
3199 ** in a better query plan. | 4055 ** in a better query plan. |
3200 */ | 4056 */ |
3201 void sqlite3VdbeSetVarmask(Vdbe *v, int iVar){ | 4057 void sqlite3VdbeSetVarmask(Vdbe *v, int iVar){ |
3202 assert( iVar>0 ); | 4058 assert( iVar>0 ); |
3203 if( iVar>32 ){ | 4059 if( iVar>32 ){ |
3204 v->expmask = 0xffffffff; | 4060 v->expmask = 0xffffffff; |
3205 }else{ | 4061 }else{ |
3206 v->expmask |= ((u32)1 << (iVar-1)); | 4062 v->expmask |= ((u32)1 << (iVar-1)); |
3207 } | 4063 } |
3208 } | 4064 } |
| 4065 |
| 4066 #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 4067 /* |
| 4068 ** Transfer error message text from an sqlite3_vtab.zErrMsg (text stored |
| 4069 ** in memory obtained from sqlite3_malloc) into a Vdbe.zErrMsg (text stored |
| 4070 ** in memory obtained from sqlite3DbMalloc). |
| 4071 */ |
| 4072 void sqlite3VtabImportErrmsg(Vdbe *p, sqlite3_vtab *pVtab){ |
| 4073 sqlite3 *db = p->db; |
| 4074 sqlite3DbFree(db, p->zErrMsg); |
| 4075 p->zErrMsg = sqlite3DbStrDup(db, pVtab->zErrMsg); |
| 4076 sqlite3_free(pVtab->zErrMsg); |
| 4077 pVtab->zErrMsg = 0; |
| 4078 } |
| 4079 #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
OLD | NEW |