drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1 | /* |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2 | ** 2001 September 15 |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 4 | ** The author disclaims copyright to this source code. In place of |
| 5 | ** a legal notice, here is a blessing: |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 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. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 10 | ** |
| 11 | ************************************************************************* |
| 12 | ** The code in this file implements the Virtual Database Engine (VDBE) |
| 13 | ** |
| 14 | ** The SQL parser generates a program which is then executed by |
| 15 | ** the VDBE to do the work of the SQL statement. VDBE programs are |
| 16 | ** similar in form to assembly language. The program consists of |
| 17 | ** a linear sequence of operations. Each operation has an opcode |
| 18 | ** and 3 operands. Operands P1 and P2 are integers. Operand P3 |
| 19 | ** is a null-terminated string. The P2 operand must be non-negative. |
| 20 | ** Opcodes will typically ignore one or more operands. Many opcodes |
| 21 | ** ignore all three operands. |
| 22 | ** |
| 23 | ** Computation results are stored on a stack. Each entry on the |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 24 | ** stack is either an integer, a null-terminated string, a floating point |
| 25 | ** number, or the SQL "NULL" value. An inplicit conversion from one |
| 26 | ** type to the other occurs as necessary. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 27 | ** |
| 28 | ** Most of the code in this file is taken up by the sqliteVdbeExec() |
| 29 | ** function which does the work of interpreting a VDBE program. |
| 30 | ** But other routines are also provided to help in building up |
| 31 | ** a program instruction by instruction. |
| 32 | ** |
drh | 1eaa269 | 2001-09-18 02:02:23 +0000 | [diff] [blame^] | 33 | ** $Id: vdbe.c,v 1.71 2001/09/18 02:02:23 drh Exp $ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 34 | */ |
| 35 | #include "sqliteInt.h" |
drh | 7c68d60 | 2000-10-11 19:28:51 +0000 | [diff] [blame] | 36 | #include <ctype.h> |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 37 | #include <unistd.h> |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 38 | |
| 39 | /* |
| 40 | ** SQL is translated into a sequence of instructions to be |
| 41 | ** executed by a virtual machine. Each instruction is an instance |
| 42 | ** of the following structure. |
| 43 | */ |
| 44 | typedef struct VdbeOp Op; |
| 45 | |
| 46 | /* |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 47 | ** Boolean values |
| 48 | */ |
| 49 | typedef unsigned char Bool; |
| 50 | |
| 51 | /* |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 52 | ** A cursor is a pointer into a database file. The database file |
| 53 | ** can represent either an SQL table or an SQL index. Each file is |
| 54 | ** a bag of key/data pairs. The cursor can loop over all key/data |
| 55 | ** pairs (in an arbitrary order) or it can retrieve a particular |
| 56 | ** key/data pair given a copy of the key. |
| 57 | ** |
| 58 | ** Every cursor that the virtual machine has open is represented by an |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 59 | ** instance of the following structure. |
| 60 | */ |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 61 | struct Cursor { |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 62 | BtCursor *pCursor; /* The cursor structure of the backend */ |
| 63 | int lastRecno; /* Last recno from a Next or NextIdx operation */ |
| 64 | Bool recnoIsValid; /* True if lastRecno is valid */ |
| 65 | Bool keyAsData; /* The OP_Column command works on key instead of data */ |
| 66 | Bool atFirst; /* True if pointing to first entry */ |
| 67 | Btree *pBt; /* Separate file holding temporary table */ |
| 68 | char *zKey; /* Key used in BeginIdx and NextIdx operators */ |
| 69 | int nKey; /* Number of bytes in zKey[] */ |
| 70 | char *zBuf; /* Buffer space used to hold a copy of zKey[] */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 71 | }; |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 72 | typedef struct Cursor Cursor; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 73 | |
| 74 | /* |
| 75 | ** A sorter builds a list of elements to be sorted. Each element of |
| 76 | ** the list is an instance of the following structure. |
| 77 | */ |
| 78 | typedef struct Sorter Sorter; |
| 79 | struct Sorter { |
| 80 | int nKey; /* Number of bytes in the key */ |
| 81 | char *zKey; /* The key by which we will sort */ |
| 82 | int nData; /* Number of bytes in the data */ |
| 83 | char *pData; /* The data associated with this key */ |
| 84 | Sorter *pNext; /* Next in the list */ |
| 85 | }; |
| 86 | |
| 87 | /* |
| 88 | ** Number of buckets used for merge-sort. |
| 89 | */ |
| 90 | #define NSORT 30 |
| 91 | |
| 92 | /* |
drh | c61053b | 2000-06-04 12:58:36 +0000 | [diff] [blame] | 93 | ** A single level of the stack is an instance of the following |
| 94 | ** structure. Except, string values are stored on a separate |
| 95 | ** list of of pointers to character. The reason for storing |
| 96 | ** strings separately is so that they can be easily passed |
| 97 | ** to the callback function. |
| 98 | */ |
| 99 | struct Stack { |
| 100 | int i; /* Integer value */ |
| 101 | int n; /* Number of characters in string value, including '\0' */ |
| 102 | int flags; /* Some combination of STK_Null, STK_Str, STK_Dyn, etc. */ |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 103 | double r; /* Real value */ |
drh | c61053b | 2000-06-04 12:58:36 +0000 | [diff] [blame] | 104 | }; |
| 105 | typedef struct Stack Stack; |
| 106 | |
| 107 | /* |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 108 | ** Memory cells use the same structure as the stack except that space |
| 109 | ** for an arbitrary string is added. |
| 110 | */ |
| 111 | struct Mem { |
| 112 | Stack s; /* All values of the memory cell besides string */ |
| 113 | char *z; /* String value for this memory cell */ |
| 114 | }; |
| 115 | typedef struct Mem Mem; |
| 116 | |
| 117 | /* |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 118 | ** Allowed values for Stack.flags |
| 119 | */ |
| 120 | #define STK_Null 0x0001 /* Value is NULL */ |
| 121 | #define STK_Str 0x0002 /* Value is a string */ |
| 122 | #define STK_Int 0x0004 /* Value is an integer */ |
| 123 | #define STK_Real 0x0008 /* Value is a real number */ |
| 124 | #define STK_Dyn 0x0010 /* Need to call sqliteFree() on zStack[*] */ |
| 125 | |
| 126 | /* |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 127 | ** An Agg structure describes an Aggregator. Each Agg consists of |
drh | 600b1b2 | 2000-06-05 21:39:48 +0000 | [diff] [blame] | 128 | ** zero or more Aggregator elements (AggElem). Each AggElem contains |
| 129 | ** a key and one or more values. The values are used in processing |
| 130 | ** aggregate functions in a SELECT. The key is used to implement |
| 131 | ** the GROUP BY clause of a select. |
| 132 | */ |
| 133 | typedef struct Agg Agg; |
| 134 | typedef struct AggElem AggElem; |
| 135 | struct Agg { |
| 136 | int nMem; /* Number of values stored in each AggElem */ |
| 137 | AggElem *pCurrent; /* The AggElem currently in focus */ |
| 138 | int nElem; /* The number of AggElems */ |
| 139 | int nHash; /* Number of slots in apHash[] */ |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 140 | AggElem **apHash; /* A hash array for looking up AggElems by zKey */ |
drh | 600b1b2 | 2000-06-05 21:39:48 +0000 | [diff] [blame] | 141 | AggElem *pFirst; /* A list of all AggElems */ |
| 142 | }; |
| 143 | struct AggElem { |
| 144 | char *zKey; /* The key to this AggElem */ |
| 145 | AggElem *pHash; /* Next AggElem with the same hash on zKey */ |
| 146 | AggElem *pNext; /* Next AggElem in a list of them all */ |
| 147 | Mem aMem[1]; /* The values for this AggElem */ |
| 148 | }; |
| 149 | |
| 150 | /* |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 151 | ** A Set structure is used for quick testing to see if a value |
| 152 | ** is part of a small set. Sets are used to implement code like |
| 153 | ** this: |
| 154 | ** x.y IN ('hi','hoo','hum') |
drh | c61053b | 2000-06-04 12:58:36 +0000 | [diff] [blame] | 155 | */ |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 156 | typedef struct Set Set; |
| 157 | typedef struct SetElem SetElem; |
| 158 | struct Set { |
| 159 | SetElem *pAll; /* All elements of this set */ |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 160 | SetElem *apHash[41]; /* A hash array for all elements in this set */ |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 161 | }; |
| 162 | struct SetElem { |
| 163 | SetElem *pHash; /* Next element with the same hash on zKey */ |
| 164 | SetElem *pNext; /* Next element in a list of them all */ |
| 165 | char zKey[1]; /* Value of this key */ |
| 166 | }; |
drh | c61053b | 2000-06-04 12:58:36 +0000 | [diff] [blame] | 167 | |
| 168 | /* |
drh | 0353ced | 2001-03-20 22:05:00 +0000 | [diff] [blame] | 169 | ** A Keylist is a bunch of keys into a table. The keylist can |
| 170 | ** grow without bound. The keylist stores the keys of database |
| 171 | ** records that need to be deleted. |
| 172 | */ |
| 173 | typedef struct Keylist Keylist; |
| 174 | struct Keylist { |
| 175 | int nKey; /* Number of slots in aKey[] */ |
| 176 | int nUsed; /* Next unwritten slot in aKey[] */ |
| 177 | int nRead; /* Next unread slot in aKey[] */ |
| 178 | Keylist *pNext; /* Next block of keys */ |
| 179 | int aKey[1]; /* One or more keys. Extra space allocated as needed */ |
| 180 | }; |
| 181 | |
| 182 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 183 | ** An instance of the virtual machine |
| 184 | */ |
| 185 | struct Vdbe { |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 186 | sqlite *db; /* The whole database */ |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 187 | Btree *pBt; /* Opaque context structure used by DB backend */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 188 | FILE *trace; /* Write an execution trace here, if not NULL */ |
| 189 | int nOp; /* Number of instructions in the program */ |
| 190 | int nOpAlloc; /* Number of slots allocated for aOp[] */ |
| 191 | Op *aOp; /* Space to hold the virtual machine's program */ |
| 192 | int nLabel; /* Number of labels used */ |
| 193 | int nLabelAlloc; /* Number of slots allocated in aLabel[] */ |
| 194 | int *aLabel; /* Space to hold the labels */ |
| 195 | int tos; /* Index of top of stack */ |
| 196 | int nStackAlloc; /* Size of the stack */ |
drh | c61053b | 2000-06-04 12:58:36 +0000 | [diff] [blame] | 197 | Stack *aStack; /* The operand stack, except string values */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 198 | char **zStack; /* Text or binary values of the stack */ |
| 199 | char **azColName; /* Becomes the 4th parameter to callbacks */ |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 200 | int nCursor; /* Number of slots in aCsr[] */ |
| 201 | Cursor *aCsr; /* On element of this array for each open cursor */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 202 | int nList; /* Number of slots in apList[] */ |
drh | 0353ced | 2001-03-20 22:05:00 +0000 | [diff] [blame] | 203 | Keylist **apList; /* For each Keylist */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 204 | int nSort; /* Number of slots in apSort[] */ |
| 205 | Sorter **apSort; /* An open sorter list */ |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 206 | FILE *pFile; /* At most one open file handler */ |
| 207 | int nField; /* Number of file fields */ |
| 208 | char **azField; /* Data for each file field */ |
| 209 | char *zLine; /* A single line from the input file */ |
| 210 | int nLineAlloc; /* Number of spaces allocated for zLine */ |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 211 | int nMem; /* Number of memory locations currently allocated */ |
| 212 | Mem *aMem; /* The memory locations */ |
drh | 600b1b2 | 2000-06-05 21:39:48 +0000 | [diff] [blame] | 213 | Agg agg; /* Aggregate information */ |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 214 | int nSet; /* Number of sets allocated */ |
| 215 | Set *aSet; /* An array of sets */ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 216 | int *pTableRoot; /* Write root page no. for new tables to this addr */ |
| 217 | int *pIndexRoot; /* Write root page no. for new indices to this addr */ |
drh | 0bdaf62 | 2000-06-11 23:50:13 +0000 | [diff] [blame] | 218 | int nFetch; /* Number of OP_Fetch instructions executed */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 219 | }; |
| 220 | |
| 221 | /* |
| 222 | ** Create a new virtual database engine. |
| 223 | */ |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 224 | Vdbe *sqliteVdbeCreate(sqlite *db){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 225 | Vdbe *p; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 226 | p = sqliteMalloc( sizeof(Vdbe) ); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 227 | if( p==0 ) return 0; |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 228 | p->pBt = db->pBe; |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 229 | p->db = db; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 230 | return p; |
| 231 | } |
| 232 | |
| 233 | /* |
| 234 | ** Turn tracing on or off |
| 235 | */ |
| 236 | void sqliteVdbeTrace(Vdbe *p, FILE *trace){ |
| 237 | p->trace = trace; |
| 238 | } |
| 239 | |
| 240 | /* |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 241 | ** Cause the next OP_CreateTable or OP_CreateIndex instruction that executes |
| 242 | ** to write the page number of the root page for the new table or index it |
| 243 | ** creates into the memory location *pAddr. |
| 244 | ** |
| 245 | ** The pointer to the place to write the page number is cleared after |
| 246 | ** the OP_Create* statement. If OP_Create* is executed and the pointer |
| 247 | ** is NULL, an error results. Hence the address can only be used once. |
| 248 | ** If the root address fields are set but OP_Create* operations never |
| 249 | ** execute, that too is an error. |
| 250 | */ |
| 251 | void sqliteVdbeTableRootAddr(Vdbe *p, int *pAddr){ |
| 252 | p->pTableRoot = pAddr; |
| 253 | } |
| 254 | void sqliteVdbeIndexRootAddr(Vdbe *p, int *pAddr){ |
| 255 | p->pIndexRoot = pAddr; |
| 256 | } |
| 257 | |
| 258 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 259 | ** Add a new instruction to the list of instructions current in the |
| 260 | ** VDBE. Return the address of the new instruction. |
| 261 | ** |
| 262 | ** Parameters: |
| 263 | ** |
| 264 | ** p Pointer to the VDBE |
| 265 | ** |
| 266 | ** op The opcode for this instruction |
| 267 | ** |
| 268 | ** p1, p2, p3 Three operands. |
| 269 | ** |
| 270 | ** lbl A symbolic label for this instruction. |
| 271 | ** |
| 272 | ** Symbolic labels are negative numbers that stand for the address |
| 273 | ** of instructions that have yet to be coded. When the instruction |
| 274 | ** is coded, its real address is substituted in the p2 field of |
| 275 | ** prior and subsequent instructions that have the lbl value in |
| 276 | ** their p2 fields. |
| 277 | */ |
| 278 | int sqliteVdbeAddOp(Vdbe *p, int op, int p1, int p2, const char *p3, int lbl){ |
| 279 | int i, j; |
| 280 | |
| 281 | i = p->nOp; |
| 282 | p->nOp++; |
| 283 | if( i>=p->nOpAlloc ){ |
| 284 | int oldSize = p->nOpAlloc; |
| 285 | p->nOpAlloc = p->nOpAlloc*2 + 10; |
| 286 | p->aOp = sqliteRealloc(p->aOp, p->nOpAlloc*sizeof(Op)); |
| 287 | if( p->aOp==0 ){ |
| 288 | p->nOp = 0; |
| 289 | p->nOpAlloc = 0; |
| 290 | return 0; |
| 291 | } |
| 292 | memset(&p->aOp[oldSize], 0, (p->nOpAlloc-oldSize)*sizeof(Op)); |
| 293 | } |
| 294 | p->aOp[i].opcode = op; |
| 295 | p->aOp[i].p1 = p1; |
| 296 | if( p2<0 && (-1-p2)<p->nLabel && p->aLabel[-1-p2]>=0 ){ |
| 297 | p2 = p->aLabel[-1-p2]; |
| 298 | } |
| 299 | p->aOp[i].p2 = p2; |
| 300 | if( p3 && p3[0] ){ |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 301 | p->aOp[i].p3 = sqliteStrDup(p3); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 302 | }else{ |
| 303 | p->aOp[i].p3 = 0; |
| 304 | } |
| 305 | if( lbl<0 && (-lbl)<=p->nLabel ){ |
| 306 | p->aLabel[-1-lbl] = i; |
| 307 | for(j=0; j<i; j++){ |
| 308 | if( p->aOp[j].p2==lbl ) p->aOp[j].p2 = i; |
| 309 | } |
| 310 | } |
| 311 | return i; |
| 312 | } |
| 313 | |
| 314 | /* |
| 315 | ** Resolve label "x" to be the address of the next instruction to |
| 316 | ** be inserted. |
| 317 | */ |
| 318 | void sqliteVdbeResolveLabel(Vdbe *p, int x){ |
| 319 | int j; |
| 320 | if( x<0 && (-x)<=p->nLabel ){ |
| 321 | p->aLabel[-1-x] = p->nOp; |
| 322 | for(j=0; j<p->nOp; j++){ |
| 323 | if( p->aOp[j].p2==x ) p->aOp[j].p2 = p->nOp; |
| 324 | } |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | /* |
| 329 | ** Return the address of the next instruction to be inserted. |
| 330 | */ |
| 331 | int sqliteVdbeCurrentAddr(Vdbe *p){ |
| 332 | return p->nOp; |
| 333 | } |
| 334 | |
| 335 | /* |
| 336 | ** Add a whole list of operations to the operation stack. Return the |
| 337 | ** address of the first operation added. |
| 338 | */ |
| 339 | int sqliteVdbeAddOpList(Vdbe *p, int nOp, VdbeOp const *aOp){ |
| 340 | int addr; |
| 341 | if( p->nOp + nOp >= p->nOpAlloc ){ |
| 342 | int oldSize = p->nOpAlloc; |
| 343 | p->nOpAlloc = p->nOpAlloc*2 + nOp + 10; |
| 344 | p->aOp = sqliteRealloc(p->aOp, p->nOpAlloc*sizeof(Op)); |
| 345 | if( p->aOp==0 ){ |
| 346 | p->nOp = 0; |
| 347 | p->nOpAlloc = 0; |
| 348 | return 0; |
| 349 | } |
| 350 | memset(&p->aOp[oldSize], 0, (p->nOpAlloc-oldSize)*sizeof(Op)); |
| 351 | } |
| 352 | addr = p->nOp; |
| 353 | if( nOp>0 ){ |
| 354 | int i; |
| 355 | for(i=0; i<nOp; i++){ |
| 356 | int p2 = aOp[i].p2; |
| 357 | if( p2<0 ) p2 = addr + ADDR(p2); |
| 358 | sqliteVdbeAddOp(p, aOp[i].opcode, aOp[i].p1, p2, aOp[i].p3, 0); |
| 359 | } |
| 360 | } |
| 361 | return addr; |
| 362 | } |
| 363 | |
| 364 | /* |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 365 | ** Change the value of the P1 operand for a specific instruction. |
| 366 | ** This routine is useful when a large program is loaded from a |
| 367 | ** static array using sqliteVdbeAddOpList but we want to make a |
| 368 | ** few minor changes to the program. |
| 369 | */ |
| 370 | void sqliteVdbeChangeP1(Vdbe *p, int addr, int val){ |
| 371 | if( p && addr>=0 && p->nOp>addr ){ |
| 372 | p->aOp[addr].p1 = val; |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 377 | ** Change the value of the P3 operand for a specific instruction. |
| 378 | ** This routine is useful when a large program is loaded from a |
| 379 | ** static array using sqliteVdbeAddOpList but we want to make a |
| 380 | ** few minor changes to the program. |
| 381 | */ |
| 382 | void sqliteVdbeChangeP3(Vdbe *p, int addr, const char *zP3, int n){ |
| 383 | if( p && addr>=0 && p->nOp>addr && zP3 ){ |
| 384 | sqliteSetNString(&p->aOp[addr].p3, zP3, n, 0); |
| 385 | } |
| 386 | } |
| 387 | |
| 388 | /* |
| 389 | ** If the P3 operand to the specified instruction appears |
| 390 | ** to be a quoted string token, then this procedure removes |
| 391 | ** the quotes. |
| 392 | ** |
| 393 | ** The quoting operator can be either a grave ascent (ASCII 0x27) |
| 394 | ** or a double quote character (ASCII 0x22). Two quotes in a row |
| 395 | ** resolve to be a single actual quote character within the string. |
| 396 | */ |
| 397 | void sqliteVdbeDequoteP3(Vdbe *p, int addr){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 398 | char *z; |
| 399 | if( addr<0 || addr>=p->nOp ) return; |
| 400 | z = p->aOp[addr].p3; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 401 | if( z ) sqliteDequote(z); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | /* |
drh | e1b6a5b | 2000-07-29 13:06:59 +0000 | [diff] [blame] | 405 | ** On the P3 argument of the given instruction, change all |
| 406 | ** strings of whitespace characters into a single space and |
| 407 | ** delete leading and trailing whitespace. |
| 408 | */ |
| 409 | void sqliteVdbeCompressSpace(Vdbe *p, int addr){ |
| 410 | char *z; |
| 411 | int i, j; |
| 412 | if( addr<0 || addr>=p->nOp ) return; |
| 413 | z = p->aOp[addr].p3; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 414 | if( z==0 ) return; |
drh | e1b6a5b | 2000-07-29 13:06:59 +0000 | [diff] [blame] | 415 | i = j = 0; |
| 416 | while( isspace(z[i]) ){ i++; } |
| 417 | while( z[i] ){ |
| 418 | if( isspace(z[i]) ){ |
| 419 | z[j++] = ' '; |
| 420 | while( isspace(z[++i]) ){} |
| 421 | }else{ |
| 422 | z[j++] = z[i++]; |
| 423 | } |
| 424 | } |
| 425 | while( i>0 && isspace(z[i-1]) ){ |
| 426 | z[i-1] = 0; |
| 427 | i--; |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 432 | ** Create a new symbolic label for an instruction that has yet to be |
| 433 | ** coded. The symbolic label is really just a negative number. The |
| 434 | ** label can be used as the P2 value of an operation. Later, when |
| 435 | ** the label is resolved to a specific address, the VDBE will scan |
| 436 | ** through its operation list and change all values of P2 which match |
| 437 | ** the label into the resolved address. |
| 438 | ** |
| 439 | ** The VDBE knows that a P2 value is a label because labels are |
| 440 | ** always negative and P2 values are suppose to be non-negative. |
| 441 | ** Hence, a negative P2 value is a label that has yet to be resolved. |
| 442 | */ |
| 443 | int sqliteVdbeMakeLabel(Vdbe *p){ |
| 444 | int i; |
| 445 | i = p->nLabel++; |
| 446 | if( i>=p->nLabelAlloc ){ |
| 447 | p->nLabelAlloc = p->nLabelAlloc*2 + 10; |
drh | 092d035 | 2001-09-15 13:15:12 +0000 | [diff] [blame] | 448 | p->aLabel = sqliteRealloc( p->aLabel, p->nLabelAlloc*sizeof(p->aLabel[0])); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 449 | } |
| 450 | if( p->aLabel==0 ){ |
| 451 | p->nLabel = 0; |
| 452 | p->nLabelAlloc = 0; |
| 453 | return 0; |
| 454 | } |
| 455 | p->aLabel[i] = -1; |
| 456 | return -1-i; |
| 457 | } |
| 458 | |
| 459 | /* |
drh | 600b1b2 | 2000-06-05 21:39:48 +0000 | [diff] [blame] | 460 | ** Reset an Agg structure. Delete all its contents. |
| 461 | */ |
| 462 | static void AggReset(Agg *p){ |
| 463 | int i; |
| 464 | while( p->pFirst ){ |
| 465 | AggElem *pElem = p->pFirst; |
| 466 | p->pFirst = pElem->pNext; |
| 467 | for(i=0; i<p->nMem; i++){ |
| 468 | if( pElem->aMem[i].s.flags & STK_Dyn ){ |
| 469 | sqliteFree(pElem->aMem[i].z); |
| 470 | } |
| 471 | } |
| 472 | sqliteFree(pElem); |
| 473 | } |
| 474 | sqliteFree(p->apHash); |
| 475 | memset(p, 0, sizeof(*p)); |
| 476 | } |
| 477 | |
| 478 | /* |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 479 | ** Add the given AggElem to the hash array |
drh | 600b1b2 | 2000-06-05 21:39:48 +0000 | [diff] [blame] | 480 | */ |
| 481 | static void AggEnhash(Agg *p, AggElem *pElem){ |
| 482 | int h = sqliteHashNoCase(pElem->zKey, 0) % p->nHash; |
| 483 | pElem->pHash = p->apHash[h]; |
| 484 | p->apHash[h] = pElem; |
| 485 | } |
| 486 | |
| 487 | /* |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 488 | ** Change the size of the hash array to the amount given. |
drh | 600b1b2 | 2000-06-05 21:39:48 +0000 | [diff] [blame] | 489 | */ |
| 490 | static void AggRehash(Agg *p, int nHash){ |
| 491 | int size; |
| 492 | AggElem *pElem; |
| 493 | if( p->nHash==nHash ) return; |
| 494 | size = nHash * sizeof(AggElem*); |
| 495 | p->apHash = sqliteRealloc(p->apHash, size ); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 496 | if( p->apHash==0 ){ |
| 497 | AggReset(p); |
| 498 | return; |
| 499 | } |
drh | 600b1b2 | 2000-06-05 21:39:48 +0000 | [diff] [blame] | 500 | memset(p->apHash, 0, size); |
| 501 | p->nHash = nHash; |
| 502 | for(pElem=p->pFirst; pElem; pElem=pElem->pNext){ |
| 503 | AggEnhash(p, pElem); |
| 504 | } |
| 505 | } |
| 506 | |
| 507 | /* |
| 508 | ** Insert a new element and make it the current element. |
| 509 | ** |
| 510 | ** Return 0 on success and 1 if memory is exhausted. |
| 511 | */ |
| 512 | static int AggInsert(Agg *p, char *zKey){ |
| 513 | AggElem *pElem; |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 514 | int i; |
| 515 | if( p->nHash <= p->nElem*2 ){ |
drh | c837e70 | 2000-06-08 16:26:24 +0000 | [diff] [blame] | 516 | AggRehash(p, p->nElem*2 + 19); |
drh | 600b1b2 | 2000-06-05 21:39:48 +0000 | [diff] [blame] | 517 | } |
| 518 | if( p->nHash==0 ) return 1; |
| 519 | pElem = sqliteMalloc( sizeof(AggElem) + strlen(zKey) + 1 + |
| 520 | (p->nMem-1)*sizeof(pElem->aMem[0]) ); |
| 521 | if( pElem==0 ) return 1; |
| 522 | pElem->zKey = (char*)&pElem->aMem[p->nMem]; |
| 523 | strcpy(pElem->zKey, zKey); |
| 524 | AggEnhash(p, pElem); |
| 525 | pElem->pNext = p->pFirst; |
| 526 | p->pFirst = pElem; |
| 527 | p->nElem++; |
| 528 | p->pCurrent = pElem; |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 529 | for(i=0; i<p->nMem; i++){ |
| 530 | pElem->aMem[i].s.flags = STK_Null; |
| 531 | } |
drh | 600b1b2 | 2000-06-05 21:39:48 +0000 | [diff] [blame] | 532 | return 0; |
| 533 | } |
| 534 | |
| 535 | /* |
| 536 | ** Get the AggElem currently in focus |
| 537 | */ |
| 538 | #define AggInFocus(P) ((P).pCurrent ? (P).pCurrent : _AggInFocus(&(P))) |
| 539 | static AggElem *_AggInFocus(Agg *p){ |
| 540 | AggElem *pFocus = p->pFirst; |
| 541 | if( pFocus ){ |
| 542 | p->pCurrent = pFocus; |
| 543 | }else{ |
| 544 | AggInsert(p,""); |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 545 | pFocus = p->pCurrent = p->pFirst; |
drh | 600b1b2 | 2000-06-05 21:39:48 +0000 | [diff] [blame] | 546 | } |
| 547 | return pFocus; |
| 548 | } |
| 549 | |
| 550 | /* |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 551 | ** Erase all information from a Set |
| 552 | */ |
| 553 | static void SetClear(Set *p){ |
| 554 | SetElem *pElem, *pNext; |
| 555 | for(pElem=p->pAll; pElem; pElem=pNext){ |
| 556 | pNext = pElem->pNext; |
| 557 | sqliteFree(pElem); |
| 558 | } |
| 559 | memset(p, 0, sizeof(*p)); |
| 560 | } |
| 561 | |
| 562 | /* |
| 563 | ** Insert a new element into the set |
| 564 | */ |
| 565 | static void SetInsert(Set *p, char *zKey){ |
| 566 | SetElem *pElem; |
| 567 | int h = sqliteHashNoCase(zKey, 0) % ArraySize(p->apHash); |
| 568 | for(pElem=p->apHash[h]; pElem; pElem=pElem->pHash){ |
| 569 | if( strcmp(pElem->zKey, zKey)==0 ) return; |
| 570 | } |
drh | cfab11b | 2000-06-06 03:31:22 +0000 | [diff] [blame] | 571 | pElem = sqliteMalloc( sizeof(*pElem) + strlen(zKey) ); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 572 | if( pElem==0 ){ |
| 573 | SetClear(p); |
| 574 | return; |
| 575 | } |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 576 | strcpy(pElem->zKey, zKey); |
| 577 | pElem->pNext = p->pAll; |
| 578 | p->pAll = pElem; |
| 579 | pElem->pHash = p->apHash[h]; |
| 580 | p->apHash[h] = pElem; |
| 581 | } |
| 582 | |
| 583 | /* |
| 584 | ** Return TRUE if an element is in the set. Return FALSE if not. |
| 585 | */ |
| 586 | static int SetTest(Set *p, char *zKey){ |
| 587 | SetElem *pElem; |
| 588 | int h = sqliteHashNoCase(zKey, 0) % ArraySize(p->apHash); |
| 589 | for(pElem=p->apHash[h]; pElem; pElem=pElem->pHash){ |
| 590 | if( strcmp(pElem->zKey, zKey)==0 ) return 1; |
| 591 | } |
| 592 | return 0; |
| 593 | } |
| 594 | |
| 595 | /* |
drh | c61053b | 2000-06-04 12:58:36 +0000 | [diff] [blame] | 596 | ** Convert the given stack entity into a string if it isn't one |
| 597 | ** already. Return non-zero if we run out of memory. |
| 598 | ** |
| 599 | ** NULLs are converted into an empty string. |
| 600 | */ |
| 601 | #define Stringify(P,I) \ |
| 602 | ((P->aStack[I].flags & STK_Str)==0 ? hardStringify(P,I) : 0) |
| 603 | static int hardStringify(Vdbe *p, int i){ |
drh | efa4e17 | 2000-10-19 14:42:04 +0000 | [diff] [blame] | 604 | Stack *pStack = &p->aStack[i]; |
| 605 | char **pzStack = &p->zStack[i]; |
drh | c61053b | 2000-06-04 12:58:36 +0000 | [diff] [blame] | 606 | char zBuf[30]; |
drh | efa4e17 | 2000-10-19 14:42:04 +0000 | [diff] [blame] | 607 | int fg = pStack->flags; |
drh | c61053b | 2000-06-04 12:58:36 +0000 | [diff] [blame] | 608 | if( fg & STK_Real ){ |
drh | efa4e17 | 2000-10-19 14:42:04 +0000 | [diff] [blame] | 609 | sprintf(zBuf,"%.15g",pStack->r); |
drh | c61053b | 2000-06-04 12:58:36 +0000 | [diff] [blame] | 610 | }else if( fg & STK_Int ){ |
drh | efa4e17 | 2000-10-19 14:42:04 +0000 | [diff] [blame] | 611 | sprintf(zBuf,"%d",pStack->i); |
drh | c61053b | 2000-06-04 12:58:36 +0000 | [diff] [blame] | 612 | }else{ |
| 613 | p->zStack[i] = ""; |
drh | efa4e17 | 2000-10-19 14:42:04 +0000 | [diff] [blame] | 614 | pStack->n = 1; |
| 615 | pStack->flags |= STK_Str; |
drh | c61053b | 2000-06-04 12:58:36 +0000 | [diff] [blame] | 616 | return 0; |
| 617 | } |
drh | efa4e17 | 2000-10-19 14:42:04 +0000 | [diff] [blame] | 618 | *pzStack = sqliteStrDup(zBuf); |
| 619 | if( *pzStack==0 ) return 1; |
| 620 | pStack->n = strlen(*pzStack)+1; |
| 621 | pStack->flags |= STK_Str|STK_Dyn; |
drh | c61053b | 2000-06-04 12:58:36 +0000 | [diff] [blame] | 622 | return 0; |
| 623 | } |
| 624 | |
| 625 | /* |
| 626 | ** Release the memory associated with the given stack level |
| 627 | */ |
| 628 | #define Release(P,I) if((P)->aStack[I].flags&STK_Dyn){ hardRelease(P,I); } |
| 629 | static void hardRelease(Vdbe *p, int i){ |
| 630 | sqliteFree(p->zStack[i]); |
| 631 | p->zStack[i] = 0; |
| 632 | p->aStack[i].flags &= ~(STK_Str|STK_Dyn); |
| 633 | } |
| 634 | |
| 635 | /* |
| 636 | ** Convert the given stack entity into a integer if it isn't one |
| 637 | ** already. |
| 638 | ** |
| 639 | ** Any prior string or real representation is invalidated. |
| 640 | ** NULLs are converted into 0. |
| 641 | */ |
| 642 | #define Integerify(P,I) \ |
| 643 | if(((P)->aStack[(I)].flags&STK_Int)==0){ hardIntegerify(P,I); } |
| 644 | static void hardIntegerify(Vdbe *p, int i){ |
| 645 | if( p->aStack[i].flags & STK_Real ){ |
| 646 | p->aStack[i].i = p->aStack[i].r; |
| 647 | Release(p, i); |
| 648 | }else if( p->aStack[i].flags & STK_Str ){ |
| 649 | p->aStack[i].i = atoi(p->zStack[i]); |
| 650 | Release(p, i); |
| 651 | }else{ |
| 652 | p->aStack[i].i = 0; |
| 653 | } |
| 654 | p->aStack[i].flags = STK_Int; |
| 655 | } |
| 656 | |
| 657 | /* |
| 658 | ** Get a valid Real representation for the given stack element. |
| 659 | ** |
| 660 | ** Any prior string or integer representation is retained. |
| 661 | ** NULLs are converted into 0.0. |
| 662 | */ |
| 663 | #define Realify(P,I) \ |
| 664 | if(((P)->aStack[(I)].flags&STK_Real)==0){ hardRealify(P,I); } |
| 665 | static void hardRealify(Vdbe *p, int i){ |
| 666 | if( p->aStack[i].flags & STK_Str ){ |
| 667 | p->aStack[i].r = atof(p->zStack[i]); |
| 668 | }else if( p->aStack[i].flags & STK_Int ){ |
| 669 | p->aStack[i].r = p->aStack[i].i; |
| 670 | }else{ |
| 671 | p->aStack[i].r = 0.0; |
| 672 | } |
| 673 | p->aStack[i].flags |= STK_Real; |
| 674 | } |
| 675 | |
| 676 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 677 | ** Pop the stack N times. Free any memory associated with the |
| 678 | ** popped stack elements. |
| 679 | */ |
| 680 | static void PopStack(Vdbe *p, int N){ |
drh | efa4e17 | 2000-10-19 14:42:04 +0000 | [diff] [blame] | 681 | char **pzStack; |
| 682 | Stack *pStack; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 683 | if( p->zStack==0 ) return; |
drh | efa4e17 | 2000-10-19 14:42:04 +0000 | [diff] [blame] | 684 | pStack = &p->aStack[p->tos]; |
| 685 | pzStack = &p->zStack[p->tos]; |
| 686 | p->tos -= N; |
| 687 | while( N-- > 0 ){ |
| 688 | if( pStack->flags & STK_Dyn ){ |
| 689 | sqliteFree(*pzStack); |
| 690 | } |
| 691 | pStack->flags = 0; |
| 692 | *pzStack = 0; |
| 693 | pStack--; |
| 694 | pzStack--; |
| 695 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 696 | } |
| 697 | |
| 698 | /* |
drh | 8c82b35 | 2000-12-10 18:23:50 +0000 | [diff] [blame] | 699 | ** Here is a macro to handle the common case of popping the stack |
| 700 | ** once. This macro only works from within the sqliteVdbeExec() |
| 701 | ** function. |
| 702 | */ |
| 703 | #define POPSTACK \ |
| 704 | if( aStack[p->tos].flags & STK_Dyn ) sqliteFree(zStack[p->tos]); \ |
| 705 | p->tos--; |
| 706 | |
| 707 | /* |
drh | c61053b | 2000-06-04 12:58:36 +0000 | [diff] [blame] | 708 | ** Make sure space has been allocated to hold at least N |
| 709 | ** stack elements. Allocate additional stack space if |
| 710 | ** necessary. |
| 711 | ** |
| 712 | ** Return 0 on success and non-zero if there are memory |
| 713 | ** allocation errors. |
| 714 | */ |
| 715 | #define NeedStack(P,N) (((P)->nStackAlloc<=(N)) ? hardNeedStack(P,N) : 0) |
| 716 | static int hardNeedStack(Vdbe *p, int N){ |
| 717 | int oldAlloc; |
| 718 | int i; |
| 719 | if( N>=p->nStackAlloc ){ |
| 720 | oldAlloc = p->nStackAlloc; |
| 721 | p->nStackAlloc = N + 20; |
| 722 | p->aStack = sqliteRealloc(p->aStack, p->nStackAlloc*sizeof(p->aStack[0])); |
| 723 | p->zStack = sqliteRealloc(p->zStack, p->nStackAlloc*sizeof(char*)); |
| 724 | if( p->aStack==0 || p->zStack==0 ){ |
| 725 | sqliteFree(p->aStack); |
| 726 | sqliteFree(p->zStack); |
| 727 | p->aStack = 0; |
| 728 | p->zStack = 0; |
| 729 | p->nStackAlloc = 0; |
| 730 | return 1; |
| 731 | } |
| 732 | for(i=oldAlloc; i<p->nStackAlloc; i++){ |
| 733 | p->zStack[i] = 0; |
| 734 | p->aStack[i].flags = 0; |
| 735 | } |
| 736 | } |
| 737 | return 0; |
| 738 | } |
| 739 | |
| 740 | /* |
drh | 0353ced | 2001-03-20 22:05:00 +0000 | [diff] [blame] | 741 | ** Delete a keylist |
| 742 | */ |
| 743 | static void KeylistFree(Keylist *p){ |
| 744 | while( p ){ |
| 745 | Keylist *pNext = p->pNext; |
| 746 | sqliteFree(p); |
| 747 | p = pNext; |
| 748 | } |
| 749 | } |
| 750 | |
| 751 | /* |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 752 | ** Close a cursor and release all the resources that cursor happens |
| 753 | ** to hold. |
| 754 | */ |
| 755 | static void cleanupCursor(Cursor *pCx){ |
| 756 | if( pCx->pCursor ){ |
| 757 | sqliteBtreeCloseCursor(pCx->pCursor); |
| 758 | } |
| 759 | if( pCx->zKey ){ |
| 760 | sqliteFree(pCx->zKey); |
| 761 | } |
| 762 | if( pCx->pBt ){ |
| 763 | sqliteBtreeClose(pCx->pBt); |
| 764 | } |
| 765 | memset(pCx, 0, sizeof(Cursor)); |
| 766 | } |
| 767 | |
| 768 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 769 | ** Clean up the VM after execution. |
| 770 | ** |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 771 | ** This routine will automatically close any cursors, lists, and/or |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 772 | ** sorters that were left open. |
| 773 | */ |
| 774 | static void Cleanup(Vdbe *p){ |
| 775 | int i; |
| 776 | PopStack(p, p->tos+1); |
| 777 | sqliteFree(p->azColName); |
| 778 | p->azColName = 0; |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 779 | for(i=0; i<p->nCursor; i++){ |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 780 | cleanupCursor(&p->aCsr[i]); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 781 | } |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 782 | sqliteFree(p->aCsr); |
| 783 | p->aCsr = 0; |
| 784 | p->nCursor = 0; |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 785 | for(i=0; i<p->nMem; i++){ |
| 786 | if( p->aMem[i].s.flags & STK_Dyn ){ |
| 787 | sqliteFree(p->aMem[i].z); |
| 788 | } |
| 789 | } |
| 790 | sqliteFree(p->aMem); |
| 791 | p->aMem = 0; |
| 792 | p->nMem = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 793 | for(i=0; i<p->nList; i++){ |
drh | 0353ced | 2001-03-20 22:05:00 +0000 | [diff] [blame] | 794 | KeylistFree(p->apList[i]); |
| 795 | p->apList[i] = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 796 | } |
| 797 | sqliteFree(p->apList); |
| 798 | p->apList = 0; |
| 799 | p->nList = 0; |
| 800 | for(i=0; i<p->nSort; i++){ |
| 801 | Sorter *pSorter; |
| 802 | while( (pSorter = p->apSort[i])!=0 ){ |
| 803 | p->apSort[i] = pSorter->pNext; |
| 804 | sqliteFree(pSorter->zKey); |
| 805 | sqliteFree(pSorter->pData); |
| 806 | sqliteFree(pSorter); |
| 807 | } |
| 808 | } |
| 809 | sqliteFree(p->apSort); |
| 810 | p->apSort = 0; |
| 811 | p->nSort = 0; |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 812 | if( p->pFile ){ |
| 813 | if( p->pFile!=stdin ) fclose(p->pFile); |
| 814 | p->pFile = 0; |
| 815 | } |
| 816 | if( p->azField ){ |
| 817 | sqliteFree(p->azField); |
| 818 | p->azField = 0; |
| 819 | } |
| 820 | p->nField = 0; |
| 821 | if( p->zLine ){ |
| 822 | sqliteFree(p->zLine); |
| 823 | p->zLine = 0; |
| 824 | } |
| 825 | p->nLineAlloc = 0; |
drh | 600b1b2 | 2000-06-05 21:39:48 +0000 | [diff] [blame] | 826 | AggReset(&p->agg); |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 827 | for(i=0; i<p->nSet; i++){ |
| 828 | SetClear(&p->aSet[i]); |
| 829 | } |
| 830 | sqliteFree(p->aSet); |
| 831 | p->aSet = 0; |
| 832 | p->nSet = 0; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 833 | p->pTableRoot = 0; |
| 834 | p->pIndexRoot = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 835 | } |
| 836 | |
| 837 | /* |
| 838 | ** Delete an entire VDBE. |
| 839 | */ |
| 840 | void sqliteVdbeDelete(Vdbe *p){ |
| 841 | int i; |
| 842 | if( p==0 ) return; |
| 843 | Cleanup(p); |
| 844 | if( p->nOpAlloc==0 ){ |
| 845 | p->aOp = 0; |
| 846 | p->nOp = 0; |
| 847 | } |
| 848 | for(i=0; i<p->nOp; i++){ |
| 849 | sqliteFree(p->aOp[i].p3); |
| 850 | } |
| 851 | sqliteFree(p->aOp); |
| 852 | sqliteFree(p->aLabel); |
drh | c61053b | 2000-06-04 12:58:36 +0000 | [diff] [blame] | 853 | sqliteFree(p->aStack); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 854 | sqliteFree(p->zStack); |
| 855 | sqliteFree(p); |
| 856 | } |
| 857 | |
| 858 | /* |
| 859 | ** A translation from opcode numbers to opcode names. Used for testing |
| 860 | ** and debugging only. |
| 861 | ** |
| 862 | ** If any of the numeric OP_ values for opcodes defined in sqliteVdbe.h |
| 863 | ** change, be sure to change this array to match. You can use the |
| 864 | ** "opNames.awk" awk script which is part of the source tree to regenerate |
| 865 | ** this array, then copy and paste it into this file, if you want. |
| 866 | */ |
| 867 | static char *zOpName[] = { 0, |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 868 | "Transaction", "Commit", "Rollback", "ReadCookie", |
| 869 | "SetCookie", "VerifyCookie", "Open", "OpenTemp", |
| 870 | "Close", "MoveTo", "Fcnt", "NewRecno", |
| 871 | "Put", "Distinct", "Found", "NotFound", |
| 872 | "Delete", "Column", "KeyAsData", "Recno", |
| 873 | "FullKey", "Rewind", "Next", "Destroy", |
| 874 | "Clear", "CreateIndex", "CreateTable", "Reorganize", |
| 875 | "BeginIdx", "NextIdx", "PutIdx", "DeleteIdx", |
| 876 | "MemLoad", "MemStore", "ListOpen", "ListWrite", |
| 877 | "ListRewind", "ListRead", "ListClose", "SortOpen", |
| 878 | "SortPut", "SortMakeRec", "SortMakeKey", "Sort", |
| 879 | "SortNext", "SortKey", "SortCallback", "SortClose", |
| 880 | "FileOpen", "FileRead", "FileColumn", "FileClose", |
| 881 | "AggReset", "AggFocus", "AggIncr", "AggNext", |
| 882 | "AggSet", "AggGet", "SetInsert", "SetFound", |
| 883 | "SetNotFound", "SetClear", "MakeRecord", "MakeKey", |
| 884 | "MakeIdxKey", "Goto", "If", "Halt", |
| 885 | "ColumnCount", "ColumnName", "Callback", "Integer", |
| 886 | "String", "Null", "Pop", "Dup", |
| 887 | "Pull", "Add", "AddImm", "Subtract", |
| 888 | "Multiply", "Divide", "Min", "Max", |
| 889 | "Like", "Glob", "Eq", "Ne", |
| 890 | "Lt", "Le", "Gt", "Ge", |
| 891 | "IsNull", "NotNull", "Negative", "And", |
| 892 | "Or", "Not", "Concat", "Noop", |
| 893 | "Strlen", "Substr", |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 894 | }; |
| 895 | |
| 896 | /* |
| 897 | ** Given the name of an opcode, return its number. Return 0 if |
| 898 | ** there is no match. |
| 899 | ** |
| 900 | ** This routine is used for testing and debugging. |
| 901 | */ |
| 902 | int sqliteVdbeOpcode(const char *zName){ |
| 903 | int i; |
| 904 | for(i=1; i<=OP_MAX; i++){ |
| 905 | if( sqliteStrICmp(zName, zOpName[i])==0 ) return i; |
| 906 | } |
| 907 | return 0; |
| 908 | } |
| 909 | |
| 910 | /* |
| 911 | ** Give a listing of the program in the virtual machine. |
| 912 | ** |
| 913 | ** The interface is the same as sqliteVdbeExec(). But instead of |
| 914 | ** running the code, it invokes the callback once for each instruction. |
| 915 | ** This feature is used to implement "EXPLAIN". |
| 916 | */ |
| 917 | int sqliteVdbeList( |
| 918 | Vdbe *p, /* The VDBE */ |
| 919 | sqlite_callback xCallback, /* The callback */ |
| 920 | void *pArg, /* 1st argument to callback */ |
| 921 | char **pzErrMsg /* Error msg written here */ |
| 922 | ){ |
| 923 | int i, rc; |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 924 | char *azValue[6]; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 925 | char zAddr[20]; |
| 926 | char zP1[20]; |
| 927 | char zP2[20]; |
| 928 | static char *azColumnNames[] = { |
| 929 | "addr", "opcode", "p1", "p2", "p3", 0 |
| 930 | }; |
| 931 | |
| 932 | if( xCallback==0 ) return 0; |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 933 | azValue[0] = zAddr; |
| 934 | azValue[2] = zP1; |
| 935 | azValue[3] = zP2; |
| 936 | azValue[5] = 0; |
drh | 58b9576 | 2000-06-02 01:17:37 +0000 | [diff] [blame] | 937 | rc = SQLITE_OK; |
drh | 4794b98 | 2000-06-06 13:54:14 +0000 | [diff] [blame] | 938 | /* if( pzErrMsg ){ *pzErrMsg = 0; } */ |
drh | 58b9576 | 2000-06-02 01:17:37 +0000 | [diff] [blame] | 939 | for(i=0; rc==SQLITE_OK && i<p->nOp; i++){ |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 940 | if( p->db->flags & SQLITE_Interrupt ){ |
| 941 | p->db->flags &= ~SQLITE_Interrupt; |
| 942 | sqliteSetString(pzErrMsg, "interrupted", 0); |
| 943 | rc = SQLITE_INTERRUPT; |
| 944 | break; |
| 945 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 946 | sprintf(zAddr,"%d",i); |
| 947 | sprintf(zP1,"%d", p->aOp[i].p1); |
| 948 | sprintf(zP2,"%d", p->aOp[i].p2); |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 949 | azValue[4] = p->aOp[i].p3; |
| 950 | azValue[1] = zOpName[p->aOp[i].opcode]; |
| 951 | if( xCallback(pArg, 5, azValue, azColumnNames) ){ |
drh | 58b9576 | 2000-06-02 01:17:37 +0000 | [diff] [blame] | 952 | rc = SQLITE_ABORT; |
| 953 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 954 | } |
| 955 | return rc; |
| 956 | } |
| 957 | |
| 958 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 959 | ** The parameters are pointers to the head of two sorted lists |
| 960 | ** of Sorter structures. Merge these two lists together and return |
| 961 | ** a single sorted list. This routine forms the core of the merge-sort |
| 962 | ** algorithm. |
| 963 | ** |
| 964 | ** In the case of a tie, left sorts in front of right. |
| 965 | */ |
| 966 | static Sorter *Merge(Sorter *pLeft, Sorter *pRight){ |
| 967 | Sorter sHead; |
| 968 | Sorter *pTail; |
| 969 | pTail = &sHead; |
| 970 | pTail->pNext = 0; |
| 971 | while( pLeft && pRight ){ |
| 972 | int c = sqliteSortCompare(pLeft->zKey, pRight->zKey); |
| 973 | if( c<=0 ){ |
| 974 | pTail->pNext = pLeft; |
| 975 | pLeft = pLeft->pNext; |
| 976 | }else{ |
| 977 | pTail->pNext = pRight; |
| 978 | pRight = pRight->pNext; |
| 979 | } |
| 980 | pTail = pTail->pNext; |
| 981 | } |
| 982 | if( pLeft ){ |
| 983 | pTail->pNext = pLeft; |
| 984 | }else if( pRight ){ |
| 985 | pTail->pNext = pRight; |
| 986 | } |
| 987 | return sHead.pNext; |
| 988 | } |
| 989 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 990 | /* |
drh | 7663448 | 2000-10-23 01:07:59 +0000 | [diff] [blame] | 991 | ** Code contained within the VERIFY() macro is not needed for correct |
| 992 | ** execution. It is there only to catch errors. So when we compile |
| 993 | ** with NDEBUG=1, the VERIFY() code is omitted. |
| 994 | */ |
| 995 | #ifdef NDEBUG |
| 996 | # define VERIFY(X) |
| 997 | #else |
| 998 | # define VERIFY(X) X |
| 999 | #endif |
| 1000 | |
| 1001 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1002 | ** Execute the program in the VDBE. |
| 1003 | ** |
| 1004 | ** If an error occurs, an error message is written to memory obtained |
| 1005 | ** from sqliteMalloc() and *pzErrMsg is made to point to that memory. |
| 1006 | ** The return parameter is the number of errors. |
| 1007 | ** |
| 1008 | ** If the callback every returns non-zero, then the program exits |
drh | 58b9576 | 2000-06-02 01:17:37 +0000 | [diff] [blame] | 1009 | ** immediately. No error message but the function does return SQLITE_ABORT. |
| 1010 | ** |
| 1011 | ** A memory allocation error causes this routine to return SQLITE_NOMEM |
| 1012 | ** and abandon furture processing. |
| 1013 | ** |
| 1014 | ** Other fatal errors return SQLITE_ERROR. |
| 1015 | ** |
| 1016 | ** If a database file could not be opened because it is locked by |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 1017 | ** another database instance, then the xBusy() callback is invoked |
| 1018 | ** with pBusyArg as its first argument, the name of the table as the |
| 1019 | ** second argument, and the number of times the open has been attempted |
| 1020 | ** as the third argument. The xBusy() callback will typically wait |
| 1021 | ** for the database file to be openable, then return. If xBusy() |
| 1022 | ** returns non-zero, another attempt is made to open the file. If |
| 1023 | ** xBusy() returns zero, or if xBusy is NULL, then execution halts |
| 1024 | ** and this routine returns SQLITE_BUSY. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1025 | */ |
| 1026 | int sqliteVdbeExec( |
| 1027 | Vdbe *p, /* The VDBE */ |
| 1028 | sqlite_callback xCallback, /* The callback */ |
| 1029 | void *pArg, /* 1st argument to callback */ |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 1030 | char **pzErrMsg, /* Error msg written here */ |
| 1031 | void *pBusyArg, /* 1st argument to the busy callback */ |
| 1032 | int (*xBusy)(void*,const char*,int) /* Called when a file is busy */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1033 | ){ |
| 1034 | int pc; /* The program counter */ |
| 1035 | Op *pOp; /* Current operation */ |
| 1036 | int rc; /* Value to return */ |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 1037 | Btree *pBt = p->pBt; /* The backend driver */ |
drh | 7663448 | 2000-10-23 01:07:59 +0000 | [diff] [blame] | 1038 | sqlite *db = p->db; /* The database */ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1039 | char **zStack; /* Text stack */ |
| 1040 | Stack *aStack; /* Additional stack information */ |
| 1041 | char zBuf[100]; /* Space to sprintf() an integer */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1042 | |
drh | 7663448 | 2000-10-23 01:07:59 +0000 | [diff] [blame] | 1043 | |
| 1044 | /* No instruction ever pushes more than a single element onto the |
| 1045 | ** stack. And the stack never grows on successive executions of the |
| 1046 | ** same loop. So the total number of instructions is an upper bound |
| 1047 | ** on the maximum stack depth required. |
| 1048 | ** |
| 1049 | ** Allocation all the stack space we will ever need. |
| 1050 | */ |
| 1051 | NeedStack(p, p->nOp); |
drh | 8c3052c | 2000-10-23 13:16:31 +0000 | [diff] [blame] | 1052 | zStack = p->zStack; |
| 1053 | aStack = p->aStack; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1054 | p->tos = -1; |
drh | 7663448 | 2000-10-23 01:07:59 +0000 | [diff] [blame] | 1055 | |
drh | 58b9576 | 2000-06-02 01:17:37 +0000 | [diff] [blame] | 1056 | rc = SQLITE_OK; |
drh | d1dedb8 | 2000-06-05 02:07:04 +0000 | [diff] [blame] | 1057 | #ifdef MEMORY_DEBUG |
| 1058 | if( access("vdbe_trace",0)==0 ){ |
drh | 3fc190c | 2001-09-14 03:24:23 +0000 | [diff] [blame] | 1059 | p->trace = stdout; |
drh | d1dedb8 | 2000-06-05 02:07:04 +0000 | [diff] [blame] | 1060 | } |
| 1061 | #endif |
drh | 4794b98 | 2000-06-06 13:54:14 +0000 | [diff] [blame] | 1062 | /* if( pzErrMsg ){ *pzErrMsg = 0; } */ |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1063 | if( sqlite_malloc_failed ) rc = SQLITE_NOMEM; |
drh | 7663448 | 2000-10-23 01:07:59 +0000 | [diff] [blame] | 1064 | for(pc=0; rc==SQLITE_OK && pc<p->nOp VERIFY(&& pc>=0); pc++){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1065 | pOp = &p->aOp[pc]; |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 1066 | |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 1067 | /* Interrupt processing if requested. |
| 1068 | */ |
drh | 7663448 | 2000-10-23 01:07:59 +0000 | [diff] [blame] | 1069 | if( db->flags & SQLITE_Interrupt ){ |
| 1070 | db->flags &= ~SQLITE_Interrupt; |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 1071 | rc = SQLITE_INTERRUPT; |
| 1072 | sqliteSetString(pzErrMsg, "interrupted", 0); |
| 1073 | break; |
| 1074 | } |
| 1075 | |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 1076 | /* Only allow tracing if NDEBUG is not defined. |
| 1077 | */ |
| 1078 | #ifndef NDEBUG |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1079 | if( p->trace ){ |
| 1080 | fprintf(p->trace,"%4d %-12s %4d %4d %s\n", |
| 1081 | pc, zOpName[pOp->opcode], pOp->p1, pOp->p2, |
| 1082 | pOp->p3 ? pOp->p3 : ""); |
drh | 3fc190c | 2001-09-14 03:24:23 +0000 | [diff] [blame] | 1083 | fflush(p->trace); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1084 | } |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 1085 | #endif |
| 1086 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1087 | switch( pOp->opcode ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1088 | |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1089 | /***************************************************************************** |
| 1090 | ** What follows is a massive switch statement where each case implements a |
| 1091 | ** separate instruction in the virtual machine. If we follow the usual |
| 1092 | ** indentation conventions, each case should be indented by 6 spaces. But |
| 1093 | ** that is a lot of wasted space on the left margin. So the code within |
| 1094 | ** the switch statement will break with convention and be flush-left. Another |
| 1095 | ** big comment (similar to this one) will mark the point in the code where |
| 1096 | ** we transition back to normal indentation. |
| 1097 | *****************************************************************************/ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1098 | |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1099 | /* Opcode: Goto P2 * * |
| 1100 | ** |
| 1101 | ** An unconditional jump to address P2. |
| 1102 | ** The next instruction executed will be |
| 1103 | ** the one at index P2 from the beginning of |
| 1104 | ** the program. |
| 1105 | */ |
| 1106 | case OP_Goto: { |
| 1107 | pc = pOp->p2 - 1; |
| 1108 | break; |
| 1109 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1110 | |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1111 | /* Opcode: Halt * * * |
| 1112 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 1113 | ** Exit immediately. All open cursors, Lists, Sorts, etc are closed |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1114 | ** automatically. |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 1115 | ** |
| 1116 | ** There is an implied Halt instruction inserted at the very end of |
| 1117 | ** every program. So a jump past the last instruction of the program |
| 1118 | ** is the same as executing Halt. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1119 | */ |
| 1120 | case OP_Halt: { |
| 1121 | pc = p->nOp-1; |
| 1122 | break; |
| 1123 | } |
drh | c61053b | 2000-06-04 12:58:36 +0000 | [diff] [blame] | 1124 | |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1125 | /* Opcode: Integer P1 * * |
| 1126 | ** |
| 1127 | ** The integer value P1 is pushed onto the stack. |
| 1128 | */ |
| 1129 | case OP_Integer: { |
| 1130 | int i = ++p->tos; |
| 1131 | VERIFY( if( NeedStack(p, p->tos) ) goto no_mem; ) |
| 1132 | aStack[i].i = pOp->p1; |
| 1133 | aStack[i].flags = STK_Int; |
| 1134 | break; |
| 1135 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1136 | |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1137 | /* Opcode: String * * P3 |
| 1138 | ** |
| 1139 | ** The string value P3 is pushed onto the stack. |
| 1140 | */ |
| 1141 | case OP_String: { |
| 1142 | int i = ++p->tos; |
| 1143 | char *z; |
| 1144 | VERIFY( if( NeedStack(p, p->tos) ) goto no_mem; ) |
| 1145 | z = pOp->p3; |
| 1146 | if( z==0 ) z = ""; |
| 1147 | zStack[i] = z; |
| 1148 | aStack[i].n = strlen(z) + 1; |
| 1149 | aStack[i].flags = STK_Str; |
| 1150 | break; |
| 1151 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1152 | |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1153 | /* Opcode: Null * * * |
| 1154 | ** |
| 1155 | ** Push a NULL value onto the stack. |
| 1156 | */ |
| 1157 | case OP_Null: { |
| 1158 | int i = ++p->tos; |
| 1159 | VERIFY( if( NeedStack(p, p->tos) ) goto no_mem; ) |
| 1160 | zStack[i] = 0; |
| 1161 | aStack[i].flags = STK_Null; |
| 1162 | break; |
| 1163 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1164 | |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1165 | /* Opcode: Pop P1 * * |
| 1166 | ** |
| 1167 | ** P1 elements are popped off of the top of stack and discarded. |
| 1168 | */ |
| 1169 | case OP_Pop: { |
| 1170 | PopStack(p, pOp->p1); |
| 1171 | break; |
| 1172 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1173 | |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1174 | /* Opcode: Dup P1 * * |
| 1175 | ** |
| 1176 | ** A copy of the P1-th element of the stack |
| 1177 | ** is made and pushed onto the top of the stack. |
| 1178 | ** The top of the stack is element 0. So the |
| 1179 | ** instruction "Dup 0 0 0" will make a copy of the |
| 1180 | ** top of the stack. |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 1181 | ** |
| 1182 | ** Also see the Pull instruction. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1183 | */ |
| 1184 | case OP_Dup: { |
| 1185 | int i = p->tos - pOp->p1; |
| 1186 | int j = ++p->tos; |
| 1187 | VERIFY( if( i<0 ) goto not_enough_stack; ) |
| 1188 | VERIFY( if( NeedStack(p, p->tos) ) goto no_mem; ) |
| 1189 | aStack[j] = aStack[i]; |
| 1190 | if( aStack[i].flags & STK_Dyn ){ |
| 1191 | zStack[j] = sqliteMalloc( aStack[j].n ); |
| 1192 | if( zStack[j]==0 ) goto no_mem; |
| 1193 | memcpy(zStack[j], zStack[i], aStack[j].n); |
| 1194 | }else{ |
| 1195 | zStack[j] = zStack[i]; |
| 1196 | } |
| 1197 | break; |
| 1198 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1199 | |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1200 | /* Opcode: Pull P1 * * |
| 1201 | ** |
| 1202 | ** The P1-th element is removed from its current location on |
| 1203 | ** the stack and pushed back on top of the stack. The |
| 1204 | ** top of the stack is element 0, so "Pull 0 0 0" is |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 1205 | ** a no-op. "Pull 1 0 0" swaps the top two elements of |
| 1206 | ** the stack. |
| 1207 | ** |
| 1208 | ** See also the Dup instruction. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1209 | */ |
| 1210 | case OP_Pull: { |
| 1211 | int from = p->tos - pOp->p1; |
| 1212 | int to = p->tos; |
| 1213 | int i; |
| 1214 | Stack ts; |
| 1215 | char *tz; |
| 1216 | VERIFY( if( from<0 ) goto not_enough_stack; ) |
| 1217 | ts = aStack[from]; |
| 1218 | tz = zStack[from]; |
| 1219 | for(i=from; i<to; i++){ |
| 1220 | aStack[i] = aStack[i+1]; |
| 1221 | zStack[i] = zStack[i+1]; |
| 1222 | } |
| 1223 | aStack[to] = ts; |
| 1224 | zStack[to] = tz; |
| 1225 | break; |
| 1226 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1227 | |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1228 | /* Opcode: ColumnCount P1 * * |
| 1229 | ** |
| 1230 | ** Specify the number of column values that will appear in the |
| 1231 | ** array passed as the 4th parameter to the callback. No checking |
| 1232 | ** is done. If this value is wrong, a coredump can result. |
| 1233 | */ |
| 1234 | case OP_ColumnCount: { |
| 1235 | p->azColName = sqliteRealloc(p->azColName, (pOp->p1+1)*sizeof(char*)); |
| 1236 | if( p->azColName==0 ) goto no_mem; |
| 1237 | p->azColName[pOp->p1] = 0; |
| 1238 | break; |
| 1239 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1240 | |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1241 | /* Opcode: ColumnName P1 * P3 |
| 1242 | ** |
| 1243 | ** P3 becomes the P1-th column name (first is 0). An array of pointers |
| 1244 | ** to all column names is passed as the 4th parameter to the callback. |
| 1245 | ** The ColumnCount opcode must be executed first to allocate space to |
| 1246 | ** hold the column names. Failure to do this will likely result in |
| 1247 | ** a coredump. |
| 1248 | */ |
| 1249 | case OP_ColumnName: { |
| 1250 | p->azColName[pOp->p1] = pOp->p3 ? pOp->p3 : ""; |
| 1251 | break; |
| 1252 | } |
drh | c61053b | 2000-06-04 12:58:36 +0000 | [diff] [blame] | 1253 | |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1254 | /* Opcode: Callback P1 * * |
| 1255 | ** |
| 1256 | ** Pop P1 values off the stack and form them into an array. Then |
| 1257 | ** invoke the callback function using the newly formed array as the |
| 1258 | ** 3rd parameter. |
| 1259 | */ |
| 1260 | case OP_Callback: { |
| 1261 | int i = p->tos - pOp->p1 + 1; |
| 1262 | int j; |
| 1263 | VERIFY( if( i<0 ) goto not_enough_stack; ) |
| 1264 | VERIFY( if( NeedStack(p, p->tos+2) ) goto no_mem; ) |
| 1265 | for(j=i; j<=p->tos; j++){ |
| 1266 | if( (aStack[j].flags & STK_Null)==0 ){ |
| 1267 | if( Stringify(p, j) ) goto no_mem; |
| 1268 | } |
| 1269 | } |
| 1270 | zStack[p->tos+1] = 0; |
| 1271 | if( xCallback!=0 ){ |
| 1272 | if( xCallback(pArg, pOp->p1, &zStack[i], p->azColName)!=0 ){ |
| 1273 | rc = SQLITE_ABORT; |
| 1274 | } |
| 1275 | } |
| 1276 | PopStack(p, pOp->p1); |
| 1277 | break; |
| 1278 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1279 | |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1280 | /* Opcode: Concat P1 P2 P3 |
| 1281 | ** |
| 1282 | ** Look at the first P1 elements of the stack. Append them all |
| 1283 | ** together with the lowest element first. Use P3 as a separator. |
| 1284 | ** Put the result on the top of the stack. The original P1 elements |
| 1285 | ** are popped from the stack if P2==0 and retained if P2==1. |
| 1286 | ** |
| 1287 | ** If P3 is NULL, then use no separator. When P1==1, this routine |
| 1288 | ** makes a copy of the top stack element into memory obtained |
| 1289 | ** from sqliteMalloc(). |
| 1290 | */ |
| 1291 | case OP_Concat: { |
| 1292 | char *zNew; |
| 1293 | int nByte; |
| 1294 | int nField; |
| 1295 | int i, j; |
| 1296 | char *zSep; |
| 1297 | int nSep; |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1298 | |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1299 | nField = pOp->p1; |
| 1300 | zSep = pOp->p3; |
| 1301 | if( zSep==0 ) zSep = ""; |
| 1302 | nSep = strlen(zSep); |
| 1303 | VERIFY( if( p->tos+1<nField ) goto not_enough_stack; ) |
| 1304 | nByte = 1 - nSep; |
| 1305 | for(i=p->tos-nField+1; i<=p->tos; i++){ |
| 1306 | if( aStack[i].flags & STK_Null ){ |
| 1307 | nByte += nSep; |
| 1308 | }else{ |
| 1309 | if( Stringify(p, i) ) goto no_mem; |
| 1310 | nByte += aStack[i].n - 1 + nSep; |
| 1311 | } |
| 1312 | } |
| 1313 | zNew = sqliteMalloc( nByte ); |
| 1314 | if( zNew==0 ) goto no_mem; |
| 1315 | j = 0; |
| 1316 | for(i=p->tos-nField+1; i<=p->tos; i++){ |
| 1317 | if( (aStack[i].flags & STK_Null)==0 ){ |
| 1318 | memcpy(&zNew[j], zStack[i], aStack[i].n-1); |
| 1319 | j += aStack[i].n-1; |
| 1320 | } |
| 1321 | if( nSep>0 && i<p->tos ){ |
| 1322 | memcpy(&zNew[j], zSep, nSep); |
| 1323 | j += nSep; |
| 1324 | } |
| 1325 | } |
| 1326 | zNew[j] = 0; |
| 1327 | if( pOp->p2==0 ) PopStack(p, nField); |
| 1328 | VERIFY( NeedStack(p, p->tos+1); ) |
| 1329 | p->tos++; |
| 1330 | aStack[p->tos].n = nByte; |
| 1331 | aStack[p->tos].flags = STK_Str|STK_Dyn; |
| 1332 | zStack[p->tos] = zNew; |
| 1333 | break; |
| 1334 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1335 | |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1336 | /* Opcode: Add * * * |
| 1337 | ** |
| 1338 | ** Pop the top two elements from the stack, add them together, |
| 1339 | ** and push the result back onto the stack. If either element |
| 1340 | ** is a string then it is converted to a double using the atof() |
| 1341 | ** function before the addition. |
| 1342 | */ |
| 1343 | /* Opcode: Multiply * * * |
| 1344 | ** |
| 1345 | ** Pop the top two elements from the stack, multiply them together, |
| 1346 | ** and push the result back onto the stack. If either element |
| 1347 | ** is a string then it is converted to a double using the atof() |
| 1348 | ** function before the multiplication. |
| 1349 | */ |
| 1350 | /* Opcode: Subtract * * * |
| 1351 | ** |
| 1352 | ** Pop the top two elements from the stack, subtract the |
| 1353 | ** first (what was on top of the stack) from the second (the |
| 1354 | ** next on stack) |
| 1355 | ** and push the result back onto the stack. If either element |
| 1356 | ** is a string then it is converted to a double using the atof() |
| 1357 | ** function before the subtraction. |
| 1358 | */ |
| 1359 | /* Opcode: Divide * * * |
| 1360 | ** |
| 1361 | ** Pop the top two elements from the stack, divide the |
| 1362 | ** first (what was on top of the stack) from the second (the |
| 1363 | ** next on stack) |
| 1364 | ** and push the result back onto the stack. If either element |
| 1365 | ** is a string then it is converted to a double using the atof() |
| 1366 | ** function before the division. Division by zero returns NULL. |
| 1367 | */ |
| 1368 | case OP_Add: |
| 1369 | case OP_Subtract: |
| 1370 | case OP_Multiply: |
| 1371 | case OP_Divide: { |
| 1372 | int tos = p->tos; |
| 1373 | int nos = tos - 1; |
| 1374 | VERIFY( if( nos<0 ) goto not_enough_stack; ) |
| 1375 | if( (aStack[tos].flags & aStack[nos].flags & STK_Int)==STK_Int ){ |
| 1376 | int a, b; |
| 1377 | a = aStack[tos].i; |
| 1378 | b = aStack[nos].i; |
| 1379 | switch( pOp->opcode ){ |
| 1380 | case OP_Add: b += a; break; |
| 1381 | case OP_Subtract: b -= a; break; |
| 1382 | case OP_Multiply: b *= a; break; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1383 | default: { |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1384 | if( a==0 ) goto divide_by_zero; |
| 1385 | b /= a; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1386 | break; |
| 1387 | } |
| 1388 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1389 | POPSTACK; |
| 1390 | Release(p, nos); |
| 1391 | aStack[nos].i = b; |
| 1392 | aStack[nos].flags = STK_Int; |
| 1393 | }else{ |
| 1394 | double a, b; |
| 1395 | Realify(p, tos); |
| 1396 | Realify(p, nos); |
| 1397 | a = aStack[tos].r; |
| 1398 | b = aStack[nos].r; |
| 1399 | switch( pOp->opcode ){ |
| 1400 | case OP_Add: b += a; break; |
| 1401 | case OP_Subtract: b -= a; break; |
| 1402 | case OP_Multiply: b *= a; break; |
| 1403 | default: { |
| 1404 | if( a==0.0 ) goto divide_by_zero; |
| 1405 | b /= a; |
| 1406 | break; |
| 1407 | } |
| 1408 | } |
| 1409 | POPSTACK; |
| 1410 | Release(p, nos); |
| 1411 | aStack[nos].r = b; |
| 1412 | aStack[nos].flags = STK_Real; |
| 1413 | } |
| 1414 | break; |
| 1415 | |
| 1416 | divide_by_zero: |
| 1417 | PopStack(p, 2); |
| 1418 | p->tos = nos; |
| 1419 | aStack[nos].flags = STK_Null; |
| 1420 | break; |
| 1421 | } |
| 1422 | |
| 1423 | /* Opcode: Max * * * |
| 1424 | ** |
| 1425 | ** Pop the top two elements from the stack then push back the |
| 1426 | ** largest of the two. |
| 1427 | */ |
| 1428 | case OP_Max: { |
| 1429 | int tos = p->tos; |
| 1430 | int nos = tos - 1; |
| 1431 | int ft, fn; |
| 1432 | int copy = 0; |
| 1433 | VERIFY( if( nos<0 ) goto not_enough_stack; ) |
| 1434 | ft = aStack[tos].flags; |
| 1435 | fn = aStack[nos].flags; |
| 1436 | if( fn & STK_Null ){ |
| 1437 | copy = 1; |
| 1438 | }else if( (ft & fn & STK_Int)==STK_Int ){ |
| 1439 | copy = aStack[nos].i<aStack[tos].i; |
| 1440 | }else if( ( (ft|fn) & (STK_Int|STK_Real) ) !=0 ){ |
| 1441 | Realify(p, tos); |
| 1442 | Realify(p, nos); |
| 1443 | copy = aStack[tos].r>aStack[nos].r; |
| 1444 | }else{ |
| 1445 | if( Stringify(p, tos) || Stringify(p, nos) ) goto no_mem; |
| 1446 | copy = sqliteCompare(zStack[tos],zStack[nos])>0; |
| 1447 | } |
| 1448 | if( copy ){ |
| 1449 | Release(p, nos); |
| 1450 | aStack[nos] = aStack[tos]; |
| 1451 | zStack[nos] = zStack[tos]; |
| 1452 | zStack[tos] = 0; |
| 1453 | aStack[tos].flags = 0; |
| 1454 | }else{ |
| 1455 | Release(p, tos); |
| 1456 | } |
| 1457 | p->tos = nos; |
| 1458 | break; |
| 1459 | } |
| 1460 | |
| 1461 | /* Opcode: Min * * * |
| 1462 | ** |
| 1463 | ** Pop the top two elements from the stack then push back the |
| 1464 | ** smaller of the two. |
| 1465 | */ |
| 1466 | case OP_Min: { |
| 1467 | int tos = p->tos; |
| 1468 | int nos = tos - 1; |
| 1469 | int ft, fn; |
| 1470 | int copy = 0; |
| 1471 | VERIFY( if( nos<0 ) goto not_enough_stack; ) |
| 1472 | ft = aStack[tos].flags; |
| 1473 | fn = aStack[nos].flags; |
| 1474 | if( fn & STK_Null ){ |
| 1475 | copy = 1; |
| 1476 | }else if( ft & STK_Null ){ |
| 1477 | copy = 0; |
| 1478 | }else if( (ft & fn & STK_Int)==STK_Int ){ |
| 1479 | copy = aStack[nos].i>aStack[tos].i; |
| 1480 | }else if( ( (ft|fn) & (STK_Int|STK_Real) ) !=0 ){ |
| 1481 | Realify(p, tos); |
| 1482 | Realify(p, nos); |
| 1483 | copy = aStack[tos].r<aStack[nos].r; |
| 1484 | }else{ |
| 1485 | if( Stringify(p, tos) || Stringify(p, nos) ) goto no_mem; |
| 1486 | copy = sqliteCompare(zStack[tos],zStack[nos])<0; |
| 1487 | } |
| 1488 | if( copy ){ |
| 1489 | Release(p, nos); |
| 1490 | aStack[nos] = aStack[tos]; |
| 1491 | zStack[nos] = zStack[tos]; |
| 1492 | zStack[tos] = 0; |
| 1493 | aStack[tos].flags = 0; |
| 1494 | }else{ |
| 1495 | Release(p, tos); |
| 1496 | } |
| 1497 | p->tos = nos; |
| 1498 | break; |
| 1499 | } |
| 1500 | |
| 1501 | /* Opcode: AddImm P1 * * |
| 1502 | ** |
| 1503 | ** Add the value P1 to whatever is on top of the stack. |
| 1504 | */ |
| 1505 | case OP_AddImm: { |
| 1506 | int tos = p->tos; |
| 1507 | VERIFY( if( tos<0 ) goto not_enough_stack; ) |
| 1508 | Integerify(p, tos); |
| 1509 | aStack[tos].i += pOp->p1; |
| 1510 | break; |
| 1511 | } |
| 1512 | |
| 1513 | /* Opcode: Eq * P2 * |
| 1514 | ** |
| 1515 | ** Pop the top two elements from the stack. If they are equal, then |
| 1516 | ** jump to instruction P2. Otherwise, continue to the next instruction. |
| 1517 | */ |
| 1518 | /* Opcode: Ne * P2 * |
| 1519 | ** |
| 1520 | ** Pop the top two elements from the stack. If they are not equal, then |
| 1521 | ** jump to instruction P2. Otherwise, continue to the next instruction. |
| 1522 | */ |
| 1523 | /* Opcode: Lt * P2 * |
| 1524 | ** |
| 1525 | ** Pop the top two elements from the stack. If second element (the |
| 1526 | ** next on stack) is less than the first (the top of stack), then |
| 1527 | ** jump to instruction P2. Otherwise, continue to the next instruction. |
| 1528 | ** In other words, jump if NOS<TOS. |
| 1529 | */ |
| 1530 | /* Opcode: Le * P2 * |
| 1531 | ** |
| 1532 | ** Pop the top two elements from the stack. If second element (the |
| 1533 | ** next on stack) is less than or equal to the first (the top of stack), |
| 1534 | ** then jump to instruction P2. In other words, jump if NOS<=TOS. |
| 1535 | */ |
| 1536 | /* Opcode: Gt * P2 * |
| 1537 | ** |
| 1538 | ** Pop the top two elements from the stack. If second element (the |
| 1539 | ** next on stack) is greater than the first (the top of stack), |
| 1540 | ** then jump to instruction P2. In other words, jump if NOS>TOS. |
| 1541 | */ |
| 1542 | /* Opcode: Ge * P2 * |
| 1543 | ** |
| 1544 | ** Pop the top two elements from the stack. If second element (the next |
| 1545 | ** on stack) is greater than or equal to the first (the top of stack), |
| 1546 | ** then jump to instruction P2. In other words, jump if NOS>=TOS. |
| 1547 | */ |
| 1548 | case OP_Eq: |
| 1549 | case OP_Ne: |
| 1550 | case OP_Lt: |
| 1551 | case OP_Le: |
| 1552 | case OP_Gt: |
| 1553 | case OP_Ge: { |
| 1554 | int tos = p->tos; |
| 1555 | int nos = tos - 1; |
| 1556 | int c; |
| 1557 | int ft, fn; |
| 1558 | VERIFY( if( nos<0 ) goto not_enough_stack; ) |
| 1559 | ft = aStack[tos].flags; |
| 1560 | fn = aStack[nos].flags; |
| 1561 | if( (ft & fn)==STK_Int ){ |
| 1562 | c = aStack[nos].i - aStack[tos].i; |
| 1563 | }else{ |
| 1564 | if( Stringify(p, tos) || Stringify(p, nos) ) goto no_mem; |
| 1565 | c = sqliteCompare(zStack[nos], zStack[tos]); |
| 1566 | } |
| 1567 | switch( pOp->opcode ){ |
| 1568 | case OP_Eq: c = c==0; break; |
| 1569 | case OP_Ne: c = c!=0; break; |
| 1570 | case OP_Lt: c = c<0; break; |
| 1571 | case OP_Le: c = c<=0; break; |
| 1572 | case OP_Gt: c = c>0; break; |
| 1573 | default: c = c>=0; break; |
| 1574 | } |
| 1575 | POPSTACK; |
| 1576 | POPSTACK; |
| 1577 | if( c ) pc = pOp->p2-1; |
| 1578 | break; |
| 1579 | } |
| 1580 | |
| 1581 | /* Opcode: Like P1 P2 * |
| 1582 | ** |
| 1583 | ** Pop the top two elements from the stack. The top-most is a |
| 1584 | ** "like" pattern -- the right operand of the SQL "LIKE" operator. |
| 1585 | ** The lower element is the string to compare against the like |
| 1586 | ** pattern. Jump to P2 if the two compare, and fall through without |
| 1587 | ** jumping if they do not. The '%' in the top-most element matches |
| 1588 | ** any sequence of zero or more characters in the lower element. The |
| 1589 | ** '_' character in the topmost matches any single character of the |
| 1590 | ** lower element. Case is ignored for this comparison. |
| 1591 | ** |
| 1592 | ** If P1 is not zero, the sense of the test is inverted and we |
| 1593 | ** have a "NOT LIKE" operator. The jump is made if the two values |
| 1594 | ** are different. |
| 1595 | */ |
| 1596 | case OP_Like: { |
| 1597 | int tos = p->tos; |
| 1598 | int nos = tos - 1; |
| 1599 | int c; |
| 1600 | VERIFY( if( nos<0 ) goto not_enough_stack; ) |
| 1601 | if( Stringify(p, tos) || Stringify(p, nos) ) goto no_mem; |
| 1602 | c = sqliteLikeCompare(zStack[tos], zStack[nos]); |
| 1603 | POPSTACK; |
| 1604 | POPSTACK; |
| 1605 | if( pOp->p1 ) c = !c; |
| 1606 | if( c ) pc = pOp->p2-1; |
| 1607 | break; |
| 1608 | } |
| 1609 | |
| 1610 | /* Opcode: Glob P1 P2 * |
| 1611 | ** |
| 1612 | ** Pop the top two elements from the stack. The top-most is a |
| 1613 | ** "glob" pattern. The lower element is the string to compare |
| 1614 | ** against the glob pattern. |
| 1615 | ** |
| 1616 | ** Jump to P2 if the two compare, and fall through without |
| 1617 | ** jumping if they do not. The '*' in the top-most element matches |
| 1618 | ** any sequence of zero or more characters in the lower element. The |
| 1619 | ** '?' character in the topmost matches any single character of the |
| 1620 | ** lower element. [...] matches a range of characters. [^...] |
| 1621 | ** matches any character not in the range. Case is significant |
| 1622 | ** for globs. |
| 1623 | ** |
| 1624 | ** If P1 is not zero, the sense of the test is inverted and we |
| 1625 | ** have a "NOT GLOB" operator. The jump is made if the two values |
| 1626 | ** are different. |
| 1627 | */ |
| 1628 | case OP_Glob: { |
| 1629 | int tos = p->tos; |
| 1630 | int nos = tos - 1; |
| 1631 | int c; |
| 1632 | VERIFY( if( nos<0 ) goto not_enough_stack; ) |
| 1633 | if( Stringify(p, tos) || Stringify(p, nos) ) goto no_mem; |
| 1634 | c = sqliteGlobCompare(zStack[tos], zStack[nos]); |
| 1635 | POPSTACK; |
| 1636 | POPSTACK; |
| 1637 | if( pOp->p1 ) c = !c; |
| 1638 | if( c ) pc = pOp->p2-1; |
| 1639 | break; |
| 1640 | } |
| 1641 | |
| 1642 | /* Opcode: And * * * |
| 1643 | ** |
| 1644 | ** Pop two values off the stack. Take the logical AND of the |
| 1645 | ** two values and push the resulting boolean value back onto the |
| 1646 | ** stack. |
| 1647 | */ |
| 1648 | /* Opcode: Or * * * |
| 1649 | ** |
| 1650 | ** Pop two values off the stack. Take the logical OR of the |
| 1651 | ** two values and push the resulting boolean value back onto the |
| 1652 | ** stack. |
| 1653 | */ |
| 1654 | case OP_And: |
| 1655 | case OP_Or: { |
| 1656 | int tos = p->tos; |
| 1657 | int nos = tos - 1; |
| 1658 | int c; |
| 1659 | VERIFY( if( nos<0 ) goto not_enough_stack; ) |
| 1660 | Integerify(p, tos); |
| 1661 | Integerify(p, nos); |
| 1662 | if( pOp->opcode==OP_And ){ |
| 1663 | c = aStack[tos].i && aStack[nos].i; |
| 1664 | }else{ |
| 1665 | c = aStack[tos].i || aStack[nos].i; |
| 1666 | } |
| 1667 | POPSTACK; |
| 1668 | Release(p, nos); |
| 1669 | aStack[nos].i = c; |
| 1670 | aStack[nos].flags = STK_Int; |
| 1671 | break; |
| 1672 | } |
| 1673 | |
| 1674 | /* Opcode: Negative * * * |
| 1675 | ** |
| 1676 | ** Treat the top of the stack as a numeric quantity. Replace it |
| 1677 | ** with its additive inverse. |
| 1678 | */ |
| 1679 | case OP_Negative: { |
| 1680 | int tos = p->tos; |
| 1681 | VERIFY( if( tos<0 ) goto not_enough_stack; ) |
| 1682 | if( aStack[tos].flags & STK_Real ){ |
| 1683 | Release(p, tos); |
| 1684 | aStack[tos].r = -aStack[tos].r; |
| 1685 | aStack[tos].flags = STK_Real; |
| 1686 | }else if( aStack[tos].flags & STK_Int ){ |
| 1687 | Release(p, tos); |
| 1688 | aStack[tos].i = -aStack[tos].i; |
| 1689 | aStack[tos].flags = STK_Int; |
| 1690 | }else{ |
| 1691 | Realify(p, tos); |
| 1692 | Release(p, tos); |
| 1693 | aStack[tos].r = -aStack[tos].r; |
| 1694 | aStack[tos].flags = STK_Real; |
| 1695 | } |
| 1696 | break; |
| 1697 | } |
| 1698 | |
| 1699 | /* Opcode: Not * * * |
| 1700 | ** |
| 1701 | ** Interpret the top of the stack as a boolean value. Replace it |
| 1702 | ** with its complement. |
| 1703 | */ |
| 1704 | case OP_Not: { |
| 1705 | int tos = p->tos; |
| 1706 | VERIFY( if( p->tos<0 ) goto not_enough_stack; ) |
| 1707 | Integerify(p, tos); |
| 1708 | Release(p, tos); |
| 1709 | aStack[tos].i = !aStack[tos].i; |
| 1710 | aStack[tos].flags = STK_Int; |
| 1711 | break; |
| 1712 | } |
| 1713 | |
| 1714 | /* Opcode: Noop * * * |
| 1715 | ** |
| 1716 | ** Do nothing. This instruction is often useful as a jump |
| 1717 | ** destination. |
| 1718 | */ |
| 1719 | case OP_Noop: { |
| 1720 | break; |
| 1721 | } |
| 1722 | |
| 1723 | /* Opcode: If * P2 * |
| 1724 | ** |
| 1725 | ** Pop a single boolean from the stack. If the boolean popped is |
| 1726 | ** true, then jump to p2. Otherwise continue to the next instruction. |
| 1727 | ** An integer is false if zero and true otherwise. A string is |
| 1728 | ** false if it has zero length and true otherwise. |
| 1729 | */ |
| 1730 | case OP_If: { |
| 1731 | int c; |
| 1732 | VERIFY( if( p->tos<0 ) goto not_enough_stack; ) |
| 1733 | Integerify(p, p->tos); |
| 1734 | c = aStack[p->tos].i; |
| 1735 | POPSTACK; |
| 1736 | if( c ) pc = pOp->p2-1; |
| 1737 | break; |
| 1738 | } |
| 1739 | |
| 1740 | /* Opcode: IsNull * P2 * |
| 1741 | ** |
| 1742 | ** Pop a single value from the stack. If the value popped is NULL |
| 1743 | ** then jump to p2. Otherwise continue to the next |
| 1744 | ** instruction. |
| 1745 | */ |
| 1746 | case OP_IsNull: { |
| 1747 | int c; |
| 1748 | VERIFY( if( p->tos<0 ) goto not_enough_stack; ) |
| 1749 | c = (aStack[p->tos].flags & STK_Null)!=0; |
| 1750 | POPSTACK; |
| 1751 | if( c ) pc = pOp->p2-1; |
| 1752 | break; |
| 1753 | } |
| 1754 | |
| 1755 | /* Opcode: NotNull * P2 * |
| 1756 | ** |
| 1757 | ** Pop a single value from the stack. If the value popped is not an |
| 1758 | ** empty string, then jump to p2. Otherwise continue to the next |
| 1759 | ** instruction. |
| 1760 | */ |
| 1761 | case OP_NotNull: { |
| 1762 | int c; |
| 1763 | VERIFY( if( p->tos<0 ) goto not_enough_stack; ) |
| 1764 | c = (aStack[p->tos].flags & STK_Null)==0; |
| 1765 | POPSTACK; |
| 1766 | if( c ) pc = pOp->p2-1; |
| 1767 | break; |
| 1768 | } |
| 1769 | |
| 1770 | /* Opcode: MakeRecord P1 * * |
| 1771 | ** |
| 1772 | ** Convert the top P1 entries of the stack into a single entry |
| 1773 | ** suitable for use as a data record in a database table. To do this |
| 1774 | ** all entries (except NULLs) are converted to strings and |
drh | 092d035 | 2001-09-15 13:15:12 +0000 | [diff] [blame] | 1775 | ** concatenated. The null-terminators are included on all string |
| 1776 | ** except for NULL columns which are represented by zero bytes. |
| 1777 | ** The lowest entry |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1778 | ** on the stack is the first in the concatenation and the top of |
| 1779 | ** the stack is the last. After all columns are concatenated, an |
drh | 092d035 | 2001-09-15 13:15:12 +0000 | [diff] [blame] | 1780 | ** index header is added. The index header consists of P1 16-bit integers |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1781 | ** which hold the offset of the beginning of each column data from the |
drh | 092d035 | 2001-09-15 13:15:12 +0000 | [diff] [blame] | 1782 | ** beginning of the completed record including the header. |
| 1783 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 1784 | ** The Column opcode is used to unpack a record manufactured with |
drh | 092d035 | 2001-09-15 13:15:12 +0000 | [diff] [blame] | 1785 | ** the opcode. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1786 | */ |
| 1787 | case OP_MakeRecord: { |
| 1788 | char *zNewRecord; |
| 1789 | int nByte; |
| 1790 | int nField; |
| 1791 | int i, j; |
drh | 092d035 | 2001-09-15 13:15:12 +0000 | [diff] [blame] | 1792 | u16 addr; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1793 | |
| 1794 | nField = pOp->p1; |
| 1795 | VERIFY( if( p->tos+1<nField ) goto not_enough_stack; ) |
| 1796 | nByte = 0; |
| 1797 | for(i=p->tos-nField+1; i<=p->tos; i++){ |
| 1798 | if( (aStack[i].flags & STK_Null)==0 ){ |
| 1799 | if( Stringify(p, i) ) goto no_mem; |
| 1800 | nByte += aStack[i].n; |
| 1801 | } |
| 1802 | } |
drh | 092d035 | 2001-09-15 13:15:12 +0000 | [diff] [blame] | 1803 | nByte += sizeof(addr)*nField; |
drh | 872ff86 | 2001-09-15 14:43:39 +0000 | [diff] [blame] | 1804 | if( nByte>MAX_BYTES_PER_ROW ){ |
drh | 092d035 | 2001-09-15 13:15:12 +0000 | [diff] [blame] | 1805 | rc = SQLITE_TOOBIG; |
| 1806 | goto abort_due_to_error; |
| 1807 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1808 | zNewRecord = sqliteMalloc( nByte ); |
| 1809 | if( zNewRecord==0 ) goto no_mem; |
| 1810 | j = 0; |
drh | 092d035 | 2001-09-15 13:15:12 +0000 | [diff] [blame] | 1811 | addr = sizeof(addr)*nField; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1812 | for(i=p->tos-nField+1; i<=p->tos; i++){ |
drh | 092d035 | 2001-09-15 13:15:12 +0000 | [diff] [blame] | 1813 | memcpy(&zNewRecord[j], (char*)&addr, sizeof(addr)); |
| 1814 | j += sizeof(addr); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1815 | if( (aStack[i].flags & STK_Null)==0 ){ |
| 1816 | addr += aStack[i].n; |
| 1817 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1818 | } |
| 1819 | for(i=p->tos-nField+1; i<=p->tos; i++){ |
| 1820 | if( (aStack[i].flags & STK_Null)==0 ){ |
| 1821 | memcpy(&zNewRecord[j], zStack[i], aStack[i].n); |
| 1822 | j += aStack[i].n; |
| 1823 | } |
| 1824 | } |
| 1825 | PopStack(p, nField); |
| 1826 | VERIFY( NeedStack(p, p->tos+1); ) |
| 1827 | p->tos++; |
| 1828 | aStack[p->tos].n = nByte; |
| 1829 | aStack[p->tos].flags = STK_Str | STK_Dyn; |
| 1830 | zStack[p->tos] = zNewRecord; |
| 1831 | break; |
| 1832 | } |
| 1833 | |
| 1834 | /* Opcode: MakeKey P1 P2 * |
| 1835 | ** |
| 1836 | ** Convert the top P1 entries of the stack into a single entry suitable |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 1837 | ** for use as the key in an index. The top P1 records are |
| 1838 | ** converted to strings and merged. The null-terminators |
| 1839 | ** are retained and used as separators. |
drh | 872ff86 | 2001-09-15 14:43:39 +0000 | [diff] [blame] | 1840 | ** The lowest entry in the stack is the first field and the top of the |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1841 | ** stack becomes the last. |
| 1842 | ** |
| 1843 | ** If P2 is not zero, then the original entries remain on the stack |
| 1844 | ** and the new key is pushed on top. If P2 is zero, the original |
| 1845 | ** data is popped off the stack first then the new key is pushed |
| 1846 | ** back in its place. |
| 1847 | ** |
| 1848 | ** See also: MakeIdxKey, SortMakeKey |
| 1849 | */ |
| 1850 | case OP_MakeKey: { |
| 1851 | char *zNewKey; |
| 1852 | int nByte; |
| 1853 | int nField; |
| 1854 | int i, j; |
| 1855 | |
| 1856 | nField = pOp->p1; |
| 1857 | VERIFY( if( p->tos+1<nField ) goto not_enough_stack; ) |
| 1858 | nByte = 0; |
| 1859 | for(i=p->tos-nField+1; i<=p->tos; i++){ |
| 1860 | if( aStack[i].flags & STK_Null ){ |
| 1861 | nByte++; |
| 1862 | }else{ |
| 1863 | if( Stringify(p, i) ) goto no_mem; |
| 1864 | nByte += aStack[i].n; |
| 1865 | } |
| 1866 | } |
drh | 872ff86 | 2001-09-15 14:43:39 +0000 | [diff] [blame] | 1867 | if( nByte+sizeof(u32)>MAX_BYTES_PER_ROW ){ |
| 1868 | rc = SQLITE_TOOBIG; |
| 1869 | goto abort_due_to_error; |
| 1870 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1871 | zNewKey = sqliteMalloc( nByte ); |
| 1872 | if( zNewKey==0 ) goto no_mem; |
| 1873 | j = 0; |
| 1874 | for(i=p->tos-nField+1; i<=p->tos; i++){ |
drh | 872ff86 | 2001-09-15 14:43:39 +0000 | [diff] [blame] | 1875 | if( aStack[i].flags & STK_Null ){ |
| 1876 | zNewKey[j++] = 0; |
| 1877 | }else{ |
| 1878 | memcpy(&zNewKey[j], zStack[i], aStack[i].n); |
| 1879 | j += aStack[i].n; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1880 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1881 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1882 | if( pOp->p2==0 ) PopStack(p, nField); |
| 1883 | VERIFY( NeedStack(p, p->tos+1); ) |
| 1884 | p->tos++; |
| 1885 | aStack[p->tos].n = nByte; |
| 1886 | aStack[p->tos].flags = STK_Str|STK_Dyn; |
| 1887 | zStack[p->tos] = zNewKey; |
| 1888 | break; |
| 1889 | } |
| 1890 | |
| 1891 | /* Opcode: MakeIdxKey P1 * * |
| 1892 | ** |
| 1893 | ** Convert the top P1 entries of the stack into a single entry suitable |
| 1894 | ** for use as the key in an index. In addition, take one additional integer |
| 1895 | ** off of the stack, treat that integer as a four-byte record number, and |
| 1896 | ** append the four bytes to the key. Thus a total of P1+1 entries are |
| 1897 | ** popped from the stack for this instruction and a single entry is pushed |
| 1898 | ** back. The first P1 entries that are popped are strings and the last |
| 1899 | ** entry (the lowest on the stack) is an integer record number. |
| 1900 | ** |
| 1901 | ** The converstion of the first P1 string entries occurs just like in |
drh | 872ff86 | 2001-09-15 14:43:39 +0000 | [diff] [blame] | 1902 | ** MakeKey. Each entry is separated from the others by a null. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1903 | ** The entire concatenation is null-terminated. The lowest entry |
| 1904 | ** in the stack is the first field and the top of the stack becomes the |
| 1905 | ** last. |
| 1906 | ** |
| 1907 | ** See also: MakeKey, SortMakeKey |
| 1908 | */ |
| 1909 | case OP_MakeIdxKey: { |
| 1910 | char *zNewKey; |
| 1911 | int nByte; |
| 1912 | int nField; |
| 1913 | int i, j; |
| 1914 | |
| 1915 | nField = pOp->p1; |
| 1916 | VERIFY( if( p->tos+1<nField ) goto not_enough_stack; ) |
drh | 092d035 | 2001-09-15 13:15:12 +0000 | [diff] [blame] | 1917 | nByte = sizeof(u32); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1918 | for(i=p->tos-nField+1; i<=p->tos; i++){ |
| 1919 | if( aStack[i].flags & STK_Null ){ |
| 1920 | nByte++; |
| 1921 | }else{ |
| 1922 | if( Stringify(p, i) ) goto no_mem; |
| 1923 | nByte += aStack[i].n; |
| 1924 | } |
| 1925 | } |
drh | 872ff86 | 2001-09-15 14:43:39 +0000 | [diff] [blame] | 1926 | if( nByte>MAX_BYTES_PER_ROW ){ |
| 1927 | rc = SQLITE_TOOBIG; |
| 1928 | goto abort_due_to_error; |
| 1929 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1930 | zNewKey = sqliteMalloc( nByte ); |
| 1931 | if( zNewKey==0 ) goto no_mem; |
| 1932 | j = 0; |
| 1933 | for(i=p->tos-nField+1; i<=p->tos; i++){ |
drh | 872ff86 | 2001-09-15 14:43:39 +0000 | [diff] [blame] | 1934 | if( aStack[i].flags & STK_Null ){ |
| 1935 | zNewKey[j++] = 0; |
| 1936 | }else{ |
| 1937 | memcpy(&zNewKey[j], zStack[i], aStack[i].n); |
| 1938 | j += aStack[i].n; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1939 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1940 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1941 | Integerify(p, p->tos-nField); |
drh | 092d035 | 2001-09-15 13:15:12 +0000 | [diff] [blame] | 1942 | memcpy(&zNewKey[j], &aStack[p->tos-nField].i, sizeof(u32)); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1943 | PopStack(p, nField+1); |
| 1944 | VERIFY( NeedStack(p, p->tos+1); ) |
| 1945 | p->tos++; |
| 1946 | aStack[p->tos].n = nByte; |
| 1947 | aStack[p->tos].flags = STK_Str|STK_Dyn; |
| 1948 | zStack[p->tos] = zNewKey; |
| 1949 | break; |
| 1950 | } |
| 1951 | |
| 1952 | /* Opcode: Transaction * * * |
| 1953 | ** |
| 1954 | ** Begin a transaction. The transaction ends when a Commit or Rollback |
| 1955 | ** opcode is encountered or whenever there is an execution error that causes |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 1956 | ** a script to abort. A transaction is not ended by a Halt. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1957 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 1958 | ** A write lock is obtained on the database file when a transaction is |
| 1959 | ** started. No other process can read or write the file while the |
| 1960 | ** transaction is underway. Starting a transaction also creates a |
| 1961 | ** rollback journal. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1962 | ** A transaction must be started before any changes can be made to the |
| 1963 | ** database. |
| 1964 | */ |
| 1965 | case OP_Transaction: { |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 1966 | rc = sqliteBtreeBeginTrans(pBt); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1967 | break; |
| 1968 | } |
| 1969 | |
| 1970 | /* Opcode: Commit * * * |
| 1971 | ** |
| 1972 | ** Cause all modifications to the database that have been made since the |
| 1973 | ** last Transaction to actually take effect. No additional modifications |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 1974 | ** are allowed until another transaction is started. The Commit instruction |
| 1975 | ** deletes the journal file and releases the write lock on the database. |
| 1976 | ** A read lock continues to be held if there are still cursors open. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1977 | */ |
| 1978 | case OP_Commit: { |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 1979 | rc = sqliteBtreeCommit(pBt); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1980 | if( rc==SQLITE_OK ){ |
| 1981 | sqliteCommitInternalChanges(db); |
| 1982 | }else{ |
| 1983 | sqliteRollbackInternalChanges(db); |
| 1984 | } |
| 1985 | break; |
| 1986 | } |
| 1987 | |
| 1988 | /* Opcode: Rollback * * * |
| 1989 | ** |
| 1990 | ** Cause all modifications to the database that have been made since the |
| 1991 | ** last Transaction to be undone. The database is restored to its state |
| 1992 | ** before the Transaction opcode was executed. No additional modifications |
| 1993 | ** are allowed until another transaction is started. |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 1994 | ** |
| 1995 | ** This instruction automatically closes all cursors and releases both |
| 1996 | ** the read and write locks on the database. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1997 | */ |
| 1998 | case OP_Rollback: { |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 1999 | rc = sqliteBtreeRollback(pBt); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2000 | sqliteRollbackInternalChanges(db); |
| 2001 | break; |
| 2002 | } |
| 2003 | |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 2004 | /* Opcode: ReadCookie * * * |
| 2005 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2006 | ** Read the schema cookie from the database file and push it onto the |
| 2007 | ** stack. The schema cookie is an integer that is used like a version |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 2008 | ** number for the database schema. Everytime the schema changes, the |
| 2009 | ** cookie changes to a new random value. This opcode is used during |
| 2010 | ** initialization to read the initial cookie value so that subsequent |
| 2011 | ** database accesses can verify that the cookie has not changed. |
| 2012 | ** |
| 2013 | ** There must be a read-lock on the database (either a transaction |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2014 | ** must be started or there must be an open cursor) before |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 2015 | ** executing this instruction. |
| 2016 | */ |
| 2017 | case OP_ReadCookie: { |
| 2018 | int i = ++p->tos; |
| 2019 | int aMeta[SQLITE_N_BTREE_META]; |
| 2020 | VERIFY( if( NeedStack(p, p->tos) ) goto no_mem; ) |
| 2021 | rc = sqliteBtreeGetMeta(pBt, aMeta); |
| 2022 | aStack[i].i = aMeta[1]; |
| 2023 | aStack[i].flags = STK_Int; |
| 2024 | break; |
| 2025 | } |
| 2026 | |
| 2027 | /* Opcode: SetCookie P1 * * |
| 2028 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2029 | ** This operation changes the value of the schema cookie on the database. |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 2030 | ** The new value is P1. |
| 2031 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2032 | ** The schema cookie changes its value whenever the database schema changes. |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 2033 | ** That way, other processes can recognize when the schema has changed |
| 2034 | ** and reread it. |
| 2035 | ** |
| 2036 | ** A transaction must be started before executing this opcode. |
| 2037 | */ |
| 2038 | case OP_SetCookie: { |
| 2039 | int aMeta[SQLITE_N_BTREE_META]; |
| 2040 | rc = sqliteBtreeGetMeta(pBt, aMeta); |
| 2041 | if( rc==SQLITE_OK ){ |
| 2042 | aMeta[1] = pOp->p1; |
| 2043 | rc = sqliteBtreeUpdateMeta(pBt, aMeta); |
| 2044 | } |
| 2045 | break; |
| 2046 | } |
| 2047 | |
| 2048 | /* Opcode: VerifyCookie P1 * * |
| 2049 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2050 | ** Check the current value of the schema cookie and make sure it is |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 2051 | ** equal to P1. If it is not, abort with an SQLITE_SCHEMA error. |
| 2052 | ** |
| 2053 | ** The cookie changes its value whenever the database schema changes. |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2054 | ** This operation is used to detect when that the cookie has changed |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 2055 | ** and that the current process needs to reread the schema. |
| 2056 | ** |
| 2057 | ** Either a transaction needs to have been started or an OP_Open needs |
| 2058 | ** to be executed (to establish a read lock) before this opcode is |
| 2059 | ** invoked. |
| 2060 | */ |
| 2061 | case OP_VerifyCookie: { |
| 2062 | int aMeta[SQLITE_N_BTREE_META]; |
| 2063 | rc = sqliteBtreeGetMeta(pBt, aMeta); |
| 2064 | if( rc==SQLITE_OK && aMeta[1]!=pOp->p1 ){ |
| 2065 | sqliteSetString(pzErrMsg, "database schema has changed", 0); |
| 2066 | rc = SQLITE_SCHEMA; |
| 2067 | } |
| 2068 | break; |
| 2069 | } |
| 2070 | |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2071 | /* Opcode: Open P1 P2 P3 |
| 2072 | ** |
| 2073 | ** Open a new cursor for the database table whose root page is |
| 2074 | ** P2 in the main database file. Give the new cursor an identifier |
| 2075 | ** of P1. The P1 values need not be contiguous but all P1 values |
| 2076 | ** should be small integers. It is an error for P1 to be negative. |
| 2077 | ** |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 2078 | ** If P2==0 then take the root page number from the top of the stack. |
| 2079 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2080 | ** There will be a read lock on the database whenever there is an |
| 2081 | ** open cursor. If the database was unlocked prior to this instruction |
| 2082 | ** then a read lock is acquired as part of this instruction. A read |
| 2083 | ** lock allows other processes to read the database but prohibits |
| 2084 | ** any other process from modifying the database. The read lock is |
| 2085 | ** released when all cursors are closed. If this instruction attempts |
| 2086 | ** to get a read lock but fails, the script terminates with an |
| 2087 | ** SQLITE_BUSY error code. |
| 2088 | ** |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2089 | ** The P3 value is the name of the table or index being opened. |
| 2090 | ** The P3 value is not actually used by this opcode and may be |
| 2091 | ** omitted. But the code generator usually inserts the index or |
| 2092 | ** table name into P3 to make the code easier to read. |
| 2093 | */ |
| 2094 | case OP_Open: { |
| 2095 | int busy = 0; |
| 2096 | int i = pOp->p1; |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 2097 | int tos = p->tos; |
| 2098 | int p2 = pOp->p2; |
| 2099 | if( p2<=0 ){ |
| 2100 | if( tos<0 ) goto not_enough_stack; |
| 2101 | Integerify(p, tos); |
| 2102 | p2 = p->aStack[tos].i; |
| 2103 | POPSTACK; |
| 2104 | if( p2<2 ){ |
| 2105 | sqliteSetString(pzErrMsg, "root page number less than 2", 0); |
| 2106 | rc = SQLITE_INTERNAL; |
| 2107 | goto cleanup; |
| 2108 | } |
| 2109 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2110 | VERIFY( if( i<0 ) goto bad_instruction; ) |
| 2111 | if( i>=p->nCursor ){ |
| 2112 | int j; |
| 2113 | p->aCsr = sqliteRealloc( p->aCsr, (i+1)*sizeof(Cursor) ); |
| 2114 | if( p->aCsr==0 ){ p->nCursor = 0; goto no_mem; } |
drh | a1b351a | 2001-09-14 16:42:12 +0000 | [diff] [blame] | 2115 | for(j=p->nCursor; j<=i; j++){ |
| 2116 | memset(&p->aCsr[j], 0, sizeof(Cursor)); |
| 2117 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2118 | p->nCursor = i+1; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2119 | } |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 2120 | cleanupCursor(&p->aCsr[i]); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2121 | memset(&p->aCsr[i], 0, sizeof(Cursor)); |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 2122 | do{ |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 2123 | rc = sqliteBtreeCursor(pBt, p2, &p->aCsr[i].pCursor); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2124 | switch( rc ){ |
| 2125 | case SQLITE_BUSY: { |
| 2126 | if( xBusy==0 || (*xBusy)(pBusyArg, pOp->p3, ++busy)==0 ){ |
| 2127 | sqliteSetString(pzErrMsg, sqliteErrStr(rc), 0); |
| 2128 | busy = 0; |
| 2129 | } |
| 2130 | break; |
| 2131 | } |
| 2132 | case SQLITE_OK: { |
| 2133 | busy = 0; |
| 2134 | break; |
| 2135 | } |
| 2136 | default: { |
| 2137 | goto abort_due_to_error; |
| 2138 | } |
| 2139 | } |
| 2140 | }while( busy ); |
| 2141 | break; |
| 2142 | } |
| 2143 | |
| 2144 | /* Opcode: OpenTemp P1 * * |
| 2145 | ** |
| 2146 | ** Open a new cursor that points to a table in a temporary database |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2147 | ** file. The temporary file is opened read/write even if the main |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2148 | ** database is read-only. The temporary file is deleted when the |
| 2149 | ** cursor is closed. |
| 2150 | */ |
| 2151 | case OP_OpenTemp: { |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2152 | int i = pOp->p1; |
| 2153 | Cursor *pCx; |
| 2154 | VERIFY( if( i<0 ) goto bad_instruction; ) |
| 2155 | if( i>=p->nCursor ){ |
| 2156 | int j; |
| 2157 | p->aCsr = sqliteRealloc( p->aCsr, (i+1)*sizeof(Cursor) ); |
| 2158 | if( p->aCsr==0 ){ p->nCursor = 0; goto no_mem; } |
drh | a1b351a | 2001-09-14 16:42:12 +0000 | [diff] [blame] | 2159 | for(j=p->nCursor; j<=i; j++){ |
| 2160 | memset(&p->aCsr[j], 0, sizeof(Cursor)); |
| 2161 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2162 | p->nCursor = i+1; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2163 | } |
| 2164 | pCx = &p->aCsr[i]; |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 2165 | cleanupCursor(pCx); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2166 | memset(pCx, 0, sizeof(*pCx)); |
drh | a1b351a | 2001-09-14 16:42:12 +0000 | [diff] [blame] | 2167 | rc = sqliteBtreeOpen(0, 0, TEMP_PAGES, &pCx->pBt); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2168 | if( rc==SQLITE_OK ){ |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 2169 | rc = sqliteBtreeCursor(pCx->pBt, 2, &pCx->pCursor); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2170 | } |
| 2171 | if( rc==SQLITE_OK ){ |
| 2172 | rc = sqliteBtreeBeginTrans(pCx->pBt); |
| 2173 | } |
| 2174 | break; |
| 2175 | } |
| 2176 | |
| 2177 | /* Opcode: Close P1 * * |
| 2178 | ** |
| 2179 | ** Close a cursor previously opened as P1. If P1 is not |
| 2180 | ** currently open, this instruction is a no-op. |
| 2181 | */ |
| 2182 | case OP_Close: { |
| 2183 | int i = pOp->p1; |
| 2184 | if( i>=0 && i<p->nCursor && p->aCsr[i].pCursor ){ |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 2185 | cleanupCursor(&p->aCsr[i]); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2186 | } |
| 2187 | break; |
| 2188 | } |
| 2189 | |
| 2190 | /* Opcode: MoveTo P1 * * |
| 2191 | ** |
| 2192 | ** Pop the top of the stack and use its value as a key. Reposition |
| 2193 | ** cursor P1 so that it points to an entry with a matching key. If |
| 2194 | ** the table contains no record with a matching key, then the cursor |
| 2195 | ** is left pointing at a nearby record. |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2196 | ** |
| 2197 | ** See also: Found, NotFound, Distinct |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2198 | */ |
| 2199 | case OP_MoveTo: { |
| 2200 | int i = pOp->p1; |
| 2201 | int tos = p->tos; |
| 2202 | VERIFY( if( tos<0 ) goto not_enough_stack; ) |
| 2203 | if( i>=0 && i<p->nCursor && p->aCsr[i].pCursor ){ |
| 2204 | int res; |
| 2205 | if( aStack[tos].flags & STK_Int ){ |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 2206 | sqliteBtreeMoveto(p->aCsr[i].pCursor, |
| 2207 | (char*)&aStack[tos].i, sizeof(int), &res); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2208 | p->aCsr[i].lastRecno = aStack[tos].i; |
| 2209 | p->aCsr[i].recnoIsValid = 1; |
| 2210 | }else{ |
| 2211 | if( Stringify(p, tos) ) goto no_mem; |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 2212 | sqliteBtreeMoveto(p->aCsr[i].pCursor, zStack[tos], aStack[tos].n, &res); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2213 | p->aCsr[i].recnoIsValid = 0; |
| 2214 | } |
| 2215 | p->nFetch++; |
| 2216 | } |
| 2217 | POPSTACK; |
| 2218 | break; |
| 2219 | } |
| 2220 | |
| 2221 | /* Opcode: Fcnt * * * |
| 2222 | ** |
| 2223 | ** Push an integer onto the stack which is the total number of |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2224 | ** MoveTo opcodes that have been executed by this virtual machine. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2225 | ** |
| 2226 | ** This instruction is used to implement the special fcnt() function |
| 2227 | ** in the SQL dialect that SQLite understands. fcnt() is used for |
| 2228 | ** testing purposes. |
| 2229 | */ |
| 2230 | case OP_Fcnt: { |
| 2231 | int i = ++p->tos; |
| 2232 | VERIFY( if( NeedStack(p, p->tos) ) goto no_mem; ) |
| 2233 | aStack[i].i = p->nFetch; |
| 2234 | aStack[i].flags = STK_Int; |
| 2235 | break; |
| 2236 | } |
| 2237 | |
| 2238 | /* Opcode: Distinct P1 P2 * |
| 2239 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2240 | ** Use the top of the stack as a key. If a record with that key does |
| 2241 | ** not exist in the table of cursor P1, then jump to P2. If the record |
drh | 3fc190c | 2001-09-14 03:24:23 +0000 | [diff] [blame] | 2242 | ** does already exist, then fall thru. The cursor is left pointing |
| 2243 | ** at the record if it exists. The key is not popped from the stack. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2244 | ** |
| 2245 | ** This operation is similar to NotFound except that this operation |
| 2246 | ** does not pop the key from the stack. |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2247 | ** |
| 2248 | ** See also: Found, NotFound, MoveTo |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2249 | */ |
| 2250 | /* Opcode: Found P1 P2 * |
| 2251 | ** |
| 2252 | ** Use the top of the stack as a key. If a record with that key |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2253 | ** does exist in table of P1, then jump to P2. If the record |
drh | 3fc190c | 2001-09-14 03:24:23 +0000 | [diff] [blame] | 2254 | ** does not exist, then fall thru. The cursor is left pointing |
| 2255 | ** to the record if it exists. The key is popped from the stack. |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2256 | ** |
| 2257 | ** See also: Distinct, NotFound, MoveTo |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2258 | */ |
| 2259 | /* Opcode: NotFound P1 P2 * |
| 2260 | ** |
| 2261 | ** Use the top of the stack as a key. If a record with that key |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2262 | ** does not exist in table of P1, then jump to P2. If the record |
drh | 3fc190c | 2001-09-14 03:24:23 +0000 | [diff] [blame] | 2263 | ** does exist, then fall thru. The cursor is left pointing to the |
| 2264 | ** record if it exists. The key is popped from the stack. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2265 | ** |
| 2266 | ** The difference between this operation and Distinct is that |
| 2267 | ** Distinct does not pop the key from the stack. |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2268 | ** |
| 2269 | ** See also: Distinct, Found, MoveTo |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2270 | */ |
| 2271 | case OP_Distinct: |
| 2272 | case OP_NotFound: |
| 2273 | case OP_Found: { |
| 2274 | int i = pOp->p1; |
| 2275 | int tos = p->tos; |
| 2276 | int alreadyExists = 0; |
| 2277 | VERIFY( if( tos<0 ) goto not_enough_stack; ) |
| 2278 | if( VERIFY( i>=0 && i<p->nCursor && ) p->aCsr[i].pCursor ){ |
| 2279 | int res, rx; |
| 2280 | if( aStack[tos].flags & STK_Int ){ |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 2281 | rx = sqliteBtreeMoveto(p->aCsr[i].pCursor, |
| 2282 | (char*)&aStack[tos].i, sizeof(int), &res); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2283 | }else{ |
| 2284 | if( Stringify(p, tos) ) goto no_mem; |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 2285 | rx = sqliteBtreeMoveto(p->aCsr[i].pCursor, |
| 2286 | zStack[tos], aStack[tos].n, &res); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2287 | } |
| 2288 | alreadyExists = rx==SQLITE_OK && res==0; |
| 2289 | } |
| 2290 | if( pOp->opcode==OP_Found ){ |
| 2291 | if( alreadyExists ) pc = pOp->p2 - 1; |
| 2292 | }else{ |
| 2293 | if( !alreadyExists ) pc = pOp->p2 - 1; |
| 2294 | } |
| 2295 | if( pOp->opcode!=OP_Distinct ){ |
| 2296 | POPSTACK; |
| 2297 | } |
| 2298 | break; |
| 2299 | } |
| 2300 | |
| 2301 | /* Opcode: NewRecno P1 * * |
| 2302 | ** |
| 2303 | ** Get a new integer record number used as the key to a table. |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2304 | ** The record number is not previously used as a key in the database |
| 2305 | ** table that cursor P1 points to. The new record number pushed |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2306 | ** onto the stack. |
| 2307 | */ |
| 2308 | case OP_NewRecno: { |
| 2309 | int i = pOp->p1; |
drh | a1b351a | 2001-09-14 16:42:12 +0000 | [diff] [blame] | 2310 | int v = 0; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2311 | if( VERIFY( i<0 || i>=p->nCursor || ) p->aCsr[i].pCursor==0 ){ |
| 2312 | v = 0; |
| 2313 | }else{ |
| 2314 | int res, rx, cnt; |
drh | a1b351a | 2001-09-14 16:42:12 +0000 | [diff] [blame] | 2315 | static int x = 0; |
| 2316 | union { |
| 2317 | char zBuf[sizeof(int)]; |
| 2318 | int i; |
| 2319 | } ux; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2320 | cnt = 0; |
| 2321 | do{ |
drh | a1b351a | 2001-09-14 16:42:12 +0000 | [diff] [blame] | 2322 | if( x==0 || cnt>5 ){ |
| 2323 | x = sqliteRandomInteger(); |
drh | 3fc190c | 2001-09-14 03:24:23 +0000 | [diff] [blame] | 2324 | }else{ |
drh | a1b351a | 2001-09-14 16:42:12 +0000 | [diff] [blame] | 2325 | x += sqliteRandomByte() + 1; |
drh | 3fc190c | 2001-09-14 03:24:23 +0000 | [diff] [blame] | 2326 | } |
drh | a1b351a | 2001-09-14 16:42:12 +0000 | [diff] [blame] | 2327 | if( x==0 ) continue; |
| 2328 | ux.zBuf[3] = x&0xff; |
| 2329 | ux.zBuf[2] = (x>>8)&0xff; |
| 2330 | ux.zBuf[1] = (x>>16)&0xff; |
| 2331 | ux.zBuf[0] = (x>>24)&0xff; |
| 2332 | v = ux.i; |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 2333 | rx = sqliteBtreeMoveto(p->aCsr[i].pCursor, &v, sizeof(v), &res); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2334 | cnt++; |
drh | 1eaa269 | 2001-09-18 02:02:23 +0000 | [diff] [blame^] | 2335 | }while( cnt<200 && rx==SQLITE_OK && res==0 ); |
| 2336 | if( rx==SQLITE_OK && res==0 ){ |
| 2337 | rc = SQLITE_FULL; |
| 2338 | goto abort_due_to_error; |
| 2339 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2340 | } |
| 2341 | VERIFY( NeedStack(p, p->tos+1); ) |
| 2342 | p->tos++; |
| 2343 | aStack[p->tos].i = v; |
| 2344 | aStack[p->tos].flags = STK_Int; |
| 2345 | break; |
| 2346 | } |
| 2347 | |
| 2348 | /* Opcode: Put P1 * * |
| 2349 | ** |
| 2350 | ** Write an entry into the database file P1. A new entry is |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2351 | ** created if it doesn't already exist or the data for an existing |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2352 | ** entry is overwritten. The data is the value on the top of the |
| 2353 | ** stack. The key is the next value down on the stack. The stack |
| 2354 | ** is popped twice by this instruction. |
| 2355 | */ |
| 2356 | case OP_Put: { |
| 2357 | int tos = p->tos; |
| 2358 | int nos = p->tos-1; |
| 2359 | int i = pOp->p1; |
| 2360 | VERIFY( if( nos<0 ) goto not_enough_stack; ) |
| 2361 | if( VERIFY( i>=0 && i<p->nCursor && ) p->aCsr[i].pCursor!=0 ){ |
| 2362 | char *zKey; |
| 2363 | int nKey; |
| 2364 | if( (aStack[nos].flags & STK_Int)==0 ){ |
| 2365 | if( Stringify(p, nos) ) goto no_mem; |
| 2366 | nKey = aStack[nos].n; |
| 2367 | zKey = zStack[nos]; |
| 2368 | }else{ |
| 2369 | nKey = sizeof(int); |
| 2370 | zKey = (char*)&aStack[nos].i; |
| 2371 | } |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 2372 | rc = sqliteBtreeInsert(p->aCsr[i].pCursor, zKey, nKey, |
| 2373 | zStack[tos], aStack[tos].n); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2374 | } |
| 2375 | POPSTACK; |
| 2376 | POPSTACK; |
| 2377 | break; |
| 2378 | } |
| 2379 | |
| 2380 | /* Opcode: Delete P1 * * |
| 2381 | ** |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 2382 | ** Delete the record at which the P1 cursor is currently pointing. |
| 2383 | ** |
| 2384 | ** The cursor will be left pointing at either the next or the previous |
| 2385 | ** record in the table. If it is left pointing at the next record, then |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2386 | ** the next Next instruction will be a no-op. Hence it is OK to delete |
| 2387 | ** a record from within an Next loop. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2388 | */ |
| 2389 | case OP_Delete: { |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2390 | int i = pOp->p1; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2391 | if( VERIFY( i>=0 && i<p->nCursor && ) p->aCsr[i].pCursor!=0 ){ |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 2392 | rc = sqliteBtreeDelete(p->aCsr[i].pCursor); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2393 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2394 | break; |
| 2395 | } |
| 2396 | |
| 2397 | /* Opcode: KeyAsData P1 P2 * |
| 2398 | ** |
| 2399 | ** Turn the key-as-data mode for cursor P1 either on (if P2==1) or |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2400 | ** off (if P2==0). In key-as-data mode, the Field opcode pulls |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2401 | ** data off of the key rather than the data. This is useful for |
| 2402 | ** processing compound selects. |
| 2403 | */ |
| 2404 | case OP_KeyAsData: { |
| 2405 | int i = pOp->p1; |
| 2406 | if( VERIFY( i>=0 && i<p->nCursor && ) p->aCsr[i].pCursor!=0 ){ |
| 2407 | p->aCsr[i].keyAsData = pOp->p2; |
| 2408 | } |
| 2409 | break; |
| 2410 | } |
| 2411 | |
| 2412 | /* Opcode: Column P1 P2 * |
| 2413 | ** |
drh | 092d035 | 2001-09-15 13:15:12 +0000 | [diff] [blame] | 2414 | ** Interpret the data that cursor P1 points to as |
| 2415 | ** a structure built using the MakeRecord instruction. |
| 2416 | ** (See the MakeRecord opcode for additional information about |
| 2417 | ** the format of the data.) |
| 2418 | ** Push onto the stack the value of the P2-th column contained |
| 2419 | ** in the data. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2420 | ** |
| 2421 | ** If the KeyAsData opcode has previously executed on this cursor, |
| 2422 | ** then the field might be extracted from the key rather than the |
| 2423 | ** data. |
| 2424 | */ |
| 2425 | case OP_Column: { |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2426 | int amt, offset, nCol, payloadSize; |
drh | 092d035 | 2001-09-15 13:15:12 +0000 | [diff] [blame] | 2427 | u16 aHdr[10]; |
drh | d78eeee | 2001-09-13 16:18:53 +0000 | [diff] [blame] | 2428 | static const int mxHdr = sizeof(aHdr)/sizeof(aHdr[0]); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2429 | int i = pOp->p1; |
| 2430 | int p2 = pOp->p2; |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 2431 | int tos = p->tos+1; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2432 | BtCursor *pCrsr; |
| 2433 | char *z; |
| 2434 | |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 2435 | VERIFY( if( NeedStack(p, tos+1) ) goto no_mem; ) |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2436 | if( VERIFY( i>=0 && i<p->nCursor && ) (pCrsr = p->aCsr[i].pCursor)!=0 ){ |
| 2437 | int (*xSize)(BtCursor*, int*); |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 2438 | int (*xRead)(BtCursor*, int, int, char*); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2439 | |
| 2440 | /* Use different access functions depending on whether the information |
| 2441 | ** is coming from the key or the data of the record. |
| 2442 | */ |
| 2443 | if( p->aCsr[i].keyAsData ){ |
| 2444 | xSize = sqliteBtreeKeySize; |
| 2445 | xRead = sqliteBtreeKey; |
| 2446 | }else{ |
| 2447 | xSize = sqliteBtreeDataSize; |
| 2448 | xRead = sqliteBtreeData; |
| 2449 | } |
| 2450 | |
| 2451 | /* |
| 2452 | ** The code is complicated by efforts to minimize the number |
| 2453 | ** of invocations of xRead() since that call can be expensive. |
| 2454 | ** For the common case where P2 is small, xRead() is invoked |
| 2455 | ** twice. For larger values of P2, it has to be called |
| 2456 | ** three times. |
| 2457 | */ |
| 2458 | (*xSize)(pCrsr, &payloadSize); |
drh | 092d035 | 2001-09-15 13:15:12 +0000 | [diff] [blame] | 2459 | if( payloadSize < sizeof(aHdr[0])*(p2+1) ){ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2460 | rc = SQLITE_CORRUPT; |
| 2461 | goto abort_due_to_error; |
| 2462 | } |
| 2463 | if( p2+1<mxHdr ){ |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 2464 | (*xRead)(pCrsr, 0, sizeof(aHdr[0])*(p2+2), (char*)aHdr); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2465 | nCol = aHdr[0]; |
drh | 092d035 | 2001-09-15 13:15:12 +0000 | [diff] [blame] | 2466 | nCol /= sizeof(aHdr[0]); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2467 | offset = aHdr[p2]; |
| 2468 | if( p2 == nCol-1 ){ |
| 2469 | amt = payloadSize - offset; |
| 2470 | }else{ |
| 2471 | amt = aHdr[p2+1] - offset; |
| 2472 | } |
| 2473 | }else{ |
drh | 092d035 | 2001-09-15 13:15:12 +0000 | [diff] [blame] | 2474 | sqliteBtreeData(pCrsr, 0, sizeof(aHdr[0]), (char*)aHdr); |
| 2475 | nCol = aHdr[0]/sizeof(aHdr[0]); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2476 | if( p2 == nCol-1 ){ |
drh | 092d035 | 2001-09-15 13:15:12 +0000 | [diff] [blame] | 2477 | (*xRead)(pCrsr, sizeof(aHdr[0])*p2, sizeof(aHdr[0]), (char*)aHdr); |
| 2478 | offset = aHdr[0]; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2479 | amt = payloadSize - offset; |
| 2480 | }else{ |
drh | 092d035 | 2001-09-15 13:15:12 +0000 | [diff] [blame] | 2481 | (*xRead)(pCrsr, sizeof(aHdr[0])*p2, sizeof(aHdr[0])*2, (char*)aHdr); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2482 | offset = aHdr[0]; |
| 2483 | amt = aHdr[1] - offset; |
| 2484 | } |
| 2485 | } |
| 2486 | if( payloadSize < nCol || amt<0 || offset<0 ){ |
| 2487 | rc = SQLITE_CORRUPT; |
| 2488 | goto abort_due_to_error; |
| 2489 | } |
drh | 092d035 | 2001-09-15 13:15:12 +0000 | [diff] [blame] | 2490 | |
| 2491 | /* amt and offset now hold the offset to the start of data and the |
| 2492 | ** amount of data. Go get the data and put it on the stack. |
| 2493 | */ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2494 | if( amt==0 ){ |
| 2495 | aStack[tos].flags = STK_Null; |
| 2496 | }else{ |
| 2497 | z = sqliteMalloc( amt ); |
| 2498 | if( z==0 ) goto no_mem; |
| 2499 | (*xRead)(pCrsr, offset, amt, z); |
| 2500 | aStack[tos].flags = STK_Str | STK_Dyn; |
| 2501 | zStack[tos] = z; |
| 2502 | aStack[tos].n = amt; |
| 2503 | } |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 2504 | p->tos = tos; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2505 | } |
| 2506 | break; |
| 2507 | } |
| 2508 | |
| 2509 | /* Opcode: Recno P1 * * |
| 2510 | ** |
| 2511 | ** Push onto the stack an integer which is the first 4 bytes of the |
| 2512 | ** the key to the current entry in a sequential scan of the database |
| 2513 | ** file P1. The sequential scan should have been started using the |
| 2514 | ** Next opcode. |
| 2515 | */ |
| 2516 | case OP_Recno: { |
| 2517 | int i = pOp->p1; |
| 2518 | int tos = ++p->tos; |
| 2519 | BtCursor *pCrsr; |
| 2520 | |
| 2521 | VERIFY( if( NeedStack(p, p->tos) ) goto no_mem; ) |
| 2522 | if( VERIFY( i>=0 && i<p->nCursor && ) (pCrsr = p->aCsr[i].pCursor)!=0 ){ |
| 2523 | int v; |
| 2524 | if( p->aCsr[i].recnoIsValid ){ |
| 2525 | v = p->aCsr[i].lastRecno; |
| 2526 | }else{ |
drh | 092d035 | 2001-09-15 13:15:12 +0000 | [diff] [blame] | 2527 | sqliteBtreeKey(pCrsr, 0, sizeof(u32), (char*)&v); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2528 | } |
| 2529 | aStack[tos].i = v; |
| 2530 | aStack[tos].flags = STK_Int; |
| 2531 | } |
| 2532 | break; |
| 2533 | } |
| 2534 | |
| 2535 | /* Opcode: FullKey P1 * * |
| 2536 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2537 | ** Extract the complete key from the record that cursor P1 is currently |
| 2538 | ** pointing to and push the key onto the stack as a string. |
| 2539 | ** |
| 2540 | ** Compare this opcode to Recno. The Recno opcode extracts the first |
| 2541 | ** 4 bytes of the key and pushes those bytes onto the stack as an |
| 2542 | ** integer. This instruction pushes the entire key as a string. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2543 | */ |
| 2544 | case OP_FullKey: { |
| 2545 | int i = pOp->p1; |
| 2546 | int tos = ++p->tos; |
| 2547 | BtCursor *pCrsr; |
| 2548 | |
| 2549 | VERIFY( if( NeedStack(p, p->tos) ) goto no_mem; ) |
| 2550 | VERIFY( if( !p->aCsr[i].keyAsData ) goto bad_instruction; ) |
| 2551 | if( VERIFY( i>=0 && i<p->nCursor && ) (pCrsr = p->aCsr[i].pCursor)!=0 ){ |
| 2552 | int amt; |
| 2553 | char *z; |
| 2554 | |
| 2555 | sqliteBtreeKeySize(pCrsr, &amt); |
| 2556 | if( amt<=0 ){ |
| 2557 | rc = SQLITE_CORRUPT; |
| 2558 | goto abort_due_to_error; |
| 2559 | } |
| 2560 | z = sqliteMalloc( amt ); |
| 2561 | sqliteBtreeKey(pCrsr, 0, amt, z); |
| 2562 | zStack[tos] = z; |
| 2563 | aStack[tos].flags = STK_Str | STK_Dyn; |
| 2564 | aStack[tos].n = amt; |
| 2565 | } |
| 2566 | break; |
| 2567 | } |
| 2568 | |
| 2569 | /* Opcode: Rewind P1 * * |
| 2570 | ** |
| 2571 | ** The next use of the Recno or Column or Next instruction for P1 |
| 2572 | ** will refer to the first entry in the database file. |
| 2573 | */ |
| 2574 | case OP_Rewind: { |
| 2575 | int i = pOp->p1; |
| 2576 | BtCursor *pCrsr; |
| 2577 | |
| 2578 | if( VERIFY( i>=0 && i<p->nCursor && ) (pCrsr = p->aCsr[i].pCursor)!=0 ){ |
| 2579 | int res; |
| 2580 | sqliteBtreeFirst(pCrsr, &res); |
| 2581 | p->aCsr[i].atFirst = res==0; |
| 2582 | } |
| 2583 | break; |
| 2584 | } |
| 2585 | |
| 2586 | /* Opcode: Next P1 P2 * |
| 2587 | ** |
| 2588 | ** Advance cursor P1 so that it points to the next key/data pair in its |
| 2589 | ** table. Or, if there are no more key/data pairs, jump to location P2. |
| 2590 | */ |
| 2591 | case OP_Next: { |
| 2592 | int i = pOp->p1; |
| 2593 | BtCursor *pCrsr; |
| 2594 | |
| 2595 | if( VERIFY( i>=0 && i<p->nCursor && ) (pCrsr = p->aCsr[i].pCursor)!=0 ){ |
| 2596 | if( !p->aCsr[i].atFirst ){ |
| 2597 | int res; |
| 2598 | sqliteBtreeNext(pCrsr, &res); |
| 2599 | if( res ){ |
| 2600 | pc = pOp->p2 - 1; |
| 2601 | }else{ |
| 2602 | p->nFetch++; |
| 2603 | } |
| 2604 | } |
| 2605 | p->aCsr[i].atFirst = 0; |
| 2606 | p->aCsr[i].recnoIsValid = 0; |
| 2607 | } |
| 2608 | break; |
| 2609 | } |
| 2610 | |
| 2611 | /* Opcode: BeginIdx P1 * * |
| 2612 | ** |
| 2613 | ** Begin searching an index for records with the key found on the |
| 2614 | ** top of the stack. The key on the top of the stack should be built |
| 2615 | ** using the MakeKey opcode. Subsequent calls to NextIdx will push |
| 2616 | ** record numbers onto the stack until all records with the same key |
| 2617 | ** have been returned. |
| 2618 | ** |
| 2619 | ** Note that the key for this opcode should be built using MakeKey |
| 2620 | ** but the key used for PutIdx and DeleteIdx should be built using |
| 2621 | ** MakeIdxKey. The difference is that MakeIdxKey adds a 4-bytes |
| 2622 | ** record number to the end of the key in order to specify a particular |
drh | 872ff86 | 2001-09-15 14:43:39 +0000 | [diff] [blame] | 2623 | ** entry in the index. MakeKey omits the 4-byte record number. |
| 2624 | ** The search that this BeginIdx instruction initiates will span all |
| 2625 | ** entries in the index where the MakeKey generated key matches all |
| 2626 | ** but the last four bytes of the MakeIdxKey generated key. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2627 | */ |
| 2628 | case OP_BeginIdx: { |
| 2629 | int i = pOp->p1; |
| 2630 | int tos = p->tos; |
| 2631 | int res, rx; |
| 2632 | Cursor *pCrsr; |
| 2633 | VERIFY( if( tos<0 ) goto not_enough_stack; ) |
| 2634 | if( i>=0 && i<p->nCursor && (pCrsr = &p->aCsr[i])->pCursor!=0 ){ |
| 2635 | if( Stringify(p, tos) ) goto no_mem; |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 2636 | if( pCrsr->zKey ) sqliteFree(pCrsr->zKey); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2637 | pCrsr->nKey = aStack[tos].n; |
drh | 872ff86 | 2001-09-15 14:43:39 +0000 | [diff] [blame] | 2638 | pCrsr->zKey = sqliteMalloc( 2*pCrsr->nKey ); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2639 | if( pCrsr->zKey==0 ) goto no_mem; |
drh | 872ff86 | 2001-09-15 14:43:39 +0000 | [diff] [blame] | 2640 | pCrsr->zBuf = &pCrsr->zKey[pCrsr->nKey]; |
| 2641 | memcpy(pCrsr->zKey, zStack[tos], aStack[tos].n); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2642 | pCrsr->zKey[aStack[tos].n] = 0; |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 2643 | rx = sqliteBtreeMoveto(pCrsr->pCursor, zStack[tos], aStack[tos].n, &res); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2644 | pCrsr->atFirst = rx==SQLITE_OK && res>0; |
| 2645 | pCrsr->recnoIsValid = 0; |
| 2646 | } |
| 2647 | POPSTACK; |
| 2648 | break; |
| 2649 | } |
| 2650 | |
| 2651 | /* Opcode: NextIdx P1 P2 * |
| 2652 | ** |
| 2653 | ** The P1 cursor points to an SQL index for which a BeginIdx operation |
drh | 872ff86 | 2001-09-15 14:43:39 +0000 | [diff] [blame] | 2654 | ** has been issued. This operation retrieves the next record from that |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2655 | ** cursor and verifies that the key on the record minus the last 4 bytes |
| 2656 | ** matches the key that was pulled from the stack by the BeginIdx instruction. |
| 2657 | ** If they match, then the last 4 bytes of the key on the record hold a record |
drh | 872ff86 | 2001-09-15 14:43:39 +0000 | [diff] [blame] | 2658 | ** number and that record number is extracted and pushed on the stack. |
| 2659 | ** If the keys do not match, there is an immediate jump to instruction P2. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2660 | */ |
| 2661 | case OP_NextIdx: { |
| 2662 | int i = pOp->p1; |
| 2663 | int tos = ++p->tos; |
| 2664 | Cursor *pCrsr; |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 2665 | BtCursor *pCur; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2666 | int rx, res, size; |
| 2667 | |
| 2668 | VERIFY( if( NeedStack(p, p->tos) ) goto no_mem; ) |
| 2669 | zStack[tos] = 0; |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 2670 | if( VERIFY( i>=0 && i<p->nCursor && ) (pCrsr = &p->aCsr[i])->pCursor!=0 ){ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2671 | pCur = pCrsr->pCursor; |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 2672 | if( pCrsr->atFirst ){ |
| 2673 | pCrsr->atFirst = 0; |
| 2674 | res = 0; |
| 2675 | }else{ |
| 2676 | rx = sqliteBtreeNext(pCur, &res); |
| 2677 | if( rx!=SQLITE_OK ) goto abort_due_to_error; |
| 2678 | } |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 2679 | sqliteBtreeKeySize(pCur, &size); |
drh | 092d035 | 2001-09-15 13:15:12 +0000 | [diff] [blame] | 2680 | if( res>0 || size!=pCrsr->nKey+sizeof(u32) || |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2681 | sqliteBtreeKey(pCur, 0, pCrsr->nKey, pCrsr->zBuf)!=pCrsr->nKey || |
drh | 872ff86 | 2001-09-15 14:43:39 +0000 | [diff] [blame] | 2682 | memcmp(pCrsr->zKey, pCrsr->zBuf, pCrsr->nKey)!=0 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2683 | ){ |
| 2684 | pc = pOp->p2 - 1; |
| 2685 | POPSTACK; |
| 2686 | }else{ |
| 2687 | int recno; |
drh | 092d035 | 2001-09-15 13:15:12 +0000 | [diff] [blame] | 2688 | sqliteBtreeKey(pCur, pCrsr->nKey, sizeof(u32), (char*)&recno); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2689 | p->aCsr[i].lastRecno = aStack[tos].i = recno; |
| 2690 | p->aCsr[i].recnoIsValid = 1; |
| 2691 | aStack[tos].flags = STK_Int; |
| 2692 | } |
| 2693 | } |
| 2694 | break; |
| 2695 | } |
| 2696 | |
| 2697 | /* Opcode: PutIdx P1 * * |
| 2698 | ** |
| 2699 | ** The top of the stack hold an SQL index key made using the |
| 2700 | ** MakeIdxKey instruction. This opcode writes that key into the |
| 2701 | ** index P1. Data for the entry is nil. |
| 2702 | */ |
| 2703 | case OP_PutIdx: { |
| 2704 | int i = pOp->p1; |
| 2705 | int tos = p->tos; |
| 2706 | BtCursor *pCrsr; |
| 2707 | VERIFY( if( tos<0 ) goto not_enough_stack; ) |
| 2708 | if( VERIFY( i>=0 && i<p->nCursor && ) (pCrsr = p->aCsr[i].pCursor)!=0 ){ |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 2709 | sqliteBtreeInsert(pCrsr, zStack[tos], aStack[tos].n, "", 0); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2710 | } |
| 2711 | POPSTACK; |
| 2712 | break; |
| 2713 | } |
| 2714 | |
| 2715 | /* Opcode: DeleteIdx P1 * * |
| 2716 | ** |
| 2717 | ** The top of the stack is an index key built using the MakeIdxKey opcode. |
| 2718 | ** This opcode removes that entry from the index. |
| 2719 | */ |
| 2720 | case OP_DeleteIdx: { |
| 2721 | int i = pOp->p1; |
| 2722 | int tos = p->tos; |
| 2723 | BtCursor *pCrsr; |
| 2724 | VERIFY( if( tos<0 ) goto not_enough_stack; ) |
| 2725 | if( VERIFY( i>=0 && i<p->nCursor && ) (pCrsr = p->aCsr[i].pCursor)!=0 ){ |
| 2726 | int rx, res; |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 2727 | rx = sqliteBtreeMoveto(pCrsr, zStack[tos], aStack[tos].n, &res); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2728 | if( rx==SQLITE_OK && res==0 ){ |
| 2729 | sqliteBtreeDelete(pCrsr); |
| 2730 | } |
| 2731 | } |
| 2732 | POPSTACK; |
| 2733 | break; |
| 2734 | } |
| 2735 | |
| 2736 | /* Opcode: Destroy P1 * * |
| 2737 | ** |
| 2738 | ** Delete an entire database table or index whose root page in the database |
| 2739 | ** file is given by P1. |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2740 | ** |
| 2741 | ** See also: Clear |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2742 | */ |
| 2743 | case OP_Destroy: { |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 2744 | sqliteBtreeDropTable(pBt, pOp->p1); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2745 | break; |
| 2746 | } |
| 2747 | |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 2748 | /* Opcode: Clear P1 * * |
| 2749 | ** |
| 2750 | ** Delete all contents of the database table or index whose root page |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2751 | ** in the database file is given by P1. But, unlike Destroy, do not |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 2752 | ** remove the table or index from the database file. |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2753 | ** |
| 2754 | ** See also: Destroy |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 2755 | */ |
| 2756 | case OP_Clear: { |
| 2757 | sqliteBtreeClearTable(pBt, pOp->p1); |
| 2758 | break; |
| 2759 | } |
| 2760 | |
drh | 5b2fd56 | 2001-09-13 15:21:31 +0000 | [diff] [blame] | 2761 | /* Opcode: CreateTable * * * |
| 2762 | ** |
| 2763 | ** Allocate a new table in the main database file. Push the page number |
| 2764 | ** for the root page of the new table onto the stack. |
| 2765 | ** |
| 2766 | ** The root page number is also written to a memory location which has |
| 2767 | ** be set up by the parser. The difference between CreateTable and |
| 2768 | ** CreateIndex is that each writes its root page number into a different |
| 2769 | ** memory location. This writing of the page number into a memory location |
| 2770 | ** is used by the SQL parser to record the page number in its internal |
| 2771 | ** data structures. |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2772 | ** |
| 2773 | ** See also: CreateIndex |
drh | 5b2fd56 | 2001-09-13 15:21:31 +0000 | [diff] [blame] | 2774 | */ |
| 2775 | case OP_CreateTable: { |
| 2776 | int i = ++p->tos; |
| 2777 | int pgno; |
| 2778 | VERIFY( if( NeedStack(p, p->tos) ) goto no_mem; ) |
| 2779 | if( p->pTableRoot==0 ){ |
| 2780 | rc = SQLITE_INTERNAL; |
| 2781 | goto abort_due_to_error; |
| 2782 | } |
| 2783 | rc = sqliteBtreeCreateTable(pBt, &pgno); |
| 2784 | if( rc==SQLITE_OK ){ |
| 2785 | aStack[i].i = pgno; |
| 2786 | aStack[i].flags = STK_Int; |
| 2787 | *p->pTableRoot = pgno; |
| 2788 | p->pTableRoot = 0; |
| 2789 | } |
| 2790 | break; |
| 2791 | } |
| 2792 | |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2793 | /* Opcode: CreateIndex P1 * * |
drh | 5b2fd56 | 2001-09-13 15:21:31 +0000 | [diff] [blame] | 2794 | ** |
| 2795 | ** Allocate a new Index in the main database file. Push the page number |
| 2796 | ** for the root page of the new table onto the stack. |
| 2797 | ** |
| 2798 | ** The root page number is also written to a memory location which has |
| 2799 | ** be set up by the parser. The difference between CreateTable and |
| 2800 | ** CreateIndex is that each writes its root page number into a different |
| 2801 | ** memory location. This writing of the page number into a memory location |
| 2802 | ** is used by the SQL parser to record the page number in its internal |
| 2803 | ** data structures. |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2804 | ** |
| 2805 | ** See also: CreateTable |
drh | 5b2fd56 | 2001-09-13 15:21:31 +0000 | [diff] [blame] | 2806 | */ |
| 2807 | case OP_CreateIndex: { |
| 2808 | int i = ++p->tos; |
| 2809 | int pgno; |
| 2810 | VERIFY( if( NeedStack(p, p->tos) ) goto no_mem; ) |
| 2811 | if( p->pIndexRoot==0 ){ |
| 2812 | rc = SQLITE_INTERNAL; |
| 2813 | goto abort_due_to_error; |
| 2814 | } |
| 2815 | rc = sqliteBtreeCreateTable(pBt, &pgno); |
| 2816 | if( rc==SQLITE_OK ){ |
| 2817 | aStack[i].i = pgno; |
| 2818 | aStack[i].flags = STK_Int; |
| 2819 | *p->pIndexRoot = pgno; |
| 2820 | p->pIndexRoot = 0; |
| 2821 | } |
| 2822 | break; |
| 2823 | } |
| 2824 | |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2825 | /* Opcode: Reorganize P1 * * |
| 2826 | ** |
| 2827 | ** Compress, optimize, and tidy up table or index whose root page in the |
| 2828 | ** database file is P1. |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2829 | ** |
| 2830 | ** In the current implementation, this is a no-op. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2831 | */ |
| 2832 | case OP_Reorganize: { |
| 2833 | /* This is currently a no-op */ |
| 2834 | break; |
| 2835 | } |
| 2836 | |
| 2837 | /* Opcode: ListOpen P1 * * |
| 2838 | ** |
| 2839 | ** Open a "List" structure used for temporary storage of integer |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2840 | ** record numbers. P1 will server as a handle to this list for future |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2841 | ** interactions. If another list with the P1 handle is |
| 2842 | ** already opened, the prior list is closed and a new one opened |
| 2843 | ** in its place. |
| 2844 | */ |
| 2845 | case OP_ListOpen: { |
| 2846 | int i = pOp->p1; |
| 2847 | VERIFY( if( i<0 ) goto bad_instruction; ) |
| 2848 | if( i>=p->nList ){ |
| 2849 | int j; |
| 2850 | p->apList = sqliteRealloc( p->apList, (i+1)*sizeof(Keylist*) ); |
| 2851 | if( p->apList==0 ){ p->nList = 0; goto no_mem; } |
| 2852 | for(j=p->nList; j<=i; j++) p->apList[j] = 0; |
| 2853 | p->nList = i+1; |
| 2854 | }else if( p->apList[i] ){ |
| 2855 | KeylistFree(p->apList[i]); |
| 2856 | p->apList[i] = 0; |
| 2857 | } |
| 2858 | break; |
| 2859 | } |
| 2860 | |
| 2861 | /* Opcode: ListWrite P1 * * |
| 2862 | ** |
| 2863 | ** Write the integer on the top of the stack |
| 2864 | ** into the temporary storage list P1. |
| 2865 | */ |
| 2866 | case OP_ListWrite: { |
| 2867 | int i = pOp->p1; |
| 2868 | Keylist *pKeylist; |
| 2869 | VERIFY( if( i<0 || i>=p->nList ) goto bad_instruction; ) |
| 2870 | VERIFY( if( p->tos<0 ) goto not_enough_stack; ) |
| 2871 | pKeylist = p->apList[i]; |
| 2872 | if( pKeylist==0 || pKeylist->nUsed>=pKeylist->nKey ){ |
drh | 092d035 | 2001-09-15 13:15:12 +0000 | [diff] [blame] | 2873 | pKeylist = sqliteMalloc( sizeof(Keylist)+999*sizeof(pKeylist->aKey[0]) ); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2874 | if( pKeylist==0 ) goto no_mem; |
| 2875 | pKeylist->nKey = 1000; |
| 2876 | pKeylist->nRead = 0; |
| 2877 | pKeylist->nUsed = 0; |
| 2878 | pKeylist->pNext = p->apList[i]; |
| 2879 | p->apList[i] = pKeylist; |
| 2880 | } |
| 2881 | Integerify(p, p->tos); |
| 2882 | pKeylist->aKey[pKeylist->nUsed++] = aStack[p->tos].i; |
| 2883 | POPSTACK; |
| 2884 | break; |
| 2885 | } |
| 2886 | |
| 2887 | /* Opcode: ListRewind P1 * * |
| 2888 | ** |
| 2889 | ** Rewind the temporary buffer P1 back to the beginning. |
| 2890 | */ |
| 2891 | case OP_ListRewind: { |
| 2892 | int i = pOp->p1; |
| 2893 | VERIFY( if( i<0 ) goto bad_instruction; ) |
| 2894 | /* This is now a no-op */ |
| 2895 | break; |
| 2896 | } |
| 2897 | |
| 2898 | /* Opcode: ListRead P1 P2 * |
| 2899 | ** |
| 2900 | ** Attempt to read an integer from temporary storage buffer P1 |
| 2901 | ** and push it onto the stack. If the storage buffer is empty, |
| 2902 | ** push nothing but instead jump to P2. |
| 2903 | */ |
| 2904 | case OP_ListRead: { |
| 2905 | int i = pOp->p1; |
| 2906 | Keylist *pKeylist; |
| 2907 | VERIFY(if( i<0 || i>=p->nList ) goto bad_instruction;) |
| 2908 | pKeylist = p->apList[i]; |
| 2909 | if( pKeylist!=0 ){ |
| 2910 | VERIFY( |
| 2911 | if( pKeylist->nRead<0 |
| 2912 | || pKeylist->nRead>=pKeylist->nUsed |
| 2913 | || pKeylist->nRead>=pKeylist->nKey ) goto bad_instruction; |
| 2914 | ) |
| 2915 | p->tos++; |
| 2916 | if( NeedStack(p, p->tos) ) goto no_mem; |
| 2917 | aStack[p->tos].i = pKeylist->aKey[pKeylist->nRead++]; |
| 2918 | aStack[p->tos].flags = STK_Int; |
| 2919 | zStack[p->tos] = 0; |
| 2920 | if( pKeylist->nRead>=pKeylist->nUsed ){ |
| 2921 | p->apList[i] = pKeylist->pNext; |
| 2922 | sqliteFree(pKeylist); |
| 2923 | } |
| 2924 | }else{ |
| 2925 | pc = pOp->p2 - 1; |
| 2926 | } |
| 2927 | break; |
| 2928 | } |
| 2929 | |
| 2930 | /* Opcode: ListClose P1 * * |
| 2931 | ** |
| 2932 | ** Close the temporary storage buffer and discard its contents. |
| 2933 | */ |
| 2934 | case OP_ListClose: { |
| 2935 | int i = pOp->p1; |
| 2936 | VERIFY( if( i<0 ) goto bad_instruction; ) |
| 2937 | VERIFY( if( i>=p->nList ) goto bad_instruction; ) |
| 2938 | KeylistFree(p->apList[i]); |
| 2939 | p->apList[i] = 0; |
| 2940 | break; |
| 2941 | } |
| 2942 | |
| 2943 | /* Opcode: SortOpen P1 * * |
| 2944 | ** |
| 2945 | ** Create a new sorter with index P1 |
| 2946 | */ |
| 2947 | case OP_SortOpen: { |
| 2948 | int i = pOp->p1; |
| 2949 | VERIFY( if( i<0 ) goto bad_instruction; ) |
| 2950 | if( i>=p->nSort ){ |
| 2951 | int j; |
| 2952 | p->apSort = sqliteRealloc( p->apSort, (i+1)*sizeof(Sorter*) ); |
| 2953 | if( p->apSort==0 ){ p->nSort = 0; goto no_mem; } |
| 2954 | for(j=p->nSort; j<=i; j++) p->apSort[j] = 0; |
| 2955 | p->nSort = i+1; |
| 2956 | } |
| 2957 | break; |
| 2958 | } |
| 2959 | |
| 2960 | /* Opcode: SortPut P1 * * |
| 2961 | ** |
| 2962 | ** The TOS is the key and the NOS is the data. Pop both from the stack |
| 2963 | ** and put them on the sorter. |
| 2964 | */ |
| 2965 | case OP_SortPut: { |
| 2966 | int i = pOp->p1; |
| 2967 | int tos = p->tos; |
| 2968 | int nos = tos - 1; |
| 2969 | Sorter *pSorter; |
| 2970 | VERIFY( if( i<0 || i>=p->nSort ) goto bad_instruction; ) |
| 2971 | VERIFY( if( tos<1 ) goto not_enough_stack; ) |
| 2972 | if( Stringify(p, tos) || Stringify(p, nos) ) goto no_mem; |
| 2973 | pSorter = sqliteMalloc( sizeof(Sorter) ); |
| 2974 | if( pSorter==0 ) goto no_mem; |
| 2975 | pSorter->pNext = p->apSort[i]; |
| 2976 | p->apSort[i] = pSorter; |
| 2977 | pSorter->nKey = aStack[tos].n; |
| 2978 | pSorter->zKey = zStack[tos]; |
| 2979 | pSorter->nData = aStack[nos].n; |
| 2980 | pSorter->pData = zStack[nos]; |
| 2981 | aStack[tos].flags = 0; |
| 2982 | aStack[nos].flags = 0; |
| 2983 | zStack[tos] = 0; |
| 2984 | zStack[nos] = 0; |
| 2985 | p->tos -= 2; |
| 2986 | break; |
| 2987 | } |
| 2988 | |
| 2989 | /* Opcode: SortMakeRec P1 * * |
| 2990 | ** |
| 2991 | ** The top P1 elements are the arguments to a callback. Form these |
| 2992 | ** elements into a single data entry that can be stored on a sorter |
| 2993 | ** using SortPut and later fed to a callback using SortCallback. |
| 2994 | */ |
| 2995 | case OP_SortMakeRec: { |
| 2996 | char *z; |
| 2997 | char **azArg; |
| 2998 | int nByte; |
| 2999 | int nField; |
| 3000 | int i, j; |
| 3001 | |
| 3002 | nField = pOp->p1; |
| 3003 | VERIFY( if( p->tos+1<nField ) goto not_enough_stack; ) |
| 3004 | nByte = 0; |
| 3005 | for(i=p->tos-nField+1; i<=p->tos; i++){ |
| 3006 | if( (aStack[i].flags & STK_Null)==0 ){ |
| 3007 | if( Stringify(p, i) ) goto no_mem; |
| 3008 | nByte += aStack[i].n; |
| 3009 | } |
| 3010 | } |
| 3011 | nByte += sizeof(char*)*(nField+1); |
| 3012 | azArg = sqliteMalloc( nByte ); |
| 3013 | if( azArg==0 ) goto no_mem; |
| 3014 | z = (char*)&azArg[nField+1]; |
| 3015 | for(j=0, i=p->tos-nField+1; i<=p->tos; i++, j++){ |
| 3016 | if( aStack[i].flags & STK_Null ){ |
| 3017 | azArg[j] = 0; |
| 3018 | }else{ |
| 3019 | azArg[j] = z; |
| 3020 | strcpy(z, zStack[i]); |
| 3021 | z += aStack[i].n; |
| 3022 | } |
| 3023 | } |
| 3024 | PopStack(p, nField); |
| 3025 | VERIFY( NeedStack(p, p->tos+1); ) |
| 3026 | p->tos++; |
| 3027 | aStack[p->tos].n = nByte; |
| 3028 | zStack[p->tos] = (char*)azArg; |
| 3029 | aStack[p->tos].flags = STK_Str|STK_Dyn; |
| 3030 | break; |
| 3031 | } |
| 3032 | |
| 3033 | /* Opcode: SortMakeKey P1 * P3 |
| 3034 | ** |
| 3035 | ** Convert the top few entries of the stack into a sort key. The |
| 3036 | ** number of stack entries consumed is the number of characters in |
| 3037 | ** the string P3. One character from P3 is prepended to each entry. |
| 3038 | ** The first character of P3 is prepended to the element lowest in |
| 3039 | ** the stack and the last character of P3 is appended to the top of |
| 3040 | ** the stack. All stack entries are separated by a \000 character |
| 3041 | ** in the result. The whole key is terminated by two \000 characters |
| 3042 | ** in a row. |
| 3043 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 3044 | ** See also the MakeKey and MakeIdxKey opcodes. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3045 | */ |
| 3046 | case OP_SortMakeKey: { |
| 3047 | char *zNewKey; |
| 3048 | int nByte; |
| 3049 | int nField; |
| 3050 | int i, j, k; |
| 3051 | |
| 3052 | nField = strlen(pOp->p3); |
| 3053 | VERIFY( if( p->tos+1<nField ) goto not_enough_stack; ) |
| 3054 | nByte = 1; |
| 3055 | for(i=p->tos-nField+1; i<=p->tos; i++){ |
| 3056 | if( Stringify(p, i) ) goto no_mem; |
| 3057 | nByte += aStack[i].n+2; |
| 3058 | } |
| 3059 | zNewKey = sqliteMalloc( nByte ); |
| 3060 | if( zNewKey==0 ) goto no_mem; |
| 3061 | j = 0; |
| 3062 | k = 0; |
| 3063 | for(i=p->tos-nField+1; i<=p->tos; i++){ |
| 3064 | zNewKey[j++] = pOp->p3[k++]; |
| 3065 | memcpy(&zNewKey[j], zStack[i], aStack[i].n-1); |
| 3066 | j += aStack[i].n-1; |
| 3067 | zNewKey[j++] = 0; |
| 3068 | } |
| 3069 | zNewKey[j] = 0; |
| 3070 | PopStack(p, nField); |
| 3071 | VERIFY( NeedStack(p, p->tos+1); ) |
| 3072 | p->tos++; |
| 3073 | aStack[p->tos].n = nByte; |
| 3074 | aStack[p->tos].flags = STK_Str|STK_Dyn; |
| 3075 | zStack[p->tos] = zNewKey; |
| 3076 | break; |
| 3077 | } |
| 3078 | |
| 3079 | /* Opcode: Sort P1 * * |
| 3080 | ** |
| 3081 | ** Sort all elements on the given sorter. The algorithm is a |
| 3082 | ** mergesort. |
| 3083 | */ |
| 3084 | case OP_Sort: { |
| 3085 | int j; |
| 3086 | j = pOp->p1; |
| 3087 | VERIFY( if( j<0 ) goto bad_instruction; ) |
| 3088 | if( j<p->nSort ){ |
| 3089 | int i; |
| 3090 | Sorter *pElem; |
| 3091 | Sorter *apSorter[NSORT]; |
| 3092 | for(i=0; i<NSORT; i++){ |
| 3093 | apSorter[i] = 0; |
| 3094 | } |
| 3095 | while( p->apSort[j] ){ |
| 3096 | pElem = p->apSort[j]; |
| 3097 | p->apSort[j] = pElem->pNext; |
| 3098 | pElem->pNext = 0; |
| 3099 | for(i=0; i<NSORT-1; i++){ |
| 3100 | if( apSorter[i]==0 ){ |
| 3101 | apSorter[i] = pElem; |
| 3102 | break; |
| 3103 | }else{ |
| 3104 | pElem = Merge(apSorter[i], pElem); |
| 3105 | apSorter[i] = 0; |
| 3106 | } |
| 3107 | } |
| 3108 | if( i>=NSORT-1 ){ |
| 3109 | apSorter[NSORT-1] = Merge(apSorter[NSORT-1],pElem); |
| 3110 | } |
| 3111 | } |
| 3112 | pElem = 0; |
| 3113 | for(i=0; i<NSORT; i++){ |
| 3114 | pElem = Merge(apSorter[i], pElem); |
| 3115 | } |
| 3116 | p->apSort[j] = pElem; |
| 3117 | } |
| 3118 | break; |
| 3119 | } |
| 3120 | |
| 3121 | /* Opcode: SortNext P1 P2 * |
| 3122 | ** |
| 3123 | ** Push the data for the topmost element in the given sorter onto the |
| 3124 | ** stack, then remove the element from the sorter. |
| 3125 | */ |
| 3126 | case OP_SortNext: { |
| 3127 | int i = pOp->p1; |
| 3128 | VERIFY( if( i<0 ) goto bad_instruction; ) |
| 3129 | if( VERIFY( i<p->nSort && ) p->apSort[i]!=0 ){ |
| 3130 | Sorter *pSorter = p->apSort[i]; |
| 3131 | p->apSort[i] = pSorter->pNext; |
| 3132 | p->tos++; |
| 3133 | VERIFY( NeedStack(p, p->tos); ) |
| 3134 | zStack[p->tos] = pSorter->pData; |
| 3135 | aStack[p->tos].n = pSorter->nData; |
| 3136 | aStack[p->tos].flags = STK_Str|STK_Dyn; |
| 3137 | sqliteFree(pSorter->zKey); |
| 3138 | sqliteFree(pSorter); |
| 3139 | }else{ |
| 3140 | pc = pOp->p2 - 1; |
| 3141 | } |
| 3142 | break; |
| 3143 | } |
| 3144 | |
| 3145 | /* Opcode: SortKey P1 * * |
| 3146 | ** |
| 3147 | ** Push the key for the topmost element of the sorter onto the stack. |
| 3148 | ** But don't change the sorter an any other way. |
| 3149 | */ |
| 3150 | case OP_SortKey: { |
| 3151 | int i = pOp->p1; |
| 3152 | VERIFY( if( i<0 ) goto bad_instruction; ) |
| 3153 | if( i<p->nSort && p->apSort[i]!=0 ){ |
| 3154 | Sorter *pSorter = p->apSort[i]; |
| 3155 | p->tos++; |
| 3156 | VERIFY( NeedStack(p, p->tos); ) |
| 3157 | sqliteSetString(&zStack[p->tos], pSorter->zKey, 0); |
| 3158 | aStack[p->tos].n = pSorter->nKey; |
| 3159 | aStack[p->tos].flags = STK_Str|STK_Dyn; |
| 3160 | } |
| 3161 | break; |
| 3162 | } |
| 3163 | |
| 3164 | /* Opcode: SortCallback P1 P2 * |
| 3165 | ** |
| 3166 | ** The top of the stack contains a callback record built using |
| 3167 | ** the SortMakeRec operation with the same P1 value as this |
| 3168 | ** instruction. Pop this record from the stack and invoke the |
| 3169 | ** callback on it. |
| 3170 | */ |
| 3171 | case OP_SortCallback: { |
| 3172 | int i = p->tos; |
| 3173 | VERIFY( if( i<0 ) goto not_enough_stack; ) |
| 3174 | if( xCallback!=0 ){ |
| 3175 | if( xCallback(pArg, pOp->p1, (char**)zStack[i], p->azColName) ){ |
| 3176 | rc = SQLITE_ABORT; |
| 3177 | } |
| 3178 | } |
| 3179 | POPSTACK; |
| 3180 | break; |
| 3181 | } |
| 3182 | |
| 3183 | /* Opcode: SortClose P1 * * |
| 3184 | ** |
| 3185 | ** Close the given sorter and remove all its elements. |
| 3186 | */ |
| 3187 | case OP_SortClose: { |
| 3188 | Sorter *pSorter; |
| 3189 | int i = pOp->p1; |
| 3190 | VERIFY( if( i<0 ) goto bad_instruction; ) |
| 3191 | if( i<p->nSort ){ |
| 3192 | while( (pSorter = p->apSort[i])!=0 ){ |
| 3193 | p->apSort[i] = pSorter->pNext; |
| 3194 | sqliteFree(pSorter->zKey); |
| 3195 | sqliteFree(pSorter->pData); |
| 3196 | sqliteFree(pSorter); |
| 3197 | } |
| 3198 | } |
| 3199 | break; |
| 3200 | } |
| 3201 | |
| 3202 | /* Opcode: FileOpen * * P3 |
| 3203 | ** |
| 3204 | ** Open the file named by P3 for reading using the FileRead opcode. |
| 3205 | ** If P3 is "stdin" then open standard input for reading. |
| 3206 | */ |
| 3207 | case OP_FileOpen: { |
| 3208 | VERIFY( if( pOp->p3==0 ) goto bad_instruction; ) |
| 3209 | if( p->pFile ){ |
| 3210 | if( p->pFile!=stdin ) fclose(p->pFile); |
| 3211 | p->pFile = 0; |
| 3212 | } |
| 3213 | if( sqliteStrICmp(pOp->p3,"stdin")==0 ){ |
| 3214 | p->pFile = stdin; |
| 3215 | }else{ |
| 3216 | p->pFile = fopen(pOp->p3, "r"); |
| 3217 | } |
| 3218 | if( p->pFile==0 ){ |
| 3219 | sqliteSetString(pzErrMsg,"unable to open file: ", pOp->p3, 0); |
| 3220 | rc = SQLITE_ERROR; |
| 3221 | goto cleanup; |
| 3222 | } |
| 3223 | break; |
| 3224 | } |
| 3225 | |
| 3226 | /* Opcode: FileClose * * * |
| 3227 | ** |
| 3228 | ** Close a file previously opened using FileOpen. This is a no-op |
| 3229 | ** if there is no prior FileOpen call. |
| 3230 | */ |
| 3231 | case OP_FileClose: { |
| 3232 | if( p->pFile ){ |
| 3233 | if( p->pFile!=stdin ) fclose(p->pFile); |
| 3234 | p->pFile = 0; |
| 3235 | } |
| 3236 | if( p->azField ){ |
| 3237 | sqliteFree(p->azField); |
| 3238 | p->azField = 0; |
| 3239 | } |
| 3240 | p->nField = 0; |
| 3241 | if( p->zLine ){ |
| 3242 | sqliteFree(p->zLine); |
| 3243 | p->zLine = 0; |
| 3244 | } |
| 3245 | p->nLineAlloc = 0; |
| 3246 | break; |
| 3247 | } |
| 3248 | |
| 3249 | /* Opcode: FileRead P1 P2 P3 |
| 3250 | ** |
| 3251 | ** Read a single line of input from the open file (the file opened using |
| 3252 | ** FileOpen). If we reach end-of-file, jump immediately to P2. If |
| 3253 | ** we are able to get another line, split the line apart using P3 as |
| 3254 | ** a delimiter. There should be P1 fields. If the input line contains |
| 3255 | ** more than P1 fields, ignore the excess. If the input line contains |
| 3256 | ** fewer than P1 fields, assume the remaining fields contain an |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 3257 | ** empty strings. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3258 | */ |
| 3259 | case OP_FileRead: { |
| 3260 | int n, eol, nField, i, c, nDelim; |
| 3261 | char *zDelim, *z; |
| 3262 | if( p->pFile==0 ) goto fileread_jump; |
| 3263 | nField = pOp->p1; |
| 3264 | if( nField<=0 ) goto fileread_jump; |
| 3265 | if( nField!=p->nField || p->azField==0 ){ |
| 3266 | p->azField = sqliteRealloc(p->azField, sizeof(char*)*nField+1); |
| 3267 | if( p->azField==0 ){ |
| 3268 | p->nField = 0; |
| 3269 | goto fileread_jump; |
| 3270 | } |
| 3271 | p->nField = nField; |
| 3272 | } |
| 3273 | n = 0; |
| 3274 | eol = 0; |
| 3275 | while( eol==0 ){ |
| 3276 | if( p->zLine==0 || n+200>p->nLineAlloc ){ |
| 3277 | p->nLineAlloc = p->nLineAlloc*2 + 300; |
| 3278 | p->zLine = sqliteRealloc(p->zLine, p->nLineAlloc); |
| 3279 | if( p->zLine==0 ){ |
| 3280 | p->nLineAlloc = 0; |
| 3281 | goto fileread_jump; |
| 3282 | } |
| 3283 | } |
| 3284 | if( fgets(&p->zLine[n], p->nLineAlloc-n, p->pFile)==0 ){ |
| 3285 | eol = 1; |
| 3286 | p->zLine[n] = 0; |
| 3287 | }else{ |
| 3288 | while( p->zLine[n] ){ n++; } |
| 3289 | if( n>0 && p->zLine[n-1]=='\n' ){ |
| 3290 | n--; |
| 3291 | p->zLine[n] = 0; |
| 3292 | eol = 1; |
| 3293 | } |
| 3294 | } |
| 3295 | } |
| 3296 | if( n==0 ) goto fileread_jump; |
| 3297 | z = p->zLine; |
| 3298 | if( z[0]=='\\' && z[1]=='.' && z[2]==0 ){ |
| 3299 | goto fileread_jump; |
| 3300 | } |
| 3301 | zDelim = pOp->p3; |
| 3302 | if( zDelim==0 ) zDelim = "\t"; |
| 3303 | c = zDelim[0]; |
| 3304 | nDelim = strlen(zDelim); |
| 3305 | p->azField[0] = z; |
| 3306 | for(i=1; *z!=0 && i<=nField; i++){ |
| 3307 | int from, to; |
| 3308 | from = to = 0; |
| 3309 | while( z[from] ){ |
| 3310 | if( z[from]=='\\' && z[from+1]!=0 ){ |
| 3311 | z[to++] = z[from+1]; |
| 3312 | from += 2; |
| 3313 | continue; |
| 3314 | } |
| 3315 | if( z[from]==c && strncmp(&z[from],zDelim,nDelim)==0 ) break; |
| 3316 | z[to++] = z[from++]; |
| 3317 | } |
| 3318 | if( z[from] ){ |
| 3319 | z[to] = 0; |
| 3320 | z += from + nDelim; |
| 3321 | if( i<nField ) p->azField[i] = z; |
| 3322 | }else{ |
| 3323 | z[to] = 0; |
| 3324 | z = ""; |
| 3325 | } |
| 3326 | } |
| 3327 | while( i<nField ){ |
| 3328 | p->azField[i++] = ""; |
| 3329 | } |
| 3330 | break; |
| 3331 | |
| 3332 | /* If we reach end-of-file, or if anything goes wrong, jump here. |
| 3333 | ** This code will cause a jump to P2 */ |
| 3334 | fileread_jump: |
| 3335 | pc = pOp->p2 - 1; |
| 3336 | break; |
| 3337 | } |
| 3338 | |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 3339 | /* Opcode: FileColumn P1 * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3340 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 3341 | ** Push onto the stack the P1-th column of the most recently read line |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3342 | ** from the input file. |
| 3343 | */ |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 3344 | case OP_FileColumn: { |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3345 | int i = pOp->p1; |
| 3346 | char *z; |
| 3347 | VERIFY( if( NeedStack(p, p->tos+1) ) goto no_mem; ) |
| 3348 | if( VERIFY( i>=0 && i<p->nField && ) p->azField ){ |
| 3349 | z = p->azField[i]; |
| 3350 | }else{ |
| 3351 | z = 0; |
| 3352 | } |
| 3353 | if( z==0 ) z = ""; |
| 3354 | p->tos++; |
| 3355 | aStack[p->tos].n = strlen(z) + 1; |
| 3356 | zStack[p->tos] = z; |
| 3357 | aStack[p->tos].flags = STK_Str; |
| 3358 | break; |
| 3359 | } |
| 3360 | |
| 3361 | /* Opcode: MemStore P1 * * |
| 3362 | ** |
| 3363 | ** Pop a single value of the stack and store that value into memory |
| 3364 | ** location P1. P1 should be a small integer since space is allocated |
| 3365 | ** for all memory locations between 0 and P1 inclusive. |
| 3366 | */ |
| 3367 | case OP_MemStore: { |
| 3368 | int i = pOp->p1; |
| 3369 | int tos = p->tos; |
| 3370 | Mem *pMem; |
| 3371 | char *zOld; |
| 3372 | VERIFY( if( tos<0 ) goto not_enough_stack; ) |
| 3373 | if( i>=p->nMem ){ |
| 3374 | int nOld = p->nMem; |
| 3375 | p->nMem = i + 5; |
| 3376 | p->aMem = sqliteRealloc(p->aMem, p->nMem*sizeof(p->aMem[0])); |
| 3377 | if( p->aMem==0 ) goto no_mem; |
| 3378 | if( nOld<p->nMem ){ |
| 3379 | memset(&p->aMem[nOld], 0, sizeof(p->aMem[0])*(p->nMem-nOld)); |
| 3380 | } |
| 3381 | } |
| 3382 | pMem = &p->aMem[i]; |
| 3383 | if( pMem->s.flags & STK_Dyn ){ |
| 3384 | zOld = pMem->z; |
| 3385 | }else{ |
| 3386 | zOld = 0; |
| 3387 | } |
| 3388 | pMem->s = aStack[tos]; |
| 3389 | if( pMem->s.flags & STK_Str ){ |
| 3390 | pMem->z = sqliteStrNDup(zStack[tos], pMem->s.n); |
| 3391 | pMem->s.flags |= STK_Dyn; |
| 3392 | } |
| 3393 | if( zOld ) sqliteFree(zOld); |
| 3394 | POPSTACK; |
| 3395 | break; |
| 3396 | } |
| 3397 | |
| 3398 | /* Opcode: MemLoad P1 * * |
| 3399 | ** |
| 3400 | ** Push a copy of the value in memory location P1 onto the stack. |
| 3401 | */ |
| 3402 | case OP_MemLoad: { |
| 3403 | int tos = ++p->tos; |
| 3404 | int i = pOp->p1; |
| 3405 | VERIFY( if( NeedStack(p, tos) ) goto no_mem; ) |
| 3406 | if( i<0 || i>=p->nMem ){ |
| 3407 | aStack[tos].flags = STK_Null; |
| 3408 | zStack[tos] = 0; |
| 3409 | }else{ |
| 3410 | aStack[tos] = p->aMem[i].s; |
| 3411 | if( aStack[tos].flags & STK_Str ){ |
| 3412 | char *z = sqliteMalloc(aStack[tos].n); |
| 3413 | if( z==0 ) goto no_mem; |
| 3414 | memcpy(z, p->aMem[i].z, aStack[tos].n); |
| 3415 | zStack[tos] = z; |
| 3416 | aStack[tos].flags |= STK_Dyn; |
| 3417 | } |
| 3418 | } |
| 3419 | break; |
| 3420 | } |
| 3421 | |
| 3422 | /* Opcode: AggReset * P2 * |
| 3423 | ** |
| 3424 | ** Reset the aggregator so that it no longer contains any data. |
| 3425 | ** Future aggregator elements will contain P2 values each. |
| 3426 | */ |
| 3427 | case OP_AggReset: { |
| 3428 | AggReset(&p->agg); |
| 3429 | p->agg.nMem = pOp->p2; |
| 3430 | break; |
| 3431 | } |
| 3432 | |
| 3433 | /* Opcode: AggFocus * P2 * |
| 3434 | ** |
| 3435 | ** Pop the top of the stack and use that as an aggregator key. If |
| 3436 | ** an aggregator with that same key already exists, then make the |
| 3437 | ** aggregator the current aggregator and jump to P2. If no aggregator |
| 3438 | ** with the given key exists, create one and make it current but |
| 3439 | ** do not jump. |
| 3440 | ** |
| 3441 | ** The order of aggregator opcodes is important. The order is: |
| 3442 | ** AggReset AggFocus AggNext. In other words, you must execute |
| 3443 | ** AggReset first, then zero or more AggFocus operations, then |
| 3444 | ** zero or more AggNext operations. You must not execute an AggFocus |
| 3445 | ** in between an AggNext and an AggReset. |
| 3446 | */ |
| 3447 | case OP_AggFocus: { |
| 3448 | int tos = p->tos; |
| 3449 | AggElem *pElem; |
| 3450 | char *zKey; |
| 3451 | int nKey; |
| 3452 | |
| 3453 | VERIFY( if( tos<0 ) goto not_enough_stack; ) |
| 3454 | if( Stringify(p, tos) ) goto no_mem; |
| 3455 | zKey = zStack[tos]; |
| 3456 | nKey = aStack[tos].n; |
| 3457 | if( p->agg.nHash<=0 ){ |
| 3458 | pElem = 0; |
| 3459 | }else{ |
| 3460 | int h = sqliteHashNoCase(zKey, nKey-1) % p->agg.nHash; |
| 3461 | for(pElem=p->agg.apHash[h]; pElem; pElem=pElem->pHash){ |
| 3462 | if( strcmp(pElem->zKey, zKey)==0 ) break; |
| 3463 | } |
| 3464 | } |
| 3465 | if( pElem ){ |
| 3466 | p->agg.pCurrent = pElem; |
| 3467 | pc = pOp->p2 - 1; |
| 3468 | }else{ |
| 3469 | AggInsert(&p->agg, zKey); |
| 3470 | if( sqlite_malloc_failed ) goto no_mem; |
| 3471 | } |
| 3472 | POPSTACK; |
| 3473 | break; |
| 3474 | } |
| 3475 | |
| 3476 | /* Opcode: AggIncr P1 P2 * |
| 3477 | ** |
| 3478 | ** Increase the integer value in the P2-th field of the aggregate |
| 3479 | ** element current in focus by an amount P1. |
| 3480 | */ |
| 3481 | case OP_AggIncr: { |
| 3482 | AggElem *pFocus = AggInFocus(p->agg); |
| 3483 | int i = pOp->p2; |
| 3484 | if( pFocus==0 ) goto no_mem; |
| 3485 | if( i>=0 && i<p->agg.nMem ){ |
| 3486 | Mem *pMem = &pFocus->aMem[i]; |
| 3487 | if( pMem->s.flags!=STK_Int ){ |
| 3488 | if( pMem->s.flags & STK_Int ){ |
| 3489 | /* Do nothing */ |
| 3490 | }else if( pMem->s.flags & STK_Real ){ |
| 3491 | pMem->s.i = pMem->s.r; |
| 3492 | }else if( pMem->s.flags & STK_Str ){ |
| 3493 | pMem->s.i = atoi(pMem->z); |
| 3494 | }else{ |
| 3495 | pMem->s.i = 0; |
| 3496 | } |
| 3497 | if( pMem->s.flags & STK_Dyn ) sqliteFree(pMem->z); |
| 3498 | pMem->z = 0; |
| 3499 | pMem->s.flags = STK_Int; |
| 3500 | } |
| 3501 | pMem->s.i += pOp->p1; |
| 3502 | } |
| 3503 | break; |
| 3504 | } |
| 3505 | |
| 3506 | /* Opcode: AggSet * P2 * |
| 3507 | ** |
| 3508 | ** Move the top of the stack into the P2-th field of the current |
| 3509 | ** aggregate. String values are duplicated into new memory. |
| 3510 | */ |
| 3511 | case OP_AggSet: { |
| 3512 | AggElem *pFocus = AggInFocus(p->agg); |
| 3513 | int i = pOp->p2; |
| 3514 | int tos = p->tos; |
| 3515 | VERIFY( if( tos<0 ) goto not_enough_stack; ) |
| 3516 | if( pFocus==0 ) goto no_mem; |
| 3517 | if( VERIFY( i>=0 && ) i<p->agg.nMem ){ |
| 3518 | Mem *pMem = &pFocus->aMem[i]; |
| 3519 | char *zOld; |
| 3520 | if( pMem->s.flags & STK_Dyn ){ |
| 3521 | zOld = pMem->z; |
| 3522 | }else{ |
| 3523 | zOld = 0; |
| 3524 | } |
| 3525 | pMem->s = aStack[tos]; |
| 3526 | if( pMem->s.flags & STK_Str ){ |
| 3527 | pMem->z = sqliteMalloc( aStack[tos].n ); |
| 3528 | if( pMem->z==0 ) goto no_mem; |
| 3529 | memcpy(pMem->z, zStack[tos], pMem->s.n); |
| 3530 | pMem->s.flags |= STK_Str|STK_Dyn; |
| 3531 | } |
| 3532 | if( zOld ) sqliteFree(zOld); |
| 3533 | } |
| 3534 | POPSTACK; |
| 3535 | break; |
| 3536 | } |
| 3537 | |
| 3538 | /* Opcode: AggGet * P2 * |
| 3539 | ** |
| 3540 | ** Push a new entry onto the stack which is a copy of the P2-th field |
| 3541 | ** of the current aggregate. Strings are not duplicated so |
| 3542 | ** string values will be ephemeral. |
| 3543 | */ |
| 3544 | case OP_AggGet: { |
| 3545 | AggElem *pFocus = AggInFocus(p->agg); |
| 3546 | int i = pOp->p2; |
| 3547 | int tos = ++p->tos; |
| 3548 | VERIFY( if( NeedStack(p, tos) ) goto no_mem; ) |
| 3549 | if( pFocus==0 ) goto no_mem; |
| 3550 | if( VERIFY( i>=0 && ) i<p->agg.nMem ){ |
| 3551 | Mem *pMem = &pFocus->aMem[i]; |
| 3552 | aStack[tos] = pMem->s; |
| 3553 | zStack[tos] = pMem->z; |
| 3554 | aStack[tos].flags &= ~STK_Dyn; |
| 3555 | } |
| 3556 | break; |
| 3557 | } |
| 3558 | |
| 3559 | /* Opcode: AggNext * P2 * |
| 3560 | ** |
| 3561 | ** Make the next aggregate value the current aggregate. The prior |
| 3562 | ** aggregate is deleted. If all aggregate values have been consumed, |
| 3563 | ** jump to P2. |
| 3564 | ** |
| 3565 | ** The order of aggregator opcodes is important. The order is: |
| 3566 | ** AggReset AggFocus AggNext. In other words, you must execute |
| 3567 | ** AggReset first, then zero or more AggFocus operations, then |
| 3568 | ** zero or more AggNext operations. You must not execute an AggFocus |
| 3569 | ** in between an AggNext and an AggReset. |
| 3570 | */ |
| 3571 | case OP_AggNext: { |
| 3572 | if( p->agg.nHash ){ |
| 3573 | p->agg.nHash = 0; |
| 3574 | sqliteFree(p->agg.apHash); |
| 3575 | p->agg.apHash = 0; |
| 3576 | p->agg.pCurrent = p->agg.pFirst; |
| 3577 | }else if( p->agg.pCurrent==p->agg.pFirst && p->agg.pCurrent!=0 ){ |
| 3578 | int i; |
| 3579 | AggElem *pElem = p->agg.pCurrent; |
| 3580 | for(i=0; i<p->agg.nMem; i++){ |
| 3581 | if( pElem->aMem[i].s.flags & STK_Dyn ){ |
| 3582 | sqliteFree(pElem->aMem[i].z); |
| 3583 | } |
| 3584 | } |
| 3585 | p->agg.pCurrent = p->agg.pFirst = pElem->pNext; |
| 3586 | sqliteFree(pElem); |
| 3587 | p->agg.nElem--; |
| 3588 | } |
| 3589 | if( p->agg.pCurrent==0 ){ |
| 3590 | pc = pOp->p2-1; |
| 3591 | } |
| 3592 | break; |
| 3593 | } |
| 3594 | |
| 3595 | /* Opcode: SetClear P1 * * |
| 3596 | ** |
| 3597 | ** Remove all elements from the P1-th Set. |
| 3598 | */ |
| 3599 | case OP_SetClear: { |
| 3600 | int i = pOp->p1; |
| 3601 | if( i>=0 && i<p->nSet ){ |
| 3602 | SetClear(&p->aSet[i]); |
| 3603 | } |
| 3604 | break; |
| 3605 | } |
| 3606 | |
| 3607 | /* Opcode: SetInsert P1 * P3 |
| 3608 | ** |
| 3609 | ** If Set P1 does not exist then create it. Then insert value |
| 3610 | ** P3 into that set. If P3 is NULL, then insert the top of the |
| 3611 | ** stack into the set. |
| 3612 | */ |
| 3613 | case OP_SetInsert: { |
| 3614 | int i = pOp->p1; |
| 3615 | if( p->nSet<=i ){ |
| 3616 | p->aSet = sqliteRealloc(p->aSet, (i+1)*sizeof(p->aSet[0]) ); |
| 3617 | if( p->aSet==0 ) goto no_mem; |
| 3618 | memset(&p->aSet[p->nSet], 0, sizeof(p->aSet[0])*(i+1 - p->nSet)); |
| 3619 | p->nSet = i+1; |
| 3620 | } |
| 3621 | if( pOp->p3 ){ |
| 3622 | SetInsert(&p->aSet[i], pOp->p3); |
| 3623 | }else{ |
| 3624 | int tos = p->tos; |
| 3625 | if( tos<0 ) goto not_enough_stack; |
| 3626 | if( Stringify(p, tos) ) goto no_mem; |
| 3627 | SetInsert(&p->aSet[i], zStack[tos]); |
| 3628 | POPSTACK; |
| 3629 | } |
| 3630 | if( sqlite_malloc_failed ) goto no_mem; |
| 3631 | break; |
| 3632 | } |
| 3633 | |
| 3634 | /* Opcode: SetFound P1 P2 * |
| 3635 | ** |
| 3636 | ** Pop the stack once and compare the value popped off with the |
| 3637 | ** contents of set P1. If the element popped exists in set P1, |
| 3638 | ** then jump to P2. Otherwise fall through. |
| 3639 | */ |
| 3640 | case OP_SetFound: { |
| 3641 | int i = pOp->p1; |
| 3642 | int tos = p->tos; |
| 3643 | VERIFY( if( tos<0 ) goto not_enough_stack; ) |
| 3644 | if( Stringify(p, tos) ) goto no_mem; |
| 3645 | if( VERIFY( i>=0 && i<p->nSet &&) SetTest(&p->aSet[i], zStack[tos])){ |
| 3646 | pc = pOp->p2 - 1; |
| 3647 | } |
| 3648 | POPSTACK; |
| 3649 | break; |
| 3650 | } |
| 3651 | |
| 3652 | /* Opcode: SetNotFound P1 P2 * |
| 3653 | ** |
| 3654 | ** Pop the stack once and compare the value popped off with the |
| 3655 | ** contents of set P1. If the element popped does not exists in |
| 3656 | ** set P1, then jump to P2. Otherwise fall through. |
| 3657 | */ |
| 3658 | case OP_SetNotFound: { |
| 3659 | int i = pOp->p1; |
| 3660 | int tos = p->tos; |
| 3661 | VERIFY( if( tos<0 ) goto not_enough_stack; ) |
| 3662 | if( Stringify(p, tos) ) goto no_mem; |
| 3663 | if(VERIFY( i>=0 && i<p->nSet &&) !SetTest(&p->aSet[i], zStack[tos])){ |
| 3664 | pc = pOp->p2 - 1; |
| 3665 | } |
| 3666 | POPSTACK; |
| 3667 | break; |
| 3668 | } |
| 3669 | |
| 3670 | /* Opcode: Strlen * * * |
| 3671 | ** |
| 3672 | ** Interpret the top of the stack as a string. Replace the top of |
| 3673 | ** stack with an integer which is the length of the string. |
| 3674 | */ |
| 3675 | case OP_Strlen: { |
| 3676 | int tos = p->tos; |
| 3677 | int len; |
| 3678 | VERIFY( if( tos<0 ) goto not_enough_stack; ) |
| 3679 | if( Stringify(p, tos) ) goto no_mem; |
| 3680 | #ifdef SQLITE_UTF8 |
| 3681 | { |
| 3682 | char *z = zStack[tos]; |
| 3683 | for(len=0; *z; z++){ if( (0xc0&*z)!=0x80 ) len++; } |
| 3684 | } |
| 3685 | #else |
| 3686 | len = aStack[tos].n-1; |
| 3687 | #endif |
| 3688 | POPSTACK; |
| 3689 | p->tos++; |
| 3690 | aStack[tos].i = len; |
| 3691 | aStack[tos].flags = STK_Int; |
| 3692 | break; |
| 3693 | } |
| 3694 | |
| 3695 | /* Opcode: Substr P1 P2 * |
| 3696 | ** |
| 3697 | ** This operation pops between 1 and 3 elements from the stack and |
| 3698 | ** pushes back a single element. The bottom-most element popped from |
| 3699 | ** the stack is a string and the element pushed back is also a string. |
| 3700 | ** The other two elements popped are integers. The integers are taken |
| 3701 | ** from the stack only if P1 and/or P2 are 0. When P1 or P2 are |
| 3702 | ** not zero, the value of the operand is used rather than the integer |
| 3703 | ** from the stack. In the sequel, we will use P1 and P2 to describe |
| 3704 | ** the two integers, even if those integers are really taken from the |
| 3705 | ** stack. |
| 3706 | ** |
| 3707 | ** The string pushed back onto the stack is a substring of the string |
| 3708 | ** that was popped. There are P2 characters in the substring. The |
| 3709 | ** first character of the substring is the P1-th character of the |
| 3710 | ** original string where the left-most character is 1 (not 0). If P1 |
| 3711 | ** is negative, then counting begins at the right instead of at the |
| 3712 | ** left. |
| 3713 | */ |
| 3714 | case OP_Substr: { |
| 3715 | int cnt; |
| 3716 | int start; |
| 3717 | int n; |
| 3718 | char *z; |
| 3719 | |
| 3720 | if( pOp->p2==0 ){ |
| 3721 | VERIFY( if( p->tos<0 ) goto not_enough_stack; ) |
| 3722 | Integerify(p, p->tos); |
| 3723 | cnt = aStack[p->tos].i; |
| 3724 | POPSTACK; |
| 3725 | }else{ |
| 3726 | cnt = pOp->p2; |
| 3727 | } |
| 3728 | if( pOp->p1==0 ){ |
| 3729 | VERIFY( if( p->tos<0 ) goto not_enough_stack; ) |
| 3730 | Integerify(p, p->tos); |
| 3731 | start = aStack[p->tos].i - 1; |
| 3732 | POPSTACK; |
| 3733 | }else{ |
| 3734 | start = pOp->p1 - 1; |
| 3735 | } |
| 3736 | VERIFY( if( p->tos<0 ) goto not_enough_stack; ) |
| 3737 | if( Stringify(p, p->tos) ) goto no_mem; |
| 3738 | |
| 3739 | /* "n" will be the number of characters in the input string. |
| 3740 | ** For iso8859, the number of characters is the number of bytes. |
| 3741 | ** Buf for UTF-8, some characters can use multiple bytes and the |
| 3742 | ** situation is more complex. |
| 3743 | */ |
| 3744 | #ifdef SQLITE_UTF8 |
| 3745 | z = zStack[p->tos]; |
| 3746 | for(n=0; *z; z++){ if( (0xc0&*z)!=0x80 ) n++; } |
| 3747 | #else |
| 3748 | n = aStack[p->tos].n - 1; |
| 3749 | #endif |
| 3750 | if( start<0 ){ |
| 3751 | start += n + 1; |
| 3752 | if( start<0 ){ |
| 3753 | cnt += start; |
| 3754 | start = 0; |
| 3755 | } |
| 3756 | } |
| 3757 | if( start>n ){ |
| 3758 | start = n; |
| 3759 | } |
| 3760 | if( cnt<0 ) cnt = 0; |
| 3761 | if( cnt > n ){ |
| 3762 | cnt = n; |
| 3763 | } |
| 3764 | |
| 3765 | /* At this point, "start" is the index of the first character to |
| 3766 | ** extract and "cnt" is the number of characters to extract. We |
| 3767 | ** need to convert units on these variable from characters into |
| 3768 | ** bytes. For iso8859, the conversion is a no-op, but for UTF-8 |
| 3769 | ** we have to do a little work. |
| 3770 | */ |
| 3771 | #ifdef SQLITE_UTF8 |
| 3772 | { |
| 3773 | int c_start = start; |
| 3774 | int c_cnt = cnt; |
| 3775 | int i; |
| 3776 | z = zStack[p->tos]; |
| 3777 | for(start=i=0; i<c_start; i++){ |
| 3778 | while( (0xc0&z[++start])==0x80 ){} |
| 3779 | } |
| 3780 | for(cnt=i=0; i<c_cnt; i++){ |
| 3781 | while( (0xc0&z[(++cnt)+start])==0x80 ){} |
| 3782 | } |
| 3783 | } |
| 3784 | #endif |
| 3785 | z = sqliteMalloc( cnt+1 ); |
| 3786 | if( z==0 ) goto no_mem; |
| 3787 | strncpy(z, &zStack[p->tos][start], cnt); |
| 3788 | z[cnt] = 0; |
| 3789 | POPSTACK; |
| 3790 | p->tos++; |
| 3791 | zStack[p->tos] = z; |
| 3792 | aStack[p->tos].n = cnt + 1; |
| 3793 | aStack[p->tos].flags = STK_Str|STK_Dyn; |
| 3794 | break; |
| 3795 | } |
| 3796 | |
| 3797 | /* An other opcode is illegal... |
| 3798 | */ |
| 3799 | default: { |
| 3800 | sprintf(zBuf,"%d",pOp->opcode); |
| 3801 | sqliteSetString(pzErrMsg, "unknown opcode ", zBuf, 0); |
| 3802 | rc = SQLITE_INTERNAL; |
| 3803 | break; |
| 3804 | } |
| 3805 | |
| 3806 | /***************************************************************************** |
| 3807 | ** The cases of the switch statement above this line should all be indented |
| 3808 | ** by 6 spaces. But the left-most 6 spaces have been removed to improve the |
| 3809 | ** readability. From this point on down, the normal indentation rules are |
| 3810 | ** restored. |
| 3811 | *****************************************************************************/ |
| 3812 | } |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 3813 | |
| 3814 | /* The following code adds nothing to the actual functionality |
| 3815 | ** of the program. It is only here for testing and debugging. |
| 3816 | ** On the other hand, it does burn CPU cycles every time through |
| 3817 | ** the evaluator loop. So we can leave it out when NDEBUG is defined. |
| 3818 | */ |
| 3819 | #ifndef NDEBUG |
drh | 58b9576 | 2000-06-02 01:17:37 +0000 | [diff] [blame] | 3820 | if( pc<-1 || pc>=p->nOp ){ |
| 3821 | sqliteSetString(pzErrMsg, "jump destination out of range", 0); |
| 3822 | rc = SQLITE_INTERNAL; |
| 3823 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3824 | if( p->trace && p->tos>=0 ){ |
| 3825 | int i; |
| 3826 | fprintf(p->trace, "Stack:"); |
| 3827 | for(i=p->tos; i>=0 && i>p->tos-5; i--){ |
drh | 8c3052c | 2000-10-23 13:16:31 +0000 | [diff] [blame] | 3828 | if( aStack[i].flags & STK_Null ){ |
drh | c61053b | 2000-06-04 12:58:36 +0000 | [diff] [blame] | 3829 | fprintf(p->trace, " NULL"); |
drh | 8c3052c | 2000-10-23 13:16:31 +0000 | [diff] [blame] | 3830 | }else if( aStack[i].flags & STK_Int ){ |
| 3831 | fprintf(p->trace, " i:%d", aStack[i].i); |
| 3832 | }else if( aStack[i].flags & STK_Real ){ |
| 3833 | fprintf(p->trace, " r:%g", aStack[i].r); |
| 3834 | }else if( aStack[i].flags & STK_Str ){ |
drh | 092d035 | 2001-09-15 13:15:12 +0000 | [diff] [blame] | 3835 | int j, k; |
| 3836 | char zBuf[100]; |
| 3837 | zBuf[0] = ' '; |
| 3838 | zBuf[1] = (aStack[i].flags & STK_Dyn)!=0 ? 'z' : 's'; |
drh | 872ff86 | 2001-09-15 14:43:39 +0000 | [diff] [blame] | 3839 | zBuf[2] = '['; |
drh | 092d035 | 2001-09-15 13:15:12 +0000 | [diff] [blame] | 3840 | k = 3; |
drh | 872ff86 | 2001-09-15 14:43:39 +0000 | [diff] [blame] | 3841 | for(j=0; j<20 && j<aStack[i].n; j++){ |
drh | 092d035 | 2001-09-15 13:15:12 +0000 | [diff] [blame] | 3842 | int c = zStack[i][j]; |
| 3843 | if( c==0 && j==aStack[i].n-1 ) break; |
| 3844 | if( isprint(c) && !isspace(c) ){ |
| 3845 | zBuf[k++] = c; |
| 3846 | }else{ |
| 3847 | zBuf[k++] = '.'; |
| 3848 | } |
drh | c61053b | 2000-06-04 12:58:36 +0000 | [diff] [blame] | 3849 | } |
drh | 872ff86 | 2001-09-15 14:43:39 +0000 | [diff] [blame] | 3850 | zBuf[k++] = ']'; |
drh | 092d035 | 2001-09-15 13:15:12 +0000 | [diff] [blame] | 3851 | zBuf[k++] = 0; |
| 3852 | fprintf(p->trace, "%s", zBuf); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3853 | }else{ |
drh | c61053b | 2000-06-04 12:58:36 +0000 | [diff] [blame] | 3854 | fprintf(p->trace, " ???"); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3855 | } |
| 3856 | } |
| 3857 | fprintf(p->trace,"\n"); |
| 3858 | } |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 3859 | #endif |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3860 | } |
| 3861 | |
| 3862 | cleanup: |
| 3863 | Cleanup(p); |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 3864 | if( (p->pTableRoot || p->pIndexRoot) && rc==SQLITE_OK ){ |
drh | 5b2fd56 | 2001-09-13 15:21:31 +0000 | [diff] [blame] | 3865 | rc = SQLITE_INTERNAL; |
| 3866 | sqliteSetString(pzErrMsg, "table or index root page not set", 0); |
| 3867 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3868 | if( rc!=SQLITE_OK && (db->flags & SQLITE_InTrans)!=0 ){ |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 3869 | sqliteBtreeRollback(pBt); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3870 | sqliteRollbackInternalChanges(db); |
| 3871 | db->flags &= ~SQLITE_InTrans; |
| 3872 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3873 | return rc; |
| 3874 | |
| 3875 | /* Jump to here if a malloc() fails. It's hard to get a malloc() |
| 3876 | ** to fail on a modern VM computer, so this code is untested. |
| 3877 | */ |
| 3878 | no_mem: |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3879 | sqliteSetString(pzErrMsg, "out or memory", 0); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3880 | rc = SQLITE_NOMEM; |
| 3881 | goto cleanup; |
| 3882 | |
| 3883 | /* Jump to here for any other kind of fatal error. The "rc" variable |
| 3884 | ** should hold the error number. |
| 3885 | */ |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 3886 | abort_due_to_error: |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3887 | sqliteSetString(pzErrMsg, sqliteErrStr(rc), 0); |
| 3888 | goto cleanup; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3889 | |
| 3890 | /* Jump to here if a operator is encountered that requires more stack |
| 3891 | ** operands than are currently available on the stack. |
| 3892 | */ |
| 3893 | not_enough_stack: |
| 3894 | sprintf(zBuf,"%d",pc); |
| 3895 | sqliteSetString(pzErrMsg, "too few operands on stack at ", zBuf, 0); |
drh | 58b9576 | 2000-06-02 01:17:37 +0000 | [diff] [blame] | 3896 | rc = SQLITE_INTERNAL; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3897 | goto cleanup; |
| 3898 | |
| 3899 | /* Jump here if an illegal or illformed instruction is executed. |
| 3900 | */ |
| 3901 | bad_instruction: |
| 3902 | sprintf(zBuf,"%d",pc); |
| 3903 | sqliteSetString(pzErrMsg, "illegal operation at ", zBuf, 0); |
drh | 58b9576 | 2000-06-02 01:17:37 +0000 | [diff] [blame] | 3904 | rc = SQLITE_INTERNAL; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3905 | goto cleanup; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3906 | } |