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 | /* |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 610 | ** If pOp is an OP_HexBlob opcode, then transform it to an OP_Blob |
| 611 | ** opcode. |
| 612 | */ |
| 613 | static int translateOp(Op *pOp){ |
| 614 | if( pOp->opcode==OP_HexBlob ){ |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 615 | pOp->p1 = strlen(pOp->p3)/2; |
danielk1977 | 3fd0a73 | 2004-05-27 13:35:19 +0000 | [diff] [blame] | 616 | if( pOp->p1 ){ |
| 617 | char *zBlob = sqlite3HexToBlob(pOp->p3); |
| 618 | if( !zBlob ) return SQLITE_NOMEM; |
| 619 | if( pOp->p3type==P3_DYNAMIC ){ |
| 620 | sqliteFree(pOp->p3); |
| 621 | } |
| 622 | pOp->p3 = zBlob; |
| 623 | pOp->p3type = P3_DYNAMIC; |
| 624 | }else{ |
| 625 | pOp->p3type = P3_STATIC; |
| 626 | pOp->p3 = ""; |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 627 | } |
danielk1977 | 3fd0a73 | 2004-05-27 13:35:19 +0000 | [diff] [blame] | 628 | pOp->opcode = OP_Blob; |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 629 | } |
danielk1977 | 3fd0a73 | 2004-05-27 13:35:19 +0000 | [diff] [blame] | 630 | return SQLITE_OK; |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 631 | } |
| 632 | |
| 633 | /* |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 634 | ** Prepare a virtual machine for execution. This involves things such |
| 635 | ** as allocating stack space and initializing the program counter. |
| 636 | ** 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] | 637 | ** calls to sqlite3VdbeExec(). |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 638 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 639 | void sqlite3VdbeMakeReady( |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 640 | Vdbe *p, /* The VDBE */ |
drh | 7c972de | 2003-09-06 22:18:07 +0000 | [diff] [blame] | 641 | int nVar, /* Number of '?' see in the SQL statement */ |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 642 | int isExplain /* True if the EXPLAIN keywords is present */ |
| 643 | ){ |
| 644 | int n; |
| 645 | |
| 646 | assert( p!=0 ); |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 647 | assert( p->magic==VDBE_MAGIC_INIT ); |
| 648 | |
| 649 | /* Add a HALT instruction to the very end of the program. |
| 650 | */ |
| 651 | 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] | 652 | sqlite3VdbeAddOp(p, OP_Halt, 0, 0); |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 653 | } |
| 654 | |
| 655 | /* No instruction ever pushes more than a single element onto the |
| 656 | ** stack. And the stack never grows on successive executions of the |
| 657 | ** same loop. So the total number of instructions is an upper bound |
| 658 | ** on the maximum stack depth required. |
| 659 | ** |
| 660 | ** Allocation all the stack space we will ever need. |
| 661 | */ |
drh | 82a4851 | 2003-09-06 22:45:20 +0000 | [diff] [blame] | 662 | if( p->aStack==0 ){ |
| 663 | p->nVar = nVar; |
| 664 | assert( nVar>=0 ); |
| 665 | n = isExplain ? 10 : p->nOp; |
| 666 | p->aStack = sqliteMalloc( |
danielk1977 | 6ddcca5 | 2004-05-24 23:48:25 +0000 | [diff] [blame] | 667 | n*(sizeof(p->aStack[0])+sizeof(Mem*)+sizeof(char*)) /* aStack, apArg */ |
drh | 5a12e68 | 2004-05-19 11:24:25 +0000 | [diff] [blame] | 668 | + p->nVar*sizeof(Mem) /* apVar */ |
drh | 82a4851 | 2003-09-06 22:45:20 +0000 | [diff] [blame] | 669 | ); |
danielk1977 | 6ddcca5 | 2004-05-24 23:48:25 +0000 | [diff] [blame] | 670 | p->apArg = (Mem **)&p->aStack[n]; |
| 671 | p->azColName = (char**)&p->apArg[n]; |
drh | 5a12e68 | 2004-05-19 11:24:25 +0000 | [diff] [blame] | 672 | p->apVar = (Mem *)&p->azColName[n]; |
danielk1977 | 54db47e | 2004-05-19 10:36:43 +0000 | [diff] [blame] | 673 | for(n=0; n<p->nVar; n++){ |
drh | 5a12e68 | 2004-05-19 11:24:25 +0000 | [diff] [blame] | 674 | p->apVar[n].flags = MEM_Null; |
danielk1977 | 54db47e | 2004-05-19 10:36:43 +0000 | [diff] [blame] | 675 | } |
drh | 82a4851 | 2003-09-06 22:45:20 +0000 | [diff] [blame] | 676 | } |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 677 | |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 678 | sqlite3HashInit(&p->agg.hash, SQLITE_HASH_BINARY, 0); |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 679 | p->agg.pSearch = 0; |
| 680 | #ifdef MEMORY_DEBUG |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 681 | if( sqlite3OsFileExists("vdbe_trace") ){ |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 682 | p->trace = stdout; |
| 683 | } |
| 684 | #endif |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 685 | p->pTos = &p->aStack[-1]; |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 686 | p->pc = 0; |
| 687 | p->rc = SQLITE_OK; |
| 688 | p->uniqueCnt = 0; |
| 689 | p->returnDepth = 0; |
| 690 | p->errorAction = OE_Abort; |
| 691 | p->undoTransOnError = 0; |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 692 | p->popStack = 0; |
| 693 | p->explain |= isExplain; |
| 694 | p->magic = VDBE_MAGIC_RUN; |
| 695 | #ifdef VDBE_PROFILE |
drh | cf64d8b | 2003-12-31 17:57:10 +0000 | [diff] [blame] | 696 | { |
| 697 | int i; |
| 698 | for(i=0; i<p->nOp; i++){ |
| 699 | p->aOp[i].cnt = 0; |
| 700 | p->aOp[i].cycles = 0; |
| 701 | } |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 702 | } |
| 703 | #endif |
danielk1977 | 3fd0a73 | 2004-05-27 13:35:19 +0000 | [diff] [blame] | 704 | if( !isExplain ){ |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 705 | int i; |
| 706 | for(i=0; i<p->nOp; i++){ |
| 707 | translateOp(&p->aOp[i]); |
| 708 | } |
| 709 | } |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 710 | } |
| 711 | |
| 712 | |
| 713 | /* |
| 714 | ** Remove any elements that remain on the sorter for the VDBE given. |
| 715 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 716 | void sqlite3VdbeSorterReset(Vdbe *p){ |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 717 | while( p->pSort ){ |
| 718 | Sorter *pSorter = p->pSort; |
| 719 | p->pSort = pSorter->pNext; |
| 720 | sqliteFree(pSorter->zKey); |
| 721 | sqliteFree(pSorter->pData); |
| 722 | sqliteFree(pSorter); |
| 723 | } |
| 724 | } |
| 725 | |
| 726 | /* |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 727 | ** Reset an Agg structure. Delete all its contents. |
| 728 | ** |
| 729 | ** For installable aggregate functions, if the step function has been |
| 730 | ** called, make sure the finalizer function has also been called. The |
| 731 | ** finalizer might need to free memory that was allocated as part of its |
| 732 | ** private context. If the finalizer has not been called yet, call it |
| 733 | ** now. |
| 734 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 735 | void sqlite3VdbeAggReset(Agg *pAgg){ |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 736 | int i; |
| 737 | HashElem *p; |
| 738 | for(p = sqliteHashFirst(&pAgg->hash); p; p = sqliteHashNext(p)){ |
| 739 | AggElem *pElem = sqliteHashData(p); |
| 740 | assert( pAgg->apFunc!=0 ); |
| 741 | for(i=0; i<pAgg->nMem; i++){ |
| 742 | Mem *pMem = &pElem->aMem[i]; |
drh | 00706be | 2004-01-30 14:49:16 +0000 | [diff] [blame] | 743 | if( pAgg->apFunc[i] && (pMem->flags & MEM_AggCtx)!=0 ){ |
danielk1977 | 0ae8b83 | 2004-05-25 12:05:56 +0000 | [diff] [blame] | 744 | sqlite3_context ctx; |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 745 | ctx.pFunc = pAgg->apFunc[i]; |
drh | 00706be | 2004-01-30 14:49:16 +0000 | [diff] [blame] | 746 | ctx.s.flags = MEM_Null; |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 747 | ctx.pAgg = pMem->z; |
drh | 00706be | 2004-01-30 14:49:16 +0000 | [diff] [blame] | 748 | ctx.cnt = pMem->i; |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 749 | ctx.isStep = 0; |
| 750 | ctx.isError = 0; |
| 751 | (*pAgg->apFunc[i]->xFinalize)(&ctx); |
drh | 00706be | 2004-01-30 14:49:16 +0000 | [diff] [blame] | 752 | if( pMem->z!=0 && pMem->z!=pMem->zShort ){ |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 753 | sqliteFree(pMem->z); |
| 754 | } |
drh | 9cbe7ca | 2004-02-18 16:57:23 +0000 | [diff] [blame] | 755 | if( ctx.s.flags & MEM_Dyn ){ |
| 756 | sqliteFree(ctx.s.z); |
| 757 | } |
drh | 00706be | 2004-01-30 14:49:16 +0000 | [diff] [blame] | 758 | }else if( pMem->flags & MEM_Dyn ){ |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 759 | sqliteFree(pMem->z); |
| 760 | } |
| 761 | } |
| 762 | sqliteFree(pElem); |
| 763 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 764 | sqlite3HashClear(&pAgg->hash); |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 765 | sqliteFree(pAgg->apFunc); |
| 766 | pAgg->apFunc = 0; |
| 767 | pAgg->pCurrent = 0; |
| 768 | pAgg->pSearch = 0; |
| 769 | pAgg->nMem = 0; |
| 770 | } |
| 771 | |
| 772 | /* |
| 773 | ** Delete a keylist |
| 774 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 775 | void sqlite3VdbeKeylistFree(Keylist *p){ |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 776 | while( p ){ |
| 777 | Keylist *pNext = p->pNext; |
| 778 | sqliteFree(p); |
| 779 | p = pNext; |
| 780 | } |
| 781 | } |
| 782 | |
| 783 | /* |
| 784 | ** Close a cursor and release all the resources that cursor happens |
| 785 | ** to hold. |
| 786 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 787 | void sqlite3VdbeCleanupCursor(Cursor *pCx){ |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 788 | if( pCx->pCursor ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 789 | sqlite3BtreeCloseCursor(pCx->pCursor); |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 790 | } |
| 791 | if( pCx->pBt ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 792 | sqlite3BtreeClose(pCx->pBt); |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 793 | } |
| 794 | sqliteFree(pCx->pData); |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 795 | sqliteFree(pCx->aType); |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame] | 796 | memset(pCx, 0, sizeof(*pCx)); |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 797 | } |
| 798 | |
| 799 | /* |
| 800 | ** Close all cursors |
| 801 | */ |
| 802 | static void closeAllCursors(Vdbe *p){ |
| 803 | int i; |
| 804 | for(i=0; i<p->nCursor; i++){ |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame] | 805 | Cursor *pC = p->apCsr[i]; |
| 806 | sqlite3VdbeCleanupCursor(pC); |
| 807 | sqliteFree(pC); |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 808 | } |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame] | 809 | sqliteFree(p->apCsr); |
| 810 | p->apCsr = 0; |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 811 | p->nCursor = 0; |
| 812 | } |
| 813 | |
| 814 | /* |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 815 | ** Clean up the VM after execution. |
| 816 | ** |
| 817 | ** This routine will automatically close any cursors, lists, and/or |
| 818 | ** sorters that were left open. It also deletes the values of |
drh | 5a12e68 | 2004-05-19 11:24:25 +0000 | [diff] [blame] | 819 | ** variables in the aVar[] array. |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 820 | */ |
| 821 | static void Cleanup(Vdbe *p){ |
| 822 | int i; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 823 | if( p->aStack ){ |
| 824 | Mem *pTos = p->pTos; |
| 825 | while( pTos>=p->aStack ){ |
| 826 | if( pTos->flags & MEM_Dyn ){ |
| 827 | sqliteFree(pTos->z); |
| 828 | } |
| 829 | pTos--; |
| 830 | } |
| 831 | p->pTos = pTos; |
| 832 | } |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 833 | closeAllCursors(p); |
| 834 | if( p->aMem ){ |
| 835 | for(i=0; i<p->nMem; i++){ |
drh | 00706be | 2004-01-30 14:49:16 +0000 | [diff] [blame] | 836 | if( p->aMem[i].flags & MEM_Dyn ){ |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 837 | sqliteFree(p->aMem[i].z); |
| 838 | } |
| 839 | } |
| 840 | } |
| 841 | sqliteFree(p->aMem); |
| 842 | p->aMem = 0; |
| 843 | p->nMem = 0; |
| 844 | if( p->pList ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 845 | sqlite3VdbeKeylistFree(p->pList); |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 846 | p->pList = 0; |
| 847 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 848 | sqlite3VdbeSorterReset(p); |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 849 | if( p->pFile ){ |
| 850 | if( p->pFile!=stdin ) fclose(p->pFile); |
| 851 | p->pFile = 0; |
| 852 | } |
| 853 | if( p->azField ){ |
| 854 | sqliteFree(p->azField); |
| 855 | p->azField = 0; |
| 856 | } |
| 857 | p->nField = 0; |
| 858 | if( p->zLine ){ |
| 859 | sqliteFree(p->zLine); |
| 860 | p->zLine = 0; |
| 861 | } |
| 862 | p->nLineAlloc = 0; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 863 | sqlite3VdbeAggReset(&p->agg); |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 864 | if( p->keylistStack ){ |
| 865 | int ii; |
| 866 | for(ii = 0; ii < p->keylistStackDepth; ii++){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 867 | sqlite3VdbeKeylistFree(p->keylistStack[ii]); |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 868 | } |
| 869 | sqliteFree(p->keylistStack); |
| 870 | p->keylistStackDepth = 0; |
| 871 | p->keylistStack = 0; |
| 872 | } |
drh | 5f96843 | 2004-02-21 19:02:30 +0000 | [diff] [blame] | 873 | sqliteFree(p->contextStack); |
| 874 | p->contextStack = 0; |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 875 | sqliteFree(p->zErrMsg); |
| 876 | p->zErrMsg = 0; |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 877 | } |
| 878 | |
| 879 | /* |
danielk1977 | 22322fd | 2004-05-25 23:35:17 +0000 | [diff] [blame] | 880 | ** Set the number of result columns that will be returned by this SQL |
| 881 | ** statement. This is now set at compile time, rather than during |
| 882 | ** execution of the vdbe program so that sqlite3_column_count() can |
| 883 | ** be called on an SQL statement before sqlite3_step(). |
| 884 | */ |
| 885 | void sqlite3VdbeSetNumCols(Vdbe *p, int nResColumn){ |
danielk1977 | 3cf8606 | 2004-05-26 10:11:05 +0000 | [diff] [blame] | 886 | assert( 0==p->nResColumn ); |
danielk1977 | 22322fd | 2004-05-25 23:35:17 +0000 | [diff] [blame] | 887 | p->nResColumn = nResColumn; |
| 888 | } |
| 889 | |
| 890 | /* |
danielk1977 | 3cf8606 | 2004-05-26 10:11:05 +0000 | [diff] [blame] | 891 | ** Set the name of the idx'th column to be returned by the SQL statement. |
| 892 | ** zName must be a pointer to a nul terminated string. |
| 893 | ** |
| 894 | ** This call must be made after a call to sqlite3VdbeSetNumCols(). |
| 895 | ** |
| 896 | ** Parameter N may be either P3_DYNAMIC or P3_STATIC. |
| 897 | */ |
| 898 | int sqlite3VdbeSetColName(Vdbe *p, int idx, const char *zName, int N){ |
| 899 | int rc; |
| 900 | Mem *pColName; |
| 901 | assert( idx<p->nResColumn ); |
| 902 | |
| 903 | /* If the Vdbe.aColName array has not yet been allocated, allocate |
| 904 | ** it now. |
| 905 | */ |
| 906 | if( !p->aColName ){ |
| 907 | int i; |
| 908 | p->aColName = (Mem *)sqliteMalloc(sizeof(Mem)*p->nResColumn); |
| 909 | if( !p->aColName ){ |
| 910 | return SQLITE_NOMEM; |
| 911 | } |
| 912 | for(i=0; i<p->nResColumn; i++){ |
| 913 | p->aColName[i].flags = MEM_Null; |
| 914 | } |
| 915 | } |
| 916 | |
| 917 | pColName = &(p->aColName[idx]); |
| 918 | if( N==0 ){ |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 919 | rc = sqlite3VdbeMemSetStr(pColName, zName, -1, TEXT_Utf8, 1); |
danielk1977 | 3cf8606 | 2004-05-26 10:11:05 +0000 | [diff] [blame] | 920 | }else{ |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 921 | rc = sqlite3VdbeMemSetStr(pColName, zName, N, TEXT_Utf8, N>0); |
danielk1977 | 3cf8606 | 2004-05-26 10:11:05 +0000 | [diff] [blame] | 922 | } |
| 923 | if( rc==SQLITE_OK && N==P3_DYNAMIC ){ |
| 924 | pColName->flags = (pColName->flags&(~MEM_Static))|MEM_Dyn; |
| 925 | } |
| 926 | return rc; |
| 927 | } |
| 928 | |
| 929 | /* |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 930 | ** Clean up a VDBE after execution but do not delete the VDBE just yet. |
| 931 | ** Write any error messages into *pzErrMsg. Return the result code. |
| 932 | ** |
| 933 | ** After this routine is run, the VDBE should be ready to be executed |
| 934 | ** again. |
| 935 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 936 | int sqlite3VdbeReset(Vdbe *p, char **pzErrMsg){ |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 937 | sqlite *db = p->db; |
| 938 | int i; |
| 939 | |
| 940 | if( p->magic!=VDBE_MAGIC_RUN && p->magic!=VDBE_MAGIC_HALT ){ |
danielk1977 | 132872b | 2004-05-10 10:37:18 +0000 | [diff] [blame] | 941 | sqlite3SetString(pzErrMsg, sqlite3_error_string(SQLITE_MISUSE), (char*)0); |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 942 | sqlite3Error(p->db, SQLITE_MISUSE, sqlite3_error_string(SQLITE_MISUSE),0); |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 943 | return SQLITE_MISUSE; |
| 944 | } |
| 945 | if( p->zErrMsg ){ |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 946 | sqlite3Error(p->db, p->rc, "%s", p->zErrMsg, 0); |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 947 | if( pzErrMsg && *pzErrMsg==0 ){ |
| 948 | *pzErrMsg = p->zErrMsg; |
| 949 | }else{ |
| 950 | sqliteFree(p->zErrMsg); |
| 951 | } |
| 952 | p->zErrMsg = 0; |
drh | a1f9b5e | 2004-02-14 16:31:02 +0000 | [diff] [blame] | 953 | }else if( p->rc ){ |
danielk1977 | 132872b | 2004-05-10 10:37:18 +0000 | [diff] [blame] | 954 | sqlite3SetString(pzErrMsg, sqlite3_error_string(p->rc), (char*)0); |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 955 | sqlite3Error(p->db, p->rc, "%s", sqlite3_error_string(p->rc) , 0); |
| 956 | }else{ |
| 957 | sqlite3Error(p->db, SQLITE_OK, 0); |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 958 | } |
| 959 | Cleanup(p); |
| 960 | if( p->rc!=SQLITE_OK ){ |
| 961 | switch( p->errorAction ){ |
| 962 | case OE_Abort: { |
| 963 | if( !p->undoTransOnError ){ |
| 964 | for(i=0; i<db->nDb; i++){ |
| 965 | if( db->aDb[i].pBt ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 966 | sqlite3BtreeRollbackStmt(db->aDb[i].pBt); |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 967 | } |
| 968 | } |
| 969 | break; |
| 970 | } |
| 971 | /* Fall through to ROLLBACK */ |
| 972 | } |
| 973 | case OE_Rollback: { |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 974 | sqlite3RollbackAll(db); |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 975 | db->flags &= ~SQLITE_InTrans; |
| 976 | db->onError = OE_Default; |
| 977 | break; |
| 978 | } |
| 979 | default: { |
| 980 | if( p->undoTransOnError ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 981 | sqlite3RollbackAll(db); |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 982 | db->flags &= ~SQLITE_InTrans; |
| 983 | db->onError = OE_Default; |
| 984 | } |
| 985 | break; |
| 986 | } |
| 987 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 988 | sqlite3RollbackInternalChanges(db); |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 989 | } |
| 990 | for(i=0; i<db->nDb; i++){ |
| 991 | if( db->aDb[i].pBt && db->aDb[i].inTrans==2 ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 992 | sqlite3BtreeCommitStmt(db->aDb[i].pBt); |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 993 | db->aDb[i].inTrans = 1; |
| 994 | } |
| 995 | } |
danielk1977 | 132872b | 2004-05-10 10:37:18 +0000 | [diff] [blame] | 996 | assert( p->pTos<&p->aStack[p->pc] || sqlite3_malloc_failed==1 ); |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 997 | #ifdef VDBE_PROFILE |
| 998 | { |
| 999 | FILE *out = fopen("vdbe_profile.out", "a"); |
| 1000 | if( out ){ |
| 1001 | int i; |
| 1002 | fprintf(out, "---- "); |
| 1003 | for(i=0; i<p->nOp; i++){ |
| 1004 | fprintf(out, "%02x", p->aOp[i].opcode); |
| 1005 | } |
| 1006 | fprintf(out, "\n"); |
| 1007 | for(i=0; i<p->nOp; i++){ |
| 1008 | fprintf(out, "%6d %10lld %8lld ", |
| 1009 | p->aOp[i].cnt, |
| 1010 | p->aOp[i].cycles, |
| 1011 | p->aOp[i].cnt>0 ? p->aOp[i].cycles/p->aOp[i].cnt : 0 |
| 1012 | ); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1013 | sqlite3VdbePrintOp(out, i, &p->aOp[i]); |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 1014 | } |
| 1015 | fclose(out); |
| 1016 | } |
| 1017 | } |
| 1018 | #endif |
| 1019 | p->magic = VDBE_MAGIC_INIT; |
| 1020 | return p->rc; |
| 1021 | } |
| 1022 | |
| 1023 | /* |
| 1024 | ** Clean up and delete a VDBE after execution. Return an integer which is |
| 1025 | ** the result code. Write any error message text into *pzErrMsg. |
| 1026 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1027 | int sqlite3VdbeFinalize(Vdbe *p, char **pzErrMsg){ |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 1028 | int rc; |
| 1029 | sqlite *db; |
| 1030 | |
| 1031 | if( p->magic!=VDBE_MAGIC_RUN && p->magic!=VDBE_MAGIC_HALT ){ |
danielk1977 | 132872b | 2004-05-10 10:37:18 +0000 | [diff] [blame] | 1032 | sqlite3SetString(pzErrMsg, sqlite3_error_string(SQLITE_MISUSE), (char*)0); |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 1033 | if( p->magic==VDBE_MAGIC_INIT ){ |
| 1034 | sqlite3Error(p->db, SQLITE_MISUSE, sqlite3_error_string(SQLITE_MISUSE),0); |
| 1035 | } |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 1036 | return SQLITE_MISUSE; |
| 1037 | } |
| 1038 | db = p->db; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1039 | rc = sqlite3VdbeReset(p, pzErrMsg); |
| 1040 | sqlite3VdbeDelete(p); |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 1041 | if( db->want_to_close && db->pVdbe==0 ){ |
danielk1977 | 132872b | 2004-05-10 10:37:18 +0000 | [diff] [blame] | 1042 | sqlite3_close(db); |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 1043 | } |
drh | a1f9b5e | 2004-02-14 16:31:02 +0000 | [diff] [blame] | 1044 | if( rc==SQLITE_SCHEMA ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1045 | sqlite3ResetInternalSchema(db, 0); |
drh | a1f9b5e | 2004-02-14 16:31:02 +0000 | [diff] [blame] | 1046 | } |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 1047 | return rc; |
| 1048 | } |
| 1049 | |
| 1050 | /* |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 1051 | ** Delete an entire VDBE. |
| 1052 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1053 | void sqlite3VdbeDelete(Vdbe *p){ |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 1054 | int i; |
| 1055 | if( p==0 ) return; |
| 1056 | Cleanup(p); |
| 1057 | if( p->pPrev ){ |
| 1058 | p->pPrev->pNext = p->pNext; |
| 1059 | }else{ |
| 1060 | assert( p->db->pVdbe==p ); |
| 1061 | p->db->pVdbe = p->pNext; |
| 1062 | } |
| 1063 | if( p->pNext ){ |
| 1064 | p->pNext->pPrev = p->pPrev; |
| 1065 | } |
| 1066 | p->pPrev = p->pNext = 0; |
| 1067 | if( p->nOpAlloc==0 ){ |
| 1068 | p->aOp = 0; |
| 1069 | p->nOp = 0; |
| 1070 | } |
| 1071 | for(i=0; i<p->nOp; i++){ |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 1072 | Op *pOp = &p->aOp[i]; |
| 1073 | if( pOp->p3type==P3_DYNAMIC || pOp->p3type==P3_KEYINFO ){ |
| 1074 | sqliteFree(pOp->p3); |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 1075 | } |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 1076 | #ifndef NDEBUG |
| 1077 | sqliteFree(pOp->zComment); |
| 1078 | #endif |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 1079 | } |
drh | 7c972de | 2003-09-06 22:18:07 +0000 | [diff] [blame] | 1080 | for(i=0; i<p->nVar; i++){ |
drh | 5a12e68 | 2004-05-19 11:24:25 +0000 | [diff] [blame] | 1081 | if( p->apVar[i].flags&MEM_Dyn ){ |
| 1082 | sqliteFree(p->apVar[i].z); |
danielk1977 | 54db47e | 2004-05-19 10:36:43 +0000 | [diff] [blame] | 1083 | } |
drh | 7c972de | 2003-09-06 22:18:07 +0000 | [diff] [blame] | 1084 | } |
danielk1977 | ca6b291 | 2004-05-21 10:49:47 +0000 | [diff] [blame] | 1085 | if( p->azColName16 ){ |
| 1086 | for(i=0; i<p->nResColumn; i++){ |
| 1087 | if( p->azColName16[i] ) sqliteFree(p->azColName16[i]); |
| 1088 | } |
| 1089 | sqliteFree(p->azColName16); |
| 1090 | } |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 1091 | sqliteFree(p->aOp); |
| 1092 | sqliteFree(p->aLabel); |
| 1093 | sqliteFree(p->aStack); |
| 1094 | p->magic = VDBE_MAGIC_DEAD; |
| 1095 | sqliteFree(p); |
| 1096 | } |
drh | a11846b | 2004-01-07 18:52:56 +0000 | [diff] [blame] | 1097 | |
| 1098 | /* |
drh | a11846b | 2004-01-07 18:52:56 +0000 | [diff] [blame] | 1099 | ** If a MoveTo operation is pending on the given cursor, then do that |
| 1100 | ** MoveTo now. Return an error code. If no MoveTo is pending, this |
| 1101 | ** routine does nothing and returns SQLITE_OK. |
| 1102 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1103 | int sqlite3VdbeCursorMoveto(Cursor *p){ |
drh | a11846b | 2004-01-07 18:52:56 +0000 | [diff] [blame] | 1104 | if( p->deferredMoveto ){ |
| 1105 | int res; |
danielk1977 | 132872b | 2004-05-10 10:37:18 +0000 | [diff] [blame] | 1106 | extern int sqlite3_search_count; |
drh | a3b321d | 2004-05-11 09:31:31 +0000 | [diff] [blame] | 1107 | assert( p->intKey ); |
danielk1977 | 6490beb | 2004-05-11 06:17:21 +0000 | [diff] [blame] | 1108 | if( p->intKey ){ |
| 1109 | sqlite3BtreeMoveto(p->pCursor, 0, p->movetoTarget, &res); |
| 1110 | }else{ |
| 1111 | sqlite3BtreeMoveto(p->pCursor,(char*)&p->movetoTarget,sizeof(i64),&res); |
| 1112 | } |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 1113 | *p->pIncrKey = 0; |
drh | a11846b | 2004-01-07 18:52:56 +0000 | [diff] [blame] | 1114 | p->lastRecno = keyToInt(p->movetoTarget); |
| 1115 | p->recnoIsValid = res==0; |
| 1116 | if( res<0 ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1117 | sqlite3BtreeNext(p->pCursor, &res); |
drh | a11846b | 2004-01-07 18:52:56 +0000 | [diff] [blame] | 1118 | } |
danielk1977 | 132872b | 2004-05-10 10:37:18 +0000 | [diff] [blame] | 1119 | sqlite3_search_count++; |
drh | a11846b | 2004-01-07 18:52:56 +0000 | [diff] [blame] | 1120 | p->deferredMoveto = 0; |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 1121 | p->cacheValid = 0; |
drh | a11846b | 2004-01-07 18:52:56 +0000 | [diff] [blame] | 1122 | } |
| 1123 | return SQLITE_OK; |
| 1124 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1125 | |
drh | ab9f7f1 | 2004-05-08 10:56:11 +0000 | [diff] [blame] | 1126 | /* |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1127 | ** The following functions: |
danielk1977 | 90e4d95 | 2004-05-10 10:05:53 +0000 | [diff] [blame] | 1128 | ** |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1129 | ** sqlite3VdbeSerialType() |
| 1130 | ** sqlite3VdbeSerialTypeLen() |
| 1131 | ** sqlite3VdbeSerialRead() |
danielk1977 | 90e4d95 | 2004-05-10 10:05:53 +0000 | [diff] [blame] | 1132 | ** sqlite3VdbeSerialLen() |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1133 | ** sqlite3VdbeSerialWrite() |
danielk1977 | 90e4d95 | 2004-05-10 10:05:53 +0000 | [diff] [blame] | 1134 | ** |
| 1135 | ** encapsulate the code that serializes values for storage in SQLite |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1136 | ** data and index records. Each serialized value consists of a |
| 1137 | ** 'serial-type' and a blob of data. The serial type is an 8-byte unsigned |
| 1138 | ** integer, stored as a varint. |
danielk1977 | 90e4d95 | 2004-05-10 10:05:53 +0000 | [diff] [blame] | 1139 | ** |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1140 | ** In an SQLite index record, the serial type is stored directly before |
| 1141 | ** the blob of data that it corresponds to. In a table record, all serial |
| 1142 | ** types are stored at the start of the record, and the blobs of data at |
| 1143 | ** the end. Hence these functions allow the caller to handle the |
| 1144 | ** serial-type and data blob seperately. |
| 1145 | ** |
| 1146 | ** The following table describes the various storage classes for data: |
| 1147 | ** |
| 1148 | ** serial type bytes of data type |
danielk1977 | 90e4d95 | 2004-05-10 10:05:53 +0000 | [diff] [blame] | 1149 | ** -------------- --------------- --------------- |
danielk1977 | 183f9f7 | 2004-05-13 05:20:26 +0000 | [diff] [blame] | 1150 | ** 0 - Not a type. |
danielk1977 | 90e4d95 | 2004-05-10 10:05:53 +0000 | [diff] [blame] | 1151 | ** 1 1 signed integer |
| 1152 | ** 2 2 signed integer |
| 1153 | ** 3 4 signed integer |
| 1154 | ** 4 8 signed integer |
| 1155 | ** 5 8 IEEE float |
danielk1977 | 183f9f7 | 2004-05-13 05:20:26 +0000 | [diff] [blame] | 1156 | ** 6 0 NULL |
| 1157 | ** 7..11 reserved for expansion |
danielk1977 | 90e4d95 | 2004-05-10 10:05:53 +0000 | [diff] [blame] | 1158 | ** N>=12 and even (N-12)/2 BLOB |
| 1159 | ** N>=13 and odd (N-13)/2 text |
| 1160 | ** |
| 1161 | */ |
| 1162 | |
| 1163 | /* |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1164 | ** Return the serial-type for the value stored in pMem. |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 1165 | */ |
drh | 25aa1b4 | 2004-05-28 01:39:01 +0000 | [diff] [blame^] | 1166 | u32 sqlite3VdbeSerialType(Mem *pMem){ |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1167 | int flags = pMem->flags; |
| 1168 | |
| 1169 | if( flags&MEM_Null ){ |
danielk1977 | 183f9f7 | 2004-05-13 05:20:26 +0000 | [diff] [blame] | 1170 | return 6; |
danielk1977 | 90e4d95 | 2004-05-10 10:05:53 +0000 | [diff] [blame] | 1171 | } |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1172 | if( flags&MEM_Int ){ |
| 1173 | /* Figure out whether to use 1, 2, 4 or 8 bytes. */ |
| 1174 | i64 i = pMem->i; |
| 1175 | if( i>=-127 && i<=127 ) return 1; |
| 1176 | if( i>=-32767 && i<=32767 ) return 2; |
| 1177 | if( i>=-2147483647 && i<=2147483647 ) return 3; |
| 1178 | return 4; |
danielk1977 | 90e4d95 | 2004-05-10 10:05:53 +0000 | [diff] [blame] | 1179 | } |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1180 | if( flags&MEM_Real ){ |
| 1181 | return 5; |
danielk1977 | 90e4d95 | 2004-05-10 10:05:53 +0000 | [diff] [blame] | 1182 | } |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1183 | if( flags&MEM_Str ){ |
danielk1977 | 93d4675 | 2004-05-23 13:30:58 +0000 | [diff] [blame] | 1184 | int n = pMem->n; |
| 1185 | assert( n>=0 ); |
danielk1977 | 93d4675 | 2004-05-23 13:30:58 +0000 | [diff] [blame] | 1186 | return ((n*2) + 13); |
danielk1977 | 90e4d95 | 2004-05-10 10:05:53 +0000 | [diff] [blame] | 1187 | } |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1188 | if( flags&MEM_Blob ){ |
| 1189 | return (pMem->n*2 + 12); |
| 1190 | } |
| 1191 | return 0; |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 1192 | } |
| 1193 | |
| 1194 | /* |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1195 | ** Return the length of the data corresponding to the supplied serial-type. |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 1196 | */ |
drh | 25aa1b4 | 2004-05-28 01:39:01 +0000 | [diff] [blame^] | 1197 | int sqlite3VdbeSerialTypeLen(u32 serial_type){ |
danielk1977 | 183f9f7 | 2004-05-13 05:20:26 +0000 | [diff] [blame] | 1198 | assert( serial_type!=0 ); |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1199 | switch(serial_type){ |
danielk1977 | 183f9f7 | 2004-05-13 05:20:26 +0000 | [diff] [blame] | 1200 | case 6: return 0; /* NULL */ |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1201 | case 1: return 1; /* 1 byte integer */ |
| 1202 | case 2: return 2; /* 2 byte integer */ |
| 1203 | case 3: return 4; /* 4 byte integer */ |
| 1204 | case 4: return 8; /* 8 byte integer */ |
| 1205 | case 5: return 8; /* 8 byte float */ |
danielk1977 | 90e4d95 | 2004-05-10 10:05:53 +0000 | [diff] [blame] | 1206 | } |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1207 | assert( serial_type>=12 ); |
| 1208 | return ((serial_type-12)>>1); /* text or blob */ |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 1209 | } |
| 1210 | |
| 1211 | /* |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1212 | ** Write the serialized data blob for the value stored in pMem into |
| 1213 | ** buf. It is assumed that the caller has allocated sufficient space. |
| 1214 | ** Return the number of bytes written. |
| 1215 | */ |
danielk1977 | b1bc953 | 2004-05-22 03:05:33 +0000 | [diff] [blame] | 1216 | int sqlite3VdbeSerialPut(unsigned char *buf, Mem *pMem){ |
drh | 25aa1b4 | 2004-05-28 01:39:01 +0000 | [diff] [blame^] | 1217 | u32 serial_type = sqlite3VdbeSerialType(pMem); |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1218 | int len; |
danielk1977 | 183f9f7 | 2004-05-13 05:20:26 +0000 | [diff] [blame] | 1219 | |
| 1220 | assert( serial_type!=0 ); |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1221 | |
| 1222 | /* NULL */ |
danielk1977 | 183f9f7 | 2004-05-13 05:20:26 +0000 | [diff] [blame] | 1223 | if( serial_type==6 ){ |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1224 | return 0; |
| 1225 | } |
| 1226 | |
drh | 1483e14 | 2004-05-21 21:12:42 +0000 | [diff] [blame] | 1227 | /* Integer and Real */ |
| 1228 | if( serial_type<=5 ){ |
| 1229 | u64 v; |
| 1230 | int i; |
| 1231 | if( serial_type==5 ){ |
| 1232 | v = *(u64*)&pMem->r; |
| 1233 | }else{ |
| 1234 | v = *(u64*)&pMem->i; |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1235 | } |
drh | 1483e14 | 2004-05-21 21:12:42 +0000 | [diff] [blame] | 1236 | len = i = sqlite3VdbeSerialTypeLen(serial_type); |
| 1237 | while( i-- ){ |
| 1238 | buf[i] = (v&0xFF); |
| 1239 | v >>= 8; |
| 1240 | } |
| 1241 | return len; |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1242 | } |
| 1243 | |
| 1244 | /* String or blob */ |
| 1245 | assert( serial_type>=12 ); |
| 1246 | len = sqlite3VdbeSerialTypeLen(serial_type); |
| 1247 | memcpy(buf, pMem->z, len); |
| 1248 | return len; |
| 1249 | } |
| 1250 | |
| 1251 | /* |
| 1252 | ** Deserialize the data blob pointed to by buf as serial type serial_type |
| 1253 | ** and store the result in pMem. Return the number of bytes read. |
| 1254 | */ |
danielk1977 | b1bc953 | 2004-05-22 03:05:33 +0000 | [diff] [blame] | 1255 | int sqlite3VdbeSerialGet( |
danielk1977 | 93d4675 | 2004-05-23 13:30:58 +0000 | [diff] [blame] | 1256 | const unsigned char *buf, /* Buffer to deserialize from */ |
drh | 25aa1b4 | 2004-05-28 01:39:01 +0000 | [diff] [blame^] | 1257 | u32 serial_type, /* Serial type to deserialize */ |
| 1258 | Mem *pMem /* Memory cell to write value into */ |
danielk1977 | b1bc953 | 2004-05-22 03:05:33 +0000 | [diff] [blame] | 1259 | ){ |
danielk1977 | 90e4d95 | 2004-05-10 10:05:53 +0000 | [diff] [blame] | 1260 | int len; |
| 1261 | |
danielk1977 | 183f9f7 | 2004-05-13 05:20:26 +0000 | [diff] [blame] | 1262 | assert( serial_type!=0 ); |
| 1263 | |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1264 | /* memset(pMem, 0, sizeof(pMem)); */ |
| 1265 | pMem->flags = 0; |
| 1266 | pMem->z = 0; |
danielk1977 | 90e4d95 | 2004-05-10 10:05:53 +0000 | [diff] [blame] | 1267 | |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1268 | /* NULL */ |
danielk1977 | 183f9f7 | 2004-05-13 05:20:26 +0000 | [diff] [blame] | 1269 | if( serial_type==6 ){ |
danielk1977 | 90e4d95 | 2004-05-10 10:05:53 +0000 | [diff] [blame] | 1270 | pMem->flags = MEM_Null; |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 1271 | pMem->type = SQLITE3_NULL; |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1272 | return 0; |
danielk1977 | 90e4d95 | 2004-05-10 10:05:53 +0000 | [diff] [blame] | 1273 | } |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1274 | |
drh | 1483e14 | 2004-05-21 21:12:42 +0000 | [diff] [blame] | 1275 | /* Integer and Real */ |
| 1276 | if( serial_type<=5 ){ |
| 1277 | u64 v = 0; |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1278 | int n; |
| 1279 | len = sqlite3VdbeSerialTypeLen(serial_type); |
danielk1977 | 90e4d95 | 2004-05-10 10:05:53 +0000 | [diff] [blame] | 1280 | |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1281 | if( buf[0]&0x80 ){ |
drh | 1483e14 | 2004-05-21 21:12:42 +0000 | [diff] [blame] | 1282 | v = -1; |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1283 | } |
| 1284 | for(n=0; n<len; n++){ |
drh | 1483e14 | 2004-05-21 21:12:42 +0000 | [diff] [blame] | 1285 | v = (v<<8) | buf[n]; |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1286 | } |
drh | 1483e14 | 2004-05-21 21:12:42 +0000 | [diff] [blame] | 1287 | if( serial_type==5 ){ |
| 1288 | pMem->flags = MEM_Real; |
| 1289 | pMem->r = *(double*)&v; |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 1290 | pMem->type = SQLITE3_FLOAT; |
drh | 1483e14 | 2004-05-21 21:12:42 +0000 | [diff] [blame] | 1291 | }else{ |
| 1292 | pMem->flags = MEM_Int; |
danielk1977 | b1bc953 | 2004-05-22 03:05:33 +0000 | [diff] [blame] | 1293 | pMem->i = *(i64*)&v; |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 1294 | pMem->type = SQLITE3_INTEGER; |
drh | 1483e14 | 2004-05-21 21:12:42 +0000 | [diff] [blame] | 1295 | } |
| 1296 | return len; |
danielk1977 | 90e4d95 | 2004-05-10 10:05:53 +0000 | [diff] [blame] | 1297 | } |
| 1298 | |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1299 | /* String or blob */ |
| 1300 | assert( serial_type>=12 ); |
danielk1977 | eb015e0 | 2004-05-18 01:31:14 +0000 | [diff] [blame] | 1301 | len = sqlite3VdbeSerialTypeLen(serial_type); |
danielk1977 | 61de0d1 | 2004-05-27 23:56:16 +0000 | [diff] [blame] | 1302 | pMem->z = (char *)buf; |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 1303 | pMem->n = len; |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1304 | if( serial_type&0x01 ){ |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 1305 | pMem->flags = MEM_Str | MEM_Ephem; |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1306 | }else{ |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 1307 | pMem->flags = MEM_Blob | MEM_Ephem; |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1308 | } |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 1309 | sqlite3VdbeMemMakeWriteable(pMem); |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1310 | return len; |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 1311 | } |
| 1312 | |
| 1313 | /* |
drh | ab9f7f1 | 2004-05-08 10:56:11 +0000 | [diff] [blame] | 1314 | ** The following is the comparison function for (non-integer) |
| 1315 | ** keys in the btrees. This function returns negative, zero, or |
| 1316 | ** positive if the first key is less than, equal to, or greater than |
| 1317 | ** the second. |
| 1318 | ** |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 1319 | ** This function assumes that each key consists of one or more type/blob |
danielk1977 | 183f9f7 | 2004-05-13 05:20:26 +0000 | [diff] [blame] | 1320 | ** pairs, encoded using the sqlite3VdbeSerialXXX() functions above. |
| 1321 | ** |
| 1322 | ** Following the type/blob pairs, each key may have a single 0x00 byte |
| 1323 | ** followed by a varint. A key may only have this traling 0x00/varint |
| 1324 | ** pair if it has at least as many type/blob pairs as the key it is being |
| 1325 | ** compared to. |
drh | ab9f7f1 | 2004-05-08 10:56:11 +0000 | [diff] [blame] | 1326 | */ |
drh | ab9f7f1 | 2004-05-08 10:56:11 +0000 | [diff] [blame] | 1327 | int sqlite3VdbeKeyCompare( |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 1328 | void *userData, |
danielk1977 | 183f9f7 | 2004-05-13 05:20:26 +0000 | [diff] [blame] | 1329 | int nKey1, const void *pKey1, |
| 1330 | int nKey2, const void *pKey2 |
drh | ab9f7f1 | 2004-05-08 10:56:11 +0000 | [diff] [blame] | 1331 | ){ |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 1332 | KeyInfo *pKeyInfo = (KeyInfo*)userData; |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 1333 | int offset1 = 0; |
| 1334 | int offset2 = 0; |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 1335 | int i = 0; |
drh | ffbc308 | 2004-05-21 01:29:06 +0000 | [diff] [blame] | 1336 | int rc = 0; |
danielk1977 | 183f9f7 | 2004-05-13 05:20:26 +0000 | [diff] [blame] | 1337 | const unsigned char *aKey1 = (const unsigned char *)pKey1; |
| 1338 | const unsigned char *aKey2 = (const unsigned char *)pKey2; |
| 1339 | |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 1340 | assert( pKeyInfo!=0 ); |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 1341 | while( offset1<nKey1 && offset2<nKey2 ){ |
| 1342 | Mem mem1; |
| 1343 | Mem mem2; |
drh | 25aa1b4 | 2004-05-28 01:39:01 +0000 | [diff] [blame^] | 1344 | u32 serial_type1; |
| 1345 | u32 serial_type2; |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 1346 | |
danielk1977 | 183f9f7 | 2004-05-13 05:20:26 +0000 | [diff] [blame] | 1347 | /* Read the serial types for the next element in each key. */ |
drh | 25aa1b4 | 2004-05-28 01:39:01 +0000 | [diff] [blame^] | 1348 | offset1 += sqlite3GetVarint32(&aKey1[offset1], &serial_type1); |
| 1349 | offset2 += sqlite3GetVarint32(&aKey2[offset2], &serial_type2); |
danielk1977 | 183f9f7 | 2004-05-13 05:20:26 +0000 | [diff] [blame] | 1350 | |
| 1351 | /* If either of the varints just read in are 0 (not a type), then |
| 1352 | ** this is the end of the keys. The remaining data in each key is |
| 1353 | ** the varint rowid. Compare these as signed integers and return |
| 1354 | ** the result. |
| 1355 | */ |
| 1356 | if( !serial_type1 || !serial_type2 ){ |
| 1357 | assert( !serial_type1 && !serial_type2 ); |
drh | 25aa1b4 | 2004-05-28 01:39:01 +0000 | [diff] [blame^] | 1358 | sqlite3GetVarint32(&aKey1[offset1], &serial_type1); |
| 1359 | sqlite3GetVarint32(&aKey2[offset2], &serial_type2); |
drh | ffbc308 | 2004-05-21 01:29:06 +0000 | [diff] [blame] | 1360 | if( serial_type1 < serial_type2 ){ |
| 1361 | rc = -1; |
| 1362 | }else if( serial_type1 > serial_type2 ){ |
| 1363 | rc = +1; |
| 1364 | }else{ |
| 1365 | rc = 0; |
| 1366 | } |
| 1367 | return rc; |
danielk1977 | 183f9f7 | 2004-05-13 05:20:26 +0000 | [diff] [blame] | 1368 | } |
| 1369 | |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 1370 | assert( i<pKeyInfo->nField ); |
| 1371 | |
danielk1977 | 183f9f7 | 2004-05-13 05:20:26 +0000 | [diff] [blame] | 1372 | /* Assert that there is enough space left in each key for the blob of |
| 1373 | ** data to go with the serial type just read. This assert may fail if |
| 1374 | ** the file is corrupted. Then read the value from each key into mem1 |
| 1375 | ** and mem2 respectively. |
| 1376 | */ |
drh | 25aa1b4 | 2004-05-28 01:39:01 +0000 | [diff] [blame^] | 1377 | offset1 += sqlite3VdbeSerialGet(&aKey1[offset1], serial_type1, &mem1); |
| 1378 | offset2 += sqlite3VdbeSerialGet(&aKey2[offset2], serial_type2, &mem2); |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 1379 | |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 1380 | rc = sqlite3MemCompare(&mem1, &mem2, pKeyInfo->aColl[i]); |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 1381 | if( mem1.flags&MEM_Dyn ){ |
| 1382 | sqliteFree(mem1.z); |
| 1383 | } |
| 1384 | if( mem2.flags&MEM_Dyn ){ |
| 1385 | sqliteFree(mem2.z); |
| 1386 | } |
| 1387 | if( rc!=0 ){ |
drh | ffbc308 | 2004-05-21 01:29:06 +0000 | [diff] [blame] | 1388 | break; |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 1389 | } |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 1390 | i++; |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 1391 | } |
| 1392 | |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 1393 | /* One of the keys ran out of fields, but all the fields up to that point |
| 1394 | ** were equal. If the incrKey flag is true, then the second key is |
| 1395 | ** treated as larger. |
| 1396 | */ |
drh | ffbc308 | 2004-05-21 01:29:06 +0000 | [diff] [blame] | 1397 | if( rc==0 ){ |
| 1398 | if( pKeyInfo->incrKey ){ |
| 1399 | assert( offset2==nKey2 ); |
| 1400 | rc = -1; |
| 1401 | }else if( offset1<nKey1 ){ |
| 1402 | rc = 1; |
| 1403 | }else if( offset2<nKey2 ){ |
| 1404 | rc = -1; |
| 1405 | } |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 1406 | } |
| 1407 | |
drh | ffbc308 | 2004-05-21 01:29:06 +0000 | [diff] [blame] | 1408 | if( pKeyInfo->aSortOrder && i<pKeyInfo->nField && pKeyInfo->aSortOrder[i] ){ |
| 1409 | rc = -rc; |
drh | ab9f7f1 | 2004-05-08 10:56:11 +0000 | [diff] [blame] | 1410 | } |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 1411 | |
drh | ffbc308 | 2004-05-21 01:29:06 +0000 | [diff] [blame] | 1412 | return rc; |
drh | ab9f7f1 | 2004-05-08 10:56:11 +0000 | [diff] [blame] | 1413 | } |
danielk1977 | 183f9f7 | 2004-05-13 05:20:26 +0000 | [diff] [blame] | 1414 | |
| 1415 | /* |
danielk1977 | eb015e0 | 2004-05-18 01:31:14 +0000 | [diff] [blame] | 1416 | ** This function compares the two table row records specified by |
| 1417 | ** {nKey1, pKey1} and {nKey2, pKey2}, returning a negative, zero |
| 1418 | ** or positive integer if {nKey1, pKey1} is less than, equal to or |
| 1419 | ** greater than {nKey2, pKey2}. |
| 1420 | ** |
danielk1977 | 84ac9d0 | 2004-05-18 09:58:06 +0000 | [diff] [blame] | 1421 | ** This function is pretty inefficient and will probably be replaced |
danielk1977 | eb015e0 | 2004-05-18 01:31:14 +0000 | [diff] [blame] | 1422 | ** by something else in the near future. It is currently required |
| 1423 | ** by compound SELECT operators. |
| 1424 | */ |
| 1425 | int sqlite3VdbeRowCompare( |
| 1426 | void *userData, |
| 1427 | int nKey1, const void *pKey1, |
| 1428 | int nKey2, const void *pKey2 |
| 1429 | ){ |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 1430 | KeyInfo *pKeyInfo = (KeyInfo*)userData; |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 1431 | u32 d1, d2; /* Offset into aKey[] of next data element */ |
| 1432 | u32 idx1, idx2; /* Offset into aKey[] of next header element */ |
| 1433 | u32 szHdr1, szHdr2; /* Number of bytes in header */ |
| 1434 | int i = 0; |
| 1435 | int nField; |
| 1436 | int rc = 0; |
danielk1977 | eb015e0 | 2004-05-18 01:31:14 +0000 | [diff] [blame] | 1437 | const unsigned char *aKey1 = (const unsigned char *)pKey1; |
| 1438 | const unsigned char *aKey2 = (const unsigned char *)pKey2; |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 1439 | |
| 1440 | idx1 = sqlite3GetVarint32(pKey1, &szHdr1); |
| 1441 | d1 = szHdr1; |
| 1442 | idx2 = sqlite3GetVarint32(pKey2, &szHdr2); |
| 1443 | d2 = szHdr2; |
| 1444 | nField = pKeyInfo->nField; |
| 1445 | while( idx1<szHdr1 && idx2<szHdr2 && d1<nKey1 && d2<nKey2 && i<nField ){ |
danielk1977 | 84ac9d0 | 2004-05-18 09:58:06 +0000 | [diff] [blame] | 1446 | Mem mem1; |
| 1447 | Mem mem2; |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 1448 | u32 serial_type1; |
| 1449 | u32 serial_type2; |
danielk1977 | 84ac9d0 | 2004-05-18 09:58:06 +0000 | [diff] [blame] | 1450 | |
| 1451 | /* Read the serial types for the next element in each key. */ |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 1452 | idx1 += sqlite3GetVarint32(&aKey1[idx1], &serial_type1); |
| 1453 | idx2 += sqlite3GetVarint32(&aKey2[idx2], &serial_type2); |
danielk1977 | 84ac9d0 | 2004-05-18 09:58:06 +0000 | [diff] [blame] | 1454 | |
| 1455 | /* Assert that there is enough space left in each key for the blob of |
| 1456 | ** data to go with the serial type just read. This assert may fail if |
| 1457 | ** the file is corrupted. Then read the value from each key into mem1 |
| 1458 | ** and mem2 respectively. |
| 1459 | */ |
drh | 25aa1b4 | 2004-05-28 01:39:01 +0000 | [diff] [blame^] | 1460 | d1 += sqlite3VdbeSerialGet(&aKey1[d1], serial_type1, &mem1); |
| 1461 | d2 += sqlite3VdbeSerialGet(&aKey2[d2], serial_type2, &mem2); |
danielk1977 | 84ac9d0 | 2004-05-18 09:58:06 +0000 | [diff] [blame] | 1462 | |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 1463 | rc = sqlite3MemCompare(&mem1, &mem2, pKeyInfo->aColl[i]); |
danielk1977 | 84ac9d0 | 2004-05-18 09:58:06 +0000 | [diff] [blame] | 1464 | if( mem1.flags&MEM_Dyn ){ |
| 1465 | sqliteFree(mem1.z); |
| 1466 | } |
| 1467 | if( mem2.flags&MEM_Dyn ){ |
| 1468 | sqliteFree(mem2.z); |
| 1469 | } |
| 1470 | if( rc!=0 ){ |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 1471 | break; |
| 1472 | } |
| 1473 | i++; |
| 1474 | } |
| 1475 | |
| 1476 | /* One of the keys ran out of fields, but all the fields up to that point |
| 1477 | ** were equal. If the incrKey flag is true, then the second key is |
| 1478 | ** treated as larger. |
| 1479 | */ |
| 1480 | if( rc==0 ){ |
| 1481 | if( pKeyInfo->incrKey ){ |
| 1482 | assert( d2==nKey2 ); |
| 1483 | rc = -1; |
| 1484 | }else if( d1<nKey1 ){ |
| 1485 | rc = 1; |
| 1486 | }else if( d2<nKey2 ){ |
| 1487 | rc = -1; |
danielk1977 | 84ac9d0 | 2004-05-18 09:58:06 +0000 | [diff] [blame] | 1488 | } |
| 1489 | } |
| 1490 | |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 1491 | if( pKeyInfo->aSortOrder && i<pKeyInfo->nField && pKeyInfo->aSortOrder[i] ){ |
| 1492 | rc = -rc; |
| 1493 | } |
| 1494 | |
| 1495 | return rc; |
danielk1977 | eb015e0 | 2004-05-18 01:31:14 +0000 | [diff] [blame] | 1496 | } |
| 1497 | |
| 1498 | |
| 1499 | /* |
danielk1977 | 183f9f7 | 2004-05-13 05:20:26 +0000 | [diff] [blame] | 1500 | ** pCur points at an index entry. Read the rowid (varint occuring at |
| 1501 | ** the end of the entry and store it in *rowid. Return SQLITE_OK if |
| 1502 | ** everything works, or an error code otherwise. |
| 1503 | */ |
| 1504 | int sqlite3VdbeIdxRowid(BtCursor *pCur, i64 *rowid){ |
| 1505 | i64 sz; |
| 1506 | int rc; |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 1507 | char buf[10]; |
danielk1977 | 183f9f7 | 2004-05-13 05:20:26 +0000 | [diff] [blame] | 1508 | int len; |
| 1509 | u64 r; |
| 1510 | |
| 1511 | rc = sqlite3BtreeKeySize(pCur, &sz); |
| 1512 | if( rc!=SQLITE_OK ){ |
| 1513 | return rc; |
| 1514 | } |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 1515 | len = ((sz>10)?10:sz); |
| 1516 | |
| 1517 | /* If there are less than 2 bytes in the key, this cannot be |
| 1518 | ** a valid index entry. In practice this comes up for a query |
| 1519 | ** of the sort "SELECT max(x) FROM t1;" when t1 is an empty table |
| 1520 | ** with an index on x. In this case just call the rowid 0. |
| 1521 | */ |
| 1522 | if( len<2 ){ |
| 1523 | *rowid = 0; |
| 1524 | return SQLITE_OK; |
| 1525 | } |
danielk1977 | 183f9f7 | 2004-05-13 05:20:26 +0000 | [diff] [blame] | 1526 | |
| 1527 | rc = sqlite3BtreeKey(pCur, sz-len, len, buf); |
| 1528 | if( rc!=SQLITE_OK ){ |
| 1529 | return rc; |
| 1530 | } |
| 1531 | |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 1532 | len--; |
| 1533 | while( buf[len-1] && --len ); |
danielk1977 | 183f9f7 | 2004-05-13 05:20:26 +0000 | [diff] [blame] | 1534 | |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 1535 | sqlite3GetVarint(&buf[len], &r); |
danielk1977 | 183f9f7 | 2004-05-13 05:20:26 +0000 | [diff] [blame] | 1536 | *rowid = r; |
| 1537 | return SQLITE_OK; |
| 1538 | } |
| 1539 | |
drh | 7cf6e4d | 2004-05-19 14:56:55 +0000 | [diff] [blame] | 1540 | /* |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 1541 | ** 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] | 1542 | ** the key string in pKey (of length nKey). Write into *pRes a number |
| 1543 | ** that is negative, zero, or positive if pC is less than, equal to, |
| 1544 | ** or greater than pKey. Return SQLITE_OK on success. |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 1545 | ** |
| 1546 | ** pKey might contain fewer terms than the cursor. |
drh | 7cf6e4d | 2004-05-19 14:56:55 +0000 | [diff] [blame] | 1547 | */ |
danielk1977 | 183f9f7 | 2004-05-13 05:20:26 +0000 | [diff] [blame] | 1548 | int sqlite3VdbeIdxKeyCompare( |
drh | 7cf6e4d | 2004-05-19 14:56:55 +0000 | [diff] [blame] | 1549 | Cursor *pC, /* The cursor to compare against */ |
| 1550 | int nKey, const u8 *pKey, /* The key to compare */ |
| 1551 | int *res /* Write the comparison result here */ |
danielk1977 | 183f9f7 | 2004-05-13 05:20:26 +0000 | [diff] [blame] | 1552 | ){ |
| 1553 | unsigned char *pCellKey; |
| 1554 | u64 nCellKey; |
| 1555 | int freeCellKey = 0; |
| 1556 | int rc; |
| 1557 | int len; |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 1558 | BtCursor *pCur = pC->pCursor; |
danielk1977 | 183f9f7 | 2004-05-13 05:20:26 +0000 | [diff] [blame] | 1559 | |
| 1560 | sqlite3BtreeKeySize(pCur, &nCellKey); |
| 1561 | if( nCellKey<=0 ){ |
| 1562 | *res = 0; |
| 1563 | return SQLITE_OK; |
| 1564 | } |
| 1565 | |
| 1566 | pCellKey = (unsigned char *)sqlite3BtreeKeyFetch(pCur, nCellKey); |
| 1567 | if( !pCellKey ){ |
drh | 10617cd | 2004-05-14 15:27:27 +0000 | [diff] [blame] | 1568 | pCellKey = (unsigned char *)sqliteMallocRaw(nCellKey); |
danielk1977 | 183f9f7 | 2004-05-13 05:20:26 +0000 | [diff] [blame] | 1569 | if( !pCellKey ){ |
| 1570 | return SQLITE_NOMEM; |
| 1571 | } |
| 1572 | freeCellKey = 1; |
| 1573 | rc = sqlite3BtreeKey(pCur, 0, nCellKey, pCellKey); |
| 1574 | if( rc!=SQLITE_OK ){ |
| 1575 | sqliteFree(pCellKey); |
| 1576 | return rc; |
| 1577 | } |
| 1578 | } |
| 1579 | |
| 1580 | len = nCellKey-2; |
| 1581 | while( pCellKey[len] && --len ); |
| 1582 | |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 1583 | *res = sqlite3VdbeKeyCompare(pC->pKeyInfo, len, pCellKey, nKey, pKey); |
danielk1977 | 183f9f7 | 2004-05-13 05:20:26 +0000 | [diff] [blame] | 1584 | |
| 1585 | if( freeCellKey ){ |
| 1586 | sqliteFree(pCellKey); |
| 1587 | } |
| 1588 | return SQLITE_OK; |
| 1589 | } |