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