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