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