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 | ** |
danielk1977 | fc57d7b | 2004-05-26 02:04:57 +0000 | [diff] [blame] | 18 | ** In the external interface, an "sqlite3_stmt*" is an opaque pointer |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 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 | ** |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame^] | 46 | ** $Id: vdbe.c,v 1.344 2004/05/28 11:37:28 danielk1977 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 |
drh | 7cf6e4d | 2004-05-19 14:56:55 +0000 | [diff] [blame] | 55 | ** moves, either by the OP_MoveXX, OP_Next, or OP_Prev opcodes. The test |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 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 | |
danielk1977 | 7e18c25 | 2004-05-25 11:47:24 +0000 | [diff] [blame] | 72 | /* |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 73 | ** Release the memory associated with the given stack level. This |
| 74 | ** leaves the Mem.flags field in an inconsistent state. |
drh | c61053b | 2000-06-04 12:58:36 +0000 | [diff] [blame] | 75 | */ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 76 | #define Release(P) if((P)->flags&MEM_Dyn){ sqliteFree((P)->z); } |
| 77 | |
| 78 | /* |
danielk1977 | bd7e460 | 2004-05-24 07:34:48 +0000 | [diff] [blame] | 79 | ** Convert the given stack entity into a string if it isn't one |
| 80 | ** already. Return non-zero if a malloc() fails. |
| 81 | */ |
| 82 | #define Stringify(P, enc) \ |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 83 | if(((P)->flags&(MEM_Str|MEM_Blob))==0 && sqlite3VdbeMemStringify(P,enc)) \ |
| 84 | { goto no_mem; } |
danielk1977 | bd7e460 | 2004-05-24 07:34:48 +0000 | [diff] [blame] | 85 | |
| 86 | /* |
| 87 | ** Convert the given stack entity into a string that has been obtained |
| 88 | ** from sqliteMalloc(). This is different from Stringify() above in that |
| 89 | ** Stringify() will use the NBFS bytes of static string space if the string |
| 90 | ** will fit but this routine always mallocs for space. |
| 91 | ** Return non-zero if we run out of memory. |
| 92 | */ |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 93 | #define Dynamicify(P,enc) sqlite3VdbeMemDynamicify(P) |
| 94 | |
danielk1977 | bd7e460 | 2004-05-24 07:34:48 +0000 | [diff] [blame] | 95 | |
| 96 | /* |
| 97 | ** An ephemeral string value (signified by the MEM_Ephem flag) contains |
| 98 | ** a pointer to a dynamically allocated string where some other entity |
| 99 | ** is responsible for deallocating that string. Because the stack entry |
| 100 | ** does not control the string, it might be deleted without the stack |
| 101 | ** entry knowing it. |
| 102 | ** |
| 103 | ** This routine converts an ephemeral string into a dynamically allocated |
| 104 | ** string that the stack entry itself controls. In other words, it |
| 105 | ** converts an MEM_Ephem string into an MEM_Dyn string. |
| 106 | */ |
| 107 | #define Deephemeralize(P) \ |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 108 | if( ((P)->flags&MEM_Ephem)!=0 \ |
| 109 | && sqlite3VdbeMemMakeWriteable(P) ){ goto no_mem;} |
danielk1977 | 93d4675 | 2004-05-23 13:30:58 +0000 | [diff] [blame] | 110 | |
| 111 | /* |
danielk1977 | 8a6b541 | 2004-05-24 07:04:25 +0000 | [diff] [blame] | 112 | ** Convert the given stack entity into a integer if it isn't one |
| 113 | ** already. |
| 114 | ** |
| 115 | ** Any prior string or real representation is invalidated. |
| 116 | ** NULLs are converted into 0. |
| 117 | */ |
| 118 | #define Integerify(P, enc) \ |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 119 | if((P)->flags!=MEM_Int){ sqlite3VdbeMemIntegerify(P); } |
danielk1977 | 8a6b541 | 2004-05-24 07:04:25 +0000 | [diff] [blame] | 120 | |
| 121 | /* |
| 122 | ** Get a valid Real representation for the given stack element. |
| 123 | ** |
| 124 | ** Any prior string or integer representation is retained. |
| 125 | ** NULLs are converted into 0.0. |
| 126 | */ |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 127 | #define Realify(P,enc) \ |
| 128 | if(((P)->flags&MEM_Real)==0){ sqlite3VdbeMemRealify(P); } |
danielk1977 | 8a6b541 | 2004-05-24 07:04:25 +0000 | [diff] [blame] | 129 | |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 130 | /* |
| 131 | ** Argument pMem points at a memory cell that will be passed to a |
| 132 | ** user-defined function or returned to the user as the result of a query. |
| 133 | ** The second argument, 'db_enc' is the text encoding used by the vdbe for |
| 134 | ** stack variables. This routine sets the pMem->enc and pMem->type |
| 135 | ** variables used by the sqlite3_value_*() routines. |
| 136 | */ |
| 137 | static void StoreTypeInfo(Mem *pMem, u8 db_enc){ |
| 138 | int flags = pMem->flags; |
| 139 | if( flags & MEM_Null ){ |
| 140 | pMem->type = SQLITE3_NULL; |
| 141 | } |
| 142 | else if( flags & MEM_Int ){ |
| 143 | pMem->type = SQLITE3_INTEGER; |
| 144 | } |
| 145 | else if( flags & MEM_Real ){ |
| 146 | pMem->type = SQLITE3_FLOAT; |
| 147 | } |
| 148 | else if( flags & MEM_Str ){ |
| 149 | pMem->type = SQLITE3_TEXT; |
| 150 | }else{ |
| 151 | pMem->type = SQLITE3_BLOB; |
| 152 | } |
| 153 | } |
danielk1977 | 8a6b541 | 2004-05-24 07:04:25 +0000 | [diff] [blame] | 154 | |
| 155 | /* |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 156 | ** Insert a new aggregate element and make it the element that |
| 157 | ** has focus. |
| 158 | ** |
| 159 | ** Return 0 on success and 1 if memory is exhausted. |
| 160 | */ |
| 161 | static int AggInsert(Agg *p, char *zKey, int nKey){ |
| 162 | AggElem *pElem, *pOld; |
| 163 | int i; |
| 164 | Mem *pMem; |
| 165 | pElem = sqliteMalloc( sizeof(AggElem) + nKey + |
| 166 | (p->nMem-1)*sizeof(pElem->aMem[0]) ); |
| 167 | if( pElem==0 ) return 1; |
| 168 | pElem->zKey = (char*)&pElem->aMem[p->nMem]; |
| 169 | memcpy(pElem->zKey, zKey, nKey); |
| 170 | pElem->nKey = nKey; |
| 171 | pOld = sqlite3HashInsert(&p->hash, pElem->zKey, pElem->nKey, pElem); |
| 172 | if( pOld!=0 ){ |
| 173 | assert( pOld==pElem ); /* Malloc failed on insert */ |
| 174 | sqliteFree(pOld); |
| 175 | return 0; |
| 176 | } |
| 177 | for(i=0, pMem=pElem->aMem; i<p->nMem; i++, pMem++){ |
| 178 | pMem->flags = MEM_Null; |
| 179 | } |
| 180 | p->pCurrent = pElem; |
| 181 | return 0; |
| 182 | } |
| 183 | |
| 184 | /* |
| 185 | ** Get the AggElem currently in focus |
| 186 | */ |
| 187 | #define AggInFocus(P) ((P).pCurrent ? (P).pCurrent : _AggInFocus(&(P))) |
| 188 | static AggElem *_AggInFocus(Agg *p){ |
| 189 | HashElem *pElem = sqliteHashFirst(&p->hash); |
| 190 | if( pElem==0 ){ |
| 191 | AggInsert(p,"",1); |
| 192 | pElem = sqliteHashFirst(&p->hash); |
| 193 | } |
| 194 | return pElem ? sqliteHashData(pElem) : 0; |
| 195 | } |
| 196 | |
| 197 | /* |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 198 | ** Pop the stack N times. |
| 199 | */ |
| 200 | static void popStack(Mem **ppTos, int N){ |
| 201 | Mem *pTos = *ppTos; |
| 202 | while( N>0 ){ |
| 203 | N--; |
| 204 | Release(pTos); |
| 205 | pTos--; |
| 206 | } |
| 207 | *ppTos = pTos; |
| 208 | } |
| 209 | |
| 210 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 211 | ** The parameters are pointers to the head of two sorted lists |
| 212 | ** of Sorter structures. Merge these two lists together and return |
| 213 | ** a single sorted list. This routine forms the core of the merge-sort |
| 214 | ** algorithm. |
| 215 | ** |
| 216 | ** In the case of a tie, left sorts in front of right. |
| 217 | */ |
drh | ffbc308 | 2004-05-21 01:29:06 +0000 | [diff] [blame] | 218 | static Sorter *Merge(Sorter *pLeft, Sorter *pRight, KeyInfo *pKeyInfo){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 219 | Sorter sHead; |
| 220 | Sorter *pTail; |
| 221 | pTail = &sHead; |
| 222 | pTail->pNext = 0; |
| 223 | while( pLeft && pRight ){ |
drh | ffbc308 | 2004-05-21 01:29:06 +0000 | [diff] [blame] | 224 | int c = sqlite3VdbeKeyCompare(pKeyInfo, pLeft->nKey, pLeft->zKey, |
| 225 | pRight->nKey, pRight->zKey); |
| 226 | /* int c = sqlite3SortCompare(pLeft->zKey, pRight->zKey); */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 227 | if( c<=0 ){ |
| 228 | pTail->pNext = pLeft; |
| 229 | pLeft = pLeft->pNext; |
| 230 | }else{ |
| 231 | pTail->pNext = pRight; |
| 232 | pRight = pRight->pNext; |
| 233 | } |
| 234 | pTail = pTail->pNext; |
| 235 | } |
| 236 | if( pLeft ){ |
| 237 | pTail->pNext = pLeft; |
| 238 | }else if( pRight ){ |
| 239 | pTail->pNext = pRight; |
| 240 | } |
| 241 | return sHead.pNext; |
| 242 | } |
| 243 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 244 | /* |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 245 | ** Make sure there is space in the Vdbe structure to hold at least |
| 246 | ** mxCursor cursors. If there is not currently enough space, then |
| 247 | ** allocate more. |
| 248 | ** |
| 249 | ** If a memory allocation error occurs, return 1. Return 0 if |
| 250 | ** everything works. |
| 251 | */ |
| 252 | static int expandCursorArraySize(Vdbe *p, int mxCursor){ |
| 253 | if( mxCursor>=p->nCursor ){ |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame] | 254 | p->apCsr = sqliteRealloc( p->apCsr, (mxCursor+1)*sizeof(Cursor*) ); |
| 255 | if( p->apCsr==0 ) return 1; |
| 256 | while( p->nCursor<=mxCursor ){ |
| 257 | Cursor *pC; |
| 258 | p->apCsr[p->nCursor++] = pC = sqliteMalloc( sizeof(Cursor) ); |
| 259 | if( pC==0 ) return 1; |
| 260 | } |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 261 | } |
| 262 | return 0; |
| 263 | } |
| 264 | |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 265 | /* |
| 266 | ** Apply any conversion required by the supplied column affinity to |
| 267 | ** memory cell pRec. affinity may be one of: |
| 268 | ** |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 269 | ** SQLITE_AFF_NUMERIC |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 270 | ** SQLITE_AFF_TEXT |
| 271 | ** SQLITE_AFF_NONE |
| 272 | ** SQLITE_AFF_INTEGER |
| 273 | ** |
| 274 | */ |
danielk1977 | 8a6b541 | 2004-05-24 07:04:25 +0000 | [diff] [blame] | 275 | static void applyAffinity(Mem *pRec, char affinity, u8 enc){ |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 276 | switch( affinity ){ |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 277 | case SQLITE_AFF_INTEGER: |
| 278 | case SQLITE_AFF_NUMERIC: |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 279 | if( 0==(pRec->flags&(MEM_Real|MEM_Int)) ){ |
| 280 | /* pRec does not have a valid integer or real representation. |
| 281 | ** Attempt a conversion if pRec has a string representation and |
| 282 | ** it looks like a number. |
| 283 | */ |
| 284 | int realnum; |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 285 | sqlite3VdbeMemNulTerminate(pRec); |
danielk1977 | 8a6b541 | 2004-05-24 07:04:25 +0000 | [diff] [blame] | 286 | if( pRec->flags&MEM_Str && sqlite3IsNumber(pRec->z, &realnum, enc) ){ |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 287 | if( realnum ){ |
danielk1977 | 8a6b541 | 2004-05-24 07:04:25 +0000 | [diff] [blame] | 288 | Realify(pRec, enc); |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 289 | }else{ |
danielk1977 | 8a6b541 | 2004-05-24 07:04:25 +0000 | [diff] [blame] | 290 | Integerify(pRec, enc); |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 291 | } |
| 292 | } |
| 293 | } |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 294 | |
| 295 | if( affinity==SQLITE_AFF_INTEGER ){ |
| 296 | /* For INTEGER affinity, try to convert a real value to an int */ |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 297 | if( (pRec->flags&MEM_Real) && !(pRec->flags&MEM_Int) ){ |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 298 | pRec->i = pRec->r; |
| 299 | if( ((double)pRec->i)==pRec->r ){ |
| 300 | pRec->flags |= MEM_Int; |
| 301 | } |
| 302 | } |
| 303 | } |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 304 | break; |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 305 | |
| 306 | case SQLITE_AFF_TEXT: |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 307 | /* Only attempt the conversion if there is an integer or real |
| 308 | ** representation (blob and NULL do not get converted) but no string |
| 309 | ** representation. |
| 310 | */ |
| 311 | if( 0==(pRec->flags&MEM_Str) && (pRec->flags&(MEM_Real|MEM_Int)) ){ |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 312 | sqlite3VdbeMemStringify(pRec, enc); |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 313 | } |
| 314 | pRec->flags &= ~(MEM_Real|MEM_Int); |
| 315 | |
| 316 | break; |
| 317 | |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 318 | case SQLITE_AFF_NONE: |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 319 | /* Affinity NONE. Do nothing. */ |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 320 | break; |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 321 | |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 322 | default: |
| 323 | assert(0); |
| 324 | } |
| 325 | } |
| 326 | |
danielk1977 | b1bc953 | 2004-05-22 03:05:33 +0000 | [diff] [blame] | 327 | #ifndef NDEBUG |
drh | b6f5452 | 2004-05-20 02:42:16 +0000 | [diff] [blame] | 328 | /* |
danielk1977 | ca6b291 | 2004-05-21 10:49:47 +0000 | [diff] [blame] | 329 | ** Write a nice string representation of the contents of cell pMem |
| 330 | ** into buffer zBuf, length nBuf. |
| 331 | */ |
danielk1977 | ca6b291 | 2004-05-21 10:49:47 +0000 | [diff] [blame] | 332 | void prettyPrintMem(Mem *pMem, char *zBuf, int nBuf){ |
| 333 | char *zCsr = zBuf; |
| 334 | int f = pMem->flags; |
| 335 | |
| 336 | if( f&MEM_Blob ){ |
| 337 | int i; |
| 338 | char c; |
| 339 | if( f & MEM_Dyn ){ |
| 340 | c = 'z'; |
| 341 | assert( (f & (MEM_Static|MEM_Ephem))==0 ); |
| 342 | }else if( f & MEM_Static ){ |
| 343 | c = 't'; |
| 344 | assert( (f & (MEM_Dyn|MEM_Ephem))==0 ); |
| 345 | }else if( f & MEM_Ephem ){ |
| 346 | c = 'e'; |
| 347 | assert( (f & (MEM_Static|MEM_Dyn))==0 ); |
| 348 | }else{ |
| 349 | c = 's'; |
| 350 | } |
| 351 | |
danielk1977 | b1bc953 | 2004-05-22 03:05:33 +0000 | [diff] [blame] | 352 | zCsr += sprintf(zCsr, "%c", c); |
| 353 | zCsr += sprintf(zCsr, "%d[", pMem->n); |
danielk1977 | ca6b291 | 2004-05-21 10:49:47 +0000 | [diff] [blame] | 354 | for(i=0; i<16 && i<pMem->n; i++){ |
| 355 | zCsr += sprintf(zCsr, "%02X ", ((int)pMem->z[i] & 0xFF)); |
| 356 | } |
| 357 | for(i=0; i<16 && i<pMem->n; i++){ |
| 358 | char z = pMem->z[i]; |
| 359 | if( z<32 || z>126 ) *zCsr++ = '.'; |
| 360 | else *zCsr++ = z; |
| 361 | } |
| 362 | |
| 363 | zCsr += sprintf(zCsr, "]"); |
danielk1977 | b1bc953 | 2004-05-22 03:05:33 +0000 | [diff] [blame] | 364 | *zCsr = '\0'; |
| 365 | }else if( f & MEM_Str ){ |
| 366 | int j, k; |
| 367 | zBuf[0] = ' '; |
| 368 | if( f & MEM_Dyn ){ |
| 369 | zBuf[1] = 'z'; |
| 370 | assert( (f & (MEM_Static|MEM_Ephem))==0 ); |
| 371 | }else if( f & MEM_Static ){ |
| 372 | zBuf[1] = 't'; |
| 373 | assert( (f & (MEM_Dyn|MEM_Ephem))==0 ); |
| 374 | }else if( f & MEM_Ephem ){ |
| 375 | zBuf[1] = 'e'; |
| 376 | assert( (f & (MEM_Static|MEM_Dyn))==0 ); |
| 377 | }else{ |
| 378 | zBuf[1] = 's'; |
| 379 | } |
| 380 | k = 2; |
| 381 | k += sprintf(&zBuf[k], "%d", pMem->n); |
| 382 | zBuf[k++] = '['; |
| 383 | for(j=0; j<15 && j<pMem->n; j++){ |
| 384 | u8 c = pMem->z[j]; |
danielk1977 | b1bc953 | 2004-05-22 03:05:33 +0000 | [diff] [blame] | 385 | /* |
danielk1977 | 8a6b541 | 2004-05-24 07:04:25 +0000 | [diff] [blame] | 386 | if( c==0 && j==pMem->n-1 ) break; |
danielk1977 | b1bc953 | 2004-05-22 03:05:33 +0000 | [diff] [blame] | 387 | zBuf[k++] = "0123456789ABCDEF"[c>>4]; |
| 388 | zBuf[k++] = "0123456789ABCDEF"[c&0xf]; |
| 389 | */ |
| 390 | if( c>=0x20 && c<0x7f ){ |
| 391 | zBuf[k++] = c; |
| 392 | }else{ |
| 393 | zBuf[k++] = '.'; |
| 394 | } |
| 395 | } |
| 396 | zBuf[k++] = ']'; |
| 397 | zBuf[k++] = 0; |
danielk1977 | ca6b291 | 2004-05-21 10:49:47 +0000 | [diff] [blame] | 398 | } |
danielk1977 | ca6b291 | 2004-05-21 10:49:47 +0000 | [diff] [blame] | 399 | } |
danielk1977 | b1bc953 | 2004-05-22 03:05:33 +0000 | [diff] [blame] | 400 | |
| 401 | /* Temporary - this is useful in conjunction with prettyPrintMem whilst |
| 402 | ** debugging. |
| 403 | */ |
| 404 | char zGdbBuf[100]; |
danielk1977 | ca6b291 | 2004-05-21 10:49:47 +0000 | [diff] [blame] | 405 | #endif |
| 406 | |
danielk1977 | 84ac9d0 | 2004-05-18 09:58:06 +0000 | [diff] [blame] | 407 | |
drh | 7b39686 | 2003-01-01 23:06:20 +0000 | [diff] [blame] | 408 | #ifdef VDBE_PROFILE |
| 409 | /* |
| 410 | ** The following routine only works on pentium-class processors. |
| 411 | ** It uses the RDTSC opcode to read cycle count value out of the |
| 412 | ** processor and returns that value. This can be used for high-res |
| 413 | ** profiling. |
| 414 | */ |
| 415 | __inline__ unsigned long long int hwtime(void){ |
| 416 | unsigned long long int x; |
| 417 | __asm__("rdtsc\n\t" |
| 418 | "mov %%edx, %%ecx\n\t" |
| 419 | :"=A" (x)); |
| 420 | return x; |
| 421 | } |
| 422 | #endif |
| 423 | |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 424 | /* |
drh | caec2f1 | 2003-01-07 02:47:47 +0000 | [diff] [blame] | 425 | ** The CHECK_FOR_INTERRUPT macro defined here looks to see if the |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 426 | ** sqlite3_interrupt() routine has been called. If it has been, then |
drh | caec2f1 | 2003-01-07 02:47:47 +0000 | [diff] [blame] | 427 | ** processing of the VDBE program is interrupted. |
| 428 | ** |
| 429 | ** This macro added to every instruction that does a jump in order to |
| 430 | ** implement a loop. This test used to be on every single instruction, |
| 431 | ** but that meant we more testing that we needed. By only testing the |
| 432 | ** flag on jump instructions, we get a (small) speed improvement. |
| 433 | */ |
| 434 | #define CHECK_FOR_INTERRUPT \ |
| 435 | if( db->flags & SQLITE_Interrupt ) goto abort_due_to_interrupt; |
| 436 | |
| 437 | |
| 438 | /* |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 439 | ** Execute as much of a VDBE program as we can then return. |
| 440 | ** |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 441 | ** sqlite3VdbeMakeReady() must be called before this routine in order to |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 442 | ** close the program with a final OP_Halt and to set up the callbacks |
| 443 | ** and the error message pointer. |
| 444 | ** |
| 445 | ** Whenever a row or result data is available, this routine will either |
| 446 | ** invoke the result callback (if there is one) or return with |
drh | 326dce7 | 2003-01-29 14:06:07 +0000 | [diff] [blame] | 447 | ** SQLITE_ROW. |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 448 | ** |
| 449 | ** If an attempt is made to open a locked database, then this routine |
| 450 | ** will either invoke the busy callback (if there is one) or it will |
| 451 | ** return SQLITE_BUSY. |
| 452 | ** |
| 453 | ** If an error occurs, an error message is written to memory obtained |
| 454 | ** from sqliteMalloc() and p->zErrMsg is made to point to that memory. |
| 455 | ** The error code is stored in p->rc and this routine returns SQLITE_ERROR. |
| 456 | ** |
| 457 | ** If the callback ever returns non-zero, then the program exits |
| 458 | ** immediately. There will be no error message but the p->rc field is |
| 459 | ** set to SQLITE_ABORT and this routine will return SQLITE_ERROR. |
| 460 | ** |
drh | 9468c7f | 2003-03-07 19:50:07 +0000 | [diff] [blame] | 461 | ** A memory allocation error causes p->rc to be set to SQLITE_NOMEM and this |
| 462 | ** routine to return SQLITE_ERROR. |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 463 | ** |
| 464 | ** Other fatal errors return SQLITE_ERROR. |
| 465 | ** |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 466 | ** After this routine has finished, sqlite3VdbeFinalize() should be |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 467 | ** used to clean up the mess that was left behind. |
| 468 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 469 | int sqlite3VdbeExec( |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 470 | Vdbe *p /* The VDBE */ |
| 471 | ){ |
| 472 | int pc; /* The program counter */ |
| 473 | Op *pOp; /* Current operation */ |
| 474 | int rc = SQLITE_OK; /* Value to return */ |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 475 | sqlite *db = p->db; /* The database */ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 476 | Mem *pTos; /* Top entry in the operand stack */ |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 477 | char zBuf[100]; /* Space to sprintf() an integer */ |
| 478 | #ifdef VDBE_PROFILE |
| 479 | unsigned long long start; /* CPU clock count at start of opcode */ |
| 480 | int origPc; /* Program counter at start of opcode */ |
| 481 | #endif |
danielk1977 | 348bb5d | 2003-10-18 09:37:26 +0000 | [diff] [blame] | 482 | #ifndef SQLITE_OMIT_PROGRESS_CALLBACK |
| 483 | int nProgressOps = 0; /* Opcodes executed since progress callback. */ |
| 484 | #endif |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 485 | |
| 486 | if( p->magic!=VDBE_MAGIC_RUN ) return SQLITE_MISUSE; |
| 487 | assert( db->magic==SQLITE_MAGIC_BUSY ); |
drh | 3a84069 | 2003-01-29 22:58:26 +0000 | [diff] [blame] | 488 | assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY ); |
| 489 | p->rc = SQLITE_OK; |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 490 | assert( p->explain==0 ); |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 491 | if( sqlite3_malloc_failed ) goto no_mem; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 492 | pTos = p->pTos; |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 493 | if( p->popStack ){ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 494 | popStack(&pTos, p->popStack); |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 495 | p->popStack = 0; |
| 496 | } |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 497 | p->resOnStack = 0; |
drh | 9358164 | 2004-02-12 13:02:55 +0000 | [diff] [blame] | 498 | CHECK_FOR_INTERRUPT; |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 499 | for(pc=p->pc; rc==SQLITE_OK; pc++){ |
drh | caec2f1 | 2003-01-07 02:47:47 +0000 | [diff] [blame] | 500 | assert( pc>=0 && pc<p->nOp ); |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 501 | assert( pTos<=&p->aStack[pc] ); |
drh | 7b39686 | 2003-01-01 23:06:20 +0000 | [diff] [blame] | 502 | #ifdef VDBE_PROFILE |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 503 | origPc = pc; |
drh | 7b39686 | 2003-01-01 23:06:20 +0000 | [diff] [blame] | 504 | start = hwtime(); |
| 505 | #endif |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 506 | pOp = &p->aOp[pc]; |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 507 | |
| 508 | /* Only allow tracing if NDEBUG is not defined. |
| 509 | */ |
| 510 | #ifndef NDEBUG |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 511 | if( p->trace ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 512 | sqlite3VdbePrintOp(p->trace, pc, pOp); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 513 | } |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 514 | #endif |
| 515 | |
drh | f603871 | 2004-02-08 18:07:34 +0000 | [diff] [blame] | 516 | /* Check to see if we need to simulate an interrupt. This only happens |
| 517 | ** if we have a special test build. |
| 518 | */ |
| 519 | #ifdef SQLITE_TEST |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 520 | if( sqlite3_interrupt_count>0 ){ |
| 521 | sqlite3_interrupt_count--; |
| 522 | if( sqlite3_interrupt_count==0 ){ |
| 523 | sqlite3_interrupt(db); |
drh | f603871 | 2004-02-08 18:07:34 +0000 | [diff] [blame] | 524 | } |
| 525 | } |
| 526 | #endif |
| 527 | |
danielk1977 | 348bb5d | 2003-10-18 09:37:26 +0000 | [diff] [blame] | 528 | #ifndef SQLITE_OMIT_PROGRESS_CALLBACK |
| 529 | /* Call the progress callback if it is configured and the required number |
| 530 | ** of VDBE ops have been executed (either since this invocation of |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 531 | ** sqlite3VdbeExec() or since last time the progress callback was called). |
danielk1977 | 348bb5d | 2003-10-18 09:37:26 +0000 | [diff] [blame] | 532 | ** If the progress callback returns non-zero, exit the virtual machine with |
| 533 | ** a return code SQLITE_ABORT. |
| 534 | */ |
drh | 3914aed | 2004-01-31 20:40:42 +0000 | [diff] [blame] | 535 | if( db->xProgress ){ |
| 536 | if( db->nProgressOps==nProgressOps ){ |
| 537 | if( db->xProgress(db->pProgressArg)!=0 ){ |
| 538 | rc = SQLITE_ABORT; |
| 539 | continue; /* skip to the next iteration of the for loop */ |
| 540 | } |
| 541 | nProgressOps = 0; |
danielk1977 | 348bb5d | 2003-10-18 09:37:26 +0000 | [diff] [blame] | 542 | } |
drh | 3914aed | 2004-01-31 20:40:42 +0000 | [diff] [blame] | 543 | nProgressOps++; |
danielk1977 | 348bb5d | 2003-10-18 09:37:26 +0000 | [diff] [blame] | 544 | } |
danielk1977 | 348bb5d | 2003-10-18 09:37:26 +0000 | [diff] [blame] | 545 | #endif |
| 546 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 547 | switch( pOp->opcode ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 548 | |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 549 | /***************************************************************************** |
| 550 | ** What follows is a massive switch statement where each case implements a |
| 551 | ** separate instruction in the virtual machine. If we follow the usual |
| 552 | ** indentation conventions, each case should be indented by 6 spaces. But |
| 553 | ** that is a lot of wasted space on the left margin. So the code within |
| 554 | ** the switch statement will break with convention and be flush-left. Another |
| 555 | ** big comment (similar to this one) will mark the point in the code where |
| 556 | ** we transition back to normal indentation. |
drh | ac82fcf | 2002-09-08 17:23:41 +0000 | [diff] [blame] | 557 | ** |
| 558 | ** The formatting of each case is important. The makefile for SQLite |
| 559 | ** generates two C files "opcodes.h" and "opcodes.c" by scanning this |
| 560 | ** file looking for lines that begin with "case OP_". The opcodes.h files |
| 561 | ** will be filled with #defines that give unique integer values to each |
| 562 | ** opcode and the opcodes.c file is filled with an array of strings where |
| 563 | ** each string is the symbolic name for the corresponding opcode. |
| 564 | ** |
| 565 | ** Documentation about VDBE opcodes is generated by scanning this file |
| 566 | ** for lines of that contain "Opcode:". That line and all subsequent |
| 567 | ** comment lines are used in the generation of the opcode.html documentation |
| 568 | ** file. |
| 569 | ** |
| 570 | ** SUMMARY: |
| 571 | ** |
| 572 | ** Formatting is important to scripts that scan this file. |
| 573 | ** Do not deviate from the formatting style currently in use. |
| 574 | ** |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 575 | *****************************************************************************/ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 576 | |
drh | 58a1168 | 2001-11-10 13:51:08 +0000 | [diff] [blame] | 577 | /* Opcode: Goto * P2 * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 578 | ** |
| 579 | ** An unconditional jump to address P2. |
| 580 | ** The next instruction executed will be |
| 581 | ** the one at index P2 from the beginning of |
| 582 | ** the program. |
| 583 | */ |
| 584 | case OP_Goto: { |
drh | caec2f1 | 2003-01-07 02:47:47 +0000 | [diff] [blame] | 585 | CHECK_FOR_INTERRUPT; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 586 | pc = pOp->p2 - 1; |
| 587 | break; |
| 588 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 589 | |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 590 | /* Opcode: Gosub * P2 * |
| 591 | ** |
| 592 | ** Push the current address plus 1 onto the return address stack |
| 593 | ** and then jump to address P2. |
| 594 | ** |
| 595 | ** The return address stack is of limited depth. If too many |
| 596 | ** OP_Gosub operations occur without intervening OP_Returns, then |
| 597 | ** the return address stack will fill up and processing will abort |
| 598 | ** with a fatal error. |
| 599 | */ |
| 600 | case OP_Gosub: { |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 601 | if( p->returnDepth>=sizeof(p->returnStack)/sizeof(p->returnStack[0]) ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 602 | sqlite3SetString(&p->zErrMsg, "return address stack overflow", (char*)0); |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 603 | p->rc = SQLITE_INTERNAL; |
| 604 | return SQLITE_ERROR; |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 605 | } |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 606 | p->returnStack[p->returnDepth++] = pc+1; |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 607 | pc = pOp->p2 - 1; |
| 608 | break; |
| 609 | } |
| 610 | |
| 611 | /* Opcode: Return * * * |
| 612 | ** |
| 613 | ** Jump immediately to the next instruction after the last unreturned |
| 614 | ** OP_Gosub. If an OP_Return has occurred for all OP_Gosubs, then |
| 615 | ** processing aborts with a fatal error. |
| 616 | */ |
| 617 | case OP_Return: { |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 618 | if( p->returnDepth<=0 ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 619 | sqlite3SetString(&p->zErrMsg, "return address stack underflow", (char*)0); |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 620 | p->rc = SQLITE_INTERNAL; |
| 621 | return SQLITE_ERROR; |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 622 | } |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 623 | p->returnDepth--; |
| 624 | pc = p->returnStack[p->returnDepth] - 1; |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 625 | break; |
| 626 | } |
| 627 | |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 628 | /* Opcode: Halt P1 P2 * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 629 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 630 | ** Exit immediately. All open cursors, Lists, Sorts, etc are closed |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 631 | ** automatically. |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 632 | ** |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 633 | ** P1 is the result code returned by sqlite3_exec(). For a normal |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 634 | ** halt, this should be SQLITE_OK (0). For errors, it can be some |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 635 | ** other value. If P1!=0 then P2 will determine whether or not to |
| 636 | ** rollback the current transaction. Do not rollback if P2==OE_Fail. |
| 637 | ** Do the rollback if P2==OE_Rollback. If P2==OE_Abort, then back |
| 638 | ** out all changes that have occurred during this execution of the |
drh | b798fa6 | 2002-09-03 19:43:23 +0000 | [diff] [blame] | 639 | ** VDBE, but do not rollback the transaction. |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 640 | ** |
| 641 | ** 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] | 642 | ** every program. So a jump past the last instruction of the program |
| 643 | ** is the same as executing Halt. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 644 | */ |
| 645 | case OP_Halt: { |
drh | 483750b | 2003-01-29 18:46:51 +0000 | [diff] [blame] | 646 | p->magic = VDBE_MAGIC_HALT; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 647 | p->pTos = pTos; |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 648 | if( pOp->p1!=SQLITE_OK ){ |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 649 | p->rc = pOp->p1; |
| 650 | p->errorAction = pOp->p2; |
danielk1977 | 6f34903 | 2002-06-11 02:25:40 +0000 | [diff] [blame] | 651 | if( pOp->p3 ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 652 | sqlite3SetString(&p->zErrMsg, pOp->p3, (char*)0); |
danielk1977 | 6f34903 | 2002-06-11 02:25:40 +0000 | [diff] [blame] | 653 | } |
drh | 483750b | 2003-01-29 18:46:51 +0000 | [diff] [blame] | 654 | return SQLITE_ERROR; |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 655 | }else{ |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 656 | p->rc = SQLITE_OK; |
drh | 483750b | 2003-01-29 18:46:51 +0000 | [diff] [blame] | 657 | return SQLITE_DONE; |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 658 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 659 | } |
drh | c61053b | 2000-06-04 12:58:36 +0000 | [diff] [blame] | 660 | |
drh | e684090 | 2002-03-06 03:08:25 +0000 | [diff] [blame] | 661 | /* Opcode: Integer P1 * P3 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 662 | ** |
drh | e684090 | 2002-03-06 03:08:25 +0000 | [diff] [blame] | 663 | ** The integer value P1 is pushed onto the stack. If P3 is not zero |
| 664 | ** then it is assumed to be a string representation of the same integer. |
drh | fec19aa | 2004-05-19 20:41:03 +0000 | [diff] [blame] | 665 | ** If P1 is zero and P3 is not zero, then the value is derived from P3. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 666 | */ |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 667 | case OP_Integer: { |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 668 | pTos++; |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 669 | if( pOp->p3==0 ){ |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 670 | pTos->flags = MEM_Int; |
danielk1977 | 93d4675 | 2004-05-23 13:30:58 +0000 | [diff] [blame] | 671 | pTos->i = pOp->p1; |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 672 | pTos->type = SQLITE3_INTEGER; |
| 673 | }else{ |
| 674 | pTos->flags = MEM_Str|MEM_Static|MEM_Term; |
| 675 | pTos->z = pOp->p3; |
| 676 | pTos->n = strlen(pTos->z); |
| 677 | pTos->enc = TEXT_Utf8; |
| 678 | Integerify(pTos, 0); |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 679 | } |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 680 | break; |
| 681 | } |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 682 | |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 683 | /* Opcode: Real * * P3 |
| 684 | ** |
| 685 | ** The string value P3 is converted to a real and pushed on to the stack. |
| 686 | */ |
| 687 | case OP_Real: { |
| 688 | pTos++; |
| 689 | pTos->flags = MEM_Str|MEM_Static|MEM_Term; |
| 690 | pTos->z = pOp->p3; |
| 691 | pTos->n = strlen(pTos->z); |
| 692 | pTos->enc = TEXT_Utf8; |
| 693 | Realify(pTos, 0); |
| 694 | break; |
| 695 | } |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame^] | 696 | |
| 697 | #if 0 |
| 698 | /* Opcode: String8 * * P3 |
| 699 | ** |
| 700 | ** This opcode does not exist at vdbe execution time. |
| 701 | */ |
| 702 | case OP_String8: { |
| 703 | break; |
| 704 | } |
| 705 | #endif |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 706 | |
| 707 | /* Opcode: String * * P3 |
| 708 | ** |
| 709 | ** The string value P3 is pushed onto the stack. If P3==0 then a |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame^] | 710 | ** NULL is pushed onto the stack. P3 is assumed to be a nul terminated |
| 711 | ** string encoded with the database native encoding. |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 712 | */ |
| 713 | case OP_String: { |
| 714 | pTos++; |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 715 | if( pOp->p3 ){ |
| 716 | pTos->flags = MEM_Str|MEM_Static|MEM_Term; |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 717 | pTos->z = pOp->p3; |
| 718 | pTos->n = strlen(pTos->z); |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame^] | 719 | pTos->enc = TEXT_Utf8; |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 720 | sqlite3VdbeChangeEncoding(pTos, db->enc); |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame^] | 721 | /* |
| 722 | if( db->enc==TEXT_Utf8 ){ |
| 723 | pTos->n = strlen(pTos->z); |
| 724 | }else{ |
| 725 | pTos->n = sqlite3utf16ByteLen(pTos->z, -1); |
| 726 | } |
| 727 | pTos->enc = db->enc; |
| 728 | */ |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 729 | }else{ |
| 730 | pTos->flags = MEM_Null; |
| 731 | } |
| 732 | break; |
| 733 | } |
| 734 | |
| 735 | #if 0 |
| 736 | /* Opcode: HexBlob * * P3 |
| 737 | ** |
| 738 | ** This opcode does not exist at vdbe execution time. |
| 739 | */ |
| 740 | case OP_HexBlob: { |
| 741 | break; |
| 742 | } |
| 743 | #endif |
| 744 | |
| 745 | /* Opcode: Blob P1 * P3 |
| 746 | ** |
| 747 | ** P3 points to a blob of data P1 bytes long. Push this |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame^] | 748 | ** value onto the stack. This instruction is not coded directly |
| 749 | ** by the compiler. Instead, the compiler layer specifies |
| 750 | ** an OP_HexBlob opcode, with the hex string representation of |
| 751 | ** the blob as P3. This opcode is transformed to an OP_Blob |
| 752 | ** before execution (within the sqlite3_prepare() function). |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 753 | */ |
| 754 | case OP_Blob: { |
| 755 | pTos++; |
| 756 | sqlite3VdbeMemSetStr(pTos, pOp->p3, pOp->p1, 0, 0); |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 757 | break; |
| 758 | } |
| 759 | |
drh | 5045789 | 2003-09-06 01:10:47 +0000 | [diff] [blame] | 760 | /* Opcode: Variable P1 * * |
| 761 | ** |
| 762 | ** Push the value of variable P1 onto the stack. A variable is |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 763 | ** an unknown in the original SQL string as handed to sqlite3_compile(). |
drh | 7c972de | 2003-09-06 22:18:07 +0000 | [diff] [blame] | 764 | ** Any occurance of the '?' character in the original SQL is considered |
| 765 | ** a variable. Variables in the SQL string are number from left to |
| 766 | ** right beginning with 1. The values of variables are set using the |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 767 | ** sqlite3_bind() API. |
drh | 5045789 | 2003-09-06 01:10:47 +0000 | [diff] [blame] | 768 | */ |
| 769 | case OP_Variable: { |
drh | 7c972de | 2003-09-06 22:18:07 +0000 | [diff] [blame] | 770 | int j = pOp->p1 - 1; |
danielk1977 | 295ba55 | 2004-05-19 10:34:51 +0000 | [diff] [blame] | 771 | assert( j>=0 && j<p->nVar ); |
| 772 | |
danielk1977 | 295ba55 | 2004-05-19 10:34:51 +0000 | [diff] [blame] | 773 | pTos++; |
danielk1977 | 93d4675 | 2004-05-23 13:30:58 +0000 | [diff] [blame] | 774 | memcpy(pTos, &p->apVar[j], sizeof(*pTos)-NBFS); |
danielk1977 | 295ba55 | 2004-05-19 10:34:51 +0000 | [diff] [blame] | 775 | if( pTos->flags&(MEM_Str|MEM_Blob) ){ |
| 776 | pTos->flags &= ~(MEM_Dyn|MEM_Ephem|MEM_Short); |
| 777 | pTos->flags |= MEM_Static; |
| 778 | } |
danielk1977 | 93d4675 | 2004-05-23 13:30:58 +0000 | [diff] [blame] | 779 | break; |
| 780 | } |
danielk1977 | 295ba55 | 2004-05-19 10:34:51 +0000 | [diff] [blame] | 781 | |
danielk1977 | 93d4675 | 2004-05-23 13:30:58 +0000 | [diff] [blame] | 782 | /* Opcode: Utf16le_8 * * * |
| 783 | ** |
| 784 | ** The element on the top of the stack must be a little-endian UTF-16 |
| 785 | ** encoded string. It is translated in-place to UTF-8. |
| 786 | */ |
| 787 | case OP_Utf16le_8: { |
| 788 | rc = SQLITE_INTERNAL; |
| 789 | break; |
| 790 | } |
| 791 | |
| 792 | /* Opcode: Utf16be_8 * * * |
| 793 | ** |
| 794 | ** The element on the top of the stack must be a big-endian UTF-16 |
| 795 | ** encoded string. It is translated in-place to UTF-8. |
| 796 | */ |
| 797 | case OP_Utf16be_8: { |
| 798 | rc = SQLITE_INTERNAL; |
| 799 | break; |
| 800 | } |
| 801 | |
| 802 | /* Opcode: Utf8_16be * * * |
| 803 | ** |
| 804 | ** The element on the top of the stack must be a UTF-8 encoded |
| 805 | ** string. It is translated to big-endian UTF-16. |
| 806 | */ |
| 807 | case OP_Utf8_16be: { |
| 808 | rc = SQLITE_INTERNAL; |
| 809 | break; |
| 810 | } |
| 811 | |
| 812 | /* Opcode: Utf8_16le * * * |
| 813 | ** |
| 814 | ** The element on the top of the stack must be a UTF-8 encoded |
| 815 | ** string. It is translated to little-endian UTF-16. |
| 816 | */ |
| 817 | case OP_Utf8_16le: { |
| 818 | rc = SQLITE_INTERNAL; |
| 819 | break; |
| 820 | } |
| 821 | |
| 822 | /* |
| 823 | ** Opcode: UtfSwab |
| 824 | ** |
| 825 | ** The element on the top of the stack must be an UTF-16 encoded |
| 826 | ** string. Every second byte is exchanged, so as to translate |
| 827 | ** the string from little-endian to big-endian or vice versa. |
| 828 | */ |
| 829 | case OP_UtfSwab: { |
| 830 | rc = SQLITE_INTERNAL; |
drh | 5045789 | 2003-09-06 01:10:47 +0000 | [diff] [blame] | 831 | break; |
| 832 | } |
| 833 | |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 834 | /* Opcode: Pop P1 * * |
| 835 | ** |
| 836 | ** P1 elements are popped off of the top of stack and discarded. |
| 837 | */ |
| 838 | case OP_Pop: { |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 839 | assert( pOp->p1>=0 ); |
| 840 | popStack(&pTos, pOp->p1); |
| 841 | assert( pTos>=&p->aStack[-1] ); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 842 | break; |
| 843 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 844 | |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 845 | /* Opcode: Dup P1 P2 * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 846 | ** |
| 847 | ** A copy of the P1-th element of the stack |
| 848 | ** is made and pushed onto the top of the stack. |
| 849 | ** The top of the stack is element 0. So the |
| 850 | ** instruction "Dup 0 0 0" will make a copy of the |
| 851 | ** top of the stack. |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 852 | ** |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 853 | ** If the content of the P1-th element is a dynamically |
| 854 | ** allocated string, then a new copy of that string |
| 855 | ** is made if P2==0. If P2!=0, then just a pointer |
| 856 | ** to the string is copied. |
| 857 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 858 | ** Also see the Pull instruction. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 859 | */ |
| 860 | case OP_Dup: { |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 861 | Mem *pFrom = &pTos[-pOp->p1]; |
| 862 | assert( pFrom<=pTos && pFrom>=p->aStack ); |
| 863 | pTos++; |
| 864 | memcpy(pTos, pFrom, sizeof(*pFrom)-NBFS); |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 865 | if( pTos->flags & (MEM_Str|MEM_Blob) ){ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 866 | if( pOp->p2 && (pTos->flags & (MEM_Dyn|MEM_Ephem)) ){ |
| 867 | pTos->flags &= ~MEM_Dyn; |
| 868 | pTos->flags |= MEM_Ephem; |
| 869 | }else if( pTos->flags & MEM_Short ){ |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 870 | memcpy(pTos->zShort, pFrom->zShort, pTos->n+2); |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 871 | pTos->z = pTos->zShort; |
| 872 | }else if( (pTos->flags & MEM_Static)==0 ){ |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 873 | pTos->z = sqliteMallocRaw(pFrom->n+2); |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 874 | if( sqlite3_malloc_failed ) goto no_mem; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 875 | memcpy(pTos->z, pFrom->z, pFrom->n); |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 876 | memcpy(&pTos->z[pTos->n], "\0", 2); |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 877 | pTos->flags &= ~(MEM_Static|MEM_Ephem|MEM_Short); |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 878 | pTos->flags |= MEM_Dyn|MEM_Term; |
drh | 9bbca4c | 2001-11-06 04:00:18 +0000 | [diff] [blame] | 879 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 880 | } |
| 881 | break; |
| 882 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 883 | |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 884 | /* Opcode: Pull P1 * * |
| 885 | ** |
| 886 | ** The P1-th element is removed from its current location on |
| 887 | ** the stack and pushed back on top of the stack. The |
| 888 | ** top of the stack is element 0, so "Pull 0 0 0" is |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 889 | ** a no-op. "Pull 1 0 0" swaps the top two elements of |
| 890 | ** the stack. |
| 891 | ** |
| 892 | ** See also the Dup instruction. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 893 | */ |
| 894 | case OP_Pull: { |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 895 | Mem *pFrom = &pTos[-pOp->p1]; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 896 | int i; |
drh | 00706be | 2004-01-30 14:49:16 +0000 | [diff] [blame] | 897 | Mem ts; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 898 | |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 899 | ts = *pFrom; |
| 900 | Deephemeralize(pTos); |
| 901 | for(i=0; i<pOp->p1; i++, pFrom++){ |
| 902 | Deephemeralize(&pFrom[1]); |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 903 | assert( (pFrom->flags & MEM_Ephem)==0 ); |
danielk1977 | 18f4189 | 2004-05-22 07:27:46 +0000 | [diff] [blame] | 904 | *pFrom = pFrom[1]; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 905 | if( pFrom->flags & MEM_Short ){ |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 906 | assert( pFrom->flags & (MEM_Str|MEM_Blob) ); |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 907 | assert( pFrom->z==pFrom[1].zShort ); |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 908 | pFrom->z = pFrom->zShort; |
drh | 9bbca4c | 2001-11-06 04:00:18 +0000 | [diff] [blame] | 909 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 910 | } |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 911 | *pTos = ts; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 912 | if( pTos->flags & MEM_Short ){ |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 913 | assert( pTos->flags & (MEM_Str|MEM_Blob) ); |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 914 | assert( pTos->z==pTos[-pOp->p1].zShort ); |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 915 | pTos->z = pTos->zShort; |
drh | 9bbca4c | 2001-11-06 04:00:18 +0000 | [diff] [blame] | 916 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 917 | break; |
| 918 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 919 | |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 920 | /* Opcode: Push P1 * * |
| 921 | ** |
| 922 | ** Overwrite the value of the P1-th element down on the |
| 923 | ** stack (P1==0 is the top of the stack) with the value |
drh | ac82fcf | 2002-09-08 17:23:41 +0000 | [diff] [blame] | 924 | ** of the top of the stack. Then pop the top of the stack. |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 925 | */ |
| 926 | case OP_Push: { |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 927 | Mem *pTo = &pTos[-pOp->p1]; |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 928 | |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 929 | assert( pTo>=p->aStack ); |
| 930 | Deephemeralize(pTos); |
| 931 | Release(pTo); |
| 932 | *pTo = *pTos; |
| 933 | if( pTo->flags & MEM_Short ){ |
| 934 | assert( pTo->z==pTos->zShort ); |
| 935 | pTo->z = pTo->zShort; |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 936 | } |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 937 | pTos--; |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 938 | break; |
| 939 | } |
| 940 | |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 941 | |
drh | d650275 | 2004-02-16 03:44:01 +0000 | [diff] [blame] | 942 | /* Opcode: ColumnName P1 P2 P3 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 943 | ** |
| 944 | ** P3 becomes the P1-th column name (first is 0). An array of pointers |
| 945 | ** to all column names is passed as the 4th parameter to the callback. |
drh | d650275 | 2004-02-16 03:44:01 +0000 | [diff] [blame] | 946 | ** If P2==1 then this is the last column in the result set and thus the |
| 947 | ** number of columns in the result set will be P1. There must be at least |
| 948 | ** one OP_ColumnName with a P2==1 before invoking OP_Callback and the |
| 949 | ** number of columns specified in OP_Callback must one more than the P1 |
| 950 | ** value of the OP_ColumnName that has P2==1. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 951 | */ |
| 952 | case OP_ColumnName: { |
danielk1977 | 3cf8606 | 2004-05-26 10:11:05 +0000 | [diff] [blame] | 953 | assert(0); |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 954 | assert( pOp->p1>=0 && pOp->p1<p->nOp ); |
drh | b136320 | 2002-06-26 02:45:03 +0000 | [diff] [blame] | 955 | p->azColName[pOp->p1] = pOp->p3; |
drh | 6a53534 | 2001-10-19 16:44:56 +0000 | [diff] [blame] | 956 | p->nCallback = 0; |
danielk1977 | 22322fd | 2004-05-25 23:35:17 +0000 | [diff] [blame] | 957 | assert( !pOp->p2 || p->nResColumn==(pOp->p1+1) ); |
| 958 | /* if( pOp->p2 ) p->nResColumn = pOp->p1+1; */ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 959 | break; |
| 960 | } |
drh | c61053b | 2000-06-04 12:58:36 +0000 | [diff] [blame] | 961 | |
drh | df199a2 | 2002-06-14 22:38:41 +0000 | [diff] [blame] | 962 | /* Opcode: Callback P1 * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 963 | ** |
| 964 | ** Pop P1 values off the stack and form them into an array. Then |
| 965 | ** invoke the callback function using the newly formed array as the |
| 966 | ** 3rd parameter. |
| 967 | */ |
| 968 | case OP_Callback: { |
danielk1977 | 8a6b541 | 2004-05-24 07:04:25 +0000 | [diff] [blame] | 969 | int i; |
drh | d650275 | 2004-02-16 03:44:01 +0000 | [diff] [blame] | 970 | assert( p->nResColumn==pOp->p1 ); |
danielk1977 | 8a6b541 | 2004-05-24 07:04:25 +0000 | [diff] [blame] | 971 | |
| 972 | for(i=0; i<pOp->p1; i++){ |
| 973 | Mem *pVal = &pTos[0-i]; |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 974 | sqlite3VdbeMemNulTerminate(pVal); |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 975 | StoreTypeInfo(pVal, db->enc); |
danielk1977 | 8a6b541 | 2004-05-24 07:04:25 +0000 | [diff] [blame] | 976 | } |
| 977 | |
| 978 | p->resOnStack = 1; |
| 979 | p->nCallback++; |
drh | 826fb5a | 2004-02-14 23:59:57 +0000 | [diff] [blame] | 980 | p->popStack = pOp->p1; |
| 981 | p->pc = pc + 1; |
| 982 | p->pTos = pTos; |
| 983 | return SQLITE_ROW; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 984 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 985 | |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 986 | /* Opcode: Concat P1 P2 P3 |
| 987 | ** |
| 988 | ** Look at the first P1 elements of the stack. Append them all |
| 989 | ** together with the lowest element first. Use P3 as a separator. |
| 990 | ** Put the result on the top of the stack. The original P1 elements |
drh | a9f9d1c | 2002-06-29 02:20:08 +0000 | [diff] [blame] | 991 | ** are popped from the stack if P2==0 and retained if P2==1. If |
| 992 | ** any element of the stack is NULL, then the result is NULL. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 993 | ** |
| 994 | ** If P3 is NULL, then use no separator. When P1==1, this routine |
| 995 | ** makes a copy of the top stack element into memory obtained |
| 996 | ** from sqliteMalloc(). |
| 997 | */ |
| 998 | case OP_Concat: { |
| 999 | char *zNew; |
| 1000 | int nByte; |
| 1001 | int nField; |
| 1002 | int i, j; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1003 | Mem *pTerm; |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 1004 | Mem mSep; /* Memory cell containing the seperator string, if any */ |
danielk1977 | 8a6b541 | 2004-05-24 07:04:25 +0000 | [diff] [blame] | 1005 | |
| 1006 | /* FIX ME: Eventually, P3 will be in database native encoding. But for |
| 1007 | ** now it is always UTF-8. So set up zSep to hold the native encoding of |
| 1008 | ** P3. |
| 1009 | */ |
| 1010 | if( pOp->p3 ){ |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 1011 | mSep.z = pOp->p3; |
| 1012 | mSep.n = strlen(mSep.z); |
| 1013 | mSep.flags = MEM_Str|MEM_Static|MEM_Term; |
| 1014 | mSep.enc = TEXT_Utf8; |
| 1015 | sqlite3VdbeChangeEncoding(&mSep, db->enc); |
danielk1977 | 8a6b541 | 2004-05-24 07:04:25 +0000 | [diff] [blame] | 1016 | }else{ |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 1017 | mSep.flags = MEM_Null; |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 1018 | mSep.n = 0; |
danielk1977 | 8a6b541 | 2004-05-24 07:04:25 +0000 | [diff] [blame] | 1019 | } |
| 1020 | |
| 1021 | /* Loop through the stack elements to see how long the result will be. */ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1022 | nField = pOp->p1; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1023 | pTerm = &pTos[1-nField]; |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 1024 | nByte = (nField-1)*mSep.n; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1025 | for(i=0; i<nField; i++, pTerm++){ |
danielk1977 | 8a6b541 | 2004-05-24 07:04:25 +0000 | [diff] [blame] | 1026 | assert( pOp->p2==0 || (pTerm->flags&MEM_Str) ); |
| 1027 | if( pTerm->flags&MEM_Null ){ |
drh | a9f9d1c | 2002-06-29 02:20:08 +0000 | [diff] [blame] | 1028 | nByte = -1; |
| 1029 | break; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1030 | } |
danielk1977 | 8a6b541 | 2004-05-24 07:04:25 +0000 | [diff] [blame] | 1031 | Stringify(pTerm, db->enc); |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 1032 | nByte += pTerm->n; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1033 | } |
danielk1977 | 8a6b541 | 2004-05-24 07:04:25 +0000 | [diff] [blame] | 1034 | |
drh | a9f9d1c | 2002-06-29 02:20:08 +0000 | [diff] [blame] | 1035 | if( nByte<0 ){ |
danielk1977 | 8a6b541 | 2004-05-24 07:04:25 +0000 | [diff] [blame] | 1036 | /* If nByte is less than zero, then there is a NULL value on the stack. |
| 1037 | ** In this case just pop the values off the stack (if required) and |
| 1038 | ** push on a NULL. |
| 1039 | */ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1040 | if( pOp->p2==0 ){ |
| 1041 | popStack(&pTos, nField); |
| 1042 | } |
| 1043 | pTos++; |
| 1044 | pTos->flags = MEM_Null; |
danielk1977 | 8a6b541 | 2004-05-24 07:04:25 +0000 | [diff] [blame] | 1045 | }else{ |
| 1046 | /* Otherwise malloc() space for the result and concatenate all the |
| 1047 | ** stack values. |
| 1048 | */ |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 1049 | zNew = sqliteMallocRaw( nByte+2 ); |
danielk1977 | 8a6b541 | 2004-05-24 07:04:25 +0000 | [diff] [blame] | 1050 | if( zNew==0 ) goto no_mem; |
| 1051 | j = 0; |
| 1052 | pTerm = &pTos[1-nField]; |
| 1053 | for(i=j=0; i<nField; i++, pTerm++){ |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 1054 | int n = pTerm->n; |
danielk1977 | 8a6b541 | 2004-05-24 07:04:25 +0000 | [diff] [blame] | 1055 | assert( pTerm->flags & MEM_Str ); |
| 1056 | memcpy(&zNew[j], pTerm->z, n); |
| 1057 | j += n; |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 1058 | if( i<nField-1 && !(mSep.flags|MEM_Null) ){ |
| 1059 | memcpy(&zNew[j], mSep.z, mSep.n); |
| 1060 | j += mSep.n; |
danielk1977 | 8a6b541 | 2004-05-24 07:04:25 +0000 | [diff] [blame] | 1061 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1062 | } |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 1063 | zNew[j] = 0; |
| 1064 | zNew[j+1] = 0; |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 1065 | assert( j==nByte ); |
danielk1977 | 8a6b541 | 2004-05-24 07:04:25 +0000 | [diff] [blame] | 1066 | |
| 1067 | if( pOp->p2==0 ){ |
| 1068 | popStack(&pTos, nField); |
| 1069 | } |
| 1070 | pTos++; |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 1071 | pTos->n = j; |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 1072 | pTos->flags = MEM_Str|MEM_Dyn|MEM_Term; |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 1073 | pTos->enc = db->enc; |
| 1074 | pTos->type = SQLITE3_TEXT; |
danielk1977 | 8a6b541 | 2004-05-24 07:04:25 +0000 | [diff] [blame] | 1075 | pTos->z = zNew; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1076 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1077 | break; |
| 1078 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1079 | |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1080 | /* Opcode: Add * * * |
| 1081 | ** |
| 1082 | ** Pop the top two elements from the stack, add them together, |
| 1083 | ** and push the result back onto the stack. If either element |
| 1084 | ** is a string then it is converted to a double using the atof() |
| 1085 | ** function before the addition. |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1086 | ** If either operand is NULL, the result is NULL. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1087 | */ |
| 1088 | /* Opcode: Multiply * * * |
| 1089 | ** |
| 1090 | ** Pop the top two elements from the stack, multiply them together, |
| 1091 | ** and push the result back onto the stack. If either element |
| 1092 | ** is a string then it is converted to a double using the atof() |
| 1093 | ** function before the multiplication. |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1094 | ** If either operand is NULL, the result is NULL. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1095 | */ |
| 1096 | /* Opcode: Subtract * * * |
| 1097 | ** |
| 1098 | ** Pop the top two elements from the stack, subtract the |
| 1099 | ** first (what was on top of the stack) from the second (the |
| 1100 | ** next on stack) |
| 1101 | ** and push the result back onto the stack. If either element |
| 1102 | ** is a string then it is converted to a double using the atof() |
| 1103 | ** function before the subtraction. |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1104 | ** If either operand is NULL, the result is NULL. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1105 | */ |
| 1106 | /* Opcode: Divide * * * |
| 1107 | ** |
| 1108 | ** Pop the top two elements from the stack, divide the |
| 1109 | ** first (what was on top of the stack) from the second (the |
| 1110 | ** next on stack) |
| 1111 | ** and push the result back onto the stack. If either element |
| 1112 | ** is a string then it is converted to a double using the atof() |
| 1113 | ** function before the division. Division by zero returns NULL. |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1114 | ** If either operand is NULL, the result is NULL. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1115 | */ |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1116 | /* Opcode: Remainder * * * |
| 1117 | ** |
| 1118 | ** Pop the top two elements from the stack, divide the |
| 1119 | ** first (what was on top of the stack) from the second (the |
| 1120 | ** next on stack) |
| 1121 | ** and push the remainder after division onto the stack. If either element |
| 1122 | ** is a string then it is converted to a double using the atof() |
| 1123 | ** function before the division. Division by zero returns NULL. |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1124 | ** If either operand is NULL, the result is NULL. |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1125 | */ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1126 | case OP_Add: |
| 1127 | case OP_Subtract: |
| 1128 | case OP_Multiply: |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1129 | case OP_Divide: |
| 1130 | case OP_Remainder: { |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1131 | Mem *pNos = &pTos[-1]; |
| 1132 | assert( pNos>=p->aStack ); |
| 1133 | if( ((pTos->flags | pNos->flags) & MEM_Null)!=0 ){ |
| 1134 | Release(pTos); |
| 1135 | pTos--; |
| 1136 | Release(pTos); |
| 1137 | pTos->flags = MEM_Null; |
| 1138 | }else if( (pTos->flags & pNos->flags & MEM_Int)==MEM_Int ){ |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1139 | i64 a, b; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1140 | a = pTos->i; |
| 1141 | b = pNos->i; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1142 | switch( pOp->opcode ){ |
| 1143 | case OP_Add: b += a; break; |
| 1144 | case OP_Subtract: b -= a; break; |
| 1145 | case OP_Multiply: b *= a; break; |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1146 | case OP_Divide: { |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1147 | if( a==0 ) goto divide_by_zero; |
| 1148 | b /= a; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1149 | break; |
| 1150 | } |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1151 | default: { |
| 1152 | if( a==0 ) goto divide_by_zero; |
| 1153 | b %= a; |
| 1154 | break; |
| 1155 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1156 | } |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1157 | Release(pTos); |
| 1158 | pTos--; |
| 1159 | Release(pTos); |
| 1160 | pTos->i = b; |
| 1161 | pTos->flags = MEM_Int; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1162 | }else{ |
| 1163 | double a, b; |
danielk1977 | 8a6b541 | 2004-05-24 07:04:25 +0000 | [diff] [blame] | 1164 | Realify(pTos, db->enc); |
| 1165 | Realify(pNos, db->enc); |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1166 | a = pTos->r; |
| 1167 | b = pNos->r; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1168 | switch( pOp->opcode ){ |
| 1169 | case OP_Add: b += a; break; |
| 1170 | case OP_Subtract: b -= a; break; |
| 1171 | case OP_Multiply: b *= a; break; |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1172 | case OP_Divide: { |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1173 | if( a==0.0 ) goto divide_by_zero; |
| 1174 | b /= a; |
| 1175 | break; |
| 1176 | } |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1177 | default: { |
drh | 1ab4300 | 2002-01-14 09:28:19 +0000 | [diff] [blame] | 1178 | int ia = (int)a; |
| 1179 | int ib = (int)b; |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1180 | if( ia==0.0 ) goto divide_by_zero; |
| 1181 | b = ib % ia; |
| 1182 | break; |
| 1183 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1184 | } |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1185 | Release(pTos); |
| 1186 | pTos--; |
| 1187 | Release(pTos); |
| 1188 | pTos->r = b; |
| 1189 | pTos->flags = MEM_Real; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1190 | } |
| 1191 | break; |
| 1192 | |
| 1193 | divide_by_zero: |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1194 | Release(pTos); |
| 1195 | pTos--; |
| 1196 | Release(pTos); |
| 1197 | pTos->flags = MEM_Null; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1198 | break; |
| 1199 | } |
| 1200 | |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 1201 | /* Opcode: Function P1 * P3 |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 1202 | ** |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 1203 | ** Invoke a user function (P3 is a pointer to a Function structure that |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 1204 | ** defines the function) with P1 string arguments taken from the stack. |
| 1205 | ** Pop all arguments from the stack and push back the result. |
| 1206 | ** |
| 1207 | ** See also: AggFunc |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 1208 | */ |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 1209 | case OP_Function: { |
danielk1977 | 51ad0ec | 2004-05-24 12:39:02 +0000 | [diff] [blame] | 1210 | int i; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1211 | Mem *pArg; |
danielk1977 | 22322fd | 2004-05-25 23:35:17 +0000 | [diff] [blame] | 1212 | sqlite3_context ctx; |
danielk1977 | 51ad0ec | 2004-05-24 12:39:02 +0000 | [diff] [blame] | 1213 | sqlite3_value **apVal; |
| 1214 | int n = pOp->p1; |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 1215 | |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 1216 | n = pOp->p1; |
danielk1977 | 6ddcca5 | 2004-05-24 23:48:25 +0000 | [diff] [blame] | 1217 | apVal = p->apArg; |
danielk1977 | 51ad0ec | 2004-05-24 12:39:02 +0000 | [diff] [blame] | 1218 | assert( apVal || n==0 ); |
| 1219 | |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1220 | pArg = &pTos[1-n]; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1221 | for(i=0; i<n; i++, pArg++){ |
danielk1977 | 51ad0ec | 2004-05-24 12:39:02 +0000 | [diff] [blame] | 1222 | apVal[i] = pArg; |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 1223 | StoreTypeInfo(pArg, db->enc); |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 1224 | } |
danielk1977 | 51ad0ec | 2004-05-24 12:39:02 +0000 | [diff] [blame] | 1225 | |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 1226 | ctx.pFunc = (FuncDef*)pOp->p3; |
drh | 00706be | 2004-01-30 14:49:16 +0000 | [diff] [blame] | 1227 | ctx.s.flags = MEM_Null; |
| 1228 | ctx.s.z = 0; |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 1229 | ctx.isError = 0; |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 1230 | ctx.isStep = 0; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1231 | if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse; |
danielk1977 | 51ad0ec | 2004-05-24 12:39:02 +0000 | [diff] [blame] | 1232 | (*ctx.pFunc->xFunc)(&ctx, n, apVal); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1233 | if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1234 | popStack(&pTos, n); |
danielk1977 | 7e18c25 | 2004-05-25 11:47:24 +0000 | [diff] [blame] | 1235 | |
| 1236 | /* Copy the result of the function to the top of the stack */ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1237 | pTos++; |
| 1238 | *pTos = ctx.s; |
| 1239 | if( pTos->flags & MEM_Short ){ |
| 1240 | pTos->z = pTos->zShort; |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 1241 | } |
danielk1977 | 7e18c25 | 2004-05-25 11:47:24 +0000 | [diff] [blame] | 1242 | /* If the function returned an error, throw an exception */ |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 1243 | if( ctx.isError ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1244 | sqlite3SetString(&p->zErrMsg, |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1245 | (pTos->flags & MEM_Str)!=0 ? pTos->z : "user function error", (char*)0); |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 1246 | rc = SQLITE_ERROR; |
| 1247 | } |
| 1248 | break; |
| 1249 | } |
| 1250 | |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1251 | /* Opcode: BitAnd * * * |
| 1252 | ** |
| 1253 | ** Pop the top two elements from the stack. Convert both elements |
| 1254 | ** to integers. Push back onto the stack the bit-wise AND of the |
| 1255 | ** two elements. |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1256 | ** If either operand is NULL, the result is NULL. |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1257 | */ |
| 1258 | /* Opcode: BitOr * * * |
| 1259 | ** |
| 1260 | ** Pop the top two elements from the stack. Convert both elements |
| 1261 | ** to integers. Push back onto the stack the bit-wise OR of the |
| 1262 | ** two elements. |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1263 | ** If either operand is NULL, the result is NULL. |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1264 | */ |
| 1265 | /* Opcode: ShiftLeft * * * |
| 1266 | ** |
| 1267 | ** Pop the top two elements from the stack. Convert both elements |
| 1268 | ** to integers. Push back onto the stack the top element shifted |
| 1269 | ** left by N bits where N is the second element on the stack. |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1270 | ** If either operand is NULL, the result is NULL. |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1271 | */ |
| 1272 | /* Opcode: ShiftRight * * * |
| 1273 | ** |
| 1274 | ** Pop the top two elements from the stack. Convert both elements |
| 1275 | ** to integers. Push back onto the stack the top element shifted |
| 1276 | ** right by N bits where N is the second element on the stack. |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1277 | ** If either operand is NULL, the result is NULL. |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1278 | */ |
| 1279 | case OP_BitAnd: |
| 1280 | case OP_BitOr: |
| 1281 | case OP_ShiftLeft: |
| 1282 | case OP_ShiftRight: { |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1283 | Mem *pNos = &pTos[-1]; |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1284 | int a, b; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1285 | |
| 1286 | assert( pNos>=p->aStack ); |
| 1287 | if( (pTos->flags | pNos->flags) & MEM_Null ){ |
| 1288 | popStack(&pTos, 2); |
| 1289 | pTos++; |
| 1290 | pTos->flags = MEM_Null; |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1291 | break; |
| 1292 | } |
danielk1977 | 8a6b541 | 2004-05-24 07:04:25 +0000 | [diff] [blame] | 1293 | Integerify(pTos, db->enc); |
| 1294 | Integerify(pNos, db->enc); |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1295 | a = pTos->i; |
| 1296 | b = pNos->i; |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1297 | switch( pOp->opcode ){ |
| 1298 | case OP_BitAnd: a &= b; break; |
| 1299 | case OP_BitOr: a |= b; break; |
| 1300 | case OP_ShiftLeft: a <<= b; break; |
| 1301 | case OP_ShiftRight: a >>= b; break; |
| 1302 | default: /* CANT HAPPEN */ break; |
| 1303 | } |
danielk1977 | 8a6b541 | 2004-05-24 07:04:25 +0000 | [diff] [blame] | 1304 | /* FIX ME: Because constant P3 values sometimes need to be translated, |
| 1305 | ** the following assert() can fail. When P3 is always in the native text |
| 1306 | ** encoding, this assert() will be valid again. Until then, the Release() |
| 1307 | ** is neeed instead. |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1308 | assert( (pTos->flags & MEM_Dyn)==0 ); |
| 1309 | assert( (pNos->flags & MEM_Dyn)==0 ); |
danielk1977 | 8a6b541 | 2004-05-24 07:04:25 +0000 | [diff] [blame] | 1310 | */ |
| 1311 | Release(pTos); |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1312 | pTos--; |
drh | 79f14b7 | 2004-03-03 01:51:24 +0000 | [diff] [blame] | 1313 | Release(pTos); |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1314 | pTos->i = a; |
drh | 79f14b7 | 2004-03-03 01:51:24 +0000 | [diff] [blame] | 1315 | pTos->flags = MEM_Int; |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1316 | break; |
| 1317 | } |
| 1318 | |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1319 | /* Opcode: AddImm P1 * * |
| 1320 | ** |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 1321 | ** Add the value P1 to whatever is on top of the stack. The result |
| 1322 | ** is always an integer. |
| 1323 | ** |
| 1324 | ** 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] | 1325 | */ |
| 1326 | case OP_AddImm: { |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1327 | assert( pTos>=p->aStack ); |
danielk1977 | 8a6b541 | 2004-05-24 07:04:25 +0000 | [diff] [blame] | 1328 | Integerify(pTos, db->enc); |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1329 | pTos->i += pOp->p1; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1330 | break; |
| 1331 | } |
| 1332 | |
drh | 751f412 | 2004-01-14 21:59:22 +0000 | [diff] [blame] | 1333 | /* Opcode: ForceInt P1 P2 * |
drh | 1dd59e0 | 2003-07-06 17:22:25 +0000 | [diff] [blame] | 1334 | ** |
drh | 751f412 | 2004-01-14 21:59:22 +0000 | [diff] [blame] | 1335 | ** Convert the top of the stack into an integer. If the current top of |
| 1336 | ** the stack is not numeric (meaning that is is a NULL or a string that |
| 1337 | ** does not look like an integer or floating point number) then pop the |
| 1338 | ** stack and jump to P2. If the top of the stack is numeric then |
| 1339 | ** convert it into the least integer that is greater than or equal to its |
| 1340 | ** current value if P1==0, or to the least integer that is strictly |
| 1341 | ** greater than its current value if P1==1. |
drh | 1dd59e0 | 2003-07-06 17:22:25 +0000 | [diff] [blame] | 1342 | */ |
drh | 751f412 | 2004-01-14 21:59:22 +0000 | [diff] [blame] | 1343 | case OP_ForceInt: { |
drh | 751f412 | 2004-01-14 21:59:22 +0000 | [diff] [blame] | 1344 | int v; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1345 | assert( pTos>=p->aStack ); |
danielk1977 | 8a6b541 | 2004-05-24 07:04:25 +0000 | [diff] [blame] | 1346 | if( (pTos->flags & (MEM_Int|MEM_Real))==0 && ((pTos->flags & MEM_Str)==0 |
| 1347 | || sqlite3IsNumber(pTos->z, 0, db->enc)==0) ){ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1348 | Release(pTos); |
| 1349 | pTos--; |
drh | 1dd59e0 | 2003-07-06 17:22:25 +0000 | [diff] [blame] | 1350 | pc = pOp->p2 - 1; |
drh | 751f412 | 2004-01-14 21:59:22 +0000 | [diff] [blame] | 1351 | break; |
drh | 1dd59e0 | 2003-07-06 17:22:25 +0000 | [diff] [blame] | 1352 | } |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1353 | if( pTos->flags & MEM_Int ){ |
| 1354 | v = pTos->i + (pOp->p1!=0); |
drh | 751f412 | 2004-01-14 21:59:22 +0000 | [diff] [blame] | 1355 | }else{ |
danielk1977 | 8a6b541 | 2004-05-24 07:04:25 +0000 | [diff] [blame] | 1356 | Realify(pTos, db->enc); |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1357 | v = (int)pTos->r; |
| 1358 | if( pTos->r>(double)v ) v++; |
| 1359 | if( pOp->p1 && pTos->r==(double)v ) v++; |
drh | 751f412 | 2004-01-14 21:59:22 +0000 | [diff] [blame] | 1360 | } |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1361 | Release(pTos); |
| 1362 | pTos->i = v; |
| 1363 | pTos->flags = MEM_Int; |
drh | 1dd59e0 | 2003-07-06 17:22:25 +0000 | [diff] [blame] | 1364 | break; |
| 1365 | } |
| 1366 | |
drh | f1351b6 | 2002-07-31 19:50:26 +0000 | [diff] [blame] | 1367 | /* Opcode: MustBeInt P1 P2 * |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 1368 | ** |
| 1369 | ** 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] | 1370 | ** stack is not an integer and cannot be converted into an integer |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 1371 | ** with out data loss, then jump immediately to P2, or if P2==0 |
| 1372 | ** raise an SQLITE_MISMATCH exception. |
drh | f1351b6 | 2002-07-31 19:50:26 +0000 | [diff] [blame] | 1373 | ** |
| 1374 | ** If the top of the stack is not an integer and P2 is not zero and |
| 1375 | ** P1 is 1, then the stack is popped. In all other cases, the depth |
| 1376 | ** of the stack is unchanged. |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 1377 | */ |
| 1378 | case OP_MustBeInt: { |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1379 | assert( pTos>=p->aStack ); |
| 1380 | if( pTos->flags & MEM_Int ){ |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 1381 | /* Do nothing */ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1382 | }else if( pTos->flags & MEM_Real ){ |
| 1383 | int i = (int)pTos->r; |
drh | 7d02cb7 | 2003-06-04 16:24:39 +0000 | [diff] [blame] | 1384 | double r = (double)i; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1385 | if( r!=pTos->r ){ |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 1386 | goto mismatch; |
| 1387 | } |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1388 | pTos->i = i; |
| 1389 | }else if( pTos->flags & MEM_Str ){ |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 1390 | i64 v; |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 1391 | if( sqlite3VdbeChangeEncoding(pTos, TEXT_Utf8) |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 1392 | || sqlite3VdbeMemNulTerminate(pTos) ){ |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 1393 | goto no_mem; |
| 1394 | } |
| 1395 | if( !sqlite3atoi64(pTos->z, &v) ){ |
drh | 1dd59e0 | 2003-07-06 17:22:25 +0000 | [diff] [blame] | 1396 | double r; |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 1397 | if( !sqlite3IsNumber(pTos->z, 0, TEXT_Utf8) ){ |
drh | 1dd59e0 | 2003-07-06 17:22:25 +0000 | [diff] [blame] | 1398 | goto mismatch; |
| 1399 | } |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 1400 | Realify(pTos, TEXT_Utf8); |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1401 | v = (int)pTos->r; |
drh | 1dd59e0 | 2003-07-06 17:22:25 +0000 | [diff] [blame] | 1402 | r = (double)v; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1403 | if( r!=pTos->r ){ |
drh | 1dd59e0 | 2003-07-06 17:22:25 +0000 | [diff] [blame] | 1404 | goto mismatch; |
| 1405 | } |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 1406 | } |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1407 | pTos->i = v; |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 1408 | }else{ |
| 1409 | goto mismatch; |
| 1410 | } |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1411 | Release(pTos); |
| 1412 | pTos->flags = MEM_Int; |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 1413 | pTos->type = SQLITE3_INTEGER; |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 1414 | break; |
| 1415 | |
| 1416 | mismatch: |
| 1417 | if( pOp->p2==0 ){ |
| 1418 | rc = SQLITE_MISMATCH; |
| 1419 | goto abort_due_to_error; |
| 1420 | }else{ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1421 | if( pOp->p1 ) popStack(&pTos, 1); |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 1422 | pc = pOp->p2 - 1; |
| 1423 | } |
| 1424 | break; |
| 1425 | } |
| 1426 | |
drh | 53db145 | 2004-05-20 13:54:53 +0000 | [diff] [blame] | 1427 | /* Opcode: Eq P1 P2 P3 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1428 | ** |
| 1429 | ** Pop the top two elements from the stack. If they are equal, then |
| 1430 | ** jump to instruction P2. Otherwise, continue to the next instruction. |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1431 | ** |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 1432 | ** The least significant byte of P1 may be either 0x00 or 0x01. If either |
| 1433 | ** operand is NULL (and thus if the result is unknown) then take the jump |
| 1434 | ** only if the least significant byte of P1 is 0x01. |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1435 | ** |
danielk1977 | e014a83 | 2004-05-17 10:48:57 +0000 | [diff] [blame] | 1436 | ** The second least significant byte of P1 must be an affinity character - |
| 1437 | ** 'n', 't', 'i' or 'o' - or 0x00. An attempt is made to coerce both values |
| 1438 | ** according to the affinity before the comparison is made. If the byte is |
| 1439 | ** 0x00, then numeric affinity is used. |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 1440 | ** |
| 1441 | ** Once any conversions have taken place, and neither value is NULL, |
| 1442 | ** the values are compared. If both values are blobs, or both are text, |
| 1443 | ** then memcmp() is used to determine the results of the comparison. If |
| 1444 | ** both values are numeric, then a numeric comparison is used. If the |
| 1445 | ** two values are of different types, then they are inequal. |
drh | c9b84a1 | 2002-06-20 11:36:48 +0000 | [diff] [blame] | 1446 | ** |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1447 | ** If P2 is zero, do not jump. Instead, push an integer 1 onto the |
| 1448 | ** stack if the jump would have been taken, or a 0 if not. Push a |
| 1449 | ** NULL if either operand was NULL. |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 1450 | ** |
drh | 53db145 | 2004-05-20 13:54:53 +0000 | [diff] [blame] | 1451 | ** If P3 is not NULL it is a pointer to a collating sequence (a CollSeq |
| 1452 | ** structure) that defines how to compare text. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1453 | */ |
drh | 53db145 | 2004-05-20 13:54:53 +0000 | [diff] [blame] | 1454 | /* Opcode: Ne P1 P2 P3 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1455 | ** |
drh | 53db145 | 2004-05-20 13:54:53 +0000 | [diff] [blame] | 1456 | ** This works just like the Eq opcode except that the jump is taken if |
| 1457 | ** the operands from the stack are not equal. See the Eq opcode for |
| 1458 | ** additional information. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1459 | */ |
drh | 53db145 | 2004-05-20 13:54:53 +0000 | [diff] [blame] | 1460 | /* Opcode: Lt P1 P2 P3 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1461 | ** |
drh | 53db145 | 2004-05-20 13:54:53 +0000 | [diff] [blame] | 1462 | ** This works just like the Eq opcode except that the jump is taken if |
| 1463 | ** the 2nd element down on the task is less than the top of the stack. |
| 1464 | ** See the Eq opcode for additional information. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1465 | */ |
drh | 53db145 | 2004-05-20 13:54:53 +0000 | [diff] [blame] | 1466 | /* Opcode: Le P1 P2 P3 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1467 | ** |
drh | 53db145 | 2004-05-20 13:54:53 +0000 | [diff] [blame] | 1468 | ** This works just like the Eq opcode except that the jump is taken if |
| 1469 | ** the 2nd element down on the task is less than or equal to the |
| 1470 | ** top of the stack. See the Eq opcode for additional information. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1471 | */ |
drh | 53db145 | 2004-05-20 13:54:53 +0000 | [diff] [blame] | 1472 | /* Opcode: Gt P1 P2 P3 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1473 | ** |
drh | 53db145 | 2004-05-20 13:54:53 +0000 | [diff] [blame] | 1474 | ** This works just like the Eq opcode except that the jump is taken if |
| 1475 | ** the 2nd element down on the task is greater than the top of the stack. |
| 1476 | ** See the Eq opcode for additional information. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1477 | */ |
drh | 53db145 | 2004-05-20 13:54:53 +0000 | [diff] [blame] | 1478 | /* Opcode: Ge P1 P2 P3 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1479 | ** |
drh | 53db145 | 2004-05-20 13:54:53 +0000 | [diff] [blame] | 1480 | ** This works just like the Eq opcode except that the jump is taken if |
| 1481 | ** the 2nd element down on the task is greater than or equal to the |
| 1482 | ** top of the stack. See the Eq opcode for additional information. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1483 | */ |
| 1484 | case OP_Eq: |
| 1485 | case OP_Ne: |
| 1486 | case OP_Lt: |
| 1487 | case OP_Le: |
| 1488 | case OP_Gt: |
| 1489 | case OP_Ge: { |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 1490 | Mem *pNos; |
| 1491 | int flags; |
| 1492 | int res; |
| 1493 | char affinity; |
| 1494 | |
| 1495 | pNos = &pTos[-1]; |
| 1496 | flags = pTos->flags|pNos->flags; |
| 1497 | |
| 1498 | /* If either value is a NULL P2 is not zero, take the jump if the least |
| 1499 | ** significant byte of P1 is true. If P2 is zero, then push a NULL onto |
| 1500 | ** the stack. |
| 1501 | */ |
| 1502 | if( flags&MEM_Null ){ |
| 1503 | popStack(&pTos, 2); |
| 1504 | if( pOp->p2 ){ |
danielk1977 | f9dd2c2 | 2004-05-16 11:57:28 +0000 | [diff] [blame] | 1505 | if( (pOp->p1&0xFF) ) pc = pOp->p2-1; |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 1506 | }else{ |
| 1507 | pTos++; |
| 1508 | pTos->flags = MEM_Null; |
| 1509 | } |
| 1510 | break; |
| 1511 | } |
| 1512 | |
| 1513 | affinity = (pOp->p1>>8)&0xFF; |
danielk1977 | e014a83 | 2004-05-17 10:48:57 +0000 | [diff] [blame] | 1514 | if( affinity=='\0' ) affinity = 'n'; |
danielk1977 | 8a6b541 | 2004-05-24 07:04:25 +0000 | [diff] [blame] | 1515 | applyAffinity(pNos, affinity, db->enc); |
| 1516 | applyAffinity(pTos, affinity, db->enc); |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 1517 | |
drh | 53db145 | 2004-05-20 13:54:53 +0000 | [diff] [blame] | 1518 | assert( pOp->p3type==P3_COLLSEQ || pOp->p3==0 ); |
| 1519 | res = sqlite3MemCompare(pNos, pTos, (CollSeq*)pOp->p3); |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 1520 | switch( pOp->opcode ){ |
| 1521 | case OP_Eq: res = res==0; break; |
| 1522 | case OP_Ne: res = res!=0; break; |
| 1523 | case OP_Lt: res = res<0; break; |
| 1524 | case OP_Le: res = res<=0; break; |
| 1525 | case OP_Gt: res = res>0; break; |
| 1526 | default: res = res>=0; break; |
| 1527 | } |
| 1528 | |
| 1529 | popStack(&pTos, 2); |
| 1530 | if( pOp->p2 ){ |
| 1531 | if( res ){ |
| 1532 | pc = pOp->p2-1; |
| 1533 | } |
| 1534 | }else{ |
| 1535 | pTos++; |
| 1536 | pTos->flags = MEM_Int; |
| 1537 | pTos->i = res; |
| 1538 | } |
| 1539 | break; |
| 1540 | } |
drh | c9b84a1 | 2002-06-20 11:36:48 +0000 | [diff] [blame] | 1541 | |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1542 | /* Opcode: And * * * |
| 1543 | ** |
| 1544 | ** Pop two values off the stack. Take the logical AND of the |
| 1545 | ** two values and push the resulting boolean value back onto the |
| 1546 | ** stack. |
| 1547 | */ |
| 1548 | /* Opcode: Or * * * |
| 1549 | ** |
| 1550 | ** Pop two values off the stack. Take the logical OR of the |
| 1551 | ** two values and push the resulting boolean value back onto the |
| 1552 | ** stack. |
| 1553 | */ |
| 1554 | case OP_And: |
| 1555 | case OP_Or: { |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1556 | Mem *pNos = &pTos[-1]; |
drh | bb11351 | 2002-05-27 01:04:51 +0000 | [diff] [blame] | 1557 | int v1, v2; /* 0==TRUE, 1==FALSE, 2==UNKNOWN or NULL */ |
| 1558 | |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1559 | assert( pNos>=p->aStack ); |
| 1560 | if( pTos->flags & MEM_Null ){ |
drh | bb11351 | 2002-05-27 01:04:51 +0000 | [diff] [blame] | 1561 | v1 = 2; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1562 | }else{ |
danielk1977 | 8a6b541 | 2004-05-24 07:04:25 +0000 | [diff] [blame] | 1563 | Integerify(pTos, db->enc); |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1564 | v1 = pTos->i==0; |
drh | bb11351 | 2002-05-27 01:04:51 +0000 | [diff] [blame] | 1565 | } |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1566 | if( pNos->flags & MEM_Null ){ |
drh | bb11351 | 2002-05-27 01:04:51 +0000 | [diff] [blame] | 1567 | v2 = 2; |
| 1568 | }else{ |
danielk1977 | 8a6b541 | 2004-05-24 07:04:25 +0000 | [diff] [blame] | 1569 | Integerify(pNos, db->enc); |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1570 | v2 = pNos->i==0; |
drh | bb11351 | 2002-05-27 01:04:51 +0000 | [diff] [blame] | 1571 | } |
| 1572 | if( pOp->opcode==OP_And ){ |
| 1573 | static const unsigned char and_logic[] = { 0, 1, 2, 1, 1, 1, 2, 1, 2 }; |
| 1574 | v1 = and_logic[v1*3+v2]; |
| 1575 | }else{ |
| 1576 | static const unsigned char or_logic[] = { 0, 0, 0, 0, 1, 2, 0, 2, 2 }; |
| 1577 | v1 = or_logic[v1*3+v2]; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1578 | } |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1579 | popStack(&pTos, 2); |
| 1580 | pTos++; |
drh | bb11351 | 2002-05-27 01:04:51 +0000 | [diff] [blame] | 1581 | if( v1==2 ){ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1582 | pTos->flags = MEM_Null; |
drh | bb11351 | 2002-05-27 01:04:51 +0000 | [diff] [blame] | 1583 | }else{ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1584 | pTos->i = v1==0; |
| 1585 | pTos->flags = MEM_Int; |
drh | bb11351 | 2002-05-27 01:04:51 +0000 | [diff] [blame] | 1586 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1587 | break; |
| 1588 | } |
| 1589 | |
| 1590 | /* Opcode: Negative * * * |
| 1591 | ** |
| 1592 | ** Treat the top of the stack as a numeric quantity. Replace it |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1593 | ** with its additive inverse. If the top of the stack is NULL |
| 1594 | ** its value is unchanged. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1595 | */ |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1596 | /* Opcode: AbsValue * * * |
| 1597 | ** |
| 1598 | ** Treat the top of the stack as a numeric quantity. Replace it |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1599 | ** with its absolute value. If the top of the stack is NULL |
| 1600 | ** its value is unchanged. |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1601 | */ |
| 1602 | case OP_Negative: |
| 1603 | case OP_AbsValue: { |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1604 | assert( pTos>=p->aStack ); |
| 1605 | if( pTos->flags & MEM_Real ){ |
| 1606 | Release(pTos); |
| 1607 | if( pOp->opcode==OP_Negative || pTos->r<0.0 ){ |
| 1608 | pTos->r = -pTos->r; |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1609 | } |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1610 | pTos->flags = MEM_Real; |
| 1611 | }else if( pTos->flags & MEM_Int ){ |
| 1612 | Release(pTos); |
| 1613 | if( pOp->opcode==OP_Negative || pTos->i<0 ){ |
| 1614 | pTos->i = -pTos->i; |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1615 | } |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1616 | pTos->flags = MEM_Int; |
| 1617 | }else if( pTos->flags & MEM_Null ){ |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1618 | /* Do nothing */ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1619 | }else{ |
danielk1977 | 8a6b541 | 2004-05-24 07:04:25 +0000 | [diff] [blame] | 1620 | Realify(pTos, db->enc); |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1621 | Release(pTos); |
| 1622 | if( pOp->opcode==OP_Negative || pTos->r<0.0 ){ |
| 1623 | pTos->r = -pTos->r; |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1624 | } |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1625 | pTos->flags = MEM_Real; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1626 | } |
| 1627 | break; |
| 1628 | } |
| 1629 | |
| 1630 | /* Opcode: Not * * * |
| 1631 | ** |
| 1632 | ** Interpret the top of the stack as a boolean value. Replace it |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1633 | ** with its complement. If the top of the stack is NULL its value |
| 1634 | ** is unchanged. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1635 | */ |
| 1636 | case OP_Not: { |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1637 | assert( pTos>=p->aStack ); |
| 1638 | if( pTos->flags & MEM_Null ) break; /* Do nothing to NULLs */ |
danielk1977 | 8a6b541 | 2004-05-24 07:04:25 +0000 | [diff] [blame] | 1639 | Integerify(pTos, db->enc); |
drh | 79f14b7 | 2004-03-03 01:51:24 +0000 | [diff] [blame] | 1640 | Release(pTos); |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1641 | pTos->i = !pTos->i; |
drh | 79f14b7 | 2004-03-03 01:51:24 +0000 | [diff] [blame] | 1642 | pTos->flags = MEM_Int; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1643 | break; |
| 1644 | } |
| 1645 | |
drh | 18b81e5 | 2001-11-01 13:52:52 +0000 | [diff] [blame] | 1646 | /* Opcode: BitNot * * * |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1647 | ** |
| 1648 | ** Interpret the top of the stack as an value. Replace it |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1649 | ** with its ones-complement. If the top of the stack is NULL its |
| 1650 | ** value is unchanged. |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1651 | */ |
| 1652 | case OP_BitNot: { |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1653 | assert( pTos>=p->aStack ); |
| 1654 | if( pTos->flags & MEM_Null ) break; /* Do nothing to NULLs */ |
danielk1977 | 8a6b541 | 2004-05-24 07:04:25 +0000 | [diff] [blame] | 1655 | Integerify(pTos, db->enc); |
drh | 79f14b7 | 2004-03-03 01:51:24 +0000 | [diff] [blame] | 1656 | Release(pTos); |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1657 | pTos->i = ~pTos->i; |
drh | 79f14b7 | 2004-03-03 01:51:24 +0000 | [diff] [blame] | 1658 | pTos->flags = MEM_Int; |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1659 | break; |
| 1660 | } |
| 1661 | |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1662 | /* Opcode: Noop * * * |
| 1663 | ** |
| 1664 | ** Do nothing. This instruction is often useful as a jump |
| 1665 | ** destination. |
| 1666 | */ |
| 1667 | case OP_Noop: { |
| 1668 | break; |
| 1669 | } |
| 1670 | |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1671 | /* Opcode: If P1 P2 * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1672 | ** |
| 1673 | ** Pop a single boolean from the stack. If the boolean popped is |
| 1674 | ** true, then jump to p2. Otherwise continue to the next instruction. |
| 1675 | ** An integer is false if zero and true otherwise. A string is |
| 1676 | ** false if it has zero length and true otherwise. |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1677 | ** |
| 1678 | ** If the value popped of the stack is NULL, then take the jump if P1 |
| 1679 | ** is true and fall through if P1 is false. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1680 | */ |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1681 | /* Opcode: IfNot P1 P2 * |
| 1682 | ** |
| 1683 | ** Pop a single boolean from the stack. If the boolean popped is |
| 1684 | ** false, then jump to p2. Otherwise continue to the next instruction. |
| 1685 | ** An integer is false if zero and true otherwise. A string is |
| 1686 | ** false if it has zero length and true otherwise. |
| 1687 | ** |
| 1688 | ** If the value popped of the stack is NULL, then take the jump if P1 |
| 1689 | ** is true and fall through if P1 is false. |
| 1690 | */ |
| 1691 | case OP_If: |
| 1692 | case OP_IfNot: { |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1693 | int c; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1694 | assert( pTos>=p->aStack ); |
| 1695 | if( pTos->flags & MEM_Null ){ |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1696 | c = pOp->p1; |
| 1697 | }else{ |
danielk1977 | 8a6b541 | 2004-05-24 07:04:25 +0000 | [diff] [blame] | 1698 | Integerify(pTos, db->enc); |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1699 | c = pTos->i; |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1700 | if( pOp->opcode==OP_IfNot ) c = !c; |
| 1701 | } |
danielk1977 | 8a6b541 | 2004-05-24 07:04:25 +0000 | [diff] [blame] | 1702 | /* FIX ME: Because constant P3 values sometimes need to be translated, |
| 1703 | ** the following assert() can fail. When P3 is always in the native text |
| 1704 | ** encoding, this assert() will be valid again. Until then, the Release() |
| 1705 | ** is neeed instead. |
| 1706 | assert( (pTos->flags & MEM_Dyn)==0 ); |
| 1707 | */ |
| 1708 | Release(pTos); |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1709 | pTos--; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1710 | if( c ) pc = pOp->p2-1; |
| 1711 | break; |
| 1712 | } |
| 1713 | |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1714 | /* Opcode: IsNull P1 P2 * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1715 | ** |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1716 | ** 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] | 1717 | ** to P2. Pop the stack P1 times if P1>0. If P1<0 leave the stack |
| 1718 | ** unchanged. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1719 | */ |
| 1720 | case OP_IsNull: { |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1721 | int i, cnt; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1722 | Mem *pTerm; |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1723 | cnt = pOp->p1; |
| 1724 | if( cnt<0 ) cnt = -cnt; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1725 | pTerm = &pTos[1-cnt]; |
| 1726 | assert( pTerm>=p->aStack ); |
| 1727 | for(i=0; i<cnt; i++, pTerm++){ |
| 1728 | if( pTerm->flags & MEM_Null ){ |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1729 | pc = pOp->p2-1; |
| 1730 | break; |
| 1731 | } |
| 1732 | } |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1733 | if( pOp->p1>0 ) popStack(&pTos, cnt); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1734 | break; |
| 1735 | } |
| 1736 | |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1737 | /* Opcode: NotNull P1 P2 * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1738 | ** |
drh | 143f3c4 | 2004-01-07 20:37:52 +0000 | [diff] [blame] | 1739 | ** Jump to P2 if the top P1 values on the stack are all not NULL. Pop the |
| 1740 | ** stack if P1 times if P1 is greater than zero. If P1 is less than |
| 1741 | ** zero then leave the stack unchanged. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1742 | */ |
| 1743 | case OP_NotNull: { |
drh | 143f3c4 | 2004-01-07 20:37:52 +0000 | [diff] [blame] | 1744 | int i, cnt; |
| 1745 | cnt = pOp->p1; |
| 1746 | if( cnt<0 ) cnt = -cnt; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1747 | assert( &pTos[1-cnt] >= p->aStack ); |
| 1748 | 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] | 1749 | if( i>=cnt ) pc = pOp->p2-1; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1750 | if( pOp->p1>0 ) popStack(&pTos, cnt); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1751 | break; |
| 1752 | } |
| 1753 | |
danielk1977 | b4964b7 | 2004-05-18 01:23:38 +0000 | [diff] [blame] | 1754 | /* Opcode: SetNumColumns P1 P2 * |
| 1755 | ** |
| 1756 | ** Before the OP_Column opcode can be executed on a cursor, this |
| 1757 | ** opcode must be called to set the number of fields in the table. |
| 1758 | ** |
| 1759 | ** This opcode sets the number of columns for cursor P1 to P2. |
| 1760 | */ |
| 1761 | case OP_SetNumColumns: { |
| 1762 | assert( (pOp->p1)<p->nCursor ); |
| 1763 | p->apCsr[pOp->p1]->nField = pOp->p2; |
| 1764 | break; |
| 1765 | } |
| 1766 | |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 1767 | /* Opcode: IdxColumn P1 * * |
| 1768 | ** |
| 1769 | ** P1 is a cursor opened on an index. Push the first field from the |
| 1770 | ** current index key onto the stack. |
| 1771 | */ |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 1772 | /* Opcode: Column P1 P2 * |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 1773 | ** |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1774 | ** Interpret the data that cursor P1 points to as a structure built using |
| 1775 | ** the MakeRecord instruction. (See the MakeRecord opcode for additional |
| 1776 | ** information about the format of the data.) Push onto the stack the value |
| 1777 | ** of the P2-th column contained in the data. |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 1778 | ** |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1779 | ** If the KeyAsData opcode has previously executed on this cursor, then the |
| 1780 | ** field might be extracted from the key rather than the data. |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 1781 | ** |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1782 | ** If P1 is negative, then the record is stored on the stack rather than in |
| 1783 | ** a table. For P1==-1, the top of the stack is used. For P1==-2, the |
| 1784 | ** next on the stack is used. And so forth. The value pushed is always |
| 1785 | ** just a pointer into the record which is stored further down on the |
danielk1977 | 84ac9d0 | 2004-05-18 09:58:06 +0000 | [diff] [blame] | 1786 | ** stack. The column value is not copied. The number of columns in the |
| 1787 | ** record is stored on the stack just above the record itself. |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 1788 | */ |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 1789 | case OP_IdxColumn: |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1790 | case OP_Column: { |
| 1791 | int payloadSize; /* Number of bytes in the record */ |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 1792 | int p1 = pOp->p1; /* P1 value of the opcode */ |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1793 | int p2 = pOp->p2; /* column number to retrieve */ |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 1794 | Cursor *pC = 0; /* The VDBE cursor */ |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1795 | char *zRec; /* Pointer to record-data from stack or pseudo-table. */ |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 1796 | BtCursor *pCrsr; /* The BTree cursor */ |
| 1797 | u32 *aType; /* aType[i] holds the numeric type of the i-th column */ |
| 1798 | u32 *aOffset; /* aOffset[i] is offset to start of data for i-th column */ |
drh | 25aa1b4 | 2004-05-28 01:39:01 +0000 | [diff] [blame] | 1799 | u32 nField; /* number of fields in the record */ |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 1800 | u32 szHdr; /* Number of bytes in the record header */ |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1801 | int len; /* The length of the serialized data for the column */ |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 1802 | int offset = 0; /* Offset into the data */ |
| 1803 | int idx; /* Index into the header */ |
| 1804 | int i; /* Loop counter */ |
| 1805 | char *zData; /* Part of the record being decoded */ |
| 1806 | Mem sMem; /* For storing the record being decoded */ |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 1807 | |
drh | b6f5452 | 2004-05-20 02:42:16 +0000 | [diff] [blame] | 1808 | sMem.flags = 0; |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 1809 | assert( p1<p->nCursor ); |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 1810 | pTos++; |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1811 | |
| 1812 | /* This block sets the variable payloadSize, and if the data is coming |
| 1813 | ** from the stack or from a pseudo-table zRec. If the data is coming |
| 1814 | ** from a real cursor, then zRec is left as NULL. |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 1815 | ** |
| 1816 | ** We also compute the number of columns in the record. For cursors, |
| 1817 | ** the number of columns is stored in the Cursor.nField element. For |
| 1818 | ** records on the stack, the next entry down on the stack is an integer |
| 1819 | ** which is the number of records. |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1820 | */ |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 1821 | if( p1<0 ){ |
| 1822 | Mem *pRec = &pTos[p1]; |
| 1823 | Mem *pCnt = &pRec[-1]; |
| 1824 | assert( pRec>=p->aStack ); |
| 1825 | assert( pRec->flags & MEM_Blob ); |
| 1826 | payloadSize = pRec->n; |
| 1827 | zRec = pRec->z; |
| 1828 | assert( pCnt>=p->aStack ); |
| 1829 | assert( pCnt->flags & MEM_Int ); |
| 1830 | nField = pCnt->i; |
| 1831 | }else if( (pC = p->apCsr[p1])->pCursor!=0 ){ |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 1832 | sqlite3VdbeCursorMoveto(pC); |
| 1833 | zRec = 0; |
| 1834 | pCrsr = pC->pCursor; |
| 1835 | if( pC->nullRow ){ |
| 1836 | payloadSize = 0; |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 1837 | }else if( pC->cacheValid ){ |
| 1838 | payloadSize = pC->payloadSize; |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 1839 | }else if( pC->keyAsData ){ |
danielk1977 | 96fc5fe | 2004-05-13 11:34:16 +0000 | [diff] [blame] | 1840 | i64 payloadSize64; |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 1841 | sqlite3BtreeKeySize(pCrsr, &payloadSize64); |
| 1842 | payloadSize = payloadSize64; |
| 1843 | }else{ |
| 1844 | sqlite3BtreeDataSize(pCrsr, &payloadSize); |
| 1845 | } |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 1846 | nField = pC->nField; |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 1847 | }else if( pC->pseudoTable ){ |
| 1848 | payloadSize = pC->nData; |
| 1849 | zRec = pC->pData; |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 1850 | pC->cacheValid = 0; |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 1851 | assert( payloadSize==0 || zRec!=0 ); |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 1852 | nField = pC->nField; |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 1853 | }else{ |
| 1854 | payloadSize = 0; |
| 1855 | } |
| 1856 | |
| 1857 | /* If payloadSize is 0, then just push a NULL onto the stack. */ |
| 1858 | if( payloadSize==0 ){ |
| 1859 | pTos->flags = MEM_Null; |
| 1860 | break; |
| 1861 | } |
| 1862 | |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 1863 | assert( p2<nField ); |
danielk1977 | b4964b7 | 2004-05-18 01:23:38 +0000 | [diff] [blame] | 1864 | |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 1865 | /* Read and parse the table header. Store the results of the parse |
| 1866 | ** into the record header cache fields of the cursor. |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 1867 | */ |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 1868 | if( pC && pC->cacheValid ){ |
| 1869 | aType = pC->aType; |
| 1870 | aOffset = pC->aOffset; |
| 1871 | }else{ |
| 1872 | aType = sqliteMallocRaw( 2*nField*sizeof(aType) ); |
| 1873 | aOffset = &aType[nField]; |
| 1874 | if( aType==0 ){ |
| 1875 | goto no_mem; |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 1876 | } |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 1877 | |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 1878 | /* Figure out how many bytes are in the header */ |
danielk1977 | 84ac9d0 | 2004-05-18 09:58:06 +0000 | [diff] [blame] | 1879 | if( zRec ){ |
| 1880 | zData = zRec; |
| 1881 | }else{ |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 1882 | int sz = payloadSize<5 ? payloadSize : 5; |
| 1883 | if( pC->keyAsData ){ |
| 1884 | zData = (char*)sqlite3BtreeKeyFetch(pCrsr, sz); |
| 1885 | }else{ |
| 1886 | zData = (char*)sqlite3BtreeDataFetch(pCrsr, sz); |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 1887 | } |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 1888 | } |
| 1889 | idx = sqlite3GetVarint32(zData, &szHdr); |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 1890 | |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 1891 | /* Get the complete header text */ |
| 1892 | if( !zRec ){ |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 1893 | rc = sqlite3VdbeMemFromBtree(pCrsr, 0, szHdr, pC->keyAsData, &sMem); |
danielk1977 | 84ac9d0 | 2004-05-18 09:58:06 +0000 | [diff] [blame] | 1894 | if( rc!=SQLITE_OK ){ |
| 1895 | goto abort_due_to_error; |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 1896 | } |
drh | b6f5452 | 2004-05-20 02:42:16 +0000 | [diff] [blame] | 1897 | zData = sMem.z; |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 1898 | } |
| 1899 | |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 1900 | /* Scan the header and use it to fill in the aType[] and aOffset[] |
| 1901 | ** arrays. aType[i] will contain the type integer for the i-th |
| 1902 | ** column and aOffset[i] will contain the offset from the beginning |
| 1903 | ** of the record to the start of the data for the i-th column |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 1904 | */ |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 1905 | offset = szHdr; |
| 1906 | i = 0; |
| 1907 | while( idx<szHdr && i<nField && offset<=payloadSize ){ |
| 1908 | aOffset[i] = offset; |
| 1909 | idx += sqlite3GetVarint32(&zData[idx], &aType[i]); |
| 1910 | offset += sqlite3VdbeSerialTypeLen(aType[i]); |
| 1911 | i++; |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 1912 | } |
drh | b6f5452 | 2004-05-20 02:42:16 +0000 | [diff] [blame] | 1913 | Release(&sMem); |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 1914 | sMem.flags = MEM_Null; |
| 1915 | |
| 1916 | /* The header should end at the start of data and the data should |
| 1917 | ** end at last byte of the record. If this is not the case then |
| 1918 | ** we are dealing with a malformed record. |
| 1919 | */ |
| 1920 | if( idx!=szHdr || offset!=payloadSize ){ |
| 1921 | sqliteFree(aType); |
| 1922 | if( pC ) pC->aType = 0; |
| 1923 | rc = SQLITE_CORRUPT; |
| 1924 | break; |
| 1925 | } |
| 1926 | |
| 1927 | /* Remember all aType and aColumn information if we have a cursor |
| 1928 | ** to remember it in. */ |
| 1929 | if( pC ){ |
| 1930 | pC->payloadSize = payloadSize; |
| 1931 | pC->aType = aType; |
| 1932 | pC->aOffset = aOffset; |
| 1933 | pC->cacheValid = 1; |
| 1934 | } |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1935 | } |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 1936 | |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 1937 | /* Get the column information. |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 1938 | */ |
danielk1977 | 84ac9d0 | 2004-05-18 09:58:06 +0000 | [diff] [blame] | 1939 | if( zRec ){ |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 1940 | zData = &zRec[aOffset[p2]]; |
danielk1977 | 84ac9d0 | 2004-05-18 09:58:06 +0000 | [diff] [blame] | 1941 | }else{ |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 1942 | len = sqlite3VdbeSerialTypeLen(aType[p2]); |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 1943 | sqlite3VdbeMemFromBtree(pCrsr, aOffset[p2], len, pC->keyAsData, &sMem); |
drh | b6f5452 | 2004-05-20 02:42:16 +0000 | [diff] [blame] | 1944 | zData = sMem.z; |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1945 | } |
drh | 25aa1b4 | 2004-05-28 01:39:01 +0000 | [diff] [blame] | 1946 | sqlite3VdbeSerialGet(zData, aType[p2], pTos); |
| 1947 | pTos->enc = db->enc; |
danielk1977 | b1bc953 | 2004-05-22 03:05:33 +0000 | [diff] [blame] | 1948 | if( rc!=SQLITE_OK ){ |
| 1949 | goto abort_due_to_error; |
| 1950 | } |
drh | b6f5452 | 2004-05-20 02:42:16 +0000 | [diff] [blame] | 1951 | Release(&sMem); |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 1952 | |
| 1953 | /* Release the aType[] memory if we are not dealing with cursor */ |
| 1954 | if( !pC ){ |
| 1955 | sqliteFree(aType); |
| 1956 | } |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 1957 | break; |
| 1958 | } |
| 1959 | |
drh | c9b84a1 | 2002-06-20 11:36:48 +0000 | [diff] [blame] | 1960 | /* Opcode: MakeKey P1 P2 P3 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1961 | ** |
| 1962 | ** Convert the top P1 entries of the stack into a single entry suitable |
danielk1977 | 84ac9d0 | 2004-05-18 09:58:06 +0000 | [diff] [blame] | 1963 | ** for use as the key in an index. If P2 is zero, then the original |
| 1964 | ** entries are popped off the stack. If P2 is not zero, the original |
| 1965 | ** entries remain on the stack. |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 1966 | ** |
| 1967 | ** P3 is interpreted in the same way as for MakeIdxKey. |
| 1968 | */ |
| 1969 | /* Opcode: MakeIdxKey P1 P2 P3 |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 1970 | ** |
| 1971 | ** Convert the top P1 entries of the stack into a single entry suitable |
| 1972 | ** for use as the key in an index. In addition, take one additional integer |
| 1973 | ** off of the stack, treat that integer as an eight-byte record number, and |
| 1974 | ** append the integer to the key as a varint. Thus a total of P1+1 entries |
| 1975 | ** are popped from the stack for this instruction and a single entry is |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 1976 | ** pushed back. |
| 1977 | ** |
| 1978 | ** If P2 is not zero and one or more of the P1 entries that go into the |
| 1979 | ** generated key is NULL, then jump to P2 after the new key has been |
| 1980 | ** pushed on the stack. In other words, jump to P2 if the key is |
| 1981 | ** guaranteed to be unique. This jump can be used to skip a subsequent |
| 1982 | ** uniqueness test. |
| 1983 | ** |
| 1984 | ** P3 may be a string that is P1 characters long. The nth character of the |
| 1985 | ** string indicates the column affinity that should be used for the nth |
| 1986 | ** field of the index key (i.e. the first character of P3 corresponds to the |
| 1987 | ** lowest element on the stack). |
| 1988 | ** |
| 1989 | ** Character Column affinity |
| 1990 | ** ------------------------------ |
| 1991 | ** 'n' NUMERIC |
| 1992 | ** 'i' INTEGER |
| 1993 | ** 't' TEXT |
| 1994 | ** 'o' NONE |
| 1995 | ** |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 1996 | ** If P3 is NULL then datatype coercion occurs. |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 1997 | */ |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 1998 | /* Opcode MakeRecord P1 * P3 |
| 1999 | ** |
| 2000 | ** Convert the top P1 entries of the stack into a single entry |
| 2001 | ** suitable for use as a data record in a database table. The |
| 2002 | ** details of the format are irrelavant as long as the OP_Column |
| 2003 | ** opcode can decode the record later. Refer to source code |
| 2004 | ** comments for the details of the record format. |
| 2005 | ** |
| 2006 | ** P3 may be a string that is P1 characters long. The nth character of the |
| 2007 | ** string indicates the column affinity that should be used for the nth |
| 2008 | ** field of the index key (i.e. the first character of P3 corresponds to the |
| 2009 | ** lowest element on the stack). |
| 2010 | ** |
| 2011 | ** Character Column affinity |
| 2012 | ** ------------------------------ |
| 2013 | ** 'n' NUMERIC |
| 2014 | ** 'i' INTEGER |
| 2015 | ** 't' TEXT |
| 2016 | ** 'o' NONE |
| 2017 | ** |
| 2018 | ** If P3 is NULL then all index fields have the affinity NONE. |
| 2019 | */ |
danielk1977 | 452c989 | 2004-05-13 05:16:15 +0000 | [diff] [blame] | 2020 | case OP_MakeKey: |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 2021 | case OP_MakeIdxKey: |
| 2022 | case OP_MakeRecord: { |
| 2023 | /* Assuming the record contains N fields, the record format looks |
| 2024 | ** like this: |
| 2025 | ** |
| 2026 | ** -------------------------------------------------------------------------- |
| 2027 | ** | header-siz | type 0 | type 1 | ... | type N-1 | data0 | ... | data N-1 | |
| 2028 | ** -------------------------------------------------------------------------- |
| 2029 | ** |
| 2030 | ** Data(0) is taken from the lowest element of the stack and data(N-1) is |
| 2031 | ** the top of the stack. |
| 2032 | ** |
| 2033 | ** Each type field is a varint representing the serial type of the |
| 2034 | ** corresponding data element (see sqlite3VdbeSerialType()). The |
| 2035 | ** num-fields field is also a varint storing N. |
| 2036 | ** |
| 2037 | ** TODO: Even when the record is short enough for Mem::zShort, this opcode |
| 2038 | ** allocates it dynamically. |
| 2039 | */ |
| 2040 | int nField = pOp->p1; |
| 2041 | unsigned char *zNewRecord; |
| 2042 | unsigned char *zCsr; |
| 2043 | char *zAffinity; |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 2044 | Mem *pRec; |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 2045 | Mem *pRowid; |
| 2046 | int nData = 0; /* Number of bytes of data space */ |
| 2047 | int nHdr = 0; /* Number of bytes of header space */ |
| 2048 | int nByte = 0; /* Space required for this record */ |
| 2049 | int addRowid; /* True to append a rowid column at the end */ |
| 2050 | u32 serial_type; /* Type field */ |
| 2051 | int containsNull; /* True if any of the data fields are NULL */ |
| 2052 | |
| 2053 | Mem *pData0 = &pTos[1-nField]; |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 2054 | assert( pData0>=p->aStack ); |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 2055 | zAffinity = pOp->p3; |
| 2056 | addRowid = pOp->opcode==OP_MakeIdxKey; |
| 2057 | containsNull = 0; |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 2058 | |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 2059 | /* Loop through the elements that will make up the record to figure |
| 2060 | ** out how much space is required for the new record. |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 2061 | */ |
| 2062 | for(pRec=pData0; pRec<=pTos; pRec++){ |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 2063 | if( zAffinity ){ |
danielk1977 | 8a6b541 | 2004-05-24 07:04:25 +0000 | [diff] [blame] | 2064 | applyAffinity(pRec, zAffinity[pRec-pData0], db->enc); |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 2065 | } |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 2066 | if( pRec->flags&MEM_Null ){ |
danielk1977 | 452c989 | 2004-05-13 05:16:15 +0000 | [diff] [blame] | 2067 | containsNull = 1; |
| 2068 | } |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 2069 | serial_type = sqlite3VdbeSerialType(pRec); |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 2070 | nData += sqlite3VdbeSerialTypeLen(serial_type); |
| 2071 | nHdr += sqlite3VarintLen(serial_type); |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 2072 | } |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 2073 | |
| 2074 | /* If we have to append a varint rowid to this record, set 'rowid' |
| 2075 | ** to the value of the rowid and increase nByte by the amount of space |
| 2076 | ** required to store it and the 0x00 seperator byte. |
| 2077 | */ |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 2078 | if( addRowid ){ |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 2079 | pRowid = &pTos[0-nField]; |
| 2080 | assert( pRowid>=p->aStack ); |
| 2081 | Integerify(pRowid, db->enc); |
| 2082 | serial_type = sqlite3VdbeSerialType(pRowid); |
| 2083 | nData += sqlite3VdbeSerialTypeLen(serial_type); |
| 2084 | nHdr += sqlite3VarintLen(serial_type); |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 2085 | } |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 2086 | |
| 2087 | /* Add the initial header varint and total the size */ |
| 2088 | nHdr += sqlite3VarintLen(nHdr); |
| 2089 | nByte = nHdr+nData; |
| 2090 | |
danielk1977 | 96fc5fe | 2004-05-13 11:34:16 +0000 | [diff] [blame] | 2091 | if( nByte>MAX_BYTES_PER_ROW ){ |
| 2092 | rc = SQLITE_TOOBIG; |
| 2093 | goto abort_due_to_error; |
| 2094 | } |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 2095 | |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 2096 | /* Allocate space for the new record. */ |
| 2097 | zNewRecord = sqliteMallocRaw(nByte); |
| 2098 | if( !zNewRecord ){ |
danielk1977 | b4964b7 | 2004-05-18 01:23:38 +0000 | [diff] [blame] | 2099 | goto no_mem; |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 2100 | } |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 2101 | |
| 2102 | /* Write the record */ |
| 2103 | zCsr = zNewRecord; |
| 2104 | zCsr += sqlite3PutVarint(zCsr, nHdr); |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 2105 | for(pRec=pData0; pRec<=pTos; pRec++){ |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 2106 | serial_type = sqlite3VdbeSerialType(pRec); |
| 2107 | zCsr += sqlite3PutVarint(zCsr, serial_type); /* serial type */ |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 2108 | } |
| 2109 | if( addRowid ){ |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 2110 | zCsr += sqlite3PutVarint(zCsr, sqlite3VdbeSerialType(pRowid)); |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 2111 | } |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 2112 | for(pRec=pData0; pRec<=pTos; pRec++){ |
| 2113 | zCsr += sqlite3VdbeSerialPut(zCsr, pRec); /* serial data */ |
| 2114 | } |
| 2115 | if( addRowid ){ |
| 2116 | zCsr += sqlite3VdbeSerialPut(zCsr, pRowid); |
| 2117 | } |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 2118 | |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 2119 | /* If zCsr has not been advanced exactly nByte bytes, then one |
| 2120 | ** of the sqlite3PutVarint() or sqlite3VdbeSerialPut() calls above |
| 2121 | ** failed. This indicates a corrupted memory cell or code bug. |
| 2122 | */ |
| 2123 | if( zCsr!=(zNewRecord+nByte) ){ |
| 2124 | rc = SQLITE_INTERNAL; |
| 2125 | goto abort_due_to_error; |
| 2126 | } |
| 2127 | |
| 2128 | /* Pop nField entries from the stack and push the new entry on */ |
| 2129 | if( addRowid || pOp->p2==0 ){ |
danielk1977 | 452c989 | 2004-05-13 05:16:15 +0000 | [diff] [blame] | 2130 | popStack(&pTos, nField+addRowid); |
| 2131 | } |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 2132 | pTos++; |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 2133 | pTos->n = nByte; |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 2134 | pTos->z = zNewRecord; |
| 2135 | pTos->flags = MEM_Blob | MEM_Dyn; |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 2136 | |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 2137 | /* If P2 is non-zero, and if the key contains a NULL value, and if this |
| 2138 | ** was an OP_MakeIdxKey instruction, not OP_MakeKey, jump to P2. |
| 2139 | */ |
| 2140 | if( pOp->p2 && containsNull && addRowid ){ |
danielk1977 | 452c989 | 2004-05-13 05:16:15 +0000 | [diff] [blame] | 2141 | pc = pOp->p2 - 1; |
| 2142 | } |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 2143 | break; |
| 2144 | } |
| 2145 | |
drh | 7f0f12e | 2004-05-21 13:39:50 +0000 | [diff] [blame] | 2146 | /* Opcode: Statement P1 * * |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2147 | ** |
drh | 7f0f12e | 2004-05-21 13:39:50 +0000 | [diff] [blame] | 2148 | ** Begin an individual statement transaction which is part of a larger |
| 2149 | ** BEGIN..COMMIT transaction. This is needed so that the statement |
| 2150 | ** can be rolled back after an error without having to roll back the |
| 2151 | ** entire transaction. The statement transaction will automatically |
| 2152 | ** commit when the VDBE halts. |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2153 | ** |
drh | 7f0f12e | 2004-05-21 13:39:50 +0000 | [diff] [blame] | 2154 | ** The statement is begun on the database file with index P1. The main |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2155 | ** database file has an index of 0 and the file used for temporary tables |
| 2156 | ** has an index of 1. |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2157 | */ |
drh | 7f0f12e | 2004-05-21 13:39:50 +0000 | [diff] [blame] | 2158 | case OP_Statement: { |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2159 | int i = pOp->p1; |
drh | 1aa4965 | 2003-06-02 23:14:13 +0000 | [diff] [blame] | 2160 | 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] | 2161 | rc = sqlite3BtreeBeginStmt(db->aDb[i].pBt); |
drh | 1aa4965 | 2003-06-02 23:14:13 +0000 | [diff] [blame] | 2162 | if( rc==SQLITE_OK ) db->aDb[i].inTrans = 2; |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2163 | } |
| 2164 | break; |
| 2165 | } |
| 2166 | |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 2167 | /* Opcode: Transaction P1 * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2168 | ** |
| 2169 | ** Begin a transaction. The transaction ends when a Commit or Rollback |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2170 | ** opcode is encountered. Depending on the ON CONFLICT setting, the |
| 2171 | ** transaction might also be rolled back if an error is encountered. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2172 | ** |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2173 | ** P1 is the index of the database file on which the transaction is |
| 2174 | ** started. Index 0 is the main database file and index 1 is the |
| 2175 | ** file used for temporary tables. |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 2176 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2177 | ** A write lock is obtained on the database file when a transaction is |
| 2178 | ** started. No other process can read or write the file while the |
| 2179 | ** transaction is underway. Starting a transaction also creates a |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2180 | ** rollback journal. A transaction must be started before any changes |
| 2181 | ** can be made to the database. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2182 | */ |
| 2183 | case OP_Transaction: { |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 2184 | int busy = 1; |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2185 | int i = pOp->p1; |
drh | 8bf8dc9 | 2003-05-17 17:35:10 +0000 | [diff] [blame] | 2186 | assert( i>=0 && i<db->nDb ); |
| 2187 | if( db->aDb[i].inTrans ) break; |
| 2188 | while( db->aDb[i].pBt!=0 && busy ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2189 | rc = sqlite3BtreeBeginTrans(db->aDb[i].pBt); |
drh | 90bfcda | 2001-09-23 19:46:51 +0000 | [diff] [blame] | 2190 | switch( rc ){ |
| 2191 | case SQLITE_BUSY: { |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 2192 | if( db->xBusyCallback==0 ){ |
| 2193 | p->pc = pc; |
| 2194 | p->undoTransOnError = 1; |
| 2195 | p->rc = SQLITE_BUSY; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 2196 | p->pTos = pTos; |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 2197 | return SQLITE_BUSY; |
| 2198 | }else if( (*db->xBusyCallback)(db->pBusyArg, "", busy++)==0 ){ |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 2199 | sqlite3SetString(&p->zErrMsg, sqlite3_error_string(rc), (char*)0); |
drh | 90bfcda | 2001-09-23 19:46:51 +0000 | [diff] [blame] | 2200 | busy = 0; |
| 2201 | } |
| 2202 | break; |
| 2203 | } |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 2204 | case SQLITE_READONLY: { |
| 2205 | rc = SQLITE_OK; |
| 2206 | /* Fall thru into the next case */ |
| 2207 | } |
drh | 90bfcda | 2001-09-23 19:46:51 +0000 | [diff] [blame] | 2208 | case SQLITE_OK: { |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 2209 | p->inTempTrans = 0; |
drh | 90bfcda | 2001-09-23 19:46:51 +0000 | [diff] [blame] | 2210 | busy = 0; |
| 2211 | break; |
| 2212 | } |
| 2213 | default: { |
| 2214 | goto abort_due_to_error; |
| 2215 | } |
| 2216 | } |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 2217 | } |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2218 | db->aDb[i].inTrans = 1; |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 2219 | p->undoTransOnError = 1; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2220 | break; |
| 2221 | } |
| 2222 | |
| 2223 | /* Opcode: Commit * * * |
| 2224 | ** |
| 2225 | ** Cause all modifications to the database that have been made since the |
| 2226 | ** last Transaction to actually take effect. No additional modifications |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2227 | ** are allowed until another transaction is started. The Commit instruction |
| 2228 | ** deletes the journal file and releases the write lock on the database. |
| 2229 | ** A read lock continues to be held if there are still cursors open. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2230 | */ |
| 2231 | case OP_Commit: { |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2232 | int i; |
drh | aa940ea | 2004-01-15 02:44:03 +0000 | [diff] [blame] | 2233 | if( db->xCommitCallback!=0 ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2234 | if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse; |
drh | aa940ea | 2004-01-15 02:44:03 +0000 | [diff] [blame] | 2235 | if( db->xCommitCallback(db->pCommitArg)!=0 ){ |
| 2236 | rc = SQLITE_CONSTRAINT; |
| 2237 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2238 | if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse; |
drh | aa940ea | 2004-01-15 02:44:03 +0000 | [diff] [blame] | 2239 | } |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2240 | for(i=0; rc==SQLITE_OK && i<db->nDb; i++){ |
| 2241 | if( db->aDb[i].inTrans ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2242 | rc = sqlite3BtreeCommit(db->aDb[i].pBt); |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2243 | db->aDb[i].inTrans = 0; |
| 2244 | } |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 2245 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2246 | if( rc==SQLITE_OK ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2247 | sqlite3CommitInternalChanges(db); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2248 | }else{ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2249 | sqlite3RollbackAll(db); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2250 | } |
| 2251 | break; |
| 2252 | } |
| 2253 | |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2254 | /* Opcode: Rollback P1 * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2255 | ** |
| 2256 | ** Cause all modifications to the database that have been made since the |
| 2257 | ** last Transaction to be undone. The database is restored to its state |
| 2258 | ** before the Transaction opcode was executed. No additional modifications |
| 2259 | ** are allowed until another transaction is started. |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2260 | ** |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2261 | ** P1 is the index of the database file that is committed. An index of 0 |
| 2262 | ** is used for the main database and an index of 1 is used for the file used |
| 2263 | ** to hold temporary tables. |
| 2264 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2265 | ** This instruction automatically closes all cursors and releases both |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2266 | ** the read and write locks on the indicated database. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2267 | */ |
| 2268 | case OP_Rollback: { |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2269 | sqlite3RollbackAll(db); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2270 | break; |
| 2271 | } |
| 2272 | |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2273 | /* Opcode: ReadCookie P1 P2 * |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 2274 | ** |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2275 | ** Read cookie number P2 from database P1 and push it onto the stack. |
| 2276 | ** P2==0 is the schema version. P2==1 is the database format. |
| 2277 | ** P2==2 is the recommended pager cache size, and so forth. P1==0 is |
| 2278 | ** the main database file and P1==1 is the database file used to store |
| 2279 | ** temporary tables. |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 2280 | ** |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 2281 | ** There must be a read-lock on the database (either a transaction |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2282 | ** must be started or there must be an open cursor) before |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 2283 | ** executing this instruction. |
| 2284 | */ |
| 2285 | case OP_ReadCookie: { |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 2286 | int iMeta; |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 2287 | assert( pOp->p2<SQLITE_N_BTREE_META ); |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2288 | assert( pOp->p1>=0 && pOp->p1<db->nDb ); |
| 2289 | assert( db->aDb[pOp->p1].pBt!=0 ); |
drh | a3b321d | 2004-05-11 09:31:31 +0000 | [diff] [blame] | 2290 | /* The indexing of meta values at the schema layer is off by one from |
| 2291 | ** the indexing in the btree layer. The btree considers meta[0] to |
| 2292 | ** be the number of free pages in the database (a read-only value) |
| 2293 | ** and meta[1] to be the schema cookie. The schema layer considers |
| 2294 | ** meta[1] to be the schema cookie. So we have to shift the index |
| 2295 | ** by one in the following statement. |
| 2296 | */ |
| 2297 | rc = sqlite3BtreeGetMeta(db->aDb[pOp->p1].pBt, 1 + pOp->p2, &iMeta); |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 2298 | pTos++; |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 2299 | pTos->i = iMeta; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 2300 | pTos->flags = MEM_Int; |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 2301 | break; |
| 2302 | } |
| 2303 | |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2304 | /* Opcode: SetCookie P1 P2 * |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 2305 | ** |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2306 | ** Write the top of the stack into cookie number P2 of database P1. |
| 2307 | ** P2==0 is the schema version. P2==1 is the database format. |
| 2308 | ** P2==2 is the recommended pager cache size, and so forth. P1==0 is |
| 2309 | ** the main database file and P1==1 is the database file used to store |
| 2310 | ** temporary tables. |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 2311 | ** |
| 2312 | ** A transaction must be started before executing this opcode. |
| 2313 | */ |
| 2314 | case OP_SetCookie: { |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 2315 | assert( pOp->p2<SQLITE_N_BTREE_META ); |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2316 | assert( pOp->p1>=0 && pOp->p1<db->nDb ); |
| 2317 | assert( db->aDb[pOp->p1].pBt!=0 ); |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 2318 | assert( pTos>=p->aStack ); |
danielk1977 | 8a6b541 | 2004-05-24 07:04:25 +0000 | [diff] [blame] | 2319 | Integerify(pTos, db->enc); |
drh | a3b321d | 2004-05-11 09:31:31 +0000 | [diff] [blame] | 2320 | /* See note about index shifting on OP_ReadCookie */ |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 2321 | rc = sqlite3BtreeUpdateMeta(db->aDb[pOp->p1].pBt, 1+pOp->p2, (int)pTos->i); |
drh | 79f14b7 | 2004-03-03 01:51:24 +0000 | [diff] [blame] | 2322 | Release(pTos); |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 2323 | pTos--; |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 2324 | break; |
| 2325 | } |
| 2326 | |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 2327 | /* Opcode: VerifyCookie P1 P2 * |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 2328 | ** |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2329 | ** Check the value of global database parameter number 0 (the |
| 2330 | ** schema version) and make sure it is equal to P2. |
| 2331 | ** P1 is the database number which is 0 for the main database file |
| 2332 | ** and 1 for the file holding temporary tables and some higher number |
| 2333 | ** for auxiliary databases. |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 2334 | ** |
| 2335 | ** The cookie changes its value whenever the database schema changes. |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2336 | ** This operation is used to detect when that the cookie has changed |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 2337 | ** and that the current process needs to reread the schema. |
| 2338 | ** |
| 2339 | ** Either a transaction needs to have been started or an OP_Open needs |
| 2340 | ** to be executed (to establish a read lock) before this opcode is |
| 2341 | ** invoked. |
| 2342 | */ |
| 2343 | case OP_VerifyCookie: { |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 2344 | int iMeta; |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2345 | assert( pOp->p1>=0 && pOp->p1<db->nDb ); |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 2346 | rc = sqlite3BtreeGetMeta(db->aDb[pOp->p1].pBt, 1, &iMeta); |
| 2347 | if( rc==SQLITE_OK && iMeta!=pOp->p2 ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2348 | sqlite3SetString(&p->zErrMsg, "database schema has changed", (char*)0); |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 2349 | rc = SQLITE_SCHEMA; |
| 2350 | } |
| 2351 | break; |
| 2352 | } |
| 2353 | |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2354 | /* Opcode: OpenRead P1 P2 P3 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2355 | ** |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2356 | ** Open a read-only cursor for the database table whose root page is |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2357 | ** P2 in a database file. The database file is determined by an |
| 2358 | ** integer from the top of the stack. 0 means the main database and |
| 2359 | ** 1 means the database used for temporary tables. Give the new |
| 2360 | ** cursor an identifier of P1. The P1 values need not be contiguous |
| 2361 | ** but all P1 values should be small integers. It is an error for |
| 2362 | ** P1 to be negative. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2363 | ** |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2364 | ** 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] | 2365 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2366 | ** There will be a read lock on the database whenever there is an |
| 2367 | ** open cursor. If the database was unlocked prior to this instruction |
| 2368 | ** then a read lock is acquired as part of this instruction. A read |
| 2369 | ** lock allows other processes to read the database but prohibits |
| 2370 | ** any other process from modifying the database. The read lock is |
| 2371 | ** released when all cursors are closed. If this instruction attempts |
| 2372 | ** to get a read lock but fails, the script terminates with an |
| 2373 | ** SQLITE_BUSY error code. |
| 2374 | ** |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 2375 | ** The P3 value is a pointer to a KeyInfo structure that defines the |
| 2376 | ** content and collating sequence of indices. P3 is NULL for cursors |
| 2377 | ** that are not pointing to indices. |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 2378 | ** |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2379 | ** See also OpenWrite. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2380 | */ |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2381 | /* Opcode: OpenWrite P1 P2 P3 |
| 2382 | ** |
| 2383 | ** Open a read/write cursor named P1 on the table or index whose root |
| 2384 | ** page is P2. If P2==0 then take the root page number from the stack. |
| 2385 | ** |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 2386 | ** The P3 value is a pointer to a KeyInfo structure that defines the |
| 2387 | ** content and collating sequence of indices. P3 is NULL for cursors |
| 2388 | ** that are not pointing to indices. |
jplyon | 5a56422 | 2003-06-02 06:15:58 +0000 | [diff] [blame] | 2389 | ** |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2390 | ** This instruction works just like OpenRead except that it opens the cursor |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2391 | ** in read/write mode. For a given table, there can be one or more read-only |
| 2392 | ** cursors or a single read/write cursor but not both. |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 2393 | ** |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2394 | ** See also OpenRead. |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2395 | */ |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2396 | case OP_OpenRead: |
| 2397 | case OP_OpenWrite: { |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2398 | int busy = 0; |
| 2399 | int i = pOp->p1; |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 2400 | int p2 = pOp->p2; |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 2401 | int wrFlag; |
| 2402 | Btree *pX; |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2403 | int iDb; |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 2404 | Cursor *pCur; |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2405 | |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 2406 | assert( pTos>=p->aStack ); |
danielk1977 | 8a6b541 | 2004-05-24 07:04:25 +0000 | [diff] [blame] | 2407 | Integerify(pTos, db->enc); |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 2408 | iDb = pTos->i; |
| 2409 | pTos--; |
| 2410 | assert( iDb>=0 && iDb<db->nDb ); |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2411 | pX = db->aDb[iDb].pBt; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 2412 | assert( pX!=0 ); |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2413 | wrFlag = pOp->opcode==OP_OpenWrite; |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 2414 | if( p2<=0 ){ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 2415 | assert( pTos>=p->aStack ); |
danielk1977 | 8a6b541 | 2004-05-24 07:04:25 +0000 | [diff] [blame] | 2416 | Integerify(pTos, db->enc); |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 2417 | p2 = pTos->i; |
| 2418 | pTos--; |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 2419 | if( p2<2 ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2420 | sqlite3SetString(&p->zErrMsg, "root page number less than 2", (char*)0); |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 2421 | rc = SQLITE_INTERNAL; |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 2422 | break; |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 2423 | } |
| 2424 | } |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 2425 | assert( i>=0 ); |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 2426 | if( expandCursorArraySize(p, i) ) goto no_mem; |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame] | 2427 | pCur = p->apCsr[i]; |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 2428 | sqlite3VdbeCleanupCursor(pCur); |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 2429 | pCur->nullRow = 1; |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 2430 | if( pX==0 ) break; |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 2431 | do{ |
danielk1977 | 452c989 | 2004-05-13 05:16:15 +0000 | [diff] [blame] | 2432 | /* When opening cursors, always supply the comparison function |
| 2433 | ** sqlite3VdbeKeyCompare(). If the table being opened is of type |
| 2434 | ** INTKEY, the btree layer won't call the comparison function anyway. |
| 2435 | */ |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 2436 | rc = sqlite3BtreeCursor(pX, p2, wrFlag, |
| 2437 | sqlite3VdbeKeyCompare, pOp->p3, |
| 2438 | &pCur->pCursor); |
| 2439 | pCur->pKeyInfo = (KeyInfo*)pOp->p3; |
| 2440 | if( pCur->pKeyInfo ){ |
| 2441 | pCur->pIncrKey = &pCur->pKeyInfo->incrKey; |
danielk1977 | b1bc953 | 2004-05-22 03:05:33 +0000 | [diff] [blame] | 2442 | pCur->pKeyInfo->enc = p->db->enc; |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 2443 | }else{ |
| 2444 | pCur->pIncrKey = &pCur->bogusIncrKey; |
| 2445 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2446 | switch( rc ){ |
| 2447 | case SQLITE_BUSY: { |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 2448 | if( db->xBusyCallback==0 ){ |
| 2449 | p->pc = pc; |
| 2450 | p->rc = SQLITE_BUSY; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 2451 | p->pTos = &pTos[1 + (pOp->p2<=0)]; /* Operands must remain on stack */ |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 2452 | return SQLITE_BUSY; |
| 2453 | }else if( (*db->xBusyCallback)(db->pBusyArg, pOp->p3, ++busy)==0 ){ |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 2454 | sqlite3SetString(&p->zErrMsg, sqlite3_error_string(rc), (char*)0); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2455 | busy = 0; |
| 2456 | } |
| 2457 | break; |
| 2458 | } |
| 2459 | case SQLITE_OK: { |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 2460 | int flags = sqlite3BtreeFlags(pCur->pCursor); |
| 2461 | pCur->intKey = (flags & BTREE_INTKEY)!=0; |
| 2462 | pCur->zeroData = (flags & BTREE_ZERODATA)!=0; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2463 | busy = 0; |
| 2464 | break; |
| 2465 | } |
drh | f4dada7 | 2004-05-11 09:57:35 +0000 | [diff] [blame] | 2466 | case SQLITE_EMPTY: { |
| 2467 | rc = SQLITE_OK; |
| 2468 | busy = 0; |
| 2469 | break; |
| 2470 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2471 | default: { |
| 2472 | goto abort_due_to_error; |
| 2473 | } |
| 2474 | } |
| 2475 | }while( busy ); |
| 2476 | break; |
| 2477 | } |
| 2478 | |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 2479 | /* Opcode: OpenTemp P1 * P3 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2480 | ** |
drh | 5fe2d8c | 2003-05-10 03:36:53 +0000 | [diff] [blame] | 2481 | ** Open a new cursor to a transient table. |
| 2482 | ** The transient cursor is always opened read/write even if |
| 2483 | ** the main database is read-only. The transient table is deleted |
| 2484 | ** automatically when the cursor is closed. |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 2485 | ** |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 2486 | ** The cursor points to a BTree table if P3==0 and to a BTree index |
| 2487 | ** if P3 is not 0. If P3 is not NULL, it points to a KeyInfo structure |
| 2488 | ** that defines the format of keys in the index. |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 2489 | ** |
| 2490 | ** This opcode is used for tables that exist for the duration of a single |
| 2491 | ** SQL statement only. Tables created using CREATE TEMPORARY TABLE |
drh | 5fe2d8c | 2003-05-10 03:36:53 +0000 | [diff] [blame] | 2492 | ** are opened using OP_OpenRead or OP_OpenWrite. "Temporary" in the |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 2493 | ** context of this opcode means for the duration of a single SQL statement |
| 2494 | ** whereas "Temporary" in the context of CREATE TABLE means for the duration |
| 2495 | ** of the connection to the database. Same word; different meanings. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2496 | */ |
| 2497 | case OP_OpenTemp: { |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2498 | int i = pOp->p1; |
| 2499 | Cursor *pCx; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 2500 | assert( i>=0 ); |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 2501 | if( expandCursorArraySize(p, i) ) goto no_mem; |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame] | 2502 | pCx = p->apCsr[i]; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2503 | sqlite3VdbeCleanupCursor(pCx); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2504 | memset(pCx, 0, sizeof(*pCx)); |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 2505 | pCx->nullRow = 1; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2506 | rc = sqlite3BtreeFactory(db, 0, 1, TEMP_PAGES, &pCx->pBt); |
paul | b0208cc | 2003-04-13 18:26:49 +0000 | [diff] [blame] | 2507 | |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2508 | if( rc==SQLITE_OK ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2509 | rc = sqlite3BtreeBeginTrans(pCx->pBt); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2510 | } |
| 2511 | if( rc==SQLITE_OK ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2512 | /* If a transient index is required, create it by calling |
| 2513 | ** sqlite3BtreeCreateTable() with the BTREE_ZERODATA flag before |
| 2514 | ** opening it. If a transient table is required, just use the |
danielk1977 | 0dbe72b | 2004-05-11 04:54:49 +0000 | [diff] [blame] | 2515 | ** automatically created table with root-page 1 (an INTKEY table). |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2516 | */ |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 2517 | if( pOp->p3 ){ |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 2518 | int pgno; |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 2519 | assert( pOp->p3type==P3_KEYINFO ); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2520 | rc = sqlite3BtreeCreateTable(pCx->pBt, &pgno, BTREE_ZERODATA); |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 2521 | if( rc==SQLITE_OK ){ |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 2522 | assert( pgno==MASTER_ROOT+1 ); |
danielk1977 | e014a83 | 2004-05-17 10:48:57 +0000 | [diff] [blame] | 2523 | rc = sqlite3BtreeCursor(pCx->pBt, pgno, 1, sqlite3VdbeKeyCompare, |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 2524 | pOp->p3, &pCx->pCursor); |
| 2525 | pCx->pKeyInfo = (KeyInfo*)pOp->p3; |
danielk1977 | b1bc953 | 2004-05-22 03:05:33 +0000 | [diff] [blame] | 2526 | pCx->pKeyInfo->enc = p->db->enc; |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 2527 | pCx->pIncrKey = &pCx->pKeyInfo->incrKey; |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 2528 | } |
| 2529 | }else{ |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 2530 | rc = sqlite3BtreeCursor(pCx->pBt, MASTER_ROOT, 1, 0, 0, &pCx->pCursor); |
danielk1977 | 0dbe72b | 2004-05-11 04:54:49 +0000 | [diff] [blame] | 2531 | pCx->intKey = 1; |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 2532 | pCx->pIncrKey = &pCx->bogusIncrKey; |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 2533 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2534 | } |
| 2535 | break; |
| 2536 | } |
| 2537 | |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 2538 | /* Opcode: OpenPseudo P1 * * |
| 2539 | ** |
| 2540 | ** Open a new cursor that points to a fake table that contains a single |
| 2541 | ** row of data. Any attempt to write a second row of data causes the |
| 2542 | ** first row to be deleted. All data is deleted when the cursor is |
| 2543 | ** closed. |
| 2544 | ** |
| 2545 | ** A pseudo-table created by this opcode is useful for holding the |
| 2546 | ** NEW or OLD tables in a trigger. |
| 2547 | */ |
| 2548 | case OP_OpenPseudo: { |
| 2549 | int i = pOp->p1; |
| 2550 | Cursor *pCx; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 2551 | assert( i>=0 ); |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 2552 | if( expandCursorArraySize(p, i) ) goto no_mem; |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame] | 2553 | pCx = p->apCsr[i]; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2554 | sqlite3VdbeCleanupCursor(pCx); |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 2555 | memset(pCx, 0, sizeof(*pCx)); |
| 2556 | pCx->nullRow = 1; |
| 2557 | pCx->pseudoTable = 1; |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 2558 | pCx->pIncrKey = &pCx->bogusIncrKey; |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 2559 | break; |
| 2560 | } |
| 2561 | |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2562 | /* Opcode: Close P1 * * |
| 2563 | ** |
| 2564 | ** Close a cursor previously opened as P1. If P1 is not |
| 2565 | ** currently open, this instruction is a no-op. |
| 2566 | */ |
| 2567 | case OP_Close: { |
| 2568 | int i = pOp->p1; |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 2569 | if( i>=0 && i<p->nCursor ){ |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame] | 2570 | sqlite3VdbeCleanupCursor(p->apCsr[i]); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2571 | } |
| 2572 | break; |
| 2573 | } |
| 2574 | |
drh | 7cf6e4d | 2004-05-19 14:56:55 +0000 | [diff] [blame] | 2575 | /* Opcode: MoveGe P1 P2 * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2576 | ** |
| 2577 | ** Pop the top of the stack and use its value as a key. Reposition |
drh | 7cf6e4d | 2004-05-19 14:56:55 +0000 | [diff] [blame] | 2578 | ** cursor P1 so that it points to the smallest entry that is greater |
| 2579 | ** than or equal to the key that was popped ffrom the stack. |
| 2580 | ** If there are no records greater than or equal to the key and P2 |
| 2581 | ** is not zero, then jump to P2. |
| 2582 | ** |
| 2583 | ** See also: Found, NotFound, Distinct, MoveLt, MoveGt, MoveLe |
| 2584 | */ |
| 2585 | /* Opcode: MoveGt P1 P2 * |
| 2586 | ** |
| 2587 | ** Pop the top of the stack and use its value as a key. Reposition |
| 2588 | ** cursor P1 so that it points to the smallest entry that is greater |
| 2589 | ** than the key from the stack. |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 2590 | ** If there are no records greater than the key and P2 is not zero, |
drh | 7cf6e4d | 2004-05-19 14:56:55 +0000 | [diff] [blame] | 2591 | ** then jump to P2. |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2592 | ** |
drh | 7cf6e4d | 2004-05-19 14:56:55 +0000 | [diff] [blame] | 2593 | ** See also: Found, NotFound, Distinct, MoveLt, MoveGe, MoveLe |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2594 | */ |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 2595 | /* Opcode: MoveLt P1 P2 * |
| 2596 | ** |
| 2597 | ** Pop the top of the stack and use its value as a key. Reposition |
drh | 7cf6e4d | 2004-05-19 14:56:55 +0000 | [diff] [blame] | 2598 | ** cursor P1 so that it points to the largest entry that is less |
| 2599 | ** than the key from the stack. |
| 2600 | ** If there are no records less than the key and P2 is not zero, |
| 2601 | ** then jump to P2. |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 2602 | ** |
drh | 7cf6e4d | 2004-05-19 14:56:55 +0000 | [diff] [blame] | 2603 | ** See also: Found, NotFound, Distinct, MoveGt, MoveGe, MoveLe |
| 2604 | */ |
| 2605 | /* Opcode: MoveLe P1 P2 * |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 2606 | ** |
drh | 7cf6e4d | 2004-05-19 14:56:55 +0000 | [diff] [blame] | 2607 | ** Pop the top of the stack and use its value as a key. Reposition |
| 2608 | ** cursor P1 so that it points to the largest entry that is less than |
| 2609 | ** or equal to the key that was popped from the stack. |
| 2610 | ** If there are no records less than or eqal to the key and P2 is not zero, |
| 2611 | ** then jump to P2. |
| 2612 | ** |
| 2613 | ** See also: Found, NotFound, Distinct, MoveGt, MoveGe, MoveLt |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 2614 | */ |
| 2615 | case OP_MoveLt: |
drh | 7cf6e4d | 2004-05-19 14:56:55 +0000 | [diff] [blame] | 2616 | case OP_MoveLe: |
| 2617 | case OP_MoveGe: |
| 2618 | case OP_MoveGt: { |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2619 | int i = pOp->p1; |
drh | 80ff32f | 2001-11-04 18:32:46 +0000 | [diff] [blame] | 2620 | Cursor *pC; |
| 2621 | |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 2622 | assert( pTos>=p->aStack ); |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 2623 | assert( i>=0 && i<p->nCursor ); |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame] | 2624 | pC = p->apCsr[i]; |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 2625 | if( pC->pCursor!=0 ){ |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 2626 | int res, oc; |
drh | 7cf6e4d | 2004-05-19 14:56:55 +0000 | [diff] [blame] | 2627 | oc = pOp->opcode; |
drh | a11846b | 2004-01-07 18:52:56 +0000 | [diff] [blame] | 2628 | pC->nullRow = 0; |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 2629 | *pC->pIncrKey = oc==OP_MoveGt || oc==OP_MoveLe; |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 2630 | if( pC->intKey ){ |
| 2631 | i64 iKey; |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 2632 | assert( !pOp->p3 ); |
danielk1977 | 8a6b541 | 2004-05-24 07:04:25 +0000 | [diff] [blame] | 2633 | Integerify(pTos, db->enc); |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 2634 | iKey = intToKey(pTos->i); |
drh | 7cf6e4d | 2004-05-19 14:56:55 +0000 | [diff] [blame] | 2635 | if( pOp->p2==0 && pOp->opcode==OP_MoveGe ){ |
drh | a11846b | 2004-01-07 18:52:56 +0000 | [diff] [blame] | 2636 | pC->movetoTarget = iKey; |
| 2637 | pC->deferredMoveto = 1; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 2638 | Release(pTos); |
| 2639 | pTos--; |
drh | a11846b | 2004-01-07 18:52:56 +0000 | [diff] [blame] | 2640 | break; |
| 2641 | } |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 2642 | sqlite3BtreeMoveto(pC->pCursor, 0, (u64)iKey, &res); |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 2643 | pC->lastRecno = pTos->i; |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 2644 | pC->recnoIsValid = res==0; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2645 | }else{ |
danielk1977 | 8a6b541 | 2004-05-24 07:04:25 +0000 | [diff] [blame] | 2646 | Stringify(pTos, db->enc); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2647 | sqlite3BtreeMoveto(pC->pCursor, pTos->z, pTos->n, &res); |
drh | 80ff32f | 2001-11-04 18:32:46 +0000 | [diff] [blame] | 2648 | pC->recnoIsValid = 0; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2649 | } |
drh | a11846b | 2004-01-07 18:52:56 +0000 | [diff] [blame] | 2650 | pC->deferredMoveto = 0; |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2651 | pC->cacheValid = 0; |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 2652 | *pC->pIncrKey = 0; |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 2653 | sqlite3_search_count++; |
drh | 7cf6e4d | 2004-05-19 14:56:55 +0000 | [diff] [blame] | 2654 | if( oc==OP_MoveGe || oc==OP_MoveGt ){ |
| 2655 | if( res<0 ){ |
| 2656 | sqlite3BtreeNext(pC->pCursor, &res); |
| 2657 | pC->recnoIsValid = 0; |
| 2658 | if( res && pOp->p2>0 ){ |
| 2659 | pc = pOp->p2 - 1; |
| 2660 | } |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 2661 | } |
drh | 7cf6e4d | 2004-05-19 14:56:55 +0000 | [diff] [blame] | 2662 | }else{ |
| 2663 | assert( oc==OP_MoveLt || oc==OP_MoveLe ); |
drh | 1a844c3 | 2002-12-04 22:29:28 +0000 | [diff] [blame] | 2664 | if( res>=0 ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2665 | sqlite3BtreePrevious(pC->pCursor, &res); |
drh | 1a844c3 | 2002-12-04 22:29:28 +0000 | [diff] [blame] | 2666 | pC->recnoIsValid = 0; |
| 2667 | }else{ |
| 2668 | /* res might be negative because the table is empty. Check to |
| 2669 | ** see if this is the case. |
| 2670 | */ |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 2671 | res = sqlite3BtreeEof(pC->pCursor); |
drh | 1a844c3 | 2002-12-04 22:29:28 +0000 | [diff] [blame] | 2672 | } |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 2673 | if( res && pOp->p2>0 ){ |
| 2674 | pc = pOp->p2 - 1; |
| 2675 | } |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 2676 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2677 | } |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 2678 | Release(pTos); |
| 2679 | pTos--; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2680 | break; |
| 2681 | } |
| 2682 | |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2683 | /* Opcode: Distinct P1 P2 * |
| 2684 | ** |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 2685 | ** 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] | 2686 | ** 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] | 2687 | ** does already exist, then fall thru. The cursor is left pointing |
| 2688 | ** 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] | 2689 | ** |
| 2690 | ** This operation is similar to NotFound except that this operation |
| 2691 | ** does not pop the key from the stack. |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2692 | ** |
drh | d99f706 | 2002-06-08 23:25:08 +0000 | [diff] [blame] | 2693 | ** See also: Found, NotFound, MoveTo, IsUnique, NotExists |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2694 | */ |
| 2695 | /* Opcode: Found P1 P2 * |
| 2696 | ** |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 2697 | ** 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] | 2698 | ** does exist in table of P1, then jump to P2. If the record |
drh | 3fc190c | 2001-09-14 03:24:23 +0000 | [diff] [blame] | 2699 | ** does not exist, then fall thru. The cursor is left pointing |
| 2700 | ** to the record if it exists. The key is popped from the stack. |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2701 | ** |
drh | d99f706 | 2002-06-08 23:25:08 +0000 | [diff] [blame] | 2702 | ** See also: Distinct, NotFound, MoveTo, IsUnique, NotExists |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2703 | */ |
| 2704 | /* Opcode: NotFound P1 P2 * |
| 2705 | ** |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 2706 | ** 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] | 2707 | ** 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] | 2708 | ** does exist, then fall thru. The cursor is left pointing to the |
| 2709 | ** record if it exists. The key is popped from the stack. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2710 | ** |
| 2711 | ** The difference between this operation and Distinct is that |
| 2712 | ** Distinct does not pop the key from the stack. |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2713 | ** |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 2714 | ** See also: Distinct, Found, MoveTo, NotExists, IsUnique |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2715 | */ |
| 2716 | case OP_Distinct: |
| 2717 | case OP_NotFound: |
| 2718 | case OP_Found: { |
| 2719 | int i = pOp->p1; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2720 | int alreadyExists = 0; |
drh | 80ff32f | 2001-11-04 18:32:46 +0000 | [diff] [blame] | 2721 | Cursor *pC; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 2722 | assert( pTos>=p->aStack ); |
| 2723 | assert( i>=0 && i<p->nCursor ); |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame] | 2724 | if( (pC = p->apCsr[i])->pCursor!=0 ){ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2725 | int res, rx; |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 2726 | assert( pC->intKey==0 ); |
danielk1977 | 8a6b541 | 2004-05-24 07:04:25 +0000 | [diff] [blame] | 2727 | Stringify(pTos, db->enc); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2728 | rx = sqlite3BtreeMoveto(pC->pCursor, pTos->z, pTos->n, &res); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2729 | alreadyExists = rx==SQLITE_OK && res==0; |
drh | a11846b | 2004-01-07 18:52:56 +0000 | [diff] [blame] | 2730 | pC->deferredMoveto = 0; |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2731 | pC->cacheValid = 0; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2732 | } |
| 2733 | if( pOp->opcode==OP_Found ){ |
| 2734 | if( alreadyExists ) pc = pOp->p2 - 1; |
| 2735 | }else{ |
| 2736 | if( !alreadyExists ) pc = pOp->p2 - 1; |
| 2737 | } |
| 2738 | if( pOp->opcode!=OP_Distinct ){ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 2739 | Release(pTos); |
| 2740 | pTos--; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2741 | } |
| 2742 | break; |
| 2743 | } |
| 2744 | |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 2745 | /* Opcode: IsUnique P1 P2 * |
| 2746 | ** |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 2747 | ** The top of the stack is an integer record number. Call this |
| 2748 | ** record number R. The next on the stack is an index key created |
| 2749 | ** using MakeIdxKey. Call it K. This instruction pops R from the |
| 2750 | ** stack but it leaves K unchanged. |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 2751 | ** |
drh | 7cf6e4d | 2004-05-19 14:56:55 +0000 | [diff] [blame] | 2752 | ** P1 is an index. So it has no data and its key consists of a |
| 2753 | ** record generated by OP_MakeIdxKey. This key contains one or more |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 2754 | ** fields followed by a ROWID field. |
| 2755 | ** |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 2756 | ** This instruction asks if there is an entry in P1 where the |
drh | 7cf6e4d | 2004-05-19 14:56:55 +0000 | [diff] [blame] | 2757 | ** fields matches K but the rowid is different from R. |
| 2758 | ** If there is no such entry, then there is an immediate |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 2759 | ** jump to P2. If any entry does exist where the index string |
| 2760 | ** matches K but the record number is not R, then the record |
| 2761 | ** number for that entry is pushed onto the stack and control |
| 2762 | ** falls through to the next instruction. |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 2763 | ** |
drh | d99f706 | 2002-06-08 23:25:08 +0000 | [diff] [blame] | 2764 | ** See also: Distinct, NotFound, NotExists, Found |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 2765 | */ |
| 2766 | case OP_IsUnique: { |
| 2767 | int i = pOp->p1; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 2768 | Mem *pNos = &pTos[-1]; |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 2769 | Cursor *pCx; |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 2770 | BtCursor *pCrsr; |
danielk1977 | 452c989 | 2004-05-13 05:16:15 +0000 | [diff] [blame] | 2771 | i64 R; |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 2772 | |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 2773 | /* Pop the value R off the top of the stack |
| 2774 | */ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 2775 | assert( pNos>=p->aStack ); |
danielk1977 | 8a6b541 | 2004-05-24 07:04:25 +0000 | [diff] [blame] | 2776 | Integerify(pTos, db->enc); |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 2777 | R = pTos->i; |
| 2778 | pTos--; |
| 2779 | assert( i>=0 && i<=p->nCursor ); |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame] | 2780 | pCx = p->apCsr[i]; |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 2781 | pCrsr = pCx->pCursor; |
| 2782 | if( pCrsr!=0 ){ |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 2783 | int res, rc; |
danielk1977 | 452c989 | 2004-05-13 05:16:15 +0000 | [diff] [blame] | 2784 | i64 v; /* The record number on the P1 entry that matches K */ |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 2785 | char *zKey; /* The value of K */ |
| 2786 | int nKey; /* Number of bytes in K */ |
danielk1977 | 452c989 | 2004-05-13 05:16:15 +0000 | [diff] [blame] | 2787 | int len; /* Number of bytes in K without the rowid at the end */ |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 2788 | int szRowid; /* Size of the rowid column at the end of zKey */ |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 2789 | |
| 2790 | /* Make sure K is a string and make zKey point to K |
| 2791 | */ |
danielk1977 | 8a6b541 | 2004-05-24 07:04:25 +0000 | [diff] [blame] | 2792 | Stringify(pNos, db->enc); |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 2793 | zKey = pNos->z; |
| 2794 | nKey = pNos->n; |
danielk1977 | 452c989 | 2004-05-13 05:16:15 +0000 | [diff] [blame] | 2795 | |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 2796 | szRowid = sqlite3VdbeIdxRowidLen(nKey, zKey); |
| 2797 | len = nKey-szRowid; |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 2798 | |
drh | a3b321d | 2004-05-11 09:31:31 +0000 | [diff] [blame] | 2799 | /* 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] | 2800 | ** If there is no such entry, jump immediately to P2. |
| 2801 | */ |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2802 | assert( pCx->deferredMoveto==0 ); |
| 2803 | pCx->cacheValid = 0; |
danielk1977 | 452c989 | 2004-05-13 05:16:15 +0000 | [diff] [blame] | 2804 | rc = sqlite3BtreeMoveto(pCrsr, zKey, len, &res); |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 2805 | if( rc!=SQLITE_OK ) goto abort_due_to_error; |
| 2806 | if( res<0 ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2807 | rc = sqlite3BtreeNext(pCrsr, &res); |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 2808 | if( res ){ |
| 2809 | pc = pOp->p2 - 1; |
| 2810 | break; |
| 2811 | } |
| 2812 | } |
drh | 7cf6e4d | 2004-05-19 14:56:55 +0000 | [diff] [blame] | 2813 | rc = sqlite3VdbeIdxKeyCompare(pCx, len, zKey, &res); |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 2814 | if( rc!=SQLITE_OK ) goto abort_due_to_error; |
| 2815 | if( res>0 ){ |
| 2816 | pc = pOp->p2 - 1; |
| 2817 | break; |
| 2818 | } |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 2819 | |
| 2820 | /* At this point, pCrsr is pointing to an entry in P1 where all but |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 2821 | ** the final entry (the rowid) matches K. Check to see if the |
| 2822 | ** final rowid column is different from R. If it equals R then jump |
danielk1977 | 452c989 | 2004-05-13 05:16:15 +0000 | [diff] [blame] | 2823 | ** immediately to P2. |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 2824 | */ |
danielk1977 | 452c989 | 2004-05-13 05:16:15 +0000 | [diff] [blame] | 2825 | rc = sqlite3VdbeIdxRowid(pCrsr, &v); |
| 2826 | if( rc!=SQLITE_OK ){ |
| 2827 | goto abort_due_to_error; |
| 2828 | } |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 2829 | if( v==R ){ |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 2830 | pc = pOp->p2 - 1; |
| 2831 | break; |
| 2832 | } |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 2833 | |
danielk1977 | 452c989 | 2004-05-13 05:16:15 +0000 | [diff] [blame] | 2834 | /* The final varint of the key is different from R. Push it onto |
| 2835 | ** the stack. (The record number of an entry that violates a UNIQUE |
| 2836 | ** constraint.) |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 2837 | */ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 2838 | pTos++; |
| 2839 | pTos->i = v; |
| 2840 | pTos->flags = MEM_Int; |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 2841 | } |
| 2842 | break; |
| 2843 | } |
| 2844 | |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 2845 | /* Opcode: NotExists P1 P2 * |
| 2846 | ** |
| 2847 | ** Use the top of the stack as a integer key. If a record with that key |
| 2848 | ** does not exist in table of P1, then jump to P2. If the record |
| 2849 | ** does exist, then fall thru. The cursor is left pointing to the |
| 2850 | ** record if it exists. The integer key is popped from the stack. |
| 2851 | ** |
| 2852 | ** The difference between this operation and NotFound is that this |
| 2853 | ** operation assumes the key is an integer and NotFound assumes it |
| 2854 | ** is a string. |
| 2855 | ** |
drh | d99f706 | 2002-06-08 23:25:08 +0000 | [diff] [blame] | 2856 | ** See also: Distinct, Found, MoveTo, NotFound, IsUnique |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 2857 | */ |
| 2858 | case OP_NotExists: { |
| 2859 | int i = pOp->p1; |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2860 | Cursor *pC; |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 2861 | BtCursor *pCrsr; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 2862 | assert( pTos>=p->aStack ); |
| 2863 | assert( i>=0 && i<p->nCursor ); |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame] | 2864 | if( (pCrsr = (pC = p->apCsr[i])->pCursor)!=0 ){ |
danielk1977 | 36a3c70 | 2004-05-11 06:55:14 +0000 | [diff] [blame] | 2865 | int res, rx; |
| 2866 | u64 iKey; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 2867 | assert( pTos->flags & MEM_Int ); |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame] | 2868 | assert( p->apCsr[i]->intKey ); |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 2869 | iKey = intToKey(pTos->i); |
danielk1977 | 36a3c70 | 2004-05-11 06:55:14 +0000 | [diff] [blame] | 2870 | rx = sqlite3BtreeMoveto(pCrsr, 0, iKey, &res); |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2871 | pC->lastRecno = pTos->i; |
| 2872 | pC->recnoIsValid = res==0; |
| 2873 | pC->nullRow = 0; |
| 2874 | pC->cacheValid = 0; |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 2875 | if( rx!=SQLITE_OK || res!=0 ){ |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 2876 | pc = pOp->p2 - 1; |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2877 | pC->recnoIsValid = 0; |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 2878 | } |
| 2879 | } |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 2880 | Release(pTos); |
| 2881 | pTos--; |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 2882 | break; |
| 2883 | } |
| 2884 | |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2885 | /* Opcode: NewRecno P1 * * |
| 2886 | ** |
| 2887 | ** Get a new integer record number used as the key to a table. |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2888 | ** The record number is not previously used as a key in the database |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 2889 | ** table that cursor P1 points to. The new record number is pushed |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2890 | ** onto the stack. |
| 2891 | */ |
| 2892 | case OP_NewRecno: { |
| 2893 | int i = pOp->p1; |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 2894 | i64 v = 0; |
drh | 80ff32f | 2001-11-04 18:32:46 +0000 | [diff] [blame] | 2895 | Cursor *pC; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 2896 | assert( i>=0 && i<p->nCursor ); |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame] | 2897 | if( (pC = p->apCsr[i])->pCursor==0 ){ |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 2898 | /* The zero initialization above is all that is needed */ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2899 | }else{ |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 2900 | /* The next rowid or record number (different terms for the same |
| 2901 | ** thing) is obtained in a two-step algorithm. |
| 2902 | ** |
| 2903 | ** First we attempt to find the largest existing rowid and add one |
| 2904 | ** to that. But if the largest existing rowid is already the maximum |
| 2905 | ** positive integer, we have to fall through to the second |
| 2906 | ** probabilistic algorithm |
| 2907 | ** |
| 2908 | ** The second algorithm is to select a rowid at random and see if |
| 2909 | ** it already exists in the table. If it does not exist, we have |
| 2910 | ** succeeded. If the random rowid does exist, we select a new one |
| 2911 | ** and try again, up to 1000 times. |
drh | db5ed6d | 2001-09-18 22:17:44 +0000 | [diff] [blame] | 2912 | ** |
| 2913 | ** For a table with less than 2 billion entries, the probability |
| 2914 | ** of not finding a unused rowid is about 1.0e-300. This is a |
| 2915 | ** non-zero probability, but it is still vanishingly small and should |
| 2916 | ** never cause a problem. You are much, much more likely to have a |
| 2917 | ** hardware failure than for this algorithm to fail. |
| 2918 | ** |
drh | af9ff33 | 2002-01-16 21:00:27 +0000 | [diff] [blame] | 2919 | ** The analysis in the previous paragraph assumes that you have a good |
| 2920 | ** source of random numbers. Is a library function like lrand48() |
| 2921 | ** good enough? Maybe. Maybe not. It's hard to know whether there |
| 2922 | ** might be subtle bugs is some implementations of lrand48() that |
| 2923 | ** could cause problems. To avoid uncertainty, SQLite uses its own |
| 2924 | ** random number generator based on the RC4 algorithm. |
| 2925 | ** |
drh | db5ed6d | 2001-09-18 22:17:44 +0000 | [diff] [blame] | 2926 | ** To promote locality of reference for repetitive inserts, the |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 2927 | ** first few attempts at chosing a random rowid pick values just a little |
drh | db5ed6d | 2001-09-18 22:17:44 +0000 | [diff] [blame] | 2928 | ** larger than the previous rowid. This has been shown experimentally |
| 2929 | ** to double the speed of the COPY operation. |
| 2930 | */ |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 2931 | int res, rx, cnt; |
| 2932 | i64 x; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2933 | cnt = 0; |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 2934 | assert( (sqlite3BtreeFlags(pC->pCursor) & BTREE_INTKEY)!=0 ); |
| 2935 | assert( (sqlite3BtreeFlags(pC->pCursor) & BTREE_ZERODATA)==0 ); |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 2936 | if( !pC->useRandomRowid ){ |
drh | 32fbe34 | 2002-10-19 20:16:37 +0000 | [diff] [blame] | 2937 | if( pC->nextRowidValid ){ |
| 2938 | v = pC->nextRowid; |
drh | 3fc190c | 2001-09-14 03:24:23 +0000 | [diff] [blame] | 2939 | }else{ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2940 | rx = sqlite3BtreeLast(pC->pCursor, &res); |
drh | 32fbe34 | 2002-10-19 20:16:37 +0000 | [diff] [blame] | 2941 | if( res ){ |
| 2942 | v = 1; |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 2943 | }else{ |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 2944 | sqlite3BtreeKeySize(pC->pCursor, (u64*)&v); |
drh | 32fbe34 | 2002-10-19 20:16:37 +0000 | [diff] [blame] | 2945 | v = keyToInt(v); |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 2946 | if( v==0x7fffffffffffffff ){ |
drh | 32fbe34 | 2002-10-19 20:16:37 +0000 | [diff] [blame] | 2947 | pC->useRandomRowid = 1; |
| 2948 | }else{ |
| 2949 | v++; |
| 2950 | } |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 2951 | } |
drh | 3fc190c | 2001-09-14 03:24:23 +0000 | [diff] [blame] | 2952 | } |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 2953 | if( v<0x7fffffffffffffff ){ |
drh | 32fbe34 | 2002-10-19 20:16:37 +0000 | [diff] [blame] | 2954 | pC->nextRowidValid = 1; |
| 2955 | pC->nextRowid = v+1; |
| 2956 | }else{ |
| 2957 | pC->nextRowidValid = 0; |
| 2958 | } |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 2959 | } |
| 2960 | if( pC->useRandomRowid ){ |
| 2961 | v = db->priorNewRowid; |
| 2962 | cnt = 0; |
| 2963 | do{ |
| 2964 | if( v==0 || cnt>2 ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2965 | sqlite3Randomness(sizeof(v), &v); |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 2966 | if( cnt<5 ) v &= 0xffffff; |
| 2967 | }else{ |
drh | bbd82df | 2004-02-11 09:46:30 +0000 | [diff] [blame] | 2968 | unsigned char r; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2969 | sqlite3Randomness(1, &r); |
drh | bbd82df | 2004-02-11 09:46:30 +0000 | [diff] [blame] | 2970 | v += r + 1; |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 2971 | } |
| 2972 | if( v==0 ) continue; |
| 2973 | x = intToKey(v); |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 2974 | rx = sqlite3BtreeMoveto(pC->pCursor, 0, (u64)x, &res); |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 2975 | cnt++; |
| 2976 | }while( cnt<1000 && rx==SQLITE_OK && res==0 ); |
| 2977 | db->priorNewRowid = v; |
| 2978 | if( rx==SQLITE_OK && res==0 ){ |
| 2979 | rc = SQLITE_FULL; |
| 2980 | goto abort_due_to_error; |
| 2981 | } |
drh | 1eaa269 | 2001-09-18 02:02:23 +0000 | [diff] [blame] | 2982 | } |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 2983 | pC->recnoIsValid = 0; |
drh | a11846b | 2004-01-07 18:52:56 +0000 | [diff] [blame] | 2984 | pC->deferredMoveto = 0; |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2985 | pC->cacheValid = 0; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2986 | } |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 2987 | pTos++; |
| 2988 | pTos->i = v; |
| 2989 | pTos->flags = MEM_Int; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2990 | break; |
| 2991 | } |
| 2992 | |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 2993 | /* Opcode: PutIntKey P1 P2 * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2994 | ** |
jplyon | 5a56422 | 2003-06-02 06:15:58 +0000 | [diff] [blame] | 2995 | ** Write an entry into the table of cursor P1. A new entry is |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2996 | ** created if it doesn't already exist or the data for an existing |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2997 | ** entry is overwritten. The data is the value on the top of the |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 2998 | ** stack. The key is the next value down on the stack. The key must |
| 2999 | ** be an integer. The stack is popped twice by this instruction. |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 3000 | ** |
rdc | b0c374f | 2004-02-20 22:53:38 +0000 | [diff] [blame] | 3001 | ** If the OPFLAG_NCHANGE flag of P2 is set, then the row change count is |
| 3002 | ** incremented (otherwise not). If the OPFLAG_CSCHANGE flag is set, |
| 3003 | ** then the current statement change count is incremented (otherwise not). |
| 3004 | ** If the OPFLAG_LASTROWID flag of P2 is set, then rowid is |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 3005 | ** stored for subsequent return by the sqlite3_last_insert_rowid() function |
rdc | b0c374f | 2004-02-20 22:53:38 +0000 | [diff] [blame] | 3006 | ** (otherwise it's unmodified). |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3007 | */ |
drh | c8d30ac | 2002-04-12 10:08:59 +0000 | [diff] [blame] | 3008 | /* Opcode: PutStrKey P1 * * |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 3009 | ** |
jplyon | 5a56422 | 2003-06-02 06:15:58 +0000 | [diff] [blame] | 3010 | ** Write an entry into the table of cursor P1. A new entry is |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 3011 | ** created if it doesn't already exist or the data for an existing |
| 3012 | ** entry is overwritten. The data is the value on the top of the |
| 3013 | ** stack. The key is the next value down on the stack. The key must |
| 3014 | ** be a string. The stack is popped twice by this instruction. |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3015 | ** |
| 3016 | ** P1 may not be a pseudo-table opened using the OpenPseudo opcode. |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 3017 | */ |
| 3018 | case OP_PutIntKey: |
| 3019 | case OP_PutStrKey: { |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3020 | Mem *pNos = &pTos[-1]; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3021 | int i = pOp->p1; |
drh | 32fbe34 | 2002-10-19 20:16:37 +0000 | [diff] [blame] | 3022 | Cursor *pC; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3023 | assert( pNos>=p->aStack ); |
| 3024 | assert( i>=0 && i<p->nCursor ); |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame] | 3025 | if( ((pC = p->apCsr[i])->pCursor!=0 || pC->pseudoTable) ){ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3026 | char *zKey; |
danielk1977 | 5f8d8a8 | 2004-05-11 00:28:42 +0000 | [diff] [blame] | 3027 | i64 nKey; |
danielk1977 | e7c8d58 | 2004-05-13 13:38:52 +0000 | [diff] [blame] | 3028 | i64 iKey; |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 3029 | if( pOp->opcode==OP_PutStrKey ){ |
danielk1977 | 8a6b541 | 2004-05-24 07:04:25 +0000 | [diff] [blame] | 3030 | Stringify(pNos, db->enc); |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3031 | nKey = pNos->n; |
| 3032 | zKey = pNos->z; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3033 | }else{ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3034 | assert( pNos->flags & MEM_Int ); |
danielk1977 | 5f8d8a8 | 2004-05-11 00:28:42 +0000 | [diff] [blame] | 3035 | |
| 3036 | /* If the table is an INTKEY table, set nKey to the value of |
danielk1977 | 0dbe72b | 2004-05-11 04:54:49 +0000 | [diff] [blame] | 3037 | ** the integer key, and zKey to NULL. Otherwise, set nKey to |
| 3038 | ** sizeof(i64) and point zKey at iKey. iKey contains the integer |
| 3039 | ** key in the on-disk byte order. |
danielk1977 | 5f8d8a8 | 2004-05-11 00:28:42 +0000 | [diff] [blame] | 3040 | */ |
danielk1977 | 0dbe72b | 2004-05-11 04:54:49 +0000 | [diff] [blame] | 3041 | iKey = intToKey(pNos->i); |
danielk1977 | 5f8d8a8 | 2004-05-11 00:28:42 +0000 | [diff] [blame] | 3042 | if( pC->intKey ){ |
danielk1977 | 49f737d | 2004-05-11 02:10:06 +0000 | [diff] [blame] | 3043 | nKey = intToKey(pNos->i); |
danielk1977 | 5f8d8a8 | 2004-05-11 00:28:42 +0000 | [diff] [blame] | 3044 | zKey = 0; |
| 3045 | }else{ |
danielk1977 | 5f8d8a8 | 2004-05-11 00:28:42 +0000 | [diff] [blame] | 3046 | nKey = sizeof(i64); |
| 3047 | zKey = (char*)&iKey; |
| 3048 | } |
danielk1977 | 5f8d8a8 | 2004-05-11 00:28:42 +0000 | [diff] [blame] | 3049 | |
rdc | b0c374f | 2004-02-20 22:53:38 +0000 | [diff] [blame] | 3050 | if( pOp->p2 & OPFLAG_NCHANGE ) db->nChange++; |
| 3051 | if( pOp->p2 & OPFLAG_LASTROWID ) db->lastRowid = pNos->i; |
| 3052 | if( pOp->p2 & OPFLAG_CSCHANGE ) db->csChange++; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3053 | if( pC->nextRowidValid && pTos->i>=pC->nextRowid ){ |
drh | 32fbe34 | 2002-10-19 20:16:37 +0000 | [diff] [blame] | 3054 | pC->nextRowidValid = 0; |
| 3055 | } |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 3056 | } |
drh | 78a7583 | 2004-02-13 14:07:12 +0000 | [diff] [blame] | 3057 | if( pTos->flags & MEM_Null ){ |
| 3058 | pTos->z = 0; |
| 3059 | pTos->n = 0; |
| 3060 | }else{ |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 3061 | assert( pTos->flags & (MEM_Blob|MEM_Str) ); |
drh | 78a7583 | 2004-02-13 14:07:12 +0000 | [diff] [blame] | 3062 | } |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3063 | if( pC->pseudoTable ){ |
| 3064 | /* PutStrKey does not work for pseudo-tables. |
| 3065 | ** The following assert makes sure we are not trying to use |
| 3066 | ** PutStrKey on a pseudo-table |
| 3067 | */ |
| 3068 | assert( pOp->opcode==OP_PutIntKey ); |
| 3069 | sqliteFree(pC->pData); |
| 3070 | pC->iKey = iKey; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3071 | pC->nData = pTos->n; |
| 3072 | if( pTos->flags & MEM_Dyn ){ |
| 3073 | pC->pData = pTos->z; |
| 3074 | pTos->flags = MEM_Null; |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3075 | }else{ |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 3076 | pC->pData = sqliteMallocRaw( pC->nData+2 ); |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3077 | if( pC->pData ){ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3078 | memcpy(pC->pData, pTos->z, pC->nData); |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3079 | } |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 3080 | pC->pData[pC->nData] = 0; |
| 3081 | pC->pData[pC->nData+1] = 0; |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3082 | } |
| 3083 | pC->nullRow = 0; |
| 3084 | }else{ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3085 | rc = sqlite3BtreeInsert(pC->pCursor, zKey, nKey, pTos->z, pTos->n); |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3086 | } |
drh | 32fbe34 | 2002-10-19 20:16:37 +0000 | [diff] [blame] | 3087 | pC->recnoIsValid = 0; |
drh | a11846b | 2004-01-07 18:52:56 +0000 | [diff] [blame] | 3088 | pC->deferredMoveto = 0; |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 3089 | pC->cacheValid = 0; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3090 | } |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3091 | popStack(&pTos, 2); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3092 | break; |
| 3093 | } |
| 3094 | |
drh | c8d30ac | 2002-04-12 10:08:59 +0000 | [diff] [blame] | 3095 | /* Opcode: Delete P1 P2 * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3096 | ** |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 3097 | ** Delete the record at which the P1 cursor is currently pointing. |
| 3098 | ** |
| 3099 | ** The cursor will be left pointing at either the next or the previous |
| 3100 | ** 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] | 3101 | ** the next Next instruction will be a no-op. Hence it is OK to delete |
| 3102 | ** a record from within an Next loop. |
drh | c8d30ac | 2002-04-12 10:08:59 +0000 | [diff] [blame] | 3103 | ** |
rdc | b0c374f | 2004-02-20 22:53:38 +0000 | [diff] [blame] | 3104 | ** If the OPFLAG_NCHANGE flag of P2 is set, then the row change count is |
| 3105 | ** incremented (otherwise not). If OPFLAG_CSCHANGE flag is set, |
| 3106 | ** then the current statement change count is incremented (otherwise not). |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3107 | ** |
| 3108 | ** If P1 is a pseudo-table, then this instruction is a no-op. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3109 | */ |
| 3110 | case OP_Delete: { |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3111 | int i = pOp->p1; |
drh | 32fbe34 | 2002-10-19 20:16:37 +0000 | [diff] [blame] | 3112 | Cursor *pC; |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3113 | assert( i>=0 && i<p->nCursor ); |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame] | 3114 | pC = p->apCsr[i]; |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3115 | if( pC->pCursor!=0 ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3116 | sqlite3VdbeCursorMoveto(pC); |
| 3117 | rc = sqlite3BtreeDelete(pC->pCursor); |
drh | 32fbe34 | 2002-10-19 20:16:37 +0000 | [diff] [blame] | 3118 | pC->nextRowidValid = 0; |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 3119 | pC->cacheValid = 0; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3120 | } |
rdc | b0c374f | 2004-02-20 22:53:38 +0000 | [diff] [blame] | 3121 | if( pOp->p2 & OPFLAG_NCHANGE ) db->nChange++; |
| 3122 | if( pOp->p2 & OPFLAG_CSCHANGE ) db->csChange++; |
| 3123 | break; |
| 3124 | } |
| 3125 | |
| 3126 | /* Opcode: SetCounts * * * |
| 3127 | ** |
| 3128 | ** Called at end of statement. Updates lsChange (last statement change count) |
| 3129 | ** and resets csChange (current statement change count) to 0. |
| 3130 | */ |
| 3131 | case OP_SetCounts: { |
| 3132 | db->lsChange=db->csChange; |
| 3133 | db->csChange=0; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3134 | break; |
| 3135 | } |
| 3136 | |
| 3137 | /* Opcode: KeyAsData P1 P2 * |
| 3138 | ** |
| 3139 | ** 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] | 3140 | ** off (if P2==0). In key-as-data mode, the OP_Column opcode pulls |
| 3141 | ** data off of the key rather than the data. This is used for |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3142 | ** processing compound selects. |
danielk1977 | 84ac9d0 | 2004-05-18 09:58:06 +0000 | [diff] [blame] | 3143 | ** |
| 3144 | ** This opcode also instructs the cursor that the keys used will be |
| 3145 | ** serialized in the record format usually used for table data, not |
| 3146 | ** the usual index key format. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3147 | */ |
| 3148 | case OP_KeyAsData: { |
| 3149 | int i = pOp->p1; |
drh | fec19aa | 2004-05-19 20:41:03 +0000 | [diff] [blame] | 3150 | Cursor *pC; |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3151 | assert( i>=0 && i<p->nCursor ); |
drh | fec19aa | 2004-05-19 20:41:03 +0000 | [diff] [blame] | 3152 | pC = p->apCsr[i]; |
| 3153 | pC->keyAsData = pOp->p2; |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 3154 | sqlite3BtreeSetCompare(pC->pCursor, sqlite3VdbeRowCompare, pC->pKeyInfo); |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3155 | break; |
| 3156 | } |
| 3157 | |
| 3158 | /* Opcode: RowData P1 * * |
| 3159 | ** |
| 3160 | ** Push onto the stack the complete row data for cursor P1. |
| 3161 | ** There is no interpretation of the data. It is just copied |
| 3162 | ** onto the stack exactly as it is found in the database file. |
| 3163 | ** |
| 3164 | ** If the cursor is not pointing to a valid row, a NULL is pushed |
| 3165 | ** onto the stack. |
| 3166 | */ |
drh | 143f3c4 | 2004-01-07 20:37:52 +0000 | [diff] [blame] | 3167 | /* Opcode: RowKey P1 * * |
| 3168 | ** |
| 3169 | ** Push onto the stack the complete row key for cursor P1. |
| 3170 | ** There is no interpretation of the key. It is just copied |
| 3171 | ** onto the stack exactly as it is found in the database file. |
| 3172 | ** |
| 3173 | ** If the cursor is not pointing to a valid row, a NULL is pushed |
| 3174 | ** onto the stack. |
| 3175 | */ |
| 3176 | case OP_RowKey: |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3177 | case OP_RowData: { |
| 3178 | int i = pOp->p1; |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3179 | Cursor *pC; |
| 3180 | int n; |
| 3181 | |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3182 | pTos++; |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3183 | assert( i>=0 && i<p->nCursor ); |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame] | 3184 | pC = p->apCsr[i]; |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3185 | if( pC->nullRow ){ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3186 | pTos->flags = MEM_Null; |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3187 | }else if( pC->pCursor!=0 ){ |
| 3188 | BtCursor *pCrsr = pC->pCursor; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3189 | sqlite3VdbeCursorMoveto(pC); |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3190 | if( pC->nullRow ){ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3191 | pTos->flags = MEM_Null; |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3192 | break; |
drh | 143f3c4 | 2004-01-07 20:37:52 +0000 | [diff] [blame] | 3193 | }else if( pC->keyAsData || pOp->opcode==OP_RowKey ){ |
danielk1977 | 6490beb | 2004-05-11 06:17:21 +0000 | [diff] [blame] | 3194 | i64 n64; |
| 3195 | assert( !pC->intKey ); |
| 3196 | sqlite3BtreeKeySize(pCrsr, &n64); |
| 3197 | n = n64; |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3198 | }else{ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3199 | sqlite3BtreeDataSize(pCrsr, &n); |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3200 | } |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3201 | pTos->n = n; |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3202 | if( n<=NBFS ){ |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 3203 | pTos->flags = MEM_Blob | MEM_Short; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3204 | pTos->z = pTos->zShort; |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3205 | }else{ |
| 3206 | char *z = sqliteMallocRaw( n ); |
| 3207 | if( z==0 ) goto no_mem; |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 3208 | pTos->flags = MEM_Blob | MEM_Dyn; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3209 | pTos->z = z; |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3210 | } |
drh | 143f3c4 | 2004-01-07 20:37:52 +0000 | [diff] [blame] | 3211 | if( pC->keyAsData || pOp->opcode==OP_RowKey ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3212 | sqlite3BtreeKey(pCrsr, 0, n, pTos->z); |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3213 | }else{ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3214 | sqlite3BtreeData(pCrsr, 0, n, pTos->z); |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3215 | } |
| 3216 | }else if( pC->pseudoTable ){ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3217 | pTos->n = pC->nData; |
| 3218 | pTos->z = pC->pData; |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 3219 | pTos->flags = MEM_Blob|MEM_Ephem; |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3220 | }else{ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3221 | pTos->flags = MEM_Null; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3222 | } |
| 3223 | break; |
| 3224 | } |
| 3225 | |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3226 | /* Opcode: Recno P1 * * |
| 3227 | ** |
| 3228 | ** Push onto the stack an integer which is the first 4 bytes of the |
| 3229 | ** the key to the current entry in a sequential scan of the database |
| 3230 | ** file P1. The sequential scan should have been started using the |
| 3231 | ** Next opcode. |
| 3232 | */ |
| 3233 | case OP_Recno: { |
| 3234 | int i = pOp->p1; |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3235 | Cursor *pC; |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 3236 | i64 v; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3237 | |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3238 | assert( i>=0 && i<p->nCursor ); |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame] | 3239 | pC = p->apCsr[i]; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3240 | sqlite3VdbeCursorMoveto(pC); |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3241 | pTos++; |
drh | a11846b | 2004-01-07 18:52:56 +0000 | [diff] [blame] | 3242 | if( pC->recnoIsValid ){ |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3243 | v = pC->lastRecno; |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3244 | }else if( pC->pseudoTable ){ |
| 3245 | v = keyToInt(pC->iKey); |
drh | d60ccc6 | 2003-06-24 10:39:46 +0000 | [diff] [blame] | 3246 | }else if( pC->nullRow || pC->pCursor==0 ){ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3247 | pTos->flags = MEM_Null; |
drh | d60ccc6 | 2003-06-24 10:39:46 +0000 | [diff] [blame] | 3248 | break; |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3249 | }else{ |
| 3250 | assert( pC->pCursor!=0 ); |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 3251 | sqlite3BtreeKeySize(pC->pCursor, (u64*)&v); |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3252 | v = keyToInt(v); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3253 | } |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3254 | pTos->i = v; |
| 3255 | pTos->flags = MEM_Int; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3256 | break; |
| 3257 | } |
| 3258 | |
| 3259 | /* Opcode: FullKey P1 * * |
| 3260 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 3261 | ** Extract the complete key from the record that cursor P1 is currently |
| 3262 | ** pointing to and push the key onto the stack as a string. |
| 3263 | ** |
| 3264 | ** Compare this opcode to Recno. The Recno opcode extracts the first |
| 3265 | ** 4 bytes of the key and pushes those bytes onto the stack as an |
| 3266 | ** integer. This instruction pushes the entire key as a string. |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3267 | ** |
| 3268 | ** This opcode may not be used on a pseudo-table. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3269 | */ |
| 3270 | case OP_FullKey: { |
| 3271 | int i = pOp->p1; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3272 | BtCursor *pCrsr; |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame] | 3273 | Cursor *pC; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3274 | |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame] | 3275 | assert( p->apCsr[i]->keyAsData ); |
| 3276 | assert( !p->apCsr[i]->pseudoTable ); |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3277 | assert( i>=0 && i<p->nCursor ); |
| 3278 | pTos++; |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame] | 3279 | if( (pCrsr = (pC = p->apCsr[i])->pCursor)!=0 ){ |
danielk1977 | 36a3c70 | 2004-05-11 06:55:14 +0000 | [diff] [blame] | 3280 | u64 amt; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3281 | char *z; |
| 3282 | |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame] | 3283 | sqlite3VdbeCursorMoveto(pC); |
| 3284 | assert( pC->intKey==0 ); |
danielk1977 | 36a3c70 | 2004-05-11 06:55:14 +0000 | [diff] [blame] | 3285 | sqlite3BtreeKeySize(pCrsr, &amt); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3286 | if( amt<=0 ){ |
| 3287 | rc = SQLITE_CORRUPT; |
| 3288 | goto abort_due_to_error; |
| 3289 | } |
drh | 9bbca4c | 2001-11-06 04:00:18 +0000 | [diff] [blame] | 3290 | if( amt>NBFS ){ |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 3291 | z = sqliteMallocRaw( amt ); |
drh | caec2f1 | 2003-01-07 02:47:47 +0000 | [diff] [blame] | 3292 | if( z==0 ) goto no_mem; |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 3293 | pTos->flags = MEM_Blob | MEM_Dyn; |
drh | 9bbca4c | 2001-11-06 04:00:18 +0000 | [diff] [blame] | 3294 | }else{ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3295 | z = pTos->zShort; |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 3296 | pTos->flags = MEM_Blob | MEM_Short; |
drh | 9bbca4c | 2001-11-06 04:00:18 +0000 | [diff] [blame] | 3297 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3298 | sqlite3BtreeKey(pCrsr, 0, amt, z); |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3299 | pTos->z = z; |
| 3300 | pTos->n = amt; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3301 | } |
| 3302 | break; |
| 3303 | } |
| 3304 | |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 3305 | /* Opcode: NullRow P1 * * |
| 3306 | ** |
| 3307 | ** Move the cursor P1 to a null row. Any OP_Column operations |
| 3308 | ** that occur while the cursor is on the null row will always push |
| 3309 | ** a NULL onto the stack. |
| 3310 | */ |
| 3311 | case OP_NullRow: { |
| 3312 | int i = pOp->p1; |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame] | 3313 | Cursor *pC; |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 3314 | |
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 | pC = p->apCsr[i]; |
| 3317 | pC->nullRow = 1; |
| 3318 | pC->recnoIsValid = 0; |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 3319 | break; |
| 3320 | } |
| 3321 | |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 3322 | /* Opcode: Last P1 P2 * |
| 3323 | ** |
| 3324 | ** The next use of the Recno or Column or Next instruction for P1 |
| 3325 | ** will refer to the last entry in the database table or index. |
| 3326 | ** If the table or index is empty and P2>0, then jump immediately to P2. |
| 3327 | ** If P2 is 0 or if the table or index is not empty, fall through |
| 3328 | ** to the following instruction. |
| 3329 | */ |
| 3330 | case OP_Last: { |
| 3331 | int i = pOp->p1; |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3332 | Cursor *pC; |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 3333 | BtCursor *pCrsr; |
| 3334 | |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3335 | assert( i>=0 && i<p->nCursor ); |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame] | 3336 | pC = p->apCsr[i]; |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3337 | if( (pCrsr = pC->pCursor)!=0 ){ |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 3338 | int res; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3339 | rc = sqlite3BtreeLast(pCrsr, &res); |
drh | a11846b | 2004-01-07 18:52:56 +0000 | [diff] [blame] | 3340 | pC->nullRow = res; |
| 3341 | pC->deferredMoveto = 0; |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 3342 | pC->cacheValid = 0; |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 3343 | if( res && pOp->p2>0 ){ |
| 3344 | pc = pOp->p2 - 1; |
| 3345 | } |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3346 | }else{ |
| 3347 | pC->nullRow = 0; |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 3348 | } |
| 3349 | break; |
| 3350 | } |
| 3351 | |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 3352 | /* Opcode: Rewind P1 P2 * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3353 | ** |
| 3354 | ** The next use of the Recno or Column or Next instruction for P1 |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 3355 | ** will refer to the first entry in the database table or index. |
| 3356 | ** If the table or index is empty and P2>0, then jump immediately to P2. |
| 3357 | ** If P2 is 0 or if the table or index is not empty, fall through |
| 3358 | ** to the following instruction. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3359 | */ |
| 3360 | case OP_Rewind: { |
| 3361 | int i = pOp->p1; |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3362 | Cursor *pC; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3363 | BtCursor *pCrsr; |
drh | f4dada7 | 2004-05-11 09:57:35 +0000 | [diff] [blame] | 3364 | int res; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3365 | |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3366 | assert( i>=0 && i<p->nCursor ); |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame] | 3367 | pC = p->apCsr[i]; |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3368 | if( (pCrsr = pC->pCursor)!=0 ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3369 | rc = sqlite3BtreeFirst(pCrsr, &res); |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3370 | pC->atFirst = res==0; |
drh | a11846b | 2004-01-07 18:52:56 +0000 | [diff] [blame] | 3371 | pC->deferredMoveto = 0; |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 3372 | pC->cacheValid = 0; |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3373 | }else{ |
drh | f4dada7 | 2004-05-11 09:57:35 +0000 | [diff] [blame] | 3374 | res = 1; |
| 3375 | } |
| 3376 | pC->nullRow = res; |
| 3377 | if( res && pOp->p2>0 ){ |
| 3378 | pc = pOp->p2 - 1; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3379 | } |
| 3380 | break; |
| 3381 | } |
| 3382 | |
| 3383 | /* Opcode: Next P1 P2 * |
| 3384 | ** |
| 3385 | ** 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] | 3386 | ** table or index. If there are no more key/value pairs then fall through |
| 3387 | ** to the following instruction. But if the cursor advance was successful, |
| 3388 | ** jump immediately to P2. |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 3389 | ** |
| 3390 | ** See also: Prev |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 3391 | */ |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 3392 | /* Opcode: Prev P1 P2 * |
| 3393 | ** |
| 3394 | ** Back up cursor P1 so that it points to the previous key/data pair in its |
| 3395 | ** table or index. If there is no previous key/value pairs then fall through |
| 3396 | ** to the following instruction. But if the cursor backup was successful, |
| 3397 | ** jump immediately to P2. |
| 3398 | */ |
| 3399 | case OP_Prev: |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 3400 | case OP_Next: { |
drh | 7b39686 | 2003-01-01 23:06:20 +0000 | [diff] [blame] | 3401 | Cursor *pC; |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 3402 | BtCursor *pCrsr; |
| 3403 | |
drh | caec2f1 | 2003-01-07 02:47:47 +0000 | [diff] [blame] | 3404 | CHECK_FOR_INTERRUPT; |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3405 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame] | 3406 | pC = p->apCsr[pOp->p1]; |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3407 | if( (pCrsr = pC->pCursor)!=0 ){ |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 3408 | int res; |
drh | 7b39686 | 2003-01-01 23:06:20 +0000 | [diff] [blame] | 3409 | if( pC->nullRow ){ |
drh | ad2d830 | 2002-05-24 20:31:36 +0000 | [diff] [blame] | 3410 | res = 1; |
| 3411 | }else{ |
drh | a11846b | 2004-01-07 18:52:56 +0000 | [diff] [blame] | 3412 | assert( pC->deferredMoveto==0 ); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3413 | rc = pOp->opcode==OP_Next ? sqlite3BtreeNext(pCrsr, &res) : |
| 3414 | sqlite3BtreePrevious(pCrsr, &res); |
drh | 7b39686 | 2003-01-01 23:06:20 +0000 | [diff] [blame] | 3415 | pC->nullRow = res; |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 3416 | pC->cacheValid = 0; |
drh | ad2d830 | 2002-05-24 20:31:36 +0000 | [diff] [blame] | 3417 | } |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 3418 | if( res==0 ){ |
| 3419 | pc = pOp->p2 - 1; |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 3420 | sqlite3_search_count++; |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 3421 | } |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3422 | }else{ |
| 3423 | pC->nullRow = 1; |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 3424 | } |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3425 | pC->recnoIsValid = 0; |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 3426 | break; |
| 3427 | } |
| 3428 | |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 3429 | /* Opcode: IdxPut P1 P2 P3 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3430 | ** |
jplyon | 5a56422 | 2003-06-02 06:15:58 +0000 | [diff] [blame] | 3431 | ** The top of the stack holds a SQL index key made using the |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3432 | ** MakeIdxKey instruction. This opcode writes that key into the |
| 3433 | ** index P1. Data for the entry is nil. |
drh | 717e640 | 2001-09-27 03:22:32 +0000 | [diff] [blame] | 3434 | ** |
| 3435 | ** If P2==1, then the key must be unique. If the key is not unique, |
| 3436 | ** the program aborts with a SQLITE_CONSTRAINT error and the database |
jplyon | 5a56422 | 2003-06-02 06:15:58 +0000 | [diff] [blame] | 3437 | ** 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] | 3438 | ** error message returned with the SQLITE_CONSTRAINT. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3439 | */ |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 3440 | case OP_IdxPut: { |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3441 | int i = pOp->p1; |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 3442 | Cursor *pC; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3443 | BtCursor *pCrsr; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3444 | assert( pTos>=p->aStack ); |
| 3445 | assert( i>=0 && i<p->nCursor ); |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 3446 | assert( pTos->flags & MEM_Blob ); |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame] | 3447 | if( (pCrsr = (pC = p->apCsr[i])->pCursor)!=0 ){ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3448 | int nKey = pTos->n; |
| 3449 | const char *zKey = pTos->z; |
drh | 717e640 | 2001-09-27 03:22:32 +0000 | [diff] [blame] | 3450 | if( pOp->p2 ){ |
danielk1977 | 36a3c70 | 2004-05-11 06:55:14 +0000 | [diff] [blame] | 3451 | int res; |
danielk1977 | 452c989 | 2004-05-13 05:16:15 +0000 | [diff] [blame] | 3452 | int len; |
danielk1977 | 452c989 | 2004-05-13 05:16:15 +0000 | [diff] [blame] | 3453 | |
| 3454 | /* 'len' is the length of the key minus the rowid at the end */ |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 3455 | len = nKey - sqlite3VdbeIdxRowidLen(nKey, zKey); |
danielk1977 | 452c989 | 2004-05-13 05:16:15 +0000 | [diff] [blame] | 3456 | |
| 3457 | rc = sqlite3BtreeMoveto(pCrsr, zKey, len, &res); |
drh | 717e640 | 2001-09-27 03:22:32 +0000 | [diff] [blame] | 3458 | if( rc!=SQLITE_OK ) goto abort_due_to_error; |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 3459 | while( res!=0 && !sqlite3BtreeEof(pCrsr) ){ |
drh | 717e640 | 2001-09-27 03:22:32 +0000 | [diff] [blame] | 3460 | int c; |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 3461 | if( sqlite3VdbeIdxKeyCompare(pC, len, zKey, &c)==SQLITE_OK && c==0 ){ |
drh | 717e640 | 2001-09-27 03:22:32 +0000 | [diff] [blame] | 3462 | rc = SQLITE_CONSTRAINT; |
| 3463 | if( pOp->p3 && pOp->p3[0] ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3464 | sqlite3SetString(&p->zErrMsg, pOp->p3, (char*)0); |
drh | 717e640 | 2001-09-27 03:22:32 +0000 | [diff] [blame] | 3465 | } |
| 3466 | goto abort_due_to_error; |
| 3467 | } |
| 3468 | if( res<0 ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3469 | sqlite3BtreeNext(pCrsr, &res); |
drh | 717e640 | 2001-09-27 03:22:32 +0000 | [diff] [blame] | 3470 | res = +1; |
| 3471 | }else{ |
| 3472 | break; |
| 3473 | } |
| 3474 | } |
| 3475 | } |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 3476 | assert( pC->intKey==0 ); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3477 | rc = sqlite3BtreeInsert(pCrsr, zKey, nKey, "", 0); |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 3478 | assert( pC->deferredMoveto==0 ); |
| 3479 | pC->cacheValid = 0; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3480 | } |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3481 | Release(pTos); |
| 3482 | pTos--; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3483 | break; |
| 3484 | } |
| 3485 | |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 3486 | /* Opcode: IdxDelete P1 * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3487 | ** |
| 3488 | ** The top of the stack is an index key built using the MakeIdxKey opcode. |
| 3489 | ** This opcode removes that entry from the index. |
| 3490 | */ |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 3491 | case OP_IdxDelete: { |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3492 | int i = pOp->p1; |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 3493 | Cursor *pC; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3494 | BtCursor *pCrsr; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3495 | assert( pTos>=p->aStack ); |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 3496 | assert( pTos->flags & MEM_Blob ); |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3497 | assert( i>=0 && i<p->nCursor ); |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame] | 3498 | if( (pCrsr = (pC = p->apCsr[i])->pCursor)!=0 ){ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3499 | int rx, res; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3500 | rx = sqlite3BtreeMoveto(pCrsr, pTos->z, pTos->n, &res); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3501 | if( rx==SQLITE_OK && res==0 ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3502 | rc = sqlite3BtreeDelete(pCrsr); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3503 | } |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 3504 | assert( pC->deferredMoveto==0 ); |
| 3505 | pC->cacheValid = 0; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3506 | } |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3507 | Release(pTos); |
| 3508 | pTos--; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3509 | break; |
| 3510 | } |
| 3511 | |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 3512 | /* Opcode: IdxRecno P1 * * |
| 3513 | ** |
danielk1977 | 452c989 | 2004-05-13 05:16:15 +0000 | [diff] [blame] | 3514 | ** Push onto the stack an integer which is the varint located at the |
| 3515 | ** end of the index key pointed to by cursor P1. These integer should be |
| 3516 | ** the record number of the table entry to which this index entry points. |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 3517 | ** |
| 3518 | ** See also: Recno, MakeIdxKey. |
| 3519 | */ |
| 3520 | case OP_IdxRecno: { |
| 3521 | int i = pOp->p1; |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 3522 | BtCursor *pCrsr; |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame] | 3523 | Cursor *pC; |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 3524 | |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3525 | assert( i>=0 && i<p->nCursor ); |
| 3526 | pTos++; |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame] | 3527 | if( (pCrsr = (pC = p->apCsr[i])->pCursor)!=0 ){ |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 3528 | i64 rowid; |
danielk1977 | 452c989 | 2004-05-13 05:16:15 +0000 | [diff] [blame] | 3529 | |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame] | 3530 | assert( pC->deferredMoveto==0 ); |
| 3531 | assert( pC->intKey==0 ); |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 3532 | rc = sqlite3VdbeIdxRowid(pCrsr, &rowid); |
| 3533 | if( rc!=SQLITE_OK ){ |
| 3534 | goto abort_due_to_error; |
| 3535 | } |
| 3536 | pTos->flags = MEM_Int; |
| 3537 | pTos->i = rowid; |
danielk1977 | 452c989 | 2004-05-13 05:16:15 +0000 | [diff] [blame] | 3538 | |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 3539 | #if 0 |
danielk1977 | 452c989 | 2004-05-13 05:16:15 +0000 | [diff] [blame] | 3540 | /* Read the final 9 bytes of the key into buf[]. If the whole key is |
| 3541 | ** less than 9 bytes then just load the whole thing. Set len to the |
| 3542 | ** number of bytes read. |
| 3543 | */ |
danielk1977 | 6490beb | 2004-05-11 06:17:21 +0000 | [diff] [blame] | 3544 | sqlite3BtreeKeySize(pCrsr, &sz); |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 3545 | len = ((sz>10)?10:sz); |
danielk1977 | 452c989 | 2004-05-13 05:16:15 +0000 | [diff] [blame] | 3546 | rc = sqlite3BtreeKey(pCrsr, sz-len, len, buf); |
| 3547 | if( rc!=SQLITE_OK ){ |
| 3548 | goto abort_due_to_error; |
| 3549 | } |
| 3550 | |
| 3551 | len--; |
| 3552 | if( buf[len]&0x80 ){ |
| 3553 | /* If the last byte read has the 0x80 bit set, then the key does |
| 3554 | ** not end with a varint. Push a NULL onto the stack instead. |
| 3555 | */ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3556 | pTos->flags = MEM_Null; |
drh | d4d595f | 2003-04-17 12:44:23 +0000 | [diff] [blame] | 3557 | }else{ |
danielk1977 | 452c989 | 2004-05-13 05:16:15 +0000 | [diff] [blame] | 3558 | /* Find the start of the varint by searching backwards for a 0x00 |
| 3559 | ** byte. If one does not exists, then intepret the whole 9 bytes as a |
| 3560 | ** varint. |
| 3561 | */ |
| 3562 | while( len && buf[len-1] ){ |
| 3563 | len--; |
| 3564 | } |
drh | 25aa1b4 | 2004-05-28 01:39:01 +0000 | [diff] [blame] | 3565 | sqlite3GetVarint32(&buf[len], &sz); |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3566 | pTos->flags = MEM_Int; |
danielk1977 | 452c989 | 2004-05-13 05:16:15 +0000 | [diff] [blame] | 3567 | pTos->i = sz; |
drh | d4d595f | 2003-04-17 12:44:23 +0000 | [diff] [blame] | 3568 | } |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 3569 | #endif |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3570 | }else{ |
| 3571 | pTos->flags = MEM_Null; |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 3572 | } |
| 3573 | break; |
| 3574 | } |
| 3575 | |
drh | 7cf6e4d | 2004-05-19 14:56:55 +0000 | [diff] [blame] | 3576 | /* Opcode: IdxGT P1 P2 * |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 3577 | ** |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 3578 | ** The top of the stack is an index entry that omits the ROWID. Compare |
| 3579 | ** the top of stack against the index that P1 is currently pointing to. |
| 3580 | ** Ignore the ROWID on the P1 index. |
| 3581 | ** |
| 3582 | ** The top of the stack might have fewer columns that P1. |
| 3583 | ** |
| 3584 | ** If the P1 index entry is greater than the top of the stack |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 3585 | ** then jump to P2. Otherwise fall through to the next instruction. |
| 3586 | ** In either case, the stack is popped once. |
| 3587 | */ |
drh | 7cf6e4d | 2004-05-19 14:56:55 +0000 | [diff] [blame] | 3588 | /* Opcode: IdxGE P1 P2 P3 |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 3589 | ** |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 3590 | ** The top of the stack is an index entry that omits the ROWID. Compare |
| 3591 | ** the top of stack against the index that P1 is currently pointing to. |
| 3592 | ** Ignore the ROWID on the P1 index. |
| 3593 | ** |
| 3594 | ** If the P1 index entry is greater than or equal to the top of the stack |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 3595 | ** then jump to P2. Otherwise fall through to the next instruction. |
| 3596 | ** In either case, the stack is popped once. |
drh | 772ae62 | 2004-05-19 13:13:08 +0000 | [diff] [blame] | 3597 | ** |
| 3598 | ** If P3 is the "+" string (or any other non-NULL string) then the |
| 3599 | ** index taken from the top of the stack is temporarily increased by |
drh | 7cf6e4d | 2004-05-19 14:56:55 +0000 | [diff] [blame] | 3600 | ** an epsilon prior to the comparison. This make the opcode work |
| 3601 | ** like IdxGT except that if the key from the stack is a prefix of |
| 3602 | ** the key in the cursor, the result is false whereas it would be |
| 3603 | ** true with IdxGT. |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 3604 | */ |
drh | 7cf6e4d | 2004-05-19 14:56:55 +0000 | [diff] [blame] | 3605 | /* Opcode: IdxLT P1 P2 P3 |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 3606 | ** |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 3607 | ** The top of the stack is an index entry that omits the ROWID. Compare |
| 3608 | ** the top of stack against the index that P1 is currently pointing to. |
| 3609 | ** Ignore the ROWID on the P1 index. |
| 3610 | ** |
| 3611 | ** If the P1 index entry is less than the top of the stack |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 3612 | ** then jump to P2. Otherwise fall through to the next instruction. |
| 3613 | ** In either case, the stack is popped once. |
drh | 772ae62 | 2004-05-19 13:13:08 +0000 | [diff] [blame] | 3614 | ** |
| 3615 | ** If P3 is the "+" string (or any other non-NULL string) then the |
| 3616 | ** index taken from the top of the stack is temporarily increased by |
drh | 7cf6e4d | 2004-05-19 14:56:55 +0000 | [diff] [blame] | 3617 | ** an epsilon prior to the comparison. This makes the opcode work |
| 3618 | ** like IdxLE. |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 3619 | */ |
| 3620 | case OP_IdxLT: |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 3621 | case OP_IdxGT: |
| 3622 | case OP_IdxGE: { |
| 3623 | int i= pOp->p1; |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 3624 | BtCursor *pCrsr; |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame] | 3625 | Cursor *pC; |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 3626 | |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3627 | assert( i>=0 && i<p->nCursor ); |
| 3628 | assert( pTos>=p->aStack ); |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame] | 3629 | if( (pCrsr = (pC = p->apCsr[i])->pCursor)!=0 ){ |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 3630 | int res, rc; |
| 3631 | |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 3632 | assert( pTos->flags & MEM_Blob ); /* Created using OP_Make*Key */ |
danielk1977 | 8a6b541 | 2004-05-24 07:04:25 +0000 | [diff] [blame] | 3633 | Stringify(pTos, db->enc); |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame] | 3634 | assert( pC->deferredMoveto==0 ); |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 3635 | *pC->pIncrKey = pOp->p3!=0; |
drh | 7cf6e4d | 2004-05-19 14:56:55 +0000 | [diff] [blame] | 3636 | assert( pOp->p3==0 || pOp->opcode!=OP_IdxGT ); |
| 3637 | rc = sqlite3VdbeIdxKeyCompare(pC, pTos->n, pTos->z, &res); |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 3638 | *pC->pIncrKey = 0; |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 3639 | if( rc!=SQLITE_OK ){ |
| 3640 | break; |
| 3641 | } |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 3642 | if( pOp->opcode==OP_IdxLT ){ |
| 3643 | res = -res; |
| 3644 | }else if( pOp->opcode==OP_IdxGE ){ |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 3645 | res++; |
| 3646 | } |
| 3647 | if( res>0 ){ |
| 3648 | pc = pOp->p2 - 1 ; |
| 3649 | } |
| 3650 | } |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3651 | Release(pTos); |
| 3652 | pTos--; |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 3653 | break; |
| 3654 | } |
| 3655 | |
drh | 143f3c4 | 2004-01-07 20:37:52 +0000 | [diff] [blame] | 3656 | /* Opcode: IdxIsNull P1 P2 * |
| 3657 | ** |
| 3658 | ** The top of the stack contains an index entry such as might be generated |
| 3659 | ** by the MakeIdxKey opcode. This routine looks at the first P1 fields of |
| 3660 | ** that key. If any of the first P1 fields are NULL, then a jump is made |
| 3661 | ** to address P2. Otherwise we fall straight through. |
| 3662 | ** |
| 3663 | ** The index entry is always popped from the stack. |
| 3664 | */ |
| 3665 | case OP_IdxIsNull: { |
| 3666 | int i = pOp->p1; |
drh | 143f3c4 | 2004-01-07 20:37:52 +0000 | [diff] [blame] | 3667 | int k, n; |
| 3668 | const char *z; |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 3669 | u32 serial_type; |
drh | 143f3c4 | 2004-01-07 20:37:52 +0000 | [diff] [blame] | 3670 | |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3671 | assert( pTos>=p->aStack ); |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 3672 | assert( pTos->flags & MEM_Blob ); |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3673 | z = pTos->z; |
| 3674 | n = pTos->n; |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 3675 | k = sqlite3GetVarint32(z, &serial_type); |
| 3676 | for(; k<n && i>0; i--){ |
drh | 25aa1b4 | 2004-05-28 01:39:01 +0000 | [diff] [blame] | 3677 | k += sqlite3GetVarint32(&z[k], &serial_type); |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 3678 | if( serial_type==6 ){ /* Serial type 6 is a NULL */ |
drh | 143f3c4 | 2004-01-07 20:37:52 +0000 | [diff] [blame] | 3679 | pc = pOp->p2-1; |
| 3680 | break; |
| 3681 | } |
drh | 143f3c4 | 2004-01-07 20:37:52 +0000 | [diff] [blame] | 3682 | } |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3683 | Release(pTos); |
| 3684 | pTos--; |
drh | 143f3c4 | 2004-01-07 20:37:52 +0000 | [diff] [blame] | 3685 | break; |
| 3686 | } |
| 3687 | |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 3688 | /* Opcode: Destroy P1 P2 * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3689 | ** |
| 3690 | ** Delete an entire database table or index whose root page in the database |
| 3691 | ** file is given by P1. |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 3692 | ** |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 3693 | ** The table being destroyed is in the main database file if P2==0. If |
| 3694 | ** P2==1 then the table to be clear is in the auxiliary database file |
| 3695 | ** that is used to store tables create using CREATE TEMPORARY TABLE. |
| 3696 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 3697 | ** See also: Clear |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3698 | */ |
| 3699 | case OP_Destroy: { |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3700 | rc = sqlite3BtreeDropTable(db->aDb[pOp->p2].pBt, pOp->p1); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3701 | break; |
| 3702 | } |
| 3703 | |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 3704 | /* Opcode: Clear P1 P2 * |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 3705 | ** |
| 3706 | ** Delete all contents of the database table or index whose root page |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 3707 | ** in the database file is given by P1. But, unlike Destroy, do not |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 3708 | ** remove the table or index from the database file. |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 3709 | ** |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 3710 | ** The table being clear is in the main database file if P2==0. If |
| 3711 | ** P2==1 then the table to be clear is in the auxiliary database file |
| 3712 | ** that is used to store tables create using CREATE TEMPORARY TABLE. |
| 3713 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 3714 | ** See also: Destroy |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 3715 | */ |
| 3716 | case OP_Clear: { |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3717 | rc = sqlite3BtreeClearTable(db->aDb[pOp->p2].pBt, pOp->p1); |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 3718 | break; |
| 3719 | } |
| 3720 | |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 3721 | /* Opcode: CreateTable * P2 P3 |
drh | 5b2fd56 | 2001-09-13 15:21:31 +0000 | [diff] [blame] | 3722 | ** |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 3723 | ** Allocate a new table in the main database file if P2==0 or in the |
| 3724 | ** auxiliary database file if P2==1. Push the page number |
drh | 5b2fd56 | 2001-09-13 15:21:31 +0000 | [diff] [blame] | 3725 | ** for the root page of the new table onto the stack. |
| 3726 | ** |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 3727 | ** The root page number is also written to a memory location that P3 |
| 3728 | ** points to. This is the mechanism is used to write the root page |
| 3729 | ** number into the parser's internal data structures that describe the |
| 3730 | ** new table. |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 3731 | ** |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 3732 | ** The difference between a table and an index is this: A table must |
| 3733 | ** have a 4-byte integer key and can have arbitrary data. An index |
| 3734 | ** has an arbitrary key but no data. |
| 3735 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 3736 | ** See also: CreateIndex |
drh | 5b2fd56 | 2001-09-13 15:21:31 +0000 | [diff] [blame] | 3737 | */ |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 3738 | /* Opcode: CreateIndex * P2 P3 |
| 3739 | ** |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 3740 | ** Allocate a new index in the main database file if P2==0 or in the |
| 3741 | ** auxiliary database file if P2==1. Push the page number of the |
| 3742 | ** root page of the new index onto the stack. |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 3743 | ** |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 3744 | ** See documentation on OP_CreateTable for additional information. |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 3745 | */ |
| 3746 | case OP_CreateIndex: |
drh | 5b2fd56 | 2001-09-13 15:21:31 +0000 | [diff] [blame] | 3747 | case OP_CreateTable: { |
drh | 5b2fd56 | 2001-09-13 15:21:31 +0000 | [diff] [blame] | 3748 | int pgno; |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 3749 | int flags; |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 3750 | assert( pOp->p3!=0 && pOp->p3type==P3_POINTER ); |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 3751 | assert( pOp->p2>=0 && pOp->p2<db->nDb ); |
| 3752 | assert( db->aDb[pOp->p2].pBt!=0 ); |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 3753 | if( pOp->opcode==OP_CreateTable ){ |
danielk1977 | 9407625 | 2004-05-14 12:16:11 +0000 | [diff] [blame] | 3754 | /* flags = BTREE_INTKEY; */ |
| 3755 | flags = BTREE_LEAFDATA|BTREE_INTKEY; |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 3756 | }else{ |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 3757 | flags = BTREE_ZERODATA; |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 3758 | } |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 3759 | rc = sqlite3BtreeCreateTable(db->aDb[pOp->p2].pBt, &pgno, flags); |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3760 | pTos++; |
drh | 5b2fd56 | 2001-09-13 15:21:31 +0000 | [diff] [blame] | 3761 | if( rc==SQLITE_OK ){ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3762 | pTos->i = pgno; |
| 3763 | pTos->flags = MEM_Int; |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 3764 | *(u32*)pOp->p3 = pgno; |
| 3765 | pOp->p3 = 0; |
drh | f1b07b0 | 2004-02-08 06:17:19 +0000 | [diff] [blame] | 3766 | }else{ |
| 3767 | pTos->flags = MEM_Null; |
drh | 5b2fd56 | 2001-09-13 15:21:31 +0000 | [diff] [blame] | 3768 | } |
| 3769 | break; |
| 3770 | } |
| 3771 | |
drh | 7906975 | 2004-05-22 21:30:40 +0000 | [diff] [blame] | 3772 | /* Opcode: IntegrityCk * P2 * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3773 | ** |
drh | 1dd397f | 2002-02-03 03:34:07 +0000 | [diff] [blame] | 3774 | ** Do an analysis of the currently open database. Push onto the |
| 3775 | ** stack the text of an error message describing any problems. |
| 3776 | ** If there are no errors, push a "ok" onto the stack. |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 3777 | ** |
drh | 7906975 | 2004-05-22 21:30:40 +0000 | [diff] [blame] | 3778 | ** The root page numbers of all tables in the database are integer |
| 3779 | ** values on the stack. This opcode pulls as many integers as it |
| 3780 | ** can off of the stack and uses those numbers as the root pages. |
drh | 2150432 | 2002-06-25 13:16:02 +0000 | [diff] [blame] | 3781 | ** |
| 3782 | ** If P2 is not zero, the check is done on the auxiliary database |
| 3783 | ** file, not the main database file. |
drh | 1dd397f | 2002-02-03 03:34:07 +0000 | [diff] [blame] | 3784 | ** |
| 3785 | ** This opcode is used for testing purposes only. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3786 | */ |
drh | aaab572 | 2002-02-19 13:39:21 +0000 | [diff] [blame] | 3787 | case OP_IntegrityCk: { |
drh | 1dd397f | 2002-02-03 03:34:07 +0000 | [diff] [blame] | 3788 | int nRoot; |
| 3789 | int *aRoot; |
drh | 1dd397f | 2002-02-03 03:34:07 +0000 | [diff] [blame] | 3790 | int j; |
drh | 1dd397f | 2002-02-03 03:34:07 +0000 | [diff] [blame] | 3791 | char *z; |
| 3792 | |
drh | 7906975 | 2004-05-22 21:30:40 +0000 | [diff] [blame] | 3793 | for(nRoot=0; &pTos[-nRoot]>=p->aStack; nRoot++){ |
| 3794 | if( (pTos[-nRoot].flags & MEM_Int)==0 ) break; |
| 3795 | } |
| 3796 | assert( nRoot>0 ); |
| 3797 | aRoot = sqliteMallocRaw( sizeof(int*)*(nRoot+1) ); |
drh | caec2f1 | 2003-01-07 02:47:47 +0000 | [diff] [blame] | 3798 | if( aRoot==0 ) goto no_mem; |
drh | 7906975 | 2004-05-22 21:30:40 +0000 | [diff] [blame] | 3799 | for(j=0; j<nRoot; j++){ |
| 3800 | Mem *pMem = &pTos[-j]; |
| 3801 | aRoot[j] = pMem->i; |
drh | 1dd397f | 2002-02-03 03:34:07 +0000 | [diff] [blame] | 3802 | } |
| 3803 | aRoot[j] = 0; |
drh | 7906975 | 2004-05-22 21:30:40 +0000 | [diff] [blame] | 3804 | popStack(&pTos, nRoot); |
| 3805 | pTos++; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3806 | z = sqlite3BtreeIntegrityCheck(db->aDb[pOp->p2].pBt, aRoot, nRoot); |
drh | 1dd397f | 2002-02-03 03:34:07 +0000 | [diff] [blame] | 3807 | if( z==0 || z[0]==0 ){ |
drh | 2150432 | 2002-06-25 13:16:02 +0000 | [diff] [blame] | 3808 | if( z ) sqliteFree(z); |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3809 | pTos->z = "ok"; |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 3810 | pTos->n = 2; |
| 3811 | pTos->flags = MEM_Str | MEM_Static | MEM_Term; |
drh | 1dd397f | 2002-02-03 03:34:07 +0000 | [diff] [blame] | 3812 | }else{ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3813 | pTos->z = z; |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 3814 | pTos->n = strlen(z); |
| 3815 | pTos->flags = MEM_Str | MEM_Dyn | MEM_Term; |
danielk1977 | 8a6b541 | 2004-05-24 07:04:25 +0000 | [diff] [blame] | 3816 | } |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 3817 | pTos->enc = TEXT_Utf8; |
| 3818 | sqlite3VdbeChangeEncoding(pTos, db->enc); |
drh | 24e97df | 2002-02-03 19:06:02 +0000 | [diff] [blame] | 3819 | sqliteFree(aRoot); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3820 | break; |
| 3821 | } |
| 3822 | |
drh | a8b38d2 | 2001-11-01 14:41:34 +0000 | [diff] [blame] | 3823 | /* Opcode: ListWrite * * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3824 | ** |
| 3825 | ** Write the integer on the top of the stack |
drh | a8b38d2 | 2001-11-01 14:41:34 +0000 | [diff] [blame] | 3826 | ** into the temporary storage list. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3827 | */ |
| 3828 | case OP_ListWrite: { |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3829 | Keylist *pKeylist; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3830 | assert( pTos>=p->aStack ); |
drh | a8b38d2 | 2001-11-01 14:41:34 +0000 | [diff] [blame] | 3831 | pKeylist = p->pList; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3832 | if( pKeylist==0 || pKeylist->nUsed>=pKeylist->nKey ){ |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 3833 | pKeylist = sqliteMallocRaw( sizeof(Keylist)+999*sizeof(pKeylist->aKey[0]) ); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3834 | if( pKeylist==0 ) goto no_mem; |
| 3835 | pKeylist->nKey = 1000; |
| 3836 | pKeylist->nRead = 0; |
| 3837 | pKeylist->nUsed = 0; |
drh | a8b38d2 | 2001-11-01 14:41:34 +0000 | [diff] [blame] | 3838 | pKeylist->pNext = p->pList; |
| 3839 | p->pList = pKeylist; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3840 | } |
danielk1977 | 8a6b541 | 2004-05-24 07:04:25 +0000 | [diff] [blame] | 3841 | Integerify(pTos, db->enc); |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3842 | pKeylist->aKey[pKeylist->nUsed++] = pTos->i; |
drh | 79f14b7 | 2004-03-03 01:51:24 +0000 | [diff] [blame] | 3843 | Release(pTos); |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3844 | pTos--; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3845 | break; |
| 3846 | } |
| 3847 | |
drh | a8b38d2 | 2001-11-01 14:41:34 +0000 | [diff] [blame] | 3848 | /* Opcode: ListRewind * * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3849 | ** |
drh | fb044c1 | 2004-02-10 13:41:52 +0000 | [diff] [blame] | 3850 | ** Rewind the temporary buffer back to the beginning. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3851 | */ |
| 3852 | case OP_ListRewind: { |
drh | fb044c1 | 2004-02-10 13:41:52 +0000 | [diff] [blame] | 3853 | /* What this opcode codes, really, is reverse the order of the |
| 3854 | ** linked list of Keylist structures so that they are read out |
| 3855 | ** in the same order that they were read in. */ |
| 3856 | Keylist *pRev, *pTop; |
| 3857 | pRev = 0; |
| 3858 | while( p->pList ){ |
| 3859 | pTop = p->pList; |
| 3860 | p->pList = pTop->pNext; |
| 3861 | pTop->pNext = pRev; |
| 3862 | pRev = pTop; |
| 3863 | } |
| 3864 | p->pList = pRev; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3865 | break; |
| 3866 | } |
| 3867 | |
drh | a8b38d2 | 2001-11-01 14:41:34 +0000 | [diff] [blame] | 3868 | /* Opcode: ListRead * P2 * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3869 | ** |
drh | a8b38d2 | 2001-11-01 14:41:34 +0000 | [diff] [blame] | 3870 | ** Attempt to read an integer from the temporary storage buffer |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3871 | ** and push it onto the stack. If the storage buffer is empty, |
| 3872 | ** push nothing but instead jump to P2. |
| 3873 | */ |
| 3874 | case OP_ListRead: { |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3875 | Keylist *pKeylist; |
drh | caec2f1 | 2003-01-07 02:47:47 +0000 | [diff] [blame] | 3876 | CHECK_FOR_INTERRUPT; |
drh | a8b38d2 | 2001-11-01 14:41:34 +0000 | [diff] [blame] | 3877 | pKeylist = p->pList; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3878 | if( pKeylist!=0 ){ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3879 | assert( pKeylist->nRead>=0 ); |
| 3880 | assert( pKeylist->nRead<pKeylist->nUsed ); |
| 3881 | assert( pKeylist->nRead<pKeylist->nKey ); |
| 3882 | pTos++; |
| 3883 | pTos->i = pKeylist->aKey[pKeylist->nRead++]; |
| 3884 | pTos->flags = MEM_Int; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3885 | if( pKeylist->nRead>=pKeylist->nUsed ){ |
drh | a8b38d2 | 2001-11-01 14:41:34 +0000 | [diff] [blame] | 3886 | p->pList = pKeylist->pNext; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3887 | sqliteFree(pKeylist); |
| 3888 | } |
| 3889 | }else{ |
| 3890 | pc = pOp->p2 - 1; |
| 3891 | } |
| 3892 | break; |
| 3893 | } |
| 3894 | |
drh | a8b38d2 | 2001-11-01 14:41:34 +0000 | [diff] [blame] | 3895 | /* Opcode: ListReset * * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3896 | ** |
drh | a8b38d2 | 2001-11-01 14:41:34 +0000 | [diff] [blame] | 3897 | ** Reset the temporary storage buffer so that it holds nothing. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3898 | */ |
drh | a8b38d2 | 2001-11-01 14:41:34 +0000 | [diff] [blame] | 3899 | case OP_ListReset: { |
| 3900 | if( p->pList ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3901 | sqlite3VdbeKeylistFree(p->pList); |
drh | a8b38d2 | 2001-11-01 14:41:34 +0000 | [diff] [blame] | 3902 | p->pList = 0; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3903 | } |
| 3904 | break; |
| 3905 | } |
| 3906 | |
drh | bd5a451 | 2002-05-23 22:07:02 +0000 | [diff] [blame] | 3907 | /* Opcode: ListPush * * * |
| 3908 | ** |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 3909 | ** 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] | 3910 | ** opcode. The list is empty after this is executed. |
| 3911 | */ |
| 3912 | case OP_ListPush: { |
| 3913 | p->keylistStackDepth++; |
| 3914 | assert(p->keylistStackDepth > 0); |
| 3915 | p->keylistStack = sqliteRealloc(p->keylistStack, |
| 3916 | sizeof(Keylist *) * p->keylistStackDepth); |
drh | caec2f1 | 2003-01-07 02:47:47 +0000 | [diff] [blame] | 3917 | if( p->keylistStack==0 ) goto no_mem; |
drh | bd5a451 | 2002-05-23 22:07:02 +0000 | [diff] [blame] | 3918 | p->keylistStack[p->keylistStackDepth - 1] = p->pList; |
| 3919 | p->pList = 0; |
| 3920 | break; |
| 3921 | } |
| 3922 | |
| 3923 | /* Opcode: ListPop * * * |
| 3924 | ** |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 3925 | ** 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] | 3926 | ** executed. |
| 3927 | */ |
| 3928 | case OP_ListPop: { |
| 3929 | assert(p->keylistStackDepth > 0); |
| 3930 | p->keylistStackDepth--; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3931 | sqlite3VdbeKeylistFree(p->pList); |
drh | bd5a451 | 2002-05-23 22:07:02 +0000 | [diff] [blame] | 3932 | p->pList = p->keylistStack[p->keylistStackDepth]; |
| 3933 | p->keylistStack[p->keylistStackDepth] = 0; |
| 3934 | if( p->keylistStackDepth == 0 ){ |
| 3935 | sqliteFree(p->keylistStack); |
| 3936 | p->keylistStack = 0; |
| 3937 | } |
| 3938 | break; |
| 3939 | } |
| 3940 | |
rdc | b0c374f | 2004-02-20 22:53:38 +0000 | [diff] [blame] | 3941 | /* Opcode: ContextPush * * * |
| 3942 | ** |
| 3943 | ** Save the current Vdbe context such that it can be restored by a ContextPop |
| 3944 | ** opcode. The context stores the last insert row id, the last statement change |
| 3945 | ** count, and the current statement change count. |
| 3946 | */ |
| 3947 | case OP_ContextPush: { |
| 3948 | p->contextStackDepth++; |
| 3949 | assert(p->contextStackDepth > 0); |
| 3950 | p->contextStack = sqliteRealloc(p->contextStack, |
| 3951 | sizeof(Context) * p->contextStackDepth); |
| 3952 | if( p->contextStack==0 ) goto no_mem; |
| 3953 | p->contextStack[p->contextStackDepth - 1].lastRowid = p->db->lastRowid; |
| 3954 | p->contextStack[p->contextStackDepth - 1].lsChange = p->db->lsChange; |
| 3955 | p->contextStack[p->contextStackDepth - 1].csChange = p->db->csChange; |
| 3956 | break; |
| 3957 | } |
| 3958 | |
| 3959 | /* Opcode: ContextPop * * * |
| 3960 | ** |
| 3961 | ** Restore the Vdbe context to the state it was in when contextPush was last |
| 3962 | ** executed. The context stores the last insert row id, the last statement |
| 3963 | ** change count, and the current statement change count. |
| 3964 | */ |
| 3965 | case OP_ContextPop: { |
| 3966 | assert(p->contextStackDepth > 0); |
| 3967 | p->contextStackDepth--; |
| 3968 | p->db->lastRowid = p->contextStack[p->contextStackDepth].lastRowid; |
| 3969 | p->db->lsChange = p->contextStack[p->contextStackDepth].lsChange; |
| 3970 | p->db->csChange = p->contextStack[p->contextStackDepth].csChange; |
| 3971 | if( p->contextStackDepth == 0 ){ |
| 3972 | sqliteFree(p->contextStack); |
| 3973 | p->contextStack = 0; |
| 3974 | } |
| 3975 | break; |
| 3976 | } |
| 3977 | |
drh | a8b38d2 | 2001-11-01 14:41:34 +0000 | [diff] [blame] | 3978 | /* Opcode: SortPut * * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3979 | ** |
| 3980 | ** 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] | 3981 | ** and put them on the sorter. The key and data should have been |
| 3982 | ** made using SortMakeKey and SortMakeRec, respectively. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3983 | */ |
| 3984 | case OP_SortPut: { |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3985 | Mem *pNos = &pTos[-1]; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3986 | Sorter *pSorter; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3987 | assert( pNos>=p->aStack ); |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 3988 | Stringify(pNos, db->enc); |
danielk1977 | 8a6b541 | 2004-05-24 07:04:25 +0000 | [diff] [blame] | 3989 | if( Dynamicify(pTos, db->enc) || Dynamicify(pNos, db->enc) ) goto no_mem; |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 3990 | pSorter = sqliteMallocRaw( sizeof(Sorter) ); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3991 | if( pSorter==0 ) goto no_mem; |
drh | a8b38d2 | 2001-11-01 14:41:34 +0000 | [diff] [blame] | 3992 | pSorter->pNext = p->pSort; |
| 3993 | p->pSort = pSorter; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3994 | assert( pTos->flags & MEM_Dyn ); |
| 3995 | pSorter->nKey = pTos->n; |
| 3996 | pSorter->zKey = pTos->z; |
| 3997 | assert( pNos->flags & MEM_Dyn ); |
| 3998 | pSorter->nData = pNos->n; |
| 3999 | pSorter->pData = pNos->z; |
| 4000 | pTos -= 2; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4001 | break; |
| 4002 | } |
| 4003 | |
drh | ffbc308 | 2004-05-21 01:29:06 +0000 | [diff] [blame] | 4004 | /* Opcode: Sort * * P3 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4005 | ** |
drh | a8b38d2 | 2001-11-01 14:41:34 +0000 | [diff] [blame] | 4006 | ** Sort all elements on the sorter. The algorithm is a |
drh | ffbc308 | 2004-05-21 01:29:06 +0000 | [diff] [blame] | 4007 | ** mergesort. The P3 argument is a pointer to a KeyInfo structure |
| 4008 | ** that describes the keys to be sorted. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4009 | */ |
| 4010 | case OP_Sort: { |
drh | a8b38d2 | 2001-11-01 14:41:34 +0000 | [diff] [blame] | 4011 | int i; |
drh | ffbc308 | 2004-05-21 01:29:06 +0000 | [diff] [blame] | 4012 | KeyInfo *pKeyInfo = (KeyInfo*)pOp->p3; |
drh | a8b38d2 | 2001-11-01 14:41:34 +0000 | [diff] [blame] | 4013 | Sorter *pElem; |
| 4014 | Sorter *apSorter[NSORT]; |
drh | 60ca804 | 2004-05-22 11:09:30 +0000 | [diff] [blame] | 4015 | pKeyInfo->enc = p->db->enc; |
drh | a8b38d2 | 2001-11-01 14:41:34 +0000 | [diff] [blame] | 4016 | for(i=0; i<NSORT; i++){ |
| 4017 | apSorter[i] = 0; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4018 | } |
drh | a8b38d2 | 2001-11-01 14:41:34 +0000 | [diff] [blame] | 4019 | while( p->pSort ){ |
| 4020 | pElem = p->pSort; |
| 4021 | p->pSort = pElem->pNext; |
| 4022 | pElem->pNext = 0; |
| 4023 | for(i=0; i<NSORT-1; i++){ |
| 4024 | if( apSorter[i]==0 ){ |
| 4025 | apSorter[i] = pElem; |
| 4026 | break; |
| 4027 | }else{ |
drh | ffbc308 | 2004-05-21 01:29:06 +0000 | [diff] [blame] | 4028 | pElem = Merge(apSorter[i], pElem, pKeyInfo); |
drh | a8b38d2 | 2001-11-01 14:41:34 +0000 | [diff] [blame] | 4029 | apSorter[i] = 0; |
| 4030 | } |
| 4031 | } |
| 4032 | if( i>=NSORT-1 ){ |
drh | ffbc308 | 2004-05-21 01:29:06 +0000 | [diff] [blame] | 4033 | apSorter[NSORT-1] = Merge(apSorter[NSORT-1],pElem, pKeyInfo); |
drh | a8b38d2 | 2001-11-01 14:41:34 +0000 | [diff] [blame] | 4034 | } |
| 4035 | } |
| 4036 | pElem = 0; |
| 4037 | for(i=0; i<NSORT; i++){ |
drh | ffbc308 | 2004-05-21 01:29:06 +0000 | [diff] [blame] | 4038 | pElem = Merge(apSorter[i], pElem, pKeyInfo); |
drh | a8b38d2 | 2001-11-01 14:41:34 +0000 | [diff] [blame] | 4039 | } |
| 4040 | p->pSort = pElem; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4041 | break; |
| 4042 | } |
| 4043 | |
drh | a8b38d2 | 2001-11-01 14:41:34 +0000 | [diff] [blame] | 4044 | /* Opcode: SortNext * P2 * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4045 | ** |
drh | a8b38d2 | 2001-11-01 14:41:34 +0000 | [diff] [blame] | 4046 | ** Push the data for the topmost element in the sorter onto the |
| 4047 | ** stack, then remove the element from the sorter. If the sorter |
| 4048 | ** is empty, push nothing on the stack and instead jump immediately |
| 4049 | ** to instruction P2. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4050 | */ |
| 4051 | case OP_SortNext: { |
drh | a8b38d2 | 2001-11-01 14:41:34 +0000 | [diff] [blame] | 4052 | Sorter *pSorter = p->pSort; |
drh | caec2f1 | 2003-01-07 02:47:47 +0000 | [diff] [blame] | 4053 | CHECK_FOR_INTERRUPT; |
drh | a8b38d2 | 2001-11-01 14:41:34 +0000 | [diff] [blame] | 4054 | if( pSorter!=0 ){ |
| 4055 | p->pSort = pSorter->pNext; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 4056 | pTos++; |
| 4057 | pTos->z = pSorter->pData; |
| 4058 | pTos->n = pSorter->nData; |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 4059 | pTos->flags = MEM_Blob|MEM_Dyn|MEM_Term; |
| 4060 | pTos->enc = 0; |
| 4061 | pTos->type = SQLITE3_BLOB; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4062 | sqliteFree(pSorter->zKey); |
| 4063 | sqliteFree(pSorter); |
| 4064 | }else{ |
| 4065 | pc = pOp->p2 - 1; |
| 4066 | } |
| 4067 | break; |
| 4068 | } |
| 4069 | |
drh | a8b38d2 | 2001-11-01 14:41:34 +0000 | [diff] [blame] | 4070 | /* Opcode: SortReset * * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4071 | ** |
drh | a8b38d2 | 2001-11-01 14:41:34 +0000 | [diff] [blame] | 4072 | ** Remove any elements that remain on the sorter. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4073 | */ |
drh | a8b38d2 | 2001-11-01 14:41:34 +0000 | [diff] [blame] | 4074 | case OP_SortReset: { |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 4075 | sqlite3VdbeSorterReset(p); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4076 | break; |
| 4077 | } |
| 4078 | |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 4079 | /* Opcode: MemStore P1 P2 * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4080 | ** |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 4081 | ** Write the top of the stack into memory location P1. |
| 4082 | ** P1 should be a small integer since space is allocated |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4083 | ** for all memory locations between 0 and P1 inclusive. |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 4084 | ** |
| 4085 | ** After the data is stored in the memory location, the |
| 4086 | ** stack is popped once if P2 is 1. If P2 is zero, then |
| 4087 | ** the original data remains on the stack. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4088 | */ |
| 4089 | case OP_MemStore: { |
| 4090 | int i = pOp->p1; |
drh | 9bbca4c | 2001-11-06 04:00:18 +0000 | [diff] [blame] | 4091 | Mem *pMem; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 4092 | assert( pTos>=p->aStack ); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4093 | if( i>=p->nMem ){ |
| 4094 | int nOld = p->nMem; |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 4095 | Mem *aMem; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4096 | p->nMem = i + 5; |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 4097 | aMem = sqliteRealloc(p->aMem, p->nMem*sizeof(p->aMem[0])); |
| 4098 | if( aMem==0 ) goto no_mem; |
drh | 3e56c04 | 2002-09-17 03:20:46 +0000 | [diff] [blame] | 4099 | if( aMem!=p->aMem ){ |
| 4100 | int j; |
| 4101 | for(j=0; j<nOld; j++){ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 4102 | if( aMem[j].flags & MEM_Short ){ |
drh | 00706be | 2004-01-30 14:49:16 +0000 | [diff] [blame] | 4103 | aMem[j].z = aMem[j].zShort; |
drh | 3e56c04 | 2002-09-17 03:20:46 +0000 | [diff] [blame] | 4104 | } |
| 4105 | } |
| 4106 | } |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 4107 | p->aMem = aMem; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4108 | if( nOld<p->nMem ){ |
| 4109 | memset(&p->aMem[nOld], 0, sizeof(p->aMem[0])*(p->nMem-nOld)); |
| 4110 | } |
| 4111 | } |
drh | 2c79c67 | 2004-01-31 20:20:29 +0000 | [diff] [blame] | 4112 | Deephemeralize(pTos); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4113 | pMem = &p->aMem[i]; |
drh | 2c79c67 | 2004-01-31 20:20:29 +0000 | [diff] [blame] | 4114 | Release(pMem); |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 4115 | *pMem = *pTos; |
drh | 2c79c67 | 2004-01-31 20:20:29 +0000 | [diff] [blame] | 4116 | if( pMem->flags & MEM_Dyn ){ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 4117 | if( pOp->p2 ){ |
| 4118 | pTos->flags = MEM_Null; |
| 4119 | }else{ |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 4120 | pMem->z = sqliteMallocRaw( pMem->n+2 ); |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 4121 | if( pMem->z==0 ) goto no_mem; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 4122 | memcpy(pMem->z, pTos->z, pMem->n); |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 4123 | memcpy(&pMem->z[pMem->n], "\000", 2); |
| 4124 | pMem->flags |= MEM_Term; |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 4125 | } |
drh | 2c79c67 | 2004-01-31 20:20:29 +0000 | [diff] [blame] | 4126 | }else if( pMem->flags & MEM_Short ){ |
drh | 00706be | 2004-01-30 14:49:16 +0000 | [diff] [blame] | 4127 | pMem->z = pMem->zShort; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4128 | } |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 4129 | if( pOp->p2 ){ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 4130 | Release(pTos); |
| 4131 | pTos--; |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 4132 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4133 | break; |
| 4134 | } |
| 4135 | |
| 4136 | /* Opcode: MemLoad P1 * * |
| 4137 | ** |
| 4138 | ** Push a copy of the value in memory location P1 onto the stack. |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 4139 | ** |
| 4140 | ** If the value is a string, then the value pushed is a pointer to |
| 4141 | ** the string that is stored in the memory location. If the memory |
| 4142 | ** location is subsequently changed (using OP_MemStore) then the |
| 4143 | ** value pushed onto the stack will change too. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4144 | */ |
| 4145 | case OP_MemLoad: { |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4146 | int i = pOp->p1; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 4147 | assert( i>=0 && i<p->nMem ); |
| 4148 | pTos++; |
| 4149 | memcpy(pTos, &p->aMem[i], sizeof(pTos[0])-NBFS);; |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 4150 | if( pTos->flags & (MEM_Str|MEM_Blob) ){ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 4151 | pTos->flags |= MEM_Ephem; |
| 4152 | pTos->flags &= ~(MEM_Dyn|MEM_Static|MEM_Short); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4153 | } |
| 4154 | break; |
| 4155 | } |
| 4156 | |
drh | d11d382 | 2002-06-21 23:01:49 +0000 | [diff] [blame] | 4157 | /* Opcode: MemIncr P1 P2 * |
| 4158 | ** |
| 4159 | ** Increment the integer valued memory cell P1 by 1. If P2 is not zero |
| 4160 | ** and the result after the increment is greater than zero, then jump |
| 4161 | ** to P2. |
| 4162 | ** |
| 4163 | ** This instruction throws an error if the memory cell is not initially |
| 4164 | ** an integer. |
| 4165 | */ |
| 4166 | case OP_MemIncr: { |
| 4167 | int i = pOp->p1; |
| 4168 | Mem *pMem; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 4169 | assert( i>=0 && i<p->nMem ); |
drh | d11d382 | 2002-06-21 23:01:49 +0000 | [diff] [blame] | 4170 | pMem = &p->aMem[i]; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 4171 | assert( pMem->flags==MEM_Int ); |
drh | 00706be | 2004-01-30 14:49:16 +0000 | [diff] [blame] | 4172 | pMem->i++; |
| 4173 | if( pOp->p2>0 && pMem->i>0 ){ |
drh | d11d382 | 2002-06-21 23:01:49 +0000 | [diff] [blame] | 4174 | pc = pOp->p2 - 1; |
| 4175 | } |
| 4176 | break; |
| 4177 | } |
| 4178 | |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4179 | /* Opcode: AggReset * P2 * |
| 4180 | ** |
| 4181 | ** Reset the aggregator so that it no longer contains any data. |
| 4182 | ** Future aggregator elements will contain P2 values each. |
| 4183 | */ |
| 4184 | case OP_AggReset: { |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 4185 | sqlite3VdbeAggReset(&p->agg); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4186 | p->agg.nMem = pOp->p2; |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 4187 | p->agg.apFunc = sqliteMalloc( p->agg.nMem*sizeof(p->agg.apFunc[0]) ); |
drh | caec2f1 | 2003-01-07 02:47:47 +0000 | [diff] [blame] | 4188 | if( p->agg.apFunc==0 ) goto no_mem; |
drh | e509535 | 2002-02-24 03:25:14 +0000 | [diff] [blame] | 4189 | break; |
| 4190 | } |
| 4191 | |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 4192 | /* Opcode: AggInit * P2 P3 |
drh | e509535 | 2002-02-24 03:25:14 +0000 | [diff] [blame] | 4193 | ** |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 4194 | ** Initialize the function parameters for an aggregate function. |
| 4195 | ** The aggregate will operate out of aggregate column P2. |
| 4196 | ** P3 is a pointer to the FuncDef structure for the function. |
drh | e509535 | 2002-02-24 03:25:14 +0000 | [diff] [blame] | 4197 | */ |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 4198 | case OP_AggInit: { |
drh | e509535 | 2002-02-24 03:25:14 +0000 | [diff] [blame] | 4199 | int i = pOp->p2; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 4200 | assert( i>=0 && i<p->agg.nMem ); |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 4201 | p->agg.apFunc[i] = (FuncDef*)pOp->p3; |
drh | e509535 | 2002-02-24 03:25:14 +0000 | [diff] [blame] | 4202 | break; |
| 4203 | } |
| 4204 | |
| 4205 | /* Opcode: AggFunc * P2 P3 |
| 4206 | ** |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 4207 | ** Execute the step function for an aggregate. The |
| 4208 | ** function has P2 arguments. P3 is a pointer to the FuncDef |
| 4209 | ** structure that specifies the function. |
drh | e509535 | 2002-02-24 03:25:14 +0000 | [diff] [blame] | 4210 | ** |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 4211 | ** The top of the stack must be an integer which is the index of |
| 4212 | ** the aggregate column that corresponds to this aggregate function. |
| 4213 | ** Ideally, this index would be another parameter, but there are |
| 4214 | ** no free parameters left. The integer is popped from the stack. |
drh | e509535 | 2002-02-24 03:25:14 +0000 | [diff] [blame] | 4215 | */ |
| 4216 | case OP_AggFunc: { |
| 4217 | int n = pOp->p2; |
| 4218 | int i; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 4219 | Mem *pMem, *pRec; |
danielk1977 | 22322fd | 2004-05-25 23:35:17 +0000 | [diff] [blame] | 4220 | sqlite3_context ctx; |
danielk1977 | 6ddcca5 | 2004-05-24 23:48:25 +0000 | [diff] [blame] | 4221 | sqlite3_value **apVal; |
drh | e509535 | 2002-02-24 03:25:14 +0000 | [diff] [blame] | 4222 | |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 4223 | assert( n>=0 ); |
| 4224 | assert( pTos->flags==MEM_Int ); |
| 4225 | pRec = &pTos[-n]; |
| 4226 | assert( pRec>=p->aStack ); |
danielk1977 | 6ddcca5 | 2004-05-24 23:48:25 +0000 | [diff] [blame] | 4227 | |
| 4228 | apVal = p->apArg; |
| 4229 | assert( apVal || n==0 ); |
| 4230 | |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 4231 | for(i=0; i<n; i++, pRec++){ |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 4232 | apVal[i] = pRec; |
| 4233 | StoreTypeInfo(pRec, db->enc); |
drh | e509535 | 2002-02-24 03:25:14 +0000 | [diff] [blame] | 4234 | } |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 4235 | i = pTos->i; |
| 4236 | assert( i>=0 && i<p->agg.nMem ); |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 4237 | ctx.pFunc = (FuncDef*)pOp->p3; |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 4238 | pMem = &p->agg.pCurrent->aMem[i]; |
drh | 00706be | 2004-01-30 14:49:16 +0000 | [diff] [blame] | 4239 | ctx.s.z = pMem->zShort; /* Space used for small aggregate contexts */ |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 4240 | ctx.pAgg = pMem->z; |
drh | 00706be | 2004-01-30 14:49:16 +0000 | [diff] [blame] | 4241 | ctx.cnt = ++pMem->i; |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 4242 | ctx.isError = 0; |
| 4243 | ctx.isStep = 1; |
danielk1977 | 6ddcca5 | 2004-05-24 23:48:25 +0000 | [diff] [blame] | 4244 | (ctx.pFunc->xStep)(&ctx, n, apVal); |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 4245 | pMem->z = ctx.pAgg; |
drh | 00706be | 2004-01-30 14:49:16 +0000 | [diff] [blame] | 4246 | pMem->flags = MEM_AggCtx; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 4247 | popStack(&pTos, n+1); |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 4248 | if( ctx.isError ){ |
| 4249 | rc = SQLITE_ERROR; |
| 4250 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4251 | break; |
| 4252 | } |
| 4253 | |
| 4254 | /* Opcode: AggFocus * P2 * |
| 4255 | ** |
| 4256 | ** Pop the top of the stack and use that as an aggregator key. If |
| 4257 | ** an aggregator with that same key already exists, then make the |
| 4258 | ** aggregator the current aggregator and jump to P2. If no aggregator |
| 4259 | ** with the given key exists, create one and make it current but |
| 4260 | ** do not jump. |
| 4261 | ** |
| 4262 | ** The order of aggregator opcodes is important. The order is: |
| 4263 | ** AggReset AggFocus AggNext. In other words, you must execute |
| 4264 | ** AggReset first, then zero or more AggFocus operations, then |
| 4265 | ** zero or more AggNext operations. You must not execute an AggFocus |
| 4266 | ** in between an AggNext and an AggReset. |
| 4267 | */ |
| 4268 | case OP_AggFocus: { |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4269 | AggElem *pElem; |
| 4270 | char *zKey; |
| 4271 | int nKey; |
| 4272 | |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 4273 | assert( pTos>=p->aStack ); |
danielk1977 | 8a6b541 | 2004-05-24 07:04:25 +0000 | [diff] [blame] | 4274 | Stringify(pTos, db->enc); |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 4275 | zKey = pTos->z; |
| 4276 | nKey = pTos->n; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 4277 | pElem = sqlite3HashFind(&p->agg.hash, zKey, nKey); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4278 | if( pElem ){ |
| 4279 | p->agg.pCurrent = pElem; |
| 4280 | pc = pOp->p2 - 1; |
| 4281 | }else{ |
drh | db5ed6d | 2001-09-18 22:17:44 +0000 | [diff] [blame] | 4282 | AggInsert(&p->agg, zKey, nKey); |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 4283 | if( sqlite3_malloc_failed ) goto no_mem; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4284 | } |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 4285 | Release(pTos); |
| 4286 | pTos--; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4287 | break; |
| 4288 | } |
| 4289 | |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4290 | /* Opcode: AggSet * P2 * |
| 4291 | ** |
| 4292 | ** Move the top of the stack into the P2-th field of the current |
| 4293 | ** aggregate. String values are duplicated into new memory. |
| 4294 | */ |
| 4295 | case OP_AggSet: { |
| 4296 | AggElem *pFocus = AggInFocus(p->agg); |
drh | 2c79c67 | 2004-01-31 20:20:29 +0000 | [diff] [blame] | 4297 | Mem *pMem; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4298 | int i = pOp->p2; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 4299 | assert( pTos>=p->aStack ); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4300 | if( pFocus==0 ) goto no_mem; |
drh | 2c79c67 | 2004-01-31 20:20:29 +0000 | [diff] [blame] | 4301 | assert( i>=0 && i<p->agg.nMem ); |
| 4302 | Deephemeralize(pTos); |
| 4303 | pMem = &pFocus->aMem[i]; |
| 4304 | Release(pMem); |
| 4305 | *pMem = *pTos; |
| 4306 | if( pMem->flags & MEM_Dyn ){ |
| 4307 | pTos->flags = MEM_Null; |
| 4308 | }else if( pMem->flags & MEM_Short ){ |
| 4309 | pMem->z = pMem->zShort; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4310 | } |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 4311 | pTos--; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4312 | break; |
| 4313 | } |
| 4314 | |
| 4315 | /* Opcode: AggGet * P2 * |
| 4316 | ** |
| 4317 | ** Push a new entry onto the stack which is a copy of the P2-th field |
| 4318 | ** of the current aggregate. Strings are not duplicated so |
| 4319 | ** string values will be ephemeral. |
| 4320 | */ |
| 4321 | case OP_AggGet: { |
| 4322 | AggElem *pFocus = AggInFocus(p->agg); |
drh | 2c79c67 | 2004-01-31 20:20:29 +0000 | [diff] [blame] | 4323 | Mem *pMem; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4324 | int i = pOp->p2; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4325 | if( pFocus==0 ) goto no_mem; |
drh | 2c79c67 | 2004-01-31 20:20:29 +0000 | [diff] [blame] | 4326 | assert( i>=0 && i<p->agg.nMem ); |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 4327 | pTos++; |
drh | 2c79c67 | 2004-01-31 20:20:29 +0000 | [diff] [blame] | 4328 | pMem = &pFocus->aMem[i]; |
| 4329 | *pTos = *pMem; |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 4330 | if( pTos->flags & (MEM_Str|MEM_Blob) ){ |
drh | 3914aed | 2004-01-31 20:40:42 +0000 | [diff] [blame] | 4331 | pTos->flags &= ~(MEM_Dyn|MEM_Static|MEM_Short); |
| 4332 | pTos->flags |= MEM_Ephem; |
| 4333 | } |
danielk1977 | 8a6b541 | 2004-05-24 07:04:25 +0000 | [diff] [blame] | 4334 | if( pTos->flags&MEM_Str ){ |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 4335 | sqlite3VdbeChangeEncoding(pTos, db->enc); |
danielk1977 | 8a6b541 | 2004-05-24 07:04:25 +0000 | [diff] [blame] | 4336 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4337 | break; |
| 4338 | } |
| 4339 | |
| 4340 | /* Opcode: AggNext * P2 * |
| 4341 | ** |
| 4342 | ** Make the next aggregate value the current aggregate. The prior |
| 4343 | ** aggregate is deleted. If all aggregate values have been consumed, |
| 4344 | ** jump to P2. |
| 4345 | ** |
| 4346 | ** The order of aggregator opcodes is important. The order is: |
| 4347 | ** AggReset AggFocus AggNext. In other words, you must execute |
| 4348 | ** AggReset first, then zero or more AggFocus operations, then |
| 4349 | ** zero or more AggNext operations. You must not execute an AggFocus |
| 4350 | ** in between an AggNext and an AggReset. |
| 4351 | */ |
| 4352 | case OP_AggNext: { |
drh | caec2f1 | 2003-01-07 02:47:47 +0000 | [diff] [blame] | 4353 | CHECK_FOR_INTERRUPT; |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 4354 | if( p->agg.pSearch==0 ){ |
| 4355 | p->agg.pSearch = sqliteHashFirst(&p->agg.hash); |
| 4356 | }else{ |
| 4357 | p->agg.pSearch = sqliteHashNext(p->agg.pSearch); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4358 | } |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 4359 | if( p->agg.pSearch==0 ){ |
| 4360 | pc = pOp->p2 - 1; |
| 4361 | } else { |
drh | e509535 | 2002-02-24 03:25:14 +0000 | [diff] [blame] | 4362 | int i; |
danielk1977 | 22322fd | 2004-05-25 23:35:17 +0000 | [diff] [blame] | 4363 | sqlite3_context ctx; |
drh | e509535 | 2002-02-24 03:25:14 +0000 | [diff] [blame] | 4364 | Mem *aMem; |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 4365 | p->agg.pCurrent = sqliteHashData(p->agg.pSearch); |
drh | e509535 | 2002-02-24 03:25:14 +0000 | [diff] [blame] | 4366 | aMem = p->agg.pCurrent->aMem; |
| 4367 | for(i=0; i<p->agg.nMem; i++){ |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 4368 | int freeCtx; |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 4369 | if( p->agg.apFunc[i]==0 ) continue; |
| 4370 | if( p->agg.apFunc[i]->xFinalize==0 ) continue; |
drh | 00706be | 2004-01-30 14:49:16 +0000 | [diff] [blame] | 4371 | ctx.s.flags = MEM_Null; |
| 4372 | ctx.s.z = aMem[i].zShort; |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 4373 | ctx.pAgg = (void*)aMem[i].z; |
drh | 00706be | 2004-01-30 14:49:16 +0000 | [diff] [blame] | 4374 | freeCtx = aMem[i].z && aMem[i].z!=aMem[i].zShort; |
| 4375 | ctx.cnt = aMem[i].i; |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 4376 | ctx.isStep = 0; |
| 4377 | ctx.pFunc = p->agg.apFunc[i]; |
| 4378 | (*p->agg.apFunc[i]->xFinalize)(&ctx); |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 4379 | if( freeCtx ){ |
| 4380 | sqliteFree( aMem[i].z ); |
| 4381 | } |
drh | 00706be | 2004-01-30 14:49:16 +0000 | [diff] [blame] | 4382 | aMem[i] = ctx.s; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 4383 | if( aMem[i].flags & MEM_Short ){ |
drh | 00706be | 2004-01-30 14:49:16 +0000 | [diff] [blame] | 4384 | aMem[i].z = aMem[i].zShort; |
drh | e509535 | 2002-02-24 03:25:14 +0000 | [diff] [blame] | 4385 | } |
drh | e509535 | 2002-02-24 03:25:14 +0000 | [diff] [blame] | 4386 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4387 | } |
| 4388 | break; |
| 4389 | } |
| 4390 | |
drh | 6f8c91c | 2003-12-07 00:24:35 +0000 | [diff] [blame] | 4391 | /* Opcode: Vacuum * * * |
| 4392 | ** |
| 4393 | ** Vacuum the entire database. This opcode will cause other virtual |
| 4394 | ** machines to be created and run. It may not be called from within |
| 4395 | ** a transaction. |
| 4396 | */ |
| 4397 | case OP_Vacuum: { |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 4398 | if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse; |
| 4399 | rc = sqlite3RunVacuum(&p->zErrMsg, db); |
| 4400 | if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse; |
drh | 6f8c91c | 2003-12-07 00:24:35 +0000 | [diff] [blame] | 4401 | break; |
| 4402 | } |
| 4403 | |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4404 | /* An other opcode is illegal... |
| 4405 | */ |
| 4406 | default: { |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 4407 | sqlite3_snprintf(sizeof(zBuf),zBuf,"%d",pOp->opcode); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 4408 | sqlite3SetString(&p->zErrMsg, "unknown opcode ", zBuf, (char*)0); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4409 | rc = SQLITE_INTERNAL; |
| 4410 | break; |
| 4411 | } |
| 4412 | |
| 4413 | /***************************************************************************** |
| 4414 | ** The cases of the switch statement above this line should all be indented |
| 4415 | ** by 6 spaces. But the left-most 6 spaces have been removed to improve the |
| 4416 | ** readability. From this point on down, the normal indentation rules are |
| 4417 | ** restored. |
| 4418 | *****************************************************************************/ |
| 4419 | } |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 4420 | |
drh | 7b39686 | 2003-01-01 23:06:20 +0000 | [diff] [blame] | 4421 | #ifdef VDBE_PROFILE |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 4422 | { |
| 4423 | long long elapse = hwtime() - start; |
| 4424 | pOp->cycles += elapse; |
| 4425 | pOp->cnt++; |
| 4426 | #if 0 |
| 4427 | fprintf(stdout, "%10lld ", elapse); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 4428 | sqlite3VdbePrintOp(stdout, origPc, &p->aOp[origPc]); |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 4429 | #endif |
| 4430 | } |
drh | 7b39686 | 2003-01-01 23:06:20 +0000 | [diff] [blame] | 4431 | #endif |
| 4432 | |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 4433 | /* The following code adds nothing to the actual functionality |
| 4434 | ** of the program. It is only here for testing and debugging. |
| 4435 | ** On the other hand, it does burn CPU cycles every time through |
| 4436 | ** the evaluator loop. So we can leave it out when NDEBUG is defined. |
| 4437 | */ |
| 4438 | #ifndef NDEBUG |
drh | 3914aed | 2004-01-31 20:40:42 +0000 | [diff] [blame] | 4439 | /* Sanity checking on the top element of the stack */ |
| 4440 | if( pTos>=p->aStack ){ |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 4441 | sqlite3VdbeMemSanity(pTos, db->enc); |
drh | 3914aed | 2004-01-31 20:40:42 +0000 | [diff] [blame] | 4442 | } |
drh | 58b9576 | 2000-06-02 01:17:37 +0000 | [diff] [blame] | 4443 | if( pc<-1 || pc>=p->nOp ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 4444 | sqlite3SetString(&p->zErrMsg, "jump destination out of range", (char*)0); |
drh | 58b9576 | 2000-06-02 01:17:37 +0000 | [diff] [blame] | 4445 | rc = SQLITE_INTERNAL; |
| 4446 | } |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 4447 | if( p->trace && pTos>=p->aStack ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4448 | int i; |
| 4449 | fprintf(p->trace, "Stack:"); |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 4450 | for(i=0; i>-5 && &pTos[i]>=p->aStack; i--){ |
| 4451 | if( pTos[i].flags & MEM_Null ){ |
drh | c61053b | 2000-06-04 12:58:36 +0000 | [diff] [blame] | 4452 | fprintf(p->trace, " NULL"); |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 4453 | }else if( (pTos[i].flags & (MEM_Int|MEM_Str))==(MEM_Int|MEM_Str) ){ |
drh | a3b321d | 2004-05-11 09:31:31 +0000 | [diff] [blame] | 4454 | fprintf(p->trace, " si:%lld", pTos[i].i); |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 4455 | }else if( pTos[i].flags & MEM_Int ){ |
drh | a3b321d | 2004-05-11 09:31:31 +0000 | [diff] [blame] | 4456 | fprintf(p->trace, " i:%lld", pTos[i].i); |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 4457 | }else if( pTos[i].flags & MEM_Real ){ |
| 4458 | fprintf(p->trace, " r:%g", pTos[i].r); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4459 | }else{ |
danielk1977 | ca6b291 | 2004-05-21 10:49:47 +0000 | [diff] [blame] | 4460 | char zBuf[100]; |
drh | 7906975 | 2004-05-22 21:30:40 +0000 | [diff] [blame] | 4461 | prettyPrintMem(&pTos[i], zBuf, 100); |
danielk1977 | ca6b291 | 2004-05-21 10:49:47 +0000 | [diff] [blame] | 4462 | fprintf(p->trace, " "); |
| 4463 | fprintf(p->trace, zBuf); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4464 | } |
| 4465 | } |
drh | 7bc09d3 | 2002-11-01 01:55:36 +0000 | [diff] [blame] | 4466 | if( rc!=0 ) fprintf(p->trace," rc=%d",rc); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4467 | fprintf(p->trace,"\n"); |
| 4468 | } |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 4469 | #endif |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 4470 | } /* The end of the for(;;) loop the loops through opcodes */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4471 | |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 4472 | /* If we reach this point, it means that execution is finished. |
| 4473 | */ |
| 4474 | vdbe_halt: |
| 4475 | if( rc ){ |
| 4476 | p->rc = rc; |
| 4477 | rc = SQLITE_ERROR; |
| 4478 | }else{ |
| 4479 | rc = SQLITE_DONE; |
| 4480 | } |
| 4481 | p->magic = VDBE_MAGIC_HALT; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 4482 | p->pTos = pTos; |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 4483 | return rc; |
| 4484 | |
| 4485 | /* Jump to here if a malloc() fails. It's hard to get a malloc() |
| 4486 | ** to fail on a modern VM computer, so this code is untested. |
| 4487 | */ |
| 4488 | no_mem: |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 4489 | sqlite3SetString(&p->zErrMsg, "out of memory", (char*)0); |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 4490 | rc = SQLITE_NOMEM; |
| 4491 | goto vdbe_halt; |
| 4492 | |
| 4493 | /* Jump to here for an SQLITE_MISUSE error. |
| 4494 | */ |
| 4495 | abort_due_to_misuse: |
| 4496 | rc = SQLITE_MISUSE; |
| 4497 | /* Fall thru into abort_due_to_error */ |
| 4498 | |
| 4499 | /* Jump to here for any other kind of fatal error. The "rc" variable |
| 4500 | ** should hold the error number. |
| 4501 | */ |
| 4502 | abort_due_to_error: |
drh | 483750b | 2003-01-29 18:46:51 +0000 | [diff] [blame] | 4503 | if( p->zErrMsg==0 ){ |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 4504 | if( sqlite3_malloc_failed ) rc = SQLITE_NOMEM; |
| 4505 | sqlite3SetString(&p->zErrMsg, sqlite3_error_string(rc), (char*)0); |
drh | 483750b | 2003-01-29 18:46:51 +0000 | [diff] [blame] | 4506 | } |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 4507 | goto vdbe_halt; |
| 4508 | |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 4509 | /* Jump to here if the sqlite3_interrupt() API sets the interrupt |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 4510 | ** flag. |
| 4511 | */ |
| 4512 | abort_due_to_interrupt: |
| 4513 | assert( db->flags & SQLITE_Interrupt ); |
| 4514 | db->flags &= ~SQLITE_Interrupt; |
| 4515 | if( db->magic!=SQLITE_MAGIC_BUSY ){ |
| 4516 | rc = SQLITE_MISUSE; |
| 4517 | }else{ |
| 4518 | rc = SQLITE_INTERRUPT; |
| 4519 | } |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 4520 | sqlite3SetString(&p->zErrMsg, sqlite3_error_string(rc), (char*)0); |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 4521 | goto vdbe_halt; |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 4522 | } |