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. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 45 | */ |
| 46 | #include "sqliteInt.h" |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 47 | #include "vdbeInt.h" |
drh | 8f619cc | 2002-09-08 00:04:50 +0000 | [diff] [blame] | 48 | |
| 49 | /* |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 50 | ** Invoke this macro on memory cells just prior to changing the |
| 51 | ** value of the cell. This macro verifies that shallow copies are |
| 52 | ** not misused. |
| 53 | */ |
| 54 | #ifdef SQLITE_DEBUG |
| 55 | # define memAboutToChange(P,M) sqlite3VdbeMemPrepareToChange(P,M) |
| 56 | #else |
| 57 | # define memAboutToChange(P,M) |
| 58 | #endif |
| 59 | |
| 60 | /* |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 61 | ** The following global variable is incremented every time a cursor |
drh | 959403f | 2008-12-12 17:56:16 +0000 | [diff] [blame] | 62 | ** moves, either by the OP_SeekXX, OP_Next, or OP_Prev opcodes. The test |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 63 | ** procedures use this information to make sure that indices are |
drh | ac82fcf | 2002-09-08 17:23:41 +0000 | [diff] [blame] | 64 | ** working correctly. This variable has no function other than to |
| 65 | ** help verify the correct operation of the library. |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 66 | */ |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 67 | #ifdef SQLITE_TEST |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 68 | int sqlite3_search_count = 0; |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 69 | #endif |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 70 | |
drh | f603871 | 2004-02-08 18:07:34 +0000 | [diff] [blame] | 71 | /* |
| 72 | ** When this global variable is positive, it gets decremented once before |
drh | 881feaa | 2006-07-26 01:39:30 +0000 | [diff] [blame] | 73 | ** each instruction in the VDBE. When reaches zero, the u1.isInterrupted |
| 74 | ** field of the sqlite3 structure is set in order to simulate and interrupt. |
drh | f603871 | 2004-02-08 18:07:34 +0000 | [diff] [blame] | 75 | ** |
| 76 | ** This facility is used for testing purposes only. It does not function |
| 77 | ** in an ordinary build. |
| 78 | */ |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 79 | #ifdef SQLITE_TEST |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 80 | int sqlite3_interrupt_count = 0; |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 81 | #endif |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 82 | |
danielk1977 | 7e18c25 | 2004-05-25 11:47:24 +0000 | [diff] [blame] | 83 | /* |
drh | 6bf8957 | 2004-11-03 16:27:01 +0000 | [diff] [blame] | 84 | ** The next global variable is incremented each type the OP_Sort opcode |
| 85 | ** is executed. The test procedures use this information to make sure that |
shane | 21e7feb | 2008-05-30 15:59:49 +0000 | [diff] [blame] | 86 | ** sorting is occurring or not occurring at appropriate times. This variable |
drh | 6bf8957 | 2004-11-03 16:27:01 +0000 | [diff] [blame] | 87 | ** has no function other than to help verify the correct operation of the |
| 88 | ** library. |
| 89 | */ |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 90 | #ifdef SQLITE_TEST |
drh | 6bf8957 | 2004-11-03 16:27:01 +0000 | [diff] [blame] | 91 | int sqlite3_sort_count = 0; |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 92 | #endif |
drh | 6bf8957 | 2004-11-03 16:27:01 +0000 | [diff] [blame] | 93 | |
| 94 | /* |
drh | ae7e151 | 2007-05-02 16:51:59 +0000 | [diff] [blame] | 95 | ** The next global variable records the size of the largest MEM_Blob |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 96 | ** 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] | 97 | ** use this information to make sure that the zero-blob functionality |
| 98 | ** is working correctly. This variable has no function other than to |
| 99 | ** help verify the correct operation of the library. |
| 100 | */ |
| 101 | #ifdef SQLITE_TEST |
| 102 | int sqlite3_max_blobsize = 0; |
drh | ca48c90 | 2008-01-18 14:08:24 +0000 | [diff] [blame] | 103 | static void updateMaxBlobsize(Mem *p){ |
| 104 | if( (p->flags & (MEM_Str|MEM_Blob))!=0 && p->n>sqlite3_max_blobsize ){ |
| 105 | sqlite3_max_blobsize = p->n; |
| 106 | } |
| 107 | } |
drh | ae7e151 | 2007-05-02 16:51:59 +0000 | [diff] [blame] | 108 | #endif |
| 109 | |
| 110 | /* |
dan | 0ff297e | 2009-09-25 17:03:14 +0000 | [diff] [blame] | 111 | ** The next global variable is incremented each type the OP_Found opcode |
| 112 | ** is executed. This is used to test whether or not the foreign key |
| 113 | ** operation implemented using OP_FkIsZero is working. This variable |
| 114 | ** has no function other than to help verify the correct operation of the |
| 115 | ** library. |
| 116 | */ |
| 117 | #ifdef SQLITE_TEST |
| 118 | int sqlite3_found_count = 0; |
| 119 | #endif |
| 120 | |
| 121 | /* |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 122 | ** Test a register to see if it exceeds the current maximum blob size. |
| 123 | ** If it does, record the new maximum blob size. |
| 124 | */ |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 125 | #if defined(SQLITE_TEST) && !defined(SQLITE_OMIT_BUILTIN_TEST) |
drh | ca48c90 | 2008-01-18 14:08:24 +0000 | [diff] [blame] | 126 | # define UPDATE_MAX_BLOBSIZE(P) updateMaxBlobsize(P) |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 127 | #else |
| 128 | # define UPDATE_MAX_BLOBSIZE(P) |
| 129 | #endif |
| 130 | |
| 131 | /* |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 132 | ** Convert the given register into a string if it isn't one |
danielk1977 | bd7e460 | 2004-05-24 07:34:48 +0000 | [diff] [blame] | 133 | ** already. Return non-zero if a malloc() fails. |
| 134 | */ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 135 | #define Stringify(P, enc) \ |
| 136 | if(((P)->flags&(MEM_Str|MEM_Blob))==0 && sqlite3VdbeMemStringify(P,enc)) \ |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 137 | { goto no_mem; } |
danielk1977 | bd7e460 | 2004-05-24 07:34:48 +0000 | [diff] [blame] | 138 | |
| 139 | /* |
danielk1977 | bd7e460 | 2004-05-24 07:34:48 +0000 | [diff] [blame] | 140 | ** An ephemeral string value (signified by the MEM_Ephem flag) contains |
| 141 | ** a pointer to a dynamically allocated string where some other entity |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 142 | ** is responsible for deallocating that string. Because the register |
| 143 | ** does not control the string, it might be deleted without the register |
| 144 | ** knowing it. |
danielk1977 | bd7e460 | 2004-05-24 07:34:48 +0000 | [diff] [blame] | 145 | ** |
| 146 | ** This routine converts an ephemeral string into a dynamically allocated |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 147 | ** string that the register itself controls. In other words, it |
danielk1977 | bd7e460 | 2004-05-24 07:34:48 +0000 | [diff] [blame] | 148 | ** converts an MEM_Ephem string into an MEM_Dyn string. |
| 149 | */ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 150 | #define Deephemeralize(P) \ |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 151 | if( ((P)->flags&MEM_Ephem)!=0 \ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 152 | && sqlite3VdbeMemMakeWriteable(P) ){ goto no_mem;} |
danielk1977 | 93d4675 | 2004-05-23 13:30:58 +0000 | [diff] [blame] | 153 | |
| 154 | /* |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 155 | ** Call sqlite3VdbeMemExpandBlob() on the supplied value (type Mem*) |
| 156 | ** P if required. |
| 157 | */ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 158 | #define ExpandBlob(P) (((P)->flags&MEM_Zero)?sqlite3VdbeMemExpandBlob(P):0) |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 159 | |
dan | 689ab89 | 2011-08-12 15:02:00 +0000 | [diff] [blame] | 160 | /* Return true if the cursor was opened using the OP_OpenSorter opcode. */ |
| 161 | #ifdef SQLITE_OMIT_MERGE_SORT |
| 162 | # define isSorter(x) 0 |
| 163 | #else |
| 164 | # define isSorter(x) ((x)->pSorter!=0) |
| 165 | #endif |
| 166 | |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 167 | /* |
shane | 21e7feb | 2008-05-30 15:59:49 +0000 | [diff] [blame] | 168 | ** Argument pMem points at a register that will be passed to a |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 169 | ** user-defined function or returned to the user as the result of a query. |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 170 | ** This routine sets the pMem->type variable used by the sqlite3_value_*() |
| 171 | ** routines. |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 172 | */ |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 173 | void sqlite3VdbeMemStoreType(Mem *pMem){ |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 174 | int flags = pMem->flags; |
| 175 | if( flags & MEM_Null ){ |
drh | 9c05483 | 2004-05-31 18:51:57 +0000 | [diff] [blame] | 176 | pMem->type = SQLITE_NULL; |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 177 | } |
| 178 | else if( flags & MEM_Int ){ |
drh | 9c05483 | 2004-05-31 18:51:57 +0000 | [diff] [blame] | 179 | pMem->type = SQLITE_INTEGER; |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 180 | } |
| 181 | else if( flags & MEM_Real ){ |
drh | 9c05483 | 2004-05-31 18:51:57 +0000 | [diff] [blame] | 182 | pMem->type = SQLITE_FLOAT; |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 183 | } |
| 184 | else if( flags & MEM_Str ){ |
drh | 9c05483 | 2004-05-31 18:51:57 +0000 | [diff] [blame] | 185 | pMem->type = SQLITE_TEXT; |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 186 | }else{ |
drh | 9c05483 | 2004-05-31 18:51:57 +0000 | [diff] [blame] | 187 | pMem->type = SQLITE_BLOB; |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 188 | } |
| 189 | } |
danielk1977 | 8a6b541 | 2004-05-24 07:04:25 +0000 | [diff] [blame] | 190 | |
| 191 | /* |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 192 | ** Allocate VdbeCursor number iCur. Return a pointer to it. Return NULL |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 193 | ** if we run out of memory. |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 194 | */ |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 195 | static VdbeCursor *allocateCursor( |
| 196 | Vdbe *p, /* The virtual machine */ |
| 197 | int iCur, /* Index of the new VdbeCursor */ |
danielk1977 | d336e22 | 2009-02-20 10:58:41 +0000 | [diff] [blame] | 198 | int nField, /* Number of fields in the table or index */ |
drh | 3d4501e | 2008-12-04 20:40:10 +0000 | [diff] [blame] | 199 | int iDb, /* When database the cursor belongs to, or -1 */ |
drh | 3e9ca09 | 2009-09-08 01:14:48 +0000 | [diff] [blame] | 200 | int isBtreeCursor /* True for B-Tree. False for pseudo-table or vtab */ |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 201 | ){ |
| 202 | /* Find the memory cell that will be used to store the blob of memory |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 203 | ** required for this VdbeCursor structure. It is convenient to use a |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 204 | ** vdbe memory cell to manage the memory allocation required for a |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 205 | ** VdbeCursor structure for the following reasons: |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 206 | ** |
| 207 | ** * Sometimes cursor numbers are used for a couple of different |
| 208 | ** purposes in a vdbe program. The different uses might require |
| 209 | ** different sized allocations. Memory cells provide growable |
| 210 | ** allocations. |
| 211 | ** |
| 212 | ** * When using ENABLE_MEMORY_MANAGEMENT, memory cell buffers can |
| 213 | ** be freed lazily via the sqlite3_release_memory() API. This |
| 214 | ** minimizes the number of malloc calls made by the system. |
| 215 | ** |
| 216 | ** Memory cells for cursors are allocated at the top of the address |
| 217 | ** space. Memory cell (p->nMem) corresponds to cursor 0. Space for |
| 218 | ** cursor 1 is managed by memory cell (p->nMem-1), etc. |
| 219 | */ |
| 220 | Mem *pMem = &p->aMem[p->nMem-iCur]; |
| 221 | |
danielk1977 | 5f09613 | 2008-03-28 15:44:09 +0000 | [diff] [blame] | 222 | int nByte; |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 223 | VdbeCursor *pCx = 0; |
danielk1977 | 5f09613 | 2008-03-28 15:44:09 +0000 | [diff] [blame] | 224 | nByte = |
drh | c54055b | 2009-11-13 17:05:53 +0000 | [diff] [blame] | 225 | ROUND8(sizeof(VdbeCursor)) + |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 226 | (isBtreeCursor?sqlite3BtreeCursorSize():0) + |
| 227 | 2*nField*sizeof(u32); |
| 228 | |
drh | 290c194 | 2004-08-21 17:54:45 +0000 | [diff] [blame] | 229 | assert( iCur<p->nCursor ); |
| 230 | if( p->apCsr[iCur] ){ |
danielk1977 | be71889 | 2006-06-23 08:05:19 +0000 | [diff] [blame] | 231 | sqlite3VdbeFreeCursor(p, p->apCsr[iCur]); |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 232 | p->apCsr[iCur] = 0; |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 233 | } |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 234 | if( SQLITE_OK==sqlite3VdbeMemGrow(pMem, nByte, 0) ){ |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 235 | p->apCsr[iCur] = pCx = (VdbeCursor*)pMem->z; |
drh | f25a507 | 2009-11-18 23:01:25 +0000 | [diff] [blame] | 236 | memset(pCx, 0, sizeof(VdbeCursor)); |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 +0000 | [diff] [blame] | 237 | pCx->iDb = iDb; |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 238 | pCx->nField = nField; |
| 239 | if( nField ){ |
drh | c54055b | 2009-11-13 17:05:53 +0000 | [diff] [blame] | 240 | pCx->aType = (u32 *)&pMem->z[ROUND8(sizeof(VdbeCursor))]; |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 241 | } |
| 242 | if( isBtreeCursor ){ |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 243 | pCx->pCursor = (BtCursor*) |
drh | c54055b | 2009-11-13 17:05:53 +0000 | [diff] [blame] | 244 | &pMem->z[ROUND8(sizeof(VdbeCursor))+2*nField*sizeof(u32)]; |
drh | f25a507 | 2009-11-18 23:01:25 +0000 | [diff] [blame] | 245 | sqlite3BtreeCursorZero(pCx->pCursor); |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 246 | } |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 +0000 | [diff] [blame] | 247 | } |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 248 | return pCx; |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 249 | } |
| 250 | |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 251 | /* |
drh | 29d7210 | 2006-02-09 22:13:41 +0000 | [diff] [blame] | 252 | ** Try to convert a value into a numeric representation if we can |
| 253 | ** do so without loss of information. In other words, if the string |
| 254 | ** looks like a number, convert it into a number. If it does not |
| 255 | ** look like a number, leave it alone. |
| 256 | */ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 257 | static void applyNumericAffinity(Mem *pRec){ |
drh | 29d7210 | 2006-02-09 22:13:41 +0000 | [diff] [blame] | 258 | if( (pRec->flags & (MEM_Real|MEM_Int))==0 ){ |
drh | 9339da1 | 2010-09-30 00:50:49 +0000 | [diff] [blame] | 259 | double rValue; |
| 260 | i64 iValue; |
dan | b7dca7d | 2010-03-05 16:32:12 +0000 | [diff] [blame] | 261 | u8 enc = pRec->enc; |
drh | 9339da1 | 2010-09-30 00:50:49 +0000 | [diff] [blame] | 262 | if( (pRec->flags&MEM_Str)==0 ) return; |
| 263 | if( sqlite3AtoF(pRec->z, &rValue, pRec->n, enc)==0 ) return; |
shaneh | 5f1d6b6 | 2010-09-30 16:51:25 +0000 | [diff] [blame] | 264 | if( 0==sqlite3Atoi64(pRec->z, &iValue, pRec->n, enc) ){ |
drh | 9339da1 | 2010-09-30 00:50:49 +0000 | [diff] [blame] | 265 | pRec->u.i = iValue; |
| 266 | pRec->flags |= MEM_Int; |
| 267 | }else{ |
| 268 | pRec->r = rValue; |
| 269 | pRec->flags |= MEM_Real; |
drh | 29d7210 | 2006-02-09 22:13:41 +0000 | [diff] [blame] | 270 | } |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | /* |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 275 | ** Processing is determine by the affinity parameter: |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 276 | ** |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 277 | ** SQLITE_AFF_INTEGER: |
| 278 | ** SQLITE_AFF_REAL: |
| 279 | ** SQLITE_AFF_NUMERIC: |
| 280 | ** Try to convert pRec to an integer representation or a |
| 281 | ** floating-point representation if an integer representation |
| 282 | ** is not possible. Note that the integer representation is |
| 283 | ** always preferred, even if the affinity is REAL, because |
| 284 | ** an integer representation is more space efficient on disk. |
| 285 | ** |
| 286 | ** SQLITE_AFF_TEXT: |
| 287 | ** Convert pRec to a text representation. |
| 288 | ** |
| 289 | ** SQLITE_AFF_NONE: |
| 290 | ** No-op. pRec is unchanged. |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 291 | */ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 292 | static void applyAffinity( |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 293 | Mem *pRec, /* The value to apply affinity to */ |
| 294 | char affinity, /* The affinity to be applied */ |
| 295 | u8 enc /* Use this text encoding */ |
| 296 | ){ |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 297 | if( affinity==SQLITE_AFF_TEXT ){ |
drh | 17c4029 | 2004-07-21 02:53:29 +0000 | [diff] [blame] | 298 | /* Only attempt the conversion to TEXT if there is an integer or real |
| 299 | ** representation (blob and NULL do not get converted) but no string |
| 300 | ** representation. |
| 301 | */ |
| 302 | if( 0==(pRec->flags&MEM_Str) && (pRec->flags&(MEM_Real|MEM_Int)) ){ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 303 | sqlite3VdbeMemStringify(pRec, enc); |
drh | 17c4029 | 2004-07-21 02:53:29 +0000 | [diff] [blame] | 304 | } |
| 305 | pRec->flags &= ~(MEM_Real|MEM_Int); |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 306 | }else if( affinity!=SQLITE_AFF_NONE ){ |
| 307 | assert( affinity==SQLITE_AFF_INTEGER || affinity==SQLITE_AFF_REAL |
| 308 | || affinity==SQLITE_AFF_NUMERIC ); |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 309 | applyNumericAffinity(pRec); |
drh | 29d7210 | 2006-02-09 22:13:41 +0000 | [diff] [blame] | 310 | if( pRec->flags & MEM_Real ){ |
drh | 8df447f | 2005-11-01 15:48:24 +0000 | [diff] [blame] | 311 | sqlite3VdbeIntegerAffinity(pRec); |
drh | 17c4029 | 2004-07-21 02:53:29 +0000 | [diff] [blame] | 312 | } |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 313 | } |
| 314 | } |
| 315 | |
danielk1977 | aee18ef | 2005-03-09 12:26:50 +0000 | [diff] [blame] | 316 | /* |
drh | 29d7210 | 2006-02-09 22:13:41 +0000 | [diff] [blame] | 317 | ** Try to convert the type of a function argument or a result column |
| 318 | ** into a numeric representation. Use either INTEGER or REAL whichever |
| 319 | ** is appropriate. But only do the conversion if it is possible without |
| 320 | ** loss of information and return the revised type of the argument. |
drh | 29d7210 | 2006-02-09 22:13:41 +0000 | [diff] [blame] | 321 | */ |
| 322 | int sqlite3_value_numeric_type(sqlite3_value *pVal){ |
| 323 | Mem *pMem = (Mem*)pVal; |
drh | e5a8a1d | 2010-11-18 12:31:24 +0000 | [diff] [blame] | 324 | if( pMem->type==SQLITE_TEXT ){ |
| 325 | applyNumericAffinity(pMem); |
| 326 | sqlite3VdbeMemStoreType(pMem); |
| 327 | } |
drh | 29d7210 | 2006-02-09 22:13:41 +0000 | [diff] [blame] | 328 | return pMem->type; |
| 329 | } |
| 330 | |
| 331 | /* |
danielk1977 | aee18ef | 2005-03-09 12:26:50 +0000 | [diff] [blame] | 332 | ** Exported version of applyAffinity(). This one works on sqlite3_value*, |
| 333 | ** not the internal Mem* type. |
| 334 | */ |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 335 | void sqlite3ValueApplyAffinity( |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 336 | sqlite3_value *pVal, |
| 337 | u8 affinity, |
| 338 | u8 enc |
| 339 | ){ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 340 | applyAffinity((Mem *)pVal, affinity, enc); |
danielk1977 | aee18ef | 2005-03-09 12:26:50 +0000 | [diff] [blame] | 341 | } |
| 342 | |
danielk1977 | b5402fb | 2005-01-12 07:15:04 +0000 | [diff] [blame] | 343 | #ifdef SQLITE_DEBUG |
drh | b6f5452 | 2004-05-20 02:42:16 +0000 | [diff] [blame] | 344 | /* |
danielk1977 | ca6b291 | 2004-05-21 10:49:47 +0000 | [diff] [blame] | 345 | ** Write a nice string representation of the contents of cell pMem |
| 346 | ** into buffer zBuf, length nBuf. |
| 347 | */ |
drh | 7416170 | 2006-02-24 02:53:49 +0000 | [diff] [blame] | 348 | void sqlite3VdbeMemPrettyPrint(Mem *pMem, char *zBuf){ |
danielk1977 | ca6b291 | 2004-05-21 10:49:47 +0000 | [diff] [blame] | 349 | char *zCsr = zBuf; |
| 350 | int f = pMem->flags; |
| 351 | |
drh | 5719628 | 2004-10-06 15:41:16 +0000 | [diff] [blame] | 352 | static const char *const encnames[] = {"(X)", "(8)", "(16LE)", "(16BE)"}; |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame] | 353 | |
danielk1977 | ca6b291 | 2004-05-21 10:49:47 +0000 | [diff] [blame] | 354 | if( f&MEM_Blob ){ |
| 355 | int i; |
| 356 | char c; |
| 357 | if( f & MEM_Dyn ){ |
| 358 | c = 'z'; |
| 359 | assert( (f & (MEM_Static|MEM_Ephem))==0 ); |
| 360 | }else if( f & MEM_Static ){ |
| 361 | c = 't'; |
| 362 | assert( (f & (MEM_Dyn|MEM_Ephem))==0 ); |
| 363 | }else if( f & MEM_Ephem ){ |
| 364 | c = 'e'; |
| 365 | assert( (f & (MEM_Static|MEM_Dyn))==0 ); |
| 366 | }else{ |
| 367 | c = 's'; |
| 368 | } |
| 369 | |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 370 | sqlite3_snprintf(100, zCsr, "%c", c); |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 371 | zCsr += sqlite3Strlen30(zCsr); |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 372 | sqlite3_snprintf(100, zCsr, "%d[", pMem->n); |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 373 | zCsr += sqlite3Strlen30(zCsr); |
danielk1977 | ca6b291 | 2004-05-21 10:49:47 +0000 | [diff] [blame] | 374 | for(i=0; i<16 && i<pMem->n; i++){ |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 375 | sqlite3_snprintf(100, zCsr, "%02X", ((int)pMem->z[i] & 0xFF)); |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 376 | zCsr += sqlite3Strlen30(zCsr); |
danielk1977 | ca6b291 | 2004-05-21 10:49:47 +0000 | [diff] [blame] | 377 | } |
| 378 | for(i=0; i<16 && i<pMem->n; i++){ |
| 379 | char z = pMem->z[i]; |
| 380 | if( z<32 || z>126 ) *zCsr++ = '.'; |
| 381 | else *zCsr++ = z; |
| 382 | } |
| 383 | |
drh | e718efe | 2007-05-10 21:14:03 +0000 | [diff] [blame] | 384 | sqlite3_snprintf(100, zCsr, "]%s", encnames[pMem->enc]); |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 385 | zCsr += sqlite3Strlen30(zCsr); |
drh | fdf972a | 2007-05-02 13:30:27 +0000 | [diff] [blame] | 386 | if( f & MEM_Zero ){ |
drh | 8df3284 | 2008-12-09 02:51:23 +0000 | [diff] [blame] | 387 | sqlite3_snprintf(100, zCsr,"+%dz",pMem->u.nZero); |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 388 | zCsr += sqlite3Strlen30(zCsr); |
drh | fdf972a | 2007-05-02 13:30:27 +0000 | [diff] [blame] | 389 | } |
danielk1977 | b1bc953 | 2004-05-22 03:05:33 +0000 | [diff] [blame] | 390 | *zCsr = '\0'; |
| 391 | }else if( f & MEM_Str ){ |
| 392 | int j, k; |
| 393 | zBuf[0] = ' '; |
| 394 | if( f & MEM_Dyn ){ |
| 395 | zBuf[1] = 'z'; |
| 396 | assert( (f & (MEM_Static|MEM_Ephem))==0 ); |
| 397 | }else if( f & MEM_Static ){ |
| 398 | zBuf[1] = 't'; |
| 399 | assert( (f & (MEM_Dyn|MEM_Ephem))==0 ); |
| 400 | }else if( f & MEM_Ephem ){ |
| 401 | zBuf[1] = 'e'; |
| 402 | assert( (f & (MEM_Static|MEM_Dyn))==0 ); |
| 403 | }else{ |
| 404 | zBuf[1] = 's'; |
| 405 | } |
| 406 | k = 2; |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 407 | sqlite3_snprintf(100, &zBuf[k], "%d", pMem->n); |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 408 | k += sqlite3Strlen30(&zBuf[k]); |
danielk1977 | b1bc953 | 2004-05-22 03:05:33 +0000 | [diff] [blame] | 409 | zBuf[k++] = '['; |
| 410 | for(j=0; j<15 && j<pMem->n; j++){ |
| 411 | u8 c = pMem->z[j]; |
danielk1977 | b1bc953 | 2004-05-22 03:05:33 +0000 | [diff] [blame] | 412 | if( c>=0x20 && c<0x7f ){ |
| 413 | zBuf[k++] = c; |
| 414 | }else{ |
| 415 | zBuf[k++] = '.'; |
| 416 | } |
| 417 | } |
| 418 | zBuf[k++] = ']'; |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 419 | sqlite3_snprintf(100,&zBuf[k], encnames[pMem->enc]); |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 420 | k += sqlite3Strlen30(&zBuf[k]); |
danielk1977 | b1bc953 | 2004-05-22 03:05:33 +0000 | [diff] [blame] | 421 | zBuf[k++] = 0; |
danielk1977 | ca6b291 | 2004-05-21 10:49:47 +0000 | [diff] [blame] | 422 | } |
danielk1977 | ca6b291 | 2004-05-21 10:49:47 +0000 | [diff] [blame] | 423 | } |
| 424 | #endif |
| 425 | |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 426 | #ifdef SQLITE_DEBUG |
| 427 | /* |
| 428 | ** Print the value of a register for tracing purposes: |
| 429 | */ |
| 430 | static void memTracePrint(FILE *out, Mem *p){ |
| 431 | if( p->flags & MEM_Null ){ |
| 432 | fprintf(out, " NULL"); |
| 433 | }else if( (p->flags & (MEM_Int|MEM_Str))==(MEM_Int|MEM_Str) ){ |
| 434 | fprintf(out, " si:%lld", p->u.i); |
| 435 | }else if( p->flags & MEM_Int ){ |
| 436 | fprintf(out, " i:%lld", p->u.i); |
drh | 0b3bf92 | 2009-06-15 20:45:34 +0000 | [diff] [blame] | 437 | #ifndef SQLITE_OMIT_FLOATING_POINT |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 438 | }else if( p->flags & MEM_Real ){ |
| 439 | fprintf(out, " r:%g", p->r); |
drh | 0b3bf92 | 2009-06-15 20:45:34 +0000 | [diff] [blame] | 440 | #endif |
drh | 733bf1b | 2009-04-22 00:47:00 +0000 | [diff] [blame] | 441 | }else if( p->flags & MEM_RowSet ){ |
| 442 | fprintf(out, " (rowset)"); |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 443 | }else{ |
| 444 | char zBuf[200]; |
| 445 | sqlite3VdbeMemPrettyPrint(p, zBuf); |
| 446 | fprintf(out, " "); |
| 447 | fprintf(out, "%s", zBuf); |
| 448 | } |
| 449 | } |
| 450 | static void registerTrace(FILE *out, int iReg, Mem *p){ |
| 451 | fprintf(out, "REG[%d] = ", iReg); |
| 452 | memTracePrint(out, p); |
| 453 | fprintf(out, "\n"); |
| 454 | } |
| 455 | #endif |
| 456 | |
| 457 | #ifdef SQLITE_DEBUG |
drh | b21e7c7 | 2008-06-22 12:37:57 +0000 | [diff] [blame] | 458 | # define REGISTER_TRACE(R,M) if(p->trace)registerTrace(p->trace,R,M) |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 459 | #else |
| 460 | # define REGISTER_TRACE(R,M) |
| 461 | #endif |
| 462 | |
danielk1977 | 84ac9d0 | 2004-05-18 09:58:06 +0000 | [diff] [blame] | 463 | |
drh | 7b39686 | 2003-01-01 23:06:20 +0000 | [diff] [blame] | 464 | #ifdef VDBE_PROFILE |
shane | 9bcbdad | 2008-05-29 20:22:37 +0000 | [diff] [blame] | 465 | |
| 466 | /* |
| 467 | ** hwtime.h contains inline assembler code for implementing |
| 468 | ** high-performance timing routines. |
drh | 7b39686 | 2003-01-01 23:06:20 +0000 | [diff] [blame] | 469 | */ |
shane | 9bcbdad | 2008-05-29 20:22:37 +0000 | [diff] [blame] | 470 | #include "hwtime.h" |
| 471 | |
drh | 7b39686 | 2003-01-01 23:06:20 +0000 | [diff] [blame] | 472 | #endif |
| 473 | |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 474 | /* |
drh | caec2f1 | 2003-01-07 02:47:47 +0000 | [diff] [blame] | 475 | ** The CHECK_FOR_INTERRUPT macro defined here looks to see if the |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 476 | ** sqlite3_interrupt() routine has been called. If it has been, then |
drh | caec2f1 | 2003-01-07 02:47:47 +0000 | [diff] [blame] | 477 | ** processing of the VDBE program is interrupted. |
| 478 | ** |
| 479 | ** This macro added to every instruction that does a jump in order to |
| 480 | ** implement a loop. This test used to be on every single instruction, |
| 481 | ** but that meant we more testing that we needed. By only testing the |
| 482 | ** flag on jump instructions, we get a (small) speed improvement. |
| 483 | */ |
| 484 | #define CHECK_FOR_INTERRUPT \ |
drh | 881feaa | 2006-07-26 01:39:30 +0000 | [diff] [blame] | 485 | if( db->u1.isInterrupted ) goto abort_due_to_interrupt; |
drh | caec2f1 | 2003-01-07 02:47:47 +0000 | [diff] [blame] | 486 | |
| 487 | |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 488 | #ifndef NDEBUG |
| 489 | /* |
| 490 | ** This function is only called from within an assert() expression. It |
| 491 | ** checks that the sqlite3.nTransaction variable is correctly set to |
| 492 | ** the number of non-transaction savepoints currently in the |
| 493 | ** linked list starting at sqlite3.pSavepoint. |
| 494 | ** |
| 495 | ** Usage: |
| 496 | ** |
| 497 | ** assert( checkSavepointCount(db) ); |
| 498 | */ |
| 499 | static int checkSavepointCount(sqlite3 *db){ |
| 500 | int n = 0; |
| 501 | Savepoint *p; |
| 502 | for(p=db->pSavepoint; p; p=p->pNext) n++; |
| 503 | assert( n==(db->nSavepoint + db->isTransactionSavepoint) ); |
| 504 | return 1; |
| 505 | } |
| 506 | #endif |
| 507 | |
drh | caec2f1 | 2003-01-07 02:47:47 +0000 | [diff] [blame] | 508 | /* |
drh | b975598 | 2010-07-24 16:34:37 +0000 | [diff] [blame] | 509 | ** Transfer error message text from an sqlite3_vtab.zErrMsg (text stored |
| 510 | ** in memory obtained from sqlite3_malloc) into a Vdbe.zErrMsg (text stored |
| 511 | ** in memory obtained from sqlite3DbMalloc). |
| 512 | */ |
| 513 | static void importVtabErrMsg(Vdbe *p, sqlite3_vtab *pVtab){ |
| 514 | sqlite3 *db = p->db; |
| 515 | sqlite3DbFree(db, p->zErrMsg); |
| 516 | p->zErrMsg = sqlite3DbStrDup(db, pVtab->zErrMsg); |
| 517 | sqlite3_free(pVtab->zErrMsg); |
| 518 | pVtab->zErrMsg = 0; |
| 519 | } |
| 520 | |
dan | 5538807 | 2011-09-20 15:53:02 +0000 | [diff] [blame] | 521 | /* |
| 522 | ** Call sqlite3PagerCacheStats() on all database pagers used by the VM |
| 523 | ** passed as the first argument, incrementing *pnHit and *pnMiss for |
| 524 | ** with each call. |
| 525 | */ |
| 526 | static void vdbeCacheStats(Vdbe *p, int *pnHit, int *pnMiss){ |
| 527 | int i; |
| 528 | yDbMask mask; |
| 529 | sqlite3 *db; |
| 530 | Db *aDb; |
| 531 | int nDb; |
| 532 | if( p->lockMask==0 ) return; /* The common case */ |
| 533 | db = p->db; |
| 534 | aDb = db->aDb; |
| 535 | nDb = db->nDb; |
| 536 | for(i=0, mask=1; i<nDb; i++, mask += mask){ |
| 537 | if( i!=1 && (mask & p->lockMask)!=0 && ALWAYS(aDb[i].pBt!=0) ){ |
| 538 | sqlite3PagerCacheStats(sqlite3BtreePager(aDb[i].pBt), pnHit, pnMiss); |
| 539 | } |
| 540 | } |
| 541 | } |
drh | b975598 | 2010-07-24 16:34:37 +0000 | [diff] [blame] | 542 | |
| 543 | /* |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 544 | ** Execute as much of a VDBE program as we can then return. |
| 545 | ** |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 546 | ** sqlite3VdbeMakeReady() must be called before this routine in order to |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 547 | ** close the program with a final OP_Halt and to set up the callbacks |
| 548 | ** and the error message pointer. |
| 549 | ** |
| 550 | ** Whenever a row or result data is available, this routine will either |
| 551 | ** invoke the result callback (if there is one) or return with |
drh | 326dce7 | 2003-01-29 14:06:07 +0000 | [diff] [blame] | 552 | ** SQLITE_ROW. |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 553 | ** |
| 554 | ** If an attempt is made to open a locked database, then this routine |
| 555 | ** will either invoke the busy callback (if there is one) or it will |
| 556 | ** return SQLITE_BUSY. |
| 557 | ** |
| 558 | ** If an error occurs, an error message is written to memory obtained |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 559 | ** from sqlite3_malloc() and p->zErrMsg is made to point to that memory. |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 560 | ** The error code is stored in p->rc and this routine returns SQLITE_ERROR. |
| 561 | ** |
| 562 | ** If the callback ever returns non-zero, then the program exits |
| 563 | ** immediately. There will be no error message but the p->rc field is |
| 564 | ** set to SQLITE_ABORT and this routine will return SQLITE_ERROR. |
| 565 | ** |
drh | 9468c7f | 2003-03-07 19:50:07 +0000 | [diff] [blame] | 566 | ** A memory allocation error causes p->rc to be set to SQLITE_NOMEM and this |
| 567 | ** routine to return SQLITE_ERROR. |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 568 | ** |
| 569 | ** Other fatal errors return SQLITE_ERROR. |
| 570 | ** |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 571 | ** After this routine has finished, sqlite3VdbeFinalize() should be |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 572 | ** used to clean up the mess that was left behind. |
| 573 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 574 | int sqlite3VdbeExec( |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 575 | Vdbe *p /* The VDBE */ |
| 576 | ){ |
shaneh | 84f4b2f | 2010-02-26 01:46:54 +0000 | [diff] [blame] | 577 | int pc=0; /* The program counter */ |
drh | bbe879d | 2009-11-14 18:04:35 +0000 | [diff] [blame] | 578 | Op *aOp = p->aOp; /* Copy of p->aOp */ |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 579 | Op *pOp; /* Current operation */ |
| 580 | int rc = SQLITE_OK; /* Value to return */ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 581 | sqlite3 *db = p->db; /* The database */ |
drh | cdf011d | 2011-04-04 21:25:28 +0000 | [diff] [blame] | 582 | u8 resetSchemaOnFault = 0; /* Reset schema after an error if positive */ |
drh | 8079a0d | 2006-01-12 17:20:50 +0000 | [diff] [blame] | 583 | u8 encoding = ENC(db); /* The database encoding */ |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 584 | #ifndef SQLITE_OMIT_PROGRESS_CALLBACK |
shaneh | 5e17e8b | 2009-12-03 04:40:47 +0000 | [diff] [blame] | 585 | int checkProgress; /* True if progress callbacks are enabled */ |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 586 | int nProgressOps = 0; /* Opcodes executed since progress callback. */ |
| 587 | #endif |
| 588 | Mem *aMem = p->aMem; /* Copy of p->aMem */ |
drh | b27b7f5 | 2008-12-10 18:03:45 +0000 | [diff] [blame] | 589 | Mem *pIn1 = 0; /* 1st input operand */ |
| 590 | Mem *pIn2 = 0; /* 2nd input operand */ |
| 591 | Mem *pIn3 = 0; /* 3rd input operand */ |
| 592 | Mem *pOut = 0; /* Output operand */ |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 593 | int iCompare = 0; /* Result of last OP_Compare operation */ |
shane | be21779 | 2009-03-05 04:20:31 +0000 | [diff] [blame] | 594 | int *aPermute = 0; /* Permutation of columns for OP_Compare */ |
drh | 99a6692 | 2011-05-13 18:51:42 +0000 | [diff] [blame] | 595 | i64 lastRowid = db->lastRowid; /* Saved value of the last insert ROWID */ |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 596 | #ifdef VDBE_PROFILE |
shane | 9bcbdad | 2008-05-29 20:22:37 +0000 | [diff] [blame] | 597 | u64 start; /* CPU clock count at start of opcode */ |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 598 | int origPc; /* Program counter at start of opcode */ |
| 599 | #endif |
dan | 5538807 | 2011-09-20 15:53:02 +0000 | [diff] [blame] | 600 | int nHit = 0; /* Cache hits for this call */ |
| 601 | int nMiss = 0; /* Cache misses for this call */ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 602 | /*** INSERT STACK UNION HERE ***/ |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 603 | |
drh | ca48c90 | 2008-01-18 14:08:24 +0000 | [diff] [blame] | 604 | assert( p->magic==VDBE_MAGIC_RUN ); /* sqlite3_step() verifies this */ |
drh | bdaec52 | 2011-04-04 00:14:43 +0000 | [diff] [blame] | 605 | sqlite3VdbeEnter(p); |
dan | 5538807 | 2011-09-20 15:53:02 +0000 | [diff] [blame] | 606 | vdbeCacheStats(p, &nHit, &nMiss); |
danielk1977 | 2e588c7 | 2005-12-09 14:25:08 +0000 | [diff] [blame] | 607 | if( p->rc==SQLITE_NOMEM ){ |
| 608 | /* This happens if a malloc() inside a call to sqlite3_column_text() or |
| 609 | ** sqlite3_column_text16() failed. */ |
| 610 | goto no_mem; |
| 611 | } |
drh | 3a84069 | 2003-01-29 22:58:26 +0000 | [diff] [blame] | 612 | assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY ); |
| 613 | p->rc = SQLITE_OK; |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 614 | assert( p->explain==0 ); |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 615 | p->pResultSet = 0; |
drh | a4afb65 | 2005-07-09 02:16:02 +0000 | [diff] [blame] | 616 | db->busyHandler.nBusy = 0; |
drh | 9358164 | 2004-02-12 13:02:55 +0000 | [diff] [blame] | 617 | CHECK_FOR_INTERRUPT; |
drh | 602c237 | 2007-03-01 00:29:13 +0000 | [diff] [blame] | 618 | sqlite3VdbeIOTraceSql(p); |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 619 | #ifndef SQLITE_OMIT_PROGRESS_CALLBACK |
| 620 | checkProgress = db->xProgress!=0; |
| 621 | #endif |
drh | 3c23a88 | 2007-01-09 14:01:13 +0000 | [diff] [blame] | 622 | #ifdef SQLITE_DEBUG |
danielk1977 | 2d1d86f | 2008-06-20 14:59:51 +0000 | [diff] [blame] | 623 | sqlite3BeginBenignMalloc(); |
drh | 4222441 | 2010-05-31 14:28:25 +0000 | [diff] [blame] | 624 | if( p->pc==0 && (p->db->flags & SQLITE_VdbeListing)!=0 ){ |
drh | 3c23a88 | 2007-01-09 14:01:13 +0000 | [diff] [blame] | 625 | int i; |
| 626 | printf("VDBE Program Listing:\n"); |
| 627 | sqlite3VdbePrintSql(p); |
| 628 | for(i=0; i<p->nOp; i++){ |
drh | bbe879d | 2009-11-14 18:04:35 +0000 | [diff] [blame] | 629 | sqlite3VdbePrintOp(stdout, i, &aOp[i]); |
drh | 3c23a88 | 2007-01-09 14:01:13 +0000 | [diff] [blame] | 630 | } |
| 631 | } |
danielk1977 | 2d1d86f | 2008-06-20 14:59:51 +0000 | [diff] [blame] | 632 | sqlite3EndBenignMalloc(); |
drh | 3c23a88 | 2007-01-09 14:01:13 +0000 | [diff] [blame] | 633 | #endif |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 634 | for(pc=p->pc; rc==SQLITE_OK; pc++){ |
drh | caec2f1 | 2003-01-07 02:47:47 +0000 | [diff] [blame] | 635 | assert( pc>=0 && pc<p->nOp ); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 636 | if( db->mallocFailed ) goto no_mem; |
drh | 7b39686 | 2003-01-01 23:06:20 +0000 | [diff] [blame] | 637 | #ifdef VDBE_PROFILE |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 638 | origPc = pc; |
shane | 9bcbdad | 2008-05-29 20:22:37 +0000 | [diff] [blame] | 639 | start = sqlite3Hwtime(); |
drh | 7b39686 | 2003-01-01 23:06:20 +0000 | [diff] [blame] | 640 | #endif |
drh | bbe879d | 2009-11-14 18:04:35 +0000 | [diff] [blame] | 641 | pOp = &aOp[pc]; |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 642 | |
danielk1977 | 8b60e0f | 2005-01-12 09:10:39 +0000 | [diff] [blame] | 643 | /* Only allow tracing if SQLITE_DEBUG is defined. |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 644 | */ |
danielk1977 | 8b60e0f | 2005-01-12 09:10:39 +0000 | [diff] [blame] | 645 | #ifdef SQLITE_DEBUG |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 646 | if( p->trace ){ |
drh | 3f7d4e4 | 2004-07-24 14:35:58 +0000 | [diff] [blame] | 647 | if( pc==0 ){ |
| 648 | printf("VDBE Execution Trace:\n"); |
| 649 | sqlite3VdbePrintSql(p); |
| 650 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 651 | sqlite3VdbePrintOp(p->trace, pc, pOp); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 652 | } |
drh | 3f7d4e4 | 2004-07-24 14:35:58 +0000 | [diff] [blame] | 653 | #endif |
| 654 | |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 655 | |
drh | f603871 | 2004-02-08 18:07:34 +0000 | [diff] [blame] | 656 | /* Check to see if we need to simulate an interrupt. This only happens |
| 657 | ** if we have a special test build. |
| 658 | */ |
| 659 | #ifdef SQLITE_TEST |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 660 | if( sqlite3_interrupt_count>0 ){ |
| 661 | sqlite3_interrupt_count--; |
| 662 | if( sqlite3_interrupt_count==0 ){ |
| 663 | sqlite3_interrupt(db); |
drh | f603871 | 2004-02-08 18:07:34 +0000 | [diff] [blame] | 664 | } |
| 665 | } |
| 666 | #endif |
| 667 | |
danielk1977 | 348bb5d | 2003-10-18 09:37:26 +0000 | [diff] [blame] | 668 | #ifndef SQLITE_OMIT_PROGRESS_CALLBACK |
| 669 | /* Call the progress callback if it is configured and the required number |
| 670 | ** of VDBE ops have been executed (either since this invocation of |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 671 | ** sqlite3VdbeExec() or since last time the progress callback was called). |
danielk1977 | 348bb5d | 2003-10-18 09:37:26 +0000 | [diff] [blame] | 672 | ** If the progress callback returns non-zero, exit the virtual machine with |
| 673 | ** a return code SQLITE_ABORT. |
| 674 | */ |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 675 | if( checkProgress ){ |
drh | 3914aed | 2004-01-31 20:40:42 +0000 | [diff] [blame] | 676 | if( db->nProgressOps==nProgressOps ){ |
danielk1977 | de523ac | 2007-06-15 14:53:53 +0000 | [diff] [blame] | 677 | int prc; |
drh | 9978c97 | 2010-02-23 17:36:32 +0000 | [diff] [blame] | 678 | prc = db->xProgress(db->pProgressArg); |
danielk1977 | de523ac | 2007-06-15 14:53:53 +0000 | [diff] [blame] | 679 | if( prc!=0 ){ |
| 680 | rc = SQLITE_INTERRUPT; |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 681 | goto vdbe_error_halt; |
danielk1977 | de523ac | 2007-06-15 14:53:53 +0000 | [diff] [blame] | 682 | } |
danielk1977 | 3fe11f3 | 2007-06-13 16:49:48 +0000 | [diff] [blame] | 683 | nProgressOps = 0; |
danielk1977 | 348bb5d | 2003-10-18 09:37:26 +0000 | [diff] [blame] | 684 | } |
drh | 3914aed | 2004-01-31 20:40:42 +0000 | [diff] [blame] | 685 | nProgressOps++; |
danielk1977 | 348bb5d | 2003-10-18 09:37:26 +0000 | [diff] [blame] | 686 | } |
danielk1977 | 348bb5d | 2003-10-18 09:37:26 +0000 | [diff] [blame] | 687 | #endif |
| 688 | |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 689 | /* On any opcode with the "out2-prerelase" tag, free any |
| 690 | ** external allocations out of mem[p2] and set mem[p2] to be |
| 691 | ** an undefined integer. Opcodes will either fill in the integer |
| 692 | ** value or convert mem[p2] to a different type. |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 693 | */ |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 694 | assert( pOp->opflags==sqlite3OpcodeProperty[pOp->opcode] ); |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 695 | if( pOp->opflags & OPFLG_OUT2_PRERELEASE ){ |
| 696 | assert( pOp->p2>0 ); |
| 697 | assert( pOp->p2<=p->nMem ); |
| 698 | pOut = &aMem[pOp->p2]; |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 699 | memAboutToChange(p, pOut); |
drh | 2d36eb4 | 2011-08-29 02:49:41 +0000 | [diff] [blame] | 700 | MemReleaseExt(pOut); |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 701 | pOut->flags = MEM_Int; |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 702 | } |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 703 | |
| 704 | /* Sanity checking on other operands */ |
| 705 | #ifdef SQLITE_DEBUG |
| 706 | if( (pOp->opflags & OPFLG_IN1)!=0 ){ |
| 707 | assert( pOp->p1>0 ); |
| 708 | assert( pOp->p1<=p->nMem ); |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 709 | assert( memIsValid(&aMem[pOp->p1]) ); |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 710 | REGISTER_TRACE(pOp->p1, &aMem[pOp->p1]); |
| 711 | } |
| 712 | if( (pOp->opflags & OPFLG_IN2)!=0 ){ |
| 713 | assert( pOp->p2>0 ); |
| 714 | assert( pOp->p2<=p->nMem ); |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 715 | assert( memIsValid(&aMem[pOp->p2]) ); |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 716 | REGISTER_TRACE(pOp->p2, &aMem[pOp->p2]); |
| 717 | } |
| 718 | if( (pOp->opflags & OPFLG_IN3)!=0 ){ |
| 719 | assert( pOp->p3>0 ); |
| 720 | assert( pOp->p3<=p->nMem ); |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 721 | assert( memIsValid(&aMem[pOp->p3]) ); |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 722 | REGISTER_TRACE(pOp->p3, &aMem[pOp->p3]); |
| 723 | } |
| 724 | if( (pOp->opflags & OPFLG_OUT2)!=0 ){ |
| 725 | assert( pOp->p2>0 ); |
| 726 | assert( pOp->p2<=p->nMem ); |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 727 | memAboutToChange(p, &aMem[pOp->p2]); |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 728 | } |
| 729 | if( (pOp->opflags & OPFLG_OUT3)!=0 ){ |
| 730 | assert( pOp->p3>0 ); |
| 731 | assert( pOp->p3<=p->nMem ); |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 732 | memAboutToChange(p, &aMem[pOp->p3]); |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 733 | } |
| 734 | #endif |
drh | 93952eb | 2009-11-13 19:43:43 +0000 | [diff] [blame] | 735 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 736 | switch( pOp->opcode ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 737 | |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 738 | /***************************************************************************** |
| 739 | ** What follows is a massive switch statement where each case implements a |
| 740 | ** separate instruction in the virtual machine. If we follow the usual |
| 741 | ** indentation conventions, each case should be indented by 6 spaces. But |
| 742 | ** that is a lot of wasted space on the left margin. So the code within |
| 743 | ** the switch statement will break with convention and be flush-left. Another |
| 744 | ** big comment (similar to this one) will mark the point in the code where |
| 745 | ** we transition back to normal indentation. |
drh | ac82fcf | 2002-09-08 17:23:41 +0000 | [diff] [blame] | 746 | ** |
| 747 | ** The formatting of each case is important. The makefile for SQLite |
| 748 | ** generates two C files "opcodes.h" and "opcodes.c" by scanning this |
| 749 | ** file looking for lines that begin with "case OP_". The opcodes.h files |
| 750 | ** will be filled with #defines that give unique integer values to each |
| 751 | ** 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] | 752 | ** each string is the symbolic name for the corresponding opcode. If the |
| 753 | ** case statement is followed by a comment of the form "/# same as ... #/" |
| 754 | ** that comment is used to determine the particular value of the opcode. |
drh | ac82fcf | 2002-09-08 17:23:41 +0000 | [diff] [blame] | 755 | ** |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 756 | ** Other keywords in the comment that follows each case are used to |
| 757 | ** construct the OPFLG_INITIALIZER value that initializes opcodeProperty[]. |
| 758 | ** Keywords include: in1, in2, in3, out2_prerelease, out2, out3. See |
| 759 | ** the mkopcodeh.awk script for additional information. |
danielk1977 | bc04f85 | 2005-03-29 08:26:13 +0000 | [diff] [blame] | 760 | ** |
drh | ac82fcf | 2002-09-08 17:23:41 +0000 | [diff] [blame] | 761 | ** Documentation about VDBE opcodes is generated by scanning this file |
| 762 | ** for lines of that contain "Opcode:". That line and all subsequent |
| 763 | ** comment lines are used in the generation of the opcode.html documentation |
| 764 | ** file. |
| 765 | ** |
| 766 | ** SUMMARY: |
| 767 | ** |
| 768 | ** Formatting is important to scripts that scan this file. |
| 769 | ** Do not deviate from the formatting style currently in use. |
| 770 | ** |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 771 | *****************************************************************************/ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 772 | |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 773 | /* Opcode: Goto * P2 * * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 774 | ** |
| 775 | ** An unconditional jump to address P2. |
| 776 | ** The next instruction executed will be |
| 777 | ** the one at index P2 from the beginning of |
| 778 | ** the program. |
| 779 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 780 | case OP_Goto: { /* jump */ |
drh | caec2f1 | 2003-01-07 02:47:47 +0000 | [diff] [blame] | 781 | CHECK_FOR_INTERRUPT; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 782 | pc = pOp->p2 - 1; |
| 783 | break; |
| 784 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 785 | |
drh | 2eb9537 | 2008-06-06 15:04:36 +0000 | [diff] [blame] | 786 | /* Opcode: Gosub P1 P2 * * * |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 787 | ** |
drh | 2eb9537 | 2008-06-06 15:04:36 +0000 | [diff] [blame] | 788 | ** Write the current address onto register P1 |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 789 | ** and then jump to address P2. |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 790 | */ |
drh | 93952eb | 2009-11-13 19:43:43 +0000 | [diff] [blame] | 791 | case OP_Gosub: { /* jump, in1 */ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 792 | pIn1 = &aMem[pOp->p1]; |
drh | 2eb9537 | 2008-06-06 15:04:36 +0000 | [diff] [blame] | 793 | assert( (pIn1->flags & MEM_Dyn)==0 ); |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 794 | memAboutToChange(p, pIn1); |
drh | 2eb9537 | 2008-06-06 15:04:36 +0000 | [diff] [blame] | 795 | pIn1->flags = MEM_Int; |
| 796 | pIn1->u.i = pc; |
| 797 | REGISTER_TRACE(pOp->p1, pIn1); |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 798 | pc = pOp->p2 - 1; |
| 799 | break; |
| 800 | } |
| 801 | |
drh | 2eb9537 | 2008-06-06 15:04:36 +0000 | [diff] [blame] | 802 | /* Opcode: Return P1 * * * * |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 803 | ** |
drh | 2eb9537 | 2008-06-06 15:04:36 +0000 | [diff] [blame] | 804 | ** Jump to the next instruction after the address in register P1. |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 805 | */ |
drh | 2eb9537 | 2008-06-06 15:04:36 +0000 | [diff] [blame] | 806 | case OP_Return: { /* in1 */ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 807 | pIn1 = &aMem[pOp->p1]; |
drh | 2eb9537 | 2008-06-06 15:04:36 +0000 | [diff] [blame] | 808 | assert( pIn1->flags & MEM_Int ); |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 809 | pc = (int)pIn1->u.i; |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 810 | break; |
| 811 | } |
| 812 | |
drh | e00ee6e | 2008-06-20 15:24:01 +0000 | [diff] [blame] | 813 | /* Opcode: Yield P1 * * * * |
| 814 | ** |
| 815 | ** Swap the program counter with the value in register P1. |
| 816 | */ |
danielk1977 | f73ab8b | 2008-12-29 10:39:53 +0000 | [diff] [blame] | 817 | case OP_Yield: { /* in1 */ |
drh | e00ee6e | 2008-06-20 15:24:01 +0000 | [diff] [blame] | 818 | int pcDest; |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 819 | pIn1 = &aMem[pOp->p1]; |
drh | e00ee6e | 2008-06-20 15:24:01 +0000 | [diff] [blame] | 820 | assert( (pIn1->flags & MEM_Dyn)==0 ); |
| 821 | pIn1->flags = MEM_Int; |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 822 | pcDest = (int)pIn1->u.i; |
drh | e00ee6e | 2008-06-20 15:24:01 +0000 | [diff] [blame] | 823 | pIn1->u.i = pc; |
| 824 | REGISTER_TRACE(pOp->p1, pIn1); |
| 825 | pc = pcDest; |
| 826 | break; |
| 827 | } |
| 828 | |
drh | 5053a79 | 2009-02-20 03:02:23 +0000 | [diff] [blame] | 829 | /* Opcode: HaltIfNull P1 P2 P3 P4 * |
| 830 | ** |
drh | ef8662b | 2011-06-20 21:47:58 +0000 | [diff] [blame] | 831 | ** Check the value in register P3. If it is NULL then Halt using |
drh | 5053a79 | 2009-02-20 03:02:23 +0000 | [diff] [blame] | 832 | ** parameter P1, P2, and P4 as if this were a Halt instruction. If the |
| 833 | ** value in register P3 is not NULL, then this routine is a no-op. |
| 834 | */ |
| 835 | case OP_HaltIfNull: { /* in3 */ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 836 | pIn3 = &aMem[pOp->p3]; |
drh | 5053a79 | 2009-02-20 03:02:23 +0000 | [diff] [blame] | 837 | if( (pIn3->flags & MEM_Null)==0 ) break; |
| 838 | /* Fall through into OP_Halt */ |
| 839 | } |
drh | e00ee6e | 2008-06-20 15:24:01 +0000 | [diff] [blame] | 840 | |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 841 | /* Opcode: Halt P1 P2 * P4 * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 842 | ** |
drh | 3d4501e | 2008-12-04 20:40:10 +0000 | [diff] [blame] | 843 | ** Exit immediately. All open cursors, etc are closed |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 844 | ** automatically. |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 845 | ** |
drh | 92f02c3 | 2004-09-02 14:57:08 +0000 | [diff] [blame] | 846 | ** P1 is the result code returned by sqlite3_exec(), sqlite3_reset(), |
| 847 | ** or sqlite3_finalize(). For a normal halt, this should be SQLITE_OK (0). |
| 848 | ** For errors, it can be some other value. If P1!=0 then P2 will determine |
| 849 | ** whether or not to rollback the current transaction. Do not rollback |
| 850 | ** if P2==OE_Fail. Do the rollback if P2==OE_Rollback. If P2==OE_Abort, |
| 851 | ** then back out all changes that have occurred during this execution of the |
drh | b798fa6 | 2002-09-03 19:43:23 +0000 | [diff] [blame] | 852 | ** VDBE, but do not rollback the transaction. |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 853 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 854 | ** If P4 is not null then it is an error message string. |
drh | 7f057c9 | 2005-06-24 03:53:06 +0000 | [diff] [blame] | 855 | ** |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 856 | ** 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] | 857 | ** every program. So a jump past the last instruction of the program |
| 858 | ** is the same as executing Halt. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 859 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 860 | case OP_Halt: { |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 861 | if( pOp->p1==SQLITE_OK && p->pFrame ){ |
dan | 2832ad4 | 2009-08-31 15:27:27 +0000 | [diff] [blame] | 862 | /* Halt the sub-program. Return control to the parent frame. */ |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 863 | VdbeFrame *pFrame = p->pFrame; |
| 864 | p->pFrame = pFrame->pParent; |
| 865 | p->nFrame--; |
dan | 2832ad4 | 2009-08-31 15:27:27 +0000 | [diff] [blame] | 866 | sqlite3VdbeSetChanges(db, p->nChange); |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 867 | pc = sqlite3VdbeFrameRestore(pFrame); |
drh | 99a6692 | 2011-05-13 18:51:42 +0000 | [diff] [blame] | 868 | lastRowid = db->lastRowid; |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 869 | if( pOp->p2==OE_Ignore ){ |
dan | 2832ad4 | 2009-08-31 15:27:27 +0000 | [diff] [blame] | 870 | /* Instruction pc is the OP_Program that invoked the sub-program |
| 871 | ** currently being halted. If the p2 instruction of this OP_Halt |
| 872 | ** instruction is set to OE_Ignore, then the sub-program is throwing |
| 873 | ** an IGNORE exception. In this case jump to the address specified |
| 874 | ** as the p2 of the calling OP_Program. */ |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 875 | pc = p->aOp[pc].p2-1; |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 876 | } |
drh | bbe879d | 2009-11-14 18:04:35 +0000 | [diff] [blame] | 877 | aOp = p->aOp; |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 878 | aMem = p->aMem; |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 879 | break; |
| 880 | } |
dan | 2832ad4 | 2009-08-31 15:27:27 +0000 | [diff] [blame] | 881 | |
drh | 92f02c3 | 2004-09-02 14:57:08 +0000 | [diff] [blame] | 882 | p->rc = pOp->p1; |
shane | 36840fd | 2009-06-26 16:32:13 +0000 | [diff] [blame] | 883 | p->errorAction = (u8)pOp->p2; |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 884 | p->pc = pc; |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 885 | if( pOp->p4.z ){ |
drh | 413c3d3 | 2010-02-23 20:11:56 +0000 | [diff] [blame] | 886 | assert( p->rc!=SQLITE_OK ); |
drh | f089aa4 | 2008-07-08 19:34:06 +0000 | [diff] [blame] | 887 | sqlite3SetString(&p->zErrMsg, db, "%s", pOp->p4.z); |
drh | af46dc1 | 2010-02-24 21:44:07 +0000 | [diff] [blame] | 888 | testcase( sqlite3GlobalConfig.xLog!=0 ); |
drh | 413c3d3 | 2010-02-23 20:11:56 +0000 | [diff] [blame] | 889 | sqlite3_log(pOp->p1, "abort at %d in [%s]: %s", pc, p->zSql, pOp->p4.z); |
drh | cda455b | 2010-02-24 19:23:56 +0000 | [diff] [blame] | 890 | }else if( p->rc ){ |
drh | af46dc1 | 2010-02-24 21:44:07 +0000 | [diff] [blame] | 891 | testcase( sqlite3GlobalConfig.xLog!=0 ); |
drh | cda455b | 2010-02-24 19:23:56 +0000 | [diff] [blame] | 892 | sqlite3_log(pOp->p1, "constraint failed at %d in [%s]", pc, p->zSql); |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 893 | } |
drh | 92f02c3 | 2004-09-02 14:57:08 +0000 | [diff] [blame] | 894 | rc = sqlite3VdbeHalt(p); |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 895 | assert( rc==SQLITE_BUSY || rc==SQLITE_OK || rc==SQLITE_ERROR ); |
drh | 92f02c3 | 2004-09-02 14:57:08 +0000 | [diff] [blame] | 896 | if( rc==SQLITE_BUSY ){ |
drh | 900b31e | 2007-08-28 02:27:51 +0000 | [diff] [blame] | 897 | p->rc = rc = SQLITE_BUSY; |
| 898 | }else{ |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 899 | assert( rc==SQLITE_OK || p->rc==SQLITE_CONSTRAINT ); |
| 900 | assert( rc==SQLITE_OK || db->nDeferredCons>0 ); |
drh | 900b31e | 2007-08-28 02:27:51 +0000 | [diff] [blame] | 901 | rc = p->rc ? SQLITE_ERROR : SQLITE_DONE; |
drh | 92f02c3 | 2004-09-02 14:57:08 +0000 | [diff] [blame] | 902 | } |
drh | 900b31e | 2007-08-28 02:27:51 +0000 | [diff] [blame] | 903 | goto vdbe_return; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 904 | } |
drh | c61053b | 2000-06-04 12:58:36 +0000 | [diff] [blame] | 905 | |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 906 | /* Opcode: Integer P1 P2 * * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 907 | ** |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 908 | ** The 32-bit integer value P1 is written into register P2. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 909 | */ |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 910 | case OP_Integer: { /* out2-prerelease */ |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 911 | pOut->u.i = pOp->p1; |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 912 | break; |
| 913 | } |
| 914 | |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 915 | /* Opcode: Int64 * P2 * P4 * |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 916 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 917 | ** P4 is a pointer to a 64-bit integer value. |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 918 | ** Write that value into register P2. |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 919 | */ |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 920 | case OP_Int64: { /* out2-prerelease */ |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 921 | assert( pOp->p4.pI64!=0 ); |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 922 | pOut->u.i = *pOp->p4.pI64; |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 923 | break; |
| 924 | } |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 925 | |
drh | 13573c7 | 2010-01-12 17:04:07 +0000 | [diff] [blame] | 926 | #ifndef SQLITE_OMIT_FLOATING_POINT |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 927 | /* Opcode: Real * P2 * P4 * |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 928 | ** |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 929 | ** P4 is a pointer to a 64-bit floating point value. |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 930 | ** Write that value into register P2. |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 931 | */ |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 932 | case OP_Real: { /* same as TK_FLOAT, out2-prerelease */ |
| 933 | pOut->flags = MEM_Real; |
drh | 2eaf93d | 2008-04-29 00:15:20 +0000 | [diff] [blame] | 934 | assert( !sqlite3IsNaN(*pOp->p4.pReal) ); |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 935 | pOut->r = *pOp->p4.pReal; |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 936 | break; |
| 937 | } |
drh | 13573c7 | 2010-01-12 17:04:07 +0000 | [diff] [blame] | 938 | #endif |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 939 | |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 940 | /* Opcode: String8 * P2 * P4 * |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 941 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 942 | ** P4 points to a nul terminated UTF-8 string. This opcode is transformed |
danielk1977 | 0f69c1e | 2004-05-29 11:24:50 +0000 | [diff] [blame] | 943 | ** into an OP_String before it is executed for the first time. |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 944 | */ |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 945 | case OP_String8: { /* same as TK_STRING, out2-prerelease */ |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 946 | assert( pOp->p4.z!=0 ); |
drh | ed2df7f | 2005-11-16 04:34:32 +0000 | [diff] [blame] | 947 | pOp->opcode = OP_String; |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 948 | pOp->p1 = sqlite3Strlen30(pOp->p4.z); |
drh | ed2df7f | 2005-11-16 04:34:32 +0000 | [diff] [blame] | 949 | |
| 950 | #ifndef SQLITE_OMIT_UTF16 |
drh | 8079a0d | 2006-01-12 17:20:50 +0000 | [diff] [blame] | 951 | if( encoding!=SQLITE_UTF8 ){ |
drh | 3a9cf17 | 2009-06-17 21:42:33 +0000 | [diff] [blame] | 952 | rc = sqlite3VdbeMemSetStr(pOut, pOp->p4.z, -1, SQLITE_UTF8, SQLITE_STATIC); |
| 953 | if( rc==SQLITE_TOOBIG ) goto too_big; |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 954 | if( SQLITE_OK!=sqlite3VdbeChangeEncoding(pOut, encoding) ) goto no_mem; |
drh | 3a9cf17 | 2009-06-17 21:42:33 +0000 | [diff] [blame] | 955 | assert( pOut->zMalloc==pOut->z ); |
| 956 | assert( pOut->flags & MEM_Dyn ); |
danielk1977 | 5f09613 | 2008-03-28 15:44:09 +0000 | [diff] [blame] | 957 | pOut->zMalloc = 0; |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 958 | pOut->flags |= MEM_Static; |
drh | 191b54c | 2008-04-15 12:14:21 +0000 | [diff] [blame] | 959 | pOut->flags &= ~MEM_Dyn; |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 960 | if( pOp->p4type==P4_DYNAMIC ){ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 961 | sqlite3DbFree(db, pOp->p4.z); |
danielk1977 | e004840 | 2004-06-15 16:51:01 +0000 | [diff] [blame] | 962 | } |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 963 | pOp->p4type = P4_DYNAMIC; |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 964 | pOp->p4.z = pOut->z; |
| 965 | pOp->p1 = pOut->n; |
danielk1977 | 0f69c1e | 2004-05-29 11:24:50 +0000 | [diff] [blame] | 966 | } |
danielk1977 | 93758c8 | 2005-01-21 08:13:14 +0000 | [diff] [blame] | 967 | #endif |
drh | bb4957f | 2008-03-20 14:03:29 +0000 | [diff] [blame] | 968 | if( pOp->p1>db->aLimit[SQLITE_LIMIT_LENGTH] ){ |
drh | cbd2da9 | 2007-12-17 16:20:06 +0000 | [diff] [blame] | 969 | goto too_big; |
| 970 | } |
| 971 | /* Fall through to the next case, OP_String */ |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 972 | } |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 973 | |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 974 | /* Opcode: String P1 P2 * P4 * |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 975 | ** |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 976 | ** The string value P4 of length P1 (bytes) is stored in register P2. |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 977 | */ |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 978 | case OP_String: { /* out2-prerelease */ |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 979 | assert( pOp->p4.z!=0 ); |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 980 | pOut->flags = MEM_Str|MEM_Static|MEM_Term; |
| 981 | pOut->z = pOp->p4.z; |
| 982 | pOut->n = pOp->p1; |
| 983 | pOut->enc = encoding; |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 984 | UPDATE_MAX_BLOBSIZE(pOut); |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 985 | break; |
| 986 | } |
| 987 | |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 988 | /* Opcode: Null * P2 * * * |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 989 | ** |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 990 | ** Write a NULL into register P2. |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 991 | */ |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 992 | case OP_Null: { /* out2-prerelease */ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 993 | pOut->flags = MEM_Null; |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 994 | break; |
| 995 | } |
| 996 | |
| 997 | |
drh | 9de221d | 2008-01-05 06:51:30 +0000 | [diff] [blame] | 998 | /* Opcode: Blob P1 P2 * P4 |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 999 | ** |
drh | 9de221d | 2008-01-05 06:51:30 +0000 | [diff] [blame] | 1000 | ** P4 points to a blob of data P1 bytes long. Store this |
drh | 710c484 | 2010-08-30 01:17:20 +0000 | [diff] [blame] | 1001 | ** blob in register P2. |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 1002 | */ |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 1003 | case OP_Blob: { /* out2-prerelease */ |
drh | cbd2da9 | 2007-12-17 16:20:06 +0000 | [diff] [blame] | 1004 | assert( pOp->p1 <= SQLITE_MAX_LENGTH ); |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 1005 | sqlite3VdbeMemSetStr(pOut, pOp->p4.z, pOp->p1, 0, 0); |
drh | 9de221d | 2008-01-05 06:51:30 +0000 | [diff] [blame] | 1006 | pOut->enc = encoding; |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 1007 | UPDATE_MAX_BLOBSIZE(pOut); |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 1008 | break; |
| 1009 | } |
| 1010 | |
drh | eaf52d8 | 2010-05-12 13:50:23 +0000 | [diff] [blame] | 1011 | /* Opcode: Variable P1 P2 * P4 * |
drh | 5045789 | 2003-09-06 01:10:47 +0000 | [diff] [blame] | 1012 | ** |
drh | eaf52d8 | 2010-05-12 13:50:23 +0000 | [diff] [blame] | 1013 | ** Transfer the values of bound parameter P1 into register P2 |
drh | 08de149 | 2009-02-20 03:55:05 +0000 | [diff] [blame] | 1014 | ** |
| 1015 | ** If the parameter is named, then its name appears in P4 and P3==1. |
| 1016 | ** The P4 value is used by sqlite3_bind_parameter_name(). |
drh | 5045789 | 2003-09-06 01:10:47 +0000 | [diff] [blame] | 1017 | */ |
drh | eaf52d8 | 2010-05-12 13:50:23 +0000 | [diff] [blame] | 1018 | case OP_Variable: { /* out2-prerelease */ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1019 | Mem *pVar; /* Value being transferred */ |
| 1020 | |
drh | eaf52d8 | 2010-05-12 13:50:23 +0000 | [diff] [blame] | 1021 | assert( pOp->p1>0 && pOp->p1<=p->nVar ); |
drh | 04e9eea | 2011-06-01 19:16:06 +0000 | [diff] [blame] | 1022 | assert( pOp->p4.z==0 || pOp->p4.z==p->azVar[pOp->p1-1] ); |
drh | eaf52d8 | 2010-05-12 13:50:23 +0000 | [diff] [blame] | 1023 | pVar = &p->aVar[pOp->p1 - 1]; |
| 1024 | if( sqlite3VdbeMemTooBig(pVar) ){ |
| 1025 | goto too_big; |
drh | 023ae03 | 2007-05-08 12:12:16 +0000 | [diff] [blame] | 1026 | } |
drh | eaf52d8 | 2010-05-12 13:50:23 +0000 | [diff] [blame] | 1027 | sqlite3VdbeMemShallowCopy(pOut, pVar, MEM_Static); |
| 1028 | UPDATE_MAX_BLOBSIZE(pOut); |
danielk1977 | 93d4675 | 2004-05-23 13:30:58 +0000 | [diff] [blame] | 1029 | break; |
| 1030 | } |
danielk1977 | 295ba55 | 2004-05-19 10:34:51 +0000 | [diff] [blame] | 1031 | |
drh | b21e7c7 | 2008-06-22 12:37:57 +0000 | [diff] [blame] | 1032 | /* Opcode: Move P1 P2 P3 * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1033 | ** |
drh | b21e7c7 | 2008-06-22 12:37:57 +0000 | [diff] [blame] | 1034 | ** Move the values in register P1..P1+P3-1 over into |
| 1035 | ** registers P2..P2+P3-1. Registers P1..P1+P1-1 are |
| 1036 | ** left holding a NULL. It is an error for register ranges |
| 1037 | ** P1..P1+P3-1 and P2..P2+P3-1 to overlap. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1038 | */ |
drh | e1349cb | 2008-04-01 00:36:10 +0000 | [diff] [blame] | 1039 | case OP_Move: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1040 | char *zMalloc; /* Holding variable for allocated memory */ |
| 1041 | int n; /* Number of registers left to copy */ |
| 1042 | int p1; /* Register to copy from */ |
| 1043 | int p2; /* Register to copy to */ |
| 1044 | |
| 1045 | n = pOp->p3; |
| 1046 | p1 = pOp->p1; |
| 1047 | p2 = pOp->p2; |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1048 | assert( n>0 && p1>0 && p2>0 ); |
drh | b21e7c7 | 2008-06-22 12:37:57 +0000 | [diff] [blame] | 1049 | assert( p1+n<=p2 || p2+n<=p1 ); |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1050 | |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 1051 | pIn1 = &aMem[p1]; |
| 1052 | pOut = &aMem[p2]; |
drh | b21e7c7 | 2008-06-22 12:37:57 +0000 | [diff] [blame] | 1053 | while( n-- ){ |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 1054 | assert( pOut<=&aMem[p->nMem] ); |
| 1055 | assert( pIn1<=&aMem[p->nMem] ); |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 1056 | assert( memIsValid(pIn1) ); |
| 1057 | memAboutToChange(p, pOut); |
drh | b21e7c7 | 2008-06-22 12:37:57 +0000 | [diff] [blame] | 1058 | zMalloc = pOut->zMalloc; |
| 1059 | pOut->zMalloc = 0; |
| 1060 | sqlite3VdbeMemMove(pOut, pIn1); |
drh | 52043d7 | 2011-08-03 16:40:15 +0000 | [diff] [blame] | 1061 | #ifdef SQLITE_DEBUG |
| 1062 | if( pOut->pScopyFrom>=&aMem[p1] && pOut->pScopyFrom<&aMem[p1+pOp->p3] ){ |
| 1063 | pOut->pScopyFrom += p1 - pOp->p2; |
| 1064 | } |
| 1065 | #endif |
drh | b21e7c7 | 2008-06-22 12:37:57 +0000 | [diff] [blame] | 1066 | pIn1->zMalloc = zMalloc; |
| 1067 | REGISTER_TRACE(p2++, pOut); |
| 1068 | pIn1++; |
| 1069 | pOut++; |
| 1070 | } |
drh | e1349cb | 2008-04-01 00:36:10 +0000 | [diff] [blame] | 1071 | break; |
| 1072 | } |
| 1073 | |
drh | b1fdb2a | 2008-01-05 04:06:03 +0000 | [diff] [blame] | 1074 | /* Opcode: Copy P1 P2 * * * |
| 1075 | ** |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1076 | ** Make a copy of register P1 into register P2. |
drh | b1fdb2a | 2008-01-05 04:06:03 +0000 | [diff] [blame] | 1077 | ** |
| 1078 | ** This instruction makes a deep copy of the value. A duplicate |
| 1079 | ** is made of any string or blob constant. See also OP_SCopy. |
| 1080 | */ |
drh | 93952eb | 2009-11-13 19:43:43 +0000 | [diff] [blame] | 1081 | case OP_Copy: { /* in1, out2 */ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 1082 | pIn1 = &aMem[pOp->p1]; |
| 1083 | pOut = &aMem[pOp->p2]; |
drh | e1349cb | 2008-04-01 00:36:10 +0000 | [diff] [blame] | 1084 | assert( pOut!=pIn1 ); |
| 1085 | sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem); |
| 1086 | Deephemeralize(pOut); |
| 1087 | REGISTER_TRACE(pOp->p2, pOut); |
| 1088 | break; |
| 1089 | } |
| 1090 | |
drh | b1fdb2a | 2008-01-05 04:06:03 +0000 | [diff] [blame] | 1091 | /* Opcode: SCopy P1 P2 * * * |
| 1092 | ** |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1093 | ** Make a shallow copy of register P1 into register P2. |
drh | b1fdb2a | 2008-01-05 04:06:03 +0000 | [diff] [blame] | 1094 | ** |
| 1095 | ** This instruction makes a shallow copy of the value. If the value |
| 1096 | ** is a string or blob, then the copy is only a pointer to the |
| 1097 | ** original and hence if the original changes so will the copy. |
| 1098 | ** Worse, if the original is deallocated, the copy becomes invalid. |
| 1099 | ** Thus the program must guarantee that the original will not change |
| 1100 | ** during the lifetime of the copy. Use OP_Copy to make a complete |
| 1101 | ** copy. |
| 1102 | */ |
drh | 93952eb | 2009-11-13 19:43:43 +0000 | [diff] [blame] | 1103 | case OP_SCopy: { /* in1, out2 */ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 1104 | pIn1 = &aMem[pOp->p1]; |
| 1105 | pOut = &aMem[pOp->p2]; |
drh | 2d401ab | 2008-01-10 23:50:11 +0000 | [diff] [blame] | 1106 | assert( pOut!=pIn1 ); |
drh | e1349cb | 2008-04-01 00:36:10 +0000 | [diff] [blame] | 1107 | sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem); |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 1108 | #ifdef SQLITE_DEBUG |
| 1109 | if( pOut->pScopyFrom==0 ) pOut->pScopyFrom = pIn1; |
| 1110 | #endif |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1111 | REGISTER_TRACE(pOp->p2, pOut); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1112 | break; |
| 1113 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1114 | |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1115 | /* Opcode: ResultRow P1 P2 * * * |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 1116 | ** |
shane | 21e7feb | 2008-05-30 15:59:49 +0000 | [diff] [blame] | 1117 | ** The registers P1 through P1+P2-1 contain a single row of |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 1118 | ** results. This opcode causes the sqlite3_step() call to terminate |
| 1119 | ** with an SQLITE_ROW return code and it sets up the sqlite3_stmt |
| 1120 | ** structure to provide access to the top P1 values as the result |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1121 | ** row. |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 1122 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1123 | case OP_ResultRow: { |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 1124 | Mem *pMem; |
| 1125 | int i; |
| 1126 | assert( p->nResColumn==pOp->p2 ); |
drh | 0a07c10 | 2008-01-03 18:03:08 +0000 | [diff] [blame] | 1127 | assert( pOp->p1>0 ); |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1128 | assert( pOp->p1+pOp->p2<=p->nMem+1 ); |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 1129 | |
dan | 32b09f2 | 2009-09-23 17:29:59 +0000 | [diff] [blame] | 1130 | /* If this statement has violated immediate foreign key constraints, do |
| 1131 | ** not return the number of rows modified. And do not RELEASE the statement |
| 1132 | ** transaction. It needs to be rolled back. */ |
| 1133 | if( SQLITE_OK!=(rc = sqlite3VdbeCheckFk(p, 0)) ){ |
| 1134 | assert( db->flags&SQLITE_CountRows ); |
| 1135 | assert( p->usesStmtJournal ); |
| 1136 | break; |
| 1137 | } |
| 1138 | |
danielk1977 | bd43455 | 2009-03-18 10:33:00 +0000 | [diff] [blame] | 1139 | /* If the SQLITE_CountRows flag is set in sqlite3.flags mask, then |
| 1140 | ** DML statements invoke this opcode to return the number of rows |
| 1141 | ** modified to the user. This is the only way that a VM that |
| 1142 | ** opens a statement transaction may invoke this opcode. |
| 1143 | ** |
| 1144 | ** In case this is such a statement, close any statement transaction |
| 1145 | ** opened by this VM before returning control to the user. This is to |
| 1146 | ** ensure that statement-transactions are always nested, not overlapping. |
| 1147 | ** If the open statement-transaction is not closed here, then the user |
| 1148 | ** may step another VM that opens its own statement transaction. This |
| 1149 | ** may lead to overlapping statement transactions. |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 1150 | ** |
| 1151 | ** The statement transaction is never a top-level transaction. Hence |
| 1152 | ** the RELEASE call below can never fail. |
danielk1977 | bd43455 | 2009-03-18 10:33:00 +0000 | [diff] [blame] | 1153 | */ |
| 1154 | assert( p->iStatement==0 || db->flags&SQLITE_CountRows ); |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 1155 | rc = sqlite3VdbeCloseStatement(p, SAVEPOINT_RELEASE); |
| 1156 | if( NEVER(rc!=SQLITE_OK) ){ |
danielk1977 | bd43455 | 2009-03-18 10:33:00 +0000 | [diff] [blame] | 1157 | break; |
| 1158 | } |
| 1159 | |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 1160 | /* Invalidate all ephemeral cursor row caches */ |
| 1161 | p->cacheCtr = (p->cacheCtr + 2)|1; |
| 1162 | |
| 1163 | /* Make sure the results of the current row are \000 terminated |
shane | 21e7feb | 2008-05-30 15:59:49 +0000 | [diff] [blame] | 1164 | ** and have an assigned type. The results are de-ephemeralized as |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 1165 | ** as side effect. |
| 1166 | */ |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 1167 | pMem = p->pResultSet = &aMem[pOp->p1]; |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 1168 | for(i=0; i<pOp->p2; i++){ |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 1169 | assert( memIsValid(&pMem[i]) ); |
drh | ebc1671 | 2010-09-28 00:25:58 +0000 | [diff] [blame] | 1170 | Deephemeralize(&pMem[i]); |
drh | 746fd9c | 2010-09-28 06:00:47 +0000 | [diff] [blame] | 1171 | assert( (pMem[i].flags & MEM_Ephem)==0 |
| 1172 | || (pMem[i].flags & (MEM_Str|MEM_Blob))==0 ); |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 1173 | sqlite3VdbeMemNulTerminate(&pMem[i]); |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 1174 | sqlite3VdbeMemStoreType(&pMem[i]); |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 1175 | REGISTER_TRACE(pOp->p1+i, &pMem[i]); |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 1176 | } |
drh | 2803969 | 2008-03-17 16:54:01 +0000 | [diff] [blame] | 1177 | if( db->mallocFailed ) goto no_mem; |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 1178 | |
| 1179 | /* Return SQLITE_ROW |
| 1180 | */ |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 1181 | p->pc = pc + 1; |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 1182 | rc = SQLITE_ROW; |
| 1183 | goto vdbe_return; |
| 1184 | } |
| 1185 | |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1186 | /* Opcode: Concat P1 P2 P3 * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1187 | ** |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1188 | ** Add the text in register P1 onto the end of the text in |
| 1189 | ** register P2 and store the result in register P3. |
| 1190 | ** 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] | 1191 | ** |
| 1192 | ** P3 = P2 || P1 |
| 1193 | ** |
| 1194 | ** It is illegal for P1 and P3 to be the same register. Sometimes, |
| 1195 | ** if P3 is the same register as P2, the implementation is able |
| 1196 | ** to avoid a memcpy(). |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1197 | */ |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1198 | case OP_Concat: { /* same as TK_CONCAT, in1, in2, out3 */ |
drh | 023ae03 | 2007-05-08 12:12:16 +0000 | [diff] [blame] | 1199 | i64 nByte; |
danielk1977 | 8a6b541 | 2004-05-24 07:04:25 +0000 | [diff] [blame] | 1200 | |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 1201 | pIn1 = &aMem[pOp->p1]; |
| 1202 | pIn2 = &aMem[pOp->p2]; |
| 1203 | pOut = &aMem[pOp->p3]; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1204 | assert( pIn1!=pOut ); |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1205 | if( (pIn1->flags | pIn2->flags) & MEM_Null ){ |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1206 | sqlite3VdbeMemSetNull(pOut); |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1207 | break; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1208 | } |
drh | a0c0652 | 2009-06-17 22:50:41 +0000 | [diff] [blame] | 1209 | if( ExpandBlob(pIn1) || ExpandBlob(pIn2) ) goto no_mem; |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1210 | Stringify(pIn1, encoding); |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1211 | Stringify(pIn2, encoding); |
| 1212 | nByte = pIn1->n + pIn2->n; |
drh | bb4957f | 2008-03-20 14:03:29 +0000 | [diff] [blame] | 1213 | if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){ |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1214 | goto too_big; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1215 | } |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1216 | MemSetTypeFlag(pOut, MEM_Str); |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 1217 | if( sqlite3VdbeMemGrow(pOut, (int)nByte+2, pOut==pIn2) ){ |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1218 | goto no_mem; |
| 1219 | } |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1220 | if( pOut!=pIn2 ){ |
| 1221 | memcpy(pOut->z, pIn2->z, pIn2->n); |
| 1222 | } |
| 1223 | memcpy(&pOut->z[pIn2->n], pIn1->z, pIn1->n); |
| 1224 | pOut->z[nByte] = 0; |
| 1225 | pOut->z[nByte+1] = 0; |
| 1226 | pOut->flags |= MEM_Term; |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 1227 | pOut->n = (int)nByte; |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1228 | pOut->enc = encoding; |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 1229 | UPDATE_MAX_BLOBSIZE(pOut); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1230 | break; |
| 1231 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1232 | |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 1233 | /* Opcode: Add P1 P2 P3 * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1234 | ** |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1235 | ** Add the value in register P1 to the value in register P2 |
shane | 21e7feb | 2008-05-30 15:59:49 +0000 | [diff] [blame] | 1236 | ** and store the result in register P3. |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1237 | ** If either input is NULL, the result is NULL. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1238 | */ |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 1239 | /* Opcode: Multiply P1 P2 P3 * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1240 | ** |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 1241 | ** |
shane | 21e7feb | 2008-05-30 15:59:49 +0000 | [diff] [blame] | 1242 | ** Multiply the value in register P1 by the value in register P2 |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1243 | ** and store the result in register P3. |
| 1244 | ** If either input is NULL, the result is NULL. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1245 | */ |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 1246 | /* Opcode: Subtract P1 P2 P3 * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1247 | ** |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1248 | ** Subtract the value in register P1 from the value in register P2 |
| 1249 | ** and store the result in register P3. |
| 1250 | ** If either input is NULL, the result is NULL. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1251 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1252 | /* Opcode: Divide P1 P2 P3 * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1253 | ** |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1254 | ** Divide the value in register P1 by the value in register P2 |
dan | e275dc3 | 2009-08-18 16:24:58 +0000 | [diff] [blame] | 1255 | ** and store the result in register P3 (P3=P2/P1). If the value in |
| 1256 | ** register P1 is zero, then the result is NULL. If either input is |
| 1257 | ** NULL, the result is NULL. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1258 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1259 | /* Opcode: Remainder P1 P2 P3 * * |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1260 | ** |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 1261 | ** Compute the remainder after integer division of the value in |
| 1262 | ** register P1 by the value in register P2 and store the result in P3. |
| 1263 | ** If the value in register P2 is zero the result is NULL. |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1264 | ** If either operand is NULL, the result is NULL. |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1265 | */ |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1266 | case OP_Add: /* same as TK_PLUS, in1, in2, out3 */ |
| 1267 | case OP_Subtract: /* same as TK_MINUS, in1, in2, out3 */ |
| 1268 | case OP_Multiply: /* same as TK_STAR, in1, in2, out3 */ |
| 1269 | case OP_Divide: /* same as TK_SLASH, in1, in2, out3 */ |
| 1270 | case OP_Remainder: { /* same as TK_REM, in1, in2, out3 */ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1271 | int flags; /* Combined MEM_* flags from both inputs */ |
| 1272 | i64 iA; /* Integer value of left operand */ |
| 1273 | i64 iB; /* Integer value of right operand */ |
| 1274 | double rA; /* Real value of left operand */ |
| 1275 | double rB; /* Real value of right operand */ |
| 1276 | |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 1277 | pIn1 = &aMem[pOp->p1]; |
drh | 61669b3 | 2008-07-30 13:27:10 +0000 | [diff] [blame] | 1278 | applyNumericAffinity(pIn1); |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 1279 | pIn2 = &aMem[pOp->p2]; |
drh | 61669b3 | 2008-07-30 13:27:10 +0000 | [diff] [blame] | 1280 | applyNumericAffinity(pIn2); |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 1281 | pOut = &aMem[pOp->p3]; |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1282 | flags = pIn1->flags | pIn2->flags; |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 1283 | if( (flags & MEM_Null)!=0 ) goto arithmetic_result_is_null; |
| 1284 | if( (pIn1->flags & pIn2->flags & MEM_Int)==MEM_Int ){ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1285 | iA = pIn1->u.i; |
| 1286 | iB = pIn2->u.i; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1287 | switch( pOp->opcode ){ |
drh | 158b9cb | 2011-03-05 20:59:46 +0000 | [diff] [blame] | 1288 | case OP_Add: if( sqlite3AddInt64(&iB,iA) ) goto fp_math; break; |
| 1289 | case OP_Subtract: if( sqlite3SubInt64(&iB,iA) ) goto fp_math; break; |
| 1290 | case OP_Multiply: if( sqlite3MulInt64(&iB,iA) ) goto fp_math; break; |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1291 | case OP_Divide: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1292 | if( iA==0 ) goto arithmetic_result_is_null; |
drh | 158b9cb | 2011-03-05 20:59:46 +0000 | [diff] [blame] | 1293 | if( iA==-1 && iB==SMALLEST_INT64 ) goto fp_math; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1294 | iB /= iA; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1295 | break; |
| 1296 | } |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1297 | default: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1298 | if( iA==0 ) goto arithmetic_result_is_null; |
| 1299 | if( iA==-1 ) iA = 1; |
| 1300 | iB %= iA; |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1301 | break; |
| 1302 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1303 | } |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1304 | pOut->u.i = iB; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1305 | MemSetTypeFlag(pOut, MEM_Int); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1306 | }else{ |
drh | 158b9cb | 2011-03-05 20:59:46 +0000 | [diff] [blame] | 1307 | fp_math: |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1308 | rA = sqlite3VdbeRealValue(pIn1); |
| 1309 | rB = sqlite3VdbeRealValue(pIn2); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1310 | switch( pOp->opcode ){ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1311 | case OP_Add: rB += rA; break; |
| 1312 | case OP_Subtract: rB -= rA; break; |
| 1313 | case OP_Multiply: rB *= rA; break; |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1314 | case OP_Divide: { |
shane | fbd60f8 | 2009-02-04 03:59:25 +0000 | [diff] [blame] | 1315 | /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1316 | if( rA==(double)0 ) goto arithmetic_result_is_null; |
| 1317 | rB /= rA; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1318 | break; |
| 1319 | } |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1320 | default: { |
shane | 75ac1de | 2009-06-09 18:58:52 +0000 | [diff] [blame] | 1321 | iA = (i64)rA; |
| 1322 | iB = (i64)rB; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1323 | if( iA==0 ) goto arithmetic_result_is_null; |
| 1324 | if( iA==-1 ) iA = 1; |
| 1325 | rB = (double)(iB % iA); |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1326 | break; |
| 1327 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1328 | } |
drh | c5a7b51 | 2010-01-13 16:25:42 +0000 | [diff] [blame] | 1329 | #ifdef SQLITE_OMIT_FLOATING_POINT |
| 1330 | pOut->u.i = rB; |
| 1331 | MemSetTypeFlag(pOut, MEM_Int); |
| 1332 | #else |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1333 | if( sqlite3IsNaN(rB) ){ |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 1334 | goto arithmetic_result_is_null; |
drh | 53c1402 | 2007-05-10 17:23:11 +0000 | [diff] [blame] | 1335 | } |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1336 | pOut->r = rB; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1337 | MemSetTypeFlag(pOut, MEM_Real); |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1338 | if( (flags & MEM_Real)==0 ){ |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1339 | sqlite3VdbeIntegerAffinity(pOut); |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1340 | } |
drh | c5a7b51 | 2010-01-13 16:25:42 +0000 | [diff] [blame] | 1341 | #endif |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1342 | } |
| 1343 | break; |
| 1344 | |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 1345 | arithmetic_result_is_null: |
| 1346 | sqlite3VdbeMemSetNull(pOut); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1347 | break; |
| 1348 | } |
| 1349 | |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 1350 | /* Opcode: CollSeq * * P4 |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 1351 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 1352 | ** 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] | 1353 | ** or aggregate calls sqlite3GetFuncCollSeq(), this collation sequence will |
| 1354 | ** be returned. This is used by the built-in min(), max() and nullif() |
drh | e6f85e7 | 2004-12-25 01:03:13 +0000 | [diff] [blame] | 1355 | ** functions. |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 1356 | ** |
| 1357 | ** The interface used by the implementation of the aforementioned functions |
| 1358 | ** to retrieve the collation sequence set by this opcode is not available |
| 1359 | ** publicly, only to user functions defined in func.c. |
| 1360 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1361 | case OP_CollSeq: { |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 1362 | assert( pOp->p4type==P4_COLLSEQ ); |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 1363 | break; |
| 1364 | } |
| 1365 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1366 | /* Opcode: Function P1 P2 P3 P4 P5 |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 1367 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 1368 | ** Invoke a user function (P4 is a pointer to a Function structure that |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1369 | ** defines the function) with P5 arguments taken from register P2 and |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1370 | ** successors. The result of the function is stored in register P3. |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1371 | ** Register P3 must not be one of the function inputs. |
danielk1977 | 682f68b | 2004-06-05 10:22:17 +0000 | [diff] [blame] | 1372 | ** |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 1373 | ** 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] | 1374 | ** function was determined to be constant at compile time. If the first |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 1375 | ** 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] | 1376 | ** whether meta data associated with a user function argument using the |
| 1377 | ** sqlite3_set_auxdata() API may be safely retained until the next |
| 1378 | ** invocation of this opcode. |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 1379 | ** |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 1380 | ** See also: AggStep and AggFinal |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 1381 | */ |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 1382 | case OP_Function: { |
danielk1977 | 51ad0ec | 2004-05-24 12:39:02 +0000 | [diff] [blame] | 1383 | int i; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1384 | Mem *pArg; |
danielk1977 | 22322fd | 2004-05-25 23:35:17 +0000 | [diff] [blame] | 1385 | sqlite3_context ctx; |
danielk1977 | 51ad0ec | 2004-05-24 12:39:02 +0000 | [diff] [blame] | 1386 | sqlite3_value **apVal; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1387 | int n; |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 1388 | |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1389 | n = pOp->p5; |
danielk1977 | 6ddcca5 | 2004-05-24 23:48:25 +0000 | [diff] [blame] | 1390 | apVal = p->apArg; |
danielk1977 | 51ad0ec | 2004-05-24 12:39:02 +0000 | [diff] [blame] | 1391 | assert( apVal || n==0 ); |
drh | ebc1671 | 2010-09-28 00:25:58 +0000 | [diff] [blame] | 1392 | assert( pOp->p3>0 && pOp->p3<=p->nMem ); |
| 1393 | pOut = &aMem[pOp->p3]; |
| 1394 | memAboutToChange(p, pOut); |
danielk1977 | 51ad0ec | 2004-05-24 12:39:02 +0000 | [diff] [blame] | 1395 | |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1396 | assert( n==0 || (pOp->p2>0 && pOp->p2+n<=p->nMem+1) ); |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1397 | assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n ); |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 1398 | pArg = &aMem[pOp->p2]; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1399 | for(i=0; i<n; i++, pArg++){ |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 1400 | assert( memIsValid(pArg) ); |
danielk1977 | 51ad0ec | 2004-05-24 12:39:02 +0000 | [diff] [blame] | 1401 | apVal[i] = pArg; |
drh | ebc1671 | 2010-09-28 00:25:58 +0000 | [diff] [blame] | 1402 | Deephemeralize(pArg); |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 1403 | sqlite3VdbeMemStoreType(pArg); |
drh | ab5cd70 | 2010-04-07 14:32:11 +0000 | [diff] [blame] | 1404 | REGISTER_TRACE(pOp->p2+i, pArg); |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 1405 | } |
danielk1977 | 51ad0ec | 2004-05-24 12:39:02 +0000 | [diff] [blame] | 1406 | |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 1407 | assert( pOp->p4type==P4_FUNCDEF || pOp->p4type==P4_VDBEFUNC ); |
| 1408 | if( pOp->p4type==P4_FUNCDEF ){ |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 1409 | ctx.pFunc = pOp->p4.pFunc; |
danielk1977 | 682f68b | 2004-06-05 10:22:17 +0000 | [diff] [blame] | 1410 | ctx.pVdbeFunc = 0; |
| 1411 | }else{ |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 1412 | ctx.pVdbeFunc = (VdbeFunc*)pOp->p4.pVdbeFunc; |
danielk1977 | 682f68b | 2004-06-05 10:22:17 +0000 | [diff] [blame] | 1413 | ctx.pFunc = ctx.pVdbeFunc->pFunc; |
| 1414 | } |
| 1415 | |
drh | 00706be | 2004-01-30 14:49:16 +0000 | [diff] [blame] | 1416 | ctx.s.flags = MEM_Null; |
drh | fa4a4b9 | 2008-03-19 21:45:51 +0000 | [diff] [blame] | 1417 | ctx.s.db = db; |
danielk1977 | 5f09613 | 2008-03-28 15:44:09 +0000 | [diff] [blame] | 1418 | ctx.s.xDel = 0; |
| 1419 | ctx.s.zMalloc = 0; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1420 | |
| 1421 | /* The output cell may already have a buffer allocated. Move |
| 1422 | ** the pointer to ctx.s so in case the user-function can use |
| 1423 | ** the already allocated buffer instead of allocating a new one. |
| 1424 | */ |
| 1425 | sqlite3VdbeMemMove(&ctx.s, pOut); |
| 1426 | MemSetTypeFlag(&ctx.s, MEM_Null); |
| 1427 | |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 1428 | ctx.isError = 0; |
drh | e82f5d0 | 2008-10-07 19:53:14 +0000 | [diff] [blame] | 1429 | if( ctx.pFunc->flags & SQLITE_FUNC_NEEDCOLL ){ |
drh | bbe879d | 2009-11-14 18:04:35 +0000 | [diff] [blame] | 1430 | assert( pOp>aOp ); |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 1431 | assert( pOp[-1].p4type==P4_COLLSEQ ); |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 1432 | assert( pOp[-1].opcode==OP_CollSeq ); |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 1433 | ctx.pColl = pOp[-1].p4.pColl; |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 1434 | } |
drh | 99a6692 | 2011-05-13 18:51:42 +0000 | [diff] [blame] | 1435 | db->lastRowid = lastRowid; |
drh | ee9ff67 | 2010-09-03 18:50:48 +0000 | [diff] [blame] | 1436 | (*ctx.pFunc->xFunc)(&ctx, n, apVal); /* IMP: R-24505-23230 */ |
drh | 99a6692 | 2011-05-13 18:51:42 +0000 | [diff] [blame] | 1437 | lastRowid = db->lastRowid; |
danielk1977 | 7e18c25 | 2004-05-25 11:47:24 +0000 | [diff] [blame] | 1438 | |
shane | 21e7feb | 2008-05-30 15:59:49 +0000 | [diff] [blame] | 1439 | /* If any auxiliary data functions have been called by this user function, |
danielk1977 | 682f68b | 2004-06-05 10:22:17 +0000 | [diff] [blame] | 1440 | ** immediately call the destructor for any non-static values. |
| 1441 | */ |
| 1442 | if( ctx.pVdbeFunc ){ |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 1443 | sqlite3VdbeDeleteAuxData(ctx.pVdbeFunc, pOp->p1); |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 1444 | pOp->p4.pVdbeFunc = ctx.pVdbeFunc; |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 1445 | pOp->p4type = P4_VDBEFUNC; |
danielk1977 | 682f68b | 2004-06-05 10:22:17 +0000 | [diff] [blame] | 1446 | } |
| 1447 | |
dan | 5f84e14 | 2011-06-14 14:18:45 +0000 | [diff] [blame] | 1448 | if( db->mallocFailed ){ |
| 1449 | /* Even though a malloc() has failed, the implementation of the |
| 1450 | ** user function may have called an sqlite3_result_XXX() function |
| 1451 | ** to return a value. The following call releases any resources |
| 1452 | ** associated with such a value. |
| 1453 | */ |
| 1454 | sqlite3VdbeMemRelease(&ctx.s); |
| 1455 | goto no_mem; |
| 1456 | } |
| 1457 | |
drh | 90669c1 | 2006-01-20 15:45:36 +0000 | [diff] [blame] | 1458 | /* If the function returned an error, throw an exception */ |
| 1459 | if( ctx.isError ){ |
drh | f089aa4 | 2008-07-08 19:34:06 +0000 | [diff] [blame] | 1460 | sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(&ctx.s)); |
drh | 69544ec | 2008-02-06 14:11:34 +0000 | [diff] [blame] | 1461 | rc = ctx.isError; |
drh | 90669c1 | 2006-01-20 15:45:36 +0000 | [diff] [blame] | 1462 | } |
| 1463 | |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1464 | /* Copy the result of the function into register P3 */ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 1465 | sqlite3VdbeChangeEncoding(&ctx.s, encoding); |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1466 | sqlite3VdbeMemMove(pOut, &ctx.s); |
| 1467 | if( sqlite3VdbeMemTooBig(pOut) ){ |
drh | 023ae03 | 2007-05-08 12:12:16 +0000 | [diff] [blame] | 1468 | goto too_big; |
| 1469 | } |
drh | 7b94e7f | 2011-04-04 12:29:20 +0000 | [diff] [blame] | 1470 | |
| 1471 | #if 0 |
| 1472 | /* The app-defined function has done something that as caused this |
| 1473 | ** statement to expire. (Perhaps the function called sqlite3_exec() |
| 1474 | ** with a CREATE TABLE statement.) |
| 1475 | */ |
| 1476 | if( p->expired ) rc = SQLITE_ABORT; |
| 1477 | #endif |
| 1478 | |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 1479 | REGISTER_TRACE(pOp->p3, pOut); |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 1480 | UPDATE_MAX_BLOBSIZE(pOut); |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 1481 | break; |
| 1482 | } |
| 1483 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1484 | /* Opcode: BitAnd P1 P2 P3 * * |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1485 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1486 | ** Take the bit-wise AND of the values in register P1 and P2 and |
| 1487 | ** store the result in register P3. |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1488 | ** If either input is NULL, the result is NULL. |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1489 | */ |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1490 | /* Opcode: BitOr P1 P2 P3 * * |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1491 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1492 | ** Take the bit-wise OR of the values in register P1 and P2 and |
| 1493 | ** store the result in register P3. |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1494 | ** If either input is NULL, the result is NULL. |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1495 | */ |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1496 | /* Opcode: ShiftLeft P1 P2 P3 * * |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1497 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1498 | ** Shift the integer value in register P2 to the left by the |
drh | 710c484 | 2010-08-30 01:17:20 +0000 | [diff] [blame] | 1499 | ** number of bits specified by the integer in register P1. |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1500 | ** Store the result in register P3. |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1501 | ** If either input is NULL, the result is NULL. |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1502 | */ |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1503 | /* Opcode: ShiftRight P1 P2 P3 * * |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1504 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1505 | ** Shift the integer value in register P2 to the right by the |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1506 | ** number of bits specified by the integer in register P1. |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1507 | ** Store the result in register P3. |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1508 | ** If either input is NULL, the result is NULL. |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1509 | */ |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1510 | case OP_BitAnd: /* same as TK_BITAND, in1, in2, out3 */ |
| 1511 | case OP_BitOr: /* same as TK_BITOR, in1, in2, out3 */ |
| 1512 | case OP_ShiftLeft: /* same as TK_LSHIFT, in1, in2, out3 */ |
| 1513 | case OP_ShiftRight: { /* same as TK_RSHIFT, in1, in2, out3 */ |
drh | 158b9cb | 2011-03-05 20:59:46 +0000 | [diff] [blame] | 1514 | i64 iA; |
| 1515 | u64 uA; |
| 1516 | i64 iB; |
| 1517 | u8 op; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1518 | |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 1519 | pIn1 = &aMem[pOp->p1]; |
| 1520 | pIn2 = &aMem[pOp->p2]; |
| 1521 | pOut = &aMem[pOp->p3]; |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1522 | if( (pIn1->flags | pIn2->flags) & MEM_Null ){ |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 1523 | sqlite3VdbeMemSetNull(pOut); |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1524 | break; |
| 1525 | } |
drh | 158b9cb | 2011-03-05 20:59:46 +0000 | [diff] [blame] | 1526 | iA = sqlite3VdbeIntValue(pIn2); |
| 1527 | iB = sqlite3VdbeIntValue(pIn1); |
| 1528 | op = pOp->opcode; |
| 1529 | if( op==OP_BitAnd ){ |
| 1530 | iA &= iB; |
| 1531 | }else if( op==OP_BitOr ){ |
| 1532 | iA |= iB; |
| 1533 | }else if( iB!=0 ){ |
| 1534 | assert( op==OP_ShiftRight || op==OP_ShiftLeft ); |
| 1535 | |
| 1536 | /* If shifting by a negative amount, shift in the other direction */ |
| 1537 | if( iB<0 ){ |
| 1538 | assert( OP_ShiftRight==OP_ShiftLeft+1 ); |
| 1539 | op = 2*OP_ShiftLeft + 1 - op; |
| 1540 | iB = iB>(-64) ? -iB : 64; |
| 1541 | } |
| 1542 | |
| 1543 | if( iB>=64 ){ |
| 1544 | iA = (iA>=0 || op==OP_ShiftLeft) ? 0 : -1; |
| 1545 | }else{ |
| 1546 | memcpy(&uA, &iA, sizeof(uA)); |
| 1547 | if( op==OP_ShiftLeft ){ |
| 1548 | uA <<= iB; |
| 1549 | }else{ |
| 1550 | uA >>= iB; |
| 1551 | /* Sign-extend on a right shift of a negative number */ |
| 1552 | if( iA<0 ) uA |= ((((u64)0xffffffff)<<32)|0xffffffff) << (64-iB); |
| 1553 | } |
| 1554 | memcpy(&iA, &uA, sizeof(iA)); |
| 1555 | } |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1556 | } |
drh | 158b9cb | 2011-03-05 20:59:46 +0000 | [diff] [blame] | 1557 | pOut->u.i = iA; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1558 | MemSetTypeFlag(pOut, MEM_Int); |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1559 | break; |
| 1560 | } |
| 1561 | |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1562 | /* Opcode: AddImm P1 P2 * * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1563 | ** |
danielk1977 | 0cdc022 | 2008-06-26 18:04:03 +0000 | [diff] [blame] | 1564 | ** Add the constant P2 to the value in register P1. |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1565 | ** The result is always an integer. |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 1566 | ** |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1567 | ** To force any register to be an integer, just add 0. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1568 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1569 | case OP_AddImm: { /* in1 */ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 1570 | pIn1 = &aMem[pOp->p1]; |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 1571 | memAboutToChange(p, pIn1); |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1572 | sqlite3VdbeMemIntegerify(pIn1); |
| 1573 | pIn1->u.i += pOp->p2; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1574 | break; |
| 1575 | } |
| 1576 | |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1577 | /* Opcode: MustBeInt P1 P2 * * * |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 1578 | ** |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1579 | ** Force the value in register P1 to be an integer. If the value |
| 1580 | ** in P1 is not an integer and cannot be converted into an integer |
danielk1977 | 9a96b66 | 2007-11-29 17:05:18 +0000 | [diff] [blame] | 1581 | ** without data loss, then jump immediately to P2, or if P2==0 |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 1582 | ** raise an SQLITE_MISMATCH exception. |
| 1583 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1584 | case OP_MustBeInt: { /* jump, in1 */ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 1585 | pIn1 = &aMem[pOp->p1]; |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 1586 | applyAffinity(pIn1, SQLITE_AFF_NUMERIC, encoding); |
| 1587 | if( (pIn1->flags & MEM_Int)==0 ){ |
drh | 17c4029 | 2004-07-21 02:53:29 +0000 | [diff] [blame] | 1588 | if( pOp->p2==0 ){ |
| 1589 | rc = SQLITE_MISMATCH; |
| 1590 | goto abort_due_to_error; |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 1591 | }else{ |
drh | 17c4029 | 2004-07-21 02:53:29 +0000 | [diff] [blame] | 1592 | pc = pOp->p2 - 1; |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 1593 | } |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 1594 | }else{ |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1595 | MemSetTypeFlag(pIn1, MEM_Int); |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 1596 | } |
| 1597 | break; |
| 1598 | } |
| 1599 | |
drh | 13573c7 | 2010-01-12 17:04:07 +0000 | [diff] [blame] | 1600 | #ifndef SQLITE_OMIT_FLOATING_POINT |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1601 | /* Opcode: RealAffinity P1 * * * * |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 1602 | ** |
drh | 2133d82 | 2008-01-03 18:44:59 +0000 | [diff] [blame] | 1603 | ** If register P1 holds an integer convert it to a real value. |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 1604 | ** |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1605 | ** This opcode is used when extracting information from a column that |
| 1606 | ** has REAL affinity. Such column values may still be stored as |
| 1607 | ** integers, for space efficiency, but after extraction we want them |
| 1608 | ** to have only a real value. |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 1609 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1610 | case OP_RealAffinity: { /* in1 */ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 1611 | pIn1 = &aMem[pOp->p1]; |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1612 | if( pIn1->flags & MEM_Int ){ |
| 1613 | sqlite3VdbeMemRealify(pIn1); |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1614 | } |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 1615 | break; |
| 1616 | } |
drh | 13573c7 | 2010-01-12 17:04:07 +0000 | [diff] [blame] | 1617 | #endif |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 1618 | |
drh | 8df447f | 2005-11-01 15:48:24 +0000 | [diff] [blame] | 1619 | #ifndef SQLITE_OMIT_CAST |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1620 | /* Opcode: ToText P1 * * * * |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 1621 | ** |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1622 | ** Force the value in register P1 to be text. |
drh | 31beae9 | 2005-11-24 14:34:36 +0000 | [diff] [blame] | 1623 | ** If the value is numeric, convert it to a string using the |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 1624 | ** equivalent of printf(). Blob values are unchanged and |
| 1625 | ** are afterwards simply interpreted as text. |
| 1626 | ** |
| 1627 | ** A NULL value is not changed by this routine. It remains NULL. |
| 1628 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1629 | case OP_ToText: { /* same as TK_TO_TEXT, in1 */ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 1630 | pIn1 = &aMem[pOp->p1]; |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 1631 | memAboutToChange(p, pIn1); |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1632 | if( pIn1->flags & MEM_Null ) break; |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 1633 | assert( MEM_Str==(MEM_Blob>>3) ); |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1634 | pIn1->flags |= (pIn1->flags&MEM_Blob)>>3; |
| 1635 | applyAffinity(pIn1, SQLITE_AFF_TEXT, encoding); |
| 1636 | rc = ExpandBlob(pIn1); |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1637 | assert( pIn1->flags & MEM_Str || db->mallocFailed ); |
drh | 68ac65e | 2009-01-05 18:02:27 +0000 | [diff] [blame] | 1638 | pIn1->flags &= ~(MEM_Int|MEM_Real|MEM_Blob|MEM_Zero); |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 1639 | UPDATE_MAX_BLOBSIZE(pIn1); |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 1640 | break; |
| 1641 | } |
| 1642 | |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1643 | /* Opcode: ToBlob P1 * * * * |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 1644 | ** |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1645 | ** Force the value in register P1 to be a BLOB. |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 1646 | ** If the value is numeric, convert it to a string first. |
| 1647 | ** Strings are simply reinterpreted as blobs with no change |
| 1648 | ** to the underlying data. |
| 1649 | ** |
| 1650 | ** A NULL value is not changed by this routine. It remains NULL. |
| 1651 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1652 | case OP_ToBlob: { /* same as TK_TO_BLOB, in1 */ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 1653 | pIn1 = &aMem[pOp->p1]; |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1654 | if( pIn1->flags & MEM_Null ) break; |
| 1655 | if( (pIn1->flags & MEM_Blob)==0 ){ |
| 1656 | applyAffinity(pIn1, SQLITE_AFF_TEXT, encoding); |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1657 | assert( pIn1->flags & MEM_Str || db->mallocFailed ); |
drh | de58ddb | 2009-01-05 22:30:38 +0000 | [diff] [blame] | 1658 | MemSetTypeFlag(pIn1, MEM_Blob); |
| 1659 | }else{ |
| 1660 | pIn1->flags &= ~(MEM_TypeMask&~MEM_Blob); |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 1661 | } |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 1662 | UPDATE_MAX_BLOBSIZE(pIn1); |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 1663 | break; |
| 1664 | } |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1665 | |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1666 | /* Opcode: ToNumeric P1 * * * * |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1667 | ** |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1668 | ** Force the value in register P1 to be numeric (either an |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1669 | ** integer or a floating-point number.) |
| 1670 | ** If the value is text or blob, try to convert it to an using the |
| 1671 | ** equivalent of atoi() or atof() and store 0 if no such conversion |
| 1672 | ** is possible. |
| 1673 | ** |
| 1674 | ** A NULL value is not changed by this routine. It remains NULL. |
| 1675 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1676 | case OP_ToNumeric: { /* same as TK_TO_NUMERIC, in1 */ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 1677 | pIn1 = &aMem[pOp->p1]; |
drh | 9351862 | 2010-09-30 14:48:06 +0000 | [diff] [blame] | 1678 | sqlite3VdbeMemNumerify(pIn1); |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1679 | break; |
| 1680 | } |
| 1681 | #endif /* SQLITE_OMIT_CAST */ |
| 1682 | |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1683 | /* Opcode: ToInt P1 * * * * |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1684 | ** |
drh | 710c484 | 2010-08-30 01:17:20 +0000 | [diff] [blame] | 1685 | ** Force the value in register P1 to be an integer. If |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1686 | ** The value is currently a real number, drop its fractional part. |
| 1687 | ** If the value is text or blob, try to convert it to an integer using the |
| 1688 | ** equivalent of atoi() and store 0 if no such conversion is possible. |
| 1689 | ** |
| 1690 | ** A NULL value is not changed by this routine. It remains NULL. |
| 1691 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1692 | case OP_ToInt: { /* same as TK_TO_INT, in1 */ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 1693 | pIn1 = &aMem[pOp->p1]; |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1694 | if( (pIn1->flags & MEM_Null)==0 ){ |
| 1695 | sqlite3VdbeMemIntegerify(pIn1); |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1696 | } |
| 1697 | break; |
| 1698 | } |
| 1699 | |
drh | 13573c7 | 2010-01-12 17:04:07 +0000 | [diff] [blame] | 1700 | #if !defined(SQLITE_OMIT_CAST) && !defined(SQLITE_OMIT_FLOATING_POINT) |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1701 | /* Opcode: ToReal P1 * * * * |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1702 | ** |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1703 | ** Force the value in register P1 to be a floating point number. |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1704 | ** If The value is currently an integer, convert it. |
| 1705 | ** 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] | 1706 | ** equivalent of atoi() and store 0.0 if no such conversion is possible. |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1707 | ** |
| 1708 | ** A NULL value is not changed by this routine. It remains NULL. |
| 1709 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1710 | case OP_ToReal: { /* same as TK_TO_REAL, in1 */ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 1711 | pIn1 = &aMem[pOp->p1]; |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 1712 | memAboutToChange(p, pIn1); |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1713 | if( (pIn1->flags & MEM_Null)==0 ){ |
| 1714 | sqlite3VdbeMemRealify(pIn1); |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1715 | } |
| 1716 | break; |
| 1717 | } |
drh | 13573c7 | 2010-01-12 17:04:07 +0000 | [diff] [blame] | 1718 | #endif /* !defined(SQLITE_OMIT_CAST) && !defined(SQLITE_OMIT_FLOATING_POINT) */ |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 1719 | |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 1720 | /* Opcode: Lt P1 P2 P3 P4 P5 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1721 | ** |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 1722 | ** Compare the values in register P1 and P3. If reg(P3)<reg(P1) then |
| 1723 | ** jump to address P2. |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1724 | ** |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 1725 | ** If the SQLITE_JUMPIFNULL bit of P5 is set and either reg(P1) or |
| 1726 | ** reg(P3) is NULL then take the jump. If the SQLITE_JUMPIFNULL |
drh | 710c484 | 2010-08-30 01:17:20 +0000 | [diff] [blame] | 1727 | ** bit is clear then fall through if either operand is NULL. |
drh | 4f68623 | 2005-09-20 13:55:18 +0000 | [diff] [blame] | 1728 | ** |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 1729 | ** The SQLITE_AFF_MASK portion of P5 must be an affinity character - |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1730 | ** SQLITE_AFF_TEXT, SQLITE_AFF_INTEGER, and so forth. An attempt is made |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1731 | ** to coerce both inputs according to this affinity before the |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 1732 | ** comparison is made. If the SQLITE_AFF_MASK is 0x00, then numeric |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1733 | ** affinity is used. Note that the affinity conversions are stored |
| 1734 | ** back into the input registers P1 and P3. So this opcode can cause |
| 1735 | ** persistent changes to registers P1 and P3. |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 1736 | ** |
| 1737 | ** Once any conversions have taken place, and neither value is NULL, |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 1738 | ** the values are compared. If both values are blobs then memcmp() is |
| 1739 | ** used to determine the results of the comparison. If both values |
| 1740 | ** are text, then the appropriate collating function specified in |
| 1741 | ** P4 is used to do the comparison. If P4 is not specified then |
| 1742 | ** memcmp() is used to compare text string. If both values are |
| 1743 | ** numeric, then a numeric comparison is used. If the two values |
| 1744 | ** are of different types, then numbers are considered less than |
| 1745 | ** strings and strings are considered less than blobs. |
drh | c9b84a1 | 2002-06-20 11:36:48 +0000 | [diff] [blame] | 1746 | ** |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 1747 | ** If the SQLITE_STOREP2 bit of P5 is set, then do not jump. Instead, |
| 1748 | ** store a boolean result (either 0, or 1, or NULL) in register P2. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1749 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1750 | /* Opcode: Ne P1 P2 P3 P4 P5 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1751 | ** |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 1752 | ** This works just like the Lt opcode except that the jump is taken if |
| 1753 | ** 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] | 1754 | ** additional information. |
drh | 6a2fe09 | 2009-09-23 02:29:36 +0000 | [diff] [blame] | 1755 | ** |
| 1756 | ** If SQLITE_NULLEQ is set in P5 then the result of comparison is always either |
| 1757 | ** true or false and is never NULL. If both operands are NULL then the result |
| 1758 | ** of comparison is false. If either operand is NULL then the result is true. |
drh | ef8662b | 2011-06-20 21:47:58 +0000 | [diff] [blame] | 1759 | ** If neither operand is NULL the result is the same as it would be if |
drh | 6a2fe09 | 2009-09-23 02:29:36 +0000 | [diff] [blame] | 1760 | ** the SQLITE_NULLEQ flag were omitted from P5. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1761 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1762 | /* Opcode: Eq P1 P2 P3 P4 P5 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1763 | ** |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 1764 | ** This works just like the Lt opcode except that the jump is taken if |
| 1765 | ** the operands in registers P1 and P3 are equal. |
| 1766 | ** See the Lt opcode for additional information. |
drh | 6a2fe09 | 2009-09-23 02:29:36 +0000 | [diff] [blame] | 1767 | ** |
| 1768 | ** If SQLITE_NULLEQ is set in P5 then the result of comparison is always either |
| 1769 | ** true or false and is never NULL. If both operands are NULL then the result |
| 1770 | ** of comparison is true. If either operand is NULL then the result is false. |
drh | ef8662b | 2011-06-20 21:47:58 +0000 | [diff] [blame] | 1771 | ** If neither operand is NULL the result is the same as it would be if |
drh | 6a2fe09 | 2009-09-23 02:29:36 +0000 | [diff] [blame] | 1772 | ** the SQLITE_NULLEQ flag were omitted from P5. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1773 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1774 | /* Opcode: Le P1 P2 P3 P4 P5 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1775 | ** |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 1776 | ** This works just like the Lt opcode except that the jump is taken if |
| 1777 | ** the content of register P3 is less than or equal to the content of |
| 1778 | ** register P1. See the Lt opcode for additional information. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1779 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1780 | /* Opcode: Gt P1 P2 P3 P4 P5 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1781 | ** |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 1782 | ** This works just like the Lt opcode except that the jump is taken if |
| 1783 | ** the content of register P3 is greater than the content of |
| 1784 | ** register P1. See the Lt opcode for additional information. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1785 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1786 | /* Opcode: Ge P1 P2 P3 P4 P5 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1787 | ** |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 1788 | ** This works just like the Lt opcode except that the jump is taken if |
| 1789 | ** the content of register P3 is greater than or equal to the content of |
| 1790 | ** register P1. See the Lt opcode for additional information. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1791 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1792 | case OP_Eq: /* same as TK_EQ, jump, in1, in3 */ |
| 1793 | case OP_Ne: /* same as TK_NE, jump, in1, in3 */ |
| 1794 | case OP_Lt: /* same as TK_LT, jump, in1, in3 */ |
| 1795 | case OP_Le: /* same as TK_LE, jump, in1, in3 */ |
| 1796 | case OP_Gt: /* same as TK_GT, jump, in1, in3 */ |
| 1797 | case OP_Ge: { /* same as TK_GE, jump, in1, in3 */ |
drh | 6a2fe09 | 2009-09-23 02:29:36 +0000 | [diff] [blame] | 1798 | int res; /* Result of the comparison of pIn1 against pIn3 */ |
| 1799 | char affinity; /* Affinity to use for comparison */ |
dan | b7dca7d | 2010-03-05 16:32:12 +0000 | [diff] [blame] | 1800 | u16 flags1; /* Copy of initial value of pIn1->flags */ |
| 1801 | u16 flags3; /* Copy of initial value of pIn3->flags */ |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 1802 | |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 1803 | pIn1 = &aMem[pOp->p1]; |
| 1804 | pIn3 = &aMem[pOp->p3]; |
dan | b7dca7d | 2010-03-05 16:32:12 +0000 | [diff] [blame] | 1805 | flags1 = pIn1->flags; |
| 1806 | flags3 = pIn3->flags; |
drh | c3f1d5f | 2011-05-30 23:42:16 +0000 | [diff] [blame] | 1807 | if( (flags1 | flags3)&MEM_Null ){ |
drh | 6a2fe09 | 2009-09-23 02:29:36 +0000 | [diff] [blame] | 1808 | /* One or both operands are NULL */ |
| 1809 | if( pOp->p5 & SQLITE_NULLEQ ){ |
| 1810 | /* If SQLITE_NULLEQ is set (which will only happen if the operator is |
| 1811 | ** OP_Eq or OP_Ne) then take the jump or not depending on whether |
| 1812 | ** or not both operands are null. |
| 1813 | */ |
| 1814 | assert( pOp->opcode==OP_Eq || pOp->opcode==OP_Ne ); |
drh | c3f1d5f | 2011-05-30 23:42:16 +0000 | [diff] [blame] | 1815 | res = (flags1 & flags3 & MEM_Null)==0; |
drh | 6a2fe09 | 2009-09-23 02:29:36 +0000 | [diff] [blame] | 1816 | }else{ |
| 1817 | /* SQLITE_NULLEQ is clear and at least one operand is NULL, |
| 1818 | ** then the result is always NULL. |
| 1819 | ** The jump is taken if the SQLITE_JUMPIFNULL bit is set. |
| 1820 | */ |
| 1821 | if( pOp->p5 & SQLITE_STOREP2 ){ |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 1822 | pOut = &aMem[pOp->p2]; |
drh | 6a2fe09 | 2009-09-23 02:29:36 +0000 | [diff] [blame] | 1823 | MemSetTypeFlag(pOut, MEM_Null); |
| 1824 | REGISTER_TRACE(pOp->p2, pOut); |
| 1825 | }else if( pOp->p5 & SQLITE_JUMPIFNULL ){ |
| 1826 | pc = pOp->p2-1; |
| 1827 | } |
| 1828 | break; |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 1829 | } |
drh | 6a2fe09 | 2009-09-23 02:29:36 +0000 | [diff] [blame] | 1830 | }else{ |
| 1831 | /* Neither operand is NULL. Do a comparison. */ |
| 1832 | affinity = pOp->p5 & SQLITE_AFF_MASK; |
| 1833 | if( affinity ){ |
| 1834 | applyAffinity(pIn1, affinity, encoding); |
| 1835 | applyAffinity(pIn3, affinity, encoding); |
| 1836 | if( db->mallocFailed ) goto no_mem; |
| 1837 | } |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 1838 | |
drh | 6a2fe09 | 2009-09-23 02:29:36 +0000 | [diff] [blame] | 1839 | assert( pOp->p4type==P4_COLLSEQ || pOp->p4.pColl==0 ); |
| 1840 | ExpandBlob(pIn1); |
| 1841 | ExpandBlob(pIn3); |
| 1842 | res = sqlite3MemCompare(pIn3, pIn1, pOp->p4.pColl); |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 1843 | } |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 1844 | switch( pOp->opcode ){ |
| 1845 | case OP_Eq: res = res==0; break; |
| 1846 | case OP_Ne: res = res!=0; break; |
| 1847 | case OP_Lt: res = res<0; break; |
| 1848 | case OP_Le: res = res<=0; break; |
| 1849 | case OP_Gt: res = res>0; break; |
| 1850 | default: res = res>=0; break; |
| 1851 | } |
| 1852 | |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 1853 | if( pOp->p5 & SQLITE_STOREP2 ){ |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 1854 | pOut = &aMem[pOp->p2]; |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 1855 | memAboutToChange(p, pOut); |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1856 | MemSetTypeFlag(pOut, MEM_Int); |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 1857 | pOut->u.i = res; |
| 1858 | REGISTER_TRACE(pOp->p2, pOut); |
| 1859 | }else if( res ){ |
| 1860 | pc = pOp->p2-1; |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 1861 | } |
dan | b7dca7d | 2010-03-05 16:32:12 +0000 | [diff] [blame] | 1862 | |
| 1863 | /* Undo any changes made by applyAffinity() to the input registers. */ |
| 1864 | pIn1->flags = (pIn1->flags&~MEM_TypeMask) | (flags1&MEM_TypeMask); |
| 1865 | pIn3->flags = (pIn3->flags&~MEM_TypeMask) | (flags3&MEM_TypeMask); |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 1866 | break; |
| 1867 | } |
drh | c9b84a1 | 2002-06-20 11:36:48 +0000 | [diff] [blame] | 1868 | |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 1869 | /* Opcode: Permutation * * * P4 * |
| 1870 | ** |
shane | be21779 | 2009-03-05 04:20:31 +0000 | [diff] [blame] | 1871 | ** Set the permutation used by the OP_Compare operator to be the array |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 1872 | ** of integers in P4. |
| 1873 | ** |
| 1874 | ** The permutation is only valid until the next OP_Permutation, OP_Compare, |
| 1875 | ** OP_Halt, or OP_ResultRow. Typically the OP_Permutation should occur |
| 1876 | ** immediately prior to the OP_Compare. |
| 1877 | */ |
| 1878 | case OP_Permutation: { |
| 1879 | assert( pOp->p4type==P4_INTARRAY ); |
| 1880 | assert( pOp->p4.ai ); |
| 1881 | aPermute = pOp->p4.ai; |
| 1882 | break; |
| 1883 | } |
| 1884 | |
drh | 16ee60f | 2008-06-20 18:13:25 +0000 | [diff] [blame] | 1885 | /* Opcode: Compare P1 P2 P3 P4 * |
| 1886 | ** |
drh | 710c484 | 2010-08-30 01:17:20 +0000 | [diff] [blame] | 1887 | ** Compare two vectors of registers in reg(P1)..reg(P1+P3-1) (call this |
| 1888 | ** vector "A") and in reg(P2)..reg(P2+P3-1) ("B"). Save the result of |
drh | 16ee60f | 2008-06-20 18:13:25 +0000 | [diff] [blame] | 1889 | ** the comparison for use by the next OP_Jump instruct. |
| 1890 | ** |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 1891 | ** P4 is a KeyInfo structure that defines collating sequences and sort |
| 1892 | ** orders for the comparison. The permutation applies to registers |
| 1893 | ** only. The KeyInfo elements are used sequentially. |
| 1894 | ** |
| 1895 | ** The comparison is a sort comparison, so NULLs compare equal, |
| 1896 | ** NULLs are less than numbers, numbers are less than strings, |
drh | 16ee60f | 2008-06-20 18:13:25 +0000 | [diff] [blame] | 1897 | ** and strings are less than blobs. |
| 1898 | */ |
| 1899 | case OP_Compare: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1900 | int n; |
| 1901 | int i; |
| 1902 | int p1; |
| 1903 | int p2; |
| 1904 | const KeyInfo *pKeyInfo; |
| 1905 | int idx; |
| 1906 | CollSeq *pColl; /* Collating sequence to use on this term */ |
| 1907 | int bRev; /* True for DESCENDING sort order */ |
| 1908 | |
| 1909 | n = pOp->p3; |
| 1910 | pKeyInfo = pOp->p4.pKeyInfo; |
drh | 16ee60f | 2008-06-20 18:13:25 +0000 | [diff] [blame] | 1911 | assert( n>0 ); |
drh | 93a960a | 2008-07-10 00:32:42 +0000 | [diff] [blame] | 1912 | assert( pKeyInfo!=0 ); |
drh | 16ee60f | 2008-06-20 18:13:25 +0000 | [diff] [blame] | 1913 | p1 = pOp->p1; |
drh | 16ee60f | 2008-06-20 18:13:25 +0000 | [diff] [blame] | 1914 | p2 = pOp->p2; |
drh | 6a2fe09 | 2009-09-23 02:29:36 +0000 | [diff] [blame] | 1915 | #if SQLITE_DEBUG |
| 1916 | if( aPermute ){ |
| 1917 | int k, mx = 0; |
| 1918 | for(k=0; k<n; k++) if( aPermute[k]>mx ) mx = aPermute[k]; |
| 1919 | assert( p1>0 && p1+mx<=p->nMem+1 ); |
| 1920 | assert( p2>0 && p2+mx<=p->nMem+1 ); |
| 1921 | }else{ |
| 1922 | assert( p1>0 && p1+n<=p->nMem+1 ); |
| 1923 | assert( p2>0 && p2+n<=p->nMem+1 ); |
| 1924 | } |
| 1925 | #endif /* SQLITE_DEBUG */ |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 1926 | for(i=0; i<n; i++){ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1927 | idx = aPermute ? aPermute[i] : i; |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 1928 | assert( memIsValid(&aMem[p1+idx]) ); |
| 1929 | assert( memIsValid(&aMem[p2+idx]) ); |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 1930 | REGISTER_TRACE(p1+idx, &aMem[p1+idx]); |
| 1931 | REGISTER_TRACE(p2+idx, &aMem[p2+idx]); |
drh | 93a960a | 2008-07-10 00:32:42 +0000 | [diff] [blame] | 1932 | assert( i<pKeyInfo->nField ); |
| 1933 | pColl = pKeyInfo->aColl[i]; |
| 1934 | bRev = pKeyInfo->aSortOrder[i]; |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 1935 | iCompare = sqlite3MemCompare(&aMem[p1+idx], &aMem[p2+idx], pColl); |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 1936 | if( iCompare ){ |
| 1937 | if( bRev ) iCompare = -iCompare; |
| 1938 | break; |
| 1939 | } |
drh | 16ee60f | 2008-06-20 18:13:25 +0000 | [diff] [blame] | 1940 | } |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 1941 | aPermute = 0; |
drh | 16ee60f | 2008-06-20 18:13:25 +0000 | [diff] [blame] | 1942 | break; |
| 1943 | } |
| 1944 | |
| 1945 | /* Opcode: Jump P1 P2 P3 * * |
| 1946 | ** |
| 1947 | ** Jump to the instruction at address P1, P2, or P3 depending on whether |
| 1948 | ** in the most recent OP_Compare instruction the P1 vector was less than |
| 1949 | ** equal to, or greater than the P2 vector, respectively. |
| 1950 | */ |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 1951 | case OP_Jump: { /* jump */ |
| 1952 | if( iCompare<0 ){ |
drh | 16ee60f | 2008-06-20 18:13:25 +0000 | [diff] [blame] | 1953 | pc = pOp->p1 - 1; |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 1954 | }else if( iCompare==0 ){ |
drh | 16ee60f | 2008-06-20 18:13:25 +0000 | [diff] [blame] | 1955 | pc = pOp->p2 - 1; |
| 1956 | }else{ |
| 1957 | pc = pOp->p3 - 1; |
| 1958 | } |
| 1959 | break; |
| 1960 | } |
| 1961 | |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1962 | /* Opcode: And P1 P2 P3 * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1963 | ** |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1964 | ** Take the logical AND of the values in registers P1 and P2 and |
| 1965 | ** write the result into register P3. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1966 | ** |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1967 | ** If either P1 or P2 is 0 (false) then the result is 0 even if |
| 1968 | ** the other input is NULL. A NULL and true or two NULLs give |
| 1969 | ** a NULL output. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1970 | */ |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1971 | /* Opcode: Or P1 P2 P3 * * |
| 1972 | ** |
| 1973 | ** Take the logical OR of the values in register P1 and P2 and |
| 1974 | ** store the answer in register P3. |
| 1975 | ** |
| 1976 | ** If either P1 or P2 is nonzero (true) then the result is 1 (true) |
| 1977 | ** even if the other input is NULL. A NULL and false or two NULLs |
| 1978 | ** give a NULL output. |
| 1979 | */ |
| 1980 | case OP_And: /* same as TK_AND, in1, in2, out3 */ |
| 1981 | case OP_Or: { /* same as TK_OR, in1, in2, out3 */ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1982 | int v1; /* Left operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */ |
| 1983 | int v2; /* Right operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */ |
drh | bb11351 | 2002-05-27 01:04:51 +0000 | [diff] [blame] | 1984 | |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 1985 | pIn1 = &aMem[pOp->p1]; |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1986 | if( pIn1->flags & MEM_Null ){ |
drh | bb11351 | 2002-05-27 01:04:51 +0000 | [diff] [blame] | 1987 | v1 = 2; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1988 | }else{ |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1989 | v1 = sqlite3VdbeIntValue(pIn1)!=0; |
drh | bb11351 | 2002-05-27 01:04:51 +0000 | [diff] [blame] | 1990 | } |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 1991 | pIn2 = &aMem[pOp->p2]; |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1992 | if( pIn2->flags & MEM_Null ){ |
drh | bb11351 | 2002-05-27 01:04:51 +0000 | [diff] [blame] | 1993 | v2 = 2; |
| 1994 | }else{ |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1995 | v2 = sqlite3VdbeIntValue(pIn2)!=0; |
drh | bb11351 | 2002-05-27 01:04:51 +0000 | [diff] [blame] | 1996 | } |
| 1997 | if( pOp->opcode==OP_And ){ |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1998 | 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] | 1999 | v1 = and_logic[v1*3+v2]; |
| 2000 | }else{ |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 2001 | 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] | 2002 | v1 = or_logic[v1*3+v2]; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2003 | } |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 2004 | pOut = &aMem[pOp->p3]; |
drh | bb11351 | 2002-05-27 01:04:51 +0000 | [diff] [blame] | 2005 | if( v1==2 ){ |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 2006 | MemSetTypeFlag(pOut, MEM_Null); |
drh | bb11351 | 2002-05-27 01:04:51 +0000 | [diff] [blame] | 2007 | }else{ |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 2008 | pOut->u.i = v1; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 2009 | MemSetTypeFlag(pOut, MEM_Int); |
drh | bb11351 | 2002-05-27 01:04:51 +0000 | [diff] [blame] | 2010 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2011 | break; |
| 2012 | } |
| 2013 | |
drh | e99fa2a | 2008-12-15 15:27:51 +0000 | [diff] [blame] | 2014 | /* Opcode: Not P1 P2 * * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2015 | ** |
drh | e99fa2a | 2008-12-15 15:27:51 +0000 | [diff] [blame] | 2016 | ** Interpret the value in register P1 as a boolean value. Store the |
| 2017 | ** boolean complement in register P2. If the value in register P1 is |
| 2018 | ** NULL, then a NULL is stored in P2. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2019 | */ |
drh | 93952eb | 2009-11-13 19:43:43 +0000 | [diff] [blame] | 2020 | case OP_Not: { /* same as TK_NOT, in1, out2 */ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 2021 | pIn1 = &aMem[pOp->p1]; |
| 2022 | pOut = &aMem[pOp->p2]; |
drh | e99fa2a | 2008-12-15 15:27:51 +0000 | [diff] [blame] | 2023 | if( pIn1->flags & MEM_Null ){ |
| 2024 | sqlite3VdbeMemSetNull(pOut); |
| 2025 | }else{ |
| 2026 | sqlite3VdbeMemSetInt64(pOut, !sqlite3VdbeIntValue(pIn1)); |
| 2027 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2028 | break; |
| 2029 | } |
| 2030 | |
drh | e99fa2a | 2008-12-15 15:27:51 +0000 | [diff] [blame] | 2031 | /* Opcode: BitNot P1 P2 * * * |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 2032 | ** |
drh | e99fa2a | 2008-12-15 15:27:51 +0000 | [diff] [blame] | 2033 | ** Interpret the content of register P1 as an integer. Store the |
| 2034 | ** ones-complement of the P1 value into register P2. If P1 holds |
| 2035 | ** a NULL then store a NULL in P2. |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 2036 | */ |
drh | 93952eb | 2009-11-13 19:43:43 +0000 | [diff] [blame] | 2037 | case OP_BitNot: { /* same as TK_BITNOT, in1, out2 */ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 2038 | pIn1 = &aMem[pOp->p1]; |
| 2039 | pOut = &aMem[pOp->p2]; |
drh | e99fa2a | 2008-12-15 15:27:51 +0000 | [diff] [blame] | 2040 | if( pIn1->flags & MEM_Null ){ |
| 2041 | sqlite3VdbeMemSetNull(pOut); |
| 2042 | }else{ |
| 2043 | sqlite3VdbeMemSetInt64(pOut, ~sqlite3VdbeIntValue(pIn1)); |
| 2044 | } |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 2045 | break; |
| 2046 | } |
| 2047 | |
drh | 48f2d3b | 2011-09-16 01:34:43 +0000 | [diff] [blame] | 2048 | /* Opcode: Once P1 P2 * * * |
| 2049 | ** |
| 2050 | ** Jump to P2 if the value in register P1 is a not null or zero. If |
| 2051 | ** the value is NULL or zero, fall through and change the P1 register |
| 2052 | ** to an integer 1. |
| 2053 | ** |
| 2054 | ** When P1 is not used otherwise in a program, this opcode falls through |
| 2055 | ** once and jumps on all subsequent invocations. It is the equivalent |
| 2056 | ** of "OP_If P1 P2", followed by "OP_Integer 1 P1". |
| 2057 | */ |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 2058 | /* Opcode: If P1 P2 P3 * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2059 | ** |
drh | ef8662b | 2011-06-20 21:47:58 +0000 | [diff] [blame] | 2060 | ** Jump to P2 if the value in register P1 is true. The value |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 2061 | ** is considered true if it is numeric and non-zero. If the value |
| 2062 | ** in P1 is NULL then take the jump if P3 is true. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2063 | */ |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 2064 | /* Opcode: IfNot P1 P2 P3 * * |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 2065 | ** |
drh | ef8662b | 2011-06-20 21:47:58 +0000 | [diff] [blame] | 2066 | ** Jump to P2 if the value in register P1 is False. The value |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 2067 | ** is considered true if it has a numeric value of zero. If the value |
| 2068 | ** in P1 is NULL then take the jump if P3 is true. |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 2069 | */ |
drh | 48f2d3b | 2011-09-16 01:34:43 +0000 | [diff] [blame] | 2070 | case OP_Once: /* jump, in1 */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2071 | case OP_If: /* jump, in1 */ |
| 2072 | case OP_IfNot: { /* jump, in1 */ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2073 | int c; |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 2074 | pIn1 = &aMem[pOp->p1]; |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 2075 | if( pIn1->flags & MEM_Null ){ |
| 2076 | c = pOp->p3; |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 2077 | }else{ |
drh | ba0232a | 2005-06-06 17:27:19 +0000 | [diff] [blame] | 2078 | #ifdef SQLITE_OMIT_FLOATING_POINT |
shane | fbd60f8 | 2009-02-04 03:59:25 +0000 | [diff] [blame] | 2079 | c = sqlite3VdbeIntValue(pIn1)!=0; |
drh | ba0232a | 2005-06-06 17:27:19 +0000 | [diff] [blame] | 2080 | #else |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 2081 | c = sqlite3VdbeRealValue(pIn1)!=0.0; |
drh | ba0232a | 2005-06-06 17:27:19 +0000 | [diff] [blame] | 2082 | #endif |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 2083 | if( pOp->opcode==OP_IfNot ) c = !c; |
| 2084 | } |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 2085 | if( c ){ |
| 2086 | pc = pOp->p2-1; |
drh | 48f2d3b | 2011-09-16 01:34:43 +0000 | [diff] [blame] | 2087 | }else if( pOp->opcode==OP_Once ){ |
| 2088 | assert( (pIn1->flags & (MEM_Agg|MEM_Dyn|MEM_RowSet|MEM_Frame))==0 ); |
| 2089 | memAboutToChange(p, pIn1); |
| 2090 | pIn1->flags = MEM_Int; |
| 2091 | pIn1->u.i = 1; |
| 2092 | REGISTER_TRACE(pOp->p1, pIn1); |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 2093 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2094 | break; |
| 2095 | } |
| 2096 | |
drh | 830ecf9 | 2009-06-18 00:41:55 +0000 | [diff] [blame] | 2097 | /* Opcode: IsNull P1 P2 * * * |
drh | 477df4b | 2008-01-05 18:48:24 +0000 | [diff] [blame] | 2098 | ** |
drh | 830ecf9 | 2009-06-18 00:41:55 +0000 | [diff] [blame] | 2099 | ** Jump to P2 if the value in register P1 is NULL. |
drh | 477df4b | 2008-01-05 18:48:24 +0000 | [diff] [blame] | 2100 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2101 | case OP_IsNull: { /* same as TK_ISNULL, jump, in1 */ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 2102 | pIn1 = &aMem[pOp->p1]; |
drh | 830ecf9 | 2009-06-18 00:41:55 +0000 | [diff] [blame] | 2103 | if( (pIn1->flags & MEM_Null)!=0 ){ |
| 2104 | pc = pOp->p2 - 1; |
| 2105 | } |
drh | 477df4b | 2008-01-05 18:48:24 +0000 | [diff] [blame] | 2106 | break; |
| 2107 | } |
| 2108 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 2109 | /* Opcode: NotNull P1 P2 * * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2110 | ** |
drh | 6a288a3 | 2008-01-07 19:20:24 +0000 | [diff] [blame] | 2111 | ** Jump to P2 if the value in register P1 is not NULL. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2112 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2113 | case OP_NotNull: { /* same as TK_NOTNULL, jump, in1 */ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 2114 | pIn1 = &aMem[pOp->p1]; |
drh | 6a288a3 | 2008-01-07 19:20:24 +0000 | [diff] [blame] | 2115 | if( (pIn1->flags & MEM_Null)==0 ){ |
| 2116 | pc = pOp->p2 - 1; |
| 2117 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2118 | break; |
| 2119 | } |
| 2120 | |
drh | 3e9ca09 | 2009-09-08 01:14:48 +0000 | [diff] [blame] | 2121 | /* Opcode: Column P1 P2 P3 P4 P5 |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2122 | ** |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 2123 | ** Interpret the data that cursor P1 points to as a structure built using |
| 2124 | ** the MakeRecord instruction. (See the MakeRecord opcode for additional |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 2125 | ** information about the format of the data.) Extract the P2-th column |
| 2126 | ** from this record. If there are less that (P2+1) |
| 2127 | ** values in the record, extract a NULL. |
| 2128 | ** |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2129 | ** The value extracted is stored in register P3. |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2130 | ** |
danielk1977 | 1f4aa33 | 2008-01-03 09:51:55 +0000 | [diff] [blame] | 2131 | ** If the column contains fewer than P2 fields, then extract a NULL. Or, |
| 2132 | ** if the P4 argument is a P4_MEM use the value of the P4 argument as |
| 2133 | ** the result. |
drh | 3e9ca09 | 2009-09-08 01:14:48 +0000 | [diff] [blame] | 2134 | ** |
| 2135 | ** If the OPFLAG_CLEARCACHE bit is set on P5 and P1 is a pseudo-table cursor, |
| 2136 | ** then the cache of the cursor is reset prior to extracting the column. |
| 2137 | ** The first OP_Column against a pseudo-table after the value of the content |
| 2138 | ** register has changed should have this bit set. |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2139 | */ |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 2140 | case OP_Column: { |
drh | 35cd643 | 2009-06-05 14:17:21 +0000 | [diff] [blame] | 2141 | u32 payloadSize; /* Number of bytes in the record */ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2142 | i64 payloadSize64; /* Number of bytes in the record */ |
| 2143 | int p1; /* P1 value of the opcode */ |
| 2144 | int p2; /* column number to retrieve */ |
| 2145 | VdbeCursor *pC; /* The VDBE cursor */ |
drh | e61cffc | 2004-06-12 18:12:15 +0000 | [diff] [blame] | 2146 | char *zRec; /* Pointer to complete record-data */ |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 2147 | BtCursor *pCrsr; /* The BTree cursor */ |
| 2148 | u32 *aType; /* aType[i] holds the numeric type of the i-th column */ |
| 2149 | u32 *aOffset; /* aOffset[i] is offset to start of data for i-th column */ |
danielk1977 | 64202cf | 2008-11-17 15:31:47 +0000 | [diff] [blame] | 2150 | int nField; /* number of fields in the record */ |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 2151 | int len; /* The length of the serialized data for the column */ |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 2152 | int i; /* Loop counter */ |
| 2153 | char *zData; /* Part of the record being decoded */ |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 2154 | Mem *pDest; /* Where to write the extracted value */ |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 2155 | Mem sMem; /* For storing the record being decoded */ |
drh | 35cd643 | 2009-06-05 14:17:21 +0000 | [diff] [blame] | 2156 | u8 *zIdx; /* Index into header */ |
| 2157 | u8 *zEndHdr; /* Pointer to first byte after the header */ |
| 2158 | u32 offset; /* Offset into the data */ |
drh | 6658cd9 | 2010-02-05 14:12:53 +0000 | [diff] [blame] | 2159 | u32 szField; /* Number of bytes in the content of a field */ |
drh | 35cd643 | 2009-06-05 14:17:21 +0000 | [diff] [blame] | 2160 | int szHdr; /* Size of the header size field at start of record */ |
| 2161 | int avail; /* Number of bytes of available data */ |
drh | 5a077b7 | 2011-08-29 02:16:18 +0000 | [diff] [blame] | 2162 | u32 t; /* A type code from the record header */ |
drh | 3e9ca09 | 2009-09-08 01:14:48 +0000 | [diff] [blame] | 2163 | Mem *pReg; /* PseudoTable input register */ |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2164 | |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2165 | |
| 2166 | p1 = pOp->p1; |
| 2167 | p2 = pOp->p2; |
| 2168 | pC = 0; |
drh | b27b7f5 | 2008-12-10 18:03:45 +0000 | [diff] [blame] | 2169 | memset(&sMem, 0, sizeof(sMem)); |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 2170 | assert( p1<p->nCursor ); |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2171 | assert( pOp->p3>0 && pOp->p3<=p->nMem ); |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 2172 | pDest = &aMem[pOp->p3]; |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 2173 | memAboutToChange(p, pDest); |
shane | 36840fd | 2009-06-26 16:32:13 +0000 | [diff] [blame] | 2174 | zRec = 0; |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 2175 | |
drh | e61cffc | 2004-06-12 18:12:15 +0000 | [diff] [blame] | 2176 | /* This block sets the variable payloadSize to be the total number of |
| 2177 | ** bytes in the record. |
| 2178 | ** |
| 2179 | ** 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] | 2180 | ** The complete record text is always available for pseudo-tables |
| 2181 | ** If the record is stored in a cursor, the complete record text |
| 2182 | ** might be available in the pC->aRow cache. Or it might not be. |
| 2183 | ** If the data is unavailable, zRec is set to NULL. |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 2184 | ** |
| 2185 | ** We also compute the number of columns in the record. For cursors, |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 2186 | ** the number of columns is stored in the VdbeCursor.nField element. |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 2187 | */ |
drh | b73857f | 2006-03-17 00:25:59 +0000 | [diff] [blame] | 2188 | pC = p->apCsr[p1]; |
danielk1977 | 6c92409 | 2007-11-12 08:09:34 +0000 | [diff] [blame] | 2189 | assert( pC!=0 ); |
danielk1977 | 0817d0d | 2007-02-14 09:19:36 +0000 | [diff] [blame] | 2190 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 2191 | assert( pC->pVtabCursor==0 ); |
| 2192 | #endif |
shane | 36840fd | 2009-06-26 16:32:13 +0000 | [diff] [blame] | 2193 | pCrsr = pC->pCursor; |
| 2194 | if( pCrsr!=0 ){ |
drh | e61cffc | 2004-06-12 18:12:15 +0000 | [diff] [blame] | 2195 | /* The record is stored in a B-Tree */ |
drh | 536065a | 2005-01-26 21:55:31 +0000 | [diff] [blame] | 2196 | rc = sqlite3VdbeCursorMoveto(pC); |
drh | 52f159e | 2005-01-27 00:33:21 +0000 | [diff] [blame] | 2197 | if( rc ) goto abort_due_to_error; |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2198 | if( pC->nullRow ){ |
| 2199 | payloadSize = 0; |
drh | 76873ab | 2006-01-07 18:48:26 +0000 | [diff] [blame] | 2200 | }else if( pC->cacheStatus==p->cacheCtr ){ |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2201 | payloadSize = pC->payloadSize; |
drh | 2646da7 | 2005-12-09 20:02:05 +0000 | [diff] [blame] | 2202 | zRec = (char*)pC->aRow; |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 2203 | }else if( pC->isIndex ){ |
drh | ea8ffdf | 2009-07-22 00:35:23 +0000 | [diff] [blame] | 2204 | assert( sqlite3BtreeCursorIsValid(pCrsr) ); |
drh | c27ae61 | 2009-07-14 18:35:44 +0000 | [diff] [blame] | 2205 | rc = sqlite3BtreeKeySize(pCrsr, &payloadSize64); |
| 2206 | assert( rc==SQLITE_OK ); /* True because of CursorMoveto() call above */ |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 2207 | /* sqlite3BtreeParseCellPtr() uses getVarint32() to extract the |
| 2208 | ** payload size, so it is impossible for payloadSize64 to be |
| 2209 | ** larger than 32 bits. */ |
| 2210 | assert( (payloadSize64 & SQLITE_MAX_U32)==(u64)payloadSize64 ); |
drh | 35cd643 | 2009-06-05 14:17:21 +0000 | [diff] [blame] | 2211 | payloadSize = (u32)payloadSize64; |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2212 | }else{ |
drh | ea8ffdf | 2009-07-22 00:35:23 +0000 | [diff] [blame] | 2213 | assert( sqlite3BtreeCursorIsValid(pCrsr) ); |
drh | c27ae61 | 2009-07-14 18:35:44 +0000 | [diff] [blame] | 2214 | rc = sqlite3BtreeDataSize(pCrsr, &payloadSize); |
drh | ea8ffdf | 2009-07-22 00:35:23 +0000 | [diff] [blame] | 2215 | assert( rc==SQLITE_OK ); /* DataSize() cannot fail */ |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2216 | } |
drh | 4a6f3aa | 2011-08-28 00:19:26 +0000 | [diff] [blame] | 2217 | }else if( ALWAYS(pC->pseudoTableReg>0) ){ |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 2218 | pReg = &aMem[pC->pseudoTableReg]; |
drh | 3e9ca09 | 2009-09-08 01:14:48 +0000 | [diff] [blame] | 2219 | assert( pReg->flags & MEM_Blob ); |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 2220 | assert( memIsValid(pReg) ); |
drh | 3e9ca09 | 2009-09-08 01:14:48 +0000 | [diff] [blame] | 2221 | payloadSize = pReg->n; |
| 2222 | zRec = pReg->z; |
| 2223 | pC->cacheStatus = (pOp->p5&OPFLAG_CLEARCACHE) ? CACHE_STALE : p->cacheCtr; |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2224 | assert( payloadSize==0 || zRec!=0 ); |
drh | 9a65f2c | 2009-06-22 19:05:40 +0000 | [diff] [blame] | 2225 | }else{ |
| 2226 | /* Consider the row to be NULL */ |
| 2227 | payloadSize = 0; |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2228 | } |
| 2229 | |
drh | e6f43fc | 2011-08-28 02:15:34 +0000 | [diff] [blame] | 2230 | /* If payloadSize is 0, then just store a NULL. This can happen because of |
| 2231 | ** nullRow or because of a corrupt database. */ |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2232 | if( payloadSize==0 ){ |
drh | e6f43fc | 2011-08-28 02:15:34 +0000 | [diff] [blame] | 2233 | MemSetTypeFlag(pDest, MEM_Null); |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 2234 | goto op_column_out; |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2235 | } |
drh | 35cd643 | 2009-06-05 14:17:21 +0000 | [diff] [blame] | 2236 | assert( db->aLimit[SQLITE_LIMIT_LENGTH]>=0 ); |
| 2237 | if( payloadSize > (u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){ |
drh | 023ae03 | 2007-05-08 12:12:16 +0000 | [diff] [blame] | 2238 | goto too_big; |
| 2239 | } |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2240 | |
shane | 36840fd | 2009-06-26 16:32:13 +0000 | [diff] [blame] | 2241 | nField = pC->nField; |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 2242 | assert( p2<nField ); |
danielk1977 | b4964b7 | 2004-05-18 01:23:38 +0000 | [diff] [blame] | 2243 | |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2244 | /* Read and parse the table header. Store the results of the parse |
| 2245 | ** into the record header cache fields of the cursor. |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2246 | */ |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 2247 | aType = pC->aType; |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 2248 | if( pC->cacheStatus==p->cacheCtr ){ |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 2249 | aOffset = pC->aOffset; |
| 2250 | }else{ |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 2251 | assert(aType); |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2252 | avail = 0; |
drh | b73857f | 2006-03-17 00:25:59 +0000 | [diff] [blame] | 2253 | pC->aOffset = aOffset = &aType[nField]; |
| 2254 | pC->payloadSize = payloadSize; |
| 2255 | pC->cacheStatus = p->cacheCtr; |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2256 | |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 2257 | /* Figure out how many bytes are in the header */ |
danielk1977 | 84ac9d0 | 2004-05-18 09:58:06 +0000 | [diff] [blame] | 2258 | if( zRec ){ |
| 2259 | zData = zRec; |
| 2260 | }else{ |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 2261 | if( pC->isIndex ){ |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 2262 | zData = (char*)sqlite3BtreeKeyFetch(pCrsr, &avail); |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 2263 | }else{ |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 2264 | zData = (char*)sqlite3BtreeDataFetch(pCrsr, &avail); |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2265 | } |
drh | e61cffc | 2004-06-12 18:12:15 +0000 | [diff] [blame] | 2266 | /* If KeyFetch()/DataFetch() managed to get the entire payload, |
| 2267 | ** save the payload in the pC->aRow cache. That will save us from |
| 2268 | ** having to make additional calls to fetch the content portion of |
| 2269 | ** the record. |
| 2270 | */ |
drh | 35cd643 | 2009-06-05 14:17:21 +0000 | [diff] [blame] | 2271 | assert( avail>=0 ); |
| 2272 | if( payloadSize <= (u32)avail ){ |
drh | 2646da7 | 2005-12-09 20:02:05 +0000 | [diff] [blame] | 2273 | zRec = zData; |
| 2274 | pC->aRow = (u8*)zData; |
drh | e61cffc | 2004-06-12 18:12:15 +0000 | [diff] [blame] | 2275 | }else{ |
| 2276 | pC->aRow = 0; |
| 2277 | } |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 2278 | } |
drh | 588f5bc | 2007-01-02 18:41:54 +0000 | [diff] [blame] | 2279 | /* The following assert is true in all cases accept when |
| 2280 | ** the database file has been corrupted externally. |
| 2281 | ** assert( zRec!=0 || avail>=payloadSize || avail>=9 ); */ |
drh | 35cd643 | 2009-06-05 14:17:21 +0000 | [diff] [blame] | 2282 | szHdr = getVarint32((u8*)zData, offset); |
| 2283 | |
| 2284 | /* Make sure a corrupt database has not given us an oversize header. |
| 2285 | ** Do this now to avoid an oversize memory allocation. |
| 2286 | ** |
| 2287 | ** Type entries can be between 1 and 5 bytes each. But 4 and 5 byte |
| 2288 | ** types use so much data space that there can only be 4096 and 32 of |
| 2289 | ** them, respectively. So the maximum header length results from a |
| 2290 | ** 3-byte type for each of the maximum of 32768 columns plus three |
| 2291 | ** extra bytes for the header length itself. 32768*3 + 3 = 98307. |
| 2292 | */ |
| 2293 | if( offset > 98307 ){ |
| 2294 | rc = SQLITE_CORRUPT_BKPT; |
| 2295 | goto op_column_out; |
| 2296 | } |
| 2297 | |
| 2298 | /* Compute in len the number of bytes of data we need to read in order |
| 2299 | ** to get nField type values. offset is an upper bound on this. But |
| 2300 | ** nField might be significantly less than the true number of columns |
| 2301 | ** in the table, and in that case, 5*nField+3 might be smaller than offset. |
| 2302 | ** We want to minimize len in order to limit the size of the memory |
| 2303 | ** allocation, especially if a corrupt database file has caused offset |
| 2304 | ** to be oversized. Offset is limited to 98307 above. But 98307 might |
| 2305 | ** still exceed Robson memory allocation limits on some configurations. |
| 2306 | ** On systems that cannot tolerate large memory allocations, nField*5+3 |
| 2307 | ** will likely be much smaller since nField will likely be less than |
| 2308 | ** 20 or so. This insures that Robson memory allocation limits are |
| 2309 | ** not exceeded even for corrupt database files. |
| 2310 | */ |
| 2311 | len = nField*5 + 3; |
shane | 75ac1de | 2009-06-09 18:58:52 +0000 | [diff] [blame] | 2312 | if( len > (int)offset ) len = (int)offset; |
drh | e61cffc | 2004-06-12 18:12:15 +0000 | [diff] [blame] | 2313 | |
| 2314 | /* The KeyFetch() or DataFetch() above are fast and will get the entire |
| 2315 | ** record header in most cases. But they will fail to get the complete |
| 2316 | ** record header if the record header does not fit on a single page |
| 2317 | ** in the B-Tree. When that happens, use sqlite3VdbeMemFromBtree() to |
| 2318 | ** acquire the complete header text. |
| 2319 | */ |
drh | 35cd643 | 2009-06-05 14:17:21 +0000 | [diff] [blame] | 2320 | if( !zRec && avail<len ){ |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 2321 | sMem.flags = 0; |
| 2322 | sMem.db = 0; |
drh | 35cd643 | 2009-06-05 14:17:21 +0000 | [diff] [blame] | 2323 | rc = sqlite3VdbeMemFromBtree(pCrsr, 0, len, pC->isIndex, &sMem); |
danielk1977 | 84ac9d0 | 2004-05-18 09:58:06 +0000 | [diff] [blame] | 2324 | if( rc!=SQLITE_OK ){ |
danielk1977 | 3c9cc8d | 2005-01-17 03:40:08 +0000 | [diff] [blame] | 2325 | goto op_column_out; |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2326 | } |
drh | b6f5452 | 2004-05-20 02:42:16 +0000 | [diff] [blame] | 2327 | zData = sMem.z; |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2328 | } |
drh | 35cd643 | 2009-06-05 14:17:21 +0000 | [diff] [blame] | 2329 | zEndHdr = (u8 *)&zData[len]; |
| 2330 | zIdx = (u8 *)&zData[szHdr]; |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2331 | |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 2332 | /* Scan the header and use it to fill in the aType[] and aOffset[] |
| 2333 | ** arrays. aType[i] will contain the type integer for the i-th |
| 2334 | ** column and aOffset[i] will contain the offset from the beginning |
| 2335 | ** 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] | 2336 | */ |
danielk1977 | dedf45b | 2006-01-13 17:12:01 +0000 | [diff] [blame] | 2337 | for(i=0; i<nField; i++){ |
| 2338 | if( zIdx<zEndHdr ){ |
drh | 6658cd9 | 2010-02-05 14:12:53 +0000 | [diff] [blame] | 2339 | aOffset[i] = offset; |
drh | 5a077b7 | 2011-08-29 02:16:18 +0000 | [diff] [blame] | 2340 | if( zIdx[0]<0x80 ){ |
| 2341 | t = zIdx[0]; |
| 2342 | zIdx++; |
| 2343 | }else{ |
| 2344 | zIdx += sqlite3GetVarint32(zIdx, &t); |
| 2345 | } |
| 2346 | aType[i] = t; |
| 2347 | szField = sqlite3VdbeSerialTypeLen(t); |
drh | 6658cd9 | 2010-02-05 14:12:53 +0000 | [diff] [blame] | 2348 | offset += szField; |
| 2349 | if( offset<szField ){ /* True if offset overflows */ |
| 2350 | zIdx = &zEndHdr[1]; /* Forces SQLITE_CORRUPT return below */ |
| 2351 | break; |
| 2352 | } |
danielk1977 | dedf45b | 2006-01-13 17:12:01 +0000 | [diff] [blame] | 2353 | }else{ |
| 2354 | /* If i is less that nField, then there are less fields in this |
| 2355 | ** record than SetNumColumns indicated there are columns in the |
| 2356 | ** table. Set the offset for any extra columns not present in |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2357 | ** the record to 0. This tells code below to store a NULL |
| 2358 | ** instead of deserializing a value from the record. |
danielk1977 | dedf45b | 2006-01-13 17:12:01 +0000 | [diff] [blame] | 2359 | */ |
| 2360 | aOffset[i] = 0; |
| 2361 | } |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2362 | } |
danielk1977 | 5f09613 | 2008-03-28 15:44:09 +0000 | [diff] [blame] | 2363 | sqlite3VdbeMemRelease(&sMem); |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 2364 | sMem.flags = MEM_Null; |
| 2365 | |
danielk1977 | 9792eef | 2006-01-13 15:58:43 +0000 | [diff] [blame] | 2366 | /* If we have read more header data than was contained in the header, |
| 2367 | ** 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] | 2368 | ** record, or if the end of the last field appears to be before the end |
| 2369 | ** of the record (when all fields present), then we must be dealing |
| 2370 | ** with a corrupt database. |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 2371 | */ |
drh | 6658cd9 | 2010-02-05 14:12:53 +0000 | [diff] [blame] | 2372 | if( (zIdx > zEndHdr) || (offset > payloadSize) |
| 2373 | || (zIdx==zEndHdr && offset!=payloadSize) ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 2374 | rc = SQLITE_CORRUPT_BKPT; |
danielk1977 | 3c9cc8d | 2005-01-17 03:40:08 +0000 | [diff] [blame] | 2375 | goto op_column_out; |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 2376 | } |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 2377 | } |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2378 | |
danielk1977 | 36963fd | 2005-02-19 08:18:05 +0000 | [diff] [blame] | 2379 | /* Get the column information. If aOffset[p2] is non-zero, then |
| 2380 | ** deserialize the value from the record. If aOffset[p2] is zero, |
| 2381 | ** then there are not enough fields in the record to satisfy the |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 2382 | ** 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] | 2383 | ** a pointer to a Mem object. |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2384 | */ |
danielk1977 | 36963fd | 2005-02-19 08:18:05 +0000 | [diff] [blame] | 2385 | if( aOffset[p2] ){ |
| 2386 | assert( rc==SQLITE_OK ); |
| 2387 | if( zRec ){ |
drh | 2d36eb4 | 2011-08-29 02:49:41 +0000 | [diff] [blame] | 2388 | MemReleaseExt(pDest); |
danielk1977 | 808ec7c | 2008-07-29 10:18:57 +0000 | [diff] [blame] | 2389 | sqlite3VdbeSerialGet((u8 *)&zRec[aOffset[p2]], aType[p2], pDest); |
danielk1977 | 36963fd | 2005-02-19 08:18:05 +0000 | [diff] [blame] | 2390 | }else{ |
| 2391 | len = sqlite3VdbeSerialTypeLen(aType[p2]); |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 2392 | sqlite3VdbeMemMove(&sMem, pDest); |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 2393 | rc = sqlite3VdbeMemFromBtree(pCrsr, aOffset[p2], len, pC->isIndex, &sMem); |
danielk1977 | 36963fd | 2005-02-19 08:18:05 +0000 | [diff] [blame] | 2394 | if( rc!=SQLITE_OK ){ |
| 2395 | goto op_column_out; |
| 2396 | } |
| 2397 | zData = sMem.z; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 2398 | sqlite3VdbeSerialGet((u8*)zData, aType[p2], pDest); |
danielk1977 | 7701e81 | 2005-01-10 12:59:51 +0000 | [diff] [blame] | 2399 | } |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 2400 | pDest->enc = encoding; |
danielk1977 | 36963fd | 2005-02-19 08:18:05 +0000 | [diff] [blame] | 2401 | }else{ |
danielk1977 | 60585dd | 2008-01-03 08:08:40 +0000 | [diff] [blame] | 2402 | if( pOp->p4type==P4_MEM ){ |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 2403 | sqlite3VdbeMemShallowCopy(pDest, pOp->p4.pMem, MEM_Static); |
danielk1977 | aee18ef | 2005-03-09 12:26:50 +0000 | [diff] [blame] | 2404 | }else{ |
drh | e6f43fc | 2011-08-28 02:15:34 +0000 | [diff] [blame] | 2405 | MemSetTypeFlag(pDest, MEM_Null); |
danielk1977 | aee18ef | 2005-03-09 12:26:50 +0000 | [diff] [blame] | 2406 | } |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 2407 | } |
drh | febe106 | 2004-08-28 18:17:48 +0000 | [diff] [blame] | 2408 | |
| 2409 | /* If we dynamically allocated space to hold the data (in the |
| 2410 | ** sqlite3VdbeMemFromBtree() call above) then transfer control of that |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 2411 | ** dynamically allocated space over to the pDest structure. |
drh | febe106 | 2004-08-28 18:17:48 +0000 | [diff] [blame] | 2412 | ** This prevents a memory copy. |
| 2413 | */ |
danielk1977 | 5f09613 | 2008-03-28 15:44:09 +0000 | [diff] [blame] | 2414 | if( sMem.zMalloc ){ |
| 2415 | assert( sMem.z==sMem.zMalloc ); |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 2416 | assert( !(pDest->flags & MEM_Dyn) ); |
| 2417 | assert( !(pDest->flags & (MEM_Blob|MEM_Str)) || pDest->z==sMem.z ); |
| 2418 | pDest->flags &= ~(MEM_Ephem|MEM_Static); |
danielk1977 | 5f09613 | 2008-03-28 15:44:09 +0000 | [diff] [blame] | 2419 | pDest->flags |= MEM_Term; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 2420 | pDest->z = sMem.z; |
danielk1977 | 5f09613 | 2008-03-28 15:44:09 +0000 | [diff] [blame] | 2421 | pDest->zMalloc = sMem.zMalloc; |
danielk1977 | b1bc953 | 2004-05-22 03:05:33 +0000 | [diff] [blame] | 2422 | } |
drh | febe106 | 2004-08-28 18:17:48 +0000 | [diff] [blame] | 2423 | |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 2424 | rc = sqlite3VdbeMemMakeWriteable(pDest); |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 2425 | |
danielk1977 | 3c9cc8d | 2005-01-17 03:40:08 +0000 | [diff] [blame] | 2426 | op_column_out: |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 2427 | UPDATE_MAX_BLOBSIZE(pDest); |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 2428 | REGISTER_TRACE(pOp->p3, pDest); |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2429 | break; |
| 2430 | } |
| 2431 | |
danielk1977 | 751de56 | 2008-04-18 09:01:15 +0000 | [diff] [blame] | 2432 | /* Opcode: Affinity P1 P2 * P4 * |
| 2433 | ** |
| 2434 | ** Apply affinities to a range of P2 registers starting with P1. |
| 2435 | ** |
| 2436 | ** P4 is a string that is P2 characters long. The nth character of the |
| 2437 | ** string indicates the column affinity that should be used for the nth |
| 2438 | ** memory cell in the range. |
| 2439 | */ |
| 2440 | case OP_Affinity: { |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 2441 | const char *zAffinity; /* The affinity to be applied */ |
| 2442 | char cAff; /* A single character of affinity */ |
danielk1977 | 751de56 | 2008-04-18 09:01:15 +0000 | [diff] [blame] | 2443 | |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2444 | zAffinity = pOp->p4.z; |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 2445 | assert( zAffinity!=0 ); |
| 2446 | assert( zAffinity[pOp->p2]==0 ); |
| 2447 | pIn1 = &aMem[pOp->p1]; |
| 2448 | while( (cAff = *(zAffinity++))!=0 ){ |
| 2449 | assert( pIn1 <= &p->aMem[p->nMem] ); |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 2450 | assert( memIsValid(pIn1) ); |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 2451 | ExpandBlob(pIn1); |
| 2452 | applyAffinity(pIn1, cAff, encoding); |
| 2453 | pIn1++; |
danielk1977 | 751de56 | 2008-04-18 09:01:15 +0000 | [diff] [blame] | 2454 | } |
| 2455 | break; |
| 2456 | } |
| 2457 | |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2458 | /* Opcode: MakeRecord P1 P2 P3 P4 * |
drh | 7a224de | 2004-06-02 01:22:02 +0000 | [diff] [blame] | 2459 | ** |
drh | 710c484 | 2010-08-30 01:17:20 +0000 | [diff] [blame] | 2460 | ** Convert P2 registers beginning with P1 into the [record format] |
| 2461 | ** use as a data record in a database table or as a key |
| 2462 | ** in an index. The OP_Column opcode can decode the record later. |
drh | 7a224de | 2004-06-02 01:22:02 +0000 | [diff] [blame] | 2463 | ** |
danielk1977 | 751de56 | 2008-04-18 09:01:15 +0000 | [diff] [blame] | 2464 | ** 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] | 2465 | ** string indicates the column affinity that should be used for the nth |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2466 | ** field of the index key. |
drh | 7a224de | 2004-06-02 01:22:02 +0000 | [diff] [blame] | 2467 | ** |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 2468 | ** The mapping from character to affinity is given by the SQLITE_AFF_ |
| 2469 | ** macros defined in sqliteInt.h. |
drh | 7a224de | 2004-06-02 01:22:02 +0000 | [diff] [blame] | 2470 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 2471 | ** If P4 is NULL then all index fields have the affinity NONE. |
drh | 7f057c9 | 2005-06-24 03:53:06 +0000 | [diff] [blame] | 2472 | */ |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2473 | case OP_MakeRecord: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2474 | u8 *zNewRecord; /* A buffer to hold the data for the new record */ |
| 2475 | Mem *pRec; /* The new record */ |
| 2476 | u64 nData; /* Number of bytes of data space */ |
| 2477 | int nHdr; /* Number of bytes of header space */ |
| 2478 | i64 nByte; /* Data space required for this record */ |
| 2479 | int nZero; /* Number of zero bytes at the end of the record */ |
| 2480 | int nVarint; /* Number of bytes in a varint */ |
| 2481 | u32 serial_type; /* Type field */ |
| 2482 | Mem *pData0; /* First field to be combined into the record */ |
| 2483 | Mem *pLast; /* Last field of the record */ |
| 2484 | int nField; /* Number of fields in the record */ |
| 2485 | char *zAffinity; /* The affinity string for the record */ |
| 2486 | int file_format; /* File format to use for encoding */ |
| 2487 | int i; /* Space used in zNewRecord[] */ |
| 2488 | int len; /* Length of a field */ |
| 2489 | |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 2490 | /* Assuming the record contains N fields, the record format looks |
| 2491 | ** like this: |
| 2492 | ** |
drh | 7a224de | 2004-06-02 01:22:02 +0000 | [diff] [blame] | 2493 | ** ------------------------------------------------------------------------ |
| 2494 | ** | hdr-size | type 0 | type 1 | ... | type N-1 | data0 | ... | data N-1 | |
| 2495 | ** ------------------------------------------------------------------------ |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 2496 | ** |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2497 | ** Data(0) is taken from register P1. Data(1) comes from register P1+1 |
| 2498 | ** and so froth. |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 2499 | ** |
| 2500 | ** Each type field is a varint representing the serial type of the |
| 2501 | ** corresponding data element (see sqlite3VdbeSerialType()). The |
drh | 7a224de | 2004-06-02 01:22:02 +0000 | [diff] [blame] | 2502 | ** hdr-size field is also a varint which is the offset from the beginning |
| 2503 | ** of the record to data0. |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 2504 | */ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2505 | nData = 0; /* Number of bytes of data space */ |
| 2506 | nHdr = 0; /* Number of bytes of header space */ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2507 | nZero = 0; /* Number of zero bytes at the end of the record */ |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2508 | nField = pOp->p1; |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 2509 | zAffinity = pOp->p4.z; |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 2510 | assert( nField>0 && pOp->p2>0 && pOp->p2+nField<=p->nMem+1 ); |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 2511 | pData0 = &aMem[nField]; |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2512 | nField = pOp->p2; |
| 2513 | pLast = &pData0[nField-1]; |
drh | d946db0 | 2005-12-29 19:23:06 +0000 | [diff] [blame] | 2514 | file_format = p->minWriteFileFormat; |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 2515 | |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 2516 | /* Identify the output register */ |
| 2517 | assert( pOp->p3<pOp->p1 || pOp->p3>=pOp->p1+pOp->p2 ); |
| 2518 | pOut = &aMem[pOp->p3]; |
| 2519 | memAboutToChange(p, pOut); |
| 2520 | |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 2521 | /* Loop through the elements that will make up the record to figure |
| 2522 | ** out how much space is required for the new record. |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 2523 | */ |
drh | a2a49dc | 2008-01-02 14:28:13 +0000 | [diff] [blame] | 2524 | for(pRec=pData0; pRec<=pLast; pRec++){ |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 2525 | assert( memIsValid(pRec) ); |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 2526 | if( zAffinity ){ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 2527 | applyAffinity(pRec, zAffinity[pRec-pData0], encoding); |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 2528 | } |
danielk1977 | d908f5a | 2007-05-11 07:08:28 +0000 | [diff] [blame] | 2529 | if( pRec->flags&MEM_Zero && pRec->n>0 ){ |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 2530 | sqlite3VdbeMemExpandBlob(pRec); |
danielk1977 | d908f5a | 2007-05-11 07:08:28 +0000 | [diff] [blame] | 2531 | } |
drh | d946db0 | 2005-12-29 19:23:06 +0000 | [diff] [blame] | 2532 | serial_type = sqlite3VdbeSerialType(pRec, file_format); |
drh | ae7e151 | 2007-05-02 16:51:59 +0000 | [diff] [blame] | 2533 | len = sqlite3VdbeSerialTypeLen(serial_type); |
| 2534 | nData += len; |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 2535 | nHdr += sqlite3VarintLen(serial_type); |
drh | fdf972a | 2007-05-02 13:30:27 +0000 | [diff] [blame] | 2536 | if( pRec->flags & MEM_Zero ){ |
| 2537 | /* Only pure zero-filled BLOBs can be input to this Opcode. |
| 2538 | ** We do not allow blobs with a prefix and a zero-filled tail. */ |
drh | 8df3284 | 2008-12-09 02:51:23 +0000 | [diff] [blame] | 2539 | nZero += pRec->u.nZero; |
drh | ae7e151 | 2007-05-02 16:51:59 +0000 | [diff] [blame] | 2540 | }else if( len ){ |
drh | fdf972a | 2007-05-02 13:30:27 +0000 | [diff] [blame] | 2541 | nZero = 0; |
| 2542 | } |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 2543 | } |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 2544 | |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 2545 | /* Add the initial header varint and total the size */ |
drh | cb9882a | 2005-03-17 03:15:40 +0000 | [diff] [blame] | 2546 | nHdr += nVarint = sqlite3VarintLen(nHdr); |
| 2547 | if( nVarint<sqlite3VarintLen(nHdr) ){ |
| 2548 | nHdr++; |
| 2549 | } |
drh | fdf972a | 2007-05-02 13:30:27 +0000 | [diff] [blame] | 2550 | nByte = nHdr+nData-nZero; |
drh | bb4957f | 2008-03-20 14:03:29 +0000 | [diff] [blame] | 2551 | if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){ |
drh | 023ae03 | 2007-05-08 12:12:16 +0000 | [diff] [blame] | 2552 | goto too_big; |
| 2553 | } |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 2554 | |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 2555 | /* Make sure the output register has a buffer large enough to store |
| 2556 | ** the new record. The output register (pOp->p3) is not allowed to |
| 2557 | ** be one of the input registers (because the following call to |
| 2558 | ** sqlite3VdbeMemGrow() could clobber the value before it is used). |
| 2559 | */ |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 2560 | if( sqlite3VdbeMemGrow(pOut, (int)nByte, 0) ){ |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 2561 | goto no_mem; |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 2562 | } |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 2563 | zNewRecord = (u8 *)pOut->z; |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 2564 | |
| 2565 | /* Write the record */ |
shane | 3f8d5cf | 2008-04-24 19:15:09 +0000 | [diff] [blame] | 2566 | i = putVarint32(zNewRecord, nHdr); |
drh | a2a49dc | 2008-01-02 14:28:13 +0000 | [diff] [blame] | 2567 | for(pRec=pData0; pRec<=pLast; pRec++){ |
drh | d946db0 | 2005-12-29 19:23:06 +0000 | [diff] [blame] | 2568 | serial_type = sqlite3VdbeSerialType(pRec, file_format); |
shane | 3f8d5cf | 2008-04-24 19:15:09 +0000 | [diff] [blame] | 2569 | i += putVarint32(&zNewRecord[i], serial_type); /* serial type */ |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 2570 | } |
drh | a2a49dc | 2008-01-02 14:28:13 +0000 | [diff] [blame] | 2571 | for(pRec=pData0; pRec<=pLast; pRec++){ /* serial data */ |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 2572 | i += sqlite3VdbeSerialPut(&zNewRecord[i], (int)(nByte-i), pRec,file_format); |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 2573 | } |
drh | fdf972a | 2007-05-02 13:30:27 +0000 | [diff] [blame] | 2574 | assert( i==nByte ); |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 2575 | |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2576 | assert( pOp->p3>0 && pOp->p3<=p->nMem ); |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 2577 | pOut->n = (int)nByte; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 2578 | pOut->flags = MEM_Blob | MEM_Dyn; |
| 2579 | pOut->xDel = 0; |
drh | fdf972a | 2007-05-02 13:30:27 +0000 | [diff] [blame] | 2580 | if( nZero ){ |
drh | 8df3284 | 2008-12-09 02:51:23 +0000 | [diff] [blame] | 2581 | pOut->u.nZero = nZero; |
drh | 477df4b | 2008-01-05 18:48:24 +0000 | [diff] [blame] | 2582 | pOut->flags |= MEM_Zero; |
drh | fdf972a | 2007-05-02 13:30:27 +0000 | [diff] [blame] | 2583 | } |
drh | 477df4b | 2008-01-05 18:48:24 +0000 | [diff] [blame] | 2584 | pOut->enc = SQLITE_UTF8; /* In case the blob is ever converted to text */ |
drh | 1013c93 | 2008-01-06 00:25:21 +0000 | [diff] [blame] | 2585 | REGISTER_TRACE(pOp->p3, pOut); |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 2586 | UPDATE_MAX_BLOBSIZE(pOut); |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 2587 | break; |
| 2588 | } |
| 2589 | |
danielk1977 | a553316 | 2009-02-24 10:01:51 +0000 | [diff] [blame] | 2590 | /* Opcode: Count P1 P2 * * * |
| 2591 | ** |
| 2592 | ** Store the number of entries (an integer value) in the table or index |
| 2593 | ** opened by cursor P1 in register P2 |
| 2594 | */ |
| 2595 | #ifndef SQLITE_OMIT_BTREECOUNT |
| 2596 | case OP_Count: { /* out2-prerelease */ |
| 2597 | i64 nEntry; |
drh | c54a617 | 2009-06-02 16:06:03 +0000 | [diff] [blame] | 2598 | BtCursor *pCrsr; |
| 2599 | |
| 2600 | pCrsr = p->apCsr[pOp->p1]->pCursor; |
dan | a205a48 | 2011-08-27 18:48:57 +0000 | [diff] [blame] | 2601 | if( ALWAYS(pCrsr) ){ |
drh | 818e39a | 2009-04-02 20:27:28 +0000 | [diff] [blame] | 2602 | rc = sqlite3BtreeCount(pCrsr, &nEntry); |
| 2603 | }else{ |
| 2604 | nEntry = 0; |
| 2605 | } |
danielk1977 | a553316 | 2009-02-24 10:01:51 +0000 | [diff] [blame] | 2606 | pOut->u.i = nEntry; |
| 2607 | break; |
| 2608 | } |
| 2609 | #endif |
| 2610 | |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2611 | /* Opcode: Savepoint P1 * * P4 * |
| 2612 | ** |
| 2613 | ** Open, release or rollback the savepoint named by parameter P4, depending |
| 2614 | ** on the value of P1. To open a new savepoint, P1==0. To release (commit) an |
| 2615 | ** existing savepoint, P1==1, or to rollback an existing savepoint P1==2. |
| 2616 | */ |
| 2617 | case OP_Savepoint: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2618 | int p1; /* Value of P1 operand */ |
| 2619 | char *zName; /* Name of savepoint */ |
| 2620 | int nName; |
| 2621 | Savepoint *pNew; |
| 2622 | Savepoint *pSavepoint; |
| 2623 | Savepoint *pTmp; |
| 2624 | int iSavepoint; |
| 2625 | int ii; |
| 2626 | |
| 2627 | p1 = pOp->p1; |
| 2628 | zName = pOp->p4.z; |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2629 | |
| 2630 | /* Assert that the p1 parameter is valid. Also that if there is no open |
| 2631 | ** transaction, then there cannot be any savepoints. |
| 2632 | */ |
| 2633 | assert( db->pSavepoint==0 || db->autoCommit==0 ); |
| 2634 | assert( p1==SAVEPOINT_BEGIN||p1==SAVEPOINT_RELEASE||p1==SAVEPOINT_ROLLBACK ); |
| 2635 | assert( db->pSavepoint || db->isTransactionSavepoint==0 ); |
| 2636 | assert( checkSavepointCount(db) ); |
| 2637 | |
| 2638 | if( p1==SAVEPOINT_BEGIN ){ |
danielk1977 | 34cf35d | 2008-12-18 18:31:38 +0000 | [diff] [blame] | 2639 | if( db->writeVdbeCnt>0 ){ |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2640 | /* A new savepoint cannot be created if there are active write |
| 2641 | ** statements (i.e. open read/write incremental blob handles). |
| 2642 | */ |
| 2643 | sqlite3SetString(&p->zErrMsg, db, "cannot open savepoint - " |
| 2644 | "SQL statements in progress"); |
| 2645 | rc = SQLITE_BUSY; |
| 2646 | }else{ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2647 | nName = sqlite3Strlen30(zName); |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2648 | |
drh | be07ec5 | 2011-06-03 12:15:26 +0000 | [diff] [blame] | 2649 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
dan | d9495cd | 2011-04-27 12:08:04 +0000 | [diff] [blame] | 2650 | /* This call is Ok even if this savepoint is actually a transaction |
| 2651 | ** savepoint (and therefore should not prompt xSavepoint()) callbacks. |
| 2652 | ** If this is a transaction savepoint being opened, it is guaranteed |
| 2653 | ** that the db->aVTrans[] array is empty. */ |
| 2654 | assert( db->autoCommit==0 || db->nVTrans==0 ); |
drh | a24bc9c | 2011-05-24 00:35:56 +0000 | [diff] [blame] | 2655 | rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN, |
| 2656 | db->nStatement+db->nSavepoint); |
dan | d9495cd | 2011-04-27 12:08:04 +0000 | [diff] [blame] | 2657 | if( rc!=SQLITE_OK ) goto abort_due_to_error; |
drh | 305ebab | 2011-05-26 14:19:14 +0000 | [diff] [blame] | 2658 | #endif |
dan | d9495cd | 2011-04-27 12:08:04 +0000 | [diff] [blame] | 2659 | |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2660 | /* Create a new savepoint structure. */ |
| 2661 | pNew = sqlite3DbMallocRaw(db, sizeof(Savepoint)+nName+1); |
| 2662 | if( pNew ){ |
| 2663 | pNew->zName = (char *)&pNew[1]; |
| 2664 | memcpy(pNew->zName, zName, nName+1); |
| 2665 | |
| 2666 | /* If there is no open transaction, then mark this as a special |
| 2667 | ** "transaction savepoint". */ |
| 2668 | if( db->autoCommit ){ |
| 2669 | db->autoCommit = 0; |
| 2670 | db->isTransactionSavepoint = 1; |
| 2671 | }else{ |
| 2672 | db->nSavepoint++; |
danielk1977 | d829335 | 2009-04-30 09:10:37 +0000 | [diff] [blame] | 2673 | } |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2674 | |
| 2675 | /* Link the new savepoint into the database handle's list. */ |
| 2676 | pNew->pNext = db->pSavepoint; |
| 2677 | db->pSavepoint = pNew; |
dan | ba9108b | 2009-09-22 07:13:42 +0000 | [diff] [blame] | 2678 | pNew->nDeferredCons = db->nDeferredCons; |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2679 | } |
| 2680 | } |
| 2681 | }else{ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2682 | iSavepoint = 0; |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2683 | |
| 2684 | /* Find the named savepoint. If there is no such savepoint, then an |
| 2685 | ** an error is returned to the user. */ |
| 2686 | for( |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2687 | pSavepoint = db->pSavepoint; |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2688 | pSavepoint && sqlite3StrICmp(pSavepoint->zName, zName); |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2689 | pSavepoint = pSavepoint->pNext |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2690 | ){ |
| 2691 | iSavepoint++; |
| 2692 | } |
| 2693 | if( !pSavepoint ){ |
| 2694 | sqlite3SetString(&p->zErrMsg, db, "no such savepoint: %s", zName); |
| 2695 | rc = SQLITE_ERROR; |
| 2696 | }else if( |
| 2697 | db->writeVdbeCnt>0 || (p1==SAVEPOINT_ROLLBACK && db->activeVdbeCnt>1) |
| 2698 | ){ |
| 2699 | /* It is not possible to release (commit) a savepoint if there are |
| 2700 | ** active write statements. It is not possible to rollback a savepoint |
| 2701 | ** if there are any active statements at all. |
| 2702 | */ |
| 2703 | sqlite3SetString(&p->zErrMsg, db, |
| 2704 | "cannot %s savepoint - SQL statements in progress", |
| 2705 | (p1==SAVEPOINT_ROLLBACK ? "rollback": "release") |
| 2706 | ); |
| 2707 | rc = SQLITE_BUSY; |
| 2708 | }else{ |
| 2709 | |
| 2710 | /* Determine whether or not this is a transaction savepoint. If so, |
danielk1977 | 34cf35d | 2008-12-18 18:31:38 +0000 | [diff] [blame] | 2711 | ** and this is a RELEASE command, then the current transaction |
| 2712 | ** is committed. |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2713 | */ |
| 2714 | int isTransaction = pSavepoint->pNext==0 && db->isTransactionSavepoint; |
| 2715 | if( isTransaction && p1==SAVEPOINT_RELEASE ){ |
dan | 32b09f2 | 2009-09-23 17:29:59 +0000 | [diff] [blame] | 2716 | if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){ |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 2717 | goto vdbe_return; |
| 2718 | } |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2719 | db->autoCommit = 1; |
| 2720 | if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){ |
| 2721 | p->pc = pc; |
| 2722 | db->autoCommit = 0; |
| 2723 | p->rc = rc = SQLITE_BUSY; |
| 2724 | goto vdbe_return; |
| 2725 | } |
danielk1977 | 34cf35d | 2008-12-18 18:31:38 +0000 | [diff] [blame] | 2726 | db->isTransactionSavepoint = 0; |
| 2727 | rc = p->rc; |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2728 | }else{ |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2729 | iSavepoint = db->nSavepoint - iSavepoint - 1; |
| 2730 | for(ii=0; ii<db->nDb; ii++){ |
| 2731 | rc = sqlite3BtreeSavepoint(db->aDb[ii].pBt, p1, iSavepoint); |
| 2732 | if( rc!=SQLITE_OK ){ |
| 2733 | goto abort_due_to_error; |
danielk1977 | bd43455 | 2009-03-18 10:33:00 +0000 | [diff] [blame] | 2734 | } |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2735 | } |
drh | 9f0bbf9 | 2009-01-02 21:08:09 +0000 | [diff] [blame] | 2736 | if( p1==SAVEPOINT_ROLLBACK && (db->flags&SQLITE_InternChanges)!=0 ){ |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2737 | sqlite3ExpirePreparedStatements(db); |
drh | c7792fa | 2011-04-02 16:28:52 +0000 | [diff] [blame] | 2738 | sqlite3ResetInternalSchema(db, -1); |
dan | c311fee | 2010-08-31 16:25:19 +0000 | [diff] [blame] | 2739 | db->flags = (db->flags | SQLITE_InternChanges); |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2740 | } |
| 2741 | } |
| 2742 | |
| 2743 | /* Regardless of whether this is a RELEASE or ROLLBACK, destroy all |
| 2744 | ** savepoints nested inside of the savepoint being operated on. */ |
| 2745 | while( db->pSavepoint!=pSavepoint ){ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2746 | pTmp = db->pSavepoint; |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2747 | db->pSavepoint = pTmp->pNext; |
| 2748 | sqlite3DbFree(db, pTmp); |
| 2749 | db->nSavepoint--; |
| 2750 | } |
| 2751 | |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 2752 | /* If it is a RELEASE, then destroy the savepoint being operated on |
| 2753 | ** too. If it is a ROLLBACK TO, then set the number of deferred |
| 2754 | ** constraint violations present in the database to the value stored |
| 2755 | ** when the savepoint was created. */ |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2756 | if( p1==SAVEPOINT_RELEASE ){ |
| 2757 | assert( pSavepoint==db->pSavepoint ); |
| 2758 | db->pSavepoint = pSavepoint->pNext; |
| 2759 | sqlite3DbFree(db, pSavepoint); |
| 2760 | if( !isTransaction ){ |
| 2761 | db->nSavepoint--; |
| 2762 | } |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 2763 | }else{ |
| 2764 | db->nDeferredCons = pSavepoint->nDeferredCons; |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2765 | } |
dan | d9495cd | 2011-04-27 12:08:04 +0000 | [diff] [blame] | 2766 | |
| 2767 | if( !isTransaction ){ |
| 2768 | rc = sqlite3VtabSavepoint(db, p1, iSavepoint); |
| 2769 | if( rc!=SQLITE_OK ) goto abort_due_to_error; |
| 2770 | } |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2771 | } |
| 2772 | } |
| 2773 | |
| 2774 | break; |
| 2775 | } |
| 2776 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 2777 | /* Opcode: AutoCommit P1 P2 * * * |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 2778 | ** |
| 2779 | ** 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] | 2780 | ** back any currently active btree transactions. If there are any active |
drh | c25eabe | 2009-02-24 18:57:31 +0000 | [diff] [blame] | 2781 | ** VMs (apart from this one), then a ROLLBACK fails. A COMMIT fails if |
| 2782 | ** there are active writing VMs or active VMs that use shared cache. |
drh | 92f02c3 | 2004-09-02 14:57:08 +0000 | [diff] [blame] | 2783 | ** |
| 2784 | ** This instruction causes the VM to halt. |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 2785 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2786 | case OP_AutoCommit: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2787 | int desiredAutoCommit; |
shane | 68c0273 | 2009-06-09 18:14:18 +0000 | [diff] [blame] | 2788 | int iRollback; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2789 | int turnOnAC; |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 2790 | |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2791 | desiredAutoCommit = pOp->p1; |
shane | 68c0273 | 2009-06-09 18:14:18 +0000 | [diff] [blame] | 2792 | iRollback = pOp->p2; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2793 | turnOnAC = desiredAutoCommit && !db->autoCommit; |
drh | ad4a4b8 | 2008-11-05 16:37:34 +0000 | [diff] [blame] | 2794 | assert( desiredAutoCommit==1 || desiredAutoCommit==0 ); |
shane | 68c0273 | 2009-06-09 18:14:18 +0000 | [diff] [blame] | 2795 | assert( desiredAutoCommit==1 || iRollback==0 ); |
drh | 92f02c3 | 2004-09-02 14:57:08 +0000 | [diff] [blame] | 2796 | assert( db->activeVdbeCnt>0 ); /* At least this one VM is active */ |
danielk1977 | 46c43ed | 2004-06-30 06:30:25 +0000 | [diff] [blame] | 2797 | |
shane | 68c0273 | 2009-06-09 18:14:18 +0000 | [diff] [blame] | 2798 | if( turnOnAC && iRollback && db->activeVdbeCnt>1 ){ |
drh | ad4a4b8 | 2008-11-05 16:37:34 +0000 | [diff] [blame] | 2799 | /* If this instruction implements a ROLLBACK and other VMs are |
danielk1977 | 46c43ed | 2004-06-30 06:30:25 +0000 | [diff] [blame] | 2800 | ** still running, and a transaction is active, return an error indicating |
| 2801 | ** that the other VMs must complete first. |
| 2802 | */ |
drh | ad4a4b8 | 2008-11-05 16:37:34 +0000 | [diff] [blame] | 2803 | sqlite3SetString(&p->zErrMsg, db, "cannot rollback transaction - " |
| 2804 | "SQL statements in progress"); |
drh | 99dfe5e | 2008-10-30 15:03:15 +0000 | [diff] [blame] | 2805 | rc = SQLITE_BUSY; |
drh | 9eb8cbe | 2009-06-19 22:23:41 +0000 | [diff] [blame] | 2806 | }else if( turnOnAC && !iRollback && db->writeVdbeCnt>0 ){ |
drh | ad4a4b8 | 2008-11-05 16:37:34 +0000 | [diff] [blame] | 2807 | /* If this instruction implements a COMMIT and other VMs are writing |
| 2808 | ** return an error indicating that the other VMs must complete first. |
| 2809 | */ |
| 2810 | sqlite3SetString(&p->zErrMsg, db, "cannot commit transaction - " |
| 2811 | "SQL statements in progress"); |
| 2812 | rc = SQLITE_BUSY; |
| 2813 | }else if( desiredAutoCommit!=db->autoCommit ){ |
shane | 68c0273 | 2009-06-09 18:14:18 +0000 | [diff] [blame] | 2814 | if( iRollback ){ |
drh | ad4a4b8 | 2008-11-05 16:37:34 +0000 | [diff] [blame] | 2815 | assert( desiredAutoCommit==1 ); |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 2816 | sqlite3RollbackAll(db); |
danielk1977 | f3f06bb | 2005-12-16 15:24:28 +0000 | [diff] [blame] | 2817 | db->autoCommit = 1; |
dan | 32b09f2 | 2009-09-23 17:29:59 +0000 | [diff] [blame] | 2818 | }else if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){ |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 2819 | goto vdbe_return; |
danielk1977 | f3f06bb | 2005-12-16 15:24:28 +0000 | [diff] [blame] | 2820 | }else{ |
shane | 7d3846a | 2008-12-11 02:58:26 +0000 | [diff] [blame] | 2821 | db->autoCommit = (u8)desiredAutoCommit; |
danielk1977 | f3f06bb | 2005-12-16 15:24:28 +0000 | [diff] [blame] | 2822 | if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){ |
danielk1977 | f3f06bb | 2005-12-16 15:24:28 +0000 | [diff] [blame] | 2823 | p->pc = pc; |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 2824 | db->autoCommit = (u8)(1-desiredAutoCommit); |
drh | 900b31e | 2007-08-28 02:27:51 +0000 | [diff] [blame] | 2825 | p->rc = rc = SQLITE_BUSY; |
| 2826 | goto vdbe_return; |
danielk1977 | f3f06bb | 2005-12-16 15:24:28 +0000 | [diff] [blame] | 2827 | } |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 2828 | } |
danielk1977 | bd43455 | 2009-03-18 10:33:00 +0000 | [diff] [blame] | 2829 | assert( db->nStatement==0 ); |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2830 | sqlite3CloseSavepoints(db); |
drh | 83968c4 | 2007-04-18 16:45:24 +0000 | [diff] [blame] | 2831 | if( p->rc==SQLITE_OK ){ |
drh | 900b31e | 2007-08-28 02:27:51 +0000 | [diff] [blame] | 2832 | rc = SQLITE_DONE; |
drh | 83968c4 | 2007-04-18 16:45:24 +0000 | [diff] [blame] | 2833 | }else{ |
drh | 900b31e | 2007-08-28 02:27:51 +0000 | [diff] [blame] | 2834 | rc = SQLITE_ERROR; |
drh | 83968c4 | 2007-04-18 16:45:24 +0000 | [diff] [blame] | 2835 | } |
drh | 900b31e | 2007-08-28 02:27:51 +0000 | [diff] [blame] | 2836 | goto vdbe_return; |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 2837 | }else{ |
drh | f089aa4 | 2008-07-08 19:34:06 +0000 | [diff] [blame] | 2838 | sqlite3SetString(&p->zErrMsg, db, |
drh | ad4a4b8 | 2008-11-05 16:37:34 +0000 | [diff] [blame] | 2839 | (!desiredAutoCommit)?"cannot start a transaction within a transaction":( |
shane | 68c0273 | 2009-06-09 18:14:18 +0000 | [diff] [blame] | 2840 | (iRollback)?"cannot rollback - no transaction is active": |
drh | f089aa4 | 2008-07-08 19:34:06 +0000 | [diff] [blame] | 2841 | "cannot commit - no transaction is active")); |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 2842 | |
| 2843 | rc = SQLITE_ERROR; |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2844 | } |
| 2845 | break; |
| 2846 | } |
| 2847 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 2848 | /* Opcode: Transaction P1 P2 * * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2849 | ** |
| 2850 | ** Begin a transaction. The transaction ends when a Commit or Rollback |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2851 | ** opcode is encountered. Depending on the ON CONFLICT setting, the |
| 2852 | ** transaction might also be rolled back if an error is encountered. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2853 | ** |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2854 | ** P1 is the index of the database file on which the transaction is |
| 2855 | ** started. Index 0 is the main database file and index 1 is the |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 2856 | ** file used for temporary tables. Indices of 2 or more are used for |
| 2857 | ** attached databases. |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 2858 | ** |
drh | 8024205 | 2004-06-09 00:48:12 +0000 | [diff] [blame] | 2859 | ** 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] | 2860 | ** obtained on the database file when a write-transaction is started. No |
drh | 8024205 | 2004-06-09 00:48:12 +0000 | [diff] [blame] | 2861 | ** other process can start another write transaction while this transaction is |
| 2862 | ** underway. Starting a write transaction also creates a rollback journal. A |
| 2863 | ** write transaction must be started before any changes can be made to the |
drh | 684917c | 2004-10-05 02:41:42 +0000 | [diff] [blame] | 2864 | ** database. If P2 is 2 or greater then an EXCLUSIVE lock is also obtained |
| 2865 | ** on the file. |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 2866 | ** |
dan | e0af83a | 2009-09-08 19:15:01 +0000 | [diff] [blame] | 2867 | ** If a write-transaction is started and the Vdbe.usesStmtJournal flag is |
| 2868 | ** true (this flag is set if the Vdbe may modify more than one row and may |
| 2869 | ** throw an ABORT exception), a statement transaction may also be opened. |
| 2870 | ** More specifically, a statement transaction is opened iff the database |
| 2871 | ** connection is currently not in autocommit mode, or if there are other |
| 2872 | ** active statements. A statement transaction allows the affects of this |
| 2873 | ** VDBE to be rolled back after an error without having to roll back the |
| 2874 | ** entire transaction. If no error is encountered, the statement transaction |
| 2875 | ** will automatically commit when the VDBE halts. |
| 2876 | ** |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 2877 | ** 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] | 2878 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2879 | case OP_Transaction: { |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 2880 | Btree *pBt; |
| 2881 | |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 2882 | assert( pOp->p1>=0 && pOp->p1<db->nDb ); |
drh | dddd779 | 2011-04-03 18:19:25 +0000 | [diff] [blame] | 2883 | assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 ); |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 2884 | pBt = db->aDb[pOp->p1].pBt; |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 2885 | |
danielk1977 | 24162fe | 2004-06-04 06:22:00 +0000 | [diff] [blame] | 2886 | if( pBt ){ |
danielk1977 | 40b38dc | 2004-06-26 08:38:24 +0000 | [diff] [blame] | 2887 | rc = sqlite3BtreeBeginTrans(pBt, pOp->p2); |
danielk1977 | 24162fe | 2004-06-04 06:22:00 +0000 | [diff] [blame] | 2888 | if( rc==SQLITE_BUSY ){ |
danielk1977 | 2a764eb | 2004-06-12 01:43:26 +0000 | [diff] [blame] | 2889 | p->pc = pc; |
drh | 900b31e | 2007-08-28 02:27:51 +0000 | [diff] [blame] | 2890 | p->rc = rc = SQLITE_BUSY; |
drh | 900b31e | 2007-08-28 02:27:51 +0000 | [diff] [blame] | 2891 | goto vdbe_return; |
danielk1977 | 24162fe | 2004-06-04 06:22:00 +0000 | [diff] [blame] | 2892 | } |
drh | 9e9f1bd | 2009-10-13 15:36:51 +0000 | [diff] [blame] | 2893 | if( rc!=SQLITE_OK ){ |
danielk1977 | 24162fe | 2004-06-04 06:22:00 +0000 | [diff] [blame] | 2894 | goto abort_due_to_error; |
drh | 90bfcda | 2001-09-23 19:46:51 +0000 | [diff] [blame] | 2895 | } |
dan | e0af83a | 2009-09-08 19:15:01 +0000 | [diff] [blame] | 2896 | |
| 2897 | if( pOp->p2 && p->usesStmtJournal |
| 2898 | && (db->autoCommit==0 || db->activeVdbeCnt>1) |
| 2899 | ){ |
| 2900 | assert( sqlite3BtreeIsInTrans(pBt) ); |
| 2901 | if( p->iStatement==0 ){ |
| 2902 | assert( db->nStatement>=0 && db->nSavepoint>=0 ); |
| 2903 | db->nStatement++; |
| 2904 | p->iStatement = db->nSavepoint + db->nStatement; |
| 2905 | } |
dan | a311b80 | 2011-04-26 19:21:34 +0000 | [diff] [blame] | 2906 | |
drh | 346506f | 2011-05-25 01:16:42 +0000 | [diff] [blame] | 2907 | rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN, p->iStatement-1); |
dan | a311b80 | 2011-04-26 19:21:34 +0000 | [diff] [blame] | 2908 | if( rc==SQLITE_OK ){ |
| 2909 | rc = sqlite3BtreeBeginStmt(pBt, p->iStatement); |
| 2910 | } |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 2911 | |
| 2912 | /* Store the current value of the database handles deferred constraint |
| 2913 | ** counter. If the statement transaction needs to be rolled back, |
| 2914 | ** the value of this counter needs to be restored too. */ |
| 2915 | p->nStmtDefCons = db->nDeferredCons; |
dan | e0af83a | 2009-09-08 19:15:01 +0000 | [diff] [blame] | 2916 | } |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 2917 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2918 | break; |
| 2919 | } |
| 2920 | |
drh | b1fdb2a | 2008-01-05 04:06:03 +0000 | [diff] [blame] | 2921 | /* Opcode: ReadCookie P1 P2 P3 * * |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 2922 | ** |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2923 | ** Read cookie number P3 from database P1 and write it into register P2. |
danielk1977 | 0d19f7a | 2009-06-03 11:25:07 +0000 | [diff] [blame] | 2924 | ** P3==1 is the schema version. P3==2 is the database format. |
| 2925 | ** P3==3 is the recommended pager cache size, and so forth. P1==0 is |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2926 | ** the main database file and P1==1 is the database file used to store |
| 2927 | ** temporary tables. |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 2928 | ** |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 2929 | ** There must be a read-lock on the database (either a transaction |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2930 | ** must be started or there must be an open cursor) before |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 2931 | ** executing this instruction. |
| 2932 | */ |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 2933 | case OP_ReadCookie: { /* out2-prerelease */ |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 2934 | int iMeta; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2935 | int iDb; |
| 2936 | int iCookie; |
danielk1977 | 180b56a | 2007-06-24 08:00:42 +0000 | [diff] [blame] | 2937 | |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2938 | iDb = pOp->p1; |
| 2939 | iCookie = pOp->p3; |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 2940 | assert( pOp->p3<SQLITE_N_BTREE_META ); |
danielk1977 | 180b56a | 2007-06-24 08:00:42 +0000 | [diff] [blame] | 2941 | assert( iDb>=0 && iDb<db->nDb ); |
| 2942 | assert( db->aDb[iDb].pBt!=0 ); |
drh | dddd779 | 2011-04-03 18:19:25 +0000 | [diff] [blame] | 2943 | assert( (p->btreeMask & (((yDbMask)1)<<iDb))!=0 ); |
danielk1977 | 0d19f7a | 2009-06-03 11:25:07 +0000 | [diff] [blame] | 2944 | |
danielk1977 | 602b466 | 2009-07-02 07:47:33 +0000 | [diff] [blame] | 2945 | sqlite3BtreeGetMeta(db->aDb[iDb].pBt, iCookie, (u32 *)&iMeta); |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 2946 | pOut->u.i = iMeta; |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 2947 | break; |
| 2948 | } |
| 2949 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 2950 | /* Opcode: SetCookie P1 P2 P3 * * |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 2951 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 2952 | ** Write the content of register P3 (interpreted as an integer) |
danielk1977 | 0d19f7a | 2009-06-03 11:25:07 +0000 | [diff] [blame] | 2953 | ** into cookie number P2 of database P1. P2==1 is the schema version. |
| 2954 | ** P2==2 is the database format. P2==3 is the recommended pager cache |
| 2955 | ** size, and so forth. P1==0 is the main database file and P1==1 is the |
| 2956 | ** database file used to store temporary tables. |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 2957 | ** |
| 2958 | ** A transaction must be started before executing this opcode. |
| 2959 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2960 | case OP_SetCookie: { /* in3 */ |
drh | 3f7d4e4 | 2004-07-24 14:35:58 +0000 | [diff] [blame] | 2961 | Db *pDb; |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 2962 | assert( pOp->p2<SQLITE_N_BTREE_META ); |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2963 | assert( pOp->p1>=0 && pOp->p1<db->nDb ); |
drh | dddd779 | 2011-04-03 18:19:25 +0000 | [diff] [blame] | 2964 | assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 ); |
drh | 3f7d4e4 | 2004-07-24 14:35:58 +0000 | [diff] [blame] | 2965 | pDb = &db->aDb[pOp->p1]; |
| 2966 | assert( pDb->pBt!=0 ); |
drh | 2120608 | 2011-04-04 18:22:02 +0000 | [diff] [blame] | 2967 | assert( sqlite3SchemaMutexHeld(db, pOp->p1, 0) ); |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 2968 | pIn3 = &aMem[pOp->p3]; |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 2969 | sqlite3VdbeMemIntegerify(pIn3); |
drh | a3b321d | 2004-05-11 09:31:31 +0000 | [diff] [blame] | 2970 | /* See note about index shifting on OP_ReadCookie */ |
danielk1977 | 0d19f7a | 2009-06-03 11:25:07 +0000 | [diff] [blame] | 2971 | rc = sqlite3BtreeUpdateMeta(pDb->pBt, pOp->p2, (int)pIn3->u.i); |
| 2972 | if( pOp->p2==BTREE_SCHEMA_VERSION ){ |
drh | 3f7d4e4 | 2004-07-24 14:35:58 +0000 | [diff] [blame] | 2973 | /* When the schema cookie changes, record the new cookie internally */ |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 2974 | pDb->pSchema->schema_cookie = (int)pIn3->u.i; |
drh | 3f7d4e4 | 2004-07-24 14:35:58 +0000 | [diff] [blame] | 2975 | db->flags |= SQLITE_InternChanges; |
danielk1977 | 0d19f7a | 2009-06-03 11:25:07 +0000 | [diff] [blame] | 2976 | }else if( pOp->p2==BTREE_FILE_FORMAT ){ |
drh | d28bcb3 | 2005-12-21 14:43:11 +0000 | [diff] [blame] | 2977 | /* Record changes in the file format */ |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 2978 | pDb->pSchema->file_format = (u8)pIn3->u.i; |
drh | 3f7d4e4 | 2004-07-24 14:35:58 +0000 | [diff] [blame] | 2979 | } |
drh | fd426c6 | 2006-01-30 15:34:22 +0000 | [diff] [blame] | 2980 | if( pOp->p1==1 ){ |
| 2981 | /* Invalidate all prepared statements whenever the TEMP database |
| 2982 | ** schema is changed. Ticket #1644 */ |
| 2983 | sqlite3ExpirePreparedStatements(db); |
dan | fa401de | 2009-10-16 14:55:03 +0000 | [diff] [blame] | 2984 | p->expired = 0; |
drh | fd426c6 | 2006-01-30 15:34:22 +0000 | [diff] [blame] | 2985 | } |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 2986 | break; |
| 2987 | } |
| 2988 | |
drh | c2a7555 | 2011-03-18 21:55:46 +0000 | [diff] [blame] | 2989 | /* Opcode: VerifyCookie P1 P2 P3 * * |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 2990 | ** |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2991 | ** Check the value of global database parameter number 0 (the |
drh | c2a7555 | 2011-03-18 21:55:46 +0000 | [diff] [blame] | 2992 | ** schema version) and make sure it is equal to P2 and that the |
| 2993 | ** generation counter on the local schema parse equals P3. |
| 2994 | ** |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2995 | ** P1 is the database number which is 0 for the main database file |
| 2996 | ** and 1 for the file holding temporary tables and some higher number |
| 2997 | ** for auxiliary databases. |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 2998 | ** |
| 2999 | ** The cookie changes its value whenever the database schema changes. |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 3000 | ** This operation is used to detect when that the cookie has changed |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 3001 | ** and that the current process needs to reread the schema. |
| 3002 | ** |
| 3003 | ** Either a transaction needs to have been started or an OP_Open needs |
| 3004 | ** to be executed (to establish a read lock) before this opcode is |
| 3005 | ** invoked. |
| 3006 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3007 | case OP_VerifyCookie: { |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 3008 | int iMeta; |
drh | c2a7555 | 2011-03-18 21:55:46 +0000 | [diff] [blame] | 3009 | int iGen; |
drh | c275b4e | 2004-07-19 17:25:24 +0000 | [diff] [blame] | 3010 | Btree *pBt; |
drh | c2a7555 | 2011-03-18 21:55:46 +0000 | [diff] [blame] | 3011 | |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 3012 | assert( pOp->p1>=0 && pOp->p1<db->nDb ); |
drh | dddd779 | 2011-04-03 18:19:25 +0000 | [diff] [blame] | 3013 | assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 ); |
drh | 2120608 | 2011-04-04 18:22:02 +0000 | [diff] [blame] | 3014 | assert( sqlite3SchemaMutexHeld(db, pOp->p1, 0) ); |
drh | c275b4e | 2004-07-19 17:25:24 +0000 | [diff] [blame] | 3015 | pBt = db->aDb[pOp->p1].pBt; |
| 3016 | if( pBt ){ |
danielk1977 | 602b466 | 2009-07-02 07:47:33 +0000 | [diff] [blame] | 3017 | sqlite3BtreeGetMeta(pBt, BTREE_SCHEMA_VERSION, (u32 *)&iMeta); |
drh | c2a7555 | 2011-03-18 21:55:46 +0000 | [diff] [blame] | 3018 | iGen = db->aDb[pOp->p1].pSchema->iGeneration; |
drh | c275b4e | 2004-07-19 17:25:24 +0000 | [diff] [blame] | 3019 | }else{ |
drh | fcd71b6 | 2011-04-05 22:08:24 +0000 | [diff] [blame] | 3020 | iGen = iMeta = 0; |
drh | c275b4e | 2004-07-19 17:25:24 +0000 | [diff] [blame] | 3021 | } |
drh | c2a7555 | 2011-03-18 21:55:46 +0000 | [diff] [blame] | 3022 | if( iMeta!=pOp->p2 || iGen!=pOp->p3 ){ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 3023 | sqlite3DbFree(db, p->zErrMsg); |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 3024 | p->zErrMsg = sqlite3DbStrDup(db, "database schema has changed"); |
danielk1977 | 896e792 | 2007-04-17 08:32:33 +0000 | [diff] [blame] | 3025 | /* If the schema-cookie from the database file matches the cookie |
| 3026 | ** stored with the in-memory representation of the schema, do |
| 3027 | ** not reload the schema from the database file. |
| 3028 | ** |
shane | 21e7feb | 2008-05-30 15:59:49 +0000 | [diff] [blame] | 3029 | ** If virtual-tables are in use, this is not just an optimization. |
danielk1977 | 896e792 | 2007-04-17 08:32:33 +0000 | [diff] [blame] | 3030 | ** Often, v-tables store their data in other SQLite tables, which |
| 3031 | ** are queried from within xNext() and other v-table methods using |
| 3032 | ** prepared queries. If such a query is out-of-date, we do not want to |
| 3033 | ** discard the database schema, as the user code implementing the |
| 3034 | ** v-table would have to be ready for the sqlite3_vtab structure itself |
| 3035 | ** to be invalidated whenever sqlite3_step() is called from within |
| 3036 | ** a v-table method. |
| 3037 | */ |
| 3038 | if( db->aDb[pOp->p1].pSchema->schema_cookie!=iMeta ){ |
| 3039 | sqlite3ResetInternalSchema(db, pOp->p1); |
| 3040 | } |
| 3041 | |
drh | 5b6c545 | 2011-02-22 03:34:56 +0000 | [diff] [blame] | 3042 | p->expired = 1; |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 3043 | rc = SQLITE_SCHEMA; |
| 3044 | } |
| 3045 | break; |
| 3046 | } |
| 3047 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 3048 | /* Opcode: OpenRead P1 P2 P3 P4 P5 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3049 | ** |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 3050 | ** Open a read-only cursor for the database table whose root page is |
danielk1977 | 207872a | 2008-01-03 07:54:23 +0000 | [diff] [blame] | 3051 | ** P2 in a database file. The database file is determined by P3. |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 3052 | ** P3==0 means the main database, P3==1 means the database used for |
| 3053 | ** temporary tables, and P3>1 means used the corresponding attached |
| 3054 | ** database. Give the new cursor an identifier of P1. The P1 |
danielk1977 | 207872a | 2008-01-03 07:54:23 +0000 | [diff] [blame] | 3055 | ** values need not be contiguous but all P1 values should be small integers. |
| 3056 | ** It is an error for P1 to be negative. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3057 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 3058 | ** If P5!=0 then use the content of register P2 as the root page, not |
| 3059 | ** the value of P2 itself. |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 3060 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 3061 | ** There will be a read lock on the database whenever there is an |
| 3062 | ** open cursor. If the database was unlocked prior to this instruction |
| 3063 | ** then a read lock is acquired as part of this instruction. A read |
| 3064 | ** lock allows other processes to read the database but prohibits |
| 3065 | ** any other process from modifying the database. The read lock is |
| 3066 | ** released when all cursors are closed. If this instruction attempts |
| 3067 | ** to get a read lock but fails, the script terminates with an |
| 3068 | ** SQLITE_BUSY error code. |
| 3069 | ** |
danielk1977 | d336e22 | 2009-02-20 10:58:41 +0000 | [diff] [blame] | 3070 | ** The P4 value may be either an integer (P4_INT32) or a pointer to |
| 3071 | ** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo |
| 3072 | ** structure, then said structure defines the content and collating |
| 3073 | ** sequence of the index being opened. Otherwise, if P4 is an integer |
| 3074 | ** value, it is set to the number of columns in the table. |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 3075 | ** |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 3076 | ** See also OpenWrite. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3077 | */ |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 3078 | /* Opcode: OpenWrite P1 P2 P3 P4 P5 |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 3079 | ** |
| 3080 | ** 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] | 3081 | ** page is P2. Or if P5!=0 use the content of register P2 to find the |
| 3082 | ** root page. |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 3083 | ** |
danielk1977 | d336e22 | 2009-02-20 10:58:41 +0000 | [diff] [blame] | 3084 | ** The P4 value may be either an integer (P4_INT32) or a pointer to |
| 3085 | ** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo |
| 3086 | ** structure, then said structure defines the content and collating |
| 3087 | ** sequence of the index being opened. Otherwise, if P4 is an integer |
drh | 35cd643 | 2009-06-05 14:17:21 +0000 | [diff] [blame] | 3088 | ** value, it is set to the number of columns in the table, or to the |
| 3089 | ** largest index of any column of the table that is actually used. |
jplyon | 5a56422 | 2003-06-02 06:15:58 +0000 | [diff] [blame] | 3090 | ** |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 3091 | ** This instruction works just like OpenRead except that it opens the cursor |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 3092 | ** in read/write mode. For a given table, there can be one or more read-only |
| 3093 | ** cursors or a single read/write cursor but not both. |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 3094 | ** |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 3095 | ** See also OpenRead. |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 3096 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3097 | case OP_OpenRead: |
| 3098 | case OP_OpenWrite: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3099 | int nField; |
| 3100 | KeyInfo *pKeyInfo; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3101 | int p2; |
| 3102 | int iDb; |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 3103 | int wrFlag; |
| 3104 | Btree *pX; |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 3105 | VdbeCursor *pCur; |
drh | d946db0 | 2005-12-29 19:23:06 +0000 | [diff] [blame] | 3106 | Db *pDb; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3107 | |
dan | fa401de | 2009-10-16 14:55:03 +0000 | [diff] [blame] | 3108 | if( p->expired ){ |
| 3109 | rc = SQLITE_ABORT; |
| 3110 | break; |
| 3111 | } |
| 3112 | |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3113 | nField = 0; |
| 3114 | pKeyInfo = 0; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3115 | p2 = pOp->p2; |
| 3116 | iDb = pOp->p3; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3117 | assert( iDb>=0 && iDb<db->nDb ); |
drh | dddd779 | 2011-04-03 18:19:25 +0000 | [diff] [blame] | 3118 | assert( (p->btreeMask & (((yDbMask)1)<<iDb))!=0 ); |
drh | d946db0 | 2005-12-29 19:23:06 +0000 | [diff] [blame] | 3119 | pDb = &db->aDb[iDb]; |
| 3120 | pX = pDb->pBt; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3121 | assert( pX!=0 ); |
drh | d946db0 | 2005-12-29 19:23:06 +0000 | [diff] [blame] | 3122 | if( pOp->opcode==OP_OpenWrite ){ |
| 3123 | wrFlag = 1; |
drh | 2120608 | 2011-04-04 18:22:02 +0000 | [diff] [blame] | 3124 | assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3125 | if( pDb->pSchema->file_format < p->minWriteFileFormat ){ |
| 3126 | p->minWriteFileFormat = pDb->pSchema->file_format; |
drh | d946db0 | 2005-12-29 19:23:06 +0000 | [diff] [blame] | 3127 | } |
| 3128 | }else{ |
| 3129 | wrFlag = 0; |
| 3130 | } |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 3131 | if( pOp->p5 ){ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3132 | assert( p2>0 ); |
| 3133 | assert( p2<=p->nMem ); |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 3134 | pIn2 = &aMem[p2]; |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 3135 | assert( memIsValid(pIn2) ); |
| 3136 | assert( (pIn2->flags & MEM_Int)!=0 ); |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3137 | sqlite3VdbeMemIntegerify(pIn2); |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 3138 | p2 = (int)pIn2->u.i; |
drh | 9a65f2c | 2009-06-22 19:05:40 +0000 | [diff] [blame] | 3139 | /* The p2 value always comes from a prior OP_CreateTable opcode and |
| 3140 | ** that opcode will always set the p2 value to 2 or more or else fail. |
| 3141 | ** If there were a failure, the prepared statement would have halted |
| 3142 | ** before reaching this instruction. */ |
drh | 27731d7 | 2009-06-22 12:05:10 +0000 | [diff] [blame] | 3143 | if( NEVER(p2<2) ) { |
shane | dcc50b7 | 2008-11-13 18:29:50 +0000 | [diff] [blame] | 3144 | rc = SQLITE_CORRUPT_BKPT; |
| 3145 | goto abort_due_to_error; |
| 3146 | } |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 3147 | } |
danielk1977 | d336e22 | 2009-02-20 10:58:41 +0000 | [diff] [blame] | 3148 | if( pOp->p4type==P4_KEYINFO ){ |
| 3149 | pKeyInfo = pOp->p4.pKeyInfo; |
| 3150 | pKeyInfo->enc = ENC(p->db); |
| 3151 | nField = pKeyInfo->nField+1; |
| 3152 | }else if( pOp->p4type==P4_INT32 ){ |
| 3153 | nField = pOp->p4.i; |
| 3154 | } |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 3155 | assert( pOp->p1>=0 ); |
| 3156 | pCur = allocateCursor(p, pOp->p1, nField, iDb, 1); |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 3157 | if( pCur==0 ) goto no_mem; |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 3158 | pCur->nullRow = 1; |
drh | d4187c7 | 2010-08-30 22:15:45 +0000 | [diff] [blame] | 3159 | pCur->isOrdered = 1; |
danielk1977 | d336e22 | 2009-02-20 10:58:41 +0000 | [diff] [blame] | 3160 | rc = sqlite3BtreeCursor(pX, p2, wrFlag, pKeyInfo, pCur->pCursor); |
| 3161 | pCur->pKeyInfo = pKeyInfo; |
| 3162 | |
dan | a205a48 | 2011-08-27 18:48:57 +0000 | [diff] [blame] | 3163 | /* Since it performs no memory allocation or IO, the only value that |
| 3164 | ** sqlite3BtreeCursor() may return is SQLITE_OK. */ |
| 3165 | assert( rc==SQLITE_OK ); |
danielk1977 | 172114a | 2009-07-07 15:47:12 +0000 | [diff] [blame] | 3166 | |
| 3167 | /* Set the VdbeCursor.isTable and isIndex variables. Previous versions of |
| 3168 | ** SQLite used to check if the root-page flags were sane at this point |
| 3169 | ** and report database corruption if they were not, but this check has |
| 3170 | ** since moved into the btree layer. */ |
| 3171 | pCur->isTable = pOp->p4type!=P4_KEYINFO; |
| 3172 | pCur->isIndex = !pCur->isTable; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3173 | break; |
| 3174 | } |
| 3175 | |
drh | 2a5d990 | 2011-08-26 00:34:45 +0000 | [diff] [blame] | 3176 | /* Opcode: OpenEphemeral P1 P2 * P4 P5 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3177 | ** |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 3178 | ** Open a new cursor P1 to a transient table. |
drh | 9170dd7 | 2005-07-08 17:13:46 +0000 | [diff] [blame] | 3179 | ** The cursor is always opened read/write even if |
drh | 25d3adb | 2010-04-05 15:11:08 +0000 | [diff] [blame] | 3180 | ** the main database is read-only. The ephemeral |
drh | 9170dd7 | 2005-07-08 17:13:46 +0000 | [diff] [blame] | 3181 | ** table is deleted automatically when the cursor is closed. |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 3182 | ** |
drh | 25d3adb | 2010-04-05 15:11:08 +0000 | [diff] [blame] | 3183 | ** P2 is the number of columns in the ephemeral table. |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 3184 | ** The cursor points to a BTree table if P4==0 and to a BTree index |
| 3185 | ** 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] | 3186 | ** that defines the format of keys in the index. |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 3187 | ** |
| 3188 | ** This opcode was once called OpenTemp. But that created |
| 3189 | ** confusion because the term "temp table", might refer either |
| 3190 | ** to a TEMP table at the SQL level, or to a table opened by |
| 3191 | ** this opcode. Then this opcode was call OpenVirtual. But |
| 3192 | ** that created confusion with the whole virtual-table idea. |
drh | 2a5d990 | 2011-08-26 00:34:45 +0000 | [diff] [blame] | 3193 | ** |
| 3194 | ** The P5 parameter can be a mask of the BTREE_* flags defined |
| 3195 | ** in btree.h. These flags control aspects of the operation of |
| 3196 | ** the btree. The BTREE_OMIT_JOURNAL and BTREE_SINGLE flags are |
| 3197 | ** added automatically. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3198 | */ |
drh | a21a64d | 2010-04-06 22:33:55 +0000 | [diff] [blame] | 3199 | /* Opcode: OpenAutoindex P1 P2 * P4 * |
| 3200 | ** |
| 3201 | ** This opcode works the same as OP_OpenEphemeral. It has a |
| 3202 | ** different name to distinguish its use. Tables created using |
| 3203 | ** by this opcode will be used for automatically created transient |
| 3204 | ** indices in joins. |
| 3205 | */ |
| 3206 | case OP_OpenAutoindex: |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3207 | case OP_OpenEphemeral: { |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 3208 | VdbeCursor *pCx; |
drh | d4187c7 | 2010-08-30 22:15:45 +0000 | [diff] [blame] | 3209 | static const int vfsFlags = |
drh | 33f4e02 | 2007-09-03 15:19:34 +0000 | [diff] [blame] | 3210 | SQLITE_OPEN_READWRITE | |
| 3211 | SQLITE_OPEN_CREATE | |
| 3212 | SQLITE_OPEN_EXCLUSIVE | |
| 3213 | SQLITE_OPEN_DELETEONCLOSE | |
| 3214 | SQLITE_OPEN_TRANSIENT_DB; |
| 3215 | |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 3216 | assert( pOp->p1>=0 ); |
| 3217 | pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, 1); |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 3218 | if( pCx==0 ) goto no_mem; |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 3219 | pCx->nullRow = 1; |
dan | 689ab89 | 2011-08-12 15:02:00 +0000 | [diff] [blame] | 3220 | rc = sqlite3BtreeOpen(db->pVfs, 0, db, &pCx->pBt, |
| 3221 | BTREE_OMIT_JOURNAL | BTREE_SINGLE | pOp->p5, vfsFlags); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3222 | if( rc==SQLITE_OK ){ |
danielk1977 | 40b38dc | 2004-06-26 08:38:24 +0000 | [diff] [blame] | 3223 | rc = sqlite3BtreeBeginTrans(pCx->pBt, 1); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3224 | } |
| 3225 | if( rc==SQLITE_OK ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3226 | /* If a transient index is required, create it by calling |
drh | d4187c7 | 2010-08-30 22:15:45 +0000 | [diff] [blame] | 3227 | ** sqlite3BtreeCreateTable() with the BTREE_BLOBKEY flag before |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3228 | ** opening it. If a transient table is required, just use the |
drh | d4187c7 | 2010-08-30 22:15:45 +0000 | [diff] [blame] | 3229 | ** automatically created table with root-page 1 (an BLOB_INTKEY table). |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3230 | */ |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 3231 | if( pOp->p4.pKeyInfo ){ |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 3232 | int pgno; |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 3233 | assert( pOp->p4type==P4_KEYINFO ); |
drh | e1b4f0f | 2011-06-29 17:11:39 +0000 | [diff] [blame] | 3234 | rc = sqlite3BtreeCreateTable(pCx->pBt, &pgno, BTREE_BLOBKEY | pOp->p5); |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 3235 | if( rc==SQLITE_OK ){ |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 3236 | assert( pgno==MASTER_ROOT+1 ); |
drh | 1e968a0 | 2008-03-25 00:22:21 +0000 | [diff] [blame] | 3237 | rc = sqlite3BtreeCursor(pCx->pBt, pgno, 1, |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 3238 | (KeyInfo*)pOp->p4.z, pCx->pCursor); |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 3239 | pCx->pKeyInfo = pOp->p4.pKeyInfo; |
dan | 689ab89 | 2011-08-12 15:02:00 +0000 | [diff] [blame] | 3240 | pCx->pKeyInfo->enc = ENC(p->db); |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 3241 | } |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3242 | pCx->isTable = 0; |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 3243 | }else{ |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 3244 | rc = sqlite3BtreeCursor(pCx->pBt, MASTER_ROOT, 1, 0, pCx->pCursor); |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3245 | pCx->isTable = 1; |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 3246 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3247 | } |
drh | d4187c7 | 2010-08-30 22:15:45 +0000 | [diff] [blame] | 3248 | pCx->isOrdered = (pOp->p5!=BTREE_UNORDERED); |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3249 | pCx->isIndex = !pCx->isTable; |
dan | 5134d13 | 2011-09-02 10:31:11 +0000 | [diff] [blame] | 3250 | break; |
| 3251 | } |
| 3252 | |
| 3253 | /* Opcode: OpenSorter P1 P2 * P4 * |
| 3254 | ** |
| 3255 | ** This opcode works like OP_OpenEphemeral except that it opens |
| 3256 | ** a transient index that is specifically designed to sort large |
| 3257 | ** tables using an external merge-sort algorithm. |
| 3258 | */ |
drh | ca892a7 | 2011-09-03 00:17:51 +0000 | [diff] [blame] | 3259 | case OP_SorterOpen: { |
dan | 5134d13 | 2011-09-02 10:31:11 +0000 | [diff] [blame] | 3260 | VdbeCursor *pCx; |
drh | ca892a7 | 2011-09-03 00:17:51 +0000 | [diff] [blame] | 3261 | #ifndef SQLITE_OMIT_MERGE_SORT |
dan | 5134d13 | 2011-09-02 10:31:11 +0000 | [diff] [blame] | 3262 | pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, 1); |
| 3263 | if( pCx==0 ) goto no_mem; |
| 3264 | pCx->pKeyInfo = pOp->p4.pKeyInfo; |
| 3265 | pCx->pKeyInfo->enc = ENC(p->db); |
| 3266 | pCx->isSorter = 1; |
| 3267 | rc = sqlite3VdbeSorterInit(db, pCx); |
drh | ca892a7 | 2011-09-03 00:17:51 +0000 | [diff] [blame] | 3268 | #else |
| 3269 | pOp->opcode = OP_OpenEphemeral; |
| 3270 | pc--; |
| 3271 | #endif |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3272 | break; |
| 3273 | } |
| 3274 | |
danielk1977 | d336e22 | 2009-02-20 10:58:41 +0000 | [diff] [blame] | 3275 | /* Opcode: OpenPseudo P1 P2 P3 * * |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3276 | ** |
| 3277 | ** Open a new cursor that points to a fake table that contains a single |
drh | 3e9ca09 | 2009-09-08 01:14:48 +0000 | [diff] [blame] | 3278 | ** row of data. The content of that one row in the content of memory |
| 3279 | ** register P2. In other words, cursor P1 becomes an alias for the |
| 3280 | ** MEM_Blob content contained in register P2. |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3281 | ** |
drh | 2d8d7ce | 2010-02-15 15:17:05 +0000 | [diff] [blame] | 3282 | ** A pseudo-table created by this opcode is used to hold a single |
drh | cdd536f | 2006-03-17 00:04:03 +0000 | [diff] [blame] | 3283 | ** row output from the sorter so that the row can be decomposed into |
drh | 3e9ca09 | 2009-09-08 01:14:48 +0000 | [diff] [blame] | 3284 | ** individual columns using the OP_Column opcode. The OP_Column opcode |
| 3285 | ** is the only cursor opcode that works with a pseudo-table. |
danielk1977 | d336e22 | 2009-02-20 10:58:41 +0000 | [diff] [blame] | 3286 | ** |
| 3287 | ** P3 is the number of fields in the records that will be stored by |
| 3288 | ** the pseudo-table. |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3289 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3290 | case OP_OpenPseudo: { |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 3291 | VdbeCursor *pCx; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3292 | |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 3293 | assert( pOp->p1>=0 ); |
| 3294 | pCx = allocateCursor(p, pOp->p1, pOp->p3, -1, 0); |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 3295 | if( pCx==0 ) goto no_mem; |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3296 | pCx->nullRow = 1; |
drh | 3e9ca09 | 2009-09-08 01:14:48 +0000 | [diff] [blame] | 3297 | pCx->pseudoTableReg = pOp->p2; |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3298 | pCx->isTable = 1; |
| 3299 | pCx->isIndex = 0; |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3300 | break; |
| 3301 | } |
| 3302 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 3303 | /* Opcode: Close P1 * * * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3304 | ** |
| 3305 | ** Close a cursor previously opened as P1. If P1 is not |
| 3306 | ** currently open, this instruction is a no-op. |
| 3307 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3308 | case OP_Close: { |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 3309 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 3310 | sqlite3VdbeFreeCursor(p, p->apCsr[pOp->p1]); |
| 3311 | p->apCsr[pOp->p1] = 0; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3312 | break; |
| 3313 | } |
| 3314 | |
drh | 959403f | 2008-12-12 17:56:16 +0000 | [diff] [blame] | 3315 | /* Opcode: SeekGe P1 P2 P3 P4 * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3316 | ** |
danielk1977 | b790c6c | 2008-04-18 10:25:24 +0000 | [diff] [blame] | 3317 | ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys), |
drh | 959403f | 2008-12-12 17:56:16 +0000 | [diff] [blame] | 3318 | ** use the value in register P3 as the key. If cursor P1 refers |
danielk1977 | b790c6c | 2008-04-18 10:25:24 +0000 | [diff] [blame] | 3319 | ** to an SQL index, then P3 is the first in an array of P4 registers |
| 3320 | ** that are used as an unpacked index key. |
| 3321 | ** |
| 3322 | ** Reposition cursor P1 so that it points to the smallest entry that |
| 3323 | ** is greater than or equal to the key value. If there are no records |
| 3324 | ** 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] | 3325 | ** |
drh | 959403f | 2008-12-12 17:56:16 +0000 | [diff] [blame] | 3326 | ** See also: Found, NotFound, Distinct, SeekLt, SeekGt, SeekLe |
drh | 7cf6e4d | 2004-05-19 14:56:55 +0000 | [diff] [blame] | 3327 | */ |
drh | 959403f | 2008-12-12 17:56:16 +0000 | [diff] [blame] | 3328 | /* Opcode: SeekGt P1 P2 P3 P4 * |
drh | 7cf6e4d | 2004-05-19 14:56:55 +0000 | [diff] [blame] | 3329 | ** |
danielk1977 | b790c6c | 2008-04-18 10:25:24 +0000 | [diff] [blame] | 3330 | ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys), |
drh | 959403f | 2008-12-12 17:56:16 +0000 | [diff] [blame] | 3331 | ** use the value in register P3 as a key. If cursor P1 refers |
danielk1977 | b790c6c | 2008-04-18 10:25:24 +0000 | [diff] [blame] | 3332 | ** to an SQL index, then P3 is the first in an array of P4 registers |
| 3333 | ** that are used as an unpacked index key. |
| 3334 | ** |
| 3335 | ** Reposition cursor P1 so that it points to the smallest entry that |
| 3336 | ** is greater than the key value. If there are no records greater than |
| 3337 | ** the key and P2 is not zero, then jump to P2. |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 3338 | ** |
drh | 959403f | 2008-12-12 17:56:16 +0000 | [diff] [blame] | 3339 | ** See also: Found, NotFound, Distinct, SeekLt, SeekGe, SeekLe |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3340 | */ |
drh | 959403f | 2008-12-12 17:56:16 +0000 | [diff] [blame] | 3341 | /* Opcode: SeekLt P1 P2 P3 P4 * |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 3342 | ** |
danielk1977 | b790c6c | 2008-04-18 10:25:24 +0000 | [diff] [blame] | 3343 | ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys), |
drh | 959403f | 2008-12-12 17:56:16 +0000 | [diff] [blame] | 3344 | ** use the value in register P3 as a key. If cursor P1 refers |
danielk1977 | b790c6c | 2008-04-18 10:25:24 +0000 | [diff] [blame] | 3345 | ** to an SQL index, then P3 is the first in an array of P4 registers |
| 3346 | ** that are used as an unpacked index key. |
| 3347 | ** |
| 3348 | ** Reposition cursor P1 so that it points to the largest entry that |
| 3349 | ** is less than the key value. If there are no records less than |
| 3350 | ** the key and P2 is not zero, then jump to P2. |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 3351 | ** |
drh | 959403f | 2008-12-12 17:56:16 +0000 | [diff] [blame] | 3352 | ** See also: Found, NotFound, Distinct, SeekGt, SeekGe, SeekLe |
drh | 7cf6e4d | 2004-05-19 14:56:55 +0000 | [diff] [blame] | 3353 | */ |
drh | 959403f | 2008-12-12 17:56:16 +0000 | [diff] [blame] | 3354 | /* Opcode: SeekLe P1 P2 P3 P4 * |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 3355 | ** |
danielk1977 | b790c6c | 2008-04-18 10:25:24 +0000 | [diff] [blame] | 3356 | ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys), |
drh | 959403f | 2008-12-12 17:56:16 +0000 | [diff] [blame] | 3357 | ** use the value in register P3 as a key. If cursor P1 refers |
danielk1977 | b790c6c | 2008-04-18 10:25:24 +0000 | [diff] [blame] | 3358 | ** to an SQL index, then P3 is the first in an array of P4 registers |
| 3359 | ** that are used as an unpacked index key. |
danielk1977 | 751de56 | 2008-04-18 09:01:15 +0000 | [diff] [blame] | 3360 | ** |
danielk1977 | b790c6c | 2008-04-18 10:25:24 +0000 | [diff] [blame] | 3361 | ** Reposition cursor P1 so that it points to the largest entry that |
| 3362 | ** is less than or equal to the key value. If there are no records |
| 3363 | ** 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] | 3364 | ** |
drh | 959403f | 2008-12-12 17:56:16 +0000 | [diff] [blame] | 3365 | ** See also: Found, NotFound, Distinct, SeekGt, SeekGe, SeekLt |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 3366 | */ |
drh | 959403f | 2008-12-12 17:56:16 +0000 | [diff] [blame] | 3367 | case OP_SeekLt: /* jump, in3 */ |
| 3368 | case OP_SeekLe: /* jump, in3 */ |
| 3369 | case OP_SeekGe: /* jump, in3 */ |
| 3370 | case OP_SeekGt: { /* jump, in3 */ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3371 | int res; |
| 3372 | int oc; |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 3373 | VdbeCursor *pC; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3374 | UnpackedRecord r; |
| 3375 | int nField; |
| 3376 | i64 iKey; /* The rowid we are to seek to */ |
drh | 80ff32f | 2001-11-04 18:32:46 +0000 | [diff] [blame] | 3377 | |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 3378 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
drh | 959403f | 2008-12-12 17:56:16 +0000 | [diff] [blame] | 3379 | assert( pOp->p2!=0 ); |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 3380 | pC = p->apCsr[pOp->p1]; |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 3381 | assert( pC!=0 ); |
drh | 3e9ca09 | 2009-09-08 01:14:48 +0000 | [diff] [blame] | 3382 | assert( pC->pseudoTableReg==0 ); |
drh | 1f35012 | 2009-11-13 20:52:43 +0000 | [diff] [blame] | 3383 | assert( OP_SeekLe == OP_SeekLt+1 ); |
| 3384 | assert( OP_SeekGe == OP_SeekLt+2 ); |
| 3385 | assert( OP_SeekGt == OP_SeekLt+3 ); |
drh | d4187c7 | 2010-08-30 22:15:45 +0000 | [diff] [blame] | 3386 | assert( pC->isOrdered ); |
dan | a205a48 | 2011-08-27 18:48:57 +0000 | [diff] [blame] | 3387 | if( ALWAYS(pC->pCursor!=0) ){ |
drh | 7cf6e4d | 2004-05-19 14:56:55 +0000 | [diff] [blame] | 3388 | oc = pOp->opcode; |
drh | a11846b | 2004-01-07 18:52:56 +0000 | [diff] [blame] | 3389 | pC->nullRow = 0; |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3390 | if( pC->isTable ){ |
drh | 959403f | 2008-12-12 17:56:16 +0000 | [diff] [blame] | 3391 | /* The input value in P3 might be of any type: integer, real, string, |
| 3392 | ** blob, or NULL. But it needs to be an integer before we can do |
| 3393 | ** the seek, so covert it. */ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 3394 | pIn3 = &aMem[pOp->p3]; |
drh | 959403f | 2008-12-12 17:56:16 +0000 | [diff] [blame] | 3395 | applyNumericAffinity(pIn3); |
| 3396 | iKey = sqlite3VdbeIntValue(pIn3); |
| 3397 | pC->rowidIsValid = 0; |
| 3398 | |
| 3399 | /* If the P3 value could not be converted into an integer without |
| 3400 | ** loss of information, then special processing is required... */ |
| 3401 | if( (pIn3->flags & MEM_Int)==0 ){ |
| 3402 | if( (pIn3->flags & MEM_Real)==0 ){ |
| 3403 | /* If the P3 value cannot be converted into any kind of a number, |
| 3404 | ** then the seek is not possible, so jump to P2 */ |
| 3405 | pc = pOp->p2 - 1; |
| 3406 | break; |
| 3407 | } |
| 3408 | /* If we reach this point, then the P3 value must be a floating |
| 3409 | ** point number. */ |
| 3410 | assert( (pIn3->flags & MEM_Real)!=0 ); |
| 3411 | |
| 3412 | if( iKey==SMALLEST_INT64 && (pIn3->r<(double)iKey || pIn3->r>0) ){ |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 3413 | /* The P3 value is too large in magnitude to be expressed as an |
drh | 959403f | 2008-12-12 17:56:16 +0000 | [diff] [blame] | 3414 | ** integer. */ |
| 3415 | res = 1; |
| 3416 | if( pIn3->r<0 ){ |
drh | 1f35012 | 2009-11-13 20:52:43 +0000 | [diff] [blame] | 3417 | if( oc>=OP_SeekGe ){ assert( oc==OP_SeekGe || oc==OP_SeekGt ); |
drh | 959403f | 2008-12-12 17:56:16 +0000 | [diff] [blame] | 3418 | rc = sqlite3BtreeFirst(pC->pCursor, &res); |
| 3419 | if( rc!=SQLITE_OK ) goto abort_due_to_error; |
| 3420 | } |
| 3421 | }else{ |
drh | 1f35012 | 2009-11-13 20:52:43 +0000 | [diff] [blame] | 3422 | if( oc<=OP_SeekLe ){ assert( oc==OP_SeekLt || oc==OP_SeekLe ); |
drh | 959403f | 2008-12-12 17:56:16 +0000 | [diff] [blame] | 3423 | rc = sqlite3BtreeLast(pC->pCursor, &res); |
| 3424 | if( rc!=SQLITE_OK ) goto abort_due_to_error; |
| 3425 | } |
| 3426 | } |
| 3427 | if( res ){ |
| 3428 | pc = pOp->p2 - 1; |
| 3429 | } |
| 3430 | break; |
| 3431 | }else if( oc==OP_SeekLt || oc==OP_SeekGe ){ |
| 3432 | /* Use the ceiling() function to convert real->int */ |
| 3433 | if( pIn3->r > (double)iKey ) iKey++; |
| 3434 | }else{ |
| 3435 | /* Use the floor() function to convert real->int */ |
| 3436 | assert( oc==OP_SeekLe || oc==OP_SeekGt ); |
| 3437 | if( pIn3->r < (double)iKey ) iKey--; |
| 3438 | } |
| 3439 | } |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 3440 | rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, 0, (u64)iKey, 0, &res); |
danielk1977 | 2812956 | 2005-01-11 10:25:06 +0000 | [diff] [blame] | 3441 | if( rc!=SQLITE_OK ){ |
| 3442 | goto abort_due_to_error; |
| 3443 | } |
drh | 959403f | 2008-12-12 17:56:16 +0000 | [diff] [blame] | 3444 | if( res==0 ){ |
| 3445 | pC->rowidIsValid = 1; |
| 3446 | pC->lastRowid = iKey; |
| 3447 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3448 | }else{ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3449 | nField = pOp->p4.i; |
danielk1977 | b790c6c | 2008-04-18 10:25:24 +0000 | [diff] [blame] | 3450 | assert( pOp->p4type==P4_INT32 ); |
| 3451 | assert( nField>0 ); |
| 3452 | r.pKeyInfo = pC->pKeyInfo; |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 3453 | r.nField = (u16)nField; |
drh | 1f35012 | 2009-11-13 20:52:43 +0000 | [diff] [blame] | 3454 | |
| 3455 | /* The next line of code computes as follows, only faster: |
| 3456 | ** if( oc==OP_SeekGt || oc==OP_SeekLe ){ |
| 3457 | ** r.flags = UNPACKED_INCRKEY; |
| 3458 | ** }else{ |
| 3459 | ** r.flags = 0; |
| 3460 | ** } |
| 3461 | */ |
shaneh | 5e17e8b | 2009-12-03 04:40:47 +0000 | [diff] [blame] | 3462 | r.flags = (u16)(UNPACKED_INCRKEY * (1 & (oc - OP_SeekLt))); |
drh | 1f35012 | 2009-11-13 20:52:43 +0000 | [diff] [blame] | 3463 | assert( oc!=OP_SeekGt || r.flags==UNPACKED_INCRKEY ); |
| 3464 | assert( oc!=OP_SeekLe || r.flags==UNPACKED_INCRKEY ); |
| 3465 | assert( oc!=OP_SeekGe || r.flags==0 ); |
| 3466 | assert( oc!=OP_SeekLt || r.flags==0 ); |
| 3467 | |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 3468 | r.aMem = &aMem[pOp->p3]; |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 3469 | #ifdef SQLITE_DEBUG |
| 3470 | { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); } |
| 3471 | #endif |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 3472 | ExpandBlob(r.aMem); |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 3473 | rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, &r, 0, 0, &res); |
danielk1977 | 2812956 | 2005-01-11 10:25:06 +0000 | [diff] [blame] | 3474 | if( rc!=SQLITE_OK ){ |
| 3475 | goto abort_due_to_error; |
| 3476 | } |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3477 | pC->rowidIsValid = 0; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3478 | } |
drh | a11846b | 2004-01-07 18:52:56 +0000 | [diff] [blame] | 3479 | pC->deferredMoveto = 0; |
drh | 76873ab | 2006-01-07 18:48:26 +0000 | [diff] [blame] | 3480 | pC->cacheStatus = CACHE_STALE; |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 3481 | #ifdef SQLITE_TEST |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 3482 | sqlite3_search_count++; |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 3483 | #endif |
drh | 1f35012 | 2009-11-13 20:52:43 +0000 | [diff] [blame] | 3484 | if( oc>=OP_SeekGe ){ assert( oc==OP_SeekGe || oc==OP_SeekGt ); |
drh | 959403f | 2008-12-12 17:56:16 +0000 | [diff] [blame] | 3485 | if( res<0 || (res==0 && oc==OP_SeekGt) ){ |
danielk1977 | 2812956 | 2005-01-11 10:25:06 +0000 | [diff] [blame] | 3486 | rc = sqlite3BtreeNext(pC->pCursor, &res); |
danielk1977 | 01427a6 | 2005-01-11 13:02:33 +0000 | [diff] [blame] | 3487 | if( rc!=SQLITE_OK ) goto abort_due_to_error; |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3488 | pC->rowidIsValid = 0; |
drh | 1af3fdb | 2004-07-18 21:33:01 +0000 | [diff] [blame] | 3489 | }else{ |
| 3490 | res = 0; |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 3491 | } |
drh | 7cf6e4d | 2004-05-19 14:56:55 +0000 | [diff] [blame] | 3492 | }else{ |
drh | 959403f | 2008-12-12 17:56:16 +0000 | [diff] [blame] | 3493 | assert( oc==OP_SeekLt || oc==OP_SeekLe ); |
| 3494 | if( res>0 || (res==0 && oc==OP_SeekLt) ){ |
danielk1977 | 01427a6 | 2005-01-11 13:02:33 +0000 | [diff] [blame] | 3495 | rc = sqlite3BtreePrevious(pC->pCursor, &res); |
| 3496 | if( rc!=SQLITE_OK ) goto abort_due_to_error; |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3497 | pC->rowidIsValid = 0; |
drh | 1a844c3 | 2002-12-04 22:29:28 +0000 | [diff] [blame] | 3498 | }else{ |
| 3499 | /* res might be negative because the table is empty. Check to |
| 3500 | ** see if this is the case. |
| 3501 | */ |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 3502 | res = sqlite3BtreeEof(pC->pCursor); |
drh | 1a844c3 | 2002-12-04 22:29:28 +0000 | [diff] [blame] | 3503 | } |
drh | 1af3fdb | 2004-07-18 21:33:01 +0000 | [diff] [blame] | 3504 | } |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 3505 | assert( pOp->p2>0 ); |
drh | 1af3fdb | 2004-07-18 21:33:01 +0000 | [diff] [blame] | 3506 | if( res ){ |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 3507 | pc = pOp->p2 - 1; |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 3508 | } |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 3509 | }else{ |
danielk1977 | f7b9d66 | 2008-06-23 18:49:43 +0000 | [diff] [blame] | 3510 | /* This happens when attempting to open the sqlite3_master table |
| 3511 | ** for read access returns SQLITE_EMPTY. In this case always |
| 3512 | ** take the jump (since there are no records in the table). |
| 3513 | */ |
| 3514 | pc = pOp->p2 - 1; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3515 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3516 | break; |
| 3517 | } |
| 3518 | |
drh | 959403f | 2008-12-12 17:56:16 +0000 | [diff] [blame] | 3519 | /* Opcode: Seek P1 P2 * * * |
| 3520 | ** |
| 3521 | ** P1 is an open table cursor and P2 is a rowid integer. Arrange |
| 3522 | ** for P1 to move so that it points to the rowid given by P2. |
| 3523 | ** |
| 3524 | ** This is actually a deferred seek. Nothing actually happens until |
| 3525 | ** the cursor is used to read a record. That way, if no reads |
| 3526 | ** occur, no unnecessary I/O happens. |
| 3527 | */ |
| 3528 | case OP_Seek: { /* in2 */ |
drh | 959403f | 2008-12-12 17:56:16 +0000 | [diff] [blame] | 3529 | VdbeCursor *pC; |
| 3530 | |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 3531 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 3532 | pC = p->apCsr[pOp->p1]; |
drh | 959403f | 2008-12-12 17:56:16 +0000 | [diff] [blame] | 3533 | assert( pC!=0 ); |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 3534 | if( ALWAYS(pC->pCursor!=0) ){ |
drh | 959403f | 2008-12-12 17:56:16 +0000 | [diff] [blame] | 3535 | assert( pC->isTable ); |
| 3536 | pC->nullRow = 0; |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 3537 | pIn2 = &aMem[pOp->p2]; |
drh | 959403f | 2008-12-12 17:56:16 +0000 | [diff] [blame] | 3538 | pC->movetoTarget = sqlite3VdbeIntValue(pIn2); |
| 3539 | pC->rowidIsValid = 0; |
| 3540 | pC->deferredMoveto = 1; |
| 3541 | } |
| 3542 | break; |
| 3543 | } |
| 3544 | |
| 3545 | |
drh | 8cff69d | 2009-11-12 19:59:44 +0000 | [diff] [blame] | 3546 | /* Opcode: Found P1 P2 P3 P4 * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3547 | ** |
drh | 8cff69d | 2009-11-12 19:59:44 +0000 | [diff] [blame] | 3548 | ** If P4==0 then register P3 holds a blob constructed by MakeRecord. If |
| 3549 | ** P4>0 then register P3 is the first of P4 registers that form an unpacked |
| 3550 | ** record. |
| 3551 | ** |
| 3552 | ** Cursor P1 is on an index btree. If the record identified by P3 and P4 |
| 3553 | ** is a prefix of any entry in P1 then a jump is made to P2 and |
drh | e3365e6 | 2009-11-12 17:52:24 +0000 | [diff] [blame] | 3554 | ** P1 is left pointing at the matching entry. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3555 | */ |
drh | 8cff69d | 2009-11-12 19:59:44 +0000 | [diff] [blame] | 3556 | /* Opcode: NotFound P1 P2 P3 P4 * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3557 | ** |
drh | 8cff69d | 2009-11-12 19:59:44 +0000 | [diff] [blame] | 3558 | ** If P4==0 then register P3 holds a blob constructed by MakeRecord. If |
| 3559 | ** P4>0 then register P3 is the first of P4 registers that form an unpacked |
| 3560 | ** record. |
| 3561 | ** |
| 3562 | ** Cursor P1 is on an index btree. If the record identified by P3 and P4 |
| 3563 | ** is not the prefix of any entry in P1 then a jump is made to P2. If P1 |
| 3564 | ** does contain an entry whose prefix matches the P3/P4 record then control |
| 3565 | ** falls through to the next instruction and P1 is left pointing at the |
| 3566 | ** matching entry. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3567 | ** |
drh | cb6d50e | 2008-08-21 19:28:30 +0000 | [diff] [blame] | 3568 | ** See also: Found, NotExists, IsUnique |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3569 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3570 | case OP_NotFound: /* jump, in3 */ |
| 3571 | case OP_Found: { /* jump, in3 */ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3572 | int alreadyExists; |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 3573 | VdbeCursor *pC; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3574 | int res; |
dan | 03e9cfc | 2011-09-05 14:20:27 +0000 | [diff] [blame] | 3575 | char *pFree; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3576 | UnpackedRecord *pIdxKey; |
drh | 8cff69d | 2009-11-12 19:59:44 +0000 | [diff] [blame] | 3577 | UnpackedRecord r; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3578 | char aTempRec[ROUND8(sizeof(UnpackedRecord)) + sizeof(Mem)*3 + 7]; |
| 3579 | |
dan | 0ff297e | 2009-09-25 17:03:14 +0000 | [diff] [blame] | 3580 | #ifdef SQLITE_TEST |
| 3581 | sqlite3_found_count++; |
| 3582 | #endif |
| 3583 | |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3584 | alreadyExists = 0; |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 3585 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
drh | 8cff69d | 2009-11-12 19:59:44 +0000 | [diff] [blame] | 3586 | assert( pOp->p4type==P4_INT32 ); |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 3587 | pC = p->apCsr[pOp->p1]; |
| 3588 | assert( pC!=0 ); |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 3589 | pIn3 = &aMem[pOp->p3]; |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 3590 | if( ALWAYS(pC->pCursor!=0) ){ |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 3591 | |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3592 | assert( pC->isTable==0 ); |
drh | 8cff69d | 2009-11-12 19:59:44 +0000 | [diff] [blame] | 3593 | if( pOp->p4.i>0 ){ |
| 3594 | r.pKeyInfo = pC->pKeyInfo; |
shaneh | 5e17e8b | 2009-12-03 04:40:47 +0000 | [diff] [blame] | 3595 | r.nField = (u16)pOp->p4.i; |
drh | 8cff69d | 2009-11-12 19:59:44 +0000 | [diff] [blame] | 3596 | r.aMem = pIn3; |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 3597 | #ifdef SQLITE_DEBUG |
| 3598 | { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); } |
| 3599 | #endif |
drh | 8cff69d | 2009-11-12 19:59:44 +0000 | [diff] [blame] | 3600 | r.flags = UNPACKED_PREFIX_MATCH; |
| 3601 | pIdxKey = &r; |
| 3602 | }else{ |
dan | 03e9cfc | 2011-09-05 14:20:27 +0000 | [diff] [blame] | 3603 | pIdxKey = sqlite3VdbeAllocUnpackedRecord( |
| 3604 | pC->pKeyInfo, aTempRec, sizeof(aTempRec), &pFree |
| 3605 | ); |
| 3606 | if( pIdxKey==0 ) goto no_mem; |
drh | 8cff69d | 2009-11-12 19:59:44 +0000 | [diff] [blame] | 3607 | assert( pIn3->flags & MEM_Blob ); |
drh | d81a142 | 2010-09-28 07:11:24 +0000 | [diff] [blame] | 3608 | assert( (pIn3->flags & MEM_Zero)==0 ); /* zeroblobs already expanded */ |
dan | 03e9cfc | 2011-09-05 14:20:27 +0000 | [diff] [blame] | 3609 | sqlite3VdbeRecordUnpack(pC->pKeyInfo, pIn3->n, pIn3->z, pIdxKey); |
drh | 8cff69d | 2009-11-12 19:59:44 +0000 | [diff] [blame] | 3610 | pIdxKey->flags |= UNPACKED_PREFIX_MATCH; |
danielk1977 | 9a96b66 | 2007-11-29 17:05:18 +0000 | [diff] [blame] | 3611 | } |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 3612 | rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, pIdxKey, 0, 0, &res); |
drh | 8cff69d | 2009-11-12 19:59:44 +0000 | [diff] [blame] | 3613 | if( pOp->p4.i==0 ){ |
dan | 03e9cfc | 2011-09-05 14:20:27 +0000 | [diff] [blame] | 3614 | sqlite3DbFree(db, pFree); |
drh | 8cff69d | 2009-11-12 19:59:44 +0000 | [diff] [blame] | 3615 | } |
danielk1977 | 7751940 | 2007-08-30 11:48:31 +0000 | [diff] [blame] | 3616 | if( rc!=SQLITE_OK ){ |
| 3617 | break; |
| 3618 | } |
| 3619 | alreadyExists = (res==0); |
drh | a11846b | 2004-01-07 18:52:56 +0000 | [diff] [blame] | 3620 | pC->deferredMoveto = 0; |
drh | 76873ab | 2006-01-07 18:48:26 +0000 | [diff] [blame] | 3621 | pC->cacheStatus = CACHE_STALE; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3622 | } |
| 3623 | if( pOp->opcode==OP_Found ){ |
| 3624 | if( alreadyExists ) pc = pOp->p2 - 1; |
| 3625 | }else{ |
| 3626 | if( !alreadyExists ) pc = pOp->p2 - 1; |
| 3627 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3628 | break; |
| 3629 | } |
| 3630 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 3631 | /* Opcode: IsUnique P1 P2 P3 P4 * |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 3632 | ** |
drh | 8cff69d | 2009-11-12 19:59:44 +0000 | [diff] [blame] | 3633 | ** Cursor P1 is open on an index b-tree - that is to say, a btree which |
| 3634 | ** no data and where the key are records generated by OP_MakeRecord with |
| 3635 | ** the list field being the integer ROWID of the entry that the index |
| 3636 | ** entry refers to. |
danielk1977 | de63035 | 2009-05-04 11:42:29 +0000 | [diff] [blame] | 3637 | ** |
| 3638 | ** The P3 register contains an integer record number. Call this record |
| 3639 | ** number R. Register P4 is the first in a set of N contiguous registers |
| 3640 | ** that make up an unpacked index key that can be used with cursor P1. |
| 3641 | ** The value of N can be inferred from the cursor. N includes the rowid |
| 3642 | ** value appended to the end of the index record. This rowid value may |
| 3643 | ** or may not be the same as R. |
| 3644 | ** |
| 3645 | ** If any of the N registers beginning with register P4 contains a NULL |
| 3646 | ** value, jump immediately to P2. |
| 3647 | ** |
| 3648 | ** Otherwise, this instruction checks if cursor P1 contains an entry |
| 3649 | ** where the first (N-1) fields match but the rowid value at the end |
| 3650 | ** of the index entry is not R. If there is no such entry, control jumps |
| 3651 | ** to instruction P2. Otherwise, the rowid of the conflicting index |
| 3652 | ** entry is copied to register P3 and control falls through to the next |
| 3653 | ** instruction. |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 3654 | ** |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3655 | ** See also: NotFound, NotExists, Found |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 3656 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3657 | case OP_IsUnique: { /* jump, in3 */ |
shane | 60a4b53 | 2009-05-06 18:57:09 +0000 | [diff] [blame] | 3658 | u16 ii; |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 3659 | VdbeCursor *pCx; |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 3660 | BtCursor *pCrsr; |
shane | 60a4b53 | 2009-05-06 18:57:09 +0000 | [diff] [blame] | 3661 | u16 nField; |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 3662 | Mem *aMx; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3663 | UnpackedRecord r; /* B-Tree index search key */ |
| 3664 | i64 R; /* Rowid stored in register P3 */ |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 3665 | |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 3666 | pIn3 = &aMem[pOp->p3]; |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 3667 | aMx = &aMem[pOp->p4.i]; |
danielk1977 | de63035 | 2009-05-04 11:42:29 +0000 | [diff] [blame] | 3668 | /* Assert that the values of parameters P1 and P4 are in range. */ |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 3669 | assert( pOp->p4type==P4_INT32 ); |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3670 | assert( pOp->p4.i>0 && pOp->p4.i<=p->nMem ); |
danielk1977 | de63035 | 2009-05-04 11:42:29 +0000 | [diff] [blame] | 3671 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 3672 | |
| 3673 | /* Find the index cursor. */ |
| 3674 | pCx = p->apCsr[pOp->p1]; |
| 3675 | assert( pCx->deferredMoveto==0 ); |
| 3676 | pCx->seekResult = 0; |
| 3677 | pCx->cacheStatus = CACHE_STALE; |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 3678 | pCrsr = pCx->pCursor; |
danielk1977 | de63035 | 2009-05-04 11:42:29 +0000 | [diff] [blame] | 3679 | |
| 3680 | /* If any of the values are NULL, take the jump. */ |
| 3681 | nField = pCx->pKeyInfo->nField; |
| 3682 | for(ii=0; ii<nField; ii++){ |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 3683 | if( aMx[ii].flags & MEM_Null ){ |
danielk1977 | de63035 | 2009-05-04 11:42:29 +0000 | [diff] [blame] | 3684 | pc = pOp->p2 - 1; |
| 3685 | pCrsr = 0; |
| 3686 | break; |
| 3687 | } |
| 3688 | } |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 3689 | assert( (aMx[nField].flags & MEM_Null)==0 ); |
danielk1977 | de63035 | 2009-05-04 11:42:29 +0000 | [diff] [blame] | 3690 | |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 3691 | if( pCrsr!=0 ){ |
danielk1977 | de63035 | 2009-05-04 11:42:29 +0000 | [diff] [blame] | 3692 | /* Populate the index search key. */ |
| 3693 | r.pKeyInfo = pCx->pKeyInfo; |
| 3694 | r.nField = nField + 1; |
| 3695 | r.flags = UNPACKED_PREFIX_SEARCH; |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 3696 | r.aMem = aMx; |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 3697 | #ifdef SQLITE_DEBUG |
| 3698 | { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); } |
| 3699 | #endif |
danielk1977 | 452c989 | 2004-05-13 05:16:15 +0000 | [diff] [blame] | 3700 | |
danielk1977 | de63035 | 2009-05-04 11:42:29 +0000 | [diff] [blame] | 3701 | /* Extract the value of R from register P3. */ |
| 3702 | sqlite3VdbeMemIntegerify(pIn3); |
| 3703 | R = pIn3->u.i; |
| 3704 | |
| 3705 | /* Search the B-Tree index. If no conflicting record is found, jump |
| 3706 | ** to P2. Otherwise, copy the rowid of the conflicting record to |
| 3707 | ** register P3 and fall through to the next instruction. */ |
| 3708 | rc = sqlite3BtreeMovetoUnpacked(pCrsr, &r, 0, 0, &pCx->seekResult); |
| 3709 | if( (r.flags & UNPACKED_PREFIX_SEARCH) || r.rowid==R ){ |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 3710 | pc = pOp->p2 - 1; |
danielk1977 | de63035 | 2009-05-04 11:42:29 +0000 | [diff] [blame] | 3711 | }else{ |
| 3712 | pIn3->u.i = r.rowid; |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 3713 | } |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 3714 | } |
| 3715 | break; |
| 3716 | } |
| 3717 | |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3718 | /* Opcode: NotExists P1 P2 P3 * * |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 3719 | ** |
drh | ef8662b | 2011-06-20 21:47:58 +0000 | [diff] [blame] | 3720 | ** Use the content of register P3 as an integer key. If a record |
danielk1977 | 96cb76f | 2008-01-04 13:24:28 +0000 | [diff] [blame] | 3721 | ** with that key does not exist in table of P1, then jump to P2. |
drh | 710c484 | 2010-08-30 01:17:20 +0000 | [diff] [blame] | 3722 | ** If the record does exist, then fall through. The cursor is left |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3723 | ** pointing to the record if it exists. |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 3724 | ** |
| 3725 | ** The difference between this operation and NotFound is that this |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3726 | ** operation assumes the key is an integer and that P1 is a table whereas |
| 3727 | ** NotFound assumes key is a blob constructed from MakeRecord and |
| 3728 | ** P1 is an index. |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 3729 | ** |
drh | cb6d50e | 2008-08-21 19:28:30 +0000 | [diff] [blame] | 3730 | ** See also: Found, NotFound, IsUnique |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 3731 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3732 | case OP_NotExists: { /* jump, in3 */ |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 3733 | VdbeCursor *pC; |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 3734 | BtCursor *pCrsr; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3735 | int res; |
| 3736 | u64 iKey; |
| 3737 | |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 3738 | pIn3 = &aMem[pOp->p3]; |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 3739 | assert( pIn3->flags & MEM_Int ); |
| 3740 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 3741 | pC = p->apCsr[pOp->p1]; |
| 3742 | assert( pC!=0 ); |
| 3743 | assert( pC->isTable ); |
drh | 3e9ca09 | 2009-09-08 01:14:48 +0000 | [diff] [blame] | 3744 | assert( pC->pseudoTableReg==0 ); |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 3745 | pCrsr = pC->pCursor; |
dan | a205a48 | 2011-08-27 18:48:57 +0000 | [diff] [blame] | 3746 | if( ALWAYS(pCrsr!=0) ){ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3747 | res = 0; |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 3748 | iKey = pIn3->u.i; |
danielk1977 | de63035 | 2009-05-04 11:42:29 +0000 | [diff] [blame] | 3749 | rc = sqlite3BtreeMovetoUnpacked(pCrsr, 0, iKey, 0, &res); |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 3750 | pC->lastRowid = pIn3->u.i; |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 3751 | pC->rowidIsValid = res==0 ?1:0; |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 3752 | pC->nullRow = 0; |
drh | 76873ab | 2006-01-07 18:48:26 +0000 | [diff] [blame] | 3753 | pC->cacheStatus = CACHE_STALE; |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3754 | pC->deferredMoveto = 0; |
danielk1977 | 2812956 | 2005-01-11 10:25:06 +0000 | [diff] [blame] | 3755 | if( res!=0 ){ |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 3756 | pc = pOp->p2 - 1; |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 3757 | assert( pC->rowidIsValid==0 ); |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 3758 | } |
danielk1977 | de63035 | 2009-05-04 11:42:29 +0000 | [diff] [blame] | 3759 | pC->seekResult = res; |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 3760 | }else{ |
danielk1977 | f7b9d66 | 2008-06-23 18:49:43 +0000 | [diff] [blame] | 3761 | /* This happens when an attempt to open a read cursor on the |
| 3762 | ** sqlite_master table returns SQLITE_EMPTY. |
| 3763 | */ |
danielk1977 | f7b9d66 | 2008-06-23 18:49:43 +0000 | [diff] [blame] | 3764 | pc = pOp->p2 - 1; |
| 3765 | assert( pC->rowidIsValid==0 ); |
danielk1977 | de63035 | 2009-05-04 11:42:29 +0000 | [diff] [blame] | 3766 | pC->seekResult = 0; |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 3767 | } |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 3768 | break; |
| 3769 | } |
| 3770 | |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 3771 | /* Opcode: Sequence P1 P2 * * * |
drh | 4db38a7 | 2005-09-01 12:16:28 +0000 | [diff] [blame] | 3772 | ** |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 3773 | ** Find the next available sequence number for cursor P1. |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3774 | ** Write the sequence number into register P2. |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 3775 | ** The sequence number on the cursor is incremented after this |
| 3776 | ** instruction. |
drh | 4db38a7 | 2005-09-01 12:16:28 +0000 | [diff] [blame] | 3777 | */ |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 3778 | case OP_Sequence: { /* out2-prerelease */ |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 3779 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 3780 | assert( p->apCsr[pOp->p1]!=0 ); |
| 3781 | pOut->u.i = p->apCsr[pOp->p1]->seqCount++; |
drh | 4db38a7 | 2005-09-01 12:16:28 +0000 | [diff] [blame] | 3782 | break; |
| 3783 | } |
| 3784 | |
| 3785 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 3786 | /* Opcode: NewRowid P1 P2 P3 * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3787 | ** |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3788 | ** 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] | 3789 | ** The record number is not previously used as a key in the database |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3790 | ** table that cursor P1 points to. The new record number is written |
| 3791 | ** written to register P2. |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 3792 | ** |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 3793 | ** If P3>0 then P3 is a register in the root frame of this VDBE that holds |
| 3794 | ** the largest previously generated record number. No new record numbers are |
| 3795 | ** allowed to be less than this value. When this value reaches its maximum, |
drh | ef8662b | 2011-06-20 21:47:58 +0000 | [diff] [blame] | 3796 | ** an SQLITE_FULL error is generated. The P3 register is updated with the ' |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 3797 | ** generated record number. This P3 mechanism is used to help implement the |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 3798 | ** AUTOINCREMENT feature. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3799 | */ |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 3800 | case OP_NewRowid: { /* out2-prerelease */ |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 3801 | i64 v; /* The new rowid */ |
| 3802 | VdbeCursor *pC; /* Cursor of table to get the new rowid */ |
| 3803 | int res; /* Result of an sqlite3BtreeLast() */ |
| 3804 | int cnt; /* Counter to limit the number of searches */ |
| 3805 | Mem *pMem; /* Register holding largest rowid for AUTOINCREMENT */ |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 3806 | VdbeFrame *pFrame; /* Root frame of VDBE */ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3807 | |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3808 | v = 0; |
| 3809 | res = 0; |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 3810 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 3811 | pC = p->apCsr[pOp->p1]; |
| 3812 | assert( pC!=0 ); |
| 3813 | if( NEVER(pC->pCursor==0) ){ |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 3814 | /* The zero initialization above is all that is needed */ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3815 | }else{ |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 3816 | /* The next rowid or record number (different terms for the same |
| 3817 | ** thing) is obtained in a two-step algorithm. |
| 3818 | ** |
| 3819 | ** First we attempt to find the largest existing rowid and add one |
| 3820 | ** to that. But if the largest existing rowid is already the maximum |
| 3821 | ** positive integer, we have to fall through to the second |
| 3822 | ** probabilistic algorithm |
| 3823 | ** |
| 3824 | ** The second algorithm is to select a rowid at random and see if |
| 3825 | ** it already exists in the table. If it does not exist, we have |
| 3826 | ** succeeded. If the random rowid does exist, we select a new one |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 3827 | ** and try again, up to 100 times. |
drh | db5ed6d | 2001-09-18 22:17:44 +0000 | [diff] [blame] | 3828 | */ |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 3829 | assert( pC->isTable ); |
drh | fe2093d | 2005-01-20 22:48:47 +0000 | [diff] [blame] | 3830 | |
drh | 75f86a4 | 2005-02-17 00:03:06 +0000 | [diff] [blame] | 3831 | #ifdef SQLITE_32BIT_ROWID |
| 3832 | # define MAX_ROWID 0x7fffffff |
| 3833 | #else |
drh | fe2093d | 2005-01-20 22:48:47 +0000 | [diff] [blame] | 3834 | /* Some compilers complain about constants of the form 0x7fffffffffffffff. |
| 3835 | ** Others complain about 0x7ffffffffffffffffLL. The following macro seems |
| 3836 | ** to provide the constant while making all compilers happy. |
| 3837 | */ |
danielk1977 | 64202cf | 2008-11-17 15:31:47 +0000 | [diff] [blame] | 3838 | # define MAX_ROWID (i64)( (((u64)0x7fffffff)<<32) | (u64)0xffffffff ) |
drh | 75f86a4 | 2005-02-17 00:03:06 +0000 | [diff] [blame] | 3839 | #endif |
drh | fe2093d | 2005-01-20 22:48:47 +0000 | [diff] [blame] | 3840 | |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 3841 | if( !pC->useRandomRowid ){ |
drh | 7f75122 | 2009-03-17 22:33:00 +0000 | [diff] [blame] | 3842 | v = sqlite3BtreeGetCachedRowid(pC->pCursor); |
| 3843 | if( v==0 ){ |
danielk1977 | 261919c | 2005-12-06 12:52:59 +0000 | [diff] [blame] | 3844 | rc = sqlite3BtreeLast(pC->pCursor, &res); |
| 3845 | if( rc!=SQLITE_OK ){ |
| 3846 | goto abort_due_to_error; |
| 3847 | } |
drh | 32fbe34 | 2002-10-19 20:16:37 +0000 | [diff] [blame] | 3848 | if( res ){ |
drh | c79c761 | 2010-01-01 18:57:48 +0000 | [diff] [blame] | 3849 | v = 1; /* IMP: R-61914-48074 */ |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 3850 | }else{ |
drh | ea8ffdf | 2009-07-22 00:35:23 +0000 | [diff] [blame] | 3851 | assert( sqlite3BtreeCursorIsValid(pC->pCursor) ); |
drh | c27ae61 | 2009-07-14 18:35:44 +0000 | [diff] [blame] | 3852 | rc = sqlite3BtreeKeySize(pC->pCursor, &v); |
| 3853 | assert( rc==SQLITE_OK ); /* Cannot fail following BtreeLast() */ |
drh | 75f86a4 | 2005-02-17 00:03:06 +0000 | [diff] [blame] | 3854 | if( v==MAX_ROWID ){ |
drh | 32fbe34 | 2002-10-19 20:16:37 +0000 | [diff] [blame] | 3855 | pC->useRandomRowid = 1; |
| 3856 | }else{ |
drh | c79c761 | 2010-01-01 18:57:48 +0000 | [diff] [blame] | 3857 | v++; /* IMP: R-29538-34987 */ |
drh | 32fbe34 | 2002-10-19 20:16:37 +0000 | [diff] [blame] | 3858 | } |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 3859 | } |
drh | 3fc190c | 2001-09-14 03:24:23 +0000 | [diff] [blame] | 3860 | } |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 3861 | |
| 3862 | #ifndef SQLITE_OMIT_AUTOINCREMENT |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 3863 | if( pOp->p3 ){ |
shane | abc6b89 | 2009-09-10 19:09:03 +0000 | [diff] [blame] | 3864 | /* Assert that P3 is a valid memory cell. */ |
| 3865 | assert( pOp->p3>0 ); |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 3866 | if( p->pFrame ){ |
| 3867 | for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent); |
shane | abc6b89 | 2009-09-10 19:09:03 +0000 | [diff] [blame] | 3868 | /* Assert that P3 is a valid memory cell. */ |
| 3869 | assert( pOp->p3<=pFrame->nMem ); |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 3870 | pMem = &pFrame->aMem[pOp->p3]; |
| 3871 | }else{ |
shane | abc6b89 | 2009-09-10 19:09:03 +0000 | [diff] [blame] | 3872 | /* Assert that P3 is a valid memory cell. */ |
| 3873 | assert( pOp->p3<=p->nMem ); |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 3874 | pMem = &aMem[pOp->p3]; |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 3875 | memAboutToChange(p, pMem); |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 3876 | } |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 3877 | assert( memIsValid(pMem) ); |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 3878 | |
| 3879 | REGISTER_TRACE(pOp->p3, pMem); |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 3880 | sqlite3VdbeMemIntegerify(pMem); |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 3881 | assert( (pMem->flags & MEM_Int)!=0 ); /* mem(P3) holds an integer */ |
drh | 3c024d6 | 2007-03-30 11:23:45 +0000 | [diff] [blame] | 3882 | if( pMem->u.i==MAX_ROWID || pC->useRandomRowid ){ |
drh | c79c761 | 2010-01-01 18:57:48 +0000 | [diff] [blame] | 3883 | rc = SQLITE_FULL; /* IMP: R-12275-61338 */ |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 3884 | goto abort_due_to_error; |
| 3885 | } |
drh | 3c024d6 | 2007-03-30 11:23:45 +0000 | [diff] [blame] | 3886 | if( v<pMem->u.i+1 ){ |
| 3887 | v = pMem->u.i + 1; |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 3888 | } |
drh | 3c024d6 | 2007-03-30 11:23:45 +0000 | [diff] [blame] | 3889 | pMem->u.i = v; |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 3890 | } |
| 3891 | #endif |
| 3892 | |
drh | 7f75122 | 2009-03-17 22:33:00 +0000 | [diff] [blame] | 3893 | sqlite3BtreeSetCachedRowid(pC->pCursor, v<MAX_ROWID ? v+1 : 0); |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 3894 | } |
| 3895 | if( pC->useRandomRowid ){ |
drh | 748a52c | 2010-09-01 11:50:08 +0000 | [diff] [blame] | 3896 | /* IMPLEMENTATION-OF: R-07677-41881 If the largest ROWID is equal to the |
drh | c79c761 | 2010-01-01 18:57:48 +0000 | [diff] [blame] | 3897 | ** largest possible integer (9223372036854775807) then the database |
drh | 748a52c | 2010-09-01 11:50:08 +0000 | [diff] [blame] | 3898 | ** engine starts picking positive candidate ROWIDs at random until |
| 3899 | ** it finds one that is not previously used. */ |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 3900 | assert( pOp->p3==0 ); /* We cannot be in random rowid mode if this is |
| 3901 | ** an AUTOINCREMENT table. */ |
shaneh | c4d340a | 2010-09-01 02:37:56 +0000 | [diff] [blame] | 3902 | /* on the first attempt, simply do one more than previous */ |
drh | 99a6692 | 2011-05-13 18:51:42 +0000 | [diff] [blame] | 3903 | v = lastRowid; |
shaneh | c4d340a | 2010-09-01 02:37:56 +0000 | [diff] [blame] | 3904 | v &= (MAX_ROWID>>1); /* ensure doesn't go negative */ |
| 3905 | v++; /* ensure non-zero */ |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 3906 | cnt = 0; |
drh | 748a52c | 2010-09-01 11:50:08 +0000 | [diff] [blame] | 3907 | while( ((rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, 0, (u64)v, |
| 3908 | 0, &res))==SQLITE_OK) |
shaneh | c4d340a | 2010-09-01 02:37:56 +0000 | [diff] [blame] | 3909 | && (res==0) |
| 3910 | && (++cnt<100)){ |
| 3911 | /* collision - try another random rowid */ |
| 3912 | sqlite3_randomness(sizeof(v), &v); |
| 3913 | if( cnt<5 ){ |
| 3914 | /* try "small" random rowids for the initial attempts */ |
| 3915 | v &= 0xffffff; |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 3916 | }else{ |
shaneh | c4d340a | 2010-09-01 02:37:56 +0000 | [diff] [blame] | 3917 | v &= (MAX_ROWID>>1); /* ensure doesn't go negative */ |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 3918 | } |
shaneh | c4d340a | 2010-09-01 02:37:56 +0000 | [diff] [blame] | 3919 | v++; /* ensure non-zero */ |
| 3920 | } |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 3921 | if( rc==SQLITE_OK && res==0 ){ |
drh | c79c761 | 2010-01-01 18:57:48 +0000 | [diff] [blame] | 3922 | rc = SQLITE_FULL; /* IMP: R-38219-53002 */ |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 3923 | goto abort_due_to_error; |
| 3924 | } |
drh | 748a52c | 2010-09-01 11:50:08 +0000 | [diff] [blame] | 3925 | assert( v>0 ); /* EV: R-40812-03570 */ |
drh | 1eaa269 | 2001-09-18 02:02:23 +0000 | [diff] [blame] | 3926 | } |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3927 | pC->rowidIsValid = 0; |
drh | a11846b | 2004-01-07 18:52:56 +0000 | [diff] [blame] | 3928 | pC->deferredMoveto = 0; |
drh | 76873ab | 2006-01-07 18:48:26 +0000 | [diff] [blame] | 3929 | pC->cacheStatus = CACHE_STALE; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3930 | } |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 3931 | pOut->u.i = v; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3932 | break; |
| 3933 | } |
| 3934 | |
danielk1977 | 1f4aa33 | 2008-01-03 09:51:55 +0000 | [diff] [blame] | 3935 | /* Opcode: Insert P1 P2 P3 P4 P5 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3936 | ** |
jplyon | 5a56422 | 2003-06-02 06:15:58 +0000 | [diff] [blame] | 3937 | ** Write an entry into the table of cursor P1. A new entry is |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 3938 | ** created if it doesn't already exist or the data for an existing |
drh | 3e9ca09 | 2009-09-08 01:14:48 +0000 | [diff] [blame] | 3939 | ** entry is overwritten. The data is the value MEM_Blob stored in register |
danielk1977 | 1f4aa33 | 2008-01-03 09:51:55 +0000 | [diff] [blame] | 3940 | ** number P2. The key is stored in register P3. The key must |
drh | 3e9ca09 | 2009-09-08 01:14:48 +0000 | [diff] [blame] | 3941 | ** be a MEM_Int. |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 3942 | ** |
danielk1977 | 1f4aa33 | 2008-01-03 09:51:55 +0000 | [diff] [blame] | 3943 | ** If the OPFLAG_NCHANGE flag of P5 is set, then the row change count is |
| 3944 | ** incremented (otherwise not). If the OPFLAG_LASTROWID flag of P5 is set, |
danielk1977 | b28af71 | 2004-06-21 06:50:26 +0000 | [diff] [blame] | 3945 | ** then rowid is stored for subsequent return by the |
drh | 85b623f | 2007-12-13 21:54:09 +0000 | [diff] [blame] | 3946 | ** sqlite3_last_insert_rowid() function (otherwise it is unmodified). |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 3947 | ** |
drh | 3e9ca09 | 2009-09-08 01:14:48 +0000 | [diff] [blame] | 3948 | ** If the OPFLAG_USESEEKRESULT flag of P5 is set and if the result of |
| 3949 | ** the last seek operation (OP_NotExists) was a success, then this |
| 3950 | ** operation will not attempt to find the appropriate row before doing |
| 3951 | ** the insert but will instead overwrite the row that the cursor is |
| 3952 | ** currently pointing to. Presumably, the prior OP_NotExists opcode |
| 3953 | ** has already positioned the cursor correctly. This is an optimization |
| 3954 | ** that boosts performance by avoiding redundant seeks. |
| 3955 | ** |
| 3956 | ** If the OPFLAG_ISUPDATE flag is set, then this opcode is part of an |
| 3957 | ** UPDATE operation. Otherwise (if the flag is clear) then this opcode |
| 3958 | ** is part of an INSERT operation. The difference is only important to |
| 3959 | ** the update hook. |
| 3960 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 3961 | ** Parameter P4 may point to a string containing the table-name, or |
danielk1977 | 1f6eec5 | 2006-06-16 06:17:47 +0000 | [diff] [blame] | 3962 | ** may be NULL. If it is not NULL, then the update-hook |
| 3963 | ** (sqlite3.xUpdateCallback) is invoked following a successful insert. |
| 3964 | ** |
drh | 93aed5a | 2008-01-16 17:46:38 +0000 | [diff] [blame] | 3965 | ** (WARNING/TODO: If P1 is a pseudo-cursor and P2 is dynamically |
| 3966 | ** allocated, then ownership of P2 is transferred to the pseudo-cursor |
| 3967 | ** and register P2 becomes ephemeral. If the cursor is changed, the |
| 3968 | ** value of register P2 will then change. Make sure this does not |
| 3969 | ** cause any problems.) |
| 3970 | ** |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3971 | ** This instruction only works on tables. The equivalent instruction |
| 3972 | ** for indices is OP_IdxInsert. |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 3973 | */ |
drh | e05c929 | 2009-10-29 13:48:10 +0000 | [diff] [blame] | 3974 | /* Opcode: InsertInt P1 P2 P3 P4 P5 |
| 3975 | ** |
| 3976 | ** This works exactly like OP_Insert except that the key is the |
| 3977 | ** integer value P3, not the value of the integer stored in register P3. |
| 3978 | */ |
| 3979 | case OP_Insert: |
| 3980 | case OP_InsertInt: { |
drh | 3e9ca09 | 2009-09-08 01:14:48 +0000 | [diff] [blame] | 3981 | Mem *pData; /* MEM cell holding data for the record to be inserted */ |
| 3982 | Mem *pKey; /* MEM cell holding key for the record */ |
| 3983 | i64 iKey; /* The integer ROWID or key for the record to be inserted */ |
| 3984 | VdbeCursor *pC; /* Cursor to table into which insert is written */ |
| 3985 | int nZero; /* Number of zero-bytes to append */ |
| 3986 | int seekResult; /* Result of prior seek or 0 if no USESEEKRESULT flag */ |
| 3987 | const char *zDb; /* database name - used by the update hook */ |
| 3988 | const char *zTbl; /* Table name - used by the opdate hook */ |
| 3989 | int op; /* Opcode for update hook: SQLITE_UPDATE or SQLITE_INSERT */ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3990 | |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 3991 | pData = &aMem[pOp->p2]; |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 3992 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 3993 | assert( memIsValid(pData) ); |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 3994 | pC = p->apCsr[pOp->p1]; |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 3995 | assert( pC!=0 ); |
drh | 3e9ca09 | 2009-09-08 01:14:48 +0000 | [diff] [blame] | 3996 | assert( pC->pCursor!=0 ); |
| 3997 | assert( pC->pseudoTableReg==0 ); |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 3998 | assert( pC->isTable ); |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 3999 | REGISTER_TRACE(pOp->p2, pData); |
danielk1977 | 5f8d8a8 | 2004-05-11 00:28:42 +0000 | [diff] [blame] | 4000 | |
drh | e05c929 | 2009-10-29 13:48:10 +0000 | [diff] [blame] | 4001 | if( pOp->opcode==OP_Insert ){ |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 4002 | pKey = &aMem[pOp->p3]; |
drh | e05c929 | 2009-10-29 13:48:10 +0000 | [diff] [blame] | 4003 | assert( pKey->flags & MEM_Int ); |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 4004 | assert( memIsValid(pKey) ); |
drh | e05c929 | 2009-10-29 13:48:10 +0000 | [diff] [blame] | 4005 | REGISTER_TRACE(pOp->p3, pKey); |
| 4006 | iKey = pKey->u.i; |
| 4007 | }else{ |
| 4008 | assert( pOp->opcode==OP_InsertInt ); |
| 4009 | iKey = pOp->p3; |
| 4010 | } |
| 4011 | |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 4012 | if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++; |
drh | 99a6692 | 2011-05-13 18:51:42 +0000 | [diff] [blame] | 4013 | if( pOp->p5 & OPFLAG_LASTROWID ) db->lastRowid = lastRowid = iKey; |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 4014 | if( pData->flags & MEM_Null ){ |
| 4015 | pData->z = 0; |
| 4016 | pData->n = 0; |
| 4017 | }else{ |
| 4018 | assert( pData->flags & (MEM_Blob|MEM_Str) ); |
| 4019 | } |
drh | 3e9ca09 | 2009-09-08 01:14:48 +0000 | [diff] [blame] | 4020 | seekResult = ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0); |
| 4021 | if( pData->flags & MEM_Zero ){ |
| 4022 | nZero = pData->u.nZero; |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 4023 | }else{ |
drh | 3e9ca09 | 2009-09-08 01:14:48 +0000 | [diff] [blame] | 4024 | nZero = 0; |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 4025 | } |
drh | 3e9ca09 | 2009-09-08 01:14:48 +0000 | [diff] [blame] | 4026 | sqlite3BtreeSetCachedRowid(pC->pCursor, 0); |
| 4027 | rc = sqlite3BtreeInsert(pC->pCursor, 0, iKey, |
| 4028 | pData->z, pData->n, nZero, |
| 4029 | pOp->p5 & OPFLAG_APPEND, seekResult |
| 4030 | ); |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 4031 | pC->rowidIsValid = 0; |
| 4032 | pC->deferredMoveto = 0; |
| 4033 | pC->cacheStatus = CACHE_STALE; |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 +0000 | [diff] [blame] | 4034 | |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 4035 | /* Invoke the update-hook if required. */ |
| 4036 | if( rc==SQLITE_OK && db->xUpdateCallback && pOp->p4.z ){ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 4037 | zDb = db->aDb[pC->iDb].zName; |
| 4038 | zTbl = pOp->p4.z; |
| 4039 | op = ((pOp->p5 & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_INSERT); |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 4040 | assert( pC->isTable ); |
| 4041 | db->xUpdateCallback(db->pUpdateArg, op, zDb, zTbl, iKey); |
| 4042 | assert( pC->iDb>=0 ); |
| 4043 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4044 | break; |
| 4045 | } |
| 4046 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4047 | /* Opcode: Delete P1 P2 * P4 * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4048 | ** |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 4049 | ** Delete the record at which the P1 cursor is currently pointing. |
| 4050 | ** |
| 4051 | ** The cursor will be left pointing at either the next or the previous |
| 4052 | ** 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] | 4053 | ** the next Next instruction will be a no-op. Hence it is OK to delete |
| 4054 | ** a record from within an Next loop. |
drh | c8d30ac | 2002-04-12 10:08:59 +0000 | [diff] [blame] | 4055 | ** |
rdc | b0c374f | 2004-02-20 22:53:38 +0000 | [diff] [blame] | 4056 | ** 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] | 4057 | ** incremented (otherwise not). |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 4058 | ** |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 4059 | ** P1 must not be pseudo-table. It has to be a real table with |
| 4060 | ** multiple rows. |
| 4061 | ** |
| 4062 | ** If P4 is not NULL, then it is the name of the table that P1 is |
| 4063 | ** pointing to. The update hook will be invoked, if it exists. |
| 4064 | ** If P4 is not NULL then the P1 cursor must have been positioned |
| 4065 | ** using OP_NotFound prior to invoking this opcode. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4066 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4067 | case OP_Delete: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 4068 | i64 iKey; |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 4069 | VdbeCursor *pC; |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 4070 | |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 4071 | iKey = 0; |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 4072 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 4073 | pC = p->apCsr[pOp->p1]; |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 4074 | assert( pC!=0 ); |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 4075 | assert( pC->pCursor!=0 ); /* Only valid for real tables, no pseudotables */ |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 +0000 | [diff] [blame] | 4076 | |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 4077 | /* If the update-hook will be invoked, set iKey to the rowid of the |
| 4078 | ** row being deleted. |
| 4079 | */ |
| 4080 | if( db->xUpdateCallback && pOp->p4.z ){ |
| 4081 | assert( pC->isTable ); |
| 4082 | assert( pC->rowidIsValid ); /* lastRowid set by previous OP_NotFound */ |
| 4083 | iKey = pC->lastRowid; |
| 4084 | } |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 +0000 | [diff] [blame] | 4085 | |
drh | 9a65f2c | 2009-06-22 19:05:40 +0000 | [diff] [blame] | 4086 | /* The OP_Delete opcode always follows an OP_NotExists or OP_Last or |
| 4087 | ** OP_Column on the same table without any intervening operations that |
| 4088 | ** might move or invalidate the cursor. Hence cursor pC is always pointing |
| 4089 | ** to the row to be deleted and the sqlite3VdbeCursorMoveto() operation |
| 4090 | ** below is always a no-op and cannot fail. We will run it anyhow, though, |
| 4091 | ** to guard against future changes to the code generator. |
| 4092 | **/ |
| 4093 | assert( pC->deferredMoveto==0 ); |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 4094 | rc = sqlite3VdbeCursorMoveto(pC); |
drh | 9a65f2c | 2009-06-22 19:05:40 +0000 | [diff] [blame] | 4095 | if( NEVER(rc!=SQLITE_OK) ) goto abort_due_to_error; |
| 4096 | |
drh | 7f75122 | 2009-03-17 22:33:00 +0000 | [diff] [blame] | 4097 | sqlite3BtreeSetCachedRowid(pC->pCursor, 0); |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 4098 | rc = sqlite3BtreeDelete(pC->pCursor); |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 4099 | pC->cacheStatus = CACHE_STALE; |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 +0000 | [diff] [blame] | 4100 | |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 4101 | /* Invoke the update-hook if required. */ |
| 4102 | if( rc==SQLITE_OK && db->xUpdateCallback && pOp->p4.z ){ |
| 4103 | const char *zDb = db->aDb[pC->iDb].zName; |
| 4104 | const char *zTbl = pOp->p4.z; |
| 4105 | db->xUpdateCallback(db->pUpdateArg, SQLITE_DELETE, zDb, zTbl, iKey); |
| 4106 | assert( pC->iDb>=0 ); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4107 | } |
danielk1977 | b28af71 | 2004-06-21 06:50:26 +0000 | [diff] [blame] | 4108 | if( pOp->p2 & OPFLAG_NCHANGE ) p->nChange++; |
rdc | b0c374f | 2004-02-20 22:53:38 +0000 | [diff] [blame] | 4109 | break; |
| 4110 | } |
drh | b7f1d9a | 2009-09-08 02:27:58 +0000 | [diff] [blame] | 4111 | /* Opcode: ResetCount * * * * * |
rdc | b0c374f | 2004-02-20 22:53:38 +0000 | [diff] [blame] | 4112 | ** |
drh | b7f1d9a | 2009-09-08 02:27:58 +0000 | [diff] [blame] | 4113 | ** The value of the change counter is copied to the database handle |
| 4114 | ** change counter (returned by subsequent calls to sqlite3_changes()). |
| 4115 | ** Then the VMs internal change counter resets to 0. |
| 4116 | ** This is used by trigger programs. |
rdc | b0c374f | 2004-02-20 22:53:38 +0000 | [diff] [blame] | 4117 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4118 | case OP_ResetCount: { |
drh | b7f1d9a | 2009-09-08 02:27:58 +0000 | [diff] [blame] | 4119 | sqlite3VdbeSetChanges(db, p->nChange); |
danielk1977 | b28af71 | 2004-06-21 06:50:26 +0000 | [diff] [blame] | 4120 | p->nChange = 0; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4121 | break; |
| 4122 | } |
| 4123 | |
dan | 5134d13 | 2011-09-02 10:31:11 +0000 | [diff] [blame] | 4124 | /* Opcode: SorterCompare P1 P2 P3 |
| 4125 | ** |
| 4126 | ** P1 is a sorter cursor. This instruction compares the record blob in |
| 4127 | ** register P3 with the entry that the sorter cursor currently points to. |
| 4128 | ** If, excluding the rowid fields at the end, the two records are a match, |
| 4129 | ** fall through to the next instruction. Otherwise, jump to instruction P2. |
| 4130 | */ |
| 4131 | case OP_SorterCompare: { |
| 4132 | VdbeCursor *pC; |
| 4133 | int res; |
| 4134 | |
| 4135 | pC = p->apCsr[pOp->p1]; |
| 4136 | assert( isSorter(pC) ); |
| 4137 | pIn3 = &aMem[pOp->p3]; |
| 4138 | rc = sqlite3VdbeSorterCompare(pC, pIn3, &res); |
| 4139 | if( res ){ |
| 4140 | pc = pOp->p2-1; |
| 4141 | } |
| 4142 | break; |
| 4143 | }; |
| 4144 | |
| 4145 | /* Opcode: SorterData P1 P2 * * * |
| 4146 | ** |
| 4147 | ** Write into register P2 the current sorter data for sorter cursor P1. |
| 4148 | */ |
| 4149 | case OP_SorterData: { |
| 4150 | VdbeCursor *pC; |
drh | ca892a7 | 2011-09-03 00:17:51 +0000 | [diff] [blame] | 4151 | #ifndef SQLITE_OMIT_MERGE_SORT |
dan | 5134d13 | 2011-09-02 10:31:11 +0000 | [diff] [blame] | 4152 | pOut = &aMem[pOp->p2]; |
| 4153 | pC = p->apCsr[pOp->p1]; |
| 4154 | assert( pC->isSorter ); |
| 4155 | rc = sqlite3VdbeSorterRowkey(pC, pOut); |
drh | ca892a7 | 2011-09-03 00:17:51 +0000 | [diff] [blame] | 4156 | #else |
| 4157 | pOp->opcode = OP_RowKey; |
| 4158 | pc--; |
| 4159 | #endif |
dan | 5134d13 | 2011-09-02 10:31:11 +0000 | [diff] [blame] | 4160 | break; |
| 4161 | } |
| 4162 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4163 | /* Opcode: RowData P1 P2 * * * |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 4164 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4165 | ** Write into register P2 the complete row data for cursor P1. |
| 4166 | ** There is no interpretation of the data. |
| 4167 | ** It is just copied onto the P2 register exactly as |
danielk1977 | 96cb76f | 2008-01-04 13:24:28 +0000 | [diff] [blame] | 4168 | ** it is found in the database file. |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 4169 | ** |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 4170 | ** If the P1 cursor must be pointing to a valid row (not a NULL row) |
| 4171 | ** of a real table, not a pseudo-table. |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 4172 | */ |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4173 | /* Opcode: RowKey P1 P2 * * * |
drh | 143f3c4 | 2004-01-07 20:37:52 +0000 | [diff] [blame] | 4174 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4175 | ** Write into register P2 the complete row key for cursor P1. |
| 4176 | ** There is no interpretation of the data. |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4177 | ** The key is copied onto the P3 register exactly as |
danielk1977 | 96cb76f | 2008-01-04 13:24:28 +0000 | [diff] [blame] | 4178 | ** it is found in the database file. |
drh | 143f3c4 | 2004-01-07 20:37:52 +0000 | [diff] [blame] | 4179 | ** |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 4180 | ** If the P1 cursor must be pointing to a valid row (not a NULL row) |
| 4181 | ** of a real table, not a pseudo-table. |
drh | 143f3c4 | 2004-01-07 20:37:52 +0000 | [diff] [blame] | 4182 | */ |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 4183 | case OP_RowKey: |
| 4184 | case OP_RowData: { |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 4185 | VdbeCursor *pC; |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 4186 | BtCursor *pCrsr; |
danielk1977 | e0d4b06 | 2004-06-28 01:11:46 +0000 | [diff] [blame] | 4187 | u32 n; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 4188 | i64 n64; |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 4189 | |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 4190 | pOut = &aMem[pOp->p2]; |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 4191 | memAboutToChange(p, pOut); |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 4192 | |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 4193 | /* Note that RowKey and RowData are really exactly the same instruction */ |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 4194 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 4195 | pC = p->apCsr[pOp->p1]; |
dan | 5134d13 | 2011-09-02 10:31:11 +0000 | [diff] [blame] | 4196 | assert( pC->isSorter==0 ); |
drh | c6aff30 | 2011-09-01 15:32:47 +0000 | [diff] [blame] | 4197 | assert( pC->isTable || pOp->opcode!=OP_RowData ); |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 4198 | assert( pC->isIndex || pOp->opcode==OP_RowData ); |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 4199 | assert( pC!=0 ); |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 4200 | assert( pC->nullRow==0 ); |
drh | 3e9ca09 | 2009-09-08 01:14:48 +0000 | [diff] [blame] | 4201 | assert( pC->pseudoTableReg==0 ); |
drh | ca892a7 | 2011-09-03 00:17:51 +0000 | [diff] [blame] | 4202 | assert( !pC->isSorter ); |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 4203 | assert( pC->pCursor!=0 ); |
| 4204 | pCrsr = pC->pCursor; |
drh | ea8ffdf | 2009-07-22 00:35:23 +0000 | [diff] [blame] | 4205 | assert( sqlite3BtreeCursorIsValid(pCrsr) ); |
drh | 9a65f2c | 2009-06-22 19:05:40 +0000 | [diff] [blame] | 4206 | |
| 4207 | /* The OP_RowKey and OP_RowData opcodes always follow OP_NotExists or |
| 4208 | ** OP_Rewind/Op_Next with no intervening instructions that might invalidate |
| 4209 | ** the cursor. Hence the following sqlite3VdbeCursorMoveto() call is always |
| 4210 | ** a no-op and can never fail. But we leave it in place as a safety. |
| 4211 | */ |
| 4212 | assert( pC->deferredMoveto==0 ); |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 4213 | rc = sqlite3VdbeCursorMoveto(pC); |
drh | 9a65f2c | 2009-06-22 19:05:40 +0000 | [diff] [blame] | 4214 | if( NEVER(rc!=SQLITE_OK) ) goto abort_due_to_error; |
| 4215 | |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 4216 | if( pC->isIndex ){ |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 4217 | assert( !pC->isTable ); |
drh | c27ae61 | 2009-07-14 18:35:44 +0000 | [diff] [blame] | 4218 | rc = sqlite3BtreeKeySize(pCrsr, &n64); |
| 4219 | assert( rc==SQLITE_OK ); /* True because of CursorMoveto() call above */ |
drh | bb4957f | 2008-03-20 14:03:29 +0000 | [diff] [blame] | 4220 | if( n64>db->aLimit[SQLITE_LIMIT_LENGTH] ){ |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 4221 | goto too_big; |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 4222 | } |
drh | bfb19dc | 2009-06-05 16:46:53 +0000 | [diff] [blame] | 4223 | n = (u32)n64; |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 4224 | }else{ |
drh | c27ae61 | 2009-07-14 18:35:44 +0000 | [diff] [blame] | 4225 | rc = sqlite3BtreeDataSize(pCrsr, &n); |
drh | ea8ffdf | 2009-07-22 00:35:23 +0000 | [diff] [blame] | 4226 | assert( rc==SQLITE_OK ); /* DataSize() cannot fail */ |
shane | 75ac1de | 2009-06-09 18:58:52 +0000 | [diff] [blame] | 4227 | if( n>(u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){ |
drh | 023ae03 | 2007-05-08 12:12:16 +0000 | [diff] [blame] | 4228 | goto too_big; |
| 4229 | } |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 4230 | } |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 4231 | if( sqlite3VdbeMemGrow(pOut, n, 0) ){ |
| 4232 | goto no_mem; |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 4233 | } |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 4234 | pOut->n = n; |
| 4235 | MemSetTypeFlag(pOut, MEM_Blob); |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 4236 | if( pC->isIndex ){ |
| 4237 | rc = sqlite3BtreeKey(pCrsr, 0, n, pOut->z); |
| 4238 | }else{ |
| 4239 | rc = sqlite3BtreeData(pCrsr, 0, n, pOut->z); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4240 | } |
danielk1977 | 96cb76f | 2008-01-04 13:24:28 +0000 | [diff] [blame] | 4241 | pOut->enc = SQLITE_UTF8; /* In case the blob is ever cast to text */ |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 4242 | UPDATE_MAX_BLOBSIZE(pOut); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4243 | break; |
| 4244 | } |
| 4245 | |
drh | 2133d82 | 2008-01-03 18:44:59 +0000 | [diff] [blame] | 4246 | /* Opcode: Rowid P1 P2 * * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4247 | ** |
drh | 2133d82 | 2008-01-03 18:44:59 +0000 | [diff] [blame] | 4248 | ** 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] | 4249 | ** P1 is currently point to. |
drh | 044925b | 2009-04-22 17:15:02 +0000 | [diff] [blame] | 4250 | ** |
| 4251 | ** P1 can be either an ordinary table or a virtual table. There used to |
| 4252 | ** be a separate OP_VRowid opcode for use with virtual tables, but this |
| 4253 | ** one opcode now works for both table types. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4254 | */ |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 4255 | case OP_Rowid: { /* out2-prerelease */ |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 4256 | VdbeCursor *pC; |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 4257 | i64 v; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 4258 | sqlite3_vtab *pVtab; |
| 4259 | const sqlite3_module *pModule; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4260 | |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 4261 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 4262 | pC = p->apCsr[pOp->p1]; |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 4263 | assert( pC!=0 ); |
drh | 3e9ca09 | 2009-09-08 01:14:48 +0000 | [diff] [blame] | 4264 | assert( pC->pseudoTableReg==0 ); |
drh | 044925b | 2009-04-22 17:15:02 +0000 | [diff] [blame] | 4265 | if( pC->nullRow ){ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 4266 | pOut->flags = MEM_Null; |
drh | 044925b | 2009-04-22 17:15:02 +0000 | [diff] [blame] | 4267 | break; |
| 4268 | }else if( pC->deferredMoveto ){ |
drh | 6149526 | 2009-04-22 15:32:59 +0000 | [diff] [blame] | 4269 | v = pC->movetoTarget; |
drh | 044925b | 2009-04-22 17:15:02 +0000 | [diff] [blame] | 4270 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 4271 | }else if( pC->pVtabCursor ){ |
drh | 044925b | 2009-04-22 17:15:02 +0000 | [diff] [blame] | 4272 | pVtab = pC->pVtabCursor->pVtab; |
| 4273 | pModule = pVtab->pModule; |
| 4274 | assert( pModule->xRowid ); |
drh | 044925b | 2009-04-22 17:15:02 +0000 | [diff] [blame] | 4275 | rc = pModule->xRowid(pC->pVtabCursor, &v); |
drh | b975598 | 2010-07-24 16:34:37 +0000 | [diff] [blame] | 4276 | importVtabErrMsg(p, pVtab); |
drh | 044925b | 2009-04-22 17:15:02 +0000 | [diff] [blame] | 4277 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 4278 | }else{ |
drh | 6be240e | 2009-07-14 02:33:02 +0000 | [diff] [blame] | 4279 | assert( pC->pCursor!=0 ); |
drh | 6149526 | 2009-04-22 15:32:59 +0000 | [diff] [blame] | 4280 | rc = sqlite3VdbeCursorMoveto(pC); |
| 4281 | if( rc ) goto abort_due_to_error; |
| 4282 | if( pC->rowidIsValid ){ |
| 4283 | v = pC->lastRowid; |
drh | 6149526 | 2009-04-22 15:32:59 +0000 | [diff] [blame] | 4284 | }else{ |
drh | c27ae61 | 2009-07-14 18:35:44 +0000 | [diff] [blame] | 4285 | rc = sqlite3BtreeKeySize(pC->pCursor, &v); |
| 4286 | assert( rc==SQLITE_OK ); /* Always so because of CursorMoveto() above */ |
drh | 6149526 | 2009-04-22 15:32:59 +0000 | [diff] [blame] | 4287 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4288 | } |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 4289 | pOut->u.i = v; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4290 | break; |
| 4291 | } |
| 4292 | |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4293 | /* Opcode: NullRow P1 * * * * |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 4294 | ** |
| 4295 | ** Move the cursor P1 to a null row. Any OP_Column operations |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4296 | ** that occur while the cursor is on the null row will always |
| 4297 | ** write a NULL. |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 4298 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4299 | case OP_NullRow: { |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 4300 | VdbeCursor *pC; |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 4301 | |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 4302 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 4303 | pC = p->apCsr[pOp->p1]; |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 4304 | assert( pC!=0 ); |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame] | 4305 | pC->nullRow = 1; |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 4306 | pC->rowidIsValid = 0; |
dan | a205a48 | 2011-08-27 18:48:57 +0000 | [diff] [blame] | 4307 | assert( pC->pCursor || pC->pVtabCursor ); |
danielk1977 | be51a65 | 2008-10-08 17:58:48 +0000 | [diff] [blame] | 4308 | if( pC->pCursor ){ |
| 4309 | sqlite3BtreeClearCursor(pC->pCursor); |
| 4310 | } |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 4311 | break; |
| 4312 | } |
| 4313 | |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4314 | /* Opcode: Last P1 P2 * * * |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 4315 | ** |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 4316 | ** The next use of the Rowid or Column or Next instruction for P1 |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 4317 | ** will refer to the last entry in the database table or index. |
| 4318 | ** If the table or index is empty and P2>0, then jump immediately to P2. |
| 4319 | ** If P2 is 0 or if the table or index is not empty, fall through |
| 4320 | ** to the following instruction. |
| 4321 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4322 | case OP_Last: { /* jump */ |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 4323 | VdbeCursor *pC; |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 4324 | BtCursor *pCrsr; |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 4325 | int res; |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 4326 | |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 4327 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 4328 | pC = p->apCsr[pOp->p1]; |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 4329 | assert( pC!=0 ); |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 4330 | pCrsr = pC->pCursor; |
dan | a205a48 | 2011-08-27 18:48:57 +0000 | [diff] [blame] | 4331 | if( NEVER(pCrsr==0) ){ |
drh | 9a65f2c | 2009-06-22 19:05:40 +0000 | [diff] [blame] | 4332 | res = 1; |
| 4333 | }else{ |
| 4334 | rc = sqlite3BtreeLast(pCrsr, &res); |
| 4335 | } |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 4336 | pC->nullRow = (u8)res; |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 4337 | pC->deferredMoveto = 0; |
drh | a7e7706 | 2009-01-14 00:55:09 +0000 | [diff] [blame] | 4338 | pC->rowidIsValid = 0; |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 4339 | pC->cacheStatus = CACHE_STALE; |
drh | 9a65f2c | 2009-06-22 19:05:40 +0000 | [diff] [blame] | 4340 | if( pOp->p2>0 && res ){ |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 4341 | pc = pOp->p2 - 1; |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 4342 | } |
| 4343 | break; |
| 4344 | } |
| 4345 | |
drh | 0342b1f | 2005-09-01 03:07:44 +0000 | [diff] [blame] | 4346 | |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4347 | /* Opcode: Sort P1 P2 * * * |
drh | 0342b1f | 2005-09-01 03:07:44 +0000 | [diff] [blame] | 4348 | ** |
| 4349 | ** This opcode does exactly the same thing as OP_Rewind except that |
| 4350 | ** it increments an undocumented global variable used for testing. |
| 4351 | ** |
| 4352 | ** Sorting is accomplished by writing records into a sorting index, |
| 4353 | ** then rewinding that index and playing it back from beginning to |
| 4354 | ** end. We use the OP_Sort opcode instead of OP_Rewind to do the |
| 4355 | ** rewinding so that the global variable will be incremented and |
| 4356 | ** regression tests can determine whether or not the optimizer is |
| 4357 | ** correctly optimizing out sorts. |
| 4358 | */ |
drh | c6aff30 | 2011-09-01 15:32:47 +0000 | [diff] [blame] | 4359 | case OP_SorterSort: /* jump */ |
drh | ca892a7 | 2011-09-03 00:17:51 +0000 | [diff] [blame] | 4360 | #ifdef SQLITE_OMIT_MERGE_SORT |
| 4361 | pOp->opcode = OP_Sort; |
| 4362 | #endif |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4363 | case OP_Sort: { /* jump */ |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 4364 | #ifdef SQLITE_TEST |
drh | 0342b1f | 2005-09-01 03:07:44 +0000 | [diff] [blame] | 4365 | sqlite3_sort_count++; |
drh | 4db38a7 | 2005-09-01 12:16:28 +0000 | [diff] [blame] | 4366 | sqlite3_search_count--; |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 4367 | #endif |
drh | d1d3848 | 2008-10-07 23:46:38 +0000 | [diff] [blame] | 4368 | p->aCounter[SQLITE_STMTSTATUS_SORT-1]++; |
drh | 0342b1f | 2005-09-01 03:07:44 +0000 | [diff] [blame] | 4369 | /* Fall through into OP_Rewind */ |
| 4370 | } |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4371 | /* Opcode: Rewind P1 P2 * * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4372 | ** |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 4373 | ** The next use of the Rowid or Column or Next instruction for P1 |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 4374 | ** will refer to the first entry in the database table or index. |
| 4375 | ** If the table or index is empty and P2>0, then jump immediately to P2. |
| 4376 | ** If P2 is 0 or if the table or index is not empty, fall through |
| 4377 | ** to the following instruction. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4378 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4379 | case OP_Rewind: { /* jump */ |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 4380 | VdbeCursor *pC; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4381 | BtCursor *pCrsr; |
drh | f4dada7 | 2004-05-11 09:57:35 +0000 | [diff] [blame] | 4382 | int res; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4383 | |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 4384 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 4385 | pC = p->apCsr[pOp->p1]; |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 4386 | assert( pC!=0 ); |
drh | c6aff30 | 2011-09-01 15:32:47 +0000 | [diff] [blame] | 4387 | assert( pC->isSorter==(pOp->opcode==OP_SorterSort) ); |
dan | 2411dea | 2010-07-03 05:56:09 +0000 | [diff] [blame] | 4388 | res = 1; |
dan | 689ab89 | 2011-08-12 15:02:00 +0000 | [diff] [blame] | 4389 | if( isSorter(pC) ){ |
dan | a20fde6 | 2011-07-12 14:28:05 +0000 | [diff] [blame] | 4390 | rc = sqlite3VdbeSorterRewind(db, pC, &res); |
dan | a205a48 | 2011-08-27 18:48:57 +0000 | [diff] [blame] | 4391 | }else{ |
| 4392 | pCrsr = pC->pCursor; |
| 4393 | assert( pCrsr ); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 4394 | rc = sqlite3BtreeFirst(pCrsr, &res); |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 4395 | pC->atFirst = res==0 ?1:0; |
drh | a11846b | 2004-01-07 18:52:56 +0000 | [diff] [blame] | 4396 | pC->deferredMoveto = 0; |
drh | 76873ab | 2006-01-07 18:48:26 +0000 | [diff] [blame] | 4397 | pC->cacheStatus = CACHE_STALE; |
drh | a7e7706 | 2009-01-14 00:55:09 +0000 | [diff] [blame] | 4398 | pC->rowidIsValid = 0; |
drh | f4dada7 | 2004-05-11 09:57:35 +0000 | [diff] [blame] | 4399 | } |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 4400 | pC->nullRow = (u8)res; |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 4401 | assert( pOp->p2>0 && pOp->p2<p->nOp ); |
| 4402 | if( res ){ |
drh | f4dada7 | 2004-05-11 09:57:35 +0000 | [diff] [blame] | 4403 | pc = pOp->p2 - 1; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4404 | } |
| 4405 | break; |
| 4406 | } |
| 4407 | |
dan | a205a48 | 2011-08-27 18:48:57 +0000 | [diff] [blame] | 4408 | /* Opcode: Next P1 P2 * P4 P5 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4409 | ** |
| 4410 | ** 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] | 4411 | ** table or index. If there are no more key/value pairs then fall through |
| 4412 | ** to the following instruction. But if the cursor advance was successful, |
| 4413 | ** jump immediately to P2. |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 4414 | ** |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 4415 | ** The P1 cursor must be for a real table, not a pseudo-table. |
| 4416 | ** |
dan | a205a48 | 2011-08-27 18:48:57 +0000 | [diff] [blame] | 4417 | ** P4 is always of type P4_ADVANCE. The function pointer points to |
| 4418 | ** sqlite3BtreeNext(). |
| 4419 | ** |
drh | afc266a | 2010-03-31 17:47:44 +0000 | [diff] [blame] | 4420 | ** If P5 is positive and the jump is taken, then event counter |
| 4421 | ** number P5-1 in the prepared statement is incremented. |
| 4422 | ** |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 4423 | ** See also: Prev |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 4424 | */ |
drh | afc266a | 2010-03-31 17:47:44 +0000 | [diff] [blame] | 4425 | /* Opcode: Prev P1 P2 * * P5 |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 4426 | ** |
| 4427 | ** Back up cursor P1 so that it points to the previous key/data pair in its |
| 4428 | ** table or index. If there is no previous key/value pairs then fall through |
| 4429 | ** to the following instruction. But if the cursor backup was successful, |
| 4430 | ** jump immediately to P2. |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 4431 | ** |
| 4432 | ** The P1 cursor must be for a real table, not a pseudo-table. |
drh | afc266a | 2010-03-31 17:47:44 +0000 | [diff] [blame] | 4433 | ** |
dan | a205a48 | 2011-08-27 18:48:57 +0000 | [diff] [blame] | 4434 | ** P4 is always of type P4_ADVANCE. The function pointer points to |
| 4435 | ** sqlite3BtreePrevious(). |
| 4436 | ** |
drh | afc266a | 2010-03-31 17:47:44 +0000 | [diff] [blame] | 4437 | ** If P5 is positive and the jump is taken, then event counter |
| 4438 | ** number P5-1 in the prepared statement is incremented. |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 4439 | */ |
drh | c6aff30 | 2011-09-01 15:32:47 +0000 | [diff] [blame] | 4440 | case OP_SorterNext: /* jump */ |
drh | ca892a7 | 2011-09-03 00:17:51 +0000 | [diff] [blame] | 4441 | #ifdef SQLITE_OMIT_MERGE_SORT |
| 4442 | pOp->opcode = OP_Next; |
| 4443 | #endif |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4444 | case OP_Prev: /* jump */ |
| 4445 | case OP_Next: { /* jump */ |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 4446 | VdbeCursor *pC; |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 4447 | int res; |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 4448 | |
drh | caec2f1 | 2003-01-07 02:47:47 +0000 | [diff] [blame] | 4449 | CHECK_FOR_INTERRUPT; |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 4450 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
drh | afc266a | 2010-03-31 17:47:44 +0000 | [diff] [blame] | 4451 | assert( pOp->p5<=ArraySize(p->aCounter) ); |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame] | 4452 | pC = p->apCsr[pOp->p1]; |
drh | 72e8fa4 | 2007-03-28 14:30:06 +0000 | [diff] [blame] | 4453 | if( pC==0 ){ |
| 4454 | break; /* See ticket #2273 */ |
| 4455 | } |
drh | c6aff30 | 2011-09-01 15:32:47 +0000 | [diff] [blame] | 4456 | assert( pC->isSorter==(pOp->opcode==OP_SorterNext) ); |
dan | 689ab89 | 2011-08-12 15:02:00 +0000 | [diff] [blame] | 4457 | if( isSorter(pC) ){ |
dan | 5134d13 | 2011-09-02 10:31:11 +0000 | [diff] [blame] | 4458 | assert( pOp->opcode==OP_SorterNext ); |
dan | a20fde6 | 2011-07-12 14:28:05 +0000 | [diff] [blame] | 4459 | rc = sqlite3VdbeSorterNext(db, pC, &res); |
| 4460 | }else{ |
dan | a20fde6 | 2011-07-12 14:28:05 +0000 | [diff] [blame] | 4461 | res = 1; |
| 4462 | assert( pC->deferredMoveto==0 ); |
dan | a205a48 | 2011-08-27 18:48:57 +0000 | [diff] [blame] | 4463 | assert( pC->pCursor ); |
| 4464 | assert( pOp->opcode!=OP_Next || pOp->p4.xAdvance==sqlite3BtreeNext ); |
| 4465 | assert( pOp->opcode!=OP_Prev || pOp->p4.xAdvance==sqlite3BtreePrevious ); |
| 4466 | rc = pOp->p4.xAdvance(pC->pCursor, &res); |
drh | 9a65f2c | 2009-06-22 19:05:40 +0000 | [diff] [blame] | 4467 | } |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 4468 | pC->nullRow = (u8)res; |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 4469 | pC->cacheStatus = CACHE_STALE; |
| 4470 | if( res==0 ){ |
| 4471 | pc = pOp->p2 - 1; |
drh | d1d3848 | 2008-10-07 23:46:38 +0000 | [diff] [blame] | 4472 | if( pOp->p5 ) p->aCounter[pOp->p5-1]++; |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 4473 | #ifdef SQLITE_TEST |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 4474 | sqlite3_search_count++; |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 4475 | #endif |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 4476 | } |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 4477 | pC->rowidIsValid = 0; |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 4478 | break; |
| 4479 | } |
| 4480 | |
danielk1977 | de63035 | 2009-05-04 11:42:29 +0000 | [diff] [blame] | 4481 | /* Opcode: IdxInsert P1 P2 P3 * P5 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4482 | ** |
drh | ef8662b | 2011-06-20 21:47:58 +0000 | [diff] [blame] | 4483 | ** Register P2 holds an SQL index key made using the |
drh | 9437bd2 | 2009-02-01 00:29:56 +0000 | [diff] [blame] | 4484 | ** MakeRecord instructions. This opcode writes that key |
drh | ee32e0a | 2006-01-10 19:45:49 +0000 | [diff] [blame] | 4485 | ** into the index P1. Data for the entry is nil. |
drh | 717e640 | 2001-09-27 03:22:32 +0000 | [diff] [blame] | 4486 | ** |
drh | aa9b896 | 2008-01-08 02:57:55 +0000 | [diff] [blame] | 4487 | ** 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] | 4488 | ** insert is likely to be an append. |
| 4489 | ** |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 4490 | ** This instruction only works for indices. The equivalent instruction |
| 4491 | ** for tables is OP_Insert. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4492 | */ |
drh | ca892a7 | 2011-09-03 00:17:51 +0000 | [diff] [blame] | 4493 | case OP_SorterInsert: /* in2 */ |
| 4494 | #ifdef SQLITE_OMIT_MERGE_SORT |
| 4495 | pOp->opcode = OP_IdxInsert; |
| 4496 | #endif |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4497 | case OP_IdxInsert: { /* in2 */ |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 4498 | VdbeCursor *pC; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4499 | BtCursor *pCrsr; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 4500 | int nKey; |
| 4501 | const char *zKey; |
| 4502 | |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 4503 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 4504 | pC = p->apCsr[pOp->p1]; |
| 4505 | assert( pC!=0 ); |
drh | c6aff30 | 2011-09-01 15:32:47 +0000 | [diff] [blame] | 4506 | assert( pC->isSorter==(pOp->opcode==OP_SorterInsert) ); |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 4507 | pIn2 = &aMem[pOp->p2]; |
drh | aa9b896 | 2008-01-08 02:57:55 +0000 | [diff] [blame] | 4508 | assert( pIn2->flags & MEM_Blob ); |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 4509 | pCrsr = pC->pCursor; |
drh | 9a65f2c | 2009-06-22 19:05:40 +0000 | [diff] [blame] | 4510 | if( ALWAYS(pCrsr!=0) ){ |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 4511 | assert( pC->isTable==0 ); |
drh | aa9b896 | 2008-01-08 02:57:55 +0000 | [diff] [blame] | 4512 | rc = ExpandBlob(pIn2); |
danielk1977 | d908f5a | 2007-05-11 07:08:28 +0000 | [diff] [blame] | 4513 | if( rc==SQLITE_OK ){ |
dan | 5134d13 | 2011-09-02 10:31:11 +0000 | [diff] [blame] | 4514 | if( isSorter(pC) ){ |
| 4515 | rc = sqlite3VdbeSorterWrite(db, pC, pIn2); |
| 4516 | }else{ |
| 4517 | nKey = pIn2->n; |
| 4518 | zKey = pIn2->z; |
dan | 1e74e60 | 2011-08-06 12:01:58 +0000 | [diff] [blame] | 4519 | rc = sqlite3BtreeInsert(pCrsr, zKey, nKey, "", 0, 0, pOp->p3, |
| 4520 | ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0) |
dan | 5134d13 | 2011-09-02 10:31:11 +0000 | [diff] [blame] | 4521 | ); |
dan | 1e74e60 | 2011-08-06 12:01:58 +0000 | [diff] [blame] | 4522 | assert( pC->deferredMoveto==0 ); |
dan | 5134d13 | 2011-09-02 10:31:11 +0000 | [diff] [blame] | 4523 | pC->cacheStatus = CACHE_STALE; |
dan | 1e74e60 | 2011-08-06 12:01:58 +0000 | [diff] [blame] | 4524 | } |
danielk1977 | d908f5a | 2007-05-11 07:08:28 +0000 | [diff] [blame] | 4525 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4526 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4527 | break; |
| 4528 | } |
| 4529 | |
drh | d1d3848 | 2008-10-07 23:46:38 +0000 | [diff] [blame] | 4530 | /* Opcode: IdxDelete P1 P2 P3 * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4531 | ** |
drh | e14006d | 2008-03-25 17:23:32 +0000 | [diff] [blame] | 4532 | ** The content of P3 registers starting at register P2 form |
| 4533 | ** an unpacked index key. This opcode removes that entry from the |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 4534 | ** index opened by cursor P1. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4535 | */ |
drh | e14006d | 2008-03-25 17:23:32 +0000 | [diff] [blame] | 4536 | case OP_IdxDelete: { |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 4537 | VdbeCursor *pC; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4538 | BtCursor *pCrsr; |
drh | 9a65f2c | 2009-06-22 19:05:40 +0000 | [diff] [blame] | 4539 | int res; |
| 4540 | UnpackedRecord r; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 4541 | |
drh | e14006d | 2008-03-25 17:23:32 +0000 | [diff] [blame] | 4542 | assert( pOp->p3>0 ); |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 4543 | assert( pOp->p2>0 && pOp->p2+pOp->p3<=p->nMem+1 ); |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 4544 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 4545 | pC = p->apCsr[pOp->p1]; |
| 4546 | assert( pC!=0 ); |
| 4547 | pCrsr = pC->pCursor; |
drh | 9a65f2c | 2009-06-22 19:05:40 +0000 | [diff] [blame] | 4548 | if( ALWAYS(pCrsr!=0) ){ |
drh | e14006d | 2008-03-25 17:23:32 +0000 | [diff] [blame] | 4549 | r.pKeyInfo = pC->pKeyInfo; |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 4550 | r.nField = (u16)pOp->p3; |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 4551 | r.flags = 0; |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 4552 | r.aMem = &aMem[pOp->p2]; |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 4553 | #ifdef SQLITE_DEBUG |
| 4554 | { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); } |
| 4555 | #endif |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 4556 | rc = sqlite3BtreeMovetoUnpacked(pCrsr, &r, 0, 0, &res); |
danielk1977 | 75bab7d | 2006-01-23 13:09:45 +0000 | [diff] [blame] | 4557 | if( rc==SQLITE_OK && res==0 ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 4558 | rc = sqlite3BtreeDelete(pCrsr); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4559 | } |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 4560 | assert( pC->deferredMoveto==0 ); |
drh | 76873ab | 2006-01-07 18:48:26 +0000 | [diff] [blame] | 4561 | pC->cacheStatus = CACHE_STALE; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4562 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4563 | break; |
| 4564 | } |
| 4565 | |
drh | 2133d82 | 2008-01-03 18:44:59 +0000 | [diff] [blame] | 4566 | /* Opcode: IdxRowid P1 P2 * * * |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 4567 | ** |
drh | 2133d82 | 2008-01-03 18:44:59 +0000 | [diff] [blame] | 4568 | ** 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] | 4569 | ** the end of the index key pointed to by cursor P1. This integer should be |
| 4570 | ** the rowid of the table entry to which this index entry points. |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 4571 | ** |
drh | 9437bd2 | 2009-02-01 00:29:56 +0000 | [diff] [blame] | 4572 | ** See also: Rowid, MakeRecord. |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 4573 | */ |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 4574 | case OP_IdxRowid: { /* out2-prerelease */ |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 4575 | BtCursor *pCrsr; |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 4576 | VdbeCursor *pC; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 4577 | i64 rowid; |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 4578 | |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 4579 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 4580 | pC = p->apCsr[pOp->p1]; |
| 4581 | assert( pC!=0 ); |
| 4582 | pCrsr = pC->pCursor; |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 4583 | pOut->flags = MEM_Null; |
drh | 9a65f2c | 2009-06-22 19:05:40 +0000 | [diff] [blame] | 4584 | if( ALWAYS(pCrsr!=0) ){ |
danielk1977 | c4d201c | 2009-04-07 09:16:56 +0000 | [diff] [blame] | 4585 | rc = sqlite3VdbeCursorMoveto(pC); |
drh | 9a65f2c | 2009-06-22 19:05:40 +0000 | [diff] [blame] | 4586 | if( NEVER(rc) ) goto abort_due_to_error; |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame] | 4587 | assert( pC->deferredMoveto==0 ); |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 4588 | assert( pC->isTable==0 ); |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 4589 | if( !pC->nullRow ){ |
drh | 35f6b93 | 2009-06-23 14:15:04 +0000 | [diff] [blame] | 4590 | rc = sqlite3VdbeIdxRowid(db, pCrsr, &rowid); |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 4591 | if( rc!=SQLITE_OK ){ |
| 4592 | goto abort_due_to_error; |
| 4593 | } |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 4594 | pOut->u.i = rowid; |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 4595 | pOut->flags = MEM_Int; |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 4596 | } |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 4597 | } |
| 4598 | break; |
| 4599 | } |
| 4600 | |
danielk1977 | 61dd583 | 2008-04-18 11:31:12 +0000 | [diff] [blame] | 4601 | /* Opcode: IdxGE P1 P2 P3 P4 P5 |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 4602 | ** |
danielk1977 | 61dd583 | 2008-04-18 11:31:12 +0000 | [diff] [blame] | 4603 | ** The P4 register values beginning with P3 form an unpacked index |
| 4604 | ** key that omits the ROWID. Compare this key value against the index |
| 4605 | ** that P1 is currently pointing to, ignoring the ROWID on the P1 index. |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 4606 | ** |
danielk1977 | 61dd583 | 2008-04-18 11:31:12 +0000 | [diff] [blame] | 4607 | ** If the P1 index entry is greater than or equal to the key value |
| 4608 | ** then jump to P2. Otherwise fall through to the next instruction. |
drh | 772ae62 | 2004-05-19 13:13:08 +0000 | [diff] [blame] | 4609 | ** |
danielk1977 | 61dd583 | 2008-04-18 11:31:12 +0000 | [diff] [blame] | 4610 | ** If P5 is non-zero then the key value is increased by an epsilon |
| 4611 | ** prior to the comparison. This make the opcode work like IdxGT except |
| 4612 | ** that if the key from register P3 is a prefix of the key in the cursor, |
| 4613 | ** the result is false whereas it would be true with IdxGT. |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 4614 | */ |
drh | 3bb9b93 | 2010-08-06 02:10:00 +0000 | [diff] [blame] | 4615 | /* Opcode: IdxLT P1 P2 P3 P4 P5 |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 4616 | ** |
danielk1977 | 61dd583 | 2008-04-18 11:31:12 +0000 | [diff] [blame] | 4617 | ** The P4 register values beginning with P3 form an unpacked index |
| 4618 | ** key that omits the ROWID. Compare this key value against the index |
| 4619 | ** that P1 is currently pointing to, ignoring the ROWID on the P1 index. |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 4620 | ** |
danielk1977 | 61dd583 | 2008-04-18 11:31:12 +0000 | [diff] [blame] | 4621 | ** If the P1 index entry is less than the key value then jump to P2. |
| 4622 | ** Otherwise fall through to the next instruction. |
drh | 772ae62 | 2004-05-19 13:13:08 +0000 | [diff] [blame] | 4623 | ** |
danielk1977 | 61dd583 | 2008-04-18 11:31:12 +0000 | [diff] [blame] | 4624 | ** If P5 is non-zero then the key value is increased by an epsilon prior |
| 4625 | ** to the comparison. This makes the opcode work like IdxLE. |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 4626 | */ |
drh | 93952eb | 2009-11-13 19:43:43 +0000 | [diff] [blame] | 4627 | case OP_IdxLT: /* jump */ |
| 4628 | case OP_IdxGE: { /* jump */ |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 4629 | VdbeCursor *pC; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 4630 | int res; |
| 4631 | UnpackedRecord r; |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 4632 | |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 4633 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 4634 | pC = p->apCsr[pOp->p1]; |
| 4635 | assert( pC!=0 ); |
drh | d4187c7 | 2010-08-30 22:15:45 +0000 | [diff] [blame] | 4636 | assert( pC->isOrdered ); |
drh | 9a65f2c | 2009-06-22 19:05:40 +0000 | [diff] [blame] | 4637 | if( ALWAYS(pC->pCursor!=0) ){ |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame] | 4638 | assert( pC->deferredMoveto==0 ); |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 4639 | assert( pOp->p5==0 || pOp->p5==1 ); |
danielk1977 | 61dd583 | 2008-04-18 11:31:12 +0000 | [diff] [blame] | 4640 | assert( pOp->p4type==P4_INT32 ); |
| 4641 | r.pKeyInfo = pC->pKeyInfo; |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 4642 | r.nField = (u16)pOp->p4.i; |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 4643 | if( pOp->p5 ){ |
| 4644 | r.flags = UNPACKED_INCRKEY | UNPACKED_IGNORE_ROWID; |
| 4645 | }else{ |
| 4646 | r.flags = UNPACKED_IGNORE_ROWID; |
| 4647 | } |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 4648 | r.aMem = &aMem[pOp->p3]; |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 4649 | #ifdef SQLITE_DEBUG |
| 4650 | { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); } |
| 4651 | #endif |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 4652 | rc = sqlite3VdbeIdxKeyCompare(pC, &r, &res); |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 4653 | if( pOp->opcode==OP_IdxLT ){ |
| 4654 | res = -res; |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 4655 | }else{ |
| 4656 | assert( pOp->opcode==OP_IdxGE ); |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 4657 | res++; |
| 4658 | } |
| 4659 | if( res>0 ){ |
| 4660 | pc = pOp->p2 - 1 ; |
| 4661 | } |
| 4662 | } |
| 4663 | break; |
| 4664 | } |
| 4665 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4666 | /* Opcode: Destroy P1 P2 P3 * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4667 | ** |
| 4668 | ** Delete an entire database table or index whose root page in the database |
| 4669 | ** file is given by P1. |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 4670 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4671 | ** The table being destroyed is in the main database file if P3==0. If |
| 4672 | ** 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] | 4673 | ** that is used to store tables create using CREATE TEMPORARY TABLE. |
| 4674 | ** |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 4675 | ** If AUTOVACUUM is enabled then it is possible that another root page |
| 4676 | ** might be moved into the newly deleted root page in order to keep all |
| 4677 | ** root pages contiguous at the beginning of the database. The former |
| 4678 | ** value of the root page that moved - its value before the move occurred - |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4679 | ** is stored in register P2. If no page |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4680 | ** movement was required (because the table being dropped was already |
| 4681 | ** the last one in the database) then a zero is stored in register P2. |
| 4682 | ** If AUTOVACUUM is disabled then a zero is stored in register P2. |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 4683 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 4684 | ** See also: Clear |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4685 | */ |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4686 | case OP_Destroy: { /* out2-prerelease */ |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 4687 | int iMoved; |
drh | 3765df4 | 2006-06-28 18:18:09 +0000 | [diff] [blame] | 4688 | int iCnt; |
drh | 5a91a53 | 2007-01-05 16:39:43 +0000 | [diff] [blame] | 4689 | Vdbe *pVdbe; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 4690 | int iDb; |
| 4691 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
danielk1977 | 212b218 | 2006-06-23 14:32:08 +0000 | [diff] [blame] | 4692 | iCnt = 0; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 4693 | for(pVdbe=db->pVdbe; pVdbe; pVdbe = pVdbe->pNext){ |
danielk1977 | 212b218 | 2006-06-23 14:32:08 +0000 | [diff] [blame] | 4694 | if( pVdbe->magic==VDBE_MAGIC_RUN && pVdbe->inVtabMethod<2 && pVdbe->pc>=0 ){ |
| 4695 | iCnt++; |
| 4696 | } |
| 4697 | } |
drh | 3765df4 | 2006-06-28 18:18:09 +0000 | [diff] [blame] | 4698 | #else |
| 4699 | iCnt = db->activeVdbeCnt; |
danielk1977 | 212b218 | 2006-06-23 14:32:08 +0000 | [diff] [blame] | 4700 | #endif |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 4701 | pOut->flags = MEM_Null; |
danielk1977 | 212b218 | 2006-06-23 14:32:08 +0000 | [diff] [blame] | 4702 | if( iCnt>1 ){ |
danielk1977 | e6efa74 | 2004-11-10 11:55:10 +0000 | [diff] [blame] | 4703 | rc = SQLITE_LOCKED; |
drh | 77658e2 | 2007-12-04 16:54:52 +0000 | [diff] [blame] | 4704 | p->errorAction = OE_Abort; |
danielk1977 | e6efa74 | 2004-11-10 11:55:10 +0000 | [diff] [blame] | 4705 | }else{ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 4706 | iDb = pOp->p3; |
danielk1977 | 212b218 | 2006-06-23 14:32:08 +0000 | [diff] [blame] | 4707 | assert( iCnt==1 ); |
drh | dddd779 | 2011-04-03 18:19:25 +0000 | [diff] [blame] | 4708 | assert( (p->btreeMask & (((yDbMask)1)<<iDb))!=0 ); |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4709 | rc = sqlite3BtreeDropTable(db->aDb[iDb].pBt, pOp->p1, &iMoved); |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 4710 | pOut->flags = MEM_Int; |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4711 | pOut->u.i = iMoved; |
drh | 3765df4 | 2006-06-28 18:18:09 +0000 | [diff] [blame] | 4712 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | e6efa74 | 2004-11-10 11:55:10 +0000 | [diff] [blame] | 4713 | if( rc==SQLITE_OK && iMoved!=0 ){ |
drh | cdf011d | 2011-04-04 21:25:28 +0000 | [diff] [blame] | 4714 | sqlite3RootPageMoved(db, iDb, iMoved, pOp->p1); |
| 4715 | /* All OP_Destroy operations occur on the same btree */ |
| 4716 | assert( resetSchemaOnFault==0 || resetSchemaOnFault==iDb+1 ); |
| 4717 | resetSchemaOnFault = iDb+1; |
danielk1977 | e6efa74 | 2004-11-10 11:55:10 +0000 | [diff] [blame] | 4718 | } |
drh | 3765df4 | 2006-06-28 18:18:09 +0000 | [diff] [blame] | 4719 | #endif |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 4720 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4721 | break; |
| 4722 | } |
| 4723 | |
danielk1977 | c7af484 | 2008-10-27 13:59:33 +0000 | [diff] [blame] | 4724 | /* Opcode: Clear P1 P2 P3 |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 4725 | ** |
| 4726 | ** Delete all contents of the database table or index whose root page |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 4727 | ** in the database file is given by P1. But, unlike Destroy, do not |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 4728 | ** remove the table or index from the database file. |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 4729 | ** |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 4730 | ** The table being clear is in the main database file if P2==0. If |
| 4731 | ** P2==1 then the table to be clear is in the auxiliary database file |
| 4732 | ** that is used to store tables create using CREATE TEMPORARY TABLE. |
| 4733 | ** |
shane | be21779 | 2009-03-05 04:20:31 +0000 | [diff] [blame] | 4734 | ** If the P3 value is non-zero, then the table referred to must be an |
danielk1977 | c7af484 | 2008-10-27 13:59:33 +0000 | [diff] [blame] | 4735 | ** intkey table (an SQL table, not an index). In this case the row change |
| 4736 | ** count is incremented by the number of rows in the table being cleared. |
| 4737 | ** If P3 is greater than zero, then the value stored in register P3 is |
| 4738 | ** also incremented by the number of rows in the table being cleared. |
| 4739 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 4740 | ** See also: Destroy |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 4741 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4742 | case OP_Clear: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 4743 | int nChange; |
| 4744 | |
| 4745 | nChange = 0; |
drh | dddd779 | 2011-04-03 18:19:25 +0000 | [diff] [blame] | 4746 | assert( (p->btreeMask & (((yDbMask)1)<<pOp->p2))!=0 ); |
danielk1977 | c7af484 | 2008-10-27 13:59:33 +0000 | [diff] [blame] | 4747 | rc = sqlite3BtreeClearTable( |
| 4748 | db->aDb[pOp->p2].pBt, pOp->p1, (pOp->p3 ? &nChange : 0) |
| 4749 | ); |
| 4750 | if( pOp->p3 ){ |
| 4751 | p->nChange += nChange; |
| 4752 | if( pOp->p3>0 ){ |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 4753 | assert( memIsValid(&aMem[pOp->p3]) ); |
| 4754 | memAboutToChange(p, &aMem[pOp->p3]); |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 4755 | aMem[pOp->p3].u.i += nChange; |
danielk1977 | c7af484 | 2008-10-27 13:59:33 +0000 | [diff] [blame] | 4756 | } |
| 4757 | } |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 4758 | break; |
| 4759 | } |
| 4760 | |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 4761 | /* Opcode: CreateTable P1 P2 * * * |
drh | 5b2fd56 | 2001-09-13 15:21:31 +0000 | [diff] [blame] | 4762 | ** |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 4763 | ** Allocate a new table in the main database file if P1==0 or in the |
| 4764 | ** auxiliary database file if P1==1 or in an attached database if |
| 4765 | ** P1>1. Write the root page number of the new table into |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4766 | ** register P2 |
drh | 5b2fd56 | 2001-09-13 15:21:31 +0000 | [diff] [blame] | 4767 | ** |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 4768 | ** The difference between a table and an index is this: A table must |
| 4769 | ** have a 4-byte integer key and can have arbitrary data. An index |
| 4770 | ** has an arbitrary key but no data. |
| 4771 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 4772 | ** See also: CreateIndex |
drh | 5b2fd56 | 2001-09-13 15:21:31 +0000 | [diff] [blame] | 4773 | */ |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 4774 | /* Opcode: CreateIndex P1 P2 * * * |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 4775 | ** |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 4776 | ** Allocate a new index in the main database file if P1==0 or in the |
| 4777 | ** auxiliary database file if P1==1 or in an attached database if |
| 4778 | ** P1>1. Write the root page number of the new table into |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4779 | ** register P2. |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 4780 | ** |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 4781 | ** See documentation on OP_CreateTable for additional information. |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 4782 | */ |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 4783 | case OP_CreateIndex: /* out2-prerelease */ |
| 4784 | case OP_CreateTable: { /* out2-prerelease */ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 4785 | int pgno; |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 4786 | int flags; |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 4787 | Db *pDb; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 4788 | |
| 4789 | pgno = 0; |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 4790 | assert( pOp->p1>=0 && pOp->p1<db->nDb ); |
drh | dddd779 | 2011-04-03 18:19:25 +0000 | [diff] [blame] | 4791 | assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 ); |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 4792 | pDb = &db->aDb[pOp->p1]; |
| 4793 | assert( pDb->pBt!=0 ); |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 4794 | if( pOp->opcode==OP_CreateTable ){ |
danielk1977 | 9407625 | 2004-05-14 12:16:11 +0000 | [diff] [blame] | 4795 | /* flags = BTREE_INTKEY; */ |
drh | d4187c7 | 2010-08-30 22:15:45 +0000 | [diff] [blame] | 4796 | flags = BTREE_INTKEY; |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 4797 | }else{ |
drh | d4187c7 | 2010-08-30 22:15:45 +0000 | [diff] [blame] | 4798 | flags = BTREE_BLOBKEY; |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 4799 | } |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 4800 | rc = sqlite3BtreeCreateTable(pDb->pBt, &pgno, flags); |
drh | 88a003e | 2008-12-11 16:17:03 +0000 | [diff] [blame] | 4801 | pOut->u.i = pgno; |
drh | 5b2fd56 | 2001-09-13 15:21:31 +0000 | [diff] [blame] | 4802 | break; |
| 4803 | } |
| 4804 | |
drh | 2264584 | 2011-03-24 01:34:03 +0000 | [diff] [blame] | 4805 | /* Opcode: ParseSchema P1 * * P4 * |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 4806 | ** |
| 4807 | ** Read and parse all entries from the SQLITE_MASTER table of database P1 |
drh | 2264584 | 2011-03-24 01:34:03 +0000 | [diff] [blame] | 4808 | ** that match the WHERE clause P4. |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 4809 | ** |
| 4810 | ** This opcode invokes the parser to create a new virtual machine, |
shane | 21e7feb | 2008-05-30 15:59:49 +0000 | [diff] [blame] | 4811 | ** then runs the new virtual machine. It is thus a re-entrant opcode. |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 4812 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4813 | case OP_ParseSchema: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 4814 | int iDb; |
| 4815 | const char *zMaster; |
| 4816 | char *zSql; |
| 4817 | InitData initData; |
| 4818 | |
drh | bdaec52 | 2011-04-04 00:14:43 +0000 | [diff] [blame] | 4819 | /* Any prepared statement that invokes this opcode will hold mutexes |
| 4820 | ** on every btree. This is a prerequisite for invoking |
| 4821 | ** sqlite3InitCallback(). |
| 4822 | */ |
| 4823 | #ifdef SQLITE_DEBUG |
| 4824 | for(iDb=0; iDb<db->nDb; iDb++){ |
| 4825 | assert( iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) ); |
| 4826 | } |
| 4827 | #endif |
drh | bdaec52 | 2011-04-04 00:14:43 +0000 | [diff] [blame] | 4828 | |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 4829 | iDb = pOp->p1; |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 4830 | assert( iDb>=0 && iDb<db->nDb ); |
dan | 6c15487 | 2011-04-02 09:44:43 +0000 | [diff] [blame] | 4831 | assert( DbHasProperty(db, iDb, DB_SchemaLoaded) ); |
drh | bdaec52 | 2011-04-04 00:14:43 +0000 | [diff] [blame] | 4832 | /* Used to be a conditional */ { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 4833 | zMaster = SCHEMA_TABLE(iDb); |
danielk1977 | a8bbef8 | 2009-03-23 17:11:26 +0000 | [diff] [blame] | 4834 | initData.db = db; |
| 4835 | initData.iDb = pOp->p1; |
| 4836 | initData.pzErrMsg = &p->zErrMsg; |
| 4837 | zSql = sqlite3MPrintf(db, |
drh | 6a9c64b | 2010-01-12 23:54:14 +0000 | [diff] [blame] | 4838 | "SELECT name, rootpage, sql FROM '%q'.%s WHERE %s ORDER BY rowid", |
danielk1977 | a8bbef8 | 2009-03-23 17:11:26 +0000 | [diff] [blame] | 4839 | db->aDb[iDb].zName, zMaster, pOp->p4.z); |
| 4840 | if( zSql==0 ){ |
| 4841 | rc = SQLITE_NOMEM; |
| 4842 | }else{ |
danielk1977 | a8bbef8 | 2009-03-23 17:11:26 +0000 | [diff] [blame] | 4843 | assert( db->init.busy==0 ); |
| 4844 | db->init.busy = 1; |
| 4845 | initData.rc = SQLITE_OK; |
| 4846 | assert( !db->mallocFailed ); |
| 4847 | rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0); |
| 4848 | if( rc==SQLITE_OK ) rc = initData.rc; |
| 4849 | sqlite3DbFree(db, zSql); |
| 4850 | db->init.busy = 0; |
danielk1977 | a8bbef8 | 2009-03-23 17:11:26 +0000 | [diff] [blame] | 4851 | } |
drh | 3c23a88 | 2007-01-09 14:01:13 +0000 | [diff] [blame] | 4852 | } |
danielk1977 | 261919c | 2005-12-06 12:52:59 +0000 | [diff] [blame] | 4853 | if( rc==SQLITE_NOMEM ){ |
danielk1977 | 261919c | 2005-12-06 12:52:59 +0000 | [diff] [blame] | 4854 | goto no_mem; |
| 4855 | } |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 4856 | break; |
| 4857 | } |
| 4858 | |
drh | 8bfdf72 | 2009-06-19 14:06:03 +0000 | [diff] [blame] | 4859 | #if !defined(SQLITE_OMIT_ANALYZE) |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4860 | /* Opcode: LoadAnalysis P1 * * * * |
drh | 497e446 | 2005-07-23 03:18:40 +0000 | [diff] [blame] | 4861 | ** |
| 4862 | ** Read the sqlite_stat1 table for database P1 and load the content |
| 4863 | ** of that table into the internal index hash table. This will cause |
| 4864 | ** the analysis to be used when preparing all subsequent queries. |
| 4865 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4866 | case OP_LoadAnalysis: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 4867 | assert( pOp->p1>=0 && pOp->p1<db->nDb ); |
| 4868 | rc = sqlite3AnalysisLoad(db, pOp->p1); |
drh | 497e446 | 2005-07-23 03:18:40 +0000 | [diff] [blame] | 4869 | break; |
| 4870 | } |
drh | 8bfdf72 | 2009-06-19 14:06:03 +0000 | [diff] [blame] | 4871 | #endif /* !defined(SQLITE_OMIT_ANALYZE) */ |
drh | 497e446 | 2005-07-23 03:18:40 +0000 | [diff] [blame] | 4872 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4873 | /* Opcode: DropTable P1 * * P4 * |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 4874 | ** |
| 4875 | ** Remove the internal (in-memory) data structures that describe |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 4876 | ** the table named P4 in database P1. This is called after a table |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 4877 | ** is dropped in order to keep the internal representation of the |
| 4878 | ** schema consistent with what is on disk. |
| 4879 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4880 | case OP_DropTable: { |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 4881 | sqlite3UnlinkAndDeleteTable(db, pOp->p1, pOp->p4.z); |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 4882 | break; |
| 4883 | } |
| 4884 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4885 | /* Opcode: DropIndex P1 * * P4 * |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 4886 | ** |
| 4887 | ** Remove the internal (in-memory) data structures that describe |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 4888 | ** the index named P4 in database P1. This is called after an index |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 4889 | ** is dropped in order to keep the internal representation of the |
| 4890 | ** schema consistent with what is on disk. |
| 4891 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4892 | case OP_DropIndex: { |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 4893 | sqlite3UnlinkAndDeleteIndex(db, pOp->p1, pOp->p4.z); |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 4894 | break; |
| 4895 | } |
| 4896 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4897 | /* Opcode: DropTrigger P1 * * P4 * |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 4898 | ** |
| 4899 | ** Remove the internal (in-memory) data structures that describe |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 4900 | ** the trigger named P4 in database P1. This is called after a trigger |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 4901 | ** is dropped in order to keep the internal representation of the |
| 4902 | ** schema consistent with what is on disk. |
| 4903 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4904 | case OP_DropTrigger: { |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 4905 | sqlite3UnlinkAndDeleteTrigger(db, pOp->p1, pOp->p4.z); |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 4906 | break; |
| 4907 | } |
| 4908 | |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 4909 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 4910 | #ifndef SQLITE_OMIT_INTEGRITY_CHECK |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4911 | /* Opcode: IntegrityCk P1 P2 P3 * P5 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4912 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4913 | ** Do an analysis of the currently open database. Store in |
| 4914 | ** register P1 the text of an error message describing any problems. |
| 4915 | ** If no problems are found, store a NULL in register P1. |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 4916 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4917 | ** The register P3 contains the maximum number of allowed errors. |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 4918 | ** At most reg(P3) errors will be reported. |
| 4919 | ** In other words, the analysis stops as soon as reg(P1) errors are |
| 4920 | ** seen. Reg(P1) is updated with the number of errors remaining. |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 4921 | ** |
drh | 7906975 | 2004-05-22 21:30:40 +0000 | [diff] [blame] | 4922 | ** The root page numbers of all tables in the database are integer |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 4923 | ** 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] | 4924 | ** total. |
drh | 2150432 | 2002-06-25 13:16:02 +0000 | [diff] [blame] | 4925 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4926 | ** If P5 is not zero, the check is done on the auxiliary database |
drh | 2150432 | 2002-06-25 13:16:02 +0000 | [diff] [blame] | 4927 | ** file, not the main database file. |
drh | 1dd397f | 2002-02-03 03:34:07 +0000 | [diff] [blame] | 4928 | ** |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 4929 | ** This opcode is used to implement the integrity_check pragma. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4930 | */ |
drh | aaab572 | 2002-02-19 13:39:21 +0000 | [diff] [blame] | 4931 | case OP_IntegrityCk: { |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4932 | int nRoot; /* Number of tables to check. (Number of root pages.) */ |
| 4933 | int *aRoot; /* Array of rootpage numbers for tables to be checked */ |
| 4934 | int j; /* Loop counter */ |
| 4935 | int nErr; /* Number of errors reported */ |
| 4936 | char *z; /* Text of the error report */ |
| 4937 | Mem *pnErr; /* Register keeping track of errors remaining */ |
| 4938 | |
| 4939 | nRoot = pOp->p2; |
drh | 7906975 | 2004-05-22 21:30:40 +0000 | [diff] [blame] | 4940 | assert( nRoot>0 ); |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 4941 | aRoot = sqlite3DbMallocRaw(db, sizeof(int)*(nRoot+1) ); |
drh | caec2f1 | 2003-01-07 02:47:47 +0000 | [diff] [blame] | 4942 | if( aRoot==0 ) goto no_mem; |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4943 | assert( pOp->p3>0 && pOp->p3<=p->nMem ); |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 4944 | pnErr = &aMem[pOp->p3]; |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 4945 | assert( (pnErr->flags & MEM_Int)!=0 ); |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4946 | assert( (pnErr->flags & (MEM_Str|MEM_Blob))==0 ); |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 4947 | pIn1 = &aMem[pOp->p1]; |
drh | 7906975 | 2004-05-22 21:30:40 +0000 | [diff] [blame] | 4948 | for(j=0; j<nRoot; j++){ |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 4949 | aRoot[j] = (int)sqlite3VdbeIntValue(&pIn1[j]); |
drh | 1dd397f | 2002-02-03 03:34:07 +0000 | [diff] [blame] | 4950 | } |
| 4951 | aRoot[j] = 0; |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4952 | assert( pOp->p5<db->nDb ); |
drh | dddd779 | 2011-04-03 18:19:25 +0000 | [diff] [blame] | 4953 | assert( (p->btreeMask & (((yDbMask)1)<<pOp->p5))!=0 ); |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4954 | z = sqlite3BtreeIntegrityCheck(db->aDb[pOp->p5].pBt, aRoot, nRoot, |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 4955 | (int)pnErr->u.i, &nErr); |
drh | c890fec | 2008-08-01 20:10:08 +0000 | [diff] [blame] | 4956 | sqlite3DbFree(db, aRoot); |
drh | 3c024d6 | 2007-03-30 11:23:45 +0000 | [diff] [blame] | 4957 | pnErr->u.i -= nErr; |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 4958 | sqlite3VdbeMemSetNull(pIn1); |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 4959 | if( nErr==0 ){ |
| 4960 | assert( z==0 ); |
drh | c890fec | 2008-08-01 20:10:08 +0000 | [diff] [blame] | 4961 | }else if( z==0 ){ |
| 4962 | goto no_mem; |
drh | 1dd397f | 2002-02-03 03:34:07 +0000 | [diff] [blame] | 4963 | }else{ |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 4964 | sqlite3VdbeMemSetStr(pIn1, z, -1, SQLITE_UTF8, sqlite3_free); |
danielk1977 | 8a6b541 | 2004-05-24 07:04:25 +0000 | [diff] [blame] | 4965 | } |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 4966 | UPDATE_MAX_BLOBSIZE(pIn1); |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4967 | sqlite3VdbeChangeEncoding(pIn1, encoding); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4968 | break; |
| 4969 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 4970 | #endif /* SQLITE_OMIT_INTEGRITY_CHECK */ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4971 | |
drh | 3d4501e | 2008-12-04 20:40:10 +0000 | [diff] [blame] | 4972 | /* Opcode: RowSetAdd P1 P2 * * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4973 | ** |
drh | 3d4501e | 2008-12-04 20:40:10 +0000 | [diff] [blame] | 4974 | ** Insert the integer value held by register P2 into a boolean index |
| 4975 | ** held in register P1. |
| 4976 | ** |
| 4977 | ** An assertion fails if P2 is not an integer. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4978 | */ |
drh | 93952eb | 2009-11-13 19:43:43 +0000 | [diff] [blame] | 4979 | case OP_RowSetAdd: { /* in1, in2 */ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 4980 | pIn1 = &aMem[pOp->p1]; |
| 4981 | pIn2 = &aMem[pOp->p2]; |
drh | 93952eb | 2009-11-13 19:43:43 +0000 | [diff] [blame] | 4982 | assert( (pIn2->flags & MEM_Int)!=0 ); |
| 4983 | if( (pIn1->flags & MEM_RowSet)==0 ){ |
| 4984 | sqlite3VdbeMemSetRowSet(pIn1); |
| 4985 | if( (pIn1->flags & MEM_RowSet)==0 ) goto no_mem; |
drh | 3d4501e | 2008-12-04 20:40:10 +0000 | [diff] [blame] | 4986 | } |
drh | 93952eb | 2009-11-13 19:43:43 +0000 | [diff] [blame] | 4987 | sqlite3RowSetInsert(pIn1->u.pRowSet, pIn2->u.i); |
drh | 3d4501e | 2008-12-04 20:40:10 +0000 | [diff] [blame] | 4988 | break; |
| 4989 | } |
| 4990 | |
| 4991 | /* Opcode: RowSetRead P1 P2 P3 * * |
| 4992 | ** |
| 4993 | ** Extract the smallest value from boolean index P1 and put that value into |
| 4994 | ** register P3. Or, if boolean index P1 is initially empty, leave P3 |
| 4995 | ** unchanged and jump to instruction P2. |
| 4996 | */ |
drh | 93952eb | 2009-11-13 19:43:43 +0000 | [diff] [blame] | 4997 | case OP_RowSetRead: { /* jump, in1, out3 */ |
drh | 3d4501e | 2008-12-04 20:40:10 +0000 | [diff] [blame] | 4998 | i64 val; |
drh | 3d4501e | 2008-12-04 20:40:10 +0000 | [diff] [blame] | 4999 | CHECK_FOR_INTERRUPT; |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 5000 | pIn1 = &aMem[pOp->p1]; |
drh | 93952eb | 2009-11-13 19:43:43 +0000 | [diff] [blame] | 5001 | if( (pIn1->flags & MEM_RowSet)==0 |
| 5002 | || sqlite3RowSetNext(pIn1->u.pRowSet, &val)==0 |
drh | 3d4501e | 2008-12-04 20:40:10 +0000 | [diff] [blame] | 5003 | ){ |
| 5004 | /* The boolean index is empty */ |
drh | 93952eb | 2009-11-13 19:43:43 +0000 | [diff] [blame] | 5005 | sqlite3VdbeMemSetNull(pIn1); |
drh | 3d4501e | 2008-12-04 20:40:10 +0000 | [diff] [blame] | 5006 | pc = pOp->p2 - 1; |
| 5007 | }else{ |
| 5008 | /* A value was pulled from the index */ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 5009 | sqlite3VdbeMemSetInt64(&aMem[pOp->p3], val); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 5010 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5011 | break; |
| 5012 | } |
| 5013 | |
drh | 1b26c7c | 2009-04-22 02:15:47 +0000 | [diff] [blame] | 5014 | /* Opcode: RowSetTest P1 P2 P3 P4 |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 5015 | ** |
drh | ade9760 | 2009-04-21 15:05:18 +0000 | [diff] [blame] | 5016 | ** Register P3 is assumed to hold a 64-bit integer value. If register P1 |
drh | 1b26c7c | 2009-04-22 02:15:47 +0000 | [diff] [blame] | 5017 | ** contains a RowSet object and that RowSet object contains |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 5018 | ** the value held in P3, jump to register P2. Otherwise, insert the |
drh | 1b26c7c | 2009-04-22 02:15:47 +0000 | [diff] [blame] | 5019 | ** integer in P3 into the RowSet and continue on to the |
drh | ade9760 | 2009-04-21 15:05:18 +0000 | [diff] [blame] | 5020 | ** next opcode. |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 5021 | ** |
drh | 1b26c7c | 2009-04-22 02:15:47 +0000 | [diff] [blame] | 5022 | ** The RowSet object is optimized for the case where successive sets |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 5023 | ** of integers, where each set contains no duplicates. Each set |
| 5024 | ** of values is identified by a unique P4 value. The first set |
drh | 1b26c7c | 2009-04-22 02:15:47 +0000 | [diff] [blame] | 5025 | ** must have P4==0, the final set P4=-1. P4 must be either -1 or |
| 5026 | ** non-negative. For non-negative values of P4 only the lower 4 |
| 5027 | ** bits are significant. |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 5028 | ** |
| 5029 | ** This allows optimizations: (a) when P4==0 there is no need to test |
drh | 1b26c7c | 2009-04-22 02:15:47 +0000 | [diff] [blame] | 5030 | ** the rowset object for P3, as it is guaranteed not to contain it, |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 5031 | ** (b) when P4==-1 there is no need to insert the value, as it will |
| 5032 | ** never be tested for, and (c) when a value that is part of set X is |
| 5033 | ** inserted, there is no need to search to see if the same value was |
| 5034 | ** previously inserted as part of set X (only if it was previously |
| 5035 | ** inserted as part of some other set). |
| 5036 | */ |
drh | 1b26c7c | 2009-04-22 02:15:47 +0000 | [diff] [blame] | 5037 | case OP_RowSetTest: { /* jump, in1, in3 */ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 5038 | int iSet; |
| 5039 | int exists; |
| 5040 | |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 5041 | pIn1 = &aMem[pOp->p1]; |
| 5042 | pIn3 = &aMem[pOp->p3]; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 5043 | iSet = pOp->p4.i; |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 5044 | assert( pIn3->flags&MEM_Int ); |
| 5045 | |
drh | 1b26c7c | 2009-04-22 02:15:47 +0000 | [diff] [blame] | 5046 | /* If there is anything other than a rowset object in memory cell P1, |
| 5047 | ** delete it now and initialize P1 with an empty rowset |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 5048 | */ |
drh | 733bf1b | 2009-04-22 00:47:00 +0000 | [diff] [blame] | 5049 | if( (pIn1->flags & MEM_RowSet)==0 ){ |
| 5050 | sqlite3VdbeMemSetRowSet(pIn1); |
| 5051 | if( (pIn1->flags & MEM_RowSet)==0 ) goto no_mem; |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 5052 | } |
| 5053 | |
| 5054 | assert( pOp->p4type==P4_INT32 ); |
drh | 1b26c7c | 2009-04-22 02:15:47 +0000 | [diff] [blame] | 5055 | assert( iSet==-1 || iSet>=0 ); |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 5056 | if( iSet ){ |
shane | 60a4b53 | 2009-05-06 18:57:09 +0000 | [diff] [blame] | 5057 | exists = sqlite3RowSetTest(pIn1->u.pRowSet, |
| 5058 | (u8)(iSet>=0 ? iSet & 0xf : 0xff), |
drh | 733bf1b | 2009-04-22 00:47:00 +0000 | [diff] [blame] | 5059 | pIn3->u.i); |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 5060 | if( exists ){ |
| 5061 | pc = pOp->p2 - 1; |
| 5062 | break; |
| 5063 | } |
| 5064 | } |
| 5065 | if( iSet>=0 ){ |
drh | 733bf1b | 2009-04-22 00:47:00 +0000 | [diff] [blame] | 5066 | sqlite3RowSetInsert(pIn1->u.pRowSet, pIn3->u.i); |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 5067 | } |
| 5068 | break; |
| 5069 | } |
| 5070 | |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5071 | |
danielk1977 | 93758c8 | 2005-01-21 08:13:14 +0000 | [diff] [blame] | 5072 | #ifndef SQLITE_OMIT_TRIGGER |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 5073 | |
| 5074 | /* Opcode: Program P1 P2 P3 P4 * |
| 5075 | ** |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 5076 | ** Execute the trigger program passed as P4 (type P4_SUBPROGRAM). |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 5077 | ** |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 5078 | ** P1 contains the address of the memory cell that contains the first memory |
| 5079 | ** cell in an array of values used as arguments to the sub-program. P2 |
| 5080 | ** contains the address to jump to if the sub-program throws an IGNORE |
| 5081 | ** exception using the RAISE() function. Register P3 contains the address |
| 5082 | ** of a memory cell in this (the parent) VM that is used to allocate the |
| 5083 | ** memory required by the sub-vdbe at runtime. |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 5084 | ** |
| 5085 | ** P4 is a pointer to the VM containing the trigger program. |
| 5086 | */ |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 5087 | case OP_Program: { /* jump */ |
dan | 65a7cd1 | 2009-09-01 12:16:01 +0000 | [diff] [blame] | 5088 | int nMem; /* Number of memory registers for sub-program */ |
| 5089 | int nByte; /* Bytes of runtime space required for sub-program */ |
| 5090 | Mem *pRt; /* Register to allocate runtime space */ |
| 5091 | Mem *pMem; /* Used to iterate through memory cells */ |
| 5092 | Mem *pEnd; /* Last memory cell in new array */ |
| 5093 | VdbeFrame *pFrame; /* New vdbe frame to execute in */ |
| 5094 | SubProgram *pProgram; /* Sub-program to execute */ |
| 5095 | void *t; /* Token identifying trigger */ |
| 5096 | |
| 5097 | pProgram = pOp->p4.pProgram; |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 5098 | pRt = &aMem[pOp->p3]; |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 5099 | assert( memIsValid(pRt) ); |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 5100 | assert( pProgram->nOp>0 ); |
| 5101 | |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 5102 | /* If the p5 flag is clear, then recursive invocation of triggers is |
| 5103 | ** disabled for backwards compatibility (p5 is set if this sub-program |
| 5104 | ** is really a trigger, not a foreign key action, and the flag set |
| 5105 | ** and cleared by the "PRAGMA recursive_triggers" command is clear). |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 5106 | ** |
| 5107 | ** It is recursive invocation of triggers, at the SQL level, that is |
| 5108 | ** disabled. In some cases a single trigger may generate more than one |
| 5109 | ** SubProgram (if the trigger may be executed with more than one different |
| 5110 | ** ON CONFLICT algorithm). SubProgram structures associated with a |
| 5111 | ** single trigger all have the same value for the SubProgram.token |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 5112 | ** variable. */ |
| 5113 | if( pOp->p5 ){ |
dan | 65a7cd1 | 2009-09-01 12:16:01 +0000 | [diff] [blame] | 5114 | t = pProgram->token; |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 5115 | for(pFrame=p->pFrame; pFrame && pFrame->token!=t; pFrame=pFrame->pParent); |
| 5116 | if( pFrame ) break; |
| 5117 | } |
| 5118 | |
dan | f589450 | 2009-10-07 18:41:19 +0000 | [diff] [blame] | 5119 | if( p->nFrame>=db->aLimit[SQLITE_LIMIT_TRIGGER_DEPTH] ){ |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 5120 | rc = SQLITE_ERROR; |
| 5121 | sqlite3SetString(&p->zErrMsg, db, "too many levels of trigger recursion"); |
| 5122 | break; |
| 5123 | } |
| 5124 | |
| 5125 | /* Register pRt is used to store the memory required to save the state |
| 5126 | ** of the current program, and the memory required at runtime to execute |
| 5127 | ** the trigger program. If this trigger has been fired before, then pRt |
| 5128 | ** is already allocated. Otherwise, it must be initialized. */ |
| 5129 | if( (pRt->flags&MEM_Frame)==0 ){ |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 5130 | /* SubProgram.nMem is set to the number of memory cells used by the |
| 5131 | ** program stored in SubProgram.aOp. As well as these, one memory |
| 5132 | ** cell is required for each cursor used by the program. Set local |
| 5133 | ** variable nMem (and later, VdbeFrame.nChildMem) to this value. |
| 5134 | */ |
dan | 65a7cd1 | 2009-09-01 12:16:01 +0000 | [diff] [blame] | 5135 | nMem = pProgram->nMem + pProgram->nCsr; |
| 5136 | nByte = ROUND8(sizeof(VdbeFrame)) |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 5137 | + nMem * sizeof(Mem) |
| 5138 | + pProgram->nCsr * sizeof(VdbeCursor *); |
| 5139 | pFrame = sqlite3DbMallocZero(db, nByte); |
| 5140 | if( !pFrame ){ |
| 5141 | goto no_mem; |
| 5142 | } |
| 5143 | sqlite3VdbeMemRelease(pRt); |
| 5144 | pRt->flags = MEM_Frame; |
| 5145 | pRt->u.pFrame = pFrame; |
| 5146 | |
| 5147 | pFrame->v = p; |
| 5148 | pFrame->nChildMem = nMem; |
| 5149 | pFrame->nChildCsr = pProgram->nCsr; |
| 5150 | pFrame->pc = pc; |
| 5151 | pFrame->aMem = p->aMem; |
| 5152 | pFrame->nMem = p->nMem; |
| 5153 | pFrame->apCsr = p->apCsr; |
| 5154 | pFrame->nCursor = p->nCursor; |
| 5155 | pFrame->aOp = p->aOp; |
| 5156 | pFrame->nOp = p->nOp; |
| 5157 | pFrame->token = pProgram->token; |
| 5158 | |
| 5159 | pEnd = &VdbeFrameMem(pFrame)[pFrame->nChildMem]; |
| 5160 | for(pMem=VdbeFrameMem(pFrame); pMem!=pEnd; pMem++){ |
| 5161 | pMem->flags = MEM_Null; |
| 5162 | pMem->db = db; |
| 5163 | } |
| 5164 | }else{ |
| 5165 | pFrame = pRt->u.pFrame; |
| 5166 | assert( pProgram->nMem+pProgram->nCsr==pFrame->nChildMem ); |
| 5167 | assert( pProgram->nCsr==pFrame->nChildCsr ); |
| 5168 | assert( pc==pFrame->pc ); |
| 5169 | } |
| 5170 | |
| 5171 | p->nFrame++; |
| 5172 | pFrame->pParent = p->pFrame; |
drh | 99a6692 | 2011-05-13 18:51:42 +0000 | [diff] [blame] | 5173 | pFrame->lastRowid = lastRowid; |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 5174 | pFrame->nChange = p->nChange; |
dan | 2832ad4 | 2009-08-31 15:27:27 +0000 | [diff] [blame] | 5175 | p->nChange = 0; |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 5176 | p->pFrame = pFrame; |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 5177 | p->aMem = aMem = &VdbeFrameMem(pFrame)[-1]; |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 5178 | p->nMem = pFrame->nChildMem; |
shane | cea72b2 | 2009-09-07 04:38:36 +0000 | [diff] [blame] | 5179 | p->nCursor = (u16)pFrame->nChildCsr; |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 5180 | p->apCsr = (VdbeCursor **)&aMem[p->nMem+1]; |
drh | bbe879d | 2009-11-14 18:04:35 +0000 | [diff] [blame] | 5181 | p->aOp = aOp = pProgram->aOp; |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 5182 | p->nOp = pProgram->nOp; |
| 5183 | pc = -1; |
| 5184 | |
| 5185 | break; |
| 5186 | } |
| 5187 | |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 5188 | /* Opcode: Param P1 P2 * * * |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 5189 | ** |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 5190 | ** This opcode is only ever present in sub-programs called via the |
| 5191 | ** OP_Program instruction. Copy a value currently stored in a memory |
| 5192 | ** cell of the calling (parent) frame to cell P2 in the current frames |
| 5193 | ** address space. This is used by trigger programs to access the new.* |
| 5194 | ** and old.* values. |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 5195 | ** |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 5196 | ** The address of the cell in the parent frame is determined by adding |
| 5197 | ** the value of the P1 argument to the value of the P1 argument to the |
| 5198 | ** calling OP_Program instruction. |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 5199 | */ |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 5200 | case OP_Param: { /* out2-prerelease */ |
dan | 65a7cd1 | 2009-09-01 12:16:01 +0000 | [diff] [blame] | 5201 | VdbeFrame *pFrame; |
| 5202 | Mem *pIn; |
| 5203 | pFrame = p->pFrame; |
| 5204 | pIn = &pFrame->aMem[pOp->p1 + pFrame->aOp[pFrame->pc].p1]; |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 5205 | sqlite3VdbeMemShallowCopy(pOut, pIn, MEM_Ephem); |
| 5206 | break; |
| 5207 | } |
| 5208 | |
danielk1977 | 93758c8 | 2005-01-21 08:13:14 +0000 | [diff] [blame] | 5209 | #endif /* #ifndef SQLITE_OMIT_TRIGGER */ |
rdc | b0c374f | 2004-02-20 22:53:38 +0000 | [diff] [blame] | 5210 | |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 5211 | #ifndef SQLITE_OMIT_FOREIGN_KEY |
dan | 32b09f2 | 2009-09-23 17:29:59 +0000 | [diff] [blame] | 5212 | /* Opcode: FkCounter P1 P2 * * * |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 5213 | ** |
dan | 0ff297e | 2009-09-25 17:03:14 +0000 | [diff] [blame] | 5214 | ** Increment a "constraint counter" by P2 (P2 may be negative or positive). |
| 5215 | ** If P1 is non-zero, the database constraint counter is incremented |
| 5216 | ** (deferred foreign key constraints). Otherwise, if P1 is zero, the |
dan | 32b09f2 | 2009-09-23 17:29:59 +0000 | [diff] [blame] | 5217 | ** statement counter is incremented (immediate foreign key constraints). |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 5218 | */ |
dan | 32b09f2 | 2009-09-23 17:29:59 +0000 | [diff] [blame] | 5219 | case OP_FkCounter: { |
dan | 0ff297e | 2009-09-25 17:03:14 +0000 | [diff] [blame] | 5220 | if( pOp->p1 ){ |
| 5221 | db->nDeferredCons += pOp->p2; |
dan | 32b09f2 | 2009-09-23 17:29:59 +0000 | [diff] [blame] | 5222 | }else{ |
dan | 0ff297e | 2009-09-25 17:03:14 +0000 | [diff] [blame] | 5223 | p->nFkConstraint += pOp->p2; |
| 5224 | } |
| 5225 | break; |
| 5226 | } |
| 5227 | |
| 5228 | /* Opcode: FkIfZero P1 P2 * * * |
| 5229 | ** |
| 5230 | ** This opcode tests if a foreign key constraint-counter is currently zero. |
| 5231 | ** If so, jump to instruction P2. Otherwise, fall through to the next |
| 5232 | ** instruction. |
| 5233 | ** |
| 5234 | ** If P1 is non-zero, then the jump is taken if the database constraint-counter |
| 5235 | ** is zero (the one that counts deferred constraint violations). If P1 is |
| 5236 | ** zero, the jump is taken if the statement constraint-counter is zero |
| 5237 | ** (immediate foreign key constraint violations). |
| 5238 | */ |
| 5239 | case OP_FkIfZero: { /* jump */ |
| 5240 | if( pOp->p1 ){ |
| 5241 | if( db->nDeferredCons==0 ) pc = pOp->p2-1; |
| 5242 | }else{ |
| 5243 | if( p->nFkConstraint==0 ) pc = pOp->p2-1; |
dan | 32b09f2 | 2009-09-23 17:29:59 +0000 | [diff] [blame] | 5244 | } |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 5245 | break; |
| 5246 | } |
| 5247 | #endif /* #ifndef SQLITE_OMIT_FOREIGN_KEY */ |
| 5248 | |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 5249 | #ifndef SQLITE_OMIT_AUTOINCREMENT |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 5250 | /* Opcode: MemMax P1 P2 * * * |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 5251 | ** |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 5252 | ** P1 is a register in the root frame of this VM (the root frame is |
| 5253 | ** different from the current frame if this instruction is being executed |
| 5254 | ** within a sub-program). Set the value of register P1 to the maximum of |
| 5255 | ** its current value and the value in register P2. |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 5256 | ** |
| 5257 | ** This instruction throws an error if the memory cell is not initially |
| 5258 | ** an integer. |
| 5259 | */ |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 5260 | case OP_MemMax: { /* in2 */ |
| 5261 | Mem *pIn1; |
| 5262 | VdbeFrame *pFrame; |
| 5263 | if( p->pFrame ){ |
| 5264 | for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent); |
| 5265 | pIn1 = &pFrame->aMem[pOp->p1]; |
| 5266 | }else{ |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 5267 | pIn1 = &aMem[pOp->p1]; |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 5268 | } |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 5269 | assert( memIsValid(pIn1) ); |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 5270 | sqlite3VdbeMemIntegerify(pIn1); |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 5271 | pIn2 = &aMem[pOp->p2]; |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 5272 | sqlite3VdbeMemIntegerify(pIn2); |
| 5273 | if( pIn1->u.i<pIn2->u.i){ |
| 5274 | pIn1->u.i = pIn2->u.i; |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 5275 | } |
| 5276 | break; |
| 5277 | } |
| 5278 | #endif /* SQLITE_OMIT_AUTOINCREMENT */ |
| 5279 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 5280 | /* Opcode: IfPos P1 P2 * * * |
danielk1977 | a2dc3b1 | 2005-02-05 12:48:48 +0000 | [diff] [blame] | 5281 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 5282 | ** If the value of register P1 is 1 or greater, jump to P2. |
drh | 6f58f70 | 2006-01-08 05:26:41 +0000 | [diff] [blame] | 5283 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 5284 | ** It is illegal to use this instruction on a register that does |
drh | 6f58f70 | 2006-01-08 05:26:41 +0000 | [diff] [blame] | 5285 | ** not contain an integer. An assertion fault will result if you try. |
danielk1977 | a2dc3b1 | 2005-02-05 12:48:48 +0000 | [diff] [blame] | 5286 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 5287 | case OP_IfPos: { /* jump, in1 */ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 5288 | pIn1 = &aMem[pOp->p1]; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 5289 | assert( pIn1->flags&MEM_Int ); |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 5290 | if( pIn1->u.i>0 ){ |
drh | ec7429a | 2005-10-06 16:53:14 +0000 | [diff] [blame] | 5291 | pc = pOp->p2 - 1; |
| 5292 | } |
| 5293 | break; |
| 5294 | } |
| 5295 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 5296 | /* Opcode: IfNeg P1 P2 * * * |
drh | 15007a9 | 2006-01-08 18:10:17 +0000 | [diff] [blame] | 5297 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 5298 | ** If the value of register P1 is less than zero, jump to P2. |
drh | 15007a9 | 2006-01-08 18:10:17 +0000 | [diff] [blame] | 5299 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 5300 | ** It is illegal to use this instruction on a register that does |
drh | 15007a9 | 2006-01-08 18:10:17 +0000 | [diff] [blame] | 5301 | ** not contain an integer. An assertion fault will result if you try. |
| 5302 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 5303 | case OP_IfNeg: { /* jump, in1 */ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 5304 | pIn1 = &aMem[pOp->p1]; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 5305 | assert( pIn1->flags&MEM_Int ); |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 5306 | if( pIn1->u.i<0 ){ |
drh | 15007a9 | 2006-01-08 18:10:17 +0000 | [diff] [blame] | 5307 | pc = pOp->p2 - 1; |
| 5308 | } |
| 5309 | break; |
| 5310 | } |
| 5311 | |
drh | 9b918ed | 2009-11-12 03:13:26 +0000 | [diff] [blame] | 5312 | /* Opcode: IfZero P1 P2 P3 * * |
drh | ec7429a | 2005-10-06 16:53:14 +0000 | [diff] [blame] | 5313 | ** |
drh | 9b918ed | 2009-11-12 03:13:26 +0000 | [diff] [blame] | 5314 | ** The register P1 must contain an integer. Add literal P3 to the |
| 5315 | ** value in register P1. If the result is exactly 0, jump to P2. |
drh | 6f58f70 | 2006-01-08 05:26:41 +0000 | [diff] [blame] | 5316 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 5317 | ** It is illegal to use this instruction on a register that does |
drh | 6f58f70 | 2006-01-08 05:26:41 +0000 | [diff] [blame] | 5318 | ** not contain an integer. An assertion fault will result if you try. |
drh | ec7429a | 2005-10-06 16:53:14 +0000 | [diff] [blame] | 5319 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 5320 | case OP_IfZero: { /* jump, in1 */ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 5321 | pIn1 = &aMem[pOp->p1]; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 5322 | assert( pIn1->flags&MEM_Int ); |
drh | 9b918ed | 2009-11-12 03:13:26 +0000 | [diff] [blame] | 5323 | pIn1->u.i += pOp->p3; |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 5324 | if( pIn1->u.i==0 ){ |
drh | a2a49dc | 2008-01-02 14:28:13 +0000 | [diff] [blame] | 5325 | pc = pOp->p2 - 1; |
| 5326 | } |
| 5327 | break; |
| 5328 | } |
| 5329 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 5330 | /* Opcode: AggStep * P2 P3 P4 P5 |
drh | e509535 | 2002-02-24 03:25:14 +0000 | [diff] [blame] | 5331 | ** |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 5332 | ** Execute the step function for an aggregate. The |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 5333 | ** function has P5 arguments. P4 is a pointer to the FuncDef |
| 5334 | ** structure that specifies the function. Use register |
| 5335 | ** P3 as the accumulator. |
drh | e509535 | 2002-02-24 03:25:14 +0000 | [diff] [blame] | 5336 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 5337 | ** The P5 arguments are taken from register P2 and its |
| 5338 | ** successors. |
drh | e509535 | 2002-02-24 03:25:14 +0000 | [diff] [blame] | 5339 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 5340 | case OP_AggStep: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 5341 | int n; |
drh | e509535 | 2002-02-24 03:25:14 +0000 | [diff] [blame] | 5342 | int i; |
drh | c54a617 | 2009-06-02 16:06:03 +0000 | [diff] [blame] | 5343 | Mem *pMem; |
| 5344 | Mem *pRec; |
danielk1977 | 22322fd | 2004-05-25 23:35:17 +0000 | [diff] [blame] | 5345 | sqlite3_context ctx; |
danielk1977 | 6ddcca5 | 2004-05-24 23:48:25 +0000 | [diff] [blame] | 5346 | sqlite3_value **apVal; |
drh | e509535 | 2002-02-24 03:25:14 +0000 | [diff] [blame] | 5347 | |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 5348 | n = pOp->p5; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 5349 | assert( n>=0 ); |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 5350 | pRec = &aMem[pOp->p2]; |
danielk1977 | 6ddcca5 | 2004-05-24 23:48:25 +0000 | [diff] [blame] | 5351 | apVal = p->apArg; |
| 5352 | assert( apVal || n==0 ); |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 5353 | for(i=0; i<n; i++, pRec++){ |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 5354 | assert( memIsValid(pRec) ); |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 5355 | apVal[i] = pRec; |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 5356 | memAboutToChange(p, pRec); |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 5357 | sqlite3VdbeMemStoreType(pRec); |
drh | e509535 | 2002-02-24 03:25:14 +0000 | [diff] [blame] | 5358 | } |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 5359 | ctx.pFunc = pOp->p4.pFunc; |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 5360 | assert( pOp->p3>0 && pOp->p3<=p->nMem ); |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 5361 | ctx.pMem = pMem = &aMem[pOp->p3]; |
drh | abfcea2 | 2005-09-06 20:36:48 +0000 | [diff] [blame] | 5362 | pMem->n++; |
drh | 90669c1 | 2006-01-20 15:45:36 +0000 | [diff] [blame] | 5363 | ctx.s.flags = MEM_Null; |
| 5364 | ctx.s.z = 0; |
danielk1977 | 5f09613 | 2008-03-28 15:44:09 +0000 | [diff] [blame] | 5365 | ctx.s.zMalloc = 0; |
drh | 90669c1 | 2006-01-20 15:45:36 +0000 | [diff] [blame] | 5366 | ctx.s.xDel = 0; |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 5367 | ctx.s.db = db; |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 5368 | ctx.isError = 0; |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 5369 | ctx.pColl = 0; |
drh | e82f5d0 | 2008-10-07 19:53:14 +0000 | [diff] [blame] | 5370 | if( ctx.pFunc->flags & SQLITE_FUNC_NEEDCOLL ){ |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 5371 | assert( pOp>p->aOp ); |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 5372 | assert( pOp[-1].p4type==P4_COLLSEQ ); |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 5373 | assert( pOp[-1].opcode==OP_CollSeq ); |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 5374 | ctx.pColl = pOp[-1].p4.pColl; |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 5375 | } |
drh | ee9ff67 | 2010-09-03 18:50:48 +0000 | [diff] [blame] | 5376 | (ctx.pFunc->xStep)(&ctx, n, apVal); /* IMP: R-24505-23230 */ |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 5377 | if( ctx.isError ){ |
drh | f089aa4 | 2008-07-08 19:34:06 +0000 | [diff] [blame] | 5378 | sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(&ctx.s)); |
drh | 69544ec | 2008-02-06 14:11:34 +0000 | [diff] [blame] | 5379 | rc = ctx.isError; |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 5380 | } |
drh | bdaec52 | 2011-04-04 00:14:43 +0000 | [diff] [blame] | 5381 | |
drh | 90669c1 | 2006-01-20 15:45:36 +0000 | [diff] [blame] | 5382 | sqlite3VdbeMemRelease(&ctx.s); |
drh | bdaec52 | 2011-04-04 00:14:43 +0000 | [diff] [blame] | 5383 | |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5384 | break; |
| 5385 | } |
| 5386 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 5387 | /* Opcode: AggFinal P1 P2 * P4 * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5388 | ** |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 5389 | ** Execute the finalizer function for an aggregate. P1 is |
| 5390 | ** the memory location that is the accumulator for the aggregate. |
drh | a10a34b | 2005-09-07 22:09:48 +0000 | [diff] [blame] | 5391 | ** |
| 5392 | ** P2 is the number of arguments that the step function takes and |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 5393 | ** P4 is a pointer to the FuncDef for this function. The P2 |
drh | a10a34b | 2005-09-07 22:09:48 +0000 | [diff] [blame] | 5394 | ** argument is not used by this opcode. It is only there to disambiguate |
| 5395 | ** functions that can take varying numbers of arguments. The |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 5396 | ** P4 argument is only needed for the degenerate case where |
drh | a10a34b | 2005-09-07 22:09:48 +0000 | [diff] [blame] | 5397 | ** the step function was not previously called. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5398 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 5399 | case OP_AggFinal: { |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 5400 | Mem *pMem; |
drh | 0a07c10 | 2008-01-03 18:03:08 +0000 | [diff] [blame] | 5401 | assert( pOp->p1>0 && pOp->p1<=p->nMem ); |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 5402 | pMem = &aMem[pOp->p1]; |
drh | a10a34b | 2005-09-07 22:09:48 +0000 | [diff] [blame] | 5403 | assert( (pMem->flags & ~(MEM_Null|MEM_Agg))==0 ); |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 5404 | rc = sqlite3VdbeMemFinalize(pMem, pOp->p4.pFunc); |
drh | 4c8555f | 2009-06-25 01:47:11 +0000 | [diff] [blame] | 5405 | if( rc ){ |
drh | f089aa4 | 2008-07-08 19:34:06 +0000 | [diff] [blame] | 5406 | sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(pMem)); |
drh | 90669c1 | 2006-01-20 15:45:36 +0000 | [diff] [blame] | 5407 | } |
drh | 2dca868 | 2008-03-21 17:13:13 +0000 | [diff] [blame] | 5408 | sqlite3VdbeChangeEncoding(pMem, encoding); |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 5409 | UPDATE_MAX_BLOBSIZE(pMem); |
drh | 023ae03 | 2007-05-08 12:12:16 +0000 | [diff] [blame] | 5410 | if( sqlite3VdbeMemTooBig(pMem) ){ |
| 5411 | goto too_big; |
| 5412 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5413 | break; |
| 5414 | } |
| 5415 | |
dan | 5cf5353 | 2010-05-01 16:40:20 +0000 | [diff] [blame] | 5416 | #ifndef SQLITE_OMIT_WAL |
dan | cdc1f04 | 2010-11-18 12:11:05 +0000 | [diff] [blame] | 5417 | /* Opcode: Checkpoint P1 P2 P3 * * |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 5418 | ** |
| 5419 | ** Checkpoint database P1. This is a no-op if P1 is not currently in |
dan | cdc1f04 | 2010-11-18 12:11:05 +0000 | [diff] [blame] | 5420 | ** WAL mode. Parameter P2 is one of SQLITE_CHECKPOINT_PASSIVE, FULL |
drh | 30aa3b9 | 2011-02-07 23:56:01 +0000 | [diff] [blame] | 5421 | ** or RESTART. Write 1 or 0 into mem[P3] if the checkpoint returns |
| 5422 | ** SQLITE_BUSY or not, respectively. Write the number of pages in the |
| 5423 | ** WAL after the checkpoint into mem[P3+1] and the number of pages |
| 5424 | ** in the WAL that have been checkpointed after the checkpoint |
| 5425 | ** completes into mem[P3+2]. However on an error, mem[P3+1] and |
| 5426 | ** mem[P3+2] are initialized to -1. |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 5427 | */ |
| 5428 | case OP_Checkpoint: { |
drh | 30aa3b9 | 2011-02-07 23:56:01 +0000 | [diff] [blame] | 5429 | int i; /* Loop counter */ |
| 5430 | int aRes[3]; /* Results */ |
| 5431 | Mem *pMem; /* Write results here */ |
| 5432 | |
| 5433 | aRes[0] = 0; |
| 5434 | aRes[1] = aRes[2] = -1; |
dan | cdc1f04 | 2010-11-18 12:11:05 +0000 | [diff] [blame] | 5435 | assert( pOp->p2==SQLITE_CHECKPOINT_PASSIVE |
| 5436 | || pOp->p2==SQLITE_CHECKPOINT_FULL |
| 5437 | || pOp->p2==SQLITE_CHECKPOINT_RESTART |
| 5438 | ); |
drh | 30aa3b9 | 2011-02-07 23:56:01 +0000 | [diff] [blame] | 5439 | rc = sqlite3Checkpoint(db, pOp->p1, pOp->p2, &aRes[1], &aRes[2]); |
dan | cdc1f04 | 2010-11-18 12:11:05 +0000 | [diff] [blame] | 5440 | if( rc==SQLITE_BUSY ){ |
| 5441 | rc = SQLITE_OK; |
drh | 30aa3b9 | 2011-02-07 23:56:01 +0000 | [diff] [blame] | 5442 | aRes[0] = 1; |
dan | cdc1f04 | 2010-11-18 12:11:05 +0000 | [diff] [blame] | 5443 | } |
drh | 30aa3b9 | 2011-02-07 23:56:01 +0000 | [diff] [blame] | 5444 | for(i=0, pMem = &aMem[pOp->p3]; i<3; i++, pMem++){ |
| 5445 | sqlite3VdbeMemSetInt64(pMem, (i64)aRes[i]); |
| 5446 | } |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 5447 | break; |
| 5448 | }; |
dan | 5cf5353 | 2010-05-01 16:40:20 +0000 | [diff] [blame] | 5449 | #endif |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5450 | |
drh | cac29a6 | 2010-07-02 19:36:52 +0000 | [diff] [blame] | 5451 | #ifndef SQLITE_OMIT_PRAGMA |
drh | ab9b744 | 2010-05-10 11:20:05 +0000 | [diff] [blame] | 5452 | /* Opcode: JournalMode P1 P2 P3 * P5 |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 5453 | ** |
| 5454 | ** Change the journal mode of database P1 to P3. P3 must be one of the |
| 5455 | ** PAGER_JOURNALMODE_XXX values. If changing between the various rollback |
| 5456 | ** modes (delete, truncate, persist, off and memory), this is a simple |
| 5457 | ** operation. No IO is required. |
| 5458 | ** |
| 5459 | ** If changing into or out of WAL mode the procedure is more complicated. |
| 5460 | ** |
| 5461 | ** Write a string containing the final journal-mode to register P2. |
| 5462 | */ |
drh | d80b233 | 2010-05-01 00:59:37 +0000 | [diff] [blame] | 5463 | case OP_JournalMode: { /* out2-prerelease */ |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 5464 | Btree *pBt; /* Btree to change journal mode of */ |
| 5465 | Pager *pPager; /* Pager associated with pBt */ |
drh | d80b233 | 2010-05-01 00:59:37 +0000 | [diff] [blame] | 5466 | int eNew; /* New journal mode */ |
| 5467 | int eOld; /* The old journal mode */ |
drh | d80b233 | 2010-05-01 00:59:37 +0000 | [diff] [blame] | 5468 | const char *zFilename; /* Name of database file for pPager */ |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 5469 | |
drh | d80b233 | 2010-05-01 00:59:37 +0000 | [diff] [blame] | 5470 | eNew = pOp->p3; |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 5471 | assert( eNew==PAGER_JOURNALMODE_DELETE |
| 5472 | || eNew==PAGER_JOURNALMODE_TRUNCATE |
| 5473 | || eNew==PAGER_JOURNALMODE_PERSIST |
| 5474 | || eNew==PAGER_JOURNALMODE_OFF |
| 5475 | || eNew==PAGER_JOURNALMODE_MEMORY |
| 5476 | || eNew==PAGER_JOURNALMODE_WAL |
| 5477 | || eNew==PAGER_JOURNALMODE_QUERY |
| 5478 | ); |
| 5479 | assert( pOp->p1>=0 && pOp->p1<db->nDb ); |
drh | 3ebaee9 | 2010-05-06 21:37:22 +0000 | [diff] [blame] | 5480 | |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 5481 | pBt = db->aDb[pOp->p1].pBt; |
| 5482 | pPager = sqlite3BtreePager(pBt); |
drh | 0b9b430 | 2010-06-11 17:01:24 +0000 | [diff] [blame] | 5483 | eOld = sqlite3PagerGetJournalMode(pPager); |
| 5484 | if( eNew==PAGER_JOURNALMODE_QUERY ) eNew = eOld; |
| 5485 | if( !sqlite3PagerOkToChangeJournalMode(pPager) ) eNew = eOld; |
dan | 5cf5353 | 2010-05-01 16:40:20 +0000 | [diff] [blame] | 5486 | |
| 5487 | #ifndef SQLITE_OMIT_WAL |
drh | d80b233 | 2010-05-01 00:59:37 +0000 | [diff] [blame] | 5488 | zFilename = sqlite3PagerFilename(pPager); |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 5489 | |
drh | d80b233 | 2010-05-01 00:59:37 +0000 | [diff] [blame] | 5490 | /* Do not allow a transition to journal_mode=WAL for a database |
drh | 6e1f482 | 2010-07-13 23:41:40 +0000 | [diff] [blame] | 5491 | ** in temporary storage or if the VFS does not support shared memory |
drh | d80b233 | 2010-05-01 00:59:37 +0000 | [diff] [blame] | 5492 | */ |
| 5493 | if( eNew==PAGER_JOURNALMODE_WAL |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 5494 | && (zFilename[0]==0 /* Temp file */ |
drh | 6e1f482 | 2010-07-13 23:41:40 +0000 | [diff] [blame] | 5495 | || !sqlite3PagerWalSupported(pPager)) /* No shared-memory support */ |
dan | e180c29 | 2010-04-26 17:42:56 +0000 | [diff] [blame] | 5496 | ){ |
drh | 0b9b430 | 2010-06-11 17:01:24 +0000 | [diff] [blame] | 5497 | eNew = eOld; |
dan | e180c29 | 2010-04-26 17:42:56 +0000 | [diff] [blame] | 5498 | } |
| 5499 | |
drh | 0b9b430 | 2010-06-11 17:01:24 +0000 | [diff] [blame] | 5500 | if( (eNew!=eOld) |
| 5501 | && (eOld==PAGER_JOURNALMODE_WAL || eNew==PAGER_JOURNALMODE_WAL) |
| 5502 | ){ |
| 5503 | if( !db->autoCommit || db->activeVdbeCnt>1 ){ |
| 5504 | rc = SQLITE_ERROR; |
| 5505 | sqlite3SetString(&p->zErrMsg, db, |
| 5506 | "cannot change %s wal mode from within a transaction", |
| 5507 | (eNew==PAGER_JOURNALMODE_WAL ? "into" : "out of") |
| 5508 | ); |
| 5509 | break; |
| 5510 | }else{ |
| 5511 | |
| 5512 | if( eOld==PAGER_JOURNALMODE_WAL ){ |
| 5513 | /* If leaving WAL mode, close the log file. If successful, the call |
| 5514 | ** to PagerCloseWal() checkpoints and deletes the write-ahead-log |
| 5515 | ** file. An EXCLUSIVE lock may still be held on the database file |
| 5516 | ** after a successful return. |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 5517 | */ |
drh | 0b9b430 | 2010-06-11 17:01:24 +0000 | [diff] [blame] | 5518 | rc = sqlite3PagerCloseWal(pPager); |
drh | ab9b744 | 2010-05-10 11:20:05 +0000 | [diff] [blame] | 5519 | if( rc==SQLITE_OK ){ |
drh | 0b9b430 | 2010-06-11 17:01:24 +0000 | [diff] [blame] | 5520 | sqlite3PagerSetJournalMode(pPager, eNew); |
drh | 89c3f2f | 2010-05-15 01:09:38 +0000 | [diff] [blame] | 5521 | } |
drh | 242c4f7 | 2010-06-22 14:49:39 +0000 | [diff] [blame] | 5522 | }else if( eOld==PAGER_JOURNALMODE_MEMORY ){ |
| 5523 | /* Cannot transition directly from MEMORY to WAL. Use mode OFF |
| 5524 | ** as an intermediate */ |
| 5525 | sqlite3PagerSetJournalMode(pPager, PAGER_JOURNALMODE_OFF); |
drh | 0b9b430 | 2010-06-11 17:01:24 +0000 | [diff] [blame] | 5526 | } |
| 5527 | |
| 5528 | /* Open a transaction on the database file. Regardless of the journal |
| 5529 | ** mode, this transaction always uses a rollback journal. |
| 5530 | */ |
| 5531 | assert( sqlite3BtreeIsInTrans(pBt)==0 ); |
| 5532 | if( rc==SQLITE_OK ){ |
dan | 731bf5b | 2010-06-17 16:44:21 +0000 | [diff] [blame] | 5533 | rc = sqlite3BtreeSetVersion(pBt, (eNew==PAGER_JOURNALMODE_WAL ? 2 : 1)); |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 5534 | } |
| 5535 | } |
| 5536 | } |
dan | 5cf5353 | 2010-05-01 16:40:20 +0000 | [diff] [blame] | 5537 | #endif /* ifndef SQLITE_OMIT_WAL */ |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 5538 | |
dan | d956efe | 2010-06-18 16:13:45 +0000 | [diff] [blame] | 5539 | if( rc ){ |
dan | d956efe | 2010-06-18 16:13:45 +0000 | [diff] [blame] | 5540 | eNew = eOld; |
| 5541 | } |
drh | 0b9b430 | 2010-06-11 17:01:24 +0000 | [diff] [blame] | 5542 | eNew = sqlite3PagerSetJournalMode(pPager, eNew); |
dan | 731bf5b | 2010-06-17 16:44:21 +0000 | [diff] [blame] | 5543 | |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 5544 | pOut = &aMem[pOp->p2]; |
| 5545 | pOut->flags = MEM_Str|MEM_Static|MEM_Term; |
dan | b978002 | 2010-04-21 18:37:57 +0000 | [diff] [blame] | 5546 | pOut->z = (char *)sqlite3JournalModename(eNew); |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 5547 | pOut->n = sqlite3Strlen30(pOut->z); |
| 5548 | pOut->enc = SQLITE_UTF8; |
| 5549 | sqlite3VdbeChangeEncoding(pOut, encoding); |
| 5550 | break; |
drh | cac29a6 | 2010-07-02 19:36:52 +0000 | [diff] [blame] | 5551 | }; |
| 5552 | #endif /* SQLITE_OMIT_PRAGMA */ |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 5553 | |
drh | fdbcdee | 2007-03-27 14:44:50 +0000 | [diff] [blame] | 5554 | #if !defined(SQLITE_OMIT_VACUUM) && !defined(SQLITE_OMIT_ATTACH) |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 5555 | /* Opcode: Vacuum * * * * * |
drh | 6f8c91c | 2003-12-07 00:24:35 +0000 | [diff] [blame] | 5556 | ** |
| 5557 | ** Vacuum the entire database. This opcode will cause other virtual |
| 5558 | ** machines to be created and run. It may not be called from within |
| 5559 | ** a transaction. |
| 5560 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 5561 | case OP_Vacuum: { |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 5562 | rc = sqlite3RunVacuum(&p->zErrMsg, db); |
drh | 6f8c91c | 2003-12-07 00:24:35 +0000 | [diff] [blame] | 5563 | break; |
| 5564 | } |
drh | 154d4b2 | 2006-09-21 11:02:16 +0000 | [diff] [blame] | 5565 | #endif |
drh | 6f8c91c | 2003-12-07 00:24:35 +0000 | [diff] [blame] | 5566 | |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 5567 | #if !defined(SQLITE_OMIT_AUTOVACUUM) |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 5568 | /* Opcode: IncrVacuum P1 P2 * * * |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 5569 | ** |
| 5570 | ** Perform a single step of the incremental vacuum procedure on |
drh | ca5557f | 2007-05-04 18:30:40 +0000 | [diff] [blame] | 5571 | ** the P1 database. If the vacuum has finished, jump to instruction |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 5572 | ** P2. Otherwise, fall through to the next instruction. |
| 5573 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 5574 | case OP_IncrVacuum: { /* jump */ |
drh | ca5557f | 2007-05-04 18:30:40 +0000 | [diff] [blame] | 5575 | Btree *pBt; |
| 5576 | |
| 5577 | assert( pOp->p1>=0 && pOp->p1<db->nDb ); |
drh | dddd779 | 2011-04-03 18:19:25 +0000 | [diff] [blame] | 5578 | assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 ); |
drh | ca5557f | 2007-05-04 18:30:40 +0000 | [diff] [blame] | 5579 | pBt = db->aDb[pOp->p1].pBt; |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 5580 | rc = sqlite3BtreeIncrVacuum(pBt); |
| 5581 | if( rc==SQLITE_DONE ){ |
| 5582 | pc = pOp->p2 - 1; |
| 5583 | rc = SQLITE_OK; |
| 5584 | } |
| 5585 | break; |
| 5586 | } |
| 5587 | #endif |
| 5588 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 5589 | /* Opcode: Expire P1 * * * * |
danielk1977 | a21c6b6 | 2005-01-24 10:25:59 +0000 | [diff] [blame] | 5590 | ** |
| 5591 | ** Cause precompiled statements to become expired. An expired statement |
| 5592 | ** fails with an error code of SQLITE_SCHEMA if it is ever executed |
| 5593 | ** (via sqlite3_step()). |
| 5594 | ** |
| 5595 | ** If P1 is 0, then all SQL statements become expired. If P1 is non-zero, |
| 5596 | ** then only the currently executing statement is affected. |
| 5597 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 5598 | case OP_Expire: { |
danielk1977 | a21c6b6 | 2005-01-24 10:25:59 +0000 | [diff] [blame] | 5599 | if( !pOp->p1 ){ |
| 5600 | sqlite3ExpirePreparedStatements(db); |
| 5601 | }else{ |
| 5602 | p->expired = 1; |
| 5603 | } |
| 5604 | break; |
| 5605 | } |
| 5606 | |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 5607 | #ifndef SQLITE_OMIT_SHARED_CACHE |
drh | 6a9ad3d | 2008-04-02 16:29:30 +0000 | [diff] [blame] | 5608 | /* Opcode: TableLock P1 P2 P3 P4 * |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 5609 | ** |
| 5610 | ** Obtain a lock on a particular table. This instruction is only used when |
| 5611 | ** the shared-cache feature is enabled. |
| 5612 | ** |
danielk1977 | 96d48e9 | 2009-06-29 06:00:37 +0000 | [diff] [blame] | 5613 | ** P1 is the index of the database in sqlite3.aDb[] of the database |
drh | 6a9ad3d | 2008-04-02 16:29:30 +0000 | [diff] [blame] | 5614 | ** on which the lock is acquired. A readlock is obtained if P3==0 or |
| 5615 | ** a write lock if P3==1. |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 5616 | ** |
| 5617 | ** P2 contains the root-page of the table to lock. |
| 5618 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 5619 | ** 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] | 5620 | ** used to generate an error message if the lock cannot be obtained. |
| 5621 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 5622 | case OP_TableLock: { |
danielk1977 | e0d9e6f | 2009-07-03 16:25:06 +0000 | [diff] [blame] | 5623 | u8 isWriteLock = (u8)pOp->p3; |
| 5624 | if( isWriteLock || 0==(db->flags&SQLITE_ReadUncommitted) ){ |
| 5625 | int p1 = pOp->p1; |
| 5626 | assert( p1>=0 && p1<db->nDb ); |
drh | dddd779 | 2011-04-03 18:19:25 +0000 | [diff] [blame] | 5627 | assert( (p->btreeMask & (((yDbMask)1)<<p1))!=0 ); |
danielk1977 | e0d9e6f | 2009-07-03 16:25:06 +0000 | [diff] [blame] | 5628 | assert( isWriteLock==0 || isWriteLock==1 ); |
| 5629 | rc = sqlite3BtreeLockTable(db->aDb[p1].pBt, pOp->p2, isWriteLock); |
| 5630 | if( (rc&0xFF)==SQLITE_LOCKED ){ |
| 5631 | const char *z = pOp->p4.z; |
| 5632 | sqlite3SetString(&p->zErrMsg, db, "database table is locked: %s", z); |
| 5633 | } |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 5634 | } |
| 5635 | break; |
| 5636 | } |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 5637 | #endif /* SQLITE_OMIT_SHARED_CACHE */ |
| 5638 | |
| 5639 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 5640 | /* Opcode: VBegin * * * P4 * |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 5641 | ** |
danielk1977 | 3e3a84d | 2008-08-01 17:37:40 +0000 | [diff] [blame] | 5642 | ** P4 may be a pointer to an sqlite3_vtab structure. If so, call the |
| 5643 | ** xBegin method for that table. |
| 5644 | ** |
| 5645 | ** Also, whether or not P4 is set, check that this is not being called from |
danielk1977 | 404ca07 | 2009-03-16 13:19:36 +0000 | [diff] [blame] | 5646 | ** within a callback to a virtual table xSync() method. If it is, the error |
| 5647 | ** code will be set to SQLITE_LOCKED. |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 5648 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 5649 | case OP_VBegin: { |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 5650 | VTable *pVTab; |
| 5651 | pVTab = pOp->p4.pVtab; |
| 5652 | rc = sqlite3VtabBegin(db, pVTab); |
drh | b975598 | 2010-07-24 16:34:37 +0000 | [diff] [blame] | 5653 | if( pVTab ) importVtabErrMsg(p, pVTab->pVtab); |
danielk1977 | f9e7dda | 2006-06-16 16:08:53 +0000 | [diff] [blame] | 5654 | break; |
| 5655 | } |
| 5656 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 5657 | |
| 5658 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 5659 | /* Opcode: VCreate P1 * * P4 * |
danielk1977 | f9e7dda | 2006-06-16 16:08:53 +0000 | [diff] [blame] | 5660 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 5661 | ** 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] | 5662 | ** for that table. |
| 5663 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 5664 | case OP_VCreate: { |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 5665 | rc = sqlite3VtabCallCreate(db, pOp->p1, pOp->p4.z, &p->zErrMsg); |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 5666 | break; |
| 5667 | } |
| 5668 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 5669 | |
| 5670 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 5671 | /* Opcode: VDestroy P1 * * P4 * |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 5672 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 5673 | ** 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] | 5674 | ** of that table. |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 5675 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 5676 | case OP_VDestroy: { |
danielk1977 | 212b218 | 2006-06-23 14:32:08 +0000 | [diff] [blame] | 5677 | p->inVtabMethod = 2; |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 5678 | rc = sqlite3VtabCallDestroy(db, pOp->p1, pOp->p4.z); |
danielk1977 | 212b218 | 2006-06-23 14:32:08 +0000 | [diff] [blame] | 5679 | p->inVtabMethod = 0; |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 5680 | break; |
| 5681 | } |
| 5682 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 5683 | |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 5684 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 5685 | /* Opcode: VOpen P1 * * P4 * |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 5686 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 5687 | ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure. |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 5688 | ** P1 is a cursor number. This opcode opens a cursor to the virtual |
| 5689 | ** table and stores that cursor in P1. |
| 5690 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 5691 | case OP_VOpen: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 5692 | VdbeCursor *pCur; |
| 5693 | sqlite3_vtab_cursor *pVtabCursor; |
| 5694 | sqlite3_vtab *pVtab; |
| 5695 | sqlite3_module *pModule; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 5696 | |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 5697 | pCur = 0; |
| 5698 | pVtabCursor = 0; |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 5699 | pVtab = pOp->p4.pVtab->pVtab; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 5700 | pModule = (sqlite3_module *)pVtab->pModule; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 5701 | assert(pVtab && pModule); |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 5702 | rc = pModule->xOpen(pVtab, &pVtabCursor); |
drh | b975598 | 2010-07-24 16:34:37 +0000 | [diff] [blame] | 5703 | importVtabErrMsg(p, pVtab); |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 5704 | if( SQLITE_OK==rc ){ |
shane | 21e7feb | 2008-05-30 15:59:49 +0000 | [diff] [blame] | 5705 | /* Initialize sqlite3_vtab_cursor base class */ |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 5706 | pVtabCursor->pVtab = pVtab; |
| 5707 | |
| 5708 | /* Initialise vdbe cursor object */ |
danielk1977 | d336e22 | 2009-02-20 10:58:41 +0000 | [diff] [blame] | 5709 | pCur = allocateCursor(p, pOp->p1, 0, -1, 0); |
danielk1977 | be71889 | 2006-06-23 08:05:19 +0000 | [diff] [blame] | 5710 | if( pCur ){ |
| 5711 | pCur->pVtabCursor = pVtabCursor; |
| 5712 | pCur->pModule = pVtabCursor->pVtab->pModule; |
danielk1977 | b7a2f2e | 2006-06-23 11:34:54 +0000 | [diff] [blame] | 5713 | }else{ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 5714 | db->mallocFailed = 1; |
danielk1977 | b7a2f2e | 2006-06-23 11:34:54 +0000 | [diff] [blame] | 5715 | pModule->xClose(pVtabCursor); |
danielk1977 | be71889 | 2006-06-23 08:05:19 +0000 | [diff] [blame] | 5716 | } |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 5717 | } |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 5718 | break; |
| 5719 | } |
| 5720 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 5721 | |
| 5722 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
danielk1977 | 6dbee81 | 2008-01-03 18:39:41 +0000 | [diff] [blame] | 5723 | /* Opcode: VFilter P1 P2 P3 P4 * |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 5724 | ** |
| 5725 | ** P1 is a cursor opened using VOpen. P2 is an address to jump to if |
| 5726 | ** the filtered result set is empty. |
| 5727 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 5728 | ** P4 is either NULL or a string that was generated by the xBestIndex |
| 5729 | ** method of the module. The interpretation of the P4 string is left |
drh | 4be8b51 | 2006-06-13 23:51:34 +0000 | [diff] [blame] | 5730 | ** to the module implementation. |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 5731 | ** |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 5732 | ** This opcode invokes the xFilter method on the virtual table specified |
danielk1977 | 6dbee81 | 2008-01-03 18:39:41 +0000 | [diff] [blame] | 5733 | ** by P1. The integer query plan parameter to xFilter is stored in register |
| 5734 | ** P3. Register P3+1 stores the argc parameter to be passed to the |
drh | 174edc6 | 2008-05-29 05:23:41 +0000 | [diff] [blame] | 5735 | ** xFilter method. Registers P3+2..P3+1+argc are the argc |
| 5736 | ** additional parameters which are passed to |
danielk1977 | 6dbee81 | 2008-01-03 18:39:41 +0000 | [diff] [blame] | 5737 | ** xFilter as argv. Register P3+2 becomes argv[0] when passed to xFilter. |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 5738 | ** |
danielk1977 | 6dbee81 | 2008-01-03 18:39:41 +0000 | [diff] [blame] | 5739 | ** 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] | 5740 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 5741 | case OP_VFilter: { /* jump */ |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 5742 | int nArg; |
danielk1977 | 6dbee81 | 2008-01-03 18:39:41 +0000 | [diff] [blame] | 5743 | int iQuery; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 5744 | const sqlite3_module *pModule; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 5745 | Mem *pQuery; |
| 5746 | Mem *pArgc; |
drh | 4dc754d | 2008-07-23 18:17:32 +0000 | [diff] [blame] | 5747 | sqlite3_vtab_cursor *pVtabCursor; |
| 5748 | sqlite3_vtab *pVtab; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 5749 | VdbeCursor *pCur; |
| 5750 | int res; |
| 5751 | int i; |
| 5752 | Mem **apArg; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 5753 | |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 5754 | pQuery = &aMem[pOp->p3]; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 5755 | pArgc = &pQuery[1]; |
| 5756 | pCur = p->apCsr[pOp->p1]; |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 5757 | assert( memIsValid(pQuery) ); |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 5758 | REGISTER_TRACE(pOp->p3, pQuery); |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 5759 | assert( pCur->pVtabCursor ); |
drh | 4dc754d | 2008-07-23 18:17:32 +0000 | [diff] [blame] | 5760 | pVtabCursor = pCur->pVtabCursor; |
| 5761 | pVtab = pVtabCursor->pVtab; |
| 5762 | pModule = pVtab->pModule; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 5763 | |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 5764 | /* Grab the index number and argc parameters */ |
danielk1977 | 6dbee81 | 2008-01-03 18:39:41 +0000 | [diff] [blame] | 5765 | assert( (pQuery->flags&MEM_Int)!=0 && pArgc->flags==MEM_Int ); |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 5766 | nArg = (int)pArgc->u.i; |
| 5767 | iQuery = (int)pQuery->u.i; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 5768 | |
drh | 644a529 | 2006-12-20 14:53:38 +0000 | [diff] [blame] | 5769 | /* Invoke the xFilter method */ |
| 5770 | { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 5771 | res = 0; |
| 5772 | apArg = p->apArg; |
drh | 4be8b51 | 2006-06-13 23:51:34 +0000 | [diff] [blame] | 5773 | for(i = 0; i<nArg; i++){ |
danielk1977 | 6dbee81 | 2008-01-03 18:39:41 +0000 | [diff] [blame] | 5774 | apArg[i] = &pArgc[i+1]; |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 5775 | sqlite3VdbeMemStoreType(apArg[i]); |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 5776 | } |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 5777 | |
danielk1977 | be71889 | 2006-06-23 08:05:19 +0000 | [diff] [blame] | 5778 | p->inVtabMethod = 1; |
drh | 4dc754d | 2008-07-23 18:17:32 +0000 | [diff] [blame] | 5779 | rc = pModule->xFilter(pVtabCursor, iQuery, pOp->p4.z, nArg, apArg); |
danielk1977 | be71889 | 2006-06-23 08:05:19 +0000 | [diff] [blame] | 5780 | p->inVtabMethod = 0; |
drh | b975598 | 2010-07-24 16:34:37 +0000 | [diff] [blame] | 5781 | importVtabErrMsg(p, pVtab); |
danielk1977 | a298e90 | 2006-06-22 09:53:48 +0000 | [diff] [blame] | 5782 | if( rc==SQLITE_OK ){ |
drh | 4dc754d | 2008-07-23 18:17:32 +0000 | [diff] [blame] | 5783 | res = pModule->xEof(pVtabCursor); |
danielk1977 | a298e90 | 2006-06-22 09:53:48 +0000 | [diff] [blame] | 5784 | } |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 5785 | |
danielk1977 | a298e90 | 2006-06-22 09:53:48 +0000 | [diff] [blame] | 5786 | if( res ){ |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 5787 | pc = pOp->p2 - 1; |
| 5788 | } |
| 5789 | } |
drh | 1d454a3 | 2008-01-31 19:34:51 +0000 | [diff] [blame] | 5790 | pCur->nullRow = 0; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 5791 | |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 5792 | break; |
| 5793 | } |
| 5794 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 5795 | |
| 5796 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 5797 | /* Opcode: VColumn P1 P2 P3 * * |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 5798 | ** |
drh | 2133d82 | 2008-01-03 18:44:59 +0000 | [diff] [blame] | 5799 | ** Store the value of the P2-th column of |
| 5800 | ** the row of the virtual-table that the |
| 5801 | ** P1 cursor is pointing to into register P3. |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 5802 | */ |
| 5803 | case OP_VColumn: { |
danielk1977 | 3e3a84d | 2008-08-01 17:37:40 +0000 | [diff] [blame] | 5804 | sqlite3_vtab *pVtab; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 5805 | const sqlite3_module *pModule; |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 5806 | Mem *pDest; |
| 5807 | sqlite3_context sContext; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 5808 | |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 5809 | VdbeCursor *pCur = p->apCsr[pOp->p1]; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 5810 | assert( pCur->pVtabCursor ); |
drh | 2945b4a | 2008-01-31 15:53:45 +0000 | [diff] [blame] | 5811 | assert( pOp->p3>0 && pOp->p3<=p->nMem ); |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 5812 | pDest = &aMem[pOp->p3]; |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 5813 | memAboutToChange(p, pDest); |
drh | 2945b4a | 2008-01-31 15:53:45 +0000 | [diff] [blame] | 5814 | if( pCur->nullRow ){ |
| 5815 | sqlite3VdbeMemSetNull(pDest); |
| 5816 | break; |
| 5817 | } |
danielk1977 | 3e3a84d | 2008-08-01 17:37:40 +0000 | [diff] [blame] | 5818 | pVtab = pCur->pVtabCursor->pVtab; |
| 5819 | pModule = pVtab->pModule; |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 5820 | assert( pModule->xColumn ); |
| 5821 | memset(&sContext, 0, sizeof(sContext)); |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 5822 | |
| 5823 | /* The output cell may already have a buffer allocated. Move |
| 5824 | ** the current contents to sContext.s so in case the user-function |
| 5825 | ** can use the already allocated buffer instead of allocating a |
| 5826 | ** new one. |
| 5827 | */ |
| 5828 | sqlite3VdbeMemMove(&sContext.s, pDest); |
| 5829 | MemSetTypeFlag(&sContext.s, MEM_Null); |
| 5830 | |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 5831 | rc = pModule->xColumn(pCur->pVtabCursor, &sContext, pOp->p2); |
drh | b975598 | 2010-07-24 16:34:37 +0000 | [diff] [blame] | 5832 | importVtabErrMsg(p, pVtab); |
drh | 4c8555f | 2009-06-25 01:47:11 +0000 | [diff] [blame] | 5833 | if( sContext.isError ){ |
| 5834 | rc = sContext.isError; |
| 5835 | } |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 5836 | |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 5837 | /* Copy the result of the function to the P3 register. We |
shane | be21779 | 2009-03-05 04:20:31 +0000 | [diff] [blame] | 5838 | ** do this regardless of whether or not an error occurred to ensure any |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 5839 | ** dynamic allocation in sContext.s (a Mem struct) is released. |
| 5840 | */ |
| 5841 | sqlite3VdbeChangeEncoding(&sContext.s, encoding); |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 5842 | sqlite3VdbeMemMove(pDest, &sContext.s); |
drh | 5ff4437 | 2009-11-24 16:26:17 +0000 | [diff] [blame] | 5843 | REGISTER_TRACE(pOp->p3, pDest); |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 5844 | UPDATE_MAX_BLOBSIZE(pDest); |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 5845 | |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 5846 | if( sqlite3VdbeMemTooBig(pDest) ){ |
| 5847 | goto too_big; |
| 5848 | } |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 5849 | break; |
| 5850 | } |
| 5851 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 5852 | |
| 5853 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 5854 | /* Opcode: VNext P1 P2 * * * |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 5855 | ** |
| 5856 | ** Advance virtual table P1 to the next row in its result set and |
| 5857 | ** jump to instruction P2. Or, if the virtual table has reached |
| 5858 | ** the end of its result set, then fall through to the next instruction. |
| 5859 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 5860 | case OP_VNext: { /* jump */ |
danielk1977 | 3e3a84d | 2008-08-01 17:37:40 +0000 | [diff] [blame] | 5861 | sqlite3_vtab *pVtab; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 5862 | const sqlite3_module *pModule; |
drh | c54a617 | 2009-06-02 16:06:03 +0000 | [diff] [blame] | 5863 | int res; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 5864 | VdbeCursor *pCur; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 5865 | |
drh | c54a617 | 2009-06-02 16:06:03 +0000 | [diff] [blame] | 5866 | res = 0; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 5867 | pCur = p->apCsr[pOp->p1]; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 5868 | assert( pCur->pVtabCursor ); |
drh | 2945b4a | 2008-01-31 15:53:45 +0000 | [diff] [blame] | 5869 | if( pCur->nullRow ){ |
| 5870 | break; |
| 5871 | } |
danielk1977 | 3e3a84d | 2008-08-01 17:37:40 +0000 | [diff] [blame] | 5872 | pVtab = pCur->pVtabCursor->pVtab; |
| 5873 | pModule = pVtab->pModule; |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 5874 | assert( pModule->xNext ); |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 5875 | |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 5876 | /* Invoke the xNext() method of the module. There is no way for the |
| 5877 | ** underlying implementation to return an error if one occurs during |
| 5878 | ** xNext(). Instead, if an error occurs, true is returned (indicating that |
| 5879 | ** data is available) and the error code returned when xColumn or |
| 5880 | ** some other method is next invoked on the save virtual table cursor. |
| 5881 | */ |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 5882 | p->inVtabMethod = 1; |
| 5883 | rc = pModule->xNext(pCur->pVtabCursor); |
| 5884 | p->inVtabMethod = 0; |
drh | b975598 | 2010-07-24 16:34:37 +0000 | [diff] [blame] | 5885 | importVtabErrMsg(p, pVtab); |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 5886 | if( rc==SQLITE_OK ){ |
| 5887 | res = pModule->xEof(pCur->pVtabCursor); |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 5888 | } |
| 5889 | |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 5890 | if( !res ){ |
| 5891 | /* If there is data, jump to P2 */ |
| 5892 | pc = pOp->p2 - 1; |
| 5893 | } |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 5894 | break; |
| 5895 | } |
| 5896 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 5897 | |
danielk1977 | 182c4ba | 2007-06-27 15:53:34 +0000 | [diff] [blame] | 5898 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 5899 | /* Opcode: VRename P1 * * P4 * |
danielk1977 | 182c4ba | 2007-06-27 15:53:34 +0000 | [diff] [blame] | 5900 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 5901 | ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure. |
danielk1977 | 182c4ba | 2007-06-27 15:53:34 +0000 | [diff] [blame] | 5902 | ** This opcode invokes the corresponding xRename method. The value |
danielk1977 | 6dbee81 | 2008-01-03 18:39:41 +0000 | [diff] [blame] | 5903 | ** in register P1 is passed as the zName argument to the xRename method. |
danielk1977 | 182c4ba | 2007-06-27 15:53:34 +0000 | [diff] [blame] | 5904 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 5905 | case OP_VRename: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 5906 | sqlite3_vtab *pVtab; |
| 5907 | Mem *pName; |
| 5908 | |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 5909 | pVtab = pOp->p4.pVtab->pVtab; |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 5910 | pName = &aMem[pOp->p1]; |
danielk1977 | 182c4ba | 2007-06-27 15:53:34 +0000 | [diff] [blame] | 5911 | assert( pVtab->pModule->xRename ); |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 5912 | assert( memIsValid(pName) ); |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 5913 | REGISTER_TRACE(pOp->p1, pName); |
drh | 35f6b93 | 2009-06-23 14:15:04 +0000 | [diff] [blame] | 5914 | assert( pName->flags & MEM_Str ); |
danielk1977 | 6dbee81 | 2008-01-03 18:39:41 +0000 | [diff] [blame] | 5915 | rc = pVtab->pModule->xRename(pVtab, pName->z); |
drh | b975598 | 2010-07-24 16:34:37 +0000 | [diff] [blame] | 5916 | importVtabErrMsg(p, pVtab); |
dan | a235d0c | 2010-08-24 16:59:47 +0000 | [diff] [blame] | 5917 | p->expired = 0; |
danielk1977 | 182c4ba | 2007-06-27 15:53:34 +0000 | [diff] [blame] | 5918 | |
danielk1977 | 182c4ba | 2007-06-27 15:53:34 +0000 | [diff] [blame] | 5919 | break; |
| 5920 | } |
| 5921 | #endif |
drh | 4cbdda9 | 2006-06-14 19:00:20 +0000 | [diff] [blame] | 5922 | |
| 5923 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 5924 | /* Opcode: VUpdate P1 P2 P3 P4 * |
danielk1977 | 399918f | 2006-06-14 13:03:23 +0000 | [diff] [blame] | 5925 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 5926 | ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure. |
danielk1977 | 399918f | 2006-06-14 13:03:23 +0000 | [diff] [blame] | 5927 | ** This opcode invokes the corresponding xUpdate method. P2 values |
danielk1977 | 2a339ff | 2008-01-03 17:31:44 +0000 | [diff] [blame] | 5928 | ** are contiguous memory cells starting at P3 to pass to the xUpdate |
| 5929 | ** invocation. The value in register (P3+P2-1) corresponds to the |
| 5930 | ** p2th element of the argv array passed to xUpdate. |
drh | 4cbdda9 | 2006-06-14 19:00:20 +0000 | [diff] [blame] | 5931 | ** |
| 5932 | ** The xUpdate method will do a DELETE or an INSERT or both. |
danielk1977 | 2a339ff | 2008-01-03 17:31:44 +0000 | [diff] [blame] | 5933 | ** The argv[0] element (which corresponds to memory cell P3) |
| 5934 | ** is the rowid of a row to delete. If argv[0] is NULL then no |
| 5935 | ** deletion occurs. The argv[1] element is the rowid of the new |
| 5936 | ** row. This can be NULL to have the virtual table select the new |
| 5937 | ** rowid for itself. The subsequent elements in the array are |
| 5938 | ** the values of columns in the new row. |
drh | 4cbdda9 | 2006-06-14 19:00:20 +0000 | [diff] [blame] | 5939 | ** |
| 5940 | ** If P2==1 then no insert is performed. argv[0] is the rowid of |
| 5941 | ** a row to delete. |
danielk1977 | 1f6eec5 | 2006-06-16 06:17:47 +0000 | [diff] [blame] | 5942 | ** |
| 5943 | ** P1 is a boolean flag. If it is set to true and the xUpdate call |
| 5944 | ** is successful, then the value returned by sqlite3_last_insert_rowid() |
| 5945 | ** is set to the value of the rowid for the row just inserted. |
danielk1977 | 399918f | 2006-06-14 13:03:23 +0000 | [diff] [blame] | 5946 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 5947 | case OP_VUpdate: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 5948 | sqlite3_vtab *pVtab; |
| 5949 | sqlite3_module *pModule; |
| 5950 | int nArg; |
| 5951 | int i; |
| 5952 | sqlite_int64 rowid; |
| 5953 | Mem **apArg; |
| 5954 | Mem *pX; |
| 5955 | |
dan | b061d05 | 2011-04-25 18:49:57 +0000 | [diff] [blame] | 5956 | assert( pOp->p2==1 || pOp->p5==OE_Fail || pOp->p5==OE_Rollback |
| 5957 | || pOp->p5==OE_Abort || pOp->p5==OE_Ignore || pOp->p5==OE_Replace |
| 5958 | ); |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 5959 | pVtab = pOp->p4.pVtab->pVtab; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 5960 | pModule = (sqlite3_module *)pVtab->pModule; |
| 5961 | nArg = pOp->p2; |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 5962 | assert( pOp->p4type==P4_VTAB ); |
drh | 35f6b93 | 2009-06-23 14:15:04 +0000 | [diff] [blame] | 5963 | if( ALWAYS(pModule->xUpdate) ){ |
dan | b061d05 | 2011-04-25 18:49:57 +0000 | [diff] [blame] | 5964 | u8 vtabOnConflict = db->vtabOnConflict; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 5965 | apArg = p->apArg; |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 5966 | pX = &aMem[pOp->p3]; |
danielk1977 | 2a339ff | 2008-01-03 17:31:44 +0000 | [diff] [blame] | 5967 | for(i=0; i<nArg; i++){ |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 5968 | assert( memIsValid(pX) ); |
| 5969 | memAboutToChange(p, pX); |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 5970 | sqlite3VdbeMemStoreType(pX); |
drh | 9c41938 | 2006-06-16 21:13:21 +0000 | [diff] [blame] | 5971 | apArg[i] = pX; |
danielk1977 | 2a339ff | 2008-01-03 17:31:44 +0000 | [diff] [blame] | 5972 | pX++; |
danielk1977 | 399918f | 2006-06-14 13:03:23 +0000 | [diff] [blame] | 5973 | } |
dan | b061d05 | 2011-04-25 18:49:57 +0000 | [diff] [blame] | 5974 | db->vtabOnConflict = pOp->p5; |
danielk1977 | 1f6eec5 | 2006-06-16 06:17:47 +0000 | [diff] [blame] | 5975 | rc = pModule->xUpdate(pVtab, nArg, apArg, &rowid); |
dan | b061d05 | 2011-04-25 18:49:57 +0000 | [diff] [blame] | 5976 | db->vtabOnConflict = vtabOnConflict; |
drh | b975598 | 2010-07-24 16:34:37 +0000 | [diff] [blame] | 5977 | importVtabErrMsg(p, pVtab); |
drh | 35f6b93 | 2009-06-23 14:15:04 +0000 | [diff] [blame] | 5978 | if( rc==SQLITE_OK && pOp->p1 ){ |
danielk1977 | 1f6eec5 | 2006-06-16 06:17:47 +0000 | [diff] [blame] | 5979 | assert( nArg>1 && apArg[0] && (apArg[0]->flags&MEM_Null) ); |
drh | 99a6692 | 2011-05-13 18:51:42 +0000 | [diff] [blame] | 5980 | db->lastRowid = lastRowid = rowid; |
danielk1977 | 1f6eec5 | 2006-06-16 06:17:47 +0000 | [diff] [blame] | 5981 | } |
dan | b061d05 | 2011-04-25 18:49:57 +0000 | [diff] [blame] | 5982 | if( rc==SQLITE_CONSTRAINT && pOp->p4.pVtab->bConstraint ){ |
| 5983 | if( pOp->p5==OE_Ignore ){ |
| 5984 | rc = SQLITE_OK; |
| 5985 | }else{ |
| 5986 | p->errorAction = ((pOp->p5==OE_Replace) ? OE_Abort : pOp->p5); |
| 5987 | } |
| 5988 | }else{ |
| 5989 | p->nChange++; |
| 5990 | } |
danielk1977 | 399918f | 2006-06-14 13:03:23 +0000 | [diff] [blame] | 5991 | } |
drh | 4cbdda9 | 2006-06-14 19:00:20 +0000 | [diff] [blame] | 5992 | break; |
danielk1977 | 399918f | 2006-06-14 13:03:23 +0000 | [diff] [blame] | 5993 | } |
| 5994 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 5995 | |
danielk1977 | 59a9379 | 2008-05-15 17:48:20 +0000 | [diff] [blame] | 5996 | #ifndef SQLITE_OMIT_PAGER_PRAGMAS |
| 5997 | /* Opcode: Pagecount P1 P2 * * * |
| 5998 | ** |
| 5999 | ** Write the current number of pages in database P1 to memory cell P2. |
| 6000 | */ |
| 6001 | case OP_Pagecount: { /* out2-prerelease */ |
drh | b129915 | 2010-03-30 22:58:33 +0000 | [diff] [blame] | 6002 | pOut->u.i = sqlite3BtreeLastPage(db->aDb[pOp->p1].pBt); |
danielk1977 | 59a9379 | 2008-05-15 17:48:20 +0000 | [diff] [blame] | 6003 | break; |
| 6004 | } |
| 6005 | #endif |
| 6006 | |
drh | 60ac3f4 | 2010-11-23 18:59:27 +0000 | [diff] [blame] | 6007 | |
| 6008 | #ifndef SQLITE_OMIT_PAGER_PRAGMAS |
| 6009 | /* Opcode: MaxPgcnt P1 P2 P3 * * |
| 6010 | ** |
| 6011 | ** Try to set the maximum page count for database P1 to the value in P3. |
drh | c84e033 | 2010-11-23 20:25:08 +0000 | [diff] [blame] | 6012 | ** Do not let the maximum page count fall below the current page count and |
| 6013 | ** do not change the maximum page count value if P3==0. |
| 6014 | ** |
drh | 60ac3f4 | 2010-11-23 18:59:27 +0000 | [diff] [blame] | 6015 | ** Store the maximum page count after the change in register P2. |
| 6016 | */ |
| 6017 | case OP_MaxPgcnt: { /* out2-prerelease */ |
drh | c84e033 | 2010-11-23 20:25:08 +0000 | [diff] [blame] | 6018 | unsigned int newMax; |
drh | 60ac3f4 | 2010-11-23 18:59:27 +0000 | [diff] [blame] | 6019 | Btree *pBt; |
| 6020 | |
| 6021 | pBt = db->aDb[pOp->p1].pBt; |
drh | c84e033 | 2010-11-23 20:25:08 +0000 | [diff] [blame] | 6022 | newMax = 0; |
| 6023 | if( pOp->p3 ){ |
| 6024 | newMax = sqlite3BtreeLastPage(pBt); |
drh | 6ea28d6 | 2010-11-26 16:49:59 +0000 | [diff] [blame] | 6025 | if( newMax < (unsigned)pOp->p3 ) newMax = (unsigned)pOp->p3; |
drh | c84e033 | 2010-11-23 20:25:08 +0000 | [diff] [blame] | 6026 | } |
| 6027 | pOut->u.i = sqlite3BtreeMaxPageCount(pBt, newMax); |
drh | 60ac3f4 | 2010-11-23 18:59:27 +0000 | [diff] [blame] | 6028 | break; |
| 6029 | } |
| 6030 | #endif |
| 6031 | |
| 6032 | |
drh | 949f9cd | 2008-01-12 21:35:57 +0000 | [diff] [blame] | 6033 | #ifndef SQLITE_OMIT_TRACE |
| 6034 | /* Opcode: Trace * * * P4 * |
| 6035 | ** |
| 6036 | ** If tracing is enabled (by the sqlite3_trace()) interface, then |
| 6037 | ** the UTF-8 string contained in P4 is emitted on the trace callback. |
| 6038 | */ |
| 6039 | case OP_Trace: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 6040 | char *zTrace; |
drh | c3f1d5f | 2011-05-30 23:42:16 +0000 | [diff] [blame] | 6041 | char *z; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 6042 | |
drh | c3f1d5f | 2011-05-30 23:42:16 +0000 | [diff] [blame] | 6043 | if( db->xTrace && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0 ){ |
| 6044 | z = sqlite3VdbeExpandSql(p, zTrace); |
| 6045 | db->xTrace(db->pTraceArg, z); |
| 6046 | sqlite3DbFree(db, z); |
drh | 949f9cd | 2008-01-12 21:35:57 +0000 | [diff] [blame] | 6047 | } |
drh | c3f1d5f | 2011-05-30 23:42:16 +0000 | [diff] [blame] | 6048 | #ifdef SQLITE_DEBUG |
| 6049 | if( (db->flags & SQLITE_SqlTrace)!=0 |
| 6050 | && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0 |
| 6051 | ){ |
| 6052 | sqlite3DebugPrintf("SQL-trace: %s\n", zTrace); |
| 6053 | } |
| 6054 | #endif /* SQLITE_DEBUG */ |
drh | 949f9cd | 2008-01-12 21:35:57 +0000 | [diff] [blame] | 6055 | break; |
| 6056 | } |
| 6057 | #endif |
| 6058 | |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 6059 | |
| 6060 | /* Opcode: Noop * * * * * |
| 6061 | ** |
| 6062 | ** Do nothing. This instruction is often useful as a jump |
| 6063 | ** destination. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 6064 | */ |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 6065 | /* |
| 6066 | ** The magic Explain opcode are only inserted when explain==2 (which |
| 6067 | ** is to say when the EXPLAIN QUERY PLAN syntax is used.) |
| 6068 | ** This opcode records information from the optimizer. It is the |
| 6069 | ** the same as a no-op. This opcodesnever appears in a real VM program. |
| 6070 | */ |
| 6071 | default: { /* This is really OP_Noop and OP_Explain */ |
drh | 13573c7 | 2010-01-12 17:04:07 +0000 | [diff] [blame] | 6072 | assert( pOp->opcode==OP_Noop || pOp->opcode==OP_Explain ); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 6073 | break; |
| 6074 | } |
| 6075 | |
| 6076 | /***************************************************************************** |
| 6077 | ** The cases of the switch statement above this line should all be indented |
| 6078 | ** by 6 spaces. But the left-most 6 spaces have been removed to improve the |
| 6079 | ** readability. From this point on down, the normal indentation rules are |
| 6080 | ** restored. |
| 6081 | *****************************************************************************/ |
| 6082 | } |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 6083 | |
drh | 7b39686 | 2003-01-01 23:06:20 +0000 | [diff] [blame] | 6084 | #ifdef VDBE_PROFILE |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 6085 | { |
shane | 9bcbdad | 2008-05-29 20:22:37 +0000 | [diff] [blame] | 6086 | u64 elapsed = sqlite3Hwtime() - start; |
| 6087 | pOp->cycles += elapsed; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 6088 | pOp->cnt++; |
| 6089 | #if 0 |
shane | 9bcbdad | 2008-05-29 20:22:37 +0000 | [diff] [blame] | 6090 | fprintf(stdout, "%10llu ", elapsed); |
drh | bbe879d | 2009-11-14 18:04:35 +0000 | [diff] [blame] | 6091 | sqlite3VdbePrintOp(stdout, origPc, &aOp[origPc]); |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 6092 | #endif |
| 6093 | } |
drh | 7b39686 | 2003-01-01 23:06:20 +0000 | [diff] [blame] | 6094 | #endif |
| 6095 | |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 6096 | /* The following code adds nothing to the actual functionality |
| 6097 | ** of the program. It is only here for testing and debugging. |
| 6098 | ** On the other hand, it does burn CPU cycles every time through |
| 6099 | ** the evaluator loop. So we can leave it out when NDEBUG is defined. |
| 6100 | */ |
| 6101 | #ifndef NDEBUG |
drh | a611040 | 2005-07-28 20:51:19 +0000 | [diff] [blame] | 6102 | assert( pc>=-1 && pc<p->nOp ); |
drh | ae7e151 | 2007-05-02 16:51:59 +0000 | [diff] [blame] | 6103 | |
drh | cf1023c | 2007-05-08 20:59:49 +0000 | [diff] [blame] | 6104 | #ifdef SQLITE_DEBUG |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 6105 | if( p->trace ){ |
| 6106 | if( rc!=0 ) fprintf(p->trace,"rc=%d\n",rc); |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 6107 | if( pOp->opflags & (OPFLG_OUT2_PRERELEASE|OPFLG_OUT2) ){ |
| 6108 | registerTrace(p->trace, pOp->p2, &aMem[pOp->p2]); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6109 | } |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 6110 | if( pOp->opflags & OPFLG_OUT3 ){ |
| 6111 | registerTrace(p->trace, pOp->p3, &aMem[pOp->p3]); |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 6112 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6113 | } |
danielk1977 | b5402fb | 2005-01-12 07:15:04 +0000 | [diff] [blame] | 6114 | #endif /* SQLITE_DEBUG */ |
| 6115 | #endif /* NDEBUG */ |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 6116 | } /* The end of the for(;;) loop the loops through opcodes */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6117 | |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 6118 | /* If we reach this point, it means that execution is finished with |
| 6119 | ** an error of some kind. |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 6120 | */ |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 6121 | vdbe_error_halt: |
| 6122 | assert( rc ); |
| 6123 | p->rc = rc; |
drh | a64fa91 | 2010-03-04 00:53:32 +0000 | [diff] [blame] | 6124 | testcase( sqlite3GlobalConfig.xLog!=0 ); |
| 6125 | sqlite3_log(rc, "statement aborts at %d: [%s] %s", |
| 6126 | pc, p->zSql, p->zErrMsg); |
drh | 92f02c3 | 2004-09-02 14:57:08 +0000 | [diff] [blame] | 6127 | sqlite3VdbeHalt(p); |
danielk1977 | 7eaabcd | 2008-07-07 14:56:56 +0000 | [diff] [blame] | 6128 | if( rc==SQLITE_IOERR_NOMEM ) db->mallocFailed = 1; |
| 6129 | rc = SQLITE_ERROR; |
drh | cdf011d | 2011-04-04 21:25:28 +0000 | [diff] [blame] | 6130 | if( resetSchemaOnFault>0 ){ |
| 6131 | sqlite3ResetInternalSchema(db, resetSchemaOnFault-1); |
drh | bdaec52 | 2011-04-04 00:14:43 +0000 | [diff] [blame] | 6132 | } |
drh | 900b31e | 2007-08-28 02:27:51 +0000 | [diff] [blame] | 6133 | |
| 6134 | /* This is the only way out of this procedure. We have to |
| 6135 | ** release the mutexes on btrees that were acquired at the |
| 6136 | ** top. */ |
| 6137 | vdbe_return: |
drh | 99a6692 | 2011-05-13 18:51:42 +0000 | [diff] [blame] | 6138 | db->lastRowid = lastRowid; |
dan | 5538807 | 2011-09-20 15:53:02 +0000 | [diff] [blame] | 6139 | |
| 6140 | /* Update the statement and database cache hit/miss statistics. */ |
| 6141 | nHit = -nHit; |
| 6142 | nMiss = -nMiss; |
| 6143 | vdbeCacheStats(p, &nHit, &nMiss); |
| 6144 | p->aCounter[SQLITE_STMTSTATUS_CACHE_HIT-1] += nHit; |
| 6145 | p->aCounter[SQLITE_STMTSTATUS_CACHE_MISS-1] += nMiss; |
| 6146 | db->aHitMiss[0] += nHit; |
| 6147 | db->aHitMiss[1] += nMiss; |
| 6148 | |
drh | bdaec52 | 2011-04-04 00:14:43 +0000 | [diff] [blame] | 6149 | sqlite3VdbeLeave(p); |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 6150 | return rc; |
| 6151 | |
drh | 023ae03 | 2007-05-08 12:12:16 +0000 | [diff] [blame] | 6152 | /* Jump to here if a string or blob larger than SQLITE_MAX_LENGTH |
| 6153 | ** is encountered. |
| 6154 | */ |
| 6155 | too_big: |
drh | f089aa4 | 2008-07-08 19:34:06 +0000 | [diff] [blame] | 6156 | sqlite3SetString(&p->zErrMsg, db, "string or blob too big"); |
drh | 023ae03 | 2007-05-08 12:12:16 +0000 | [diff] [blame] | 6157 | rc = SQLITE_TOOBIG; |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 6158 | goto vdbe_error_halt; |
drh | 023ae03 | 2007-05-08 12:12:16 +0000 | [diff] [blame] | 6159 | |
drh | 98640a3 | 2007-06-07 19:08:32 +0000 | [diff] [blame] | 6160 | /* Jump to here if a malloc() fails. |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 6161 | */ |
| 6162 | no_mem: |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 6163 | db->mallocFailed = 1; |
drh | f089aa4 | 2008-07-08 19:34:06 +0000 | [diff] [blame] | 6164 | sqlite3SetString(&p->zErrMsg, db, "out of memory"); |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 6165 | rc = SQLITE_NOMEM; |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 6166 | goto vdbe_error_halt; |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 6167 | |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 6168 | /* Jump to here for any other kind of fatal error. The "rc" variable |
| 6169 | ** should hold the error number. |
| 6170 | */ |
| 6171 | abort_due_to_error: |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 6172 | assert( p->zErrMsg==0 ); |
| 6173 | if( db->mallocFailed ) rc = SQLITE_NOMEM; |
danielk1977 | 7eaabcd | 2008-07-07 14:56:56 +0000 | [diff] [blame] | 6174 | if( rc!=SQLITE_IOERR_NOMEM ){ |
drh | f089aa4 | 2008-07-08 19:34:06 +0000 | [diff] [blame] | 6175 | sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3ErrStr(rc)); |
danielk1977 | 7eaabcd | 2008-07-07 14:56:56 +0000 | [diff] [blame] | 6176 | } |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 6177 | goto vdbe_error_halt; |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 6178 | |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 6179 | /* Jump to here if the sqlite3_interrupt() API sets the interrupt |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 6180 | ** flag. |
| 6181 | */ |
| 6182 | abort_due_to_interrupt: |
drh | 881feaa | 2006-07-26 01:39:30 +0000 | [diff] [blame] | 6183 | assert( db->u1.isInterrupted ); |
drh | 7e8b848 | 2008-01-23 03:03:05 +0000 | [diff] [blame] | 6184 | rc = SQLITE_INTERRUPT; |
danielk1977 | 026d270 | 2004-06-14 13:14:59 +0000 | [diff] [blame] | 6185 | p->rc = rc; |
drh | f089aa4 | 2008-07-08 19:34:06 +0000 | [diff] [blame] | 6186 | sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3ErrStr(rc)); |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 6187 | goto vdbe_error_halt; |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 6188 | } |