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 |
| 13 | ** a VDBE (or an "sqlite_vm" as it is known to the outside world.) Prior |
| 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 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 36 | Vdbe *sqlite3VdbeCreate(sqlite *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 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 111 | int sqlite3VdbeOp3(Vdbe *p, int op, int p1, int p2, const char *zP3, int p3type){ |
| 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 | /* |
| 118 | ** Add multiple opcodes. The list is terminated by an opcode of 0. |
| 119 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 120 | int sqlite3VdbeCode(Vdbe *p, ...){ |
drh | 701a0ae | 2004-02-22 20:05:00 +0000 | [diff] [blame] | 121 | int addr; |
| 122 | va_list ap; |
| 123 | int opcode, p1, p2; |
| 124 | va_start(ap, p); |
| 125 | addr = p->nOp; |
| 126 | while( (opcode = va_arg(ap,int))!=0 ){ |
| 127 | p1 = va_arg(ap,int); |
| 128 | p2 = va_arg(ap,int); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 129 | sqlite3VdbeAddOp(p, opcode, p1, p2); |
drh | 701a0ae | 2004-02-22 20:05:00 +0000 | [diff] [blame] | 130 | } |
| 131 | va_end(ap); |
| 132 | return addr; |
| 133 | } |
| 134 | |
| 135 | |
| 136 | |
| 137 | /* |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 138 | ** Create a new symbolic label for an instruction that has yet to be |
| 139 | ** coded. The symbolic label is really just a negative number. The |
| 140 | ** label can be used as the P2 value of an operation. Later, when |
| 141 | ** the label is resolved to a specific address, the VDBE will scan |
| 142 | ** through its operation list and change all values of P2 which match |
| 143 | ** the label into the resolved address. |
| 144 | ** |
| 145 | ** The VDBE knows that a P2 value is a label because labels are |
| 146 | ** always negative and P2 values are suppose to be non-negative. |
| 147 | ** Hence, a negative P2 value is a label that has yet to be resolved. |
| 148 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 149 | int sqlite3VdbeMakeLabel(Vdbe *p){ |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 150 | int i; |
| 151 | i = p->nLabel++; |
| 152 | assert( p->magic==VDBE_MAGIC_INIT ); |
| 153 | if( i>=p->nLabelAlloc ){ |
| 154 | int *aNew; |
| 155 | p->nLabelAlloc = p->nLabelAlloc*2 + 10; |
| 156 | aNew = sqliteRealloc( p->aLabel, p->nLabelAlloc*sizeof(p->aLabel[0])); |
| 157 | if( aNew==0 ){ |
| 158 | sqliteFree(p->aLabel); |
| 159 | } |
| 160 | p->aLabel = aNew; |
| 161 | } |
| 162 | if( p->aLabel==0 ){ |
| 163 | p->nLabel = 0; |
| 164 | p->nLabelAlloc = 0; |
| 165 | return 0; |
| 166 | } |
| 167 | p->aLabel[i] = -1; |
| 168 | return -1-i; |
| 169 | } |
| 170 | |
| 171 | /* |
| 172 | ** Resolve label "x" to be the address of the next instruction to |
| 173 | ** be inserted. The parameter "x" must have been obtained from |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 174 | ** a prior call to sqlite3VdbeMakeLabel(). |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 175 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 176 | void sqlite3VdbeResolveLabel(Vdbe *p, int x){ |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 177 | int j; |
| 178 | assert( p->magic==VDBE_MAGIC_INIT ); |
| 179 | if( x<0 && (-x)<=p->nLabel && p->aOp ){ |
| 180 | if( p->aLabel[-1-x]==p->nOp ) return; |
| 181 | assert( p->aLabel[-1-x]<0 ); |
| 182 | p->aLabel[-1-x] = p->nOp; |
| 183 | for(j=0; j<p->nOp; j++){ |
| 184 | if( p->aOp[j].p2==x ) p->aOp[j].p2 = p->nOp; |
| 185 | } |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | /* |
| 190 | ** Return the address of the next instruction to be inserted. |
| 191 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 192 | int sqlite3VdbeCurrentAddr(Vdbe *p){ |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 193 | assert( p->magic==VDBE_MAGIC_INIT ); |
| 194 | return p->nOp; |
| 195 | } |
| 196 | |
| 197 | /* |
| 198 | ** Add a whole list of operations to the operation stack. Return the |
| 199 | ** address of the first operation added. |
| 200 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 201 | int sqlite3VdbeAddOpList(Vdbe *p, int nOp, VdbeOpList const *aOp){ |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 202 | int addr; |
| 203 | assert( p->magic==VDBE_MAGIC_INIT ); |
| 204 | if( p->nOp + nOp >= p->nOpAlloc ){ |
| 205 | int oldSize = p->nOpAlloc; |
| 206 | Op *aNew; |
| 207 | p->nOpAlloc = p->nOpAlloc*2 + nOp + 10; |
| 208 | aNew = sqliteRealloc(p->aOp, p->nOpAlloc*sizeof(Op)); |
| 209 | if( aNew==0 ){ |
| 210 | p->nOpAlloc = oldSize; |
| 211 | return 0; |
| 212 | } |
| 213 | p->aOp = aNew; |
| 214 | memset(&p->aOp[oldSize], 0, (p->nOpAlloc-oldSize)*sizeof(Op)); |
| 215 | } |
| 216 | addr = p->nOp; |
| 217 | if( nOp>0 ){ |
| 218 | int i; |
drh | 905793e | 2004-02-21 13:31:09 +0000 | [diff] [blame] | 219 | VdbeOpList const *pIn = aOp; |
| 220 | for(i=0; i<nOp; i++, pIn++){ |
| 221 | int p2 = pIn->p2; |
| 222 | VdbeOp *pOut = &p->aOp[i+addr]; |
| 223 | pOut->opcode = pIn->opcode; |
| 224 | pOut->p1 = pIn->p1; |
| 225 | pOut->p2 = p2<0 ? addr + ADDR(p2) : p2; |
| 226 | pOut->p3 = pIn->p3; |
| 227 | pOut->p3type = pIn->p3 ? P3_STATIC : P3_NOTUSED; |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 228 | #ifndef NDEBUG |
danielk1977 | 132872b | 2004-05-10 10:37:18 +0000 | [diff] [blame] | 229 | if( sqlite3_vdbe_addop_trace ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 230 | sqlite3VdbePrintOp(0, i+addr, &p->aOp[i+addr]); |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 231 | } |
| 232 | #endif |
| 233 | } |
| 234 | p->nOp += nOp; |
| 235 | } |
| 236 | return addr; |
| 237 | } |
| 238 | |
| 239 | /* |
| 240 | ** Change the value of the P1 operand for a specific instruction. |
| 241 | ** This routine is useful when a large program is loaded from a |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 242 | ** static array using sqlite3VdbeAddOpList but we want to make a |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 243 | ** few minor changes to the program. |
| 244 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 245 | void sqlite3VdbeChangeP1(Vdbe *p, int addr, int val){ |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 246 | assert( p->magic==VDBE_MAGIC_INIT ); |
| 247 | if( p && addr>=0 && p->nOp>addr && p->aOp ){ |
| 248 | p->aOp[addr].p1 = val; |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | /* |
| 253 | ** Change the value of the P2 operand for a specific instruction. |
| 254 | ** This routine is useful for setting a jump destination. |
| 255 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 256 | void sqlite3VdbeChangeP2(Vdbe *p, int addr, int val){ |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 257 | assert( val>=0 ); |
| 258 | assert( p->magic==VDBE_MAGIC_INIT ); |
| 259 | if( p && addr>=0 && p->nOp>addr && p->aOp ){ |
| 260 | p->aOp[addr].p2 = val; |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | /* |
| 265 | ** Change the value of the P3 operand for a specific instruction. |
| 266 | ** This routine is useful when a large program is loaded from a |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 267 | ** static array using sqlite3VdbeAddOpList but we want to make a |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 268 | ** few minor changes to the program. |
| 269 | ** |
| 270 | ** If n>=0 then the P3 operand is dynamic, meaning that a copy of |
| 271 | ** the string is made into memory obtained from sqliteMalloc(). |
| 272 | ** A value of n==0 means copy bytes of zP3 up to and including the |
| 273 | ** first null byte. If n>0 then copy n+1 bytes of zP3. |
| 274 | ** |
| 275 | ** If n==P3_STATIC it means that zP3 is a pointer to a constant static |
| 276 | ** string and we can just copy the pointer. n==P3_POINTER means zP3 is |
| 277 | ** a pointer to some object other than a string. |
| 278 | ** |
| 279 | ** If addr<0 then change P3 on the most recently inserted instruction. |
| 280 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 281 | void sqlite3VdbeChangeP3(Vdbe *p, int addr, const char *zP3, int n){ |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 282 | Op *pOp; |
| 283 | assert( p->magic==VDBE_MAGIC_INIT ); |
| 284 | if( p==0 || p->aOp==0 ) return; |
| 285 | if( addr<0 || addr>=p->nOp ){ |
| 286 | addr = p->nOp - 1; |
| 287 | if( addr<0 ) return; |
| 288 | } |
| 289 | pOp = &p->aOp[addr]; |
| 290 | if( pOp->p3 && pOp->p3type==P3_DYNAMIC ){ |
| 291 | sqliteFree(pOp->p3); |
| 292 | pOp->p3 = 0; |
| 293 | } |
| 294 | if( zP3==0 ){ |
| 295 | pOp->p3 = 0; |
| 296 | pOp->p3type = P3_NOTUSED; |
| 297 | }else if( n<0 ){ |
| 298 | pOp->p3 = (char*)zP3; |
| 299 | pOp->p3type = n; |
| 300 | }else{ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 301 | sqlite3SetNString(&pOp->p3, zP3, n, 0); |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 302 | pOp->p3type = P3_DYNAMIC; |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | /* |
| 307 | ** If the P3 operand to the specified instruction appears |
| 308 | ** to be a quoted string token, then this procedure removes |
| 309 | ** the quotes. |
| 310 | ** |
| 311 | ** The quoting operator can be either a grave ascent (ASCII 0x27) |
| 312 | ** or a double quote character (ASCII 0x22). Two quotes in a row |
| 313 | ** resolve to be a single actual quote character within the string. |
| 314 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 315 | void sqlite3VdbeDequoteP3(Vdbe *p, int addr){ |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 316 | Op *pOp; |
| 317 | assert( p->magic==VDBE_MAGIC_INIT ); |
drh | 51e9a44 | 2004-01-16 16:42:53 +0000 | [diff] [blame] | 318 | if( p->aOp==0 ) return; |
| 319 | if( addr<0 || addr>=p->nOp ){ |
| 320 | addr = p->nOp - 1; |
| 321 | if( addr<0 ) return; |
| 322 | } |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 323 | pOp = &p->aOp[addr]; |
| 324 | if( pOp->p3==0 || pOp->p3[0]==0 ) return; |
| 325 | if( pOp->p3type==P3_POINTER ) return; |
| 326 | if( pOp->p3type!=P3_DYNAMIC ){ |
| 327 | pOp->p3 = sqliteStrDup(pOp->p3); |
| 328 | pOp->p3type = P3_DYNAMIC; |
| 329 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 330 | sqlite3Dequote(pOp->p3); |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | /* |
| 334 | ** On the P3 argument of the given instruction, change all |
| 335 | ** strings of whitespace characters into a single space and |
| 336 | ** delete leading and trailing whitespace. |
| 337 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 338 | void sqlite3VdbeCompressSpace(Vdbe *p, int addr){ |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 339 | unsigned char *z; |
| 340 | int i, j; |
| 341 | Op *pOp; |
| 342 | assert( p->magic==VDBE_MAGIC_INIT ); |
| 343 | if( p->aOp==0 || addr<0 || addr>=p->nOp ) return; |
| 344 | pOp = &p->aOp[addr]; |
| 345 | if( pOp->p3type==P3_POINTER ){ |
| 346 | return; |
| 347 | } |
| 348 | if( pOp->p3type!=P3_DYNAMIC ){ |
| 349 | pOp->p3 = sqliteStrDup(pOp->p3); |
| 350 | pOp->p3type = P3_DYNAMIC; |
| 351 | } |
| 352 | z = (unsigned char*)pOp->p3; |
| 353 | if( z==0 ) return; |
| 354 | i = j = 0; |
| 355 | while( isspace(z[i]) ){ i++; } |
| 356 | while( z[i] ){ |
| 357 | if( isspace(z[i]) ){ |
| 358 | z[j++] = ' '; |
| 359 | while( isspace(z[++i]) ){} |
| 360 | }else{ |
| 361 | z[j++] = z[i++]; |
| 362 | } |
| 363 | } |
| 364 | while( j>0 && isspace(z[j-1]) ){ j--; } |
| 365 | z[j] = 0; |
| 366 | } |
| 367 | |
| 368 | /* |
| 369 | ** Search for the current program for the given opcode and P2 |
| 370 | ** value. Return the address plus 1 if found and 0 if not found. |
| 371 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 372 | int sqlite3VdbeFindOp(Vdbe *p, int op, int p2){ |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 373 | int i; |
| 374 | assert( p->magic==VDBE_MAGIC_INIT ); |
| 375 | for(i=0; i<p->nOp; i++){ |
| 376 | if( p->aOp[i].opcode==op && p->aOp[i].p2==p2 ) return i+1; |
| 377 | } |
| 378 | return 0; |
| 379 | } |
| 380 | |
| 381 | /* |
| 382 | ** Return the opcode for a given address. |
| 383 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 384 | VdbeOp *sqlite3VdbeGetOp(Vdbe *p, int addr){ |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 385 | assert( p->magic==VDBE_MAGIC_INIT ); |
| 386 | assert( addr>=0 && addr<p->nOp ); |
| 387 | return &p->aOp[addr]; |
| 388 | } |
| 389 | |
| 390 | /* |
| 391 | ** The following group or routines are employed by installable functions |
| 392 | ** to return their results. |
| 393 | ** |
danielk1977 | 132872b | 2004-05-10 10:37:18 +0000 | [diff] [blame] | 394 | ** The sqlite3_set_result_string() routine can be used to return a string |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 395 | ** value or to return a NULL. To return a NULL, pass in NULL for zResult. |
| 396 | ** A copy is made of the string before this routine returns so it is safe |
| 397 | ** to pass in an ephemeral string. |
| 398 | ** |
danielk1977 | 132872b | 2004-05-10 10:37:18 +0000 | [diff] [blame] | 399 | ** sqlite3_set_result_error() works like sqlite3_set_result_string() except |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 400 | ** that it signals a fatal error. The string argument, if any, is the |
| 401 | ** error message. If the argument is NULL a generic substitute error message |
| 402 | ** is used. |
| 403 | ** |
danielk1977 | 132872b | 2004-05-10 10:37:18 +0000 | [diff] [blame] | 404 | ** The sqlite3_set_result_int() and sqlite3_set_result_double() set the return |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 405 | ** value of the user function to an integer or a double. |
| 406 | ** |
| 407 | ** These routines are defined here in vdbe.c because they depend on knowing |
| 408 | ** the internals of the sqlite_func structure which is only defined in |
| 409 | ** this source file. |
| 410 | */ |
danielk1977 | 132872b | 2004-05-10 10:37:18 +0000 | [diff] [blame] | 411 | char *sqlite3_set_result_string(sqlite_func *p, const char *zResult, int n){ |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 412 | assert( !p->isStep ); |
drh | 00706be | 2004-01-30 14:49:16 +0000 | [diff] [blame] | 413 | if( p->s.flags & MEM_Dyn ){ |
| 414 | sqliteFree(p->s.z); |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 415 | } |
| 416 | if( zResult==0 ){ |
drh | 00706be | 2004-01-30 14:49:16 +0000 | [diff] [blame] | 417 | p->s.flags = MEM_Null; |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 418 | n = 0; |
drh | 00706be | 2004-01-30 14:49:16 +0000 | [diff] [blame] | 419 | p->s.z = 0; |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 420 | p->s.n = 0; |
| 421 | }else{ |
| 422 | if( n<0 ) n = strlen(zResult); |
| 423 | if( n<NBFS-1 ){ |
drh | 00706be | 2004-01-30 14:49:16 +0000 | [diff] [blame] | 424 | memcpy(p->s.zShort, zResult, n); |
| 425 | p->s.zShort[n] = 0; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 426 | p->s.flags = MEM_Str | MEM_Short; |
drh | 00706be | 2004-01-30 14:49:16 +0000 | [diff] [blame] | 427 | p->s.z = p->s.zShort; |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 428 | }else{ |
drh | 00706be | 2004-01-30 14:49:16 +0000 | [diff] [blame] | 429 | p->s.z = sqliteMallocRaw( n+1 ); |
| 430 | if( p->s.z ){ |
| 431 | memcpy(p->s.z, zResult, n); |
| 432 | p->s.z[n] = 0; |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 433 | } |
drh | 00706be | 2004-01-30 14:49:16 +0000 | [diff] [blame] | 434 | p->s.flags = MEM_Str | MEM_Dyn; |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 435 | } |
| 436 | p->s.n = n+1; |
| 437 | } |
drh | 00706be | 2004-01-30 14:49:16 +0000 | [diff] [blame] | 438 | return p->s.z; |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 439 | } |
danielk1977 | 132872b | 2004-05-10 10:37:18 +0000 | [diff] [blame] | 440 | void sqlite3_set_result_int(sqlite_func *p, int iResult){ |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 441 | assert( !p->isStep ); |
drh | 00706be | 2004-01-30 14:49:16 +0000 | [diff] [blame] | 442 | if( p->s.flags & MEM_Dyn ){ |
| 443 | sqliteFree(p->s.z); |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 444 | } |
| 445 | p->s.i = iResult; |
drh | 00706be | 2004-01-30 14:49:16 +0000 | [diff] [blame] | 446 | p->s.flags = MEM_Int; |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 447 | } |
danielk1977 | 132872b | 2004-05-10 10:37:18 +0000 | [diff] [blame] | 448 | void sqlite3_set_result_double(sqlite_func *p, double rResult){ |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 449 | assert( !p->isStep ); |
drh | 00706be | 2004-01-30 14:49:16 +0000 | [diff] [blame] | 450 | if( p->s.flags & MEM_Dyn ){ |
| 451 | sqliteFree(p->s.z); |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 452 | } |
| 453 | p->s.r = rResult; |
drh | 00706be | 2004-01-30 14:49:16 +0000 | [diff] [blame] | 454 | p->s.flags = MEM_Real; |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 455 | } |
danielk1977 | 132872b | 2004-05-10 10:37:18 +0000 | [diff] [blame] | 456 | void sqlite3_set_result_error(sqlite_func *p, const char *zMsg, int n){ |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 457 | assert( !p->isStep ); |
danielk1977 | 132872b | 2004-05-10 10:37:18 +0000 | [diff] [blame] | 458 | sqlite3_set_result_string(p, zMsg, n); |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 459 | p->isError = 1; |
| 460 | } |
| 461 | |
| 462 | /* |
| 463 | ** Extract the user data from a sqlite_func structure and return a |
| 464 | ** pointer to it. |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 465 | */ |
danielk1977 | 132872b | 2004-05-10 10:37:18 +0000 | [diff] [blame] | 466 | void *sqlite3_user_data(sqlite_func *p){ |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 467 | assert( p && p->pFunc ); |
| 468 | return p->pFunc->pUserData; |
| 469 | } |
| 470 | |
| 471 | /* |
| 472 | ** Allocate or return the aggregate context for a user function. A new |
| 473 | ** context is allocated on the first call. Subsequent calls return the |
| 474 | ** same context that was returned on prior calls. |
| 475 | ** |
| 476 | ** This routine is defined here in vdbe.c because it depends on knowing |
| 477 | ** the internals of the sqlite_func structure which is only defined in |
| 478 | ** this source file. |
| 479 | */ |
danielk1977 | 132872b | 2004-05-10 10:37:18 +0000 | [diff] [blame] | 480 | void *sqlite3_aggregate_context(sqlite_func *p, int nByte){ |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 481 | assert( p && p->pFunc && p->pFunc->xStep ); |
| 482 | if( p->pAgg==0 ){ |
| 483 | if( nByte<=NBFS ){ |
drh | 00706be | 2004-01-30 14:49:16 +0000 | [diff] [blame] | 484 | p->pAgg = (void*)p->s.z; |
| 485 | memset(p->pAgg, 0, nByte); |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 486 | }else{ |
| 487 | p->pAgg = sqliteMalloc( nByte ); |
| 488 | } |
| 489 | } |
| 490 | return p->pAgg; |
| 491 | } |
| 492 | |
| 493 | /* |
| 494 | ** Return the number of times the Step function of a aggregate has been |
| 495 | ** called. |
| 496 | ** |
| 497 | ** This routine is defined here in vdbe.c because it depends on knowing |
| 498 | ** the internals of the sqlite_func structure which is only defined in |
| 499 | ** this source file. |
| 500 | */ |
danielk1977 | 132872b | 2004-05-10 10:37:18 +0000 | [diff] [blame] | 501 | int sqlite3_aggregate_count(sqlite_func *p){ |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 502 | assert( p && p->pFunc && p->pFunc->xStep ); |
| 503 | return p->cnt; |
| 504 | } |
| 505 | |
| 506 | #if !defined(NDEBUG) || defined(VDBE_PROFILE) |
| 507 | /* |
| 508 | ** Print a single opcode. This routine is used for debugging only. |
| 509 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 510 | void sqlite3VdbePrintOp(FILE *pOut, int pc, Op *pOp){ |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 511 | char *zP3; |
| 512 | char zPtr[40]; |
| 513 | if( pOp->p3type==P3_POINTER ){ |
| 514 | sprintf(zPtr, "ptr(%#x)", (int)pOp->p3); |
| 515 | zP3 = zPtr; |
| 516 | }else{ |
| 517 | zP3 = pOp->p3; |
| 518 | } |
| 519 | if( pOut==0 ) pOut = stdout; |
| 520 | fprintf(pOut,"%4d %-12s %4d %4d %s\n", |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 521 | pc, sqlite3OpcodeNames[pOp->opcode], pOp->p1, pOp->p2, zP3 ? zP3 : ""); |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 522 | fflush(pOut); |
| 523 | } |
| 524 | #endif |
| 525 | |
| 526 | /* |
| 527 | ** Give a listing of the program in the virtual machine. |
| 528 | ** |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 529 | ** The interface is the same as sqlite3VdbeExec(). But instead of |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 530 | ** running the code, it invokes the callback once for each instruction. |
| 531 | ** This feature is used to implement "EXPLAIN". |
| 532 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 533 | int sqlite3VdbeList( |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 534 | Vdbe *p /* The VDBE */ |
| 535 | ){ |
| 536 | sqlite *db = p->db; |
| 537 | int i; |
drh | 826fb5a | 2004-02-14 23:59:57 +0000 | [diff] [blame] | 538 | int rc = SQLITE_OK; |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 539 | static char *azColumnNames[] = { |
| 540 | "addr", "opcode", "p1", "p2", "p3", |
| 541 | "int", "text", "int", "int", "text", |
| 542 | 0 |
| 543 | }; |
| 544 | |
| 545 | assert( p->popStack==0 ); |
| 546 | assert( p->explain ); |
| 547 | p->azColName = azColumnNames; |
drh | 00706be | 2004-01-30 14:49:16 +0000 | [diff] [blame] | 548 | p->azResColumn = p->zArgv; |
| 549 | for(i=0; i<5; i++) p->zArgv[i] = p->aStack[i].zShort; |
drh | 826fb5a | 2004-02-14 23:59:57 +0000 | [diff] [blame] | 550 | i = p->pc; |
| 551 | if( i>=p->nOp ){ |
| 552 | p->rc = SQLITE_OK; |
| 553 | rc = SQLITE_DONE; |
| 554 | }else if( db->flags & SQLITE_Interrupt ){ |
| 555 | db->flags &= ~SQLITE_Interrupt; |
| 556 | if( db->magic!=SQLITE_MAGIC_BUSY ){ |
| 557 | p->rc = SQLITE_MISUSE; |
| 558 | }else{ |
| 559 | p->rc = SQLITE_INTERRUPT; |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 560 | } |
drh | 826fb5a | 2004-02-14 23:59:57 +0000 | [diff] [blame] | 561 | rc = SQLITE_ERROR; |
danielk1977 | 132872b | 2004-05-10 10:37:18 +0000 | [diff] [blame] | 562 | sqlite3SetString(&p->zErrMsg, sqlite3_error_string(p->rc), (char*)0); |
drh | 826fb5a | 2004-02-14 23:59:57 +0000 | [diff] [blame] | 563 | }else{ |
drh | 00706be | 2004-01-30 14:49:16 +0000 | [diff] [blame] | 564 | sprintf(p->zArgv[0],"%d",i); |
| 565 | sprintf(p->zArgv[2],"%d", p->aOp[i].p1); |
| 566 | sprintf(p->zArgv[3],"%d", p->aOp[i].p2); |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 567 | if( p->aOp[i].p3type==P3_POINTER ){ |
drh | 00706be | 2004-01-30 14:49:16 +0000 | [diff] [blame] | 568 | sprintf(p->aStack[4].zShort, "ptr(%#x)", (int)p->aOp[i].p3); |
| 569 | p->zArgv[4] = p->aStack[4].zShort; |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 570 | }else{ |
drh | 00706be | 2004-01-30 14:49:16 +0000 | [diff] [blame] | 571 | p->zArgv[4] = p->aOp[i].p3; |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 572 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 573 | p->zArgv[1] = sqlite3OpcodeNames[p->aOp[i].opcode]; |
drh | 826fb5a | 2004-02-14 23:59:57 +0000 | [diff] [blame] | 574 | p->pc = i+1; |
| 575 | p->azResColumn = p->zArgv; |
| 576 | p->nResColumn = 5; |
| 577 | p->rc = SQLITE_OK; |
| 578 | rc = SQLITE_ROW; |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 579 | } |
drh | 826fb5a | 2004-02-14 23:59:57 +0000 | [diff] [blame] | 580 | return rc; |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 581 | } |
| 582 | |
| 583 | /* |
| 584 | ** Prepare a virtual machine for execution. This involves things such |
| 585 | ** as allocating stack space and initializing the program counter. |
| 586 | ** 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] | 587 | ** calls to sqlite3VdbeExec(). |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 588 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 589 | void sqlite3VdbeMakeReady( |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 590 | Vdbe *p, /* The VDBE */ |
drh | 7c972de | 2003-09-06 22:18:07 +0000 | [diff] [blame] | 591 | int nVar, /* Number of '?' see in the SQL statement */ |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 592 | int isExplain /* True if the EXPLAIN keywords is present */ |
| 593 | ){ |
| 594 | int n; |
| 595 | |
| 596 | assert( p!=0 ); |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 597 | assert( p->magic==VDBE_MAGIC_INIT ); |
| 598 | |
| 599 | /* Add a HALT instruction to the very end of the program. |
| 600 | */ |
| 601 | 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] | 602 | sqlite3VdbeAddOp(p, OP_Halt, 0, 0); |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 603 | } |
| 604 | |
| 605 | /* No instruction ever pushes more than a single element onto the |
| 606 | ** stack. And the stack never grows on successive executions of the |
| 607 | ** same loop. So the total number of instructions is an upper bound |
| 608 | ** on the maximum stack depth required. |
| 609 | ** |
| 610 | ** Allocation all the stack space we will ever need. |
| 611 | */ |
drh | 82a4851 | 2003-09-06 22:45:20 +0000 | [diff] [blame] | 612 | if( p->aStack==0 ){ |
| 613 | p->nVar = nVar; |
| 614 | assert( nVar>=0 ); |
| 615 | n = isExplain ? 10 : p->nOp; |
| 616 | p->aStack = sqliteMalloc( |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 617 | n*(sizeof(p->aStack[0]) + 2*sizeof(char*)) /* aStack and zArgv */ |
| 618 | + p->nVar*(sizeof(char*)+sizeof(int)+1) /* azVar, anVar, abVar */ |
drh | 82a4851 | 2003-09-06 22:45:20 +0000 | [diff] [blame] | 619 | ); |
drh | 00706be | 2004-01-30 14:49:16 +0000 | [diff] [blame] | 620 | p->zArgv = (char**)&p->aStack[n]; |
| 621 | p->azColName = (char**)&p->zArgv[n]; |
drh | 82a4851 | 2003-09-06 22:45:20 +0000 | [diff] [blame] | 622 | p->azVar = (char**)&p->azColName[n]; |
| 623 | p->anVar = (int*)&p->azVar[p->nVar]; |
| 624 | p->abVar = (u8*)&p->anVar[p->nVar]; |
| 625 | } |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 626 | |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 627 | sqlite3HashInit(&p->agg.hash, SQLITE_HASH_BINARY, 0); |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 628 | p->agg.pSearch = 0; |
| 629 | #ifdef MEMORY_DEBUG |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 630 | if( sqlite3OsFileExists("vdbe_trace") ){ |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 631 | p->trace = stdout; |
| 632 | } |
| 633 | #endif |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 634 | p->pTos = &p->aStack[-1]; |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 635 | p->pc = 0; |
| 636 | p->rc = SQLITE_OK; |
| 637 | p->uniqueCnt = 0; |
| 638 | p->returnDepth = 0; |
| 639 | p->errorAction = OE_Abort; |
| 640 | p->undoTransOnError = 0; |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 641 | p->popStack = 0; |
| 642 | p->explain |= isExplain; |
| 643 | p->magic = VDBE_MAGIC_RUN; |
| 644 | #ifdef VDBE_PROFILE |
drh | cf64d8b | 2003-12-31 17:57:10 +0000 | [diff] [blame] | 645 | { |
| 646 | int i; |
| 647 | for(i=0; i<p->nOp; i++){ |
| 648 | p->aOp[i].cnt = 0; |
| 649 | p->aOp[i].cycles = 0; |
| 650 | } |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 651 | } |
| 652 | #endif |
| 653 | } |
| 654 | |
| 655 | |
| 656 | /* |
| 657 | ** Remove any elements that remain on the sorter for the VDBE given. |
| 658 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 659 | void sqlite3VdbeSorterReset(Vdbe *p){ |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 660 | while( p->pSort ){ |
| 661 | Sorter *pSorter = p->pSort; |
| 662 | p->pSort = pSorter->pNext; |
| 663 | sqliteFree(pSorter->zKey); |
| 664 | sqliteFree(pSorter->pData); |
| 665 | sqliteFree(pSorter); |
| 666 | } |
| 667 | } |
| 668 | |
| 669 | /* |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 670 | ** Reset an Agg structure. Delete all its contents. |
| 671 | ** |
| 672 | ** For installable aggregate functions, if the step function has been |
| 673 | ** called, make sure the finalizer function has also been called. The |
| 674 | ** finalizer might need to free memory that was allocated as part of its |
| 675 | ** private context. If the finalizer has not been called yet, call it |
| 676 | ** now. |
| 677 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 678 | void sqlite3VdbeAggReset(Agg *pAgg){ |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 679 | int i; |
| 680 | HashElem *p; |
| 681 | for(p = sqliteHashFirst(&pAgg->hash); p; p = sqliteHashNext(p)){ |
| 682 | AggElem *pElem = sqliteHashData(p); |
| 683 | assert( pAgg->apFunc!=0 ); |
| 684 | for(i=0; i<pAgg->nMem; i++){ |
| 685 | Mem *pMem = &pElem->aMem[i]; |
drh | 00706be | 2004-01-30 14:49:16 +0000 | [diff] [blame] | 686 | if( pAgg->apFunc[i] && (pMem->flags & MEM_AggCtx)!=0 ){ |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 687 | sqlite_func ctx; |
| 688 | ctx.pFunc = pAgg->apFunc[i]; |
drh | 00706be | 2004-01-30 14:49:16 +0000 | [diff] [blame] | 689 | ctx.s.flags = MEM_Null; |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 690 | ctx.pAgg = pMem->z; |
drh | 00706be | 2004-01-30 14:49:16 +0000 | [diff] [blame] | 691 | ctx.cnt = pMem->i; |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 692 | ctx.isStep = 0; |
| 693 | ctx.isError = 0; |
| 694 | (*pAgg->apFunc[i]->xFinalize)(&ctx); |
drh | 00706be | 2004-01-30 14:49:16 +0000 | [diff] [blame] | 695 | if( pMem->z!=0 && pMem->z!=pMem->zShort ){ |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 696 | sqliteFree(pMem->z); |
| 697 | } |
drh | 9cbe7ca | 2004-02-18 16:57:23 +0000 | [diff] [blame] | 698 | if( ctx.s.flags & MEM_Dyn ){ |
| 699 | sqliteFree(ctx.s.z); |
| 700 | } |
drh | 00706be | 2004-01-30 14:49:16 +0000 | [diff] [blame] | 701 | }else if( pMem->flags & MEM_Dyn ){ |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 702 | sqliteFree(pMem->z); |
| 703 | } |
| 704 | } |
| 705 | sqliteFree(pElem); |
| 706 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 707 | sqlite3HashClear(&pAgg->hash); |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 708 | sqliteFree(pAgg->apFunc); |
| 709 | pAgg->apFunc = 0; |
| 710 | pAgg->pCurrent = 0; |
| 711 | pAgg->pSearch = 0; |
| 712 | pAgg->nMem = 0; |
| 713 | } |
| 714 | |
| 715 | /* |
| 716 | ** Delete a keylist |
| 717 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 718 | void sqlite3VdbeKeylistFree(Keylist *p){ |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 719 | while( p ){ |
| 720 | Keylist *pNext = p->pNext; |
| 721 | sqliteFree(p); |
| 722 | p = pNext; |
| 723 | } |
| 724 | } |
| 725 | |
| 726 | /* |
| 727 | ** Close a cursor and release all the resources that cursor happens |
| 728 | ** to hold. |
| 729 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 730 | void sqlite3VdbeCleanupCursor(Cursor *pCx){ |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 731 | if( pCx->pCursor ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 732 | sqlite3BtreeCloseCursor(pCx->pCursor); |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 733 | } |
| 734 | if( pCx->pBt ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 735 | sqlite3BtreeClose(pCx->pBt); |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 736 | } |
| 737 | sqliteFree(pCx->pData); |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 738 | sqliteFree(pCx->aType); |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame^] | 739 | memset(pCx, 0, sizeof(*pCx)); |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 740 | } |
| 741 | |
| 742 | /* |
| 743 | ** Close all cursors |
| 744 | */ |
| 745 | static void closeAllCursors(Vdbe *p){ |
| 746 | int i; |
| 747 | for(i=0; i<p->nCursor; i++){ |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame^] | 748 | Cursor *pC = p->apCsr[i]; |
| 749 | sqlite3VdbeCleanupCursor(pC); |
| 750 | sqliteFree(pC); |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 751 | } |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame^] | 752 | sqliteFree(p->apCsr); |
| 753 | p->apCsr = 0; |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 754 | p->nCursor = 0; |
| 755 | } |
| 756 | |
| 757 | /* |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 758 | ** Clean up the VM after execution. |
| 759 | ** |
| 760 | ** This routine will automatically close any cursors, lists, and/or |
| 761 | ** sorters that were left open. It also deletes the values of |
| 762 | ** variables in the azVariable[] array. |
| 763 | */ |
| 764 | static void Cleanup(Vdbe *p){ |
| 765 | int i; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 766 | if( p->aStack ){ |
| 767 | Mem *pTos = p->pTos; |
| 768 | while( pTos>=p->aStack ){ |
| 769 | if( pTos->flags & MEM_Dyn ){ |
| 770 | sqliteFree(pTos->z); |
| 771 | } |
| 772 | pTos--; |
| 773 | } |
| 774 | p->pTos = pTos; |
| 775 | } |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 776 | closeAllCursors(p); |
| 777 | if( p->aMem ){ |
| 778 | for(i=0; i<p->nMem; i++){ |
drh | 00706be | 2004-01-30 14:49:16 +0000 | [diff] [blame] | 779 | if( p->aMem[i].flags & MEM_Dyn ){ |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 780 | sqliteFree(p->aMem[i].z); |
| 781 | } |
| 782 | } |
| 783 | } |
| 784 | sqliteFree(p->aMem); |
| 785 | p->aMem = 0; |
| 786 | p->nMem = 0; |
| 787 | if( p->pList ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 788 | sqlite3VdbeKeylistFree(p->pList); |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 789 | p->pList = 0; |
| 790 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 791 | sqlite3VdbeSorterReset(p); |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 792 | if( p->pFile ){ |
| 793 | if( p->pFile!=stdin ) fclose(p->pFile); |
| 794 | p->pFile = 0; |
| 795 | } |
| 796 | if( p->azField ){ |
| 797 | sqliteFree(p->azField); |
| 798 | p->azField = 0; |
| 799 | } |
| 800 | p->nField = 0; |
| 801 | if( p->zLine ){ |
| 802 | sqliteFree(p->zLine); |
| 803 | p->zLine = 0; |
| 804 | } |
| 805 | p->nLineAlloc = 0; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 806 | sqlite3VdbeAggReset(&p->agg); |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 807 | if( p->aSet ){ |
| 808 | for(i=0; i<p->nSet; i++){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 809 | sqlite3HashClear(&p->aSet[i].hash); |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 810 | } |
| 811 | } |
| 812 | sqliteFree(p->aSet); |
| 813 | p->aSet = 0; |
| 814 | p->nSet = 0; |
| 815 | if( p->keylistStack ){ |
| 816 | int ii; |
| 817 | for(ii = 0; ii < p->keylistStackDepth; ii++){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 818 | sqlite3VdbeKeylistFree(p->keylistStack[ii]); |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 819 | } |
| 820 | sqliteFree(p->keylistStack); |
| 821 | p->keylistStackDepth = 0; |
| 822 | p->keylistStack = 0; |
| 823 | } |
drh | 5f96843 | 2004-02-21 19:02:30 +0000 | [diff] [blame] | 824 | sqliteFree(p->contextStack); |
| 825 | p->contextStack = 0; |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 826 | sqliteFree(p->zErrMsg); |
| 827 | p->zErrMsg = 0; |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 828 | } |
| 829 | |
| 830 | /* |
| 831 | ** Clean up a VDBE after execution but do not delete the VDBE just yet. |
| 832 | ** Write any error messages into *pzErrMsg. Return the result code. |
| 833 | ** |
| 834 | ** After this routine is run, the VDBE should be ready to be executed |
| 835 | ** again. |
| 836 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 837 | int sqlite3VdbeReset(Vdbe *p, char **pzErrMsg){ |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 838 | sqlite *db = p->db; |
| 839 | int i; |
| 840 | |
| 841 | if( p->magic!=VDBE_MAGIC_RUN && p->magic!=VDBE_MAGIC_HALT ){ |
danielk1977 | 132872b | 2004-05-10 10:37:18 +0000 | [diff] [blame] | 842 | sqlite3SetString(pzErrMsg, sqlite3_error_string(SQLITE_MISUSE), (char*)0); |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 843 | return SQLITE_MISUSE; |
| 844 | } |
| 845 | if( p->zErrMsg ){ |
| 846 | if( pzErrMsg && *pzErrMsg==0 ){ |
| 847 | *pzErrMsg = p->zErrMsg; |
| 848 | }else{ |
| 849 | sqliteFree(p->zErrMsg); |
| 850 | } |
| 851 | p->zErrMsg = 0; |
drh | a1f9b5e | 2004-02-14 16:31:02 +0000 | [diff] [blame] | 852 | }else if( p->rc ){ |
danielk1977 | 132872b | 2004-05-10 10:37:18 +0000 | [diff] [blame] | 853 | sqlite3SetString(pzErrMsg, sqlite3_error_string(p->rc), (char*)0); |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 854 | } |
| 855 | Cleanup(p); |
| 856 | if( p->rc!=SQLITE_OK ){ |
| 857 | switch( p->errorAction ){ |
| 858 | case OE_Abort: { |
| 859 | if( !p->undoTransOnError ){ |
| 860 | for(i=0; i<db->nDb; i++){ |
| 861 | if( db->aDb[i].pBt ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 862 | sqlite3BtreeRollbackStmt(db->aDb[i].pBt); |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 863 | } |
| 864 | } |
| 865 | break; |
| 866 | } |
| 867 | /* Fall through to ROLLBACK */ |
| 868 | } |
| 869 | case OE_Rollback: { |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 870 | sqlite3RollbackAll(db); |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 871 | db->flags &= ~SQLITE_InTrans; |
| 872 | db->onError = OE_Default; |
| 873 | break; |
| 874 | } |
| 875 | default: { |
| 876 | if( p->undoTransOnError ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 877 | sqlite3RollbackAll(db); |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 878 | db->flags &= ~SQLITE_InTrans; |
| 879 | db->onError = OE_Default; |
| 880 | } |
| 881 | break; |
| 882 | } |
| 883 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 884 | sqlite3RollbackInternalChanges(db); |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 885 | } |
| 886 | for(i=0; i<db->nDb; i++){ |
| 887 | if( db->aDb[i].pBt && db->aDb[i].inTrans==2 ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 888 | sqlite3BtreeCommitStmt(db->aDb[i].pBt); |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 889 | db->aDb[i].inTrans = 1; |
| 890 | } |
| 891 | } |
danielk1977 | 132872b | 2004-05-10 10:37:18 +0000 | [diff] [blame] | 892 | assert( p->pTos<&p->aStack[p->pc] || sqlite3_malloc_failed==1 ); |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 893 | #ifdef VDBE_PROFILE |
| 894 | { |
| 895 | FILE *out = fopen("vdbe_profile.out", "a"); |
| 896 | if( out ){ |
| 897 | int i; |
| 898 | fprintf(out, "---- "); |
| 899 | for(i=0; i<p->nOp; i++){ |
| 900 | fprintf(out, "%02x", p->aOp[i].opcode); |
| 901 | } |
| 902 | fprintf(out, "\n"); |
| 903 | for(i=0; i<p->nOp; i++){ |
| 904 | fprintf(out, "%6d %10lld %8lld ", |
| 905 | p->aOp[i].cnt, |
| 906 | p->aOp[i].cycles, |
| 907 | p->aOp[i].cnt>0 ? p->aOp[i].cycles/p->aOp[i].cnt : 0 |
| 908 | ); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 909 | sqlite3VdbePrintOp(out, i, &p->aOp[i]); |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 910 | } |
| 911 | fclose(out); |
| 912 | } |
| 913 | } |
| 914 | #endif |
| 915 | p->magic = VDBE_MAGIC_INIT; |
| 916 | return p->rc; |
| 917 | } |
| 918 | |
| 919 | /* |
| 920 | ** Clean up and delete a VDBE after execution. Return an integer which is |
| 921 | ** the result code. Write any error message text into *pzErrMsg. |
| 922 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 923 | int sqlite3VdbeFinalize(Vdbe *p, char **pzErrMsg){ |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 924 | int rc; |
| 925 | sqlite *db; |
| 926 | |
| 927 | if( p->magic!=VDBE_MAGIC_RUN && p->magic!=VDBE_MAGIC_HALT ){ |
danielk1977 | 132872b | 2004-05-10 10:37:18 +0000 | [diff] [blame] | 928 | sqlite3SetString(pzErrMsg, sqlite3_error_string(SQLITE_MISUSE), (char*)0); |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 929 | return SQLITE_MISUSE; |
| 930 | } |
| 931 | db = p->db; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 932 | rc = sqlite3VdbeReset(p, pzErrMsg); |
| 933 | sqlite3VdbeDelete(p); |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 934 | if( db->want_to_close && db->pVdbe==0 ){ |
danielk1977 | 132872b | 2004-05-10 10:37:18 +0000 | [diff] [blame] | 935 | sqlite3_close(db); |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 936 | } |
drh | a1f9b5e | 2004-02-14 16:31:02 +0000 | [diff] [blame] | 937 | if( rc==SQLITE_SCHEMA ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 938 | sqlite3ResetInternalSchema(db, 0); |
drh | a1f9b5e | 2004-02-14 16:31:02 +0000 | [diff] [blame] | 939 | } |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 940 | return rc; |
| 941 | } |
| 942 | |
| 943 | /* |
| 944 | ** Set the values of all variables. Variable $1 in the original SQL will |
| 945 | ** be the string azValue[0]. $2 will have the value azValue[1]. And |
| 946 | ** so forth. If a value is out of range (for example $3 when nValue==2) |
| 947 | ** then its value will be NULL. |
| 948 | ** |
| 949 | ** This routine overrides any prior call. |
| 950 | */ |
danielk1977 | 132872b | 2004-05-10 10:37:18 +0000 | [diff] [blame] | 951 | int sqlite3_bind(sqlite_vm *pVm, int i, const char *zVal, int len, int copy){ |
drh | 7c972de | 2003-09-06 22:18:07 +0000 | [diff] [blame] | 952 | Vdbe *p = (Vdbe*)pVm; |
| 953 | if( p->magic!=VDBE_MAGIC_RUN || p->pc!=0 ){ |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 954 | return SQLITE_MISUSE; |
| 955 | } |
drh | 7c972de | 2003-09-06 22:18:07 +0000 | [diff] [blame] | 956 | if( i<1 || i>p->nVar ){ |
| 957 | return SQLITE_RANGE; |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 958 | } |
drh | 7c972de | 2003-09-06 22:18:07 +0000 | [diff] [blame] | 959 | i--; |
| 960 | if( p->abVar[i] ){ |
| 961 | sqliteFree(p->azVar[i]); |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 962 | } |
drh | 7c972de | 2003-09-06 22:18:07 +0000 | [diff] [blame] | 963 | if( zVal==0 ){ |
| 964 | copy = 0; |
| 965 | len = 0; |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 966 | } |
drh | 7c972de | 2003-09-06 22:18:07 +0000 | [diff] [blame] | 967 | if( len<0 ){ |
| 968 | len = strlen(zVal)+1; |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 969 | } |
drh | 7c972de | 2003-09-06 22:18:07 +0000 | [diff] [blame] | 970 | if( copy ){ |
| 971 | p->azVar[i] = sqliteMalloc( len ); |
| 972 | if( p->azVar[i] ) memcpy(p->azVar[i], zVal, len); |
| 973 | }else{ |
drh | 82a4851 | 2003-09-06 22:45:20 +0000 | [diff] [blame] | 974 | p->azVar[i] = (char*)zVal; |
drh | 7c972de | 2003-09-06 22:18:07 +0000 | [diff] [blame] | 975 | } |
| 976 | p->abVar[i] = copy; |
| 977 | p->anVar[i] = len; |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 978 | return SQLITE_OK; |
| 979 | } |
| 980 | |
| 981 | |
| 982 | /* |
| 983 | ** Delete an entire VDBE. |
| 984 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 985 | void sqlite3VdbeDelete(Vdbe *p){ |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 986 | int i; |
| 987 | if( p==0 ) return; |
| 988 | Cleanup(p); |
| 989 | if( p->pPrev ){ |
| 990 | p->pPrev->pNext = p->pNext; |
| 991 | }else{ |
| 992 | assert( p->db->pVdbe==p ); |
| 993 | p->db->pVdbe = p->pNext; |
| 994 | } |
| 995 | if( p->pNext ){ |
| 996 | p->pNext->pPrev = p->pPrev; |
| 997 | } |
| 998 | p->pPrev = p->pNext = 0; |
| 999 | if( p->nOpAlloc==0 ){ |
| 1000 | p->aOp = 0; |
| 1001 | p->nOp = 0; |
| 1002 | } |
| 1003 | for(i=0; i<p->nOp; i++){ |
| 1004 | if( p->aOp[i].p3type==P3_DYNAMIC ){ |
| 1005 | sqliteFree(p->aOp[i].p3); |
| 1006 | } |
| 1007 | } |
drh | 7c972de | 2003-09-06 22:18:07 +0000 | [diff] [blame] | 1008 | for(i=0; i<p->nVar; i++){ |
| 1009 | if( p->abVar[i] ) sqliteFree(p->azVar[i]); |
| 1010 | } |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 1011 | sqliteFree(p->aOp); |
| 1012 | sqliteFree(p->aLabel); |
| 1013 | sqliteFree(p->aStack); |
| 1014 | p->magic = VDBE_MAGIC_DEAD; |
| 1015 | sqliteFree(p); |
| 1016 | } |
drh | a11846b | 2004-01-07 18:52:56 +0000 | [diff] [blame] | 1017 | |
| 1018 | /* |
| 1019 | ** Convert an integer in between the native integer format and |
| 1020 | ** the bigEndian format used as the record number for tables. |
| 1021 | ** |
| 1022 | ** The bigEndian format (most significant byte first) is used for |
| 1023 | ** record numbers so that records will sort into the correct order |
| 1024 | ** even though memcmp() is used to compare the keys. On machines |
| 1025 | ** whose native integer format is little endian (ex: i486) the |
| 1026 | ** order of bytes is reversed. On native big-endian machines |
| 1027 | ** (ex: Alpha, Sparc, Motorola) the byte order is the same. |
| 1028 | ** |
| 1029 | ** This function is its own inverse. In other words |
| 1030 | ** |
| 1031 | ** X == byteSwap(byteSwap(X)) |
| 1032 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1033 | int sqlite3VdbeByteSwap(int x){ |
drh | a11846b | 2004-01-07 18:52:56 +0000 | [diff] [blame] | 1034 | union { |
| 1035 | char zBuf[sizeof(int)]; |
| 1036 | int i; |
| 1037 | } ux; |
| 1038 | ux.zBuf[3] = x&0xff; |
| 1039 | ux.zBuf[2] = (x>>8)&0xff; |
| 1040 | ux.zBuf[1] = (x>>16)&0xff; |
| 1041 | ux.zBuf[0] = (x>>24)&0xff; |
| 1042 | return ux.i; |
| 1043 | } |
| 1044 | |
| 1045 | /* |
| 1046 | ** If a MoveTo operation is pending on the given cursor, then do that |
| 1047 | ** MoveTo now. Return an error code. If no MoveTo is pending, this |
| 1048 | ** routine does nothing and returns SQLITE_OK. |
| 1049 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1050 | int sqlite3VdbeCursorMoveto(Cursor *p){ |
drh | a11846b | 2004-01-07 18:52:56 +0000 | [diff] [blame] | 1051 | if( p->deferredMoveto ){ |
| 1052 | int res; |
danielk1977 | 132872b | 2004-05-10 10:37:18 +0000 | [diff] [blame] | 1053 | extern int sqlite3_search_count; |
drh | a3b321d | 2004-05-11 09:31:31 +0000 | [diff] [blame] | 1054 | assert( p->intKey ); |
danielk1977 | 6490beb | 2004-05-11 06:17:21 +0000 | [diff] [blame] | 1055 | if( p->intKey ){ |
| 1056 | sqlite3BtreeMoveto(p->pCursor, 0, p->movetoTarget, &res); |
| 1057 | }else{ |
| 1058 | sqlite3BtreeMoveto(p->pCursor,(char*)&p->movetoTarget,sizeof(i64),&res); |
| 1059 | } |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 1060 | p->incrKey = 0; |
drh | a11846b | 2004-01-07 18:52:56 +0000 | [diff] [blame] | 1061 | p->lastRecno = keyToInt(p->movetoTarget); |
| 1062 | p->recnoIsValid = res==0; |
| 1063 | if( res<0 ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1064 | sqlite3BtreeNext(p->pCursor, &res); |
drh | a11846b | 2004-01-07 18:52:56 +0000 | [diff] [blame] | 1065 | } |
danielk1977 | 132872b | 2004-05-10 10:37:18 +0000 | [diff] [blame] | 1066 | sqlite3_search_count++; |
drh | a11846b | 2004-01-07 18:52:56 +0000 | [diff] [blame] | 1067 | p->deferredMoveto = 0; |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 1068 | p->cacheValid = 0; |
drh | a11846b | 2004-01-07 18:52:56 +0000 | [diff] [blame] | 1069 | } |
| 1070 | return SQLITE_OK; |
| 1071 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1072 | |
danielk1977 | 189621d | 2004-05-09 23:23:56 +0000 | [diff] [blame] | 1073 | /* |
| 1074 | ** FIX ME |
| 1075 | ** |
| 1076 | ** This function is included temporarily so that regression tests have |
| 1077 | ** a chance of passing. It always uses memcmp(). |
| 1078 | */ |
| 1079 | int sqlite2BtreeKeyCompare( |
| 1080 | BtCursor *pCur, /* Pointer to entry to compare against */ |
| 1081 | const void *pKey, /* Key to compare against entry that pCur points to */ |
| 1082 | int nKey, /* Number of bytes in pKey */ |
| 1083 | int nIgnore, /* Ignore this many bytes at the end of pCur */ |
| 1084 | int *pResult /* Write the result here */ |
| 1085 | ){ |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 1086 | const void *pCellKey; |
| 1087 | void *pMallocedKey; |
danielk1977 | 189621d | 2004-05-09 23:23:56 +0000 | [diff] [blame] | 1088 | u64 nCellKey; |
| 1089 | int rc; |
| 1090 | |
| 1091 | sqlite3BtreeKeySize(pCur, &nCellKey); |
| 1092 | nCellKey = nCellKey - nIgnore; |
| 1093 | if( nCellKey<=0 ){ |
| 1094 | *pResult = 0; |
| 1095 | return SQLITE_OK; |
| 1096 | } |
| 1097 | |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 1098 | pCellKey = sqlite3BtreeKeyFetch(pCur, nCellKey); |
danielk1977 | 189621d | 2004-05-09 23:23:56 +0000 | [diff] [blame] | 1099 | if( pCellKey ){ |
| 1100 | *pResult = memcmp(pCellKey, pKey, nKey>nCellKey?nCellKey:nKey); |
| 1101 | return SQLITE_OK; |
| 1102 | } |
| 1103 | |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 1104 | pMallocedKey = sqliteMalloc( nCellKey ); |
| 1105 | if( pMallocedKey==0 ) return SQLITE_NOMEM; |
danielk1977 | 189621d | 2004-05-09 23:23:56 +0000 | [diff] [blame] | 1106 | |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 1107 | rc = sqlite3BtreeKey(pCur, 0, nCellKey, pMallocedKey); |
| 1108 | *pResult = memcmp(pMallocedKey, pKey, nKey>nCellKey?nCellKey:nKey); |
| 1109 | sqliteFree(pMallocedKey); |
danielk1977 | 189621d | 2004-05-09 23:23:56 +0000 | [diff] [blame] | 1110 | |
| 1111 | return rc; |
| 1112 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1113 | |
drh | ab9f7f1 | 2004-05-08 10:56:11 +0000 | [diff] [blame] | 1114 | /* |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1115 | ** The following functions: |
danielk1977 | 90e4d95 | 2004-05-10 10:05:53 +0000 | [diff] [blame] | 1116 | ** |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1117 | ** sqlite3VdbeSerialType() |
| 1118 | ** sqlite3VdbeSerialTypeLen() |
| 1119 | ** sqlite3VdbeSerialRead() |
danielk1977 | 90e4d95 | 2004-05-10 10:05:53 +0000 | [diff] [blame] | 1120 | ** sqlite3VdbeSerialLen() |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1121 | ** sqlite3VdbeSerialWrite() |
danielk1977 | 90e4d95 | 2004-05-10 10:05:53 +0000 | [diff] [blame] | 1122 | ** |
| 1123 | ** encapsulate the code that serializes values for storage in SQLite |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1124 | ** data and index records. Each serialized value consists of a |
| 1125 | ** 'serial-type' and a blob of data. The serial type is an 8-byte unsigned |
| 1126 | ** integer, stored as a varint. |
danielk1977 | 90e4d95 | 2004-05-10 10:05:53 +0000 | [diff] [blame] | 1127 | ** |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1128 | ** In an SQLite index record, the serial type is stored directly before |
| 1129 | ** the blob of data that it corresponds to. In a table record, all serial |
| 1130 | ** types are stored at the start of the record, and the blobs of data at |
| 1131 | ** the end. Hence these functions allow the caller to handle the |
| 1132 | ** serial-type and data blob seperately. |
| 1133 | ** |
| 1134 | ** The following table describes the various storage classes for data: |
| 1135 | ** |
| 1136 | ** serial type bytes of data type |
danielk1977 | 90e4d95 | 2004-05-10 10:05:53 +0000 | [diff] [blame] | 1137 | ** -------------- --------------- --------------- |
danielk1977 | 183f9f7 | 2004-05-13 05:20:26 +0000 | [diff] [blame] | 1138 | ** 0 - Not a type. |
danielk1977 | 90e4d95 | 2004-05-10 10:05:53 +0000 | [diff] [blame] | 1139 | ** 1 1 signed integer |
| 1140 | ** 2 2 signed integer |
| 1141 | ** 3 4 signed integer |
| 1142 | ** 4 8 signed integer |
| 1143 | ** 5 8 IEEE float |
danielk1977 | 183f9f7 | 2004-05-13 05:20:26 +0000 | [diff] [blame] | 1144 | ** 6 0 NULL |
| 1145 | ** 7..11 reserved for expansion |
danielk1977 | 90e4d95 | 2004-05-10 10:05:53 +0000 | [diff] [blame] | 1146 | ** N>=12 and even (N-12)/2 BLOB |
| 1147 | ** N>=13 and odd (N-13)/2 text |
| 1148 | ** |
| 1149 | */ |
| 1150 | |
| 1151 | /* |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1152 | ** Return the serial-type for the value stored in pMem. |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 1153 | */ |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1154 | u64 sqlite3VdbeSerialType(const Mem *pMem){ |
| 1155 | int flags = pMem->flags; |
| 1156 | |
| 1157 | if( flags&MEM_Null ){ |
danielk1977 | 183f9f7 | 2004-05-13 05:20:26 +0000 | [diff] [blame] | 1158 | return 6; |
danielk1977 | 90e4d95 | 2004-05-10 10:05:53 +0000 | [diff] [blame] | 1159 | } |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1160 | if( flags&MEM_Int ){ |
| 1161 | /* Figure out whether to use 1, 2, 4 or 8 bytes. */ |
| 1162 | i64 i = pMem->i; |
| 1163 | if( i>=-127 && i<=127 ) return 1; |
| 1164 | if( i>=-32767 && i<=32767 ) return 2; |
| 1165 | if( i>=-2147483647 && i<=2147483647 ) return 3; |
| 1166 | return 4; |
danielk1977 | 90e4d95 | 2004-05-10 10:05:53 +0000 | [diff] [blame] | 1167 | } |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1168 | if( flags&MEM_Real ){ |
| 1169 | return 5; |
danielk1977 | 90e4d95 | 2004-05-10 10:05:53 +0000 | [diff] [blame] | 1170 | } |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1171 | if( flags&MEM_Str ){ |
| 1172 | return (pMem->n*2 + 13); |
danielk1977 | 90e4d95 | 2004-05-10 10:05:53 +0000 | [diff] [blame] | 1173 | } |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1174 | if( flags&MEM_Blob ){ |
| 1175 | return (pMem->n*2 + 12); |
| 1176 | } |
| 1177 | return 0; |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 1178 | } |
| 1179 | |
| 1180 | /* |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1181 | ** Return the length of the data corresponding to the supplied serial-type. |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 1182 | */ |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1183 | int sqlite3VdbeSerialTypeLen(u64 serial_type){ |
danielk1977 | 183f9f7 | 2004-05-13 05:20:26 +0000 | [diff] [blame] | 1184 | assert( serial_type!=0 ); |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1185 | switch(serial_type){ |
danielk1977 | 183f9f7 | 2004-05-13 05:20:26 +0000 | [diff] [blame] | 1186 | case 6: return 0; /* NULL */ |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1187 | case 1: return 1; /* 1 byte integer */ |
| 1188 | case 2: return 2; /* 2 byte integer */ |
| 1189 | case 3: return 4; /* 4 byte integer */ |
| 1190 | case 4: return 8; /* 8 byte integer */ |
| 1191 | case 5: return 8; /* 8 byte float */ |
danielk1977 | 90e4d95 | 2004-05-10 10:05:53 +0000 | [diff] [blame] | 1192 | } |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1193 | assert( serial_type>=12 ); |
| 1194 | return ((serial_type-12)>>1); /* text or blob */ |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 1195 | } |
| 1196 | |
| 1197 | /* |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1198 | ** Write the serialized data blob for the value stored in pMem into |
| 1199 | ** buf. It is assumed that the caller has allocated sufficient space. |
| 1200 | ** Return the number of bytes written. |
| 1201 | */ |
| 1202 | int sqlite3VdbeSerialPut(unsigned char *buf, const Mem *pMem){ |
| 1203 | u64 serial_type = sqlite3VdbeSerialType(pMem); |
| 1204 | int len; |
danielk1977 | 183f9f7 | 2004-05-13 05:20:26 +0000 | [diff] [blame] | 1205 | |
| 1206 | assert( serial_type!=0 ); |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1207 | |
| 1208 | /* NULL */ |
danielk1977 | 183f9f7 | 2004-05-13 05:20:26 +0000 | [diff] [blame] | 1209 | if( serial_type==6 ){ |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1210 | return 0; |
| 1211 | } |
| 1212 | |
| 1213 | /* Integer */ |
| 1214 | if( serial_type<5 ){ |
| 1215 | i64 i = pMem->i; |
| 1216 | len = sqlite3VdbeSerialTypeLen(serial_type); |
| 1217 | while( len-- ){ |
| 1218 | buf[len] = (i&0xFF); |
| 1219 | i = i >> 8; |
| 1220 | } |
| 1221 | return sqlite3VdbeSerialTypeLen(serial_type); |
| 1222 | } |
| 1223 | |
| 1224 | /* Float */ |
| 1225 | if( serial_type==5 ){ |
| 1226 | /* TODO: byte ordering? */ |
| 1227 | assert( sizeof(double)==8 ); |
| 1228 | memcpy(buf, &pMem->r, 8); |
| 1229 | return 8; |
| 1230 | } |
| 1231 | |
| 1232 | /* String or blob */ |
| 1233 | assert( serial_type>=12 ); |
| 1234 | len = sqlite3VdbeSerialTypeLen(serial_type); |
| 1235 | memcpy(buf, pMem->z, len); |
| 1236 | return len; |
| 1237 | } |
| 1238 | |
| 1239 | /* |
| 1240 | ** Deserialize the data blob pointed to by buf as serial type serial_type |
| 1241 | ** and store the result in pMem. Return the number of bytes read. |
| 1242 | */ |
| 1243 | int sqlite3VdbeSerialGet(const unsigned char *buf, u64 serial_type, Mem *pMem){ |
danielk1977 | 90e4d95 | 2004-05-10 10:05:53 +0000 | [diff] [blame] | 1244 | int len; |
| 1245 | |
danielk1977 | 183f9f7 | 2004-05-13 05:20:26 +0000 | [diff] [blame] | 1246 | assert( serial_type!=0 ); |
| 1247 | |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1248 | /* memset(pMem, 0, sizeof(pMem)); */ |
| 1249 | pMem->flags = 0; |
| 1250 | pMem->z = 0; |
danielk1977 | 90e4d95 | 2004-05-10 10:05:53 +0000 | [diff] [blame] | 1251 | |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1252 | /* NULL */ |
danielk1977 | 183f9f7 | 2004-05-13 05:20:26 +0000 | [diff] [blame] | 1253 | if( serial_type==6 ){ |
danielk1977 | 90e4d95 | 2004-05-10 10:05:53 +0000 | [diff] [blame] | 1254 | pMem->flags = MEM_Null; |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1255 | return 0; |
danielk1977 | 90e4d95 | 2004-05-10 10:05:53 +0000 | [diff] [blame] | 1256 | } |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1257 | |
| 1258 | /* Integer */ |
| 1259 | if( serial_type<5 ){ |
| 1260 | i64 i = 0; |
| 1261 | int n; |
| 1262 | len = sqlite3VdbeSerialTypeLen(serial_type); |
danielk1977 | 90e4d95 | 2004-05-10 10:05:53 +0000 | [diff] [blame] | 1263 | |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1264 | if( buf[0]&0x80 ){ |
| 1265 | for(n=0; n<(8-len); n++){ |
| 1266 | i = (i<<8)+0xFF; |
| 1267 | } |
| 1268 | } |
| 1269 | for(n=0; n<len; n++){ |
| 1270 | i = i << 8; |
| 1271 | i = i + buf[n]; |
| 1272 | } |
danielk1977 | 90e4d95 | 2004-05-10 10:05:53 +0000 | [diff] [blame] | 1273 | pMem->flags = MEM_Int; |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1274 | pMem->i = i; |
| 1275 | return sqlite3VdbeSerialTypeLen(serial_type); |
danielk1977 | 90e4d95 | 2004-05-10 10:05:53 +0000 | [diff] [blame] | 1276 | } |
| 1277 | |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1278 | /* Float */ |
| 1279 | if( serial_type==5 ){ |
| 1280 | /* TODO: byte ordering? */ |
| 1281 | assert( sizeof(double)==8 ); |
| 1282 | memcpy(&pMem->r, buf, 8); |
| 1283 | pMem->flags = MEM_Real; |
| 1284 | return 8; |
danielk1977 | 90e4d95 | 2004-05-10 10:05:53 +0000 | [diff] [blame] | 1285 | } |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1286 | |
| 1287 | /* String or blob */ |
| 1288 | assert( serial_type>=12 ); |
| 1289 | if( serial_type&0x01 ){ |
| 1290 | pMem->flags = MEM_Str; |
| 1291 | }else{ |
| 1292 | pMem->flags = MEM_Blob; |
| 1293 | } |
| 1294 | len = sqlite3VdbeSerialTypeLen(serial_type); |
| 1295 | pMem->n = len; |
danielk1977 | 90e4d95 | 2004-05-10 10:05:53 +0000 | [diff] [blame] | 1296 | if( len>NBFS ){ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 1297 | pMem->z = sqliteMallocRaw( len ); |
danielk1977 | 90e4d95 | 2004-05-10 10:05:53 +0000 | [diff] [blame] | 1298 | if( !pMem->z ){ |
| 1299 | return -1; |
| 1300 | } |
| 1301 | pMem->flags |= MEM_Dyn; |
| 1302 | }else{ |
| 1303 | pMem->z = pMem->zShort; |
| 1304 | pMem->flags |= MEM_Short; |
| 1305 | } |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1306 | memcpy(pMem->z, buf, len); |
danielk1977 | 90e4d95 | 2004-05-10 10:05:53 +0000 | [diff] [blame] | 1307 | |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1308 | return len; |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 1309 | } |
| 1310 | |
| 1311 | /* |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 1312 | ** Compare the values contained by the two memory cells, returning |
| 1313 | ** negative, zero or positive if pMem1 is less than, equal to, or greater |
| 1314 | ** than pMem2. Sorting order is NULL's first, followed by numbers (integers |
| 1315 | ** and reals) sorted numerically, followed by text ordered by memcmp() and |
| 1316 | ** finally blob's ordered by memcmp(). |
| 1317 | ** |
| 1318 | ** Two NULL values are considered equal by this function. |
| 1319 | */ |
| 1320 | int compareMemCells(Mem *pMem1, Mem *pMem2){ |
| 1321 | int rc; |
| 1322 | int combined_flags = pMem1->flags|pMem2->flags; |
| 1323 | |
| 1324 | /* If one value is NULL, it is less than the other. If both values |
| 1325 | ** are NULL, return 0. |
| 1326 | */ |
| 1327 | if( combined_flags&MEM_Null ){ |
| 1328 | return (pMem2->flags&MEM_Null) - (pMem1->flags&MEM_Null); |
| 1329 | } |
| 1330 | |
| 1331 | /* If one value is a number and the other is not, the number is less. |
| 1332 | ** If both are numbers, compare as reals if one is a real, or as integers |
| 1333 | ** if both values are integers. |
| 1334 | */ |
| 1335 | if( combined_flags&(MEM_Int|MEM_Real) ){ |
| 1336 | if( !(pMem1->flags&(MEM_Int|MEM_Real)) ){ |
| 1337 | return 1; |
| 1338 | } |
| 1339 | if( !(pMem2->flags&(MEM_Int|MEM_Real)) ){ |
| 1340 | return -1; |
| 1341 | } |
| 1342 | |
| 1343 | if( combined_flags&MEM_Real ){ |
| 1344 | if( pMem1->flags&MEM_Int ){ |
| 1345 | pMem1->r = pMem1->i; |
| 1346 | } |
| 1347 | if( pMem2->flags&MEM_Int ){ |
| 1348 | pMem2->r = pMem2->i; |
| 1349 | } |
| 1350 | if( pMem1->r < pMem2->r ) return -1; |
| 1351 | if( pMem1->r > pMem2->r ) return 1; |
| 1352 | return 0; |
| 1353 | } |
| 1354 | |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 1355 | return (pMem1->i - pMem2->i); |
| 1356 | } |
| 1357 | |
| 1358 | rc = (pMem2->flags&MEM_Null) - (pMem1->flags&MEM_Null); |
| 1359 | if( rc ){ |
| 1360 | return rc; |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 1361 | } |
| 1362 | |
| 1363 | /* Both values must be strings or blobs. If only one is a string, then |
| 1364 | ** that value is less. Otherwise, compare with memcmp(). If memcmp() |
| 1365 | ** returns 0 and one value is longer than the other, then that value |
| 1366 | ** is greater. |
| 1367 | */ |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 1368 | rc = memcmp(pMem1->z, pMem2->z, (pMem1->n>pMem2->n)?pMem2->n:pMem1->n); |
| 1369 | if( rc ){ |
| 1370 | return rc; |
| 1371 | } |
| 1372 | |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 1373 | return (pMem1->n - pMem2->n); |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 1374 | } |
| 1375 | |
| 1376 | /* |
drh | ab9f7f1 | 2004-05-08 10:56:11 +0000 | [diff] [blame] | 1377 | ** The following is the comparison function for (non-integer) |
| 1378 | ** keys in the btrees. This function returns negative, zero, or |
| 1379 | ** positive if the first key is less than, equal to, or greater than |
| 1380 | ** the second. |
| 1381 | ** |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 1382 | ** This function assumes that each key consists of one or more type/blob |
danielk1977 | 183f9f7 | 2004-05-13 05:20:26 +0000 | [diff] [blame] | 1383 | ** pairs, encoded using the sqlite3VdbeSerialXXX() functions above. |
| 1384 | ** |
| 1385 | ** Following the type/blob pairs, each key may have a single 0x00 byte |
| 1386 | ** followed by a varint. A key may only have this traling 0x00/varint |
| 1387 | ** pair if it has at least as many type/blob pairs as the key it is being |
| 1388 | ** compared to. |
drh | ab9f7f1 | 2004-05-08 10:56:11 +0000 | [diff] [blame] | 1389 | */ |
drh | ab9f7f1 | 2004-05-08 10:56:11 +0000 | [diff] [blame] | 1390 | int sqlite3VdbeKeyCompare( |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 1391 | void *userData, |
danielk1977 | 183f9f7 | 2004-05-13 05:20:26 +0000 | [diff] [blame] | 1392 | int nKey1, const void *pKey1, |
| 1393 | int nKey2, const void *pKey2 |
drh | ab9f7f1 | 2004-05-08 10:56:11 +0000 | [diff] [blame] | 1394 | ){ |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 1395 | Cursor *pC = (Cursor *)userData; |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 1396 | int offset1 = 0; |
| 1397 | int offset2 = 0; |
danielk1977 | 183f9f7 | 2004-05-13 05:20:26 +0000 | [diff] [blame] | 1398 | const unsigned char *aKey1 = (const unsigned char *)pKey1; |
| 1399 | const unsigned char *aKey2 = (const unsigned char *)pKey2; |
| 1400 | |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 1401 | while( offset1<nKey1 && offset2<nKey2 ){ |
| 1402 | Mem mem1; |
| 1403 | Mem mem2; |
| 1404 | u64 serial_type1; |
| 1405 | u64 serial_type2; |
| 1406 | int rc; |
| 1407 | |
danielk1977 | 183f9f7 | 2004-05-13 05:20:26 +0000 | [diff] [blame] | 1408 | /* Read the serial types for the next element in each key. */ |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 1409 | offset1 += sqlite3GetVarint(&aKey1[offset1], &serial_type1); |
| 1410 | offset2 += sqlite3GetVarint(&aKey2[offset2], &serial_type2); |
danielk1977 | 183f9f7 | 2004-05-13 05:20:26 +0000 | [diff] [blame] | 1411 | |
| 1412 | /* If either of the varints just read in are 0 (not a type), then |
| 1413 | ** this is the end of the keys. The remaining data in each key is |
| 1414 | ** the varint rowid. Compare these as signed integers and return |
| 1415 | ** the result. |
| 1416 | */ |
| 1417 | if( !serial_type1 || !serial_type2 ){ |
| 1418 | assert( !serial_type1 && !serial_type2 ); |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 1419 | assert( !pC || !pC->incrKey ); |
danielk1977 | 183f9f7 | 2004-05-13 05:20:26 +0000 | [diff] [blame] | 1420 | sqlite3GetVarint(&aKey1[offset1], &serial_type1); |
| 1421 | sqlite3GetVarint(&aKey2[offset2], &serial_type2); |
| 1422 | return ( (i64)serial_type1 - (i64)serial_type2 ); |
| 1423 | } |
| 1424 | |
| 1425 | /* Assert that there is enough space left in each key for the blob of |
| 1426 | ** data to go with the serial type just read. This assert may fail if |
| 1427 | ** the file is corrupted. Then read the value from each key into mem1 |
| 1428 | ** and mem2 respectively. |
| 1429 | */ |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 1430 | offset1 += sqlite3VdbeSerialGet(&aKey1[offset1], serial_type1, &mem1); |
| 1431 | offset2 += sqlite3VdbeSerialGet(&aKey2[offset2], serial_type2, &mem2); |
| 1432 | |
| 1433 | rc = compareMemCells(&mem1, &mem2); |
| 1434 | if( mem1.flags&MEM_Dyn ){ |
| 1435 | sqliteFree(mem1.z); |
| 1436 | } |
| 1437 | if( mem2.flags&MEM_Dyn ){ |
| 1438 | sqliteFree(mem2.z); |
| 1439 | } |
| 1440 | if( rc!=0 ){ |
| 1441 | return rc; |
| 1442 | } |
| 1443 | } |
| 1444 | |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 1445 | /* One of the keys ran out of fields, but all the fields up to that point |
| 1446 | ** were equal. If the incrKey flag is true, then the second key is |
| 1447 | ** treated as larger. |
| 1448 | */ |
| 1449 | if( pC && pC->incrKey ){ |
| 1450 | assert( offset2==nKey2 ); |
| 1451 | return -1; |
| 1452 | } |
| 1453 | |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 1454 | if( offset1<nKey1 ){ |
| 1455 | return 1; |
| 1456 | } |
| 1457 | if( offset2<nKey2 ){ |
| 1458 | return -1; |
drh | ab9f7f1 | 2004-05-08 10:56:11 +0000 | [diff] [blame] | 1459 | } |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 1460 | |
| 1461 | return_result: |
| 1462 | |
drh | ab9f7f1 | 2004-05-08 10:56:11 +0000 | [diff] [blame] | 1463 | return 0; |
drh | ab9f7f1 | 2004-05-08 10:56:11 +0000 | [diff] [blame] | 1464 | } |
danielk1977 | 183f9f7 | 2004-05-13 05:20:26 +0000 | [diff] [blame] | 1465 | |
| 1466 | /* |
| 1467 | ** pCur points at an index entry. Read the rowid (varint occuring at |
| 1468 | ** the end of the entry and store it in *rowid. Return SQLITE_OK if |
| 1469 | ** everything works, or an error code otherwise. |
| 1470 | */ |
| 1471 | int sqlite3VdbeIdxRowid(BtCursor *pCur, i64 *rowid){ |
| 1472 | i64 sz; |
| 1473 | int rc; |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 1474 | char buf[10]; |
danielk1977 | 183f9f7 | 2004-05-13 05:20:26 +0000 | [diff] [blame] | 1475 | int len; |
| 1476 | u64 r; |
| 1477 | |
| 1478 | rc = sqlite3BtreeKeySize(pCur, &sz); |
| 1479 | if( rc!=SQLITE_OK ){ |
| 1480 | return rc; |
| 1481 | } |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 1482 | len = ((sz>10)?10:sz); |
| 1483 | |
| 1484 | /* If there are less than 2 bytes in the key, this cannot be |
| 1485 | ** a valid index entry. In practice this comes up for a query |
| 1486 | ** of the sort "SELECT max(x) FROM t1;" when t1 is an empty table |
| 1487 | ** with an index on x. In this case just call the rowid 0. |
| 1488 | */ |
| 1489 | if( len<2 ){ |
| 1490 | *rowid = 0; |
| 1491 | return SQLITE_OK; |
| 1492 | } |
danielk1977 | 183f9f7 | 2004-05-13 05:20:26 +0000 | [diff] [blame] | 1493 | |
| 1494 | rc = sqlite3BtreeKey(pCur, sz-len, len, buf); |
| 1495 | if( rc!=SQLITE_OK ){ |
| 1496 | return rc; |
| 1497 | } |
| 1498 | |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 1499 | len--; |
| 1500 | while( buf[len-1] && --len ); |
danielk1977 | 183f9f7 | 2004-05-13 05:20:26 +0000 | [diff] [blame] | 1501 | |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 1502 | sqlite3GetVarint(&buf[len], &r); |
danielk1977 | 183f9f7 | 2004-05-13 05:20:26 +0000 | [diff] [blame] | 1503 | *rowid = r; |
| 1504 | return SQLITE_OK; |
| 1505 | } |
| 1506 | |
| 1507 | int sqlite3VdbeIdxKeyCompare( |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 1508 | Cursor *pC, |
danielk1977 | 183f9f7 | 2004-05-13 05:20:26 +0000 | [diff] [blame] | 1509 | int nKey, const unsigned char *pKey, |
| 1510 | int ignorerowid, |
| 1511 | int *res |
| 1512 | ){ |
| 1513 | unsigned char *pCellKey; |
| 1514 | u64 nCellKey; |
| 1515 | int freeCellKey = 0; |
| 1516 | int rc; |
| 1517 | int len; |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 1518 | BtCursor *pCur = pC->pCursor; |
danielk1977 | 183f9f7 | 2004-05-13 05:20:26 +0000 | [diff] [blame] | 1519 | |
| 1520 | sqlite3BtreeKeySize(pCur, &nCellKey); |
| 1521 | if( nCellKey<=0 ){ |
| 1522 | *res = 0; |
| 1523 | return SQLITE_OK; |
| 1524 | } |
| 1525 | |
| 1526 | pCellKey = (unsigned char *)sqlite3BtreeKeyFetch(pCur, nCellKey); |
| 1527 | if( !pCellKey ){ |
drh | 10617cd | 2004-05-14 15:27:27 +0000 | [diff] [blame] | 1528 | pCellKey = (unsigned char *)sqliteMallocRaw(nCellKey); |
danielk1977 | 183f9f7 | 2004-05-13 05:20:26 +0000 | [diff] [blame] | 1529 | if( !pCellKey ){ |
| 1530 | return SQLITE_NOMEM; |
| 1531 | } |
| 1532 | freeCellKey = 1; |
| 1533 | rc = sqlite3BtreeKey(pCur, 0, nCellKey, pCellKey); |
| 1534 | if( rc!=SQLITE_OK ){ |
| 1535 | sqliteFree(pCellKey); |
| 1536 | return rc; |
| 1537 | } |
| 1538 | } |
| 1539 | |
| 1540 | len = nCellKey-2; |
| 1541 | while( pCellKey[len] && --len ); |
| 1542 | |
| 1543 | if( ignorerowid ){ |
| 1544 | nKey--; |
| 1545 | while( pKey[nKey] && --nKey ); |
| 1546 | } |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 1547 | *res = sqlite3VdbeKeyCompare(pC, len, pCellKey, nKey, pKey); |
danielk1977 | 183f9f7 | 2004-05-13 05:20:26 +0000 | [diff] [blame] | 1548 | |
| 1549 | if( freeCellKey ){ |
| 1550 | sqliteFree(pCellKey); |
| 1551 | } |
| 1552 | return SQLITE_OK; |
| 1553 | } |