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 | ************************************************************************* |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 12 | ** The code in this file implements execution method of the |
| 13 | ** Virtual Database Engine (VDBE). A separate file ("vdbeaux.c") |
| 14 | ** handles housekeeping details such as creating and deleting |
| 15 | ** VDBE instances. This file is solely interested in executing |
| 16 | ** the VDBE program. |
| 17 | ** |
| 18 | ** In the external interface, an "sqlite_vm*" is an opaque pointer |
| 19 | ** to a VDBE. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 20 | ** |
| 21 | ** The SQL parser generates a program which is then executed by |
| 22 | ** the VDBE to do the work of the SQL statement. VDBE programs are |
| 23 | ** similar in form to assembly language. The program consists of |
| 24 | ** a linear sequence of operations. Each operation has an opcode |
| 25 | ** and 3 operands. Operands P1 and P2 are integers. Operand P3 |
| 26 | ** is a null-terminated string. The P2 operand must be non-negative. |
| 27 | ** Opcodes will typically ignore one or more operands. Many opcodes |
| 28 | ** ignore all three operands. |
| 29 | ** |
| 30 | ** Computation results are stored on a stack. Each entry on the |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 31 | ** stack is either an integer, a null-terminated string, a floating point |
| 32 | ** number, or the SQL "NULL" value. An inplicit conversion from one |
| 33 | ** type to the other occurs as necessary. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 34 | ** |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 35 | ** Most of the code in this file is taken up by the sqlite3VdbeExec() |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 36 | ** function which does the work of interpreting a VDBE program. |
| 37 | ** But other routines are also provided to help in building up |
| 38 | ** a program instruction by instruction. |
| 39 | ** |
drh | ac82fcf | 2002-09-08 17:23:41 +0000 | [diff] [blame] | 40 | ** Various scripts scan this source file in order to generate HTML |
| 41 | ** documentation, headers files, or other derived files. The formatting |
| 42 | ** of the code in this file is, therefore, important. See other comments |
| 43 | ** in this file for details. If in doubt, do not deviate from existing |
| 44 | ** commenting and indentation practices when changing or adding code. |
| 45 | ** |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame^] | 46 | ** $Id: vdbe.c,v 1.293 2004/05/14 21:59:40 drh Exp $ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 47 | */ |
| 48 | #include "sqliteInt.h" |
drh | 6f8fd3c | 2003-06-07 11:33:45 +0000 | [diff] [blame] | 49 | #include "os.h" |
drh | 7c68d60 | 2000-10-11 19:28:51 +0000 | [diff] [blame] | 50 | #include <ctype.h> |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 51 | #include "vdbeInt.h" |
drh | 8f619cc | 2002-09-08 00:04:50 +0000 | [diff] [blame] | 52 | |
| 53 | /* |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 54 | ** The following global variable is incremented every time a cursor |
| 55 | ** moves, either by the OP_MoveTo or the OP_Next opcode. The test |
| 56 | ** procedures use this information to make sure that indices are |
drh | ac82fcf | 2002-09-08 17:23:41 +0000 | [diff] [blame] | 57 | ** working correctly. This variable has no function other than to |
| 58 | ** help verify the correct operation of the library. |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 59 | */ |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 60 | int sqlite3_search_count = 0; |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 61 | |
drh | f603871 | 2004-02-08 18:07:34 +0000 | [diff] [blame] | 62 | /* |
| 63 | ** When this global variable is positive, it gets decremented once before |
| 64 | ** each instruction in the VDBE. When reaches zero, the SQLITE_Interrupt |
| 65 | ** of the db.flags field is set in order to simulate and interrupt. |
| 66 | ** |
| 67 | ** This facility is used for testing purposes only. It does not function |
| 68 | ** in an ordinary build. |
| 69 | */ |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 70 | int sqlite3_interrupt_count = 0; |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 71 | |
| 72 | /* |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 73 | ** Advance the virtual machine to the next output row. |
| 74 | ** |
drh | 3a84069 | 2003-01-29 22:58:26 +0000 | [diff] [blame] | 75 | ** The return vale will be either SQLITE_BUSY, SQLITE_DONE, |
| 76 | ** SQLITE_ROW, SQLITE_ERROR, or SQLITE_MISUSE. |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 77 | ** |
| 78 | ** SQLITE_BUSY means that the virtual machine attempted to open |
| 79 | ** a locked database and there is no busy callback registered. |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 80 | ** Call sqlite3_step() again to retry the open. *pN is set to 0 |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 81 | ** and *pazColName and *pazValue are both set to NULL. |
| 82 | ** |
| 83 | ** SQLITE_DONE means that the virtual machine has finished |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 84 | ** executing. sqlite3_step() should not be called again on this |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 85 | ** virtual machine. *pN and *pazColName are set appropriately |
| 86 | ** but *pazValue is set to NULL. |
| 87 | ** |
| 88 | ** SQLITE_ROW means that the virtual machine has generated another |
| 89 | ** row of the result set. *pN is set to the number of columns in |
| 90 | ** the row. *pazColName is set to the names of the columns followed |
| 91 | ** by the column datatypes. *pazValue is set to the values of each |
| 92 | ** column in the row. The value of the i-th column is (*pazValue)[i]. |
| 93 | ** The name of the i-th column is (*pazColName)[i] and the datatype |
| 94 | ** of the i-th column is (*pazColName)[i+*pN]. |
| 95 | ** |
drh | 3a84069 | 2003-01-29 22:58:26 +0000 | [diff] [blame] | 96 | ** SQLITE_ERROR means that a run-time error (such as a constraint |
| 97 | ** violation) has occurred. The details of the error will be returned |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 98 | ** by the next call to sqlite3_finalize(). sqlite3_step() should not |
drh | 3a84069 | 2003-01-29 22:58:26 +0000 | [diff] [blame] | 99 | ** be called again on the VM. |
| 100 | ** |
| 101 | ** SQLITE_MISUSE means that the this routine was called inappropriately. |
| 102 | ** Perhaps it was called on a virtual machine that had already been |
| 103 | ** finalized or on one that had previously returned SQLITE_ERROR or |
| 104 | ** SQLITE_DONE. Or it could be the case the the same database connection |
| 105 | ** is being used simulataneously by two or more threads. |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 106 | */ |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 107 | int sqlite3_step( |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 108 | sqlite_vm *pVm, /* The virtual machine to execute */ |
| 109 | int *pN, /* OUT: Number of columns in result */ |
| 110 | const char ***pazValue, /* OUT: Column data */ |
| 111 | const char ***pazColName /* OUT: Column names and datatypes */ |
| 112 | ){ |
| 113 | Vdbe *p = (Vdbe*)pVm; |
drh | 326dce7 | 2003-01-29 14:06:07 +0000 | [diff] [blame] | 114 | sqlite *db; |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 115 | int rc; |
| 116 | |
| 117 | if( p->magic!=VDBE_MAGIC_RUN ){ |
| 118 | return SQLITE_MISUSE; |
| 119 | } |
drh | 326dce7 | 2003-01-29 14:06:07 +0000 | [diff] [blame] | 120 | db = p->db; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 121 | if( sqlite3SafetyOn(db) ){ |
drh | a1f9b5e | 2004-02-14 16:31:02 +0000 | [diff] [blame] | 122 | p->rc = SQLITE_MISUSE; |
drh | 326dce7 | 2003-01-29 14:06:07 +0000 | [diff] [blame] | 123 | return SQLITE_MISUSE; |
| 124 | } |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 125 | if( p->explain ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 126 | rc = sqlite3VdbeList(p); |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 127 | }else{ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 128 | rc = sqlite3VdbeExec(p); |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 129 | } |
drh | 326dce7 | 2003-01-29 14:06:07 +0000 | [diff] [blame] | 130 | if( rc==SQLITE_DONE || rc==SQLITE_ROW ){ |
drh | 073e5a7 | 2003-07-09 00:28:13 +0000 | [diff] [blame] | 131 | if( pazColName ) *pazColName = (const char**)p->azColName; |
| 132 | if( pN ) *pN = p->nResColumn; |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 133 | }else{ |
drh | 073e5a7 | 2003-07-09 00:28:13 +0000 | [diff] [blame] | 134 | if( pazColName) *pazColName = 0; |
| 135 | if( pN ) *pN = 0; |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 136 | } |
drh | 073e5a7 | 2003-07-09 00:28:13 +0000 | [diff] [blame] | 137 | if( pazValue ){ |
| 138 | if( rc==SQLITE_ROW ){ |
| 139 | *pazValue = (const char**)p->azResColumn; |
| 140 | }else{ |
| 141 | *pazValue = 0; |
| 142 | } |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 143 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 144 | if( sqlite3SafetyOff(db) ){ |
drh | 326dce7 | 2003-01-29 14:06:07 +0000 | [diff] [blame] | 145 | return SQLITE_MISUSE; |
| 146 | } |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 147 | return rc; |
| 148 | } |
| 149 | |
| 150 | /* |
drh | ac82fcf | 2002-09-08 17:23:41 +0000 | [diff] [blame] | 151 | ** Insert a new aggregate element and make it the element that |
| 152 | ** has focus. |
drh | 600b1b2 | 2000-06-05 21:39:48 +0000 | [diff] [blame] | 153 | ** |
| 154 | ** Return 0 on success and 1 if memory is exhausted. |
| 155 | */ |
drh | db5ed6d | 2001-09-18 22:17:44 +0000 | [diff] [blame] | 156 | static int AggInsert(Agg *p, char *zKey, int nKey){ |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 157 | AggElem *pElem, *pOld; |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 158 | int i; |
drh | 2c79c67 | 2004-01-31 20:20:29 +0000 | [diff] [blame] | 159 | Mem *pMem; |
drh | db5ed6d | 2001-09-18 22:17:44 +0000 | [diff] [blame] | 160 | pElem = sqliteMalloc( sizeof(AggElem) + nKey + |
drh | 600b1b2 | 2000-06-05 21:39:48 +0000 | [diff] [blame] | 161 | (p->nMem-1)*sizeof(pElem->aMem[0]) ); |
| 162 | if( pElem==0 ) return 1; |
| 163 | pElem->zKey = (char*)&pElem->aMem[p->nMem]; |
drh | db5ed6d | 2001-09-18 22:17:44 +0000 | [diff] [blame] | 164 | memcpy(pElem->zKey, zKey, nKey); |
| 165 | pElem->nKey = nKey; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 166 | pOld = sqlite3HashInsert(&p->hash, pElem->zKey, pElem->nKey, pElem); |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 167 | if( pOld!=0 ){ |
| 168 | assert( pOld==pElem ); /* Malloc failed on insert */ |
| 169 | sqliteFree(pOld); |
| 170 | return 0; |
| 171 | } |
drh | 2c79c67 | 2004-01-31 20:20:29 +0000 | [diff] [blame] | 172 | for(i=0, pMem=pElem->aMem; i<p->nMem; i++, pMem++){ |
| 173 | pMem->flags = MEM_Null; |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 174 | } |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 175 | p->pCurrent = pElem; |
drh | 600b1b2 | 2000-06-05 21:39:48 +0000 | [diff] [blame] | 176 | return 0; |
| 177 | } |
| 178 | |
| 179 | /* |
| 180 | ** Get the AggElem currently in focus |
| 181 | */ |
| 182 | #define AggInFocus(P) ((P).pCurrent ? (P).pCurrent : _AggInFocus(&(P))) |
| 183 | static AggElem *_AggInFocus(Agg *p){ |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 184 | HashElem *pElem = sqliteHashFirst(&p->hash); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 185 | if( pElem==0 ){ |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 186 | AggInsert(p,"",1); |
| 187 | pElem = sqliteHashFirst(&p->hash); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 188 | } |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 189 | return pElem ? sqliteHashData(pElem) : 0; |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | /* |
drh | c61053b | 2000-06-04 12:58:36 +0000 | [diff] [blame] | 193 | ** Convert the given stack entity into a string if it isn't one |
drh | 371ac44 | 2003-01-07 13:43:45 +0000 | [diff] [blame] | 194 | ** already. |
drh | c61053b | 2000-06-04 12:58:36 +0000 | [diff] [blame] | 195 | */ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 196 | #define Stringify(P) if(((P)->flags & MEM_Str)==0){hardStringify(P);} |
| 197 | static int hardStringify(Mem *pStack){ |
drh | efa4e17 | 2000-10-19 14:42:04 +0000 | [diff] [blame] | 198 | int fg = pStack->flags; |
drh | 00706be | 2004-01-30 14:49:16 +0000 | [diff] [blame] | 199 | if( fg & MEM_Real ){ |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 200 | sqlite3_snprintf(sizeof(pStack->zShort),pStack->zShort,"%.15g",pStack->r); |
drh | 00706be | 2004-01-30 14:49:16 +0000 | [diff] [blame] | 201 | }else if( fg & MEM_Int ){ |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 202 | sqlite3_snprintf(sizeof(pStack->zShort),pStack->zShort,"%lld",pStack->i); |
drh | c61053b | 2000-06-04 12:58:36 +0000 | [diff] [blame] | 203 | }else{ |
drh | 00706be | 2004-01-30 14:49:16 +0000 | [diff] [blame] | 204 | pStack->zShort[0] = 0; |
drh | c61053b | 2000-06-04 12:58:36 +0000 | [diff] [blame] | 205 | } |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 206 | pStack->z = pStack->zShort; |
drh | 00706be | 2004-01-30 14:49:16 +0000 | [diff] [blame] | 207 | pStack->n = strlen(pStack->zShort)+1; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 208 | pStack->flags = MEM_Str | MEM_Short; |
drh | c61053b | 2000-06-04 12:58:36 +0000 | [diff] [blame] | 209 | return 0; |
| 210 | } |
| 211 | |
| 212 | /* |
drh | 371ac44 | 2003-01-07 13:43:45 +0000 | [diff] [blame] | 213 | ** Convert the given stack entity into a string that has been obtained |
| 214 | ** from sqliteMalloc(). This is different from Stringify() above in that |
| 215 | ** Stringify() will use the NBFS bytes of static string space if the string |
| 216 | ** will fit but this routine always mallocs for space. |
| 217 | ** Return non-zero if we run out of memory. |
| 218 | */ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 219 | #define Dynamicify(P) (((P)->flags & MEM_Dyn)==0 ? hardDynamicify(P):0) |
| 220 | static int hardDynamicify(Mem *pStack){ |
drh | 371ac44 | 2003-01-07 13:43:45 +0000 | [diff] [blame] | 221 | int fg = pStack->flags; |
| 222 | char *z; |
drh | 00706be | 2004-01-30 14:49:16 +0000 | [diff] [blame] | 223 | if( (fg & MEM_Str)==0 ){ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 224 | hardStringify(pStack); |
drh | 371ac44 | 2003-01-07 13:43:45 +0000 | [diff] [blame] | 225 | } |
drh | 00706be | 2004-01-30 14:49:16 +0000 | [diff] [blame] | 226 | assert( (fg & MEM_Dyn)==0 ); |
drh | 371ac44 | 2003-01-07 13:43:45 +0000 | [diff] [blame] | 227 | z = sqliteMallocRaw( pStack->n ); |
| 228 | if( z==0 ) return 1; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 229 | memcpy(z, pStack->z, pStack->n); |
| 230 | pStack->z = z; |
drh | 00706be | 2004-01-30 14:49:16 +0000 | [diff] [blame] | 231 | pStack->flags |= MEM_Dyn; |
drh | 371ac44 | 2003-01-07 13:43:45 +0000 | [diff] [blame] | 232 | return 0; |
| 233 | } |
| 234 | |
| 235 | /* |
drh | 00706be | 2004-01-30 14:49:16 +0000 | [diff] [blame] | 236 | ** An ephemeral string value (signified by the MEM_Ephem flag) contains |
drh | e958bb4 | 2002-10-22 15:04:34 +0000 | [diff] [blame] | 237 | ** a pointer to a dynamically allocated string where some other entity |
| 238 | ** is responsible for deallocating that string. Because the stack entry |
| 239 | ** does not control the string, it might be deleted without the stack |
| 240 | ** entry knowing it. |
| 241 | ** |
| 242 | ** This routine converts an ephemeral string into a dynamically allocated |
| 243 | ** string that the stack entry itself controls. In other words, it |
drh | 00706be | 2004-01-30 14:49:16 +0000 | [diff] [blame] | 244 | ** converts an MEM_Ephem string into an MEM_Dyn string. |
drh | e958bb4 | 2002-10-22 15:04:34 +0000 | [diff] [blame] | 245 | */ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 246 | #define Deephemeralize(P) \ |
| 247 | if( ((P)->flags&MEM_Ephem)!=0 && hardDeephem(P) ){ goto no_mem;} |
| 248 | static int hardDeephem(Mem *pStack){ |
drh | e958bb4 | 2002-10-22 15:04:34 +0000 | [diff] [blame] | 249 | char *z; |
drh | 00706be | 2004-01-30 14:49:16 +0000 | [diff] [blame] | 250 | assert( (pStack->flags & MEM_Ephem)!=0 ); |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 251 | z = sqliteMallocRaw( pStack->n ); |
drh | e958bb4 | 2002-10-22 15:04:34 +0000 | [diff] [blame] | 252 | if( z==0 ) return 1; |
drh | 00706be | 2004-01-30 14:49:16 +0000 | [diff] [blame] | 253 | memcpy(z, pStack->z, pStack->n); |
| 254 | pStack->z = z; |
| 255 | pStack->flags &= ~MEM_Ephem; |
| 256 | pStack->flags |= MEM_Dyn; |
drh | e958bb4 | 2002-10-22 15:04:34 +0000 | [diff] [blame] | 257 | return 0; |
| 258 | } |
| 259 | |
| 260 | /* |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 261 | ** Release the memory associated with the given stack level. This |
| 262 | ** leaves the Mem.flags field in an inconsistent state. |
drh | c61053b | 2000-06-04 12:58:36 +0000 | [diff] [blame] | 263 | */ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 264 | #define Release(P) if((P)->flags&MEM_Dyn){ sqliteFree((P)->z); } |
| 265 | |
| 266 | /* |
| 267 | ** Pop the stack N times. |
| 268 | */ |
| 269 | static void popStack(Mem **ppTos, int N){ |
| 270 | Mem *pTos = *ppTos; |
| 271 | while( N>0 ){ |
| 272 | N--; |
| 273 | Release(pTos); |
| 274 | pTos--; |
| 275 | } |
| 276 | *ppTos = pTos; |
drh | c61053b | 2000-06-04 12:58:36 +0000 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | /* |
drh | dc2d94d | 2003-07-27 17:16:06 +0000 | [diff] [blame] | 280 | ** Return TRUE if zNum is a 32-bit signed integer and write |
| 281 | ** the value of the integer into *pNum. If zNum is not an integer |
| 282 | ** or is an integer that is too large to be expressed with just 32 |
| 283 | ** bits, then return false. |
drh | 32c05e9 | 2002-11-11 00:05:42 +0000 | [diff] [blame] | 284 | ** |
| 285 | ** Under Linux (RedHat 7.2) this routine is much faster than atoi() |
| 286 | ** for converting strings into integers. |
| 287 | */ |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 288 | static int toInt(const char *zNum, i64 *pNum){ |
| 289 | i64 v = 0; |
drh | 32c05e9 | 2002-11-11 00:05:42 +0000 | [diff] [blame] | 290 | int neg; |
drh | dc2d94d | 2003-07-27 17:16:06 +0000 | [diff] [blame] | 291 | int i, c; |
drh | 32c05e9 | 2002-11-11 00:05:42 +0000 | [diff] [blame] | 292 | if( *zNum=='-' ){ |
| 293 | neg = 1; |
| 294 | zNum++; |
| 295 | }else if( *zNum=='+' ){ |
| 296 | neg = 0; |
| 297 | zNum++; |
| 298 | }else{ |
| 299 | neg = 0; |
| 300 | } |
drh | dc2d94d | 2003-07-27 17:16:06 +0000 | [diff] [blame] | 301 | for(i=0; (c=zNum[i])>='0' && c<='9'; i++){ |
| 302 | v = v*10 + c - '0'; |
drh | 32c05e9 | 2002-11-11 00:05:42 +0000 | [diff] [blame] | 303 | } |
| 304 | *pNum = neg ? -v : v; |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 305 | return c==0 && i>0 && |
| 306 | (i<10 || (i==19 && memcmp(zNum,"9223372036854775807",19)<=0)); |
drh | 32c05e9 | 2002-11-11 00:05:42 +0000 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | /* |
drh | c61053b | 2000-06-04 12:58:36 +0000 | [diff] [blame] | 310 | ** Convert the given stack entity into a integer if it isn't one |
| 311 | ** already. |
| 312 | ** |
| 313 | ** Any prior string or real representation is invalidated. |
| 314 | ** NULLs are converted into 0. |
| 315 | */ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 316 | #define Integerify(P) if(((P)->flags&MEM_Int)==0){ hardIntegerify(P); } |
| 317 | static void hardIntegerify(Mem *pStack){ |
| 318 | if( pStack->flags & MEM_Real ){ |
| 319 | pStack->i = (int)pStack->r; |
| 320 | Release(pStack); |
| 321 | }else if( pStack->flags & MEM_Str ){ |
| 322 | toInt(pStack->z, &pStack->i); |
| 323 | Release(pStack); |
drh | c61053b | 2000-06-04 12:58:36 +0000 | [diff] [blame] | 324 | }else{ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 325 | pStack->i = 0; |
drh | c61053b | 2000-06-04 12:58:36 +0000 | [diff] [blame] | 326 | } |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 327 | pStack->flags = MEM_Int; |
drh | c61053b | 2000-06-04 12:58:36 +0000 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | /* |
| 331 | ** Get a valid Real representation for the given stack element. |
| 332 | ** |
| 333 | ** Any prior string or integer representation is retained. |
| 334 | ** NULLs are converted into 0.0. |
| 335 | */ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 336 | #define Realify(P) if(((P)->flags&MEM_Real)==0){ hardRealify(P); } |
| 337 | static void hardRealify(Mem *pStack){ |
| 338 | if( pStack->flags & MEM_Str ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 339 | pStack->r = sqlite3AtoF(pStack->z, 0); |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 340 | }else if( pStack->flags & MEM_Int ){ |
| 341 | pStack->r = pStack->i; |
drh | c61053b | 2000-06-04 12:58:36 +0000 | [diff] [blame] | 342 | }else{ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 343 | pStack->r = 0.0; |
drh | c61053b | 2000-06-04 12:58:36 +0000 | [diff] [blame] | 344 | } |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 345 | pStack->flags |= MEM_Real; |
drh | c61053b | 2000-06-04 12:58:36 +0000 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 349 | ** The parameters are pointers to the head of two sorted lists |
| 350 | ** of Sorter structures. Merge these two lists together and return |
| 351 | ** a single sorted list. This routine forms the core of the merge-sort |
| 352 | ** algorithm. |
| 353 | ** |
| 354 | ** In the case of a tie, left sorts in front of right. |
| 355 | */ |
| 356 | static Sorter *Merge(Sorter *pLeft, Sorter *pRight){ |
| 357 | Sorter sHead; |
| 358 | Sorter *pTail; |
| 359 | pTail = &sHead; |
| 360 | pTail->pNext = 0; |
| 361 | while( pLeft && pRight ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 362 | int c = sqlite3SortCompare(pLeft->zKey, pRight->zKey); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 363 | if( c<=0 ){ |
| 364 | pTail->pNext = pLeft; |
| 365 | pLeft = pLeft->pNext; |
| 366 | }else{ |
| 367 | pTail->pNext = pRight; |
| 368 | pRight = pRight->pNext; |
| 369 | } |
| 370 | pTail = pTail->pNext; |
| 371 | } |
| 372 | if( pLeft ){ |
| 373 | pTail->pNext = pLeft; |
| 374 | }else if( pRight ){ |
| 375 | pTail->pNext = pRight; |
| 376 | } |
| 377 | return sHead.pNext; |
| 378 | } |
| 379 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 380 | /* |
drh | 62160e7 | 2002-07-30 17:20:40 +0000 | [diff] [blame] | 381 | ** The following routine works like a replacement for the standard |
| 382 | ** library routine fgets(). The difference is in how end-of-line (EOL) |
| 383 | ** is handled. Standard fgets() uses LF for EOL under unix, CRLF |
| 384 | ** under windows, and CR under mac. This routine accepts any of these |
| 385 | ** character sequences as an EOL mark. The EOL mark is replaced by |
| 386 | ** a single LF character in zBuf. |
| 387 | */ |
| 388 | static char *vdbe_fgets(char *zBuf, int nBuf, FILE *in){ |
| 389 | int i, c; |
| 390 | for(i=0; i<nBuf-1 && (c=getc(in))!=EOF; i++){ |
| 391 | zBuf[i] = c; |
| 392 | if( c=='\r' || c=='\n' ){ |
| 393 | if( c=='\r' ){ |
| 394 | zBuf[i] = '\n'; |
| 395 | c = getc(in); |
| 396 | if( c!=EOF && c!='\n' ) ungetc(c, in); |
| 397 | } |
| 398 | i++; |
| 399 | break; |
| 400 | } |
| 401 | } |
| 402 | zBuf[i] = 0; |
| 403 | return i>0 ? zBuf : 0; |
| 404 | } |
| 405 | |
| 406 | /* |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 407 | ** Make sure there is space in the Vdbe structure to hold at least |
| 408 | ** mxCursor cursors. If there is not currently enough space, then |
| 409 | ** allocate more. |
| 410 | ** |
| 411 | ** If a memory allocation error occurs, return 1. Return 0 if |
| 412 | ** everything works. |
| 413 | */ |
| 414 | static int expandCursorArraySize(Vdbe *p, int mxCursor){ |
| 415 | if( mxCursor>=p->nCursor ){ |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame^] | 416 | p->apCsr = sqliteRealloc( p->apCsr, (mxCursor+1)*sizeof(Cursor*) ); |
| 417 | if( p->apCsr==0 ) return 1; |
| 418 | while( p->nCursor<=mxCursor ){ |
| 419 | Cursor *pC; |
| 420 | p->apCsr[p->nCursor++] = pC = sqliteMalloc( sizeof(Cursor) ); |
| 421 | if( pC==0 ) return 1; |
| 422 | } |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 423 | } |
| 424 | return 0; |
| 425 | } |
| 426 | |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 427 | /* |
| 428 | ** Apply any conversion required by the supplied column affinity to |
| 429 | ** memory cell pRec. affinity may be one of: |
| 430 | ** |
| 431 | ** SQLITE_AFF_NUM |
| 432 | ** SQLITE_AFF_TEXT |
| 433 | ** SQLITE_AFF_NONE |
| 434 | ** SQLITE_AFF_INTEGER |
| 435 | ** |
| 436 | */ |
| 437 | static void applyAffinity(Mem *pRec, int affinity){ |
| 438 | switch( affinity ){ |
| 439 | case SQLITE_SO_NUM: |
| 440 | if( 0==(pRec->flags&(MEM_Real|MEM_Int)) ){ |
| 441 | /* pRec does not have a valid integer or real representation. |
| 442 | ** Attempt a conversion if pRec has a string representation and |
| 443 | ** it looks like a number. |
| 444 | */ |
| 445 | int realnum; |
| 446 | if( pRec->flags&MEM_Str && sqlite3IsNumber(pRec->z, &realnum) ){ |
| 447 | if( realnum ){ |
| 448 | Realify(pRec); |
| 449 | }else{ |
| 450 | Integerify(pRec); |
| 451 | } |
| 452 | } |
| 453 | } |
| 454 | break; |
| 455 | case SQLITE_SO_TEXT: |
| 456 | /* Only attempt the conversion if there is an integer or real |
| 457 | ** representation (blob and NULL do not get converted) but no string |
| 458 | ** representation. |
| 459 | */ |
| 460 | if( 0==(pRec->flags&MEM_Str) && (pRec->flags&(MEM_Real|MEM_Int)) ){ |
| 461 | Stringify(pRec); |
| 462 | } |
| 463 | pRec->flags &= ~(MEM_Real|MEM_Int); |
| 464 | |
| 465 | break; |
| 466 | |
| 467 | /* |
| 468 | case SQLITE_AFF_INTEGER: |
| 469 | case SQLITE_AFF_NONE: |
| 470 | break; |
| 471 | */ |
| 472 | default: |
| 473 | assert(0); |
| 474 | } |
| 475 | } |
| 476 | |
| 477 | /* |
| 478 | ** This function interprets the character 'affinity' according to the |
| 479 | ** following table and calls the applyAffinity() function. |
| 480 | */ |
| 481 | static void applyAffinityByChar(Mem *pRec, char affinity){ |
| 482 | switch( affinity ){ |
| 483 | case 'n': return applyAffinity(pRec, SQLITE_SO_NUM); |
| 484 | case 't': return applyAffinity(pRec, SQLITE_SO_TEXT); |
| 485 | default: assert(0); |
| 486 | } |
| 487 | } |
| 488 | |
drh | 7b39686 | 2003-01-01 23:06:20 +0000 | [diff] [blame] | 489 | #ifdef VDBE_PROFILE |
| 490 | /* |
| 491 | ** The following routine only works on pentium-class processors. |
| 492 | ** It uses the RDTSC opcode to read cycle count value out of the |
| 493 | ** processor and returns that value. This can be used for high-res |
| 494 | ** profiling. |
| 495 | */ |
| 496 | __inline__ unsigned long long int hwtime(void){ |
| 497 | unsigned long long int x; |
| 498 | __asm__("rdtsc\n\t" |
| 499 | "mov %%edx, %%ecx\n\t" |
| 500 | :"=A" (x)); |
| 501 | return x; |
| 502 | } |
| 503 | #endif |
| 504 | |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 505 | /* |
drh | caec2f1 | 2003-01-07 02:47:47 +0000 | [diff] [blame] | 506 | ** The CHECK_FOR_INTERRUPT macro defined here looks to see if the |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 507 | ** sqlite3_interrupt() routine has been called. If it has been, then |
drh | caec2f1 | 2003-01-07 02:47:47 +0000 | [diff] [blame] | 508 | ** processing of the VDBE program is interrupted. |
| 509 | ** |
| 510 | ** This macro added to every instruction that does a jump in order to |
| 511 | ** implement a loop. This test used to be on every single instruction, |
| 512 | ** but that meant we more testing that we needed. By only testing the |
| 513 | ** flag on jump instructions, we get a (small) speed improvement. |
| 514 | */ |
| 515 | #define CHECK_FOR_INTERRUPT \ |
| 516 | if( db->flags & SQLITE_Interrupt ) goto abort_due_to_interrupt; |
| 517 | |
| 518 | |
| 519 | /* |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 520 | ** Execute as much of a VDBE program as we can then return. |
| 521 | ** |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 522 | ** sqlite3VdbeMakeReady() must be called before this routine in order to |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 523 | ** close the program with a final OP_Halt and to set up the callbacks |
| 524 | ** and the error message pointer. |
| 525 | ** |
| 526 | ** Whenever a row or result data is available, this routine will either |
| 527 | ** invoke the result callback (if there is one) or return with |
drh | 326dce7 | 2003-01-29 14:06:07 +0000 | [diff] [blame] | 528 | ** SQLITE_ROW. |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 529 | ** |
| 530 | ** If an attempt is made to open a locked database, then this routine |
| 531 | ** will either invoke the busy callback (if there is one) or it will |
| 532 | ** return SQLITE_BUSY. |
| 533 | ** |
| 534 | ** If an error occurs, an error message is written to memory obtained |
| 535 | ** from sqliteMalloc() and p->zErrMsg is made to point to that memory. |
| 536 | ** The error code is stored in p->rc and this routine returns SQLITE_ERROR. |
| 537 | ** |
| 538 | ** If the callback ever returns non-zero, then the program exits |
| 539 | ** immediately. There will be no error message but the p->rc field is |
| 540 | ** set to SQLITE_ABORT and this routine will return SQLITE_ERROR. |
| 541 | ** |
drh | 9468c7f | 2003-03-07 19:50:07 +0000 | [diff] [blame] | 542 | ** A memory allocation error causes p->rc to be set to SQLITE_NOMEM and this |
| 543 | ** routine to return SQLITE_ERROR. |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 544 | ** |
| 545 | ** Other fatal errors return SQLITE_ERROR. |
| 546 | ** |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 547 | ** After this routine has finished, sqlite3VdbeFinalize() should be |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 548 | ** used to clean up the mess that was left behind. |
| 549 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 550 | int sqlite3VdbeExec( |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 551 | Vdbe *p /* The VDBE */ |
| 552 | ){ |
| 553 | int pc; /* The program counter */ |
| 554 | Op *pOp; /* Current operation */ |
| 555 | int rc = SQLITE_OK; /* Value to return */ |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 556 | sqlite *db = p->db; /* The database */ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 557 | Mem *pTos; /* Top entry in the operand stack */ |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 558 | char zBuf[100]; /* Space to sprintf() an integer */ |
| 559 | #ifdef VDBE_PROFILE |
| 560 | unsigned long long start; /* CPU clock count at start of opcode */ |
| 561 | int origPc; /* Program counter at start of opcode */ |
| 562 | #endif |
danielk1977 | 348bb5d | 2003-10-18 09:37:26 +0000 | [diff] [blame] | 563 | #ifndef SQLITE_OMIT_PROGRESS_CALLBACK |
| 564 | int nProgressOps = 0; /* Opcodes executed since progress callback. */ |
| 565 | #endif |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 566 | |
| 567 | if( p->magic!=VDBE_MAGIC_RUN ) return SQLITE_MISUSE; |
| 568 | assert( db->magic==SQLITE_MAGIC_BUSY ); |
drh | 3a84069 | 2003-01-29 22:58:26 +0000 | [diff] [blame] | 569 | assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY ); |
| 570 | p->rc = SQLITE_OK; |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 571 | assert( p->explain==0 ); |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 572 | if( sqlite3_malloc_failed ) goto no_mem; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 573 | pTos = p->pTos; |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 574 | if( p->popStack ){ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 575 | popStack(&pTos, p->popStack); |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 576 | p->popStack = 0; |
| 577 | } |
drh | 9358164 | 2004-02-12 13:02:55 +0000 | [diff] [blame] | 578 | CHECK_FOR_INTERRUPT; |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 579 | for(pc=p->pc; rc==SQLITE_OK; pc++){ |
drh | caec2f1 | 2003-01-07 02:47:47 +0000 | [diff] [blame] | 580 | assert( pc>=0 && pc<p->nOp ); |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 581 | assert( pTos<=&p->aStack[pc] ); |
drh | 7b39686 | 2003-01-01 23:06:20 +0000 | [diff] [blame] | 582 | #ifdef VDBE_PROFILE |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 583 | origPc = pc; |
drh | 7b39686 | 2003-01-01 23:06:20 +0000 | [diff] [blame] | 584 | start = hwtime(); |
| 585 | #endif |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 586 | pOp = &p->aOp[pc]; |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 587 | |
| 588 | /* Only allow tracing if NDEBUG is not defined. |
| 589 | */ |
| 590 | #ifndef NDEBUG |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 591 | if( p->trace ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 592 | sqlite3VdbePrintOp(p->trace, pc, pOp); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 593 | } |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 594 | #endif |
| 595 | |
drh | f603871 | 2004-02-08 18:07:34 +0000 | [diff] [blame] | 596 | /* Check to see if we need to simulate an interrupt. This only happens |
| 597 | ** if we have a special test build. |
| 598 | */ |
| 599 | #ifdef SQLITE_TEST |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 600 | if( sqlite3_interrupt_count>0 ){ |
| 601 | sqlite3_interrupt_count--; |
| 602 | if( sqlite3_interrupt_count==0 ){ |
| 603 | sqlite3_interrupt(db); |
drh | f603871 | 2004-02-08 18:07:34 +0000 | [diff] [blame] | 604 | } |
| 605 | } |
| 606 | #endif |
| 607 | |
danielk1977 | 348bb5d | 2003-10-18 09:37:26 +0000 | [diff] [blame] | 608 | #ifndef SQLITE_OMIT_PROGRESS_CALLBACK |
| 609 | /* Call the progress callback if it is configured and the required number |
| 610 | ** of VDBE ops have been executed (either since this invocation of |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 611 | ** sqlite3VdbeExec() or since last time the progress callback was called). |
danielk1977 | 348bb5d | 2003-10-18 09:37:26 +0000 | [diff] [blame] | 612 | ** If the progress callback returns non-zero, exit the virtual machine with |
| 613 | ** a return code SQLITE_ABORT. |
| 614 | */ |
drh | 3914aed | 2004-01-31 20:40:42 +0000 | [diff] [blame] | 615 | if( db->xProgress ){ |
| 616 | if( db->nProgressOps==nProgressOps ){ |
| 617 | if( db->xProgress(db->pProgressArg)!=0 ){ |
| 618 | rc = SQLITE_ABORT; |
| 619 | continue; /* skip to the next iteration of the for loop */ |
| 620 | } |
| 621 | nProgressOps = 0; |
danielk1977 | 348bb5d | 2003-10-18 09:37:26 +0000 | [diff] [blame] | 622 | } |
drh | 3914aed | 2004-01-31 20:40:42 +0000 | [diff] [blame] | 623 | nProgressOps++; |
danielk1977 | 348bb5d | 2003-10-18 09:37:26 +0000 | [diff] [blame] | 624 | } |
danielk1977 | 348bb5d | 2003-10-18 09:37:26 +0000 | [diff] [blame] | 625 | #endif |
| 626 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 627 | switch( pOp->opcode ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 628 | |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 629 | /***************************************************************************** |
| 630 | ** What follows is a massive switch statement where each case implements a |
| 631 | ** separate instruction in the virtual machine. If we follow the usual |
| 632 | ** indentation conventions, each case should be indented by 6 spaces. But |
| 633 | ** that is a lot of wasted space on the left margin. So the code within |
| 634 | ** the switch statement will break with convention and be flush-left. Another |
| 635 | ** big comment (similar to this one) will mark the point in the code where |
| 636 | ** we transition back to normal indentation. |
drh | ac82fcf | 2002-09-08 17:23:41 +0000 | [diff] [blame] | 637 | ** |
| 638 | ** The formatting of each case is important. The makefile for SQLite |
| 639 | ** generates two C files "opcodes.h" and "opcodes.c" by scanning this |
| 640 | ** file looking for lines that begin with "case OP_". The opcodes.h files |
| 641 | ** will be filled with #defines that give unique integer values to each |
| 642 | ** opcode and the opcodes.c file is filled with an array of strings where |
| 643 | ** each string is the symbolic name for the corresponding opcode. |
| 644 | ** |
| 645 | ** Documentation about VDBE opcodes is generated by scanning this file |
| 646 | ** for lines of that contain "Opcode:". That line and all subsequent |
| 647 | ** comment lines are used in the generation of the opcode.html documentation |
| 648 | ** file. |
| 649 | ** |
| 650 | ** SUMMARY: |
| 651 | ** |
| 652 | ** Formatting is important to scripts that scan this file. |
| 653 | ** Do not deviate from the formatting style currently in use. |
| 654 | ** |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 655 | *****************************************************************************/ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 656 | |
drh | 58a1168 | 2001-11-10 13:51:08 +0000 | [diff] [blame] | 657 | /* Opcode: Goto * P2 * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 658 | ** |
| 659 | ** An unconditional jump to address P2. |
| 660 | ** The next instruction executed will be |
| 661 | ** the one at index P2 from the beginning of |
| 662 | ** the program. |
| 663 | */ |
| 664 | case OP_Goto: { |
drh | caec2f1 | 2003-01-07 02:47:47 +0000 | [diff] [blame] | 665 | CHECK_FOR_INTERRUPT; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 666 | pc = pOp->p2 - 1; |
| 667 | break; |
| 668 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 669 | |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 670 | /* Opcode: Gosub * P2 * |
| 671 | ** |
| 672 | ** Push the current address plus 1 onto the return address stack |
| 673 | ** and then jump to address P2. |
| 674 | ** |
| 675 | ** The return address stack is of limited depth. If too many |
| 676 | ** OP_Gosub operations occur without intervening OP_Returns, then |
| 677 | ** the return address stack will fill up and processing will abort |
| 678 | ** with a fatal error. |
| 679 | */ |
| 680 | case OP_Gosub: { |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 681 | if( p->returnDepth>=sizeof(p->returnStack)/sizeof(p->returnStack[0]) ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 682 | sqlite3SetString(&p->zErrMsg, "return address stack overflow", (char*)0); |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 683 | p->rc = SQLITE_INTERNAL; |
| 684 | return SQLITE_ERROR; |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 685 | } |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 686 | p->returnStack[p->returnDepth++] = pc+1; |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 687 | pc = pOp->p2 - 1; |
| 688 | break; |
| 689 | } |
| 690 | |
| 691 | /* Opcode: Return * * * |
| 692 | ** |
| 693 | ** Jump immediately to the next instruction after the last unreturned |
| 694 | ** OP_Gosub. If an OP_Return has occurred for all OP_Gosubs, then |
| 695 | ** processing aborts with a fatal error. |
| 696 | */ |
| 697 | case OP_Return: { |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 698 | if( p->returnDepth<=0 ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 699 | sqlite3SetString(&p->zErrMsg, "return address stack underflow", (char*)0); |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 700 | p->rc = SQLITE_INTERNAL; |
| 701 | return SQLITE_ERROR; |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 702 | } |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 703 | p->returnDepth--; |
| 704 | pc = p->returnStack[p->returnDepth] - 1; |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 705 | break; |
| 706 | } |
| 707 | |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 708 | /* Opcode: Halt P1 P2 * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 709 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 710 | ** Exit immediately. All open cursors, Lists, Sorts, etc are closed |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 711 | ** automatically. |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 712 | ** |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 713 | ** P1 is the result code returned by sqlite3_exec(). For a normal |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 714 | ** halt, this should be SQLITE_OK (0). For errors, it can be some |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 715 | ** other value. If P1!=0 then P2 will determine whether or not to |
| 716 | ** rollback the current transaction. Do not rollback if P2==OE_Fail. |
| 717 | ** Do the rollback if P2==OE_Rollback. If P2==OE_Abort, then back |
| 718 | ** out all changes that have occurred during this execution of the |
drh | b798fa6 | 2002-09-03 19:43:23 +0000 | [diff] [blame] | 719 | ** VDBE, but do not rollback the transaction. |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 720 | ** |
| 721 | ** There is an implied "Halt 0 0 0" instruction inserted at the very end of |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 722 | ** every program. So a jump past the last instruction of the program |
| 723 | ** is the same as executing Halt. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 724 | */ |
| 725 | case OP_Halt: { |
drh | 483750b | 2003-01-29 18:46:51 +0000 | [diff] [blame] | 726 | p->magic = VDBE_MAGIC_HALT; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 727 | p->pTos = pTos; |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 728 | if( pOp->p1!=SQLITE_OK ){ |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 729 | p->rc = pOp->p1; |
| 730 | p->errorAction = pOp->p2; |
danielk1977 | 6f34903 | 2002-06-11 02:25:40 +0000 | [diff] [blame] | 731 | if( pOp->p3 ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 732 | sqlite3SetString(&p->zErrMsg, pOp->p3, (char*)0); |
danielk1977 | 6f34903 | 2002-06-11 02:25:40 +0000 | [diff] [blame] | 733 | } |
drh | 483750b | 2003-01-29 18:46:51 +0000 | [diff] [blame] | 734 | return SQLITE_ERROR; |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 735 | }else{ |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 736 | p->rc = SQLITE_OK; |
drh | 483750b | 2003-01-29 18:46:51 +0000 | [diff] [blame] | 737 | return SQLITE_DONE; |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 738 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 739 | } |
drh | c61053b | 2000-06-04 12:58:36 +0000 | [diff] [blame] | 740 | |
drh | e684090 | 2002-03-06 03:08:25 +0000 | [diff] [blame] | 741 | /* Opcode: Integer P1 * P3 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 742 | ** |
drh | e684090 | 2002-03-06 03:08:25 +0000 | [diff] [blame] | 743 | ** The integer value P1 is pushed onto the stack. If P3 is not zero |
| 744 | ** then it is assumed to be a string representation of the same integer. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 745 | */ |
| 746 | case OP_Integer: { |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 747 | pTos++; |
| 748 | pTos->i = pOp->p1; |
| 749 | pTos->flags = MEM_Int; |
drh | e684090 | 2002-03-06 03:08:25 +0000 | [diff] [blame] | 750 | if( pOp->p3 ){ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 751 | pTos->z = pOp->p3; |
| 752 | pTos->flags |= MEM_Str | MEM_Static; |
| 753 | pTos->n = strlen(pOp->p3)+1; |
drh | e684090 | 2002-03-06 03:08:25 +0000 | [diff] [blame] | 754 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 755 | break; |
| 756 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 757 | |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 758 | /* Opcode: String * * P3 |
| 759 | ** |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 760 | ** The string value P3 is pushed onto the stack. If P3==0 then a |
| 761 | ** NULL is pushed onto the stack. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 762 | */ |
| 763 | case OP_String: { |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 764 | char *z = pOp->p3; |
| 765 | pTos++; |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 766 | if( z==0 ){ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 767 | pTos->flags = MEM_Null; |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 768 | }else{ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 769 | pTos->z = z; |
| 770 | pTos->n = strlen(z) + 1; |
| 771 | pTos->flags = MEM_Str | MEM_Static; |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 772 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 773 | break; |
| 774 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 775 | |
drh | 5045789 | 2003-09-06 01:10:47 +0000 | [diff] [blame] | 776 | /* Opcode: Variable P1 * * |
| 777 | ** |
| 778 | ** Push the value of variable P1 onto the stack. A variable is |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 779 | ** an unknown in the original SQL string as handed to sqlite3_compile(). |
drh | 7c972de | 2003-09-06 22:18:07 +0000 | [diff] [blame] | 780 | ** Any occurance of the '?' character in the original SQL is considered |
| 781 | ** a variable. Variables in the SQL string are number from left to |
| 782 | ** right beginning with 1. The values of variables are set using the |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 783 | ** sqlite3_bind() API. |
drh | 5045789 | 2003-09-06 01:10:47 +0000 | [diff] [blame] | 784 | */ |
| 785 | case OP_Variable: { |
drh | 7c972de | 2003-09-06 22:18:07 +0000 | [diff] [blame] | 786 | int j = pOp->p1 - 1; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 787 | pTos++; |
drh | 7c972de | 2003-09-06 22:18:07 +0000 | [diff] [blame] | 788 | if( j>=0 && j<p->nVar && p->azVar[j]!=0 ){ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 789 | pTos->z = p->azVar[j]; |
| 790 | pTos->n = p->anVar[j]; |
| 791 | pTos->flags = MEM_Str | MEM_Static; |
drh | 5045789 | 2003-09-06 01:10:47 +0000 | [diff] [blame] | 792 | }else{ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 793 | pTos->flags = MEM_Null; |
drh | 5045789 | 2003-09-06 01:10:47 +0000 | [diff] [blame] | 794 | } |
| 795 | break; |
| 796 | } |
| 797 | |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 798 | /* Opcode: Pop P1 * * |
| 799 | ** |
| 800 | ** P1 elements are popped off of the top of stack and discarded. |
| 801 | */ |
| 802 | case OP_Pop: { |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 803 | assert( pOp->p1>=0 ); |
| 804 | popStack(&pTos, pOp->p1); |
| 805 | assert( pTos>=&p->aStack[-1] ); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 806 | break; |
| 807 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 808 | |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 809 | /* Opcode: Dup P1 P2 * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 810 | ** |
| 811 | ** A copy of the P1-th element of the stack |
| 812 | ** is made and pushed onto the top of the stack. |
| 813 | ** The top of the stack is element 0. So the |
| 814 | ** instruction "Dup 0 0 0" will make a copy of the |
| 815 | ** top of the stack. |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 816 | ** |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 817 | ** If the content of the P1-th element is a dynamically |
| 818 | ** allocated string, then a new copy of that string |
| 819 | ** is made if P2==0. If P2!=0, then just a pointer |
| 820 | ** to the string is copied. |
| 821 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 822 | ** Also see the Pull instruction. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 823 | */ |
| 824 | case OP_Dup: { |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 825 | Mem *pFrom = &pTos[-pOp->p1]; |
| 826 | assert( pFrom<=pTos && pFrom>=p->aStack ); |
| 827 | pTos++; |
| 828 | memcpy(pTos, pFrom, sizeof(*pFrom)-NBFS); |
| 829 | if( pTos->flags & MEM_Str ){ |
| 830 | if( pOp->p2 && (pTos->flags & (MEM_Dyn|MEM_Ephem)) ){ |
| 831 | pTos->flags &= ~MEM_Dyn; |
| 832 | pTos->flags |= MEM_Ephem; |
| 833 | }else if( pTos->flags & MEM_Short ){ |
| 834 | memcpy(pTos->zShort, pFrom->zShort, pTos->n); |
| 835 | pTos->z = pTos->zShort; |
| 836 | }else if( (pTos->flags & MEM_Static)==0 ){ |
| 837 | pTos->z = sqliteMallocRaw(pFrom->n); |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 838 | if( sqlite3_malloc_failed ) goto no_mem; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 839 | memcpy(pTos->z, pFrom->z, pFrom->n); |
| 840 | pTos->flags &= ~(MEM_Static|MEM_Ephem|MEM_Short); |
| 841 | pTos->flags |= MEM_Dyn; |
drh | 9bbca4c | 2001-11-06 04:00:18 +0000 | [diff] [blame] | 842 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 843 | } |
| 844 | break; |
| 845 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 846 | |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 847 | /* Opcode: Pull P1 * * |
| 848 | ** |
| 849 | ** The P1-th element is removed from its current location on |
| 850 | ** the stack and pushed back on top of the stack. The |
| 851 | ** top of the stack is element 0, so "Pull 0 0 0" is |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 852 | ** a no-op. "Pull 1 0 0" swaps the top two elements of |
| 853 | ** the stack. |
| 854 | ** |
| 855 | ** See also the Dup instruction. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 856 | */ |
| 857 | case OP_Pull: { |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 858 | Mem *pFrom = &pTos[-pOp->p1]; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 859 | int i; |
drh | 00706be | 2004-01-30 14:49:16 +0000 | [diff] [blame] | 860 | Mem ts; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 861 | |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 862 | ts = *pFrom; |
| 863 | Deephemeralize(pTos); |
| 864 | for(i=0; i<pOp->p1; i++, pFrom++){ |
| 865 | Deephemeralize(&pFrom[1]); |
| 866 | *pFrom = pFrom[1]; |
| 867 | assert( (pFrom->flags & MEM_Ephem)==0 ); |
| 868 | if( pFrom->flags & MEM_Short ){ |
| 869 | assert( pFrom->flags & MEM_Str ); |
| 870 | assert( pFrom->z==pFrom[1].zShort ); |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 871 | pFrom->z = pFrom->zShort; |
drh | 9bbca4c | 2001-11-06 04:00:18 +0000 | [diff] [blame] | 872 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 873 | } |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 874 | *pTos = ts; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 875 | if( pTos->flags & MEM_Short ){ |
| 876 | assert( pTos->flags & MEM_Str ); |
| 877 | assert( pTos->z==pTos[-pOp->p1].zShort ); |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 878 | pTos->z = pTos->zShort; |
drh | 9bbca4c | 2001-11-06 04:00:18 +0000 | [diff] [blame] | 879 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 880 | break; |
| 881 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 882 | |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 883 | /* Opcode: Push P1 * * |
| 884 | ** |
| 885 | ** Overwrite the value of the P1-th element down on the |
| 886 | ** stack (P1==0 is the top of the stack) with the value |
drh | ac82fcf | 2002-09-08 17:23:41 +0000 | [diff] [blame] | 887 | ** of the top of the stack. Then pop the top of the stack. |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 888 | */ |
| 889 | case OP_Push: { |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 890 | Mem *pTo = &pTos[-pOp->p1]; |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 891 | |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 892 | assert( pTo>=p->aStack ); |
| 893 | Deephemeralize(pTos); |
| 894 | Release(pTo); |
| 895 | *pTo = *pTos; |
| 896 | if( pTo->flags & MEM_Short ){ |
| 897 | assert( pTo->z==pTos->zShort ); |
| 898 | pTo->z = pTo->zShort; |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 899 | } |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 900 | pTos--; |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 901 | break; |
| 902 | } |
| 903 | |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 904 | |
drh | d650275 | 2004-02-16 03:44:01 +0000 | [diff] [blame] | 905 | /* Opcode: ColumnName P1 P2 P3 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 906 | ** |
| 907 | ** P3 becomes the P1-th column name (first is 0). An array of pointers |
| 908 | ** to all column names is passed as the 4th parameter to the callback. |
drh | d650275 | 2004-02-16 03:44:01 +0000 | [diff] [blame] | 909 | ** If P2==1 then this is the last column in the result set and thus the |
| 910 | ** number of columns in the result set will be P1. There must be at least |
| 911 | ** one OP_ColumnName with a P2==1 before invoking OP_Callback and the |
| 912 | ** number of columns specified in OP_Callback must one more than the P1 |
| 913 | ** value of the OP_ColumnName that has P2==1. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 914 | */ |
| 915 | case OP_ColumnName: { |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 916 | assert( pOp->p1>=0 && pOp->p1<p->nOp ); |
drh | b136320 | 2002-06-26 02:45:03 +0000 | [diff] [blame] | 917 | p->azColName[pOp->p1] = pOp->p3; |
drh | 6a53534 | 2001-10-19 16:44:56 +0000 | [diff] [blame] | 918 | p->nCallback = 0; |
drh | d650275 | 2004-02-16 03:44:01 +0000 | [diff] [blame] | 919 | if( pOp->p2 ) p->nResColumn = pOp->p1+1; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 920 | break; |
| 921 | } |
drh | c61053b | 2000-06-04 12:58:36 +0000 | [diff] [blame] | 922 | |
drh | df199a2 | 2002-06-14 22:38:41 +0000 | [diff] [blame] | 923 | /* Opcode: Callback P1 * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 924 | ** |
| 925 | ** Pop P1 values off the stack and form them into an array. Then |
| 926 | ** invoke the callback function using the newly formed array as the |
| 927 | ** 3rd parameter. |
| 928 | */ |
| 929 | case OP_Callback: { |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 930 | int i; |
| 931 | char **azArgv = p->zArgv; |
| 932 | Mem *pCol; |
| 933 | |
| 934 | pCol = &pTos[1-pOp->p1]; |
| 935 | assert( pCol>=p->aStack ); |
| 936 | for(i=0; i<pOp->p1; i++, pCol++){ |
| 937 | if( pCol->flags & MEM_Null ){ |
| 938 | azArgv[i] = 0; |
drh | a84f600 | 2001-11-13 19:35:14 +0000 | [diff] [blame] | 939 | }else{ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 940 | Stringify(pCol); |
| 941 | azArgv[i] = pCol->z; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 942 | } |
| 943 | } |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 944 | azArgv[i] = 0; |
drh | a1f9b5e | 2004-02-14 16:31:02 +0000 | [diff] [blame] | 945 | p->nCallback++; |
drh | 826fb5a | 2004-02-14 23:59:57 +0000 | [diff] [blame] | 946 | p->azResColumn = azArgv; |
drh | d650275 | 2004-02-16 03:44:01 +0000 | [diff] [blame] | 947 | assert( p->nResColumn==pOp->p1 ); |
drh | 826fb5a | 2004-02-14 23:59:57 +0000 | [diff] [blame] | 948 | p->popStack = pOp->p1; |
| 949 | p->pc = pc + 1; |
| 950 | p->pTos = pTos; |
| 951 | return SQLITE_ROW; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 952 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 953 | |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 954 | /* Opcode: Concat P1 P2 P3 |
| 955 | ** |
| 956 | ** Look at the first P1 elements of the stack. Append them all |
| 957 | ** together with the lowest element first. Use P3 as a separator. |
| 958 | ** Put the result on the top of the stack. The original P1 elements |
drh | a9f9d1c | 2002-06-29 02:20:08 +0000 | [diff] [blame] | 959 | ** are popped from the stack if P2==0 and retained if P2==1. If |
| 960 | ** any element of the stack is NULL, then the result is NULL. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 961 | ** |
| 962 | ** If P3 is NULL, then use no separator. When P1==1, this routine |
| 963 | ** makes a copy of the top stack element into memory obtained |
| 964 | ** from sqliteMalloc(). |
| 965 | */ |
| 966 | case OP_Concat: { |
| 967 | char *zNew; |
| 968 | int nByte; |
| 969 | int nField; |
| 970 | int i, j; |
| 971 | char *zSep; |
| 972 | int nSep; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 973 | Mem *pTerm; |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 974 | |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 975 | nField = pOp->p1; |
| 976 | zSep = pOp->p3; |
| 977 | if( zSep==0 ) zSep = ""; |
| 978 | nSep = strlen(zSep); |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 979 | assert( &pTos[1-nField] >= p->aStack ); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 980 | nByte = 1 - nSep; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 981 | pTerm = &pTos[1-nField]; |
| 982 | for(i=0; i<nField; i++, pTerm++){ |
| 983 | if( pTerm->flags & MEM_Null ){ |
drh | a9f9d1c | 2002-06-29 02:20:08 +0000 | [diff] [blame] | 984 | nByte = -1; |
| 985 | break; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 986 | }else{ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 987 | Stringify(pTerm); |
| 988 | nByte += pTerm->n - 1 + nSep; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 989 | } |
| 990 | } |
drh | a9f9d1c | 2002-06-29 02:20:08 +0000 | [diff] [blame] | 991 | if( nByte<0 ){ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 992 | if( pOp->p2==0 ){ |
| 993 | popStack(&pTos, nField); |
| 994 | } |
| 995 | pTos++; |
| 996 | pTos->flags = MEM_Null; |
drh | a9f9d1c | 2002-06-29 02:20:08 +0000 | [diff] [blame] | 997 | break; |
| 998 | } |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 999 | zNew = sqliteMallocRaw( nByte ); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1000 | if( zNew==0 ) goto no_mem; |
| 1001 | j = 0; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1002 | pTerm = &pTos[1-nField]; |
| 1003 | for(i=j=0; i<nField; i++, pTerm++){ |
| 1004 | assert( pTerm->flags & MEM_Str ); |
| 1005 | memcpy(&zNew[j], pTerm->z, pTerm->n-1); |
| 1006 | j += pTerm->n-1; |
| 1007 | if( nSep>0 && i<nField-1 ){ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1008 | memcpy(&zNew[j], zSep, nSep); |
| 1009 | j += nSep; |
| 1010 | } |
| 1011 | } |
| 1012 | zNew[j] = 0; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1013 | if( pOp->p2==0 ){ |
| 1014 | popStack(&pTos, nField); |
| 1015 | } |
| 1016 | pTos++; |
| 1017 | pTos->n = nByte; |
| 1018 | pTos->flags = MEM_Str|MEM_Dyn; |
| 1019 | pTos->z = zNew; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1020 | break; |
| 1021 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1022 | |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1023 | /* Opcode: Add * * * |
| 1024 | ** |
| 1025 | ** Pop the top two elements from the stack, add them together, |
| 1026 | ** and push the result back onto the stack. If either element |
| 1027 | ** is a string then it is converted to a double using the atof() |
| 1028 | ** function before the addition. |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1029 | ** If either operand is NULL, the result is NULL. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1030 | */ |
| 1031 | /* Opcode: Multiply * * * |
| 1032 | ** |
| 1033 | ** Pop the top two elements from the stack, multiply them together, |
| 1034 | ** and push the result back onto the stack. If either element |
| 1035 | ** is a string then it is converted to a double using the atof() |
| 1036 | ** function before the multiplication. |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1037 | ** If either operand is NULL, the result is NULL. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1038 | */ |
| 1039 | /* Opcode: Subtract * * * |
| 1040 | ** |
| 1041 | ** Pop the top two elements from the stack, subtract the |
| 1042 | ** first (what was on top of the stack) from the second (the |
| 1043 | ** next on stack) |
| 1044 | ** and push the result back onto the stack. If either element |
| 1045 | ** is a string then it is converted to a double using the atof() |
| 1046 | ** function before the subtraction. |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1047 | ** If either operand is NULL, the result is NULL. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1048 | */ |
| 1049 | /* Opcode: Divide * * * |
| 1050 | ** |
| 1051 | ** Pop the top two elements from the stack, divide the |
| 1052 | ** first (what was on top of the stack) from the second (the |
| 1053 | ** next on stack) |
| 1054 | ** and push the result back onto the stack. If either element |
| 1055 | ** is a string then it is converted to a double using the atof() |
| 1056 | ** function before the division. Division by zero returns NULL. |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1057 | ** If either operand is NULL, the result is NULL. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1058 | */ |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1059 | /* Opcode: Remainder * * * |
| 1060 | ** |
| 1061 | ** Pop the top two elements from the stack, divide the |
| 1062 | ** first (what was on top of the stack) from the second (the |
| 1063 | ** next on stack) |
| 1064 | ** and push the remainder after division onto the stack. If either element |
| 1065 | ** is a string then it is converted to a double using the atof() |
| 1066 | ** function before the division. Division by zero returns NULL. |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1067 | ** If either operand is NULL, the result is NULL. |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1068 | */ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1069 | case OP_Add: |
| 1070 | case OP_Subtract: |
| 1071 | case OP_Multiply: |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1072 | case OP_Divide: |
| 1073 | case OP_Remainder: { |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1074 | Mem *pNos = &pTos[-1]; |
| 1075 | assert( pNos>=p->aStack ); |
| 1076 | if( ((pTos->flags | pNos->flags) & MEM_Null)!=0 ){ |
| 1077 | Release(pTos); |
| 1078 | pTos--; |
| 1079 | Release(pTos); |
| 1080 | pTos->flags = MEM_Null; |
| 1081 | }else if( (pTos->flags & pNos->flags & MEM_Int)==MEM_Int ){ |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1082 | i64 a, b; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1083 | a = pTos->i; |
| 1084 | b = pNos->i; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1085 | switch( pOp->opcode ){ |
| 1086 | case OP_Add: b += a; break; |
| 1087 | case OP_Subtract: b -= a; break; |
| 1088 | case OP_Multiply: b *= a; break; |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1089 | case OP_Divide: { |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1090 | if( a==0 ) goto divide_by_zero; |
| 1091 | b /= a; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1092 | break; |
| 1093 | } |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1094 | default: { |
| 1095 | if( a==0 ) goto divide_by_zero; |
| 1096 | b %= a; |
| 1097 | break; |
| 1098 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1099 | } |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1100 | Release(pTos); |
| 1101 | pTos--; |
| 1102 | Release(pTos); |
| 1103 | pTos->i = b; |
| 1104 | pTos->flags = MEM_Int; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1105 | }else{ |
| 1106 | double a, b; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1107 | Realify(pTos); |
| 1108 | Realify(pNos); |
| 1109 | a = pTos->r; |
| 1110 | b = pNos->r; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1111 | switch( pOp->opcode ){ |
| 1112 | case OP_Add: b += a; break; |
| 1113 | case OP_Subtract: b -= a; break; |
| 1114 | case OP_Multiply: b *= a; break; |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1115 | case OP_Divide: { |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1116 | if( a==0.0 ) goto divide_by_zero; |
| 1117 | b /= a; |
| 1118 | break; |
| 1119 | } |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1120 | default: { |
drh | 1ab4300 | 2002-01-14 09:28:19 +0000 | [diff] [blame] | 1121 | int ia = (int)a; |
| 1122 | int ib = (int)b; |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1123 | if( ia==0.0 ) goto divide_by_zero; |
| 1124 | b = ib % ia; |
| 1125 | break; |
| 1126 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1127 | } |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1128 | Release(pTos); |
| 1129 | pTos--; |
| 1130 | Release(pTos); |
| 1131 | pTos->r = b; |
| 1132 | pTos->flags = MEM_Real; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1133 | } |
| 1134 | break; |
| 1135 | |
| 1136 | divide_by_zero: |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1137 | Release(pTos); |
| 1138 | pTos--; |
| 1139 | Release(pTos); |
| 1140 | pTos->flags = MEM_Null; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1141 | break; |
| 1142 | } |
| 1143 | |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 1144 | /* Opcode: Function P1 * P3 |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 1145 | ** |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 1146 | ** Invoke a user function (P3 is a pointer to a Function structure that |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 1147 | ** defines the function) with P1 string arguments taken from the stack. |
| 1148 | ** Pop all arguments from the stack and push back the result. |
| 1149 | ** |
| 1150 | ** See also: AggFunc |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 1151 | */ |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 1152 | case OP_Function: { |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 1153 | int n, i; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1154 | Mem *pArg; |
| 1155 | char **azArgv; |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 1156 | sqlite_func ctx; |
| 1157 | |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 1158 | n = pOp->p1; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1159 | pArg = &pTos[1-n]; |
| 1160 | azArgv = p->zArgv; |
| 1161 | for(i=0; i<n; i++, pArg++){ |
| 1162 | if( pArg->flags & MEM_Null ){ |
| 1163 | azArgv[i] = 0; |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1164 | }else{ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1165 | Stringify(pArg); |
| 1166 | azArgv[i] = pArg->z; |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 1167 | } |
| 1168 | } |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 1169 | ctx.pFunc = (FuncDef*)pOp->p3; |
drh | 00706be | 2004-01-30 14:49:16 +0000 | [diff] [blame] | 1170 | ctx.s.flags = MEM_Null; |
| 1171 | ctx.s.z = 0; |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 1172 | ctx.isError = 0; |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 1173 | ctx.isStep = 0; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1174 | if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1175 | (*ctx.pFunc->xFunc)(&ctx, n, (const char**)azArgv); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1176 | if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1177 | popStack(&pTos, n); |
| 1178 | pTos++; |
| 1179 | *pTos = ctx.s; |
| 1180 | if( pTos->flags & MEM_Short ){ |
| 1181 | pTos->z = pTos->zShort; |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 1182 | } |
| 1183 | if( ctx.isError ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1184 | sqlite3SetString(&p->zErrMsg, |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1185 | (pTos->flags & MEM_Str)!=0 ? pTos->z : "user function error", (char*)0); |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 1186 | rc = SQLITE_ERROR; |
| 1187 | } |
| 1188 | break; |
| 1189 | } |
| 1190 | |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1191 | /* Opcode: BitAnd * * * |
| 1192 | ** |
| 1193 | ** Pop the top two elements from the stack. Convert both elements |
| 1194 | ** to integers. Push back onto the stack the bit-wise AND of the |
| 1195 | ** two elements. |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1196 | ** If either operand is NULL, the result is NULL. |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1197 | */ |
| 1198 | /* Opcode: BitOr * * * |
| 1199 | ** |
| 1200 | ** Pop the top two elements from the stack. Convert both elements |
| 1201 | ** to integers. Push back onto the stack the bit-wise OR of the |
| 1202 | ** two elements. |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1203 | ** If either operand is NULL, the result is NULL. |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1204 | */ |
| 1205 | /* Opcode: ShiftLeft * * * |
| 1206 | ** |
| 1207 | ** Pop the top two elements from the stack. Convert both elements |
| 1208 | ** to integers. Push back onto the stack the top element shifted |
| 1209 | ** left by N bits where N is the second element on the stack. |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1210 | ** If either operand is NULL, the result is NULL. |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1211 | */ |
| 1212 | /* Opcode: ShiftRight * * * |
| 1213 | ** |
| 1214 | ** Pop the top two elements from the stack. Convert both elements |
| 1215 | ** to integers. Push back onto the stack the top element shifted |
| 1216 | ** right by N bits where N is the second element on the stack. |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1217 | ** If either operand is NULL, the result is NULL. |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1218 | */ |
| 1219 | case OP_BitAnd: |
| 1220 | case OP_BitOr: |
| 1221 | case OP_ShiftLeft: |
| 1222 | case OP_ShiftRight: { |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1223 | Mem *pNos = &pTos[-1]; |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1224 | int a, b; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1225 | |
| 1226 | assert( pNos>=p->aStack ); |
| 1227 | if( (pTos->flags | pNos->flags) & MEM_Null ){ |
| 1228 | popStack(&pTos, 2); |
| 1229 | pTos++; |
| 1230 | pTos->flags = MEM_Null; |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1231 | break; |
| 1232 | } |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1233 | Integerify(pTos); |
| 1234 | Integerify(pNos); |
| 1235 | a = pTos->i; |
| 1236 | b = pNos->i; |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1237 | switch( pOp->opcode ){ |
| 1238 | case OP_BitAnd: a &= b; break; |
| 1239 | case OP_BitOr: a |= b; break; |
| 1240 | case OP_ShiftLeft: a <<= b; break; |
| 1241 | case OP_ShiftRight: a >>= b; break; |
| 1242 | default: /* CANT HAPPEN */ break; |
| 1243 | } |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1244 | assert( (pTos->flags & MEM_Dyn)==0 ); |
| 1245 | assert( (pNos->flags & MEM_Dyn)==0 ); |
| 1246 | pTos--; |
drh | 79f14b7 | 2004-03-03 01:51:24 +0000 | [diff] [blame] | 1247 | Release(pTos); |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1248 | pTos->i = a; |
drh | 79f14b7 | 2004-03-03 01:51:24 +0000 | [diff] [blame] | 1249 | pTos->flags = MEM_Int; |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1250 | break; |
| 1251 | } |
| 1252 | |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1253 | /* Opcode: AddImm P1 * * |
| 1254 | ** |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 1255 | ** Add the value P1 to whatever is on top of the stack. The result |
| 1256 | ** is always an integer. |
| 1257 | ** |
| 1258 | ** To force the top of the stack to be an integer, just add 0. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1259 | */ |
| 1260 | case OP_AddImm: { |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1261 | assert( pTos>=p->aStack ); |
| 1262 | Integerify(pTos); |
| 1263 | pTos->i += pOp->p1; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1264 | break; |
| 1265 | } |
| 1266 | |
drh | 751f412 | 2004-01-14 21:59:22 +0000 | [diff] [blame] | 1267 | /* Opcode: ForceInt P1 P2 * |
drh | 1dd59e0 | 2003-07-06 17:22:25 +0000 | [diff] [blame] | 1268 | ** |
drh | 751f412 | 2004-01-14 21:59:22 +0000 | [diff] [blame] | 1269 | ** Convert the top of the stack into an integer. If the current top of |
| 1270 | ** the stack is not numeric (meaning that is is a NULL or a string that |
| 1271 | ** does not look like an integer or floating point number) then pop the |
| 1272 | ** stack and jump to P2. If the top of the stack is numeric then |
| 1273 | ** convert it into the least integer that is greater than or equal to its |
| 1274 | ** current value if P1==0, or to the least integer that is strictly |
| 1275 | ** greater than its current value if P1==1. |
drh | 1dd59e0 | 2003-07-06 17:22:25 +0000 | [diff] [blame] | 1276 | */ |
drh | 751f412 | 2004-01-14 21:59:22 +0000 | [diff] [blame] | 1277 | case OP_ForceInt: { |
drh | 751f412 | 2004-01-14 21:59:22 +0000 | [diff] [blame] | 1278 | int v; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1279 | assert( pTos>=p->aStack ); |
| 1280 | if( (pTos->flags & (MEM_Int|MEM_Real))==0 |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 1281 | && ((pTos->flags & MEM_Str)==0 || sqlite3IsNumber(pTos->z, 0)==0) ){ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1282 | Release(pTos); |
| 1283 | pTos--; |
drh | 1dd59e0 | 2003-07-06 17:22:25 +0000 | [diff] [blame] | 1284 | pc = pOp->p2 - 1; |
drh | 751f412 | 2004-01-14 21:59:22 +0000 | [diff] [blame] | 1285 | break; |
drh | 1dd59e0 | 2003-07-06 17:22:25 +0000 | [diff] [blame] | 1286 | } |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1287 | if( pTos->flags & MEM_Int ){ |
| 1288 | v = pTos->i + (pOp->p1!=0); |
drh | 751f412 | 2004-01-14 21:59:22 +0000 | [diff] [blame] | 1289 | }else{ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1290 | Realify(pTos); |
| 1291 | v = (int)pTos->r; |
| 1292 | if( pTos->r>(double)v ) v++; |
| 1293 | if( pOp->p1 && pTos->r==(double)v ) v++; |
drh | 751f412 | 2004-01-14 21:59:22 +0000 | [diff] [blame] | 1294 | } |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1295 | Release(pTos); |
| 1296 | pTos->i = v; |
| 1297 | pTos->flags = MEM_Int; |
drh | 1dd59e0 | 2003-07-06 17:22:25 +0000 | [diff] [blame] | 1298 | break; |
| 1299 | } |
| 1300 | |
drh | f1351b6 | 2002-07-31 19:50:26 +0000 | [diff] [blame] | 1301 | /* Opcode: MustBeInt P1 P2 * |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 1302 | ** |
| 1303 | ** Force the top of the stack to be an integer. If the top of the |
drh | d99f706 | 2002-06-08 23:25:08 +0000 | [diff] [blame] | 1304 | ** stack is not an integer and cannot be converted into an integer |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 1305 | ** with out data loss, then jump immediately to P2, or if P2==0 |
| 1306 | ** raise an SQLITE_MISMATCH exception. |
drh | f1351b6 | 2002-07-31 19:50:26 +0000 | [diff] [blame] | 1307 | ** |
| 1308 | ** If the top of the stack is not an integer and P2 is not zero and |
| 1309 | ** P1 is 1, then the stack is popped. In all other cases, the depth |
| 1310 | ** of the stack is unchanged. |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 1311 | */ |
| 1312 | case OP_MustBeInt: { |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1313 | assert( pTos>=p->aStack ); |
| 1314 | if( pTos->flags & MEM_Int ){ |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 1315 | /* Do nothing */ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1316 | }else if( pTos->flags & MEM_Real ){ |
| 1317 | int i = (int)pTos->r; |
drh | 7d02cb7 | 2003-06-04 16:24:39 +0000 | [diff] [blame] | 1318 | double r = (double)i; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1319 | if( r!=pTos->r ){ |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 1320 | goto mismatch; |
| 1321 | } |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1322 | pTos->i = i; |
| 1323 | }else if( pTos->flags & MEM_Str ){ |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 1324 | i64 v; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1325 | if( !toInt(pTos->z, &v) ){ |
drh | 1dd59e0 | 2003-07-06 17:22:25 +0000 | [diff] [blame] | 1326 | double r; |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 1327 | if( !sqlite3IsNumber(pTos->z, 0) ){ |
drh | 1dd59e0 | 2003-07-06 17:22:25 +0000 | [diff] [blame] | 1328 | goto mismatch; |
| 1329 | } |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1330 | Realify(pTos); |
| 1331 | v = (int)pTos->r; |
drh | 1dd59e0 | 2003-07-06 17:22:25 +0000 | [diff] [blame] | 1332 | r = (double)v; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1333 | if( r!=pTos->r ){ |
drh | 1dd59e0 | 2003-07-06 17:22:25 +0000 | [diff] [blame] | 1334 | goto mismatch; |
| 1335 | } |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 1336 | } |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1337 | pTos->i = v; |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 1338 | }else{ |
| 1339 | goto mismatch; |
| 1340 | } |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1341 | Release(pTos); |
| 1342 | pTos->flags = MEM_Int; |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 1343 | break; |
| 1344 | |
| 1345 | mismatch: |
| 1346 | if( pOp->p2==0 ){ |
| 1347 | rc = SQLITE_MISMATCH; |
| 1348 | goto abort_due_to_error; |
| 1349 | }else{ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1350 | if( pOp->p1 ) popStack(&pTos, 1); |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 1351 | pc = pOp->p2 - 1; |
| 1352 | } |
| 1353 | break; |
| 1354 | } |
| 1355 | |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1356 | /* Opcode: Eq P1 P2 * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1357 | ** |
| 1358 | ** Pop the top two elements from the stack. If they are equal, then |
| 1359 | ** jump to instruction P2. Otherwise, continue to the next instruction. |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1360 | ** |
| 1361 | ** If either operand is NULL (and thus if the result is unknown) then |
| 1362 | ** take the jump if P1 is true. |
| 1363 | ** |
drh | c9b84a1 | 2002-06-20 11:36:48 +0000 | [diff] [blame] | 1364 | ** If both values are numeric, they are converted to doubles using atof() |
| 1365 | ** and compared for equality that way. Otherwise the strcmp() library |
| 1366 | ** routine is used for the comparison. For a pure text comparison |
| 1367 | ** use OP_StrEq. |
| 1368 | ** |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1369 | ** If P2 is zero, do not jump. Instead, push an integer 1 onto the |
| 1370 | ** stack if the jump would have been taken, or a 0 if not. Push a |
| 1371 | ** NULL if either operand was NULL. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1372 | */ |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1373 | /* Opcode: Ne P1 P2 * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1374 | ** |
| 1375 | ** Pop the top two elements from the stack. If they are not equal, then |
| 1376 | ** jump to instruction P2. Otherwise, continue to the next instruction. |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1377 | ** |
| 1378 | ** If either operand is NULL (and thus if the result is unknown) then |
| 1379 | ** take the jump if P1 is true. |
| 1380 | ** |
drh | c9b84a1 | 2002-06-20 11:36:48 +0000 | [diff] [blame] | 1381 | ** If both values are numeric, they are converted to doubles using atof() |
| 1382 | ** and compared in that format. Otherwise the strcmp() library |
| 1383 | ** routine is used for the comparison. For a pure text comparison |
| 1384 | ** use OP_StrNe. |
| 1385 | ** |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1386 | ** If P2 is zero, do not jump. Instead, push an integer 1 onto the |
| 1387 | ** stack if the jump would have been taken, or a 0 if not. Push a |
| 1388 | ** NULL if either operand was NULL. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1389 | */ |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1390 | /* Opcode: Lt P1 P2 * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1391 | ** |
| 1392 | ** Pop the top two elements from the stack. If second element (the |
| 1393 | ** next on stack) is less than the first (the top of stack), then |
| 1394 | ** jump to instruction P2. Otherwise, continue to the next instruction. |
| 1395 | ** In other words, jump if NOS<TOS. |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1396 | ** |
| 1397 | ** If either operand is NULL (and thus if the result is unknown) then |
| 1398 | ** take the jump if P1 is true. |
| 1399 | ** |
drh | c9b84a1 | 2002-06-20 11:36:48 +0000 | [diff] [blame] | 1400 | ** If both values are numeric, they are converted to doubles using atof() |
| 1401 | ** and compared in that format. Numeric values are always less than |
| 1402 | ** non-numeric values. If both operands are non-numeric, the strcmp() library |
| 1403 | ** routine is used for the comparison. For a pure text comparison |
| 1404 | ** use OP_StrLt. |
| 1405 | ** |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1406 | ** If P2 is zero, do not jump. Instead, push an integer 1 onto the |
| 1407 | ** stack if the jump would have been taken, or a 0 if not. Push a |
| 1408 | ** NULL if either operand was NULL. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1409 | */ |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1410 | /* Opcode: Le P1 P2 * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1411 | ** |
| 1412 | ** Pop the top two elements from the stack. If second element (the |
| 1413 | ** next on stack) is less than or equal to the first (the top of stack), |
| 1414 | ** then jump to instruction P2. In other words, jump if NOS<=TOS. |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1415 | ** |
| 1416 | ** If either operand is NULL (and thus if the result is unknown) then |
| 1417 | ** take the jump if P1 is true. |
| 1418 | ** |
drh | c9b84a1 | 2002-06-20 11:36:48 +0000 | [diff] [blame] | 1419 | ** If both values are numeric, they are converted to doubles using atof() |
| 1420 | ** and compared in that format. Numeric values are always less than |
| 1421 | ** non-numeric values. If both operands are non-numeric, the strcmp() library |
| 1422 | ** routine is used for the comparison. For a pure text comparison |
| 1423 | ** use OP_StrLe. |
| 1424 | ** |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1425 | ** If P2 is zero, do not jump. Instead, push an integer 1 onto the |
| 1426 | ** stack if the jump would have been taken, or a 0 if not. Push a |
| 1427 | ** NULL if either operand was NULL. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1428 | */ |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1429 | /* Opcode: Gt P1 P2 * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1430 | ** |
| 1431 | ** Pop the top two elements from the stack. If second element (the |
| 1432 | ** next on stack) is greater than the first (the top of stack), |
| 1433 | ** then jump to instruction P2. In other words, jump if NOS>TOS. |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1434 | ** |
| 1435 | ** If either operand is NULL (and thus if the result is unknown) then |
| 1436 | ** take the jump if P1 is true. |
| 1437 | ** |
drh | c9b84a1 | 2002-06-20 11:36:48 +0000 | [diff] [blame] | 1438 | ** If both values are numeric, they are converted to doubles using atof() |
| 1439 | ** and compared in that format. Numeric values are always less than |
| 1440 | ** non-numeric values. If both operands are non-numeric, the strcmp() library |
| 1441 | ** routine is used for the comparison. For a pure text comparison |
| 1442 | ** use OP_StrGt. |
| 1443 | ** |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1444 | ** If P2 is zero, do not jump. Instead, push an integer 1 onto the |
| 1445 | ** stack if the jump would have been taken, or a 0 if not. Push a |
| 1446 | ** NULL if either operand was NULL. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1447 | */ |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1448 | /* Opcode: Ge P1 P2 * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1449 | ** |
| 1450 | ** Pop the top two elements from the stack. If second element (the next |
| 1451 | ** on stack) is greater than or equal to the first (the top of stack), |
| 1452 | ** then jump to instruction P2. In other words, jump if NOS>=TOS. |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1453 | ** |
| 1454 | ** If either operand is NULL (and thus if the result is unknown) then |
| 1455 | ** take the jump if P1 is true. |
| 1456 | ** |
drh | c9b84a1 | 2002-06-20 11:36:48 +0000 | [diff] [blame] | 1457 | ** If both values are numeric, they are converted to doubles using atof() |
| 1458 | ** and compared in that format. Numeric values are always less than |
| 1459 | ** non-numeric values. If both operands are non-numeric, the strcmp() library |
| 1460 | ** routine is used for the comparison. For a pure text comparison |
| 1461 | ** use OP_StrGe. |
| 1462 | ** |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1463 | ** If P2 is zero, do not jump. Instead, push an integer 1 onto the |
| 1464 | ** stack if the jump would have been taken, or a 0 if not. Push a |
| 1465 | ** NULL if either operand was NULL. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1466 | */ |
| 1467 | case OP_Eq: |
| 1468 | case OP_Ne: |
| 1469 | case OP_Lt: |
| 1470 | case OP_Le: |
| 1471 | case OP_Gt: |
| 1472 | case OP_Ge: { |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1473 | Mem *pNos = &pTos[-1]; |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 1474 | i64 c, v; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1475 | int ft, fn; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1476 | assert( pNos>=p->aStack ); |
| 1477 | ft = pTos->flags; |
| 1478 | fn = pNos->flags; |
drh | 00706be | 2004-01-30 14:49:16 +0000 | [diff] [blame] | 1479 | if( (ft | fn) & MEM_Null ){ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1480 | popStack(&pTos, 2); |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1481 | if( pOp->p2 ){ |
| 1482 | if( pOp->p1 ) pc = pOp->p2-1; |
| 1483 | }else{ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1484 | pTos++; |
| 1485 | pTos->flags = MEM_Null; |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1486 | } |
| 1487 | break; |
drh | 00706be | 2004-01-30 14:49:16 +0000 | [diff] [blame] | 1488 | }else if( (ft & fn & MEM_Int)==MEM_Int ){ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1489 | c = pNos->i - pTos->i; |
| 1490 | }else if( (ft & MEM_Int)!=0 && (fn & MEM_Str)!=0 && toInt(pNos->z,&v) ){ |
| 1491 | c = v - pTos->i; |
| 1492 | }else if( (fn & MEM_Int)!=0 && (ft & MEM_Str)!=0 && toInt(pTos->z,&v) ){ |
| 1493 | c = pNos->i - v; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1494 | }else{ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1495 | Stringify(pTos); |
| 1496 | Stringify(pNos); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1497 | c = sqlite3Compare(pNos->z, pTos->z); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1498 | } |
| 1499 | switch( pOp->opcode ){ |
| 1500 | case OP_Eq: c = c==0; break; |
| 1501 | case OP_Ne: c = c!=0; break; |
| 1502 | case OP_Lt: c = c<0; break; |
| 1503 | case OP_Le: c = c<=0; break; |
| 1504 | case OP_Gt: c = c>0; break; |
| 1505 | default: c = c>=0; break; |
| 1506 | } |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1507 | popStack(&pTos, 2); |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1508 | if( pOp->p2 ){ |
| 1509 | if( c ) pc = pOp->p2-1; |
| 1510 | }else{ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1511 | pTos++; |
| 1512 | pTos->i = c; |
| 1513 | pTos->flags = MEM_Int; |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1514 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1515 | break; |
| 1516 | } |
drh | 8f619cc | 2002-09-08 00:04:50 +0000 | [diff] [blame] | 1517 | /* INSERT NO CODE HERE! |
| 1518 | ** |
| 1519 | ** The opcode numbers are extracted from this source file by doing |
| 1520 | ** |
| 1521 | ** grep '^case OP_' vdbe.c | ... >opcodes.h |
| 1522 | ** |
| 1523 | ** The opcodes are numbered in the order that they appear in this file. |
| 1524 | ** But in order for the expression generating code to work right, the |
| 1525 | ** string comparison operators that follow must be numbered exactly 6 |
| 1526 | ** greater than the numeric comparison opcodes above. So no other |
| 1527 | ** cases can appear between the two. |
| 1528 | */ |
drh | c9b84a1 | 2002-06-20 11:36:48 +0000 | [diff] [blame] | 1529 | /* Opcode: StrEq P1 P2 * |
| 1530 | ** |
| 1531 | ** Pop the top two elements from the stack. If they are equal, then |
| 1532 | ** jump to instruction P2. Otherwise, continue to the next instruction. |
| 1533 | ** |
| 1534 | ** If either operand is NULL (and thus if the result is unknown) then |
| 1535 | ** take the jump if P1 is true. |
| 1536 | ** |
| 1537 | ** The strcmp() library routine is used for the comparison. For a |
| 1538 | ** numeric comparison, use OP_Eq. |
| 1539 | ** |
| 1540 | ** If P2 is zero, do not jump. Instead, push an integer 1 onto the |
| 1541 | ** stack if the jump would have been taken, or a 0 if not. Push a |
| 1542 | ** NULL if either operand was NULL. |
| 1543 | */ |
| 1544 | /* Opcode: StrNe P1 P2 * |
| 1545 | ** |
| 1546 | ** Pop the top two elements from the stack. If they are not equal, then |
| 1547 | ** jump to instruction P2. Otherwise, continue to the next instruction. |
| 1548 | ** |
| 1549 | ** If either operand is NULL (and thus if the result is unknown) then |
| 1550 | ** take the jump if P1 is true. |
| 1551 | ** |
| 1552 | ** The strcmp() library routine is used for the comparison. For a |
| 1553 | ** numeric comparison, use OP_Ne. |
| 1554 | ** |
| 1555 | ** If P2 is zero, do not jump. Instead, push an integer 1 onto the |
| 1556 | ** stack if the jump would have been taken, or a 0 if not. Push a |
| 1557 | ** NULL if either operand was NULL. |
| 1558 | */ |
| 1559 | /* Opcode: StrLt P1 P2 * |
| 1560 | ** |
| 1561 | ** Pop the top two elements from the stack. If second element (the |
| 1562 | ** next on stack) is less than the first (the top of stack), then |
| 1563 | ** jump to instruction P2. Otherwise, continue to the next instruction. |
| 1564 | ** In other words, jump if NOS<TOS. |
| 1565 | ** |
| 1566 | ** If either operand is NULL (and thus if the result is unknown) then |
| 1567 | ** take the jump if P1 is true. |
| 1568 | ** |
| 1569 | ** The strcmp() library routine is used for the comparison. For a |
| 1570 | ** numeric comparison, use OP_Lt. |
| 1571 | ** |
| 1572 | ** If P2 is zero, do not jump. Instead, push an integer 1 onto the |
| 1573 | ** stack if the jump would have been taken, or a 0 if not. Push a |
| 1574 | ** NULL if either operand was NULL. |
| 1575 | */ |
| 1576 | /* Opcode: StrLe P1 P2 * |
| 1577 | ** |
| 1578 | ** Pop the top two elements from the stack. If second element (the |
| 1579 | ** next on stack) is less than or equal to the first (the top of stack), |
| 1580 | ** then jump to instruction P2. In other words, jump if NOS<=TOS. |
| 1581 | ** |
| 1582 | ** If either operand is NULL (and thus if the result is unknown) then |
| 1583 | ** take the jump if P1 is true. |
| 1584 | ** |
| 1585 | ** The strcmp() library routine is used for the comparison. For a |
| 1586 | ** numeric comparison, use OP_Le. |
| 1587 | ** |
| 1588 | ** If P2 is zero, do not jump. Instead, push an integer 1 onto the |
| 1589 | ** stack if the jump would have been taken, or a 0 if not. Push a |
| 1590 | ** NULL if either operand was NULL. |
| 1591 | */ |
| 1592 | /* Opcode: StrGt P1 P2 * |
| 1593 | ** |
| 1594 | ** Pop the top two elements from the stack. If second element (the |
| 1595 | ** next on stack) is greater than the first (the top of stack), |
| 1596 | ** then jump to instruction P2. In other words, jump if NOS>TOS. |
| 1597 | ** |
| 1598 | ** If either operand is NULL (and thus if the result is unknown) then |
| 1599 | ** take the jump if P1 is true. |
| 1600 | ** |
| 1601 | ** The strcmp() library routine is used for the comparison. For a |
| 1602 | ** numeric comparison, use OP_Gt. |
| 1603 | ** |
| 1604 | ** If P2 is zero, do not jump. Instead, push an integer 1 onto the |
| 1605 | ** stack if the jump would have been taken, or a 0 if not. Push a |
| 1606 | ** NULL if either operand was NULL. |
| 1607 | */ |
| 1608 | /* Opcode: StrGe P1 P2 * |
| 1609 | ** |
| 1610 | ** Pop the top two elements from the stack. If second element (the next |
| 1611 | ** on stack) is greater than or equal to the first (the top of stack), |
| 1612 | ** then jump to instruction P2. In other words, jump if NOS>=TOS. |
| 1613 | ** |
| 1614 | ** If either operand is NULL (and thus if the result is unknown) then |
| 1615 | ** take the jump if P1 is true. |
| 1616 | ** |
| 1617 | ** The strcmp() library routine is used for the comparison. For a |
| 1618 | ** numeric comparison, use OP_Ge. |
| 1619 | ** |
| 1620 | ** If P2 is zero, do not jump. Instead, push an integer 1 onto the |
| 1621 | ** stack if the jump would have been taken, or a 0 if not. Push a |
| 1622 | ** NULL if either operand was NULL. |
| 1623 | */ |
| 1624 | case OP_StrEq: |
| 1625 | case OP_StrNe: |
| 1626 | case OP_StrLt: |
| 1627 | case OP_StrLe: |
| 1628 | case OP_StrGt: |
| 1629 | case OP_StrGe: { |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1630 | Mem *pNos = &pTos[-1]; |
drh | c9b84a1 | 2002-06-20 11:36:48 +0000 | [diff] [blame] | 1631 | int c; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1632 | assert( pNos>=p->aStack ); |
| 1633 | if( (pNos->flags | pTos->flags) & MEM_Null ){ |
| 1634 | popStack(&pTos, 2); |
drh | c9b84a1 | 2002-06-20 11:36:48 +0000 | [diff] [blame] | 1635 | if( pOp->p2 ){ |
| 1636 | if( pOp->p1 ) pc = pOp->p2-1; |
| 1637 | }else{ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1638 | pTos++; |
| 1639 | pTos->flags = MEM_Null; |
drh | c9b84a1 | 2002-06-20 11:36:48 +0000 | [diff] [blame] | 1640 | } |
| 1641 | break; |
| 1642 | }else{ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1643 | Stringify(pTos); |
| 1644 | Stringify(pNos); |
| 1645 | c = strcmp(pNos->z, pTos->z); |
drh | c9b84a1 | 2002-06-20 11:36:48 +0000 | [diff] [blame] | 1646 | } |
drh | 8f619cc | 2002-09-08 00:04:50 +0000 | [diff] [blame] | 1647 | /* The asserts on each case of the following switch are there to verify |
| 1648 | ** that string comparison opcodes are always exactly 6 greater than the |
| 1649 | ** corresponding numeric comparison opcodes. The code generator depends |
| 1650 | ** on this fact. |
| 1651 | */ |
drh | c9b84a1 | 2002-06-20 11:36:48 +0000 | [diff] [blame] | 1652 | switch( pOp->opcode ){ |
drh | 8f619cc | 2002-09-08 00:04:50 +0000 | [diff] [blame] | 1653 | case OP_StrEq: c = c==0; assert( pOp->opcode-6==OP_Eq ); break; |
| 1654 | case OP_StrNe: c = c!=0; assert( pOp->opcode-6==OP_Ne ); break; |
| 1655 | case OP_StrLt: c = c<0; assert( pOp->opcode-6==OP_Lt ); break; |
| 1656 | case OP_StrLe: c = c<=0; assert( pOp->opcode-6==OP_Le ); break; |
| 1657 | case OP_StrGt: c = c>0; assert( pOp->opcode-6==OP_Gt ); break; |
| 1658 | default: c = c>=0; assert( pOp->opcode-6==OP_Ge ); break; |
drh | c9b84a1 | 2002-06-20 11:36:48 +0000 | [diff] [blame] | 1659 | } |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1660 | popStack(&pTos, 2); |
drh | c9b84a1 | 2002-06-20 11:36:48 +0000 | [diff] [blame] | 1661 | if( pOp->p2 ){ |
| 1662 | if( c ) pc = pOp->p2-1; |
| 1663 | }else{ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1664 | pTos++; |
| 1665 | pTos->flags = MEM_Int; |
| 1666 | pTos->i = c; |
drh | c9b84a1 | 2002-06-20 11:36:48 +0000 | [diff] [blame] | 1667 | } |
| 1668 | break; |
| 1669 | } |
| 1670 | |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1671 | /* Opcode: And * * * |
| 1672 | ** |
| 1673 | ** Pop two values off the stack. Take the logical AND of the |
| 1674 | ** two values and push the resulting boolean value back onto the |
| 1675 | ** stack. |
| 1676 | */ |
| 1677 | /* Opcode: Or * * * |
| 1678 | ** |
| 1679 | ** Pop two values off the stack. Take the logical OR of the |
| 1680 | ** two values and push the resulting boolean value back onto the |
| 1681 | ** stack. |
| 1682 | */ |
| 1683 | case OP_And: |
| 1684 | case OP_Or: { |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1685 | Mem *pNos = &pTos[-1]; |
drh | bb11351 | 2002-05-27 01:04:51 +0000 | [diff] [blame] | 1686 | int v1, v2; /* 0==TRUE, 1==FALSE, 2==UNKNOWN or NULL */ |
| 1687 | |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1688 | assert( pNos>=p->aStack ); |
| 1689 | if( pTos->flags & MEM_Null ){ |
drh | bb11351 | 2002-05-27 01:04:51 +0000 | [diff] [blame] | 1690 | v1 = 2; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1691 | }else{ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1692 | Integerify(pTos); |
| 1693 | v1 = pTos->i==0; |
drh | bb11351 | 2002-05-27 01:04:51 +0000 | [diff] [blame] | 1694 | } |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1695 | if( pNos->flags & MEM_Null ){ |
drh | bb11351 | 2002-05-27 01:04:51 +0000 | [diff] [blame] | 1696 | v2 = 2; |
| 1697 | }else{ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1698 | Integerify(pNos); |
| 1699 | v2 = pNos->i==0; |
drh | bb11351 | 2002-05-27 01:04:51 +0000 | [diff] [blame] | 1700 | } |
| 1701 | if( pOp->opcode==OP_And ){ |
| 1702 | static const unsigned char and_logic[] = { 0, 1, 2, 1, 1, 1, 2, 1, 2 }; |
| 1703 | v1 = and_logic[v1*3+v2]; |
| 1704 | }else{ |
| 1705 | static const unsigned char or_logic[] = { 0, 0, 0, 0, 1, 2, 0, 2, 2 }; |
| 1706 | v1 = or_logic[v1*3+v2]; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1707 | } |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1708 | popStack(&pTos, 2); |
| 1709 | pTos++; |
drh | bb11351 | 2002-05-27 01:04:51 +0000 | [diff] [blame] | 1710 | if( v1==2 ){ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1711 | pTos->flags = MEM_Null; |
drh | bb11351 | 2002-05-27 01:04:51 +0000 | [diff] [blame] | 1712 | }else{ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1713 | pTos->i = v1==0; |
| 1714 | pTos->flags = MEM_Int; |
drh | bb11351 | 2002-05-27 01:04:51 +0000 | [diff] [blame] | 1715 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1716 | break; |
| 1717 | } |
| 1718 | |
| 1719 | /* Opcode: Negative * * * |
| 1720 | ** |
| 1721 | ** Treat the top of the stack as a numeric quantity. Replace it |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1722 | ** with its additive inverse. If the top of the stack is NULL |
| 1723 | ** its value is unchanged. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1724 | */ |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1725 | /* Opcode: AbsValue * * * |
| 1726 | ** |
| 1727 | ** Treat the top of the stack as a numeric quantity. Replace it |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1728 | ** with its absolute value. If the top of the stack is NULL |
| 1729 | ** its value is unchanged. |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1730 | */ |
| 1731 | case OP_Negative: |
| 1732 | case OP_AbsValue: { |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1733 | assert( pTos>=p->aStack ); |
| 1734 | if( pTos->flags & MEM_Real ){ |
| 1735 | Release(pTos); |
| 1736 | if( pOp->opcode==OP_Negative || pTos->r<0.0 ){ |
| 1737 | pTos->r = -pTos->r; |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1738 | } |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1739 | pTos->flags = MEM_Real; |
| 1740 | }else if( pTos->flags & MEM_Int ){ |
| 1741 | Release(pTos); |
| 1742 | if( pOp->opcode==OP_Negative || pTos->i<0 ){ |
| 1743 | pTos->i = -pTos->i; |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1744 | } |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1745 | pTos->flags = MEM_Int; |
| 1746 | }else if( pTos->flags & MEM_Null ){ |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1747 | /* Do nothing */ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1748 | }else{ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1749 | Realify(pTos); |
| 1750 | Release(pTos); |
| 1751 | if( pOp->opcode==OP_Negative || pTos->r<0.0 ){ |
| 1752 | pTos->r = -pTos->r; |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1753 | } |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1754 | pTos->flags = MEM_Real; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1755 | } |
| 1756 | break; |
| 1757 | } |
| 1758 | |
| 1759 | /* Opcode: Not * * * |
| 1760 | ** |
| 1761 | ** Interpret the top of the stack as a boolean value. Replace it |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1762 | ** with its complement. If the top of the stack is NULL its value |
| 1763 | ** is unchanged. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1764 | */ |
| 1765 | case OP_Not: { |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1766 | assert( pTos>=p->aStack ); |
| 1767 | if( pTos->flags & MEM_Null ) break; /* Do nothing to NULLs */ |
| 1768 | Integerify(pTos); |
drh | 79f14b7 | 2004-03-03 01:51:24 +0000 | [diff] [blame] | 1769 | Release(pTos); |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1770 | pTos->i = !pTos->i; |
drh | 79f14b7 | 2004-03-03 01:51:24 +0000 | [diff] [blame] | 1771 | pTos->flags = MEM_Int; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1772 | break; |
| 1773 | } |
| 1774 | |
drh | 18b81e5 | 2001-11-01 13:52:52 +0000 | [diff] [blame] | 1775 | /* Opcode: BitNot * * * |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1776 | ** |
| 1777 | ** Interpret the top of the stack as an value. Replace it |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1778 | ** with its ones-complement. If the top of the stack is NULL its |
| 1779 | ** value is unchanged. |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1780 | */ |
| 1781 | case OP_BitNot: { |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1782 | assert( pTos>=p->aStack ); |
| 1783 | if( pTos->flags & MEM_Null ) break; /* Do nothing to NULLs */ |
| 1784 | Integerify(pTos); |
drh | 79f14b7 | 2004-03-03 01:51:24 +0000 | [diff] [blame] | 1785 | Release(pTos); |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1786 | pTos->i = ~pTos->i; |
drh | 79f14b7 | 2004-03-03 01:51:24 +0000 | [diff] [blame] | 1787 | pTos->flags = MEM_Int; |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1788 | break; |
| 1789 | } |
| 1790 | |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1791 | /* Opcode: Noop * * * |
| 1792 | ** |
| 1793 | ** Do nothing. This instruction is often useful as a jump |
| 1794 | ** destination. |
| 1795 | */ |
| 1796 | case OP_Noop: { |
| 1797 | break; |
| 1798 | } |
| 1799 | |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1800 | /* Opcode: If P1 P2 * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1801 | ** |
| 1802 | ** Pop a single boolean from the stack. If the boolean popped is |
| 1803 | ** true, then jump to p2. Otherwise continue to the next instruction. |
| 1804 | ** An integer is false if zero and true otherwise. A string is |
| 1805 | ** false if it has zero length and true otherwise. |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1806 | ** |
| 1807 | ** If the value popped of the stack is NULL, then take the jump if P1 |
| 1808 | ** is true and fall through if P1 is false. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1809 | */ |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1810 | /* Opcode: IfNot P1 P2 * |
| 1811 | ** |
| 1812 | ** Pop a single boolean from the stack. If the boolean popped is |
| 1813 | ** false, then jump to p2. Otherwise continue to the next instruction. |
| 1814 | ** An integer is false if zero and true otherwise. A string is |
| 1815 | ** false if it has zero length and true otherwise. |
| 1816 | ** |
| 1817 | ** If the value popped of the stack is NULL, then take the jump if P1 |
| 1818 | ** is true and fall through if P1 is false. |
| 1819 | */ |
| 1820 | case OP_If: |
| 1821 | case OP_IfNot: { |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1822 | int c; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1823 | assert( pTos>=p->aStack ); |
| 1824 | if( pTos->flags & MEM_Null ){ |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1825 | c = pOp->p1; |
| 1826 | }else{ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1827 | Integerify(pTos); |
| 1828 | c = pTos->i; |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1829 | if( pOp->opcode==OP_IfNot ) c = !c; |
| 1830 | } |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1831 | assert( (pTos->flags & MEM_Dyn)==0 ); |
| 1832 | pTos--; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1833 | if( c ) pc = pOp->p2-1; |
| 1834 | break; |
| 1835 | } |
| 1836 | |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1837 | /* Opcode: IsNull P1 P2 * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1838 | ** |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1839 | ** If any of the top abs(P1) values on the stack are NULL, then jump |
drh | 143f3c4 | 2004-01-07 20:37:52 +0000 | [diff] [blame] | 1840 | ** to P2. Pop the stack P1 times if P1>0. If P1<0 leave the stack |
| 1841 | ** unchanged. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1842 | */ |
| 1843 | case OP_IsNull: { |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1844 | int i, cnt; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1845 | Mem *pTerm; |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1846 | cnt = pOp->p1; |
| 1847 | if( cnt<0 ) cnt = -cnt; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1848 | pTerm = &pTos[1-cnt]; |
| 1849 | assert( pTerm>=p->aStack ); |
| 1850 | for(i=0; i<cnt; i++, pTerm++){ |
| 1851 | if( pTerm->flags & MEM_Null ){ |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1852 | pc = pOp->p2-1; |
| 1853 | break; |
| 1854 | } |
| 1855 | } |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1856 | if( pOp->p1>0 ) popStack(&pTos, cnt); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1857 | break; |
| 1858 | } |
| 1859 | |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1860 | /* Opcode: NotNull P1 P2 * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1861 | ** |
drh | 143f3c4 | 2004-01-07 20:37:52 +0000 | [diff] [blame] | 1862 | ** Jump to P2 if the top P1 values on the stack are all not NULL. Pop the |
| 1863 | ** stack if P1 times if P1 is greater than zero. If P1 is less than |
| 1864 | ** zero then leave the stack unchanged. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1865 | */ |
| 1866 | case OP_NotNull: { |
drh | 143f3c4 | 2004-01-07 20:37:52 +0000 | [diff] [blame] | 1867 | int i, cnt; |
| 1868 | cnt = pOp->p1; |
| 1869 | if( cnt<0 ) cnt = -cnt; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1870 | assert( &pTos[1-cnt] >= p->aStack ); |
| 1871 | for(i=0; i<cnt && (pTos[1+i-cnt].flags & MEM_Null)==0; i++){} |
drh | 143f3c4 | 2004-01-07 20:37:52 +0000 | [diff] [blame] | 1872 | if( i>=cnt ) pc = pOp->p2-1; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1873 | if( pOp->p1>0 ) popStack(&pTos, cnt); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1874 | break; |
| 1875 | } |
| 1876 | |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 1877 | /* Opcode: Column P1 P2 * |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 1878 | ** |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1879 | ** Interpret the data that cursor P1 points to as a structure built using |
| 1880 | ** the MakeRecord instruction. (See the MakeRecord opcode for additional |
| 1881 | ** information about the format of the data.) Push onto the stack the value |
| 1882 | ** of the P2-th column contained in the data. |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 1883 | ** |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1884 | ** If the KeyAsData opcode has previously executed on this cursor, then the |
| 1885 | ** field might be extracted from the key rather than the data. |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 1886 | ** |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1887 | ** If P1 is negative, then the record is stored on the stack rather than in |
| 1888 | ** a table. For P1==-1, the top of the stack is used. For P1==-2, the |
| 1889 | ** next on the stack is used. And so forth. The value pushed is always |
| 1890 | ** just a pointer into the record which is stored further down on the |
| 1891 | ** stack. The column value is not copied. |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 1892 | */ |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1893 | case OP_Column: { |
| 1894 | int payloadSize; /* Number of bytes in the record */ |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 1895 | int i = pOp->p1; |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1896 | int p2 = pOp->p2; /* column number to retrieve */ |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 1897 | Cursor *pC; |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1898 | char *zRec; /* Pointer to record-data from stack or pseudo-table. */ |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 1899 | BtCursor *pCrsr; |
| 1900 | |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1901 | char *zData; |
| 1902 | int freeZdata = 0; /* zData requires sqliteFree() */ |
| 1903 | |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 1904 | u64 nField; /* number of fields in the record */ |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1905 | |
| 1906 | int len; /* The length of the serialized data for the column */ |
| 1907 | int offset; |
| 1908 | int nn; |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 1909 | |
| 1910 | assert( i<p->nCursor ); |
| 1911 | pTos++; |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1912 | |
| 1913 | /* This block sets the variable payloadSize, and if the data is coming |
| 1914 | ** from the stack or from a pseudo-table zRec. If the data is coming |
| 1915 | ** from a real cursor, then zRec is left as NULL. |
| 1916 | */ |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 1917 | if( i<0 ){ |
| 1918 | assert( &pTos[i]>=p->aStack ); |
| 1919 | assert( pTos[i].flags & MEM_Str ); |
| 1920 | zRec = pTos[i].z; |
| 1921 | payloadSize = pTos[i].n; |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 1922 | pC->cacheValid = 0; |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame^] | 1923 | }else if( (pC = p->apCsr[i])->pCursor!=0 ){ |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 1924 | sqlite3VdbeCursorMoveto(pC); |
| 1925 | zRec = 0; |
| 1926 | pCrsr = pC->pCursor; |
| 1927 | if( pC->nullRow ){ |
| 1928 | payloadSize = 0; |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 1929 | }else if( pC->cacheValid ){ |
| 1930 | payloadSize = pC->payloadSize; |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 1931 | }else if( pC->keyAsData ){ |
danielk1977 | 96fc5fe | 2004-05-13 11:34:16 +0000 | [diff] [blame] | 1932 | i64 payloadSize64; |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 1933 | sqlite3BtreeKeySize(pCrsr, &payloadSize64); |
| 1934 | payloadSize = payloadSize64; |
| 1935 | }else{ |
| 1936 | sqlite3BtreeDataSize(pCrsr, &payloadSize); |
| 1937 | } |
| 1938 | }else if( pC->pseudoTable ){ |
| 1939 | payloadSize = pC->nData; |
| 1940 | zRec = pC->pData; |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 1941 | pC->cacheValid = 0; |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 1942 | assert( payloadSize==0 || zRec!=0 ); |
| 1943 | }else{ |
| 1944 | payloadSize = 0; |
| 1945 | } |
| 1946 | |
| 1947 | /* If payloadSize is 0, then just push a NULL onto the stack. */ |
| 1948 | if( payloadSize==0 ){ |
| 1949 | pTos->flags = MEM_Null; |
| 1950 | break; |
| 1951 | } |
| 1952 | |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 1953 | /* Read and parse the table header. Store the results of the parse |
| 1954 | ** into the record header cache fields of the cursor. |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 1955 | */ |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 1956 | if( !pC->cacheValid ){ |
| 1957 | pC->payloadSize = payloadSize; |
| 1958 | if( zRec ){ |
| 1959 | zData = zRec; |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 1960 | }else{ |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 1961 | /* We can assume that 9 bytes (maximum length of a varint) fits |
| 1962 | ** on the main page in all cases. |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1963 | */ |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 1964 | int n = 9; |
| 1965 | if( payloadSize<9 ) n = payloadSize; |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1966 | if( pC->keyAsData ){ |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 1967 | zData = (char *)sqlite3BtreeKeyFetch(pCrsr, n); |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1968 | }else{ |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 1969 | zData = (char *)sqlite3BtreeDataFetch(pCrsr, n); |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1970 | } |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 1971 | assert( zData ); |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 1972 | } |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 1973 | offset = sqlite3GetVarint(zData, &nField); |
| 1974 | if( nField>pC->nField ){ |
| 1975 | sqliteFree(pC->aType); |
| 1976 | pC->aType = sqliteMallocRaw( nField*sizeof(pC->aType[0]) ); |
| 1977 | if( pC->aType==0 ){ |
| 1978 | goto no_mem; |
| 1979 | } |
| 1980 | } |
| 1981 | pC->nField = nField; |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 1982 | |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 1983 | if( !zRec ){ |
| 1984 | /* If the record is stored in a table, see if enough of it is on |
| 1985 | ** the main page to use sqlite3BtreeDataFetch() to get the data |
| 1986 | ** containing the nField serial types (varints). This will almost |
| 1987 | ** always work, but if it doesn't sqliteMalloc() space and use |
| 1988 | ** sqlite3BtreeData(). |
| 1989 | ** |
| 1990 | ** Estimate the maximum space required by the nField varints by |
| 1991 | ** assuming the maximum space for each is the length required to store: |
| 1992 | ** |
| 1993 | ** (<record length> * 2) + 13 |
| 1994 | ** |
| 1995 | ** This is the serial-type for a text object as long as the record |
| 1996 | ** itself. In all cases the length required to store this is three |
| 1997 | ** bytes or less. |
| 1998 | */ |
| 1999 | int max_space = sqlite3VarintLen((((u64)payloadSize)<<1)+13)*nField; |
| 2000 | max_space += offset; |
| 2001 | if( max_space>payloadSize ){ |
| 2002 | max_space = payloadSize; |
| 2003 | } |
| 2004 | |
| 2005 | if( pC->keyAsData ){ |
| 2006 | zData = (char *)sqlite3BtreeKeyFetch(pCrsr, max_space); |
| 2007 | }else{ |
| 2008 | zData = (char *)sqlite3BtreeDataFetch(pCrsr, max_space); |
| 2009 | } |
| 2010 | if( !zData ){ |
| 2011 | /* This code will run very infrequently (e.g. tables with several |
| 2012 | ** hundred columns). |
| 2013 | */ |
| 2014 | zData = (char *)sqliteMallocRaw(max_space); |
| 2015 | if( !zData ){ |
| 2016 | goto no_mem; |
| 2017 | } |
| 2018 | if( pC->keyAsData ){ |
| 2019 | rc = sqlite3BtreeKey(pCrsr, 0, max_space, zData); |
| 2020 | }else{ |
| 2021 | rc = sqlite3BtreeData(pCrsr, 0, max_space, zData); |
| 2022 | } |
| 2023 | if( rc!=SQLITE_OK ){ |
| 2024 | sqliteFree(zData); |
| 2025 | goto abort_due_to_error; |
| 2026 | } |
| 2027 | freeZdata = 1; |
| 2028 | } |
| 2029 | } |
| 2030 | |
| 2031 | /* Read all the serial types for the record. At the end of this block |
| 2032 | ** variable offset is set to the offset to the start of Data0 in the record. |
| 2033 | */ |
| 2034 | for(nn=0; nn<nField; nn++){ |
| 2035 | offset += sqlite3GetVarint(&zData[offset], &pC->aType[nn]); |
| 2036 | } |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 2037 | if( freeZdata ){ |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 2038 | freeZdata = 0; |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2039 | sqliteFree(zData); |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2040 | } |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2041 | pC->nHeader = offset; |
| 2042 | pC->cacheValid = 1; |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 2043 | } |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2044 | |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2045 | /* Compute the offset from the beginning of the record to the beginning |
| 2046 | ** of the data. And get the length of the data. |
| 2047 | */ |
| 2048 | offset = pC->nHeader; |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 2049 | for(nn=0; nn<p2; nn++){ |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2050 | offset += sqlite3VdbeSerialTypeLen(pC->aType[nn]); |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 2051 | } |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2052 | len = sqlite3VdbeSerialTypeLen(pC->aType[p2]); |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 2053 | |
| 2054 | if( !zRec ){ |
| 2055 | /* If the record is stored in a table, see if enough of it |
| 2056 | ** is on the main page to read our column using |
| 2057 | ** sqlite3BtreeDataFetch(). If not sqliteMalloc() space and read data |
| 2058 | ** with sqlite3BtreeData(). |
| 2059 | */ |
| 2060 | if( pC->keyAsData ){ |
| 2061 | zData = (char *)sqlite3BtreeKeyFetch(pCrsr, offset+len); |
| 2062 | }else{ |
| 2063 | zData = (char *)sqlite3BtreeDataFetch(pCrsr, offset+len); |
| 2064 | } |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2065 | if( !zData ){ |
drh | 10617cd | 2004-05-14 15:27:27 +0000 | [diff] [blame] | 2066 | zData = (char *)sqliteMallocRaw(len); |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 2067 | if( !zData ){ |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2068 | goto no_mem; |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 2069 | } |
| 2070 | if( pC->keyAsData ){ |
| 2071 | rc = sqlite3BtreeKey(pCrsr, offset, len, zData); |
| 2072 | }else{ |
| 2073 | rc = sqlite3BtreeData(pCrsr, offset, len, zData); |
| 2074 | } |
| 2075 | if( rc!=SQLITE_OK ){ |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 2076 | sqliteFree( zData ); |
| 2077 | goto abort_due_to_error; |
| 2078 | } |
| 2079 | freeZdata = 1; |
| 2080 | offset = 0; |
| 2081 | } |
| 2082 | } |
| 2083 | |
| 2084 | /* Deserialize the value directly into the top of the stack */ |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2085 | sqlite3VdbeSerialGet(&zData[offset], pC->aType[p2], pTos); |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 2086 | |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 2087 | if( freeZdata ){ |
| 2088 | sqliteFree(zData); |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2089 | } |
| 2090 | break; |
| 2091 | } |
| 2092 | |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 2093 | /* Opcode MakeRecord P1 * P3 |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2094 | ** |
| 2095 | ** This opcode (not yet in use) is a replacement for the current |
| 2096 | ** OP_MakeRecord that supports the SQLite3 manifest typing feature. |
| 2097 | ** It drops the (P2==1) option that was never use. |
| 2098 | ** |
| 2099 | ** Convert the top P1 entries of the stack into a single entry |
| 2100 | ** suitable for use as a data record in a database table. The |
| 2101 | ** details of the format are irrelavant as long as the OP_Column |
| 2102 | ** opcode can decode the record later. Refer to source code |
| 2103 | ** comments for the details of the record format. |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 2104 | ** |
| 2105 | ** P3 may be a string that is P1 characters long. The nth character of the |
| 2106 | ** string indicates the column affinity that should be used for the nth |
| 2107 | ** field of the index key (i.e. the first character of P3 corresponds to the |
| 2108 | ** lowest element on the stack). |
| 2109 | ** |
| 2110 | ** Character Column affinity |
| 2111 | ** ------------------------------ |
| 2112 | ** 'n' NUMERIC |
| 2113 | ** 'i' INTEGER |
| 2114 | ** 't' TEXT |
| 2115 | ** 'o' NONE |
| 2116 | ** |
| 2117 | ** If P3 is NULL then all index fields have the affinity NONE. |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2118 | */ |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 2119 | case OP_MakeRecord: { |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2120 | /* Assuming the record contains N fields, the record format looks |
| 2121 | ** like this: |
| 2122 | ** |
| 2123 | ** -------------------------------------------------------------------------- |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 2124 | ** | num-fields | type 0 | type 1 | ... | type N-1 | data0 | ... | data N-1 | |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2125 | ** -------------------------------------------------------------------------- |
| 2126 | ** |
| 2127 | ** Data(0) is taken from the lowest element of the stack and data(N-1) is |
| 2128 | ** the top of the stack. |
| 2129 | ** |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 2130 | ** Each type field is a varint representing the serial type of the |
| 2131 | ** corresponding data element (see sqlite3VdbeSerialType()). The |
| 2132 | ** num-fields field is also a varint storing N. |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2133 | ** |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2134 | ** TODO: Even when the record is short enough for Mem::zShort, this opcode |
| 2135 | ** allocates it dynamically. |
| 2136 | */ |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2137 | int nField = pOp->p1; |
| 2138 | unsigned char *zNewRecord; |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 2139 | unsigned char *zCsr; |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 2140 | char *zAffinity; |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2141 | Mem *pRec; |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 2142 | int nBytes; /* Space required for this record */ |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2143 | |
| 2144 | Mem *pData0 = &pTos[1-nField]; |
| 2145 | assert( pData0>=p->aStack ); |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 2146 | zAffinity = pOp->p3; |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2147 | |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 2148 | /* Loop through the elements that will make up the record to figure |
| 2149 | ** out how much space is required for the new record. |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2150 | */ |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 2151 | nBytes = sqlite3VarintLen(nField); |
| 2152 | for(pRec=pData0; pRec<=pTos; pRec++){ |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 2153 | u64 serial_type; |
| 2154 | if( zAffinity ){ |
| 2155 | applyAffinityByChar(pRec, zAffinity[pRec-pData0]); |
| 2156 | } |
| 2157 | serial_type = sqlite3VdbeSerialType(pRec); |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 2158 | nBytes += sqlite3VdbeSerialTypeLen(serial_type); |
| 2159 | nBytes += sqlite3VarintLen(serial_type); |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2160 | } |
| 2161 | |
danielk1977 | 96fc5fe | 2004-05-13 11:34:16 +0000 | [diff] [blame] | 2162 | if( nBytes>MAX_BYTES_PER_ROW ){ |
| 2163 | rc = SQLITE_TOOBIG; |
| 2164 | goto abort_due_to_error; |
| 2165 | } |
| 2166 | |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 2167 | /* Allocate space for the new record. */ |
drh | 10617cd | 2004-05-14 15:27:27 +0000 | [diff] [blame] | 2168 | zNewRecord = sqliteMallocRaw(nBytes); |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2169 | if( !zNewRecord ){ |
| 2170 | rc = SQLITE_NOMEM; |
| 2171 | goto abort_due_to_error; |
| 2172 | } |
| 2173 | |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 2174 | /* Write the record */ |
| 2175 | zCsr = zNewRecord; |
| 2176 | zCsr += sqlite3PutVarint(zCsr, nField); /* number of fields */ |
| 2177 | for(pRec=pData0; pRec<=pTos; pRec++){ |
| 2178 | u64 serial_type = sqlite3VdbeSerialType(pRec); |
| 2179 | zCsr += sqlite3PutVarint(zCsr, serial_type); /* serial type */ |
| 2180 | } |
| 2181 | for(pRec=pData0; pRec<=pTos; pRec++){ |
| 2182 | zCsr += sqlite3VdbeSerialPut(zCsr, pRec); /* serial data */ |
| 2183 | } |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2184 | |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 2185 | /* If zCsr has not been advanced exactly nBytes bytes, then one |
| 2186 | ** of the sqlite3PutVarint() or sqlite3VdbeSerialPut() calls above |
| 2187 | ** failed. This indicates a corrupted memory cell or code bug. |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2188 | */ |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 2189 | if( zCsr!=(zNewRecord+nBytes) ){ |
| 2190 | rc = SQLITE_INTERNAL; |
| 2191 | goto abort_due_to_error; |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2192 | } |
| 2193 | |
| 2194 | /* Pop nField entries from the stack and push the new entry on */ |
| 2195 | popStack(&pTos, nField); |
| 2196 | pTos++; |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 2197 | pTos->n = nBytes; |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2198 | pTos->z = zNewRecord; |
| 2199 | pTos->flags = MEM_Str | MEM_Dyn; |
| 2200 | |
| 2201 | break; |
| 2202 | } |
| 2203 | |
drh | c9b84a1 | 2002-06-20 11:36:48 +0000 | [diff] [blame] | 2204 | /* Opcode: MakeKey P1 P2 P3 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2205 | ** |
| 2206 | ** Convert the top P1 entries of the stack into a single entry suitable |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 2207 | ** for use as the key in an index. If P2 is not zero, then the original |
| 2208 | ** entries are popped off the stack. If P2 is zero, the original entries |
| 2209 | ** remain on the stack. |
| 2210 | ** |
| 2211 | ** P3 is interpreted in the same way as for MakeIdxKey. |
| 2212 | */ |
| 2213 | /* Opcode: MakeIdxKey P1 P2 P3 |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 2214 | ** |
| 2215 | ** Convert the top P1 entries of the stack into a single entry suitable |
| 2216 | ** for use as the key in an index. In addition, take one additional integer |
| 2217 | ** off of the stack, treat that integer as an eight-byte record number, and |
| 2218 | ** append the integer to the key as a varint. Thus a total of P1+1 entries |
| 2219 | ** are popped from the stack for this instruction and a single entry is |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 2220 | ** pushed back. |
| 2221 | ** |
| 2222 | ** If P2 is not zero and one or more of the P1 entries that go into the |
| 2223 | ** generated key is NULL, then jump to P2 after the new key has been |
| 2224 | ** pushed on the stack. In other words, jump to P2 if the key is |
| 2225 | ** guaranteed to be unique. This jump can be used to skip a subsequent |
| 2226 | ** uniqueness test. |
| 2227 | ** |
| 2228 | ** P3 may be a string that is P1 characters long. The nth character of the |
| 2229 | ** string indicates the column affinity that should be used for the nth |
| 2230 | ** field of the index key (i.e. the first character of P3 corresponds to the |
| 2231 | ** lowest element on the stack). |
| 2232 | ** |
| 2233 | ** Character Column affinity |
| 2234 | ** ------------------------------ |
| 2235 | ** 'n' NUMERIC |
| 2236 | ** 'i' INTEGER |
| 2237 | ** 't' TEXT |
| 2238 | ** 'o' NONE |
| 2239 | ** |
| 2240 | ** If P3 is NULL then all index fields have the affinity NUMERIC. |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 2241 | */ |
danielk1977 | 452c989 | 2004-05-13 05:16:15 +0000 | [diff] [blame] | 2242 | case OP_MakeKey: |
| 2243 | case OP_MakeIdxKey: { |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 2244 | Mem *pRec; |
| 2245 | Mem *pData0; |
| 2246 | int nField; |
| 2247 | u64 rowid; |
| 2248 | int nByte = 0; |
| 2249 | int addRowid; |
danielk1977 | 452c989 | 2004-05-13 05:16:15 +0000 | [diff] [blame] | 2250 | int containsNull = 0; |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 2251 | char *zKey; /* The new key */ |
danielk1977 | 452c989 | 2004-05-13 05:16:15 +0000 | [diff] [blame] | 2252 | int offset = 0; |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 2253 | char *zAffinity = pOp->p3; |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 2254 | |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 2255 | assert( zAffinity ); |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 2256 | nField = pOp->p1; |
| 2257 | pData0 = &pTos[1-nField]; |
| 2258 | assert( pData0>=p->aStack ); |
| 2259 | |
danielk1977 | 452c989 | 2004-05-13 05:16:15 +0000 | [diff] [blame] | 2260 | addRowid = ((pOp->opcode==OP_MakeIdxKey)?1:0); |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 2261 | |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 2262 | /* Loop through the P1 elements that will make up the new index |
| 2263 | ** key. Call applyAffinity() to perform any conversion required |
| 2264 | ** the column affinity string P3 to modify stack elements in place. |
| 2265 | ** Set containsNull to 1 if a NULL value is encountered. |
| 2266 | ** |
| 2267 | ** Once the value has been coerced, figure out how much space is required |
| 2268 | ** to store the coerced values serial-type and blob, and add this |
| 2269 | ** quantity to nByte. |
| 2270 | ** |
| 2271 | ** TODO: Figure out if the in-place coercion causes a problem for |
| 2272 | ** OP_MakeKey when P2 is 0 (used by DISTINCT). |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 2273 | */ |
| 2274 | for(pRec=pData0; pRec<=pTos; pRec++){ |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 2275 | u64 serial_type; |
| 2276 | if( zAffinity ){ |
| 2277 | applyAffinityByChar(pRec, zAffinity[pRec-pData0]); |
| 2278 | }else{ |
| 2279 | applyAffinity(pRec, SQLITE_SO_NUM); |
| 2280 | } |
| 2281 | if( pRec->flags&MEM_Null ){ |
danielk1977 | 452c989 | 2004-05-13 05:16:15 +0000 | [diff] [blame] | 2282 | containsNull = 1; |
| 2283 | } |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 2284 | serial_type = sqlite3VdbeSerialType(pRec); |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 2285 | nByte += sqlite3VarintLen(serial_type); |
| 2286 | nByte += sqlite3VdbeSerialTypeLen(serial_type); |
| 2287 | } |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 2288 | |
| 2289 | /* If we have to append a varint rowid to this record, set 'rowid' |
| 2290 | ** to the value of the rowid and increase nByte by the amount of space |
| 2291 | ** required to store it and the 0x00 seperator byte. |
| 2292 | */ |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 2293 | if( addRowid ){ |
danielk1977 | 452c989 | 2004-05-13 05:16:15 +0000 | [diff] [blame] | 2294 | pRec = &pTos[0-nField]; |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 2295 | assert( pRec>=p->aStack ); |
| 2296 | Integerify(pRec); |
| 2297 | rowid = pRec->i; |
| 2298 | nByte += sqlite3VarintLen(rowid); |
danielk1977 | 452c989 | 2004-05-13 05:16:15 +0000 | [diff] [blame] | 2299 | nByte++; |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 2300 | } |
danielk1977 | 96fc5fe | 2004-05-13 11:34:16 +0000 | [diff] [blame] | 2301 | |
| 2302 | if( nByte>MAX_BYTES_PER_ROW ){ |
| 2303 | rc = SQLITE_TOOBIG; |
| 2304 | goto abort_due_to_error; |
| 2305 | } |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 2306 | |
| 2307 | /* Allocate space for the new key */ |
drh | 10617cd | 2004-05-14 15:27:27 +0000 | [diff] [blame] | 2308 | zKey = (char *)sqliteMallocRaw(nByte); |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 2309 | if( !zKey ){ |
| 2310 | rc = SQLITE_NOMEM; |
| 2311 | goto abort_due_to_error; |
| 2312 | } |
| 2313 | |
| 2314 | /* Build the key in the buffer pointed to by zKey. */ |
| 2315 | for(pRec=pData0; pRec<=pTos; pRec++){ |
danielk1977 | 452c989 | 2004-05-13 05:16:15 +0000 | [diff] [blame] | 2316 | offset += sqlite3PutVarint(&zKey[offset], sqlite3VdbeSerialType(pRec)); |
| 2317 | offset += sqlite3VdbeSerialPut(&zKey[offset], pRec); |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 2318 | } |
| 2319 | if( addRowid ){ |
danielk1977 | 452c989 | 2004-05-13 05:16:15 +0000 | [diff] [blame] | 2320 | zKey[offset++] = '\0'; |
| 2321 | offset += sqlite3PutVarint(&zKey[offset], rowid); |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 2322 | } |
danielk1977 | 452c989 | 2004-05-13 05:16:15 +0000 | [diff] [blame] | 2323 | assert( offset==nByte ); |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 2324 | |
| 2325 | /* Pop the consumed values off the stack and push on the new key. */ |
danielk1977 | 452c989 | 2004-05-13 05:16:15 +0000 | [diff] [blame] | 2326 | if( addRowid||(pOp->p2==0) ){ |
| 2327 | popStack(&pTos, nField+addRowid); |
| 2328 | } |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 2329 | pTos++; |
danielk1977 | 452c989 | 2004-05-13 05:16:15 +0000 | [diff] [blame] | 2330 | pTos->flags = MEM_Str|MEM_Dyn; /* TODO: should eventually be MEM_Blob */ |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 2331 | pTos->z = zKey; |
| 2332 | pTos->n = nByte; |
| 2333 | |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 2334 | /* If P2 is non-zero, and if the key contains a NULL value, and if this |
| 2335 | ** was an OP_MakeIdxKey instruction, not OP_MakeKey, jump to P2. |
| 2336 | */ |
| 2337 | if( pOp->p2 && containsNull && addRowid ){ |
danielk1977 | 452c989 | 2004-05-13 05:16:15 +0000 | [diff] [blame] | 2338 | pc = pOp->p2 - 1; |
| 2339 | } |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 2340 | break; |
| 2341 | } |
| 2342 | |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2343 | /* Opcode: Checkpoint P1 * * |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2344 | ** |
| 2345 | ** Begin a checkpoint. A checkpoint is the beginning of a operation that |
| 2346 | ** is part of a larger transaction but which might need to be rolled back |
| 2347 | ** itself without effecting the containing transaction. A checkpoint will |
| 2348 | ** be automatically committed or rollback when the VDBE halts. |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2349 | ** |
| 2350 | ** The checkpoint is begun on the database file with index P1. The main |
| 2351 | ** database file has an index of 0 and the file used for temporary tables |
| 2352 | ** has an index of 1. |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2353 | */ |
| 2354 | case OP_Checkpoint: { |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2355 | int i = pOp->p1; |
drh | 1aa4965 | 2003-06-02 23:14:13 +0000 | [diff] [blame] | 2356 | if( i>=0 && i<db->nDb && db->aDb[i].pBt && db->aDb[i].inTrans==1 ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2357 | rc = sqlite3BtreeBeginStmt(db->aDb[i].pBt); |
drh | 1aa4965 | 2003-06-02 23:14:13 +0000 | [diff] [blame] | 2358 | if( rc==SQLITE_OK ) db->aDb[i].inTrans = 2; |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2359 | } |
| 2360 | break; |
| 2361 | } |
| 2362 | |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 2363 | /* Opcode: Transaction P1 * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2364 | ** |
| 2365 | ** Begin a transaction. The transaction ends when a Commit or Rollback |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2366 | ** opcode is encountered. Depending on the ON CONFLICT setting, the |
| 2367 | ** transaction might also be rolled back if an error is encountered. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2368 | ** |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2369 | ** P1 is the index of the database file on which the transaction is |
| 2370 | ** started. Index 0 is the main database file and index 1 is the |
| 2371 | ** file used for temporary tables. |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 2372 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2373 | ** A write lock is obtained on the database file when a transaction is |
| 2374 | ** started. No other process can read or write the file while the |
| 2375 | ** transaction is underway. Starting a transaction also creates a |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2376 | ** rollback journal. A transaction must be started before any changes |
| 2377 | ** can be made to the database. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2378 | */ |
| 2379 | case OP_Transaction: { |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 2380 | int busy = 1; |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2381 | int i = pOp->p1; |
drh | 8bf8dc9 | 2003-05-17 17:35:10 +0000 | [diff] [blame] | 2382 | assert( i>=0 && i<db->nDb ); |
| 2383 | if( db->aDb[i].inTrans ) break; |
| 2384 | while( db->aDb[i].pBt!=0 && busy ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2385 | rc = sqlite3BtreeBeginTrans(db->aDb[i].pBt); |
drh | 90bfcda | 2001-09-23 19:46:51 +0000 | [diff] [blame] | 2386 | switch( rc ){ |
| 2387 | case SQLITE_BUSY: { |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 2388 | if( db->xBusyCallback==0 ){ |
| 2389 | p->pc = pc; |
| 2390 | p->undoTransOnError = 1; |
| 2391 | p->rc = SQLITE_BUSY; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 2392 | p->pTos = pTos; |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 2393 | return SQLITE_BUSY; |
| 2394 | }else if( (*db->xBusyCallback)(db->pBusyArg, "", busy++)==0 ){ |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 2395 | sqlite3SetString(&p->zErrMsg, sqlite3_error_string(rc), (char*)0); |
drh | 90bfcda | 2001-09-23 19:46:51 +0000 | [diff] [blame] | 2396 | busy = 0; |
| 2397 | } |
| 2398 | break; |
| 2399 | } |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 2400 | case SQLITE_READONLY: { |
| 2401 | rc = SQLITE_OK; |
| 2402 | /* Fall thru into the next case */ |
| 2403 | } |
drh | 90bfcda | 2001-09-23 19:46:51 +0000 | [diff] [blame] | 2404 | case SQLITE_OK: { |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 2405 | p->inTempTrans = 0; |
drh | 90bfcda | 2001-09-23 19:46:51 +0000 | [diff] [blame] | 2406 | busy = 0; |
| 2407 | break; |
| 2408 | } |
| 2409 | default: { |
| 2410 | goto abort_due_to_error; |
| 2411 | } |
| 2412 | } |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 2413 | } |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2414 | db->aDb[i].inTrans = 1; |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 2415 | p->undoTransOnError = 1; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2416 | break; |
| 2417 | } |
| 2418 | |
| 2419 | /* Opcode: Commit * * * |
| 2420 | ** |
| 2421 | ** Cause all modifications to the database that have been made since the |
| 2422 | ** last Transaction to actually take effect. No additional modifications |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2423 | ** are allowed until another transaction is started. The Commit instruction |
| 2424 | ** deletes the journal file and releases the write lock on the database. |
| 2425 | ** A read lock continues to be held if there are still cursors open. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2426 | */ |
| 2427 | case OP_Commit: { |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2428 | int i; |
drh | aa940ea | 2004-01-15 02:44:03 +0000 | [diff] [blame] | 2429 | if( db->xCommitCallback!=0 ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2430 | if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse; |
drh | aa940ea | 2004-01-15 02:44:03 +0000 | [diff] [blame] | 2431 | if( db->xCommitCallback(db->pCommitArg)!=0 ){ |
| 2432 | rc = SQLITE_CONSTRAINT; |
| 2433 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2434 | if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse; |
drh | aa940ea | 2004-01-15 02:44:03 +0000 | [diff] [blame] | 2435 | } |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2436 | for(i=0; rc==SQLITE_OK && i<db->nDb; i++){ |
| 2437 | if( db->aDb[i].inTrans ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2438 | rc = sqlite3BtreeCommit(db->aDb[i].pBt); |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2439 | db->aDb[i].inTrans = 0; |
| 2440 | } |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 2441 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2442 | if( rc==SQLITE_OK ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2443 | sqlite3CommitInternalChanges(db); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2444 | }else{ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2445 | sqlite3RollbackAll(db); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2446 | } |
| 2447 | break; |
| 2448 | } |
| 2449 | |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2450 | /* Opcode: Rollback P1 * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2451 | ** |
| 2452 | ** Cause all modifications to the database that have been made since the |
| 2453 | ** last Transaction to be undone. The database is restored to its state |
| 2454 | ** before the Transaction opcode was executed. No additional modifications |
| 2455 | ** are allowed until another transaction is started. |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2456 | ** |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2457 | ** P1 is the index of the database file that is committed. An index of 0 |
| 2458 | ** is used for the main database and an index of 1 is used for the file used |
| 2459 | ** to hold temporary tables. |
| 2460 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2461 | ** This instruction automatically closes all cursors and releases both |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2462 | ** the read and write locks on the indicated database. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2463 | */ |
| 2464 | case OP_Rollback: { |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2465 | sqlite3RollbackAll(db); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2466 | break; |
| 2467 | } |
| 2468 | |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2469 | /* Opcode: ReadCookie P1 P2 * |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 2470 | ** |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2471 | ** Read cookie number P2 from database P1 and push it onto the stack. |
| 2472 | ** P2==0 is the schema version. P2==1 is the database format. |
| 2473 | ** P2==2 is the recommended pager cache size, and so forth. P1==0 is |
| 2474 | ** the main database file and P1==1 is the database file used to store |
| 2475 | ** temporary tables. |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 2476 | ** |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 2477 | ** There must be a read-lock on the database (either a transaction |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2478 | ** must be started or there must be an open cursor) before |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 2479 | ** executing this instruction. |
| 2480 | */ |
| 2481 | case OP_ReadCookie: { |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 2482 | int iMeta; |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 2483 | assert( pOp->p2<SQLITE_N_BTREE_META ); |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2484 | assert( pOp->p1>=0 && pOp->p1<db->nDb ); |
| 2485 | assert( db->aDb[pOp->p1].pBt!=0 ); |
drh | a3b321d | 2004-05-11 09:31:31 +0000 | [diff] [blame] | 2486 | /* The indexing of meta values at the schema layer is off by one from |
| 2487 | ** the indexing in the btree layer. The btree considers meta[0] to |
| 2488 | ** be the number of free pages in the database (a read-only value) |
| 2489 | ** and meta[1] to be the schema cookie. The schema layer considers |
| 2490 | ** meta[1] to be the schema cookie. So we have to shift the index |
| 2491 | ** by one in the following statement. |
| 2492 | */ |
| 2493 | rc = sqlite3BtreeGetMeta(db->aDb[pOp->p1].pBt, 1 + pOp->p2, &iMeta); |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 2494 | pTos++; |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 2495 | pTos->i = iMeta; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 2496 | pTos->flags = MEM_Int; |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 2497 | break; |
| 2498 | } |
| 2499 | |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2500 | /* Opcode: SetCookie P1 P2 * |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 2501 | ** |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2502 | ** Write the top of the stack into cookie number P2 of database P1. |
| 2503 | ** P2==0 is the schema version. P2==1 is the database format. |
| 2504 | ** P2==2 is the recommended pager cache size, and so forth. P1==0 is |
| 2505 | ** the main database file and P1==1 is the database file used to store |
| 2506 | ** temporary tables. |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 2507 | ** |
| 2508 | ** A transaction must be started before executing this opcode. |
| 2509 | */ |
| 2510 | case OP_SetCookie: { |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 2511 | assert( pOp->p2<SQLITE_N_BTREE_META ); |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2512 | assert( pOp->p1>=0 && pOp->p1<db->nDb ); |
| 2513 | assert( db->aDb[pOp->p1].pBt!=0 ); |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 2514 | assert( pTos>=p->aStack ); |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 2515 | Integerify(pTos); |
drh | a3b321d | 2004-05-11 09:31:31 +0000 | [diff] [blame] | 2516 | /* See note about index shifting on OP_ReadCookie */ |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 2517 | rc = sqlite3BtreeUpdateMeta(db->aDb[pOp->p1].pBt, 1+pOp->p2, (int)pTos->i); |
drh | 79f14b7 | 2004-03-03 01:51:24 +0000 | [diff] [blame] | 2518 | Release(pTos); |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 2519 | pTos--; |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 2520 | break; |
| 2521 | } |
| 2522 | |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 2523 | /* Opcode: VerifyCookie P1 P2 * |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 2524 | ** |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2525 | ** Check the value of global database parameter number 0 (the |
| 2526 | ** schema version) and make sure it is equal to P2. |
| 2527 | ** P1 is the database number which is 0 for the main database file |
| 2528 | ** and 1 for the file holding temporary tables and some higher number |
| 2529 | ** for auxiliary databases. |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 2530 | ** |
| 2531 | ** The cookie changes its value whenever the database schema changes. |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2532 | ** This operation is used to detect when that the cookie has changed |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 2533 | ** and that the current process needs to reread the schema. |
| 2534 | ** |
| 2535 | ** Either a transaction needs to have been started or an OP_Open needs |
| 2536 | ** to be executed (to establish a read lock) before this opcode is |
| 2537 | ** invoked. |
| 2538 | */ |
| 2539 | case OP_VerifyCookie: { |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 2540 | int iMeta; |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2541 | assert( pOp->p1>=0 && pOp->p1<db->nDb ); |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 2542 | rc = sqlite3BtreeGetMeta(db->aDb[pOp->p1].pBt, 1, &iMeta); |
| 2543 | if( rc==SQLITE_OK && iMeta!=pOp->p2 ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2544 | sqlite3SetString(&p->zErrMsg, "database schema has changed", (char*)0); |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 2545 | rc = SQLITE_SCHEMA; |
| 2546 | } |
| 2547 | break; |
| 2548 | } |
| 2549 | |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2550 | /* Opcode: OpenRead P1 P2 P3 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2551 | ** |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2552 | ** Open a read-only cursor for the database table whose root page is |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2553 | ** P2 in a database file. The database file is determined by an |
| 2554 | ** integer from the top of the stack. 0 means the main database and |
| 2555 | ** 1 means the database used for temporary tables. Give the new |
| 2556 | ** cursor an identifier of P1. The P1 values need not be contiguous |
| 2557 | ** but all P1 values should be small integers. It is an error for |
| 2558 | ** P1 to be negative. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2559 | ** |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2560 | ** If P2==0 then take the root page number from the next of the stack. |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 2561 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2562 | ** There will be a read lock on the database whenever there is an |
| 2563 | ** open cursor. If the database was unlocked prior to this instruction |
| 2564 | ** then a read lock is acquired as part of this instruction. A read |
| 2565 | ** lock allows other processes to read the database but prohibits |
| 2566 | ** any other process from modifying the database. The read lock is |
| 2567 | ** released when all cursors are closed. If this instruction attempts |
| 2568 | ** to get a read lock but fails, the script terminates with an |
| 2569 | ** SQLITE_BUSY error code. |
| 2570 | ** |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2571 | ** The P3 value is the name of the table or index being opened. |
| 2572 | ** The P3 value is not actually used by this opcode and may be |
| 2573 | ** omitted. But the code generator usually inserts the index or |
| 2574 | ** table name into P3 to make the code easier to read. |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 2575 | ** |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2576 | ** See also OpenWrite. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2577 | */ |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2578 | /* Opcode: OpenWrite P1 P2 P3 |
| 2579 | ** |
| 2580 | ** Open a read/write cursor named P1 on the table or index whose root |
| 2581 | ** page is P2. If P2==0 then take the root page number from the stack. |
| 2582 | ** |
jplyon | 5a56422 | 2003-06-02 06:15:58 +0000 | [diff] [blame] | 2583 | ** The P3 value is the name of the table or index being opened. |
| 2584 | ** The P3 value is not actually used by this opcode and may be |
| 2585 | ** omitted. But the code generator usually inserts the index or |
| 2586 | ** table name into P3 to make the code easier to read. |
| 2587 | ** |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2588 | ** This instruction works just like OpenRead except that it opens the cursor |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2589 | ** in read/write mode. For a given table, there can be one or more read-only |
| 2590 | ** cursors or a single read/write cursor but not both. |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 2591 | ** |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2592 | ** See also OpenRead. |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2593 | */ |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2594 | case OP_OpenRead: |
| 2595 | case OP_OpenWrite: { |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2596 | int busy = 0; |
| 2597 | int i = pOp->p1; |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 2598 | int p2 = pOp->p2; |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 2599 | int wrFlag; |
| 2600 | Btree *pX; |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2601 | int iDb; |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 2602 | Cursor *pCur; |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2603 | |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 2604 | assert( pTos>=p->aStack ); |
| 2605 | Integerify(pTos); |
| 2606 | iDb = pTos->i; |
| 2607 | pTos--; |
| 2608 | assert( iDb>=0 && iDb<db->nDb ); |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2609 | pX = db->aDb[iDb].pBt; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 2610 | assert( pX!=0 ); |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2611 | wrFlag = pOp->opcode==OP_OpenWrite; |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 2612 | if( p2<=0 ){ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 2613 | assert( pTos>=p->aStack ); |
| 2614 | Integerify(pTos); |
| 2615 | p2 = pTos->i; |
| 2616 | pTos--; |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 2617 | if( p2<2 ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2618 | sqlite3SetString(&p->zErrMsg, "root page number less than 2", (char*)0); |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 2619 | rc = SQLITE_INTERNAL; |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 2620 | break; |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 2621 | } |
| 2622 | } |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 2623 | assert( i>=0 ); |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 2624 | if( expandCursorArraySize(p, i) ) goto no_mem; |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame^] | 2625 | pCur = p->apCsr[i]; |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 2626 | sqlite3VdbeCleanupCursor(pCur); |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 2627 | pCur->nullRow = 1; |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 2628 | if( pX==0 ) break; |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 2629 | do{ |
danielk1977 | 452c989 | 2004-05-13 05:16:15 +0000 | [diff] [blame] | 2630 | /* When opening cursors, always supply the comparison function |
| 2631 | ** sqlite3VdbeKeyCompare(). If the table being opened is of type |
| 2632 | ** INTKEY, the btree layer won't call the comparison function anyway. |
| 2633 | */ |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 2634 | rc = sqlite3BtreeCursor(pX, p2, wrFlag, sqlite3VdbeKeyCompare, pCur, |
danielk1977 | 452c989 | 2004-05-13 05:16:15 +0000 | [diff] [blame] | 2635 | &pCur->pCursor); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2636 | switch( rc ){ |
| 2637 | case SQLITE_BUSY: { |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 2638 | if( db->xBusyCallback==0 ){ |
| 2639 | p->pc = pc; |
| 2640 | p->rc = SQLITE_BUSY; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 2641 | p->pTos = &pTos[1 + (pOp->p2<=0)]; /* Operands must remain on stack */ |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 2642 | return SQLITE_BUSY; |
| 2643 | }else if( (*db->xBusyCallback)(db->pBusyArg, pOp->p3, ++busy)==0 ){ |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 2644 | sqlite3SetString(&p->zErrMsg, sqlite3_error_string(rc), (char*)0); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2645 | busy = 0; |
| 2646 | } |
| 2647 | break; |
| 2648 | } |
| 2649 | case SQLITE_OK: { |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 2650 | int flags = sqlite3BtreeFlags(pCur->pCursor); |
| 2651 | pCur->intKey = (flags & BTREE_INTKEY)!=0; |
| 2652 | pCur->zeroData = (flags & BTREE_ZERODATA)!=0; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2653 | busy = 0; |
| 2654 | break; |
| 2655 | } |
drh | f4dada7 | 2004-05-11 09:57:35 +0000 | [diff] [blame] | 2656 | case SQLITE_EMPTY: { |
| 2657 | rc = SQLITE_OK; |
| 2658 | busy = 0; |
| 2659 | break; |
| 2660 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2661 | default: { |
| 2662 | goto abort_due_to_error; |
| 2663 | } |
| 2664 | } |
| 2665 | }while( busy ); |
| 2666 | break; |
| 2667 | } |
| 2668 | |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 2669 | /* Opcode: OpenTemp P1 P2 * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2670 | ** |
drh | 5fe2d8c | 2003-05-10 03:36:53 +0000 | [diff] [blame] | 2671 | ** Open a new cursor to a transient table. |
| 2672 | ** The transient cursor is always opened read/write even if |
| 2673 | ** the main database is read-only. The transient table is deleted |
| 2674 | ** automatically when the cursor is closed. |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 2675 | ** |
| 2676 | ** The cursor points to a BTree table if P2==0 and to a BTree index |
| 2677 | ** if P2==1. A BTree table must have an integer key and can have arbitrary |
| 2678 | ** data. A BTree index has no data but can have an arbitrary key. |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 2679 | ** |
| 2680 | ** This opcode is used for tables that exist for the duration of a single |
| 2681 | ** SQL statement only. Tables created using CREATE TEMPORARY TABLE |
drh | 5fe2d8c | 2003-05-10 03:36:53 +0000 | [diff] [blame] | 2682 | ** are opened using OP_OpenRead or OP_OpenWrite. "Temporary" in the |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 2683 | ** context of this opcode means for the duration of a single SQL statement |
| 2684 | ** whereas "Temporary" in the context of CREATE TABLE means for the duration |
| 2685 | ** of the connection to the database. Same word; different meanings. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2686 | */ |
| 2687 | case OP_OpenTemp: { |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2688 | int i = pOp->p1; |
| 2689 | Cursor *pCx; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 2690 | assert( i>=0 ); |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 2691 | if( expandCursorArraySize(p, i) ) goto no_mem; |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame^] | 2692 | pCx = p->apCsr[i]; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2693 | sqlite3VdbeCleanupCursor(pCx); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2694 | memset(pCx, 0, sizeof(*pCx)); |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 2695 | pCx->nullRow = 1; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2696 | rc = sqlite3BtreeFactory(db, 0, 1, TEMP_PAGES, &pCx->pBt); |
paul | b0208cc | 2003-04-13 18:26:49 +0000 | [diff] [blame] | 2697 | |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2698 | if( rc==SQLITE_OK ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2699 | rc = sqlite3BtreeBeginTrans(pCx->pBt); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2700 | } |
| 2701 | if( rc==SQLITE_OK ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2702 | /* If a transient index is required, create it by calling |
| 2703 | ** sqlite3BtreeCreateTable() with the BTREE_ZERODATA flag before |
| 2704 | ** opening it. If a transient table is required, just use the |
danielk1977 | 0dbe72b | 2004-05-11 04:54:49 +0000 | [diff] [blame] | 2705 | ** automatically created table with root-page 1 (an INTKEY table). |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2706 | */ |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 2707 | if( pOp->p2 ){ |
| 2708 | int pgno; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2709 | rc = sqlite3BtreeCreateTable(pCx->pBt, &pgno, BTREE_ZERODATA); |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 2710 | if( rc==SQLITE_OK ){ |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 2711 | assert( pgno==MASTER_ROOT+1 ); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2712 | rc = sqlite3BtreeCursor(pCx->pBt, pgno, 1, 0, 0, &pCx->pCursor); |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 2713 | } |
| 2714 | }else{ |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 2715 | rc = sqlite3BtreeCursor(pCx->pBt, MASTER_ROOT, 1, 0, 0, &pCx->pCursor); |
danielk1977 | 0dbe72b | 2004-05-11 04:54:49 +0000 | [diff] [blame] | 2716 | pCx->intKey = 1; |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 2717 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2718 | } |
| 2719 | break; |
| 2720 | } |
| 2721 | |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 2722 | /* Opcode: OpenPseudo P1 * * |
| 2723 | ** |
| 2724 | ** Open a new cursor that points to a fake table that contains a single |
| 2725 | ** row of data. Any attempt to write a second row of data causes the |
| 2726 | ** first row to be deleted. All data is deleted when the cursor is |
| 2727 | ** closed. |
| 2728 | ** |
| 2729 | ** A pseudo-table created by this opcode is useful for holding the |
| 2730 | ** NEW or OLD tables in a trigger. |
| 2731 | */ |
| 2732 | case OP_OpenPseudo: { |
| 2733 | int i = pOp->p1; |
| 2734 | Cursor *pCx; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 2735 | assert( i>=0 ); |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 2736 | if( expandCursorArraySize(p, i) ) goto no_mem; |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame^] | 2737 | pCx = p->apCsr[i]; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2738 | sqlite3VdbeCleanupCursor(pCx); |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 2739 | memset(pCx, 0, sizeof(*pCx)); |
| 2740 | pCx->nullRow = 1; |
| 2741 | pCx->pseudoTable = 1; |
| 2742 | break; |
| 2743 | } |
| 2744 | |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2745 | /* Opcode: Close P1 * * |
| 2746 | ** |
| 2747 | ** Close a cursor previously opened as P1. If P1 is not |
| 2748 | ** currently open, this instruction is a no-op. |
| 2749 | */ |
| 2750 | case OP_Close: { |
| 2751 | int i = pOp->p1; |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 2752 | if( i>=0 && i<p->nCursor ){ |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame^] | 2753 | sqlite3VdbeCleanupCursor(p->apCsr[i]); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2754 | } |
| 2755 | break; |
| 2756 | } |
| 2757 | |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 2758 | /* Opcode: MoveTo P1 P2 * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2759 | ** |
| 2760 | ** Pop the top of the stack and use its value as a key. Reposition |
| 2761 | ** cursor P1 so that it points to an entry with a matching key. If |
| 2762 | ** the table contains no record with a matching key, then the cursor |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 2763 | ** is left pointing at the first record that is greater than the key. |
| 2764 | ** If there are no records greater than the key and P2 is not zero, |
| 2765 | ** then an immediate jump to P2 is made. |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2766 | ** |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 2767 | ** If P3 is not NULL, then the cursor is left pointing at the first |
| 2768 | ** record that is greater than the key of which the key is not a prefix. |
| 2769 | ** This is the same effect that executing OP_IncrKey on the key value |
| 2770 | ** before OP_MoveTo used to have. |
| 2771 | ** |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 2772 | ** See also: Found, NotFound, Distinct, MoveLt |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2773 | */ |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 2774 | /* Opcode: MoveLt P1 P2 * |
| 2775 | ** |
| 2776 | ** Pop the top of the stack and use its value as a key. Reposition |
| 2777 | ** cursor P1 so that it points to the entry with the largest key that is |
| 2778 | ** less than the key popped from the stack. |
| 2779 | ** If there are no records less than than the key and P2 |
| 2780 | ** is not zero then an immediate jump to P2 is made. |
| 2781 | ** |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 2782 | ** If P3 is not NULL, and keys exist in the index of which the stack key |
| 2783 | ** is a prefix, leave the cursor pointing at the largest of these. |
| 2784 | ** This is the same effect that executing OP_IncrKey on the key value |
| 2785 | ** before OP_MoveLt used to have. |
| 2786 | ** |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 2787 | ** See also: MoveTo |
| 2788 | */ |
| 2789 | case OP_MoveLt: |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2790 | case OP_MoveTo: { |
| 2791 | int i = pOp->p1; |
drh | 80ff32f | 2001-11-04 18:32:46 +0000 | [diff] [blame] | 2792 | Cursor *pC; |
| 2793 | |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 2794 | assert( pTos>=p->aStack ); |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 2795 | assert( i>=0 && i<p->nCursor ); |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame^] | 2796 | pC = p->apCsr[i]; |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 2797 | if( pC->pCursor!=0 ){ |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 2798 | int res, oc; |
drh | a11846b | 2004-01-07 18:52:56 +0000 | [diff] [blame] | 2799 | pC->nullRow = 0; |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 2800 | if( pC->intKey ){ |
| 2801 | i64 iKey; |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 2802 | assert( !pOp->p3 ); |
danielk1977 | 96fc5fe | 2004-05-13 11:34:16 +0000 | [diff] [blame] | 2803 | Integerify(pTos); |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 2804 | iKey = intToKey(pTos->i); |
drh | a11846b | 2004-01-07 18:52:56 +0000 | [diff] [blame] | 2805 | if( pOp->p2==0 && pOp->opcode==OP_MoveTo ){ |
| 2806 | pC->movetoTarget = iKey; |
| 2807 | pC->deferredMoveto = 1; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 2808 | Release(pTos); |
| 2809 | pTos--; |
drh | a11846b | 2004-01-07 18:52:56 +0000 | [diff] [blame] | 2810 | break; |
| 2811 | } |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 2812 | sqlite3BtreeMoveto(pC->pCursor, 0, (u64)iKey, &res); |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 2813 | pC->lastRecno = pTos->i; |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 2814 | pC->recnoIsValid = res==0; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2815 | }else{ |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 2816 | if( pOp->p3 ){ |
| 2817 | pC->incrKey = 1; |
| 2818 | } |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 2819 | Stringify(pTos); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2820 | sqlite3BtreeMoveto(pC->pCursor, pTos->z, pTos->n, &res); |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 2821 | pC->incrKey = 0; |
drh | 80ff32f | 2001-11-04 18:32:46 +0000 | [diff] [blame] | 2822 | pC->recnoIsValid = 0; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2823 | } |
drh | a11846b | 2004-01-07 18:52:56 +0000 | [diff] [blame] | 2824 | pC->deferredMoveto = 0; |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2825 | pC->cacheValid = 0; |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 2826 | pC->incrKey = 0; |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 2827 | sqlite3_search_count++; |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 2828 | oc = pOp->opcode; |
| 2829 | if( oc==OP_MoveTo && res<0 ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2830 | sqlite3BtreeNext(pC->pCursor, &res); |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 2831 | pC->recnoIsValid = 0; |
| 2832 | if( res && pOp->p2>0 ){ |
| 2833 | pc = pOp->p2 - 1; |
| 2834 | } |
drh | 1a844c3 | 2002-12-04 22:29:28 +0000 | [diff] [blame] | 2835 | }else if( oc==OP_MoveLt ){ |
| 2836 | if( res>=0 ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2837 | sqlite3BtreePrevious(pC->pCursor, &res); |
drh | 1a844c3 | 2002-12-04 22:29:28 +0000 | [diff] [blame] | 2838 | pC->recnoIsValid = 0; |
| 2839 | }else{ |
| 2840 | /* res might be negative because the table is empty. Check to |
| 2841 | ** see if this is the case. |
| 2842 | */ |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 2843 | res = sqlite3BtreeEof(pC->pCursor); |
drh | 1a844c3 | 2002-12-04 22:29:28 +0000 | [diff] [blame] | 2844 | } |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 2845 | if( res && pOp->p2>0 ){ |
| 2846 | pc = pOp->p2 - 1; |
| 2847 | } |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 2848 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2849 | } |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 2850 | Release(pTos); |
| 2851 | pTos--; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2852 | break; |
| 2853 | } |
| 2854 | |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2855 | /* Opcode: Distinct P1 P2 * |
| 2856 | ** |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 2857 | ** Use the top of the stack as a string key. If a record with that key does |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2858 | ** 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] | 2859 | ** does already exist, then fall thru. The cursor is left pointing |
| 2860 | ** 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] | 2861 | ** |
| 2862 | ** This operation is similar to NotFound except that this operation |
| 2863 | ** does not pop the key from the stack. |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2864 | ** |
drh | d99f706 | 2002-06-08 23:25:08 +0000 | [diff] [blame] | 2865 | ** See also: Found, NotFound, MoveTo, IsUnique, NotExists |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2866 | */ |
| 2867 | /* Opcode: Found P1 P2 * |
| 2868 | ** |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 2869 | ** Use the top of the stack as a string key. If a record with that key |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2870 | ** does exist in table of P1, then jump to P2. If the record |
drh | 3fc190c | 2001-09-14 03:24:23 +0000 | [diff] [blame] | 2871 | ** does not exist, then fall thru. The cursor is left pointing |
| 2872 | ** to the record if it exists. The key is popped from the stack. |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2873 | ** |
drh | d99f706 | 2002-06-08 23:25:08 +0000 | [diff] [blame] | 2874 | ** See also: Distinct, NotFound, MoveTo, IsUnique, NotExists |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2875 | */ |
| 2876 | /* Opcode: NotFound P1 P2 * |
| 2877 | ** |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 2878 | ** Use the top of the stack as a string key. If a record with that key |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2879 | ** 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] | 2880 | ** does exist, then fall thru. The cursor is left pointing to the |
| 2881 | ** record if it exists. The key is popped from the stack. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2882 | ** |
| 2883 | ** The difference between this operation and Distinct is that |
| 2884 | ** Distinct does not pop the key from the stack. |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2885 | ** |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 2886 | ** See also: Distinct, Found, MoveTo, NotExists, IsUnique |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2887 | */ |
| 2888 | case OP_Distinct: |
| 2889 | case OP_NotFound: |
| 2890 | case OP_Found: { |
| 2891 | int i = pOp->p1; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2892 | int alreadyExists = 0; |
drh | 80ff32f | 2001-11-04 18:32:46 +0000 | [diff] [blame] | 2893 | Cursor *pC; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 2894 | assert( pTos>=p->aStack ); |
| 2895 | assert( i>=0 && i<p->nCursor ); |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame^] | 2896 | if( (pC = p->apCsr[i])->pCursor!=0 ){ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2897 | int res, rx; |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 2898 | assert( pC->intKey==0 ); |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 2899 | Stringify(pTos); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2900 | rx = sqlite3BtreeMoveto(pC->pCursor, pTos->z, pTos->n, &res); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2901 | alreadyExists = rx==SQLITE_OK && res==0; |
drh | a11846b | 2004-01-07 18:52:56 +0000 | [diff] [blame] | 2902 | pC->deferredMoveto = 0; |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2903 | pC->cacheValid = 0; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2904 | } |
| 2905 | if( pOp->opcode==OP_Found ){ |
| 2906 | if( alreadyExists ) pc = pOp->p2 - 1; |
| 2907 | }else{ |
| 2908 | if( !alreadyExists ) pc = pOp->p2 - 1; |
| 2909 | } |
| 2910 | if( pOp->opcode!=OP_Distinct ){ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 2911 | Release(pTos); |
| 2912 | pTos--; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2913 | } |
| 2914 | break; |
| 2915 | } |
| 2916 | |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 2917 | /* Opcode: IsUnique P1 P2 * |
| 2918 | ** |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 2919 | ** The top of the stack is an integer record number. Call this |
| 2920 | ** record number R. The next on the stack is an index key created |
| 2921 | ** using MakeIdxKey. Call it K. This instruction pops R from the |
| 2922 | ** stack but it leaves K unchanged. |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 2923 | ** |
drh | a3b321d | 2004-05-11 09:31:31 +0000 | [diff] [blame] | 2924 | ** P1 is an index. So all but the last four bytes of K are an |
| 2925 | ** index string. The last four bytes of K are a record number. |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 2926 | ** |
| 2927 | ** This instruction asks if there is an entry in P1 where the |
| 2928 | ** index string matches K but the record number is different |
| 2929 | ** from R. If there is no such entry, then there is an immediate |
| 2930 | ** jump to P2. If any entry does exist where the index string |
| 2931 | ** matches K but the record number is not R, then the record |
| 2932 | ** number for that entry is pushed onto the stack and control |
| 2933 | ** falls through to the next instruction. |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 2934 | ** |
drh | d99f706 | 2002-06-08 23:25:08 +0000 | [diff] [blame] | 2935 | ** See also: Distinct, NotFound, NotExists, Found |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 2936 | */ |
| 2937 | case OP_IsUnique: { |
| 2938 | int i = pOp->p1; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 2939 | Mem *pNos = &pTos[-1]; |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 2940 | Cursor *pCx; |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 2941 | BtCursor *pCrsr; |
danielk1977 | 452c989 | 2004-05-13 05:16:15 +0000 | [diff] [blame] | 2942 | i64 R; |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 2943 | |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 2944 | /* Pop the value R off the top of the stack |
| 2945 | */ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 2946 | assert( pNos>=p->aStack ); |
| 2947 | Integerify(pTos); |
| 2948 | R = pTos->i; |
| 2949 | pTos--; |
| 2950 | assert( i>=0 && i<=p->nCursor ); |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame^] | 2951 | pCx = p->apCsr[i]; |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 2952 | pCrsr = pCx->pCursor; |
| 2953 | if( pCrsr!=0 ){ |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 2954 | int res, rc; |
danielk1977 | 452c989 | 2004-05-13 05:16:15 +0000 | [diff] [blame] | 2955 | i64 v; /* The record number on the P1 entry that matches K */ |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 2956 | char *zKey; /* The value of K */ |
| 2957 | int nKey; /* Number of bytes in K */ |
danielk1977 | 452c989 | 2004-05-13 05:16:15 +0000 | [diff] [blame] | 2958 | int len; /* Number of bytes in K without the rowid at the end */ |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 2959 | |
| 2960 | /* Make sure K is a string and make zKey point to K |
| 2961 | */ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 2962 | Stringify(pNos); |
| 2963 | zKey = pNos->z; |
| 2964 | nKey = pNos->n; |
danielk1977 | 452c989 | 2004-05-13 05:16:15 +0000 | [diff] [blame] | 2965 | |
| 2966 | assert( nKey >= 2 ); |
| 2967 | len = nKey-2; |
| 2968 | while( zKey[len] && --len ); |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 2969 | |
drh | a3b321d | 2004-05-11 09:31:31 +0000 | [diff] [blame] | 2970 | /* Search for an entry in P1 where all but the last four bytes match K. |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 2971 | ** If there is no such entry, jump immediately to P2. |
| 2972 | */ |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2973 | assert( pCx->deferredMoveto==0 ); |
| 2974 | pCx->cacheValid = 0; |
danielk1977 | 452c989 | 2004-05-13 05:16:15 +0000 | [diff] [blame] | 2975 | rc = sqlite3BtreeMoveto(pCrsr, zKey, len, &res); |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 2976 | if( rc!=SQLITE_OK ) goto abort_due_to_error; |
| 2977 | if( res<0 ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2978 | rc = sqlite3BtreeNext(pCrsr, &res); |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 2979 | if( res ){ |
| 2980 | pc = pOp->p2 - 1; |
| 2981 | break; |
| 2982 | } |
| 2983 | } |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 2984 | rc = sqlite3VdbeIdxKeyCompare(pCx, len, zKey, 0, &res); |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 2985 | if( rc!=SQLITE_OK ) goto abort_due_to_error; |
| 2986 | if( res>0 ){ |
| 2987 | pc = pOp->p2 - 1; |
| 2988 | break; |
| 2989 | } |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 2990 | |
| 2991 | /* At this point, pCrsr is pointing to an entry in P1 where all but |
danielk1977 | 452c989 | 2004-05-13 05:16:15 +0000 | [diff] [blame] | 2992 | ** the final varint (the rowid) matches K. Check to see if the |
| 2993 | ** final varint is different from R. If it equals R then jump |
| 2994 | ** immediately to P2. |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 2995 | */ |
danielk1977 | 452c989 | 2004-05-13 05:16:15 +0000 | [diff] [blame] | 2996 | rc = sqlite3VdbeIdxRowid(pCrsr, &v); |
| 2997 | if( rc!=SQLITE_OK ){ |
| 2998 | goto abort_due_to_error; |
| 2999 | } |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 3000 | if( v==R ){ |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 3001 | pc = pOp->p2 - 1; |
| 3002 | break; |
| 3003 | } |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 3004 | |
danielk1977 | 452c989 | 2004-05-13 05:16:15 +0000 | [diff] [blame] | 3005 | /* The final varint of the key is different from R. Push it onto |
| 3006 | ** the stack. (The record number of an entry that violates a UNIQUE |
| 3007 | ** constraint.) |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 3008 | */ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3009 | pTos++; |
| 3010 | pTos->i = v; |
| 3011 | pTos->flags = MEM_Int; |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 3012 | } |
| 3013 | break; |
| 3014 | } |
| 3015 | |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 3016 | /* Opcode: NotExists P1 P2 * |
| 3017 | ** |
| 3018 | ** Use the top of the stack as a integer key. If a record with that key |
| 3019 | ** does not exist in table of P1, then jump to P2. If the record |
| 3020 | ** does exist, then fall thru. The cursor is left pointing to the |
| 3021 | ** record if it exists. The integer key is popped from the stack. |
| 3022 | ** |
| 3023 | ** The difference between this operation and NotFound is that this |
| 3024 | ** operation assumes the key is an integer and NotFound assumes it |
| 3025 | ** is a string. |
| 3026 | ** |
drh | d99f706 | 2002-06-08 23:25:08 +0000 | [diff] [blame] | 3027 | ** See also: Distinct, Found, MoveTo, NotFound, IsUnique |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 3028 | */ |
| 3029 | case OP_NotExists: { |
| 3030 | int i = pOp->p1; |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 3031 | Cursor *pC; |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 3032 | BtCursor *pCrsr; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3033 | assert( pTos>=p->aStack ); |
| 3034 | assert( i>=0 && i<p->nCursor ); |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame^] | 3035 | if( (pCrsr = (pC = p->apCsr[i])->pCursor)!=0 ){ |
danielk1977 | 36a3c70 | 2004-05-11 06:55:14 +0000 | [diff] [blame] | 3036 | int res, rx; |
| 3037 | u64 iKey; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3038 | assert( pTos->flags & MEM_Int ); |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame^] | 3039 | assert( p->apCsr[i]->intKey ); |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3040 | iKey = intToKey(pTos->i); |
danielk1977 | 36a3c70 | 2004-05-11 06:55:14 +0000 | [diff] [blame] | 3041 | rx = sqlite3BtreeMoveto(pCrsr, 0, iKey, &res); |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 3042 | pC->lastRecno = pTos->i; |
| 3043 | pC->recnoIsValid = res==0; |
| 3044 | pC->nullRow = 0; |
| 3045 | pC->cacheValid = 0; |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 3046 | if( rx!=SQLITE_OK || res!=0 ){ |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 3047 | pc = pOp->p2 - 1; |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 3048 | pC->recnoIsValid = 0; |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 3049 | } |
| 3050 | } |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3051 | Release(pTos); |
| 3052 | pTos--; |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 3053 | break; |
| 3054 | } |
| 3055 | |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3056 | /* Opcode: NewRecno P1 * * |
| 3057 | ** |
| 3058 | ** Get a new integer record number used as the key to a table. |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 3059 | ** The record number is not previously used as a key in the database |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 3060 | ** table that cursor P1 points to. The new record number is pushed |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3061 | ** onto the stack. |
| 3062 | */ |
| 3063 | case OP_NewRecno: { |
| 3064 | int i = pOp->p1; |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 3065 | i64 v = 0; |
drh | 80ff32f | 2001-11-04 18:32:46 +0000 | [diff] [blame] | 3066 | Cursor *pC; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3067 | assert( i>=0 && i<p->nCursor ); |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame^] | 3068 | if( (pC = p->apCsr[i])->pCursor==0 ){ |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 3069 | /* The zero initialization above is all that is needed */ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3070 | }else{ |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 3071 | /* The next rowid or record number (different terms for the same |
| 3072 | ** thing) is obtained in a two-step algorithm. |
| 3073 | ** |
| 3074 | ** First we attempt to find the largest existing rowid and add one |
| 3075 | ** to that. But if the largest existing rowid is already the maximum |
| 3076 | ** positive integer, we have to fall through to the second |
| 3077 | ** probabilistic algorithm |
| 3078 | ** |
| 3079 | ** The second algorithm is to select a rowid at random and see if |
| 3080 | ** it already exists in the table. If it does not exist, we have |
| 3081 | ** succeeded. If the random rowid does exist, we select a new one |
| 3082 | ** and try again, up to 1000 times. |
drh | db5ed6d | 2001-09-18 22:17:44 +0000 | [diff] [blame] | 3083 | ** |
| 3084 | ** For a table with less than 2 billion entries, the probability |
| 3085 | ** of not finding a unused rowid is about 1.0e-300. This is a |
| 3086 | ** non-zero probability, but it is still vanishingly small and should |
| 3087 | ** never cause a problem. You are much, much more likely to have a |
| 3088 | ** hardware failure than for this algorithm to fail. |
| 3089 | ** |
drh | af9ff33 | 2002-01-16 21:00:27 +0000 | [diff] [blame] | 3090 | ** The analysis in the previous paragraph assumes that you have a good |
| 3091 | ** source of random numbers. Is a library function like lrand48() |
| 3092 | ** good enough? Maybe. Maybe not. It's hard to know whether there |
| 3093 | ** might be subtle bugs is some implementations of lrand48() that |
| 3094 | ** could cause problems. To avoid uncertainty, SQLite uses its own |
| 3095 | ** random number generator based on the RC4 algorithm. |
| 3096 | ** |
drh | db5ed6d | 2001-09-18 22:17:44 +0000 | [diff] [blame] | 3097 | ** To promote locality of reference for repetitive inserts, the |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 3098 | ** first few attempts at chosing a random rowid pick values just a little |
drh | db5ed6d | 2001-09-18 22:17:44 +0000 | [diff] [blame] | 3099 | ** larger than the previous rowid. This has been shown experimentally |
| 3100 | ** to double the speed of the COPY operation. |
| 3101 | */ |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 3102 | int res, rx, cnt; |
| 3103 | i64 x; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3104 | cnt = 0; |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 3105 | assert( (sqlite3BtreeFlags(pC->pCursor) & BTREE_INTKEY)!=0 ); |
| 3106 | assert( (sqlite3BtreeFlags(pC->pCursor) & BTREE_ZERODATA)==0 ); |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 3107 | if( !pC->useRandomRowid ){ |
drh | 32fbe34 | 2002-10-19 20:16:37 +0000 | [diff] [blame] | 3108 | if( pC->nextRowidValid ){ |
| 3109 | v = pC->nextRowid; |
drh | 3fc190c | 2001-09-14 03:24:23 +0000 | [diff] [blame] | 3110 | }else{ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3111 | rx = sqlite3BtreeLast(pC->pCursor, &res); |
drh | 32fbe34 | 2002-10-19 20:16:37 +0000 | [diff] [blame] | 3112 | if( res ){ |
| 3113 | v = 1; |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 3114 | }else{ |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 3115 | sqlite3BtreeKeySize(pC->pCursor, (u64*)&v); |
drh | 32fbe34 | 2002-10-19 20:16:37 +0000 | [diff] [blame] | 3116 | v = keyToInt(v); |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 3117 | if( v==0x7fffffffffffffff ){ |
drh | 32fbe34 | 2002-10-19 20:16:37 +0000 | [diff] [blame] | 3118 | pC->useRandomRowid = 1; |
| 3119 | }else{ |
| 3120 | v++; |
| 3121 | } |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 3122 | } |
drh | 3fc190c | 2001-09-14 03:24:23 +0000 | [diff] [blame] | 3123 | } |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 3124 | if( v<0x7fffffffffffffff ){ |
drh | 32fbe34 | 2002-10-19 20:16:37 +0000 | [diff] [blame] | 3125 | pC->nextRowidValid = 1; |
| 3126 | pC->nextRowid = v+1; |
| 3127 | }else{ |
| 3128 | pC->nextRowidValid = 0; |
| 3129 | } |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 3130 | } |
| 3131 | if( pC->useRandomRowid ){ |
| 3132 | v = db->priorNewRowid; |
| 3133 | cnt = 0; |
| 3134 | do{ |
| 3135 | if( v==0 || cnt>2 ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3136 | sqlite3Randomness(sizeof(v), &v); |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 3137 | if( cnt<5 ) v &= 0xffffff; |
| 3138 | }else{ |
drh | bbd82df | 2004-02-11 09:46:30 +0000 | [diff] [blame] | 3139 | unsigned char r; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3140 | sqlite3Randomness(1, &r); |
drh | bbd82df | 2004-02-11 09:46:30 +0000 | [diff] [blame] | 3141 | v += r + 1; |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 3142 | } |
| 3143 | if( v==0 ) continue; |
| 3144 | x = intToKey(v); |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 3145 | rx = sqlite3BtreeMoveto(pC->pCursor, 0, (u64)x, &res); |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 3146 | cnt++; |
| 3147 | }while( cnt<1000 && rx==SQLITE_OK && res==0 ); |
| 3148 | db->priorNewRowid = v; |
| 3149 | if( rx==SQLITE_OK && res==0 ){ |
| 3150 | rc = SQLITE_FULL; |
| 3151 | goto abort_due_to_error; |
| 3152 | } |
drh | 1eaa269 | 2001-09-18 02:02:23 +0000 | [diff] [blame] | 3153 | } |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 3154 | pC->recnoIsValid = 0; |
drh | a11846b | 2004-01-07 18:52:56 +0000 | [diff] [blame] | 3155 | pC->deferredMoveto = 0; |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 3156 | pC->cacheValid = 0; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3157 | } |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3158 | pTos++; |
| 3159 | pTos->i = v; |
| 3160 | pTos->flags = MEM_Int; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3161 | break; |
| 3162 | } |
| 3163 | |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 3164 | /* Opcode: PutIntKey P1 P2 * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3165 | ** |
jplyon | 5a56422 | 2003-06-02 06:15:58 +0000 | [diff] [blame] | 3166 | ** Write an entry into the table of cursor P1. A new entry is |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 3167 | ** created if it doesn't already exist or the data for an existing |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3168 | ** entry is overwritten. The data is the value on the top of the |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 3169 | ** stack. The key is the next value down on the stack. The key must |
| 3170 | ** be an integer. The stack is popped twice by this instruction. |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 3171 | ** |
rdc | b0c374f | 2004-02-20 22:53:38 +0000 | [diff] [blame] | 3172 | ** If the OPFLAG_NCHANGE flag of P2 is set, then the row change count is |
| 3173 | ** incremented (otherwise not). If the OPFLAG_CSCHANGE flag is set, |
| 3174 | ** then the current statement change count is incremented (otherwise not). |
| 3175 | ** If the OPFLAG_LASTROWID flag of P2 is set, then rowid is |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 3176 | ** stored for subsequent return by the sqlite3_last_insert_rowid() function |
rdc | b0c374f | 2004-02-20 22:53:38 +0000 | [diff] [blame] | 3177 | ** (otherwise it's unmodified). |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3178 | */ |
drh | c8d30ac | 2002-04-12 10:08:59 +0000 | [diff] [blame] | 3179 | /* Opcode: PutStrKey P1 * * |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 3180 | ** |
jplyon | 5a56422 | 2003-06-02 06:15:58 +0000 | [diff] [blame] | 3181 | ** Write an entry into the table of cursor P1. A new entry is |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 3182 | ** created if it doesn't already exist or the data for an existing |
| 3183 | ** entry is overwritten. The data is the value on the top of the |
| 3184 | ** stack. The key is the next value down on the stack. The key must |
| 3185 | ** be a string. The stack is popped twice by this instruction. |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3186 | ** |
| 3187 | ** P1 may not be a pseudo-table opened using the OpenPseudo opcode. |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 3188 | */ |
| 3189 | case OP_PutIntKey: |
| 3190 | case OP_PutStrKey: { |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3191 | Mem *pNos = &pTos[-1]; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3192 | int i = pOp->p1; |
drh | 32fbe34 | 2002-10-19 20:16:37 +0000 | [diff] [blame] | 3193 | Cursor *pC; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3194 | assert( pNos>=p->aStack ); |
| 3195 | assert( i>=0 && i<p->nCursor ); |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame^] | 3196 | if( ((pC = p->apCsr[i])->pCursor!=0 || pC->pseudoTable) ){ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3197 | char *zKey; |
danielk1977 | 5f8d8a8 | 2004-05-11 00:28:42 +0000 | [diff] [blame] | 3198 | i64 nKey; |
danielk1977 | e7c8d58 | 2004-05-13 13:38:52 +0000 | [diff] [blame] | 3199 | i64 iKey; |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 3200 | if( pOp->opcode==OP_PutStrKey ){ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3201 | Stringify(pNos); |
| 3202 | nKey = pNos->n; |
| 3203 | zKey = pNos->z; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3204 | }else{ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3205 | assert( pNos->flags & MEM_Int ); |
danielk1977 | 5f8d8a8 | 2004-05-11 00:28:42 +0000 | [diff] [blame] | 3206 | |
| 3207 | /* If the table is an INTKEY table, set nKey to the value of |
danielk1977 | 0dbe72b | 2004-05-11 04:54:49 +0000 | [diff] [blame] | 3208 | ** the integer key, and zKey to NULL. Otherwise, set nKey to |
| 3209 | ** sizeof(i64) and point zKey at iKey. iKey contains the integer |
| 3210 | ** key in the on-disk byte order. |
danielk1977 | 5f8d8a8 | 2004-05-11 00:28:42 +0000 | [diff] [blame] | 3211 | */ |
danielk1977 | 0dbe72b | 2004-05-11 04:54:49 +0000 | [diff] [blame] | 3212 | iKey = intToKey(pNos->i); |
danielk1977 | 5f8d8a8 | 2004-05-11 00:28:42 +0000 | [diff] [blame] | 3213 | if( pC->intKey ){ |
danielk1977 | 49f737d | 2004-05-11 02:10:06 +0000 | [diff] [blame] | 3214 | nKey = intToKey(pNos->i); |
danielk1977 | 5f8d8a8 | 2004-05-11 00:28:42 +0000 | [diff] [blame] | 3215 | zKey = 0; |
| 3216 | }else{ |
danielk1977 | 5f8d8a8 | 2004-05-11 00:28:42 +0000 | [diff] [blame] | 3217 | nKey = sizeof(i64); |
| 3218 | zKey = (char*)&iKey; |
| 3219 | } |
danielk1977 | 5f8d8a8 | 2004-05-11 00:28:42 +0000 | [diff] [blame] | 3220 | |
rdc | b0c374f | 2004-02-20 22:53:38 +0000 | [diff] [blame] | 3221 | if( pOp->p2 & OPFLAG_NCHANGE ) db->nChange++; |
| 3222 | if( pOp->p2 & OPFLAG_LASTROWID ) db->lastRowid = pNos->i; |
| 3223 | if( pOp->p2 & OPFLAG_CSCHANGE ) db->csChange++; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3224 | if( pC->nextRowidValid && pTos->i>=pC->nextRowid ){ |
drh | 32fbe34 | 2002-10-19 20:16:37 +0000 | [diff] [blame] | 3225 | pC->nextRowidValid = 0; |
| 3226 | } |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 3227 | } |
drh | 78a7583 | 2004-02-13 14:07:12 +0000 | [diff] [blame] | 3228 | if( pTos->flags & MEM_Null ){ |
| 3229 | pTos->z = 0; |
| 3230 | pTos->n = 0; |
| 3231 | }else{ |
| 3232 | assert( pTos->flags & MEM_Str ); |
| 3233 | } |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3234 | if( pC->pseudoTable ){ |
| 3235 | /* PutStrKey does not work for pseudo-tables. |
| 3236 | ** The following assert makes sure we are not trying to use |
| 3237 | ** PutStrKey on a pseudo-table |
| 3238 | */ |
| 3239 | assert( pOp->opcode==OP_PutIntKey ); |
| 3240 | sqliteFree(pC->pData); |
| 3241 | pC->iKey = iKey; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3242 | pC->nData = pTos->n; |
| 3243 | if( pTos->flags & MEM_Dyn ){ |
| 3244 | pC->pData = pTos->z; |
| 3245 | pTos->flags = MEM_Null; |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3246 | }else{ |
| 3247 | pC->pData = sqliteMallocRaw( pC->nData ); |
| 3248 | if( pC->pData ){ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3249 | memcpy(pC->pData, pTos->z, pC->nData); |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3250 | } |
| 3251 | } |
| 3252 | pC->nullRow = 0; |
| 3253 | }else{ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3254 | rc = sqlite3BtreeInsert(pC->pCursor, zKey, nKey, pTos->z, pTos->n); |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3255 | } |
drh | 32fbe34 | 2002-10-19 20:16:37 +0000 | [diff] [blame] | 3256 | pC->recnoIsValid = 0; |
drh | a11846b | 2004-01-07 18:52:56 +0000 | [diff] [blame] | 3257 | pC->deferredMoveto = 0; |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 3258 | pC->cacheValid = 0; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3259 | } |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3260 | popStack(&pTos, 2); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3261 | break; |
| 3262 | } |
| 3263 | |
drh | c8d30ac | 2002-04-12 10:08:59 +0000 | [diff] [blame] | 3264 | /* Opcode: Delete P1 P2 * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3265 | ** |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 3266 | ** Delete the record at which the P1 cursor is currently pointing. |
| 3267 | ** |
| 3268 | ** The cursor will be left pointing at either the next or the previous |
| 3269 | ** 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] | 3270 | ** the next Next instruction will be a no-op. Hence it is OK to delete |
| 3271 | ** a record from within an Next loop. |
drh | c8d30ac | 2002-04-12 10:08:59 +0000 | [diff] [blame] | 3272 | ** |
rdc | b0c374f | 2004-02-20 22:53:38 +0000 | [diff] [blame] | 3273 | ** If the OPFLAG_NCHANGE flag of P2 is set, then the row change count is |
| 3274 | ** incremented (otherwise not). If OPFLAG_CSCHANGE flag is set, |
| 3275 | ** then the current statement change count is incremented (otherwise not). |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3276 | ** |
| 3277 | ** If P1 is a pseudo-table, then this instruction is a no-op. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3278 | */ |
| 3279 | case OP_Delete: { |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3280 | int i = pOp->p1; |
drh | 32fbe34 | 2002-10-19 20:16:37 +0000 | [diff] [blame] | 3281 | Cursor *pC; |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3282 | assert( i>=0 && i<p->nCursor ); |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame^] | 3283 | pC = p->apCsr[i]; |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3284 | if( pC->pCursor!=0 ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3285 | sqlite3VdbeCursorMoveto(pC); |
| 3286 | rc = sqlite3BtreeDelete(pC->pCursor); |
drh | 32fbe34 | 2002-10-19 20:16:37 +0000 | [diff] [blame] | 3287 | pC->nextRowidValid = 0; |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 3288 | pC->cacheValid = 0; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3289 | } |
rdc | b0c374f | 2004-02-20 22:53:38 +0000 | [diff] [blame] | 3290 | if( pOp->p2 & OPFLAG_NCHANGE ) db->nChange++; |
| 3291 | if( pOp->p2 & OPFLAG_CSCHANGE ) db->csChange++; |
| 3292 | break; |
| 3293 | } |
| 3294 | |
| 3295 | /* Opcode: SetCounts * * * |
| 3296 | ** |
| 3297 | ** Called at end of statement. Updates lsChange (last statement change count) |
| 3298 | ** and resets csChange (current statement change count) to 0. |
| 3299 | */ |
| 3300 | case OP_SetCounts: { |
| 3301 | db->lsChange=db->csChange; |
| 3302 | db->csChange=0; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3303 | break; |
| 3304 | } |
| 3305 | |
| 3306 | /* Opcode: KeyAsData P1 P2 * |
| 3307 | ** |
| 3308 | ** Turn the key-as-data mode for cursor P1 either on (if P2==1) or |
drh | 5fe2d8c | 2003-05-10 03:36:53 +0000 | [diff] [blame] | 3309 | ** off (if P2==0). In key-as-data mode, the OP_Column opcode pulls |
| 3310 | ** data off of the key rather than the data. This is used for |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3311 | ** processing compound selects. |
| 3312 | */ |
| 3313 | case OP_KeyAsData: { |
| 3314 | int i = pOp->p1; |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3315 | assert( i>=0 && i<p->nCursor ); |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame^] | 3316 | p->apCsr[i]->keyAsData = pOp->p2; |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3317 | break; |
| 3318 | } |
| 3319 | |
| 3320 | /* Opcode: RowData P1 * * |
| 3321 | ** |
| 3322 | ** Push onto the stack the complete row data for cursor P1. |
| 3323 | ** There is no interpretation of the data. It is just copied |
| 3324 | ** onto the stack exactly as it is found in the database file. |
| 3325 | ** |
| 3326 | ** If the cursor is not pointing to a valid row, a NULL is pushed |
| 3327 | ** onto the stack. |
| 3328 | */ |
drh | 143f3c4 | 2004-01-07 20:37:52 +0000 | [diff] [blame] | 3329 | /* Opcode: RowKey P1 * * |
| 3330 | ** |
| 3331 | ** Push onto the stack the complete row key for cursor P1. |
| 3332 | ** There is no interpretation of the key. It is just copied |
| 3333 | ** onto the stack exactly as it is found in the database file. |
| 3334 | ** |
| 3335 | ** If the cursor is not pointing to a valid row, a NULL is pushed |
| 3336 | ** onto the stack. |
| 3337 | */ |
| 3338 | case OP_RowKey: |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3339 | case OP_RowData: { |
| 3340 | int i = pOp->p1; |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3341 | Cursor *pC; |
| 3342 | int n; |
| 3343 | |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3344 | pTos++; |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3345 | assert( i>=0 && i<p->nCursor ); |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame^] | 3346 | pC = p->apCsr[i]; |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3347 | if( pC->nullRow ){ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3348 | pTos->flags = MEM_Null; |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3349 | }else if( pC->pCursor!=0 ){ |
| 3350 | BtCursor *pCrsr = pC->pCursor; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3351 | sqlite3VdbeCursorMoveto(pC); |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3352 | if( pC->nullRow ){ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3353 | pTos->flags = MEM_Null; |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3354 | break; |
drh | 143f3c4 | 2004-01-07 20:37:52 +0000 | [diff] [blame] | 3355 | }else if( pC->keyAsData || pOp->opcode==OP_RowKey ){ |
danielk1977 | 6490beb | 2004-05-11 06:17:21 +0000 | [diff] [blame] | 3356 | i64 n64; |
| 3357 | assert( !pC->intKey ); |
| 3358 | sqlite3BtreeKeySize(pCrsr, &n64); |
| 3359 | n = n64; |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3360 | }else{ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3361 | sqlite3BtreeDataSize(pCrsr, &n); |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3362 | } |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3363 | pTos->n = n; |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3364 | if( n<=NBFS ){ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3365 | pTos->flags = MEM_Str | MEM_Short; |
| 3366 | pTos->z = pTos->zShort; |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3367 | }else{ |
| 3368 | char *z = sqliteMallocRaw( n ); |
| 3369 | if( z==0 ) goto no_mem; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3370 | pTos->flags = MEM_Str | MEM_Dyn; |
| 3371 | pTos->z = z; |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3372 | } |
drh | 143f3c4 | 2004-01-07 20:37:52 +0000 | [diff] [blame] | 3373 | if( pC->keyAsData || pOp->opcode==OP_RowKey ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3374 | sqlite3BtreeKey(pCrsr, 0, n, pTos->z); |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3375 | }else{ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3376 | sqlite3BtreeData(pCrsr, 0, n, pTos->z); |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3377 | } |
| 3378 | }else if( pC->pseudoTable ){ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3379 | pTos->n = pC->nData; |
| 3380 | pTos->z = pC->pData; |
| 3381 | pTos->flags = MEM_Str|MEM_Ephem; |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3382 | }else{ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3383 | pTos->flags = MEM_Null; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3384 | } |
| 3385 | break; |
| 3386 | } |
| 3387 | |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3388 | /* Opcode: Recno P1 * * |
| 3389 | ** |
| 3390 | ** Push onto the stack an integer which is the first 4 bytes of the |
| 3391 | ** the key to the current entry in a sequential scan of the database |
| 3392 | ** file P1. The sequential scan should have been started using the |
| 3393 | ** Next opcode. |
| 3394 | */ |
| 3395 | case OP_Recno: { |
| 3396 | int i = pOp->p1; |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3397 | Cursor *pC; |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 3398 | i64 v; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3399 | |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3400 | assert( i>=0 && i<p->nCursor ); |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame^] | 3401 | pC = p->apCsr[i]; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3402 | sqlite3VdbeCursorMoveto(pC); |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3403 | pTos++; |
drh | a11846b | 2004-01-07 18:52:56 +0000 | [diff] [blame] | 3404 | if( pC->recnoIsValid ){ |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3405 | v = pC->lastRecno; |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3406 | }else if( pC->pseudoTable ){ |
| 3407 | v = keyToInt(pC->iKey); |
drh | d60ccc6 | 2003-06-24 10:39:46 +0000 | [diff] [blame] | 3408 | }else if( pC->nullRow || pC->pCursor==0 ){ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3409 | pTos->flags = MEM_Null; |
drh | d60ccc6 | 2003-06-24 10:39:46 +0000 | [diff] [blame] | 3410 | break; |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3411 | }else{ |
| 3412 | assert( pC->pCursor!=0 ); |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 3413 | sqlite3BtreeKeySize(pC->pCursor, (u64*)&v); |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3414 | v = keyToInt(v); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3415 | } |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3416 | pTos->i = v; |
| 3417 | pTos->flags = MEM_Int; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3418 | break; |
| 3419 | } |
| 3420 | |
| 3421 | /* Opcode: FullKey P1 * * |
| 3422 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 3423 | ** Extract the complete key from the record that cursor P1 is currently |
| 3424 | ** pointing to and push the key onto the stack as a string. |
| 3425 | ** |
| 3426 | ** Compare this opcode to Recno. The Recno opcode extracts the first |
| 3427 | ** 4 bytes of the key and pushes those bytes onto the stack as an |
| 3428 | ** integer. This instruction pushes the entire key as a string. |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3429 | ** |
| 3430 | ** This opcode may not be used on a pseudo-table. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3431 | */ |
| 3432 | case OP_FullKey: { |
| 3433 | int i = pOp->p1; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3434 | BtCursor *pCrsr; |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame^] | 3435 | Cursor *pC; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3436 | |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame^] | 3437 | assert( p->apCsr[i]->keyAsData ); |
| 3438 | assert( !p->apCsr[i]->pseudoTable ); |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3439 | assert( i>=0 && i<p->nCursor ); |
| 3440 | pTos++; |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame^] | 3441 | if( (pCrsr = (pC = p->apCsr[i])->pCursor)!=0 ){ |
danielk1977 | 36a3c70 | 2004-05-11 06:55:14 +0000 | [diff] [blame] | 3442 | u64 amt; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3443 | char *z; |
| 3444 | |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame^] | 3445 | sqlite3VdbeCursorMoveto(pC); |
| 3446 | assert( pC->intKey==0 ); |
danielk1977 | 36a3c70 | 2004-05-11 06:55:14 +0000 | [diff] [blame] | 3447 | sqlite3BtreeKeySize(pCrsr, &amt); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3448 | if( amt<=0 ){ |
| 3449 | rc = SQLITE_CORRUPT; |
| 3450 | goto abort_due_to_error; |
| 3451 | } |
drh | 9bbca4c | 2001-11-06 04:00:18 +0000 | [diff] [blame] | 3452 | if( amt>NBFS ){ |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 3453 | z = sqliteMallocRaw( amt ); |
drh | caec2f1 | 2003-01-07 02:47:47 +0000 | [diff] [blame] | 3454 | if( z==0 ) goto no_mem; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3455 | pTos->flags = MEM_Str | MEM_Dyn; |
drh | 9bbca4c | 2001-11-06 04:00:18 +0000 | [diff] [blame] | 3456 | }else{ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3457 | z = pTos->zShort; |
| 3458 | pTos->flags = MEM_Str | MEM_Short; |
drh | 9bbca4c | 2001-11-06 04:00:18 +0000 | [diff] [blame] | 3459 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3460 | sqlite3BtreeKey(pCrsr, 0, amt, z); |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3461 | pTos->z = z; |
| 3462 | pTos->n = amt; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3463 | } |
| 3464 | break; |
| 3465 | } |
| 3466 | |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 3467 | /* Opcode: NullRow P1 * * |
| 3468 | ** |
| 3469 | ** Move the cursor P1 to a null row. Any OP_Column operations |
| 3470 | ** that occur while the cursor is on the null row will always push |
| 3471 | ** a NULL onto the stack. |
| 3472 | */ |
| 3473 | case OP_NullRow: { |
| 3474 | int i = pOp->p1; |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame^] | 3475 | Cursor *pC; |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 3476 | |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3477 | assert( i>=0 && i<p->nCursor ); |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame^] | 3478 | pC = p->apCsr[i]; |
| 3479 | pC->nullRow = 1; |
| 3480 | pC->recnoIsValid = 0; |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 3481 | break; |
| 3482 | } |
| 3483 | |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 3484 | /* Opcode: Last P1 P2 * |
| 3485 | ** |
| 3486 | ** The next use of the Recno or Column or Next instruction for P1 |
| 3487 | ** will refer to the last entry in the database table or index. |
| 3488 | ** If the table or index is empty and P2>0, then jump immediately to P2. |
| 3489 | ** If P2 is 0 or if the table or index is not empty, fall through |
| 3490 | ** to the following instruction. |
| 3491 | */ |
| 3492 | case OP_Last: { |
| 3493 | int i = pOp->p1; |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3494 | Cursor *pC; |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 3495 | BtCursor *pCrsr; |
| 3496 | |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3497 | assert( i>=0 && i<p->nCursor ); |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame^] | 3498 | pC = p->apCsr[i]; |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3499 | if( (pCrsr = pC->pCursor)!=0 ){ |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 3500 | int res; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3501 | rc = sqlite3BtreeLast(pCrsr, &res); |
drh | a11846b | 2004-01-07 18:52:56 +0000 | [diff] [blame] | 3502 | pC->nullRow = res; |
| 3503 | pC->deferredMoveto = 0; |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 3504 | pC->cacheValid = 0; |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 3505 | if( res && pOp->p2>0 ){ |
| 3506 | pc = pOp->p2 - 1; |
| 3507 | } |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3508 | }else{ |
| 3509 | pC->nullRow = 0; |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 3510 | } |
| 3511 | break; |
| 3512 | } |
| 3513 | |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 3514 | /* Opcode: Rewind P1 P2 * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3515 | ** |
| 3516 | ** The next use of the Recno or Column or Next instruction for P1 |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 3517 | ** will refer to the first entry in the database table or index. |
| 3518 | ** If the table or index is empty and P2>0, then jump immediately to P2. |
| 3519 | ** If P2 is 0 or if the table or index is not empty, fall through |
| 3520 | ** to the following instruction. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3521 | */ |
| 3522 | case OP_Rewind: { |
| 3523 | int i = pOp->p1; |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3524 | Cursor *pC; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3525 | BtCursor *pCrsr; |
drh | f4dada7 | 2004-05-11 09:57:35 +0000 | [diff] [blame] | 3526 | int res; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3527 | |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3528 | assert( i>=0 && i<p->nCursor ); |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame^] | 3529 | pC = p->apCsr[i]; |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3530 | if( (pCrsr = pC->pCursor)!=0 ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3531 | rc = sqlite3BtreeFirst(pCrsr, &res); |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3532 | pC->atFirst = res==0; |
drh | a11846b | 2004-01-07 18:52:56 +0000 | [diff] [blame] | 3533 | pC->deferredMoveto = 0; |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 3534 | pC->cacheValid = 0; |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3535 | }else{ |
drh | f4dada7 | 2004-05-11 09:57:35 +0000 | [diff] [blame] | 3536 | res = 1; |
| 3537 | } |
| 3538 | pC->nullRow = res; |
| 3539 | if( res && pOp->p2>0 ){ |
| 3540 | pc = pOp->p2 - 1; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3541 | } |
| 3542 | break; |
| 3543 | } |
| 3544 | |
| 3545 | /* Opcode: Next P1 P2 * |
| 3546 | ** |
| 3547 | ** Advance cursor P1 so that it points to the next key/data pair in its |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 3548 | ** table or index. If there are no more key/value pairs then fall through |
| 3549 | ** to the following instruction. But if the cursor advance was successful, |
| 3550 | ** jump immediately to P2. |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 3551 | ** |
| 3552 | ** See also: Prev |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 3553 | */ |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 3554 | /* Opcode: Prev P1 P2 * |
| 3555 | ** |
| 3556 | ** Back up cursor P1 so that it points to the previous key/data pair in its |
| 3557 | ** table or index. If there is no previous key/value pairs then fall through |
| 3558 | ** to the following instruction. But if the cursor backup was successful, |
| 3559 | ** jump immediately to P2. |
| 3560 | */ |
| 3561 | case OP_Prev: |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 3562 | case OP_Next: { |
drh | 7b39686 | 2003-01-01 23:06:20 +0000 | [diff] [blame] | 3563 | Cursor *pC; |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 3564 | BtCursor *pCrsr; |
| 3565 | |
drh | caec2f1 | 2003-01-07 02:47:47 +0000 | [diff] [blame] | 3566 | CHECK_FOR_INTERRUPT; |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3567 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame^] | 3568 | pC = p->apCsr[pOp->p1]; |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3569 | if( (pCrsr = pC->pCursor)!=0 ){ |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 3570 | int res; |
drh | 7b39686 | 2003-01-01 23:06:20 +0000 | [diff] [blame] | 3571 | if( pC->nullRow ){ |
drh | ad2d830 | 2002-05-24 20:31:36 +0000 | [diff] [blame] | 3572 | res = 1; |
| 3573 | }else{ |
drh | a11846b | 2004-01-07 18:52:56 +0000 | [diff] [blame] | 3574 | assert( pC->deferredMoveto==0 ); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3575 | rc = pOp->opcode==OP_Next ? sqlite3BtreeNext(pCrsr, &res) : |
| 3576 | sqlite3BtreePrevious(pCrsr, &res); |
drh | 7b39686 | 2003-01-01 23:06:20 +0000 | [diff] [blame] | 3577 | pC->nullRow = res; |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 3578 | pC->cacheValid = 0; |
drh | ad2d830 | 2002-05-24 20:31:36 +0000 | [diff] [blame] | 3579 | } |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 3580 | if( res==0 ){ |
| 3581 | pc = pOp->p2 - 1; |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 3582 | sqlite3_search_count++; |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 3583 | } |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3584 | }else{ |
| 3585 | pC->nullRow = 1; |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 3586 | } |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3587 | pC->recnoIsValid = 0; |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 3588 | break; |
| 3589 | } |
| 3590 | |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 3591 | /* Opcode: IdxPut P1 P2 P3 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3592 | ** |
jplyon | 5a56422 | 2003-06-02 06:15:58 +0000 | [diff] [blame] | 3593 | ** The top of the stack holds a SQL index key made using the |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3594 | ** MakeIdxKey instruction. This opcode writes that key into the |
| 3595 | ** index P1. Data for the entry is nil. |
drh | 717e640 | 2001-09-27 03:22:32 +0000 | [diff] [blame] | 3596 | ** |
| 3597 | ** If P2==1, then the key must be unique. If the key is not unique, |
| 3598 | ** the program aborts with a SQLITE_CONSTRAINT error and the database |
jplyon | 5a56422 | 2003-06-02 06:15:58 +0000 | [diff] [blame] | 3599 | ** is rolled back. If P3 is not null, then it becomes part of the |
drh | 717e640 | 2001-09-27 03:22:32 +0000 | [diff] [blame] | 3600 | ** error message returned with the SQLITE_CONSTRAINT. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3601 | */ |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 3602 | case OP_IdxPut: { |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3603 | int i = pOp->p1; |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 3604 | Cursor *pC; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3605 | BtCursor *pCrsr; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3606 | assert( pTos>=p->aStack ); |
| 3607 | assert( i>=0 && i<p->nCursor ); |
| 3608 | assert( pTos->flags & MEM_Str ); |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame^] | 3609 | if( (pCrsr = (pC = p->apCsr[i])->pCursor)!=0 ){ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3610 | int nKey = pTos->n; |
| 3611 | const char *zKey = pTos->z; |
drh | 717e640 | 2001-09-27 03:22:32 +0000 | [diff] [blame] | 3612 | if( pOp->p2 ){ |
danielk1977 | 36a3c70 | 2004-05-11 06:55:14 +0000 | [diff] [blame] | 3613 | int res; |
danielk1977 | 452c989 | 2004-05-13 05:16:15 +0000 | [diff] [blame] | 3614 | int len; |
danielk1977 | 36a3c70 | 2004-05-11 06:55:14 +0000 | [diff] [blame] | 3615 | u64 n; |
danielk1977 | 452c989 | 2004-05-13 05:16:15 +0000 | [diff] [blame] | 3616 | |
| 3617 | /* 'len' is the length of the key minus the rowid at the end */ |
| 3618 | len = nKey-2; |
| 3619 | while( zKey[len] && --len ); |
| 3620 | |
| 3621 | rc = sqlite3BtreeMoveto(pCrsr, zKey, len, &res); |
drh | 717e640 | 2001-09-27 03:22:32 +0000 | [diff] [blame] | 3622 | if( rc!=SQLITE_OK ) goto abort_due_to_error; |
| 3623 | while( res!=0 ){ |
| 3624 | int c; |
danielk1977 | 36a3c70 | 2004-05-11 06:55:14 +0000 | [diff] [blame] | 3625 | sqlite3BtreeKeySize(pCrsr, &n); |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 3626 | if( n==nKey && |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 3627 | sqlite3VdbeIdxKeyCompare(pC, len, zKey, 0, &c)==SQLITE_OK |
danielk1977 | 452c989 | 2004-05-13 05:16:15 +0000 | [diff] [blame] | 3628 | && c==0 |
drh | 717e640 | 2001-09-27 03:22:32 +0000 | [diff] [blame] | 3629 | ){ |
| 3630 | rc = SQLITE_CONSTRAINT; |
| 3631 | if( pOp->p3 && pOp->p3[0] ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3632 | sqlite3SetString(&p->zErrMsg, pOp->p3, (char*)0); |
drh | 717e640 | 2001-09-27 03:22:32 +0000 | [diff] [blame] | 3633 | } |
| 3634 | goto abort_due_to_error; |
| 3635 | } |
| 3636 | if( res<0 ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3637 | sqlite3BtreeNext(pCrsr, &res); |
drh | 717e640 | 2001-09-27 03:22:32 +0000 | [diff] [blame] | 3638 | res = +1; |
| 3639 | }else{ |
| 3640 | break; |
| 3641 | } |
| 3642 | } |
| 3643 | } |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 3644 | assert( pC->intKey==0 ); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3645 | rc = sqlite3BtreeInsert(pCrsr, zKey, nKey, "", 0); |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 3646 | assert( pC->deferredMoveto==0 ); |
| 3647 | pC->cacheValid = 0; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3648 | } |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3649 | Release(pTos); |
| 3650 | pTos--; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3651 | break; |
| 3652 | } |
| 3653 | |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 3654 | /* Opcode: IdxDelete P1 * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3655 | ** |
| 3656 | ** The top of the stack is an index key built using the MakeIdxKey opcode. |
| 3657 | ** This opcode removes that entry from the index. |
| 3658 | */ |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 3659 | case OP_IdxDelete: { |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3660 | int i = pOp->p1; |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 3661 | Cursor *pC; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3662 | BtCursor *pCrsr; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3663 | assert( pTos>=p->aStack ); |
| 3664 | assert( pTos->flags & MEM_Str ); |
| 3665 | assert( i>=0 && i<p->nCursor ); |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame^] | 3666 | if( (pCrsr = (pC = p->apCsr[i])->pCursor)!=0 ){ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3667 | int rx, res; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3668 | rx = sqlite3BtreeMoveto(pCrsr, pTos->z, pTos->n, &res); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3669 | if( rx==SQLITE_OK && res==0 ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3670 | rc = sqlite3BtreeDelete(pCrsr); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3671 | } |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 3672 | assert( pC->deferredMoveto==0 ); |
| 3673 | pC->cacheValid = 0; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3674 | } |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3675 | Release(pTos); |
| 3676 | pTos--; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3677 | break; |
| 3678 | } |
| 3679 | |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 3680 | /* Opcode: IdxRecno P1 * * |
| 3681 | ** |
danielk1977 | 452c989 | 2004-05-13 05:16:15 +0000 | [diff] [blame] | 3682 | ** Push onto the stack an integer which is the varint located at the |
| 3683 | ** end of the index key pointed to by cursor P1. These integer should be |
| 3684 | ** the record number of the table entry to which this index entry points. |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 3685 | ** |
| 3686 | ** See also: Recno, MakeIdxKey. |
| 3687 | */ |
| 3688 | case OP_IdxRecno: { |
| 3689 | int i = pOp->p1; |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 3690 | BtCursor *pCrsr; |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame^] | 3691 | Cursor *pC; |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 3692 | |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3693 | assert( i>=0 && i<p->nCursor ); |
| 3694 | pTos++; |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame^] | 3695 | if( (pCrsr = (pC = p->apCsr[i])->pCursor)!=0 ){ |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 3696 | i64 rowid; |
danielk1977 | 452c989 | 2004-05-13 05:16:15 +0000 | [diff] [blame] | 3697 | |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame^] | 3698 | assert( pC->deferredMoveto==0 ); |
| 3699 | assert( pC->intKey==0 ); |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 3700 | rc = sqlite3VdbeIdxRowid(pCrsr, &rowid); |
| 3701 | if( rc!=SQLITE_OK ){ |
| 3702 | goto abort_due_to_error; |
| 3703 | } |
| 3704 | pTos->flags = MEM_Int; |
| 3705 | pTos->i = rowid; |
danielk1977 | 452c989 | 2004-05-13 05:16:15 +0000 | [diff] [blame] | 3706 | |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 3707 | #if 0 |
danielk1977 | 452c989 | 2004-05-13 05:16:15 +0000 | [diff] [blame] | 3708 | /* Read the final 9 bytes of the key into buf[]. If the whole key is |
| 3709 | ** less than 9 bytes then just load the whole thing. Set len to the |
| 3710 | ** number of bytes read. |
| 3711 | */ |
danielk1977 | 6490beb | 2004-05-11 06:17:21 +0000 | [diff] [blame] | 3712 | sqlite3BtreeKeySize(pCrsr, &sz); |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 3713 | len = ((sz>10)?10:sz); |
danielk1977 | 452c989 | 2004-05-13 05:16:15 +0000 | [diff] [blame] | 3714 | rc = sqlite3BtreeKey(pCrsr, sz-len, len, buf); |
| 3715 | if( rc!=SQLITE_OK ){ |
| 3716 | goto abort_due_to_error; |
| 3717 | } |
| 3718 | |
| 3719 | len--; |
| 3720 | if( buf[len]&0x80 ){ |
| 3721 | /* If the last byte read has the 0x80 bit set, then the key does |
| 3722 | ** not end with a varint. Push a NULL onto the stack instead. |
| 3723 | */ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3724 | pTos->flags = MEM_Null; |
drh | d4d595f | 2003-04-17 12:44:23 +0000 | [diff] [blame] | 3725 | }else{ |
danielk1977 | 452c989 | 2004-05-13 05:16:15 +0000 | [diff] [blame] | 3726 | /* Find the start of the varint by searching backwards for a 0x00 |
| 3727 | ** byte. If one does not exists, then intepret the whole 9 bytes as a |
| 3728 | ** varint. |
| 3729 | */ |
| 3730 | while( len && buf[len-1] ){ |
| 3731 | len--; |
| 3732 | } |
| 3733 | sqlite3GetVarint(&buf[len], &sz); |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3734 | pTos->flags = MEM_Int; |
danielk1977 | 452c989 | 2004-05-13 05:16:15 +0000 | [diff] [blame] | 3735 | pTos->i = sz; |
drh | d4d595f | 2003-04-17 12:44:23 +0000 | [diff] [blame] | 3736 | } |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 3737 | #endif |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3738 | }else{ |
| 3739 | pTos->flags = MEM_Null; |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 3740 | } |
| 3741 | break; |
| 3742 | } |
| 3743 | |
| 3744 | /* Opcode: IdxGT P1 P2 * |
| 3745 | ** |
| 3746 | ** Compare the top of the stack against the key on the index entry that |
| 3747 | ** cursor P1 is currently pointing to. Ignore the last 4 bytes of the |
| 3748 | ** index entry. If the index entry is greater than the top of the stack |
| 3749 | ** then jump to P2. Otherwise fall through to the next instruction. |
| 3750 | ** In either case, the stack is popped once. |
| 3751 | */ |
| 3752 | /* Opcode: IdxGE P1 P2 * |
| 3753 | ** |
| 3754 | ** Compare the top of the stack against the key on the index entry that |
| 3755 | ** cursor P1 is currently pointing to. Ignore the last 4 bytes of the |
| 3756 | ** index entry. If the index entry is greater than or equal to |
| 3757 | ** the top of the stack |
| 3758 | ** then jump to P2. Otherwise fall through to the next instruction. |
| 3759 | ** In either case, the stack is popped once. |
| 3760 | */ |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 3761 | /* Opcode: IdxLT P1 P2 * |
| 3762 | ** |
| 3763 | ** Compare the top of the stack against the key on the index entry that |
| 3764 | ** cursor P1 is currently pointing to. Ignore the last 4 bytes of the |
| 3765 | ** index entry. If the index entry is less than the top of the stack |
| 3766 | ** then jump to P2. Otherwise fall through to the next instruction. |
| 3767 | ** In either case, the stack is popped once. |
| 3768 | */ |
| 3769 | case OP_IdxLT: |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 3770 | case OP_IdxGT: |
| 3771 | case OP_IdxGE: { |
| 3772 | int i= pOp->p1; |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 3773 | BtCursor *pCrsr; |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame^] | 3774 | Cursor *pC; |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 3775 | |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3776 | assert( i>=0 && i<p->nCursor ); |
| 3777 | assert( pTos>=p->aStack ); |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame^] | 3778 | if( (pCrsr = (pC = p->apCsr[i])->pCursor)!=0 ){ |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 3779 | int res, rc; |
| 3780 | |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3781 | Stringify(pTos); |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame^] | 3782 | assert( pC->deferredMoveto==0 ); |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 3783 | if( pOp->p3 ){ |
| 3784 | pC->incrKey = 1; |
| 3785 | } |
| 3786 | rc = sqlite3VdbeIdxKeyCompare(pC, pTos->n, pTos->z, 0, &res); |
| 3787 | pC->incrKey = 0; |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 3788 | if( rc!=SQLITE_OK ){ |
| 3789 | break; |
| 3790 | } |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 3791 | if( pOp->opcode==OP_IdxLT ){ |
| 3792 | res = -res; |
| 3793 | }else if( pOp->opcode==OP_IdxGE ){ |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 3794 | res++; |
| 3795 | } |
| 3796 | if( res>0 ){ |
| 3797 | pc = pOp->p2 - 1 ; |
| 3798 | } |
| 3799 | } |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3800 | Release(pTos); |
| 3801 | pTos--; |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 3802 | break; |
| 3803 | } |
| 3804 | |
drh | 143f3c4 | 2004-01-07 20:37:52 +0000 | [diff] [blame] | 3805 | /* Opcode: IdxIsNull P1 P2 * |
| 3806 | ** |
| 3807 | ** The top of the stack contains an index entry such as might be generated |
| 3808 | ** by the MakeIdxKey opcode. This routine looks at the first P1 fields of |
| 3809 | ** that key. If any of the first P1 fields are NULL, then a jump is made |
| 3810 | ** to address P2. Otherwise we fall straight through. |
| 3811 | ** |
| 3812 | ** The index entry is always popped from the stack. |
| 3813 | */ |
| 3814 | case OP_IdxIsNull: { |
| 3815 | int i = pOp->p1; |
drh | 143f3c4 | 2004-01-07 20:37:52 +0000 | [diff] [blame] | 3816 | int k, n; |
| 3817 | const char *z; |
| 3818 | |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3819 | assert( pTos>=p->aStack ); |
| 3820 | assert( pTos->flags & MEM_Str ); |
| 3821 | z = pTos->z; |
| 3822 | n = pTos->n; |
drh | 143f3c4 | 2004-01-07 20:37:52 +0000 | [diff] [blame] | 3823 | for(k=0; k<n && i>0; i--){ |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 3824 | u64 serial_type; |
| 3825 | k += sqlite3GetVarint(&z[k], &serial_type); |
| 3826 | if( serial_type==6 ){ /* Serial type 6 is a NULL */ |
drh | 143f3c4 | 2004-01-07 20:37:52 +0000 | [diff] [blame] | 3827 | pc = pOp->p2-1; |
| 3828 | break; |
| 3829 | } |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 3830 | k += sqlite3VdbeSerialTypeLen(serial_type); |
drh | 143f3c4 | 2004-01-07 20:37:52 +0000 | [diff] [blame] | 3831 | } |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3832 | Release(pTos); |
| 3833 | pTos--; |
drh | 143f3c4 | 2004-01-07 20:37:52 +0000 | [diff] [blame] | 3834 | break; |
| 3835 | } |
| 3836 | |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 3837 | /* Opcode: Destroy P1 P2 * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3838 | ** |
| 3839 | ** Delete an entire database table or index whose root page in the database |
| 3840 | ** file is given by P1. |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 3841 | ** |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 3842 | ** The table being destroyed is in the main database file if P2==0. If |
| 3843 | ** P2==1 then the table to be clear is in the auxiliary database file |
| 3844 | ** that is used to store tables create using CREATE TEMPORARY TABLE. |
| 3845 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 3846 | ** See also: Clear |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3847 | */ |
| 3848 | case OP_Destroy: { |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3849 | rc = sqlite3BtreeDropTable(db->aDb[pOp->p2].pBt, pOp->p1); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3850 | break; |
| 3851 | } |
| 3852 | |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 3853 | /* Opcode: Clear P1 P2 * |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 3854 | ** |
| 3855 | ** Delete all contents of the database table or index whose root page |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 3856 | ** in the database file is given by P1. But, unlike Destroy, do not |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 3857 | ** remove the table or index from the database file. |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 3858 | ** |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 3859 | ** The table being clear is in the main database file if P2==0. If |
| 3860 | ** P2==1 then the table to be clear is in the auxiliary database file |
| 3861 | ** that is used to store tables create using CREATE TEMPORARY TABLE. |
| 3862 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 3863 | ** See also: Destroy |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 3864 | */ |
| 3865 | case OP_Clear: { |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3866 | rc = sqlite3BtreeClearTable(db->aDb[pOp->p2].pBt, pOp->p1); |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 3867 | break; |
| 3868 | } |
| 3869 | |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 3870 | /* Opcode: CreateTable * P2 P3 |
drh | 5b2fd56 | 2001-09-13 15:21:31 +0000 | [diff] [blame] | 3871 | ** |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 3872 | ** Allocate a new table in the main database file if P2==0 or in the |
| 3873 | ** auxiliary database file if P2==1. Push the page number |
drh | 5b2fd56 | 2001-09-13 15:21:31 +0000 | [diff] [blame] | 3874 | ** for the root page of the new table onto the stack. |
| 3875 | ** |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 3876 | ** The root page number is also written to a memory location that P3 |
| 3877 | ** points to. This is the mechanism is used to write the root page |
| 3878 | ** number into the parser's internal data structures that describe the |
| 3879 | ** new table. |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 3880 | ** |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 3881 | ** The difference between a table and an index is this: A table must |
| 3882 | ** have a 4-byte integer key and can have arbitrary data. An index |
| 3883 | ** has an arbitrary key but no data. |
| 3884 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 3885 | ** See also: CreateIndex |
drh | 5b2fd56 | 2001-09-13 15:21:31 +0000 | [diff] [blame] | 3886 | */ |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 3887 | /* Opcode: CreateIndex * P2 P3 |
| 3888 | ** |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 3889 | ** Allocate a new index in the main database file if P2==0 or in the |
| 3890 | ** auxiliary database file if P2==1. Push the page number of the |
| 3891 | ** root page of the new index onto the stack. |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 3892 | ** |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 3893 | ** See documentation on OP_CreateTable for additional information. |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 3894 | */ |
| 3895 | case OP_CreateIndex: |
drh | 5b2fd56 | 2001-09-13 15:21:31 +0000 | [diff] [blame] | 3896 | case OP_CreateTable: { |
drh | 5b2fd56 | 2001-09-13 15:21:31 +0000 | [diff] [blame] | 3897 | int pgno; |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 3898 | int flags; |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 3899 | assert( pOp->p3!=0 && pOp->p3type==P3_POINTER ); |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 3900 | assert( pOp->p2>=0 && pOp->p2<db->nDb ); |
| 3901 | assert( db->aDb[pOp->p2].pBt!=0 ); |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 3902 | if( pOp->opcode==OP_CreateTable ){ |
danielk1977 | 9407625 | 2004-05-14 12:16:11 +0000 | [diff] [blame] | 3903 | /* flags = BTREE_INTKEY; */ |
| 3904 | flags = BTREE_LEAFDATA|BTREE_INTKEY; |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 3905 | }else{ |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 3906 | flags = BTREE_ZERODATA; |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 3907 | } |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 3908 | rc = sqlite3BtreeCreateTable(db->aDb[pOp->p2].pBt, &pgno, flags); |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3909 | pTos++; |
drh | 5b2fd56 | 2001-09-13 15:21:31 +0000 | [diff] [blame] | 3910 | if( rc==SQLITE_OK ){ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3911 | pTos->i = pgno; |
| 3912 | pTos->flags = MEM_Int; |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 3913 | *(u32*)pOp->p3 = pgno; |
| 3914 | pOp->p3 = 0; |
drh | f1b07b0 | 2004-02-08 06:17:19 +0000 | [diff] [blame] | 3915 | }else{ |
| 3916 | pTos->flags = MEM_Null; |
drh | 5b2fd56 | 2001-09-13 15:21:31 +0000 | [diff] [blame] | 3917 | } |
| 3918 | break; |
| 3919 | } |
| 3920 | |
drh | 2150432 | 2002-06-25 13:16:02 +0000 | [diff] [blame] | 3921 | /* Opcode: IntegrityCk P1 P2 * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3922 | ** |
drh | 1dd397f | 2002-02-03 03:34:07 +0000 | [diff] [blame] | 3923 | ** Do an analysis of the currently open database. Push onto the |
| 3924 | ** stack the text of an error message describing any problems. |
| 3925 | ** If there are no errors, push a "ok" onto the stack. |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 3926 | ** |
drh | 1dd397f | 2002-02-03 03:34:07 +0000 | [diff] [blame] | 3927 | ** P1 is the index of a set that contains the root page numbers |
drh | ed717fe | 2003-06-15 23:42:24 +0000 | [diff] [blame] | 3928 | ** for all tables and indices in the main database file. The set |
| 3929 | ** is cleared by this opcode. In other words, after this opcode |
| 3930 | ** has executed, the set will be empty. |
drh | 2150432 | 2002-06-25 13:16:02 +0000 | [diff] [blame] | 3931 | ** |
| 3932 | ** If P2 is not zero, the check is done on the auxiliary database |
| 3933 | ** file, not the main database file. |
drh | 1dd397f | 2002-02-03 03:34:07 +0000 | [diff] [blame] | 3934 | ** |
| 3935 | ** This opcode is used for testing purposes only. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3936 | */ |
drh | aaab572 | 2002-02-19 13:39:21 +0000 | [diff] [blame] | 3937 | case OP_IntegrityCk: { |
drh | 1dd397f | 2002-02-03 03:34:07 +0000 | [diff] [blame] | 3938 | int nRoot; |
| 3939 | int *aRoot; |
drh | 1dd397f | 2002-02-03 03:34:07 +0000 | [diff] [blame] | 3940 | int iSet = pOp->p1; |
| 3941 | Set *pSet; |
| 3942 | int j; |
| 3943 | HashElem *i; |
| 3944 | char *z; |
| 3945 | |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3946 | assert( iSet>=0 && iSet<p->nSet ); |
| 3947 | pTos++; |
drh | 1dd397f | 2002-02-03 03:34:07 +0000 | [diff] [blame] | 3948 | pSet = &p->aSet[iSet]; |
| 3949 | nRoot = sqliteHashCount(&pSet->hash); |
drh | caec2f1 | 2003-01-07 02:47:47 +0000 | [diff] [blame] | 3950 | aRoot = sqliteMallocRaw( sizeof(int)*(nRoot+1) ); |
| 3951 | if( aRoot==0 ) goto no_mem; |
drh | 1dd397f | 2002-02-03 03:34:07 +0000 | [diff] [blame] | 3952 | for(j=0, i=sqliteHashFirst(&pSet->hash); i; i=sqliteHashNext(i), j++){ |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 3953 | i64 root64; |
| 3954 | toInt((char*)sqliteHashKey(i), &root64); |
| 3955 | aRoot[j] = root64; |
drh | 1dd397f | 2002-02-03 03:34:07 +0000 | [diff] [blame] | 3956 | } |
| 3957 | aRoot[j] = 0; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3958 | sqlite3HashClear(&pSet->hash); |
drh | ed717fe | 2003-06-15 23:42:24 +0000 | [diff] [blame] | 3959 | pSet->prev = 0; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3960 | z = sqlite3BtreeIntegrityCheck(db->aDb[pOp->p2].pBt, aRoot, nRoot); |
drh | 1dd397f | 2002-02-03 03:34:07 +0000 | [diff] [blame] | 3961 | if( z==0 || z[0]==0 ){ |
drh | 2150432 | 2002-06-25 13:16:02 +0000 | [diff] [blame] | 3962 | if( z ) sqliteFree(z); |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3963 | pTos->z = "ok"; |
| 3964 | pTos->n = 3; |
| 3965 | pTos->flags = MEM_Str | MEM_Static; |
drh | 1dd397f | 2002-02-03 03:34:07 +0000 | [diff] [blame] | 3966 | }else{ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3967 | pTos->z = z; |
| 3968 | pTos->n = strlen(z) + 1; |
| 3969 | pTos->flags = MEM_Str | MEM_Dyn; |
drh | 1dd397f | 2002-02-03 03:34:07 +0000 | [diff] [blame] | 3970 | } |
drh | 24e97df | 2002-02-03 19:06:02 +0000 | [diff] [blame] | 3971 | sqliteFree(aRoot); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3972 | break; |
| 3973 | } |
| 3974 | |
drh | a8b38d2 | 2001-11-01 14:41:34 +0000 | [diff] [blame] | 3975 | /* Opcode: ListWrite * * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3976 | ** |
| 3977 | ** Write the integer on the top of the stack |
drh | a8b38d2 | 2001-11-01 14:41:34 +0000 | [diff] [blame] | 3978 | ** into the temporary storage list. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3979 | */ |
| 3980 | case OP_ListWrite: { |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3981 | Keylist *pKeylist; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3982 | assert( pTos>=p->aStack ); |
drh | a8b38d2 | 2001-11-01 14:41:34 +0000 | [diff] [blame] | 3983 | pKeylist = p->pList; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3984 | if( pKeylist==0 || pKeylist->nUsed>=pKeylist->nKey ){ |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 3985 | pKeylist = sqliteMallocRaw( sizeof(Keylist)+999*sizeof(pKeylist->aKey[0]) ); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3986 | if( pKeylist==0 ) goto no_mem; |
| 3987 | pKeylist->nKey = 1000; |
| 3988 | pKeylist->nRead = 0; |
| 3989 | pKeylist->nUsed = 0; |
drh | a8b38d2 | 2001-11-01 14:41:34 +0000 | [diff] [blame] | 3990 | pKeylist->pNext = p->pList; |
| 3991 | p->pList = pKeylist; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3992 | } |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3993 | Integerify(pTos); |
| 3994 | pKeylist->aKey[pKeylist->nUsed++] = pTos->i; |
drh | 79f14b7 | 2004-03-03 01:51:24 +0000 | [diff] [blame] | 3995 | Release(pTos); |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3996 | pTos--; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3997 | break; |
| 3998 | } |
| 3999 | |
drh | a8b38d2 | 2001-11-01 14:41:34 +0000 | [diff] [blame] | 4000 | /* Opcode: ListRewind * * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4001 | ** |
drh | fb044c1 | 2004-02-10 13:41:52 +0000 | [diff] [blame] | 4002 | ** Rewind the temporary buffer back to the beginning. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4003 | */ |
| 4004 | case OP_ListRewind: { |
drh | fb044c1 | 2004-02-10 13:41:52 +0000 | [diff] [blame] | 4005 | /* What this opcode codes, really, is reverse the order of the |
| 4006 | ** linked list of Keylist structures so that they are read out |
| 4007 | ** in the same order that they were read in. */ |
| 4008 | Keylist *pRev, *pTop; |
| 4009 | pRev = 0; |
| 4010 | while( p->pList ){ |
| 4011 | pTop = p->pList; |
| 4012 | p->pList = pTop->pNext; |
| 4013 | pTop->pNext = pRev; |
| 4014 | pRev = pTop; |
| 4015 | } |
| 4016 | p->pList = pRev; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4017 | break; |
| 4018 | } |
| 4019 | |
drh | a8b38d2 | 2001-11-01 14:41:34 +0000 | [diff] [blame] | 4020 | /* Opcode: ListRead * P2 * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4021 | ** |
drh | a8b38d2 | 2001-11-01 14:41:34 +0000 | [diff] [blame] | 4022 | ** Attempt to read an integer from the temporary storage buffer |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4023 | ** and push it onto the stack. If the storage buffer is empty, |
| 4024 | ** push nothing but instead jump to P2. |
| 4025 | */ |
| 4026 | case OP_ListRead: { |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4027 | Keylist *pKeylist; |
drh | caec2f1 | 2003-01-07 02:47:47 +0000 | [diff] [blame] | 4028 | CHECK_FOR_INTERRUPT; |
drh | a8b38d2 | 2001-11-01 14:41:34 +0000 | [diff] [blame] | 4029 | pKeylist = p->pList; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4030 | if( pKeylist!=0 ){ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 4031 | assert( pKeylist->nRead>=0 ); |
| 4032 | assert( pKeylist->nRead<pKeylist->nUsed ); |
| 4033 | assert( pKeylist->nRead<pKeylist->nKey ); |
| 4034 | pTos++; |
| 4035 | pTos->i = pKeylist->aKey[pKeylist->nRead++]; |
| 4036 | pTos->flags = MEM_Int; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4037 | if( pKeylist->nRead>=pKeylist->nUsed ){ |
drh | a8b38d2 | 2001-11-01 14:41:34 +0000 | [diff] [blame] | 4038 | p->pList = pKeylist->pNext; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4039 | sqliteFree(pKeylist); |
| 4040 | } |
| 4041 | }else{ |
| 4042 | pc = pOp->p2 - 1; |
| 4043 | } |
| 4044 | break; |
| 4045 | } |
| 4046 | |
drh | a8b38d2 | 2001-11-01 14:41:34 +0000 | [diff] [blame] | 4047 | /* Opcode: ListReset * * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4048 | ** |
drh | a8b38d2 | 2001-11-01 14:41:34 +0000 | [diff] [blame] | 4049 | ** Reset the temporary storage buffer so that it holds nothing. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4050 | */ |
drh | a8b38d2 | 2001-11-01 14:41:34 +0000 | [diff] [blame] | 4051 | case OP_ListReset: { |
| 4052 | if( p->pList ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 4053 | sqlite3VdbeKeylistFree(p->pList); |
drh | a8b38d2 | 2001-11-01 14:41:34 +0000 | [diff] [blame] | 4054 | p->pList = 0; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4055 | } |
| 4056 | break; |
| 4057 | } |
| 4058 | |
drh | bd5a451 | 2002-05-23 22:07:02 +0000 | [diff] [blame] | 4059 | /* Opcode: ListPush * * * |
| 4060 | ** |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 4061 | ** Save the current Vdbe list such that it can be restored by a ListPop |
drh | bd5a451 | 2002-05-23 22:07:02 +0000 | [diff] [blame] | 4062 | ** opcode. The list is empty after this is executed. |
| 4063 | */ |
| 4064 | case OP_ListPush: { |
| 4065 | p->keylistStackDepth++; |
| 4066 | assert(p->keylistStackDepth > 0); |
| 4067 | p->keylistStack = sqliteRealloc(p->keylistStack, |
| 4068 | sizeof(Keylist *) * p->keylistStackDepth); |
drh | caec2f1 | 2003-01-07 02:47:47 +0000 | [diff] [blame] | 4069 | if( p->keylistStack==0 ) goto no_mem; |
drh | bd5a451 | 2002-05-23 22:07:02 +0000 | [diff] [blame] | 4070 | p->keylistStack[p->keylistStackDepth - 1] = p->pList; |
| 4071 | p->pList = 0; |
| 4072 | break; |
| 4073 | } |
| 4074 | |
| 4075 | /* Opcode: ListPop * * * |
| 4076 | ** |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 4077 | ** Restore the Vdbe list to the state it was in when ListPush was last |
drh | bd5a451 | 2002-05-23 22:07:02 +0000 | [diff] [blame] | 4078 | ** executed. |
| 4079 | */ |
| 4080 | case OP_ListPop: { |
| 4081 | assert(p->keylistStackDepth > 0); |
| 4082 | p->keylistStackDepth--; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 4083 | sqlite3VdbeKeylistFree(p->pList); |
drh | bd5a451 | 2002-05-23 22:07:02 +0000 | [diff] [blame] | 4084 | p->pList = p->keylistStack[p->keylistStackDepth]; |
| 4085 | p->keylistStack[p->keylistStackDepth] = 0; |
| 4086 | if( p->keylistStackDepth == 0 ){ |
| 4087 | sqliteFree(p->keylistStack); |
| 4088 | p->keylistStack = 0; |
| 4089 | } |
| 4090 | break; |
| 4091 | } |
| 4092 | |
rdc | b0c374f | 2004-02-20 22:53:38 +0000 | [diff] [blame] | 4093 | /* Opcode: ContextPush * * * |
| 4094 | ** |
| 4095 | ** Save the current Vdbe context such that it can be restored by a ContextPop |
| 4096 | ** opcode. The context stores the last insert row id, the last statement change |
| 4097 | ** count, and the current statement change count. |
| 4098 | */ |
| 4099 | case OP_ContextPush: { |
| 4100 | p->contextStackDepth++; |
| 4101 | assert(p->contextStackDepth > 0); |
| 4102 | p->contextStack = sqliteRealloc(p->contextStack, |
| 4103 | sizeof(Context) * p->contextStackDepth); |
| 4104 | if( p->contextStack==0 ) goto no_mem; |
| 4105 | p->contextStack[p->contextStackDepth - 1].lastRowid = p->db->lastRowid; |
| 4106 | p->contextStack[p->contextStackDepth - 1].lsChange = p->db->lsChange; |
| 4107 | p->contextStack[p->contextStackDepth - 1].csChange = p->db->csChange; |
| 4108 | break; |
| 4109 | } |
| 4110 | |
| 4111 | /* Opcode: ContextPop * * * |
| 4112 | ** |
| 4113 | ** Restore the Vdbe context to the state it was in when contextPush was last |
| 4114 | ** executed. The context stores the last insert row id, the last statement |
| 4115 | ** change count, and the current statement change count. |
| 4116 | */ |
| 4117 | case OP_ContextPop: { |
| 4118 | assert(p->contextStackDepth > 0); |
| 4119 | p->contextStackDepth--; |
| 4120 | p->db->lastRowid = p->contextStack[p->contextStackDepth].lastRowid; |
| 4121 | p->db->lsChange = p->contextStack[p->contextStackDepth].lsChange; |
| 4122 | p->db->csChange = p->contextStack[p->contextStackDepth].csChange; |
| 4123 | if( p->contextStackDepth == 0 ){ |
| 4124 | sqliteFree(p->contextStack); |
| 4125 | p->contextStack = 0; |
| 4126 | } |
| 4127 | break; |
| 4128 | } |
| 4129 | |
drh | a8b38d2 | 2001-11-01 14:41:34 +0000 | [diff] [blame] | 4130 | /* Opcode: SortPut * * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4131 | ** |
| 4132 | ** The TOS is the key and the NOS is the data. Pop both from the stack |
drh | 9bbca4c | 2001-11-06 04:00:18 +0000 | [diff] [blame] | 4133 | ** and put them on the sorter. The key and data should have been |
| 4134 | ** made using SortMakeKey and SortMakeRec, respectively. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4135 | */ |
| 4136 | case OP_SortPut: { |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 4137 | Mem *pNos = &pTos[-1]; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4138 | Sorter *pSorter; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 4139 | assert( pNos>=p->aStack ); |
| 4140 | if( Dynamicify(pTos) || Dynamicify(pNos) ) goto no_mem; |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 4141 | pSorter = sqliteMallocRaw( sizeof(Sorter) ); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4142 | if( pSorter==0 ) goto no_mem; |
drh | a8b38d2 | 2001-11-01 14:41:34 +0000 | [diff] [blame] | 4143 | pSorter->pNext = p->pSort; |
| 4144 | p->pSort = pSorter; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 4145 | assert( pTos->flags & MEM_Dyn ); |
| 4146 | pSorter->nKey = pTos->n; |
| 4147 | pSorter->zKey = pTos->z; |
| 4148 | assert( pNos->flags & MEM_Dyn ); |
| 4149 | pSorter->nData = pNos->n; |
| 4150 | pSorter->pData = pNos->z; |
| 4151 | pTos -= 2; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4152 | break; |
| 4153 | } |
| 4154 | |
| 4155 | /* Opcode: SortMakeRec P1 * * |
| 4156 | ** |
| 4157 | ** The top P1 elements are the arguments to a callback. Form these |
| 4158 | ** elements into a single data entry that can be stored on a sorter |
| 4159 | ** using SortPut and later fed to a callback using SortCallback. |
| 4160 | */ |
| 4161 | case OP_SortMakeRec: { |
| 4162 | char *z; |
| 4163 | char **azArg; |
| 4164 | int nByte; |
| 4165 | int nField; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 4166 | int i; |
| 4167 | Mem *pRec; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4168 | |
| 4169 | nField = pOp->p1; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 4170 | pRec = &pTos[1-nField]; |
| 4171 | assert( pRec>=p->aStack ); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4172 | nByte = 0; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 4173 | for(i=0; i<nField; i++, pRec++){ |
| 4174 | if( (pRec->flags & MEM_Null)==0 ){ |
| 4175 | Stringify(pRec); |
| 4176 | nByte += pRec->n; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4177 | } |
| 4178 | } |
| 4179 | nByte += sizeof(char*)*(nField+1); |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 4180 | azArg = sqliteMallocRaw( nByte ); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4181 | if( azArg==0 ) goto no_mem; |
| 4182 | z = (char*)&azArg[nField+1]; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 4183 | for(pRec=&pTos[1-nField], i=0; i<nField; i++, pRec++){ |
| 4184 | if( pRec->flags & MEM_Null ){ |
| 4185 | azArg[i] = 0; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4186 | }else{ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 4187 | azArg[i] = z; |
| 4188 | memcpy(z, pRec->z, pRec->n); |
| 4189 | z += pRec->n; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4190 | } |
| 4191 | } |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 4192 | popStack(&pTos, nField); |
| 4193 | pTos++; |
| 4194 | pTos->n = nByte; |
| 4195 | pTos->z = (char*)azArg; |
| 4196 | pTos->flags = MEM_Str | MEM_Dyn; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4197 | break; |
| 4198 | } |
| 4199 | |
drh | a8b38d2 | 2001-11-01 14:41:34 +0000 | [diff] [blame] | 4200 | /* Opcode: SortMakeKey * * P3 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4201 | ** |
| 4202 | ** Convert the top few entries of the stack into a sort key. The |
| 4203 | ** number of stack entries consumed is the number of characters in |
| 4204 | ** the string P3. One character from P3 is prepended to each entry. |
| 4205 | ** The first character of P3 is prepended to the element lowest in |
drh | da30d36 | 2002-08-26 19:55:07 +0000 | [diff] [blame] | 4206 | ** the stack and the last character of P3 is prepended to the top of |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4207 | ** the stack. All stack entries are separated by a \000 character |
| 4208 | ** in the result. The whole key is terminated by two \000 characters |
| 4209 | ** in a row. |
| 4210 | ** |
drh | da30d36 | 2002-08-26 19:55:07 +0000 | [diff] [blame] | 4211 | ** "N" is substituted in place of the P3 character for NULL values. |
| 4212 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 4213 | ** See also the MakeKey and MakeIdxKey opcodes. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4214 | */ |
| 4215 | case OP_SortMakeKey: { |
| 4216 | char *zNewKey; |
| 4217 | int nByte; |
| 4218 | int nField; |
| 4219 | int i, j, k; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 4220 | Mem *pRec; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4221 | |
| 4222 | nField = strlen(pOp->p3); |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 4223 | pRec = &pTos[1-nField]; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4224 | nByte = 1; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 4225 | for(i=0; i<nField; i++, pRec++){ |
| 4226 | if( pRec->flags & MEM_Null ){ |
drh | da30d36 | 2002-08-26 19:55:07 +0000 | [diff] [blame] | 4227 | nByte += 2; |
| 4228 | }else{ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 4229 | Stringify(pRec); |
| 4230 | nByte += pRec->n+2; |
drh | da30d36 | 2002-08-26 19:55:07 +0000 | [diff] [blame] | 4231 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4232 | } |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 4233 | zNewKey = sqliteMallocRaw( nByte ); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4234 | if( zNewKey==0 ) goto no_mem; |
| 4235 | j = 0; |
| 4236 | k = 0; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 4237 | for(pRec=&pTos[1-nField], i=0; i<nField; i++, pRec++){ |
| 4238 | if( pRec->flags & MEM_Null ){ |
drh | da30d36 | 2002-08-26 19:55:07 +0000 | [diff] [blame] | 4239 | zNewKey[j++] = 'N'; |
| 4240 | zNewKey[j++] = 0; |
| 4241 | k++; |
| 4242 | }else{ |
| 4243 | zNewKey[j++] = pOp->p3[k++]; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 4244 | memcpy(&zNewKey[j], pRec->z, pRec->n-1); |
| 4245 | j += pRec->n-1; |
drh | da30d36 | 2002-08-26 19:55:07 +0000 | [diff] [blame] | 4246 | zNewKey[j++] = 0; |
| 4247 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4248 | } |
| 4249 | zNewKey[j] = 0; |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 4250 | assert( j<nByte ); |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 4251 | popStack(&pTos, nField); |
| 4252 | pTos++; |
| 4253 | pTos->n = nByte; |
| 4254 | pTos->flags = MEM_Str|MEM_Dyn; |
| 4255 | pTos->z = zNewKey; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4256 | break; |
| 4257 | } |
| 4258 | |
drh | a8b38d2 | 2001-11-01 14:41:34 +0000 | [diff] [blame] | 4259 | /* Opcode: Sort * * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4260 | ** |
drh | a8b38d2 | 2001-11-01 14:41:34 +0000 | [diff] [blame] | 4261 | ** Sort all elements on the sorter. The algorithm is a |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4262 | ** mergesort. |
| 4263 | */ |
| 4264 | case OP_Sort: { |
drh | a8b38d2 | 2001-11-01 14:41:34 +0000 | [diff] [blame] | 4265 | int i; |
| 4266 | Sorter *pElem; |
| 4267 | Sorter *apSorter[NSORT]; |
| 4268 | for(i=0; i<NSORT; i++){ |
| 4269 | apSorter[i] = 0; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4270 | } |
drh | a8b38d2 | 2001-11-01 14:41:34 +0000 | [diff] [blame] | 4271 | while( p->pSort ){ |
| 4272 | pElem = p->pSort; |
| 4273 | p->pSort = pElem->pNext; |
| 4274 | pElem->pNext = 0; |
| 4275 | for(i=0; i<NSORT-1; i++){ |
| 4276 | if( apSorter[i]==0 ){ |
| 4277 | apSorter[i] = pElem; |
| 4278 | break; |
| 4279 | }else{ |
| 4280 | pElem = Merge(apSorter[i], pElem); |
| 4281 | apSorter[i] = 0; |
| 4282 | } |
| 4283 | } |
| 4284 | if( i>=NSORT-1 ){ |
| 4285 | apSorter[NSORT-1] = Merge(apSorter[NSORT-1],pElem); |
| 4286 | } |
| 4287 | } |
| 4288 | pElem = 0; |
| 4289 | for(i=0; i<NSORT; i++){ |
| 4290 | pElem = Merge(apSorter[i], pElem); |
| 4291 | } |
| 4292 | p->pSort = pElem; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4293 | break; |
| 4294 | } |
| 4295 | |
drh | a8b38d2 | 2001-11-01 14:41:34 +0000 | [diff] [blame] | 4296 | /* Opcode: SortNext * P2 * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4297 | ** |
drh | a8b38d2 | 2001-11-01 14:41:34 +0000 | [diff] [blame] | 4298 | ** Push the data for the topmost element in the sorter onto the |
| 4299 | ** stack, then remove the element from the sorter. If the sorter |
| 4300 | ** is empty, push nothing on the stack and instead jump immediately |
| 4301 | ** to instruction P2. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4302 | */ |
| 4303 | case OP_SortNext: { |
drh | a8b38d2 | 2001-11-01 14:41:34 +0000 | [diff] [blame] | 4304 | Sorter *pSorter = p->pSort; |
drh | caec2f1 | 2003-01-07 02:47:47 +0000 | [diff] [blame] | 4305 | CHECK_FOR_INTERRUPT; |
drh | a8b38d2 | 2001-11-01 14:41:34 +0000 | [diff] [blame] | 4306 | if( pSorter!=0 ){ |
| 4307 | p->pSort = pSorter->pNext; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 4308 | pTos++; |
| 4309 | pTos->z = pSorter->pData; |
| 4310 | pTos->n = pSorter->nData; |
| 4311 | pTos->flags = MEM_Str|MEM_Dyn; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4312 | sqliteFree(pSorter->zKey); |
| 4313 | sqliteFree(pSorter); |
| 4314 | }else{ |
| 4315 | pc = pOp->p2 - 1; |
| 4316 | } |
| 4317 | break; |
| 4318 | } |
| 4319 | |
drh | df199a2 | 2002-06-14 22:38:41 +0000 | [diff] [blame] | 4320 | /* Opcode: SortCallback P1 * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4321 | ** |
| 4322 | ** The top of the stack contains a callback record built using |
| 4323 | ** the SortMakeRec operation with the same P1 value as this |
| 4324 | ** instruction. Pop this record from the stack and invoke the |
| 4325 | ** callback on it. |
| 4326 | */ |
| 4327 | case OP_SortCallback: { |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 4328 | assert( pTos>=p->aStack ); |
| 4329 | assert( pTos->flags & MEM_Str ); |
drh | a1f9b5e | 2004-02-14 16:31:02 +0000 | [diff] [blame] | 4330 | p->nCallback++; |
drh | 826fb5a | 2004-02-14 23:59:57 +0000 | [diff] [blame] | 4331 | p->pc = pc+1; |
| 4332 | p->azResColumn = (char**)pTos->z; |
drh | d650275 | 2004-02-16 03:44:01 +0000 | [diff] [blame] | 4333 | assert( p->nResColumn==pOp->p1 ); |
drh | 826fb5a | 2004-02-14 23:59:57 +0000 | [diff] [blame] | 4334 | p->popStack = 1; |
| 4335 | p->pTos = pTos; |
| 4336 | return SQLITE_ROW; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4337 | } |
| 4338 | |
drh | a8b38d2 | 2001-11-01 14:41:34 +0000 | [diff] [blame] | 4339 | /* Opcode: SortReset * * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4340 | ** |
drh | a8b38d2 | 2001-11-01 14:41:34 +0000 | [diff] [blame] | 4341 | ** Remove any elements that remain on the sorter. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4342 | */ |
drh | a8b38d2 | 2001-11-01 14:41:34 +0000 | [diff] [blame] | 4343 | case OP_SortReset: { |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 4344 | sqlite3VdbeSorterReset(p); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4345 | break; |
| 4346 | } |
| 4347 | |
| 4348 | /* Opcode: FileOpen * * P3 |
| 4349 | ** |
| 4350 | ** Open the file named by P3 for reading using the FileRead opcode. |
| 4351 | ** If P3 is "stdin" then open standard input for reading. |
| 4352 | */ |
| 4353 | case OP_FileOpen: { |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 4354 | assert( pOp->p3!=0 ); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4355 | if( p->pFile ){ |
| 4356 | if( p->pFile!=stdin ) fclose(p->pFile); |
| 4357 | p->pFile = 0; |
| 4358 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 4359 | if( sqlite3StrICmp(pOp->p3,"stdin")==0 ){ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4360 | p->pFile = stdin; |
| 4361 | }else{ |
| 4362 | p->pFile = fopen(pOp->p3, "r"); |
| 4363 | } |
| 4364 | if( p->pFile==0 ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 4365 | sqlite3SetString(&p->zErrMsg,"unable to open file: ", pOp->p3, (char*)0); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4366 | rc = SQLITE_ERROR; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4367 | } |
| 4368 | break; |
| 4369 | } |
| 4370 | |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4371 | /* Opcode: FileRead P1 P2 P3 |
| 4372 | ** |
| 4373 | ** Read a single line of input from the open file (the file opened using |
| 4374 | ** FileOpen). If we reach end-of-file, jump immediately to P2. If |
| 4375 | ** we are able to get another line, split the line apart using P3 as |
| 4376 | ** a delimiter. There should be P1 fields. If the input line contains |
| 4377 | ** more than P1 fields, ignore the excess. If the input line contains |
drh | b419a92 | 2002-01-30 16:17:23 +0000 | [diff] [blame] | 4378 | ** fewer than P1 fields, assume the remaining fields contain NULLs. |
| 4379 | ** |
| 4380 | ** Input ends if a line consists of just "\.". A field containing only |
| 4381 | ** "\N" is a null field. The backslash \ character can be used be used |
| 4382 | ** to escape newlines or the delimiter. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4383 | */ |
| 4384 | case OP_FileRead: { |
| 4385 | int n, eol, nField, i, c, nDelim; |
| 4386 | char *zDelim, *z; |
drh | caec2f1 | 2003-01-07 02:47:47 +0000 | [diff] [blame] | 4387 | CHECK_FOR_INTERRUPT; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4388 | if( p->pFile==0 ) goto fileread_jump; |
| 4389 | nField = pOp->p1; |
| 4390 | if( nField<=0 ) goto fileread_jump; |
| 4391 | if( nField!=p->nField || p->azField==0 ){ |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 4392 | char **azField = sqliteRealloc(p->azField, sizeof(char*)*nField+1); |
| 4393 | if( azField==0 ){ goto no_mem; } |
| 4394 | p->azField = azField; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4395 | p->nField = nField; |
| 4396 | } |
| 4397 | n = 0; |
| 4398 | eol = 0; |
| 4399 | while( eol==0 ){ |
| 4400 | if( p->zLine==0 || n+200>p->nLineAlloc ){ |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 4401 | char *zLine; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4402 | p->nLineAlloc = p->nLineAlloc*2 + 300; |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 4403 | zLine = sqliteRealloc(p->zLine, p->nLineAlloc); |
| 4404 | if( zLine==0 ){ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4405 | p->nLineAlloc = 0; |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 4406 | sqliteFree(p->zLine); |
| 4407 | p->zLine = 0; |
| 4408 | goto no_mem; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4409 | } |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 4410 | p->zLine = zLine; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4411 | } |
drh | 62160e7 | 2002-07-30 17:20:40 +0000 | [diff] [blame] | 4412 | if( vdbe_fgets(&p->zLine[n], p->nLineAlloc-n, p->pFile)==0 ){ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4413 | eol = 1; |
| 4414 | p->zLine[n] = 0; |
| 4415 | }else{ |
drh | b419a92 | 2002-01-30 16:17:23 +0000 | [diff] [blame] | 4416 | int c; |
| 4417 | while( (c = p->zLine[n])!=0 ){ |
| 4418 | if( c=='\\' ){ |
| 4419 | if( p->zLine[n+1]==0 ) break; |
| 4420 | n += 2; |
| 4421 | }else if( c=='\n' ){ |
| 4422 | p->zLine[n] = 0; |
| 4423 | eol = 1; |
| 4424 | break; |
| 4425 | }else{ |
| 4426 | n++; |
| 4427 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4428 | } |
| 4429 | } |
| 4430 | } |
| 4431 | if( n==0 ) goto fileread_jump; |
| 4432 | z = p->zLine; |
| 4433 | if( z[0]=='\\' && z[1]=='.' && z[2]==0 ){ |
| 4434 | goto fileread_jump; |
| 4435 | } |
| 4436 | zDelim = pOp->p3; |
| 4437 | if( zDelim==0 ) zDelim = "\t"; |
| 4438 | c = zDelim[0]; |
| 4439 | nDelim = strlen(zDelim); |
| 4440 | p->azField[0] = z; |
| 4441 | for(i=1; *z!=0 && i<=nField; i++){ |
| 4442 | int from, to; |
| 4443 | from = to = 0; |
drh | b419a92 | 2002-01-30 16:17:23 +0000 | [diff] [blame] | 4444 | if( z[0]=='\\' && z[1]=='N' |
| 4445 | && (z[2]==0 || strncmp(&z[2],zDelim,nDelim)==0) ){ |
| 4446 | if( i<=nField ) p->azField[i-1] = 0; |
| 4447 | z += 2 + nDelim; |
| 4448 | if( i<nField ) p->azField[i] = z; |
| 4449 | continue; |
| 4450 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4451 | while( z[from] ){ |
| 4452 | if( z[from]=='\\' && z[from+1]!=0 ){ |
drh | 3775084 | 2003-09-27 00:56:31 +0000 | [diff] [blame] | 4453 | int tx = z[from+1]; |
| 4454 | switch( tx ){ |
| 4455 | case 'b': tx = '\b'; break; |
| 4456 | case 'f': tx = '\f'; break; |
| 4457 | case 'n': tx = '\n'; break; |
| 4458 | case 'r': tx = '\r'; break; |
| 4459 | case 't': tx = '\t'; break; |
| 4460 | case 'v': tx = '\v'; break; |
| 4461 | default: break; |
| 4462 | } |
| 4463 | z[to++] = tx; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4464 | from += 2; |
| 4465 | continue; |
| 4466 | } |
| 4467 | if( z[from]==c && strncmp(&z[from],zDelim,nDelim)==0 ) break; |
| 4468 | z[to++] = z[from++]; |
| 4469 | } |
| 4470 | if( z[from] ){ |
| 4471 | z[to] = 0; |
| 4472 | z += from + nDelim; |
| 4473 | if( i<nField ) p->azField[i] = z; |
| 4474 | }else{ |
| 4475 | z[to] = 0; |
| 4476 | z = ""; |
| 4477 | } |
| 4478 | } |
| 4479 | while( i<nField ){ |
drh | b419a92 | 2002-01-30 16:17:23 +0000 | [diff] [blame] | 4480 | p->azField[i++] = 0; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4481 | } |
| 4482 | break; |
| 4483 | |
| 4484 | /* If we reach end-of-file, or if anything goes wrong, jump here. |
| 4485 | ** This code will cause a jump to P2 */ |
| 4486 | fileread_jump: |
| 4487 | pc = pOp->p2 - 1; |
| 4488 | break; |
| 4489 | } |
| 4490 | |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 4491 | /* Opcode: FileColumn P1 * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4492 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 4493 | ** 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] | 4494 | ** from the input file. |
| 4495 | */ |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 4496 | case OP_FileColumn: { |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4497 | int i = pOp->p1; |
| 4498 | char *z; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 4499 | assert( i>=0 && i<p->nField ); |
| 4500 | if( p->azField ){ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4501 | z = p->azField[i]; |
| 4502 | }else{ |
| 4503 | z = 0; |
| 4504 | } |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 4505 | pTos++; |
drh | b419a92 | 2002-01-30 16:17:23 +0000 | [diff] [blame] | 4506 | if( z ){ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 4507 | pTos->n = strlen(z) + 1; |
| 4508 | pTos->z = z; |
| 4509 | pTos->flags = MEM_Str | MEM_Ephem; |
drh | b419a92 | 2002-01-30 16:17:23 +0000 | [diff] [blame] | 4510 | }else{ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 4511 | pTos->flags = MEM_Null; |
drh | b419a92 | 2002-01-30 16:17:23 +0000 | [diff] [blame] | 4512 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4513 | break; |
| 4514 | } |
| 4515 | |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 4516 | /* Opcode: MemStore P1 P2 * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4517 | ** |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 4518 | ** Write the top of the stack into memory location P1. |
| 4519 | ** P1 should be a small integer since space is allocated |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4520 | ** for all memory locations between 0 and P1 inclusive. |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 4521 | ** |
| 4522 | ** After the data is stored in the memory location, the |
| 4523 | ** stack is popped once if P2 is 1. If P2 is zero, then |
| 4524 | ** the original data remains on the stack. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4525 | */ |
| 4526 | case OP_MemStore: { |
| 4527 | int i = pOp->p1; |
drh | 9bbca4c | 2001-11-06 04:00:18 +0000 | [diff] [blame] | 4528 | Mem *pMem; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 4529 | assert( pTos>=p->aStack ); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4530 | if( i>=p->nMem ){ |
| 4531 | int nOld = p->nMem; |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 4532 | Mem *aMem; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4533 | p->nMem = i + 5; |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 4534 | aMem = sqliteRealloc(p->aMem, p->nMem*sizeof(p->aMem[0])); |
| 4535 | if( aMem==0 ) goto no_mem; |
drh | 3e56c04 | 2002-09-17 03:20:46 +0000 | [diff] [blame] | 4536 | if( aMem!=p->aMem ){ |
| 4537 | int j; |
| 4538 | for(j=0; j<nOld; j++){ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 4539 | if( aMem[j].flags & MEM_Short ){ |
drh | 00706be | 2004-01-30 14:49:16 +0000 | [diff] [blame] | 4540 | aMem[j].z = aMem[j].zShort; |
drh | 3e56c04 | 2002-09-17 03:20:46 +0000 | [diff] [blame] | 4541 | } |
| 4542 | } |
| 4543 | } |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 4544 | p->aMem = aMem; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4545 | if( nOld<p->nMem ){ |
| 4546 | memset(&p->aMem[nOld], 0, sizeof(p->aMem[0])*(p->nMem-nOld)); |
| 4547 | } |
| 4548 | } |
drh | 2c79c67 | 2004-01-31 20:20:29 +0000 | [diff] [blame] | 4549 | Deephemeralize(pTos); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4550 | pMem = &p->aMem[i]; |
drh | 2c79c67 | 2004-01-31 20:20:29 +0000 | [diff] [blame] | 4551 | Release(pMem); |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 4552 | *pMem = *pTos; |
drh | 2c79c67 | 2004-01-31 20:20:29 +0000 | [diff] [blame] | 4553 | if( pMem->flags & MEM_Dyn ){ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 4554 | if( pOp->p2 ){ |
| 4555 | pTos->flags = MEM_Null; |
| 4556 | }else{ |
drh | 00706be | 2004-01-30 14:49:16 +0000 | [diff] [blame] | 4557 | pMem->z = sqliteMallocRaw( pMem->n ); |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 4558 | if( pMem->z==0 ) goto no_mem; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 4559 | memcpy(pMem->z, pTos->z, pMem->n); |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 4560 | } |
drh | 2c79c67 | 2004-01-31 20:20:29 +0000 | [diff] [blame] | 4561 | }else if( pMem->flags & MEM_Short ){ |
drh | 00706be | 2004-01-30 14:49:16 +0000 | [diff] [blame] | 4562 | pMem->z = pMem->zShort; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4563 | } |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 4564 | if( pOp->p2 ){ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 4565 | Release(pTos); |
| 4566 | pTos--; |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 4567 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4568 | break; |
| 4569 | } |
| 4570 | |
| 4571 | /* Opcode: MemLoad P1 * * |
| 4572 | ** |
| 4573 | ** Push a copy of the value in memory location P1 onto the stack. |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 4574 | ** |
| 4575 | ** If the value is a string, then the value pushed is a pointer to |
| 4576 | ** the string that is stored in the memory location. If the memory |
| 4577 | ** location is subsequently changed (using OP_MemStore) then the |
| 4578 | ** value pushed onto the stack will change too. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4579 | */ |
| 4580 | case OP_MemLoad: { |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4581 | int i = pOp->p1; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 4582 | assert( i>=0 && i<p->nMem ); |
| 4583 | pTos++; |
| 4584 | memcpy(pTos, &p->aMem[i], sizeof(pTos[0])-NBFS);; |
| 4585 | if( pTos->flags & MEM_Str ){ |
| 4586 | pTos->flags |= MEM_Ephem; |
| 4587 | pTos->flags &= ~(MEM_Dyn|MEM_Static|MEM_Short); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4588 | } |
| 4589 | break; |
| 4590 | } |
| 4591 | |
drh | d11d382 | 2002-06-21 23:01:49 +0000 | [diff] [blame] | 4592 | /* Opcode: MemIncr P1 P2 * |
| 4593 | ** |
| 4594 | ** Increment the integer valued memory cell P1 by 1. If P2 is not zero |
| 4595 | ** and the result after the increment is greater than zero, then jump |
| 4596 | ** to P2. |
| 4597 | ** |
| 4598 | ** This instruction throws an error if the memory cell is not initially |
| 4599 | ** an integer. |
| 4600 | */ |
| 4601 | case OP_MemIncr: { |
| 4602 | int i = pOp->p1; |
| 4603 | Mem *pMem; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 4604 | assert( i>=0 && i<p->nMem ); |
drh | d11d382 | 2002-06-21 23:01:49 +0000 | [diff] [blame] | 4605 | pMem = &p->aMem[i]; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 4606 | assert( pMem->flags==MEM_Int ); |
drh | 00706be | 2004-01-30 14:49:16 +0000 | [diff] [blame] | 4607 | pMem->i++; |
| 4608 | if( pOp->p2>0 && pMem->i>0 ){ |
drh | d11d382 | 2002-06-21 23:01:49 +0000 | [diff] [blame] | 4609 | pc = pOp->p2 - 1; |
| 4610 | } |
| 4611 | break; |
| 4612 | } |
| 4613 | |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4614 | /* Opcode: AggReset * P2 * |
| 4615 | ** |
| 4616 | ** Reset the aggregator so that it no longer contains any data. |
| 4617 | ** Future aggregator elements will contain P2 values each. |
| 4618 | */ |
| 4619 | case OP_AggReset: { |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 4620 | sqlite3VdbeAggReset(&p->agg); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4621 | p->agg.nMem = pOp->p2; |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 4622 | p->agg.apFunc = sqliteMalloc( p->agg.nMem*sizeof(p->agg.apFunc[0]) ); |
drh | caec2f1 | 2003-01-07 02:47:47 +0000 | [diff] [blame] | 4623 | if( p->agg.apFunc==0 ) goto no_mem; |
drh | e509535 | 2002-02-24 03:25:14 +0000 | [diff] [blame] | 4624 | break; |
| 4625 | } |
| 4626 | |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 4627 | /* Opcode: AggInit * P2 P3 |
drh | e509535 | 2002-02-24 03:25:14 +0000 | [diff] [blame] | 4628 | ** |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 4629 | ** Initialize the function parameters for an aggregate function. |
| 4630 | ** The aggregate will operate out of aggregate column P2. |
| 4631 | ** P3 is a pointer to the FuncDef structure for the function. |
drh | e509535 | 2002-02-24 03:25:14 +0000 | [diff] [blame] | 4632 | */ |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 4633 | case OP_AggInit: { |
drh | e509535 | 2002-02-24 03:25:14 +0000 | [diff] [blame] | 4634 | int i = pOp->p2; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 4635 | assert( i>=0 && i<p->agg.nMem ); |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 4636 | p->agg.apFunc[i] = (FuncDef*)pOp->p3; |
drh | e509535 | 2002-02-24 03:25:14 +0000 | [diff] [blame] | 4637 | break; |
| 4638 | } |
| 4639 | |
| 4640 | /* Opcode: AggFunc * P2 P3 |
| 4641 | ** |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 4642 | ** Execute the step function for an aggregate. The |
| 4643 | ** function has P2 arguments. P3 is a pointer to the FuncDef |
| 4644 | ** structure that specifies the function. |
drh | e509535 | 2002-02-24 03:25:14 +0000 | [diff] [blame] | 4645 | ** |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 4646 | ** The top of the stack must be an integer which is the index of |
| 4647 | ** the aggregate column that corresponds to this aggregate function. |
| 4648 | ** Ideally, this index would be another parameter, but there are |
| 4649 | ** no free parameters left. The integer is popped from the stack. |
drh | e509535 | 2002-02-24 03:25:14 +0000 | [diff] [blame] | 4650 | */ |
| 4651 | case OP_AggFunc: { |
| 4652 | int n = pOp->p2; |
| 4653 | int i; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 4654 | Mem *pMem, *pRec; |
| 4655 | char **azArgv = p->zArgv; |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 4656 | sqlite_func ctx; |
drh | e509535 | 2002-02-24 03:25:14 +0000 | [diff] [blame] | 4657 | |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 4658 | assert( n>=0 ); |
| 4659 | assert( pTos->flags==MEM_Int ); |
| 4660 | pRec = &pTos[-n]; |
| 4661 | assert( pRec>=p->aStack ); |
| 4662 | for(i=0; i<n; i++, pRec++){ |
| 4663 | if( pRec->flags & MEM_Null ){ |
| 4664 | azArgv[i] = 0; |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 4665 | }else{ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 4666 | Stringify(pRec); |
| 4667 | azArgv[i] = pRec->z; |
drh | e509535 | 2002-02-24 03:25:14 +0000 | [diff] [blame] | 4668 | } |
| 4669 | } |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 4670 | i = pTos->i; |
| 4671 | assert( i>=0 && i<p->agg.nMem ); |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 4672 | ctx.pFunc = (FuncDef*)pOp->p3; |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 4673 | pMem = &p->agg.pCurrent->aMem[i]; |
drh | 00706be | 2004-01-30 14:49:16 +0000 | [diff] [blame] | 4674 | ctx.s.z = pMem->zShort; /* Space used for small aggregate contexts */ |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 4675 | ctx.pAgg = pMem->z; |
drh | 00706be | 2004-01-30 14:49:16 +0000 | [diff] [blame] | 4676 | ctx.cnt = ++pMem->i; |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 4677 | ctx.isError = 0; |
| 4678 | ctx.isStep = 1; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 4679 | (ctx.pFunc->xStep)(&ctx, n, (const char**)azArgv); |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 4680 | pMem->z = ctx.pAgg; |
drh | 00706be | 2004-01-30 14:49:16 +0000 | [diff] [blame] | 4681 | pMem->flags = MEM_AggCtx; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 4682 | popStack(&pTos, n+1); |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 4683 | if( ctx.isError ){ |
| 4684 | rc = SQLITE_ERROR; |
| 4685 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4686 | break; |
| 4687 | } |
| 4688 | |
| 4689 | /* Opcode: AggFocus * P2 * |
| 4690 | ** |
| 4691 | ** Pop the top of the stack and use that as an aggregator key. If |
| 4692 | ** an aggregator with that same key already exists, then make the |
| 4693 | ** aggregator the current aggregator and jump to P2. If no aggregator |
| 4694 | ** with the given key exists, create one and make it current but |
| 4695 | ** do not jump. |
| 4696 | ** |
| 4697 | ** The order of aggregator opcodes is important. The order is: |
| 4698 | ** AggReset AggFocus AggNext. In other words, you must execute |
| 4699 | ** AggReset first, then zero or more AggFocus operations, then |
| 4700 | ** zero or more AggNext operations. You must not execute an AggFocus |
| 4701 | ** in between an AggNext and an AggReset. |
| 4702 | */ |
| 4703 | case OP_AggFocus: { |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4704 | AggElem *pElem; |
| 4705 | char *zKey; |
| 4706 | int nKey; |
| 4707 | |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 4708 | assert( pTos>=p->aStack ); |
| 4709 | Stringify(pTos); |
| 4710 | zKey = pTos->z; |
| 4711 | nKey = pTos->n; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 4712 | pElem = sqlite3HashFind(&p->agg.hash, zKey, nKey); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4713 | if( pElem ){ |
| 4714 | p->agg.pCurrent = pElem; |
| 4715 | pc = pOp->p2 - 1; |
| 4716 | }else{ |
drh | db5ed6d | 2001-09-18 22:17:44 +0000 | [diff] [blame] | 4717 | AggInsert(&p->agg, zKey, nKey); |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 4718 | if( sqlite3_malloc_failed ) goto no_mem; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4719 | } |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 4720 | Release(pTos); |
| 4721 | pTos--; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4722 | break; |
| 4723 | } |
| 4724 | |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4725 | /* Opcode: AggSet * P2 * |
| 4726 | ** |
| 4727 | ** Move the top of the stack into the P2-th field of the current |
| 4728 | ** aggregate. String values are duplicated into new memory. |
| 4729 | */ |
| 4730 | case OP_AggSet: { |
| 4731 | AggElem *pFocus = AggInFocus(p->agg); |
drh | 2c79c67 | 2004-01-31 20:20:29 +0000 | [diff] [blame] | 4732 | Mem *pMem; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4733 | int i = pOp->p2; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 4734 | assert( pTos>=p->aStack ); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4735 | if( pFocus==0 ) goto no_mem; |
drh | 2c79c67 | 2004-01-31 20:20:29 +0000 | [diff] [blame] | 4736 | assert( i>=0 && i<p->agg.nMem ); |
| 4737 | Deephemeralize(pTos); |
| 4738 | pMem = &pFocus->aMem[i]; |
| 4739 | Release(pMem); |
| 4740 | *pMem = *pTos; |
| 4741 | if( pMem->flags & MEM_Dyn ){ |
| 4742 | pTos->flags = MEM_Null; |
| 4743 | }else if( pMem->flags & MEM_Short ){ |
| 4744 | pMem->z = pMem->zShort; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4745 | } |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 4746 | Release(pTos); |
| 4747 | pTos--; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4748 | break; |
| 4749 | } |
| 4750 | |
| 4751 | /* Opcode: AggGet * P2 * |
| 4752 | ** |
| 4753 | ** Push a new entry onto the stack which is a copy of the P2-th field |
| 4754 | ** of the current aggregate. Strings are not duplicated so |
| 4755 | ** string values will be ephemeral. |
| 4756 | */ |
| 4757 | case OP_AggGet: { |
| 4758 | AggElem *pFocus = AggInFocus(p->agg); |
drh | 2c79c67 | 2004-01-31 20:20:29 +0000 | [diff] [blame] | 4759 | Mem *pMem; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4760 | int i = pOp->p2; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4761 | if( pFocus==0 ) goto no_mem; |
drh | 2c79c67 | 2004-01-31 20:20:29 +0000 | [diff] [blame] | 4762 | assert( i>=0 && i<p->agg.nMem ); |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 4763 | pTos++; |
drh | 2c79c67 | 2004-01-31 20:20:29 +0000 | [diff] [blame] | 4764 | pMem = &pFocus->aMem[i]; |
| 4765 | *pTos = *pMem; |
drh | 3914aed | 2004-01-31 20:40:42 +0000 | [diff] [blame] | 4766 | if( pTos->flags & MEM_Str ){ |
| 4767 | pTos->flags &= ~(MEM_Dyn|MEM_Static|MEM_Short); |
| 4768 | pTos->flags |= MEM_Ephem; |
| 4769 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4770 | break; |
| 4771 | } |
| 4772 | |
| 4773 | /* Opcode: AggNext * P2 * |
| 4774 | ** |
| 4775 | ** Make the next aggregate value the current aggregate. The prior |
| 4776 | ** aggregate is deleted. If all aggregate values have been consumed, |
| 4777 | ** jump to P2. |
| 4778 | ** |
| 4779 | ** The order of aggregator opcodes is important. The order is: |
| 4780 | ** AggReset AggFocus AggNext. In other words, you must execute |
| 4781 | ** AggReset first, then zero or more AggFocus operations, then |
| 4782 | ** zero or more AggNext operations. You must not execute an AggFocus |
| 4783 | ** in between an AggNext and an AggReset. |
| 4784 | */ |
| 4785 | case OP_AggNext: { |
drh | caec2f1 | 2003-01-07 02:47:47 +0000 | [diff] [blame] | 4786 | CHECK_FOR_INTERRUPT; |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 4787 | if( p->agg.pSearch==0 ){ |
| 4788 | p->agg.pSearch = sqliteHashFirst(&p->agg.hash); |
| 4789 | }else{ |
| 4790 | p->agg.pSearch = sqliteHashNext(p->agg.pSearch); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4791 | } |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 4792 | if( p->agg.pSearch==0 ){ |
| 4793 | pc = pOp->p2 - 1; |
| 4794 | } else { |
drh | e509535 | 2002-02-24 03:25:14 +0000 | [diff] [blame] | 4795 | int i; |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 4796 | sqlite_func ctx; |
drh | e509535 | 2002-02-24 03:25:14 +0000 | [diff] [blame] | 4797 | Mem *aMem; |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 4798 | p->agg.pCurrent = sqliteHashData(p->agg.pSearch); |
drh | e509535 | 2002-02-24 03:25:14 +0000 | [diff] [blame] | 4799 | aMem = p->agg.pCurrent->aMem; |
| 4800 | for(i=0; i<p->agg.nMem; i++){ |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 4801 | int freeCtx; |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 4802 | if( p->agg.apFunc[i]==0 ) continue; |
| 4803 | if( p->agg.apFunc[i]->xFinalize==0 ) continue; |
drh | 00706be | 2004-01-30 14:49:16 +0000 | [diff] [blame] | 4804 | ctx.s.flags = MEM_Null; |
| 4805 | ctx.s.z = aMem[i].zShort; |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 4806 | ctx.pAgg = (void*)aMem[i].z; |
drh | 00706be | 2004-01-30 14:49:16 +0000 | [diff] [blame] | 4807 | freeCtx = aMem[i].z && aMem[i].z!=aMem[i].zShort; |
| 4808 | ctx.cnt = aMem[i].i; |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 4809 | ctx.isStep = 0; |
| 4810 | ctx.pFunc = p->agg.apFunc[i]; |
| 4811 | (*p->agg.apFunc[i]->xFinalize)(&ctx); |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 4812 | if( freeCtx ){ |
| 4813 | sqliteFree( aMem[i].z ); |
| 4814 | } |
drh | 00706be | 2004-01-30 14:49:16 +0000 | [diff] [blame] | 4815 | aMem[i] = ctx.s; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 4816 | if( aMem[i].flags & MEM_Short ){ |
drh | 00706be | 2004-01-30 14:49:16 +0000 | [diff] [blame] | 4817 | aMem[i].z = aMem[i].zShort; |
drh | e509535 | 2002-02-24 03:25:14 +0000 | [diff] [blame] | 4818 | } |
drh | e509535 | 2002-02-24 03:25:14 +0000 | [diff] [blame] | 4819 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4820 | } |
| 4821 | break; |
| 4822 | } |
| 4823 | |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4824 | /* Opcode: SetInsert P1 * P3 |
| 4825 | ** |
| 4826 | ** If Set P1 does not exist then create it. Then insert value |
| 4827 | ** P3 into that set. If P3 is NULL, then insert the top of the |
| 4828 | ** stack into the set. |
| 4829 | */ |
| 4830 | case OP_SetInsert: { |
| 4831 | int i = pOp->p1; |
| 4832 | if( p->nSet<=i ){ |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 4833 | int k; |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 4834 | Set *aSet = sqliteRealloc(p->aSet, (i+1)*sizeof(p->aSet[0]) ); |
| 4835 | if( aSet==0 ) goto no_mem; |
| 4836 | p->aSet = aSet; |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 4837 | for(k=p->nSet; k<=i; k++){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 4838 | sqlite3HashInit(&p->aSet[k].hash, SQLITE_HASH_BINARY, 1); |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 4839 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4840 | p->nSet = i+1; |
| 4841 | } |
| 4842 | if( pOp->p3 ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 4843 | sqlite3HashInsert(&p->aSet[i].hash, pOp->p3, strlen(pOp->p3)+1, p); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4844 | }else{ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 4845 | assert( pTos>=p->aStack ); |
| 4846 | Stringify(pTos); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 4847 | sqlite3HashInsert(&p->aSet[i].hash, pTos->z, pTos->n, p); |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 4848 | Release(pTos); |
| 4849 | pTos--; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4850 | } |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 4851 | if( sqlite3_malloc_failed ) goto no_mem; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4852 | break; |
| 4853 | } |
| 4854 | |
| 4855 | /* Opcode: SetFound P1 P2 * |
| 4856 | ** |
| 4857 | ** Pop the stack once and compare the value popped off with the |
| 4858 | ** contents of set P1. If the element popped exists in set P1, |
| 4859 | ** then jump to P2. Otherwise fall through. |
| 4860 | */ |
| 4861 | case OP_SetFound: { |
| 4862 | int i = pOp->p1; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 4863 | assert( pTos>=p->aStack ); |
| 4864 | Stringify(pTos); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 4865 | if( i>=0 && i<p->nSet && sqlite3HashFind(&p->aSet[i].hash, pTos->z, pTos->n)){ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4866 | pc = pOp->p2 - 1; |
| 4867 | } |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 4868 | Release(pTos); |
| 4869 | pTos--; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4870 | break; |
| 4871 | } |
| 4872 | |
| 4873 | /* Opcode: SetNotFound P1 P2 * |
| 4874 | ** |
| 4875 | ** Pop the stack once and compare the value popped off with the |
| 4876 | ** contents of set P1. If the element popped does not exists in |
| 4877 | ** set P1, then jump to P2. Otherwise fall through. |
| 4878 | */ |
| 4879 | case OP_SetNotFound: { |
| 4880 | int i = pOp->p1; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 4881 | assert( pTos>=p->aStack ); |
| 4882 | Stringify(pTos); |
drh | 38dd0b4 | 2002-10-30 22:42:58 +0000 | [diff] [blame] | 4883 | if( i<0 || i>=p->nSet || |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 4884 | sqlite3HashFind(&p->aSet[i].hash, pTos->z, pTos->n)==0 ){ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4885 | pc = pOp->p2 - 1; |
| 4886 | } |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 4887 | Release(pTos); |
| 4888 | pTos--; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4889 | break; |
| 4890 | } |
| 4891 | |
drh | d99f706 | 2002-06-08 23:25:08 +0000 | [diff] [blame] | 4892 | /* Opcode: SetFirst P1 P2 * |
| 4893 | ** |
| 4894 | ** Read the first element from set P1 and push it onto the stack. If the |
| 4895 | ** set is empty, push nothing and jump immediately to P2. This opcode is |
| 4896 | ** used in combination with OP_SetNext to loop over all elements of a set. |
| 4897 | */ |
| 4898 | /* Opcode: SetNext P1 P2 * |
| 4899 | ** |
| 4900 | ** Read the next element from set P1 and push it onto the stack. If there |
| 4901 | ** are no more elements in the set, do not do the push and fall through. |
| 4902 | ** Otherwise, jump to P2 after pushing the next set element. |
| 4903 | */ |
| 4904 | case OP_SetFirst: |
| 4905 | case OP_SetNext: { |
| 4906 | Set *pSet; |
drh | caec2f1 | 2003-01-07 02:47:47 +0000 | [diff] [blame] | 4907 | CHECK_FOR_INTERRUPT; |
drh | 38dd0b4 | 2002-10-30 22:42:58 +0000 | [diff] [blame] | 4908 | if( pOp->p1<0 || pOp->p1>=p->nSet ){ |
| 4909 | if( pOp->opcode==OP_SetFirst ) pc = pOp->p2 - 1; |
| 4910 | break; |
| 4911 | } |
drh | d99f706 | 2002-06-08 23:25:08 +0000 | [diff] [blame] | 4912 | pSet = &p->aSet[pOp->p1]; |
| 4913 | if( pOp->opcode==OP_SetFirst ){ |
| 4914 | pSet->prev = sqliteHashFirst(&pSet->hash); |
| 4915 | if( pSet->prev==0 ){ |
| 4916 | pc = pOp->p2 - 1; |
| 4917 | break; |
| 4918 | } |
| 4919 | }else{ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 4920 | assert( pSet->prev ); |
drh | d99f706 | 2002-06-08 23:25:08 +0000 | [diff] [blame] | 4921 | pSet->prev = sqliteHashNext(pSet->prev); |
| 4922 | if( pSet->prev==0 ){ |
| 4923 | break; |
| 4924 | }else{ |
| 4925 | pc = pOp->p2 - 1; |
| 4926 | } |
| 4927 | } |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 4928 | pTos++; |
| 4929 | pTos->z = sqliteHashKey(pSet->prev); |
| 4930 | pTos->n = sqliteHashKeysize(pSet->prev); |
| 4931 | pTos->flags = MEM_Str | MEM_Ephem; |
drh | d99f706 | 2002-06-08 23:25:08 +0000 | [diff] [blame] | 4932 | break; |
| 4933 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4934 | |
drh | 6f8c91c | 2003-12-07 00:24:35 +0000 | [diff] [blame] | 4935 | /* Opcode: Vacuum * * * |
| 4936 | ** |
| 4937 | ** Vacuum the entire database. This opcode will cause other virtual |
| 4938 | ** machines to be created and run. It may not be called from within |
| 4939 | ** a transaction. |
| 4940 | */ |
| 4941 | case OP_Vacuum: { |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 4942 | if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse; |
| 4943 | rc = sqlite3RunVacuum(&p->zErrMsg, db); |
| 4944 | if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse; |
drh | 6f8c91c | 2003-12-07 00:24:35 +0000 | [diff] [blame] | 4945 | break; |
| 4946 | } |
| 4947 | |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4948 | /* An other opcode is illegal... |
| 4949 | */ |
| 4950 | default: { |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 4951 | sqlite3_snprintf(sizeof(zBuf),zBuf,"%d",pOp->opcode); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 4952 | sqlite3SetString(&p->zErrMsg, "unknown opcode ", zBuf, (char*)0); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4953 | rc = SQLITE_INTERNAL; |
| 4954 | break; |
| 4955 | } |
| 4956 | |
| 4957 | /***************************************************************************** |
| 4958 | ** The cases of the switch statement above this line should all be indented |
| 4959 | ** by 6 spaces. But the left-most 6 spaces have been removed to improve the |
| 4960 | ** readability. From this point on down, the normal indentation rules are |
| 4961 | ** restored. |
| 4962 | *****************************************************************************/ |
| 4963 | } |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 4964 | |
drh | 7b39686 | 2003-01-01 23:06:20 +0000 | [diff] [blame] | 4965 | #ifdef VDBE_PROFILE |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 4966 | { |
| 4967 | long long elapse = hwtime() - start; |
| 4968 | pOp->cycles += elapse; |
| 4969 | pOp->cnt++; |
| 4970 | #if 0 |
| 4971 | fprintf(stdout, "%10lld ", elapse); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 4972 | sqlite3VdbePrintOp(stdout, origPc, &p->aOp[origPc]); |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 4973 | #endif |
| 4974 | } |
drh | 7b39686 | 2003-01-01 23:06:20 +0000 | [diff] [blame] | 4975 | #endif |
| 4976 | |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 4977 | /* The following code adds nothing to the actual functionality |
| 4978 | ** of the program. It is only here for testing and debugging. |
| 4979 | ** On the other hand, it does burn CPU cycles every time through |
| 4980 | ** the evaluator loop. So we can leave it out when NDEBUG is defined. |
| 4981 | */ |
| 4982 | #ifndef NDEBUG |
drh | 3914aed | 2004-01-31 20:40:42 +0000 | [diff] [blame] | 4983 | /* Sanity checking on the top element of the stack */ |
| 4984 | if( pTos>=p->aStack ){ |
| 4985 | assert( pTos->flags!=0 ); /* Must define some type */ |
| 4986 | if( pTos->flags & MEM_Str ){ |
| 4987 | int x = pTos->flags & (MEM_Static|MEM_Dyn|MEM_Ephem|MEM_Short); |
| 4988 | assert( x!=0 ); /* Strings must define a string subtype */ |
| 4989 | assert( (x & (x-1))==0 ); /* Only one string subtype can be defined */ |
| 4990 | assert( pTos->z!=0 ); /* Strings must have a value */ |
| 4991 | /* Mem.z points to Mem.zShort iff the subtype is MEM_Short */ |
| 4992 | assert( (pTos->flags & MEM_Short)==0 || pTos->z==pTos->zShort ); |
| 4993 | assert( (pTos->flags & MEM_Short)!=0 || pTos->z!=pTos->zShort ); |
| 4994 | }else{ |
| 4995 | /* Cannot define a string subtype for non-string objects */ |
| 4996 | assert( (pTos->flags & (MEM_Static|MEM_Dyn|MEM_Ephem|MEM_Short))==0 ); |
| 4997 | } |
| 4998 | /* MEM_Null excludes all other types */ |
| 4999 | assert( pTos->flags==MEM_Null || (pTos->flags&MEM_Null)==0 ); |
| 5000 | } |
drh | 58b9576 | 2000-06-02 01:17:37 +0000 | [diff] [blame] | 5001 | if( pc<-1 || pc>=p->nOp ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 5002 | sqlite3SetString(&p->zErrMsg, "jump destination out of range", (char*)0); |
drh | 58b9576 | 2000-06-02 01:17:37 +0000 | [diff] [blame] | 5003 | rc = SQLITE_INTERNAL; |
| 5004 | } |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 5005 | if( p->trace && pTos>=p->aStack ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5006 | int i; |
| 5007 | fprintf(p->trace, "Stack:"); |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 5008 | for(i=0; i>-5 && &pTos[i]>=p->aStack; i--){ |
| 5009 | if( pTos[i].flags & MEM_Null ){ |
drh | c61053b | 2000-06-04 12:58:36 +0000 | [diff] [blame] | 5010 | fprintf(p->trace, " NULL"); |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 5011 | }else if( (pTos[i].flags & (MEM_Int|MEM_Str))==(MEM_Int|MEM_Str) ){ |
drh | a3b321d | 2004-05-11 09:31:31 +0000 | [diff] [blame] | 5012 | fprintf(p->trace, " si:%lld", pTos[i].i); |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 5013 | }else if( pTos[i].flags & MEM_Int ){ |
drh | a3b321d | 2004-05-11 09:31:31 +0000 | [diff] [blame] | 5014 | fprintf(p->trace, " i:%lld", pTos[i].i); |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 5015 | }else if( pTos[i].flags & MEM_Real ){ |
| 5016 | fprintf(p->trace, " r:%g", pTos[i].r); |
| 5017 | }else if( pTos[i].flags & MEM_Str ){ |
drh | 092d035 | 2001-09-15 13:15:12 +0000 | [diff] [blame] | 5018 | int j, k; |
| 5019 | char zBuf[100]; |
| 5020 | zBuf[0] = ' '; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 5021 | if( pTos[i].flags & MEM_Dyn ){ |
drh | 84e6b7b | 2001-11-12 13:10:52 +0000 | [diff] [blame] | 5022 | zBuf[1] = 'z'; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 5023 | assert( (pTos[i].flags & (MEM_Static|MEM_Ephem))==0 ); |
| 5024 | }else if( pTos[i].flags & MEM_Static ){ |
drh | 84e6b7b | 2001-11-12 13:10:52 +0000 | [diff] [blame] | 5025 | zBuf[1] = 't'; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 5026 | assert( (pTos[i].flags & (MEM_Dyn|MEM_Ephem))==0 ); |
| 5027 | }else if( pTos[i].flags & MEM_Ephem ){ |
drh | e958bb4 | 2002-10-22 15:04:34 +0000 | [diff] [blame] | 5028 | zBuf[1] = 'e'; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 5029 | assert( (pTos[i].flags & (MEM_Static|MEM_Dyn))==0 ); |
drh | 84e6b7b | 2001-11-12 13:10:52 +0000 | [diff] [blame] | 5030 | }else{ |
| 5031 | zBuf[1] = 's'; |
| 5032 | } |
drh | 872ff86 | 2001-09-15 14:43:39 +0000 | [diff] [blame] | 5033 | zBuf[2] = '['; |
drh | 092d035 | 2001-09-15 13:15:12 +0000 | [diff] [blame] | 5034 | k = 3; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 5035 | for(j=0; j<20 && j<pTos[i].n; j++){ |
| 5036 | int c = pTos[i].z[j]; |
| 5037 | if( c==0 && j==pTos[i].n-1 ) break; |
drh | 092d035 | 2001-09-15 13:15:12 +0000 | [diff] [blame] | 5038 | if( isprint(c) && !isspace(c) ){ |
| 5039 | zBuf[k++] = c; |
| 5040 | }else{ |
| 5041 | zBuf[k++] = '.'; |
| 5042 | } |
drh | c61053b | 2000-06-04 12:58:36 +0000 | [diff] [blame] | 5043 | } |
drh | 872ff86 | 2001-09-15 14:43:39 +0000 | [diff] [blame] | 5044 | zBuf[k++] = ']'; |
drh | 092d035 | 2001-09-15 13:15:12 +0000 | [diff] [blame] | 5045 | zBuf[k++] = 0; |
| 5046 | fprintf(p->trace, "%s", zBuf); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5047 | }else{ |
drh | c61053b | 2000-06-04 12:58:36 +0000 | [diff] [blame] | 5048 | fprintf(p->trace, " ???"); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5049 | } |
| 5050 | } |
drh | 7bc09d3 | 2002-11-01 01:55:36 +0000 | [diff] [blame] | 5051 | if( rc!=0 ) fprintf(p->trace," rc=%d",rc); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5052 | fprintf(p->trace,"\n"); |
| 5053 | } |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 5054 | #endif |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 5055 | } /* The end of the for(;;) loop the loops through opcodes */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5056 | |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 5057 | /* If we reach this point, it means that execution is finished. |
| 5058 | */ |
| 5059 | vdbe_halt: |
| 5060 | if( rc ){ |
| 5061 | p->rc = rc; |
| 5062 | rc = SQLITE_ERROR; |
| 5063 | }else{ |
| 5064 | rc = SQLITE_DONE; |
| 5065 | } |
| 5066 | p->magic = VDBE_MAGIC_HALT; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 5067 | p->pTos = pTos; |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 5068 | return rc; |
| 5069 | |
| 5070 | /* Jump to here if a malloc() fails. It's hard to get a malloc() |
| 5071 | ** to fail on a modern VM computer, so this code is untested. |
| 5072 | */ |
| 5073 | no_mem: |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 5074 | sqlite3SetString(&p->zErrMsg, "out of memory", (char*)0); |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 5075 | rc = SQLITE_NOMEM; |
| 5076 | goto vdbe_halt; |
| 5077 | |
| 5078 | /* Jump to here for an SQLITE_MISUSE error. |
| 5079 | */ |
| 5080 | abort_due_to_misuse: |
| 5081 | rc = SQLITE_MISUSE; |
| 5082 | /* Fall thru into abort_due_to_error */ |
| 5083 | |
| 5084 | /* Jump to here for any other kind of fatal error. The "rc" variable |
| 5085 | ** should hold the error number. |
| 5086 | */ |
| 5087 | abort_due_to_error: |
drh | 483750b | 2003-01-29 18:46:51 +0000 | [diff] [blame] | 5088 | if( p->zErrMsg==0 ){ |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 5089 | if( sqlite3_malloc_failed ) rc = SQLITE_NOMEM; |
| 5090 | sqlite3SetString(&p->zErrMsg, sqlite3_error_string(rc), (char*)0); |
drh | 483750b | 2003-01-29 18:46:51 +0000 | [diff] [blame] | 5091 | } |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 5092 | goto vdbe_halt; |
| 5093 | |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 5094 | /* Jump to here if the sqlite3_interrupt() API sets the interrupt |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 5095 | ** flag. |
| 5096 | */ |
| 5097 | abort_due_to_interrupt: |
| 5098 | assert( db->flags & SQLITE_Interrupt ); |
| 5099 | db->flags &= ~SQLITE_Interrupt; |
| 5100 | if( db->magic!=SQLITE_MAGIC_BUSY ){ |
| 5101 | rc = SQLITE_MISUSE; |
| 5102 | }else{ |
| 5103 | rc = SQLITE_INTERRUPT; |
| 5104 | } |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 5105 | sqlite3SetString(&p->zErrMsg, sqlite3_error_string(rc), (char*)0); |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 5106 | goto vdbe_halt; |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 5107 | } |