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