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 | 0fd6135 | 2014-02-07 02:29:45 +0000 | [diff] [blame] | 12 | ** The code in this file implements the function that runs the |
| 13 | ** bytecode of a prepared statement. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 14 | ** |
drh | ac82fcf | 2002-09-08 17:23:41 +0000 | [diff] [blame] | 15 | ** Various scripts scan this source file in order to generate HTML |
| 16 | ** documentation, headers files, or other derived files. The formatting |
| 17 | ** of the code in this file is, therefore, important. See other comments |
| 18 | ** in this file for details. If in doubt, do not deviate from existing |
| 19 | ** commenting and indentation practices when changing or adding code. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 20 | */ |
| 21 | #include "sqliteInt.h" |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 22 | #include "vdbeInt.h" |
drh | 8f619cc | 2002-09-08 00:04:50 +0000 | [diff] [blame] | 23 | |
| 24 | /* |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 25 | ** Invoke this macro on memory cells just prior to changing the |
| 26 | ** value of the cell. This macro verifies that shallow copies are |
drh | 0fd6135 | 2014-02-07 02:29:45 +0000 | [diff] [blame] | 27 | ** not misused. A shallow copy of a string or blob just copies a |
| 28 | ** pointer to the string or blob, not the content. If the original |
| 29 | ** is changed while the copy is still in use, the string or blob might |
| 30 | ** be changed out from under the copy. This macro verifies that nothing |
drh | b6e8fd1 | 2014-03-06 01:56:33 +0000 | [diff] [blame] | 31 | ** like that ever happens. |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 32 | */ |
| 33 | #ifdef SQLITE_DEBUG |
drh | e4c88c0 | 2012-01-04 12:57:45 +0000 | [diff] [blame] | 34 | # define memAboutToChange(P,M) sqlite3VdbeMemAboutToChange(P,M) |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 35 | #else |
| 36 | # define memAboutToChange(P,M) |
| 37 | #endif |
| 38 | |
| 39 | /* |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 40 | ** The following global variable is incremented every time a cursor |
drh | 959403f | 2008-12-12 17:56:16 +0000 | [diff] [blame] | 41 | ** 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] | 42 | ** procedures use this information to make sure that indices are |
drh | ac82fcf | 2002-09-08 17:23:41 +0000 | [diff] [blame] | 43 | ** working correctly. This variable has no function other than to |
| 44 | ** help verify the correct operation of the library. |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 45 | */ |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 46 | #ifdef SQLITE_TEST |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 47 | int sqlite3_search_count = 0; |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 48 | #endif |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 49 | |
drh | f603871 | 2004-02-08 18:07:34 +0000 | [diff] [blame] | 50 | /* |
| 51 | ** When this global variable is positive, it gets decremented once before |
drh | e4c88c0 | 2012-01-04 12:57:45 +0000 | [diff] [blame] | 52 | ** each instruction in the VDBE. When it reaches zero, the u1.isInterrupted |
| 53 | ** field of the sqlite3 structure is set in order to simulate an interrupt. |
drh | f603871 | 2004-02-08 18:07:34 +0000 | [diff] [blame] | 54 | ** |
| 55 | ** This facility is used for testing purposes only. It does not function |
| 56 | ** in an ordinary build. |
| 57 | */ |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 58 | #ifdef SQLITE_TEST |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 59 | int sqlite3_interrupt_count = 0; |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 60 | #endif |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 61 | |
danielk1977 | 7e18c25 | 2004-05-25 11:47:24 +0000 | [diff] [blame] | 62 | /* |
drh | 6bf8957 | 2004-11-03 16:27:01 +0000 | [diff] [blame] | 63 | ** The next global variable is incremented each type the OP_Sort opcode |
| 64 | ** is executed. The test procedures use this information to make sure that |
shane | 21e7feb | 2008-05-30 15:59:49 +0000 | [diff] [blame] | 65 | ** sorting is occurring or not occurring at appropriate times. This variable |
drh | 6bf8957 | 2004-11-03 16:27:01 +0000 | [diff] [blame] | 66 | ** has no function other than to help verify the correct operation of the |
| 67 | ** library. |
| 68 | */ |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 69 | #ifdef SQLITE_TEST |
drh | 6bf8957 | 2004-11-03 16:27:01 +0000 | [diff] [blame] | 70 | int sqlite3_sort_count = 0; |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 71 | #endif |
drh | 6bf8957 | 2004-11-03 16:27:01 +0000 | [diff] [blame] | 72 | |
| 73 | /* |
drh | ae7e151 | 2007-05-02 16:51:59 +0000 | [diff] [blame] | 74 | ** The next global variable records the size of the largest MEM_Blob |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 75 | ** 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] | 76 | ** use this information to make sure that the zero-blob functionality |
| 77 | ** is working correctly. This variable has no function other than to |
| 78 | ** help verify the correct operation of the library. |
| 79 | */ |
| 80 | #ifdef SQLITE_TEST |
| 81 | int sqlite3_max_blobsize = 0; |
drh | ca48c90 | 2008-01-18 14:08:24 +0000 | [diff] [blame] | 82 | static void updateMaxBlobsize(Mem *p){ |
| 83 | if( (p->flags & (MEM_Str|MEM_Blob))!=0 && p->n>sqlite3_max_blobsize ){ |
| 84 | sqlite3_max_blobsize = p->n; |
| 85 | } |
| 86 | } |
drh | ae7e151 | 2007-05-02 16:51:59 +0000 | [diff] [blame] | 87 | #endif |
| 88 | |
| 89 | /* |
drh | 9b1c62d | 2011-03-30 21:04:43 +0000 | [diff] [blame] | 90 | ** This macro evaluates to true if either the update hook or the preupdate |
| 91 | ** hook are enabled for database connect DB. |
| 92 | */ |
| 93 | #ifdef SQLITE_ENABLE_PREUPDATE_HOOK |
drh | 74c3302 | 2016-03-30 12:56:55 +0000 | [diff] [blame] | 94 | # define HAS_UPDATE_HOOK(DB) ((DB)->xPreUpdateCallback||(DB)->xUpdateCallback) |
drh | 9b1c62d | 2011-03-30 21:04:43 +0000 | [diff] [blame] | 95 | #else |
drh | 74c3302 | 2016-03-30 12:56:55 +0000 | [diff] [blame] | 96 | # define HAS_UPDATE_HOOK(DB) ((DB)->xUpdateCallback) |
drh | 9b1c62d | 2011-03-30 21:04:43 +0000 | [diff] [blame] | 97 | #endif |
| 98 | |
| 99 | /* |
drh | 0fd6135 | 2014-02-07 02:29:45 +0000 | [diff] [blame] | 100 | ** The next global variable is incremented each time the OP_Found opcode |
dan | 0ff297e | 2009-09-25 17:03:14 +0000 | [diff] [blame] | 101 | ** is executed. This is used to test whether or not the foreign key |
| 102 | ** operation implemented using OP_FkIsZero is working. This variable |
| 103 | ** has no function other than to help verify the correct operation of the |
| 104 | ** library. |
| 105 | */ |
| 106 | #ifdef SQLITE_TEST |
| 107 | int sqlite3_found_count = 0; |
| 108 | #endif |
| 109 | |
| 110 | /* |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 111 | ** Test a register to see if it exceeds the current maximum blob size. |
| 112 | ** If it does, record the new maximum blob size. |
| 113 | */ |
drh | d12602a | 2016-12-07 15:49:02 +0000 | [diff] [blame] | 114 | #if defined(SQLITE_TEST) && !defined(SQLITE_UNTESTABLE) |
drh | ca48c90 | 2008-01-18 14:08:24 +0000 | [diff] [blame] | 115 | # define UPDATE_MAX_BLOBSIZE(P) updateMaxBlobsize(P) |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 116 | #else |
| 117 | # define UPDATE_MAX_BLOBSIZE(P) |
| 118 | #endif |
| 119 | |
| 120 | /* |
drh | 5655c54 | 2014-02-19 19:14:34 +0000 | [diff] [blame] | 121 | ** Invoke the VDBE coverage callback, if that callback is defined. This |
| 122 | ** feature is used for test suite validation only and does not appear an |
| 123 | ** production builds. |
| 124 | ** |
drh | 7083a48 | 2018-07-10 16:04:04 +0000 | [diff] [blame] | 125 | ** M is an integer between 2 and 4. 2 indicates a ordinary two-way |
| 126 | ** branch (I=0 means fall through and I=1 means taken). 3 indicates |
| 127 | ** a 3-way branch where the third way is when one of the operands is |
| 128 | ** NULL. 4 indicates the OP_Jump instruction which has three destinations |
| 129 | ** depending on whether the first operand is less than, equal to, or greater |
| 130 | ** than the second. |
drh | 4336b0e | 2014-08-05 00:53:51 +0000 | [diff] [blame] | 131 | ** |
| 132 | ** iSrcLine is the source code line (from the __LINE__ macro) that |
drh | 7083a48 | 2018-07-10 16:04:04 +0000 | [diff] [blame] | 133 | ** generated the VDBE instruction combined with flag bits. The source |
| 134 | ** code line number is in the lower 24 bits of iSrcLine and the upper |
| 135 | ** 8 bytes are flags. The lower three bits of the flags indicate |
| 136 | ** values for I that should never occur. For example, if the branch is |
| 137 | ** always taken, the flags should be 0x05 since the fall-through and |
| 138 | ** alternate branch are never taken. If a branch is never taken then |
| 139 | ** flags should be 0x06 since only the fall-through approach is allowed. |
| 140 | ** |
| 141 | ** Bit 0x04 of the flags indicates an OP_Jump opcode that is only |
| 142 | ** interested in equal or not-equal. In other words, I==0 and I==2 |
| 143 | ** should be treated the same. |
| 144 | ** |
| 145 | ** Since only a line number is retained, not the filename, this macro |
| 146 | ** only works for amalgamation builds. But that is ok, since these macros |
| 147 | ** should be no-ops except for special builds used to measure test coverage. |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 148 | */ |
| 149 | #if !defined(SQLITE_VDBE_COVERAGE) |
| 150 | # define VdbeBranchTaken(I,M) |
| 151 | #else |
drh | 5655c54 | 2014-02-19 19:14:34 +0000 | [diff] [blame] | 152 | # define VdbeBranchTaken(I,M) vdbeTakeBranch(pOp->iSrcLine,I,M) |
drh | 7083a48 | 2018-07-10 16:04:04 +0000 | [diff] [blame] | 153 | static void vdbeTakeBranch(u32 iSrcLine, u8 I, u8 M){ |
| 154 | u8 mNever; |
| 155 | assert( I<=2 ); /* 0: fall through, 1: taken, 2: alternate taken */ |
| 156 | assert( M<=4 ); /* 2: two-way branch, 3: three-way branch, 4: OP_Jump */ |
| 157 | assert( I<M ); /* I can only be 2 if M is 3 or 4 */ |
| 158 | /* Transform I from a integer [0,1,2] into a bitmask of [1,2,4] */ |
| 159 | I = 1<<I; |
| 160 | /* The upper 8 bits of iSrcLine are flags. The lower three bits of |
| 161 | ** the flags indicate directions that the branch can never go. If |
| 162 | ** a branch really does go in one of those directions, assert right |
| 163 | ** away. */ |
| 164 | mNever = iSrcLine >> 24; |
| 165 | assert( (I & mNever)==0 ); |
| 166 | if( sqlite3GlobalConfig.xVdbeBranch==0 ) return; /*NO_TEST*/ |
| 167 | I |= mNever; |
| 168 | if( M==2 ) I |= 0x04; |
| 169 | if( M==4 ){ |
| 170 | I |= 0x08; |
drh | 6ccbd27 | 2018-07-10 17:10:44 +0000 | [diff] [blame] | 171 | if( (mNever&0x08)!=0 && (I&0x05)!=0) I |= 0x05; /*NO_TEST*/ |
drh | 5655c54 | 2014-02-19 19:14:34 +0000 | [diff] [blame] | 172 | } |
drh | 7083a48 | 2018-07-10 16:04:04 +0000 | [diff] [blame] | 173 | sqlite3GlobalConfig.xVdbeBranch(sqlite3GlobalConfig.pVdbeBranchArg, |
| 174 | iSrcLine&0xffffff, I, M); |
drh | 5655c54 | 2014-02-19 19:14:34 +0000 | [diff] [blame] | 175 | } |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 176 | #endif |
| 177 | |
| 178 | /* |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 179 | ** Convert the given register into a string if it isn't one |
danielk1977 | bd7e460 | 2004-05-24 07:34:48 +0000 | [diff] [blame] | 180 | ** already. Return non-zero if a malloc() fails. |
| 181 | */ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 182 | #define Stringify(P, enc) \ |
drh | bd9507c | 2014-08-23 17:21:37 +0000 | [diff] [blame] | 183 | if(((P)->flags&(MEM_Str|MEM_Blob))==0 && sqlite3VdbeMemStringify(P,enc,0)) \ |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 184 | { goto no_mem; } |
danielk1977 | bd7e460 | 2004-05-24 07:34:48 +0000 | [diff] [blame] | 185 | |
| 186 | /* |
danielk1977 | bd7e460 | 2004-05-24 07:34:48 +0000 | [diff] [blame] | 187 | ** An ephemeral string value (signified by the MEM_Ephem flag) contains |
| 188 | ** a pointer to a dynamically allocated string where some other entity |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 189 | ** is responsible for deallocating that string. Because the register |
| 190 | ** does not control the string, it might be deleted without the register |
| 191 | ** knowing it. |
danielk1977 | bd7e460 | 2004-05-24 07:34:48 +0000 | [diff] [blame] | 192 | ** |
| 193 | ** This routine converts an ephemeral string into a dynamically allocated |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 194 | ** string that the register itself controls. In other words, it |
drh | c91b2fd | 2014-03-01 18:13:23 +0000 | [diff] [blame] | 195 | ** converts an MEM_Ephem string into a string with P.z==P.zMalloc. |
danielk1977 | bd7e460 | 2004-05-24 07:34:48 +0000 | [diff] [blame] | 196 | */ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 197 | #define Deephemeralize(P) \ |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 198 | if( ((P)->flags&MEM_Ephem)!=0 \ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 199 | && sqlite3VdbeMemMakeWriteable(P) ){ goto no_mem;} |
danielk1977 | 93d4675 | 2004-05-23 13:30:58 +0000 | [diff] [blame] | 200 | |
dan | 689ab89 | 2011-08-12 15:02:00 +0000 | [diff] [blame] | 201 | /* Return true if the cursor was opened using the OP_OpenSorter opcode. */ |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 202 | #define isSorter(x) ((x)->eCurType==CURTYPE_SORTER) |
danielk1977 | 8a6b541 | 2004-05-24 07:04:25 +0000 | [diff] [blame] | 203 | |
| 204 | /* |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 205 | ** Allocate VdbeCursor number iCur. Return a pointer to it. Return NULL |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 206 | ** if we run out of memory. |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 207 | */ |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 208 | static VdbeCursor *allocateCursor( |
| 209 | Vdbe *p, /* The virtual machine */ |
| 210 | int iCur, /* Index of the new VdbeCursor */ |
danielk1977 | d336e22 | 2009-02-20 10:58:41 +0000 | [diff] [blame] | 211 | int nField, /* Number of fields in the table or index */ |
drh | e4c88c0 | 2012-01-04 12:57:45 +0000 | [diff] [blame] | 212 | int iDb, /* Database the cursor belongs to, or -1 */ |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 213 | u8 eCurType /* Type of the new cursor */ |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 214 | ){ |
| 215 | /* 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] | 216 | ** required for this VdbeCursor structure. It is convenient to use a |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 217 | ** vdbe memory cell to manage the memory allocation required for a |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 218 | ** VdbeCursor structure for the following reasons: |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 219 | ** |
| 220 | ** * Sometimes cursor numbers are used for a couple of different |
| 221 | ** purposes in a vdbe program. The different uses might require |
| 222 | ** different sized allocations. Memory cells provide growable |
| 223 | ** allocations. |
| 224 | ** |
| 225 | ** * When using ENABLE_MEMORY_MANAGEMENT, memory cell buffers can |
| 226 | ** be freed lazily via the sqlite3_release_memory() API. This |
| 227 | ** minimizes the number of malloc calls made by the system. |
| 228 | ** |
drh | 3cdce92 | 2016-03-21 00:30:40 +0000 | [diff] [blame] | 229 | ** The memory cell for cursor 0 is aMem[0]. The rest are allocated from |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 230 | ** the top of the register space. Cursor 1 is at Mem[p->nMem-1]. |
| 231 | ** Cursor 2 is at Mem[p->nMem-2]. And so forth. |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 232 | */ |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 233 | Mem *pMem = iCur>0 ? &p->aMem[p->nMem-iCur] : p->aMem; |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 234 | |
danielk1977 | 5f09613 | 2008-03-28 15:44:09 +0000 | [diff] [blame] | 235 | int nByte; |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 236 | VdbeCursor *pCx = 0; |
danielk1977 | 5f09613 | 2008-03-28 15:44:09 +0000 | [diff] [blame] | 237 | nByte = |
drh | 5cc1023 | 2013-11-21 01:04:02 +0000 | [diff] [blame] | 238 | ROUND8(sizeof(VdbeCursor)) + 2*sizeof(u32)*nField + |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 239 | (eCurType==CURTYPE_BTREE?sqlite3BtreeCursorSize():0); |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 240 | |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 241 | assert( iCur>=0 && iCur<p->nCursor ); |
drh | a3fa140 | 2016-04-29 02:55:05 +0000 | [diff] [blame] | 242 | if( p->apCsr[iCur] ){ /*OPTIMIZATION-IF-FALSE*/ |
danielk1977 | be71889 | 2006-06-23 08:05:19 +0000 | [diff] [blame] | 243 | sqlite3VdbeFreeCursor(p, p->apCsr[iCur]); |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 244 | p->apCsr[iCur] = 0; |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 245 | } |
drh | 322f285 | 2014-09-19 00:43:39 +0000 | [diff] [blame] | 246 | if( SQLITE_OK==sqlite3VdbeMemClearAndResize(pMem, nByte) ){ |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 247 | p->apCsr[iCur] = pCx = (VdbeCursor*)pMem->z; |
drh | fbd8cbd | 2016-12-10 12:58:15 +0000 | [diff] [blame] | 248 | memset(pCx, 0, offsetof(VdbeCursor,pAltCursor)); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 249 | pCx->eCurType = eCurType; |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 +0000 | [diff] [blame] | 250 | pCx->iDb = iDb; |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 251 | pCx->nField = nField; |
drh | b53a5a9 | 2014-10-12 22:37:22 +0000 | [diff] [blame] | 252 | pCx->aOffset = &pCx->aType[nField]; |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 253 | if( eCurType==CURTYPE_BTREE ){ |
| 254 | pCx->uc.pCursor = (BtCursor*) |
drh | 5cc1023 | 2013-11-21 01:04:02 +0000 | [diff] [blame] | 255 | &pMem->z[ROUND8(sizeof(VdbeCursor))+2*sizeof(u32)*nField]; |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 256 | sqlite3BtreeCursorZero(pCx->uc.pCursor); |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 257 | } |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 +0000 | [diff] [blame] | 258 | } |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 259 | return pCx; |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 260 | } |
| 261 | |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 262 | /* |
drh | 29d7210 | 2006-02-09 22:13:41 +0000 | [diff] [blame] | 263 | ** Try to convert a value into a numeric representation if we can |
| 264 | ** do so without loss of information. In other words, if the string |
| 265 | ** looks like a number, convert it into a number. If it does not |
| 266 | ** look like a number, leave it alone. |
drh | bd9507c | 2014-08-23 17:21:37 +0000 | [diff] [blame] | 267 | ** |
| 268 | ** If the bTryForInt flag is true, then extra effort is made to give |
| 269 | ** an integer representation. Strings that look like floating point |
| 270 | ** values but which have no fractional component (example: '48.00') |
| 271 | ** will have a MEM_Int representation when bTryForInt is true. |
| 272 | ** |
| 273 | ** If bTryForInt is false, then if the input string contains a decimal |
| 274 | ** point or exponential notation, the result is only MEM_Real, even |
| 275 | ** if there is an exact integer representation of the quantity. |
drh | 29d7210 | 2006-02-09 22:13:41 +0000 | [diff] [blame] | 276 | */ |
drh | bd9507c | 2014-08-23 17:21:37 +0000 | [diff] [blame] | 277 | static void applyNumericAffinity(Mem *pRec, int bTryForInt){ |
drh | 975b4c6 | 2014-07-26 16:47:23 +0000 | [diff] [blame] | 278 | double rValue; |
| 279 | i64 iValue; |
| 280 | u8 enc = pRec->enc; |
drh | 11a6eee | 2014-09-19 22:01:54 +0000 | [diff] [blame] | 281 | assert( (pRec->flags & (MEM_Str|MEM_Int|MEM_Real))==MEM_Str ); |
drh | 975b4c6 | 2014-07-26 16:47:23 +0000 | [diff] [blame] | 282 | if( sqlite3AtoF(pRec->z, &rValue, pRec->n, enc)==0 ) return; |
| 283 | if( 0==sqlite3Atoi64(pRec->z, &iValue, pRec->n, enc) ){ |
| 284 | pRec->u.i = iValue; |
| 285 | pRec->flags |= MEM_Int; |
| 286 | }else{ |
drh | 74eaba4 | 2014-09-18 17:52:15 +0000 | [diff] [blame] | 287 | pRec->u.r = rValue; |
drh | 975b4c6 | 2014-07-26 16:47:23 +0000 | [diff] [blame] | 288 | pRec->flags |= MEM_Real; |
drh | bd9507c | 2014-08-23 17:21:37 +0000 | [diff] [blame] | 289 | if( bTryForInt ) sqlite3VdbeIntegerAffinity(pRec); |
drh | 29d7210 | 2006-02-09 22:13:41 +0000 | [diff] [blame] | 290 | } |
drh | 06b3bd5 | 2018-02-01 01:13:33 +0000 | [diff] [blame] | 291 | /* TEXT->NUMERIC is many->one. Hence, it is important to invalidate the |
| 292 | ** string representation after computing a numeric equivalent, because the |
| 293 | ** string representation might not be the canonical representation for the |
| 294 | ** numeric value. Ticket [343634942dd54ab57b7024] 2018-01-31. */ |
| 295 | pRec->flags &= ~MEM_Str; |
drh | 29d7210 | 2006-02-09 22:13:41 +0000 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | /* |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 299 | ** Processing is determine by the affinity parameter: |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 300 | ** |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 301 | ** SQLITE_AFF_INTEGER: |
| 302 | ** SQLITE_AFF_REAL: |
| 303 | ** SQLITE_AFF_NUMERIC: |
| 304 | ** Try to convert pRec to an integer representation or a |
| 305 | ** floating-point representation if an integer representation |
| 306 | ** is not possible. Note that the integer representation is |
| 307 | ** always preferred, even if the affinity is REAL, because |
| 308 | ** an integer representation is more space efficient on disk. |
| 309 | ** |
| 310 | ** SQLITE_AFF_TEXT: |
| 311 | ** Convert pRec to a text representation. |
| 312 | ** |
drh | 05883a3 | 2015-06-02 15:32:08 +0000 | [diff] [blame] | 313 | ** SQLITE_AFF_BLOB: |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 314 | ** No-op. pRec is unchanged. |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 315 | */ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 316 | static void applyAffinity( |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 317 | Mem *pRec, /* The value to apply affinity to */ |
| 318 | char affinity, /* The affinity to be applied */ |
| 319 | u8 enc /* Use this text encoding */ |
| 320 | ){ |
drh | 7ea31cc | 2014-09-18 14:36:00 +0000 | [diff] [blame] | 321 | if( affinity>=SQLITE_AFF_NUMERIC ){ |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 322 | assert( affinity==SQLITE_AFF_INTEGER || affinity==SQLITE_AFF_REAL |
| 323 | || affinity==SQLITE_AFF_NUMERIC ); |
drh | a3fa140 | 2016-04-29 02:55:05 +0000 | [diff] [blame] | 324 | if( (pRec->flags & MEM_Int)==0 ){ /*OPTIMIZATION-IF-FALSE*/ |
drh | bd9507c | 2014-08-23 17:21:37 +0000 | [diff] [blame] | 325 | if( (pRec->flags & MEM_Real)==0 ){ |
drh | 11a6eee | 2014-09-19 22:01:54 +0000 | [diff] [blame] | 326 | if( pRec->flags & MEM_Str ) applyNumericAffinity(pRec,1); |
drh | bd9507c | 2014-08-23 17:21:37 +0000 | [diff] [blame] | 327 | }else{ |
| 328 | sqlite3VdbeIntegerAffinity(pRec); |
| 329 | } |
drh | 17c4029 | 2004-07-21 02:53:29 +0000 | [diff] [blame] | 330 | } |
drh | 7ea31cc | 2014-09-18 14:36:00 +0000 | [diff] [blame] | 331 | }else if( affinity==SQLITE_AFF_TEXT ){ |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 332 | /* Only attempt the conversion to TEXT if there is an integer or real |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 333 | ** representation (blob and NULL do not get converted) but no string |
drh | a3fa140 | 2016-04-29 02:55:05 +0000 | [diff] [blame] | 334 | ** representation. It would be harmless to repeat the conversion if |
| 335 | ** there is already a string rep, but it is pointless to waste those |
| 336 | ** CPU cycles. */ |
| 337 | if( 0==(pRec->flags&MEM_Str) ){ /*OPTIMIZATION-IF-FALSE*/ |
| 338 | if( (pRec->flags&(MEM_Real|MEM_Int)) ){ |
| 339 | sqlite3VdbeMemStringify(pRec, enc, 1); |
| 340 | } |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 341 | } |
dan | dde548c | 2015-05-19 19:44:25 +0000 | [diff] [blame] | 342 | pRec->flags &= ~(MEM_Real|MEM_Int); |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 343 | } |
| 344 | } |
| 345 | |
danielk1977 | aee18ef | 2005-03-09 12:26:50 +0000 | [diff] [blame] | 346 | /* |
drh | 29d7210 | 2006-02-09 22:13:41 +0000 | [diff] [blame] | 347 | ** Try to convert the type of a function argument or a result column |
| 348 | ** into a numeric representation. Use either INTEGER or REAL whichever |
| 349 | ** is appropriate. But only do the conversion if it is possible without |
| 350 | ** loss of information and return the revised type of the argument. |
drh | 29d7210 | 2006-02-09 22:13:41 +0000 | [diff] [blame] | 351 | */ |
| 352 | int sqlite3_value_numeric_type(sqlite3_value *pVal){ |
drh | 1b27b8c | 2014-02-10 03:21:57 +0000 | [diff] [blame] | 353 | int eType = sqlite3_value_type(pVal); |
| 354 | if( eType==SQLITE_TEXT ){ |
| 355 | Mem *pMem = (Mem*)pVal; |
drh | bd9507c | 2014-08-23 17:21:37 +0000 | [diff] [blame] | 356 | applyNumericAffinity(pMem, 0); |
drh | 1b27b8c | 2014-02-10 03:21:57 +0000 | [diff] [blame] | 357 | eType = sqlite3_value_type(pVal); |
drh | e5a8a1d | 2010-11-18 12:31:24 +0000 | [diff] [blame] | 358 | } |
drh | 1b27b8c | 2014-02-10 03:21:57 +0000 | [diff] [blame] | 359 | return eType; |
drh | 29d7210 | 2006-02-09 22:13:41 +0000 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | /* |
danielk1977 | aee18ef | 2005-03-09 12:26:50 +0000 | [diff] [blame] | 363 | ** Exported version of applyAffinity(). This one works on sqlite3_value*, |
| 364 | ** not the internal Mem* type. |
| 365 | */ |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 366 | void sqlite3ValueApplyAffinity( |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 367 | sqlite3_value *pVal, |
| 368 | u8 affinity, |
| 369 | u8 enc |
| 370 | ){ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 371 | applyAffinity((Mem *)pVal, affinity, enc); |
danielk1977 | aee18ef | 2005-03-09 12:26:50 +0000 | [diff] [blame] | 372 | } |
| 373 | |
drh | 3d1d90a | 2014-03-24 15:00:15 +0000 | [diff] [blame] | 374 | /* |
drh | f1a89ed | 2014-08-23 17:41:15 +0000 | [diff] [blame] | 375 | ** pMem currently only holds a string type (or maybe a BLOB that we can |
| 376 | ** interpret as a string if we want to). Compute its corresponding |
drh | 74eaba4 | 2014-09-18 17:52:15 +0000 | [diff] [blame] | 377 | ** numeric type, if has one. Set the pMem->u.r and pMem->u.i fields |
drh | f1a89ed | 2014-08-23 17:41:15 +0000 | [diff] [blame] | 378 | ** accordingly. |
| 379 | */ |
| 380 | static u16 SQLITE_NOINLINE computeNumericType(Mem *pMem){ |
| 381 | assert( (pMem->flags & (MEM_Int|MEM_Real))==0 ); |
| 382 | assert( (pMem->flags & (MEM_Str|MEM_Blob))!=0 ); |
drh | 74eaba4 | 2014-09-18 17:52:15 +0000 | [diff] [blame] | 383 | if( sqlite3AtoF(pMem->z, &pMem->u.r, pMem->n, pMem->enc)==0 ){ |
drh | f1a89ed | 2014-08-23 17:41:15 +0000 | [diff] [blame] | 384 | return 0; |
| 385 | } |
drh | 84d4f1a | 2017-09-20 10:47:10 +0000 | [diff] [blame] | 386 | if( sqlite3Atoi64(pMem->z, &pMem->u.i, pMem->n, pMem->enc)==0 ){ |
drh | f1a89ed | 2014-08-23 17:41:15 +0000 | [diff] [blame] | 387 | return MEM_Int; |
| 388 | } |
| 389 | return MEM_Real; |
| 390 | } |
| 391 | |
| 392 | /* |
drh | 3d1d90a | 2014-03-24 15:00:15 +0000 | [diff] [blame] | 393 | ** Return the numeric type for pMem, either MEM_Int or MEM_Real or both or |
| 394 | ** none. |
| 395 | ** |
| 396 | ** Unlike applyNumericAffinity(), this routine does not modify pMem->flags. |
drh | 74eaba4 | 2014-09-18 17:52:15 +0000 | [diff] [blame] | 397 | ** But it does set pMem->u.r and pMem->u.i appropriately. |
drh | 3d1d90a | 2014-03-24 15:00:15 +0000 | [diff] [blame] | 398 | */ |
| 399 | static u16 numericType(Mem *pMem){ |
| 400 | if( pMem->flags & (MEM_Int|MEM_Real) ){ |
| 401 | return pMem->flags & (MEM_Int|MEM_Real); |
| 402 | } |
| 403 | if( pMem->flags & (MEM_Str|MEM_Blob) ){ |
drh | f1a89ed | 2014-08-23 17:41:15 +0000 | [diff] [blame] | 404 | return computeNumericType(pMem); |
drh | 3d1d90a | 2014-03-24 15:00:15 +0000 | [diff] [blame] | 405 | } |
| 406 | return 0; |
| 407 | } |
| 408 | |
danielk1977 | b5402fb | 2005-01-12 07:15:04 +0000 | [diff] [blame] | 409 | #ifdef SQLITE_DEBUG |
drh | b6f5452 | 2004-05-20 02:42:16 +0000 | [diff] [blame] | 410 | /* |
danielk1977 | ca6b291 | 2004-05-21 10:49:47 +0000 | [diff] [blame] | 411 | ** Write a nice string representation of the contents of cell pMem |
| 412 | ** into buffer zBuf, length nBuf. |
| 413 | */ |
drh | 7416170 | 2006-02-24 02:53:49 +0000 | [diff] [blame] | 414 | void sqlite3VdbeMemPrettyPrint(Mem *pMem, char *zBuf){ |
danielk1977 | ca6b291 | 2004-05-21 10:49:47 +0000 | [diff] [blame] | 415 | char *zCsr = zBuf; |
| 416 | int f = pMem->flags; |
| 417 | |
drh | 5719628 | 2004-10-06 15:41:16 +0000 | [diff] [blame] | 418 | static const char *const encnames[] = {"(X)", "(8)", "(16LE)", "(16BE)"}; |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame] | 419 | |
danielk1977 | ca6b291 | 2004-05-21 10:49:47 +0000 | [diff] [blame] | 420 | if( f&MEM_Blob ){ |
| 421 | int i; |
| 422 | char c; |
| 423 | if( f & MEM_Dyn ){ |
| 424 | c = 'z'; |
| 425 | assert( (f & (MEM_Static|MEM_Ephem))==0 ); |
| 426 | }else if( f & MEM_Static ){ |
| 427 | c = 't'; |
| 428 | assert( (f & (MEM_Dyn|MEM_Ephem))==0 ); |
| 429 | }else if( f & MEM_Ephem ){ |
| 430 | c = 'e'; |
| 431 | assert( (f & (MEM_Static|MEM_Dyn))==0 ); |
| 432 | }else{ |
| 433 | c = 's'; |
| 434 | } |
drh | 85c2dc0 | 2017-03-16 13:30:58 +0000 | [diff] [blame] | 435 | *(zCsr++) = c; |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 436 | sqlite3_snprintf(100, zCsr, "%d[", pMem->n); |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 437 | zCsr += sqlite3Strlen30(zCsr); |
danielk1977 | ca6b291 | 2004-05-21 10:49:47 +0000 | [diff] [blame] | 438 | for(i=0; i<16 && i<pMem->n; i++){ |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 439 | sqlite3_snprintf(100, zCsr, "%02X", ((int)pMem->z[i] & 0xFF)); |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 440 | zCsr += sqlite3Strlen30(zCsr); |
danielk1977 | ca6b291 | 2004-05-21 10:49:47 +0000 | [diff] [blame] | 441 | } |
| 442 | for(i=0; i<16 && i<pMem->n; i++){ |
| 443 | char z = pMem->z[i]; |
| 444 | if( z<32 || z>126 ) *zCsr++ = '.'; |
| 445 | else *zCsr++ = z; |
| 446 | } |
drh | 85c2dc0 | 2017-03-16 13:30:58 +0000 | [diff] [blame] | 447 | *(zCsr++) = ']'; |
drh | fdf972a | 2007-05-02 13:30:27 +0000 | [diff] [blame] | 448 | if( f & MEM_Zero ){ |
drh | 8df3284 | 2008-12-09 02:51:23 +0000 | [diff] [blame] | 449 | sqlite3_snprintf(100, zCsr,"+%dz",pMem->u.nZero); |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 450 | zCsr += sqlite3Strlen30(zCsr); |
drh | fdf972a | 2007-05-02 13:30:27 +0000 | [diff] [blame] | 451 | } |
danielk1977 | b1bc953 | 2004-05-22 03:05:33 +0000 | [diff] [blame] | 452 | *zCsr = '\0'; |
| 453 | }else if( f & MEM_Str ){ |
| 454 | int j, k; |
| 455 | zBuf[0] = ' '; |
| 456 | if( f & MEM_Dyn ){ |
| 457 | zBuf[1] = 'z'; |
| 458 | assert( (f & (MEM_Static|MEM_Ephem))==0 ); |
| 459 | }else if( f & MEM_Static ){ |
| 460 | zBuf[1] = 't'; |
| 461 | assert( (f & (MEM_Dyn|MEM_Ephem))==0 ); |
| 462 | }else if( f & MEM_Ephem ){ |
| 463 | zBuf[1] = 'e'; |
| 464 | assert( (f & (MEM_Static|MEM_Dyn))==0 ); |
| 465 | }else{ |
| 466 | zBuf[1] = 's'; |
| 467 | } |
| 468 | k = 2; |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 469 | sqlite3_snprintf(100, &zBuf[k], "%d", pMem->n); |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 470 | k += sqlite3Strlen30(&zBuf[k]); |
danielk1977 | b1bc953 | 2004-05-22 03:05:33 +0000 | [diff] [blame] | 471 | zBuf[k++] = '['; |
| 472 | for(j=0; j<15 && j<pMem->n; j++){ |
| 473 | u8 c = pMem->z[j]; |
danielk1977 | b1bc953 | 2004-05-22 03:05:33 +0000 | [diff] [blame] | 474 | if( c>=0x20 && c<0x7f ){ |
| 475 | zBuf[k++] = c; |
| 476 | }else{ |
| 477 | zBuf[k++] = '.'; |
| 478 | } |
| 479 | } |
| 480 | zBuf[k++] = ']'; |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 481 | sqlite3_snprintf(100,&zBuf[k], encnames[pMem->enc]); |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 482 | k += sqlite3Strlen30(&zBuf[k]); |
danielk1977 | b1bc953 | 2004-05-22 03:05:33 +0000 | [diff] [blame] | 483 | zBuf[k++] = 0; |
danielk1977 | ca6b291 | 2004-05-21 10:49:47 +0000 | [diff] [blame] | 484 | } |
danielk1977 | ca6b291 | 2004-05-21 10:49:47 +0000 | [diff] [blame] | 485 | } |
| 486 | #endif |
| 487 | |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 488 | #ifdef SQLITE_DEBUG |
| 489 | /* |
| 490 | ** Print the value of a register for tracing purposes: |
| 491 | */ |
drh | 84e55a8 | 2013-11-13 17:58:23 +0000 | [diff] [blame] | 492 | static void memTracePrint(Mem *p){ |
drh | a5750cf | 2014-02-07 13:20:31 +0000 | [diff] [blame] | 493 | if( p->flags & MEM_Undefined ){ |
drh | 84e55a8 | 2013-11-13 17:58:23 +0000 | [diff] [blame] | 494 | printf(" undefined"); |
drh | 953f761 | 2012-12-07 22:18:54 +0000 | [diff] [blame] | 495 | }else if( p->flags & MEM_Null ){ |
drh | ce2fbd1 | 2018-01-12 21:00:14 +0000 | [diff] [blame] | 496 | printf(p->flags & MEM_Zero ? " NULL-nochng" : " NULL"); |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 497 | }else if( (p->flags & (MEM_Int|MEM_Str))==(MEM_Int|MEM_Str) ){ |
drh | 84e55a8 | 2013-11-13 17:58:23 +0000 | [diff] [blame] | 498 | printf(" si:%lld", p->u.i); |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 499 | }else if( p->flags & MEM_Int ){ |
drh | 84e55a8 | 2013-11-13 17:58:23 +0000 | [diff] [blame] | 500 | printf(" i:%lld", p->u.i); |
drh | 0b3bf92 | 2009-06-15 20:45:34 +0000 | [diff] [blame] | 501 | #ifndef SQLITE_OMIT_FLOATING_POINT |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 502 | }else if( p->flags & MEM_Real ){ |
drh | 74eaba4 | 2014-09-18 17:52:15 +0000 | [diff] [blame] | 503 | printf(" r:%g", p->u.r); |
drh | 0b3bf92 | 2009-06-15 20:45:34 +0000 | [diff] [blame] | 504 | #endif |
drh | 733bf1b | 2009-04-22 00:47:00 +0000 | [diff] [blame] | 505 | }else if( p->flags & MEM_RowSet ){ |
drh | 84e55a8 | 2013-11-13 17:58:23 +0000 | [diff] [blame] | 506 | printf(" (rowset)"); |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 507 | }else{ |
| 508 | char zBuf[200]; |
| 509 | sqlite3VdbeMemPrettyPrint(p, zBuf); |
drh | 84e55a8 | 2013-11-13 17:58:23 +0000 | [diff] [blame] | 510 | printf(" %s", zBuf); |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 511 | } |
dan | 5b6c8e4 | 2016-01-30 15:46:03 +0000 | [diff] [blame] | 512 | if( p->flags & MEM_Subtype ) printf(" subtype=0x%02x", p->eSubtype); |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 513 | } |
drh | 84e55a8 | 2013-11-13 17:58:23 +0000 | [diff] [blame] | 514 | static void registerTrace(int iReg, Mem *p){ |
| 515 | printf("REG[%d] = ", iReg); |
| 516 | memTracePrint(p); |
| 517 | printf("\n"); |
drh | e2bc655 | 2017-04-17 20:50:34 +0000 | [diff] [blame] | 518 | sqlite3VdbeCheckMemInvariants(p); |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 519 | } |
| 520 | #endif |
| 521 | |
| 522 | #ifdef SQLITE_DEBUG |
drh | 84e55a8 | 2013-11-13 17:58:23 +0000 | [diff] [blame] | 523 | # define REGISTER_TRACE(R,M) if(db->flags&SQLITE_VdbeTrace)registerTrace(R,M) |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 524 | #else |
| 525 | # define REGISTER_TRACE(R,M) |
| 526 | #endif |
| 527 | |
danielk1977 | 84ac9d0 | 2004-05-18 09:58:06 +0000 | [diff] [blame] | 528 | |
drh | 7b39686 | 2003-01-01 23:06:20 +0000 | [diff] [blame] | 529 | #ifdef VDBE_PROFILE |
shane | 9bcbdad | 2008-05-29 20:22:37 +0000 | [diff] [blame] | 530 | |
| 531 | /* |
| 532 | ** hwtime.h contains inline assembler code for implementing |
| 533 | ** high-performance timing routines. |
drh | 7b39686 | 2003-01-01 23:06:20 +0000 | [diff] [blame] | 534 | */ |
shane | 9bcbdad | 2008-05-29 20:22:37 +0000 | [diff] [blame] | 535 | #include "hwtime.h" |
| 536 | |
drh | 7b39686 | 2003-01-01 23:06:20 +0000 | [diff] [blame] | 537 | #endif |
| 538 | |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 539 | #ifndef NDEBUG |
| 540 | /* |
| 541 | ** This function is only called from within an assert() expression. It |
| 542 | ** checks that the sqlite3.nTransaction variable is correctly set to |
| 543 | ** the number of non-transaction savepoints currently in the |
| 544 | ** linked list starting at sqlite3.pSavepoint. |
| 545 | ** |
| 546 | ** Usage: |
| 547 | ** |
| 548 | ** assert( checkSavepointCount(db) ); |
| 549 | */ |
| 550 | static int checkSavepointCount(sqlite3 *db){ |
| 551 | int n = 0; |
| 552 | Savepoint *p; |
| 553 | for(p=db->pSavepoint; p; p=p->pNext) n++; |
| 554 | assert( n==(db->nSavepoint + db->isTransactionSavepoint) ); |
| 555 | return 1; |
| 556 | } |
| 557 | #endif |
| 558 | |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 559 | /* |
| 560 | ** Return the register of pOp->p2 after first preparing it to be |
| 561 | ** overwritten with an integer value. |
drh | 9eef8c6 | 2015-10-15 17:31:41 +0000 | [diff] [blame] | 562 | */ |
| 563 | static SQLITE_NOINLINE Mem *out2PrereleaseWithClear(Mem *pOut){ |
| 564 | sqlite3VdbeMemSetNull(pOut); |
| 565 | pOut->flags = MEM_Int; |
| 566 | return pOut; |
| 567 | } |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 568 | static Mem *out2Prerelease(Vdbe *p, VdbeOp *pOp){ |
| 569 | Mem *pOut; |
| 570 | assert( pOp->p2>0 ); |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 571 | assert( pOp->p2<=(p->nMem+1 - p->nCursor) ); |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 572 | pOut = &p->aMem[pOp->p2]; |
| 573 | memAboutToChange(p, pOut); |
drh | a3fa140 | 2016-04-29 02:55:05 +0000 | [diff] [blame] | 574 | if( VdbeMemDynamic(pOut) ){ /*OPTIMIZATION-IF-FALSE*/ |
drh | 9eef8c6 | 2015-10-15 17:31:41 +0000 | [diff] [blame] | 575 | return out2PrereleaseWithClear(pOut); |
| 576 | }else{ |
| 577 | pOut->flags = MEM_Int; |
| 578 | return pOut; |
| 579 | } |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 580 | } |
| 581 | |
drh | b975598 | 2010-07-24 16:34:37 +0000 | [diff] [blame] | 582 | |
| 583 | /* |
drh | 0fd6135 | 2014-02-07 02:29:45 +0000 | [diff] [blame] | 584 | ** Execute as much of a VDBE program as we can. |
| 585 | ** This is the core of sqlite3_step(). |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 586 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 587 | int sqlite3VdbeExec( |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 588 | Vdbe *p /* The VDBE */ |
| 589 | ){ |
drh | bbe879d | 2009-11-14 18:04:35 +0000 | [diff] [blame] | 590 | Op *aOp = p->aOp; /* Copy of p->aOp */ |
mistachkin | 5f7b95f | 2017-02-01 23:03:54 +0000 | [diff] [blame] | 591 | Op *pOp = aOp; /* Current operation */ |
drh | 6dc4148 | 2015-04-16 17:31:02 +0000 | [diff] [blame] | 592 | #if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE) |
| 593 | Op *pOrigOp; /* Value of pOp at the top of the loop */ |
| 594 | #endif |
drh | b89aeb6 | 2016-01-27 15:49:32 +0000 | [diff] [blame] | 595 | #ifdef SQLITE_DEBUG |
drh | def19e3 | 2016-01-27 16:26:25 +0000 | [diff] [blame] | 596 | int nExtraDelete = 0; /* Verifies FORDELETE and AUXDELETE flags */ |
drh | b89aeb6 | 2016-01-27 15:49:32 +0000 | [diff] [blame] | 597 | #endif |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 598 | int rc = SQLITE_OK; /* Value to return */ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 599 | sqlite3 *db = p->db; /* The database */ |
drh | cdf011d | 2011-04-04 21:25:28 +0000 | [diff] [blame] | 600 | u8 resetSchemaOnFault = 0; /* Reset schema after an error if positive */ |
drh | 8079a0d | 2006-01-12 17:20:50 +0000 | [diff] [blame] | 601 | u8 encoding = ENC(db); /* The database encoding */ |
drh | 0f825a7 | 2016-08-13 14:17:02 +0000 | [diff] [blame] | 602 | int iCompare = 0; /* Result of last comparison */ |
drh | bf159fa | 2013-06-25 22:01:22 +0000 | [diff] [blame] | 603 | unsigned nVmStep = 0; /* Number of virtual machine steps */ |
drh | 49afe3a | 2013-07-10 03:05:14 +0000 | [diff] [blame] | 604 | #ifndef SQLITE_OMIT_PROGRESS_CALLBACK |
drh | 2ab792e | 2017-05-30 18:34:07 +0000 | [diff] [blame] | 605 | unsigned nProgressLimit; /* Invoke xProgress() when nVmStep reaches this */ |
drh | 49afe3a | 2013-07-10 03:05:14 +0000 | [diff] [blame] | 606 | #endif |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 607 | Mem *aMem = p->aMem; /* Copy of p->aMem */ |
drh | b27b7f5 | 2008-12-10 18:03:45 +0000 | [diff] [blame] | 608 | Mem *pIn1 = 0; /* 1st input operand */ |
| 609 | Mem *pIn2 = 0; /* 2nd input operand */ |
| 610 | Mem *pIn3 = 0; /* 3rd input operand */ |
| 611 | Mem *pOut = 0; /* Output operand */ |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 612 | #ifdef VDBE_PROFILE |
shane | 9bcbdad | 2008-05-29 20:22:37 +0000 | [diff] [blame] | 613 | u64 start; /* CPU clock count at start of opcode */ |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 614 | #endif |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 615 | /*** INSERT STACK UNION HERE ***/ |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 616 | |
drh | ca48c90 | 2008-01-18 14:08:24 +0000 | [diff] [blame] | 617 | assert( p->magic==VDBE_MAGIC_RUN ); /* sqlite3_step() verifies this */ |
drh | bdaec52 | 2011-04-04 00:14:43 +0000 | [diff] [blame] | 618 | sqlite3VdbeEnter(p); |
danielk1977 | 2e588c7 | 2005-12-09 14:25:08 +0000 | [diff] [blame] | 619 | if( p->rc==SQLITE_NOMEM ){ |
| 620 | /* This happens if a malloc() inside a call to sqlite3_column_text() or |
| 621 | ** sqlite3_column_text16() failed. */ |
| 622 | goto no_mem; |
| 623 | } |
drh | cbd8db3 | 2015-08-20 17:18:32 +0000 | [diff] [blame] | 624 | assert( p->rc==SQLITE_OK || (p->rc&0xff)==SQLITE_BUSY ); |
drh | 1713afb | 2013-06-28 01:24:57 +0000 | [diff] [blame] | 625 | assert( p->bIsReader || p->readOnly!=0 ); |
drh | 95a7b3e | 2013-09-16 12:57:19 +0000 | [diff] [blame] | 626 | p->iCurrentTime = 0; |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 627 | assert( p->explain==0 ); |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 628 | p->pResultSet = 0; |
drh | a4afb65 | 2005-07-09 02:16:02 +0000 | [diff] [blame] | 629 | db->busyHandler.nBusy = 0; |
drh | 0fd6135 | 2014-02-07 02:29:45 +0000 | [diff] [blame] | 630 | if( db->u1.isInterrupted ) goto abort_due_to_interrupt; |
drh | 602c237 | 2007-03-01 00:29:13 +0000 | [diff] [blame] | 631 | sqlite3VdbeIOTraceSql(p); |
drh | 0d1961e | 2013-07-25 16:27:51 +0000 | [diff] [blame] | 632 | #ifndef SQLITE_OMIT_PROGRESS_CALLBACK |
| 633 | if( db->xProgress ){ |
drh | 6cbbdb0 | 2015-06-24 14:36:27 +0000 | [diff] [blame] | 634 | u32 iPrior = p->aCounter[SQLITE_STMTSTATUS_VM_STEP]; |
drh | 0d1961e | 2013-07-25 16:27:51 +0000 | [diff] [blame] | 635 | assert( 0 < db->nProgressOps ); |
drh | 6cbbdb0 | 2015-06-24 14:36:27 +0000 | [diff] [blame] | 636 | nProgressLimit = db->nProgressOps - (iPrior % db->nProgressOps); |
drh | 2ab792e | 2017-05-30 18:34:07 +0000 | [diff] [blame] | 637 | }else{ |
| 638 | nProgressLimit = 0xffffffff; |
drh | 0d1961e | 2013-07-25 16:27:51 +0000 | [diff] [blame] | 639 | } |
| 640 | #endif |
drh | 3c23a88 | 2007-01-09 14:01:13 +0000 | [diff] [blame] | 641 | #ifdef SQLITE_DEBUG |
danielk1977 | 2d1d86f | 2008-06-20 14:59:51 +0000 | [diff] [blame] | 642 | sqlite3BeginBenignMalloc(); |
drh | 84e55a8 | 2013-11-13 17:58:23 +0000 | [diff] [blame] | 643 | if( p->pc==0 |
| 644 | && (p->db->flags & (SQLITE_VdbeListing|SQLITE_VdbeEQP|SQLITE_VdbeTrace))!=0 |
| 645 | ){ |
drh | 3c23a88 | 2007-01-09 14:01:13 +0000 | [diff] [blame] | 646 | int i; |
drh | 84e55a8 | 2013-11-13 17:58:23 +0000 | [diff] [blame] | 647 | int once = 1; |
drh | 3c23a88 | 2007-01-09 14:01:13 +0000 | [diff] [blame] | 648 | sqlite3VdbePrintSql(p); |
drh | 84e55a8 | 2013-11-13 17:58:23 +0000 | [diff] [blame] | 649 | if( p->db->flags & SQLITE_VdbeListing ){ |
| 650 | printf("VDBE Program Listing:\n"); |
| 651 | for(i=0; i<p->nOp; i++){ |
| 652 | sqlite3VdbePrintOp(stdout, i, &aOp[i]); |
| 653 | } |
drh | 3c23a88 | 2007-01-09 14:01:13 +0000 | [diff] [blame] | 654 | } |
drh | 84e55a8 | 2013-11-13 17:58:23 +0000 | [diff] [blame] | 655 | if( p->db->flags & SQLITE_VdbeEQP ){ |
| 656 | for(i=0; i<p->nOp; i++){ |
| 657 | if( aOp[i].opcode==OP_Explain ){ |
| 658 | if( once ) printf("VDBE Query Plan:\n"); |
| 659 | printf("%s\n", aOp[i].p4.z); |
| 660 | once = 0; |
| 661 | } |
| 662 | } |
| 663 | } |
| 664 | if( p->db->flags & SQLITE_VdbeTrace ) printf("VDBE Trace:\n"); |
drh | 3c23a88 | 2007-01-09 14:01:13 +0000 | [diff] [blame] | 665 | } |
danielk1977 | 2d1d86f | 2008-06-20 14:59:51 +0000 | [diff] [blame] | 666 | sqlite3EndBenignMalloc(); |
drh | 3c23a88 | 2007-01-09 14:01:13 +0000 | [diff] [blame] | 667 | #endif |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 668 | for(pOp=&aOp[p->pc]; 1; pOp++){ |
| 669 | /* Errors are detected by individual opcodes, with an immediate |
| 670 | ** jumps to abort_due_to_error. */ |
| 671 | assert( rc==SQLITE_OK ); |
| 672 | |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 673 | assert( pOp>=aOp && pOp<&aOp[p->nOp]); |
drh | 7b39686 | 2003-01-01 23:06:20 +0000 | [diff] [blame] | 674 | #ifdef VDBE_PROFILE |
drh | 35043cc | 2018-02-12 20:27:34 +0000 | [diff] [blame] | 675 | start = sqlite3NProfileCnt ? sqlite3NProfileCnt : sqlite3Hwtime(); |
drh | 7b39686 | 2003-01-01 23:06:20 +0000 | [diff] [blame] | 676 | #endif |
drh | bf159fa | 2013-06-25 22:01:22 +0000 | [diff] [blame] | 677 | nVmStep++; |
dan | 6f9702e | 2014-11-01 20:38:06 +0000 | [diff] [blame] | 678 | #ifdef SQLITE_ENABLE_STMT_SCANSTATUS |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 679 | if( p->anExec ) p->anExec[(int)(pOp-aOp)]++; |
dan | 6f9702e | 2014-11-01 20:38:06 +0000 | [diff] [blame] | 680 | #endif |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 681 | |
danielk1977 | 8b60e0f | 2005-01-12 09:10:39 +0000 | [diff] [blame] | 682 | /* Only allow tracing if SQLITE_DEBUG is defined. |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 683 | */ |
danielk1977 | 8b60e0f | 2005-01-12 09:10:39 +0000 | [diff] [blame] | 684 | #ifdef SQLITE_DEBUG |
drh | 84e55a8 | 2013-11-13 17:58:23 +0000 | [diff] [blame] | 685 | if( db->flags & SQLITE_VdbeTrace ){ |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 686 | sqlite3VdbePrintOp(stdout, (int)(pOp - aOp), pOp); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 687 | } |
drh | 3f7d4e4 | 2004-07-24 14:35:58 +0000 | [diff] [blame] | 688 | #endif |
| 689 | |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 690 | |
drh | f603871 | 2004-02-08 18:07:34 +0000 | [diff] [blame] | 691 | /* Check to see if we need to simulate an interrupt. This only happens |
| 692 | ** if we have a special test build. |
| 693 | */ |
| 694 | #ifdef SQLITE_TEST |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 695 | if( sqlite3_interrupt_count>0 ){ |
| 696 | sqlite3_interrupt_count--; |
| 697 | if( sqlite3_interrupt_count==0 ){ |
| 698 | sqlite3_interrupt(db); |
drh | f603871 | 2004-02-08 18:07:34 +0000 | [diff] [blame] | 699 | } |
| 700 | } |
| 701 | #endif |
| 702 | |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 703 | /* Sanity checking on other operands */ |
| 704 | #ifdef SQLITE_DEBUG |
drh | 7cc84c2 | 2016-04-11 13:36:42 +0000 | [diff] [blame] | 705 | { |
| 706 | u8 opProperty = sqlite3OpcodeProperty[pOp->opcode]; |
| 707 | if( (opProperty & OPFLG_IN1)!=0 ){ |
| 708 | assert( pOp->p1>0 ); |
| 709 | assert( pOp->p1<=(p->nMem+1 - p->nCursor) ); |
| 710 | assert( memIsValid(&aMem[pOp->p1]) ); |
| 711 | assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p1]) ); |
| 712 | REGISTER_TRACE(pOp->p1, &aMem[pOp->p1]); |
| 713 | } |
| 714 | if( (opProperty & OPFLG_IN2)!=0 ){ |
| 715 | assert( pOp->p2>0 ); |
| 716 | assert( pOp->p2<=(p->nMem+1 - p->nCursor) ); |
| 717 | assert( memIsValid(&aMem[pOp->p2]) ); |
| 718 | assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p2]) ); |
| 719 | REGISTER_TRACE(pOp->p2, &aMem[pOp->p2]); |
| 720 | } |
| 721 | if( (opProperty & OPFLG_IN3)!=0 ){ |
| 722 | assert( pOp->p3>0 ); |
| 723 | assert( pOp->p3<=(p->nMem+1 - p->nCursor) ); |
| 724 | assert( memIsValid(&aMem[pOp->p3]) ); |
| 725 | assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p3]) ); |
| 726 | REGISTER_TRACE(pOp->p3, &aMem[pOp->p3]); |
| 727 | } |
| 728 | if( (opProperty & OPFLG_OUT2)!=0 ){ |
| 729 | assert( pOp->p2>0 ); |
| 730 | assert( pOp->p2<=(p->nMem+1 - p->nCursor) ); |
| 731 | memAboutToChange(p, &aMem[pOp->p2]); |
| 732 | } |
| 733 | if( (opProperty & OPFLG_OUT3)!=0 ){ |
| 734 | assert( pOp->p3>0 ); |
| 735 | assert( pOp->p3<=(p->nMem+1 - p->nCursor) ); |
| 736 | memAboutToChange(p, &aMem[pOp->p3]); |
| 737 | } |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 738 | } |
| 739 | #endif |
drh | 6dc4148 | 2015-04-16 17:31:02 +0000 | [diff] [blame] | 740 | #if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE) |
| 741 | pOrigOp = pOp; |
| 742 | #endif |
drh | 93952eb | 2009-11-13 19:43:43 +0000 | [diff] [blame] | 743 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 744 | switch( pOp->opcode ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 745 | |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 746 | /***************************************************************************** |
| 747 | ** What follows is a massive switch statement where each case implements a |
| 748 | ** separate instruction in the virtual machine. If we follow the usual |
| 749 | ** indentation conventions, each case should be indented by 6 spaces. But |
| 750 | ** that is a lot of wasted space on the left margin. So the code within |
| 751 | ** the switch statement will break with convention and be flush-left. Another |
| 752 | ** big comment (similar to this one) will mark the point in the code where |
| 753 | ** we transition back to normal indentation. |
drh | ac82fcf | 2002-09-08 17:23:41 +0000 | [diff] [blame] | 754 | ** |
| 755 | ** The formatting of each case is important. The makefile for SQLite |
| 756 | ** generates two C files "opcodes.h" and "opcodes.c" by scanning this |
| 757 | ** file looking for lines that begin with "case OP_". The opcodes.h files |
| 758 | ** will be filled with #defines that give unique integer values to each |
| 759 | ** 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] | 760 | ** each string is the symbolic name for the corresponding opcode. If the |
| 761 | ** case statement is followed by a comment of the form "/# same as ... #/" |
| 762 | ** that comment is used to determine the particular value of the opcode. |
drh | ac82fcf | 2002-09-08 17:23:41 +0000 | [diff] [blame] | 763 | ** |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 764 | ** Other keywords in the comment that follows each case are used to |
| 765 | ** construct the OPFLG_INITIALIZER value that initializes opcodeProperty[]. |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 766 | ** Keywords include: in1, in2, in3, out2, out3. See |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 767 | ** the mkopcodeh.awk script for additional information. |
danielk1977 | bc04f85 | 2005-03-29 08:26:13 +0000 | [diff] [blame] | 768 | ** |
drh | ac82fcf | 2002-09-08 17:23:41 +0000 | [diff] [blame] | 769 | ** Documentation about VDBE opcodes is generated by scanning this file |
| 770 | ** for lines of that contain "Opcode:". That line and all subsequent |
| 771 | ** comment lines are used in the generation of the opcode.html documentation |
| 772 | ** file. |
| 773 | ** |
| 774 | ** SUMMARY: |
| 775 | ** |
| 776 | ** Formatting is important to scripts that scan this file. |
| 777 | ** Do not deviate from the formatting style currently in use. |
| 778 | ** |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 779 | *****************************************************************************/ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 780 | |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 781 | /* Opcode: Goto * P2 * * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 782 | ** |
| 783 | ** An unconditional jump to address P2. |
| 784 | ** The next instruction executed will be |
| 785 | ** the one at index P2 from the beginning of |
| 786 | ** the program. |
drh | fe70510 | 2014-03-06 13:38:37 +0000 | [diff] [blame] | 787 | ** |
| 788 | ** The P1 parameter is not actually used by this opcode. However, it |
| 789 | ** is sometimes set to 1 instead of 0 as a hint to the command-line shell |
| 790 | ** that this Goto is the bottom of a loop and that the lines from P2 down |
| 791 | ** to the current line should be indented for EXPLAIN output. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 792 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 793 | case OP_Goto: { /* jump */ |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 794 | jump_to_p2_and_check_for_interrupt: |
| 795 | pOp = &aOp[pOp->p2 - 1]; |
drh | 49afe3a | 2013-07-10 03:05:14 +0000 | [diff] [blame] | 796 | |
| 797 | /* Opcodes that are used as the bottom of a loop (OP_Next, OP_Prev, |
drh | bb6783b | 2017-04-29 18:02:49 +0000 | [diff] [blame] | 798 | ** OP_VNext, or OP_SorterNext) all jump here upon |
drh | 49afe3a | 2013-07-10 03:05:14 +0000 | [diff] [blame] | 799 | ** completion. Check to see if sqlite3_interrupt() has been called |
| 800 | ** or if the progress callback needs to be invoked. |
| 801 | ** |
| 802 | ** This code uses unstructured "goto" statements and does not look clean. |
| 803 | ** But that is not due to sloppy coding habits. The code is written this |
| 804 | ** way for performance, to avoid having to run the interrupt and progress |
| 805 | ** checks on every opcode. This helps sqlite3_step() to run about 1.5% |
| 806 | ** faster according to "valgrind --tool=cachegrind" */ |
| 807 | check_for_interrupt: |
drh | 0fd6135 | 2014-02-07 02:29:45 +0000 | [diff] [blame] | 808 | if( db->u1.isInterrupted ) goto abort_due_to_interrupt; |
drh | 49afe3a | 2013-07-10 03:05:14 +0000 | [diff] [blame] | 809 | #ifndef SQLITE_OMIT_PROGRESS_CALLBACK |
| 810 | /* Call the progress callback if it is configured and the required number |
| 811 | ** of VDBE ops have been executed (either since this invocation of |
| 812 | ** sqlite3VdbeExec() or since last time the progress callback was called). |
| 813 | ** If the progress callback returns non-zero, exit the virtual machine with |
| 814 | ** a return code SQLITE_ABORT. |
| 815 | */ |
drh | 2ab792e | 2017-05-30 18:34:07 +0000 | [diff] [blame] | 816 | if( nVmStep>=nProgressLimit && db->xProgress!=0 ){ |
drh | 400fcba | 2013-11-14 00:09:48 +0000 | [diff] [blame] | 817 | assert( db->nProgressOps!=0 ); |
| 818 | nProgressLimit = nVmStep + db->nProgressOps - (nVmStep%db->nProgressOps); |
| 819 | if( db->xProgress(db->pProgressArg) ){ |
drh | 49afe3a | 2013-07-10 03:05:14 +0000 | [diff] [blame] | 820 | rc = SQLITE_INTERRUPT; |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 821 | goto abort_due_to_error; |
drh | 49afe3a | 2013-07-10 03:05:14 +0000 | [diff] [blame] | 822 | } |
drh | 49afe3a | 2013-07-10 03:05:14 +0000 | [diff] [blame] | 823 | } |
| 824 | #endif |
| 825 | |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 826 | break; |
| 827 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 828 | |
drh | 2eb9537 | 2008-06-06 15:04:36 +0000 | [diff] [blame] | 829 | /* Opcode: Gosub P1 P2 * * * |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 830 | ** |
drh | 2eb9537 | 2008-06-06 15:04:36 +0000 | [diff] [blame] | 831 | ** Write the current address onto register P1 |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 832 | ** and then jump to address P2. |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 833 | */ |
drh | b8475df | 2011-12-09 16:21:19 +0000 | [diff] [blame] | 834 | case OP_Gosub: { /* jump */ |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 835 | assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) ); |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 836 | pIn1 = &aMem[pOp->p1]; |
drh | c91b2fd | 2014-03-01 18:13:23 +0000 | [diff] [blame] | 837 | assert( VdbeMemDynamic(pIn1)==0 ); |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 838 | memAboutToChange(p, pIn1); |
drh | 2eb9537 | 2008-06-06 15:04:36 +0000 | [diff] [blame] | 839 | pIn1->flags = MEM_Int; |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 840 | pIn1->u.i = (int)(pOp-aOp); |
drh | 2eb9537 | 2008-06-06 15:04:36 +0000 | [diff] [blame] | 841 | REGISTER_TRACE(pOp->p1, pIn1); |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 842 | |
| 843 | /* Most jump operations do a goto to this spot in order to update |
| 844 | ** the pOp pointer. */ |
| 845 | jump_to_p2: |
| 846 | pOp = &aOp[pOp->p2 - 1]; |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 847 | break; |
| 848 | } |
| 849 | |
drh | 2eb9537 | 2008-06-06 15:04:36 +0000 | [diff] [blame] | 850 | /* Opcode: Return P1 * * * * |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 851 | ** |
drh | 81cf13e | 2014-02-07 18:27:53 +0000 | [diff] [blame] | 852 | ** Jump to the next instruction after the address in register P1. After |
| 853 | ** the jump, register P1 becomes undefined. |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 854 | */ |
drh | 2eb9537 | 2008-06-06 15:04:36 +0000 | [diff] [blame] | 855 | case OP_Return: { /* in1 */ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 856 | pIn1 = &aMem[pOp->p1]; |
drh | 81cf13e | 2014-02-07 18:27:53 +0000 | [diff] [blame] | 857 | assert( pIn1->flags==MEM_Int ); |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 858 | pOp = &aOp[pIn1->u.i]; |
drh | 81cf13e | 2014-02-07 18:27:53 +0000 | [diff] [blame] | 859 | pIn1->flags = MEM_Undefined; |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 860 | break; |
| 861 | } |
| 862 | |
drh | ed71a83 | 2014-02-07 19:18:10 +0000 | [diff] [blame] | 863 | /* Opcode: InitCoroutine P1 P2 P3 * * |
drh | 81cf13e | 2014-02-07 18:27:53 +0000 | [diff] [blame] | 864 | ** |
drh | 5dad9a3 | 2014-07-25 18:37:42 +0000 | [diff] [blame] | 865 | ** Set up register P1 so that it will Yield to the coroutine |
drh | ed71a83 | 2014-02-07 19:18:10 +0000 | [diff] [blame] | 866 | ** located at address P3. |
| 867 | ** |
drh | 5dad9a3 | 2014-07-25 18:37:42 +0000 | [diff] [blame] | 868 | ** If P2!=0 then the coroutine implementation immediately follows |
| 869 | ** this opcode. So jump over the coroutine implementation to |
drh | ed71a83 | 2014-02-07 19:18:10 +0000 | [diff] [blame] | 870 | ** address P2. |
drh | 5dad9a3 | 2014-07-25 18:37:42 +0000 | [diff] [blame] | 871 | ** |
| 872 | ** See also: EndCoroutine |
drh | 81cf13e | 2014-02-07 18:27:53 +0000 | [diff] [blame] | 873 | */ |
| 874 | case OP_InitCoroutine: { /* jump */ |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 875 | assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) ); |
drh | ed71a83 | 2014-02-07 19:18:10 +0000 | [diff] [blame] | 876 | assert( pOp->p2>=0 && pOp->p2<p->nOp ); |
| 877 | assert( pOp->p3>=0 && pOp->p3<p->nOp ); |
drh | 81cf13e | 2014-02-07 18:27:53 +0000 | [diff] [blame] | 878 | pOut = &aMem[pOp->p1]; |
drh | ed71a83 | 2014-02-07 19:18:10 +0000 | [diff] [blame] | 879 | assert( !VdbeMemDynamic(pOut) ); |
| 880 | pOut->u.i = pOp->p3 - 1; |
drh | 81cf13e | 2014-02-07 18:27:53 +0000 | [diff] [blame] | 881 | pOut->flags = MEM_Int; |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 882 | if( pOp->p2 ) goto jump_to_p2; |
drh | 81cf13e | 2014-02-07 18:27:53 +0000 | [diff] [blame] | 883 | break; |
| 884 | } |
| 885 | |
| 886 | /* Opcode: EndCoroutine P1 * * * * |
| 887 | ** |
drh | bc5cf38 | 2014-08-06 01:08:07 +0000 | [diff] [blame] | 888 | ** The instruction at the address in register P1 is a Yield. |
drh | 5dad9a3 | 2014-07-25 18:37:42 +0000 | [diff] [blame] | 889 | ** Jump to the P2 parameter of that Yield. |
drh | 81cf13e | 2014-02-07 18:27:53 +0000 | [diff] [blame] | 890 | ** After the jump, register P1 becomes undefined. |
drh | 5dad9a3 | 2014-07-25 18:37:42 +0000 | [diff] [blame] | 891 | ** |
| 892 | ** See also: InitCoroutine |
drh | 81cf13e | 2014-02-07 18:27:53 +0000 | [diff] [blame] | 893 | */ |
| 894 | case OP_EndCoroutine: { /* in1 */ |
| 895 | VdbeOp *pCaller; |
| 896 | pIn1 = &aMem[pOp->p1]; |
| 897 | assert( pIn1->flags==MEM_Int ); |
| 898 | assert( pIn1->u.i>=0 && pIn1->u.i<p->nOp ); |
| 899 | pCaller = &aOp[pIn1->u.i]; |
| 900 | assert( pCaller->opcode==OP_Yield ); |
| 901 | assert( pCaller->p2>=0 && pCaller->p2<p->nOp ); |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 902 | pOp = &aOp[pCaller->p2 - 1]; |
drh | 81cf13e | 2014-02-07 18:27:53 +0000 | [diff] [blame] | 903 | pIn1->flags = MEM_Undefined; |
| 904 | break; |
| 905 | } |
| 906 | |
| 907 | /* Opcode: Yield P1 P2 * * * |
drh | e00ee6e | 2008-06-20 15:24:01 +0000 | [diff] [blame] | 908 | ** |
drh | 5dad9a3 | 2014-07-25 18:37:42 +0000 | [diff] [blame] | 909 | ** Swap the program counter with the value in register P1. This |
| 910 | ** has the effect of yielding to a coroutine. |
drh | 81cf13e | 2014-02-07 18:27:53 +0000 | [diff] [blame] | 911 | ** |
drh | 5dad9a3 | 2014-07-25 18:37:42 +0000 | [diff] [blame] | 912 | ** If the coroutine that is launched by this instruction ends with |
| 913 | ** Yield or Return then continue to the next instruction. But if |
| 914 | ** the coroutine launched by this instruction ends with |
| 915 | ** EndCoroutine, then jump to P2 rather than continuing with the |
| 916 | ** next instruction. |
| 917 | ** |
| 918 | ** See also: InitCoroutine |
drh | e00ee6e | 2008-06-20 15:24:01 +0000 | [diff] [blame] | 919 | */ |
drh | 81cf13e | 2014-02-07 18:27:53 +0000 | [diff] [blame] | 920 | case OP_Yield: { /* in1, jump */ |
drh | e00ee6e | 2008-06-20 15:24:01 +0000 | [diff] [blame] | 921 | int pcDest; |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 922 | pIn1 = &aMem[pOp->p1]; |
drh | c91b2fd | 2014-03-01 18:13:23 +0000 | [diff] [blame] | 923 | assert( VdbeMemDynamic(pIn1)==0 ); |
drh | e00ee6e | 2008-06-20 15:24:01 +0000 | [diff] [blame] | 924 | pIn1->flags = MEM_Int; |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 925 | pcDest = (int)pIn1->u.i; |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 926 | pIn1->u.i = (int)(pOp - aOp); |
drh | e00ee6e | 2008-06-20 15:24:01 +0000 | [diff] [blame] | 927 | REGISTER_TRACE(pOp->p1, pIn1); |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 928 | pOp = &aOp[pcDest]; |
drh | e00ee6e | 2008-06-20 15:24:01 +0000 | [diff] [blame] | 929 | break; |
| 930 | } |
| 931 | |
drh | f9c8ce3 | 2013-11-05 13:33:55 +0000 | [diff] [blame] | 932 | /* Opcode: HaltIfNull P1 P2 P3 P4 P5 |
drh | 72e26de | 2016-08-24 21:24:04 +0000 | [diff] [blame] | 933 | ** Synopsis: if r[P3]=null halt |
drh | 5053a79 | 2009-02-20 03:02:23 +0000 | [diff] [blame] | 934 | ** |
drh | ef8662b | 2011-06-20 21:47:58 +0000 | [diff] [blame] | 935 | ** Check the value in register P3. If it is NULL then Halt using |
drh | 5053a79 | 2009-02-20 03:02:23 +0000 | [diff] [blame] | 936 | ** parameter P1, P2, and P4 as if this were a Halt instruction. If the |
| 937 | ** value in register P3 is not NULL, then this routine is a no-op. |
drh | f9c8ce3 | 2013-11-05 13:33:55 +0000 | [diff] [blame] | 938 | ** The P5 parameter should be 1. |
drh | 5053a79 | 2009-02-20 03:02:23 +0000 | [diff] [blame] | 939 | */ |
| 940 | case OP_HaltIfNull: { /* in3 */ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 941 | pIn3 = &aMem[pOp->p3]; |
drh | 4031baf | 2018-05-28 17:31:20 +0000 | [diff] [blame] | 942 | #ifdef SQLITE_DEBUG |
| 943 | if( pOp->p2==OE_Abort ){ sqlite3VdbeAssertAbortable(p); } |
| 944 | #endif |
drh | 5053a79 | 2009-02-20 03:02:23 +0000 | [diff] [blame] | 945 | if( (pIn3->flags & MEM_Null)==0 ) break; |
| 946 | /* Fall through into OP_Halt */ |
| 947 | } |
drh | e00ee6e | 2008-06-20 15:24:01 +0000 | [diff] [blame] | 948 | |
drh | f9c8ce3 | 2013-11-05 13:33:55 +0000 | [diff] [blame] | 949 | /* Opcode: Halt P1 P2 * P4 P5 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 950 | ** |
drh | 3d4501e | 2008-12-04 20:40:10 +0000 | [diff] [blame] | 951 | ** Exit immediately. All open cursors, etc are closed |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 952 | ** automatically. |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 953 | ** |
drh | 92f02c3 | 2004-09-02 14:57:08 +0000 | [diff] [blame] | 954 | ** P1 is the result code returned by sqlite3_exec(), sqlite3_reset(), |
| 955 | ** or sqlite3_finalize(). For a normal halt, this should be SQLITE_OK (0). |
| 956 | ** For errors, it can be some other value. If P1!=0 then P2 will determine |
| 957 | ** whether or not to rollback the current transaction. Do not rollback |
| 958 | ** if P2==OE_Fail. Do the rollback if P2==OE_Rollback. If P2==OE_Abort, |
| 959 | ** then back out all changes that have occurred during this execution of the |
drh | b798fa6 | 2002-09-03 19:43:23 +0000 | [diff] [blame] | 960 | ** VDBE, but do not rollback the transaction. |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 961 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 962 | ** If P4 is not null then it is an error message string. |
drh | 7f057c9 | 2005-06-24 03:53:06 +0000 | [diff] [blame] | 963 | ** |
drh | f9c8ce3 | 2013-11-05 13:33:55 +0000 | [diff] [blame] | 964 | ** P5 is a value between 0 and 4, inclusive, that modifies the P4 string. |
| 965 | ** |
| 966 | ** 0: (no change) |
| 967 | ** 1: NOT NULL contraint failed: P4 |
| 968 | ** 2: UNIQUE constraint failed: P4 |
| 969 | ** 3: CHECK constraint failed: P4 |
| 970 | ** 4: FOREIGN KEY constraint failed: P4 |
| 971 | ** |
| 972 | ** If P5 is not zero and P4 is NULL, then everything after the ":" is |
| 973 | ** omitted. |
| 974 | ** |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 975 | ** 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] | 976 | ** every program. So a jump past the last instruction of the program |
| 977 | ** is the same as executing Halt. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 978 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 979 | case OP_Halt: { |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 980 | VdbeFrame *pFrame; |
| 981 | int pcx; |
drh | f9c8ce3 | 2013-11-05 13:33:55 +0000 | [diff] [blame] | 982 | |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 983 | pcx = (int)(pOp - aOp); |
drh | 4031baf | 2018-05-28 17:31:20 +0000 | [diff] [blame] | 984 | #ifdef SQLITE_DEBUG |
| 985 | if( pOp->p2==OE_Abort ){ sqlite3VdbeAssertAbortable(p); } |
| 986 | #endif |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 987 | if( pOp->p1==SQLITE_OK && p->pFrame ){ |
dan | 2832ad4 | 2009-08-31 15:27:27 +0000 | [diff] [blame] | 988 | /* Halt the sub-program. Return control to the parent frame. */ |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 989 | pFrame = p->pFrame; |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 990 | p->pFrame = pFrame->pParent; |
| 991 | p->nFrame--; |
dan | 2832ad4 | 2009-08-31 15:27:27 +0000 | [diff] [blame] | 992 | sqlite3VdbeSetChanges(db, p->nChange); |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 993 | pcx = sqlite3VdbeFrameRestore(pFrame); |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 994 | if( pOp->p2==OE_Ignore ){ |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 995 | /* Instruction pcx is the OP_Program that invoked the sub-program |
dan | 2832ad4 | 2009-08-31 15:27:27 +0000 | [diff] [blame] | 996 | ** currently being halted. If the p2 instruction of this OP_Halt |
| 997 | ** instruction is set to OE_Ignore, then the sub-program is throwing |
| 998 | ** an IGNORE exception. In this case jump to the address specified |
| 999 | ** as the p2 of the calling OP_Program. */ |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 1000 | pcx = p->aOp[pcx].p2-1; |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 1001 | } |
drh | bbe879d | 2009-11-14 18:04:35 +0000 | [diff] [blame] | 1002 | aOp = p->aOp; |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 1003 | aMem = p->aMem; |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 1004 | pOp = &aOp[pcx]; |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 1005 | break; |
| 1006 | } |
drh | 92f02c3 | 2004-09-02 14:57:08 +0000 | [diff] [blame] | 1007 | p->rc = pOp->p1; |
shane | 36840fd | 2009-06-26 16:32:13 +0000 | [diff] [blame] | 1008 | p->errorAction = (u8)pOp->p2; |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 1009 | p->pc = pcx; |
drh | fb4e3a3 | 2016-12-30 00:09:14 +0000 | [diff] [blame] | 1010 | assert( pOp->p5<=4 ); |
drh | f9c8ce3 | 2013-11-05 13:33:55 +0000 | [diff] [blame] | 1011 | if( p->rc ){ |
drh | d9b7ec9 | 2013-11-06 14:05:21 +0000 | [diff] [blame] | 1012 | if( pOp->p5 ){ |
| 1013 | static const char * const azType[] = { "NOT NULL", "UNIQUE", "CHECK", |
| 1014 | "FOREIGN KEY" }; |
drh | d9b7ec9 | 2013-11-06 14:05:21 +0000 | [diff] [blame] | 1015 | testcase( pOp->p5==1 ); |
| 1016 | testcase( pOp->p5==2 ); |
| 1017 | testcase( pOp->p5==3 ); |
| 1018 | testcase( pOp->p5==4 ); |
drh | 99f5de7 | 2016-04-30 02:59:15 +0000 | [diff] [blame] | 1019 | sqlite3VdbeError(p, "%s constraint failed", azType[pOp->p5-1]); |
| 1020 | if( pOp->p4.z ){ |
| 1021 | p->zErrMsg = sqlite3MPrintf(db, "%z: %s", p->zErrMsg, pOp->p4.z); |
| 1022 | } |
drh | d9b7ec9 | 2013-11-06 14:05:21 +0000 | [diff] [blame] | 1023 | }else{ |
drh | 22c17b8 | 2015-05-15 04:13:15 +0000 | [diff] [blame] | 1024 | sqlite3VdbeError(p, "%s", pOp->p4.z); |
drh | f9c8ce3 | 2013-11-05 13:33:55 +0000 | [diff] [blame] | 1025 | } |
drh | 99f5de7 | 2016-04-30 02:59:15 +0000 | [diff] [blame] | 1026 | sqlite3_log(pOp->p1, "abort at %d in [%s]: %s", pcx, p->zSql, p->zErrMsg); |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 1027 | } |
drh | 92f02c3 | 2004-09-02 14:57:08 +0000 | [diff] [blame] | 1028 | rc = sqlite3VdbeHalt(p); |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 1029 | assert( rc==SQLITE_BUSY || rc==SQLITE_OK || rc==SQLITE_ERROR ); |
drh | 92f02c3 | 2004-09-02 14:57:08 +0000 | [diff] [blame] | 1030 | if( rc==SQLITE_BUSY ){ |
drh | 99f5de7 | 2016-04-30 02:59:15 +0000 | [diff] [blame] | 1031 | p->rc = SQLITE_BUSY; |
drh | 900b31e | 2007-08-28 02:27:51 +0000 | [diff] [blame] | 1032 | }else{ |
drh | d91c1a1 | 2013-02-09 13:58:25 +0000 | [diff] [blame] | 1033 | assert( rc==SQLITE_OK || (p->rc&0xff)==SQLITE_CONSTRAINT ); |
dan | cb3e4b7 | 2013-07-03 19:53:05 +0000 | [diff] [blame] | 1034 | assert( rc==SQLITE_OK || db->nDeferredCons>0 || db->nDeferredImmCons>0 ); |
drh | 900b31e | 2007-08-28 02:27:51 +0000 | [diff] [blame] | 1035 | rc = p->rc ? SQLITE_ERROR : SQLITE_DONE; |
drh | 92f02c3 | 2004-09-02 14:57:08 +0000 | [diff] [blame] | 1036 | } |
drh | 900b31e | 2007-08-28 02:27:51 +0000 | [diff] [blame] | 1037 | goto vdbe_return; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1038 | } |
drh | c61053b | 2000-06-04 12:58:36 +0000 | [diff] [blame] | 1039 | |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 1040 | /* Opcode: Integer P1 P2 * * * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 1041 | ** Synopsis: r[P2]=P1 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1042 | ** |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1043 | ** The 32-bit integer value P1 is written into register P2. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1044 | */ |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 1045 | case OP_Integer: { /* out2 */ |
| 1046 | pOut = out2Prerelease(p, pOp); |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 1047 | pOut->u.i = pOp->p1; |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 1048 | break; |
| 1049 | } |
| 1050 | |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 1051 | /* Opcode: Int64 * P2 * P4 * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 1052 | ** Synopsis: r[P2]=P4 |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 1053 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 1054 | ** P4 is a pointer to a 64-bit integer value. |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1055 | ** Write that value into register P2. |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 1056 | */ |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 1057 | case OP_Int64: { /* out2 */ |
| 1058 | pOut = out2Prerelease(p, pOp); |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 1059 | assert( pOp->p4.pI64!=0 ); |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 1060 | pOut->u.i = *pOp->p4.pI64; |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 1061 | break; |
| 1062 | } |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 1063 | |
drh | 13573c7 | 2010-01-12 17:04:07 +0000 | [diff] [blame] | 1064 | #ifndef SQLITE_OMIT_FLOATING_POINT |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 1065 | /* Opcode: Real * P2 * P4 * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 1066 | ** Synopsis: r[P2]=P4 |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 1067 | ** |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 1068 | ** P4 is a pointer to a 64-bit floating point value. |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1069 | ** Write that value into register P2. |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 1070 | */ |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 1071 | case OP_Real: { /* same as TK_FLOAT, out2 */ |
| 1072 | pOut = out2Prerelease(p, pOp); |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 1073 | pOut->flags = MEM_Real; |
drh | 2eaf93d | 2008-04-29 00:15:20 +0000 | [diff] [blame] | 1074 | assert( !sqlite3IsNaN(*pOp->p4.pReal) ); |
drh | 74eaba4 | 2014-09-18 17:52:15 +0000 | [diff] [blame] | 1075 | pOut->u.r = *pOp->p4.pReal; |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 1076 | break; |
| 1077 | } |
drh | 13573c7 | 2010-01-12 17:04:07 +0000 | [diff] [blame] | 1078 | #endif |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 1079 | |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 1080 | /* Opcode: String8 * P2 * P4 * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 1081 | ** Synopsis: r[P2]='P4' |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 1082 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 1083 | ** P4 points to a nul terminated UTF-8 string. This opcode is transformed |
drh | f07cf6e | 2015-03-06 16:45:16 +0000 | [diff] [blame] | 1084 | ** into a String opcode before it is executed for the first time. During |
drh | 0fd6135 | 2014-02-07 02:29:45 +0000 | [diff] [blame] | 1085 | ** this transformation, the length of string P4 is computed and stored |
| 1086 | ** as the P1 parameter. |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 1087 | */ |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 1088 | case OP_String8: { /* same as TK_STRING, out2 */ |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 1089 | assert( pOp->p4.z!=0 ); |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 1090 | pOut = out2Prerelease(p, pOp); |
drh | ed2df7f | 2005-11-16 04:34:32 +0000 | [diff] [blame] | 1091 | pOp->opcode = OP_String; |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 1092 | pOp->p1 = sqlite3Strlen30(pOp->p4.z); |
drh | ed2df7f | 2005-11-16 04:34:32 +0000 | [diff] [blame] | 1093 | |
| 1094 | #ifndef SQLITE_OMIT_UTF16 |
drh | 8079a0d | 2006-01-12 17:20:50 +0000 | [diff] [blame] | 1095 | if( encoding!=SQLITE_UTF8 ){ |
drh | 3a9cf17 | 2009-06-17 21:42:33 +0000 | [diff] [blame] | 1096 | rc = sqlite3VdbeMemSetStr(pOut, pOp->p4.z, -1, SQLITE_UTF8, SQLITE_STATIC); |
drh | 2f55511 | 2016-04-30 18:10:34 +0000 | [diff] [blame] | 1097 | assert( rc==SQLITE_OK || rc==SQLITE_TOOBIG ); |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 1098 | if( SQLITE_OK!=sqlite3VdbeChangeEncoding(pOut, encoding) ) goto no_mem; |
drh | 17bcb10 | 2014-09-18 21:25:33 +0000 | [diff] [blame] | 1099 | assert( pOut->szMalloc>0 && pOut->zMalloc==pOut->z ); |
drh | c91b2fd | 2014-03-01 18:13:23 +0000 | [diff] [blame] | 1100 | assert( VdbeMemDynamic(pOut)==0 ); |
drh | 17bcb10 | 2014-09-18 21:25:33 +0000 | [diff] [blame] | 1101 | pOut->szMalloc = 0; |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 1102 | pOut->flags |= MEM_Static; |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 1103 | if( pOp->p4type==P4_DYNAMIC ){ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 1104 | sqlite3DbFree(db, pOp->p4.z); |
danielk1977 | e004840 | 2004-06-15 16:51:01 +0000 | [diff] [blame] | 1105 | } |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 1106 | pOp->p4type = P4_DYNAMIC; |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 1107 | pOp->p4.z = pOut->z; |
| 1108 | pOp->p1 = pOut->n; |
danielk1977 | 0f69c1e | 2004-05-29 11:24:50 +0000 | [diff] [blame] | 1109 | } |
drh | 2f55511 | 2016-04-30 18:10:34 +0000 | [diff] [blame] | 1110 | testcase( rc==SQLITE_TOOBIG ); |
danielk1977 | 93758c8 | 2005-01-21 08:13:14 +0000 | [diff] [blame] | 1111 | #endif |
drh | bb4957f | 2008-03-20 14:03:29 +0000 | [diff] [blame] | 1112 | if( pOp->p1>db->aLimit[SQLITE_LIMIT_LENGTH] ){ |
drh | cbd2da9 | 2007-12-17 16:20:06 +0000 | [diff] [blame] | 1113 | goto too_big; |
| 1114 | } |
drh | 2f55511 | 2016-04-30 18:10:34 +0000 | [diff] [blame] | 1115 | assert( rc==SQLITE_OK ); |
drh | cbd2da9 | 2007-12-17 16:20:06 +0000 | [diff] [blame] | 1116 | /* Fall through to the next case, OP_String */ |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 1117 | } |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 1118 | |
drh | f07cf6e | 2015-03-06 16:45:16 +0000 | [diff] [blame] | 1119 | /* Opcode: String P1 P2 P3 P4 P5 |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 1120 | ** Synopsis: r[P2]='P4' (len=P1) |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 1121 | ** |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1122 | ** The string value P4 of length P1 (bytes) is stored in register P2. |
drh | f07cf6e | 2015-03-06 16:45:16 +0000 | [diff] [blame] | 1123 | ** |
drh | 44aebff | 2016-05-02 10:25:42 +0000 | [diff] [blame] | 1124 | ** If P3 is not zero and the content of register P3 is equal to P5, then |
drh | a9c18a9 | 2015-03-06 20:49:52 +0000 | [diff] [blame] | 1125 | ** the datatype of the register P2 is converted to BLOB. The content is |
| 1126 | ** the same sequence of bytes, it is merely interpreted as a BLOB instead |
drh | 44aebff | 2016-05-02 10:25:42 +0000 | [diff] [blame] | 1127 | ** of a string, as if it had been CAST. In other words: |
| 1128 | ** |
| 1129 | ** if( P3!=0 and reg[P3]==P5 ) reg[P2] := CAST(reg[P2] as BLOB) |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 1130 | */ |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 1131 | case OP_String: { /* out2 */ |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 1132 | assert( pOp->p4.z!=0 ); |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 1133 | pOut = out2Prerelease(p, pOp); |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 1134 | pOut->flags = MEM_Str|MEM_Static|MEM_Term; |
| 1135 | pOut->z = pOp->p4.z; |
| 1136 | pOut->n = pOp->p1; |
| 1137 | pOut->enc = encoding; |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 1138 | UPDATE_MAX_BLOBSIZE(pOut); |
drh | 41d2e66 | 2015-12-01 21:23:07 +0000 | [diff] [blame] | 1139 | #ifndef SQLITE_LIKE_DOESNT_MATCH_BLOBS |
drh | 44aebff | 2016-05-02 10:25:42 +0000 | [diff] [blame] | 1140 | if( pOp->p3>0 ){ |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 1141 | assert( pOp->p3<=(p->nMem+1 - p->nCursor) ); |
drh | f07cf6e | 2015-03-06 16:45:16 +0000 | [diff] [blame] | 1142 | pIn3 = &aMem[pOp->p3]; |
| 1143 | assert( pIn3->flags & MEM_Int ); |
drh | 44aebff | 2016-05-02 10:25:42 +0000 | [diff] [blame] | 1144 | if( pIn3->u.i==pOp->p5 ) pOut->flags = MEM_Blob|MEM_Static|MEM_Term; |
drh | f07cf6e | 2015-03-06 16:45:16 +0000 | [diff] [blame] | 1145 | } |
drh | 41d2e66 | 2015-12-01 21:23:07 +0000 | [diff] [blame] | 1146 | #endif |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 1147 | break; |
| 1148 | } |
| 1149 | |
drh | 053a128 | 2012-09-19 21:15:46 +0000 | [diff] [blame] | 1150 | /* Opcode: Null P1 P2 P3 * * |
drh | 72e26de | 2016-08-24 21:24:04 +0000 | [diff] [blame] | 1151 | ** Synopsis: r[P2..P3]=NULL |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 1152 | ** |
drh | b8475df | 2011-12-09 16:21:19 +0000 | [diff] [blame] | 1153 | ** Write a NULL into registers P2. If P3 greater than P2, then also write |
drh | 053a128 | 2012-09-19 21:15:46 +0000 | [diff] [blame] | 1154 | ** NULL into register P3 and every register in between P2 and P3. If P3 |
drh | b8475df | 2011-12-09 16:21:19 +0000 | [diff] [blame] | 1155 | ** is less than P2 (typically P3 is zero) then only register P2 is |
drh | 053a128 | 2012-09-19 21:15:46 +0000 | [diff] [blame] | 1156 | ** set to NULL. |
| 1157 | ** |
| 1158 | ** If the P1 value is non-zero, then also set the MEM_Cleared flag so that |
| 1159 | ** NULL values will not compare equal even if SQLITE_NULLEQ is set on |
| 1160 | ** OP_Ne or OP_Eq. |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 1161 | */ |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 1162 | case OP_Null: { /* out2 */ |
drh | b8475df | 2011-12-09 16:21:19 +0000 | [diff] [blame] | 1163 | int cnt; |
drh | 053a128 | 2012-09-19 21:15:46 +0000 | [diff] [blame] | 1164 | u16 nullFlag; |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 1165 | pOut = out2Prerelease(p, pOp); |
drh | b8475df | 2011-12-09 16:21:19 +0000 | [diff] [blame] | 1166 | cnt = pOp->p3-pOp->p2; |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 1167 | assert( pOp->p3<=(p->nMem+1 - p->nCursor) ); |
drh | 053a128 | 2012-09-19 21:15:46 +0000 | [diff] [blame] | 1168 | pOut->flags = nullFlag = pOp->p1 ? (MEM_Null|MEM_Cleared) : MEM_Null; |
drh | 2a1df93 | 2016-09-30 17:46:44 +0000 | [diff] [blame] | 1169 | pOut->n = 0; |
drh | 2c885d0 | 2018-07-07 19:36:04 +0000 | [diff] [blame] | 1170 | #ifdef SQLITE_DEBUG |
| 1171 | pOut->uTemp = 0; |
| 1172 | #endif |
drh | b8475df | 2011-12-09 16:21:19 +0000 | [diff] [blame] | 1173 | while( cnt>0 ){ |
| 1174 | pOut++; |
| 1175 | memAboutToChange(p, pOut); |
drh | 0725cab | 2014-09-17 14:52:46 +0000 | [diff] [blame] | 1176 | sqlite3VdbeMemSetNull(pOut); |
drh | 053a128 | 2012-09-19 21:15:46 +0000 | [diff] [blame] | 1177 | pOut->flags = nullFlag; |
drh | 2a1df93 | 2016-09-30 17:46:44 +0000 | [diff] [blame] | 1178 | pOut->n = 0; |
drh | b8475df | 2011-12-09 16:21:19 +0000 | [diff] [blame] | 1179 | cnt--; |
| 1180 | } |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 1181 | break; |
| 1182 | } |
| 1183 | |
drh | 05a86c5 | 2014-02-16 01:55:49 +0000 | [diff] [blame] | 1184 | /* Opcode: SoftNull P1 * * * * |
drh | 72e26de | 2016-08-24 21:24:04 +0000 | [diff] [blame] | 1185 | ** Synopsis: r[P1]=NULL |
drh | 05a86c5 | 2014-02-16 01:55:49 +0000 | [diff] [blame] | 1186 | ** |
| 1187 | ** Set register P1 to have the value NULL as seen by the OP_MakeRecord |
| 1188 | ** instruction, but do not free any string or blob memory associated with |
| 1189 | ** the register, so that if the value was a string or blob that was |
| 1190 | ** previously copied using OP_SCopy, the copies will continue to be valid. |
| 1191 | */ |
| 1192 | case OP_SoftNull: { |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 1193 | assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) ); |
drh | 05a86c5 | 2014-02-16 01:55:49 +0000 | [diff] [blame] | 1194 | pOut = &aMem[pOp->p1]; |
drh | e2bc655 | 2017-04-17 20:50:34 +0000 | [diff] [blame] | 1195 | pOut->flags = (pOut->flags&~(MEM_Undefined|MEM_AffMask))|MEM_Null; |
drh | 05a86c5 | 2014-02-16 01:55:49 +0000 | [diff] [blame] | 1196 | break; |
| 1197 | } |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 1198 | |
drh | a5750cf | 2014-02-07 13:20:31 +0000 | [diff] [blame] | 1199 | /* Opcode: Blob P1 P2 * P4 * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 1200 | ** Synopsis: r[P2]=P4 (len=P1) |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 1201 | ** |
drh | 9de221d | 2008-01-05 06:51:30 +0000 | [diff] [blame] | 1202 | ** P4 points to a blob of data P1 bytes long. Store this |
drh | 710c484 | 2010-08-30 01:17:20 +0000 | [diff] [blame] | 1203 | ** blob in register P2. |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 1204 | */ |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 1205 | case OP_Blob: { /* out2 */ |
drh | cbd2da9 | 2007-12-17 16:20:06 +0000 | [diff] [blame] | 1206 | assert( pOp->p1 <= SQLITE_MAX_LENGTH ); |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 1207 | pOut = out2Prerelease(p, pOp); |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 1208 | sqlite3VdbeMemSetStr(pOut, pOp->p4.z, pOp->p1, 0, 0); |
drh | 9de221d | 2008-01-05 06:51:30 +0000 | [diff] [blame] | 1209 | pOut->enc = encoding; |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 1210 | UPDATE_MAX_BLOBSIZE(pOut); |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 1211 | break; |
| 1212 | } |
| 1213 | |
drh | eaf52d8 | 2010-05-12 13:50:23 +0000 | [diff] [blame] | 1214 | /* Opcode: Variable P1 P2 * P4 * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 1215 | ** Synopsis: r[P2]=parameter(P1,P4) |
drh | 5045789 | 2003-09-06 01:10:47 +0000 | [diff] [blame] | 1216 | ** |
drh | eaf52d8 | 2010-05-12 13:50:23 +0000 | [diff] [blame] | 1217 | ** Transfer the values of bound parameter P1 into register P2 |
drh | 08de149 | 2009-02-20 03:55:05 +0000 | [diff] [blame] | 1218 | ** |
drh | 0fd6135 | 2014-02-07 02:29:45 +0000 | [diff] [blame] | 1219 | ** If the parameter is named, then its name appears in P4. |
drh | 08de149 | 2009-02-20 03:55:05 +0000 | [diff] [blame] | 1220 | ** The P4 value is used by sqlite3_bind_parameter_name(). |
drh | 5045789 | 2003-09-06 01:10:47 +0000 | [diff] [blame] | 1221 | */ |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 1222 | case OP_Variable: { /* out2 */ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1223 | Mem *pVar; /* Value being transferred */ |
| 1224 | |
drh | eaf52d8 | 2010-05-12 13:50:23 +0000 | [diff] [blame] | 1225 | assert( pOp->p1>0 && pOp->p1<=p->nVar ); |
drh | 9bf755c | 2016-12-23 03:59:31 +0000 | [diff] [blame] | 1226 | assert( pOp->p4.z==0 || pOp->p4.z==sqlite3VListNumToName(p->pVList,pOp->p1) ); |
drh | eaf52d8 | 2010-05-12 13:50:23 +0000 | [diff] [blame] | 1227 | pVar = &p->aVar[pOp->p1 - 1]; |
| 1228 | if( sqlite3VdbeMemTooBig(pVar) ){ |
| 1229 | goto too_big; |
drh | 023ae03 | 2007-05-08 12:12:16 +0000 | [diff] [blame] | 1230 | } |
drh | 7441df7 | 2017-01-09 19:27:04 +0000 | [diff] [blame] | 1231 | pOut = &aMem[pOp->p2]; |
drh | eaf52d8 | 2010-05-12 13:50:23 +0000 | [diff] [blame] | 1232 | sqlite3VdbeMemShallowCopy(pOut, pVar, MEM_Static); |
| 1233 | UPDATE_MAX_BLOBSIZE(pOut); |
danielk1977 | 93d4675 | 2004-05-23 13:30:58 +0000 | [diff] [blame] | 1234 | break; |
| 1235 | } |
danielk1977 | 295ba55 | 2004-05-19 10:34:51 +0000 | [diff] [blame] | 1236 | |
drh | b21e7c7 | 2008-06-22 12:37:57 +0000 | [diff] [blame] | 1237 | /* Opcode: Move P1 P2 P3 * * |
drh | 72e26de | 2016-08-24 21:24:04 +0000 | [diff] [blame] | 1238 | ** Synopsis: r[P2@P3]=r[P1@P3] |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1239 | ** |
drh | 079a307 | 2014-03-19 14:10:55 +0000 | [diff] [blame] | 1240 | ** Move the P3 values in register P1..P1+P3-1 over into |
| 1241 | ** registers P2..P2+P3-1. Registers P1..P1+P3-1 are |
drh | b21e7c7 | 2008-06-22 12:37:57 +0000 | [diff] [blame] | 1242 | ** left holding a NULL. It is an error for register ranges |
drh | 079a307 | 2014-03-19 14:10:55 +0000 | [diff] [blame] | 1243 | ** P1..P1+P3-1 and P2..P2+P3-1 to overlap. It is an error |
| 1244 | ** for P3 to be less than 1. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1245 | */ |
drh | e1349cb | 2008-04-01 00:36:10 +0000 | [diff] [blame] | 1246 | case OP_Move: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1247 | int n; /* Number of registers left to copy */ |
| 1248 | int p1; /* Register to copy from */ |
| 1249 | int p2; /* Register to copy to */ |
| 1250 | |
drh | e09f43f | 2013-11-21 04:18:31 +0000 | [diff] [blame] | 1251 | n = pOp->p3; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1252 | p1 = pOp->p1; |
| 1253 | p2 = pOp->p2; |
drh | 079a307 | 2014-03-19 14:10:55 +0000 | [diff] [blame] | 1254 | assert( n>0 && p1>0 && p2>0 ); |
drh | b21e7c7 | 2008-06-22 12:37:57 +0000 | [diff] [blame] | 1255 | assert( p1+n<=p2 || p2+n<=p1 ); |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1256 | |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 1257 | pIn1 = &aMem[p1]; |
| 1258 | pOut = &aMem[p2]; |
drh | e09f43f | 2013-11-21 04:18:31 +0000 | [diff] [blame] | 1259 | do{ |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 1260 | assert( pOut<=&aMem[(p->nMem+1 - p->nCursor)] ); |
| 1261 | assert( pIn1<=&aMem[(p->nMem+1 - p->nCursor)] ); |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 1262 | assert( memIsValid(pIn1) ); |
| 1263 | memAboutToChange(p, pOut); |
drh | 17bcb10 | 2014-09-18 21:25:33 +0000 | [diff] [blame] | 1264 | sqlite3VdbeMemMove(pOut, pIn1); |
drh | 52043d7 | 2011-08-03 16:40:15 +0000 | [diff] [blame] | 1265 | #ifdef SQLITE_DEBUG |
drh | bd6789e | 2015-04-28 14:00:02 +0000 | [diff] [blame] | 1266 | if( pOut->pScopyFrom>=&aMem[p1] && pOut->pScopyFrom<pOut ){ |
drh | 5fb7125 | 2015-04-28 12:44:55 +0000 | [diff] [blame] | 1267 | pOut->pScopyFrom += pOp->p2 - p1; |
drh | 52043d7 | 2011-08-03 16:40:15 +0000 | [diff] [blame] | 1268 | } |
| 1269 | #endif |
drh | bd6789e | 2015-04-28 14:00:02 +0000 | [diff] [blame] | 1270 | Deephemeralize(pOut); |
drh | b21e7c7 | 2008-06-22 12:37:57 +0000 | [diff] [blame] | 1271 | REGISTER_TRACE(p2++, pOut); |
| 1272 | pIn1++; |
| 1273 | pOut++; |
drh | 079a307 | 2014-03-19 14:10:55 +0000 | [diff] [blame] | 1274 | }while( --n ); |
drh | e1349cb | 2008-04-01 00:36:10 +0000 | [diff] [blame] | 1275 | break; |
| 1276 | } |
| 1277 | |
drh | e8e4af7 | 2012-09-21 00:04:28 +0000 | [diff] [blame] | 1278 | /* Opcode: Copy P1 P2 P3 * * |
drh | 4eded60 | 2013-12-20 15:59:20 +0000 | [diff] [blame] | 1279 | ** Synopsis: r[P2@P3+1]=r[P1@P3+1] |
drh | b1fdb2a | 2008-01-05 04:06:03 +0000 | [diff] [blame] | 1280 | ** |
drh | e8e4af7 | 2012-09-21 00:04:28 +0000 | [diff] [blame] | 1281 | ** Make a copy of registers P1..P1+P3 into registers P2..P2+P3. |
drh | b1fdb2a | 2008-01-05 04:06:03 +0000 | [diff] [blame] | 1282 | ** |
| 1283 | ** This instruction makes a deep copy of the value. A duplicate |
| 1284 | ** is made of any string or blob constant. See also OP_SCopy. |
| 1285 | */ |
drh | e8e4af7 | 2012-09-21 00:04:28 +0000 | [diff] [blame] | 1286 | case OP_Copy: { |
| 1287 | int n; |
| 1288 | |
| 1289 | n = pOp->p3; |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 1290 | pIn1 = &aMem[pOp->p1]; |
| 1291 | pOut = &aMem[pOp->p2]; |
drh | e1349cb | 2008-04-01 00:36:10 +0000 | [diff] [blame] | 1292 | assert( pOut!=pIn1 ); |
drh | e8e4af7 | 2012-09-21 00:04:28 +0000 | [diff] [blame] | 1293 | while( 1 ){ |
drh | 58773a5 | 2018-06-12 13:52:23 +0000 | [diff] [blame] | 1294 | memAboutToChange(p, pOut); |
drh | e8e4af7 | 2012-09-21 00:04:28 +0000 | [diff] [blame] | 1295 | sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem); |
| 1296 | Deephemeralize(pOut); |
drh | 953f761 | 2012-12-07 22:18:54 +0000 | [diff] [blame] | 1297 | #ifdef SQLITE_DEBUG |
| 1298 | pOut->pScopyFrom = 0; |
| 1299 | #endif |
drh | e8e4af7 | 2012-09-21 00:04:28 +0000 | [diff] [blame] | 1300 | REGISTER_TRACE(pOp->p2+pOp->p3-n, pOut); |
| 1301 | if( (n--)==0 ) break; |
| 1302 | pOut++; |
| 1303 | pIn1++; |
| 1304 | } |
drh | e1349cb | 2008-04-01 00:36:10 +0000 | [diff] [blame] | 1305 | break; |
| 1306 | } |
| 1307 | |
drh | b1fdb2a | 2008-01-05 04:06:03 +0000 | [diff] [blame] | 1308 | /* Opcode: SCopy P1 P2 * * * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 1309 | ** Synopsis: r[P2]=r[P1] |
drh | b1fdb2a | 2008-01-05 04:06:03 +0000 | [diff] [blame] | 1310 | ** |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1311 | ** Make a shallow copy of register P1 into register P2. |
drh | b1fdb2a | 2008-01-05 04:06:03 +0000 | [diff] [blame] | 1312 | ** |
| 1313 | ** This instruction makes a shallow copy of the value. If the value |
| 1314 | ** is a string or blob, then the copy is only a pointer to the |
| 1315 | ** original and hence if the original changes so will the copy. |
| 1316 | ** Worse, if the original is deallocated, the copy becomes invalid. |
| 1317 | ** Thus the program must guarantee that the original will not change |
| 1318 | ** during the lifetime of the copy. Use OP_Copy to make a complete |
| 1319 | ** copy. |
| 1320 | */ |
drh | 26198bb | 2013-10-31 11:15:09 +0000 | [diff] [blame] | 1321 | case OP_SCopy: { /* out2 */ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 1322 | pIn1 = &aMem[pOp->p1]; |
| 1323 | pOut = &aMem[pOp->p2]; |
drh | 2d401ab | 2008-01-10 23:50:11 +0000 | [diff] [blame] | 1324 | assert( pOut!=pIn1 ); |
drh | e1349cb | 2008-04-01 00:36:10 +0000 | [diff] [blame] | 1325 | sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem); |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 1326 | #ifdef SQLITE_DEBUG |
drh | 58773a5 | 2018-06-12 13:52:23 +0000 | [diff] [blame] | 1327 | pOut->pScopyFrom = pIn1; |
| 1328 | pOut->mScopyFlags = pIn1->flags; |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 1329 | #endif |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1330 | break; |
| 1331 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1332 | |
drh | fed7ac6 | 2015-10-15 18:04:59 +0000 | [diff] [blame] | 1333 | /* Opcode: IntCopy P1 P2 * * * |
| 1334 | ** Synopsis: r[P2]=r[P1] |
| 1335 | ** |
| 1336 | ** Transfer the integer value held in register P1 into register P2. |
| 1337 | ** |
| 1338 | ** This is an optimized version of SCopy that works only for integer |
| 1339 | ** values. |
| 1340 | */ |
| 1341 | case OP_IntCopy: { /* out2 */ |
| 1342 | pIn1 = &aMem[pOp->p1]; |
| 1343 | assert( (pIn1->flags & MEM_Int)!=0 ); |
| 1344 | pOut = &aMem[pOp->p2]; |
| 1345 | sqlite3VdbeMemSetInt64(pOut, pIn1->u.i); |
| 1346 | break; |
| 1347 | } |
| 1348 | |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1349 | /* Opcode: ResultRow P1 P2 * * * |
drh | 72e26de | 2016-08-24 21:24:04 +0000 | [diff] [blame] | 1350 | ** Synopsis: output=r[P1@P2] |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 1351 | ** |
shane | 21e7feb | 2008-05-30 15:59:49 +0000 | [diff] [blame] | 1352 | ** The registers P1 through P1+P2-1 contain a single row of |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 1353 | ** results. This opcode causes the sqlite3_step() call to terminate |
| 1354 | ** with an SQLITE_ROW return code and it sets up the sqlite3_stmt |
drh | 4d87aae | 2014-02-20 19:42:00 +0000 | [diff] [blame] | 1355 | ** structure to provide access to the r(P1)..r(P1+P2-1) values as |
drh | 0fd6135 | 2014-02-07 02:29:45 +0000 | [diff] [blame] | 1356 | ** the result row. |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 1357 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1358 | case OP_ResultRow: { |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 1359 | Mem *pMem; |
| 1360 | int i; |
| 1361 | assert( p->nResColumn==pOp->p2 ); |
drh | 0a07c10 | 2008-01-03 18:03:08 +0000 | [diff] [blame] | 1362 | assert( pOp->p1>0 ); |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 1363 | assert( pOp->p1+pOp->p2<=(p->nMem+1 - p->nCursor)+1 ); |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 1364 | |
drh | e6400b9 | 2013-11-13 23:48:46 +0000 | [diff] [blame] | 1365 | #ifndef SQLITE_OMIT_PROGRESS_CALLBACK |
| 1366 | /* Run the progress counter just before returning. |
| 1367 | */ |
| 1368 | if( db->xProgress!=0 |
drh | 2ab792e | 2017-05-30 18:34:07 +0000 | [diff] [blame] | 1369 | && nVmStep>=nProgressLimit |
drh | e6400b9 | 2013-11-13 23:48:46 +0000 | [diff] [blame] | 1370 | && db->xProgress(db->pProgressArg)!=0 |
| 1371 | ){ |
| 1372 | rc = SQLITE_INTERRUPT; |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 1373 | goto abort_due_to_error; |
drh | e6400b9 | 2013-11-13 23:48:46 +0000 | [diff] [blame] | 1374 | } |
| 1375 | #endif |
| 1376 | |
dan | 32b09f2 | 2009-09-23 17:29:59 +0000 | [diff] [blame] | 1377 | /* If this statement has violated immediate foreign key constraints, do |
| 1378 | ** not return the number of rows modified. And do not RELEASE the statement |
| 1379 | ** transaction. It needs to be rolled back. */ |
| 1380 | if( SQLITE_OK!=(rc = sqlite3VdbeCheckFk(p, 0)) ){ |
| 1381 | assert( db->flags&SQLITE_CountRows ); |
| 1382 | assert( p->usesStmtJournal ); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 1383 | goto abort_due_to_error; |
dan | 32b09f2 | 2009-09-23 17:29:59 +0000 | [diff] [blame] | 1384 | } |
| 1385 | |
danielk1977 | bd43455 | 2009-03-18 10:33:00 +0000 | [diff] [blame] | 1386 | /* If the SQLITE_CountRows flag is set in sqlite3.flags mask, then |
| 1387 | ** DML statements invoke this opcode to return the number of rows |
| 1388 | ** modified to the user. This is the only way that a VM that |
| 1389 | ** opens a statement transaction may invoke this opcode. |
| 1390 | ** |
| 1391 | ** In case this is such a statement, close any statement transaction |
| 1392 | ** opened by this VM before returning control to the user. This is to |
| 1393 | ** ensure that statement-transactions are always nested, not overlapping. |
| 1394 | ** If the open statement-transaction is not closed here, then the user |
| 1395 | ** may step another VM that opens its own statement transaction. This |
| 1396 | ** may lead to overlapping statement transactions. |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 1397 | ** |
| 1398 | ** The statement transaction is never a top-level transaction. Hence |
| 1399 | ** the RELEASE call below can never fail. |
danielk1977 | bd43455 | 2009-03-18 10:33:00 +0000 | [diff] [blame] | 1400 | */ |
| 1401 | assert( p->iStatement==0 || db->flags&SQLITE_CountRows ); |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 1402 | rc = sqlite3VdbeCloseStatement(p, SAVEPOINT_RELEASE); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 1403 | assert( rc==SQLITE_OK ); |
danielk1977 | bd43455 | 2009-03-18 10:33:00 +0000 | [diff] [blame] | 1404 | |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 1405 | /* Invalidate all ephemeral cursor row caches */ |
| 1406 | p->cacheCtr = (p->cacheCtr + 2)|1; |
| 1407 | |
| 1408 | /* Make sure the results of the current row are \000 terminated |
shane | 21e7feb | 2008-05-30 15:59:49 +0000 | [diff] [blame] | 1409 | ** and have an assigned type. The results are de-ephemeralized as |
drh | b8a45bb | 2011-12-31 21:51:55 +0000 | [diff] [blame] | 1410 | ** a side effect. |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 1411 | */ |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 1412 | pMem = p->pResultSet = &aMem[pOp->p1]; |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 1413 | for(i=0; i<pOp->p2; i++){ |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 1414 | assert( memIsValid(&pMem[i]) ); |
drh | ebc1671 | 2010-09-28 00:25:58 +0000 | [diff] [blame] | 1415 | Deephemeralize(&pMem[i]); |
drh | 746fd9c | 2010-09-28 06:00:47 +0000 | [diff] [blame] | 1416 | assert( (pMem[i].flags & MEM_Ephem)==0 |
| 1417 | || (pMem[i].flags & (MEM_Str|MEM_Blob))==0 ); |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 1418 | sqlite3VdbeMemNulTerminate(&pMem[i]); |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 1419 | REGISTER_TRACE(pOp->p1+i, &pMem[i]); |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 1420 | } |
drh | 2803969 | 2008-03-17 16:54:01 +0000 | [diff] [blame] | 1421 | if( db->mallocFailed ) goto no_mem; |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 1422 | |
drh | 3d2a529 | 2016-07-13 22:55:01 +0000 | [diff] [blame] | 1423 | if( db->mTrace & SQLITE_TRACE_ROW ){ |
| 1424 | db->xTrace(SQLITE_TRACE_ROW, db->pTraceArg, p, 0); |
| 1425 | } |
| 1426 | |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 1427 | /* Return SQLITE_ROW |
| 1428 | */ |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 1429 | p->pc = (int)(pOp - aOp) + 1; |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 1430 | rc = SQLITE_ROW; |
| 1431 | goto vdbe_return; |
| 1432 | } |
| 1433 | |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1434 | /* Opcode: Concat P1 P2 P3 * * |
drh | 313619f | 2013-10-31 20:34:06 +0000 | [diff] [blame] | 1435 | ** Synopsis: r[P3]=r[P2]+r[P1] |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1436 | ** |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1437 | ** Add the text in register P1 onto the end of the text in |
| 1438 | ** register P2 and store the result in register P3. |
| 1439 | ** 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] | 1440 | ** |
| 1441 | ** P3 = P2 || P1 |
| 1442 | ** |
| 1443 | ** It is illegal for P1 and P3 to be the same register. Sometimes, |
| 1444 | ** if P3 is the same register as P2, the implementation is able |
| 1445 | ** to avoid a memcpy(). |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1446 | */ |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1447 | case OP_Concat: { /* same as TK_CONCAT, in1, in2, out3 */ |
drh | 023ae03 | 2007-05-08 12:12:16 +0000 | [diff] [blame] | 1448 | i64 nByte; |
danielk1977 | 8a6b541 | 2004-05-24 07:04:25 +0000 | [diff] [blame] | 1449 | |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 1450 | pIn1 = &aMem[pOp->p1]; |
| 1451 | pIn2 = &aMem[pOp->p2]; |
| 1452 | pOut = &aMem[pOp->p3]; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1453 | assert( pIn1!=pOut ); |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1454 | if( (pIn1->flags | pIn2->flags) & MEM_Null ){ |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1455 | sqlite3VdbeMemSetNull(pOut); |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1456 | break; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1457 | } |
drh | a0c0652 | 2009-06-17 22:50:41 +0000 | [diff] [blame] | 1458 | if( ExpandBlob(pIn1) || ExpandBlob(pIn2) ) goto no_mem; |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1459 | Stringify(pIn1, encoding); |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1460 | Stringify(pIn2, encoding); |
| 1461 | nByte = pIn1->n + pIn2->n; |
drh | bb4957f | 2008-03-20 14:03:29 +0000 | [diff] [blame] | 1462 | if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){ |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1463 | goto too_big; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1464 | } |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 1465 | if( sqlite3VdbeMemGrow(pOut, (int)nByte+2, pOut==pIn2) ){ |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1466 | goto no_mem; |
| 1467 | } |
drh | c91b2fd | 2014-03-01 18:13:23 +0000 | [diff] [blame] | 1468 | MemSetTypeFlag(pOut, MEM_Str); |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1469 | if( pOut!=pIn2 ){ |
| 1470 | memcpy(pOut->z, pIn2->z, pIn2->n); |
| 1471 | } |
| 1472 | memcpy(&pOut->z[pIn2->n], pIn1->z, pIn1->n); |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 1473 | pOut->z[nByte]=0; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1474 | pOut->z[nByte+1] = 0; |
| 1475 | pOut->flags |= MEM_Term; |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 1476 | pOut->n = (int)nByte; |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1477 | pOut->enc = encoding; |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 1478 | UPDATE_MAX_BLOBSIZE(pOut); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1479 | break; |
| 1480 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1481 | |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 1482 | /* Opcode: Add P1 P2 P3 * * |
drh | 72e26de | 2016-08-24 21:24:04 +0000 | [diff] [blame] | 1483 | ** Synopsis: r[P3]=r[P1]+r[P2] |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1484 | ** |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1485 | ** Add the value in register P1 to the value in register P2 |
shane | 21e7feb | 2008-05-30 15:59:49 +0000 | [diff] [blame] | 1486 | ** and store the result in register P3. |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1487 | ** If either input is NULL, the result is NULL. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1488 | */ |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 1489 | /* Opcode: Multiply P1 P2 P3 * * |
drh | 72e26de | 2016-08-24 21:24:04 +0000 | [diff] [blame] | 1490 | ** Synopsis: r[P3]=r[P1]*r[P2] |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1491 | ** |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 1492 | ** |
shane | 21e7feb | 2008-05-30 15:59:49 +0000 | [diff] [blame] | 1493 | ** Multiply the value in register P1 by the value in register P2 |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1494 | ** and store the result in register P3. |
| 1495 | ** If either input is NULL, the result is NULL. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1496 | */ |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 1497 | /* Opcode: Subtract P1 P2 P3 * * |
drh | 72e26de | 2016-08-24 21:24:04 +0000 | [diff] [blame] | 1498 | ** Synopsis: r[P3]=r[P2]-r[P1] |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1499 | ** |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1500 | ** Subtract the value in register P1 from the value in register P2 |
| 1501 | ** and store the result in register P3. |
| 1502 | ** If either input is NULL, the result is NULL. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1503 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1504 | /* Opcode: Divide P1 P2 P3 * * |
drh | 72e26de | 2016-08-24 21:24:04 +0000 | [diff] [blame] | 1505 | ** Synopsis: r[P3]=r[P2]/r[P1] |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1506 | ** |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1507 | ** Divide the value in register P1 by the value in register P2 |
dan | e275dc3 | 2009-08-18 16:24:58 +0000 | [diff] [blame] | 1508 | ** and store the result in register P3 (P3=P2/P1). If the value in |
| 1509 | ** register P1 is zero, then the result is NULL. If either input is |
| 1510 | ** NULL, the result is NULL. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1511 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1512 | /* Opcode: Remainder P1 P2 P3 * * |
drh | 72e26de | 2016-08-24 21:24:04 +0000 | [diff] [blame] | 1513 | ** Synopsis: r[P3]=r[P2]%r[P1] |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1514 | ** |
drh | 40864a1 | 2013-11-15 18:58:37 +0000 | [diff] [blame] | 1515 | ** Compute the remainder after integer register P2 is divided by |
| 1516 | ** register P1 and store the result in register P3. |
| 1517 | ** If the value in register P1 is zero the result is NULL. |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1518 | ** If either operand is NULL, the result is NULL. |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1519 | */ |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1520 | case OP_Add: /* same as TK_PLUS, in1, in2, out3 */ |
| 1521 | case OP_Subtract: /* same as TK_MINUS, in1, in2, out3 */ |
| 1522 | case OP_Multiply: /* same as TK_STAR, in1, in2, out3 */ |
| 1523 | case OP_Divide: /* same as TK_SLASH, in1, in2, out3 */ |
| 1524 | case OP_Remainder: { /* same as TK_REM, in1, in2, out3 */ |
drh | be707b3 | 2012-12-10 22:19:14 +0000 | [diff] [blame] | 1525 | char bIntint; /* Started out as two integer operands */ |
drh | 3d1d90a | 2014-03-24 15:00:15 +0000 | [diff] [blame] | 1526 | u16 flags; /* Combined MEM_* flags from both inputs */ |
| 1527 | u16 type1; /* Numeric type of left operand */ |
| 1528 | u16 type2; /* Numeric type of right operand */ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1529 | i64 iA; /* Integer value of left operand */ |
| 1530 | i64 iB; /* Integer value of right operand */ |
| 1531 | double rA; /* Real value of left operand */ |
| 1532 | double rB; /* Real value of right operand */ |
| 1533 | |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 1534 | pIn1 = &aMem[pOp->p1]; |
drh | 3d1d90a | 2014-03-24 15:00:15 +0000 | [diff] [blame] | 1535 | type1 = numericType(pIn1); |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 1536 | pIn2 = &aMem[pOp->p2]; |
drh | 3d1d90a | 2014-03-24 15:00:15 +0000 | [diff] [blame] | 1537 | type2 = numericType(pIn2); |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 1538 | pOut = &aMem[pOp->p3]; |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1539 | flags = pIn1->flags | pIn2->flags; |
drh | 3d1d90a | 2014-03-24 15:00:15 +0000 | [diff] [blame] | 1540 | if( (type1 & type2 & MEM_Int)!=0 ){ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1541 | iA = pIn1->u.i; |
| 1542 | iB = pIn2->u.i; |
drh | be707b3 | 2012-12-10 22:19:14 +0000 | [diff] [blame] | 1543 | bIntint = 1; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1544 | switch( pOp->opcode ){ |
drh | 158b9cb | 2011-03-05 20:59:46 +0000 | [diff] [blame] | 1545 | case OP_Add: if( sqlite3AddInt64(&iB,iA) ) goto fp_math; break; |
| 1546 | case OP_Subtract: if( sqlite3SubInt64(&iB,iA) ) goto fp_math; break; |
| 1547 | case OP_Multiply: if( sqlite3MulInt64(&iB,iA) ) goto fp_math; break; |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1548 | case OP_Divide: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1549 | if( iA==0 ) goto arithmetic_result_is_null; |
drh | 158b9cb | 2011-03-05 20:59:46 +0000 | [diff] [blame] | 1550 | if( iA==-1 && iB==SMALLEST_INT64 ) goto fp_math; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1551 | iB /= iA; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1552 | break; |
| 1553 | } |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1554 | default: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1555 | if( iA==0 ) goto arithmetic_result_is_null; |
| 1556 | if( iA==-1 ) iA = 1; |
| 1557 | iB %= iA; |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1558 | break; |
| 1559 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1560 | } |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1561 | pOut->u.i = iB; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1562 | MemSetTypeFlag(pOut, MEM_Int); |
drh | cfcca02 | 2017-04-17 23:23:17 +0000 | [diff] [blame] | 1563 | }else if( (flags & MEM_Null)!=0 ){ |
| 1564 | goto arithmetic_result_is_null; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1565 | }else{ |
drh | be707b3 | 2012-12-10 22:19:14 +0000 | [diff] [blame] | 1566 | bIntint = 0; |
drh | 158b9cb | 2011-03-05 20:59:46 +0000 | [diff] [blame] | 1567 | fp_math: |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1568 | rA = sqlite3VdbeRealValue(pIn1); |
| 1569 | rB = sqlite3VdbeRealValue(pIn2); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1570 | switch( pOp->opcode ){ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1571 | case OP_Add: rB += rA; break; |
| 1572 | case OP_Subtract: rB -= rA; break; |
| 1573 | case OP_Multiply: rB *= rA; break; |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1574 | case OP_Divide: { |
shane | fbd60f8 | 2009-02-04 03:59:25 +0000 | [diff] [blame] | 1575 | /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1576 | if( rA==(double)0 ) goto arithmetic_result_is_null; |
| 1577 | rB /= rA; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1578 | break; |
| 1579 | } |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1580 | default: { |
shane | 75ac1de | 2009-06-09 18:58:52 +0000 | [diff] [blame] | 1581 | iA = (i64)rA; |
| 1582 | iB = (i64)rB; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1583 | if( iA==0 ) goto arithmetic_result_is_null; |
| 1584 | if( iA==-1 ) iA = 1; |
| 1585 | rB = (double)(iB % iA); |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1586 | break; |
| 1587 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1588 | } |
drh | c5a7b51 | 2010-01-13 16:25:42 +0000 | [diff] [blame] | 1589 | #ifdef SQLITE_OMIT_FLOATING_POINT |
| 1590 | pOut->u.i = rB; |
| 1591 | MemSetTypeFlag(pOut, MEM_Int); |
| 1592 | #else |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1593 | if( sqlite3IsNaN(rB) ){ |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 1594 | goto arithmetic_result_is_null; |
drh | 53c1402 | 2007-05-10 17:23:11 +0000 | [diff] [blame] | 1595 | } |
drh | 74eaba4 | 2014-09-18 17:52:15 +0000 | [diff] [blame] | 1596 | pOut->u.r = rB; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1597 | MemSetTypeFlag(pOut, MEM_Real); |
drh | 3d1d90a | 2014-03-24 15:00:15 +0000 | [diff] [blame] | 1598 | if( ((type1|type2)&MEM_Real)==0 && !bIntint ){ |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1599 | sqlite3VdbeIntegerAffinity(pOut); |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1600 | } |
drh | c5a7b51 | 2010-01-13 16:25:42 +0000 | [diff] [blame] | 1601 | #endif |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1602 | } |
| 1603 | break; |
| 1604 | |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 1605 | arithmetic_result_is_null: |
| 1606 | sqlite3VdbeMemSetNull(pOut); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1607 | break; |
| 1608 | } |
| 1609 | |
drh | 7a95789 | 2012-02-02 17:35:43 +0000 | [diff] [blame] | 1610 | /* Opcode: CollSeq P1 * * P4 |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 1611 | ** |
drh | bb6783b | 2017-04-29 18:02:49 +0000 | [diff] [blame] | 1612 | ** P4 is a pointer to a CollSeq object. If the next call to a user function |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 1613 | ** or aggregate calls sqlite3GetFuncCollSeq(), this collation sequence will |
| 1614 | ** be returned. This is used by the built-in min(), max() and nullif() |
drh | e6f85e7 | 2004-12-25 01:03:13 +0000 | [diff] [blame] | 1615 | ** functions. |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 1616 | ** |
drh | 7a95789 | 2012-02-02 17:35:43 +0000 | [diff] [blame] | 1617 | ** If P1 is not zero, then it is a register that a subsequent min() or |
| 1618 | ** max() aggregate will set to 1 if the current row is not the minimum or |
| 1619 | ** maximum. The P1 register is initialized to 0 by this instruction. |
| 1620 | ** |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 1621 | ** The interface used by the implementation of the aforementioned functions |
| 1622 | ** to retrieve the collation sequence set by this opcode is not available |
drh | 0a0d056 | 2015-03-12 05:08:34 +0000 | [diff] [blame] | 1623 | ** publicly. Only built-in functions have access to this feature. |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 1624 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1625 | case OP_CollSeq: { |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 1626 | assert( pOp->p4type==P4_COLLSEQ ); |
drh | 7a95789 | 2012-02-02 17:35:43 +0000 | [diff] [blame] | 1627 | if( pOp->p1 ){ |
| 1628 | sqlite3VdbeMemSetInt64(&aMem[pOp->p1], 0); |
| 1629 | } |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 1630 | break; |
| 1631 | } |
| 1632 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1633 | /* Opcode: BitAnd P1 P2 P3 * * |
drh | 72e26de | 2016-08-24 21:24:04 +0000 | [diff] [blame] | 1634 | ** Synopsis: r[P3]=r[P1]&r[P2] |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1635 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1636 | ** Take the bit-wise AND of the values in register P1 and P2 and |
| 1637 | ** store the result in register P3. |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1638 | ** If either input is NULL, the result is NULL. |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1639 | */ |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1640 | /* Opcode: BitOr P1 P2 P3 * * |
drh | 72e26de | 2016-08-24 21:24:04 +0000 | [diff] [blame] | 1641 | ** Synopsis: r[P3]=r[P1]|r[P2] |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1642 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1643 | ** Take the bit-wise OR of the values in register P1 and P2 and |
| 1644 | ** store the result in register P3. |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1645 | ** If either input is NULL, the result is NULL. |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1646 | */ |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1647 | /* Opcode: ShiftLeft P1 P2 P3 * * |
drh | 72e26de | 2016-08-24 21:24:04 +0000 | [diff] [blame] | 1648 | ** Synopsis: r[P3]=r[P2]<<r[P1] |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1649 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1650 | ** Shift the integer value in register P2 to the left by the |
drh | 710c484 | 2010-08-30 01:17:20 +0000 | [diff] [blame] | 1651 | ** number of bits specified by the integer in register P1. |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1652 | ** Store the result in register P3. |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1653 | ** If either input is NULL, the result is NULL. |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1654 | */ |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1655 | /* Opcode: ShiftRight P1 P2 P3 * * |
drh | 72e26de | 2016-08-24 21:24:04 +0000 | [diff] [blame] | 1656 | ** Synopsis: r[P3]=r[P2]>>r[P1] |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1657 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1658 | ** Shift the integer value in register P2 to the right by the |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1659 | ** number of bits specified by the integer in register P1. |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1660 | ** Store the result in register P3. |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1661 | ** If either input is NULL, the result is NULL. |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1662 | */ |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1663 | case OP_BitAnd: /* same as TK_BITAND, in1, in2, out3 */ |
| 1664 | case OP_BitOr: /* same as TK_BITOR, in1, in2, out3 */ |
| 1665 | case OP_ShiftLeft: /* same as TK_LSHIFT, in1, in2, out3 */ |
| 1666 | case OP_ShiftRight: { /* same as TK_RSHIFT, in1, in2, out3 */ |
drh | 158b9cb | 2011-03-05 20:59:46 +0000 | [diff] [blame] | 1667 | i64 iA; |
| 1668 | u64 uA; |
| 1669 | i64 iB; |
| 1670 | u8 op; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1671 | |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 1672 | pIn1 = &aMem[pOp->p1]; |
| 1673 | pIn2 = &aMem[pOp->p2]; |
| 1674 | pOut = &aMem[pOp->p3]; |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1675 | if( (pIn1->flags | pIn2->flags) & MEM_Null ){ |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 1676 | sqlite3VdbeMemSetNull(pOut); |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1677 | break; |
| 1678 | } |
drh | 158b9cb | 2011-03-05 20:59:46 +0000 | [diff] [blame] | 1679 | iA = sqlite3VdbeIntValue(pIn2); |
| 1680 | iB = sqlite3VdbeIntValue(pIn1); |
| 1681 | op = pOp->opcode; |
| 1682 | if( op==OP_BitAnd ){ |
| 1683 | iA &= iB; |
| 1684 | }else if( op==OP_BitOr ){ |
| 1685 | iA |= iB; |
| 1686 | }else if( iB!=0 ){ |
| 1687 | assert( op==OP_ShiftRight || op==OP_ShiftLeft ); |
| 1688 | |
| 1689 | /* If shifting by a negative amount, shift in the other direction */ |
| 1690 | if( iB<0 ){ |
| 1691 | assert( OP_ShiftRight==OP_ShiftLeft+1 ); |
| 1692 | op = 2*OP_ShiftLeft + 1 - op; |
| 1693 | iB = iB>(-64) ? -iB : 64; |
| 1694 | } |
| 1695 | |
| 1696 | if( iB>=64 ){ |
| 1697 | iA = (iA>=0 || op==OP_ShiftLeft) ? 0 : -1; |
| 1698 | }else{ |
| 1699 | memcpy(&uA, &iA, sizeof(uA)); |
| 1700 | if( op==OP_ShiftLeft ){ |
| 1701 | uA <<= iB; |
| 1702 | }else{ |
| 1703 | uA >>= iB; |
| 1704 | /* Sign-extend on a right shift of a negative number */ |
| 1705 | if( iA<0 ) uA |= ((((u64)0xffffffff)<<32)|0xffffffff) << (64-iB); |
| 1706 | } |
| 1707 | memcpy(&iA, &uA, sizeof(iA)); |
| 1708 | } |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1709 | } |
drh | 158b9cb | 2011-03-05 20:59:46 +0000 | [diff] [blame] | 1710 | pOut->u.i = iA; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1711 | MemSetTypeFlag(pOut, MEM_Int); |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1712 | break; |
| 1713 | } |
| 1714 | |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1715 | /* Opcode: AddImm P1 P2 * * * |
drh | 72e26de | 2016-08-24 21:24:04 +0000 | [diff] [blame] | 1716 | ** Synopsis: r[P1]=r[P1]+P2 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1717 | ** |
danielk1977 | 0cdc022 | 2008-06-26 18:04:03 +0000 | [diff] [blame] | 1718 | ** Add the constant P2 to the value in register P1. |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1719 | ** The result is always an integer. |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 1720 | ** |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1721 | ** To force any register to be an integer, just add 0. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1722 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1723 | case OP_AddImm: { /* in1 */ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 1724 | pIn1 = &aMem[pOp->p1]; |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 1725 | memAboutToChange(p, pIn1); |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1726 | sqlite3VdbeMemIntegerify(pIn1); |
| 1727 | pIn1->u.i += pOp->p2; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1728 | break; |
| 1729 | } |
| 1730 | |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1731 | /* Opcode: MustBeInt P1 P2 * * * |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 1732 | ** |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1733 | ** Force the value in register P1 to be an integer. If the value |
| 1734 | ** in P1 is not an integer and cannot be converted into an integer |
danielk1977 | 9a96b66 | 2007-11-29 17:05:18 +0000 | [diff] [blame] | 1735 | ** without data loss, then jump immediately to P2, or if P2==0 |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 1736 | ** raise an SQLITE_MISMATCH exception. |
| 1737 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1738 | case OP_MustBeInt: { /* jump, in1 */ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 1739 | pIn1 = &aMem[pOp->p1]; |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 1740 | if( (pIn1->flags & MEM_Int)==0 ){ |
drh | 83b301b | 2013-11-20 00:59:02 +0000 | [diff] [blame] | 1741 | applyAffinity(pIn1, SQLITE_AFF_NUMERIC, encoding); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 1742 | VdbeBranchTaken((pIn1->flags&MEM_Int)==0, 2); |
drh | 83b301b | 2013-11-20 00:59:02 +0000 | [diff] [blame] | 1743 | if( (pIn1->flags & MEM_Int)==0 ){ |
| 1744 | if( pOp->p2==0 ){ |
| 1745 | rc = SQLITE_MISMATCH; |
| 1746 | goto abort_due_to_error; |
| 1747 | }else{ |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 1748 | goto jump_to_p2; |
drh | 83b301b | 2013-11-20 00:59:02 +0000 | [diff] [blame] | 1749 | } |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 1750 | } |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 1751 | } |
drh | 83b301b | 2013-11-20 00:59:02 +0000 | [diff] [blame] | 1752 | MemSetTypeFlag(pIn1, MEM_Int); |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 1753 | break; |
| 1754 | } |
| 1755 | |
drh | 13573c7 | 2010-01-12 17:04:07 +0000 | [diff] [blame] | 1756 | #ifndef SQLITE_OMIT_FLOATING_POINT |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1757 | /* Opcode: RealAffinity P1 * * * * |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 1758 | ** |
drh | 2133d82 | 2008-01-03 18:44:59 +0000 | [diff] [blame] | 1759 | ** If register P1 holds an integer convert it to a real value. |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 1760 | ** |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1761 | ** This opcode is used when extracting information from a column that |
| 1762 | ** has REAL affinity. Such column values may still be stored as |
| 1763 | ** integers, for space efficiency, but after extraction we want them |
| 1764 | ** to have only a real value. |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 1765 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1766 | case OP_RealAffinity: { /* in1 */ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 1767 | pIn1 = &aMem[pOp->p1]; |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1768 | if( pIn1->flags & MEM_Int ){ |
| 1769 | sqlite3VdbeMemRealify(pIn1); |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1770 | } |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 1771 | break; |
| 1772 | } |
drh | 13573c7 | 2010-01-12 17:04:07 +0000 | [diff] [blame] | 1773 | #endif |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 1774 | |
drh | 8df447f | 2005-11-01 15:48:24 +0000 | [diff] [blame] | 1775 | #ifndef SQLITE_OMIT_CAST |
drh | 4169e43 | 2014-08-25 20:11:52 +0000 | [diff] [blame] | 1776 | /* Opcode: Cast P1 P2 * * * |
mistachkin | a1dc42a | 2014-08-27 17:53:40 +0000 | [diff] [blame] | 1777 | ** Synopsis: affinity(r[P1]) |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 1778 | ** |
drh | 4169e43 | 2014-08-25 20:11:52 +0000 | [diff] [blame] | 1779 | ** Force the value in register P1 to be the type defined by P2. |
| 1780 | ** |
| 1781 | ** <ul> |
drh | bb6783b | 2017-04-29 18:02:49 +0000 | [diff] [blame] | 1782 | ** <li> P2=='A' → BLOB |
| 1783 | ** <li> P2=='B' → TEXT |
| 1784 | ** <li> P2=='C' → NUMERIC |
| 1785 | ** <li> P2=='D' → INTEGER |
| 1786 | ** <li> P2=='E' → REAL |
drh | 4169e43 | 2014-08-25 20:11:52 +0000 | [diff] [blame] | 1787 | ** </ul> |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 1788 | ** |
| 1789 | ** A NULL value is not changed by this routine. It remains NULL. |
| 1790 | */ |
drh | 4169e43 | 2014-08-25 20:11:52 +0000 | [diff] [blame] | 1791 | case OP_Cast: { /* in1 */ |
drh | 05883a3 | 2015-06-02 15:32:08 +0000 | [diff] [blame] | 1792 | assert( pOp->p2>=SQLITE_AFF_BLOB && pOp->p2<=SQLITE_AFF_REAL ); |
drh | 05bbb2e | 2014-08-25 22:37:19 +0000 | [diff] [blame] | 1793 | testcase( pOp->p2==SQLITE_AFF_TEXT ); |
drh | 05883a3 | 2015-06-02 15:32:08 +0000 | [diff] [blame] | 1794 | testcase( pOp->p2==SQLITE_AFF_BLOB ); |
drh | 05bbb2e | 2014-08-25 22:37:19 +0000 | [diff] [blame] | 1795 | testcase( pOp->p2==SQLITE_AFF_NUMERIC ); |
| 1796 | testcase( pOp->p2==SQLITE_AFF_INTEGER ); |
| 1797 | testcase( pOp->p2==SQLITE_AFF_REAL ); |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 1798 | pIn1 = &aMem[pOp->p1]; |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 1799 | memAboutToChange(p, pIn1); |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1800 | rc = ExpandBlob(pIn1); |
drh | 4169e43 | 2014-08-25 20:11:52 +0000 | [diff] [blame] | 1801 | sqlite3VdbeMemCast(pIn1, pOp->p2, encoding); |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 1802 | UPDATE_MAX_BLOBSIZE(pIn1); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 1803 | if( rc ) goto abort_due_to_error; |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 1804 | break; |
| 1805 | } |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1806 | #endif /* SQLITE_OMIT_CAST */ |
| 1807 | |
drh | 79752b6 | 2016-08-13 10:02:17 +0000 | [diff] [blame] | 1808 | /* Opcode: Eq P1 P2 P3 P4 P5 |
drh | 88e665f | 2016-08-27 01:41:53 +0000 | [diff] [blame] | 1809 | ** Synopsis: IF r[P3]==r[P1] |
drh | 79752b6 | 2016-08-13 10:02:17 +0000 | [diff] [blame] | 1810 | ** |
| 1811 | ** Compare the values in register P1 and P3. If reg(P3)==reg(P1) then |
| 1812 | ** jump to address P2. Or if the SQLITE_STOREP2 flag is set in P5, then |
| 1813 | ** store the result of comparison in register P2. |
| 1814 | ** |
| 1815 | ** The SQLITE_AFF_MASK portion of P5 must be an affinity character - |
| 1816 | ** SQLITE_AFF_TEXT, SQLITE_AFF_INTEGER, and so forth. An attempt is made |
| 1817 | ** to coerce both inputs according to this affinity before the |
| 1818 | ** comparison is made. If the SQLITE_AFF_MASK is 0x00, then numeric |
| 1819 | ** affinity is used. Note that the affinity conversions are stored |
| 1820 | ** back into the input registers P1 and P3. So this opcode can cause |
| 1821 | ** persistent changes to registers P1 and P3. |
| 1822 | ** |
| 1823 | ** Once any conversions have taken place, and neither value is NULL, |
| 1824 | ** the values are compared. If both values are blobs then memcmp() is |
| 1825 | ** used to determine the results of the comparison. If both values |
| 1826 | ** are text, then the appropriate collating function specified in |
| 1827 | ** P4 is used to do the comparison. If P4 is not specified then |
| 1828 | ** memcmp() is used to compare text string. If both values are |
| 1829 | ** numeric, then a numeric comparison is used. If the two values |
| 1830 | ** are of different types, then numbers are considered less than |
| 1831 | ** strings and strings are considered less than blobs. |
| 1832 | ** |
| 1833 | ** If SQLITE_NULLEQ is set in P5 then the result of comparison is always either |
| 1834 | ** true or false and is never NULL. If both operands are NULL then the result |
| 1835 | ** of comparison is true. If either operand is NULL then the result is false. |
| 1836 | ** If neither operand is NULL the result is the same as it would be if |
| 1837 | ** the SQLITE_NULLEQ flag were omitted from P5. |
| 1838 | ** |
| 1839 | ** If both SQLITE_STOREP2 and SQLITE_KEEPNULL flags are set then the |
drh | 3fffbf9 | 2016-09-05 15:02:41 +0000 | [diff] [blame] | 1840 | ** content of r[P2] is only changed if the new value is NULL or 0 (false). |
| 1841 | ** In other words, a prior r[P2] value will not be overwritten by 1 (true). |
drh | 79752b6 | 2016-08-13 10:02:17 +0000 | [diff] [blame] | 1842 | */ |
| 1843 | /* Opcode: Ne P1 P2 P3 P4 P5 |
drh | 88e665f | 2016-08-27 01:41:53 +0000 | [diff] [blame] | 1844 | ** Synopsis: IF r[P3]!=r[P1] |
drh | 79752b6 | 2016-08-13 10:02:17 +0000 | [diff] [blame] | 1845 | ** |
| 1846 | ** This works just like the Eq opcode except that the jump is taken if |
| 1847 | ** the operands in registers P1 and P3 are not equal. See the Eq opcode for |
| 1848 | ** additional information. |
| 1849 | ** |
| 1850 | ** If both SQLITE_STOREP2 and SQLITE_KEEPNULL flags are set then the |
drh | 3fffbf9 | 2016-09-05 15:02:41 +0000 | [diff] [blame] | 1851 | ** content of r[P2] is only changed if the new value is NULL or 1 (true). |
| 1852 | ** In other words, a prior r[P2] value will not be overwritten by 0 (false). |
drh | 79752b6 | 2016-08-13 10:02:17 +0000 | [diff] [blame] | 1853 | */ |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 1854 | /* Opcode: Lt P1 P2 P3 P4 P5 |
drh | 88e665f | 2016-08-27 01:41:53 +0000 | [diff] [blame] | 1855 | ** Synopsis: IF r[P3]<r[P1] |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1856 | ** |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 1857 | ** Compare the values in register P1 and P3. If reg(P3)<reg(P1) then |
drh | 79752b6 | 2016-08-13 10:02:17 +0000 | [diff] [blame] | 1858 | ** jump to address P2. Or if the SQLITE_STOREP2 flag is set in P5 store |
| 1859 | ** the result of comparison (0 or 1 or NULL) into register P2. |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1860 | ** |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 1861 | ** If the SQLITE_JUMPIFNULL bit of P5 is set and either reg(P1) or |
drh | 79752b6 | 2016-08-13 10:02:17 +0000 | [diff] [blame] | 1862 | ** reg(P3) is NULL then the take the jump. If the SQLITE_JUMPIFNULL |
drh | 710c484 | 2010-08-30 01:17:20 +0000 | [diff] [blame] | 1863 | ** bit is clear then fall through if either operand is NULL. |
drh | 4f68623 | 2005-09-20 13:55:18 +0000 | [diff] [blame] | 1864 | ** |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 1865 | ** The SQLITE_AFF_MASK portion of P5 must be an affinity character - |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1866 | ** SQLITE_AFF_TEXT, SQLITE_AFF_INTEGER, and so forth. An attempt is made |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1867 | ** to coerce both inputs according to this affinity before the |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 1868 | ** comparison is made. If the SQLITE_AFF_MASK is 0x00, then numeric |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1869 | ** affinity is used. Note that the affinity conversions are stored |
| 1870 | ** back into the input registers P1 and P3. So this opcode can cause |
| 1871 | ** persistent changes to registers P1 and P3. |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 1872 | ** |
| 1873 | ** Once any conversions have taken place, and neither value is NULL, |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 1874 | ** the values are compared. If both values are blobs then memcmp() is |
| 1875 | ** used to determine the results of the comparison. If both values |
| 1876 | ** are text, then the appropriate collating function specified in |
| 1877 | ** P4 is used to do the comparison. If P4 is not specified then |
| 1878 | ** memcmp() is used to compare text string. If both values are |
| 1879 | ** numeric, then a numeric comparison is used. If the two values |
| 1880 | ** are of different types, then numbers are considered less than |
| 1881 | ** strings and strings are considered less than blobs. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1882 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1883 | /* Opcode: Le P1 P2 P3 P4 P5 |
drh | 88e665f | 2016-08-27 01:41:53 +0000 | [diff] [blame] | 1884 | ** Synopsis: IF r[P3]<=r[P1] |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1885 | ** |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 1886 | ** This works just like the Lt opcode except that the jump is taken if |
| 1887 | ** the content of register P3 is less than or equal to the content of |
| 1888 | ** register P1. See the Lt opcode for additional information. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1889 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1890 | /* Opcode: Gt P1 P2 P3 P4 P5 |
drh | 88e665f | 2016-08-27 01:41:53 +0000 | [diff] [blame] | 1891 | ** Synopsis: IF r[P3]>r[P1] |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1892 | ** |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 1893 | ** This works just like the Lt opcode except that the jump is taken if |
| 1894 | ** the content of register P3 is greater than the content of |
| 1895 | ** register P1. See the Lt opcode for additional information. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1896 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1897 | /* Opcode: Ge P1 P2 P3 P4 P5 |
drh | 88e665f | 2016-08-27 01:41:53 +0000 | [diff] [blame] | 1898 | ** Synopsis: IF r[P3]>=r[P1] |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1899 | ** |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 1900 | ** This works just like the Lt opcode except that the jump is taken if |
| 1901 | ** the content of register P3 is greater than or equal to the content of |
| 1902 | ** register P1. See the Lt opcode for additional information. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1903 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1904 | case OP_Eq: /* same as TK_EQ, jump, in1, in3 */ |
| 1905 | case OP_Ne: /* same as TK_NE, jump, in1, in3 */ |
| 1906 | case OP_Lt: /* same as TK_LT, jump, in1, in3 */ |
| 1907 | case OP_Le: /* same as TK_LE, jump, in1, in3 */ |
| 1908 | case OP_Gt: /* same as TK_GT, jump, in1, in3 */ |
| 1909 | case OP_Ge: { /* same as TK_GE, jump, in1, in3 */ |
drh | 4910a76 | 2016-09-03 01:46:15 +0000 | [diff] [blame] | 1910 | int res, res2; /* Result of the comparison of pIn1 against pIn3 */ |
drh | 6a2fe09 | 2009-09-23 02:29:36 +0000 | [diff] [blame] | 1911 | char affinity; /* Affinity to use for comparison */ |
dan | b7dca7d | 2010-03-05 16:32:12 +0000 | [diff] [blame] | 1912 | u16 flags1; /* Copy of initial value of pIn1->flags */ |
| 1913 | u16 flags3; /* Copy of initial value of pIn3->flags */ |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 1914 | |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 1915 | pIn1 = &aMem[pOp->p1]; |
| 1916 | pIn3 = &aMem[pOp->p3]; |
dan | b7dca7d | 2010-03-05 16:32:12 +0000 | [diff] [blame] | 1917 | flags1 = pIn1->flags; |
| 1918 | flags3 = pIn3->flags; |
drh | c3f1d5f | 2011-05-30 23:42:16 +0000 | [diff] [blame] | 1919 | if( (flags1 | flags3)&MEM_Null ){ |
drh | 6a2fe09 | 2009-09-23 02:29:36 +0000 | [diff] [blame] | 1920 | /* One or both operands are NULL */ |
| 1921 | if( pOp->p5 & SQLITE_NULLEQ ){ |
| 1922 | /* If SQLITE_NULLEQ is set (which will only happen if the operator is |
| 1923 | ** OP_Eq or OP_Ne) then take the jump or not depending on whether |
| 1924 | ** or not both operands are null. |
| 1925 | */ |
| 1926 | assert( pOp->opcode==OP_Eq || pOp->opcode==OP_Ne ); |
drh | 053a128 | 2012-09-19 21:15:46 +0000 | [diff] [blame] | 1927 | assert( (flags1 & MEM_Cleared)==0 ); |
drh | 3d77dee | 2014-02-19 14:20:49 +0000 | [diff] [blame] | 1928 | assert( (pOp->p5 & SQLITE_JUMPIFNULL)==0 ); |
drh | c3191d2 | 2016-10-18 16:36:15 +0000 | [diff] [blame] | 1929 | if( (flags1&flags3&MEM_Null)!=0 |
drh | 053a128 | 2012-09-19 21:15:46 +0000 | [diff] [blame] | 1930 | && (flags3&MEM_Cleared)==0 |
| 1931 | ){ |
drh | 4910a76 | 2016-09-03 01:46:15 +0000 | [diff] [blame] | 1932 | res = 0; /* Operands are equal */ |
drh | 053a128 | 2012-09-19 21:15:46 +0000 | [diff] [blame] | 1933 | }else{ |
drh | 4910a76 | 2016-09-03 01:46:15 +0000 | [diff] [blame] | 1934 | res = 1; /* Operands are not equal */ |
drh | 053a128 | 2012-09-19 21:15:46 +0000 | [diff] [blame] | 1935 | } |
drh | 6a2fe09 | 2009-09-23 02:29:36 +0000 | [diff] [blame] | 1936 | }else{ |
| 1937 | /* SQLITE_NULLEQ is clear and at least one operand is NULL, |
| 1938 | ** then the result is always NULL. |
| 1939 | ** The jump is taken if the SQLITE_JUMPIFNULL bit is set. |
| 1940 | */ |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 1941 | if( pOp->p5 & SQLITE_STOREP2 ){ |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 1942 | pOut = &aMem[pOp->p2]; |
drh | 4910a76 | 2016-09-03 01:46:15 +0000 | [diff] [blame] | 1943 | iCompare = 1; /* Operands are not equal */ |
dan | b1d6b53 | 2015-12-14 19:42:19 +0000 | [diff] [blame] | 1944 | memAboutToChange(p, pOut); |
drh | 6a2fe09 | 2009-09-23 02:29:36 +0000 | [diff] [blame] | 1945 | MemSetTypeFlag(pOut, MEM_Null); |
| 1946 | REGISTER_TRACE(pOp->p2, pOut); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 1947 | }else{ |
drh | f4345e4 | 2014-02-18 11:31:59 +0000 | [diff] [blame] | 1948 | VdbeBranchTaken(2,3); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 1949 | if( pOp->p5 & SQLITE_JUMPIFNULL ){ |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 1950 | goto jump_to_p2; |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 1951 | } |
drh | 6a2fe09 | 2009-09-23 02:29:36 +0000 | [diff] [blame] | 1952 | } |
| 1953 | break; |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 1954 | } |
drh | 6a2fe09 | 2009-09-23 02:29:36 +0000 | [diff] [blame] | 1955 | }else{ |
| 1956 | /* Neither operand is NULL. Do a comparison. */ |
| 1957 | affinity = pOp->p5 & SQLITE_AFF_MASK; |
drh | 24a0962 | 2014-09-18 16:28:59 +0000 | [diff] [blame] | 1958 | if( affinity>=SQLITE_AFF_NUMERIC ){ |
drh | 5fd0c12 | 2016-04-04 13:46:24 +0000 | [diff] [blame] | 1959 | if( (flags1 | flags3)&MEM_Str ){ |
| 1960 | if( (flags1 & (MEM_Int|MEM_Real|MEM_Str))==MEM_Str ){ |
| 1961 | applyNumericAffinity(pIn1,0); |
drh | 24846bc | 2018-08-06 01:21:53 +0000 | [diff] [blame] | 1962 | assert( flags3==pIn3->flags ); |
drh | cfdeeeb | 2018-08-04 20:12:10 +0000 | [diff] [blame] | 1963 | /* testcase( flags3!=pIn3->flags ); |
| 1964 | ** this used to be possible with pIn1==pIn3, but not since |
| 1965 | ** the column cache was removed. The following assignment |
drh | 24846bc | 2018-08-06 01:21:53 +0000 | [diff] [blame] | 1966 | ** is essentially a no-op. But, it provides defense-in-depth |
drh | cfdeeeb | 2018-08-04 20:12:10 +0000 | [diff] [blame] | 1967 | ** in case our analysis is incorrect, so it is left in. */ |
drh | 4b37cd4 | 2016-06-25 11:43:47 +0000 | [diff] [blame] | 1968 | flags3 = pIn3->flags; |
drh | 5fd0c12 | 2016-04-04 13:46:24 +0000 | [diff] [blame] | 1969 | } |
| 1970 | if( (flags3 & (MEM_Int|MEM_Real|MEM_Str))==MEM_Str ){ |
| 1971 | applyNumericAffinity(pIn3,0); |
| 1972 | } |
drh | 24a0962 | 2014-09-18 16:28:59 +0000 | [diff] [blame] | 1973 | } |
drh | 64caee4 | 2016-09-09 19:33:00 +0000 | [diff] [blame] | 1974 | /* Handle the common case of integer comparison here, as an |
| 1975 | ** optimization, to avoid a call to sqlite3MemCompare() */ |
| 1976 | if( (pIn1->flags & pIn3->flags & MEM_Int)!=0 ){ |
| 1977 | if( pIn3->u.i > pIn1->u.i ){ res = +1; goto compare_op; } |
| 1978 | if( pIn3->u.i < pIn1->u.i ){ res = -1; goto compare_op; } |
| 1979 | res = 0; |
| 1980 | goto compare_op; |
| 1981 | } |
drh | 24a0962 | 2014-09-18 16:28:59 +0000 | [diff] [blame] | 1982 | }else if( affinity==SQLITE_AFF_TEXT ){ |
drh | e5520e2 | 2015-12-31 04:34:26 +0000 | [diff] [blame] | 1983 | if( (flags1 & MEM_Str)==0 && (flags1 & (MEM_Int|MEM_Real))!=0 ){ |
drh | e7a3466 | 2014-09-19 22:44:20 +0000 | [diff] [blame] | 1984 | testcase( pIn1->flags & MEM_Int ); |
| 1985 | testcase( pIn1->flags & MEM_Real ); |
drh | 24a0962 | 2014-09-18 16:28:59 +0000 | [diff] [blame] | 1986 | sqlite3VdbeMemStringify(pIn1, encoding, 1); |
drh | bc8a6b3 | 2015-03-31 11:42:23 +0000 | [diff] [blame] | 1987 | testcase( (flags1&MEM_Dyn) != (pIn1->flags&MEM_Dyn) ); |
| 1988 | flags1 = (pIn1->flags & ~MEM_TypeMask) | (flags1 & MEM_TypeMask); |
drh | 21e19b4 | 2016-09-15 14:54:51 +0000 | [diff] [blame] | 1989 | assert( pIn1!=pIn3 ); |
drh | 24a0962 | 2014-09-18 16:28:59 +0000 | [diff] [blame] | 1990 | } |
drh | e5520e2 | 2015-12-31 04:34:26 +0000 | [diff] [blame] | 1991 | if( (flags3 & MEM_Str)==0 && (flags3 & (MEM_Int|MEM_Real))!=0 ){ |
drh | e7a3466 | 2014-09-19 22:44:20 +0000 | [diff] [blame] | 1992 | testcase( pIn3->flags & MEM_Int ); |
| 1993 | testcase( pIn3->flags & MEM_Real ); |
drh | 24a0962 | 2014-09-18 16:28:59 +0000 | [diff] [blame] | 1994 | sqlite3VdbeMemStringify(pIn3, encoding, 1); |
drh | bc8a6b3 | 2015-03-31 11:42:23 +0000 | [diff] [blame] | 1995 | testcase( (flags3&MEM_Dyn) != (pIn3->flags&MEM_Dyn) ); |
| 1996 | flags3 = (pIn3->flags & ~MEM_TypeMask) | (flags3 & MEM_TypeMask); |
drh | 24a0962 | 2014-09-18 16:28:59 +0000 | [diff] [blame] | 1997 | } |
drh | 6a2fe09 | 2009-09-23 02:29:36 +0000 | [diff] [blame] | 1998 | } |
drh | 6a2fe09 | 2009-09-23 02:29:36 +0000 | [diff] [blame] | 1999 | assert( pOp->p4type==P4_COLLSEQ || pOp->p4.pColl==0 ); |
drh | 4910a76 | 2016-09-03 01:46:15 +0000 | [diff] [blame] | 2000 | res = sqlite3MemCompare(pIn3, pIn1, pOp->p4.pColl); |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 2001 | } |
drh | 64caee4 | 2016-09-09 19:33:00 +0000 | [diff] [blame] | 2002 | compare_op: |
drh | 5859636 | 2017-08-03 00:29:23 +0000 | [diff] [blame] | 2003 | /* At this point, res is negative, zero, or positive if reg[P1] is |
| 2004 | ** less than, equal to, or greater than reg[P3], respectively. Compute |
| 2005 | ** the answer to this operator in res2, depending on what the comparison |
| 2006 | ** operator actually is. The next block of code depends on the fact |
| 2007 | ** that the 6 comparison operators are consecutive integers in this |
| 2008 | ** order: NE, EQ, GT, LE, LT, GE */ |
| 2009 | assert( OP_Eq==OP_Ne+1 ); assert( OP_Gt==OP_Ne+2 ); assert( OP_Le==OP_Ne+3 ); |
| 2010 | assert( OP_Lt==OP_Ne+4 ); assert( OP_Ge==OP_Ne+5 ); |
| 2011 | if( res<0 ){ /* ne, eq, gt, le, lt, ge */ |
| 2012 | static const unsigned char aLTb[] = { 1, 0, 0, 1, 1, 0 }; |
| 2013 | res2 = aLTb[pOp->opcode - OP_Ne]; |
| 2014 | }else if( res==0 ){ |
| 2015 | static const unsigned char aEQb[] = { 0, 1, 0, 1, 0, 1 }; |
| 2016 | res2 = aEQb[pOp->opcode - OP_Ne]; |
| 2017 | }else{ |
| 2018 | static const unsigned char aGTb[] = { 1, 0, 1, 0, 0, 1 }; |
| 2019 | res2 = aGTb[pOp->opcode - OP_Ne]; |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 2020 | } |
| 2021 | |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 2022 | /* Undo any changes made by applyAffinity() to the input registers. */ |
| 2023 | assert( (pIn1->flags & MEM_Dyn) == (flags1 & MEM_Dyn) ); |
| 2024 | pIn1->flags = flags1; |
| 2025 | assert( (pIn3->flags & MEM_Dyn) == (flags3 & MEM_Dyn) ); |
| 2026 | pIn3->flags = flags3; |
| 2027 | |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 2028 | if( pOp->p5 & SQLITE_STOREP2 ){ |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 2029 | pOut = &aMem[pOp->p2]; |
drh | 4910a76 | 2016-09-03 01:46:15 +0000 | [diff] [blame] | 2030 | iCompare = res; |
drh | 3fffbf9 | 2016-09-05 15:02:41 +0000 | [diff] [blame] | 2031 | if( (pOp->p5 & SQLITE_KEEPNULL)!=0 ){ |
drh | 79752b6 | 2016-08-13 10:02:17 +0000 | [diff] [blame] | 2032 | /* The KEEPNULL flag prevents OP_Eq from overwriting a NULL with 1 |
drh | 3fffbf9 | 2016-09-05 15:02:41 +0000 | [diff] [blame] | 2033 | ** and prevents OP_Ne from overwriting NULL with 0. This flag |
| 2034 | ** is only used in contexts where either: |
| 2035 | ** (1) op==OP_Eq && (r[P2]==NULL || r[P2]==0) |
| 2036 | ** (2) op==OP_Ne && (r[P2]==NULL || r[P2]==1) |
| 2037 | ** Therefore it is not necessary to check the content of r[P2] for |
| 2038 | ** NULL. */ |
drh | 79752b6 | 2016-08-13 10:02:17 +0000 | [diff] [blame] | 2039 | assert( pOp->opcode==OP_Ne || pOp->opcode==OP_Eq ); |
drh | 4910a76 | 2016-09-03 01:46:15 +0000 | [diff] [blame] | 2040 | assert( res2==0 || res2==1 ); |
drh | 3fffbf9 | 2016-09-05 15:02:41 +0000 | [diff] [blame] | 2041 | testcase( res2==0 && pOp->opcode==OP_Eq ); |
| 2042 | testcase( res2==1 && pOp->opcode==OP_Eq ); |
| 2043 | testcase( res2==0 && pOp->opcode==OP_Ne ); |
| 2044 | testcase( res2==1 && pOp->opcode==OP_Ne ); |
drh | 4910a76 | 2016-09-03 01:46:15 +0000 | [diff] [blame] | 2045 | if( (pOp->opcode==OP_Eq)==res2 ) break; |
drh | 79752b6 | 2016-08-13 10:02:17 +0000 | [diff] [blame] | 2046 | } |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 2047 | memAboutToChange(p, pOut); |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 2048 | MemSetTypeFlag(pOut, MEM_Int); |
drh | 4910a76 | 2016-09-03 01:46:15 +0000 | [diff] [blame] | 2049 | pOut->u.i = res2; |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 2050 | REGISTER_TRACE(pOp->p2, pOut); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 2051 | }else{ |
drh | f4345e4 | 2014-02-18 11:31:59 +0000 | [diff] [blame] | 2052 | VdbeBranchTaken(res!=0, (pOp->p5 & SQLITE_NULLEQ)?2:3); |
drh | 4910a76 | 2016-09-03 01:46:15 +0000 | [diff] [blame] | 2053 | if( res2 ){ |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 2054 | goto jump_to_p2; |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 2055 | } |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 2056 | } |
| 2057 | break; |
| 2058 | } |
drh | c9b84a1 | 2002-06-20 11:36:48 +0000 | [diff] [blame] | 2059 | |
drh | 79752b6 | 2016-08-13 10:02:17 +0000 | [diff] [blame] | 2060 | /* Opcode: ElseNotEq * P2 * * * |
| 2061 | ** |
drh | fd7459e | 2016-09-17 17:39:01 +0000 | [diff] [blame] | 2062 | ** This opcode must immediately follow an OP_Lt or OP_Gt comparison operator. |
| 2063 | ** If result of an OP_Eq comparison on the same two operands |
| 2064 | ** would have be NULL or false (0), then then jump to P2. |
| 2065 | ** If the result of an OP_Eq comparison on the two previous operands |
| 2066 | ** would have been true (1), then fall through. |
drh | 79752b6 | 2016-08-13 10:02:17 +0000 | [diff] [blame] | 2067 | */ |
| 2068 | case OP_ElseNotEq: { /* same as TK_ESCAPE, jump */ |
| 2069 | assert( pOp>aOp ); |
| 2070 | assert( pOp[-1].opcode==OP_Lt || pOp[-1].opcode==OP_Gt ); |
drh | 4910a76 | 2016-09-03 01:46:15 +0000 | [diff] [blame] | 2071 | assert( pOp[-1].p5 & SQLITE_STOREP2 ); |
drh | 0f825a7 | 2016-08-13 14:17:02 +0000 | [diff] [blame] | 2072 | VdbeBranchTaken(iCompare!=0, 2); |
| 2073 | if( iCompare!=0 ) goto jump_to_p2; |
drh | 79752b6 | 2016-08-13 10:02:17 +0000 | [diff] [blame] | 2074 | break; |
| 2075 | } |
| 2076 | |
| 2077 | |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 2078 | /* Opcode: Permutation * * * P4 * |
| 2079 | ** |
drh | b7dab70 | 2017-01-26 18:00:00 +0000 | [diff] [blame] | 2080 | ** Set the permutation used by the OP_Compare operator in the next |
| 2081 | ** instruction. The permutation is stored in the P4 operand. |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 2082 | ** |
drh | 953f761 | 2012-12-07 22:18:54 +0000 | [diff] [blame] | 2083 | ** The permutation is only valid until the next OP_Compare that has |
| 2084 | ** the OPFLAG_PERMUTE bit set in P5. Typically the OP_Permutation should |
| 2085 | ** occur immediately prior to the OP_Compare. |
drh | b170202 | 2016-01-30 00:45:18 +0000 | [diff] [blame] | 2086 | ** |
| 2087 | ** The first integer in the P4 integer array is the length of the array |
| 2088 | ** and does not become part of the permutation. |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 2089 | */ |
| 2090 | case OP_Permutation: { |
| 2091 | assert( pOp->p4type==P4_INTARRAY ); |
| 2092 | assert( pOp->p4.ai ); |
drh | b7dab70 | 2017-01-26 18:00:00 +0000 | [diff] [blame] | 2093 | assert( pOp[1].opcode==OP_Compare ); |
| 2094 | assert( pOp[1].p5 & OPFLAG_PERMUTE ); |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 2095 | break; |
| 2096 | } |
| 2097 | |
drh | 953f761 | 2012-12-07 22:18:54 +0000 | [diff] [blame] | 2098 | /* Opcode: Compare P1 P2 P3 P4 P5 |
drh | 079a307 | 2014-03-19 14:10:55 +0000 | [diff] [blame] | 2099 | ** Synopsis: r[P1@P3] <-> r[P2@P3] |
drh | 16ee60f | 2008-06-20 18:13:25 +0000 | [diff] [blame] | 2100 | ** |
drh | 710c484 | 2010-08-30 01:17:20 +0000 | [diff] [blame] | 2101 | ** Compare two vectors of registers in reg(P1)..reg(P1+P3-1) (call this |
| 2102 | ** 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] | 2103 | ** the comparison for use by the next OP_Jump instruct. |
| 2104 | ** |
drh | 0ca10df | 2012-12-08 13:26:23 +0000 | [diff] [blame] | 2105 | ** If P5 has the OPFLAG_PERMUTE bit set, then the order of comparison is |
| 2106 | ** determined by the most recent OP_Permutation operator. If the |
| 2107 | ** OPFLAG_PERMUTE bit is clear, then register are compared in sequential |
| 2108 | ** order. |
| 2109 | ** |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 2110 | ** P4 is a KeyInfo structure that defines collating sequences and sort |
| 2111 | ** orders for the comparison. The permutation applies to registers |
| 2112 | ** only. The KeyInfo elements are used sequentially. |
| 2113 | ** |
| 2114 | ** The comparison is a sort comparison, so NULLs compare equal, |
| 2115 | ** NULLs are less than numbers, numbers are less than strings, |
drh | 16ee60f | 2008-06-20 18:13:25 +0000 | [diff] [blame] | 2116 | ** and strings are less than blobs. |
| 2117 | */ |
| 2118 | case OP_Compare: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2119 | int n; |
| 2120 | int i; |
| 2121 | int p1; |
| 2122 | int p2; |
| 2123 | const KeyInfo *pKeyInfo; |
| 2124 | int idx; |
| 2125 | CollSeq *pColl; /* Collating sequence to use on this term */ |
| 2126 | int bRev; /* True for DESCENDING sort order */ |
drh | b7dab70 | 2017-01-26 18:00:00 +0000 | [diff] [blame] | 2127 | int *aPermute; /* The permutation */ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2128 | |
drh | b7dab70 | 2017-01-26 18:00:00 +0000 | [diff] [blame] | 2129 | if( (pOp->p5 & OPFLAG_PERMUTE)==0 ){ |
| 2130 | aPermute = 0; |
| 2131 | }else{ |
| 2132 | assert( pOp>aOp ); |
| 2133 | assert( pOp[-1].opcode==OP_Permutation ); |
| 2134 | assert( pOp[-1].p4type==P4_INTARRAY ); |
| 2135 | aPermute = pOp[-1].p4.ai + 1; |
| 2136 | assert( aPermute!=0 ); |
| 2137 | } |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2138 | n = pOp->p3; |
| 2139 | pKeyInfo = pOp->p4.pKeyInfo; |
drh | 16ee60f | 2008-06-20 18:13:25 +0000 | [diff] [blame] | 2140 | assert( n>0 ); |
drh | 93a960a | 2008-07-10 00:32:42 +0000 | [diff] [blame] | 2141 | assert( pKeyInfo!=0 ); |
drh | 16ee60f | 2008-06-20 18:13:25 +0000 | [diff] [blame] | 2142 | p1 = pOp->p1; |
drh | 16ee60f | 2008-06-20 18:13:25 +0000 | [diff] [blame] | 2143 | p2 = pOp->p2; |
drh | d879e3e | 2017-02-13 13:35:55 +0000 | [diff] [blame] | 2144 | #ifdef SQLITE_DEBUG |
drh | 6a2fe09 | 2009-09-23 02:29:36 +0000 | [diff] [blame] | 2145 | if( aPermute ){ |
| 2146 | int k, mx = 0; |
| 2147 | for(k=0; k<n; k++) if( aPermute[k]>mx ) mx = aPermute[k]; |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 2148 | assert( p1>0 && p1+mx<=(p->nMem+1 - p->nCursor)+1 ); |
| 2149 | assert( p2>0 && p2+mx<=(p->nMem+1 - p->nCursor)+1 ); |
drh | 6a2fe09 | 2009-09-23 02:29:36 +0000 | [diff] [blame] | 2150 | }else{ |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 2151 | assert( p1>0 && p1+n<=(p->nMem+1 - p->nCursor)+1 ); |
| 2152 | assert( p2>0 && p2+n<=(p->nMem+1 - p->nCursor)+1 ); |
drh | 6a2fe09 | 2009-09-23 02:29:36 +0000 | [diff] [blame] | 2153 | } |
| 2154 | #endif /* SQLITE_DEBUG */ |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 2155 | for(i=0; i<n; i++){ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2156 | idx = aPermute ? aPermute[i] : i; |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 2157 | assert( memIsValid(&aMem[p1+idx]) ); |
| 2158 | assert( memIsValid(&aMem[p2+idx]) ); |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 2159 | REGISTER_TRACE(p1+idx, &aMem[p1+idx]); |
| 2160 | REGISTER_TRACE(p2+idx, &aMem[p2+idx]); |
drh | a485ad1 | 2017-08-02 22:43:14 +0000 | [diff] [blame] | 2161 | assert( i<pKeyInfo->nKeyField ); |
drh | 93a960a | 2008-07-10 00:32:42 +0000 | [diff] [blame] | 2162 | pColl = pKeyInfo->aColl[i]; |
| 2163 | bRev = pKeyInfo->aSortOrder[i]; |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 2164 | iCompare = sqlite3MemCompare(&aMem[p1+idx], &aMem[p2+idx], pColl); |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 2165 | if( iCompare ){ |
| 2166 | if( bRev ) iCompare = -iCompare; |
| 2167 | break; |
| 2168 | } |
drh | 16ee60f | 2008-06-20 18:13:25 +0000 | [diff] [blame] | 2169 | } |
| 2170 | break; |
| 2171 | } |
| 2172 | |
| 2173 | /* Opcode: Jump P1 P2 P3 * * |
| 2174 | ** |
| 2175 | ** Jump to the instruction at address P1, P2, or P3 depending on whether |
| 2176 | ** in the most recent OP_Compare instruction the P1 vector was less than |
| 2177 | ** equal to, or greater than the P2 vector, respectively. |
| 2178 | */ |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 2179 | case OP_Jump: { /* jump */ |
| 2180 | if( iCompare<0 ){ |
drh | 7083a48 | 2018-07-10 16:04:04 +0000 | [diff] [blame] | 2181 | VdbeBranchTaken(0,4); pOp = &aOp[pOp->p1 - 1]; |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 2182 | }else if( iCompare==0 ){ |
drh | 7083a48 | 2018-07-10 16:04:04 +0000 | [diff] [blame] | 2183 | VdbeBranchTaken(1,4); pOp = &aOp[pOp->p2 - 1]; |
drh | 16ee60f | 2008-06-20 18:13:25 +0000 | [diff] [blame] | 2184 | }else{ |
drh | 7083a48 | 2018-07-10 16:04:04 +0000 | [diff] [blame] | 2185 | VdbeBranchTaken(2,4); pOp = &aOp[pOp->p3 - 1]; |
drh | 16ee60f | 2008-06-20 18:13:25 +0000 | [diff] [blame] | 2186 | } |
| 2187 | break; |
| 2188 | } |
| 2189 | |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 2190 | /* Opcode: And P1 P2 P3 * * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 2191 | ** Synopsis: r[P3]=(r[P1] && r[P2]) |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2192 | ** |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 2193 | ** Take the logical AND of the values in registers P1 and P2 and |
| 2194 | ** write the result into register P3. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2195 | ** |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 2196 | ** If either P1 or P2 is 0 (false) then the result is 0 even if |
| 2197 | ** the other input is NULL. A NULL and true or two NULLs give |
| 2198 | ** a NULL output. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2199 | */ |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 2200 | /* Opcode: Or P1 P2 P3 * * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 2201 | ** Synopsis: r[P3]=(r[P1] || r[P2]) |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 2202 | ** |
| 2203 | ** Take the logical OR of the values in register P1 and P2 and |
| 2204 | ** store the answer in register P3. |
| 2205 | ** |
| 2206 | ** If either P1 or P2 is nonzero (true) then the result is 1 (true) |
| 2207 | ** even if the other input is NULL. A NULL and false or two NULLs |
| 2208 | ** give a NULL output. |
| 2209 | */ |
| 2210 | case OP_And: /* same as TK_AND, in1, in2, out3 */ |
| 2211 | case OP_Or: { /* same as TK_OR, in1, in2, out3 */ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2212 | int v1; /* Left operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */ |
| 2213 | int v2; /* Right operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */ |
drh | bb11351 | 2002-05-27 01:04:51 +0000 | [diff] [blame] | 2214 | |
drh | 1fcfa72 | 2018-02-26 15:27:31 +0000 | [diff] [blame] | 2215 | v1 = sqlite3VdbeBooleanValue(&aMem[pOp->p1], 2); |
| 2216 | v2 = sqlite3VdbeBooleanValue(&aMem[pOp->p2], 2); |
drh | bb11351 | 2002-05-27 01:04:51 +0000 | [diff] [blame] | 2217 | if( pOp->opcode==OP_And ){ |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 2218 | 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] | 2219 | v1 = and_logic[v1*3+v2]; |
| 2220 | }else{ |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 2221 | 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] | 2222 | v1 = or_logic[v1*3+v2]; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2223 | } |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 2224 | pOut = &aMem[pOp->p3]; |
drh | bb11351 | 2002-05-27 01:04:51 +0000 | [diff] [blame] | 2225 | if( v1==2 ){ |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 2226 | MemSetTypeFlag(pOut, MEM_Null); |
drh | bb11351 | 2002-05-27 01:04:51 +0000 | [diff] [blame] | 2227 | }else{ |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 2228 | pOut->u.i = v1; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 2229 | MemSetTypeFlag(pOut, MEM_Int); |
drh | bb11351 | 2002-05-27 01:04:51 +0000 | [diff] [blame] | 2230 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2231 | break; |
| 2232 | } |
| 2233 | |
drh | 8abed7b | 2018-02-26 18:49:05 +0000 | [diff] [blame] | 2234 | /* Opcode: IsTrue P1 P2 P3 P4 * |
| 2235 | ** Synopsis: r[P2] = coalesce(r[P1]==TRUE,P3) ^ P4 |
| 2236 | ** |
| 2237 | ** This opcode implements the IS TRUE, IS FALSE, IS NOT TRUE, and |
| 2238 | ** IS NOT FALSE operators. |
| 2239 | ** |
drh | 96acafb | 2018-02-27 14:49:25 +0000 | [diff] [blame] | 2240 | ** Interpret the value in register P1 as a boolean value. Store that |
drh | 8abed7b | 2018-02-26 18:49:05 +0000 | [diff] [blame] | 2241 | ** boolean (a 0 or 1) in register P2. Or if the value in register P1 is |
| 2242 | ** NULL, then the P3 is stored in register P2. Invert the answer if P4 |
| 2243 | ** is 1. |
| 2244 | ** |
| 2245 | ** The logic is summarized like this: |
| 2246 | ** |
| 2247 | ** <ul> |
drh | 96acafb | 2018-02-27 14:49:25 +0000 | [diff] [blame] | 2248 | ** <li> If P3==0 and P4==0 then r[P2] := r[P1] IS TRUE |
| 2249 | ** <li> If P3==1 and P4==1 then r[P2] := r[P1] IS FALSE |
| 2250 | ** <li> If P3==0 and P4==1 then r[P2] := r[P1] IS NOT TRUE |
| 2251 | ** <li> If P3==1 and P4==0 then r[P2] := r[P1] IS NOT FALSE |
drh | 8abed7b | 2018-02-26 18:49:05 +0000 | [diff] [blame] | 2252 | ** </ul> |
| 2253 | */ |
| 2254 | case OP_IsTrue: { /* in1, out2 */ |
| 2255 | assert( pOp->p4type==P4_INT32 ); |
| 2256 | assert( pOp->p4.i==0 || pOp->p4.i==1 ); |
drh | 96acafb | 2018-02-27 14:49:25 +0000 | [diff] [blame] | 2257 | assert( pOp->p3==0 || pOp->p3==1 ); |
drh | 8abed7b | 2018-02-26 18:49:05 +0000 | [diff] [blame] | 2258 | sqlite3VdbeMemSetInt64(&aMem[pOp->p2], |
| 2259 | sqlite3VdbeBooleanValue(&aMem[pOp->p1], pOp->p3) ^ pOp->p4.i); |
| 2260 | break; |
| 2261 | } |
| 2262 | |
drh | e99fa2a | 2008-12-15 15:27:51 +0000 | [diff] [blame] | 2263 | /* Opcode: Not P1 P2 * * * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 2264 | ** Synopsis: r[P2]= !r[P1] |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2265 | ** |
drh | e99fa2a | 2008-12-15 15:27:51 +0000 | [diff] [blame] | 2266 | ** Interpret the value in register P1 as a boolean value. Store the |
| 2267 | ** boolean complement in register P2. If the value in register P1 is |
| 2268 | ** NULL, then a NULL is stored in P2. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2269 | */ |
drh | 93952eb | 2009-11-13 19:43:43 +0000 | [diff] [blame] | 2270 | case OP_Not: { /* same as TK_NOT, in1, out2 */ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 2271 | pIn1 = &aMem[pOp->p1]; |
| 2272 | pOut = &aMem[pOp->p2]; |
drh | 0725cab | 2014-09-17 14:52:46 +0000 | [diff] [blame] | 2273 | if( (pIn1->flags & MEM_Null)==0 ){ |
drh | bc8f68a | 2018-02-26 15:31:39 +0000 | [diff] [blame] | 2274 | sqlite3VdbeMemSetInt64(pOut, !sqlite3VdbeBooleanValue(pIn1,0)); |
drh | 007c843 | 2018-02-26 03:20:18 +0000 | [diff] [blame] | 2275 | }else{ |
| 2276 | sqlite3VdbeMemSetNull(pOut); |
drh | e99fa2a | 2008-12-15 15:27:51 +0000 | [diff] [blame] | 2277 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2278 | break; |
| 2279 | } |
| 2280 | |
drh | e99fa2a | 2008-12-15 15:27:51 +0000 | [diff] [blame] | 2281 | /* Opcode: BitNot P1 P2 * * * |
drh | cd9e014 | 2018-06-12 13:16:57 +0000 | [diff] [blame] | 2282 | ** Synopsis: r[P2]= ~r[P1] |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 2283 | ** |
drh | e99fa2a | 2008-12-15 15:27:51 +0000 | [diff] [blame] | 2284 | ** Interpret the content of register P1 as an integer. Store the |
| 2285 | ** ones-complement of the P1 value into register P2. If P1 holds |
| 2286 | ** a NULL then store a NULL in P2. |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 2287 | */ |
drh | 93952eb | 2009-11-13 19:43:43 +0000 | [diff] [blame] | 2288 | case OP_BitNot: { /* same as TK_BITNOT, in1, out2 */ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 2289 | pIn1 = &aMem[pOp->p1]; |
| 2290 | pOut = &aMem[pOp->p2]; |
drh | 0725cab | 2014-09-17 14:52:46 +0000 | [diff] [blame] | 2291 | sqlite3VdbeMemSetNull(pOut); |
| 2292 | if( (pIn1->flags & MEM_Null)==0 ){ |
| 2293 | pOut->flags = MEM_Int; |
| 2294 | pOut->u.i = ~sqlite3VdbeIntValue(pIn1); |
drh | e99fa2a | 2008-12-15 15:27:51 +0000 | [diff] [blame] | 2295 | } |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 2296 | break; |
| 2297 | } |
| 2298 | |
drh | 48f2d3b | 2011-09-16 01:34:43 +0000 | [diff] [blame] | 2299 | /* Opcode: Once P1 P2 * * * |
| 2300 | ** |
drh | ab087d4 | 2017-03-24 17:59:56 +0000 | [diff] [blame] | 2301 | ** Fall through to the next instruction the first time this opcode is |
| 2302 | ** encountered on each invocation of the byte-code program. Jump to P2 |
| 2303 | ** on the second and all subsequent encounters during the same invocation. |
| 2304 | ** |
| 2305 | ** Top-level programs determine first invocation by comparing the P1 |
| 2306 | ** operand against the P1 operand on the OP_Init opcode at the beginning |
| 2307 | ** of the program. If the P1 values differ, then fall through and make |
| 2308 | ** the P1 of this opcode equal to the P1 of OP_Init. If P1 values are |
| 2309 | ** the same then take the jump. |
| 2310 | ** |
| 2311 | ** For subprograms, there is a bitmask in the VdbeFrame that determines |
| 2312 | ** whether or not the jump should be taken. The bitmask is necessary |
| 2313 | ** because the self-altering code trick does not work for recursive |
| 2314 | ** triggers. |
drh | 48f2d3b | 2011-09-16 01:34:43 +0000 | [diff] [blame] | 2315 | */ |
dan | 1d8cb21 | 2011-12-09 13:24:16 +0000 | [diff] [blame] | 2316 | case OP_Once: { /* jump */ |
drh | ab087d4 | 2017-03-24 17:59:56 +0000 | [diff] [blame] | 2317 | u32 iAddr; /* Address of this instruction */ |
drh | 9e5eb9c | 2016-09-18 16:08:10 +0000 | [diff] [blame] | 2318 | assert( p->aOp[0].opcode==OP_Init ); |
drh | ab087d4 | 2017-03-24 17:59:56 +0000 | [diff] [blame] | 2319 | if( p->pFrame ){ |
| 2320 | iAddr = (int)(pOp - p->aOp); |
| 2321 | if( (p->pFrame->aOnce[iAddr/8] & (1<<(iAddr & 7)))!=0 ){ |
| 2322 | VdbeBranchTaken(1, 2); |
drh | ab087d4 | 2017-03-24 17:59:56 +0000 | [diff] [blame] | 2323 | goto jump_to_p2; |
| 2324 | } |
drh | 18333ef | 2017-03-24 18:38:41 +0000 | [diff] [blame] | 2325 | p->pFrame->aOnce[iAddr/8] |= 1<<(iAddr & 7); |
dan | 1d8cb21 | 2011-12-09 13:24:16 +0000 | [diff] [blame] | 2326 | }else{ |
drh | ab087d4 | 2017-03-24 17:59:56 +0000 | [diff] [blame] | 2327 | if( p->aOp[0].p1==pOp->p1 ){ |
| 2328 | VdbeBranchTaken(1, 2); |
| 2329 | goto jump_to_p2; |
| 2330 | } |
dan | 1d8cb21 | 2011-12-09 13:24:16 +0000 | [diff] [blame] | 2331 | } |
drh | ab087d4 | 2017-03-24 17:59:56 +0000 | [diff] [blame] | 2332 | VdbeBranchTaken(0, 2); |
| 2333 | pOp->p1 = p->aOp[0].p1; |
dan | 1d8cb21 | 2011-12-09 13:24:16 +0000 | [diff] [blame] | 2334 | break; |
| 2335 | } |
| 2336 | |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 2337 | /* Opcode: If P1 P2 P3 * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2338 | ** |
drh | ef8662b | 2011-06-20 21:47:58 +0000 | [diff] [blame] | 2339 | ** Jump to P2 if the value in register P1 is true. The value |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 2340 | ** is considered true if it is numeric and non-zero. If the value |
drh | e21a6e1 | 2014-08-01 18:00:24 +0000 | [diff] [blame] | 2341 | ** in P1 is NULL then take the jump if and only if P3 is non-zero. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2342 | */ |
drh | 1fcfa72 | 2018-02-26 15:27:31 +0000 | [diff] [blame] | 2343 | case OP_If: { /* jump, in1 */ |
| 2344 | int c; |
| 2345 | c = sqlite3VdbeBooleanValue(&aMem[pOp->p1], pOp->p3); |
| 2346 | VdbeBranchTaken(c!=0, 2); |
| 2347 | if( c ) goto jump_to_p2; |
| 2348 | break; |
| 2349 | } |
| 2350 | |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 2351 | /* Opcode: IfNot P1 P2 P3 * * |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 2352 | ** |
drh | ef8662b | 2011-06-20 21:47:58 +0000 | [diff] [blame] | 2353 | ** Jump to P2 if the value in register P1 is False. The value |
drh | b8475df | 2011-12-09 16:21:19 +0000 | [diff] [blame] | 2354 | ** is considered false if it has a numeric value of zero. If the value |
drh | e21a6e1 | 2014-08-01 18:00:24 +0000 | [diff] [blame] | 2355 | ** in P1 is NULL then take the jump if and only if P3 is non-zero. |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 2356 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2357 | case OP_IfNot: { /* jump, in1 */ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2358 | int c; |
drh | 1fcfa72 | 2018-02-26 15:27:31 +0000 | [diff] [blame] | 2359 | c = !sqlite3VdbeBooleanValue(&aMem[pOp->p1], !pOp->p3); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 2360 | VdbeBranchTaken(c!=0, 2); |
drh | 1fcfa72 | 2018-02-26 15:27:31 +0000 | [diff] [blame] | 2361 | if( c ) goto jump_to_p2; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2362 | break; |
| 2363 | } |
| 2364 | |
drh | 830ecf9 | 2009-06-18 00:41:55 +0000 | [diff] [blame] | 2365 | /* Opcode: IsNull P1 P2 * * * |
drh | 72e26de | 2016-08-24 21:24:04 +0000 | [diff] [blame] | 2366 | ** Synopsis: if r[P1]==NULL goto P2 |
drh | 477df4b | 2008-01-05 18:48:24 +0000 | [diff] [blame] | 2367 | ** |
drh | 830ecf9 | 2009-06-18 00:41:55 +0000 | [diff] [blame] | 2368 | ** Jump to P2 if the value in register P1 is NULL. |
drh | 477df4b | 2008-01-05 18:48:24 +0000 | [diff] [blame] | 2369 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2370 | case OP_IsNull: { /* same as TK_ISNULL, jump, in1 */ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 2371 | pIn1 = &aMem[pOp->p1]; |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 2372 | VdbeBranchTaken( (pIn1->flags & MEM_Null)!=0, 2); |
drh | 830ecf9 | 2009-06-18 00:41:55 +0000 | [diff] [blame] | 2373 | if( (pIn1->flags & MEM_Null)!=0 ){ |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 2374 | goto jump_to_p2; |
drh | 830ecf9 | 2009-06-18 00:41:55 +0000 | [diff] [blame] | 2375 | } |
drh | 477df4b | 2008-01-05 18:48:24 +0000 | [diff] [blame] | 2376 | break; |
| 2377 | } |
| 2378 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 2379 | /* Opcode: NotNull P1 P2 * * * |
drh | fc8d4f9 | 2013-11-08 15:19:46 +0000 | [diff] [blame] | 2380 | ** Synopsis: if r[P1]!=NULL goto P2 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2381 | ** |
drh | 6a288a3 | 2008-01-07 19:20:24 +0000 | [diff] [blame] | 2382 | ** Jump to P2 if the value in register P1 is not NULL. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2383 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2384 | case OP_NotNull: { /* same as TK_NOTNULL, jump, in1 */ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 2385 | pIn1 = &aMem[pOp->p1]; |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 2386 | VdbeBranchTaken( (pIn1->flags & MEM_Null)==0, 2); |
drh | 6a288a3 | 2008-01-07 19:20:24 +0000 | [diff] [blame] | 2387 | if( (pIn1->flags & MEM_Null)==0 ){ |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 2388 | goto jump_to_p2; |
drh | 6a288a3 | 2008-01-07 19:20:24 +0000 | [diff] [blame] | 2389 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2390 | break; |
| 2391 | } |
| 2392 | |
drh | 31d6fd5 | 2017-04-14 19:03:10 +0000 | [diff] [blame] | 2393 | /* Opcode: IfNullRow P1 P2 P3 * * |
| 2394 | ** Synopsis: if P1.nullRow then r[P3]=NULL, goto P2 |
| 2395 | ** |
| 2396 | ** Check the cursor P1 to see if it is currently pointing at a NULL row. |
| 2397 | ** If it is, then set register P3 to NULL and jump immediately to P2. |
| 2398 | ** If P1 is not on a NULL row, then fall through without making any |
| 2399 | ** changes. |
| 2400 | */ |
| 2401 | case OP_IfNullRow: { /* jump */ |
| 2402 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
drh | 3f1e9e0 | 2017-05-23 01:21:07 +0000 | [diff] [blame] | 2403 | assert( p->apCsr[pOp->p1]!=0 ); |
drh | 31d6fd5 | 2017-04-14 19:03:10 +0000 | [diff] [blame] | 2404 | if( p->apCsr[pOp->p1]->nullRow ){ |
| 2405 | sqlite3VdbeMemSetNull(aMem + pOp->p3); |
| 2406 | goto jump_to_p2; |
| 2407 | } |
| 2408 | break; |
| 2409 | } |
| 2410 | |
drh | 092457b | 2017-12-29 15:04:49 +0000 | [diff] [blame] | 2411 | #ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC |
| 2412 | /* Opcode: Offset P1 P2 P3 * * |
| 2413 | ** Synopsis: r[P3] = sqlite_offset(P1) |
drh | 2fc865c | 2017-12-16 20:20:37 +0000 | [diff] [blame] | 2414 | ** |
drh | 092457b | 2017-12-29 15:04:49 +0000 | [diff] [blame] | 2415 | ** Store in register r[P3] the byte offset into the database file that is the |
drh | 2fc865c | 2017-12-16 20:20:37 +0000 | [diff] [blame] | 2416 | ** start of the payload for the record at which that cursor P1 is currently |
| 2417 | ** pointing. |
drh | fe6d20e | 2017-12-29 14:33:54 +0000 | [diff] [blame] | 2418 | ** |
drh | 092457b | 2017-12-29 15:04:49 +0000 | [diff] [blame] | 2419 | ** P2 is the column number for the argument to the sqlite_offset() function. |
drh | fe6d20e | 2017-12-29 14:33:54 +0000 | [diff] [blame] | 2420 | ** This opcode does not use P2 itself, but the P2 value is used by the |
| 2421 | ** code generator. The P1, P2, and P3 operands to this opcode are the |
mistachkin | 5e9825e | 2018-03-01 18:09:02 +0000 | [diff] [blame] | 2422 | ** same as for OP_Column. |
drh | 092457b | 2017-12-29 15:04:49 +0000 | [diff] [blame] | 2423 | ** |
| 2424 | ** This opcode is only available if SQLite is compiled with the |
| 2425 | ** -DSQLITE_ENABLE_OFFSET_SQL_FUNC option. |
drh | 2fc865c | 2017-12-16 20:20:37 +0000 | [diff] [blame] | 2426 | */ |
drh | 092457b | 2017-12-29 15:04:49 +0000 | [diff] [blame] | 2427 | case OP_Offset: { /* out3 */ |
drh | 2fc865c | 2017-12-16 20:20:37 +0000 | [diff] [blame] | 2428 | VdbeCursor *pC; /* The VDBE cursor */ |
| 2429 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 2430 | pC = p->apCsr[pOp->p1]; |
drh | fe6d20e | 2017-12-29 14:33:54 +0000 | [diff] [blame] | 2431 | pOut = &p->aMem[pOp->p3]; |
drh | c64487b | 2017-12-29 17:21:21 +0000 | [diff] [blame] | 2432 | if( NEVER(pC==0) || pC->eCurType!=CURTYPE_BTREE ){ |
drh | fe6d20e | 2017-12-29 14:33:54 +0000 | [diff] [blame] | 2433 | sqlite3VdbeMemSetNull(pOut); |
drh | 2fc865c | 2017-12-16 20:20:37 +0000 | [diff] [blame] | 2434 | }else{ |
drh | 092457b | 2017-12-29 15:04:49 +0000 | [diff] [blame] | 2435 | sqlite3VdbeMemSetInt64(pOut, sqlite3BtreeOffset(pC->uc.pCursor)); |
drh | 2fc865c | 2017-12-16 20:20:37 +0000 | [diff] [blame] | 2436 | } |
| 2437 | break; |
| 2438 | } |
drh | 092457b | 2017-12-29 15:04:49 +0000 | [diff] [blame] | 2439 | #endif /* SQLITE_ENABLE_OFFSET_SQL_FUNC */ |
drh | 2fc865c | 2017-12-16 20:20:37 +0000 | [diff] [blame] | 2440 | |
drh | 3e9ca09 | 2009-09-08 01:14:48 +0000 | [diff] [blame] | 2441 | /* Opcode: Column P1 P2 P3 P4 P5 |
drh | 72e26de | 2016-08-24 21:24:04 +0000 | [diff] [blame] | 2442 | ** Synopsis: r[P3]=PX |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2443 | ** |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 2444 | ** Interpret the data that cursor P1 points to as a structure built using |
| 2445 | ** the MakeRecord instruction. (See the MakeRecord opcode for additional |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 2446 | ** information about the format of the data.) Extract the P2-th column |
| 2447 | ** from this record. If there are less that (P2+1) |
| 2448 | ** values in the record, extract a NULL. |
| 2449 | ** |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2450 | ** The value extracted is stored in register P3. |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2451 | ** |
drh | 1cc3a36 | 2017-04-03 13:17:31 +0000 | [diff] [blame] | 2452 | ** If the record contains fewer than P2 fields, then extract a NULL. Or, |
danielk1977 | 1f4aa33 | 2008-01-03 09:51:55 +0000 | [diff] [blame] | 2453 | ** if the P4 argument is a P4_MEM use the value of the P4 argument as |
| 2454 | ** the result. |
drh | 3e9ca09 | 2009-09-08 01:14:48 +0000 | [diff] [blame] | 2455 | ** |
| 2456 | ** If the OPFLAG_CLEARCACHE bit is set on P5 and P1 is a pseudo-table cursor, |
| 2457 | ** then the cache of the cursor is reset prior to extracting the column. |
| 2458 | ** The first OP_Column against a pseudo-table after the value of the content |
| 2459 | ** register has changed should have this bit set. |
drh | a748fdc | 2012-03-28 01:34:47 +0000 | [diff] [blame] | 2460 | ** |
drh | 1cc3a36 | 2017-04-03 13:17:31 +0000 | [diff] [blame] | 2461 | ** If the OPFLAG_LENGTHARG and OPFLAG_TYPEOFARG bits are set on P5 then |
drh | dda5c08 | 2012-03-28 13:41:10 +0000 | [diff] [blame] | 2462 | ** the result is guaranteed to only be used as the argument of a length() |
| 2463 | ** or typeof() function, respectively. The loading of large blobs can be |
| 2464 | ** skipped for length() and all content loading can be skipped for typeof(). |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2465 | */ |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 2466 | case OP_Column: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2467 | int p2; /* column number to retrieve */ |
| 2468 | VdbeCursor *pC; /* The VDBE cursor */ |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 2469 | BtCursor *pCrsr; /* The BTree cursor */ |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 2470 | u32 *aOffset; /* aOffset[i] is offset to start of data for i-th column */ |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 2471 | int len; /* The length of the serialized data for the column */ |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 2472 | int i; /* Loop counter */ |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 2473 | Mem *pDest; /* Where to write the extracted value */ |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 2474 | Mem sMem; /* For storing the record being decoded */ |
drh | 399af1d | 2013-11-20 17:25:55 +0000 | [diff] [blame] | 2475 | const u8 *zData; /* Part of the record being decoded */ |
| 2476 | const u8 *zHdr; /* Next unparsed byte of the header */ |
| 2477 | const u8 *zEndHdr; /* Pointer to first byte after the header */ |
drh | c6ce3883 | 2015-10-15 21:30:24 +0000 | [diff] [blame] | 2478 | u64 offset64; /* 64-bit offset */ |
drh | 5a077b7 | 2011-08-29 02:16:18 +0000 | [diff] [blame] | 2479 | u32 t; /* A type code from the record header */ |
drh | 3e9ca09 | 2009-09-08 01:14:48 +0000 | [diff] [blame] | 2480 | Mem *pReg; /* PseudoTable input register */ |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2481 | |
dan | de892d9 | 2016-01-29 19:29:45 +0000 | [diff] [blame] | 2482 | pC = p->apCsr[pOp->p1]; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2483 | p2 = pOp->p2; |
dan | de892d9 | 2016-01-29 19:29:45 +0000 | [diff] [blame] | 2484 | |
drh | 170ad68 | 2017-06-02 15:44:22 +0000 | [diff] [blame] | 2485 | /* If the cursor cache is stale (meaning it is not currently point at |
| 2486 | ** the correct row) then bring it up-to-date by doing the necessary |
| 2487 | ** B-Tree seek. */ |
dan | de892d9 | 2016-01-29 19:29:45 +0000 | [diff] [blame] | 2488 | rc = sqlite3VdbeCursorMoveto(&pC, &p2); |
drh | 4ca239f | 2016-05-19 11:12:43 +0000 | [diff] [blame] | 2489 | if( rc ) goto abort_due_to_error; |
dan | de892d9 | 2016-01-29 19:29:45 +0000 | [diff] [blame] | 2490 | |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 2491 | assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) ); |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 2492 | pDest = &aMem[pOp->p3]; |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 2493 | memAboutToChange(p, pDest); |
drh | c8606e4 | 2013-11-20 19:28:03 +0000 | [diff] [blame] | 2494 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
danielk1977 | 6c92409 | 2007-11-12 08:09:34 +0000 | [diff] [blame] | 2495 | assert( pC!=0 ); |
drh | c8606e4 | 2013-11-20 19:28:03 +0000 | [diff] [blame] | 2496 | assert( p2<pC->nField ); |
drh | b53a5a9 | 2014-10-12 22:37:22 +0000 | [diff] [blame] | 2497 | aOffset = pC->aOffset; |
drh | 62aaa6c | 2015-11-21 17:27:42 +0000 | [diff] [blame] | 2498 | assert( pC->eCurType!=CURTYPE_VTAB ); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 2499 | assert( pC->eCurType!=CURTYPE_PSEUDO || pC->nullRow ); |
| 2500 | assert( pC->eCurType!=CURTYPE_SORTER ); |
drh | 399af1d | 2013-11-20 17:25:55 +0000 | [diff] [blame] | 2501 | |
drh | a43a02e | 2016-05-19 17:51:19 +0000 | [diff] [blame] | 2502 | if( pC->cacheStatus!=p->cacheCtr ){ /*OPTIMIZATION-IF-FALSE*/ |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2503 | if( pC->nullRow ){ |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 2504 | if( pC->eCurType==CURTYPE_PSEUDO ){ |
drh | fe0cf7a | 2017-08-16 19:20:20 +0000 | [diff] [blame] | 2505 | /* For the special case of as pseudo-cursor, the seekResult field |
| 2506 | ** identifies the register that holds the record */ |
| 2507 | assert( pC->seekResult>0 ); |
| 2508 | pReg = &aMem[pC->seekResult]; |
drh | c8606e4 | 2013-11-20 19:28:03 +0000 | [diff] [blame] | 2509 | assert( pReg->flags & MEM_Blob ); |
| 2510 | assert( memIsValid(pReg) ); |
drh | 6cd8c8c | 2017-08-15 14:14:36 +0000 | [diff] [blame] | 2511 | pC->payloadSize = pC->szRow = pReg->n; |
drh | c8606e4 | 2013-11-20 19:28:03 +0000 | [diff] [blame] | 2512 | pC->aRow = (u8*)pReg->z; |
| 2513 | }else{ |
drh | 6b5631e | 2014-11-05 15:57:39 +0000 | [diff] [blame] | 2514 | sqlite3VdbeMemSetNull(pDest); |
drh | 399af1d | 2013-11-20 17:25:55 +0000 | [diff] [blame] | 2515 | goto op_column_out; |
| 2516 | } |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2517 | }else{ |
drh | 06a09a8 | 2016-11-25 17:03:03 +0000 | [diff] [blame] | 2518 | pCrsr = pC->uc.pCursor; |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 2519 | assert( pC->eCurType==CURTYPE_BTREE ); |
drh | c8606e4 | 2013-11-20 19:28:03 +0000 | [diff] [blame] | 2520 | assert( pCrsr ); |
drh | a7c90c4 | 2016-06-04 20:37:10 +0000 | [diff] [blame] | 2521 | assert( sqlite3BtreeCursorIsValid(pCrsr) ); |
| 2522 | pC->payloadSize = sqlite3BtreePayloadSize(pCrsr); |
drh | 6cd8c8c | 2017-08-15 14:14:36 +0000 | [diff] [blame] | 2523 | pC->aRow = sqlite3BtreePayloadFetch(pCrsr, &pC->szRow); |
| 2524 | assert( pC->szRow<=pC->payloadSize ); |
| 2525 | assert( pC->szRow<=65536 ); /* Maximum page size is 64KiB */ |
| 2526 | if( pC->payloadSize > (u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){ |
drh | 5f7dacb | 2015-11-20 13:33:56 +0000 | [diff] [blame] | 2527 | goto too_big; |
drh | 399af1d | 2013-11-20 17:25:55 +0000 | [diff] [blame] | 2528 | } |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2529 | } |
drh | b73857f | 2006-03-17 00:25:59 +0000 | [diff] [blame] | 2530 | pC->cacheStatus = p->cacheCtr; |
drh | 1f613c4 | 2017-08-16 14:16:19 +0000 | [diff] [blame] | 2531 | pC->iHdrOffset = getVarint32(pC->aRow, aOffset[0]); |
drh | 399af1d | 2013-11-20 17:25:55 +0000 | [diff] [blame] | 2532 | pC->nHdrParsed = 0; |
drh | 35cd643 | 2009-06-05 14:17:21 +0000 | [diff] [blame] | 2533 | |
drh | c81aa2e | 2014-10-11 23:31:52 +0000 | [diff] [blame] | 2534 | |
drh | 1f613c4 | 2017-08-16 14:16:19 +0000 | [diff] [blame] | 2535 | if( pC->szRow<aOffset[0] ){ /*OPTIMIZATION-IF-FALSE*/ |
drh | c81aa2e | 2014-10-11 23:31:52 +0000 | [diff] [blame] | 2536 | /* pC->aRow does not have to hold the entire row, but it does at least |
| 2537 | ** need to cover the header of the record. If pC->aRow does not contain |
| 2538 | ** the complete header, then set it to zero, forcing the header to be |
| 2539 | ** dynamically allocated. */ |
| 2540 | pC->aRow = 0; |
| 2541 | pC->szRow = 0; |
drh | 848a332 | 2015-10-16 12:53:47 +0000 | [diff] [blame] | 2542 | |
| 2543 | /* Make sure a corrupt database has not given us an oversize header. |
| 2544 | ** Do this now to avoid an oversize memory allocation. |
| 2545 | ** |
| 2546 | ** Type entries can be between 1 and 5 bytes each. But 4 and 5 byte |
| 2547 | ** types use so much data space that there can only be 4096 and 32 of |
| 2548 | ** them, respectively. So the maximum header length results from a |
| 2549 | ** 3-byte type for each of the maximum of 32768 columns plus three |
| 2550 | ** extra bytes for the header length itself. 32768*3 + 3 = 98307. |
| 2551 | */ |
drh | 1f613c4 | 2017-08-16 14:16:19 +0000 | [diff] [blame] | 2552 | if( aOffset[0] > 98307 || aOffset[0] > pC->payloadSize ){ |
drh | 74588ce | 2017-09-13 00:13:05 +0000 | [diff] [blame] | 2553 | goto op_column_corrupt; |
drh | 848a332 | 2015-10-16 12:53:47 +0000 | [diff] [blame] | 2554 | } |
drh | 95b225a | 2017-08-16 11:04:22 +0000 | [diff] [blame] | 2555 | }else{ |
| 2556 | /* This is an optimization. By skipping over the first few tests |
| 2557 | ** (ex: pC->nHdrParsed<=p2) in the next section, we achieve a |
| 2558 | ** measurable performance gain. |
| 2559 | ** |
drh | 1f613c4 | 2017-08-16 14:16:19 +0000 | [diff] [blame] | 2560 | ** This branch is taken even if aOffset[0]==0. Such a record is never |
drh | 95b225a | 2017-08-16 11:04:22 +0000 | [diff] [blame] | 2561 | ** generated by SQLite, and could be considered corruption, but we |
drh | 1f613c4 | 2017-08-16 14:16:19 +0000 | [diff] [blame] | 2562 | ** accept it for historical reasons. When aOffset[0]==0, the code this |
drh | 95b225a | 2017-08-16 11:04:22 +0000 | [diff] [blame] | 2563 | ** branch jumps to reads past the end of the record, but never more |
| 2564 | ** than a few bytes. Even if the record occurs at the end of the page |
| 2565 | ** content area, the "page header" comes after the page content and so |
| 2566 | ** this overread is harmless. Similar overreads can occur for a corrupt |
| 2567 | ** database file. |
drh | 0eda6cd | 2016-05-19 16:58:42 +0000 | [diff] [blame] | 2568 | */ |
| 2569 | zData = pC->aRow; |
| 2570 | assert( pC->nHdrParsed<=p2 ); /* Conditional skipped */ |
drh | 1f613c4 | 2017-08-16 14:16:19 +0000 | [diff] [blame] | 2571 | testcase( aOffset[0]==0 ); |
drh | 0eda6cd | 2016-05-19 16:58:42 +0000 | [diff] [blame] | 2572 | goto op_column_read_header; |
drh | c81aa2e | 2014-10-11 23:31:52 +0000 | [diff] [blame] | 2573 | } |
drh | 399af1d | 2013-11-20 17:25:55 +0000 | [diff] [blame] | 2574 | } |
drh | 35cd643 | 2009-06-05 14:17:21 +0000 | [diff] [blame] | 2575 | |
drh | 399af1d | 2013-11-20 17:25:55 +0000 | [diff] [blame] | 2576 | /* Make sure at least the first p2+1 entries of the header have been |
drh | 0c8f760 | 2014-09-19 16:56:45 +0000 | [diff] [blame] | 2577 | ** parsed and valid information is in aOffset[] and pC->aType[]. |
drh | 399af1d | 2013-11-20 17:25:55 +0000 | [diff] [blame] | 2578 | */ |
drh | c8606e4 | 2013-11-20 19:28:03 +0000 | [diff] [blame] | 2579 | if( pC->nHdrParsed<=p2 ){ |
drh | 380d685 | 2013-11-20 20:58:00 +0000 | [diff] [blame] | 2580 | /* If there is more header available for parsing in the record, try |
| 2581 | ** to extract additional fields up through the p2+1-th field |
drh | 35cd643 | 2009-06-05 14:17:21 +0000 | [diff] [blame] | 2582 | */ |
drh | c8606e4 | 2013-11-20 19:28:03 +0000 | [diff] [blame] | 2583 | if( pC->iHdrOffset<aOffset[0] ){ |
| 2584 | /* Make sure zData points to enough of the record to cover the header. */ |
| 2585 | if( pC->aRow==0 ){ |
| 2586 | memset(&sMem, 0, sizeof(sMem)); |
drh | cb3cabd | 2016-11-25 19:18:28 +0000 | [diff] [blame] | 2587 | rc = sqlite3VdbeMemFromBtree(pC->uc.pCursor, 0, aOffset[0], &sMem); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 2588 | if( rc!=SQLITE_OK ) goto abort_due_to_error; |
drh | c8606e4 | 2013-11-20 19:28:03 +0000 | [diff] [blame] | 2589 | zData = (u8*)sMem.z; |
| 2590 | }else{ |
| 2591 | zData = pC->aRow; |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2592 | } |
drh | c8606e4 | 2013-11-20 19:28:03 +0000 | [diff] [blame] | 2593 | |
drh | 0c8f760 | 2014-09-19 16:56:45 +0000 | [diff] [blame] | 2594 | /* Fill in pC->aType[i] and aOffset[i] values through the p2-th field. */ |
drh | 0eda6cd | 2016-05-19 16:58:42 +0000 | [diff] [blame] | 2595 | op_column_read_header: |
drh | c8606e4 | 2013-11-20 19:28:03 +0000 | [diff] [blame] | 2596 | i = pC->nHdrParsed; |
drh | c6ce3883 | 2015-10-15 21:30:24 +0000 | [diff] [blame] | 2597 | offset64 = aOffset[i]; |
drh | c8606e4 | 2013-11-20 19:28:03 +0000 | [diff] [blame] | 2598 | zHdr = zData + pC->iHdrOffset; |
| 2599 | zEndHdr = zData + aOffset[0]; |
drh | 95b225a | 2017-08-16 11:04:22 +0000 | [diff] [blame] | 2600 | testcase( zHdr>=zEndHdr ); |
drh | c8606e4 | 2013-11-20 19:28:03 +0000 | [diff] [blame] | 2601 | do{ |
drh | 95fa606 | 2015-10-16 13:50:08 +0000 | [diff] [blame] | 2602 | if( (t = zHdr[0])<0x80 ){ |
drh | c8606e4 | 2013-11-20 19:28:03 +0000 | [diff] [blame] | 2603 | zHdr++; |
drh | faf3727 | 2015-10-16 14:23:42 +0000 | [diff] [blame] | 2604 | offset64 += sqlite3VdbeOneByteSerialTypeLen(t); |
drh | 5a077b7 | 2011-08-29 02:16:18 +0000 | [diff] [blame] | 2605 | }else{ |
drh | c8606e4 | 2013-11-20 19:28:03 +0000 | [diff] [blame] | 2606 | zHdr += sqlite3GetVarint32(zHdr, &t); |
drh | faf3727 | 2015-10-16 14:23:42 +0000 | [diff] [blame] | 2607 | offset64 += sqlite3VdbeSerialTypeLen(t); |
drh | 5a077b7 | 2011-08-29 02:16:18 +0000 | [diff] [blame] | 2608 | } |
drh | faf3727 | 2015-10-16 14:23:42 +0000 | [diff] [blame] | 2609 | pC->aType[i++] = t; |
drh | c6ce3883 | 2015-10-15 21:30:24 +0000 | [diff] [blame] | 2610 | aOffset[i] = (u32)(offset64 & 0xffffffff); |
drh | c8606e4 | 2013-11-20 19:28:03 +0000 | [diff] [blame] | 2611 | }while( i<=p2 && zHdr<zEndHdr ); |
drh | 170c276 | 2016-05-20 21:40:11 +0000 | [diff] [blame] | 2612 | |
drh | 8dd8362 | 2014-10-13 23:39:02 +0000 | [diff] [blame] | 2613 | /* The record is corrupt if any of the following are true: |
| 2614 | ** (1) the bytes of the header extend past the declared header size |
drh | 8dd8362 | 2014-10-13 23:39:02 +0000 | [diff] [blame] | 2615 | ** (2) the entire header was used but not all data was used |
drh | 8dd8362 | 2014-10-13 23:39:02 +0000 | [diff] [blame] | 2616 | ** (3) the end of the data extends beyond the end of the record. |
drh | c8606e4 | 2013-11-20 19:28:03 +0000 | [diff] [blame] | 2617 | */ |
drh | c6ce3883 | 2015-10-15 21:30:24 +0000 | [diff] [blame] | 2618 | if( (zHdr>=zEndHdr && (zHdr>zEndHdr || offset64!=pC->payloadSize)) |
| 2619 | || (offset64 > pC->payloadSize) |
drh | c8606e4 | 2013-11-20 19:28:03 +0000 | [diff] [blame] | 2620 | ){ |
drh | 95b225a | 2017-08-16 11:04:22 +0000 | [diff] [blame] | 2621 | if( aOffset[0]==0 ){ |
| 2622 | i = 0; |
| 2623 | zHdr = zEndHdr; |
| 2624 | }else{ |
| 2625 | if( pC->aRow==0 ) sqlite3VdbeMemRelease(&sMem); |
drh | 74588ce | 2017-09-13 00:13:05 +0000 | [diff] [blame] | 2626 | goto op_column_corrupt; |
drh | 95b225a | 2017-08-16 11:04:22 +0000 | [diff] [blame] | 2627 | } |
danielk1977 | dedf45b | 2006-01-13 17:12:01 +0000 | [diff] [blame] | 2628 | } |
drh | ddb2b4a | 2016-03-25 12:10:32 +0000 | [diff] [blame] | 2629 | |
drh | 170c276 | 2016-05-20 21:40:11 +0000 | [diff] [blame] | 2630 | pC->nHdrParsed = i; |
| 2631 | pC->iHdrOffset = (u32)(zHdr - zData); |
| 2632 | if( pC->aRow==0 ) sqlite3VdbeMemRelease(&sMem); |
mistachkin | 8c7cd6a | 2015-12-16 21:09:53 +0000 | [diff] [blame] | 2633 | }else{ |
drh | 9fbc885 | 2016-01-04 03:48:46 +0000 | [diff] [blame] | 2634 | t = 0; |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2635 | } |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 2636 | |
drh | f2db338 | 2015-04-30 20:33:25 +0000 | [diff] [blame] | 2637 | /* If after trying to extract new entries from the header, nHdrParsed is |
drh | 380d685 | 2013-11-20 20:58:00 +0000 | [diff] [blame] | 2638 | ** still not up to p2, that means that the record has fewer than p2 |
| 2639 | ** columns. So the result will be either the default value or a NULL. |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 2640 | */ |
drh | c8606e4 | 2013-11-20 19:28:03 +0000 | [diff] [blame] | 2641 | if( pC->nHdrParsed<=p2 ){ |
| 2642 | if( pOp->p4type==P4_MEM ){ |
| 2643 | sqlite3VdbeMemShallowCopy(pDest, pOp->p4.pMem, MEM_Static); |
| 2644 | }else{ |
drh | 22e8d83 | 2014-10-29 00:58:38 +0000 | [diff] [blame] | 2645 | sqlite3VdbeMemSetNull(pDest); |
drh | c8606e4 | 2013-11-20 19:28:03 +0000 | [diff] [blame] | 2646 | } |
danielk1977 | 3c9cc8d | 2005-01-17 03:40:08 +0000 | [diff] [blame] | 2647 | goto op_column_out; |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 2648 | } |
drh | 95fa606 | 2015-10-16 13:50:08 +0000 | [diff] [blame] | 2649 | }else{ |
| 2650 | t = pC->aType[p2]; |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 2651 | } |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2652 | |
drh | 380d685 | 2013-11-20 20:58:00 +0000 | [diff] [blame] | 2653 | /* Extract the content for the p2+1-th column. Control can only |
drh | 0c8f760 | 2014-09-19 16:56:45 +0000 | [diff] [blame] | 2654 | ** reach this point if aOffset[p2], aOffset[p2+1], and pC->aType[p2] are |
drh | 380d685 | 2013-11-20 20:58:00 +0000 | [diff] [blame] | 2655 | ** all valid. |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2656 | */ |
drh | c8606e4 | 2013-11-20 19:28:03 +0000 | [diff] [blame] | 2657 | assert( p2<pC->nHdrParsed ); |
| 2658 | assert( rc==SQLITE_OK ); |
drh | 75fd054 | 2014-03-01 16:24:44 +0000 | [diff] [blame] | 2659 | assert( sqlite3VdbeCheckMemInvariants(pDest) ); |
drh | a1851ef | 2016-05-20 19:51:28 +0000 | [diff] [blame] | 2660 | if( VdbeMemDynamic(pDest) ){ |
| 2661 | sqlite3VdbeMemSetNull(pDest); |
| 2662 | } |
drh | 95fa606 | 2015-10-16 13:50:08 +0000 | [diff] [blame] | 2663 | assert( t==pC->aType[p2] ); |
drh | c8606e4 | 2013-11-20 19:28:03 +0000 | [diff] [blame] | 2664 | if( pC->szRow>=aOffset[p2+1] ){ |
drh | 380d685 | 2013-11-20 20:58:00 +0000 | [diff] [blame] | 2665 | /* This is the common case where the desired content fits on the original |
| 2666 | ** page - where the content is not on an overflow page */ |
drh | 69f6e25 | 2016-01-11 18:05:00 +0000 | [diff] [blame] | 2667 | zData = pC->aRow + aOffset[p2]; |
| 2668 | if( t<12 ){ |
| 2669 | sqlite3VdbeSerialGet(zData, t, pDest); |
| 2670 | }else{ |
| 2671 | /* If the column value is a string, we need a persistent value, not |
| 2672 | ** a MEM_Ephem value. This branch is a fast short-cut that is equivalent |
| 2673 | ** to calling sqlite3VdbeSerialGet() and sqlite3VdbeDeephemeralize(). |
| 2674 | */ |
| 2675 | static const u16 aFlag[] = { MEM_Blob, MEM_Str|MEM_Term }; |
| 2676 | pDest->n = len = (t-12)/2; |
drh | a1851ef | 2016-05-20 19:51:28 +0000 | [diff] [blame] | 2677 | pDest->enc = encoding; |
drh | 69f6e25 | 2016-01-11 18:05:00 +0000 | [diff] [blame] | 2678 | if( pDest->szMalloc < len+2 ){ |
| 2679 | pDest->flags = MEM_Null; |
| 2680 | if( sqlite3VdbeMemGrow(pDest, len+2, 0) ) goto no_mem; |
| 2681 | }else{ |
| 2682 | pDest->z = pDest->zMalloc; |
| 2683 | } |
| 2684 | memcpy(pDest->z, zData, len); |
| 2685 | pDest->z[len] = 0; |
| 2686 | pDest->z[len+1] = 0; |
| 2687 | pDest->flags = aFlag[t&1]; |
| 2688 | } |
danielk1977 | 36963fd | 2005-02-19 08:18:05 +0000 | [diff] [blame] | 2689 | }else{ |
drh | a1851ef | 2016-05-20 19:51:28 +0000 | [diff] [blame] | 2690 | pDest->enc = encoding; |
drh | 58c9608 | 2013-12-23 11:33:32 +0000 | [diff] [blame] | 2691 | /* This branch happens only when content is on overflow pages */ |
drh | 380d685 | 2013-11-20 20:58:00 +0000 | [diff] [blame] | 2692 | if( ((pOp->p5 & (OPFLAG_LENGTHARG|OPFLAG_TYPEOFARG))!=0 |
| 2693 | && ((t>=12 && (t&1)==0) || (pOp->p5 & OPFLAG_TYPEOFARG)!=0)) |
| 2694 | || (len = sqlite3VdbeSerialTypeLen(t))==0 |
drh | c8606e4 | 2013-11-20 19:28:03 +0000 | [diff] [blame] | 2695 | ){ |
drh | 2a2a696 | 2014-09-16 18:22:44 +0000 | [diff] [blame] | 2696 | /* Content is irrelevant for |
| 2697 | ** 1. the typeof() function, |
| 2698 | ** 2. the length(X) function if X is a blob, and |
| 2699 | ** 3. if the content length is zero. |
| 2700 | ** So we might as well use bogus content rather than reading |
dan | 1f9144e | 2017-03-17 13:59:06 +0000 | [diff] [blame] | 2701 | ** content from disk. |
| 2702 | ** |
| 2703 | ** Although sqlite3VdbeSerialGet() may read at most 8 bytes from the |
| 2704 | ** buffer passed to it, debugging function VdbeMemPrettyPrint() may |
| 2705 | ** read up to 16. So 16 bytes of bogus content is supplied. |
| 2706 | */ |
| 2707 | static u8 aZero[16]; /* This is the bogus content */ |
drh | 69f6e25 | 2016-01-11 18:05:00 +0000 | [diff] [blame] | 2708 | sqlite3VdbeSerialGet(aZero, t, pDest); |
danielk1977 | aee18ef | 2005-03-09 12:26:50 +0000 | [diff] [blame] | 2709 | }else{ |
drh | cb3cabd | 2016-11-25 19:18:28 +0000 | [diff] [blame] | 2710 | rc = sqlite3VdbeMemFromBtree(pC->uc.pCursor, aOffset[p2], len, pDest); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 2711 | if( rc!=SQLITE_OK ) goto abort_due_to_error; |
| 2712 | sqlite3VdbeSerialGet((const u8*)pDest->z, t, pDest); |
| 2713 | pDest->flags &= ~MEM_Ephem; |
danielk1977 | aee18ef | 2005-03-09 12:26:50 +0000 | [diff] [blame] | 2714 | } |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 2715 | } |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 2716 | |
danielk1977 | 3c9cc8d | 2005-01-17 03:40:08 +0000 | [diff] [blame] | 2717 | op_column_out: |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 2718 | UPDATE_MAX_BLOBSIZE(pDest); |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 2719 | REGISTER_TRACE(pOp->p3, pDest); |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2720 | break; |
drh | 74588ce | 2017-09-13 00:13:05 +0000 | [diff] [blame] | 2721 | |
| 2722 | op_column_corrupt: |
| 2723 | if( aOp[0].p3>0 ){ |
| 2724 | pOp = &aOp[aOp[0].p3-1]; |
| 2725 | break; |
| 2726 | }else{ |
| 2727 | rc = SQLITE_CORRUPT_BKPT; |
| 2728 | goto abort_due_to_error; |
| 2729 | } |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2730 | } |
| 2731 | |
danielk1977 | 751de56 | 2008-04-18 09:01:15 +0000 | [diff] [blame] | 2732 | /* Opcode: Affinity P1 P2 * P4 * |
drh | f63552b | 2013-10-30 00:25:03 +0000 | [diff] [blame] | 2733 | ** Synopsis: affinity(r[P1@P2]) |
danielk1977 | 751de56 | 2008-04-18 09:01:15 +0000 | [diff] [blame] | 2734 | ** |
| 2735 | ** Apply affinities to a range of P2 registers starting with P1. |
| 2736 | ** |
drh | bb6783b | 2017-04-29 18:02:49 +0000 | [diff] [blame] | 2737 | ** P4 is a string that is P2 characters long. The N-th character of the |
| 2738 | ** string indicates the column affinity that should be used for the N-th |
danielk1977 | 751de56 | 2008-04-18 09:01:15 +0000 | [diff] [blame] | 2739 | ** memory cell in the range. |
| 2740 | */ |
| 2741 | case OP_Affinity: { |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 2742 | const char *zAffinity; /* The affinity to be applied */ |
danielk1977 | 751de56 | 2008-04-18 09:01:15 +0000 | [diff] [blame] | 2743 | |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2744 | zAffinity = pOp->p4.z; |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 2745 | assert( zAffinity!=0 ); |
drh | 662c50e | 2017-04-01 20:14:01 +0000 | [diff] [blame] | 2746 | assert( pOp->p2>0 ); |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 2747 | assert( zAffinity[pOp->p2]==0 ); |
| 2748 | pIn1 = &aMem[pOp->p1]; |
drh | 662c50e | 2017-04-01 20:14:01 +0000 | [diff] [blame] | 2749 | do{ |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 2750 | assert( pIn1 <= &p->aMem[(p->nMem+1 - p->nCursor)] ); |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 2751 | assert( memIsValid(pIn1) ); |
drh | 662c50e | 2017-04-01 20:14:01 +0000 | [diff] [blame] | 2752 | applyAffinity(pIn1, *(zAffinity++), encoding); |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 2753 | pIn1++; |
drh | 662c50e | 2017-04-01 20:14:01 +0000 | [diff] [blame] | 2754 | }while( zAffinity[0] ); |
danielk1977 | 751de56 | 2008-04-18 09:01:15 +0000 | [diff] [blame] | 2755 | break; |
| 2756 | } |
| 2757 | |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2758 | /* Opcode: MakeRecord P1 P2 P3 P4 * |
drh | f63552b | 2013-10-30 00:25:03 +0000 | [diff] [blame] | 2759 | ** Synopsis: r[P3]=mkrec(r[P1@P2]) |
drh | 7a224de | 2004-06-02 01:22:02 +0000 | [diff] [blame] | 2760 | ** |
drh | 710c484 | 2010-08-30 01:17:20 +0000 | [diff] [blame] | 2761 | ** Convert P2 registers beginning with P1 into the [record format] |
| 2762 | ** use as a data record in a database table or as a key |
| 2763 | ** in an index. The OP_Column opcode can decode the record later. |
drh | 7a224de | 2004-06-02 01:22:02 +0000 | [diff] [blame] | 2764 | ** |
drh | bb6783b | 2017-04-29 18:02:49 +0000 | [diff] [blame] | 2765 | ** P4 may be a string that is P2 characters long. The N-th character of the |
| 2766 | ** string indicates the column affinity that should be used for the N-th |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2767 | ** field of the index key. |
drh | 7a224de | 2004-06-02 01:22:02 +0000 | [diff] [blame] | 2768 | ** |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 2769 | ** The mapping from character to affinity is given by the SQLITE_AFF_ |
| 2770 | ** macros defined in sqliteInt.h. |
drh | 7a224de | 2004-06-02 01:22:02 +0000 | [diff] [blame] | 2771 | ** |
drh | 05883a3 | 2015-06-02 15:32:08 +0000 | [diff] [blame] | 2772 | ** If P4 is NULL then all index fields have the affinity BLOB. |
drh | 7f057c9 | 2005-06-24 03:53:06 +0000 | [diff] [blame] | 2773 | */ |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2774 | case OP_MakeRecord: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2775 | u8 *zNewRecord; /* A buffer to hold the data for the new record */ |
| 2776 | Mem *pRec; /* The new record */ |
| 2777 | u64 nData; /* Number of bytes of data space */ |
| 2778 | int nHdr; /* Number of bytes of header space */ |
| 2779 | i64 nByte; /* Data space required for this record */ |
drh | 4a33507 | 2015-04-11 02:08:48 +0000 | [diff] [blame] | 2780 | i64 nZero; /* Number of zero bytes at the end of the record */ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2781 | int nVarint; /* Number of bytes in a varint */ |
| 2782 | u32 serial_type; /* Type field */ |
| 2783 | Mem *pData0; /* First field to be combined into the record */ |
| 2784 | Mem *pLast; /* Last field of the record */ |
| 2785 | int nField; /* Number of fields in the record */ |
| 2786 | char *zAffinity; /* The affinity string for the record */ |
| 2787 | int file_format; /* File format to use for encoding */ |
drh | 59bf00c | 2013-12-08 23:33:28 +0000 | [diff] [blame] | 2788 | int i; /* Space used in zNewRecord[] header */ |
| 2789 | int j; /* Space used in zNewRecord[] content */ |
drh | be37c12 | 2015-10-16 14:54:17 +0000 | [diff] [blame] | 2790 | u32 len; /* Length of a field */ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2791 | |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 2792 | /* Assuming the record contains N fields, the record format looks |
| 2793 | ** like this: |
| 2794 | ** |
drh | 7a224de | 2004-06-02 01:22:02 +0000 | [diff] [blame] | 2795 | ** ------------------------------------------------------------------------ |
| 2796 | ** | hdr-size | type 0 | type 1 | ... | type N-1 | data0 | ... | data N-1 | |
| 2797 | ** ------------------------------------------------------------------------ |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 2798 | ** |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2799 | ** Data(0) is taken from register P1. Data(1) comes from register P1+1 |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 2800 | ** and so forth. |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 2801 | ** |
| 2802 | ** Each type field is a varint representing the serial type of the |
| 2803 | ** corresponding data element (see sqlite3VdbeSerialType()). The |
drh | 7a224de | 2004-06-02 01:22:02 +0000 | [diff] [blame] | 2804 | ** hdr-size field is also a varint which is the offset from the beginning |
| 2805 | ** of the record to data0. |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 2806 | */ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2807 | nData = 0; /* Number of bytes of data space */ |
| 2808 | nHdr = 0; /* Number of bytes of header space */ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2809 | nZero = 0; /* Number of zero bytes at the end of the record */ |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2810 | nField = pOp->p1; |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 2811 | zAffinity = pOp->p4.z; |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 2812 | assert( nField>0 && pOp->p2>0 && pOp->p2+nField<=(p->nMem+1 - p->nCursor)+1 ); |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 2813 | pData0 = &aMem[nField]; |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2814 | nField = pOp->p2; |
| 2815 | pLast = &pData0[nField-1]; |
drh | d946db0 | 2005-12-29 19:23:06 +0000 | [diff] [blame] | 2816 | file_format = p->minWriteFileFormat; |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 2817 | |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 2818 | /* Identify the output register */ |
| 2819 | assert( pOp->p3<pOp->p1 || pOp->p3>=pOp->p1+pOp->p2 ); |
| 2820 | pOut = &aMem[pOp->p3]; |
| 2821 | memAboutToChange(p, pOut); |
| 2822 | |
drh | 3e6c060 | 2013-12-10 20:53:01 +0000 | [diff] [blame] | 2823 | /* Apply the requested affinity to all inputs |
| 2824 | */ |
| 2825 | assert( pData0<=pLast ); |
| 2826 | if( zAffinity ){ |
| 2827 | pRec = pData0; |
| 2828 | do{ |
drh | 57bf4a8 | 2014-02-17 14:59:22 +0000 | [diff] [blame] | 2829 | applyAffinity(pRec++, *(zAffinity++), encoding); |
| 2830 | assert( zAffinity[0]==0 || pRec<=pLast ); |
| 2831 | }while( zAffinity[0] ); |
drh | 3e6c060 | 2013-12-10 20:53:01 +0000 | [diff] [blame] | 2832 | } |
| 2833 | |
drh | d447dce | 2017-01-25 20:55:11 +0000 | [diff] [blame] | 2834 | #ifdef SQLITE_ENABLE_NULL_TRIM |
drh | 585ce19 | 2017-01-25 14:58:27 +0000 | [diff] [blame] | 2835 | /* NULLs can be safely trimmed from the end of the record, as long as |
| 2836 | ** as the schema format is 2 or more and none of the omitted columns |
| 2837 | ** have a non-NULL default value. Also, the record must be left with |
| 2838 | ** at least one field. If P5>0 then it will be one more than the |
| 2839 | ** index of the right-most column with a non-NULL default value */ |
| 2840 | if( pOp->p5 ){ |
| 2841 | while( (pLast->flags & MEM_Null)!=0 && nField>pOp->p5 ){ |
| 2842 | pLast--; |
| 2843 | nField--; |
| 2844 | } |
| 2845 | } |
drh | d447dce | 2017-01-25 20:55:11 +0000 | [diff] [blame] | 2846 | #endif |
drh | 585ce19 | 2017-01-25 14:58:27 +0000 | [diff] [blame] | 2847 | |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 2848 | /* Loop through the elements that will make up the record to figure |
| 2849 | ** out how much space is required for the new record. |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 2850 | */ |
drh | 038b7bc | 2013-12-09 23:17:22 +0000 | [diff] [blame] | 2851 | pRec = pLast; |
drh | 59bf00c | 2013-12-08 23:33:28 +0000 | [diff] [blame] | 2852 | do{ |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 2853 | assert( memIsValid(pRec) ); |
drh | 41fb367 | 2018-01-12 23:18:38 +0000 | [diff] [blame] | 2854 | serial_type = sqlite3VdbeSerialType(pRec, file_format, &len); |
drh | fdf972a | 2007-05-02 13:30:27 +0000 | [diff] [blame] | 2855 | if( pRec->flags & MEM_Zero ){ |
drh | ce2fbd1 | 2018-01-12 21:00:14 +0000 | [diff] [blame] | 2856 | if( serial_type==0 ){ |
drh | 41fb367 | 2018-01-12 23:18:38 +0000 | [diff] [blame] | 2857 | /* Values with MEM_Null and MEM_Zero are created by xColumn virtual |
| 2858 | ** table methods that never invoke sqlite3_result_xxxxx() while |
| 2859 | ** computing an unchanging column value in an UPDATE statement. |
| 2860 | ** Give such values a special internal-use-only serial-type of 10 |
| 2861 | ** so that they can be passed through to xUpdate and have |
| 2862 | ** a true sqlite3_value_nochange(). */ |
| 2863 | assert( pOp->p5==OPFLAG_NOCHNG_MAGIC || CORRUPT_DB ); |
| 2864 | serial_type = 10; |
drh | ce2fbd1 | 2018-01-12 21:00:14 +0000 | [diff] [blame] | 2865 | }else if( nData ){ |
drh | 53e66c3 | 2015-07-24 15:49:23 +0000 | [diff] [blame] | 2866 | if( sqlite3VdbeMemExpandBlob(pRec) ) goto no_mem; |
drh | 038b7bc | 2013-12-09 23:17:22 +0000 | [diff] [blame] | 2867 | }else{ |
| 2868 | nZero += pRec->u.nZero; |
| 2869 | len -= pRec->u.nZero; |
| 2870 | } |
drh | fdf972a | 2007-05-02 13:30:27 +0000 | [diff] [blame] | 2871 | } |
drh | 8079a0d | 2006-01-12 17:20:50 +0000 | [diff] [blame] | 2872 | nData += len; |
drh | 59bf00c | 2013-12-08 23:33:28 +0000 | [diff] [blame] | 2873 | testcase( serial_type==127 ); |
| 2874 | testcase( serial_type==128 ); |
drh | 2a24287 | 2013-12-08 22:59:29 +0000 | [diff] [blame] | 2875 | nHdr += serial_type<=127 ? 1 : sqlite3VarintLen(serial_type); |
drh | 41fb367 | 2018-01-12 23:18:38 +0000 | [diff] [blame] | 2876 | pRec->uTemp = serial_type; |
drh | 45c3c66 | 2016-04-07 14:16:16 +0000 | [diff] [blame] | 2877 | if( pRec==pData0 ) break; |
| 2878 | pRec--; |
| 2879 | }while(1); |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 2880 | |
drh | 654858d | 2014-11-20 02:18:14 +0000 | [diff] [blame] | 2881 | /* EVIDENCE-OF: R-22564-11647 The header begins with a single varint |
| 2882 | ** which determines the total number of bytes in the header. The varint |
| 2883 | ** value is the size of the header in bytes including the size varint |
| 2884 | ** itself. */ |
drh | 59bf00c | 2013-12-08 23:33:28 +0000 | [diff] [blame] | 2885 | testcase( nHdr==126 ); |
| 2886 | testcase( nHdr==127 ); |
drh | 2a24287 | 2013-12-08 22:59:29 +0000 | [diff] [blame] | 2887 | if( nHdr<=126 ){ |
| 2888 | /* The common case */ |
| 2889 | nHdr += 1; |
| 2890 | }else{ |
| 2891 | /* Rare case of a really large header */ |
| 2892 | nVarint = sqlite3VarintLen(nHdr); |
| 2893 | nHdr += nVarint; |
| 2894 | if( nVarint<sqlite3VarintLen(nHdr) ) nHdr++; |
drh | cb9882a | 2005-03-17 03:15:40 +0000 | [diff] [blame] | 2895 | } |
drh | 038b7bc | 2013-12-09 23:17:22 +0000 | [diff] [blame] | 2896 | nByte = nHdr+nData; |
drh | 4a33507 | 2015-04-11 02:08:48 +0000 | [diff] [blame] | 2897 | if( nByte+nZero>db->aLimit[SQLITE_LIMIT_LENGTH] ){ |
drh | 023ae03 | 2007-05-08 12:12:16 +0000 | [diff] [blame] | 2898 | goto too_big; |
| 2899 | } |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 2900 | |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 2901 | /* Make sure the output register has a buffer large enough to store |
| 2902 | ** the new record. The output register (pOp->p3) is not allowed to |
| 2903 | ** be one of the input registers (because the following call to |
drh | 322f285 | 2014-09-19 00:43:39 +0000 | [diff] [blame] | 2904 | ** sqlite3VdbeMemClearAndResize() could clobber the value before it is used). |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 2905 | */ |
drh | 322f285 | 2014-09-19 00:43:39 +0000 | [diff] [blame] | 2906 | if( sqlite3VdbeMemClearAndResize(pOut, (int)nByte) ){ |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 2907 | goto no_mem; |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 2908 | } |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 2909 | zNewRecord = (u8 *)pOut->z; |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 2910 | |
| 2911 | /* Write the record */ |
shane | 3f8d5cf | 2008-04-24 19:15:09 +0000 | [diff] [blame] | 2912 | i = putVarint32(zNewRecord, nHdr); |
drh | 59bf00c | 2013-12-08 23:33:28 +0000 | [diff] [blame] | 2913 | j = nHdr; |
| 2914 | assert( pData0<=pLast ); |
| 2915 | pRec = pData0; |
| 2916 | do{ |
drh | facf47a | 2014-10-13 20:12:47 +0000 | [diff] [blame] | 2917 | serial_type = pRec->uTemp; |
drh | 654858d | 2014-11-20 02:18:14 +0000 | [diff] [blame] | 2918 | /* EVIDENCE-OF: R-06529-47362 Following the size varint are one or more |
| 2919 | ** additional varints, one per column. */ |
drh | 038b7bc | 2013-12-09 23:17:22 +0000 | [diff] [blame] | 2920 | i += putVarint32(&zNewRecord[i], serial_type); /* serial type */ |
drh | 654858d | 2014-11-20 02:18:14 +0000 | [diff] [blame] | 2921 | /* EVIDENCE-OF: R-64536-51728 The values for each column in the record |
| 2922 | ** immediately follow the header. */ |
drh | a9ab481 | 2013-12-11 11:00:44 +0000 | [diff] [blame] | 2923 | j += sqlite3VdbeSerialPut(&zNewRecord[j], pRec, serial_type); /* content */ |
drh | 59bf00c | 2013-12-08 23:33:28 +0000 | [diff] [blame] | 2924 | }while( (++pRec)<=pLast ); |
| 2925 | assert( i==nHdr ); |
| 2926 | assert( j==nByte ); |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 2927 | |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 2928 | assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) ); |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 2929 | pOut->n = (int)nByte; |
drh | c91b2fd | 2014-03-01 18:13:23 +0000 | [diff] [blame] | 2930 | pOut->flags = MEM_Blob; |
drh | fdf972a | 2007-05-02 13:30:27 +0000 | [diff] [blame] | 2931 | if( nZero ){ |
drh | 8df3284 | 2008-12-09 02:51:23 +0000 | [diff] [blame] | 2932 | pOut->u.nZero = nZero; |
drh | 477df4b | 2008-01-05 18:48:24 +0000 | [diff] [blame] | 2933 | pOut->flags |= MEM_Zero; |
drh | fdf972a | 2007-05-02 13:30:27 +0000 | [diff] [blame] | 2934 | } |
drh | 1013c93 | 2008-01-06 00:25:21 +0000 | [diff] [blame] | 2935 | REGISTER_TRACE(pOp->p3, pOut); |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 2936 | UPDATE_MAX_BLOBSIZE(pOut); |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 2937 | break; |
| 2938 | } |
| 2939 | |
danielk1977 | a553316 | 2009-02-24 10:01:51 +0000 | [diff] [blame] | 2940 | /* Opcode: Count P1 P2 * * * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 2941 | ** Synopsis: r[P2]=count() |
danielk1977 | a553316 | 2009-02-24 10:01:51 +0000 | [diff] [blame] | 2942 | ** |
| 2943 | ** Store the number of entries (an integer value) in the table or index |
| 2944 | ** opened by cursor P1 in register P2 |
| 2945 | */ |
| 2946 | #ifndef SQLITE_OMIT_BTREECOUNT |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 2947 | case OP_Count: { /* out2 */ |
danielk1977 | a553316 | 2009-02-24 10:01:51 +0000 | [diff] [blame] | 2948 | i64 nEntry; |
drh | c54a617 | 2009-06-02 16:06:03 +0000 | [diff] [blame] | 2949 | BtCursor *pCrsr; |
| 2950 | |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 2951 | assert( p->apCsr[pOp->p1]->eCurType==CURTYPE_BTREE ); |
| 2952 | pCrsr = p->apCsr[pOp->p1]->uc.pCursor; |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 2953 | assert( pCrsr ); |
drh | 2dc0648 | 2013-12-11 00:59:10 +0000 | [diff] [blame] | 2954 | nEntry = 0; /* Not needed. Only used to silence a warning. */ |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 2955 | rc = sqlite3BtreeCount(pCrsr, &nEntry); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 2956 | if( rc ) goto abort_due_to_error; |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 2957 | pOut = out2Prerelease(p, pOp); |
danielk1977 | a553316 | 2009-02-24 10:01:51 +0000 | [diff] [blame] | 2958 | pOut->u.i = nEntry; |
| 2959 | break; |
| 2960 | } |
| 2961 | #endif |
| 2962 | |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2963 | /* Opcode: Savepoint P1 * * P4 * |
| 2964 | ** |
| 2965 | ** Open, release or rollback the savepoint named by parameter P4, depending |
| 2966 | ** on the value of P1. To open a new savepoint, P1==0. To release (commit) an |
| 2967 | ** existing savepoint, P1==1, or to rollback an existing savepoint P1==2. |
| 2968 | */ |
| 2969 | case OP_Savepoint: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2970 | int p1; /* Value of P1 operand */ |
| 2971 | char *zName; /* Name of savepoint */ |
| 2972 | int nName; |
| 2973 | Savepoint *pNew; |
| 2974 | Savepoint *pSavepoint; |
| 2975 | Savepoint *pTmp; |
| 2976 | int iSavepoint; |
| 2977 | int ii; |
| 2978 | |
| 2979 | p1 = pOp->p1; |
| 2980 | zName = pOp->p4.z; |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2981 | |
| 2982 | /* Assert that the p1 parameter is valid. Also that if there is no open |
| 2983 | ** transaction, then there cannot be any savepoints. |
| 2984 | */ |
| 2985 | assert( db->pSavepoint==0 || db->autoCommit==0 ); |
| 2986 | assert( p1==SAVEPOINT_BEGIN||p1==SAVEPOINT_RELEASE||p1==SAVEPOINT_ROLLBACK ); |
| 2987 | assert( db->pSavepoint || db->isTransactionSavepoint==0 ); |
| 2988 | assert( checkSavepointCount(db) ); |
dan | c0537fe | 2013-06-28 19:41:43 +0000 | [diff] [blame] | 2989 | assert( p->bIsReader ); |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2990 | |
| 2991 | if( p1==SAVEPOINT_BEGIN ){ |
drh | 4f7d3a5 | 2013-06-27 23:54:02 +0000 | [diff] [blame] | 2992 | if( db->nVdbeWrite>0 ){ |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2993 | /* A new savepoint cannot be created if there are active write |
| 2994 | ** statements (i.e. open read/write incremental blob handles). |
| 2995 | */ |
drh | 22c17b8 | 2015-05-15 04:13:15 +0000 | [diff] [blame] | 2996 | sqlite3VdbeError(p, "cannot open savepoint - SQL statements in progress"); |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2997 | rc = SQLITE_BUSY; |
| 2998 | }else{ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2999 | nName = sqlite3Strlen30(zName); |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3000 | |
drh | be07ec5 | 2011-06-03 12:15:26 +0000 | [diff] [blame] | 3001 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
dan | d9495cd | 2011-04-27 12:08:04 +0000 | [diff] [blame] | 3002 | /* This call is Ok even if this savepoint is actually a transaction |
| 3003 | ** savepoint (and therefore should not prompt xSavepoint()) callbacks. |
| 3004 | ** If this is a transaction savepoint being opened, it is guaranteed |
| 3005 | ** that the db->aVTrans[] array is empty. */ |
| 3006 | assert( db->autoCommit==0 || db->nVTrans==0 ); |
drh | a24bc9c | 2011-05-24 00:35:56 +0000 | [diff] [blame] | 3007 | rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN, |
| 3008 | db->nStatement+db->nSavepoint); |
dan | d9495cd | 2011-04-27 12:08:04 +0000 | [diff] [blame] | 3009 | if( rc!=SQLITE_OK ) goto abort_due_to_error; |
drh | 305ebab | 2011-05-26 14:19:14 +0000 | [diff] [blame] | 3010 | #endif |
dan | d9495cd | 2011-04-27 12:08:04 +0000 | [diff] [blame] | 3011 | |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3012 | /* Create a new savepoint structure. */ |
drh | 575fad6 | 2016-02-05 13:38:36 +0000 | [diff] [blame] | 3013 | pNew = sqlite3DbMallocRawNN(db, sizeof(Savepoint)+nName+1); |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3014 | if( pNew ){ |
| 3015 | pNew->zName = (char *)&pNew[1]; |
| 3016 | memcpy(pNew->zName, zName, nName+1); |
| 3017 | |
| 3018 | /* If there is no open transaction, then mark this as a special |
| 3019 | ** "transaction savepoint". */ |
| 3020 | if( db->autoCommit ){ |
| 3021 | db->autoCommit = 0; |
| 3022 | db->isTransactionSavepoint = 1; |
| 3023 | }else{ |
| 3024 | db->nSavepoint++; |
danielk1977 | d829335 | 2009-04-30 09:10:37 +0000 | [diff] [blame] | 3025 | } |
dan | 21e8d01 | 2011-03-03 20:05:59 +0000 | [diff] [blame] | 3026 | |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3027 | /* Link the new savepoint into the database handle's list. */ |
| 3028 | pNew->pNext = db->pSavepoint; |
| 3029 | db->pSavepoint = pNew; |
dan | ba9108b | 2009-09-22 07:13:42 +0000 | [diff] [blame] | 3030 | pNew->nDeferredCons = db->nDeferredCons; |
dan | cb3e4b7 | 2013-07-03 19:53:05 +0000 | [diff] [blame] | 3031 | pNew->nDeferredImmCons = db->nDeferredImmCons; |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3032 | } |
| 3033 | } |
| 3034 | }else{ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3035 | iSavepoint = 0; |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3036 | |
| 3037 | /* Find the named savepoint. If there is no such savepoint, then an |
| 3038 | ** an error is returned to the user. */ |
| 3039 | for( |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3040 | pSavepoint = db->pSavepoint; |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3041 | pSavepoint && sqlite3StrICmp(pSavepoint->zName, zName); |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3042 | pSavepoint = pSavepoint->pNext |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3043 | ){ |
| 3044 | iSavepoint++; |
| 3045 | } |
| 3046 | if( !pSavepoint ){ |
drh | 22c17b8 | 2015-05-15 04:13:15 +0000 | [diff] [blame] | 3047 | sqlite3VdbeError(p, "no such savepoint: %s", zName); |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3048 | rc = SQLITE_ERROR; |
drh | 4f7d3a5 | 2013-06-27 23:54:02 +0000 | [diff] [blame] | 3049 | }else if( db->nVdbeWrite>0 && p1==SAVEPOINT_RELEASE ){ |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3050 | /* It is not possible to release (commit) a savepoint if there are |
drh | 0f198a7 | 2012-02-13 16:43:16 +0000 | [diff] [blame] | 3051 | ** active write statements. |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3052 | */ |
drh | 22c17b8 | 2015-05-15 04:13:15 +0000 | [diff] [blame] | 3053 | sqlite3VdbeError(p, "cannot release savepoint - " |
| 3054 | "SQL statements in progress"); |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3055 | rc = SQLITE_BUSY; |
| 3056 | }else{ |
| 3057 | |
| 3058 | /* Determine whether or not this is a transaction savepoint. If so, |
danielk1977 | 34cf35d | 2008-12-18 18:31:38 +0000 | [diff] [blame] | 3059 | ** and this is a RELEASE command, then the current transaction |
| 3060 | ** is committed. |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3061 | */ |
| 3062 | int isTransaction = pSavepoint->pNext==0 && db->isTransactionSavepoint; |
| 3063 | if( isTransaction && p1==SAVEPOINT_RELEASE ){ |
dan | 32b09f2 | 2009-09-23 17:29:59 +0000 | [diff] [blame] | 3064 | if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){ |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 3065 | goto vdbe_return; |
| 3066 | } |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3067 | db->autoCommit = 1; |
| 3068 | if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){ |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 3069 | p->pc = (int)(pOp - aOp); |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3070 | db->autoCommit = 0; |
| 3071 | p->rc = rc = SQLITE_BUSY; |
| 3072 | goto vdbe_return; |
| 3073 | } |
danielk1977 | 34cf35d | 2008-12-18 18:31:38 +0000 | [diff] [blame] | 3074 | db->isTransactionSavepoint = 0; |
| 3075 | rc = p->rc; |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3076 | }else{ |
drh | 47b7fc7 | 2014-11-11 01:33:57 +0000 | [diff] [blame] | 3077 | int isSchemaChange; |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3078 | iSavepoint = db->nSavepoint - iSavepoint - 1; |
drh | 31f1005 | 2012-03-31 17:17:26 +0000 | [diff] [blame] | 3079 | if( p1==SAVEPOINT_ROLLBACK ){ |
drh | 8257aa8 | 2017-07-26 19:59:13 +0000 | [diff] [blame] | 3080 | isSchemaChange = (db->mDbFlags & DBFLAG_SchemaChange)!=0; |
drh | 31f1005 | 2012-03-31 17:17:26 +0000 | [diff] [blame] | 3081 | for(ii=0; ii<db->nDb; ii++){ |
drh | 77b1dee | 2014-11-17 17:13:06 +0000 | [diff] [blame] | 3082 | rc = sqlite3BtreeTripAllCursors(db->aDb[ii].pBt, |
| 3083 | SQLITE_ABORT_ROLLBACK, |
drh | 47b7fc7 | 2014-11-11 01:33:57 +0000 | [diff] [blame] | 3084 | isSchemaChange==0); |
dan | 8023104 | 2014-11-12 14:56:02 +0000 | [diff] [blame] | 3085 | if( rc!=SQLITE_OK ) goto abort_due_to_error; |
drh | 31f1005 | 2012-03-31 17:17:26 +0000 | [diff] [blame] | 3086 | } |
drh | 47b7fc7 | 2014-11-11 01:33:57 +0000 | [diff] [blame] | 3087 | }else{ |
| 3088 | isSchemaChange = 0; |
drh | 0f198a7 | 2012-02-13 16:43:16 +0000 | [diff] [blame] | 3089 | } |
| 3090 | for(ii=0; ii<db->nDb; ii++){ |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3091 | rc = sqlite3BtreeSavepoint(db->aDb[ii].pBt, p1, iSavepoint); |
| 3092 | if( rc!=SQLITE_OK ){ |
| 3093 | goto abort_due_to_error; |
danielk1977 | bd43455 | 2009-03-18 10:33:00 +0000 | [diff] [blame] | 3094 | } |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3095 | } |
drh | 47b7fc7 | 2014-11-11 01:33:57 +0000 | [diff] [blame] | 3096 | if( isSchemaChange ){ |
drh | ba968db | 2018-07-24 22:02:12 +0000 | [diff] [blame] | 3097 | sqlite3ExpirePreparedStatements(db, 0); |
drh | 81028a4 | 2012-05-15 18:28:27 +0000 | [diff] [blame] | 3098 | sqlite3ResetAllSchemasOfConnection(db); |
drh | 8257aa8 | 2017-07-26 19:59:13 +0000 | [diff] [blame] | 3099 | db->mDbFlags |= DBFLAG_SchemaChange; |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3100 | } |
| 3101 | } |
| 3102 | |
| 3103 | /* Regardless of whether this is a RELEASE or ROLLBACK, destroy all |
| 3104 | ** savepoints nested inside of the savepoint being operated on. */ |
| 3105 | while( db->pSavepoint!=pSavepoint ){ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3106 | pTmp = db->pSavepoint; |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3107 | db->pSavepoint = pTmp->pNext; |
| 3108 | sqlite3DbFree(db, pTmp); |
| 3109 | db->nSavepoint--; |
| 3110 | } |
| 3111 | |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 3112 | /* If it is a RELEASE, then destroy the savepoint being operated on |
| 3113 | ** too. If it is a ROLLBACK TO, then set the number of deferred |
| 3114 | ** constraint violations present in the database to the value stored |
| 3115 | ** when the savepoint was created. */ |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3116 | if( p1==SAVEPOINT_RELEASE ){ |
| 3117 | assert( pSavepoint==db->pSavepoint ); |
| 3118 | db->pSavepoint = pSavepoint->pNext; |
| 3119 | sqlite3DbFree(db, pSavepoint); |
| 3120 | if( !isTransaction ){ |
| 3121 | db->nSavepoint--; |
| 3122 | } |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 3123 | }else{ |
| 3124 | db->nDeferredCons = pSavepoint->nDeferredCons; |
dan | cb3e4b7 | 2013-07-03 19:53:05 +0000 | [diff] [blame] | 3125 | db->nDeferredImmCons = pSavepoint->nDeferredImmCons; |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3126 | } |
dan | d9495cd | 2011-04-27 12:08:04 +0000 | [diff] [blame] | 3127 | |
dan | ea8562e | 2015-04-18 16:25:54 +0000 | [diff] [blame] | 3128 | if( !isTransaction || p1==SAVEPOINT_ROLLBACK ){ |
dan | d9495cd | 2011-04-27 12:08:04 +0000 | [diff] [blame] | 3129 | rc = sqlite3VtabSavepoint(db, p1, iSavepoint); |
| 3130 | if( rc!=SQLITE_OK ) goto abort_due_to_error; |
| 3131 | } |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3132 | } |
| 3133 | } |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 3134 | if( rc ) goto abort_due_to_error; |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3135 | |
| 3136 | break; |
| 3137 | } |
| 3138 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 3139 | /* Opcode: AutoCommit P1 P2 * * * |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 3140 | ** |
| 3141 | ** 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] | 3142 | ** back any currently active btree transactions. If there are any active |
drh | c25eabe | 2009-02-24 18:57:31 +0000 | [diff] [blame] | 3143 | ** VMs (apart from this one), then a ROLLBACK fails. A COMMIT fails if |
| 3144 | ** there are active writing VMs or active VMs that use shared cache. |
drh | 92f02c3 | 2004-09-02 14:57:08 +0000 | [diff] [blame] | 3145 | ** |
| 3146 | ** This instruction causes the VM to halt. |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 3147 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3148 | case OP_AutoCommit: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3149 | int desiredAutoCommit; |
shane | 68c0273 | 2009-06-09 18:14:18 +0000 | [diff] [blame] | 3150 | int iRollback; |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 3151 | |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3152 | desiredAutoCommit = pOp->p1; |
shane | 68c0273 | 2009-06-09 18:14:18 +0000 | [diff] [blame] | 3153 | iRollback = pOp->p2; |
drh | ad4a4b8 | 2008-11-05 16:37:34 +0000 | [diff] [blame] | 3154 | assert( desiredAutoCommit==1 || desiredAutoCommit==0 ); |
shane | 68c0273 | 2009-06-09 18:14:18 +0000 | [diff] [blame] | 3155 | assert( desiredAutoCommit==1 || iRollback==0 ); |
drh | 4f7d3a5 | 2013-06-27 23:54:02 +0000 | [diff] [blame] | 3156 | assert( db->nVdbeActive>0 ); /* At least this one VM is active */ |
dan | c0537fe | 2013-06-28 19:41:43 +0000 | [diff] [blame] | 3157 | assert( p->bIsReader ); |
danielk1977 | 46c43ed | 2004-06-30 06:30:25 +0000 | [diff] [blame] | 3158 | |
drh | b0c8865 | 2016-02-01 13:21:13 +0000 | [diff] [blame] | 3159 | if( desiredAutoCommit!=db->autoCommit ){ |
shane | 68c0273 | 2009-06-09 18:14:18 +0000 | [diff] [blame] | 3160 | if( iRollback ){ |
drh | ad4a4b8 | 2008-11-05 16:37:34 +0000 | [diff] [blame] | 3161 | assert( desiredAutoCommit==1 ); |
drh | 21021a5 | 2012-02-13 17:01:51 +0000 | [diff] [blame] | 3162 | sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK); |
danielk1977 | f3f06bb | 2005-12-16 15:24:28 +0000 | [diff] [blame] | 3163 | db->autoCommit = 1; |
drh | b0c8865 | 2016-02-01 13:21:13 +0000 | [diff] [blame] | 3164 | }else if( desiredAutoCommit && db->nVdbeWrite>0 ){ |
| 3165 | /* If this instruction implements a COMMIT and other VMs are writing |
| 3166 | ** return an error indicating that the other VMs must complete first. |
| 3167 | */ |
| 3168 | sqlite3VdbeError(p, "cannot commit transaction - " |
| 3169 | "SQL statements in progress"); |
| 3170 | rc = SQLITE_BUSY; |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 3171 | goto abort_due_to_error; |
dan | 32b09f2 | 2009-09-23 17:29:59 +0000 | [diff] [blame] | 3172 | }else if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){ |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 3173 | goto vdbe_return; |
danielk1977 | f3f06bb | 2005-12-16 15:24:28 +0000 | [diff] [blame] | 3174 | }else{ |
shane | 7d3846a | 2008-12-11 02:58:26 +0000 | [diff] [blame] | 3175 | db->autoCommit = (u8)desiredAutoCommit; |
drh | 8ff2587 | 2015-07-31 18:59:56 +0000 | [diff] [blame] | 3176 | } |
| 3177 | if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){ |
| 3178 | p->pc = (int)(pOp - aOp); |
| 3179 | db->autoCommit = (u8)(1-desiredAutoCommit); |
| 3180 | p->rc = rc = SQLITE_BUSY; |
| 3181 | goto vdbe_return; |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 3182 | } |
danielk1977 | bd43455 | 2009-03-18 10:33:00 +0000 | [diff] [blame] | 3183 | assert( db->nStatement==0 ); |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3184 | sqlite3CloseSavepoints(db); |
drh | 83968c4 | 2007-04-18 16:45:24 +0000 | [diff] [blame] | 3185 | if( p->rc==SQLITE_OK ){ |
drh | 900b31e | 2007-08-28 02:27:51 +0000 | [diff] [blame] | 3186 | rc = SQLITE_DONE; |
drh | 83968c4 | 2007-04-18 16:45:24 +0000 | [diff] [blame] | 3187 | }else{ |
drh | 900b31e | 2007-08-28 02:27:51 +0000 | [diff] [blame] | 3188 | rc = SQLITE_ERROR; |
drh | 83968c4 | 2007-04-18 16:45:24 +0000 | [diff] [blame] | 3189 | } |
drh | 900b31e | 2007-08-28 02:27:51 +0000 | [diff] [blame] | 3190 | goto vdbe_return; |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 3191 | }else{ |
drh | 22c17b8 | 2015-05-15 04:13:15 +0000 | [diff] [blame] | 3192 | sqlite3VdbeError(p, |
drh | ad4a4b8 | 2008-11-05 16:37:34 +0000 | [diff] [blame] | 3193 | (!desiredAutoCommit)?"cannot start a transaction within a transaction":( |
shane | 68c0273 | 2009-06-09 18:14:18 +0000 | [diff] [blame] | 3194 | (iRollback)?"cannot rollback - no transaction is active": |
drh | f089aa4 | 2008-07-08 19:34:06 +0000 | [diff] [blame] | 3195 | "cannot commit - no transaction is active")); |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 3196 | |
| 3197 | rc = SQLITE_ERROR; |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 3198 | goto abort_due_to_error; |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 3199 | } |
| 3200 | break; |
| 3201 | } |
| 3202 | |
drh | b22f7c8 | 2014-02-06 23:56:27 +0000 | [diff] [blame] | 3203 | /* Opcode: Transaction P1 P2 P3 P4 P5 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3204 | ** |
drh | 05a86c5 | 2014-02-16 01:55:49 +0000 | [diff] [blame] | 3205 | ** Begin a transaction on database P1 if a transaction is not already |
| 3206 | ** active. |
| 3207 | ** If P2 is non-zero, then a write-transaction is started, or if a |
| 3208 | ** read-transaction is already active, it is upgraded to a write-transaction. |
| 3209 | ** If P2 is zero, then a read-transaction is started. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3210 | ** |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 3211 | ** P1 is the index of the database file on which the transaction is |
| 3212 | ** started. Index 0 is the main database file and index 1 is the |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 3213 | ** file used for temporary tables. Indices of 2 or more are used for |
| 3214 | ** attached databases. |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 3215 | ** |
dan | e0af83a | 2009-09-08 19:15:01 +0000 | [diff] [blame] | 3216 | ** If a write-transaction is started and the Vdbe.usesStmtJournal flag is |
| 3217 | ** true (this flag is set if the Vdbe may modify more than one row and may |
| 3218 | ** throw an ABORT exception), a statement transaction may also be opened. |
| 3219 | ** More specifically, a statement transaction is opened iff the database |
| 3220 | ** connection is currently not in autocommit mode, or if there are other |
drh | a451017 | 2012-02-02 15:50:17 +0000 | [diff] [blame] | 3221 | ** active statements. A statement transaction allows the changes made by this |
dan | e0af83a | 2009-09-08 19:15:01 +0000 | [diff] [blame] | 3222 | ** VDBE to be rolled back after an error without having to roll back the |
| 3223 | ** entire transaction. If no error is encountered, the statement transaction |
| 3224 | ** will automatically commit when the VDBE halts. |
| 3225 | ** |
drh | b22f7c8 | 2014-02-06 23:56:27 +0000 | [diff] [blame] | 3226 | ** If P5!=0 then this opcode also checks the schema cookie against P3 |
| 3227 | ** and the schema generation counter against P4. |
| 3228 | ** The cookie changes its value whenever the database schema changes. |
| 3229 | ** This operation is used to detect when that the cookie has changed |
drh | 05a86c5 | 2014-02-16 01:55:49 +0000 | [diff] [blame] | 3230 | ** and that the current process needs to reread the schema. If the schema |
| 3231 | ** cookie in P3 differs from the schema cookie in the database header or |
| 3232 | ** if the schema generation counter in P4 differs from the current |
| 3233 | ** generation counter, then an SQLITE_SCHEMA error is raised and execution |
| 3234 | ** halts. The sqlite3_step() wrapper function might then reprepare the |
| 3235 | ** statement and rerun it from the beginning. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3236 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3237 | case OP_Transaction: { |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 3238 | Btree *pBt; |
drh | bb2d9b1 | 2018-06-06 16:28:40 +0000 | [diff] [blame] | 3239 | int iMeta = 0; |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 3240 | |
drh | 1713afb | 2013-06-28 01:24:57 +0000 | [diff] [blame] | 3241 | assert( p->bIsReader ); |
drh | 9e92a47 | 2013-06-27 17:40:30 +0000 | [diff] [blame] | 3242 | assert( p->readOnly==0 || pOp->p2==0 ); |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 3243 | assert( pOp->p1>=0 && pOp->p1<db->nDb ); |
drh | a7ab6d8 | 2014-07-21 15:44:39 +0000 | [diff] [blame] | 3244 | assert( DbMaskTest(p->btreeMask, pOp->p1) ); |
drh | 13447bf | 2013-07-10 13:33:49 +0000 | [diff] [blame] | 3245 | if( pOp->p2 && (db->flags & SQLITE_QueryOnly)!=0 ){ |
| 3246 | rc = SQLITE_READONLY; |
| 3247 | goto abort_due_to_error; |
| 3248 | } |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 3249 | pBt = db->aDb[pOp->p1].pBt; |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 3250 | |
danielk1977 | 24162fe | 2004-06-04 06:22:00 +0000 | [diff] [blame] | 3251 | if( pBt ){ |
drh | bb2d9b1 | 2018-06-06 16:28:40 +0000 | [diff] [blame] | 3252 | rc = sqlite3BtreeBeginTrans(pBt, pOp->p2, &iMeta); |
drh | cbd8db3 | 2015-08-20 17:18:32 +0000 | [diff] [blame] | 3253 | testcase( rc==SQLITE_BUSY_SNAPSHOT ); |
| 3254 | testcase( rc==SQLITE_BUSY_RECOVERY ); |
drh | 9e9f1bd | 2009-10-13 15:36:51 +0000 | [diff] [blame] | 3255 | if( rc!=SQLITE_OK ){ |
drh | fadd2b1 | 2016-09-19 23:39:34 +0000 | [diff] [blame] | 3256 | if( (rc&0xff)==SQLITE_BUSY ){ |
| 3257 | p->pc = (int)(pOp - aOp); |
| 3258 | p->rc = rc; |
| 3259 | goto vdbe_return; |
| 3260 | } |
danielk1977 | 24162fe | 2004-06-04 06:22:00 +0000 | [diff] [blame] | 3261 | goto abort_due_to_error; |
drh | 90bfcda | 2001-09-23 19:46:51 +0000 | [diff] [blame] | 3262 | } |
dan | e0af83a | 2009-09-08 19:15:01 +0000 | [diff] [blame] | 3263 | |
| 3264 | if( pOp->p2 && p->usesStmtJournal |
dan | c0537fe | 2013-06-28 19:41:43 +0000 | [diff] [blame] | 3265 | && (db->autoCommit==0 || db->nVdbeRead>1) |
dan | e0af83a | 2009-09-08 19:15:01 +0000 | [diff] [blame] | 3266 | ){ |
| 3267 | assert( sqlite3BtreeIsInTrans(pBt) ); |
| 3268 | if( p->iStatement==0 ){ |
| 3269 | assert( db->nStatement>=0 && db->nSavepoint>=0 ); |
| 3270 | db->nStatement++; |
| 3271 | p->iStatement = db->nSavepoint + db->nStatement; |
| 3272 | } |
dan | a311b80 | 2011-04-26 19:21:34 +0000 | [diff] [blame] | 3273 | |
drh | 346506f | 2011-05-25 01:16:42 +0000 | [diff] [blame] | 3274 | rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN, p->iStatement-1); |
dan | a311b80 | 2011-04-26 19:21:34 +0000 | [diff] [blame] | 3275 | if( rc==SQLITE_OK ){ |
| 3276 | rc = sqlite3BtreeBeginStmt(pBt, p->iStatement); |
| 3277 | } |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 3278 | |
| 3279 | /* Store the current value of the database handles deferred constraint |
| 3280 | ** counter. If the statement transaction needs to be rolled back, |
| 3281 | ** the value of this counter needs to be restored too. */ |
| 3282 | p->nStmtDefCons = db->nDeferredCons; |
dan | cb3e4b7 | 2013-07-03 19:53:05 +0000 | [diff] [blame] | 3283 | p->nStmtDefImmCons = db->nDeferredImmCons; |
dan | e0af83a | 2009-09-08 19:15:01 +0000 | [diff] [blame] | 3284 | } |
drh | 397776a | 2018-06-06 17:45:51 +0000 | [diff] [blame] | 3285 | } |
| 3286 | assert( pOp->p5==0 || pOp->p4type==P4_INT32 ); |
| 3287 | if( pOp->p5 |
| 3288 | && (iMeta!=pOp->p3 |
| 3289 | || db->aDb[pOp->p1].pSchema->iGeneration!=pOp->p4.i) |
| 3290 | ){ |
| 3291 | /* |
drh | 96fdcb4 | 2016-09-27 00:09:33 +0000 | [diff] [blame] | 3292 | ** IMPLEMENTATION-OF: R-03189-51135 As each SQL statement runs, the schema |
| 3293 | ** version is checked to ensure that the schema has not changed since the |
| 3294 | ** SQL statement was prepared. |
drh | 51a74d4 | 2015-02-28 01:04:27 +0000 | [diff] [blame] | 3295 | */ |
drh | b22f7c8 | 2014-02-06 23:56:27 +0000 | [diff] [blame] | 3296 | sqlite3DbFree(db, p->zErrMsg); |
| 3297 | p->zErrMsg = sqlite3DbStrDup(db, "database schema has changed"); |
| 3298 | /* If the schema-cookie from the database file matches the cookie |
| 3299 | ** stored with the in-memory representation of the schema, do |
| 3300 | ** not reload the schema from the database file. |
| 3301 | ** |
| 3302 | ** If virtual-tables are in use, this is not just an optimization. |
| 3303 | ** Often, v-tables store their data in other SQLite tables, which |
| 3304 | ** are queried from within xNext() and other v-table methods using |
| 3305 | ** prepared queries. If such a query is out-of-date, we do not want to |
| 3306 | ** discard the database schema, as the user code implementing the |
| 3307 | ** v-table would have to be ready for the sqlite3_vtab structure itself |
| 3308 | ** to be invalidated whenever sqlite3_step() is called from within |
| 3309 | ** a v-table method. |
| 3310 | */ |
| 3311 | if( db->aDb[pOp->p1].pSchema->schema_cookie!=iMeta ){ |
| 3312 | sqlite3ResetOneSchema(db, pOp->p1); |
| 3313 | } |
| 3314 | p->expired = 1; |
| 3315 | rc = SQLITE_SCHEMA; |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 3316 | } |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 3317 | if( rc ) goto abort_due_to_error; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3318 | break; |
| 3319 | } |
| 3320 | |
drh | b1fdb2a | 2008-01-05 04:06:03 +0000 | [diff] [blame] | 3321 | /* Opcode: ReadCookie P1 P2 P3 * * |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 3322 | ** |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3323 | ** Read cookie number P3 from database P1 and write it into register P2. |
danielk1977 | 0d19f7a | 2009-06-03 11:25:07 +0000 | [diff] [blame] | 3324 | ** P3==1 is the schema version. P3==2 is the database format. |
| 3325 | ** 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] | 3326 | ** the main database file and P1==1 is the database file used to store |
| 3327 | ** temporary tables. |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 3328 | ** |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 3329 | ** There must be a read-lock on the database (either a transaction |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 3330 | ** must be started or there must be an open cursor) before |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 3331 | ** executing this instruction. |
| 3332 | */ |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 3333 | case OP_ReadCookie: { /* out2 */ |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 3334 | int iMeta; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3335 | int iDb; |
| 3336 | int iCookie; |
danielk1977 | 180b56a | 2007-06-24 08:00:42 +0000 | [diff] [blame] | 3337 | |
drh | 1713afb | 2013-06-28 01:24:57 +0000 | [diff] [blame] | 3338 | assert( p->bIsReader ); |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3339 | iDb = pOp->p1; |
| 3340 | iCookie = pOp->p3; |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 3341 | assert( pOp->p3<SQLITE_N_BTREE_META ); |
danielk1977 | 180b56a | 2007-06-24 08:00:42 +0000 | [diff] [blame] | 3342 | assert( iDb>=0 && iDb<db->nDb ); |
| 3343 | assert( db->aDb[iDb].pBt!=0 ); |
drh | a7ab6d8 | 2014-07-21 15:44:39 +0000 | [diff] [blame] | 3344 | assert( DbMaskTest(p->btreeMask, iDb) ); |
danielk1977 | 0d19f7a | 2009-06-03 11:25:07 +0000 | [diff] [blame] | 3345 | |
danielk1977 | 602b466 | 2009-07-02 07:47:33 +0000 | [diff] [blame] | 3346 | sqlite3BtreeGetMeta(db->aDb[iDb].pBt, iCookie, (u32 *)&iMeta); |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 3347 | pOut = out2Prerelease(p, pOp); |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 3348 | pOut->u.i = iMeta; |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 3349 | break; |
| 3350 | } |
| 3351 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 3352 | /* Opcode: SetCookie P1 P2 P3 * * |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 3353 | ** |
drh | 1861afc | 2016-02-01 21:48:34 +0000 | [diff] [blame] | 3354 | ** Write the integer value P3 into cookie number P2 of database P1. |
| 3355 | ** P2==1 is the schema version. P2==2 is the database format. |
| 3356 | ** P2==3 is the recommended pager cache |
danielk1977 | 0d19f7a | 2009-06-03 11:25:07 +0000 | [diff] [blame] | 3357 | ** size, and so forth. P1==0 is the main database file and P1==1 is the |
| 3358 | ** database file used to store temporary tables. |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 3359 | ** |
| 3360 | ** A transaction must be started before executing this opcode. |
| 3361 | */ |
drh | 1861afc | 2016-02-01 21:48:34 +0000 | [diff] [blame] | 3362 | case OP_SetCookie: { |
drh | 3f7d4e4 | 2004-07-24 14:35:58 +0000 | [diff] [blame] | 3363 | Db *pDb; |
drh | 4031baf | 2018-05-28 17:31:20 +0000 | [diff] [blame] | 3364 | |
| 3365 | sqlite3VdbeIncrWriteCounter(p, 0); |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 3366 | assert( pOp->p2<SQLITE_N_BTREE_META ); |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 3367 | assert( pOp->p1>=0 && pOp->p1<db->nDb ); |
drh | a7ab6d8 | 2014-07-21 15:44:39 +0000 | [diff] [blame] | 3368 | assert( DbMaskTest(p->btreeMask, pOp->p1) ); |
drh | 9e92a47 | 2013-06-27 17:40:30 +0000 | [diff] [blame] | 3369 | assert( p->readOnly==0 ); |
drh | 3f7d4e4 | 2004-07-24 14:35:58 +0000 | [diff] [blame] | 3370 | pDb = &db->aDb[pOp->p1]; |
| 3371 | assert( pDb->pBt!=0 ); |
drh | 2120608 | 2011-04-04 18:22:02 +0000 | [diff] [blame] | 3372 | assert( sqlite3SchemaMutexHeld(db, pOp->p1, 0) ); |
drh | a3b321d | 2004-05-11 09:31:31 +0000 | [diff] [blame] | 3373 | /* See note about index shifting on OP_ReadCookie */ |
drh | 1861afc | 2016-02-01 21:48:34 +0000 | [diff] [blame] | 3374 | rc = sqlite3BtreeUpdateMeta(pDb->pBt, pOp->p2, pOp->p3); |
danielk1977 | 0d19f7a | 2009-06-03 11:25:07 +0000 | [diff] [blame] | 3375 | if( pOp->p2==BTREE_SCHEMA_VERSION ){ |
drh | 3f7d4e4 | 2004-07-24 14:35:58 +0000 | [diff] [blame] | 3376 | /* When the schema cookie changes, record the new cookie internally */ |
drh | 1861afc | 2016-02-01 21:48:34 +0000 | [diff] [blame] | 3377 | pDb->pSchema->schema_cookie = pOp->p3; |
drh | 8257aa8 | 2017-07-26 19:59:13 +0000 | [diff] [blame] | 3378 | db->mDbFlags |= DBFLAG_SchemaChange; |
danielk1977 | 0d19f7a | 2009-06-03 11:25:07 +0000 | [diff] [blame] | 3379 | }else if( pOp->p2==BTREE_FILE_FORMAT ){ |
drh | d28bcb3 | 2005-12-21 14:43:11 +0000 | [diff] [blame] | 3380 | /* Record changes in the file format */ |
drh | 1861afc | 2016-02-01 21:48:34 +0000 | [diff] [blame] | 3381 | pDb->pSchema->file_format = pOp->p3; |
drh | 3f7d4e4 | 2004-07-24 14:35:58 +0000 | [diff] [blame] | 3382 | } |
drh | fd426c6 | 2006-01-30 15:34:22 +0000 | [diff] [blame] | 3383 | if( pOp->p1==1 ){ |
| 3384 | /* Invalidate all prepared statements whenever the TEMP database |
| 3385 | ** schema is changed. Ticket #1644 */ |
drh | ba968db | 2018-07-24 22:02:12 +0000 | [diff] [blame] | 3386 | sqlite3ExpirePreparedStatements(db, 0); |
dan | fa401de | 2009-10-16 14:55:03 +0000 | [diff] [blame] | 3387 | p->expired = 0; |
drh | fd426c6 | 2006-01-30 15:34:22 +0000 | [diff] [blame] | 3388 | } |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 3389 | if( rc ) goto abort_due_to_error; |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 3390 | break; |
| 3391 | } |
| 3392 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 3393 | /* Opcode: OpenRead P1 P2 P3 P4 P5 |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 3394 | ** Synopsis: root=P2 iDb=P3 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3395 | ** |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 3396 | ** Open a read-only cursor for the database table whose root page is |
danielk1977 | 207872a | 2008-01-03 07:54:23 +0000 | [diff] [blame] | 3397 | ** P2 in a database file. The database file is determined by P3. |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 3398 | ** P3==0 means the main database, P3==1 means the database used for |
| 3399 | ** temporary tables, and P3>1 means used the corresponding attached |
| 3400 | ** database. Give the new cursor an identifier of P1. The P1 |
danielk1977 | 207872a | 2008-01-03 07:54:23 +0000 | [diff] [blame] | 3401 | ** values need not be contiguous but all P1 values should be small integers. |
| 3402 | ** It is an error for P1 to be negative. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3403 | ** |
drh | 8e9deb6 | 2018-06-05 13:43:02 +0000 | [diff] [blame] | 3404 | ** Allowed P5 bits: |
| 3405 | ** <ul> |
| 3406 | ** <li> <b>0x02 OPFLAG_SEEKEQ</b>: This cursor will only be used for |
| 3407 | ** equality lookups (implemented as a pair of opcodes OP_SeekGE/OP_IdxGT |
| 3408 | ** of OP_SeekLE/OP_IdxGT) |
| 3409 | ** </ul> |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 3410 | ** |
danielk1977 | d336e22 | 2009-02-20 10:58:41 +0000 | [diff] [blame] | 3411 | ** The P4 value may be either an integer (P4_INT32) or a pointer to |
| 3412 | ** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo |
drh | 8e9deb6 | 2018-06-05 13:43:02 +0000 | [diff] [blame] | 3413 | ** object, then table being opened must be an [index b-tree] where the |
| 3414 | ** KeyInfo object defines the content and collating |
| 3415 | ** sequence of that index b-tree. Otherwise, if P4 is an integer |
| 3416 | ** value, then the table being opened must be a [table b-tree] with a |
| 3417 | ** number of columns no less than the value of P4. |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 3418 | ** |
drh | 3526319 | 2014-07-22 20:02:19 +0000 | [diff] [blame] | 3419 | ** See also: OpenWrite, ReopenIdx |
| 3420 | */ |
| 3421 | /* Opcode: ReopenIdx P1 P2 P3 P4 P5 |
| 3422 | ** Synopsis: root=P2 iDb=P3 |
| 3423 | ** |
drh | 8e9deb6 | 2018-06-05 13:43:02 +0000 | [diff] [blame] | 3424 | ** The ReopenIdx opcode works like OP_OpenRead except that it first |
| 3425 | ** checks to see if the cursor on P1 is already open on the same |
| 3426 | ** b-tree and if it is this opcode becomes a no-op. In other words, |
drh | 3526319 | 2014-07-22 20:02:19 +0000 | [diff] [blame] | 3427 | ** if the cursor is already open, do not reopen it. |
| 3428 | ** |
drh | 8e9deb6 | 2018-06-05 13:43:02 +0000 | [diff] [blame] | 3429 | ** The ReopenIdx opcode may only be used with P5==0 or P5==OPFLAG_SEEKEQ |
| 3430 | ** and with P4 being a P4_KEYINFO object. Furthermore, the P3 value must |
| 3431 | ** be the same as every other ReopenIdx or OpenRead for the same cursor |
| 3432 | ** number. |
drh | 3526319 | 2014-07-22 20:02:19 +0000 | [diff] [blame] | 3433 | ** |
drh | 8e9deb6 | 2018-06-05 13:43:02 +0000 | [diff] [blame] | 3434 | ** Allowed P5 bits: |
| 3435 | ** <ul> |
| 3436 | ** <li> <b>0x02 OPFLAG_SEEKEQ</b>: This cursor will only be used for |
| 3437 | ** equality lookups (implemented as a pair of opcodes OP_SeekGE/OP_IdxGT |
| 3438 | ** of OP_SeekLE/OP_IdxGT) |
| 3439 | ** </ul> |
| 3440 | ** |
| 3441 | ** See also: OP_OpenRead, OP_OpenWrite |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3442 | */ |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 3443 | /* Opcode: OpenWrite P1 P2 P3 P4 P5 |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 3444 | ** Synopsis: root=P2 iDb=P3 |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 3445 | ** |
| 3446 | ** Open a read/write cursor named P1 on the table or index whose root |
drh | 8e9deb6 | 2018-06-05 13:43:02 +0000 | [diff] [blame] | 3447 | ** page is P2 (or whose root page is held in register P2 if the |
| 3448 | ** OPFLAG_P2ISREG bit is set in P5 - see below). |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 3449 | ** |
danielk1977 | d336e22 | 2009-02-20 10:58:41 +0000 | [diff] [blame] | 3450 | ** The P4 value may be either an integer (P4_INT32) or a pointer to |
| 3451 | ** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo |
drh | 8e9deb6 | 2018-06-05 13:43:02 +0000 | [diff] [blame] | 3452 | ** object, then table being opened must be an [index b-tree] where the |
| 3453 | ** KeyInfo object defines the content and collating |
| 3454 | ** sequence of that index b-tree. Otherwise, if P4 is an integer |
| 3455 | ** value, then the table being opened must be a [table b-tree] with a |
| 3456 | ** number of columns no less than the value of P4. |
jplyon | 5a56422 | 2003-06-02 06:15:58 +0000 | [diff] [blame] | 3457 | ** |
drh | 8e9deb6 | 2018-06-05 13:43:02 +0000 | [diff] [blame] | 3458 | ** Allowed P5 bits: |
| 3459 | ** <ul> |
| 3460 | ** <li> <b>0x02 OPFLAG_SEEKEQ</b>: This cursor will only be used for |
| 3461 | ** equality lookups (implemented as a pair of opcodes OP_SeekGE/OP_IdxGT |
| 3462 | ** of OP_SeekLE/OP_IdxGT) |
| 3463 | ** <li> <b>0x08 OPFLAG_FORDELETE</b>: This cursor is used only to seek |
| 3464 | ** and subsequently delete entries in an index btree. This is a |
| 3465 | ** hint to the storage engine that the storage engine is allowed to |
| 3466 | ** ignore. The hint is not used by the official SQLite b*tree storage |
| 3467 | ** engine, but is used by COMDB2. |
| 3468 | ** <li> <b>0x10 OPFLAG_P2ISREG</b>: Use the content of register P2 |
| 3469 | ** as the root page, not the value of P2 itself. |
| 3470 | ** </ul> |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 3471 | ** |
drh | 8e9deb6 | 2018-06-05 13:43:02 +0000 | [diff] [blame] | 3472 | ** This instruction works like OpenRead except that it opens the cursor |
| 3473 | ** in read/write mode. |
| 3474 | ** |
| 3475 | ** See also: OP_OpenRead, OP_ReopenIdx |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 3476 | */ |
drh | 3526319 | 2014-07-22 20:02:19 +0000 | [diff] [blame] | 3477 | case OP_ReopenIdx: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3478 | int nField; |
| 3479 | KeyInfo *pKeyInfo; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3480 | int p2; |
| 3481 | int iDb; |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 3482 | int wrFlag; |
| 3483 | Btree *pX; |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 3484 | VdbeCursor *pCur; |
drh | d946db0 | 2005-12-29 19:23:06 +0000 | [diff] [blame] | 3485 | Db *pDb; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3486 | |
drh | e0997b3 | 2015-03-20 14:57:50 +0000 | [diff] [blame] | 3487 | assert( pOp->p5==0 || pOp->p5==OPFLAG_SEEKEQ ); |
drh | 3526319 | 2014-07-22 20:02:19 +0000 | [diff] [blame] | 3488 | assert( pOp->p4type==P4_KEYINFO ); |
| 3489 | pCur = p->apCsr[pOp->p1]; |
drh | e8f2c9d | 2014-08-06 17:49:13 +0000 | [diff] [blame] | 3490 | if( pCur && pCur->pgnoRoot==(u32)pOp->p2 ){ |
drh | 3526319 | 2014-07-22 20:02:19 +0000 | [diff] [blame] | 3491 | assert( pCur->iDb==pOp->p3 ); /* Guaranteed by the code generator */ |
drh | e0997b3 | 2015-03-20 14:57:50 +0000 | [diff] [blame] | 3492 | goto open_cursor_set_hints; |
drh | 3526319 | 2014-07-22 20:02:19 +0000 | [diff] [blame] | 3493 | } |
| 3494 | /* If the cursor is not currently open or is open on a different |
| 3495 | ** index, then fall through into OP_OpenRead to force a reopen */ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3496 | case OP_OpenRead: |
drh | 1fa509a | 2015-03-20 16:34:49 +0000 | [diff] [blame] | 3497 | case OP_OpenWrite: |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3498 | |
drh | e0997b3 | 2015-03-20 14:57:50 +0000 | [diff] [blame] | 3499 | assert( pOp->opcode==OP_OpenWrite || pOp->p5==0 || pOp->p5==OPFLAG_SEEKEQ ); |
drh | 1713afb | 2013-06-28 01:24:57 +0000 | [diff] [blame] | 3500 | assert( p->bIsReader ); |
drh | 3526319 | 2014-07-22 20:02:19 +0000 | [diff] [blame] | 3501 | assert( pOp->opcode==OP_OpenRead || pOp->opcode==OP_ReopenIdx |
| 3502 | || p->readOnly==0 ); |
dan | 428c218 | 2012-08-06 18:50:11 +0000 | [diff] [blame] | 3503 | |
drh | ba968db | 2018-07-24 22:02:12 +0000 | [diff] [blame] | 3504 | if( p->expired==1 ){ |
drh | 47b7fc7 | 2014-11-11 01:33:57 +0000 | [diff] [blame] | 3505 | rc = SQLITE_ABORT_ROLLBACK; |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 3506 | goto abort_due_to_error; |
dan | fa401de | 2009-10-16 14:55:03 +0000 | [diff] [blame] | 3507 | } |
| 3508 | |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3509 | nField = 0; |
| 3510 | pKeyInfo = 0; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3511 | p2 = pOp->p2; |
| 3512 | iDb = pOp->p3; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3513 | assert( iDb>=0 && iDb<db->nDb ); |
drh | a7ab6d8 | 2014-07-21 15:44:39 +0000 | [diff] [blame] | 3514 | assert( DbMaskTest(p->btreeMask, iDb) ); |
drh | d946db0 | 2005-12-29 19:23:06 +0000 | [diff] [blame] | 3515 | pDb = &db->aDb[iDb]; |
| 3516 | pX = pDb->pBt; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3517 | assert( pX!=0 ); |
drh | d946db0 | 2005-12-29 19:23:06 +0000 | [diff] [blame] | 3518 | if( pOp->opcode==OP_OpenWrite ){ |
dan | fd261ec | 2015-10-22 20:54:33 +0000 | [diff] [blame] | 3519 | assert( OPFLAG_FORDELETE==BTREE_FORDELETE ); |
| 3520 | wrFlag = BTREE_WRCSR | (pOp->p5 & OPFLAG_FORDELETE); |
drh | 2120608 | 2011-04-04 18:22:02 +0000 | [diff] [blame] | 3521 | assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3522 | if( pDb->pSchema->file_format < p->minWriteFileFormat ){ |
| 3523 | p->minWriteFileFormat = pDb->pSchema->file_format; |
drh | d946db0 | 2005-12-29 19:23:06 +0000 | [diff] [blame] | 3524 | } |
| 3525 | }else{ |
| 3526 | wrFlag = 0; |
| 3527 | } |
dan | 428c218 | 2012-08-06 18:50:11 +0000 | [diff] [blame] | 3528 | if( pOp->p5 & OPFLAG_P2ISREG ){ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3529 | assert( p2>0 ); |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 3530 | assert( p2<=(p->nMem+1 - p->nCursor) ); |
drh | 8e9deb6 | 2018-06-05 13:43:02 +0000 | [diff] [blame] | 3531 | assert( pOp->opcode==OP_OpenWrite ); |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 3532 | pIn2 = &aMem[p2]; |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 3533 | assert( memIsValid(pIn2) ); |
| 3534 | assert( (pIn2->flags & MEM_Int)!=0 ); |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3535 | sqlite3VdbeMemIntegerify(pIn2); |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 3536 | p2 = (int)pIn2->u.i; |
drh | 0f3f766 | 2017-08-18 14:34:28 +0000 | [diff] [blame] | 3537 | /* The p2 value always comes from a prior OP_CreateBtree opcode and |
drh | 9a65f2c | 2009-06-22 19:05:40 +0000 | [diff] [blame] | 3538 | ** that opcode will always set the p2 value to 2 or more or else fail. |
| 3539 | ** If there were a failure, the prepared statement would have halted |
| 3540 | ** before reaching this instruction. */ |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 3541 | assert( p2>=2 ); |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 3542 | } |
danielk1977 | d336e22 | 2009-02-20 10:58:41 +0000 | [diff] [blame] | 3543 | if( pOp->p4type==P4_KEYINFO ){ |
| 3544 | pKeyInfo = pOp->p4.pKeyInfo; |
drh | 41e13e1 | 2013-11-07 14:09:39 +0000 | [diff] [blame] | 3545 | assert( pKeyInfo->enc==ENC(db) ); |
| 3546 | assert( pKeyInfo->db==db ); |
drh | a485ad1 | 2017-08-02 22:43:14 +0000 | [diff] [blame] | 3547 | nField = pKeyInfo->nAllField; |
danielk1977 | d336e22 | 2009-02-20 10:58:41 +0000 | [diff] [blame] | 3548 | }else if( pOp->p4type==P4_INT32 ){ |
| 3549 | nField = pOp->p4.i; |
| 3550 | } |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 3551 | assert( pOp->p1>=0 ); |
drh | 399af1d | 2013-11-20 17:25:55 +0000 | [diff] [blame] | 3552 | assert( nField>=0 ); |
| 3553 | testcase( nField==0 ); /* Table with INTEGER PRIMARY KEY and nothing else */ |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 3554 | pCur = allocateCursor(p, pOp->p1, nField, iDb, CURTYPE_BTREE); |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 3555 | if( pCur==0 ) goto no_mem; |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 3556 | pCur->nullRow = 1; |
drh | d4187c7 | 2010-08-30 22:15:45 +0000 | [diff] [blame] | 3557 | pCur->isOrdered = 1; |
drh | 3526319 | 2014-07-22 20:02:19 +0000 | [diff] [blame] | 3558 | pCur->pgnoRoot = p2; |
drh | b89aeb6 | 2016-01-27 15:49:32 +0000 | [diff] [blame] | 3559 | #ifdef SQLITE_DEBUG |
| 3560 | pCur->wrFlag = wrFlag; |
| 3561 | #endif |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 3562 | rc = sqlite3BtreeCursor(pX, p2, wrFlag, pKeyInfo, pCur->uc.pCursor); |
danielk1977 | d336e22 | 2009-02-20 10:58:41 +0000 | [diff] [blame] | 3563 | pCur->pKeyInfo = pKeyInfo; |
drh | 14da87f | 2013-11-20 21:51:33 +0000 | [diff] [blame] | 3564 | /* Set the VdbeCursor.isTable variable. Previous versions of |
danielk1977 | 172114a | 2009-07-07 15:47:12 +0000 | [diff] [blame] | 3565 | ** SQLite used to check if the root-page flags were sane at this point |
| 3566 | ** and report database corruption if they were not, but this check has |
| 3567 | ** since moved into the btree layer. */ |
| 3568 | pCur->isTable = pOp->p4type!=P4_KEYINFO; |
drh | e0997b3 | 2015-03-20 14:57:50 +0000 | [diff] [blame] | 3569 | |
| 3570 | open_cursor_set_hints: |
| 3571 | assert( OPFLAG_BULKCSR==BTREE_BULKLOAD ); |
| 3572 | assert( OPFLAG_SEEKEQ==BTREE_SEEK_EQ ); |
drh | 0403cb3 | 2015-08-14 23:57:04 +0000 | [diff] [blame] | 3573 | testcase( pOp->p5 & OPFLAG_BULKCSR ); |
drh | 9abe841 | 2016-01-02 05:00:31 +0000 | [diff] [blame] | 3574 | #ifdef SQLITE_ENABLE_CURSOR_HINTS |
drh | 0403cb3 | 2015-08-14 23:57:04 +0000 | [diff] [blame] | 3575 | testcase( pOp->p2 & OPFLAG_SEEKEQ ); |
| 3576 | #endif |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 3577 | sqlite3BtreeCursorHintFlags(pCur->uc.pCursor, |
drh | f7854c7 | 2015-10-27 13:24:37 +0000 | [diff] [blame] | 3578 | (pOp->p5 & (OPFLAG_BULKCSR|OPFLAG_SEEKEQ))); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 3579 | if( rc ) goto abort_due_to_error; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3580 | break; |
| 3581 | } |
| 3582 | |
drh | e08e8d6 | 2017-05-01 15:15:41 +0000 | [diff] [blame] | 3583 | /* Opcode: OpenDup P1 P2 * * * |
| 3584 | ** |
| 3585 | ** Open a new cursor P1 that points to the same ephemeral table as |
| 3586 | ** cursor P2. The P2 cursor must have been opened by a prior OP_OpenEphemeral |
| 3587 | ** opcode. Only ephemeral cursors may be duplicated. |
| 3588 | ** |
| 3589 | ** Duplicate ephemeral cursors are used for self-joins of materialized views. |
| 3590 | */ |
| 3591 | case OP_OpenDup: { |
| 3592 | VdbeCursor *pOrig; /* The original cursor to be duplicated */ |
| 3593 | VdbeCursor *pCx; /* The new cursor */ |
| 3594 | |
| 3595 | pOrig = p->apCsr[pOp->p2]; |
| 3596 | assert( pOrig->pBtx!=0 ); /* Only ephemeral cursors can be duplicated */ |
| 3597 | |
| 3598 | pCx = allocateCursor(p, pOp->p1, pOrig->nField, -1, CURTYPE_BTREE); |
| 3599 | if( pCx==0 ) goto no_mem; |
| 3600 | pCx->nullRow = 1; |
| 3601 | pCx->isEphemeral = 1; |
| 3602 | pCx->pKeyInfo = pOrig->pKeyInfo; |
| 3603 | pCx->isTable = pOrig->isTable; |
| 3604 | rc = sqlite3BtreeCursor(pOrig->pBtx, MASTER_ROOT, BTREE_WRCSR, |
| 3605 | pCx->pKeyInfo, pCx->uc.pCursor); |
drh | 3f4df4c | 2017-05-02 17:54:19 +0000 | [diff] [blame] | 3606 | /* The sqlite3BtreeCursor() routine can only fail for the first cursor |
| 3607 | ** opened for a database. Since there is already an open cursor when this |
| 3608 | ** opcode is run, the sqlite3BtreeCursor() cannot fail */ |
| 3609 | assert( rc==SQLITE_OK ); |
drh | e08e8d6 | 2017-05-01 15:15:41 +0000 | [diff] [blame] | 3610 | break; |
| 3611 | } |
| 3612 | |
| 3613 | |
drh | 2a5d990 | 2011-08-26 00:34:45 +0000 | [diff] [blame] | 3614 | /* Opcode: OpenEphemeral P1 P2 * P4 P5 |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 3615 | ** Synopsis: nColumn=P2 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3616 | ** |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 3617 | ** Open a new cursor P1 to a transient table. |
drh | 9170dd7 | 2005-07-08 17:13:46 +0000 | [diff] [blame] | 3618 | ** The cursor is always opened read/write even if |
drh | 25d3adb | 2010-04-05 15:11:08 +0000 | [diff] [blame] | 3619 | ** the main database is read-only. The ephemeral |
drh | 9170dd7 | 2005-07-08 17:13:46 +0000 | [diff] [blame] | 3620 | ** table is deleted automatically when the cursor is closed. |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 3621 | ** |
drh | 25d3adb | 2010-04-05 15:11:08 +0000 | [diff] [blame] | 3622 | ** P2 is the number of columns in the ephemeral table. |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 3623 | ** The cursor points to a BTree table if P4==0 and to a BTree index |
| 3624 | ** 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] | 3625 | ** that defines the format of keys in the index. |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 3626 | ** |
drh | 2a5d990 | 2011-08-26 00:34:45 +0000 | [diff] [blame] | 3627 | ** The P5 parameter can be a mask of the BTREE_* flags defined |
| 3628 | ** in btree.h. These flags control aspects of the operation of |
| 3629 | ** the btree. The BTREE_OMIT_JOURNAL and BTREE_SINGLE flags are |
| 3630 | ** added automatically. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3631 | */ |
drh | a21a64d | 2010-04-06 22:33:55 +0000 | [diff] [blame] | 3632 | /* Opcode: OpenAutoindex P1 P2 * P4 * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 3633 | ** Synopsis: nColumn=P2 |
drh | a21a64d | 2010-04-06 22:33:55 +0000 | [diff] [blame] | 3634 | ** |
| 3635 | ** This opcode works the same as OP_OpenEphemeral. It has a |
| 3636 | ** different name to distinguish its use. Tables created using |
| 3637 | ** by this opcode will be used for automatically created transient |
| 3638 | ** indices in joins. |
| 3639 | */ |
| 3640 | case OP_OpenAutoindex: |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3641 | case OP_OpenEphemeral: { |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 3642 | VdbeCursor *pCx; |
drh | 41e13e1 | 2013-11-07 14:09:39 +0000 | [diff] [blame] | 3643 | KeyInfo *pKeyInfo; |
| 3644 | |
drh | d4187c7 | 2010-08-30 22:15:45 +0000 | [diff] [blame] | 3645 | static const int vfsFlags = |
drh | 33f4e02 | 2007-09-03 15:19:34 +0000 | [diff] [blame] | 3646 | SQLITE_OPEN_READWRITE | |
| 3647 | SQLITE_OPEN_CREATE | |
| 3648 | SQLITE_OPEN_EXCLUSIVE | |
| 3649 | SQLITE_OPEN_DELETEONCLOSE | |
| 3650 | SQLITE_OPEN_TRANSIENT_DB; |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 3651 | assert( pOp->p1>=0 ); |
drh | 399af1d | 2013-11-20 17:25:55 +0000 | [diff] [blame] | 3652 | assert( pOp->p2>=0 ); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 3653 | pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, CURTYPE_BTREE); |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 3654 | if( pCx==0 ) goto no_mem; |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 3655 | pCx->nullRow = 1; |
drh | 079a307 | 2014-03-19 14:10:55 +0000 | [diff] [blame] | 3656 | pCx->isEphemeral = 1; |
drh | fbd8cbd | 2016-12-10 12:58:15 +0000 | [diff] [blame] | 3657 | rc = sqlite3BtreeOpen(db->pVfs, 0, db, &pCx->pBtx, |
drh | d4187c7 | 2010-08-30 22:15:45 +0000 | [diff] [blame] | 3658 | BTREE_OMIT_JOURNAL | BTREE_SINGLE | pOp->p5, vfsFlags); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3659 | if( rc==SQLITE_OK ){ |
drh | bb2d9b1 | 2018-06-06 16:28:40 +0000 | [diff] [blame] | 3660 | rc = sqlite3BtreeBeginTrans(pCx->pBtx, 1, 0); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3661 | } |
| 3662 | if( rc==SQLITE_OK ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3663 | /* If a transient index is required, create it by calling |
drh | d4187c7 | 2010-08-30 22:15:45 +0000 | [diff] [blame] | 3664 | ** sqlite3BtreeCreateTable() with the BTREE_BLOBKEY flag before |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3665 | ** opening it. If a transient table is required, just use the |
drh | d4187c7 | 2010-08-30 22:15:45 +0000 | [diff] [blame] | 3666 | ** automatically created table with root-page 1 (an BLOB_INTKEY table). |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3667 | */ |
drh | fbd8cbd | 2016-12-10 12:58:15 +0000 | [diff] [blame] | 3668 | if( (pCx->pKeyInfo = pKeyInfo = pOp->p4.pKeyInfo)!=0 ){ |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 3669 | int pgno; |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 3670 | assert( pOp->p4type==P4_KEYINFO ); |
drh | fbd8cbd | 2016-12-10 12:58:15 +0000 | [diff] [blame] | 3671 | rc = sqlite3BtreeCreateTable(pCx->pBtx, &pgno, BTREE_BLOBKEY | pOp->p5); |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 3672 | if( rc==SQLITE_OK ){ |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 3673 | assert( pgno==MASTER_ROOT+1 ); |
drh | 41e13e1 | 2013-11-07 14:09:39 +0000 | [diff] [blame] | 3674 | assert( pKeyInfo->db==db ); |
| 3675 | assert( pKeyInfo->enc==ENC(db) ); |
drh | fbd8cbd | 2016-12-10 12:58:15 +0000 | [diff] [blame] | 3676 | rc = sqlite3BtreeCursor(pCx->pBtx, pgno, BTREE_WRCSR, |
drh | 62aaa6c | 2015-11-21 17:27:42 +0000 | [diff] [blame] | 3677 | pKeyInfo, pCx->uc.pCursor); |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 3678 | } |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3679 | pCx->isTable = 0; |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 3680 | }else{ |
drh | fbd8cbd | 2016-12-10 12:58:15 +0000 | [diff] [blame] | 3681 | rc = sqlite3BtreeCursor(pCx->pBtx, MASTER_ROOT, BTREE_WRCSR, |
drh | 62aaa6c | 2015-11-21 17:27:42 +0000 | [diff] [blame] | 3682 | 0, pCx->uc.pCursor); |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3683 | pCx->isTable = 1; |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 3684 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3685 | } |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 3686 | if( rc ) goto abort_due_to_error; |
drh | d4187c7 | 2010-08-30 22:15:45 +0000 | [diff] [blame] | 3687 | pCx->isOrdered = (pOp->p5!=BTREE_UNORDERED); |
dan | 5134d13 | 2011-09-02 10:31:11 +0000 | [diff] [blame] | 3688 | break; |
| 3689 | } |
| 3690 | |
dan | fad9f9a | 2014-04-01 18:41:51 +0000 | [diff] [blame] | 3691 | /* Opcode: SorterOpen P1 P2 P3 P4 * |
dan | 5134d13 | 2011-09-02 10:31:11 +0000 | [diff] [blame] | 3692 | ** |
| 3693 | ** This opcode works like OP_OpenEphemeral except that it opens |
| 3694 | ** a transient index that is specifically designed to sort large |
| 3695 | ** tables using an external merge-sort algorithm. |
dan | fad9f9a | 2014-04-01 18:41:51 +0000 | [diff] [blame] | 3696 | ** |
| 3697 | ** If argument P3 is non-zero, then it indicates that the sorter may |
| 3698 | ** assume that a stable sort considering the first P3 fields of each |
| 3699 | ** key is sufficient to produce the required results. |
dan | 5134d13 | 2011-09-02 10:31:11 +0000 | [diff] [blame] | 3700 | */ |
drh | ca892a7 | 2011-09-03 00:17:51 +0000 | [diff] [blame] | 3701 | case OP_SorterOpen: { |
dan | 5134d13 | 2011-09-02 10:31:11 +0000 | [diff] [blame] | 3702 | VdbeCursor *pCx; |
drh | 3a94987 | 2012-09-18 13:20:13 +0000 | [diff] [blame] | 3703 | |
drh | 399af1d | 2013-11-20 17:25:55 +0000 | [diff] [blame] | 3704 | assert( pOp->p1>=0 ); |
| 3705 | assert( pOp->p2>=0 ); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 3706 | pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, CURTYPE_SORTER); |
dan | 5134d13 | 2011-09-02 10:31:11 +0000 | [diff] [blame] | 3707 | if( pCx==0 ) goto no_mem; |
| 3708 | pCx->pKeyInfo = pOp->p4.pKeyInfo; |
drh | 41e13e1 | 2013-11-07 14:09:39 +0000 | [diff] [blame] | 3709 | assert( pCx->pKeyInfo->db==db ); |
| 3710 | assert( pCx->pKeyInfo->enc==ENC(db) ); |
dan | fad9f9a | 2014-04-01 18:41:51 +0000 | [diff] [blame] | 3711 | rc = sqlite3VdbeSorterInit(db, pOp->p3, pCx); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 3712 | if( rc ) goto abort_due_to_error; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3713 | break; |
| 3714 | } |
| 3715 | |
dan | 78d5843 | 2014-03-25 15:04:07 +0000 | [diff] [blame] | 3716 | /* Opcode: SequenceTest P1 P2 * * * |
| 3717 | ** Synopsis: if( cursor[P1].ctr++ ) pc = P2 |
| 3718 | ** |
| 3719 | ** P1 is a sorter cursor. If the sequence counter is currently zero, jump |
| 3720 | ** to P2. Regardless of whether or not the jump is taken, increment the |
| 3721 | ** the sequence value. |
| 3722 | */ |
| 3723 | case OP_SequenceTest: { |
| 3724 | VdbeCursor *pC; |
| 3725 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 3726 | pC = p->apCsr[pOp->p1]; |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 3727 | assert( isSorter(pC) ); |
dan | 78d5843 | 2014-03-25 15:04:07 +0000 | [diff] [blame] | 3728 | if( (pC->seqCount++)==0 ){ |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 3729 | goto jump_to_p2; |
dan | 78d5843 | 2014-03-25 15:04:07 +0000 | [diff] [blame] | 3730 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3731 | break; |
| 3732 | } |
| 3733 | |
drh | 5f61229 | 2014-02-08 23:20:32 +0000 | [diff] [blame] | 3734 | /* Opcode: OpenPseudo P1 P2 P3 * * |
drh | 60830e3 | 2014-02-10 15:56:34 +0000 | [diff] [blame] | 3735 | ** Synopsis: P3 columns in r[P2] |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3736 | ** |
| 3737 | ** Open a new cursor that points to a fake table that contains a single |
drh | 5f61229 | 2014-02-08 23:20:32 +0000 | [diff] [blame] | 3738 | ** row of data. The content of that one row is the content of memory |
| 3739 | ** register P2. In other words, cursor P1 becomes an alias for the |
| 3740 | ** MEM_Blob content contained in register P2. |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3741 | ** |
drh | 2d8d7ce | 2010-02-15 15:17:05 +0000 | [diff] [blame] | 3742 | ** A pseudo-table created by this opcode is used to hold a single |
drh | cdd536f | 2006-03-17 00:04:03 +0000 | [diff] [blame] | 3743 | ** row output from the sorter so that the row can be decomposed into |
drh | 3e9ca09 | 2009-09-08 01:14:48 +0000 | [diff] [blame] | 3744 | ** individual columns using the OP_Column opcode. The OP_Column opcode |
| 3745 | ** is the only cursor opcode that works with a pseudo-table. |
danielk1977 | d336e22 | 2009-02-20 10:58:41 +0000 | [diff] [blame] | 3746 | ** |
| 3747 | ** P3 is the number of fields in the records that will be stored by |
| 3748 | ** the pseudo-table. |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3749 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3750 | case OP_OpenPseudo: { |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 3751 | VdbeCursor *pCx; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3752 | |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 3753 | assert( pOp->p1>=0 ); |
drh | 399af1d | 2013-11-20 17:25:55 +0000 | [diff] [blame] | 3754 | assert( pOp->p3>=0 ); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 3755 | pCx = allocateCursor(p, pOp->p1, pOp->p3, -1, CURTYPE_PSEUDO); |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 3756 | if( pCx==0 ) goto no_mem; |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3757 | pCx->nullRow = 1; |
drh | fe0cf7a | 2017-08-16 19:20:20 +0000 | [diff] [blame] | 3758 | pCx->seekResult = pOp->p2; |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3759 | pCx->isTable = 1; |
drh | fe0cf7a | 2017-08-16 19:20:20 +0000 | [diff] [blame] | 3760 | /* Give this pseudo-cursor a fake BtCursor pointer so that pCx |
| 3761 | ** can be safely passed to sqlite3VdbeCursorMoveto(). This avoids a test |
| 3762 | ** for pCx->eCurType==CURTYPE_BTREE inside of sqlite3VdbeCursorMoveto() |
| 3763 | ** which is a performance optimization */ |
| 3764 | pCx->uc.pCursor = sqlite3BtreeFakeValidCursor(); |
drh | 5f61229 | 2014-02-08 23:20:32 +0000 | [diff] [blame] | 3765 | assert( pOp->p5==0 ); |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3766 | break; |
| 3767 | } |
| 3768 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 3769 | /* Opcode: Close P1 * * * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3770 | ** |
| 3771 | ** Close a cursor previously opened as P1. If P1 is not |
| 3772 | ** currently open, this instruction is a no-op. |
| 3773 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3774 | case OP_Close: { |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 3775 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 3776 | sqlite3VdbeFreeCursor(p, p->apCsr[pOp->p1]); |
| 3777 | p->apCsr[pOp->p1] = 0; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3778 | break; |
| 3779 | } |
| 3780 | |
drh | 97bae79 | 2015-06-05 15:59:57 +0000 | [diff] [blame] | 3781 | #ifdef SQLITE_ENABLE_COLUMN_USED_MASK |
| 3782 | /* Opcode: ColumnsUsed P1 * * P4 * |
| 3783 | ** |
| 3784 | ** This opcode (which only exists if SQLite was compiled with |
| 3785 | ** SQLITE_ENABLE_COLUMN_USED_MASK) identifies which columns of the |
| 3786 | ** table or index for cursor P1 are used. P4 is a 64-bit integer |
| 3787 | ** (P4_INT64) in which the first 63 bits are one for each of the |
| 3788 | ** first 63 columns of the table or index that are actually used |
| 3789 | ** by the cursor. The high-order bit is set if any column after |
| 3790 | ** the 64th is used. |
| 3791 | */ |
| 3792 | case OP_ColumnsUsed: { |
| 3793 | VdbeCursor *pC; |
| 3794 | pC = p->apCsr[pOp->p1]; |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 3795 | assert( pC->eCurType==CURTYPE_BTREE ); |
drh | 97bae79 | 2015-06-05 15:59:57 +0000 | [diff] [blame] | 3796 | pC->maskUsed = *(u64*)pOp->p4.pI64; |
| 3797 | break; |
| 3798 | } |
| 3799 | #endif |
| 3800 | |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 3801 | /* Opcode: SeekGE P1 P2 P3 P4 * |
drh | f63552b | 2013-10-30 00:25:03 +0000 | [diff] [blame] | 3802 | ** Synopsis: key=r[P3@P4] |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3803 | ** |
danielk1977 | b790c6c | 2008-04-18 10:25:24 +0000 | [diff] [blame] | 3804 | ** 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] | 3805 | ** use the value in register P3 as the key. If cursor P1 refers |
danielk1977 | b790c6c | 2008-04-18 10:25:24 +0000 | [diff] [blame] | 3806 | ** to an SQL index, then P3 is the first in an array of P4 registers |
| 3807 | ** that are used as an unpacked index key. |
| 3808 | ** |
| 3809 | ** Reposition cursor P1 so that it points to the smallest entry that |
| 3810 | ** is greater than or equal to the key value. If there are no records |
| 3811 | ** 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] | 3812 | ** |
drh | b1d607d | 2015-11-05 22:30:54 +0000 | [diff] [blame] | 3813 | ** If the cursor P1 was opened using the OPFLAG_SEEKEQ flag, then this |
| 3814 | ** opcode will always land on a record that equally equals the key, or |
| 3815 | ** else jump immediately to P2. When the cursor is OPFLAG_SEEKEQ, this |
| 3816 | ** opcode must be followed by an IdxLE opcode with the same arguments. |
| 3817 | ** The IdxLE opcode will be skipped if this opcode succeeds, but the |
| 3818 | ** IdxLE opcode will be used on subsequent loop iterations. |
| 3819 | ** |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 3820 | ** This opcode leaves the cursor configured to move in forward order, |
drh | bc5cf38 | 2014-08-06 01:08:07 +0000 | [diff] [blame] | 3821 | ** from the beginning toward the end. In other words, the cursor is |
drh | 5dad9a3 | 2014-07-25 18:37:42 +0000 | [diff] [blame] | 3822 | ** configured to use Next, not Prev. |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 3823 | ** |
drh | 935850e | 2014-05-24 17:15:15 +0000 | [diff] [blame] | 3824 | ** See also: Found, NotFound, SeekLt, SeekGt, SeekLe |
drh | 7cf6e4d | 2004-05-19 14:56:55 +0000 | [diff] [blame] | 3825 | */ |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 3826 | /* Opcode: SeekGT P1 P2 P3 P4 * |
drh | f63552b | 2013-10-30 00:25:03 +0000 | [diff] [blame] | 3827 | ** Synopsis: key=r[P3@P4] |
drh | 7cf6e4d | 2004-05-19 14:56:55 +0000 | [diff] [blame] | 3828 | ** |
danielk1977 | b790c6c | 2008-04-18 10:25:24 +0000 | [diff] [blame] | 3829 | ** 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] | 3830 | ** use the value in register P3 as a key. If cursor P1 refers |
danielk1977 | b790c6c | 2008-04-18 10:25:24 +0000 | [diff] [blame] | 3831 | ** to an SQL index, then P3 is the first in an array of P4 registers |
| 3832 | ** that are used as an unpacked index key. |
| 3833 | ** |
| 3834 | ** Reposition cursor P1 so that it points to the smallest entry that |
| 3835 | ** is greater than the key value. If there are no records greater than |
| 3836 | ** the key and P2 is not zero, then jump to P2. |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 3837 | ** |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 3838 | ** This opcode leaves the cursor configured to move in forward order, |
drh | 4ed2fb9 | 2014-08-14 13:06:25 +0000 | [diff] [blame] | 3839 | ** from the beginning toward the end. In other words, the cursor is |
drh | 5dad9a3 | 2014-07-25 18:37:42 +0000 | [diff] [blame] | 3840 | ** configured to use Next, not Prev. |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 3841 | ** |
drh | 935850e | 2014-05-24 17:15:15 +0000 | [diff] [blame] | 3842 | ** See also: Found, NotFound, SeekLt, SeekGe, SeekLe |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3843 | */ |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 3844 | /* Opcode: SeekLT P1 P2 P3 P4 * |
drh | f63552b | 2013-10-30 00:25:03 +0000 | [diff] [blame] | 3845 | ** Synopsis: key=r[P3@P4] |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 3846 | ** |
danielk1977 | b790c6c | 2008-04-18 10:25:24 +0000 | [diff] [blame] | 3847 | ** 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] | 3848 | ** use the value in register P3 as a key. If cursor P1 refers |
danielk1977 | b790c6c | 2008-04-18 10:25:24 +0000 | [diff] [blame] | 3849 | ** to an SQL index, then P3 is the first in an array of P4 registers |
| 3850 | ** that are used as an unpacked index key. |
| 3851 | ** |
| 3852 | ** Reposition cursor P1 so that it points to the largest entry that |
| 3853 | ** is less than the key value. If there are no records less than |
| 3854 | ** the key and P2 is not zero, then jump to P2. |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 3855 | ** |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 3856 | ** This opcode leaves the cursor configured to move in reverse order, |
| 3857 | ** from the end toward the beginning. In other words, the cursor is |
drh | 5dad9a3 | 2014-07-25 18:37:42 +0000 | [diff] [blame] | 3858 | ** configured to use Prev, not Next. |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 3859 | ** |
drh | 935850e | 2014-05-24 17:15:15 +0000 | [diff] [blame] | 3860 | ** See also: Found, NotFound, SeekGt, SeekGe, SeekLe |
drh | 7cf6e4d | 2004-05-19 14:56:55 +0000 | [diff] [blame] | 3861 | */ |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 3862 | /* Opcode: SeekLE P1 P2 P3 P4 * |
drh | f63552b | 2013-10-30 00:25:03 +0000 | [diff] [blame] | 3863 | ** Synopsis: key=r[P3@P4] |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 3864 | ** |
danielk1977 | b790c6c | 2008-04-18 10:25:24 +0000 | [diff] [blame] | 3865 | ** 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] | 3866 | ** use the value in register P3 as a key. If cursor P1 refers |
danielk1977 | b790c6c | 2008-04-18 10:25:24 +0000 | [diff] [blame] | 3867 | ** to an SQL index, then P3 is the first in an array of P4 registers |
| 3868 | ** that are used as an unpacked index key. |
danielk1977 | 751de56 | 2008-04-18 09:01:15 +0000 | [diff] [blame] | 3869 | ** |
danielk1977 | b790c6c | 2008-04-18 10:25:24 +0000 | [diff] [blame] | 3870 | ** Reposition cursor P1 so that it points to the largest entry that |
| 3871 | ** is less than or equal to the key value. If there are no records |
| 3872 | ** 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] | 3873 | ** |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 3874 | ** This opcode leaves the cursor configured to move in reverse order, |
| 3875 | ** from the end toward the beginning. In other words, the cursor is |
drh | 5dad9a3 | 2014-07-25 18:37:42 +0000 | [diff] [blame] | 3876 | ** configured to use Prev, not Next. |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 3877 | ** |
drh | b1d607d | 2015-11-05 22:30:54 +0000 | [diff] [blame] | 3878 | ** If the cursor P1 was opened using the OPFLAG_SEEKEQ flag, then this |
| 3879 | ** opcode will always land on a record that equally equals the key, or |
| 3880 | ** else jump immediately to P2. When the cursor is OPFLAG_SEEKEQ, this |
| 3881 | ** opcode must be followed by an IdxGE opcode with the same arguments. |
| 3882 | ** The IdxGE opcode will be skipped if this opcode succeeds, but the |
| 3883 | ** IdxGE opcode will be used on subsequent loop iterations. |
| 3884 | ** |
drh | 935850e | 2014-05-24 17:15:15 +0000 | [diff] [blame] | 3885 | ** See also: Found, NotFound, SeekGt, SeekGe, SeekLt |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 3886 | */ |
mistachkin | 758784d | 2018-07-25 15:12:29 +0000 | [diff] [blame] | 3887 | case OP_SeekLT: /* jump, in3, group */ |
| 3888 | case OP_SeekLE: /* jump, in3, group */ |
| 3889 | case OP_SeekGE: /* jump, in3, group */ |
| 3890 | case OP_SeekGT: { /* jump, in3, group */ |
drh | b1d607d | 2015-11-05 22:30:54 +0000 | [diff] [blame] | 3891 | int res; /* Comparison result */ |
| 3892 | int oc; /* Opcode */ |
| 3893 | VdbeCursor *pC; /* The cursor to seek */ |
| 3894 | UnpackedRecord r; /* The key to seek for */ |
| 3895 | int nField; /* Number of columns or fields in the key */ |
| 3896 | i64 iKey; /* The rowid we are to seek to */ |
drh | d6b7946 | 2015-11-07 01:19:00 +0000 | [diff] [blame] | 3897 | int eqOnly; /* Only interested in == results */ |
drh | 80ff32f | 2001-11-04 18:32:46 +0000 | [diff] [blame] | 3898 | |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 3899 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
drh | 959403f | 2008-12-12 17:56:16 +0000 | [diff] [blame] | 3900 | assert( pOp->p2!=0 ); |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 3901 | pC = p->apCsr[pOp->p1]; |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 3902 | assert( pC!=0 ); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 3903 | assert( pC->eCurType==CURTYPE_BTREE ); |
drh | 4a1d365 | 2014-02-14 15:13:36 +0000 | [diff] [blame] | 3904 | assert( OP_SeekLE == OP_SeekLT+1 ); |
| 3905 | assert( OP_SeekGE == OP_SeekLT+2 ); |
| 3906 | assert( OP_SeekGT == OP_SeekLT+3 ); |
drh | d4187c7 | 2010-08-30 22:15:45 +0000 | [diff] [blame] | 3907 | assert( pC->isOrdered ); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 3908 | assert( pC->uc.pCursor!=0 ); |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 3909 | oc = pOp->opcode; |
drh | d6b7946 | 2015-11-07 01:19:00 +0000 | [diff] [blame] | 3910 | eqOnly = 0; |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 3911 | pC->nullRow = 0; |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 3912 | #ifdef SQLITE_DEBUG |
| 3913 | pC->seekOp = pOp->opcode; |
| 3914 | #endif |
drh | e0997b3 | 2015-03-20 14:57:50 +0000 | [diff] [blame] | 3915 | |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 3916 | if( pC->isTable ){ |
drh | d6b7946 | 2015-11-07 01:19:00 +0000 | [diff] [blame] | 3917 | /* The BTREE_SEEK_EQ flag is only set on index cursors */ |
drh | 218c66e | 2016-12-27 12:35:36 +0000 | [diff] [blame] | 3918 | assert( sqlite3BtreeCursorHasHint(pC->uc.pCursor, BTREE_SEEK_EQ)==0 |
| 3919 | || CORRUPT_DB ); |
drh | d6b7946 | 2015-11-07 01:19:00 +0000 | [diff] [blame] | 3920 | |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 3921 | /* The input value in P3 might be of any type: integer, real, string, |
| 3922 | ** blob, or NULL. But it needs to be an integer before we can do |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 3923 | ** the seek, so convert it. */ |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 3924 | pIn3 = &aMem[pOp->p3]; |
drh | 11a6eee | 2014-09-19 22:01:54 +0000 | [diff] [blame] | 3925 | if( (pIn3->flags & (MEM_Int|MEM_Real|MEM_Str))==MEM_Str ){ |
drh | bd9507c | 2014-08-23 17:21:37 +0000 | [diff] [blame] | 3926 | applyNumericAffinity(pIn3, 0); |
| 3927 | } |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 3928 | iKey = sqlite3VdbeIntValue(pIn3); |
drh | 959403f | 2008-12-12 17:56:16 +0000 | [diff] [blame] | 3929 | |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 3930 | /* If the P3 value could not be converted into an integer without |
| 3931 | ** loss of information, then special processing is required... */ |
| 3932 | if( (pIn3->flags & MEM_Int)==0 ){ |
| 3933 | if( (pIn3->flags & MEM_Real)==0 ){ |
| 3934 | /* If the P3 value cannot be converted into any kind of a number, |
| 3935 | ** then the seek is not possible, so jump to P2 */ |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 3936 | VdbeBranchTaken(1,2); goto jump_to_p2; |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 3937 | break; |
| 3938 | } |
drh | 959403f | 2008-12-12 17:56:16 +0000 | [diff] [blame] | 3939 | |
dan | aa1776f | 2013-11-26 18:22:59 +0000 | [diff] [blame] | 3940 | /* If the approximation iKey is larger than the actual real search |
| 3941 | ** term, substitute >= for > and < for <=. e.g. if the search term |
| 3942 | ** is 4.9 and the integer approximation 5: |
| 3943 | ** |
| 3944 | ** (x > 4.9) -> (x >= 5) |
| 3945 | ** (x <= 4.9) -> (x < 5) |
| 3946 | */ |
drh | 74eaba4 | 2014-09-18 17:52:15 +0000 | [diff] [blame] | 3947 | if( pIn3->u.r<(double)iKey ){ |
drh | 4a1d365 | 2014-02-14 15:13:36 +0000 | [diff] [blame] | 3948 | assert( OP_SeekGE==(OP_SeekGT-1) ); |
| 3949 | assert( OP_SeekLT==(OP_SeekLE-1) ); |
| 3950 | assert( (OP_SeekLE & 0x0001)==(OP_SeekGT & 0x0001) ); |
| 3951 | if( (oc & 0x0001)==(OP_SeekGT & 0x0001) ) oc--; |
dan | aa1776f | 2013-11-26 18:22:59 +0000 | [diff] [blame] | 3952 | } |
| 3953 | |
| 3954 | /* If the approximation iKey is smaller than the actual real search |
| 3955 | ** term, substitute <= for < and > for >=. */ |
drh | 74eaba4 | 2014-09-18 17:52:15 +0000 | [diff] [blame] | 3956 | else if( pIn3->u.r>(double)iKey ){ |
drh | 4a1d365 | 2014-02-14 15:13:36 +0000 | [diff] [blame] | 3957 | assert( OP_SeekLE==(OP_SeekLT+1) ); |
| 3958 | assert( OP_SeekGT==(OP_SeekGE+1) ); |
| 3959 | assert( (OP_SeekLT & 0x0001)==(OP_SeekGE & 0x0001) ); |
| 3960 | if( (oc & 0x0001)==(OP_SeekLT & 0x0001) ) oc++; |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 3961 | } |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 3962 | } |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 3963 | rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, 0, (u64)iKey, 0, &res); |
drh | b53a5a9 | 2014-10-12 22:37:22 +0000 | [diff] [blame] | 3964 | pC->movetoTarget = iKey; /* Used by OP_Delete */ |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 3965 | if( rc!=SQLITE_OK ){ |
| 3966 | goto abort_due_to_error; |
drh | 1af3fdb | 2004-07-18 21:33:01 +0000 | [diff] [blame] | 3967 | } |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 3968 | }else{ |
drh | d6b7946 | 2015-11-07 01:19:00 +0000 | [diff] [blame] | 3969 | /* For a cursor with the BTREE_SEEK_EQ hint, only the OP_SeekGE and |
| 3970 | ** OP_SeekLE opcodes are allowed, and these must be immediately followed |
| 3971 | ** by an OP_IdxGT or OP_IdxLT opcode, respectively, with the same key. |
| 3972 | */ |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 3973 | if( sqlite3BtreeCursorHasHint(pC->uc.pCursor, BTREE_SEEK_EQ) ){ |
drh | d6b7946 | 2015-11-07 01:19:00 +0000 | [diff] [blame] | 3974 | eqOnly = 1; |
| 3975 | assert( pOp->opcode==OP_SeekGE || pOp->opcode==OP_SeekLE ); |
| 3976 | assert( pOp[1].opcode==OP_IdxLT || pOp[1].opcode==OP_IdxGT ); |
| 3977 | assert( pOp[1].p1==pOp[0].p1 ); |
| 3978 | assert( pOp[1].p2==pOp[0].p2 ); |
| 3979 | assert( pOp[1].p3==pOp[0].p3 ); |
| 3980 | assert( pOp[1].p4.i==pOp[0].p4.i ); |
| 3981 | } |
| 3982 | |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 3983 | nField = pOp->p4.i; |
| 3984 | assert( pOp->p4type==P4_INT32 ); |
| 3985 | assert( nField>0 ); |
| 3986 | r.pKeyInfo = pC->pKeyInfo; |
| 3987 | r.nField = (u16)nField; |
| 3988 | |
| 3989 | /* The next line of code computes as follows, only faster: |
drh | 4a1d365 | 2014-02-14 15:13:36 +0000 | [diff] [blame] | 3990 | ** if( oc==OP_SeekGT || oc==OP_SeekLE ){ |
dan | 1fed5da | 2014-02-25 21:01:25 +0000 | [diff] [blame] | 3991 | ** r.default_rc = -1; |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 3992 | ** }else{ |
dan | 1fed5da | 2014-02-25 21:01:25 +0000 | [diff] [blame] | 3993 | ** r.default_rc = +1; |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 3994 | ** } |
danielk1977 | f7b9d66 | 2008-06-23 18:49:43 +0000 | [diff] [blame] | 3995 | */ |
dan | 1fed5da | 2014-02-25 21:01:25 +0000 | [diff] [blame] | 3996 | r.default_rc = ((1 & (oc - OP_SeekLT)) ? -1 : +1); |
| 3997 | assert( oc!=OP_SeekGT || r.default_rc==-1 ); |
| 3998 | assert( oc!=OP_SeekLE || r.default_rc==-1 ); |
| 3999 | assert( oc!=OP_SeekGE || r.default_rc==+1 ); |
| 4000 | assert( oc!=OP_SeekLT || r.default_rc==+1 ); |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 4001 | |
| 4002 | r.aMem = &aMem[pOp->p3]; |
| 4003 | #ifdef SQLITE_DEBUG |
| 4004 | { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); } |
| 4005 | #endif |
drh | 70528d7 | 2015-11-05 20:25:09 +0000 | [diff] [blame] | 4006 | r.eqSeen = 0; |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 4007 | rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, &r, 0, 0, &res); |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 4008 | if( rc!=SQLITE_OK ){ |
| 4009 | goto abort_due_to_error; |
| 4010 | } |
drh | b1d607d | 2015-11-05 22:30:54 +0000 | [diff] [blame] | 4011 | if( eqOnly && r.eqSeen==0 ){ |
| 4012 | assert( res!=0 ); |
| 4013 | goto seek_not_found; |
drh | 70528d7 | 2015-11-05 20:25:09 +0000 | [diff] [blame] | 4014 | } |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 4015 | } |
| 4016 | pC->deferredMoveto = 0; |
| 4017 | pC->cacheStatus = CACHE_STALE; |
| 4018 | #ifdef SQLITE_TEST |
| 4019 | sqlite3_search_count++; |
| 4020 | #endif |
drh | 4a1d365 | 2014-02-14 15:13:36 +0000 | [diff] [blame] | 4021 | if( oc>=OP_SeekGE ){ assert( oc==OP_SeekGE || oc==OP_SeekGT ); |
| 4022 | if( res<0 || (res==0 && oc==OP_SeekGT) ){ |
drh | e39a732 | 2014-02-03 14:04:11 +0000 | [diff] [blame] | 4023 | res = 0; |
drh | 2ab792e | 2017-05-30 18:34:07 +0000 | [diff] [blame] | 4024 | rc = sqlite3BtreeNext(pC->uc.pCursor, 0); |
| 4025 | if( rc!=SQLITE_OK ){ |
| 4026 | if( rc==SQLITE_DONE ){ |
| 4027 | rc = SQLITE_OK; |
| 4028 | res = 1; |
| 4029 | }else{ |
| 4030 | goto abort_due_to_error; |
| 4031 | } |
| 4032 | } |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 4033 | }else{ |
| 4034 | res = 0; |
| 4035 | } |
| 4036 | }else{ |
drh | 4a1d365 | 2014-02-14 15:13:36 +0000 | [diff] [blame] | 4037 | assert( oc==OP_SeekLT || oc==OP_SeekLE ); |
| 4038 | if( res>0 || (res==0 && oc==OP_SeekLT) ){ |
drh | e39a732 | 2014-02-03 14:04:11 +0000 | [diff] [blame] | 4039 | res = 0; |
drh | 2ab792e | 2017-05-30 18:34:07 +0000 | [diff] [blame] | 4040 | rc = sqlite3BtreePrevious(pC->uc.pCursor, 0); |
| 4041 | if( rc!=SQLITE_OK ){ |
| 4042 | if( rc==SQLITE_DONE ){ |
| 4043 | rc = SQLITE_OK; |
| 4044 | res = 1; |
| 4045 | }else{ |
| 4046 | goto abort_due_to_error; |
| 4047 | } |
| 4048 | } |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 4049 | }else{ |
| 4050 | /* res might be negative because the table is empty. Check to |
| 4051 | ** see if this is the case. |
| 4052 | */ |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 4053 | res = sqlite3BtreeEof(pC->uc.pCursor); |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 4054 | } |
| 4055 | } |
drh | b1d607d | 2015-11-05 22:30:54 +0000 | [diff] [blame] | 4056 | seek_not_found: |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 4057 | assert( pOp->p2>0 ); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 4058 | VdbeBranchTaken(res!=0,2); |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 4059 | if( res ){ |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 4060 | goto jump_to_p2; |
drh | b1d607d | 2015-11-05 22:30:54 +0000 | [diff] [blame] | 4061 | }else if( eqOnly ){ |
| 4062 | assert( pOp[1].opcode==OP_IdxLT || pOp[1].opcode==OP_IdxGT ); |
| 4063 | pOp++; /* Skip the OP_IdxLt or OP_IdxGT that follows */ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4064 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4065 | break; |
| 4066 | } |
dan | 71c57db | 2016-07-09 20:23:55 +0000 | [diff] [blame] | 4067 | |
drh | 8c2b6d7 | 2018-06-05 20:45:20 +0000 | [diff] [blame] | 4068 | /* Opcode: SeekHit P1 P2 * * * |
| 4069 | ** Synopsis: seekHit=P2 |
| 4070 | ** |
| 4071 | ** Set the seekHit flag on cursor P1 to the value in P2. |
| 4072 | ** The seekHit flag is used by the IfNoHope opcode. |
| 4073 | ** |
| 4074 | ** P1 must be a valid b-tree cursor. P2 must be a boolean value, |
| 4075 | ** either 0 or 1. |
| 4076 | */ |
| 4077 | case OP_SeekHit: { |
| 4078 | VdbeCursor *pC; |
| 4079 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 4080 | pC = p->apCsr[pOp->p1]; |
| 4081 | assert( pC!=0 ); |
| 4082 | assert( pOp->p2==0 || pOp->p2==1 ); |
| 4083 | pC->seekHit = pOp->p2 & 1; |
| 4084 | break; |
| 4085 | } |
| 4086 | |
drh | 8cff69d | 2009-11-12 19:59:44 +0000 | [diff] [blame] | 4087 | /* Opcode: Found P1 P2 P3 P4 * |
drh | f63552b | 2013-10-30 00:25:03 +0000 | [diff] [blame] | 4088 | ** Synopsis: key=r[P3@P4] |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4089 | ** |
drh | 8cff69d | 2009-11-12 19:59:44 +0000 | [diff] [blame] | 4090 | ** If P4==0 then register P3 holds a blob constructed by MakeRecord. If |
| 4091 | ** P4>0 then register P3 is the first of P4 registers that form an unpacked |
| 4092 | ** record. |
| 4093 | ** |
| 4094 | ** Cursor P1 is on an index btree. If the record identified by P3 and P4 |
| 4095 | ** 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] | 4096 | ** P1 is left pointing at the matching entry. |
drh | 6f225d0 | 2013-10-26 13:36:51 +0000 | [diff] [blame] | 4097 | ** |
drh | cefc87f | 2014-08-01 01:40:33 +0000 | [diff] [blame] | 4098 | ** This operation leaves the cursor in a state where it can be |
| 4099 | ** advanced in the forward direction. The Next instruction will work, |
| 4100 | ** but not the Prev instruction. |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 4101 | ** |
drh | 6f225d0 | 2013-10-26 13:36:51 +0000 | [diff] [blame] | 4102 | ** See also: NotFound, NoConflict, NotExists. SeekGe |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4103 | */ |
drh | 8cff69d | 2009-11-12 19:59:44 +0000 | [diff] [blame] | 4104 | /* Opcode: NotFound P1 P2 P3 P4 * |
drh | f63552b | 2013-10-30 00:25:03 +0000 | [diff] [blame] | 4105 | ** Synopsis: key=r[P3@P4] |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4106 | ** |
drh | 8cff69d | 2009-11-12 19:59:44 +0000 | [diff] [blame] | 4107 | ** If P4==0 then register P3 holds a blob constructed by MakeRecord. If |
| 4108 | ** P4>0 then register P3 is the first of P4 registers that form an unpacked |
| 4109 | ** record. |
| 4110 | ** |
| 4111 | ** Cursor P1 is on an index btree. If the record identified by P3 and P4 |
| 4112 | ** is not the prefix of any entry in P1 then a jump is made to P2. If P1 |
| 4113 | ** does contain an entry whose prefix matches the P3/P4 record then control |
| 4114 | ** falls through to the next instruction and P1 is left pointing at the |
| 4115 | ** matching entry. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4116 | ** |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 4117 | ** This operation leaves the cursor in a state where it cannot be |
| 4118 | ** advanced in either direction. In other words, the Next and Prev |
| 4119 | ** opcodes do not work after this operation. |
| 4120 | ** |
drh | 8c2b6d7 | 2018-06-05 20:45:20 +0000 | [diff] [blame] | 4121 | ** See also: Found, NotExists, NoConflict, IfNoHope |
| 4122 | */ |
| 4123 | /* Opcode: IfNoHope P1 P2 P3 P4 * |
| 4124 | ** Synopsis: key=r[P3@P4] |
| 4125 | ** |
| 4126 | ** Register P3 is the first of P4 registers that form an unpacked |
| 4127 | ** record. |
| 4128 | ** |
| 4129 | ** Cursor P1 is on an index btree. If the seekHit flag is set on P1, then |
| 4130 | ** this opcode is a no-op. But if the seekHit flag of P1 is clear, then |
| 4131 | ** check to see if there is any entry in P1 that matches the |
| 4132 | ** prefix identified by P3 and P4. If no entry matches the prefix, |
| 4133 | ** jump to P2. Otherwise fall through. |
| 4134 | ** |
| 4135 | ** This opcode behaves like OP_NotFound if the seekHit |
| 4136 | ** flag is clear and it behaves like OP_Noop if the seekHit flag is set. |
| 4137 | ** |
| 4138 | ** This opcode is used in IN clause processing for a multi-column key. |
| 4139 | ** If an IN clause is attached to an element of the key other than the |
| 4140 | ** left-most element, and if there are no matches on the most recent |
| 4141 | ** seek over the whole key, then it might be that one of the key element |
| 4142 | ** to the left is prohibiting a match, and hence there is "no hope" of |
| 4143 | ** any match regardless of how many IN clause elements are checked. |
| 4144 | ** In such a case, we abandon the IN clause search early, using this |
| 4145 | ** opcode. The opcode name comes from the fact that the |
| 4146 | ** jump is taken if there is "no hope" of achieving a match. |
| 4147 | ** |
| 4148 | ** See also: NotFound, SeekHit |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4149 | */ |
drh | 6f225d0 | 2013-10-26 13:36:51 +0000 | [diff] [blame] | 4150 | /* Opcode: NoConflict P1 P2 P3 P4 * |
drh | 4af5bee | 2013-10-30 02:37:50 +0000 | [diff] [blame] | 4151 | ** Synopsis: key=r[P3@P4] |
drh | 6f225d0 | 2013-10-26 13:36:51 +0000 | [diff] [blame] | 4152 | ** |
| 4153 | ** If P4==0 then register P3 holds a blob constructed by MakeRecord. If |
| 4154 | ** P4>0 then register P3 is the first of P4 registers that form an unpacked |
| 4155 | ** record. |
| 4156 | ** |
| 4157 | ** Cursor P1 is on an index btree. If the record identified by P3 and P4 |
| 4158 | ** contains any NULL value, jump immediately to P2. If all terms of the |
| 4159 | ** record are not-NULL then a check is done to determine if any row in the |
| 4160 | ** P1 index btree has a matching key prefix. If there are no matches, jump |
| 4161 | ** immediately to P2. If there is a match, fall through and leave the P1 |
| 4162 | ** cursor pointing to the matching row. |
| 4163 | ** |
| 4164 | ** This opcode is similar to OP_NotFound with the exceptions that the |
| 4165 | ** branch is always taken if any part of the search key input is NULL. |
| 4166 | ** |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 4167 | ** This operation leaves the cursor in a state where it cannot be |
| 4168 | ** advanced in either direction. In other words, the Next and Prev |
| 4169 | ** opcodes do not work after this operation. |
| 4170 | ** |
drh | 6f225d0 | 2013-10-26 13:36:51 +0000 | [diff] [blame] | 4171 | ** See also: NotFound, Found, NotExists |
| 4172 | */ |
drh | 8c2b6d7 | 2018-06-05 20:45:20 +0000 | [diff] [blame] | 4173 | case OP_IfNoHope: { /* jump, in3 */ |
| 4174 | VdbeCursor *pC; |
| 4175 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 4176 | pC = p->apCsr[pOp->p1]; |
| 4177 | assert( pC!=0 ); |
| 4178 | if( pC->seekHit ) break; |
| 4179 | /* Fall through into OP_NotFound */ |
| 4180 | } |
drh | 6f225d0 | 2013-10-26 13:36:51 +0000 | [diff] [blame] | 4181 | case OP_NoConflict: /* jump, in3 */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4182 | case OP_NotFound: /* jump, in3 */ |
| 4183 | case OP_Found: { /* jump, in3 */ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 4184 | int alreadyExists; |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 4185 | int takeJump; |
drh | 6f225d0 | 2013-10-26 13:36:51 +0000 | [diff] [blame] | 4186 | int ii; |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 4187 | VdbeCursor *pC; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 4188 | int res; |
drh | a582b01 | 2016-12-21 19:45:54 +0000 | [diff] [blame] | 4189 | UnpackedRecord *pFree; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 4190 | UnpackedRecord *pIdxKey; |
drh | 8cff69d | 2009-11-12 19:59:44 +0000 | [diff] [blame] | 4191 | UnpackedRecord r; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 4192 | |
dan | 0ff297e | 2009-09-25 17:03:14 +0000 | [diff] [blame] | 4193 | #ifdef SQLITE_TEST |
drh | 6f225d0 | 2013-10-26 13:36:51 +0000 | [diff] [blame] | 4194 | if( pOp->opcode!=OP_NoConflict ) sqlite3_found_count++; |
dan | 0ff297e | 2009-09-25 17:03:14 +0000 | [diff] [blame] | 4195 | #endif |
| 4196 | |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 4197 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
drh | 8cff69d | 2009-11-12 19:59:44 +0000 | [diff] [blame] | 4198 | assert( pOp->p4type==P4_INT32 ); |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 4199 | pC = p->apCsr[pOp->p1]; |
| 4200 | assert( pC!=0 ); |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 4201 | #ifdef SQLITE_DEBUG |
drh | cefc87f | 2014-08-01 01:40:33 +0000 | [diff] [blame] | 4202 | pC->seekOp = pOp->opcode; |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 4203 | #endif |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 4204 | pIn3 = &aMem[pOp->p3]; |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 4205 | assert( pC->eCurType==CURTYPE_BTREE ); |
| 4206 | assert( pC->uc.pCursor!=0 ); |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 4207 | assert( pC->isTable==0 ); |
| 4208 | if( pOp->p4.i>0 ){ |
| 4209 | r.pKeyInfo = pC->pKeyInfo; |
| 4210 | r.nField = (u16)pOp->p4.i; |
| 4211 | r.aMem = pIn3; |
drh | 8aaf7bc | 2016-09-20 01:19:18 +0000 | [diff] [blame] | 4212 | #ifdef SQLITE_DEBUG |
drh | 826af37 | 2014-02-08 19:12:21 +0000 | [diff] [blame] | 4213 | for(ii=0; ii<r.nField; ii++){ |
| 4214 | assert( memIsValid(&r.aMem[ii]) ); |
drh | 8aaf7bc | 2016-09-20 01:19:18 +0000 | [diff] [blame] | 4215 | assert( (r.aMem[ii].flags & MEM_Zero)==0 || r.aMem[ii].n==0 ); |
drh | 826af37 | 2014-02-08 19:12:21 +0000 | [diff] [blame] | 4216 | if( ii ) REGISTER_TRACE(pOp->p3+ii, &r.aMem[ii]); |
drh | 826af37 | 2014-02-08 19:12:21 +0000 | [diff] [blame] | 4217 | } |
drh | 8aaf7bc | 2016-09-20 01:19:18 +0000 | [diff] [blame] | 4218 | #endif |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 4219 | pIdxKey = &r; |
drh | a582b01 | 2016-12-21 19:45:54 +0000 | [diff] [blame] | 4220 | pFree = 0; |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 4221 | }else{ |
drh | e46515b | 2017-05-19 22:51:00 +0000 | [diff] [blame] | 4222 | assert( pIn3->flags & MEM_Blob ); |
| 4223 | rc = ExpandBlob(pIn3); |
| 4224 | assert( rc==SQLITE_OK || rc==SQLITE_NOMEM ); |
| 4225 | if( rc ) goto no_mem; |
drh | a582b01 | 2016-12-21 19:45:54 +0000 | [diff] [blame] | 4226 | pFree = pIdxKey = sqlite3VdbeAllocUnpackedRecord(pC->pKeyInfo); |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 4227 | if( pIdxKey==0 ) goto no_mem; |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 4228 | sqlite3VdbeRecordUnpack(pC->pKeyInfo, pIn3->n, pIn3->z, pIdxKey); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4229 | } |
dan | 1fed5da | 2014-02-25 21:01:25 +0000 | [diff] [blame] | 4230 | pIdxKey->default_rc = 0; |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 4231 | takeJump = 0; |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 4232 | if( pOp->opcode==OP_NoConflict ){ |
| 4233 | /* For the OP_NoConflict opcode, take the jump if any of the |
| 4234 | ** input fields are NULL, since any key with a NULL will not |
| 4235 | ** conflict */ |
mistachkin | 7bb6e8e | 2015-01-12 18:52:41 +0000 | [diff] [blame] | 4236 | for(ii=0; ii<pIdxKey->nField; ii++){ |
| 4237 | if( pIdxKey->aMem[ii].flags & MEM_Null ){ |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 4238 | takeJump = 1; |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 4239 | break; |
drh | 6f225d0 | 2013-10-26 13:36:51 +0000 | [diff] [blame] | 4240 | } |
| 4241 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4242 | } |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 4243 | rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, pIdxKey, 0, 0, &res); |
drh | dbd6a7d | 2017-04-05 12:39:49 +0000 | [diff] [blame] | 4244 | if( pFree ) sqlite3DbFreeNN(db, pFree); |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 4245 | if( rc!=SQLITE_OK ){ |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 4246 | goto abort_due_to_error; |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 4247 | } |
| 4248 | pC->seekResult = res; |
| 4249 | alreadyExists = (res==0); |
| 4250 | pC->nullRow = 1-alreadyExists; |
| 4251 | pC->deferredMoveto = 0; |
| 4252 | pC->cacheStatus = CACHE_STALE; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4253 | if( pOp->opcode==OP_Found ){ |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 4254 | VdbeBranchTaken(alreadyExists!=0,2); |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 4255 | if( alreadyExists ) goto jump_to_p2; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4256 | }else{ |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 4257 | VdbeBranchTaken(takeJump||alreadyExists==0,2); |
| 4258 | if( takeJump || !alreadyExists ) goto jump_to_p2; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4259 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4260 | break; |
| 4261 | } |
| 4262 | |
drh | eeb9565 | 2016-05-26 20:56:38 +0000 | [diff] [blame] | 4263 | /* Opcode: SeekRowid P1 P2 P3 * * |
| 4264 | ** Synopsis: intkey=r[P3] |
| 4265 | ** |
| 4266 | ** P1 is the index of a cursor open on an SQL table btree (with integer |
| 4267 | ** keys). If register P3 does not contain an integer or if P1 does not |
| 4268 | ** contain a record with rowid P3 then jump immediately to P2. |
| 4269 | ** Or, if P2 is 0, raise an SQLITE_CORRUPT error. If P1 does contain |
| 4270 | ** a record with rowid P3 then |
| 4271 | ** leave the cursor pointing at that record and fall through to the next |
| 4272 | ** instruction. |
| 4273 | ** |
| 4274 | ** The OP_NotExists opcode performs the same operation, but with OP_NotExists |
| 4275 | ** the P3 register must be guaranteed to contain an integer value. With this |
| 4276 | ** opcode, register P3 might not contain an integer. |
| 4277 | ** |
| 4278 | ** The OP_NotFound opcode performs the same operation on index btrees |
| 4279 | ** (with arbitrary multi-value keys). |
| 4280 | ** |
| 4281 | ** This opcode leaves the cursor in a state where it cannot be advanced |
| 4282 | ** in either direction. In other words, the Next and Prev opcodes will |
| 4283 | ** not work following this opcode. |
| 4284 | ** |
| 4285 | ** See also: Found, NotFound, NoConflict, SeekRowid |
| 4286 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4287 | /* Opcode: NotExists P1 P2 P3 * * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 4288 | ** Synopsis: intkey=r[P3] |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 4289 | ** |
drh | 261c02d | 2013-10-25 14:46:15 +0000 | [diff] [blame] | 4290 | ** P1 is the index of a cursor open on an SQL table btree (with integer |
| 4291 | ** keys). P3 is an integer rowid. If P1 does not contain a record with |
dan | c6157e1 | 2015-09-14 09:23:47 +0000 | [diff] [blame] | 4292 | ** rowid P3 then jump immediately to P2. Or, if P2 is 0, raise an |
| 4293 | ** SQLITE_CORRUPT error. If P1 does contain a record with rowid P3 then |
| 4294 | ** leave the cursor pointing at that record and fall through to the next |
| 4295 | ** instruction. |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 4296 | ** |
drh | eeb9565 | 2016-05-26 20:56:38 +0000 | [diff] [blame] | 4297 | ** The OP_SeekRowid opcode performs the same operation but also allows the |
| 4298 | ** P3 register to contain a non-integer value, in which case the jump is |
| 4299 | ** always taken. This opcode requires that P3 always contain an integer. |
| 4300 | ** |
drh | 261c02d | 2013-10-25 14:46:15 +0000 | [diff] [blame] | 4301 | ** The OP_NotFound opcode performs the same operation on index btrees |
| 4302 | ** (with arbitrary multi-value keys). |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 4303 | ** |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 4304 | ** This opcode leaves the cursor in a state where it cannot be advanced |
| 4305 | ** in either direction. In other words, the Next and Prev opcodes will |
| 4306 | ** not work following this opcode. |
| 4307 | ** |
drh | eeb9565 | 2016-05-26 20:56:38 +0000 | [diff] [blame] | 4308 | ** See also: Found, NotFound, NoConflict, SeekRowid |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 4309 | */ |
drh | eeb9565 | 2016-05-26 20:56:38 +0000 | [diff] [blame] | 4310 | case OP_SeekRowid: { /* jump, in3 */ |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 4311 | VdbeCursor *pC; |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 4312 | BtCursor *pCrsr; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 4313 | int res; |
| 4314 | u64 iKey; |
| 4315 | |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 4316 | pIn3 = &aMem[pOp->p3]; |
drh | eeb9565 | 2016-05-26 20:56:38 +0000 | [diff] [blame] | 4317 | if( (pIn3->flags & MEM_Int)==0 ){ |
drh | e4fe6d4 | 2018-08-03 15:58:07 +0000 | [diff] [blame] | 4318 | /* Make sure pIn3->u.i contains a valid integer representation of |
| 4319 | ** the key value, but do not change the datatype of the register, as |
| 4320 | ** other parts of the perpared statement might be depending on the |
| 4321 | ** current datatype. */ |
| 4322 | u16 origFlags = pIn3->flags; |
| 4323 | int isNotInt; |
drh | eeb9565 | 2016-05-26 20:56:38 +0000 | [diff] [blame] | 4324 | applyAffinity(pIn3, SQLITE_AFF_NUMERIC, encoding); |
drh | e4fe6d4 | 2018-08-03 15:58:07 +0000 | [diff] [blame] | 4325 | isNotInt = (pIn3->flags & MEM_Int)==0; |
| 4326 | pIn3->flags = origFlags; |
| 4327 | if( isNotInt ) goto jump_to_p2; |
drh | eeb9565 | 2016-05-26 20:56:38 +0000 | [diff] [blame] | 4328 | } |
| 4329 | /* Fall through into OP_NotExists */ |
| 4330 | case OP_NotExists: /* jump, in3 */ |
| 4331 | pIn3 = &aMem[pOp->p3]; |
drh | e4fe6d4 | 2018-08-03 15:58:07 +0000 | [diff] [blame] | 4332 | assert( (pIn3->flags & MEM_Int)!=0 || pOp->opcode==OP_SeekRowid ); |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 4333 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 4334 | pC = p->apCsr[pOp->p1]; |
| 4335 | assert( pC!=0 ); |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 4336 | #ifdef SQLITE_DEBUG |
drh | cf025a8 | 2018-06-07 18:01:21 +0000 | [diff] [blame] | 4337 | pC->seekOp = OP_SeekRowid; |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 4338 | #endif |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 4339 | assert( pC->isTable ); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 4340 | assert( pC->eCurType==CURTYPE_BTREE ); |
| 4341 | pCrsr = pC->uc.pCursor; |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 4342 | assert( pCrsr!=0 ); |
| 4343 | res = 0; |
| 4344 | iKey = pIn3->u.i; |
| 4345 | rc = sqlite3BtreeMovetoUnpacked(pCrsr, 0, iKey, 0, &res); |
drh | b79d552 | 2015-09-14 19:26:37 +0000 | [diff] [blame] | 4346 | assert( rc==SQLITE_OK || res==0 ); |
drh | b53a5a9 | 2014-10-12 22:37:22 +0000 | [diff] [blame] | 4347 | pC->movetoTarget = iKey; /* Used by OP_Delete */ |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 4348 | pC->nullRow = 0; |
| 4349 | pC->cacheStatus = CACHE_STALE; |
| 4350 | pC->deferredMoveto = 0; |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 4351 | VdbeBranchTaken(res!=0,2); |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 4352 | pC->seekResult = res; |
dan | c6157e1 | 2015-09-14 09:23:47 +0000 | [diff] [blame] | 4353 | if( res!=0 ){ |
drh | b79d552 | 2015-09-14 19:26:37 +0000 | [diff] [blame] | 4354 | assert( rc==SQLITE_OK ); |
| 4355 | if( pOp->p2==0 ){ |
| 4356 | rc = SQLITE_CORRUPT_BKPT; |
| 4357 | }else{ |
| 4358 | goto jump_to_p2; |
| 4359 | } |
dan | c6157e1 | 2015-09-14 09:23:47 +0000 | [diff] [blame] | 4360 | } |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 4361 | if( rc ) goto abort_due_to_error; |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 4362 | break; |
| 4363 | } |
| 4364 | |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 4365 | /* Opcode: Sequence P1 P2 * * * |
drh | 079a307 | 2014-03-19 14:10:55 +0000 | [diff] [blame] | 4366 | ** Synopsis: r[P2]=cursor[P1].ctr++ |
drh | 4db38a7 | 2005-09-01 12:16:28 +0000 | [diff] [blame] | 4367 | ** |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 4368 | ** Find the next available sequence number for cursor P1. |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4369 | ** Write the sequence number into register P2. |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 4370 | ** The sequence number on the cursor is incremented after this |
| 4371 | ** instruction. |
drh | 4db38a7 | 2005-09-01 12:16:28 +0000 | [diff] [blame] | 4372 | */ |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 4373 | case OP_Sequence: { /* out2 */ |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 4374 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 4375 | assert( p->apCsr[pOp->p1]!=0 ); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 4376 | assert( p->apCsr[pOp->p1]->eCurType!=CURTYPE_VTAB ); |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 4377 | pOut = out2Prerelease(p, pOp); |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 4378 | pOut->u.i = p->apCsr[pOp->p1]->seqCount++; |
drh | 4db38a7 | 2005-09-01 12:16:28 +0000 | [diff] [blame] | 4379 | break; |
| 4380 | } |
| 4381 | |
| 4382 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4383 | /* Opcode: NewRowid P1 P2 P3 * * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 4384 | ** Synopsis: r[P2]=rowid |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4385 | ** |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 4386 | ** 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] | 4387 | ** The record number is not previously used as a key in the database |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4388 | ** table that cursor P1 points to. The new record number is written |
| 4389 | ** written to register P2. |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 4390 | ** |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 4391 | ** If P3>0 then P3 is a register in the root frame of this VDBE that holds |
| 4392 | ** the largest previously generated record number. No new record numbers are |
| 4393 | ** allowed to be less than this value. When this value reaches its maximum, |
drh | ef8662b | 2011-06-20 21:47:58 +0000 | [diff] [blame] | 4394 | ** an SQLITE_FULL error is generated. The P3 register is updated with the ' |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 4395 | ** generated record number. This P3 mechanism is used to help implement the |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 4396 | ** AUTOINCREMENT feature. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4397 | */ |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 4398 | case OP_NewRowid: { /* out2 */ |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 4399 | i64 v; /* The new rowid */ |
| 4400 | VdbeCursor *pC; /* Cursor of table to get the new rowid */ |
| 4401 | int res; /* Result of an sqlite3BtreeLast() */ |
| 4402 | int cnt; /* Counter to limit the number of searches */ |
| 4403 | Mem *pMem; /* Register holding largest rowid for AUTOINCREMENT */ |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 4404 | VdbeFrame *pFrame; /* Root frame of VDBE */ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 4405 | |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 4406 | v = 0; |
| 4407 | res = 0; |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 4408 | pOut = out2Prerelease(p, pOp); |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 4409 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 4410 | pC = p->apCsr[pOp->p1]; |
| 4411 | assert( pC!=0 ); |
drh | 4c57e32 | 2018-05-23 17:53:07 +0000 | [diff] [blame] | 4412 | assert( pC->isTable ); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 4413 | assert( pC->eCurType==CURTYPE_BTREE ); |
| 4414 | assert( pC->uc.pCursor!=0 ); |
drh | 98ef0f6 | 2015-06-30 01:25:52 +0000 | [diff] [blame] | 4415 | { |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 4416 | /* The next rowid or record number (different terms for the same |
| 4417 | ** thing) is obtained in a two-step algorithm. |
| 4418 | ** |
| 4419 | ** First we attempt to find the largest existing rowid and add one |
| 4420 | ** to that. But if the largest existing rowid is already the maximum |
| 4421 | ** positive integer, we have to fall through to the second |
| 4422 | ** probabilistic algorithm |
| 4423 | ** |
| 4424 | ** The second algorithm is to select a rowid at random and see if |
| 4425 | ** it already exists in the table. If it does not exist, we have |
| 4426 | ** succeeded. If the random rowid does exist, we select a new one |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 4427 | ** and try again, up to 100 times. |
drh | db5ed6d | 2001-09-18 22:17:44 +0000 | [diff] [blame] | 4428 | */ |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 4429 | assert( pC->isTable ); |
drh | fe2093d | 2005-01-20 22:48:47 +0000 | [diff] [blame] | 4430 | |
drh | 75f86a4 | 2005-02-17 00:03:06 +0000 | [diff] [blame] | 4431 | #ifdef SQLITE_32BIT_ROWID |
| 4432 | # define MAX_ROWID 0x7fffffff |
| 4433 | #else |
drh | fe2093d | 2005-01-20 22:48:47 +0000 | [diff] [blame] | 4434 | /* Some compilers complain about constants of the form 0x7fffffffffffffff. |
| 4435 | ** Others complain about 0x7ffffffffffffffffLL. The following macro seems |
| 4436 | ** to provide the constant while making all compilers happy. |
| 4437 | */ |
danielk1977 | 64202cf | 2008-11-17 15:31:47 +0000 | [diff] [blame] | 4438 | # define MAX_ROWID (i64)( (((u64)0x7fffffff)<<32) | (u64)0xffffffff ) |
drh | 75f86a4 | 2005-02-17 00:03:06 +0000 | [diff] [blame] | 4439 | #endif |
drh | fe2093d | 2005-01-20 22:48:47 +0000 | [diff] [blame] | 4440 | |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 4441 | if( !pC->useRandomRowid ){ |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 4442 | rc = sqlite3BtreeLast(pC->uc.pCursor, &res); |
drh | e0670b6 | 2014-02-12 21:31:12 +0000 | [diff] [blame] | 4443 | if( rc!=SQLITE_OK ){ |
| 4444 | goto abort_due_to_error; |
| 4445 | } |
| 4446 | if( res ){ |
| 4447 | v = 1; /* IMP: R-61914-48074 */ |
| 4448 | }else{ |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 4449 | assert( sqlite3BtreeCursorIsValid(pC->uc.pCursor) ); |
drh | a7c90c4 | 2016-06-04 20:37:10 +0000 | [diff] [blame] | 4450 | v = sqlite3BtreeIntegerKey(pC->uc.pCursor); |
drh | e0670b6 | 2014-02-12 21:31:12 +0000 | [diff] [blame] | 4451 | if( v>=MAX_ROWID ){ |
| 4452 | pC->useRandomRowid = 1; |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 4453 | }else{ |
drh | e0670b6 | 2014-02-12 21:31:12 +0000 | [diff] [blame] | 4454 | v++; /* IMP: R-29538-34987 */ |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 4455 | } |
drh | 3fc190c | 2001-09-14 03:24:23 +0000 | [diff] [blame] | 4456 | } |
drh | e0670b6 | 2014-02-12 21:31:12 +0000 | [diff] [blame] | 4457 | } |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 4458 | |
| 4459 | #ifndef SQLITE_OMIT_AUTOINCREMENT |
drh | e0670b6 | 2014-02-12 21:31:12 +0000 | [diff] [blame] | 4460 | if( pOp->p3 ){ |
| 4461 | /* Assert that P3 is a valid memory cell. */ |
| 4462 | assert( pOp->p3>0 ); |
| 4463 | if( p->pFrame ){ |
| 4464 | for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent); |
shane | abc6b89 | 2009-09-10 19:09:03 +0000 | [diff] [blame] | 4465 | /* Assert that P3 is a valid memory cell. */ |
drh | e0670b6 | 2014-02-12 21:31:12 +0000 | [diff] [blame] | 4466 | assert( pOp->p3<=pFrame->nMem ); |
| 4467 | pMem = &pFrame->aMem[pOp->p3]; |
| 4468 | }else{ |
| 4469 | /* Assert that P3 is a valid memory cell. */ |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 4470 | assert( pOp->p3<=(p->nMem+1 - p->nCursor) ); |
drh | e0670b6 | 2014-02-12 21:31:12 +0000 | [diff] [blame] | 4471 | pMem = &aMem[pOp->p3]; |
| 4472 | memAboutToChange(p, pMem); |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 4473 | } |
drh | e0670b6 | 2014-02-12 21:31:12 +0000 | [diff] [blame] | 4474 | assert( memIsValid(pMem) ); |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 4475 | |
drh | e0670b6 | 2014-02-12 21:31:12 +0000 | [diff] [blame] | 4476 | REGISTER_TRACE(pOp->p3, pMem); |
| 4477 | sqlite3VdbeMemIntegerify(pMem); |
| 4478 | assert( (pMem->flags & MEM_Int)!=0 ); /* mem(P3) holds an integer */ |
| 4479 | if( pMem->u.i==MAX_ROWID || pC->useRandomRowid ){ |
drh | e77caa1 | 2016-11-02 13:18:46 +0000 | [diff] [blame] | 4480 | rc = SQLITE_FULL; /* IMP: R-17817-00630 */ |
drh | e0670b6 | 2014-02-12 21:31:12 +0000 | [diff] [blame] | 4481 | goto abort_due_to_error; |
| 4482 | } |
| 4483 | if( v<pMem->u.i+1 ){ |
| 4484 | v = pMem->u.i + 1; |
| 4485 | } |
| 4486 | pMem->u.i = v; |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 4487 | } |
drh | e0670b6 | 2014-02-12 21:31:12 +0000 | [diff] [blame] | 4488 | #endif |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 4489 | if( pC->useRandomRowid ){ |
drh | 748a52c | 2010-09-01 11:50:08 +0000 | [diff] [blame] | 4490 | /* IMPLEMENTATION-OF: R-07677-41881 If the largest ROWID is equal to the |
drh | c79c761 | 2010-01-01 18:57:48 +0000 | [diff] [blame] | 4491 | ** largest possible integer (9223372036854775807) then the database |
drh | 748a52c | 2010-09-01 11:50:08 +0000 | [diff] [blame] | 4492 | ** engine starts picking positive candidate ROWIDs at random until |
| 4493 | ** it finds one that is not previously used. */ |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 4494 | assert( pOp->p3==0 ); /* We cannot be in random rowid mode if this is |
| 4495 | ** an AUTOINCREMENT table. */ |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 4496 | cnt = 0; |
drh | 2c4dc63 | 2014-09-25 12:31:28 +0000 | [diff] [blame] | 4497 | do{ |
| 4498 | sqlite3_randomness(sizeof(v), &v); |
drh | d863346 | 2014-09-25 17:42:41 +0000 | [diff] [blame] | 4499 | v &= (MAX_ROWID>>1); v++; /* Ensure that v is greater than zero */ |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 4500 | }while( ((rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, 0, (u64)v, |
drh | 748a52c | 2010-09-01 11:50:08 +0000 | [diff] [blame] | 4501 | 0, &res))==SQLITE_OK) |
shaneh | c4d340a | 2010-09-01 02:37:56 +0000 | [diff] [blame] | 4502 | && (res==0) |
drh | 2c4dc63 | 2014-09-25 12:31:28 +0000 | [diff] [blame] | 4503 | && (++cnt<100)); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 4504 | if( rc ) goto abort_due_to_error; |
| 4505 | if( res==0 ){ |
drh | c79c761 | 2010-01-01 18:57:48 +0000 | [diff] [blame] | 4506 | rc = SQLITE_FULL; /* IMP: R-38219-53002 */ |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 4507 | goto abort_due_to_error; |
| 4508 | } |
drh | 748a52c | 2010-09-01 11:50:08 +0000 | [diff] [blame] | 4509 | assert( v>0 ); /* EV: R-40812-03570 */ |
drh | 1eaa269 | 2001-09-18 02:02:23 +0000 | [diff] [blame] | 4510 | } |
drh | a11846b | 2004-01-07 18:52:56 +0000 | [diff] [blame] | 4511 | pC->deferredMoveto = 0; |
drh | 76873ab | 2006-01-07 18:48:26 +0000 | [diff] [blame] | 4512 | pC->cacheStatus = CACHE_STALE; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4513 | } |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 4514 | pOut->u.i = v; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4515 | break; |
| 4516 | } |
| 4517 | |
danielk1977 | 1f4aa33 | 2008-01-03 09:51:55 +0000 | [diff] [blame] | 4518 | /* Opcode: Insert P1 P2 P3 P4 P5 |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 4519 | ** Synopsis: intkey=r[P3] data=r[P2] |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4520 | ** |
jplyon | 5a56422 | 2003-06-02 06:15:58 +0000 | [diff] [blame] | 4521 | ** Write an entry into the table of cursor P1. A new entry is |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 4522 | ** created if it doesn't already exist or the data for an existing |
drh | 3e9ca09 | 2009-09-08 01:14:48 +0000 | [diff] [blame] | 4523 | ** entry is overwritten. The data is the value MEM_Blob stored in register |
danielk1977 | 1f4aa33 | 2008-01-03 09:51:55 +0000 | [diff] [blame] | 4524 | ** number P2. The key is stored in register P3. The key must |
drh | 3e9ca09 | 2009-09-08 01:14:48 +0000 | [diff] [blame] | 4525 | ** be a MEM_Int. |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 4526 | ** |
danielk1977 | 1f4aa33 | 2008-01-03 09:51:55 +0000 | [diff] [blame] | 4527 | ** If the OPFLAG_NCHANGE flag of P5 is set, then the row change count is |
| 4528 | ** incremented (otherwise not). If the OPFLAG_LASTROWID flag of P5 is set, |
danielk1977 | b28af71 | 2004-06-21 06:50:26 +0000 | [diff] [blame] | 4529 | ** then rowid is stored for subsequent return by the |
drh | 85b623f | 2007-12-13 21:54:09 +0000 | [diff] [blame] | 4530 | ** sqlite3_last_insert_rowid() function (otherwise it is unmodified). |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 4531 | ** |
drh | eaf6ae2 | 2016-11-09 20:14:34 +0000 | [diff] [blame] | 4532 | ** If the OPFLAG_USESEEKRESULT flag of P5 is set, the implementation might |
| 4533 | ** run faster by avoiding an unnecessary seek on cursor P1. However, |
| 4534 | ** the OPFLAG_USESEEKRESULT flag must only be set if there have been no prior |
| 4535 | ** seeks on the cursor or if the most recent seek used a key equal to P3. |
drh | 3e9ca09 | 2009-09-08 01:14:48 +0000 | [diff] [blame] | 4536 | ** |
| 4537 | ** If the OPFLAG_ISUPDATE flag is set, then this opcode is part of an |
| 4538 | ** UPDATE operation. Otherwise (if the flag is clear) then this opcode |
| 4539 | ** is part of an INSERT operation. The difference is only important to |
| 4540 | ** the update hook. |
| 4541 | ** |
dan | 319eeb7 | 2011-03-19 08:38:50 +0000 | [diff] [blame] | 4542 | ** Parameter P4 may point to a Table structure, or may be NULL. If it is |
| 4543 | ** not NULL, then the update-hook (sqlite3.xUpdateCallback) is invoked |
| 4544 | ** following a successful insert. |
danielk1977 | 1f6eec5 | 2006-06-16 06:17:47 +0000 | [diff] [blame] | 4545 | ** |
drh | 93aed5a | 2008-01-16 17:46:38 +0000 | [diff] [blame] | 4546 | ** (WARNING/TODO: If P1 is a pseudo-cursor and P2 is dynamically |
| 4547 | ** allocated, then ownership of P2 is transferred to the pseudo-cursor |
| 4548 | ** and register P2 becomes ephemeral. If the cursor is changed, the |
| 4549 | ** value of register P2 will then change. Make sure this does not |
| 4550 | ** cause any problems.) |
| 4551 | ** |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 4552 | ** This instruction only works on tables. The equivalent instruction |
| 4553 | ** for indices is OP_IdxInsert. |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 4554 | */ |
drh | e05c929 | 2009-10-29 13:48:10 +0000 | [diff] [blame] | 4555 | /* Opcode: InsertInt P1 P2 P3 P4 P5 |
drh | 72e26de | 2016-08-24 21:24:04 +0000 | [diff] [blame] | 4556 | ** Synopsis: intkey=P3 data=r[P2] |
drh | e05c929 | 2009-10-29 13:48:10 +0000 | [diff] [blame] | 4557 | ** |
| 4558 | ** This works exactly like OP_Insert except that the key is the |
| 4559 | ** integer value P3, not the value of the integer stored in register P3. |
| 4560 | */ |
| 4561 | case OP_Insert: |
| 4562 | case OP_InsertInt: { |
drh | 3e9ca09 | 2009-09-08 01:14:48 +0000 | [diff] [blame] | 4563 | Mem *pData; /* MEM cell holding data for the record to be inserted */ |
| 4564 | Mem *pKey; /* MEM cell holding key for the record */ |
drh | 3e9ca09 | 2009-09-08 01:14:48 +0000 | [diff] [blame] | 4565 | VdbeCursor *pC; /* Cursor to table into which insert is written */ |
drh | 3e9ca09 | 2009-09-08 01:14:48 +0000 | [diff] [blame] | 4566 | int seekResult; /* Result of prior seek or 0 if no USESEEKRESULT flag */ |
| 4567 | const char *zDb; /* database name - used by the update hook */ |
dan | 319eeb7 | 2011-03-19 08:38:50 +0000 | [diff] [blame] | 4568 | Table *pTab; /* Table structure - used by update and pre-update hooks */ |
drh | 8eeb446 | 2016-05-21 20:03:42 +0000 | [diff] [blame] | 4569 | BtreePayload x; /* Payload to be inserted */ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 4570 | |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 4571 | pData = &aMem[pOp->p2]; |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 4572 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 4573 | assert( memIsValid(pData) ); |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 4574 | pC = p->apCsr[pOp->p1]; |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 4575 | assert( pC!=0 ); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 4576 | assert( pC->eCurType==CURTYPE_BTREE ); |
| 4577 | assert( pC->uc.pCursor!=0 ); |
dan | cb9a364 | 2017-01-30 19:44:53 +0000 | [diff] [blame] | 4578 | assert( (pOp->p5 & OPFLAG_ISNOOP) || pC->isTable ); |
drh | cbf1b8e | 2013-11-11 22:55:26 +0000 | [diff] [blame] | 4579 | assert( pOp->p4type==P4_TABLE || pOp->p4type>=P4_STATIC ); |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 4580 | REGISTER_TRACE(pOp->p2, pData); |
drh | 4031baf | 2018-05-28 17:31:20 +0000 | [diff] [blame] | 4581 | sqlite3VdbeIncrWriteCounter(p, pC); |
danielk1977 | 5f8d8a8 | 2004-05-11 00:28:42 +0000 | [diff] [blame] | 4582 | |
drh | e05c929 | 2009-10-29 13:48:10 +0000 | [diff] [blame] | 4583 | if( pOp->opcode==OP_Insert ){ |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 4584 | pKey = &aMem[pOp->p3]; |
drh | e05c929 | 2009-10-29 13:48:10 +0000 | [diff] [blame] | 4585 | assert( pKey->flags & MEM_Int ); |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 4586 | assert( memIsValid(pKey) ); |
drh | e05c929 | 2009-10-29 13:48:10 +0000 | [diff] [blame] | 4587 | REGISTER_TRACE(pOp->p3, pKey); |
drh | 8eeb446 | 2016-05-21 20:03:42 +0000 | [diff] [blame] | 4588 | x.nKey = pKey->u.i; |
drh | e05c929 | 2009-10-29 13:48:10 +0000 | [diff] [blame] | 4589 | }else{ |
| 4590 | assert( pOp->opcode==OP_InsertInt ); |
drh | 8eeb446 | 2016-05-21 20:03:42 +0000 | [diff] [blame] | 4591 | x.nKey = pOp->p3; |
drh | e05c929 | 2009-10-29 13:48:10 +0000 | [diff] [blame] | 4592 | } |
| 4593 | |
drh | 9b1c62d | 2011-03-30 21:04:43 +0000 | [diff] [blame] | 4594 | if( pOp->p4type==P4_TABLE && HAS_UPDATE_HOOK(db) ){ |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 4595 | assert( pC->iDb>=0 ); |
drh | 69c3382 | 2016-08-18 14:33:11 +0000 | [diff] [blame] | 4596 | zDb = db->aDb[pC->iDb].zDbSName; |
dan | 319eeb7 | 2011-03-19 08:38:50 +0000 | [diff] [blame] | 4597 | pTab = pOp->p4.pTab; |
dan | cb9a364 | 2017-01-30 19:44:53 +0000 | [diff] [blame] | 4598 | assert( (pOp->p5 & OPFLAG_ISNOOP) || HasRowid(pTab) ); |
drh | 74c3302 | 2016-03-30 12:56:55 +0000 | [diff] [blame] | 4599 | }else{ |
drh | 4ec6f3a | 2018-01-12 19:33:18 +0000 | [diff] [blame] | 4600 | pTab = 0; |
drh | 74c3302 | 2016-03-30 12:56:55 +0000 | [diff] [blame] | 4601 | zDb = 0; /* Not needed. Silence a compiler warning. */ |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 4602 | } |
| 4603 | |
drh | 9b1c62d | 2011-03-30 21:04:43 +0000 | [diff] [blame] | 4604 | #ifdef SQLITE_ENABLE_PREUPDATE_HOOK |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 4605 | /* Invoke the pre-update hook, if any */ |
drh | 4ec6f3a | 2018-01-12 19:33:18 +0000 | [diff] [blame] | 4606 | if( pTab ){ |
drh | 84ebe2b | 2018-01-12 18:46:52 +0000 | [diff] [blame] | 4607 | if( db->xPreUpdateCallback && !(pOp->p5 & OPFLAG_ISUPDATE) ){ |
| 4608 | sqlite3VdbePreUpdateHook(p, pC, SQLITE_INSERT, zDb, pTab, x.nKey,pOp->p2); |
| 4609 | } |
drh | 4ec6f3a | 2018-01-12 19:33:18 +0000 | [diff] [blame] | 4610 | if( db->xUpdateCallback==0 || pTab->aCol==0 ){ |
| 4611 | /* Prevent post-update hook from running in cases when it should not */ |
| 4612 | pTab = 0; |
drh | 84ebe2b | 2018-01-12 18:46:52 +0000 | [diff] [blame] | 4613 | } |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 4614 | } |
dan | cb9a364 | 2017-01-30 19:44:53 +0000 | [diff] [blame] | 4615 | if( pOp->p5 & OPFLAG_ISNOOP ) break; |
drh | 9b1c62d | 2011-03-30 21:04:43 +0000 | [diff] [blame] | 4616 | #endif |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 4617 | |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 4618 | if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++; |
drh | fae58d5 | 2017-01-26 17:26:44 +0000 | [diff] [blame] | 4619 | if( pOp->p5 & OPFLAG_LASTROWID ) db->lastRowid = x.nKey; |
dan | 21cd29a | 2017-10-23 16:03:54 +0000 | [diff] [blame] | 4620 | assert( pData->flags & (MEM_Blob|MEM_Str) ); |
| 4621 | x.pData = pData->z; |
| 4622 | x.nData = pData->n; |
drh | 3e9ca09 | 2009-09-08 01:14:48 +0000 | [diff] [blame] | 4623 | seekResult = ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0); |
| 4624 | if( pData->flags & MEM_Zero ){ |
drh | 8eeb446 | 2016-05-21 20:03:42 +0000 | [diff] [blame] | 4625 | x.nZero = pData->u.nZero; |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 4626 | }else{ |
drh | 8eeb446 | 2016-05-21 20:03:42 +0000 | [diff] [blame] | 4627 | x.nZero = 0; |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 4628 | } |
drh | 8eeb446 | 2016-05-21 20:03:42 +0000 | [diff] [blame] | 4629 | x.pKey = 0; |
| 4630 | rc = sqlite3BtreeInsert(pC->uc.pCursor, &x, |
dan | f91c131 | 2017-01-10 20:04:38 +0000 | [diff] [blame] | 4631 | (pOp->p5 & (OPFLAG_APPEND|OPFLAG_SAVEPOSITION)), seekResult |
drh | 3e9ca09 | 2009-09-08 01:14:48 +0000 | [diff] [blame] | 4632 | ); |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 4633 | pC->deferredMoveto = 0; |
| 4634 | pC->cacheStatus = CACHE_STALE; |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 +0000 | [diff] [blame] | 4635 | |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 4636 | /* Invoke the update-hook if required. */ |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 4637 | if( rc ) goto abort_due_to_error; |
drh | 4ec6f3a | 2018-01-12 19:33:18 +0000 | [diff] [blame] | 4638 | if( pTab ){ |
| 4639 | assert( db->xUpdateCallback!=0 ); |
| 4640 | assert( pTab->aCol!=0 ); |
| 4641 | db->xUpdateCallback(db->pUpdateArg, |
| 4642 | (pOp->p5 & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_INSERT, |
| 4643 | zDb, pTab->zName, x.nKey); |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 4644 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4645 | break; |
| 4646 | } |
| 4647 | |
dan | 438b881 | 2015-09-15 15:55:15 +0000 | [diff] [blame] | 4648 | /* Opcode: Delete P1 P2 P3 P4 P5 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4649 | ** |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 4650 | ** Delete the record at which the P1 cursor is currently pointing. |
| 4651 | ** |
drh | e807bdb | 2016-01-21 17:06:33 +0000 | [diff] [blame] | 4652 | ** If the OPFLAG_SAVEPOSITION bit of the P5 parameter is set, then |
| 4653 | ** the cursor will be left pointing at either the next or the previous |
| 4654 | ** record in the table. If it is left pointing at the next record, then |
| 4655 | ** the next Next instruction will be a no-op. As a result, in this case |
| 4656 | ** it is ok to delete a record from within a Next loop. If |
| 4657 | ** OPFLAG_SAVEPOSITION bit of P5 is clear, then the cursor will be |
| 4658 | ** left in an undefined state. |
drh | c8d30ac | 2002-04-12 10:08:59 +0000 | [diff] [blame] | 4659 | ** |
drh | def19e3 | 2016-01-27 16:26:25 +0000 | [diff] [blame] | 4660 | ** If the OPFLAG_AUXDELETE bit is set on P5, that indicates that this |
| 4661 | ** delete one of several associated with deleting a table row and all its |
| 4662 | ** associated index entries. Exactly one of those deletes is the "primary" |
| 4663 | ** delete. The others are all on OPFLAG_FORDELETE cursors or else are |
| 4664 | ** marked with the AUXDELETE flag. |
drh | e807bdb | 2016-01-21 17:06:33 +0000 | [diff] [blame] | 4665 | ** |
| 4666 | ** If the OPFLAG_NCHANGE flag of P2 (NB: P2 not P5) is set, then the row |
| 4667 | ** change count is incremented (otherwise not). |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 4668 | ** |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 4669 | ** P1 must not be pseudo-table. It has to be a real table with |
| 4670 | ** multiple rows. |
| 4671 | ** |
drh | 5e769a5 | 2016-09-28 16:05:53 +0000 | [diff] [blame] | 4672 | ** If P4 is not NULL then it points to a Table object. In this case either |
dan | 319eeb7 | 2011-03-19 08:38:50 +0000 | [diff] [blame] | 4673 | ** the update or pre-update hook, or both, may be invoked. The P1 cursor must |
| 4674 | ** have been positioned using OP_NotFound prior to invoking this opcode in |
| 4675 | ** this case. Specifically, if one is configured, the pre-update hook is |
| 4676 | ** invoked if P4 is not NULL. The update-hook is invoked if one is configured, |
| 4677 | ** P4 is not NULL, and the OPFLAG_NCHANGE flag is set in P2. |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 4678 | ** |
| 4679 | ** If the OPFLAG_ISUPDATE flag is set in P2, then P3 contains the address |
| 4680 | ** of the memory cell that contains the value that the rowid of the row will |
| 4681 | ** be set to by the update. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4682 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4683 | case OP_Delete: { |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 4684 | VdbeCursor *pC; |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 4685 | const char *zDb; |
dan | 319eeb7 | 2011-03-19 08:38:50 +0000 | [diff] [blame] | 4686 | Table *pTab; |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 4687 | int opflags; |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 4688 | |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 4689 | opflags = pOp->p2; |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 4690 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 4691 | pC = p->apCsr[pOp->p1]; |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 4692 | assert( pC!=0 ); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 4693 | assert( pC->eCurType==CURTYPE_BTREE ); |
| 4694 | assert( pC->uc.pCursor!=0 ); |
drh | 9a65f2c | 2009-06-22 19:05:40 +0000 | [diff] [blame] | 4695 | assert( pC->deferredMoveto==0 ); |
drh | 4031baf | 2018-05-28 17:31:20 +0000 | [diff] [blame] | 4696 | sqlite3VdbeIncrWriteCounter(p, pC); |
drh | 9a65f2c | 2009-06-22 19:05:40 +0000 | [diff] [blame] | 4697 | |
drh | b53a5a9 | 2014-10-12 22:37:22 +0000 | [diff] [blame] | 4698 | #ifdef SQLITE_DEBUG |
dan | 438b881 | 2015-09-15 15:55:15 +0000 | [diff] [blame] | 4699 | if( pOp->p4type==P4_TABLE && HasRowid(pOp->p4.pTab) && pOp->p5==0 ){ |
| 4700 | /* If p5 is zero, the seek operation that positioned the cursor prior to |
| 4701 | ** OP_Delete will have also set the pC->movetoTarget field to the rowid of |
| 4702 | ** the row that is being deleted */ |
drh | a7c90c4 | 2016-06-04 20:37:10 +0000 | [diff] [blame] | 4703 | i64 iKey = sqlite3BtreeIntegerKey(pC->uc.pCursor); |
drh | 92fe38e | 2014-10-14 13:41:32 +0000 | [diff] [blame] | 4704 | assert( pC->movetoTarget==iKey ); |
drh | b53a5a9 | 2014-10-12 22:37:22 +0000 | [diff] [blame] | 4705 | } |
| 4706 | #endif |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 4707 | |
dan | 438b881 | 2015-09-15 15:55:15 +0000 | [diff] [blame] | 4708 | /* If the update-hook or pre-update-hook will be invoked, set zDb to |
| 4709 | ** the name of the db to pass as to it. Also set local pTab to a copy |
| 4710 | ** of p4.pTab. Finally, if p5 is true, indicating that this cursor was |
| 4711 | ** last moved with OP_Next or OP_Prev, not Seek or NotFound, set |
| 4712 | ** VdbeCursor.movetoTarget to the current rowid. */ |
drh | c556f3c | 2016-03-30 15:30:07 +0000 | [diff] [blame] | 4713 | if( pOp->p4type==P4_TABLE && HAS_UPDATE_HOOK(db) ){ |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 4714 | assert( pC->iDb>=0 ); |
drh | c556f3c | 2016-03-30 15:30:07 +0000 | [diff] [blame] | 4715 | assert( pOp->p4.pTab!=0 ); |
drh | 69c3382 | 2016-08-18 14:33:11 +0000 | [diff] [blame] | 4716 | zDb = db->aDb[pC->iDb].zDbSName; |
dan | 319eeb7 | 2011-03-19 08:38:50 +0000 | [diff] [blame] | 4717 | pTab = pOp->p4.pTab; |
drh | c556f3c | 2016-03-30 15:30:07 +0000 | [diff] [blame] | 4718 | if( (pOp->p5 & OPFLAG_SAVEPOSITION)!=0 && pC->isTable ){ |
drh | a7c90c4 | 2016-06-04 20:37:10 +0000 | [diff] [blame] | 4719 | pC->movetoTarget = sqlite3BtreeIntegerKey(pC->uc.pCursor); |
dan | 438b881 | 2015-09-15 15:55:15 +0000 | [diff] [blame] | 4720 | } |
drh | 74c3302 | 2016-03-30 12:56:55 +0000 | [diff] [blame] | 4721 | }else{ |
| 4722 | zDb = 0; /* Not needed. Silence a compiler warning. */ |
| 4723 | pTab = 0; /* Not needed. Silence a compiler warning. */ |
drh | 92fe38e | 2014-10-14 13:41:32 +0000 | [diff] [blame] | 4724 | } |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 4725 | |
drh | 9b1c62d | 2011-03-30 21:04:43 +0000 | [diff] [blame] | 4726 | #ifdef SQLITE_ENABLE_PREUPDATE_HOOK |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 4727 | /* Invoke the pre-update-hook if required. */ |
dan | cb9a364 | 2017-01-30 19:44:53 +0000 | [diff] [blame] | 4728 | if( db->xPreUpdateCallback && pOp->p4.pTab ){ |
| 4729 | assert( !(opflags & OPFLAG_ISUPDATE) |
| 4730 | || HasRowid(pTab)==0 |
| 4731 | || (aMem[pOp->p3].flags & MEM_Int) |
| 4732 | ); |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 4733 | sqlite3VdbePreUpdateHook(p, pC, |
| 4734 | (opflags & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_DELETE, |
drh | 92fe38e | 2014-10-14 13:41:32 +0000 | [diff] [blame] | 4735 | zDb, pTab, pC->movetoTarget, |
dan | 37db03b | 2011-03-16 19:59:18 +0000 | [diff] [blame] | 4736 | pOp->p3 |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 4737 | ); |
| 4738 | } |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 4739 | if( opflags & OPFLAG_ISNOOP ) break; |
drh | c556f3c | 2016-03-30 15:30:07 +0000 | [diff] [blame] | 4740 | #endif |
drh | b53a5a9 | 2014-10-12 22:37:22 +0000 | [diff] [blame] | 4741 | |
drh | def19e3 | 2016-01-27 16:26:25 +0000 | [diff] [blame] | 4742 | /* Only flags that can be set are SAVEPOISTION and AUXDELETE */ |
| 4743 | assert( (pOp->p5 & ~(OPFLAG_SAVEPOSITION|OPFLAG_AUXDELETE))==0 ); |
drh | e807bdb | 2016-01-21 17:06:33 +0000 | [diff] [blame] | 4744 | assert( OPFLAG_SAVEPOSITION==BTREE_SAVEPOSITION ); |
drh | def19e3 | 2016-01-27 16:26:25 +0000 | [diff] [blame] | 4745 | assert( OPFLAG_AUXDELETE==BTREE_AUXDELETE ); |
drh | b89aeb6 | 2016-01-27 15:49:32 +0000 | [diff] [blame] | 4746 | |
| 4747 | #ifdef SQLITE_DEBUG |
dan | e61bbf4 | 2016-01-28 17:06:17 +0000 | [diff] [blame] | 4748 | if( p->pFrame==0 ){ |
| 4749 | if( pC->isEphemeral==0 |
| 4750 | && (pOp->p5 & OPFLAG_AUXDELETE)==0 |
| 4751 | && (pC->wrFlag & OPFLAG_FORDELETE)==0 |
| 4752 | ){ |
| 4753 | nExtraDelete++; |
| 4754 | } |
| 4755 | if( pOp->p2 & OPFLAG_NCHANGE ){ |
| 4756 | nExtraDelete--; |
| 4757 | } |
drh | b89aeb6 | 2016-01-27 15:49:32 +0000 | [diff] [blame] | 4758 | } |
| 4759 | #endif |
| 4760 | |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 4761 | rc = sqlite3BtreeDelete(pC->uc.pCursor, pOp->p5); |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 4762 | pC->cacheStatus = CACHE_STALE; |
dan | 3b908d4 | 2016-11-08 19:22:32 +0000 | [diff] [blame] | 4763 | pC->seekResult = 0; |
drh | d3e1af4 | 2016-02-25 18:54:30 +0000 | [diff] [blame] | 4764 | if( rc ) goto abort_due_to_error; |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 +0000 | [diff] [blame] | 4765 | |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 4766 | /* Invoke the update-hook if required. */ |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 4767 | if( opflags & OPFLAG_NCHANGE ){ |
| 4768 | p->nChange++; |
drh | c556f3c | 2016-03-30 15:30:07 +0000 | [diff] [blame] | 4769 | if( db->xUpdateCallback && HasRowid(pTab) ){ |
drh | 92fe38e | 2014-10-14 13:41:32 +0000 | [diff] [blame] | 4770 | db->xUpdateCallback(db->pUpdateArg, SQLITE_DELETE, zDb, pTab->zName, |
dan | 438b881 | 2015-09-15 15:55:15 +0000 | [diff] [blame] | 4771 | pC->movetoTarget); |
| 4772 | assert( pC->iDb>=0 ); |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 4773 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4774 | } |
dan | 438b881 | 2015-09-15 15:55:15 +0000 | [diff] [blame] | 4775 | |
rdc | b0c374f | 2004-02-20 22:53:38 +0000 | [diff] [blame] | 4776 | break; |
| 4777 | } |
drh | b7f1d9a | 2009-09-08 02:27:58 +0000 | [diff] [blame] | 4778 | /* Opcode: ResetCount * * * * * |
rdc | b0c374f | 2004-02-20 22:53:38 +0000 | [diff] [blame] | 4779 | ** |
drh | b7f1d9a | 2009-09-08 02:27:58 +0000 | [diff] [blame] | 4780 | ** The value of the change counter is copied to the database handle |
| 4781 | ** change counter (returned by subsequent calls to sqlite3_changes()). |
| 4782 | ** Then the VMs internal change counter resets to 0. |
| 4783 | ** This is used by trigger programs. |
rdc | b0c374f | 2004-02-20 22:53:38 +0000 | [diff] [blame] | 4784 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4785 | case OP_ResetCount: { |
drh | b7f1d9a | 2009-09-08 02:27:58 +0000 | [diff] [blame] | 4786 | sqlite3VdbeSetChanges(db, p->nChange); |
danielk1977 | b28af71 | 2004-06-21 06:50:26 +0000 | [diff] [blame] | 4787 | p->nChange = 0; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4788 | break; |
| 4789 | } |
| 4790 | |
drh | 1153c7b | 2013-11-01 22:02:56 +0000 | [diff] [blame] | 4791 | /* Opcode: SorterCompare P1 P2 P3 P4 |
drh | 72e26de | 2016-08-24 21:24:04 +0000 | [diff] [blame] | 4792 | ** Synopsis: if key(P1)!=trim(r[P3],P4) goto P2 |
dan | 5134d13 | 2011-09-02 10:31:11 +0000 | [diff] [blame] | 4793 | ** |
drh | 1153c7b | 2013-11-01 22:02:56 +0000 | [diff] [blame] | 4794 | ** P1 is a sorter cursor. This instruction compares a prefix of the |
drh | bc5cf38 | 2014-08-06 01:08:07 +0000 | [diff] [blame] | 4795 | ** record blob in register P3 against a prefix of the entry that |
drh | ac50232 | 2014-07-30 13:56:48 +0000 | [diff] [blame] | 4796 | ** the sorter cursor currently points to. Only the first P4 fields |
| 4797 | ** of r[P3] and the sorter record are compared. |
drh | 1153c7b | 2013-11-01 22:02:56 +0000 | [diff] [blame] | 4798 | ** |
| 4799 | ** If either P3 or the sorter contains a NULL in one of their significant |
| 4800 | ** fields (not counting the P4 fields at the end which are ignored) then |
| 4801 | ** the comparison is assumed to be equal. |
| 4802 | ** |
| 4803 | ** Fall through to next instruction if the two records compare equal to |
| 4804 | ** each other. Jump to P2 if they are different. |
dan | 5134d13 | 2011-09-02 10:31:11 +0000 | [diff] [blame] | 4805 | */ |
| 4806 | case OP_SorterCompare: { |
| 4807 | VdbeCursor *pC; |
| 4808 | int res; |
drh | ac50232 | 2014-07-30 13:56:48 +0000 | [diff] [blame] | 4809 | int nKeyCol; |
dan | 5134d13 | 2011-09-02 10:31:11 +0000 | [diff] [blame] | 4810 | |
| 4811 | pC = p->apCsr[pOp->p1]; |
| 4812 | assert( isSorter(pC) ); |
drh | 1153c7b | 2013-11-01 22:02:56 +0000 | [diff] [blame] | 4813 | assert( pOp->p4type==P4_INT32 ); |
dan | 5134d13 | 2011-09-02 10:31:11 +0000 | [diff] [blame] | 4814 | pIn3 = &aMem[pOp->p3]; |
drh | ac50232 | 2014-07-30 13:56:48 +0000 | [diff] [blame] | 4815 | nKeyCol = pOp->p4.i; |
drh | 958d261 | 2014-04-18 13:40:07 +0000 | [diff] [blame] | 4816 | res = 0; |
drh | ac50232 | 2014-07-30 13:56:48 +0000 | [diff] [blame] | 4817 | rc = sqlite3VdbeSorterCompare(pC, pIn3, nKeyCol, &res); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 4818 | VdbeBranchTaken(res!=0,2); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 4819 | if( rc ) goto abort_due_to_error; |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 4820 | if( res ) goto jump_to_p2; |
dan | 5134d13 | 2011-09-02 10:31:11 +0000 | [diff] [blame] | 4821 | break; |
| 4822 | }; |
| 4823 | |
drh | 6cf4a7d | 2014-10-13 13:00:58 +0000 | [diff] [blame] | 4824 | /* Opcode: SorterData P1 P2 P3 * * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 4825 | ** Synopsis: r[P2]=data |
dan | 5134d13 | 2011-09-02 10:31:11 +0000 | [diff] [blame] | 4826 | ** |
| 4827 | ** Write into register P2 the current sorter data for sorter cursor P1. |
drh | 6cf4a7d | 2014-10-13 13:00:58 +0000 | [diff] [blame] | 4828 | ** Then clear the column header cache on cursor P3. |
| 4829 | ** |
| 4830 | ** This opcode is normally use to move a record out of the sorter and into |
| 4831 | ** a register that is the source for a pseudo-table cursor created using |
| 4832 | ** OpenPseudo. That pseudo-table cursor is the one that is identified by |
| 4833 | ** parameter P3. Clearing the P3 column cache as part of this opcode saves |
| 4834 | ** us from having to issue a separate NullRow instruction to clear that cache. |
dan | 5134d13 | 2011-09-02 10:31:11 +0000 | [diff] [blame] | 4835 | */ |
| 4836 | case OP_SorterData: { |
| 4837 | VdbeCursor *pC; |
drh | 3a94987 | 2012-09-18 13:20:13 +0000 | [diff] [blame] | 4838 | |
dan | 5134d13 | 2011-09-02 10:31:11 +0000 | [diff] [blame] | 4839 | pOut = &aMem[pOp->p2]; |
| 4840 | pC = p->apCsr[pOp->p1]; |
drh | 14da87f | 2013-11-20 21:51:33 +0000 | [diff] [blame] | 4841 | assert( isSorter(pC) ); |
dan | 5134d13 | 2011-09-02 10:31:11 +0000 | [diff] [blame] | 4842 | rc = sqlite3VdbeSorterRowkey(pC, pOut); |
dan | 3852413 | 2014-05-01 20:26:48 +0000 | [diff] [blame] | 4843 | assert( rc!=SQLITE_OK || (pOut->flags & MEM_Blob) ); |
drh | 6cf4a7d | 2014-10-13 13:00:58 +0000 | [diff] [blame] | 4844 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 4845 | if( rc ) goto abort_due_to_error; |
drh | 6cf4a7d | 2014-10-13 13:00:58 +0000 | [diff] [blame] | 4846 | p->apCsr[pOp->p3]->cacheStatus = CACHE_STALE; |
dan | 5134d13 | 2011-09-02 10:31:11 +0000 | [diff] [blame] | 4847 | break; |
| 4848 | } |
| 4849 | |
drh | e7b554d | 2017-01-09 15:44:25 +0000 | [diff] [blame] | 4850 | /* Opcode: RowData P1 P2 P3 * * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 4851 | ** Synopsis: r[P2]=data |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 4852 | ** |
drh | 9057fc7 | 2016-11-25 19:32:32 +0000 | [diff] [blame] | 4853 | ** Write into register P2 the complete row content for the row at |
| 4854 | ** which cursor P1 is currently pointing. |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4855 | ** There is no interpretation of the data. |
| 4856 | ** It is just copied onto the P2 register exactly as |
danielk1977 | 96cb76f | 2008-01-04 13:24:28 +0000 | [diff] [blame] | 4857 | ** it is found in the database file. |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 4858 | ** |
drh | 9057fc7 | 2016-11-25 19:32:32 +0000 | [diff] [blame] | 4859 | ** If cursor P1 is an index, then the content is the key of the row. |
| 4860 | ** If cursor P2 is a table, then the content extracted is the data. |
drh | 143f3c4 | 2004-01-07 20:37:52 +0000 | [diff] [blame] | 4861 | ** |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 4862 | ** If the P1 cursor must be pointing to a valid row (not a NULL row) |
| 4863 | ** of a real table, not a pseudo-table. |
drh | e7b554d | 2017-01-09 15:44:25 +0000 | [diff] [blame] | 4864 | ** |
drh | 8cdafc3 | 2018-05-31 19:00:20 +0000 | [diff] [blame] | 4865 | ** If P3!=0 then this opcode is allowed to make an ephemeral pointer |
drh | e7b554d | 2017-01-09 15:44:25 +0000 | [diff] [blame] | 4866 | ** into the database page. That means that the content of the output |
| 4867 | ** register will be invalidated as soon as the cursor moves - including |
drh | 416a801 | 2018-05-31 19:14:52 +0000 | [diff] [blame] | 4868 | ** moves caused by other cursors that "save" the current cursors |
drh | e7b554d | 2017-01-09 15:44:25 +0000 | [diff] [blame] | 4869 | ** position in order that they can write to the same table. If P3==0 |
| 4870 | ** then a copy of the data is made into memory. P3!=0 is faster, but |
| 4871 | ** P3==0 is safer. |
| 4872 | ** |
| 4873 | ** If P3!=0 then the content of the P2 register is unsuitable for use |
| 4874 | ** in OP_Result and any OP_Result will invalidate the P2 register content. |
mistachkin | ab61cf7 | 2017-01-09 18:22:54 +0000 | [diff] [blame] | 4875 | ** The P2 register content is invalidated by opcodes like OP_Function or |
drh | e7b554d | 2017-01-09 15:44:25 +0000 | [diff] [blame] | 4876 | ** by any use of another cursor pointing to the same table. |
drh | 143f3c4 | 2004-01-07 20:37:52 +0000 | [diff] [blame] | 4877 | */ |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 4878 | case OP_RowData: { |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 4879 | VdbeCursor *pC; |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 4880 | BtCursor *pCrsr; |
danielk1977 | e0d4b06 | 2004-06-28 01:11:46 +0000 | [diff] [blame] | 4881 | u32 n; |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 4882 | |
drh | e7b554d | 2017-01-09 15:44:25 +0000 | [diff] [blame] | 4883 | pOut = out2Prerelease(p, pOp); |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 4884 | |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 4885 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 4886 | pC = p->apCsr[pOp->p1]; |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 4887 | assert( pC!=0 ); |
| 4888 | assert( pC->eCurType==CURTYPE_BTREE ); |
drh | 14da87f | 2013-11-20 21:51:33 +0000 | [diff] [blame] | 4889 | assert( isSorter(pC)==0 ); |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 4890 | assert( pC->nullRow==0 ); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 4891 | assert( pC->uc.pCursor!=0 ); |
| 4892 | pCrsr = pC->uc.pCursor; |
drh | 9a65f2c | 2009-06-22 19:05:40 +0000 | [diff] [blame] | 4893 | |
drh | 9057fc7 | 2016-11-25 19:32:32 +0000 | [diff] [blame] | 4894 | /* The OP_RowData opcodes always follow OP_NotExists or |
drh | eeb9565 | 2016-05-26 20:56:38 +0000 | [diff] [blame] | 4895 | ** OP_SeekRowid or OP_Rewind/Op_Next with no intervening instructions |
| 4896 | ** that might invalidate the cursor. |
| 4897 | ** If this where not the case, on of the following assert()s |
drh | c22284f | 2014-10-13 16:02:20 +0000 | [diff] [blame] | 4898 | ** would fail. Should this ever change (because of changes in the code |
| 4899 | ** generator) then the fix would be to insert a call to |
| 4900 | ** sqlite3VdbeCursorMoveto(). |
drh | 9a65f2c | 2009-06-22 19:05:40 +0000 | [diff] [blame] | 4901 | */ |
| 4902 | assert( pC->deferredMoveto==0 ); |
drh | c22284f | 2014-10-13 16:02:20 +0000 | [diff] [blame] | 4903 | assert( sqlite3BtreeCursorIsValid(pCrsr) ); |
| 4904 | #if 0 /* Not required due to the previous to assert() statements */ |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 4905 | rc = sqlite3VdbeCursorMoveto(pC); |
drh | c22284f | 2014-10-13 16:02:20 +0000 | [diff] [blame] | 4906 | if( rc!=SQLITE_OK ) goto abort_due_to_error; |
| 4907 | #endif |
drh | 9a65f2c | 2009-06-22 19:05:40 +0000 | [diff] [blame] | 4908 | |
drh | a7c90c4 | 2016-06-04 20:37:10 +0000 | [diff] [blame] | 4909 | n = sqlite3BtreePayloadSize(pCrsr); |
drh | d66c4f8 | 2016-06-04 20:58:35 +0000 | [diff] [blame] | 4910 | if( n>(u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){ |
drh | a7c90c4 | 2016-06-04 20:37:10 +0000 | [diff] [blame] | 4911 | goto too_big; |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 4912 | } |
drh | 722246e | 2014-10-07 23:02:24 +0000 | [diff] [blame] | 4913 | testcase( n==0 ); |
drh | e7b554d | 2017-01-09 15:44:25 +0000 | [diff] [blame] | 4914 | rc = sqlite3VdbeMemFromBtree(pCrsr, 0, n, pOut); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 4915 | if( rc ) goto abort_due_to_error; |
drh | e7b554d | 2017-01-09 15:44:25 +0000 | [diff] [blame] | 4916 | if( !pOp->p3 ) Deephemeralize(pOut); |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 4917 | UPDATE_MAX_BLOBSIZE(pOut); |
drh | ee0ec8e | 2013-10-31 17:38:01 +0000 | [diff] [blame] | 4918 | REGISTER_TRACE(pOp->p2, pOut); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4919 | break; |
| 4920 | } |
| 4921 | |
drh | 2133d82 | 2008-01-03 18:44:59 +0000 | [diff] [blame] | 4922 | /* Opcode: Rowid P1 P2 * * * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 4923 | ** Synopsis: r[P2]=rowid |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4924 | ** |
drh | 2133d82 | 2008-01-03 18:44:59 +0000 | [diff] [blame] | 4925 | ** 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] | 4926 | ** P1 is currently point to. |
drh | 044925b | 2009-04-22 17:15:02 +0000 | [diff] [blame] | 4927 | ** |
| 4928 | ** P1 can be either an ordinary table or a virtual table. There used to |
| 4929 | ** be a separate OP_VRowid opcode for use with virtual tables, but this |
| 4930 | ** one opcode now works for both table types. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4931 | */ |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 4932 | case OP_Rowid: { /* out2 */ |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 4933 | VdbeCursor *pC; |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 4934 | i64 v; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 4935 | sqlite3_vtab *pVtab; |
| 4936 | const sqlite3_module *pModule; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4937 | |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 4938 | pOut = out2Prerelease(p, pOp); |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 4939 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 4940 | pC = p->apCsr[pOp->p1]; |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 4941 | assert( pC!=0 ); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 4942 | assert( pC->eCurType!=CURTYPE_PSEUDO || pC->nullRow ); |
drh | 044925b | 2009-04-22 17:15:02 +0000 | [diff] [blame] | 4943 | if( pC->nullRow ){ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 4944 | pOut->flags = MEM_Null; |
drh | 044925b | 2009-04-22 17:15:02 +0000 | [diff] [blame] | 4945 | break; |
| 4946 | }else if( pC->deferredMoveto ){ |
drh | 6149526 | 2009-04-22 15:32:59 +0000 | [diff] [blame] | 4947 | v = pC->movetoTarget; |
drh | 044925b | 2009-04-22 17:15:02 +0000 | [diff] [blame] | 4948 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 4949 | }else if( pC->eCurType==CURTYPE_VTAB ){ |
| 4950 | assert( pC->uc.pVCur!=0 ); |
| 4951 | pVtab = pC->uc.pVCur->pVtab; |
drh | 044925b | 2009-04-22 17:15:02 +0000 | [diff] [blame] | 4952 | pModule = pVtab->pModule; |
| 4953 | assert( pModule->xRowid ); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 4954 | rc = pModule->xRowid(pC->uc.pVCur, &v); |
dan | 016f781 | 2013-08-21 17:35:48 +0000 | [diff] [blame] | 4955 | sqlite3VtabImportErrmsg(p, pVtab); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 4956 | if( rc ) goto abort_due_to_error; |
drh | 044925b | 2009-04-22 17:15:02 +0000 | [diff] [blame] | 4957 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 4958 | }else{ |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 4959 | assert( pC->eCurType==CURTYPE_BTREE ); |
| 4960 | assert( pC->uc.pCursor!=0 ); |
drh | c22284f | 2014-10-13 16:02:20 +0000 | [diff] [blame] | 4961 | rc = sqlite3VdbeCursorRestore(pC); |
drh | 6149526 | 2009-04-22 15:32:59 +0000 | [diff] [blame] | 4962 | if( rc ) goto abort_due_to_error; |
dan | 2b8669a | 2014-11-17 19:42:48 +0000 | [diff] [blame] | 4963 | if( pC->nullRow ){ |
| 4964 | pOut->flags = MEM_Null; |
| 4965 | break; |
| 4966 | } |
drh | a7c90c4 | 2016-06-04 20:37:10 +0000 | [diff] [blame] | 4967 | v = sqlite3BtreeIntegerKey(pC->uc.pCursor); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4968 | } |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 4969 | pOut->u.i = v; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4970 | break; |
| 4971 | } |
| 4972 | |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4973 | /* Opcode: NullRow P1 * * * * |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 4974 | ** |
| 4975 | ** Move the cursor P1 to a null row. Any OP_Column operations |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4976 | ** that occur while the cursor is on the null row will always |
| 4977 | ** write a NULL. |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 4978 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4979 | case OP_NullRow: { |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 4980 | VdbeCursor *pC; |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 4981 | |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 4982 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 4983 | pC = p->apCsr[pOp->p1]; |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 4984 | assert( pC!=0 ); |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame] | 4985 | pC->nullRow = 1; |
drh | 399af1d | 2013-11-20 17:25:55 +0000 | [diff] [blame] | 4986 | pC->cacheStatus = CACHE_STALE; |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 4987 | if( pC->eCurType==CURTYPE_BTREE ){ |
| 4988 | assert( pC->uc.pCursor!=0 ); |
| 4989 | sqlite3BtreeClearCursor(pC->uc.pCursor); |
danielk1977 | be51a65 | 2008-10-08 17:58:48 +0000 | [diff] [blame] | 4990 | } |
drh | cf025a8 | 2018-06-07 18:01:21 +0000 | [diff] [blame] | 4991 | #ifdef SQLITE_DEBUG |
| 4992 | if( pC->seekOp==0 ) pC->seekOp = OP_NullRow; |
| 4993 | #endif |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 4994 | break; |
| 4995 | } |
| 4996 | |
drh | 86b40df | 2017-08-01 19:53:43 +0000 | [diff] [blame] | 4997 | /* Opcode: SeekEnd P1 * * * * |
| 4998 | ** |
| 4999 | ** Position cursor P1 at the end of the btree for the purpose of |
| 5000 | ** appending a new entry onto the btree. |
| 5001 | ** |
| 5002 | ** It is assumed that the cursor is used only for appending and so |
| 5003 | ** if the cursor is valid, then the cursor must already be pointing |
| 5004 | ** at the end of the btree and so no changes are made to |
| 5005 | ** the cursor. |
| 5006 | */ |
| 5007 | /* Opcode: Last P1 P2 * * * |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 5008 | ** |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 5009 | ** The next use of the Rowid or Column or Prev instruction for P1 |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 5010 | ** will refer to the last entry in the database table or index. |
| 5011 | ** If the table or index is empty and P2>0, then jump immediately to P2. |
| 5012 | ** If P2 is 0 or if the table or index is not empty, fall through |
| 5013 | ** to the following instruction. |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 5014 | ** |
| 5015 | ** This opcode leaves the cursor configured to move in reverse order, |
| 5016 | ** from the end toward the beginning. In other words, the cursor is |
drh | 5dad9a3 | 2014-07-25 18:37:42 +0000 | [diff] [blame] | 5017 | ** configured to use Prev, not Next. |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 5018 | */ |
drh | 86b40df | 2017-08-01 19:53:43 +0000 | [diff] [blame] | 5019 | case OP_SeekEnd: |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 5020 | case OP_Last: { /* jump */ |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 5021 | VdbeCursor *pC; |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 5022 | BtCursor *pCrsr; |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 5023 | int res; |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 5024 | |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 5025 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 5026 | pC = p->apCsr[pOp->p1]; |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 5027 | assert( pC!=0 ); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 5028 | assert( pC->eCurType==CURTYPE_BTREE ); |
| 5029 | pCrsr = pC->uc.pCursor; |
drh | 7abc540 | 2011-10-22 21:00:46 +0000 | [diff] [blame] | 5030 | res = 0; |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 5031 | assert( pCrsr!=0 ); |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 5032 | #ifdef SQLITE_DEBUG |
drh | 86b40df | 2017-08-01 19:53:43 +0000 | [diff] [blame] | 5033 | pC->seekOp = pOp->opcode; |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 5034 | #endif |
drh | 86b40df | 2017-08-01 19:53:43 +0000 | [diff] [blame] | 5035 | if( pOp->opcode==OP_SeekEnd ){ |
drh | d6ef5af | 2016-11-15 04:00:24 +0000 | [diff] [blame] | 5036 | assert( pOp->p2==0 ); |
drh | 86b40df | 2017-08-01 19:53:43 +0000 | [diff] [blame] | 5037 | pC->seekResult = -1; |
| 5038 | if( sqlite3BtreeCursorIsValidNN(pCrsr) ){ |
| 5039 | break; |
| 5040 | } |
| 5041 | } |
| 5042 | rc = sqlite3BtreeLast(pCrsr, &res); |
| 5043 | pC->nullRow = (u8)res; |
| 5044 | pC->deferredMoveto = 0; |
| 5045 | pC->cacheStatus = CACHE_STALE; |
| 5046 | if( rc ) goto abort_due_to_error; |
| 5047 | if( pOp->p2>0 ){ |
| 5048 | VdbeBranchTaken(res!=0,2); |
| 5049 | if( res ) goto jump_to_p2; |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 5050 | } |
| 5051 | break; |
| 5052 | } |
| 5053 | |
drh | 5e98e83 | 2017-02-17 19:24:06 +0000 | [diff] [blame] | 5054 | /* Opcode: IfSmaller P1 P2 P3 * * |
| 5055 | ** |
| 5056 | ** Estimate the number of rows in the table P1. Jump to P2 if that |
| 5057 | ** estimate is less than approximately 2**(0.1*P3). |
| 5058 | */ |
| 5059 | case OP_IfSmaller: { /* jump */ |
| 5060 | VdbeCursor *pC; |
| 5061 | BtCursor *pCrsr; |
| 5062 | int res; |
| 5063 | i64 sz; |
| 5064 | |
| 5065 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 5066 | pC = p->apCsr[pOp->p1]; |
| 5067 | assert( pC!=0 ); |
| 5068 | pCrsr = pC->uc.pCursor; |
| 5069 | assert( pCrsr ); |
| 5070 | rc = sqlite3BtreeFirst(pCrsr, &res); |
| 5071 | if( rc ) goto abort_due_to_error; |
| 5072 | if( res==0 ){ |
| 5073 | sz = sqlite3BtreeRowCountEst(pCrsr); |
| 5074 | if( ALWAYS(sz>=0) && sqlite3LogEst((u64)sz)<pOp->p3 ) res = 1; |
| 5075 | } |
| 5076 | VdbeBranchTaken(res!=0,2); |
| 5077 | if( res ) goto jump_to_p2; |
| 5078 | break; |
| 5079 | } |
| 5080 | |
drh | 0342b1f | 2005-09-01 03:07:44 +0000 | [diff] [blame] | 5081 | |
drh | 6bd4dc6 | 2016-12-23 16:05:22 +0000 | [diff] [blame] | 5082 | /* Opcode: SorterSort P1 P2 * * * |
| 5083 | ** |
| 5084 | ** After all records have been inserted into the Sorter object |
| 5085 | ** identified by P1, invoke this opcode to actually do the sorting. |
| 5086 | ** Jump to P2 if there are no records to be sorted. |
| 5087 | ** |
| 5088 | ** This opcode is an alias for OP_Sort and OP_Rewind that is used |
| 5089 | ** for Sorter objects. |
| 5090 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 5091 | /* Opcode: Sort P1 P2 * * * |
drh | 0342b1f | 2005-09-01 03:07:44 +0000 | [diff] [blame] | 5092 | ** |
| 5093 | ** This opcode does exactly the same thing as OP_Rewind except that |
| 5094 | ** it increments an undocumented global variable used for testing. |
| 5095 | ** |
| 5096 | ** Sorting is accomplished by writing records into a sorting index, |
| 5097 | ** then rewinding that index and playing it back from beginning to |
| 5098 | ** end. We use the OP_Sort opcode instead of OP_Rewind to do the |
| 5099 | ** rewinding so that the global variable will be incremented and |
| 5100 | ** regression tests can determine whether or not the optimizer is |
| 5101 | ** correctly optimizing out sorts. |
| 5102 | */ |
drh | c6aff30 | 2011-09-01 15:32:47 +0000 | [diff] [blame] | 5103 | case OP_SorterSort: /* jump */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 5104 | case OP_Sort: { /* jump */ |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 5105 | #ifdef SQLITE_TEST |
drh | 0342b1f | 2005-09-01 03:07:44 +0000 | [diff] [blame] | 5106 | sqlite3_sort_count++; |
drh | 4db38a7 | 2005-09-01 12:16:28 +0000 | [diff] [blame] | 5107 | sqlite3_search_count--; |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 5108 | #endif |
drh | 9b47ee3 | 2013-08-20 03:13:51 +0000 | [diff] [blame] | 5109 | p->aCounter[SQLITE_STMTSTATUS_SORT]++; |
drh | 0342b1f | 2005-09-01 03:07:44 +0000 | [diff] [blame] | 5110 | /* Fall through into OP_Rewind */ |
| 5111 | } |
dan | 9a94722 | 2018-06-14 19:06:36 +0000 | [diff] [blame] | 5112 | /* Opcode: Rewind P1 P2 * * P5 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5113 | ** |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 5114 | ** The next use of the Rowid or Column or Next instruction for P1 |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 5115 | ** will refer to the first entry in the database table or index. |
dan | 04489b6 | 2014-10-31 20:11:32 +0000 | [diff] [blame] | 5116 | ** If the table or index is empty, jump immediately to P2. |
| 5117 | ** If the table or index is not empty, fall through to the following |
| 5118 | ** instruction. |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 5119 | ** |
dan | 9a94722 | 2018-06-14 19:06:36 +0000 | [diff] [blame] | 5120 | ** If P5 is non-zero and the table is not empty, then the "skip-next" |
| 5121 | ** flag is set on the cursor so that the next OP_Next instruction |
| 5122 | ** executed on it is a no-op. |
| 5123 | ** |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 5124 | ** This opcode leaves the cursor configured to move in forward order, |
drh | 4ed2fb9 | 2014-08-14 13:06:25 +0000 | [diff] [blame] | 5125 | ** from the beginning toward the end. In other words, the cursor is |
drh | 5dad9a3 | 2014-07-25 18:37:42 +0000 | [diff] [blame] | 5126 | ** configured to use Next, not Prev. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5127 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 5128 | case OP_Rewind: { /* jump */ |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 5129 | VdbeCursor *pC; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5130 | BtCursor *pCrsr; |
drh | f4dada7 | 2004-05-11 09:57:35 +0000 | [diff] [blame] | 5131 | int res; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5132 | |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 5133 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 5134 | pC = p->apCsr[pOp->p1]; |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 5135 | assert( pC!=0 ); |
drh | 14da87f | 2013-11-20 21:51:33 +0000 | [diff] [blame] | 5136 | assert( isSorter(pC)==(pOp->opcode==OP_SorterSort) ); |
dan | 2411dea | 2010-07-03 05:56:09 +0000 | [diff] [blame] | 5137 | res = 1; |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 5138 | #ifdef SQLITE_DEBUG |
| 5139 | pC->seekOp = OP_Rewind; |
| 5140 | #endif |
dan | 689ab89 | 2011-08-12 15:02:00 +0000 | [diff] [blame] | 5141 | if( isSorter(pC) ){ |
drh | 958d261 | 2014-04-18 13:40:07 +0000 | [diff] [blame] | 5142 | rc = sqlite3VdbeSorterRewind(pC, &res); |
dan | a205a48 | 2011-08-27 18:48:57 +0000 | [diff] [blame] | 5143 | }else{ |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 5144 | assert( pC->eCurType==CURTYPE_BTREE ); |
| 5145 | pCrsr = pC->uc.pCursor; |
dan | a205a48 | 2011-08-27 18:48:57 +0000 | [diff] [blame] | 5146 | assert( pCrsr ); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 5147 | rc = sqlite3BtreeFirst(pCrsr, &res); |
dan | 67a9b8e | 2018-06-22 20:51:35 +0000 | [diff] [blame] | 5148 | #ifndef SQLITE_OMIT_WINDOWFUNC |
dan | c3a20c1 | 2018-05-23 20:55:37 +0000 | [diff] [blame] | 5149 | if( pOp->p5 ) sqlite3BtreeSkipNext(pCrsr); |
dan | 67a9b8e | 2018-06-22 20:51:35 +0000 | [diff] [blame] | 5150 | #endif |
drh | a11846b | 2004-01-07 18:52:56 +0000 | [diff] [blame] | 5151 | pC->deferredMoveto = 0; |
drh | 76873ab | 2006-01-07 18:48:26 +0000 | [diff] [blame] | 5152 | pC->cacheStatus = CACHE_STALE; |
drh | f4dada7 | 2004-05-11 09:57:35 +0000 | [diff] [blame] | 5153 | } |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 5154 | if( rc ) goto abort_due_to_error; |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 5155 | pC->nullRow = (u8)res; |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 5156 | assert( pOp->p2>0 && pOp->p2<p->nOp ); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 5157 | VdbeBranchTaken(res!=0,2); |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 5158 | if( res ) goto jump_to_p2; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5159 | break; |
| 5160 | } |
| 5161 | |
drh | 0fd6135 | 2014-02-07 02:29:45 +0000 | [diff] [blame] | 5162 | /* Opcode: Next P1 P2 P3 P4 P5 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5163 | ** |
| 5164 | ** 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] | 5165 | ** table or index. If there are no more key/value pairs then fall through |
| 5166 | ** to the following instruction. But if the cursor advance was successful, |
| 5167 | ** jump immediately to P2. |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 5168 | ** |
drh | 5dad9a3 | 2014-07-25 18:37:42 +0000 | [diff] [blame] | 5169 | ** The Next opcode is only valid following an SeekGT, SeekGE, or |
| 5170 | ** OP_Rewind opcode used to position the cursor. Next is not allowed |
| 5171 | ** to follow SeekLT, SeekLE, or OP_Last. |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 5172 | ** |
drh | f93cd94 | 2013-11-21 03:12:25 +0000 | [diff] [blame] | 5173 | ** The P1 cursor must be for a real table, not a pseudo-table. P1 must have |
| 5174 | ** been opened prior to this opcode or the program will segfault. |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 5175 | ** |
drh | e39a732 | 2014-02-03 14:04:11 +0000 | [diff] [blame] | 5176 | ** The P3 value is a hint to the btree implementation. If P3==1, that |
| 5177 | ** means P1 is an SQL index and that this instruction could have been |
| 5178 | ** omitted if that index had been unique. P3 is usually 0. P3 is |
| 5179 | ** always either 0 or 1. |
| 5180 | ** |
dan | a205a48 | 2011-08-27 18:48:57 +0000 | [diff] [blame] | 5181 | ** P4 is always of type P4_ADVANCE. The function pointer points to |
| 5182 | ** sqlite3BtreeNext(). |
| 5183 | ** |
drh | afc266a | 2010-03-31 17:47:44 +0000 | [diff] [blame] | 5184 | ** If P5 is positive and the jump is taken, then event counter |
| 5185 | ** number P5-1 in the prepared statement is incremented. |
| 5186 | ** |
drh | f1949b6 | 2018-06-07 17:32:59 +0000 | [diff] [blame] | 5187 | ** See also: Prev |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 5188 | */ |
drh | 0fd6135 | 2014-02-07 02:29:45 +0000 | [diff] [blame] | 5189 | /* Opcode: Prev P1 P2 P3 P4 P5 |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 5190 | ** |
| 5191 | ** Back up cursor P1 so that it points to the previous key/data pair in its |
| 5192 | ** table or index. If there is no previous key/value pairs then fall through |
| 5193 | ** to the following instruction. But if the cursor backup was successful, |
| 5194 | ** jump immediately to P2. |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 5195 | ** |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 5196 | ** |
drh | 5dad9a3 | 2014-07-25 18:37:42 +0000 | [diff] [blame] | 5197 | ** The Prev opcode is only valid following an SeekLT, SeekLE, or |
| 5198 | ** OP_Last opcode used to position the cursor. Prev is not allowed |
| 5199 | ** to follow SeekGT, SeekGE, or OP_Rewind. |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 5200 | ** |
drh | f93cd94 | 2013-11-21 03:12:25 +0000 | [diff] [blame] | 5201 | ** The P1 cursor must be for a real table, not a pseudo-table. If P1 is |
| 5202 | ** not open then the behavior is undefined. |
drh | afc266a | 2010-03-31 17:47:44 +0000 | [diff] [blame] | 5203 | ** |
drh | e39a732 | 2014-02-03 14:04:11 +0000 | [diff] [blame] | 5204 | ** The P3 value is a hint to the btree implementation. If P3==1, that |
| 5205 | ** means P1 is an SQL index and that this instruction could have been |
| 5206 | ** omitted if that index had been unique. P3 is usually 0. P3 is |
| 5207 | ** always either 0 or 1. |
| 5208 | ** |
dan | a205a48 | 2011-08-27 18:48:57 +0000 | [diff] [blame] | 5209 | ** P4 is always of type P4_ADVANCE. The function pointer points to |
| 5210 | ** sqlite3BtreePrevious(). |
| 5211 | ** |
drh | afc266a | 2010-03-31 17:47:44 +0000 | [diff] [blame] | 5212 | ** If P5 is positive and the jump is taken, then event counter |
| 5213 | ** number P5-1 in the prepared statement is incremented. |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 5214 | */ |
drh | 6bd4dc6 | 2016-12-23 16:05:22 +0000 | [diff] [blame] | 5215 | /* Opcode: SorterNext P1 P2 * * P5 |
| 5216 | ** |
| 5217 | ** This opcode works just like OP_Next except that P1 must be a |
| 5218 | ** sorter object for which the OP_SorterSort opcode has been |
| 5219 | ** invoked. This opcode advances the cursor to the next sorted |
| 5220 | ** record, or jumps to P2 if there are no more sorted records. |
| 5221 | */ |
drh | f93cd94 | 2013-11-21 03:12:25 +0000 | [diff] [blame] | 5222 | case OP_SorterNext: { /* jump */ |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 5223 | VdbeCursor *pC; |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 5224 | |
drh | f93cd94 | 2013-11-21 03:12:25 +0000 | [diff] [blame] | 5225 | pC = p->apCsr[pOp->p1]; |
| 5226 | assert( isSorter(pC) ); |
drh | 2ab792e | 2017-05-30 18:34:07 +0000 | [diff] [blame] | 5227 | rc = sqlite3VdbeSorterNext(db, pC); |
drh | f93cd94 | 2013-11-21 03:12:25 +0000 | [diff] [blame] | 5228 | goto next_tail; |
drh | f93cd94 | 2013-11-21 03:12:25 +0000 | [diff] [blame] | 5229 | case OP_Prev: /* jump */ |
| 5230 | case OP_Next: /* jump */ |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 5231 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
drh | 9b47ee3 | 2013-08-20 03:13:51 +0000 | [diff] [blame] | 5232 | assert( pOp->p5<ArraySize(p->aCounter) ); |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame] | 5233 | pC = p->apCsr[pOp->p1]; |
drh | f93cd94 | 2013-11-21 03:12:25 +0000 | [diff] [blame] | 5234 | assert( pC!=0 ); |
| 5235 | assert( pC->deferredMoveto==0 ); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 5236 | assert( pC->eCurType==CURTYPE_BTREE ); |
drh | f93cd94 | 2013-11-21 03:12:25 +0000 | [diff] [blame] | 5237 | assert( pOp->opcode!=OP_Next || pOp->p4.xAdvance==sqlite3BtreeNext ); |
| 5238 | assert( pOp->opcode!=OP_Prev || pOp->p4.xAdvance==sqlite3BtreePrevious ); |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 5239 | |
drh | cf025a8 | 2018-06-07 18:01:21 +0000 | [diff] [blame] | 5240 | /* The Next opcode is only used after SeekGT, SeekGE, Rewind, and Found. |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 5241 | ** The Prev opcode is only used after SeekLT, SeekLE, and Last. */ |
drh | f1949b6 | 2018-06-07 17:32:59 +0000 | [diff] [blame] | 5242 | assert( pOp->opcode!=OP_Next |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 5243 | || pC->seekOp==OP_SeekGT || pC->seekOp==OP_SeekGE |
drh | cf025a8 | 2018-06-07 18:01:21 +0000 | [diff] [blame] | 5244 | || pC->seekOp==OP_Rewind || pC->seekOp==OP_Found |
| 5245 | || pC->seekOp==OP_NullRow); |
drh | f1949b6 | 2018-06-07 17:32:59 +0000 | [diff] [blame] | 5246 | assert( pOp->opcode!=OP_Prev |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 5247 | || pC->seekOp==OP_SeekLT || pC->seekOp==OP_SeekLE |
drh | cf025a8 | 2018-06-07 18:01:21 +0000 | [diff] [blame] | 5248 | || pC->seekOp==OP_Last |
| 5249 | || pC->seekOp==OP_NullRow); |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 5250 | |
drh | 2ab792e | 2017-05-30 18:34:07 +0000 | [diff] [blame] | 5251 | rc = pOp->p4.xAdvance(pC->uc.pCursor, pOp->p3); |
drh | f93cd94 | 2013-11-21 03:12:25 +0000 | [diff] [blame] | 5252 | next_tail: |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 5253 | pC->cacheStatus = CACHE_STALE; |
drh | 2ab792e | 2017-05-30 18:34:07 +0000 | [diff] [blame] | 5254 | VdbeBranchTaken(rc==SQLITE_OK,2); |
| 5255 | if( rc==SQLITE_OK ){ |
drh | f93cd94 | 2013-11-21 03:12:25 +0000 | [diff] [blame] | 5256 | pC->nullRow = 0; |
drh | 9b47ee3 | 2013-08-20 03:13:51 +0000 | [diff] [blame] | 5257 | p->aCounter[pOp->p5]++; |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 5258 | #ifdef SQLITE_TEST |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 5259 | sqlite3_search_count++; |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 5260 | #endif |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 5261 | goto jump_to_p2_and_check_for_interrupt; |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 5262 | } |
drh | 2ab792e | 2017-05-30 18:34:07 +0000 | [diff] [blame] | 5263 | if( rc!=SQLITE_DONE ) goto abort_due_to_error; |
| 5264 | rc = SQLITE_OK; |
| 5265 | pC->nullRow = 1; |
drh | 49afe3a | 2013-07-10 03:05:14 +0000 | [diff] [blame] | 5266 | goto check_for_interrupt; |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 5267 | } |
| 5268 | |
drh | 9b4eaeb | 2016-11-09 00:10:33 +0000 | [diff] [blame] | 5269 | /* Opcode: IdxInsert P1 P2 P3 P4 P5 |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 5270 | ** Synopsis: key=r[P2] |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5271 | ** |
drh | ef8662b | 2011-06-20 21:47:58 +0000 | [diff] [blame] | 5272 | ** Register P2 holds an SQL index key made using the |
drh | 9437bd2 | 2009-02-01 00:29:56 +0000 | [diff] [blame] | 5273 | ** MakeRecord instructions. This opcode writes that key |
drh | ee32e0a | 2006-01-10 19:45:49 +0000 | [diff] [blame] | 5274 | ** into the index P1. Data for the entry is nil. |
drh | 717e640 | 2001-09-27 03:22:32 +0000 | [diff] [blame] | 5275 | ** |
drh | fb8c56f | 2016-11-09 01:19:25 +0000 | [diff] [blame] | 5276 | ** If P4 is not zero, then it is the number of values in the unpacked |
drh | 9b4eaeb | 2016-11-09 00:10:33 +0000 | [diff] [blame] | 5277 | ** key of reg(P2). In that case, P3 is the index of the first register |
| 5278 | ** for the unpacked key. The availability of the unpacked key can sometimes |
| 5279 | ** be an optimization. |
| 5280 | ** |
| 5281 | ** If P5 has the OPFLAG_APPEND bit set, that is a hint to the b-tree layer |
| 5282 | ** that this insert is likely to be an append. |
drh | e4d9081 | 2007-03-29 05:51:49 +0000 | [diff] [blame] | 5283 | ** |
mistachkin | 21a919f | 2014-02-07 03:28:02 +0000 | [diff] [blame] | 5284 | ** If P5 has the OPFLAG_NCHANGE bit set, then the change counter is |
| 5285 | ** incremented by this instruction. If the OPFLAG_NCHANGE bit is clear, |
| 5286 | ** then the change counter is unchanged. |
drh | 0fd6135 | 2014-02-07 02:29:45 +0000 | [diff] [blame] | 5287 | ** |
drh | eaf6ae2 | 2016-11-09 20:14:34 +0000 | [diff] [blame] | 5288 | ** If the OPFLAG_USESEEKRESULT flag of P5 is set, the implementation might |
| 5289 | ** run faster by avoiding an unnecessary seek on cursor P1. However, |
| 5290 | ** the OPFLAG_USESEEKRESULT flag must only be set if there have been no prior |
| 5291 | ** seeks on the cursor or if the most recent seek used a key equivalent |
| 5292 | ** to P2. |
drh | 0fd6135 | 2014-02-07 02:29:45 +0000 | [diff] [blame] | 5293 | ** |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 5294 | ** This instruction only works for indices. The equivalent instruction |
| 5295 | ** for tables is OP_Insert. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5296 | */ |
drh | f013e20 | 2016-10-15 18:37:05 +0000 | [diff] [blame] | 5297 | /* Opcode: SorterInsert P1 P2 * * * |
| 5298 | ** Synopsis: key=r[P2] |
| 5299 | ** |
| 5300 | ** Register P2 holds an SQL index key made using the |
| 5301 | ** MakeRecord instructions. This opcode writes that key |
| 5302 | ** into the sorter P1. Data for the entry is nil. |
| 5303 | */ |
drh | ca892a7 | 2011-09-03 00:17:51 +0000 | [diff] [blame] | 5304 | case OP_SorterInsert: /* in2 */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 5305 | case OP_IdxInsert: { /* in2 */ |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 5306 | VdbeCursor *pC; |
drh | 8eeb446 | 2016-05-21 20:03:42 +0000 | [diff] [blame] | 5307 | BtreePayload x; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 5308 | |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 5309 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 5310 | pC = p->apCsr[pOp->p1]; |
drh | 4031baf | 2018-05-28 17:31:20 +0000 | [diff] [blame] | 5311 | sqlite3VdbeIncrWriteCounter(p, pC); |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 5312 | assert( pC!=0 ); |
drh | 14da87f | 2013-11-20 21:51:33 +0000 | [diff] [blame] | 5313 | assert( isSorter(pC)==(pOp->opcode==OP_SorterInsert) ); |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 5314 | pIn2 = &aMem[pOp->p2]; |
drh | aa9b896 | 2008-01-08 02:57:55 +0000 | [diff] [blame] | 5315 | assert( pIn2->flags & MEM_Blob ); |
drh | 6546af1 | 2013-11-04 15:23:25 +0000 | [diff] [blame] | 5316 | if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++; |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 5317 | assert( pC->eCurType==CURTYPE_BTREE || pOp->opcode==OP_SorterInsert ); |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 5318 | assert( pC->isTable==0 ); |
| 5319 | rc = ExpandBlob(pIn2); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 5320 | if( rc ) goto abort_due_to_error; |
| 5321 | if( pOp->opcode==OP_SorterInsert ){ |
| 5322 | rc = sqlite3VdbeSorterWrite(pC, pIn2); |
| 5323 | }else{ |
drh | 8eeb446 | 2016-05-21 20:03:42 +0000 | [diff] [blame] | 5324 | x.nKey = pIn2->n; |
| 5325 | x.pKey = pIn2->z; |
drh | 9b4eaeb | 2016-11-09 00:10:33 +0000 | [diff] [blame] | 5326 | x.aMem = aMem + pOp->p3; |
| 5327 | x.nMem = (u16)pOp->p4.i; |
| 5328 | rc = sqlite3BtreeInsert(pC->uc.pCursor, &x, |
dan | f91c131 | 2017-01-10 20:04:38 +0000 | [diff] [blame] | 5329 | (pOp->p5 & (OPFLAG_APPEND|OPFLAG_SAVEPOSITION)), |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 5330 | ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0) |
| 5331 | ); |
| 5332 | assert( pC->deferredMoveto==0 ); |
| 5333 | pC->cacheStatus = CACHE_STALE; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5334 | } |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 5335 | if( rc) goto abort_due_to_error; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5336 | break; |
| 5337 | } |
| 5338 | |
drh | d1d3848 | 2008-10-07 23:46:38 +0000 | [diff] [blame] | 5339 | /* Opcode: IdxDelete P1 P2 P3 * * |
drh | f63552b | 2013-10-30 00:25:03 +0000 | [diff] [blame] | 5340 | ** Synopsis: key=r[P2@P3] |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5341 | ** |
drh | e14006d | 2008-03-25 17:23:32 +0000 | [diff] [blame] | 5342 | ** The content of P3 registers starting at register P2 form |
| 5343 | ** an unpacked index key. This opcode removes that entry from the |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 5344 | ** index opened by cursor P1. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5345 | */ |
drh | e14006d | 2008-03-25 17:23:32 +0000 | [diff] [blame] | 5346 | case OP_IdxDelete: { |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 5347 | VdbeCursor *pC; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5348 | BtCursor *pCrsr; |
drh | 9a65f2c | 2009-06-22 19:05:40 +0000 | [diff] [blame] | 5349 | int res; |
| 5350 | UnpackedRecord r; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 5351 | |
drh | e14006d | 2008-03-25 17:23:32 +0000 | [diff] [blame] | 5352 | assert( pOp->p3>0 ); |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 5353 | assert( pOp->p2>0 && pOp->p2+pOp->p3<=(p->nMem+1 - p->nCursor)+1 ); |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 5354 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 5355 | pC = p->apCsr[pOp->p1]; |
| 5356 | assert( pC!=0 ); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 5357 | assert( pC->eCurType==CURTYPE_BTREE ); |
drh | 4031baf | 2018-05-28 17:31:20 +0000 | [diff] [blame] | 5358 | sqlite3VdbeIncrWriteCounter(p, pC); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 5359 | pCrsr = pC->uc.pCursor; |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 5360 | assert( pCrsr!=0 ); |
drh | 4308e34 | 2013-11-11 16:55:52 +0000 | [diff] [blame] | 5361 | assert( pOp->p5==0 ); |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 5362 | r.pKeyInfo = pC->pKeyInfo; |
| 5363 | r.nField = (u16)pOp->p3; |
dan | 1fed5da | 2014-02-25 21:01:25 +0000 | [diff] [blame] | 5364 | r.default_rc = 0; |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 5365 | r.aMem = &aMem[pOp->p2]; |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 5366 | rc = sqlite3BtreeMovetoUnpacked(pCrsr, &r, 0, 0, &res); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 5367 | if( rc ) goto abort_due_to_error; |
| 5368 | if( res==0 ){ |
dan | e61bbf4 | 2016-01-28 17:06:17 +0000 | [diff] [blame] | 5369 | rc = sqlite3BtreeDelete(pCrsr, BTREE_AUXDELETE); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 5370 | if( rc ) goto abort_due_to_error; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5371 | } |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 5372 | assert( pC->deferredMoveto==0 ); |
| 5373 | pC->cacheStatus = CACHE_STALE; |
dan | 3b908d4 | 2016-11-08 19:22:32 +0000 | [diff] [blame] | 5374 | pC->seekResult = 0; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5375 | break; |
| 5376 | } |
| 5377 | |
drh | 170ad68 | 2017-06-02 15:44:22 +0000 | [diff] [blame] | 5378 | /* Opcode: DeferredSeek P1 * P3 P4 * |
| 5379 | ** Synopsis: Move P3 to P1.rowid if needed |
drh | 784c1b9 | 2016-01-30 16:59:56 +0000 | [diff] [blame] | 5380 | ** |
| 5381 | ** P1 is an open index cursor and P3 is a cursor on the corresponding |
| 5382 | ** table. This opcode does a deferred seek of the P3 table cursor |
| 5383 | ** to the row that corresponds to the current row of P1. |
| 5384 | ** |
| 5385 | ** This is a deferred seek. Nothing actually happens until |
| 5386 | ** the cursor is used to read a record. That way, if no reads |
| 5387 | ** occur, no unnecessary I/O happens. |
| 5388 | ** |
| 5389 | ** P4 may be an array of integers (type P4_INTARRAY) containing |
drh | 19d720d | 2016-02-03 19:52:06 +0000 | [diff] [blame] | 5390 | ** one entry for each column in the P3 table. If array entry a(i) |
| 5391 | ** is non-zero, then reading column a(i)-1 from cursor P3 is |
drh | 784c1b9 | 2016-01-30 16:59:56 +0000 | [diff] [blame] | 5392 | ** equivalent to performing the deferred seek and then reading column i |
| 5393 | ** from P1. This information is stored in P3 and used to redirect |
| 5394 | ** reads against P3 over to P1, thus possibly avoiding the need to |
| 5395 | ** seek and read cursor P3. |
| 5396 | */ |
drh | 2133d82 | 2008-01-03 18:44:59 +0000 | [diff] [blame] | 5397 | /* Opcode: IdxRowid P1 P2 * * * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 5398 | ** Synopsis: r[P2]=rowid |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 5399 | ** |
drh | 2133d82 | 2008-01-03 18:44:59 +0000 | [diff] [blame] | 5400 | ** 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] | 5401 | ** the end of the index key pointed to by cursor P1. This integer should be |
| 5402 | ** the rowid of the table entry to which this index entry points. |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 5403 | ** |
drh | 9437bd2 | 2009-02-01 00:29:56 +0000 | [diff] [blame] | 5404 | ** See also: Rowid, MakeRecord. |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 5405 | */ |
drh | 170ad68 | 2017-06-02 15:44:22 +0000 | [diff] [blame] | 5406 | case OP_DeferredSeek: |
| 5407 | case OP_IdxRowid: { /* out2 */ |
| 5408 | VdbeCursor *pC; /* The P1 index cursor */ |
| 5409 | VdbeCursor *pTabCur; /* The P2 table cursor (OP_DeferredSeek only) */ |
| 5410 | i64 rowid; /* Rowid that P1 current points to */ |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 5411 | |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 5412 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 5413 | pC = p->apCsr[pOp->p1]; |
| 5414 | assert( pC!=0 ); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 5415 | assert( pC->eCurType==CURTYPE_BTREE ); |
drh | 784c1b9 | 2016-01-30 16:59:56 +0000 | [diff] [blame] | 5416 | assert( pC->uc.pCursor!=0 ); |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 5417 | assert( pC->isTable==0 ); |
drh | c22284f | 2014-10-13 16:02:20 +0000 | [diff] [blame] | 5418 | assert( pC->deferredMoveto==0 ); |
drh | 784c1b9 | 2016-01-30 16:59:56 +0000 | [diff] [blame] | 5419 | assert( !pC->nullRow || pOp->opcode==OP_IdxRowid ); |
| 5420 | |
| 5421 | /* The IdxRowid and Seek opcodes are combined because of the commonality |
| 5422 | ** of sqlite3VdbeCursorRestore() and sqlite3VdbeIdxRowid(). */ |
| 5423 | rc = sqlite3VdbeCursorRestore(pC); |
drh | c22284f | 2014-10-13 16:02:20 +0000 | [diff] [blame] | 5424 | |
| 5425 | /* sqlite3VbeCursorRestore() can only fail if the record has been deleted |
drh | 784c1b9 | 2016-01-30 16:59:56 +0000 | [diff] [blame] | 5426 | ** out from under the cursor. That will never happens for an IdxRowid |
| 5427 | ** or Seek opcode */ |
drh | c22284f | 2014-10-13 16:02:20 +0000 | [diff] [blame] | 5428 | if( NEVER(rc!=SQLITE_OK) ) goto abort_due_to_error; |
| 5429 | |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 5430 | if( !pC->nullRow ){ |
drh | 2dc0648 | 2013-12-11 00:59:10 +0000 | [diff] [blame] | 5431 | rowid = 0; /* Not needed. Only used to silence a warning. */ |
drh | 784c1b9 | 2016-01-30 16:59:56 +0000 | [diff] [blame] | 5432 | rc = sqlite3VdbeIdxRowid(db, pC->uc.pCursor, &rowid); |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 5433 | if( rc!=SQLITE_OK ){ |
| 5434 | goto abort_due_to_error; |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 5435 | } |
drh | 170ad68 | 2017-06-02 15:44:22 +0000 | [diff] [blame] | 5436 | if( pOp->opcode==OP_DeferredSeek ){ |
drh | 784c1b9 | 2016-01-30 16:59:56 +0000 | [diff] [blame] | 5437 | assert( pOp->p3>=0 && pOp->p3<p->nCursor ); |
| 5438 | pTabCur = p->apCsr[pOp->p3]; |
| 5439 | assert( pTabCur!=0 ); |
| 5440 | assert( pTabCur->eCurType==CURTYPE_BTREE ); |
| 5441 | assert( pTabCur->uc.pCursor!=0 ); |
| 5442 | assert( pTabCur->isTable ); |
| 5443 | pTabCur->nullRow = 0; |
| 5444 | pTabCur->movetoTarget = rowid; |
| 5445 | pTabCur->deferredMoveto = 1; |
| 5446 | assert( pOp->p4type==P4_INTARRAY || pOp->p4.ai==0 ); |
| 5447 | pTabCur->aAltMap = pOp->p4.ai; |
| 5448 | pTabCur->pAltCursor = pC; |
| 5449 | }else{ |
| 5450 | pOut = out2Prerelease(p, pOp); |
| 5451 | pOut->u.i = rowid; |
drh | 784c1b9 | 2016-01-30 16:59:56 +0000 | [diff] [blame] | 5452 | } |
| 5453 | }else{ |
| 5454 | assert( pOp->opcode==OP_IdxRowid ); |
| 5455 | sqlite3VdbeMemSetNull(&aMem[pOp->p2]); |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 5456 | } |
| 5457 | break; |
| 5458 | } |
| 5459 | |
danielk1977 | 61dd583 | 2008-04-18 11:31:12 +0000 | [diff] [blame] | 5460 | /* Opcode: IdxGE P1 P2 P3 P4 P5 |
drh | f63552b | 2013-10-30 00:25:03 +0000 | [diff] [blame] | 5461 | ** Synopsis: key=r[P3@P4] |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 5462 | ** |
danielk1977 | 61dd583 | 2008-04-18 11:31:12 +0000 | [diff] [blame] | 5463 | ** The P4 register values beginning with P3 form an unpacked index |
drh | 4a1d365 | 2014-02-14 15:13:36 +0000 | [diff] [blame] | 5464 | ** key that omits the PRIMARY KEY. Compare this key value against the index |
| 5465 | ** that P1 is currently pointing to, ignoring the PRIMARY KEY or ROWID |
| 5466 | ** fields at the end. |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 5467 | ** |
danielk1977 | 61dd583 | 2008-04-18 11:31:12 +0000 | [diff] [blame] | 5468 | ** If the P1 index entry is greater than or equal to the key value |
| 5469 | ** then jump to P2. Otherwise fall through to the next instruction. |
drh | 4a1d365 | 2014-02-14 15:13:36 +0000 | [diff] [blame] | 5470 | */ |
| 5471 | /* Opcode: IdxGT P1 P2 P3 P4 P5 |
| 5472 | ** Synopsis: key=r[P3@P4] |
drh | 772ae62 | 2004-05-19 13:13:08 +0000 | [diff] [blame] | 5473 | ** |
drh | 4a1d365 | 2014-02-14 15:13:36 +0000 | [diff] [blame] | 5474 | ** The P4 register values beginning with P3 form an unpacked index |
| 5475 | ** key that omits the PRIMARY KEY. Compare this key value against the index |
| 5476 | ** that P1 is currently pointing to, ignoring the PRIMARY KEY or ROWID |
| 5477 | ** fields at the end. |
| 5478 | ** |
| 5479 | ** If the P1 index entry is greater than the key value |
| 5480 | ** then jump to P2. Otherwise fall through to the next instruction. |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 5481 | */ |
drh | 3bb9b93 | 2010-08-06 02:10:00 +0000 | [diff] [blame] | 5482 | /* Opcode: IdxLT P1 P2 P3 P4 P5 |
drh | f63552b | 2013-10-30 00:25:03 +0000 | [diff] [blame] | 5483 | ** Synopsis: key=r[P3@P4] |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 5484 | ** |
danielk1977 | 61dd583 | 2008-04-18 11:31:12 +0000 | [diff] [blame] | 5485 | ** The P4 register values beginning with P3 form an unpacked index |
drh | 4a1d365 | 2014-02-14 15:13:36 +0000 | [diff] [blame] | 5486 | ** key that omits the PRIMARY KEY or ROWID. Compare this key value against |
| 5487 | ** the index that P1 is currently pointing to, ignoring the PRIMARY KEY or |
| 5488 | ** ROWID on the P1 index. |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 5489 | ** |
danielk1977 | 61dd583 | 2008-04-18 11:31:12 +0000 | [diff] [blame] | 5490 | ** If the P1 index entry is less than the key value then jump to P2. |
| 5491 | ** Otherwise fall through to the next instruction. |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 5492 | */ |
drh | 4a1d365 | 2014-02-14 15:13:36 +0000 | [diff] [blame] | 5493 | /* Opcode: IdxLE P1 P2 P3 P4 P5 |
| 5494 | ** Synopsis: key=r[P3@P4] |
| 5495 | ** |
| 5496 | ** The P4 register values beginning with P3 form an unpacked index |
| 5497 | ** key that omits the PRIMARY KEY or ROWID. Compare this key value against |
| 5498 | ** the index that P1 is currently pointing to, ignoring the PRIMARY KEY or |
| 5499 | ** ROWID on the P1 index. |
| 5500 | ** |
| 5501 | ** If the P1 index entry is less than or equal to the key value then jump |
| 5502 | ** to P2. Otherwise fall through to the next instruction. |
| 5503 | */ |
| 5504 | case OP_IdxLE: /* jump */ |
| 5505 | case OP_IdxGT: /* jump */ |
drh | 93952eb | 2009-11-13 19:43:43 +0000 | [diff] [blame] | 5506 | case OP_IdxLT: /* jump */ |
drh | 4a1d365 | 2014-02-14 15:13:36 +0000 | [diff] [blame] | 5507 | case OP_IdxGE: { /* jump */ |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 5508 | VdbeCursor *pC; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 5509 | int res; |
| 5510 | UnpackedRecord r; |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 5511 | |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 5512 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 5513 | pC = p->apCsr[pOp->p1]; |
| 5514 | assert( pC!=0 ); |
drh | d4187c7 | 2010-08-30 22:15:45 +0000 | [diff] [blame] | 5515 | assert( pC->isOrdered ); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 5516 | assert( pC->eCurType==CURTYPE_BTREE ); |
| 5517 | assert( pC->uc.pCursor!=0); |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 5518 | assert( pC->deferredMoveto==0 ); |
| 5519 | assert( pOp->p5==0 || pOp->p5==1 ); |
| 5520 | assert( pOp->p4type==P4_INT32 ); |
| 5521 | r.pKeyInfo = pC->pKeyInfo; |
| 5522 | r.nField = (u16)pOp->p4.i; |
drh | 4a1d365 | 2014-02-14 15:13:36 +0000 | [diff] [blame] | 5523 | if( pOp->opcode<OP_IdxLT ){ |
| 5524 | assert( pOp->opcode==OP_IdxLE || pOp->opcode==OP_IdxGT ); |
dan | 1fed5da | 2014-02-25 21:01:25 +0000 | [diff] [blame] | 5525 | r.default_rc = -1; |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 5526 | }else{ |
drh | 4a1d365 | 2014-02-14 15:13:36 +0000 | [diff] [blame] | 5527 | assert( pOp->opcode==OP_IdxGE || pOp->opcode==OP_IdxLT ); |
dan | 1fed5da | 2014-02-25 21:01:25 +0000 | [diff] [blame] | 5528 | r.default_rc = 0; |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 5529 | } |
| 5530 | r.aMem = &aMem[pOp->p3]; |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 5531 | #ifdef SQLITE_DEBUG |
drh | 5eae974 | 2018-08-03 13:56:26 +0000 | [diff] [blame] | 5532 | { |
| 5533 | int i; |
| 5534 | for(i=0; i<r.nField; i++){ |
| 5535 | assert( memIsValid(&r.aMem[i]) ); |
| 5536 | REGISTER_TRACE(pOp->p3+i, &aMem[pOp->p3+i]); |
| 5537 | } |
| 5538 | } |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 5539 | #endif |
drh | 2dc0648 | 2013-12-11 00:59:10 +0000 | [diff] [blame] | 5540 | res = 0; /* Not needed. Only used to silence a warning. */ |
drh | d3b7420 | 2014-09-17 16:41:15 +0000 | [diff] [blame] | 5541 | rc = sqlite3VdbeIdxKeyCompare(db, pC, &r, &res); |
drh | 4a1d365 | 2014-02-14 15:13:36 +0000 | [diff] [blame] | 5542 | assert( (OP_IdxLE&1)==(OP_IdxLT&1) && (OP_IdxGE&1)==(OP_IdxGT&1) ); |
| 5543 | if( (pOp->opcode&1)==(OP_IdxLT&1) ){ |
| 5544 | assert( pOp->opcode==OP_IdxLE || pOp->opcode==OP_IdxLT ); |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 5545 | res = -res; |
| 5546 | }else{ |
drh | 4a1d365 | 2014-02-14 15:13:36 +0000 | [diff] [blame] | 5547 | assert( pOp->opcode==OP_IdxGE || pOp->opcode==OP_IdxGT ); |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 5548 | res++; |
| 5549 | } |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 5550 | VdbeBranchTaken(res>0,2); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 5551 | if( rc ) goto abort_due_to_error; |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 5552 | if( res>0 ) goto jump_to_p2; |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 5553 | break; |
| 5554 | } |
| 5555 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 5556 | /* Opcode: Destroy P1 P2 P3 * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5557 | ** |
| 5558 | ** Delete an entire database table or index whose root page in the database |
| 5559 | ** file is given by P1. |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 5560 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 5561 | ** The table being destroyed is in the main database file if P3==0. If |
| 5562 | ** 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] | 5563 | ** that is used to store tables create using CREATE TEMPORARY TABLE. |
| 5564 | ** |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 5565 | ** If AUTOVACUUM is enabled then it is possible that another root page |
| 5566 | ** might be moved into the newly deleted root page in order to keep all |
| 5567 | ** root pages contiguous at the beginning of the database. The former |
| 5568 | ** value of the root page that moved - its value before the move occurred - |
dan | a34adaf | 2017-04-08 14:11:47 +0000 | [diff] [blame] | 5569 | ** is stored in register P2. If no page movement was required (because the |
| 5570 | ** table being dropped was already the last one in the database) then a |
| 5571 | ** zero is stored in register P2. If AUTOVACUUM is disabled then a zero |
| 5572 | ** is stored in register P2. |
| 5573 | ** |
| 5574 | ** This opcode throws an error if there are any active reader VMs when |
| 5575 | ** it is invoked. This is done to avoid the difficulty associated with |
| 5576 | ** updating existing cursors when a root page is moved in an AUTOVACUUM |
| 5577 | ** database. This error is thrown even if the database is not an AUTOVACUUM |
| 5578 | ** db in order to avoid introducing an incompatibility between autovacuum |
| 5579 | ** and non-autovacuum modes. |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 5580 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 5581 | ** See also: Clear |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5582 | */ |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 5583 | case OP_Destroy: { /* out2 */ |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 5584 | int iMoved; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 5585 | int iDb; |
drh | 3a94987 | 2012-09-18 13:20:13 +0000 | [diff] [blame] | 5586 | |
drh | 4031baf | 2018-05-28 17:31:20 +0000 | [diff] [blame] | 5587 | sqlite3VdbeIncrWriteCounter(p, 0); |
drh | 9e92a47 | 2013-06-27 17:40:30 +0000 | [diff] [blame] | 5588 | assert( p->readOnly==0 ); |
drh | 055f298 | 2016-01-15 15:06:41 +0000 | [diff] [blame] | 5589 | assert( pOp->p1>1 ); |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 5590 | pOut = out2Prerelease(p, pOp); |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 5591 | pOut->flags = MEM_Null; |
drh | 086723a | 2015-03-24 12:51:52 +0000 | [diff] [blame] | 5592 | if( db->nVdbeRead > db->nVDestroy+1 ){ |
danielk1977 | e6efa74 | 2004-11-10 11:55:10 +0000 | [diff] [blame] | 5593 | rc = SQLITE_LOCKED; |
drh | 77658e2 | 2007-12-04 16:54:52 +0000 | [diff] [blame] | 5594 | p->errorAction = OE_Abort; |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 5595 | goto abort_due_to_error; |
danielk1977 | e6efa74 | 2004-11-10 11:55:10 +0000 | [diff] [blame] | 5596 | }else{ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 5597 | iDb = pOp->p3; |
drh | a7ab6d8 | 2014-07-21 15:44:39 +0000 | [diff] [blame] | 5598 | assert( DbMaskTest(p->btreeMask, iDb) ); |
drh | 2dc0648 | 2013-12-11 00:59:10 +0000 | [diff] [blame] | 5599 | iMoved = 0; /* Not needed. Only to silence a warning. */ |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 5600 | rc = sqlite3BtreeDropTable(db->aDb[iDb].pBt, pOp->p1, &iMoved); |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 5601 | pOut->flags = MEM_Int; |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 5602 | pOut->u.i = iMoved; |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 5603 | if( rc ) goto abort_due_to_error; |
drh | 3765df4 | 2006-06-28 18:18:09 +0000 | [diff] [blame] | 5604 | #ifndef SQLITE_OMIT_AUTOVACUUM |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 5605 | if( iMoved!=0 ){ |
drh | cdf011d | 2011-04-04 21:25:28 +0000 | [diff] [blame] | 5606 | sqlite3RootPageMoved(db, iDb, iMoved, pOp->p1); |
| 5607 | /* All OP_Destroy operations occur on the same btree */ |
| 5608 | assert( resetSchemaOnFault==0 || resetSchemaOnFault==iDb+1 ); |
| 5609 | resetSchemaOnFault = iDb+1; |
danielk1977 | e6efa74 | 2004-11-10 11:55:10 +0000 | [diff] [blame] | 5610 | } |
drh | 3765df4 | 2006-06-28 18:18:09 +0000 | [diff] [blame] | 5611 | #endif |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 5612 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5613 | break; |
| 5614 | } |
| 5615 | |
danielk1977 | c7af484 | 2008-10-27 13:59:33 +0000 | [diff] [blame] | 5616 | /* Opcode: Clear P1 P2 P3 |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 5617 | ** |
| 5618 | ** Delete all contents of the database table or index whose root page |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 5619 | ** in the database file is given by P1. But, unlike Destroy, do not |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 5620 | ** remove the table or index from the database file. |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 5621 | ** |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 5622 | ** The table being clear is in the main database file if P2==0. If |
| 5623 | ** P2==1 then the table to be clear is in the auxiliary database file |
| 5624 | ** that is used to store tables create using CREATE TEMPORARY TABLE. |
| 5625 | ** |
shane | be21779 | 2009-03-05 04:20:31 +0000 | [diff] [blame] | 5626 | ** 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] | 5627 | ** intkey table (an SQL table, not an index). In this case the row change |
| 5628 | ** count is incremented by the number of rows in the table being cleared. |
| 5629 | ** If P3 is greater than zero, then the value stored in register P3 is |
| 5630 | ** also incremented by the number of rows in the table being cleared. |
| 5631 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 5632 | ** See also: Destroy |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 5633 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 5634 | case OP_Clear: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 5635 | int nChange; |
| 5636 | |
drh | 4031baf | 2018-05-28 17:31:20 +0000 | [diff] [blame] | 5637 | sqlite3VdbeIncrWriteCounter(p, 0); |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 5638 | nChange = 0; |
drh | 9e92a47 | 2013-06-27 17:40:30 +0000 | [diff] [blame] | 5639 | assert( p->readOnly==0 ); |
drh | a7ab6d8 | 2014-07-21 15:44:39 +0000 | [diff] [blame] | 5640 | assert( DbMaskTest(p->btreeMask, pOp->p2) ); |
danielk1977 | c7af484 | 2008-10-27 13:59:33 +0000 | [diff] [blame] | 5641 | rc = sqlite3BtreeClearTable( |
| 5642 | db->aDb[pOp->p2].pBt, pOp->p1, (pOp->p3 ? &nChange : 0) |
| 5643 | ); |
| 5644 | if( pOp->p3 ){ |
| 5645 | p->nChange += nChange; |
| 5646 | if( pOp->p3>0 ){ |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 5647 | assert( memIsValid(&aMem[pOp->p3]) ); |
| 5648 | memAboutToChange(p, &aMem[pOp->p3]); |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 5649 | aMem[pOp->p3].u.i += nChange; |
danielk1977 | c7af484 | 2008-10-27 13:59:33 +0000 | [diff] [blame] | 5650 | } |
| 5651 | } |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 5652 | if( rc ) goto abort_due_to_error; |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 5653 | break; |
| 5654 | } |
| 5655 | |
drh | 65ea12c | 2014-03-19 17:41:36 +0000 | [diff] [blame] | 5656 | /* Opcode: ResetSorter P1 * * * * |
drh | 079a307 | 2014-03-19 14:10:55 +0000 | [diff] [blame] | 5657 | ** |
drh | 65ea12c | 2014-03-19 17:41:36 +0000 | [diff] [blame] | 5658 | ** Delete all contents from the ephemeral table or sorter |
| 5659 | ** that is open on cursor P1. |
drh | 079a307 | 2014-03-19 14:10:55 +0000 | [diff] [blame] | 5660 | ** |
drh | 65ea12c | 2014-03-19 17:41:36 +0000 | [diff] [blame] | 5661 | ** This opcode only works for cursors used for sorting and |
| 5662 | ** opened with OP_OpenEphemeral or OP_SorterOpen. |
drh | 079a307 | 2014-03-19 14:10:55 +0000 | [diff] [blame] | 5663 | */ |
drh | 65ea12c | 2014-03-19 17:41:36 +0000 | [diff] [blame] | 5664 | case OP_ResetSorter: { |
drh | 079a307 | 2014-03-19 14:10:55 +0000 | [diff] [blame] | 5665 | VdbeCursor *pC; |
| 5666 | |
| 5667 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 5668 | pC = p->apCsr[pOp->p1]; |
| 5669 | assert( pC!=0 ); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 5670 | if( isSorter(pC) ){ |
| 5671 | sqlite3VdbeSorterReset(db, pC->uc.pSorter); |
drh | 65ea12c | 2014-03-19 17:41:36 +0000 | [diff] [blame] | 5672 | }else{ |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 5673 | assert( pC->eCurType==CURTYPE_BTREE ); |
drh | 65ea12c | 2014-03-19 17:41:36 +0000 | [diff] [blame] | 5674 | assert( pC->isEphemeral ); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 5675 | rc = sqlite3BtreeClearTableOfCursor(pC->uc.pCursor); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 5676 | if( rc ) goto abort_due_to_error; |
drh | 65ea12c | 2014-03-19 17:41:36 +0000 | [diff] [blame] | 5677 | } |
drh | 079a307 | 2014-03-19 14:10:55 +0000 | [diff] [blame] | 5678 | break; |
| 5679 | } |
| 5680 | |
drh | 0f3f766 | 2017-08-18 14:34:28 +0000 | [diff] [blame] | 5681 | /* Opcode: CreateBtree P1 P2 P3 * * |
| 5682 | ** Synopsis: r[P2]=root iDb=P1 flags=P3 |
drh | 5b2fd56 | 2001-09-13 15:21:31 +0000 | [diff] [blame] | 5683 | ** |
drh | 0f3f766 | 2017-08-18 14:34:28 +0000 | [diff] [blame] | 5684 | ** Allocate a new b-tree in the main database file if P1==0 or in the |
| 5685 | ** TEMP database file if P1==1 or in an attached database if |
| 5686 | ** P1>1. The P3 argument must be 1 (BTREE_INTKEY) for a rowid table |
drh | 416a801 | 2018-05-31 19:14:52 +0000 | [diff] [blame] | 5687 | ** it must be 2 (BTREE_BLOBKEY) for an index or WITHOUT ROWID table. |
drh | 0f3f766 | 2017-08-18 14:34:28 +0000 | [diff] [blame] | 5688 | ** The root page number of the new b-tree is stored in register P2. |
drh | 5b2fd56 | 2001-09-13 15:21:31 +0000 | [diff] [blame] | 5689 | */ |
drh | 0f3f766 | 2017-08-18 14:34:28 +0000 | [diff] [blame] | 5690 | case OP_CreateBtree: { /* out2 */ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 5691 | int pgno; |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 5692 | Db *pDb; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 5693 | |
drh | 4031baf | 2018-05-28 17:31:20 +0000 | [diff] [blame] | 5694 | sqlite3VdbeIncrWriteCounter(p, 0); |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 5695 | pOut = out2Prerelease(p, pOp); |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 5696 | pgno = 0; |
drh | 0f3f766 | 2017-08-18 14:34:28 +0000 | [diff] [blame] | 5697 | assert( pOp->p3==BTREE_INTKEY || pOp->p3==BTREE_BLOBKEY ); |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 5698 | assert( pOp->p1>=0 && pOp->p1<db->nDb ); |
drh | a7ab6d8 | 2014-07-21 15:44:39 +0000 | [diff] [blame] | 5699 | assert( DbMaskTest(p->btreeMask, pOp->p1) ); |
drh | 9e92a47 | 2013-06-27 17:40:30 +0000 | [diff] [blame] | 5700 | assert( p->readOnly==0 ); |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 5701 | pDb = &db->aDb[pOp->p1]; |
| 5702 | assert( pDb->pBt!=0 ); |
drh | 0f3f766 | 2017-08-18 14:34:28 +0000 | [diff] [blame] | 5703 | rc = sqlite3BtreeCreateTable(pDb->pBt, &pgno, pOp->p3); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 5704 | if( rc ) goto abort_due_to_error; |
drh | 88a003e | 2008-12-11 16:17:03 +0000 | [diff] [blame] | 5705 | pOut->u.i = pgno; |
drh | 5b2fd56 | 2001-09-13 15:21:31 +0000 | [diff] [blame] | 5706 | break; |
| 5707 | } |
| 5708 | |
drh | 4a54bb5 | 2017-02-18 15:58:52 +0000 | [diff] [blame] | 5709 | /* Opcode: SqlExec * * * P4 * |
| 5710 | ** |
| 5711 | ** Run the SQL statement or statements specified in the P4 string. |
| 5712 | */ |
| 5713 | case OP_SqlExec: { |
drh | 4031baf | 2018-05-28 17:31:20 +0000 | [diff] [blame] | 5714 | sqlite3VdbeIncrWriteCounter(p, 0); |
drh | bce0414 | 2017-02-23 00:58:36 +0000 | [diff] [blame] | 5715 | db->nSqlExec++; |
drh | 4a54bb5 | 2017-02-18 15:58:52 +0000 | [diff] [blame] | 5716 | rc = sqlite3_exec(db, pOp->p4.z, 0, 0, 0); |
drh | bce0414 | 2017-02-23 00:58:36 +0000 | [diff] [blame] | 5717 | db->nSqlExec--; |
drh | 4a54bb5 | 2017-02-18 15:58:52 +0000 | [diff] [blame] | 5718 | if( rc ) goto abort_due_to_error; |
| 5719 | break; |
| 5720 | } |
| 5721 | |
drh | 2264584 | 2011-03-24 01:34:03 +0000 | [diff] [blame] | 5722 | /* Opcode: ParseSchema P1 * * P4 * |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 5723 | ** |
| 5724 | ** Read and parse all entries from the SQLITE_MASTER table of database P1 |
dan | 987db76 | 2018-08-14 20:18:50 +0000 | [diff] [blame] | 5725 | ** that match the WHERE clause P4. If P4 is a NULL pointer, then the |
| 5726 | ** entire schema for P1 is reparsed. |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 5727 | ** |
| 5728 | ** This opcode invokes the parser to create a new virtual machine, |
shane | 21e7feb | 2008-05-30 15:59:49 +0000 | [diff] [blame] | 5729 | ** then runs the new virtual machine. It is thus a re-entrant opcode. |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 5730 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 5731 | case OP_ParseSchema: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 5732 | int iDb; |
| 5733 | const char *zMaster; |
| 5734 | char *zSql; |
| 5735 | InitData initData; |
| 5736 | |
drh | bdaec52 | 2011-04-04 00:14:43 +0000 | [diff] [blame] | 5737 | /* Any prepared statement that invokes this opcode will hold mutexes |
| 5738 | ** on every btree. This is a prerequisite for invoking |
| 5739 | ** sqlite3InitCallback(). |
| 5740 | */ |
| 5741 | #ifdef SQLITE_DEBUG |
| 5742 | for(iDb=0; iDb<db->nDb; iDb++){ |
| 5743 | assert( iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) ); |
| 5744 | } |
| 5745 | #endif |
drh | bdaec52 | 2011-04-04 00:14:43 +0000 | [diff] [blame] | 5746 | |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 5747 | iDb = pOp->p1; |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 5748 | assert( iDb>=0 && iDb<db->nDb ); |
dan | 6c15487 | 2011-04-02 09:44:43 +0000 | [diff] [blame] | 5749 | assert( DbHasProperty(db, iDb, DB_SchemaLoaded) ); |
dan | e325ffe | 2018-08-11 13:40:20 +0000 | [diff] [blame] | 5750 | |
| 5751 | #ifndef SQLITE_OMIT_ALTERTABLE |
| 5752 | if( pOp->p4.z==0 ){ |
| 5753 | sqlite3SchemaClear(db->aDb[iDb].pSchema); |
dan | b0c7920 | 2018-08-11 18:34:25 +0000 | [diff] [blame] | 5754 | db->mDbFlags &= ~DBFLAG_SchemaKnownOk; |
dan | 987db76 | 2018-08-14 20:18:50 +0000 | [diff] [blame] | 5755 | rc = sqlite3InitOne(db, iDb, &p->zErrMsg, INITFLAG_AlterTable); |
dan | e325ffe | 2018-08-11 13:40:20 +0000 | [diff] [blame] | 5756 | db->mDbFlags |= DBFLAG_SchemaChange; |
| 5757 | }else |
| 5758 | #endif |
dan | 987db76 | 2018-08-14 20:18:50 +0000 | [diff] [blame] | 5759 | { |
drh | e0a04a3 | 2016-12-16 01:00:21 +0000 | [diff] [blame] | 5760 | zMaster = MASTER_NAME; |
danielk1977 | a8bbef8 | 2009-03-23 17:11:26 +0000 | [diff] [blame] | 5761 | initData.db = db; |
| 5762 | initData.iDb = pOp->p1; |
| 5763 | initData.pzErrMsg = &p->zErrMsg; |
| 5764 | zSql = sqlite3MPrintf(db, |
drh | 6a9c64b | 2010-01-12 23:54:14 +0000 | [diff] [blame] | 5765 | "SELECT name, rootpage, sql FROM '%q'.%s WHERE %s ORDER BY rowid", |
drh | 69c3382 | 2016-08-18 14:33:11 +0000 | [diff] [blame] | 5766 | db->aDb[iDb].zDbSName, zMaster, pOp->p4.z); |
danielk1977 | a8bbef8 | 2009-03-23 17:11:26 +0000 | [diff] [blame] | 5767 | if( zSql==0 ){ |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 5768 | rc = SQLITE_NOMEM_BKPT; |
danielk1977 | a8bbef8 | 2009-03-23 17:11:26 +0000 | [diff] [blame] | 5769 | }else{ |
danielk1977 | a8bbef8 | 2009-03-23 17:11:26 +0000 | [diff] [blame] | 5770 | assert( db->init.busy==0 ); |
| 5771 | db->init.busy = 1; |
| 5772 | initData.rc = SQLITE_OK; |
| 5773 | assert( !db->mallocFailed ); |
| 5774 | rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0); |
| 5775 | if( rc==SQLITE_OK ) rc = initData.rc; |
drh | dbd6a7d | 2017-04-05 12:39:49 +0000 | [diff] [blame] | 5776 | sqlite3DbFreeNN(db, zSql); |
danielk1977 | a8bbef8 | 2009-03-23 17:11:26 +0000 | [diff] [blame] | 5777 | db->init.busy = 0; |
danielk1977 | a8bbef8 | 2009-03-23 17:11:26 +0000 | [diff] [blame] | 5778 | } |
drh | 3c23a88 | 2007-01-09 14:01:13 +0000 | [diff] [blame] | 5779 | } |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 5780 | if( rc ){ |
| 5781 | sqlite3ResetAllSchemasOfConnection(db); |
| 5782 | if( rc==SQLITE_NOMEM ){ |
| 5783 | goto no_mem; |
| 5784 | } |
| 5785 | goto abort_due_to_error; |
danielk1977 | 261919c | 2005-12-06 12:52:59 +0000 | [diff] [blame] | 5786 | } |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 5787 | break; |
| 5788 | } |
| 5789 | |
drh | 8bfdf72 | 2009-06-19 14:06:03 +0000 | [diff] [blame] | 5790 | #if !defined(SQLITE_OMIT_ANALYZE) |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 5791 | /* Opcode: LoadAnalysis P1 * * * * |
drh | 497e446 | 2005-07-23 03:18:40 +0000 | [diff] [blame] | 5792 | ** |
| 5793 | ** Read the sqlite_stat1 table for database P1 and load the content |
| 5794 | ** of that table into the internal index hash table. This will cause |
| 5795 | ** the analysis to be used when preparing all subsequent queries. |
| 5796 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 5797 | case OP_LoadAnalysis: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 5798 | assert( pOp->p1>=0 && pOp->p1<db->nDb ); |
| 5799 | rc = sqlite3AnalysisLoad(db, pOp->p1); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 5800 | if( rc ) goto abort_due_to_error; |
drh | 497e446 | 2005-07-23 03:18:40 +0000 | [diff] [blame] | 5801 | break; |
| 5802 | } |
drh | 8bfdf72 | 2009-06-19 14:06:03 +0000 | [diff] [blame] | 5803 | #endif /* !defined(SQLITE_OMIT_ANALYZE) */ |
drh | 497e446 | 2005-07-23 03:18:40 +0000 | [diff] [blame] | 5804 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 5805 | /* Opcode: DropTable P1 * * P4 * |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 5806 | ** |
| 5807 | ** Remove the internal (in-memory) data structures that describe |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 5808 | ** the table named P4 in database P1. This is called after a table |
drh | 5dad9a3 | 2014-07-25 18:37:42 +0000 | [diff] [blame] | 5809 | ** is dropped from disk (using the Destroy opcode) in order to keep |
| 5810 | ** the internal representation of the |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 5811 | ** schema consistent with what is on disk. |
| 5812 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 5813 | case OP_DropTable: { |
drh | 4031baf | 2018-05-28 17:31:20 +0000 | [diff] [blame] | 5814 | sqlite3VdbeIncrWriteCounter(p, 0); |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 5815 | sqlite3UnlinkAndDeleteTable(db, pOp->p1, pOp->p4.z); |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 5816 | break; |
| 5817 | } |
| 5818 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 5819 | /* Opcode: DropIndex P1 * * P4 * |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 5820 | ** |
| 5821 | ** Remove the internal (in-memory) data structures that describe |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 5822 | ** the index named P4 in database P1. This is called after an index |
drh | 5dad9a3 | 2014-07-25 18:37:42 +0000 | [diff] [blame] | 5823 | ** is dropped from disk (using the Destroy opcode) |
| 5824 | ** in order to keep the internal representation of the |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 5825 | ** schema consistent with what is on disk. |
| 5826 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 5827 | case OP_DropIndex: { |
drh | 4031baf | 2018-05-28 17:31:20 +0000 | [diff] [blame] | 5828 | sqlite3VdbeIncrWriteCounter(p, 0); |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 5829 | sqlite3UnlinkAndDeleteIndex(db, pOp->p1, pOp->p4.z); |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 5830 | break; |
| 5831 | } |
| 5832 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 5833 | /* Opcode: DropTrigger P1 * * P4 * |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 5834 | ** |
| 5835 | ** Remove the internal (in-memory) data structures that describe |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 5836 | ** the trigger named P4 in database P1. This is called after a trigger |
drh | 5dad9a3 | 2014-07-25 18:37:42 +0000 | [diff] [blame] | 5837 | ** is dropped from disk (using the Destroy opcode) in order to keep |
| 5838 | ** the internal representation of the |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 5839 | ** schema consistent with what is on disk. |
| 5840 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 5841 | case OP_DropTrigger: { |
drh | 4031baf | 2018-05-28 17:31:20 +0000 | [diff] [blame] | 5842 | sqlite3VdbeIncrWriteCounter(p, 0); |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 5843 | sqlite3UnlinkAndDeleteTrigger(db, pOp->p1, pOp->p4.z); |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 5844 | break; |
| 5845 | } |
| 5846 | |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 5847 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 5848 | #ifndef SQLITE_OMIT_INTEGRITY_CHECK |
drh | 98968b2 | 2016-03-15 22:00:39 +0000 | [diff] [blame] | 5849 | /* Opcode: IntegrityCk P1 P2 P3 P4 P5 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5850 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 5851 | ** Do an analysis of the currently open database. Store in |
| 5852 | ** register P1 the text of an error message describing any problems. |
| 5853 | ** If no problems are found, store a NULL in register P1. |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 5854 | ** |
drh | 66accfc | 2017-02-22 18:04:42 +0000 | [diff] [blame] | 5855 | ** The register P3 contains one less than the maximum number of allowed errors. |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 5856 | ** At most reg(P3) errors will be reported. |
| 5857 | ** In other words, the analysis stops as soon as reg(P1) errors are |
| 5858 | ** seen. Reg(P1) is updated with the number of errors remaining. |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 5859 | ** |
drh | 98968b2 | 2016-03-15 22:00:39 +0000 | [diff] [blame] | 5860 | ** The root page numbers of all tables in the database are integers |
| 5861 | ** stored in P4_INTARRAY argument. |
drh | 2150432 | 2002-06-25 13:16:02 +0000 | [diff] [blame] | 5862 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 5863 | ** If P5 is not zero, the check is done on the auxiliary database |
drh | 2150432 | 2002-06-25 13:16:02 +0000 | [diff] [blame] | 5864 | ** file, not the main database file. |
drh | 1dd397f | 2002-02-03 03:34:07 +0000 | [diff] [blame] | 5865 | ** |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 5866 | ** This opcode is used to implement the integrity_check pragma. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5867 | */ |
drh | aaab572 | 2002-02-19 13:39:21 +0000 | [diff] [blame] | 5868 | case OP_IntegrityCk: { |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 5869 | int nRoot; /* Number of tables to check. (Number of root pages.) */ |
| 5870 | int *aRoot; /* Array of rootpage numbers for tables to be checked */ |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 5871 | int nErr; /* Number of errors reported */ |
| 5872 | char *z; /* Text of the error report */ |
| 5873 | Mem *pnErr; /* Register keeping track of errors remaining */ |
drh | 9e92a47 | 2013-06-27 17:40:30 +0000 | [diff] [blame] | 5874 | |
drh | 1713afb | 2013-06-28 01:24:57 +0000 | [diff] [blame] | 5875 | assert( p->bIsReader ); |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 5876 | nRoot = pOp->p2; |
drh | 98968b2 | 2016-03-15 22:00:39 +0000 | [diff] [blame] | 5877 | aRoot = pOp->p4.ai; |
drh | 7906975 | 2004-05-22 21:30:40 +0000 | [diff] [blame] | 5878 | assert( nRoot>0 ); |
drh | b5c1063 | 2017-09-21 00:49:15 +0000 | [diff] [blame] | 5879 | assert( aRoot[0]==nRoot ); |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 5880 | assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) ); |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 5881 | pnErr = &aMem[pOp->p3]; |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 5882 | assert( (pnErr->flags & MEM_Int)!=0 ); |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 5883 | assert( (pnErr->flags & (MEM_Str|MEM_Blob))==0 ); |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 5884 | pIn1 = &aMem[pOp->p1]; |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 5885 | assert( pOp->p5<db->nDb ); |
drh | a7ab6d8 | 2014-07-21 15:44:39 +0000 | [diff] [blame] | 5886 | assert( DbMaskTest(p->btreeMask, pOp->p5) ); |
drh | b5c1063 | 2017-09-21 00:49:15 +0000 | [diff] [blame] | 5887 | z = sqlite3BtreeIntegrityCheck(db->aDb[pOp->p5].pBt, &aRoot[1], nRoot, |
drh | 66accfc | 2017-02-22 18:04:42 +0000 | [diff] [blame] | 5888 | (int)pnErr->u.i+1, &nErr); |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 5889 | sqlite3VdbeMemSetNull(pIn1); |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 5890 | if( nErr==0 ){ |
| 5891 | assert( z==0 ); |
drh | c890fec | 2008-08-01 20:10:08 +0000 | [diff] [blame] | 5892 | }else if( z==0 ){ |
| 5893 | goto no_mem; |
drh | 1dd397f | 2002-02-03 03:34:07 +0000 | [diff] [blame] | 5894 | }else{ |
drh | 66accfc | 2017-02-22 18:04:42 +0000 | [diff] [blame] | 5895 | pnErr->u.i -= nErr-1; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 5896 | sqlite3VdbeMemSetStr(pIn1, z, -1, SQLITE_UTF8, sqlite3_free); |
danielk1977 | 8a6b541 | 2004-05-24 07:04:25 +0000 | [diff] [blame] | 5897 | } |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 5898 | UPDATE_MAX_BLOBSIZE(pIn1); |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 5899 | sqlite3VdbeChangeEncoding(pIn1, encoding); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5900 | break; |
| 5901 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 5902 | #endif /* SQLITE_OMIT_INTEGRITY_CHECK */ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5903 | |
drh | 3d4501e | 2008-12-04 20:40:10 +0000 | [diff] [blame] | 5904 | /* Opcode: RowSetAdd P1 P2 * * * |
drh | 72e26de | 2016-08-24 21:24:04 +0000 | [diff] [blame] | 5905 | ** Synopsis: rowset(P1)=r[P2] |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5906 | ** |
drh | bb6783b | 2017-04-29 18:02:49 +0000 | [diff] [blame] | 5907 | ** Insert the integer value held by register P2 into a RowSet object |
drh | 3d4501e | 2008-12-04 20:40:10 +0000 | [diff] [blame] | 5908 | ** held in register P1. |
| 5909 | ** |
| 5910 | ** An assertion fails if P2 is not an integer. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5911 | */ |
drh | 93952eb | 2009-11-13 19:43:43 +0000 | [diff] [blame] | 5912 | case OP_RowSetAdd: { /* in1, in2 */ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 5913 | pIn1 = &aMem[pOp->p1]; |
| 5914 | pIn2 = &aMem[pOp->p2]; |
drh | 93952eb | 2009-11-13 19:43:43 +0000 | [diff] [blame] | 5915 | assert( (pIn2->flags & MEM_Int)!=0 ); |
| 5916 | if( (pIn1->flags & MEM_RowSet)==0 ){ |
| 5917 | sqlite3VdbeMemSetRowSet(pIn1); |
| 5918 | if( (pIn1->flags & MEM_RowSet)==0 ) goto no_mem; |
drh | 3d4501e | 2008-12-04 20:40:10 +0000 | [diff] [blame] | 5919 | } |
drh | 93952eb | 2009-11-13 19:43:43 +0000 | [diff] [blame] | 5920 | sqlite3RowSetInsert(pIn1->u.pRowSet, pIn2->u.i); |
drh | 3d4501e | 2008-12-04 20:40:10 +0000 | [diff] [blame] | 5921 | break; |
| 5922 | } |
| 5923 | |
| 5924 | /* Opcode: RowSetRead P1 P2 P3 * * |
drh | 72e26de | 2016-08-24 21:24:04 +0000 | [diff] [blame] | 5925 | ** Synopsis: r[P3]=rowset(P1) |
drh | 3d4501e | 2008-12-04 20:40:10 +0000 | [diff] [blame] | 5926 | ** |
drh | bb6783b | 2017-04-29 18:02:49 +0000 | [diff] [blame] | 5927 | ** Extract the smallest value from the RowSet object in P1 |
| 5928 | ** and put that value into register P3. |
| 5929 | ** Or, if RowSet object P1 is initially empty, leave P3 |
drh | 3d4501e | 2008-12-04 20:40:10 +0000 | [diff] [blame] | 5930 | ** unchanged and jump to instruction P2. |
| 5931 | */ |
drh | 93952eb | 2009-11-13 19:43:43 +0000 | [diff] [blame] | 5932 | case OP_RowSetRead: { /* jump, in1, out3 */ |
drh | 3d4501e | 2008-12-04 20:40:10 +0000 | [diff] [blame] | 5933 | i64 val; |
drh | 49afe3a | 2013-07-10 03:05:14 +0000 | [diff] [blame] | 5934 | |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 5935 | pIn1 = &aMem[pOp->p1]; |
drh | 93952eb | 2009-11-13 19:43:43 +0000 | [diff] [blame] | 5936 | if( (pIn1->flags & MEM_RowSet)==0 |
| 5937 | || sqlite3RowSetNext(pIn1->u.pRowSet, &val)==0 |
drh | 3d4501e | 2008-12-04 20:40:10 +0000 | [diff] [blame] | 5938 | ){ |
| 5939 | /* The boolean index is empty */ |
drh | 93952eb | 2009-11-13 19:43:43 +0000 | [diff] [blame] | 5940 | sqlite3VdbeMemSetNull(pIn1); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 5941 | VdbeBranchTaken(1,2); |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 5942 | goto jump_to_p2_and_check_for_interrupt; |
drh | 3d4501e | 2008-12-04 20:40:10 +0000 | [diff] [blame] | 5943 | }else{ |
| 5944 | /* A value was pulled from the index */ |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 5945 | VdbeBranchTaken(0,2); |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 5946 | sqlite3VdbeMemSetInt64(&aMem[pOp->p3], val); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 5947 | } |
drh | 49afe3a | 2013-07-10 03:05:14 +0000 | [diff] [blame] | 5948 | goto check_for_interrupt; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5949 | } |
| 5950 | |
drh | 1b26c7c | 2009-04-22 02:15:47 +0000 | [diff] [blame] | 5951 | /* Opcode: RowSetTest P1 P2 P3 P4 |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 5952 | ** Synopsis: if r[P3] in rowset(P1) goto P2 |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 5953 | ** |
drh | ade9760 | 2009-04-21 15:05:18 +0000 | [diff] [blame] | 5954 | ** 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] | 5955 | ** contains a RowSet object and that RowSet object contains |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 5956 | ** the value held in P3, jump to register P2. Otherwise, insert the |
drh | 1b26c7c | 2009-04-22 02:15:47 +0000 | [diff] [blame] | 5957 | ** integer in P3 into the RowSet and continue on to the |
drh | ade9760 | 2009-04-21 15:05:18 +0000 | [diff] [blame] | 5958 | ** next opcode. |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 5959 | ** |
drh | bb6783b | 2017-04-29 18:02:49 +0000 | [diff] [blame] | 5960 | ** The RowSet object is optimized for the case where sets of integers |
| 5961 | ** are inserted in distinct phases, which each set contains no duplicates. |
| 5962 | ** Each set is identified by a unique P4 value. The first set |
| 5963 | ** must have P4==0, the final set must have P4==-1, and for all other sets |
| 5964 | ** must have P4>0. |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 5965 | ** |
| 5966 | ** This allows optimizations: (a) when P4==0 there is no need to test |
drh | bb6783b | 2017-04-29 18:02:49 +0000 | [diff] [blame] | 5967 | ** the RowSet object for P3, as it is guaranteed not to contain it, |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 5968 | ** (b) when P4==-1 there is no need to insert the value, as it will |
| 5969 | ** never be tested for, and (c) when a value that is part of set X is |
| 5970 | ** inserted, there is no need to search to see if the same value was |
| 5971 | ** previously inserted as part of set X (only if it was previously |
| 5972 | ** inserted as part of some other set). |
| 5973 | */ |
drh | 1b26c7c | 2009-04-22 02:15:47 +0000 | [diff] [blame] | 5974 | case OP_RowSetTest: { /* jump, in1, in3 */ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 5975 | int iSet; |
| 5976 | int exists; |
| 5977 | |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 5978 | pIn1 = &aMem[pOp->p1]; |
| 5979 | pIn3 = &aMem[pOp->p3]; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 5980 | iSet = pOp->p4.i; |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 5981 | assert( pIn3->flags&MEM_Int ); |
| 5982 | |
drh | 1b26c7c | 2009-04-22 02:15:47 +0000 | [diff] [blame] | 5983 | /* If there is anything other than a rowset object in memory cell P1, |
| 5984 | ** delete it now and initialize P1 with an empty rowset |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 5985 | */ |
drh | 733bf1b | 2009-04-22 00:47:00 +0000 | [diff] [blame] | 5986 | if( (pIn1->flags & MEM_RowSet)==0 ){ |
| 5987 | sqlite3VdbeMemSetRowSet(pIn1); |
| 5988 | if( (pIn1->flags & MEM_RowSet)==0 ) goto no_mem; |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 5989 | } |
| 5990 | |
| 5991 | assert( pOp->p4type==P4_INT32 ); |
drh | 1b26c7c | 2009-04-22 02:15:47 +0000 | [diff] [blame] | 5992 | assert( iSet==-1 || iSet>=0 ); |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 5993 | if( iSet ){ |
drh | d83cad2 | 2014-04-10 02:24:48 +0000 | [diff] [blame] | 5994 | exists = sqlite3RowSetTest(pIn1->u.pRowSet, iSet, pIn3->u.i); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 5995 | VdbeBranchTaken(exists!=0,2); |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 5996 | if( exists ) goto jump_to_p2; |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 5997 | } |
| 5998 | if( iSet>=0 ){ |
drh | 733bf1b | 2009-04-22 00:47:00 +0000 | [diff] [blame] | 5999 | sqlite3RowSetInsert(pIn1->u.pRowSet, pIn3->u.i); |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 6000 | } |
| 6001 | break; |
| 6002 | } |
| 6003 | |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 6004 | |
danielk1977 | 93758c8 | 2005-01-21 08:13:14 +0000 | [diff] [blame] | 6005 | #ifndef SQLITE_OMIT_TRIGGER |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 6006 | |
drh | 0fd6135 | 2014-02-07 02:29:45 +0000 | [diff] [blame] | 6007 | /* Opcode: Program P1 P2 P3 P4 P5 |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 6008 | ** |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 6009 | ** Execute the trigger program passed as P4 (type P4_SUBPROGRAM). |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 6010 | ** |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 6011 | ** P1 contains the address of the memory cell that contains the first memory |
| 6012 | ** cell in an array of values used as arguments to the sub-program. P2 |
| 6013 | ** contains the address to jump to if the sub-program throws an IGNORE |
| 6014 | ** exception using the RAISE() function. Register P3 contains the address |
| 6015 | ** of a memory cell in this (the parent) VM that is used to allocate the |
| 6016 | ** memory required by the sub-vdbe at runtime. |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 6017 | ** |
| 6018 | ** P4 is a pointer to the VM containing the trigger program. |
drh | 0fd6135 | 2014-02-07 02:29:45 +0000 | [diff] [blame] | 6019 | ** |
| 6020 | ** If P5 is non-zero, then recursive program invocation is enabled. |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 6021 | */ |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 6022 | case OP_Program: { /* jump */ |
dan | 65a7cd1 | 2009-09-01 12:16:01 +0000 | [diff] [blame] | 6023 | int nMem; /* Number of memory registers for sub-program */ |
| 6024 | int nByte; /* Bytes of runtime space required for sub-program */ |
| 6025 | Mem *pRt; /* Register to allocate runtime space */ |
| 6026 | Mem *pMem; /* Used to iterate through memory cells */ |
| 6027 | Mem *pEnd; /* Last memory cell in new array */ |
| 6028 | VdbeFrame *pFrame; /* New vdbe frame to execute in */ |
| 6029 | SubProgram *pProgram; /* Sub-program to execute */ |
| 6030 | void *t; /* Token identifying trigger */ |
| 6031 | |
| 6032 | pProgram = pOp->p4.pProgram; |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 6033 | pRt = &aMem[pOp->p3]; |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 6034 | assert( pProgram->nOp>0 ); |
| 6035 | |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 6036 | /* If the p5 flag is clear, then recursive invocation of triggers is |
| 6037 | ** disabled for backwards compatibility (p5 is set if this sub-program |
| 6038 | ** is really a trigger, not a foreign key action, and the flag set |
| 6039 | ** and cleared by the "PRAGMA recursive_triggers" command is clear). |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 6040 | ** |
| 6041 | ** It is recursive invocation of triggers, at the SQL level, that is |
| 6042 | ** disabled. In some cases a single trigger may generate more than one |
| 6043 | ** SubProgram (if the trigger may be executed with more than one different |
| 6044 | ** ON CONFLICT algorithm). SubProgram structures associated with a |
| 6045 | ** single trigger all have the same value for the SubProgram.token |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 6046 | ** variable. */ |
| 6047 | if( pOp->p5 ){ |
dan | 65a7cd1 | 2009-09-01 12:16:01 +0000 | [diff] [blame] | 6048 | t = pProgram->token; |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 6049 | for(pFrame=p->pFrame; pFrame && pFrame->token!=t; pFrame=pFrame->pParent); |
| 6050 | if( pFrame ) break; |
| 6051 | } |
| 6052 | |
dan | f589450 | 2009-10-07 18:41:19 +0000 | [diff] [blame] | 6053 | if( p->nFrame>=db->aLimit[SQLITE_LIMIT_TRIGGER_DEPTH] ){ |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 6054 | rc = SQLITE_ERROR; |
drh | 22c17b8 | 2015-05-15 04:13:15 +0000 | [diff] [blame] | 6055 | sqlite3VdbeError(p, "too many levels of trigger recursion"); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 6056 | goto abort_due_to_error; |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 6057 | } |
| 6058 | |
| 6059 | /* Register pRt is used to store the memory required to save the state |
| 6060 | ** of the current program, and the memory required at runtime to execute |
| 6061 | ** the trigger program. If this trigger has been fired before, then pRt |
| 6062 | ** is already allocated. Otherwise, it must be initialized. */ |
| 6063 | if( (pRt->flags&MEM_Frame)==0 ){ |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 6064 | /* SubProgram.nMem is set to the number of memory cells used by the |
| 6065 | ** program stored in SubProgram.aOp. As well as these, one memory |
| 6066 | ** cell is required for each cursor used by the program. Set local |
| 6067 | ** variable nMem (and later, VdbeFrame.nChildMem) to this value. |
| 6068 | */ |
dan | 65a7cd1 | 2009-09-01 12:16:01 +0000 | [diff] [blame] | 6069 | nMem = pProgram->nMem + pProgram->nCsr; |
drh | 3cdce92 | 2016-03-21 00:30:40 +0000 | [diff] [blame] | 6070 | assert( nMem>0 ); |
| 6071 | if( pProgram->nCsr==0 ) nMem++; |
dan | 65a7cd1 | 2009-09-01 12:16:01 +0000 | [diff] [blame] | 6072 | nByte = ROUND8(sizeof(VdbeFrame)) |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 6073 | + nMem * sizeof(Mem) |
drh | ab087d4 | 2017-03-24 17:59:56 +0000 | [diff] [blame] | 6074 | + pProgram->nCsr * sizeof(VdbeCursor*) |
| 6075 | + (pProgram->nOp + 7)/8; |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 6076 | pFrame = sqlite3DbMallocZero(db, nByte); |
| 6077 | if( !pFrame ){ |
| 6078 | goto no_mem; |
| 6079 | } |
| 6080 | sqlite3VdbeMemRelease(pRt); |
| 6081 | pRt->flags = MEM_Frame; |
| 6082 | pRt->u.pFrame = pFrame; |
| 6083 | |
| 6084 | pFrame->v = p; |
| 6085 | pFrame->nChildMem = nMem; |
| 6086 | pFrame->nChildCsr = pProgram->nCsr; |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 6087 | pFrame->pc = (int)(pOp - aOp); |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 6088 | pFrame->aMem = p->aMem; |
| 6089 | pFrame->nMem = p->nMem; |
| 6090 | pFrame->apCsr = p->apCsr; |
| 6091 | pFrame->nCursor = p->nCursor; |
| 6092 | pFrame->aOp = p->aOp; |
| 6093 | pFrame->nOp = p->nOp; |
| 6094 | pFrame->token = pProgram->token; |
dan | e2f771b | 2014-11-03 15:33:17 +0000 | [diff] [blame] | 6095 | #ifdef SQLITE_ENABLE_STMT_SCANSTATUS |
dan | 43764a8 | 2014-11-01 21:00:04 +0000 | [diff] [blame] | 6096 | pFrame->anExec = p->anExec; |
dan | e2f771b | 2014-11-03 15:33:17 +0000 | [diff] [blame] | 6097 | #endif |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 6098 | |
| 6099 | pEnd = &VdbeFrameMem(pFrame)[pFrame->nChildMem]; |
| 6100 | for(pMem=VdbeFrameMem(pFrame); pMem!=pEnd; pMem++){ |
drh | a5750cf | 2014-02-07 13:20:31 +0000 | [diff] [blame] | 6101 | pMem->flags = MEM_Undefined; |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 6102 | pMem->db = db; |
| 6103 | } |
| 6104 | }else{ |
| 6105 | pFrame = pRt->u.pFrame; |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 6106 | assert( pProgram->nMem+pProgram->nCsr==pFrame->nChildMem |
| 6107 | || (pProgram->nCsr==0 && pProgram->nMem+1==pFrame->nChildMem) ); |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 6108 | assert( pProgram->nCsr==pFrame->nChildCsr ); |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 6109 | assert( (int)(pOp - aOp)==pFrame->pc ); |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 6110 | } |
| 6111 | |
| 6112 | p->nFrame++; |
| 6113 | pFrame->pParent = p->pFrame; |
drh | fae58d5 | 2017-01-26 17:26:44 +0000 | [diff] [blame] | 6114 | pFrame->lastRowid = db->lastRowid; |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 6115 | pFrame->nChange = p->nChange; |
dan | c3da667 | 2014-10-28 18:24:16 +0000 | [diff] [blame] | 6116 | pFrame->nDbChange = p->db->nChange; |
dan | 3200132 | 2016-02-19 18:54:29 +0000 | [diff] [blame] | 6117 | assert( pFrame->pAuxData==0 ); |
| 6118 | pFrame->pAuxData = p->pAuxData; |
| 6119 | p->pAuxData = 0; |
dan | 2832ad4 | 2009-08-31 15:27:27 +0000 | [diff] [blame] | 6120 | p->nChange = 0; |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 6121 | p->pFrame = pFrame; |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 6122 | p->aMem = aMem = VdbeFrameMem(pFrame); |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 6123 | p->nMem = pFrame->nChildMem; |
shane | cea72b2 | 2009-09-07 04:38:36 +0000 | [diff] [blame] | 6124 | p->nCursor = (u16)pFrame->nChildCsr; |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 6125 | p->apCsr = (VdbeCursor **)&aMem[p->nMem]; |
drh | ab087d4 | 2017-03-24 17:59:56 +0000 | [diff] [blame] | 6126 | pFrame->aOnce = (u8*)&p->apCsr[pProgram->nCsr]; |
drh | 18333ef | 2017-03-24 18:38:41 +0000 | [diff] [blame] | 6127 | memset(pFrame->aOnce, 0, (pProgram->nOp + 7)/8); |
drh | bbe879d | 2009-11-14 18:04:35 +0000 | [diff] [blame] | 6128 | p->aOp = aOp = pProgram->aOp; |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 6129 | p->nOp = pProgram->nOp; |
dan | e2f771b | 2014-11-03 15:33:17 +0000 | [diff] [blame] | 6130 | #ifdef SQLITE_ENABLE_STMT_SCANSTATUS |
dan | 43764a8 | 2014-11-01 21:00:04 +0000 | [diff] [blame] | 6131 | p->anExec = 0; |
dan | e2f771b | 2014-11-03 15:33:17 +0000 | [diff] [blame] | 6132 | #endif |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 6133 | pOp = &aOp[-1]; |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 6134 | |
| 6135 | break; |
| 6136 | } |
| 6137 | |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 6138 | /* Opcode: Param P1 P2 * * * |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 6139 | ** |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 6140 | ** This opcode is only ever present in sub-programs called via the |
| 6141 | ** OP_Program instruction. Copy a value currently stored in a memory |
| 6142 | ** cell of the calling (parent) frame to cell P2 in the current frames |
| 6143 | ** address space. This is used by trigger programs to access the new.* |
| 6144 | ** and old.* values. |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 6145 | ** |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 6146 | ** The address of the cell in the parent frame is determined by adding |
| 6147 | ** the value of the P1 argument to the value of the P1 argument to the |
| 6148 | ** calling OP_Program instruction. |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 6149 | */ |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 6150 | case OP_Param: { /* out2 */ |
dan | 65a7cd1 | 2009-09-01 12:16:01 +0000 | [diff] [blame] | 6151 | VdbeFrame *pFrame; |
| 6152 | Mem *pIn; |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 6153 | pOut = out2Prerelease(p, pOp); |
dan | 65a7cd1 | 2009-09-01 12:16:01 +0000 | [diff] [blame] | 6154 | pFrame = p->pFrame; |
| 6155 | pIn = &pFrame->aMem[pOp->p1 + pFrame->aOp[pFrame->pc].p1]; |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 6156 | sqlite3VdbeMemShallowCopy(pOut, pIn, MEM_Ephem); |
| 6157 | break; |
| 6158 | } |
| 6159 | |
danielk1977 | 93758c8 | 2005-01-21 08:13:14 +0000 | [diff] [blame] | 6160 | #endif /* #ifndef SQLITE_OMIT_TRIGGER */ |
rdc | b0c374f | 2004-02-20 22:53:38 +0000 | [diff] [blame] | 6161 | |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 6162 | #ifndef SQLITE_OMIT_FOREIGN_KEY |
dan | 32b09f2 | 2009-09-23 17:29:59 +0000 | [diff] [blame] | 6163 | /* Opcode: FkCounter P1 P2 * * * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 6164 | ** Synopsis: fkctr[P1]+=P2 |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 6165 | ** |
dan | 0ff297e | 2009-09-25 17:03:14 +0000 | [diff] [blame] | 6166 | ** Increment a "constraint counter" by P2 (P2 may be negative or positive). |
| 6167 | ** If P1 is non-zero, the database constraint counter is incremented |
| 6168 | ** (deferred foreign key constraints). Otherwise, if P1 is zero, the |
dan | 32b09f2 | 2009-09-23 17:29:59 +0000 | [diff] [blame] | 6169 | ** statement counter is incremented (immediate foreign key constraints). |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 6170 | */ |
dan | 32b09f2 | 2009-09-23 17:29:59 +0000 | [diff] [blame] | 6171 | case OP_FkCounter: { |
drh | 963c74d | 2013-07-11 12:19:12 +0000 | [diff] [blame] | 6172 | if( db->flags & SQLITE_DeferFKs ){ |
dan | cb3e4b7 | 2013-07-03 19:53:05 +0000 | [diff] [blame] | 6173 | db->nDeferredImmCons += pOp->p2; |
| 6174 | }else if( pOp->p1 ){ |
dan | 0ff297e | 2009-09-25 17:03:14 +0000 | [diff] [blame] | 6175 | db->nDeferredCons += pOp->p2; |
dan | 32b09f2 | 2009-09-23 17:29:59 +0000 | [diff] [blame] | 6176 | }else{ |
dan | 0ff297e | 2009-09-25 17:03:14 +0000 | [diff] [blame] | 6177 | p->nFkConstraint += pOp->p2; |
| 6178 | } |
| 6179 | break; |
| 6180 | } |
| 6181 | |
| 6182 | /* Opcode: FkIfZero P1 P2 * * * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 6183 | ** Synopsis: if fkctr[P1]==0 goto P2 |
dan | 0ff297e | 2009-09-25 17:03:14 +0000 | [diff] [blame] | 6184 | ** |
| 6185 | ** This opcode tests if a foreign key constraint-counter is currently zero. |
| 6186 | ** If so, jump to instruction P2. Otherwise, fall through to the next |
| 6187 | ** instruction. |
| 6188 | ** |
| 6189 | ** If P1 is non-zero, then the jump is taken if the database constraint-counter |
| 6190 | ** is zero (the one that counts deferred constraint violations). If P1 is |
| 6191 | ** zero, the jump is taken if the statement constraint-counter is zero |
| 6192 | ** (immediate foreign key constraint violations). |
| 6193 | */ |
| 6194 | case OP_FkIfZero: { /* jump */ |
| 6195 | if( pOp->p1 ){ |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 6196 | VdbeBranchTaken(db->nDeferredCons==0 && db->nDeferredImmCons==0, 2); |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 6197 | if( db->nDeferredCons==0 && db->nDeferredImmCons==0 ) goto jump_to_p2; |
dan | 0ff297e | 2009-09-25 17:03:14 +0000 | [diff] [blame] | 6198 | }else{ |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 6199 | VdbeBranchTaken(p->nFkConstraint==0 && db->nDeferredImmCons==0, 2); |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 6200 | if( p->nFkConstraint==0 && db->nDeferredImmCons==0 ) goto jump_to_p2; |
dan | 32b09f2 | 2009-09-23 17:29:59 +0000 | [diff] [blame] | 6201 | } |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 6202 | break; |
| 6203 | } |
| 6204 | #endif /* #ifndef SQLITE_OMIT_FOREIGN_KEY */ |
| 6205 | |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 6206 | #ifndef SQLITE_OMIT_AUTOINCREMENT |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 6207 | /* Opcode: MemMax P1 P2 * * * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 6208 | ** Synopsis: r[P1]=max(r[P1],r[P2]) |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 6209 | ** |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 6210 | ** P1 is a register in the root frame of this VM (the root frame is |
| 6211 | ** different from the current frame if this instruction is being executed |
| 6212 | ** within a sub-program). Set the value of register P1 to the maximum of |
| 6213 | ** its current value and the value in register P2. |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 6214 | ** |
| 6215 | ** This instruction throws an error if the memory cell is not initially |
| 6216 | ** an integer. |
| 6217 | */ |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 6218 | case OP_MemMax: { /* in2 */ |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 6219 | VdbeFrame *pFrame; |
| 6220 | if( p->pFrame ){ |
| 6221 | for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent); |
| 6222 | pIn1 = &pFrame->aMem[pOp->p1]; |
| 6223 | }else{ |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 6224 | pIn1 = &aMem[pOp->p1]; |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 6225 | } |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 6226 | assert( memIsValid(pIn1) ); |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 6227 | sqlite3VdbeMemIntegerify(pIn1); |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 6228 | pIn2 = &aMem[pOp->p2]; |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 6229 | sqlite3VdbeMemIntegerify(pIn2); |
| 6230 | if( pIn1->u.i<pIn2->u.i){ |
| 6231 | pIn1->u.i = pIn2->u.i; |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 6232 | } |
| 6233 | break; |
| 6234 | } |
| 6235 | #endif /* SQLITE_OMIT_AUTOINCREMENT */ |
| 6236 | |
drh | 8b0cf38 | 2015-10-06 21:07:06 +0000 | [diff] [blame] | 6237 | /* Opcode: IfPos P1 P2 P3 * * |
| 6238 | ** Synopsis: if r[P1]>0 then r[P1]-=P3, goto P2 |
danielk1977 | a2dc3b1 | 2005-02-05 12:48:48 +0000 | [diff] [blame] | 6239 | ** |
drh | 1689707 | 2015-03-07 00:57:37 +0000 | [diff] [blame] | 6240 | ** Register P1 must contain an integer. |
mistachkin | 91a3ecb | 2015-10-06 21:49:55 +0000 | [diff] [blame] | 6241 | ** If the value of register P1 is 1 or greater, subtract P3 from the |
drh | 8b0cf38 | 2015-10-06 21:07:06 +0000 | [diff] [blame] | 6242 | ** value in P1 and jump to P2. |
drh | 6f58f70 | 2006-01-08 05:26:41 +0000 | [diff] [blame] | 6243 | ** |
drh | 1689707 | 2015-03-07 00:57:37 +0000 | [diff] [blame] | 6244 | ** If the initial value of register P1 is less than 1, then the |
| 6245 | ** value is unchanged and control passes through to the next instruction. |
danielk1977 | a2dc3b1 | 2005-02-05 12:48:48 +0000 | [diff] [blame] | 6246 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 6247 | case OP_IfPos: { /* jump, in1 */ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 6248 | pIn1 = &aMem[pOp->p1]; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 6249 | assert( pIn1->flags&MEM_Int ); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 6250 | VdbeBranchTaken( pIn1->u.i>0, 2); |
drh | 8b0cf38 | 2015-10-06 21:07:06 +0000 | [diff] [blame] | 6251 | if( pIn1->u.i>0 ){ |
| 6252 | pIn1->u.i -= pOp->p3; |
| 6253 | goto jump_to_p2; |
| 6254 | } |
drh | ec7429a | 2005-10-06 16:53:14 +0000 | [diff] [blame] | 6255 | break; |
| 6256 | } |
| 6257 | |
drh | cc2fa4c | 2016-01-25 15:57:29 +0000 | [diff] [blame] | 6258 | /* Opcode: OffsetLimit P1 P2 P3 * * |
| 6259 | ** Synopsis: if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1) |
drh | 15007a9 | 2006-01-08 18:10:17 +0000 | [diff] [blame] | 6260 | ** |
drh | cc2fa4c | 2016-01-25 15:57:29 +0000 | [diff] [blame] | 6261 | ** This opcode performs a commonly used computation associated with |
| 6262 | ** LIMIT and OFFSET process. r[P1] holds the limit counter. r[P3] |
| 6263 | ** holds the offset counter. The opcode computes the combined value |
| 6264 | ** of the LIMIT and OFFSET and stores that value in r[P2]. The r[P2] |
| 6265 | ** value computed is the total number of rows that will need to be |
| 6266 | ** visited in order to complete the query. |
| 6267 | ** |
| 6268 | ** If r[P3] is zero or negative, that means there is no OFFSET |
| 6269 | ** and r[P2] is set to be the value of the LIMIT, r[P1]. |
| 6270 | ** |
| 6271 | ** if r[P1] is zero or negative, that means there is no LIMIT |
| 6272 | ** and r[P2] is set to -1. |
| 6273 | ** |
| 6274 | ** Otherwise, r[P2] is set to the sum of r[P1] and r[P3]. |
drh | 15007a9 | 2006-01-08 18:10:17 +0000 | [diff] [blame] | 6275 | */ |
drh | cc2fa4c | 2016-01-25 15:57:29 +0000 | [diff] [blame] | 6276 | case OP_OffsetLimit: { /* in1, out2, in3 */ |
drh | 719da30 | 2016-12-10 04:06:49 +0000 | [diff] [blame] | 6277 | i64 x; |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 6278 | pIn1 = &aMem[pOp->p1]; |
drh | cc2fa4c | 2016-01-25 15:57:29 +0000 | [diff] [blame] | 6279 | pIn3 = &aMem[pOp->p3]; |
| 6280 | pOut = out2Prerelease(p, pOp); |
| 6281 | assert( pIn1->flags & MEM_Int ); |
| 6282 | assert( pIn3->flags & MEM_Int ); |
drh | 719da30 | 2016-12-10 04:06:49 +0000 | [diff] [blame] | 6283 | x = pIn1->u.i; |
| 6284 | if( x<=0 || sqlite3AddInt64(&x, pIn3->u.i>0?pIn3->u.i:0) ){ |
| 6285 | /* If the LIMIT is less than or equal to zero, loop forever. This |
| 6286 | ** is documented. But also, if the LIMIT+OFFSET exceeds 2^63 then |
| 6287 | ** also loop forever. This is undocumented. In fact, one could argue |
| 6288 | ** that the loop should terminate. But assuming 1 billion iterations |
| 6289 | ** per second (far exceeding the capabilities of any current hardware) |
| 6290 | ** it would take nearly 300 years to actually reach the limit. So |
| 6291 | ** looping forever is a reasonable approximation. */ |
| 6292 | pOut->u.i = -1; |
| 6293 | }else{ |
| 6294 | pOut->u.i = x; |
| 6295 | } |
drh | 15007a9 | 2006-01-08 18:10:17 +0000 | [diff] [blame] | 6296 | break; |
| 6297 | } |
| 6298 | |
drh | f99dd35 | 2016-12-18 17:42:00 +0000 | [diff] [blame] | 6299 | /* Opcode: IfNotZero P1 P2 * * * |
| 6300 | ** Synopsis: if r[P1]!=0 then r[P1]--, goto P2 |
drh | ec7429a | 2005-10-06 16:53:14 +0000 | [diff] [blame] | 6301 | ** |
drh | 1689707 | 2015-03-07 00:57:37 +0000 | [diff] [blame] | 6302 | ** Register P1 must contain an integer. If the content of register P1 is |
drh | f99dd35 | 2016-12-18 17:42:00 +0000 | [diff] [blame] | 6303 | ** initially greater than zero, then decrement the value in register P1. |
| 6304 | ** If it is non-zero (negative or positive) and then also jump to P2. |
| 6305 | ** If register P1 is initially zero, leave it unchanged and fall through. |
drh | ec7429a | 2005-10-06 16:53:14 +0000 | [diff] [blame] | 6306 | */ |
drh | 1689707 | 2015-03-07 00:57:37 +0000 | [diff] [blame] | 6307 | case OP_IfNotZero: { /* jump, in1 */ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 6308 | pIn1 = &aMem[pOp->p1]; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 6309 | assert( pIn1->flags&MEM_Int ); |
drh | 1689707 | 2015-03-07 00:57:37 +0000 | [diff] [blame] | 6310 | VdbeBranchTaken(pIn1->u.i<0, 2); |
| 6311 | if( pIn1->u.i ){ |
drh | f99dd35 | 2016-12-18 17:42:00 +0000 | [diff] [blame] | 6312 | if( pIn1->u.i>0 ) pIn1->u.i--; |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 6313 | goto jump_to_p2; |
drh | 1689707 | 2015-03-07 00:57:37 +0000 | [diff] [blame] | 6314 | } |
| 6315 | break; |
| 6316 | } |
| 6317 | |
| 6318 | /* Opcode: DecrJumpZero P1 P2 * * * |
| 6319 | ** Synopsis: if (--r[P1])==0 goto P2 |
| 6320 | ** |
drh | ab5be2e | 2016-11-30 05:08:59 +0000 | [diff] [blame] | 6321 | ** Register P1 must hold an integer. Decrement the value in P1 |
| 6322 | ** and jump to P2 if the new value is exactly zero. |
drh | 1689707 | 2015-03-07 00:57:37 +0000 | [diff] [blame] | 6323 | */ |
| 6324 | case OP_DecrJumpZero: { /* jump, in1 */ |
| 6325 | pIn1 = &aMem[pOp->p1]; |
| 6326 | assert( pIn1->flags&MEM_Int ); |
drh | ab5be2e | 2016-11-30 05:08:59 +0000 | [diff] [blame] | 6327 | if( pIn1->u.i>SMALLEST_INT64 ) pIn1->u.i--; |
| 6328 | VdbeBranchTaken(pIn1->u.i==0, 2); |
| 6329 | if( pIn1->u.i==0 ) goto jump_to_p2; |
drh | a2a49dc | 2008-01-02 14:28:13 +0000 | [diff] [blame] | 6330 | break; |
| 6331 | } |
| 6332 | |
drh | 1689707 | 2015-03-07 00:57:37 +0000 | [diff] [blame] | 6333 | |
drh | 8f26da6 | 2018-07-05 21:22:57 +0000 | [diff] [blame] | 6334 | /* Opcode: AggStep * P2 P3 P4 P5 |
drh | f63552b | 2013-10-30 00:25:03 +0000 | [diff] [blame] | 6335 | ** Synopsis: accum=r[P3] step(r[P2@P5]) |
drh | e509535 | 2002-02-24 03:25:14 +0000 | [diff] [blame] | 6336 | ** |
drh | 8f26da6 | 2018-07-05 21:22:57 +0000 | [diff] [blame] | 6337 | ** Execute the xStep function for an aggregate. |
| 6338 | ** The function has P5 arguments. P4 is a pointer to the |
dan | 9a94722 | 2018-06-14 19:06:36 +0000 | [diff] [blame] | 6339 | ** FuncDef structure that specifies the function. Register P3 is the |
drh | e2d9e7c | 2015-06-26 18:47:53 +0000 | [diff] [blame] | 6340 | ** accumulator. |
drh | e509535 | 2002-02-24 03:25:14 +0000 | [diff] [blame] | 6341 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 6342 | ** The P5 arguments are taken from register P2 and its |
| 6343 | ** successors. |
drh | e509535 | 2002-02-24 03:25:14 +0000 | [diff] [blame] | 6344 | */ |
drh | 8f26da6 | 2018-07-05 21:22:57 +0000 | [diff] [blame] | 6345 | /* Opcode: AggInverse * P2 P3 P4 P5 |
| 6346 | ** Synopsis: accum=r[P3] inverse(r[P2@P5]) |
| 6347 | ** |
| 6348 | ** Execute the xInverse function for an aggregate. |
| 6349 | ** The function has P5 arguments. P4 is a pointer to the |
| 6350 | ** FuncDef structure that specifies the function. Register P3 is the |
| 6351 | ** accumulator. |
| 6352 | ** |
| 6353 | ** The P5 arguments are taken from register P2 and its |
| 6354 | ** successors. |
| 6355 | */ |
| 6356 | /* Opcode: AggStep1 P1 P2 P3 P4 P5 |
drh | e2d9e7c | 2015-06-26 18:47:53 +0000 | [diff] [blame] | 6357 | ** Synopsis: accum=r[P3] step(r[P2@P5]) |
| 6358 | ** |
dan | 9a94722 | 2018-06-14 19:06:36 +0000 | [diff] [blame] | 6359 | ** Execute the xStep (if P1==0) or xInverse (if P1!=0) function for an |
| 6360 | ** aggregate. The function has P5 arguments. P4 is a pointer to the |
| 6361 | ** FuncDef structure that specifies the function. Register P3 is the |
| 6362 | ** accumulator. |
drh | e2d9e7c | 2015-06-26 18:47:53 +0000 | [diff] [blame] | 6363 | ** |
| 6364 | ** The P5 arguments are taken from register P2 and its |
| 6365 | ** successors. |
| 6366 | ** |
| 6367 | ** This opcode is initially coded as OP_AggStep0. On first evaluation, |
| 6368 | ** the FuncDef stored in P4 is converted into an sqlite3_context and |
| 6369 | ** the opcode is changed. In this way, the initialization of the |
| 6370 | ** sqlite3_context only happens once, instead of on each call to the |
| 6371 | ** step function. |
| 6372 | */ |
drh | 8f26da6 | 2018-07-05 21:22:57 +0000 | [diff] [blame] | 6373 | case OP_AggInverse: |
| 6374 | case OP_AggStep: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 6375 | int n; |
drh | 9c7c913 | 2015-06-26 18:16:52 +0000 | [diff] [blame] | 6376 | sqlite3_context *pCtx; |
drh | e509535 | 2002-02-24 03:25:14 +0000 | [diff] [blame] | 6377 | |
drh | 9c7c913 | 2015-06-26 18:16:52 +0000 | [diff] [blame] | 6378 | assert( pOp->p4type==P4_FUNCDEF ); |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 6379 | n = pOp->p5; |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 6380 | assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) ); |
| 6381 | assert( n==0 || (pOp->p2>0 && pOp->p2+n<=(p->nMem+1 - p->nCursor)+1) ); |
drh | 9c7c913 | 2015-06-26 18:16:52 +0000 | [diff] [blame] | 6382 | assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n ); |
drh | f09ac0b | 2018-01-23 03:44:06 +0000 | [diff] [blame] | 6383 | pCtx = sqlite3DbMallocRawNN(db, n*sizeof(sqlite3_value*) + |
| 6384 | (sizeof(pCtx[0]) + sizeof(Mem) - sizeof(sqlite3_value*))); |
drh | 9c7c913 | 2015-06-26 18:16:52 +0000 | [diff] [blame] | 6385 | if( pCtx==0 ) goto no_mem; |
| 6386 | pCtx->pMem = 0; |
drh | f09ac0b | 2018-01-23 03:44:06 +0000 | [diff] [blame] | 6387 | pCtx->pOut = (Mem*)&(pCtx->argv[n]); |
| 6388 | sqlite3VdbeMemInit(pCtx->pOut, db, MEM_Null); |
drh | 9c7c913 | 2015-06-26 18:16:52 +0000 | [diff] [blame] | 6389 | pCtx->pFunc = pOp->p4.pFunc; |
| 6390 | pCtx->iOp = (int)(pOp - aOp); |
| 6391 | pCtx->pVdbe = p; |
drh | f09ac0b | 2018-01-23 03:44:06 +0000 | [diff] [blame] | 6392 | pCtx->skipFlag = 0; |
| 6393 | pCtx->isError = 0; |
drh | 9c7c913 | 2015-06-26 18:16:52 +0000 | [diff] [blame] | 6394 | pCtx->argc = n; |
| 6395 | pOp->p4type = P4_FUNCCTX; |
| 6396 | pOp->p4.pCtx = pCtx; |
drh | 2c885d0 | 2018-07-07 19:36:04 +0000 | [diff] [blame] | 6397 | |
| 6398 | /* OP_AggInverse must have P1==1 and OP_AggStep must have P1==0 */ |
drh | 8f26da6 | 2018-07-05 21:22:57 +0000 | [diff] [blame] | 6399 | assert( pOp->p1==(pOp->opcode==OP_AggInverse) ); |
drh | 2c885d0 | 2018-07-07 19:36:04 +0000 | [diff] [blame] | 6400 | |
drh | 8f26da6 | 2018-07-05 21:22:57 +0000 | [diff] [blame] | 6401 | pOp->opcode = OP_AggStep1; |
drh | 9c7c913 | 2015-06-26 18:16:52 +0000 | [diff] [blame] | 6402 | /* Fall through into OP_AggStep */ |
| 6403 | } |
drh | 8f26da6 | 2018-07-05 21:22:57 +0000 | [diff] [blame] | 6404 | case OP_AggStep1: { |
drh | 9c7c913 | 2015-06-26 18:16:52 +0000 | [diff] [blame] | 6405 | int i; |
| 6406 | sqlite3_context *pCtx; |
| 6407 | Mem *pMem; |
drh | 9c7c913 | 2015-06-26 18:16:52 +0000 | [diff] [blame] | 6408 | |
| 6409 | assert( pOp->p4type==P4_FUNCCTX ); |
| 6410 | pCtx = pOp->p4.pCtx; |
| 6411 | pMem = &aMem[pOp->p3]; |
| 6412 | |
drh | 2c885d0 | 2018-07-07 19:36:04 +0000 | [diff] [blame] | 6413 | #ifdef SQLITE_DEBUG |
| 6414 | if( pOp->p1 ){ |
| 6415 | /* This is an OP_AggInverse call. Verify that xStep has always |
| 6416 | ** been called at least once prior to any xInverse call. */ |
| 6417 | assert( pMem->uTemp==0x1122e0e3 ); |
| 6418 | }else{ |
| 6419 | /* This is an OP_AggStep call. Mark it as such. */ |
| 6420 | pMem->uTemp = 0x1122e0e3; |
| 6421 | } |
| 6422 | #endif |
| 6423 | |
drh | 9c7c913 | 2015-06-26 18:16:52 +0000 | [diff] [blame] | 6424 | /* If this function is inside of a trigger, the register array in aMem[] |
| 6425 | ** might change from one evaluation to the next. The next block of code |
| 6426 | ** checks to see if the register array has changed, and if so it |
| 6427 | ** reinitializes the relavant parts of the sqlite3_context object */ |
| 6428 | if( pCtx->pMem != pMem ){ |
| 6429 | pCtx->pMem = pMem; |
| 6430 | for(i=pCtx->argc-1; i>=0; i--) pCtx->argv[i] = &aMem[pOp->p2+i]; |
| 6431 | } |
| 6432 | |
| 6433 | #ifdef SQLITE_DEBUG |
| 6434 | for(i=0; i<pCtx->argc; i++){ |
| 6435 | assert( memIsValid(pCtx->argv[i]) ); |
| 6436 | REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]); |
| 6437 | } |
| 6438 | #endif |
| 6439 | |
drh | abfcea2 | 2005-09-06 20:36:48 +0000 | [diff] [blame] | 6440 | pMem->n++; |
drh | f09ac0b | 2018-01-23 03:44:06 +0000 | [diff] [blame] | 6441 | assert( pCtx->pOut->flags==MEM_Null ); |
| 6442 | assert( pCtx->isError==0 ); |
| 6443 | assert( pCtx->skipFlag==0 ); |
dan | 67a9b8e | 2018-06-22 20:51:35 +0000 | [diff] [blame] | 6444 | #ifndef SQLITE_OMIT_WINDOWFUNC |
| 6445 | if( pOp->p1 ){ |
| 6446 | (pCtx->pFunc->xInverse)(pCtx,pCtx->argc,pCtx->argv); |
| 6447 | }else |
| 6448 | #endif |
| 6449 | (pCtx->pFunc->xSFunc)(pCtx,pCtx->argc,pCtx->argv); /* IMP: R-24505-23230 */ |
| 6450 | |
drh | f09ac0b | 2018-01-23 03:44:06 +0000 | [diff] [blame] | 6451 | if( pCtx->isError ){ |
| 6452 | if( pCtx->isError>0 ){ |
| 6453 | sqlite3VdbeError(p, "%s", sqlite3_value_text(pCtx->pOut)); |
drh | 9c7c913 | 2015-06-26 18:16:52 +0000 | [diff] [blame] | 6454 | rc = pCtx->isError; |
| 6455 | } |
drh | f09ac0b | 2018-01-23 03:44:06 +0000 | [diff] [blame] | 6456 | if( pCtx->skipFlag ){ |
| 6457 | assert( pOp[-1].opcode==OP_CollSeq ); |
| 6458 | i = pOp[-1].p1; |
| 6459 | if( i ) sqlite3VdbeMemSetInt64(&aMem[i], 1); |
| 6460 | pCtx->skipFlag = 0; |
| 6461 | } |
| 6462 | sqlite3VdbeMemRelease(pCtx->pOut); |
| 6463 | pCtx->pOut->flags = MEM_Null; |
| 6464 | pCtx->isError = 0; |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 6465 | if( rc ) goto abort_due_to_error; |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 6466 | } |
drh | f09ac0b | 2018-01-23 03:44:06 +0000 | [diff] [blame] | 6467 | assert( pCtx->pOut->flags==MEM_Null ); |
| 6468 | assert( pCtx->skipFlag==0 ); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 6469 | break; |
| 6470 | } |
| 6471 | |
drh | 8f26da6 | 2018-07-05 21:22:57 +0000 | [diff] [blame] | 6472 | /* Opcode: AggFinal P1 P2 * P4 * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 6473 | ** Synopsis: accum=r[P1] N=P2 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 6474 | ** |
dan | 9a94722 | 2018-06-14 19:06:36 +0000 | [diff] [blame] | 6475 | ** P1 is the memory location that is the accumulator for an aggregate |
drh | 8f26da6 | 2018-07-05 21:22:57 +0000 | [diff] [blame] | 6476 | ** or window function. Execute the finalizer function |
| 6477 | ** for an aggregate and store the result in P1. |
drh | a10a34b | 2005-09-07 22:09:48 +0000 | [diff] [blame] | 6478 | ** |
| 6479 | ** P2 is the number of arguments that the step function takes and |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 6480 | ** P4 is a pointer to the FuncDef for this function. The P2 |
drh | a10a34b | 2005-09-07 22:09:48 +0000 | [diff] [blame] | 6481 | ** argument is not used by this opcode. It is only there to disambiguate |
| 6482 | ** functions that can take varying numbers of arguments. The |
drh | 8be47a7 | 2018-07-05 20:05:29 +0000 | [diff] [blame] | 6483 | ** P4 argument is only needed for the case where |
drh | a10a34b | 2005-09-07 22:09:48 +0000 | [diff] [blame] | 6484 | ** the step function was not previously called. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 6485 | */ |
drh | 8f26da6 | 2018-07-05 21:22:57 +0000 | [diff] [blame] | 6486 | /* Opcode: AggValue * P2 P3 P4 * |
| 6487 | ** Synopsis: r[P3]=value N=P2 |
| 6488 | ** |
| 6489 | ** Invoke the xValue() function and store the result in register P3. |
| 6490 | ** |
| 6491 | ** P2 is the number of arguments that the step function takes and |
| 6492 | ** P4 is a pointer to the FuncDef for this function. The P2 |
| 6493 | ** argument is not used by this opcode. It is only there to disambiguate |
| 6494 | ** functions that can take varying numbers of arguments. The |
| 6495 | ** P4 argument is only needed for the case where |
| 6496 | ** the step function was not previously called. |
| 6497 | */ |
| 6498 | case OP_AggValue: |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 6499 | case OP_AggFinal: { |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 6500 | Mem *pMem; |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 6501 | assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) ); |
drh | 8f26da6 | 2018-07-05 21:22:57 +0000 | [diff] [blame] | 6502 | assert( pOp->p3==0 || pOp->opcode==OP_AggValue ); |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 6503 | pMem = &aMem[pOp->p1]; |
drh | a10a34b | 2005-09-07 22:09:48 +0000 | [diff] [blame] | 6504 | assert( (pMem->flags & ~(MEM_Null|MEM_Agg))==0 ); |
dan | 67a9b8e | 2018-06-22 20:51:35 +0000 | [diff] [blame] | 6505 | #ifndef SQLITE_OMIT_WINDOWFUNC |
dan | 86fb6e1 | 2018-05-16 20:58:07 +0000 | [diff] [blame] | 6506 | if( pOp->p3 ){ |
| 6507 | rc = sqlite3VdbeMemAggValue(pMem, &aMem[pOp->p3], pOp->p4.pFunc); |
dan | 660af93 | 2018-06-18 16:55:22 +0000 | [diff] [blame] | 6508 | pMem = &aMem[pOp->p3]; |
dan | 67a9b8e | 2018-06-22 20:51:35 +0000 | [diff] [blame] | 6509 | }else |
| 6510 | #endif |
drh | 8f26da6 | 2018-07-05 21:22:57 +0000 | [diff] [blame] | 6511 | { |
| 6512 | rc = sqlite3VdbeMemFinalize(pMem, pOp->p4.pFunc); |
| 6513 | } |
dan | 67a9b8e | 2018-06-22 20:51:35 +0000 | [diff] [blame] | 6514 | |
drh | 4c8555f | 2009-06-25 01:47:11 +0000 | [diff] [blame] | 6515 | if( rc ){ |
drh | 22c17b8 | 2015-05-15 04:13:15 +0000 | [diff] [blame] | 6516 | sqlite3VdbeError(p, "%s", sqlite3_value_text(pMem)); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 6517 | goto abort_due_to_error; |
drh | 90669c1 | 2006-01-20 15:45:36 +0000 | [diff] [blame] | 6518 | } |
drh | 2dca868 | 2008-03-21 17:13:13 +0000 | [diff] [blame] | 6519 | sqlite3VdbeChangeEncoding(pMem, encoding); |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 6520 | UPDATE_MAX_BLOBSIZE(pMem); |
drh | 023ae03 | 2007-05-08 12:12:16 +0000 | [diff] [blame] | 6521 | if( sqlite3VdbeMemTooBig(pMem) ){ |
| 6522 | goto too_big; |
| 6523 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 6524 | break; |
| 6525 | } |
| 6526 | |
dan | 5cf5353 | 2010-05-01 16:40:20 +0000 | [diff] [blame] | 6527 | #ifndef SQLITE_OMIT_WAL |
dan | cdc1f04 | 2010-11-18 12:11:05 +0000 | [diff] [blame] | 6528 | /* Opcode: Checkpoint P1 P2 P3 * * |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 6529 | ** |
| 6530 | ** Checkpoint database P1. This is a no-op if P1 is not currently in |
drh | a25165f | 2014-12-04 04:50:59 +0000 | [diff] [blame] | 6531 | ** WAL mode. Parameter P2 is one of SQLITE_CHECKPOINT_PASSIVE, FULL, |
| 6532 | ** RESTART, or TRUNCATE. Write 1 or 0 into mem[P3] if the checkpoint returns |
drh | 30aa3b9 | 2011-02-07 23:56:01 +0000 | [diff] [blame] | 6533 | ** SQLITE_BUSY or not, respectively. Write the number of pages in the |
| 6534 | ** WAL after the checkpoint into mem[P3+1] and the number of pages |
| 6535 | ** in the WAL that have been checkpointed after the checkpoint |
| 6536 | ** completes into mem[P3+2]. However on an error, mem[P3+1] and |
| 6537 | ** mem[P3+2] are initialized to -1. |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 6538 | */ |
| 6539 | case OP_Checkpoint: { |
drh | 30aa3b9 | 2011-02-07 23:56:01 +0000 | [diff] [blame] | 6540 | int i; /* Loop counter */ |
| 6541 | int aRes[3]; /* Results */ |
| 6542 | Mem *pMem; /* Write results here */ |
| 6543 | |
drh | 9e92a47 | 2013-06-27 17:40:30 +0000 | [diff] [blame] | 6544 | assert( p->readOnly==0 ); |
drh | 30aa3b9 | 2011-02-07 23:56:01 +0000 | [diff] [blame] | 6545 | aRes[0] = 0; |
| 6546 | aRes[1] = aRes[2] = -1; |
dan | cdc1f04 | 2010-11-18 12:11:05 +0000 | [diff] [blame] | 6547 | assert( pOp->p2==SQLITE_CHECKPOINT_PASSIVE |
| 6548 | || pOp->p2==SQLITE_CHECKPOINT_FULL |
| 6549 | || pOp->p2==SQLITE_CHECKPOINT_RESTART |
dan | f26a154 | 2014-12-02 19:04:54 +0000 | [diff] [blame] | 6550 | || pOp->p2==SQLITE_CHECKPOINT_TRUNCATE |
dan | cdc1f04 | 2010-11-18 12:11:05 +0000 | [diff] [blame] | 6551 | ); |
drh | 30aa3b9 | 2011-02-07 23:56:01 +0000 | [diff] [blame] | 6552 | rc = sqlite3Checkpoint(db, pOp->p1, pOp->p2, &aRes[1], &aRes[2]); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 6553 | if( rc ){ |
| 6554 | if( rc!=SQLITE_BUSY ) goto abort_due_to_error; |
dan | cdc1f04 | 2010-11-18 12:11:05 +0000 | [diff] [blame] | 6555 | rc = SQLITE_OK; |
drh | 30aa3b9 | 2011-02-07 23:56:01 +0000 | [diff] [blame] | 6556 | aRes[0] = 1; |
dan | cdc1f04 | 2010-11-18 12:11:05 +0000 | [diff] [blame] | 6557 | } |
drh | 30aa3b9 | 2011-02-07 23:56:01 +0000 | [diff] [blame] | 6558 | for(i=0, pMem = &aMem[pOp->p3]; i<3; i++, pMem++){ |
| 6559 | sqlite3VdbeMemSetInt64(pMem, (i64)aRes[i]); |
| 6560 | } |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 6561 | break; |
| 6562 | }; |
dan | 5cf5353 | 2010-05-01 16:40:20 +0000 | [diff] [blame] | 6563 | #endif |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 6564 | |
drh | cac29a6 | 2010-07-02 19:36:52 +0000 | [diff] [blame] | 6565 | #ifndef SQLITE_OMIT_PRAGMA |
drh | 0fd6135 | 2014-02-07 02:29:45 +0000 | [diff] [blame] | 6566 | /* Opcode: JournalMode P1 P2 P3 * * |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 6567 | ** |
| 6568 | ** Change the journal mode of database P1 to P3. P3 must be one of the |
| 6569 | ** PAGER_JOURNALMODE_XXX values. If changing between the various rollback |
| 6570 | ** modes (delete, truncate, persist, off and memory), this is a simple |
| 6571 | ** operation. No IO is required. |
| 6572 | ** |
| 6573 | ** If changing into or out of WAL mode the procedure is more complicated. |
| 6574 | ** |
| 6575 | ** Write a string containing the final journal-mode to register P2. |
| 6576 | */ |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 6577 | case OP_JournalMode: { /* out2 */ |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 6578 | Btree *pBt; /* Btree to change journal mode of */ |
| 6579 | Pager *pPager; /* Pager associated with pBt */ |
drh | d80b233 | 2010-05-01 00:59:37 +0000 | [diff] [blame] | 6580 | int eNew; /* New journal mode */ |
| 6581 | int eOld; /* The old journal mode */ |
mistachkin | 59ee77c | 2012-09-13 15:26:44 +0000 | [diff] [blame] | 6582 | #ifndef SQLITE_OMIT_WAL |
drh | d80b233 | 2010-05-01 00:59:37 +0000 | [diff] [blame] | 6583 | const char *zFilename; /* Name of database file for pPager */ |
mistachkin | 59ee77c | 2012-09-13 15:26:44 +0000 | [diff] [blame] | 6584 | #endif |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 6585 | |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 6586 | pOut = out2Prerelease(p, pOp); |
drh | d80b233 | 2010-05-01 00:59:37 +0000 | [diff] [blame] | 6587 | eNew = pOp->p3; |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 6588 | assert( eNew==PAGER_JOURNALMODE_DELETE |
| 6589 | || eNew==PAGER_JOURNALMODE_TRUNCATE |
| 6590 | || eNew==PAGER_JOURNALMODE_PERSIST |
| 6591 | || eNew==PAGER_JOURNALMODE_OFF |
| 6592 | || eNew==PAGER_JOURNALMODE_MEMORY |
| 6593 | || eNew==PAGER_JOURNALMODE_WAL |
| 6594 | || eNew==PAGER_JOURNALMODE_QUERY |
| 6595 | ); |
| 6596 | assert( pOp->p1>=0 && pOp->p1<db->nDb ); |
drh | 9e92a47 | 2013-06-27 17:40:30 +0000 | [diff] [blame] | 6597 | assert( p->readOnly==0 ); |
drh | 3ebaee9 | 2010-05-06 21:37:22 +0000 | [diff] [blame] | 6598 | |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 6599 | pBt = db->aDb[pOp->p1].pBt; |
| 6600 | pPager = sqlite3BtreePager(pBt); |
drh | 0b9b430 | 2010-06-11 17:01:24 +0000 | [diff] [blame] | 6601 | eOld = sqlite3PagerGetJournalMode(pPager); |
| 6602 | if( eNew==PAGER_JOURNALMODE_QUERY ) eNew = eOld; |
| 6603 | if( !sqlite3PagerOkToChangeJournalMode(pPager) ) eNew = eOld; |
dan | 5cf5353 | 2010-05-01 16:40:20 +0000 | [diff] [blame] | 6604 | |
| 6605 | #ifndef SQLITE_OMIT_WAL |
drh | d4e0bb0 | 2012-05-27 01:19:04 +0000 | [diff] [blame] | 6606 | zFilename = sqlite3PagerFilename(pPager, 1); |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 6607 | |
drh | d80b233 | 2010-05-01 00:59:37 +0000 | [diff] [blame] | 6608 | /* Do not allow a transition to journal_mode=WAL for a database |
drh | 6e1f482 | 2010-07-13 23:41:40 +0000 | [diff] [blame] | 6609 | ** in temporary storage or if the VFS does not support shared memory |
drh | d80b233 | 2010-05-01 00:59:37 +0000 | [diff] [blame] | 6610 | */ |
| 6611 | if( eNew==PAGER_JOURNALMODE_WAL |
drh | 057fc81 | 2011-10-17 23:15:31 +0000 | [diff] [blame] | 6612 | && (sqlite3Strlen30(zFilename)==0 /* Temp file */ |
drh | 6e1f482 | 2010-07-13 23:41:40 +0000 | [diff] [blame] | 6613 | || !sqlite3PagerWalSupported(pPager)) /* No shared-memory support */ |
dan | e180c29 | 2010-04-26 17:42:56 +0000 | [diff] [blame] | 6614 | ){ |
drh | 0b9b430 | 2010-06-11 17:01:24 +0000 | [diff] [blame] | 6615 | eNew = eOld; |
dan | e180c29 | 2010-04-26 17:42:56 +0000 | [diff] [blame] | 6616 | } |
| 6617 | |
drh | 0b9b430 | 2010-06-11 17:01:24 +0000 | [diff] [blame] | 6618 | if( (eNew!=eOld) |
| 6619 | && (eOld==PAGER_JOURNALMODE_WAL || eNew==PAGER_JOURNALMODE_WAL) |
| 6620 | ){ |
dan | c0537fe | 2013-06-28 19:41:43 +0000 | [diff] [blame] | 6621 | if( !db->autoCommit || db->nVdbeRead>1 ){ |
drh | 0b9b430 | 2010-06-11 17:01:24 +0000 | [diff] [blame] | 6622 | rc = SQLITE_ERROR; |
drh | 22c17b8 | 2015-05-15 04:13:15 +0000 | [diff] [blame] | 6623 | sqlite3VdbeError(p, |
drh | 0b9b430 | 2010-06-11 17:01:24 +0000 | [diff] [blame] | 6624 | "cannot change %s wal mode from within a transaction", |
| 6625 | (eNew==PAGER_JOURNALMODE_WAL ? "into" : "out of") |
| 6626 | ); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 6627 | goto abort_due_to_error; |
drh | 0b9b430 | 2010-06-11 17:01:24 +0000 | [diff] [blame] | 6628 | }else{ |
| 6629 | |
| 6630 | if( eOld==PAGER_JOURNALMODE_WAL ){ |
| 6631 | /* If leaving WAL mode, close the log file. If successful, the call |
| 6632 | ** to PagerCloseWal() checkpoints and deletes the write-ahead-log |
| 6633 | ** file. An EXCLUSIVE lock may still be held on the database file |
| 6634 | ** after a successful return. |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 6635 | */ |
dan | 7fb8990 | 2016-08-12 16:21:15 +0000 | [diff] [blame] | 6636 | rc = sqlite3PagerCloseWal(pPager, db); |
drh | ab9b744 | 2010-05-10 11:20:05 +0000 | [diff] [blame] | 6637 | if( rc==SQLITE_OK ){ |
drh | 0b9b430 | 2010-06-11 17:01:24 +0000 | [diff] [blame] | 6638 | sqlite3PagerSetJournalMode(pPager, eNew); |
drh | 89c3f2f | 2010-05-15 01:09:38 +0000 | [diff] [blame] | 6639 | } |
drh | 242c4f7 | 2010-06-22 14:49:39 +0000 | [diff] [blame] | 6640 | }else if( eOld==PAGER_JOURNALMODE_MEMORY ){ |
| 6641 | /* Cannot transition directly from MEMORY to WAL. Use mode OFF |
| 6642 | ** as an intermediate */ |
| 6643 | sqlite3PagerSetJournalMode(pPager, PAGER_JOURNALMODE_OFF); |
drh | 0b9b430 | 2010-06-11 17:01:24 +0000 | [diff] [blame] | 6644 | } |
| 6645 | |
| 6646 | /* Open a transaction on the database file. Regardless of the journal |
| 6647 | ** mode, this transaction always uses a rollback journal. |
| 6648 | */ |
| 6649 | assert( sqlite3BtreeIsInTrans(pBt)==0 ); |
| 6650 | if( rc==SQLITE_OK ){ |
dan | 731bf5b | 2010-06-17 16:44:21 +0000 | [diff] [blame] | 6651 | rc = sqlite3BtreeSetVersion(pBt, (eNew==PAGER_JOURNALMODE_WAL ? 2 : 1)); |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 6652 | } |
| 6653 | } |
| 6654 | } |
dan | 5cf5353 | 2010-05-01 16:40:20 +0000 | [diff] [blame] | 6655 | #endif /* ifndef SQLITE_OMIT_WAL */ |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 6656 | |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 6657 | if( rc ) eNew = eOld; |
drh | 0b9b430 | 2010-06-11 17:01:24 +0000 | [diff] [blame] | 6658 | eNew = sqlite3PagerSetJournalMode(pPager, eNew); |
dan | 731bf5b | 2010-06-17 16:44:21 +0000 | [diff] [blame] | 6659 | |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 6660 | pOut->flags = MEM_Str|MEM_Static|MEM_Term; |
dan | b978002 | 2010-04-21 18:37:57 +0000 | [diff] [blame] | 6661 | pOut->z = (char *)sqlite3JournalModename(eNew); |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 6662 | pOut->n = sqlite3Strlen30(pOut->z); |
| 6663 | pOut->enc = SQLITE_UTF8; |
| 6664 | sqlite3VdbeChangeEncoding(pOut, encoding); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 6665 | if( rc ) goto abort_due_to_error; |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 6666 | break; |
drh | cac29a6 | 2010-07-02 19:36:52 +0000 | [diff] [blame] | 6667 | }; |
| 6668 | #endif /* SQLITE_OMIT_PRAGMA */ |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 6669 | |
drh | fdbcdee | 2007-03-27 14:44:50 +0000 | [diff] [blame] | 6670 | #if !defined(SQLITE_OMIT_VACUUM) && !defined(SQLITE_OMIT_ATTACH) |
drh | 9ef5e77 | 2016-08-19 14:20:56 +0000 | [diff] [blame] | 6671 | /* Opcode: Vacuum P1 * * * * |
drh | 6f8c91c | 2003-12-07 00:24:35 +0000 | [diff] [blame] | 6672 | ** |
drh | 9ef5e77 | 2016-08-19 14:20:56 +0000 | [diff] [blame] | 6673 | ** Vacuum the entire database P1. P1 is 0 for "main", and 2 or more |
| 6674 | ** for an attached database. The "temp" database may not be vacuumed. |
drh | 6f8c91c | 2003-12-07 00:24:35 +0000 | [diff] [blame] | 6675 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 6676 | case OP_Vacuum: { |
drh | 9e92a47 | 2013-06-27 17:40:30 +0000 | [diff] [blame] | 6677 | assert( p->readOnly==0 ); |
drh | 9ef5e77 | 2016-08-19 14:20:56 +0000 | [diff] [blame] | 6678 | rc = sqlite3RunVacuum(&p->zErrMsg, db, pOp->p1); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 6679 | if( rc ) goto abort_due_to_error; |
drh | 6f8c91c | 2003-12-07 00:24:35 +0000 | [diff] [blame] | 6680 | break; |
| 6681 | } |
drh | 154d4b2 | 2006-09-21 11:02:16 +0000 | [diff] [blame] | 6682 | #endif |
drh | 6f8c91c | 2003-12-07 00:24:35 +0000 | [diff] [blame] | 6683 | |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 6684 | #if !defined(SQLITE_OMIT_AUTOVACUUM) |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 6685 | /* Opcode: IncrVacuum P1 P2 * * * |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 6686 | ** |
| 6687 | ** Perform a single step of the incremental vacuum procedure on |
drh | ca5557f | 2007-05-04 18:30:40 +0000 | [diff] [blame] | 6688 | ** the P1 database. If the vacuum has finished, jump to instruction |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 6689 | ** P2. Otherwise, fall through to the next instruction. |
| 6690 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 6691 | case OP_IncrVacuum: { /* jump */ |
drh | ca5557f | 2007-05-04 18:30:40 +0000 | [diff] [blame] | 6692 | Btree *pBt; |
| 6693 | |
| 6694 | assert( pOp->p1>=0 && pOp->p1<db->nDb ); |
drh | a7ab6d8 | 2014-07-21 15:44:39 +0000 | [diff] [blame] | 6695 | assert( DbMaskTest(p->btreeMask, pOp->p1) ); |
drh | 9e92a47 | 2013-06-27 17:40:30 +0000 | [diff] [blame] | 6696 | assert( p->readOnly==0 ); |
drh | ca5557f | 2007-05-04 18:30:40 +0000 | [diff] [blame] | 6697 | pBt = db->aDb[pOp->p1].pBt; |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 6698 | rc = sqlite3BtreeIncrVacuum(pBt); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 6699 | VdbeBranchTaken(rc==SQLITE_DONE,2); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 6700 | if( rc ){ |
| 6701 | if( rc!=SQLITE_DONE ) goto abort_due_to_error; |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 6702 | rc = SQLITE_OK; |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 6703 | goto jump_to_p2; |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 6704 | } |
| 6705 | break; |
| 6706 | } |
| 6707 | #endif |
| 6708 | |
drh | ba968db | 2018-07-24 22:02:12 +0000 | [diff] [blame] | 6709 | /* Opcode: Expire P1 P2 * * * |
danielk1977 | a21c6b6 | 2005-01-24 10:25:59 +0000 | [diff] [blame] | 6710 | ** |
drh | 25df48d | 2014-07-22 14:58:12 +0000 | [diff] [blame] | 6711 | ** Cause precompiled statements to expire. When an expired statement |
| 6712 | ** is executed using sqlite3_step() it will either automatically |
| 6713 | ** reprepare itself (if it was originally created using sqlite3_prepare_v2()) |
| 6714 | ** or it will fail with SQLITE_SCHEMA. |
danielk1977 | a21c6b6 | 2005-01-24 10:25:59 +0000 | [diff] [blame] | 6715 | ** |
| 6716 | ** If P1 is 0, then all SQL statements become expired. If P1 is non-zero, |
drh | 25df48d | 2014-07-22 14:58:12 +0000 | [diff] [blame] | 6717 | ** then only the currently executing statement is expired. |
drh | ba968db | 2018-07-24 22:02:12 +0000 | [diff] [blame] | 6718 | ** |
| 6719 | ** If P2 is 0, then SQL statements are expired immediately. If P2 is 1, |
| 6720 | ** then running SQL statements are allowed to continue to run to completion. |
| 6721 | ** The P2==1 case occurs when a CREATE INDEX or similar schema change happens |
| 6722 | ** that might help the statement run faster but which does not affect the |
| 6723 | ** correctness of operation. |
danielk1977 | a21c6b6 | 2005-01-24 10:25:59 +0000 | [diff] [blame] | 6724 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 6725 | case OP_Expire: { |
drh | ba968db | 2018-07-24 22:02:12 +0000 | [diff] [blame] | 6726 | assert( pOp->p2==0 || pOp->p2==1 ); |
danielk1977 | a21c6b6 | 2005-01-24 10:25:59 +0000 | [diff] [blame] | 6727 | if( !pOp->p1 ){ |
drh | ba968db | 2018-07-24 22:02:12 +0000 | [diff] [blame] | 6728 | sqlite3ExpirePreparedStatements(db, pOp->p2); |
danielk1977 | a21c6b6 | 2005-01-24 10:25:59 +0000 | [diff] [blame] | 6729 | }else{ |
drh | ba968db | 2018-07-24 22:02:12 +0000 | [diff] [blame] | 6730 | p->expired = pOp->p2+1; |
danielk1977 | a21c6b6 | 2005-01-24 10:25:59 +0000 | [diff] [blame] | 6731 | } |
| 6732 | break; |
| 6733 | } |
| 6734 | |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 6735 | #ifndef SQLITE_OMIT_SHARED_CACHE |
drh | 6a9ad3d | 2008-04-02 16:29:30 +0000 | [diff] [blame] | 6736 | /* Opcode: TableLock P1 P2 P3 P4 * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 6737 | ** Synopsis: iDb=P1 root=P2 write=P3 |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 6738 | ** |
| 6739 | ** Obtain a lock on a particular table. This instruction is only used when |
| 6740 | ** the shared-cache feature is enabled. |
| 6741 | ** |
danielk1977 | 96d48e9 | 2009-06-29 06:00:37 +0000 | [diff] [blame] | 6742 | ** P1 is the index of the database in sqlite3.aDb[] of the database |
drh | 6a9ad3d | 2008-04-02 16:29:30 +0000 | [diff] [blame] | 6743 | ** on which the lock is acquired. A readlock is obtained if P3==0 or |
| 6744 | ** a write lock if P3==1. |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 6745 | ** |
| 6746 | ** P2 contains the root-page of the table to lock. |
| 6747 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 6748 | ** 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] | 6749 | ** used to generate an error message if the lock cannot be obtained. |
| 6750 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 6751 | case OP_TableLock: { |
danielk1977 | e0d9e6f | 2009-07-03 16:25:06 +0000 | [diff] [blame] | 6752 | u8 isWriteLock = (u8)pOp->p3; |
drh | 169dd92 | 2017-06-26 13:57:49 +0000 | [diff] [blame] | 6753 | if( isWriteLock || 0==(db->flags&SQLITE_ReadUncommit) ){ |
danielk1977 | e0d9e6f | 2009-07-03 16:25:06 +0000 | [diff] [blame] | 6754 | int p1 = pOp->p1; |
| 6755 | assert( p1>=0 && p1<db->nDb ); |
drh | a7ab6d8 | 2014-07-21 15:44:39 +0000 | [diff] [blame] | 6756 | assert( DbMaskTest(p->btreeMask, p1) ); |
danielk1977 | e0d9e6f | 2009-07-03 16:25:06 +0000 | [diff] [blame] | 6757 | assert( isWriteLock==0 || isWriteLock==1 ); |
| 6758 | rc = sqlite3BtreeLockTable(db->aDb[p1].pBt, pOp->p2, isWriteLock); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 6759 | if( rc ){ |
| 6760 | if( (rc&0xFF)==SQLITE_LOCKED ){ |
| 6761 | const char *z = pOp->p4.z; |
| 6762 | sqlite3VdbeError(p, "database table is locked: %s", z); |
| 6763 | } |
| 6764 | goto abort_due_to_error; |
danielk1977 | e0d9e6f | 2009-07-03 16:25:06 +0000 | [diff] [blame] | 6765 | } |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 6766 | } |
| 6767 | break; |
| 6768 | } |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 6769 | #endif /* SQLITE_OMIT_SHARED_CACHE */ |
| 6770 | |
| 6771 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 6772 | /* Opcode: VBegin * * * P4 * |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 6773 | ** |
danielk1977 | 3e3a84d | 2008-08-01 17:37:40 +0000 | [diff] [blame] | 6774 | ** P4 may be a pointer to an sqlite3_vtab structure. If so, call the |
| 6775 | ** xBegin method for that table. |
| 6776 | ** |
| 6777 | ** 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] | 6778 | ** within a callback to a virtual table xSync() method. If it is, the error |
| 6779 | ** code will be set to SQLITE_LOCKED. |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 6780 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 6781 | case OP_VBegin: { |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 6782 | VTable *pVTab; |
| 6783 | pVTab = pOp->p4.pVtab; |
| 6784 | rc = sqlite3VtabBegin(db, pVTab); |
dan | 016f781 | 2013-08-21 17:35:48 +0000 | [diff] [blame] | 6785 | if( pVTab ) sqlite3VtabImportErrmsg(p, pVTab->pVtab); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 6786 | if( rc ) goto abort_due_to_error; |
danielk1977 | f9e7dda | 2006-06-16 16:08:53 +0000 | [diff] [blame] | 6787 | break; |
| 6788 | } |
| 6789 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 6790 | |
| 6791 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
dan | 7377945 | 2015-03-19 18:56:17 +0000 | [diff] [blame] | 6792 | /* Opcode: VCreate P1 P2 * * * |
danielk1977 | f9e7dda | 2006-06-16 16:08:53 +0000 | [diff] [blame] | 6793 | ** |
dan | 7377945 | 2015-03-19 18:56:17 +0000 | [diff] [blame] | 6794 | ** P2 is a register that holds the name of a virtual table in database |
| 6795 | ** P1. Call the xCreate method for that table. |
danielk1977 | f9e7dda | 2006-06-16 16:08:53 +0000 | [diff] [blame] | 6796 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 6797 | case OP_VCreate: { |
dan | 7377945 | 2015-03-19 18:56:17 +0000 | [diff] [blame] | 6798 | Mem sMem; /* For storing the record being decoded */ |
drh | 4746406 | 2015-03-21 12:22:16 +0000 | [diff] [blame] | 6799 | const char *zTab; /* Name of the virtual table */ |
| 6800 | |
dan | 7377945 | 2015-03-19 18:56:17 +0000 | [diff] [blame] | 6801 | memset(&sMem, 0, sizeof(sMem)); |
| 6802 | sMem.db = db; |
drh | 4746406 | 2015-03-21 12:22:16 +0000 | [diff] [blame] | 6803 | /* Because P2 is always a static string, it is impossible for the |
| 6804 | ** sqlite3VdbeMemCopy() to fail */ |
| 6805 | assert( (aMem[pOp->p2].flags & MEM_Str)!=0 ); |
| 6806 | assert( (aMem[pOp->p2].flags & MEM_Static)!=0 ); |
dan | 7377945 | 2015-03-19 18:56:17 +0000 | [diff] [blame] | 6807 | rc = sqlite3VdbeMemCopy(&sMem, &aMem[pOp->p2]); |
drh | 4746406 | 2015-03-21 12:22:16 +0000 | [diff] [blame] | 6808 | assert( rc==SQLITE_OK ); |
| 6809 | zTab = (const char*)sqlite3_value_text(&sMem); |
| 6810 | assert( zTab || db->mallocFailed ); |
| 6811 | if( zTab ){ |
| 6812 | rc = sqlite3VtabCallCreate(db, pOp->p1, zTab, &p->zErrMsg); |
dan | 7377945 | 2015-03-19 18:56:17 +0000 | [diff] [blame] | 6813 | } |
| 6814 | sqlite3VdbeMemRelease(&sMem); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 6815 | if( rc ) goto abort_due_to_error; |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 6816 | break; |
| 6817 | } |
| 6818 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 6819 | |
| 6820 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 6821 | /* Opcode: VDestroy P1 * * P4 * |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 6822 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 6823 | ** 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] | 6824 | ** of that table. |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 6825 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 6826 | case OP_VDestroy: { |
drh | 086723a | 2015-03-24 12:51:52 +0000 | [diff] [blame] | 6827 | db->nVDestroy++; |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 6828 | rc = sqlite3VtabCallDestroy(db, pOp->p1, pOp->p4.z); |
drh | 086723a | 2015-03-24 12:51:52 +0000 | [diff] [blame] | 6829 | db->nVDestroy--; |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 6830 | if( rc ) goto abort_due_to_error; |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 6831 | break; |
| 6832 | } |
| 6833 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 6834 | |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 6835 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 6836 | /* Opcode: VOpen P1 * * P4 * |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 6837 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 6838 | ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure. |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 6839 | ** P1 is a cursor number. This opcode opens a cursor to the virtual |
| 6840 | ** table and stores that cursor in P1. |
| 6841 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 6842 | case OP_VOpen: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 6843 | VdbeCursor *pCur; |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 6844 | sqlite3_vtab_cursor *pVCur; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 6845 | sqlite3_vtab *pVtab; |
drh | f496a7d | 2015-03-24 14:05:50 +0000 | [diff] [blame] | 6846 | const sqlite3_module *pModule; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 6847 | |
drh | 1713afb | 2013-06-28 01:24:57 +0000 | [diff] [blame] | 6848 | assert( p->bIsReader ); |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 6849 | pCur = 0; |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 6850 | pVCur = 0; |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 6851 | pVtab = pOp->p4.pVtab->pVtab; |
drh | f496a7d | 2015-03-24 14:05:50 +0000 | [diff] [blame] | 6852 | if( pVtab==0 || NEVER(pVtab->pModule==0) ){ |
| 6853 | rc = SQLITE_LOCKED; |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 6854 | goto abort_due_to_error; |
drh | f496a7d | 2015-03-24 14:05:50 +0000 | [diff] [blame] | 6855 | } |
| 6856 | pModule = pVtab->pModule; |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 6857 | rc = pModule->xOpen(pVtab, &pVCur); |
dan | 016f781 | 2013-08-21 17:35:48 +0000 | [diff] [blame] | 6858 | sqlite3VtabImportErrmsg(p, pVtab); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 6859 | if( rc ) goto abort_due_to_error; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 6860 | |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 6861 | /* Initialize sqlite3_vtab_cursor base class */ |
| 6862 | pVCur->pVtab = pVtab; |
| 6863 | |
| 6864 | /* Initialize vdbe cursor object */ |
| 6865 | pCur = allocateCursor(p, pOp->p1, 0, -1, CURTYPE_VTAB); |
| 6866 | if( pCur ){ |
| 6867 | pCur->uc.pVCur = pVCur; |
| 6868 | pVtab->nRef++; |
| 6869 | }else{ |
| 6870 | assert( db->mallocFailed ); |
| 6871 | pModule->xClose(pVCur); |
| 6872 | goto no_mem; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 6873 | } |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 6874 | break; |
| 6875 | } |
| 6876 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 6877 | |
| 6878 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
danielk1977 | 6dbee81 | 2008-01-03 18:39:41 +0000 | [diff] [blame] | 6879 | /* Opcode: VFilter P1 P2 P3 P4 * |
drh | 831116d | 2014-04-03 14:31:00 +0000 | [diff] [blame] | 6880 | ** Synopsis: iplan=r[P3] zplan='P4' |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 6881 | ** |
| 6882 | ** P1 is a cursor opened using VOpen. P2 is an address to jump to if |
| 6883 | ** the filtered result set is empty. |
| 6884 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 6885 | ** P4 is either NULL or a string that was generated by the xBestIndex |
| 6886 | ** method of the module. The interpretation of the P4 string is left |
drh | 4be8b51 | 2006-06-13 23:51:34 +0000 | [diff] [blame] | 6887 | ** to the module implementation. |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 6888 | ** |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 6889 | ** This opcode invokes the xFilter method on the virtual table specified |
danielk1977 | 6dbee81 | 2008-01-03 18:39:41 +0000 | [diff] [blame] | 6890 | ** by P1. The integer query plan parameter to xFilter is stored in register |
| 6891 | ** P3. Register P3+1 stores the argc parameter to be passed to the |
drh | 174edc6 | 2008-05-29 05:23:41 +0000 | [diff] [blame] | 6892 | ** xFilter method. Registers P3+2..P3+1+argc are the argc |
| 6893 | ** additional parameters which are passed to |
danielk1977 | 6dbee81 | 2008-01-03 18:39:41 +0000 | [diff] [blame] | 6894 | ** xFilter as argv. Register P3+2 becomes argv[0] when passed to xFilter. |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 6895 | ** |
danielk1977 | 6dbee81 | 2008-01-03 18:39:41 +0000 | [diff] [blame] | 6896 | ** 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] | 6897 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 6898 | case OP_VFilter: { /* jump */ |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 6899 | int nArg; |
danielk1977 | 6dbee81 | 2008-01-03 18:39:41 +0000 | [diff] [blame] | 6900 | int iQuery; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 6901 | const sqlite3_module *pModule; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 6902 | Mem *pQuery; |
| 6903 | Mem *pArgc; |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 6904 | sqlite3_vtab_cursor *pVCur; |
drh | 4dc754d | 2008-07-23 18:17:32 +0000 | [diff] [blame] | 6905 | sqlite3_vtab *pVtab; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 6906 | VdbeCursor *pCur; |
| 6907 | int res; |
| 6908 | int i; |
| 6909 | Mem **apArg; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 6910 | |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 6911 | pQuery = &aMem[pOp->p3]; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 6912 | pArgc = &pQuery[1]; |
| 6913 | pCur = p->apCsr[pOp->p1]; |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 6914 | assert( memIsValid(pQuery) ); |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 6915 | REGISTER_TRACE(pOp->p3, pQuery); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 6916 | assert( pCur->eCurType==CURTYPE_VTAB ); |
| 6917 | pVCur = pCur->uc.pVCur; |
| 6918 | pVtab = pVCur->pVtab; |
drh | 4dc754d | 2008-07-23 18:17:32 +0000 | [diff] [blame] | 6919 | pModule = pVtab->pModule; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 6920 | |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 6921 | /* Grab the index number and argc parameters */ |
danielk1977 | 6dbee81 | 2008-01-03 18:39:41 +0000 | [diff] [blame] | 6922 | assert( (pQuery->flags&MEM_Int)!=0 && pArgc->flags==MEM_Int ); |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 6923 | nArg = (int)pArgc->u.i; |
| 6924 | iQuery = (int)pQuery->u.i; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 6925 | |
drh | 644a529 | 2006-12-20 14:53:38 +0000 | [diff] [blame] | 6926 | /* Invoke the xFilter method */ |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 6927 | res = 0; |
| 6928 | apArg = p->apArg; |
| 6929 | for(i = 0; i<nArg; i++){ |
| 6930 | apArg[i] = &pArgc[i+1]; |
| 6931 | } |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 6932 | rc = pModule->xFilter(pVCur, iQuery, pOp->p4.z, nArg, apArg); |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 6933 | sqlite3VtabImportErrmsg(p, pVtab); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 6934 | if( rc ) goto abort_due_to_error; |
| 6935 | res = pModule->xEof(pVCur); |
drh | 1d454a3 | 2008-01-31 19:34:51 +0000 | [diff] [blame] | 6936 | pCur->nullRow = 0; |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 6937 | VdbeBranchTaken(res!=0,2); |
| 6938 | if( res ) goto jump_to_p2; |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 6939 | break; |
| 6940 | } |
| 6941 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 6942 | |
| 6943 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | ce2fbd1 | 2018-01-12 21:00:14 +0000 | [diff] [blame] | 6944 | /* Opcode: VColumn P1 P2 P3 * P5 |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 6945 | ** Synopsis: r[P3]=vcolumn(P2) |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 6946 | ** |
drh | 6f390be | 2018-01-11 17:04:26 +0000 | [diff] [blame] | 6947 | ** Store in register P3 the value of the P2-th column of |
| 6948 | ** the current row of the virtual-table of cursor P1. |
| 6949 | ** |
| 6950 | ** If the VColumn opcode is being used to fetch the value of |
drh | ce2fbd1 | 2018-01-12 21:00:14 +0000 | [diff] [blame] | 6951 | ** an unchanging column during an UPDATE operation, then the P5 |
| 6952 | ** value is 1. Otherwise, P5 is 0. The P5 value is returned |
drh | bbd574b | 2018-05-24 17:25:35 +0000 | [diff] [blame] | 6953 | ** by sqlite3_vtab_nochange() routine and can be used |
drh | 6f390be | 2018-01-11 17:04:26 +0000 | [diff] [blame] | 6954 | ** by virtual table implementations to return special "no-change" |
| 6955 | ** marks which can be more efficient, depending on the virtual table. |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 6956 | */ |
| 6957 | case OP_VColumn: { |
danielk1977 | 3e3a84d | 2008-08-01 17:37:40 +0000 | [diff] [blame] | 6958 | sqlite3_vtab *pVtab; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 6959 | const sqlite3_module *pModule; |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 6960 | Mem *pDest; |
| 6961 | sqlite3_context sContext; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 6962 | |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 6963 | VdbeCursor *pCur = p->apCsr[pOp->p1]; |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 6964 | assert( pCur->eCurType==CURTYPE_VTAB ); |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 6965 | assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) ); |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 6966 | pDest = &aMem[pOp->p3]; |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 6967 | memAboutToChange(p, pDest); |
drh | 2945b4a | 2008-01-31 15:53:45 +0000 | [diff] [blame] | 6968 | if( pCur->nullRow ){ |
| 6969 | sqlite3VdbeMemSetNull(pDest); |
| 6970 | break; |
| 6971 | } |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 6972 | pVtab = pCur->uc.pVCur->pVtab; |
danielk1977 | 3e3a84d | 2008-08-01 17:37:40 +0000 | [diff] [blame] | 6973 | pModule = pVtab->pModule; |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 6974 | assert( pModule->xColumn ); |
| 6975 | memset(&sContext, 0, sizeof(sContext)); |
drh | 9bd038f | 2014-08-27 14:14:06 +0000 | [diff] [blame] | 6976 | sContext.pOut = pDest; |
drh | ce2fbd1 | 2018-01-12 21:00:14 +0000 | [diff] [blame] | 6977 | if( pOp->p5 ){ |
| 6978 | sqlite3VdbeMemSetNull(pDest); |
| 6979 | pDest->flags = MEM_Null|MEM_Zero; |
| 6980 | pDest->u.nZero = 0; |
| 6981 | }else{ |
| 6982 | MemSetTypeFlag(pDest, MEM_Null); |
| 6983 | } |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 6984 | rc = pModule->xColumn(pCur->uc.pVCur, &sContext, pOp->p2); |
dan | 016f781 | 2013-08-21 17:35:48 +0000 | [diff] [blame] | 6985 | sqlite3VtabImportErrmsg(p, pVtab); |
drh | f09ac0b | 2018-01-23 03:44:06 +0000 | [diff] [blame] | 6986 | if( sContext.isError>0 ){ |
dan | 099fa84 | 2018-01-30 18:33:23 +0000 | [diff] [blame] | 6987 | sqlite3VdbeError(p, "%s", sqlite3_value_text(pDest)); |
drh | 4c8555f | 2009-06-25 01:47:11 +0000 | [diff] [blame] | 6988 | rc = sContext.isError; |
| 6989 | } |
drh | 9bd038f | 2014-08-27 14:14:06 +0000 | [diff] [blame] | 6990 | sqlite3VdbeChangeEncoding(pDest, encoding); |
drh | 5ff4437 | 2009-11-24 16:26:17 +0000 | [diff] [blame] | 6991 | REGISTER_TRACE(pOp->p3, pDest); |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 6992 | UPDATE_MAX_BLOBSIZE(pDest); |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 6993 | |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 6994 | if( sqlite3VdbeMemTooBig(pDest) ){ |
| 6995 | goto too_big; |
| 6996 | } |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 6997 | if( rc ) goto abort_due_to_error; |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 6998 | break; |
| 6999 | } |
| 7000 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 7001 | |
| 7002 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 7003 | /* Opcode: VNext P1 P2 * * * |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 7004 | ** |
| 7005 | ** Advance virtual table P1 to the next row in its result set and |
| 7006 | ** jump to instruction P2. Or, if the virtual table has reached |
| 7007 | ** the end of its result set, then fall through to the next instruction. |
| 7008 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 7009 | case OP_VNext: { /* jump */ |
danielk1977 | 3e3a84d | 2008-08-01 17:37:40 +0000 | [diff] [blame] | 7010 | sqlite3_vtab *pVtab; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 7011 | const sqlite3_module *pModule; |
drh | c54a617 | 2009-06-02 16:06:03 +0000 | [diff] [blame] | 7012 | int res; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 7013 | VdbeCursor *pCur; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 7014 | |
drh | c54a617 | 2009-06-02 16:06:03 +0000 | [diff] [blame] | 7015 | res = 0; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 7016 | pCur = p->apCsr[pOp->p1]; |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 7017 | assert( pCur->eCurType==CURTYPE_VTAB ); |
drh | 2945b4a | 2008-01-31 15:53:45 +0000 | [diff] [blame] | 7018 | if( pCur->nullRow ){ |
| 7019 | break; |
| 7020 | } |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 7021 | pVtab = pCur->uc.pVCur->pVtab; |
danielk1977 | 3e3a84d | 2008-08-01 17:37:40 +0000 | [diff] [blame] | 7022 | pModule = pVtab->pModule; |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 7023 | assert( pModule->xNext ); |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 7024 | |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 7025 | /* Invoke the xNext() method of the module. There is no way for the |
| 7026 | ** underlying implementation to return an error if one occurs during |
| 7027 | ** xNext(). Instead, if an error occurs, true is returned (indicating that |
| 7028 | ** data is available) and the error code returned when xColumn or |
| 7029 | ** some other method is next invoked on the save virtual table cursor. |
| 7030 | */ |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 7031 | rc = pModule->xNext(pCur->uc.pVCur); |
dan | 016f781 | 2013-08-21 17:35:48 +0000 | [diff] [blame] | 7032 | sqlite3VtabImportErrmsg(p, pVtab); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 7033 | if( rc ) goto abort_due_to_error; |
| 7034 | res = pModule->xEof(pCur->uc.pVCur); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 7035 | VdbeBranchTaken(!res,2); |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 7036 | if( !res ){ |
| 7037 | /* If there is data, jump to P2 */ |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 7038 | goto jump_to_p2_and_check_for_interrupt; |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 7039 | } |
drh | 49afe3a | 2013-07-10 03:05:14 +0000 | [diff] [blame] | 7040 | goto check_for_interrupt; |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 7041 | } |
| 7042 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 7043 | |
danielk1977 | 182c4ba | 2007-06-27 15:53:34 +0000 | [diff] [blame] | 7044 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 7045 | /* Opcode: VRename P1 * * P4 * |
danielk1977 | 182c4ba | 2007-06-27 15:53:34 +0000 | [diff] [blame] | 7046 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 7047 | ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure. |
danielk1977 | 182c4ba | 2007-06-27 15:53:34 +0000 | [diff] [blame] | 7048 | ** This opcode invokes the corresponding xRename method. The value |
danielk1977 | 6dbee81 | 2008-01-03 18:39:41 +0000 | [diff] [blame] | 7049 | ** in register P1 is passed as the zName argument to the xRename method. |
danielk1977 | 182c4ba | 2007-06-27 15:53:34 +0000 | [diff] [blame] | 7050 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 7051 | case OP_VRename: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 7052 | sqlite3_vtab *pVtab; |
| 7053 | Mem *pName; |
| 7054 | |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 7055 | pVtab = pOp->p4.pVtab->pVtab; |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 7056 | pName = &aMem[pOp->p1]; |
danielk1977 | 182c4ba | 2007-06-27 15:53:34 +0000 | [diff] [blame] | 7057 | assert( pVtab->pModule->xRename ); |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 7058 | assert( memIsValid(pName) ); |
drh | 9e92a47 | 2013-06-27 17:40:30 +0000 | [diff] [blame] | 7059 | assert( p->readOnly==0 ); |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 7060 | REGISTER_TRACE(pOp->p1, pName); |
drh | 35f6b93 | 2009-06-23 14:15:04 +0000 | [diff] [blame] | 7061 | assert( pName->flags & MEM_Str ); |
drh | 98655a6 | 2011-10-18 22:07:47 +0000 | [diff] [blame] | 7062 | testcase( pName->enc==SQLITE_UTF8 ); |
| 7063 | testcase( pName->enc==SQLITE_UTF16BE ); |
| 7064 | testcase( pName->enc==SQLITE_UTF16LE ); |
| 7065 | rc = sqlite3VdbeChangeEncoding(pName, SQLITE_UTF8); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 7066 | if( rc ) goto abort_due_to_error; |
| 7067 | rc = pVtab->pModule->xRename(pVtab, pName->z); |
| 7068 | sqlite3VtabImportErrmsg(p, pVtab); |
| 7069 | p->expired = 0; |
| 7070 | if( rc ) goto abort_due_to_error; |
danielk1977 | 182c4ba | 2007-06-27 15:53:34 +0000 | [diff] [blame] | 7071 | break; |
| 7072 | } |
| 7073 | #endif |
drh | 4cbdda9 | 2006-06-14 19:00:20 +0000 | [diff] [blame] | 7074 | |
| 7075 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | 0fd6135 | 2014-02-07 02:29:45 +0000 | [diff] [blame] | 7076 | /* Opcode: VUpdate P1 P2 P3 P4 P5 |
drh | f63552b | 2013-10-30 00:25:03 +0000 | [diff] [blame] | 7077 | ** Synopsis: data=r[P3@P2] |
danielk1977 | 399918f | 2006-06-14 13:03:23 +0000 | [diff] [blame] | 7078 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 7079 | ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure. |
danielk1977 | 399918f | 2006-06-14 13:03:23 +0000 | [diff] [blame] | 7080 | ** This opcode invokes the corresponding xUpdate method. P2 values |
danielk1977 | 2a339ff | 2008-01-03 17:31:44 +0000 | [diff] [blame] | 7081 | ** are contiguous memory cells starting at P3 to pass to the xUpdate |
| 7082 | ** invocation. The value in register (P3+P2-1) corresponds to the |
| 7083 | ** p2th element of the argv array passed to xUpdate. |
drh | 4cbdda9 | 2006-06-14 19:00:20 +0000 | [diff] [blame] | 7084 | ** |
| 7085 | ** The xUpdate method will do a DELETE or an INSERT or both. |
danielk1977 | 2a339ff | 2008-01-03 17:31:44 +0000 | [diff] [blame] | 7086 | ** The argv[0] element (which corresponds to memory cell P3) |
| 7087 | ** is the rowid of a row to delete. If argv[0] is NULL then no |
| 7088 | ** deletion occurs. The argv[1] element is the rowid of the new |
| 7089 | ** row. This can be NULL to have the virtual table select the new |
| 7090 | ** rowid for itself. The subsequent elements in the array are |
| 7091 | ** the values of columns in the new row. |
drh | 4cbdda9 | 2006-06-14 19:00:20 +0000 | [diff] [blame] | 7092 | ** |
| 7093 | ** If P2==1 then no insert is performed. argv[0] is the rowid of |
| 7094 | ** a row to delete. |
danielk1977 | 1f6eec5 | 2006-06-16 06:17:47 +0000 | [diff] [blame] | 7095 | ** |
| 7096 | ** P1 is a boolean flag. If it is set to true and the xUpdate call |
| 7097 | ** is successful, then the value returned by sqlite3_last_insert_rowid() |
| 7098 | ** is set to the value of the rowid for the row just inserted. |
drh | 0fd6135 | 2014-02-07 02:29:45 +0000 | [diff] [blame] | 7099 | ** |
| 7100 | ** P5 is the error actions (OE_Replace, OE_Fail, OE_Ignore, etc) to |
| 7101 | ** apply in the case of a constraint failure on an insert or update. |
danielk1977 | 399918f | 2006-06-14 13:03:23 +0000 | [diff] [blame] | 7102 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 7103 | case OP_VUpdate: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 7104 | sqlite3_vtab *pVtab; |
drh | f496a7d | 2015-03-24 14:05:50 +0000 | [diff] [blame] | 7105 | const sqlite3_module *pModule; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 7106 | int nArg; |
| 7107 | int i; |
| 7108 | sqlite_int64 rowid; |
| 7109 | Mem **apArg; |
| 7110 | Mem *pX; |
| 7111 | |
dan | b061d05 | 2011-04-25 18:49:57 +0000 | [diff] [blame] | 7112 | assert( pOp->p2==1 || pOp->p5==OE_Fail || pOp->p5==OE_Rollback |
| 7113 | || pOp->p5==OE_Abort || pOp->p5==OE_Ignore || pOp->p5==OE_Replace |
| 7114 | ); |
drh | 9e92a47 | 2013-06-27 17:40:30 +0000 | [diff] [blame] | 7115 | assert( p->readOnly==0 ); |
dan | 466ea9b | 2018-06-13 11:11:13 +0000 | [diff] [blame] | 7116 | if( db->mallocFailed ) goto no_mem; |
drh | 4031baf | 2018-05-28 17:31:20 +0000 | [diff] [blame] | 7117 | sqlite3VdbeIncrWriteCounter(p, 0); |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 7118 | pVtab = pOp->p4.pVtab->pVtab; |
drh | f496a7d | 2015-03-24 14:05:50 +0000 | [diff] [blame] | 7119 | if( pVtab==0 || NEVER(pVtab->pModule==0) ){ |
| 7120 | rc = SQLITE_LOCKED; |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 7121 | goto abort_due_to_error; |
drh | f496a7d | 2015-03-24 14:05:50 +0000 | [diff] [blame] | 7122 | } |
| 7123 | pModule = pVtab->pModule; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 7124 | nArg = pOp->p2; |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 7125 | assert( pOp->p4type==P4_VTAB ); |
drh | 35f6b93 | 2009-06-23 14:15:04 +0000 | [diff] [blame] | 7126 | if( ALWAYS(pModule->xUpdate) ){ |
dan | b061d05 | 2011-04-25 18:49:57 +0000 | [diff] [blame] | 7127 | u8 vtabOnConflict = db->vtabOnConflict; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 7128 | apArg = p->apArg; |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 7129 | pX = &aMem[pOp->p3]; |
danielk1977 | 2a339ff | 2008-01-03 17:31:44 +0000 | [diff] [blame] | 7130 | for(i=0; i<nArg; i++){ |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 7131 | assert( memIsValid(pX) ); |
| 7132 | memAboutToChange(p, pX); |
drh | 9c41938 | 2006-06-16 21:13:21 +0000 | [diff] [blame] | 7133 | apArg[i] = pX; |
danielk1977 | 2a339ff | 2008-01-03 17:31:44 +0000 | [diff] [blame] | 7134 | pX++; |
danielk1977 | 399918f | 2006-06-14 13:03:23 +0000 | [diff] [blame] | 7135 | } |
dan | b061d05 | 2011-04-25 18:49:57 +0000 | [diff] [blame] | 7136 | db->vtabOnConflict = pOp->p5; |
danielk1977 | 1f6eec5 | 2006-06-16 06:17:47 +0000 | [diff] [blame] | 7137 | rc = pModule->xUpdate(pVtab, nArg, apArg, &rowid); |
dan | b061d05 | 2011-04-25 18:49:57 +0000 | [diff] [blame] | 7138 | db->vtabOnConflict = vtabOnConflict; |
dan | 016f781 | 2013-08-21 17:35:48 +0000 | [diff] [blame] | 7139 | sqlite3VtabImportErrmsg(p, pVtab); |
drh | 35f6b93 | 2009-06-23 14:15:04 +0000 | [diff] [blame] | 7140 | if( rc==SQLITE_OK && pOp->p1 ){ |
danielk1977 | 1f6eec5 | 2006-06-16 06:17:47 +0000 | [diff] [blame] | 7141 | assert( nArg>1 && apArg[0] && (apArg[0]->flags&MEM_Null) ); |
drh | fae58d5 | 2017-01-26 17:26:44 +0000 | [diff] [blame] | 7142 | db->lastRowid = rowid; |
danielk1977 | 1f6eec5 | 2006-06-16 06:17:47 +0000 | [diff] [blame] | 7143 | } |
drh | d91c1a1 | 2013-02-09 13:58:25 +0000 | [diff] [blame] | 7144 | if( (rc&0xff)==SQLITE_CONSTRAINT && pOp->p4.pVtab->bConstraint ){ |
dan | b061d05 | 2011-04-25 18:49:57 +0000 | [diff] [blame] | 7145 | if( pOp->p5==OE_Ignore ){ |
| 7146 | rc = SQLITE_OK; |
| 7147 | }else{ |
| 7148 | p->errorAction = ((pOp->p5==OE_Replace) ? OE_Abort : pOp->p5); |
| 7149 | } |
| 7150 | }else{ |
| 7151 | p->nChange++; |
| 7152 | } |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 7153 | if( rc ) goto abort_due_to_error; |
danielk1977 | 399918f | 2006-06-14 13:03:23 +0000 | [diff] [blame] | 7154 | } |
drh | 4cbdda9 | 2006-06-14 19:00:20 +0000 | [diff] [blame] | 7155 | break; |
danielk1977 | 399918f | 2006-06-14 13:03:23 +0000 | [diff] [blame] | 7156 | } |
| 7157 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 7158 | |
danielk1977 | 59a9379 | 2008-05-15 17:48:20 +0000 | [diff] [blame] | 7159 | #ifndef SQLITE_OMIT_PAGER_PRAGMAS |
| 7160 | /* Opcode: Pagecount P1 P2 * * * |
| 7161 | ** |
| 7162 | ** Write the current number of pages in database P1 to memory cell P2. |
| 7163 | */ |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 7164 | case OP_Pagecount: { /* out2 */ |
| 7165 | pOut = out2Prerelease(p, pOp); |
drh | b129915 | 2010-03-30 22:58:33 +0000 | [diff] [blame] | 7166 | pOut->u.i = sqlite3BtreeLastPage(db->aDb[pOp->p1].pBt); |
danielk1977 | 59a9379 | 2008-05-15 17:48:20 +0000 | [diff] [blame] | 7167 | break; |
| 7168 | } |
| 7169 | #endif |
| 7170 | |
drh | 60ac3f4 | 2010-11-23 18:59:27 +0000 | [diff] [blame] | 7171 | |
| 7172 | #ifndef SQLITE_OMIT_PAGER_PRAGMAS |
| 7173 | /* Opcode: MaxPgcnt P1 P2 P3 * * |
| 7174 | ** |
| 7175 | ** 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] | 7176 | ** Do not let the maximum page count fall below the current page count and |
| 7177 | ** do not change the maximum page count value if P3==0. |
| 7178 | ** |
drh | 60ac3f4 | 2010-11-23 18:59:27 +0000 | [diff] [blame] | 7179 | ** Store the maximum page count after the change in register P2. |
| 7180 | */ |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 7181 | case OP_MaxPgcnt: { /* out2 */ |
drh | c84e033 | 2010-11-23 20:25:08 +0000 | [diff] [blame] | 7182 | unsigned int newMax; |
drh | 60ac3f4 | 2010-11-23 18:59:27 +0000 | [diff] [blame] | 7183 | Btree *pBt; |
| 7184 | |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 7185 | pOut = out2Prerelease(p, pOp); |
drh | 60ac3f4 | 2010-11-23 18:59:27 +0000 | [diff] [blame] | 7186 | pBt = db->aDb[pOp->p1].pBt; |
drh | c84e033 | 2010-11-23 20:25:08 +0000 | [diff] [blame] | 7187 | newMax = 0; |
| 7188 | if( pOp->p3 ){ |
| 7189 | newMax = sqlite3BtreeLastPage(pBt); |
drh | 6ea28d6 | 2010-11-26 16:49:59 +0000 | [diff] [blame] | 7190 | if( newMax < (unsigned)pOp->p3 ) newMax = (unsigned)pOp->p3; |
drh | c84e033 | 2010-11-23 20:25:08 +0000 | [diff] [blame] | 7191 | } |
| 7192 | pOut->u.i = sqlite3BtreeMaxPageCount(pBt, newMax); |
drh | 60ac3f4 | 2010-11-23 18:59:27 +0000 | [diff] [blame] | 7193 | break; |
| 7194 | } |
| 7195 | #endif |
| 7196 | |
drh | 3e34eab | 2017-07-19 19:48:40 +0000 | [diff] [blame] | 7197 | /* Opcode: Function0 P1 P2 P3 P4 P5 |
| 7198 | ** Synopsis: r[P3]=func(r[P2@P5]) |
| 7199 | ** |
| 7200 | ** Invoke a user function (P4 is a pointer to a FuncDef object that |
| 7201 | ** defines the function) with P5 arguments taken from register P2 and |
| 7202 | ** successors. The result of the function is stored in register P3. |
| 7203 | ** Register P3 must not be one of the function inputs. |
| 7204 | ** |
| 7205 | ** P1 is a 32-bit bitmask indicating whether or not each argument to the |
| 7206 | ** function was determined to be constant at compile time. If the first |
| 7207 | ** argument was constant then bit 0 of P1 is set. This is used to determine |
| 7208 | ** whether meta data associated with a user function argument using the |
| 7209 | ** sqlite3_set_auxdata() API may be safely retained until the next |
| 7210 | ** invocation of this opcode. |
| 7211 | ** |
| 7212 | ** See also: Function, AggStep, AggFinal |
| 7213 | */ |
| 7214 | /* Opcode: Function P1 P2 P3 P4 P5 |
| 7215 | ** Synopsis: r[P3]=func(r[P2@P5]) |
| 7216 | ** |
| 7217 | ** Invoke a user function (P4 is a pointer to an sqlite3_context object that |
| 7218 | ** contains a pointer to the function to be run) with P5 arguments taken |
| 7219 | ** from register P2 and successors. The result of the function is stored |
| 7220 | ** in register P3. Register P3 must not be one of the function inputs. |
| 7221 | ** |
| 7222 | ** P1 is a 32-bit bitmask indicating whether or not each argument to the |
| 7223 | ** function was determined to be constant at compile time. If the first |
| 7224 | ** argument was constant then bit 0 of P1 is set. This is used to determine |
| 7225 | ** whether meta data associated with a user function argument using the |
| 7226 | ** sqlite3_set_auxdata() API may be safely retained until the next |
| 7227 | ** invocation of this opcode. |
| 7228 | ** |
| 7229 | ** SQL functions are initially coded as OP_Function0 with P4 pointing |
| 7230 | ** to a FuncDef object. But on first evaluation, the P4 operand is |
| 7231 | ** automatically converted into an sqlite3_context object and the operation |
| 7232 | ** changed to this OP_Function opcode. In this way, the initialization of |
| 7233 | ** the sqlite3_context object occurs only once, rather than once for each |
| 7234 | ** evaluation of the function. |
| 7235 | ** |
| 7236 | ** See also: Function0, AggStep, AggFinal |
| 7237 | */ |
mistachkin | 758784d | 2018-07-25 15:12:29 +0000 | [diff] [blame] | 7238 | case OP_PureFunc0: /* group */ |
| 7239 | case OP_Function0: { /* group */ |
drh | 3e34eab | 2017-07-19 19:48:40 +0000 | [diff] [blame] | 7240 | int n; |
| 7241 | sqlite3_context *pCtx; |
| 7242 | |
| 7243 | assert( pOp->p4type==P4_FUNCDEF ); |
| 7244 | n = pOp->p5; |
| 7245 | assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) ); |
| 7246 | assert( n==0 || (pOp->p2>0 && pOp->p2+n<=(p->nMem+1 - p->nCursor)+1) ); |
| 7247 | assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n ); |
| 7248 | pCtx = sqlite3DbMallocRawNN(db, sizeof(*pCtx) + (n-1)*sizeof(sqlite3_value*)); |
| 7249 | if( pCtx==0 ) goto no_mem; |
| 7250 | pCtx->pOut = 0; |
| 7251 | pCtx->pFunc = pOp->p4.pFunc; |
| 7252 | pCtx->iOp = (int)(pOp - aOp); |
| 7253 | pCtx->pVdbe = p; |
drh | f09ac0b | 2018-01-23 03:44:06 +0000 | [diff] [blame] | 7254 | pCtx->isError = 0; |
drh | 3e34eab | 2017-07-19 19:48:40 +0000 | [diff] [blame] | 7255 | pCtx->argc = n; |
| 7256 | pOp->p4type = P4_FUNCCTX; |
| 7257 | pOp->p4.pCtx = pCtx; |
| 7258 | assert( OP_PureFunc == OP_PureFunc0+2 ); |
| 7259 | assert( OP_Function == OP_Function0+2 ); |
| 7260 | pOp->opcode += 2; |
| 7261 | /* Fall through into OP_Function */ |
| 7262 | } |
mistachkin | 758784d | 2018-07-25 15:12:29 +0000 | [diff] [blame] | 7263 | case OP_PureFunc: /* group */ |
| 7264 | case OP_Function: { /* group */ |
drh | 3e34eab | 2017-07-19 19:48:40 +0000 | [diff] [blame] | 7265 | int i; |
| 7266 | sqlite3_context *pCtx; |
| 7267 | |
| 7268 | assert( pOp->p4type==P4_FUNCCTX ); |
| 7269 | pCtx = pOp->p4.pCtx; |
| 7270 | |
| 7271 | /* If this function is inside of a trigger, the register array in aMem[] |
| 7272 | ** might change from one evaluation to the next. The next block of code |
| 7273 | ** checks to see if the register array has changed, and if so it |
| 7274 | ** reinitializes the relavant parts of the sqlite3_context object */ |
| 7275 | pOut = &aMem[pOp->p3]; |
| 7276 | if( pCtx->pOut != pOut ){ |
| 7277 | pCtx->pOut = pOut; |
| 7278 | for(i=pCtx->argc-1; i>=0; i--) pCtx->argv[i] = &aMem[pOp->p2+i]; |
| 7279 | } |
| 7280 | |
| 7281 | memAboutToChange(p, pOut); |
| 7282 | #ifdef SQLITE_DEBUG |
| 7283 | for(i=0; i<pCtx->argc; i++){ |
| 7284 | assert( memIsValid(pCtx->argv[i]) ); |
| 7285 | REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]); |
| 7286 | } |
| 7287 | #endif |
| 7288 | MemSetTypeFlag(pOut, MEM_Null); |
drh | f09ac0b | 2018-01-23 03:44:06 +0000 | [diff] [blame] | 7289 | assert( pCtx->isError==0 ); |
drh | 3e34eab | 2017-07-19 19:48:40 +0000 | [diff] [blame] | 7290 | (*pCtx->pFunc->xSFunc)(pCtx, pCtx->argc, pCtx->argv);/* IMP: R-24505-23230 */ |
| 7291 | |
| 7292 | /* If the function returned an error, throw an exception */ |
drh | f09ac0b | 2018-01-23 03:44:06 +0000 | [diff] [blame] | 7293 | if( pCtx->isError ){ |
| 7294 | if( pCtx->isError>0 ){ |
drh | 3e34eab | 2017-07-19 19:48:40 +0000 | [diff] [blame] | 7295 | sqlite3VdbeError(p, "%s", sqlite3_value_text(pOut)); |
| 7296 | rc = pCtx->isError; |
| 7297 | } |
| 7298 | sqlite3VdbeDeleteAuxData(db, &p->pAuxData, pCtx->iOp, pOp->p1); |
drh | f09ac0b | 2018-01-23 03:44:06 +0000 | [diff] [blame] | 7299 | pCtx->isError = 0; |
drh | 3e34eab | 2017-07-19 19:48:40 +0000 | [diff] [blame] | 7300 | if( rc ) goto abort_due_to_error; |
| 7301 | } |
| 7302 | |
| 7303 | /* Copy the result of the function into register P3 */ |
| 7304 | if( pOut->flags & (MEM_Str|MEM_Blob) ){ |
| 7305 | sqlite3VdbeChangeEncoding(pOut, encoding); |
| 7306 | if( sqlite3VdbeMemTooBig(pOut) ) goto too_big; |
| 7307 | } |
| 7308 | |
| 7309 | REGISTER_TRACE(pOp->p3, pOut); |
| 7310 | UPDATE_MAX_BLOBSIZE(pOut); |
| 7311 | break; |
| 7312 | } |
| 7313 | |
drh | f259df5 | 2017-12-27 20:38:35 +0000 | [diff] [blame] | 7314 | /* Opcode: Trace P1 P2 * P4 * |
| 7315 | ** |
| 7316 | ** Write P4 on the statement trace output if statement tracing is |
| 7317 | ** enabled. |
| 7318 | ** |
| 7319 | ** Operand P1 must be 0x7fffffff and P2 must positive. |
| 7320 | */ |
drh | 74588ce | 2017-09-13 00:13:05 +0000 | [diff] [blame] | 7321 | /* Opcode: Init P1 P2 P3 P4 * |
drh | 72e26de | 2016-08-24 21:24:04 +0000 | [diff] [blame] | 7322 | ** Synopsis: Start at P2 |
drh | aceb31b | 2014-02-08 01:40:27 +0000 | [diff] [blame] | 7323 | ** |
| 7324 | ** Programs contain a single instance of this opcode as the very first |
| 7325 | ** opcode. |
drh | 949f9cd | 2008-01-12 21:35:57 +0000 | [diff] [blame] | 7326 | ** |
| 7327 | ** If tracing is enabled (by the sqlite3_trace()) interface, then |
| 7328 | ** the UTF-8 string contained in P4 is emitted on the trace callback. |
drh | aceb31b | 2014-02-08 01:40:27 +0000 | [diff] [blame] | 7329 | ** Or if P4 is blank, use the string returned by sqlite3_sql(). |
| 7330 | ** |
| 7331 | ** If P2 is not zero, jump to instruction P2. |
drh | 9e5eb9c | 2016-09-18 16:08:10 +0000 | [diff] [blame] | 7332 | ** |
| 7333 | ** Increment the value of P1 so that OP_Once opcodes will jump the |
| 7334 | ** first time they are evaluated for this run. |
drh | 74588ce | 2017-09-13 00:13:05 +0000 | [diff] [blame] | 7335 | ** |
| 7336 | ** If P3 is not zero, then it is an address to jump to if an SQLITE_CORRUPT |
| 7337 | ** error is encountered. |
drh | 949f9cd | 2008-01-12 21:35:57 +0000 | [diff] [blame] | 7338 | */ |
drh | f259df5 | 2017-12-27 20:38:35 +0000 | [diff] [blame] | 7339 | case OP_Trace: |
drh | aceb31b | 2014-02-08 01:40:27 +0000 | [diff] [blame] | 7340 | case OP_Init: { /* jump */ |
drh | 9e5eb9c | 2016-09-18 16:08:10 +0000 | [diff] [blame] | 7341 | int i; |
drh | b9f4799 | 2018-01-24 12:14:43 +0000 | [diff] [blame] | 7342 | #ifndef SQLITE_OMIT_TRACE |
| 7343 | char *zTrace; |
| 7344 | #endif |
drh | 5fe63bf | 2016-07-25 02:42:22 +0000 | [diff] [blame] | 7345 | |
| 7346 | /* If the P4 argument is not NULL, then it must be an SQL comment string. |
| 7347 | ** The "--" string is broken up to prevent false-positives with srcck1.c. |
| 7348 | ** |
| 7349 | ** This assert() provides evidence for: |
| 7350 | ** EVIDENCE-OF: R-50676-09860 The callback can compute the same text that |
| 7351 | ** would have been returned by the legacy sqlite3_trace() interface by |
| 7352 | ** using the X argument when X begins with "--" and invoking |
| 7353 | ** sqlite3_expanded_sql(P) otherwise. |
| 7354 | */ |
| 7355 | assert( pOp->p4.z==0 || strncmp(pOp->p4.z, "-" "- ", 3)==0 ); |
drh | f259df5 | 2017-12-27 20:38:35 +0000 | [diff] [blame] | 7356 | |
| 7357 | /* OP_Init is always instruction 0 */ |
| 7358 | assert( pOp==p->aOp || pOp->opcode==OP_Trace ); |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 7359 | |
drh | aceb31b | 2014-02-08 01:40:27 +0000 | [diff] [blame] | 7360 | #ifndef SQLITE_OMIT_TRACE |
drh | fca760c | 2016-07-14 01:09:08 +0000 | [diff] [blame] | 7361 | if( (db->mTrace & (SQLITE_TRACE_STMT|SQLITE_TRACE_LEGACY))!=0 |
drh | 37f58e9 | 2012-09-04 21:34:26 +0000 | [diff] [blame] | 7362 | && !p->doingRerun |
| 7363 | && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0 |
| 7364 | ){ |
drh | 3d2a529 | 2016-07-13 22:55:01 +0000 | [diff] [blame] | 7365 | #ifndef SQLITE_OMIT_DEPRECATED |
drh | fca760c | 2016-07-14 01:09:08 +0000 | [diff] [blame] | 7366 | if( db->mTrace & SQLITE_TRACE_LEGACY ){ |
| 7367 | void (*x)(void*,const char*) = (void(*)(void*,const char*))db->xTrace; |
drh | 5fe63bf | 2016-07-25 02:42:22 +0000 | [diff] [blame] | 7368 | char *z = sqlite3VdbeExpandSql(p, zTrace); |
drh | fca760c | 2016-07-14 01:09:08 +0000 | [diff] [blame] | 7369 | x(db->pTraceArg, z); |
drh | bd441f7 | 2016-07-25 02:31:48 +0000 | [diff] [blame] | 7370 | sqlite3_free(z); |
drh | fca760c | 2016-07-14 01:09:08 +0000 | [diff] [blame] | 7371 | }else |
drh | 3d2a529 | 2016-07-13 22:55:01 +0000 | [diff] [blame] | 7372 | #endif |
drh | 7adbcff | 2017-03-20 15:29:28 +0000 | [diff] [blame] | 7373 | if( db->nVdbeExec>1 ){ |
| 7374 | char *z = sqlite3MPrintf(db, "-- %s", zTrace); |
| 7375 | (void)db->xTrace(SQLITE_TRACE_STMT, db->pTraceArg, p, z); |
| 7376 | sqlite3DbFree(db, z); |
| 7377 | }else{ |
drh | bd441f7 | 2016-07-25 02:31:48 +0000 | [diff] [blame] | 7378 | (void)db->xTrace(SQLITE_TRACE_STMT, db->pTraceArg, p, zTrace); |
drh | 3d2a529 | 2016-07-13 22:55:01 +0000 | [diff] [blame] | 7379 | } |
drh | 949f9cd | 2008-01-12 21:35:57 +0000 | [diff] [blame] | 7380 | } |
drh | 8f8b231 | 2013-10-18 20:03:43 +0000 | [diff] [blame] | 7381 | #ifdef SQLITE_USE_FCNTL_TRACE |
| 7382 | zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql); |
| 7383 | if( zTrace ){ |
mistachkin | d8992ce | 2016-09-20 17:49:01 +0000 | [diff] [blame] | 7384 | int j; |
| 7385 | for(j=0; j<db->nDb; j++){ |
| 7386 | if( DbMaskTest(p->btreeMask, j)==0 ) continue; |
| 7387 | sqlite3_file_control(db, db->aDb[j].zDbSName, SQLITE_FCNTL_TRACE, zTrace); |
drh | 8f8b231 | 2013-10-18 20:03:43 +0000 | [diff] [blame] | 7388 | } |
| 7389 | } |
| 7390 | #endif /* SQLITE_USE_FCNTL_TRACE */ |
drh | c3f1d5f | 2011-05-30 23:42:16 +0000 | [diff] [blame] | 7391 | #ifdef SQLITE_DEBUG |
| 7392 | if( (db->flags & SQLITE_SqlTrace)!=0 |
| 7393 | && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0 |
| 7394 | ){ |
| 7395 | sqlite3DebugPrintf("SQL-trace: %s\n", zTrace); |
| 7396 | } |
| 7397 | #endif /* SQLITE_DEBUG */ |
drh | aceb31b | 2014-02-08 01:40:27 +0000 | [diff] [blame] | 7398 | #endif /* SQLITE_OMIT_TRACE */ |
drh | 4910a76 | 2016-09-03 01:46:15 +0000 | [diff] [blame] | 7399 | assert( pOp->p2>0 ); |
drh | 9e5eb9c | 2016-09-18 16:08:10 +0000 | [diff] [blame] | 7400 | if( pOp->p1>=sqlite3GlobalConfig.iOnceResetThreshold ){ |
drh | f259df5 | 2017-12-27 20:38:35 +0000 | [diff] [blame] | 7401 | if( pOp->opcode==OP_Trace ) break; |
drh | 9e5eb9c | 2016-09-18 16:08:10 +0000 | [diff] [blame] | 7402 | for(i=1; i<p->nOp; i++){ |
| 7403 | if( p->aOp[i].opcode==OP_Once ) p->aOp[i].p1 = 0; |
| 7404 | } |
| 7405 | pOp->p1 = 0; |
| 7406 | } |
| 7407 | pOp->p1++; |
drh | 00d11d4 | 2017-06-29 12:49:18 +0000 | [diff] [blame] | 7408 | p->aCounter[SQLITE_STMTSTATUS_RUN]++; |
drh | 4910a76 | 2016-09-03 01:46:15 +0000 | [diff] [blame] | 7409 | goto jump_to_p2; |
drh | 949f9cd | 2008-01-12 21:35:57 +0000 | [diff] [blame] | 7410 | } |
drh | 949f9cd | 2008-01-12 21:35:57 +0000 | [diff] [blame] | 7411 | |
drh | 2893536 | 2013-12-07 20:39:19 +0000 | [diff] [blame] | 7412 | #ifdef SQLITE_ENABLE_CURSOR_HINTS |
drh | 0df5701 | 2015-08-14 15:05:55 +0000 | [diff] [blame] | 7413 | /* Opcode: CursorHint P1 * * P4 * |
drh | 2893536 | 2013-12-07 20:39:19 +0000 | [diff] [blame] | 7414 | ** |
| 7415 | ** Provide a hint to cursor P1 that it only needs to return rows that |
drh | 0df5701 | 2015-08-14 15:05:55 +0000 | [diff] [blame] | 7416 | ** satisfy the Expr in P4. TK_REGISTER terms in the P4 expression refer |
| 7417 | ** to values currently held in registers. TK_COLUMN terms in the P4 |
| 7418 | ** expression refer to columns in the b-tree to which cursor P1 is pointing. |
drh | 2893536 | 2013-12-07 20:39:19 +0000 | [diff] [blame] | 7419 | */ |
| 7420 | case OP_CursorHint: { |
| 7421 | VdbeCursor *pC; |
| 7422 | |
| 7423 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 7424 | assert( pOp->p4type==P4_EXPR ); |
| 7425 | pC = p->apCsr[pOp->p1]; |
dan | 91d3a61 | 2014-07-15 11:59:44 +0000 | [diff] [blame] | 7426 | if( pC ){ |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 7427 | assert( pC->eCurType==CURTYPE_BTREE ); |
drh | 62aaa6c | 2015-11-21 17:27:42 +0000 | [diff] [blame] | 7428 | sqlite3BtreeCursorHint(pC->uc.pCursor, BTREE_HINT_RANGE, |
| 7429 | pOp->p4.pExpr, aMem); |
dan | 91d3a61 | 2014-07-15 11:59:44 +0000 | [diff] [blame] | 7430 | } |
drh | 2893536 | 2013-12-07 20:39:19 +0000 | [diff] [blame] | 7431 | break; |
| 7432 | } |
| 7433 | #endif /* SQLITE_ENABLE_CURSOR_HINTS */ |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 7434 | |
drh | 4031baf | 2018-05-28 17:31:20 +0000 | [diff] [blame] | 7435 | #ifdef SQLITE_DEBUG |
| 7436 | /* Opcode: Abortable * * * * * |
| 7437 | ** |
| 7438 | ** Verify that an Abort can happen. Assert if an Abort at this point |
| 7439 | ** might cause database corruption. This opcode only appears in debugging |
| 7440 | ** builds. |
| 7441 | ** |
| 7442 | ** An Abort is safe if either there have been no writes, or if there is |
| 7443 | ** an active statement journal. |
| 7444 | */ |
| 7445 | case OP_Abortable: { |
| 7446 | sqlite3VdbeAssertAbortable(p); |
| 7447 | break; |
| 7448 | } |
| 7449 | #endif |
| 7450 | |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 7451 | /* Opcode: Noop * * * * * |
| 7452 | ** |
| 7453 | ** Do nothing. This instruction is often useful as a jump |
| 7454 | ** destination. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 7455 | */ |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 7456 | /* |
| 7457 | ** The magic Explain opcode are only inserted when explain==2 (which |
| 7458 | ** is to say when the EXPLAIN QUERY PLAN syntax is used.) |
| 7459 | ** This opcode records information from the optimizer. It is the |
| 7460 | ** the same as a no-op. This opcodesnever appears in a real VM program. |
| 7461 | */ |
drh | 4031baf | 2018-05-28 17:31:20 +0000 | [diff] [blame] | 7462 | default: { /* This is really OP_Noop, OP_Explain */ |
drh | 13573c7 | 2010-01-12 17:04:07 +0000 | [diff] [blame] | 7463 | assert( pOp->opcode==OP_Noop || pOp->opcode==OP_Explain ); |
drh | 4031baf | 2018-05-28 17:31:20 +0000 | [diff] [blame] | 7464 | |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 7465 | break; |
| 7466 | } |
| 7467 | |
| 7468 | /***************************************************************************** |
| 7469 | ** The cases of the switch statement above this line should all be indented |
| 7470 | ** by 6 spaces. But the left-most 6 spaces have been removed to improve the |
| 7471 | ** readability. From this point on down, the normal indentation rules are |
| 7472 | ** restored. |
| 7473 | *****************************************************************************/ |
| 7474 | } |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 7475 | |
drh | 7b39686 | 2003-01-01 23:06:20 +0000 | [diff] [blame] | 7476 | #ifdef VDBE_PROFILE |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 7477 | { |
drh | 35043cc | 2018-02-12 20:27:34 +0000 | [diff] [blame] | 7478 | u64 endTime = sqlite3NProfileCnt ? sqlite3NProfileCnt : sqlite3Hwtime(); |
drh | 6dc4148 | 2015-04-16 17:31:02 +0000 | [diff] [blame] | 7479 | if( endTime>start ) pOrigOp->cycles += endTime - start; |
| 7480 | pOrigOp->cnt++; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 7481 | } |
drh | 7b39686 | 2003-01-01 23:06:20 +0000 | [diff] [blame] | 7482 | #endif |
| 7483 | |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 7484 | /* The following code adds nothing to the actual functionality |
| 7485 | ** of the program. It is only here for testing and debugging. |
| 7486 | ** On the other hand, it does burn CPU cycles every time through |
| 7487 | ** the evaluator loop. So we can leave it out when NDEBUG is defined. |
| 7488 | */ |
| 7489 | #ifndef NDEBUG |
drh | 6dc4148 | 2015-04-16 17:31:02 +0000 | [diff] [blame] | 7490 | assert( pOp>=&aOp[-1] && pOp<&aOp[p->nOp-1] ); |
drh | ae7e151 | 2007-05-02 16:51:59 +0000 | [diff] [blame] | 7491 | |
drh | cf1023c | 2007-05-08 20:59:49 +0000 | [diff] [blame] | 7492 | #ifdef SQLITE_DEBUG |
drh | 84e55a8 | 2013-11-13 17:58:23 +0000 | [diff] [blame] | 7493 | if( db->flags & SQLITE_VdbeTrace ){ |
drh | 7cc84c2 | 2016-04-11 13:36:42 +0000 | [diff] [blame] | 7494 | u8 opProperty = sqlite3OpcodeProperty[pOrigOp->opcode]; |
drh | 84e55a8 | 2013-11-13 17:58:23 +0000 | [diff] [blame] | 7495 | if( rc!=0 ) printf("rc=%d\n",rc); |
drh | 7cc84c2 | 2016-04-11 13:36:42 +0000 | [diff] [blame] | 7496 | if( opProperty & (OPFLG_OUT2) ){ |
drh | 6dc4148 | 2015-04-16 17:31:02 +0000 | [diff] [blame] | 7497 | registerTrace(pOrigOp->p2, &aMem[pOrigOp->p2]); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 7498 | } |
drh | 7cc84c2 | 2016-04-11 13:36:42 +0000 | [diff] [blame] | 7499 | if( opProperty & OPFLG_OUT3 ){ |
drh | 6dc4148 | 2015-04-16 17:31:02 +0000 | [diff] [blame] | 7500 | registerTrace(pOrigOp->p3, &aMem[pOrigOp->p3]); |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 7501 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 7502 | } |
danielk1977 | b5402fb | 2005-01-12 07:15:04 +0000 | [diff] [blame] | 7503 | #endif /* SQLITE_DEBUG */ |
| 7504 | #endif /* NDEBUG */ |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 7505 | } /* The end of the for(;;) loop the loops through opcodes */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 7506 | |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 7507 | /* If we reach this point, it means that execution is finished with |
| 7508 | ** an error of some kind. |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 7509 | */ |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 7510 | abort_due_to_error: |
| 7511 | if( db->mallocFailed ) rc = SQLITE_NOMEM_BKPT; |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 7512 | assert( rc ); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 7513 | if( p->zErrMsg==0 && rc!=SQLITE_IOERR_NOMEM ){ |
| 7514 | sqlite3VdbeError(p, "%s", sqlite3ErrStr(rc)); |
| 7515 | } |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 7516 | p->rc = rc; |
drh | f68521c | 2016-03-21 12:28:02 +0000 | [diff] [blame] | 7517 | sqlite3SystemError(db, rc); |
drh | a64fa91 | 2010-03-04 00:53:32 +0000 | [diff] [blame] | 7518 | testcase( sqlite3GlobalConfig.xLog!=0 ); |
| 7519 | sqlite3_log(rc, "statement aborts at %d: [%s] %s", |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 7520 | (int)(pOp - aOp), p->zSql, p->zErrMsg); |
drh | 92f02c3 | 2004-09-02 14:57:08 +0000 | [diff] [blame] | 7521 | sqlite3VdbeHalt(p); |
drh | 4a642b6 | 2016-02-05 01:55:27 +0000 | [diff] [blame] | 7522 | if( rc==SQLITE_IOERR_NOMEM ) sqlite3OomFault(db); |
danielk1977 | 7eaabcd | 2008-07-07 14:56:56 +0000 | [diff] [blame] | 7523 | rc = SQLITE_ERROR; |
drh | cdf011d | 2011-04-04 21:25:28 +0000 | [diff] [blame] | 7524 | if( resetSchemaOnFault>0 ){ |
drh | 81028a4 | 2012-05-15 18:28:27 +0000 | [diff] [blame] | 7525 | sqlite3ResetOneSchema(db, resetSchemaOnFault-1); |
drh | bdaec52 | 2011-04-04 00:14:43 +0000 | [diff] [blame] | 7526 | } |
drh | 900b31e | 2007-08-28 02:27:51 +0000 | [diff] [blame] | 7527 | |
| 7528 | /* This is the only way out of this procedure. We have to |
| 7529 | ** release the mutexes on btrees that were acquired at the |
| 7530 | ** top. */ |
| 7531 | vdbe_return: |
drh | 77dfd5b | 2013-08-19 11:15:48 +0000 | [diff] [blame] | 7532 | testcase( nVmStep>0 ); |
drh | 9b47ee3 | 2013-08-20 03:13:51 +0000 | [diff] [blame] | 7533 | p->aCounter[SQLITE_STMTSTATUS_VM_STEP] += (int)nVmStep; |
drh | bdaec52 | 2011-04-04 00:14:43 +0000 | [diff] [blame] | 7534 | sqlite3VdbeLeave(p); |
dan | 83f0ab8 | 2016-01-29 18:04:31 +0000 | [diff] [blame] | 7535 | assert( rc!=SQLITE_OK || nExtraDelete==0 |
| 7536 | || sqlite3_strlike("DELETE%",p->zSql,0)!=0 |
| 7537 | ); |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 7538 | return rc; |
| 7539 | |
drh | 023ae03 | 2007-05-08 12:12:16 +0000 | [diff] [blame] | 7540 | /* Jump to here if a string or blob larger than SQLITE_MAX_LENGTH |
| 7541 | ** is encountered. |
| 7542 | */ |
| 7543 | too_big: |
drh | 22c17b8 | 2015-05-15 04:13:15 +0000 | [diff] [blame] | 7544 | sqlite3VdbeError(p, "string or blob too big"); |
drh | 023ae03 | 2007-05-08 12:12:16 +0000 | [diff] [blame] | 7545 | rc = SQLITE_TOOBIG; |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 7546 | goto abort_due_to_error; |
drh | 023ae03 | 2007-05-08 12:12:16 +0000 | [diff] [blame] | 7547 | |
drh | 98640a3 | 2007-06-07 19:08:32 +0000 | [diff] [blame] | 7548 | /* Jump to here if a malloc() fails. |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 7549 | */ |
| 7550 | no_mem: |
drh | 4a642b6 | 2016-02-05 01:55:27 +0000 | [diff] [blame] | 7551 | sqlite3OomFault(db); |
drh | 22c17b8 | 2015-05-15 04:13:15 +0000 | [diff] [blame] | 7552 | sqlite3VdbeError(p, "out of memory"); |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 7553 | rc = SQLITE_NOMEM_BKPT; |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 7554 | goto abort_due_to_error; |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 7555 | |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 7556 | /* Jump to here if the sqlite3_interrupt() API sets the interrupt |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 7557 | ** flag. |
| 7558 | */ |
| 7559 | abort_due_to_interrupt: |
drh | 881feaa | 2006-07-26 01:39:30 +0000 | [diff] [blame] | 7560 | assert( db->u1.isInterrupted ); |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 7561 | rc = db->mallocFailed ? SQLITE_NOMEM_BKPT : SQLITE_INTERRUPT; |
danielk1977 | 026d270 | 2004-06-14 13:14:59 +0000 | [diff] [blame] | 7562 | p->rc = rc; |
drh | 22c17b8 | 2015-05-15 04:13:15 +0000 | [diff] [blame] | 7563 | sqlite3VdbeError(p, "%s", sqlite3ErrStr(rc)); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 7564 | goto abort_due_to_error; |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 7565 | } |