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