drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 1 | /* |
| 2 | ** 2003 September 6 |
| 3 | ** |
| 4 | ** The author disclaims copyright to this source code. In place of |
| 5 | ** a legal notice, here is a blessing: |
| 6 | ** |
| 7 | ** May you do good and not evil. |
| 8 | ** May you find forgiveness for yourself and forgive others. |
| 9 | ** May you share freely, never taking more than you give. |
| 10 | ** |
| 11 | ************************************************************************* |
| 12 | ** This file contains code used for creating, destroying, and populating |
| 13 | ** a VDBE (or an "sqlite_vm" as it is known to the outside world.) Prior |
| 14 | ** to version 2.8.7, all this code was combined into the vdbe.c source file. |
| 15 | ** But that file was getting too big so this subroutines were split out. |
| 16 | */ |
| 17 | #include "sqliteInt.h" |
| 18 | #include "os.h" |
| 19 | #include <ctype.h> |
| 20 | #include "vdbeInt.h" |
| 21 | |
| 22 | |
| 23 | /* |
| 24 | ** When debugging the code generator in a symbolic debugger, one can |
danielk1977 | 132872b | 2004-05-10 10:37:18 +0000 | [diff] [blame] | 25 | ** set the sqlite3_vdbe_addop_trace to 1 and all opcodes will be printed |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 26 | ** as they are added to the instruction stream. |
| 27 | */ |
| 28 | #ifndef NDEBUG |
danielk1977 | 132872b | 2004-05-10 10:37:18 +0000 | [diff] [blame] | 29 | int sqlite3_vdbe_addop_trace = 0; |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 30 | #endif |
| 31 | |
| 32 | |
| 33 | /* |
| 34 | ** Create a new virtual database engine. |
| 35 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 36 | Vdbe *sqlite3VdbeCreate(sqlite *db){ |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 37 | Vdbe *p; |
| 38 | p = sqliteMalloc( sizeof(Vdbe) ); |
| 39 | if( p==0 ) return 0; |
| 40 | p->db = db; |
| 41 | if( db->pVdbe ){ |
| 42 | db->pVdbe->pPrev = p; |
| 43 | } |
| 44 | p->pNext = db->pVdbe; |
| 45 | p->pPrev = 0; |
| 46 | db->pVdbe = p; |
| 47 | p->magic = VDBE_MAGIC_INIT; |
| 48 | return p; |
| 49 | } |
| 50 | |
| 51 | /* |
| 52 | ** Turn tracing on or off |
| 53 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 54 | void sqlite3VdbeTrace(Vdbe *p, FILE *trace){ |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 55 | p->trace = trace; |
| 56 | } |
| 57 | |
| 58 | /* |
| 59 | ** Add a new instruction to the list of instructions current in the |
| 60 | ** VDBE. Return the address of the new instruction. |
| 61 | ** |
| 62 | ** Parameters: |
| 63 | ** |
| 64 | ** p Pointer to the VDBE |
| 65 | ** |
| 66 | ** op The opcode for this instruction |
| 67 | ** |
| 68 | ** p1, p2 First two of the three possible operands. |
| 69 | ** |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 70 | ** Use the sqlite3VdbeResolveLabel() function to fix an address and |
| 71 | ** the sqlite3VdbeChangeP3() function to change the value of the P3 |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 72 | ** operand. |
| 73 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 74 | int sqlite3VdbeAddOp(Vdbe *p, int op, int p1, int p2){ |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 75 | int i; |
drh | 701a0ae | 2004-02-22 20:05:00 +0000 | [diff] [blame] | 76 | VdbeOp *pOp; |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 77 | |
| 78 | i = p->nOp; |
| 79 | p->nOp++; |
| 80 | assert( p->magic==VDBE_MAGIC_INIT ); |
| 81 | if( i>=p->nOpAlloc ){ |
| 82 | int oldSize = p->nOpAlloc; |
| 83 | Op *aNew; |
| 84 | p->nOpAlloc = p->nOpAlloc*2 + 100; |
| 85 | aNew = sqliteRealloc(p->aOp, p->nOpAlloc*sizeof(Op)); |
| 86 | if( aNew==0 ){ |
| 87 | p->nOpAlloc = oldSize; |
| 88 | return 0; |
| 89 | } |
| 90 | p->aOp = aNew; |
| 91 | memset(&p->aOp[oldSize], 0, (p->nOpAlloc-oldSize)*sizeof(Op)); |
| 92 | } |
drh | 701a0ae | 2004-02-22 20:05:00 +0000 | [diff] [blame] | 93 | pOp = &p->aOp[i]; |
| 94 | pOp->opcode = op; |
| 95 | pOp->p1 = p1; |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 96 | if( p2<0 && (-1-p2)<p->nLabel && p->aLabel[-1-p2]>=0 ){ |
| 97 | p2 = p->aLabel[-1-p2]; |
| 98 | } |
drh | 701a0ae | 2004-02-22 20:05:00 +0000 | [diff] [blame] | 99 | pOp->p2 = p2; |
| 100 | pOp->p3 = 0; |
| 101 | pOp->p3type = P3_NOTUSED; |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 102 | #ifndef NDEBUG |
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 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 112 | int sqlite3VdbeOp3(Vdbe *p, int op, int p1, int p2, const char *zP3, int p3type){ |
| 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 | /* |
| 428 | ** The following group or routines are employed by installable functions |
| 429 | ** to return their results. |
| 430 | ** |
danielk1977 | 132872b | 2004-05-10 10:37:18 +0000 | [diff] [blame] | 431 | ** The sqlite3_set_result_string() routine can be used to return a string |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 432 | ** value or to return a NULL. To return a NULL, pass in NULL for zResult. |
| 433 | ** A copy is made of the string before this routine returns so it is safe |
| 434 | ** to pass in an ephemeral string. |
| 435 | ** |
danielk1977 | 132872b | 2004-05-10 10:37:18 +0000 | [diff] [blame] | 436 | ** sqlite3_set_result_error() works like sqlite3_set_result_string() except |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 437 | ** that it signals a fatal error. The string argument, if any, is the |
| 438 | ** error message. If the argument is NULL a generic substitute error message |
| 439 | ** is used. |
| 440 | ** |
danielk1977 | 132872b | 2004-05-10 10:37:18 +0000 | [diff] [blame] | 441 | ** The sqlite3_set_result_int() and sqlite3_set_result_double() set the return |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 442 | ** value of the user function to an integer or a double. |
| 443 | ** |
| 444 | ** These routines are defined here in vdbe.c because they depend on knowing |
| 445 | ** the internals of the sqlite_func structure which is only defined in |
| 446 | ** this source file. |
| 447 | */ |
danielk1977 | 132872b | 2004-05-10 10:37:18 +0000 | [diff] [blame] | 448 | char *sqlite3_set_result_string(sqlite_func *p, const char *zResult, int n){ |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 449 | assert( !p->isStep ); |
drh | 00706be | 2004-01-30 14:49:16 +0000 | [diff] [blame] | 450 | if( p->s.flags & MEM_Dyn ){ |
| 451 | sqliteFree(p->s.z); |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 452 | } |
| 453 | if( zResult==0 ){ |
drh | 00706be | 2004-01-30 14:49:16 +0000 | [diff] [blame] | 454 | p->s.flags = MEM_Null; |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 455 | n = 0; |
drh | 00706be | 2004-01-30 14:49:16 +0000 | [diff] [blame] | 456 | p->s.z = 0; |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 457 | p->s.n = 0; |
| 458 | }else{ |
| 459 | if( n<0 ) n = strlen(zResult); |
| 460 | if( n<NBFS-1 ){ |
drh | 00706be | 2004-01-30 14:49:16 +0000 | [diff] [blame] | 461 | memcpy(p->s.zShort, zResult, n); |
| 462 | p->s.zShort[n] = 0; |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 463 | p->s.flags = MEM_Utf8 | MEM_Str | MEM_Short; |
drh | 00706be | 2004-01-30 14:49:16 +0000 | [diff] [blame] | 464 | p->s.z = p->s.zShort; |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 465 | }else{ |
drh | 00706be | 2004-01-30 14:49:16 +0000 | [diff] [blame] | 466 | p->s.z = sqliteMallocRaw( n+1 ); |
| 467 | if( p->s.z ){ |
| 468 | memcpy(p->s.z, zResult, n); |
| 469 | p->s.z[n] = 0; |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 470 | } |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 471 | p->s.flags = MEM_Utf8 | MEM_Str | MEM_Dyn; |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 472 | } |
| 473 | p->s.n = n+1; |
| 474 | } |
drh | 00706be | 2004-01-30 14:49:16 +0000 | [diff] [blame] | 475 | return p->s.z; |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 476 | } |
danielk1977 | 132872b | 2004-05-10 10:37:18 +0000 | [diff] [blame] | 477 | void sqlite3_set_result_int(sqlite_func *p, int iResult){ |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 478 | assert( !p->isStep ); |
drh | 00706be | 2004-01-30 14:49:16 +0000 | [diff] [blame] | 479 | if( p->s.flags & MEM_Dyn ){ |
| 480 | sqliteFree(p->s.z); |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 481 | } |
| 482 | p->s.i = iResult; |
drh | 00706be | 2004-01-30 14:49:16 +0000 | [diff] [blame] | 483 | p->s.flags = MEM_Int; |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 484 | } |
danielk1977 | 132872b | 2004-05-10 10:37:18 +0000 | [diff] [blame] | 485 | void sqlite3_set_result_double(sqlite_func *p, double rResult){ |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 486 | assert( !p->isStep ); |
drh | 00706be | 2004-01-30 14:49:16 +0000 | [diff] [blame] | 487 | if( p->s.flags & MEM_Dyn ){ |
| 488 | sqliteFree(p->s.z); |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 489 | } |
| 490 | p->s.r = rResult; |
drh | 00706be | 2004-01-30 14:49:16 +0000 | [diff] [blame] | 491 | p->s.flags = MEM_Real; |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 492 | } |
danielk1977 | 132872b | 2004-05-10 10:37:18 +0000 | [diff] [blame] | 493 | void sqlite3_set_result_error(sqlite_func *p, const char *zMsg, int n){ |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 494 | assert( !p->isStep ); |
danielk1977 | 132872b | 2004-05-10 10:37:18 +0000 | [diff] [blame] | 495 | sqlite3_set_result_string(p, zMsg, n); |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 496 | p->isError = 1; |
| 497 | } |
| 498 | |
| 499 | /* |
| 500 | ** Extract the user data from a sqlite_func structure and return a |
| 501 | ** pointer to it. |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 502 | */ |
danielk1977 | 132872b | 2004-05-10 10:37:18 +0000 | [diff] [blame] | 503 | void *sqlite3_user_data(sqlite_func *p){ |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 504 | assert( p && p->pFunc ); |
| 505 | return p->pFunc->pUserData; |
| 506 | } |
| 507 | |
| 508 | /* |
| 509 | ** Allocate or return the aggregate context for a user function. A new |
| 510 | ** context is allocated on the first call. Subsequent calls return the |
| 511 | ** same context that was returned on prior calls. |
| 512 | ** |
| 513 | ** This routine is defined here in vdbe.c because it depends on knowing |
| 514 | ** the internals of the sqlite_func structure which is only defined in |
| 515 | ** this source file. |
| 516 | */ |
danielk1977 | 132872b | 2004-05-10 10:37:18 +0000 | [diff] [blame] | 517 | void *sqlite3_aggregate_context(sqlite_func *p, int nByte){ |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 518 | assert( p && p->pFunc && p->pFunc->xStep ); |
| 519 | if( p->pAgg==0 ){ |
| 520 | if( nByte<=NBFS ){ |
drh | 00706be | 2004-01-30 14:49:16 +0000 | [diff] [blame] | 521 | p->pAgg = (void*)p->s.z; |
| 522 | memset(p->pAgg, 0, nByte); |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 523 | }else{ |
| 524 | p->pAgg = sqliteMalloc( nByte ); |
| 525 | } |
| 526 | } |
| 527 | return p->pAgg; |
| 528 | } |
| 529 | |
| 530 | /* |
| 531 | ** Return the number of times the Step function of a aggregate has been |
| 532 | ** called. |
| 533 | ** |
| 534 | ** This routine is defined here in vdbe.c because it depends on knowing |
| 535 | ** the internals of the sqlite_func structure which is only defined in |
| 536 | ** this source file. |
| 537 | */ |
danielk1977 | 132872b | 2004-05-10 10:37:18 +0000 | [diff] [blame] | 538 | int sqlite3_aggregate_count(sqlite_func *p){ |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 539 | assert( p && p->pFunc && p->pFunc->xStep ); |
| 540 | return p->cnt; |
| 541 | } |
| 542 | |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 543 | /* |
| 544 | ** Compute a string that describes the P3 parameter for an opcode. |
| 545 | ** Use zTemp for any required temporary buffer space. |
| 546 | */ |
| 547 | static char *displayP3(Op *pOp, char *zTemp, int nTemp){ |
| 548 | char *zP3; |
| 549 | assert( nTemp>=20 ); |
| 550 | switch( pOp->p3type ){ |
| 551 | case P3_POINTER: { |
| 552 | sprintf(zTemp, "ptr(%#x)", (int)pOp->p3); |
| 553 | zP3 = zTemp; |
| 554 | break; |
| 555 | } |
| 556 | case P3_KEYINFO: { |
| 557 | int i, j; |
| 558 | KeyInfo *pKeyInfo = (KeyInfo*)pOp->p3; |
| 559 | sprintf(zTemp, "keyinfo(%d", pKeyInfo->nField); |
| 560 | i = strlen(zTemp); |
| 561 | for(j=0; j<pKeyInfo->nField; j++){ |
| 562 | CollSeq *pColl = pKeyInfo->aColl[j]; |
| 563 | if( pColl ){ |
| 564 | int n = strlen(pColl->zName); |
| 565 | if( i+n>nTemp-6 ){ |
| 566 | strcpy(&zTemp[i],",..."); |
| 567 | break; |
| 568 | } |
| 569 | zTemp[i++] = ','; |
drh | ffbc308 | 2004-05-21 01:29:06 +0000 | [diff] [blame] | 570 | if( pKeyInfo->aSortOrder && pKeyInfo->aSortOrder[j] ){ |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 571 | zTemp[i++] = '-'; |
| 572 | } |
| 573 | strcpy(&zTemp[i], pColl->zName); |
| 574 | i += n; |
| 575 | }else if( i+4<nTemp-6 ){ |
| 576 | strcpy(&zTemp[i],",nil"); |
| 577 | i += 4; |
| 578 | } |
| 579 | } |
| 580 | zTemp[i++] = ')'; |
| 581 | zTemp[i] = 0; |
| 582 | assert( i<nTemp ); |
| 583 | zP3 = zTemp; |
| 584 | break; |
| 585 | } |
| 586 | case P3_COLLSEQ: { |
| 587 | CollSeq *pColl = (CollSeq*)pOp->p3; |
drh | ffbc308 | 2004-05-21 01:29:06 +0000 | [diff] [blame] | 588 | sprintf(zTemp, "collseq(%.20s)", pColl->zName); |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 589 | zP3 = zTemp; |
| 590 | break; |
| 591 | } |
| 592 | default: { |
| 593 | zP3 = pOp->p3; |
| 594 | if( zP3==0 ){ |
| 595 | zP3 = ""; |
| 596 | } |
| 597 | } |
| 598 | } |
| 599 | return zP3; |
| 600 | } |
| 601 | |
| 602 | |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 603 | #if !defined(NDEBUG) || defined(VDBE_PROFILE) |
| 604 | /* |
| 605 | ** Print a single opcode. This routine is used for debugging only. |
| 606 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 607 | void sqlite3VdbePrintOp(FILE *pOut, int pc, Op *pOp){ |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 608 | char *zP3; |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 609 | char zPtr[50]; |
| 610 | static const char *zFormat1 = "%4d %-13s %4d %4d %s\n"; |
| 611 | static const char *zFormat2 = "%4d %-13s %4d %4d %-20s -- %s\n"; |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 612 | if( pOut==0 ) pOut = stdout; |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 613 | zP3 = displayP3(pOp, zPtr, sizeof(zPtr)); |
| 614 | #ifdef NDEBUG |
| 615 | fprintf(pOut, zFormat1, |
| 616 | pc, sqlite3OpcodeNames[pOp->opcode], pOp->p1, pOp->p2, zP3); |
| 617 | #else |
| 618 | fprintf(pOut, pOp->zComment ? zFormat2 : zFormat1, |
| 619 | pc, sqlite3OpcodeNames[pOp->opcode], pOp->p1, pOp->p2, zP3,pOp->zComment); |
| 620 | #endif |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 621 | fflush(pOut); |
| 622 | } |
| 623 | #endif |
| 624 | |
| 625 | /* |
| 626 | ** Give a listing of the program in the virtual machine. |
| 627 | ** |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 628 | ** The interface is the same as sqlite3VdbeExec(). But instead of |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 629 | ** running the code, it invokes the callback once for each instruction. |
| 630 | ** This feature is used to implement "EXPLAIN". |
| 631 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 632 | int sqlite3VdbeList( |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 633 | Vdbe *p /* The VDBE */ |
| 634 | ){ |
| 635 | sqlite *db = p->db; |
| 636 | int i; |
drh | 826fb5a | 2004-02-14 23:59:57 +0000 | [diff] [blame] | 637 | int rc = SQLITE_OK; |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 638 | static char *azColumnNames[] = { |
| 639 | "addr", "opcode", "p1", "p2", "p3", |
| 640 | "int", "text", "int", "int", "text", |
| 641 | 0 |
| 642 | }; |
| 643 | |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 644 | assert( p->explain ); |
danielk1977 | 18f4189 | 2004-05-22 07:27:46 +0000 | [diff] [blame^] | 645 | |
| 646 | /* Even though this opcode does not put dynamic strings onto the |
| 647 | ** the stack, they may become dynamic if the user calls |
| 648 | ** sqlite3_column_data16(), causing a translation to UTF-16 encoding. |
| 649 | */ |
| 650 | if( p->pTos==&p->aStack[4] ){ |
| 651 | for(i=0; i<5; i++){ |
| 652 | if( p->aStack[i].flags & MEM_Dyn ){ |
| 653 | sqliteFree(p->aStack[i].z); |
| 654 | } |
| 655 | p->aStack[i].flags = 0; |
| 656 | } |
| 657 | } |
| 658 | |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 659 | p->azColName = azColumnNames; |
danielk1977 | 18f4189 | 2004-05-22 07:27:46 +0000 | [diff] [blame^] | 660 | p->resOnStack = 0; |
| 661 | |
| 662 | i = p->pc++; |
drh | 826fb5a | 2004-02-14 23:59:57 +0000 | [diff] [blame] | 663 | if( i>=p->nOp ){ |
| 664 | p->rc = SQLITE_OK; |
| 665 | rc = SQLITE_DONE; |
| 666 | }else if( db->flags & SQLITE_Interrupt ){ |
| 667 | db->flags &= ~SQLITE_Interrupt; |
| 668 | if( db->magic!=SQLITE_MAGIC_BUSY ){ |
| 669 | p->rc = SQLITE_MISUSE; |
| 670 | }else{ |
| 671 | p->rc = SQLITE_INTERRUPT; |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 672 | } |
drh | 826fb5a | 2004-02-14 23:59:57 +0000 | [diff] [blame] | 673 | rc = SQLITE_ERROR; |
danielk1977 | 132872b | 2004-05-10 10:37:18 +0000 | [diff] [blame] | 674 | sqlite3SetString(&p->zErrMsg, sqlite3_error_string(p->rc), (char*)0); |
drh | 826fb5a | 2004-02-14 23:59:57 +0000 | [diff] [blame] | 675 | }else{ |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 676 | Op *pOp = &p->aOp[i]; |
danielk1977 | 18f4189 | 2004-05-22 07:27:46 +0000 | [diff] [blame^] | 677 | p->aStack[0].flags = MEM_Int; |
| 678 | p->aStack[0].i = i; /* Program counter */ |
| 679 | p->aStack[1].flags = MEM_Static|MEM_Str|MEM_Utf8|MEM_Term; |
| 680 | p->aStack[1].z = sqlite3OpcodeNames[pOp->opcode]; /* Opcode */ |
| 681 | p->aStack[2].flags = MEM_Int; |
| 682 | p->aStack[2].i = pOp->p1; /* P1 */ |
| 683 | p->aStack[3].flags = MEM_Int; |
| 684 | p->aStack[3].i = pOp->p2; /* P2 */ |
| 685 | p->aStack[4].flags = MEM_Str|MEM_Utf8|MEM_Term; /* P3 */ |
| 686 | p->aStack[4].z = displayP3(pOp, p->aStack[4].zShort, NBFS); |
| 687 | if( p->aStack[4].z==p->aStack[4].zShort ){ |
| 688 | p->aStack[4].flags |= MEM_Short; |
| 689 | }else{ |
| 690 | p->aStack[4].flags |= MEM_Static; |
| 691 | } |
drh | 826fb5a | 2004-02-14 23:59:57 +0000 | [diff] [blame] | 692 | p->nResColumn = 5; |
danielk1977 | 18f4189 | 2004-05-22 07:27:46 +0000 | [diff] [blame^] | 693 | p->pTos = &p->aStack[4]; |
drh | 826fb5a | 2004-02-14 23:59:57 +0000 | [diff] [blame] | 694 | p->rc = SQLITE_OK; |
danielk1977 | 18f4189 | 2004-05-22 07:27:46 +0000 | [diff] [blame^] | 695 | p->resOnStack = 1; |
drh | 826fb5a | 2004-02-14 23:59:57 +0000 | [diff] [blame] | 696 | rc = SQLITE_ROW; |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 697 | } |
drh | 826fb5a | 2004-02-14 23:59:57 +0000 | [diff] [blame] | 698 | return rc; |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 699 | } |
| 700 | |
| 701 | /* |
| 702 | ** Prepare a virtual machine for execution. This involves things such |
| 703 | ** as allocating stack space and initializing the program counter. |
| 704 | ** 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] | 705 | ** calls to sqlite3VdbeExec(). |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 706 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 707 | void sqlite3VdbeMakeReady( |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 708 | Vdbe *p, /* The VDBE */ |
drh | 7c972de | 2003-09-06 22:18:07 +0000 | [diff] [blame] | 709 | int nVar, /* Number of '?' see in the SQL statement */ |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 710 | int isExplain /* True if the EXPLAIN keywords is present */ |
| 711 | ){ |
| 712 | int n; |
| 713 | |
| 714 | assert( p!=0 ); |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 715 | assert( p->magic==VDBE_MAGIC_INIT ); |
| 716 | |
| 717 | /* Add a HALT instruction to the very end of the program. |
| 718 | */ |
| 719 | 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] | 720 | sqlite3VdbeAddOp(p, OP_Halt, 0, 0); |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 721 | } |
| 722 | |
| 723 | /* No instruction ever pushes more than a single element onto the |
| 724 | ** stack. And the stack never grows on successive executions of the |
| 725 | ** same loop. So the total number of instructions is an upper bound |
| 726 | ** on the maximum stack depth required. |
| 727 | ** |
| 728 | ** Allocation all the stack space we will ever need. |
| 729 | */ |
drh | 82a4851 | 2003-09-06 22:45:20 +0000 | [diff] [blame] | 730 | if( p->aStack==0 ){ |
| 731 | p->nVar = nVar; |
| 732 | assert( nVar>=0 ); |
| 733 | n = isExplain ? 10 : p->nOp; |
| 734 | p->aStack = sqliteMalloc( |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 735 | n*(sizeof(p->aStack[0]) + 2*sizeof(char*)) /* aStack and zArgv */ |
drh | 5a12e68 | 2004-05-19 11:24:25 +0000 | [diff] [blame] | 736 | + p->nVar*sizeof(Mem) /* apVar */ |
drh | 82a4851 | 2003-09-06 22:45:20 +0000 | [diff] [blame] | 737 | ); |
drh | 00706be | 2004-01-30 14:49:16 +0000 | [diff] [blame] | 738 | p->zArgv = (char**)&p->aStack[n]; |
| 739 | p->azColName = (char**)&p->zArgv[n]; |
drh | 5a12e68 | 2004-05-19 11:24:25 +0000 | [diff] [blame] | 740 | p->apVar = (Mem *)&p->azColName[n]; |
danielk1977 | 54db47e | 2004-05-19 10:36:43 +0000 | [diff] [blame] | 741 | for(n=0; n<p->nVar; n++){ |
drh | 5a12e68 | 2004-05-19 11:24:25 +0000 | [diff] [blame] | 742 | p->apVar[n].flags = MEM_Null; |
danielk1977 | 54db47e | 2004-05-19 10:36:43 +0000 | [diff] [blame] | 743 | } |
drh | 82a4851 | 2003-09-06 22:45:20 +0000 | [diff] [blame] | 744 | } |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 745 | |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 746 | sqlite3HashInit(&p->agg.hash, SQLITE_HASH_BINARY, 0); |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 747 | p->agg.pSearch = 0; |
| 748 | #ifdef MEMORY_DEBUG |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 749 | if( sqlite3OsFileExists("vdbe_trace") ){ |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 750 | p->trace = stdout; |
| 751 | } |
| 752 | #endif |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 753 | p->pTos = &p->aStack[-1]; |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 754 | p->pc = 0; |
| 755 | p->rc = SQLITE_OK; |
| 756 | p->uniqueCnt = 0; |
| 757 | p->returnDepth = 0; |
| 758 | p->errorAction = OE_Abort; |
| 759 | p->undoTransOnError = 0; |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 760 | p->popStack = 0; |
| 761 | p->explain |= isExplain; |
| 762 | p->magic = VDBE_MAGIC_RUN; |
| 763 | #ifdef VDBE_PROFILE |
drh | cf64d8b | 2003-12-31 17:57:10 +0000 | [diff] [blame] | 764 | { |
| 765 | int i; |
| 766 | for(i=0; i<p->nOp; i++){ |
| 767 | p->aOp[i].cnt = 0; |
| 768 | p->aOp[i].cycles = 0; |
| 769 | } |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 770 | } |
| 771 | #endif |
| 772 | } |
| 773 | |
| 774 | |
| 775 | /* |
| 776 | ** Remove any elements that remain on the sorter for the VDBE given. |
| 777 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 778 | void sqlite3VdbeSorterReset(Vdbe *p){ |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 779 | while( p->pSort ){ |
| 780 | Sorter *pSorter = p->pSort; |
| 781 | p->pSort = pSorter->pNext; |
| 782 | sqliteFree(pSorter->zKey); |
| 783 | sqliteFree(pSorter->pData); |
| 784 | sqliteFree(pSorter); |
| 785 | } |
| 786 | } |
| 787 | |
| 788 | /* |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 789 | ** Reset an Agg structure. Delete all its contents. |
| 790 | ** |
| 791 | ** For installable aggregate functions, if the step function has been |
| 792 | ** called, make sure the finalizer function has also been called. The |
| 793 | ** finalizer might need to free memory that was allocated as part of its |
| 794 | ** private context. If the finalizer has not been called yet, call it |
| 795 | ** now. |
| 796 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 797 | void sqlite3VdbeAggReset(Agg *pAgg){ |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 798 | int i; |
| 799 | HashElem *p; |
| 800 | for(p = sqliteHashFirst(&pAgg->hash); p; p = sqliteHashNext(p)){ |
| 801 | AggElem *pElem = sqliteHashData(p); |
| 802 | assert( pAgg->apFunc!=0 ); |
| 803 | for(i=0; i<pAgg->nMem; i++){ |
| 804 | Mem *pMem = &pElem->aMem[i]; |
drh | 00706be | 2004-01-30 14:49:16 +0000 | [diff] [blame] | 805 | if( pAgg->apFunc[i] && (pMem->flags & MEM_AggCtx)!=0 ){ |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 806 | sqlite_func ctx; |
| 807 | ctx.pFunc = pAgg->apFunc[i]; |
drh | 00706be | 2004-01-30 14:49:16 +0000 | [diff] [blame] | 808 | ctx.s.flags = MEM_Null; |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 809 | ctx.pAgg = pMem->z; |
drh | 00706be | 2004-01-30 14:49:16 +0000 | [diff] [blame] | 810 | ctx.cnt = pMem->i; |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 811 | ctx.isStep = 0; |
| 812 | ctx.isError = 0; |
| 813 | (*pAgg->apFunc[i]->xFinalize)(&ctx); |
drh | 00706be | 2004-01-30 14:49:16 +0000 | [diff] [blame] | 814 | if( pMem->z!=0 && pMem->z!=pMem->zShort ){ |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 815 | sqliteFree(pMem->z); |
| 816 | } |
drh | 9cbe7ca | 2004-02-18 16:57:23 +0000 | [diff] [blame] | 817 | if( ctx.s.flags & MEM_Dyn ){ |
| 818 | sqliteFree(ctx.s.z); |
| 819 | } |
drh | 00706be | 2004-01-30 14:49:16 +0000 | [diff] [blame] | 820 | }else if( pMem->flags & MEM_Dyn ){ |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 821 | sqliteFree(pMem->z); |
| 822 | } |
| 823 | } |
| 824 | sqliteFree(pElem); |
| 825 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 826 | sqlite3HashClear(&pAgg->hash); |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 827 | sqliteFree(pAgg->apFunc); |
| 828 | pAgg->apFunc = 0; |
| 829 | pAgg->pCurrent = 0; |
| 830 | pAgg->pSearch = 0; |
| 831 | pAgg->nMem = 0; |
| 832 | } |
| 833 | |
| 834 | /* |
| 835 | ** Delete a keylist |
| 836 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 837 | void sqlite3VdbeKeylistFree(Keylist *p){ |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 838 | while( p ){ |
| 839 | Keylist *pNext = p->pNext; |
| 840 | sqliteFree(p); |
| 841 | p = pNext; |
| 842 | } |
| 843 | } |
| 844 | |
| 845 | /* |
| 846 | ** Close a cursor and release all the resources that cursor happens |
| 847 | ** to hold. |
| 848 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 849 | void sqlite3VdbeCleanupCursor(Cursor *pCx){ |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 850 | if( pCx->pCursor ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 851 | sqlite3BtreeCloseCursor(pCx->pCursor); |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 852 | } |
| 853 | if( pCx->pBt ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 854 | sqlite3BtreeClose(pCx->pBt); |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 855 | } |
| 856 | sqliteFree(pCx->pData); |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 857 | sqliteFree(pCx->aType); |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame] | 858 | memset(pCx, 0, sizeof(*pCx)); |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 859 | } |
| 860 | |
| 861 | /* |
| 862 | ** Close all cursors |
| 863 | */ |
| 864 | static void closeAllCursors(Vdbe *p){ |
| 865 | int i; |
| 866 | for(i=0; i<p->nCursor; i++){ |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame] | 867 | Cursor *pC = p->apCsr[i]; |
| 868 | sqlite3VdbeCleanupCursor(pC); |
| 869 | sqliteFree(pC); |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 870 | } |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame] | 871 | sqliteFree(p->apCsr); |
| 872 | p->apCsr = 0; |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 873 | p->nCursor = 0; |
| 874 | } |
| 875 | |
| 876 | /* |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 877 | ** Clean up the VM after execution. |
| 878 | ** |
| 879 | ** This routine will automatically close any cursors, lists, and/or |
| 880 | ** sorters that were left open. It also deletes the values of |
drh | 5a12e68 | 2004-05-19 11:24:25 +0000 | [diff] [blame] | 881 | ** variables in the aVar[] array. |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 882 | */ |
| 883 | static void Cleanup(Vdbe *p){ |
| 884 | int i; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 885 | if( p->aStack ){ |
| 886 | Mem *pTos = p->pTos; |
| 887 | while( pTos>=p->aStack ){ |
| 888 | if( pTos->flags & MEM_Dyn ){ |
| 889 | sqliteFree(pTos->z); |
| 890 | } |
| 891 | pTos--; |
| 892 | } |
| 893 | p->pTos = pTos; |
| 894 | } |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 895 | closeAllCursors(p); |
| 896 | if( p->aMem ){ |
| 897 | for(i=0; i<p->nMem; i++){ |
drh | 00706be | 2004-01-30 14:49:16 +0000 | [diff] [blame] | 898 | if( p->aMem[i].flags & MEM_Dyn ){ |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 899 | sqliteFree(p->aMem[i].z); |
| 900 | } |
| 901 | } |
| 902 | } |
| 903 | sqliteFree(p->aMem); |
| 904 | p->aMem = 0; |
| 905 | p->nMem = 0; |
| 906 | if( p->pList ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 907 | sqlite3VdbeKeylistFree(p->pList); |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 908 | p->pList = 0; |
| 909 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 910 | sqlite3VdbeSorterReset(p); |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 911 | if( p->pFile ){ |
| 912 | if( p->pFile!=stdin ) fclose(p->pFile); |
| 913 | p->pFile = 0; |
| 914 | } |
| 915 | if( p->azField ){ |
| 916 | sqliteFree(p->azField); |
| 917 | p->azField = 0; |
| 918 | } |
| 919 | p->nField = 0; |
| 920 | if( p->zLine ){ |
| 921 | sqliteFree(p->zLine); |
| 922 | p->zLine = 0; |
| 923 | } |
| 924 | p->nLineAlloc = 0; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 925 | sqlite3VdbeAggReset(&p->agg); |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 926 | if( p->aSet ){ |
| 927 | for(i=0; i<p->nSet; i++){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 928 | sqlite3HashClear(&p->aSet[i].hash); |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 929 | } |
| 930 | } |
| 931 | sqliteFree(p->aSet); |
| 932 | p->aSet = 0; |
| 933 | p->nSet = 0; |
| 934 | if( p->keylistStack ){ |
| 935 | int ii; |
| 936 | for(ii = 0; ii < p->keylistStackDepth; ii++){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 937 | sqlite3VdbeKeylistFree(p->keylistStack[ii]); |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 938 | } |
| 939 | sqliteFree(p->keylistStack); |
| 940 | p->keylistStackDepth = 0; |
| 941 | p->keylistStack = 0; |
| 942 | } |
drh | 5f96843 | 2004-02-21 19:02:30 +0000 | [diff] [blame] | 943 | sqliteFree(p->contextStack); |
| 944 | p->contextStack = 0; |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 945 | sqliteFree(p->zErrMsg); |
| 946 | p->zErrMsg = 0; |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 947 | } |
| 948 | |
| 949 | /* |
| 950 | ** Clean up a VDBE after execution but do not delete the VDBE just yet. |
| 951 | ** Write any error messages into *pzErrMsg. Return the result code. |
| 952 | ** |
| 953 | ** After this routine is run, the VDBE should be ready to be executed |
| 954 | ** again. |
| 955 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 956 | int sqlite3VdbeReset(Vdbe *p, char **pzErrMsg){ |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 957 | sqlite *db = p->db; |
| 958 | int i; |
| 959 | |
| 960 | if( p->magic!=VDBE_MAGIC_RUN && p->magic!=VDBE_MAGIC_HALT ){ |
danielk1977 | 132872b | 2004-05-10 10:37:18 +0000 | [diff] [blame] | 961 | sqlite3SetString(pzErrMsg, sqlite3_error_string(SQLITE_MISUSE), (char*)0); |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 962 | sqlite3Error(p->db, SQLITE_MISUSE, sqlite3_error_string(SQLITE_MISUSE),0); |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 963 | return SQLITE_MISUSE; |
| 964 | } |
| 965 | if( p->zErrMsg ){ |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 966 | sqlite3Error(p->db, p->rc, "%s", p->zErrMsg, 0); |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 967 | if( pzErrMsg && *pzErrMsg==0 ){ |
| 968 | *pzErrMsg = p->zErrMsg; |
| 969 | }else{ |
| 970 | sqliteFree(p->zErrMsg); |
| 971 | } |
| 972 | p->zErrMsg = 0; |
drh | a1f9b5e | 2004-02-14 16:31:02 +0000 | [diff] [blame] | 973 | }else if( p->rc ){ |
danielk1977 | 132872b | 2004-05-10 10:37:18 +0000 | [diff] [blame] | 974 | sqlite3SetString(pzErrMsg, sqlite3_error_string(p->rc), (char*)0); |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 975 | sqlite3Error(p->db, p->rc, "%s", sqlite3_error_string(p->rc) , 0); |
| 976 | }else{ |
| 977 | sqlite3Error(p->db, SQLITE_OK, 0); |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 978 | } |
| 979 | Cleanup(p); |
| 980 | if( p->rc!=SQLITE_OK ){ |
| 981 | switch( p->errorAction ){ |
| 982 | case OE_Abort: { |
| 983 | if( !p->undoTransOnError ){ |
| 984 | for(i=0; i<db->nDb; i++){ |
| 985 | if( db->aDb[i].pBt ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 986 | sqlite3BtreeRollbackStmt(db->aDb[i].pBt); |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 987 | } |
| 988 | } |
| 989 | break; |
| 990 | } |
| 991 | /* Fall through to ROLLBACK */ |
| 992 | } |
| 993 | case OE_Rollback: { |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 994 | sqlite3RollbackAll(db); |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 995 | db->flags &= ~SQLITE_InTrans; |
| 996 | db->onError = OE_Default; |
| 997 | break; |
| 998 | } |
| 999 | default: { |
| 1000 | if( p->undoTransOnError ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1001 | sqlite3RollbackAll(db); |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 1002 | db->flags &= ~SQLITE_InTrans; |
| 1003 | db->onError = OE_Default; |
| 1004 | } |
| 1005 | break; |
| 1006 | } |
| 1007 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1008 | sqlite3RollbackInternalChanges(db); |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 1009 | } |
| 1010 | for(i=0; i<db->nDb; i++){ |
| 1011 | if( db->aDb[i].pBt && db->aDb[i].inTrans==2 ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1012 | sqlite3BtreeCommitStmt(db->aDb[i].pBt); |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 1013 | db->aDb[i].inTrans = 1; |
| 1014 | } |
| 1015 | } |
danielk1977 | 132872b | 2004-05-10 10:37:18 +0000 | [diff] [blame] | 1016 | assert( p->pTos<&p->aStack[p->pc] || sqlite3_malloc_failed==1 ); |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 1017 | #ifdef VDBE_PROFILE |
| 1018 | { |
| 1019 | FILE *out = fopen("vdbe_profile.out", "a"); |
| 1020 | if( out ){ |
| 1021 | int i; |
| 1022 | fprintf(out, "---- "); |
| 1023 | for(i=0; i<p->nOp; i++){ |
| 1024 | fprintf(out, "%02x", p->aOp[i].opcode); |
| 1025 | } |
| 1026 | fprintf(out, "\n"); |
| 1027 | for(i=0; i<p->nOp; i++){ |
| 1028 | fprintf(out, "%6d %10lld %8lld ", |
| 1029 | p->aOp[i].cnt, |
| 1030 | p->aOp[i].cycles, |
| 1031 | p->aOp[i].cnt>0 ? p->aOp[i].cycles/p->aOp[i].cnt : 0 |
| 1032 | ); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1033 | sqlite3VdbePrintOp(out, i, &p->aOp[i]); |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 1034 | } |
| 1035 | fclose(out); |
| 1036 | } |
| 1037 | } |
| 1038 | #endif |
| 1039 | p->magic = VDBE_MAGIC_INIT; |
| 1040 | return p->rc; |
| 1041 | } |
| 1042 | |
| 1043 | /* |
| 1044 | ** Clean up and delete a VDBE after execution. Return an integer which is |
| 1045 | ** the result code. Write any error message text into *pzErrMsg. |
| 1046 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1047 | int sqlite3VdbeFinalize(Vdbe *p, char **pzErrMsg){ |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 1048 | int rc; |
| 1049 | sqlite *db; |
| 1050 | |
| 1051 | if( p->magic!=VDBE_MAGIC_RUN && p->magic!=VDBE_MAGIC_HALT ){ |
danielk1977 | 132872b | 2004-05-10 10:37:18 +0000 | [diff] [blame] | 1052 | sqlite3SetString(pzErrMsg, sqlite3_error_string(SQLITE_MISUSE), (char*)0); |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 1053 | if( p->magic==VDBE_MAGIC_INIT ){ |
| 1054 | sqlite3Error(p->db, SQLITE_MISUSE, sqlite3_error_string(SQLITE_MISUSE),0); |
| 1055 | } |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 1056 | return SQLITE_MISUSE; |
| 1057 | } |
| 1058 | db = p->db; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1059 | rc = sqlite3VdbeReset(p, pzErrMsg); |
| 1060 | sqlite3VdbeDelete(p); |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 1061 | if( db->want_to_close && db->pVdbe==0 ){ |
danielk1977 | 132872b | 2004-05-10 10:37:18 +0000 | [diff] [blame] | 1062 | sqlite3_close(db); |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 1063 | } |
drh | a1f9b5e | 2004-02-14 16:31:02 +0000 | [diff] [blame] | 1064 | if( rc==SQLITE_SCHEMA ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1065 | sqlite3ResetInternalSchema(db, 0); |
drh | a1f9b5e | 2004-02-14 16:31:02 +0000 | [diff] [blame] | 1066 | } |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 1067 | return rc; |
| 1068 | } |
| 1069 | |
| 1070 | /* |
danielk1977 | 54db47e | 2004-05-19 10:36:43 +0000 | [diff] [blame] | 1071 | ** Unbind the value bound to variable $i in virtual machine p. This is the |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 1072 | ** the same as binding a NULL value to the column. If the "i" parameter is |
| 1073 | ** out of range, then SQLITE_RANGE is returned. Othewise SQLITE_OK. |
| 1074 | ** |
| 1075 | ** The error code stored in database p->db is overwritten with the return |
| 1076 | ** value in any case. |
danielk1977 | 54db47e | 2004-05-19 10:36:43 +0000 | [diff] [blame] | 1077 | */ |
| 1078 | static int vdbeUnbind(Vdbe *p, int i){ |
| 1079 | Mem *pVar; |
| 1080 | if( p->magic!=VDBE_MAGIC_RUN || p->pc!=0 ){ |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 1081 | sqlite3Error(p->db, SQLITE_MISUSE, 0); |
danielk1977 | 54db47e | 2004-05-19 10:36:43 +0000 | [diff] [blame] | 1082 | return SQLITE_MISUSE; |
| 1083 | } |
| 1084 | if( i<1 || i>p->nVar ){ |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 1085 | sqlite3Error(p->db, SQLITE_RANGE, 0); |
danielk1977 | 54db47e | 2004-05-19 10:36:43 +0000 | [diff] [blame] | 1086 | return SQLITE_RANGE; |
| 1087 | } |
| 1088 | i--; |
drh | 5a12e68 | 2004-05-19 11:24:25 +0000 | [diff] [blame] | 1089 | pVar = &p->apVar[i]; |
danielk1977 | 54db47e | 2004-05-19 10:36:43 +0000 | [diff] [blame] | 1090 | if( pVar->flags&MEM_Dyn ){ |
| 1091 | sqliteFree(pVar->z); |
| 1092 | } |
| 1093 | pVar->flags = MEM_Null; |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 1094 | sqlite3Error(p->db, SQLITE_OK, 0); |
danielk1977 | 54db47e | 2004-05-19 10:36:43 +0000 | [diff] [blame] | 1095 | return SQLITE_OK; |
| 1096 | } |
| 1097 | |
| 1098 | /* |
| 1099 | ** This routine is used to bind text or blob data to an SQL variable (a ?). |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 1100 | ** It may also be used to bind a NULL value, by setting zVal to 0. Any |
| 1101 | ** existing value is unbound. |
| 1102 | ** |
| 1103 | ** The error code stored in p->db is overwritten with the return value in |
| 1104 | ** all cases. |
danielk1977 | 54db47e | 2004-05-19 10:36:43 +0000 | [diff] [blame] | 1105 | */ |
| 1106 | static int vdbeBindBlob( |
| 1107 | Vdbe *p, /* Virtual machine */ |
| 1108 | int i, /* Var number to bind (numbered from 1 upward) */ |
| 1109 | const char *zVal, /* Pointer to blob of data */ |
| 1110 | int bytes, /* Number of bytes to copy */ |
| 1111 | int copy, /* True to copy the memory, false to copy a pointer */ |
| 1112 | int flags /* Valid combination of MEM_Blob, MEM_Str, MEM_UtfXX */ |
| 1113 | ){ |
| 1114 | Mem *pVar; |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 1115 | int rc; |
danielk1977 | 54db47e | 2004-05-19 10:36:43 +0000 | [diff] [blame] | 1116 | |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 1117 | rc = vdbeUnbind(p, i); |
| 1118 | if( rc!=SQLITE_OK ){ |
| 1119 | return rc; |
| 1120 | } |
drh | 5a12e68 | 2004-05-19 11:24:25 +0000 | [diff] [blame] | 1121 | pVar = &p->apVar[i-1]; |
danielk1977 | 54db47e | 2004-05-19 10:36:43 +0000 | [diff] [blame] | 1122 | |
| 1123 | if( zVal ){ |
| 1124 | pVar->n = bytes; |
| 1125 | pVar->flags = flags; |
| 1126 | if( !copy ){ |
| 1127 | pVar->z = (char *)zVal; |
| 1128 | pVar->flags |= MEM_Static; |
| 1129 | }else{ |
| 1130 | if( bytes>NBFS ){ |
| 1131 | pVar->z = (char *)sqliteMalloc(bytes); |
| 1132 | if( !pVar->z ){ |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 1133 | sqlite3Error(p->db, SQLITE_NOMEM, 0); |
danielk1977 | 54db47e | 2004-05-19 10:36:43 +0000 | [diff] [blame] | 1134 | return SQLITE_NOMEM; |
| 1135 | } |
| 1136 | pVar->flags |= MEM_Dyn; |
| 1137 | }else{ |
| 1138 | pVar->z = pVar->zShort; |
| 1139 | pVar->flags |= MEM_Short; |
| 1140 | } |
| 1141 | memcpy(pVar->z, zVal, bytes); |
| 1142 | } |
| 1143 | } |
| 1144 | |
| 1145 | return SQLITE_OK; |
| 1146 | } |
| 1147 | |
danielk1977 | e3209e4 | 2004-05-20 01:40:18 +0000 | [diff] [blame] | 1148 | /* |
| 1149 | ** Bind a 64 bit integer to an SQL statement variable. |
| 1150 | */ |
danielk1977 | 54db47e | 2004-05-19 10:36:43 +0000 | [diff] [blame] | 1151 | int sqlite3_bind_int64(sqlite3_stmt *p, int i, long long int iValue){ |
| 1152 | int rc; |
| 1153 | Vdbe *v = (Vdbe *)p; |
| 1154 | rc = vdbeUnbind(v, i); |
| 1155 | if( rc==SQLITE_OK ){ |
drh | 5a12e68 | 2004-05-19 11:24:25 +0000 | [diff] [blame] | 1156 | Mem *pVar = &v->apVar[i-1]; |
danielk1977 | 54db47e | 2004-05-19 10:36:43 +0000 | [diff] [blame] | 1157 | pVar->flags = MEM_Int; |
| 1158 | pVar->i = iValue; |
| 1159 | } |
danielk1977 | b1bc953 | 2004-05-22 03:05:33 +0000 | [diff] [blame] | 1160 | return rc; |
danielk1977 | 54db47e | 2004-05-19 10:36:43 +0000 | [diff] [blame] | 1161 | } |
| 1162 | |
danielk1977 | e3209e4 | 2004-05-20 01:40:18 +0000 | [diff] [blame] | 1163 | /* |
| 1164 | ** Bind a 32 bit integer to an SQL statement variable. |
| 1165 | */ |
danielk1977 | 54db47e | 2004-05-19 10:36:43 +0000 | [diff] [blame] | 1166 | int sqlite3_bind_int32(sqlite3_stmt *p, int i, int iValue){ |
| 1167 | return sqlite3_bind_int64(p, i, (long long int)iValue); |
| 1168 | } |
| 1169 | |
danielk1977 | e3209e4 | 2004-05-20 01:40:18 +0000 | [diff] [blame] | 1170 | /* |
| 1171 | ** Bind a double (real) to an SQL statement variable. |
| 1172 | */ |
danielk1977 | 54db47e | 2004-05-19 10:36:43 +0000 | [diff] [blame] | 1173 | int sqlite3_bind_double(sqlite3_stmt *p, int i, double iValue){ |
| 1174 | int rc; |
| 1175 | Vdbe *v = (Vdbe *)p; |
| 1176 | rc = vdbeUnbind(v, i); |
| 1177 | if( rc==SQLITE_OK ){ |
drh | 5a12e68 | 2004-05-19 11:24:25 +0000 | [diff] [blame] | 1178 | Mem *pVar = &v->apVar[i-1]; |
danielk1977 | 54db47e | 2004-05-19 10:36:43 +0000 | [diff] [blame] | 1179 | pVar->flags = MEM_Real; |
| 1180 | pVar->r = iValue; |
| 1181 | } |
| 1182 | return SQLITE_OK; |
| 1183 | } |
| 1184 | |
danielk1977 | e3209e4 | 2004-05-20 01:40:18 +0000 | [diff] [blame] | 1185 | /* |
| 1186 | ** Bind a NULL value to an SQL statement variable. |
| 1187 | */ |
danielk1977 | 54db47e | 2004-05-19 10:36:43 +0000 | [diff] [blame] | 1188 | int sqlite3_bind_null(sqlite3_stmt* p, int i){ |
| 1189 | return vdbeUnbind((Vdbe *)p, i); |
| 1190 | } |
| 1191 | |
danielk1977 | e3209e4 | 2004-05-20 01:40:18 +0000 | [diff] [blame] | 1192 | /* |
| 1193 | ** Bind a UTF-8 text value to an SQL statement variable. |
| 1194 | */ |
danielk1977 | 54db47e | 2004-05-19 10:36:43 +0000 | [diff] [blame] | 1195 | int sqlite3_bind_text( |
| 1196 | sqlite3_stmt *p, |
| 1197 | int i, |
| 1198 | const char *zData, |
| 1199 | int nData, |
| 1200 | int eCopy |
| 1201 | ){ |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 1202 | int flags = MEM_Str|MEM_Utf8; |
| 1203 | if( zData ){ |
| 1204 | if( nData<0 ){ |
| 1205 | nData = strlen(zData)+1; |
| 1206 | flags |= MEM_Term; |
| 1207 | }else if( !zData[nData-1] ){ |
| 1208 | flags |= MEM_Term; |
| 1209 | } |
danielk1977 | 54db47e | 2004-05-19 10:36:43 +0000 | [diff] [blame] | 1210 | } |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 1211 | return vdbeBindBlob((Vdbe *)p, i, zData, nData, eCopy, flags); |
danielk1977 | 54db47e | 2004-05-19 10:36:43 +0000 | [diff] [blame] | 1212 | } |
| 1213 | |
danielk1977 | e3209e4 | 2004-05-20 01:40:18 +0000 | [diff] [blame] | 1214 | /* |
| 1215 | ** Bind a UTF-16 text value to an SQL statement variable. |
| 1216 | */ |
danielk1977 | 54db47e | 2004-05-19 10:36:43 +0000 | [diff] [blame] | 1217 | int sqlite3_bind_text16( |
| 1218 | sqlite3_stmt *p, |
| 1219 | int i, |
| 1220 | const void *zData, |
| 1221 | int nData, |
| 1222 | int eCopy |
| 1223 | ){ |
danielk1977 | b1bc953 | 2004-05-22 03:05:33 +0000 | [diff] [blame] | 1224 | int flags; |
| 1225 | |
| 1226 | if( SQLITE3_BIGENDIAN ){ |
| 1227 | flags = MEM_Str|MEM_Utf16be; |
| 1228 | }else{ |
| 1229 | flags = MEM_Str|MEM_Utf16le; |
| 1230 | } |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 1231 | |
| 1232 | if( zData ){ |
| 1233 | /* If nData is less than zero, measure the length of the string. |
| 1234 | ** manually. In this case the variable will always be null terminated. |
| 1235 | */ |
| 1236 | if( nData<0 ){ |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 1237 | nData = sqlite3utf16ByteLen(zData, -1) + 2; |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 1238 | flags |= MEM_Term; |
| 1239 | }else{ |
| 1240 | /* If nData is greater than zero, check if the final character appears |
| 1241 | ** to be a terminator. |
| 1242 | */ |
| 1243 | if( !(((u8 *)zData)[nData-1]) && !(((u8 *)zData)[nData-2]) ){ |
| 1244 | flags |= MEM_Term; |
| 1245 | } |
| 1246 | } |
| 1247 | } |
danielk1977 | 54db47e | 2004-05-19 10:36:43 +0000 | [diff] [blame] | 1248 | |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 1249 | return vdbeBindBlob((Vdbe *)p, i, zData, nData, eCopy, flags); |
danielk1977 | 54db47e | 2004-05-19 10:36:43 +0000 | [diff] [blame] | 1250 | } |
| 1251 | |
danielk1977 | e3209e4 | 2004-05-20 01:40:18 +0000 | [diff] [blame] | 1252 | /* |
| 1253 | ** Bind a blob value to an SQL statement variable. |
| 1254 | */ |
danielk1977 | 54db47e | 2004-05-19 10:36:43 +0000 | [diff] [blame] | 1255 | int sqlite3_bind_blob( |
| 1256 | sqlite3_stmt *p, |
| 1257 | int i, |
| 1258 | const void *zData, |
| 1259 | int nData, |
| 1260 | int eCopy |
| 1261 | ){ |
| 1262 | return vdbeBindBlob((Vdbe *)p, i, zData, nData, eCopy, MEM_Blob); |
| 1263 | } |
| 1264 | |
| 1265 | /* |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 1266 | ** Set the values of all variables. Variable $1 in the original SQL will |
| 1267 | ** be the string azValue[0]. $2 will have the value azValue[1]. And |
| 1268 | ** so forth. If a value is out of range (for example $3 when nValue==2) |
| 1269 | ** then its value will be NULL. |
| 1270 | ** |
| 1271 | ** This routine overrides any prior call. |
| 1272 | */ |
danielk1977 | 132872b | 2004-05-10 10:37:18 +0000 | [diff] [blame] | 1273 | int sqlite3_bind(sqlite_vm *pVm, int i, const char *zVal, int len, int copy){ |
danielk1977 | 54db47e | 2004-05-19 10:36:43 +0000 | [diff] [blame] | 1274 | return sqlite3_bind_text(pVm, i, zVal, len, copy); |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 1275 | } |
| 1276 | |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 1277 | /* |
| 1278 | ** Delete an entire VDBE. |
| 1279 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1280 | void sqlite3VdbeDelete(Vdbe *p){ |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 1281 | int i; |
| 1282 | if( p==0 ) return; |
| 1283 | Cleanup(p); |
| 1284 | if( p->pPrev ){ |
| 1285 | p->pPrev->pNext = p->pNext; |
| 1286 | }else{ |
| 1287 | assert( p->db->pVdbe==p ); |
| 1288 | p->db->pVdbe = p->pNext; |
| 1289 | } |
| 1290 | if( p->pNext ){ |
| 1291 | p->pNext->pPrev = p->pPrev; |
| 1292 | } |
| 1293 | p->pPrev = p->pNext = 0; |
| 1294 | if( p->nOpAlloc==0 ){ |
| 1295 | p->aOp = 0; |
| 1296 | p->nOp = 0; |
| 1297 | } |
| 1298 | for(i=0; i<p->nOp; i++){ |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 1299 | Op *pOp = &p->aOp[i]; |
| 1300 | if( pOp->p3type==P3_DYNAMIC || pOp->p3type==P3_KEYINFO ){ |
| 1301 | sqliteFree(pOp->p3); |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 1302 | } |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 1303 | #ifndef NDEBUG |
| 1304 | sqliteFree(pOp->zComment); |
| 1305 | #endif |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 1306 | } |
drh | 7c972de | 2003-09-06 22:18:07 +0000 | [diff] [blame] | 1307 | for(i=0; i<p->nVar; i++){ |
drh | 5a12e68 | 2004-05-19 11:24:25 +0000 | [diff] [blame] | 1308 | if( p->apVar[i].flags&MEM_Dyn ){ |
| 1309 | sqliteFree(p->apVar[i].z); |
danielk1977 | 54db47e | 2004-05-19 10:36:43 +0000 | [diff] [blame] | 1310 | } |
drh | 7c972de | 2003-09-06 22:18:07 +0000 | [diff] [blame] | 1311 | } |
danielk1977 | ca6b291 | 2004-05-21 10:49:47 +0000 | [diff] [blame] | 1312 | if( p->azColName16 ){ |
| 1313 | for(i=0; i<p->nResColumn; i++){ |
| 1314 | if( p->azColName16[i] ) sqliteFree(p->azColName16[i]); |
| 1315 | } |
| 1316 | sqliteFree(p->azColName16); |
| 1317 | } |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 1318 | sqliteFree(p->aOp); |
| 1319 | sqliteFree(p->aLabel); |
| 1320 | sqliteFree(p->aStack); |
| 1321 | p->magic = VDBE_MAGIC_DEAD; |
| 1322 | sqliteFree(p); |
| 1323 | } |
drh | a11846b | 2004-01-07 18:52:56 +0000 | [diff] [blame] | 1324 | |
| 1325 | /* |
drh | a11846b | 2004-01-07 18:52:56 +0000 | [diff] [blame] | 1326 | ** If a MoveTo operation is pending on the given cursor, then do that |
| 1327 | ** MoveTo now. Return an error code. If no MoveTo is pending, this |
| 1328 | ** routine does nothing and returns SQLITE_OK. |
| 1329 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1330 | int sqlite3VdbeCursorMoveto(Cursor *p){ |
drh | a11846b | 2004-01-07 18:52:56 +0000 | [diff] [blame] | 1331 | if( p->deferredMoveto ){ |
| 1332 | int res; |
danielk1977 | 132872b | 2004-05-10 10:37:18 +0000 | [diff] [blame] | 1333 | extern int sqlite3_search_count; |
drh | a3b321d | 2004-05-11 09:31:31 +0000 | [diff] [blame] | 1334 | assert( p->intKey ); |
danielk1977 | 6490beb | 2004-05-11 06:17:21 +0000 | [diff] [blame] | 1335 | if( p->intKey ){ |
| 1336 | sqlite3BtreeMoveto(p->pCursor, 0, p->movetoTarget, &res); |
| 1337 | }else{ |
| 1338 | sqlite3BtreeMoveto(p->pCursor,(char*)&p->movetoTarget,sizeof(i64),&res); |
| 1339 | } |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 1340 | *p->pIncrKey = 0; |
drh | a11846b | 2004-01-07 18:52:56 +0000 | [diff] [blame] | 1341 | p->lastRecno = keyToInt(p->movetoTarget); |
| 1342 | p->recnoIsValid = res==0; |
| 1343 | if( res<0 ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1344 | sqlite3BtreeNext(p->pCursor, &res); |
drh | a11846b | 2004-01-07 18:52:56 +0000 | [diff] [blame] | 1345 | } |
danielk1977 | 132872b | 2004-05-10 10:37:18 +0000 | [diff] [blame] | 1346 | sqlite3_search_count++; |
drh | a11846b | 2004-01-07 18:52:56 +0000 | [diff] [blame] | 1347 | p->deferredMoveto = 0; |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 1348 | p->cacheValid = 0; |
drh | a11846b | 2004-01-07 18:52:56 +0000 | [diff] [blame] | 1349 | } |
| 1350 | return SQLITE_OK; |
| 1351 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1352 | |
drh | ab9f7f1 | 2004-05-08 10:56:11 +0000 | [diff] [blame] | 1353 | /* |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1354 | ** The following functions: |
danielk1977 | 90e4d95 | 2004-05-10 10:05:53 +0000 | [diff] [blame] | 1355 | ** |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1356 | ** sqlite3VdbeSerialType() |
| 1357 | ** sqlite3VdbeSerialTypeLen() |
| 1358 | ** sqlite3VdbeSerialRead() |
danielk1977 | 90e4d95 | 2004-05-10 10:05:53 +0000 | [diff] [blame] | 1359 | ** sqlite3VdbeSerialLen() |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1360 | ** sqlite3VdbeSerialWrite() |
danielk1977 | 90e4d95 | 2004-05-10 10:05:53 +0000 | [diff] [blame] | 1361 | ** |
| 1362 | ** encapsulate the code that serializes values for storage in SQLite |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1363 | ** data and index records. Each serialized value consists of a |
| 1364 | ** 'serial-type' and a blob of data. The serial type is an 8-byte unsigned |
| 1365 | ** integer, stored as a varint. |
danielk1977 | 90e4d95 | 2004-05-10 10:05:53 +0000 | [diff] [blame] | 1366 | ** |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1367 | ** In an SQLite index record, the serial type is stored directly before |
| 1368 | ** the blob of data that it corresponds to. In a table record, all serial |
| 1369 | ** types are stored at the start of the record, and the blobs of data at |
| 1370 | ** the end. Hence these functions allow the caller to handle the |
| 1371 | ** serial-type and data blob seperately. |
| 1372 | ** |
| 1373 | ** The following table describes the various storage classes for data: |
| 1374 | ** |
| 1375 | ** serial type bytes of data type |
danielk1977 | 90e4d95 | 2004-05-10 10:05:53 +0000 | [diff] [blame] | 1376 | ** -------------- --------------- --------------- |
danielk1977 | 183f9f7 | 2004-05-13 05:20:26 +0000 | [diff] [blame] | 1377 | ** 0 - Not a type. |
danielk1977 | 90e4d95 | 2004-05-10 10:05:53 +0000 | [diff] [blame] | 1378 | ** 1 1 signed integer |
| 1379 | ** 2 2 signed integer |
| 1380 | ** 3 4 signed integer |
| 1381 | ** 4 8 signed integer |
| 1382 | ** 5 8 IEEE float |
danielk1977 | 183f9f7 | 2004-05-13 05:20:26 +0000 | [diff] [blame] | 1383 | ** 6 0 NULL |
| 1384 | ** 7..11 reserved for expansion |
danielk1977 | 90e4d95 | 2004-05-10 10:05:53 +0000 | [diff] [blame] | 1385 | ** N>=12 and even (N-12)/2 BLOB |
| 1386 | ** N>=13 and odd (N-13)/2 text |
| 1387 | ** |
| 1388 | */ |
| 1389 | |
| 1390 | /* |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1391 | ** Return the serial-type for the value stored in pMem. |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 1392 | */ |
danielk1977 | b1bc953 | 2004-05-22 03:05:33 +0000 | [diff] [blame] | 1393 | u64 sqlite3VdbeSerialType(Mem *pMem){ |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1394 | int flags = pMem->flags; |
| 1395 | |
| 1396 | if( flags&MEM_Null ){ |
danielk1977 | 183f9f7 | 2004-05-13 05:20:26 +0000 | [diff] [blame] | 1397 | return 6; |
danielk1977 | 90e4d95 | 2004-05-10 10:05:53 +0000 | [diff] [blame] | 1398 | } |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1399 | if( flags&MEM_Int ){ |
| 1400 | /* Figure out whether to use 1, 2, 4 or 8 bytes. */ |
| 1401 | i64 i = pMem->i; |
| 1402 | if( i>=-127 && i<=127 ) return 1; |
| 1403 | if( i>=-32767 && i<=32767 ) return 2; |
| 1404 | if( i>=-2147483647 && i<=2147483647 ) return 3; |
| 1405 | return 4; |
danielk1977 | 90e4d95 | 2004-05-10 10:05:53 +0000 | [diff] [blame] | 1406 | } |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1407 | if( flags&MEM_Real ){ |
| 1408 | return 5; |
danielk1977 | 90e4d95 | 2004-05-10 10:05:53 +0000 | [diff] [blame] | 1409 | } |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1410 | if( flags&MEM_Str ){ |
danielk1977 | b1bc953 | 2004-05-22 03:05:33 +0000 | [diff] [blame] | 1411 | u64 t; |
danielk1977 | eb015e0 | 2004-05-18 01:31:14 +0000 | [diff] [blame] | 1412 | assert( pMem->n>0 ); |
danielk1977 | b1bc953 | 2004-05-22 03:05:33 +0000 | [diff] [blame] | 1413 | t = (pMem->n*2) + 13; |
| 1414 | if( pMem->flags&MEM_Term ){ |
| 1415 | t -= ((pMem->flags&MEM_Utf8)?2:4); |
| 1416 | } |
| 1417 | return t; |
danielk1977 | 90e4d95 | 2004-05-10 10:05:53 +0000 | [diff] [blame] | 1418 | } |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1419 | if( flags&MEM_Blob ){ |
| 1420 | return (pMem->n*2 + 12); |
| 1421 | } |
| 1422 | return 0; |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 1423 | } |
| 1424 | |
| 1425 | /* |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1426 | ** Return the length of the data corresponding to the supplied serial-type. |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 1427 | */ |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1428 | int sqlite3VdbeSerialTypeLen(u64 serial_type){ |
danielk1977 | 183f9f7 | 2004-05-13 05:20:26 +0000 | [diff] [blame] | 1429 | assert( serial_type!=0 ); |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1430 | switch(serial_type){ |
danielk1977 | 183f9f7 | 2004-05-13 05:20:26 +0000 | [diff] [blame] | 1431 | case 6: return 0; /* NULL */ |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1432 | case 1: return 1; /* 1 byte integer */ |
| 1433 | case 2: return 2; /* 2 byte integer */ |
| 1434 | case 3: return 4; /* 4 byte integer */ |
| 1435 | case 4: return 8; /* 8 byte integer */ |
| 1436 | case 5: return 8; /* 8 byte float */ |
danielk1977 | 90e4d95 | 2004-05-10 10:05:53 +0000 | [diff] [blame] | 1437 | } |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1438 | assert( serial_type>=12 ); |
| 1439 | return ((serial_type-12)>>1); /* text or blob */ |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 1440 | } |
| 1441 | |
| 1442 | /* |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1443 | ** Write the serialized data blob for the value stored in pMem into |
| 1444 | ** buf. It is assumed that the caller has allocated sufficient space. |
| 1445 | ** Return the number of bytes written. |
| 1446 | */ |
danielk1977 | b1bc953 | 2004-05-22 03:05:33 +0000 | [diff] [blame] | 1447 | int sqlite3VdbeSerialPut(unsigned char *buf, Mem *pMem){ |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1448 | u64 serial_type = sqlite3VdbeSerialType(pMem); |
| 1449 | int len; |
danielk1977 | 183f9f7 | 2004-05-13 05:20:26 +0000 | [diff] [blame] | 1450 | |
| 1451 | assert( serial_type!=0 ); |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1452 | |
| 1453 | /* NULL */ |
danielk1977 | 183f9f7 | 2004-05-13 05:20:26 +0000 | [diff] [blame] | 1454 | if( serial_type==6 ){ |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1455 | return 0; |
| 1456 | } |
| 1457 | |
drh | 1483e14 | 2004-05-21 21:12:42 +0000 | [diff] [blame] | 1458 | /* Integer and Real */ |
| 1459 | if( serial_type<=5 ){ |
| 1460 | u64 v; |
| 1461 | int i; |
| 1462 | if( serial_type==5 ){ |
| 1463 | v = *(u64*)&pMem->r; |
| 1464 | }else{ |
| 1465 | v = *(u64*)&pMem->i; |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1466 | } |
drh | 1483e14 | 2004-05-21 21:12:42 +0000 | [diff] [blame] | 1467 | len = i = sqlite3VdbeSerialTypeLen(serial_type); |
| 1468 | while( i-- ){ |
| 1469 | buf[i] = (v&0xFF); |
| 1470 | v >>= 8; |
| 1471 | } |
| 1472 | return len; |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1473 | } |
| 1474 | |
| 1475 | /* String or blob */ |
| 1476 | assert( serial_type>=12 ); |
| 1477 | len = sqlite3VdbeSerialTypeLen(serial_type); |
| 1478 | memcpy(buf, pMem->z, len); |
| 1479 | return len; |
| 1480 | } |
| 1481 | |
| 1482 | /* |
| 1483 | ** Deserialize the data blob pointed to by buf as serial type serial_type |
| 1484 | ** and store the result in pMem. Return the number of bytes read. |
| 1485 | */ |
danielk1977 | b1bc953 | 2004-05-22 03:05:33 +0000 | [diff] [blame] | 1486 | int sqlite3VdbeSerialGet( |
| 1487 | const unsigned char *buf, |
| 1488 | u64 serial_type, |
| 1489 | Mem *pMem, |
| 1490 | u8 enc |
| 1491 | ){ |
danielk1977 | 90e4d95 | 2004-05-10 10:05:53 +0000 | [diff] [blame] | 1492 | int len; |
| 1493 | |
danielk1977 | 183f9f7 | 2004-05-13 05:20:26 +0000 | [diff] [blame] | 1494 | assert( serial_type!=0 ); |
| 1495 | |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1496 | /* memset(pMem, 0, sizeof(pMem)); */ |
| 1497 | pMem->flags = 0; |
| 1498 | pMem->z = 0; |
danielk1977 | 90e4d95 | 2004-05-10 10:05:53 +0000 | [diff] [blame] | 1499 | |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1500 | /* NULL */ |
danielk1977 | 183f9f7 | 2004-05-13 05:20:26 +0000 | [diff] [blame] | 1501 | if( serial_type==6 ){ |
danielk1977 | 90e4d95 | 2004-05-10 10:05:53 +0000 | [diff] [blame] | 1502 | pMem->flags = MEM_Null; |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1503 | return 0; |
danielk1977 | 90e4d95 | 2004-05-10 10:05:53 +0000 | [diff] [blame] | 1504 | } |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1505 | |
drh | 1483e14 | 2004-05-21 21:12:42 +0000 | [diff] [blame] | 1506 | /* Integer and Real */ |
| 1507 | if( serial_type<=5 ){ |
| 1508 | u64 v = 0; |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1509 | int n; |
| 1510 | len = sqlite3VdbeSerialTypeLen(serial_type); |
danielk1977 | 90e4d95 | 2004-05-10 10:05:53 +0000 | [diff] [blame] | 1511 | |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1512 | if( buf[0]&0x80 ){ |
drh | 1483e14 | 2004-05-21 21:12:42 +0000 | [diff] [blame] | 1513 | v = -1; |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1514 | } |
| 1515 | for(n=0; n<len; n++){ |
drh | 1483e14 | 2004-05-21 21:12:42 +0000 | [diff] [blame] | 1516 | v = (v<<8) | buf[n]; |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1517 | } |
drh | 1483e14 | 2004-05-21 21:12:42 +0000 | [diff] [blame] | 1518 | if( serial_type==5 ){ |
| 1519 | pMem->flags = MEM_Real; |
| 1520 | pMem->r = *(double*)&v; |
| 1521 | }else{ |
| 1522 | pMem->flags = MEM_Int; |
danielk1977 | b1bc953 | 2004-05-22 03:05:33 +0000 | [diff] [blame] | 1523 | pMem->i = *(i64*)&v; |
drh | 1483e14 | 2004-05-21 21:12:42 +0000 | [diff] [blame] | 1524 | } |
| 1525 | return len; |
danielk1977 | 90e4d95 | 2004-05-10 10:05:53 +0000 | [diff] [blame] | 1526 | } |
| 1527 | |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1528 | /* String or blob */ |
| 1529 | assert( serial_type>=12 ); |
danielk1977 | eb015e0 | 2004-05-18 01:31:14 +0000 | [diff] [blame] | 1530 | len = sqlite3VdbeSerialTypeLen(serial_type); |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1531 | if( serial_type&0x01 ){ |
danielk1977 | b1bc953 | 2004-05-22 03:05:33 +0000 | [diff] [blame] | 1532 | switch( enc ){ |
| 1533 | case TEXT_Utf8: |
| 1534 | pMem->flags = MEM_Str|MEM_Utf8|MEM_Term; |
| 1535 | break; |
| 1536 | case TEXT_Utf16le: |
| 1537 | pMem->flags = MEM_Str|MEM_Utf16le|MEM_Term; |
| 1538 | break; |
| 1539 | case TEXT_Utf16be: |
| 1540 | pMem->flags = MEM_Str|MEM_Utf16be|MEM_Term; |
| 1541 | break; |
| 1542 | assert(0); |
| 1543 | } |
| 1544 | pMem->n = len+(enc==TEXT_Utf8?1:2); |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1545 | }else{ |
| 1546 | pMem->flags = MEM_Blob; |
danielk1977 | eb015e0 | 2004-05-18 01:31:14 +0000 | [diff] [blame] | 1547 | pMem->n = len; |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1548 | } |
danielk1977 | eb015e0 | 2004-05-18 01:31:14 +0000 | [diff] [blame] | 1549 | |
| 1550 | if( (pMem->n)>NBFS ){ |
| 1551 | pMem->z = sqliteMallocRaw( pMem->n ); |
danielk1977 | 90e4d95 | 2004-05-10 10:05:53 +0000 | [diff] [blame] | 1552 | if( !pMem->z ){ |
| 1553 | return -1; |
| 1554 | } |
| 1555 | pMem->flags |= MEM_Dyn; |
| 1556 | }else{ |
| 1557 | pMem->z = pMem->zShort; |
| 1558 | pMem->flags |= MEM_Short; |
| 1559 | } |
danielk1977 | eb015e0 | 2004-05-18 01:31:14 +0000 | [diff] [blame] | 1560 | |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1561 | memcpy(pMem->z, buf, len); |
danielk1977 | eb015e0 | 2004-05-18 01:31:14 +0000 | [diff] [blame] | 1562 | if( pMem->flags&MEM_Str ){ |
| 1563 | pMem->z[len] = '\0'; |
danielk1977 | b1bc953 | 2004-05-22 03:05:33 +0000 | [diff] [blame] | 1564 | if( enc!=TEXT_Utf8 ){ |
| 1565 | pMem->z[len+1] = '\0'; |
| 1566 | } |
danielk1977 | eb015e0 | 2004-05-18 01:31:14 +0000 | [diff] [blame] | 1567 | } |
danielk1977 | 90e4d95 | 2004-05-10 10:05:53 +0000 | [diff] [blame] | 1568 | |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1569 | return len; |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 1570 | } |
| 1571 | |
| 1572 | /* |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 1573 | ** Compare the values contained by the two memory cells, returning |
| 1574 | ** negative, zero or positive if pMem1 is less than, equal to, or greater |
| 1575 | ** than pMem2. Sorting order is NULL's first, followed by numbers (integers |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 1576 | ** and reals) sorted numerically, followed by text ordered by the collating |
| 1577 | ** sequence pColl and finally blob's ordered by memcmp(). |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 1578 | ** |
| 1579 | ** Two NULL values are considered equal by this function. |
| 1580 | */ |
drh | 53db145 | 2004-05-20 13:54:53 +0000 | [diff] [blame] | 1581 | int sqlite3MemCompare(const Mem *pMem1, const Mem *pMem2, const CollSeq *pColl){ |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 1582 | int rc; |
drh | 53db145 | 2004-05-20 13:54:53 +0000 | [diff] [blame] | 1583 | int f1, f2; |
| 1584 | int combined_flags; |
| 1585 | |
| 1586 | /* Interchange pMem1 and pMem2 if the collating sequence specifies |
| 1587 | ** DESC order. |
| 1588 | */ |
drh | 53db145 | 2004-05-20 13:54:53 +0000 | [diff] [blame] | 1589 | f1 = pMem1->flags; |
| 1590 | f2 = pMem2->flags; |
| 1591 | combined_flags = f1|f2; |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 1592 | |
| 1593 | /* If one value is NULL, it is less than the other. If both values |
| 1594 | ** are NULL, return 0. |
| 1595 | */ |
| 1596 | if( combined_flags&MEM_Null ){ |
drh | 53db145 | 2004-05-20 13:54:53 +0000 | [diff] [blame] | 1597 | return (f2&MEM_Null) - (f1&MEM_Null); |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 1598 | } |
| 1599 | |
| 1600 | /* If one value is a number and the other is not, the number is less. |
| 1601 | ** If both are numbers, compare as reals if one is a real, or as integers |
| 1602 | ** if both values are integers. |
| 1603 | */ |
| 1604 | if( combined_flags&(MEM_Int|MEM_Real) ){ |
drh | 53db145 | 2004-05-20 13:54:53 +0000 | [diff] [blame] | 1605 | if( !(f1&(MEM_Int|MEM_Real)) ){ |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 1606 | return 1; |
| 1607 | } |
drh | 53db145 | 2004-05-20 13:54:53 +0000 | [diff] [blame] | 1608 | if( !(f2&(MEM_Int|MEM_Real)) ){ |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 1609 | return -1; |
| 1610 | } |
drh | 53db145 | 2004-05-20 13:54:53 +0000 | [diff] [blame] | 1611 | if( (f1 & f2 & MEM_Int)==0 ){ |
| 1612 | double r1, r2; |
| 1613 | if( (f1&MEM_Real)==0 ){ |
| 1614 | r1 = pMem1->i; |
| 1615 | }else{ |
| 1616 | r1 = pMem1->r; |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 1617 | } |
drh | 53db145 | 2004-05-20 13:54:53 +0000 | [diff] [blame] | 1618 | if( (f2&MEM_Real)==0 ){ |
| 1619 | r2 = pMem2->i; |
| 1620 | }else{ |
| 1621 | r2 = pMem2->r; |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 1622 | } |
drh | 53db145 | 2004-05-20 13:54:53 +0000 | [diff] [blame] | 1623 | if( r1<r2 ) return -1; |
| 1624 | if( r1>r2 ) return 1; |
| 1625 | return 0; |
| 1626 | }else{ |
| 1627 | assert( f1&MEM_Int ); |
| 1628 | assert( f2&MEM_Int ); |
| 1629 | if( pMem1->i < pMem2->i ) return -1; |
| 1630 | if( pMem1->i > pMem2->i ) return 1; |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 1631 | return 0; |
| 1632 | } |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 1633 | } |
| 1634 | |
drh | 53db145 | 2004-05-20 13:54:53 +0000 | [diff] [blame] | 1635 | /* If one value is a string and the other is a blob, the string is less. |
| 1636 | ** If both are strings, compare using the collating functions. |
| 1637 | */ |
| 1638 | if( combined_flags&MEM_Str ){ |
| 1639 | if( (f1 & MEM_Str)==0 ){ |
| 1640 | return 1; |
| 1641 | } |
| 1642 | if( (f2 & MEM_Str)==0 ){ |
| 1643 | return -1; |
| 1644 | } |
| 1645 | if( pColl && pColl->xCmp ){ |
| 1646 | return pColl->xCmp(pColl->pUser, pMem1->n, pMem1->z, pMem2->n, pMem2->z); |
| 1647 | }else{ |
| 1648 | /* If no collating sequence is defined, fall through into the |
| 1649 | ** blob case below and use memcmp() for the comparison. */ |
| 1650 | } |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 1651 | } |
drh | 53db145 | 2004-05-20 13:54:53 +0000 | [diff] [blame] | 1652 | |
| 1653 | /* Both values must be blobs. Compare using memcmp(). |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 1654 | */ |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 1655 | rc = memcmp(pMem1->z, pMem2->z, (pMem1->n>pMem2->n)?pMem2->n:pMem1->n); |
drh | 53db145 | 2004-05-20 13:54:53 +0000 | [diff] [blame] | 1656 | if( rc==0 ){ |
| 1657 | rc = pMem1->n - pMem2->n; |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 1658 | } |
drh | 53db145 | 2004-05-20 13:54:53 +0000 | [diff] [blame] | 1659 | return rc; |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 1660 | } |
| 1661 | |
| 1662 | /* |
drh | ab9f7f1 | 2004-05-08 10:56:11 +0000 | [diff] [blame] | 1663 | ** The following is the comparison function for (non-integer) |
| 1664 | ** keys in the btrees. This function returns negative, zero, or |
| 1665 | ** positive if the first key is less than, equal to, or greater than |
| 1666 | ** the second. |
| 1667 | ** |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 1668 | ** This function assumes that each key consists of one or more type/blob |
danielk1977 | 183f9f7 | 2004-05-13 05:20:26 +0000 | [diff] [blame] | 1669 | ** pairs, encoded using the sqlite3VdbeSerialXXX() functions above. |
| 1670 | ** |
| 1671 | ** Following the type/blob pairs, each key may have a single 0x00 byte |
| 1672 | ** followed by a varint. A key may only have this traling 0x00/varint |
| 1673 | ** pair if it has at least as many type/blob pairs as the key it is being |
| 1674 | ** compared to. |
drh | ab9f7f1 | 2004-05-08 10:56:11 +0000 | [diff] [blame] | 1675 | */ |
drh | ab9f7f1 | 2004-05-08 10:56:11 +0000 | [diff] [blame] | 1676 | int sqlite3VdbeKeyCompare( |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 1677 | void *userData, |
danielk1977 | 183f9f7 | 2004-05-13 05:20:26 +0000 | [diff] [blame] | 1678 | int nKey1, const void *pKey1, |
| 1679 | int nKey2, const void *pKey2 |
drh | ab9f7f1 | 2004-05-08 10:56:11 +0000 | [diff] [blame] | 1680 | ){ |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 1681 | KeyInfo *pKeyInfo = (KeyInfo*)userData; |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 1682 | int offset1 = 0; |
| 1683 | int offset2 = 0; |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 1684 | int i = 0; |
drh | ffbc308 | 2004-05-21 01:29:06 +0000 | [diff] [blame] | 1685 | int rc = 0; |
danielk1977 | b1bc953 | 2004-05-22 03:05:33 +0000 | [diff] [blame] | 1686 | u8 enc = pKeyInfo->enc; |
danielk1977 | 183f9f7 | 2004-05-13 05:20:26 +0000 | [diff] [blame] | 1687 | const unsigned char *aKey1 = (const unsigned char *)pKey1; |
| 1688 | const unsigned char *aKey2 = (const unsigned char *)pKey2; |
| 1689 | |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 1690 | assert( pKeyInfo!=0 ); |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 1691 | while( offset1<nKey1 && offset2<nKey2 ){ |
| 1692 | Mem mem1; |
| 1693 | Mem mem2; |
| 1694 | u64 serial_type1; |
| 1695 | u64 serial_type2; |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 1696 | |
danielk1977 | 183f9f7 | 2004-05-13 05:20:26 +0000 | [diff] [blame] | 1697 | /* Read the serial types for the next element in each key. */ |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 1698 | offset1 += sqlite3GetVarint(&aKey1[offset1], &serial_type1); |
| 1699 | offset2 += sqlite3GetVarint(&aKey2[offset2], &serial_type2); |
danielk1977 | 183f9f7 | 2004-05-13 05:20:26 +0000 | [diff] [blame] | 1700 | |
| 1701 | /* If either of the varints just read in are 0 (not a type), then |
| 1702 | ** this is the end of the keys. The remaining data in each key is |
| 1703 | ** the varint rowid. Compare these as signed integers and return |
| 1704 | ** the result. |
| 1705 | */ |
| 1706 | if( !serial_type1 || !serial_type2 ){ |
| 1707 | assert( !serial_type1 && !serial_type2 ); |
| 1708 | sqlite3GetVarint(&aKey1[offset1], &serial_type1); |
| 1709 | sqlite3GetVarint(&aKey2[offset2], &serial_type2); |
drh | ffbc308 | 2004-05-21 01:29:06 +0000 | [diff] [blame] | 1710 | if( serial_type1 < serial_type2 ){ |
| 1711 | rc = -1; |
| 1712 | }else if( serial_type1 > serial_type2 ){ |
| 1713 | rc = +1; |
| 1714 | }else{ |
| 1715 | rc = 0; |
| 1716 | } |
| 1717 | return rc; |
danielk1977 | 183f9f7 | 2004-05-13 05:20:26 +0000 | [diff] [blame] | 1718 | } |
| 1719 | |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 1720 | assert( i<pKeyInfo->nField ); |
| 1721 | |
danielk1977 | 183f9f7 | 2004-05-13 05:20:26 +0000 | [diff] [blame] | 1722 | /* Assert that there is enough space left in each key for the blob of |
| 1723 | ** data to go with the serial type just read. This assert may fail if |
| 1724 | ** the file is corrupted. Then read the value from each key into mem1 |
| 1725 | ** and mem2 respectively. |
| 1726 | */ |
danielk1977 | b1bc953 | 2004-05-22 03:05:33 +0000 | [diff] [blame] | 1727 | offset1 += sqlite3VdbeSerialGet(&aKey1[offset1], serial_type1, &mem1, enc); |
| 1728 | offset2 += sqlite3VdbeSerialGet(&aKey2[offset2], serial_type2, &mem2, enc); |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 1729 | |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 1730 | rc = sqlite3MemCompare(&mem1, &mem2, pKeyInfo->aColl[i]); |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 1731 | if( mem1.flags&MEM_Dyn ){ |
| 1732 | sqliteFree(mem1.z); |
| 1733 | } |
| 1734 | if( mem2.flags&MEM_Dyn ){ |
| 1735 | sqliteFree(mem2.z); |
| 1736 | } |
| 1737 | if( rc!=0 ){ |
drh | ffbc308 | 2004-05-21 01:29:06 +0000 | [diff] [blame] | 1738 | break; |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 1739 | } |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 1740 | i++; |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 1741 | } |
| 1742 | |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 1743 | /* One of the keys ran out of fields, but all the fields up to that point |
| 1744 | ** were equal. If the incrKey flag is true, then the second key is |
| 1745 | ** treated as larger. |
| 1746 | */ |
drh | ffbc308 | 2004-05-21 01:29:06 +0000 | [diff] [blame] | 1747 | if( rc==0 ){ |
| 1748 | if( pKeyInfo->incrKey ){ |
| 1749 | assert( offset2==nKey2 ); |
| 1750 | rc = -1; |
| 1751 | }else if( offset1<nKey1 ){ |
| 1752 | rc = 1; |
| 1753 | }else if( offset2<nKey2 ){ |
| 1754 | rc = -1; |
| 1755 | } |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 1756 | } |
| 1757 | |
drh | ffbc308 | 2004-05-21 01:29:06 +0000 | [diff] [blame] | 1758 | if( pKeyInfo->aSortOrder && i<pKeyInfo->nField && pKeyInfo->aSortOrder[i] ){ |
| 1759 | rc = -rc; |
drh | ab9f7f1 | 2004-05-08 10:56:11 +0000 | [diff] [blame] | 1760 | } |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 1761 | |
drh | ffbc308 | 2004-05-21 01:29:06 +0000 | [diff] [blame] | 1762 | return rc; |
drh | ab9f7f1 | 2004-05-08 10:56:11 +0000 | [diff] [blame] | 1763 | } |
danielk1977 | 183f9f7 | 2004-05-13 05:20:26 +0000 | [diff] [blame] | 1764 | |
| 1765 | /* |
danielk1977 | eb015e0 | 2004-05-18 01:31:14 +0000 | [diff] [blame] | 1766 | ** This function compares the two table row records specified by |
| 1767 | ** {nKey1, pKey1} and {nKey2, pKey2}, returning a negative, zero |
| 1768 | ** or positive integer if {nKey1, pKey1} is less than, equal to or |
| 1769 | ** greater than {nKey2, pKey2}. |
| 1770 | ** |
danielk1977 | 84ac9d0 | 2004-05-18 09:58:06 +0000 | [diff] [blame] | 1771 | ** This function is pretty inefficient and will probably be replaced |
danielk1977 | eb015e0 | 2004-05-18 01:31:14 +0000 | [diff] [blame] | 1772 | ** by something else in the near future. It is currently required |
| 1773 | ** by compound SELECT operators. |
| 1774 | */ |
| 1775 | int sqlite3VdbeRowCompare( |
| 1776 | void *userData, |
| 1777 | int nKey1, const void *pKey1, |
| 1778 | int nKey2, const void *pKey2 |
| 1779 | ){ |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 1780 | KeyInfo *pKeyInfo = (KeyInfo*)userData; |
danielk1977 | eb015e0 | 2004-05-18 01:31:14 +0000 | [diff] [blame] | 1781 | int offset1 = 0; |
| 1782 | int offset2 = 0; |
danielk1977 | 84ac9d0 | 2004-05-18 09:58:06 +0000 | [diff] [blame] | 1783 | int toffset1 = 0; |
| 1784 | int toffset2 = 0; |
| 1785 | int i; |
danielk1977 | b1bc953 | 2004-05-22 03:05:33 +0000 | [diff] [blame] | 1786 | u8 enc = pKeyInfo->enc; |
danielk1977 | eb015e0 | 2004-05-18 01:31:14 +0000 | [diff] [blame] | 1787 | const unsigned char *aKey1 = (const unsigned char *)pKey1; |
| 1788 | const unsigned char *aKey2 = (const unsigned char *)pKey2; |
| 1789 | |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 1790 | assert( pKeyInfo ); |
| 1791 | assert( pKeyInfo->nField>0 ); |
danielk1977 | 84ac9d0 | 2004-05-18 09:58:06 +0000 | [diff] [blame] | 1792 | |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 1793 | for( i=0; i<pKeyInfo->nField; i++ ){ |
danielk1977 | 84ac9d0 | 2004-05-18 09:58:06 +0000 | [diff] [blame] | 1794 | u64 dummy; |
| 1795 | offset1 += sqlite3GetVarint(&aKey1[offset1], &dummy); |
| 1796 | offset2 += sqlite3GetVarint(&aKey1[offset1], &dummy); |
| 1797 | } |
| 1798 | |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 1799 | for( i=0; i<pKeyInfo->nField; i++ ){ |
danielk1977 | 84ac9d0 | 2004-05-18 09:58:06 +0000 | [diff] [blame] | 1800 | Mem mem1; |
| 1801 | Mem mem2; |
| 1802 | u64 serial_type1; |
| 1803 | u64 serial_type2; |
| 1804 | int rc; |
| 1805 | |
| 1806 | /* Read the serial types for the next element in each key. */ |
| 1807 | toffset1 += sqlite3GetVarint(&aKey1[toffset1], &serial_type1); |
| 1808 | toffset2 += sqlite3GetVarint(&aKey2[toffset2], &serial_type2); |
| 1809 | |
| 1810 | assert( serial_type1 && serial_type2 ); |
| 1811 | |
| 1812 | /* Assert that there is enough space left in each key for the blob of |
| 1813 | ** data to go with the serial type just read. This assert may fail if |
| 1814 | ** the file is corrupted. Then read the value from each key into mem1 |
| 1815 | ** and mem2 respectively. |
| 1816 | */ |
danielk1977 | b1bc953 | 2004-05-22 03:05:33 +0000 | [diff] [blame] | 1817 | offset1 += sqlite3VdbeSerialGet(&aKey1[offset1], serial_type1, &mem1, enc); |
| 1818 | offset2 += sqlite3VdbeSerialGet(&aKey2[offset2], serial_type2, &mem2, enc); |
danielk1977 | 84ac9d0 | 2004-05-18 09:58:06 +0000 | [diff] [blame] | 1819 | |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 1820 | rc = sqlite3MemCompare(&mem1, &mem2, pKeyInfo->aColl[i]); |
danielk1977 | 84ac9d0 | 2004-05-18 09:58:06 +0000 | [diff] [blame] | 1821 | if( mem1.flags&MEM_Dyn ){ |
| 1822 | sqliteFree(mem1.z); |
| 1823 | } |
| 1824 | if( mem2.flags&MEM_Dyn ){ |
| 1825 | sqliteFree(mem2.z); |
| 1826 | } |
| 1827 | if( rc!=0 ){ |
| 1828 | return rc; |
| 1829 | } |
| 1830 | } |
| 1831 | |
| 1832 | return 0; |
danielk1977 | eb015e0 | 2004-05-18 01:31:14 +0000 | [diff] [blame] | 1833 | } |
| 1834 | |
| 1835 | |
| 1836 | /* |
danielk1977 | 183f9f7 | 2004-05-13 05:20:26 +0000 | [diff] [blame] | 1837 | ** pCur points at an index entry. Read the rowid (varint occuring at |
| 1838 | ** the end of the entry and store it in *rowid. Return SQLITE_OK if |
| 1839 | ** everything works, or an error code otherwise. |
| 1840 | */ |
| 1841 | int sqlite3VdbeIdxRowid(BtCursor *pCur, i64 *rowid){ |
| 1842 | i64 sz; |
| 1843 | int rc; |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 1844 | char buf[10]; |
danielk1977 | 183f9f7 | 2004-05-13 05:20:26 +0000 | [diff] [blame] | 1845 | int len; |
| 1846 | u64 r; |
| 1847 | |
| 1848 | rc = sqlite3BtreeKeySize(pCur, &sz); |
| 1849 | if( rc!=SQLITE_OK ){ |
| 1850 | return rc; |
| 1851 | } |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 1852 | len = ((sz>10)?10:sz); |
| 1853 | |
| 1854 | /* If there are less than 2 bytes in the key, this cannot be |
| 1855 | ** a valid index entry. In practice this comes up for a query |
| 1856 | ** of the sort "SELECT max(x) FROM t1;" when t1 is an empty table |
| 1857 | ** with an index on x. In this case just call the rowid 0. |
| 1858 | */ |
| 1859 | if( len<2 ){ |
| 1860 | *rowid = 0; |
| 1861 | return SQLITE_OK; |
| 1862 | } |
danielk1977 | 183f9f7 | 2004-05-13 05:20:26 +0000 | [diff] [blame] | 1863 | |
| 1864 | rc = sqlite3BtreeKey(pCur, sz-len, len, buf); |
| 1865 | if( rc!=SQLITE_OK ){ |
| 1866 | return rc; |
| 1867 | } |
| 1868 | |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 1869 | len--; |
| 1870 | while( buf[len-1] && --len ); |
danielk1977 | 183f9f7 | 2004-05-13 05:20:26 +0000 | [diff] [blame] | 1871 | |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 1872 | sqlite3GetVarint(&buf[len], &r); |
danielk1977 | 183f9f7 | 2004-05-13 05:20:26 +0000 | [diff] [blame] | 1873 | *rowid = r; |
| 1874 | return SQLITE_OK; |
| 1875 | } |
| 1876 | |
drh | 7cf6e4d | 2004-05-19 14:56:55 +0000 | [diff] [blame] | 1877 | /* |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 1878 | ** 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] | 1879 | ** the key string in pKey (of length nKey). Write into *pRes a number |
| 1880 | ** that is negative, zero, or positive if pC is less than, equal to, |
| 1881 | ** or greater than pKey. Return SQLITE_OK on success. |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 1882 | ** |
| 1883 | ** pKey might contain fewer terms than the cursor. |
drh | 7cf6e4d | 2004-05-19 14:56:55 +0000 | [diff] [blame] | 1884 | */ |
danielk1977 | 183f9f7 | 2004-05-13 05:20:26 +0000 | [diff] [blame] | 1885 | int sqlite3VdbeIdxKeyCompare( |
drh | 7cf6e4d | 2004-05-19 14:56:55 +0000 | [diff] [blame] | 1886 | Cursor *pC, /* The cursor to compare against */ |
| 1887 | int nKey, const u8 *pKey, /* The key to compare */ |
| 1888 | int *res /* Write the comparison result here */ |
danielk1977 | 183f9f7 | 2004-05-13 05:20:26 +0000 | [diff] [blame] | 1889 | ){ |
| 1890 | unsigned char *pCellKey; |
| 1891 | u64 nCellKey; |
| 1892 | int freeCellKey = 0; |
| 1893 | int rc; |
| 1894 | int len; |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 1895 | BtCursor *pCur = pC->pCursor; |
danielk1977 | 183f9f7 | 2004-05-13 05:20:26 +0000 | [diff] [blame] | 1896 | |
| 1897 | sqlite3BtreeKeySize(pCur, &nCellKey); |
| 1898 | if( nCellKey<=0 ){ |
| 1899 | *res = 0; |
| 1900 | return SQLITE_OK; |
| 1901 | } |
| 1902 | |
| 1903 | pCellKey = (unsigned char *)sqlite3BtreeKeyFetch(pCur, nCellKey); |
| 1904 | if( !pCellKey ){ |
drh | 10617cd | 2004-05-14 15:27:27 +0000 | [diff] [blame] | 1905 | pCellKey = (unsigned char *)sqliteMallocRaw(nCellKey); |
danielk1977 | 183f9f7 | 2004-05-13 05:20:26 +0000 | [diff] [blame] | 1906 | if( !pCellKey ){ |
| 1907 | return SQLITE_NOMEM; |
| 1908 | } |
| 1909 | freeCellKey = 1; |
| 1910 | rc = sqlite3BtreeKey(pCur, 0, nCellKey, pCellKey); |
| 1911 | if( rc!=SQLITE_OK ){ |
| 1912 | sqliteFree(pCellKey); |
| 1913 | return rc; |
| 1914 | } |
| 1915 | } |
| 1916 | |
| 1917 | len = nCellKey-2; |
| 1918 | while( pCellKey[len] && --len ); |
| 1919 | |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 1920 | *res = sqlite3VdbeKeyCompare(pC->pKeyInfo, len, pCellKey, nKey, pKey); |
danielk1977 | 183f9f7 | 2004-05-13 05:20:26 +0000 | [diff] [blame] | 1921 | |
| 1922 | if( freeCellKey ){ |
| 1923 | sqliteFree(pCellKey); |
| 1924 | } |
| 1925 | return SQLITE_OK; |
| 1926 | } |