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 |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 25 | ** and 5 operands. Operands P1, P2, and P3 are integers. Operand P4 |
| 26 | ** is a null-terminated string. Operand P5 is an unsigned character. |
| 27 | ** Few opcodes use all 5 operands. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 28 | ** |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 29 | ** Computation results are stored on a set of registers numbered beginning |
| 30 | ** with 1 and going up to Vdbe.nMem. Each register can store |
| 31 | ** either an integer, a null-terminated string, a floating point |
shane | 21e7feb | 2008-05-30 15:59:49 +0000 | [diff] [blame] | 32 | ** number, or the SQL "NULL" value. An implicit conversion from one |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 33 | ** type to the other occurs as necessary. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 34 | ** |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 35 | ** Most of the code in this file is taken up by the sqlite3VdbeExec() |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 36 | ** function which does the work of interpreting a VDBE program. |
| 37 | ** But other routines are also provided to help in building up |
| 38 | ** a program instruction by instruction. |
| 39 | ** |
drh | ac82fcf | 2002-09-08 17:23:41 +0000 | [diff] [blame] | 40 | ** Various scripts scan this source file in order to generate HTML |
| 41 | ** documentation, headers files, or other derived files. The formatting |
| 42 | ** of the code in this file is, therefore, important. See other comments |
| 43 | ** in this file for details. If in doubt, do not deviate from existing |
| 44 | ** commenting and indentation practices when changing or adding code. |
| 45 | ** |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame^] | 46 | ** $Id: vdbe.c,v 1.761 2008/07/11 21:02:54 drh Exp $ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 47 | */ |
| 48 | #include "sqliteInt.h" |
drh | 7c68d60 | 2000-10-11 19:28:51 +0000 | [diff] [blame] | 49 | #include <ctype.h> |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 50 | #include "vdbeInt.h" |
drh | 8f619cc | 2002-09-08 00:04:50 +0000 | [diff] [blame] | 51 | |
| 52 | /* |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 53 | ** The following global variable is incremented every time a cursor |
drh | 7cf6e4d | 2004-05-19 14:56:55 +0000 | [diff] [blame] | 54 | ** 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] | 55 | ** procedures use this information to make sure that indices are |
drh | ac82fcf | 2002-09-08 17:23:41 +0000 | [diff] [blame] | 56 | ** working correctly. This variable has no function other than to |
| 57 | ** help verify the correct operation of the library. |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 58 | */ |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 59 | #ifdef SQLITE_TEST |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 60 | int sqlite3_search_count = 0; |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 61 | #endif |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 62 | |
drh | f603871 | 2004-02-08 18:07:34 +0000 | [diff] [blame] | 63 | /* |
| 64 | ** When this global variable is positive, it gets decremented once before |
drh | 881feaa | 2006-07-26 01:39:30 +0000 | [diff] [blame] | 65 | ** each instruction in the VDBE. When reaches zero, the u1.isInterrupted |
| 66 | ** field of the sqlite3 structure is set in order to simulate and interrupt. |
drh | f603871 | 2004-02-08 18:07:34 +0000 | [diff] [blame] | 67 | ** |
| 68 | ** This facility is used for testing purposes only. It does not function |
| 69 | ** in an ordinary build. |
| 70 | */ |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 71 | #ifdef SQLITE_TEST |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 72 | int sqlite3_interrupt_count = 0; |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 73 | #endif |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 74 | |
danielk1977 | 7e18c25 | 2004-05-25 11:47:24 +0000 | [diff] [blame] | 75 | /* |
drh | 6bf8957 | 2004-11-03 16:27:01 +0000 | [diff] [blame] | 76 | ** The next global variable is incremented each type the OP_Sort opcode |
| 77 | ** is executed. The test procedures use this information to make sure that |
shane | 21e7feb | 2008-05-30 15:59:49 +0000 | [diff] [blame] | 78 | ** sorting is occurring or not occurring at appropriate times. This variable |
drh | 6bf8957 | 2004-11-03 16:27:01 +0000 | [diff] [blame] | 79 | ** has no function other than to help verify the correct operation of the |
| 80 | ** library. |
| 81 | */ |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 82 | #ifdef SQLITE_TEST |
drh | 6bf8957 | 2004-11-03 16:27:01 +0000 | [diff] [blame] | 83 | int sqlite3_sort_count = 0; |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 84 | #endif |
drh | 6bf8957 | 2004-11-03 16:27:01 +0000 | [diff] [blame] | 85 | |
| 86 | /* |
drh | ae7e151 | 2007-05-02 16:51:59 +0000 | [diff] [blame] | 87 | ** The next global variable records the size of the largest MEM_Blob |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 88 | ** or MEM_Str that has been used by a VDBE opcode. The test procedures |
drh | ae7e151 | 2007-05-02 16:51:59 +0000 | [diff] [blame] | 89 | ** use this information to make sure that the zero-blob functionality |
| 90 | ** is working correctly. This variable has no function other than to |
| 91 | ** help verify the correct operation of the library. |
| 92 | */ |
| 93 | #ifdef SQLITE_TEST |
| 94 | int sqlite3_max_blobsize = 0; |
drh | ca48c90 | 2008-01-18 14:08:24 +0000 | [diff] [blame] | 95 | static void updateMaxBlobsize(Mem *p){ |
| 96 | if( (p->flags & (MEM_Str|MEM_Blob))!=0 && p->n>sqlite3_max_blobsize ){ |
| 97 | sqlite3_max_blobsize = p->n; |
| 98 | } |
| 99 | } |
drh | ae7e151 | 2007-05-02 16:51:59 +0000 | [diff] [blame] | 100 | #endif |
| 101 | |
| 102 | /* |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 103 | ** Test a register to see if it exceeds the current maximum blob size. |
| 104 | ** If it does, record the new maximum blob size. |
| 105 | */ |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 106 | #if defined(SQLITE_TEST) && !defined(SQLITE_OMIT_BUILTIN_TEST) |
drh | ca48c90 | 2008-01-18 14:08:24 +0000 | [diff] [blame] | 107 | # define UPDATE_MAX_BLOBSIZE(P) updateMaxBlobsize(P) |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 108 | #else |
| 109 | # define UPDATE_MAX_BLOBSIZE(P) |
| 110 | #endif |
| 111 | |
| 112 | /* |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 113 | ** Release the memory associated with a register. This |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 114 | ** leaves the Mem.flags field in an inconsistent state. |
drh | c61053b | 2000-06-04 12:58:36 +0000 | [diff] [blame] | 115 | */ |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 116 | #define Release(P) if((P)->flags&MEM_Dyn){ sqlite3VdbeMemRelease(P); } |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 117 | |
| 118 | /* |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 119 | ** Convert the given register into a string if it isn't one |
danielk1977 | bd7e460 | 2004-05-24 07:34:48 +0000 | [diff] [blame] | 120 | ** already. Return non-zero if a malloc() fails. |
| 121 | */ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 122 | #define Stringify(P, enc) \ |
| 123 | if(((P)->flags&(MEM_Str|MEM_Blob))==0 && sqlite3VdbeMemStringify(P,enc)) \ |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 124 | { goto no_mem; } |
danielk1977 | bd7e460 | 2004-05-24 07:34:48 +0000 | [diff] [blame] | 125 | |
| 126 | /* |
danielk1977 | bd7e460 | 2004-05-24 07:34:48 +0000 | [diff] [blame] | 127 | ** An ephemeral string value (signified by the MEM_Ephem flag) contains |
| 128 | ** a pointer to a dynamically allocated string where some other entity |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 129 | ** is responsible for deallocating that string. Because the register |
| 130 | ** does not control the string, it might be deleted without the register |
| 131 | ** knowing it. |
danielk1977 | bd7e460 | 2004-05-24 07:34:48 +0000 | [diff] [blame] | 132 | ** |
| 133 | ** This routine converts an ephemeral string into a dynamically allocated |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 134 | ** string that the register itself controls. In other words, it |
danielk1977 | bd7e460 | 2004-05-24 07:34:48 +0000 | [diff] [blame] | 135 | ** converts an MEM_Ephem string into an MEM_Dyn string. |
| 136 | */ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 137 | #define Deephemeralize(P) \ |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 138 | if( ((P)->flags&MEM_Ephem)!=0 \ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 139 | && sqlite3VdbeMemMakeWriteable(P) ){ goto no_mem;} |
danielk1977 | 93d4675 | 2004-05-23 13:30:58 +0000 | [diff] [blame] | 140 | |
| 141 | /* |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 142 | ** Call sqlite3VdbeMemExpandBlob() on the supplied value (type Mem*) |
| 143 | ** P if required. |
| 144 | */ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 145 | #define ExpandBlob(P) (((P)->flags&MEM_Zero)?sqlite3VdbeMemExpandBlob(P):0) |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 146 | |
| 147 | /* |
shane | 21e7feb | 2008-05-30 15:59:49 +0000 | [diff] [blame] | 148 | ** Argument pMem points at a register that will be passed to a |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 149 | ** user-defined function or returned to the user as the result of a query. |
| 150 | ** The second argument, 'db_enc' is the text encoding used by the vdbe for |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 151 | ** register variables. This routine sets the pMem->enc and pMem->type |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 152 | ** variables used by the sqlite3_value_*() routines. |
| 153 | */ |
drh | 3a41a3f | 2004-05-30 02:14:17 +0000 | [diff] [blame] | 154 | #define storeTypeInfo(A,B) _storeTypeInfo(A) |
| 155 | static void _storeTypeInfo(Mem *pMem){ |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 156 | int flags = pMem->flags; |
| 157 | if( flags & MEM_Null ){ |
drh | 9c05483 | 2004-05-31 18:51:57 +0000 | [diff] [blame] | 158 | pMem->type = SQLITE_NULL; |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 159 | } |
| 160 | else if( flags & MEM_Int ){ |
drh | 9c05483 | 2004-05-31 18:51:57 +0000 | [diff] [blame] | 161 | pMem->type = SQLITE_INTEGER; |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 162 | } |
| 163 | else if( flags & MEM_Real ){ |
drh | 9c05483 | 2004-05-31 18:51:57 +0000 | [diff] [blame] | 164 | pMem->type = SQLITE_FLOAT; |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 165 | } |
| 166 | else if( flags & MEM_Str ){ |
drh | 9c05483 | 2004-05-31 18:51:57 +0000 | [diff] [blame] | 167 | pMem->type = SQLITE_TEXT; |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 168 | }else{ |
drh | 9c05483 | 2004-05-31 18:51:57 +0000 | [diff] [blame] | 169 | pMem->type = SQLITE_BLOB; |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 170 | } |
| 171 | } |
danielk1977 | 8a6b541 | 2004-05-24 07:04:25 +0000 | [diff] [blame] | 172 | |
| 173 | /* |
drh | 3a40f69 | 2008-01-04 16:50:09 +0000 | [diff] [blame] | 174 | ** Properties of opcodes. The OPFLG_INITIALIZER macro is |
| 175 | ** created by mkopcodeh.awk during compilation. Data is obtained |
| 176 | ** from the comments following the "case OP_xxxx:" statements in |
| 177 | ** this file. |
drh | 3a40f69 | 2008-01-04 16:50:09 +0000 | [diff] [blame] | 178 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 179 | static unsigned char opcodeProperty[] = OPFLG_INITIALIZER; |
drh | 3a40f69 | 2008-01-04 16:50:09 +0000 | [diff] [blame] | 180 | |
| 181 | /* |
| 182 | ** Return true if an opcode has any of the OPFLG_xxx properties |
| 183 | ** specified by mask. |
| 184 | */ |
| 185 | int sqlite3VdbeOpcodeHasProperty(int opcode, int mask){ |
| 186 | assert( opcode>0 && opcode<sizeof(opcodeProperty) ); |
| 187 | return (opcodeProperty[opcode]&mask)!=0; |
| 188 | } |
| 189 | |
| 190 | /* |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 191 | ** Allocate cursor number iCur. Return a pointer to it. Return NULL |
| 192 | ** if we run out of memory. |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 193 | */ |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 194 | static Cursor *allocateCursor( |
| 195 | Vdbe *p, |
| 196 | int iCur, |
| 197 | Op *pOp, |
| 198 | int iDb, |
| 199 | int isBtreeCursor |
| 200 | ){ |
| 201 | /* Find the memory cell that will be used to store the blob of memory |
| 202 | ** required for this Cursor structure. It is convenient to use a |
| 203 | ** vdbe memory cell to manage the memory allocation required for a |
| 204 | ** Cursor structure for the following reasons: |
| 205 | ** |
| 206 | ** * Sometimes cursor numbers are used for a couple of different |
| 207 | ** purposes in a vdbe program. The different uses might require |
| 208 | ** different sized allocations. Memory cells provide growable |
| 209 | ** allocations. |
| 210 | ** |
| 211 | ** * When using ENABLE_MEMORY_MANAGEMENT, memory cell buffers can |
| 212 | ** be freed lazily via the sqlite3_release_memory() API. This |
| 213 | ** minimizes the number of malloc calls made by the system. |
| 214 | ** |
| 215 | ** Memory cells for cursors are allocated at the top of the address |
| 216 | ** space. Memory cell (p->nMem) corresponds to cursor 0. Space for |
| 217 | ** cursor 1 is managed by memory cell (p->nMem-1), etc. |
| 218 | */ |
| 219 | Mem *pMem = &p->aMem[p->nMem-iCur]; |
| 220 | |
danielk1977 | 5f09613 | 2008-03-28 15:44:09 +0000 | [diff] [blame] | 221 | int nByte; |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 222 | Cursor *pCx = 0; |
| 223 | /* If the opcode of pOp is OP_SetNumColumns, then pOp->p2 contains |
| 224 | ** the number of fields in the records contained in the table or |
| 225 | ** index being opened. Use this to reserve space for the |
| 226 | ** Cursor.aType[] array. |
| 227 | */ |
| 228 | int nField = 0; |
| 229 | if( pOp->opcode==OP_SetNumColumns || pOp->opcode==OP_OpenEphemeral ){ |
| 230 | nField = pOp->p2; |
| 231 | } |
danielk1977 | 5f09613 | 2008-03-28 15:44:09 +0000 | [diff] [blame] | 232 | nByte = |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 233 | sizeof(Cursor) + |
| 234 | (isBtreeCursor?sqlite3BtreeCursorSize():0) + |
| 235 | 2*nField*sizeof(u32); |
| 236 | |
drh | 290c194 | 2004-08-21 17:54:45 +0000 | [diff] [blame] | 237 | assert( iCur<p->nCursor ); |
| 238 | if( p->apCsr[iCur] ){ |
danielk1977 | be71889 | 2006-06-23 08:05:19 +0000 | [diff] [blame] | 239 | sqlite3VdbeFreeCursor(p, p->apCsr[iCur]); |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 240 | p->apCsr[iCur] = 0; |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 241 | } |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 242 | if( SQLITE_OK==sqlite3VdbeMemGrow(pMem, nByte, 0) ){ |
| 243 | p->apCsr[iCur] = pCx = (Cursor *)pMem->z; |
| 244 | memset(pMem->z, 0, nByte); |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 +0000 | [diff] [blame] | 245 | pCx->iDb = iDb; |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 246 | pCx->nField = nField; |
| 247 | if( nField ){ |
| 248 | pCx->aType = (u32 *)&pMem->z[sizeof(Cursor)]; |
| 249 | } |
| 250 | if( isBtreeCursor ){ |
| 251 | pCx->pCursor = (BtCursor *)&pMem->z[sizeof(Cursor)+2*nField*sizeof(u32)]; |
| 252 | } |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 +0000 | [diff] [blame] | 253 | } |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 254 | return pCx; |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 255 | } |
| 256 | |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 257 | /* |
drh | 29d7210 | 2006-02-09 22:13:41 +0000 | [diff] [blame] | 258 | ** Try to convert a value into a numeric representation if we can |
| 259 | ** do so without loss of information. In other words, if the string |
| 260 | ** looks like a number, convert it into a number. If it does not |
| 261 | ** look like a number, leave it alone. |
| 262 | */ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 263 | static void applyNumericAffinity(Mem *pRec){ |
drh | 29d7210 | 2006-02-09 22:13:41 +0000 | [diff] [blame] | 264 | if( (pRec->flags & (MEM_Real|MEM_Int))==0 ){ |
| 265 | int realnum; |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 266 | sqlite3VdbeMemNulTerminate(pRec); |
drh | 29d7210 | 2006-02-09 22:13:41 +0000 | [diff] [blame] | 267 | if( (pRec->flags&MEM_Str) |
| 268 | && sqlite3IsNumber(pRec->z, &realnum, pRec->enc) ){ |
| 269 | i64 value; |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 270 | sqlite3VdbeChangeEncoding(pRec, SQLITE_UTF8); |
drh | b6a9ece | 2007-06-26 00:37:27 +0000 | [diff] [blame] | 271 | if( !realnum && sqlite3Atoi64(pRec->z, &value) ){ |
drh | 3c024d6 | 2007-03-30 11:23:45 +0000 | [diff] [blame] | 272 | pRec->u.i = value; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 273 | MemSetTypeFlag(pRec, MEM_Int); |
drh | 29d7210 | 2006-02-09 22:13:41 +0000 | [diff] [blame] | 274 | }else{ |
| 275 | sqlite3VdbeMemRealify(pRec); |
| 276 | } |
| 277 | } |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | /* |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 282 | ** Processing is determine by the affinity parameter: |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 283 | ** |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 284 | ** SQLITE_AFF_INTEGER: |
| 285 | ** SQLITE_AFF_REAL: |
| 286 | ** SQLITE_AFF_NUMERIC: |
| 287 | ** Try to convert pRec to an integer representation or a |
| 288 | ** floating-point representation if an integer representation |
| 289 | ** is not possible. Note that the integer representation is |
| 290 | ** always preferred, even if the affinity is REAL, because |
| 291 | ** an integer representation is more space efficient on disk. |
| 292 | ** |
| 293 | ** SQLITE_AFF_TEXT: |
| 294 | ** Convert pRec to a text representation. |
| 295 | ** |
| 296 | ** SQLITE_AFF_NONE: |
| 297 | ** No-op. pRec is unchanged. |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 298 | */ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 299 | static void applyAffinity( |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 300 | Mem *pRec, /* The value to apply affinity to */ |
| 301 | char affinity, /* The affinity to be applied */ |
| 302 | u8 enc /* Use this text encoding */ |
| 303 | ){ |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 304 | if( affinity==SQLITE_AFF_TEXT ){ |
drh | 17c4029 | 2004-07-21 02:53:29 +0000 | [diff] [blame] | 305 | /* Only attempt the conversion to TEXT if there is an integer or real |
| 306 | ** representation (blob and NULL do not get converted) but no string |
| 307 | ** representation. |
| 308 | */ |
| 309 | if( 0==(pRec->flags&MEM_Str) && (pRec->flags&(MEM_Real|MEM_Int)) ){ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 310 | sqlite3VdbeMemStringify(pRec, enc); |
drh | 17c4029 | 2004-07-21 02:53:29 +0000 | [diff] [blame] | 311 | } |
| 312 | pRec->flags &= ~(MEM_Real|MEM_Int); |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 313 | }else if( affinity!=SQLITE_AFF_NONE ){ |
| 314 | assert( affinity==SQLITE_AFF_INTEGER || affinity==SQLITE_AFF_REAL |
| 315 | || affinity==SQLITE_AFF_NUMERIC ); |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 316 | applyNumericAffinity(pRec); |
drh | 29d7210 | 2006-02-09 22:13:41 +0000 | [diff] [blame] | 317 | if( pRec->flags & MEM_Real ){ |
drh | 8df447f | 2005-11-01 15:48:24 +0000 | [diff] [blame] | 318 | sqlite3VdbeIntegerAffinity(pRec); |
drh | 17c4029 | 2004-07-21 02:53:29 +0000 | [diff] [blame] | 319 | } |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 320 | } |
| 321 | } |
| 322 | |
danielk1977 | aee18ef | 2005-03-09 12:26:50 +0000 | [diff] [blame] | 323 | /* |
drh | 29d7210 | 2006-02-09 22:13:41 +0000 | [diff] [blame] | 324 | ** Try to convert the type of a function argument or a result column |
| 325 | ** into a numeric representation. Use either INTEGER or REAL whichever |
| 326 | ** is appropriate. But only do the conversion if it is possible without |
| 327 | ** loss of information and return the revised type of the argument. |
| 328 | ** |
| 329 | ** This is an EXPERIMENTAL api and is subject to change or removal. |
| 330 | */ |
| 331 | int sqlite3_value_numeric_type(sqlite3_value *pVal){ |
| 332 | Mem *pMem = (Mem*)pVal; |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 333 | applyNumericAffinity(pMem); |
drh | 29d7210 | 2006-02-09 22:13:41 +0000 | [diff] [blame] | 334 | storeTypeInfo(pMem, 0); |
| 335 | return pMem->type; |
| 336 | } |
| 337 | |
| 338 | /* |
danielk1977 | aee18ef | 2005-03-09 12:26:50 +0000 | [diff] [blame] | 339 | ** Exported version of applyAffinity(). This one works on sqlite3_value*, |
| 340 | ** not the internal Mem* type. |
| 341 | */ |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 342 | void sqlite3ValueApplyAffinity( |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 343 | sqlite3_value *pVal, |
| 344 | u8 affinity, |
| 345 | u8 enc |
| 346 | ){ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 347 | applyAffinity((Mem *)pVal, affinity, enc); |
danielk1977 | aee18ef | 2005-03-09 12:26:50 +0000 | [diff] [blame] | 348 | } |
| 349 | |
danielk1977 | b5402fb | 2005-01-12 07:15:04 +0000 | [diff] [blame] | 350 | #ifdef SQLITE_DEBUG |
drh | b6f5452 | 2004-05-20 02:42:16 +0000 | [diff] [blame] | 351 | /* |
danielk1977 | ca6b291 | 2004-05-21 10:49:47 +0000 | [diff] [blame] | 352 | ** Write a nice string representation of the contents of cell pMem |
| 353 | ** into buffer zBuf, length nBuf. |
| 354 | */ |
drh | 7416170 | 2006-02-24 02:53:49 +0000 | [diff] [blame] | 355 | void sqlite3VdbeMemPrettyPrint(Mem *pMem, char *zBuf){ |
danielk1977 | ca6b291 | 2004-05-21 10:49:47 +0000 | [diff] [blame] | 356 | char *zCsr = zBuf; |
| 357 | int f = pMem->flags; |
| 358 | |
drh | 5719628 | 2004-10-06 15:41:16 +0000 | [diff] [blame] | 359 | static const char *const encnames[] = {"(X)", "(8)", "(16LE)", "(16BE)"}; |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame] | 360 | |
danielk1977 | ca6b291 | 2004-05-21 10:49:47 +0000 | [diff] [blame] | 361 | if( f&MEM_Blob ){ |
| 362 | int i; |
| 363 | char c; |
| 364 | if( f & MEM_Dyn ){ |
| 365 | c = 'z'; |
| 366 | assert( (f & (MEM_Static|MEM_Ephem))==0 ); |
| 367 | }else if( f & MEM_Static ){ |
| 368 | c = 't'; |
| 369 | assert( (f & (MEM_Dyn|MEM_Ephem))==0 ); |
| 370 | }else if( f & MEM_Ephem ){ |
| 371 | c = 'e'; |
| 372 | assert( (f & (MEM_Static|MEM_Dyn))==0 ); |
| 373 | }else{ |
| 374 | c = 's'; |
| 375 | } |
| 376 | |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 377 | sqlite3_snprintf(100, zCsr, "%c", c); |
| 378 | zCsr += strlen(zCsr); |
| 379 | sqlite3_snprintf(100, zCsr, "%d[", pMem->n); |
| 380 | zCsr += strlen(zCsr); |
danielk1977 | ca6b291 | 2004-05-21 10:49:47 +0000 | [diff] [blame] | 381 | for(i=0; i<16 && i<pMem->n; i++){ |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 382 | sqlite3_snprintf(100, zCsr, "%02X", ((int)pMem->z[i] & 0xFF)); |
| 383 | zCsr += strlen(zCsr); |
danielk1977 | ca6b291 | 2004-05-21 10:49:47 +0000 | [diff] [blame] | 384 | } |
| 385 | for(i=0; i<16 && i<pMem->n; i++){ |
| 386 | char z = pMem->z[i]; |
| 387 | if( z<32 || z>126 ) *zCsr++ = '.'; |
| 388 | else *zCsr++ = z; |
| 389 | } |
| 390 | |
drh | e718efe | 2007-05-10 21:14:03 +0000 | [diff] [blame] | 391 | sqlite3_snprintf(100, zCsr, "]%s", encnames[pMem->enc]); |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 392 | zCsr += strlen(zCsr); |
drh | fdf972a | 2007-05-02 13:30:27 +0000 | [diff] [blame] | 393 | if( f & MEM_Zero ){ |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 394 | sqlite3_snprintf(100, zCsr,"+%lldz",pMem->u.i); |
| 395 | zCsr += strlen(zCsr); |
drh | fdf972a | 2007-05-02 13:30:27 +0000 | [diff] [blame] | 396 | } |
danielk1977 | b1bc953 | 2004-05-22 03:05:33 +0000 | [diff] [blame] | 397 | *zCsr = '\0'; |
| 398 | }else if( f & MEM_Str ){ |
| 399 | int j, k; |
| 400 | zBuf[0] = ' '; |
| 401 | if( f & MEM_Dyn ){ |
| 402 | zBuf[1] = 'z'; |
| 403 | assert( (f & (MEM_Static|MEM_Ephem))==0 ); |
| 404 | }else if( f & MEM_Static ){ |
| 405 | zBuf[1] = 't'; |
| 406 | assert( (f & (MEM_Dyn|MEM_Ephem))==0 ); |
| 407 | }else if( f & MEM_Ephem ){ |
| 408 | zBuf[1] = 'e'; |
| 409 | assert( (f & (MEM_Static|MEM_Dyn))==0 ); |
| 410 | }else{ |
| 411 | zBuf[1] = 's'; |
| 412 | } |
| 413 | k = 2; |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 414 | sqlite3_snprintf(100, &zBuf[k], "%d", pMem->n); |
| 415 | k += strlen(&zBuf[k]); |
danielk1977 | b1bc953 | 2004-05-22 03:05:33 +0000 | [diff] [blame] | 416 | zBuf[k++] = '['; |
| 417 | for(j=0; j<15 && j<pMem->n; j++){ |
| 418 | u8 c = pMem->z[j]; |
danielk1977 | b1bc953 | 2004-05-22 03:05:33 +0000 | [diff] [blame] | 419 | if( c>=0x20 && c<0x7f ){ |
| 420 | zBuf[k++] = c; |
| 421 | }else{ |
| 422 | zBuf[k++] = '.'; |
| 423 | } |
| 424 | } |
| 425 | zBuf[k++] = ']'; |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 426 | sqlite3_snprintf(100,&zBuf[k], encnames[pMem->enc]); |
| 427 | k += strlen(&zBuf[k]); |
danielk1977 | b1bc953 | 2004-05-22 03:05:33 +0000 | [diff] [blame] | 428 | zBuf[k++] = 0; |
danielk1977 | ca6b291 | 2004-05-21 10:49:47 +0000 | [diff] [blame] | 429 | } |
danielk1977 | ca6b291 | 2004-05-21 10:49:47 +0000 | [diff] [blame] | 430 | } |
| 431 | #endif |
| 432 | |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 433 | #ifdef SQLITE_DEBUG |
| 434 | /* |
| 435 | ** Print the value of a register for tracing purposes: |
| 436 | */ |
| 437 | static void memTracePrint(FILE *out, Mem *p){ |
| 438 | if( p->flags & MEM_Null ){ |
| 439 | fprintf(out, " NULL"); |
| 440 | }else if( (p->flags & (MEM_Int|MEM_Str))==(MEM_Int|MEM_Str) ){ |
| 441 | fprintf(out, " si:%lld", p->u.i); |
| 442 | }else if( p->flags & MEM_Int ){ |
| 443 | fprintf(out, " i:%lld", p->u.i); |
| 444 | }else if( p->flags & MEM_Real ){ |
| 445 | fprintf(out, " r:%g", p->r); |
| 446 | }else{ |
| 447 | char zBuf[200]; |
| 448 | sqlite3VdbeMemPrettyPrint(p, zBuf); |
| 449 | fprintf(out, " "); |
| 450 | fprintf(out, "%s", zBuf); |
| 451 | } |
| 452 | } |
| 453 | static void registerTrace(FILE *out, int iReg, Mem *p){ |
| 454 | fprintf(out, "REG[%d] = ", iReg); |
| 455 | memTracePrint(out, p); |
| 456 | fprintf(out, "\n"); |
| 457 | } |
| 458 | #endif |
| 459 | |
| 460 | #ifdef SQLITE_DEBUG |
drh | b21e7c7 | 2008-06-22 12:37:57 +0000 | [diff] [blame] | 461 | # define REGISTER_TRACE(R,M) if(p->trace)registerTrace(p->trace,R,M) |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 462 | #else |
| 463 | # define REGISTER_TRACE(R,M) |
| 464 | #endif |
| 465 | |
danielk1977 | 84ac9d0 | 2004-05-18 09:58:06 +0000 | [diff] [blame] | 466 | |
drh | 7b39686 | 2003-01-01 23:06:20 +0000 | [diff] [blame] | 467 | #ifdef VDBE_PROFILE |
shane | 9bcbdad | 2008-05-29 20:22:37 +0000 | [diff] [blame] | 468 | |
| 469 | /* |
| 470 | ** hwtime.h contains inline assembler code for implementing |
| 471 | ** high-performance timing routines. |
drh | 7b39686 | 2003-01-01 23:06:20 +0000 | [diff] [blame] | 472 | */ |
shane | 9bcbdad | 2008-05-29 20:22:37 +0000 | [diff] [blame] | 473 | #include "hwtime.h" |
| 474 | |
drh | 7b39686 | 2003-01-01 23:06:20 +0000 | [diff] [blame] | 475 | #endif |
| 476 | |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 477 | /* |
drh | caec2f1 | 2003-01-07 02:47:47 +0000 | [diff] [blame] | 478 | ** The CHECK_FOR_INTERRUPT macro defined here looks to see if the |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 479 | ** sqlite3_interrupt() routine has been called. If it has been, then |
drh | caec2f1 | 2003-01-07 02:47:47 +0000 | [diff] [blame] | 480 | ** processing of the VDBE program is interrupted. |
| 481 | ** |
| 482 | ** This macro added to every instruction that does a jump in order to |
| 483 | ** implement a loop. This test used to be on every single instruction, |
| 484 | ** but that meant we more testing that we needed. By only testing the |
| 485 | ** flag on jump instructions, we get a (small) speed improvement. |
| 486 | */ |
| 487 | #define CHECK_FOR_INTERRUPT \ |
drh | 881feaa | 2006-07-26 01:39:30 +0000 | [diff] [blame] | 488 | if( db->u1.isInterrupted ) goto abort_due_to_interrupt; |
drh | caec2f1 | 2003-01-07 02:47:47 +0000 | [diff] [blame] | 489 | |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 490 | #ifdef SQLITE_DEBUG |
| 491 | static int fileExists(sqlite3 *db, const char *zFile){ |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame] | 492 | int res = 0; |
| 493 | int rc = SQLITE_OK; |
| 494 | #ifdef SQLITE_TEST |
| 495 | /* If we are currently testing IO errors, then do not call OsAccess() to |
| 496 | ** test for the presence of zFile. This is because any IO error that |
| 497 | ** occurs here will not be reported, causing the test to fail. |
| 498 | */ |
| 499 | extern int sqlite3_io_error_pending; |
| 500 | if( sqlite3_io_error_pending<=0 ) |
| 501 | #endif |
| 502 | rc = sqlite3OsAccess(db->pVfs, zFile, SQLITE_ACCESS_EXISTS, &res); |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 503 | return (res && rc==SQLITE_OK); |
| 504 | } |
| 505 | #endif |
drh | caec2f1 | 2003-01-07 02:47:47 +0000 | [diff] [blame] | 506 | |
| 507 | /* |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 508 | ** Execute as much of a VDBE program as we can then return. |
| 509 | ** |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 510 | ** sqlite3VdbeMakeReady() must be called before this routine in order to |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 511 | ** close the program with a final OP_Halt and to set up the callbacks |
| 512 | ** and the error message pointer. |
| 513 | ** |
| 514 | ** Whenever a row or result data is available, this routine will either |
| 515 | ** invoke the result callback (if there is one) or return with |
drh | 326dce7 | 2003-01-29 14:06:07 +0000 | [diff] [blame] | 516 | ** SQLITE_ROW. |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 517 | ** |
| 518 | ** If an attempt is made to open a locked database, then this routine |
| 519 | ** will either invoke the busy callback (if there is one) or it will |
| 520 | ** return SQLITE_BUSY. |
| 521 | ** |
| 522 | ** If an error occurs, an error message is written to memory obtained |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 523 | ** from sqlite3_malloc() and p->zErrMsg is made to point to that memory. |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 524 | ** The error code is stored in p->rc and this routine returns SQLITE_ERROR. |
| 525 | ** |
| 526 | ** If the callback ever returns non-zero, then the program exits |
| 527 | ** immediately. There will be no error message but the p->rc field is |
| 528 | ** set to SQLITE_ABORT and this routine will return SQLITE_ERROR. |
| 529 | ** |
drh | 9468c7f | 2003-03-07 19:50:07 +0000 | [diff] [blame] | 530 | ** A memory allocation error causes p->rc to be set to SQLITE_NOMEM and this |
| 531 | ** routine to return SQLITE_ERROR. |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 532 | ** |
| 533 | ** Other fatal errors return SQLITE_ERROR. |
| 534 | ** |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 535 | ** After this routine has finished, sqlite3VdbeFinalize() should be |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 536 | ** used to clean up the mess that was left behind. |
| 537 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 538 | int sqlite3VdbeExec( |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 539 | Vdbe *p /* The VDBE */ |
| 540 | ){ |
| 541 | int pc; /* The program counter */ |
| 542 | Op *pOp; /* Current operation */ |
| 543 | int rc = SQLITE_OK; /* Value to return */ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 544 | sqlite3 *db = p->db; /* The database */ |
drh | 8079a0d | 2006-01-12 17:20:50 +0000 | [diff] [blame] | 545 | u8 encoding = ENC(db); /* The database encoding */ |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 546 | Mem *pIn1, *pIn2, *pIn3; /* Input operands */ |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 547 | Mem *pOut; /* Output operand */ |
drh | b1fdb2a | 2008-01-05 04:06:03 +0000 | [diff] [blame] | 548 | u8 opProperty; |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 549 | int iCompare = 0; /* Result of last OP_Compare operation */ |
| 550 | int *aPermute = 0; /* Permuation of columns for OP_Compare */ |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 551 | #ifdef VDBE_PROFILE |
shane | 9bcbdad | 2008-05-29 20:22:37 +0000 | [diff] [blame] | 552 | u64 start; /* CPU clock count at start of opcode */ |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 553 | int origPc; /* Program counter at start of opcode */ |
| 554 | #endif |
danielk1977 | 348bb5d | 2003-10-18 09:37:26 +0000 | [diff] [blame] | 555 | #ifndef SQLITE_OMIT_PROGRESS_CALLBACK |
| 556 | int nProgressOps = 0; /* Opcodes executed since progress callback. */ |
| 557 | #endif |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 558 | |
drh | ca48c90 | 2008-01-18 14:08:24 +0000 | [diff] [blame] | 559 | assert( p->magic==VDBE_MAGIC_RUN ); /* sqlite3_step() verifies this */ |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 560 | assert( db->magic==SQLITE_MAGIC_BUSY ); |
drh | 4cf7c7f | 2007-08-28 23:28:07 +0000 | [diff] [blame] | 561 | sqlite3BtreeMutexArrayEnter(&p->aMutex); |
danielk1977 | 2e588c7 | 2005-12-09 14:25:08 +0000 | [diff] [blame] | 562 | if( p->rc==SQLITE_NOMEM ){ |
| 563 | /* This happens if a malloc() inside a call to sqlite3_column_text() or |
| 564 | ** sqlite3_column_text16() failed. */ |
| 565 | goto no_mem; |
| 566 | } |
drh | 3a84069 | 2003-01-29 22:58:26 +0000 | [diff] [blame] | 567 | assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY ); |
| 568 | p->rc = SQLITE_OK; |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 569 | assert( p->explain==0 ); |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 570 | p->pResultSet = 0; |
drh | a4afb65 | 2005-07-09 02:16:02 +0000 | [diff] [blame] | 571 | db->busyHandler.nBusy = 0; |
drh | 9358164 | 2004-02-12 13:02:55 +0000 | [diff] [blame] | 572 | CHECK_FOR_INTERRUPT; |
drh | 602c237 | 2007-03-01 00:29:13 +0000 | [diff] [blame] | 573 | sqlite3VdbeIOTraceSql(p); |
drh | 3c23a88 | 2007-01-09 14:01:13 +0000 | [diff] [blame] | 574 | #ifdef SQLITE_DEBUG |
danielk1977 | 2d1d86f | 2008-06-20 14:59:51 +0000 | [diff] [blame] | 575 | sqlite3BeginBenignMalloc(); |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 576 | if( p->pc==0 |
| 577 | && ((p->db->flags & SQLITE_VdbeListing) || fileExists(db, "vdbe_explain")) |
drh | 3c23a88 | 2007-01-09 14:01:13 +0000 | [diff] [blame] | 578 | ){ |
| 579 | int i; |
| 580 | printf("VDBE Program Listing:\n"); |
| 581 | sqlite3VdbePrintSql(p); |
| 582 | for(i=0; i<p->nOp; i++){ |
| 583 | sqlite3VdbePrintOp(stdout, i, &p->aOp[i]); |
| 584 | } |
| 585 | } |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 586 | if( fileExists(db, "vdbe_trace") ){ |
drh | 3c23a88 | 2007-01-09 14:01:13 +0000 | [diff] [blame] | 587 | p->trace = stdout; |
| 588 | } |
danielk1977 | 2d1d86f | 2008-06-20 14:59:51 +0000 | [diff] [blame] | 589 | sqlite3EndBenignMalloc(); |
drh | 3c23a88 | 2007-01-09 14:01:13 +0000 | [diff] [blame] | 590 | #endif |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 591 | for(pc=p->pc; rc==SQLITE_OK; pc++){ |
drh | caec2f1 | 2003-01-07 02:47:47 +0000 | [diff] [blame] | 592 | assert( pc>=0 && pc<p->nOp ); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 593 | if( db->mallocFailed ) goto no_mem; |
drh | 7b39686 | 2003-01-01 23:06:20 +0000 | [diff] [blame] | 594 | #ifdef VDBE_PROFILE |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 595 | origPc = pc; |
shane | 9bcbdad | 2008-05-29 20:22:37 +0000 | [diff] [blame] | 596 | start = sqlite3Hwtime(); |
drh | 7b39686 | 2003-01-01 23:06:20 +0000 | [diff] [blame] | 597 | #endif |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 598 | pOp = &p->aOp[pc]; |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 599 | |
danielk1977 | 8b60e0f | 2005-01-12 09:10:39 +0000 | [diff] [blame] | 600 | /* Only allow tracing if SQLITE_DEBUG is defined. |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 601 | */ |
danielk1977 | 8b60e0f | 2005-01-12 09:10:39 +0000 | [diff] [blame] | 602 | #ifdef SQLITE_DEBUG |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 603 | if( p->trace ){ |
drh | 3f7d4e4 | 2004-07-24 14:35:58 +0000 | [diff] [blame] | 604 | if( pc==0 ){ |
| 605 | printf("VDBE Execution Trace:\n"); |
| 606 | sqlite3VdbePrintSql(p); |
| 607 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 608 | sqlite3VdbePrintOp(p->trace, pc, pOp); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 609 | } |
drh | 19db935 | 2008-03-27 22:42:51 +0000 | [diff] [blame] | 610 | if( p->trace==0 && pc==0 ){ |
danielk1977 | 2d1d86f | 2008-06-20 14:59:51 +0000 | [diff] [blame] | 611 | sqlite3BeginBenignMalloc(); |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 612 | if( fileExists(db, "vdbe_sqltrace") ){ |
drh | 19db935 | 2008-03-27 22:42:51 +0000 | [diff] [blame] | 613 | sqlite3VdbePrintSql(p); |
| 614 | } |
danielk1977 | 2d1d86f | 2008-06-20 14:59:51 +0000 | [diff] [blame] | 615 | sqlite3EndBenignMalloc(); |
drh | 3f7d4e4 | 2004-07-24 14:35:58 +0000 | [diff] [blame] | 616 | } |
| 617 | #endif |
| 618 | |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 619 | |
drh | f603871 | 2004-02-08 18:07:34 +0000 | [diff] [blame] | 620 | /* Check to see if we need to simulate an interrupt. This only happens |
| 621 | ** if we have a special test build. |
| 622 | */ |
| 623 | #ifdef SQLITE_TEST |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 624 | if( sqlite3_interrupt_count>0 ){ |
| 625 | sqlite3_interrupt_count--; |
| 626 | if( sqlite3_interrupt_count==0 ){ |
| 627 | sqlite3_interrupt(db); |
drh | f603871 | 2004-02-08 18:07:34 +0000 | [diff] [blame] | 628 | } |
| 629 | } |
| 630 | #endif |
| 631 | |
danielk1977 | 348bb5d | 2003-10-18 09:37:26 +0000 | [diff] [blame] | 632 | #ifndef SQLITE_OMIT_PROGRESS_CALLBACK |
| 633 | /* Call the progress callback if it is configured and the required number |
| 634 | ** of VDBE ops have been executed (either since this invocation of |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 635 | ** sqlite3VdbeExec() or since last time the progress callback was called). |
danielk1977 | 348bb5d | 2003-10-18 09:37:26 +0000 | [diff] [blame] | 636 | ** If the progress callback returns non-zero, exit the virtual machine with |
| 637 | ** a return code SQLITE_ABORT. |
| 638 | */ |
drh | 3914aed | 2004-01-31 20:40:42 +0000 | [diff] [blame] | 639 | if( db->xProgress ){ |
| 640 | if( db->nProgressOps==nProgressOps ){ |
danielk1977 | de523ac | 2007-06-15 14:53:53 +0000 | [diff] [blame] | 641 | int prc; |
drh | f8888bb | 2006-05-26 19:57:19 +0000 | [diff] [blame] | 642 | if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse; |
danielk1977 | de523ac | 2007-06-15 14:53:53 +0000 | [diff] [blame] | 643 | prc =db->xProgress(db->pProgressArg); |
drh | f8888bb | 2006-05-26 19:57:19 +0000 | [diff] [blame] | 644 | if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse; |
danielk1977 | de523ac | 2007-06-15 14:53:53 +0000 | [diff] [blame] | 645 | if( prc!=0 ){ |
| 646 | rc = SQLITE_INTERRUPT; |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 647 | goto vdbe_error_halt; |
danielk1977 | de523ac | 2007-06-15 14:53:53 +0000 | [diff] [blame] | 648 | } |
danielk1977 | 3fe11f3 | 2007-06-13 16:49:48 +0000 | [diff] [blame] | 649 | nProgressOps = 0; |
danielk1977 | 348bb5d | 2003-10-18 09:37:26 +0000 | [diff] [blame] | 650 | } |
drh | 3914aed | 2004-01-31 20:40:42 +0000 | [diff] [blame] | 651 | nProgressOps++; |
danielk1977 | 348bb5d | 2003-10-18 09:37:26 +0000 | [diff] [blame] | 652 | } |
danielk1977 | 348bb5d | 2003-10-18 09:37:26 +0000 | [diff] [blame] | 653 | #endif |
| 654 | |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 655 | /* Do common setup processing for any opcode that is marked |
| 656 | ** with the "out2-prerelease" tag. Such opcodes have a single |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 657 | ** output which is specified by the P2 parameter. The P2 register |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 658 | ** is initialized to a NULL. |
| 659 | */ |
drh | b1fdb2a | 2008-01-05 04:06:03 +0000 | [diff] [blame] | 660 | opProperty = opcodeProperty[pOp->opcode]; |
| 661 | if( (opProperty & OPFLG_OUT2_PRERELEASE)!=0 ){ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 662 | assert( pOp->p2>0 ); |
| 663 | assert( pOp->p2<=p->nMem ); |
| 664 | pOut = &p->aMem[pOp->p2]; |
danielk1977 | 5f09613 | 2008-03-28 15:44:09 +0000 | [diff] [blame] | 665 | sqlite3VdbeMemReleaseExternal(pOut); |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 666 | pOut->flags = MEM_Null; |
drh | b1fdb2a | 2008-01-05 04:06:03 +0000 | [diff] [blame] | 667 | }else |
| 668 | |
| 669 | /* Do common setup for opcodes marked with one of the following |
| 670 | ** combinations of properties. |
| 671 | ** |
| 672 | ** in1 |
| 673 | ** in1 in2 |
| 674 | ** in1 in2 out3 |
| 675 | ** in1 in3 |
drh | b1fdb2a | 2008-01-05 04:06:03 +0000 | [diff] [blame] | 676 | ** |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 677 | ** Variables pIn1, pIn2, and pIn3 are made to point to appropriate |
| 678 | ** registers for inputs. Variable pOut points to the output register. |
drh | b1fdb2a | 2008-01-05 04:06:03 +0000 | [diff] [blame] | 679 | */ |
| 680 | if( (opProperty & OPFLG_IN1)!=0 ){ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 681 | assert( pOp->p1>0 ); |
| 682 | assert( pOp->p1<=p->nMem ); |
| 683 | pIn1 = &p->aMem[pOp->p1]; |
| 684 | REGISTER_TRACE(pOp->p1, pIn1); |
drh | b1fdb2a | 2008-01-05 04:06:03 +0000 | [diff] [blame] | 685 | if( (opProperty & OPFLG_IN2)!=0 ){ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 686 | assert( pOp->p2>0 ); |
drh | aa9b896 | 2008-01-08 02:57:55 +0000 | [diff] [blame] | 687 | assert( pOp->p2<=p->nMem ); |
| 688 | pIn2 = &p->aMem[pOp->p2]; |
| 689 | REGISTER_TRACE(pOp->p2, pIn2); |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 690 | if( (opProperty & OPFLG_OUT3)!=0 ){ |
| 691 | assert( pOp->p3>0 ); |
| 692 | assert( pOp->p3<=p->nMem ); |
| 693 | pOut = &p->aMem[pOp->p3]; |
| 694 | } |
| 695 | }else if( (opProperty & OPFLG_IN3)!=0 ){ |
| 696 | assert( pOp->p3>0 ); |
drh | aa9b896 | 2008-01-08 02:57:55 +0000 | [diff] [blame] | 697 | assert( pOp->p3<=p->nMem ); |
| 698 | pIn3 = &p->aMem[pOp->p3]; |
| 699 | REGISTER_TRACE(pOp->p3, pIn3); |
| 700 | } |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 701 | }else if( (opProperty & OPFLG_IN2)!=0 ){ |
| 702 | assert( pOp->p2>0 ); |
| 703 | assert( pOp->p2<=p->nMem ); |
| 704 | pIn2 = &p->aMem[pOp->p2]; |
| 705 | REGISTER_TRACE(pOp->p2, pIn2); |
| 706 | }else if( (opProperty & OPFLG_IN3)!=0 ){ |
| 707 | assert( pOp->p3>0 ); |
| 708 | assert( pOp->p3<=p->nMem ); |
| 709 | pIn3 = &p->aMem[pOp->p3]; |
| 710 | REGISTER_TRACE(pOp->p3, pIn3); |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 711 | } |
| 712 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 713 | switch( pOp->opcode ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 714 | |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 715 | /***************************************************************************** |
| 716 | ** What follows is a massive switch statement where each case implements a |
| 717 | ** separate instruction in the virtual machine. If we follow the usual |
| 718 | ** indentation conventions, each case should be indented by 6 spaces. But |
| 719 | ** that is a lot of wasted space on the left margin. So the code within |
| 720 | ** the switch statement will break with convention and be flush-left. Another |
| 721 | ** big comment (similar to this one) will mark the point in the code where |
| 722 | ** we transition back to normal indentation. |
drh | ac82fcf | 2002-09-08 17:23:41 +0000 | [diff] [blame] | 723 | ** |
| 724 | ** The formatting of each case is important. The makefile for SQLite |
| 725 | ** generates two C files "opcodes.h" and "opcodes.c" by scanning this |
| 726 | ** file looking for lines that begin with "case OP_". The opcodes.h files |
| 727 | ** will be filled with #defines that give unique integer values to each |
| 728 | ** 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] | 729 | ** each string is the symbolic name for the corresponding opcode. If the |
| 730 | ** case statement is followed by a comment of the form "/# same as ... #/" |
| 731 | ** that comment is used to determine the particular value of the opcode. |
drh | ac82fcf | 2002-09-08 17:23:41 +0000 | [diff] [blame] | 732 | ** |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 733 | ** Other keywords in the comment that follows each case are used to |
| 734 | ** construct the OPFLG_INITIALIZER value that initializes opcodeProperty[]. |
| 735 | ** Keywords include: in1, in2, in3, out2_prerelease, out2, out3. See |
| 736 | ** the mkopcodeh.awk script for additional information. |
danielk1977 | bc04f85 | 2005-03-29 08:26:13 +0000 | [diff] [blame] | 737 | ** |
drh | ac82fcf | 2002-09-08 17:23:41 +0000 | [diff] [blame] | 738 | ** Documentation about VDBE opcodes is generated by scanning this file |
| 739 | ** for lines of that contain "Opcode:". That line and all subsequent |
| 740 | ** comment lines are used in the generation of the opcode.html documentation |
| 741 | ** file. |
| 742 | ** |
| 743 | ** SUMMARY: |
| 744 | ** |
| 745 | ** Formatting is important to scripts that scan this file. |
| 746 | ** Do not deviate from the formatting style currently in use. |
| 747 | ** |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 748 | *****************************************************************************/ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 749 | |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 750 | /* Opcode: Goto * P2 * * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 751 | ** |
| 752 | ** An unconditional jump to address P2. |
| 753 | ** The next instruction executed will be |
| 754 | ** the one at index P2 from the beginning of |
| 755 | ** the program. |
| 756 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 757 | case OP_Goto: { /* jump */ |
drh | caec2f1 | 2003-01-07 02:47:47 +0000 | [diff] [blame] | 758 | CHECK_FOR_INTERRUPT; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 759 | pc = pOp->p2 - 1; |
| 760 | break; |
| 761 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 762 | |
drh | 2eb9537 | 2008-06-06 15:04:36 +0000 | [diff] [blame] | 763 | /* Opcode: Gosub P1 P2 * * * |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 764 | ** |
drh | 2eb9537 | 2008-06-06 15:04:36 +0000 | [diff] [blame] | 765 | ** Write the current address onto register P1 |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 766 | ** and then jump to address P2. |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 767 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 768 | case OP_Gosub: { /* jump */ |
drh | 2eb9537 | 2008-06-06 15:04:36 +0000 | [diff] [blame] | 769 | assert( pOp->p1>0 ); |
| 770 | assert( pOp->p1<=p->nMem ); |
| 771 | pIn1 = &p->aMem[pOp->p1]; |
| 772 | assert( (pIn1->flags & MEM_Dyn)==0 ); |
| 773 | pIn1->flags = MEM_Int; |
| 774 | pIn1->u.i = pc; |
| 775 | REGISTER_TRACE(pOp->p1, pIn1); |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 776 | pc = pOp->p2 - 1; |
| 777 | break; |
| 778 | } |
| 779 | |
drh | 2eb9537 | 2008-06-06 15:04:36 +0000 | [diff] [blame] | 780 | /* Opcode: Return P1 * * * * |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 781 | ** |
drh | 2eb9537 | 2008-06-06 15:04:36 +0000 | [diff] [blame] | 782 | ** Jump to the next instruction after the address in register P1. |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 783 | */ |
drh | 2eb9537 | 2008-06-06 15:04:36 +0000 | [diff] [blame] | 784 | case OP_Return: { /* in1 */ |
| 785 | assert( pIn1->flags & MEM_Int ); |
| 786 | pc = pIn1->u.i; |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 787 | break; |
| 788 | } |
| 789 | |
drh | e00ee6e | 2008-06-20 15:24:01 +0000 | [diff] [blame] | 790 | /* Opcode: Yield P1 * * * * |
| 791 | ** |
| 792 | ** Swap the program counter with the value in register P1. |
| 793 | */ |
| 794 | case OP_Yield: { |
| 795 | int pcDest; |
| 796 | assert( pOp->p1>0 ); |
| 797 | assert( pOp->p1<=p->nMem ); |
| 798 | pIn1 = &p->aMem[pOp->p1]; |
| 799 | assert( (pIn1->flags & MEM_Dyn)==0 ); |
| 800 | pIn1->flags = MEM_Int; |
| 801 | pcDest = pIn1->u.i; |
| 802 | pIn1->u.i = pc; |
| 803 | REGISTER_TRACE(pOp->p1, pIn1); |
| 804 | pc = pcDest; |
| 805 | break; |
| 806 | } |
| 807 | |
| 808 | |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 809 | /* Opcode: Halt P1 P2 * P4 * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 810 | ** |
drh | 0342b1f | 2005-09-01 03:07:44 +0000 | [diff] [blame] | 811 | ** Exit immediately. All open cursors, Fifos, etc are closed |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 812 | ** automatically. |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 813 | ** |
drh | 92f02c3 | 2004-09-02 14:57:08 +0000 | [diff] [blame] | 814 | ** P1 is the result code returned by sqlite3_exec(), sqlite3_reset(), |
| 815 | ** or sqlite3_finalize(). For a normal halt, this should be SQLITE_OK (0). |
| 816 | ** For errors, it can be some other value. If P1!=0 then P2 will determine |
| 817 | ** whether or not to rollback the current transaction. Do not rollback |
| 818 | ** if P2==OE_Fail. Do the rollback if P2==OE_Rollback. If P2==OE_Abort, |
| 819 | ** then back out all changes that have occurred during this execution of the |
drh | b798fa6 | 2002-09-03 19:43:23 +0000 | [diff] [blame] | 820 | ** VDBE, but do not rollback the transaction. |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 821 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 822 | ** If P4 is not null then it is an error message string. |
drh | 7f057c9 | 2005-06-24 03:53:06 +0000 | [diff] [blame] | 823 | ** |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 824 | ** 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] | 825 | ** every program. So a jump past the last instruction of the program |
| 826 | ** is the same as executing Halt. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 827 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 828 | case OP_Halt: { |
drh | 92f02c3 | 2004-09-02 14:57:08 +0000 | [diff] [blame] | 829 | p->rc = pOp->p1; |
| 830 | p->pc = pc; |
| 831 | p->errorAction = pOp->p2; |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 832 | if( pOp->p4.z ){ |
drh | f089aa4 | 2008-07-08 19:34:06 +0000 | [diff] [blame] | 833 | sqlite3SetString(&p->zErrMsg, db, "%s", pOp->p4.z); |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 834 | } |
drh | 92f02c3 | 2004-09-02 14:57:08 +0000 | [diff] [blame] | 835 | rc = sqlite3VdbeHalt(p); |
danielk1977 | 01427a6 | 2005-01-11 13:02:33 +0000 | [diff] [blame] | 836 | assert( rc==SQLITE_BUSY || rc==SQLITE_OK ); |
drh | 92f02c3 | 2004-09-02 14:57:08 +0000 | [diff] [blame] | 837 | if( rc==SQLITE_BUSY ){ |
drh | 900b31e | 2007-08-28 02:27:51 +0000 | [diff] [blame] | 838 | p->rc = rc = SQLITE_BUSY; |
| 839 | }else{ |
| 840 | rc = p->rc ? SQLITE_ERROR : SQLITE_DONE; |
drh | 92f02c3 | 2004-09-02 14:57:08 +0000 | [diff] [blame] | 841 | } |
drh | 900b31e | 2007-08-28 02:27:51 +0000 | [diff] [blame] | 842 | goto vdbe_return; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 843 | } |
drh | c61053b | 2000-06-04 12:58:36 +0000 | [diff] [blame] | 844 | |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 845 | /* Opcode: Integer P1 P2 * * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 846 | ** |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 847 | ** The 32-bit integer value P1 is written into register P2. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 848 | */ |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 849 | case OP_Integer: { /* out2-prerelease */ |
| 850 | pOut->flags = MEM_Int; |
| 851 | pOut->u.i = pOp->p1; |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 852 | break; |
| 853 | } |
| 854 | |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 855 | /* Opcode: Int64 * P2 * P4 * |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 856 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 857 | ** P4 is a pointer to a 64-bit integer value. |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 858 | ** Write that value into register P2. |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 859 | */ |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 860 | case OP_Int64: { /* out2-prerelease */ |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 861 | assert( pOp->p4.pI64!=0 ); |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 862 | pOut->flags = MEM_Int; |
| 863 | pOut->u.i = *pOp->p4.pI64; |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 864 | break; |
| 865 | } |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 866 | |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 867 | /* Opcode: Real * P2 * P4 * |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 868 | ** |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 869 | ** P4 is a pointer to a 64-bit floating point value. |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 870 | ** Write that value into register P2. |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 871 | */ |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 872 | case OP_Real: { /* same as TK_FLOAT, out2-prerelease */ |
| 873 | pOut->flags = MEM_Real; |
drh | 2eaf93d | 2008-04-29 00:15:20 +0000 | [diff] [blame] | 874 | assert( !sqlite3IsNaN(*pOp->p4.pReal) ); |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 875 | pOut->r = *pOp->p4.pReal; |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 876 | break; |
| 877 | } |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 878 | |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 879 | /* Opcode: String8 * P2 * P4 * |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 880 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 881 | ** P4 points to a nul terminated UTF-8 string. This opcode is transformed |
danielk1977 | 0f69c1e | 2004-05-29 11:24:50 +0000 | [diff] [blame] | 882 | ** into an OP_String before it is executed for the first time. |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 883 | */ |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 884 | case OP_String8: { /* same as TK_STRING, out2-prerelease */ |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 885 | assert( pOp->p4.z!=0 ); |
drh | ed2df7f | 2005-11-16 04:34:32 +0000 | [diff] [blame] | 886 | pOp->opcode = OP_String; |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 887 | pOp->p1 = strlen(pOp->p4.z); |
drh | ed2df7f | 2005-11-16 04:34:32 +0000 | [diff] [blame] | 888 | |
| 889 | #ifndef SQLITE_OMIT_UTF16 |
drh | 8079a0d | 2006-01-12 17:20:50 +0000 | [diff] [blame] | 890 | if( encoding!=SQLITE_UTF8 ){ |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 891 | sqlite3VdbeMemSetStr(pOut, pOp->p4.z, -1, SQLITE_UTF8, SQLITE_STATIC); |
| 892 | if( SQLITE_OK!=sqlite3VdbeChangeEncoding(pOut, encoding) ) goto no_mem; |
| 893 | if( SQLITE_OK!=sqlite3VdbeMemDynamicify(pOut) ) goto no_mem; |
danielk1977 | 5f09613 | 2008-03-28 15:44:09 +0000 | [diff] [blame] | 894 | pOut->zMalloc = 0; |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 895 | pOut->flags |= MEM_Static; |
drh | 191b54c | 2008-04-15 12:14:21 +0000 | [diff] [blame] | 896 | pOut->flags &= ~MEM_Dyn; |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 897 | if( pOp->p4type==P4_DYNAMIC ){ |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 898 | sqlite3_free(pOp->p4.z); |
danielk1977 | e004840 | 2004-06-15 16:51:01 +0000 | [diff] [blame] | 899 | } |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 900 | pOp->p4type = P4_DYNAMIC; |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 901 | pOp->p4.z = pOut->z; |
| 902 | pOp->p1 = pOut->n; |
drh | bb4957f | 2008-03-20 14:03:29 +0000 | [diff] [blame] | 903 | if( pOp->p1>db->aLimit[SQLITE_LIMIT_LENGTH] ){ |
drh | cbd2da9 | 2007-12-17 16:20:06 +0000 | [diff] [blame] | 904 | goto too_big; |
| 905 | } |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 906 | UPDATE_MAX_BLOBSIZE(pOut); |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame] | 907 | break; |
danielk1977 | 0f69c1e | 2004-05-29 11:24:50 +0000 | [diff] [blame] | 908 | } |
danielk1977 | 93758c8 | 2005-01-21 08:13:14 +0000 | [diff] [blame] | 909 | #endif |
drh | bb4957f | 2008-03-20 14:03:29 +0000 | [diff] [blame] | 910 | if( pOp->p1>db->aLimit[SQLITE_LIMIT_LENGTH] ){ |
drh | cbd2da9 | 2007-12-17 16:20:06 +0000 | [diff] [blame] | 911 | goto too_big; |
| 912 | } |
| 913 | /* Fall through to the next case, OP_String */ |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 914 | } |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 915 | |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 916 | /* Opcode: String P1 P2 * P4 * |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 917 | ** |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 918 | ** The string value P4 of length P1 (bytes) is stored in register P2. |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 919 | */ |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 920 | case OP_String: { /* out2-prerelease */ |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 921 | assert( pOp->p4.z!=0 ); |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 922 | pOut->flags = MEM_Str|MEM_Static|MEM_Term; |
| 923 | pOut->z = pOp->p4.z; |
| 924 | pOut->n = pOp->p1; |
| 925 | pOut->enc = encoding; |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 926 | UPDATE_MAX_BLOBSIZE(pOut); |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 927 | break; |
| 928 | } |
| 929 | |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 930 | /* Opcode: Null * P2 * * * |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 931 | ** |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 932 | ** Write a NULL into register P2. |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 933 | */ |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 934 | case OP_Null: { /* out2-prerelease */ |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 935 | break; |
| 936 | } |
| 937 | |
| 938 | |
drh | a71aa00 | 2004-11-03 13:59:04 +0000 | [diff] [blame] | 939 | #ifndef SQLITE_OMIT_BLOB_LITERAL |
drh | 9de221d | 2008-01-05 06:51:30 +0000 | [diff] [blame] | 940 | /* Opcode: Blob P1 P2 * P4 |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 941 | ** |
drh | 9de221d | 2008-01-05 06:51:30 +0000 | [diff] [blame] | 942 | ** P4 points to a blob of data P1 bytes long. Store this |
| 943 | ** blob in register P2. This instruction is not coded directly |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 944 | ** by the compiler. Instead, the compiler layer specifies |
| 945 | ** an OP_HexBlob opcode, with the hex string representation of |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 946 | ** the blob as P4. This opcode is transformed to an OP_Blob |
danielk1977 | 93758c8 | 2005-01-21 08:13:14 +0000 | [diff] [blame] | 947 | ** the first time it is executed. |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 948 | */ |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 949 | case OP_Blob: { /* out2-prerelease */ |
drh | cbd2da9 | 2007-12-17 16:20:06 +0000 | [diff] [blame] | 950 | assert( pOp->p1 <= SQLITE_MAX_LENGTH ); |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 951 | sqlite3VdbeMemSetStr(pOut, pOp->p4.z, pOp->p1, 0, 0); |
drh | 9de221d | 2008-01-05 06:51:30 +0000 | [diff] [blame] | 952 | pOut->enc = encoding; |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 953 | UPDATE_MAX_BLOBSIZE(pOut); |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 954 | break; |
| 955 | } |
danielk1977 | 93758c8 | 2005-01-21 08:13:14 +0000 | [diff] [blame] | 956 | #endif /* SQLITE_OMIT_BLOB_LITERAL */ |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 957 | |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 958 | /* Opcode: Variable P1 P2 * * * |
drh | 5045789 | 2003-09-06 01:10:47 +0000 | [diff] [blame] | 959 | ** |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 960 | ** The value of variable P1 is written into register P2. A variable is |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 961 | ** an unknown in the original SQL string as handed to sqlite3_compile(). |
shane | 21e7feb | 2008-05-30 15:59:49 +0000 | [diff] [blame] | 962 | ** Any occurrence of the '?' character in the original SQL is considered |
drh | 7c972de | 2003-09-06 22:18:07 +0000 | [diff] [blame] | 963 | ** a variable. Variables in the SQL string are number from left to |
| 964 | ** right beginning with 1. The values of variables are set using the |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 965 | ** sqlite3_bind() API. |
drh | 5045789 | 2003-09-06 01:10:47 +0000 | [diff] [blame] | 966 | */ |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 967 | case OP_Variable: { /* out2-prerelease */ |
drh | 7c972de | 2003-09-06 22:18:07 +0000 | [diff] [blame] | 968 | int j = pOp->p1 - 1; |
drh | 023ae03 | 2007-05-08 12:12:16 +0000 | [diff] [blame] | 969 | Mem *pVar; |
danielk1977 | 295ba55 | 2004-05-19 10:34:51 +0000 | [diff] [blame] | 970 | assert( j>=0 && j<p->nVar ); |
| 971 | |
drh | 023ae03 | 2007-05-08 12:12:16 +0000 | [diff] [blame] | 972 | pVar = &p->aVar[j]; |
| 973 | if( sqlite3VdbeMemTooBig(pVar) ){ |
| 974 | goto too_big; |
| 975 | } |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 976 | sqlite3VdbeMemShallowCopy(pOut, &p->aVar[j], MEM_Static); |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 977 | UPDATE_MAX_BLOBSIZE(pOut); |
danielk1977 | 93d4675 | 2004-05-23 13:30:58 +0000 | [diff] [blame] | 978 | break; |
| 979 | } |
danielk1977 | 295ba55 | 2004-05-19 10:34:51 +0000 | [diff] [blame] | 980 | |
drh | b21e7c7 | 2008-06-22 12:37:57 +0000 | [diff] [blame] | 981 | /* Opcode: Move P1 P2 P3 * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 982 | ** |
drh | b21e7c7 | 2008-06-22 12:37:57 +0000 | [diff] [blame] | 983 | ** Move the values in register P1..P1+P3-1 over into |
| 984 | ** registers P2..P2+P3-1. Registers P1..P1+P1-1 are |
| 985 | ** left holding a NULL. It is an error for register ranges |
| 986 | ** P1..P1+P3-1 and P2..P2+P3-1 to overlap. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 987 | */ |
drh | e1349cb | 2008-04-01 00:36:10 +0000 | [diff] [blame] | 988 | case OP_Move: { |
| 989 | char *zMalloc; |
drh | b21e7c7 | 2008-06-22 12:37:57 +0000 | [diff] [blame] | 990 | int n = pOp->p3; |
| 991 | int p1 = pOp->p1; |
| 992 | int p2 = pOp->p2; |
| 993 | assert( n>0 ); |
| 994 | assert( p1>0 ); |
| 995 | assert( p1+n<p->nMem ); |
| 996 | pIn1 = &p->aMem[p1]; |
| 997 | assert( p2>0 ); |
| 998 | assert( p2+n<p->nMem ); |
| 999 | pOut = &p->aMem[p2]; |
| 1000 | assert( p1+n<=p2 || p2+n<=p1 ); |
| 1001 | while( n-- ){ |
drh | b21e7c7 | 2008-06-22 12:37:57 +0000 | [diff] [blame] | 1002 | zMalloc = pOut->zMalloc; |
| 1003 | pOut->zMalloc = 0; |
| 1004 | sqlite3VdbeMemMove(pOut, pIn1); |
| 1005 | pIn1->zMalloc = zMalloc; |
| 1006 | REGISTER_TRACE(p2++, pOut); |
| 1007 | pIn1++; |
| 1008 | pOut++; |
| 1009 | } |
drh | e1349cb | 2008-04-01 00:36:10 +0000 | [diff] [blame] | 1010 | break; |
| 1011 | } |
| 1012 | |
drh | b1fdb2a | 2008-01-05 04:06:03 +0000 | [diff] [blame] | 1013 | /* Opcode: Copy P1 P2 * * * |
| 1014 | ** |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1015 | ** Make a copy of register P1 into register P2. |
drh | b1fdb2a | 2008-01-05 04:06:03 +0000 | [diff] [blame] | 1016 | ** |
| 1017 | ** This instruction makes a deep copy of the value. A duplicate |
| 1018 | ** is made of any string or blob constant. See also OP_SCopy. |
| 1019 | */ |
drh | e1349cb | 2008-04-01 00:36:10 +0000 | [diff] [blame] | 1020 | case OP_Copy: { |
| 1021 | assert( pOp->p1>0 ); |
| 1022 | assert( pOp->p1<=p->nMem ); |
| 1023 | pIn1 = &p->aMem[pOp->p1]; |
drh | e1349cb | 2008-04-01 00:36:10 +0000 | [diff] [blame] | 1024 | assert( pOp->p2>0 ); |
| 1025 | assert( pOp->p2<=p->nMem ); |
| 1026 | pOut = &p->aMem[pOp->p2]; |
| 1027 | assert( pOut!=pIn1 ); |
| 1028 | sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem); |
| 1029 | Deephemeralize(pOut); |
| 1030 | REGISTER_TRACE(pOp->p2, pOut); |
| 1031 | break; |
| 1032 | } |
| 1033 | |
drh | b1fdb2a | 2008-01-05 04:06:03 +0000 | [diff] [blame] | 1034 | /* Opcode: SCopy P1 P2 * * * |
| 1035 | ** |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1036 | ** Make a shallow copy of register P1 into register P2. |
drh | b1fdb2a | 2008-01-05 04:06:03 +0000 | [diff] [blame] | 1037 | ** |
| 1038 | ** This instruction makes a shallow copy of the value. If the value |
| 1039 | ** is a string or blob, then the copy is only a pointer to the |
| 1040 | ** original and hence if the original changes so will the copy. |
| 1041 | ** Worse, if the original is deallocated, the copy becomes invalid. |
| 1042 | ** Thus the program must guarantee that the original will not change |
| 1043 | ** during the lifetime of the copy. Use OP_Copy to make a complete |
| 1044 | ** copy. |
| 1045 | */ |
drh | b1fdb2a | 2008-01-05 04:06:03 +0000 | [diff] [blame] | 1046 | case OP_SCopy: { |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1047 | assert( pOp->p1>0 ); |
| 1048 | assert( pOp->p1<=p->nMem ); |
| 1049 | pIn1 = &p->aMem[pOp->p1]; |
| 1050 | REGISTER_TRACE(pOp->p1, pIn1); |
| 1051 | assert( pOp->p2>0 ); |
| 1052 | assert( pOp->p2<=p->nMem ); |
| 1053 | pOut = &p->aMem[pOp->p2]; |
drh | 2d401ab | 2008-01-10 23:50:11 +0000 | [diff] [blame] | 1054 | assert( pOut!=pIn1 ); |
drh | e1349cb | 2008-04-01 00:36:10 +0000 | [diff] [blame] | 1055 | sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem); |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1056 | REGISTER_TRACE(pOp->p2, pOut); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1057 | break; |
| 1058 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1059 | |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1060 | /* Opcode: ResultRow P1 P2 * * * |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 1061 | ** |
shane | 21e7feb | 2008-05-30 15:59:49 +0000 | [diff] [blame] | 1062 | ** The registers P1 through P1+P2-1 contain a single row of |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 1063 | ** results. This opcode causes the sqlite3_step() call to terminate |
| 1064 | ** with an SQLITE_ROW return code and it sets up the sqlite3_stmt |
| 1065 | ** structure to provide access to the top P1 values as the result |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1066 | ** row. |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 1067 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1068 | case OP_ResultRow: { |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 1069 | Mem *pMem; |
| 1070 | int i; |
| 1071 | assert( p->nResColumn==pOp->p2 ); |
drh | 0a07c10 | 2008-01-03 18:03:08 +0000 | [diff] [blame] | 1072 | assert( pOp->p1>0 ); |
| 1073 | assert( pOp->p1+pOp->p2<=p->nMem ); |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 1074 | |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 1075 | /* Invalidate all ephemeral cursor row caches */ |
| 1076 | p->cacheCtr = (p->cacheCtr + 2)|1; |
| 1077 | |
| 1078 | /* Make sure the results of the current row are \000 terminated |
shane | 21e7feb | 2008-05-30 15:59:49 +0000 | [diff] [blame] | 1079 | ** and have an assigned type. The results are de-ephemeralized as |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 1080 | ** as side effect. |
| 1081 | */ |
| 1082 | pMem = p->pResultSet = &p->aMem[pOp->p1]; |
| 1083 | for(i=0; i<pOp->p2; i++){ |
| 1084 | sqlite3VdbeMemNulTerminate(&pMem[i]); |
| 1085 | storeTypeInfo(&pMem[i], encoding); |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 1086 | REGISTER_TRACE(pOp->p1+i, &pMem[i]); |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 1087 | } |
drh | 2803969 | 2008-03-17 16:54:01 +0000 | [diff] [blame] | 1088 | if( db->mallocFailed ) goto no_mem; |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 1089 | |
| 1090 | /* Return SQLITE_ROW |
| 1091 | */ |
| 1092 | p->nCallback++; |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 1093 | p->pc = pc + 1; |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 1094 | rc = SQLITE_ROW; |
| 1095 | goto vdbe_return; |
| 1096 | } |
| 1097 | |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1098 | /* Opcode: Concat P1 P2 P3 * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1099 | ** |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1100 | ** Add the text in register P1 onto the end of the text in |
| 1101 | ** register P2 and store the result in register P3. |
| 1102 | ** If either the P1 or P2 text are NULL then store NULL in P3. |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1103 | ** |
| 1104 | ** P3 = P2 || P1 |
| 1105 | ** |
| 1106 | ** It is illegal for P1 and P3 to be the same register. Sometimes, |
| 1107 | ** if P3 is the same register as P2, the implementation is able |
| 1108 | ** to avoid a memcpy(). |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1109 | */ |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1110 | case OP_Concat: { /* same as TK_CONCAT, in1, in2, out3 */ |
drh | 023ae03 | 2007-05-08 12:12:16 +0000 | [diff] [blame] | 1111 | i64 nByte; |
danielk1977 | 8a6b541 | 2004-05-24 07:04:25 +0000 | [diff] [blame] | 1112 | |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1113 | assert( pIn1!=pOut ); |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1114 | if( (pIn1->flags | pIn2->flags) & MEM_Null ){ |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1115 | sqlite3VdbeMemSetNull(pOut); |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1116 | break; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1117 | } |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1118 | ExpandBlob(pIn1); |
| 1119 | Stringify(pIn1, encoding); |
| 1120 | ExpandBlob(pIn2); |
| 1121 | Stringify(pIn2, encoding); |
| 1122 | nByte = pIn1->n + pIn2->n; |
drh | bb4957f | 2008-03-20 14:03:29 +0000 | [diff] [blame] | 1123 | if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){ |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1124 | goto too_big; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1125 | } |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1126 | MemSetTypeFlag(pOut, MEM_Str); |
| 1127 | if( sqlite3VdbeMemGrow(pOut, nByte+2, pOut==pIn2) ){ |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1128 | goto no_mem; |
| 1129 | } |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1130 | if( pOut!=pIn2 ){ |
| 1131 | memcpy(pOut->z, pIn2->z, pIn2->n); |
| 1132 | } |
| 1133 | memcpy(&pOut->z[pIn2->n], pIn1->z, pIn1->n); |
| 1134 | pOut->z[nByte] = 0; |
| 1135 | pOut->z[nByte+1] = 0; |
| 1136 | pOut->flags |= MEM_Term; |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1137 | pOut->n = nByte; |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1138 | pOut->enc = encoding; |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 1139 | UPDATE_MAX_BLOBSIZE(pOut); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1140 | break; |
| 1141 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1142 | |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 1143 | /* Opcode: Add P1 P2 P3 * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1144 | ** |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1145 | ** Add the value in register P1 to the value in register P2 |
shane | 21e7feb | 2008-05-30 15:59:49 +0000 | [diff] [blame] | 1146 | ** and store the result in register P3. |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1147 | ** If either input is NULL, the result is NULL. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1148 | */ |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 1149 | /* Opcode: Multiply P1 P2 P3 * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1150 | ** |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 1151 | ** |
shane | 21e7feb | 2008-05-30 15:59:49 +0000 | [diff] [blame] | 1152 | ** Multiply the value in register P1 by the value in register P2 |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1153 | ** and store the result in register P3. |
| 1154 | ** If either input is NULL, the result is NULL. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1155 | */ |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 1156 | /* Opcode: Subtract P1 P2 P3 * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1157 | ** |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1158 | ** Subtract the value in register P1 from the value in register P2 |
| 1159 | ** and store the result in register P3. |
| 1160 | ** If either input is NULL, the result is NULL. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1161 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1162 | /* Opcode: Divide P1 P2 P3 * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1163 | ** |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1164 | ** Divide the value in register P1 by the value in register P2 |
| 1165 | ** and store the result in register P3. If the value in register P2 |
| 1166 | ** is zero, then the result is NULL. |
| 1167 | ** If either input is NULL, the result is NULL. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1168 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1169 | /* Opcode: Remainder P1 P2 P3 * * |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1170 | ** |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 1171 | ** Compute the remainder after integer division of the value in |
| 1172 | ** register P1 by the value in register P2 and store the result in P3. |
| 1173 | ** If the value in register P2 is zero the result is NULL. |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1174 | ** If either operand is NULL, the result is NULL. |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1175 | */ |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1176 | case OP_Add: /* same as TK_PLUS, in1, in2, out3 */ |
| 1177 | case OP_Subtract: /* same as TK_MINUS, in1, in2, out3 */ |
| 1178 | case OP_Multiply: /* same as TK_STAR, in1, in2, out3 */ |
| 1179 | case OP_Divide: /* same as TK_SLASH, in1, in2, out3 */ |
| 1180 | case OP_Remainder: { /* same as TK_REM, in1, in2, out3 */ |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1181 | int flags; |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1182 | flags = pIn1->flags | pIn2->flags; |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 1183 | if( (flags & MEM_Null)!=0 ) goto arithmetic_result_is_null; |
| 1184 | if( (pIn1->flags & pIn2->flags & MEM_Int)==MEM_Int ){ |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1185 | i64 a, b; |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1186 | a = pIn1->u.i; |
| 1187 | b = pIn2->u.i; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1188 | switch( pOp->opcode ){ |
| 1189 | case OP_Add: b += a; break; |
| 1190 | case OP_Subtract: b -= a; break; |
| 1191 | case OP_Multiply: b *= a; break; |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1192 | case OP_Divide: { |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 1193 | if( a==0 ) goto arithmetic_result_is_null; |
danielk1977 | 42d4ef2 | 2007-06-26 11:13:25 +0000 | [diff] [blame] | 1194 | /* Dividing the largest possible negative 64-bit integer (1<<63) by |
drh | 0f05035 | 2008-05-09 18:03:13 +0000 | [diff] [blame] | 1195 | ** -1 returns an integer too large to store in a 64-bit data-type. On |
danielk1977 | 42d4ef2 | 2007-06-26 11:13:25 +0000 | [diff] [blame] | 1196 | ** some architectures, the value overflows to (1<<63). On others, |
| 1197 | ** a SIGFPE is issued. The following statement normalizes this |
shane | 21e7feb | 2008-05-30 15:59:49 +0000 | [diff] [blame] | 1198 | ** behavior so that all architectures behave as if integer |
| 1199 | ** overflow occurred. |
danielk1977 | 42d4ef2 | 2007-06-26 11:13:25 +0000 | [diff] [blame] | 1200 | */ |
drh | 0f05035 | 2008-05-09 18:03:13 +0000 | [diff] [blame] | 1201 | if( a==-1 && b==SMALLEST_INT64 ) a = 1; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1202 | b /= a; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1203 | break; |
| 1204 | } |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1205 | default: { |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 1206 | if( a==0 ) goto arithmetic_result_is_null; |
danielk1977 | 42d4ef2 | 2007-06-26 11:13:25 +0000 | [diff] [blame] | 1207 | if( a==-1 ) a = 1; |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1208 | b %= a; |
| 1209 | break; |
| 1210 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1211 | } |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1212 | pOut->u.i = b; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1213 | MemSetTypeFlag(pOut, MEM_Int); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1214 | }else{ |
| 1215 | double a, b; |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1216 | a = sqlite3VdbeRealValue(pIn1); |
| 1217 | b = sqlite3VdbeRealValue(pIn2); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1218 | switch( pOp->opcode ){ |
| 1219 | case OP_Add: b += a; break; |
| 1220 | case OP_Subtract: b -= a; break; |
| 1221 | case OP_Multiply: b *= a; break; |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1222 | case OP_Divide: { |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 1223 | if( a==0.0 ) goto arithmetic_result_is_null; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1224 | b /= a; |
| 1225 | break; |
| 1226 | } |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1227 | default: { |
danielk1977 | 4b5710e | 2007-05-08 13:57:34 +0000 | [diff] [blame] | 1228 | i64 ia = (i64)a; |
| 1229 | i64 ib = (i64)b; |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 1230 | if( ia==0 ) goto arithmetic_result_is_null; |
danielk1977 | 42d4ef2 | 2007-06-26 11:13:25 +0000 | [diff] [blame] | 1231 | if( ia==-1 ) ia = 1; |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1232 | b = ib % ia; |
| 1233 | break; |
| 1234 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1235 | } |
drh | 0de3ae9 | 2008-04-28 16:55:26 +0000 | [diff] [blame] | 1236 | if( sqlite3IsNaN(b) ){ |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 1237 | goto arithmetic_result_is_null; |
drh | 53c1402 | 2007-05-10 17:23:11 +0000 | [diff] [blame] | 1238 | } |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1239 | pOut->r = b; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1240 | MemSetTypeFlag(pOut, MEM_Real); |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1241 | if( (flags & MEM_Real)==0 ){ |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1242 | sqlite3VdbeIntegerAffinity(pOut); |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1243 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1244 | } |
| 1245 | break; |
| 1246 | |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 1247 | arithmetic_result_is_null: |
| 1248 | sqlite3VdbeMemSetNull(pOut); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1249 | break; |
| 1250 | } |
| 1251 | |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 1252 | /* Opcode: CollSeq * * P4 |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 1253 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 1254 | ** P4 is a pointer to a CollSeq struct. If the next call to a user function |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 1255 | ** or aggregate calls sqlite3GetFuncCollSeq(), this collation sequence will |
| 1256 | ** be returned. This is used by the built-in min(), max() and nullif() |
drh | e6f85e7 | 2004-12-25 01:03:13 +0000 | [diff] [blame] | 1257 | ** functions. |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 1258 | ** |
| 1259 | ** The interface used by the implementation of the aforementioned functions |
| 1260 | ** to retrieve the collation sequence set by this opcode is not available |
| 1261 | ** publicly, only to user functions defined in func.c. |
| 1262 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1263 | case OP_CollSeq: { |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 1264 | assert( pOp->p4type==P4_COLLSEQ ); |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 1265 | break; |
| 1266 | } |
| 1267 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1268 | /* Opcode: Function P1 P2 P3 P4 P5 |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 1269 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 1270 | ** Invoke a user function (P4 is a pointer to a Function structure that |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1271 | ** defines the function) with P5 arguments taken from register P2 and |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1272 | ** successors. The result of the function is stored in register P3. |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1273 | ** Register P3 must not be one of the function inputs. |
danielk1977 | 682f68b | 2004-06-05 10:22:17 +0000 | [diff] [blame] | 1274 | ** |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 1275 | ** 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] | 1276 | ** function was determined to be constant at compile time. If the first |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 1277 | ** 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] | 1278 | ** whether meta data associated with a user function argument using the |
| 1279 | ** sqlite3_set_auxdata() API may be safely retained until the next |
| 1280 | ** invocation of this opcode. |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 1281 | ** |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 1282 | ** See also: AggStep and AggFinal |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 1283 | */ |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 1284 | case OP_Function: { |
danielk1977 | 51ad0ec | 2004-05-24 12:39:02 +0000 | [diff] [blame] | 1285 | int i; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1286 | Mem *pArg; |
danielk1977 | 22322fd | 2004-05-25 23:35:17 +0000 | [diff] [blame] | 1287 | sqlite3_context ctx; |
danielk1977 | 51ad0ec | 2004-05-24 12:39:02 +0000 | [diff] [blame] | 1288 | sqlite3_value **apVal; |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1289 | int n = pOp->p5; |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 1290 | |
danielk1977 | 6ddcca5 | 2004-05-24 23:48:25 +0000 | [diff] [blame] | 1291 | apVal = p->apArg; |
danielk1977 | 51ad0ec | 2004-05-24 12:39:02 +0000 | [diff] [blame] | 1292 | assert( apVal || n==0 ); |
| 1293 | |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1294 | assert( n==0 || (pOp->p2>0 && pOp->p2+n<=p->nMem) ); |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1295 | assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n ); |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1296 | pArg = &p->aMem[pOp->p2]; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1297 | for(i=0; i<n; i++, pArg++){ |
danielk1977 | 51ad0ec | 2004-05-24 12:39:02 +0000 | [diff] [blame] | 1298 | apVal[i] = pArg; |
drh | 8079a0d | 2006-01-12 17:20:50 +0000 | [diff] [blame] | 1299 | storeTypeInfo(pArg, encoding); |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 1300 | REGISTER_TRACE(pOp->p2, pArg); |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 1301 | } |
danielk1977 | 51ad0ec | 2004-05-24 12:39:02 +0000 | [diff] [blame] | 1302 | |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 1303 | assert( pOp->p4type==P4_FUNCDEF || pOp->p4type==P4_VDBEFUNC ); |
| 1304 | if( pOp->p4type==P4_FUNCDEF ){ |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 1305 | ctx.pFunc = pOp->p4.pFunc; |
danielk1977 | 682f68b | 2004-06-05 10:22:17 +0000 | [diff] [blame] | 1306 | ctx.pVdbeFunc = 0; |
| 1307 | }else{ |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 1308 | ctx.pVdbeFunc = (VdbeFunc*)pOp->p4.pVdbeFunc; |
danielk1977 | 682f68b | 2004-06-05 10:22:17 +0000 | [diff] [blame] | 1309 | ctx.pFunc = ctx.pVdbeFunc->pFunc; |
| 1310 | } |
| 1311 | |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1312 | assert( pOp->p3>0 && pOp->p3<=p->nMem ); |
| 1313 | pOut = &p->aMem[pOp->p3]; |
drh | 00706be | 2004-01-30 14:49:16 +0000 | [diff] [blame] | 1314 | ctx.s.flags = MEM_Null; |
drh | fa4a4b9 | 2008-03-19 21:45:51 +0000 | [diff] [blame] | 1315 | ctx.s.db = db; |
danielk1977 | 5f09613 | 2008-03-28 15:44:09 +0000 | [diff] [blame] | 1316 | ctx.s.xDel = 0; |
| 1317 | ctx.s.zMalloc = 0; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1318 | |
| 1319 | /* The output cell may already have a buffer allocated. Move |
| 1320 | ** the pointer to ctx.s so in case the user-function can use |
| 1321 | ** the already allocated buffer instead of allocating a new one. |
| 1322 | */ |
| 1323 | sqlite3VdbeMemMove(&ctx.s, pOut); |
| 1324 | MemSetTypeFlag(&ctx.s, MEM_Null); |
| 1325 | |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 1326 | ctx.isError = 0; |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 1327 | if( ctx.pFunc->needCollSeq ){ |
| 1328 | assert( pOp>p->aOp ); |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 1329 | assert( pOp[-1].p4type==P4_COLLSEQ ); |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 1330 | assert( pOp[-1].opcode==OP_CollSeq ); |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 1331 | ctx.pColl = pOp[-1].p4.pColl; |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 1332 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1333 | if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse; |
danielk1977 | 51ad0ec | 2004-05-24 12:39:02 +0000 | [diff] [blame] | 1334 | (*ctx.pFunc->xFunc)(&ctx, n, apVal); |
danielk1977 | 75eb016 | 2008-03-28 19:16:33 +0000 | [diff] [blame] | 1335 | if( sqlite3SafetyOn(db) ){ |
| 1336 | sqlite3VdbeMemRelease(&ctx.s); |
| 1337 | goto abort_due_to_misuse; |
| 1338 | } |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 1339 | if( db->mallocFailed ){ |
danielk1977 | e0fc526 | 2007-07-26 06:50:05 +0000 | [diff] [blame] | 1340 | /* Even though a malloc() has failed, the implementation of the |
| 1341 | ** user function may have called an sqlite3_result_XXX() function |
| 1342 | ** to return a value. The following call releases any resources |
| 1343 | ** associated with such a value. |
| 1344 | ** |
| 1345 | ** Note: Maybe MemRelease() should be called if sqlite3SafetyOn() |
| 1346 | ** fails also (the if(...) statement above). But if people are |
| 1347 | ** misusing sqlite, they have bigger problems than a leaked value. |
| 1348 | */ |
| 1349 | sqlite3VdbeMemRelease(&ctx.s); |
| 1350 | goto no_mem; |
| 1351 | } |
danielk1977 | 7e18c25 | 2004-05-25 11:47:24 +0000 | [diff] [blame] | 1352 | |
shane | 21e7feb | 2008-05-30 15:59:49 +0000 | [diff] [blame] | 1353 | /* If any auxiliary data functions have been called by this user function, |
danielk1977 | 682f68b | 2004-06-05 10:22:17 +0000 | [diff] [blame] | 1354 | ** immediately call the destructor for any non-static values. |
| 1355 | */ |
| 1356 | if( ctx.pVdbeFunc ){ |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 1357 | sqlite3VdbeDeleteAuxData(ctx.pVdbeFunc, pOp->p1); |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 1358 | pOp->p4.pVdbeFunc = ctx.pVdbeFunc; |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 1359 | pOp->p4type = P4_VDBEFUNC; |
danielk1977 | 682f68b | 2004-06-05 10:22:17 +0000 | [diff] [blame] | 1360 | } |
| 1361 | |
drh | 90669c1 | 2006-01-20 15:45:36 +0000 | [diff] [blame] | 1362 | /* If the function returned an error, throw an exception */ |
| 1363 | if( ctx.isError ){ |
drh | f089aa4 | 2008-07-08 19:34:06 +0000 | [diff] [blame] | 1364 | sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(&ctx.s)); |
drh | 69544ec | 2008-02-06 14:11:34 +0000 | [diff] [blame] | 1365 | rc = ctx.isError; |
drh | 90669c1 | 2006-01-20 15:45:36 +0000 | [diff] [blame] | 1366 | } |
| 1367 | |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1368 | /* Copy the result of the function into register P3 */ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 1369 | sqlite3VdbeChangeEncoding(&ctx.s, encoding); |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1370 | sqlite3VdbeMemMove(pOut, &ctx.s); |
| 1371 | if( sqlite3VdbeMemTooBig(pOut) ){ |
drh | 023ae03 | 2007-05-08 12:12:16 +0000 | [diff] [blame] | 1372 | goto too_big; |
| 1373 | } |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 1374 | REGISTER_TRACE(pOp->p3, pOut); |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 1375 | UPDATE_MAX_BLOBSIZE(pOut); |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 1376 | break; |
| 1377 | } |
| 1378 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1379 | /* Opcode: BitAnd P1 P2 P3 * * |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1380 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1381 | ** Take the bit-wise AND of the values in register P1 and P2 and |
| 1382 | ** store the result in register P3. |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1383 | ** If either input is NULL, the result is NULL. |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1384 | */ |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1385 | /* Opcode: BitOr P1 P2 P3 * * |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1386 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1387 | ** Take the bit-wise OR of the values in register P1 and P2 and |
| 1388 | ** store the result in register P3. |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1389 | ** If either input is NULL, the result is NULL. |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1390 | */ |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1391 | /* Opcode: ShiftLeft P1 P2 P3 * * |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1392 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1393 | ** Shift the integer value in register P2 to the left by the |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1394 | ** number of bits specified by the integer in regiser P1. |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1395 | ** Store the result in register P3. |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1396 | ** If either input is NULL, the result is NULL. |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1397 | */ |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1398 | /* Opcode: ShiftRight P1 P2 P3 * * |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1399 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1400 | ** Shift the integer value in register P2 to the right by the |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1401 | ** number of bits specified by the integer in register P1. |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1402 | ** Store the result in register P3. |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1403 | ** If either input is NULL, the result is NULL. |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1404 | */ |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1405 | case OP_BitAnd: /* same as TK_BITAND, in1, in2, out3 */ |
| 1406 | case OP_BitOr: /* same as TK_BITOR, in1, in2, out3 */ |
| 1407 | case OP_ShiftLeft: /* same as TK_LSHIFT, in1, in2, out3 */ |
| 1408 | case OP_ShiftRight: { /* same as TK_RSHIFT, in1, in2, out3 */ |
drh | b127612 | 2005-10-29 15:48:30 +0000 | [diff] [blame] | 1409 | i64 a, b; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1410 | |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1411 | if( (pIn1->flags | pIn2->flags) & MEM_Null ){ |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 1412 | sqlite3VdbeMemSetNull(pOut); |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1413 | break; |
| 1414 | } |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1415 | a = sqlite3VdbeIntValue(pIn2); |
| 1416 | b = sqlite3VdbeIntValue(pIn1); |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1417 | switch( pOp->opcode ){ |
| 1418 | case OP_BitAnd: a &= b; break; |
| 1419 | case OP_BitOr: a |= b; break; |
| 1420 | case OP_ShiftLeft: a <<= b; break; |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 1421 | default: assert( pOp->opcode==OP_ShiftRight ); |
| 1422 | a >>= b; break; |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1423 | } |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1424 | pOut->u.i = a; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1425 | MemSetTypeFlag(pOut, MEM_Int); |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1426 | break; |
| 1427 | } |
| 1428 | |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1429 | /* Opcode: AddImm P1 P2 * * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1430 | ** |
danielk1977 | 0cdc022 | 2008-06-26 18:04:03 +0000 | [diff] [blame] | 1431 | ** Add the constant P2 to the value in register P1. |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1432 | ** The result is always an integer. |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 1433 | ** |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1434 | ** To force any register to be an integer, just add 0. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1435 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1436 | case OP_AddImm: { /* in1 */ |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1437 | sqlite3VdbeMemIntegerify(pIn1); |
| 1438 | pIn1->u.i += pOp->p2; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1439 | break; |
| 1440 | } |
| 1441 | |
drh | 41c2bf0 | 2008-01-05 05:38:21 +0000 | [diff] [blame] | 1442 | /* Opcode: ForceInt P1 P2 P3 * * |
drh | 1dd59e0 | 2003-07-06 17:22:25 +0000 | [diff] [blame] | 1443 | ** |
drh | 41c2bf0 | 2008-01-05 05:38:21 +0000 | [diff] [blame] | 1444 | ** Convert value in register P1 into an integer. If the value |
| 1445 | ** in P1 is not numeric (meaning that is is a NULL or a string that |
| 1446 | ** does not look like an integer or floating point number) then |
| 1447 | ** jump to P2. If the value in P1 is numeric then |
drh | 751f412 | 2004-01-14 21:59:22 +0000 | [diff] [blame] | 1448 | ** convert it into the least integer that is greater than or equal to its |
drh | 41c2bf0 | 2008-01-05 05:38:21 +0000 | [diff] [blame] | 1449 | ** current value if P3==0, or to the least integer that is strictly |
| 1450 | ** greater than its current value if P3==1. |
drh | 1dd59e0 | 2003-07-06 17:22:25 +0000 | [diff] [blame] | 1451 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1452 | case OP_ForceInt: { /* jump, in1 */ |
drh | f4f8fd5 | 2005-03-31 18:40:04 +0000 | [diff] [blame] | 1453 | i64 v; |
drh | 41c2bf0 | 2008-01-05 05:38:21 +0000 | [diff] [blame] | 1454 | applyAffinity(pIn1, SQLITE_AFF_NUMERIC, encoding); |
| 1455 | if( (pIn1->flags & (MEM_Int|MEM_Real))==0 ){ |
drh | 1dd59e0 | 2003-07-06 17:22:25 +0000 | [diff] [blame] | 1456 | pc = pOp->p2 - 1; |
drh | 751f412 | 2004-01-14 21:59:22 +0000 | [diff] [blame] | 1457 | break; |
drh | 1dd59e0 | 2003-07-06 17:22:25 +0000 | [diff] [blame] | 1458 | } |
drh | 41c2bf0 | 2008-01-05 05:38:21 +0000 | [diff] [blame] | 1459 | if( pIn1->flags & MEM_Int ){ |
| 1460 | v = pIn1->u.i + (pOp->p3!=0); |
drh | 751f412 | 2004-01-14 21:59:22 +0000 | [diff] [blame] | 1461 | }else{ |
drh | 41c2bf0 | 2008-01-05 05:38:21 +0000 | [diff] [blame] | 1462 | assert( pIn1->flags & MEM_Real ); |
| 1463 | v = (sqlite3_int64)pIn1->r; |
| 1464 | if( pIn1->r>(double)v ) v++; |
| 1465 | if( pOp->p3 && pIn1->r==(double)v ) v++; |
drh | 751f412 | 2004-01-14 21:59:22 +0000 | [diff] [blame] | 1466 | } |
drh | 41c2bf0 | 2008-01-05 05:38:21 +0000 | [diff] [blame] | 1467 | pIn1->u.i = v; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1468 | MemSetTypeFlag(pIn1, MEM_Int); |
drh | 1dd59e0 | 2003-07-06 17:22:25 +0000 | [diff] [blame] | 1469 | break; |
| 1470 | } |
| 1471 | |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1472 | /* Opcode: MustBeInt P1 P2 * * * |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 1473 | ** |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1474 | ** Force the value in register P1 to be an integer. If the value |
| 1475 | ** in P1 is not an integer and cannot be converted into an integer |
danielk1977 | 9a96b66 | 2007-11-29 17:05:18 +0000 | [diff] [blame] | 1476 | ** without data loss, then jump immediately to P2, or if P2==0 |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 1477 | ** raise an SQLITE_MISMATCH exception. |
| 1478 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1479 | case OP_MustBeInt: { /* jump, in1 */ |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 1480 | applyAffinity(pIn1, SQLITE_AFF_NUMERIC, encoding); |
| 1481 | if( (pIn1->flags & MEM_Int)==0 ){ |
drh | 17c4029 | 2004-07-21 02:53:29 +0000 | [diff] [blame] | 1482 | if( pOp->p2==0 ){ |
| 1483 | rc = SQLITE_MISMATCH; |
| 1484 | goto abort_due_to_error; |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 1485 | }else{ |
drh | 17c4029 | 2004-07-21 02:53:29 +0000 | [diff] [blame] | 1486 | pc = pOp->p2 - 1; |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 1487 | } |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 1488 | }else{ |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1489 | MemSetTypeFlag(pIn1, MEM_Int); |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 1490 | } |
| 1491 | break; |
| 1492 | } |
| 1493 | |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1494 | /* Opcode: RealAffinity P1 * * * * |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 1495 | ** |
drh | 2133d82 | 2008-01-03 18:44:59 +0000 | [diff] [blame] | 1496 | ** If register P1 holds an integer convert it to a real value. |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 1497 | ** |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1498 | ** This opcode is used when extracting information from a column that |
| 1499 | ** has REAL affinity. Such column values may still be stored as |
| 1500 | ** integers, for space efficiency, but after extraction we want them |
| 1501 | ** to have only a real value. |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 1502 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1503 | case OP_RealAffinity: { /* in1 */ |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1504 | if( pIn1->flags & MEM_Int ){ |
| 1505 | sqlite3VdbeMemRealify(pIn1); |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1506 | } |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 1507 | break; |
| 1508 | } |
| 1509 | |
drh | 8df447f | 2005-11-01 15:48:24 +0000 | [diff] [blame] | 1510 | #ifndef SQLITE_OMIT_CAST |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1511 | /* Opcode: ToText P1 * * * * |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 1512 | ** |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1513 | ** Force the value in register P1 to be text. |
drh | 31beae9 | 2005-11-24 14:34:36 +0000 | [diff] [blame] | 1514 | ** If the value is numeric, convert it to a string using the |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 1515 | ** equivalent of printf(). Blob values are unchanged and |
| 1516 | ** are afterwards simply interpreted as text. |
| 1517 | ** |
| 1518 | ** A NULL value is not changed by this routine. It remains NULL. |
| 1519 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1520 | case OP_ToText: { /* same as TK_TO_TEXT, in1 */ |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1521 | if( pIn1->flags & MEM_Null ) break; |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 1522 | assert( MEM_Str==(MEM_Blob>>3) ); |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1523 | pIn1->flags |= (pIn1->flags&MEM_Blob)>>3; |
| 1524 | applyAffinity(pIn1, SQLITE_AFF_TEXT, encoding); |
| 1525 | rc = ExpandBlob(pIn1); |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1526 | assert( pIn1->flags & MEM_Str || db->mallocFailed ); |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1527 | pIn1->flags &= ~(MEM_Int|MEM_Real|MEM_Blob); |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 1528 | UPDATE_MAX_BLOBSIZE(pIn1); |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 1529 | break; |
| 1530 | } |
| 1531 | |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1532 | /* Opcode: ToBlob P1 * * * * |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 1533 | ** |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1534 | ** Force the value in register P1 to be a BLOB. |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 1535 | ** If the value is numeric, convert it to a string first. |
| 1536 | ** Strings are simply reinterpreted as blobs with no change |
| 1537 | ** to the underlying data. |
| 1538 | ** |
| 1539 | ** A NULL value is not changed by this routine. It remains NULL. |
| 1540 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1541 | case OP_ToBlob: { /* same as TK_TO_BLOB, in1 */ |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1542 | if( pIn1->flags & MEM_Null ) break; |
| 1543 | if( (pIn1->flags & MEM_Blob)==0 ){ |
| 1544 | applyAffinity(pIn1, SQLITE_AFF_TEXT, encoding); |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1545 | assert( pIn1->flags & MEM_Str || db->mallocFailed ); |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 1546 | } |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1547 | MemSetTypeFlag(pIn1, MEM_Blob); |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 1548 | UPDATE_MAX_BLOBSIZE(pIn1); |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 1549 | break; |
| 1550 | } |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1551 | |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1552 | /* Opcode: ToNumeric P1 * * * * |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1553 | ** |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1554 | ** Force the value in register P1 to be numeric (either an |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1555 | ** integer or a floating-point number.) |
| 1556 | ** If the value is text or blob, try to convert it to an using the |
| 1557 | ** equivalent of atoi() or atof() and store 0 if no such conversion |
| 1558 | ** is possible. |
| 1559 | ** |
| 1560 | ** A NULL value is not changed by this routine. It remains NULL. |
| 1561 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1562 | case OP_ToNumeric: { /* same as TK_TO_NUMERIC, in1 */ |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1563 | if( (pIn1->flags & (MEM_Null|MEM_Int|MEM_Real))==0 ){ |
| 1564 | sqlite3VdbeMemNumerify(pIn1); |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1565 | } |
| 1566 | break; |
| 1567 | } |
| 1568 | #endif /* SQLITE_OMIT_CAST */ |
| 1569 | |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1570 | /* Opcode: ToInt P1 * * * * |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1571 | ** |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1572 | ** Force the value in register P1 be an integer. If |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1573 | ** The value is currently a real number, drop its fractional part. |
| 1574 | ** If the value is text or blob, try to convert it to an integer using the |
| 1575 | ** equivalent of atoi() and store 0 if no such conversion is possible. |
| 1576 | ** |
| 1577 | ** A NULL value is not changed by this routine. It remains NULL. |
| 1578 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1579 | case OP_ToInt: { /* same as TK_TO_INT, in1 */ |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1580 | if( (pIn1->flags & MEM_Null)==0 ){ |
| 1581 | sqlite3VdbeMemIntegerify(pIn1); |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1582 | } |
| 1583 | break; |
| 1584 | } |
| 1585 | |
| 1586 | #ifndef SQLITE_OMIT_CAST |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1587 | /* Opcode: ToReal P1 * * * * |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1588 | ** |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1589 | ** Force the value in register P1 to be a floating point number. |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1590 | ** If The value is currently an integer, convert it. |
| 1591 | ** If the value is text or blob, try to convert it to an integer using the |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1592 | ** equivalent of atoi() and store 0.0 if no such conversion is possible. |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1593 | ** |
| 1594 | ** A NULL value is not changed by this routine. It remains NULL. |
| 1595 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1596 | case OP_ToReal: { /* same as TK_TO_REAL, in1 */ |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1597 | if( (pIn1->flags & MEM_Null)==0 ){ |
| 1598 | sqlite3VdbeMemRealify(pIn1); |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1599 | } |
| 1600 | break; |
| 1601 | } |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 1602 | #endif /* SQLITE_OMIT_CAST */ |
| 1603 | |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 1604 | /* Opcode: Lt P1 P2 P3 P4 P5 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1605 | ** |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 1606 | ** Compare the values in register P1 and P3. If reg(P3)<reg(P1) then |
| 1607 | ** jump to address P2. |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1608 | ** |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 1609 | ** If the SQLITE_JUMPIFNULL bit of P5 is set and either reg(P1) or |
| 1610 | ** reg(P3) is NULL then take the jump. If the SQLITE_JUMPIFNULL |
| 1611 | ** bit is clear then fall thru if either operand is NULL. |
drh | 4f68623 | 2005-09-20 13:55:18 +0000 | [diff] [blame] | 1612 | ** |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 1613 | ** The SQLITE_AFF_MASK portion of P5 must be an affinity character - |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1614 | ** SQLITE_AFF_TEXT, SQLITE_AFF_INTEGER, and so forth. An attempt is made |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1615 | ** to coerce both inputs according to this affinity before the |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 1616 | ** comparison is made. If the SQLITE_AFF_MASK is 0x00, then numeric |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1617 | ** affinity is used. Note that the affinity conversions are stored |
| 1618 | ** back into the input registers P1 and P3. So this opcode can cause |
| 1619 | ** persistent changes to registers P1 and P3. |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 1620 | ** |
| 1621 | ** Once any conversions have taken place, and neither value is NULL, |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 1622 | ** the values are compared. If both values are blobs then memcmp() is |
| 1623 | ** used to determine the results of the comparison. If both values |
| 1624 | ** are text, then the appropriate collating function specified in |
| 1625 | ** P4 is used to do the comparison. If P4 is not specified then |
| 1626 | ** memcmp() is used to compare text string. If both values are |
| 1627 | ** numeric, then a numeric comparison is used. If the two values |
| 1628 | ** are of different types, then numbers are considered less than |
| 1629 | ** strings and strings are considered less than blobs. |
drh | c9b84a1 | 2002-06-20 11:36:48 +0000 | [diff] [blame] | 1630 | ** |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 1631 | ** If the SQLITE_STOREP2 bit of P5 is set, then do not jump. Instead, |
| 1632 | ** store a boolean result (either 0, or 1, or NULL) in register P2. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1633 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1634 | /* Opcode: Ne P1 P2 P3 P4 P5 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1635 | ** |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 1636 | ** This works just like the Lt opcode except that the jump is taken if |
| 1637 | ** the operands in registers P1 and P3 are not equal. See the Lt opcode for |
drh | 53db145 | 2004-05-20 13:54:53 +0000 | [diff] [blame] | 1638 | ** additional information. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1639 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1640 | /* Opcode: Eq P1 P2 P3 P4 P5 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1641 | ** |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 1642 | ** This works just like the Lt opcode except that the jump is taken if |
| 1643 | ** the operands in registers P1 and P3 are equal. |
| 1644 | ** See the Lt opcode for additional information. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1645 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1646 | /* Opcode: Le P1 P2 P3 P4 P5 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1647 | ** |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 1648 | ** This works just like the Lt opcode except that the jump is taken if |
| 1649 | ** the content of register P3 is less than or equal to the content of |
| 1650 | ** register P1. See the Lt opcode for additional information. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1651 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1652 | /* Opcode: Gt P1 P2 P3 P4 P5 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1653 | ** |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 1654 | ** This works just like the Lt opcode except that the jump is taken if |
| 1655 | ** the content of register P3 is greater than the content of |
| 1656 | ** register P1. See the Lt opcode for additional information. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1657 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1658 | /* Opcode: Ge P1 P2 P3 P4 P5 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1659 | ** |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 1660 | ** This works just like the Lt opcode except that the jump is taken if |
| 1661 | ** the content of register P3 is greater than or equal to the content of |
| 1662 | ** register P1. See the Lt opcode for additional information. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1663 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1664 | case OP_Eq: /* same as TK_EQ, jump, in1, in3 */ |
| 1665 | case OP_Ne: /* same as TK_NE, jump, in1, in3 */ |
| 1666 | case OP_Lt: /* same as TK_LT, jump, in1, in3 */ |
| 1667 | case OP_Le: /* same as TK_LE, jump, in1, in3 */ |
| 1668 | case OP_Gt: /* same as TK_GT, jump, in1, in3 */ |
| 1669 | case OP_Ge: { /* same as TK_GE, jump, in1, in3 */ |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 1670 | int flags; |
| 1671 | int res; |
| 1672 | char affinity; |
| 1673 | |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 1674 | flags = pIn1->flags|pIn3->flags; |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 1675 | |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 1676 | if( flags&MEM_Null ){ |
drh | 93a960a | 2008-07-10 00:32:42 +0000 | [diff] [blame] | 1677 | /* If either operand is NULL then the result is always NULL. |
| 1678 | ** The jump is taken if the SQLITE_JUMPIFNULL bit is set. |
| 1679 | */ |
| 1680 | if( pOp->p5 & SQLITE_STOREP2 ){ |
| 1681 | pOut = &p->aMem[pOp->p2]; |
| 1682 | MemSetTypeFlag(pOut, MEM_Null); |
| 1683 | REGISTER_TRACE(pOp->p2, pOut); |
| 1684 | }else if( pOp->p5 & SQLITE_JUMPIFNULL ){ |
| 1685 | pc = pOp->p2-1; |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 1686 | } |
drh | 93a960a | 2008-07-10 00:32:42 +0000 | [diff] [blame] | 1687 | break; |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 1688 | } |
| 1689 | |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 1690 | affinity = pOp->p5 & SQLITE_AFF_MASK; |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 1691 | if( affinity ){ |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 1692 | applyAffinity(pIn1, affinity, encoding); |
| 1693 | applyAffinity(pIn3, affinity, encoding); |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 1694 | } |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 1695 | |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 1696 | assert( pOp->p4type==P4_COLLSEQ || pOp->p4.pColl==0 ); |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 1697 | ExpandBlob(pIn1); |
| 1698 | ExpandBlob(pIn3); |
| 1699 | res = sqlite3MemCompare(pIn3, pIn1, pOp->p4.pColl); |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 1700 | switch( pOp->opcode ){ |
| 1701 | case OP_Eq: res = res==0; break; |
| 1702 | case OP_Ne: res = res!=0; break; |
| 1703 | case OP_Lt: res = res<0; break; |
| 1704 | case OP_Le: res = res<=0; break; |
| 1705 | case OP_Gt: res = res>0; break; |
| 1706 | default: res = res>=0; break; |
| 1707 | } |
| 1708 | |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 1709 | if( pOp->p5 & SQLITE_STOREP2 ){ |
| 1710 | pOut = &p->aMem[pOp->p2]; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1711 | MemSetTypeFlag(pOut, MEM_Int); |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 1712 | pOut->u.i = res; |
| 1713 | REGISTER_TRACE(pOp->p2, pOut); |
| 1714 | }else if( res ){ |
| 1715 | pc = pOp->p2-1; |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 1716 | } |
| 1717 | break; |
| 1718 | } |
drh | c9b84a1 | 2002-06-20 11:36:48 +0000 | [diff] [blame] | 1719 | |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 1720 | /* Opcode: Permutation * * * P4 * |
| 1721 | ** |
| 1722 | ** Set the permuation used by the OP_Compare operator to be the array |
| 1723 | ** of integers in P4. |
| 1724 | ** |
| 1725 | ** The permutation is only valid until the next OP_Permutation, OP_Compare, |
| 1726 | ** OP_Halt, or OP_ResultRow. Typically the OP_Permutation should occur |
| 1727 | ** immediately prior to the OP_Compare. |
| 1728 | */ |
| 1729 | case OP_Permutation: { |
| 1730 | assert( pOp->p4type==P4_INTARRAY ); |
| 1731 | assert( pOp->p4.ai ); |
| 1732 | aPermute = pOp->p4.ai; |
| 1733 | break; |
| 1734 | } |
| 1735 | |
drh | 16ee60f | 2008-06-20 18:13:25 +0000 | [diff] [blame] | 1736 | /* Opcode: Compare P1 P2 P3 P4 * |
| 1737 | ** |
| 1738 | ** Compare to vectors of registers in reg(P1)..reg(P1+P3-1) (all this |
| 1739 | ** one "A") and in reg(P2)..reg(P2+P3-1) ("B"). Save the result of |
| 1740 | ** the comparison for use by the next OP_Jump instruct. |
| 1741 | ** |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 1742 | ** P4 is a KeyInfo structure that defines collating sequences and sort |
| 1743 | ** orders for the comparison. The permutation applies to registers |
| 1744 | ** only. The KeyInfo elements are used sequentially. |
| 1745 | ** |
| 1746 | ** The comparison is a sort comparison, so NULLs compare equal, |
| 1747 | ** NULLs are less than numbers, numbers are less than strings, |
drh | 16ee60f | 2008-06-20 18:13:25 +0000 | [diff] [blame] | 1748 | ** and strings are less than blobs. |
| 1749 | */ |
| 1750 | case OP_Compare: { |
| 1751 | int n = pOp->p3; |
| 1752 | int i, p1, p2; |
| 1753 | const KeyInfo *pKeyInfo = pOp->p4.pKeyInfo; |
| 1754 | assert( n>0 ); |
drh | 93a960a | 2008-07-10 00:32:42 +0000 | [diff] [blame] | 1755 | assert( pKeyInfo!=0 ); |
drh | 16ee60f | 2008-06-20 18:13:25 +0000 | [diff] [blame] | 1756 | p1 = pOp->p1; |
| 1757 | assert( p1>0 && p1+n-1<p->nMem ); |
| 1758 | p2 = pOp->p2; |
| 1759 | assert( p2>0 && p2+n-1<p->nMem ); |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 1760 | for(i=0; i<n; i++){ |
| 1761 | int idx = aPermute ? aPermute[i] : i; |
| 1762 | CollSeq *pColl; /* Collating sequence to use on this term */ |
| 1763 | int bRev; /* True for DESCENDING sort order */ |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 1764 | REGISTER_TRACE(p1+idx, &p->aMem[p1+idx]); |
| 1765 | REGISTER_TRACE(p2+idx, &p->aMem[p2+idx]); |
drh | 93a960a | 2008-07-10 00:32:42 +0000 | [diff] [blame] | 1766 | assert( i<pKeyInfo->nField ); |
| 1767 | pColl = pKeyInfo->aColl[i]; |
| 1768 | bRev = pKeyInfo->aSortOrder[i]; |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 1769 | iCompare = sqlite3MemCompare(&p->aMem[p1+idx], &p->aMem[p2+idx], pColl); |
| 1770 | if( iCompare ){ |
| 1771 | if( bRev ) iCompare = -iCompare; |
| 1772 | break; |
| 1773 | } |
drh | 16ee60f | 2008-06-20 18:13:25 +0000 | [diff] [blame] | 1774 | } |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 1775 | aPermute = 0; |
drh | 16ee60f | 2008-06-20 18:13:25 +0000 | [diff] [blame] | 1776 | break; |
| 1777 | } |
| 1778 | |
| 1779 | /* Opcode: Jump P1 P2 P3 * * |
| 1780 | ** |
| 1781 | ** Jump to the instruction at address P1, P2, or P3 depending on whether |
| 1782 | ** in the most recent OP_Compare instruction the P1 vector was less than |
| 1783 | ** equal to, or greater than the P2 vector, respectively. |
| 1784 | */ |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 1785 | case OP_Jump: { /* jump */ |
| 1786 | if( iCompare<0 ){ |
drh | 16ee60f | 2008-06-20 18:13:25 +0000 | [diff] [blame] | 1787 | pc = pOp->p1 - 1; |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 1788 | }else if( iCompare==0 ){ |
drh | 16ee60f | 2008-06-20 18:13:25 +0000 | [diff] [blame] | 1789 | pc = pOp->p2 - 1; |
| 1790 | }else{ |
| 1791 | pc = pOp->p3 - 1; |
| 1792 | } |
| 1793 | break; |
| 1794 | } |
| 1795 | |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1796 | /* Opcode: And P1 P2 P3 * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1797 | ** |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1798 | ** Take the logical AND of the values in registers P1 and P2 and |
| 1799 | ** write the result into register P3. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1800 | ** |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1801 | ** If either P1 or P2 is 0 (false) then the result is 0 even if |
| 1802 | ** the other input is NULL. A NULL and true or two NULLs give |
| 1803 | ** a NULL output. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1804 | */ |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1805 | /* Opcode: Or P1 P2 P3 * * |
| 1806 | ** |
| 1807 | ** Take the logical OR of the values in register P1 and P2 and |
| 1808 | ** store the answer in register P3. |
| 1809 | ** |
| 1810 | ** If either P1 or P2 is nonzero (true) then the result is 1 (true) |
| 1811 | ** even if the other input is NULL. A NULL and false or two NULLs |
| 1812 | ** give a NULL output. |
| 1813 | */ |
| 1814 | case OP_And: /* same as TK_AND, in1, in2, out3 */ |
| 1815 | case OP_Or: { /* same as TK_OR, in1, in2, out3 */ |
| 1816 | int v1, v2; /* 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */ |
drh | bb11351 | 2002-05-27 01:04:51 +0000 | [diff] [blame] | 1817 | |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1818 | if( pIn1->flags & MEM_Null ){ |
drh | bb11351 | 2002-05-27 01:04:51 +0000 | [diff] [blame] | 1819 | v1 = 2; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1820 | }else{ |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1821 | v1 = sqlite3VdbeIntValue(pIn1)!=0; |
drh | bb11351 | 2002-05-27 01:04:51 +0000 | [diff] [blame] | 1822 | } |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1823 | if( pIn2->flags & MEM_Null ){ |
drh | bb11351 | 2002-05-27 01:04:51 +0000 | [diff] [blame] | 1824 | v2 = 2; |
| 1825 | }else{ |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1826 | v2 = sqlite3VdbeIntValue(pIn2)!=0; |
drh | bb11351 | 2002-05-27 01:04:51 +0000 | [diff] [blame] | 1827 | } |
| 1828 | if( pOp->opcode==OP_And ){ |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1829 | static const unsigned char and_logic[] = { 0, 0, 0, 0, 1, 2, 0, 2, 2 }; |
drh | bb11351 | 2002-05-27 01:04:51 +0000 | [diff] [blame] | 1830 | v1 = and_logic[v1*3+v2]; |
| 1831 | }else{ |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1832 | static const unsigned char or_logic[] = { 0, 1, 2, 1, 1, 1, 2, 1, 2 }; |
drh | bb11351 | 2002-05-27 01:04:51 +0000 | [diff] [blame] | 1833 | v1 = or_logic[v1*3+v2]; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1834 | } |
drh | bb11351 | 2002-05-27 01:04:51 +0000 | [diff] [blame] | 1835 | if( v1==2 ){ |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1836 | MemSetTypeFlag(pOut, MEM_Null); |
drh | bb11351 | 2002-05-27 01:04:51 +0000 | [diff] [blame] | 1837 | }else{ |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1838 | pOut->u.i = v1; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1839 | MemSetTypeFlag(pOut, MEM_Int); |
drh | bb11351 | 2002-05-27 01:04:51 +0000 | [diff] [blame] | 1840 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1841 | break; |
| 1842 | } |
| 1843 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1844 | /* Opcode: Not P1 * * * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1845 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1846 | ** Interpret the value in register P1 as a boolean value. Replace it |
| 1847 | ** with its complement. If the value in register P1 is NULL its value |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1848 | ** is unchanged. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1849 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1850 | case OP_Not: { /* same as TK_NOT, in1 */ |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 1851 | if( pIn1->flags & MEM_Null ) break; /* Do nothing to NULLs */ |
| 1852 | sqlite3VdbeMemIntegerify(pIn1); |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1853 | pIn1->u.i = !pIn1->u.i; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1854 | assert( pIn1->flags&MEM_Int ); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1855 | break; |
| 1856 | } |
| 1857 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1858 | /* Opcode: BitNot P1 * * * * |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1859 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1860 | ** Interpret the content of register P1 as an integer. Replace it |
| 1861 | ** with its ones-complement. If the value is originally NULL, leave |
| 1862 | ** it unchanged. |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1863 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1864 | case OP_BitNot: { /* same as TK_BITNOT, in1 */ |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 1865 | if( pIn1->flags & MEM_Null ) break; /* Do nothing to NULLs */ |
| 1866 | sqlite3VdbeMemIntegerify(pIn1); |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1867 | pIn1->u.i = ~pIn1->u.i; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1868 | assert( pIn1->flags&MEM_Int ); |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1869 | break; |
| 1870 | } |
| 1871 | |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 1872 | /* Opcode: If P1 P2 P3 * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1873 | ** |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 1874 | ** Jump to P2 if the value in register P1 is true. The value is |
| 1875 | ** is considered true if it is numeric and non-zero. If the value |
| 1876 | ** in P1 is NULL then take the jump if P3 is true. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1877 | */ |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 1878 | /* Opcode: IfNot P1 P2 P3 * * |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1879 | ** |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 1880 | ** Jump to P2 if the value in register P1 is False. The value is |
| 1881 | ** is considered true if it has a numeric value of zero. If the value |
| 1882 | ** in P1 is NULL then take the jump if P3 is true. |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1883 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1884 | case OP_If: /* jump, in1 */ |
| 1885 | case OP_IfNot: { /* jump, in1 */ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1886 | int c; |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 1887 | if( pIn1->flags & MEM_Null ){ |
| 1888 | c = pOp->p3; |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1889 | }else{ |
drh | ba0232a | 2005-06-06 17:27:19 +0000 | [diff] [blame] | 1890 | #ifdef SQLITE_OMIT_FLOATING_POINT |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 1891 | c = sqlite3VdbeIntValue(pIn1); |
drh | ba0232a | 2005-06-06 17:27:19 +0000 | [diff] [blame] | 1892 | #else |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 1893 | c = sqlite3VdbeRealValue(pIn1)!=0.0; |
drh | ba0232a | 2005-06-06 17:27:19 +0000 | [diff] [blame] | 1894 | #endif |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1895 | if( pOp->opcode==OP_IfNot ) c = !c; |
| 1896 | } |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 1897 | if( c ){ |
| 1898 | pc = pOp->p2-1; |
| 1899 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1900 | break; |
| 1901 | } |
| 1902 | |
drh | 2d401ab | 2008-01-10 23:50:11 +0000 | [diff] [blame] | 1903 | /* Opcode: IsNull P1 P2 P3 * * |
drh | 477df4b | 2008-01-05 18:48:24 +0000 | [diff] [blame] | 1904 | ** |
drh | 2d401ab | 2008-01-10 23:50:11 +0000 | [diff] [blame] | 1905 | ** Jump to P2 if the value in register P1 is NULL. If P3 is greater |
| 1906 | ** than zero, then check all values reg(P1), reg(P1+1), |
| 1907 | ** reg(P1+2), ..., reg(P1+P3-1). |
drh | 477df4b | 2008-01-05 18:48:24 +0000 | [diff] [blame] | 1908 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1909 | case OP_IsNull: { /* same as TK_ISNULL, jump, in1 */ |
drh | 2d401ab | 2008-01-10 23:50:11 +0000 | [diff] [blame] | 1910 | int n = pOp->p3; |
| 1911 | assert( pOp->p3==0 || pOp->p1>0 ); |
| 1912 | do{ |
| 1913 | if( (pIn1->flags & MEM_Null)!=0 ){ |
| 1914 | pc = pOp->p2 - 1; |
| 1915 | break; |
| 1916 | } |
| 1917 | pIn1++; |
| 1918 | }while( --n > 0 ); |
drh | 477df4b | 2008-01-05 18:48:24 +0000 | [diff] [blame] | 1919 | break; |
| 1920 | } |
| 1921 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1922 | /* Opcode: NotNull P1 P2 * * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1923 | ** |
drh | 6a288a3 | 2008-01-07 19:20:24 +0000 | [diff] [blame] | 1924 | ** Jump to P2 if the value in register P1 is not NULL. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1925 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1926 | case OP_NotNull: { /* same as TK_NOTNULL, jump, in1 */ |
drh | 6a288a3 | 2008-01-07 19:20:24 +0000 | [diff] [blame] | 1927 | if( (pIn1->flags & MEM_Null)==0 ){ |
| 1928 | pc = pOp->p2 - 1; |
| 1929 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1930 | break; |
| 1931 | } |
| 1932 | |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 1933 | /* Opcode: SetNumColumns * P2 * * * |
danielk1977 | b4964b7 | 2004-05-18 01:23:38 +0000 | [diff] [blame] | 1934 | ** |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 1935 | ** This opcode sets the number of columns for the cursor opened by the |
| 1936 | ** following instruction to P2. |
danielk1977 | b4964b7 | 2004-05-18 01:23:38 +0000 | [diff] [blame] | 1937 | ** |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 1938 | ** An OP_SetNumColumns is only useful if it occurs immediately before |
| 1939 | ** one of the following opcodes: |
danielk1977 | ac17178 | 2005-02-05 06:49:54 +0000 | [diff] [blame] | 1940 | ** |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 1941 | ** OpenRead |
| 1942 | ** OpenWrite |
| 1943 | ** OpenPseudo |
| 1944 | ** |
| 1945 | ** If the OP_Column opcode is to be executed on a cursor, then |
| 1946 | ** this opcode must be present immediately before the opcode that |
| 1947 | ** opens the cursor. |
danielk1977 | b4964b7 | 2004-05-18 01:23:38 +0000 | [diff] [blame] | 1948 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1949 | case OP_SetNumColumns: { |
danielk1977 | b4964b7 | 2004-05-18 01:23:38 +0000 | [diff] [blame] | 1950 | break; |
| 1951 | } |
| 1952 | |
danielk1977 | 60585dd | 2008-01-03 08:08:40 +0000 | [diff] [blame] | 1953 | /* Opcode: Column P1 P2 P3 P4 * |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 1954 | ** |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1955 | ** Interpret the data that cursor P1 points to as a structure built using |
| 1956 | ** the MakeRecord instruction. (See the MakeRecord opcode for additional |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 1957 | ** information about the format of the data.) Extract the P2-th column |
| 1958 | ** from this record. If there are less that (P2+1) |
| 1959 | ** values in the record, extract a NULL. |
| 1960 | ** |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1961 | ** The value extracted is stored in register P3. |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 1962 | ** |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1963 | ** If the KeyAsData opcode has previously executed on this cursor, then the |
| 1964 | ** field might be extracted from the key rather than the data. |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 1965 | ** |
danielk1977 | 1f4aa33 | 2008-01-03 09:51:55 +0000 | [diff] [blame] | 1966 | ** If the column contains fewer than P2 fields, then extract a NULL. Or, |
| 1967 | ** if the P4 argument is a P4_MEM use the value of the P4 argument as |
| 1968 | ** the result. |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 1969 | */ |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1970 | case OP_Column: { |
danielk1977 | e0d4b06 | 2004-06-28 01:11:46 +0000 | [diff] [blame] | 1971 | u32 payloadSize; /* Number of bytes in the record */ |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 1972 | int p1 = pOp->p1; /* P1 value of the opcode */ |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1973 | int p2 = pOp->p2; /* column number to retrieve */ |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 1974 | Cursor *pC = 0; /* The VDBE cursor */ |
drh | e61cffc | 2004-06-12 18:12:15 +0000 | [diff] [blame] | 1975 | char *zRec; /* Pointer to complete record-data */ |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 1976 | BtCursor *pCrsr; /* The BTree cursor */ |
| 1977 | u32 *aType; /* aType[i] holds the numeric type of the i-th column */ |
| 1978 | 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] | 1979 | u32 nField; /* number of fields in the record */ |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1980 | int len; /* The length of the serialized data for the column */ |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 1981 | int i; /* Loop counter */ |
| 1982 | char *zData; /* Part of the record being decoded */ |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 1983 | Mem *pDest; /* Where to write the extracted value */ |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 1984 | Mem sMem; /* For storing the record being decoded */ |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 1985 | |
drh | b6f5452 | 2004-05-20 02:42:16 +0000 | [diff] [blame] | 1986 | sMem.flags = 0; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1987 | sMem.db = 0; |
danielk1977 | 5f09613 | 2008-03-28 15:44:09 +0000 | [diff] [blame] | 1988 | sMem.zMalloc = 0; |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 1989 | assert( p1<p->nCursor ); |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1990 | assert( pOp->p3>0 && pOp->p3<=p->nMem ); |
| 1991 | pDest = &p->aMem[pOp->p3]; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1992 | MemSetTypeFlag(pDest, MEM_Null); |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1993 | |
drh | e61cffc | 2004-06-12 18:12:15 +0000 | [diff] [blame] | 1994 | /* This block sets the variable payloadSize to be the total number of |
| 1995 | ** bytes in the record. |
| 1996 | ** |
| 1997 | ** 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] | 1998 | ** The complete record text is always available for pseudo-tables |
| 1999 | ** If the record is stored in a cursor, the complete record text |
| 2000 | ** might be available in the pC->aRow cache. Or it might not be. |
| 2001 | ** If the data is unavailable, zRec is set to NULL. |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 2002 | ** |
| 2003 | ** We also compute the number of columns in the record. For cursors, |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2004 | ** the number of columns is stored in the Cursor.nField element. |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 2005 | */ |
drh | b73857f | 2006-03-17 00:25:59 +0000 | [diff] [blame] | 2006 | pC = p->apCsr[p1]; |
danielk1977 | 6c92409 | 2007-11-12 08:09:34 +0000 | [diff] [blame] | 2007 | assert( pC!=0 ); |
danielk1977 | 0817d0d | 2007-02-14 09:19:36 +0000 | [diff] [blame] | 2008 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 2009 | assert( pC->pVtabCursor==0 ); |
| 2010 | #endif |
drh | b73857f | 2006-03-17 00:25:59 +0000 | [diff] [blame] | 2011 | if( pC->pCursor!=0 ){ |
drh | e61cffc | 2004-06-12 18:12:15 +0000 | [diff] [blame] | 2012 | /* The record is stored in a B-Tree */ |
drh | 536065a | 2005-01-26 21:55:31 +0000 | [diff] [blame] | 2013 | rc = sqlite3VdbeCursorMoveto(pC); |
drh | 52f159e | 2005-01-27 00:33:21 +0000 | [diff] [blame] | 2014 | if( rc ) goto abort_due_to_error; |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2015 | zRec = 0; |
| 2016 | pCrsr = pC->pCursor; |
| 2017 | if( pC->nullRow ){ |
| 2018 | payloadSize = 0; |
drh | 76873ab | 2006-01-07 18:48:26 +0000 | [diff] [blame] | 2019 | }else if( pC->cacheStatus==p->cacheCtr ){ |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2020 | payloadSize = pC->payloadSize; |
drh | 2646da7 | 2005-12-09 20:02:05 +0000 | [diff] [blame] | 2021 | zRec = (char*)pC->aRow; |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 2022 | }else if( pC->isIndex ){ |
danielk1977 | 96fc5fe | 2004-05-13 11:34:16 +0000 | [diff] [blame] | 2023 | i64 payloadSize64; |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2024 | sqlite3BtreeKeySize(pCrsr, &payloadSize64); |
| 2025 | payloadSize = payloadSize64; |
| 2026 | }else{ |
| 2027 | sqlite3BtreeDataSize(pCrsr, &payloadSize); |
| 2028 | } |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 2029 | nField = pC->nField; |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 2030 | }else{ |
| 2031 | assert( pC->pseudoTable ); |
drh | e61cffc | 2004-06-12 18:12:15 +0000 | [diff] [blame] | 2032 | /* The record is the sole entry of a pseudo-table */ |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2033 | payloadSize = pC->nData; |
| 2034 | zRec = pC->pData; |
drh | 76873ab | 2006-01-07 18:48:26 +0000 | [diff] [blame] | 2035 | pC->cacheStatus = CACHE_STALE; |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2036 | assert( payloadSize==0 || zRec!=0 ); |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 2037 | nField = pC->nField; |
danielk1977 | f7df9cc | 2004-06-16 12:02:47 +0000 | [diff] [blame] | 2038 | pCrsr = 0; |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2039 | } |
| 2040 | |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2041 | /* If payloadSize is 0, then just store a NULL */ |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2042 | if( payloadSize==0 ){ |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 2043 | assert( pDest->flags&MEM_Null ); |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 2044 | goto op_column_out; |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2045 | } |
drh | bb4957f | 2008-03-20 14:03:29 +0000 | [diff] [blame] | 2046 | if( payloadSize>db->aLimit[SQLITE_LIMIT_LENGTH] ){ |
drh | 023ae03 | 2007-05-08 12:12:16 +0000 | [diff] [blame] | 2047 | goto too_big; |
| 2048 | } |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2049 | |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 2050 | assert( p2<nField ); |
danielk1977 | b4964b7 | 2004-05-18 01:23:38 +0000 | [diff] [blame] | 2051 | |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2052 | /* Read and parse the table header. Store the results of the parse |
| 2053 | ** into the record header cache fields of the cursor. |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2054 | */ |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 2055 | aType = pC->aType; |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 2056 | if( pC->cacheStatus==p->cacheCtr ){ |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 2057 | aOffset = pC->aOffset; |
| 2058 | }else{ |
danielk1977 | dedf45b | 2006-01-13 17:12:01 +0000 | [diff] [blame] | 2059 | u8 *zIdx; /* Index into header */ |
| 2060 | u8 *zEndHdr; /* Pointer to first byte after the header */ |
| 2061 | u32 offset; /* Offset into the data */ |
drh | 0ac0719 | 2006-02-10 14:02:07 +0000 | [diff] [blame] | 2062 | int szHdrSz; /* Size of the header size field at start of record */ |
danielk1977 | dedf45b | 2006-01-13 17:12:01 +0000 | [diff] [blame] | 2063 | int avail; /* Number of bytes of available data */ |
drh | b73857f | 2006-03-17 00:25:59 +0000 | [diff] [blame] | 2064 | |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 2065 | assert(aType); |
drh | b73857f | 2006-03-17 00:25:59 +0000 | [diff] [blame] | 2066 | pC->aOffset = aOffset = &aType[nField]; |
| 2067 | pC->payloadSize = payloadSize; |
| 2068 | pC->cacheStatus = p->cacheCtr; |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2069 | |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 2070 | /* Figure out how many bytes are in the header */ |
danielk1977 | 84ac9d0 | 2004-05-18 09:58:06 +0000 | [diff] [blame] | 2071 | if( zRec ){ |
| 2072 | zData = zRec; |
| 2073 | }else{ |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 2074 | if( pC->isIndex ){ |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 2075 | zData = (char*)sqlite3BtreeKeyFetch(pCrsr, &avail); |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 2076 | }else{ |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 2077 | zData = (char*)sqlite3BtreeDataFetch(pCrsr, &avail); |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2078 | } |
drh | e61cffc | 2004-06-12 18:12:15 +0000 | [diff] [blame] | 2079 | /* If KeyFetch()/DataFetch() managed to get the entire payload, |
| 2080 | ** save the payload in the pC->aRow cache. That will save us from |
| 2081 | ** having to make additional calls to fetch the content portion of |
| 2082 | ** the record. |
| 2083 | */ |
| 2084 | if( avail>=payloadSize ){ |
drh | 2646da7 | 2005-12-09 20:02:05 +0000 | [diff] [blame] | 2085 | zRec = zData; |
| 2086 | pC->aRow = (u8*)zData; |
drh | e61cffc | 2004-06-12 18:12:15 +0000 | [diff] [blame] | 2087 | }else{ |
| 2088 | pC->aRow = 0; |
| 2089 | } |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 2090 | } |
drh | 588f5bc | 2007-01-02 18:41:54 +0000 | [diff] [blame] | 2091 | /* The following assert is true in all cases accept when |
| 2092 | ** the database file has been corrupted externally. |
| 2093 | ** assert( zRec!=0 || avail>=payloadSize || avail>=9 ); */ |
shane | 3f8d5cf | 2008-04-24 19:15:09 +0000 | [diff] [blame] | 2094 | szHdrSz = getVarint32((u8*)zData, offset); |
drh | e61cffc | 2004-06-12 18:12:15 +0000 | [diff] [blame] | 2095 | |
| 2096 | /* The KeyFetch() or DataFetch() above are fast and will get the entire |
| 2097 | ** record header in most cases. But they will fail to get the complete |
| 2098 | ** record header if the record header does not fit on a single page |
| 2099 | ** in the B-Tree. When that happens, use sqlite3VdbeMemFromBtree() to |
| 2100 | ** acquire the complete header text. |
| 2101 | */ |
danielk1977 | dedf45b | 2006-01-13 17:12:01 +0000 | [diff] [blame] | 2102 | if( !zRec && avail<offset ){ |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 2103 | sMem.flags = 0; |
| 2104 | sMem.db = 0; |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 2105 | rc = sqlite3VdbeMemFromBtree(pCrsr, 0, offset, pC->isIndex, &sMem); |
danielk1977 | 84ac9d0 | 2004-05-18 09:58:06 +0000 | [diff] [blame] | 2106 | if( rc!=SQLITE_OK ){ |
danielk1977 | 3c9cc8d | 2005-01-17 03:40:08 +0000 | [diff] [blame] | 2107 | goto op_column_out; |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2108 | } |
drh | b6f5452 | 2004-05-20 02:42:16 +0000 | [diff] [blame] | 2109 | zData = sMem.z; |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2110 | } |
drh | 0ac0719 | 2006-02-10 14:02:07 +0000 | [diff] [blame] | 2111 | zEndHdr = (u8 *)&zData[offset]; |
| 2112 | zIdx = (u8 *)&zData[szHdrSz]; |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2113 | |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 2114 | /* Scan the header and use it to fill in the aType[] and aOffset[] |
| 2115 | ** arrays. aType[i] will contain the type integer for the i-th |
| 2116 | ** column and aOffset[i] will contain the offset from the beginning |
| 2117 | ** 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] | 2118 | */ |
danielk1977 | dedf45b | 2006-01-13 17:12:01 +0000 | [diff] [blame] | 2119 | for(i=0; i<nField; i++){ |
| 2120 | if( zIdx<zEndHdr ){ |
| 2121 | aOffset[i] = offset; |
shane | 3f8d5cf | 2008-04-24 19:15:09 +0000 | [diff] [blame] | 2122 | zIdx += getVarint32(zIdx, aType[i]); |
danielk1977 | dedf45b | 2006-01-13 17:12:01 +0000 | [diff] [blame] | 2123 | offset += sqlite3VdbeSerialTypeLen(aType[i]); |
| 2124 | }else{ |
| 2125 | /* If i is less that nField, then there are less fields in this |
| 2126 | ** record than SetNumColumns indicated there are columns in the |
| 2127 | ** table. Set the offset for any extra columns not present in |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2128 | ** the record to 0. This tells code below to store a NULL |
| 2129 | ** instead of deserializing a value from the record. |
danielk1977 | dedf45b | 2006-01-13 17:12:01 +0000 | [diff] [blame] | 2130 | */ |
| 2131 | aOffset[i] = 0; |
| 2132 | } |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2133 | } |
danielk1977 | 5f09613 | 2008-03-28 15:44:09 +0000 | [diff] [blame] | 2134 | sqlite3VdbeMemRelease(&sMem); |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 2135 | sMem.flags = MEM_Null; |
| 2136 | |
danielk1977 | 9792eef | 2006-01-13 15:58:43 +0000 | [diff] [blame] | 2137 | /* If we have read more header data than was contained in the header, |
| 2138 | ** or if the end of the last field appears to be past the end of the |
shane | 2ca8bc0 | 2008-05-07 18:59:28 +0000 | [diff] [blame] | 2139 | ** record, or if the end of the last field appears to be before the end |
| 2140 | ** of the record (when all fields present), then we must be dealing |
| 2141 | ** with a corrupt database. |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 2142 | */ |
shane | 2ca8bc0 | 2008-05-07 18:59:28 +0000 | [diff] [blame] | 2143 | if( zIdx>zEndHdr || offset>payloadSize || (zIdx==zEndHdr && offset!=payloadSize) ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 2144 | rc = SQLITE_CORRUPT_BKPT; |
danielk1977 | 3c9cc8d | 2005-01-17 03:40:08 +0000 | [diff] [blame] | 2145 | goto op_column_out; |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 2146 | } |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 2147 | } |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2148 | |
danielk1977 | 36963fd | 2005-02-19 08:18:05 +0000 | [diff] [blame] | 2149 | /* Get the column information. If aOffset[p2] is non-zero, then |
| 2150 | ** deserialize the value from the record. If aOffset[p2] is zero, |
| 2151 | ** then there are not enough fields in the record to satisfy the |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 2152 | ** request. In this case, set the value NULL or to P4 if P4 is |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 2153 | ** a pointer to a Mem object. |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2154 | */ |
danielk1977 | 36963fd | 2005-02-19 08:18:05 +0000 | [diff] [blame] | 2155 | if( aOffset[p2] ){ |
| 2156 | assert( rc==SQLITE_OK ); |
| 2157 | if( zRec ){ |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 2158 | if( pDest->flags&MEM_Dyn ){ |
| 2159 | sqlite3VdbeSerialGet((u8 *)&zRec[aOffset[p2]], aType[p2], &sMem); |
| 2160 | sMem.db = db; |
danielk1977 | 5f09613 | 2008-03-28 15:44:09 +0000 | [diff] [blame] | 2161 | rc = sqlite3VdbeMemCopy(pDest, &sMem); |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 2162 | assert( !(sMem.flags&MEM_Dyn) ); |
danielk1977 | 5f09613 | 2008-03-28 15:44:09 +0000 | [diff] [blame] | 2163 | if( rc!=SQLITE_OK ){ |
| 2164 | goto op_column_out; |
| 2165 | } |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 2166 | }else{ |
| 2167 | sqlite3VdbeSerialGet((u8 *)&zRec[aOffset[p2]], aType[p2], pDest); |
| 2168 | } |
danielk1977 | 36963fd | 2005-02-19 08:18:05 +0000 | [diff] [blame] | 2169 | }else{ |
| 2170 | len = sqlite3VdbeSerialTypeLen(aType[p2]); |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 2171 | sqlite3VdbeMemMove(&sMem, pDest); |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 2172 | rc = sqlite3VdbeMemFromBtree(pCrsr, aOffset[p2], len, pC->isIndex, &sMem); |
danielk1977 | 36963fd | 2005-02-19 08:18:05 +0000 | [diff] [blame] | 2173 | if( rc!=SQLITE_OK ){ |
| 2174 | goto op_column_out; |
| 2175 | } |
| 2176 | zData = sMem.z; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 2177 | sqlite3VdbeSerialGet((u8*)zData, aType[p2], pDest); |
danielk1977 | 7701e81 | 2005-01-10 12:59:51 +0000 | [diff] [blame] | 2178 | } |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 2179 | pDest->enc = encoding; |
danielk1977 | 36963fd | 2005-02-19 08:18:05 +0000 | [diff] [blame] | 2180 | }else{ |
danielk1977 | 60585dd | 2008-01-03 08:08:40 +0000 | [diff] [blame] | 2181 | if( pOp->p4type==P4_MEM ){ |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 2182 | sqlite3VdbeMemShallowCopy(pDest, pOp->p4.pMem, MEM_Static); |
danielk1977 | aee18ef | 2005-03-09 12:26:50 +0000 | [diff] [blame] | 2183 | }else{ |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 2184 | assert( pDest->flags&MEM_Null ); |
danielk1977 | aee18ef | 2005-03-09 12:26:50 +0000 | [diff] [blame] | 2185 | } |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 2186 | } |
drh | febe106 | 2004-08-28 18:17:48 +0000 | [diff] [blame] | 2187 | |
| 2188 | /* If we dynamically allocated space to hold the data (in the |
| 2189 | ** sqlite3VdbeMemFromBtree() call above) then transfer control of that |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 2190 | ** dynamically allocated space over to the pDest structure. |
drh | febe106 | 2004-08-28 18:17:48 +0000 | [diff] [blame] | 2191 | ** This prevents a memory copy. |
| 2192 | */ |
danielk1977 | 5f09613 | 2008-03-28 15:44:09 +0000 | [diff] [blame] | 2193 | if( sMem.zMalloc ){ |
| 2194 | assert( sMem.z==sMem.zMalloc ); |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 2195 | assert( !(pDest->flags & MEM_Dyn) ); |
| 2196 | assert( !(pDest->flags & (MEM_Blob|MEM_Str)) || pDest->z==sMem.z ); |
| 2197 | pDest->flags &= ~(MEM_Ephem|MEM_Static); |
danielk1977 | 5f09613 | 2008-03-28 15:44:09 +0000 | [diff] [blame] | 2198 | pDest->flags |= MEM_Term; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 2199 | pDest->z = sMem.z; |
danielk1977 | 5f09613 | 2008-03-28 15:44:09 +0000 | [diff] [blame] | 2200 | pDest->zMalloc = sMem.zMalloc; |
danielk1977 | b1bc953 | 2004-05-22 03:05:33 +0000 | [diff] [blame] | 2201 | } |
drh | febe106 | 2004-08-28 18:17:48 +0000 | [diff] [blame] | 2202 | |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 2203 | rc = sqlite3VdbeMemMakeWriteable(pDest); |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 2204 | |
danielk1977 | 3c9cc8d | 2005-01-17 03:40:08 +0000 | [diff] [blame] | 2205 | op_column_out: |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 2206 | UPDATE_MAX_BLOBSIZE(pDest); |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 2207 | REGISTER_TRACE(pOp->p3, pDest); |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2208 | break; |
| 2209 | } |
| 2210 | |
danielk1977 | 751de56 | 2008-04-18 09:01:15 +0000 | [diff] [blame] | 2211 | /* Opcode: Affinity P1 P2 * P4 * |
| 2212 | ** |
| 2213 | ** Apply affinities to a range of P2 registers starting with P1. |
| 2214 | ** |
| 2215 | ** P4 is a string that is P2 characters long. The nth character of the |
| 2216 | ** string indicates the column affinity that should be used for the nth |
| 2217 | ** memory cell in the range. |
| 2218 | */ |
| 2219 | case OP_Affinity: { |
| 2220 | char *zAffinity = pOp->p4.z; |
| 2221 | Mem *pData0 = &p->aMem[pOp->p1]; |
| 2222 | Mem *pLast = &pData0[pOp->p2-1]; |
| 2223 | Mem *pRec; |
| 2224 | |
| 2225 | for(pRec=pData0; pRec<=pLast; pRec++){ |
danielk1977 | b790c6c | 2008-04-18 10:25:24 +0000 | [diff] [blame] | 2226 | ExpandBlob(pRec); |
danielk1977 | 751de56 | 2008-04-18 09:01:15 +0000 | [diff] [blame] | 2227 | applyAffinity(pRec, zAffinity[pRec-pData0], encoding); |
| 2228 | } |
| 2229 | break; |
| 2230 | } |
| 2231 | |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2232 | /* Opcode: MakeRecord P1 P2 P3 P4 * |
drh | 7a224de | 2004-06-02 01:22:02 +0000 | [diff] [blame] | 2233 | ** |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2234 | ** Convert P2 registers beginning with P1 into a single entry |
drh | 7a224de | 2004-06-02 01:22:02 +0000 | [diff] [blame] | 2235 | ** suitable for use as a data record in a database table or as a key |
shane | 21e7feb | 2008-05-30 15:59:49 +0000 | [diff] [blame] | 2236 | ** in an index. The details of the format are irrelevant as long as |
drh | 1e968a0 | 2008-03-25 00:22:21 +0000 | [diff] [blame] | 2237 | ** the OP_Column opcode can decode the record later. |
| 2238 | ** Refer to source code comments for the details of the record |
drh | 7a224de | 2004-06-02 01:22:02 +0000 | [diff] [blame] | 2239 | ** format. |
| 2240 | ** |
danielk1977 | 751de56 | 2008-04-18 09:01:15 +0000 | [diff] [blame] | 2241 | ** P4 may be a string that is P2 characters long. The nth character of the |
drh | 7a224de | 2004-06-02 01:22:02 +0000 | [diff] [blame] | 2242 | ** string indicates the column affinity that should be used for the nth |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2243 | ** field of the index key. |
drh | 7a224de | 2004-06-02 01:22:02 +0000 | [diff] [blame] | 2244 | ** |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 2245 | ** The mapping from character to affinity is given by the SQLITE_AFF_ |
| 2246 | ** macros defined in sqliteInt.h. |
drh | 7a224de | 2004-06-02 01:22:02 +0000 | [diff] [blame] | 2247 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 2248 | ** If P4 is NULL then all index fields have the affinity NONE. |
drh | 7f057c9 | 2005-06-24 03:53:06 +0000 | [diff] [blame] | 2249 | */ |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2250 | case OP_MakeRecord: { |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 2251 | /* Assuming the record contains N fields, the record format looks |
| 2252 | ** like this: |
| 2253 | ** |
drh | 7a224de | 2004-06-02 01:22:02 +0000 | [diff] [blame] | 2254 | ** ------------------------------------------------------------------------ |
| 2255 | ** | hdr-size | type 0 | type 1 | ... | type N-1 | data0 | ... | data N-1 | |
| 2256 | ** ------------------------------------------------------------------------ |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 2257 | ** |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2258 | ** Data(0) is taken from register P1. Data(1) comes from register P1+1 |
| 2259 | ** and so froth. |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 2260 | ** |
| 2261 | ** Each type field is a varint representing the serial type of the |
| 2262 | ** corresponding data element (see sqlite3VdbeSerialType()). The |
drh | 7a224de | 2004-06-02 01:22:02 +0000 | [diff] [blame] | 2263 | ** hdr-size field is also a varint which is the offset from the beginning |
| 2264 | ** of the record to data0. |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 2265 | */ |
drh | fdf972a | 2007-05-02 13:30:27 +0000 | [diff] [blame] | 2266 | u8 *zNewRecord; /* A buffer to hold the data for the new record */ |
| 2267 | Mem *pRec; /* The new record */ |
drh | 023ae03 | 2007-05-08 12:12:16 +0000 | [diff] [blame] | 2268 | u64 nData = 0; /* Number of bytes of data space */ |
danielk1977 | ededfd5 | 2004-06-17 07:53:01 +0000 | [diff] [blame] | 2269 | int nHdr = 0; /* Number of bytes of header space */ |
drh | 023ae03 | 2007-05-08 12:12:16 +0000 | [diff] [blame] | 2270 | u64 nByte = 0; /* Data space required for this record */ |
drh | fdf972a | 2007-05-02 13:30:27 +0000 | [diff] [blame] | 2271 | int nZero = 0; /* Number of zero bytes at the end of the record */ |
drh | cb9882a | 2005-03-17 03:15:40 +0000 | [diff] [blame] | 2272 | int nVarint; /* Number of bytes in a varint */ |
danielk1977 | ededfd5 | 2004-06-17 07:53:01 +0000 | [diff] [blame] | 2273 | u32 serial_type; /* Type field */ |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2274 | Mem *pData0; /* First field to be combined into the record */ |
| 2275 | Mem *pLast; /* Last field of the record */ |
danielk1977 | ededfd5 | 2004-06-17 07:53:01 +0000 | [diff] [blame] | 2276 | int nField; /* Number of fields in the record */ |
danielk1977 | ededfd5 | 2004-06-17 07:53:01 +0000 | [diff] [blame] | 2277 | char *zAffinity; /* The affinity string for the record */ |
drh | d946db0 | 2005-12-29 19:23:06 +0000 | [diff] [blame] | 2278 | int file_format; /* File format to use for encoding */ |
drh | fdf972a | 2007-05-02 13:30:27 +0000 | [diff] [blame] | 2279 | int i; /* Space used in zNewRecord[] */ |
danielk1977 | ededfd5 | 2004-06-17 07:53:01 +0000 | [diff] [blame] | 2280 | |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2281 | nField = pOp->p1; |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 2282 | zAffinity = pOp->p4.z; |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2283 | assert( nField>0 && pOp->p2>0 && pOp->p2+nField<=p->nMem ); |
| 2284 | pData0 = &p->aMem[nField]; |
| 2285 | nField = pOp->p2; |
| 2286 | pLast = &pData0[nField-1]; |
drh | d946db0 | 2005-12-29 19:23:06 +0000 | [diff] [blame] | 2287 | file_format = p->minWriteFileFormat; |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 2288 | |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 2289 | /* Loop through the elements that will make up the record to figure |
| 2290 | ** out how much space is required for the new record. |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 2291 | */ |
drh | a2a49dc | 2008-01-02 14:28:13 +0000 | [diff] [blame] | 2292 | for(pRec=pData0; pRec<=pLast; pRec++){ |
drh | ae7e151 | 2007-05-02 16:51:59 +0000 | [diff] [blame] | 2293 | int len; |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 2294 | if( zAffinity ){ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 2295 | applyAffinity(pRec, zAffinity[pRec-pData0], encoding); |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 2296 | } |
danielk1977 | d908f5a | 2007-05-11 07:08:28 +0000 | [diff] [blame] | 2297 | if( pRec->flags&MEM_Zero && pRec->n>0 ){ |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 2298 | sqlite3VdbeMemExpandBlob(pRec); |
danielk1977 | d908f5a | 2007-05-11 07:08:28 +0000 | [diff] [blame] | 2299 | } |
drh | d946db0 | 2005-12-29 19:23:06 +0000 | [diff] [blame] | 2300 | serial_type = sqlite3VdbeSerialType(pRec, file_format); |
drh | ae7e151 | 2007-05-02 16:51:59 +0000 | [diff] [blame] | 2301 | len = sqlite3VdbeSerialTypeLen(serial_type); |
| 2302 | nData += len; |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 2303 | nHdr += sqlite3VarintLen(serial_type); |
drh | fdf972a | 2007-05-02 13:30:27 +0000 | [diff] [blame] | 2304 | if( pRec->flags & MEM_Zero ){ |
| 2305 | /* Only pure zero-filled BLOBs can be input to this Opcode. |
| 2306 | ** We do not allow blobs with a prefix and a zero-filled tail. */ |
drh | fdf972a | 2007-05-02 13:30:27 +0000 | [diff] [blame] | 2307 | nZero += pRec->u.i; |
drh | ae7e151 | 2007-05-02 16:51:59 +0000 | [diff] [blame] | 2308 | }else if( len ){ |
drh | fdf972a | 2007-05-02 13:30:27 +0000 | [diff] [blame] | 2309 | nZero = 0; |
| 2310 | } |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 2311 | } |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 2312 | |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 2313 | /* Add the initial header varint and total the size */ |
drh | cb9882a | 2005-03-17 03:15:40 +0000 | [diff] [blame] | 2314 | nHdr += nVarint = sqlite3VarintLen(nHdr); |
| 2315 | if( nVarint<sqlite3VarintLen(nHdr) ){ |
| 2316 | nHdr++; |
| 2317 | } |
drh | fdf972a | 2007-05-02 13:30:27 +0000 | [diff] [blame] | 2318 | nByte = nHdr+nData-nZero; |
drh | bb4957f | 2008-03-20 14:03:29 +0000 | [diff] [blame] | 2319 | if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){ |
drh | 023ae03 | 2007-05-08 12:12:16 +0000 | [diff] [blame] | 2320 | goto too_big; |
| 2321 | } |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 2322 | |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 2323 | /* Make sure the output register has a buffer large enough to store |
| 2324 | ** the new record. The output register (pOp->p3) is not allowed to |
| 2325 | ** be one of the input registers (because the following call to |
| 2326 | ** sqlite3VdbeMemGrow() could clobber the value before it is used). |
| 2327 | */ |
| 2328 | assert( pOp->p3<pOp->p1 || pOp->p3>=pOp->p1+pOp->p2 ); |
| 2329 | pOut = &p->aMem[pOp->p3]; |
| 2330 | if( sqlite3VdbeMemGrow(pOut, nByte, 0) ){ |
| 2331 | goto no_mem; |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 2332 | } |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 2333 | zNewRecord = (u8 *)pOut->z; |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 2334 | |
| 2335 | /* Write the record */ |
shane | 3f8d5cf | 2008-04-24 19:15:09 +0000 | [diff] [blame] | 2336 | i = putVarint32(zNewRecord, nHdr); |
drh | a2a49dc | 2008-01-02 14:28:13 +0000 | [diff] [blame] | 2337 | for(pRec=pData0; pRec<=pLast; pRec++){ |
drh | d946db0 | 2005-12-29 19:23:06 +0000 | [diff] [blame] | 2338 | serial_type = sqlite3VdbeSerialType(pRec, file_format); |
shane | 3f8d5cf | 2008-04-24 19:15:09 +0000 | [diff] [blame] | 2339 | i += putVarint32(&zNewRecord[i], serial_type); /* serial type */ |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 2340 | } |
drh | a2a49dc | 2008-01-02 14:28:13 +0000 | [diff] [blame] | 2341 | for(pRec=pData0; pRec<=pLast; pRec++){ /* serial data */ |
drh | fdf972a | 2007-05-02 13:30:27 +0000 | [diff] [blame] | 2342 | i += sqlite3VdbeSerialPut(&zNewRecord[i], nByte-i, pRec, file_format); |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 2343 | } |
drh | fdf972a | 2007-05-02 13:30:27 +0000 | [diff] [blame] | 2344 | assert( i==nByte ); |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 2345 | |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2346 | assert( pOp->p3>0 && pOp->p3<=p->nMem ); |
drh | 477df4b | 2008-01-05 18:48:24 +0000 | [diff] [blame] | 2347 | pOut->n = nByte; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 2348 | pOut->flags = MEM_Blob | MEM_Dyn; |
| 2349 | pOut->xDel = 0; |
drh | fdf972a | 2007-05-02 13:30:27 +0000 | [diff] [blame] | 2350 | if( nZero ){ |
drh | 477df4b | 2008-01-05 18:48:24 +0000 | [diff] [blame] | 2351 | pOut->u.i = nZero; |
| 2352 | pOut->flags |= MEM_Zero; |
drh | fdf972a | 2007-05-02 13:30:27 +0000 | [diff] [blame] | 2353 | } |
drh | 477df4b | 2008-01-05 18:48:24 +0000 | [diff] [blame] | 2354 | pOut->enc = SQLITE_UTF8; /* In case the blob is ever converted to text */ |
drh | 1013c93 | 2008-01-06 00:25:21 +0000 | [diff] [blame] | 2355 | REGISTER_TRACE(pOp->p3, pOut); |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 2356 | UPDATE_MAX_BLOBSIZE(pOut); |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 2357 | break; |
| 2358 | } |
| 2359 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 2360 | /* Opcode: Statement P1 * * * * |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2361 | ** |
drh | 7f0f12e | 2004-05-21 13:39:50 +0000 | [diff] [blame] | 2362 | ** Begin an individual statement transaction which is part of a larger |
drh | 82ed1e5 | 2008-04-25 12:25:42 +0000 | [diff] [blame] | 2363 | ** transaction. This is needed so that the statement |
drh | 7f0f12e | 2004-05-21 13:39:50 +0000 | [diff] [blame] | 2364 | ** can be rolled back after an error without having to roll back the |
| 2365 | ** entire transaction. The statement transaction will automatically |
| 2366 | ** commit when the VDBE halts. |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2367 | ** |
drh | 82ed1e5 | 2008-04-25 12:25:42 +0000 | [diff] [blame] | 2368 | ** If the database connection is currently in autocommit mode (that |
| 2369 | ** is to say, if it is in between BEGIN and COMMIT) |
| 2370 | ** and if there are no other active statements on the same database |
| 2371 | ** connection, then this operation is a no-op. No statement transaction |
| 2372 | ** is needed since any error can use the normal ROLLBACK process to |
| 2373 | ** undo changes. |
| 2374 | ** |
| 2375 | ** If a statement transaction is started, then a statement journal file |
| 2376 | ** will be allocated and initialized. |
| 2377 | ** |
drh | 7f0f12e | 2004-05-21 13:39:50 +0000 | [diff] [blame] | 2378 | ** The statement is begun on the database file with index P1. The main |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2379 | ** database file has an index of 0 and the file used for temporary tables |
| 2380 | ** has an index of 1. |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2381 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2382 | case OP_Statement: { |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 2383 | if( db->autoCommit==0 || db->activeVdbeCnt>1 ){ |
| 2384 | int i = pOp->p1; |
| 2385 | Btree *pBt; |
| 2386 | assert( i>=0 && i<db->nDb ); |
| 2387 | assert( db->aDb[i].pBt!=0 ); |
| 2388 | pBt = db->aDb[i].pBt; |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 2389 | assert( sqlite3BtreeIsInTrans(pBt) ); |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 2390 | assert( (p->btreeMask & (1<<i))!=0 ); |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 2391 | if( !sqlite3BtreeIsInStmt(pBt) ){ |
| 2392 | rc = sqlite3BtreeBeginStmt(pBt); |
danielk1977 | 182c4ba | 2007-06-27 15:53:34 +0000 | [diff] [blame] | 2393 | p->openedStatement = 1; |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 2394 | } |
| 2395 | } |
| 2396 | break; |
| 2397 | } |
| 2398 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 2399 | /* Opcode: AutoCommit P1 P2 * * * |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 2400 | ** |
| 2401 | ** 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] | 2402 | ** back any currently active btree transactions. If there are any active |
| 2403 | ** VMs (apart from this one), then the COMMIT or ROLLBACK statement fails. |
drh | 92f02c3 | 2004-09-02 14:57:08 +0000 | [diff] [blame] | 2404 | ** |
| 2405 | ** This instruction causes the VM to halt. |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 2406 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2407 | case OP_AutoCommit: { |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 2408 | u8 i = pOp->p1; |
| 2409 | u8 rollback = pOp->p2; |
| 2410 | |
| 2411 | assert( i==1 || i==0 ); |
| 2412 | assert( i==1 || rollback==0 ); |
| 2413 | |
drh | 92f02c3 | 2004-09-02 14:57:08 +0000 | [diff] [blame] | 2414 | assert( db->activeVdbeCnt>0 ); /* At least this one VM is active */ |
danielk1977 | 46c43ed | 2004-06-30 06:30:25 +0000 | [diff] [blame] | 2415 | |
| 2416 | if( db->activeVdbeCnt>1 && i && !db->autoCommit ){ |
| 2417 | /* If this instruction implements a COMMIT or ROLLBACK, other VMs are |
| 2418 | ** still running, and a transaction is active, return an error indicating |
| 2419 | ** that the other VMs must complete first. |
| 2420 | */ |
drh | f089aa4 | 2008-07-08 19:34:06 +0000 | [diff] [blame] | 2421 | sqlite3SetString(&p->zErrMsg, db, "cannot %s transaction - " |
| 2422 | "SQL statements in progress", |
| 2423 | rollback ? "rollback" : "commit"); |
danielk1977 | 46c43ed | 2004-06-30 06:30:25 +0000 | [diff] [blame] | 2424 | rc = SQLITE_ERROR; |
| 2425 | }else if( i!=db->autoCommit ){ |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 2426 | if( pOp->p2 ){ |
drh | 92f02c3 | 2004-09-02 14:57:08 +0000 | [diff] [blame] | 2427 | assert( i==1 ); |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 2428 | sqlite3RollbackAll(db); |
danielk1977 | f3f06bb | 2005-12-16 15:24:28 +0000 | [diff] [blame] | 2429 | db->autoCommit = 1; |
| 2430 | }else{ |
| 2431 | db->autoCommit = i; |
| 2432 | if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){ |
danielk1977 | f3f06bb | 2005-12-16 15:24:28 +0000 | [diff] [blame] | 2433 | p->pc = pc; |
| 2434 | db->autoCommit = 1-i; |
drh | 900b31e | 2007-08-28 02:27:51 +0000 | [diff] [blame] | 2435 | p->rc = rc = SQLITE_BUSY; |
| 2436 | goto vdbe_return; |
danielk1977 | f3f06bb | 2005-12-16 15:24:28 +0000 | [diff] [blame] | 2437 | } |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 2438 | } |
drh | 83968c4 | 2007-04-18 16:45:24 +0000 | [diff] [blame] | 2439 | if( p->rc==SQLITE_OK ){ |
drh | 900b31e | 2007-08-28 02:27:51 +0000 | [diff] [blame] | 2440 | rc = SQLITE_DONE; |
drh | 83968c4 | 2007-04-18 16:45:24 +0000 | [diff] [blame] | 2441 | }else{ |
drh | 900b31e | 2007-08-28 02:27:51 +0000 | [diff] [blame] | 2442 | rc = SQLITE_ERROR; |
drh | 83968c4 | 2007-04-18 16:45:24 +0000 | [diff] [blame] | 2443 | } |
drh | 900b31e | 2007-08-28 02:27:51 +0000 | [diff] [blame] | 2444 | goto vdbe_return; |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 2445 | }else{ |
drh | f089aa4 | 2008-07-08 19:34:06 +0000 | [diff] [blame] | 2446 | sqlite3SetString(&p->zErrMsg, db, |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 2447 | (!i)?"cannot start a transaction within a transaction":( |
| 2448 | (rollback)?"cannot rollback - no transaction is active": |
drh | f089aa4 | 2008-07-08 19:34:06 +0000 | [diff] [blame] | 2449 | "cannot commit - no transaction is active")); |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 2450 | |
| 2451 | rc = SQLITE_ERROR; |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2452 | } |
| 2453 | break; |
| 2454 | } |
| 2455 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 2456 | /* Opcode: Transaction P1 P2 * * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2457 | ** |
| 2458 | ** Begin a transaction. The transaction ends when a Commit or Rollback |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2459 | ** opcode is encountered. Depending on the ON CONFLICT setting, the |
| 2460 | ** transaction might also be rolled back if an error is encountered. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2461 | ** |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2462 | ** P1 is the index of the database file on which the transaction is |
| 2463 | ** started. Index 0 is the main database file and index 1 is the |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 2464 | ** file used for temporary tables. Indices of 2 or more are used for |
| 2465 | ** attached databases. |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 2466 | ** |
drh | 8024205 | 2004-06-09 00:48:12 +0000 | [diff] [blame] | 2467 | ** 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] | 2468 | ** obtained on the database file when a write-transaction is started. No |
drh | 8024205 | 2004-06-09 00:48:12 +0000 | [diff] [blame] | 2469 | ** other process can start another write transaction while this transaction is |
| 2470 | ** underway. Starting a write transaction also creates a rollback journal. A |
| 2471 | ** write transaction must be started before any changes can be made to the |
drh | 684917c | 2004-10-05 02:41:42 +0000 | [diff] [blame] | 2472 | ** database. If P2 is 2 or greater then an EXCLUSIVE lock is also obtained |
| 2473 | ** on the file. |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 2474 | ** |
| 2475 | ** 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] | 2476 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2477 | case OP_Transaction: { |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2478 | int i = pOp->p1; |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 2479 | Btree *pBt; |
| 2480 | |
drh | 8bf8dc9 | 2003-05-17 17:35:10 +0000 | [diff] [blame] | 2481 | assert( i>=0 && i<db->nDb ); |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 2482 | assert( (p->btreeMask & (1<<i))!=0 ); |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 2483 | pBt = db->aDb[i].pBt; |
| 2484 | |
danielk1977 | 24162fe | 2004-06-04 06:22:00 +0000 | [diff] [blame] | 2485 | if( pBt ){ |
danielk1977 | 40b38dc | 2004-06-26 08:38:24 +0000 | [diff] [blame] | 2486 | rc = sqlite3BtreeBeginTrans(pBt, pOp->p2); |
danielk1977 | 24162fe | 2004-06-04 06:22:00 +0000 | [diff] [blame] | 2487 | if( rc==SQLITE_BUSY ){ |
danielk1977 | 2a764eb | 2004-06-12 01:43:26 +0000 | [diff] [blame] | 2488 | p->pc = pc; |
drh | 900b31e | 2007-08-28 02:27:51 +0000 | [diff] [blame] | 2489 | p->rc = rc = SQLITE_BUSY; |
drh | 900b31e | 2007-08-28 02:27:51 +0000 | [diff] [blame] | 2490 | goto vdbe_return; |
danielk1977 | 24162fe | 2004-06-04 06:22:00 +0000 | [diff] [blame] | 2491 | } |
danielk1977 | 2ef6848 | 2008-07-07 17:13:08 +0000 | [diff] [blame] | 2492 | if( rc!=SQLITE_OK && rc!=SQLITE_READONLY /* && rc!=SQLITE_BUSY */ ){ |
danielk1977 | 24162fe | 2004-06-04 06:22:00 +0000 | [diff] [blame] | 2493 | goto abort_due_to_error; |
drh | 90bfcda | 2001-09-23 19:46:51 +0000 | [diff] [blame] | 2494 | } |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 2495 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2496 | break; |
| 2497 | } |
| 2498 | |
drh | b1fdb2a | 2008-01-05 04:06:03 +0000 | [diff] [blame] | 2499 | /* Opcode: ReadCookie P1 P2 P3 * * |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 2500 | ** |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2501 | ** Read cookie number P3 from database P1 and write it into register P2. |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 2502 | ** P3==0 is the schema version. P3==1 is the database format. |
| 2503 | ** P3==2 is the recommended pager cache size, and so forth. P1==0 is |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2504 | ** the main database file and P1==1 is the database file used to store |
| 2505 | ** temporary tables. |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 2506 | ** |
danielk1977 | 418899a | 2007-06-24 10:14:00 +0000 | [diff] [blame] | 2507 | ** If P1 is negative, then this is a request to read the size of a |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 2508 | ** databases free-list. P3 must be set to 1 in this case. The actual |
danielk1977 | 418899a | 2007-06-24 10:14:00 +0000 | [diff] [blame] | 2509 | ** database accessed is ((P1+1)*-1). For example, a P1 parameter of -1 |
danielk1977 | d62e76c | 2007-06-24 16:11:03 +0000 | [diff] [blame] | 2510 | ** corresponds to database 0 ("main"), a P1 of -2 is database 1 ("temp"). |
danielk1977 | 418899a | 2007-06-24 10:14:00 +0000 | [diff] [blame] | 2511 | ** |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 2512 | ** There must be a read-lock on the database (either a transaction |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2513 | ** must be started or there must be an open cursor) before |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 2514 | ** executing this instruction. |
| 2515 | */ |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 2516 | case OP_ReadCookie: { /* out2-prerelease */ |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 2517 | int iMeta; |
danielk1977 | 180b56a | 2007-06-24 08:00:42 +0000 | [diff] [blame] | 2518 | int iDb = pOp->p1; |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 2519 | int iCookie = pOp->p3; |
danielk1977 | 180b56a | 2007-06-24 08:00:42 +0000 | [diff] [blame] | 2520 | |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 2521 | assert( pOp->p3<SQLITE_N_BTREE_META ); |
danielk1977 | 180b56a | 2007-06-24 08:00:42 +0000 | [diff] [blame] | 2522 | if( iDb<0 ){ |
| 2523 | iDb = (-1*(iDb+1)); |
| 2524 | iCookie *= -1; |
| 2525 | } |
| 2526 | assert( iDb>=0 && iDb<db->nDb ); |
| 2527 | assert( db->aDb[iDb].pBt!=0 ); |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 2528 | assert( (p->btreeMask & (1<<iDb))!=0 ); |
drh | a3b321d | 2004-05-11 09:31:31 +0000 | [diff] [blame] | 2529 | /* The indexing of meta values at the schema layer is off by one from |
| 2530 | ** the indexing in the btree layer. The btree considers meta[0] to |
| 2531 | ** be the number of free pages in the database (a read-only value) |
| 2532 | ** and meta[1] to be the schema cookie. The schema layer considers |
| 2533 | ** meta[1] to be the schema cookie. So we have to shift the index |
| 2534 | ** by one in the following statement. |
| 2535 | */ |
danielk1977 | 180b56a | 2007-06-24 08:00:42 +0000 | [diff] [blame] | 2536 | rc = sqlite3BtreeGetMeta(db->aDb[iDb].pBt, 1 + iCookie, (u32 *)&iMeta); |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 2537 | pOut->u.i = iMeta; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 2538 | MemSetTypeFlag(pOut, MEM_Int); |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 2539 | break; |
| 2540 | } |
| 2541 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 2542 | /* Opcode: SetCookie P1 P2 P3 * * |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 2543 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 2544 | ** Write the content of register P3 (interpreted as an integer) |
| 2545 | ** into cookie number P2 of database P1. |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2546 | ** P2==0 is the schema version. P2==1 is the database format. |
| 2547 | ** P2==2 is the recommended pager cache size, and so forth. P1==0 is |
| 2548 | ** the main database file and P1==1 is the database file used to store |
| 2549 | ** temporary tables. |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 2550 | ** |
| 2551 | ** A transaction must be started before executing this opcode. |
| 2552 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2553 | case OP_SetCookie: { /* in3 */ |
drh | 3f7d4e4 | 2004-07-24 14:35:58 +0000 | [diff] [blame] | 2554 | Db *pDb; |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 2555 | assert( pOp->p2<SQLITE_N_BTREE_META ); |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2556 | assert( pOp->p1>=0 && pOp->p1<db->nDb ); |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 2557 | assert( (p->btreeMask & (1<<pOp->p1))!=0 ); |
drh | 3f7d4e4 | 2004-07-24 14:35:58 +0000 | [diff] [blame] | 2558 | pDb = &db->aDb[pOp->p1]; |
| 2559 | assert( pDb->pBt!=0 ); |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 2560 | sqlite3VdbeMemIntegerify(pIn3); |
drh | a3b321d | 2004-05-11 09:31:31 +0000 | [diff] [blame] | 2561 | /* See note about index shifting on OP_ReadCookie */ |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 2562 | rc = sqlite3BtreeUpdateMeta(pDb->pBt, 1+pOp->p2, (int)pIn3->u.i); |
drh | 3f7d4e4 | 2004-07-24 14:35:58 +0000 | [diff] [blame] | 2563 | if( pOp->p2==0 ){ |
| 2564 | /* When the schema cookie changes, record the new cookie internally */ |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 2565 | pDb->pSchema->schema_cookie = pIn3->u.i; |
drh | 3f7d4e4 | 2004-07-24 14:35:58 +0000 | [diff] [blame] | 2566 | db->flags |= SQLITE_InternChanges; |
drh | d28bcb3 | 2005-12-21 14:43:11 +0000 | [diff] [blame] | 2567 | }else if( pOp->p2==1 ){ |
| 2568 | /* Record changes in the file format */ |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 2569 | pDb->pSchema->file_format = pIn3->u.i; |
drh | 3f7d4e4 | 2004-07-24 14:35:58 +0000 | [diff] [blame] | 2570 | } |
drh | fd426c6 | 2006-01-30 15:34:22 +0000 | [diff] [blame] | 2571 | if( pOp->p1==1 ){ |
| 2572 | /* Invalidate all prepared statements whenever the TEMP database |
| 2573 | ** schema is changed. Ticket #1644 */ |
| 2574 | sqlite3ExpirePreparedStatements(db); |
| 2575 | } |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 2576 | break; |
| 2577 | } |
| 2578 | |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 2579 | /* Opcode: VerifyCookie P1 P2 * |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 2580 | ** |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2581 | ** Check the value of global database parameter number 0 (the |
| 2582 | ** schema version) and make sure it is equal to P2. |
| 2583 | ** P1 is the database number which is 0 for the main database file |
| 2584 | ** and 1 for the file holding temporary tables and some higher number |
| 2585 | ** for auxiliary databases. |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 2586 | ** |
| 2587 | ** The cookie changes its value whenever the database schema changes. |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2588 | ** This operation is used to detect when that the cookie has changed |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 2589 | ** and that the current process needs to reread the schema. |
| 2590 | ** |
| 2591 | ** Either a transaction needs to have been started or an OP_Open needs |
| 2592 | ** to be executed (to establish a read lock) before this opcode is |
| 2593 | ** invoked. |
| 2594 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2595 | case OP_VerifyCookie: { |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 2596 | int iMeta; |
drh | c275b4e | 2004-07-19 17:25:24 +0000 | [diff] [blame] | 2597 | Btree *pBt; |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2598 | assert( pOp->p1>=0 && pOp->p1<db->nDb ); |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 2599 | assert( (p->btreeMask & (1<<pOp->p1))!=0 ); |
drh | c275b4e | 2004-07-19 17:25:24 +0000 | [diff] [blame] | 2600 | pBt = db->aDb[pOp->p1].pBt; |
| 2601 | if( pBt ){ |
| 2602 | rc = sqlite3BtreeGetMeta(pBt, 1, (u32 *)&iMeta); |
| 2603 | }else{ |
| 2604 | rc = SQLITE_OK; |
| 2605 | iMeta = 0; |
| 2606 | } |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 2607 | if( rc==SQLITE_OK && iMeta!=pOp->p2 ){ |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 2608 | sqlite3_free(p->zErrMsg); |
| 2609 | p->zErrMsg = sqlite3DbStrDup(db, "database schema has changed"); |
danielk1977 | 896e792 | 2007-04-17 08:32:33 +0000 | [diff] [blame] | 2610 | /* If the schema-cookie from the database file matches the cookie |
| 2611 | ** stored with the in-memory representation of the schema, do |
| 2612 | ** not reload the schema from the database file. |
| 2613 | ** |
shane | 21e7feb | 2008-05-30 15:59:49 +0000 | [diff] [blame] | 2614 | ** If virtual-tables are in use, this is not just an optimization. |
danielk1977 | 896e792 | 2007-04-17 08:32:33 +0000 | [diff] [blame] | 2615 | ** Often, v-tables store their data in other SQLite tables, which |
| 2616 | ** are queried from within xNext() and other v-table methods using |
| 2617 | ** prepared queries. If such a query is out-of-date, we do not want to |
| 2618 | ** discard the database schema, as the user code implementing the |
| 2619 | ** v-table would have to be ready for the sqlite3_vtab structure itself |
| 2620 | ** to be invalidated whenever sqlite3_step() is called from within |
| 2621 | ** a v-table method. |
| 2622 | */ |
| 2623 | if( db->aDb[pOp->p1].pSchema->schema_cookie!=iMeta ){ |
| 2624 | sqlite3ResetInternalSchema(db, pOp->p1); |
| 2625 | } |
| 2626 | |
drh | f6d8ab8 | 2007-01-12 23:43:42 +0000 | [diff] [blame] | 2627 | sqlite3ExpirePreparedStatements(db); |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 2628 | rc = SQLITE_SCHEMA; |
| 2629 | } |
| 2630 | break; |
| 2631 | } |
| 2632 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 2633 | /* Opcode: OpenRead P1 P2 P3 P4 P5 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2634 | ** |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2635 | ** Open a read-only cursor for the database table whose root page is |
danielk1977 | 207872a | 2008-01-03 07:54:23 +0000 | [diff] [blame] | 2636 | ** P2 in a database file. The database file is determined by P3. |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 2637 | ** P3==0 means the main database, P3==1 means the database used for |
| 2638 | ** temporary tables, and P3>1 means used the corresponding attached |
| 2639 | ** database. Give the new cursor an identifier of P1. The P1 |
danielk1977 | 207872a | 2008-01-03 07:54:23 +0000 | [diff] [blame] | 2640 | ** values need not be contiguous but all P1 values should be small integers. |
| 2641 | ** It is an error for P1 to be negative. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2642 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 2643 | ** If P5!=0 then use the content of register P2 as the root page, not |
| 2644 | ** the value of P2 itself. |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 2645 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2646 | ** There will be a read lock on the database whenever there is an |
| 2647 | ** open cursor. If the database was unlocked prior to this instruction |
| 2648 | ** then a read lock is acquired as part of this instruction. A read |
| 2649 | ** lock allows other processes to read the database but prohibits |
| 2650 | ** any other process from modifying the database. The read lock is |
| 2651 | ** released when all cursors are closed. If this instruction attempts |
| 2652 | ** to get a read lock but fails, the script terminates with an |
| 2653 | ** SQLITE_BUSY error code. |
| 2654 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 2655 | ** The P4 value is a pointer to a KeyInfo structure that defines the |
| 2656 | ** content and collating sequence of indices. P4 is NULL for cursors |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 2657 | ** that are not pointing to indices. |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 2658 | ** |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2659 | ** See also OpenWrite. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2660 | */ |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 2661 | /* Opcode: OpenWrite P1 P2 P3 P4 P5 |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2662 | ** |
| 2663 | ** Open a read/write cursor named P1 on the table or index whose root |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 2664 | ** page is P2. Or if P5!=0 use the content of register P2 to find the |
| 2665 | ** root page. |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2666 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 2667 | ** The P4 value is a pointer to a KeyInfo structure that defines the |
| 2668 | ** content and collating sequence of indices. P4 is NULL for cursors |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 2669 | ** that are not pointing to indices. |
jplyon | 5a56422 | 2003-06-02 06:15:58 +0000 | [diff] [blame] | 2670 | ** |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2671 | ** This instruction works just like OpenRead except that it opens the cursor |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2672 | ** in read/write mode. For a given table, there can be one or more read-only |
| 2673 | ** cursors or a single read/write cursor but not both. |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 2674 | ** |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2675 | ** See also OpenRead. |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2676 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2677 | case OP_OpenRead: |
| 2678 | case OP_OpenWrite: { |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2679 | int i = pOp->p1; |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 2680 | int p2 = pOp->p2; |
danielk1977 | 207872a | 2008-01-03 07:54:23 +0000 | [diff] [blame] | 2681 | int iDb = pOp->p3; |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 2682 | int wrFlag; |
| 2683 | Btree *pX; |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 2684 | Cursor *pCur; |
drh | d946db0 | 2005-12-29 19:23:06 +0000 | [diff] [blame] | 2685 | Db *pDb; |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2686 | |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 2687 | assert( iDb>=0 && iDb<db->nDb ); |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 2688 | assert( (p->btreeMask & (1<<iDb))!=0 ); |
drh | d946db0 | 2005-12-29 19:23:06 +0000 | [diff] [blame] | 2689 | pDb = &db->aDb[iDb]; |
| 2690 | pX = pDb->pBt; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 2691 | assert( pX!=0 ); |
drh | d946db0 | 2005-12-29 19:23:06 +0000 | [diff] [blame] | 2692 | if( pOp->opcode==OP_OpenWrite ){ |
| 2693 | wrFlag = 1; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2694 | if( pDb->pSchema->file_format < p->minWriteFileFormat ){ |
| 2695 | p->minWriteFileFormat = pDb->pSchema->file_format; |
drh | d946db0 | 2005-12-29 19:23:06 +0000 | [diff] [blame] | 2696 | } |
| 2697 | }else{ |
| 2698 | wrFlag = 0; |
| 2699 | } |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 2700 | if( pOp->p5 ){ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2701 | assert( p2>0 ); |
| 2702 | assert( p2<=p->nMem ); |
| 2703 | pIn2 = &p->aMem[p2]; |
| 2704 | sqlite3VdbeMemIntegerify(pIn2); |
| 2705 | p2 = pIn2->u.i; |
drh | a611040 | 2005-07-28 20:51:19 +0000 | [diff] [blame] | 2706 | assert( p2>=2 ); |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 2707 | } |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 2708 | assert( i>=0 ); |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 2709 | pCur = allocateCursor(p, i, &pOp[-1], iDb, 1); |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 2710 | if( pCur==0 ) goto no_mem; |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 2711 | pCur->nullRow = 1; |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 2712 | rc = sqlite3BtreeCursor(pX, p2, wrFlag, pOp->p4.p, pCur->pCursor); |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 2713 | if( pOp->p4type==P4_KEYINFO ){ |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 2714 | pCur->pKeyInfo = pOp->p4.pKeyInfo; |
danielk1977 | 24162fe | 2004-06-04 06:22:00 +0000 | [diff] [blame] | 2715 | pCur->pIncrKey = &pCur->pKeyInfo->incrKey; |
danielk1977 | 14db266 | 2006-01-09 16:12:04 +0000 | [diff] [blame] | 2716 | pCur->pKeyInfo->enc = ENC(p->db); |
danielk1977 | 24162fe | 2004-06-04 06:22:00 +0000 | [diff] [blame] | 2717 | }else{ |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 2718 | pCur->pKeyInfo = 0; |
danielk1977 | 24162fe | 2004-06-04 06:22:00 +0000 | [diff] [blame] | 2719 | pCur->pIncrKey = &pCur->bogusIncrKey; |
| 2720 | } |
| 2721 | switch( rc ){ |
| 2722 | case SQLITE_BUSY: { |
danielk1977 | 2a764eb | 2004-06-12 01:43:26 +0000 | [diff] [blame] | 2723 | p->pc = pc; |
drh | 900b31e | 2007-08-28 02:27:51 +0000 | [diff] [blame] | 2724 | p->rc = rc = SQLITE_BUSY; |
drh | 900b31e | 2007-08-28 02:27:51 +0000 | [diff] [blame] | 2725 | goto vdbe_return; |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 2726 | } |
danielk1977 | 24162fe | 2004-06-04 06:22:00 +0000 | [diff] [blame] | 2727 | case SQLITE_OK: { |
| 2728 | int flags = sqlite3BtreeFlags(pCur->pCursor); |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 2729 | /* Sanity checking. Only the lower four bits of the flags byte should |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame] | 2730 | ** be used. Bit 3 (mask 0x08) is unpredictable. The lower 3 bits |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 2731 | ** (mask 0x07) should be either 5 (intkey+leafdata for tables) or |
| 2732 | ** 2 (zerodata for indices). If these conditions are not met it can |
| 2733 | ** only mean that we are dealing with a corrupt database file |
| 2734 | */ |
| 2735 | if( (flags & 0xf0)!=0 || ((flags & 0x07)!=5 && (flags & 0x07)!=2) ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 2736 | rc = SQLITE_CORRUPT_BKPT; |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 2737 | goto abort_due_to_error; |
| 2738 | } |
| 2739 | pCur->isTable = (flags & BTREE_INTKEY)!=0; |
| 2740 | pCur->isIndex = (flags & BTREE_ZERODATA)!=0; |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 2741 | /* If P4==0 it means we are expected to open a table. If P4!=0 then |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 2742 | ** we expect to be opening an index. If this is not what happened, |
| 2743 | ** then the database is corrupt |
| 2744 | */ |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 2745 | if( (pCur->isTable && pOp->p4type==P4_KEYINFO) |
| 2746 | || (pCur->isIndex && pOp->p4type!=P4_KEYINFO) ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 2747 | rc = SQLITE_CORRUPT_BKPT; |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 2748 | goto abort_due_to_error; |
| 2749 | } |
danielk1977 | 24162fe | 2004-06-04 06:22:00 +0000 | [diff] [blame] | 2750 | break; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2751 | } |
danielk1977 | 24162fe | 2004-06-04 06:22:00 +0000 | [diff] [blame] | 2752 | case SQLITE_EMPTY: { |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 2753 | pCur->isTable = pOp->p4type!=P4_KEYINFO; |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 2754 | pCur->isIndex = !pCur->isTable; |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 2755 | pCur->pCursor = 0; |
danielk1977 | 24162fe | 2004-06-04 06:22:00 +0000 | [diff] [blame] | 2756 | rc = SQLITE_OK; |
| 2757 | break; |
| 2758 | } |
| 2759 | default: { |
| 2760 | goto abort_due_to_error; |
| 2761 | } |
| 2762 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2763 | break; |
| 2764 | } |
| 2765 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 2766 | /* Opcode: OpenEphemeral P1 P2 * P4 * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2767 | ** |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 2768 | ** Open a new cursor P1 to a transient table. |
drh | 9170dd7 | 2005-07-08 17:13:46 +0000 | [diff] [blame] | 2769 | ** The cursor is always opened read/write even if |
| 2770 | ** the main database is read-only. The transient or virtual |
| 2771 | ** table is deleted automatically when the cursor is closed. |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 2772 | ** |
drh | 0342b1f | 2005-09-01 03:07:44 +0000 | [diff] [blame] | 2773 | ** P2 is the number of columns in the virtual table. |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 2774 | ** The cursor points to a BTree table if P4==0 and to a BTree index |
| 2775 | ** if P4 is not 0. If P4 is not NULL, it points to a KeyInfo structure |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 2776 | ** that defines the format of keys in the index. |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 2777 | ** |
| 2778 | ** This opcode was once called OpenTemp. But that created |
| 2779 | ** confusion because the term "temp table", might refer either |
| 2780 | ** to a TEMP table at the SQL level, or to a table opened by |
| 2781 | ** this opcode. Then this opcode was call OpenVirtual. But |
| 2782 | ** that created confusion with the whole virtual-table idea. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2783 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2784 | case OP_OpenEphemeral: { |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2785 | int i = pOp->p1; |
| 2786 | Cursor *pCx; |
drh | 33f4e02 | 2007-09-03 15:19:34 +0000 | [diff] [blame] | 2787 | static const int openFlags = |
| 2788 | SQLITE_OPEN_READWRITE | |
| 2789 | SQLITE_OPEN_CREATE | |
| 2790 | SQLITE_OPEN_EXCLUSIVE | |
| 2791 | SQLITE_OPEN_DELETEONCLOSE | |
| 2792 | SQLITE_OPEN_TRANSIENT_DB; |
| 2793 | |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 2794 | assert( i>=0 ); |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 2795 | pCx = allocateCursor(p, i, pOp, -1, 1); |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 2796 | if( pCx==0 ) goto no_mem; |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 2797 | pCx->nullRow = 1; |
drh | 33f4e02 | 2007-09-03 15:19:34 +0000 | [diff] [blame] | 2798 | rc = sqlite3BtreeFactory(db, 0, 1, SQLITE_DEFAULT_TEMP_CACHE_SIZE, openFlags, |
| 2799 | &pCx->pBt); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2800 | if( rc==SQLITE_OK ){ |
danielk1977 | 40b38dc | 2004-06-26 08:38:24 +0000 | [diff] [blame] | 2801 | rc = sqlite3BtreeBeginTrans(pCx->pBt, 1); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2802 | } |
| 2803 | if( rc==SQLITE_OK ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2804 | /* If a transient index is required, create it by calling |
| 2805 | ** sqlite3BtreeCreateTable() with the BTREE_ZERODATA flag before |
| 2806 | ** opening it. If a transient table is required, just use the |
danielk1977 | 0dbe72b | 2004-05-11 04:54:49 +0000 | [diff] [blame] | 2807 | ** automatically created table with root-page 1 (an INTKEY table). |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2808 | */ |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 2809 | if( pOp->p4.pKeyInfo ){ |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 2810 | int pgno; |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 2811 | assert( pOp->p4type==P4_KEYINFO ); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2812 | rc = sqlite3BtreeCreateTable(pCx->pBt, &pgno, BTREE_ZERODATA); |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 2813 | if( rc==SQLITE_OK ){ |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 2814 | assert( pgno==MASTER_ROOT+1 ); |
drh | 1e968a0 | 2008-03-25 00:22:21 +0000 | [diff] [blame] | 2815 | rc = sqlite3BtreeCursor(pCx->pBt, pgno, 1, |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 2816 | (KeyInfo*)pOp->p4.z, pCx->pCursor); |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 2817 | pCx->pKeyInfo = pOp->p4.pKeyInfo; |
danielk1977 | 14db266 | 2006-01-09 16:12:04 +0000 | [diff] [blame] | 2818 | pCx->pKeyInfo->enc = ENC(p->db); |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 2819 | pCx->pIncrKey = &pCx->pKeyInfo->incrKey; |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 2820 | } |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 2821 | pCx->isTable = 0; |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 2822 | }else{ |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 2823 | rc = sqlite3BtreeCursor(pCx->pBt, MASTER_ROOT, 1, 0, pCx->pCursor); |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 2824 | pCx->isTable = 1; |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 2825 | pCx->pIncrKey = &pCx->bogusIncrKey; |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 2826 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2827 | } |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 2828 | pCx->isIndex = !pCx->isTable; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2829 | break; |
| 2830 | } |
| 2831 | |
danielk1977 | 9882d99 | 2008-03-27 17:59:01 +0000 | [diff] [blame] | 2832 | /* Opcode: OpenPseudo P1 P2 * * * |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 2833 | ** |
| 2834 | ** Open a new cursor that points to a fake table that contains a single |
| 2835 | ** row of data. Any attempt to write a second row of data causes the |
| 2836 | ** first row to be deleted. All data is deleted when the cursor is |
| 2837 | ** closed. |
| 2838 | ** |
| 2839 | ** A pseudo-table created by this opcode is useful for holding the |
drh | cdd536f | 2006-03-17 00:04:03 +0000 | [diff] [blame] | 2840 | ** NEW or OLD tables in a trigger. Also used to hold the a single |
| 2841 | ** row output from the sorter so that the row can be decomposed into |
| 2842 | ** individual columns using the OP_Column opcode. |
danielk1977 | 9882d99 | 2008-03-27 17:59:01 +0000 | [diff] [blame] | 2843 | ** |
| 2844 | ** When OP_Insert is executed to insert a row in to the pseudo table, |
| 2845 | ** the pseudo-table cursor may or may not make it's own copy of the |
| 2846 | ** original row data. If P2 is 0, then the pseudo-table will copy the |
| 2847 | ** original row data. Otherwise, a pointer to the original memory cell |
| 2848 | ** is stored. In this case, the vdbe program must ensure that the |
| 2849 | ** memory cell containing the row data is not overwritten until the |
| 2850 | ** pseudo table is closed (or a new row is inserted into it). |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 2851 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2852 | case OP_OpenPseudo: { |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 2853 | int i = pOp->p1; |
| 2854 | Cursor *pCx; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 2855 | assert( i>=0 ); |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 2856 | pCx = allocateCursor(p, i, &pOp[-1], -1, 0); |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 2857 | if( pCx==0 ) goto no_mem; |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 2858 | pCx->nullRow = 1; |
| 2859 | pCx->pseudoTable = 1; |
danielk1977 | 9882d99 | 2008-03-27 17:59:01 +0000 | [diff] [blame] | 2860 | pCx->ephemPseudoTable = pOp->p2; |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 2861 | pCx->pIncrKey = &pCx->bogusIncrKey; |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 2862 | pCx->isTable = 1; |
| 2863 | pCx->isIndex = 0; |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 2864 | break; |
| 2865 | } |
| 2866 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 2867 | /* Opcode: Close P1 * * * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2868 | ** |
| 2869 | ** Close a cursor previously opened as P1. If P1 is not |
| 2870 | ** currently open, this instruction is a no-op. |
| 2871 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2872 | case OP_Close: { |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2873 | int i = pOp->p1; |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 2874 | assert( i>=0 && i<p->nCursor ); |
| 2875 | sqlite3VdbeFreeCursor(p, p->apCsr[i]); |
| 2876 | p->apCsr[i] = 0; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2877 | break; |
| 2878 | } |
| 2879 | |
danielk1977 | b790c6c | 2008-04-18 10:25:24 +0000 | [diff] [blame] | 2880 | /* Opcode: MoveGe P1 P2 P3 P4 * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2881 | ** |
danielk1977 | b790c6c | 2008-04-18 10:25:24 +0000 | [diff] [blame] | 2882 | ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys), |
| 2883 | ** use the integer value in register P3 as a key. If cursor P1 refers |
| 2884 | ** to an SQL index, then P3 is the first in an array of P4 registers |
| 2885 | ** that are used as an unpacked index key. |
| 2886 | ** |
| 2887 | ** Reposition cursor P1 so that it points to the smallest entry that |
| 2888 | ** is greater than or equal to the key value. If there are no records |
| 2889 | ** greater than or equal to the key and P2 is not zero, then jump to P2. |
drh | 7cf6e4d | 2004-05-19 14:56:55 +0000 | [diff] [blame] | 2890 | ** |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 2891 | ** A special feature of this opcode (and different from the |
| 2892 | ** related OP_MoveGt, OP_MoveLt, and OP_MoveLe) is that if P2 is |
| 2893 | ** zero and P1 is an SQL table (a b-tree with integer keys) then |
| 2894 | ** the seek is deferred until it is actually needed. It might be |
| 2895 | ** the case that the cursor is never accessed. By deferring the |
| 2896 | ** seek, we avoid unnecessary seeks. |
| 2897 | ** |
drh | 7cf6e4d | 2004-05-19 14:56:55 +0000 | [diff] [blame] | 2898 | ** See also: Found, NotFound, Distinct, MoveLt, MoveGt, MoveLe |
| 2899 | */ |
danielk1977 | b790c6c | 2008-04-18 10:25:24 +0000 | [diff] [blame] | 2900 | /* Opcode: MoveGt P1 P2 P3 P4 * |
drh | 7cf6e4d | 2004-05-19 14:56:55 +0000 | [diff] [blame] | 2901 | ** |
danielk1977 | b790c6c | 2008-04-18 10:25:24 +0000 | [diff] [blame] | 2902 | ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys), |
| 2903 | ** use the integer value in register P3 as a key. If cursor P1 refers |
| 2904 | ** to an SQL index, then P3 is the first in an array of P4 registers |
| 2905 | ** that are used as an unpacked index key. |
| 2906 | ** |
| 2907 | ** Reposition cursor P1 so that it points to the smallest entry that |
| 2908 | ** is greater than the key value. If there are no records greater than |
| 2909 | ** the key and P2 is not zero, then jump to P2. |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2910 | ** |
drh | 7cf6e4d | 2004-05-19 14:56:55 +0000 | [diff] [blame] | 2911 | ** See also: Found, NotFound, Distinct, MoveLt, MoveGe, MoveLe |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2912 | */ |
danielk1977 | b790c6c | 2008-04-18 10:25:24 +0000 | [diff] [blame] | 2913 | /* Opcode: MoveLt P1 P2 P3 P4 * |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 2914 | ** |
danielk1977 | b790c6c | 2008-04-18 10:25:24 +0000 | [diff] [blame] | 2915 | ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys), |
| 2916 | ** use the integer value in register P3 as a key. If cursor P1 refers |
| 2917 | ** to an SQL index, then P3 is the first in an array of P4 registers |
| 2918 | ** that are used as an unpacked index key. |
| 2919 | ** |
| 2920 | ** Reposition cursor P1 so that it points to the largest entry that |
| 2921 | ** is less than the key value. If there are no records less than |
| 2922 | ** the key and P2 is not zero, then jump to P2. |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 2923 | ** |
drh | 7cf6e4d | 2004-05-19 14:56:55 +0000 | [diff] [blame] | 2924 | ** See also: Found, NotFound, Distinct, MoveGt, MoveGe, MoveLe |
| 2925 | */ |
danielk1977 | 751de56 | 2008-04-18 09:01:15 +0000 | [diff] [blame] | 2926 | /* Opcode: MoveLe P1 P2 P3 P4 * |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 2927 | ** |
danielk1977 | b790c6c | 2008-04-18 10:25:24 +0000 | [diff] [blame] | 2928 | ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys), |
| 2929 | ** use the integer value in register P3 as a key. If cursor P1 refers |
| 2930 | ** to an SQL index, then P3 is the first in an array of P4 registers |
| 2931 | ** that are used as an unpacked index key. |
danielk1977 | 751de56 | 2008-04-18 09:01:15 +0000 | [diff] [blame] | 2932 | ** |
danielk1977 | b790c6c | 2008-04-18 10:25:24 +0000 | [diff] [blame] | 2933 | ** Reposition cursor P1 so that it points to the largest entry that |
| 2934 | ** is less than or equal to the key value. If there are no records |
| 2935 | ** less than or equal to the key and P2 is not zero, then jump to P2. |
drh | 7cf6e4d | 2004-05-19 14:56:55 +0000 | [diff] [blame] | 2936 | ** |
| 2937 | ** See also: Found, NotFound, Distinct, MoveGt, MoveGe, MoveLt |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 2938 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2939 | case OP_MoveLt: /* jump, in3 */ |
| 2940 | case OP_MoveLe: /* jump, in3 */ |
| 2941 | case OP_MoveGe: /* jump, in3 */ |
| 2942 | case OP_MoveGt: { /* jump, in3 */ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2943 | int i = pOp->p1; |
drh | 80ff32f | 2001-11-04 18:32:46 +0000 | [diff] [blame] | 2944 | Cursor *pC; |
| 2945 | |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 2946 | assert( i>=0 && i<p->nCursor ); |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame] | 2947 | pC = p->apCsr[i]; |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 2948 | assert( pC!=0 ); |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 2949 | if( pC->pCursor!=0 ){ |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 2950 | int res, oc; |
drh | 7cf6e4d | 2004-05-19 14:56:55 +0000 | [diff] [blame] | 2951 | oc = pOp->opcode; |
drh | a11846b | 2004-01-07 18:52:56 +0000 | [diff] [blame] | 2952 | pC->nullRow = 0; |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 2953 | *pC->pIncrKey = oc==OP_MoveGt || oc==OP_MoveLe; |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 2954 | if( pC->isTable ){ |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 2955 | i64 iKey = sqlite3VdbeIntValue(pIn3); |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 2956 | if( pOp->p2==0 ){ |
| 2957 | assert( pOp->opcode==OP_MoveGe ); |
drh | a11846b | 2004-01-07 18:52:56 +0000 | [diff] [blame] | 2958 | pC->movetoTarget = iKey; |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 2959 | pC->rowidIsValid = 0; |
drh | a11846b | 2004-01-07 18:52:56 +0000 | [diff] [blame] | 2960 | pC->deferredMoveto = 1; |
drh | a11846b | 2004-01-07 18:52:56 +0000 | [diff] [blame] | 2961 | break; |
| 2962 | } |
drh | e14006d | 2008-03-25 17:23:32 +0000 | [diff] [blame] | 2963 | rc = sqlite3BtreeMoveto(pC->pCursor, 0, 0, (u64)iKey, 0, &res); |
danielk1977 | 2812956 | 2005-01-11 10:25:06 +0000 | [diff] [blame] | 2964 | if( rc!=SQLITE_OK ){ |
| 2965 | goto abort_due_to_error; |
| 2966 | } |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 2967 | pC->lastRowid = iKey; |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 2968 | pC->rowidIsValid = res==0; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2969 | }else{ |
danielk1977 | b790c6c | 2008-04-18 10:25:24 +0000 | [diff] [blame] | 2970 | UnpackedRecord r; |
| 2971 | int nField = pOp->p4.i; |
| 2972 | assert( pOp->p4type==P4_INT32 ); |
| 2973 | assert( nField>0 ); |
| 2974 | r.pKeyInfo = pC->pKeyInfo; |
| 2975 | r.nField = nField; |
| 2976 | r.needFree = 0; |
| 2977 | r.needDestroy = 0; |
| 2978 | r.aMem = &p->aMem[pOp->p3]; |
| 2979 | rc = sqlite3BtreeMoveto(pC->pCursor, 0, &r, 0, 0, &res); |
danielk1977 | 2812956 | 2005-01-11 10:25:06 +0000 | [diff] [blame] | 2980 | if( rc!=SQLITE_OK ){ |
| 2981 | goto abort_due_to_error; |
| 2982 | } |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 2983 | pC->rowidIsValid = 0; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2984 | } |
drh | a11846b | 2004-01-07 18:52:56 +0000 | [diff] [blame] | 2985 | pC->deferredMoveto = 0; |
drh | 76873ab | 2006-01-07 18:48:26 +0000 | [diff] [blame] | 2986 | pC->cacheStatus = CACHE_STALE; |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 2987 | *pC->pIncrKey = 0; |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 2988 | #ifdef SQLITE_TEST |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 2989 | sqlite3_search_count++; |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 2990 | #endif |
drh | 7cf6e4d | 2004-05-19 14:56:55 +0000 | [diff] [blame] | 2991 | if( oc==OP_MoveGe || oc==OP_MoveGt ){ |
| 2992 | if( res<0 ){ |
danielk1977 | 2812956 | 2005-01-11 10:25:06 +0000 | [diff] [blame] | 2993 | rc = sqlite3BtreeNext(pC->pCursor, &res); |
danielk1977 | 01427a6 | 2005-01-11 13:02:33 +0000 | [diff] [blame] | 2994 | if( rc!=SQLITE_OK ) goto abort_due_to_error; |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 2995 | pC->rowidIsValid = 0; |
drh | 1af3fdb | 2004-07-18 21:33:01 +0000 | [diff] [blame] | 2996 | }else{ |
| 2997 | res = 0; |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 2998 | } |
drh | 7cf6e4d | 2004-05-19 14:56:55 +0000 | [diff] [blame] | 2999 | }else{ |
| 3000 | assert( oc==OP_MoveLt || oc==OP_MoveLe ); |
drh | 1a844c3 | 2002-12-04 22:29:28 +0000 | [diff] [blame] | 3001 | if( res>=0 ){ |
danielk1977 | 01427a6 | 2005-01-11 13:02:33 +0000 | [diff] [blame] | 3002 | rc = sqlite3BtreePrevious(pC->pCursor, &res); |
| 3003 | if( rc!=SQLITE_OK ) goto abort_due_to_error; |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3004 | pC->rowidIsValid = 0; |
drh | 1a844c3 | 2002-12-04 22:29:28 +0000 | [diff] [blame] | 3005 | }else{ |
| 3006 | /* res might be negative because the table is empty. Check to |
| 3007 | ** see if this is the case. |
| 3008 | */ |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 3009 | res = sqlite3BtreeEof(pC->pCursor); |
drh | 1a844c3 | 2002-12-04 22:29:28 +0000 | [diff] [blame] | 3010 | } |
drh | 1af3fdb | 2004-07-18 21:33:01 +0000 | [diff] [blame] | 3011 | } |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 3012 | assert( pOp->p2>0 ); |
drh | 1af3fdb | 2004-07-18 21:33:01 +0000 | [diff] [blame] | 3013 | if( res ){ |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 3014 | pc = pOp->p2 - 1; |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 3015 | } |
danielk1977 | f7b9d66 | 2008-06-23 18:49:43 +0000 | [diff] [blame] | 3016 | }else if( !pC->pseudoTable ){ |
| 3017 | /* This happens when attempting to open the sqlite3_master table |
| 3018 | ** for read access returns SQLITE_EMPTY. In this case always |
| 3019 | ** take the jump (since there are no records in the table). |
| 3020 | */ |
| 3021 | pc = pOp->p2 - 1; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3022 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3023 | break; |
| 3024 | } |
| 3025 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 3026 | /* Opcode: Found P1 P2 P3 * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3027 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 3028 | ** Register P3 holds a blob constructed by MakeRecord. P1 is an index. |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3029 | ** If an entry that matches the value in register p3 exists in P1 then |
| 3030 | ** jump to P2. If the P3 value does not match any entry in P1 |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3031 | ** then fall thru. The P1 cursor is left pointing at the matching entry |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 3032 | ** if it exists. |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3033 | ** |
| 3034 | ** This instruction is used to implement the IN operator where the |
danielk1977 | 9a96b66 | 2007-11-29 17:05:18 +0000 | [diff] [blame] | 3035 | ** left-hand side is a SELECT statement. P1 may be a true index, or it |
| 3036 | ** may be a temporary index that holds the results of the SELECT |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 3037 | ** statement. This instruction is also used to implement the |
| 3038 | ** DISTINCT keyword in SELECT statements. |
danielk1977 | 9a96b66 | 2007-11-29 17:05:18 +0000 | [diff] [blame] | 3039 | ** |
| 3040 | ** This instruction checks if index P1 contains a record for which |
shane | 21e7feb | 2008-05-30 15:59:49 +0000 | [diff] [blame] | 3041 | ** the first N serialized values exactly match the N serialized values |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3042 | ** in the record in register P3, where N is the total number of values in |
| 3043 | ** the P3 record (the P3 record is a prefix of the P1 record). |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 3044 | ** |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 3045 | ** See also: NotFound, MoveTo, IsUnique, NotExists |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3046 | */ |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 3047 | /* Opcode: NotFound P1 P2 P3 * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3048 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 3049 | ** Register P3 holds a blob constructed by MakeRecord. P1 is |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3050 | ** an index. If no entry exists in P1 that matches the blob then jump |
drh | 795ab9b | 2007-01-27 13:37:22 +0000 | [diff] [blame] | 3051 | ** to P2. If an entry does existing, fall through. The cursor is left |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3052 | ** pointing to the entry that matches. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3053 | ** |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3054 | ** See also: Found, MoveTo, NotExists, IsUnique |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3055 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3056 | case OP_NotFound: /* jump, in3 */ |
| 3057 | case OP_Found: { /* jump, in3 */ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3058 | int i = pOp->p1; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3059 | int alreadyExists = 0; |
drh | 80ff32f | 2001-11-04 18:32:46 +0000 | [diff] [blame] | 3060 | Cursor *pC; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3061 | assert( i>=0 && i<p->nCursor ); |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 3062 | assert( p->apCsr[i]!=0 ); |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame] | 3063 | if( (pC = p->apCsr[i])->pCursor!=0 ){ |
danielk1977 | 7751940 | 2007-08-30 11:48:31 +0000 | [diff] [blame] | 3064 | int res; |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3065 | assert( pC->isTable==0 ); |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 3066 | assert( pIn3->flags & MEM_Blob ); |
danielk1977 | 9a96b66 | 2007-11-29 17:05:18 +0000 | [diff] [blame] | 3067 | if( pOp->opcode==OP_Found ){ |
| 3068 | pC->pKeyInfo->prefixIsEqual = 1; |
| 3069 | } |
drh | e14006d | 2008-03-25 17:23:32 +0000 | [diff] [blame] | 3070 | rc = sqlite3BtreeMoveto(pC->pCursor, pIn3->z, 0, pIn3->n, 0, &res); |
danielk1977 | 9a96b66 | 2007-11-29 17:05:18 +0000 | [diff] [blame] | 3071 | pC->pKeyInfo->prefixIsEqual = 0; |
danielk1977 | 7751940 | 2007-08-30 11:48:31 +0000 | [diff] [blame] | 3072 | if( rc!=SQLITE_OK ){ |
| 3073 | break; |
| 3074 | } |
| 3075 | alreadyExists = (res==0); |
drh | a11846b | 2004-01-07 18:52:56 +0000 | [diff] [blame] | 3076 | pC->deferredMoveto = 0; |
drh | 76873ab | 2006-01-07 18:48:26 +0000 | [diff] [blame] | 3077 | pC->cacheStatus = CACHE_STALE; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3078 | } |
| 3079 | if( pOp->opcode==OP_Found ){ |
| 3080 | if( alreadyExists ) pc = pOp->p2 - 1; |
| 3081 | }else{ |
| 3082 | if( !alreadyExists ) pc = pOp->p2 - 1; |
| 3083 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3084 | break; |
| 3085 | } |
| 3086 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 3087 | /* Opcode: IsUnique P1 P2 P3 P4 * |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 3088 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 3089 | ** The P3 register contains an integer record number. Call this |
| 3090 | ** record number R. The P4 register contains an index key created |
| 3091 | ** using MakeIdxRec. Call it K. |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 3092 | ** |
drh | 7cf6e4d | 2004-05-19 14:56:55 +0000 | [diff] [blame] | 3093 | ** 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] | 3094 | ** record generated by OP_MakeRecord where the last field is the |
| 3095 | ** rowid of the entry that the index refers to. |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 3096 | ** |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 3097 | ** This instruction asks if there is an entry in P1 where the |
drh | 7cf6e4d | 2004-05-19 14:56:55 +0000 | [diff] [blame] | 3098 | ** fields matches K but the rowid is different from R. |
| 3099 | ** If there is no such entry, then there is an immediate |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 3100 | ** jump to P2. If any entry does exist where the index string |
| 3101 | ** matches K but the record number is not R, then the record |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 3102 | ** number for that entry is written into P3 and control |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 3103 | ** falls through to the next instruction. |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 3104 | ** |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3105 | ** See also: NotFound, NotExists, Found |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 3106 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3107 | case OP_IsUnique: { /* jump, in3 */ |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 3108 | int i = pOp->p1; |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 3109 | Cursor *pCx; |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 3110 | BtCursor *pCrsr; |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 3111 | Mem *pK; |
danielk1977 | 452c989 | 2004-05-13 05:16:15 +0000 | [diff] [blame] | 3112 | i64 R; |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 3113 | |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 3114 | /* Pop the value R off the top of the stack |
| 3115 | */ |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 3116 | assert( pOp->p4type==P4_INT32 ); |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3117 | assert( pOp->p4.i>0 && pOp->p4.i<=p->nMem ); |
| 3118 | pK = &p->aMem[pOp->p4.i]; |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 3119 | sqlite3VdbeMemIntegerify(pIn3); |
| 3120 | R = pIn3->u.i; |
drh | 73bdf07 | 2006-08-15 14:21:16 +0000 | [diff] [blame] | 3121 | assert( i>=0 && i<p->nCursor ); |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame] | 3122 | pCx = p->apCsr[i]; |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 3123 | assert( pCx!=0 ); |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 3124 | pCrsr = pCx->pCursor; |
| 3125 | if( pCrsr!=0 ){ |
danielk1977 | f2fa831 | 2006-01-24 13:09:33 +0000 | [diff] [blame] | 3126 | int res; |
danielk1977 | 452c989 | 2004-05-13 05:16:15 +0000 | [diff] [blame] | 3127 | i64 v; /* The record number on the P1 entry that matches K */ |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 3128 | char *zKey; /* The value of K */ |
| 3129 | int nKey; /* Number of bytes in K */ |
danielk1977 | 452c989 | 2004-05-13 05:16:15 +0000 | [diff] [blame] | 3130 | int len; /* Number of bytes in K without the rowid at the end */ |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 3131 | int szRowid; /* Size of the rowid column at the end of zKey */ |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 3132 | |
| 3133 | /* Make sure K is a string and make zKey point to K |
| 3134 | */ |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 3135 | assert( pK->flags & MEM_Blob ); |
| 3136 | zKey = pK->z; |
| 3137 | nKey = pK->n; |
danielk1977 | 452c989 | 2004-05-13 05:16:15 +0000 | [diff] [blame] | 3138 | |
drh | 7416170 | 2006-02-24 02:53:49 +0000 | [diff] [blame] | 3139 | szRowid = sqlite3VdbeIdxRowidLen((u8*)zKey); |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 3140 | len = nKey-szRowid; |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 3141 | |
drh | a3b321d | 2004-05-11 09:31:31 +0000 | [diff] [blame] | 3142 | /* 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] | 3143 | ** If there is no such entry, jump immediately to P2. |
| 3144 | */ |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 3145 | assert( pCx->deferredMoveto==0 ); |
drh | 76873ab | 2006-01-07 18:48:26 +0000 | [diff] [blame] | 3146 | pCx->cacheStatus = CACHE_STALE; |
drh | e14006d | 2008-03-25 17:23:32 +0000 | [diff] [blame] | 3147 | rc = sqlite3BtreeMoveto(pCrsr, zKey, 0, len, 0, &res); |
danielk1977 | f011300 | 2006-01-24 12:09:17 +0000 | [diff] [blame] | 3148 | if( rc!=SQLITE_OK ){ |
| 3149 | goto abort_due_to_error; |
| 3150 | } |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 3151 | if( res<0 ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3152 | rc = sqlite3BtreeNext(pCrsr, &res); |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 3153 | if( res ){ |
| 3154 | pc = pOp->p2 - 1; |
| 3155 | break; |
| 3156 | } |
| 3157 | } |
danielk1977 | 751de56 | 2008-04-18 09:01:15 +0000 | [diff] [blame] | 3158 | rc = sqlite3VdbeIdxKeyCompare(pCx, 0, len, (u8*)zKey, &res); |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 3159 | if( rc!=SQLITE_OK ) goto abort_due_to_error; |
| 3160 | if( res>0 ){ |
| 3161 | pc = pOp->p2 - 1; |
| 3162 | break; |
| 3163 | } |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 3164 | |
| 3165 | /* 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] | 3166 | ** the final entry (the rowid) matches K. Check to see if the |
| 3167 | ** final rowid column is different from R. If it equals R then jump |
danielk1977 | 452c989 | 2004-05-13 05:16:15 +0000 | [diff] [blame] | 3168 | ** immediately to P2. |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 3169 | */ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 3170 | rc = sqlite3VdbeIdxRowid(pCrsr, &v); |
danielk1977 | 452c989 | 2004-05-13 05:16:15 +0000 | [diff] [blame] | 3171 | if( rc!=SQLITE_OK ){ |
| 3172 | goto abort_due_to_error; |
| 3173 | } |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 3174 | if( v==R ){ |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 3175 | pc = pOp->p2 - 1; |
| 3176 | break; |
| 3177 | } |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 3178 | |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3179 | /* The final varint of the key is different from R. Store it back |
| 3180 | ** into register R3. (The record number of an entry that violates |
| 3181 | ** a UNIQUE constraint.) |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 3182 | */ |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 3183 | pIn3->u.i = v; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 3184 | assert( pIn3->flags&MEM_Int ); |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 3185 | } |
| 3186 | break; |
| 3187 | } |
| 3188 | |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3189 | /* Opcode: NotExists P1 P2 P3 * * |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 3190 | ** |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3191 | ** Use the content of register P3 as a integer key. If a record |
danielk1977 | 96cb76f | 2008-01-04 13:24:28 +0000 | [diff] [blame] | 3192 | ** with that key does not exist in table of P1, then jump to P2. |
| 3193 | ** If the record does exist, then fall thru. The cursor is left |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3194 | ** pointing to the record if it exists. |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 3195 | ** |
| 3196 | ** The difference between this operation and NotFound is that this |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3197 | ** operation assumes the key is an integer and that P1 is a table whereas |
| 3198 | ** NotFound assumes key is a blob constructed from MakeRecord and |
| 3199 | ** P1 is an index. |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 3200 | ** |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3201 | ** See also: Found, MoveTo, NotFound, IsUnique |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 3202 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3203 | case OP_NotExists: { /* jump, in3 */ |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 3204 | int i = pOp->p1; |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 3205 | Cursor *pC; |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 3206 | BtCursor *pCrsr; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3207 | assert( i>=0 && i<p->nCursor ); |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 3208 | assert( p->apCsr[i]!=0 ); |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame] | 3209 | if( (pCrsr = (pC = p->apCsr[i])->pCursor)!=0 ){ |
danielk1977 | 1400b52 | 2005-01-11 11:08:22 +0000 | [diff] [blame] | 3210 | int res; |
danielk1977 | 36a3c70 | 2004-05-11 06:55:14 +0000 | [diff] [blame] | 3211 | u64 iKey; |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 3212 | assert( pIn3->flags & MEM_Int ); |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3213 | assert( p->apCsr[i]->isTable ); |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 3214 | iKey = intToKey(pIn3->u.i); |
drh | e14006d | 2008-03-25 17:23:32 +0000 | [diff] [blame] | 3215 | rc = sqlite3BtreeMoveto(pCrsr, 0, 0, iKey, 0,&res); |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 3216 | pC->lastRowid = pIn3->u.i; |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3217 | pC->rowidIsValid = res==0; |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 3218 | pC->nullRow = 0; |
drh | 76873ab | 2006-01-07 18:48:26 +0000 | [diff] [blame] | 3219 | pC->cacheStatus = CACHE_STALE; |
drh | 0bc5370 | 2007-01-04 01:20:11 +0000 | [diff] [blame] | 3220 | /* res might be uninitialized if rc!=SQLITE_OK. But if rc!=SQLITE_OK |
| 3221 | ** processing is about to abort so we really do not care whether or not |
drh | c416ba9 | 2007-03-30 18:42:55 +0000 | [diff] [blame] | 3222 | ** the following jump is taken. (In other words, do not stress over |
| 3223 | ** the error that valgrind sometimes shows on the next statement when |
| 3224 | ** running ioerr.test and similar failure-recovery test scripts.) */ |
danielk1977 | 2812956 | 2005-01-11 10:25:06 +0000 | [diff] [blame] | 3225 | if( res!=0 ){ |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 3226 | pc = pOp->p2 - 1; |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 3227 | assert( pC->rowidIsValid==0 ); |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 3228 | } |
danielk1977 | f7b9d66 | 2008-06-23 18:49:43 +0000 | [diff] [blame] | 3229 | }else if( !pC->pseudoTable ){ |
| 3230 | /* This happens when an attempt to open a read cursor on the |
| 3231 | ** sqlite_master table returns SQLITE_EMPTY. |
| 3232 | */ |
| 3233 | assert( pC->isTable ); |
| 3234 | pc = pOp->p2 - 1; |
| 3235 | assert( pC->rowidIsValid==0 ); |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 3236 | } |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 3237 | break; |
| 3238 | } |
| 3239 | |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 3240 | /* Opcode: Sequence P1 P2 * * * |
drh | 4db38a7 | 2005-09-01 12:16:28 +0000 | [diff] [blame] | 3241 | ** |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 3242 | ** Find the next available sequence number for cursor P1. |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3243 | ** Write the sequence number into register P2. |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 3244 | ** The sequence number on the cursor is incremented after this |
| 3245 | ** instruction. |
drh | 4db38a7 | 2005-09-01 12:16:28 +0000 | [diff] [blame] | 3246 | */ |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 3247 | case OP_Sequence: { /* out2-prerelease */ |
drh | 4db38a7 | 2005-09-01 12:16:28 +0000 | [diff] [blame] | 3248 | int i = pOp->p1; |
drh | 4db38a7 | 2005-09-01 12:16:28 +0000 | [diff] [blame] | 3249 | assert( i>=0 && i<p->nCursor ); |
| 3250 | assert( p->apCsr[i]!=0 ); |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 3251 | pOut->u.i = p->apCsr[i]->seqCount++; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 3252 | MemSetTypeFlag(pOut, MEM_Int); |
drh | 4db38a7 | 2005-09-01 12:16:28 +0000 | [diff] [blame] | 3253 | break; |
| 3254 | } |
| 3255 | |
| 3256 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 3257 | /* Opcode: NewRowid P1 P2 P3 * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3258 | ** |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3259 | ** 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] | 3260 | ** The record number is not previously used as a key in the database |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3261 | ** table that cursor P1 points to. The new record number is written |
| 3262 | ** written to register P2. |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 3263 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 3264 | ** If P3>0 then P3 is a register that holds the largest previously |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 3265 | ** generated record number. No new record numbers are allowed to be less |
drh | 2958a4e | 2004-11-12 03:56:15 +0000 | [diff] [blame] | 3266 | ** than this value. When this value reaches its maximum, a SQLITE_FULL |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 3267 | ** error is generated. The P3 register is updated with the generated |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 3268 | ** record number. This P3 mechanism is used to help implement the |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 3269 | ** AUTOINCREMENT feature. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3270 | */ |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 3271 | case OP_NewRowid: { /* out2-prerelease */ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3272 | int i = pOp->p1; |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 3273 | i64 v = 0; |
drh | 80ff32f | 2001-11-04 18:32:46 +0000 | [diff] [blame] | 3274 | Cursor *pC; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3275 | assert( i>=0 && i<p->nCursor ); |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 3276 | assert( p->apCsr[i]!=0 ); |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame] | 3277 | if( (pC = p->apCsr[i])->pCursor==0 ){ |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 3278 | /* The zero initialization above is all that is needed */ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3279 | }else{ |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 3280 | /* The next rowid or record number (different terms for the same |
| 3281 | ** thing) is obtained in a two-step algorithm. |
| 3282 | ** |
| 3283 | ** First we attempt to find the largest existing rowid and add one |
| 3284 | ** to that. But if the largest existing rowid is already the maximum |
| 3285 | ** positive integer, we have to fall through to the second |
| 3286 | ** probabilistic algorithm |
| 3287 | ** |
| 3288 | ** The second algorithm is to select a rowid at random and see if |
| 3289 | ** it already exists in the table. If it does not exist, we have |
| 3290 | ** succeeded. If the random rowid does exist, we select a new one |
| 3291 | ** and try again, up to 1000 times. |
drh | db5ed6d | 2001-09-18 22:17:44 +0000 | [diff] [blame] | 3292 | ** |
| 3293 | ** For a table with less than 2 billion entries, the probability |
| 3294 | ** of not finding a unused rowid is about 1.0e-300. This is a |
| 3295 | ** non-zero probability, but it is still vanishingly small and should |
| 3296 | ** never cause a problem. You are much, much more likely to have a |
| 3297 | ** hardware failure than for this algorithm to fail. |
| 3298 | ** |
drh | af9ff33 | 2002-01-16 21:00:27 +0000 | [diff] [blame] | 3299 | ** The analysis in the previous paragraph assumes that you have a good |
| 3300 | ** source of random numbers. Is a library function like lrand48() |
| 3301 | ** good enough? Maybe. Maybe not. It's hard to know whether there |
| 3302 | ** might be subtle bugs is some implementations of lrand48() that |
| 3303 | ** could cause problems. To avoid uncertainty, SQLite uses its own |
| 3304 | ** random number generator based on the RC4 algorithm. |
| 3305 | ** |
drh | db5ed6d | 2001-09-18 22:17:44 +0000 | [diff] [blame] | 3306 | ** To promote locality of reference for repetitive inserts, the |
shane | 21e7feb | 2008-05-30 15:59:49 +0000 | [diff] [blame] | 3307 | ** first few attempts at choosing a random rowid pick values just a little |
drh | db5ed6d | 2001-09-18 22:17:44 +0000 | [diff] [blame] | 3308 | ** larger than the previous rowid. This has been shown experimentally |
| 3309 | ** to double the speed of the COPY operation. |
| 3310 | */ |
danielk1977 | f7df9cc | 2004-06-16 12:02:47 +0000 | [diff] [blame] | 3311 | int res, rx=SQLITE_OK, cnt; |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 3312 | i64 x; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3313 | cnt = 0; |
drh | 4e6083c | 2005-02-04 21:13:00 +0000 | [diff] [blame] | 3314 | if( (sqlite3BtreeFlags(pC->pCursor)&(BTREE_INTKEY|BTREE_ZERODATA)) != |
| 3315 | BTREE_INTKEY ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 3316 | rc = SQLITE_CORRUPT_BKPT; |
drh | 4e6083c | 2005-02-04 21:13:00 +0000 | [diff] [blame] | 3317 | goto abort_due_to_error; |
| 3318 | } |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 3319 | assert( (sqlite3BtreeFlags(pC->pCursor) & BTREE_INTKEY)!=0 ); |
| 3320 | assert( (sqlite3BtreeFlags(pC->pCursor) & BTREE_ZERODATA)==0 ); |
drh | fe2093d | 2005-01-20 22:48:47 +0000 | [diff] [blame] | 3321 | |
drh | 75f86a4 | 2005-02-17 00:03:06 +0000 | [diff] [blame] | 3322 | #ifdef SQLITE_32BIT_ROWID |
| 3323 | # define MAX_ROWID 0x7fffffff |
| 3324 | #else |
drh | fe2093d | 2005-01-20 22:48:47 +0000 | [diff] [blame] | 3325 | /* Some compilers complain about constants of the form 0x7fffffffffffffff. |
| 3326 | ** Others complain about 0x7ffffffffffffffffLL. The following macro seems |
| 3327 | ** to provide the constant while making all compilers happy. |
| 3328 | */ |
drh | 75f86a4 | 2005-02-17 00:03:06 +0000 | [diff] [blame] | 3329 | # define MAX_ROWID ( (((u64)0x7fffffff)<<32) | (u64)0xffffffff ) |
| 3330 | #endif |
drh | fe2093d | 2005-01-20 22:48:47 +0000 | [diff] [blame] | 3331 | |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 3332 | if( !pC->useRandomRowid ){ |
drh | 32fbe34 | 2002-10-19 20:16:37 +0000 | [diff] [blame] | 3333 | if( pC->nextRowidValid ){ |
| 3334 | v = pC->nextRowid; |
drh | 3fc190c | 2001-09-14 03:24:23 +0000 | [diff] [blame] | 3335 | }else{ |
danielk1977 | 261919c | 2005-12-06 12:52:59 +0000 | [diff] [blame] | 3336 | rc = sqlite3BtreeLast(pC->pCursor, &res); |
| 3337 | if( rc!=SQLITE_OK ){ |
| 3338 | goto abort_due_to_error; |
| 3339 | } |
drh | 32fbe34 | 2002-10-19 20:16:37 +0000 | [diff] [blame] | 3340 | if( res ){ |
| 3341 | v = 1; |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 3342 | }else{ |
danielk1977 | e0d4b06 | 2004-06-28 01:11:46 +0000 | [diff] [blame] | 3343 | sqlite3BtreeKeySize(pC->pCursor, &v); |
drh | 32fbe34 | 2002-10-19 20:16:37 +0000 | [diff] [blame] | 3344 | v = keyToInt(v); |
drh | 75f86a4 | 2005-02-17 00:03:06 +0000 | [diff] [blame] | 3345 | if( v==MAX_ROWID ){ |
drh | 32fbe34 | 2002-10-19 20:16:37 +0000 | [diff] [blame] | 3346 | pC->useRandomRowid = 1; |
| 3347 | }else{ |
| 3348 | v++; |
| 3349 | } |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 3350 | } |
drh | 3fc190c | 2001-09-14 03:24:23 +0000 | [diff] [blame] | 3351 | } |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 3352 | |
| 3353 | #ifndef SQLITE_OMIT_AUTOINCREMENT |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 3354 | if( pOp->p3 ){ |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 3355 | Mem *pMem; |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 3356 | assert( pOp->p3>0 && pOp->p3<=p->nMem ); /* P3 is a valid memory cell */ |
| 3357 | pMem = &p->aMem[pOp->p3]; |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 3358 | REGISTER_TRACE(pOp->p3, pMem); |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 3359 | sqlite3VdbeMemIntegerify(pMem); |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 3360 | assert( (pMem->flags & MEM_Int)!=0 ); /* mem(P3) holds an integer */ |
drh | 3c024d6 | 2007-03-30 11:23:45 +0000 | [diff] [blame] | 3361 | if( pMem->u.i==MAX_ROWID || pC->useRandomRowid ){ |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 3362 | rc = SQLITE_FULL; |
| 3363 | goto abort_due_to_error; |
| 3364 | } |
drh | 3c024d6 | 2007-03-30 11:23:45 +0000 | [diff] [blame] | 3365 | if( v<pMem->u.i+1 ){ |
| 3366 | v = pMem->u.i + 1; |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 3367 | } |
drh | 3c024d6 | 2007-03-30 11:23:45 +0000 | [diff] [blame] | 3368 | pMem->u.i = v; |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 3369 | } |
| 3370 | #endif |
| 3371 | |
drh | 75f86a4 | 2005-02-17 00:03:06 +0000 | [diff] [blame] | 3372 | if( v<MAX_ROWID ){ |
drh | 32fbe34 | 2002-10-19 20:16:37 +0000 | [diff] [blame] | 3373 | pC->nextRowidValid = 1; |
| 3374 | pC->nextRowid = v+1; |
| 3375 | }else{ |
| 3376 | pC->nextRowidValid = 0; |
| 3377 | } |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 3378 | } |
| 3379 | if( pC->useRandomRowid ){ |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 3380 | assert( pOp->p3==0 ); /* SQLITE_FULL must have occurred prior to this */ |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 3381 | v = db->priorNewRowid; |
| 3382 | cnt = 0; |
| 3383 | do{ |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 3384 | if( cnt==0 && (v&0xffffff)==v ){ |
| 3385 | v++; |
| 3386 | }else{ |
drh | 2fa1868 | 2008-03-19 14:15:34 +0000 | [diff] [blame] | 3387 | sqlite3_randomness(sizeof(v), &v); |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 3388 | if( cnt<5 ) v &= 0xffffff; |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 3389 | } |
| 3390 | if( v==0 ) continue; |
| 3391 | x = intToKey(v); |
drh | e14006d | 2008-03-25 17:23:32 +0000 | [diff] [blame] | 3392 | rx = sqlite3BtreeMoveto(pC->pCursor, 0, 0, (u64)x, 0, &res); |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 3393 | cnt++; |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 3394 | }while( cnt<100 && rx==SQLITE_OK && res==0 ); |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 3395 | db->priorNewRowid = v; |
| 3396 | if( rx==SQLITE_OK && res==0 ){ |
| 3397 | rc = SQLITE_FULL; |
| 3398 | goto abort_due_to_error; |
| 3399 | } |
drh | 1eaa269 | 2001-09-18 02:02:23 +0000 | [diff] [blame] | 3400 | } |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3401 | pC->rowidIsValid = 0; |
drh | a11846b | 2004-01-07 18:52:56 +0000 | [diff] [blame] | 3402 | pC->deferredMoveto = 0; |
drh | 76873ab | 2006-01-07 18:48:26 +0000 | [diff] [blame] | 3403 | pC->cacheStatus = CACHE_STALE; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3404 | } |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 3405 | MemSetTypeFlag(pOut, MEM_Int); |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 3406 | pOut->u.i = v; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3407 | break; |
| 3408 | } |
| 3409 | |
danielk1977 | 1f4aa33 | 2008-01-03 09:51:55 +0000 | [diff] [blame] | 3410 | /* Opcode: Insert P1 P2 P3 P4 P5 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3411 | ** |
jplyon | 5a56422 | 2003-06-02 06:15:58 +0000 | [diff] [blame] | 3412 | ** Write an entry into the table of cursor P1. A new entry is |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 3413 | ** created if it doesn't already exist or the data for an existing |
danielk1977 | 1f4aa33 | 2008-01-03 09:51:55 +0000 | [diff] [blame] | 3414 | ** entry is overwritten. The data is the value stored register |
| 3415 | ** number P2. The key is stored in register P3. The key must |
| 3416 | ** be an integer. |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 3417 | ** |
danielk1977 | 1f4aa33 | 2008-01-03 09:51:55 +0000 | [diff] [blame] | 3418 | ** If the OPFLAG_NCHANGE flag of P5 is set, then the row change count is |
| 3419 | ** incremented (otherwise not). If the OPFLAG_LASTROWID flag of P5 is set, |
danielk1977 | b28af71 | 2004-06-21 06:50:26 +0000 | [diff] [blame] | 3420 | ** then rowid is stored for subsequent return by the |
drh | 85b623f | 2007-12-13 21:54:09 +0000 | [diff] [blame] | 3421 | ** sqlite3_last_insert_rowid() function (otherwise it is unmodified). |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 3422 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 3423 | ** Parameter P4 may point to a string containing the table-name, or |
danielk1977 | 1f6eec5 | 2006-06-16 06:17:47 +0000 | [diff] [blame] | 3424 | ** may be NULL. If it is not NULL, then the update-hook |
| 3425 | ** (sqlite3.xUpdateCallback) is invoked following a successful insert. |
| 3426 | ** |
drh | 93aed5a | 2008-01-16 17:46:38 +0000 | [diff] [blame] | 3427 | ** (WARNING/TODO: If P1 is a pseudo-cursor and P2 is dynamically |
| 3428 | ** allocated, then ownership of P2 is transferred to the pseudo-cursor |
| 3429 | ** and register P2 becomes ephemeral. If the cursor is changed, the |
| 3430 | ** value of register P2 will then change. Make sure this does not |
| 3431 | ** cause any problems.) |
| 3432 | ** |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3433 | ** This instruction only works on tables. The equivalent instruction |
| 3434 | ** for indices is OP_IdxInsert. |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 3435 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3436 | case OP_Insert: { |
danielk1977 | 1f4aa33 | 2008-01-03 09:51:55 +0000 | [diff] [blame] | 3437 | Mem *pData = &p->aMem[pOp->p2]; |
| 3438 | Mem *pKey = &p->aMem[pOp->p3]; |
| 3439 | |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 3440 | i64 iKey; /* The integer ROWID or key for the record to be inserted */ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3441 | int i = pOp->p1; |
drh | 32fbe34 | 2002-10-19 20:16:37 +0000 | [diff] [blame] | 3442 | Cursor *pC; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3443 | assert( i>=0 && i<p->nCursor ); |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 3444 | pC = p->apCsr[i]; |
| 3445 | assert( pC!=0 ); |
| 3446 | assert( pC->pCursor!=0 || pC->pseudoTable ); |
| 3447 | assert( pKey->flags & MEM_Int ); |
| 3448 | assert( pC->isTable ); |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 3449 | REGISTER_TRACE(pOp->p2, pData); |
| 3450 | REGISTER_TRACE(pOp->p3, pKey); |
danielk1977 | 5f8d8a8 | 2004-05-11 00:28:42 +0000 | [diff] [blame] | 3451 | |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 3452 | iKey = intToKey(pKey->u.i); |
| 3453 | if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++; |
| 3454 | if( pOp->p5 & OPFLAG_LASTROWID ) db->lastRowid = pKey->u.i; |
| 3455 | if( pC->nextRowidValid && pKey->u.i>=pC->nextRowid ){ |
| 3456 | pC->nextRowidValid = 0; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3457 | } |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 3458 | if( pData->flags & MEM_Null ){ |
| 3459 | pData->z = 0; |
| 3460 | pData->n = 0; |
| 3461 | }else{ |
| 3462 | assert( pData->flags & (MEM_Blob|MEM_Str) ); |
| 3463 | } |
| 3464 | if( pC->pseudoTable ){ |
danielk1977 | 9882d99 | 2008-03-27 17:59:01 +0000 | [diff] [blame] | 3465 | if( !pC->ephemPseudoTable ){ |
| 3466 | sqlite3_free(pC->pData); |
| 3467 | } |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 3468 | pC->iKey = iKey; |
| 3469 | pC->nData = pData->n; |
danielk1977 | 5f09613 | 2008-03-28 15:44:09 +0000 | [diff] [blame] | 3470 | if( pData->z==pData->zMalloc || pC->ephemPseudoTable ){ |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 3471 | pC->pData = pData->z; |
danielk1977 | 9882d99 | 2008-03-27 17:59:01 +0000 | [diff] [blame] | 3472 | if( !pC->ephemPseudoTable ){ |
| 3473 | pData->flags &= ~MEM_Dyn; |
| 3474 | pData->flags |= MEM_Ephem; |
danielk1977 | 5f09613 | 2008-03-28 15:44:09 +0000 | [diff] [blame] | 3475 | pData->zMalloc = 0; |
danielk1977 | 9882d99 | 2008-03-27 17:59:01 +0000 | [diff] [blame] | 3476 | } |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 3477 | }else{ |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 3478 | pC->pData = sqlite3Malloc( pC->nData+2 ); |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 3479 | if( !pC->pData ) goto no_mem; |
| 3480 | memcpy(pC->pData, pData->z, pC->nData); |
| 3481 | pC->pData[pC->nData] = 0; |
| 3482 | pC->pData[pC->nData+1] = 0; |
| 3483 | } |
| 3484 | pC->nullRow = 0; |
| 3485 | }else{ |
| 3486 | int nZero; |
| 3487 | if( pData->flags & MEM_Zero ){ |
| 3488 | nZero = pData->u.i; |
| 3489 | }else{ |
| 3490 | nZero = 0; |
| 3491 | } |
| 3492 | rc = sqlite3BtreeInsert(pC->pCursor, 0, iKey, |
| 3493 | pData->z, pData->n, nZero, |
| 3494 | pOp->p5 & OPFLAG_APPEND); |
| 3495 | } |
| 3496 | |
| 3497 | pC->rowidIsValid = 0; |
| 3498 | pC->deferredMoveto = 0; |
| 3499 | pC->cacheStatus = CACHE_STALE; |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 +0000 | [diff] [blame] | 3500 | |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 3501 | /* Invoke the update-hook if required. */ |
| 3502 | if( rc==SQLITE_OK && db->xUpdateCallback && pOp->p4.z ){ |
| 3503 | const char *zDb = db->aDb[pC->iDb].zName; |
| 3504 | const char *zTbl = pOp->p4.z; |
| 3505 | int op = ((pOp->p5 & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_INSERT); |
| 3506 | assert( pC->isTable ); |
| 3507 | db->xUpdateCallback(db->pUpdateArg, op, zDb, zTbl, iKey); |
| 3508 | assert( pC->iDb>=0 ); |
| 3509 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3510 | break; |
| 3511 | } |
| 3512 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 3513 | /* Opcode: Delete P1 P2 * P4 * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3514 | ** |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 3515 | ** Delete the record at which the P1 cursor is currently pointing. |
| 3516 | ** |
| 3517 | ** The cursor will be left pointing at either the next or the previous |
| 3518 | ** 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] | 3519 | ** the next Next instruction will be a no-op. Hence it is OK to delete |
| 3520 | ** a record from within an Next loop. |
drh | c8d30ac | 2002-04-12 10:08:59 +0000 | [diff] [blame] | 3521 | ** |
rdc | b0c374f | 2004-02-20 22:53:38 +0000 | [diff] [blame] | 3522 | ** 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] | 3523 | ** incremented (otherwise not). |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3524 | ** |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 3525 | ** P1 must not be pseudo-table. It has to be a real table with |
| 3526 | ** multiple rows. |
| 3527 | ** |
| 3528 | ** If P4 is not NULL, then it is the name of the table that P1 is |
| 3529 | ** pointing to. The update hook will be invoked, if it exists. |
| 3530 | ** If P4 is not NULL then the P1 cursor must have been positioned |
| 3531 | ** using OP_NotFound prior to invoking this opcode. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3532 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3533 | case OP_Delete: { |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3534 | int i = pOp->p1; |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 3535 | i64 iKey; |
drh | 32fbe34 | 2002-10-19 20:16:37 +0000 | [diff] [blame] | 3536 | Cursor *pC; |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 3537 | |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3538 | assert( i>=0 && i<p->nCursor ); |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame] | 3539 | pC = p->apCsr[i]; |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 3540 | assert( pC!=0 ); |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 3541 | assert( pC->pCursor!=0 ); /* Only valid for real tables, no pseudotables */ |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 +0000 | [diff] [blame] | 3542 | |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 3543 | /* If the update-hook will be invoked, set iKey to the rowid of the |
| 3544 | ** row being deleted. |
| 3545 | */ |
| 3546 | if( db->xUpdateCallback && pOp->p4.z ){ |
| 3547 | assert( pC->isTable ); |
| 3548 | assert( pC->rowidIsValid ); /* lastRowid set by previous OP_NotFound */ |
| 3549 | iKey = pC->lastRowid; |
| 3550 | } |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 +0000 | [diff] [blame] | 3551 | |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 3552 | rc = sqlite3VdbeCursorMoveto(pC); |
| 3553 | if( rc ) goto abort_due_to_error; |
| 3554 | rc = sqlite3BtreeDelete(pC->pCursor); |
| 3555 | pC->nextRowidValid = 0; |
| 3556 | pC->cacheStatus = CACHE_STALE; |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 +0000 | [diff] [blame] | 3557 | |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 3558 | /* Invoke the update-hook if required. */ |
| 3559 | if( rc==SQLITE_OK && db->xUpdateCallback && pOp->p4.z ){ |
| 3560 | const char *zDb = db->aDb[pC->iDb].zName; |
| 3561 | const char *zTbl = pOp->p4.z; |
| 3562 | db->xUpdateCallback(db->pUpdateArg, SQLITE_DELETE, zDb, zTbl, iKey); |
| 3563 | assert( pC->iDb>=0 ); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3564 | } |
danielk1977 | b28af71 | 2004-06-21 06:50:26 +0000 | [diff] [blame] | 3565 | if( pOp->p2 & OPFLAG_NCHANGE ) p->nChange++; |
rdc | b0c374f | 2004-02-20 22:53:38 +0000 | [diff] [blame] | 3566 | break; |
| 3567 | } |
| 3568 | |
danielk1977 | b28af71 | 2004-06-21 06:50:26 +0000 | [diff] [blame] | 3569 | /* Opcode: ResetCount P1 * * |
rdc | b0c374f | 2004-02-20 22:53:38 +0000 | [diff] [blame] | 3570 | ** |
danielk1977 | b28af71 | 2004-06-21 06:50:26 +0000 | [diff] [blame] | 3571 | ** This opcode resets the VMs internal change counter to 0. If P1 is true, |
| 3572 | ** then the value of the change counter is copied to the database handle |
| 3573 | ** change counter (returned by subsequent calls to sqlite3_changes()) |
| 3574 | ** before it is reset. This is used by trigger programs. |
rdc | b0c374f | 2004-02-20 22:53:38 +0000 | [diff] [blame] | 3575 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3576 | case OP_ResetCount: { |
danielk1977 | b28af71 | 2004-06-21 06:50:26 +0000 | [diff] [blame] | 3577 | if( pOp->p1 ){ |
drh | 344737f | 2004-09-19 00:50:20 +0000 | [diff] [blame] | 3578 | sqlite3VdbeSetChanges(db, p->nChange); |
danielk1977 | b28af71 | 2004-06-21 06:50:26 +0000 | [diff] [blame] | 3579 | } |
| 3580 | p->nChange = 0; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3581 | break; |
| 3582 | } |
| 3583 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 3584 | /* Opcode: RowData P1 P2 * * * |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3585 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 3586 | ** Write into register P2 the complete row data for cursor P1. |
| 3587 | ** There is no interpretation of the data. |
| 3588 | ** It is just copied onto the P2 register exactly as |
danielk1977 | 96cb76f | 2008-01-04 13:24:28 +0000 | [diff] [blame] | 3589 | ** it is found in the database file. |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3590 | ** |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 3591 | ** If the P1 cursor must be pointing to a valid row (not a NULL row) |
| 3592 | ** of a real table, not a pseudo-table. |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3593 | */ |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 3594 | /* Opcode: RowKey P1 P2 * * * |
drh | 143f3c4 | 2004-01-07 20:37:52 +0000 | [diff] [blame] | 3595 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 3596 | ** Write into register P2 the complete row key for cursor P1. |
| 3597 | ** There is no interpretation of the data. |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3598 | ** The key is copied onto the P3 register exactly as |
danielk1977 | 96cb76f | 2008-01-04 13:24:28 +0000 | [diff] [blame] | 3599 | ** it is found in the database file. |
drh | 143f3c4 | 2004-01-07 20:37:52 +0000 | [diff] [blame] | 3600 | ** |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 3601 | ** If the P1 cursor must be pointing to a valid row (not a NULL row) |
| 3602 | ** of a real table, not a pseudo-table. |
drh | 143f3c4 | 2004-01-07 20:37:52 +0000 | [diff] [blame] | 3603 | */ |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 3604 | case OP_RowKey: |
| 3605 | case OP_RowData: { |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3606 | int i = pOp->p1; |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3607 | Cursor *pC; |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 3608 | BtCursor *pCrsr; |
danielk1977 | e0d4b06 | 2004-06-28 01:11:46 +0000 | [diff] [blame] | 3609 | u32 n; |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3610 | |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 3611 | pOut = &p->aMem[pOp->p2]; |
| 3612 | |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3613 | /* Note that RowKey and RowData are really exactly the same instruction */ |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3614 | assert( i>=0 && i<p->nCursor ); |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame] | 3615 | pC = p->apCsr[i]; |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3616 | assert( pC->isTable || pOp->opcode==OP_RowKey ); |
| 3617 | assert( pC->isIndex || pOp->opcode==OP_RowData ); |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 3618 | assert( pC!=0 ); |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 3619 | assert( pC->nullRow==0 ); |
| 3620 | assert( pC->pseudoTable==0 ); |
| 3621 | assert( pC->pCursor!=0 ); |
| 3622 | pCrsr = pC->pCursor; |
| 3623 | rc = sqlite3VdbeCursorMoveto(pC); |
| 3624 | if( rc ) goto abort_due_to_error; |
| 3625 | if( pC->isIndex ){ |
| 3626 | i64 n64; |
| 3627 | assert( !pC->isTable ); |
| 3628 | sqlite3BtreeKeySize(pCrsr, &n64); |
drh | bb4957f | 2008-03-20 14:03:29 +0000 | [diff] [blame] | 3629 | if( n64>db->aLimit[SQLITE_LIMIT_LENGTH] ){ |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 3630 | goto too_big; |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3631 | } |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 3632 | n = n64; |
| 3633 | }else{ |
| 3634 | sqlite3BtreeDataSize(pCrsr, &n); |
drh | bb4957f | 2008-03-20 14:03:29 +0000 | [diff] [blame] | 3635 | if( n>db->aLimit[SQLITE_LIMIT_LENGTH] ){ |
drh | 023ae03 | 2007-05-08 12:12:16 +0000 | [diff] [blame] | 3636 | goto too_big; |
| 3637 | } |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 3638 | } |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 3639 | if( sqlite3VdbeMemGrow(pOut, n, 0) ){ |
| 3640 | goto no_mem; |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 3641 | } |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 3642 | pOut->n = n; |
| 3643 | MemSetTypeFlag(pOut, MEM_Blob); |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 3644 | if( pC->isIndex ){ |
| 3645 | rc = sqlite3BtreeKey(pCrsr, 0, n, pOut->z); |
| 3646 | }else{ |
| 3647 | rc = sqlite3BtreeData(pCrsr, 0, n, pOut->z); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3648 | } |
danielk1977 | 96cb76f | 2008-01-04 13:24:28 +0000 | [diff] [blame] | 3649 | pOut->enc = SQLITE_UTF8; /* In case the blob is ever cast to text */ |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 3650 | UPDATE_MAX_BLOBSIZE(pOut); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3651 | break; |
| 3652 | } |
| 3653 | |
drh | 2133d82 | 2008-01-03 18:44:59 +0000 | [diff] [blame] | 3654 | /* Opcode: Rowid P1 P2 * * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3655 | ** |
drh | 2133d82 | 2008-01-03 18:44:59 +0000 | [diff] [blame] | 3656 | ** Store in register P2 an integer which is the key of the table entry that |
drh | bfdc754 | 2008-05-29 03:12:54 +0000 | [diff] [blame] | 3657 | ** P1 is currently point to. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3658 | */ |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 3659 | case OP_Rowid: { /* out2-prerelease */ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3660 | int i = pOp->p1; |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3661 | Cursor *pC; |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 3662 | i64 v; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3663 | |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3664 | assert( i>=0 && i<p->nCursor ); |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame] | 3665 | pC = p->apCsr[i]; |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 3666 | assert( pC!=0 ); |
drh | 536065a | 2005-01-26 21:55:31 +0000 | [diff] [blame] | 3667 | rc = sqlite3VdbeCursorMoveto(pC); |
drh | 52f159e | 2005-01-27 00:33:21 +0000 | [diff] [blame] | 3668 | if( rc ) goto abort_due_to_error; |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3669 | if( pC->rowidIsValid ){ |
| 3670 | v = pC->lastRowid; |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3671 | }else if( pC->pseudoTable ){ |
| 3672 | v = keyToInt(pC->iKey); |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 3673 | }else if( pC->nullRow ){ |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 3674 | /* Leave the rowid set to a NULL */ |
drh | d60ccc6 | 2003-06-24 10:39:46 +0000 | [diff] [blame] | 3675 | break; |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3676 | }else{ |
| 3677 | assert( pC->pCursor!=0 ); |
danielk1977 | e0d4b06 | 2004-06-28 01:11:46 +0000 | [diff] [blame] | 3678 | sqlite3BtreeKeySize(pC->pCursor, &v); |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3679 | v = keyToInt(v); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3680 | } |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 3681 | pOut->u.i = v; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 3682 | MemSetTypeFlag(pOut, MEM_Int); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3683 | break; |
| 3684 | } |
| 3685 | |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3686 | /* Opcode: NullRow P1 * * * * |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 3687 | ** |
| 3688 | ** Move the cursor P1 to a null row. Any OP_Column operations |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3689 | ** that occur while the cursor is on the null row will always |
| 3690 | ** write a NULL. |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 3691 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3692 | case OP_NullRow: { |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 3693 | int i = pOp->p1; |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame] | 3694 | Cursor *pC; |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 3695 | |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3696 | assert( i>=0 && i<p->nCursor ); |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame] | 3697 | pC = p->apCsr[i]; |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 3698 | assert( pC!=0 ); |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame] | 3699 | pC->nullRow = 1; |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3700 | pC->rowidIsValid = 0; |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 3701 | break; |
| 3702 | } |
| 3703 | |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3704 | /* Opcode: Last P1 P2 * * * |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 3705 | ** |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3706 | ** The next use of the Rowid or Column or Next instruction for P1 |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 3707 | ** will refer to the last entry in the database table or index. |
| 3708 | ** If the table or index is empty and P2>0, then jump immediately to P2. |
| 3709 | ** If P2 is 0 or if the table or index is not empty, fall through |
| 3710 | ** to the following instruction. |
| 3711 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3712 | case OP_Last: { /* jump */ |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 3713 | int i = pOp->p1; |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3714 | Cursor *pC; |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 3715 | BtCursor *pCrsr; |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 3716 | int res; |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 3717 | |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3718 | assert( i>=0 && i<p->nCursor ); |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame] | 3719 | pC = p->apCsr[i]; |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 3720 | assert( pC!=0 ); |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 3721 | pCrsr = pC->pCursor; |
| 3722 | assert( pCrsr!=0 ); |
| 3723 | rc = sqlite3BtreeLast(pCrsr, &res); |
| 3724 | pC->nullRow = res; |
| 3725 | pC->deferredMoveto = 0; |
| 3726 | pC->cacheStatus = CACHE_STALE; |
| 3727 | if( res && pOp->p2>0 ){ |
| 3728 | pc = pOp->p2 - 1; |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 3729 | } |
| 3730 | break; |
| 3731 | } |
| 3732 | |
drh | 0342b1f | 2005-09-01 03:07:44 +0000 | [diff] [blame] | 3733 | |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3734 | /* Opcode: Sort P1 P2 * * * |
drh | 0342b1f | 2005-09-01 03:07:44 +0000 | [diff] [blame] | 3735 | ** |
| 3736 | ** This opcode does exactly the same thing as OP_Rewind except that |
| 3737 | ** it increments an undocumented global variable used for testing. |
| 3738 | ** |
| 3739 | ** Sorting is accomplished by writing records into a sorting index, |
| 3740 | ** then rewinding that index and playing it back from beginning to |
| 3741 | ** end. We use the OP_Sort opcode instead of OP_Rewind to do the |
| 3742 | ** rewinding so that the global variable will be incremented and |
| 3743 | ** regression tests can determine whether or not the optimizer is |
| 3744 | ** correctly optimizing out sorts. |
| 3745 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3746 | case OP_Sort: { /* jump */ |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 3747 | #ifdef SQLITE_TEST |
drh | 0342b1f | 2005-09-01 03:07:44 +0000 | [diff] [blame] | 3748 | sqlite3_sort_count++; |
drh | 4db38a7 | 2005-09-01 12:16:28 +0000 | [diff] [blame] | 3749 | sqlite3_search_count--; |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 3750 | #endif |
drh | 0342b1f | 2005-09-01 03:07:44 +0000 | [diff] [blame] | 3751 | /* Fall through into OP_Rewind */ |
| 3752 | } |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3753 | /* Opcode: Rewind P1 P2 * * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3754 | ** |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3755 | ** The next use of the Rowid or Column or Next instruction for P1 |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 3756 | ** will refer to the first entry in the database table or index. |
| 3757 | ** If the table or index is empty and P2>0, then jump immediately to P2. |
| 3758 | ** If P2 is 0 or if the table or index is not empty, fall through |
| 3759 | ** to the following instruction. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3760 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3761 | case OP_Rewind: { /* jump */ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3762 | int i = pOp->p1; |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3763 | Cursor *pC; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3764 | BtCursor *pCrsr; |
drh | f4dada7 | 2004-05-11 09:57:35 +0000 | [diff] [blame] | 3765 | int res; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3766 | |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3767 | assert( i>=0 && i<p->nCursor ); |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame] | 3768 | pC = p->apCsr[i]; |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 3769 | assert( pC!=0 ); |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3770 | if( (pCrsr = pC->pCursor)!=0 ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3771 | rc = sqlite3BtreeFirst(pCrsr, &res); |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3772 | pC->atFirst = res==0; |
drh | a11846b | 2004-01-07 18:52:56 +0000 | [diff] [blame] | 3773 | pC->deferredMoveto = 0; |
drh | 76873ab | 2006-01-07 18:48:26 +0000 | [diff] [blame] | 3774 | pC->cacheStatus = CACHE_STALE; |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3775 | }else{ |
drh | f4dada7 | 2004-05-11 09:57:35 +0000 | [diff] [blame] | 3776 | res = 1; |
| 3777 | } |
| 3778 | pC->nullRow = res; |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 3779 | assert( pOp->p2>0 && pOp->p2<p->nOp ); |
| 3780 | if( res ){ |
drh | f4dada7 | 2004-05-11 09:57:35 +0000 | [diff] [blame] | 3781 | pc = pOp->p2 - 1; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3782 | } |
| 3783 | break; |
| 3784 | } |
| 3785 | |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3786 | /* Opcode: Next P1 P2 * * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3787 | ** |
| 3788 | ** 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] | 3789 | ** table or index. If there are no more key/value pairs then fall through |
| 3790 | ** to the following instruction. But if the cursor advance was successful, |
| 3791 | ** jump immediately to P2. |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 3792 | ** |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 3793 | ** The P1 cursor must be for a real table, not a pseudo-table. |
| 3794 | ** |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 3795 | ** See also: Prev |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 3796 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3797 | /* Opcode: Prev P1 P2 * * * |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 3798 | ** |
| 3799 | ** Back up cursor P1 so that it points to the previous key/data pair in its |
| 3800 | ** table or index. If there is no previous key/value pairs then fall through |
| 3801 | ** to the following instruction. But if the cursor backup was successful, |
| 3802 | ** jump immediately to P2. |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 3803 | ** |
| 3804 | ** The P1 cursor must be for a real table, not a pseudo-table. |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 3805 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3806 | case OP_Prev: /* jump */ |
| 3807 | case OP_Next: { /* jump */ |
drh | 7b39686 | 2003-01-01 23:06:20 +0000 | [diff] [blame] | 3808 | Cursor *pC; |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 3809 | BtCursor *pCrsr; |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame^] | 3810 | int res; |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 3811 | |
drh | caec2f1 | 2003-01-07 02:47:47 +0000 | [diff] [blame] | 3812 | CHECK_FOR_INTERRUPT; |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3813 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame] | 3814 | pC = p->apCsr[pOp->p1]; |
drh | 72e8fa4 | 2007-03-28 14:30:06 +0000 | [diff] [blame] | 3815 | if( pC==0 ){ |
| 3816 | break; /* See ticket #2273 */ |
| 3817 | } |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 3818 | pCrsr = pC->pCursor; |
| 3819 | assert( pCrsr ); |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame^] | 3820 | res = 1; |
| 3821 | assert( pC->deferredMoveto==0 ); |
| 3822 | rc = pOp->opcode==OP_Next ? sqlite3BtreeNext(pCrsr, &res) : |
| 3823 | sqlite3BtreePrevious(pCrsr, &res); |
| 3824 | pC->nullRow = res; |
| 3825 | pC->cacheStatus = CACHE_STALE; |
| 3826 | if( res==0 ){ |
| 3827 | pc = pOp->p2 - 1; |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 3828 | #ifdef SQLITE_TEST |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame^] | 3829 | sqlite3_search_count++; |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 3830 | #endif |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 3831 | } |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3832 | pC->rowidIsValid = 0; |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 3833 | break; |
| 3834 | } |
| 3835 | |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3836 | /* Opcode: IdxInsert P1 P2 P3 * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3837 | ** |
drh | aa9b896 | 2008-01-08 02:57:55 +0000 | [diff] [blame] | 3838 | ** Register P2 holds a SQL index key made using the |
| 3839 | ** MakeIdxRec instructions. This opcode writes that key |
drh | ee32e0a | 2006-01-10 19:45:49 +0000 | [diff] [blame] | 3840 | ** into the index P1. Data for the entry is nil. |
drh | 717e640 | 2001-09-27 03:22:32 +0000 | [diff] [blame] | 3841 | ** |
drh | aa9b896 | 2008-01-08 02:57:55 +0000 | [diff] [blame] | 3842 | ** P3 is a flag that provides a hint to the b-tree layer that this |
drh | e4d9081 | 2007-03-29 05:51:49 +0000 | [diff] [blame] | 3843 | ** insert is likely to be an append. |
| 3844 | ** |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3845 | ** This instruction only works for indices. The equivalent instruction |
| 3846 | ** for tables is OP_Insert. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3847 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3848 | case OP_IdxInsert: { /* in2 */ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3849 | int i = pOp->p1; |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 3850 | Cursor *pC; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3851 | BtCursor *pCrsr; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3852 | assert( i>=0 && i<p->nCursor ); |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 3853 | assert( p->apCsr[i]!=0 ); |
drh | aa9b896 | 2008-01-08 02:57:55 +0000 | [diff] [blame] | 3854 | assert( pIn2->flags & MEM_Blob ); |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame] | 3855 | if( (pCrsr = (pC = p->apCsr[i])->pCursor)!=0 ){ |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3856 | assert( pC->isTable==0 ); |
drh | aa9b896 | 2008-01-08 02:57:55 +0000 | [diff] [blame] | 3857 | rc = ExpandBlob(pIn2); |
danielk1977 | d908f5a | 2007-05-11 07:08:28 +0000 | [diff] [blame] | 3858 | if( rc==SQLITE_OK ){ |
drh | aa9b896 | 2008-01-08 02:57:55 +0000 | [diff] [blame] | 3859 | int nKey = pIn2->n; |
| 3860 | const char *zKey = pIn2->z; |
| 3861 | rc = sqlite3BtreeInsert(pCrsr, zKey, nKey, "", 0, 0, pOp->p3); |
danielk1977 | d908f5a | 2007-05-11 07:08:28 +0000 | [diff] [blame] | 3862 | assert( pC->deferredMoveto==0 ); |
| 3863 | pC->cacheStatus = CACHE_STALE; |
| 3864 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3865 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3866 | break; |
| 3867 | } |
| 3868 | |
drh | e14006d | 2008-03-25 17:23:32 +0000 | [diff] [blame] | 3869 | /* Opcode: IdxDeleteM P1 P2 P3 * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3870 | ** |
drh | e14006d | 2008-03-25 17:23:32 +0000 | [diff] [blame] | 3871 | ** The content of P3 registers starting at register P2 form |
| 3872 | ** an unpacked index key. This opcode removes that entry from the |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 3873 | ** index opened by cursor P1. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3874 | */ |
drh | e14006d | 2008-03-25 17:23:32 +0000 | [diff] [blame] | 3875 | case OP_IdxDelete: { |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3876 | int i = pOp->p1; |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 3877 | Cursor *pC; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3878 | BtCursor *pCrsr; |
drh | e14006d | 2008-03-25 17:23:32 +0000 | [diff] [blame] | 3879 | assert( pOp->p3>0 ); |
| 3880 | assert( pOp->p2>0 && pOp->p2+pOp->p3<=p->nMem ); |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3881 | assert( i>=0 && i<p->nCursor ); |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 3882 | assert( p->apCsr[i]!=0 ); |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame] | 3883 | if( (pCrsr = (pC = p->apCsr[i])->pCursor)!=0 ){ |
danielk1977 | 75bab7d | 2006-01-23 13:09:45 +0000 | [diff] [blame] | 3884 | int res; |
drh | e14006d | 2008-03-25 17:23:32 +0000 | [diff] [blame] | 3885 | UnpackedRecord r; |
| 3886 | r.pKeyInfo = pC->pKeyInfo; |
| 3887 | r.nField = pOp->p3; |
| 3888 | r.needFree = 0; |
| 3889 | r.needDestroy = 0; |
| 3890 | r.aMem = &p->aMem[pOp->p2]; |
| 3891 | rc = sqlite3BtreeMoveto(pCrsr, 0, &r, 0, 0, &res); |
danielk1977 | 75bab7d | 2006-01-23 13:09:45 +0000 | [diff] [blame] | 3892 | if( rc==SQLITE_OK && res==0 ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3893 | rc = sqlite3BtreeDelete(pCrsr); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3894 | } |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 3895 | assert( pC->deferredMoveto==0 ); |
drh | 76873ab | 2006-01-07 18:48:26 +0000 | [diff] [blame] | 3896 | pC->cacheStatus = CACHE_STALE; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3897 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3898 | break; |
| 3899 | } |
| 3900 | |
drh | 2133d82 | 2008-01-03 18:44:59 +0000 | [diff] [blame] | 3901 | /* Opcode: IdxRowid P1 P2 * * * |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 3902 | ** |
drh | 2133d82 | 2008-01-03 18:44:59 +0000 | [diff] [blame] | 3903 | ** Write into register P2 an integer which is the last entry in the record at |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3904 | ** the end of the index key pointed to by cursor P1. This integer should be |
| 3905 | ** the rowid of the table entry to which this index entry points. |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 3906 | ** |
drh | 3c899a6 | 2006-01-10 18:44:08 +0000 | [diff] [blame] | 3907 | ** See also: Rowid, MakeIdxRec. |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 3908 | */ |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 3909 | case OP_IdxRowid: { /* out2-prerelease */ |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 3910 | int i = pOp->p1; |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 3911 | BtCursor *pCrsr; |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame] | 3912 | Cursor *pC; |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 3913 | |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3914 | assert( i>=0 && i<p->nCursor ); |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 3915 | assert( p->apCsr[i]!=0 ); |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame] | 3916 | if( (pCrsr = (pC = p->apCsr[i])->pCursor)!=0 ){ |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 3917 | i64 rowid; |
danielk1977 | 452c989 | 2004-05-13 05:16:15 +0000 | [diff] [blame] | 3918 | |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame] | 3919 | assert( pC->deferredMoveto==0 ); |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3920 | assert( pC->isTable==0 ); |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 3921 | if( !pC->nullRow ){ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 3922 | rc = sqlite3VdbeIdxRowid(pCrsr, &rowid); |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 3923 | if( rc!=SQLITE_OK ){ |
| 3924 | goto abort_due_to_error; |
| 3925 | } |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 3926 | MemSetTypeFlag(pOut, MEM_Int); |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 3927 | pOut->u.i = rowid; |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 3928 | } |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 3929 | } |
| 3930 | break; |
| 3931 | } |
| 3932 | |
danielk1977 | 61dd583 | 2008-04-18 11:31:12 +0000 | [diff] [blame] | 3933 | /* Opcode: IdxGE P1 P2 P3 P4 P5 |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 3934 | ** |
danielk1977 | 61dd583 | 2008-04-18 11:31:12 +0000 | [diff] [blame] | 3935 | ** The P4 register values beginning with P3 form an unpacked index |
| 3936 | ** key that omits the ROWID. Compare this key value against the index |
| 3937 | ** that P1 is currently pointing to, ignoring the ROWID on the P1 index. |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 3938 | ** |
danielk1977 | 61dd583 | 2008-04-18 11:31:12 +0000 | [diff] [blame] | 3939 | ** If the P1 index entry is greater than or equal to the key value |
| 3940 | ** then jump to P2. Otherwise fall through to the next instruction. |
drh | 772ae62 | 2004-05-19 13:13:08 +0000 | [diff] [blame] | 3941 | ** |
danielk1977 | 61dd583 | 2008-04-18 11:31:12 +0000 | [diff] [blame] | 3942 | ** If P5 is non-zero then the key value is increased by an epsilon |
| 3943 | ** prior to the comparison. This make the opcode work like IdxGT except |
| 3944 | ** that if the key from register P3 is a prefix of the key in the cursor, |
| 3945 | ** the result is false whereas it would be true with IdxGT. |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 3946 | */ |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 3947 | /* Opcode: IdxLT P1 P2 P3 * P5 |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 3948 | ** |
danielk1977 | 61dd583 | 2008-04-18 11:31:12 +0000 | [diff] [blame] | 3949 | ** The P4 register values beginning with P3 form an unpacked index |
| 3950 | ** key that omits the ROWID. Compare this key value against the index |
| 3951 | ** that P1 is currently pointing to, ignoring the ROWID on the P1 index. |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 3952 | ** |
danielk1977 | 61dd583 | 2008-04-18 11:31:12 +0000 | [diff] [blame] | 3953 | ** If the P1 index entry is less than the key value then jump to P2. |
| 3954 | ** Otherwise fall through to the next instruction. |
drh | 772ae62 | 2004-05-19 13:13:08 +0000 | [diff] [blame] | 3955 | ** |
danielk1977 | 61dd583 | 2008-04-18 11:31:12 +0000 | [diff] [blame] | 3956 | ** If P5 is non-zero then the key value is increased by an epsilon prior |
| 3957 | ** to the comparison. This makes the opcode work like IdxLE. |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 3958 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3959 | case OP_IdxLT: /* jump, in3 */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3960 | case OP_IdxGE: { /* jump, in3 */ |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 3961 | int i= pOp->p1; |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame] | 3962 | Cursor *pC; |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 3963 | |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3964 | assert( i>=0 && i<p->nCursor ); |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 3965 | assert( p->apCsr[i]!=0 ); |
drh | 4f26bb6 | 2005-09-08 14:17:20 +0000 | [diff] [blame] | 3966 | if( (pC = p->apCsr[i])->pCursor!=0 ){ |
drh | 0850b53 | 2006-01-31 19:31:43 +0000 | [diff] [blame] | 3967 | int res; |
danielk1977 | 61dd583 | 2008-04-18 11:31:12 +0000 | [diff] [blame] | 3968 | UnpackedRecord r; |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame] | 3969 | assert( pC->deferredMoveto==0 ); |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 3970 | assert( pOp->p5==0 || pOp->p5==1 ); |
danielk1977 | 61dd583 | 2008-04-18 11:31:12 +0000 | [diff] [blame] | 3971 | assert( pOp->p4type==P4_INT32 ); |
| 3972 | r.pKeyInfo = pC->pKeyInfo; |
| 3973 | r.nField = pOp->p4.i; |
| 3974 | r.needFree = 0; |
| 3975 | r.needDestroy = 0; |
| 3976 | r.aMem = &p->aMem[pOp->p3]; |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 3977 | *pC->pIncrKey = pOp->p5; |
danielk1977 | 61dd583 | 2008-04-18 11:31:12 +0000 | [diff] [blame] | 3978 | rc = sqlite3VdbeIdxKeyCompare(pC, &r, 0, 0, &res); |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 3979 | *pC->pIncrKey = 0; |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 3980 | if( pOp->opcode==OP_IdxLT ){ |
| 3981 | res = -res; |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 3982 | }else{ |
| 3983 | assert( pOp->opcode==OP_IdxGE ); |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 3984 | res++; |
| 3985 | } |
| 3986 | if( res>0 ){ |
| 3987 | pc = pOp->p2 - 1 ; |
| 3988 | } |
| 3989 | } |
| 3990 | break; |
| 3991 | } |
| 3992 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 3993 | /* Opcode: Destroy P1 P2 P3 * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3994 | ** |
| 3995 | ** Delete an entire database table or index whose root page in the database |
| 3996 | ** file is given by P1. |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 3997 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 3998 | ** The table being destroyed is in the main database file if P3==0. If |
| 3999 | ** P3==1 then the table to be clear is in the auxiliary database file |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 4000 | ** that is used to store tables create using CREATE TEMPORARY TABLE. |
| 4001 | ** |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 4002 | ** If AUTOVACUUM is enabled then it is possible that another root page |
| 4003 | ** might be moved into the newly deleted root page in order to keep all |
| 4004 | ** root pages contiguous at the beginning of the database. The former |
| 4005 | ** value of the root page that moved - its value before the move occurred - |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4006 | ** is stored in register P2. If no page |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4007 | ** movement was required (because the table being dropped was already |
| 4008 | ** the last one in the database) then a zero is stored in register P2. |
| 4009 | ** If AUTOVACUUM is disabled then a zero is stored in register P2. |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 4010 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 4011 | ** See also: Clear |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4012 | */ |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4013 | case OP_Destroy: { /* out2-prerelease */ |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 4014 | int iMoved; |
drh | 3765df4 | 2006-06-28 18:18:09 +0000 | [diff] [blame] | 4015 | int iCnt; |
danielk1977 | 212b218 | 2006-06-23 14:32:08 +0000 | [diff] [blame] | 4016 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | 5a91a53 | 2007-01-05 16:39:43 +0000 | [diff] [blame] | 4017 | Vdbe *pVdbe; |
danielk1977 | 212b218 | 2006-06-23 14:32:08 +0000 | [diff] [blame] | 4018 | iCnt = 0; |
| 4019 | for(pVdbe=db->pVdbe; pVdbe; pVdbe=pVdbe->pNext){ |
| 4020 | if( pVdbe->magic==VDBE_MAGIC_RUN && pVdbe->inVtabMethod<2 && pVdbe->pc>=0 ){ |
| 4021 | iCnt++; |
| 4022 | } |
| 4023 | } |
drh | 3765df4 | 2006-06-28 18:18:09 +0000 | [diff] [blame] | 4024 | #else |
| 4025 | iCnt = db->activeVdbeCnt; |
danielk1977 | 212b218 | 2006-06-23 14:32:08 +0000 | [diff] [blame] | 4026 | #endif |
| 4027 | if( iCnt>1 ){ |
danielk1977 | e6efa74 | 2004-11-10 11:55:10 +0000 | [diff] [blame] | 4028 | rc = SQLITE_LOCKED; |
drh | 77658e2 | 2007-12-04 16:54:52 +0000 | [diff] [blame] | 4029 | p->errorAction = OE_Abort; |
danielk1977 | e6efa74 | 2004-11-10 11:55:10 +0000 | [diff] [blame] | 4030 | }else{ |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4031 | int iDb = pOp->p3; |
danielk1977 | 212b218 | 2006-06-23 14:32:08 +0000 | [diff] [blame] | 4032 | assert( iCnt==1 ); |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4033 | assert( (p->btreeMask & (1<<iDb))!=0 ); |
| 4034 | rc = sqlite3BtreeDropTable(db->aDb[iDb].pBt, pOp->p1, &iMoved); |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 4035 | MemSetTypeFlag(pOut, MEM_Int); |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4036 | pOut->u.i = iMoved; |
drh | 3765df4 | 2006-06-28 18:18:09 +0000 | [diff] [blame] | 4037 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | e6efa74 | 2004-11-10 11:55:10 +0000 | [diff] [blame] | 4038 | if( rc==SQLITE_OK && iMoved!=0 ){ |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4039 | sqlite3RootPageMoved(&db->aDb[iDb], iMoved, pOp->p1); |
danielk1977 | e6efa74 | 2004-11-10 11:55:10 +0000 | [diff] [blame] | 4040 | } |
drh | 3765df4 | 2006-06-28 18:18:09 +0000 | [diff] [blame] | 4041 | #endif |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 4042 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4043 | break; |
| 4044 | } |
| 4045 | |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 4046 | /* Opcode: Clear P1 P2 * |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 4047 | ** |
| 4048 | ** Delete all contents of the database table or index whose root page |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 4049 | ** in the database file is given by P1. But, unlike Destroy, do not |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 4050 | ** remove the table or index from the database file. |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 4051 | ** |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 4052 | ** The table being clear is in the main database file if P2==0. If |
| 4053 | ** P2==1 then the table to be clear is in the auxiliary database file |
| 4054 | ** that is used to store tables create using CREATE TEMPORARY TABLE. |
| 4055 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 4056 | ** See also: Destroy |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 4057 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4058 | case OP_Clear: { |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 4059 | assert( (p->btreeMask & (1<<pOp->p2))!=0 ); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 4060 | rc = sqlite3BtreeClearTable(db->aDb[pOp->p2].pBt, pOp->p1); |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 4061 | break; |
| 4062 | } |
| 4063 | |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 4064 | /* Opcode: CreateTable P1 P2 * * * |
drh | 5b2fd56 | 2001-09-13 15:21:31 +0000 | [diff] [blame] | 4065 | ** |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 4066 | ** Allocate a new table in the main database file if P1==0 or in the |
| 4067 | ** auxiliary database file if P1==1 or in an attached database if |
| 4068 | ** P1>1. Write the root page number of the new table into |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4069 | ** register P2 |
drh | 5b2fd56 | 2001-09-13 15:21:31 +0000 | [diff] [blame] | 4070 | ** |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 4071 | ** The difference between a table and an index is this: A table must |
| 4072 | ** have a 4-byte integer key and can have arbitrary data. An index |
| 4073 | ** has an arbitrary key but no data. |
| 4074 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 4075 | ** See also: CreateIndex |
drh | 5b2fd56 | 2001-09-13 15:21:31 +0000 | [diff] [blame] | 4076 | */ |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 4077 | /* Opcode: CreateIndex P1 P2 * * * |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 4078 | ** |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 4079 | ** Allocate a new index in the main database file if P1==0 or in the |
| 4080 | ** auxiliary database file if P1==1 or in an attached database if |
| 4081 | ** P1>1. Write the root page number of the new table into |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4082 | ** register P2. |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 4083 | ** |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 4084 | ** See documentation on OP_CreateTable for additional information. |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 4085 | */ |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 4086 | case OP_CreateIndex: /* out2-prerelease */ |
| 4087 | case OP_CreateTable: { /* out2-prerelease */ |
drh | 5b2fd56 | 2001-09-13 15:21:31 +0000 | [diff] [blame] | 4088 | int pgno; |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 4089 | int flags; |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 4090 | Db *pDb; |
| 4091 | assert( pOp->p1>=0 && pOp->p1<db->nDb ); |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 4092 | assert( (p->btreeMask & (1<<pOp->p1))!=0 ); |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 4093 | pDb = &db->aDb[pOp->p1]; |
| 4094 | assert( pDb->pBt!=0 ); |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 4095 | if( pOp->opcode==OP_CreateTable ){ |
danielk1977 | 9407625 | 2004-05-14 12:16:11 +0000 | [diff] [blame] | 4096 | /* flags = BTREE_INTKEY; */ |
| 4097 | flags = BTREE_LEAFDATA|BTREE_INTKEY; |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 4098 | }else{ |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 4099 | flags = BTREE_ZERODATA; |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 4100 | } |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 4101 | rc = sqlite3BtreeCreateTable(pDb->pBt, &pgno, flags); |
drh | 5b2fd56 | 2001-09-13 15:21:31 +0000 | [diff] [blame] | 4102 | if( rc==SQLITE_OK ){ |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4103 | pOut->u.i = pgno; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 4104 | MemSetTypeFlag(pOut, MEM_Int); |
drh | 5b2fd56 | 2001-09-13 15:21:31 +0000 | [diff] [blame] | 4105 | } |
| 4106 | break; |
| 4107 | } |
| 4108 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4109 | /* Opcode: ParseSchema P1 P2 * P4 * |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 4110 | ** |
| 4111 | ** Read and parse all entries from the SQLITE_MASTER table of database P1 |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 4112 | ** that match the WHERE clause P4. P2 is the "force" flag. Always do |
drh | 3c23a88 | 2007-01-09 14:01:13 +0000 | [diff] [blame] | 4113 | ** the parsing if P2 is true. If P2 is false, then this routine is a |
| 4114 | ** no-op if the schema is not currently loaded. In other words, if P2 |
| 4115 | ** is false, the SQLITE_MASTER table is only parsed if the rest of the |
| 4116 | ** schema is already loaded into the symbol table. |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 4117 | ** |
| 4118 | ** This opcode invokes the parser to create a new virtual machine, |
shane | 21e7feb | 2008-05-30 15:59:49 +0000 | [diff] [blame] | 4119 | ** then runs the new virtual machine. It is thus a re-entrant opcode. |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 4120 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4121 | case OP_ParseSchema: { |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 4122 | char *zSql; |
| 4123 | int iDb = pOp->p1; |
| 4124 | const char *zMaster; |
| 4125 | InitData initData; |
| 4126 | |
| 4127 | assert( iDb>=0 && iDb<db->nDb ); |
drh | 3c23a88 | 2007-01-09 14:01:13 +0000 | [diff] [blame] | 4128 | if( !pOp->p2 && !DbHasProperty(db, iDb, DB_SchemaLoaded) ){ |
| 4129 | break; |
| 4130 | } |
danielk1977 | 53c0f74 | 2005-03-29 03:10:59 +0000 | [diff] [blame] | 4131 | zMaster = SCHEMA_TABLE(iDb); |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 4132 | initData.db = db; |
drh | ece3c72 | 2006-09-23 20:36:01 +0000 | [diff] [blame] | 4133 | initData.iDb = pOp->p1; |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 4134 | initData.pzErrMsg = &p->zErrMsg; |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 4135 | zSql = sqlite3MPrintf(db, |
drh | ece3c72 | 2006-09-23 20:36:01 +0000 | [diff] [blame] | 4136 | "SELECT name, rootpage, sql FROM '%q'.%s WHERE %s", |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 4137 | db->aDb[iDb].zName, zMaster, pOp->p4.z); |
drh | 71c697e | 2004-08-08 23:39:19 +0000 | [diff] [blame] | 4138 | if( zSql==0 ) goto no_mem; |
drh | 7e8b848 | 2008-01-23 03:03:05 +0000 | [diff] [blame] | 4139 | (void)sqlite3SafetyOff(db); |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 4140 | assert( db->init.busy==0 ); |
| 4141 | db->init.busy = 1; |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 4142 | assert( !db->mallocFailed ); |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 4143 | rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0); |
drh | 15ca1df | 2006-07-26 13:43:30 +0000 | [diff] [blame] | 4144 | if( rc==SQLITE_ABORT ) rc = initData.rc; |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 4145 | sqlite3_free(zSql); |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 4146 | db->init.busy = 0; |
drh | 7e8b848 | 2008-01-23 03:03:05 +0000 | [diff] [blame] | 4147 | (void)sqlite3SafetyOn(db); |
danielk1977 | 261919c | 2005-12-06 12:52:59 +0000 | [diff] [blame] | 4148 | if( rc==SQLITE_NOMEM ){ |
danielk1977 | 261919c | 2005-12-06 12:52:59 +0000 | [diff] [blame] | 4149 | goto no_mem; |
| 4150 | } |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 4151 | break; |
| 4152 | } |
| 4153 | |
drh | cfed7bc | 2006-03-13 14:28:05 +0000 | [diff] [blame] | 4154 | #if !defined(SQLITE_OMIT_ANALYZE) && !defined(SQLITE_OMIT_PARSER) |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4155 | /* Opcode: LoadAnalysis P1 * * * * |
drh | 497e446 | 2005-07-23 03:18:40 +0000 | [diff] [blame] | 4156 | ** |
| 4157 | ** Read the sqlite_stat1 table for database P1 and load the content |
| 4158 | ** of that table into the internal index hash table. This will cause |
| 4159 | ** the analysis to be used when preparing all subsequent queries. |
| 4160 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4161 | case OP_LoadAnalysis: { |
drh | 497e446 | 2005-07-23 03:18:40 +0000 | [diff] [blame] | 4162 | int iDb = pOp->p1; |
| 4163 | assert( iDb>=0 && iDb<db->nDb ); |
drh | cf1be45 | 2007-05-12 12:08:51 +0000 | [diff] [blame] | 4164 | rc = sqlite3AnalysisLoad(db, iDb); |
drh | 497e446 | 2005-07-23 03:18:40 +0000 | [diff] [blame] | 4165 | break; |
| 4166 | } |
drh | cfed7bc | 2006-03-13 14:28:05 +0000 | [diff] [blame] | 4167 | #endif /* !defined(SQLITE_OMIT_ANALYZE) && !defined(SQLITE_OMIT_PARSER) */ |
drh | 497e446 | 2005-07-23 03:18:40 +0000 | [diff] [blame] | 4168 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4169 | /* Opcode: DropTable P1 * * P4 * |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 4170 | ** |
| 4171 | ** Remove the internal (in-memory) data structures that describe |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 4172 | ** the table named P4 in database P1. This is called after a table |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 4173 | ** is dropped in order to keep the internal representation of the |
| 4174 | ** schema consistent with what is on disk. |
| 4175 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4176 | case OP_DropTable: { |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 4177 | sqlite3UnlinkAndDeleteTable(db, pOp->p1, pOp->p4.z); |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 4178 | break; |
| 4179 | } |
| 4180 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4181 | /* Opcode: DropIndex P1 * * P4 * |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 4182 | ** |
| 4183 | ** Remove the internal (in-memory) data structures that describe |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 4184 | ** the index named P4 in database P1. This is called after an index |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 4185 | ** is dropped in order to keep the internal representation of the |
| 4186 | ** schema consistent with what is on disk. |
| 4187 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4188 | case OP_DropIndex: { |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 4189 | sqlite3UnlinkAndDeleteIndex(db, pOp->p1, pOp->p4.z); |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 4190 | break; |
| 4191 | } |
| 4192 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4193 | /* Opcode: DropTrigger P1 * * P4 * |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 4194 | ** |
| 4195 | ** Remove the internal (in-memory) data structures that describe |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 4196 | ** the trigger named P4 in database P1. This is called after a trigger |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 4197 | ** is dropped in order to keep the internal representation of the |
| 4198 | ** schema consistent with what is on disk. |
| 4199 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4200 | case OP_DropTrigger: { |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 4201 | sqlite3UnlinkAndDeleteTrigger(db, pOp->p1, pOp->p4.z); |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 4202 | break; |
| 4203 | } |
| 4204 | |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 4205 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 4206 | #ifndef SQLITE_OMIT_INTEGRITY_CHECK |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4207 | /* Opcode: IntegrityCk P1 P2 P3 * P5 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4208 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4209 | ** Do an analysis of the currently open database. Store in |
| 4210 | ** register P1 the text of an error message describing any problems. |
| 4211 | ** If no problems are found, store a NULL in register P1. |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 4212 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4213 | ** The register P3 contains the maximum number of allowed errors. |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 4214 | ** At most reg(P3) errors will be reported. |
| 4215 | ** In other words, the analysis stops as soon as reg(P1) errors are |
| 4216 | ** seen. Reg(P1) is updated with the number of errors remaining. |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 4217 | ** |
drh | 7906975 | 2004-05-22 21:30:40 +0000 | [diff] [blame] | 4218 | ** The root page numbers of all tables in the database are integer |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 4219 | ** stored in reg(P1), reg(P1+1), reg(P1+2), .... There are P2 tables |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4220 | ** total. |
drh | 2150432 | 2002-06-25 13:16:02 +0000 | [diff] [blame] | 4221 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4222 | ** If P5 is not zero, the check is done on the auxiliary database |
drh | 2150432 | 2002-06-25 13:16:02 +0000 | [diff] [blame] | 4223 | ** file, not the main database file. |
drh | 1dd397f | 2002-02-03 03:34:07 +0000 | [diff] [blame] | 4224 | ** |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 4225 | ** This opcode is used to implement the integrity_check pragma. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4226 | */ |
drh | aaab572 | 2002-02-19 13:39:21 +0000 | [diff] [blame] | 4227 | case OP_IntegrityCk: { |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4228 | int nRoot; /* Number of tables to check. (Number of root pages.) */ |
| 4229 | int *aRoot; /* Array of rootpage numbers for tables to be checked */ |
| 4230 | int j; /* Loop counter */ |
| 4231 | int nErr; /* Number of errors reported */ |
| 4232 | char *z; /* Text of the error report */ |
| 4233 | Mem *pnErr; /* Register keeping track of errors remaining */ |
| 4234 | |
| 4235 | nRoot = pOp->p2; |
drh | 7906975 | 2004-05-22 21:30:40 +0000 | [diff] [blame] | 4236 | assert( nRoot>0 ); |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 4237 | aRoot = sqlite3Malloc( sizeof(int)*(nRoot+1) ); |
drh | caec2f1 | 2003-01-07 02:47:47 +0000 | [diff] [blame] | 4238 | if( aRoot==0 ) goto no_mem; |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4239 | assert( pOp->p3>0 && pOp->p3<=p->nMem ); |
| 4240 | pnErr = &p->aMem[pOp->p3]; |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 4241 | assert( (pnErr->flags & MEM_Int)!=0 ); |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4242 | assert( (pnErr->flags & (MEM_Str|MEM_Blob))==0 ); |
| 4243 | pIn1 = &p->aMem[pOp->p1]; |
drh | 7906975 | 2004-05-22 21:30:40 +0000 | [diff] [blame] | 4244 | for(j=0; j<nRoot; j++){ |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4245 | aRoot[j] = sqlite3VdbeIntValue(&pIn1[j]); |
drh | 1dd397f | 2002-02-03 03:34:07 +0000 | [diff] [blame] | 4246 | } |
| 4247 | aRoot[j] = 0; |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4248 | assert( pOp->p5<db->nDb ); |
| 4249 | assert( (p->btreeMask & (1<<pOp->p5))!=0 ); |
| 4250 | z = sqlite3BtreeIntegrityCheck(db->aDb[pOp->p5].pBt, aRoot, nRoot, |
drh | 3c024d6 | 2007-03-30 11:23:45 +0000 | [diff] [blame] | 4251 | pnErr->u.i, &nErr); |
| 4252 | pnErr->u.i -= nErr; |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 4253 | sqlite3VdbeMemSetNull(pIn1); |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 4254 | if( nErr==0 ){ |
| 4255 | assert( z==0 ); |
drh | 1dd397f | 2002-02-03 03:34:07 +0000 | [diff] [blame] | 4256 | }else{ |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 4257 | sqlite3VdbeMemSetStr(pIn1, z, -1, SQLITE_UTF8, sqlite3_free); |
danielk1977 | 8a6b541 | 2004-05-24 07:04:25 +0000 | [diff] [blame] | 4258 | } |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 4259 | UPDATE_MAX_BLOBSIZE(pIn1); |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4260 | sqlite3VdbeChangeEncoding(pIn1, encoding); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 4261 | sqlite3_free(aRoot); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4262 | break; |
| 4263 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 4264 | #endif /* SQLITE_OMIT_INTEGRITY_CHECK */ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4265 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4266 | /* Opcode: FifoWrite P1 * * * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4267 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4268 | ** Write the integer from register P1 into the Fifo. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4269 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4270 | case OP_FifoWrite: { /* in1 */ |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4271 | if( sqlite3VdbeFifoPush(&p->sFifo, sqlite3VdbeIntValue(pIn1))==SQLITE_NOMEM ){ |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 4272 | goto no_mem; |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 4273 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4274 | break; |
| 4275 | } |
| 4276 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4277 | /* Opcode: FifoRead P1 P2 * * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4278 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4279 | ** Attempt to read a single integer from the Fifo. Store that |
| 4280 | ** integer in register P1. |
danielk1977 | 96cb76f | 2008-01-04 13:24:28 +0000 | [diff] [blame] | 4281 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4282 | ** If the Fifo is empty jump to P2. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4283 | */ |
drh | 3a40f69 | 2008-01-04 16:50:09 +0000 | [diff] [blame] | 4284 | case OP_FifoRead: { /* jump */ |
drh | caec2f1 | 2003-01-07 02:47:47 +0000 | [diff] [blame] | 4285 | CHECK_FOR_INTERRUPT; |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4286 | assert( pOp->p1>0 && pOp->p1<=p->nMem ); |
| 4287 | pOut = &p->aMem[pOp->p1]; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 4288 | MemSetTypeFlag(pOut, MEM_Int); |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4289 | if( sqlite3VdbeFifoPop(&p->sFifo, &pOut->u.i)==SQLITE_DONE ){ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4290 | pc = pOp->p2 - 1; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4291 | } |
| 4292 | break; |
| 4293 | } |
| 4294 | |
danielk1977 | 93758c8 | 2005-01-21 08:13:14 +0000 | [diff] [blame] | 4295 | #ifndef SQLITE_OMIT_TRIGGER |
rdc | b0c374f | 2004-02-20 22:53:38 +0000 | [diff] [blame] | 4296 | /* Opcode: ContextPush * * * |
| 4297 | ** |
| 4298 | ** Save the current Vdbe context such that it can be restored by a ContextPop |
| 4299 | ** opcode. The context stores the last insert row id, the last statement change |
| 4300 | ** count, and the current statement change count. |
| 4301 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4302 | case OP_ContextPush: { |
drh | 344737f | 2004-09-19 00:50:20 +0000 | [diff] [blame] | 4303 | int i = p->contextStackTop++; |
| 4304 | Context *pContext; |
danielk1977 | b28af71 | 2004-06-21 06:50:26 +0000 | [diff] [blame] | 4305 | |
drh | 344737f | 2004-09-19 00:50:20 +0000 | [diff] [blame] | 4306 | assert( i>=0 ); |
danielk1977 | b28af71 | 2004-06-21 06:50:26 +0000 | [diff] [blame] | 4307 | /* 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] | 4308 | if( i>=p->contextStackDepth ){ |
| 4309 | p->contextStackDepth = i+1; |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 4310 | p->contextStack = sqlite3DbReallocOrFree(db, p->contextStack, |
drh | cf64372 | 2007-03-27 13:36:37 +0000 | [diff] [blame] | 4311 | sizeof(Context)*(i+1)); |
drh | 344737f | 2004-09-19 00:50:20 +0000 | [diff] [blame] | 4312 | if( p->contextStack==0 ) goto no_mem; |
| 4313 | } |
| 4314 | pContext = &p->contextStack[i]; |
| 4315 | pContext->lastRowid = db->lastRowid; |
| 4316 | pContext->nChange = p->nChange; |
drh | a01f79d | 2005-07-08 13:07:59 +0000 | [diff] [blame] | 4317 | pContext->sFifo = p->sFifo; |
| 4318 | sqlite3VdbeFifoInit(&p->sFifo); |
rdc | b0c374f | 2004-02-20 22:53:38 +0000 | [diff] [blame] | 4319 | break; |
| 4320 | } |
| 4321 | |
| 4322 | /* Opcode: ContextPop * * * |
| 4323 | ** |
| 4324 | ** Restore the Vdbe context to the state it was in when contextPush was last |
| 4325 | ** executed. The context stores the last insert row id, the last statement |
| 4326 | ** change count, and the current statement change count. |
| 4327 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4328 | case OP_ContextPop: { |
drh | 344737f | 2004-09-19 00:50:20 +0000 | [diff] [blame] | 4329 | Context *pContext = &p->contextStack[--p->contextStackTop]; |
| 4330 | assert( p->contextStackTop>=0 ); |
| 4331 | db->lastRowid = pContext->lastRowid; |
| 4332 | p->nChange = pContext->nChange; |
drh | a01f79d | 2005-07-08 13:07:59 +0000 | [diff] [blame] | 4333 | sqlite3VdbeFifoClear(&p->sFifo); |
| 4334 | p->sFifo = pContext->sFifo; |
rdc | b0c374f | 2004-02-20 22:53:38 +0000 | [diff] [blame] | 4335 | break; |
| 4336 | } |
danielk1977 | 93758c8 | 2005-01-21 08:13:14 +0000 | [diff] [blame] | 4337 | #endif /* #ifndef SQLITE_OMIT_TRIGGER */ |
rdc | b0c374f | 2004-02-20 22:53:38 +0000 | [diff] [blame] | 4338 | |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 4339 | #ifndef SQLITE_OMIT_AUTOINCREMENT |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4340 | /* Opcode: MemMax P1 P2 * * * |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 4341 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4342 | ** Set the value of register P1 to the maximum of its current value |
| 4343 | ** and the value in register P2. |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 4344 | ** |
| 4345 | ** This instruction throws an error if the memory cell is not initially |
| 4346 | ** an integer. |
| 4347 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4348 | case OP_MemMax: { /* in1, in2 */ |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4349 | sqlite3VdbeMemIntegerify(pIn1); |
| 4350 | sqlite3VdbeMemIntegerify(pIn2); |
| 4351 | if( pIn1->u.i<pIn2->u.i){ |
| 4352 | pIn1->u.i = pIn2->u.i; |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 4353 | } |
| 4354 | break; |
| 4355 | } |
| 4356 | #endif /* SQLITE_OMIT_AUTOINCREMENT */ |
| 4357 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4358 | /* Opcode: IfPos P1 P2 * * * |
danielk1977 | a2dc3b1 | 2005-02-05 12:48:48 +0000 | [diff] [blame] | 4359 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4360 | ** If the value of register P1 is 1 or greater, jump to P2. |
drh | 6f58f70 | 2006-01-08 05:26:41 +0000 | [diff] [blame] | 4361 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4362 | ** It is illegal to use this instruction on a register that does |
drh | 6f58f70 | 2006-01-08 05:26:41 +0000 | [diff] [blame] | 4363 | ** not contain an integer. An assertion fault will result if you try. |
danielk1977 | a2dc3b1 | 2005-02-05 12:48:48 +0000 | [diff] [blame] | 4364 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4365 | case OP_IfPos: { /* jump, in1 */ |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 4366 | assert( pIn1->flags&MEM_Int ); |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 4367 | if( pIn1->u.i>0 ){ |
drh | ec7429a | 2005-10-06 16:53:14 +0000 | [diff] [blame] | 4368 | pc = pOp->p2 - 1; |
| 4369 | } |
| 4370 | break; |
| 4371 | } |
| 4372 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4373 | /* Opcode: IfNeg P1 P2 * * * |
drh | 15007a9 | 2006-01-08 18:10:17 +0000 | [diff] [blame] | 4374 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4375 | ** If the value of register P1 is less than zero, jump to P2. |
drh | 15007a9 | 2006-01-08 18:10:17 +0000 | [diff] [blame] | 4376 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4377 | ** It is illegal to use this instruction on a register that does |
drh | 15007a9 | 2006-01-08 18:10:17 +0000 | [diff] [blame] | 4378 | ** not contain an integer. An assertion fault will result if you try. |
| 4379 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4380 | case OP_IfNeg: { /* jump, in1 */ |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 4381 | assert( pIn1->flags&MEM_Int ); |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 4382 | if( pIn1->u.i<0 ){ |
drh | 15007a9 | 2006-01-08 18:10:17 +0000 | [diff] [blame] | 4383 | pc = pOp->p2 - 1; |
| 4384 | } |
| 4385 | break; |
| 4386 | } |
| 4387 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4388 | /* Opcode: IfZero P1 P2 * * * |
drh | ec7429a | 2005-10-06 16:53:14 +0000 | [diff] [blame] | 4389 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4390 | ** If the value of register P1 is exactly 0, jump to P2. |
drh | 6f58f70 | 2006-01-08 05:26:41 +0000 | [diff] [blame] | 4391 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4392 | ** It is illegal to use this instruction on a register that does |
drh | 6f58f70 | 2006-01-08 05:26:41 +0000 | [diff] [blame] | 4393 | ** not contain an integer. An assertion fault will result if you try. |
drh | ec7429a | 2005-10-06 16:53:14 +0000 | [diff] [blame] | 4394 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4395 | case OP_IfZero: { /* jump, in1 */ |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 4396 | assert( pIn1->flags&MEM_Int ); |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 4397 | if( pIn1->u.i==0 ){ |
drh | a2a49dc | 2008-01-02 14:28:13 +0000 | [diff] [blame] | 4398 | pc = pOp->p2 - 1; |
| 4399 | } |
| 4400 | break; |
| 4401 | } |
| 4402 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4403 | /* Opcode: AggStep * P2 P3 P4 P5 |
drh | e509535 | 2002-02-24 03:25:14 +0000 | [diff] [blame] | 4404 | ** |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 4405 | ** Execute the step function for an aggregate. The |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4406 | ** function has P5 arguments. P4 is a pointer to the FuncDef |
| 4407 | ** structure that specifies the function. Use register |
| 4408 | ** P3 as the accumulator. |
drh | e509535 | 2002-02-24 03:25:14 +0000 | [diff] [blame] | 4409 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4410 | ** The P5 arguments are taken from register P2 and its |
| 4411 | ** successors. |
drh | e509535 | 2002-02-24 03:25:14 +0000 | [diff] [blame] | 4412 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4413 | case OP_AggStep: { |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4414 | int n = pOp->p5; |
drh | e509535 | 2002-02-24 03:25:14 +0000 | [diff] [blame] | 4415 | int i; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 4416 | Mem *pMem, *pRec; |
danielk1977 | 22322fd | 2004-05-25 23:35:17 +0000 | [diff] [blame] | 4417 | sqlite3_context ctx; |
danielk1977 | 6ddcca5 | 2004-05-24 23:48:25 +0000 | [diff] [blame] | 4418 | sqlite3_value **apVal; |
drh | e509535 | 2002-02-24 03:25:14 +0000 | [diff] [blame] | 4419 | |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 4420 | assert( n>=0 ); |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4421 | pRec = &p->aMem[pOp->p2]; |
danielk1977 | 6ddcca5 | 2004-05-24 23:48:25 +0000 | [diff] [blame] | 4422 | apVal = p->apArg; |
| 4423 | assert( apVal || n==0 ); |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 4424 | for(i=0; i<n; i++, pRec++){ |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 4425 | apVal[i] = pRec; |
drh | 8079a0d | 2006-01-12 17:20:50 +0000 | [diff] [blame] | 4426 | storeTypeInfo(pRec, encoding); |
drh | e509535 | 2002-02-24 03:25:14 +0000 | [diff] [blame] | 4427 | } |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 4428 | ctx.pFunc = pOp->p4.pFunc; |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4429 | assert( pOp->p3>0 && pOp->p3<=p->nMem ); |
| 4430 | ctx.pMem = pMem = &p->aMem[pOp->p3]; |
drh | abfcea2 | 2005-09-06 20:36:48 +0000 | [diff] [blame] | 4431 | pMem->n++; |
drh | 90669c1 | 2006-01-20 15:45:36 +0000 | [diff] [blame] | 4432 | ctx.s.flags = MEM_Null; |
| 4433 | ctx.s.z = 0; |
danielk1977 | 5f09613 | 2008-03-28 15:44:09 +0000 | [diff] [blame] | 4434 | ctx.s.zMalloc = 0; |
drh | 90669c1 | 2006-01-20 15:45:36 +0000 | [diff] [blame] | 4435 | ctx.s.xDel = 0; |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 4436 | ctx.s.db = db; |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 4437 | ctx.isError = 0; |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 4438 | ctx.pColl = 0; |
| 4439 | if( ctx.pFunc->needCollSeq ){ |
| 4440 | assert( pOp>p->aOp ); |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 4441 | assert( pOp[-1].p4type==P4_COLLSEQ ); |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 4442 | assert( pOp[-1].opcode==OP_CollSeq ); |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 4443 | ctx.pColl = pOp[-1].p4.pColl; |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 4444 | } |
danielk1977 | 6ddcca5 | 2004-05-24 23:48:25 +0000 | [diff] [blame] | 4445 | (ctx.pFunc->xStep)(&ctx, n, apVal); |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 4446 | if( ctx.isError ){ |
drh | f089aa4 | 2008-07-08 19:34:06 +0000 | [diff] [blame] | 4447 | sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(&ctx.s)); |
drh | 69544ec | 2008-02-06 14:11:34 +0000 | [diff] [blame] | 4448 | rc = ctx.isError; |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 4449 | } |
drh | 90669c1 | 2006-01-20 15:45:36 +0000 | [diff] [blame] | 4450 | sqlite3VdbeMemRelease(&ctx.s); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4451 | break; |
| 4452 | } |
| 4453 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4454 | /* Opcode: AggFinal P1 P2 * P4 * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4455 | ** |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 4456 | ** Execute the finalizer function for an aggregate. P1 is |
| 4457 | ** the memory location that is the accumulator for the aggregate. |
drh | a10a34b | 2005-09-07 22:09:48 +0000 | [diff] [blame] | 4458 | ** |
| 4459 | ** P2 is the number of arguments that the step function takes and |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 4460 | ** P4 is a pointer to the FuncDef for this function. The P2 |
drh | a10a34b | 2005-09-07 22:09:48 +0000 | [diff] [blame] | 4461 | ** argument is not used by this opcode. It is only there to disambiguate |
| 4462 | ** functions that can take varying numbers of arguments. The |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 4463 | ** P4 argument is only needed for the degenerate case where |
drh | a10a34b | 2005-09-07 22:09:48 +0000 | [diff] [blame] | 4464 | ** the step function was not previously called. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4465 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4466 | case OP_AggFinal: { |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 4467 | Mem *pMem; |
drh | 0a07c10 | 2008-01-03 18:03:08 +0000 | [diff] [blame] | 4468 | assert( pOp->p1>0 && pOp->p1<=p->nMem ); |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 4469 | pMem = &p->aMem[pOp->p1]; |
drh | a10a34b | 2005-09-07 22:09:48 +0000 | [diff] [blame] | 4470 | assert( (pMem->flags & ~(MEM_Null|MEM_Agg))==0 ); |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 4471 | rc = sqlite3VdbeMemFinalize(pMem, pOp->p4.pFunc); |
drh | 90669c1 | 2006-01-20 15:45:36 +0000 | [diff] [blame] | 4472 | if( rc==SQLITE_ERROR ){ |
drh | f089aa4 | 2008-07-08 19:34:06 +0000 | [diff] [blame] | 4473 | sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(pMem)); |
drh | 90669c1 | 2006-01-20 15:45:36 +0000 | [diff] [blame] | 4474 | } |
drh | 2dca868 | 2008-03-21 17:13:13 +0000 | [diff] [blame] | 4475 | sqlite3VdbeChangeEncoding(pMem, encoding); |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 4476 | UPDATE_MAX_BLOBSIZE(pMem); |
drh | 023ae03 | 2007-05-08 12:12:16 +0000 | [diff] [blame] | 4477 | if( sqlite3VdbeMemTooBig(pMem) ){ |
| 4478 | goto too_big; |
| 4479 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4480 | break; |
| 4481 | } |
| 4482 | |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4483 | |
drh | fdbcdee | 2007-03-27 14:44:50 +0000 | [diff] [blame] | 4484 | #if !defined(SQLITE_OMIT_VACUUM) && !defined(SQLITE_OMIT_ATTACH) |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4485 | /* Opcode: Vacuum * * * * * |
drh | 6f8c91c | 2003-12-07 00:24:35 +0000 | [diff] [blame] | 4486 | ** |
| 4487 | ** Vacuum the entire database. This opcode will cause other virtual |
| 4488 | ** machines to be created and run. It may not be called from within |
| 4489 | ** a transaction. |
| 4490 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4491 | case OP_Vacuum: { |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 4492 | if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse; |
| 4493 | rc = sqlite3RunVacuum(&p->zErrMsg, db); |
| 4494 | if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse; |
drh | 6f8c91c | 2003-12-07 00:24:35 +0000 | [diff] [blame] | 4495 | break; |
| 4496 | } |
drh | 154d4b2 | 2006-09-21 11:02:16 +0000 | [diff] [blame] | 4497 | #endif |
drh | 6f8c91c | 2003-12-07 00:24:35 +0000 | [diff] [blame] | 4498 | |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 4499 | #if !defined(SQLITE_OMIT_AUTOVACUUM) |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4500 | /* Opcode: IncrVacuum P1 P2 * * * |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 4501 | ** |
| 4502 | ** Perform a single step of the incremental vacuum procedure on |
drh | ca5557f | 2007-05-04 18:30:40 +0000 | [diff] [blame] | 4503 | ** the P1 database. If the vacuum has finished, jump to instruction |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 4504 | ** P2. Otherwise, fall through to the next instruction. |
| 4505 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4506 | case OP_IncrVacuum: { /* jump */ |
drh | ca5557f | 2007-05-04 18:30:40 +0000 | [diff] [blame] | 4507 | Btree *pBt; |
| 4508 | |
| 4509 | assert( pOp->p1>=0 && pOp->p1<db->nDb ); |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 4510 | assert( (p->btreeMask & (1<<pOp->p1))!=0 ); |
drh | ca5557f | 2007-05-04 18:30:40 +0000 | [diff] [blame] | 4511 | pBt = db->aDb[pOp->p1].pBt; |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 4512 | rc = sqlite3BtreeIncrVacuum(pBt); |
| 4513 | if( rc==SQLITE_DONE ){ |
| 4514 | pc = pOp->p2 - 1; |
| 4515 | rc = SQLITE_OK; |
| 4516 | } |
| 4517 | break; |
| 4518 | } |
| 4519 | #endif |
| 4520 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4521 | /* Opcode: Expire P1 * * * * |
danielk1977 | a21c6b6 | 2005-01-24 10:25:59 +0000 | [diff] [blame] | 4522 | ** |
| 4523 | ** Cause precompiled statements to become expired. An expired statement |
| 4524 | ** fails with an error code of SQLITE_SCHEMA if it is ever executed |
| 4525 | ** (via sqlite3_step()). |
| 4526 | ** |
| 4527 | ** If P1 is 0, then all SQL statements become expired. If P1 is non-zero, |
| 4528 | ** then only the currently executing statement is affected. |
| 4529 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4530 | case OP_Expire: { |
danielk1977 | a21c6b6 | 2005-01-24 10:25:59 +0000 | [diff] [blame] | 4531 | if( !pOp->p1 ){ |
| 4532 | sqlite3ExpirePreparedStatements(db); |
| 4533 | }else{ |
| 4534 | p->expired = 1; |
| 4535 | } |
| 4536 | break; |
| 4537 | } |
| 4538 | |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 4539 | #ifndef SQLITE_OMIT_SHARED_CACHE |
drh | 6a9ad3d | 2008-04-02 16:29:30 +0000 | [diff] [blame] | 4540 | /* Opcode: TableLock P1 P2 P3 P4 * |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 4541 | ** |
| 4542 | ** Obtain a lock on a particular table. This instruction is only used when |
| 4543 | ** the shared-cache feature is enabled. |
| 4544 | ** |
drh | 6a9ad3d | 2008-04-02 16:29:30 +0000 | [diff] [blame] | 4545 | ** If P1 is the index of the database in sqlite3.aDb[] of the database |
| 4546 | ** on which the lock is acquired. A readlock is obtained if P3==0 or |
| 4547 | ** a write lock if P3==1. |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 4548 | ** |
| 4549 | ** P2 contains the root-page of the table to lock. |
| 4550 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 4551 | ** P4 contains a pointer to the name of the table being locked. This is only |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 4552 | ** used to generate an error message if the lock cannot be obtained. |
| 4553 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4554 | case OP_TableLock: { |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 4555 | int p1 = pOp->p1; |
drh | 6a9ad3d | 2008-04-02 16:29:30 +0000 | [diff] [blame] | 4556 | u8 isWriteLock = pOp->p3; |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 4557 | assert( p1>=0 && p1<db->nDb ); |
| 4558 | assert( (p->btreeMask & (1<<p1))!=0 ); |
drh | 6a9ad3d | 2008-04-02 16:29:30 +0000 | [diff] [blame] | 4559 | assert( isWriteLock==0 || isWriteLock==1 ); |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 4560 | rc = sqlite3BtreeLockTable(db->aDb[p1].pBt, pOp->p2, isWriteLock); |
| 4561 | if( rc==SQLITE_LOCKED ){ |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 4562 | const char *z = pOp->p4.z; |
drh | f089aa4 | 2008-07-08 19:34:06 +0000 | [diff] [blame] | 4563 | sqlite3SetString(&p->zErrMsg, db, "database table is locked: %s", z); |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 4564 | } |
| 4565 | break; |
| 4566 | } |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 4567 | #endif /* SQLITE_OMIT_SHARED_CACHE */ |
| 4568 | |
| 4569 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4570 | /* Opcode: VBegin * * * P4 * |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 4571 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 4572 | ** P4 a pointer to an sqlite3_vtab structure. Call the xBegin method |
danielk1977 | 78efaba | 2006-06-12 06:09:17 +0000 | [diff] [blame] | 4573 | ** for that table. |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 4574 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4575 | case OP_VBegin: { |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 4576 | rc = sqlite3VtabBegin(db, pOp->p4.pVtab); |
danielk1977 | f9e7dda | 2006-06-16 16:08:53 +0000 | [diff] [blame] | 4577 | break; |
| 4578 | } |
| 4579 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 4580 | |
| 4581 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4582 | /* Opcode: VCreate P1 * * P4 * |
danielk1977 | f9e7dda | 2006-06-16 16:08:53 +0000 | [diff] [blame] | 4583 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 4584 | ** P4 is the name of a virtual table in database P1. Call the xCreate method |
danielk1977 | f9e7dda | 2006-06-16 16:08:53 +0000 | [diff] [blame] | 4585 | ** for that table. |
| 4586 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4587 | case OP_VCreate: { |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 4588 | rc = sqlite3VtabCallCreate(db, pOp->p1, pOp->p4.z, &p->zErrMsg); |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 4589 | break; |
| 4590 | } |
| 4591 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 4592 | |
| 4593 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4594 | /* Opcode: VDestroy P1 * * P4 * |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 4595 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 4596 | ** P4 is the name of a virtual table in database P1. Call the xDestroy method |
danielk1977 | 9e39ce8 | 2006-06-12 16:01:21 +0000 | [diff] [blame] | 4597 | ** of that table. |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 4598 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4599 | case OP_VDestroy: { |
danielk1977 | 212b218 | 2006-06-23 14:32:08 +0000 | [diff] [blame] | 4600 | p->inVtabMethod = 2; |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 4601 | rc = sqlite3VtabCallDestroy(db, pOp->p1, pOp->p4.z); |
danielk1977 | 212b218 | 2006-06-23 14:32:08 +0000 | [diff] [blame] | 4602 | p->inVtabMethod = 0; |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 4603 | break; |
| 4604 | } |
| 4605 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 4606 | |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 4607 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4608 | /* Opcode: VOpen P1 * * P4 * |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 4609 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 4610 | ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure. |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 4611 | ** P1 is a cursor number. This opcode opens a cursor to the virtual |
| 4612 | ** table and stores that cursor in P1. |
| 4613 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4614 | case OP_VOpen: { |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 4615 | Cursor *pCur = 0; |
| 4616 | sqlite3_vtab_cursor *pVtabCursor = 0; |
| 4617 | |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 4618 | sqlite3_vtab *pVtab = pOp->p4.pVtab; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 4619 | sqlite3_module *pModule = (sqlite3_module *)pVtab->pModule; |
| 4620 | |
| 4621 | assert(pVtab && pModule); |
| 4622 | if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse; |
| 4623 | rc = pModule->xOpen(pVtab, &pVtabCursor); |
| 4624 | if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse; |
| 4625 | if( SQLITE_OK==rc ){ |
shane | 21e7feb | 2008-05-30 15:59:49 +0000 | [diff] [blame] | 4626 | /* Initialize sqlite3_vtab_cursor base class */ |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 4627 | pVtabCursor->pVtab = pVtab; |
| 4628 | |
| 4629 | /* Initialise vdbe cursor object */ |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 4630 | pCur = allocateCursor(p, pOp->p1, &pOp[-1], -1, 0); |
danielk1977 | be71889 | 2006-06-23 08:05:19 +0000 | [diff] [blame] | 4631 | if( pCur ){ |
| 4632 | pCur->pVtabCursor = pVtabCursor; |
| 4633 | pCur->pModule = pVtabCursor->pVtab->pModule; |
danielk1977 | b7a2f2e | 2006-06-23 11:34:54 +0000 | [diff] [blame] | 4634 | }else{ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 4635 | db->mallocFailed = 1; |
danielk1977 | b7a2f2e | 2006-06-23 11:34:54 +0000 | [diff] [blame] | 4636 | pModule->xClose(pVtabCursor); |
danielk1977 | be71889 | 2006-06-23 08:05:19 +0000 | [diff] [blame] | 4637 | } |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 4638 | } |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 4639 | break; |
| 4640 | } |
| 4641 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 4642 | |
| 4643 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
danielk1977 | 6dbee81 | 2008-01-03 18:39:41 +0000 | [diff] [blame] | 4644 | /* Opcode: VFilter P1 P2 P3 P4 * |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 4645 | ** |
| 4646 | ** P1 is a cursor opened using VOpen. P2 is an address to jump to if |
| 4647 | ** the filtered result set is empty. |
| 4648 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 4649 | ** P4 is either NULL or a string that was generated by the xBestIndex |
| 4650 | ** method of the module. The interpretation of the P4 string is left |
drh | 4be8b51 | 2006-06-13 23:51:34 +0000 | [diff] [blame] | 4651 | ** to the module implementation. |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 4652 | ** |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 4653 | ** This opcode invokes the xFilter method on the virtual table specified |
danielk1977 | 6dbee81 | 2008-01-03 18:39:41 +0000 | [diff] [blame] | 4654 | ** by P1. The integer query plan parameter to xFilter is stored in register |
| 4655 | ** P3. Register P3+1 stores the argc parameter to be passed to the |
drh | 174edc6 | 2008-05-29 05:23:41 +0000 | [diff] [blame] | 4656 | ** xFilter method. Registers P3+2..P3+1+argc are the argc |
| 4657 | ** additional parameters which are passed to |
danielk1977 | 6dbee81 | 2008-01-03 18:39:41 +0000 | [diff] [blame] | 4658 | ** xFilter as argv. Register P3+2 becomes argv[0] when passed to xFilter. |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 4659 | ** |
danielk1977 | 6dbee81 | 2008-01-03 18:39:41 +0000 | [diff] [blame] | 4660 | ** A jump is made to P2 if the result set after filtering would be empty. |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 4661 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4662 | case OP_VFilter: { /* jump */ |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 4663 | int nArg; |
danielk1977 | 6dbee81 | 2008-01-03 18:39:41 +0000 | [diff] [blame] | 4664 | int iQuery; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 4665 | const sqlite3_module *pModule; |
danielk1977 | 6dbee81 | 2008-01-03 18:39:41 +0000 | [diff] [blame] | 4666 | Mem *pQuery = &p->aMem[pOp->p3]; |
| 4667 | Mem *pArgc = &pQuery[1]; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 4668 | |
| 4669 | Cursor *pCur = p->apCsr[pOp->p1]; |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 4670 | |
| 4671 | REGISTER_TRACE(pOp->p3, pQuery); |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 4672 | assert( pCur->pVtabCursor ); |
| 4673 | pModule = pCur->pVtabCursor->pVtab->pModule; |
| 4674 | |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4675 | /* Grab the index number and argc parameters */ |
danielk1977 | 6dbee81 | 2008-01-03 18:39:41 +0000 | [diff] [blame] | 4676 | assert( (pQuery->flags&MEM_Int)!=0 && pArgc->flags==MEM_Int ); |
| 4677 | nArg = pArgc->u.i; |
| 4678 | iQuery = pQuery->u.i; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 4679 | |
drh | 644a529 | 2006-12-20 14:53:38 +0000 | [diff] [blame] | 4680 | /* Invoke the xFilter method */ |
| 4681 | { |
drh | 3f87d2a | 2006-12-20 14:31:24 +0000 | [diff] [blame] | 4682 | int res = 0; |
drh | 4be8b51 | 2006-06-13 23:51:34 +0000 | [diff] [blame] | 4683 | int i; |
| 4684 | Mem **apArg = p->apArg; |
| 4685 | for(i = 0; i<nArg; i++){ |
danielk1977 | 6dbee81 | 2008-01-03 18:39:41 +0000 | [diff] [blame] | 4686 | apArg[i] = &pArgc[i+1]; |
drh | 4be8b51 | 2006-06-13 23:51:34 +0000 | [diff] [blame] | 4687 | storeTypeInfo(apArg[i], 0); |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 4688 | } |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 4689 | |
| 4690 | if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse; |
danielk1977 | be71889 | 2006-06-23 08:05:19 +0000 | [diff] [blame] | 4691 | p->inVtabMethod = 1; |
danielk1977 | 6dbee81 | 2008-01-03 18:39:41 +0000 | [diff] [blame] | 4692 | rc = pModule->xFilter(pCur->pVtabCursor, iQuery, pOp->p4.z, nArg, apArg); |
danielk1977 | be71889 | 2006-06-23 08:05:19 +0000 | [diff] [blame] | 4693 | p->inVtabMethod = 0; |
danielk1977 | a298e90 | 2006-06-22 09:53:48 +0000 | [diff] [blame] | 4694 | if( rc==SQLITE_OK ){ |
| 4695 | res = pModule->xEof(pCur->pVtabCursor); |
| 4696 | } |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 4697 | if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse; |
| 4698 | |
danielk1977 | a298e90 | 2006-06-22 09:53:48 +0000 | [diff] [blame] | 4699 | if( res ){ |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 4700 | pc = pOp->p2 - 1; |
| 4701 | } |
| 4702 | } |
drh | 1d454a3 | 2008-01-31 19:34:51 +0000 | [diff] [blame] | 4703 | pCur->nullRow = 0; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 4704 | |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 4705 | break; |
| 4706 | } |
| 4707 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 4708 | |
| 4709 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4710 | /* Opcode: VRowid P1 P2 * * * |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 4711 | ** |
drh | 2133d82 | 2008-01-03 18:44:59 +0000 | [diff] [blame] | 4712 | ** Store into register P2 the rowid of |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 4713 | ** the virtual-table that the P1 cursor is pointing to. |
| 4714 | */ |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 4715 | case OP_VRowid: { /* out2-prerelease */ |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 4716 | const sqlite3_module *pModule; |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 4717 | sqlite_int64 iRow; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 4718 | Cursor *pCur = p->apCsr[pOp->p1]; |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 4719 | |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 4720 | assert( pCur->pVtabCursor ); |
drh | 2945b4a | 2008-01-31 15:53:45 +0000 | [diff] [blame] | 4721 | if( pCur->nullRow ){ |
| 4722 | break; |
| 4723 | } |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 4724 | pModule = pCur->pVtabCursor->pVtab->pModule; |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 4725 | assert( pModule->xRowid ); |
| 4726 | if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse; |
| 4727 | rc = pModule->xRowid(pCur->pVtabCursor, &iRow); |
| 4728 | if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 4729 | MemSetTypeFlag(pOut, MEM_Int); |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 4730 | pOut->u.i = iRow; |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 4731 | break; |
| 4732 | } |
| 4733 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 4734 | |
| 4735 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4736 | /* Opcode: VColumn P1 P2 P3 * * |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 4737 | ** |
drh | 2133d82 | 2008-01-03 18:44:59 +0000 | [diff] [blame] | 4738 | ** Store the value of the P2-th column of |
| 4739 | ** the row of the virtual-table that the |
| 4740 | ** P1 cursor is pointing to into register P3. |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 4741 | */ |
| 4742 | case OP_VColumn: { |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 4743 | const sqlite3_module *pModule; |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 4744 | Mem *pDest; |
| 4745 | sqlite3_context sContext; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 4746 | |
| 4747 | Cursor *pCur = p->apCsr[pOp->p1]; |
| 4748 | assert( pCur->pVtabCursor ); |
drh | 2945b4a | 2008-01-31 15:53:45 +0000 | [diff] [blame] | 4749 | assert( pOp->p3>0 && pOp->p3<=p->nMem ); |
| 4750 | pDest = &p->aMem[pOp->p3]; |
| 4751 | if( pCur->nullRow ){ |
| 4752 | sqlite3VdbeMemSetNull(pDest); |
| 4753 | break; |
| 4754 | } |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 4755 | pModule = pCur->pVtabCursor->pVtab->pModule; |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 4756 | assert( pModule->xColumn ); |
| 4757 | memset(&sContext, 0, sizeof(sContext)); |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 4758 | |
| 4759 | /* The output cell may already have a buffer allocated. Move |
| 4760 | ** the current contents to sContext.s so in case the user-function |
| 4761 | ** can use the already allocated buffer instead of allocating a |
| 4762 | ** new one. |
| 4763 | */ |
| 4764 | sqlite3VdbeMemMove(&sContext.s, pDest); |
| 4765 | MemSetTypeFlag(&sContext.s, MEM_Null); |
| 4766 | |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 4767 | if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse; |
| 4768 | rc = pModule->xColumn(pCur->pVtabCursor, &sContext, pOp->p2); |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 4769 | |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 4770 | /* Copy the result of the function to the P3 register. We |
| 4771 | ** do this regardless of whether or not an error occured to ensure any |
| 4772 | ** dynamic allocation in sContext.s (a Mem struct) is released. |
| 4773 | */ |
| 4774 | sqlite3VdbeChangeEncoding(&sContext.s, encoding); |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 4775 | REGISTER_TRACE(pOp->p3, pDest); |
| 4776 | sqlite3VdbeMemMove(pDest, &sContext.s); |
| 4777 | UPDATE_MAX_BLOBSIZE(pDest); |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 4778 | |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 4779 | if( sqlite3SafetyOn(db) ){ |
| 4780 | goto abort_due_to_misuse; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 4781 | } |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 4782 | if( sqlite3VdbeMemTooBig(pDest) ){ |
| 4783 | goto too_big; |
| 4784 | } |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 4785 | break; |
| 4786 | } |
| 4787 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 4788 | |
| 4789 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4790 | /* Opcode: VNext P1 P2 * * * |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 4791 | ** |
| 4792 | ** Advance virtual table P1 to the next row in its result set and |
| 4793 | ** jump to instruction P2. Or, if the virtual table has reached |
| 4794 | ** the end of its result set, then fall through to the next instruction. |
| 4795 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4796 | case OP_VNext: { /* jump */ |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 4797 | const sqlite3_module *pModule; |
| 4798 | int res = 0; |
| 4799 | |
| 4800 | Cursor *pCur = p->apCsr[pOp->p1]; |
| 4801 | assert( pCur->pVtabCursor ); |
drh | 2945b4a | 2008-01-31 15:53:45 +0000 | [diff] [blame] | 4802 | if( pCur->nullRow ){ |
| 4803 | break; |
| 4804 | } |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 4805 | pModule = pCur->pVtabCursor->pVtab->pModule; |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 4806 | assert( pModule->xNext ); |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 4807 | |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 4808 | /* Invoke the xNext() method of the module. There is no way for the |
| 4809 | ** underlying implementation to return an error if one occurs during |
| 4810 | ** xNext(). Instead, if an error occurs, true is returned (indicating that |
| 4811 | ** data is available) and the error code returned when xColumn or |
| 4812 | ** some other method is next invoked on the save virtual table cursor. |
| 4813 | */ |
| 4814 | if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse; |
| 4815 | p->inVtabMethod = 1; |
| 4816 | rc = pModule->xNext(pCur->pVtabCursor); |
| 4817 | p->inVtabMethod = 0; |
| 4818 | if( rc==SQLITE_OK ){ |
| 4819 | res = pModule->xEof(pCur->pVtabCursor); |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 4820 | } |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 4821 | if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 4822 | |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 4823 | if( !res ){ |
| 4824 | /* If there is data, jump to P2 */ |
| 4825 | pc = pOp->p2 - 1; |
| 4826 | } |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 4827 | break; |
| 4828 | } |
| 4829 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 4830 | |
danielk1977 | 182c4ba | 2007-06-27 15:53:34 +0000 | [diff] [blame] | 4831 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4832 | /* Opcode: VRename P1 * * P4 * |
danielk1977 | 182c4ba | 2007-06-27 15:53:34 +0000 | [diff] [blame] | 4833 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 4834 | ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure. |
danielk1977 | 182c4ba | 2007-06-27 15:53:34 +0000 | [diff] [blame] | 4835 | ** This opcode invokes the corresponding xRename method. The value |
danielk1977 | 6dbee81 | 2008-01-03 18:39:41 +0000 | [diff] [blame] | 4836 | ** in register P1 is passed as the zName argument to the xRename method. |
danielk1977 | 182c4ba | 2007-06-27 15:53:34 +0000 | [diff] [blame] | 4837 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4838 | case OP_VRename: { |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 4839 | sqlite3_vtab *pVtab = pOp->p4.pVtab; |
danielk1977 | 6dbee81 | 2008-01-03 18:39:41 +0000 | [diff] [blame] | 4840 | Mem *pName = &p->aMem[pOp->p1]; |
danielk1977 | 182c4ba | 2007-06-27 15:53:34 +0000 | [diff] [blame] | 4841 | assert( pVtab->pModule->xRename ); |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 4842 | REGISTER_TRACE(pOp->p1, pName); |
danielk1977 | 182c4ba | 2007-06-27 15:53:34 +0000 | [diff] [blame] | 4843 | |
danielk1977 | 6dbee81 | 2008-01-03 18:39:41 +0000 | [diff] [blame] | 4844 | Stringify(pName, encoding); |
danielk1977 | 182c4ba | 2007-06-27 15:53:34 +0000 | [diff] [blame] | 4845 | |
| 4846 | if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse; |
| 4847 | sqlite3VtabLock(pVtab); |
danielk1977 | 6dbee81 | 2008-01-03 18:39:41 +0000 | [diff] [blame] | 4848 | rc = pVtab->pModule->xRename(pVtab, pName->z); |
danielk1977 | 182c4ba | 2007-06-27 15:53:34 +0000 | [diff] [blame] | 4849 | sqlite3VtabUnlock(db, pVtab); |
| 4850 | if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse; |
| 4851 | |
danielk1977 | 182c4ba | 2007-06-27 15:53:34 +0000 | [diff] [blame] | 4852 | break; |
| 4853 | } |
| 4854 | #endif |
drh | 4cbdda9 | 2006-06-14 19:00:20 +0000 | [diff] [blame] | 4855 | |
| 4856 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4857 | /* Opcode: VUpdate P1 P2 P3 P4 * |
danielk1977 | 399918f | 2006-06-14 13:03:23 +0000 | [diff] [blame] | 4858 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 4859 | ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure. |
danielk1977 | 399918f | 2006-06-14 13:03:23 +0000 | [diff] [blame] | 4860 | ** This opcode invokes the corresponding xUpdate method. P2 values |
danielk1977 | 2a339ff | 2008-01-03 17:31:44 +0000 | [diff] [blame] | 4861 | ** are contiguous memory cells starting at P3 to pass to the xUpdate |
| 4862 | ** invocation. The value in register (P3+P2-1) corresponds to the |
| 4863 | ** p2th element of the argv array passed to xUpdate. |
drh | 4cbdda9 | 2006-06-14 19:00:20 +0000 | [diff] [blame] | 4864 | ** |
| 4865 | ** The xUpdate method will do a DELETE or an INSERT or both. |
danielk1977 | 2a339ff | 2008-01-03 17:31:44 +0000 | [diff] [blame] | 4866 | ** The argv[0] element (which corresponds to memory cell P3) |
| 4867 | ** is the rowid of a row to delete. If argv[0] is NULL then no |
| 4868 | ** deletion occurs. The argv[1] element is the rowid of the new |
| 4869 | ** row. This can be NULL to have the virtual table select the new |
| 4870 | ** rowid for itself. The subsequent elements in the array are |
| 4871 | ** the values of columns in the new row. |
drh | 4cbdda9 | 2006-06-14 19:00:20 +0000 | [diff] [blame] | 4872 | ** |
| 4873 | ** If P2==1 then no insert is performed. argv[0] is the rowid of |
| 4874 | ** a row to delete. |
danielk1977 | 1f6eec5 | 2006-06-16 06:17:47 +0000 | [diff] [blame] | 4875 | ** |
| 4876 | ** P1 is a boolean flag. If it is set to true and the xUpdate call |
| 4877 | ** is successful, then the value returned by sqlite3_last_insert_rowid() |
| 4878 | ** is set to the value of the rowid for the row just inserted. |
danielk1977 | 399918f | 2006-06-14 13:03:23 +0000 | [diff] [blame] | 4879 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4880 | case OP_VUpdate: { |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 4881 | sqlite3_vtab *pVtab = pOp->p4.pVtab; |
danielk1977 | 399918f | 2006-06-14 13:03:23 +0000 | [diff] [blame] | 4882 | sqlite3_module *pModule = (sqlite3_module *)pVtab->pModule; |
drh | 4cbdda9 | 2006-06-14 19:00:20 +0000 | [diff] [blame] | 4883 | int nArg = pOp->p2; |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 4884 | assert( pOp->p4type==P4_VTAB ); |
danielk1977 | 399918f | 2006-06-14 13:03:23 +0000 | [diff] [blame] | 4885 | if( pModule->xUpdate==0 ){ |
drh | f089aa4 | 2008-07-08 19:34:06 +0000 | [diff] [blame] | 4886 | sqlite3SetString(&p->zErrMsg, db, "read-only table"); |
danielk1977 | 399918f | 2006-06-14 13:03:23 +0000 | [diff] [blame] | 4887 | rc = SQLITE_ERROR; |
| 4888 | }else{ |
| 4889 | int i; |
danielk1977 | 1f6eec5 | 2006-06-16 06:17:47 +0000 | [diff] [blame] | 4890 | sqlite_int64 rowid; |
danielk1977 | 399918f | 2006-06-14 13:03:23 +0000 | [diff] [blame] | 4891 | Mem **apArg = p->apArg; |
danielk1977 | 2a339ff | 2008-01-03 17:31:44 +0000 | [diff] [blame] | 4892 | Mem *pX = &p->aMem[pOp->p3]; |
| 4893 | for(i=0; i<nArg; i++){ |
drh | 9c41938 | 2006-06-16 21:13:21 +0000 | [diff] [blame] | 4894 | storeTypeInfo(pX, 0); |
| 4895 | apArg[i] = pX; |
danielk1977 | 2a339ff | 2008-01-03 17:31:44 +0000 | [diff] [blame] | 4896 | pX++; |
danielk1977 | 399918f | 2006-06-14 13:03:23 +0000 | [diff] [blame] | 4897 | } |
danielk1977 | c7d5410 | 2006-06-15 07:29:00 +0000 | [diff] [blame] | 4898 | if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse; |
drh | 189d4af | 2006-09-02 20:57:52 +0000 | [diff] [blame] | 4899 | sqlite3VtabLock(pVtab); |
danielk1977 | 1f6eec5 | 2006-06-16 06:17:47 +0000 | [diff] [blame] | 4900 | rc = pModule->xUpdate(pVtab, nArg, apArg, &rowid); |
danielk1977 | a04a34f | 2007-04-16 15:06:25 +0000 | [diff] [blame] | 4901 | sqlite3VtabUnlock(db, pVtab); |
danielk1977 | c7d5410 | 2006-06-15 07:29:00 +0000 | [diff] [blame] | 4902 | if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse; |
danielk1977 | 1f6eec5 | 2006-06-16 06:17:47 +0000 | [diff] [blame] | 4903 | if( pOp->p1 && rc==SQLITE_OK ){ |
| 4904 | assert( nArg>1 && apArg[0] && (apArg[0]->flags&MEM_Null) ); |
| 4905 | db->lastRowid = rowid; |
| 4906 | } |
drh | b5df144 | 2008-04-10 14:00:09 +0000 | [diff] [blame] | 4907 | p->nChange++; |
danielk1977 | 399918f | 2006-06-14 13:03:23 +0000 | [diff] [blame] | 4908 | } |
drh | 4cbdda9 | 2006-06-14 19:00:20 +0000 | [diff] [blame] | 4909 | break; |
danielk1977 | 399918f | 2006-06-14 13:03:23 +0000 | [diff] [blame] | 4910 | } |
| 4911 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 4912 | |
danielk1977 | 59a9379 | 2008-05-15 17:48:20 +0000 | [diff] [blame] | 4913 | #ifndef SQLITE_OMIT_PAGER_PRAGMAS |
| 4914 | /* Opcode: Pagecount P1 P2 * * * |
| 4915 | ** |
| 4916 | ** Write the current number of pages in database P1 to memory cell P2. |
| 4917 | */ |
| 4918 | case OP_Pagecount: { /* out2-prerelease */ |
| 4919 | int p1 = pOp->p1; |
| 4920 | int nPage; |
| 4921 | Pager *pPager = sqlite3BtreePager(db->aDb[p1].pBt); |
| 4922 | |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame] | 4923 | rc = sqlite3PagerPagecount(pPager, &nPage); |
| 4924 | if( rc==SQLITE_OK ){ |
danielk1977 | 59a9379 | 2008-05-15 17:48:20 +0000 | [diff] [blame] | 4925 | pOut->flags = MEM_Int; |
| 4926 | pOut->u.i = nPage; |
| 4927 | } |
| 4928 | break; |
| 4929 | } |
| 4930 | #endif |
| 4931 | |
drh | 949f9cd | 2008-01-12 21:35:57 +0000 | [diff] [blame] | 4932 | #ifndef SQLITE_OMIT_TRACE |
| 4933 | /* Opcode: Trace * * * P4 * |
| 4934 | ** |
| 4935 | ** If tracing is enabled (by the sqlite3_trace()) interface, then |
| 4936 | ** the UTF-8 string contained in P4 is emitted on the trace callback. |
| 4937 | */ |
| 4938 | case OP_Trace: { |
| 4939 | if( pOp->p4.z ){ |
| 4940 | if( db->xTrace ){ |
| 4941 | db->xTrace(db->pTraceArg, pOp->p4.z); |
| 4942 | } |
| 4943 | #ifdef SQLITE_DEBUG |
| 4944 | if( (db->flags & SQLITE_SqlTrace)!=0 ){ |
| 4945 | sqlite3DebugPrintf("SQL-trace: %s\n", pOp->p4.z); |
| 4946 | } |
| 4947 | #endif /* SQLITE_DEBUG */ |
| 4948 | } |
| 4949 | break; |
| 4950 | } |
| 4951 | #endif |
| 4952 | |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 4953 | |
| 4954 | /* Opcode: Noop * * * * * |
| 4955 | ** |
| 4956 | ** Do nothing. This instruction is often useful as a jump |
| 4957 | ** destination. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4958 | */ |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 4959 | /* |
| 4960 | ** The magic Explain opcode are only inserted when explain==2 (which |
| 4961 | ** is to say when the EXPLAIN QUERY PLAN syntax is used.) |
| 4962 | ** This opcode records information from the optimizer. It is the |
| 4963 | ** the same as a no-op. This opcodesnever appears in a real VM program. |
| 4964 | */ |
| 4965 | default: { /* This is really OP_Noop and OP_Explain */ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4966 | break; |
| 4967 | } |
| 4968 | |
| 4969 | /***************************************************************************** |
| 4970 | ** The cases of the switch statement above this line should all be indented |
| 4971 | ** by 6 spaces. But the left-most 6 spaces have been removed to improve the |
| 4972 | ** readability. From this point on down, the normal indentation rules are |
| 4973 | ** restored. |
| 4974 | *****************************************************************************/ |
| 4975 | } |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 4976 | |
drh | 7b39686 | 2003-01-01 23:06:20 +0000 | [diff] [blame] | 4977 | #ifdef VDBE_PROFILE |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 4978 | { |
shane | 9bcbdad | 2008-05-29 20:22:37 +0000 | [diff] [blame] | 4979 | u64 elapsed = sqlite3Hwtime() - start; |
| 4980 | pOp->cycles += elapsed; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 4981 | pOp->cnt++; |
| 4982 | #if 0 |
shane | 9bcbdad | 2008-05-29 20:22:37 +0000 | [diff] [blame] | 4983 | fprintf(stdout, "%10llu ", elapsed); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 4984 | sqlite3VdbePrintOp(stdout, origPc, &p->aOp[origPc]); |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 4985 | #endif |
| 4986 | } |
drh | 7b39686 | 2003-01-01 23:06:20 +0000 | [diff] [blame] | 4987 | #endif |
| 4988 | |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 4989 | /* The following code adds nothing to the actual functionality |
| 4990 | ** of the program. It is only here for testing and debugging. |
| 4991 | ** On the other hand, it does burn CPU cycles every time through |
| 4992 | ** the evaluator loop. So we can leave it out when NDEBUG is defined. |
| 4993 | */ |
| 4994 | #ifndef NDEBUG |
drh | a611040 | 2005-07-28 20:51:19 +0000 | [diff] [blame] | 4995 | assert( pc>=-1 && pc<p->nOp ); |
drh | ae7e151 | 2007-05-02 16:51:59 +0000 | [diff] [blame] | 4996 | |
drh | cf1023c | 2007-05-08 20:59:49 +0000 | [diff] [blame] | 4997 | #ifdef SQLITE_DEBUG |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 4998 | if( p->trace ){ |
| 4999 | if( rc!=0 ) fprintf(p->trace,"rc=%d\n",rc); |
drh | ca48c90 | 2008-01-18 14:08:24 +0000 | [diff] [blame] | 5000 | if( opProperty & OPFLG_OUT2_PRERELEASE ){ |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 5001 | registerTrace(p->trace, pOp->p2, pOut); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5002 | } |
drh | ca48c90 | 2008-01-18 14:08:24 +0000 | [diff] [blame] | 5003 | if( opProperty & OPFLG_OUT3 ){ |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 5004 | registerTrace(p->trace, pOp->p3, pOut); |
| 5005 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5006 | } |
danielk1977 | b5402fb | 2005-01-12 07:15:04 +0000 | [diff] [blame] | 5007 | #endif /* SQLITE_DEBUG */ |
| 5008 | #endif /* NDEBUG */ |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 5009 | } /* The end of the for(;;) loop the loops through opcodes */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5010 | |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 5011 | /* If we reach this point, it means that execution is finished with |
| 5012 | ** an error of some kind. |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 5013 | */ |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 5014 | vdbe_error_halt: |
| 5015 | assert( rc ); |
| 5016 | p->rc = rc; |
drh | 92f02c3 | 2004-09-02 14:57:08 +0000 | [diff] [blame] | 5017 | sqlite3VdbeHalt(p); |
danielk1977 | 7eaabcd | 2008-07-07 14:56:56 +0000 | [diff] [blame] | 5018 | if( rc==SQLITE_IOERR_NOMEM ) db->mallocFailed = 1; |
| 5019 | rc = SQLITE_ERROR; |
drh | 900b31e | 2007-08-28 02:27:51 +0000 | [diff] [blame] | 5020 | |
| 5021 | /* This is the only way out of this procedure. We have to |
| 5022 | ** release the mutexes on btrees that were acquired at the |
| 5023 | ** top. */ |
| 5024 | vdbe_return: |
drh | 4cf7c7f | 2007-08-28 23:28:07 +0000 | [diff] [blame] | 5025 | sqlite3BtreeMutexArrayLeave(&p->aMutex); |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 5026 | return rc; |
| 5027 | |
drh | 023ae03 | 2007-05-08 12:12:16 +0000 | [diff] [blame] | 5028 | /* Jump to here if a string or blob larger than SQLITE_MAX_LENGTH |
| 5029 | ** is encountered. |
| 5030 | */ |
| 5031 | too_big: |
drh | f089aa4 | 2008-07-08 19:34:06 +0000 | [diff] [blame] | 5032 | sqlite3SetString(&p->zErrMsg, db, "string or blob too big"); |
drh | 023ae03 | 2007-05-08 12:12:16 +0000 | [diff] [blame] | 5033 | rc = SQLITE_TOOBIG; |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 5034 | goto vdbe_error_halt; |
drh | 023ae03 | 2007-05-08 12:12:16 +0000 | [diff] [blame] | 5035 | |
drh | 98640a3 | 2007-06-07 19:08:32 +0000 | [diff] [blame] | 5036 | /* Jump to here if a malloc() fails. |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 5037 | */ |
| 5038 | no_mem: |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 5039 | db->mallocFailed = 1; |
drh | f089aa4 | 2008-07-08 19:34:06 +0000 | [diff] [blame] | 5040 | sqlite3SetString(&p->zErrMsg, db, "out of memory"); |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 5041 | rc = SQLITE_NOMEM; |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 5042 | goto vdbe_error_halt; |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 5043 | |
| 5044 | /* Jump to here for an SQLITE_MISUSE error. |
| 5045 | */ |
| 5046 | abort_due_to_misuse: |
| 5047 | rc = SQLITE_MISUSE; |
| 5048 | /* Fall thru into abort_due_to_error */ |
| 5049 | |
| 5050 | /* Jump to here for any other kind of fatal error. The "rc" variable |
| 5051 | ** should hold the error number. |
| 5052 | */ |
| 5053 | abort_due_to_error: |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 5054 | assert( p->zErrMsg==0 ); |
| 5055 | if( db->mallocFailed ) rc = SQLITE_NOMEM; |
danielk1977 | 7eaabcd | 2008-07-07 14:56:56 +0000 | [diff] [blame] | 5056 | if( rc!=SQLITE_IOERR_NOMEM ){ |
drh | f089aa4 | 2008-07-08 19:34:06 +0000 | [diff] [blame] | 5057 | sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3ErrStr(rc)); |
danielk1977 | 7eaabcd | 2008-07-07 14:56:56 +0000 | [diff] [blame] | 5058 | } |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 5059 | goto vdbe_error_halt; |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 5060 | |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 5061 | /* Jump to here if the sqlite3_interrupt() API sets the interrupt |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 5062 | ** flag. |
| 5063 | */ |
| 5064 | abort_due_to_interrupt: |
drh | 881feaa | 2006-07-26 01:39:30 +0000 | [diff] [blame] | 5065 | assert( db->u1.isInterrupted ); |
drh | 7e8b848 | 2008-01-23 03:03:05 +0000 | [diff] [blame] | 5066 | rc = SQLITE_INTERRUPT; |
danielk1977 | 026d270 | 2004-06-14 13:14:59 +0000 | [diff] [blame] | 5067 | p->rc = rc; |
drh | f089aa4 | 2008-07-08 19:34:06 +0000 | [diff] [blame] | 5068 | sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3ErrStr(rc)); |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 5069 | goto vdbe_error_halt; |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 5070 | } |