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