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 | be8a783 | 2006-06-13 15:00:54 +0000 | [diff] [blame] | 46 | ** $Id: vdbe.c,v 1.557 2006/06/13 15:00:55 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 | 6bf8957 | 2004-11-03 16:27:01 +0000 | [diff] [blame] | 73 | ** The next global variable is incremented each type the OP_Sort opcode |
| 74 | ** is executed. The test procedures use this information to make sure that |
| 75 | ** sorting is occurring or not occuring at appropriate times. This variable |
| 76 | ** has no function other than to help verify the correct operation of the |
| 77 | ** library. |
| 78 | */ |
| 79 | int sqlite3_sort_count = 0; |
| 80 | |
| 81 | /* |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 82 | ** Release the memory associated with the given stack level. This |
| 83 | ** leaves the Mem.flags field in an inconsistent state. |
drh | c61053b | 2000-06-04 12:58:36 +0000 | [diff] [blame] | 84 | */ |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 85 | #define Release(P) if((P)->flags&MEM_Dyn){ sqlite3VdbeMemRelease(P); } |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 86 | |
| 87 | /* |
danielk1977 | bd7e460 | 2004-05-24 07:34:48 +0000 | [diff] [blame] | 88 | ** Convert the given stack entity into a string if it isn't one |
| 89 | ** already. Return non-zero if a malloc() fails. |
| 90 | */ |
| 91 | #define Stringify(P, enc) \ |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 92 | if(((P)->flags&(MEM_Str|MEM_Blob))==0 && sqlite3VdbeMemStringify(P,enc)) \ |
| 93 | { goto no_mem; } |
danielk1977 | bd7e460 | 2004-05-24 07:34:48 +0000 | [diff] [blame] | 94 | |
| 95 | /* |
| 96 | ** Convert the given stack entity into a string that has been obtained |
| 97 | ** from sqliteMalloc(). This is different from Stringify() above in that |
| 98 | ** Stringify() will use the NBFS bytes of static string space if the string |
| 99 | ** will fit but this routine always mallocs for space. |
| 100 | ** Return non-zero if we run out of memory. |
| 101 | */ |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 102 | #define Dynamicify(P,enc) sqlite3VdbeMemDynamicify(P) |
| 103 | |
drh | d508e7f | 2006-01-13 01:48:59 +0000 | [diff] [blame] | 104 | /* |
| 105 | ** The header of a record consists of a sequence variable-length integers. |
| 106 | ** These integers are almost always small and are encoded as a single byte. |
| 107 | ** The following macro takes advantage this fact to provide a fast decode |
| 108 | ** of the integers in a record header. It is faster for the common case |
| 109 | ** where the integer is a single byte. It is a little slower when the |
| 110 | ** integer is two or more bytes. But overall it is faster. |
| 111 | ** |
| 112 | ** The following expressions are equivalent: |
| 113 | ** |
| 114 | ** x = sqlite3GetVarint32( A, &B ); |
| 115 | ** |
| 116 | ** x = GetVarint( A, B ); |
| 117 | ** |
| 118 | */ |
| 119 | #define GetVarint(A,B) ((B = *(A))<=0x7f ? 1 : sqlite3GetVarint32(A, &B)) |
danielk1977 | bd7e460 | 2004-05-24 07:34:48 +0000 | [diff] [blame] | 120 | |
| 121 | /* |
| 122 | ** An ephemeral string value (signified by the MEM_Ephem flag) contains |
| 123 | ** a pointer to a dynamically allocated string where some other entity |
| 124 | ** is responsible for deallocating that string. Because the stack entry |
| 125 | ** does not control the string, it might be deleted without the stack |
| 126 | ** entry knowing it. |
| 127 | ** |
| 128 | ** This routine converts an ephemeral string into a dynamically allocated |
| 129 | ** string that the stack entry itself controls. In other words, it |
| 130 | ** converts an MEM_Ephem string into an MEM_Dyn string. |
| 131 | */ |
| 132 | #define Deephemeralize(P) \ |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 133 | if( ((P)->flags&MEM_Ephem)!=0 \ |
| 134 | && sqlite3VdbeMemMakeWriteable(P) ){ goto no_mem;} |
danielk1977 | 93d4675 | 2004-05-23 13:30:58 +0000 | [diff] [blame] | 135 | |
| 136 | /* |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 137 | ** Argument pMem points at a memory cell that will be passed to a |
| 138 | ** user-defined function or returned to the user as the result of a query. |
| 139 | ** The second argument, 'db_enc' is the text encoding used by the vdbe for |
| 140 | ** stack variables. This routine sets the pMem->enc and pMem->type |
| 141 | ** variables used by the sqlite3_value_*() routines. |
| 142 | */ |
drh | 3a41a3f | 2004-05-30 02:14:17 +0000 | [diff] [blame] | 143 | #define storeTypeInfo(A,B) _storeTypeInfo(A) |
| 144 | static void _storeTypeInfo(Mem *pMem){ |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 145 | int flags = pMem->flags; |
| 146 | if( flags & MEM_Null ){ |
drh | 9c05483 | 2004-05-31 18:51:57 +0000 | [diff] [blame] | 147 | pMem->type = SQLITE_NULL; |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 148 | } |
| 149 | else if( flags & MEM_Int ){ |
drh | 9c05483 | 2004-05-31 18:51:57 +0000 | [diff] [blame] | 150 | pMem->type = SQLITE_INTEGER; |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 151 | } |
| 152 | else if( flags & MEM_Real ){ |
drh | 9c05483 | 2004-05-31 18:51:57 +0000 | [diff] [blame] | 153 | pMem->type = SQLITE_FLOAT; |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 154 | } |
| 155 | else if( flags & MEM_Str ){ |
drh | 9c05483 | 2004-05-31 18:51:57 +0000 | [diff] [blame] | 156 | pMem->type = SQLITE_TEXT; |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 157 | }else{ |
drh | 9c05483 | 2004-05-31 18:51:57 +0000 | [diff] [blame] | 158 | pMem->type = SQLITE_BLOB; |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 159 | } |
| 160 | } |
danielk1977 | 8a6b541 | 2004-05-24 07:04:25 +0000 | [diff] [blame] | 161 | |
| 162 | /* |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 163 | ** Pop the stack N times. |
| 164 | */ |
| 165 | static void popStack(Mem **ppTos, int N){ |
| 166 | Mem *pTos = *ppTos; |
| 167 | while( N>0 ){ |
| 168 | N--; |
| 169 | Release(pTos); |
| 170 | pTos--; |
| 171 | } |
| 172 | *ppTos = pTos; |
| 173 | } |
| 174 | |
| 175 | /* |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 176 | ** Allocate cursor number iCur. Return a pointer to it. Return NULL |
| 177 | ** if we run out of memory. |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 178 | */ |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 +0000 | [diff] [blame] | 179 | static Cursor *allocateCursor(Vdbe *p, int iCur, int iDb){ |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 180 | Cursor *pCx; |
drh | 290c194 | 2004-08-21 17:54:45 +0000 | [diff] [blame] | 181 | assert( iCur<p->nCursor ); |
| 182 | if( p->apCsr[iCur] ){ |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 183 | sqlite3VdbeFreeCursor(p->apCsr[iCur]); |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 184 | } |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 185 | p->apCsr[iCur] = pCx = sqliteMalloc( sizeof(Cursor) ); |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 +0000 | [diff] [blame] | 186 | if( pCx ){ |
| 187 | pCx->iDb = iDb; |
| 188 | } |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 189 | return pCx; |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 190 | } |
| 191 | |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 192 | /* |
drh | 29d7210 | 2006-02-09 22:13:41 +0000 | [diff] [blame] | 193 | ** Try to convert a value into a numeric representation if we can |
| 194 | ** do so without loss of information. In other words, if the string |
| 195 | ** looks like a number, convert it into a number. If it does not |
| 196 | ** look like a number, leave it alone. |
| 197 | */ |
| 198 | static void applyNumericAffinity(Mem *pRec){ |
| 199 | if( (pRec->flags & (MEM_Real|MEM_Int))==0 ){ |
| 200 | int realnum; |
| 201 | sqlite3VdbeMemNulTerminate(pRec); |
| 202 | if( (pRec->flags&MEM_Str) |
| 203 | && sqlite3IsNumber(pRec->z, &realnum, pRec->enc) ){ |
| 204 | i64 value; |
| 205 | sqlite3VdbeChangeEncoding(pRec, SQLITE_UTF8); |
| 206 | if( !realnum && sqlite3atoi64(pRec->z, &value) ){ |
| 207 | sqlite3VdbeMemRelease(pRec); |
| 208 | pRec->i = value; |
| 209 | pRec->flags = MEM_Int; |
| 210 | }else{ |
| 211 | sqlite3VdbeMemRealify(pRec); |
| 212 | } |
| 213 | } |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | /* |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 218 | ** Processing is determine by the affinity parameter: |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 219 | ** |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 220 | ** SQLITE_AFF_INTEGER: |
| 221 | ** SQLITE_AFF_REAL: |
| 222 | ** SQLITE_AFF_NUMERIC: |
| 223 | ** Try to convert pRec to an integer representation or a |
| 224 | ** floating-point representation if an integer representation |
| 225 | ** is not possible. Note that the integer representation is |
| 226 | ** always preferred, even if the affinity is REAL, because |
| 227 | ** an integer representation is more space efficient on disk. |
| 228 | ** |
| 229 | ** SQLITE_AFF_TEXT: |
| 230 | ** Convert pRec to a text representation. |
| 231 | ** |
| 232 | ** SQLITE_AFF_NONE: |
| 233 | ** No-op. pRec is unchanged. |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 234 | */ |
danielk1977 | 8a6b541 | 2004-05-24 07:04:25 +0000 | [diff] [blame] | 235 | static void applyAffinity(Mem *pRec, char affinity, u8 enc){ |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 236 | if( affinity==SQLITE_AFF_TEXT ){ |
drh | 17c4029 | 2004-07-21 02:53:29 +0000 | [diff] [blame] | 237 | /* Only attempt the conversion to TEXT if there is an integer or real |
| 238 | ** representation (blob and NULL do not get converted) but no string |
| 239 | ** representation. |
| 240 | */ |
| 241 | if( 0==(pRec->flags&MEM_Str) && (pRec->flags&(MEM_Real|MEM_Int)) ){ |
| 242 | sqlite3VdbeMemStringify(pRec, enc); |
| 243 | } |
| 244 | pRec->flags &= ~(MEM_Real|MEM_Int); |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 245 | }else if( affinity!=SQLITE_AFF_NONE ){ |
| 246 | assert( affinity==SQLITE_AFF_INTEGER || affinity==SQLITE_AFF_REAL |
| 247 | || affinity==SQLITE_AFF_NUMERIC ); |
drh | 29d7210 | 2006-02-09 22:13:41 +0000 | [diff] [blame] | 248 | applyNumericAffinity(pRec); |
| 249 | if( pRec->flags & MEM_Real ){ |
drh | 8df447f | 2005-11-01 15:48:24 +0000 | [diff] [blame] | 250 | sqlite3VdbeIntegerAffinity(pRec); |
drh | 17c4029 | 2004-07-21 02:53:29 +0000 | [diff] [blame] | 251 | } |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 252 | } |
| 253 | } |
| 254 | |
danielk1977 | aee18ef | 2005-03-09 12:26:50 +0000 | [diff] [blame] | 255 | /* |
drh | 29d7210 | 2006-02-09 22:13:41 +0000 | [diff] [blame] | 256 | ** Try to convert the type of a function argument or a result column |
| 257 | ** into a numeric representation. Use either INTEGER or REAL whichever |
| 258 | ** is appropriate. But only do the conversion if it is possible without |
| 259 | ** loss of information and return the revised type of the argument. |
| 260 | ** |
| 261 | ** This is an EXPERIMENTAL api and is subject to change or removal. |
| 262 | */ |
| 263 | int sqlite3_value_numeric_type(sqlite3_value *pVal){ |
| 264 | Mem *pMem = (Mem*)pVal; |
| 265 | applyNumericAffinity(pMem); |
| 266 | storeTypeInfo(pMem, 0); |
| 267 | return pMem->type; |
| 268 | } |
| 269 | |
| 270 | /* |
danielk1977 | aee18ef | 2005-03-09 12:26:50 +0000 | [diff] [blame] | 271 | ** Exported version of applyAffinity(). This one works on sqlite3_value*, |
| 272 | ** not the internal Mem* type. |
| 273 | */ |
| 274 | void sqlite3ValueApplyAffinity(sqlite3_value *pVal, u8 affinity, u8 enc){ |
| 275 | applyAffinity((Mem *)pVal, affinity, enc); |
| 276 | } |
| 277 | |
danielk1977 | b5402fb | 2005-01-12 07:15:04 +0000 | [diff] [blame] | 278 | #ifdef SQLITE_DEBUG |
drh | b6f5452 | 2004-05-20 02:42:16 +0000 | [diff] [blame] | 279 | /* |
danielk1977 | ca6b291 | 2004-05-21 10:49:47 +0000 | [diff] [blame] | 280 | ** Write a nice string representation of the contents of cell pMem |
| 281 | ** into buffer zBuf, length nBuf. |
| 282 | */ |
drh | 7416170 | 2006-02-24 02:53:49 +0000 | [diff] [blame] | 283 | void sqlite3VdbeMemPrettyPrint(Mem *pMem, char *zBuf){ |
danielk1977 | ca6b291 | 2004-05-21 10:49:47 +0000 | [diff] [blame] | 284 | char *zCsr = zBuf; |
| 285 | int f = pMem->flags; |
| 286 | |
drh | 5719628 | 2004-10-06 15:41:16 +0000 | [diff] [blame] | 287 | static const char *const encnames[] = {"(X)", "(8)", "(16LE)", "(16BE)"}; |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame] | 288 | |
danielk1977 | ca6b291 | 2004-05-21 10:49:47 +0000 | [diff] [blame] | 289 | if( f&MEM_Blob ){ |
| 290 | int i; |
| 291 | char c; |
| 292 | if( f & MEM_Dyn ){ |
| 293 | c = 'z'; |
| 294 | assert( (f & (MEM_Static|MEM_Ephem))==0 ); |
| 295 | }else if( f & MEM_Static ){ |
| 296 | c = 't'; |
| 297 | assert( (f & (MEM_Dyn|MEM_Ephem))==0 ); |
| 298 | }else if( f & MEM_Ephem ){ |
| 299 | c = 'e'; |
| 300 | assert( (f & (MEM_Static|MEM_Dyn))==0 ); |
| 301 | }else{ |
| 302 | c = 's'; |
| 303 | } |
| 304 | |
danielk1977 | b1bc953 | 2004-05-22 03:05:33 +0000 | [diff] [blame] | 305 | zCsr += sprintf(zCsr, "%c", c); |
| 306 | zCsr += sprintf(zCsr, "%d[", pMem->n); |
danielk1977 | ca6b291 | 2004-05-21 10:49:47 +0000 | [diff] [blame] | 307 | for(i=0; i<16 && i<pMem->n; i++){ |
| 308 | zCsr += sprintf(zCsr, "%02X ", ((int)pMem->z[i] & 0xFF)); |
| 309 | } |
| 310 | for(i=0; i<16 && i<pMem->n; i++){ |
| 311 | char z = pMem->z[i]; |
| 312 | if( z<32 || z>126 ) *zCsr++ = '.'; |
| 313 | else *zCsr++ = z; |
| 314 | } |
| 315 | |
| 316 | zCsr += sprintf(zCsr, "]"); |
danielk1977 | b1bc953 | 2004-05-22 03:05:33 +0000 | [diff] [blame] | 317 | *zCsr = '\0'; |
| 318 | }else if( f & MEM_Str ){ |
| 319 | int j, k; |
| 320 | zBuf[0] = ' '; |
| 321 | if( f & MEM_Dyn ){ |
| 322 | zBuf[1] = 'z'; |
| 323 | assert( (f & (MEM_Static|MEM_Ephem))==0 ); |
| 324 | }else if( f & MEM_Static ){ |
| 325 | zBuf[1] = 't'; |
| 326 | assert( (f & (MEM_Dyn|MEM_Ephem))==0 ); |
| 327 | }else if( f & MEM_Ephem ){ |
| 328 | zBuf[1] = 'e'; |
| 329 | assert( (f & (MEM_Static|MEM_Dyn))==0 ); |
| 330 | }else{ |
| 331 | zBuf[1] = 's'; |
| 332 | } |
| 333 | k = 2; |
| 334 | k += sprintf(&zBuf[k], "%d", pMem->n); |
| 335 | zBuf[k++] = '['; |
| 336 | for(j=0; j<15 && j<pMem->n; j++){ |
| 337 | u8 c = pMem->z[j]; |
danielk1977 | b1bc953 | 2004-05-22 03:05:33 +0000 | [diff] [blame] | 338 | if( c>=0x20 && c<0x7f ){ |
| 339 | zBuf[k++] = c; |
| 340 | }else{ |
| 341 | zBuf[k++] = '.'; |
| 342 | } |
| 343 | } |
| 344 | zBuf[k++] = ']'; |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame] | 345 | k += sprintf(&zBuf[k], encnames[pMem->enc]); |
danielk1977 | b1bc953 | 2004-05-22 03:05:33 +0000 | [diff] [blame] | 346 | zBuf[k++] = 0; |
danielk1977 | ca6b291 | 2004-05-21 10:49:47 +0000 | [diff] [blame] | 347 | } |
danielk1977 | ca6b291 | 2004-05-21 10:49:47 +0000 | [diff] [blame] | 348 | } |
| 349 | #endif |
| 350 | |
danielk1977 | 84ac9d0 | 2004-05-18 09:58:06 +0000 | [diff] [blame] | 351 | |
drh | 7b39686 | 2003-01-01 23:06:20 +0000 | [diff] [blame] | 352 | #ifdef VDBE_PROFILE |
| 353 | /* |
| 354 | ** The following routine only works on pentium-class processors. |
drh | 81db88e | 2004-12-07 12:29:17 +0000 | [diff] [blame] | 355 | ** It uses the RDTSC opcode to read the cycle count value out of the |
drh | 7b39686 | 2003-01-01 23:06:20 +0000 | [diff] [blame] | 356 | ** processor and returns that value. This can be used for high-res |
| 357 | ** profiling. |
| 358 | */ |
| 359 | __inline__ unsigned long long int hwtime(void){ |
| 360 | unsigned long long int x; |
| 361 | __asm__("rdtsc\n\t" |
| 362 | "mov %%edx, %%ecx\n\t" |
| 363 | :"=A" (x)); |
| 364 | return x; |
| 365 | } |
| 366 | #endif |
| 367 | |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 368 | /* |
drh | caec2f1 | 2003-01-07 02:47:47 +0000 | [diff] [blame] | 369 | ** The CHECK_FOR_INTERRUPT macro defined here looks to see if the |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 370 | ** sqlite3_interrupt() routine has been called. If it has been, then |
drh | caec2f1 | 2003-01-07 02:47:47 +0000 | [diff] [blame] | 371 | ** processing of the VDBE program is interrupted. |
| 372 | ** |
| 373 | ** This macro added to every instruction that does a jump in order to |
| 374 | ** implement a loop. This test used to be on every single instruction, |
| 375 | ** but that meant we more testing that we needed. By only testing the |
| 376 | ** flag on jump instructions, we get a (small) speed improvement. |
| 377 | */ |
| 378 | #define CHECK_FOR_INTERRUPT \ |
| 379 | if( db->flags & SQLITE_Interrupt ) goto abort_due_to_interrupt; |
| 380 | |
| 381 | |
| 382 | /* |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 383 | ** Execute as much of a VDBE program as we can then return. |
| 384 | ** |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 385 | ** sqlite3VdbeMakeReady() must be called before this routine in order to |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 386 | ** close the program with a final OP_Halt and to set up the callbacks |
| 387 | ** and the error message pointer. |
| 388 | ** |
| 389 | ** Whenever a row or result data is available, this routine will either |
| 390 | ** invoke the result callback (if there is one) or return with |
drh | 326dce7 | 2003-01-29 14:06:07 +0000 | [diff] [blame] | 391 | ** SQLITE_ROW. |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 392 | ** |
| 393 | ** If an attempt is made to open a locked database, then this routine |
| 394 | ** will either invoke the busy callback (if there is one) or it will |
| 395 | ** return SQLITE_BUSY. |
| 396 | ** |
| 397 | ** If an error occurs, an error message is written to memory obtained |
| 398 | ** from sqliteMalloc() and p->zErrMsg is made to point to that memory. |
| 399 | ** The error code is stored in p->rc and this routine returns SQLITE_ERROR. |
| 400 | ** |
| 401 | ** If the callback ever returns non-zero, then the program exits |
| 402 | ** immediately. There will be no error message but the p->rc field is |
| 403 | ** set to SQLITE_ABORT and this routine will return SQLITE_ERROR. |
| 404 | ** |
drh | 9468c7f | 2003-03-07 19:50:07 +0000 | [diff] [blame] | 405 | ** A memory allocation error causes p->rc to be set to SQLITE_NOMEM and this |
| 406 | ** routine to return SQLITE_ERROR. |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 407 | ** |
| 408 | ** Other fatal errors return SQLITE_ERROR. |
| 409 | ** |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 410 | ** After this routine has finished, sqlite3VdbeFinalize() should be |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 411 | ** used to clean up the mess that was left behind. |
| 412 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 413 | int sqlite3VdbeExec( |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 414 | Vdbe *p /* The VDBE */ |
| 415 | ){ |
| 416 | int pc; /* The program counter */ |
| 417 | Op *pOp; /* Current operation */ |
| 418 | int rc = SQLITE_OK; /* Value to return */ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 419 | sqlite3 *db = p->db; /* The database */ |
drh | 8079a0d | 2006-01-12 17:20:50 +0000 | [diff] [blame] | 420 | u8 encoding = ENC(db); /* The database encoding */ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 421 | Mem *pTos; /* Top entry in the operand stack */ |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 422 | #ifdef VDBE_PROFILE |
| 423 | unsigned long long start; /* CPU clock count at start of opcode */ |
| 424 | int origPc; /* Program counter at start of opcode */ |
| 425 | #endif |
danielk1977 | 348bb5d | 2003-10-18 09:37:26 +0000 | [diff] [blame] | 426 | #ifndef SQLITE_OMIT_PROGRESS_CALLBACK |
| 427 | int nProgressOps = 0; /* Opcodes executed since progress callback. */ |
| 428 | #endif |
danielk1977 | bc04f85 | 2005-03-29 08:26:13 +0000 | [diff] [blame] | 429 | #ifndef NDEBUG |
| 430 | Mem *pStackLimit; |
| 431 | #endif |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 432 | |
| 433 | if( p->magic!=VDBE_MAGIC_RUN ) return SQLITE_MISUSE; |
| 434 | assert( db->magic==SQLITE_MAGIC_BUSY ); |
danielk1977 | 2e588c7 | 2005-12-09 14:25:08 +0000 | [diff] [blame] | 435 | pTos = p->pTos; |
| 436 | if( p->rc==SQLITE_NOMEM ){ |
| 437 | /* This happens if a malloc() inside a call to sqlite3_column_text() or |
| 438 | ** sqlite3_column_text16() failed. */ |
| 439 | goto no_mem; |
| 440 | } |
drh | 3a84069 | 2003-01-29 22:58:26 +0000 | [diff] [blame] | 441 | assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY ); |
| 442 | p->rc = SQLITE_OK; |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 443 | assert( p->explain==0 ); |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 444 | if( p->popStack ){ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 445 | popStack(&pTos, p->popStack); |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 446 | p->popStack = 0; |
| 447 | } |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 448 | p->resOnStack = 0; |
drh | a4afb65 | 2005-07-09 02:16:02 +0000 | [diff] [blame] | 449 | db->busyHandler.nBusy = 0; |
drh | 9358164 | 2004-02-12 13:02:55 +0000 | [diff] [blame] | 450 | CHECK_FOR_INTERRUPT; |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 451 | for(pc=p->pc; rc==SQLITE_OK; pc++){ |
drh | caec2f1 | 2003-01-07 02:47:47 +0000 | [diff] [blame] | 452 | assert( pc>=0 && pc<p->nOp ); |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 453 | assert( pTos<=&p->aStack[pc] ); |
danielk1977 | 9e12800 | 2006-01-18 16:51:35 +0000 | [diff] [blame] | 454 | if( sqlite3MallocFailed() ) goto no_mem; |
drh | 7b39686 | 2003-01-01 23:06:20 +0000 | [diff] [blame] | 455 | #ifdef VDBE_PROFILE |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 456 | origPc = pc; |
drh | 7b39686 | 2003-01-01 23:06:20 +0000 | [diff] [blame] | 457 | start = hwtime(); |
| 458 | #endif |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 459 | pOp = &p->aOp[pc]; |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 460 | |
danielk1977 | 8b60e0f | 2005-01-12 09:10:39 +0000 | [diff] [blame] | 461 | /* Only allow tracing if SQLITE_DEBUG is defined. |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 462 | */ |
danielk1977 | 8b60e0f | 2005-01-12 09:10:39 +0000 | [diff] [blame] | 463 | #ifdef SQLITE_DEBUG |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 464 | if( p->trace ){ |
drh | 3f7d4e4 | 2004-07-24 14:35:58 +0000 | [diff] [blame] | 465 | if( pc==0 ){ |
| 466 | printf("VDBE Execution Trace:\n"); |
| 467 | sqlite3VdbePrintSql(p); |
| 468 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 469 | sqlite3VdbePrintOp(p->trace, pc, pOp); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 470 | } |
drh | 66560ad | 2006-01-06 14:32:19 +0000 | [diff] [blame] | 471 | if( p->trace==0 && pc==0 && sqlite3OsFileExists("vdbe_sqltrace") ){ |
drh | 3f7d4e4 | 2004-07-24 14:35:58 +0000 | [diff] [blame] | 472 | sqlite3VdbePrintSql(p); |
| 473 | } |
| 474 | #endif |
| 475 | |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 476 | |
drh | f603871 | 2004-02-08 18:07:34 +0000 | [diff] [blame] | 477 | /* Check to see if we need to simulate an interrupt. This only happens |
| 478 | ** if we have a special test build. |
| 479 | */ |
| 480 | #ifdef SQLITE_TEST |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 481 | if( sqlite3_interrupt_count>0 ){ |
| 482 | sqlite3_interrupt_count--; |
| 483 | if( sqlite3_interrupt_count==0 ){ |
| 484 | sqlite3_interrupt(db); |
drh | f603871 | 2004-02-08 18:07:34 +0000 | [diff] [blame] | 485 | } |
| 486 | } |
| 487 | #endif |
| 488 | |
danielk1977 | 348bb5d | 2003-10-18 09:37:26 +0000 | [diff] [blame] | 489 | #ifndef SQLITE_OMIT_PROGRESS_CALLBACK |
| 490 | /* Call the progress callback if it is configured and the required number |
| 491 | ** of VDBE ops have been executed (either since this invocation of |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 492 | ** sqlite3VdbeExec() or since last time the progress callback was called). |
danielk1977 | 348bb5d | 2003-10-18 09:37:26 +0000 | [diff] [blame] | 493 | ** If the progress callback returns non-zero, exit the virtual machine with |
| 494 | ** a return code SQLITE_ABORT. |
| 495 | */ |
drh | 3914aed | 2004-01-31 20:40:42 +0000 | [diff] [blame] | 496 | if( db->xProgress ){ |
| 497 | if( db->nProgressOps==nProgressOps ){ |
drh | f8888bb | 2006-05-26 19:57:19 +0000 | [diff] [blame] | 498 | if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse; |
drh | 3914aed | 2004-01-31 20:40:42 +0000 | [diff] [blame] | 499 | if( db->xProgress(db->pProgressArg)!=0 ){ |
drh | f8888bb | 2006-05-26 19:57:19 +0000 | [diff] [blame] | 500 | sqlite3SafetyOn(db); |
drh | 3914aed | 2004-01-31 20:40:42 +0000 | [diff] [blame] | 501 | rc = SQLITE_ABORT; |
| 502 | continue; /* skip to the next iteration of the for loop */ |
| 503 | } |
| 504 | nProgressOps = 0; |
drh | f8888bb | 2006-05-26 19:57:19 +0000 | [diff] [blame] | 505 | if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse; |
danielk1977 | 348bb5d | 2003-10-18 09:37:26 +0000 | [diff] [blame] | 506 | } |
drh | 3914aed | 2004-01-31 20:40:42 +0000 | [diff] [blame] | 507 | nProgressOps++; |
danielk1977 | 348bb5d | 2003-10-18 09:37:26 +0000 | [diff] [blame] | 508 | } |
danielk1977 | 348bb5d | 2003-10-18 09:37:26 +0000 | [diff] [blame] | 509 | #endif |
| 510 | |
danielk1977 | bc04f85 | 2005-03-29 08:26:13 +0000 | [diff] [blame] | 511 | #ifndef NDEBUG |
| 512 | /* This is to check that the return value of static function |
danielk1977 | 7a5147c | 2005-03-29 13:07:00 +0000 | [diff] [blame] | 513 | ** opcodeNoPush() (see vdbeaux.c) returns values that match the |
danielk1977 | bc04f85 | 2005-03-29 08:26:13 +0000 | [diff] [blame] | 514 | ** implementation of the virtual machine in this file. If |
danielk1977 | 7a5147c | 2005-03-29 13:07:00 +0000 | [diff] [blame] | 515 | ** opcodeNoPush() returns non-zero, then the stack is guarenteed |
danielk1977 | bc04f85 | 2005-03-29 08:26:13 +0000 | [diff] [blame] | 516 | ** not to grow when the opcode is executed. If it returns zero, then |
| 517 | ** the stack may grow by at most 1. |
| 518 | ** |
| 519 | ** The global wrapper function sqlite3VdbeOpcodeUsesStack() is not |
| 520 | ** available if NDEBUG is defined at build time. |
| 521 | */ |
| 522 | pStackLimit = pTos; |
danielk1977 | 7a5147c | 2005-03-29 13:07:00 +0000 | [diff] [blame] | 523 | if( !sqlite3VdbeOpcodeNoPush(pOp->opcode) ){ |
danielk1977 | bc04f85 | 2005-03-29 08:26:13 +0000 | [diff] [blame] | 524 | pStackLimit++; |
| 525 | } |
| 526 | #endif |
| 527 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 528 | switch( pOp->opcode ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 529 | |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 530 | /***************************************************************************** |
| 531 | ** What follows is a massive switch statement where each case implements a |
| 532 | ** separate instruction in the virtual machine. If we follow the usual |
| 533 | ** indentation conventions, each case should be indented by 6 spaces. But |
| 534 | ** that is a lot of wasted space on the left margin. So the code within |
| 535 | ** the switch statement will break with convention and be flush-left. Another |
| 536 | ** big comment (similar to this one) will mark the point in the code where |
| 537 | ** we transition back to normal indentation. |
drh | ac82fcf | 2002-09-08 17:23:41 +0000 | [diff] [blame] | 538 | ** |
| 539 | ** The formatting of each case is important. The makefile for SQLite |
| 540 | ** generates two C files "opcodes.h" and "opcodes.c" by scanning this |
| 541 | ** file looking for lines that begin with "case OP_". The opcodes.h files |
| 542 | ** will be filled with #defines that give unique integer values to each |
| 543 | ** opcode and the opcodes.c file is filled with an array of strings where |
drh | f2bc013 | 2004-10-04 13:19:23 +0000 | [diff] [blame] | 544 | ** each string is the symbolic name for the corresponding opcode. If the |
| 545 | ** case statement is followed by a comment of the form "/# same as ... #/" |
| 546 | ** that comment is used to determine the particular value of the opcode. |
drh | ac82fcf | 2002-09-08 17:23:41 +0000 | [diff] [blame] | 547 | ** |
danielk1977 | bc04f85 | 2005-03-29 08:26:13 +0000 | [diff] [blame] | 548 | ** If a comment on the same line as the "case OP_" construction contains |
danielk1977 | 7a5147c | 2005-03-29 13:07:00 +0000 | [diff] [blame] | 549 | ** the word "no-push", then the opcode is guarenteed not to grow the |
| 550 | ** vdbe stack when it is executed. See function opcode() in |
danielk1977 | bc04f85 | 2005-03-29 08:26:13 +0000 | [diff] [blame] | 551 | ** vdbeaux.c for details. |
| 552 | ** |
drh | ac82fcf | 2002-09-08 17:23:41 +0000 | [diff] [blame] | 553 | ** Documentation about VDBE opcodes is generated by scanning this file |
| 554 | ** for lines of that contain "Opcode:". That line and all subsequent |
| 555 | ** comment lines are used in the generation of the opcode.html documentation |
| 556 | ** file. |
| 557 | ** |
| 558 | ** SUMMARY: |
| 559 | ** |
| 560 | ** Formatting is important to scripts that scan this file. |
| 561 | ** Do not deviate from the formatting style currently in use. |
| 562 | ** |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 563 | *****************************************************************************/ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 564 | |
drh | 58a1168 | 2001-11-10 13:51:08 +0000 | [diff] [blame] | 565 | /* Opcode: Goto * P2 * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 566 | ** |
| 567 | ** An unconditional jump to address P2. |
| 568 | ** The next instruction executed will be |
| 569 | ** the one at index P2 from the beginning of |
| 570 | ** the program. |
| 571 | */ |
danielk1977 | 7a5147c | 2005-03-29 13:07:00 +0000 | [diff] [blame] | 572 | case OP_Goto: { /* no-push */ |
drh | caec2f1 | 2003-01-07 02:47:47 +0000 | [diff] [blame] | 573 | CHECK_FOR_INTERRUPT; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 574 | pc = pOp->p2 - 1; |
| 575 | break; |
| 576 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 577 | |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 578 | /* Opcode: Gosub * P2 * |
| 579 | ** |
| 580 | ** Push the current address plus 1 onto the return address stack |
| 581 | ** and then jump to address P2. |
| 582 | ** |
| 583 | ** The return address stack is of limited depth. If too many |
| 584 | ** OP_Gosub operations occur without intervening OP_Returns, then |
| 585 | ** the return address stack will fill up and processing will abort |
| 586 | ** with a fatal error. |
| 587 | */ |
danielk1977 | 7a5147c | 2005-03-29 13:07:00 +0000 | [diff] [blame] | 588 | case OP_Gosub: { /* no-push */ |
drh | 17c4029 | 2004-07-21 02:53:29 +0000 | [diff] [blame] | 589 | assert( p->returnDepth<sizeof(p->returnStack)/sizeof(p->returnStack[0]) ); |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 590 | p->returnStack[p->returnDepth++] = pc+1; |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 591 | pc = pOp->p2 - 1; |
| 592 | break; |
| 593 | } |
| 594 | |
| 595 | /* Opcode: Return * * * |
| 596 | ** |
| 597 | ** Jump immediately to the next instruction after the last unreturned |
| 598 | ** OP_Gosub. If an OP_Return has occurred for all OP_Gosubs, then |
| 599 | ** processing aborts with a fatal error. |
| 600 | */ |
danielk1977 | 7a5147c | 2005-03-29 13:07:00 +0000 | [diff] [blame] | 601 | case OP_Return: { /* no-push */ |
drh | 17c4029 | 2004-07-21 02:53:29 +0000 | [diff] [blame] | 602 | assert( p->returnDepth>0 ); |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 603 | p->returnDepth--; |
| 604 | pc = p->returnStack[p->returnDepth] - 1; |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 605 | break; |
| 606 | } |
| 607 | |
drh | 7f057c9 | 2005-06-24 03:53:06 +0000 | [diff] [blame] | 608 | /* Opcode: Halt P1 P2 P3 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 609 | ** |
drh | 0342b1f | 2005-09-01 03:07:44 +0000 | [diff] [blame] | 610 | ** Exit immediately. All open cursors, Fifos, etc are closed |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 611 | ** automatically. |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 612 | ** |
drh | 92f02c3 | 2004-09-02 14:57:08 +0000 | [diff] [blame] | 613 | ** P1 is the result code returned by sqlite3_exec(), sqlite3_reset(), |
| 614 | ** or sqlite3_finalize(). For a normal halt, this should be SQLITE_OK (0). |
| 615 | ** For errors, it can be some other value. If P1!=0 then P2 will determine |
| 616 | ** whether or not to rollback the current transaction. Do not rollback |
| 617 | ** if P2==OE_Fail. Do the rollback if P2==OE_Rollback. If P2==OE_Abort, |
| 618 | ** then back out all changes that have occurred during this execution of the |
drh | b798fa6 | 2002-09-03 19:43:23 +0000 | [diff] [blame] | 619 | ** VDBE, but do not rollback the transaction. |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 620 | ** |
drh | 7f057c9 | 2005-06-24 03:53:06 +0000 | [diff] [blame] | 621 | ** If P3 is not null then it is an error message string. |
| 622 | ** |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 623 | ** 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] | 624 | ** every program. So a jump past the last instruction of the program |
| 625 | ** is the same as executing Halt. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 626 | */ |
danielk1977 | 7a5147c | 2005-03-29 13:07:00 +0000 | [diff] [blame] | 627 | case OP_Halt: { /* no-push */ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 628 | p->pTos = pTos; |
drh | 92f02c3 | 2004-09-02 14:57:08 +0000 | [diff] [blame] | 629 | p->rc = pOp->p1; |
| 630 | p->pc = pc; |
| 631 | p->errorAction = pOp->p2; |
| 632 | if( pOp->p3 ){ |
drh | 17c4029 | 2004-07-21 02:53:29 +0000 | [diff] [blame] | 633 | sqlite3SetString(&p->zErrMsg, pOp->p3, (char*)0); |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 634 | } |
drh | 92f02c3 | 2004-09-02 14:57:08 +0000 | [diff] [blame] | 635 | rc = sqlite3VdbeHalt(p); |
danielk1977 | 01427a6 | 2005-01-11 13:02:33 +0000 | [diff] [blame] | 636 | assert( rc==SQLITE_BUSY || rc==SQLITE_OK ); |
drh | 92f02c3 | 2004-09-02 14:57:08 +0000 | [diff] [blame] | 637 | if( rc==SQLITE_BUSY ){ |
| 638 | p->rc = SQLITE_BUSY; |
| 639 | return SQLITE_BUSY; |
drh | 92f02c3 | 2004-09-02 14:57:08 +0000 | [diff] [blame] | 640 | } |
| 641 | return p->rc ? SQLITE_ERROR : SQLITE_DONE; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 642 | } |
drh | c61053b | 2000-06-04 12:58:36 +0000 | [diff] [blame] | 643 | |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 644 | /* Opcode: Integer P1 * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 645 | ** |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 646 | ** The 32-bit integer value P1 is pushed onto the stack. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 647 | */ |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 648 | case OP_Integer: { |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 649 | pTos++; |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 650 | pTos->flags = MEM_Int; |
| 651 | pTos->i = pOp->p1; |
| 652 | break; |
| 653 | } |
| 654 | |
| 655 | /* Opcode: Int64 * * P3 |
| 656 | ** |
| 657 | ** P3 is a string representation of an integer. Convert that integer |
| 658 | ** to a 64-bit value and push it onto the stack. |
| 659 | */ |
| 660 | case OP_Int64: { |
| 661 | pTos++; |
| 662 | assert( pOp->p3!=0 ); |
| 663 | pTos->flags = MEM_Str|MEM_Static|MEM_Term; |
| 664 | pTos->z = pOp->p3; |
| 665 | pTos->n = strlen(pTos->z); |
| 666 | pTos->enc = SQLITE_UTF8; |
| 667 | pTos->i = sqlite3VdbeIntValue(pTos); |
| 668 | pTos->flags |= MEM_Int; |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 669 | break; |
| 670 | } |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 671 | |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 672 | /* Opcode: Real * * P3 |
| 673 | ** |
| 674 | ** The string value P3 is converted to a real and pushed on to the stack. |
| 675 | */ |
danielk1977 | bc04f85 | 2005-03-29 08:26:13 +0000 | [diff] [blame] | 676 | case OP_Real: { /* same as TK_FLOAT, */ |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 677 | pTos++; |
| 678 | pTos->flags = MEM_Str|MEM_Static|MEM_Term; |
| 679 | pTos->z = pOp->p3; |
| 680 | pTos->n = strlen(pTos->z); |
danielk1977 | dc8453f | 2004-06-12 00:42:34 +0000 | [diff] [blame] | 681 | pTos->enc = SQLITE_UTF8; |
drh | 6a6124e | 2004-06-27 01:56:33 +0000 | [diff] [blame] | 682 | pTos->r = sqlite3VdbeRealValue(pTos); |
| 683 | pTos->flags |= MEM_Real; |
drh | 8079a0d | 2006-01-12 17:20:50 +0000 | [diff] [blame] | 684 | sqlite3VdbeChangeEncoding(pTos, encoding); |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 685 | break; |
| 686 | } |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 687 | |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 688 | /* Opcode: String8 * * P3 |
| 689 | ** |
danielk1977 | 819d7f4 | 2006-01-15 14:11:48 +0000 | [diff] [blame] | 690 | ** P3 points to a nul terminated UTF-8 string. This opcode is transformed |
danielk1977 | 0f69c1e | 2004-05-29 11:24:50 +0000 | [diff] [blame] | 691 | ** into an OP_String before it is executed for the first time. |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 692 | */ |
drh | f2bc013 | 2004-10-04 13:19:23 +0000 | [diff] [blame] | 693 | case OP_String8: { /* same as TK_STRING */ |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 694 | assert( pOp->p3!=0 ); |
drh | ed2df7f | 2005-11-16 04:34:32 +0000 | [diff] [blame] | 695 | pOp->opcode = OP_String; |
| 696 | pOp->p1 = strlen(pOp->p3); |
| 697 | |
| 698 | #ifndef SQLITE_OMIT_UTF16 |
drh | 8079a0d | 2006-01-12 17:20:50 +0000 | [diff] [blame] | 699 | if( encoding!=SQLITE_UTF8 ){ |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame] | 700 | pTos++; |
| 701 | sqlite3VdbeMemSetStr(pTos, pOp->p3, -1, SQLITE_UTF8, SQLITE_STATIC); |
drh | 8079a0d | 2006-01-12 17:20:50 +0000 | [diff] [blame] | 702 | if( SQLITE_OK!=sqlite3VdbeChangeEncoding(pTos, encoding) ) goto no_mem; |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame] | 703 | if( SQLITE_OK!=sqlite3VdbeMemDynamicify(pTos) ) goto no_mem; |
| 704 | pTos->flags &= ~(MEM_Dyn); |
| 705 | pTos->flags |= MEM_Static; |
danielk1977 | e004840 | 2004-06-15 16:51:01 +0000 | [diff] [blame] | 706 | if( pOp->p3type==P3_DYNAMIC ){ |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame] | 707 | sqliteFree(pOp->p3); |
danielk1977 | e004840 | 2004-06-15 16:51:01 +0000 | [diff] [blame] | 708 | } |
| 709 | pOp->p3type = P3_DYNAMIC; |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame] | 710 | pOp->p3 = pTos->z; |
danielk1977 | 819d7f4 | 2006-01-15 14:11:48 +0000 | [diff] [blame] | 711 | pOp->p1 = pTos->n; |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame] | 712 | break; |
danielk1977 | 0f69c1e | 2004-05-29 11:24:50 +0000 | [diff] [blame] | 713 | } |
danielk1977 | 93758c8 | 2005-01-21 08:13:14 +0000 | [diff] [blame] | 714 | #endif |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame] | 715 | /* Otherwise fall through to the next case, OP_String */ |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 716 | } |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 717 | |
drh | ed2df7f | 2005-11-16 04:34:32 +0000 | [diff] [blame] | 718 | /* Opcode: String P1 * P3 |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 719 | ** |
danielk1977 | 819d7f4 | 2006-01-15 14:11:48 +0000 | [diff] [blame] | 720 | ** The string value P3 of length P1 (bytes) is pushed onto the stack. |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 721 | */ |
| 722 | case OP_String: { |
| 723 | pTos++; |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 724 | assert( pOp->p3!=0 ); |
| 725 | pTos->flags = MEM_Str|MEM_Static|MEM_Term; |
| 726 | pTos->z = pOp->p3; |
drh | ed2df7f | 2005-11-16 04:34:32 +0000 | [diff] [blame] | 727 | pTos->n = pOp->p1; |
drh | 8079a0d | 2006-01-12 17:20:50 +0000 | [diff] [blame] | 728 | pTos->enc = encoding; |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 729 | break; |
| 730 | } |
| 731 | |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 732 | /* Opcode: Null * * * |
| 733 | ** |
| 734 | ** Push a NULL onto the stack. |
| 735 | */ |
| 736 | case OP_Null: { |
| 737 | pTos++; |
| 738 | pTos->flags = MEM_Null; |
drh | d1c301e | 2005-09-07 23:05:21 +0000 | [diff] [blame] | 739 | pTos->n = 0; |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 740 | break; |
| 741 | } |
| 742 | |
| 743 | |
drh | a71aa00 | 2004-11-03 13:59:04 +0000 | [diff] [blame] | 744 | #ifndef SQLITE_OMIT_BLOB_LITERAL |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 745 | /* Opcode: HexBlob * * P3 |
| 746 | ** |
danielk1977 | 72c952a | 2004-06-21 09:06:41 +0000 | [diff] [blame] | 747 | ** P3 is an UTF-8 SQL hex encoding of a blob. The blob is pushed onto the |
| 748 | ** vdbe stack. |
danielk1977 | 0f69c1e | 2004-05-29 11:24:50 +0000 | [diff] [blame] | 749 | ** |
danielk1977 | 72c952a | 2004-06-21 09:06:41 +0000 | [diff] [blame] | 750 | ** The first time this instruction executes, in transforms itself into a |
| 751 | ** 'Blob' opcode with a binary blob as P3. |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 752 | */ |
drh | f2bc013 | 2004-10-04 13:19:23 +0000 | [diff] [blame] | 753 | case OP_HexBlob: { /* same as TK_BLOB */ |
danielk1977 | 0f69c1e | 2004-05-29 11:24:50 +0000 | [diff] [blame] | 754 | pOp->opcode = OP_Blob; |
| 755 | pOp->p1 = strlen(pOp->p3)/2; |
| 756 | if( pOp->p1 ){ |
| 757 | char *zBlob = sqlite3HexToBlob(pOp->p3); |
| 758 | if( !zBlob ) goto no_mem; |
| 759 | if( pOp->p3type==P3_DYNAMIC ){ |
| 760 | sqliteFree(pOp->p3); |
| 761 | } |
| 762 | pOp->p3 = zBlob; |
| 763 | pOp->p3type = P3_DYNAMIC; |
| 764 | }else{ |
danielk1977 | e004840 | 2004-06-15 16:51:01 +0000 | [diff] [blame] | 765 | if( pOp->p3type==P3_DYNAMIC ){ |
| 766 | sqliteFree(pOp->p3); |
| 767 | } |
danielk1977 | 0f69c1e | 2004-05-29 11:24:50 +0000 | [diff] [blame] | 768 | pOp->p3type = P3_STATIC; |
| 769 | pOp->p3 = ""; |
| 770 | } |
| 771 | |
| 772 | /* Fall through to the next case, OP_Blob. */ |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 773 | } |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 774 | |
| 775 | /* Opcode: Blob P1 * P3 |
| 776 | ** |
| 777 | ** P3 points to a blob of data P1 bytes long. Push this |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 778 | ** value onto the stack. This instruction is not coded directly |
| 779 | ** by the compiler. Instead, the compiler layer specifies |
| 780 | ** an OP_HexBlob opcode, with the hex string representation of |
| 781 | ** the blob as P3. This opcode is transformed to an OP_Blob |
danielk1977 | 93758c8 | 2005-01-21 08:13:14 +0000 | [diff] [blame] | 782 | ** the first time it is executed. |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 783 | */ |
| 784 | case OP_Blob: { |
| 785 | pTos++; |
| 786 | sqlite3VdbeMemSetStr(pTos, pOp->p3, pOp->p1, 0, 0); |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 787 | break; |
| 788 | } |
danielk1977 | 93758c8 | 2005-01-21 08:13:14 +0000 | [diff] [blame] | 789 | #endif /* SQLITE_OMIT_BLOB_LITERAL */ |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 790 | |
drh | 5045789 | 2003-09-06 01:10:47 +0000 | [diff] [blame] | 791 | /* Opcode: Variable P1 * * |
| 792 | ** |
| 793 | ** Push the value of variable P1 onto the stack. A variable is |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 794 | ** an unknown in the original SQL string as handed to sqlite3_compile(). |
drh | 7c972de | 2003-09-06 22:18:07 +0000 | [diff] [blame] | 795 | ** Any occurance of the '?' character in the original SQL is considered |
| 796 | ** a variable. Variables in the SQL string are number from left to |
| 797 | ** right beginning with 1. The values of variables are set using the |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 798 | ** sqlite3_bind() API. |
drh | 5045789 | 2003-09-06 01:10:47 +0000 | [diff] [blame] | 799 | */ |
| 800 | case OP_Variable: { |
drh | 7c972de | 2003-09-06 22:18:07 +0000 | [diff] [blame] | 801 | int j = pOp->p1 - 1; |
danielk1977 | 295ba55 | 2004-05-19 10:34:51 +0000 | [diff] [blame] | 802 | assert( j>=0 && j<p->nVar ); |
| 803 | |
danielk1977 | 295ba55 | 2004-05-19 10:34:51 +0000 | [diff] [blame] | 804 | pTos++; |
drh | febe106 | 2004-08-28 18:17:48 +0000 | [diff] [blame] | 805 | sqlite3VdbeMemShallowCopy(pTos, &p->aVar[j], MEM_Static); |
danielk1977 | 93d4675 | 2004-05-23 13:30:58 +0000 | [diff] [blame] | 806 | break; |
| 807 | } |
danielk1977 | 295ba55 | 2004-05-19 10:34:51 +0000 | [diff] [blame] | 808 | |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 809 | /* Opcode: Pop P1 * * |
| 810 | ** |
| 811 | ** P1 elements are popped off of the top of stack and discarded. |
| 812 | */ |
danielk1977 | 7a5147c | 2005-03-29 13:07:00 +0000 | [diff] [blame] | 813 | case OP_Pop: { /* no-push */ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 814 | assert( pOp->p1>=0 ); |
| 815 | popStack(&pTos, pOp->p1); |
| 816 | assert( pTos>=&p->aStack[-1] ); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 817 | break; |
| 818 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 819 | |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 820 | /* Opcode: Dup P1 P2 * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 821 | ** |
| 822 | ** A copy of the P1-th element of the stack |
| 823 | ** is made and pushed onto the top of the stack. |
| 824 | ** The top of the stack is element 0. So the |
| 825 | ** instruction "Dup 0 0 0" will make a copy of the |
| 826 | ** top of the stack. |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 827 | ** |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 828 | ** If the content of the P1-th element is a dynamically |
| 829 | ** allocated string, then a new copy of that string |
| 830 | ** is made if P2==0. If P2!=0, then just a pointer |
| 831 | ** to the string is copied. |
| 832 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 833 | ** Also see the Pull instruction. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 834 | */ |
| 835 | case OP_Dup: { |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 836 | Mem *pFrom = &pTos[-pOp->p1]; |
| 837 | assert( pFrom<=pTos && pFrom>=p->aStack ); |
| 838 | pTos++; |
drh | febe106 | 2004-08-28 18:17:48 +0000 | [diff] [blame] | 839 | sqlite3VdbeMemShallowCopy(pTos, pFrom, MEM_Ephem); |
| 840 | if( pOp->p2 ){ |
| 841 | Deephemeralize(pTos); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 842 | } |
| 843 | break; |
| 844 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 845 | |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 846 | /* Opcode: Pull P1 * * |
| 847 | ** |
| 848 | ** The P1-th element is removed from its current location on |
| 849 | ** the stack and pushed back on top of the stack. The |
| 850 | ** top of the stack is element 0, so "Pull 0 0 0" is |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 851 | ** a no-op. "Pull 1 0 0" swaps the top two elements of |
| 852 | ** the stack. |
| 853 | ** |
| 854 | ** See also the Dup instruction. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 855 | */ |
danielk1977 | 7a5147c | 2005-03-29 13:07:00 +0000 | [diff] [blame] | 856 | case OP_Pull: { /* no-push */ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 857 | Mem *pFrom = &pTos[-pOp->p1]; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 858 | int i; |
drh | 00706be | 2004-01-30 14:49:16 +0000 | [diff] [blame] | 859 | Mem ts; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 860 | |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 861 | ts = *pFrom; |
| 862 | Deephemeralize(pTos); |
| 863 | for(i=0; i<pOp->p1; i++, pFrom++){ |
| 864 | Deephemeralize(&pFrom[1]); |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 865 | assert( (pFrom->flags & MEM_Ephem)==0 ); |
danielk1977 | 18f4189 | 2004-05-22 07:27:46 +0000 | [diff] [blame] | 866 | *pFrom = pFrom[1]; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 867 | if( pFrom->flags & MEM_Short ){ |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 868 | assert( pFrom->flags & (MEM_Str|MEM_Blob) ); |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 869 | assert( pFrom->z==pFrom[1].zShort ); |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 870 | pFrom->z = pFrom->zShort; |
drh | 9bbca4c | 2001-11-06 04:00:18 +0000 | [diff] [blame] | 871 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 872 | } |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 873 | *pTos = ts; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 874 | if( pTos->flags & MEM_Short ){ |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 875 | assert( pTos->flags & (MEM_Str|MEM_Blob) ); |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 876 | assert( pTos->z==pTos[-pOp->p1].zShort ); |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 877 | pTos->z = pTos->zShort; |
drh | 9bbca4c | 2001-11-06 04:00:18 +0000 | [diff] [blame] | 878 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 879 | break; |
| 880 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 881 | |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 882 | /* Opcode: Push P1 * * |
| 883 | ** |
| 884 | ** Overwrite the value of the P1-th element down on the |
| 885 | ** stack (P1==0 is the top of the stack) with the value |
drh | ac82fcf | 2002-09-08 17:23:41 +0000 | [diff] [blame] | 886 | ** of the top of the stack. Then pop the top of the stack. |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 887 | */ |
danielk1977 | 7a5147c | 2005-03-29 13:07:00 +0000 | [diff] [blame] | 888 | case OP_Push: { /* no-push */ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 889 | Mem *pTo = &pTos[-pOp->p1]; |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 890 | |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 891 | assert( pTo>=p->aStack ); |
drh | febe106 | 2004-08-28 18:17:48 +0000 | [diff] [blame] | 892 | sqlite3VdbeMemMove(pTo, pTos); |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 893 | pTos--; |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 894 | break; |
| 895 | } |
| 896 | |
drh | df199a2 | 2002-06-14 22:38:41 +0000 | [diff] [blame] | 897 | /* Opcode: Callback P1 * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 898 | ** |
drh | 403110c | 2006-01-07 18:10:32 +0000 | [diff] [blame] | 899 | ** The top P1 values on the stack represent a single result row from |
| 900 | ** a query. This opcode causes the sqlite3_step() call to terminate |
| 901 | ** with an SQLITE_ROW return code and it sets up the sqlite3_stmt |
| 902 | ** structure to provide access to the top P1 values as the result |
| 903 | ** row. When the sqlite3_step() function is run again, the top P1 |
| 904 | ** values will be automatically popped from the stack before the next |
| 905 | ** instruction executes. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 906 | */ |
danielk1977 | 7a5147c | 2005-03-29 13:07:00 +0000 | [diff] [blame] | 907 | case OP_Callback: { /* no-push */ |
drh | 403110c | 2006-01-07 18:10:32 +0000 | [diff] [blame] | 908 | Mem *pMem; |
| 909 | Mem *pFirstColumn; |
drh | d650275 | 2004-02-16 03:44:01 +0000 | [diff] [blame] | 910 | assert( p->nResColumn==pOp->p1 ); |
danielk1977 | 8a6b541 | 2004-05-24 07:04:25 +0000 | [diff] [blame] | 911 | |
drh | 403110c | 2006-01-07 18:10:32 +0000 | [diff] [blame] | 912 | /* Data in the pager might be moved or changed out from under us |
| 913 | ** in between the return from this sqlite3_step() call and the |
| 914 | ** next call to sqlite3_step(). So deephermeralize everything on |
| 915 | ** the stack. Note that ephemeral data is never stored in memory |
| 916 | ** cells so we do not have to worry about them. |
| 917 | */ |
| 918 | pFirstColumn = &pTos[0-pOp->p1]; |
| 919 | for(pMem = p->aStack; pMem<pFirstColumn; pMem++){ |
| 920 | Deephemeralize(pMem); |
danielk1977 | 8a6b541 | 2004-05-24 07:04:25 +0000 | [diff] [blame] | 921 | } |
| 922 | |
drh | 76873ab | 2006-01-07 18:48:26 +0000 | [diff] [blame] | 923 | /* Invalidate all ephemeral cursor row caches */ |
| 924 | p->cacheCtr = (p->cacheCtr + 2)|1; |
| 925 | |
drh | 403110c | 2006-01-07 18:10:32 +0000 | [diff] [blame] | 926 | /* Make sure the results of the current row are \000 terminated |
| 927 | ** and have an assigned type. The results are deephemeralized as |
| 928 | ** as side effect. |
| 929 | */ |
| 930 | for(; pMem<=pTos; pMem++ ){ |
| 931 | sqlite3VdbeMemNulTerminate(pMem); |
drh | 8079a0d | 2006-01-12 17:20:50 +0000 | [diff] [blame] | 932 | storeTypeInfo(pMem, encoding); |
drh | 403110c | 2006-01-07 18:10:32 +0000 | [diff] [blame] | 933 | } |
| 934 | |
| 935 | /* Set up the statement structure so that it will pop the current |
| 936 | ** results from the stack when the statement returns. |
| 937 | */ |
danielk1977 | 8a6b541 | 2004-05-24 07:04:25 +0000 | [diff] [blame] | 938 | p->resOnStack = 1; |
| 939 | p->nCallback++; |
drh | 826fb5a | 2004-02-14 23:59:57 +0000 | [diff] [blame] | 940 | p->popStack = pOp->p1; |
| 941 | p->pc = pc + 1; |
| 942 | p->pTos = pTos; |
| 943 | return SQLITE_ROW; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 944 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 945 | |
drh | 4230e2c | 2004-06-29 13:54:50 +0000 | [diff] [blame] | 946 | /* Opcode: Concat P1 P2 * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 947 | ** |
drh | 855eb1c | 2004-08-31 13:45:11 +0000 | [diff] [blame] | 948 | ** Look at the first P1+2 elements of the stack. Append them all |
| 949 | ** together with the lowest element first. The original P1+2 elements |
drh | a9f9d1c | 2002-06-29 02:20:08 +0000 | [diff] [blame] | 950 | ** are popped from the stack if P2==0 and retained if P2==1. If |
| 951 | ** any element of the stack is NULL, then the result is NULL. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 952 | ** |
drh | 4230e2c | 2004-06-29 13:54:50 +0000 | [diff] [blame] | 953 | ** When P1==1, this routine makes a copy of the top stack element |
| 954 | ** into memory obtained from sqliteMalloc(). |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 955 | */ |
drh | f2bc013 | 2004-10-04 13:19:23 +0000 | [diff] [blame] | 956 | case OP_Concat: { /* same as TK_CONCAT */ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 957 | char *zNew; |
| 958 | int nByte; |
| 959 | int nField; |
| 960 | int i, j; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 961 | Mem *pTerm; |
danielk1977 | 8a6b541 | 2004-05-24 07:04:25 +0000 | [diff] [blame] | 962 | |
| 963 | /* Loop through the stack elements to see how long the result will be. */ |
drh | 855eb1c | 2004-08-31 13:45:11 +0000 | [diff] [blame] | 964 | nField = pOp->p1 + 2; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 965 | pTerm = &pTos[1-nField]; |
drh | 4230e2c | 2004-06-29 13:54:50 +0000 | [diff] [blame] | 966 | nByte = 0; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 967 | for(i=0; i<nField; i++, pTerm++){ |
danielk1977 | 8a6b541 | 2004-05-24 07:04:25 +0000 | [diff] [blame] | 968 | assert( pOp->p2==0 || (pTerm->flags&MEM_Str) ); |
| 969 | if( pTerm->flags&MEM_Null ){ |
drh | a9f9d1c | 2002-06-29 02:20:08 +0000 | [diff] [blame] | 970 | nByte = -1; |
| 971 | break; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 972 | } |
drh | 8079a0d | 2006-01-12 17:20:50 +0000 | [diff] [blame] | 973 | Stringify(pTerm, encoding); |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 974 | nByte += pTerm->n; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 975 | } |
danielk1977 | 8a6b541 | 2004-05-24 07:04:25 +0000 | [diff] [blame] | 976 | |
drh | a9f9d1c | 2002-06-29 02:20:08 +0000 | [diff] [blame] | 977 | if( nByte<0 ){ |
danielk1977 | 8a6b541 | 2004-05-24 07:04:25 +0000 | [diff] [blame] | 978 | /* If nByte is less than zero, then there is a NULL value on the stack. |
| 979 | ** In this case just pop the values off the stack (if required) and |
| 980 | ** push on a NULL. |
| 981 | */ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 982 | if( pOp->p2==0 ){ |
| 983 | popStack(&pTos, nField); |
| 984 | } |
| 985 | pTos++; |
| 986 | pTos->flags = MEM_Null; |
danielk1977 | 8a6b541 | 2004-05-24 07:04:25 +0000 | [diff] [blame] | 987 | }else{ |
| 988 | /* Otherwise malloc() space for the result and concatenate all the |
| 989 | ** stack values. |
| 990 | */ |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 991 | zNew = sqliteMallocRaw( nByte+2 ); |
danielk1977 | 8a6b541 | 2004-05-24 07:04:25 +0000 | [diff] [blame] | 992 | if( zNew==0 ) goto no_mem; |
| 993 | j = 0; |
| 994 | pTerm = &pTos[1-nField]; |
| 995 | for(i=j=0; i<nField; i++, pTerm++){ |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 996 | int n = pTerm->n; |
drh | edef8fc | 2005-06-22 02:36:37 +0000 | [diff] [blame] | 997 | assert( pTerm->flags & (MEM_Str|MEM_Blob) ); |
danielk1977 | 8a6b541 | 2004-05-24 07:04:25 +0000 | [diff] [blame] | 998 | memcpy(&zNew[j], pTerm->z, n); |
| 999 | j += n; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1000 | } |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 1001 | zNew[j] = 0; |
| 1002 | zNew[j+1] = 0; |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 1003 | assert( j==nByte ); |
danielk1977 | 8a6b541 | 2004-05-24 07:04:25 +0000 | [diff] [blame] | 1004 | |
| 1005 | if( pOp->p2==0 ){ |
| 1006 | popStack(&pTos, nField); |
| 1007 | } |
| 1008 | pTos++; |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 1009 | pTos->n = j; |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 1010 | pTos->flags = MEM_Str|MEM_Dyn|MEM_Term; |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 1011 | pTos->xDel = 0; |
drh | 8079a0d | 2006-01-12 17:20:50 +0000 | [diff] [blame] | 1012 | pTos->enc = encoding; |
danielk1977 | 8a6b541 | 2004-05-24 07:04:25 +0000 | [diff] [blame] | 1013 | pTos->z = zNew; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1014 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1015 | break; |
| 1016 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1017 | |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1018 | /* Opcode: Add * * * |
| 1019 | ** |
| 1020 | ** Pop the top two elements from the stack, add them together, |
| 1021 | ** and push the result back onto the stack. If either element |
| 1022 | ** is a string then it is converted to a double using the atof() |
| 1023 | ** function before the addition. |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1024 | ** If either operand is NULL, the result is NULL. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1025 | */ |
| 1026 | /* Opcode: Multiply * * * |
| 1027 | ** |
| 1028 | ** Pop the top two elements from the stack, multiply them together, |
| 1029 | ** and push the result back onto the stack. If either element |
| 1030 | ** is a string then it is converted to a double using the atof() |
| 1031 | ** function before the multiplication. |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1032 | ** If either operand is NULL, the result is NULL. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1033 | */ |
| 1034 | /* Opcode: Subtract * * * |
| 1035 | ** |
| 1036 | ** Pop the top two elements from the stack, subtract the |
| 1037 | ** first (what was on top of the stack) from the second (the |
| 1038 | ** next on stack) |
| 1039 | ** and push the result back onto the stack. If either element |
| 1040 | ** is a string then it is converted to a double using the atof() |
| 1041 | ** function before the subtraction. |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1042 | ** If either operand is NULL, the result is NULL. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1043 | */ |
| 1044 | /* Opcode: Divide * * * |
| 1045 | ** |
| 1046 | ** Pop the top two elements from the stack, divide the |
| 1047 | ** first (what was on top of the stack) from the second (the |
| 1048 | ** next on stack) |
| 1049 | ** and push the result back onto the stack. If either element |
| 1050 | ** is a string then it is converted to a double using the atof() |
| 1051 | ** function before the division. Division by zero returns NULL. |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1052 | ** If either operand is NULL, the result is NULL. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1053 | */ |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1054 | /* Opcode: Remainder * * * |
| 1055 | ** |
| 1056 | ** Pop the top two elements from the stack, divide the |
| 1057 | ** first (what was on top of the stack) from the second (the |
| 1058 | ** next on stack) |
| 1059 | ** and push the remainder after division onto the stack. If either element |
| 1060 | ** is a string then it is converted to a double using the atof() |
| 1061 | ** function before the division. Division by zero returns NULL. |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1062 | ** If either operand is NULL, the result is NULL. |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1063 | */ |
danielk1977 | 7a5147c | 2005-03-29 13:07:00 +0000 | [diff] [blame] | 1064 | case OP_Add: /* same as TK_PLUS, no-push */ |
| 1065 | case OP_Subtract: /* same as TK_MINUS, no-push */ |
| 1066 | case OP_Multiply: /* same as TK_STAR, no-push */ |
| 1067 | case OP_Divide: /* same as TK_SLASH, no-push */ |
| 1068 | case OP_Remainder: { /* same as TK_REM, no-push */ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1069 | Mem *pNos = &pTos[-1]; |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1070 | int flags; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1071 | assert( pNos>=p->aStack ); |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1072 | flags = pTos->flags | pNos->flags; |
| 1073 | if( (flags & MEM_Null)!=0 ){ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1074 | Release(pTos); |
| 1075 | pTos--; |
| 1076 | Release(pTos); |
| 1077 | pTos->flags = MEM_Null; |
| 1078 | }else if( (pTos->flags & pNos->flags & MEM_Int)==MEM_Int ){ |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1079 | i64 a, b; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1080 | a = pTos->i; |
| 1081 | b = pNos->i; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1082 | switch( pOp->opcode ){ |
| 1083 | case OP_Add: b += a; break; |
| 1084 | case OP_Subtract: b -= a; break; |
| 1085 | case OP_Multiply: b *= a; break; |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1086 | case OP_Divide: { |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1087 | if( a==0 ) goto divide_by_zero; |
| 1088 | b /= a; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1089 | break; |
| 1090 | } |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1091 | default: { |
| 1092 | if( a==0 ) goto divide_by_zero; |
| 1093 | b %= a; |
| 1094 | break; |
| 1095 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1096 | } |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1097 | Release(pTos); |
| 1098 | pTos--; |
| 1099 | Release(pTos); |
| 1100 | pTos->i = b; |
| 1101 | pTos->flags = MEM_Int; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1102 | }else{ |
| 1103 | double a, b; |
drh | 6a6124e | 2004-06-27 01:56:33 +0000 | [diff] [blame] | 1104 | a = sqlite3VdbeRealValue(pTos); |
| 1105 | b = sqlite3VdbeRealValue(pNos); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1106 | switch( pOp->opcode ){ |
| 1107 | case OP_Add: b += a; break; |
| 1108 | case OP_Subtract: b -= a; break; |
| 1109 | case OP_Multiply: b *= a; break; |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1110 | case OP_Divide: { |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1111 | if( a==0.0 ) goto divide_by_zero; |
| 1112 | b /= a; |
| 1113 | break; |
| 1114 | } |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1115 | default: { |
drh | 1ab4300 | 2002-01-14 09:28:19 +0000 | [diff] [blame] | 1116 | int ia = (int)a; |
| 1117 | int ib = (int)b; |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1118 | if( ia==0.0 ) goto divide_by_zero; |
| 1119 | b = ib % ia; |
| 1120 | break; |
| 1121 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1122 | } |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1123 | Release(pTos); |
| 1124 | pTos--; |
| 1125 | Release(pTos); |
| 1126 | pTos->r = b; |
| 1127 | pTos->flags = MEM_Real; |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1128 | if( (flags & MEM_Real)==0 ){ |
| 1129 | sqlite3VdbeIntegerAffinity(pTos); |
| 1130 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1131 | } |
| 1132 | break; |
| 1133 | |
| 1134 | divide_by_zero: |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1135 | Release(pTos); |
| 1136 | pTos--; |
| 1137 | Release(pTos); |
| 1138 | pTos->flags = MEM_Null; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1139 | break; |
| 1140 | } |
| 1141 | |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 1142 | /* Opcode: CollSeq * * P3 |
| 1143 | ** |
| 1144 | ** P3 is a pointer to a CollSeq struct. If the next call to a user function |
| 1145 | ** or aggregate calls sqlite3GetFuncCollSeq(), this collation sequence will |
| 1146 | ** be returned. This is used by the built-in min(), max() and nullif() |
drh | e6f85e7 | 2004-12-25 01:03:13 +0000 | [diff] [blame] | 1147 | ** functions. |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 1148 | ** |
| 1149 | ** The interface used by the implementation of the aforementioned functions |
| 1150 | ** to retrieve the collation sequence set by this opcode is not available |
| 1151 | ** publicly, only to user functions defined in func.c. |
| 1152 | */ |
danielk1977 | 7a5147c | 2005-03-29 13:07:00 +0000 | [diff] [blame] | 1153 | case OP_CollSeq: { /* no-push */ |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 1154 | assert( pOp->p3type==P3_COLLSEQ ); |
| 1155 | break; |
| 1156 | } |
| 1157 | |
danielk1977 | 682f68b | 2004-06-05 10:22:17 +0000 | [diff] [blame] | 1158 | /* Opcode: Function P1 P2 P3 |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 1159 | ** |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 1160 | ** Invoke a user function (P3 is a pointer to a Function structure that |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 1161 | ** defines the function) with P2 arguments taken from the stack. Pop all |
danielk1977 | 682f68b | 2004-06-05 10:22:17 +0000 | [diff] [blame] | 1162 | ** arguments from the stack and push back the result. |
| 1163 | ** |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 1164 | ** P1 is a 32-bit bitmask indicating whether or not each argument to the |
danielk1977 | 682f68b | 2004-06-05 10:22:17 +0000 | [diff] [blame] | 1165 | ** function was determined to be constant at compile time. If the first |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 1166 | ** argument was constant then bit 0 of P1 is set. This is used to determine |
danielk1977 | 682f68b | 2004-06-05 10:22:17 +0000 | [diff] [blame] | 1167 | ** whether meta data associated with a user function argument using the |
| 1168 | ** sqlite3_set_auxdata() API may be safely retained until the next |
| 1169 | ** invocation of this opcode. |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 1170 | ** |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 1171 | ** See also: AggStep and AggFinal |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 1172 | */ |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 1173 | case OP_Function: { |
danielk1977 | 51ad0ec | 2004-05-24 12:39:02 +0000 | [diff] [blame] | 1174 | int i; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1175 | Mem *pArg; |
danielk1977 | 22322fd | 2004-05-25 23:35:17 +0000 | [diff] [blame] | 1176 | sqlite3_context ctx; |
danielk1977 | 51ad0ec | 2004-05-24 12:39:02 +0000 | [diff] [blame] | 1177 | sqlite3_value **apVal; |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 1178 | int n = pOp->p2; |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 1179 | |
danielk1977 | 6ddcca5 | 2004-05-24 23:48:25 +0000 | [diff] [blame] | 1180 | apVal = p->apArg; |
danielk1977 | 51ad0ec | 2004-05-24 12:39:02 +0000 | [diff] [blame] | 1181 | assert( apVal || n==0 ); |
| 1182 | |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1183 | pArg = &pTos[1-n]; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1184 | for(i=0; i<n; i++, pArg++){ |
danielk1977 | 51ad0ec | 2004-05-24 12:39:02 +0000 | [diff] [blame] | 1185 | apVal[i] = pArg; |
drh | 8079a0d | 2006-01-12 17:20:50 +0000 | [diff] [blame] | 1186 | storeTypeInfo(pArg, encoding); |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 1187 | } |
danielk1977 | 51ad0ec | 2004-05-24 12:39:02 +0000 | [diff] [blame] | 1188 | |
danielk1977 | 682f68b | 2004-06-05 10:22:17 +0000 | [diff] [blame] | 1189 | assert( pOp->p3type==P3_FUNCDEF || pOp->p3type==P3_VDBEFUNC ); |
| 1190 | if( pOp->p3type==P3_FUNCDEF ){ |
| 1191 | ctx.pFunc = (FuncDef*)pOp->p3; |
| 1192 | ctx.pVdbeFunc = 0; |
| 1193 | }else{ |
| 1194 | ctx.pVdbeFunc = (VdbeFunc*)pOp->p3; |
| 1195 | ctx.pFunc = ctx.pVdbeFunc->pFunc; |
| 1196 | } |
| 1197 | |
drh | 00706be | 2004-01-30 14:49:16 +0000 | [diff] [blame] | 1198 | ctx.s.flags = MEM_Null; |
| 1199 | ctx.s.z = 0; |
drh | 22276bd | 2004-06-22 22:54:22 +0000 | [diff] [blame] | 1200 | ctx.s.xDel = 0; |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 1201 | ctx.isError = 0; |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 1202 | if( ctx.pFunc->needCollSeq ){ |
| 1203 | assert( pOp>p->aOp ); |
| 1204 | assert( pOp[-1].p3type==P3_COLLSEQ ); |
| 1205 | assert( pOp[-1].opcode==OP_CollSeq ); |
| 1206 | ctx.pColl = (CollSeq *)pOp[-1].p3; |
| 1207 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1208 | if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse; |
danielk1977 | 51ad0ec | 2004-05-24 12:39:02 +0000 | [diff] [blame] | 1209 | (*ctx.pFunc->xFunc)(&ctx, n, apVal); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1210 | if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse; |
danielk1977 | 9e12800 | 2006-01-18 16:51:35 +0000 | [diff] [blame] | 1211 | if( sqlite3MallocFailed() ) goto no_mem; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1212 | popStack(&pTos, n); |
danielk1977 | 7e18c25 | 2004-05-25 11:47:24 +0000 | [diff] [blame] | 1213 | |
danielk1977 | 682f68b | 2004-06-05 10:22:17 +0000 | [diff] [blame] | 1214 | /* If any auxilary data functions have been called by this user function, |
| 1215 | ** immediately call the destructor for any non-static values. |
| 1216 | */ |
| 1217 | if( ctx.pVdbeFunc ){ |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 1218 | sqlite3VdbeDeleteAuxData(ctx.pVdbeFunc, pOp->p1); |
danielk1977 | 682f68b | 2004-06-05 10:22:17 +0000 | [diff] [blame] | 1219 | pOp->p3 = (char *)ctx.pVdbeFunc; |
| 1220 | pOp->p3type = P3_VDBEFUNC; |
| 1221 | } |
| 1222 | |
drh | 90669c1 | 2006-01-20 15:45:36 +0000 | [diff] [blame] | 1223 | /* If the function returned an error, throw an exception */ |
| 1224 | if( ctx.isError ){ |
| 1225 | sqlite3SetString(&p->zErrMsg, sqlite3_value_text(&ctx.s), (char*)0); |
| 1226 | rc = SQLITE_ERROR; |
| 1227 | } |
| 1228 | |
danielk1977 | 7e18c25 | 2004-05-25 11:47:24 +0000 | [diff] [blame] | 1229 | /* Copy the result of the function to the top of the stack */ |
drh | 8079a0d | 2006-01-12 17:20:50 +0000 | [diff] [blame] | 1230 | sqlite3VdbeChangeEncoding(&ctx.s, encoding); |
drh | febe106 | 2004-08-28 18:17:48 +0000 | [diff] [blame] | 1231 | pTos++; |
| 1232 | pTos->flags = 0; |
| 1233 | sqlite3VdbeMemMove(pTos, &ctx.s); |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 1234 | break; |
| 1235 | } |
| 1236 | |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1237 | /* Opcode: BitAnd * * * |
| 1238 | ** |
| 1239 | ** Pop the top two elements from the stack. Convert both elements |
| 1240 | ** to integers. Push back onto the stack the bit-wise AND of the |
| 1241 | ** two elements. |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1242 | ** If either operand is NULL, the result is NULL. |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1243 | */ |
| 1244 | /* Opcode: BitOr * * * |
| 1245 | ** |
| 1246 | ** Pop the top two elements from the stack. Convert both elements |
| 1247 | ** to integers. Push back onto the stack the bit-wise OR of the |
| 1248 | ** two elements. |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1249 | ** If either operand is NULL, the result is NULL. |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1250 | */ |
| 1251 | /* Opcode: ShiftLeft * * * |
| 1252 | ** |
| 1253 | ** Pop the top two elements from the stack. Convert both elements |
drh | 17c4029 | 2004-07-21 02:53:29 +0000 | [diff] [blame] | 1254 | ** to integers. Push back onto the stack the second element shifted |
| 1255 | ** left by N bits where N is the top element on the stack. |
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: ShiftRight * * * |
| 1259 | ** |
| 1260 | ** Pop the top two elements from the stack. Convert both elements |
drh | 17c4029 | 2004-07-21 02:53:29 +0000 | [diff] [blame] | 1261 | ** to integers. Push back onto the stack the second element shifted |
| 1262 | ** right by N bits where N is the top element on the stack. |
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 | */ |
danielk1977 | 7a5147c | 2005-03-29 13:07:00 +0000 | [diff] [blame] | 1265 | case OP_BitAnd: /* same as TK_BITAND, no-push */ |
| 1266 | case OP_BitOr: /* same as TK_BITOR, no-push */ |
| 1267 | case OP_ShiftLeft: /* same as TK_LSHIFT, no-push */ |
| 1268 | case OP_ShiftRight: { /* same as TK_RSHIFT, no-push */ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1269 | Mem *pNos = &pTos[-1]; |
drh | b127612 | 2005-10-29 15:48:30 +0000 | [diff] [blame] | 1270 | i64 a, b; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1271 | |
| 1272 | assert( pNos>=p->aStack ); |
| 1273 | if( (pTos->flags | pNos->flags) & MEM_Null ){ |
| 1274 | popStack(&pTos, 2); |
| 1275 | pTos++; |
| 1276 | pTos->flags = MEM_Null; |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1277 | break; |
| 1278 | } |
drh | 17c4029 | 2004-07-21 02:53:29 +0000 | [diff] [blame] | 1279 | a = sqlite3VdbeIntValue(pNos); |
| 1280 | b = sqlite3VdbeIntValue(pTos); |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1281 | switch( pOp->opcode ){ |
| 1282 | case OP_BitAnd: a &= b; break; |
| 1283 | case OP_BitOr: a |= b; break; |
| 1284 | case OP_ShiftLeft: a <<= b; break; |
| 1285 | case OP_ShiftRight: a >>= b; break; |
| 1286 | default: /* CANT HAPPEN */ break; |
| 1287 | } |
danielk1977 | 8a6b541 | 2004-05-24 07:04:25 +0000 | [diff] [blame] | 1288 | Release(pTos); |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1289 | pTos--; |
drh | 79f14b7 | 2004-03-03 01:51:24 +0000 | [diff] [blame] | 1290 | Release(pTos); |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1291 | pTos->i = a; |
drh | 79f14b7 | 2004-03-03 01:51:24 +0000 | [diff] [blame] | 1292 | pTos->flags = MEM_Int; |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1293 | break; |
| 1294 | } |
| 1295 | |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1296 | /* Opcode: AddImm P1 * * |
| 1297 | ** |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 1298 | ** Add the value P1 to whatever is on top of the stack. The result |
| 1299 | ** is always an integer. |
| 1300 | ** |
| 1301 | ** 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] | 1302 | */ |
danielk1977 | 7a5147c | 2005-03-29 13:07:00 +0000 | [diff] [blame] | 1303 | case OP_AddImm: { /* no-push */ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1304 | assert( pTos>=p->aStack ); |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1305 | sqlite3VdbeMemIntegerify(pTos); |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1306 | pTos->i += pOp->p1; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1307 | break; |
| 1308 | } |
| 1309 | |
drh | 751f412 | 2004-01-14 21:59:22 +0000 | [diff] [blame] | 1310 | /* Opcode: ForceInt P1 P2 * |
drh | 1dd59e0 | 2003-07-06 17:22:25 +0000 | [diff] [blame] | 1311 | ** |
drh | 751f412 | 2004-01-14 21:59:22 +0000 | [diff] [blame] | 1312 | ** Convert the top of the stack into an integer. If the current top of |
| 1313 | ** the stack is not numeric (meaning that is is a NULL or a string that |
| 1314 | ** does not look like an integer or floating point number) then pop the |
| 1315 | ** stack and jump to P2. If the top of the stack is numeric then |
| 1316 | ** convert it into the least integer that is greater than or equal to its |
| 1317 | ** current value if P1==0, or to the least integer that is strictly |
| 1318 | ** greater than its current value if P1==1. |
drh | 1dd59e0 | 2003-07-06 17:22:25 +0000 | [diff] [blame] | 1319 | */ |
danielk1977 | 7a5147c | 2005-03-29 13:07:00 +0000 | [diff] [blame] | 1320 | case OP_ForceInt: { /* no-push */ |
drh | f4f8fd5 | 2005-03-31 18:40:04 +0000 | [diff] [blame] | 1321 | i64 v; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1322 | assert( pTos>=p->aStack ); |
drh | 8079a0d | 2006-01-12 17:20:50 +0000 | [diff] [blame] | 1323 | applyAffinity(pTos, SQLITE_AFF_NUMERIC, encoding); |
drh | 17c4029 | 2004-07-21 02:53:29 +0000 | [diff] [blame] | 1324 | if( (pTos->flags & (MEM_Int|MEM_Real))==0 ){ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1325 | Release(pTos); |
| 1326 | pTos--; |
drh | 1dd59e0 | 2003-07-06 17:22:25 +0000 | [diff] [blame] | 1327 | pc = pOp->p2 - 1; |
drh | 751f412 | 2004-01-14 21:59:22 +0000 | [diff] [blame] | 1328 | break; |
drh | 1dd59e0 | 2003-07-06 17:22:25 +0000 | [diff] [blame] | 1329 | } |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1330 | if( pTos->flags & MEM_Int ){ |
| 1331 | v = pTos->i + (pOp->p1!=0); |
drh | 751f412 | 2004-01-14 21:59:22 +0000 | [diff] [blame] | 1332 | }else{ |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1333 | /* FIX ME: should this not be assert( pTos->flags & MEM_Real ) ??? */ |
| 1334 | sqlite3VdbeMemRealify(pTos); |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1335 | v = (int)pTos->r; |
| 1336 | if( pTos->r>(double)v ) v++; |
| 1337 | if( pOp->p1 && pTos->r==(double)v ) v++; |
drh | 751f412 | 2004-01-14 21:59:22 +0000 | [diff] [blame] | 1338 | } |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1339 | Release(pTos); |
| 1340 | pTos->i = v; |
| 1341 | pTos->flags = MEM_Int; |
drh | 1dd59e0 | 2003-07-06 17:22:25 +0000 | [diff] [blame] | 1342 | break; |
| 1343 | } |
| 1344 | |
drh | f1351b6 | 2002-07-31 19:50:26 +0000 | [diff] [blame] | 1345 | /* Opcode: MustBeInt P1 P2 * |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 1346 | ** |
| 1347 | ** 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] | 1348 | ** stack is not an integer and cannot be converted into an integer |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 1349 | ** with out data loss, then jump immediately to P2, or if P2==0 |
| 1350 | ** raise an SQLITE_MISMATCH exception. |
drh | f1351b6 | 2002-07-31 19:50:26 +0000 | [diff] [blame] | 1351 | ** |
| 1352 | ** If the top of the stack is not an integer and P2 is not zero and |
| 1353 | ** P1 is 1, then the stack is popped. In all other cases, the depth |
| 1354 | ** of the stack is unchanged. |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 1355 | */ |
danielk1977 | 7a5147c | 2005-03-29 13:07:00 +0000 | [diff] [blame] | 1356 | case OP_MustBeInt: { /* no-push */ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1357 | assert( pTos>=p->aStack ); |
drh | 8079a0d | 2006-01-12 17:20:50 +0000 | [diff] [blame] | 1358 | applyAffinity(pTos, SQLITE_AFF_NUMERIC, encoding); |
drh | 17c4029 | 2004-07-21 02:53:29 +0000 | [diff] [blame] | 1359 | if( (pTos->flags & MEM_Int)==0 ){ |
| 1360 | if( pOp->p2==0 ){ |
| 1361 | rc = SQLITE_MISMATCH; |
| 1362 | goto abort_due_to_error; |
| 1363 | }else{ |
| 1364 | if( pOp->p1 ) popStack(&pTos, 1); |
| 1365 | pc = pOp->p2 - 1; |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 1366 | } |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 1367 | }else{ |
drh | 17c4029 | 2004-07-21 02:53:29 +0000 | [diff] [blame] | 1368 | Release(pTos); |
| 1369 | pTos->flags = MEM_Int; |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 1370 | } |
| 1371 | break; |
| 1372 | } |
| 1373 | |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1374 | /* Opcode: RealAffinity * * * |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 1375 | ** |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1376 | ** If the top of the stack is an integer, convert it to a real value. |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 1377 | ** |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1378 | ** This opcode is used when extracting information from a column that |
| 1379 | ** has REAL affinity. Such column values may still be stored as |
| 1380 | ** integers, for space efficiency, but after extraction we want them |
| 1381 | ** to have only a real value. |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 1382 | */ |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1383 | case OP_RealAffinity: { /* no-push */ |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 1384 | assert( pTos>=p->aStack ); |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1385 | if( pTos->flags & MEM_Int ){ |
| 1386 | sqlite3VdbeMemRealify(pTos); |
| 1387 | } |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 1388 | break; |
| 1389 | } |
| 1390 | |
drh | 8df447f | 2005-11-01 15:48:24 +0000 | [diff] [blame] | 1391 | #ifndef SQLITE_OMIT_CAST |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 1392 | /* Opcode: ToText * * * |
| 1393 | ** |
| 1394 | ** Force the value on the top of the stack to be text. |
drh | 31beae9 | 2005-11-24 14:34:36 +0000 | [diff] [blame] | 1395 | ** If the value is numeric, convert it to a string using the |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 1396 | ** equivalent of printf(). Blob values are unchanged and |
| 1397 | ** are afterwards simply interpreted as text. |
| 1398 | ** |
| 1399 | ** A NULL value is not changed by this routine. It remains NULL. |
| 1400 | */ |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1401 | case OP_ToText: { /* same as TK_TO_TEXT, no-push */ |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 1402 | assert( pTos>=p->aStack ); |
| 1403 | if( pTos->flags & MEM_Null ) break; |
| 1404 | assert( MEM_Str==(MEM_Blob>>3) ); |
| 1405 | pTos->flags |= (pTos->flags&MEM_Blob)>>3; |
drh | 8079a0d | 2006-01-12 17:20:50 +0000 | [diff] [blame] | 1406 | applyAffinity(pTos, SQLITE_AFF_TEXT, encoding); |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 1407 | assert( pTos->flags & MEM_Str ); |
| 1408 | pTos->flags &= ~(MEM_Int|MEM_Real|MEM_Blob); |
| 1409 | break; |
| 1410 | } |
| 1411 | |
| 1412 | /* Opcode: ToBlob * * * |
| 1413 | ** |
| 1414 | ** Force the value on the top of the stack to be a BLOB. |
| 1415 | ** If the value is numeric, convert it to a string first. |
| 1416 | ** Strings are simply reinterpreted as blobs with no change |
| 1417 | ** to the underlying data. |
| 1418 | ** |
| 1419 | ** A NULL value is not changed by this routine. It remains NULL. |
| 1420 | */ |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1421 | case OP_ToBlob: { /* same as TK_TO_BLOB, no-push */ |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 1422 | assert( pTos>=p->aStack ); |
| 1423 | if( pTos->flags & MEM_Null ) break; |
| 1424 | if( (pTos->flags & MEM_Blob)==0 ){ |
drh | 8079a0d | 2006-01-12 17:20:50 +0000 | [diff] [blame] | 1425 | applyAffinity(pTos, SQLITE_AFF_TEXT, encoding); |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 1426 | assert( pTos->flags & MEM_Str ); |
| 1427 | pTos->flags |= MEM_Blob; |
| 1428 | } |
| 1429 | pTos->flags &= ~(MEM_Int|MEM_Real|MEM_Str); |
| 1430 | break; |
| 1431 | } |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1432 | |
| 1433 | /* Opcode: ToNumeric * * * |
| 1434 | ** |
| 1435 | ** Force the value on the top of the stack to be numeric (either an |
| 1436 | ** integer or a floating-point number.) |
| 1437 | ** If the value is text or blob, try to convert it to an using the |
| 1438 | ** equivalent of atoi() or atof() and store 0 if no such conversion |
| 1439 | ** is possible. |
| 1440 | ** |
| 1441 | ** A NULL value is not changed by this routine. It remains NULL. |
| 1442 | */ |
| 1443 | case OP_ToNumeric: { /* same as TK_TO_NUMERIC, no-push */ |
| 1444 | assert( pTos>=p->aStack ); |
| 1445 | if( (pTos->flags & MEM_Null)==0 ){ |
| 1446 | sqlite3VdbeMemNumerify(pTos); |
| 1447 | } |
| 1448 | break; |
| 1449 | } |
| 1450 | #endif /* SQLITE_OMIT_CAST */ |
| 1451 | |
| 1452 | /* Opcode: ToInt * * * |
| 1453 | ** |
| 1454 | ** Force the value on the top of the stack to be an integer. If |
| 1455 | ** The value is currently a real number, drop its fractional part. |
| 1456 | ** If the value is text or blob, try to convert it to an integer using the |
| 1457 | ** equivalent of atoi() and store 0 if no such conversion is possible. |
| 1458 | ** |
| 1459 | ** A NULL value is not changed by this routine. It remains NULL. |
| 1460 | */ |
| 1461 | case OP_ToInt: { /* same as TK_TO_INT, no-push */ |
| 1462 | assert( pTos>=p->aStack ); |
| 1463 | if( (pTos->flags & MEM_Null)==0 ){ |
| 1464 | sqlite3VdbeMemIntegerify(pTos); |
| 1465 | } |
| 1466 | break; |
| 1467 | } |
| 1468 | |
| 1469 | #ifndef SQLITE_OMIT_CAST |
| 1470 | /* Opcode: ToReal * * * |
| 1471 | ** |
| 1472 | ** Force the value on the top of the stack to be a floating point number. |
| 1473 | ** If The value is currently an integer, convert it. |
| 1474 | ** If the value is text or blob, try to convert it to an integer using the |
| 1475 | ** equivalent of atoi() and store 0 if no such conversion is possible. |
| 1476 | ** |
| 1477 | ** A NULL value is not changed by this routine. It remains NULL. |
| 1478 | */ |
| 1479 | case OP_ToReal: { /* same as TK_TO_REAL, no-push */ |
| 1480 | assert( pTos>=p->aStack ); |
| 1481 | if( (pTos->flags & MEM_Null)==0 ){ |
| 1482 | sqlite3VdbeMemRealify(pTos); |
| 1483 | } |
| 1484 | break; |
| 1485 | } |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 1486 | #endif /* SQLITE_OMIT_CAST */ |
| 1487 | |
drh | 53db145 | 2004-05-20 13:54:53 +0000 | [diff] [blame] | 1488 | /* Opcode: Eq P1 P2 P3 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1489 | ** |
| 1490 | ** Pop the top two elements from the stack. If they are equal, then |
| 1491 | ** jump to instruction P2. Otherwise, continue to the next instruction. |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1492 | ** |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 1493 | ** If the 0x100 bit of P1 is true and either operand is NULL then take the |
drh | e313382 | 2005-09-20 13:11:59 +0000 | [diff] [blame] | 1494 | ** jump. If the 0x100 bit of P1 is clear then fall thru if either operand |
drh | 4f68623 | 2005-09-20 13:55:18 +0000 | [diff] [blame] | 1495 | ** is NULL. |
| 1496 | ** |
| 1497 | ** If the 0x200 bit of P1 is set and either operand is NULL then |
| 1498 | ** both operands are converted to integers prior to comparison. |
| 1499 | ** NULL operands are converted to zero and non-NULL operands are |
| 1500 | ** converted to 1. Thus, for example, with 0x200 set, NULL==NULL is true |
| 1501 | ** whereas it would normally be NULL. Similarly, NULL==123 is false when |
| 1502 | ** 0x200 is set but is NULL when the 0x200 bit of P1 is clear. |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1503 | ** |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 1504 | ** The least significant byte of P1 (mask 0xff) must be an affinity character - |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1505 | ** SQLITE_AFF_TEXT, SQLITE_AFF_INTEGER, and so forth. An attempt is made |
| 1506 | ** to coerce both values |
danielk1977 | e014a83 | 2004-05-17 10:48:57 +0000 | [diff] [blame] | 1507 | ** according to the affinity before the comparison is made. If the byte is |
| 1508 | ** 0x00, then numeric affinity is used. |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 1509 | ** |
| 1510 | ** Once any conversions have taken place, and neither value is NULL, |
| 1511 | ** the values are compared. If both values are blobs, or both are text, |
| 1512 | ** then memcmp() is used to determine the results of the comparison. If |
| 1513 | ** both values are numeric, then a numeric comparison is used. If the |
| 1514 | ** two values are of different types, then they are inequal. |
drh | c9b84a1 | 2002-06-20 11:36:48 +0000 | [diff] [blame] | 1515 | ** |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1516 | ** If P2 is zero, do not jump. Instead, push an integer 1 onto the |
| 1517 | ** stack if the jump would have been taken, or a 0 if not. Push a |
| 1518 | ** NULL if either operand was NULL. |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 1519 | ** |
drh | 53db145 | 2004-05-20 13:54:53 +0000 | [diff] [blame] | 1520 | ** If P3 is not NULL it is a pointer to a collating sequence (a CollSeq |
| 1521 | ** structure) that defines how to compare text. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1522 | */ |
drh | 53db145 | 2004-05-20 13:54:53 +0000 | [diff] [blame] | 1523 | /* Opcode: Ne P1 P2 P3 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1524 | ** |
drh | 53db145 | 2004-05-20 13:54:53 +0000 | [diff] [blame] | 1525 | ** This works just like the Eq opcode except that the jump is taken if |
| 1526 | ** the operands from the stack are not equal. See the Eq opcode for |
| 1527 | ** additional information. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1528 | */ |
drh | 53db145 | 2004-05-20 13:54:53 +0000 | [diff] [blame] | 1529 | /* Opcode: Lt P1 P2 P3 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1530 | ** |
drh | 53db145 | 2004-05-20 13:54:53 +0000 | [diff] [blame] | 1531 | ** This works just like the Eq opcode except that the jump is taken if |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 1532 | ** the 2nd element down on the stack is less than the top of the stack. |
drh | 53db145 | 2004-05-20 13:54:53 +0000 | [diff] [blame] | 1533 | ** See the Eq opcode for additional information. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1534 | */ |
drh | 53db145 | 2004-05-20 13:54:53 +0000 | [diff] [blame] | 1535 | /* Opcode: Le P1 P2 P3 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1536 | ** |
drh | 53db145 | 2004-05-20 13:54:53 +0000 | [diff] [blame] | 1537 | ** This works just like the Eq opcode except that the jump is taken if |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 1538 | ** the 2nd element down on the stack is less than or equal to the |
drh | 53db145 | 2004-05-20 13:54:53 +0000 | [diff] [blame] | 1539 | ** top of the stack. See the Eq opcode for additional information. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1540 | */ |
drh | 53db145 | 2004-05-20 13:54:53 +0000 | [diff] [blame] | 1541 | /* Opcode: Gt P1 P2 P3 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1542 | ** |
drh | 53db145 | 2004-05-20 13:54:53 +0000 | [diff] [blame] | 1543 | ** This works just like the Eq opcode except that the jump is taken if |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 1544 | ** the 2nd element down on the stack is greater than the top of the stack. |
drh | 53db145 | 2004-05-20 13:54:53 +0000 | [diff] [blame] | 1545 | ** See the Eq opcode for additional information. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1546 | */ |
drh | 53db145 | 2004-05-20 13:54:53 +0000 | [diff] [blame] | 1547 | /* Opcode: Ge P1 P2 P3 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1548 | ** |
drh | 53db145 | 2004-05-20 13:54:53 +0000 | [diff] [blame] | 1549 | ** This works just like the Eq opcode except that the jump is taken if |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 1550 | ** the 2nd element down on the stack is greater than or equal to the |
drh | 53db145 | 2004-05-20 13:54:53 +0000 | [diff] [blame] | 1551 | ** top of the stack. See the Eq opcode for additional information. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1552 | */ |
danielk1977 | 7a5147c | 2005-03-29 13:07:00 +0000 | [diff] [blame] | 1553 | case OP_Eq: /* same as TK_EQ, no-push */ |
| 1554 | case OP_Ne: /* same as TK_NE, no-push */ |
| 1555 | case OP_Lt: /* same as TK_LT, no-push */ |
| 1556 | case OP_Le: /* same as TK_LE, no-push */ |
| 1557 | case OP_Gt: /* same as TK_GT, no-push */ |
| 1558 | case OP_Ge: { /* same as TK_GE, no-push */ |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 1559 | Mem *pNos; |
| 1560 | int flags; |
| 1561 | int res; |
| 1562 | char affinity; |
| 1563 | |
| 1564 | pNos = &pTos[-1]; |
| 1565 | flags = pTos->flags|pNos->flags; |
| 1566 | |
| 1567 | /* If either value is a NULL P2 is not zero, take the jump if the least |
| 1568 | ** significant byte of P1 is true. If P2 is zero, then push a NULL onto |
| 1569 | ** the stack. |
| 1570 | */ |
| 1571 | if( flags&MEM_Null ){ |
drh | 4f68623 | 2005-09-20 13:55:18 +0000 | [diff] [blame] | 1572 | if( (pOp->p1 & 0x200)!=0 ){ |
| 1573 | /* The 0x200 bit of P1 means, roughly "do not treat NULL as the |
| 1574 | ** magic SQL value it normally is - treat it as if it were another |
| 1575 | ** integer". |
| 1576 | ** |
| 1577 | ** With 0x200 set, if either operand is NULL then both operands |
| 1578 | ** are converted to integers prior to being passed down into the |
| 1579 | ** normal comparison logic below. NULL operands are converted to |
| 1580 | ** zero and non-NULL operands are converted to 1. Thus, for example, |
| 1581 | ** with 0x200 set, NULL==NULL is true whereas it would normally |
| 1582 | ** be NULL. Similarly, NULL!=123 is true. |
drh | e313382 | 2005-09-20 13:11:59 +0000 | [diff] [blame] | 1583 | */ |
drh | 4f68623 | 2005-09-20 13:55:18 +0000 | [diff] [blame] | 1584 | sqlite3VdbeMemSetInt64(pTos, (pTos->flags & MEM_Null)==0); |
| 1585 | sqlite3VdbeMemSetInt64(pNos, (pNos->flags & MEM_Null)==0); |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 1586 | }else{ |
drh | 4f68623 | 2005-09-20 13:55:18 +0000 | [diff] [blame] | 1587 | /* If the 0x200 bit of P1 is clear and either operand is NULL then |
| 1588 | ** the result is always NULL. The jump is taken if the 0x100 bit |
drh | e313382 | 2005-09-20 13:11:59 +0000 | [diff] [blame] | 1589 | ** of P1 is set. |
| 1590 | */ |
| 1591 | popStack(&pTos, 2); |
| 1592 | if( pOp->p2 ){ |
| 1593 | if( pOp->p1 & 0x100 ){ |
| 1594 | pc = pOp->p2-1; |
| 1595 | } |
| 1596 | }else{ |
| 1597 | pTos++; |
| 1598 | pTos->flags = MEM_Null; |
| 1599 | } |
| 1600 | break; |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 1601 | } |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 1602 | } |
| 1603 | |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 1604 | affinity = pOp->p1 & 0xFF; |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 1605 | if( affinity ){ |
drh | 8079a0d | 2006-01-12 17:20:50 +0000 | [diff] [blame] | 1606 | applyAffinity(pNos, affinity, encoding); |
| 1607 | applyAffinity(pTos, affinity, encoding); |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 1608 | } |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 1609 | |
drh | 53db145 | 2004-05-20 13:54:53 +0000 | [diff] [blame] | 1610 | assert( pOp->p3type==P3_COLLSEQ || pOp->p3==0 ); |
| 1611 | res = sqlite3MemCompare(pNos, pTos, (CollSeq*)pOp->p3); |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 1612 | switch( pOp->opcode ){ |
| 1613 | case OP_Eq: res = res==0; break; |
| 1614 | case OP_Ne: res = res!=0; break; |
| 1615 | case OP_Lt: res = res<0; break; |
| 1616 | case OP_Le: res = res<=0; break; |
| 1617 | case OP_Gt: res = res>0; break; |
| 1618 | default: res = res>=0; break; |
| 1619 | } |
| 1620 | |
| 1621 | popStack(&pTos, 2); |
| 1622 | if( pOp->p2 ){ |
| 1623 | if( res ){ |
| 1624 | pc = pOp->p2-1; |
| 1625 | } |
| 1626 | }else{ |
| 1627 | pTos++; |
| 1628 | pTos->flags = MEM_Int; |
| 1629 | pTos->i = res; |
| 1630 | } |
| 1631 | break; |
| 1632 | } |
drh | c9b84a1 | 2002-06-20 11:36:48 +0000 | [diff] [blame] | 1633 | |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1634 | /* Opcode: And * * * |
| 1635 | ** |
| 1636 | ** Pop two values off the stack. Take the logical AND of the |
| 1637 | ** two values and push the resulting boolean value back onto the |
| 1638 | ** stack. |
| 1639 | */ |
| 1640 | /* Opcode: Or * * * |
| 1641 | ** |
| 1642 | ** Pop two values off the stack. Take the logical OR of the |
| 1643 | ** two values and push the resulting boolean value back onto the |
| 1644 | ** stack. |
| 1645 | */ |
danielk1977 | 7a5147c | 2005-03-29 13:07:00 +0000 | [diff] [blame] | 1646 | case OP_And: /* same as TK_AND, no-push */ |
| 1647 | case OP_Or: { /* same as TK_OR, no-push */ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1648 | Mem *pNos = &pTos[-1]; |
drh | bb11351 | 2002-05-27 01:04:51 +0000 | [diff] [blame] | 1649 | int v1, v2; /* 0==TRUE, 1==FALSE, 2==UNKNOWN or NULL */ |
| 1650 | |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1651 | assert( pNos>=p->aStack ); |
| 1652 | if( pTos->flags & MEM_Null ){ |
drh | bb11351 | 2002-05-27 01:04:51 +0000 | [diff] [blame] | 1653 | v1 = 2; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1654 | }else{ |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1655 | sqlite3VdbeMemIntegerify(pTos); |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1656 | v1 = pTos->i==0; |
drh | bb11351 | 2002-05-27 01:04:51 +0000 | [diff] [blame] | 1657 | } |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1658 | if( pNos->flags & MEM_Null ){ |
drh | bb11351 | 2002-05-27 01:04:51 +0000 | [diff] [blame] | 1659 | v2 = 2; |
| 1660 | }else{ |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1661 | sqlite3VdbeMemIntegerify(pNos); |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1662 | v2 = pNos->i==0; |
drh | bb11351 | 2002-05-27 01:04:51 +0000 | [diff] [blame] | 1663 | } |
| 1664 | if( pOp->opcode==OP_And ){ |
| 1665 | static const unsigned char and_logic[] = { 0, 1, 2, 1, 1, 1, 2, 1, 2 }; |
| 1666 | v1 = and_logic[v1*3+v2]; |
| 1667 | }else{ |
| 1668 | static const unsigned char or_logic[] = { 0, 0, 0, 0, 1, 2, 0, 2, 2 }; |
| 1669 | v1 = or_logic[v1*3+v2]; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1670 | } |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1671 | popStack(&pTos, 2); |
| 1672 | pTos++; |
drh | bb11351 | 2002-05-27 01:04:51 +0000 | [diff] [blame] | 1673 | if( v1==2 ){ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1674 | pTos->flags = MEM_Null; |
drh | bb11351 | 2002-05-27 01:04:51 +0000 | [diff] [blame] | 1675 | }else{ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1676 | pTos->i = v1==0; |
| 1677 | pTos->flags = MEM_Int; |
drh | bb11351 | 2002-05-27 01:04:51 +0000 | [diff] [blame] | 1678 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1679 | break; |
| 1680 | } |
| 1681 | |
| 1682 | /* Opcode: Negative * * * |
| 1683 | ** |
| 1684 | ** Treat the top of the stack as a numeric quantity. Replace it |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1685 | ** with its additive inverse. If the top of the stack is NULL |
| 1686 | ** its value is unchanged. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1687 | */ |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1688 | /* Opcode: AbsValue * * * |
| 1689 | ** |
| 1690 | ** Treat the top of the stack as a numeric quantity. Replace it |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1691 | ** with its absolute value. If the top of the stack is NULL |
| 1692 | ** its value is unchanged. |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1693 | */ |
danielk1977 | 7a5147c | 2005-03-29 13:07:00 +0000 | [diff] [blame] | 1694 | case OP_Negative: /* same as TK_UMINUS, no-push */ |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1695 | case OP_AbsValue: { |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1696 | assert( pTos>=p->aStack ); |
| 1697 | if( pTos->flags & MEM_Real ){ |
drh | 8df447f | 2005-11-01 15:48:24 +0000 | [diff] [blame] | 1698 | neg_abs_real_case: |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1699 | Release(pTos); |
| 1700 | if( pOp->opcode==OP_Negative || pTos->r<0.0 ){ |
| 1701 | pTos->r = -pTos->r; |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1702 | } |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1703 | pTos->flags = MEM_Real; |
| 1704 | }else if( pTos->flags & MEM_Int ){ |
| 1705 | Release(pTos); |
| 1706 | if( pOp->opcode==OP_Negative || pTos->i<0 ){ |
| 1707 | pTos->i = -pTos->i; |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1708 | } |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1709 | pTos->flags = MEM_Int; |
| 1710 | }else if( pTos->flags & MEM_Null ){ |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1711 | /* Do nothing */ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1712 | }else{ |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1713 | sqlite3VdbeMemNumerify(pTos); |
drh | 8df447f | 2005-11-01 15:48:24 +0000 | [diff] [blame] | 1714 | goto neg_abs_real_case; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1715 | } |
| 1716 | break; |
| 1717 | } |
| 1718 | |
| 1719 | /* Opcode: Not * * * |
| 1720 | ** |
| 1721 | ** Interpret the top of the stack as a boolean value. Replace it |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1722 | ** with its complement. If the top of the stack is NULL its value |
| 1723 | ** is unchanged. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1724 | */ |
danielk1977 | 7a5147c | 2005-03-29 13:07:00 +0000 | [diff] [blame] | 1725 | case OP_Not: { /* same as TK_NOT, no-push */ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1726 | assert( pTos>=p->aStack ); |
| 1727 | if( pTos->flags & MEM_Null ) break; /* Do nothing to NULLs */ |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1728 | sqlite3VdbeMemIntegerify(pTos); |
drh | 6a6124e | 2004-06-27 01:56:33 +0000 | [diff] [blame] | 1729 | assert( (pTos->flags & MEM_Dyn)==0 ); |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1730 | pTos->i = !pTos->i; |
drh | 79f14b7 | 2004-03-03 01:51:24 +0000 | [diff] [blame] | 1731 | pTos->flags = MEM_Int; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1732 | break; |
| 1733 | } |
| 1734 | |
drh | 18b81e5 | 2001-11-01 13:52:52 +0000 | [diff] [blame] | 1735 | /* Opcode: BitNot * * * |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1736 | ** |
| 1737 | ** Interpret the top of the stack as an value. Replace it |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1738 | ** with its ones-complement. If the top of the stack is NULL its |
| 1739 | ** value is unchanged. |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1740 | */ |
danielk1977 | 7a5147c | 2005-03-29 13:07:00 +0000 | [diff] [blame] | 1741 | case OP_BitNot: { /* same as TK_BITNOT, no-push */ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1742 | assert( pTos>=p->aStack ); |
| 1743 | if( pTos->flags & MEM_Null ) break; /* Do nothing to NULLs */ |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1744 | sqlite3VdbeMemIntegerify(pTos); |
drh | 6a6124e | 2004-06-27 01:56:33 +0000 | [diff] [blame] | 1745 | assert( (pTos->flags & MEM_Dyn)==0 ); |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1746 | pTos->i = ~pTos->i; |
drh | 79f14b7 | 2004-03-03 01:51:24 +0000 | [diff] [blame] | 1747 | pTos->flags = MEM_Int; |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1748 | break; |
| 1749 | } |
| 1750 | |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1751 | /* Opcode: Noop * * * |
| 1752 | ** |
| 1753 | ** Do nothing. This instruction is often useful as a jump |
| 1754 | ** destination. |
| 1755 | */ |
drh | ecc9242 | 2005-09-10 16:46:12 +0000 | [diff] [blame] | 1756 | /* |
| 1757 | ** The magic Explain opcode are only inserted when explain==2 (which |
| 1758 | ** is to say when the EXPLAIN QUERY PLAN syntax is used.) |
| 1759 | ** This opcode records information from the optimizer. It is the |
| 1760 | ** the same as a no-op. This opcodesnever appears in a real VM program. |
| 1761 | */ |
| 1762 | case OP_Explain: |
danielk1977 | 7a5147c | 2005-03-29 13:07:00 +0000 | [diff] [blame] | 1763 | case OP_Noop: { /* no-push */ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1764 | break; |
| 1765 | } |
| 1766 | |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1767 | /* Opcode: If P1 P2 * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1768 | ** |
| 1769 | ** Pop a single boolean from the stack. If the boolean popped is |
| 1770 | ** true, then jump to p2. Otherwise continue to the next instruction. |
| 1771 | ** An integer is false if zero and true otherwise. A string is |
| 1772 | ** false if it has zero length and true otherwise. |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1773 | ** |
| 1774 | ** If the value popped of the stack is NULL, then take the jump if P1 |
| 1775 | ** is true and fall through if P1 is false. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1776 | */ |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1777 | /* Opcode: IfNot P1 P2 * |
| 1778 | ** |
| 1779 | ** Pop a single boolean from the stack. If the boolean popped is |
| 1780 | ** false, then jump to p2. Otherwise continue to the next instruction. |
| 1781 | ** An integer is false if zero and true otherwise. A string is |
| 1782 | ** false if it has zero length and true otherwise. |
| 1783 | ** |
| 1784 | ** If the value popped of the stack is NULL, then take the jump if P1 |
| 1785 | ** is true and fall through if P1 is false. |
| 1786 | */ |
danielk1977 | 7a5147c | 2005-03-29 13:07:00 +0000 | [diff] [blame] | 1787 | case OP_If: /* no-push */ |
| 1788 | case OP_IfNot: { /* no-push */ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1789 | int c; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1790 | assert( pTos>=p->aStack ); |
| 1791 | if( pTos->flags & MEM_Null ){ |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1792 | c = pOp->p1; |
| 1793 | }else{ |
drh | ba0232a | 2005-06-06 17:27:19 +0000 | [diff] [blame] | 1794 | #ifdef SQLITE_OMIT_FLOATING_POINT |
drh | 6a6124e | 2004-06-27 01:56:33 +0000 | [diff] [blame] | 1795 | c = sqlite3VdbeIntValue(pTos); |
drh | ba0232a | 2005-06-06 17:27:19 +0000 | [diff] [blame] | 1796 | #else |
| 1797 | c = sqlite3VdbeRealValue(pTos)!=0.0; |
| 1798 | #endif |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1799 | if( pOp->opcode==OP_IfNot ) c = !c; |
| 1800 | } |
danielk1977 | 8a6b541 | 2004-05-24 07:04:25 +0000 | [diff] [blame] | 1801 | Release(pTos); |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1802 | pTos--; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1803 | if( c ) pc = pOp->p2-1; |
| 1804 | break; |
| 1805 | } |
| 1806 | |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1807 | /* Opcode: IsNull P1 P2 * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1808 | ** |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1809 | ** 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] | 1810 | ** to P2. Pop the stack P1 times if P1>0. If P1<0 leave the stack |
| 1811 | ** unchanged. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1812 | */ |
danielk1977 | 7a5147c | 2005-03-29 13:07:00 +0000 | [diff] [blame] | 1813 | case OP_IsNull: { /* same as TK_ISNULL, no-push */ |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1814 | int i, cnt; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1815 | Mem *pTerm; |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1816 | cnt = pOp->p1; |
| 1817 | if( cnt<0 ) cnt = -cnt; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1818 | pTerm = &pTos[1-cnt]; |
| 1819 | assert( pTerm>=p->aStack ); |
| 1820 | for(i=0; i<cnt; i++, pTerm++){ |
| 1821 | if( pTerm->flags & MEM_Null ){ |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1822 | pc = pOp->p2-1; |
| 1823 | break; |
| 1824 | } |
| 1825 | } |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1826 | if( pOp->p1>0 ) popStack(&pTos, cnt); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1827 | break; |
| 1828 | } |
| 1829 | |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1830 | /* Opcode: NotNull P1 P2 * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1831 | ** |
drh | 143f3c4 | 2004-01-07 20:37:52 +0000 | [diff] [blame] | 1832 | ** Jump to P2 if the top P1 values on the stack are all not NULL. Pop the |
| 1833 | ** stack if P1 times if P1 is greater than zero. If P1 is less than |
| 1834 | ** zero then leave the stack unchanged. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1835 | */ |
danielk1977 | 7a5147c | 2005-03-29 13:07:00 +0000 | [diff] [blame] | 1836 | case OP_NotNull: { /* same as TK_NOTNULL, no-push */ |
drh | 143f3c4 | 2004-01-07 20:37:52 +0000 | [diff] [blame] | 1837 | int i, cnt; |
| 1838 | cnt = pOp->p1; |
| 1839 | if( cnt<0 ) cnt = -cnt; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1840 | assert( &pTos[1-cnt] >= p->aStack ); |
| 1841 | 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] | 1842 | if( i>=cnt ) pc = pOp->p2-1; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1843 | if( pOp->p1>0 ) popStack(&pTos, cnt); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1844 | break; |
| 1845 | } |
| 1846 | |
danielk1977 | b4964b7 | 2004-05-18 01:23:38 +0000 | [diff] [blame] | 1847 | /* Opcode: SetNumColumns P1 P2 * |
| 1848 | ** |
| 1849 | ** Before the OP_Column opcode can be executed on a cursor, this |
| 1850 | ** opcode must be called to set the number of fields in the table. |
| 1851 | ** |
| 1852 | ** This opcode sets the number of columns for cursor P1 to P2. |
danielk1977 | ac17178 | 2005-02-05 06:49:54 +0000 | [diff] [blame] | 1853 | ** |
| 1854 | ** If OP_KeyAsData is to be applied to cursor P1, it must be executed |
| 1855 | ** before this op-code. |
danielk1977 | b4964b7 | 2004-05-18 01:23:38 +0000 | [diff] [blame] | 1856 | */ |
danielk1977 | 7a5147c | 2005-03-29 13:07:00 +0000 | [diff] [blame] | 1857 | case OP_SetNumColumns: { /* no-push */ |
danielk1977 | ac17178 | 2005-02-05 06:49:54 +0000 | [diff] [blame] | 1858 | Cursor *pC; |
danielk1977 | b4964b7 | 2004-05-18 01:23:38 +0000 | [diff] [blame] | 1859 | assert( (pOp->p1)<p->nCursor ); |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 1860 | assert( p->apCsr[pOp->p1]!=0 ); |
danielk1977 | ac17178 | 2005-02-05 06:49:54 +0000 | [diff] [blame] | 1861 | pC = p->apCsr[pOp->p1]; |
| 1862 | pC->nField = pOp->p2; |
danielk1977 | b4964b7 | 2004-05-18 01:23:38 +0000 | [diff] [blame] | 1863 | break; |
| 1864 | } |
| 1865 | |
danielk1977 | aee18ef | 2005-03-09 12:26:50 +0000 | [diff] [blame] | 1866 | /* Opcode: Column P1 P2 P3 |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 1867 | ** |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1868 | ** Interpret the data that cursor P1 points to as a structure built using |
| 1869 | ** the MakeRecord instruction. (See the MakeRecord opcode for additional |
| 1870 | ** information about the format of the data.) Push onto the stack the value |
danielk1977 | 36963fd | 2005-02-19 08:18:05 +0000 | [diff] [blame] | 1871 | ** of the P2-th column contained in the data. If there are less that (P2+1) |
| 1872 | ** values in the record, push a NULL onto the stack. |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 1873 | ** |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1874 | ** If the KeyAsData opcode has previously executed on this cursor, then the |
| 1875 | ** field might be extracted from the key rather than the data. |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 1876 | ** |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 1877 | ** If the column contains fewer than P2 fields, then push a NULL. Or |
| 1878 | ** if P3 is of type P3_MEM, then push the P3 value. The P3 value will |
| 1879 | ** be default value for a column that has been added using the ALTER TABLE |
| 1880 | ** ADD COLUMN command. If P3 is an ordinary string, just push a NULL. |
| 1881 | ** When P3 is a string it is really just a comment describing the value |
| 1882 | ** to be pushed, not a default value. |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 1883 | */ |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1884 | case OP_Column: { |
danielk1977 | e0d4b06 | 2004-06-28 01:11:46 +0000 | [diff] [blame] | 1885 | u32 payloadSize; /* Number of bytes in the record */ |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 1886 | int p1 = pOp->p1; /* P1 value of the opcode */ |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1887 | int p2 = pOp->p2; /* column number to retrieve */ |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 1888 | Cursor *pC = 0; /* The VDBE cursor */ |
drh | e61cffc | 2004-06-12 18:12:15 +0000 | [diff] [blame] | 1889 | char *zRec; /* Pointer to complete record-data */ |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 1890 | BtCursor *pCrsr; /* The BTree cursor */ |
| 1891 | u32 *aType; /* aType[i] holds the numeric type of the i-th column */ |
| 1892 | 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] | 1893 | u32 nField; /* number of fields in the record */ |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1894 | int len; /* The length of the serialized data for the column */ |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 1895 | int i; /* Loop counter */ |
| 1896 | char *zData; /* Part of the record being decoded */ |
| 1897 | Mem sMem; /* For storing the record being decoded */ |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 1898 | |
drh | b6f5452 | 2004-05-20 02:42:16 +0000 | [diff] [blame] | 1899 | sMem.flags = 0; |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 1900 | assert( p1<p->nCursor ); |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 1901 | pTos++; |
drh | febe106 | 2004-08-28 18:17:48 +0000 | [diff] [blame] | 1902 | pTos->flags = MEM_Null; |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1903 | |
drh | e61cffc | 2004-06-12 18:12:15 +0000 | [diff] [blame] | 1904 | /* This block sets the variable payloadSize to be the total number of |
| 1905 | ** bytes in the record. |
| 1906 | ** |
| 1907 | ** zRec is set to be the complete text of the record if it is available. |
drh | b73857f | 2006-03-17 00:25:59 +0000 | [diff] [blame] | 1908 | ** The complete record text is always available for pseudo-tables |
| 1909 | ** If the record is stored in a cursor, the complete record text |
| 1910 | ** might be available in the pC->aRow cache. Or it might not be. |
| 1911 | ** If the data is unavailable, zRec is set to NULL. |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 1912 | ** |
| 1913 | ** We also compute the number of columns in the record. For cursors, |
| 1914 | ** the number of columns is stored in the Cursor.nField element. For |
| 1915 | ** records on the stack, the next entry down on the stack is an integer |
| 1916 | ** which is the number of records. |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1917 | */ |
drh | b73857f | 2006-03-17 00:25:59 +0000 | [diff] [blame] | 1918 | pC = p->apCsr[p1]; |
| 1919 | assert( pC!=0 ); |
| 1920 | if( pC->pCursor!=0 ){ |
drh | e61cffc | 2004-06-12 18:12:15 +0000 | [diff] [blame] | 1921 | /* The record is stored in a B-Tree */ |
drh | 536065a | 2005-01-26 21:55:31 +0000 | [diff] [blame] | 1922 | rc = sqlite3VdbeCursorMoveto(pC); |
drh | 52f159e | 2005-01-27 00:33:21 +0000 | [diff] [blame] | 1923 | if( rc ) goto abort_due_to_error; |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 1924 | zRec = 0; |
| 1925 | pCrsr = pC->pCursor; |
| 1926 | if( pC->nullRow ){ |
| 1927 | payloadSize = 0; |
drh | 76873ab | 2006-01-07 18:48:26 +0000 | [diff] [blame] | 1928 | }else if( pC->cacheStatus==p->cacheCtr ){ |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 1929 | payloadSize = pC->payloadSize; |
drh | 2646da7 | 2005-12-09 20:02:05 +0000 | [diff] [blame] | 1930 | zRec = (char*)pC->aRow; |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 1931 | }else if( pC->isIndex ){ |
danielk1977 | 96fc5fe | 2004-05-13 11:34:16 +0000 | [diff] [blame] | 1932 | i64 payloadSize64; |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 1933 | sqlite3BtreeKeySize(pCrsr, &payloadSize64); |
| 1934 | payloadSize = payloadSize64; |
| 1935 | }else{ |
| 1936 | sqlite3BtreeDataSize(pCrsr, &payloadSize); |
| 1937 | } |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 1938 | nField = pC->nField; |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 1939 | }else if( pC->pseudoTable ){ |
drh | e61cffc | 2004-06-12 18:12:15 +0000 | [diff] [blame] | 1940 | /* The record is the sole entry of a pseudo-table */ |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 1941 | payloadSize = pC->nData; |
| 1942 | zRec = pC->pData; |
drh | 76873ab | 2006-01-07 18:48:26 +0000 | [diff] [blame] | 1943 | pC->cacheStatus = CACHE_STALE; |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 1944 | assert( payloadSize==0 || zRec!=0 ); |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 1945 | nField = pC->nField; |
danielk1977 | f7df9cc | 2004-06-16 12:02:47 +0000 | [diff] [blame] | 1946 | pCrsr = 0; |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 1947 | }else{ |
danielk1977 | f7df9cc | 2004-06-16 12:02:47 +0000 | [diff] [blame] | 1948 | zRec = 0; |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 1949 | payloadSize = 0; |
danielk1977 | f7df9cc | 2004-06-16 12:02:47 +0000 | [diff] [blame] | 1950 | pCrsr = 0; |
| 1951 | nField = 0; |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 1952 | } |
| 1953 | |
| 1954 | /* If payloadSize is 0, then just push a NULL onto the stack. */ |
| 1955 | if( payloadSize==0 ){ |
danielk1977 | 9792eef | 2006-01-13 15:58:43 +0000 | [diff] [blame] | 1956 | assert( pTos->flags==MEM_Null ); |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 1957 | break; |
| 1958 | } |
| 1959 | |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 1960 | assert( p2<nField ); |
danielk1977 | b4964b7 | 2004-05-18 01:23:38 +0000 | [diff] [blame] | 1961 | |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 1962 | /* Read and parse the table header. Store the results of the parse |
| 1963 | ** into the record header cache fields of the cursor. |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 1964 | */ |
drh | 76873ab | 2006-01-07 18:48:26 +0000 | [diff] [blame] | 1965 | if( pC && pC->cacheStatus==p->cacheCtr ){ |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 1966 | aType = pC->aType; |
| 1967 | aOffset = pC->aOffset; |
| 1968 | }else{ |
danielk1977 | dedf45b | 2006-01-13 17:12:01 +0000 | [diff] [blame] | 1969 | u8 *zIdx; /* Index into header */ |
| 1970 | u8 *zEndHdr; /* Pointer to first byte after the header */ |
| 1971 | u32 offset; /* Offset into the data */ |
drh | 0ac0719 | 2006-02-10 14:02:07 +0000 | [diff] [blame] | 1972 | int szHdrSz; /* Size of the header size field at start of record */ |
danielk1977 | dedf45b | 2006-01-13 17:12:01 +0000 | [diff] [blame] | 1973 | int avail; /* Number of bytes of available data */ |
drh | b73857f | 2006-03-17 00:25:59 +0000 | [diff] [blame] | 1974 | |
| 1975 | aType = pC->aType; |
| 1976 | if( aType==0 ){ |
| 1977 | pC->aType = aType = sqliteMallocRaw( 2*nField*sizeof(aType) ); |
drh | 51846b5 | 2004-05-28 16:00:21 +0000 | [diff] [blame] | 1978 | } |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 1979 | if( aType==0 ){ |
| 1980 | goto no_mem; |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 1981 | } |
drh | b73857f | 2006-03-17 00:25:59 +0000 | [diff] [blame] | 1982 | pC->aOffset = aOffset = &aType[nField]; |
| 1983 | pC->payloadSize = payloadSize; |
| 1984 | pC->cacheStatus = p->cacheCtr; |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 1985 | |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 1986 | /* Figure out how many bytes are in the header */ |
danielk1977 | 84ac9d0 | 2004-05-18 09:58:06 +0000 | [diff] [blame] | 1987 | if( zRec ){ |
| 1988 | zData = zRec; |
| 1989 | }else{ |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 1990 | if( pC->isIndex ){ |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 1991 | zData = (char*)sqlite3BtreeKeyFetch(pCrsr, &avail); |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 1992 | }else{ |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 1993 | zData = (char*)sqlite3BtreeDataFetch(pCrsr, &avail); |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 1994 | } |
drh | e61cffc | 2004-06-12 18:12:15 +0000 | [diff] [blame] | 1995 | /* If KeyFetch()/DataFetch() managed to get the entire payload, |
| 1996 | ** save the payload in the pC->aRow cache. That will save us from |
| 1997 | ** having to make additional calls to fetch the content portion of |
| 1998 | ** the record. |
| 1999 | */ |
| 2000 | if( avail>=payloadSize ){ |
drh | 2646da7 | 2005-12-09 20:02:05 +0000 | [diff] [blame] | 2001 | zRec = zData; |
| 2002 | pC->aRow = (u8*)zData; |
drh | e61cffc | 2004-06-12 18:12:15 +0000 | [diff] [blame] | 2003 | }else{ |
| 2004 | pC->aRow = 0; |
| 2005 | } |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 2006 | } |
drh | 0ac0719 | 2006-02-10 14:02:07 +0000 | [diff] [blame] | 2007 | assert( zRec!=0 || avail>=payloadSize || avail>=9 ); |
| 2008 | szHdrSz = GetVarint((u8*)zData, offset); |
drh | e61cffc | 2004-06-12 18:12:15 +0000 | [diff] [blame] | 2009 | |
| 2010 | /* The KeyFetch() or DataFetch() above are fast and will get the entire |
| 2011 | ** record header in most cases. But they will fail to get the complete |
| 2012 | ** record header if the record header does not fit on a single page |
| 2013 | ** in the B-Tree. When that happens, use sqlite3VdbeMemFromBtree() to |
| 2014 | ** acquire the complete header text. |
| 2015 | */ |
danielk1977 | dedf45b | 2006-01-13 17:12:01 +0000 | [diff] [blame] | 2016 | if( !zRec && avail<offset ){ |
| 2017 | rc = sqlite3VdbeMemFromBtree(pCrsr, 0, offset, pC->isIndex, &sMem); |
danielk1977 | 84ac9d0 | 2004-05-18 09:58:06 +0000 | [diff] [blame] | 2018 | if( rc!=SQLITE_OK ){ |
danielk1977 | 3c9cc8d | 2005-01-17 03:40:08 +0000 | [diff] [blame] | 2019 | goto op_column_out; |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2020 | } |
drh | b6f5452 | 2004-05-20 02:42:16 +0000 | [diff] [blame] | 2021 | zData = sMem.z; |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2022 | } |
drh | 0ac0719 | 2006-02-10 14:02:07 +0000 | [diff] [blame] | 2023 | zEndHdr = (u8 *)&zData[offset]; |
| 2024 | zIdx = (u8 *)&zData[szHdrSz]; |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2025 | |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 2026 | /* Scan the header and use it to fill in the aType[] and aOffset[] |
| 2027 | ** arrays. aType[i] will contain the type integer for the i-th |
| 2028 | ** column and aOffset[i] will contain the offset from the beginning |
| 2029 | ** 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] | 2030 | */ |
danielk1977 | dedf45b | 2006-01-13 17:12:01 +0000 | [diff] [blame] | 2031 | for(i=0; i<nField; i++){ |
| 2032 | if( zIdx<zEndHdr ){ |
| 2033 | aOffset[i] = offset; |
| 2034 | zIdx += GetVarint(zIdx, aType[i]); |
| 2035 | offset += sqlite3VdbeSerialTypeLen(aType[i]); |
| 2036 | }else{ |
| 2037 | /* If i is less that nField, then there are less fields in this |
| 2038 | ** record than SetNumColumns indicated there are columns in the |
| 2039 | ** table. Set the offset for any extra columns not present in |
| 2040 | ** the record to 0. This tells code below to push a NULL onto the |
| 2041 | ** stack instead of deserializing a value from the record. |
| 2042 | */ |
| 2043 | aOffset[i] = 0; |
| 2044 | } |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2045 | } |
drh | b6f5452 | 2004-05-20 02:42:16 +0000 | [diff] [blame] | 2046 | Release(&sMem); |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 2047 | sMem.flags = MEM_Null; |
| 2048 | |
danielk1977 | 9792eef | 2006-01-13 15:58:43 +0000 | [diff] [blame] | 2049 | /* If we have read more header data than was contained in the header, |
| 2050 | ** or if the end of the last field appears to be past the end of the |
| 2051 | ** record, then we must be dealing with a corrupt database. |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 2052 | */ |
danielk1977 | dedf45b | 2006-01-13 17:12:01 +0000 | [diff] [blame] | 2053 | if( zIdx>zEndHdr || offset>payloadSize ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 2054 | rc = SQLITE_CORRUPT_BKPT; |
danielk1977 | 3c9cc8d | 2005-01-17 03:40:08 +0000 | [diff] [blame] | 2055 | goto op_column_out; |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 2056 | } |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 2057 | } |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2058 | |
danielk1977 | 36963fd | 2005-02-19 08:18:05 +0000 | [diff] [blame] | 2059 | /* Get the column information. If aOffset[p2] is non-zero, then |
| 2060 | ** deserialize the value from the record. If aOffset[p2] is zero, |
| 2061 | ** then there are not enough fields in the record to satisfy the |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 2062 | ** request. In this case, set the value NULL or to P3 if P3 is |
| 2063 | ** a pointer to a Mem object. |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2064 | */ |
danielk1977 | 36963fd | 2005-02-19 08:18:05 +0000 | [diff] [blame] | 2065 | if( aOffset[p2] ){ |
| 2066 | assert( rc==SQLITE_OK ); |
| 2067 | if( zRec ){ |
| 2068 | zData = &zRec[aOffset[p2]]; |
| 2069 | }else{ |
| 2070 | len = sqlite3VdbeSerialTypeLen(aType[p2]); |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 2071 | rc = sqlite3VdbeMemFromBtree(pCrsr, aOffset[p2], len, pC->isIndex,&sMem); |
danielk1977 | 36963fd | 2005-02-19 08:18:05 +0000 | [diff] [blame] | 2072 | if( rc!=SQLITE_OK ){ |
| 2073 | goto op_column_out; |
| 2074 | } |
| 2075 | zData = sMem.z; |
danielk1977 | 7701e81 | 2005-01-10 12:59:51 +0000 | [diff] [blame] | 2076 | } |
drh | 2646da7 | 2005-12-09 20:02:05 +0000 | [diff] [blame] | 2077 | sqlite3VdbeSerialGet((u8*)zData, aType[p2], pTos); |
drh | 8079a0d | 2006-01-12 17:20:50 +0000 | [diff] [blame] | 2078 | pTos->enc = encoding; |
danielk1977 | 36963fd | 2005-02-19 08:18:05 +0000 | [diff] [blame] | 2079 | }else{ |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 2080 | if( pOp->p3type==P3_MEM ){ |
danielk1977 | aee18ef | 2005-03-09 12:26:50 +0000 | [diff] [blame] | 2081 | sqlite3VdbeMemShallowCopy(pTos, (Mem *)(pOp->p3), MEM_Static); |
| 2082 | }else{ |
| 2083 | pTos->flags = MEM_Null; |
| 2084 | } |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 2085 | } |
drh | febe106 | 2004-08-28 18:17:48 +0000 | [diff] [blame] | 2086 | |
| 2087 | /* If we dynamically allocated space to hold the data (in the |
| 2088 | ** sqlite3VdbeMemFromBtree() call above) then transfer control of that |
drh | cdd536f | 2006-03-17 00:04:03 +0000 | [diff] [blame] | 2089 | ** dynamically allocated space over to the pTos structure. |
drh | febe106 | 2004-08-28 18:17:48 +0000 | [diff] [blame] | 2090 | ** This prevents a memory copy. |
| 2091 | */ |
| 2092 | if( (sMem.flags & MEM_Dyn)!=0 ){ |
| 2093 | assert( pTos->flags & MEM_Ephem ); |
| 2094 | assert( pTos->flags & (MEM_Str|MEM_Blob) ); |
| 2095 | assert( pTos->z==sMem.z ); |
| 2096 | assert( sMem.flags & MEM_Term ); |
| 2097 | pTos->flags &= ~MEM_Ephem; |
| 2098 | pTos->flags |= MEM_Dyn|MEM_Term; |
danielk1977 | b1bc953 | 2004-05-22 03:05:33 +0000 | [diff] [blame] | 2099 | } |
drh | febe106 | 2004-08-28 18:17:48 +0000 | [diff] [blame] | 2100 | |
| 2101 | /* pTos->z might be pointing to sMem.zShort[]. Fix that so that we |
| 2102 | ** can abandon sMem */ |
| 2103 | rc = sqlite3VdbeMemMakeWriteable(pTos); |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 2104 | |
danielk1977 | 3c9cc8d | 2005-01-17 03:40:08 +0000 | [diff] [blame] | 2105 | op_column_out: |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2106 | break; |
| 2107 | } |
| 2108 | |
drh | 670fb03 | 2004-11-15 23:42:27 +0000 | [diff] [blame] | 2109 | /* Opcode: MakeRecord P1 P2 P3 |
drh | 7a224de | 2004-06-02 01:22:02 +0000 | [diff] [blame] | 2110 | ** |
| 2111 | ** Convert the top abs(P1) entries of the stack into a single entry |
| 2112 | ** suitable for use as a data record in a database table or as a key |
| 2113 | ** in an index. The details of the format are irrelavant as long as |
| 2114 | ** the OP_Column opcode can decode the record later and as long as the |
| 2115 | ** sqlite3VdbeRecordCompare function will correctly compare two encoded |
| 2116 | ** records. Refer to source code comments for the details of the record |
| 2117 | ** format. |
| 2118 | ** |
| 2119 | ** The original stack entries are popped from the stack if P1>0 but |
| 2120 | ** remain on the stack if P1<0. |
| 2121 | ** |
drh | 7f057c9 | 2005-06-24 03:53:06 +0000 | [diff] [blame] | 2122 | ** If P2 is not zero and one or more of the entries are NULL, then jump |
| 2123 | ** to the address given by P2. This feature can be used to skip a |
| 2124 | ** uniqueness test on indices. |
drh | 7a224de | 2004-06-02 01:22:02 +0000 | [diff] [blame] | 2125 | ** |
| 2126 | ** P3 may be a string that is P1 characters long. The nth character of the |
| 2127 | ** string indicates the column affinity that should be used for the nth |
| 2128 | ** field of the index key (i.e. the first character of P3 corresponds to the |
| 2129 | ** lowest element on the stack). |
| 2130 | ** |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 2131 | ** The mapping from character to affinity is given by the SQLITE_AFF_ |
| 2132 | ** macros defined in sqliteInt.h. |
drh | 7a224de | 2004-06-02 01:22:02 +0000 | [diff] [blame] | 2133 | ** |
| 2134 | ** If P3 is NULL then all index fields have the affinity NONE. |
drh | 7f057c9 | 2005-06-24 03:53:06 +0000 | [diff] [blame] | 2135 | ** |
| 2136 | ** See also OP_MakeIdxRec |
drh | 7a224de | 2004-06-02 01:22:02 +0000 | [diff] [blame] | 2137 | */ |
drh | d946db0 | 2005-12-29 19:23:06 +0000 | [diff] [blame] | 2138 | /* Opcode: MakeIdxRec P1 P2 P3 |
drh | 7f057c9 | 2005-06-24 03:53:06 +0000 | [diff] [blame] | 2139 | ** |
| 2140 | ** This opcode works just OP_MakeRecord except that it reads an extra |
| 2141 | ** integer from the stack (thus reading a total of abs(P1+1) entries) |
| 2142 | ** and appends that extra integer to the end of the record as a varint. |
| 2143 | ** This results in an index key. |
| 2144 | */ |
| 2145 | case OP_MakeIdxRec: |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 2146 | case OP_MakeRecord: { |
| 2147 | /* Assuming the record contains N fields, the record format looks |
| 2148 | ** like this: |
| 2149 | ** |
drh | 7a224de | 2004-06-02 01:22:02 +0000 | [diff] [blame] | 2150 | ** ------------------------------------------------------------------------ |
| 2151 | ** | hdr-size | type 0 | type 1 | ... | type N-1 | data0 | ... | data N-1 | |
| 2152 | ** ------------------------------------------------------------------------ |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 2153 | ** |
| 2154 | ** Data(0) is taken from the lowest element of the stack and data(N-1) is |
| 2155 | ** the top of the stack. |
| 2156 | ** |
| 2157 | ** Each type field is a varint representing the serial type of the |
| 2158 | ** corresponding data element (see sqlite3VdbeSerialType()). The |
drh | 7a224de | 2004-06-02 01:22:02 +0000 | [diff] [blame] | 2159 | ** hdr-size field is also a varint which is the offset from the beginning |
| 2160 | ** of the record to data0. |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 2161 | */ |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 2162 | unsigned char *zNewRecord; |
| 2163 | unsigned char *zCsr; |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 2164 | Mem *pRec; |
danielk1977 | f7df9cc | 2004-06-16 12:02:47 +0000 | [diff] [blame] | 2165 | Mem *pRowid = 0; |
danielk1977 | ededfd5 | 2004-06-17 07:53:01 +0000 | [diff] [blame] | 2166 | int nData = 0; /* Number of bytes of data space */ |
| 2167 | int nHdr = 0; /* Number of bytes of header space */ |
| 2168 | int nByte = 0; /* Space required for this record */ |
drh | cb9882a | 2005-03-17 03:15:40 +0000 | [diff] [blame] | 2169 | int nVarint; /* Number of bytes in a varint */ |
danielk1977 | ededfd5 | 2004-06-17 07:53:01 +0000 | [diff] [blame] | 2170 | u32 serial_type; /* Type field */ |
| 2171 | int containsNull = 0; /* True if any of the data fields are NULL */ |
| 2172 | char zTemp[NBFS]; /* Space to hold small records */ |
| 2173 | Mem *pData0; |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 2174 | |
danielk1977 | ededfd5 | 2004-06-17 07:53:01 +0000 | [diff] [blame] | 2175 | int leaveOnStack; /* If true, leave the entries on the stack */ |
| 2176 | int nField; /* Number of fields in the record */ |
| 2177 | int jumpIfNull; /* Jump here if non-zero and any entries are NULL. */ |
| 2178 | int addRowid; /* True to append a rowid column at the end */ |
| 2179 | char *zAffinity; /* The affinity string for the record */ |
drh | d946db0 | 2005-12-29 19:23:06 +0000 | [diff] [blame] | 2180 | int file_format; /* File format to use for encoding */ |
danielk1977 | ededfd5 | 2004-06-17 07:53:01 +0000 | [diff] [blame] | 2181 | |
| 2182 | leaveOnStack = ((pOp->p1<0)?1:0); |
| 2183 | nField = pOp->p1 * (leaveOnStack?-1:1); |
drh | 7f057c9 | 2005-06-24 03:53:06 +0000 | [diff] [blame] | 2184 | jumpIfNull = pOp->p2; |
| 2185 | addRowid = pOp->opcode==OP_MakeIdxRec; |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 2186 | zAffinity = pOp->p3; |
danielk1977 | ededfd5 | 2004-06-17 07:53:01 +0000 | [diff] [blame] | 2187 | |
| 2188 | pData0 = &pTos[1-nField]; |
| 2189 | assert( pData0>=p->aStack ); |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 2190 | containsNull = 0; |
drh | d946db0 | 2005-12-29 19:23:06 +0000 | [diff] [blame] | 2191 | file_format = p->minWriteFileFormat; |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 2192 | |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 2193 | /* Loop through the elements that will make up the record to figure |
| 2194 | ** out how much space is required for the new record. |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 2195 | */ |
| 2196 | for(pRec=pData0; pRec<=pTos; pRec++){ |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 2197 | if( zAffinity ){ |
drh | 8079a0d | 2006-01-12 17:20:50 +0000 | [diff] [blame] | 2198 | applyAffinity(pRec, zAffinity[pRec-pData0], encoding); |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 2199 | } |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 2200 | if( pRec->flags&MEM_Null ){ |
danielk1977 | 452c989 | 2004-05-13 05:16:15 +0000 | [diff] [blame] | 2201 | containsNull = 1; |
| 2202 | } |
drh | d946db0 | 2005-12-29 19:23:06 +0000 | [diff] [blame] | 2203 | serial_type = sqlite3VdbeSerialType(pRec, file_format); |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 2204 | nData += sqlite3VdbeSerialTypeLen(serial_type); |
| 2205 | nHdr += sqlite3VarintLen(serial_type); |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 2206 | } |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 2207 | |
| 2208 | /* If we have to append a varint rowid to this record, set 'rowid' |
| 2209 | ** to the value of the rowid and increase nByte by the amount of space |
| 2210 | ** required to store it and the 0x00 seperator byte. |
| 2211 | */ |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 2212 | if( addRowid ){ |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 2213 | pRowid = &pTos[0-nField]; |
| 2214 | assert( pRowid>=p->aStack ); |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 2215 | sqlite3VdbeMemIntegerify(pRowid); |
drh | d946db0 | 2005-12-29 19:23:06 +0000 | [diff] [blame] | 2216 | serial_type = sqlite3VdbeSerialType(pRowid, 0); |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 2217 | nData += sqlite3VdbeSerialTypeLen(serial_type); |
| 2218 | nHdr += sqlite3VarintLen(serial_type); |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 2219 | } |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 2220 | |
| 2221 | /* Add the initial header varint and total the size */ |
drh | cb9882a | 2005-03-17 03:15:40 +0000 | [diff] [blame] | 2222 | nHdr += nVarint = sqlite3VarintLen(nHdr); |
| 2223 | if( nVarint<sqlite3VarintLen(nHdr) ){ |
| 2224 | nHdr++; |
| 2225 | } |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 2226 | nByte = nHdr+nData; |
| 2227 | |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 2228 | /* Allocate space for the new record. */ |
drh | 51846b5 | 2004-05-28 16:00:21 +0000 | [diff] [blame] | 2229 | if( nByte>sizeof(zTemp) ){ |
| 2230 | zNewRecord = sqliteMallocRaw(nByte); |
| 2231 | if( !zNewRecord ){ |
| 2232 | goto no_mem; |
| 2233 | } |
| 2234 | }else{ |
drh | 2646da7 | 2005-12-09 20:02:05 +0000 | [diff] [blame] | 2235 | zNewRecord = (u8*)zTemp; |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 2236 | } |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 2237 | |
| 2238 | /* Write the record */ |
| 2239 | zCsr = zNewRecord; |
| 2240 | zCsr += sqlite3PutVarint(zCsr, nHdr); |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 2241 | for(pRec=pData0; pRec<=pTos; pRec++){ |
drh | d946db0 | 2005-12-29 19:23:06 +0000 | [diff] [blame] | 2242 | serial_type = sqlite3VdbeSerialType(pRec, file_format); |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 2243 | zCsr += sqlite3PutVarint(zCsr, serial_type); /* serial type */ |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 2244 | } |
| 2245 | if( addRowid ){ |
drh | d946db0 | 2005-12-29 19:23:06 +0000 | [diff] [blame] | 2246 | zCsr += sqlite3PutVarint(zCsr, sqlite3VdbeSerialType(pRowid, 0)); |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 2247 | } |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 2248 | for(pRec=pData0; pRec<=pTos; pRec++){ |
drh | d946db0 | 2005-12-29 19:23:06 +0000 | [diff] [blame] | 2249 | zCsr += sqlite3VdbeSerialPut(zCsr, pRec, file_format); /* serial data */ |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 2250 | } |
| 2251 | if( addRowid ){ |
drh | d946db0 | 2005-12-29 19:23:06 +0000 | [diff] [blame] | 2252 | zCsr += sqlite3VdbeSerialPut(zCsr, pRowid, 0); |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 2253 | } |
drh | 4ff5508 | 2005-03-17 03:52:47 +0000 | [diff] [blame] | 2254 | assert( zCsr==(zNewRecord+nByte) ); |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 2255 | |
danielk1977 | ededfd5 | 2004-06-17 07:53:01 +0000 | [diff] [blame] | 2256 | /* Pop entries off the stack if required. Push the new record on. */ |
| 2257 | if( !leaveOnStack ){ |
danielk1977 | 452c989 | 2004-05-13 05:16:15 +0000 | [diff] [blame] | 2258 | popStack(&pTos, nField+addRowid); |
| 2259 | } |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 2260 | pTos++; |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 2261 | pTos->n = nByte; |
drh | 51846b5 | 2004-05-28 16:00:21 +0000 | [diff] [blame] | 2262 | if( nByte<=sizeof(zTemp) ){ |
danielk1977 | ef2cb63 | 2004-05-29 02:37:19 +0000 | [diff] [blame] | 2263 | assert( zNewRecord==(unsigned char *)zTemp ); |
drh | 51846b5 | 2004-05-28 16:00:21 +0000 | [diff] [blame] | 2264 | pTos->z = pTos->zShort; |
| 2265 | memcpy(pTos->zShort, zTemp, nByte); |
| 2266 | pTos->flags = MEM_Blob | MEM_Short; |
| 2267 | }else{ |
danielk1977 | ef2cb63 | 2004-05-29 02:37:19 +0000 | [diff] [blame] | 2268 | assert( zNewRecord!=(unsigned char *)zTemp ); |
drh | 2646da7 | 2005-12-09 20:02:05 +0000 | [diff] [blame] | 2269 | pTos->z = (char*)zNewRecord; |
drh | 51846b5 | 2004-05-28 16:00:21 +0000 | [diff] [blame] | 2270 | pTos->flags = MEM_Blob | MEM_Dyn; |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 2271 | pTos->xDel = 0; |
drh | 51846b5 | 2004-05-28 16:00:21 +0000 | [diff] [blame] | 2272 | } |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 2273 | pTos->enc = SQLITE_UTF8; /* In case the blob is ever converted to text */ |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 2274 | |
danielk1977 | ededfd5 | 2004-06-17 07:53:01 +0000 | [diff] [blame] | 2275 | /* If a NULL was encountered and jumpIfNull is non-zero, take the jump. */ |
| 2276 | if( jumpIfNull && containsNull ){ |
| 2277 | pc = jumpIfNull - 1; |
danielk1977 | 452c989 | 2004-05-13 05:16:15 +0000 | [diff] [blame] | 2278 | } |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 2279 | break; |
| 2280 | } |
| 2281 | |
drh | 7f0f12e | 2004-05-21 13:39:50 +0000 | [diff] [blame] | 2282 | /* Opcode: Statement P1 * * |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2283 | ** |
drh | 7f0f12e | 2004-05-21 13:39:50 +0000 | [diff] [blame] | 2284 | ** Begin an individual statement transaction which is part of a larger |
| 2285 | ** BEGIN..COMMIT transaction. This is needed so that the statement |
| 2286 | ** can be rolled back after an error without having to roll back the |
| 2287 | ** entire transaction. The statement transaction will automatically |
| 2288 | ** commit when the VDBE halts. |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2289 | ** |
drh | 7f0f12e | 2004-05-21 13:39:50 +0000 | [diff] [blame] | 2290 | ** The statement is begun on the database file with index P1. The main |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2291 | ** database file has an index of 0 and the file used for temporary tables |
| 2292 | ** has an index of 1. |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2293 | */ |
danielk1977 | 7a5147c | 2005-03-29 13:07:00 +0000 | [diff] [blame] | 2294 | case OP_Statement: { /* no-push */ |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2295 | int i = pOp->p1; |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 2296 | Btree *pBt; |
drh | d9cb6ac | 2005-10-20 07:28:17 +0000 | [diff] [blame] | 2297 | if( i>=0 && i<db->nDb && (pBt = db->aDb[i].pBt)!=0 && !(db->autoCommit) ){ |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 2298 | assert( sqlite3BtreeIsInTrans(pBt) ); |
| 2299 | if( !sqlite3BtreeIsInStmt(pBt) ){ |
| 2300 | rc = sqlite3BtreeBeginStmt(pBt); |
| 2301 | } |
| 2302 | } |
| 2303 | break; |
| 2304 | } |
| 2305 | |
| 2306 | /* Opcode: AutoCommit P1 P2 * |
| 2307 | ** |
| 2308 | ** Set the database auto-commit flag to P1 (1 or 0). If P2 is true, roll |
danielk1977 | 46c43ed | 2004-06-30 06:30:25 +0000 | [diff] [blame] | 2309 | ** back any currently active btree transactions. If there are any active |
| 2310 | ** VMs (apart from this one), then the COMMIT or ROLLBACK statement fails. |
drh | 92f02c3 | 2004-09-02 14:57:08 +0000 | [diff] [blame] | 2311 | ** |
| 2312 | ** This instruction causes the VM to halt. |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 2313 | */ |
danielk1977 | 7a5147c | 2005-03-29 13:07:00 +0000 | [diff] [blame] | 2314 | case OP_AutoCommit: { /* no-push */ |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 2315 | u8 i = pOp->p1; |
| 2316 | u8 rollback = pOp->p2; |
| 2317 | |
| 2318 | assert( i==1 || i==0 ); |
| 2319 | assert( i==1 || rollback==0 ); |
| 2320 | |
drh | 92f02c3 | 2004-09-02 14:57:08 +0000 | [diff] [blame] | 2321 | assert( db->activeVdbeCnt>0 ); /* At least this one VM is active */ |
danielk1977 | 46c43ed | 2004-06-30 06:30:25 +0000 | [diff] [blame] | 2322 | |
| 2323 | if( db->activeVdbeCnt>1 && i && !db->autoCommit ){ |
| 2324 | /* If this instruction implements a COMMIT or ROLLBACK, other VMs are |
| 2325 | ** still running, and a transaction is active, return an error indicating |
| 2326 | ** that the other VMs must complete first. |
| 2327 | */ |
| 2328 | sqlite3SetString(&p->zErrMsg, "cannot ", rollback?"rollback":"commit", |
drh | f93339d | 2006-01-03 15:16:26 +0000 | [diff] [blame] | 2329 | " transaction - SQL statements in progress", (char*)0); |
danielk1977 | 46c43ed | 2004-06-30 06:30:25 +0000 | [diff] [blame] | 2330 | rc = SQLITE_ERROR; |
| 2331 | }else if( i!=db->autoCommit ){ |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 2332 | if( pOp->p2 ){ |
drh | 92f02c3 | 2004-09-02 14:57:08 +0000 | [diff] [blame] | 2333 | assert( i==1 ); |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 2334 | sqlite3RollbackAll(db); |
danielk1977 | f3f06bb | 2005-12-16 15:24:28 +0000 | [diff] [blame] | 2335 | db->autoCommit = 1; |
| 2336 | }else{ |
| 2337 | db->autoCommit = i; |
| 2338 | if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){ |
| 2339 | p->pTos = pTos; |
| 2340 | p->pc = pc; |
| 2341 | db->autoCommit = 1-i; |
| 2342 | p->rc = SQLITE_BUSY; |
| 2343 | return SQLITE_BUSY; |
| 2344 | } |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 2345 | } |
drh | 92f02c3 | 2004-09-02 14:57:08 +0000 | [diff] [blame] | 2346 | return SQLITE_DONE; |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 2347 | }else{ |
| 2348 | sqlite3SetString(&p->zErrMsg, |
| 2349 | (!i)?"cannot start a transaction within a transaction":( |
| 2350 | (rollback)?"cannot rollback - no transaction is active": |
drh | f93339d | 2006-01-03 15:16:26 +0000 | [diff] [blame] | 2351 | "cannot commit - no transaction is active"), (char*)0); |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 2352 | |
| 2353 | rc = SQLITE_ERROR; |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2354 | } |
| 2355 | break; |
| 2356 | } |
| 2357 | |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 2358 | /* Opcode: Transaction P1 P2 * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2359 | ** |
| 2360 | ** Begin a transaction. The transaction ends when a Commit or Rollback |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2361 | ** opcode is encountered. Depending on the ON CONFLICT setting, the |
| 2362 | ** transaction might also be rolled back if an error is encountered. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2363 | ** |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2364 | ** P1 is the index of the database file on which the transaction is |
| 2365 | ** started. Index 0 is the main database file and index 1 is the |
| 2366 | ** file used for temporary tables. |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 2367 | ** |
drh | 8024205 | 2004-06-09 00:48:12 +0000 | [diff] [blame] | 2368 | ** If P2 is non-zero, then a write-transaction is started. A RESERVED lock is |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 2369 | ** obtained on the database file when a write-transaction is started. No |
drh | 8024205 | 2004-06-09 00:48:12 +0000 | [diff] [blame] | 2370 | ** other process can start another write transaction while this transaction is |
| 2371 | ** underway. Starting a write transaction also creates a rollback journal. A |
| 2372 | ** write transaction must be started before any changes can be made to the |
drh | 684917c | 2004-10-05 02:41:42 +0000 | [diff] [blame] | 2373 | ** database. If P2 is 2 or greater then an EXCLUSIVE lock is also obtained |
| 2374 | ** on the file. |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 2375 | ** |
| 2376 | ** If P2 is zero, then a read-lock is obtained on the database file. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2377 | */ |
danielk1977 | 7a5147c | 2005-03-29 13:07:00 +0000 | [diff] [blame] | 2378 | case OP_Transaction: { /* no-push */ |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2379 | int i = pOp->p1; |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 2380 | Btree *pBt; |
| 2381 | |
drh | 8bf8dc9 | 2003-05-17 17:35:10 +0000 | [diff] [blame] | 2382 | assert( i>=0 && i<db->nDb ); |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 2383 | pBt = db->aDb[i].pBt; |
| 2384 | |
danielk1977 | 24162fe | 2004-06-04 06:22:00 +0000 | [diff] [blame] | 2385 | if( pBt ){ |
danielk1977 | 40b38dc | 2004-06-26 08:38:24 +0000 | [diff] [blame] | 2386 | rc = sqlite3BtreeBeginTrans(pBt, pOp->p2); |
danielk1977 | 24162fe | 2004-06-04 06:22:00 +0000 | [diff] [blame] | 2387 | if( rc==SQLITE_BUSY ){ |
danielk1977 | 2a764eb | 2004-06-12 01:43:26 +0000 | [diff] [blame] | 2388 | p->pc = pc; |
| 2389 | p->rc = SQLITE_BUSY; |
| 2390 | p->pTos = pTos; |
| 2391 | return SQLITE_BUSY; |
danielk1977 | 24162fe | 2004-06-04 06:22:00 +0000 | [diff] [blame] | 2392 | } |
danielk1977 | 2a764eb | 2004-06-12 01:43:26 +0000 | [diff] [blame] | 2393 | if( rc!=SQLITE_OK && rc!=SQLITE_READONLY /* && rc!=SQLITE_BUSY */ ){ |
danielk1977 | 24162fe | 2004-06-04 06:22:00 +0000 | [diff] [blame] | 2394 | goto abort_due_to_error; |
drh | 90bfcda | 2001-09-23 19:46:51 +0000 | [diff] [blame] | 2395 | } |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 2396 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2397 | break; |
| 2398 | } |
| 2399 | |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2400 | /* Opcode: ReadCookie P1 P2 * |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 2401 | ** |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2402 | ** Read cookie number P2 from database P1 and push it onto the stack. |
| 2403 | ** P2==0 is the schema version. P2==1 is the database format. |
| 2404 | ** P2==2 is the recommended pager cache size, and so forth. P1==0 is |
| 2405 | ** the main database file and P1==1 is the database file used to store |
| 2406 | ** temporary tables. |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 2407 | ** |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 2408 | ** There must be a read-lock on the database (either a transaction |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2409 | ** must be started or there must be an open cursor) before |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 2410 | ** executing this instruction. |
| 2411 | */ |
| 2412 | case OP_ReadCookie: { |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 2413 | int iMeta; |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 2414 | assert( pOp->p2<SQLITE_N_BTREE_META ); |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2415 | assert( pOp->p1>=0 && pOp->p1<db->nDb ); |
| 2416 | assert( db->aDb[pOp->p1].pBt!=0 ); |
drh | a3b321d | 2004-05-11 09:31:31 +0000 | [diff] [blame] | 2417 | /* The indexing of meta values at the schema layer is off by one from |
| 2418 | ** the indexing in the btree layer. The btree considers meta[0] to |
| 2419 | ** be the number of free pages in the database (a read-only value) |
| 2420 | ** and meta[1] to be the schema cookie. The schema layer considers |
| 2421 | ** meta[1] to be the schema cookie. So we have to shift the index |
| 2422 | ** by one in the following statement. |
| 2423 | */ |
danielk1977 | e0d4b06 | 2004-06-28 01:11:46 +0000 | [diff] [blame] | 2424 | rc = sqlite3BtreeGetMeta(db->aDb[pOp->p1].pBt, 1 + pOp->p2, (u32 *)&iMeta); |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 2425 | pTos++; |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 2426 | pTos->i = iMeta; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 2427 | pTos->flags = MEM_Int; |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 2428 | break; |
| 2429 | } |
| 2430 | |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2431 | /* Opcode: SetCookie P1 P2 * |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 2432 | ** |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2433 | ** Write the top of the stack into cookie number P2 of database P1. |
| 2434 | ** P2==0 is the schema version. P2==1 is the database format. |
| 2435 | ** P2==2 is the recommended pager cache size, and so forth. P1==0 is |
| 2436 | ** the main database file and P1==1 is the database file used to store |
| 2437 | ** temporary tables. |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 2438 | ** |
| 2439 | ** A transaction must be started before executing this opcode. |
| 2440 | */ |
danielk1977 | 7a5147c | 2005-03-29 13:07:00 +0000 | [diff] [blame] | 2441 | case OP_SetCookie: { /* no-push */ |
drh | 3f7d4e4 | 2004-07-24 14:35:58 +0000 | [diff] [blame] | 2442 | Db *pDb; |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 2443 | assert( pOp->p2<SQLITE_N_BTREE_META ); |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2444 | assert( pOp->p1>=0 && pOp->p1<db->nDb ); |
drh | 3f7d4e4 | 2004-07-24 14:35:58 +0000 | [diff] [blame] | 2445 | pDb = &db->aDb[pOp->p1]; |
| 2446 | assert( pDb->pBt!=0 ); |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 2447 | assert( pTos>=p->aStack ); |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 2448 | sqlite3VdbeMemIntegerify(pTos); |
drh | a3b321d | 2004-05-11 09:31:31 +0000 | [diff] [blame] | 2449 | /* See note about index shifting on OP_ReadCookie */ |
drh | 3f7d4e4 | 2004-07-24 14:35:58 +0000 | [diff] [blame] | 2450 | rc = sqlite3BtreeUpdateMeta(pDb->pBt, 1+pOp->p2, (int)pTos->i); |
| 2451 | if( pOp->p2==0 ){ |
| 2452 | /* When the schema cookie changes, record the new cookie internally */ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2453 | pDb->pSchema->schema_cookie = pTos->i; |
drh | 3f7d4e4 | 2004-07-24 14:35:58 +0000 | [diff] [blame] | 2454 | db->flags |= SQLITE_InternChanges; |
drh | d28bcb3 | 2005-12-21 14:43:11 +0000 | [diff] [blame] | 2455 | }else if( pOp->p2==1 ){ |
| 2456 | /* Record changes in the file format */ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2457 | pDb->pSchema->file_format = pTos->i; |
drh | 3f7d4e4 | 2004-07-24 14:35:58 +0000 | [diff] [blame] | 2458 | } |
drh | 6a6124e | 2004-06-27 01:56:33 +0000 | [diff] [blame] | 2459 | assert( (pTos->flags & MEM_Dyn)==0 ); |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 2460 | pTos--; |
drh | fd426c6 | 2006-01-30 15:34:22 +0000 | [diff] [blame] | 2461 | if( pOp->p1==1 ){ |
| 2462 | /* Invalidate all prepared statements whenever the TEMP database |
| 2463 | ** schema is changed. Ticket #1644 */ |
| 2464 | sqlite3ExpirePreparedStatements(db); |
| 2465 | } |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 2466 | break; |
| 2467 | } |
| 2468 | |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 2469 | /* Opcode: VerifyCookie P1 P2 * |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 2470 | ** |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2471 | ** Check the value of global database parameter number 0 (the |
| 2472 | ** schema version) and make sure it is equal to P2. |
| 2473 | ** P1 is the database number which is 0 for the main database file |
| 2474 | ** and 1 for the file holding temporary tables and some higher number |
| 2475 | ** for auxiliary databases. |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 2476 | ** |
| 2477 | ** The cookie changes its value whenever the database schema changes. |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2478 | ** This operation is used to detect when that the cookie has changed |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 2479 | ** and that the current process needs to reread the schema. |
| 2480 | ** |
| 2481 | ** Either a transaction needs to have been started or an OP_Open needs |
| 2482 | ** to be executed (to establish a read lock) before this opcode is |
| 2483 | ** invoked. |
| 2484 | */ |
danielk1977 | 7a5147c | 2005-03-29 13:07:00 +0000 | [diff] [blame] | 2485 | case OP_VerifyCookie: { /* no-push */ |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 2486 | int iMeta; |
drh | c275b4e | 2004-07-19 17:25:24 +0000 | [diff] [blame] | 2487 | Btree *pBt; |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2488 | assert( pOp->p1>=0 && pOp->p1<db->nDb ); |
drh | c275b4e | 2004-07-19 17:25:24 +0000 | [diff] [blame] | 2489 | pBt = db->aDb[pOp->p1].pBt; |
| 2490 | if( pBt ){ |
| 2491 | rc = sqlite3BtreeGetMeta(pBt, 1, (u32 *)&iMeta); |
| 2492 | }else{ |
| 2493 | rc = SQLITE_OK; |
| 2494 | iMeta = 0; |
| 2495 | } |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 2496 | if( rc==SQLITE_OK && iMeta!=pOp->p2 ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2497 | sqlite3SetString(&p->zErrMsg, "database schema has changed", (char*)0); |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 2498 | rc = SQLITE_SCHEMA; |
| 2499 | } |
| 2500 | break; |
| 2501 | } |
| 2502 | |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2503 | /* Opcode: OpenRead P1 P2 P3 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2504 | ** |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2505 | ** Open a read-only cursor for the database table whose root page is |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2506 | ** P2 in a database file. The database file is determined by an |
| 2507 | ** integer from the top of the stack. 0 means the main database and |
| 2508 | ** 1 means the database used for temporary tables. Give the new |
| 2509 | ** cursor an identifier of P1. The P1 values need not be contiguous |
| 2510 | ** but all P1 values should be small integers. It is an error for |
| 2511 | ** P1 to be negative. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2512 | ** |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2513 | ** 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] | 2514 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2515 | ** There will be a read lock on the database whenever there is an |
| 2516 | ** open cursor. If the database was unlocked prior to this instruction |
| 2517 | ** then a read lock is acquired as part of this instruction. A read |
| 2518 | ** lock allows other processes to read the database but prohibits |
| 2519 | ** any other process from modifying the database. The read lock is |
| 2520 | ** released when all cursors are closed. If this instruction attempts |
| 2521 | ** to get a read lock but fails, the script terminates with an |
| 2522 | ** SQLITE_BUSY error code. |
| 2523 | ** |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 2524 | ** The P3 value is a pointer to a KeyInfo structure that defines the |
| 2525 | ** content and collating sequence of indices. P3 is NULL for cursors |
| 2526 | ** that are not pointing to indices. |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 2527 | ** |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2528 | ** See also OpenWrite. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2529 | */ |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2530 | /* Opcode: OpenWrite P1 P2 P3 |
| 2531 | ** |
| 2532 | ** Open a read/write cursor named P1 on the table or index whose root |
| 2533 | ** page is P2. If P2==0 then take the root page number from the stack. |
| 2534 | ** |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 2535 | ** The P3 value is a pointer to a KeyInfo structure that defines the |
| 2536 | ** content and collating sequence of indices. P3 is NULL for cursors |
| 2537 | ** that are not pointing to indices. |
jplyon | 5a56422 | 2003-06-02 06:15:58 +0000 | [diff] [blame] | 2538 | ** |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2539 | ** This instruction works just like OpenRead except that it opens the cursor |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2540 | ** in read/write mode. For a given table, there can be one or more read-only |
| 2541 | ** cursors or a single read/write cursor but not both. |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 2542 | ** |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2543 | ** See also OpenRead. |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2544 | */ |
danielk1977 | 7a5147c | 2005-03-29 13:07:00 +0000 | [diff] [blame] | 2545 | case OP_OpenRead: /* no-push */ |
| 2546 | case OP_OpenWrite: { /* no-push */ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2547 | int i = pOp->p1; |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 2548 | int p2 = pOp->p2; |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 2549 | int wrFlag; |
| 2550 | Btree *pX; |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2551 | int iDb; |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 2552 | Cursor *pCur; |
drh | d946db0 | 2005-12-29 19:23:06 +0000 | [diff] [blame] | 2553 | Db *pDb; |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2554 | |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 2555 | assert( pTos>=p->aStack ); |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 2556 | sqlite3VdbeMemIntegerify(pTos); |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 2557 | iDb = pTos->i; |
drh | 6a6124e | 2004-06-27 01:56:33 +0000 | [diff] [blame] | 2558 | assert( (pTos->flags & MEM_Dyn)==0 ); |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 2559 | pTos--; |
| 2560 | assert( iDb>=0 && iDb<db->nDb ); |
drh | d946db0 | 2005-12-29 19:23:06 +0000 | [diff] [blame] | 2561 | pDb = &db->aDb[iDb]; |
| 2562 | pX = pDb->pBt; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 2563 | assert( pX!=0 ); |
drh | d946db0 | 2005-12-29 19:23:06 +0000 | [diff] [blame] | 2564 | if( pOp->opcode==OP_OpenWrite ){ |
| 2565 | wrFlag = 1; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2566 | if( pDb->pSchema->file_format < p->minWriteFileFormat ){ |
| 2567 | p->minWriteFileFormat = pDb->pSchema->file_format; |
drh | d946db0 | 2005-12-29 19:23:06 +0000 | [diff] [blame] | 2568 | } |
| 2569 | }else{ |
| 2570 | wrFlag = 0; |
| 2571 | } |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 2572 | if( p2<=0 ){ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 2573 | assert( pTos>=p->aStack ); |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 2574 | sqlite3VdbeMemIntegerify(pTos); |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 2575 | p2 = pTos->i; |
drh | 6a6124e | 2004-06-27 01:56:33 +0000 | [diff] [blame] | 2576 | assert( (pTos->flags & MEM_Dyn)==0 ); |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 2577 | pTos--; |
drh | a611040 | 2005-07-28 20:51:19 +0000 | [diff] [blame] | 2578 | assert( p2>=2 ); |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 2579 | } |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 2580 | assert( i>=0 ); |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 +0000 | [diff] [blame] | 2581 | pCur = allocateCursor(p, i, iDb); |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 2582 | if( pCur==0 ) goto no_mem; |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 2583 | pCur->nullRow = 1; |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 2584 | if( pX==0 ) break; |
danielk1977 | 24162fe | 2004-06-04 06:22:00 +0000 | [diff] [blame] | 2585 | /* We always provide a key comparison function. If the table being |
| 2586 | ** opened is of type INTKEY, the comparision function will be ignored. */ |
| 2587 | rc = sqlite3BtreeCursor(pX, p2, wrFlag, |
| 2588 | sqlite3VdbeRecordCompare, pOp->p3, |
| 2589 | &pCur->pCursor); |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 2590 | if( pOp->p3type==P3_KEYINFO ){ |
| 2591 | pCur->pKeyInfo = (KeyInfo*)pOp->p3; |
danielk1977 | 24162fe | 2004-06-04 06:22:00 +0000 | [diff] [blame] | 2592 | pCur->pIncrKey = &pCur->pKeyInfo->incrKey; |
danielk1977 | 14db266 | 2006-01-09 16:12:04 +0000 | [diff] [blame] | 2593 | pCur->pKeyInfo->enc = ENC(p->db); |
danielk1977 | 24162fe | 2004-06-04 06:22:00 +0000 | [diff] [blame] | 2594 | }else{ |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 2595 | pCur->pKeyInfo = 0; |
danielk1977 | 24162fe | 2004-06-04 06:22:00 +0000 | [diff] [blame] | 2596 | pCur->pIncrKey = &pCur->bogusIncrKey; |
| 2597 | } |
| 2598 | switch( rc ){ |
| 2599 | case SQLITE_BUSY: { |
danielk1977 | 2a764eb | 2004-06-12 01:43:26 +0000 | [diff] [blame] | 2600 | p->pc = pc; |
| 2601 | p->rc = SQLITE_BUSY; |
| 2602 | p->pTos = &pTos[1 + (pOp->p2<=0)]; /* Operands must remain on stack */ |
| 2603 | return SQLITE_BUSY; |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 2604 | } |
danielk1977 | 24162fe | 2004-06-04 06:22:00 +0000 | [diff] [blame] | 2605 | case SQLITE_OK: { |
| 2606 | int flags = sqlite3BtreeFlags(pCur->pCursor); |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 2607 | /* Sanity checking. Only the lower four bits of the flags byte should |
| 2608 | ** be used. Bit 3 (mask 0x08) is unpreditable. The lower 3 bits |
| 2609 | ** (mask 0x07) should be either 5 (intkey+leafdata for tables) or |
| 2610 | ** 2 (zerodata for indices). If these conditions are not met it can |
| 2611 | ** only mean that we are dealing with a corrupt database file |
| 2612 | */ |
| 2613 | if( (flags & 0xf0)!=0 || ((flags & 0x07)!=5 && (flags & 0x07)!=2) ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 2614 | rc = SQLITE_CORRUPT_BKPT; |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 2615 | goto abort_due_to_error; |
| 2616 | } |
| 2617 | pCur->isTable = (flags & BTREE_INTKEY)!=0; |
| 2618 | pCur->isIndex = (flags & BTREE_ZERODATA)!=0; |
| 2619 | /* If P3==0 it means we are expected to open a table. If P3!=0 then |
| 2620 | ** we expect to be opening an index. If this is not what happened, |
| 2621 | ** then the database is corrupt |
| 2622 | */ |
| 2623 | if( (pCur->isTable && pOp->p3type==P3_KEYINFO) |
| 2624 | || (pCur->isIndex && pOp->p3type!=P3_KEYINFO) ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 2625 | rc = SQLITE_CORRUPT_BKPT; |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 2626 | goto abort_due_to_error; |
| 2627 | } |
danielk1977 | 24162fe | 2004-06-04 06:22:00 +0000 | [diff] [blame] | 2628 | break; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2629 | } |
danielk1977 | 24162fe | 2004-06-04 06:22:00 +0000 | [diff] [blame] | 2630 | case SQLITE_EMPTY: { |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 2631 | pCur->isTable = pOp->p3type!=P3_KEYINFO; |
| 2632 | pCur->isIndex = !pCur->isTable; |
danielk1977 | 24162fe | 2004-06-04 06:22:00 +0000 | [diff] [blame] | 2633 | rc = SQLITE_OK; |
| 2634 | break; |
| 2635 | } |
| 2636 | default: { |
| 2637 | goto abort_due_to_error; |
| 2638 | } |
| 2639 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2640 | break; |
| 2641 | } |
| 2642 | |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 2643 | /* Opcode: OpenEphemeral P1 P2 P3 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2644 | ** |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 2645 | ** Open a new cursor P1 to a transient table. |
drh | 9170dd7 | 2005-07-08 17:13:46 +0000 | [diff] [blame] | 2646 | ** The cursor is always opened read/write even if |
| 2647 | ** the main database is read-only. The transient or virtual |
| 2648 | ** table is deleted automatically when the cursor is closed. |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 2649 | ** |
drh | 0342b1f | 2005-09-01 03:07:44 +0000 | [diff] [blame] | 2650 | ** P2 is the number of columns in the virtual table. |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 2651 | ** The cursor points to a BTree table if P3==0 and to a BTree index |
| 2652 | ** if P3 is not 0. If P3 is not NULL, it points to a KeyInfo structure |
| 2653 | ** that defines the format of keys in the index. |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 2654 | ** |
| 2655 | ** This opcode was once called OpenTemp. But that created |
| 2656 | ** confusion because the term "temp table", might refer either |
| 2657 | ** to a TEMP table at the SQL level, or to a table opened by |
| 2658 | ** this opcode. Then this opcode was call OpenVirtual. But |
| 2659 | ** that created confusion with the whole virtual-table idea. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2660 | */ |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 2661 | case OP_OpenEphemeral: { /* no-push */ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2662 | int i = pOp->p1; |
| 2663 | Cursor *pCx; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 2664 | assert( i>=0 ); |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 +0000 | [diff] [blame] | 2665 | pCx = allocateCursor(p, i, -1); |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 2666 | if( pCx==0 ) goto no_mem; |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 2667 | pCx->nullRow = 1; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2668 | rc = sqlite3BtreeFactory(db, 0, 1, TEMP_PAGES, &pCx->pBt); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2669 | if( rc==SQLITE_OK ){ |
danielk1977 | 40b38dc | 2004-06-26 08:38:24 +0000 | [diff] [blame] | 2670 | rc = sqlite3BtreeBeginTrans(pCx->pBt, 1); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2671 | } |
| 2672 | if( rc==SQLITE_OK ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2673 | /* If a transient index is required, create it by calling |
| 2674 | ** sqlite3BtreeCreateTable() with the BTREE_ZERODATA flag before |
| 2675 | ** opening it. If a transient table is required, just use the |
danielk1977 | 0dbe72b | 2004-05-11 04:54:49 +0000 | [diff] [blame] | 2676 | ** automatically created table with root-page 1 (an INTKEY table). |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2677 | */ |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 2678 | if( pOp->p3 ){ |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 2679 | int pgno; |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 2680 | assert( pOp->p3type==P3_KEYINFO ); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2681 | rc = sqlite3BtreeCreateTable(pCx->pBt, &pgno, BTREE_ZERODATA); |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 2682 | if( rc==SQLITE_OK ){ |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 2683 | assert( pgno==MASTER_ROOT+1 ); |
drh | 7a224de | 2004-06-02 01:22:02 +0000 | [diff] [blame] | 2684 | rc = sqlite3BtreeCursor(pCx->pBt, pgno, 1, sqlite3VdbeRecordCompare, |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 2685 | pOp->p3, &pCx->pCursor); |
| 2686 | pCx->pKeyInfo = (KeyInfo*)pOp->p3; |
danielk1977 | 14db266 | 2006-01-09 16:12:04 +0000 | [diff] [blame] | 2687 | pCx->pKeyInfo->enc = ENC(p->db); |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 2688 | pCx->pIncrKey = &pCx->pKeyInfo->incrKey; |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 2689 | } |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 2690 | pCx->isTable = 0; |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 2691 | }else{ |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 2692 | rc = sqlite3BtreeCursor(pCx->pBt, MASTER_ROOT, 1, 0, 0, &pCx->pCursor); |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 2693 | pCx->isTable = 1; |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 2694 | pCx->pIncrKey = &pCx->bogusIncrKey; |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 2695 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2696 | } |
drh | 0342b1f | 2005-09-01 03:07:44 +0000 | [diff] [blame] | 2697 | pCx->nField = pOp->p2; |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 2698 | pCx->isIndex = !pCx->isTable; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2699 | break; |
| 2700 | } |
| 2701 | |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 2702 | /* Opcode: OpenPseudo P1 * * |
| 2703 | ** |
| 2704 | ** Open a new cursor that points to a fake table that contains a single |
| 2705 | ** row of data. Any attempt to write a second row of data causes the |
| 2706 | ** first row to be deleted. All data is deleted when the cursor is |
| 2707 | ** closed. |
| 2708 | ** |
| 2709 | ** A pseudo-table created by this opcode is useful for holding the |
drh | cdd536f | 2006-03-17 00:04:03 +0000 | [diff] [blame] | 2710 | ** NEW or OLD tables in a trigger. Also used to hold the a single |
| 2711 | ** row output from the sorter so that the row can be decomposed into |
| 2712 | ** individual columns using the OP_Column opcode. |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 2713 | */ |
danielk1977 | 7a5147c | 2005-03-29 13:07:00 +0000 | [diff] [blame] | 2714 | case OP_OpenPseudo: { /* no-push */ |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 2715 | int i = pOp->p1; |
| 2716 | Cursor *pCx; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 2717 | assert( i>=0 ); |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 +0000 | [diff] [blame] | 2718 | pCx = allocateCursor(p, i, -1); |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 2719 | if( pCx==0 ) goto no_mem; |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 2720 | pCx->nullRow = 1; |
| 2721 | pCx->pseudoTable = 1; |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 2722 | pCx->pIncrKey = &pCx->bogusIncrKey; |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 2723 | pCx->isTable = 1; |
| 2724 | pCx->isIndex = 0; |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 2725 | break; |
| 2726 | } |
| 2727 | |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2728 | /* Opcode: Close P1 * * |
| 2729 | ** |
| 2730 | ** Close a cursor previously opened as P1. If P1 is not |
| 2731 | ** currently open, this instruction is a no-op. |
| 2732 | */ |
danielk1977 | 7a5147c | 2005-03-29 13:07:00 +0000 | [diff] [blame] | 2733 | case OP_Close: { /* no-push */ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2734 | int i = pOp->p1; |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 2735 | if( i>=0 && i<p->nCursor ){ |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 2736 | sqlite3VdbeFreeCursor(p->apCsr[i]); |
| 2737 | p->apCsr[i] = 0; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2738 | } |
| 2739 | break; |
| 2740 | } |
| 2741 | |
drh | 7cf6e4d | 2004-05-19 14:56:55 +0000 | [diff] [blame] | 2742 | /* Opcode: MoveGe P1 P2 * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2743 | ** |
| 2744 | ** 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] | 2745 | ** cursor P1 so that it points to the smallest entry that is greater |
| 2746 | ** than or equal to the key that was popped ffrom the stack. |
| 2747 | ** If there are no records greater than or equal to the key and P2 |
| 2748 | ** is not zero, then jump to P2. |
| 2749 | ** |
| 2750 | ** See also: Found, NotFound, Distinct, MoveLt, MoveGt, MoveLe |
| 2751 | */ |
| 2752 | /* Opcode: MoveGt P1 P2 * |
| 2753 | ** |
| 2754 | ** Pop the top of the stack and use its value as a key. Reposition |
| 2755 | ** cursor P1 so that it points to the smallest entry that is greater |
| 2756 | ** than the key from the stack. |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 2757 | ** 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] | 2758 | ** then jump to P2. |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2759 | ** |
drh | 7cf6e4d | 2004-05-19 14:56:55 +0000 | [diff] [blame] | 2760 | ** See also: Found, NotFound, Distinct, MoveLt, MoveGe, MoveLe |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2761 | */ |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 2762 | /* Opcode: MoveLt P1 P2 * |
| 2763 | ** |
| 2764 | ** 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] | 2765 | ** cursor P1 so that it points to the largest entry that is less |
| 2766 | ** than the key from the stack. |
| 2767 | ** If there are no records less than the key and P2 is not zero, |
| 2768 | ** then jump to P2. |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 2769 | ** |
drh | 7cf6e4d | 2004-05-19 14:56:55 +0000 | [diff] [blame] | 2770 | ** See also: Found, NotFound, Distinct, MoveGt, MoveGe, MoveLe |
| 2771 | */ |
| 2772 | /* Opcode: MoveLe P1 P2 * |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 2773 | ** |
drh | 7cf6e4d | 2004-05-19 14:56:55 +0000 | [diff] [blame] | 2774 | ** Pop the top of the stack and use its value as a key. Reposition |
| 2775 | ** cursor P1 so that it points to the largest entry that is less than |
| 2776 | ** or equal to the key that was popped from the stack. |
| 2777 | ** If there are no records less than or eqal to the key and P2 is not zero, |
| 2778 | ** then jump to P2. |
| 2779 | ** |
| 2780 | ** See also: Found, NotFound, Distinct, MoveGt, MoveGe, MoveLt |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 2781 | */ |
danielk1977 | 7a5147c | 2005-03-29 13:07:00 +0000 | [diff] [blame] | 2782 | case OP_MoveLt: /* no-push */ |
| 2783 | case OP_MoveLe: /* no-push */ |
| 2784 | case OP_MoveGe: /* no-push */ |
| 2785 | case OP_MoveGt: { /* no-push */ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2786 | int i = pOp->p1; |
drh | 80ff32f | 2001-11-04 18:32:46 +0000 | [diff] [blame] | 2787 | Cursor *pC; |
| 2788 | |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 2789 | assert( pTos>=p->aStack ); |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 2790 | assert( i>=0 && i<p->nCursor ); |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame] | 2791 | pC = p->apCsr[i]; |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 2792 | assert( pC!=0 ); |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 2793 | if( pC->pCursor!=0 ){ |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 2794 | int res, oc; |
drh | 7cf6e4d | 2004-05-19 14:56:55 +0000 | [diff] [blame] | 2795 | oc = pOp->opcode; |
drh | a11846b | 2004-01-07 18:52:56 +0000 | [diff] [blame] | 2796 | pC->nullRow = 0; |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 2797 | *pC->pIncrKey = oc==OP_MoveGt || oc==OP_MoveLe; |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 2798 | if( pC->isTable ){ |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 2799 | i64 iKey; |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 2800 | sqlite3VdbeMemIntegerify(pTos); |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 2801 | iKey = intToKey(pTos->i); |
drh | 7cf6e4d | 2004-05-19 14:56:55 +0000 | [diff] [blame] | 2802 | if( pOp->p2==0 && pOp->opcode==OP_MoveGe ){ |
drh | a11846b | 2004-01-07 18:52:56 +0000 | [diff] [blame] | 2803 | pC->movetoTarget = iKey; |
| 2804 | pC->deferredMoveto = 1; |
drh | 6a6124e | 2004-06-27 01:56:33 +0000 | [diff] [blame] | 2805 | assert( (pTos->flags & MEM_Dyn)==0 ); |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 2806 | pTos--; |
drh | a11846b | 2004-01-07 18:52:56 +0000 | [diff] [blame] | 2807 | break; |
| 2808 | } |
danielk1977 | 2812956 | 2005-01-11 10:25:06 +0000 | [diff] [blame] | 2809 | rc = sqlite3BtreeMoveto(pC->pCursor, 0, (u64)iKey, &res); |
| 2810 | if( rc!=SQLITE_OK ){ |
| 2811 | goto abort_due_to_error; |
| 2812 | } |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 2813 | pC->lastRowid = pTos->i; |
| 2814 | pC->rowidIsValid = res==0; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2815 | }else{ |
drh | 0b2f316 | 2005-12-21 18:36:45 +0000 | [diff] [blame] | 2816 | assert( pTos->flags & MEM_Blob ); |
drh | 8079a0d | 2006-01-12 17:20:50 +0000 | [diff] [blame] | 2817 | /* Stringify(pTos, encoding); */ |
danielk1977 | 2812956 | 2005-01-11 10:25:06 +0000 | [diff] [blame] | 2818 | rc = sqlite3BtreeMoveto(pC->pCursor, pTos->z, pTos->n, &res); |
| 2819 | if( rc!=SQLITE_OK ){ |
| 2820 | goto abort_due_to_error; |
| 2821 | } |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 2822 | pC->rowidIsValid = 0; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2823 | } |
drh | a11846b | 2004-01-07 18:52:56 +0000 | [diff] [blame] | 2824 | pC->deferredMoveto = 0; |
drh | 76873ab | 2006-01-07 18:48:26 +0000 | [diff] [blame] | 2825 | pC->cacheStatus = CACHE_STALE; |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 2826 | *pC->pIncrKey = 0; |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 2827 | sqlite3_search_count++; |
drh | 7cf6e4d | 2004-05-19 14:56:55 +0000 | [diff] [blame] | 2828 | if( oc==OP_MoveGe || oc==OP_MoveGt ){ |
| 2829 | if( res<0 ){ |
danielk1977 | 2812956 | 2005-01-11 10:25:06 +0000 | [diff] [blame] | 2830 | rc = sqlite3BtreeNext(pC->pCursor, &res); |
danielk1977 | 01427a6 | 2005-01-11 13:02:33 +0000 | [diff] [blame] | 2831 | if( rc!=SQLITE_OK ) goto abort_due_to_error; |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 2832 | pC->rowidIsValid = 0; |
drh | 1af3fdb | 2004-07-18 21:33:01 +0000 | [diff] [blame] | 2833 | }else{ |
| 2834 | res = 0; |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 2835 | } |
drh | 7cf6e4d | 2004-05-19 14:56:55 +0000 | [diff] [blame] | 2836 | }else{ |
| 2837 | assert( oc==OP_MoveLt || oc==OP_MoveLe ); |
drh | 1a844c3 | 2002-12-04 22:29:28 +0000 | [diff] [blame] | 2838 | if( res>=0 ){ |
danielk1977 | 01427a6 | 2005-01-11 13:02:33 +0000 | [diff] [blame] | 2839 | rc = sqlite3BtreePrevious(pC->pCursor, &res); |
| 2840 | if( rc!=SQLITE_OK ) goto abort_due_to_error; |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 2841 | pC->rowidIsValid = 0; |
drh | 1a844c3 | 2002-12-04 22:29:28 +0000 | [diff] [blame] | 2842 | }else{ |
| 2843 | /* res might be negative because the table is empty. Check to |
| 2844 | ** see if this is the case. |
| 2845 | */ |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 2846 | res = sqlite3BtreeEof(pC->pCursor); |
drh | 1a844c3 | 2002-12-04 22:29:28 +0000 | [diff] [blame] | 2847 | } |
drh | 1af3fdb | 2004-07-18 21:33:01 +0000 | [diff] [blame] | 2848 | } |
| 2849 | if( res ){ |
| 2850 | if( pOp->p2>0 ){ |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 2851 | pc = pOp->p2 - 1; |
drh | 1af3fdb | 2004-07-18 21:33:01 +0000 | [diff] [blame] | 2852 | }else{ |
| 2853 | pC->nullRow = 1; |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 2854 | } |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 2855 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2856 | } |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 2857 | Release(pTos); |
| 2858 | pTos--; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2859 | break; |
| 2860 | } |
| 2861 | |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2862 | /* Opcode: Distinct P1 P2 * |
| 2863 | ** |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 2864 | ** Use the top of the stack as a record created using MakeRecord. P1 is a |
| 2865 | ** cursor on a table that declared as an index. If that table contains an |
| 2866 | ** entry that matches the top of the stack fall thru. If the top of the stack |
| 2867 | ** matches no entry in P1 then jump to P2. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2868 | ** |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 2869 | ** The cursor is left pointing at the matching entry if it exists. The |
| 2870 | ** record on the top of the stack is not popped. |
| 2871 | ** |
| 2872 | ** This instruction is similar to NotFound except that this operation |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2873 | ** does not pop the key from the stack. |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2874 | ** |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 2875 | ** The instruction is used to implement the DISTINCT operator on SELECT |
| 2876 | ** statements. The P1 table is not a true index but rather a record of |
| 2877 | ** all results that have produced so far. |
| 2878 | ** |
drh | d99f706 | 2002-06-08 23:25:08 +0000 | [diff] [blame] | 2879 | ** See also: Found, NotFound, MoveTo, IsUnique, NotExists |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2880 | */ |
| 2881 | /* Opcode: Found P1 P2 * |
| 2882 | ** |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 2883 | ** Top of the stack holds a blob constructed by MakeRecord. P1 is an index. |
| 2884 | ** If an entry that matches the top of the stack exists in P1 then |
| 2885 | ** jump to P2. If the top of the stack does not match any entry in P1 |
| 2886 | ** then fall thru. The P1 cursor is left pointing at the matching entry |
| 2887 | ** if it exists. The blob is popped off the top of the stack. |
| 2888 | ** |
| 2889 | ** This instruction is used to implement the IN operator where the |
| 2890 | ** left-hand side is a SELECT statement. P1 is not a true index but |
| 2891 | ** is instead a temporary index that holds the results of the SELECT |
| 2892 | ** statement. This instruction just checks to see if the left-hand side |
| 2893 | ** of the IN operator (stored on the top of the stack) exists in the |
| 2894 | ** result of the SELECT statement. |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2895 | ** |
drh | d99f706 | 2002-06-08 23:25:08 +0000 | [diff] [blame] | 2896 | ** See also: Distinct, NotFound, MoveTo, IsUnique, NotExists |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2897 | */ |
| 2898 | /* Opcode: NotFound P1 P2 * |
| 2899 | ** |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 2900 | ** The top of the stack holds a blob constructed by MakeRecord. P1 is |
| 2901 | ** an index. If no entry exists in P1 that matches the blob then jump |
| 2902 | ** to P1. If an entry does existing, fall through. The cursor is left |
| 2903 | ** pointing to the entry that matches. The blob is popped from the stack. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2904 | ** |
| 2905 | ** The difference between this operation and Distinct is that |
| 2906 | ** Distinct does not pop the key from the stack. |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2907 | ** |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 2908 | ** See also: Distinct, Found, MoveTo, NotExists, IsUnique |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2909 | */ |
danielk1977 | 7a5147c | 2005-03-29 13:07:00 +0000 | [diff] [blame] | 2910 | case OP_Distinct: /* no-push */ |
| 2911 | case OP_NotFound: /* no-push */ |
| 2912 | case OP_Found: { /* no-push */ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2913 | int i = pOp->p1; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2914 | int alreadyExists = 0; |
drh | 80ff32f | 2001-11-04 18:32:46 +0000 | [diff] [blame] | 2915 | Cursor *pC; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 2916 | assert( pTos>=p->aStack ); |
| 2917 | assert( i>=0 && i<p->nCursor ); |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 2918 | assert( p->apCsr[i]!=0 ); |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame] | 2919 | if( (pC = p->apCsr[i])->pCursor!=0 ){ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2920 | int res, rx; |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 2921 | assert( pC->isTable==0 ); |
drh | 8079a0d | 2006-01-12 17:20:50 +0000 | [diff] [blame] | 2922 | Stringify(pTos, encoding); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2923 | rx = sqlite3BtreeMoveto(pC->pCursor, pTos->z, pTos->n, &res); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2924 | alreadyExists = rx==SQLITE_OK && res==0; |
drh | a11846b | 2004-01-07 18:52:56 +0000 | [diff] [blame] | 2925 | pC->deferredMoveto = 0; |
drh | 76873ab | 2006-01-07 18:48:26 +0000 | [diff] [blame] | 2926 | pC->cacheStatus = CACHE_STALE; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2927 | } |
| 2928 | if( pOp->opcode==OP_Found ){ |
| 2929 | if( alreadyExists ) pc = pOp->p2 - 1; |
| 2930 | }else{ |
| 2931 | if( !alreadyExists ) pc = pOp->p2 - 1; |
| 2932 | } |
| 2933 | if( pOp->opcode!=OP_Distinct ){ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 2934 | Release(pTos); |
| 2935 | pTos--; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2936 | } |
| 2937 | break; |
| 2938 | } |
| 2939 | |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 2940 | /* Opcode: IsUnique P1 P2 * |
| 2941 | ** |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 2942 | ** The top of the stack is an integer record number. Call this |
| 2943 | ** record number R. The next on the stack is an index key created |
drh | 3c899a6 | 2006-01-10 18:44:08 +0000 | [diff] [blame] | 2944 | ** using MakeIdxRec. Call it K. This instruction pops R from the |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 2945 | ** stack but it leaves K unchanged. |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 2946 | ** |
drh | 7cf6e4d | 2004-05-19 14:56:55 +0000 | [diff] [blame] | 2947 | ** P1 is an index. So it has no data and its key consists of a |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 2948 | ** record generated by OP_MakeRecord where the last field is the |
| 2949 | ** rowid of the entry that the index refers to. |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 2950 | ** |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 2951 | ** This instruction asks if there is an entry in P1 where the |
drh | 7cf6e4d | 2004-05-19 14:56:55 +0000 | [diff] [blame] | 2952 | ** fields matches K but the rowid is different from R. |
| 2953 | ** If there is no such entry, then there is an immediate |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 2954 | ** jump to P2. If any entry does exist where the index string |
| 2955 | ** matches K but the record number is not R, then the record |
| 2956 | ** number for that entry is pushed onto the stack and control |
| 2957 | ** falls through to the next instruction. |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 2958 | ** |
drh | d99f706 | 2002-06-08 23:25:08 +0000 | [diff] [blame] | 2959 | ** See also: Distinct, NotFound, NotExists, Found |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 2960 | */ |
danielk1977 | 7a5147c | 2005-03-29 13:07:00 +0000 | [diff] [blame] | 2961 | case OP_IsUnique: { /* no-push */ |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 2962 | int i = pOp->p1; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 2963 | Mem *pNos = &pTos[-1]; |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 2964 | Cursor *pCx; |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 2965 | BtCursor *pCrsr; |
danielk1977 | 452c989 | 2004-05-13 05:16:15 +0000 | [diff] [blame] | 2966 | i64 R; |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 2967 | |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 2968 | /* Pop the value R off the top of the stack |
| 2969 | */ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 2970 | assert( pNos>=p->aStack ); |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 2971 | sqlite3VdbeMemIntegerify(pTos); |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 2972 | R = pTos->i; |
drh | 6a6124e | 2004-06-27 01:56:33 +0000 | [diff] [blame] | 2973 | assert( (pTos->flags & MEM_Dyn)==0 ); |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 2974 | pTos--; |
| 2975 | assert( i>=0 && i<=p->nCursor ); |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame] | 2976 | pCx = p->apCsr[i]; |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 2977 | assert( pCx!=0 ); |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 2978 | pCrsr = pCx->pCursor; |
| 2979 | if( pCrsr!=0 ){ |
danielk1977 | f2fa831 | 2006-01-24 13:09:33 +0000 | [diff] [blame] | 2980 | int res; |
danielk1977 | 452c989 | 2004-05-13 05:16:15 +0000 | [diff] [blame] | 2981 | i64 v; /* The record number on the P1 entry that matches K */ |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 2982 | char *zKey; /* The value of K */ |
| 2983 | int nKey; /* Number of bytes in K */ |
danielk1977 | 452c989 | 2004-05-13 05:16:15 +0000 | [diff] [blame] | 2984 | int len; /* Number of bytes in K without the rowid at the end */ |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 2985 | int szRowid; /* Size of the rowid column at the end of zKey */ |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 2986 | |
| 2987 | /* Make sure K is a string and make zKey point to K |
| 2988 | */ |
drh | 8079a0d | 2006-01-12 17:20:50 +0000 | [diff] [blame] | 2989 | Stringify(pNos, encoding); |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 2990 | zKey = pNos->z; |
| 2991 | nKey = pNos->n; |
danielk1977 | 452c989 | 2004-05-13 05:16:15 +0000 | [diff] [blame] | 2992 | |
drh | 7416170 | 2006-02-24 02:53:49 +0000 | [diff] [blame] | 2993 | szRowid = sqlite3VdbeIdxRowidLen((u8*)zKey); |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 2994 | len = nKey-szRowid; |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 2995 | |
drh | a3b321d | 2004-05-11 09:31:31 +0000 | [diff] [blame] | 2996 | /* 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] | 2997 | ** If there is no such entry, jump immediately to P2. |
| 2998 | */ |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2999 | assert( pCx->deferredMoveto==0 ); |
drh | 76873ab | 2006-01-07 18:48:26 +0000 | [diff] [blame] | 3000 | pCx->cacheStatus = CACHE_STALE; |
danielk1977 | 452c989 | 2004-05-13 05:16:15 +0000 | [diff] [blame] | 3001 | rc = sqlite3BtreeMoveto(pCrsr, zKey, len, &res); |
danielk1977 | f011300 | 2006-01-24 12:09:17 +0000 | [diff] [blame] | 3002 | if( rc!=SQLITE_OK ){ |
| 3003 | goto abort_due_to_error; |
| 3004 | } |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 3005 | if( res<0 ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3006 | rc = sqlite3BtreeNext(pCrsr, &res); |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 3007 | if( res ){ |
| 3008 | pc = pOp->p2 - 1; |
| 3009 | break; |
| 3010 | } |
| 3011 | } |
drh | 2646da7 | 2005-12-09 20:02:05 +0000 | [diff] [blame] | 3012 | rc = sqlite3VdbeIdxKeyCompare(pCx, len, (u8*)zKey, &res); |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 3013 | if( rc!=SQLITE_OK ) goto abort_due_to_error; |
| 3014 | if( res>0 ){ |
| 3015 | pc = pOp->p2 - 1; |
| 3016 | break; |
| 3017 | } |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 3018 | |
| 3019 | /* 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] | 3020 | ** the final entry (the rowid) matches K. Check to see if the |
| 3021 | ** final rowid column is different from R. If it equals R then jump |
danielk1977 | 452c989 | 2004-05-13 05:16:15 +0000 | [diff] [blame] | 3022 | ** immediately to P2. |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 3023 | */ |
danielk1977 | 452c989 | 2004-05-13 05:16:15 +0000 | [diff] [blame] | 3024 | rc = sqlite3VdbeIdxRowid(pCrsr, &v); |
| 3025 | if( rc!=SQLITE_OK ){ |
| 3026 | goto abort_due_to_error; |
| 3027 | } |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 3028 | if( v==R ){ |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 3029 | pc = pOp->p2 - 1; |
| 3030 | break; |
| 3031 | } |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 3032 | |
danielk1977 | 452c989 | 2004-05-13 05:16:15 +0000 | [diff] [blame] | 3033 | /* The final varint of the key is different from R. Push it onto |
| 3034 | ** the stack. (The record number of an entry that violates a UNIQUE |
| 3035 | ** constraint.) |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 3036 | */ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3037 | pTos++; |
| 3038 | pTos->i = v; |
| 3039 | pTos->flags = MEM_Int; |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 3040 | } |
| 3041 | break; |
| 3042 | } |
| 3043 | |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 3044 | /* Opcode: NotExists P1 P2 * |
| 3045 | ** |
| 3046 | ** Use the top of the stack as a integer key. If a record with that key |
| 3047 | ** does not exist in table of P1, then jump to P2. If the record |
| 3048 | ** does exist, then fall thru. The cursor is left pointing to the |
| 3049 | ** record if it exists. The integer key is popped from the stack. |
| 3050 | ** |
| 3051 | ** The difference between this operation and NotFound is that this |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3052 | ** operation assumes the key is an integer and that P1 is a table whereas |
| 3053 | ** NotFound assumes key is a blob constructed from MakeRecord and |
| 3054 | ** P1 is an index. |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 3055 | ** |
drh | d99f706 | 2002-06-08 23:25:08 +0000 | [diff] [blame] | 3056 | ** See also: Distinct, Found, MoveTo, NotFound, IsUnique |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 3057 | */ |
danielk1977 | 7a5147c | 2005-03-29 13:07:00 +0000 | [diff] [blame] | 3058 | case OP_NotExists: { /* no-push */ |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 3059 | int i = pOp->p1; |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 3060 | Cursor *pC; |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 3061 | BtCursor *pCrsr; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3062 | assert( pTos>=p->aStack ); |
| 3063 | assert( i>=0 && i<p->nCursor ); |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 3064 | assert( p->apCsr[i]!=0 ); |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame] | 3065 | if( (pCrsr = (pC = p->apCsr[i])->pCursor)!=0 ){ |
danielk1977 | 1400b52 | 2005-01-11 11:08:22 +0000 | [diff] [blame] | 3066 | int res; |
danielk1977 | 36a3c70 | 2004-05-11 06:55:14 +0000 | [diff] [blame] | 3067 | u64 iKey; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3068 | assert( pTos->flags & MEM_Int ); |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3069 | assert( p->apCsr[i]->isTable ); |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3070 | iKey = intToKey(pTos->i); |
danielk1977 | 2812956 | 2005-01-11 10:25:06 +0000 | [diff] [blame] | 3071 | rc = sqlite3BtreeMoveto(pCrsr, 0, iKey, &res); |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3072 | pC->lastRowid = pTos->i; |
| 3073 | pC->rowidIsValid = res==0; |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 3074 | pC->nullRow = 0; |
drh | 76873ab | 2006-01-07 18:48:26 +0000 | [diff] [blame] | 3075 | pC->cacheStatus = CACHE_STALE; |
danielk1977 | 2812956 | 2005-01-11 10:25:06 +0000 | [diff] [blame] | 3076 | if( res!=0 ){ |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 3077 | pc = pOp->p2 - 1; |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3078 | pC->rowidIsValid = 0; |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 3079 | } |
| 3080 | } |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3081 | Release(pTos); |
| 3082 | pTos--; |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 3083 | break; |
| 3084 | } |
| 3085 | |
drh | 4db38a7 | 2005-09-01 12:16:28 +0000 | [diff] [blame] | 3086 | /* Opcode: Sequence P1 * * |
| 3087 | ** |
| 3088 | ** Push an integer onto the stack which is the next available |
| 3089 | ** sequence number for cursor P1. The sequence number on the |
| 3090 | ** cursor is incremented after the push. |
| 3091 | */ |
| 3092 | case OP_Sequence: { |
| 3093 | int i = pOp->p1; |
| 3094 | assert( pTos>=p->aStack ); |
| 3095 | assert( i>=0 && i<p->nCursor ); |
| 3096 | assert( p->apCsr[i]!=0 ); |
| 3097 | pTos++; |
| 3098 | pTos->i = p->apCsr[i]->seqCount++; |
| 3099 | pTos->flags = MEM_Int; |
| 3100 | break; |
| 3101 | } |
| 3102 | |
| 3103 | |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3104 | /* Opcode: NewRowid P1 P2 * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3105 | ** |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3106 | ** Get a new integer record number (a.k.a "rowid") used as the key to a table. |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 3107 | ** The record number is not previously used as a key in the database |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 3108 | ** table that cursor P1 points to. The new record number is pushed |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3109 | ** onto the stack. |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 3110 | ** |
| 3111 | ** If P2>0 then P2 is a memory cell that holds the largest previously |
| 3112 | ** generated record number. No new record numbers are allowed to be less |
drh | 2958a4e | 2004-11-12 03:56:15 +0000 | [diff] [blame] | 3113 | ** than this value. When this value reaches its maximum, a SQLITE_FULL |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 3114 | ** error is generated. The P2 memory cell is updated with the generated |
| 3115 | ** record number. This P2 mechanism is used to help implement the |
| 3116 | ** AUTOINCREMENT feature. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3117 | */ |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3118 | case OP_NewRowid: { |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3119 | int i = pOp->p1; |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 3120 | i64 v = 0; |
drh | 80ff32f | 2001-11-04 18:32:46 +0000 | [diff] [blame] | 3121 | Cursor *pC; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3122 | assert( i>=0 && i<p->nCursor ); |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 3123 | assert( p->apCsr[i]!=0 ); |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame] | 3124 | if( (pC = p->apCsr[i])->pCursor==0 ){ |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 3125 | /* The zero initialization above is all that is needed */ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3126 | }else{ |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 3127 | /* The next rowid or record number (different terms for the same |
| 3128 | ** thing) is obtained in a two-step algorithm. |
| 3129 | ** |
| 3130 | ** First we attempt to find the largest existing rowid and add one |
| 3131 | ** to that. But if the largest existing rowid is already the maximum |
| 3132 | ** positive integer, we have to fall through to the second |
| 3133 | ** probabilistic algorithm |
| 3134 | ** |
| 3135 | ** The second algorithm is to select a rowid at random and see if |
| 3136 | ** it already exists in the table. If it does not exist, we have |
| 3137 | ** succeeded. If the random rowid does exist, we select a new one |
| 3138 | ** and try again, up to 1000 times. |
drh | db5ed6d | 2001-09-18 22:17:44 +0000 | [diff] [blame] | 3139 | ** |
| 3140 | ** For a table with less than 2 billion entries, the probability |
| 3141 | ** of not finding a unused rowid is about 1.0e-300. This is a |
| 3142 | ** non-zero probability, but it is still vanishingly small and should |
| 3143 | ** never cause a problem. You are much, much more likely to have a |
| 3144 | ** hardware failure than for this algorithm to fail. |
| 3145 | ** |
drh | af9ff33 | 2002-01-16 21:00:27 +0000 | [diff] [blame] | 3146 | ** The analysis in the previous paragraph assumes that you have a good |
| 3147 | ** source of random numbers. Is a library function like lrand48() |
| 3148 | ** good enough? Maybe. Maybe not. It's hard to know whether there |
| 3149 | ** might be subtle bugs is some implementations of lrand48() that |
| 3150 | ** could cause problems. To avoid uncertainty, SQLite uses its own |
| 3151 | ** random number generator based on the RC4 algorithm. |
| 3152 | ** |
drh | db5ed6d | 2001-09-18 22:17:44 +0000 | [diff] [blame] | 3153 | ** To promote locality of reference for repetitive inserts, the |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 3154 | ** first few attempts at chosing a random rowid pick values just a little |
drh | db5ed6d | 2001-09-18 22:17:44 +0000 | [diff] [blame] | 3155 | ** larger than the previous rowid. This has been shown experimentally |
| 3156 | ** to double the speed of the COPY operation. |
| 3157 | */ |
danielk1977 | f7df9cc | 2004-06-16 12:02:47 +0000 | [diff] [blame] | 3158 | int res, rx=SQLITE_OK, cnt; |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 3159 | i64 x; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3160 | cnt = 0; |
drh | 4e6083c | 2005-02-04 21:13:00 +0000 | [diff] [blame] | 3161 | if( (sqlite3BtreeFlags(pC->pCursor)&(BTREE_INTKEY|BTREE_ZERODATA)) != |
| 3162 | BTREE_INTKEY ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 3163 | rc = SQLITE_CORRUPT_BKPT; |
drh | 4e6083c | 2005-02-04 21:13:00 +0000 | [diff] [blame] | 3164 | goto abort_due_to_error; |
| 3165 | } |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 3166 | assert( (sqlite3BtreeFlags(pC->pCursor) & BTREE_INTKEY)!=0 ); |
| 3167 | assert( (sqlite3BtreeFlags(pC->pCursor) & BTREE_ZERODATA)==0 ); |
drh | fe2093d | 2005-01-20 22:48:47 +0000 | [diff] [blame] | 3168 | |
drh | 75f86a4 | 2005-02-17 00:03:06 +0000 | [diff] [blame] | 3169 | #ifdef SQLITE_32BIT_ROWID |
| 3170 | # define MAX_ROWID 0x7fffffff |
| 3171 | #else |
drh | fe2093d | 2005-01-20 22:48:47 +0000 | [diff] [blame] | 3172 | /* Some compilers complain about constants of the form 0x7fffffffffffffff. |
| 3173 | ** Others complain about 0x7ffffffffffffffffLL. The following macro seems |
| 3174 | ** to provide the constant while making all compilers happy. |
| 3175 | */ |
drh | 75f86a4 | 2005-02-17 00:03:06 +0000 | [diff] [blame] | 3176 | # define MAX_ROWID ( (((u64)0x7fffffff)<<32) | (u64)0xffffffff ) |
| 3177 | #endif |
drh | fe2093d | 2005-01-20 22:48:47 +0000 | [diff] [blame] | 3178 | |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 3179 | if( !pC->useRandomRowid ){ |
drh | 32fbe34 | 2002-10-19 20:16:37 +0000 | [diff] [blame] | 3180 | if( pC->nextRowidValid ){ |
| 3181 | v = pC->nextRowid; |
drh | 3fc190c | 2001-09-14 03:24:23 +0000 | [diff] [blame] | 3182 | }else{ |
danielk1977 | 261919c | 2005-12-06 12:52:59 +0000 | [diff] [blame] | 3183 | rc = sqlite3BtreeLast(pC->pCursor, &res); |
| 3184 | if( rc!=SQLITE_OK ){ |
| 3185 | goto abort_due_to_error; |
| 3186 | } |
drh | 32fbe34 | 2002-10-19 20:16:37 +0000 | [diff] [blame] | 3187 | if( res ){ |
| 3188 | v = 1; |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 3189 | }else{ |
danielk1977 | e0d4b06 | 2004-06-28 01:11:46 +0000 | [diff] [blame] | 3190 | sqlite3BtreeKeySize(pC->pCursor, &v); |
drh | 32fbe34 | 2002-10-19 20:16:37 +0000 | [diff] [blame] | 3191 | v = keyToInt(v); |
drh | 75f86a4 | 2005-02-17 00:03:06 +0000 | [diff] [blame] | 3192 | if( v==MAX_ROWID ){ |
drh | 32fbe34 | 2002-10-19 20:16:37 +0000 | [diff] [blame] | 3193 | pC->useRandomRowid = 1; |
| 3194 | }else{ |
| 3195 | v++; |
| 3196 | } |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 3197 | } |
drh | 3fc190c | 2001-09-14 03:24:23 +0000 | [diff] [blame] | 3198 | } |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 3199 | |
| 3200 | #ifndef SQLITE_OMIT_AUTOINCREMENT |
| 3201 | if( pOp->p2 ){ |
| 3202 | Mem *pMem; |
| 3203 | assert( pOp->p2>0 && pOp->p2<p->nMem ); /* P2 is a valid memory cell */ |
| 3204 | pMem = &p->aMem[pOp->p2]; |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 3205 | sqlite3VdbeMemIntegerify(pMem); |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 3206 | assert( (pMem->flags & MEM_Int)!=0 ); /* mem(P2) holds an integer */ |
drh | 75f86a4 | 2005-02-17 00:03:06 +0000 | [diff] [blame] | 3207 | if( pMem->i==MAX_ROWID || pC->useRandomRowid ){ |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 3208 | rc = SQLITE_FULL; |
| 3209 | goto abort_due_to_error; |
| 3210 | } |
| 3211 | if( v<pMem->i+1 ){ |
| 3212 | v = pMem->i + 1; |
| 3213 | } |
| 3214 | pMem->i = v; |
| 3215 | } |
| 3216 | #endif |
| 3217 | |
drh | 75f86a4 | 2005-02-17 00:03:06 +0000 | [diff] [blame] | 3218 | if( v<MAX_ROWID ){ |
drh | 32fbe34 | 2002-10-19 20:16:37 +0000 | [diff] [blame] | 3219 | pC->nextRowidValid = 1; |
| 3220 | pC->nextRowid = v+1; |
| 3221 | }else{ |
| 3222 | pC->nextRowidValid = 0; |
| 3223 | } |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 3224 | } |
| 3225 | if( pC->useRandomRowid ){ |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 3226 | assert( pOp->p2==0 ); /* SQLITE_FULL must have occurred prior to this */ |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 3227 | v = db->priorNewRowid; |
| 3228 | cnt = 0; |
| 3229 | do{ |
| 3230 | if( v==0 || cnt>2 ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3231 | sqlite3Randomness(sizeof(v), &v); |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 3232 | if( cnt<5 ) v &= 0xffffff; |
| 3233 | }else{ |
drh | bbd82df | 2004-02-11 09:46:30 +0000 | [diff] [blame] | 3234 | unsigned char r; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3235 | sqlite3Randomness(1, &r); |
drh | bbd82df | 2004-02-11 09:46:30 +0000 | [diff] [blame] | 3236 | v += r + 1; |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 3237 | } |
| 3238 | if( v==0 ) continue; |
| 3239 | x = intToKey(v); |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 3240 | rx = sqlite3BtreeMoveto(pC->pCursor, 0, (u64)x, &res); |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 3241 | cnt++; |
| 3242 | }while( cnt<1000 && rx==SQLITE_OK && res==0 ); |
| 3243 | db->priorNewRowid = v; |
| 3244 | if( rx==SQLITE_OK && res==0 ){ |
| 3245 | rc = SQLITE_FULL; |
| 3246 | goto abort_due_to_error; |
| 3247 | } |
drh | 1eaa269 | 2001-09-18 02:02:23 +0000 | [diff] [blame] | 3248 | } |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3249 | pC->rowidIsValid = 0; |
drh | a11846b | 2004-01-07 18:52:56 +0000 | [diff] [blame] | 3250 | pC->deferredMoveto = 0; |
drh | 76873ab | 2006-01-07 18:48:26 +0000 | [diff] [blame] | 3251 | pC->cacheStatus = CACHE_STALE; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3252 | } |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3253 | pTos++; |
| 3254 | pTos->i = v; |
| 3255 | pTos->flags = MEM_Int; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3256 | break; |
| 3257 | } |
| 3258 | |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 +0000 | [diff] [blame] | 3259 | /* Opcode: Insert P1 P2 P3 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3260 | ** |
jplyon | 5a56422 | 2003-06-02 06:15:58 +0000 | [diff] [blame] | 3261 | ** Write an entry into the table of cursor P1. A new entry is |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 3262 | ** created if it doesn't already exist or the data for an existing |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3263 | ** entry is overwritten. The data is the value on the top of the |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 3264 | ** stack. The key is the next value down on the stack. The key must |
| 3265 | ** be an integer. The stack is popped twice by this instruction. |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 3266 | ** |
rdc | b0c374f | 2004-02-20 22:53:38 +0000 | [diff] [blame] | 3267 | ** If the OPFLAG_NCHANGE flag of P2 is set, then the row change count is |
danielk1977 | b28af71 | 2004-06-21 06:50:26 +0000 | [diff] [blame] | 3268 | ** incremented (otherwise not). If the OPFLAG_LASTROWID flag of P2 is set, |
| 3269 | ** then rowid is stored for subsequent return by the |
| 3270 | ** sqlite3_last_insert_rowid() function (otherwise it's unmodified). |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 3271 | ** |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3272 | ** This instruction only works on tables. The equivalent instruction |
| 3273 | ** for indices is OP_IdxInsert. |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 3274 | */ |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3275 | case OP_Insert: { /* no-push */ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3276 | Mem *pNos = &pTos[-1]; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3277 | int i = pOp->p1; |
drh | 32fbe34 | 2002-10-19 20:16:37 +0000 | [diff] [blame] | 3278 | Cursor *pC; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3279 | assert( pNos>=p->aStack ); |
| 3280 | assert( i>=0 && i<p->nCursor ); |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 3281 | assert( p->apCsr[i]!=0 ); |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame] | 3282 | if( ((pC = p->apCsr[i])->pCursor!=0 || pC->pseudoTable) ){ |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3283 | i64 iKey; /* The integer ROWID or key for the record to be inserted */ |
danielk1977 | 5f8d8a8 | 2004-05-11 00:28:42 +0000 | [diff] [blame] | 3284 | |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3285 | assert( pNos->flags & MEM_Int ); |
| 3286 | assert( pC->isTable ); |
| 3287 | iKey = intToKey(pNos->i); |
danielk1977 | 5f8d8a8 | 2004-05-11 00:28:42 +0000 | [diff] [blame] | 3288 | |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3289 | if( pOp->p2 & OPFLAG_NCHANGE ) p->nChange++; |
| 3290 | if( pOp->p2 & OPFLAG_LASTROWID ) db->lastRowid = pNos->i; |
drh | c490e7d | 2006-01-23 17:43:53 +0000 | [diff] [blame] | 3291 | if( pC->nextRowidValid && pNos->i>=pC->nextRowid ){ |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3292 | pC->nextRowidValid = 0; |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 3293 | } |
drh | 78a7583 | 2004-02-13 14:07:12 +0000 | [diff] [blame] | 3294 | if( pTos->flags & MEM_Null ){ |
| 3295 | pTos->z = 0; |
| 3296 | pTos->n = 0; |
| 3297 | }else{ |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 3298 | assert( pTos->flags & (MEM_Blob|MEM_Str) ); |
drh | 78a7583 | 2004-02-13 14:07:12 +0000 | [diff] [blame] | 3299 | } |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3300 | if( pC->pseudoTable ){ |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3301 | sqliteFree(pC->pData); |
| 3302 | pC->iKey = iKey; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3303 | pC->nData = pTos->n; |
| 3304 | if( pTos->flags & MEM_Dyn ){ |
| 3305 | pC->pData = pTos->z; |
| 3306 | pTos->flags = MEM_Null; |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3307 | }else{ |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 3308 | pC->pData = sqliteMallocRaw( pC->nData+2 ); |
danielk1977 | 8def5ea | 2004-06-16 10:39:52 +0000 | [diff] [blame] | 3309 | if( !pC->pData ) goto no_mem; |
| 3310 | memcpy(pC->pData, pTos->z, pC->nData); |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 3311 | pC->pData[pC->nData] = 0; |
| 3312 | pC->pData[pC->nData+1] = 0; |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3313 | } |
| 3314 | pC->nullRow = 0; |
| 3315 | }else{ |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3316 | rc = sqlite3BtreeInsert(pC->pCursor, 0, iKey, pTos->z, pTos->n); |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3317 | } |
danielk1977 | 93758c8 | 2005-01-21 08:13:14 +0000 | [diff] [blame] | 3318 | |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3319 | pC->rowidIsValid = 0; |
drh | a11846b | 2004-01-07 18:52:56 +0000 | [diff] [blame] | 3320 | pC->deferredMoveto = 0; |
drh | 76873ab | 2006-01-07 18:48:26 +0000 | [diff] [blame] | 3321 | pC->cacheStatus = CACHE_STALE; |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 +0000 | [diff] [blame] | 3322 | |
| 3323 | /* Invoke the update-hook if required. */ |
| 3324 | if( rc==SQLITE_OK && db->xUpdateCallback && pOp->p3 ){ |
| 3325 | const char *zDb = db->aDb[pC->iDb].zName; |
| 3326 | const char *zTbl = pOp->p3; |
| 3327 | int op = ((pOp->p2 & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_INSERT); |
| 3328 | assert( pC->isTable ); |
| 3329 | db->xUpdateCallback(db->pUpdateArg, op, zDb, zTbl, iKey); |
| 3330 | assert( pC->iDb>=0 ); |
| 3331 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3332 | } |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3333 | popStack(&pTos, 2); |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 +0000 | [diff] [blame] | 3334 | |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3335 | break; |
| 3336 | } |
| 3337 | |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 +0000 | [diff] [blame] | 3338 | /* Opcode: Delete P1 P2 P3 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3339 | ** |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 3340 | ** Delete the record at which the P1 cursor is currently pointing. |
| 3341 | ** |
| 3342 | ** The cursor will be left pointing at either the next or the previous |
| 3343 | ** 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] | 3344 | ** the next Next instruction will be a no-op. Hence it is OK to delete |
| 3345 | ** a record from within an Next loop. |
drh | c8d30ac | 2002-04-12 10:08:59 +0000 | [diff] [blame] | 3346 | ** |
rdc | b0c374f | 2004-02-20 22:53:38 +0000 | [diff] [blame] | 3347 | ** If the OPFLAG_NCHANGE flag of P2 is set, then the row change count is |
danielk1977 | b28af71 | 2004-06-21 06:50:26 +0000 | [diff] [blame] | 3348 | ** incremented (otherwise not). |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3349 | ** |
| 3350 | ** If P1 is a pseudo-table, then this instruction is a no-op. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3351 | */ |
danielk1977 | 7a5147c | 2005-03-29 13:07:00 +0000 | [diff] [blame] | 3352 | case OP_Delete: { /* no-push */ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3353 | int i = pOp->p1; |
drh | 32fbe34 | 2002-10-19 20:16:37 +0000 | [diff] [blame] | 3354 | Cursor *pC; |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3355 | assert( i>=0 && i<p->nCursor ); |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame] | 3356 | pC = p->apCsr[i]; |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 3357 | assert( pC!=0 ); |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3358 | if( pC->pCursor!=0 ){ |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 +0000 | [diff] [blame] | 3359 | i64 iKey; |
| 3360 | |
| 3361 | /* If the update-hook will be invoked, set iKey to the rowid of the |
| 3362 | ** row being deleted. |
| 3363 | */ |
| 3364 | if( db->xUpdateCallback && pOp->p3 ){ |
| 3365 | assert( pC->isTable ); |
| 3366 | if( pC->rowidIsValid ){ |
| 3367 | iKey = pC->lastRowid; |
| 3368 | }else{ |
| 3369 | rc = sqlite3BtreeKeySize(pC->pCursor, &iKey); |
| 3370 | if( rc ){ |
| 3371 | goto abort_due_to_error; |
| 3372 | } |
| 3373 | iKey = keyToInt(iKey); |
| 3374 | } |
| 3375 | } |
| 3376 | |
drh | 536065a | 2005-01-26 21:55:31 +0000 | [diff] [blame] | 3377 | rc = sqlite3VdbeCursorMoveto(pC); |
drh | 52f159e | 2005-01-27 00:33:21 +0000 | [diff] [blame] | 3378 | if( rc ) goto abort_due_to_error; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3379 | rc = sqlite3BtreeDelete(pC->pCursor); |
drh | 32fbe34 | 2002-10-19 20:16:37 +0000 | [diff] [blame] | 3380 | pC->nextRowidValid = 0; |
drh | 76873ab | 2006-01-07 18:48:26 +0000 | [diff] [blame] | 3381 | pC->cacheStatus = CACHE_STALE; |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 +0000 | [diff] [blame] | 3382 | |
| 3383 | /* Invoke the update-hook if required. */ |
| 3384 | if( rc==SQLITE_OK && db->xUpdateCallback && pOp->p3 ){ |
| 3385 | const char *zDb = db->aDb[pC->iDb].zName; |
| 3386 | const char *zTbl = pOp->p3; |
| 3387 | db->xUpdateCallback(db->pUpdateArg, SQLITE_DELETE, zDb, zTbl, iKey); |
| 3388 | assert( pC->iDb>=0 ); |
| 3389 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3390 | } |
danielk1977 | b28af71 | 2004-06-21 06:50:26 +0000 | [diff] [blame] | 3391 | if( pOp->p2 & OPFLAG_NCHANGE ) p->nChange++; |
rdc | b0c374f | 2004-02-20 22:53:38 +0000 | [diff] [blame] | 3392 | break; |
| 3393 | } |
| 3394 | |
danielk1977 | b28af71 | 2004-06-21 06:50:26 +0000 | [diff] [blame] | 3395 | /* Opcode: ResetCount P1 * * |
rdc | b0c374f | 2004-02-20 22:53:38 +0000 | [diff] [blame] | 3396 | ** |
danielk1977 | b28af71 | 2004-06-21 06:50:26 +0000 | [diff] [blame] | 3397 | ** This opcode resets the VMs internal change counter to 0. If P1 is true, |
| 3398 | ** then the value of the change counter is copied to the database handle |
| 3399 | ** change counter (returned by subsequent calls to sqlite3_changes()) |
| 3400 | ** before it is reset. This is used by trigger programs. |
rdc | b0c374f | 2004-02-20 22:53:38 +0000 | [diff] [blame] | 3401 | */ |
danielk1977 | 7a5147c | 2005-03-29 13:07:00 +0000 | [diff] [blame] | 3402 | case OP_ResetCount: { /* no-push */ |
danielk1977 | b28af71 | 2004-06-21 06:50:26 +0000 | [diff] [blame] | 3403 | if( pOp->p1 ){ |
drh | 344737f | 2004-09-19 00:50:20 +0000 | [diff] [blame] | 3404 | sqlite3VdbeSetChanges(db, p->nChange); |
danielk1977 | b28af71 | 2004-06-21 06:50:26 +0000 | [diff] [blame] | 3405 | } |
| 3406 | p->nChange = 0; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3407 | break; |
| 3408 | } |
| 3409 | |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3410 | /* Opcode: RowData P1 * * |
| 3411 | ** |
| 3412 | ** Push onto the stack the complete row data for cursor P1. |
| 3413 | ** There is no interpretation of the data. It is just copied |
| 3414 | ** onto the stack exactly as it is found in the database file. |
| 3415 | ** |
| 3416 | ** If the cursor is not pointing to a valid row, a NULL is pushed |
| 3417 | ** onto the stack. |
| 3418 | */ |
drh | 143f3c4 | 2004-01-07 20:37:52 +0000 | [diff] [blame] | 3419 | /* Opcode: RowKey P1 * * |
| 3420 | ** |
| 3421 | ** Push onto the stack the complete row key for cursor P1. |
| 3422 | ** There is no interpretation of the key. It is just copied |
| 3423 | ** onto the stack exactly as it is found in the database file. |
| 3424 | ** |
| 3425 | ** If the cursor is not pointing to a valid row, a NULL is pushed |
| 3426 | ** onto the stack. |
| 3427 | */ |
| 3428 | case OP_RowKey: |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3429 | case OP_RowData: { |
| 3430 | int i = pOp->p1; |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3431 | Cursor *pC; |
danielk1977 | e0d4b06 | 2004-06-28 01:11:46 +0000 | [diff] [blame] | 3432 | u32 n; |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3433 | |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3434 | /* Note that RowKey and RowData are really exactly the same instruction */ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3435 | pTos++; |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3436 | assert( i>=0 && i<p->nCursor ); |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame] | 3437 | pC = p->apCsr[i]; |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3438 | assert( pC->isTable || pOp->opcode==OP_RowKey ); |
| 3439 | assert( pC->isIndex || pOp->opcode==OP_RowData ); |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 3440 | assert( pC!=0 ); |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3441 | if( pC->nullRow ){ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3442 | pTos->flags = MEM_Null; |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3443 | }else if( pC->pCursor!=0 ){ |
| 3444 | BtCursor *pCrsr = pC->pCursor; |
drh | 536065a | 2005-01-26 21:55:31 +0000 | [diff] [blame] | 3445 | rc = sqlite3VdbeCursorMoveto(pC); |
drh | 52f159e | 2005-01-27 00:33:21 +0000 | [diff] [blame] | 3446 | if( rc ) goto abort_due_to_error; |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3447 | if( pC->nullRow ){ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3448 | pTos->flags = MEM_Null; |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3449 | break; |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3450 | }else if( pC->isIndex ){ |
danielk1977 | 6490beb | 2004-05-11 06:17:21 +0000 | [diff] [blame] | 3451 | i64 n64; |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3452 | assert( !pC->isTable ); |
danielk1977 | 6490beb | 2004-05-11 06:17:21 +0000 | [diff] [blame] | 3453 | sqlite3BtreeKeySize(pCrsr, &n64); |
| 3454 | n = n64; |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3455 | }else{ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3456 | sqlite3BtreeDataSize(pCrsr, &n); |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3457 | } |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3458 | pTos->n = n; |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3459 | if( n<=NBFS ){ |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 3460 | pTos->flags = MEM_Blob | MEM_Short; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3461 | pTos->z = pTos->zShort; |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3462 | }else{ |
| 3463 | char *z = sqliteMallocRaw( n ); |
| 3464 | if( z==0 ) goto no_mem; |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 3465 | pTos->flags = MEM_Blob | MEM_Dyn; |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 3466 | pTos->xDel = 0; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3467 | pTos->z = z; |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3468 | } |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3469 | if( pC->isIndex ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3470 | sqlite3BtreeKey(pCrsr, 0, n, pTos->z); |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3471 | }else{ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3472 | sqlite3BtreeData(pCrsr, 0, n, pTos->z); |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3473 | } |
| 3474 | }else if( pC->pseudoTable ){ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3475 | pTos->n = pC->nData; |
| 3476 | pTos->z = pC->pData; |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 3477 | pTos->flags = MEM_Blob|MEM_Ephem; |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3478 | }else{ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3479 | pTos->flags = MEM_Null; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3480 | } |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 3481 | pTos->enc = SQLITE_UTF8; /* In case the blob is ever cast to text */ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3482 | break; |
| 3483 | } |
| 3484 | |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3485 | /* Opcode: Rowid P1 * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3486 | ** |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3487 | ** Push onto the stack an integer which is the key of the table entry that |
| 3488 | ** P1 is currently point to. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3489 | */ |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3490 | case OP_Rowid: { |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3491 | int i = pOp->p1; |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3492 | Cursor *pC; |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 3493 | i64 v; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3494 | |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3495 | assert( i>=0 && i<p->nCursor ); |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame] | 3496 | pC = p->apCsr[i]; |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 3497 | assert( pC!=0 ); |
drh | 536065a | 2005-01-26 21:55:31 +0000 | [diff] [blame] | 3498 | rc = sqlite3VdbeCursorMoveto(pC); |
drh | 52f159e | 2005-01-27 00:33:21 +0000 | [diff] [blame] | 3499 | if( rc ) goto abort_due_to_error; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3500 | pTos++; |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3501 | if( pC->rowidIsValid ){ |
| 3502 | v = pC->lastRowid; |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3503 | }else if( pC->pseudoTable ){ |
| 3504 | v = keyToInt(pC->iKey); |
drh | d60ccc6 | 2003-06-24 10:39:46 +0000 | [diff] [blame] | 3505 | }else if( pC->nullRow || pC->pCursor==0 ){ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3506 | pTos->flags = MEM_Null; |
drh | d60ccc6 | 2003-06-24 10:39:46 +0000 | [diff] [blame] | 3507 | break; |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3508 | }else{ |
| 3509 | assert( pC->pCursor!=0 ); |
danielk1977 | e0d4b06 | 2004-06-28 01:11:46 +0000 | [diff] [blame] | 3510 | sqlite3BtreeKeySize(pC->pCursor, &v); |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3511 | v = keyToInt(v); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3512 | } |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3513 | pTos->i = v; |
| 3514 | pTos->flags = MEM_Int; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3515 | break; |
| 3516 | } |
| 3517 | |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 3518 | /* Opcode: NullRow P1 * * |
| 3519 | ** |
| 3520 | ** Move the cursor P1 to a null row. Any OP_Column operations |
| 3521 | ** that occur while the cursor is on the null row will always push |
| 3522 | ** a NULL onto the stack. |
| 3523 | */ |
danielk1977 | 7a5147c | 2005-03-29 13:07:00 +0000 | [diff] [blame] | 3524 | case OP_NullRow: { /* no-push */ |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 3525 | int i = pOp->p1; |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame] | 3526 | Cursor *pC; |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 3527 | |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3528 | assert( i>=0 && i<p->nCursor ); |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame] | 3529 | pC = p->apCsr[i]; |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 3530 | assert( pC!=0 ); |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame] | 3531 | pC->nullRow = 1; |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3532 | pC->rowidIsValid = 0; |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 3533 | break; |
| 3534 | } |
| 3535 | |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 3536 | /* Opcode: Last P1 P2 * |
| 3537 | ** |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3538 | ** The next use of the Rowid or Column or Next instruction for P1 |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 3539 | ** will refer to the last entry in the database table or index. |
| 3540 | ** If the table or index is empty and P2>0, then jump immediately to P2. |
| 3541 | ** If P2 is 0 or if the table or index is not empty, fall through |
| 3542 | ** to the following instruction. |
| 3543 | */ |
danielk1977 | 7a5147c | 2005-03-29 13:07:00 +0000 | [diff] [blame] | 3544 | case OP_Last: { /* no-push */ |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 3545 | int i = pOp->p1; |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3546 | Cursor *pC; |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 3547 | BtCursor *pCrsr; |
| 3548 | |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3549 | assert( i>=0 && i<p->nCursor ); |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame] | 3550 | pC = p->apCsr[i]; |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 3551 | assert( pC!=0 ); |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3552 | if( (pCrsr = pC->pCursor)!=0 ){ |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 3553 | int res; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3554 | rc = sqlite3BtreeLast(pCrsr, &res); |
drh | a11846b | 2004-01-07 18:52:56 +0000 | [diff] [blame] | 3555 | pC->nullRow = res; |
| 3556 | pC->deferredMoveto = 0; |
drh | 76873ab | 2006-01-07 18:48:26 +0000 | [diff] [blame] | 3557 | pC->cacheStatus = CACHE_STALE; |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 3558 | if( res && pOp->p2>0 ){ |
| 3559 | pc = pOp->p2 - 1; |
| 3560 | } |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3561 | }else{ |
| 3562 | pC->nullRow = 0; |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 3563 | } |
| 3564 | break; |
| 3565 | } |
| 3566 | |
drh | 0342b1f | 2005-09-01 03:07:44 +0000 | [diff] [blame] | 3567 | |
| 3568 | /* Opcode: Sort P1 P2 * |
| 3569 | ** |
| 3570 | ** This opcode does exactly the same thing as OP_Rewind except that |
| 3571 | ** it increments an undocumented global variable used for testing. |
| 3572 | ** |
| 3573 | ** Sorting is accomplished by writing records into a sorting index, |
| 3574 | ** then rewinding that index and playing it back from beginning to |
| 3575 | ** end. We use the OP_Sort opcode instead of OP_Rewind to do the |
| 3576 | ** rewinding so that the global variable will be incremented and |
| 3577 | ** regression tests can determine whether or not the optimizer is |
| 3578 | ** correctly optimizing out sorts. |
| 3579 | */ |
| 3580 | case OP_Sort: { /* no-push */ |
| 3581 | sqlite3_sort_count++; |
drh | 4db38a7 | 2005-09-01 12:16:28 +0000 | [diff] [blame] | 3582 | sqlite3_search_count--; |
drh | 0342b1f | 2005-09-01 03:07:44 +0000 | [diff] [blame] | 3583 | /* Fall through into OP_Rewind */ |
| 3584 | } |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 3585 | /* Opcode: Rewind P1 P2 * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3586 | ** |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3587 | ** The next use of the Rowid or Column or Next instruction for P1 |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 3588 | ** will refer to the first entry in the database table or index. |
| 3589 | ** If the table or index is empty and P2>0, then jump immediately to P2. |
| 3590 | ** If P2 is 0 or if the table or index is not empty, fall through |
| 3591 | ** to the following instruction. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3592 | */ |
danielk1977 | 7a5147c | 2005-03-29 13:07:00 +0000 | [diff] [blame] | 3593 | case OP_Rewind: { /* no-push */ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3594 | int i = pOp->p1; |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3595 | Cursor *pC; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3596 | BtCursor *pCrsr; |
drh | f4dada7 | 2004-05-11 09:57:35 +0000 | [diff] [blame] | 3597 | int res; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3598 | |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3599 | assert( i>=0 && i<p->nCursor ); |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame] | 3600 | pC = p->apCsr[i]; |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 3601 | assert( pC!=0 ); |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3602 | if( (pCrsr = pC->pCursor)!=0 ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3603 | rc = sqlite3BtreeFirst(pCrsr, &res); |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3604 | pC->atFirst = res==0; |
drh | a11846b | 2004-01-07 18:52:56 +0000 | [diff] [blame] | 3605 | pC->deferredMoveto = 0; |
drh | 76873ab | 2006-01-07 18:48:26 +0000 | [diff] [blame] | 3606 | pC->cacheStatus = CACHE_STALE; |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3607 | }else{ |
drh | f4dada7 | 2004-05-11 09:57:35 +0000 | [diff] [blame] | 3608 | res = 1; |
| 3609 | } |
| 3610 | pC->nullRow = res; |
| 3611 | if( res && pOp->p2>0 ){ |
| 3612 | pc = pOp->p2 - 1; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3613 | } |
| 3614 | break; |
| 3615 | } |
| 3616 | |
| 3617 | /* Opcode: Next P1 P2 * |
| 3618 | ** |
| 3619 | ** 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] | 3620 | ** table or index. If there are no more key/value pairs then fall through |
| 3621 | ** to the following instruction. But if the cursor advance was successful, |
| 3622 | ** jump immediately to P2. |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 3623 | ** |
| 3624 | ** See also: Prev |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 3625 | */ |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 3626 | /* Opcode: Prev P1 P2 * |
| 3627 | ** |
| 3628 | ** Back up cursor P1 so that it points to the previous key/data pair in its |
| 3629 | ** table or index. If there is no previous key/value pairs then fall through |
| 3630 | ** to the following instruction. But if the cursor backup was successful, |
| 3631 | ** jump immediately to P2. |
| 3632 | */ |
danielk1977 | 7a5147c | 2005-03-29 13:07:00 +0000 | [diff] [blame] | 3633 | case OP_Prev: /* no-push */ |
| 3634 | case OP_Next: { /* no-push */ |
drh | 7b39686 | 2003-01-01 23:06:20 +0000 | [diff] [blame] | 3635 | Cursor *pC; |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 3636 | BtCursor *pCrsr; |
| 3637 | |
drh | caec2f1 | 2003-01-07 02:47:47 +0000 | [diff] [blame] | 3638 | CHECK_FOR_INTERRUPT; |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3639 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame] | 3640 | pC = p->apCsr[pOp->p1]; |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 3641 | assert( pC!=0 ); |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3642 | if( (pCrsr = pC->pCursor)!=0 ){ |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 3643 | int res; |
drh | 7b39686 | 2003-01-01 23:06:20 +0000 | [diff] [blame] | 3644 | if( pC->nullRow ){ |
drh | ad2d830 | 2002-05-24 20:31:36 +0000 | [diff] [blame] | 3645 | res = 1; |
| 3646 | }else{ |
drh | a11846b | 2004-01-07 18:52:56 +0000 | [diff] [blame] | 3647 | assert( pC->deferredMoveto==0 ); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3648 | rc = pOp->opcode==OP_Next ? sqlite3BtreeNext(pCrsr, &res) : |
| 3649 | sqlite3BtreePrevious(pCrsr, &res); |
drh | 7b39686 | 2003-01-01 23:06:20 +0000 | [diff] [blame] | 3650 | pC->nullRow = res; |
drh | 76873ab | 2006-01-07 18:48:26 +0000 | [diff] [blame] | 3651 | pC->cacheStatus = CACHE_STALE; |
drh | ad2d830 | 2002-05-24 20:31:36 +0000 | [diff] [blame] | 3652 | } |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 3653 | if( res==0 ){ |
| 3654 | pc = pOp->p2 - 1; |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 3655 | sqlite3_search_count++; |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 3656 | } |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3657 | }else{ |
| 3658 | pC->nullRow = 1; |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 3659 | } |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3660 | pC->rowidIsValid = 0; |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 3661 | break; |
| 3662 | } |
| 3663 | |
drh | 7f057c9 | 2005-06-24 03:53:06 +0000 | [diff] [blame] | 3664 | /* Opcode: IdxInsert P1 * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3665 | ** |
drh | ee32e0a | 2006-01-10 19:45:49 +0000 | [diff] [blame] | 3666 | ** The top of the stack holds a SQL index key made using either the |
| 3667 | ** MakeIdxRec or MakeRecord instructions. This opcode writes that key |
| 3668 | ** into the index P1. Data for the entry is nil. |
drh | 717e640 | 2001-09-27 03:22:32 +0000 | [diff] [blame] | 3669 | ** |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3670 | ** This instruction only works for indices. The equivalent instruction |
| 3671 | ** for tables is OP_Insert. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3672 | */ |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3673 | case OP_IdxInsert: { /* no-push */ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3674 | int i = pOp->p1; |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 3675 | Cursor *pC; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3676 | BtCursor *pCrsr; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3677 | assert( pTos>=p->aStack ); |
| 3678 | assert( i>=0 && i<p->nCursor ); |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 3679 | assert( p->apCsr[i]!=0 ); |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 3680 | assert( pTos->flags & MEM_Blob ); |
drh | 7f057c9 | 2005-06-24 03:53:06 +0000 | [diff] [blame] | 3681 | assert( pOp->p2==0 ); |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame] | 3682 | if( (pCrsr = (pC = p->apCsr[i])->pCursor)!=0 ){ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3683 | int nKey = pTos->n; |
| 3684 | const char *zKey = pTos->z; |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3685 | assert( pC->isTable==0 ); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3686 | rc = sqlite3BtreeInsert(pCrsr, zKey, nKey, "", 0); |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 3687 | assert( pC->deferredMoveto==0 ); |
drh | 76873ab | 2006-01-07 18:48:26 +0000 | [diff] [blame] | 3688 | pC->cacheStatus = CACHE_STALE; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3689 | } |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3690 | Release(pTos); |
| 3691 | pTos--; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3692 | break; |
| 3693 | } |
| 3694 | |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 3695 | /* Opcode: IdxDelete P1 * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3696 | ** |
drh | ee32e0a | 2006-01-10 19:45:49 +0000 | [diff] [blame] | 3697 | ** The top of the stack is an index key built using the either the |
| 3698 | ** MakeIdxRec or MakeRecord opcodes. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3699 | ** This opcode removes that entry from the index. |
| 3700 | */ |
danielk1977 | 7a5147c | 2005-03-29 13:07:00 +0000 | [diff] [blame] | 3701 | case OP_IdxDelete: { /* no-push */ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3702 | int i = pOp->p1; |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 3703 | Cursor *pC; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3704 | BtCursor *pCrsr; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3705 | assert( pTos>=p->aStack ); |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 3706 | assert( pTos->flags & MEM_Blob ); |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3707 | assert( i>=0 && i<p->nCursor ); |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 3708 | assert( p->apCsr[i]!=0 ); |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame] | 3709 | if( (pCrsr = (pC = p->apCsr[i])->pCursor)!=0 ){ |
danielk1977 | 75bab7d | 2006-01-23 13:09:45 +0000 | [diff] [blame] | 3710 | int res; |
| 3711 | rc = sqlite3BtreeMoveto(pCrsr, pTos->z, pTos->n, &res); |
| 3712 | if( rc==SQLITE_OK && res==0 ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3713 | rc = sqlite3BtreeDelete(pCrsr); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3714 | } |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 3715 | assert( pC->deferredMoveto==0 ); |
drh | 76873ab | 2006-01-07 18:48:26 +0000 | [diff] [blame] | 3716 | pC->cacheStatus = CACHE_STALE; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3717 | } |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3718 | Release(pTos); |
| 3719 | pTos--; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3720 | break; |
| 3721 | } |
| 3722 | |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3723 | /* Opcode: IdxRowid P1 * * |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 3724 | ** |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3725 | ** Push onto the stack an integer which is the last entry in the record at |
| 3726 | ** the end of the index key pointed to by cursor P1. This integer should be |
| 3727 | ** the rowid of the table entry to which this index entry points. |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 3728 | ** |
drh | 3c899a6 | 2006-01-10 18:44:08 +0000 | [diff] [blame] | 3729 | ** See also: Rowid, MakeIdxRec. |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 3730 | */ |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3731 | case OP_IdxRowid: { |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 3732 | int i = pOp->p1; |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 3733 | BtCursor *pCrsr; |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame] | 3734 | Cursor *pC; |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 3735 | |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3736 | assert( i>=0 && i<p->nCursor ); |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 3737 | assert( p->apCsr[i]!=0 ); |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3738 | pTos++; |
drh | febe106 | 2004-08-28 18:17:48 +0000 | [diff] [blame] | 3739 | pTos->flags = MEM_Null; |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame] | 3740 | if( (pCrsr = (pC = p->apCsr[i])->pCursor)!=0 ){ |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 3741 | i64 rowid; |
danielk1977 | 452c989 | 2004-05-13 05:16:15 +0000 | [diff] [blame] | 3742 | |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame] | 3743 | assert( pC->deferredMoveto==0 ); |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3744 | assert( pC->isTable==0 ); |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 3745 | if( pC->nullRow ){ |
| 3746 | pTos->flags = MEM_Null; |
| 3747 | }else{ |
| 3748 | rc = sqlite3VdbeIdxRowid(pCrsr, &rowid); |
| 3749 | if( rc!=SQLITE_OK ){ |
| 3750 | goto abort_due_to_error; |
| 3751 | } |
| 3752 | pTos->flags = MEM_Int; |
| 3753 | pTos->i = rowid; |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 3754 | } |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 3755 | } |
| 3756 | break; |
| 3757 | } |
| 3758 | |
drh | 7cf6e4d | 2004-05-19 14:56:55 +0000 | [diff] [blame] | 3759 | /* Opcode: IdxGT P1 P2 * |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 3760 | ** |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 3761 | ** The top of the stack is an index entry that omits the ROWID. Compare |
| 3762 | ** the top of stack against the index that P1 is currently pointing to. |
| 3763 | ** Ignore the ROWID on the P1 index. |
| 3764 | ** |
| 3765 | ** The top of the stack might have fewer columns that P1. |
| 3766 | ** |
| 3767 | ** If the P1 index entry is greater than the top of the stack |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 3768 | ** then jump to P2. Otherwise fall through to the next instruction. |
| 3769 | ** In either case, the stack is popped once. |
| 3770 | */ |
drh | 7cf6e4d | 2004-05-19 14:56:55 +0000 | [diff] [blame] | 3771 | /* Opcode: IdxGE P1 P2 P3 |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 3772 | ** |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 3773 | ** The top of the stack is an index entry that omits the ROWID. Compare |
| 3774 | ** the top of stack against the index that P1 is currently pointing to. |
| 3775 | ** Ignore the ROWID on the P1 index. |
| 3776 | ** |
| 3777 | ** 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] | 3778 | ** then jump to P2. Otherwise fall through to the next instruction. |
| 3779 | ** In either case, the stack is popped once. |
drh | 772ae62 | 2004-05-19 13:13:08 +0000 | [diff] [blame] | 3780 | ** |
| 3781 | ** If P3 is the "+" string (or any other non-NULL string) then the |
| 3782 | ** index taken from the top of the stack is temporarily increased by |
drh | 7cf6e4d | 2004-05-19 14:56:55 +0000 | [diff] [blame] | 3783 | ** an epsilon prior to the comparison. This make the opcode work |
| 3784 | ** like IdxGT except that if the key from the stack is a prefix of |
| 3785 | ** the key in the cursor, the result is false whereas it would be |
| 3786 | ** true with IdxGT. |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 3787 | */ |
drh | 7cf6e4d | 2004-05-19 14:56:55 +0000 | [diff] [blame] | 3788 | /* Opcode: IdxLT P1 P2 P3 |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 3789 | ** |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 3790 | ** The top of the stack is an index entry that omits the ROWID. Compare |
| 3791 | ** the top of stack against the index that P1 is currently pointing to. |
| 3792 | ** Ignore the ROWID on the P1 index. |
| 3793 | ** |
| 3794 | ** If the P1 index entry is less than the top of the stack |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 3795 | ** then jump to P2. Otherwise fall through to the next instruction. |
| 3796 | ** In either case, the stack is popped once. |
drh | 772ae62 | 2004-05-19 13:13:08 +0000 | [diff] [blame] | 3797 | ** |
| 3798 | ** If P3 is the "+" string (or any other non-NULL string) then the |
| 3799 | ** index taken from the top of the stack is temporarily increased by |
drh | 7cf6e4d | 2004-05-19 14:56:55 +0000 | [diff] [blame] | 3800 | ** an epsilon prior to the comparison. This makes the opcode work |
| 3801 | ** like IdxLE. |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 3802 | */ |
danielk1977 | 7a5147c | 2005-03-29 13:07:00 +0000 | [diff] [blame] | 3803 | case OP_IdxLT: /* no-push */ |
| 3804 | case OP_IdxGT: /* no-push */ |
| 3805 | case OP_IdxGE: { /* no-push */ |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 3806 | int i= pOp->p1; |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame] | 3807 | Cursor *pC; |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 3808 | |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3809 | assert( i>=0 && i<p->nCursor ); |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 3810 | assert( p->apCsr[i]!=0 ); |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3811 | assert( pTos>=p->aStack ); |
drh | 4f26bb6 | 2005-09-08 14:17:20 +0000 | [diff] [blame] | 3812 | if( (pC = p->apCsr[i])->pCursor!=0 ){ |
drh | 0850b53 | 2006-01-31 19:31:43 +0000 | [diff] [blame] | 3813 | int res; |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 3814 | |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 3815 | assert( pTos->flags & MEM_Blob ); /* Created using OP_Make*Key */ |
drh | 8079a0d | 2006-01-12 17:20:50 +0000 | [diff] [blame] | 3816 | Stringify(pTos, encoding); |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame] | 3817 | assert( pC->deferredMoveto==0 ); |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 3818 | *pC->pIncrKey = pOp->p3!=0; |
drh | 7cf6e4d | 2004-05-19 14:56:55 +0000 | [diff] [blame] | 3819 | assert( pOp->p3==0 || pOp->opcode!=OP_IdxGT ); |
drh | 2646da7 | 2005-12-09 20:02:05 +0000 | [diff] [blame] | 3820 | rc = sqlite3VdbeIdxKeyCompare(pC, pTos->n, (u8*)pTos->z, &res); |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 3821 | *pC->pIncrKey = 0; |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 3822 | if( rc!=SQLITE_OK ){ |
| 3823 | break; |
| 3824 | } |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 3825 | if( pOp->opcode==OP_IdxLT ){ |
| 3826 | res = -res; |
| 3827 | }else if( pOp->opcode==OP_IdxGE ){ |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 3828 | res++; |
| 3829 | } |
| 3830 | if( res>0 ){ |
| 3831 | pc = pOp->p2 - 1 ; |
| 3832 | } |
| 3833 | } |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3834 | Release(pTos); |
| 3835 | pTos--; |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 3836 | break; |
| 3837 | } |
| 3838 | |
drh | 143f3c4 | 2004-01-07 20:37:52 +0000 | [diff] [blame] | 3839 | /* Opcode: IdxIsNull P1 P2 * |
| 3840 | ** |
| 3841 | ** The top of the stack contains an index entry such as might be generated |
drh | 3c899a6 | 2006-01-10 18:44:08 +0000 | [diff] [blame] | 3842 | ** by the MakeIdxRec opcode. This routine looks at the first P1 fields of |
drh | 143f3c4 | 2004-01-07 20:37:52 +0000 | [diff] [blame] | 3843 | ** that key. If any of the first P1 fields are NULL, then a jump is made |
| 3844 | ** to address P2. Otherwise we fall straight through. |
| 3845 | ** |
| 3846 | ** The index entry is always popped from the stack. |
| 3847 | */ |
danielk1977 | 7a5147c | 2005-03-29 13:07:00 +0000 | [diff] [blame] | 3848 | case OP_IdxIsNull: { /* no-push */ |
drh | 143f3c4 | 2004-01-07 20:37:52 +0000 | [diff] [blame] | 3849 | int i = pOp->p1; |
drh | 143f3c4 | 2004-01-07 20:37:52 +0000 | [diff] [blame] | 3850 | int k, n; |
| 3851 | const char *z; |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 3852 | u32 serial_type; |
drh | 143f3c4 | 2004-01-07 20:37:52 +0000 | [diff] [blame] | 3853 | |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3854 | assert( pTos>=p->aStack ); |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 3855 | assert( pTos->flags & MEM_Blob ); |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3856 | z = pTos->z; |
| 3857 | n = pTos->n; |
drh | 2646da7 | 2005-12-09 20:02:05 +0000 | [diff] [blame] | 3858 | k = sqlite3GetVarint32((u8*)z, &serial_type); |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 3859 | for(; k<n && i>0; i--){ |
drh | 2646da7 | 2005-12-09 20:02:05 +0000 | [diff] [blame] | 3860 | k += sqlite3GetVarint32((u8*)&z[k], &serial_type); |
drh | a19b775 | 2004-05-30 21:14:58 +0000 | [diff] [blame] | 3861 | if( serial_type==0 ){ /* Serial type 0 is a NULL */ |
drh | 143f3c4 | 2004-01-07 20:37:52 +0000 | [diff] [blame] | 3862 | pc = pOp->p2-1; |
| 3863 | break; |
| 3864 | } |
drh | 143f3c4 | 2004-01-07 20:37:52 +0000 | [diff] [blame] | 3865 | } |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3866 | Release(pTos); |
| 3867 | pTos--; |
drh | 143f3c4 | 2004-01-07 20:37:52 +0000 | [diff] [blame] | 3868 | break; |
| 3869 | } |
| 3870 | |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 3871 | /* Opcode: Destroy P1 P2 * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3872 | ** |
| 3873 | ** Delete an entire database table or index whose root page in the database |
| 3874 | ** file is given by P1. |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 3875 | ** |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 3876 | ** The table being destroyed is in the main database file if P2==0. If |
| 3877 | ** P2==1 then the table to be clear is in the auxiliary database file |
| 3878 | ** that is used to store tables create using CREATE TEMPORARY TABLE. |
| 3879 | ** |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 3880 | ** If AUTOVACUUM is enabled then it is possible that another root page |
| 3881 | ** might be moved into the newly deleted root page in order to keep all |
| 3882 | ** root pages contiguous at the beginning of the database. The former |
| 3883 | ** value of the root page that moved - its value before the move occurred - |
| 3884 | ** is pushed onto the stack. If no page movement was required (because |
| 3885 | ** the table being dropped was already the last one in the database) then |
drh | 81db88e | 2004-12-07 12:29:17 +0000 | [diff] [blame] | 3886 | ** a zero is pushed onto the stack. If AUTOVACUUM is disabled |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 3887 | ** then a zero is pushed onto the stack. |
| 3888 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 3889 | ** See also: Clear |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3890 | */ |
| 3891 | case OP_Destroy: { |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 3892 | int iMoved; |
danielk1977 | e6efa74 | 2004-11-10 11:55:10 +0000 | [diff] [blame] | 3893 | if( db->activeVdbeCnt>1 ){ |
| 3894 | rc = SQLITE_LOCKED; |
| 3895 | }else{ |
| 3896 | assert( db->activeVdbeCnt==1 ); |
| 3897 | rc = sqlite3BtreeDropTable(db->aDb[pOp->p2].pBt, pOp->p1, &iMoved); |
| 3898 | pTos++; |
| 3899 | pTos->flags = MEM_Int; |
| 3900 | pTos->i = iMoved; |
| 3901 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 3902 | if( rc==SQLITE_OK && iMoved!=0 ){ |
| 3903 | sqlite3RootPageMoved(&db->aDb[pOp->p2], iMoved, pOp->p1); |
| 3904 | } |
| 3905 | #endif |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 3906 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3907 | break; |
| 3908 | } |
| 3909 | |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 3910 | /* Opcode: Clear P1 P2 * |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 3911 | ** |
| 3912 | ** Delete all contents of the database table or index whose root page |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 3913 | ** in the database file is given by P1. But, unlike Destroy, do not |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 3914 | ** remove the table or index from the database file. |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 3915 | ** |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 3916 | ** The table being clear is in the main database file if P2==0. If |
| 3917 | ** P2==1 then the table to be clear is in the auxiliary database file |
| 3918 | ** that is used to store tables create using CREATE TEMPORARY TABLE. |
| 3919 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 3920 | ** See also: Destroy |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 3921 | */ |
danielk1977 | 7a5147c | 2005-03-29 13:07:00 +0000 | [diff] [blame] | 3922 | case OP_Clear: { /* no-push */ |
drh | d78901d | 2006-01-05 23:42:50 +0000 | [diff] [blame] | 3923 | |
| 3924 | /* For consistency with the way other features of SQLite operate |
| 3925 | ** with a truncate, we will also skip the update callback. |
| 3926 | */ |
| 3927 | #if 0 |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 +0000 | [diff] [blame] | 3928 | Btree *pBt = db->aDb[pOp->p2].pBt; |
| 3929 | if( db->xUpdateCallback && pOp->p3 ){ |
| 3930 | const char *zDb = db->aDb[pOp->p2].zName; |
| 3931 | const char *zTbl = pOp->p3; |
| 3932 | BtCursor *pCur = 0; |
| 3933 | int fin = 0; |
| 3934 | |
| 3935 | rc = sqlite3BtreeCursor(pBt, pOp->p1, 0, 0, 0, &pCur); |
| 3936 | if( rc!=SQLITE_OK ){ |
| 3937 | goto abort_due_to_error; |
| 3938 | } |
| 3939 | for( |
| 3940 | rc=sqlite3BtreeFirst(pCur, &fin); |
| 3941 | rc==SQLITE_OK && !fin; |
| 3942 | rc=sqlite3BtreeNext(pCur, &fin) |
| 3943 | ){ |
| 3944 | i64 iKey; |
| 3945 | rc = sqlite3BtreeKeySize(pCur, &iKey); |
| 3946 | if( rc ){ |
| 3947 | break; |
| 3948 | } |
| 3949 | iKey = keyToInt(iKey); |
| 3950 | db->xUpdateCallback(db->pUpdateArg, SQLITE_DELETE, zDb, zTbl, iKey); |
| 3951 | } |
| 3952 | sqlite3BtreeCloseCursor(pCur); |
| 3953 | if( rc!=SQLITE_OK ){ |
| 3954 | goto abort_due_to_error; |
| 3955 | } |
| 3956 | } |
drh | d78901d | 2006-01-05 23:42:50 +0000 | [diff] [blame] | 3957 | #endif |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3958 | rc = sqlite3BtreeClearTable(db->aDb[pOp->p2].pBt, pOp->p1); |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 3959 | break; |
| 3960 | } |
| 3961 | |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 3962 | /* Opcode: CreateTable P1 * * |
drh | 5b2fd56 | 2001-09-13 15:21:31 +0000 | [diff] [blame] | 3963 | ** |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 3964 | ** Allocate a new table in the main database file if P2==0 or in the |
| 3965 | ** auxiliary database file if P2==1. Push the page number |
drh | 5b2fd56 | 2001-09-13 15:21:31 +0000 | [diff] [blame] | 3966 | ** for the root page of the new table onto the stack. |
| 3967 | ** |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 3968 | ** The difference between a table and an index is this: A table must |
| 3969 | ** have a 4-byte integer key and can have arbitrary data. An index |
| 3970 | ** has an arbitrary key but no data. |
| 3971 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 3972 | ** See also: CreateIndex |
drh | 5b2fd56 | 2001-09-13 15:21:31 +0000 | [diff] [blame] | 3973 | */ |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 3974 | /* Opcode: CreateIndex P1 * * |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 3975 | ** |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 3976 | ** Allocate a new index in the main database file if P2==0 or in the |
| 3977 | ** auxiliary database file if P2==1. Push the page number of the |
| 3978 | ** root page of the new index onto the stack. |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 3979 | ** |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 3980 | ** See documentation on OP_CreateTable for additional information. |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 3981 | */ |
| 3982 | case OP_CreateIndex: |
drh | 5b2fd56 | 2001-09-13 15:21:31 +0000 | [diff] [blame] | 3983 | case OP_CreateTable: { |
drh | 5b2fd56 | 2001-09-13 15:21:31 +0000 | [diff] [blame] | 3984 | int pgno; |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 3985 | int flags; |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 3986 | Db *pDb; |
| 3987 | assert( pOp->p1>=0 && pOp->p1<db->nDb ); |
| 3988 | pDb = &db->aDb[pOp->p1]; |
| 3989 | assert( pDb->pBt!=0 ); |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 3990 | if( pOp->opcode==OP_CreateTable ){ |
danielk1977 | 9407625 | 2004-05-14 12:16:11 +0000 | [diff] [blame] | 3991 | /* flags = BTREE_INTKEY; */ |
| 3992 | flags = BTREE_LEAFDATA|BTREE_INTKEY; |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 3993 | }else{ |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 3994 | flags = BTREE_ZERODATA; |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 3995 | } |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 3996 | rc = sqlite3BtreeCreateTable(pDb->pBt, &pgno, flags); |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3997 | pTos++; |
drh | 5b2fd56 | 2001-09-13 15:21:31 +0000 | [diff] [blame] | 3998 | if( rc==SQLITE_OK ){ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3999 | pTos->i = pgno; |
| 4000 | pTos->flags = MEM_Int; |
drh | f1b07b0 | 2004-02-08 06:17:19 +0000 | [diff] [blame] | 4001 | }else{ |
| 4002 | pTos->flags = MEM_Null; |
drh | 5b2fd56 | 2001-09-13 15:21:31 +0000 | [diff] [blame] | 4003 | } |
| 4004 | break; |
| 4005 | } |
| 4006 | |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 4007 | /* Opcode: ParseSchema P1 * P3 |
| 4008 | ** |
| 4009 | ** Read and parse all entries from the SQLITE_MASTER table of database P1 |
| 4010 | ** that match the WHERE clause P3. |
| 4011 | ** |
| 4012 | ** This opcode invokes the parser to create a new virtual machine, |
| 4013 | ** then runs the new virtual machine. It is thus a reentrant opcode. |
| 4014 | */ |
danielk1977 | 7a5147c | 2005-03-29 13:07:00 +0000 | [diff] [blame] | 4015 | case OP_ParseSchema: { /* no-push */ |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 4016 | char *zSql; |
| 4017 | int iDb = pOp->p1; |
| 4018 | const char *zMaster; |
| 4019 | InitData initData; |
| 4020 | |
| 4021 | assert( iDb>=0 && iDb<db->nDb ); |
drh | 3f7d4e4 | 2004-07-24 14:35:58 +0000 | [diff] [blame] | 4022 | if( !DbHasProperty(db, iDb, DB_SchemaLoaded) ) break; |
danielk1977 | 53c0f74 | 2005-03-29 03:10:59 +0000 | [diff] [blame] | 4023 | zMaster = SCHEMA_TABLE(iDb); |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 4024 | initData.db = db; |
| 4025 | initData.pzErrMsg = &p->zErrMsg; |
| 4026 | zSql = sqlite3MPrintf( |
| 4027 | "SELECT name, rootpage, sql, %d FROM '%q'.%s WHERE %s", |
| 4028 | pOp->p1, db->aDb[iDb].zName, zMaster, pOp->p3); |
drh | 71c697e | 2004-08-08 23:39:19 +0000 | [diff] [blame] | 4029 | if( zSql==0 ) goto no_mem; |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 4030 | sqlite3SafetyOff(db); |
| 4031 | assert( db->init.busy==0 ); |
| 4032 | db->init.busy = 1; |
danielk1977 | 9e12800 | 2006-01-18 16:51:35 +0000 | [diff] [blame] | 4033 | assert( !sqlite3MallocFailed() ); |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 4034 | rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0); |
danielk1977 | 2e588c7 | 2005-12-09 14:25:08 +0000 | [diff] [blame] | 4035 | sqliteFree(zSql); |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 4036 | db->init.busy = 0; |
| 4037 | sqlite3SafetyOn(db); |
danielk1977 | 261919c | 2005-12-06 12:52:59 +0000 | [diff] [blame] | 4038 | if( rc==SQLITE_NOMEM ){ |
danielk1977 | 9e12800 | 2006-01-18 16:51:35 +0000 | [diff] [blame] | 4039 | sqlite3FailedMalloc(); |
danielk1977 | 261919c | 2005-12-06 12:52:59 +0000 | [diff] [blame] | 4040 | goto no_mem; |
| 4041 | } |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 4042 | break; |
| 4043 | } |
| 4044 | |
drh | cfed7bc | 2006-03-13 14:28:05 +0000 | [diff] [blame] | 4045 | #if !defined(SQLITE_OMIT_ANALYZE) && !defined(SQLITE_OMIT_PARSER) |
drh | 497e446 | 2005-07-23 03:18:40 +0000 | [diff] [blame] | 4046 | /* Opcode: LoadAnalysis P1 * * |
| 4047 | ** |
| 4048 | ** Read the sqlite_stat1 table for database P1 and load the content |
| 4049 | ** of that table into the internal index hash table. This will cause |
| 4050 | ** the analysis to be used when preparing all subsequent queries. |
| 4051 | */ |
| 4052 | case OP_LoadAnalysis: { /* no-push */ |
| 4053 | int iDb = pOp->p1; |
| 4054 | assert( iDb>=0 && iDb<db->nDb ); |
| 4055 | sqlite3AnalysisLoad(db, iDb); |
| 4056 | break; |
| 4057 | } |
drh | cfed7bc | 2006-03-13 14:28:05 +0000 | [diff] [blame] | 4058 | #endif /* !defined(SQLITE_OMIT_ANALYZE) && !defined(SQLITE_OMIT_PARSER) */ |
drh | 497e446 | 2005-07-23 03:18:40 +0000 | [diff] [blame] | 4059 | |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 4060 | /* Opcode: DropTable P1 * P3 |
| 4061 | ** |
| 4062 | ** Remove the internal (in-memory) data structures that describe |
| 4063 | ** the table named P3 in database P1. This is called after a table |
| 4064 | ** is dropped in order to keep the internal representation of the |
| 4065 | ** schema consistent with what is on disk. |
| 4066 | */ |
danielk1977 | 7a5147c | 2005-03-29 13:07:00 +0000 | [diff] [blame] | 4067 | case OP_DropTable: { /* no-push */ |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 4068 | sqlite3UnlinkAndDeleteTable(db, pOp->p1, pOp->p3); |
| 4069 | break; |
| 4070 | } |
| 4071 | |
| 4072 | /* Opcode: DropIndex P1 * P3 |
| 4073 | ** |
| 4074 | ** Remove the internal (in-memory) data structures that describe |
| 4075 | ** the index named P3 in database P1. This is called after an index |
| 4076 | ** is dropped in order to keep the internal representation of the |
| 4077 | ** schema consistent with what is on disk. |
| 4078 | */ |
danielk1977 | 7a5147c | 2005-03-29 13:07:00 +0000 | [diff] [blame] | 4079 | case OP_DropIndex: { /* no-push */ |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 4080 | sqlite3UnlinkAndDeleteIndex(db, pOp->p1, pOp->p3); |
| 4081 | break; |
| 4082 | } |
| 4083 | |
| 4084 | /* Opcode: DropTrigger P1 * P3 |
| 4085 | ** |
| 4086 | ** Remove the internal (in-memory) data structures that describe |
| 4087 | ** the trigger named P3 in database P1. This is called after a trigger |
| 4088 | ** is dropped in order to keep the internal representation of the |
| 4089 | ** schema consistent with what is on disk. |
| 4090 | */ |
danielk1977 | 7a5147c | 2005-03-29 13:07:00 +0000 | [diff] [blame] | 4091 | case OP_DropTrigger: { /* no-push */ |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 4092 | sqlite3UnlinkAndDeleteTrigger(db, pOp->p1, pOp->p3); |
| 4093 | break; |
| 4094 | } |
| 4095 | |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 4096 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 4097 | #ifndef SQLITE_OMIT_INTEGRITY_CHECK |
drh | 7906975 | 2004-05-22 21:30:40 +0000 | [diff] [blame] | 4098 | /* Opcode: IntegrityCk * P2 * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4099 | ** |
drh | 1dd397f | 2002-02-03 03:34:07 +0000 | [diff] [blame] | 4100 | ** Do an analysis of the currently open database. Push onto the |
| 4101 | ** stack the text of an error message describing any problems. |
| 4102 | ** If there are no errors, push a "ok" onto the stack. |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 4103 | ** |
drh | 7906975 | 2004-05-22 21:30:40 +0000 | [diff] [blame] | 4104 | ** The root page numbers of all tables in the database are integer |
| 4105 | ** values on the stack. This opcode pulls as many integers as it |
| 4106 | ** can off of the stack and uses those numbers as the root pages. |
drh | 2150432 | 2002-06-25 13:16:02 +0000 | [diff] [blame] | 4107 | ** |
| 4108 | ** If P2 is not zero, the check is done on the auxiliary database |
| 4109 | ** file, not the main database file. |
drh | 1dd397f | 2002-02-03 03:34:07 +0000 | [diff] [blame] | 4110 | ** |
| 4111 | ** This opcode is used for testing purposes only. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4112 | */ |
drh | aaab572 | 2002-02-19 13:39:21 +0000 | [diff] [blame] | 4113 | case OP_IntegrityCk: { |
drh | 1dd397f | 2002-02-03 03:34:07 +0000 | [diff] [blame] | 4114 | int nRoot; |
| 4115 | int *aRoot; |
drh | 1dd397f | 2002-02-03 03:34:07 +0000 | [diff] [blame] | 4116 | int j; |
drh | 1dd397f | 2002-02-03 03:34:07 +0000 | [diff] [blame] | 4117 | char *z; |
| 4118 | |
drh | 7906975 | 2004-05-22 21:30:40 +0000 | [diff] [blame] | 4119 | for(nRoot=0; &pTos[-nRoot]>=p->aStack; nRoot++){ |
| 4120 | if( (pTos[-nRoot].flags & MEM_Int)==0 ) break; |
| 4121 | } |
| 4122 | assert( nRoot>0 ); |
| 4123 | aRoot = sqliteMallocRaw( sizeof(int*)*(nRoot+1) ); |
drh | caec2f1 | 2003-01-07 02:47:47 +0000 | [diff] [blame] | 4124 | if( aRoot==0 ) goto no_mem; |
drh | 7906975 | 2004-05-22 21:30:40 +0000 | [diff] [blame] | 4125 | for(j=0; j<nRoot; j++){ |
| 4126 | Mem *pMem = &pTos[-j]; |
| 4127 | aRoot[j] = pMem->i; |
drh | 1dd397f | 2002-02-03 03:34:07 +0000 | [diff] [blame] | 4128 | } |
| 4129 | aRoot[j] = 0; |
drh | 7906975 | 2004-05-22 21:30:40 +0000 | [diff] [blame] | 4130 | popStack(&pTos, nRoot); |
| 4131 | pTos++; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 4132 | z = sqlite3BtreeIntegrityCheck(db->aDb[pOp->p2].pBt, aRoot, nRoot); |
drh | 1dd397f | 2002-02-03 03:34:07 +0000 | [diff] [blame] | 4133 | if( z==0 || z[0]==0 ){ |
drh | 2150432 | 2002-06-25 13:16:02 +0000 | [diff] [blame] | 4134 | if( z ) sqliteFree(z); |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 4135 | pTos->z = "ok"; |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 4136 | pTos->n = 2; |
| 4137 | pTos->flags = MEM_Str | MEM_Static | MEM_Term; |
drh | 1dd397f | 2002-02-03 03:34:07 +0000 | [diff] [blame] | 4138 | }else{ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 4139 | pTos->z = z; |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 4140 | pTos->n = strlen(z); |
| 4141 | pTos->flags = MEM_Str | MEM_Dyn | MEM_Term; |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 4142 | pTos->xDel = 0; |
danielk1977 | 8a6b541 | 2004-05-24 07:04:25 +0000 | [diff] [blame] | 4143 | } |
danielk1977 | dc8453f | 2004-06-12 00:42:34 +0000 | [diff] [blame] | 4144 | pTos->enc = SQLITE_UTF8; |
drh | 8079a0d | 2006-01-12 17:20:50 +0000 | [diff] [blame] | 4145 | sqlite3VdbeChangeEncoding(pTos, encoding); |
drh | 24e97df | 2002-02-03 19:06:02 +0000 | [diff] [blame] | 4146 | sqliteFree(aRoot); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4147 | break; |
| 4148 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 4149 | #endif /* SQLITE_OMIT_INTEGRITY_CHECK */ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4150 | |
drh | a01f79d | 2005-07-08 13:07:59 +0000 | [diff] [blame] | 4151 | /* Opcode: FifoWrite * * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4152 | ** |
| 4153 | ** Write the integer on the top of the stack |
drh | a01f79d | 2005-07-08 13:07:59 +0000 | [diff] [blame] | 4154 | ** into the Fifo. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4155 | */ |
drh | a01f79d | 2005-07-08 13:07:59 +0000 | [diff] [blame] | 4156 | case OP_FifoWrite: { /* no-push */ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 4157 | assert( pTos>=p->aStack ); |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 4158 | sqlite3VdbeMemIntegerify(pTos); |
drh | a01f79d | 2005-07-08 13:07:59 +0000 | [diff] [blame] | 4159 | sqlite3VdbeFifoPush(&p->sFifo, pTos->i); |
drh | 6a6124e | 2004-06-27 01:56:33 +0000 | [diff] [blame] | 4160 | assert( (pTos->flags & MEM_Dyn)==0 ); |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 4161 | pTos--; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4162 | break; |
| 4163 | } |
| 4164 | |
drh | a01f79d | 2005-07-08 13:07:59 +0000 | [diff] [blame] | 4165 | /* Opcode: FifoRead * P2 * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4166 | ** |
drh | a01f79d | 2005-07-08 13:07:59 +0000 | [diff] [blame] | 4167 | ** Attempt to read a single integer from the Fifo |
| 4168 | ** and push it onto the stack. If the Fifo is empty |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4169 | ** push nothing but instead jump to P2. |
| 4170 | */ |
drh | a01f79d | 2005-07-08 13:07:59 +0000 | [diff] [blame] | 4171 | case OP_FifoRead: { |
| 4172 | i64 v; |
drh | caec2f1 | 2003-01-07 02:47:47 +0000 | [diff] [blame] | 4173 | CHECK_FOR_INTERRUPT; |
drh | a01f79d | 2005-07-08 13:07:59 +0000 | [diff] [blame] | 4174 | if( sqlite3VdbeFifoPop(&p->sFifo, &v)==SQLITE_DONE ){ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4175 | pc = pOp->p2 - 1; |
drh | a01f79d | 2005-07-08 13:07:59 +0000 | [diff] [blame] | 4176 | }else{ |
| 4177 | pTos++; |
| 4178 | pTos->i = v; |
| 4179 | pTos->flags = MEM_Int; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4180 | } |
| 4181 | break; |
| 4182 | } |
| 4183 | |
danielk1977 | 93758c8 | 2005-01-21 08:13:14 +0000 | [diff] [blame] | 4184 | #ifndef SQLITE_OMIT_TRIGGER |
rdc | b0c374f | 2004-02-20 22:53:38 +0000 | [diff] [blame] | 4185 | /* Opcode: ContextPush * * * |
| 4186 | ** |
| 4187 | ** Save the current Vdbe context such that it can be restored by a ContextPop |
| 4188 | ** opcode. The context stores the last insert row id, the last statement change |
| 4189 | ** count, and the current statement change count. |
| 4190 | */ |
danielk1977 | 7a5147c | 2005-03-29 13:07:00 +0000 | [diff] [blame] | 4191 | case OP_ContextPush: { /* no-push */ |
drh | 344737f | 2004-09-19 00:50:20 +0000 | [diff] [blame] | 4192 | int i = p->contextStackTop++; |
| 4193 | Context *pContext; |
danielk1977 | b28af71 | 2004-06-21 06:50:26 +0000 | [diff] [blame] | 4194 | |
drh | 344737f | 2004-09-19 00:50:20 +0000 | [diff] [blame] | 4195 | assert( i>=0 ); |
danielk1977 | b28af71 | 2004-06-21 06:50:26 +0000 | [diff] [blame] | 4196 | /* FIX ME: This should be allocated as part of the vdbe at compile-time */ |
drh | 344737f | 2004-09-19 00:50:20 +0000 | [diff] [blame] | 4197 | if( i>=p->contextStackDepth ){ |
| 4198 | p->contextStackDepth = i+1; |
danielk1977 | e725929 | 2006-01-13 06:33:23 +0000 | [diff] [blame] | 4199 | sqliteReallocOrFree((void**)&p->contextStack, sizeof(Context)*(i+1)); |
drh | 344737f | 2004-09-19 00:50:20 +0000 | [diff] [blame] | 4200 | if( p->contextStack==0 ) goto no_mem; |
| 4201 | } |
| 4202 | pContext = &p->contextStack[i]; |
| 4203 | pContext->lastRowid = db->lastRowid; |
| 4204 | pContext->nChange = p->nChange; |
drh | a01f79d | 2005-07-08 13:07:59 +0000 | [diff] [blame] | 4205 | pContext->sFifo = p->sFifo; |
| 4206 | sqlite3VdbeFifoInit(&p->sFifo); |
rdc | b0c374f | 2004-02-20 22:53:38 +0000 | [diff] [blame] | 4207 | break; |
| 4208 | } |
| 4209 | |
| 4210 | /* Opcode: ContextPop * * * |
| 4211 | ** |
| 4212 | ** Restore the Vdbe context to the state it was in when contextPush was last |
| 4213 | ** executed. The context stores the last insert row id, the last statement |
| 4214 | ** change count, and the current statement change count. |
| 4215 | */ |
danielk1977 | 7a5147c | 2005-03-29 13:07:00 +0000 | [diff] [blame] | 4216 | case OP_ContextPop: { /* no-push */ |
drh | 344737f | 2004-09-19 00:50:20 +0000 | [diff] [blame] | 4217 | Context *pContext = &p->contextStack[--p->contextStackTop]; |
| 4218 | assert( p->contextStackTop>=0 ); |
| 4219 | db->lastRowid = pContext->lastRowid; |
| 4220 | p->nChange = pContext->nChange; |
drh | a01f79d | 2005-07-08 13:07:59 +0000 | [diff] [blame] | 4221 | sqlite3VdbeFifoClear(&p->sFifo); |
| 4222 | p->sFifo = pContext->sFifo; |
rdc | b0c374f | 2004-02-20 22:53:38 +0000 | [diff] [blame] | 4223 | break; |
| 4224 | } |
danielk1977 | 93758c8 | 2005-01-21 08:13:14 +0000 | [diff] [blame] | 4225 | #endif /* #ifndef SQLITE_OMIT_TRIGGER */ |
rdc | b0c374f | 2004-02-20 22:53:38 +0000 | [diff] [blame] | 4226 | |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 4227 | /* Opcode: MemStore P1 P2 * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4228 | ** |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 4229 | ** Write the top of the stack into memory location P1. |
| 4230 | ** P1 should be a small integer since space is allocated |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4231 | ** for all memory locations between 0 and P1 inclusive. |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 4232 | ** |
| 4233 | ** After the data is stored in the memory location, the |
| 4234 | ** stack is popped once if P2 is 1. If P2 is zero, then |
| 4235 | ** the original data remains on the stack. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4236 | */ |
danielk1977 | 7a5147c | 2005-03-29 13:07:00 +0000 | [diff] [blame] | 4237 | case OP_MemStore: { /* no-push */ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 4238 | assert( pTos>=p->aStack ); |
drh | febe106 | 2004-08-28 18:17:48 +0000 | [diff] [blame] | 4239 | assert( pOp->p1>=0 && pOp->p1<p->nMem ); |
| 4240 | rc = sqlite3VdbeMemMove(&p->aMem[pOp->p1], pTos); |
drh | 290c194 | 2004-08-21 17:54:45 +0000 | [diff] [blame] | 4241 | pTos--; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4242 | |
drh | 290c194 | 2004-08-21 17:54:45 +0000 | [diff] [blame] | 4243 | /* If P2 is 0 then fall thru to the next opcode, OP_MemLoad, that will |
| 4244 | ** restore the top of the stack to its original value. |
| 4245 | */ |
| 4246 | if( pOp->p2 ){ |
| 4247 | break; |
| 4248 | } |
| 4249 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4250 | /* Opcode: MemLoad P1 * * |
| 4251 | ** |
| 4252 | ** Push a copy of the value in memory location P1 onto the stack. |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 4253 | ** |
| 4254 | ** If the value is a string, then the value pushed is a pointer to |
| 4255 | ** the string that is stored in the memory location. If the memory |
| 4256 | ** location is subsequently changed (using OP_MemStore) then the |
| 4257 | ** value pushed onto the stack will change too. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4258 | */ |
| 4259 | case OP_MemLoad: { |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4260 | int i = pOp->p1; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 4261 | assert( i>=0 && i<p->nMem ); |
| 4262 | pTos++; |
drh | febe106 | 2004-08-28 18:17:48 +0000 | [diff] [blame] | 4263 | sqlite3VdbeMemShallowCopy(pTos, &p->aMem[i], MEM_Ephem); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4264 | break; |
| 4265 | } |
| 4266 | |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 4267 | #ifndef SQLITE_OMIT_AUTOINCREMENT |
| 4268 | /* Opcode: MemMax P1 * * |
| 4269 | ** |
| 4270 | ** Set the value of memory cell P1 to the maximum of its current value |
| 4271 | ** and the value on the top of the stack. The stack is unchanged. |
| 4272 | ** |
| 4273 | ** This instruction throws an error if the memory cell is not initially |
| 4274 | ** an integer. |
| 4275 | */ |
danielk1977 | 7a5147c | 2005-03-29 13:07:00 +0000 | [diff] [blame] | 4276 | case OP_MemMax: { /* no-push */ |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 4277 | int i = pOp->p1; |
| 4278 | Mem *pMem; |
| 4279 | assert( pTos>=p->aStack ); |
| 4280 | assert( i>=0 && i<p->nMem ); |
| 4281 | pMem = &p->aMem[i]; |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 4282 | sqlite3VdbeMemIntegerify(pMem); |
| 4283 | sqlite3VdbeMemIntegerify(pTos); |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 4284 | if( pMem->i<pTos->i){ |
| 4285 | pMem->i = pTos->i; |
| 4286 | } |
| 4287 | break; |
| 4288 | } |
| 4289 | #endif /* SQLITE_OMIT_AUTOINCREMENT */ |
| 4290 | |
drh | 15007a9 | 2006-01-08 18:10:17 +0000 | [diff] [blame] | 4291 | /* Opcode: MemIncr P1 P2 * |
drh | d11d382 | 2002-06-21 23:01:49 +0000 | [diff] [blame] | 4292 | ** |
drh | 15007a9 | 2006-01-08 18:10:17 +0000 | [diff] [blame] | 4293 | ** Increment the integer valued memory cell P2 by the value in P1. |
drh | d11d382 | 2002-06-21 23:01:49 +0000 | [diff] [blame] | 4294 | ** |
drh | 6f58f70 | 2006-01-08 05:26:41 +0000 | [diff] [blame] | 4295 | ** It is illegal to use this instruction on a memory cell that does |
| 4296 | ** not contain an integer. An assertion fault will result if you try. |
drh | d11d382 | 2002-06-21 23:01:49 +0000 | [diff] [blame] | 4297 | */ |
danielk1977 | 7a5147c | 2005-03-29 13:07:00 +0000 | [diff] [blame] | 4298 | case OP_MemIncr: { /* no-push */ |
drh | 15007a9 | 2006-01-08 18:10:17 +0000 | [diff] [blame] | 4299 | int i = pOp->p2; |
drh | d11d382 | 2002-06-21 23:01:49 +0000 | [diff] [blame] | 4300 | Mem *pMem; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 4301 | assert( i>=0 && i<p->nMem ); |
drh | d11d382 | 2002-06-21 23:01:49 +0000 | [diff] [blame] | 4302 | pMem = &p->aMem[i]; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 4303 | assert( pMem->flags==MEM_Int ); |
drh | 15007a9 | 2006-01-08 18:10:17 +0000 | [diff] [blame] | 4304 | pMem->i += pOp->p1; |
danielk1977 | a2dc3b1 | 2005-02-05 12:48:48 +0000 | [diff] [blame] | 4305 | break; |
| 4306 | } |
| 4307 | |
| 4308 | /* Opcode: IfMemPos P1 P2 * |
| 4309 | ** |
drh | 15007a9 | 2006-01-08 18:10:17 +0000 | [diff] [blame] | 4310 | ** If the value of memory cell P1 is 1 or greater, jump to P2. |
drh | 6f58f70 | 2006-01-08 05:26:41 +0000 | [diff] [blame] | 4311 | ** |
| 4312 | ** It is illegal to use this instruction on a memory cell that does |
| 4313 | ** not contain an integer. An assertion fault will result if you try. |
danielk1977 | a2dc3b1 | 2005-02-05 12:48:48 +0000 | [diff] [blame] | 4314 | */ |
danielk1977 | 7a5147c | 2005-03-29 13:07:00 +0000 | [diff] [blame] | 4315 | case OP_IfMemPos: { /* no-push */ |
danielk1977 | a2dc3b1 | 2005-02-05 12:48:48 +0000 | [diff] [blame] | 4316 | int i = pOp->p1; |
| 4317 | Mem *pMem; |
| 4318 | assert( i>=0 && i<p->nMem ); |
| 4319 | pMem = &p->aMem[i]; |
drh | 6f58f70 | 2006-01-08 05:26:41 +0000 | [diff] [blame] | 4320 | assert( pMem->flags==MEM_Int ); |
| 4321 | if( pMem->i>0 ){ |
drh | ec7429a | 2005-10-06 16:53:14 +0000 | [diff] [blame] | 4322 | pc = pOp->p2 - 1; |
| 4323 | } |
| 4324 | break; |
| 4325 | } |
| 4326 | |
drh | 15007a9 | 2006-01-08 18:10:17 +0000 | [diff] [blame] | 4327 | /* Opcode: IfMemNeg P1 P2 * |
| 4328 | ** |
| 4329 | ** If the value of memory cell P1 is less than zero, jump to P2. |
| 4330 | ** |
| 4331 | ** It is illegal to use this instruction on a memory cell that does |
| 4332 | ** not contain an integer. An assertion fault will result if you try. |
| 4333 | */ |
| 4334 | case OP_IfMemNeg: { /* no-push */ |
| 4335 | int i = pOp->p1; |
| 4336 | Mem *pMem; |
| 4337 | assert( i>=0 && i<p->nMem ); |
| 4338 | pMem = &p->aMem[i]; |
| 4339 | assert( pMem->flags==MEM_Int ); |
| 4340 | if( pMem->i<0 ){ |
| 4341 | pc = pOp->p2 - 1; |
| 4342 | } |
| 4343 | break; |
| 4344 | } |
| 4345 | |
drh | ec7429a | 2005-10-06 16:53:14 +0000 | [diff] [blame] | 4346 | /* Opcode: IfMemZero P1 P2 * |
| 4347 | ** |
drh | 6f58f70 | 2006-01-08 05:26:41 +0000 | [diff] [blame] | 4348 | ** If the value of memory cell P1 is exactly 0, jump to P2. |
| 4349 | ** |
| 4350 | ** It is illegal to use this instruction on a memory cell that does |
| 4351 | ** not contain an integer. An assertion fault will result if you try. |
drh | ec7429a | 2005-10-06 16:53:14 +0000 | [diff] [blame] | 4352 | */ |
| 4353 | case OP_IfMemZero: { /* no-push */ |
| 4354 | int i = pOp->p1; |
| 4355 | Mem *pMem; |
| 4356 | assert( i>=0 && i<p->nMem ); |
| 4357 | pMem = &p->aMem[i]; |
drh | 6f58f70 | 2006-01-08 05:26:41 +0000 | [diff] [blame] | 4358 | assert( pMem->flags==MEM_Int ); |
| 4359 | if( pMem->i==0 ){ |
drh | d11d382 | 2002-06-21 23:01:49 +0000 | [diff] [blame] | 4360 | pc = pOp->p2 - 1; |
| 4361 | } |
| 4362 | break; |
| 4363 | } |
| 4364 | |
drh | d654be8 | 2005-09-20 17:42:23 +0000 | [diff] [blame] | 4365 | /* Opcode: MemNull P1 * * |
| 4366 | ** |
| 4367 | ** Store a NULL in memory cell P1 |
| 4368 | */ |
| 4369 | case OP_MemNull: { |
| 4370 | assert( pOp->p1>=0 && pOp->p1<p->nMem ); |
| 4371 | sqlite3VdbeMemSetNull(&p->aMem[pOp->p1]); |
| 4372 | break; |
| 4373 | } |
| 4374 | |
| 4375 | /* Opcode: MemInt P1 P2 * |
| 4376 | ** |
| 4377 | ** Store the integer value P1 in memory cell P2. |
| 4378 | */ |
| 4379 | case OP_MemInt: { |
| 4380 | assert( pOp->p2>=0 && pOp->p2<p->nMem ); |
| 4381 | sqlite3VdbeMemSetInt64(&p->aMem[pOp->p2], pOp->p1); |
| 4382 | break; |
| 4383 | } |
| 4384 | |
| 4385 | /* Opcode: MemMove P1 P2 * |
| 4386 | ** |
| 4387 | ** Move the content of memory cell P2 over to memory cell P1. |
| 4388 | ** Any prior content of P1 is erased. Memory cell P2 is left |
| 4389 | ** containing a NULL. |
| 4390 | */ |
| 4391 | case OP_MemMove: { |
| 4392 | assert( pOp->p1>=0 && pOp->p1<p->nMem ); |
| 4393 | assert( pOp->p2>=0 && pOp->p2<p->nMem ); |
| 4394 | rc = sqlite3VdbeMemMove(&p->aMem[pOp->p1], &p->aMem[pOp->p2]); |
| 4395 | break; |
| 4396 | } |
| 4397 | |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 4398 | /* Opcode: AggStep P1 P2 P3 |
drh | e509535 | 2002-02-24 03:25:14 +0000 | [diff] [blame] | 4399 | ** |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 4400 | ** Execute the step function for an aggregate. The |
| 4401 | ** function has P2 arguments. P3 is a pointer to the FuncDef |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 4402 | ** structure that specifies the function. Use memory location |
| 4403 | ** P1 as the accumulator. |
drh | e509535 | 2002-02-24 03:25:14 +0000 | [diff] [blame] | 4404 | ** |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 4405 | ** The P2 arguments are popped from the stack. |
drh | e509535 | 2002-02-24 03:25:14 +0000 | [diff] [blame] | 4406 | */ |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 4407 | case OP_AggStep: { /* no-push */ |
drh | e509535 | 2002-02-24 03:25:14 +0000 | [diff] [blame] | 4408 | int n = pOp->p2; |
| 4409 | int i; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 4410 | Mem *pMem, *pRec; |
danielk1977 | 22322fd | 2004-05-25 23:35:17 +0000 | [diff] [blame] | 4411 | sqlite3_context ctx; |
danielk1977 | 6ddcca5 | 2004-05-24 23:48:25 +0000 | [diff] [blame] | 4412 | sqlite3_value **apVal; |
drh | e509535 | 2002-02-24 03:25:14 +0000 | [diff] [blame] | 4413 | |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 4414 | assert( n>=0 ); |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 4415 | pRec = &pTos[1-n]; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 4416 | assert( pRec>=p->aStack ); |
danielk1977 | 6ddcca5 | 2004-05-24 23:48:25 +0000 | [diff] [blame] | 4417 | apVal = p->apArg; |
| 4418 | assert( apVal || n==0 ); |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 4419 | for(i=0; i<n; i++, pRec++){ |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 4420 | apVal[i] = pRec; |
drh | 8079a0d | 2006-01-12 17:20:50 +0000 | [diff] [blame] | 4421 | storeTypeInfo(pRec, encoding); |
drh | e509535 | 2002-02-24 03:25:14 +0000 | [diff] [blame] | 4422 | } |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 4423 | ctx.pFunc = (FuncDef*)pOp->p3; |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 4424 | assert( pOp->p1>=0 && pOp->p1<p->nMem ); |
| 4425 | ctx.pMem = pMem = &p->aMem[pOp->p1]; |
drh | abfcea2 | 2005-09-06 20:36:48 +0000 | [diff] [blame] | 4426 | pMem->n++; |
drh | 90669c1 | 2006-01-20 15:45:36 +0000 | [diff] [blame] | 4427 | ctx.s.flags = MEM_Null; |
| 4428 | ctx.s.z = 0; |
| 4429 | ctx.s.xDel = 0; |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 4430 | ctx.isError = 0; |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 4431 | ctx.pColl = 0; |
| 4432 | if( ctx.pFunc->needCollSeq ){ |
| 4433 | assert( pOp>p->aOp ); |
| 4434 | assert( pOp[-1].p3type==P3_COLLSEQ ); |
| 4435 | assert( pOp[-1].opcode==OP_CollSeq ); |
| 4436 | ctx.pColl = (CollSeq *)pOp[-1].p3; |
| 4437 | } |
danielk1977 | 6ddcca5 | 2004-05-24 23:48:25 +0000 | [diff] [blame] | 4438 | (ctx.pFunc->xStep)(&ctx, n, apVal); |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 4439 | popStack(&pTos, n); |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 4440 | if( ctx.isError ){ |
drh | 90669c1 | 2006-01-20 15:45:36 +0000 | [diff] [blame] | 4441 | sqlite3SetString(&p->zErrMsg, sqlite3_value_text(&ctx.s), (char*)0); |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 4442 | rc = SQLITE_ERROR; |
| 4443 | } |
drh | 90669c1 | 2006-01-20 15:45:36 +0000 | [diff] [blame] | 4444 | sqlite3VdbeMemRelease(&ctx.s); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4445 | break; |
| 4446 | } |
| 4447 | |
drh | a10a34b | 2005-09-07 22:09:48 +0000 | [diff] [blame] | 4448 | /* Opcode: AggFinal P1 P2 P3 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4449 | ** |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 4450 | ** Execute the finalizer function for an aggregate. P1 is |
| 4451 | ** the memory location that is the accumulator for the aggregate. |
drh | a10a34b | 2005-09-07 22:09:48 +0000 | [diff] [blame] | 4452 | ** |
| 4453 | ** P2 is the number of arguments that the step function takes and |
| 4454 | ** P3 is a pointer to the FuncDef for this function. The P2 |
| 4455 | ** argument is not used by this opcode. It is only there to disambiguate |
| 4456 | ** functions that can take varying numbers of arguments. The |
| 4457 | ** P3 argument is only needed for the degenerate case where |
| 4458 | ** the step function was not previously called. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4459 | */ |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 4460 | case OP_AggFinal: { /* no-push */ |
| 4461 | Mem *pMem; |
| 4462 | assert( pOp->p1>=0 && pOp->p1<p->nMem ); |
| 4463 | pMem = &p->aMem[pOp->p1]; |
drh | a10a34b | 2005-09-07 22:09:48 +0000 | [diff] [blame] | 4464 | assert( (pMem->flags & ~(MEM_Null|MEM_Agg))==0 ); |
drh | 90669c1 | 2006-01-20 15:45:36 +0000 | [diff] [blame] | 4465 | rc = sqlite3VdbeMemFinalize(pMem, (FuncDef*)pOp->p3); |
| 4466 | if( rc==SQLITE_ERROR ){ |
| 4467 | sqlite3SetString(&p->zErrMsg, sqlite3_value_text(pMem), (char*)0); |
| 4468 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4469 | break; |
| 4470 | } |
| 4471 | |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4472 | |
drh | 6f8c91c | 2003-12-07 00:24:35 +0000 | [diff] [blame] | 4473 | /* Opcode: Vacuum * * * |
| 4474 | ** |
| 4475 | ** Vacuum the entire database. This opcode will cause other virtual |
| 4476 | ** machines to be created and run. It may not be called from within |
| 4477 | ** a transaction. |
| 4478 | */ |
danielk1977 | 7a5147c | 2005-03-29 13:07:00 +0000 | [diff] [blame] | 4479 | case OP_Vacuum: { /* no-push */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 4480 | if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse; |
| 4481 | rc = sqlite3RunVacuum(&p->zErrMsg, db); |
| 4482 | if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse; |
drh | 6f8c91c | 2003-12-07 00:24:35 +0000 | [diff] [blame] | 4483 | break; |
| 4484 | } |
| 4485 | |
danielk1977 | a21c6b6 | 2005-01-24 10:25:59 +0000 | [diff] [blame] | 4486 | /* Opcode: Expire P1 * * |
| 4487 | ** |
| 4488 | ** Cause precompiled statements to become expired. An expired statement |
| 4489 | ** fails with an error code of SQLITE_SCHEMA if it is ever executed |
| 4490 | ** (via sqlite3_step()). |
| 4491 | ** |
| 4492 | ** If P1 is 0, then all SQL statements become expired. If P1 is non-zero, |
| 4493 | ** then only the currently executing statement is affected. |
| 4494 | */ |
danielk1977 | 7a5147c | 2005-03-29 13:07:00 +0000 | [diff] [blame] | 4495 | case OP_Expire: { /* no-push */ |
danielk1977 | a21c6b6 | 2005-01-24 10:25:59 +0000 | [diff] [blame] | 4496 | if( !pOp->p1 ){ |
| 4497 | sqlite3ExpirePreparedStatements(db); |
| 4498 | }else{ |
| 4499 | p->expired = 1; |
| 4500 | } |
| 4501 | break; |
| 4502 | } |
| 4503 | |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 4504 | #ifndef SQLITE_OMIT_SHARED_CACHE |
| 4505 | /* Opcode: TableLock P1 P2 P3 |
| 4506 | ** |
| 4507 | ** Obtain a lock on a particular table. This instruction is only used when |
| 4508 | ** the shared-cache feature is enabled. |
| 4509 | ** |
drh | a154dcd | 2006-03-22 22:10:07 +0000 | [diff] [blame] | 4510 | ** If P1 is not negative, then it is the index of the database |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 4511 | ** in sqlite3.aDb[] and a read-lock is required. If P1 is negative, a |
| 4512 | ** write-lock is required. In this case the index of the database is the |
| 4513 | ** absolute value of P1 minus one (iDb = abs(P1) - 1;) and a write-lock is |
| 4514 | ** required. |
| 4515 | ** |
| 4516 | ** P2 contains the root-page of the table to lock. |
| 4517 | ** |
| 4518 | ** P3 contains a pointer to the name of the table being locked. This is only |
| 4519 | ** used to generate an error message if the lock cannot be obtained. |
| 4520 | */ |
| 4521 | case OP_TableLock: { /* no-push */ |
| 4522 | int p1 = pOp->p1; |
| 4523 | u8 isWriteLock = (p1<0); |
| 4524 | if( isWriteLock ){ |
| 4525 | p1 = (-1*p1)-1; |
| 4526 | } |
| 4527 | rc = sqlite3BtreeLockTable(db->aDb[p1].pBt, pOp->p2, isWriteLock); |
| 4528 | if( rc==SQLITE_LOCKED ){ |
| 4529 | const char *z = (const char *)pOp->p3; |
| 4530 | sqlite3SetString(&p->zErrMsg, "database table is locked: ", z, (char*)0); |
| 4531 | } |
| 4532 | break; |
| 4533 | } |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 4534 | #endif /* SQLITE_OMIT_SHARED_CACHE */ |
| 4535 | |
| 4536 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
danielk1977 | 78efaba | 2006-06-12 06:09:17 +0000 | [diff] [blame] | 4537 | /* Opcode: VCreate P1 * P3 |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 4538 | ** |
danielk1977 | 78efaba | 2006-06-12 06:09:17 +0000 | [diff] [blame] | 4539 | ** P3 is the name of a virtual table in database P1. Call the xCreate method |
| 4540 | ** for that table. |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 4541 | */ |
| 4542 | case OP_VCreate: { |
danielk1977 | 78efaba | 2006-06-12 06:09:17 +0000 | [diff] [blame] | 4543 | rc = sqlite3VtabCallCreate(db, pOp->p1, pOp->p3, &p->zErrMsg); |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 4544 | break; |
| 4545 | } |
| 4546 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 4547 | |
| 4548 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
danielk1977 | 9e39ce8 | 2006-06-12 16:01:21 +0000 | [diff] [blame] | 4549 | /* Opcode: VDestroy P1 * P3 |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 4550 | ** |
danielk1977 | 9e39ce8 | 2006-06-12 16:01:21 +0000 | [diff] [blame] | 4551 | ** P3 is the name of a virtual table in database P1. Call the xDestroy method |
| 4552 | ** of that table. |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 4553 | */ |
| 4554 | case OP_VDestroy: { |
danielk1977 | 9e39ce8 | 2006-06-12 16:01:21 +0000 | [diff] [blame] | 4555 | rc = sqlite3VtabCallDestroy(db, pOp->p1, pOp->p3); |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 4556 | break; |
| 4557 | } |
| 4558 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 4559 | |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 4560 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 4561 | /* Opcode: VOpen P1 * P3 |
| 4562 | ** |
| 4563 | ** P3 is a pointer to a virtual table object, an sqlite3_vtab structure. |
| 4564 | ** P1 is a cursor number. This opcode opens a cursor to the virtual |
| 4565 | ** table and stores that cursor in P1. |
| 4566 | */ |
| 4567 | case OP_VOpen: { |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 4568 | Cursor *pCur = 0; |
| 4569 | sqlite3_vtab_cursor *pVtabCursor = 0; |
| 4570 | |
| 4571 | sqlite3_vtab *pVtab = (sqlite3_vtab *)(pOp->p3); |
| 4572 | sqlite3_module *pModule = (sqlite3_module *)pVtab->pModule; |
| 4573 | |
| 4574 | assert(pVtab && pModule); |
| 4575 | if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse; |
| 4576 | rc = pModule->xOpen(pVtab, &pVtabCursor); |
| 4577 | if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse; |
| 4578 | if( SQLITE_OK==rc ){ |
| 4579 | /* Initialise sqlite3_vtab_cursor base class */ |
| 4580 | pVtabCursor->pVtab = pVtab; |
| 4581 | |
| 4582 | /* Initialise vdbe cursor object */ |
| 4583 | pCur = allocateCursor(p, pOp->p1, -1); |
| 4584 | pCur->pVtabCursor = pVtabCursor; |
| 4585 | } |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 4586 | break; |
| 4587 | } |
| 4588 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 4589 | |
| 4590 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 4591 | /* Opcode: VFilter P1 P2 P3 |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 4592 | ** |
| 4593 | ** P1 is a cursor opened using VOpen. P2 is an address to jump to if |
| 4594 | ** the filtered result set is empty. |
| 4595 | ** |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 4596 | ** P3 points to enough free space to use to marshall the arguments. |
| 4597 | ** |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 4598 | ** This opcode invokes the xFilter method on the virtual table specified |
danielk1977 | be8a783 | 2006-06-13 15:00:54 +0000 | [diff] [blame] | 4599 | ** by P1. The query plan parameter to xFilter is the top of the stack. |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 4600 | ** Next down on the stack is the argc parameter. Beneath the |
| 4601 | ** next of stack are argc additional parameters which are passed to |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 4602 | ** xFilter as argv. The topmost parameter (i.e. 3rd element popped from |
| 4603 | ** the stack) becomes argv[argc-1] when passed to xFilter. |
| 4604 | ** |
danielk1977 | be8a783 | 2006-06-13 15:00:54 +0000 | [diff] [blame] | 4605 | ** The query plan, argc, and all argv stack values are popped from the |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 4606 | ** stack before this instruction completes. |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 4607 | ** |
| 4608 | ** A jump is made to P2 if the result set after filtering would be |
| 4609 | ** empty. |
| 4610 | */ |
| 4611 | case OP_VFilter: { |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 4612 | int nArg; |
| 4613 | |
| 4614 | const sqlite3_module *pModule; |
| 4615 | |
| 4616 | Cursor *pCur = p->apCsr[pOp->p1]; |
| 4617 | assert( pCur->pVtabCursor ); |
| 4618 | pModule = pCur->pVtabCursor->pVtab->pModule; |
| 4619 | |
| 4620 | /* Grab the index number and argc parameters off the top of the stack. */ |
| 4621 | assert( (&pTos[-1])>=p->aStack ); |
danielk1977 | be8a783 | 2006-06-13 15:00:54 +0000 | [diff] [blame] | 4622 | assert( pTos[0].flags&MEM_Blob && pTos[-1].flags==MEM_Int ); |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 4623 | nArg = pTos[-1].i; |
| 4624 | |
| 4625 | /* Invoke the xFilter method if one is defined. */ |
| 4626 | if( pModule->xFilter ){ |
| 4627 | int res; |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 4628 | int ii; |
danielk1977 | be8a783 | 2006-06-13 15:00:54 +0000 | [diff] [blame] | 4629 | Mem **apArg = (Mem **)pOp->p3; |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 4630 | for(ii = 0; ii<nArg; ii++){ |
| 4631 | apArg[ii] = &pTos[ii+1-2-nArg]; |
| 4632 | } |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 4633 | |
| 4634 | if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse; |
danielk1977 | be8a783 | 2006-06-13 15:00:54 +0000 | [diff] [blame] | 4635 | res = pModule->xFilter(pCur->pVtabCursor, pTos->z, pTos->n, nArg, apArg); |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 4636 | if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse; |
| 4637 | |
| 4638 | if( res==0 ){ |
| 4639 | pc = pOp->p2 - 1; |
| 4640 | } |
| 4641 | } |
| 4642 | |
| 4643 | /* Pop the index number, argc value and parameters off the stack */ |
| 4644 | popStack(&pTos, 2+nArg); |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 4645 | break; |
| 4646 | } |
| 4647 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 4648 | |
| 4649 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 4650 | /* Opcode: VRowid P1 * * |
| 4651 | ** |
| 4652 | ** Push an integer onto the stack which is the rowid of |
| 4653 | ** the virtual-table that the P1 cursor is pointing to. |
| 4654 | */ |
| 4655 | case OP_VRowid: { |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 4656 | const sqlite3_module *pModule; |
| 4657 | |
| 4658 | Cursor *pCur = p->apCsr[pOp->p1]; |
| 4659 | assert( pCur->pVtabCursor ); |
| 4660 | pModule = pCur->pVtabCursor->pVtab->pModule; |
| 4661 | if( pModule->xRowid==0 ){ |
| 4662 | sqlite3SetString(&p->zErrMsg, "Unsupported module operation: xRowid", 0); |
| 4663 | rc = SQLITE_ERROR; |
| 4664 | } else { |
| 4665 | sqlite_int64 iRow; |
| 4666 | |
| 4667 | if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse; |
| 4668 | rc = pModule->xRowid(pCur->pVtabCursor, &iRow); |
| 4669 | if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse; |
| 4670 | |
| 4671 | pTos++; |
| 4672 | pTos->flags = MEM_Int; |
| 4673 | pTos->i = iRow; |
| 4674 | } |
| 4675 | |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 4676 | break; |
| 4677 | } |
| 4678 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 4679 | |
| 4680 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 4681 | /* Opcode: VColumn P1 P2 * |
| 4682 | ** |
| 4683 | ** Push onto the stack the value of the P2-th column of |
| 4684 | ** the row of the virtual-table that the P1 cursor is pointing to. |
| 4685 | */ |
| 4686 | case OP_VColumn: { |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 4687 | const sqlite3_module *pModule; |
| 4688 | |
| 4689 | Cursor *pCur = p->apCsr[pOp->p1]; |
| 4690 | assert( pCur->pVtabCursor ); |
| 4691 | pModule = pCur->pVtabCursor->pVtab->pModule; |
| 4692 | if( pModule->xColumn==0 ){ |
| 4693 | sqlite3SetString(&p->zErrMsg, "Unsupported module operation: xColumn", 0); |
| 4694 | rc = SQLITE_ERROR; |
| 4695 | } else { |
| 4696 | sqlite3_context sContext; |
| 4697 | memset(&sContext, 0, sizeof(sContext)); |
| 4698 | sContext.s.flags = MEM_Null; |
| 4699 | if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse; |
| 4700 | rc = pModule->xColumn(pCur->pVtabCursor, &sContext, pOp->p2); |
| 4701 | |
| 4702 | /* Copy the result of the function to the top of the stack. We |
| 4703 | ** do this regardless of whether or not an error occured to ensure any |
| 4704 | ** dynamic allocation in sContext.s (a Mem struct) is released. |
| 4705 | */ |
| 4706 | sqlite3VdbeChangeEncoding(&sContext.s, encoding); |
| 4707 | pTos++; |
| 4708 | pTos->flags = 0; |
| 4709 | sqlite3VdbeMemMove(pTos, &sContext.s); |
| 4710 | |
| 4711 | if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse; |
| 4712 | } |
| 4713 | |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 4714 | break; |
| 4715 | } |
| 4716 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 4717 | |
| 4718 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 4719 | /* Opcode: VNext P1 P2 * |
| 4720 | ** |
| 4721 | ** Advance virtual table P1 to the next row in its result set and |
| 4722 | ** jump to instruction P2. Or, if the virtual table has reached |
| 4723 | ** the end of its result set, then fall through to the next instruction. |
| 4724 | */ |
| 4725 | case OP_VNext: { |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 4726 | const sqlite3_module *pModule; |
| 4727 | int res = 0; |
| 4728 | |
| 4729 | Cursor *pCur = p->apCsr[pOp->p1]; |
| 4730 | assert( pCur->pVtabCursor ); |
| 4731 | pModule = pCur->pVtabCursor->pVtab->pModule; |
| 4732 | if( pModule->xNext==0 ){ |
| 4733 | sqlite3SetString(&p->zErrMsg, "Unsupported module operation: xNext", 0); |
| 4734 | rc = SQLITE_ERROR; |
| 4735 | } else { |
| 4736 | /* Invoke the xNext() method of the module. There is no way for the |
| 4737 | ** underlying implementation to return an error if one occurs during |
| 4738 | ** xNext(). Instead, if an error occurs, true is returned (indicating that |
| 4739 | ** data is available) and the error code returned when xColumn or |
| 4740 | ** some other method is next invoked on the save virtual table cursor. |
| 4741 | */ |
| 4742 | if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse; |
| 4743 | res = pModule->xNext(pCur->pVtabCursor); |
| 4744 | if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse; |
| 4745 | |
| 4746 | if( res ){ |
| 4747 | /* If there is data (or an error), jump to P2 */ |
| 4748 | pc = pOp->p2 - 1; |
| 4749 | } |
| 4750 | } |
| 4751 | |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 4752 | break; |
| 4753 | } |
| 4754 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 4755 | |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4756 | /* An other opcode is illegal... |
| 4757 | */ |
| 4758 | default: { |
drh | a611040 | 2005-07-28 20:51:19 +0000 | [diff] [blame] | 4759 | assert( 0 ); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4760 | break; |
| 4761 | } |
| 4762 | |
| 4763 | /***************************************************************************** |
| 4764 | ** The cases of the switch statement above this line should all be indented |
| 4765 | ** by 6 spaces. But the left-most 6 spaces have been removed to improve the |
| 4766 | ** readability. From this point on down, the normal indentation rules are |
| 4767 | ** restored. |
| 4768 | *****************************************************************************/ |
| 4769 | } |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 4770 | |
danielk1977 | bc04f85 | 2005-03-29 08:26:13 +0000 | [diff] [blame] | 4771 | /* Make sure the stack limit was not exceeded */ |
| 4772 | assert( pTos<=pStackLimit ); |
| 4773 | |
drh | 7b39686 | 2003-01-01 23:06:20 +0000 | [diff] [blame] | 4774 | #ifdef VDBE_PROFILE |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 4775 | { |
| 4776 | long long elapse = hwtime() - start; |
| 4777 | pOp->cycles += elapse; |
| 4778 | pOp->cnt++; |
| 4779 | #if 0 |
| 4780 | fprintf(stdout, "%10lld ", elapse); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 4781 | sqlite3VdbePrintOp(stdout, origPc, &p->aOp[origPc]); |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 4782 | #endif |
| 4783 | } |
drh | 7b39686 | 2003-01-01 23:06:20 +0000 | [diff] [blame] | 4784 | #endif |
| 4785 | |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 4786 | /* The following code adds nothing to the actual functionality |
| 4787 | ** of the program. It is only here for testing and debugging. |
| 4788 | ** On the other hand, it does burn CPU cycles every time through |
| 4789 | ** the evaluator loop. So we can leave it out when NDEBUG is defined. |
| 4790 | */ |
| 4791 | #ifndef NDEBUG |
drh | 3914aed | 2004-01-31 20:40:42 +0000 | [diff] [blame] | 4792 | /* Sanity checking on the top element of the stack */ |
| 4793 | if( pTos>=p->aStack ){ |
drh | 7416170 | 2006-02-24 02:53:49 +0000 | [diff] [blame] | 4794 | sqlite3VdbeMemSanity(pTos); |
drh | 3914aed | 2004-01-31 20:40:42 +0000 | [diff] [blame] | 4795 | } |
drh | a611040 | 2005-07-28 20:51:19 +0000 | [diff] [blame] | 4796 | assert( pc>=-1 && pc<p->nOp ); |
danielk1977 | b5402fb | 2005-01-12 07:15:04 +0000 | [diff] [blame] | 4797 | #ifdef SQLITE_DEBUG |
| 4798 | /* Code for tracing the vdbe stack. */ |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 4799 | if( p->trace && pTos>=p->aStack ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4800 | int i; |
| 4801 | fprintf(p->trace, "Stack:"); |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 4802 | for(i=0; i>-5 && &pTos[i]>=p->aStack; i--){ |
| 4803 | if( pTos[i].flags & MEM_Null ){ |
drh | c61053b | 2000-06-04 12:58:36 +0000 | [diff] [blame] | 4804 | fprintf(p->trace, " NULL"); |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 4805 | }else if( (pTos[i].flags & (MEM_Int|MEM_Str))==(MEM_Int|MEM_Str) ){ |
drh | a3b321d | 2004-05-11 09:31:31 +0000 | [diff] [blame] | 4806 | fprintf(p->trace, " si:%lld", pTos[i].i); |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 4807 | }else if( pTos[i].flags & MEM_Int ){ |
drh | a3b321d | 2004-05-11 09:31:31 +0000 | [diff] [blame] | 4808 | fprintf(p->trace, " i:%lld", pTos[i].i); |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 4809 | }else if( pTos[i].flags & MEM_Real ){ |
| 4810 | fprintf(p->trace, " r:%g", pTos[i].r); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4811 | }else{ |
danielk1977 | ca6b291 | 2004-05-21 10:49:47 +0000 | [diff] [blame] | 4812 | char zBuf[100]; |
drh | 7416170 | 2006-02-24 02:53:49 +0000 | [diff] [blame] | 4813 | sqlite3VdbeMemPrettyPrint(&pTos[i], zBuf); |
danielk1977 | ca6b291 | 2004-05-21 10:49:47 +0000 | [diff] [blame] | 4814 | fprintf(p->trace, " "); |
danielk1977 | ec8450f | 2004-06-19 09:35:36 +0000 | [diff] [blame] | 4815 | fprintf(p->trace, "%s", zBuf); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4816 | } |
| 4817 | } |
drh | 7bc09d3 | 2002-11-01 01:55:36 +0000 | [diff] [blame] | 4818 | if( rc!=0 ) fprintf(p->trace," rc=%d",rc); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4819 | fprintf(p->trace,"\n"); |
| 4820 | } |
danielk1977 | b5402fb | 2005-01-12 07:15:04 +0000 | [diff] [blame] | 4821 | #endif /* SQLITE_DEBUG */ |
| 4822 | #endif /* NDEBUG */ |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 4823 | } /* The end of the for(;;) loop the loops through opcodes */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4824 | |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 4825 | /* If we reach this point, it means that execution is finished. |
| 4826 | */ |
| 4827 | vdbe_halt: |
| 4828 | if( rc ){ |
| 4829 | p->rc = rc; |
| 4830 | rc = SQLITE_ERROR; |
| 4831 | }else{ |
| 4832 | rc = SQLITE_DONE; |
| 4833 | } |
drh | 92f02c3 | 2004-09-02 14:57:08 +0000 | [diff] [blame] | 4834 | sqlite3VdbeHalt(p); |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 4835 | p->pTos = pTos; |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 4836 | return rc; |
| 4837 | |
| 4838 | /* Jump to here if a malloc() fails. It's hard to get a malloc() |
| 4839 | ** to fail on a modern VM computer, so this code is untested. |
| 4840 | */ |
| 4841 | no_mem: |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 4842 | sqlite3SetString(&p->zErrMsg, "out of memory", (char*)0); |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 4843 | rc = SQLITE_NOMEM; |
| 4844 | goto vdbe_halt; |
| 4845 | |
| 4846 | /* Jump to here for an SQLITE_MISUSE error. |
| 4847 | */ |
| 4848 | abort_due_to_misuse: |
| 4849 | rc = SQLITE_MISUSE; |
| 4850 | /* Fall thru into abort_due_to_error */ |
| 4851 | |
| 4852 | /* Jump to here for any other kind of fatal error. The "rc" variable |
| 4853 | ** should hold the error number. |
| 4854 | */ |
| 4855 | abort_due_to_error: |
drh | 483750b | 2003-01-29 18:46:51 +0000 | [diff] [blame] | 4856 | if( p->zErrMsg==0 ){ |
danielk1977 | 9e12800 | 2006-01-18 16:51:35 +0000 | [diff] [blame] | 4857 | if( sqlite3MallocFailed() ) rc = SQLITE_NOMEM; |
danielk1977 | f20b21c | 2004-05-31 23:56:42 +0000 | [diff] [blame] | 4858 | sqlite3SetString(&p->zErrMsg, sqlite3ErrStr(rc), (char*)0); |
drh | 483750b | 2003-01-29 18:46:51 +0000 | [diff] [blame] | 4859 | } |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 4860 | goto vdbe_halt; |
| 4861 | |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 4862 | /* Jump to here if the sqlite3_interrupt() API sets the interrupt |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 4863 | ** flag. |
| 4864 | */ |
| 4865 | abort_due_to_interrupt: |
| 4866 | assert( db->flags & SQLITE_Interrupt ); |
| 4867 | db->flags &= ~SQLITE_Interrupt; |
| 4868 | if( db->magic!=SQLITE_MAGIC_BUSY ){ |
| 4869 | rc = SQLITE_MISUSE; |
| 4870 | }else{ |
| 4871 | rc = SQLITE_INTERRUPT; |
| 4872 | } |
danielk1977 | 026d270 | 2004-06-14 13:14:59 +0000 | [diff] [blame] | 4873 | p->rc = rc; |
danielk1977 | f20b21c | 2004-05-31 23:56:42 +0000 | [diff] [blame] | 4874 | sqlite3SetString(&p->zErrMsg, sqlite3ErrStr(rc), (char*)0); |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 4875 | goto vdbe_halt; |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 4876 | } |