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