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