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