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 | |
drh | 52f11b8 | 2020-01-02 13:26:49 +0000 | [diff] [blame] | 120 | #ifdef SQLITE_DEBUG |
| 121 | /* This routine provides a convenient place to set a breakpoint during |
| 122 | ** tracing with PRAGMA vdbe_trace=on. The breakpoint fires right after |
| 123 | ** each opcode is printed. Variables "pc" (program counter) and pOp are |
| 124 | ** available to add conditionals to the breakpoint. GDB example: |
| 125 | ** |
| 126 | ** break test_trace_breakpoint if pc=22 |
| 127 | ** |
| 128 | ** Other useful labels for breakpoints include: |
| 129 | ** test_addop_breakpoint(pc,pOp) |
| 130 | ** sqlite3CorruptError(lineno) |
| 131 | ** sqlite3MisuseError(lineno) |
| 132 | ** sqlite3CantopenError(lineno) |
| 133 | */ |
drh | 22e95fb | 2020-01-02 14:42:42 +0000 | [diff] [blame] | 134 | static void test_trace_breakpoint(int pc, Op *pOp, Vdbe *v){ |
drh | 52f11b8 | 2020-01-02 13:26:49 +0000 | [diff] [blame] | 135 | static int n = 0; |
| 136 | n++; |
| 137 | } |
| 138 | #endif |
| 139 | |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 140 | /* |
drh | 5655c54 | 2014-02-19 19:14:34 +0000 | [diff] [blame] | 141 | ** Invoke the VDBE coverage callback, if that callback is defined. This |
| 142 | ** feature is used for test suite validation only and does not appear an |
| 143 | ** production builds. |
| 144 | ** |
drh | c906533 | 2019-04-01 14:01:21 +0000 | [diff] [blame] | 145 | ** M is the type of branch. I is the direction taken for this instance of |
| 146 | ** the branch. |
| 147 | ** |
| 148 | ** M: 2 - two-way branch (I=0: fall-thru 1: jump ) |
| 149 | ** 3 - two-way + NULL (I=0: fall-thru 1: jump 2: NULL ) |
| 150 | ** 4 - OP_Jump (I=0: jump p1 1: jump p2 2: jump p3) |
| 151 | ** |
| 152 | ** In other words, if M is 2, then I is either 0 (for fall-through) or |
| 153 | ** 1 (for when the branch is taken). If M is 3, the I is 0 for an |
| 154 | ** ordinary fall-through, I is 1 if the branch was taken, and I is 2 |
| 155 | ** if the result of comparison is NULL. For M=3, I=2 the jump may or |
| 156 | ** may not be taken, depending on the SQLITE_JUMPIFNULL flags in p5. |
| 157 | ** When M is 4, that means that an OP_Jump is being run. I is 0, 1, or 2 |
| 158 | ** depending on if the operands are less than, equal, or greater than. |
drh | 4336b0e | 2014-08-05 00:53:51 +0000 | [diff] [blame] | 159 | ** |
| 160 | ** iSrcLine is the source code line (from the __LINE__ macro) that |
drh | 7083a48 | 2018-07-10 16:04:04 +0000 | [diff] [blame] | 161 | ** generated the VDBE instruction combined with flag bits. The source |
| 162 | ** code line number is in the lower 24 bits of iSrcLine and the upper |
| 163 | ** 8 bytes are flags. The lower three bits of the flags indicate |
| 164 | ** values for I that should never occur. For example, if the branch is |
| 165 | ** always taken, the flags should be 0x05 since the fall-through and |
| 166 | ** alternate branch are never taken. If a branch is never taken then |
| 167 | ** flags should be 0x06 since only the fall-through approach is allowed. |
| 168 | ** |
drh | c906533 | 2019-04-01 14:01:21 +0000 | [diff] [blame] | 169 | ** Bit 0x08 of the flags indicates an OP_Jump opcode that is only |
drh | 7083a48 | 2018-07-10 16:04:04 +0000 | [diff] [blame] | 170 | ** interested in equal or not-equal. In other words, I==0 and I==2 |
drh | c906533 | 2019-04-01 14:01:21 +0000 | [diff] [blame] | 171 | ** should be treated as equivalent |
drh | 7083a48 | 2018-07-10 16:04:04 +0000 | [diff] [blame] | 172 | ** |
| 173 | ** Since only a line number is retained, not the filename, this macro |
| 174 | ** only works for amalgamation builds. But that is ok, since these macros |
| 175 | ** should be no-ops except for special builds used to measure test coverage. |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 176 | */ |
| 177 | #if !defined(SQLITE_VDBE_COVERAGE) |
| 178 | # define VdbeBranchTaken(I,M) |
| 179 | #else |
drh | 5655c54 | 2014-02-19 19:14:34 +0000 | [diff] [blame] | 180 | # define VdbeBranchTaken(I,M) vdbeTakeBranch(pOp->iSrcLine,I,M) |
drh | 7083a48 | 2018-07-10 16:04:04 +0000 | [diff] [blame] | 181 | static void vdbeTakeBranch(u32 iSrcLine, u8 I, u8 M){ |
| 182 | u8 mNever; |
| 183 | assert( I<=2 ); /* 0: fall through, 1: taken, 2: alternate taken */ |
| 184 | assert( M<=4 ); /* 2: two-way branch, 3: three-way branch, 4: OP_Jump */ |
| 185 | assert( I<M ); /* I can only be 2 if M is 3 or 4 */ |
| 186 | /* Transform I from a integer [0,1,2] into a bitmask of [1,2,4] */ |
| 187 | I = 1<<I; |
| 188 | /* The upper 8 bits of iSrcLine are flags. The lower three bits of |
| 189 | ** the flags indicate directions that the branch can never go. If |
| 190 | ** a branch really does go in one of those directions, assert right |
| 191 | ** away. */ |
| 192 | mNever = iSrcLine >> 24; |
| 193 | assert( (I & mNever)==0 ); |
| 194 | if( sqlite3GlobalConfig.xVdbeBranch==0 ) return; /*NO_TEST*/ |
drh | c906533 | 2019-04-01 14:01:21 +0000 | [diff] [blame] | 195 | /* Invoke the branch coverage callback with three arguments: |
| 196 | ** iSrcLine - the line number of the VdbeCoverage() macro, with |
| 197 | ** flags removed. |
| 198 | ** I - Mask of bits 0x07 indicating which cases are are |
| 199 | ** fulfilled by this instance of the jump. 0x01 means |
| 200 | ** fall-thru, 0x02 means taken, 0x04 means NULL. Any |
| 201 | ** impossible cases (ex: if the comparison is never NULL) |
| 202 | ** are filled in automatically so that the coverage |
| 203 | ** measurement logic does not flag those impossible cases |
| 204 | ** as missed coverage. |
| 205 | ** M - Type of jump. Same as M argument above |
| 206 | */ |
drh | 7083a48 | 2018-07-10 16:04:04 +0000 | [diff] [blame] | 207 | I |= mNever; |
| 208 | if( M==2 ) I |= 0x04; |
| 209 | if( M==4 ){ |
| 210 | I |= 0x08; |
drh | 6ccbd27 | 2018-07-10 17:10:44 +0000 | [diff] [blame] | 211 | if( (mNever&0x08)!=0 && (I&0x05)!=0) I |= 0x05; /*NO_TEST*/ |
drh | 5655c54 | 2014-02-19 19:14:34 +0000 | [diff] [blame] | 212 | } |
drh | 7083a48 | 2018-07-10 16:04:04 +0000 | [diff] [blame] | 213 | sqlite3GlobalConfig.xVdbeBranch(sqlite3GlobalConfig.pVdbeBranchArg, |
| 214 | iSrcLine&0xffffff, I, M); |
drh | 5655c54 | 2014-02-19 19:14:34 +0000 | [diff] [blame] | 215 | } |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 216 | #endif |
| 217 | |
| 218 | /* |
danielk1977 | bd7e460 | 2004-05-24 07:34:48 +0000 | [diff] [blame] | 219 | ** An ephemeral string value (signified by the MEM_Ephem flag) contains |
| 220 | ** a pointer to a dynamically allocated string where some other entity |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 221 | ** is responsible for deallocating that string. Because the register |
| 222 | ** does not control the string, it might be deleted without the register |
| 223 | ** knowing it. |
danielk1977 | bd7e460 | 2004-05-24 07:34:48 +0000 | [diff] [blame] | 224 | ** |
| 225 | ** This routine converts an ephemeral string into a dynamically allocated |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 226 | ** string that the register itself controls. In other words, it |
drh | c91b2fd | 2014-03-01 18:13:23 +0000 | [diff] [blame] | 227 | ** converts an MEM_Ephem string into a string with P.z==P.zMalloc. |
danielk1977 | bd7e460 | 2004-05-24 07:34:48 +0000 | [diff] [blame] | 228 | */ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 229 | #define Deephemeralize(P) \ |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 230 | if( ((P)->flags&MEM_Ephem)!=0 \ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 231 | && sqlite3VdbeMemMakeWriteable(P) ){ goto no_mem;} |
danielk1977 | 93d4675 | 2004-05-23 13:30:58 +0000 | [diff] [blame] | 232 | |
dan | 689ab89 | 2011-08-12 15:02:00 +0000 | [diff] [blame] | 233 | /* Return true if the cursor was opened using the OP_OpenSorter opcode. */ |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 234 | #define isSorter(x) ((x)->eCurType==CURTYPE_SORTER) |
danielk1977 | 8a6b541 | 2004-05-24 07:04:25 +0000 | [diff] [blame] | 235 | |
| 236 | /* |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 237 | ** Allocate VdbeCursor number iCur. Return a pointer to it. Return NULL |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 238 | ** if we run out of memory. |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 239 | */ |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 240 | static VdbeCursor *allocateCursor( |
| 241 | Vdbe *p, /* The virtual machine */ |
| 242 | int iCur, /* Index of the new VdbeCursor */ |
danielk1977 | d336e22 | 2009-02-20 10:58:41 +0000 | [diff] [blame] | 243 | int nField, /* Number of fields in the table or index */ |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 244 | u8 eCurType /* Type of the new cursor */ |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 245 | ){ |
| 246 | /* 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] | 247 | ** required for this VdbeCursor structure. It is convenient to use a |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 248 | ** vdbe memory cell to manage the memory allocation required for a |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 249 | ** VdbeCursor structure for the following reasons: |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 250 | ** |
| 251 | ** * Sometimes cursor numbers are used for a couple of different |
| 252 | ** purposes in a vdbe program. The different uses might require |
| 253 | ** different sized allocations. Memory cells provide growable |
| 254 | ** allocations. |
| 255 | ** |
| 256 | ** * When using ENABLE_MEMORY_MANAGEMENT, memory cell buffers can |
| 257 | ** be freed lazily via the sqlite3_release_memory() API. This |
| 258 | ** minimizes the number of malloc calls made by the system. |
| 259 | ** |
drh | 3cdce92 | 2016-03-21 00:30:40 +0000 | [diff] [blame] | 260 | ** 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] | 261 | ** the top of the register space. Cursor 1 is at Mem[p->nMem-1]. |
| 262 | ** Cursor 2 is at Mem[p->nMem-2]. And so forth. |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 263 | */ |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 264 | Mem *pMem = iCur>0 ? &p->aMem[p->nMem-iCur] : p->aMem; |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 265 | |
danielk1977 | 5f09613 | 2008-03-28 15:44:09 +0000 | [diff] [blame] | 266 | int nByte; |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 267 | VdbeCursor *pCx = 0; |
danielk1977 | 5f09613 | 2008-03-28 15:44:09 +0000 | [diff] [blame] | 268 | nByte = |
drh | cf6e3fd | 2022-04-01 18:45:11 +0000 | [diff] [blame] | 269 | ROUND8P(sizeof(VdbeCursor)) + 2*sizeof(u32)*nField + |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 270 | (eCurType==CURTYPE_BTREE?sqlite3BtreeCursorSize():0); |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 271 | |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 272 | assert( iCur>=0 && iCur<p->nCursor ); |
drh | a3fa140 | 2016-04-29 02:55:05 +0000 | [diff] [blame] | 273 | if( p->apCsr[iCur] ){ /*OPTIMIZATION-IF-FALSE*/ |
drh | 473571b | 2022-04-01 18:19:04 +0000 | [diff] [blame] | 274 | sqlite3VdbeFreeCursorNN(p, p->apCsr[iCur]); |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 275 | p->apCsr[iCur] = 0; |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 276 | } |
drh | 2454e4a | 2021-05-15 19:36:36 +0000 | [diff] [blame] | 277 | |
| 278 | /* There used to be a call to sqlite3VdbeMemClearAndResize() to make sure |
| 279 | ** the pMem used to hold space for the cursor has enough storage available |
| 280 | ** in pMem->zMalloc. But for the special case of the aMem[] entries used |
| 281 | ** to hold cursors, it is faster to in-line the logic. */ |
| 282 | assert( pMem->flags==MEM_Undefined ); |
| 283 | assert( (pMem->flags & MEM_Dyn)==0 ); |
| 284 | assert( pMem->szMalloc==0 || pMem->z==pMem->zMalloc ); |
| 285 | if( pMem->szMalloc<nByte ){ |
| 286 | if( pMem->szMalloc>0 ){ |
| 287 | sqlite3DbFreeNN(pMem->db, pMem->zMalloc); |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 288 | } |
drh | 2454e4a | 2021-05-15 19:36:36 +0000 | [diff] [blame] | 289 | pMem->z = pMem->zMalloc = sqlite3DbMallocRaw(pMem->db, nByte); |
| 290 | if( pMem->zMalloc==0 ){ |
| 291 | pMem->szMalloc = 0; |
| 292 | return 0; |
| 293 | } |
| 294 | pMem->szMalloc = nByte; |
| 295 | } |
| 296 | |
| 297 | p->apCsr[iCur] = pCx = (VdbeCursor*)pMem->zMalloc; |
| 298 | memset(pCx, 0, offsetof(VdbeCursor,pAltCursor)); |
| 299 | pCx->eCurType = eCurType; |
drh | 2454e4a | 2021-05-15 19:36:36 +0000 | [diff] [blame] | 300 | pCx->nField = nField; |
| 301 | pCx->aOffset = &pCx->aType[nField]; |
| 302 | if( eCurType==CURTYPE_BTREE ){ |
| 303 | pCx->uc.pCursor = (BtCursor*) |
drh | cf6e3fd | 2022-04-01 18:45:11 +0000 | [diff] [blame] | 304 | &pMem->z[ROUND8P(sizeof(VdbeCursor))+2*sizeof(u32)*nField]; |
drh | 2454e4a | 2021-05-15 19:36:36 +0000 | [diff] [blame] | 305 | sqlite3BtreeCursorZero(pCx->uc.pCursor); |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 +0000 | [diff] [blame] | 306 | } |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 307 | return pCx; |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 308 | } |
| 309 | |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 310 | /* |
drh | 8a3884e | 2019-05-29 21:18:27 +0000 | [diff] [blame] | 311 | ** The string in pRec is known to look like an integer and to have a |
| 312 | ** floating point value of rValue. Return true and set *piValue to the |
| 313 | ** integer value if the string is in range to be an integer. Otherwise, |
| 314 | ** return false. |
| 315 | */ |
| 316 | static int alsoAnInt(Mem *pRec, double rValue, i64 *piValue){ |
| 317 | i64 iValue = (double)rValue; |
| 318 | if( sqlite3RealSameAsInt(rValue,iValue) ){ |
drh | c285ded | 2019-06-10 18:33:16 +0000 | [diff] [blame] | 319 | *piValue = iValue; |
| 320 | return 1; |
drh | 8a3884e | 2019-05-29 21:18:27 +0000 | [diff] [blame] | 321 | } |
| 322 | return 0==sqlite3Atoi64(pRec->z, piValue, pRec->n, pRec->enc); |
| 323 | } |
| 324 | |
| 325 | /* |
drh | 29d7210 | 2006-02-09 22:13:41 +0000 | [diff] [blame] | 326 | ** Try to convert a value into a numeric representation if we can |
| 327 | ** do so without loss of information. In other words, if the string |
| 328 | ** looks like a number, convert it into a number. If it does not |
| 329 | ** look like a number, leave it alone. |
drh | bd9507c | 2014-08-23 17:21:37 +0000 | [diff] [blame] | 330 | ** |
| 331 | ** If the bTryForInt flag is true, then extra effort is made to give |
| 332 | ** an integer representation. Strings that look like floating point |
| 333 | ** values but which have no fractional component (example: '48.00') |
| 334 | ** will have a MEM_Int representation when bTryForInt is true. |
| 335 | ** |
| 336 | ** If bTryForInt is false, then if the input string contains a decimal |
| 337 | ** point or exponential notation, the result is only MEM_Real, even |
| 338 | ** if there is an exact integer representation of the quantity. |
drh | 29d7210 | 2006-02-09 22:13:41 +0000 | [diff] [blame] | 339 | */ |
drh | bd9507c | 2014-08-23 17:21:37 +0000 | [diff] [blame] | 340 | static void applyNumericAffinity(Mem *pRec, int bTryForInt){ |
drh | 975b4c6 | 2014-07-26 16:47:23 +0000 | [diff] [blame] | 341 | double rValue; |
drh | 975b4c6 | 2014-07-26 16:47:23 +0000 | [diff] [blame] | 342 | u8 enc = pRec->enc; |
drh | 8a3884e | 2019-05-29 21:18:27 +0000 | [diff] [blame] | 343 | int rc; |
drh | 169f077 | 2019-05-02 21:36:26 +0000 | [diff] [blame] | 344 | assert( (pRec->flags & (MEM_Str|MEM_Int|MEM_Real|MEM_IntReal))==MEM_Str ); |
drh | 8a3884e | 2019-05-29 21:18:27 +0000 | [diff] [blame] | 345 | rc = sqlite3AtoF(pRec->z, &rValue, pRec->n, enc); |
drh | 9a27822 | 2019-06-07 22:26:08 +0000 | [diff] [blame] | 346 | if( rc<=0 ) return; |
drh | 8a3884e | 2019-05-29 21:18:27 +0000 | [diff] [blame] | 347 | if( rc==1 && alsoAnInt(pRec, rValue, &pRec->u.i) ){ |
drh | 975b4c6 | 2014-07-26 16:47:23 +0000 | [diff] [blame] | 348 | pRec->flags |= MEM_Int; |
| 349 | }else{ |
drh | 74eaba4 | 2014-09-18 17:52:15 +0000 | [diff] [blame] | 350 | pRec->u.r = rValue; |
drh | 975b4c6 | 2014-07-26 16:47:23 +0000 | [diff] [blame] | 351 | pRec->flags |= MEM_Real; |
drh | bd9507c | 2014-08-23 17:21:37 +0000 | [diff] [blame] | 352 | if( bTryForInt ) sqlite3VdbeIntegerAffinity(pRec); |
drh | 29d7210 | 2006-02-09 22:13:41 +0000 | [diff] [blame] | 353 | } |
drh | 06b3bd5 | 2018-02-01 01:13:33 +0000 | [diff] [blame] | 354 | /* TEXT->NUMERIC is many->one. Hence, it is important to invalidate the |
| 355 | ** string representation after computing a numeric equivalent, because the |
| 356 | ** string representation might not be the canonical representation for the |
| 357 | ** numeric value. Ticket [343634942dd54ab57b7024] 2018-01-31. */ |
| 358 | pRec->flags &= ~MEM_Str; |
drh | 29d7210 | 2006-02-09 22:13:41 +0000 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | /* |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 362 | ** Processing is determine by the affinity parameter: |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 363 | ** |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 364 | ** SQLITE_AFF_INTEGER: |
| 365 | ** SQLITE_AFF_REAL: |
| 366 | ** SQLITE_AFF_NUMERIC: |
| 367 | ** Try to convert pRec to an integer representation or a |
| 368 | ** floating-point representation if an integer representation |
| 369 | ** is not possible. Note that the integer representation is |
| 370 | ** always preferred, even if the affinity is REAL, because |
| 371 | ** an integer representation is more space efficient on disk. |
| 372 | ** |
| 373 | ** SQLITE_AFF_TEXT: |
| 374 | ** Convert pRec to a text representation. |
| 375 | ** |
drh | 05883a3 | 2015-06-02 15:32:08 +0000 | [diff] [blame] | 376 | ** SQLITE_AFF_BLOB: |
drh | 96fb16e | 2019-08-06 14:37:24 +0000 | [diff] [blame] | 377 | ** SQLITE_AFF_NONE: |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 378 | ** No-op. pRec is unchanged. |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 379 | */ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 380 | static void applyAffinity( |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 381 | Mem *pRec, /* The value to apply affinity to */ |
| 382 | char affinity, /* The affinity to be applied */ |
| 383 | u8 enc /* Use this text encoding */ |
| 384 | ){ |
drh | 7ea31cc | 2014-09-18 14:36:00 +0000 | [diff] [blame] | 385 | if( affinity>=SQLITE_AFF_NUMERIC ){ |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 386 | assert( affinity==SQLITE_AFF_INTEGER || affinity==SQLITE_AFF_REAL |
| 387 | || affinity==SQLITE_AFF_NUMERIC ); |
drh | a3fa140 | 2016-04-29 02:55:05 +0000 | [diff] [blame] | 388 | if( (pRec->flags & MEM_Int)==0 ){ /*OPTIMIZATION-IF-FALSE*/ |
drh | bd9507c | 2014-08-23 17:21:37 +0000 | [diff] [blame] | 389 | if( (pRec->flags & MEM_Real)==0 ){ |
drh | 11a6eee | 2014-09-19 22:01:54 +0000 | [diff] [blame] | 390 | if( pRec->flags & MEM_Str ) applyNumericAffinity(pRec,1); |
drh | bd9507c | 2014-08-23 17:21:37 +0000 | [diff] [blame] | 391 | }else{ |
| 392 | sqlite3VdbeIntegerAffinity(pRec); |
| 393 | } |
drh | 17c4029 | 2004-07-21 02:53:29 +0000 | [diff] [blame] | 394 | } |
drh | 7ea31cc | 2014-09-18 14:36:00 +0000 | [diff] [blame] | 395 | }else if( affinity==SQLITE_AFF_TEXT ){ |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 396 | /* Only attempt the conversion to TEXT if there is an integer or real |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 397 | ** representation (blob and NULL do not get converted) but no string |
drh | a3fa140 | 2016-04-29 02:55:05 +0000 | [diff] [blame] | 398 | ** representation. It would be harmless to repeat the conversion if |
| 399 | ** there is already a string rep, but it is pointless to waste those |
| 400 | ** CPU cycles. */ |
| 401 | if( 0==(pRec->flags&MEM_Str) ){ /*OPTIMIZATION-IF-FALSE*/ |
drh | 169f077 | 2019-05-02 21:36:26 +0000 | [diff] [blame] | 402 | if( (pRec->flags&(MEM_Real|MEM_Int|MEM_IntReal)) ){ |
drh | 3242c69 | 2019-05-04 01:29:13 +0000 | [diff] [blame] | 403 | testcase( pRec->flags & MEM_Int ); |
| 404 | testcase( pRec->flags & MEM_Real ); |
| 405 | testcase( pRec->flags & MEM_IntReal ); |
drh | a3fa140 | 2016-04-29 02:55:05 +0000 | [diff] [blame] | 406 | sqlite3VdbeMemStringify(pRec, enc, 1); |
| 407 | } |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 408 | } |
drh | 169f077 | 2019-05-02 21:36:26 +0000 | [diff] [blame] | 409 | pRec->flags &= ~(MEM_Real|MEM_Int|MEM_IntReal); |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 410 | } |
| 411 | } |
| 412 | |
danielk1977 | aee18ef | 2005-03-09 12:26:50 +0000 | [diff] [blame] | 413 | /* |
drh | 29d7210 | 2006-02-09 22:13:41 +0000 | [diff] [blame] | 414 | ** Try to convert the type of a function argument or a result column |
| 415 | ** into a numeric representation. Use either INTEGER or REAL whichever |
| 416 | ** is appropriate. But only do the conversion if it is possible without |
| 417 | ** loss of information and return the revised type of the argument. |
drh | 29d7210 | 2006-02-09 22:13:41 +0000 | [diff] [blame] | 418 | */ |
| 419 | int sqlite3_value_numeric_type(sqlite3_value *pVal){ |
drh | 1b27b8c | 2014-02-10 03:21:57 +0000 | [diff] [blame] | 420 | int eType = sqlite3_value_type(pVal); |
| 421 | if( eType==SQLITE_TEXT ){ |
| 422 | Mem *pMem = (Mem*)pVal; |
drh | bd9507c | 2014-08-23 17:21:37 +0000 | [diff] [blame] | 423 | applyNumericAffinity(pMem, 0); |
drh | 1b27b8c | 2014-02-10 03:21:57 +0000 | [diff] [blame] | 424 | eType = sqlite3_value_type(pVal); |
drh | e5a8a1d | 2010-11-18 12:31:24 +0000 | [diff] [blame] | 425 | } |
drh | 1b27b8c | 2014-02-10 03:21:57 +0000 | [diff] [blame] | 426 | return eType; |
drh | 29d7210 | 2006-02-09 22:13:41 +0000 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | /* |
danielk1977 | aee18ef | 2005-03-09 12:26:50 +0000 | [diff] [blame] | 430 | ** Exported version of applyAffinity(). This one works on sqlite3_value*, |
| 431 | ** not the internal Mem* type. |
| 432 | */ |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 433 | void sqlite3ValueApplyAffinity( |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 434 | sqlite3_value *pVal, |
| 435 | u8 affinity, |
| 436 | u8 enc |
| 437 | ){ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 438 | applyAffinity((Mem *)pVal, affinity, enc); |
danielk1977 | aee18ef | 2005-03-09 12:26:50 +0000 | [diff] [blame] | 439 | } |
| 440 | |
drh | 3d1d90a | 2014-03-24 15:00:15 +0000 | [diff] [blame] | 441 | /* |
drh | f1a89ed | 2014-08-23 17:41:15 +0000 | [diff] [blame] | 442 | ** pMem currently only holds a string type (or maybe a BLOB that we can |
| 443 | ** interpret as a string if we want to). Compute its corresponding |
drh | 74eaba4 | 2014-09-18 17:52:15 +0000 | [diff] [blame] | 444 | ** 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] | 445 | ** accordingly. |
| 446 | */ |
| 447 | static u16 SQLITE_NOINLINE computeNumericType(Mem *pMem){ |
drh | 9a27822 | 2019-06-07 22:26:08 +0000 | [diff] [blame] | 448 | int rc; |
| 449 | sqlite3_int64 ix; |
drh | 169f077 | 2019-05-02 21:36:26 +0000 | [diff] [blame] | 450 | assert( (pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal))==0 ); |
drh | f1a89ed | 2014-08-23 17:41:15 +0000 | [diff] [blame] | 451 | assert( (pMem->flags & (MEM_Str|MEM_Blob))!=0 ); |
drh | 1fd1cc4 | 2021-04-10 15:34:30 +0000 | [diff] [blame] | 452 | if( ExpandBlob(pMem) ){ |
| 453 | pMem->u.i = 0; |
| 454 | return MEM_Int; |
| 455 | } |
drh | 9a27822 | 2019-06-07 22:26:08 +0000 | [diff] [blame] | 456 | rc = sqlite3AtoF(pMem->z, &pMem->u.r, pMem->n, pMem->enc); |
| 457 | if( rc<=0 ){ |
| 458 | if( rc==0 && sqlite3Atoi64(pMem->z, &ix, pMem->n, pMem->enc)<=1 ){ |
| 459 | pMem->u.i = ix; |
| 460 | return MEM_Int; |
| 461 | }else{ |
| 462 | return MEM_Real; |
| 463 | } |
| 464 | }else if( rc==1 && sqlite3Atoi64(pMem->z, &ix, pMem->n, pMem->enc)==0 ){ |
| 465 | pMem->u.i = ix; |
drh | f1a89ed | 2014-08-23 17:41:15 +0000 | [diff] [blame] | 466 | return MEM_Int; |
| 467 | } |
| 468 | return MEM_Real; |
| 469 | } |
| 470 | |
| 471 | /* |
drh | 3d1d90a | 2014-03-24 15:00:15 +0000 | [diff] [blame] | 472 | ** Return the numeric type for pMem, either MEM_Int or MEM_Real or both or |
| 473 | ** none. |
| 474 | ** |
| 475 | ** Unlike applyNumericAffinity(), this routine does not modify pMem->flags. |
drh | 74eaba4 | 2014-09-18 17:52:15 +0000 | [diff] [blame] | 476 | ** But it does set pMem->u.r and pMem->u.i appropriately. |
drh | 3d1d90a | 2014-03-24 15:00:15 +0000 | [diff] [blame] | 477 | */ |
| 478 | static u16 numericType(Mem *pMem){ |
drh | 169f077 | 2019-05-02 21:36:26 +0000 | [diff] [blame] | 479 | if( pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal) ){ |
drh | 3242c69 | 2019-05-04 01:29:13 +0000 | [diff] [blame] | 480 | testcase( pMem->flags & MEM_Int ); |
| 481 | testcase( pMem->flags & MEM_Real ); |
| 482 | testcase( pMem->flags & MEM_IntReal ); |
drh | 169f077 | 2019-05-02 21:36:26 +0000 | [diff] [blame] | 483 | return pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal); |
drh | 3d1d90a | 2014-03-24 15:00:15 +0000 | [diff] [blame] | 484 | } |
| 485 | if( pMem->flags & (MEM_Str|MEM_Blob) ){ |
drh | 3242c69 | 2019-05-04 01:29:13 +0000 | [diff] [blame] | 486 | testcase( pMem->flags & MEM_Str ); |
| 487 | testcase( pMem->flags & MEM_Blob ); |
drh | f1a89ed | 2014-08-23 17:41:15 +0000 | [diff] [blame] | 488 | return computeNumericType(pMem); |
drh | 3d1d90a | 2014-03-24 15:00:15 +0000 | [diff] [blame] | 489 | } |
| 490 | return 0; |
| 491 | } |
| 492 | |
danielk1977 | b5402fb | 2005-01-12 07:15:04 +0000 | [diff] [blame] | 493 | #ifdef SQLITE_DEBUG |
drh | b6f5452 | 2004-05-20 02:42:16 +0000 | [diff] [blame] | 494 | /* |
danielk1977 | ca6b291 | 2004-05-21 10:49:47 +0000 | [diff] [blame] | 495 | ** Write a nice string representation of the contents of cell pMem |
| 496 | ** into buffer zBuf, length nBuf. |
| 497 | */ |
drh | 5ca0632 | 2020-01-06 19:23:41 +0000 | [diff] [blame] | 498 | void sqlite3VdbeMemPrettyPrint(Mem *pMem, StrAccum *pStr){ |
danielk1977 | ca6b291 | 2004-05-21 10:49:47 +0000 | [diff] [blame] | 499 | int f = pMem->flags; |
drh | 5719628 | 2004-10-06 15:41:16 +0000 | [diff] [blame] | 500 | static const char *const encnames[] = {"(X)", "(8)", "(16LE)", "(16BE)"}; |
danielk1977 | ca6b291 | 2004-05-21 10:49:47 +0000 | [diff] [blame] | 501 | if( f&MEM_Blob ){ |
| 502 | int i; |
| 503 | char c; |
| 504 | if( f & MEM_Dyn ){ |
| 505 | c = 'z'; |
| 506 | assert( (f & (MEM_Static|MEM_Ephem))==0 ); |
| 507 | }else if( f & MEM_Static ){ |
| 508 | c = 't'; |
| 509 | assert( (f & (MEM_Dyn|MEM_Ephem))==0 ); |
| 510 | }else if( f & MEM_Ephem ){ |
| 511 | c = 'e'; |
| 512 | assert( (f & (MEM_Static|MEM_Dyn))==0 ); |
| 513 | }else{ |
| 514 | c = 's'; |
| 515 | } |
drh | ded33cc | 2020-01-08 11:36:30 +0000 | [diff] [blame] | 516 | sqlite3_str_appendf(pStr, "%cx[", c); |
drh | efb5f9a | 2019-08-30 21:52:13 +0000 | [diff] [blame] | 517 | for(i=0; i<25 && i<pMem->n; i++){ |
drh | 5ca0632 | 2020-01-06 19:23:41 +0000 | [diff] [blame] | 518 | sqlite3_str_appendf(pStr, "%02X", ((int)pMem->z[i] & 0xFF)); |
danielk1977 | ca6b291 | 2004-05-21 10:49:47 +0000 | [diff] [blame] | 519 | } |
drh | 5ca0632 | 2020-01-06 19:23:41 +0000 | [diff] [blame] | 520 | sqlite3_str_appendf(pStr, "|"); |
drh | efb5f9a | 2019-08-30 21:52:13 +0000 | [diff] [blame] | 521 | for(i=0; i<25 && i<pMem->n; i++){ |
danielk1977 | ca6b291 | 2004-05-21 10:49:47 +0000 | [diff] [blame] | 522 | char z = pMem->z[i]; |
drh | 5ca0632 | 2020-01-06 19:23:41 +0000 | [diff] [blame] | 523 | sqlite3_str_appendchar(pStr, 1, (z<32||z>126)?'.':z); |
danielk1977 | ca6b291 | 2004-05-21 10:49:47 +0000 | [diff] [blame] | 524 | } |
drh | 5ca0632 | 2020-01-06 19:23:41 +0000 | [diff] [blame] | 525 | sqlite3_str_appendf(pStr,"]"); |
drh | fdf972a | 2007-05-02 13:30:27 +0000 | [diff] [blame] | 526 | if( f & MEM_Zero ){ |
drh | 5ca0632 | 2020-01-06 19:23:41 +0000 | [diff] [blame] | 527 | sqlite3_str_appendf(pStr, "+%dz",pMem->u.nZero); |
drh | fdf972a | 2007-05-02 13:30:27 +0000 | [diff] [blame] | 528 | } |
danielk1977 | b1bc953 | 2004-05-22 03:05:33 +0000 | [diff] [blame] | 529 | }else if( f & MEM_Str ){ |
drh | 5ca0632 | 2020-01-06 19:23:41 +0000 | [diff] [blame] | 530 | int j; |
mistachkin | 5917117 | 2020-01-18 19:02:20 +0000 | [diff] [blame] | 531 | u8 c; |
danielk1977 | b1bc953 | 2004-05-22 03:05:33 +0000 | [diff] [blame] | 532 | if( f & MEM_Dyn ){ |
drh | 5ca0632 | 2020-01-06 19:23:41 +0000 | [diff] [blame] | 533 | c = 'z'; |
danielk1977 | b1bc953 | 2004-05-22 03:05:33 +0000 | [diff] [blame] | 534 | assert( (f & (MEM_Static|MEM_Ephem))==0 ); |
| 535 | }else if( f & MEM_Static ){ |
drh | 5ca0632 | 2020-01-06 19:23:41 +0000 | [diff] [blame] | 536 | c = 't'; |
danielk1977 | b1bc953 | 2004-05-22 03:05:33 +0000 | [diff] [blame] | 537 | assert( (f & (MEM_Dyn|MEM_Ephem))==0 ); |
| 538 | }else if( f & MEM_Ephem ){ |
drh | 5ca0632 | 2020-01-06 19:23:41 +0000 | [diff] [blame] | 539 | c = 'e'; |
danielk1977 | b1bc953 | 2004-05-22 03:05:33 +0000 | [diff] [blame] | 540 | assert( (f & (MEM_Static|MEM_Dyn))==0 ); |
| 541 | }else{ |
drh | 5ca0632 | 2020-01-06 19:23:41 +0000 | [diff] [blame] | 542 | c = 's'; |
danielk1977 | b1bc953 | 2004-05-22 03:05:33 +0000 | [diff] [blame] | 543 | } |
drh | 5ca0632 | 2020-01-06 19:23:41 +0000 | [diff] [blame] | 544 | sqlite3_str_appendf(pStr, " %c%d[", c, pMem->n); |
drh | efb5f9a | 2019-08-30 21:52:13 +0000 | [diff] [blame] | 545 | for(j=0; j<25 && j<pMem->n; j++){ |
mistachkin | 5917117 | 2020-01-18 19:02:20 +0000 | [diff] [blame] | 546 | c = pMem->z[j]; |
drh | 5ca0632 | 2020-01-06 19:23:41 +0000 | [diff] [blame] | 547 | sqlite3_str_appendchar(pStr, 1, (c>=0x20&&c<=0x7f) ? c : '.'); |
danielk1977 | b1bc953 | 2004-05-22 03:05:33 +0000 | [diff] [blame] | 548 | } |
drh | 5ca0632 | 2020-01-06 19:23:41 +0000 | [diff] [blame] | 549 | sqlite3_str_appendf(pStr, "]%s", encnames[pMem->enc]); |
danielk1977 | ca6b291 | 2004-05-21 10:49:47 +0000 | [diff] [blame] | 550 | } |
danielk1977 | ca6b291 | 2004-05-21 10:49:47 +0000 | [diff] [blame] | 551 | } |
| 552 | #endif |
| 553 | |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 554 | #ifdef SQLITE_DEBUG |
| 555 | /* |
| 556 | ** Print the value of a register for tracing purposes: |
| 557 | */ |
drh | 84e55a8 | 2013-11-13 17:58:23 +0000 | [diff] [blame] | 558 | static void memTracePrint(Mem *p){ |
drh | a5750cf | 2014-02-07 13:20:31 +0000 | [diff] [blame] | 559 | if( p->flags & MEM_Undefined ){ |
drh | 84e55a8 | 2013-11-13 17:58:23 +0000 | [diff] [blame] | 560 | printf(" undefined"); |
drh | 953f761 | 2012-12-07 22:18:54 +0000 | [diff] [blame] | 561 | }else if( p->flags & MEM_Null ){ |
drh | ce2fbd1 | 2018-01-12 21:00:14 +0000 | [diff] [blame] | 562 | printf(p->flags & MEM_Zero ? " NULL-nochng" : " NULL"); |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 563 | }else if( (p->flags & (MEM_Int|MEM_Str))==(MEM_Int|MEM_Str) ){ |
drh | 84e55a8 | 2013-11-13 17:58:23 +0000 | [diff] [blame] | 564 | printf(" si:%lld", p->u.i); |
drh | 169f077 | 2019-05-02 21:36:26 +0000 | [diff] [blame] | 565 | }else if( (p->flags & (MEM_IntReal))!=0 ){ |
drh | 83a1daf | 2019-05-01 18:59:33 +0000 | [diff] [blame] | 566 | printf(" ir:%lld", p->u.i); |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 567 | }else if( p->flags & MEM_Int ){ |
drh | 84e55a8 | 2013-11-13 17:58:23 +0000 | [diff] [blame] | 568 | printf(" i:%lld", p->u.i); |
drh | 0b3bf92 | 2009-06-15 20:45:34 +0000 | [diff] [blame] | 569 | #ifndef SQLITE_OMIT_FLOATING_POINT |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 570 | }else if( p->flags & MEM_Real ){ |
drh | d1c472d | 2019-10-03 14:51:59 +0000 | [diff] [blame] | 571 | printf(" r:%.17g", p->u.r); |
drh | 0b3bf92 | 2009-06-15 20:45:34 +0000 | [diff] [blame] | 572 | #endif |
drh | 9d67afc | 2018-08-29 20:24:03 +0000 | [diff] [blame] | 573 | }else if( sqlite3VdbeMemIsRowSet(p) ){ |
drh | 84e55a8 | 2013-11-13 17:58:23 +0000 | [diff] [blame] | 574 | printf(" (rowset)"); |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 575 | }else{ |
drh | 5ca0632 | 2020-01-06 19:23:41 +0000 | [diff] [blame] | 576 | StrAccum acc; |
| 577 | char zBuf[1000]; |
| 578 | sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0); |
| 579 | sqlite3VdbeMemPrettyPrint(p, &acc); |
| 580 | printf(" %s", sqlite3StrAccumFinish(&acc)); |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 581 | } |
dan | 5b6c8e4 | 2016-01-30 15:46:03 +0000 | [diff] [blame] | 582 | if( p->flags & MEM_Subtype ) printf(" subtype=0x%02x", p->eSubtype); |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 583 | } |
drh | 84e55a8 | 2013-11-13 17:58:23 +0000 | [diff] [blame] | 584 | static void registerTrace(int iReg, Mem *p){ |
drh | 22e95fb | 2020-01-02 14:42:42 +0000 | [diff] [blame] | 585 | printf("R[%d] = ", iReg); |
drh | 84e55a8 | 2013-11-13 17:58:23 +0000 | [diff] [blame] | 586 | memTracePrint(p); |
drh | 22e95fb | 2020-01-02 14:42:42 +0000 | [diff] [blame] | 587 | if( p->pScopyFrom ){ |
| 588 | printf(" <== R[%d]", (int)(p->pScopyFrom - &p[-iReg])); |
| 589 | } |
drh | 84e55a8 | 2013-11-13 17:58:23 +0000 | [diff] [blame] | 590 | printf("\n"); |
drh | e2bc655 | 2017-04-17 20:50:34 +0000 | [diff] [blame] | 591 | sqlite3VdbeCheckMemInvariants(p); |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 592 | } |
drh | 93ffb50 | 2021-05-18 19:10:10 +0000 | [diff] [blame] | 593 | /**/ void sqlite3PrintMem(Mem *pMem){ |
drh | 59df3e9 | 2021-05-05 19:46:50 +0000 | [diff] [blame] | 594 | memTracePrint(pMem); |
| 595 | printf("\n"); |
| 596 | fflush(stdout); |
| 597 | } |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 598 | #endif |
| 599 | |
| 600 | #ifdef SQLITE_DEBUG |
drh | 22e95fb | 2020-01-02 14:42:42 +0000 | [diff] [blame] | 601 | /* |
| 602 | ** Show the values of all registers in the virtual machine. Used for |
| 603 | ** interactive debugging. |
| 604 | */ |
| 605 | void sqlite3VdbeRegisterDump(Vdbe *v){ |
| 606 | int i; |
| 607 | for(i=1; i<v->nMem; i++) registerTrace(i, v->aMem+i); |
| 608 | } |
| 609 | #endif /* SQLITE_DEBUG */ |
| 610 | |
| 611 | |
| 612 | #ifdef SQLITE_DEBUG |
drh | 84e55a8 | 2013-11-13 17:58:23 +0000 | [diff] [blame] | 613 | # define REGISTER_TRACE(R,M) if(db->flags&SQLITE_VdbeTrace)registerTrace(R,M) |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 614 | #else |
| 615 | # define REGISTER_TRACE(R,M) |
| 616 | #endif |
| 617 | |
danielk1977 | 84ac9d0 | 2004-05-18 09:58:06 +0000 | [diff] [blame] | 618 | |
drh | 7b39686 | 2003-01-01 23:06:20 +0000 | [diff] [blame] | 619 | #ifdef VDBE_PROFILE |
shane | 9bcbdad | 2008-05-29 20:22:37 +0000 | [diff] [blame] | 620 | |
| 621 | /* |
| 622 | ** hwtime.h contains inline assembler code for implementing |
| 623 | ** high-performance timing routines. |
drh | 7b39686 | 2003-01-01 23:06:20 +0000 | [diff] [blame] | 624 | */ |
shane | 9bcbdad | 2008-05-29 20:22:37 +0000 | [diff] [blame] | 625 | #include "hwtime.h" |
| 626 | |
drh | 7b39686 | 2003-01-01 23:06:20 +0000 | [diff] [blame] | 627 | #endif |
| 628 | |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 629 | #ifndef NDEBUG |
| 630 | /* |
| 631 | ** This function is only called from within an assert() expression. It |
| 632 | ** checks that the sqlite3.nTransaction variable is correctly set to |
| 633 | ** the number of non-transaction savepoints currently in the |
| 634 | ** linked list starting at sqlite3.pSavepoint. |
| 635 | ** |
| 636 | ** Usage: |
| 637 | ** |
| 638 | ** assert( checkSavepointCount(db) ); |
| 639 | */ |
| 640 | static int checkSavepointCount(sqlite3 *db){ |
| 641 | int n = 0; |
| 642 | Savepoint *p; |
| 643 | for(p=db->pSavepoint; p; p=p->pNext) n++; |
| 644 | assert( n==(db->nSavepoint + db->isTransactionSavepoint) ); |
| 645 | return 1; |
| 646 | } |
| 647 | #endif |
| 648 | |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 649 | /* |
| 650 | ** Return the register of pOp->p2 after first preparing it to be |
| 651 | ** overwritten with an integer value. |
drh | 9eef8c6 | 2015-10-15 17:31:41 +0000 | [diff] [blame] | 652 | */ |
| 653 | static SQLITE_NOINLINE Mem *out2PrereleaseWithClear(Mem *pOut){ |
| 654 | sqlite3VdbeMemSetNull(pOut); |
| 655 | pOut->flags = MEM_Int; |
| 656 | return pOut; |
| 657 | } |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 658 | static Mem *out2Prerelease(Vdbe *p, VdbeOp *pOp){ |
| 659 | Mem *pOut; |
| 660 | assert( pOp->p2>0 ); |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 661 | assert( pOp->p2<=(p->nMem+1 - p->nCursor) ); |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 662 | pOut = &p->aMem[pOp->p2]; |
| 663 | memAboutToChange(p, pOut); |
drh | a3fa140 | 2016-04-29 02:55:05 +0000 | [diff] [blame] | 664 | if( VdbeMemDynamic(pOut) ){ /*OPTIMIZATION-IF-FALSE*/ |
drh | 9eef8c6 | 2015-10-15 17:31:41 +0000 | [diff] [blame] | 665 | return out2PrereleaseWithClear(pOut); |
| 666 | }else{ |
| 667 | pOut->flags = MEM_Int; |
| 668 | return pOut; |
| 669 | } |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 670 | } |
| 671 | |
drh | faf9c77 | 2021-08-20 08:05:42 +0000 | [diff] [blame] | 672 | /* |
drh | 2db144c | 2021-12-01 16:31:02 +0000 | [diff] [blame] | 673 | ** Compute a bloom filter hash using pOp->p4.i registers from aMem[] beginning |
| 674 | ** with pOp->p3. Return the hash. |
| 675 | */ |
drh | 5baaf40 | 2021-12-06 13:07:28 +0000 | [diff] [blame] | 676 | static u64 filterHash(const Mem *aMem, const Op *pOp){ |
drh | 2db144c | 2021-12-01 16:31:02 +0000 | [diff] [blame] | 677 | int i, mx; |
drh | 5baaf40 | 2021-12-06 13:07:28 +0000 | [diff] [blame] | 678 | u64 h = 0; |
drh | 2db144c | 2021-12-01 16:31:02 +0000 | [diff] [blame] | 679 | |
drh | 2db144c | 2021-12-01 16:31:02 +0000 | [diff] [blame] | 680 | assert( pOp->p4type==P4_INT32 ); |
drh | 2db144c | 2021-12-01 16:31:02 +0000 | [diff] [blame] | 681 | for(i=pOp->p3, mx=i+pOp->p4.i; i<mx; i++){ |
| 682 | const Mem *p = &aMem[i]; |
| 683 | if( p->flags & (MEM_Int|MEM_IntReal) ){ |
drh | 5baaf40 | 2021-12-06 13:07:28 +0000 | [diff] [blame] | 684 | h += p->u.i; |
drh | 2db144c | 2021-12-01 16:31:02 +0000 | [diff] [blame] | 685 | }else if( p->flags & MEM_Real ){ |
drh | 5baaf40 | 2021-12-06 13:07:28 +0000 | [diff] [blame] | 686 | h += sqlite3VdbeIntValue(p); |
drh | 2db144c | 2021-12-01 16:31:02 +0000 | [diff] [blame] | 687 | }else if( p->flags & (MEM_Str|MEM_Blob) ){ |
| 688 | h += p->n; |
drh | 067c60c | 2021-12-04 18:45:08 +0000 | [diff] [blame] | 689 | if( p->flags & MEM_Zero ) h += p->u.nZero; |
drh | 2db144c | 2021-12-01 16:31:02 +0000 | [diff] [blame] | 690 | } |
| 691 | } |
drh | 5baaf40 | 2021-12-06 13:07:28 +0000 | [diff] [blame] | 692 | return h; |
drh | 2db144c | 2021-12-01 16:31:02 +0000 | [diff] [blame] | 693 | } |
| 694 | |
| 695 | /* |
drh | faf9c77 | 2021-08-20 08:05:42 +0000 | [diff] [blame] | 696 | ** Return the symbolic name for the data type of a pMem |
| 697 | */ |
| 698 | static const char *vdbeMemTypeName(Mem *pMem){ |
| 699 | static const char *azTypes[] = { |
| 700 | /* SQLITE_INTEGER */ "INT", |
| 701 | /* SQLITE_FLOAT */ "REAL", |
| 702 | /* SQLITE_TEXT */ "TEXT", |
| 703 | /* SQLITE_BLOB */ "BLOB", |
| 704 | /* SQLITE_NULL */ "NULL" |
| 705 | }; |
| 706 | return azTypes[sqlite3_value_type(pMem)-1]; |
| 707 | } |
drh | b975598 | 2010-07-24 16:34:37 +0000 | [diff] [blame] | 708 | |
| 709 | /* |
drh | 0fd6135 | 2014-02-07 02:29:45 +0000 | [diff] [blame] | 710 | ** Execute as much of a VDBE program as we can. |
| 711 | ** This is the core of sqlite3_step(). |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 712 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 713 | int sqlite3VdbeExec( |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 714 | Vdbe *p /* The VDBE */ |
| 715 | ){ |
drh | bbe879d | 2009-11-14 18:04:35 +0000 | [diff] [blame] | 716 | Op *aOp = p->aOp; /* Copy of p->aOp */ |
mistachkin | 5f7b95f | 2017-02-01 23:03:54 +0000 | [diff] [blame] | 717 | Op *pOp = aOp; /* Current operation */ |
drh | 6dc4148 | 2015-04-16 17:31:02 +0000 | [diff] [blame] | 718 | #if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE) |
| 719 | Op *pOrigOp; /* Value of pOp at the top of the loop */ |
| 720 | #endif |
drh | b89aeb6 | 2016-01-27 15:49:32 +0000 | [diff] [blame] | 721 | #ifdef SQLITE_DEBUG |
drh | def19e3 | 2016-01-27 16:26:25 +0000 | [diff] [blame] | 722 | int nExtraDelete = 0; /* Verifies FORDELETE and AUXDELETE flags */ |
drh | b89aeb6 | 2016-01-27 15:49:32 +0000 | [diff] [blame] | 723 | #endif |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 724 | int rc = SQLITE_OK; /* Value to return */ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 725 | sqlite3 *db = p->db; /* The database */ |
drh | cdf011d | 2011-04-04 21:25:28 +0000 | [diff] [blame] | 726 | u8 resetSchemaOnFault = 0; /* Reset schema after an error if positive */ |
drh | 8079a0d | 2006-01-12 17:20:50 +0000 | [diff] [blame] | 727 | u8 encoding = ENC(db); /* The database encoding */ |
drh | 0f825a7 | 2016-08-13 14:17:02 +0000 | [diff] [blame] | 728 | int iCompare = 0; /* Result of last comparison */ |
drh | d1d8914 | 2020-07-06 12:13:05 +0000 | [diff] [blame] | 729 | u64 nVmStep = 0; /* Number of virtual machine steps */ |
drh | 49afe3a | 2013-07-10 03:05:14 +0000 | [diff] [blame] | 730 | #ifndef SQLITE_OMIT_PROGRESS_CALLBACK |
drh | d1d8914 | 2020-07-06 12:13:05 +0000 | [diff] [blame] | 731 | u64 nProgressLimit; /* Invoke xProgress() when nVmStep reaches this */ |
drh | 49afe3a | 2013-07-10 03:05:14 +0000 | [diff] [blame] | 732 | #endif |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 733 | Mem *aMem = p->aMem; /* Copy of p->aMem */ |
drh | b27b7f5 | 2008-12-10 18:03:45 +0000 | [diff] [blame] | 734 | Mem *pIn1 = 0; /* 1st input operand */ |
| 735 | Mem *pIn2 = 0; /* 2nd input operand */ |
| 736 | Mem *pIn3 = 0; /* 3rd input operand */ |
| 737 | Mem *pOut = 0; /* Output operand */ |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 738 | #ifdef VDBE_PROFILE |
shane | 9bcbdad | 2008-05-29 20:22:37 +0000 | [diff] [blame] | 739 | u64 start; /* CPU clock count at start of opcode */ |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 740 | #endif |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 741 | /*** INSERT STACK UNION HERE ***/ |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 742 | |
drh | 66181ce | 2022-03-31 20:04:49 +0000 | [diff] [blame] | 743 | assert( p->eVdbeState==VDBE_RUN_STATE ); /* sqlite3_step() verifies this */ |
drh | bdaec52 | 2011-04-04 00:14:43 +0000 | [diff] [blame] | 744 | sqlite3VdbeEnter(p); |
drh | 82642f8 | 2019-02-12 22:58:32 +0000 | [diff] [blame] | 745 | #ifndef SQLITE_OMIT_PROGRESS_CALLBACK |
| 746 | if( db->xProgress ){ |
| 747 | u32 iPrior = p->aCounter[SQLITE_STMTSTATUS_VM_STEP]; |
| 748 | assert( 0 < db->nProgressOps ); |
| 749 | nProgressLimit = db->nProgressOps - (iPrior % db->nProgressOps); |
| 750 | }else{ |
drh | d1d8914 | 2020-07-06 12:13:05 +0000 | [diff] [blame] | 751 | nProgressLimit = LARGEST_UINT64; |
drh | 82642f8 | 2019-02-12 22:58:32 +0000 | [diff] [blame] | 752 | } |
| 753 | #endif |
danielk1977 | 2e588c7 | 2005-12-09 14:25:08 +0000 | [diff] [blame] | 754 | if( p->rc==SQLITE_NOMEM ){ |
| 755 | /* This happens if a malloc() inside a call to sqlite3_column_text() or |
| 756 | ** sqlite3_column_text16() failed. */ |
| 757 | goto no_mem; |
| 758 | } |
drh | cbd8db3 | 2015-08-20 17:18:32 +0000 | [diff] [blame] | 759 | assert( p->rc==SQLITE_OK || (p->rc&0xff)==SQLITE_BUSY ); |
drh | a5f3fb3 | 2020-06-03 19:28:10 +0000 | [diff] [blame] | 760 | testcase( p->rc!=SQLITE_OK ); |
| 761 | p->rc = SQLITE_OK; |
drh | 1713afb | 2013-06-28 01:24:57 +0000 | [diff] [blame] | 762 | assert( p->bIsReader || p->readOnly!=0 ); |
drh | 95a7b3e | 2013-09-16 12:57:19 +0000 | [diff] [blame] | 763 | p->iCurrentTime = 0; |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 764 | assert( p->explain==0 ); |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 765 | p->pResultSet = 0; |
drh | a4afb65 | 2005-07-09 02:16:02 +0000 | [diff] [blame] | 766 | db->busyHandler.nBusy = 0; |
dan | 892edb6 | 2020-03-30 13:35:05 +0000 | [diff] [blame] | 767 | if( AtomicLoad(&db->u1.isInterrupted) ) goto abort_due_to_interrupt; |
drh | 602c237 | 2007-03-01 00:29:13 +0000 | [diff] [blame] | 768 | sqlite3VdbeIOTraceSql(p); |
drh | 3c23a88 | 2007-01-09 14:01:13 +0000 | [diff] [blame] | 769 | #ifdef SQLITE_DEBUG |
danielk1977 | 2d1d86f | 2008-06-20 14:59:51 +0000 | [diff] [blame] | 770 | sqlite3BeginBenignMalloc(); |
drh | 84e55a8 | 2013-11-13 17:58:23 +0000 | [diff] [blame] | 771 | if( p->pc==0 |
| 772 | && (p->db->flags & (SQLITE_VdbeListing|SQLITE_VdbeEQP|SQLITE_VdbeTrace))!=0 |
| 773 | ){ |
drh | 3c23a88 | 2007-01-09 14:01:13 +0000 | [diff] [blame] | 774 | int i; |
drh | 84e55a8 | 2013-11-13 17:58:23 +0000 | [diff] [blame] | 775 | int once = 1; |
drh | 3c23a88 | 2007-01-09 14:01:13 +0000 | [diff] [blame] | 776 | sqlite3VdbePrintSql(p); |
drh | 84e55a8 | 2013-11-13 17:58:23 +0000 | [diff] [blame] | 777 | if( p->db->flags & SQLITE_VdbeListing ){ |
| 778 | printf("VDBE Program Listing:\n"); |
| 779 | for(i=0; i<p->nOp; i++){ |
| 780 | sqlite3VdbePrintOp(stdout, i, &aOp[i]); |
| 781 | } |
drh | 3c23a88 | 2007-01-09 14:01:13 +0000 | [diff] [blame] | 782 | } |
drh | 84e55a8 | 2013-11-13 17:58:23 +0000 | [diff] [blame] | 783 | if( p->db->flags & SQLITE_VdbeEQP ){ |
| 784 | for(i=0; i<p->nOp; i++){ |
| 785 | if( aOp[i].opcode==OP_Explain ){ |
| 786 | if( once ) printf("VDBE Query Plan:\n"); |
| 787 | printf("%s\n", aOp[i].p4.z); |
| 788 | once = 0; |
| 789 | } |
| 790 | } |
| 791 | } |
| 792 | if( p->db->flags & SQLITE_VdbeTrace ) printf("VDBE Trace:\n"); |
drh | 3c23a88 | 2007-01-09 14:01:13 +0000 | [diff] [blame] | 793 | } |
danielk1977 | 2d1d86f | 2008-06-20 14:59:51 +0000 | [diff] [blame] | 794 | sqlite3EndBenignMalloc(); |
drh | 3c23a88 | 2007-01-09 14:01:13 +0000 | [diff] [blame] | 795 | #endif |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 796 | for(pOp=&aOp[p->pc]; 1; pOp++){ |
| 797 | /* Errors are detected by individual opcodes, with an immediate |
| 798 | ** jumps to abort_due_to_error. */ |
| 799 | assert( rc==SQLITE_OK ); |
| 800 | |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 801 | assert( pOp>=aOp && pOp<&aOp[p->nOp]); |
drh | 7b39686 | 2003-01-01 23:06:20 +0000 | [diff] [blame] | 802 | #ifdef VDBE_PROFILE |
drh | 35043cc | 2018-02-12 20:27:34 +0000 | [diff] [blame] | 803 | start = sqlite3NProfileCnt ? sqlite3NProfileCnt : sqlite3Hwtime(); |
drh | 7b39686 | 2003-01-01 23:06:20 +0000 | [diff] [blame] | 804 | #endif |
drh | bf159fa | 2013-06-25 22:01:22 +0000 | [diff] [blame] | 805 | nVmStep++; |
dan | 6f9702e | 2014-11-01 20:38:06 +0000 | [diff] [blame] | 806 | #ifdef SQLITE_ENABLE_STMT_SCANSTATUS |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 807 | if( p->anExec ) p->anExec[(int)(pOp-aOp)]++; |
dan | 6f9702e | 2014-11-01 20:38:06 +0000 | [diff] [blame] | 808 | #endif |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 809 | |
danielk1977 | 8b60e0f | 2005-01-12 09:10:39 +0000 | [diff] [blame] | 810 | /* Only allow tracing if SQLITE_DEBUG is defined. |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 811 | */ |
danielk1977 | 8b60e0f | 2005-01-12 09:10:39 +0000 | [diff] [blame] | 812 | #ifdef SQLITE_DEBUG |
drh | 84e55a8 | 2013-11-13 17:58:23 +0000 | [diff] [blame] | 813 | if( db->flags & SQLITE_VdbeTrace ){ |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 814 | sqlite3VdbePrintOp(stdout, (int)(pOp - aOp), pOp); |
drh | 22e95fb | 2020-01-02 14:42:42 +0000 | [diff] [blame] | 815 | test_trace_breakpoint((int)(pOp - aOp),pOp,p); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 816 | } |
drh | 3f7d4e4 | 2004-07-24 14:35:58 +0000 | [diff] [blame] | 817 | #endif |
| 818 | |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 819 | |
drh | f603871 | 2004-02-08 18:07:34 +0000 | [diff] [blame] | 820 | /* Check to see if we need to simulate an interrupt. This only happens |
| 821 | ** if we have a special test build. |
| 822 | */ |
| 823 | #ifdef SQLITE_TEST |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 824 | if( sqlite3_interrupt_count>0 ){ |
| 825 | sqlite3_interrupt_count--; |
| 826 | if( sqlite3_interrupt_count==0 ){ |
| 827 | sqlite3_interrupt(db); |
drh | f603871 | 2004-02-08 18:07:34 +0000 | [diff] [blame] | 828 | } |
| 829 | } |
| 830 | #endif |
| 831 | |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 832 | /* Sanity checking on other operands */ |
| 833 | #ifdef SQLITE_DEBUG |
drh | 7cc84c2 | 2016-04-11 13:36:42 +0000 | [diff] [blame] | 834 | { |
| 835 | u8 opProperty = sqlite3OpcodeProperty[pOp->opcode]; |
| 836 | if( (opProperty & OPFLG_IN1)!=0 ){ |
| 837 | assert( pOp->p1>0 ); |
| 838 | assert( pOp->p1<=(p->nMem+1 - p->nCursor) ); |
| 839 | assert( memIsValid(&aMem[pOp->p1]) ); |
| 840 | assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p1]) ); |
| 841 | REGISTER_TRACE(pOp->p1, &aMem[pOp->p1]); |
| 842 | } |
| 843 | if( (opProperty & OPFLG_IN2)!=0 ){ |
| 844 | assert( pOp->p2>0 ); |
| 845 | assert( pOp->p2<=(p->nMem+1 - p->nCursor) ); |
| 846 | assert( memIsValid(&aMem[pOp->p2]) ); |
| 847 | assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p2]) ); |
| 848 | REGISTER_TRACE(pOp->p2, &aMem[pOp->p2]); |
| 849 | } |
| 850 | if( (opProperty & OPFLG_IN3)!=0 ){ |
| 851 | assert( pOp->p3>0 ); |
| 852 | assert( pOp->p3<=(p->nMem+1 - p->nCursor) ); |
| 853 | assert( memIsValid(&aMem[pOp->p3]) ); |
| 854 | assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p3]) ); |
| 855 | REGISTER_TRACE(pOp->p3, &aMem[pOp->p3]); |
| 856 | } |
| 857 | if( (opProperty & OPFLG_OUT2)!=0 ){ |
| 858 | assert( pOp->p2>0 ); |
| 859 | assert( pOp->p2<=(p->nMem+1 - p->nCursor) ); |
| 860 | memAboutToChange(p, &aMem[pOp->p2]); |
| 861 | } |
| 862 | if( (opProperty & OPFLG_OUT3)!=0 ){ |
| 863 | assert( pOp->p3>0 ); |
| 864 | assert( pOp->p3<=(p->nMem+1 - p->nCursor) ); |
| 865 | memAboutToChange(p, &aMem[pOp->p3]); |
| 866 | } |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 867 | } |
| 868 | #endif |
drh | 6dc4148 | 2015-04-16 17:31:02 +0000 | [diff] [blame] | 869 | #if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE) |
| 870 | pOrigOp = pOp; |
| 871 | #endif |
drh | 93952eb | 2009-11-13 19:43:43 +0000 | [diff] [blame] | 872 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 873 | switch( pOp->opcode ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 874 | |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 875 | /***************************************************************************** |
| 876 | ** What follows is a massive switch statement where each case implements a |
| 877 | ** separate instruction in the virtual machine. If we follow the usual |
| 878 | ** indentation conventions, each case should be indented by 6 spaces. But |
| 879 | ** that is a lot of wasted space on the left margin. So the code within |
| 880 | ** the switch statement will break with convention and be flush-left. Another |
| 881 | ** big comment (similar to this one) will mark the point in the code where |
| 882 | ** we transition back to normal indentation. |
drh | ac82fcf | 2002-09-08 17:23:41 +0000 | [diff] [blame] | 883 | ** |
| 884 | ** The formatting of each case is important. The makefile for SQLite |
| 885 | ** generates two C files "opcodes.h" and "opcodes.c" by scanning this |
| 886 | ** file looking for lines that begin with "case OP_". The opcodes.h files |
| 887 | ** will be filled with #defines that give unique integer values to each |
| 888 | ** 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] | 889 | ** each string is the symbolic name for the corresponding opcode. If the |
| 890 | ** case statement is followed by a comment of the form "/# same as ... #/" |
| 891 | ** that comment is used to determine the particular value of the opcode. |
drh | ac82fcf | 2002-09-08 17:23:41 +0000 | [diff] [blame] | 892 | ** |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 893 | ** Other keywords in the comment that follows each case are used to |
| 894 | ** construct the OPFLG_INITIALIZER value that initializes opcodeProperty[]. |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 895 | ** Keywords include: in1, in2, in3, out2, out3. See |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 896 | ** the mkopcodeh.awk script for additional information. |
danielk1977 | bc04f85 | 2005-03-29 08:26:13 +0000 | [diff] [blame] | 897 | ** |
drh | ac82fcf | 2002-09-08 17:23:41 +0000 | [diff] [blame] | 898 | ** Documentation about VDBE opcodes is generated by scanning this file |
| 899 | ** for lines of that contain "Opcode:". That line and all subsequent |
| 900 | ** comment lines are used in the generation of the opcode.html documentation |
| 901 | ** file. |
| 902 | ** |
| 903 | ** SUMMARY: |
| 904 | ** |
| 905 | ** Formatting is important to scripts that scan this file. |
| 906 | ** Do not deviate from the formatting style currently in use. |
| 907 | ** |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 908 | *****************************************************************************/ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 909 | |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 910 | /* Opcode: Goto * P2 * * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 911 | ** |
| 912 | ** An unconditional jump to address P2. |
| 913 | ** The next instruction executed will be |
| 914 | ** the one at index P2 from the beginning of |
| 915 | ** the program. |
drh | fe70510 | 2014-03-06 13:38:37 +0000 | [diff] [blame] | 916 | ** |
| 917 | ** The P1 parameter is not actually used by this opcode. However, it |
| 918 | ** is sometimes set to 1 instead of 0 as a hint to the command-line shell |
| 919 | ** that this Goto is the bottom of a loop and that the lines from P2 down |
| 920 | ** to the current line should be indented for EXPLAIN output. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 921 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 922 | case OP_Goto: { /* jump */ |
drh | d9670ab | 2019-12-28 01:52:46 +0000 | [diff] [blame] | 923 | |
| 924 | #ifdef SQLITE_DEBUG |
| 925 | /* In debuggging mode, when the p5 flags is set on an OP_Goto, that |
| 926 | ** means we should really jump back to the preceeding OP_ReleaseReg |
| 927 | ** instruction. */ |
| 928 | if( pOp->p5 ){ |
| 929 | assert( pOp->p2 < (int)(pOp - aOp) ); |
| 930 | assert( pOp->p2 > 1 ); |
| 931 | pOp = &aOp[pOp->p2 - 2]; |
| 932 | assert( pOp[1].opcode==OP_ReleaseReg ); |
| 933 | goto check_for_interrupt; |
| 934 | } |
| 935 | #endif |
| 936 | |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 937 | jump_to_p2_and_check_for_interrupt: |
| 938 | pOp = &aOp[pOp->p2 - 1]; |
drh | 49afe3a | 2013-07-10 03:05:14 +0000 | [diff] [blame] | 939 | |
| 940 | /* 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] | 941 | ** OP_VNext, or OP_SorterNext) all jump here upon |
drh | 49afe3a | 2013-07-10 03:05:14 +0000 | [diff] [blame] | 942 | ** completion. Check to see if sqlite3_interrupt() has been called |
| 943 | ** or if the progress callback needs to be invoked. |
| 944 | ** |
| 945 | ** This code uses unstructured "goto" statements and does not look clean. |
| 946 | ** But that is not due to sloppy coding habits. The code is written this |
| 947 | ** way for performance, to avoid having to run the interrupt and progress |
| 948 | ** checks on every opcode. This helps sqlite3_step() to run about 1.5% |
| 949 | ** faster according to "valgrind --tool=cachegrind" */ |
| 950 | check_for_interrupt: |
dan | 892edb6 | 2020-03-30 13:35:05 +0000 | [diff] [blame] | 951 | if( AtomicLoad(&db->u1.isInterrupted) ) goto abort_due_to_interrupt; |
drh | 49afe3a | 2013-07-10 03:05:14 +0000 | [diff] [blame] | 952 | #ifndef SQLITE_OMIT_PROGRESS_CALLBACK |
| 953 | /* Call the progress callback if it is configured and the required number |
| 954 | ** of VDBE ops have been executed (either since this invocation of |
| 955 | ** sqlite3VdbeExec() or since last time the progress callback was called). |
| 956 | ** If the progress callback returns non-zero, exit the virtual machine with |
| 957 | ** a return code SQLITE_ABORT. |
| 958 | */ |
drh | b1af9c6 | 2019-02-20 13:55:45 +0000 | [diff] [blame] | 959 | while( nVmStep>=nProgressLimit && db->xProgress!=0 ){ |
drh | 400fcba | 2013-11-14 00:09:48 +0000 | [diff] [blame] | 960 | assert( db->nProgressOps!=0 ); |
drh | b1af9c6 | 2019-02-20 13:55:45 +0000 | [diff] [blame] | 961 | nProgressLimit += db->nProgressOps; |
drh | 400fcba | 2013-11-14 00:09:48 +0000 | [diff] [blame] | 962 | if( db->xProgress(db->pProgressArg) ){ |
drh | d1d8914 | 2020-07-06 12:13:05 +0000 | [diff] [blame] | 963 | nProgressLimit = LARGEST_UINT64; |
drh | 49afe3a | 2013-07-10 03:05:14 +0000 | [diff] [blame] | 964 | rc = SQLITE_INTERRUPT; |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 965 | goto abort_due_to_error; |
drh | 49afe3a | 2013-07-10 03:05:14 +0000 | [diff] [blame] | 966 | } |
drh | 49afe3a | 2013-07-10 03:05:14 +0000 | [diff] [blame] | 967 | } |
| 968 | #endif |
| 969 | |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 970 | break; |
| 971 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 972 | |
drh | 2eb9537 | 2008-06-06 15:04:36 +0000 | [diff] [blame] | 973 | /* Opcode: Gosub P1 P2 * * * |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 974 | ** |
drh | 2eb9537 | 2008-06-06 15:04:36 +0000 | [diff] [blame] | 975 | ** Write the current address onto register P1 |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 976 | ** and then jump to address P2. |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 977 | */ |
drh | b8475df | 2011-12-09 16:21:19 +0000 | [diff] [blame] | 978 | case OP_Gosub: { /* jump */ |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 979 | assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) ); |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 980 | pIn1 = &aMem[pOp->p1]; |
drh | c91b2fd | 2014-03-01 18:13:23 +0000 | [diff] [blame] | 981 | assert( VdbeMemDynamic(pIn1)==0 ); |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 982 | memAboutToChange(p, pIn1); |
drh | 2eb9537 | 2008-06-06 15:04:36 +0000 | [diff] [blame] | 983 | pIn1->flags = MEM_Int; |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 984 | pIn1->u.i = (int)(pOp-aOp); |
drh | 2eb9537 | 2008-06-06 15:04:36 +0000 | [diff] [blame] | 985 | REGISTER_TRACE(pOp->p1, pIn1); |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 986 | |
| 987 | /* Most jump operations do a goto to this spot in order to update |
| 988 | ** the pOp pointer. */ |
| 989 | jump_to_p2: |
| 990 | pOp = &aOp[pOp->p2 - 1]; |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 991 | break; |
| 992 | } |
| 993 | |
drh | e603ab0 | 2022-04-07 19:06:31 +0000 | [diff] [blame] | 994 | /* Opcode: Return P1 P2 P3 * * |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 995 | ** |
drh | 6134b2d | 2022-04-11 17:27:38 +0000 | [diff] [blame] | 996 | ** Jump to the next instruction after the address stored in register P1. |
| 997 | ** |
| 998 | ** It used to be that after the jump, register P1 would become undefined. |
| 999 | ** However, for the subroutine used for the inner loop of a RIGHT JOIN, |
| 1000 | ** it is useful for R1 register to be unchanged, so that is what happens |
| 1001 | ** now. |
drh | 1902516 | 2022-03-03 15:00:44 +0000 | [diff] [blame] | 1002 | ** |
drh | e603ab0 | 2022-04-07 19:06:31 +0000 | [diff] [blame] | 1003 | ** P2 is not used by the byte-code engine. However, if P2 is positive |
| 1004 | ** and also less than the current address, then the "EXPLAIN" output |
| 1005 | ** formatter in the CLI will indent all opcodes from the P2 opcode up |
| 1006 | ** to be not including the current Return. P2 should be the first opcode |
| 1007 | ** in the subroutine from which this opcode is returnning. Thus the P2 |
| 1008 | ** value is a byte-code indentation hint. See tag-20220407a in |
| 1009 | ** wherecode.c and shell.c. |
| 1010 | ** |
drh | 1902516 | 2022-03-03 15:00:44 +0000 | [diff] [blame] | 1011 | ** P3 is not used by the byte-code engine. However, the code generator |
| 1012 | ** sets P3 to address of the associated OP_BeginSubrtn opcode, if there is |
| 1013 | ** one. |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 1014 | */ |
drh | 2eb9537 | 2008-06-06 15:04:36 +0000 | [diff] [blame] | 1015 | case OP_Return: { /* in1 */ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 1016 | pIn1 = &aMem[pOp->p1]; |
drh | 81cf13e | 2014-02-07 18:27:53 +0000 | [diff] [blame] | 1017 | assert( pIn1->flags==MEM_Int ); |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 1018 | pOp = &aOp[pIn1->u.i]; |
drh | 6134b2d | 2022-04-11 17:27:38 +0000 | [diff] [blame] | 1019 | /* pIn1->flags = MEM_Undefined; */ |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 1020 | break; |
| 1021 | } |
| 1022 | |
drh | ed71a83 | 2014-02-07 19:18:10 +0000 | [diff] [blame] | 1023 | /* Opcode: InitCoroutine P1 P2 P3 * * |
drh | 81cf13e | 2014-02-07 18:27:53 +0000 | [diff] [blame] | 1024 | ** |
drh | 5dad9a3 | 2014-07-25 18:37:42 +0000 | [diff] [blame] | 1025 | ** Set up register P1 so that it will Yield to the coroutine |
drh | ed71a83 | 2014-02-07 19:18:10 +0000 | [diff] [blame] | 1026 | ** located at address P3. |
| 1027 | ** |
drh | 5dad9a3 | 2014-07-25 18:37:42 +0000 | [diff] [blame] | 1028 | ** If P2!=0 then the coroutine implementation immediately follows |
| 1029 | ** this opcode. So jump over the coroutine implementation to |
drh | ed71a83 | 2014-02-07 19:18:10 +0000 | [diff] [blame] | 1030 | ** address P2. |
drh | 5dad9a3 | 2014-07-25 18:37:42 +0000 | [diff] [blame] | 1031 | ** |
| 1032 | ** See also: EndCoroutine |
drh | 81cf13e | 2014-02-07 18:27:53 +0000 | [diff] [blame] | 1033 | */ |
| 1034 | case OP_InitCoroutine: { /* jump */ |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 1035 | assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) ); |
drh | ed71a83 | 2014-02-07 19:18:10 +0000 | [diff] [blame] | 1036 | assert( pOp->p2>=0 && pOp->p2<p->nOp ); |
| 1037 | assert( pOp->p3>=0 && pOp->p3<p->nOp ); |
drh | 81cf13e | 2014-02-07 18:27:53 +0000 | [diff] [blame] | 1038 | pOut = &aMem[pOp->p1]; |
drh | ed71a83 | 2014-02-07 19:18:10 +0000 | [diff] [blame] | 1039 | assert( !VdbeMemDynamic(pOut) ); |
| 1040 | pOut->u.i = pOp->p3 - 1; |
drh | 81cf13e | 2014-02-07 18:27:53 +0000 | [diff] [blame] | 1041 | pOut->flags = MEM_Int; |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 1042 | if( pOp->p2 ) goto jump_to_p2; |
drh | 81cf13e | 2014-02-07 18:27:53 +0000 | [diff] [blame] | 1043 | break; |
| 1044 | } |
| 1045 | |
| 1046 | /* Opcode: EndCoroutine P1 * * * * |
| 1047 | ** |
drh | bc5cf38 | 2014-08-06 01:08:07 +0000 | [diff] [blame] | 1048 | ** The instruction at the address in register P1 is a Yield. |
drh | 5dad9a3 | 2014-07-25 18:37:42 +0000 | [diff] [blame] | 1049 | ** Jump to the P2 parameter of that Yield. |
drh | 81cf13e | 2014-02-07 18:27:53 +0000 | [diff] [blame] | 1050 | ** After the jump, register P1 becomes undefined. |
drh | 5dad9a3 | 2014-07-25 18:37:42 +0000 | [diff] [blame] | 1051 | ** |
| 1052 | ** See also: InitCoroutine |
drh | 81cf13e | 2014-02-07 18:27:53 +0000 | [diff] [blame] | 1053 | */ |
| 1054 | case OP_EndCoroutine: { /* in1 */ |
| 1055 | VdbeOp *pCaller; |
| 1056 | pIn1 = &aMem[pOp->p1]; |
| 1057 | assert( pIn1->flags==MEM_Int ); |
| 1058 | assert( pIn1->u.i>=0 && pIn1->u.i<p->nOp ); |
| 1059 | pCaller = &aOp[pIn1->u.i]; |
| 1060 | assert( pCaller->opcode==OP_Yield ); |
| 1061 | assert( pCaller->p2>=0 && pCaller->p2<p->nOp ); |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 1062 | pOp = &aOp[pCaller->p2 - 1]; |
drh | 81cf13e | 2014-02-07 18:27:53 +0000 | [diff] [blame] | 1063 | pIn1->flags = MEM_Undefined; |
| 1064 | break; |
| 1065 | } |
| 1066 | |
| 1067 | /* Opcode: Yield P1 P2 * * * |
drh | e00ee6e | 2008-06-20 15:24:01 +0000 | [diff] [blame] | 1068 | ** |
drh | 5dad9a3 | 2014-07-25 18:37:42 +0000 | [diff] [blame] | 1069 | ** Swap the program counter with the value in register P1. This |
| 1070 | ** has the effect of yielding to a coroutine. |
drh | 81cf13e | 2014-02-07 18:27:53 +0000 | [diff] [blame] | 1071 | ** |
drh | 5dad9a3 | 2014-07-25 18:37:42 +0000 | [diff] [blame] | 1072 | ** If the coroutine that is launched by this instruction ends with |
| 1073 | ** Yield or Return then continue to the next instruction. But if |
| 1074 | ** the coroutine launched by this instruction ends with |
| 1075 | ** EndCoroutine, then jump to P2 rather than continuing with the |
| 1076 | ** next instruction. |
| 1077 | ** |
| 1078 | ** See also: InitCoroutine |
drh | e00ee6e | 2008-06-20 15:24:01 +0000 | [diff] [blame] | 1079 | */ |
drh | 81cf13e | 2014-02-07 18:27:53 +0000 | [diff] [blame] | 1080 | case OP_Yield: { /* in1, jump */ |
drh | e00ee6e | 2008-06-20 15:24:01 +0000 | [diff] [blame] | 1081 | int pcDest; |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 1082 | pIn1 = &aMem[pOp->p1]; |
drh | c91b2fd | 2014-03-01 18:13:23 +0000 | [diff] [blame] | 1083 | assert( VdbeMemDynamic(pIn1)==0 ); |
drh | e00ee6e | 2008-06-20 15:24:01 +0000 | [diff] [blame] | 1084 | pIn1->flags = MEM_Int; |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 1085 | pcDest = (int)pIn1->u.i; |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 1086 | pIn1->u.i = (int)(pOp - aOp); |
drh | e00ee6e | 2008-06-20 15:24:01 +0000 | [diff] [blame] | 1087 | REGISTER_TRACE(pOp->p1, pIn1); |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 1088 | pOp = &aOp[pcDest]; |
drh | e00ee6e | 2008-06-20 15:24:01 +0000 | [diff] [blame] | 1089 | break; |
| 1090 | } |
| 1091 | |
drh | f9c8ce3 | 2013-11-05 13:33:55 +0000 | [diff] [blame] | 1092 | /* Opcode: HaltIfNull P1 P2 P3 P4 P5 |
drh | 72e26de | 2016-08-24 21:24:04 +0000 | [diff] [blame] | 1093 | ** Synopsis: if r[P3]=null halt |
drh | 5053a79 | 2009-02-20 03:02:23 +0000 | [diff] [blame] | 1094 | ** |
drh | ef8662b | 2011-06-20 21:47:58 +0000 | [diff] [blame] | 1095 | ** Check the value in register P3. If it is NULL then Halt using |
drh | 5053a79 | 2009-02-20 03:02:23 +0000 | [diff] [blame] | 1096 | ** parameter P1, P2, and P4 as if this were a Halt instruction. If the |
| 1097 | ** 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] | 1098 | ** The P5 parameter should be 1. |
drh | 5053a79 | 2009-02-20 03:02:23 +0000 | [diff] [blame] | 1099 | */ |
| 1100 | case OP_HaltIfNull: { /* in3 */ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 1101 | pIn3 = &aMem[pOp->p3]; |
drh | 4031baf | 2018-05-28 17:31:20 +0000 | [diff] [blame] | 1102 | #ifdef SQLITE_DEBUG |
| 1103 | if( pOp->p2==OE_Abort ){ sqlite3VdbeAssertAbortable(p); } |
| 1104 | #endif |
drh | 5053a79 | 2009-02-20 03:02:23 +0000 | [diff] [blame] | 1105 | if( (pIn3->flags & MEM_Null)==0 ) break; |
| 1106 | /* Fall through into OP_Halt */ |
drh | 08b9208 | 2020-08-10 14:18:00 +0000 | [diff] [blame] | 1107 | /* no break */ deliberate_fall_through |
drh | 5053a79 | 2009-02-20 03:02:23 +0000 | [diff] [blame] | 1108 | } |
drh | e00ee6e | 2008-06-20 15:24:01 +0000 | [diff] [blame] | 1109 | |
drh | f9c8ce3 | 2013-11-05 13:33:55 +0000 | [diff] [blame] | 1110 | /* Opcode: Halt P1 P2 * P4 P5 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1111 | ** |
drh | 3d4501e | 2008-12-04 20:40:10 +0000 | [diff] [blame] | 1112 | ** Exit immediately. All open cursors, etc are closed |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1113 | ** automatically. |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 1114 | ** |
drh | 92f02c3 | 2004-09-02 14:57:08 +0000 | [diff] [blame] | 1115 | ** P1 is the result code returned by sqlite3_exec(), sqlite3_reset(), |
| 1116 | ** or sqlite3_finalize(). For a normal halt, this should be SQLITE_OK (0). |
| 1117 | ** For errors, it can be some other value. If P1!=0 then P2 will determine |
| 1118 | ** whether or not to rollback the current transaction. Do not rollback |
| 1119 | ** if P2==OE_Fail. Do the rollback if P2==OE_Rollback. If P2==OE_Abort, |
| 1120 | ** then back out all changes that have occurred during this execution of the |
drh | b798fa6 | 2002-09-03 19:43:23 +0000 | [diff] [blame] | 1121 | ** VDBE, but do not rollback the transaction. |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 1122 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 1123 | ** If P4 is not null then it is an error message string. |
drh | 7f057c9 | 2005-06-24 03:53:06 +0000 | [diff] [blame] | 1124 | ** |
drh | f9c8ce3 | 2013-11-05 13:33:55 +0000 | [diff] [blame] | 1125 | ** P5 is a value between 0 and 4, inclusive, that modifies the P4 string. |
| 1126 | ** |
| 1127 | ** 0: (no change) |
| 1128 | ** 1: NOT NULL contraint failed: P4 |
| 1129 | ** 2: UNIQUE constraint failed: P4 |
| 1130 | ** 3: CHECK constraint failed: P4 |
| 1131 | ** 4: FOREIGN KEY constraint failed: P4 |
| 1132 | ** |
| 1133 | ** If P5 is not zero and P4 is NULL, then everything after the ":" is |
| 1134 | ** omitted. |
| 1135 | ** |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 1136 | ** 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] | 1137 | ** every program. So a jump past the last instruction of the program |
| 1138 | ** is the same as executing Halt. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1139 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1140 | case OP_Halt: { |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 1141 | VdbeFrame *pFrame; |
| 1142 | int pcx; |
drh | f9c8ce3 | 2013-11-05 13:33:55 +0000 | [diff] [blame] | 1143 | |
drh | 4031baf | 2018-05-28 17:31:20 +0000 | [diff] [blame] | 1144 | #ifdef SQLITE_DEBUG |
| 1145 | if( pOp->p2==OE_Abort ){ sqlite3VdbeAssertAbortable(p); } |
| 1146 | #endif |
drh | 8bb93da | 2022-04-03 20:39:48 +0000 | [diff] [blame] | 1147 | if( p->pFrame && pOp->p1==SQLITE_OK ){ |
dan | 2832ad4 | 2009-08-31 15:27:27 +0000 | [diff] [blame] | 1148 | /* Halt the sub-program. Return control to the parent frame. */ |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 1149 | pFrame = p->pFrame; |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 1150 | p->pFrame = pFrame->pParent; |
| 1151 | p->nFrame--; |
dan | 2832ad4 | 2009-08-31 15:27:27 +0000 | [diff] [blame] | 1152 | sqlite3VdbeSetChanges(db, p->nChange); |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 1153 | pcx = sqlite3VdbeFrameRestore(pFrame); |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 1154 | if( pOp->p2==OE_Ignore ){ |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 1155 | /* Instruction pcx is the OP_Program that invoked the sub-program |
dan | 2832ad4 | 2009-08-31 15:27:27 +0000 | [diff] [blame] | 1156 | ** currently being halted. If the p2 instruction of this OP_Halt |
| 1157 | ** instruction is set to OE_Ignore, then the sub-program is throwing |
| 1158 | ** an IGNORE exception. In this case jump to the address specified |
| 1159 | ** as the p2 of the calling OP_Program. */ |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 1160 | pcx = p->aOp[pcx].p2-1; |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 1161 | } |
drh | bbe879d | 2009-11-14 18:04:35 +0000 | [diff] [blame] | 1162 | aOp = p->aOp; |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 1163 | aMem = p->aMem; |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 1164 | pOp = &aOp[pcx]; |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 1165 | break; |
| 1166 | } |
drh | 92f02c3 | 2004-09-02 14:57:08 +0000 | [diff] [blame] | 1167 | p->rc = pOp->p1; |
shane | 36840fd | 2009-06-26 16:32:13 +0000 | [diff] [blame] | 1168 | p->errorAction = (u8)pOp->p2; |
drh | fb4e3a3 | 2016-12-30 00:09:14 +0000 | [diff] [blame] | 1169 | assert( pOp->p5<=4 ); |
drh | f9c8ce3 | 2013-11-05 13:33:55 +0000 | [diff] [blame] | 1170 | if( p->rc ){ |
drh | d9b7ec9 | 2013-11-06 14:05:21 +0000 | [diff] [blame] | 1171 | if( pOp->p5 ){ |
| 1172 | static const char * const azType[] = { "NOT NULL", "UNIQUE", "CHECK", |
| 1173 | "FOREIGN KEY" }; |
drh | d9b7ec9 | 2013-11-06 14:05:21 +0000 | [diff] [blame] | 1174 | testcase( pOp->p5==1 ); |
| 1175 | testcase( pOp->p5==2 ); |
| 1176 | testcase( pOp->p5==3 ); |
| 1177 | testcase( pOp->p5==4 ); |
drh | 99f5de7 | 2016-04-30 02:59:15 +0000 | [diff] [blame] | 1178 | sqlite3VdbeError(p, "%s constraint failed", azType[pOp->p5-1]); |
| 1179 | if( pOp->p4.z ){ |
| 1180 | p->zErrMsg = sqlite3MPrintf(db, "%z: %s", p->zErrMsg, pOp->p4.z); |
| 1181 | } |
drh | d9b7ec9 | 2013-11-06 14:05:21 +0000 | [diff] [blame] | 1182 | }else{ |
drh | 22c17b8 | 2015-05-15 04:13:15 +0000 | [diff] [blame] | 1183 | sqlite3VdbeError(p, "%s", pOp->p4.z); |
drh | f9c8ce3 | 2013-11-05 13:33:55 +0000 | [diff] [blame] | 1184 | } |
drh | 8bb93da | 2022-04-03 20:39:48 +0000 | [diff] [blame] | 1185 | pcx = (int)(pOp - aOp); |
drh | 99f5de7 | 2016-04-30 02:59:15 +0000 | [diff] [blame] | 1186 | 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] | 1187 | } |
drh | 92f02c3 | 2004-09-02 14:57:08 +0000 | [diff] [blame] | 1188 | rc = sqlite3VdbeHalt(p); |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 1189 | assert( rc==SQLITE_BUSY || rc==SQLITE_OK || rc==SQLITE_ERROR ); |
drh | 92f02c3 | 2004-09-02 14:57:08 +0000 | [diff] [blame] | 1190 | if( rc==SQLITE_BUSY ){ |
drh | 99f5de7 | 2016-04-30 02:59:15 +0000 | [diff] [blame] | 1191 | p->rc = SQLITE_BUSY; |
drh | 900b31e | 2007-08-28 02:27:51 +0000 | [diff] [blame] | 1192 | }else{ |
drh | d91c1a1 | 2013-02-09 13:58:25 +0000 | [diff] [blame] | 1193 | assert( rc==SQLITE_OK || (p->rc&0xff)==SQLITE_CONSTRAINT ); |
dan | cb3e4b7 | 2013-07-03 19:53:05 +0000 | [diff] [blame] | 1194 | assert( rc==SQLITE_OK || db->nDeferredCons>0 || db->nDeferredImmCons>0 ); |
drh | 900b31e | 2007-08-28 02:27:51 +0000 | [diff] [blame] | 1195 | rc = p->rc ? SQLITE_ERROR : SQLITE_DONE; |
drh | 92f02c3 | 2004-09-02 14:57:08 +0000 | [diff] [blame] | 1196 | } |
drh | 900b31e | 2007-08-28 02:27:51 +0000 | [diff] [blame] | 1197 | goto vdbe_return; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1198 | } |
drh | c61053b | 2000-06-04 12:58:36 +0000 | [diff] [blame] | 1199 | |
drh | 1902516 | 2022-03-03 15:00:44 +0000 | [diff] [blame] | 1200 | /* Opcode: BeginSubrtn P1 P2 * * * |
| 1201 | ** Synopsis: r[P2]=P1 |
| 1202 | ** |
| 1203 | ** Mark the beginning of a subroutine by loading the integer value P1 |
| 1204 | ** into register r[P2]. The P2 register is used to store the return |
| 1205 | ** address of the subroutine call. |
| 1206 | ** |
| 1207 | ** This opcode is identical to OP_Integer. It has a different name |
| 1208 | ** only to make the byte code easier to read and verify. |
| 1209 | */ |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 1210 | /* Opcode: Integer P1 P2 * * * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 1211 | ** Synopsis: r[P2]=P1 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1212 | ** |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1213 | ** The 32-bit integer value P1 is written into register P2. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1214 | */ |
drh | 1902516 | 2022-03-03 15:00:44 +0000 | [diff] [blame] | 1215 | case OP_BeginSubrtn: |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 1216 | case OP_Integer: { /* out2 */ |
| 1217 | pOut = out2Prerelease(p, pOp); |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 1218 | pOut->u.i = pOp->p1; |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 1219 | break; |
| 1220 | } |
| 1221 | |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 1222 | /* Opcode: Int64 * P2 * P4 * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 1223 | ** Synopsis: r[P2]=P4 |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 1224 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 1225 | ** P4 is a pointer to a 64-bit integer value. |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1226 | ** Write that value into register P2. |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 1227 | */ |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 1228 | case OP_Int64: { /* out2 */ |
| 1229 | pOut = out2Prerelease(p, pOp); |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 1230 | assert( pOp->p4.pI64!=0 ); |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 1231 | pOut->u.i = *pOp->p4.pI64; |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 1232 | break; |
| 1233 | } |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 1234 | |
drh | 13573c7 | 2010-01-12 17:04:07 +0000 | [diff] [blame] | 1235 | #ifndef SQLITE_OMIT_FLOATING_POINT |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 1236 | /* Opcode: Real * P2 * P4 * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 1237 | ** Synopsis: r[P2]=P4 |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 1238 | ** |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 1239 | ** P4 is a pointer to a 64-bit floating point value. |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1240 | ** Write that value into register P2. |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 1241 | */ |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 1242 | case OP_Real: { /* same as TK_FLOAT, out2 */ |
| 1243 | pOut = out2Prerelease(p, pOp); |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 1244 | pOut->flags = MEM_Real; |
drh | 2eaf93d | 2008-04-29 00:15:20 +0000 | [diff] [blame] | 1245 | assert( !sqlite3IsNaN(*pOp->p4.pReal) ); |
drh | 74eaba4 | 2014-09-18 17:52:15 +0000 | [diff] [blame] | 1246 | pOut->u.r = *pOp->p4.pReal; |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 1247 | break; |
| 1248 | } |
drh | 13573c7 | 2010-01-12 17:04:07 +0000 | [diff] [blame] | 1249 | #endif |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 1250 | |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 1251 | /* Opcode: String8 * P2 * P4 * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 1252 | ** Synopsis: r[P2]='P4' |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 1253 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 1254 | ** P4 points to a nul terminated UTF-8 string. This opcode is transformed |
drh | f07cf6e | 2015-03-06 16:45:16 +0000 | [diff] [blame] | 1255 | ** into a String opcode before it is executed for the first time. During |
drh | 0fd6135 | 2014-02-07 02:29:45 +0000 | [diff] [blame] | 1256 | ** this transformation, the length of string P4 is computed and stored |
| 1257 | ** as the P1 parameter. |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 1258 | */ |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 1259 | case OP_String8: { /* same as TK_STRING, out2 */ |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 1260 | assert( pOp->p4.z!=0 ); |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 1261 | pOut = out2Prerelease(p, pOp); |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 1262 | pOp->p1 = sqlite3Strlen30(pOp->p4.z); |
drh | ed2df7f | 2005-11-16 04:34:32 +0000 | [diff] [blame] | 1263 | |
| 1264 | #ifndef SQLITE_OMIT_UTF16 |
drh | 8079a0d | 2006-01-12 17:20:50 +0000 | [diff] [blame] | 1265 | if( encoding!=SQLITE_UTF8 ){ |
drh | 3a9cf17 | 2009-06-17 21:42:33 +0000 | [diff] [blame] | 1266 | rc = sqlite3VdbeMemSetStr(pOut, pOp->p4.z, -1, SQLITE_UTF8, SQLITE_STATIC); |
drh | 2f55511 | 2016-04-30 18:10:34 +0000 | [diff] [blame] | 1267 | assert( rc==SQLITE_OK || rc==SQLITE_TOOBIG ); |
drh | dbdddc9 | 2019-02-21 16:41:34 +0000 | [diff] [blame] | 1268 | if( rc ) goto too_big; |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 1269 | if( SQLITE_OK!=sqlite3VdbeChangeEncoding(pOut, encoding) ) goto no_mem; |
drh | 17bcb10 | 2014-09-18 21:25:33 +0000 | [diff] [blame] | 1270 | assert( pOut->szMalloc>0 && pOut->zMalloc==pOut->z ); |
drh | c91b2fd | 2014-03-01 18:13:23 +0000 | [diff] [blame] | 1271 | assert( VdbeMemDynamic(pOut)==0 ); |
drh | 17bcb10 | 2014-09-18 21:25:33 +0000 | [diff] [blame] | 1272 | pOut->szMalloc = 0; |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 1273 | pOut->flags |= MEM_Static; |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 1274 | if( pOp->p4type==P4_DYNAMIC ){ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 1275 | sqlite3DbFree(db, pOp->p4.z); |
danielk1977 | e004840 | 2004-06-15 16:51:01 +0000 | [diff] [blame] | 1276 | } |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 1277 | pOp->p4type = P4_DYNAMIC; |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 1278 | pOp->p4.z = pOut->z; |
| 1279 | pOp->p1 = pOut->n; |
danielk1977 | 0f69c1e | 2004-05-29 11:24:50 +0000 | [diff] [blame] | 1280 | } |
danielk1977 | 93758c8 | 2005-01-21 08:13:14 +0000 | [diff] [blame] | 1281 | #endif |
drh | bb4957f | 2008-03-20 14:03:29 +0000 | [diff] [blame] | 1282 | if( pOp->p1>db->aLimit[SQLITE_LIMIT_LENGTH] ){ |
drh | cbd2da9 | 2007-12-17 16:20:06 +0000 | [diff] [blame] | 1283 | goto too_big; |
| 1284 | } |
drh | ec722c1 | 2019-09-17 21:28:54 +0000 | [diff] [blame] | 1285 | pOp->opcode = OP_String; |
drh | 2f55511 | 2016-04-30 18:10:34 +0000 | [diff] [blame] | 1286 | assert( rc==SQLITE_OK ); |
drh | cbd2da9 | 2007-12-17 16:20:06 +0000 | [diff] [blame] | 1287 | /* Fall through to the next case, OP_String */ |
drh | 08b9208 | 2020-08-10 14:18:00 +0000 | [diff] [blame] | 1288 | /* no break */ deliberate_fall_through |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 1289 | } |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 1290 | |
drh | f07cf6e | 2015-03-06 16:45:16 +0000 | [diff] [blame] | 1291 | /* Opcode: String P1 P2 P3 P4 P5 |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 1292 | ** Synopsis: r[P2]='P4' (len=P1) |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 1293 | ** |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1294 | ** The string value P4 of length P1 (bytes) is stored in register P2. |
drh | f07cf6e | 2015-03-06 16:45:16 +0000 | [diff] [blame] | 1295 | ** |
drh | 44aebff | 2016-05-02 10:25:42 +0000 | [diff] [blame] | 1296 | ** 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] | 1297 | ** the datatype of the register P2 is converted to BLOB. The content is |
| 1298 | ** the same sequence of bytes, it is merely interpreted as a BLOB instead |
drh | 44aebff | 2016-05-02 10:25:42 +0000 | [diff] [blame] | 1299 | ** of a string, as if it had been CAST. In other words: |
| 1300 | ** |
| 1301 | ** 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] | 1302 | */ |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 1303 | case OP_String: { /* out2 */ |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 1304 | assert( pOp->p4.z!=0 ); |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 1305 | pOut = out2Prerelease(p, pOp); |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 1306 | pOut->flags = MEM_Str|MEM_Static|MEM_Term; |
| 1307 | pOut->z = pOp->p4.z; |
| 1308 | pOut->n = pOp->p1; |
| 1309 | pOut->enc = encoding; |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 1310 | UPDATE_MAX_BLOBSIZE(pOut); |
drh | 41d2e66 | 2015-12-01 21:23:07 +0000 | [diff] [blame] | 1311 | #ifndef SQLITE_LIKE_DOESNT_MATCH_BLOBS |
drh | 44aebff | 2016-05-02 10:25:42 +0000 | [diff] [blame] | 1312 | if( pOp->p3>0 ){ |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 1313 | assert( pOp->p3<=(p->nMem+1 - p->nCursor) ); |
drh | f07cf6e | 2015-03-06 16:45:16 +0000 | [diff] [blame] | 1314 | pIn3 = &aMem[pOp->p3]; |
| 1315 | assert( pIn3->flags & MEM_Int ); |
drh | 44aebff | 2016-05-02 10:25:42 +0000 | [diff] [blame] | 1316 | 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] | 1317 | } |
drh | 41d2e66 | 2015-12-01 21:23:07 +0000 | [diff] [blame] | 1318 | #endif |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 1319 | break; |
| 1320 | } |
| 1321 | |
drh | 053a128 | 2012-09-19 21:15:46 +0000 | [diff] [blame] | 1322 | /* Opcode: Null P1 P2 P3 * * |
drh | 72e26de | 2016-08-24 21:24:04 +0000 | [diff] [blame] | 1323 | ** Synopsis: r[P2..P3]=NULL |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 1324 | ** |
drh | b8475df | 2011-12-09 16:21:19 +0000 | [diff] [blame] | 1325 | ** 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] | 1326 | ** 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] | 1327 | ** is less than P2 (typically P3 is zero) then only register P2 is |
drh | 053a128 | 2012-09-19 21:15:46 +0000 | [diff] [blame] | 1328 | ** set to NULL. |
| 1329 | ** |
| 1330 | ** If the P1 value is non-zero, then also set the MEM_Cleared flag so that |
| 1331 | ** NULL values will not compare equal even if SQLITE_NULLEQ is set on |
| 1332 | ** OP_Ne or OP_Eq. |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 1333 | */ |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 1334 | case OP_Null: { /* out2 */ |
drh | b8475df | 2011-12-09 16:21:19 +0000 | [diff] [blame] | 1335 | int cnt; |
drh | 053a128 | 2012-09-19 21:15:46 +0000 | [diff] [blame] | 1336 | u16 nullFlag; |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 1337 | pOut = out2Prerelease(p, pOp); |
drh | b8475df | 2011-12-09 16:21:19 +0000 | [diff] [blame] | 1338 | cnt = pOp->p3-pOp->p2; |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 1339 | assert( pOp->p3<=(p->nMem+1 - p->nCursor) ); |
drh | 053a128 | 2012-09-19 21:15:46 +0000 | [diff] [blame] | 1340 | pOut->flags = nullFlag = pOp->p1 ? (MEM_Null|MEM_Cleared) : MEM_Null; |
drh | 2a1df93 | 2016-09-30 17:46:44 +0000 | [diff] [blame] | 1341 | pOut->n = 0; |
drh | 2c885d0 | 2018-07-07 19:36:04 +0000 | [diff] [blame] | 1342 | #ifdef SQLITE_DEBUG |
| 1343 | pOut->uTemp = 0; |
| 1344 | #endif |
drh | b8475df | 2011-12-09 16:21:19 +0000 | [diff] [blame] | 1345 | while( cnt>0 ){ |
| 1346 | pOut++; |
| 1347 | memAboutToChange(p, pOut); |
drh | 0725cab | 2014-09-17 14:52:46 +0000 | [diff] [blame] | 1348 | sqlite3VdbeMemSetNull(pOut); |
drh | 053a128 | 2012-09-19 21:15:46 +0000 | [diff] [blame] | 1349 | pOut->flags = nullFlag; |
drh | 2a1df93 | 2016-09-30 17:46:44 +0000 | [diff] [blame] | 1350 | pOut->n = 0; |
drh | b8475df | 2011-12-09 16:21:19 +0000 | [diff] [blame] | 1351 | cnt--; |
| 1352 | } |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 1353 | break; |
| 1354 | } |
| 1355 | |
drh | 05a86c5 | 2014-02-16 01:55:49 +0000 | [diff] [blame] | 1356 | /* Opcode: SoftNull P1 * * * * |
drh | 72e26de | 2016-08-24 21:24:04 +0000 | [diff] [blame] | 1357 | ** Synopsis: r[P1]=NULL |
drh | 05a86c5 | 2014-02-16 01:55:49 +0000 | [diff] [blame] | 1358 | ** |
| 1359 | ** Set register P1 to have the value NULL as seen by the OP_MakeRecord |
| 1360 | ** instruction, but do not free any string or blob memory associated with |
| 1361 | ** the register, so that if the value was a string or blob that was |
| 1362 | ** previously copied using OP_SCopy, the copies will continue to be valid. |
| 1363 | */ |
| 1364 | case OP_SoftNull: { |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 1365 | assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) ); |
drh | 05a86c5 | 2014-02-16 01:55:49 +0000 | [diff] [blame] | 1366 | pOut = &aMem[pOp->p1]; |
drh | e2bc655 | 2017-04-17 20:50:34 +0000 | [diff] [blame] | 1367 | pOut->flags = (pOut->flags&~(MEM_Undefined|MEM_AffMask))|MEM_Null; |
drh | 05a86c5 | 2014-02-16 01:55:49 +0000 | [diff] [blame] | 1368 | break; |
| 1369 | } |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 1370 | |
drh | a5750cf | 2014-02-07 13:20:31 +0000 | [diff] [blame] | 1371 | /* Opcode: Blob P1 P2 * P4 * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 1372 | ** Synopsis: r[P2]=P4 (len=P1) |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 1373 | ** |
drh | 9de221d | 2008-01-05 06:51:30 +0000 | [diff] [blame] | 1374 | ** P4 points to a blob of data P1 bytes long. Store this |
drh | 50fb7e0 | 2021-12-06 20:16:53 +0000 | [diff] [blame] | 1375 | ** blob in register P2. If P4 is a NULL pointer, then construct |
| 1376 | ** a zero-filled blob that is P1 bytes long in P2. |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 1377 | */ |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 1378 | case OP_Blob: { /* out2 */ |
drh | cbd2da9 | 2007-12-17 16:20:06 +0000 | [diff] [blame] | 1379 | assert( pOp->p1 <= SQLITE_MAX_LENGTH ); |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 1380 | pOut = out2Prerelease(p, pOp); |
drh | 50fb7e0 | 2021-12-06 20:16:53 +0000 | [diff] [blame] | 1381 | if( pOp->p4.z==0 ){ |
| 1382 | sqlite3VdbeMemSetZeroBlob(pOut, pOp->p1); |
| 1383 | if( sqlite3VdbeMemExpandBlob(pOut) ) goto no_mem; |
| 1384 | }else{ |
| 1385 | sqlite3VdbeMemSetStr(pOut, pOp->p4.z, pOp->p1, 0, 0); |
| 1386 | } |
drh | 9de221d | 2008-01-05 06:51:30 +0000 | [diff] [blame] | 1387 | pOut->enc = encoding; |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 1388 | UPDATE_MAX_BLOBSIZE(pOut); |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 1389 | break; |
| 1390 | } |
| 1391 | |
drh | eaf52d8 | 2010-05-12 13:50:23 +0000 | [diff] [blame] | 1392 | /* Opcode: Variable P1 P2 * P4 * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 1393 | ** Synopsis: r[P2]=parameter(P1,P4) |
drh | 5045789 | 2003-09-06 01:10:47 +0000 | [diff] [blame] | 1394 | ** |
drh | eaf52d8 | 2010-05-12 13:50:23 +0000 | [diff] [blame] | 1395 | ** Transfer the values of bound parameter P1 into register P2 |
drh | 08de149 | 2009-02-20 03:55:05 +0000 | [diff] [blame] | 1396 | ** |
drh | 0fd6135 | 2014-02-07 02:29:45 +0000 | [diff] [blame] | 1397 | ** If the parameter is named, then its name appears in P4. |
drh | 08de149 | 2009-02-20 03:55:05 +0000 | [diff] [blame] | 1398 | ** The P4 value is used by sqlite3_bind_parameter_name(). |
drh | 5045789 | 2003-09-06 01:10:47 +0000 | [diff] [blame] | 1399 | */ |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 1400 | case OP_Variable: { /* out2 */ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1401 | Mem *pVar; /* Value being transferred */ |
| 1402 | |
drh | eaf52d8 | 2010-05-12 13:50:23 +0000 | [diff] [blame] | 1403 | assert( pOp->p1>0 && pOp->p1<=p->nVar ); |
drh | 9bf755c | 2016-12-23 03:59:31 +0000 | [diff] [blame] | 1404 | assert( pOp->p4.z==0 || pOp->p4.z==sqlite3VListNumToName(p->pVList,pOp->p1) ); |
drh | eaf52d8 | 2010-05-12 13:50:23 +0000 | [diff] [blame] | 1405 | pVar = &p->aVar[pOp->p1 - 1]; |
| 1406 | if( sqlite3VdbeMemTooBig(pVar) ){ |
| 1407 | goto too_big; |
drh | 023ae03 | 2007-05-08 12:12:16 +0000 | [diff] [blame] | 1408 | } |
drh | 7441df7 | 2017-01-09 19:27:04 +0000 | [diff] [blame] | 1409 | pOut = &aMem[pOp->p2]; |
drh | e0f20b4 | 2019-04-01 20:57:11 +0000 | [diff] [blame] | 1410 | if( VdbeMemDynamic(pOut) ) sqlite3VdbeMemSetNull(pOut); |
| 1411 | memcpy(pOut, pVar, MEMCELLSIZE); |
| 1412 | pOut->flags &= ~(MEM_Dyn|MEM_Ephem); |
| 1413 | pOut->flags |= MEM_Static|MEM_FromBind; |
drh | eaf52d8 | 2010-05-12 13:50:23 +0000 | [diff] [blame] | 1414 | UPDATE_MAX_BLOBSIZE(pOut); |
danielk1977 | 93d4675 | 2004-05-23 13:30:58 +0000 | [diff] [blame] | 1415 | break; |
| 1416 | } |
danielk1977 | 295ba55 | 2004-05-19 10:34:51 +0000 | [diff] [blame] | 1417 | |
drh | b21e7c7 | 2008-06-22 12:37:57 +0000 | [diff] [blame] | 1418 | /* Opcode: Move P1 P2 P3 * * |
drh | 72e26de | 2016-08-24 21:24:04 +0000 | [diff] [blame] | 1419 | ** Synopsis: r[P2@P3]=r[P1@P3] |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1420 | ** |
drh | 079a307 | 2014-03-19 14:10:55 +0000 | [diff] [blame] | 1421 | ** Move the P3 values in register P1..P1+P3-1 over into |
| 1422 | ** registers P2..P2+P3-1. Registers P1..P1+P3-1 are |
drh | b21e7c7 | 2008-06-22 12:37:57 +0000 | [diff] [blame] | 1423 | ** left holding a NULL. It is an error for register ranges |
drh | 079a307 | 2014-03-19 14:10:55 +0000 | [diff] [blame] | 1424 | ** P1..P1+P3-1 and P2..P2+P3-1 to overlap. It is an error |
| 1425 | ** for P3 to be less than 1. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1426 | */ |
drh | e1349cb | 2008-04-01 00:36:10 +0000 | [diff] [blame] | 1427 | case OP_Move: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1428 | int n; /* Number of registers left to copy */ |
| 1429 | int p1; /* Register to copy from */ |
| 1430 | int p2; /* Register to copy to */ |
| 1431 | |
drh | e09f43f | 2013-11-21 04:18:31 +0000 | [diff] [blame] | 1432 | n = pOp->p3; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1433 | p1 = pOp->p1; |
| 1434 | p2 = pOp->p2; |
drh | 079a307 | 2014-03-19 14:10:55 +0000 | [diff] [blame] | 1435 | assert( n>0 && p1>0 && p2>0 ); |
drh | b21e7c7 | 2008-06-22 12:37:57 +0000 | [diff] [blame] | 1436 | assert( p1+n<=p2 || p2+n<=p1 ); |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1437 | |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 1438 | pIn1 = &aMem[p1]; |
| 1439 | pOut = &aMem[p2]; |
drh | e09f43f | 2013-11-21 04:18:31 +0000 | [diff] [blame] | 1440 | do{ |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 1441 | assert( pOut<=&aMem[(p->nMem+1 - p->nCursor)] ); |
| 1442 | assert( pIn1<=&aMem[(p->nMem+1 - p->nCursor)] ); |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 1443 | assert( memIsValid(pIn1) ); |
| 1444 | memAboutToChange(p, pOut); |
drh | 17bcb10 | 2014-09-18 21:25:33 +0000 | [diff] [blame] | 1445 | sqlite3VdbeMemMove(pOut, pIn1); |
drh | 52043d7 | 2011-08-03 16:40:15 +0000 | [diff] [blame] | 1446 | #ifdef SQLITE_DEBUG |
drh | 4cbd847 | 2020-01-02 15:02:08 +0000 | [diff] [blame] | 1447 | pIn1->pScopyFrom = 0; |
| 1448 | { int i; |
| 1449 | for(i=1; i<p->nMem; i++){ |
| 1450 | if( aMem[i].pScopyFrom==pIn1 ){ |
| 1451 | aMem[i].pScopyFrom = pOut; |
| 1452 | } |
| 1453 | } |
drh | 52043d7 | 2011-08-03 16:40:15 +0000 | [diff] [blame] | 1454 | } |
| 1455 | #endif |
drh | bd6789e | 2015-04-28 14:00:02 +0000 | [diff] [blame] | 1456 | Deephemeralize(pOut); |
drh | b21e7c7 | 2008-06-22 12:37:57 +0000 | [diff] [blame] | 1457 | REGISTER_TRACE(p2++, pOut); |
| 1458 | pIn1++; |
| 1459 | pOut++; |
drh | 079a307 | 2014-03-19 14:10:55 +0000 | [diff] [blame] | 1460 | }while( --n ); |
drh | e1349cb | 2008-04-01 00:36:10 +0000 | [diff] [blame] | 1461 | break; |
| 1462 | } |
| 1463 | |
drh | e8e4af7 | 2012-09-21 00:04:28 +0000 | [diff] [blame] | 1464 | /* Opcode: Copy P1 P2 P3 * * |
drh | 4eded60 | 2013-12-20 15:59:20 +0000 | [diff] [blame] | 1465 | ** Synopsis: r[P2@P3+1]=r[P1@P3+1] |
drh | b1fdb2a | 2008-01-05 04:06:03 +0000 | [diff] [blame] | 1466 | ** |
drh | e8e4af7 | 2012-09-21 00:04:28 +0000 | [diff] [blame] | 1467 | ** Make a copy of registers P1..P1+P3 into registers P2..P2+P3. |
drh | b1fdb2a | 2008-01-05 04:06:03 +0000 | [diff] [blame] | 1468 | ** |
| 1469 | ** This instruction makes a deep copy of the value. A duplicate |
| 1470 | ** is made of any string or blob constant. See also OP_SCopy. |
| 1471 | */ |
drh | e8e4af7 | 2012-09-21 00:04:28 +0000 | [diff] [blame] | 1472 | case OP_Copy: { |
| 1473 | int n; |
| 1474 | |
| 1475 | n = pOp->p3; |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 1476 | pIn1 = &aMem[pOp->p1]; |
| 1477 | pOut = &aMem[pOp->p2]; |
drh | e1349cb | 2008-04-01 00:36:10 +0000 | [diff] [blame] | 1478 | assert( pOut!=pIn1 ); |
drh | e8e4af7 | 2012-09-21 00:04:28 +0000 | [diff] [blame] | 1479 | while( 1 ){ |
drh | 58773a5 | 2018-06-12 13:52:23 +0000 | [diff] [blame] | 1480 | memAboutToChange(p, pOut); |
drh | e8e4af7 | 2012-09-21 00:04:28 +0000 | [diff] [blame] | 1481 | sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem); |
| 1482 | Deephemeralize(pOut); |
drh | 953f761 | 2012-12-07 22:18:54 +0000 | [diff] [blame] | 1483 | #ifdef SQLITE_DEBUG |
| 1484 | pOut->pScopyFrom = 0; |
| 1485 | #endif |
drh | e8e4af7 | 2012-09-21 00:04:28 +0000 | [diff] [blame] | 1486 | REGISTER_TRACE(pOp->p2+pOp->p3-n, pOut); |
| 1487 | if( (n--)==0 ) break; |
| 1488 | pOut++; |
| 1489 | pIn1++; |
| 1490 | } |
drh | e1349cb | 2008-04-01 00:36:10 +0000 | [diff] [blame] | 1491 | break; |
| 1492 | } |
| 1493 | |
drh | b1fdb2a | 2008-01-05 04:06:03 +0000 | [diff] [blame] | 1494 | /* Opcode: SCopy P1 P2 * * * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 1495 | ** Synopsis: r[P2]=r[P1] |
drh | b1fdb2a | 2008-01-05 04:06:03 +0000 | [diff] [blame] | 1496 | ** |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1497 | ** Make a shallow copy of register P1 into register P2. |
drh | b1fdb2a | 2008-01-05 04:06:03 +0000 | [diff] [blame] | 1498 | ** |
| 1499 | ** This instruction makes a shallow copy of the value. If the value |
| 1500 | ** is a string or blob, then the copy is only a pointer to the |
| 1501 | ** original and hence if the original changes so will the copy. |
| 1502 | ** Worse, if the original is deallocated, the copy becomes invalid. |
| 1503 | ** Thus the program must guarantee that the original will not change |
| 1504 | ** during the lifetime of the copy. Use OP_Copy to make a complete |
| 1505 | ** copy. |
| 1506 | */ |
drh | 26198bb | 2013-10-31 11:15:09 +0000 | [diff] [blame] | 1507 | case OP_SCopy: { /* out2 */ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 1508 | pIn1 = &aMem[pOp->p1]; |
| 1509 | pOut = &aMem[pOp->p2]; |
drh | 2d401ab | 2008-01-10 23:50:11 +0000 | [diff] [blame] | 1510 | assert( pOut!=pIn1 ); |
drh | e1349cb | 2008-04-01 00:36:10 +0000 | [diff] [blame] | 1511 | sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem); |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 1512 | #ifdef SQLITE_DEBUG |
drh | 58773a5 | 2018-06-12 13:52:23 +0000 | [diff] [blame] | 1513 | pOut->pScopyFrom = pIn1; |
| 1514 | pOut->mScopyFlags = pIn1->flags; |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 1515 | #endif |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1516 | break; |
| 1517 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1518 | |
drh | fed7ac6 | 2015-10-15 18:04:59 +0000 | [diff] [blame] | 1519 | /* Opcode: IntCopy P1 P2 * * * |
| 1520 | ** Synopsis: r[P2]=r[P1] |
| 1521 | ** |
| 1522 | ** Transfer the integer value held in register P1 into register P2. |
| 1523 | ** |
| 1524 | ** This is an optimized version of SCopy that works only for integer |
| 1525 | ** values. |
| 1526 | */ |
| 1527 | case OP_IntCopy: { /* out2 */ |
| 1528 | pIn1 = &aMem[pOp->p1]; |
| 1529 | assert( (pIn1->flags & MEM_Int)!=0 ); |
| 1530 | pOut = &aMem[pOp->p2]; |
| 1531 | sqlite3VdbeMemSetInt64(pOut, pIn1->u.i); |
| 1532 | break; |
| 1533 | } |
| 1534 | |
drh | 3b26b2b | 2021-12-01 19:17:14 +0000 | [diff] [blame] | 1535 | /* Opcode: FkCheck * * * * * |
drh | 18e5607 | 2021-01-31 15:50:36 +0000 | [diff] [blame] | 1536 | ** |
drh | 3b26b2b | 2021-12-01 19:17:14 +0000 | [diff] [blame] | 1537 | ** Halt with an SQLITE_CONSTRAINT error if there are any unresolved |
| 1538 | ** foreign key constraint violations. If there are no foreign key |
| 1539 | ** constraint violations, this is a no-op. |
drh | 18e5607 | 2021-01-31 15:50:36 +0000 | [diff] [blame] | 1540 | ** |
drh | 3b26b2b | 2021-12-01 19:17:14 +0000 | [diff] [blame] | 1541 | ** FK constraint violations are also checked when the prepared statement |
| 1542 | ** exits. This opcode is used to raise foreign key constraint errors prior |
| 1543 | ** to returning results such as a row change count or the result of a |
| 1544 | ** RETURNING clause. |
drh | 18e5607 | 2021-01-31 15:50:36 +0000 | [diff] [blame] | 1545 | */ |
drh | 3b26b2b | 2021-12-01 19:17:14 +0000 | [diff] [blame] | 1546 | case OP_FkCheck: { |
drh | 18e5607 | 2021-01-31 15:50:36 +0000 | [diff] [blame] | 1547 | if( (rc = sqlite3VdbeCheckFk(p,0))!=SQLITE_OK ){ |
| 1548 | goto abort_due_to_error; |
| 1549 | } |
drh | 3b26b2b | 2021-12-01 19:17:14 +0000 | [diff] [blame] | 1550 | break; |
drh | 18e5607 | 2021-01-31 15:50:36 +0000 | [diff] [blame] | 1551 | } |
| 1552 | |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1553 | /* Opcode: ResultRow P1 P2 * * * |
drh | 72e26de | 2016-08-24 21:24:04 +0000 | [diff] [blame] | 1554 | ** Synopsis: output=r[P1@P2] |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 1555 | ** |
shane | 21e7feb | 2008-05-30 15:59:49 +0000 | [diff] [blame] | 1556 | ** The registers P1 through P1+P2-1 contain a single row of |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 1557 | ** results. This opcode causes the sqlite3_step() call to terminate |
| 1558 | ** with an SQLITE_ROW return code and it sets up the sqlite3_stmt |
drh | 4d87aae | 2014-02-20 19:42:00 +0000 | [diff] [blame] | 1559 | ** 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] | 1560 | ** the result row. |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 1561 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1562 | case OP_ResultRow: { |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 1563 | assert( p->nResColumn==pOp->p2 ); |
drh | 9ce612a | 2021-04-05 22:30:56 +0000 | [diff] [blame] | 1564 | assert( pOp->p1>0 || CORRUPT_DB ); |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 1565 | assert( pOp->p1+pOp->p2<=(p->nMem+1 - p->nCursor)+1 ); |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 1566 | |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 1567 | p->cacheCtr = (p->cacheCtr + 2)|1; |
drh | 3b8b5be | 2022-04-01 20:19:36 +0000 | [diff] [blame] | 1568 | p->pResultSet = &aMem[pOp->p1]; |
drh | 02ff747 | 2019-12-31 12:18:24 +0000 | [diff] [blame] | 1569 | #ifdef SQLITE_DEBUG |
drh | 3b8b5be | 2022-04-01 20:19:36 +0000 | [diff] [blame] | 1570 | { |
| 1571 | Mem *pMem = p->pResultSet; |
| 1572 | int i; |
| 1573 | for(i=0; i<pOp->p2; i++){ |
| 1574 | assert( memIsValid(&pMem[i]) ); |
| 1575 | REGISTER_TRACE(pOp->p1+i, &pMem[i]); |
| 1576 | /* The registers in the result will not be used again when the |
| 1577 | ** prepared statement restarts. This is because sqlite3_column() |
| 1578 | ** APIs might have caused type conversions of made other changes to |
| 1579 | ** the register values. Therefore, we can go ahead and break any |
| 1580 | ** OP_SCopy dependencies. */ |
| 1581 | pMem[i].pScopyFrom = 0; |
| 1582 | } |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 1583 | } |
drh | 3b8b5be | 2022-04-01 20:19:36 +0000 | [diff] [blame] | 1584 | #endif |
drh | 2803969 | 2008-03-17 16:54:01 +0000 | [diff] [blame] | 1585 | if( db->mallocFailed ) goto no_mem; |
drh | 3d2a529 | 2016-07-13 22:55:01 +0000 | [diff] [blame] | 1586 | if( db->mTrace & SQLITE_TRACE_ROW ){ |
drh | 08b9208 | 2020-08-10 14:18:00 +0000 | [diff] [blame] | 1587 | db->trace.xV2(SQLITE_TRACE_ROW, db->pTraceArg, p, 0); |
drh | 3d2a529 | 2016-07-13 22:55:01 +0000 | [diff] [blame] | 1588 | } |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 1589 | p->pc = (int)(pOp - aOp) + 1; |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 1590 | rc = SQLITE_ROW; |
| 1591 | goto vdbe_return; |
| 1592 | } |
| 1593 | |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1594 | /* Opcode: Concat P1 P2 P3 * * |
drh | 313619f | 2013-10-31 20:34:06 +0000 | [diff] [blame] | 1595 | ** Synopsis: r[P3]=r[P2]+r[P1] |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1596 | ** |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1597 | ** Add the text in register P1 onto the end of the text in |
| 1598 | ** register P2 and store the result in register P3. |
| 1599 | ** 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] | 1600 | ** |
| 1601 | ** P3 = P2 || P1 |
| 1602 | ** |
| 1603 | ** It is illegal for P1 and P3 to be the same register. Sometimes, |
| 1604 | ** if P3 is the same register as P2, the implementation is able |
| 1605 | ** to avoid a memcpy(). |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1606 | */ |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1607 | case OP_Concat: { /* same as TK_CONCAT, in1, in2, out3 */ |
drh | 8a7e11f | 2019-05-01 15:32:40 +0000 | [diff] [blame] | 1608 | i64 nByte; /* Total size of the output string or blob */ |
| 1609 | u16 flags1; /* Initial flags for P1 */ |
| 1610 | u16 flags2; /* Initial flags for P2 */ |
danielk1977 | 8a6b541 | 2004-05-24 07:04:25 +0000 | [diff] [blame] | 1611 | |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 1612 | pIn1 = &aMem[pOp->p1]; |
| 1613 | pIn2 = &aMem[pOp->p2]; |
| 1614 | pOut = &aMem[pOp->p3]; |
drh | 8a7e11f | 2019-05-01 15:32:40 +0000 | [diff] [blame] | 1615 | testcase( pOut==pIn2 ); |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1616 | assert( pIn1!=pOut ); |
drh | 8a7e11f | 2019-05-01 15:32:40 +0000 | [diff] [blame] | 1617 | flags1 = pIn1->flags; |
| 1618 | testcase( flags1 & MEM_Null ); |
| 1619 | testcase( pIn2->flags & MEM_Null ); |
| 1620 | if( (flags1 | pIn2->flags) & MEM_Null ){ |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1621 | sqlite3VdbeMemSetNull(pOut); |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1622 | break; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1623 | } |
drh | 8a7e11f | 2019-05-01 15:32:40 +0000 | [diff] [blame] | 1624 | if( (flags1 & (MEM_Str|MEM_Blob))==0 ){ |
| 1625 | if( sqlite3VdbeMemStringify(pIn1,encoding,0) ) goto no_mem; |
drh | 01325a3 | 2019-05-02 00:52:50 +0000 | [diff] [blame] | 1626 | flags1 = pIn1->flags & ~MEM_Str; |
drh | 8a7e11f | 2019-05-01 15:32:40 +0000 | [diff] [blame] | 1627 | }else if( (flags1 & MEM_Zero)!=0 ){ |
| 1628 | if( sqlite3VdbeMemExpandBlob(pIn1) ) goto no_mem; |
drh | 01325a3 | 2019-05-02 00:52:50 +0000 | [diff] [blame] | 1629 | flags1 = pIn1->flags & ~MEM_Str; |
drh | 8a7e11f | 2019-05-01 15:32:40 +0000 | [diff] [blame] | 1630 | } |
| 1631 | flags2 = pIn2->flags; |
| 1632 | if( (flags2 & (MEM_Str|MEM_Blob))==0 ){ |
| 1633 | if( sqlite3VdbeMemStringify(pIn2,encoding,0) ) goto no_mem; |
drh | 01325a3 | 2019-05-02 00:52:50 +0000 | [diff] [blame] | 1634 | flags2 = pIn2->flags & ~MEM_Str; |
drh | 8a7e11f | 2019-05-01 15:32:40 +0000 | [diff] [blame] | 1635 | }else if( (flags2 & MEM_Zero)!=0 ){ |
| 1636 | if( sqlite3VdbeMemExpandBlob(pIn2) ) goto no_mem; |
drh | 01325a3 | 2019-05-02 00:52:50 +0000 | [diff] [blame] | 1637 | flags2 = pIn2->flags & ~MEM_Str; |
drh | 8a7e11f | 2019-05-01 15:32:40 +0000 | [diff] [blame] | 1638 | } |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1639 | nByte = pIn1->n + pIn2->n; |
drh | bb4957f | 2008-03-20 14:03:29 +0000 | [diff] [blame] | 1640 | if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){ |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1641 | goto too_big; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1642 | } |
drh | df82afc | 2019-05-16 01:22:21 +0000 | [diff] [blame] | 1643 | if( sqlite3VdbeMemGrow(pOut, (int)nByte+3, pOut==pIn2) ){ |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1644 | goto no_mem; |
| 1645 | } |
drh | c91b2fd | 2014-03-01 18:13:23 +0000 | [diff] [blame] | 1646 | MemSetTypeFlag(pOut, MEM_Str); |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1647 | if( pOut!=pIn2 ){ |
| 1648 | memcpy(pOut->z, pIn2->z, pIn2->n); |
drh | 8a7e11f | 2019-05-01 15:32:40 +0000 | [diff] [blame] | 1649 | assert( (pIn2->flags & MEM_Dyn) == (flags2 & MEM_Dyn) ); |
| 1650 | pIn2->flags = flags2; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1651 | } |
| 1652 | memcpy(&pOut->z[pIn2->n], pIn1->z, pIn1->n); |
drh | 8a7e11f | 2019-05-01 15:32:40 +0000 | [diff] [blame] | 1653 | assert( (pIn1->flags & MEM_Dyn) == (flags1 & MEM_Dyn) ); |
| 1654 | pIn1->flags = flags1; |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 1655 | pOut->z[nByte]=0; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1656 | pOut->z[nByte+1] = 0; |
drh | df82afc | 2019-05-16 01:22:21 +0000 | [diff] [blame] | 1657 | pOut->z[nByte+2] = 0; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1658 | pOut->flags |= MEM_Term; |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 1659 | pOut->n = (int)nByte; |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1660 | pOut->enc = encoding; |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 1661 | UPDATE_MAX_BLOBSIZE(pOut); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1662 | break; |
| 1663 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1664 | |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 1665 | /* Opcode: Add P1 P2 P3 * * |
drh | 72e26de | 2016-08-24 21:24:04 +0000 | [diff] [blame] | 1666 | ** Synopsis: r[P3]=r[P1]+r[P2] |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1667 | ** |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1668 | ** Add the value in register P1 to the value in register P2 |
shane | 21e7feb | 2008-05-30 15:59:49 +0000 | [diff] [blame] | 1669 | ** and store the result in register P3. |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1670 | ** If either input is NULL, the result is NULL. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1671 | */ |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 1672 | /* Opcode: Multiply P1 P2 P3 * * |
drh | 72e26de | 2016-08-24 21:24:04 +0000 | [diff] [blame] | 1673 | ** Synopsis: r[P3]=r[P1]*r[P2] |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1674 | ** |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 1675 | ** |
shane | 21e7feb | 2008-05-30 15:59:49 +0000 | [diff] [blame] | 1676 | ** Multiply the value in register P1 by the value in register P2 |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1677 | ** and store the result in register P3. |
| 1678 | ** If either input is NULL, the result is NULL. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1679 | */ |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 1680 | /* Opcode: Subtract P1 P2 P3 * * |
drh | 72e26de | 2016-08-24 21:24:04 +0000 | [diff] [blame] | 1681 | ** Synopsis: r[P3]=r[P2]-r[P1] |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1682 | ** |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1683 | ** Subtract the value in register P1 from the value in register P2 |
| 1684 | ** and store the result in register P3. |
| 1685 | ** If either input is NULL, the result is NULL. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1686 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1687 | /* Opcode: Divide P1 P2 P3 * * |
drh | 72e26de | 2016-08-24 21:24:04 +0000 | [diff] [blame] | 1688 | ** Synopsis: r[P3]=r[P2]/r[P1] |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1689 | ** |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1690 | ** Divide the value in register P1 by the value in register P2 |
dan | e275dc3 | 2009-08-18 16:24:58 +0000 | [diff] [blame] | 1691 | ** and store the result in register P3 (P3=P2/P1). If the value in |
| 1692 | ** register P1 is zero, then the result is NULL. If either input is |
| 1693 | ** NULL, the result is NULL. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1694 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1695 | /* Opcode: Remainder P1 P2 P3 * * |
drh | 72e26de | 2016-08-24 21:24:04 +0000 | [diff] [blame] | 1696 | ** Synopsis: r[P3]=r[P2]%r[P1] |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1697 | ** |
drh | 40864a1 | 2013-11-15 18:58:37 +0000 | [diff] [blame] | 1698 | ** Compute the remainder after integer register P2 is divided by |
| 1699 | ** register P1 and store the result in register P3. |
| 1700 | ** If the value in register P1 is zero the result is NULL. |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1701 | ** If either operand is NULL, the result is NULL. |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1702 | */ |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1703 | case OP_Add: /* same as TK_PLUS, in1, in2, out3 */ |
| 1704 | case OP_Subtract: /* same as TK_MINUS, in1, in2, out3 */ |
| 1705 | case OP_Multiply: /* same as TK_STAR, in1, in2, out3 */ |
| 1706 | case OP_Divide: /* same as TK_SLASH, in1, in2, out3 */ |
| 1707 | case OP_Remainder: { /* same as TK_REM, in1, in2, out3 */ |
drh | 3d1d90a | 2014-03-24 15:00:15 +0000 | [diff] [blame] | 1708 | u16 flags; /* Combined MEM_* flags from both inputs */ |
| 1709 | u16 type1; /* Numeric type of left operand */ |
| 1710 | u16 type2; /* Numeric type of right operand */ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1711 | i64 iA; /* Integer value of left operand */ |
| 1712 | i64 iB; /* Integer value of right operand */ |
| 1713 | double rA; /* Real value of left operand */ |
| 1714 | double rB; /* Real value of right operand */ |
| 1715 | |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 1716 | pIn1 = &aMem[pOp->p1]; |
drh | 3d1d90a | 2014-03-24 15:00:15 +0000 | [diff] [blame] | 1717 | type1 = numericType(pIn1); |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 1718 | pIn2 = &aMem[pOp->p2]; |
drh | 3d1d90a | 2014-03-24 15:00:15 +0000 | [diff] [blame] | 1719 | type2 = numericType(pIn2); |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 1720 | pOut = &aMem[pOp->p3]; |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1721 | flags = pIn1->flags | pIn2->flags; |
drh | 3d1d90a | 2014-03-24 15:00:15 +0000 | [diff] [blame] | 1722 | if( (type1 & type2 & MEM_Int)!=0 ){ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1723 | iA = pIn1->u.i; |
| 1724 | iB = pIn2->u.i; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1725 | switch( pOp->opcode ){ |
drh | 158b9cb | 2011-03-05 20:59:46 +0000 | [diff] [blame] | 1726 | case OP_Add: if( sqlite3AddInt64(&iB,iA) ) goto fp_math; break; |
| 1727 | case OP_Subtract: if( sqlite3SubInt64(&iB,iA) ) goto fp_math; break; |
| 1728 | case OP_Multiply: if( sqlite3MulInt64(&iB,iA) ) goto fp_math; break; |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1729 | case OP_Divide: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1730 | if( iA==0 ) goto arithmetic_result_is_null; |
drh | 158b9cb | 2011-03-05 20:59:46 +0000 | [diff] [blame] | 1731 | if( iA==-1 && iB==SMALLEST_INT64 ) goto fp_math; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1732 | iB /= iA; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1733 | break; |
| 1734 | } |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1735 | default: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1736 | if( iA==0 ) goto arithmetic_result_is_null; |
| 1737 | if( iA==-1 ) iA = 1; |
| 1738 | iB %= iA; |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1739 | break; |
| 1740 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1741 | } |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1742 | pOut->u.i = iB; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1743 | MemSetTypeFlag(pOut, MEM_Int); |
drh | cfcca02 | 2017-04-17 23:23:17 +0000 | [diff] [blame] | 1744 | }else if( (flags & MEM_Null)!=0 ){ |
| 1745 | goto arithmetic_result_is_null; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1746 | }else{ |
drh | 158b9cb | 2011-03-05 20:59:46 +0000 | [diff] [blame] | 1747 | fp_math: |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1748 | rA = sqlite3VdbeRealValue(pIn1); |
| 1749 | rB = sqlite3VdbeRealValue(pIn2); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1750 | switch( pOp->opcode ){ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1751 | case OP_Add: rB += rA; break; |
| 1752 | case OP_Subtract: rB -= rA; break; |
| 1753 | case OP_Multiply: rB *= rA; break; |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1754 | case OP_Divide: { |
shane | fbd60f8 | 2009-02-04 03:59:25 +0000 | [diff] [blame] | 1755 | /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1756 | if( rA==(double)0 ) goto arithmetic_result_is_null; |
| 1757 | rB /= rA; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1758 | break; |
| 1759 | } |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1760 | default: { |
drh | e3b89d2 | 2019-01-18 17:53:50 +0000 | [diff] [blame] | 1761 | iA = sqlite3VdbeIntValue(pIn1); |
| 1762 | iB = sqlite3VdbeIntValue(pIn2); |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1763 | if( iA==0 ) goto arithmetic_result_is_null; |
| 1764 | if( iA==-1 ) iA = 1; |
| 1765 | rB = (double)(iB % iA); |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1766 | break; |
| 1767 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1768 | } |
drh | c5a7b51 | 2010-01-13 16:25:42 +0000 | [diff] [blame] | 1769 | #ifdef SQLITE_OMIT_FLOATING_POINT |
| 1770 | pOut->u.i = rB; |
| 1771 | MemSetTypeFlag(pOut, MEM_Int); |
| 1772 | #else |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1773 | if( sqlite3IsNaN(rB) ){ |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 1774 | goto arithmetic_result_is_null; |
drh | 53c1402 | 2007-05-10 17:23:11 +0000 | [diff] [blame] | 1775 | } |
drh | 74eaba4 | 2014-09-18 17:52:15 +0000 | [diff] [blame] | 1776 | pOut->u.r = rB; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1777 | MemSetTypeFlag(pOut, MEM_Real); |
drh | c5a7b51 | 2010-01-13 16:25:42 +0000 | [diff] [blame] | 1778 | #endif |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1779 | } |
| 1780 | break; |
| 1781 | |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 1782 | arithmetic_result_is_null: |
| 1783 | sqlite3VdbeMemSetNull(pOut); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1784 | break; |
| 1785 | } |
| 1786 | |
drh | 7a95789 | 2012-02-02 17:35:43 +0000 | [diff] [blame] | 1787 | /* Opcode: CollSeq P1 * * P4 |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 1788 | ** |
drh | bb6783b | 2017-04-29 18:02:49 +0000 | [diff] [blame] | 1789 | ** 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] | 1790 | ** or aggregate calls sqlite3GetFuncCollSeq(), this collation sequence will |
| 1791 | ** be returned. This is used by the built-in min(), max() and nullif() |
drh | e6f85e7 | 2004-12-25 01:03:13 +0000 | [diff] [blame] | 1792 | ** functions. |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 1793 | ** |
drh | 7a95789 | 2012-02-02 17:35:43 +0000 | [diff] [blame] | 1794 | ** If P1 is not zero, then it is a register that a subsequent min() or |
| 1795 | ** max() aggregate will set to 1 if the current row is not the minimum or |
| 1796 | ** maximum. The P1 register is initialized to 0 by this instruction. |
| 1797 | ** |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 1798 | ** The interface used by the implementation of the aforementioned functions |
| 1799 | ** to retrieve the collation sequence set by this opcode is not available |
drh | 0a0d056 | 2015-03-12 05:08:34 +0000 | [diff] [blame] | 1800 | ** publicly. Only built-in functions have access to this feature. |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 1801 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1802 | case OP_CollSeq: { |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 1803 | assert( pOp->p4type==P4_COLLSEQ ); |
drh | 7a95789 | 2012-02-02 17:35:43 +0000 | [diff] [blame] | 1804 | if( pOp->p1 ){ |
| 1805 | sqlite3VdbeMemSetInt64(&aMem[pOp->p1], 0); |
| 1806 | } |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 1807 | break; |
| 1808 | } |
| 1809 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1810 | /* Opcode: BitAnd P1 P2 P3 * * |
drh | 72e26de | 2016-08-24 21:24:04 +0000 | [diff] [blame] | 1811 | ** Synopsis: r[P3]=r[P1]&r[P2] |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1812 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1813 | ** Take the bit-wise AND of the values in register P1 and P2 and |
| 1814 | ** store the result in register P3. |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1815 | ** If either input is NULL, the result is NULL. |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1816 | */ |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1817 | /* Opcode: BitOr P1 P2 P3 * * |
drh | 72e26de | 2016-08-24 21:24:04 +0000 | [diff] [blame] | 1818 | ** Synopsis: r[P3]=r[P1]|r[P2] |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1819 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1820 | ** Take the bit-wise OR of the values in register P1 and P2 and |
| 1821 | ** store the result in register P3. |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1822 | ** If either input is NULL, the result is NULL. |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1823 | */ |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1824 | /* Opcode: ShiftLeft P1 P2 P3 * * |
drh | 72e26de | 2016-08-24 21:24:04 +0000 | [diff] [blame] | 1825 | ** Synopsis: r[P3]=r[P2]<<r[P1] |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1826 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1827 | ** Shift the integer value in register P2 to the left by the |
drh | 710c484 | 2010-08-30 01:17:20 +0000 | [diff] [blame] | 1828 | ** number of bits specified by the integer in register P1. |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1829 | ** Store the result in register P3. |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1830 | ** If either input is NULL, the result is NULL. |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1831 | */ |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1832 | /* Opcode: ShiftRight P1 P2 P3 * * |
drh | 72e26de | 2016-08-24 21:24:04 +0000 | [diff] [blame] | 1833 | ** Synopsis: r[P3]=r[P2]>>r[P1] |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1834 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1835 | ** Shift the integer value in register P2 to the right by the |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1836 | ** number of bits specified by the integer in register P1. |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1837 | ** Store the result in register P3. |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1838 | ** If either input is NULL, the result is NULL. |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1839 | */ |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1840 | case OP_BitAnd: /* same as TK_BITAND, in1, in2, out3 */ |
| 1841 | case OP_BitOr: /* same as TK_BITOR, in1, in2, out3 */ |
| 1842 | case OP_ShiftLeft: /* same as TK_LSHIFT, in1, in2, out3 */ |
| 1843 | case OP_ShiftRight: { /* same as TK_RSHIFT, in1, in2, out3 */ |
drh | 158b9cb | 2011-03-05 20:59:46 +0000 | [diff] [blame] | 1844 | i64 iA; |
| 1845 | u64 uA; |
| 1846 | i64 iB; |
| 1847 | u8 op; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1848 | |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 1849 | pIn1 = &aMem[pOp->p1]; |
| 1850 | pIn2 = &aMem[pOp->p2]; |
| 1851 | pOut = &aMem[pOp->p3]; |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1852 | if( (pIn1->flags | pIn2->flags) & MEM_Null ){ |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 1853 | sqlite3VdbeMemSetNull(pOut); |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1854 | break; |
| 1855 | } |
drh | 158b9cb | 2011-03-05 20:59:46 +0000 | [diff] [blame] | 1856 | iA = sqlite3VdbeIntValue(pIn2); |
| 1857 | iB = sqlite3VdbeIntValue(pIn1); |
| 1858 | op = pOp->opcode; |
| 1859 | if( op==OP_BitAnd ){ |
| 1860 | iA &= iB; |
| 1861 | }else if( op==OP_BitOr ){ |
| 1862 | iA |= iB; |
| 1863 | }else if( iB!=0 ){ |
| 1864 | assert( op==OP_ShiftRight || op==OP_ShiftLeft ); |
| 1865 | |
| 1866 | /* If shifting by a negative amount, shift in the other direction */ |
| 1867 | if( iB<0 ){ |
| 1868 | assert( OP_ShiftRight==OP_ShiftLeft+1 ); |
| 1869 | op = 2*OP_ShiftLeft + 1 - op; |
| 1870 | iB = iB>(-64) ? -iB : 64; |
| 1871 | } |
| 1872 | |
| 1873 | if( iB>=64 ){ |
| 1874 | iA = (iA>=0 || op==OP_ShiftLeft) ? 0 : -1; |
| 1875 | }else{ |
| 1876 | memcpy(&uA, &iA, sizeof(uA)); |
| 1877 | if( op==OP_ShiftLeft ){ |
| 1878 | uA <<= iB; |
| 1879 | }else{ |
| 1880 | uA >>= iB; |
| 1881 | /* Sign-extend on a right shift of a negative number */ |
| 1882 | if( iA<0 ) uA |= ((((u64)0xffffffff)<<32)|0xffffffff) << (64-iB); |
| 1883 | } |
| 1884 | memcpy(&iA, &uA, sizeof(iA)); |
| 1885 | } |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1886 | } |
drh | 158b9cb | 2011-03-05 20:59:46 +0000 | [diff] [blame] | 1887 | pOut->u.i = iA; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1888 | MemSetTypeFlag(pOut, MEM_Int); |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1889 | break; |
| 1890 | } |
| 1891 | |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1892 | /* Opcode: AddImm P1 P2 * * * |
drh | 72e26de | 2016-08-24 21:24:04 +0000 | [diff] [blame] | 1893 | ** Synopsis: r[P1]=r[P1]+P2 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1894 | ** |
danielk1977 | 0cdc022 | 2008-06-26 18:04:03 +0000 | [diff] [blame] | 1895 | ** Add the constant P2 to the value in register P1. |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1896 | ** The result is always an integer. |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 1897 | ** |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1898 | ** To force any register to be an integer, just add 0. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1899 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1900 | case OP_AddImm: { /* in1 */ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 1901 | pIn1 = &aMem[pOp->p1]; |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 1902 | memAboutToChange(p, pIn1); |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1903 | sqlite3VdbeMemIntegerify(pIn1); |
| 1904 | pIn1->u.i += pOp->p2; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1905 | break; |
| 1906 | } |
| 1907 | |
dan | e5166e0 | 2019-03-19 11:56:39 +0000 | [diff] [blame] | 1908 | /* Opcode: MustBeInt P1 P2 * * * |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 1909 | ** |
dan | e5166e0 | 2019-03-19 11:56:39 +0000 | [diff] [blame] | 1910 | ** Force the value in register P1 to be an integer. If the value |
| 1911 | ** in P1 is not an integer and cannot be converted into an integer |
| 1912 | ** without data loss, then jump immediately to P2, or if P2==0 |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 1913 | ** raise an SQLITE_MISMATCH exception. |
| 1914 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1915 | case OP_MustBeInt: { /* jump, in1 */ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 1916 | pIn1 = &aMem[pOp->p1]; |
dan | e5166e0 | 2019-03-19 11:56:39 +0000 | [diff] [blame] | 1917 | if( (pIn1->flags & MEM_Int)==0 ){ |
drh | 83b301b | 2013-11-20 00:59:02 +0000 | [diff] [blame] | 1918 | applyAffinity(pIn1, SQLITE_AFF_NUMERIC, encoding); |
dan | e5166e0 | 2019-03-19 11:56:39 +0000 | [diff] [blame] | 1919 | if( (pIn1->flags & MEM_Int)==0 ){ |
drh | c906533 | 2019-04-01 14:01:21 +0000 | [diff] [blame] | 1920 | VdbeBranchTaken(1, 2); |
drh | 83b301b | 2013-11-20 00:59:02 +0000 | [diff] [blame] | 1921 | if( pOp->p2==0 ){ |
| 1922 | rc = SQLITE_MISMATCH; |
| 1923 | goto abort_due_to_error; |
| 1924 | }else{ |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 1925 | goto jump_to_p2; |
drh | 83b301b | 2013-11-20 00:59:02 +0000 | [diff] [blame] | 1926 | } |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 1927 | } |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 1928 | } |
drh | c906533 | 2019-04-01 14:01:21 +0000 | [diff] [blame] | 1929 | VdbeBranchTaken(0, 2); |
dan | e5166e0 | 2019-03-19 11:56:39 +0000 | [diff] [blame] | 1930 | MemSetTypeFlag(pIn1, MEM_Int); |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 1931 | break; |
| 1932 | } |
| 1933 | |
drh | 13573c7 | 2010-01-12 17:04:07 +0000 | [diff] [blame] | 1934 | #ifndef SQLITE_OMIT_FLOATING_POINT |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1935 | /* Opcode: RealAffinity P1 * * * * |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 1936 | ** |
drh | 2133d82 | 2008-01-03 18:44:59 +0000 | [diff] [blame] | 1937 | ** If register P1 holds an integer convert it to a real value. |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 1938 | ** |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1939 | ** This opcode is used when extracting information from a column that |
| 1940 | ** has REAL affinity. Such column values may still be stored as |
| 1941 | ** integers, for space efficiency, but after extraction we want them |
| 1942 | ** to have only a real value. |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 1943 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1944 | case OP_RealAffinity: { /* in1 */ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 1945 | pIn1 = &aMem[pOp->p1]; |
drh | 169f077 | 2019-05-02 21:36:26 +0000 | [diff] [blame] | 1946 | if( pIn1->flags & (MEM_Int|MEM_IntReal) ){ |
drh | 3242c69 | 2019-05-04 01:29:13 +0000 | [diff] [blame] | 1947 | testcase( pIn1->flags & MEM_Int ); |
| 1948 | testcase( pIn1->flags & MEM_IntReal ); |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1949 | sqlite3VdbeMemRealify(pIn1); |
drh | efb5f9a | 2019-08-30 21:52:13 +0000 | [diff] [blame] | 1950 | REGISTER_TRACE(pOp->p1, pIn1); |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1951 | } |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 1952 | break; |
| 1953 | } |
drh | 13573c7 | 2010-01-12 17:04:07 +0000 | [diff] [blame] | 1954 | #endif |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 1955 | |
drh | 8df447f | 2005-11-01 15:48:24 +0000 | [diff] [blame] | 1956 | #ifndef SQLITE_OMIT_CAST |
drh | 4169e43 | 2014-08-25 20:11:52 +0000 | [diff] [blame] | 1957 | /* Opcode: Cast P1 P2 * * * |
mistachkin | a1dc42a | 2014-08-27 17:53:40 +0000 | [diff] [blame] | 1958 | ** Synopsis: affinity(r[P1]) |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 1959 | ** |
drh | 4169e43 | 2014-08-25 20:11:52 +0000 | [diff] [blame] | 1960 | ** Force the value in register P1 to be the type defined by P2. |
| 1961 | ** |
| 1962 | ** <ul> |
drh | bb6783b | 2017-04-29 18:02:49 +0000 | [diff] [blame] | 1963 | ** <li> P2=='A' → BLOB |
| 1964 | ** <li> P2=='B' → TEXT |
| 1965 | ** <li> P2=='C' → NUMERIC |
| 1966 | ** <li> P2=='D' → INTEGER |
| 1967 | ** <li> P2=='E' → REAL |
drh | 4169e43 | 2014-08-25 20:11:52 +0000 | [diff] [blame] | 1968 | ** </ul> |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 1969 | ** |
| 1970 | ** A NULL value is not changed by this routine. It remains NULL. |
| 1971 | */ |
drh | 4169e43 | 2014-08-25 20:11:52 +0000 | [diff] [blame] | 1972 | case OP_Cast: { /* in1 */ |
drh | 05883a3 | 2015-06-02 15:32:08 +0000 | [diff] [blame] | 1973 | assert( pOp->p2>=SQLITE_AFF_BLOB && pOp->p2<=SQLITE_AFF_REAL ); |
drh | 05bbb2e | 2014-08-25 22:37:19 +0000 | [diff] [blame] | 1974 | testcase( pOp->p2==SQLITE_AFF_TEXT ); |
drh | 05883a3 | 2015-06-02 15:32:08 +0000 | [diff] [blame] | 1975 | testcase( pOp->p2==SQLITE_AFF_BLOB ); |
drh | 05bbb2e | 2014-08-25 22:37:19 +0000 | [diff] [blame] | 1976 | testcase( pOp->p2==SQLITE_AFF_NUMERIC ); |
| 1977 | testcase( pOp->p2==SQLITE_AFF_INTEGER ); |
| 1978 | testcase( pOp->p2==SQLITE_AFF_REAL ); |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 1979 | pIn1 = &aMem[pOp->p1]; |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 1980 | memAboutToChange(p, pIn1); |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1981 | rc = ExpandBlob(pIn1); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 1982 | if( rc ) goto abort_due_to_error; |
drh | 0af6ddd | 2019-12-23 03:37:46 +0000 | [diff] [blame] | 1983 | rc = sqlite3VdbeMemCast(pIn1, pOp->p2, encoding); |
| 1984 | if( rc ) goto abort_due_to_error; |
| 1985 | UPDATE_MAX_BLOBSIZE(pIn1); |
drh | 5d73272 | 2019-12-20 17:25:10 +0000 | [diff] [blame] | 1986 | REGISTER_TRACE(pOp->p1, pIn1); |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 1987 | break; |
| 1988 | } |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1989 | #endif /* SQLITE_OMIT_CAST */ |
| 1990 | |
drh | 79752b6 | 2016-08-13 10:02:17 +0000 | [diff] [blame] | 1991 | /* Opcode: Eq P1 P2 P3 P4 P5 |
drh | 88e665f | 2016-08-27 01:41:53 +0000 | [diff] [blame] | 1992 | ** Synopsis: IF r[P3]==r[P1] |
drh | 79752b6 | 2016-08-13 10:02:17 +0000 | [diff] [blame] | 1993 | ** |
| 1994 | ** Compare the values in register P1 and P3. If reg(P3)==reg(P1) then |
drh | 4bc2045 | 2021-03-29 18:53:47 +0000 | [diff] [blame] | 1995 | ** jump to address P2. |
drh | 79752b6 | 2016-08-13 10:02:17 +0000 | [diff] [blame] | 1996 | ** |
| 1997 | ** The SQLITE_AFF_MASK portion of P5 must be an affinity character - |
| 1998 | ** SQLITE_AFF_TEXT, SQLITE_AFF_INTEGER, and so forth. An attempt is made |
| 1999 | ** to coerce both inputs according to this affinity before the |
| 2000 | ** comparison is made. If the SQLITE_AFF_MASK is 0x00, then numeric |
| 2001 | ** affinity is used. Note that the affinity conversions are stored |
| 2002 | ** back into the input registers P1 and P3. So this opcode can cause |
| 2003 | ** persistent changes to registers P1 and P3. |
| 2004 | ** |
| 2005 | ** Once any conversions have taken place, and neither value is NULL, |
| 2006 | ** the values are compared. If both values are blobs then memcmp() is |
| 2007 | ** used to determine the results of the comparison. If both values |
| 2008 | ** are text, then the appropriate collating function specified in |
| 2009 | ** P4 is used to do the comparison. If P4 is not specified then |
| 2010 | ** memcmp() is used to compare text string. If both values are |
| 2011 | ** numeric, then a numeric comparison is used. If the two values |
| 2012 | ** are of different types, then numbers are considered less than |
| 2013 | ** strings and strings are considered less than blobs. |
| 2014 | ** |
| 2015 | ** If SQLITE_NULLEQ is set in P5 then the result of comparison is always either |
| 2016 | ** true or false and is never NULL. If both operands are NULL then the result |
| 2017 | ** of comparison is true. If either operand is NULL then the result is false. |
| 2018 | ** If neither operand is NULL the result is the same as it would be if |
| 2019 | ** the SQLITE_NULLEQ flag were omitted from P5. |
| 2020 | ** |
drh | 1f97d26 | 2021-03-29 13:47:20 +0000 | [diff] [blame] | 2021 | ** This opcode saves the result of comparison for use by the new |
| 2022 | ** OP_Jump opcode. |
drh | 79752b6 | 2016-08-13 10:02:17 +0000 | [diff] [blame] | 2023 | */ |
| 2024 | /* Opcode: Ne P1 P2 P3 P4 P5 |
drh | 88e665f | 2016-08-27 01:41:53 +0000 | [diff] [blame] | 2025 | ** Synopsis: IF r[P3]!=r[P1] |
drh | 79752b6 | 2016-08-13 10:02:17 +0000 | [diff] [blame] | 2026 | ** |
| 2027 | ** This works just like the Eq opcode except that the jump is taken if |
| 2028 | ** the operands in registers P1 and P3 are not equal. See the Eq opcode for |
| 2029 | ** additional information. |
drh | 79752b6 | 2016-08-13 10:02:17 +0000 | [diff] [blame] | 2030 | */ |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 2031 | /* Opcode: Lt P1 P2 P3 P4 P5 |
drh | 88e665f | 2016-08-27 01:41:53 +0000 | [diff] [blame] | 2032 | ** Synopsis: IF r[P3]<r[P1] |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2033 | ** |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 2034 | ** Compare the values in register P1 and P3. If reg(P3)<reg(P1) then |
drh | 4bc2045 | 2021-03-29 18:53:47 +0000 | [diff] [blame] | 2035 | ** jump to address P2. |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 2036 | ** |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 2037 | ** 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] | 2038 | ** reg(P3) is NULL then the take the jump. If the SQLITE_JUMPIFNULL |
drh | 710c484 | 2010-08-30 01:17:20 +0000 | [diff] [blame] | 2039 | ** bit is clear then fall through if either operand is NULL. |
drh | 4f68623 | 2005-09-20 13:55:18 +0000 | [diff] [blame] | 2040 | ** |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 2041 | ** The SQLITE_AFF_MASK portion of P5 must be an affinity character - |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 2042 | ** SQLITE_AFF_TEXT, SQLITE_AFF_INTEGER, and so forth. An attempt is made |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 2043 | ** to coerce both inputs according to this affinity before the |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 2044 | ** comparison is made. If the SQLITE_AFF_MASK is 0x00, then numeric |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 2045 | ** affinity is used. Note that the affinity conversions are stored |
| 2046 | ** back into the input registers P1 and P3. So this opcode can cause |
| 2047 | ** persistent changes to registers P1 and P3. |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 2048 | ** |
| 2049 | ** Once any conversions have taken place, and neither value is NULL, |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 2050 | ** the values are compared. If both values are blobs then memcmp() is |
| 2051 | ** used to determine the results of the comparison. If both values |
| 2052 | ** are text, then the appropriate collating function specified in |
| 2053 | ** P4 is used to do the comparison. If P4 is not specified then |
| 2054 | ** memcmp() is used to compare text string. If both values are |
| 2055 | ** numeric, then a numeric comparison is used. If the two values |
| 2056 | ** are of different types, then numbers are considered less than |
| 2057 | ** strings and strings are considered less than blobs. |
drh | 1f97d26 | 2021-03-29 13:47:20 +0000 | [diff] [blame] | 2058 | ** |
| 2059 | ** This opcode saves the result of comparison for use by the new |
| 2060 | ** OP_Jump opcode. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2061 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2062 | /* Opcode: Le P1 P2 P3 P4 P5 |
drh | 88e665f | 2016-08-27 01:41:53 +0000 | [diff] [blame] | 2063 | ** Synopsis: IF r[P3]<=r[P1] |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2064 | ** |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 2065 | ** This works just like the Lt opcode except that the jump is taken if |
| 2066 | ** the content of register P3 is less than or equal to the content of |
| 2067 | ** register P1. See the Lt opcode for additional information. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2068 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2069 | /* Opcode: Gt P1 P2 P3 P4 P5 |
drh | 88e665f | 2016-08-27 01:41:53 +0000 | [diff] [blame] | 2070 | ** Synopsis: IF r[P3]>r[P1] |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2071 | ** |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 2072 | ** This works just like the Lt opcode except that the jump is taken if |
| 2073 | ** the content of register P3 is greater than the content of |
| 2074 | ** register P1. See the Lt opcode for additional information. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2075 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2076 | /* Opcode: Ge P1 P2 P3 P4 P5 |
drh | 88e665f | 2016-08-27 01:41:53 +0000 | [diff] [blame] | 2077 | ** Synopsis: IF r[P3]>=r[P1] |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2078 | ** |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 2079 | ** This works just like the Lt opcode except that the jump is taken if |
| 2080 | ** the content of register P3 is greater than or equal to the content of |
| 2081 | ** register P1. See the Lt opcode for additional information. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2082 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2083 | case OP_Eq: /* same as TK_EQ, jump, in1, in3 */ |
| 2084 | case OP_Ne: /* same as TK_NE, jump, in1, in3 */ |
| 2085 | case OP_Lt: /* same as TK_LT, jump, in1, in3 */ |
| 2086 | case OP_Le: /* same as TK_LE, jump, in1, in3 */ |
| 2087 | case OP_Gt: /* same as TK_GT, jump, in1, in3 */ |
| 2088 | case OP_Ge: { /* same as TK_GE, jump, in1, in3 */ |
drh | 4910a76 | 2016-09-03 01:46:15 +0000 | [diff] [blame] | 2089 | int res, res2; /* Result of the comparison of pIn1 against pIn3 */ |
drh | 6a2fe09 | 2009-09-23 02:29:36 +0000 | [diff] [blame] | 2090 | char affinity; /* Affinity to use for comparison */ |
dan | b7dca7d | 2010-03-05 16:32:12 +0000 | [diff] [blame] | 2091 | u16 flags1; /* Copy of initial value of pIn1->flags */ |
| 2092 | u16 flags3; /* Copy of initial value of pIn3->flags */ |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 2093 | |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 2094 | pIn1 = &aMem[pOp->p1]; |
| 2095 | pIn3 = &aMem[pOp->p3]; |
dan | b7dca7d | 2010-03-05 16:32:12 +0000 | [diff] [blame] | 2096 | flags1 = pIn1->flags; |
| 2097 | flags3 = pIn3->flags; |
drh | 8546497 | 2021-05-17 16:54:52 +0000 | [diff] [blame] | 2098 | if( (flags1 & flags3 & MEM_Int)!=0 ){ |
drh | 8ed1da1 | 2021-05-18 00:52:06 +0000 | [diff] [blame] | 2099 | assert( (pOp->p5 & SQLITE_AFF_MASK)!=SQLITE_AFF_TEXT || CORRUPT_DB ); |
drh | 8546497 | 2021-05-17 16:54:52 +0000 | [diff] [blame] | 2100 | /* Common case of comparison of two integers */ |
| 2101 | if( pIn3->u.i > pIn1->u.i ){ |
drh | 8546497 | 2021-05-17 16:54:52 +0000 | [diff] [blame] | 2102 | if( sqlite3aGTb[pOp->opcode] ){ |
| 2103 | VdbeBranchTaken(1, (pOp->p5 & SQLITE_NULLEQ)?2:3); |
| 2104 | goto jump_to_p2; |
| 2105 | } |
drh | a81a9f7 | 2022-04-04 11:38:49 +0000 | [diff] [blame] | 2106 | iCompare = +1; |
drh | 8546497 | 2021-05-17 16:54:52 +0000 | [diff] [blame] | 2107 | }else if( pIn3->u.i < pIn1->u.i ){ |
drh | 8546497 | 2021-05-17 16:54:52 +0000 | [diff] [blame] | 2108 | if( sqlite3aLTb[pOp->opcode] ){ |
| 2109 | VdbeBranchTaken(1, (pOp->p5 & SQLITE_NULLEQ)?2:3); |
| 2110 | goto jump_to_p2; |
| 2111 | } |
drh | a81a9f7 | 2022-04-04 11:38:49 +0000 | [diff] [blame] | 2112 | iCompare = -1; |
drh | 8546497 | 2021-05-17 16:54:52 +0000 | [diff] [blame] | 2113 | }else{ |
drh | 8546497 | 2021-05-17 16:54:52 +0000 | [diff] [blame] | 2114 | if( sqlite3aEQb[pOp->opcode] ){ |
| 2115 | VdbeBranchTaken(1, (pOp->p5 & SQLITE_NULLEQ)?2:3); |
| 2116 | goto jump_to_p2; |
| 2117 | } |
drh | a81a9f7 | 2022-04-04 11:38:49 +0000 | [diff] [blame] | 2118 | iCompare = 0; |
drh | 8546497 | 2021-05-17 16:54:52 +0000 | [diff] [blame] | 2119 | } |
| 2120 | VdbeBranchTaken(0, (pOp->p5 & SQLITE_NULLEQ)?2:3); |
| 2121 | break; |
| 2122 | } |
drh | c3f1d5f | 2011-05-30 23:42:16 +0000 | [diff] [blame] | 2123 | if( (flags1 | flags3)&MEM_Null ){ |
drh | 6a2fe09 | 2009-09-23 02:29:36 +0000 | [diff] [blame] | 2124 | /* One or both operands are NULL */ |
| 2125 | if( pOp->p5 & SQLITE_NULLEQ ){ |
| 2126 | /* If SQLITE_NULLEQ is set (which will only happen if the operator is |
| 2127 | ** OP_Eq or OP_Ne) then take the jump or not depending on whether |
| 2128 | ** or not both operands are null. |
| 2129 | */ |
drh | 053a128 | 2012-09-19 21:15:46 +0000 | [diff] [blame] | 2130 | assert( (flags1 & MEM_Cleared)==0 ); |
drh | a42325e | 2018-12-22 00:34:30 +0000 | [diff] [blame] | 2131 | assert( (pOp->p5 & SQLITE_JUMPIFNULL)==0 || CORRUPT_DB ); |
| 2132 | testcase( (pOp->p5 & SQLITE_JUMPIFNULL)!=0 ); |
drh | c3191d2 | 2016-10-18 16:36:15 +0000 | [diff] [blame] | 2133 | if( (flags1&flags3&MEM_Null)!=0 |
drh | 053a128 | 2012-09-19 21:15:46 +0000 | [diff] [blame] | 2134 | && (flags3&MEM_Cleared)==0 |
| 2135 | ){ |
drh | 4910a76 | 2016-09-03 01:46:15 +0000 | [diff] [blame] | 2136 | res = 0; /* Operands are equal */ |
drh | 053a128 | 2012-09-19 21:15:46 +0000 | [diff] [blame] | 2137 | }else{ |
dan | bdabe74 | 2019-03-18 16:51:24 +0000 | [diff] [blame] | 2138 | res = ((flags3 & MEM_Null) ? -1 : +1); /* Operands are not equal */ |
drh | 053a128 | 2012-09-19 21:15:46 +0000 | [diff] [blame] | 2139 | } |
drh | 6a2fe09 | 2009-09-23 02:29:36 +0000 | [diff] [blame] | 2140 | }else{ |
| 2141 | /* SQLITE_NULLEQ is clear and at least one operand is NULL, |
| 2142 | ** then the result is always NULL. |
| 2143 | ** The jump is taken if the SQLITE_JUMPIFNULL bit is set. |
| 2144 | */ |
drh | 4bc2045 | 2021-03-29 18:53:47 +0000 | [diff] [blame] | 2145 | VdbeBranchTaken(2,3); |
| 2146 | if( pOp->p5 & SQLITE_JUMPIFNULL ){ |
| 2147 | goto jump_to_p2; |
drh | 6a2fe09 | 2009-09-23 02:29:36 +0000 | [diff] [blame] | 2148 | } |
drh | a81a9f7 | 2022-04-04 11:38:49 +0000 | [diff] [blame] | 2149 | iCompare = 1; /* Operands are not equal */ |
drh | 6a2fe09 | 2009-09-23 02:29:36 +0000 | [diff] [blame] | 2150 | break; |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 2151 | } |
drh | 6a2fe09 | 2009-09-23 02:29:36 +0000 | [diff] [blame] | 2152 | }else{ |
drh | 8546497 | 2021-05-17 16:54:52 +0000 | [diff] [blame] | 2153 | /* Neither operand is NULL and we couldn't do the special high-speed |
| 2154 | ** integer comparison case. So do a general-case comparison. */ |
drh | 6a2fe09 | 2009-09-23 02:29:36 +0000 | [diff] [blame] | 2155 | affinity = pOp->p5 & SQLITE_AFF_MASK; |
drh | 24a0962 | 2014-09-18 16:28:59 +0000 | [diff] [blame] | 2156 | if( affinity>=SQLITE_AFF_NUMERIC ){ |
drh | 5fd0c12 | 2016-04-04 13:46:24 +0000 | [diff] [blame] | 2157 | if( (flags1 | flags3)&MEM_Str ){ |
drh | 169f077 | 2019-05-02 21:36:26 +0000 | [diff] [blame] | 2158 | if( (flags1 & (MEM_Int|MEM_IntReal|MEM_Real|MEM_Str))==MEM_Str ){ |
drh | 5fd0c12 | 2016-04-04 13:46:24 +0000 | [diff] [blame] | 2159 | applyNumericAffinity(pIn1,0); |
drh | 86d2de2 | 2020-06-14 13:40:13 +0000 | [diff] [blame] | 2160 | testcase( flags3==pIn3->flags ); |
drh | 4b37cd4 | 2016-06-25 11:43:47 +0000 | [diff] [blame] | 2161 | flags3 = pIn3->flags; |
drh | 5fd0c12 | 2016-04-04 13:46:24 +0000 | [diff] [blame] | 2162 | } |
drh | 169f077 | 2019-05-02 21:36:26 +0000 | [diff] [blame] | 2163 | if( (flags3 & (MEM_Int|MEM_IntReal|MEM_Real|MEM_Str))==MEM_Str ){ |
drh | 5fd0c12 | 2016-04-04 13:46:24 +0000 | [diff] [blame] | 2164 | applyNumericAffinity(pIn3,0); |
| 2165 | } |
drh | 24a0962 | 2014-09-18 16:28:59 +0000 | [diff] [blame] | 2166 | } |
| 2167 | }else if( affinity==SQLITE_AFF_TEXT ){ |
drh | 169f077 | 2019-05-02 21:36:26 +0000 | [diff] [blame] | 2168 | if( (flags1 & MEM_Str)==0 && (flags1&(MEM_Int|MEM_Real|MEM_IntReal))!=0 ){ |
drh | e7a3466 | 2014-09-19 22:44:20 +0000 | [diff] [blame] | 2169 | testcase( pIn1->flags & MEM_Int ); |
| 2170 | testcase( pIn1->flags & MEM_Real ); |
drh | 169f077 | 2019-05-02 21:36:26 +0000 | [diff] [blame] | 2171 | testcase( pIn1->flags & MEM_IntReal ); |
drh | 24a0962 | 2014-09-18 16:28:59 +0000 | [diff] [blame] | 2172 | sqlite3VdbeMemStringify(pIn1, encoding, 1); |
drh | bc8a6b3 | 2015-03-31 11:42:23 +0000 | [diff] [blame] | 2173 | testcase( (flags1&MEM_Dyn) != (pIn1->flags&MEM_Dyn) ); |
| 2174 | flags1 = (pIn1->flags & ~MEM_TypeMask) | (flags1 & MEM_TypeMask); |
drh | c4bb999 | 2022-01-14 21:34:49 +0000 | [diff] [blame] | 2175 | if( pIn1==pIn3 ) flags3 = flags1 | MEM_Str; |
drh | 24a0962 | 2014-09-18 16:28:59 +0000 | [diff] [blame] | 2176 | } |
drh | b44fec6 | 2019-12-24 21:42:22 +0000 | [diff] [blame] | 2177 | if( (flags3 & MEM_Str)==0 && (flags3&(MEM_Int|MEM_Real|MEM_IntReal))!=0 ){ |
drh | e7a3466 | 2014-09-19 22:44:20 +0000 | [diff] [blame] | 2178 | testcase( pIn3->flags & MEM_Int ); |
| 2179 | testcase( pIn3->flags & MEM_Real ); |
drh | 169f077 | 2019-05-02 21:36:26 +0000 | [diff] [blame] | 2180 | testcase( pIn3->flags & MEM_IntReal ); |
drh | 24a0962 | 2014-09-18 16:28:59 +0000 | [diff] [blame] | 2181 | sqlite3VdbeMemStringify(pIn3, encoding, 1); |
drh | bc8a6b3 | 2015-03-31 11:42:23 +0000 | [diff] [blame] | 2182 | testcase( (flags3&MEM_Dyn) != (pIn3->flags&MEM_Dyn) ); |
| 2183 | flags3 = (pIn3->flags & ~MEM_TypeMask) | (flags3 & MEM_TypeMask); |
drh | 24a0962 | 2014-09-18 16:28:59 +0000 | [diff] [blame] | 2184 | } |
drh | 6a2fe09 | 2009-09-23 02:29:36 +0000 | [diff] [blame] | 2185 | } |
drh | 6a2fe09 | 2009-09-23 02:29:36 +0000 | [diff] [blame] | 2186 | assert( pOp->p4type==P4_COLLSEQ || pOp->p4.pColl==0 ); |
drh | 4910a76 | 2016-09-03 01:46:15 +0000 | [diff] [blame] | 2187 | res = sqlite3MemCompare(pIn3, pIn1, pOp->p4.pColl); |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 2188 | } |
drh | 8546497 | 2021-05-17 16:54:52 +0000 | [diff] [blame] | 2189 | |
drh | 5859636 | 2017-08-03 00:29:23 +0000 | [diff] [blame] | 2190 | /* At this point, res is negative, zero, or positive if reg[P1] is |
| 2191 | ** less than, equal to, or greater than reg[P3], respectively. Compute |
| 2192 | ** the answer to this operator in res2, depending on what the comparison |
| 2193 | ** operator actually is. The next block of code depends on the fact |
| 2194 | ** that the 6 comparison operators are consecutive integers in this |
| 2195 | ** order: NE, EQ, GT, LE, LT, GE */ |
| 2196 | assert( OP_Eq==OP_Ne+1 ); assert( OP_Gt==OP_Ne+2 ); assert( OP_Le==OP_Ne+3 ); |
| 2197 | assert( OP_Lt==OP_Ne+4 ); assert( OP_Ge==OP_Ne+5 ); |
drh | 1af3fd5 | 2021-03-28 23:37:56 +0000 | [diff] [blame] | 2198 | if( res<0 ){ |
| 2199 | res2 = sqlite3aLTb[pOp->opcode]; |
drh | 5859636 | 2017-08-03 00:29:23 +0000 | [diff] [blame] | 2200 | }else if( res==0 ){ |
drh | 1af3fd5 | 2021-03-28 23:37:56 +0000 | [diff] [blame] | 2201 | res2 = sqlite3aEQb[pOp->opcode]; |
drh | 5859636 | 2017-08-03 00:29:23 +0000 | [diff] [blame] | 2202 | }else{ |
drh | 1af3fd5 | 2021-03-28 23:37:56 +0000 | [diff] [blame] | 2203 | res2 = sqlite3aGTb[pOp->opcode]; |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 2204 | } |
drh | 1f97d26 | 2021-03-29 13:47:20 +0000 | [diff] [blame] | 2205 | iCompare = res; |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 2206 | |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 2207 | /* Undo any changes made by applyAffinity() to the input registers. */ |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 2208 | assert( (pIn3->flags & MEM_Dyn) == (flags3 & MEM_Dyn) ); |
| 2209 | pIn3->flags = flags3; |
drh | b44fec6 | 2019-12-24 21:42:22 +0000 | [diff] [blame] | 2210 | assert( (pIn1->flags & MEM_Dyn) == (flags1 & MEM_Dyn) ); |
| 2211 | pIn1->flags = flags1; |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 2212 | |
drh | 4bc2045 | 2021-03-29 18:53:47 +0000 | [diff] [blame] | 2213 | VdbeBranchTaken(res2!=0, (pOp->p5 & SQLITE_NULLEQ)?2:3); |
| 2214 | if( res2 ){ |
| 2215 | goto jump_to_p2; |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 2216 | } |
| 2217 | break; |
| 2218 | } |
drh | c9b84a1 | 2002-06-20 11:36:48 +0000 | [diff] [blame] | 2219 | |
drh | 4bc2045 | 2021-03-29 18:53:47 +0000 | [diff] [blame] | 2220 | /* Opcode: ElseEq * P2 * * * |
drh | 79752b6 | 2016-08-13 10:02:17 +0000 | [diff] [blame] | 2221 | ** |
drh | 13d7950 | 2019-12-23 02:18:49 +0000 | [diff] [blame] | 2222 | ** This opcode must follow an OP_Lt or OP_Gt comparison operator. There |
| 2223 | ** can be zero or more OP_ReleaseReg opcodes intervening, but no other |
| 2224 | ** opcodes are allowed to occur between this instruction and the previous |
drh | 4bc2045 | 2021-03-29 18:53:47 +0000 | [diff] [blame] | 2225 | ** OP_Lt or OP_Gt. |
drh | 13d7950 | 2019-12-23 02:18:49 +0000 | [diff] [blame] | 2226 | ** |
| 2227 | ** If result of an OP_Eq comparison on the same two operands as the |
drh | 4bc2045 | 2021-03-29 18:53:47 +0000 | [diff] [blame] | 2228 | ** prior OP_Lt or OP_Gt would have been true, then jump to P2. |
| 2229 | ** If the result of an OP_Eq comparison on the two previous |
| 2230 | ** operands would have been false or NULL, then fall through. |
drh | 79752b6 | 2016-08-13 10:02:17 +0000 | [diff] [blame] | 2231 | */ |
drh | 4bc2045 | 2021-03-29 18:53:47 +0000 | [diff] [blame] | 2232 | case OP_ElseEq: { /* same as TK_ESCAPE, jump */ |
drh | 13d7950 | 2019-12-23 02:18:49 +0000 | [diff] [blame] | 2233 | |
| 2234 | #ifdef SQLITE_DEBUG |
| 2235 | /* Verify the preconditions of this opcode - that it follows an OP_Lt or |
drh | 4bc2045 | 2021-03-29 18:53:47 +0000 | [diff] [blame] | 2236 | ** OP_Gt with zero or more intervening OP_ReleaseReg opcodes */ |
drh | 13d7950 | 2019-12-23 02:18:49 +0000 | [diff] [blame] | 2237 | int iAddr; |
| 2238 | for(iAddr = (int)(pOp - aOp) - 1; ALWAYS(iAddr>=0); iAddr--){ |
| 2239 | if( aOp[iAddr].opcode==OP_ReleaseReg ) continue; |
| 2240 | assert( aOp[iAddr].opcode==OP_Lt || aOp[iAddr].opcode==OP_Gt ); |
drh | 13d7950 | 2019-12-23 02:18:49 +0000 | [diff] [blame] | 2241 | break; |
| 2242 | } |
| 2243 | #endif /* SQLITE_DEBUG */ |
drh | 4bc2045 | 2021-03-29 18:53:47 +0000 | [diff] [blame] | 2244 | VdbeBranchTaken(iCompare==0, 2); |
| 2245 | if( iCompare==0 ) goto jump_to_p2; |
drh | 79752b6 | 2016-08-13 10:02:17 +0000 | [diff] [blame] | 2246 | break; |
| 2247 | } |
| 2248 | |
| 2249 | |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 2250 | /* Opcode: Permutation * * * P4 * |
| 2251 | ** |
drh | b7dab70 | 2017-01-26 18:00:00 +0000 | [diff] [blame] | 2252 | ** Set the permutation used by the OP_Compare operator in the next |
| 2253 | ** instruction. The permutation is stored in the P4 operand. |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 2254 | ** |
drh | a81a9f7 | 2022-04-04 11:38:49 +0000 | [diff] [blame] | 2255 | ** The permutation is only valid for the next opcode which must be |
| 2256 | ** an OP_Compare that has the OPFLAG_PERMUTE bit set in P5. |
drh | b170202 | 2016-01-30 00:45:18 +0000 | [diff] [blame] | 2257 | ** |
| 2258 | ** The first integer in the P4 integer array is the length of the array |
| 2259 | ** and does not become part of the permutation. |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 2260 | */ |
| 2261 | case OP_Permutation: { |
| 2262 | assert( pOp->p4type==P4_INTARRAY ); |
| 2263 | assert( pOp->p4.ai ); |
drh | b7dab70 | 2017-01-26 18:00:00 +0000 | [diff] [blame] | 2264 | assert( pOp[1].opcode==OP_Compare ); |
| 2265 | assert( pOp[1].p5 & OPFLAG_PERMUTE ); |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 2266 | break; |
| 2267 | } |
| 2268 | |
drh | 953f761 | 2012-12-07 22:18:54 +0000 | [diff] [blame] | 2269 | /* Opcode: Compare P1 P2 P3 P4 P5 |
drh | 079a307 | 2014-03-19 14:10:55 +0000 | [diff] [blame] | 2270 | ** Synopsis: r[P1@P3] <-> r[P2@P3] |
drh | 16ee60f | 2008-06-20 18:13:25 +0000 | [diff] [blame] | 2271 | ** |
drh | 710c484 | 2010-08-30 01:17:20 +0000 | [diff] [blame] | 2272 | ** Compare two vectors of registers in reg(P1)..reg(P1+P3-1) (call this |
| 2273 | ** 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] | 2274 | ** the comparison for use by the next OP_Jump instruct. |
| 2275 | ** |
drh | 0ca10df | 2012-12-08 13:26:23 +0000 | [diff] [blame] | 2276 | ** If P5 has the OPFLAG_PERMUTE bit set, then the order of comparison is |
| 2277 | ** determined by the most recent OP_Permutation operator. If the |
| 2278 | ** OPFLAG_PERMUTE bit is clear, then register are compared in sequential |
| 2279 | ** order. |
| 2280 | ** |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 2281 | ** P4 is a KeyInfo structure that defines collating sequences and sort |
| 2282 | ** orders for the comparison. The permutation applies to registers |
| 2283 | ** only. The KeyInfo elements are used sequentially. |
| 2284 | ** |
| 2285 | ** The comparison is a sort comparison, so NULLs compare equal, |
| 2286 | ** NULLs are less than numbers, numbers are less than strings, |
drh | 16ee60f | 2008-06-20 18:13:25 +0000 | [diff] [blame] | 2287 | ** and strings are less than blobs. |
drh | a81a9f7 | 2022-04-04 11:38:49 +0000 | [diff] [blame] | 2288 | ** |
| 2289 | ** This opcode must be immediately followed by an OP_Jump opcode. |
drh | 16ee60f | 2008-06-20 18:13:25 +0000 | [diff] [blame] | 2290 | */ |
| 2291 | case OP_Compare: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2292 | int n; |
| 2293 | int i; |
| 2294 | int p1; |
| 2295 | int p2; |
| 2296 | const KeyInfo *pKeyInfo; |
drh | abc3815 | 2020-07-22 13:38:04 +0000 | [diff] [blame] | 2297 | u32 idx; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2298 | CollSeq *pColl; /* Collating sequence to use on this term */ |
| 2299 | int bRev; /* True for DESCENDING sort order */ |
drh | abc3815 | 2020-07-22 13:38:04 +0000 | [diff] [blame] | 2300 | u32 *aPermute; /* The permutation */ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2301 | |
drh | b7dab70 | 2017-01-26 18:00:00 +0000 | [diff] [blame] | 2302 | if( (pOp->p5 & OPFLAG_PERMUTE)==0 ){ |
| 2303 | aPermute = 0; |
| 2304 | }else{ |
| 2305 | assert( pOp>aOp ); |
| 2306 | assert( pOp[-1].opcode==OP_Permutation ); |
| 2307 | assert( pOp[-1].p4type==P4_INTARRAY ); |
| 2308 | aPermute = pOp[-1].p4.ai + 1; |
| 2309 | assert( aPermute!=0 ); |
| 2310 | } |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2311 | n = pOp->p3; |
| 2312 | pKeyInfo = pOp->p4.pKeyInfo; |
drh | 16ee60f | 2008-06-20 18:13:25 +0000 | [diff] [blame] | 2313 | assert( n>0 ); |
drh | 93a960a | 2008-07-10 00:32:42 +0000 | [diff] [blame] | 2314 | assert( pKeyInfo!=0 ); |
drh | 16ee60f | 2008-06-20 18:13:25 +0000 | [diff] [blame] | 2315 | p1 = pOp->p1; |
drh | 16ee60f | 2008-06-20 18:13:25 +0000 | [diff] [blame] | 2316 | p2 = pOp->p2; |
drh | d879e3e | 2017-02-13 13:35:55 +0000 | [diff] [blame] | 2317 | #ifdef SQLITE_DEBUG |
drh | 6a2fe09 | 2009-09-23 02:29:36 +0000 | [diff] [blame] | 2318 | if( aPermute ){ |
| 2319 | int k, mx = 0; |
mistachkin | cec5f1d | 2020-08-04 16:11:37 +0000 | [diff] [blame] | 2320 | for(k=0; k<n; k++) if( aPermute[k]>(u32)mx ) mx = aPermute[k]; |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 2321 | assert( p1>0 && p1+mx<=(p->nMem+1 - p->nCursor)+1 ); |
| 2322 | assert( p2>0 && p2+mx<=(p->nMem+1 - p->nCursor)+1 ); |
drh | 6a2fe09 | 2009-09-23 02:29:36 +0000 | [diff] [blame] | 2323 | }else{ |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 2324 | assert( p1>0 && p1+n<=(p->nMem+1 - p->nCursor)+1 ); |
| 2325 | assert( p2>0 && p2+n<=(p->nMem+1 - p->nCursor)+1 ); |
drh | 6a2fe09 | 2009-09-23 02:29:36 +0000 | [diff] [blame] | 2326 | } |
| 2327 | #endif /* SQLITE_DEBUG */ |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 2328 | for(i=0; i<n; i++){ |
drh | 8deae5a | 2020-07-29 12:23:20 +0000 | [diff] [blame] | 2329 | idx = aPermute ? aPermute[i] : (u32)i; |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 2330 | assert( memIsValid(&aMem[p1+idx]) ); |
| 2331 | assert( memIsValid(&aMem[p2+idx]) ); |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 2332 | REGISTER_TRACE(p1+idx, &aMem[p1+idx]); |
| 2333 | REGISTER_TRACE(p2+idx, &aMem[p2+idx]); |
drh | a485ad1 | 2017-08-02 22:43:14 +0000 | [diff] [blame] | 2334 | assert( i<pKeyInfo->nKeyField ); |
drh | 93a960a | 2008-07-10 00:32:42 +0000 | [diff] [blame] | 2335 | pColl = pKeyInfo->aColl[i]; |
dan | 6e11892 | 2019-08-12 16:36:38 +0000 | [diff] [blame] | 2336 | bRev = (pKeyInfo->aSortFlags[i] & KEYINFO_ORDER_DESC); |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 2337 | iCompare = sqlite3MemCompare(&aMem[p1+idx], &aMem[p2+idx], pColl); |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 2338 | if( iCompare ){ |
dan | 6e11892 | 2019-08-12 16:36:38 +0000 | [diff] [blame] | 2339 | if( (pKeyInfo->aSortFlags[i] & KEYINFO_ORDER_BIGNULL) |
| 2340 | && ((aMem[p1+idx].flags & MEM_Null) || (aMem[p2+idx].flags & MEM_Null)) |
| 2341 | ){ |
| 2342 | iCompare = -iCompare; |
| 2343 | } |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 2344 | if( bRev ) iCompare = -iCompare; |
| 2345 | break; |
| 2346 | } |
drh | 16ee60f | 2008-06-20 18:13:25 +0000 | [diff] [blame] | 2347 | } |
drh | a81a9f7 | 2022-04-04 11:38:49 +0000 | [diff] [blame] | 2348 | assert( pOp[1].opcode==OP_Jump ); |
drh | 16ee60f | 2008-06-20 18:13:25 +0000 | [diff] [blame] | 2349 | break; |
| 2350 | } |
| 2351 | |
| 2352 | /* Opcode: Jump P1 P2 P3 * * |
| 2353 | ** |
| 2354 | ** Jump to the instruction at address P1, P2, or P3 depending on whether |
| 2355 | ** in the most recent OP_Compare instruction the P1 vector was less than |
| 2356 | ** equal to, or greater than the P2 vector, respectively. |
drh | a81a9f7 | 2022-04-04 11:38:49 +0000 | [diff] [blame] | 2357 | ** |
| 2358 | ** This opcode must immediately follow an OP_Compare opcode. |
drh | 16ee60f | 2008-06-20 18:13:25 +0000 | [diff] [blame] | 2359 | */ |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 2360 | case OP_Jump: { /* jump */ |
drh | a81a9f7 | 2022-04-04 11:38:49 +0000 | [diff] [blame] | 2361 | assert( pOp>aOp && pOp[-1].opcode==OP_Compare ); |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 2362 | if( iCompare<0 ){ |
drh | 7083a48 | 2018-07-10 16:04:04 +0000 | [diff] [blame] | 2363 | VdbeBranchTaken(0,4); pOp = &aOp[pOp->p1 - 1]; |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 2364 | }else if( iCompare==0 ){ |
drh | 7083a48 | 2018-07-10 16:04:04 +0000 | [diff] [blame] | 2365 | VdbeBranchTaken(1,4); pOp = &aOp[pOp->p2 - 1]; |
drh | 16ee60f | 2008-06-20 18:13:25 +0000 | [diff] [blame] | 2366 | }else{ |
drh | 7083a48 | 2018-07-10 16:04:04 +0000 | [diff] [blame] | 2367 | VdbeBranchTaken(2,4); pOp = &aOp[pOp->p3 - 1]; |
drh | 16ee60f | 2008-06-20 18:13:25 +0000 | [diff] [blame] | 2368 | } |
| 2369 | break; |
| 2370 | } |
| 2371 | |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 2372 | /* Opcode: And P1 P2 P3 * * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 2373 | ** Synopsis: r[P3]=(r[P1] && r[P2]) |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2374 | ** |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 2375 | ** Take the logical AND of the values in registers P1 and P2 and |
| 2376 | ** write the result into register P3. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2377 | ** |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 2378 | ** If either P1 or P2 is 0 (false) then the result is 0 even if |
| 2379 | ** the other input is NULL. A NULL and true or two NULLs give |
| 2380 | ** a NULL output. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2381 | */ |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 2382 | /* Opcode: Or P1 P2 P3 * * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 2383 | ** Synopsis: r[P3]=(r[P1] || r[P2]) |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 2384 | ** |
| 2385 | ** Take the logical OR of the values in register P1 and P2 and |
| 2386 | ** store the answer in register P3. |
| 2387 | ** |
| 2388 | ** If either P1 or P2 is nonzero (true) then the result is 1 (true) |
| 2389 | ** even if the other input is NULL. A NULL and false or two NULLs |
| 2390 | ** give a NULL output. |
| 2391 | */ |
| 2392 | case OP_And: /* same as TK_AND, in1, in2, out3 */ |
| 2393 | case OP_Or: { /* same as TK_OR, in1, in2, out3 */ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2394 | int v1; /* Left operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */ |
| 2395 | int v2; /* Right operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */ |
drh | bb11351 | 2002-05-27 01:04:51 +0000 | [diff] [blame] | 2396 | |
drh | 1fcfa72 | 2018-02-26 15:27:31 +0000 | [diff] [blame] | 2397 | v1 = sqlite3VdbeBooleanValue(&aMem[pOp->p1], 2); |
| 2398 | v2 = sqlite3VdbeBooleanValue(&aMem[pOp->p2], 2); |
drh | bb11351 | 2002-05-27 01:04:51 +0000 | [diff] [blame] | 2399 | if( pOp->opcode==OP_And ){ |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 2400 | 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] | 2401 | v1 = and_logic[v1*3+v2]; |
| 2402 | }else{ |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 2403 | 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] | 2404 | v1 = or_logic[v1*3+v2]; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2405 | } |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 2406 | pOut = &aMem[pOp->p3]; |
drh | bb11351 | 2002-05-27 01:04:51 +0000 | [diff] [blame] | 2407 | if( v1==2 ){ |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 2408 | MemSetTypeFlag(pOut, MEM_Null); |
drh | bb11351 | 2002-05-27 01:04:51 +0000 | [diff] [blame] | 2409 | }else{ |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 2410 | pOut->u.i = v1; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 2411 | MemSetTypeFlag(pOut, MEM_Int); |
drh | bb11351 | 2002-05-27 01:04:51 +0000 | [diff] [blame] | 2412 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2413 | break; |
| 2414 | } |
| 2415 | |
drh | 8abed7b | 2018-02-26 18:49:05 +0000 | [diff] [blame] | 2416 | /* Opcode: IsTrue P1 P2 P3 P4 * |
| 2417 | ** Synopsis: r[P2] = coalesce(r[P1]==TRUE,P3) ^ P4 |
| 2418 | ** |
| 2419 | ** This opcode implements the IS TRUE, IS FALSE, IS NOT TRUE, and |
| 2420 | ** IS NOT FALSE operators. |
| 2421 | ** |
drh | 96acafb | 2018-02-27 14:49:25 +0000 | [diff] [blame] | 2422 | ** Interpret the value in register P1 as a boolean value. Store that |
drh | 8abed7b | 2018-02-26 18:49:05 +0000 | [diff] [blame] | 2423 | ** boolean (a 0 or 1) in register P2. Or if the value in register P1 is |
| 2424 | ** NULL, then the P3 is stored in register P2. Invert the answer if P4 |
| 2425 | ** is 1. |
| 2426 | ** |
| 2427 | ** The logic is summarized like this: |
| 2428 | ** |
| 2429 | ** <ul> |
drh | 96acafb | 2018-02-27 14:49:25 +0000 | [diff] [blame] | 2430 | ** <li> If P3==0 and P4==0 then r[P2] := r[P1] IS TRUE |
| 2431 | ** <li> If P3==1 and P4==1 then r[P2] := r[P1] IS FALSE |
| 2432 | ** <li> If P3==0 and P4==1 then r[P2] := r[P1] IS NOT TRUE |
| 2433 | ** <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] | 2434 | ** </ul> |
| 2435 | */ |
| 2436 | case OP_IsTrue: { /* in1, out2 */ |
| 2437 | assert( pOp->p4type==P4_INT32 ); |
| 2438 | assert( pOp->p4.i==0 || pOp->p4.i==1 ); |
drh | 96acafb | 2018-02-27 14:49:25 +0000 | [diff] [blame] | 2439 | assert( pOp->p3==0 || pOp->p3==1 ); |
drh | 8abed7b | 2018-02-26 18:49:05 +0000 | [diff] [blame] | 2440 | sqlite3VdbeMemSetInt64(&aMem[pOp->p2], |
| 2441 | sqlite3VdbeBooleanValue(&aMem[pOp->p1], pOp->p3) ^ pOp->p4.i); |
| 2442 | break; |
| 2443 | } |
| 2444 | |
drh | e99fa2a | 2008-12-15 15:27:51 +0000 | [diff] [blame] | 2445 | /* Opcode: Not P1 P2 * * * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 2446 | ** Synopsis: r[P2]= !r[P1] |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2447 | ** |
drh | e99fa2a | 2008-12-15 15:27:51 +0000 | [diff] [blame] | 2448 | ** Interpret the value in register P1 as a boolean value. Store the |
| 2449 | ** boolean complement in register P2. If the value in register P1 is |
| 2450 | ** NULL, then a NULL is stored in P2. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2451 | */ |
drh | 93952eb | 2009-11-13 19:43:43 +0000 | [diff] [blame] | 2452 | case OP_Not: { /* same as TK_NOT, in1, out2 */ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 2453 | pIn1 = &aMem[pOp->p1]; |
| 2454 | pOut = &aMem[pOp->p2]; |
drh | 0725cab | 2014-09-17 14:52:46 +0000 | [diff] [blame] | 2455 | if( (pIn1->flags & MEM_Null)==0 ){ |
drh | bc8f68a | 2018-02-26 15:31:39 +0000 | [diff] [blame] | 2456 | sqlite3VdbeMemSetInt64(pOut, !sqlite3VdbeBooleanValue(pIn1,0)); |
drh | 007c843 | 2018-02-26 03:20:18 +0000 | [diff] [blame] | 2457 | }else{ |
| 2458 | sqlite3VdbeMemSetNull(pOut); |
drh | e99fa2a | 2008-12-15 15:27:51 +0000 | [diff] [blame] | 2459 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2460 | break; |
| 2461 | } |
| 2462 | |
drh | e99fa2a | 2008-12-15 15:27:51 +0000 | [diff] [blame] | 2463 | /* Opcode: BitNot P1 P2 * * * |
drh | cd9e014 | 2018-06-12 13:16:57 +0000 | [diff] [blame] | 2464 | ** Synopsis: r[P2]= ~r[P1] |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 2465 | ** |
drh | e99fa2a | 2008-12-15 15:27:51 +0000 | [diff] [blame] | 2466 | ** Interpret the content of register P1 as an integer. Store the |
| 2467 | ** ones-complement of the P1 value into register P2. If P1 holds |
| 2468 | ** a NULL then store a NULL in P2. |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 2469 | */ |
drh | 93952eb | 2009-11-13 19:43:43 +0000 | [diff] [blame] | 2470 | case OP_BitNot: { /* same as TK_BITNOT, in1, out2 */ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 2471 | pIn1 = &aMem[pOp->p1]; |
| 2472 | pOut = &aMem[pOp->p2]; |
drh | 0725cab | 2014-09-17 14:52:46 +0000 | [diff] [blame] | 2473 | sqlite3VdbeMemSetNull(pOut); |
| 2474 | if( (pIn1->flags & MEM_Null)==0 ){ |
| 2475 | pOut->flags = MEM_Int; |
| 2476 | pOut->u.i = ~sqlite3VdbeIntValue(pIn1); |
drh | e99fa2a | 2008-12-15 15:27:51 +0000 | [diff] [blame] | 2477 | } |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 2478 | break; |
| 2479 | } |
| 2480 | |
drh | 48f2d3b | 2011-09-16 01:34:43 +0000 | [diff] [blame] | 2481 | /* Opcode: Once P1 P2 * * * |
| 2482 | ** |
drh | ab087d4 | 2017-03-24 17:59:56 +0000 | [diff] [blame] | 2483 | ** Fall through to the next instruction the first time this opcode is |
| 2484 | ** encountered on each invocation of the byte-code program. Jump to P2 |
| 2485 | ** on the second and all subsequent encounters during the same invocation. |
| 2486 | ** |
| 2487 | ** Top-level programs determine first invocation by comparing the P1 |
| 2488 | ** operand against the P1 operand on the OP_Init opcode at the beginning |
| 2489 | ** of the program. If the P1 values differ, then fall through and make |
| 2490 | ** the P1 of this opcode equal to the P1 of OP_Init. If P1 values are |
| 2491 | ** the same then take the jump. |
| 2492 | ** |
| 2493 | ** For subprograms, there is a bitmask in the VdbeFrame that determines |
| 2494 | ** whether or not the jump should be taken. The bitmask is necessary |
| 2495 | ** because the self-altering code trick does not work for recursive |
| 2496 | ** triggers. |
drh | 48f2d3b | 2011-09-16 01:34:43 +0000 | [diff] [blame] | 2497 | */ |
dan | 1d8cb21 | 2011-12-09 13:24:16 +0000 | [diff] [blame] | 2498 | case OP_Once: { /* jump */ |
drh | ab087d4 | 2017-03-24 17:59:56 +0000 | [diff] [blame] | 2499 | u32 iAddr; /* Address of this instruction */ |
drh | 9e5eb9c | 2016-09-18 16:08:10 +0000 | [diff] [blame] | 2500 | assert( p->aOp[0].opcode==OP_Init ); |
drh | ab087d4 | 2017-03-24 17:59:56 +0000 | [diff] [blame] | 2501 | if( p->pFrame ){ |
| 2502 | iAddr = (int)(pOp - p->aOp); |
| 2503 | if( (p->pFrame->aOnce[iAddr/8] & (1<<(iAddr & 7)))!=0 ){ |
| 2504 | VdbeBranchTaken(1, 2); |
drh | ab087d4 | 2017-03-24 17:59:56 +0000 | [diff] [blame] | 2505 | goto jump_to_p2; |
| 2506 | } |
drh | 18333ef | 2017-03-24 18:38:41 +0000 | [diff] [blame] | 2507 | p->pFrame->aOnce[iAddr/8] |= 1<<(iAddr & 7); |
dan | 1d8cb21 | 2011-12-09 13:24:16 +0000 | [diff] [blame] | 2508 | }else{ |
drh | ab087d4 | 2017-03-24 17:59:56 +0000 | [diff] [blame] | 2509 | if( p->aOp[0].p1==pOp->p1 ){ |
| 2510 | VdbeBranchTaken(1, 2); |
| 2511 | goto jump_to_p2; |
| 2512 | } |
dan | 1d8cb21 | 2011-12-09 13:24:16 +0000 | [diff] [blame] | 2513 | } |
drh | ab087d4 | 2017-03-24 17:59:56 +0000 | [diff] [blame] | 2514 | VdbeBranchTaken(0, 2); |
| 2515 | pOp->p1 = p->aOp[0].p1; |
dan | 1d8cb21 | 2011-12-09 13:24:16 +0000 | [diff] [blame] | 2516 | break; |
| 2517 | } |
| 2518 | |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 2519 | /* Opcode: If P1 P2 P3 * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2520 | ** |
drh | ef8662b | 2011-06-20 21:47:58 +0000 | [diff] [blame] | 2521 | ** Jump to P2 if the value in register P1 is true. The value |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 2522 | ** is considered true if it is numeric and non-zero. If the value |
drh | e21a6e1 | 2014-08-01 18:00:24 +0000 | [diff] [blame] | 2523 | ** 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] | 2524 | */ |
drh | 1fcfa72 | 2018-02-26 15:27:31 +0000 | [diff] [blame] | 2525 | case OP_If: { /* jump, in1 */ |
| 2526 | int c; |
| 2527 | c = sqlite3VdbeBooleanValue(&aMem[pOp->p1], pOp->p3); |
| 2528 | VdbeBranchTaken(c!=0, 2); |
| 2529 | if( c ) goto jump_to_p2; |
| 2530 | break; |
| 2531 | } |
| 2532 | |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 2533 | /* Opcode: IfNot P1 P2 P3 * * |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 2534 | ** |
drh | ef8662b | 2011-06-20 21:47:58 +0000 | [diff] [blame] | 2535 | ** Jump to P2 if the value in register P1 is False. The value |
drh | b8475df | 2011-12-09 16:21:19 +0000 | [diff] [blame] | 2536 | ** 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] | 2537 | ** 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] | 2538 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2539 | case OP_IfNot: { /* jump, in1 */ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2540 | int c; |
drh | 1fcfa72 | 2018-02-26 15:27:31 +0000 | [diff] [blame] | 2541 | c = !sqlite3VdbeBooleanValue(&aMem[pOp->p1], !pOp->p3); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 2542 | VdbeBranchTaken(c!=0, 2); |
drh | 1fcfa72 | 2018-02-26 15:27:31 +0000 | [diff] [blame] | 2543 | if( c ) goto jump_to_p2; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2544 | break; |
| 2545 | } |
| 2546 | |
drh | 830ecf9 | 2009-06-18 00:41:55 +0000 | [diff] [blame] | 2547 | /* Opcode: IsNull P1 P2 * * * |
drh | 72e26de | 2016-08-24 21:24:04 +0000 | [diff] [blame] | 2548 | ** Synopsis: if r[P1]==NULL goto P2 |
drh | 477df4b | 2008-01-05 18:48:24 +0000 | [diff] [blame] | 2549 | ** |
drh | 830ecf9 | 2009-06-18 00:41:55 +0000 | [diff] [blame] | 2550 | ** Jump to P2 if the value in register P1 is NULL. |
drh | 477df4b | 2008-01-05 18:48:24 +0000 | [diff] [blame] | 2551 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2552 | case OP_IsNull: { /* same as TK_ISNULL, jump, in1 */ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 2553 | pIn1 = &aMem[pOp->p1]; |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 2554 | VdbeBranchTaken( (pIn1->flags & MEM_Null)!=0, 2); |
drh | 830ecf9 | 2009-06-18 00:41:55 +0000 | [diff] [blame] | 2555 | if( (pIn1->flags & MEM_Null)!=0 ){ |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 2556 | goto jump_to_p2; |
drh | 830ecf9 | 2009-06-18 00:41:55 +0000 | [diff] [blame] | 2557 | } |
drh | 477df4b | 2008-01-05 18:48:24 +0000 | [diff] [blame] | 2558 | break; |
| 2559 | } |
| 2560 | |
drh | 71c770f | 2021-08-19 16:29:33 +0000 | [diff] [blame] | 2561 | /* Opcode: IsNullOrType P1 P2 P3 * * |
| 2562 | ** Synopsis: if typeof(r[P1]) IN (P3,5) goto P2 |
| 2563 | ** |
| 2564 | ** Jump to P2 if the value in register P1 is NULL or has a datatype P3. |
| 2565 | ** P3 is an integer which should be one of SQLITE_INTEGER, SQLITE_FLOAT, |
| 2566 | ** SQLITE_BLOB, SQLITE_NULL, or SQLITE_TEXT. |
| 2567 | */ |
| 2568 | case OP_IsNullOrType: { /* jump, in1 */ |
| 2569 | int doTheJump; |
| 2570 | pIn1 = &aMem[pOp->p1]; |
| 2571 | doTheJump = (pIn1->flags & MEM_Null)!=0 || sqlite3_value_type(pIn1)==pOp->p3; |
| 2572 | VdbeBranchTaken( doTheJump, 2); |
| 2573 | if( doTheJump ) goto jump_to_p2; |
| 2574 | break; |
| 2575 | } |
| 2576 | |
drh | 871e7ff | 2021-03-29 14:40:48 +0000 | [diff] [blame] | 2577 | /* Opcode: ZeroOrNull P1 P2 P3 * * |
drh | 4bc2045 | 2021-03-29 18:53:47 +0000 | [diff] [blame] | 2578 | ** Synopsis: r[P2] = 0 OR NULL |
drh | 871e7ff | 2021-03-29 14:40:48 +0000 | [diff] [blame] | 2579 | ** |
drh | 4bc2045 | 2021-03-29 18:53:47 +0000 | [diff] [blame] | 2580 | ** If all both registers P1 and P3 are NOT NULL, then store a zero in |
| 2581 | ** register P2. If either registers P1 or P3 are NULL then put |
| 2582 | ** a NULL in register P2. |
drh | 871e7ff | 2021-03-29 14:40:48 +0000 | [diff] [blame] | 2583 | */ |
drh | 4bc2045 | 2021-03-29 18:53:47 +0000 | [diff] [blame] | 2584 | case OP_ZeroOrNull: { /* in1, in2, out2, in3 */ |
drh | 871e7ff | 2021-03-29 14:40:48 +0000 | [diff] [blame] | 2585 | if( (aMem[pOp->p1].flags & MEM_Null)!=0 |
| 2586 | || (aMem[pOp->p3].flags & MEM_Null)!=0 |
| 2587 | ){ |
| 2588 | sqlite3VdbeMemSetNull(aMem + pOp->p2); |
| 2589 | }else{ |
| 2590 | sqlite3VdbeMemSetInt64(aMem + pOp->p2, 0); |
| 2591 | } |
| 2592 | break; |
| 2593 | } |
| 2594 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 2595 | /* Opcode: NotNull P1 P2 * * * |
drh | fc8d4f9 | 2013-11-08 15:19:46 +0000 | [diff] [blame] | 2596 | ** Synopsis: if r[P1]!=NULL goto P2 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2597 | ** |
drh | 6a288a3 | 2008-01-07 19:20:24 +0000 | [diff] [blame] | 2598 | ** Jump to P2 if the value in register P1 is not NULL. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2599 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2600 | case OP_NotNull: { /* same as TK_NOTNULL, jump, in1 */ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 2601 | pIn1 = &aMem[pOp->p1]; |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 2602 | VdbeBranchTaken( (pIn1->flags & MEM_Null)==0, 2); |
drh | 6a288a3 | 2008-01-07 19:20:24 +0000 | [diff] [blame] | 2603 | if( (pIn1->flags & MEM_Null)==0 ){ |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 2604 | goto jump_to_p2; |
drh | 6a288a3 | 2008-01-07 19:20:24 +0000 | [diff] [blame] | 2605 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2606 | break; |
| 2607 | } |
| 2608 | |
drh | 31d6fd5 | 2017-04-14 19:03:10 +0000 | [diff] [blame] | 2609 | /* Opcode: IfNullRow P1 P2 P3 * * |
| 2610 | ** Synopsis: if P1.nullRow then r[P3]=NULL, goto P2 |
| 2611 | ** |
| 2612 | ** Check the cursor P1 to see if it is currently pointing at a NULL row. |
| 2613 | ** If it is, then set register P3 to NULL and jump immediately to P2. |
| 2614 | ** If P1 is not on a NULL row, then fall through without making any |
| 2615 | ** changes. |
| 2616 | */ |
| 2617 | case OP_IfNullRow: { /* jump */ |
| 2618 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
drh | 3f1e9e0 | 2017-05-23 01:21:07 +0000 | [diff] [blame] | 2619 | assert( p->apCsr[pOp->p1]!=0 ); |
drh | 31d6fd5 | 2017-04-14 19:03:10 +0000 | [diff] [blame] | 2620 | if( p->apCsr[pOp->p1]->nullRow ){ |
| 2621 | sqlite3VdbeMemSetNull(aMem + pOp->p3); |
| 2622 | goto jump_to_p2; |
| 2623 | } |
| 2624 | break; |
| 2625 | } |
| 2626 | |
drh | 092457b | 2017-12-29 15:04:49 +0000 | [diff] [blame] | 2627 | #ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC |
| 2628 | /* Opcode: Offset P1 P2 P3 * * |
| 2629 | ** Synopsis: r[P3] = sqlite_offset(P1) |
drh | 2fc865c | 2017-12-16 20:20:37 +0000 | [diff] [blame] | 2630 | ** |
drh | 092457b | 2017-12-29 15:04:49 +0000 | [diff] [blame] | 2631 | ** 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] | 2632 | ** start of the payload for the record at which that cursor P1 is currently |
| 2633 | ** pointing. |
drh | fe6d20e | 2017-12-29 14:33:54 +0000 | [diff] [blame] | 2634 | ** |
drh | 092457b | 2017-12-29 15:04:49 +0000 | [diff] [blame] | 2635 | ** P2 is the column number for the argument to the sqlite_offset() function. |
drh | fe6d20e | 2017-12-29 14:33:54 +0000 | [diff] [blame] | 2636 | ** This opcode does not use P2 itself, but the P2 value is used by the |
| 2637 | ** code generator. The P1, P2, and P3 operands to this opcode are the |
mistachkin | 5e9825e | 2018-03-01 18:09:02 +0000 | [diff] [blame] | 2638 | ** same as for OP_Column. |
drh | 092457b | 2017-12-29 15:04:49 +0000 | [diff] [blame] | 2639 | ** |
| 2640 | ** This opcode is only available if SQLite is compiled with the |
| 2641 | ** -DSQLITE_ENABLE_OFFSET_SQL_FUNC option. |
drh | 2fc865c | 2017-12-16 20:20:37 +0000 | [diff] [blame] | 2642 | */ |
drh | 092457b | 2017-12-29 15:04:49 +0000 | [diff] [blame] | 2643 | case OP_Offset: { /* out3 */ |
drh | 2fc865c | 2017-12-16 20:20:37 +0000 | [diff] [blame] | 2644 | VdbeCursor *pC; /* The VDBE cursor */ |
| 2645 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 2646 | pC = p->apCsr[pOp->p1]; |
drh | fe6d20e | 2017-12-29 14:33:54 +0000 | [diff] [blame] | 2647 | pOut = &p->aMem[pOp->p3]; |
drh | 8f32d92 | 2022-03-05 19:36:29 +0000 | [diff] [blame] | 2648 | if( pC==0 || pC->eCurType!=CURTYPE_BTREE ){ |
drh | fe6d20e | 2017-12-29 14:33:54 +0000 | [diff] [blame] | 2649 | sqlite3VdbeMemSetNull(pOut); |
drh | 2fc865c | 2017-12-16 20:20:37 +0000 | [diff] [blame] | 2650 | }else{ |
drh | 8f32d92 | 2022-03-05 19:36:29 +0000 | [diff] [blame] | 2651 | if( pC->deferredMoveto ){ |
| 2652 | rc = sqlite3VdbeFinishMoveto(pC); |
| 2653 | if( rc ) goto abort_due_to_error; |
| 2654 | } |
drh | dc95136 | 2022-03-05 23:52:05 +0000 | [diff] [blame] | 2655 | if( sqlite3BtreeEof(pC->uc.pCursor) ){ |
drh | 8f32d92 | 2022-03-05 19:36:29 +0000 | [diff] [blame] | 2656 | sqlite3VdbeMemSetNull(pOut); |
| 2657 | }else{ |
| 2658 | sqlite3VdbeMemSetInt64(pOut, sqlite3BtreeOffset(pC->uc.pCursor)); |
| 2659 | } |
drh | 2fc865c | 2017-12-16 20:20:37 +0000 | [diff] [blame] | 2660 | } |
| 2661 | break; |
| 2662 | } |
drh | 092457b | 2017-12-29 15:04:49 +0000 | [diff] [blame] | 2663 | #endif /* SQLITE_ENABLE_OFFSET_SQL_FUNC */ |
drh | 2fc865c | 2017-12-16 20:20:37 +0000 | [diff] [blame] | 2664 | |
drh | 3e9ca09 | 2009-09-08 01:14:48 +0000 | [diff] [blame] | 2665 | /* Opcode: Column P1 P2 P3 P4 P5 |
drh | 72e26de | 2016-08-24 21:24:04 +0000 | [diff] [blame] | 2666 | ** Synopsis: r[P3]=PX |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2667 | ** |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 2668 | ** Interpret the data that cursor P1 points to as a structure built using |
| 2669 | ** the MakeRecord instruction. (See the MakeRecord opcode for additional |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 2670 | ** information about the format of the data.) Extract the P2-th column |
| 2671 | ** from this record. If there are less that (P2+1) |
| 2672 | ** values in the record, extract a NULL. |
| 2673 | ** |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2674 | ** The value extracted is stored in register P3. |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2675 | ** |
drh | 1cc3a36 | 2017-04-03 13:17:31 +0000 | [diff] [blame] | 2676 | ** If the record contains fewer than P2 fields, then extract a NULL. Or, |
danielk1977 | 1f4aa33 | 2008-01-03 09:51:55 +0000 | [diff] [blame] | 2677 | ** if the P4 argument is a P4_MEM use the value of the P4 argument as |
| 2678 | ** the result. |
drh | 3e9ca09 | 2009-09-08 01:14:48 +0000 | [diff] [blame] | 2679 | ** |
drh | 1cc3a36 | 2017-04-03 13:17:31 +0000 | [diff] [blame] | 2680 | ** If the OPFLAG_LENGTHARG and OPFLAG_TYPEOFARG bits are set on P5 then |
drh | dda5c08 | 2012-03-28 13:41:10 +0000 | [diff] [blame] | 2681 | ** the result is guaranteed to only be used as the argument of a length() |
| 2682 | ** or typeof() function, respectively. The loading of large blobs can be |
| 2683 | ** skipped for length() and all content loading can be skipped for typeof(). |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2684 | */ |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 2685 | case OP_Column: { |
drh | abc3815 | 2020-07-22 13:38:04 +0000 | [diff] [blame] | 2686 | u32 p2; /* column number to retrieve */ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2687 | VdbeCursor *pC; /* The VDBE cursor */ |
drh | fc56950 | 2022-02-25 20:11:59 +0000 | [diff] [blame] | 2688 | BtCursor *pCrsr; /* The B-Tree cursor corresponding to pC */ |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 2689 | 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] | 2690 | int len; /* The length of the serialized data for the column */ |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 2691 | int i; /* Loop counter */ |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 2692 | Mem *pDest; /* Where to write the extracted value */ |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 2693 | Mem sMem; /* For storing the record being decoded */ |
drh | 399af1d | 2013-11-20 17:25:55 +0000 | [diff] [blame] | 2694 | const u8 *zData; /* Part of the record being decoded */ |
| 2695 | const u8 *zHdr; /* Next unparsed byte of the header */ |
| 2696 | const u8 *zEndHdr; /* Pointer to first byte after the header */ |
drh | c6ce3883 | 2015-10-15 21:30:24 +0000 | [diff] [blame] | 2697 | u64 offset64; /* 64-bit offset */ |
drh | 5a077b7 | 2011-08-29 02:16:18 +0000 | [diff] [blame] | 2698 | u32 t; /* A type code from the record header */ |
drh | 3e9ca09 | 2009-09-08 01:14:48 +0000 | [diff] [blame] | 2699 | Mem *pReg; /* PseudoTable input register */ |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2700 | |
drh | 8c7715d | 2019-12-20 14:37:56 +0000 | [diff] [blame] | 2701 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
drh | fc56950 | 2022-02-25 20:11:59 +0000 | [diff] [blame] | 2702 | assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) ); |
dan | de892d9 | 2016-01-29 19:29:45 +0000 | [diff] [blame] | 2703 | pC = p->apCsr[pOp->p1]; |
drh | abc3815 | 2020-07-22 13:38:04 +0000 | [diff] [blame] | 2704 | p2 = (u32)pOp->p2; |
dan | de892d9 | 2016-01-29 19:29:45 +0000 | [diff] [blame] | 2705 | |
drh | fc56950 | 2022-02-25 20:11:59 +0000 | [diff] [blame] | 2706 | op_column_restart: |
danielk1977 | 6c92409 | 2007-11-12 08:09:34 +0000 | [diff] [blame] | 2707 | assert( pC!=0 ); |
drh | 7c96039 | 2022-04-13 18:20:23 +0000 | [diff] [blame] | 2708 | assert( p2<(u32)pC->nField |
| 2709 | || (pC->eCurType==CURTYPE_PSEUDO && pC->seekResult==0) ); |
drh | b53a5a9 | 2014-10-12 22:37:22 +0000 | [diff] [blame] | 2710 | aOffset = pC->aOffset; |
drh | b248668 | 2022-01-03 01:43:28 +0000 | [diff] [blame] | 2711 | assert( aOffset==pC->aType+pC->nField ); |
drh | 62aaa6c | 2015-11-21 17:27:42 +0000 | [diff] [blame] | 2712 | assert( pC->eCurType!=CURTYPE_VTAB ); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 2713 | assert( pC->eCurType!=CURTYPE_PSEUDO || pC->nullRow ); |
| 2714 | assert( pC->eCurType!=CURTYPE_SORTER ); |
drh | 399af1d | 2013-11-20 17:25:55 +0000 | [diff] [blame] | 2715 | |
drh | a43a02e | 2016-05-19 17:51:19 +0000 | [diff] [blame] | 2716 | if( pC->cacheStatus!=p->cacheCtr ){ /*OPTIMIZATION-IF-FALSE*/ |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2717 | if( pC->nullRow ){ |
drh | 3ac6243 | 2022-04-13 17:41:03 +0000 | [diff] [blame] | 2718 | if( pC->eCurType==CURTYPE_PSEUDO && pC->seekResult>0 ){ |
drh | fe0cf7a | 2017-08-16 19:20:20 +0000 | [diff] [blame] | 2719 | /* For the special case of as pseudo-cursor, the seekResult field |
| 2720 | ** identifies the register that holds the record */ |
drh | fe0cf7a | 2017-08-16 19:20:20 +0000 | [diff] [blame] | 2721 | pReg = &aMem[pC->seekResult]; |
drh | c8606e4 | 2013-11-20 19:28:03 +0000 | [diff] [blame] | 2722 | assert( pReg->flags & MEM_Blob ); |
| 2723 | assert( memIsValid(pReg) ); |
drh | 6cd8c8c | 2017-08-15 14:14:36 +0000 | [diff] [blame] | 2724 | pC->payloadSize = pC->szRow = pReg->n; |
drh | c8606e4 | 2013-11-20 19:28:03 +0000 | [diff] [blame] | 2725 | pC->aRow = (u8*)pReg->z; |
| 2726 | }else{ |
drh | fc56950 | 2022-02-25 20:11:59 +0000 | [diff] [blame] | 2727 | pDest = &aMem[pOp->p3]; |
| 2728 | memAboutToChange(p, pDest); |
drh | 6b5631e | 2014-11-05 15:57:39 +0000 | [diff] [blame] | 2729 | sqlite3VdbeMemSetNull(pDest); |
drh | 399af1d | 2013-11-20 17:25:55 +0000 | [diff] [blame] | 2730 | goto op_column_out; |
| 2731 | } |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2732 | }else{ |
drh | 06a09a8 | 2016-11-25 17:03:03 +0000 | [diff] [blame] | 2733 | pCrsr = pC->uc.pCursor; |
drh | fc56950 | 2022-02-25 20:11:59 +0000 | [diff] [blame] | 2734 | if( pC->deferredMoveto ){ |
| 2735 | u32 iMap; |
| 2736 | assert( !pC->isEphemeral ); |
| 2737 | if( pC->ub.aAltMap && (iMap = pC->ub.aAltMap[1+p2])>0 ){ |
| 2738 | pC = pC->pAltCursor; |
| 2739 | p2 = iMap - 1; |
| 2740 | goto op_column_restart; |
| 2741 | } |
| 2742 | rc = sqlite3VdbeFinishMoveto(pC); |
| 2743 | if( rc ) goto abort_due_to_error; |
| 2744 | }else if( sqlite3BtreeCursorHasMoved(pCrsr) ){ |
| 2745 | rc = sqlite3VdbeHandleMovedCursor(pC); |
| 2746 | if( rc ) goto abort_due_to_error; |
| 2747 | goto op_column_restart; |
| 2748 | } |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 2749 | assert( pC->eCurType==CURTYPE_BTREE ); |
drh | c8606e4 | 2013-11-20 19:28:03 +0000 | [diff] [blame] | 2750 | assert( pCrsr ); |
drh | a7c90c4 | 2016-06-04 20:37:10 +0000 | [diff] [blame] | 2751 | assert( sqlite3BtreeCursorIsValid(pCrsr) ); |
| 2752 | pC->payloadSize = sqlite3BtreePayloadSize(pCrsr); |
drh | 6cd8c8c | 2017-08-15 14:14:36 +0000 | [diff] [blame] | 2753 | pC->aRow = sqlite3BtreePayloadFetch(pCrsr, &pC->szRow); |
| 2754 | assert( pC->szRow<=pC->payloadSize ); |
| 2755 | assert( pC->szRow<=65536 ); /* Maximum page size is 64KiB */ |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2756 | } |
drh | b73857f | 2006-03-17 00:25:59 +0000 | [diff] [blame] | 2757 | pC->cacheStatus = p->cacheCtr; |
drh | c2808f3 | 2022-04-02 22:47:47 +0000 | [diff] [blame] | 2758 | if( (aOffset[0] = pC->aRow[0])<0x80 ){ |
| 2759 | pC->iHdrOffset = 1; |
| 2760 | }else{ |
| 2761 | pC->iHdrOffset = sqlite3GetVarint32(pC->aRow, aOffset); |
| 2762 | } |
drh | 399af1d | 2013-11-20 17:25:55 +0000 | [diff] [blame] | 2763 | pC->nHdrParsed = 0; |
drh | 35cd643 | 2009-06-05 14:17:21 +0000 | [diff] [blame] | 2764 | |
drh | 1f613c4 | 2017-08-16 14:16:19 +0000 | [diff] [blame] | 2765 | if( pC->szRow<aOffset[0] ){ /*OPTIMIZATION-IF-FALSE*/ |
drh | c81aa2e | 2014-10-11 23:31:52 +0000 | [diff] [blame] | 2766 | /* pC->aRow does not have to hold the entire row, but it does at least |
| 2767 | ** need to cover the header of the record. If pC->aRow does not contain |
| 2768 | ** the complete header, then set it to zero, forcing the header to be |
| 2769 | ** dynamically allocated. */ |
| 2770 | pC->aRow = 0; |
| 2771 | pC->szRow = 0; |
drh | 848a332 | 2015-10-16 12:53:47 +0000 | [diff] [blame] | 2772 | |
| 2773 | /* Make sure a corrupt database has not given us an oversize header. |
| 2774 | ** Do this now to avoid an oversize memory allocation. |
| 2775 | ** |
| 2776 | ** Type entries can be between 1 and 5 bytes each. But 4 and 5 byte |
| 2777 | ** types use so much data space that there can only be 4096 and 32 of |
| 2778 | ** them, respectively. So the maximum header length results from a |
| 2779 | ** 3-byte type for each of the maximum of 32768 columns plus three |
| 2780 | ** extra bytes for the header length itself. 32768*3 + 3 = 98307. |
| 2781 | */ |
drh | 1f613c4 | 2017-08-16 14:16:19 +0000 | [diff] [blame] | 2782 | if( aOffset[0] > 98307 || aOffset[0] > pC->payloadSize ){ |
drh | 74588ce | 2017-09-13 00:13:05 +0000 | [diff] [blame] | 2783 | goto op_column_corrupt; |
drh | 848a332 | 2015-10-16 12:53:47 +0000 | [diff] [blame] | 2784 | } |
drh | 95b225a | 2017-08-16 11:04:22 +0000 | [diff] [blame] | 2785 | }else{ |
| 2786 | /* This is an optimization. By skipping over the first few tests |
| 2787 | ** (ex: pC->nHdrParsed<=p2) in the next section, we achieve a |
| 2788 | ** measurable performance gain. |
| 2789 | ** |
drh | 1f613c4 | 2017-08-16 14:16:19 +0000 | [diff] [blame] | 2790 | ** 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] | 2791 | ** generated by SQLite, and could be considered corruption, but we |
drh | 1f613c4 | 2017-08-16 14:16:19 +0000 | [diff] [blame] | 2792 | ** accept it for historical reasons. When aOffset[0]==0, the code this |
drh | 95b225a | 2017-08-16 11:04:22 +0000 | [diff] [blame] | 2793 | ** branch jumps to reads past the end of the record, but never more |
| 2794 | ** than a few bytes. Even if the record occurs at the end of the page |
| 2795 | ** content area, the "page header" comes after the page content and so |
| 2796 | ** this overread is harmless. Similar overreads can occur for a corrupt |
| 2797 | ** database file. |
drh | 0eda6cd | 2016-05-19 16:58:42 +0000 | [diff] [blame] | 2798 | */ |
| 2799 | zData = pC->aRow; |
| 2800 | assert( pC->nHdrParsed<=p2 ); /* Conditional skipped */ |
drh | 1f613c4 | 2017-08-16 14:16:19 +0000 | [diff] [blame] | 2801 | testcase( aOffset[0]==0 ); |
drh | 0eda6cd | 2016-05-19 16:58:42 +0000 | [diff] [blame] | 2802 | goto op_column_read_header; |
drh | c81aa2e | 2014-10-11 23:31:52 +0000 | [diff] [blame] | 2803 | } |
drh | fc56950 | 2022-02-25 20:11:59 +0000 | [diff] [blame] | 2804 | }else if( sqlite3BtreeCursorHasMoved(pC->uc.pCursor) ){ |
| 2805 | rc = sqlite3VdbeHandleMovedCursor(pC); |
| 2806 | if( rc ) goto abort_due_to_error; |
| 2807 | goto op_column_restart; |
drh | 399af1d | 2013-11-20 17:25:55 +0000 | [diff] [blame] | 2808 | } |
drh | 35cd643 | 2009-06-05 14:17:21 +0000 | [diff] [blame] | 2809 | |
drh | 399af1d | 2013-11-20 17:25:55 +0000 | [diff] [blame] | 2810 | /* 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] | 2811 | ** parsed and valid information is in aOffset[] and pC->aType[]. |
drh | 399af1d | 2013-11-20 17:25:55 +0000 | [diff] [blame] | 2812 | */ |
drh | c8606e4 | 2013-11-20 19:28:03 +0000 | [diff] [blame] | 2813 | if( pC->nHdrParsed<=p2 ){ |
drh | 380d685 | 2013-11-20 20:58:00 +0000 | [diff] [blame] | 2814 | /* If there is more header available for parsing in the record, try |
| 2815 | ** to extract additional fields up through the p2+1-th field |
drh | 35cd643 | 2009-06-05 14:17:21 +0000 | [diff] [blame] | 2816 | */ |
drh | c8606e4 | 2013-11-20 19:28:03 +0000 | [diff] [blame] | 2817 | if( pC->iHdrOffset<aOffset[0] ){ |
| 2818 | /* Make sure zData points to enough of the record to cover the header. */ |
| 2819 | if( pC->aRow==0 ){ |
| 2820 | memset(&sMem, 0, sizeof(sMem)); |
drh | 2a74006 | 2020-02-05 18:28:17 +0000 | [diff] [blame] | 2821 | rc = sqlite3VdbeMemFromBtreeZeroOffset(pC->uc.pCursor,aOffset[0],&sMem); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 2822 | if( rc!=SQLITE_OK ) goto abort_due_to_error; |
drh | c8606e4 | 2013-11-20 19:28:03 +0000 | [diff] [blame] | 2823 | zData = (u8*)sMem.z; |
| 2824 | }else{ |
| 2825 | zData = pC->aRow; |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2826 | } |
drh | c8606e4 | 2013-11-20 19:28:03 +0000 | [diff] [blame] | 2827 | |
drh | 0c8f760 | 2014-09-19 16:56:45 +0000 | [diff] [blame] | 2828 | /* 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] | 2829 | op_column_read_header: |
drh | c8606e4 | 2013-11-20 19:28:03 +0000 | [diff] [blame] | 2830 | i = pC->nHdrParsed; |
drh | c6ce3883 | 2015-10-15 21:30:24 +0000 | [diff] [blame] | 2831 | offset64 = aOffset[i]; |
drh | c8606e4 | 2013-11-20 19:28:03 +0000 | [diff] [blame] | 2832 | zHdr = zData + pC->iHdrOffset; |
| 2833 | zEndHdr = zData + aOffset[0]; |
drh | 95b225a | 2017-08-16 11:04:22 +0000 | [diff] [blame] | 2834 | testcase( zHdr>=zEndHdr ); |
drh | c8606e4 | 2013-11-20 19:28:03 +0000 | [diff] [blame] | 2835 | do{ |
drh | c332e04 | 2019-02-12 21:04:33 +0000 | [diff] [blame] | 2836 | if( (pC->aType[i] = t = zHdr[0])<0x80 ){ |
drh | c8606e4 | 2013-11-20 19:28:03 +0000 | [diff] [blame] | 2837 | zHdr++; |
drh | faf3727 | 2015-10-16 14:23:42 +0000 | [diff] [blame] | 2838 | offset64 += sqlite3VdbeOneByteSerialTypeLen(t); |
drh | 5a077b7 | 2011-08-29 02:16:18 +0000 | [diff] [blame] | 2839 | }else{ |
drh | c8606e4 | 2013-11-20 19:28:03 +0000 | [diff] [blame] | 2840 | zHdr += sqlite3GetVarint32(zHdr, &t); |
drh | c332e04 | 2019-02-12 21:04:33 +0000 | [diff] [blame] | 2841 | pC->aType[i] = t; |
drh | faf3727 | 2015-10-16 14:23:42 +0000 | [diff] [blame] | 2842 | offset64 += sqlite3VdbeSerialTypeLen(t); |
drh | 5a077b7 | 2011-08-29 02:16:18 +0000 | [diff] [blame] | 2843 | } |
drh | c332e04 | 2019-02-12 21:04:33 +0000 | [diff] [blame] | 2844 | aOffset[++i] = (u32)(offset64 & 0xffffffff); |
drh | 8deae5a | 2020-07-29 12:23:20 +0000 | [diff] [blame] | 2845 | }while( (u32)i<=p2 && zHdr<zEndHdr ); |
drh | 170c276 | 2016-05-20 21:40:11 +0000 | [diff] [blame] | 2846 | |
drh | 8dd8362 | 2014-10-13 23:39:02 +0000 | [diff] [blame] | 2847 | /* The record is corrupt if any of the following are true: |
| 2848 | ** (1) the bytes of the header extend past the declared header size |
drh | 8dd8362 | 2014-10-13 23:39:02 +0000 | [diff] [blame] | 2849 | ** (2) the entire header was used but not all data was used |
drh | 8dd8362 | 2014-10-13 23:39:02 +0000 | [diff] [blame] | 2850 | ** (3) the end of the data extends beyond the end of the record. |
drh | c8606e4 | 2013-11-20 19:28:03 +0000 | [diff] [blame] | 2851 | */ |
drh | c6ce3883 | 2015-10-15 21:30:24 +0000 | [diff] [blame] | 2852 | if( (zHdr>=zEndHdr && (zHdr>zEndHdr || offset64!=pC->payloadSize)) |
| 2853 | || (offset64 > pC->payloadSize) |
drh | c8606e4 | 2013-11-20 19:28:03 +0000 | [diff] [blame] | 2854 | ){ |
drh | 95b225a | 2017-08-16 11:04:22 +0000 | [diff] [blame] | 2855 | if( aOffset[0]==0 ){ |
| 2856 | i = 0; |
| 2857 | zHdr = zEndHdr; |
| 2858 | }else{ |
| 2859 | if( pC->aRow==0 ) sqlite3VdbeMemRelease(&sMem); |
drh | 74588ce | 2017-09-13 00:13:05 +0000 | [diff] [blame] | 2860 | goto op_column_corrupt; |
drh | 95b225a | 2017-08-16 11:04:22 +0000 | [diff] [blame] | 2861 | } |
danielk1977 | dedf45b | 2006-01-13 17:12:01 +0000 | [diff] [blame] | 2862 | } |
drh | ddb2b4a | 2016-03-25 12:10:32 +0000 | [diff] [blame] | 2863 | |
drh | 170c276 | 2016-05-20 21:40:11 +0000 | [diff] [blame] | 2864 | pC->nHdrParsed = i; |
| 2865 | pC->iHdrOffset = (u32)(zHdr - zData); |
| 2866 | if( pC->aRow==0 ) sqlite3VdbeMemRelease(&sMem); |
mistachkin | 8c7cd6a | 2015-12-16 21:09:53 +0000 | [diff] [blame] | 2867 | }else{ |
drh | 9fbc885 | 2016-01-04 03:48:46 +0000 | [diff] [blame] | 2868 | t = 0; |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2869 | } |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 2870 | |
drh | f2db338 | 2015-04-30 20:33:25 +0000 | [diff] [blame] | 2871 | /* If after trying to extract new entries from the header, nHdrParsed is |
drh | 380d685 | 2013-11-20 20:58:00 +0000 | [diff] [blame] | 2872 | ** still not up to p2, that means that the record has fewer than p2 |
| 2873 | ** columns. So the result will be either the default value or a NULL. |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 2874 | */ |
drh | c8606e4 | 2013-11-20 19:28:03 +0000 | [diff] [blame] | 2875 | if( pC->nHdrParsed<=p2 ){ |
drh | fc56950 | 2022-02-25 20:11:59 +0000 | [diff] [blame] | 2876 | pDest = &aMem[pOp->p3]; |
| 2877 | memAboutToChange(p, pDest); |
drh | c8606e4 | 2013-11-20 19:28:03 +0000 | [diff] [blame] | 2878 | if( pOp->p4type==P4_MEM ){ |
| 2879 | sqlite3VdbeMemShallowCopy(pDest, pOp->p4.pMem, MEM_Static); |
| 2880 | }else{ |
drh | 22e8d83 | 2014-10-29 00:58:38 +0000 | [diff] [blame] | 2881 | sqlite3VdbeMemSetNull(pDest); |
drh | c8606e4 | 2013-11-20 19:28:03 +0000 | [diff] [blame] | 2882 | } |
danielk1977 | 3c9cc8d | 2005-01-17 03:40:08 +0000 | [diff] [blame] | 2883 | goto op_column_out; |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 2884 | } |
drh | 95fa606 | 2015-10-16 13:50:08 +0000 | [diff] [blame] | 2885 | }else{ |
| 2886 | t = pC->aType[p2]; |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 2887 | } |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2888 | |
drh | 380d685 | 2013-11-20 20:58:00 +0000 | [diff] [blame] | 2889 | /* Extract the content for the p2+1-th column. Control can only |
drh | 0c8f760 | 2014-09-19 16:56:45 +0000 | [diff] [blame] | 2890 | ** 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] | 2891 | ** all valid. |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2892 | */ |
drh | c8606e4 | 2013-11-20 19:28:03 +0000 | [diff] [blame] | 2893 | assert( p2<pC->nHdrParsed ); |
| 2894 | assert( rc==SQLITE_OK ); |
drh | fc56950 | 2022-02-25 20:11:59 +0000 | [diff] [blame] | 2895 | pDest = &aMem[pOp->p3]; |
| 2896 | memAboutToChange(p, pDest); |
drh | 75fd054 | 2014-03-01 16:24:44 +0000 | [diff] [blame] | 2897 | assert( sqlite3VdbeCheckMemInvariants(pDest) ); |
drh | a1851ef | 2016-05-20 19:51:28 +0000 | [diff] [blame] | 2898 | if( VdbeMemDynamic(pDest) ){ |
| 2899 | sqlite3VdbeMemSetNull(pDest); |
| 2900 | } |
drh | 95fa606 | 2015-10-16 13:50:08 +0000 | [diff] [blame] | 2901 | assert( t==pC->aType[p2] ); |
drh | c8606e4 | 2013-11-20 19:28:03 +0000 | [diff] [blame] | 2902 | if( pC->szRow>=aOffset[p2+1] ){ |
drh | 380d685 | 2013-11-20 20:58:00 +0000 | [diff] [blame] | 2903 | /* This is the common case where the desired content fits on the original |
| 2904 | ** page - where the content is not on an overflow page */ |
drh | 69f6e25 | 2016-01-11 18:05:00 +0000 | [diff] [blame] | 2905 | zData = pC->aRow + aOffset[p2]; |
| 2906 | if( t<12 ){ |
| 2907 | sqlite3VdbeSerialGet(zData, t, pDest); |
| 2908 | }else{ |
| 2909 | /* If the column value is a string, we need a persistent value, not |
| 2910 | ** a MEM_Ephem value. This branch is a fast short-cut that is equivalent |
| 2911 | ** to calling sqlite3VdbeSerialGet() and sqlite3VdbeDeephemeralize(). |
| 2912 | */ |
| 2913 | static const u16 aFlag[] = { MEM_Blob, MEM_Str|MEM_Term }; |
| 2914 | pDest->n = len = (t-12)/2; |
drh | a1851ef | 2016-05-20 19:51:28 +0000 | [diff] [blame] | 2915 | pDest->enc = encoding; |
drh | 69f6e25 | 2016-01-11 18:05:00 +0000 | [diff] [blame] | 2916 | if( pDest->szMalloc < len+2 ){ |
drh | 9090df6 | 2022-02-26 14:39:08 +0000 | [diff] [blame] | 2917 | if( len>db->aLimit[SQLITE_LIMIT_LENGTH] ) goto too_big; |
drh | 69f6e25 | 2016-01-11 18:05:00 +0000 | [diff] [blame] | 2918 | pDest->flags = MEM_Null; |
| 2919 | if( sqlite3VdbeMemGrow(pDest, len+2, 0) ) goto no_mem; |
| 2920 | }else{ |
| 2921 | pDest->z = pDest->zMalloc; |
| 2922 | } |
| 2923 | memcpy(pDest->z, zData, len); |
| 2924 | pDest->z[len] = 0; |
| 2925 | pDest->z[len+1] = 0; |
| 2926 | pDest->flags = aFlag[t&1]; |
| 2927 | } |
danielk1977 | 36963fd | 2005-02-19 08:18:05 +0000 | [diff] [blame] | 2928 | }else{ |
drh | a1851ef | 2016-05-20 19:51:28 +0000 | [diff] [blame] | 2929 | pDest->enc = encoding; |
drh | 58c9608 | 2013-12-23 11:33:32 +0000 | [diff] [blame] | 2930 | /* This branch happens only when content is on overflow pages */ |
drh | 380d685 | 2013-11-20 20:58:00 +0000 | [diff] [blame] | 2931 | if( ((pOp->p5 & (OPFLAG_LENGTHARG|OPFLAG_TYPEOFARG))!=0 |
| 2932 | && ((t>=12 && (t&1)==0) || (pOp->p5 & OPFLAG_TYPEOFARG)!=0)) |
| 2933 | || (len = sqlite3VdbeSerialTypeLen(t))==0 |
drh | c8606e4 | 2013-11-20 19:28:03 +0000 | [diff] [blame] | 2934 | ){ |
drh | 2a2a696 | 2014-09-16 18:22:44 +0000 | [diff] [blame] | 2935 | /* Content is irrelevant for |
| 2936 | ** 1. the typeof() function, |
| 2937 | ** 2. the length(X) function if X is a blob, and |
| 2938 | ** 3. if the content length is zero. |
| 2939 | ** So we might as well use bogus content rather than reading |
dan | 1f9144e | 2017-03-17 13:59:06 +0000 | [diff] [blame] | 2940 | ** content from disk. |
| 2941 | ** |
| 2942 | ** Although sqlite3VdbeSerialGet() may read at most 8 bytes from the |
| 2943 | ** buffer passed to it, debugging function VdbeMemPrettyPrint() may |
drh | cbae3f8 | 2020-01-06 20:48:45 +0000 | [diff] [blame] | 2944 | ** read more. Use the global constant sqlite3CtypeMap[] as the array, |
| 2945 | ** as that array is 256 bytes long (plenty for VdbeMemPrettyPrint()) |
| 2946 | ** and it begins with a bunch of zeros. |
dan | 1f9144e | 2017-03-17 13:59:06 +0000 | [diff] [blame] | 2947 | */ |
drh | cbae3f8 | 2020-01-06 20:48:45 +0000 | [diff] [blame] | 2948 | sqlite3VdbeSerialGet((u8*)sqlite3CtypeMap, t, pDest); |
danielk1977 | aee18ef | 2005-03-09 12:26:50 +0000 | [diff] [blame] | 2949 | }else{ |
drh | 9090df6 | 2022-02-26 14:39:08 +0000 | [diff] [blame] | 2950 | if( len>db->aLimit[SQLITE_LIMIT_LENGTH] ) goto too_big; |
drh | cb3cabd | 2016-11-25 19:18:28 +0000 | [diff] [blame] | 2951 | rc = sqlite3VdbeMemFromBtree(pC->uc.pCursor, aOffset[p2], len, pDest); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 2952 | if( rc!=SQLITE_OK ) goto abort_due_to_error; |
| 2953 | sqlite3VdbeSerialGet((const u8*)pDest->z, t, pDest); |
| 2954 | pDest->flags &= ~MEM_Ephem; |
danielk1977 | aee18ef | 2005-03-09 12:26:50 +0000 | [diff] [blame] | 2955 | } |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 2956 | } |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 2957 | |
danielk1977 | 3c9cc8d | 2005-01-17 03:40:08 +0000 | [diff] [blame] | 2958 | op_column_out: |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 2959 | UPDATE_MAX_BLOBSIZE(pDest); |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 2960 | REGISTER_TRACE(pOp->p3, pDest); |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2961 | break; |
drh | 74588ce | 2017-09-13 00:13:05 +0000 | [diff] [blame] | 2962 | |
| 2963 | op_column_corrupt: |
| 2964 | if( aOp[0].p3>0 ){ |
| 2965 | pOp = &aOp[aOp[0].p3-1]; |
| 2966 | break; |
| 2967 | }else{ |
| 2968 | rc = SQLITE_CORRUPT_BKPT; |
| 2969 | goto abort_due_to_error; |
| 2970 | } |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2971 | } |
| 2972 | |
drh | 926aac5 | 2021-11-03 14:02:48 +0000 | [diff] [blame] | 2973 | /* Opcode: TypeCheck P1 P2 P3 P4 * |
drh | 72532f5 | 2021-08-18 19:22:27 +0000 | [diff] [blame] | 2974 | ** Synopsis: typecheck(r[P1@P2]) |
| 2975 | ** |
| 2976 | ** Apply affinities to the range of P2 registers beginning with P1. |
| 2977 | ** Take the affinities from the Table object in P4. If any value |
| 2978 | ** cannot be coerced into the correct type, then raise an error. |
| 2979 | ** |
| 2980 | ** This opcode is similar to OP_Affinity except that this opcode |
| 2981 | ** forces the register type to the Table column type. This is used |
| 2982 | ** to implement "strict affinity". |
drh | 71c770f | 2021-08-19 16:29:33 +0000 | [diff] [blame] | 2983 | ** |
drh | 926aac5 | 2021-11-03 14:02:48 +0000 | [diff] [blame] | 2984 | ** GENERATED ALWAYS AS ... STATIC columns are only checked if P3 |
| 2985 | ** is zero. When P3 is non-zero, no type checking occurs for |
| 2986 | ** static generated columns. Virtual columns are computed at query time |
| 2987 | ** and so they are never checked. |
| 2988 | ** |
drh | 71c770f | 2021-08-19 16:29:33 +0000 | [diff] [blame] | 2989 | ** Preconditions: |
| 2990 | ** |
| 2991 | ** <ul> |
| 2992 | ** <li> P2 should be the number of non-virtual columns in the |
| 2993 | ** table of P4. |
| 2994 | ** <li> Table P4 should be a STRICT table. |
| 2995 | ** </ul> |
| 2996 | ** |
| 2997 | ** If any precondition is false, an assertion fault occurs. |
drh | 72532f5 | 2021-08-18 19:22:27 +0000 | [diff] [blame] | 2998 | */ |
| 2999 | case OP_TypeCheck: { |
| 3000 | Table *pTab; |
| 3001 | Column *aCol; |
| 3002 | int i; |
| 3003 | |
| 3004 | assert( pOp->p4type==P4_TABLE ); |
| 3005 | pTab = pOp->p4.pTab; |
| 3006 | assert( pTab->tabFlags & TF_Strict ); |
drh | 71c770f | 2021-08-19 16:29:33 +0000 | [diff] [blame] | 3007 | assert( pTab->nNVCol==pOp->p2 ); |
drh | 72532f5 | 2021-08-18 19:22:27 +0000 | [diff] [blame] | 3008 | aCol = pTab->aCol; |
| 3009 | pIn1 = &aMem[pOp->p1]; |
| 3010 | for(i=0; i<pTab->nCol; i++){ |
drh | 926aac5 | 2021-11-03 14:02:48 +0000 | [diff] [blame] | 3011 | if( aCol[i].colFlags & COLFLAG_GENERATED ){ |
| 3012 | if( aCol[i].colFlags & COLFLAG_VIRTUAL ) continue; |
| 3013 | if( pOp->p3 ){ pIn1++; continue; } |
| 3014 | } |
drh | 72532f5 | 2021-08-18 19:22:27 +0000 | [diff] [blame] | 3015 | assert( pIn1 < &aMem[pOp->p1+pOp->p2] ); |
| 3016 | applyAffinity(pIn1, aCol[i].affinity, encoding); |
| 3017 | if( (pIn1->flags & MEM_Null)==0 ){ |
| 3018 | switch( aCol[i].eCType ){ |
| 3019 | case COLTYPE_BLOB: { |
| 3020 | if( (pIn1->flags & MEM_Blob)==0 ) goto vdbe_type_error; |
| 3021 | break; |
| 3022 | } |
| 3023 | case COLTYPE_INTEGER: |
| 3024 | case COLTYPE_INT: { |
| 3025 | if( (pIn1->flags & MEM_Int)==0 ) goto vdbe_type_error; |
| 3026 | break; |
| 3027 | } |
| 3028 | case COLTYPE_TEXT: { |
| 3029 | if( (pIn1->flags & MEM_Str)==0 ) goto vdbe_type_error; |
| 3030 | break; |
| 3031 | } |
drh | 2a0eefd | 2021-08-21 20:54:19 +0000 | [diff] [blame] | 3032 | case COLTYPE_REAL: { |
drh | 1f3366c | 2022-01-17 23:37:25 +0000 | [diff] [blame] | 3033 | testcase( (pIn1->flags & (MEM_Real|MEM_IntReal))==MEM_Real ); |
| 3034 | testcase( (pIn1->flags & (MEM_Real|MEM_IntReal))==MEM_IntReal ); |
drh | 72532f5 | 2021-08-18 19:22:27 +0000 | [diff] [blame] | 3035 | if( pIn1->flags & MEM_Int ){ |
| 3036 | /* When applying REAL affinity, if the result is still an MEM_Int |
| 3037 | ** that will fit in 6 bytes, then change the type to MEM_IntReal |
| 3038 | ** so that we keep the high-resolution integer value but know that |
| 3039 | ** the type really wants to be REAL. */ |
| 3040 | testcase( pIn1->u.i==140737488355328LL ); |
| 3041 | testcase( pIn1->u.i==140737488355327LL ); |
| 3042 | testcase( pIn1->u.i==-140737488355328LL ); |
| 3043 | testcase( pIn1->u.i==-140737488355329LL ); |
| 3044 | if( pIn1->u.i<=140737488355327LL && pIn1->u.i>=-140737488355328LL){ |
| 3045 | pIn1->flags |= MEM_IntReal; |
| 3046 | pIn1->flags &= ~MEM_Int; |
| 3047 | }else{ |
| 3048 | pIn1->u.r = (double)pIn1->u.i; |
| 3049 | pIn1->flags |= MEM_Real; |
| 3050 | pIn1->flags &= ~MEM_Int; |
| 3051 | } |
drh | 1f3366c | 2022-01-17 23:37:25 +0000 | [diff] [blame] | 3052 | }else if( (pIn1->flags & (MEM_Real|MEM_IntReal))==0 ){ |
drh | 72532f5 | 2021-08-18 19:22:27 +0000 | [diff] [blame] | 3053 | goto vdbe_type_error; |
| 3054 | } |
| 3055 | break; |
| 3056 | } |
drh | 2a0eefd | 2021-08-21 20:54:19 +0000 | [diff] [blame] | 3057 | default: { |
drh | b9fd010 | 2021-08-23 10:28:02 +0000 | [diff] [blame] | 3058 | /* COLTYPE_ANY. Accept anything. */ |
drh | 2a0eefd | 2021-08-21 20:54:19 +0000 | [diff] [blame] | 3059 | break; |
| 3060 | } |
drh | 72532f5 | 2021-08-18 19:22:27 +0000 | [diff] [blame] | 3061 | } |
| 3062 | } |
| 3063 | REGISTER_TRACE((int)(pIn1-aMem), pIn1); |
| 3064 | pIn1++; |
| 3065 | } |
| 3066 | assert( pIn1 == &aMem[pOp->p1+pOp->p2] ); |
| 3067 | break; |
| 3068 | |
| 3069 | vdbe_type_error: |
drh | faf9c77 | 2021-08-20 08:05:42 +0000 | [diff] [blame] | 3070 | sqlite3VdbeError(p, "cannot store %s value in %s column %s.%s", |
| 3071 | vdbeMemTypeName(pIn1), sqlite3StdType[aCol[i].eCType-1], |
| 3072 | pTab->zName, aCol[i].zCnName); |
drh | 72532f5 | 2021-08-18 19:22:27 +0000 | [diff] [blame] | 3073 | rc = SQLITE_CONSTRAINT_DATATYPE; |
| 3074 | goto abort_due_to_error; |
| 3075 | } |
| 3076 | |
danielk1977 | 751de56 | 2008-04-18 09:01:15 +0000 | [diff] [blame] | 3077 | /* Opcode: Affinity P1 P2 * P4 * |
drh | f63552b | 2013-10-30 00:25:03 +0000 | [diff] [blame] | 3078 | ** Synopsis: affinity(r[P1@P2]) |
danielk1977 | 751de56 | 2008-04-18 09:01:15 +0000 | [diff] [blame] | 3079 | ** |
| 3080 | ** Apply affinities to a range of P2 registers starting with P1. |
| 3081 | ** |
drh | bb6783b | 2017-04-29 18:02:49 +0000 | [diff] [blame] | 3082 | ** P4 is a string that is P2 characters long. The N-th character of the |
| 3083 | ** string indicates the column affinity that should be used for the N-th |
danielk1977 | 751de56 | 2008-04-18 09:01:15 +0000 | [diff] [blame] | 3084 | ** memory cell in the range. |
| 3085 | */ |
| 3086 | case OP_Affinity: { |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 3087 | const char *zAffinity; /* The affinity to be applied */ |
danielk1977 | 751de56 | 2008-04-18 09:01:15 +0000 | [diff] [blame] | 3088 | |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3089 | zAffinity = pOp->p4.z; |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 3090 | assert( zAffinity!=0 ); |
drh | 662c50e | 2017-04-01 20:14:01 +0000 | [diff] [blame] | 3091 | assert( pOp->p2>0 ); |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 3092 | assert( zAffinity[pOp->p2]==0 ); |
| 3093 | pIn1 = &aMem[pOp->p1]; |
drh | 122c514 | 2019-07-29 05:23:01 +0000 | [diff] [blame] | 3094 | while( 1 /*exit-by-break*/ ){ |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 3095 | assert( pIn1 <= &p->aMem[(p->nMem+1 - p->nCursor)] ); |
drh | b5f6243 | 2019-12-10 02:48:41 +0000 | [diff] [blame] | 3096 | assert( zAffinity[0]==SQLITE_AFF_NONE || memIsValid(pIn1) ); |
drh | 83a1daf | 2019-05-01 18:59:33 +0000 | [diff] [blame] | 3097 | applyAffinity(pIn1, zAffinity[0], encoding); |
| 3098 | if( zAffinity[0]==SQLITE_AFF_REAL && (pIn1->flags & MEM_Int)!=0 ){ |
drh | 337cc39 | 2019-07-29 06:06:53 +0000 | [diff] [blame] | 3099 | /* When applying REAL affinity, if the result is still an MEM_Int |
| 3100 | ** that will fit in 6 bytes, then change the type to MEM_IntReal |
| 3101 | ** so that we keep the high-resolution integer value but know that |
| 3102 | ** the type really wants to be REAL. */ |
| 3103 | testcase( pIn1->u.i==140737488355328LL ); |
| 3104 | testcase( pIn1->u.i==140737488355327LL ); |
| 3105 | testcase( pIn1->u.i==-140737488355328LL ); |
| 3106 | testcase( pIn1->u.i==-140737488355329LL ); |
| 3107 | if( pIn1->u.i<=140737488355327LL && pIn1->u.i>=-140737488355328LL ){ |
| 3108 | pIn1->flags |= MEM_IntReal; |
| 3109 | pIn1->flags &= ~MEM_Int; |
| 3110 | }else{ |
| 3111 | pIn1->u.r = (double)pIn1->u.i; |
| 3112 | pIn1->flags |= MEM_Real; |
| 3113 | pIn1->flags &= ~MEM_Int; |
| 3114 | } |
drh | 83a1daf | 2019-05-01 18:59:33 +0000 | [diff] [blame] | 3115 | } |
drh | 6fcc1ec | 2019-05-01 14:41:47 +0000 | [diff] [blame] | 3116 | REGISTER_TRACE((int)(pIn1-aMem), pIn1); |
drh | 83a1daf | 2019-05-01 18:59:33 +0000 | [diff] [blame] | 3117 | zAffinity++; |
| 3118 | if( zAffinity[0]==0 ) break; |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 3119 | pIn1++; |
drh | 83a1daf | 2019-05-01 18:59:33 +0000 | [diff] [blame] | 3120 | } |
danielk1977 | 751de56 | 2008-04-18 09:01:15 +0000 | [diff] [blame] | 3121 | break; |
| 3122 | } |
| 3123 | |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 3124 | /* Opcode: MakeRecord P1 P2 P3 P4 * |
drh | f63552b | 2013-10-30 00:25:03 +0000 | [diff] [blame] | 3125 | ** Synopsis: r[P3]=mkrec(r[P1@P2]) |
drh | 7a224de | 2004-06-02 01:22:02 +0000 | [diff] [blame] | 3126 | ** |
drh | 710c484 | 2010-08-30 01:17:20 +0000 | [diff] [blame] | 3127 | ** Convert P2 registers beginning with P1 into the [record format] |
| 3128 | ** use as a data record in a database table or as a key |
| 3129 | ** in an index. The OP_Column opcode can decode the record later. |
drh | 7a224de | 2004-06-02 01:22:02 +0000 | [diff] [blame] | 3130 | ** |
drh | bb6783b | 2017-04-29 18:02:49 +0000 | [diff] [blame] | 3131 | ** P4 may be a string that is P2 characters long. The N-th character of the |
| 3132 | ** string indicates the column affinity that should be used for the N-th |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3133 | ** field of the index key. |
drh | 7a224de | 2004-06-02 01:22:02 +0000 | [diff] [blame] | 3134 | ** |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 3135 | ** The mapping from character to affinity is given by the SQLITE_AFF_ |
| 3136 | ** macros defined in sqliteInt.h. |
drh | 7a224de | 2004-06-02 01:22:02 +0000 | [diff] [blame] | 3137 | ** |
drh | 05883a3 | 2015-06-02 15:32:08 +0000 | [diff] [blame] | 3138 | ** If P4 is NULL then all index fields have the affinity BLOB. |
drh | da36933 | 2020-06-29 20:09:04 +0000 | [diff] [blame] | 3139 | ** |
| 3140 | ** The meaning of P5 depends on whether or not the SQLITE_ENABLE_NULL_TRIM |
| 3141 | ** compile-time option is enabled: |
| 3142 | ** |
| 3143 | ** * If SQLITE_ENABLE_NULL_TRIM is enabled, then the P5 is the index |
| 3144 | ** of the right-most table that can be null-trimmed. |
| 3145 | ** |
| 3146 | ** * If SQLITE_ENABLE_NULL_TRIM is omitted, then P5 has the value |
| 3147 | ** OPFLAG_NOCHNG_MAGIC if the OP_MakeRecord opcode is allowed to |
| 3148 | ** accept no-change records with serial_type 10. This value is |
| 3149 | ** only used inside an assert() and does not affect the end result. |
drh | 7f057c9 | 2005-06-24 03:53:06 +0000 | [diff] [blame] | 3150 | */ |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 3151 | case OP_MakeRecord: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3152 | Mem *pRec; /* The new record */ |
| 3153 | u64 nData; /* Number of bytes of data space */ |
| 3154 | int nHdr; /* Number of bytes of header space */ |
| 3155 | i64 nByte; /* Data space required for this record */ |
drh | 4a33507 | 2015-04-11 02:08:48 +0000 | [diff] [blame] | 3156 | i64 nZero; /* Number of zero bytes at the end of the record */ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3157 | int nVarint; /* Number of bytes in a varint */ |
| 3158 | u32 serial_type; /* Type field */ |
| 3159 | Mem *pData0; /* First field to be combined into the record */ |
| 3160 | Mem *pLast; /* Last field of the record */ |
| 3161 | int nField; /* Number of fields in the record */ |
| 3162 | char *zAffinity; /* The affinity string for the record */ |
drh | be37c12 | 2015-10-16 14:54:17 +0000 | [diff] [blame] | 3163 | u32 len; /* Length of a field */ |
drh | b70b0df | 2019-04-30 01:08:42 +0000 | [diff] [blame] | 3164 | u8 *zHdr; /* Where to write next byte of the header */ |
| 3165 | u8 *zPayload; /* Where to write next byte of the payload */ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3166 | |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 3167 | /* Assuming the record contains N fields, the record format looks |
| 3168 | ** like this: |
| 3169 | ** |
drh | 7a224de | 2004-06-02 01:22:02 +0000 | [diff] [blame] | 3170 | ** ------------------------------------------------------------------------ |
| 3171 | ** | hdr-size | type 0 | type 1 | ... | type N-1 | data0 | ... | data N-1 | |
| 3172 | ** ------------------------------------------------------------------------ |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 3173 | ** |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3174 | ** 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] | 3175 | ** and so forth. |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 3176 | ** |
| 3177 | ** Each type field is a varint representing the serial type of the |
| 3178 | ** corresponding data element (see sqlite3VdbeSerialType()). The |
drh | 7a224de | 2004-06-02 01:22:02 +0000 | [diff] [blame] | 3179 | ** hdr-size field is also a varint which is the offset from the beginning |
| 3180 | ** of the record to data0. |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 3181 | */ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3182 | nData = 0; /* Number of bytes of data space */ |
| 3183 | nHdr = 0; /* Number of bytes of header space */ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3184 | nZero = 0; /* Number of zero bytes at the end of the record */ |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 3185 | nField = pOp->p1; |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 3186 | zAffinity = pOp->p4.z; |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 3187 | 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] | 3188 | pData0 = &aMem[nField]; |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 3189 | nField = pOp->p2; |
| 3190 | pLast = &pData0[nField-1]; |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 3191 | |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 3192 | /* Identify the output register */ |
| 3193 | assert( pOp->p3<pOp->p1 || pOp->p3>=pOp->p1+pOp->p2 ); |
| 3194 | pOut = &aMem[pOp->p3]; |
| 3195 | memAboutToChange(p, pOut); |
| 3196 | |
drh | 3e6c060 | 2013-12-10 20:53:01 +0000 | [diff] [blame] | 3197 | /* Apply the requested affinity to all inputs |
| 3198 | */ |
| 3199 | assert( pData0<=pLast ); |
| 3200 | if( zAffinity ){ |
| 3201 | pRec = pData0; |
| 3202 | do{ |
drh | 5ad1251 | 2019-05-09 16:22:51 +0000 | [diff] [blame] | 3203 | applyAffinity(pRec, zAffinity[0], encoding); |
dan | be81262 | 2019-05-17 15:59:11 +0000 | [diff] [blame] | 3204 | if( zAffinity[0]==SQLITE_AFF_REAL && (pRec->flags & MEM_Int) ){ |
| 3205 | pRec->flags |= MEM_IntReal; |
| 3206 | pRec->flags &= ~(MEM_Int); |
| 3207 | } |
drh | 5ad1251 | 2019-05-09 16:22:51 +0000 | [diff] [blame] | 3208 | REGISTER_TRACE((int)(pRec-aMem), pRec); |
| 3209 | zAffinity++; |
| 3210 | pRec++; |
drh | 57bf4a8 | 2014-02-17 14:59:22 +0000 | [diff] [blame] | 3211 | assert( zAffinity[0]==0 || pRec<=pLast ); |
| 3212 | }while( zAffinity[0] ); |
drh | 3e6c060 | 2013-12-10 20:53:01 +0000 | [diff] [blame] | 3213 | } |
| 3214 | |
drh | d447dce | 2017-01-25 20:55:11 +0000 | [diff] [blame] | 3215 | #ifdef SQLITE_ENABLE_NULL_TRIM |
drh | 585ce19 | 2017-01-25 14:58:27 +0000 | [diff] [blame] | 3216 | /* NULLs can be safely trimmed from the end of the record, as long as |
| 3217 | ** as the schema format is 2 or more and none of the omitted columns |
| 3218 | ** have a non-NULL default value. Also, the record must be left with |
| 3219 | ** at least one field. If P5>0 then it will be one more than the |
| 3220 | ** index of the right-most column with a non-NULL default value */ |
| 3221 | if( pOp->p5 ){ |
| 3222 | while( (pLast->flags & MEM_Null)!=0 && nField>pOp->p5 ){ |
| 3223 | pLast--; |
| 3224 | nField--; |
| 3225 | } |
| 3226 | } |
drh | d447dce | 2017-01-25 20:55:11 +0000 | [diff] [blame] | 3227 | #endif |
drh | 585ce19 | 2017-01-25 14:58:27 +0000 | [diff] [blame] | 3228 | |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 3229 | /* Loop through the elements that will make up the record to figure |
drh | 76fd7be | 2019-07-11 19:50:18 +0000 | [diff] [blame] | 3230 | ** out how much space is required for the new record. After this loop, |
| 3231 | ** the Mem.uTemp field of each term should hold the serial-type that will |
| 3232 | ** be used for that term in the generated record: |
| 3233 | ** |
| 3234 | ** Mem.uTemp value type |
| 3235 | ** --------------- --------------- |
| 3236 | ** 0 NULL |
| 3237 | ** 1 1-byte signed integer |
| 3238 | ** 2 2-byte signed integer |
| 3239 | ** 3 3-byte signed integer |
| 3240 | ** 4 4-byte signed integer |
| 3241 | ** 5 6-byte signed integer |
| 3242 | ** 6 8-byte signed integer |
| 3243 | ** 7 IEEE float |
| 3244 | ** 8 Integer constant 0 |
| 3245 | ** 9 Integer constant 1 |
| 3246 | ** 10,11 reserved for expansion |
| 3247 | ** N>=12 and even BLOB |
| 3248 | ** N>=13 and odd text |
| 3249 | ** |
| 3250 | ** The following additional values are computed: |
| 3251 | ** nHdr Number of bytes needed for the record header |
| 3252 | ** nData Number of bytes of data space needed for the record |
| 3253 | ** nZero Zero bytes at the end of the record |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 3254 | */ |
drh | 038b7bc | 2013-12-09 23:17:22 +0000 | [diff] [blame] | 3255 | pRec = pLast; |
drh | 59bf00c | 2013-12-08 23:33:28 +0000 | [diff] [blame] | 3256 | do{ |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 3257 | assert( memIsValid(pRec) ); |
drh | c1da439 | 2019-07-11 19:22:36 +0000 | [diff] [blame] | 3258 | if( pRec->flags & MEM_Null ){ |
| 3259 | if( pRec->flags & MEM_Zero ){ |
drh | 41fb367 | 2018-01-12 23:18:38 +0000 | [diff] [blame] | 3260 | /* Values with MEM_Null and MEM_Zero are created by xColumn virtual |
| 3261 | ** table methods that never invoke sqlite3_result_xxxxx() while |
| 3262 | ** computing an unchanging column value in an UPDATE statement. |
| 3263 | ** Give such values a special internal-use-only serial-type of 10 |
| 3264 | ** so that they can be passed through to xUpdate and have |
| 3265 | ** a true sqlite3_value_nochange(). */ |
drh | da36933 | 2020-06-29 20:09:04 +0000 | [diff] [blame] | 3266 | #ifndef SQLITE_ENABLE_NULL_TRIM |
drh | 41fb367 | 2018-01-12 23:18:38 +0000 | [diff] [blame] | 3267 | assert( pOp->p5==OPFLAG_NOCHNG_MAGIC || CORRUPT_DB ); |
drh | da36933 | 2020-06-29 20:09:04 +0000 | [diff] [blame] | 3268 | #endif |
drh | c1da439 | 2019-07-11 19:22:36 +0000 | [diff] [blame] | 3269 | pRec->uTemp = 10; |
drh | 038b7bc | 2013-12-09 23:17:22 +0000 | [diff] [blame] | 3270 | }else{ |
drh | 76fd7be | 2019-07-11 19:50:18 +0000 | [diff] [blame] | 3271 | pRec->uTemp = 0; |
drh | 038b7bc | 2013-12-09 23:17:22 +0000 | [diff] [blame] | 3272 | } |
drh | c1da439 | 2019-07-11 19:22:36 +0000 | [diff] [blame] | 3273 | nHdr++; |
| 3274 | }else if( pRec->flags & (MEM_Int|MEM_IntReal) ){ |
| 3275 | /* Figure out whether to use 1, 2, 4, 6 or 8 bytes. */ |
| 3276 | i64 i = pRec->u.i; |
drh | 9c3bb59 | 2019-07-30 21:00:13 +0000 | [diff] [blame] | 3277 | u64 uu; |
drh | c1da439 | 2019-07-11 19:22:36 +0000 | [diff] [blame] | 3278 | testcase( pRec->flags & MEM_Int ); |
| 3279 | testcase( pRec->flags & MEM_IntReal ); |
| 3280 | if( i<0 ){ |
drh | 9c3bb59 | 2019-07-30 21:00:13 +0000 | [diff] [blame] | 3281 | uu = ~i; |
drh | c1da439 | 2019-07-11 19:22:36 +0000 | [diff] [blame] | 3282 | }else{ |
drh | 9c3bb59 | 2019-07-30 21:00:13 +0000 | [diff] [blame] | 3283 | uu = i; |
drh | c1da439 | 2019-07-11 19:22:36 +0000 | [diff] [blame] | 3284 | } |
| 3285 | nHdr++; |
drh | 9c3bb59 | 2019-07-30 21:00:13 +0000 | [diff] [blame] | 3286 | testcase( uu==127 ); testcase( uu==128 ); |
| 3287 | testcase( uu==32767 ); testcase( uu==32768 ); |
| 3288 | testcase( uu==8388607 ); testcase( uu==8388608 ); |
drh | 16f56e8 | 2022-02-21 14:30:59 +0000 | [diff] [blame] | 3289 | testcase( uu==2147483647 ); testcase( uu==2147483648LL ); |
drh | 9c3bb59 | 2019-07-30 21:00:13 +0000 | [diff] [blame] | 3290 | testcase( uu==140737488355327LL ); testcase( uu==140737488355328LL ); |
| 3291 | if( uu<=127 ){ |
drh | a0318fd | 2022-02-26 23:01:25 +0000 | [diff] [blame] | 3292 | if( (i&1)==i && p->minWriteFileFormat>=4 ){ |
drh | 9c3bb59 | 2019-07-30 21:00:13 +0000 | [diff] [blame] | 3293 | pRec->uTemp = 8+(u32)uu; |
drh | c1da439 | 2019-07-11 19:22:36 +0000 | [diff] [blame] | 3294 | }else{ |
| 3295 | nData++; |
| 3296 | pRec->uTemp = 1; |
| 3297 | } |
drh | 9c3bb59 | 2019-07-30 21:00:13 +0000 | [diff] [blame] | 3298 | }else if( uu<=32767 ){ |
drh | c1da439 | 2019-07-11 19:22:36 +0000 | [diff] [blame] | 3299 | nData += 2; |
| 3300 | pRec->uTemp = 2; |
drh | 9c3bb59 | 2019-07-30 21:00:13 +0000 | [diff] [blame] | 3301 | }else if( uu<=8388607 ){ |
drh | c1da439 | 2019-07-11 19:22:36 +0000 | [diff] [blame] | 3302 | nData += 3; |
| 3303 | pRec->uTemp = 3; |
drh | 9c3bb59 | 2019-07-30 21:00:13 +0000 | [diff] [blame] | 3304 | }else if( uu<=2147483647 ){ |
drh | c1da439 | 2019-07-11 19:22:36 +0000 | [diff] [blame] | 3305 | nData += 4; |
| 3306 | pRec->uTemp = 4; |
drh | 9c3bb59 | 2019-07-30 21:00:13 +0000 | [diff] [blame] | 3307 | }else if( uu<=140737488355327LL ){ |
drh | c1da439 | 2019-07-11 19:22:36 +0000 | [diff] [blame] | 3308 | nData += 6; |
| 3309 | pRec->uTemp = 5; |
| 3310 | }else{ |
| 3311 | nData += 8; |
| 3312 | if( pRec->flags & MEM_IntReal ){ |
| 3313 | /* If the value is IntReal and is going to take up 8 bytes to store |
| 3314 | ** as an integer, then we might as well make it an 8-byte floating |
| 3315 | ** point value */ |
| 3316 | pRec->u.r = (double)pRec->u.i; |
| 3317 | pRec->flags &= ~MEM_IntReal; |
| 3318 | pRec->flags |= MEM_Real; |
| 3319 | pRec->uTemp = 7; |
| 3320 | }else{ |
| 3321 | pRec->uTemp = 6; |
| 3322 | } |
| 3323 | } |
| 3324 | }else if( pRec->flags & MEM_Real ){ |
| 3325 | nHdr++; |
| 3326 | nData += 8; |
| 3327 | pRec->uTemp = 7; |
| 3328 | }else{ |
| 3329 | assert( db->mallocFailed || pRec->flags&(MEM_Str|MEM_Blob) ); |
| 3330 | assert( pRec->n>=0 ); |
| 3331 | len = (u32)pRec->n; |
| 3332 | serial_type = (len*2) + 12 + ((pRec->flags & MEM_Str)!=0); |
| 3333 | if( pRec->flags & MEM_Zero ){ |
| 3334 | serial_type += pRec->u.nZero*2; |
| 3335 | if( nData ){ |
| 3336 | if( sqlite3VdbeMemExpandBlob(pRec) ) goto no_mem; |
| 3337 | len += pRec->u.nZero; |
| 3338 | }else{ |
| 3339 | nZero += pRec->u.nZero; |
| 3340 | } |
| 3341 | } |
| 3342 | nData += len; |
| 3343 | nHdr += sqlite3VarintLen(serial_type); |
| 3344 | pRec->uTemp = serial_type; |
drh | fdf972a | 2007-05-02 13:30:27 +0000 | [diff] [blame] | 3345 | } |
drh | 45c3c66 | 2016-04-07 14:16:16 +0000 | [diff] [blame] | 3346 | if( pRec==pData0 ) break; |
| 3347 | pRec--; |
| 3348 | }while(1); |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 3349 | |
drh | 654858d | 2014-11-20 02:18:14 +0000 | [diff] [blame] | 3350 | /* EVIDENCE-OF: R-22564-11647 The header begins with a single varint |
| 3351 | ** which determines the total number of bytes in the header. The varint |
| 3352 | ** value is the size of the header in bytes including the size varint |
| 3353 | ** itself. */ |
drh | 59bf00c | 2013-12-08 23:33:28 +0000 | [diff] [blame] | 3354 | testcase( nHdr==126 ); |
| 3355 | testcase( nHdr==127 ); |
drh | 2a24287 | 2013-12-08 22:59:29 +0000 | [diff] [blame] | 3356 | if( nHdr<=126 ){ |
| 3357 | /* The common case */ |
| 3358 | nHdr += 1; |
| 3359 | }else{ |
| 3360 | /* Rare case of a really large header */ |
| 3361 | nVarint = sqlite3VarintLen(nHdr); |
| 3362 | nHdr += nVarint; |
| 3363 | if( nVarint<sqlite3VarintLen(nHdr) ) nHdr++; |
drh | cb9882a | 2005-03-17 03:15:40 +0000 | [diff] [blame] | 3364 | } |
drh | 038b7bc | 2013-12-09 23:17:22 +0000 | [diff] [blame] | 3365 | nByte = nHdr+nData; |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 3366 | |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 3367 | /* Make sure the output register has a buffer large enough to store |
| 3368 | ** the new record. The output register (pOp->p3) is not allowed to |
| 3369 | ** be one of the input registers (because the following call to |
drh | 322f285 | 2014-09-19 00:43:39 +0000 | [diff] [blame] | 3370 | ** sqlite3VdbeMemClearAndResize() could clobber the value before it is used). |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 3371 | */ |
drh | 0d7f0cc | 2018-09-21 13:07:14 +0000 | [diff] [blame] | 3372 | if( nByte+nZero<=pOut->szMalloc ){ |
| 3373 | /* The output register is already large enough to hold the record. |
| 3374 | ** No error checks or buffer enlargement is required */ |
| 3375 | pOut->z = pOut->zMalloc; |
| 3376 | }else{ |
| 3377 | /* Need to make sure that the output is not too big and then enlarge |
| 3378 | ** the output register to hold the full result */ |
| 3379 | if( nByte+nZero>db->aLimit[SQLITE_LIMIT_LENGTH] ){ |
| 3380 | goto too_big; |
| 3381 | } |
| 3382 | if( sqlite3VdbeMemClearAndResize(pOut, (int)nByte) ){ |
| 3383 | goto no_mem; |
| 3384 | } |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 3385 | } |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 3386 | pOut->n = (int)nByte; |
drh | c91b2fd | 2014-03-01 18:13:23 +0000 | [diff] [blame] | 3387 | pOut->flags = MEM_Blob; |
drh | fdf972a | 2007-05-02 13:30:27 +0000 | [diff] [blame] | 3388 | if( nZero ){ |
drh | 8df3284 | 2008-12-09 02:51:23 +0000 | [diff] [blame] | 3389 | pOut->u.nZero = nZero; |
drh | 477df4b | 2008-01-05 18:48:24 +0000 | [diff] [blame] | 3390 | pOut->flags |= MEM_Zero; |
drh | fdf972a | 2007-05-02 13:30:27 +0000 | [diff] [blame] | 3391 | } |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 3392 | UPDATE_MAX_BLOBSIZE(pOut); |
drh | b70b0df | 2019-04-30 01:08:42 +0000 | [diff] [blame] | 3393 | zHdr = (u8 *)pOut->z; |
| 3394 | zPayload = zHdr + nHdr; |
| 3395 | |
| 3396 | /* Write the record */ |
drh | b47b1f6 | 2022-04-01 21:01:37 +0000 | [diff] [blame] | 3397 | if( nHdr<0x80 ){ |
| 3398 | *(zHdr++) = nHdr; |
| 3399 | }else{ |
| 3400 | zHdr += sqlite3PutVarint(zHdr,nHdr); |
| 3401 | } |
drh | b70b0df | 2019-04-30 01:08:42 +0000 | [diff] [blame] | 3402 | assert( pData0<=pLast ); |
| 3403 | pRec = pData0; |
drh | 759e507 | 2022-04-01 20:39:40 +0000 | [diff] [blame] | 3404 | while( 1 /*exit-by-break*/ ){ |
drh | b70b0df | 2019-04-30 01:08:42 +0000 | [diff] [blame] | 3405 | serial_type = pRec->uTemp; |
| 3406 | /* EVIDENCE-OF: R-06529-47362 Following the size varint are one or more |
drh | d859dc2 | 2022-04-02 14:30:58 +0000 | [diff] [blame] | 3407 | ** additional varints, one per column. |
| 3408 | ** EVIDENCE-OF: R-64536-51728 The values for each column in the record |
| 3409 | ** immediately follow the header. */ |
| 3410 | if( serial_type<=7 ){ |
drh | b47b1f6 | 2022-04-01 21:01:37 +0000 | [diff] [blame] | 3411 | *(zHdr++) = serial_type; |
drh | d859dc2 | 2022-04-02 14:30:58 +0000 | [diff] [blame] | 3412 | if( serial_type==0 ){ |
| 3413 | /* NULL value. No change in zPayload */ |
| 3414 | }else{ |
| 3415 | u64 v; |
drh | 1f416db | 2022-04-02 20:08:48 +0000 | [diff] [blame] | 3416 | u32 i; |
drh | d859dc2 | 2022-04-02 14:30:58 +0000 | [diff] [blame] | 3417 | if( serial_type==7 ){ |
| 3418 | assert( sizeof(v)==sizeof(pRec->u.r) ); |
| 3419 | memcpy(&v, &pRec->u.r, sizeof(v)); |
| 3420 | swapMixedEndianFloat(v); |
| 3421 | }else{ |
| 3422 | v = pRec->u.i; |
| 3423 | } |
| 3424 | len = i = sqlite3SmallTypeSizes[serial_type]; |
| 3425 | assert( i>0 ); |
drh | 2c144b0 | 2022-04-02 15:19:02 +0000 | [diff] [blame] | 3426 | while( 1 /*exit-by-break*/ ){ |
drh | d859dc2 | 2022-04-02 14:30:58 +0000 | [diff] [blame] | 3427 | zPayload[--i] = (u8)(v&0xFF); |
drh | 2c144b0 | 2022-04-02 15:19:02 +0000 | [diff] [blame] | 3428 | if( i==0 ) break; |
drh | d859dc2 | 2022-04-02 14:30:58 +0000 | [diff] [blame] | 3429 | v >>= 8; |
drh | 2c144b0 | 2022-04-02 15:19:02 +0000 | [diff] [blame] | 3430 | } |
drh | d859dc2 | 2022-04-02 14:30:58 +0000 | [diff] [blame] | 3431 | zPayload += len; |
| 3432 | } |
| 3433 | }else if( serial_type<0x80 ){ |
| 3434 | *(zHdr++) = serial_type; |
drh | d13527d | 2022-04-02 19:21:58 +0000 | [diff] [blame] | 3435 | if( serial_type>=14 && pRec->n>0 ){ |
| 3436 | assert( pRec->z!=0 ); |
drh | d859dc2 | 2022-04-02 14:30:58 +0000 | [diff] [blame] | 3437 | memcpy(zPayload, pRec->z, pRec->n); |
| 3438 | zPayload += pRec->n; |
| 3439 | } |
drh | b47b1f6 | 2022-04-01 21:01:37 +0000 | [diff] [blame] | 3440 | }else{ |
| 3441 | zHdr += sqlite3PutVarint(zHdr, serial_type); |
drh | d13527d | 2022-04-02 19:21:58 +0000 | [diff] [blame] | 3442 | if( pRec->n ){ |
| 3443 | assert( pRec->z!=0 ); |
| 3444 | memcpy(zPayload, pRec->z, pRec->n); |
| 3445 | zPayload += pRec->n; |
| 3446 | } |
drh | b47b1f6 | 2022-04-01 21:01:37 +0000 | [diff] [blame] | 3447 | } |
drh | 759e507 | 2022-04-01 20:39:40 +0000 | [diff] [blame] | 3448 | if( pRec==pLast ) break; |
| 3449 | pRec++; |
| 3450 | } |
drh | b70b0df | 2019-04-30 01:08:42 +0000 | [diff] [blame] | 3451 | assert( nHdr==(int)(zHdr - (u8*)pOut->z) ); |
| 3452 | assert( nByte==(int)(zPayload - (u8*)pOut->z) ); |
| 3453 | |
| 3454 | assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) ); |
| 3455 | REGISTER_TRACE(pOp->p3, pOut); |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 3456 | break; |
| 3457 | } |
| 3458 | |
drh | 50fb7e0 | 2021-12-06 20:16:53 +0000 | [diff] [blame] | 3459 | /* Opcode: Count P1 P2 P3 * * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 3460 | ** Synopsis: r[P2]=count() |
danielk1977 | a553316 | 2009-02-24 10:01:51 +0000 | [diff] [blame] | 3461 | ** |
| 3462 | ** Store the number of entries (an integer value) in the table or index |
drh | 9f27463 | 2020-03-17 17:11:23 +0000 | [diff] [blame] | 3463 | ** opened by cursor P1 in register P2. |
| 3464 | ** |
| 3465 | ** If P3==0, then an exact count is obtained, which involves visiting |
| 3466 | ** every btree page of the table. But if P3 is non-zero, an estimate |
| 3467 | ** is returned based on the current cursor position. |
danielk1977 | a553316 | 2009-02-24 10:01:51 +0000 | [diff] [blame] | 3468 | */ |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 3469 | case OP_Count: { /* out2 */ |
danielk1977 | a553316 | 2009-02-24 10:01:51 +0000 | [diff] [blame] | 3470 | i64 nEntry; |
drh | c54a617 | 2009-06-02 16:06:03 +0000 | [diff] [blame] | 3471 | BtCursor *pCrsr; |
| 3472 | |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 3473 | assert( p->apCsr[pOp->p1]->eCurType==CURTYPE_BTREE ); |
| 3474 | pCrsr = p->apCsr[pOp->p1]->uc.pCursor; |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 3475 | assert( pCrsr ); |
drh | 9f27463 | 2020-03-17 17:11:23 +0000 | [diff] [blame] | 3476 | if( pOp->p3 ){ |
| 3477 | nEntry = sqlite3BtreeRowCountEst(pCrsr); |
| 3478 | }else{ |
| 3479 | nEntry = 0; /* Not needed. Only used to silence a warning. */ |
| 3480 | rc = sqlite3BtreeCount(db, pCrsr, &nEntry); |
| 3481 | if( rc ) goto abort_due_to_error; |
| 3482 | } |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 3483 | pOut = out2Prerelease(p, pOp); |
danielk1977 | a553316 | 2009-02-24 10:01:51 +0000 | [diff] [blame] | 3484 | pOut->u.i = nEntry; |
drh | 21f6daa | 2019-10-11 14:21:48 +0000 | [diff] [blame] | 3485 | goto check_for_interrupt; |
danielk1977 | a553316 | 2009-02-24 10:01:51 +0000 | [diff] [blame] | 3486 | } |
danielk1977 | a553316 | 2009-02-24 10:01:51 +0000 | [diff] [blame] | 3487 | |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3488 | /* Opcode: Savepoint P1 * * P4 * |
| 3489 | ** |
| 3490 | ** Open, release or rollback the savepoint named by parameter P4, depending |
drh | 2ce9b6b | 2019-05-10 14:03:07 +0000 | [diff] [blame] | 3491 | ** on the value of P1. To open a new savepoint set P1==0 (SAVEPOINT_BEGIN). |
| 3492 | ** To release (commit) an existing savepoint set P1==1 (SAVEPOINT_RELEASE). |
| 3493 | ** To rollback an existing savepoint set P1==2 (SAVEPOINT_ROLLBACK). |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3494 | */ |
| 3495 | case OP_Savepoint: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3496 | int p1; /* Value of P1 operand */ |
| 3497 | char *zName; /* Name of savepoint */ |
| 3498 | int nName; |
| 3499 | Savepoint *pNew; |
| 3500 | Savepoint *pSavepoint; |
| 3501 | Savepoint *pTmp; |
| 3502 | int iSavepoint; |
| 3503 | int ii; |
| 3504 | |
| 3505 | p1 = pOp->p1; |
| 3506 | zName = pOp->p4.z; |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3507 | |
| 3508 | /* Assert that the p1 parameter is valid. Also that if there is no open |
| 3509 | ** transaction, then there cannot be any savepoints. |
| 3510 | */ |
| 3511 | assert( db->pSavepoint==0 || db->autoCommit==0 ); |
| 3512 | assert( p1==SAVEPOINT_BEGIN||p1==SAVEPOINT_RELEASE||p1==SAVEPOINT_ROLLBACK ); |
| 3513 | assert( db->pSavepoint || db->isTransactionSavepoint==0 ); |
| 3514 | assert( checkSavepointCount(db) ); |
dan | c0537fe | 2013-06-28 19:41:43 +0000 | [diff] [blame] | 3515 | assert( p->bIsReader ); |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3516 | |
| 3517 | if( p1==SAVEPOINT_BEGIN ){ |
drh | 4f7d3a5 | 2013-06-27 23:54:02 +0000 | [diff] [blame] | 3518 | if( db->nVdbeWrite>0 ){ |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3519 | /* A new savepoint cannot be created if there are active write |
| 3520 | ** statements (i.e. open read/write incremental blob handles). |
| 3521 | */ |
drh | 22c17b8 | 2015-05-15 04:13:15 +0000 | [diff] [blame] | 3522 | sqlite3VdbeError(p, "cannot open savepoint - SQL statements in progress"); |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3523 | rc = SQLITE_BUSY; |
| 3524 | }else{ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3525 | nName = sqlite3Strlen30(zName); |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3526 | |
drh | be07ec5 | 2011-06-03 12:15:26 +0000 | [diff] [blame] | 3527 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
dan | d9495cd | 2011-04-27 12:08:04 +0000 | [diff] [blame] | 3528 | /* This call is Ok even if this savepoint is actually a transaction |
| 3529 | ** savepoint (and therefore should not prompt xSavepoint()) callbacks. |
| 3530 | ** If this is a transaction savepoint being opened, it is guaranteed |
| 3531 | ** that the db->aVTrans[] array is empty. */ |
| 3532 | assert( db->autoCommit==0 || db->nVTrans==0 ); |
drh | a24bc9c | 2011-05-24 00:35:56 +0000 | [diff] [blame] | 3533 | rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN, |
| 3534 | db->nStatement+db->nSavepoint); |
dan | d9495cd | 2011-04-27 12:08:04 +0000 | [diff] [blame] | 3535 | if( rc!=SQLITE_OK ) goto abort_due_to_error; |
drh | 305ebab | 2011-05-26 14:19:14 +0000 | [diff] [blame] | 3536 | #endif |
dan | d9495cd | 2011-04-27 12:08:04 +0000 | [diff] [blame] | 3537 | |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3538 | /* Create a new savepoint structure. */ |
drh | 575fad6 | 2016-02-05 13:38:36 +0000 | [diff] [blame] | 3539 | pNew = sqlite3DbMallocRawNN(db, sizeof(Savepoint)+nName+1); |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3540 | if( pNew ){ |
| 3541 | pNew->zName = (char *)&pNew[1]; |
| 3542 | memcpy(pNew->zName, zName, nName+1); |
| 3543 | |
| 3544 | /* If there is no open transaction, then mark this as a special |
| 3545 | ** "transaction savepoint". */ |
| 3546 | if( db->autoCommit ){ |
| 3547 | db->autoCommit = 0; |
| 3548 | db->isTransactionSavepoint = 1; |
| 3549 | }else{ |
| 3550 | db->nSavepoint++; |
danielk1977 | d829335 | 2009-04-30 09:10:37 +0000 | [diff] [blame] | 3551 | } |
dan | 21e8d01 | 2011-03-03 20:05:59 +0000 | [diff] [blame] | 3552 | |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3553 | /* Link the new savepoint into the database handle's list. */ |
| 3554 | pNew->pNext = db->pSavepoint; |
| 3555 | db->pSavepoint = pNew; |
dan | ba9108b | 2009-09-22 07:13:42 +0000 | [diff] [blame] | 3556 | pNew->nDeferredCons = db->nDeferredCons; |
dan | cb3e4b7 | 2013-07-03 19:53:05 +0000 | [diff] [blame] | 3557 | pNew->nDeferredImmCons = db->nDeferredImmCons; |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3558 | } |
| 3559 | } |
| 3560 | }else{ |
drh | 2ce9b6b | 2019-05-10 14:03:07 +0000 | [diff] [blame] | 3561 | assert( p1==SAVEPOINT_RELEASE || p1==SAVEPOINT_ROLLBACK ); |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3562 | iSavepoint = 0; |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3563 | |
| 3564 | /* Find the named savepoint. If there is no such savepoint, then an |
| 3565 | ** an error is returned to the user. */ |
| 3566 | for( |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3567 | pSavepoint = db->pSavepoint; |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3568 | pSavepoint && sqlite3StrICmp(pSavepoint->zName, zName); |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3569 | pSavepoint = pSavepoint->pNext |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3570 | ){ |
| 3571 | iSavepoint++; |
| 3572 | } |
| 3573 | if( !pSavepoint ){ |
drh | 22c17b8 | 2015-05-15 04:13:15 +0000 | [diff] [blame] | 3574 | sqlite3VdbeError(p, "no such savepoint: %s", zName); |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3575 | rc = SQLITE_ERROR; |
drh | 4f7d3a5 | 2013-06-27 23:54:02 +0000 | [diff] [blame] | 3576 | }else if( db->nVdbeWrite>0 && p1==SAVEPOINT_RELEASE ){ |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3577 | /* It is not possible to release (commit) a savepoint if there are |
drh | 0f198a7 | 2012-02-13 16:43:16 +0000 | [diff] [blame] | 3578 | ** active write statements. |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3579 | */ |
drh | 22c17b8 | 2015-05-15 04:13:15 +0000 | [diff] [blame] | 3580 | sqlite3VdbeError(p, "cannot release savepoint - " |
| 3581 | "SQL statements in progress"); |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3582 | rc = SQLITE_BUSY; |
| 3583 | }else{ |
| 3584 | |
| 3585 | /* Determine whether or not this is a transaction savepoint. If so, |
danielk1977 | 34cf35d | 2008-12-18 18:31:38 +0000 | [diff] [blame] | 3586 | ** and this is a RELEASE command, then the current transaction |
| 3587 | ** is committed. |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3588 | */ |
| 3589 | int isTransaction = pSavepoint->pNext==0 && db->isTransactionSavepoint; |
| 3590 | if( isTransaction && p1==SAVEPOINT_RELEASE ){ |
dan | 32b09f2 | 2009-09-23 17:29:59 +0000 | [diff] [blame] | 3591 | if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){ |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 3592 | goto vdbe_return; |
| 3593 | } |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3594 | db->autoCommit = 1; |
| 3595 | if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){ |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 3596 | p->pc = (int)(pOp - aOp); |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3597 | db->autoCommit = 0; |
| 3598 | p->rc = rc = SQLITE_BUSY; |
| 3599 | goto vdbe_return; |
| 3600 | } |
danielk1977 | 34cf35d | 2008-12-18 18:31:38 +0000 | [diff] [blame] | 3601 | rc = p->rc; |
drh | 94649b6 | 2019-12-18 02:12:04 +0000 | [diff] [blame] | 3602 | if( rc ){ |
| 3603 | db->autoCommit = 0; |
| 3604 | }else{ |
| 3605 | db->isTransactionSavepoint = 0; |
| 3606 | } |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3607 | }else{ |
drh | 47b7fc7 | 2014-11-11 01:33:57 +0000 | [diff] [blame] | 3608 | int isSchemaChange; |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3609 | iSavepoint = db->nSavepoint - iSavepoint - 1; |
drh | 31f1005 | 2012-03-31 17:17:26 +0000 | [diff] [blame] | 3610 | if( p1==SAVEPOINT_ROLLBACK ){ |
drh | 8257aa8 | 2017-07-26 19:59:13 +0000 | [diff] [blame] | 3611 | isSchemaChange = (db->mDbFlags & DBFLAG_SchemaChange)!=0; |
drh | 31f1005 | 2012-03-31 17:17:26 +0000 | [diff] [blame] | 3612 | for(ii=0; ii<db->nDb; ii++){ |
drh | 77b1dee | 2014-11-17 17:13:06 +0000 | [diff] [blame] | 3613 | rc = sqlite3BtreeTripAllCursors(db->aDb[ii].pBt, |
| 3614 | SQLITE_ABORT_ROLLBACK, |
drh | 47b7fc7 | 2014-11-11 01:33:57 +0000 | [diff] [blame] | 3615 | isSchemaChange==0); |
dan | 8023104 | 2014-11-12 14:56:02 +0000 | [diff] [blame] | 3616 | if( rc!=SQLITE_OK ) goto abort_due_to_error; |
drh | 31f1005 | 2012-03-31 17:17:26 +0000 | [diff] [blame] | 3617 | } |
drh | 47b7fc7 | 2014-11-11 01:33:57 +0000 | [diff] [blame] | 3618 | }else{ |
drh | 2ce9b6b | 2019-05-10 14:03:07 +0000 | [diff] [blame] | 3619 | assert( p1==SAVEPOINT_RELEASE ); |
drh | 47b7fc7 | 2014-11-11 01:33:57 +0000 | [diff] [blame] | 3620 | isSchemaChange = 0; |
drh | 0f198a7 | 2012-02-13 16:43:16 +0000 | [diff] [blame] | 3621 | } |
| 3622 | for(ii=0; ii<db->nDb; ii++){ |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3623 | rc = sqlite3BtreeSavepoint(db->aDb[ii].pBt, p1, iSavepoint); |
| 3624 | if( rc!=SQLITE_OK ){ |
| 3625 | goto abort_due_to_error; |
danielk1977 | bd43455 | 2009-03-18 10:33:00 +0000 | [diff] [blame] | 3626 | } |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3627 | } |
drh | 47b7fc7 | 2014-11-11 01:33:57 +0000 | [diff] [blame] | 3628 | if( isSchemaChange ){ |
drh | ba968db | 2018-07-24 22:02:12 +0000 | [diff] [blame] | 3629 | sqlite3ExpirePreparedStatements(db, 0); |
drh | 81028a4 | 2012-05-15 18:28:27 +0000 | [diff] [blame] | 3630 | sqlite3ResetAllSchemasOfConnection(db); |
drh | 8257aa8 | 2017-07-26 19:59:13 +0000 | [diff] [blame] | 3631 | db->mDbFlags |= DBFLAG_SchemaChange; |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3632 | } |
| 3633 | } |
drh | 95866af | 2019-12-15 00:36:33 +0000 | [diff] [blame] | 3634 | if( rc ) goto abort_due_to_error; |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3635 | |
| 3636 | /* Regardless of whether this is a RELEASE or ROLLBACK, destroy all |
| 3637 | ** savepoints nested inside of the savepoint being operated on. */ |
| 3638 | while( db->pSavepoint!=pSavepoint ){ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3639 | pTmp = db->pSavepoint; |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3640 | db->pSavepoint = pTmp->pNext; |
| 3641 | sqlite3DbFree(db, pTmp); |
| 3642 | db->nSavepoint--; |
| 3643 | } |
| 3644 | |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 3645 | /* If it is a RELEASE, then destroy the savepoint being operated on |
| 3646 | ** too. If it is a ROLLBACK TO, then set the number of deferred |
| 3647 | ** constraint violations present in the database to the value stored |
| 3648 | ** when the savepoint was created. */ |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3649 | if( p1==SAVEPOINT_RELEASE ){ |
| 3650 | assert( pSavepoint==db->pSavepoint ); |
| 3651 | db->pSavepoint = pSavepoint->pNext; |
| 3652 | sqlite3DbFree(db, pSavepoint); |
| 3653 | if( !isTransaction ){ |
| 3654 | db->nSavepoint--; |
| 3655 | } |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 3656 | }else{ |
drh | 2ce9b6b | 2019-05-10 14:03:07 +0000 | [diff] [blame] | 3657 | assert( p1==SAVEPOINT_ROLLBACK ); |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 3658 | db->nDeferredCons = pSavepoint->nDeferredCons; |
dan | cb3e4b7 | 2013-07-03 19:53:05 +0000 | [diff] [blame] | 3659 | db->nDeferredImmCons = pSavepoint->nDeferredImmCons; |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3660 | } |
dan | d9495cd | 2011-04-27 12:08:04 +0000 | [diff] [blame] | 3661 | |
dan | ea8562e | 2015-04-18 16:25:54 +0000 | [diff] [blame] | 3662 | if( !isTransaction || p1==SAVEPOINT_ROLLBACK ){ |
dan | d9495cd | 2011-04-27 12:08:04 +0000 | [diff] [blame] | 3663 | rc = sqlite3VtabSavepoint(db, p1, iSavepoint); |
| 3664 | if( rc!=SQLITE_OK ) goto abort_due_to_error; |
| 3665 | } |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3666 | } |
| 3667 | } |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 3668 | if( rc ) goto abort_due_to_error; |
drh | 8703edd | 2022-04-03 22:35:13 +0000 | [diff] [blame] | 3669 | if( p->eVdbeState==VDBE_HALT_STATE ){ |
| 3670 | rc = SQLITE_DONE; |
| 3671 | goto vdbe_return; |
| 3672 | } |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3673 | break; |
| 3674 | } |
| 3675 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 3676 | /* Opcode: AutoCommit P1 P2 * * * |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 3677 | ** |
| 3678 | ** 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] | 3679 | ** back any currently active btree transactions. If there are any active |
drh | c25eabe | 2009-02-24 18:57:31 +0000 | [diff] [blame] | 3680 | ** VMs (apart from this one), then a ROLLBACK fails. A COMMIT fails if |
| 3681 | ** there are active writing VMs or active VMs that use shared cache. |
drh | 92f02c3 | 2004-09-02 14:57:08 +0000 | [diff] [blame] | 3682 | ** |
| 3683 | ** This instruction causes the VM to halt. |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 3684 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3685 | case OP_AutoCommit: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3686 | int desiredAutoCommit; |
shane | 68c0273 | 2009-06-09 18:14:18 +0000 | [diff] [blame] | 3687 | int iRollback; |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 3688 | |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3689 | desiredAutoCommit = pOp->p1; |
shane | 68c0273 | 2009-06-09 18:14:18 +0000 | [diff] [blame] | 3690 | iRollback = pOp->p2; |
drh | ad4a4b8 | 2008-11-05 16:37:34 +0000 | [diff] [blame] | 3691 | assert( desiredAutoCommit==1 || desiredAutoCommit==0 ); |
shane | 68c0273 | 2009-06-09 18:14:18 +0000 | [diff] [blame] | 3692 | assert( desiredAutoCommit==1 || iRollback==0 ); |
drh | 4f7d3a5 | 2013-06-27 23:54:02 +0000 | [diff] [blame] | 3693 | assert( db->nVdbeActive>0 ); /* At least this one VM is active */ |
dan | c0537fe | 2013-06-28 19:41:43 +0000 | [diff] [blame] | 3694 | assert( p->bIsReader ); |
danielk1977 | 46c43ed | 2004-06-30 06:30:25 +0000 | [diff] [blame] | 3695 | |
drh | b0c8865 | 2016-02-01 13:21:13 +0000 | [diff] [blame] | 3696 | if( desiredAutoCommit!=db->autoCommit ){ |
shane | 68c0273 | 2009-06-09 18:14:18 +0000 | [diff] [blame] | 3697 | if( iRollback ){ |
drh | ad4a4b8 | 2008-11-05 16:37:34 +0000 | [diff] [blame] | 3698 | assert( desiredAutoCommit==1 ); |
drh | 21021a5 | 2012-02-13 17:01:51 +0000 | [diff] [blame] | 3699 | sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK); |
danielk1977 | f3f06bb | 2005-12-16 15:24:28 +0000 | [diff] [blame] | 3700 | db->autoCommit = 1; |
drh | b0c8865 | 2016-02-01 13:21:13 +0000 | [diff] [blame] | 3701 | }else if( desiredAutoCommit && db->nVdbeWrite>0 ){ |
| 3702 | /* If this instruction implements a COMMIT and other VMs are writing |
| 3703 | ** return an error indicating that the other VMs must complete first. |
| 3704 | */ |
| 3705 | sqlite3VdbeError(p, "cannot commit transaction - " |
| 3706 | "SQL statements in progress"); |
| 3707 | rc = SQLITE_BUSY; |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 3708 | goto abort_due_to_error; |
dan | 32b09f2 | 2009-09-23 17:29:59 +0000 | [diff] [blame] | 3709 | }else if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){ |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 3710 | goto vdbe_return; |
danielk1977 | f3f06bb | 2005-12-16 15:24:28 +0000 | [diff] [blame] | 3711 | }else{ |
shane | 7d3846a | 2008-12-11 02:58:26 +0000 | [diff] [blame] | 3712 | db->autoCommit = (u8)desiredAutoCommit; |
drh | 8ff2587 | 2015-07-31 18:59:56 +0000 | [diff] [blame] | 3713 | } |
| 3714 | if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){ |
| 3715 | p->pc = (int)(pOp - aOp); |
| 3716 | db->autoCommit = (u8)(1-desiredAutoCommit); |
| 3717 | p->rc = rc = SQLITE_BUSY; |
| 3718 | goto vdbe_return; |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 3719 | } |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3720 | sqlite3CloseSavepoints(db); |
drh | 83968c4 | 2007-04-18 16:45:24 +0000 | [diff] [blame] | 3721 | if( p->rc==SQLITE_OK ){ |
drh | 900b31e | 2007-08-28 02:27:51 +0000 | [diff] [blame] | 3722 | rc = SQLITE_DONE; |
drh | 83968c4 | 2007-04-18 16:45:24 +0000 | [diff] [blame] | 3723 | }else{ |
drh | 900b31e | 2007-08-28 02:27:51 +0000 | [diff] [blame] | 3724 | rc = SQLITE_ERROR; |
drh | 83968c4 | 2007-04-18 16:45:24 +0000 | [diff] [blame] | 3725 | } |
drh | 900b31e | 2007-08-28 02:27:51 +0000 | [diff] [blame] | 3726 | goto vdbe_return; |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 3727 | }else{ |
drh | 22c17b8 | 2015-05-15 04:13:15 +0000 | [diff] [blame] | 3728 | sqlite3VdbeError(p, |
drh | ad4a4b8 | 2008-11-05 16:37:34 +0000 | [diff] [blame] | 3729 | (!desiredAutoCommit)?"cannot start a transaction within a transaction":( |
shane | 68c0273 | 2009-06-09 18:14:18 +0000 | [diff] [blame] | 3730 | (iRollback)?"cannot rollback - no transaction is active": |
drh | f089aa4 | 2008-07-08 19:34:06 +0000 | [diff] [blame] | 3731 | "cannot commit - no transaction is active")); |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 3732 | |
| 3733 | rc = SQLITE_ERROR; |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 3734 | goto abort_due_to_error; |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 3735 | } |
drh | 8616cff | 2019-07-13 16:15:23 +0000 | [diff] [blame] | 3736 | /*NOTREACHED*/ assert(0); |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 3737 | } |
| 3738 | |
drh | b22f7c8 | 2014-02-06 23:56:27 +0000 | [diff] [blame] | 3739 | /* Opcode: Transaction P1 P2 P3 P4 P5 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3740 | ** |
drh | 05a86c5 | 2014-02-16 01:55:49 +0000 | [diff] [blame] | 3741 | ** Begin a transaction on database P1 if a transaction is not already |
| 3742 | ** active. |
| 3743 | ** If P2 is non-zero, then a write-transaction is started, or if a |
| 3744 | ** read-transaction is already active, it is upgraded to a write-transaction. |
drh | 1ca037f | 2020-10-12 13:24:00 +0000 | [diff] [blame] | 3745 | ** If P2 is zero, then a read-transaction is started. If P2 is 2 or more |
| 3746 | ** then an exclusive transaction is started. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3747 | ** |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 3748 | ** P1 is the index of the database file on which the transaction is |
| 3749 | ** started. Index 0 is the main database file and index 1 is the |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 3750 | ** file used for temporary tables. Indices of 2 or more are used for |
| 3751 | ** attached databases. |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 3752 | ** |
dan | e0af83a | 2009-09-08 19:15:01 +0000 | [diff] [blame] | 3753 | ** If a write-transaction is started and the Vdbe.usesStmtJournal flag is |
| 3754 | ** true (this flag is set if the Vdbe may modify more than one row and may |
| 3755 | ** throw an ABORT exception), a statement transaction may also be opened. |
| 3756 | ** More specifically, a statement transaction is opened iff the database |
| 3757 | ** connection is currently not in autocommit mode, or if there are other |
drh | a451017 | 2012-02-02 15:50:17 +0000 | [diff] [blame] | 3758 | ** active statements. A statement transaction allows the changes made by this |
dan | e0af83a | 2009-09-08 19:15:01 +0000 | [diff] [blame] | 3759 | ** VDBE to be rolled back after an error without having to roll back the |
| 3760 | ** entire transaction. If no error is encountered, the statement transaction |
| 3761 | ** will automatically commit when the VDBE halts. |
| 3762 | ** |
drh | b22f7c8 | 2014-02-06 23:56:27 +0000 | [diff] [blame] | 3763 | ** If P5!=0 then this opcode also checks the schema cookie against P3 |
| 3764 | ** and the schema generation counter against P4. |
| 3765 | ** The cookie changes its value whenever the database schema changes. |
| 3766 | ** This operation is used to detect when that the cookie has changed |
drh | 05a86c5 | 2014-02-16 01:55:49 +0000 | [diff] [blame] | 3767 | ** and that the current process needs to reread the schema. If the schema |
| 3768 | ** cookie in P3 differs from the schema cookie in the database header or |
| 3769 | ** if the schema generation counter in P4 differs from the current |
| 3770 | ** generation counter, then an SQLITE_SCHEMA error is raised and execution |
| 3771 | ** halts. The sqlite3_step() wrapper function might then reprepare the |
| 3772 | ** statement and rerun it from the beginning. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3773 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3774 | case OP_Transaction: { |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 3775 | Btree *pBt; |
drh | 8ee75f7 | 2022-04-03 10:42:06 +0000 | [diff] [blame] | 3776 | Db *pDb; |
drh | bb2d9b1 | 2018-06-06 16:28:40 +0000 | [diff] [blame] | 3777 | int iMeta = 0; |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 3778 | |
drh | 1713afb | 2013-06-28 01:24:57 +0000 | [diff] [blame] | 3779 | assert( p->bIsReader ); |
drh | 9e92a47 | 2013-06-27 17:40:30 +0000 | [diff] [blame] | 3780 | assert( p->readOnly==0 || pOp->p2==0 ); |
drh | 1ca037f | 2020-10-12 13:24:00 +0000 | [diff] [blame] | 3781 | assert( pOp->p2>=0 && pOp->p2<=2 ); |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 3782 | assert( pOp->p1>=0 && pOp->p1<db->nDb ); |
drh | a7ab6d8 | 2014-07-21 15:44:39 +0000 | [diff] [blame] | 3783 | assert( DbMaskTest(p->btreeMask, pOp->p1) ); |
drh | 8cb63f5 | 2021-10-29 09:59:06 +0000 | [diff] [blame] | 3784 | assert( rc==SQLITE_OK ); |
drh | 46c425b | 2021-11-10 10:59:10 +0000 | [diff] [blame] | 3785 | if( pOp->p2 && (db->flags & (SQLITE_QueryOnly|SQLITE_CorruptRdOnly))!=0 ){ |
| 3786 | if( db->flags & SQLITE_QueryOnly ){ |
| 3787 | /* Writes prohibited by the "PRAGMA query_only=TRUE" statement */ |
| 3788 | rc = SQLITE_READONLY; |
| 3789 | }else{ |
| 3790 | /* Writes prohibited due to a prior SQLITE_CORRUPT in the current |
| 3791 | ** transaction */ |
| 3792 | rc = SQLITE_CORRUPT; |
| 3793 | } |
drh | 13447bf | 2013-07-10 13:33:49 +0000 | [diff] [blame] | 3794 | goto abort_due_to_error; |
| 3795 | } |
drh | 8ee75f7 | 2022-04-03 10:42:06 +0000 | [diff] [blame] | 3796 | pDb = &db->aDb[pOp->p1]; |
| 3797 | pBt = pDb->pBt; |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 3798 | |
danielk1977 | 24162fe | 2004-06-04 06:22:00 +0000 | [diff] [blame] | 3799 | if( pBt ){ |
drh | bb2d9b1 | 2018-06-06 16:28:40 +0000 | [diff] [blame] | 3800 | rc = sqlite3BtreeBeginTrans(pBt, pOp->p2, &iMeta); |
drh | cbd8db3 | 2015-08-20 17:18:32 +0000 | [diff] [blame] | 3801 | testcase( rc==SQLITE_BUSY_SNAPSHOT ); |
| 3802 | testcase( rc==SQLITE_BUSY_RECOVERY ); |
drh | 9e9f1bd | 2009-10-13 15:36:51 +0000 | [diff] [blame] | 3803 | if( rc!=SQLITE_OK ){ |
drh | fadd2b1 | 2016-09-19 23:39:34 +0000 | [diff] [blame] | 3804 | if( (rc&0xff)==SQLITE_BUSY ){ |
| 3805 | p->pc = (int)(pOp - aOp); |
| 3806 | p->rc = rc; |
| 3807 | goto vdbe_return; |
| 3808 | } |
danielk1977 | 24162fe | 2004-06-04 06:22:00 +0000 | [diff] [blame] | 3809 | goto abort_due_to_error; |
drh | 90bfcda | 2001-09-23 19:46:51 +0000 | [diff] [blame] | 3810 | } |
dan | e0af83a | 2009-09-08 19:15:01 +0000 | [diff] [blame] | 3811 | |
drh | 4d29448 | 2019-10-05 15:28:24 +0000 | [diff] [blame] | 3812 | if( p->usesStmtJournal |
| 3813 | && pOp->p2 |
dan | c0537fe | 2013-06-28 19:41:43 +0000 | [diff] [blame] | 3814 | && (db->autoCommit==0 || db->nVdbeRead>1) |
dan | e0af83a | 2009-09-08 19:15:01 +0000 | [diff] [blame] | 3815 | ){ |
drh | 99744fa | 2020-08-25 19:09:07 +0000 | [diff] [blame] | 3816 | assert( sqlite3BtreeTxnState(pBt)==SQLITE_TXN_WRITE ); |
dan | e0af83a | 2009-09-08 19:15:01 +0000 | [diff] [blame] | 3817 | if( p->iStatement==0 ){ |
| 3818 | assert( db->nStatement>=0 && db->nSavepoint>=0 ); |
| 3819 | db->nStatement++; |
| 3820 | p->iStatement = db->nSavepoint + db->nStatement; |
| 3821 | } |
dan | a311b80 | 2011-04-26 19:21:34 +0000 | [diff] [blame] | 3822 | |
drh | 346506f | 2011-05-25 01:16:42 +0000 | [diff] [blame] | 3823 | rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN, p->iStatement-1); |
dan | a311b80 | 2011-04-26 19:21:34 +0000 | [diff] [blame] | 3824 | if( rc==SQLITE_OK ){ |
| 3825 | rc = sqlite3BtreeBeginStmt(pBt, p->iStatement); |
| 3826 | } |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 3827 | |
| 3828 | /* Store the current value of the database handles deferred constraint |
| 3829 | ** counter. If the statement transaction needs to be rolled back, |
| 3830 | ** the value of this counter needs to be restored too. */ |
| 3831 | p->nStmtDefCons = db->nDeferredCons; |
dan | cb3e4b7 | 2013-07-03 19:53:05 +0000 | [diff] [blame] | 3832 | p->nStmtDefImmCons = db->nDeferredImmCons; |
dan | e0af83a | 2009-09-08 19:15:01 +0000 | [diff] [blame] | 3833 | } |
drh | 397776a | 2018-06-06 17:45:51 +0000 | [diff] [blame] | 3834 | } |
| 3835 | assert( pOp->p5==0 || pOp->p4type==P4_INT32 ); |
drh | e4e1af5 | 2021-10-29 16:19:03 +0000 | [diff] [blame] | 3836 | if( rc==SQLITE_OK |
| 3837 | && pOp->p5 |
drh | 8ee75f7 | 2022-04-03 10:42:06 +0000 | [diff] [blame] | 3838 | && (iMeta!=pOp->p3 || pDb->pSchema->iGeneration!=pOp->p4.i) |
drh | 397776a | 2018-06-06 17:45:51 +0000 | [diff] [blame] | 3839 | ){ |
dan | d2ffc97 | 2020-12-10 19:20:15 +0000 | [diff] [blame] | 3840 | /* |
| 3841 | ** IMPLEMENTATION-OF: R-03189-51135 As each SQL statement runs, the schema |
| 3842 | ** version is checked to ensure that the schema has not changed since the |
| 3843 | ** SQL statement was prepared. |
| 3844 | */ |
| 3845 | sqlite3DbFree(db, p->zErrMsg); |
| 3846 | p->zErrMsg = sqlite3DbStrDup(db, "database schema has changed"); |
drh | b22f7c8 | 2014-02-06 23:56:27 +0000 | [diff] [blame] | 3847 | /* If the schema-cookie from the database file matches the cookie |
| 3848 | ** stored with the in-memory representation of the schema, do |
| 3849 | ** not reload the schema from the database file. |
| 3850 | ** |
| 3851 | ** If virtual-tables are in use, this is not just an optimization. |
| 3852 | ** Often, v-tables store their data in other SQLite tables, which |
| 3853 | ** are queried from within xNext() and other v-table methods using |
| 3854 | ** prepared queries. If such a query is out-of-date, we do not want to |
| 3855 | ** discard the database schema, as the user code implementing the |
| 3856 | ** v-table would have to be ready for the sqlite3_vtab structure itself |
| 3857 | ** to be invalidated whenever sqlite3_step() is called from within |
| 3858 | ** a v-table method. |
| 3859 | */ |
| 3860 | if( db->aDb[pOp->p1].pSchema->schema_cookie!=iMeta ){ |
| 3861 | sqlite3ResetOneSchema(db, pOp->p1); |
| 3862 | } |
| 3863 | p->expired = 1; |
| 3864 | rc = SQLITE_SCHEMA; |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 3865 | } |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 3866 | if( rc ) goto abort_due_to_error; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3867 | break; |
| 3868 | } |
| 3869 | |
drh | b1fdb2a | 2008-01-05 04:06:03 +0000 | [diff] [blame] | 3870 | /* Opcode: ReadCookie P1 P2 P3 * * |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 3871 | ** |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3872 | ** Read cookie number P3 from database P1 and write it into register P2. |
danielk1977 | 0d19f7a | 2009-06-03 11:25:07 +0000 | [diff] [blame] | 3873 | ** P3==1 is the schema version. P3==2 is the database format. |
| 3874 | ** 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] | 3875 | ** the main database file and P1==1 is the database file used to store |
| 3876 | ** temporary tables. |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 3877 | ** |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 3878 | ** There must be a read-lock on the database (either a transaction |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 3879 | ** must be started or there must be an open cursor) before |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 3880 | ** executing this instruction. |
| 3881 | */ |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 3882 | case OP_ReadCookie: { /* out2 */ |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 3883 | int iMeta; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3884 | int iDb; |
| 3885 | int iCookie; |
danielk1977 | 180b56a | 2007-06-24 08:00:42 +0000 | [diff] [blame] | 3886 | |
drh | 1713afb | 2013-06-28 01:24:57 +0000 | [diff] [blame] | 3887 | assert( p->bIsReader ); |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3888 | iDb = pOp->p1; |
| 3889 | iCookie = pOp->p3; |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 3890 | assert( pOp->p3<SQLITE_N_BTREE_META ); |
danielk1977 | 180b56a | 2007-06-24 08:00:42 +0000 | [diff] [blame] | 3891 | assert( iDb>=0 && iDb<db->nDb ); |
| 3892 | assert( db->aDb[iDb].pBt!=0 ); |
drh | a7ab6d8 | 2014-07-21 15:44:39 +0000 | [diff] [blame] | 3893 | assert( DbMaskTest(p->btreeMask, iDb) ); |
danielk1977 | 0d19f7a | 2009-06-03 11:25:07 +0000 | [diff] [blame] | 3894 | |
danielk1977 | 602b466 | 2009-07-02 07:47:33 +0000 | [diff] [blame] | 3895 | sqlite3BtreeGetMeta(db->aDb[iDb].pBt, iCookie, (u32 *)&iMeta); |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 3896 | pOut = out2Prerelease(p, pOp); |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 3897 | pOut->u.i = iMeta; |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 3898 | break; |
| 3899 | } |
| 3900 | |
drh | e3863b5 | 2020-07-01 16:19:14 +0000 | [diff] [blame] | 3901 | /* Opcode: SetCookie P1 P2 P3 * P5 |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 3902 | ** |
drh | 1861afc | 2016-02-01 21:48:34 +0000 | [diff] [blame] | 3903 | ** Write the integer value P3 into cookie number P2 of database P1. |
| 3904 | ** P2==1 is the schema version. P2==2 is the database format. |
| 3905 | ** P2==3 is the recommended pager cache |
danielk1977 | 0d19f7a | 2009-06-03 11:25:07 +0000 | [diff] [blame] | 3906 | ** size, and so forth. P1==0 is the main database file and P1==1 is the |
| 3907 | ** database file used to store temporary tables. |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 3908 | ** |
| 3909 | ** A transaction must be started before executing this opcode. |
drh | e3863b5 | 2020-07-01 16:19:14 +0000 | [diff] [blame] | 3910 | ** |
| 3911 | ** If P2 is the SCHEMA_VERSION cookie (cookie number 1) then the internal |
| 3912 | ** schema version is set to P3-P5. The "PRAGMA schema_version=N" statement |
| 3913 | ** has P5 set to 1, so that the internal schema version will be different |
| 3914 | ** from the database schema version, resulting in a schema reset. |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 3915 | */ |
drh | 1861afc | 2016-02-01 21:48:34 +0000 | [diff] [blame] | 3916 | case OP_SetCookie: { |
drh | 3f7d4e4 | 2004-07-24 14:35:58 +0000 | [diff] [blame] | 3917 | Db *pDb; |
drh | 4031baf | 2018-05-28 17:31:20 +0000 | [diff] [blame] | 3918 | |
| 3919 | sqlite3VdbeIncrWriteCounter(p, 0); |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 3920 | assert( pOp->p2<SQLITE_N_BTREE_META ); |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 3921 | assert( pOp->p1>=0 && pOp->p1<db->nDb ); |
drh | a7ab6d8 | 2014-07-21 15:44:39 +0000 | [diff] [blame] | 3922 | assert( DbMaskTest(p->btreeMask, pOp->p1) ); |
drh | 9e92a47 | 2013-06-27 17:40:30 +0000 | [diff] [blame] | 3923 | assert( p->readOnly==0 ); |
drh | 3f7d4e4 | 2004-07-24 14:35:58 +0000 | [diff] [blame] | 3924 | pDb = &db->aDb[pOp->p1]; |
| 3925 | assert( pDb->pBt!=0 ); |
drh | 2120608 | 2011-04-04 18:22:02 +0000 | [diff] [blame] | 3926 | assert( sqlite3SchemaMutexHeld(db, pOp->p1, 0) ); |
drh | a3b321d | 2004-05-11 09:31:31 +0000 | [diff] [blame] | 3927 | /* See note about index shifting on OP_ReadCookie */ |
drh | 1861afc | 2016-02-01 21:48:34 +0000 | [diff] [blame] | 3928 | rc = sqlite3BtreeUpdateMeta(pDb->pBt, pOp->p2, pOp->p3); |
danielk1977 | 0d19f7a | 2009-06-03 11:25:07 +0000 | [diff] [blame] | 3929 | if( pOp->p2==BTREE_SCHEMA_VERSION ){ |
drh | 3f7d4e4 | 2004-07-24 14:35:58 +0000 | [diff] [blame] | 3930 | /* When the schema cookie changes, record the new cookie internally */ |
drh | e3863b5 | 2020-07-01 16:19:14 +0000 | [diff] [blame] | 3931 | pDb->pSchema->schema_cookie = pOp->p3 - pOp->p5; |
drh | 8257aa8 | 2017-07-26 19:59:13 +0000 | [diff] [blame] | 3932 | db->mDbFlags |= DBFLAG_SchemaChange; |
drh | 44a5c02 | 2022-01-02 12:01:03 +0000 | [diff] [blame] | 3933 | sqlite3FkClearTriggerCache(db, pOp->p1); |
danielk1977 | 0d19f7a | 2009-06-03 11:25:07 +0000 | [diff] [blame] | 3934 | }else if( pOp->p2==BTREE_FILE_FORMAT ){ |
drh | d28bcb3 | 2005-12-21 14:43:11 +0000 | [diff] [blame] | 3935 | /* Record changes in the file format */ |
drh | 1861afc | 2016-02-01 21:48:34 +0000 | [diff] [blame] | 3936 | pDb->pSchema->file_format = pOp->p3; |
drh | 3f7d4e4 | 2004-07-24 14:35:58 +0000 | [diff] [blame] | 3937 | } |
drh | fd426c6 | 2006-01-30 15:34:22 +0000 | [diff] [blame] | 3938 | if( pOp->p1==1 ){ |
| 3939 | /* Invalidate all prepared statements whenever the TEMP database |
| 3940 | ** schema is changed. Ticket #1644 */ |
drh | ba968db | 2018-07-24 22:02:12 +0000 | [diff] [blame] | 3941 | sqlite3ExpirePreparedStatements(db, 0); |
dan | fa401de | 2009-10-16 14:55:03 +0000 | [diff] [blame] | 3942 | p->expired = 0; |
drh | fd426c6 | 2006-01-30 15:34:22 +0000 | [diff] [blame] | 3943 | } |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 3944 | if( rc ) goto abort_due_to_error; |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 3945 | break; |
| 3946 | } |
| 3947 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 3948 | /* Opcode: OpenRead P1 P2 P3 P4 P5 |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 3949 | ** Synopsis: root=P2 iDb=P3 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3950 | ** |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 3951 | ** Open a read-only cursor for the database table whose root page is |
danielk1977 | 207872a | 2008-01-03 07:54:23 +0000 | [diff] [blame] | 3952 | ** P2 in a database file. The database file is determined by P3. |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 3953 | ** P3==0 means the main database, P3==1 means the database used for |
| 3954 | ** temporary tables, and P3>1 means used the corresponding attached |
| 3955 | ** database. Give the new cursor an identifier of P1. The P1 |
danielk1977 | 207872a | 2008-01-03 07:54:23 +0000 | [diff] [blame] | 3956 | ** values need not be contiguous but all P1 values should be small integers. |
| 3957 | ** It is an error for P1 to be negative. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3958 | ** |
drh | 8e9deb6 | 2018-06-05 13:43:02 +0000 | [diff] [blame] | 3959 | ** Allowed P5 bits: |
| 3960 | ** <ul> |
| 3961 | ** <li> <b>0x02 OPFLAG_SEEKEQ</b>: This cursor will only be used for |
| 3962 | ** equality lookups (implemented as a pair of opcodes OP_SeekGE/OP_IdxGT |
drh | 576d0a9 | 2020-03-12 17:28:27 +0000 | [diff] [blame] | 3963 | ** of OP_SeekLE/OP_IdxLT) |
drh | 8e9deb6 | 2018-06-05 13:43:02 +0000 | [diff] [blame] | 3964 | ** </ul> |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 3965 | ** |
danielk1977 | d336e22 | 2009-02-20 10:58:41 +0000 | [diff] [blame] | 3966 | ** The P4 value may be either an integer (P4_INT32) or a pointer to |
| 3967 | ** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo |
drh | 8e9deb6 | 2018-06-05 13:43:02 +0000 | [diff] [blame] | 3968 | ** object, then table being opened must be an [index b-tree] where the |
| 3969 | ** KeyInfo object defines the content and collating |
| 3970 | ** sequence of that index b-tree. Otherwise, if P4 is an integer |
| 3971 | ** value, then the table being opened must be a [table b-tree] with a |
| 3972 | ** number of columns no less than the value of P4. |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 3973 | ** |
drh | 3526319 | 2014-07-22 20:02:19 +0000 | [diff] [blame] | 3974 | ** See also: OpenWrite, ReopenIdx |
| 3975 | */ |
| 3976 | /* Opcode: ReopenIdx P1 P2 P3 P4 P5 |
| 3977 | ** Synopsis: root=P2 iDb=P3 |
| 3978 | ** |
drh | 8e9deb6 | 2018-06-05 13:43:02 +0000 | [diff] [blame] | 3979 | ** The ReopenIdx opcode works like OP_OpenRead except that it first |
| 3980 | ** checks to see if the cursor on P1 is already open on the same |
| 3981 | ** 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] | 3982 | ** if the cursor is already open, do not reopen it. |
| 3983 | ** |
drh | 8e9deb6 | 2018-06-05 13:43:02 +0000 | [diff] [blame] | 3984 | ** The ReopenIdx opcode may only be used with P5==0 or P5==OPFLAG_SEEKEQ |
| 3985 | ** and with P4 being a P4_KEYINFO object. Furthermore, the P3 value must |
| 3986 | ** be the same as every other ReopenIdx or OpenRead for the same cursor |
| 3987 | ** number. |
drh | 3526319 | 2014-07-22 20:02:19 +0000 | [diff] [blame] | 3988 | ** |
drh | 8e9deb6 | 2018-06-05 13:43:02 +0000 | [diff] [blame] | 3989 | ** Allowed P5 bits: |
| 3990 | ** <ul> |
| 3991 | ** <li> <b>0x02 OPFLAG_SEEKEQ</b>: This cursor will only be used for |
| 3992 | ** equality lookups (implemented as a pair of opcodes OP_SeekGE/OP_IdxGT |
drh | 576d0a9 | 2020-03-12 17:28:27 +0000 | [diff] [blame] | 3993 | ** of OP_SeekLE/OP_IdxLT) |
drh | 8e9deb6 | 2018-06-05 13:43:02 +0000 | [diff] [blame] | 3994 | ** </ul> |
| 3995 | ** |
| 3996 | ** See also: OP_OpenRead, OP_OpenWrite |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3997 | */ |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 3998 | /* Opcode: OpenWrite P1 P2 P3 P4 P5 |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 3999 | ** Synopsis: root=P2 iDb=P3 |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 4000 | ** |
| 4001 | ** 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] | 4002 | ** page is P2 (or whose root page is held in register P2 if the |
| 4003 | ** OPFLAG_P2ISREG bit is set in P5 - see below). |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 4004 | ** |
danielk1977 | d336e22 | 2009-02-20 10:58:41 +0000 | [diff] [blame] | 4005 | ** The P4 value may be either an integer (P4_INT32) or a pointer to |
| 4006 | ** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo |
drh | 8e9deb6 | 2018-06-05 13:43:02 +0000 | [diff] [blame] | 4007 | ** object, then table being opened must be an [index b-tree] where the |
| 4008 | ** KeyInfo object defines the content and collating |
| 4009 | ** sequence of that index b-tree. Otherwise, if P4 is an integer |
| 4010 | ** value, then the table being opened must be a [table b-tree] with a |
| 4011 | ** number of columns no less than the value of P4. |
jplyon | 5a56422 | 2003-06-02 06:15:58 +0000 | [diff] [blame] | 4012 | ** |
drh | 8e9deb6 | 2018-06-05 13:43:02 +0000 | [diff] [blame] | 4013 | ** Allowed P5 bits: |
| 4014 | ** <ul> |
| 4015 | ** <li> <b>0x02 OPFLAG_SEEKEQ</b>: This cursor will only be used for |
| 4016 | ** equality lookups (implemented as a pair of opcodes OP_SeekGE/OP_IdxGT |
drh | 576d0a9 | 2020-03-12 17:28:27 +0000 | [diff] [blame] | 4017 | ** of OP_SeekLE/OP_IdxLT) |
drh | 8e9deb6 | 2018-06-05 13:43:02 +0000 | [diff] [blame] | 4018 | ** <li> <b>0x08 OPFLAG_FORDELETE</b>: This cursor is used only to seek |
| 4019 | ** and subsequently delete entries in an index btree. This is a |
| 4020 | ** hint to the storage engine that the storage engine is allowed to |
| 4021 | ** ignore. The hint is not used by the official SQLite b*tree storage |
| 4022 | ** engine, but is used by COMDB2. |
| 4023 | ** <li> <b>0x10 OPFLAG_P2ISREG</b>: Use the content of register P2 |
| 4024 | ** as the root page, not the value of P2 itself. |
| 4025 | ** </ul> |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 4026 | ** |
drh | 8e9deb6 | 2018-06-05 13:43:02 +0000 | [diff] [blame] | 4027 | ** This instruction works like OpenRead except that it opens the cursor |
| 4028 | ** in read/write mode. |
| 4029 | ** |
| 4030 | ** See also: OP_OpenRead, OP_ReopenIdx |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 4031 | */ |
drh | 3526319 | 2014-07-22 20:02:19 +0000 | [diff] [blame] | 4032 | case OP_ReopenIdx: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 4033 | int nField; |
| 4034 | KeyInfo *pKeyInfo; |
drh | abc3815 | 2020-07-22 13:38:04 +0000 | [diff] [blame] | 4035 | u32 p2; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 4036 | int iDb; |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 4037 | int wrFlag; |
| 4038 | Btree *pX; |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 4039 | VdbeCursor *pCur; |
drh | d946db0 | 2005-12-29 19:23:06 +0000 | [diff] [blame] | 4040 | Db *pDb; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 4041 | |
drh | e0997b3 | 2015-03-20 14:57:50 +0000 | [diff] [blame] | 4042 | assert( pOp->p5==0 || pOp->p5==OPFLAG_SEEKEQ ); |
drh | 3526319 | 2014-07-22 20:02:19 +0000 | [diff] [blame] | 4043 | assert( pOp->p4type==P4_KEYINFO ); |
| 4044 | pCur = p->apCsr[pOp->p1]; |
drh | e8f2c9d | 2014-08-06 17:49:13 +0000 | [diff] [blame] | 4045 | if( pCur && pCur->pgnoRoot==(u32)pOp->p2 ){ |
drh | 3526319 | 2014-07-22 20:02:19 +0000 | [diff] [blame] | 4046 | assert( pCur->iDb==pOp->p3 ); /* Guaranteed by the code generator */ |
drh | 4422b3a | 2021-06-25 14:48:24 +0000 | [diff] [blame] | 4047 | assert( pCur->eCurType==CURTYPE_BTREE ); |
| 4048 | sqlite3BtreeClearCursor(pCur->uc.pCursor); |
drh | e0997b3 | 2015-03-20 14:57:50 +0000 | [diff] [blame] | 4049 | goto open_cursor_set_hints; |
drh | 3526319 | 2014-07-22 20:02:19 +0000 | [diff] [blame] | 4050 | } |
| 4051 | /* If the cursor is not currently open or is open on a different |
| 4052 | ** index, then fall through into OP_OpenRead to force a reopen */ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4053 | case OP_OpenRead: |
drh | 1fa509a | 2015-03-20 16:34:49 +0000 | [diff] [blame] | 4054 | case OP_OpenWrite: |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 4055 | |
drh | e0997b3 | 2015-03-20 14:57:50 +0000 | [diff] [blame] | 4056 | assert( pOp->opcode==OP_OpenWrite || pOp->p5==0 || pOp->p5==OPFLAG_SEEKEQ ); |
drh | 1713afb | 2013-06-28 01:24:57 +0000 | [diff] [blame] | 4057 | assert( p->bIsReader ); |
drh | 3526319 | 2014-07-22 20:02:19 +0000 | [diff] [blame] | 4058 | assert( pOp->opcode==OP_OpenRead || pOp->opcode==OP_ReopenIdx |
| 4059 | || p->readOnly==0 ); |
dan | 428c218 | 2012-08-06 18:50:11 +0000 | [diff] [blame] | 4060 | |
drh | ba968db | 2018-07-24 22:02:12 +0000 | [diff] [blame] | 4061 | if( p->expired==1 ){ |
drh | 47b7fc7 | 2014-11-11 01:33:57 +0000 | [diff] [blame] | 4062 | rc = SQLITE_ABORT_ROLLBACK; |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 4063 | goto abort_due_to_error; |
dan | fa401de | 2009-10-16 14:55:03 +0000 | [diff] [blame] | 4064 | } |
| 4065 | |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 4066 | nField = 0; |
| 4067 | pKeyInfo = 0; |
drh | abc3815 | 2020-07-22 13:38:04 +0000 | [diff] [blame] | 4068 | p2 = (u32)pOp->p2; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 4069 | iDb = pOp->p3; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 4070 | assert( iDb>=0 && iDb<db->nDb ); |
drh | a7ab6d8 | 2014-07-21 15:44:39 +0000 | [diff] [blame] | 4071 | assert( DbMaskTest(p->btreeMask, iDb) ); |
drh | d946db0 | 2005-12-29 19:23:06 +0000 | [diff] [blame] | 4072 | pDb = &db->aDb[iDb]; |
| 4073 | pX = pDb->pBt; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 4074 | assert( pX!=0 ); |
drh | d946db0 | 2005-12-29 19:23:06 +0000 | [diff] [blame] | 4075 | if( pOp->opcode==OP_OpenWrite ){ |
dan | fd261ec | 2015-10-22 20:54:33 +0000 | [diff] [blame] | 4076 | assert( OPFLAG_FORDELETE==BTREE_FORDELETE ); |
| 4077 | wrFlag = BTREE_WRCSR | (pOp->p5 & OPFLAG_FORDELETE); |
drh | 2120608 | 2011-04-04 18:22:02 +0000 | [diff] [blame] | 4078 | assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 4079 | if( pDb->pSchema->file_format < p->minWriteFileFormat ){ |
| 4080 | p->minWriteFileFormat = pDb->pSchema->file_format; |
drh | d946db0 | 2005-12-29 19:23:06 +0000 | [diff] [blame] | 4081 | } |
| 4082 | }else{ |
| 4083 | wrFlag = 0; |
| 4084 | } |
dan | 428c218 | 2012-08-06 18:50:11 +0000 | [diff] [blame] | 4085 | if( pOp->p5 & OPFLAG_P2ISREG ){ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4086 | assert( p2>0 ); |
mistachkin | cec5f1d | 2020-08-04 16:11:37 +0000 | [diff] [blame] | 4087 | assert( p2<=(u32)(p->nMem+1 - p->nCursor) ); |
drh | 8e9deb6 | 2018-06-05 13:43:02 +0000 | [diff] [blame] | 4088 | assert( pOp->opcode==OP_OpenWrite ); |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 4089 | pIn2 = &aMem[p2]; |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 4090 | assert( memIsValid(pIn2) ); |
| 4091 | assert( (pIn2->flags & MEM_Int)!=0 ); |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4092 | sqlite3VdbeMemIntegerify(pIn2); |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 4093 | p2 = (int)pIn2->u.i; |
drh | 0f3f766 | 2017-08-18 14:34:28 +0000 | [diff] [blame] | 4094 | /* The p2 value always comes from a prior OP_CreateBtree opcode and |
drh | 9a65f2c | 2009-06-22 19:05:40 +0000 | [diff] [blame] | 4095 | ** that opcode will always set the p2 value to 2 or more or else fail. |
| 4096 | ** If there were a failure, the prepared statement would have halted |
| 4097 | ** before reaching this instruction. */ |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 4098 | assert( p2>=2 ); |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 4099 | } |
danielk1977 | d336e22 | 2009-02-20 10:58:41 +0000 | [diff] [blame] | 4100 | if( pOp->p4type==P4_KEYINFO ){ |
| 4101 | pKeyInfo = pOp->p4.pKeyInfo; |
drh | 41e13e1 | 2013-11-07 14:09:39 +0000 | [diff] [blame] | 4102 | assert( pKeyInfo->enc==ENC(db) ); |
| 4103 | assert( pKeyInfo->db==db ); |
drh | a485ad1 | 2017-08-02 22:43:14 +0000 | [diff] [blame] | 4104 | nField = pKeyInfo->nAllField; |
danielk1977 | d336e22 | 2009-02-20 10:58:41 +0000 | [diff] [blame] | 4105 | }else if( pOp->p4type==P4_INT32 ){ |
| 4106 | nField = pOp->p4.i; |
| 4107 | } |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 4108 | assert( pOp->p1>=0 ); |
drh | 399af1d | 2013-11-20 17:25:55 +0000 | [diff] [blame] | 4109 | assert( nField>=0 ); |
| 4110 | testcase( nField==0 ); /* Table with INTEGER PRIMARY KEY and nothing else */ |
drh | b248668 | 2022-01-03 01:43:28 +0000 | [diff] [blame] | 4111 | pCur = allocateCursor(p, pOp->p1, nField, CURTYPE_BTREE); |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 4112 | if( pCur==0 ) goto no_mem; |
drh | b248668 | 2022-01-03 01:43:28 +0000 | [diff] [blame] | 4113 | pCur->iDb = iDb; |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 4114 | pCur->nullRow = 1; |
drh | d4187c7 | 2010-08-30 22:15:45 +0000 | [diff] [blame] | 4115 | pCur->isOrdered = 1; |
drh | 3526319 | 2014-07-22 20:02:19 +0000 | [diff] [blame] | 4116 | pCur->pgnoRoot = p2; |
drh | b89aeb6 | 2016-01-27 15:49:32 +0000 | [diff] [blame] | 4117 | #ifdef SQLITE_DEBUG |
| 4118 | pCur->wrFlag = wrFlag; |
| 4119 | #endif |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 4120 | rc = sqlite3BtreeCursor(pX, p2, wrFlag, pKeyInfo, pCur->uc.pCursor); |
danielk1977 | d336e22 | 2009-02-20 10:58:41 +0000 | [diff] [blame] | 4121 | pCur->pKeyInfo = pKeyInfo; |
drh | 14da87f | 2013-11-20 21:51:33 +0000 | [diff] [blame] | 4122 | /* Set the VdbeCursor.isTable variable. Previous versions of |
danielk1977 | 172114a | 2009-07-07 15:47:12 +0000 | [diff] [blame] | 4123 | ** SQLite used to check if the root-page flags were sane at this point |
| 4124 | ** and report database corruption if they were not, but this check has |
| 4125 | ** since moved into the btree layer. */ |
| 4126 | pCur->isTable = pOp->p4type!=P4_KEYINFO; |
drh | e0997b3 | 2015-03-20 14:57:50 +0000 | [diff] [blame] | 4127 | |
| 4128 | open_cursor_set_hints: |
| 4129 | assert( OPFLAG_BULKCSR==BTREE_BULKLOAD ); |
| 4130 | assert( OPFLAG_SEEKEQ==BTREE_SEEK_EQ ); |
drh | 0403cb3 | 2015-08-14 23:57:04 +0000 | [diff] [blame] | 4131 | testcase( pOp->p5 & OPFLAG_BULKCSR ); |
drh | 0403cb3 | 2015-08-14 23:57:04 +0000 | [diff] [blame] | 4132 | testcase( pOp->p2 & OPFLAG_SEEKEQ ); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 4133 | sqlite3BtreeCursorHintFlags(pCur->uc.pCursor, |
drh | f7854c7 | 2015-10-27 13:24:37 +0000 | [diff] [blame] | 4134 | (pOp->p5 & (OPFLAG_BULKCSR|OPFLAG_SEEKEQ))); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 4135 | if( rc ) goto abort_due_to_error; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4136 | break; |
| 4137 | } |
| 4138 | |
drh | e08e8d6 | 2017-05-01 15:15:41 +0000 | [diff] [blame] | 4139 | /* Opcode: OpenDup P1 P2 * * * |
| 4140 | ** |
| 4141 | ** Open a new cursor P1 that points to the same ephemeral table as |
| 4142 | ** cursor P2. The P2 cursor must have been opened by a prior OP_OpenEphemeral |
| 4143 | ** opcode. Only ephemeral cursors may be duplicated. |
| 4144 | ** |
| 4145 | ** Duplicate ephemeral cursors are used for self-joins of materialized views. |
| 4146 | */ |
| 4147 | case OP_OpenDup: { |
| 4148 | VdbeCursor *pOrig; /* The original cursor to be duplicated */ |
| 4149 | VdbeCursor *pCx; /* The new cursor */ |
| 4150 | |
| 4151 | pOrig = p->apCsr[pOp->p2]; |
dan | 2811ea6 | 2019-12-23 14:20:46 +0000 | [diff] [blame] | 4152 | assert( pOrig ); |
drh | 5a4a15f | 2021-03-18 15:42:59 +0000 | [diff] [blame] | 4153 | assert( pOrig->isEphemeral ); /* Only ephemeral cursors can be duplicated */ |
drh | e08e8d6 | 2017-05-01 15:15:41 +0000 | [diff] [blame] | 4154 | |
drh | b248668 | 2022-01-03 01:43:28 +0000 | [diff] [blame] | 4155 | pCx = allocateCursor(p, pOp->p1, pOrig->nField, CURTYPE_BTREE); |
drh | e08e8d6 | 2017-05-01 15:15:41 +0000 | [diff] [blame] | 4156 | if( pCx==0 ) goto no_mem; |
| 4157 | pCx->nullRow = 1; |
| 4158 | pCx->isEphemeral = 1; |
| 4159 | pCx->pKeyInfo = pOrig->pKeyInfo; |
| 4160 | pCx->isTable = pOrig->isTable; |
drh | 2c04131 | 2018-12-24 02:34:49 +0000 | [diff] [blame] | 4161 | pCx->pgnoRoot = pOrig->pgnoRoot; |
dan | a0f6b83 | 2019-03-14 16:36:20 +0000 | [diff] [blame] | 4162 | pCx->isOrdered = pOrig->isOrdered; |
drh | b248668 | 2022-01-03 01:43:28 +0000 | [diff] [blame] | 4163 | pCx->ub.pBtx = pOrig->ub.pBtx; |
drh | 5a4a15f | 2021-03-18 15:42:59 +0000 | [diff] [blame] | 4164 | pCx->hasBeenDuped = 1; |
| 4165 | pOrig->hasBeenDuped = 1; |
drh | b248668 | 2022-01-03 01:43:28 +0000 | [diff] [blame] | 4166 | rc = sqlite3BtreeCursor(pCx->ub.pBtx, pCx->pgnoRoot, BTREE_WRCSR, |
drh | e08e8d6 | 2017-05-01 15:15:41 +0000 | [diff] [blame] | 4167 | pCx->pKeyInfo, pCx->uc.pCursor); |
drh | 3f4df4c | 2017-05-02 17:54:19 +0000 | [diff] [blame] | 4168 | /* The sqlite3BtreeCursor() routine can only fail for the first cursor |
| 4169 | ** opened for a database. Since there is already an open cursor when this |
| 4170 | ** opcode is run, the sqlite3BtreeCursor() cannot fail */ |
| 4171 | assert( rc==SQLITE_OK ); |
drh | e08e8d6 | 2017-05-01 15:15:41 +0000 | [diff] [blame] | 4172 | break; |
| 4173 | } |
| 4174 | |
| 4175 | |
drh | 32881be | 2020-11-17 21:26:13 +0000 | [diff] [blame] | 4176 | /* Opcode: OpenEphemeral P1 P2 P3 P4 P5 |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 4177 | ** Synopsis: nColumn=P2 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4178 | ** |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 4179 | ** Open a new cursor P1 to a transient table. |
drh | 9170dd7 | 2005-07-08 17:13:46 +0000 | [diff] [blame] | 4180 | ** The cursor is always opened read/write even if |
drh | 25d3adb | 2010-04-05 15:11:08 +0000 | [diff] [blame] | 4181 | ** the main database is read-only. The ephemeral |
drh | 9170dd7 | 2005-07-08 17:13:46 +0000 | [diff] [blame] | 4182 | ** table is deleted automatically when the cursor is closed. |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 4183 | ** |
drh | dfe3b58 | 2019-01-04 12:35:50 +0000 | [diff] [blame] | 4184 | ** If the cursor P1 is already opened on an ephemeral table, the table |
drh | 4afdfa1 | 2018-12-31 16:36:42 +0000 | [diff] [blame] | 4185 | ** is cleared (all content is erased). |
| 4186 | ** |
drh | 25d3adb | 2010-04-05 15:11:08 +0000 | [diff] [blame] | 4187 | ** P2 is the number of columns in the ephemeral table. |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 4188 | ** The cursor points to a BTree table if P4==0 and to a BTree index |
| 4189 | ** 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] | 4190 | ** that defines the format of keys in the index. |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 4191 | ** |
drh | 2a5d990 | 2011-08-26 00:34:45 +0000 | [diff] [blame] | 4192 | ** The P5 parameter can be a mask of the BTREE_* flags defined |
| 4193 | ** in btree.h. These flags control aspects of the operation of |
| 4194 | ** the btree. The BTREE_OMIT_JOURNAL and BTREE_SINGLE flags are |
| 4195 | ** added automatically. |
drh | 32881be | 2020-11-17 21:26:13 +0000 | [diff] [blame] | 4196 | ** |
| 4197 | ** If P3 is positive, then reg[P3] is modified slightly so that it |
| 4198 | ** can be used as zero-length data for OP_Insert. This is an optimization |
| 4199 | ** that avoids an extra OP_Blob opcode to initialize that register. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4200 | */ |
drh | a21a64d | 2010-04-06 22:33:55 +0000 | [diff] [blame] | 4201 | /* Opcode: OpenAutoindex P1 P2 * P4 * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 4202 | ** Synopsis: nColumn=P2 |
drh | a21a64d | 2010-04-06 22:33:55 +0000 | [diff] [blame] | 4203 | ** |
| 4204 | ** This opcode works the same as OP_OpenEphemeral. It has a |
| 4205 | ** different name to distinguish its use. Tables created using |
| 4206 | ** by this opcode will be used for automatically created transient |
| 4207 | ** indices in joins. |
| 4208 | */ |
| 4209 | case OP_OpenAutoindex: |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4210 | case OP_OpenEphemeral: { |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 4211 | VdbeCursor *pCx; |
drh | 41e13e1 | 2013-11-07 14:09:39 +0000 | [diff] [blame] | 4212 | KeyInfo *pKeyInfo; |
| 4213 | |
drh | d4187c7 | 2010-08-30 22:15:45 +0000 | [diff] [blame] | 4214 | static const int vfsFlags = |
drh | 33f4e02 | 2007-09-03 15:19:34 +0000 | [diff] [blame] | 4215 | SQLITE_OPEN_READWRITE | |
| 4216 | SQLITE_OPEN_CREATE | |
| 4217 | SQLITE_OPEN_EXCLUSIVE | |
| 4218 | SQLITE_OPEN_DELETEONCLOSE | |
| 4219 | SQLITE_OPEN_TRANSIENT_DB; |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 4220 | assert( pOp->p1>=0 ); |
drh | 399af1d | 2013-11-20 17:25:55 +0000 | [diff] [blame] | 4221 | assert( pOp->p2>=0 ); |
drh | 32881be | 2020-11-17 21:26:13 +0000 | [diff] [blame] | 4222 | if( pOp->p3>0 ){ |
| 4223 | /* Make register reg[P3] into a value that can be used as the data |
| 4224 | ** form sqlite3BtreeInsert() where the length of the data is zero. */ |
| 4225 | assert( pOp->p2==0 ); /* Only used when number of columns is zero */ |
| 4226 | assert( pOp->opcode==OP_OpenEphemeral ); |
| 4227 | assert( aMem[pOp->p3].flags & MEM_Null ); |
| 4228 | aMem[pOp->p3].n = 0; |
| 4229 | aMem[pOp->p3].z = ""; |
| 4230 | } |
drh | 4afdfa1 | 2018-12-31 16:36:42 +0000 | [diff] [blame] | 4231 | pCx = p->apCsr[pOp->p1]; |
drh | 7132f43 | 2021-10-20 12:52:12 +0000 | [diff] [blame] | 4232 | if( pCx && !pCx->hasBeenDuped && ALWAYS(pOp->p2<=pCx->nField) ){ |
drh | 5a4a15f | 2021-03-18 15:42:59 +0000 | [diff] [blame] | 4233 | /* If the ephermeral table is already open and has no duplicates from |
| 4234 | ** OP_OpenDup, then erase all existing content so that the table is |
| 4235 | ** empty again, rather than creating a new table. */ |
dan | a512972 | 2019-05-03 18:50:24 +0000 | [diff] [blame] | 4236 | assert( pCx->isEphemeral ); |
dan | 855b5d1 | 2019-06-26 21:04:30 +0000 | [diff] [blame] | 4237 | pCx->seqCount = 0; |
| 4238 | pCx->cacheStatus = CACHE_STALE; |
drh | b248668 | 2022-01-03 01:43:28 +0000 | [diff] [blame] | 4239 | rc = sqlite3BtreeClearTable(pCx->ub.pBtx, pCx->pgnoRoot, 0); |
drh | d0fb796 | 2018-12-31 17:58:05 +0000 | [diff] [blame] | 4240 | }else{ |
drh | b248668 | 2022-01-03 01:43:28 +0000 | [diff] [blame] | 4241 | pCx = allocateCursor(p, pOp->p1, pOp->p2, CURTYPE_BTREE); |
drh | d0fb796 | 2018-12-31 17:58:05 +0000 | [diff] [blame] | 4242 | if( pCx==0 ) goto no_mem; |
drh | d0fb796 | 2018-12-31 17:58:05 +0000 | [diff] [blame] | 4243 | pCx->isEphemeral = 1; |
drh | b248668 | 2022-01-03 01:43:28 +0000 | [diff] [blame] | 4244 | rc = sqlite3BtreeOpen(db->pVfs, 0, db, &pCx->ub.pBtx, |
drh | d0fb796 | 2018-12-31 17:58:05 +0000 | [diff] [blame] | 4245 | BTREE_OMIT_JOURNAL | BTREE_SINGLE | pOp->p5, |
| 4246 | vfsFlags); |
| 4247 | if( rc==SQLITE_OK ){ |
drh | b248668 | 2022-01-03 01:43:28 +0000 | [diff] [blame] | 4248 | rc = sqlite3BtreeBeginTrans(pCx->ub.pBtx, 1, 0); |
dan | eeee8a5 | 2021-03-18 14:31:37 +0000 | [diff] [blame] | 4249 | if( rc==SQLITE_OK ){ |
| 4250 | /* If a transient index is required, create it by calling |
| 4251 | ** sqlite3BtreeCreateTable() with the BTREE_BLOBKEY flag before |
| 4252 | ** opening it. If a transient table is required, just use the |
| 4253 | ** automatically created table with root-page 1 (an BLOB_INTKEY table). |
| 4254 | */ |
| 4255 | if( (pCx->pKeyInfo = pKeyInfo = pOp->p4.pKeyInfo)!=0 ){ |
| 4256 | assert( pOp->p4type==P4_KEYINFO ); |
drh | b248668 | 2022-01-03 01:43:28 +0000 | [diff] [blame] | 4257 | rc = sqlite3BtreeCreateTable(pCx->ub.pBtx, &pCx->pgnoRoot, |
dan | eeee8a5 | 2021-03-18 14:31:37 +0000 | [diff] [blame] | 4258 | BTREE_BLOBKEY | pOp->p5); |
| 4259 | if( rc==SQLITE_OK ){ |
| 4260 | assert( pCx->pgnoRoot==SCHEMA_ROOT+1 ); |
| 4261 | assert( pKeyInfo->db==db ); |
| 4262 | assert( pKeyInfo->enc==ENC(db) ); |
drh | b248668 | 2022-01-03 01:43:28 +0000 | [diff] [blame] | 4263 | rc = sqlite3BtreeCursor(pCx->ub.pBtx, pCx->pgnoRoot, BTREE_WRCSR, |
dan | eeee8a5 | 2021-03-18 14:31:37 +0000 | [diff] [blame] | 4264 | pKeyInfo, pCx->uc.pCursor); |
| 4265 | } |
| 4266 | pCx->isTable = 0; |
| 4267 | }else{ |
| 4268 | pCx->pgnoRoot = SCHEMA_ROOT; |
drh | b248668 | 2022-01-03 01:43:28 +0000 | [diff] [blame] | 4269 | rc = sqlite3BtreeCursor(pCx->ub.pBtx, SCHEMA_ROOT, BTREE_WRCSR, |
dan | eeee8a5 | 2021-03-18 14:31:37 +0000 | [diff] [blame] | 4270 | 0, pCx->uc.pCursor); |
| 4271 | pCx->isTable = 1; |
drh | d0fb796 | 2018-12-31 17:58:05 +0000 | [diff] [blame] | 4272 | } |
dan | eeee8a5 | 2021-03-18 14:31:37 +0000 | [diff] [blame] | 4273 | } |
| 4274 | pCx->isOrdered = (pOp->p5!=BTREE_UNORDERED); |
| 4275 | if( rc ){ |
drh | b248668 | 2022-01-03 01:43:28 +0000 | [diff] [blame] | 4276 | sqlite3BtreeClose(pCx->ub.pBtx); |
drh | d0fb796 | 2018-12-31 17:58:05 +0000 | [diff] [blame] | 4277 | } |
| 4278 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4279 | } |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 4280 | if( rc ) goto abort_due_to_error; |
dan | 855b5d1 | 2019-06-26 21:04:30 +0000 | [diff] [blame] | 4281 | pCx->nullRow = 1; |
dan | 5134d13 | 2011-09-02 10:31:11 +0000 | [diff] [blame] | 4282 | break; |
| 4283 | } |
| 4284 | |
dan | fad9f9a | 2014-04-01 18:41:51 +0000 | [diff] [blame] | 4285 | /* Opcode: SorterOpen P1 P2 P3 P4 * |
dan | 5134d13 | 2011-09-02 10:31:11 +0000 | [diff] [blame] | 4286 | ** |
| 4287 | ** This opcode works like OP_OpenEphemeral except that it opens |
| 4288 | ** a transient index that is specifically designed to sort large |
| 4289 | ** tables using an external merge-sort algorithm. |
dan | fad9f9a | 2014-04-01 18:41:51 +0000 | [diff] [blame] | 4290 | ** |
| 4291 | ** If argument P3 is non-zero, then it indicates that the sorter may |
| 4292 | ** assume that a stable sort considering the first P3 fields of each |
| 4293 | ** key is sufficient to produce the required results. |
dan | 5134d13 | 2011-09-02 10:31:11 +0000 | [diff] [blame] | 4294 | */ |
drh | ca892a7 | 2011-09-03 00:17:51 +0000 | [diff] [blame] | 4295 | case OP_SorterOpen: { |
dan | 5134d13 | 2011-09-02 10:31:11 +0000 | [diff] [blame] | 4296 | VdbeCursor *pCx; |
drh | 3a94987 | 2012-09-18 13:20:13 +0000 | [diff] [blame] | 4297 | |
drh | 399af1d | 2013-11-20 17:25:55 +0000 | [diff] [blame] | 4298 | assert( pOp->p1>=0 ); |
| 4299 | assert( pOp->p2>=0 ); |
drh | b248668 | 2022-01-03 01:43:28 +0000 | [diff] [blame] | 4300 | pCx = allocateCursor(p, pOp->p1, pOp->p2, CURTYPE_SORTER); |
dan | 5134d13 | 2011-09-02 10:31:11 +0000 | [diff] [blame] | 4301 | if( pCx==0 ) goto no_mem; |
| 4302 | pCx->pKeyInfo = pOp->p4.pKeyInfo; |
drh | 41e13e1 | 2013-11-07 14:09:39 +0000 | [diff] [blame] | 4303 | assert( pCx->pKeyInfo->db==db ); |
| 4304 | assert( pCx->pKeyInfo->enc==ENC(db) ); |
dan | fad9f9a | 2014-04-01 18:41:51 +0000 | [diff] [blame] | 4305 | rc = sqlite3VdbeSorterInit(db, pOp->p3, pCx); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 4306 | if( rc ) goto abort_due_to_error; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4307 | break; |
| 4308 | } |
| 4309 | |
dan | 78d5843 | 2014-03-25 15:04:07 +0000 | [diff] [blame] | 4310 | /* Opcode: SequenceTest P1 P2 * * * |
| 4311 | ** Synopsis: if( cursor[P1].ctr++ ) pc = P2 |
| 4312 | ** |
| 4313 | ** P1 is a sorter cursor. If the sequence counter is currently zero, jump |
| 4314 | ** to P2. Regardless of whether or not the jump is taken, increment the |
| 4315 | ** the sequence value. |
| 4316 | */ |
| 4317 | case OP_SequenceTest: { |
| 4318 | VdbeCursor *pC; |
| 4319 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 4320 | pC = p->apCsr[pOp->p1]; |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 4321 | assert( isSorter(pC) ); |
dan | 78d5843 | 2014-03-25 15:04:07 +0000 | [diff] [blame] | 4322 | if( (pC->seqCount++)==0 ){ |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 4323 | goto jump_to_p2; |
dan | 78d5843 | 2014-03-25 15:04:07 +0000 | [diff] [blame] | 4324 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4325 | break; |
| 4326 | } |
| 4327 | |
drh | 5f61229 | 2014-02-08 23:20:32 +0000 | [diff] [blame] | 4328 | /* Opcode: OpenPseudo P1 P2 P3 * * |
drh | 60830e3 | 2014-02-10 15:56:34 +0000 | [diff] [blame] | 4329 | ** Synopsis: P3 columns in r[P2] |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 4330 | ** |
| 4331 | ** 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] | 4332 | ** row of data. The content of that one row is the content of memory |
| 4333 | ** register P2. In other words, cursor P1 becomes an alias for the |
| 4334 | ** MEM_Blob content contained in register P2. |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 4335 | ** |
drh | 2d8d7ce | 2010-02-15 15:17:05 +0000 | [diff] [blame] | 4336 | ** A pseudo-table created by this opcode is used to hold a single |
drh | cdd536f | 2006-03-17 00:04:03 +0000 | [diff] [blame] | 4337 | ** row output from the sorter so that the row can be decomposed into |
drh | 3e9ca09 | 2009-09-08 01:14:48 +0000 | [diff] [blame] | 4338 | ** individual columns using the OP_Column opcode. The OP_Column opcode |
| 4339 | ** is the only cursor opcode that works with a pseudo-table. |
danielk1977 | d336e22 | 2009-02-20 10:58:41 +0000 | [diff] [blame] | 4340 | ** |
| 4341 | ** P3 is the number of fields in the records that will be stored by |
| 4342 | ** the pseudo-table. |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 4343 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4344 | case OP_OpenPseudo: { |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 4345 | VdbeCursor *pCx; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 4346 | |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 4347 | assert( pOp->p1>=0 ); |
drh | 399af1d | 2013-11-20 17:25:55 +0000 | [diff] [blame] | 4348 | assert( pOp->p3>=0 ); |
drh | b248668 | 2022-01-03 01:43:28 +0000 | [diff] [blame] | 4349 | pCx = allocateCursor(p, pOp->p1, pOp->p3, CURTYPE_PSEUDO); |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 4350 | if( pCx==0 ) goto no_mem; |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 4351 | pCx->nullRow = 1; |
drh | fe0cf7a | 2017-08-16 19:20:20 +0000 | [diff] [blame] | 4352 | pCx->seekResult = pOp->p2; |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 4353 | pCx->isTable = 1; |
drh | fe0cf7a | 2017-08-16 19:20:20 +0000 | [diff] [blame] | 4354 | /* Give this pseudo-cursor a fake BtCursor pointer so that pCx |
| 4355 | ** can be safely passed to sqlite3VdbeCursorMoveto(). This avoids a test |
| 4356 | ** for pCx->eCurType==CURTYPE_BTREE inside of sqlite3VdbeCursorMoveto() |
| 4357 | ** which is a performance optimization */ |
| 4358 | pCx->uc.pCursor = sqlite3BtreeFakeValidCursor(); |
drh | 5f61229 | 2014-02-08 23:20:32 +0000 | [diff] [blame] | 4359 | assert( pOp->p5==0 ); |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 4360 | break; |
| 4361 | } |
| 4362 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4363 | /* Opcode: Close P1 * * * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4364 | ** |
| 4365 | ** Close a cursor previously opened as P1. If P1 is not |
| 4366 | ** currently open, this instruction is a no-op. |
| 4367 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4368 | case OP_Close: { |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 4369 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 4370 | sqlite3VdbeFreeCursor(p, p->apCsr[pOp->p1]); |
| 4371 | p->apCsr[pOp->p1] = 0; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4372 | break; |
| 4373 | } |
| 4374 | |
drh | 97bae79 | 2015-06-05 15:59:57 +0000 | [diff] [blame] | 4375 | #ifdef SQLITE_ENABLE_COLUMN_USED_MASK |
| 4376 | /* Opcode: ColumnsUsed P1 * * P4 * |
| 4377 | ** |
| 4378 | ** This opcode (which only exists if SQLite was compiled with |
| 4379 | ** SQLITE_ENABLE_COLUMN_USED_MASK) identifies which columns of the |
| 4380 | ** table or index for cursor P1 are used. P4 is a 64-bit integer |
| 4381 | ** (P4_INT64) in which the first 63 bits are one for each of the |
| 4382 | ** first 63 columns of the table or index that are actually used |
| 4383 | ** by the cursor. The high-order bit is set if any column after |
| 4384 | ** the 64th is used. |
| 4385 | */ |
| 4386 | case OP_ColumnsUsed: { |
| 4387 | VdbeCursor *pC; |
| 4388 | pC = p->apCsr[pOp->p1]; |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 4389 | assert( pC->eCurType==CURTYPE_BTREE ); |
drh | 97bae79 | 2015-06-05 15:59:57 +0000 | [diff] [blame] | 4390 | pC->maskUsed = *(u64*)pOp->p4.pI64; |
| 4391 | break; |
| 4392 | } |
| 4393 | #endif |
| 4394 | |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 4395 | /* Opcode: SeekGE P1 P2 P3 P4 * |
drh | f63552b | 2013-10-30 00:25:03 +0000 | [diff] [blame] | 4396 | ** Synopsis: key=r[P3@P4] |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4397 | ** |
danielk1977 | b790c6c | 2008-04-18 10:25:24 +0000 | [diff] [blame] | 4398 | ** 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] | 4399 | ** use the value in register P3 as the key. If cursor P1 refers |
danielk1977 | b790c6c | 2008-04-18 10:25:24 +0000 | [diff] [blame] | 4400 | ** to an SQL index, then P3 is the first in an array of P4 registers |
| 4401 | ** that are used as an unpacked index key. |
| 4402 | ** |
| 4403 | ** Reposition cursor P1 so that it points to the smallest entry that |
| 4404 | ** is greater than or equal to the key value. If there are no records |
| 4405 | ** 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] | 4406 | ** |
drh | b1d607d | 2015-11-05 22:30:54 +0000 | [diff] [blame] | 4407 | ** If the cursor P1 was opened using the OPFLAG_SEEKEQ flag, then this |
drh | 576d0a9 | 2020-03-12 17:28:27 +0000 | [diff] [blame] | 4408 | ** opcode will either land on a record that exactly matches the key, or |
| 4409 | ** else it will cause a jump to P2. When the cursor is OPFLAG_SEEKEQ, |
| 4410 | ** this opcode must be followed by an IdxLE opcode with the same arguments. |
| 4411 | ** The IdxGT opcode will be skipped if this opcode succeeds, but the |
| 4412 | ** IdxGT opcode will be used on subsequent loop iterations. The |
| 4413 | ** OPFLAG_SEEKEQ flags is a hint to the btree layer to say that this |
| 4414 | ** is an equality search. |
drh | b1d607d | 2015-11-05 22:30:54 +0000 | [diff] [blame] | 4415 | ** |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 4416 | ** This opcode leaves the cursor configured to move in forward order, |
drh | bc5cf38 | 2014-08-06 01:08:07 +0000 | [diff] [blame] | 4417 | ** from the beginning toward the end. In other words, the cursor is |
drh | 5dad9a3 | 2014-07-25 18:37:42 +0000 | [diff] [blame] | 4418 | ** configured to use Next, not Prev. |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 4419 | ** |
drh | 935850e | 2014-05-24 17:15:15 +0000 | [diff] [blame] | 4420 | ** See also: Found, NotFound, SeekLt, SeekGt, SeekLe |
drh | 7cf6e4d | 2004-05-19 14:56:55 +0000 | [diff] [blame] | 4421 | */ |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 4422 | /* Opcode: SeekGT P1 P2 P3 P4 * |
drh | f63552b | 2013-10-30 00:25:03 +0000 | [diff] [blame] | 4423 | ** Synopsis: key=r[P3@P4] |
drh | 7cf6e4d | 2004-05-19 14:56:55 +0000 | [diff] [blame] | 4424 | ** |
danielk1977 | b790c6c | 2008-04-18 10:25:24 +0000 | [diff] [blame] | 4425 | ** 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] | 4426 | ** use the value in register P3 as a key. If cursor P1 refers |
danielk1977 | b790c6c | 2008-04-18 10:25:24 +0000 | [diff] [blame] | 4427 | ** to an SQL index, then P3 is the first in an array of P4 registers |
| 4428 | ** that are used as an unpacked index key. |
| 4429 | ** |
drh | 576d0a9 | 2020-03-12 17:28:27 +0000 | [diff] [blame] | 4430 | ** Reposition cursor P1 so that it points to the smallest entry that |
danielk1977 | b790c6c | 2008-04-18 10:25:24 +0000 | [diff] [blame] | 4431 | ** is greater than the key value. If there are no records greater than |
| 4432 | ** the key and P2 is not zero, then jump to P2. |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 4433 | ** |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 4434 | ** This opcode leaves the cursor configured to move in forward order, |
drh | 4ed2fb9 | 2014-08-14 13:06:25 +0000 | [diff] [blame] | 4435 | ** from the beginning toward the end. In other words, the cursor is |
drh | 5dad9a3 | 2014-07-25 18:37:42 +0000 | [diff] [blame] | 4436 | ** configured to use Next, not Prev. |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 4437 | ** |
drh | 935850e | 2014-05-24 17:15:15 +0000 | [diff] [blame] | 4438 | ** See also: Found, NotFound, SeekLt, SeekGe, SeekLe |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4439 | */ |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 4440 | /* Opcode: SeekLT P1 P2 P3 P4 * |
drh | f63552b | 2013-10-30 00:25:03 +0000 | [diff] [blame] | 4441 | ** Synopsis: key=r[P3@P4] |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 4442 | ** |
danielk1977 | b790c6c | 2008-04-18 10:25:24 +0000 | [diff] [blame] | 4443 | ** 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] | 4444 | ** use the value in register P3 as a key. If cursor P1 refers |
danielk1977 | b790c6c | 2008-04-18 10:25:24 +0000 | [diff] [blame] | 4445 | ** to an SQL index, then P3 is the first in an array of P4 registers |
| 4446 | ** that are used as an unpacked index key. |
| 4447 | ** |
| 4448 | ** Reposition cursor P1 so that it points to the largest entry that |
| 4449 | ** is less than the key value. If there are no records less than |
| 4450 | ** the key and P2 is not zero, then jump to P2. |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 4451 | ** |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 4452 | ** This opcode leaves the cursor configured to move in reverse order, |
| 4453 | ** from the end toward the beginning. In other words, the cursor is |
drh | 5dad9a3 | 2014-07-25 18:37:42 +0000 | [diff] [blame] | 4454 | ** configured to use Prev, not Next. |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 4455 | ** |
drh | 935850e | 2014-05-24 17:15:15 +0000 | [diff] [blame] | 4456 | ** See also: Found, NotFound, SeekGt, SeekGe, SeekLe |
drh | 7cf6e4d | 2004-05-19 14:56:55 +0000 | [diff] [blame] | 4457 | */ |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 4458 | /* Opcode: SeekLE P1 P2 P3 P4 * |
drh | f63552b | 2013-10-30 00:25:03 +0000 | [diff] [blame] | 4459 | ** Synopsis: key=r[P3@P4] |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 4460 | ** |
danielk1977 | b790c6c | 2008-04-18 10:25:24 +0000 | [diff] [blame] | 4461 | ** 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] | 4462 | ** use the value in register P3 as a key. If cursor P1 refers |
danielk1977 | b790c6c | 2008-04-18 10:25:24 +0000 | [diff] [blame] | 4463 | ** to an SQL index, then P3 is the first in an array of P4 registers |
| 4464 | ** that are used as an unpacked index key. |
danielk1977 | 751de56 | 2008-04-18 09:01:15 +0000 | [diff] [blame] | 4465 | ** |
danielk1977 | b790c6c | 2008-04-18 10:25:24 +0000 | [diff] [blame] | 4466 | ** Reposition cursor P1 so that it points to the largest entry that |
| 4467 | ** is less than or equal to the key value. If there are no records |
| 4468 | ** 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] | 4469 | ** |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 4470 | ** This opcode leaves the cursor configured to move in reverse order, |
| 4471 | ** from the end toward the beginning. In other words, the cursor is |
drh | 5dad9a3 | 2014-07-25 18:37:42 +0000 | [diff] [blame] | 4472 | ** configured to use Prev, not Next. |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 4473 | ** |
drh | b1d607d | 2015-11-05 22:30:54 +0000 | [diff] [blame] | 4474 | ** If the cursor P1 was opened using the OPFLAG_SEEKEQ flag, then this |
drh | 576d0a9 | 2020-03-12 17:28:27 +0000 | [diff] [blame] | 4475 | ** opcode will either land on a record that exactly matches the key, or |
| 4476 | ** else it will cause a jump to P2. When the cursor is OPFLAG_SEEKEQ, |
| 4477 | ** this opcode must be followed by an IdxLE opcode with the same arguments. |
drh | b1d607d | 2015-11-05 22:30:54 +0000 | [diff] [blame] | 4478 | ** The IdxGE opcode will be skipped if this opcode succeeds, but the |
drh | 576d0a9 | 2020-03-12 17:28:27 +0000 | [diff] [blame] | 4479 | ** IdxGE opcode will be used on subsequent loop iterations. The |
| 4480 | ** OPFLAG_SEEKEQ flags is a hint to the btree layer to say that this |
| 4481 | ** is an equality search. |
drh | b1d607d | 2015-11-05 22:30:54 +0000 | [diff] [blame] | 4482 | ** |
drh | 935850e | 2014-05-24 17:15:15 +0000 | [diff] [blame] | 4483 | ** See also: Found, NotFound, SeekGt, SeekGe, SeekLt |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 4484 | */ |
mistachkin | 758784d | 2018-07-25 15:12:29 +0000 | [diff] [blame] | 4485 | case OP_SeekLT: /* jump, in3, group */ |
| 4486 | case OP_SeekLE: /* jump, in3, group */ |
| 4487 | case OP_SeekGE: /* jump, in3, group */ |
| 4488 | case OP_SeekGT: { /* jump, in3, group */ |
drh | b1d607d | 2015-11-05 22:30:54 +0000 | [diff] [blame] | 4489 | int res; /* Comparison result */ |
| 4490 | int oc; /* Opcode */ |
| 4491 | VdbeCursor *pC; /* The cursor to seek */ |
| 4492 | UnpackedRecord r; /* The key to seek for */ |
| 4493 | int nField; /* Number of columns or fields in the key */ |
| 4494 | i64 iKey; /* The rowid we are to seek to */ |
drh | d6b7946 | 2015-11-07 01:19:00 +0000 | [diff] [blame] | 4495 | int eqOnly; /* Only interested in == results */ |
drh | 80ff32f | 2001-11-04 18:32:46 +0000 | [diff] [blame] | 4496 | |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 4497 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
drh | 959403f | 2008-12-12 17:56:16 +0000 | [diff] [blame] | 4498 | assert( pOp->p2!=0 ); |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 4499 | pC = p->apCsr[pOp->p1]; |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 4500 | assert( pC!=0 ); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 4501 | assert( pC->eCurType==CURTYPE_BTREE ); |
drh | 4a1d365 | 2014-02-14 15:13:36 +0000 | [diff] [blame] | 4502 | assert( OP_SeekLE == OP_SeekLT+1 ); |
| 4503 | assert( OP_SeekGE == OP_SeekLT+2 ); |
| 4504 | assert( OP_SeekGT == OP_SeekLT+3 ); |
drh | d4187c7 | 2010-08-30 22:15:45 +0000 | [diff] [blame] | 4505 | assert( pC->isOrdered ); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 4506 | assert( pC->uc.pCursor!=0 ); |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 4507 | oc = pOp->opcode; |
drh | d6b7946 | 2015-11-07 01:19:00 +0000 | [diff] [blame] | 4508 | eqOnly = 0; |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 4509 | pC->nullRow = 0; |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 4510 | #ifdef SQLITE_DEBUG |
| 4511 | pC->seekOp = pOp->opcode; |
| 4512 | #endif |
drh | e0997b3 | 2015-03-20 14:57:50 +0000 | [diff] [blame] | 4513 | |
dan | a40cb96 | 2019-05-14 20:25:22 +0000 | [diff] [blame] | 4514 | pC->deferredMoveto = 0; |
| 4515 | pC->cacheStatus = CACHE_STALE; |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 4516 | if( pC->isTable ){ |
drh | 3e36480 | 2019-08-22 00:53:16 +0000 | [diff] [blame] | 4517 | u16 flags3, newType; |
drh | 576d0a9 | 2020-03-12 17:28:27 +0000 | [diff] [blame] | 4518 | /* The OPFLAG_SEEKEQ/BTREE_SEEK_EQ flag is only set on index cursors */ |
drh | 218c66e | 2016-12-27 12:35:36 +0000 | [diff] [blame] | 4519 | assert( sqlite3BtreeCursorHasHint(pC->uc.pCursor, BTREE_SEEK_EQ)==0 |
| 4520 | || CORRUPT_DB ); |
drh | d6b7946 | 2015-11-07 01:19:00 +0000 | [diff] [blame] | 4521 | |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 4522 | /* The input value in P3 might be of any type: integer, real, string, |
| 4523 | ** 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] | 4524 | ** the seek, so convert it. */ |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 4525 | pIn3 = &aMem[pOp->p3]; |
drh | 3e36480 | 2019-08-22 00:53:16 +0000 | [diff] [blame] | 4526 | flags3 = pIn3->flags; |
| 4527 | if( (flags3 & (MEM_Int|MEM_Real|MEM_IntReal|MEM_Str))==MEM_Str ){ |
drh | bd9507c | 2014-08-23 17:21:37 +0000 | [diff] [blame] | 4528 | applyNumericAffinity(pIn3, 0); |
| 4529 | } |
drh | 3e36480 | 2019-08-22 00:53:16 +0000 | [diff] [blame] | 4530 | iKey = sqlite3VdbeIntValue(pIn3); /* Get the integer key value */ |
| 4531 | newType = pIn3->flags; /* Record the type after applying numeric affinity */ |
| 4532 | pIn3->flags = flags3; /* But convert the type back to its original */ |
drh | 959403f | 2008-12-12 17:56:16 +0000 | [diff] [blame] | 4533 | |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 4534 | /* If the P3 value could not be converted into an integer without |
| 4535 | ** loss of information, then special processing is required... */ |
drh | 3e36480 | 2019-08-22 00:53:16 +0000 | [diff] [blame] | 4536 | if( (newType & (MEM_Int|MEM_IntReal))==0 ){ |
drh | de32461 | 2021-07-19 20:52:31 +0000 | [diff] [blame] | 4537 | int c; |
drh | 3e36480 | 2019-08-22 00:53:16 +0000 | [diff] [blame] | 4538 | if( (newType & MEM_Real)==0 ){ |
| 4539 | if( (newType & MEM_Null) || oc>=OP_SeekGE ){ |
drh | 8616cff | 2019-07-13 16:15:23 +0000 | [diff] [blame] | 4540 | VdbeBranchTaken(1,2); |
| 4541 | goto jump_to_p2; |
dan | 9edd8c1 | 2019-05-08 11:42:49 +0000 | [diff] [blame] | 4542 | }else{ |
dan | 873b019 | 2019-05-09 11:19:27 +0000 | [diff] [blame] | 4543 | rc = sqlite3BtreeLast(pC->uc.pCursor, &res); |
| 4544 | if( rc!=SQLITE_OK ) goto abort_due_to_error; |
dan | 9edd8c1 | 2019-05-08 11:42:49 +0000 | [diff] [blame] | 4545 | goto seek_not_found; |
| 4546 | } |
drh | de32461 | 2021-07-19 20:52:31 +0000 | [diff] [blame] | 4547 | } |
| 4548 | c = sqlite3IntFloatCompare(iKey, pIn3->u.r); |
drh | 959403f | 2008-12-12 17:56:16 +0000 | [diff] [blame] | 4549 | |
dan | aa1776f | 2013-11-26 18:22:59 +0000 | [diff] [blame] | 4550 | /* If the approximation iKey is larger than the actual real search |
| 4551 | ** term, substitute >= for > and < for <=. e.g. if the search term |
| 4552 | ** is 4.9 and the integer approximation 5: |
| 4553 | ** |
| 4554 | ** (x > 4.9) -> (x >= 5) |
| 4555 | ** (x <= 4.9) -> (x < 5) |
| 4556 | */ |
drh | de32461 | 2021-07-19 20:52:31 +0000 | [diff] [blame] | 4557 | if( c>0 ){ |
drh | 4a1d365 | 2014-02-14 15:13:36 +0000 | [diff] [blame] | 4558 | assert( OP_SeekGE==(OP_SeekGT-1) ); |
| 4559 | assert( OP_SeekLT==(OP_SeekLE-1) ); |
| 4560 | assert( (OP_SeekLE & 0x0001)==(OP_SeekGT & 0x0001) ); |
| 4561 | if( (oc & 0x0001)==(OP_SeekGT & 0x0001) ) oc--; |
dan | aa1776f | 2013-11-26 18:22:59 +0000 | [diff] [blame] | 4562 | } |
| 4563 | |
| 4564 | /* If the approximation iKey is smaller than the actual real search |
| 4565 | ** term, substitute <= for < and > for >=. */ |
drh | de32461 | 2021-07-19 20:52:31 +0000 | [diff] [blame] | 4566 | else if( c<0 ){ |
drh | 4a1d365 | 2014-02-14 15:13:36 +0000 | [diff] [blame] | 4567 | assert( OP_SeekLE==(OP_SeekLT+1) ); |
| 4568 | assert( OP_SeekGT==(OP_SeekGE+1) ); |
| 4569 | assert( (OP_SeekLT & 0x0001)==(OP_SeekGE & 0x0001) ); |
| 4570 | if( (oc & 0x0001)==(OP_SeekLT & 0x0001) ) oc++; |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 4571 | } |
dan | 9edd8c1 | 2019-05-08 11:42:49 +0000 | [diff] [blame] | 4572 | } |
drh | 42a410d | 2021-06-19 18:32:20 +0000 | [diff] [blame] | 4573 | rc = sqlite3BtreeTableMoveto(pC->uc.pCursor, (u64)iKey, 0, &res); |
drh | b53a5a9 | 2014-10-12 22:37:22 +0000 | [diff] [blame] | 4574 | pC->movetoTarget = iKey; /* Used by OP_Delete */ |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 4575 | if( rc!=SQLITE_OK ){ |
| 4576 | goto abort_due_to_error; |
drh | 1af3fdb | 2004-07-18 21:33:01 +0000 | [diff] [blame] | 4577 | } |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 4578 | }else{ |
drh | 576d0a9 | 2020-03-12 17:28:27 +0000 | [diff] [blame] | 4579 | /* For a cursor with the OPFLAG_SEEKEQ/BTREE_SEEK_EQ hint, only the |
| 4580 | ** OP_SeekGE and OP_SeekLE opcodes are allowed, and these must be |
| 4581 | ** immediately followed by an OP_IdxGT or OP_IdxLT opcode, respectively, |
| 4582 | ** with the same key. |
drh | d6b7946 | 2015-11-07 01:19:00 +0000 | [diff] [blame] | 4583 | */ |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 4584 | if( sqlite3BtreeCursorHasHint(pC->uc.pCursor, BTREE_SEEK_EQ) ){ |
drh | d6b7946 | 2015-11-07 01:19:00 +0000 | [diff] [blame] | 4585 | eqOnly = 1; |
| 4586 | assert( pOp->opcode==OP_SeekGE || pOp->opcode==OP_SeekLE ); |
| 4587 | assert( pOp[1].opcode==OP_IdxLT || pOp[1].opcode==OP_IdxGT ); |
drh | 576d0a9 | 2020-03-12 17:28:27 +0000 | [diff] [blame] | 4588 | assert( pOp->opcode==OP_SeekGE || pOp[1].opcode==OP_IdxLT ); |
| 4589 | assert( pOp->opcode==OP_SeekLE || pOp[1].opcode==OP_IdxGT ); |
drh | d6b7946 | 2015-11-07 01:19:00 +0000 | [diff] [blame] | 4590 | assert( pOp[1].p1==pOp[0].p1 ); |
| 4591 | assert( pOp[1].p2==pOp[0].p2 ); |
| 4592 | assert( pOp[1].p3==pOp[0].p3 ); |
| 4593 | assert( pOp[1].p4.i==pOp[0].p4.i ); |
| 4594 | } |
| 4595 | |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 4596 | nField = pOp->p4.i; |
| 4597 | assert( pOp->p4type==P4_INT32 ); |
| 4598 | assert( nField>0 ); |
| 4599 | r.pKeyInfo = pC->pKeyInfo; |
| 4600 | r.nField = (u16)nField; |
| 4601 | |
| 4602 | /* The next line of code computes as follows, only faster: |
drh | 4a1d365 | 2014-02-14 15:13:36 +0000 | [diff] [blame] | 4603 | ** if( oc==OP_SeekGT || oc==OP_SeekLE ){ |
dan | 1fed5da | 2014-02-25 21:01:25 +0000 | [diff] [blame] | 4604 | ** r.default_rc = -1; |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 4605 | ** }else{ |
dan | 1fed5da | 2014-02-25 21:01:25 +0000 | [diff] [blame] | 4606 | ** r.default_rc = +1; |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 4607 | ** } |
danielk1977 | f7b9d66 | 2008-06-23 18:49:43 +0000 | [diff] [blame] | 4608 | */ |
dan | 1fed5da | 2014-02-25 21:01:25 +0000 | [diff] [blame] | 4609 | r.default_rc = ((1 & (oc - OP_SeekLT)) ? -1 : +1); |
| 4610 | assert( oc!=OP_SeekGT || r.default_rc==-1 ); |
| 4611 | assert( oc!=OP_SeekLE || r.default_rc==-1 ); |
| 4612 | assert( oc!=OP_SeekGE || r.default_rc==+1 ); |
| 4613 | assert( oc!=OP_SeekLT || r.default_rc==+1 ); |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 4614 | |
| 4615 | r.aMem = &aMem[pOp->p3]; |
| 4616 | #ifdef SQLITE_DEBUG |
| 4617 | { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); } |
| 4618 | #endif |
drh | 70528d7 | 2015-11-05 20:25:09 +0000 | [diff] [blame] | 4619 | r.eqSeen = 0; |
drh | 42a410d | 2021-06-19 18:32:20 +0000 | [diff] [blame] | 4620 | rc = sqlite3BtreeIndexMoveto(pC->uc.pCursor, &r, &res); |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 4621 | if( rc!=SQLITE_OK ){ |
| 4622 | goto abort_due_to_error; |
| 4623 | } |
drh | b1d607d | 2015-11-05 22:30:54 +0000 | [diff] [blame] | 4624 | if( eqOnly && r.eqSeen==0 ){ |
| 4625 | assert( res!=0 ); |
| 4626 | goto seek_not_found; |
drh | 70528d7 | 2015-11-05 20:25:09 +0000 | [diff] [blame] | 4627 | } |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 4628 | } |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 4629 | #ifdef SQLITE_TEST |
| 4630 | sqlite3_search_count++; |
| 4631 | #endif |
drh | 4a1d365 | 2014-02-14 15:13:36 +0000 | [diff] [blame] | 4632 | if( oc>=OP_SeekGE ){ assert( oc==OP_SeekGE || oc==OP_SeekGT ); |
| 4633 | if( res<0 || (res==0 && oc==OP_SeekGT) ){ |
drh | e39a732 | 2014-02-03 14:04:11 +0000 | [diff] [blame] | 4634 | res = 0; |
drh | 2ab792e | 2017-05-30 18:34:07 +0000 | [diff] [blame] | 4635 | rc = sqlite3BtreeNext(pC->uc.pCursor, 0); |
| 4636 | if( rc!=SQLITE_OK ){ |
| 4637 | if( rc==SQLITE_DONE ){ |
| 4638 | rc = SQLITE_OK; |
| 4639 | res = 1; |
| 4640 | }else{ |
| 4641 | goto abort_due_to_error; |
| 4642 | } |
| 4643 | } |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 4644 | }else{ |
| 4645 | res = 0; |
| 4646 | } |
| 4647 | }else{ |
drh | 4a1d365 | 2014-02-14 15:13:36 +0000 | [diff] [blame] | 4648 | assert( oc==OP_SeekLT || oc==OP_SeekLE ); |
| 4649 | if( res>0 || (res==0 && oc==OP_SeekLT) ){ |
drh | e39a732 | 2014-02-03 14:04:11 +0000 | [diff] [blame] | 4650 | res = 0; |
drh | 2ab792e | 2017-05-30 18:34:07 +0000 | [diff] [blame] | 4651 | rc = sqlite3BtreePrevious(pC->uc.pCursor, 0); |
| 4652 | if( rc!=SQLITE_OK ){ |
| 4653 | if( rc==SQLITE_DONE ){ |
| 4654 | rc = SQLITE_OK; |
| 4655 | res = 1; |
| 4656 | }else{ |
| 4657 | goto abort_due_to_error; |
| 4658 | } |
| 4659 | } |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 4660 | }else{ |
| 4661 | /* res might be negative because the table is empty. Check to |
| 4662 | ** see if this is the case. |
| 4663 | */ |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 4664 | res = sqlite3BtreeEof(pC->uc.pCursor); |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 4665 | } |
| 4666 | } |
drh | b1d607d | 2015-11-05 22:30:54 +0000 | [diff] [blame] | 4667 | seek_not_found: |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 4668 | assert( pOp->p2>0 ); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 4669 | VdbeBranchTaken(res!=0,2); |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 4670 | if( res ){ |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 4671 | goto jump_to_p2; |
drh | b1d607d | 2015-11-05 22:30:54 +0000 | [diff] [blame] | 4672 | }else if( eqOnly ){ |
| 4673 | assert( pOp[1].opcode==OP_IdxLT || pOp[1].opcode==OP_IdxGT ); |
| 4674 | pOp++; /* Skip the OP_IdxLt or OP_IdxGT that follows */ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4675 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4676 | break; |
| 4677 | } |
dan | 71c57db | 2016-07-09 20:23:55 +0000 | [diff] [blame] | 4678 | |
drh | 68cf0ac | 2020-09-28 19:51:54 +0000 | [diff] [blame] | 4679 | |
drh | 04e70ce | 2020-10-02 11:55:07 +0000 | [diff] [blame] | 4680 | /* Opcode: SeekScan P1 P2 * * * |
drh | 68cf0ac | 2020-09-28 19:51:54 +0000 | [diff] [blame] | 4681 | ** Synopsis: Scan-ahead up to P1 rows |
| 4682 | ** |
drh | dfbaae7 | 2020-09-29 17:29:11 +0000 | [diff] [blame] | 4683 | ** This opcode is a prefix opcode to OP_SeekGE. In other words, this |
drh | 04e70ce | 2020-10-02 11:55:07 +0000 | [diff] [blame] | 4684 | ** opcode must be immediately followed by OP_SeekGE. This constraint is |
drh | dfbaae7 | 2020-09-29 17:29:11 +0000 | [diff] [blame] | 4685 | ** checked by assert() statements. |
| 4686 | ** |
| 4687 | ** This opcode uses the P1 through P4 operands of the subsequent |
| 4688 | ** OP_SeekGE. In the text that follows, the operands of the subsequent |
| 4689 | ** OP_SeekGE opcode are denoted as SeekOP.P1 through SeekOP.P4. Only |
drh | 04e70ce | 2020-10-02 11:55:07 +0000 | [diff] [blame] | 4690 | ** the P1 and P2 operands of this opcode are also used, and are called |
| 4691 | ** This.P1 and This.P2. |
drh | 68cf0ac | 2020-09-28 19:51:54 +0000 | [diff] [blame] | 4692 | ** |
| 4693 | ** This opcode helps to optimize IN operators on a multi-column index |
drh | dfbaae7 | 2020-09-29 17:29:11 +0000 | [diff] [blame] | 4694 | ** where the IN operator is on the later terms of the index by avoiding |
| 4695 | ** unnecessary seeks on the btree, substituting steps to the next row |
| 4696 | ** of the b-tree instead. A correct answer is obtained if this opcode |
| 4697 | ** is omitted or is a no-op. |
drh | 68cf0ac | 2020-09-28 19:51:54 +0000 | [diff] [blame] | 4698 | ** |
drh | dfbaae7 | 2020-09-29 17:29:11 +0000 | [diff] [blame] | 4699 | ** The SeekGE.P3 and SeekGE.P4 operands identify an unpacked key which |
| 4700 | ** is the desired entry that we want the cursor SeekGE.P1 to be pointing |
| 4701 | ** to. Call this SeekGE.P4/P5 row the "target". |
drh | 68cf0ac | 2020-09-28 19:51:54 +0000 | [diff] [blame] | 4702 | ** |
drh | a54e1b1 | 2020-09-29 23:52:25 +0000 | [diff] [blame] | 4703 | ** If the SeekGE.P1 cursor is not currently pointing to a valid row, |
| 4704 | ** then this opcode is a no-op and control passes through into the OP_SeekGE. |
drh | 68cf0ac | 2020-09-28 19:51:54 +0000 | [diff] [blame] | 4705 | ** |
drh | dfbaae7 | 2020-09-29 17:29:11 +0000 | [diff] [blame] | 4706 | ** If the SeekGE.P1 cursor is pointing to a valid row, then that row |
| 4707 | ** might be the target row, or it might be near and slightly before the |
| 4708 | ** target row. This opcode attempts to position the cursor on the target |
drh | 04e70ce | 2020-10-02 11:55:07 +0000 | [diff] [blame] | 4709 | ** row by, perhaps by invoking sqlite3BtreeStep() on the cursor |
drh | dfbaae7 | 2020-09-29 17:29:11 +0000 | [diff] [blame] | 4710 | ** between 0 and This.P1 times. |
drh | 68cf0ac | 2020-09-28 19:51:54 +0000 | [diff] [blame] | 4711 | ** |
drh | dfbaae7 | 2020-09-29 17:29:11 +0000 | [diff] [blame] | 4712 | ** There are three possible outcomes from this opcode:<ol> |
drh | 68cf0ac | 2020-09-28 19:51:54 +0000 | [diff] [blame] | 4713 | ** |
drh | 3c48ee9 | 2021-03-20 01:00:26 +0000 | [diff] [blame] | 4714 | ** <li> If after This.P1 steps, the cursor is still pointing to a place that |
| 4715 | ** is earlier in the btree than the target row, then fall through |
| 4716 | ** into the subsquence OP_SeekGE opcode. |
drh | 68cf0ac | 2020-09-28 19:51:54 +0000 | [diff] [blame] | 4717 | ** |
drh | dfbaae7 | 2020-09-29 17:29:11 +0000 | [diff] [blame] | 4718 | ** <li> If the cursor is successfully moved to the target row by 0 or more |
drh | 04e70ce | 2020-10-02 11:55:07 +0000 | [diff] [blame] | 4719 | ** sqlite3BtreeNext() calls, then jump to This.P2, which will land just |
drh | 3c48ee9 | 2021-03-20 01:00:26 +0000 | [diff] [blame] | 4720 | ** past the OP_IdxGT or OP_IdxGE opcode that follows the OP_SeekGE. |
drh | dfbaae7 | 2020-09-29 17:29:11 +0000 | [diff] [blame] | 4721 | ** |
| 4722 | ** <li> If the cursor ends up past the target row (indicating the the target |
| 4723 | ** row does not exist in the btree) then jump to SeekOP.P2. |
| 4724 | ** </ol> |
drh | 68cf0ac | 2020-09-28 19:51:54 +0000 | [diff] [blame] | 4725 | */ |
| 4726 | case OP_SeekScan: { |
drh | f761d93 | 2020-09-29 01:48:46 +0000 | [diff] [blame] | 4727 | VdbeCursor *pC; |
| 4728 | int res; |
drh | deaa610 | 2020-10-01 15:46:21 +0000 | [diff] [blame] | 4729 | int nStep; |
drh | f761d93 | 2020-09-29 01:48:46 +0000 | [diff] [blame] | 4730 | UnpackedRecord r; |
| 4731 | |
drh | 68cf0ac | 2020-09-28 19:51:54 +0000 | [diff] [blame] | 4732 | assert( pOp[1].opcode==OP_SeekGE ); |
drh | 04e70ce | 2020-10-02 11:55:07 +0000 | [diff] [blame] | 4733 | |
| 4734 | /* pOp->p2 points to the first instruction past the OP_IdxGT that |
| 4735 | ** follows the OP_SeekGE. */ |
| 4736 | assert( pOp->p2>=(int)(pOp-aOp)+2 ); |
drh | 0d08402 | 2021-06-13 19:14:14 +0000 | [diff] [blame] | 4737 | assert( aOp[pOp->p2-1].opcode==OP_IdxGT || aOp[pOp->p2-1].opcode==OP_IdxGE ); |
| 4738 | testcase( aOp[pOp->p2-1].opcode==OP_IdxGE ); |
drh | 04e70ce | 2020-10-02 11:55:07 +0000 | [diff] [blame] | 4739 | assert( pOp[1].p1==aOp[pOp->p2-1].p1 ); |
| 4740 | assert( pOp[1].p2==aOp[pOp->p2-1].p2 ); |
| 4741 | assert( pOp[1].p3==aOp[pOp->p2-1].p3 ); |
drh | 04e70ce | 2020-10-02 11:55:07 +0000 | [diff] [blame] | 4742 | |
drh | 68cf0ac | 2020-09-28 19:51:54 +0000 | [diff] [blame] | 4743 | assert( pOp->p1>0 ); |
drh | f761d93 | 2020-09-29 01:48:46 +0000 | [diff] [blame] | 4744 | pC = p->apCsr[pOp[1].p1]; |
| 4745 | assert( pC!=0 ); |
| 4746 | assert( pC->eCurType==CURTYPE_BTREE ); |
| 4747 | assert( !pC->isTable ); |
drh | a54e1b1 | 2020-09-29 23:52:25 +0000 | [diff] [blame] | 4748 | if( !sqlite3BtreeCursorIsValidNN(pC->uc.pCursor) ){ |
drh | f761d93 | 2020-09-29 01:48:46 +0000 | [diff] [blame] | 4749 | #ifdef SQLITE_DEBUG |
| 4750 | if( db->flags&SQLITE_VdbeTrace ){ |
drh | a54e1b1 | 2020-09-29 23:52:25 +0000 | [diff] [blame] | 4751 | printf("... cursor not valid - fall through\n"); |
drh | f761d93 | 2020-09-29 01:48:46 +0000 | [diff] [blame] | 4752 | } |
| 4753 | #endif |
| 4754 | break; |
| 4755 | } |
drh | deaa610 | 2020-10-01 15:46:21 +0000 | [diff] [blame] | 4756 | nStep = pOp->p1; |
| 4757 | assert( nStep>=1 ); |
drh | f761d93 | 2020-09-29 01:48:46 +0000 | [diff] [blame] | 4758 | r.pKeyInfo = pC->pKeyInfo; |
| 4759 | r.nField = (u16)pOp[1].p4.i; |
| 4760 | r.default_rc = 0; |
| 4761 | r.aMem = &aMem[pOp[1].p3]; |
| 4762 | #ifdef SQLITE_DEBUG |
| 4763 | { |
| 4764 | int i; |
| 4765 | for(i=0; i<r.nField; i++){ |
| 4766 | assert( memIsValid(&r.aMem[i]) ); |
| 4767 | REGISTER_TRACE(pOp[1].p3+i, &aMem[pOp[1].p3+i]); |
| 4768 | } |
| 4769 | } |
| 4770 | #endif |
| 4771 | res = 0; /* Not needed. Only used to silence a warning. */ |
| 4772 | while(1){ |
| 4773 | rc = sqlite3VdbeIdxKeyCompare(db, pC, &r, &res); |
| 4774 | if( rc ) goto abort_due_to_error; |
| 4775 | if( res>0 ){ |
drh | 0b2949c | 2020-09-29 20:22:19 +0000 | [diff] [blame] | 4776 | seekscan_search_fail: |
drh | f761d93 | 2020-09-29 01:48:46 +0000 | [diff] [blame] | 4777 | #ifdef SQLITE_DEBUG |
drh | 0b2949c | 2020-09-29 20:22:19 +0000 | [diff] [blame] | 4778 | if( db->flags&SQLITE_VdbeTrace ){ |
drh | deaa610 | 2020-10-01 15:46:21 +0000 | [diff] [blame] | 4779 | printf("... %d steps and then skip\n", pOp->p1 - nStep); |
drh | 0b2949c | 2020-09-29 20:22:19 +0000 | [diff] [blame] | 4780 | } |
drh | f761d93 | 2020-09-29 01:48:46 +0000 | [diff] [blame] | 4781 | #endif |
drh | 0b2949c | 2020-09-29 20:22:19 +0000 | [diff] [blame] | 4782 | VdbeBranchTaken(1,3); |
drh | f287d00 | 2020-09-30 00:10:22 +0000 | [diff] [blame] | 4783 | pOp++; |
drh | f761d93 | 2020-09-29 01:48:46 +0000 | [diff] [blame] | 4784 | goto jump_to_p2; |
| 4785 | } |
| 4786 | if( res==0 ){ |
| 4787 | #ifdef SQLITE_DEBUG |
drh | 0b2949c | 2020-09-29 20:22:19 +0000 | [diff] [blame] | 4788 | if( db->flags&SQLITE_VdbeTrace ){ |
drh | deaa610 | 2020-10-01 15:46:21 +0000 | [diff] [blame] | 4789 | printf("... %d steps and then success\n", pOp->p1 - nStep); |
drh | 0b2949c | 2020-09-29 20:22:19 +0000 | [diff] [blame] | 4790 | } |
drh | f761d93 | 2020-09-29 01:48:46 +0000 | [diff] [blame] | 4791 | #endif |
drh | 0b2949c | 2020-09-29 20:22:19 +0000 | [diff] [blame] | 4792 | VdbeBranchTaken(2,3); |
drh | 04e70ce | 2020-10-02 11:55:07 +0000 | [diff] [blame] | 4793 | goto jump_to_p2; |
drh | f761d93 | 2020-09-29 01:48:46 +0000 | [diff] [blame] | 4794 | break; |
| 4795 | } |
drh | deaa610 | 2020-10-01 15:46:21 +0000 | [diff] [blame] | 4796 | if( nStep<=0 ){ |
drh | 0b2949c | 2020-09-29 20:22:19 +0000 | [diff] [blame] | 4797 | #ifdef SQLITE_DEBUG |
| 4798 | if( db->flags&SQLITE_VdbeTrace ){ |
| 4799 | printf("... fall through after %d steps\n", pOp->p1); |
| 4800 | } |
| 4801 | #endif |
| 4802 | VdbeBranchTaken(0,3); |
| 4803 | break; |
| 4804 | } |
drh | deaa610 | 2020-10-01 15:46:21 +0000 | [diff] [blame] | 4805 | nStep--; |
drh | f761d93 | 2020-09-29 01:48:46 +0000 | [diff] [blame] | 4806 | rc = sqlite3BtreeNext(pC->uc.pCursor, 0); |
drh | 0b2949c | 2020-09-29 20:22:19 +0000 | [diff] [blame] | 4807 | if( rc ){ |
| 4808 | if( rc==SQLITE_DONE ){ |
| 4809 | rc = SQLITE_OK; |
| 4810 | goto seekscan_search_fail; |
| 4811 | }else{ |
| 4812 | goto abort_due_to_error; |
| 4813 | } |
| 4814 | } |
drh | f761d93 | 2020-09-29 01:48:46 +0000 | [diff] [blame] | 4815 | } |
drh | 0b2949c | 2020-09-29 20:22:19 +0000 | [diff] [blame] | 4816 | |
drh | f761d93 | 2020-09-29 01:48:46 +0000 | [diff] [blame] | 4817 | break; |
drh | 68cf0ac | 2020-09-28 19:51:54 +0000 | [diff] [blame] | 4818 | } |
| 4819 | |
| 4820 | |
drh | fa17e13 | 2020-09-01 01:52:03 +0000 | [diff] [blame] | 4821 | /* Opcode: SeekHit P1 P2 P3 * * |
| 4822 | ** Synopsis: set P2<=seekHit<=P3 |
drh | 8c2b6d7 | 2018-06-05 20:45:20 +0000 | [diff] [blame] | 4823 | ** |
drh | fa17e13 | 2020-09-01 01:52:03 +0000 | [diff] [blame] | 4824 | ** Increase or decrease the seekHit value for cursor P1, if necessary, |
| 4825 | ** so that it is no less than P2 and no greater than P3. |
drh | 8c2b6d7 | 2018-06-05 20:45:20 +0000 | [diff] [blame] | 4826 | ** |
drh | fa17e13 | 2020-09-01 01:52:03 +0000 | [diff] [blame] | 4827 | ** The seekHit integer represents the maximum of terms in an index for which |
| 4828 | ** there is known to be at least one match. If the seekHit value is smaller |
| 4829 | ** than the total number of equality terms in an index lookup, then the |
| 4830 | ** OP_IfNoHope opcode might run to see if the IN loop can be abandoned |
| 4831 | ** early, thus saving work. This is part of the IN-early-out optimization. |
| 4832 | ** |
| 4833 | ** P1 must be a valid b-tree cursor. |
drh | 8c2b6d7 | 2018-06-05 20:45:20 +0000 | [diff] [blame] | 4834 | */ |
| 4835 | case OP_SeekHit: { |
| 4836 | VdbeCursor *pC; |
| 4837 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 4838 | pC = p->apCsr[pOp->p1]; |
| 4839 | assert( pC!=0 ); |
drh | fa17e13 | 2020-09-01 01:52:03 +0000 | [diff] [blame] | 4840 | assert( pOp->p3>=pOp->p2 ); |
| 4841 | if( pC->seekHit<pOp->p2 ){ |
drh | 7bfccfe | 2021-04-29 13:58:28 +0000 | [diff] [blame] | 4842 | #ifdef SQLITE_DEBUG |
| 4843 | if( db->flags&SQLITE_VdbeTrace ){ |
| 4844 | printf("seekHit changes from %d to %d\n", pC->seekHit, pOp->p2); |
| 4845 | } |
| 4846 | #endif |
drh | fa17e13 | 2020-09-01 01:52:03 +0000 | [diff] [blame] | 4847 | pC->seekHit = pOp->p2; |
| 4848 | }else if( pC->seekHit>pOp->p3 ){ |
drh | 7bfccfe | 2021-04-29 13:58:28 +0000 | [diff] [blame] | 4849 | #ifdef SQLITE_DEBUG |
| 4850 | if( db->flags&SQLITE_VdbeTrace ){ |
| 4851 | printf("seekHit changes from %d to %d\n", pC->seekHit, pOp->p3); |
| 4852 | } |
| 4853 | #endif |
drh | fa17e13 | 2020-09-01 01:52:03 +0000 | [diff] [blame] | 4854 | pC->seekHit = pOp->p3; |
| 4855 | } |
drh | 8c2b6d7 | 2018-06-05 20:45:20 +0000 | [diff] [blame] | 4856 | break; |
| 4857 | } |
| 4858 | |
dan | 74ebaad | 2020-01-04 16:55:57 +0000 | [diff] [blame] | 4859 | /* Opcode: IfNotOpen P1 P2 * * * |
| 4860 | ** Synopsis: if( !csr[P1] ) goto P2 |
| 4861 | ** |
| 4862 | ** If cursor P1 is not open, jump to instruction P2. Otherwise, fall through. |
| 4863 | */ |
| 4864 | case OP_IfNotOpen: { /* jump */ |
| 4865 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
drh | 56ea69b | 2020-01-04 18:33:20 +0000 | [diff] [blame] | 4866 | VdbeBranchTaken(p->apCsr[pOp->p1]==0, 2); |
dan | 74ebaad | 2020-01-04 16:55:57 +0000 | [diff] [blame] | 4867 | if( !p->apCsr[pOp->p1] ){ |
| 4868 | goto jump_to_p2_and_check_for_interrupt; |
| 4869 | } |
| 4870 | break; |
| 4871 | } |
| 4872 | |
drh | 8cff69d | 2009-11-12 19:59:44 +0000 | [diff] [blame] | 4873 | /* Opcode: Found P1 P2 P3 P4 * |
drh | f63552b | 2013-10-30 00:25:03 +0000 | [diff] [blame] | 4874 | ** Synopsis: key=r[P3@P4] |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4875 | ** |
drh | 8cff69d | 2009-11-12 19:59:44 +0000 | [diff] [blame] | 4876 | ** If P4==0 then register P3 holds a blob constructed by MakeRecord. If |
| 4877 | ** P4>0 then register P3 is the first of P4 registers that form an unpacked |
| 4878 | ** record. |
| 4879 | ** |
| 4880 | ** Cursor P1 is on an index btree. If the record identified by P3 and P4 |
| 4881 | ** 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] | 4882 | ** P1 is left pointing at the matching entry. |
drh | 6f225d0 | 2013-10-26 13:36:51 +0000 | [diff] [blame] | 4883 | ** |
drh | cefc87f | 2014-08-01 01:40:33 +0000 | [diff] [blame] | 4884 | ** This operation leaves the cursor in a state where it can be |
| 4885 | ** advanced in the forward direction. The Next instruction will work, |
| 4886 | ** but not the Prev instruction. |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 4887 | ** |
drh | 6f225d0 | 2013-10-26 13:36:51 +0000 | [diff] [blame] | 4888 | ** See also: NotFound, NoConflict, NotExists. SeekGe |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4889 | */ |
drh | 8cff69d | 2009-11-12 19:59:44 +0000 | [diff] [blame] | 4890 | /* Opcode: NotFound P1 P2 P3 P4 * |
drh | f63552b | 2013-10-30 00:25:03 +0000 | [diff] [blame] | 4891 | ** Synopsis: key=r[P3@P4] |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4892 | ** |
drh | 8cff69d | 2009-11-12 19:59:44 +0000 | [diff] [blame] | 4893 | ** If P4==0 then register P3 holds a blob constructed by MakeRecord. If |
| 4894 | ** P4>0 then register P3 is the first of P4 registers that form an unpacked |
| 4895 | ** record. |
| 4896 | ** |
| 4897 | ** Cursor P1 is on an index btree. If the record identified by P3 and P4 |
| 4898 | ** is not the prefix of any entry in P1 then a jump is made to P2. If P1 |
| 4899 | ** does contain an entry whose prefix matches the P3/P4 record then control |
| 4900 | ** falls through to the next instruction and P1 is left pointing at the |
| 4901 | ** matching entry. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4902 | ** |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 4903 | ** This operation leaves the cursor in a state where it cannot be |
| 4904 | ** advanced in either direction. In other words, the Next and Prev |
| 4905 | ** opcodes do not work after this operation. |
| 4906 | ** |
drh | 8c2b6d7 | 2018-06-05 20:45:20 +0000 | [diff] [blame] | 4907 | ** See also: Found, NotExists, NoConflict, IfNoHope |
| 4908 | */ |
| 4909 | /* Opcode: IfNoHope P1 P2 P3 P4 * |
| 4910 | ** Synopsis: key=r[P3@P4] |
| 4911 | ** |
| 4912 | ** Register P3 is the first of P4 registers that form an unpacked |
drh | fa17e13 | 2020-09-01 01:52:03 +0000 | [diff] [blame] | 4913 | ** record. Cursor P1 is an index btree. P2 is a jump destination. |
| 4914 | ** In other words, the operands to this opcode are the same as the |
| 4915 | ** operands to OP_NotFound and OP_IdxGT. |
drh | 8c2b6d7 | 2018-06-05 20:45:20 +0000 | [diff] [blame] | 4916 | ** |
drh | fa17e13 | 2020-09-01 01:52:03 +0000 | [diff] [blame] | 4917 | ** This opcode is an optimization attempt only. If this opcode always |
| 4918 | ** falls through, the correct answer is still obtained, but extra works |
| 4919 | ** is performed. |
drh | 8c2b6d7 | 2018-06-05 20:45:20 +0000 | [diff] [blame] | 4920 | ** |
drh | fa17e13 | 2020-09-01 01:52:03 +0000 | [diff] [blame] | 4921 | ** A value of N in the seekHit flag of cursor P1 means that there exists |
| 4922 | ** a key P3:N that will match some record in the index. We want to know |
| 4923 | ** if it is possible for a record P3:P4 to match some record in the |
| 4924 | ** index. If it is not possible, we can skips some work. So if seekHit |
| 4925 | ** is less than P4, attempt to find out if a match is possible by running |
| 4926 | ** OP_NotFound. |
drh | 8c2b6d7 | 2018-06-05 20:45:20 +0000 | [diff] [blame] | 4927 | ** |
| 4928 | ** This opcode is used in IN clause processing for a multi-column key. |
| 4929 | ** If an IN clause is attached to an element of the key other than the |
| 4930 | ** left-most element, and if there are no matches on the most recent |
| 4931 | ** seek over the whole key, then it might be that one of the key element |
| 4932 | ** to the left is prohibiting a match, and hence there is "no hope" of |
| 4933 | ** any match regardless of how many IN clause elements are checked. |
| 4934 | ** In such a case, we abandon the IN clause search early, using this |
| 4935 | ** opcode. The opcode name comes from the fact that the |
| 4936 | ** jump is taken if there is "no hope" of achieving a match. |
| 4937 | ** |
| 4938 | ** See also: NotFound, SeekHit |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4939 | */ |
drh | 6f225d0 | 2013-10-26 13:36:51 +0000 | [diff] [blame] | 4940 | /* Opcode: NoConflict P1 P2 P3 P4 * |
drh | 4af5bee | 2013-10-30 02:37:50 +0000 | [diff] [blame] | 4941 | ** Synopsis: key=r[P3@P4] |
drh | 6f225d0 | 2013-10-26 13:36:51 +0000 | [diff] [blame] | 4942 | ** |
| 4943 | ** If P4==0 then register P3 holds a blob constructed by MakeRecord. If |
| 4944 | ** P4>0 then register P3 is the first of P4 registers that form an unpacked |
| 4945 | ** record. |
| 4946 | ** |
| 4947 | ** Cursor P1 is on an index btree. If the record identified by P3 and P4 |
| 4948 | ** contains any NULL value, jump immediately to P2. If all terms of the |
| 4949 | ** record are not-NULL then a check is done to determine if any row in the |
| 4950 | ** P1 index btree has a matching key prefix. If there are no matches, jump |
| 4951 | ** immediately to P2. If there is a match, fall through and leave the P1 |
| 4952 | ** cursor pointing to the matching row. |
| 4953 | ** |
| 4954 | ** This opcode is similar to OP_NotFound with the exceptions that the |
| 4955 | ** branch is always taken if any part of the search key input is NULL. |
| 4956 | ** |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 4957 | ** This operation leaves the cursor in a state where it cannot be |
| 4958 | ** advanced in either direction. In other words, the Next and Prev |
| 4959 | ** opcodes do not work after this operation. |
| 4960 | ** |
drh | 6f225d0 | 2013-10-26 13:36:51 +0000 | [diff] [blame] | 4961 | ** See also: NotFound, Found, NotExists |
| 4962 | */ |
drh | 8c2b6d7 | 2018-06-05 20:45:20 +0000 | [diff] [blame] | 4963 | case OP_IfNoHope: { /* jump, in3 */ |
| 4964 | VdbeCursor *pC; |
| 4965 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 4966 | pC = p->apCsr[pOp->p1]; |
| 4967 | assert( pC!=0 ); |
drh | 7bfccfe | 2021-04-29 13:58:28 +0000 | [diff] [blame] | 4968 | #ifdef SQLITE_DEBUG |
| 4969 | if( db->flags&SQLITE_VdbeTrace ){ |
| 4970 | printf("seekHit is %d\n", pC->seekHit); |
| 4971 | } |
| 4972 | #endif |
drh | fa17e13 | 2020-09-01 01:52:03 +0000 | [diff] [blame] | 4973 | if( pC->seekHit>=pOp->p4.i ) break; |
drh | 8c2b6d7 | 2018-06-05 20:45:20 +0000 | [diff] [blame] | 4974 | /* Fall through into OP_NotFound */ |
drh | 08b9208 | 2020-08-10 14:18:00 +0000 | [diff] [blame] | 4975 | /* no break */ deliberate_fall_through |
drh | 8c2b6d7 | 2018-06-05 20:45:20 +0000 | [diff] [blame] | 4976 | } |
drh | 6f225d0 | 2013-10-26 13:36:51 +0000 | [diff] [blame] | 4977 | case OP_NoConflict: /* jump, in3 */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4978 | case OP_NotFound: /* jump, in3 */ |
| 4979 | case OP_Found: { /* jump, in3 */ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 4980 | int alreadyExists; |
drh | 6f225d0 | 2013-10-26 13:36:51 +0000 | [diff] [blame] | 4981 | int ii; |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 4982 | VdbeCursor *pC; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 4983 | UnpackedRecord *pIdxKey; |
drh | 8cff69d | 2009-11-12 19:59:44 +0000 | [diff] [blame] | 4984 | UnpackedRecord r; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 4985 | |
dan | 0ff297e | 2009-09-25 17:03:14 +0000 | [diff] [blame] | 4986 | #ifdef SQLITE_TEST |
drh | 6f225d0 | 2013-10-26 13:36:51 +0000 | [diff] [blame] | 4987 | if( pOp->opcode!=OP_NoConflict ) sqlite3_found_count++; |
dan | 0ff297e | 2009-09-25 17:03:14 +0000 | [diff] [blame] | 4988 | #endif |
| 4989 | |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 4990 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
drh | 8cff69d | 2009-11-12 19:59:44 +0000 | [diff] [blame] | 4991 | assert( pOp->p4type==P4_INT32 ); |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 4992 | pC = p->apCsr[pOp->p1]; |
| 4993 | assert( pC!=0 ); |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 4994 | #ifdef SQLITE_DEBUG |
drh | cefc87f | 2014-08-01 01:40:33 +0000 | [diff] [blame] | 4995 | pC->seekOp = pOp->opcode; |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 4996 | #endif |
drh | 95b1036 | 2022-04-13 19:00:57 +0000 | [diff] [blame] | 4997 | r.aMem = &aMem[pOp->p3]; |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 4998 | assert( pC->eCurType==CURTYPE_BTREE ); |
| 4999 | assert( pC->uc.pCursor!=0 ); |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 5000 | assert( pC->isTable==0 ); |
drh | 95b1036 | 2022-04-13 19:00:57 +0000 | [diff] [blame] | 5001 | r.nField = (u16)pOp->p4.i; |
| 5002 | if( r.nField>0 ){ |
drh | b834e0d | 2022-04-04 19:43:57 +0000 | [diff] [blame] | 5003 | /* Key values in an array of registers */ |
drh | b834e0d | 2022-04-04 19:43:57 +0000 | [diff] [blame] | 5004 | r.pKeyInfo = pC->pKeyInfo; |
drh | b834e0d | 2022-04-04 19:43:57 +0000 | [diff] [blame] | 5005 | r.default_rc = 0; |
drh | 8aaf7bc | 2016-09-20 01:19:18 +0000 | [diff] [blame] | 5006 | #ifdef SQLITE_DEBUG |
drh | 826af37 | 2014-02-08 19:12:21 +0000 | [diff] [blame] | 5007 | for(ii=0; ii<r.nField; ii++){ |
| 5008 | assert( memIsValid(&r.aMem[ii]) ); |
drh | 8aaf7bc | 2016-09-20 01:19:18 +0000 | [diff] [blame] | 5009 | assert( (r.aMem[ii].flags & MEM_Zero)==0 || r.aMem[ii].n==0 ); |
drh | 826af37 | 2014-02-08 19:12:21 +0000 | [diff] [blame] | 5010 | if( ii ) REGISTER_TRACE(pOp->p3+ii, &r.aMem[ii]); |
drh | 826af37 | 2014-02-08 19:12:21 +0000 | [diff] [blame] | 5011 | } |
drh | 8aaf7bc | 2016-09-20 01:19:18 +0000 | [diff] [blame] | 5012 | #endif |
drh | ec534e6 | 2022-04-04 20:20:22 +0000 | [diff] [blame] | 5013 | rc = sqlite3BtreeIndexMoveto(pC->uc.pCursor, &r, &pC->seekResult); |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 5014 | }else{ |
drh | b834e0d | 2022-04-04 19:43:57 +0000 | [diff] [blame] | 5015 | /* Composite key generated by OP_MakeRecord */ |
drh | 95b1036 | 2022-04-13 19:00:57 +0000 | [diff] [blame] | 5016 | assert( r.aMem->flags & MEM_Blob ); |
drh | b834e0d | 2022-04-04 19:43:57 +0000 | [diff] [blame] | 5017 | assert( pOp->opcode!=OP_NoConflict ); |
drh | 95b1036 | 2022-04-13 19:00:57 +0000 | [diff] [blame] | 5018 | rc = ExpandBlob(r.aMem); |
drh | e46515b | 2017-05-19 22:51:00 +0000 | [diff] [blame] | 5019 | assert( rc==SQLITE_OK || rc==SQLITE_NOMEM ); |
| 5020 | if( rc ) goto no_mem; |
drh | b834e0d | 2022-04-04 19:43:57 +0000 | [diff] [blame] | 5021 | pIdxKey = sqlite3VdbeAllocUnpackedRecord(pC->pKeyInfo); |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 5022 | if( pIdxKey==0 ) goto no_mem; |
drh | 95b1036 | 2022-04-13 19:00:57 +0000 | [diff] [blame] | 5023 | sqlite3VdbeRecordUnpack(pC->pKeyInfo, r.aMem->n, r.aMem->z, pIdxKey); |
drh | b834e0d | 2022-04-04 19:43:57 +0000 | [diff] [blame] | 5024 | pIdxKey->default_rc = 0; |
drh | ec534e6 | 2022-04-04 20:20:22 +0000 | [diff] [blame] | 5025 | rc = sqlite3BtreeIndexMoveto(pC->uc.pCursor, pIdxKey, &pC->seekResult); |
drh | b834e0d | 2022-04-04 19:43:57 +0000 | [diff] [blame] | 5026 | sqlite3DbFreeNN(db, pIdxKey); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5027 | } |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 5028 | if( rc!=SQLITE_OK ){ |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 5029 | goto abort_due_to_error; |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 5030 | } |
drh | ec534e6 | 2022-04-04 20:20:22 +0000 | [diff] [blame] | 5031 | alreadyExists = (pC->seekResult==0); |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 5032 | pC->nullRow = 1-alreadyExists; |
| 5033 | pC->deferredMoveto = 0; |
| 5034 | pC->cacheStatus = CACHE_STALE; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5035 | if( pOp->opcode==OP_Found ){ |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 5036 | VdbeBranchTaken(alreadyExists!=0,2); |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 5037 | if( alreadyExists ) goto jump_to_p2; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5038 | }else{ |
drh | b834e0d | 2022-04-04 19:43:57 +0000 | [diff] [blame] | 5039 | if( !alreadyExists ){ |
| 5040 | VdbeBranchTaken(1,2); |
| 5041 | goto jump_to_p2; |
| 5042 | } |
| 5043 | if( pOp->opcode==OP_NoConflict ){ |
| 5044 | /* For the OP_NoConflict opcode, take the jump if any of the |
| 5045 | ** input fields are NULL, since any key with a NULL will not |
| 5046 | ** conflict */ |
| 5047 | for(ii=0; ii<r.nField; ii++){ |
| 5048 | if( r.aMem[ii].flags & MEM_Null ){ |
| 5049 | VdbeBranchTaken(1,2); |
| 5050 | goto jump_to_p2; |
| 5051 | } |
| 5052 | } |
| 5053 | } |
| 5054 | VdbeBranchTaken(0,2); |
| 5055 | if( pOp->opcode==OP_IfNoHope ){ |
| 5056 | pC->seekHit = pOp->p4.i; |
| 5057 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5058 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5059 | break; |
| 5060 | } |
| 5061 | |
drh | eeb9565 | 2016-05-26 20:56:38 +0000 | [diff] [blame] | 5062 | /* Opcode: SeekRowid P1 P2 P3 * * |
| 5063 | ** Synopsis: intkey=r[P3] |
| 5064 | ** |
| 5065 | ** P1 is the index of a cursor open on an SQL table btree (with integer |
| 5066 | ** keys). If register P3 does not contain an integer or if P1 does not |
| 5067 | ** contain a record with rowid P3 then jump immediately to P2. |
| 5068 | ** Or, if P2 is 0, raise an SQLITE_CORRUPT error. If P1 does contain |
| 5069 | ** a record with rowid P3 then |
| 5070 | ** leave the cursor pointing at that record and fall through to the next |
| 5071 | ** instruction. |
| 5072 | ** |
| 5073 | ** The OP_NotExists opcode performs the same operation, but with OP_NotExists |
| 5074 | ** the P3 register must be guaranteed to contain an integer value. With this |
| 5075 | ** opcode, register P3 might not contain an integer. |
| 5076 | ** |
| 5077 | ** The OP_NotFound opcode performs the same operation on index btrees |
| 5078 | ** (with arbitrary multi-value keys). |
| 5079 | ** |
| 5080 | ** This opcode leaves the cursor in a state where it cannot be advanced |
| 5081 | ** in either direction. In other words, the Next and Prev opcodes will |
| 5082 | ** not work following this opcode. |
| 5083 | ** |
| 5084 | ** See also: Found, NotFound, NoConflict, SeekRowid |
| 5085 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 5086 | /* Opcode: NotExists P1 P2 P3 * * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 5087 | ** Synopsis: intkey=r[P3] |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 5088 | ** |
drh | 261c02d | 2013-10-25 14:46:15 +0000 | [diff] [blame] | 5089 | ** P1 is the index of a cursor open on an SQL table btree (with integer |
| 5090 | ** 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] | 5091 | ** rowid P3 then jump immediately to P2. Or, if P2 is 0, raise an |
| 5092 | ** SQLITE_CORRUPT error. If P1 does contain a record with rowid P3 then |
| 5093 | ** leave the cursor pointing at that record and fall through to the next |
| 5094 | ** instruction. |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 5095 | ** |
drh | eeb9565 | 2016-05-26 20:56:38 +0000 | [diff] [blame] | 5096 | ** The OP_SeekRowid opcode performs the same operation but also allows the |
| 5097 | ** P3 register to contain a non-integer value, in which case the jump is |
| 5098 | ** always taken. This opcode requires that P3 always contain an integer. |
| 5099 | ** |
drh | 261c02d | 2013-10-25 14:46:15 +0000 | [diff] [blame] | 5100 | ** The OP_NotFound opcode performs the same operation on index btrees |
| 5101 | ** (with arbitrary multi-value keys). |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 5102 | ** |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 5103 | ** This opcode leaves the cursor in a state where it cannot be advanced |
| 5104 | ** in either direction. In other words, the Next and Prev opcodes will |
| 5105 | ** not work following this opcode. |
| 5106 | ** |
drh | eeb9565 | 2016-05-26 20:56:38 +0000 | [diff] [blame] | 5107 | ** See also: Found, NotFound, NoConflict, SeekRowid |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 5108 | */ |
drh | eeb9565 | 2016-05-26 20:56:38 +0000 | [diff] [blame] | 5109 | case OP_SeekRowid: { /* jump, in3 */ |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 5110 | VdbeCursor *pC; |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 5111 | BtCursor *pCrsr; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 5112 | int res; |
| 5113 | u64 iKey; |
| 5114 | |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 5115 | pIn3 = &aMem[pOp->p3]; |
drh | 3242c69 | 2019-05-04 01:29:13 +0000 | [diff] [blame] | 5116 | testcase( pIn3->flags & MEM_Int ); |
| 5117 | testcase( pIn3->flags & MEM_IntReal ); |
drh | b29ef5e | 2019-10-07 01:05:57 +0000 | [diff] [blame] | 5118 | testcase( pIn3->flags & MEM_Real ); |
| 5119 | testcase( (pIn3->flags & (MEM_Str|MEM_Int))==MEM_Str ); |
drh | 169f077 | 2019-05-02 21:36:26 +0000 | [diff] [blame] | 5120 | if( (pIn3->flags & (MEM_Int|MEM_IntReal))==0 ){ |
drh | b29ef5e | 2019-10-07 01:05:57 +0000 | [diff] [blame] | 5121 | /* If pIn3->u.i does not contain an integer, compute iKey as the |
| 5122 | ** integer value of pIn3. Jump to P2 if pIn3 cannot be converted |
| 5123 | ** into an integer without loss of information. Take care to avoid |
| 5124 | ** changing the datatype of pIn3, however, as it is used by other |
| 5125 | ** parts of the prepared statement. */ |
| 5126 | Mem x = pIn3[0]; |
| 5127 | applyAffinity(&x, SQLITE_AFF_NUMERIC, encoding); |
| 5128 | if( (x.flags & MEM_Int)==0 ) goto jump_to_p2; |
| 5129 | iKey = x.u.i; |
| 5130 | goto notExistsWithKey; |
drh | eeb9565 | 2016-05-26 20:56:38 +0000 | [diff] [blame] | 5131 | } |
| 5132 | /* Fall through into OP_NotExists */ |
drh | 08b9208 | 2020-08-10 14:18:00 +0000 | [diff] [blame] | 5133 | /* no break */ deliberate_fall_through |
drh | eeb9565 | 2016-05-26 20:56:38 +0000 | [diff] [blame] | 5134 | case OP_NotExists: /* jump, in3 */ |
| 5135 | pIn3 = &aMem[pOp->p3]; |
drh | e4fe6d4 | 2018-08-03 15:58:07 +0000 | [diff] [blame] | 5136 | assert( (pIn3->flags & MEM_Int)!=0 || pOp->opcode==OP_SeekRowid ); |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 5137 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
drh | b29ef5e | 2019-10-07 01:05:57 +0000 | [diff] [blame] | 5138 | iKey = pIn3->u.i; |
| 5139 | notExistsWithKey: |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 5140 | pC = p->apCsr[pOp->p1]; |
| 5141 | assert( pC!=0 ); |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 5142 | #ifdef SQLITE_DEBUG |
drh | 94f4f87 | 2018-12-20 22:08:32 +0000 | [diff] [blame] | 5143 | if( pOp->opcode==OP_SeekRowid ) pC->seekOp = OP_SeekRowid; |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 5144 | #endif |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 5145 | assert( pC->isTable ); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 5146 | assert( pC->eCurType==CURTYPE_BTREE ); |
| 5147 | pCrsr = pC->uc.pCursor; |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 5148 | assert( pCrsr!=0 ); |
| 5149 | res = 0; |
drh | 42a410d | 2021-06-19 18:32:20 +0000 | [diff] [blame] | 5150 | rc = sqlite3BtreeTableMoveto(pCrsr, iKey, 0, &res); |
drh | b79d552 | 2015-09-14 19:26:37 +0000 | [diff] [blame] | 5151 | assert( rc==SQLITE_OK || res==0 ); |
drh | b53a5a9 | 2014-10-12 22:37:22 +0000 | [diff] [blame] | 5152 | pC->movetoTarget = iKey; /* Used by OP_Delete */ |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 5153 | pC->nullRow = 0; |
| 5154 | pC->cacheStatus = CACHE_STALE; |
| 5155 | pC->deferredMoveto = 0; |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 5156 | VdbeBranchTaken(res!=0,2); |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 5157 | pC->seekResult = res; |
dan | c6157e1 | 2015-09-14 09:23:47 +0000 | [diff] [blame] | 5158 | if( res!=0 ){ |
drh | b79d552 | 2015-09-14 19:26:37 +0000 | [diff] [blame] | 5159 | assert( rc==SQLITE_OK ); |
| 5160 | if( pOp->p2==0 ){ |
| 5161 | rc = SQLITE_CORRUPT_BKPT; |
| 5162 | }else{ |
| 5163 | goto jump_to_p2; |
| 5164 | } |
dan | c6157e1 | 2015-09-14 09:23:47 +0000 | [diff] [blame] | 5165 | } |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 5166 | if( rc ) goto abort_due_to_error; |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 5167 | break; |
| 5168 | } |
| 5169 | |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 5170 | /* Opcode: Sequence P1 P2 * * * |
drh | 079a307 | 2014-03-19 14:10:55 +0000 | [diff] [blame] | 5171 | ** Synopsis: r[P2]=cursor[P1].ctr++ |
drh | 4db38a7 | 2005-09-01 12:16:28 +0000 | [diff] [blame] | 5172 | ** |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 5173 | ** Find the next available sequence number for cursor P1. |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 5174 | ** Write the sequence number into register P2. |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 5175 | ** The sequence number on the cursor is incremented after this |
| 5176 | ** instruction. |
drh | 4db38a7 | 2005-09-01 12:16:28 +0000 | [diff] [blame] | 5177 | */ |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 5178 | case OP_Sequence: { /* out2 */ |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 5179 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 5180 | assert( p->apCsr[pOp->p1]!=0 ); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 5181 | assert( p->apCsr[pOp->p1]->eCurType!=CURTYPE_VTAB ); |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 5182 | pOut = out2Prerelease(p, pOp); |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 5183 | pOut->u.i = p->apCsr[pOp->p1]->seqCount++; |
drh | 4db38a7 | 2005-09-01 12:16:28 +0000 | [diff] [blame] | 5184 | break; |
| 5185 | } |
| 5186 | |
| 5187 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 5188 | /* Opcode: NewRowid P1 P2 P3 * * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 5189 | ** Synopsis: r[P2]=rowid |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5190 | ** |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 5191 | ** 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] | 5192 | ** The record number is not previously used as a key in the database |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 5193 | ** table that cursor P1 points to. The new record number is written |
| 5194 | ** written to register P2. |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 5195 | ** |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 5196 | ** If P3>0 then P3 is a register in the root frame of this VDBE that holds |
| 5197 | ** the largest previously generated record number. No new record numbers are |
| 5198 | ** allowed to be less than this value. When this value reaches its maximum, |
drh | ef8662b | 2011-06-20 21:47:58 +0000 | [diff] [blame] | 5199 | ** an SQLITE_FULL error is generated. The P3 register is updated with the ' |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 5200 | ** generated record number. This P3 mechanism is used to help implement the |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 5201 | ** AUTOINCREMENT feature. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5202 | */ |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 5203 | case OP_NewRowid: { /* out2 */ |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 5204 | i64 v; /* The new rowid */ |
| 5205 | VdbeCursor *pC; /* Cursor of table to get the new rowid */ |
| 5206 | int res; /* Result of an sqlite3BtreeLast() */ |
| 5207 | int cnt; /* Counter to limit the number of searches */ |
mistachkin | d6665c5 | 2021-01-18 19:28:56 +0000 | [diff] [blame] | 5208 | #ifndef SQLITE_OMIT_AUTOINCREMENT |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 5209 | Mem *pMem; /* Register holding largest rowid for AUTOINCREMENT */ |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 5210 | VdbeFrame *pFrame; /* Root frame of VDBE */ |
mistachkin | d6665c5 | 2021-01-18 19:28:56 +0000 | [diff] [blame] | 5211 | #endif |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 5212 | |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 5213 | v = 0; |
| 5214 | res = 0; |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 5215 | pOut = out2Prerelease(p, pOp); |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 5216 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 5217 | pC = p->apCsr[pOp->p1]; |
| 5218 | assert( pC!=0 ); |
drh | 4c57e32 | 2018-05-23 17:53:07 +0000 | [diff] [blame] | 5219 | assert( pC->isTable ); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 5220 | assert( pC->eCurType==CURTYPE_BTREE ); |
| 5221 | assert( pC->uc.pCursor!=0 ); |
drh | 98ef0f6 | 2015-06-30 01:25:52 +0000 | [diff] [blame] | 5222 | { |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 5223 | /* The next rowid or record number (different terms for the same |
| 5224 | ** thing) is obtained in a two-step algorithm. |
| 5225 | ** |
| 5226 | ** First we attempt to find the largest existing rowid and add one |
| 5227 | ** to that. But if the largest existing rowid is already the maximum |
| 5228 | ** positive integer, we have to fall through to the second |
| 5229 | ** probabilistic algorithm |
| 5230 | ** |
| 5231 | ** The second algorithm is to select a rowid at random and see if |
| 5232 | ** it already exists in the table. If it does not exist, we have |
| 5233 | ** succeeded. If the random rowid does exist, we select a new one |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 5234 | ** and try again, up to 100 times. |
drh | db5ed6d | 2001-09-18 22:17:44 +0000 | [diff] [blame] | 5235 | */ |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 5236 | assert( pC->isTable ); |
drh | fe2093d | 2005-01-20 22:48:47 +0000 | [diff] [blame] | 5237 | |
drh | 75f86a4 | 2005-02-17 00:03:06 +0000 | [diff] [blame] | 5238 | #ifdef SQLITE_32BIT_ROWID |
| 5239 | # define MAX_ROWID 0x7fffffff |
| 5240 | #else |
drh | fe2093d | 2005-01-20 22:48:47 +0000 | [diff] [blame] | 5241 | /* Some compilers complain about constants of the form 0x7fffffffffffffff. |
| 5242 | ** Others complain about 0x7ffffffffffffffffLL. The following macro seems |
| 5243 | ** to provide the constant while making all compilers happy. |
| 5244 | */ |
danielk1977 | 64202cf | 2008-11-17 15:31:47 +0000 | [diff] [blame] | 5245 | # define MAX_ROWID (i64)( (((u64)0x7fffffff)<<32) | (u64)0xffffffff ) |
drh | 75f86a4 | 2005-02-17 00:03:06 +0000 | [diff] [blame] | 5246 | #endif |
drh | fe2093d | 2005-01-20 22:48:47 +0000 | [diff] [blame] | 5247 | |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 5248 | if( !pC->useRandomRowid ){ |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 5249 | rc = sqlite3BtreeLast(pC->uc.pCursor, &res); |
drh | e0670b6 | 2014-02-12 21:31:12 +0000 | [diff] [blame] | 5250 | if( rc!=SQLITE_OK ){ |
| 5251 | goto abort_due_to_error; |
| 5252 | } |
| 5253 | if( res ){ |
| 5254 | v = 1; /* IMP: R-61914-48074 */ |
| 5255 | }else{ |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 5256 | assert( sqlite3BtreeCursorIsValid(pC->uc.pCursor) ); |
drh | a7c90c4 | 2016-06-04 20:37:10 +0000 | [diff] [blame] | 5257 | v = sqlite3BtreeIntegerKey(pC->uc.pCursor); |
drh | e0670b6 | 2014-02-12 21:31:12 +0000 | [diff] [blame] | 5258 | if( v>=MAX_ROWID ){ |
| 5259 | pC->useRandomRowid = 1; |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 5260 | }else{ |
drh | e0670b6 | 2014-02-12 21:31:12 +0000 | [diff] [blame] | 5261 | v++; /* IMP: R-29538-34987 */ |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 5262 | } |
drh | 3fc190c | 2001-09-14 03:24:23 +0000 | [diff] [blame] | 5263 | } |
drh | e0670b6 | 2014-02-12 21:31:12 +0000 | [diff] [blame] | 5264 | } |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 5265 | |
| 5266 | #ifndef SQLITE_OMIT_AUTOINCREMENT |
drh | e0670b6 | 2014-02-12 21:31:12 +0000 | [diff] [blame] | 5267 | if( pOp->p3 ){ |
| 5268 | /* Assert that P3 is a valid memory cell. */ |
| 5269 | assert( pOp->p3>0 ); |
| 5270 | if( p->pFrame ){ |
| 5271 | for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent); |
shane | abc6b89 | 2009-09-10 19:09:03 +0000 | [diff] [blame] | 5272 | /* Assert that P3 is a valid memory cell. */ |
drh | e0670b6 | 2014-02-12 21:31:12 +0000 | [diff] [blame] | 5273 | assert( pOp->p3<=pFrame->nMem ); |
| 5274 | pMem = &pFrame->aMem[pOp->p3]; |
| 5275 | }else{ |
| 5276 | /* Assert that P3 is a valid memory cell. */ |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 5277 | assert( pOp->p3<=(p->nMem+1 - p->nCursor) ); |
drh | e0670b6 | 2014-02-12 21:31:12 +0000 | [diff] [blame] | 5278 | pMem = &aMem[pOp->p3]; |
| 5279 | memAboutToChange(p, pMem); |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 5280 | } |
drh | e0670b6 | 2014-02-12 21:31:12 +0000 | [diff] [blame] | 5281 | assert( memIsValid(pMem) ); |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 5282 | |
drh | e0670b6 | 2014-02-12 21:31:12 +0000 | [diff] [blame] | 5283 | REGISTER_TRACE(pOp->p3, pMem); |
| 5284 | sqlite3VdbeMemIntegerify(pMem); |
| 5285 | assert( (pMem->flags & MEM_Int)!=0 ); /* mem(P3) holds an integer */ |
| 5286 | if( pMem->u.i==MAX_ROWID || pC->useRandomRowid ){ |
drh | e77caa1 | 2016-11-02 13:18:46 +0000 | [diff] [blame] | 5287 | rc = SQLITE_FULL; /* IMP: R-17817-00630 */ |
drh | e0670b6 | 2014-02-12 21:31:12 +0000 | [diff] [blame] | 5288 | goto abort_due_to_error; |
| 5289 | } |
| 5290 | if( v<pMem->u.i+1 ){ |
| 5291 | v = pMem->u.i + 1; |
| 5292 | } |
| 5293 | pMem->u.i = v; |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 5294 | } |
drh | e0670b6 | 2014-02-12 21:31:12 +0000 | [diff] [blame] | 5295 | #endif |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 5296 | if( pC->useRandomRowid ){ |
drh | 748a52c | 2010-09-01 11:50:08 +0000 | [diff] [blame] | 5297 | /* IMPLEMENTATION-OF: R-07677-41881 If the largest ROWID is equal to the |
drh | c79c761 | 2010-01-01 18:57:48 +0000 | [diff] [blame] | 5298 | ** largest possible integer (9223372036854775807) then the database |
drh | 748a52c | 2010-09-01 11:50:08 +0000 | [diff] [blame] | 5299 | ** engine starts picking positive candidate ROWIDs at random until |
| 5300 | ** it finds one that is not previously used. */ |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 5301 | assert( pOp->p3==0 ); /* We cannot be in random rowid mode if this is |
| 5302 | ** an AUTOINCREMENT table. */ |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 5303 | cnt = 0; |
drh | 2c4dc63 | 2014-09-25 12:31:28 +0000 | [diff] [blame] | 5304 | do{ |
| 5305 | sqlite3_randomness(sizeof(v), &v); |
drh | d863346 | 2014-09-25 17:42:41 +0000 | [diff] [blame] | 5306 | v &= (MAX_ROWID>>1); v++; /* Ensure that v is greater than zero */ |
drh | 42a410d | 2021-06-19 18:32:20 +0000 | [diff] [blame] | 5307 | }while( ((rc = sqlite3BtreeTableMoveto(pC->uc.pCursor, (u64)v, |
drh | 748a52c | 2010-09-01 11:50:08 +0000 | [diff] [blame] | 5308 | 0, &res))==SQLITE_OK) |
shaneh | c4d340a | 2010-09-01 02:37:56 +0000 | [diff] [blame] | 5309 | && (res==0) |
drh | 2c4dc63 | 2014-09-25 12:31:28 +0000 | [diff] [blame] | 5310 | && (++cnt<100)); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 5311 | if( rc ) goto abort_due_to_error; |
| 5312 | if( res==0 ){ |
drh | c79c761 | 2010-01-01 18:57:48 +0000 | [diff] [blame] | 5313 | rc = SQLITE_FULL; /* IMP: R-38219-53002 */ |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 5314 | goto abort_due_to_error; |
| 5315 | } |
drh | 748a52c | 2010-09-01 11:50:08 +0000 | [diff] [blame] | 5316 | assert( v>0 ); /* EV: R-40812-03570 */ |
drh | 1eaa269 | 2001-09-18 02:02:23 +0000 | [diff] [blame] | 5317 | } |
drh | a11846b | 2004-01-07 18:52:56 +0000 | [diff] [blame] | 5318 | pC->deferredMoveto = 0; |
drh | 76873ab | 2006-01-07 18:48:26 +0000 | [diff] [blame] | 5319 | pC->cacheStatus = CACHE_STALE; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5320 | } |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 5321 | pOut->u.i = v; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5322 | break; |
| 5323 | } |
| 5324 | |
danielk1977 | 1f4aa33 | 2008-01-03 09:51:55 +0000 | [diff] [blame] | 5325 | /* Opcode: Insert P1 P2 P3 P4 P5 |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 5326 | ** Synopsis: intkey=r[P3] data=r[P2] |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5327 | ** |
jplyon | 5a56422 | 2003-06-02 06:15:58 +0000 | [diff] [blame] | 5328 | ** Write an entry into the table of cursor P1. A new entry is |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 5329 | ** created if it doesn't already exist or the data for an existing |
drh | 3e9ca09 | 2009-09-08 01:14:48 +0000 | [diff] [blame] | 5330 | ** entry is overwritten. The data is the value MEM_Blob stored in register |
danielk1977 | 1f4aa33 | 2008-01-03 09:51:55 +0000 | [diff] [blame] | 5331 | ** number P2. The key is stored in register P3. The key must |
drh | 3e9ca09 | 2009-09-08 01:14:48 +0000 | [diff] [blame] | 5332 | ** be a MEM_Int. |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 5333 | ** |
danielk1977 | 1f4aa33 | 2008-01-03 09:51:55 +0000 | [diff] [blame] | 5334 | ** If the OPFLAG_NCHANGE flag of P5 is set, then the row change count is |
| 5335 | ** incremented (otherwise not). If the OPFLAG_LASTROWID flag of P5 is set, |
danielk1977 | b28af71 | 2004-06-21 06:50:26 +0000 | [diff] [blame] | 5336 | ** then rowid is stored for subsequent return by the |
drh | 85b623f | 2007-12-13 21:54:09 +0000 | [diff] [blame] | 5337 | ** sqlite3_last_insert_rowid() function (otherwise it is unmodified). |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 5338 | ** |
drh | eaf6ae2 | 2016-11-09 20:14:34 +0000 | [diff] [blame] | 5339 | ** If the OPFLAG_USESEEKRESULT flag of P5 is set, the implementation might |
| 5340 | ** run faster by avoiding an unnecessary seek on cursor P1. However, |
| 5341 | ** the OPFLAG_USESEEKRESULT flag must only be set if there have been no prior |
| 5342 | ** 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] | 5343 | ** |
| 5344 | ** If the OPFLAG_ISUPDATE flag is set, then this opcode is part of an |
| 5345 | ** UPDATE operation. Otherwise (if the flag is clear) then this opcode |
| 5346 | ** is part of an INSERT operation. The difference is only important to |
| 5347 | ** the update hook. |
| 5348 | ** |
dan | 319eeb7 | 2011-03-19 08:38:50 +0000 | [diff] [blame] | 5349 | ** Parameter P4 may point to a Table structure, or may be NULL. If it is |
| 5350 | ** not NULL, then the update-hook (sqlite3.xUpdateCallback) is invoked |
| 5351 | ** following a successful insert. |
danielk1977 | 1f6eec5 | 2006-06-16 06:17:47 +0000 | [diff] [blame] | 5352 | ** |
drh | 93aed5a | 2008-01-16 17:46:38 +0000 | [diff] [blame] | 5353 | ** (WARNING/TODO: If P1 is a pseudo-cursor and P2 is dynamically |
| 5354 | ** allocated, then ownership of P2 is transferred to the pseudo-cursor |
| 5355 | ** and register P2 becomes ephemeral. If the cursor is changed, the |
| 5356 | ** value of register P2 will then change. Make sure this does not |
| 5357 | ** cause any problems.) |
| 5358 | ** |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 5359 | ** This instruction only works on tables. The equivalent instruction |
| 5360 | ** for indices is OP_IdxInsert. |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 5361 | */ |
drh | 50ef671 | 2019-02-22 23:29:56 +0000 | [diff] [blame] | 5362 | case OP_Insert: { |
drh | 3e9ca09 | 2009-09-08 01:14:48 +0000 | [diff] [blame] | 5363 | Mem *pData; /* MEM cell holding data for the record to be inserted */ |
| 5364 | Mem *pKey; /* MEM cell holding key for the record */ |
drh | 3e9ca09 | 2009-09-08 01:14:48 +0000 | [diff] [blame] | 5365 | VdbeCursor *pC; /* Cursor to table into which insert is written */ |
drh | 3e9ca09 | 2009-09-08 01:14:48 +0000 | [diff] [blame] | 5366 | int seekResult; /* Result of prior seek or 0 if no USESEEKRESULT flag */ |
| 5367 | const char *zDb; /* database name - used by the update hook */ |
dan | 319eeb7 | 2011-03-19 08:38:50 +0000 | [diff] [blame] | 5368 | Table *pTab; /* Table structure - used by update and pre-update hooks */ |
drh | 8eeb446 | 2016-05-21 20:03:42 +0000 | [diff] [blame] | 5369 | BtreePayload x; /* Payload to be inserted */ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 5370 | |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 5371 | pData = &aMem[pOp->p2]; |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 5372 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 5373 | assert( memIsValid(pData) ); |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 5374 | pC = p->apCsr[pOp->p1]; |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 5375 | assert( pC!=0 ); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 5376 | assert( pC->eCurType==CURTYPE_BTREE ); |
drh | be3da24 | 2019-12-29 00:52:41 +0000 | [diff] [blame] | 5377 | assert( pC->deferredMoveto==0 ); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 5378 | assert( pC->uc.pCursor!=0 ); |
dan | cb9a364 | 2017-01-30 19:44:53 +0000 | [diff] [blame] | 5379 | assert( (pOp->p5 & OPFLAG_ISNOOP) || pC->isTable ); |
drh | cbf1b8e | 2013-11-11 22:55:26 +0000 | [diff] [blame] | 5380 | assert( pOp->p4type==P4_TABLE || pOp->p4type>=P4_STATIC ); |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 5381 | REGISTER_TRACE(pOp->p2, pData); |
drh | 4031baf | 2018-05-28 17:31:20 +0000 | [diff] [blame] | 5382 | sqlite3VdbeIncrWriteCounter(p, pC); |
danielk1977 | 5f8d8a8 | 2004-05-11 00:28:42 +0000 | [diff] [blame] | 5383 | |
drh | 50ef671 | 2019-02-22 23:29:56 +0000 | [diff] [blame] | 5384 | pKey = &aMem[pOp->p3]; |
| 5385 | assert( pKey->flags & MEM_Int ); |
| 5386 | assert( memIsValid(pKey) ); |
| 5387 | REGISTER_TRACE(pOp->p3, pKey); |
| 5388 | x.nKey = pKey->u.i; |
drh | e05c929 | 2009-10-29 13:48:10 +0000 | [diff] [blame] | 5389 | |
drh | 9b1c62d | 2011-03-30 21:04:43 +0000 | [diff] [blame] | 5390 | if( pOp->p4type==P4_TABLE && HAS_UPDATE_HOOK(db) ){ |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 5391 | assert( pC->iDb>=0 ); |
drh | 69c3382 | 2016-08-18 14:33:11 +0000 | [diff] [blame] | 5392 | zDb = db->aDb[pC->iDb].zDbSName; |
dan | 319eeb7 | 2011-03-19 08:38:50 +0000 | [diff] [blame] | 5393 | pTab = pOp->p4.pTab; |
dan | cb9a364 | 2017-01-30 19:44:53 +0000 | [diff] [blame] | 5394 | assert( (pOp->p5 & OPFLAG_ISNOOP) || HasRowid(pTab) ); |
drh | 74c3302 | 2016-03-30 12:56:55 +0000 | [diff] [blame] | 5395 | }else{ |
drh | 4ec6f3a | 2018-01-12 19:33:18 +0000 | [diff] [blame] | 5396 | pTab = 0; |
drh | e1e1a43 | 2021-10-05 11:11:43 +0000 | [diff] [blame] | 5397 | zDb = 0; |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 5398 | } |
| 5399 | |
drh | 9b1c62d | 2011-03-30 21:04:43 +0000 | [diff] [blame] | 5400 | #ifdef SQLITE_ENABLE_PREUPDATE_HOOK |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 5401 | /* Invoke the pre-update hook, if any */ |
drh | 4ec6f3a | 2018-01-12 19:33:18 +0000 | [diff] [blame] | 5402 | if( pTab ){ |
drh | 84ebe2b | 2018-01-12 18:46:52 +0000 | [diff] [blame] | 5403 | if( db->xPreUpdateCallback && !(pOp->p5 & OPFLAG_ISUPDATE) ){ |
dan | a23a873 | 2021-04-21 20:52:17 +0000 | [diff] [blame] | 5404 | sqlite3VdbePreUpdateHook(p,pC,SQLITE_INSERT,zDb,pTab,x.nKey,pOp->p2,-1); |
drh | 84ebe2b | 2018-01-12 18:46:52 +0000 | [diff] [blame] | 5405 | } |
drh | 4ec6f3a | 2018-01-12 19:33:18 +0000 | [diff] [blame] | 5406 | if( db->xUpdateCallback==0 || pTab->aCol==0 ){ |
| 5407 | /* Prevent post-update hook from running in cases when it should not */ |
| 5408 | pTab = 0; |
drh | 84ebe2b | 2018-01-12 18:46:52 +0000 | [diff] [blame] | 5409 | } |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 5410 | } |
dan | cb9a364 | 2017-01-30 19:44:53 +0000 | [diff] [blame] | 5411 | if( pOp->p5 & OPFLAG_ISNOOP ) break; |
drh | 9b1c62d | 2011-03-30 21:04:43 +0000 | [diff] [blame] | 5412 | #endif |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 5413 | |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 5414 | if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++; |
drh | fae58d5 | 2017-01-26 17:26:44 +0000 | [diff] [blame] | 5415 | if( pOp->p5 & OPFLAG_LASTROWID ) db->lastRowid = x.nKey; |
drh | 32881be | 2020-11-17 21:26:13 +0000 | [diff] [blame] | 5416 | assert( (pData->flags & (MEM_Blob|MEM_Str))!=0 || pData->n==0 ); |
dan | 21cd29a | 2017-10-23 16:03:54 +0000 | [diff] [blame] | 5417 | x.pData = pData->z; |
| 5418 | x.nData = pData->n; |
drh | 3e9ca09 | 2009-09-08 01:14:48 +0000 | [diff] [blame] | 5419 | seekResult = ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0); |
| 5420 | if( pData->flags & MEM_Zero ){ |
drh | 8eeb446 | 2016-05-21 20:03:42 +0000 | [diff] [blame] | 5421 | x.nZero = pData->u.nZero; |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 5422 | }else{ |
drh | 8eeb446 | 2016-05-21 20:03:42 +0000 | [diff] [blame] | 5423 | x.nZero = 0; |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 5424 | } |
drh | 8eeb446 | 2016-05-21 20:03:42 +0000 | [diff] [blame] | 5425 | x.pKey = 0; |
| 5426 | rc = sqlite3BtreeInsert(pC->uc.pCursor, &x, |
dan | 7aae735 | 2020-12-10 18:06:24 +0000 | [diff] [blame] | 5427 | (pOp->p5 & (OPFLAG_APPEND|OPFLAG_SAVEPOSITION|OPFLAG_PREFORMAT)), |
| 5428 | seekResult |
drh | 3e9ca09 | 2009-09-08 01:14:48 +0000 | [diff] [blame] | 5429 | ); |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 5430 | pC->deferredMoveto = 0; |
| 5431 | pC->cacheStatus = CACHE_STALE; |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 +0000 | [diff] [blame] | 5432 | |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 5433 | /* Invoke the update-hook if required. */ |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 5434 | if( rc ) goto abort_due_to_error; |
drh | 4ec6f3a | 2018-01-12 19:33:18 +0000 | [diff] [blame] | 5435 | if( pTab ){ |
| 5436 | assert( db->xUpdateCallback!=0 ); |
| 5437 | assert( pTab->aCol!=0 ); |
| 5438 | db->xUpdateCallback(db->pUpdateArg, |
| 5439 | (pOp->p5 & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_INSERT, |
| 5440 | zDb, pTab->zName, x.nKey); |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 5441 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5442 | break; |
| 5443 | } |
| 5444 | |
dan | 7aae735 | 2020-12-10 18:06:24 +0000 | [diff] [blame] | 5445 | /* Opcode: RowCell P1 P2 P3 * * |
dan | d2ffc97 | 2020-12-10 19:20:15 +0000 | [diff] [blame] | 5446 | ** |
| 5447 | ** P1 and P2 are both open cursors. Both must be opened on the same type |
| 5448 | ** of table - intkey or index. This opcode is used as part of copying |
| 5449 | ** the current row from P2 into P1. If the cursors are opened on intkey |
| 5450 | ** tables, register P3 contains the rowid to use with the new record in |
| 5451 | ** P1. If they are opened on index tables, P3 is not used. |
| 5452 | ** |
| 5453 | ** This opcode must be followed by either an Insert or InsertIdx opcode |
| 5454 | ** with the OPFLAG_PREFORMAT flag set to complete the insert operation. |
dan | 036e067 | 2020-12-08 20:19:07 +0000 | [diff] [blame] | 5455 | */ |
dan | 7aae735 | 2020-12-10 18:06:24 +0000 | [diff] [blame] | 5456 | case OP_RowCell: { |
dan | 036e067 | 2020-12-08 20:19:07 +0000 | [diff] [blame] | 5457 | VdbeCursor *pDest; /* Cursor to write to */ |
| 5458 | VdbeCursor *pSrc; /* Cursor to read from */ |
| 5459 | i64 iKey; /* Rowid value to insert with */ |
dan | 7aae735 | 2020-12-10 18:06:24 +0000 | [diff] [blame] | 5460 | assert( pOp[1].opcode==OP_Insert || pOp[1].opcode==OP_IdxInsert ); |
drh | a06eafc | 2020-12-29 15:06:26 +0000 | [diff] [blame] | 5461 | assert( pOp[1].opcode==OP_Insert || pOp->p3==0 ); |
| 5462 | assert( pOp[1].opcode==OP_IdxInsert || pOp->p3>0 ); |
dan | d2ffc97 | 2020-12-10 19:20:15 +0000 | [diff] [blame] | 5463 | assert( pOp[1].p5 & OPFLAG_PREFORMAT ); |
dan | 036e067 | 2020-12-08 20:19:07 +0000 | [diff] [blame] | 5464 | pDest = p->apCsr[pOp->p1]; |
| 5465 | pSrc = p->apCsr[pOp->p2]; |
dan | cd1b2d0 | 2020-12-09 20:33:51 +0000 | [diff] [blame] | 5466 | iKey = pOp->p3 ? aMem[pOp->p3].u.i : 0; |
dan | 7aae735 | 2020-12-10 18:06:24 +0000 | [diff] [blame] | 5467 | rc = sqlite3BtreeTransferRow(pDest->uc.pCursor, pSrc->uc.pCursor, iKey); |
dan | 036e067 | 2020-12-08 20:19:07 +0000 | [diff] [blame] | 5468 | if( rc!=SQLITE_OK ) goto abort_due_to_error; |
| 5469 | break; |
dan | 7aae735 | 2020-12-10 18:06:24 +0000 | [diff] [blame] | 5470 | }; |
dan | 036e067 | 2020-12-08 20:19:07 +0000 | [diff] [blame] | 5471 | |
dan | 438b881 | 2015-09-15 15:55:15 +0000 | [diff] [blame] | 5472 | /* Opcode: Delete P1 P2 P3 P4 P5 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5473 | ** |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 5474 | ** Delete the record at which the P1 cursor is currently pointing. |
| 5475 | ** |
drh | e807bdb | 2016-01-21 17:06:33 +0000 | [diff] [blame] | 5476 | ** If the OPFLAG_SAVEPOSITION bit of the P5 parameter is set, then |
| 5477 | ** the cursor will be left pointing at either the next or the previous |
| 5478 | ** record in the table. If it is left pointing at the next record, then |
| 5479 | ** the next Next instruction will be a no-op. As a result, in this case |
| 5480 | ** it is ok to delete a record from within a Next loop. If |
| 5481 | ** OPFLAG_SAVEPOSITION bit of P5 is clear, then the cursor will be |
| 5482 | ** left in an undefined state. |
drh | c8d30ac | 2002-04-12 10:08:59 +0000 | [diff] [blame] | 5483 | ** |
drh | def19e3 | 2016-01-27 16:26:25 +0000 | [diff] [blame] | 5484 | ** If the OPFLAG_AUXDELETE bit is set on P5, that indicates that this |
| 5485 | ** delete one of several associated with deleting a table row and all its |
| 5486 | ** associated index entries. Exactly one of those deletes is the "primary" |
| 5487 | ** delete. The others are all on OPFLAG_FORDELETE cursors or else are |
| 5488 | ** marked with the AUXDELETE flag. |
drh | e807bdb | 2016-01-21 17:06:33 +0000 | [diff] [blame] | 5489 | ** |
| 5490 | ** If the OPFLAG_NCHANGE flag of P2 (NB: P2 not P5) is set, then the row |
| 5491 | ** change count is incremented (otherwise not). |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 5492 | ** |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 5493 | ** P1 must not be pseudo-table. It has to be a real table with |
| 5494 | ** multiple rows. |
| 5495 | ** |
drh | 5e769a5 | 2016-09-28 16:05:53 +0000 | [diff] [blame] | 5496 | ** 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] | 5497 | ** the update or pre-update hook, or both, may be invoked. The P1 cursor must |
| 5498 | ** have been positioned using OP_NotFound prior to invoking this opcode in |
| 5499 | ** this case. Specifically, if one is configured, the pre-update hook is |
| 5500 | ** invoked if P4 is not NULL. The update-hook is invoked if one is configured, |
| 5501 | ** P4 is not NULL, and the OPFLAG_NCHANGE flag is set in P2. |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 5502 | ** |
| 5503 | ** If the OPFLAG_ISUPDATE flag is set in P2, then P3 contains the address |
| 5504 | ** of the memory cell that contains the value that the rowid of the row will |
| 5505 | ** be set to by the update. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5506 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 5507 | case OP_Delete: { |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 5508 | VdbeCursor *pC; |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 5509 | const char *zDb; |
dan | 319eeb7 | 2011-03-19 08:38:50 +0000 | [diff] [blame] | 5510 | Table *pTab; |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 5511 | int opflags; |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 5512 | |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 5513 | opflags = pOp->p2; |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 5514 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 5515 | pC = p->apCsr[pOp->p1]; |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 5516 | assert( pC!=0 ); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 5517 | assert( pC->eCurType==CURTYPE_BTREE ); |
| 5518 | assert( pC->uc.pCursor!=0 ); |
drh | 9a65f2c | 2009-06-22 19:05:40 +0000 | [diff] [blame] | 5519 | assert( pC->deferredMoveto==0 ); |
drh | 4031baf | 2018-05-28 17:31:20 +0000 | [diff] [blame] | 5520 | sqlite3VdbeIncrWriteCounter(p, pC); |
drh | 9a65f2c | 2009-06-22 19:05:40 +0000 | [diff] [blame] | 5521 | |
drh | b53a5a9 | 2014-10-12 22:37:22 +0000 | [diff] [blame] | 5522 | #ifdef SQLITE_DEBUG |
drh | 6b559f3 | 2020-01-02 19:50:50 +0000 | [diff] [blame] | 5523 | if( pOp->p4type==P4_TABLE |
| 5524 | && HasRowid(pOp->p4.pTab) |
| 5525 | && pOp->p5==0 |
| 5526 | && sqlite3BtreeCursorIsValidNN(pC->uc.pCursor) |
| 5527 | ){ |
dan | 438b881 | 2015-09-15 15:55:15 +0000 | [diff] [blame] | 5528 | /* If p5 is zero, the seek operation that positioned the cursor prior to |
| 5529 | ** OP_Delete will have also set the pC->movetoTarget field to the rowid of |
| 5530 | ** the row that is being deleted */ |
drh | a7c90c4 | 2016-06-04 20:37:10 +0000 | [diff] [blame] | 5531 | i64 iKey = sqlite3BtreeIntegerKey(pC->uc.pCursor); |
dan | 0971ef4 | 2019-05-16 20:13:32 +0000 | [diff] [blame] | 5532 | assert( CORRUPT_DB || pC->movetoTarget==iKey ); |
drh | b53a5a9 | 2014-10-12 22:37:22 +0000 | [diff] [blame] | 5533 | } |
| 5534 | #endif |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 5535 | |
dan | 438b881 | 2015-09-15 15:55:15 +0000 | [diff] [blame] | 5536 | /* If the update-hook or pre-update-hook will be invoked, set zDb to |
| 5537 | ** the name of the db to pass as to it. Also set local pTab to a copy |
| 5538 | ** of p4.pTab. Finally, if p5 is true, indicating that this cursor was |
| 5539 | ** last moved with OP_Next or OP_Prev, not Seek or NotFound, set |
| 5540 | ** VdbeCursor.movetoTarget to the current rowid. */ |
drh | c556f3c | 2016-03-30 15:30:07 +0000 | [diff] [blame] | 5541 | if( pOp->p4type==P4_TABLE && HAS_UPDATE_HOOK(db) ){ |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 5542 | assert( pC->iDb>=0 ); |
drh | c556f3c | 2016-03-30 15:30:07 +0000 | [diff] [blame] | 5543 | assert( pOp->p4.pTab!=0 ); |
drh | 69c3382 | 2016-08-18 14:33:11 +0000 | [diff] [blame] | 5544 | zDb = db->aDb[pC->iDb].zDbSName; |
dan | 319eeb7 | 2011-03-19 08:38:50 +0000 | [diff] [blame] | 5545 | pTab = pOp->p4.pTab; |
drh | c556f3c | 2016-03-30 15:30:07 +0000 | [diff] [blame] | 5546 | if( (pOp->p5 & OPFLAG_SAVEPOSITION)!=0 && pC->isTable ){ |
drh | a7c90c4 | 2016-06-04 20:37:10 +0000 | [diff] [blame] | 5547 | pC->movetoTarget = sqlite3BtreeIntegerKey(pC->uc.pCursor); |
dan | 438b881 | 2015-09-15 15:55:15 +0000 | [diff] [blame] | 5548 | } |
drh | 74c3302 | 2016-03-30 12:56:55 +0000 | [diff] [blame] | 5549 | }else{ |
drh | e1e1a43 | 2021-10-05 11:11:43 +0000 | [diff] [blame] | 5550 | zDb = 0; |
| 5551 | pTab = 0; |
drh | 92fe38e | 2014-10-14 13:41:32 +0000 | [diff] [blame] | 5552 | } |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 5553 | |
drh | 9b1c62d | 2011-03-30 21:04:43 +0000 | [diff] [blame] | 5554 | #ifdef SQLITE_ENABLE_PREUPDATE_HOOK |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 5555 | /* Invoke the pre-update-hook if required. */ |
drh | e1e1a43 | 2021-10-05 11:11:43 +0000 | [diff] [blame] | 5556 | assert( db->xPreUpdateCallback==0 || pTab==pOp->p4.pTab ); |
| 5557 | if( db->xPreUpdateCallback && pTab ){ |
dan | cb9a364 | 2017-01-30 19:44:53 +0000 | [diff] [blame] | 5558 | assert( !(opflags & OPFLAG_ISUPDATE) |
| 5559 | || HasRowid(pTab)==0 |
| 5560 | || (aMem[pOp->p3].flags & MEM_Int) |
| 5561 | ); |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 5562 | sqlite3VdbePreUpdateHook(p, pC, |
| 5563 | (opflags & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_DELETE, |
drh | 92fe38e | 2014-10-14 13:41:32 +0000 | [diff] [blame] | 5564 | zDb, pTab, pC->movetoTarget, |
dan | a23a873 | 2021-04-21 20:52:17 +0000 | [diff] [blame] | 5565 | pOp->p3, -1 |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 5566 | ); |
| 5567 | } |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 5568 | if( opflags & OPFLAG_ISNOOP ) break; |
drh | c556f3c | 2016-03-30 15:30:07 +0000 | [diff] [blame] | 5569 | #endif |
drh | b53a5a9 | 2014-10-12 22:37:22 +0000 | [diff] [blame] | 5570 | |
drh | def19e3 | 2016-01-27 16:26:25 +0000 | [diff] [blame] | 5571 | /* Only flags that can be set are SAVEPOISTION and AUXDELETE */ |
| 5572 | assert( (pOp->p5 & ~(OPFLAG_SAVEPOSITION|OPFLAG_AUXDELETE))==0 ); |
drh | e807bdb | 2016-01-21 17:06:33 +0000 | [diff] [blame] | 5573 | assert( OPFLAG_SAVEPOSITION==BTREE_SAVEPOSITION ); |
drh | def19e3 | 2016-01-27 16:26:25 +0000 | [diff] [blame] | 5574 | assert( OPFLAG_AUXDELETE==BTREE_AUXDELETE ); |
drh | b89aeb6 | 2016-01-27 15:49:32 +0000 | [diff] [blame] | 5575 | |
| 5576 | #ifdef SQLITE_DEBUG |
dan | e61bbf4 | 2016-01-28 17:06:17 +0000 | [diff] [blame] | 5577 | if( p->pFrame==0 ){ |
| 5578 | if( pC->isEphemeral==0 |
| 5579 | && (pOp->p5 & OPFLAG_AUXDELETE)==0 |
| 5580 | && (pC->wrFlag & OPFLAG_FORDELETE)==0 |
| 5581 | ){ |
| 5582 | nExtraDelete++; |
| 5583 | } |
| 5584 | if( pOp->p2 & OPFLAG_NCHANGE ){ |
| 5585 | nExtraDelete--; |
| 5586 | } |
drh | b89aeb6 | 2016-01-27 15:49:32 +0000 | [diff] [blame] | 5587 | } |
| 5588 | #endif |
| 5589 | |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 5590 | rc = sqlite3BtreeDelete(pC->uc.pCursor, pOp->p5); |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 5591 | pC->cacheStatus = CACHE_STALE; |
dan | 3b908d4 | 2016-11-08 19:22:32 +0000 | [diff] [blame] | 5592 | pC->seekResult = 0; |
drh | d3e1af4 | 2016-02-25 18:54:30 +0000 | [diff] [blame] | 5593 | if( rc ) goto abort_due_to_error; |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 +0000 | [diff] [blame] | 5594 | |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 5595 | /* Invoke the update-hook if required. */ |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 5596 | if( opflags & OPFLAG_NCHANGE ){ |
| 5597 | p->nChange++; |
drh | 7d4c94b | 2021-10-04 22:34:38 +0000 | [diff] [blame] | 5598 | if( db->xUpdateCallback && ALWAYS(pTab!=0) && HasRowid(pTab) ){ |
drh | 92fe38e | 2014-10-14 13:41:32 +0000 | [diff] [blame] | 5599 | db->xUpdateCallback(db->pUpdateArg, SQLITE_DELETE, zDb, pTab->zName, |
dan | 438b881 | 2015-09-15 15:55:15 +0000 | [diff] [blame] | 5600 | pC->movetoTarget); |
| 5601 | assert( pC->iDb>=0 ); |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 5602 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5603 | } |
dan | 438b881 | 2015-09-15 15:55:15 +0000 | [diff] [blame] | 5604 | |
rdc | b0c374f | 2004-02-20 22:53:38 +0000 | [diff] [blame] | 5605 | break; |
| 5606 | } |
drh | b7f1d9a | 2009-09-08 02:27:58 +0000 | [diff] [blame] | 5607 | /* Opcode: ResetCount * * * * * |
rdc | b0c374f | 2004-02-20 22:53:38 +0000 | [diff] [blame] | 5608 | ** |
drh | b7f1d9a | 2009-09-08 02:27:58 +0000 | [diff] [blame] | 5609 | ** The value of the change counter is copied to the database handle |
| 5610 | ** change counter (returned by subsequent calls to sqlite3_changes()). |
| 5611 | ** Then the VMs internal change counter resets to 0. |
| 5612 | ** This is used by trigger programs. |
rdc | b0c374f | 2004-02-20 22:53:38 +0000 | [diff] [blame] | 5613 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 5614 | case OP_ResetCount: { |
drh | b7f1d9a | 2009-09-08 02:27:58 +0000 | [diff] [blame] | 5615 | sqlite3VdbeSetChanges(db, p->nChange); |
danielk1977 | b28af71 | 2004-06-21 06:50:26 +0000 | [diff] [blame] | 5616 | p->nChange = 0; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5617 | break; |
| 5618 | } |
| 5619 | |
drh | 1153c7b | 2013-11-01 22:02:56 +0000 | [diff] [blame] | 5620 | /* Opcode: SorterCompare P1 P2 P3 P4 |
drh | 72e26de | 2016-08-24 21:24:04 +0000 | [diff] [blame] | 5621 | ** Synopsis: if key(P1)!=trim(r[P3],P4) goto P2 |
dan | 5134d13 | 2011-09-02 10:31:11 +0000 | [diff] [blame] | 5622 | ** |
drh | 1153c7b | 2013-11-01 22:02:56 +0000 | [diff] [blame] | 5623 | ** P1 is a sorter cursor. This instruction compares a prefix of the |
drh | bc5cf38 | 2014-08-06 01:08:07 +0000 | [diff] [blame] | 5624 | ** record blob in register P3 against a prefix of the entry that |
drh | ac50232 | 2014-07-30 13:56:48 +0000 | [diff] [blame] | 5625 | ** the sorter cursor currently points to. Only the first P4 fields |
| 5626 | ** of r[P3] and the sorter record are compared. |
drh | 1153c7b | 2013-11-01 22:02:56 +0000 | [diff] [blame] | 5627 | ** |
| 5628 | ** If either P3 or the sorter contains a NULL in one of their significant |
| 5629 | ** fields (not counting the P4 fields at the end which are ignored) then |
| 5630 | ** the comparison is assumed to be equal. |
| 5631 | ** |
| 5632 | ** Fall through to next instruction if the two records compare equal to |
| 5633 | ** each other. Jump to P2 if they are different. |
dan | 5134d13 | 2011-09-02 10:31:11 +0000 | [diff] [blame] | 5634 | */ |
| 5635 | case OP_SorterCompare: { |
| 5636 | VdbeCursor *pC; |
| 5637 | int res; |
drh | ac50232 | 2014-07-30 13:56:48 +0000 | [diff] [blame] | 5638 | int nKeyCol; |
dan | 5134d13 | 2011-09-02 10:31:11 +0000 | [diff] [blame] | 5639 | |
| 5640 | pC = p->apCsr[pOp->p1]; |
| 5641 | assert( isSorter(pC) ); |
drh | 1153c7b | 2013-11-01 22:02:56 +0000 | [diff] [blame] | 5642 | assert( pOp->p4type==P4_INT32 ); |
dan | 5134d13 | 2011-09-02 10:31:11 +0000 | [diff] [blame] | 5643 | pIn3 = &aMem[pOp->p3]; |
drh | ac50232 | 2014-07-30 13:56:48 +0000 | [diff] [blame] | 5644 | nKeyCol = pOp->p4.i; |
drh | 958d261 | 2014-04-18 13:40:07 +0000 | [diff] [blame] | 5645 | res = 0; |
drh | ac50232 | 2014-07-30 13:56:48 +0000 | [diff] [blame] | 5646 | rc = sqlite3VdbeSorterCompare(pC, pIn3, nKeyCol, &res); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 5647 | VdbeBranchTaken(res!=0,2); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 5648 | if( rc ) goto abort_due_to_error; |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 5649 | if( res ) goto jump_to_p2; |
dan | 5134d13 | 2011-09-02 10:31:11 +0000 | [diff] [blame] | 5650 | break; |
| 5651 | }; |
| 5652 | |
drh | 6cf4a7d | 2014-10-13 13:00:58 +0000 | [diff] [blame] | 5653 | /* Opcode: SorterData P1 P2 P3 * * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 5654 | ** Synopsis: r[P2]=data |
dan | 5134d13 | 2011-09-02 10:31:11 +0000 | [diff] [blame] | 5655 | ** |
| 5656 | ** Write into register P2 the current sorter data for sorter cursor P1. |
drh | 6cf4a7d | 2014-10-13 13:00:58 +0000 | [diff] [blame] | 5657 | ** Then clear the column header cache on cursor P3. |
| 5658 | ** |
| 5659 | ** This opcode is normally use to move a record out of the sorter and into |
| 5660 | ** a register that is the source for a pseudo-table cursor created using |
| 5661 | ** OpenPseudo. That pseudo-table cursor is the one that is identified by |
| 5662 | ** parameter P3. Clearing the P3 column cache as part of this opcode saves |
| 5663 | ** us from having to issue a separate NullRow instruction to clear that cache. |
dan | 5134d13 | 2011-09-02 10:31:11 +0000 | [diff] [blame] | 5664 | */ |
| 5665 | case OP_SorterData: { |
| 5666 | VdbeCursor *pC; |
drh | 3a94987 | 2012-09-18 13:20:13 +0000 | [diff] [blame] | 5667 | |
dan | 5134d13 | 2011-09-02 10:31:11 +0000 | [diff] [blame] | 5668 | pOut = &aMem[pOp->p2]; |
| 5669 | pC = p->apCsr[pOp->p1]; |
drh | 14da87f | 2013-11-20 21:51:33 +0000 | [diff] [blame] | 5670 | assert( isSorter(pC) ); |
dan | 5134d13 | 2011-09-02 10:31:11 +0000 | [diff] [blame] | 5671 | rc = sqlite3VdbeSorterRowkey(pC, pOut); |
dan | 3852413 | 2014-05-01 20:26:48 +0000 | [diff] [blame] | 5672 | assert( rc!=SQLITE_OK || (pOut->flags & MEM_Blob) ); |
drh | 6cf4a7d | 2014-10-13 13:00:58 +0000 | [diff] [blame] | 5673 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 5674 | if( rc ) goto abort_due_to_error; |
drh | 6cf4a7d | 2014-10-13 13:00:58 +0000 | [diff] [blame] | 5675 | p->apCsr[pOp->p3]->cacheStatus = CACHE_STALE; |
dan | 5134d13 | 2011-09-02 10:31:11 +0000 | [diff] [blame] | 5676 | break; |
| 5677 | } |
| 5678 | |
drh | e7b554d | 2017-01-09 15:44:25 +0000 | [diff] [blame] | 5679 | /* Opcode: RowData P1 P2 P3 * * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 5680 | ** Synopsis: r[P2]=data |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 5681 | ** |
drh | 9057fc7 | 2016-11-25 19:32:32 +0000 | [diff] [blame] | 5682 | ** Write into register P2 the complete row content for the row at |
| 5683 | ** which cursor P1 is currently pointing. |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 5684 | ** There is no interpretation of the data. |
| 5685 | ** It is just copied onto the P2 register exactly as |
danielk1977 | 96cb76f | 2008-01-04 13:24:28 +0000 | [diff] [blame] | 5686 | ** it is found in the database file. |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 5687 | ** |
drh | 9057fc7 | 2016-11-25 19:32:32 +0000 | [diff] [blame] | 5688 | ** If cursor P1 is an index, then the content is the key of the row. |
| 5689 | ** If cursor P2 is a table, then the content extracted is the data. |
drh | 143f3c4 | 2004-01-07 20:37:52 +0000 | [diff] [blame] | 5690 | ** |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 5691 | ** If the P1 cursor must be pointing to a valid row (not a NULL row) |
| 5692 | ** of a real table, not a pseudo-table. |
drh | e7b554d | 2017-01-09 15:44:25 +0000 | [diff] [blame] | 5693 | ** |
drh | 8cdafc3 | 2018-05-31 19:00:20 +0000 | [diff] [blame] | 5694 | ** If P3!=0 then this opcode is allowed to make an ephemeral pointer |
drh | e7b554d | 2017-01-09 15:44:25 +0000 | [diff] [blame] | 5695 | ** into the database page. That means that the content of the output |
| 5696 | ** register will be invalidated as soon as the cursor moves - including |
drh | 416a801 | 2018-05-31 19:14:52 +0000 | [diff] [blame] | 5697 | ** moves caused by other cursors that "save" the current cursors |
drh | e7b554d | 2017-01-09 15:44:25 +0000 | [diff] [blame] | 5698 | ** position in order that they can write to the same table. If P3==0 |
| 5699 | ** then a copy of the data is made into memory. P3!=0 is faster, but |
| 5700 | ** P3==0 is safer. |
| 5701 | ** |
| 5702 | ** If P3!=0 then the content of the P2 register is unsuitable for use |
| 5703 | ** in OP_Result and any OP_Result will invalidate the P2 register content. |
mistachkin | ab61cf7 | 2017-01-09 18:22:54 +0000 | [diff] [blame] | 5704 | ** The P2 register content is invalidated by opcodes like OP_Function or |
drh | e7b554d | 2017-01-09 15:44:25 +0000 | [diff] [blame] | 5705 | ** by any use of another cursor pointing to the same table. |
drh | 143f3c4 | 2004-01-07 20:37:52 +0000 | [diff] [blame] | 5706 | */ |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 5707 | case OP_RowData: { |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 5708 | VdbeCursor *pC; |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 5709 | BtCursor *pCrsr; |
danielk1977 | e0d4b06 | 2004-06-28 01:11:46 +0000 | [diff] [blame] | 5710 | u32 n; |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 5711 | |
drh | e7b554d | 2017-01-09 15:44:25 +0000 | [diff] [blame] | 5712 | pOut = out2Prerelease(p, pOp); |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 5713 | |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 5714 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 5715 | pC = p->apCsr[pOp->p1]; |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 5716 | assert( pC!=0 ); |
| 5717 | assert( pC->eCurType==CURTYPE_BTREE ); |
drh | 14da87f | 2013-11-20 21:51:33 +0000 | [diff] [blame] | 5718 | assert( isSorter(pC)==0 ); |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 5719 | assert( pC->nullRow==0 ); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 5720 | assert( pC->uc.pCursor!=0 ); |
| 5721 | pCrsr = pC->uc.pCursor; |
drh | 9a65f2c | 2009-06-22 19:05:40 +0000 | [diff] [blame] | 5722 | |
drh | 9057fc7 | 2016-11-25 19:32:32 +0000 | [diff] [blame] | 5723 | /* The OP_RowData opcodes always follow OP_NotExists or |
drh | eeb9565 | 2016-05-26 20:56:38 +0000 | [diff] [blame] | 5724 | ** OP_SeekRowid or OP_Rewind/Op_Next with no intervening instructions |
| 5725 | ** that might invalidate the cursor. |
| 5726 | ** If this where not the case, on of the following assert()s |
drh | c22284f | 2014-10-13 16:02:20 +0000 | [diff] [blame] | 5727 | ** would fail. Should this ever change (because of changes in the code |
| 5728 | ** generator) then the fix would be to insert a call to |
| 5729 | ** sqlite3VdbeCursorMoveto(). |
drh | 9a65f2c | 2009-06-22 19:05:40 +0000 | [diff] [blame] | 5730 | */ |
| 5731 | assert( pC->deferredMoveto==0 ); |
drh | c22284f | 2014-10-13 16:02:20 +0000 | [diff] [blame] | 5732 | assert( sqlite3BtreeCursorIsValid(pCrsr) ); |
drh | 9a65f2c | 2009-06-22 19:05:40 +0000 | [diff] [blame] | 5733 | |
drh | a7c90c4 | 2016-06-04 20:37:10 +0000 | [diff] [blame] | 5734 | n = sqlite3BtreePayloadSize(pCrsr); |
drh | d66c4f8 | 2016-06-04 20:58:35 +0000 | [diff] [blame] | 5735 | if( n>(u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){ |
drh | a7c90c4 | 2016-06-04 20:37:10 +0000 | [diff] [blame] | 5736 | goto too_big; |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 5737 | } |
drh | 722246e | 2014-10-07 23:02:24 +0000 | [diff] [blame] | 5738 | testcase( n==0 ); |
drh | 2a74006 | 2020-02-05 18:28:17 +0000 | [diff] [blame] | 5739 | rc = sqlite3VdbeMemFromBtreeZeroOffset(pCrsr, n, pOut); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 5740 | if( rc ) goto abort_due_to_error; |
drh | e7b554d | 2017-01-09 15:44:25 +0000 | [diff] [blame] | 5741 | if( !pOp->p3 ) Deephemeralize(pOut); |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 5742 | UPDATE_MAX_BLOBSIZE(pOut); |
drh | ee0ec8e | 2013-10-31 17:38:01 +0000 | [diff] [blame] | 5743 | REGISTER_TRACE(pOp->p2, pOut); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5744 | break; |
| 5745 | } |
| 5746 | |
drh | 2133d82 | 2008-01-03 18:44:59 +0000 | [diff] [blame] | 5747 | /* Opcode: Rowid P1 P2 * * * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 5748 | ** Synopsis: r[P2]=rowid |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5749 | ** |
drh | 2133d82 | 2008-01-03 18:44:59 +0000 | [diff] [blame] | 5750 | ** 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] | 5751 | ** P1 is currently point to. |
drh | 044925b | 2009-04-22 17:15:02 +0000 | [diff] [blame] | 5752 | ** |
| 5753 | ** P1 can be either an ordinary table or a virtual table. There used to |
| 5754 | ** be a separate OP_VRowid opcode for use with virtual tables, but this |
| 5755 | ** one opcode now works for both table types. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5756 | */ |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 5757 | case OP_Rowid: { /* out2 */ |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 5758 | VdbeCursor *pC; |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 5759 | i64 v; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 5760 | sqlite3_vtab *pVtab; |
| 5761 | const sqlite3_module *pModule; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5762 | |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 5763 | pOut = out2Prerelease(p, pOp); |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 5764 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 5765 | pC = p->apCsr[pOp->p1]; |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 5766 | assert( pC!=0 ); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 5767 | assert( pC->eCurType!=CURTYPE_PSEUDO || pC->nullRow ); |
drh | 044925b | 2009-04-22 17:15:02 +0000 | [diff] [blame] | 5768 | if( pC->nullRow ){ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 5769 | pOut->flags = MEM_Null; |
drh | 044925b | 2009-04-22 17:15:02 +0000 | [diff] [blame] | 5770 | break; |
| 5771 | }else if( pC->deferredMoveto ){ |
drh | 6149526 | 2009-04-22 15:32:59 +0000 | [diff] [blame] | 5772 | v = pC->movetoTarget; |
drh | 044925b | 2009-04-22 17:15:02 +0000 | [diff] [blame] | 5773 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 5774 | }else if( pC->eCurType==CURTYPE_VTAB ){ |
| 5775 | assert( pC->uc.pVCur!=0 ); |
| 5776 | pVtab = pC->uc.pVCur->pVtab; |
drh | 044925b | 2009-04-22 17:15:02 +0000 | [diff] [blame] | 5777 | pModule = pVtab->pModule; |
| 5778 | assert( pModule->xRowid ); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 5779 | rc = pModule->xRowid(pC->uc.pVCur, &v); |
dan | 016f781 | 2013-08-21 17:35:48 +0000 | [diff] [blame] | 5780 | sqlite3VtabImportErrmsg(p, pVtab); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 5781 | if( rc ) goto abort_due_to_error; |
drh | 044925b | 2009-04-22 17:15:02 +0000 | [diff] [blame] | 5782 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 5783 | }else{ |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 5784 | assert( pC->eCurType==CURTYPE_BTREE ); |
| 5785 | assert( pC->uc.pCursor!=0 ); |
drh | c22284f | 2014-10-13 16:02:20 +0000 | [diff] [blame] | 5786 | rc = sqlite3VdbeCursorRestore(pC); |
drh | 6149526 | 2009-04-22 15:32:59 +0000 | [diff] [blame] | 5787 | if( rc ) goto abort_due_to_error; |
dan | 2b8669a | 2014-11-17 19:42:48 +0000 | [diff] [blame] | 5788 | if( pC->nullRow ){ |
| 5789 | pOut->flags = MEM_Null; |
| 5790 | break; |
| 5791 | } |
drh | a7c90c4 | 2016-06-04 20:37:10 +0000 | [diff] [blame] | 5792 | v = sqlite3BtreeIntegerKey(pC->uc.pCursor); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5793 | } |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 5794 | pOut->u.i = v; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5795 | break; |
| 5796 | } |
| 5797 | |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 5798 | /* Opcode: NullRow P1 * * * * |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 5799 | ** |
| 5800 | ** Move the cursor P1 to a null row. Any OP_Column operations |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 5801 | ** that occur while the cursor is on the null row will always |
| 5802 | ** write a NULL. |
drh | a10be3d | 2022-02-25 18:51:09 +0000 | [diff] [blame] | 5803 | ** |
drh | 3ac6243 | 2022-04-13 17:41:03 +0000 | [diff] [blame] | 5804 | ** If cursor P1 is not previously opened, open it now to a special |
| 5805 | ** pseudo-cursor that always returns NULL for every column. |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 5806 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 5807 | case OP_NullRow: { |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 5808 | VdbeCursor *pC; |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 5809 | |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 5810 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 5811 | pC = p->apCsr[pOp->p1]; |
drh | 3ac6243 | 2022-04-13 17:41:03 +0000 | [diff] [blame] | 5812 | if( pC==0 ){ |
| 5813 | /* If the cursor is not already open, create a special kind of |
| 5814 | ** pseudo-cursor that always gives null rows. */ |
| 5815 | pC = allocateCursor(p, pOp->p1, 1, CURTYPE_PSEUDO); |
| 5816 | if( pC==0 ) goto no_mem; |
| 5817 | pC->seekResult = 0; |
| 5818 | pC->isTable = 1; |
| 5819 | pC->uc.pCursor = sqlite3BtreeFakeValidCursor(); |
| 5820 | } |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame] | 5821 | pC->nullRow = 1; |
drh | 399af1d | 2013-11-20 17:25:55 +0000 | [diff] [blame] | 5822 | pC->cacheStatus = CACHE_STALE; |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 5823 | if( pC->eCurType==CURTYPE_BTREE ){ |
| 5824 | assert( pC->uc.pCursor!=0 ); |
| 5825 | sqlite3BtreeClearCursor(pC->uc.pCursor); |
danielk1977 | be51a65 | 2008-10-08 17:58:48 +0000 | [diff] [blame] | 5826 | } |
drh | cf025a8 | 2018-06-07 18:01:21 +0000 | [diff] [blame] | 5827 | #ifdef SQLITE_DEBUG |
| 5828 | if( pC->seekOp==0 ) pC->seekOp = OP_NullRow; |
| 5829 | #endif |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 5830 | break; |
| 5831 | } |
| 5832 | |
drh | 86b40df | 2017-08-01 19:53:43 +0000 | [diff] [blame] | 5833 | /* Opcode: SeekEnd P1 * * * * |
| 5834 | ** |
| 5835 | ** Position cursor P1 at the end of the btree for the purpose of |
| 5836 | ** appending a new entry onto the btree. |
| 5837 | ** |
| 5838 | ** It is assumed that the cursor is used only for appending and so |
| 5839 | ** if the cursor is valid, then the cursor must already be pointing |
| 5840 | ** at the end of the btree and so no changes are made to |
| 5841 | ** the cursor. |
| 5842 | */ |
| 5843 | /* Opcode: Last P1 P2 * * * |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 5844 | ** |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 5845 | ** The next use of the Rowid or Column or Prev instruction for P1 |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 5846 | ** will refer to the last entry in the database table or index. |
| 5847 | ** If the table or index is empty and P2>0, then jump immediately to P2. |
| 5848 | ** If P2 is 0 or if the table or index is not empty, fall through |
| 5849 | ** to the following instruction. |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 5850 | ** |
| 5851 | ** This opcode leaves the cursor configured to move in reverse order, |
| 5852 | ** from the end toward the beginning. In other words, the cursor is |
drh | 5dad9a3 | 2014-07-25 18:37:42 +0000 | [diff] [blame] | 5853 | ** configured to use Prev, not Next. |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 5854 | */ |
drh | 86b40df | 2017-08-01 19:53:43 +0000 | [diff] [blame] | 5855 | case OP_SeekEnd: |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 5856 | case OP_Last: { /* jump */ |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 5857 | VdbeCursor *pC; |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 5858 | BtCursor *pCrsr; |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 5859 | int res; |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 5860 | |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 5861 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 5862 | pC = p->apCsr[pOp->p1]; |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 5863 | assert( pC!=0 ); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 5864 | assert( pC->eCurType==CURTYPE_BTREE ); |
| 5865 | pCrsr = pC->uc.pCursor; |
drh | 7abc540 | 2011-10-22 21:00:46 +0000 | [diff] [blame] | 5866 | res = 0; |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 5867 | assert( pCrsr!=0 ); |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 5868 | #ifdef SQLITE_DEBUG |
drh | 86b40df | 2017-08-01 19:53:43 +0000 | [diff] [blame] | 5869 | pC->seekOp = pOp->opcode; |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 5870 | #endif |
drh | 86b40df | 2017-08-01 19:53:43 +0000 | [diff] [blame] | 5871 | if( pOp->opcode==OP_SeekEnd ){ |
drh | d6ef5af | 2016-11-15 04:00:24 +0000 | [diff] [blame] | 5872 | assert( pOp->p2==0 ); |
drh | 86b40df | 2017-08-01 19:53:43 +0000 | [diff] [blame] | 5873 | pC->seekResult = -1; |
| 5874 | if( sqlite3BtreeCursorIsValidNN(pCrsr) ){ |
| 5875 | break; |
| 5876 | } |
| 5877 | } |
| 5878 | rc = sqlite3BtreeLast(pCrsr, &res); |
| 5879 | pC->nullRow = (u8)res; |
| 5880 | pC->deferredMoveto = 0; |
| 5881 | pC->cacheStatus = CACHE_STALE; |
| 5882 | if( rc ) goto abort_due_to_error; |
| 5883 | if( pOp->p2>0 ){ |
| 5884 | VdbeBranchTaken(res!=0,2); |
| 5885 | if( res ) goto jump_to_p2; |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 5886 | } |
| 5887 | break; |
| 5888 | } |
| 5889 | |
drh | 5e98e83 | 2017-02-17 19:24:06 +0000 | [diff] [blame] | 5890 | /* Opcode: IfSmaller P1 P2 P3 * * |
| 5891 | ** |
| 5892 | ** Estimate the number of rows in the table P1. Jump to P2 if that |
| 5893 | ** estimate is less than approximately 2**(0.1*P3). |
| 5894 | */ |
| 5895 | case OP_IfSmaller: { /* jump */ |
| 5896 | VdbeCursor *pC; |
| 5897 | BtCursor *pCrsr; |
| 5898 | int res; |
| 5899 | i64 sz; |
| 5900 | |
| 5901 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 5902 | pC = p->apCsr[pOp->p1]; |
| 5903 | assert( pC!=0 ); |
| 5904 | pCrsr = pC->uc.pCursor; |
| 5905 | assert( pCrsr ); |
| 5906 | rc = sqlite3BtreeFirst(pCrsr, &res); |
| 5907 | if( rc ) goto abort_due_to_error; |
| 5908 | if( res==0 ){ |
| 5909 | sz = sqlite3BtreeRowCountEst(pCrsr); |
| 5910 | if( ALWAYS(sz>=0) && sqlite3LogEst((u64)sz)<pOp->p3 ) res = 1; |
| 5911 | } |
| 5912 | VdbeBranchTaken(res!=0,2); |
| 5913 | if( res ) goto jump_to_p2; |
| 5914 | break; |
| 5915 | } |
| 5916 | |
drh | 0342b1f | 2005-09-01 03:07:44 +0000 | [diff] [blame] | 5917 | |
drh | 6bd4dc6 | 2016-12-23 16:05:22 +0000 | [diff] [blame] | 5918 | /* Opcode: SorterSort P1 P2 * * * |
| 5919 | ** |
| 5920 | ** After all records have been inserted into the Sorter object |
| 5921 | ** identified by P1, invoke this opcode to actually do the sorting. |
| 5922 | ** Jump to P2 if there are no records to be sorted. |
| 5923 | ** |
| 5924 | ** This opcode is an alias for OP_Sort and OP_Rewind that is used |
| 5925 | ** for Sorter objects. |
| 5926 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 5927 | /* Opcode: Sort P1 P2 * * * |
drh | 0342b1f | 2005-09-01 03:07:44 +0000 | [diff] [blame] | 5928 | ** |
| 5929 | ** This opcode does exactly the same thing as OP_Rewind except that |
| 5930 | ** it increments an undocumented global variable used for testing. |
| 5931 | ** |
| 5932 | ** Sorting is accomplished by writing records into a sorting index, |
| 5933 | ** then rewinding that index and playing it back from beginning to |
| 5934 | ** end. We use the OP_Sort opcode instead of OP_Rewind to do the |
| 5935 | ** rewinding so that the global variable will be incremented and |
| 5936 | ** regression tests can determine whether or not the optimizer is |
| 5937 | ** correctly optimizing out sorts. |
| 5938 | */ |
drh | c6aff30 | 2011-09-01 15:32:47 +0000 | [diff] [blame] | 5939 | case OP_SorterSort: /* jump */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 5940 | case OP_Sort: { /* jump */ |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 5941 | #ifdef SQLITE_TEST |
drh | 0342b1f | 2005-09-01 03:07:44 +0000 | [diff] [blame] | 5942 | sqlite3_sort_count++; |
drh | 4db38a7 | 2005-09-01 12:16:28 +0000 | [diff] [blame] | 5943 | sqlite3_search_count--; |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 5944 | #endif |
drh | 9b47ee3 | 2013-08-20 03:13:51 +0000 | [diff] [blame] | 5945 | p->aCounter[SQLITE_STMTSTATUS_SORT]++; |
drh | 0342b1f | 2005-09-01 03:07:44 +0000 | [diff] [blame] | 5946 | /* Fall through into OP_Rewind */ |
drh | 08b9208 | 2020-08-10 14:18:00 +0000 | [diff] [blame] | 5947 | /* no break */ deliberate_fall_through |
drh | 0342b1f | 2005-09-01 03:07:44 +0000 | [diff] [blame] | 5948 | } |
drh | 038ebf6 | 2019-03-29 15:21:22 +0000 | [diff] [blame] | 5949 | /* Opcode: Rewind P1 P2 * * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5950 | ** |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 5951 | ** The next use of the Rowid or Column or Next instruction for P1 |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 5952 | ** will refer to the first entry in the database table or index. |
dan | 04489b6 | 2014-10-31 20:11:32 +0000 | [diff] [blame] | 5953 | ** If the table or index is empty, jump immediately to P2. |
| 5954 | ** If the table or index is not empty, fall through to the following |
| 5955 | ** instruction. |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 5956 | ** |
| 5957 | ** This opcode leaves the cursor configured to move in forward order, |
drh | 4ed2fb9 | 2014-08-14 13:06:25 +0000 | [diff] [blame] | 5958 | ** from the beginning toward the end. In other words, the cursor is |
drh | 5dad9a3 | 2014-07-25 18:37:42 +0000 | [diff] [blame] | 5959 | ** configured to use Next, not Prev. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5960 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 5961 | case OP_Rewind: { /* jump */ |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 5962 | VdbeCursor *pC; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5963 | BtCursor *pCrsr; |
drh | f4dada7 | 2004-05-11 09:57:35 +0000 | [diff] [blame] | 5964 | int res; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5965 | |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 5966 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
drh | 038ebf6 | 2019-03-29 15:21:22 +0000 | [diff] [blame] | 5967 | assert( pOp->p5==0 ); |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 5968 | pC = p->apCsr[pOp->p1]; |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 5969 | assert( pC!=0 ); |
drh | 14da87f | 2013-11-20 21:51:33 +0000 | [diff] [blame] | 5970 | assert( isSorter(pC)==(pOp->opcode==OP_SorterSort) ); |
dan | 2411dea | 2010-07-03 05:56:09 +0000 | [diff] [blame] | 5971 | res = 1; |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 5972 | #ifdef SQLITE_DEBUG |
| 5973 | pC->seekOp = OP_Rewind; |
| 5974 | #endif |
dan | 689ab89 | 2011-08-12 15:02:00 +0000 | [diff] [blame] | 5975 | if( isSorter(pC) ){ |
drh | 958d261 | 2014-04-18 13:40:07 +0000 | [diff] [blame] | 5976 | rc = sqlite3VdbeSorterRewind(pC, &res); |
dan | a205a48 | 2011-08-27 18:48:57 +0000 | [diff] [blame] | 5977 | }else{ |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 5978 | assert( pC->eCurType==CURTYPE_BTREE ); |
| 5979 | pCrsr = pC->uc.pCursor; |
dan | a205a48 | 2011-08-27 18:48:57 +0000 | [diff] [blame] | 5980 | assert( pCrsr ); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 5981 | rc = sqlite3BtreeFirst(pCrsr, &res); |
drh | a11846b | 2004-01-07 18:52:56 +0000 | [diff] [blame] | 5982 | pC->deferredMoveto = 0; |
drh | 76873ab | 2006-01-07 18:48:26 +0000 | [diff] [blame] | 5983 | pC->cacheStatus = CACHE_STALE; |
drh | f4dada7 | 2004-05-11 09:57:35 +0000 | [diff] [blame] | 5984 | } |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 5985 | if( rc ) goto abort_due_to_error; |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 5986 | pC->nullRow = (u8)res; |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 5987 | assert( pOp->p2>0 && pOp->p2<p->nOp ); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 5988 | VdbeBranchTaken(res!=0,2); |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 5989 | if( res ) goto jump_to_p2; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5990 | break; |
| 5991 | } |
| 5992 | |
drh | a7c9dd5 | 2022-02-24 14:44:23 +0000 | [diff] [blame] | 5993 | /* Opcode: Next P1 P2 P3 * P5 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5994 | ** |
| 5995 | ** 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] | 5996 | ** table or index. If there are no more key/value pairs then fall through |
| 5997 | ** to the following instruction. But if the cursor advance was successful, |
| 5998 | ** jump immediately to P2. |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 5999 | ** |
drh | 5dad9a3 | 2014-07-25 18:37:42 +0000 | [diff] [blame] | 6000 | ** The Next opcode is only valid following an SeekGT, SeekGE, or |
| 6001 | ** OP_Rewind opcode used to position the cursor. Next is not allowed |
| 6002 | ** to follow SeekLT, SeekLE, or OP_Last. |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 6003 | ** |
drh | f93cd94 | 2013-11-21 03:12:25 +0000 | [diff] [blame] | 6004 | ** The P1 cursor must be for a real table, not a pseudo-table. P1 must have |
| 6005 | ** been opened prior to this opcode or the program will segfault. |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 6006 | ** |
drh | e39a732 | 2014-02-03 14:04:11 +0000 | [diff] [blame] | 6007 | ** The P3 value is a hint to the btree implementation. If P3==1, that |
| 6008 | ** means P1 is an SQL index and that this instruction could have been |
| 6009 | ** omitted if that index had been unique. P3 is usually 0. P3 is |
| 6010 | ** always either 0 or 1. |
| 6011 | ** |
drh | afc266a | 2010-03-31 17:47:44 +0000 | [diff] [blame] | 6012 | ** If P5 is positive and the jump is taken, then event counter |
| 6013 | ** number P5-1 in the prepared statement is incremented. |
| 6014 | ** |
drh | f1949b6 | 2018-06-07 17:32:59 +0000 | [diff] [blame] | 6015 | ** See also: Prev |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 6016 | */ |
drh | a7c9dd5 | 2022-02-24 14:44:23 +0000 | [diff] [blame] | 6017 | /* Opcode: Prev P1 P2 P3 * P5 |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 6018 | ** |
| 6019 | ** Back up cursor P1 so that it points to the previous key/data pair in its |
| 6020 | ** table or index. If there is no previous key/value pairs then fall through |
| 6021 | ** to the following instruction. But if the cursor backup was successful, |
| 6022 | ** jump immediately to P2. |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 6023 | ** |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 6024 | ** |
drh | 5dad9a3 | 2014-07-25 18:37:42 +0000 | [diff] [blame] | 6025 | ** The Prev opcode is only valid following an SeekLT, SeekLE, or |
| 6026 | ** OP_Last opcode used to position the cursor. Prev is not allowed |
| 6027 | ** to follow SeekGT, SeekGE, or OP_Rewind. |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 6028 | ** |
drh | f93cd94 | 2013-11-21 03:12:25 +0000 | [diff] [blame] | 6029 | ** The P1 cursor must be for a real table, not a pseudo-table. If P1 is |
| 6030 | ** not open then the behavior is undefined. |
drh | afc266a | 2010-03-31 17:47:44 +0000 | [diff] [blame] | 6031 | ** |
drh | e39a732 | 2014-02-03 14:04:11 +0000 | [diff] [blame] | 6032 | ** The P3 value is a hint to the btree implementation. If P3==1, that |
| 6033 | ** means P1 is an SQL index and that this instruction could have been |
| 6034 | ** omitted if that index had been unique. P3 is usually 0. P3 is |
| 6035 | ** always either 0 or 1. |
| 6036 | ** |
drh | afc266a | 2010-03-31 17:47:44 +0000 | [diff] [blame] | 6037 | ** If P5 is positive and the jump is taken, then event counter |
| 6038 | ** number P5-1 in the prepared statement is incremented. |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 6039 | */ |
drh | 6bd4dc6 | 2016-12-23 16:05:22 +0000 | [diff] [blame] | 6040 | /* Opcode: SorterNext P1 P2 * * P5 |
| 6041 | ** |
| 6042 | ** This opcode works just like OP_Next except that P1 must be a |
| 6043 | ** sorter object for which the OP_SorterSort opcode has been |
| 6044 | ** invoked. This opcode advances the cursor to the next sorted |
| 6045 | ** record, or jumps to P2 if there are no more sorted records. |
| 6046 | */ |
drh | f93cd94 | 2013-11-21 03:12:25 +0000 | [diff] [blame] | 6047 | case OP_SorterNext: { /* jump */ |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 6048 | VdbeCursor *pC; |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 6049 | |
drh | f93cd94 | 2013-11-21 03:12:25 +0000 | [diff] [blame] | 6050 | pC = p->apCsr[pOp->p1]; |
| 6051 | assert( isSorter(pC) ); |
drh | 2ab792e | 2017-05-30 18:34:07 +0000 | [diff] [blame] | 6052 | rc = sqlite3VdbeSorterNext(db, pC); |
drh | f93cd94 | 2013-11-21 03:12:25 +0000 | [diff] [blame] | 6053 | goto next_tail; |
drh | a7c9dd5 | 2022-02-24 14:44:23 +0000 | [diff] [blame] | 6054 | |
drh | f93cd94 | 2013-11-21 03:12:25 +0000 | [diff] [blame] | 6055 | case OP_Prev: /* jump */ |
drh | a7c9dd5 | 2022-02-24 14:44:23 +0000 | [diff] [blame] | 6056 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 6057 | assert( pOp->p5<ArraySize(p->aCounter) ); |
| 6058 | pC = p->apCsr[pOp->p1]; |
| 6059 | assert( pC!=0 ); |
| 6060 | assert( pC->deferredMoveto==0 ); |
| 6061 | assert( pC->eCurType==CURTYPE_BTREE ); |
| 6062 | assert( pC->seekOp==OP_SeekLT || pC->seekOp==OP_SeekLE |
| 6063 | || pC->seekOp==OP_Last || pC->seekOp==OP_IfNoHope |
| 6064 | || pC->seekOp==OP_NullRow); |
| 6065 | rc = sqlite3BtreePrevious(pC->uc.pCursor, pOp->p3); |
| 6066 | goto next_tail; |
| 6067 | |
drh | f93cd94 | 2013-11-21 03:12:25 +0000 | [diff] [blame] | 6068 | case OP_Next: /* jump */ |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 6069 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
drh | 9b47ee3 | 2013-08-20 03:13:51 +0000 | [diff] [blame] | 6070 | assert( pOp->p5<ArraySize(p->aCounter) ); |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame] | 6071 | pC = p->apCsr[pOp->p1]; |
drh | f93cd94 | 2013-11-21 03:12:25 +0000 | [diff] [blame] | 6072 | assert( pC!=0 ); |
| 6073 | assert( pC->deferredMoveto==0 ); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 6074 | assert( pC->eCurType==CURTYPE_BTREE ); |
drh | a7c9dd5 | 2022-02-24 14:44:23 +0000 | [diff] [blame] | 6075 | assert( pC->seekOp==OP_SeekGT || pC->seekOp==OP_SeekGE |
drh | 790b37a | 2019-08-27 17:01:07 +0000 | [diff] [blame] | 6076 | || pC->seekOp==OP_Rewind || pC->seekOp==OP_Found |
| 6077 | || pC->seekOp==OP_NullRow|| pC->seekOp==OP_SeekRowid |
| 6078 | || pC->seekOp==OP_IfNoHope); |
drh | a7c9dd5 | 2022-02-24 14:44:23 +0000 | [diff] [blame] | 6079 | rc = sqlite3BtreeNext(pC->uc.pCursor, pOp->p3); |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 6080 | |
drh | f93cd94 | 2013-11-21 03:12:25 +0000 | [diff] [blame] | 6081 | next_tail: |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 6082 | pC->cacheStatus = CACHE_STALE; |
drh | 2ab792e | 2017-05-30 18:34:07 +0000 | [diff] [blame] | 6083 | VdbeBranchTaken(rc==SQLITE_OK,2); |
| 6084 | if( rc==SQLITE_OK ){ |
drh | f93cd94 | 2013-11-21 03:12:25 +0000 | [diff] [blame] | 6085 | pC->nullRow = 0; |
drh | 9b47ee3 | 2013-08-20 03:13:51 +0000 | [diff] [blame] | 6086 | p->aCounter[pOp->p5]++; |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 6087 | #ifdef SQLITE_TEST |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 6088 | sqlite3_search_count++; |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 6089 | #endif |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 6090 | goto jump_to_p2_and_check_for_interrupt; |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 6091 | } |
drh | 2ab792e | 2017-05-30 18:34:07 +0000 | [diff] [blame] | 6092 | if( rc!=SQLITE_DONE ) goto abort_due_to_error; |
| 6093 | rc = SQLITE_OK; |
| 6094 | pC->nullRow = 1; |
drh | 49afe3a | 2013-07-10 03:05:14 +0000 | [diff] [blame] | 6095 | goto check_for_interrupt; |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 6096 | } |
| 6097 | |
drh | 9b4eaeb | 2016-11-09 00:10:33 +0000 | [diff] [blame] | 6098 | /* Opcode: IdxInsert P1 P2 P3 P4 P5 |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 6099 | ** Synopsis: key=r[P2] |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 6100 | ** |
drh | ef8662b | 2011-06-20 21:47:58 +0000 | [diff] [blame] | 6101 | ** Register P2 holds an SQL index key made using the |
drh | 9437bd2 | 2009-02-01 00:29:56 +0000 | [diff] [blame] | 6102 | ** MakeRecord instructions. This opcode writes that key |
drh | ee32e0a | 2006-01-10 19:45:49 +0000 | [diff] [blame] | 6103 | ** into the index P1. Data for the entry is nil. |
drh | 717e640 | 2001-09-27 03:22:32 +0000 | [diff] [blame] | 6104 | ** |
drh | fb8c56f | 2016-11-09 01:19:25 +0000 | [diff] [blame] | 6105 | ** 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] | 6106 | ** key of reg(P2). In that case, P3 is the index of the first register |
| 6107 | ** for the unpacked key. The availability of the unpacked key can sometimes |
| 6108 | ** be an optimization. |
| 6109 | ** |
| 6110 | ** If P5 has the OPFLAG_APPEND bit set, that is a hint to the b-tree layer |
| 6111 | ** that this insert is likely to be an append. |
drh | e4d9081 | 2007-03-29 05:51:49 +0000 | [diff] [blame] | 6112 | ** |
mistachkin | 21a919f | 2014-02-07 03:28:02 +0000 | [diff] [blame] | 6113 | ** If P5 has the OPFLAG_NCHANGE bit set, then the change counter is |
| 6114 | ** incremented by this instruction. If the OPFLAG_NCHANGE bit is clear, |
| 6115 | ** then the change counter is unchanged. |
drh | 0fd6135 | 2014-02-07 02:29:45 +0000 | [diff] [blame] | 6116 | ** |
drh | eaf6ae2 | 2016-11-09 20:14:34 +0000 | [diff] [blame] | 6117 | ** If the OPFLAG_USESEEKRESULT flag of P5 is set, the implementation might |
| 6118 | ** run faster by avoiding an unnecessary seek on cursor P1. However, |
| 6119 | ** the OPFLAG_USESEEKRESULT flag must only be set if there have been no prior |
| 6120 | ** seeks on the cursor or if the most recent seek used a key equivalent |
| 6121 | ** to P2. |
drh | 0fd6135 | 2014-02-07 02:29:45 +0000 | [diff] [blame] | 6122 | ** |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 6123 | ** This instruction only works for indices. The equivalent instruction |
| 6124 | ** for tables is OP_Insert. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 6125 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 6126 | case OP_IdxInsert: { /* in2 */ |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 6127 | VdbeCursor *pC; |
drh | 8eeb446 | 2016-05-21 20:03:42 +0000 | [diff] [blame] | 6128 | BtreePayload x; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 6129 | |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 6130 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 6131 | pC = p->apCsr[pOp->p1]; |
drh | 4031baf | 2018-05-28 17:31:20 +0000 | [diff] [blame] | 6132 | sqlite3VdbeIncrWriteCounter(p, pC); |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 6133 | assert( pC!=0 ); |
drh | c879c4e | 2020-02-06 13:57:08 +0000 | [diff] [blame] | 6134 | assert( !isSorter(pC) ); |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 6135 | pIn2 = &aMem[pOp->p2]; |
dan | 7aae735 | 2020-12-10 18:06:24 +0000 | [diff] [blame] | 6136 | assert( (pIn2->flags & MEM_Blob) || (pOp->p5 & OPFLAG_PREFORMAT) ); |
drh | 6546af1 | 2013-11-04 15:23:25 +0000 | [diff] [blame] | 6137 | if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++; |
drh | c879c4e | 2020-02-06 13:57:08 +0000 | [diff] [blame] | 6138 | assert( pC->eCurType==CURTYPE_BTREE ); |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 6139 | assert( pC->isTable==0 ); |
| 6140 | rc = ExpandBlob(pIn2); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 6141 | if( rc ) goto abort_due_to_error; |
drh | c879c4e | 2020-02-06 13:57:08 +0000 | [diff] [blame] | 6142 | x.nKey = pIn2->n; |
| 6143 | x.pKey = pIn2->z; |
| 6144 | x.aMem = aMem + pOp->p3; |
| 6145 | x.nMem = (u16)pOp->p4.i; |
| 6146 | rc = sqlite3BtreeInsert(pC->uc.pCursor, &x, |
dan | 7aae735 | 2020-12-10 18:06:24 +0000 | [diff] [blame] | 6147 | (pOp->p5 & (OPFLAG_APPEND|OPFLAG_SAVEPOSITION|OPFLAG_PREFORMAT)), |
drh | c879c4e | 2020-02-06 13:57:08 +0000 | [diff] [blame] | 6148 | ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0) |
| 6149 | ); |
| 6150 | assert( pC->deferredMoveto==0 ); |
| 6151 | pC->cacheStatus = CACHE_STALE; |
| 6152 | if( rc) goto abort_due_to_error; |
| 6153 | break; |
| 6154 | } |
| 6155 | |
| 6156 | /* Opcode: SorterInsert P1 P2 * * * |
| 6157 | ** Synopsis: key=r[P2] |
| 6158 | ** |
| 6159 | ** Register P2 holds an SQL index key made using the |
| 6160 | ** MakeRecord instructions. This opcode writes that key |
| 6161 | ** into the sorter P1. Data for the entry is nil. |
| 6162 | */ |
| 6163 | case OP_SorterInsert: { /* in2 */ |
| 6164 | VdbeCursor *pC; |
| 6165 | |
| 6166 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 6167 | pC = p->apCsr[pOp->p1]; |
| 6168 | sqlite3VdbeIncrWriteCounter(p, pC); |
| 6169 | assert( pC!=0 ); |
| 6170 | assert( isSorter(pC) ); |
| 6171 | pIn2 = &aMem[pOp->p2]; |
| 6172 | assert( pIn2->flags & MEM_Blob ); |
| 6173 | assert( pC->isTable==0 ); |
| 6174 | rc = ExpandBlob(pIn2); |
| 6175 | if( rc ) goto abort_due_to_error; |
| 6176 | rc = sqlite3VdbeSorterWrite(pC, pIn2); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 6177 | if( rc) goto abort_due_to_error; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 6178 | break; |
| 6179 | } |
| 6180 | |
drh | 85bd353 | 2020-05-05 18:42:49 +0000 | [diff] [blame] | 6181 | /* Opcode: IdxDelete P1 P2 P3 * P5 |
drh | f63552b | 2013-10-30 00:25:03 +0000 | [diff] [blame] | 6182 | ** Synopsis: key=r[P2@P3] |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 6183 | ** |
drh | e14006d | 2008-03-25 17:23:32 +0000 | [diff] [blame] | 6184 | ** The content of P3 registers starting at register P2 form |
| 6185 | ** an unpacked index key. This opcode removes that entry from the |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 6186 | ** index opened by cursor P1. |
drh | 85bd353 | 2020-05-05 18:42:49 +0000 | [diff] [blame] | 6187 | ** |
| 6188 | ** If P5 is not zero, then raise an SQLITE_CORRUPT_INDEX error |
| 6189 | ** if no matching index entry is found. This happens when running |
| 6190 | ** an UPDATE or DELETE statement and the index entry to be updated |
| 6191 | ** or deleted is not found. For some uses of IdxDelete |
| 6192 | ** (example: the EXCEPT operator) it does not matter that no matching |
drh | a76b151 | 2021-08-11 18:43:54 +0000 | [diff] [blame] | 6193 | ** entry is found. For those cases, P5 is zero. Also, do not raise |
| 6194 | ** this (self-correcting and non-critical) error if in writable_schema mode. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 6195 | */ |
drh | e14006d | 2008-03-25 17:23:32 +0000 | [diff] [blame] | 6196 | case OP_IdxDelete: { |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 6197 | VdbeCursor *pC; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 6198 | BtCursor *pCrsr; |
drh | 9a65f2c | 2009-06-22 19:05:40 +0000 | [diff] [blame] | 6199 | int res; |
| 6200 | UnpackedRecord r; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 6201 | |
drh | e14006d | 2008-03-25 17:23:32 +0000 | [diff] [blame] | 6202 | assert( pOp->p3>0 ); |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 6203 | 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] | 6204 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 6205 | pC = p->apCsr[pOp->p1]; |
| 6206 | assert( pC!=0 ); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 6207 | assert( pC->eCurType==CURTYPE_BTREE ); |
drh | 4031baf | 2018-05-28 17:31:20 +0000 | [diff] [blame] | 6208 | sqlite3VdbeIncrWriteCounter(p, pC); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 6209 | pCrsr = pC->uc.pCursor; |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 6210 | assert( pCrsr!=0 ); |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 6211 | r.pKeyInfo = pC->pKeyInfo; |
| 6212 | r.nField = (u16)pOp->p3; |
dan | 1fed5da | 2014-02-25 21:01:25 +0000 | [diff] [blame] | 6213 | r.default_rc = 0; |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 6214 | r.aMem = &aMem[pOp->p2]; |
drh | 42a410d | 2021-06-19 18:32:20 +0000 | [diff] [blame] | 6215 | rc = sqlite3BtreeIndexMoveto(pCrsr, &r, &res); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 6216 | if( rc ) goto abort_due_to_error; |
| 6217 | if( res==0 ){ |
dan | e61bbf4 | 2016-01-28 17:06:17 +0000 | [diff] [blame] | 6218 | rc = sqlite3BtreeDelete(pCrsr, BTREE_AUXDELETE); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 6219 | if( rc ) goto abort_due_to_error; |
drh | a76b151 | 2021-08-11 18:43:54 +0000 | [diff] [blame] | 6220 | }else if( pOp->p5 && !sqlite3WritableSchema(db) ){ |
drh | e5ceaac | 2021-01-25 21:24:14 +0000 | [diff] [blame] | 6221 | rc = sqlite3ReportError(SQLITE_CORRUPT_INDEX, __LINE__, "index corruption"); |
drh | 85bd353 | 2020-05-05 18:42:49 +0000 | [diff] [blame] | 6222 | goto abort_due_to_error; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 6223 | } |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 6224 | assert( pC->deferredMoveto==0 ); |
| 6225 | pC->cacheStatus = CACHE_STALE; |
dan | 3b908d4 | 2016-11-08 19:22:32 +0000 | [diff] [blame] | 6226 | pC->seekResult = 0; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 6227 | break; |
| 6228 | } |
| 6229 | |
drh | 170ad68 | 2017-06-02 15:44:22 +0000 | [diff] [blame] | 6230 | /* Opcode: DeferredSeek P1 * P3 P4 * |
| 6231 | ** Synopsis: Move P3 to P1.rowid if needed |
drh | 784c1b9 | 2016-01-30 16:59:56 +0000 | [diff] [blame] | 6232 | ** |
| 6233 | ** P1 is an open index cursor and P3 is a cursor on the corresponding |
| 6234 | ** table. This opcode does a deferred seek of the P3 table cursor |
| 6235 | ** to the row that corresponds to the current row of P1. |
| 6236 | ** |
| 6237 | ** This is a deferred seek. Nothing actually happens until |
| 6238 | ** the cursor is used to read a record. That way, if no reads |
| 6239 | ** occur, no unnecessary I/O happens. |
| 6240 | ** |
| 6241 | ** P4 may be an array of integers (type P4_INTARRAY) containing |
drh | 19d720d | 2016-02-03 19:52:06 +0000 | [diff] [blame] | 6242 | ** one entry for each column in the P3 table. If array entry a(i) |
| 6243 | ** is non-zero, then reading column a(i)-1 from cursor P3 is |
drh | 784c1b9 | 2016-01-30 16:59:56 +0000 | [diff] [blame] | 6244 | ** equivalent to performing the deferred seek and then reading column i |
| 6245 | ** from P1. This information is stored in P3 and used to redirect |
| 6246 | ** reads against P3 over to P1, thus possibly avoiding the need to |
| 6247 | ** seek and read cursor P3. |
| 6248 | */ |
drh | 2133d82 | 2008-01-03 18:44:59 +0000 | [diff] [blame] | 6249 | /* Opcode: IdxRowid P1 P2 * * * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 6250 | ** Synopsis: r[P2]=rowid |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 6251 | ** |
drh | 2133d82 | 2008-01-03 18:44:59 +0000 | [diff] [blame] | 6252 | ** 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] | 6253 | ** the end of the index key pointed to by cursor P1. This integer should be |
| 6254 | ** the rowid of the table entry to which this index entry points. |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 6255 | ** |
drh | 9437bd2 | 2009-02-01 00:29:56 +0000 | [diff] [blame] | 6256 | ** See also: Rowid, MakeRecord. |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 6257 | */ |
drh | 170ad68 | 2017-06-02 15:44:22 +0000 | [diff] [blame] | 6258 | case OP_DeferredSeek: |
| 6259 | case OP_IdxRowid: { /* out2 */ |
| 6260 | VdbeCursor *pC; /* The P1 index cursor */ |
| 6261 | VdbeCursor *pTabCur; /* The P2 table cursor (OP_DeferredSeek only) */ |
| 6262 | i64 rowid; /* Rowid that P1 current points to */ |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 6263 | |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 6264 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 6265 | pC = p->apCsr[pOp->p1]; |
| 6266 | assert( pC!=0 ); |
drh | eab6c12 | 2022-04-14 12:59:25 +0000 | [diff] [blame] | 6267 | assert( pC->eCurType==CURTYPE_BTREE || IsNullCursor(pC) ); |
drh | 784c1b9 | 2016-01-30 16:59:56 +0000 | [diff] [blame] | 6268 | assert( pC->uc.pCursor!=0 ); |
drh | c504f67 | 2022-04-14 14:19:23 +0000 | [diff] [blame^] | 6269 | assert( pC->isTable==0 || IsNullCursor(pC) ); |
drh | c22284f | 2014-10-13 16:02:20 +0000 | [diff] [blame] | 6270 | assert( pC->deferredMoveto==0 ); |
drh | 784c1b9 | 2016-01-30 16:59:56 +0000 | [diff] [blame] | 6271 | assert( !pC->nullRow || pOp->opcode==OP_IdxRowid ); |
| 6272 | |
| 6273 | /* The IdxRowid and Seek opcodes are combined because of the commonality |
| 6274 | ** of sqlite3VdbeCursorRestore() and sqlite3VdbeIdxRowid(). */ |
| 6275 | rc = sqlite3VdbeCursorRestore(pC); |
drh | c22284f | 2014-10-13 16:02:20 +0000 | [diff] [blame] | 6276 | |
| 6277 | /* sqlite3VbeCursorRestore() can only fail if the record has been deleted |
drh | 784c1b9 | 2016-01-30 16:59:56 +0000 | [diff] [blame] | 6278 | ** out from under the cursor. That will never happens for an IdxRowid |
| 6279 | ** or Seek opcode */ |
drh | c22284f | 2014-10-13 16:02:20 +0000 | [diff] [blame] | 6280 | if( NEVER(rc!=SQLITE_OK) ) goto abort_due_to_error; |
| 6281 | |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 6282 | if( !pC->nullRow ){ |
drh | 2dc0648 | 2013-12-11 00:59:10 +0000 | [diff] [blame] | 6283 | rowid = 0; /* Not needed. Only used to silence a warning. */ |
drh | 784c1b9 | 2016-01-30 16:59:56 +0000 | [diff] [blame] | 6284 | rc = sqlite3VdbeIdxRowid(db, pC->uc.pCursor, &rowid); |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 6285 | if( rc!=SQLITE_OK ){ |
| 6286 | goto abort_due_to_error; |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 6287 | } |
drh | 170ad68 | 2017-06-02 15:44:22 +0000 | [diff] [blame] | 6288 | if( pOp->opcode==OP_DeferredSeek ){ |
drh | 784c1b9 | 2016-01-30 16:59:56 +0000 | [diff] [blame] | 6289 | assert( pOp->p3>=0 && pOp->p3<p->nCursor ); |
| 6290 | pTabCur = p->apCsr[pOp->p3]; |
| 6291 | assert( pTabCur!=0 ); |
| 6292 | assert( pTabCur->eCurType==CURTYPE_BTREE ); |
| 6293 | assert( pTabCur->uc.pCursor!=0 ); |
| 6294 | assert( pTabCur->isTable ); |
| 6295 | pTabCur->nullRow = 0; |
| 6296 | pTabCur->movetoTarget = rowid; |
| 6297 | pTabCur->deferredMoveto = 1; |
drh | fc56950 | 2022-02-25 20:11:59 +0000 | [diff] [blame] | 6298 | pTabCur->cacheStatus = CACHE_STALE; |
drh | 784c1b9 | 2016-01-30 16:59:56 +0000 | [diff] [blame] | 6299 | assert( pOp->p4type==P4_INTARRAY || pOp->p4.ai==0 ); |
drh | e44ac38 | 2021-03-18 13:19:41 +0000 | [diff] [blame] | 6300 | assert( !pTabCur->isEphemeral ); |
drh | b248668 | 2022-01-03 01:43:28 +0000 | [diff] [blame] | 6301 | pTabCur->ub.aAltMap = pOp->p4.ai; |
| 6302 | assert( !pC->isEphemeral ); |
drh | 784c1b9 | 2016-01-30 16:59:56 +0000 | [diff] [blame] | 6303 | pTabCur->pAltCursor = pC; |
| 6304 | }else{ |
| 6305 | pOut = out2Prerelease(p, pOp); |
| 6306 | pOut->u.i = rowid; |
drh | 784c1b9 | 2016-01-30 16:59:56 +0000 | [diff] [blame] | 6307 | } |
| 6308 | }else{ |
| 6309 | assert( pOp->opcode==OP_IdxRowid ); |
| 6310 | sqlite3VdbeMemSetNull(&aMem[pOp->p2]); |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 6311 | } |
| 6312 | break; |
| 6313 | } |
| 6314 | |
drh | be3da24 | 2019-12-29 00:52:41 +0000 | [diff] [blame] | 6315 | /* Opcode: FinishSeek P1 * * * * |
| 6316 | ** |
| 6317 | ** If cursor P1 was previously moved via OP_DeferredSeek, complete that |
| 6318 | ** seek operation now, without further delay. If the cursor seek has |
| 6319 | ** already occurred, this instruction is a no-op. |
| 6320 | */ |
| 6321 | case OP_FinishSeek: { |
| 6322 | VdbeCursor *pC; /* The P1 index cursor */ |
| 6323 | |
| 6324 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 6325 | pC = p->apCsr[pOp->p1]; |
| 6326 | if( pC->deferredMoveto ){ |
| 6327 | rc = sqlite3VdbeFinishMoveto(pC); |
| 6328 | if( rc ) goto abort_due_to_error; |
| 6329 | } |
| 6330 | break; |
| 6331 | } |
| 6332 | |
drh | c51ceeb | 2020-08-31 12:29:03 +0000 | [diff] [blame] | 6333 | /* Opcode: IdxGE P1 P2 P3 P4 * |
drh | f63552b | 2013-10-30 00:25:03 +0000 | [diff] [blame] | 6334 | ** Synopsis: key=r[P3@P4] |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 6335 | ** |
danielk1977 | 61dd583 | 2008-04-18 11:31:12 +0000 | [diff] [blame] | 6336 | ** The P4 register values beginning with P3 form an unpacked index |
drh | 4a1d365 | 2014-02-14 15:13:36 +0000 | [diff] [blame] | 6337 | ** key that omits the PRIMARY KEY. Compare this key value against the index |
| 6338 | ** that P1 is currently pointing to, ignoring the PRIMARY KEY or ROWID |
| 6339 | ** fields at the end. |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 6340 | ** |
danielk1977 | 61dd583 | 2008-04-18 11:31:12 +0000 | [diff] [blame] | 6341 | ** If the P1 index entry is greater than or equal to the key value |
| 6342 | ** then jump to P2. Otherwise fall through to the next instruction. |
drh | 4a1d365 | 2014-02-14 15:13:36 +0000 | [diff] [blame] | 6343 | */ |
drh | c51ceeb | 2020-08-31 12:29:03 +0000 | [diff] [blame] | 6344 | /* Opcode: IdxGT P1 P2 P3 P4 * |
drh | 4a1d365 | 2014-02-14 15:13:36 +0000 | [diff] [blame] | 6345 | ** Synopsis: key=r[P3@P4] |
drh | 772ae62 | 2004-05-19 13:13:08 +0000 | [diff] [blame] | 6346 | ** |
drh | 4a1d365 | 2014-02-14 15:13:36 +0000 | [diff] [blame] | 6347 | ** The P4 register values beginning with P3 form an unpacked index |
| 6348 | ** key that omits the PRIMARY KEY. Compare this key value against the index |
| 6349 | ** that P1 is currently pointing to, ignoring the PRIMARY KEY or ROWID |
| 6350 | ** fields at the end. |
| 6351 | ** |
| 6352 | ** If the P1 index entry is greater than the key value |
| 6353 | ** then jump to P2. Otherwise fall through to the next instruction. |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 6354 | */ |
drh | c51ceeb | 2020-08-31 12:29:03 +0000 | [diff] [blame] | 6355 | /* Opcode: IdxLT P1 P2 P3 P4 * |
drh | f63552b | 2013-10-30 00:25:03 +0000 | [diff] [blame] | 6356 | ** Synopsis: key=r[P3@P4] |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 6357 | ** |
danielk1977 | 61dd583 | 2008-04-18 11:31:12 +0000 | [diff] [blame] | 6358 | ** The P4 register values beginning with P3 form an unpacked index |
drh | 4a1d365 | 2014-02-14 15:13:36 +0000 | [diff] [blame] | 6359 | ** key that omits the PRIMARY KEY or ROWID. Compare this key value against |
| 6360 | ** the index that P1 is currently pointing to, ignoring the PRIMARY KEY or |
| 6361 | ** ROWID on the P1 index. |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 6362 | ** |
danielk1977 | 61dd583 | 2008-04-18 11:31:12 +0000 | [diff] [blame] | 6363 | ** If the P1 index entry is less than the key value then jump to P2. |
| 6364 | ** Otherwise fall through to the next instruction. |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 6365 | */ |
drh | c51ceeb | 2020-08-31 12:29:03 +0000 | [diff] [blame] | 6366 | /* Opcode: IdxLE P1 P2 P3 P4 * |
drh | 4a1d365 | 2014-02-14 15:13:36 +0000 | [diff] [blame] | 6367 | ** Synopsis: key=r[P3@P4] |
| 6368 | ** |
| 6369 | ** The P4 register values beginning with P3 form an unpacked index |
| 6370 | ** key that omits the PRIMARY KEY or ROWID. Compare this key value against |
| 6371 | ** the index that P1 is currently pointing to, ignoring the PRIMARY KEY or |
| 6372 | ** ROWID on the P1 index. |
| 6373 | ** |
| 6374 | ** If the P1 index entry is less than or equal to the key value then jump |
| 6375 | ** to P2. Otherwise fall through to the next instruction. |
| 6376 | */ |
| 6377 | case OP_IdxLE: /* jump */ |
| 6378 | case OP_IdxGT: /* jump */ |
drh | 93952eb | 2009-11-13 19:43:43 +0000 | [diff] [blame] | 6379 | case OP_IdxLT: /* jump */ |
drh | 4a1d365 | 2014-02-14 15:13:36 +0000 | [diff] [blame] | 6380 | case OP_IdxGE: { /* jump */ |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 6381 | VdbeCursor *pC; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 6382 | int res; |
| 6383 | UnpackedRecord r; |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 6384 | |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 6385 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 6386 | pC = p->apCsr[pOp->p1]; |
| 6387 | assert( pC!=0 ); |
drh | d4187c7 | 2010-08-30 22:15:45 +0000 | [diff] [blame] | 6388 | assert( pC->isOrdered ); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 6389 | assert( pC->eCurType==CURTYPE_BTREE ); |
| 6390 | assert( pC->uc.pCursor!=0); |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 6391 | assert( pC->deferredMoveto==0 ); |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 6392 | assert( pOp->p4type==P4_INT32 ); |
| 6393 | r.pKeyInfo = pC->pKeyInfo; |
| 6394 | r.nField = (u16)pOp->p4.i; |
drh | 4a1d365 | 2014-02-14 15:13:36 +0000 | [diff] [blame] | 6395 | if( pOp->opcode<OP_IdxLT ){ |
| 6396 | assert( pOp->opcode==OP_IdxLE || pOp->opcode==OP_IdxGT ); |
dan | 1fed5da | 2014-02-25 21:01:25 +0000 | [diff] [blame] | 6397 | r.default_rc = -1; |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 6398 | }else{ |
drh | 4a1d365 | 2014-02-14 15:13:36 +0000 | [diff] [blame] | 6399 | assert( pOp->opcode==OP_IdxGE || pOp->opcode==OP_IdxLT ); |
dan | 1fed5da | 2014-02-25 21:01:25 +0000 | [diff] [blame] | 6400 | r.default_rc = 0; |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 6401 | } |
| 6402 | r.aMem = &aMem[pOp->p3]; |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 6403 | #ifdef SQLITE_DEBUG |
drh | 5eae974 | 2018-08-03 13:56:26 +0000 | [diff] [blame] | 6404 | { |
| 6405 | int i; |
| 6406 | for(i=0; i<r.nField; i++){ |
| 6407 | assert( memIsValid(&r.aMem[i]) ); |
| 6408 | REGISTER_TRACE(pOp->p3+i, &aMem[pOp->p3+i]); |
| 6409 | } |
| 6410 | } |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 6411 | #endif |
drh | c40076a | 2020-09-29 16:05:09 +0000 | [diff] [blame] | 6412 | |
| 6413 | /* Inlined version of sqlite3VdbeIdxKeyCompare() */ |
| 6414 | { |
| 6415 | i64 nCellKey = 0; |
| 6416 | BtCursor *pCur; |
| 6417 | Mem m; |
| 6418 | |
| 6419 | assert( pC->eCurType==CURTYPE_BTREE ); |
| 6420 | pCur = pC->uc.pCursor; |
| 6421 | assert( sqlite3BtreeCursorIsValid(pCur) ); |
| 6422 | nCellKey = sqlite3BtreePayloadSize(pCur); |
| 6423 | /* nCellKey will always be between 0 and 0xffffffff because of the way |
| 6424 | ** that btreeParseCellPtr() and sqlite3GetVarint32() are implemented */ |
| 6425 | if( nCellKey<=0 || nCellKey>0x7fffffff ){ |
| 6426 | rc = SQLITE_CORRUPT_BKPT; |
| 6427 | goto abort_due_to_error; |
| 6428 | } |
| 6429 | sqlite3VdbeMemInit(&m, db, 0); |
| 6430 | rc = sqlite3VdbeMemFromBtreeZeroOffset(pCur, (u32)nCellKey, &m); |
| 6431 | if( rc ) goto abort_due_to_error; |
| 6432 | res = sqlite3VdbeRecordCompareWithSkip(m.n, m.z, &r, 0); |
drh | fc85450 | 2022-03-02 17:50:59 +0000 | [diff] [blame] | 6433 | sqlite3VdbeMemReleaseMalloc(&m); |
drh | c40076a | 2020-09-29 16:05:09 +0000 | [diff] [blame] | 6434 | } |
| 6435 | /* End of inlined sqlite3VdbeIdxKeyCompare() */ |
| 6436 | |
drh | 4a1d365 | 2014-02-14 15:13:36 +0000 | [diff] [blame] | 6437 | assert( (OP_IdxLE&1)==(OP_IdxLT&1) && (OP_IdxGE&1)==(OP_IdxGT&1) ); |
| 6438 | if( (pOp->opcode&1)==(OP_IdxLT&1) ){ |
| 6439 | assert( pOp->opcode==OP_IdxLE || pOp->opcode==OP_IdxLT ); |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 6440 | res = -res; |
| 6441 | }else{ |
drh | 4a1d365 | 2014-02-14 15:13:36 +0000 | [diff] [blame] | 6442 | assert( pOp->opcode==OP_IdxGE || pOp->opcode==OP_IdxGT ); |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 6443 | res++; |
| 6444 | } |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 6445 | VdbeBranchTaken(res>0,2); |
drh | c40076a | 2020-09-29 16:05:09 +0000 | [diff] [blame] | 6446 | assert( rc==SQLITE_OK ); |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 6447 | if( res>0 ) goto jump_to_p2; |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 6448 | break; |
| 6449 | } |
| 6450 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 6451 | /* Opcode: Destroy P1 P2 P3 * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 6452 | ** |
| 6453 | ** Delete an entire database table or index whose root page in the database |
| 6454 | ** file is given by P1. |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 6455 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 6456 | ** The table being destroyed is in the main database file if P3==0. If |
| 6457 | ** 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] | 6458 | ** that is used to store tables create using CREATE TEMPORARY TABLE. |
| 6459 | ** |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 6460 | ** If AUTOVACUUM is enabled then it is possible that another root page |
| 6461 | ** might be moved into the newly deleted root page in order to keep all |
| 6462 | ** root pages contiguous at the beginning of the database. The former |
| 6463 | ** value of the root page that moved - its value before the move occurred - |
dan | a34adaf | 2017-04-08 14:11:47 +0000 | [diff] [blame] | 6464 | ** is stored in register P2. If no page movement was required (because the |
| 6465 | ** table being dropped was already the last one in the database) then a |
| 6466 | ** zero is stored in register P2. If AUTOVACUUM is disabled then a zero |
| 6467 | ** is stored in register P2. |
| 6468 | ** |
| 6469 | ** This opcode throws an error if there are any active reader VMs when |
| 6470 | ** it is invoked. This is done to avoid the difficulty associated with |
| 6471 | ** updating existing cursors when a root page is moved in an AUTOVACUUM |
| 6472 | ** database. This error is thrown even if the database is not an AUTOVACUUM |
| 6473 | ** db in order to avoid introducing an incompatibility between autovacuum |
| 6474 | ** and non-autovacuum modes. |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 6475 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 6476 | ** See also: Clear |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 6477 | */ |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 6478 | case OP_Destroy: { /* out2 */ |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6479 | int iMoved; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 6480 | int iDb; |
drh | 3a94987 | 2012-09-18 13:20:13 +0000 | [diff] [blame] | 6481 | |
drh | 4031baf | 2018-05-28 17:31:20 +0000 | [diff] [blame] | 6482 | sqlite3VdbeIncrWriteCounter(p, 0); |
drh | 9e92a47 | 2013-06-27 17:40:30 +0000 | [diff] [blame] | 6483 | assert( p->readOnly==0 ); |
drh | 055f298 | 2016-01-15 15:06:41 +0000 | [diff] [blame] | 6484 | assert( pOp->p1>1 ); |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 6485 | pOut = out2Prerelease(p, pOp); |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 6486 | pOut->flags = MEM_Null; |
drh | 086723a | 2015-03-24 12:51:52 +0000 | [diff] [blame] | 6487 | if( db->nVdbeRead > db->nVDestroy+1 ){ |
danielk1977 | e6efa74 | 2004-11-10 11:55:10 +0000 | [diff] [blame] | 6488 | rc = SQLITE_LOCKED; |
drh | 77658e2 | 2007-12-04 16:54:52 +0000 | [diff] [blame] | 6489 | p->errorAction = OE_Abort; |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 6490 | goto abort_due_to_error; |
danielk1977 | e6efa74 | 2004-11-10 11:55:10 +0000 | [diff] [blame] | 6491 | }else{ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 6492 | iDb = pOp->p3; |
drh | a7ab6d8 | 2014-07-21 15:44:39 +0000 | [diff] [blame] | 6493 | assert( DbMaskTest(p->btreeMask, iDb) ); |
drh | 2dc0648 | 2013-12-11 00:59:10 +0000 | [diff] [blame] | 6494 | iMoved = 0; /* Not needed. Only to silence a warning. */ |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 6495 | rc = sqlite3BtreeDropTable(db->aDb[iDb].pBt, pOp->p1, &iMoved); |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 6496 | pOut->flags = MEM_Int; |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 6497 | pOut->u.i = iMoved; |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 6498 | if( rc ) goto abort_due_to_error; |
drh | 3765df4 | 2006-06-28 18:18:09 +0000 | [diff] [blame] | 6499 | #ifndef SQLITE_OMIT_AUTOVACUUM |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 6500 | if( iMoved!=0 ){ |
drh | cdf011d | 2011-04-04 21:25:28 +0000 | [diff] [blame] | 6501 | sqlite3RootPageMoved(db, iDb, iMoved, pOp->p1); |
| 6502 | /* All OP_Destroy operations occur on the same btree */ |
| 6503 | assert( resetSchemaOnFault==0 || resetSchemaOnFault==iDb+1 ); |
| 6504 | resetSchemaOnFault = iDb+1; |
danielk1977 | e6efa74 | 2004-11-10 11:55:10 +0000 | [diff] [blame] | 6505 | } |
drh | 3765df4 | 2006-06-28 18:18:09 +0000 | [diff] [blame] | 6506 | #endif |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6507 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 6508 | break; |
| 6509 | } |
| 6510 | |
danielk1977 | c7af484 | 2008-10-27 13:59:33 +0000 | [diff] [blame] | 6511 | /* Opcode: Clear P1 P2 P3 |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 6512 | ** |
| 6513 | ** Delete all contents of the database table or index whose root page |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 6514 | ** in the database file is given by P1. But, unlike Destroy, do not |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 6515 | ** remove the table or index from the database file. |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 6516 | ** |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 6517 | ** The table being clear is in the main database file if P2==0. If |
| 6518 | ** P2==1 then the table to be clear is in the auxiliary database file |
| 6519 | ** that is used to store tables create using CREATE TEMPORARY TABLE. |
| 6520 | ** |
drh | a6df0e6 | 2021-06-03 18:51:51 +0000 | [diff] [blame] | 6521 | ** If the P3 value is non-zero, then the row change count is incremented |
| 6522 | ** by the number of rows in the table being cleared. If P3 is greater |
| 6523 | ** than zero, then the value stored in register P3 is also incremented |
| 6524 | ** by the number of rows in the table being cleared. |
danielk1977 | c7af484 | 2008-10-27 13:59:33 +0000 | [diff] [blame] | 6525 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 6526 | ** See also: Destroy |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 6527 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 6528 | case OP_Clear: { |
dan | 2c71887 | 2021-06-22 18:32:05 +0000 | [diff] [blame] | 6529 | i64 nChange; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 6530 | |
drh | 4031baf | 2018-05-28 17:31:20 +0000 | [diff] [blame] | 6531 | sqlite3VdbeIncrWriteCounter(p, 0); |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 6532 | nChange = 0; |
drh | 9e92a47 | 2013-06-27 17:40:30 +0000 | [diff] [blame] | 6533 | assert( p->readOnly==0 ); |
drh | a7ab6d8 | 2014-07-21 15:44:39 +0000 | [diff] [blame] | 6534 | assert( DbMaskTest(p->btreeMask, pOp->p2) ); |
drh | a6df0e6 | 2021-06-03 18:51:51 +0000 | [diff] [blame] | 6535 | rc = sqlite3BtreeClearTable(db->aDb[pOp->p2].pBt, (u32)pOp->p1, &nChange); |
danielk1977 | c7af484 | 2008-10-27 13:59:33 +0000 | [diff] [blame] | 6536 | if( pOp->p3 ){ |
| 6537 | p->nChange += nChange; |
| 6538 | if( pOp->p3>0 ){ |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 6539 | assert( memIsValid(&aMem[pOp->p3]) ); |
| 6540 | memAboutToChange(p, &aMem[pOp->p3]); |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 6541 | aMem[pOp->p3].u.i += nChange; |
danielk1977 | c7af484 | 2008-10-27 13:59:33 +0000 | [diff] [blame] | 6542 | } |
| 6543 | } |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 6544 | if( rc ) goto abort_due_to_error; |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 6545 | break; |
| 6546 | } |
| 6547 | |
drh | 65ea12c | 2014-03-19 17:41:36 +0000 | [diff] [blame] | 6548 | /* Opcode: ResetSorter P1 * * * * |
drh | 079a307 | 2014-03-19 14:10:55 +0000 | [diff] [blame] | 6549 | ** |
drh | 65ea12c | 2014-03-19 17:41:36 +0000 | [diff] [blame] | 6550 | ** Delete all contents from the ephemeral table or sorter |
| 6551 | ** that is open on cursor P1. |
drh | 079a307 | 2014-03-19 14:10:55 +0000 | [diff] [blame] | 6552 | ** |
drh | 65ea12c | 2014-03-19 17:41:36 +0000 | [diff] [blame] | 6553 | ** This opcode only works for cursors used for sorting and |
| 6554 | ** opened with OP_OpenEphemeral or OP_SorterOpen. |
drh | 079a307 | 2014-03-19 14:10:55 +0000 | [diff] [blame] | 6555 | */ |
drh | 65ea12c | 2014-03-19 17:41:36 +0000 | [diff] [blame] | 6556 | case OP_ResetSorter: { |
drh | 079a307 | 2014-03-19 14:10:55 +0000 | [diff] [blame] | 6557 | VdbeCursor *pC; |
| 6558 | |
| 6559 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 6560 | pC = p->apCsr[pOp->p1]; |
| 6561 | assert( pC!=0 ); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 6562 | if( isSorter(pC) ){ |
| 6563 | sqlite3VdbeSorterReset(db, pC->uc.pSorter); |
drh | 65ea12c | 2014-03-19 17:41:36 +0000 | [diff] [blame] | 6564 | }else{ |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 6565 | assert( pC->eCurType==CURTYPE_BTREE ); |
drh | 65ea12c | 2014-03-19 17:41:36 +0000 | [diff] [blame] | 6566 | assert( pC->isEphemeral ); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 6567 | rc = sqlite3BtreeClearTableOfCursor(pC->uc.pCursor); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 6568 | if( rc ) goto abort_due_to_error; |
drh | 65ea12c | 2014-03-19 17:41:36 +0000 | [diff] [blame] | 6569 | } |
drh | 079a307 | 2014-03-19 14:10:55 +0000 | [diff] [blame] | 6570 | break; |
| 6571 | } |
| 6572 | |
drh | 0f3f766 | 2017-08-18 14:34:28 +0000 | [diff] [blame] | 6573 | /* Opcode: CreateBtree P1 P2 P3 * * |
| 6574 | ** Synopsis: r[P2]=root iDb=P1 flags=P3 |
drh | 5b2fd56 | 2001-09-13 15:21:31 +0000 | [diff] [blame] | 6575 | ** |
drh | 0f3f766 | 2017-08-18 14:34:28 +0000 | [diff] [blame] | 6576 | ** Allocate a new b-tree in the main database file if P1==0 or in the |
| 6577 | ** TEMP database file if P1==1 or in an attached database if |
| 6578 | ** 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] | 6579 | ** it must be 2 (BTREE_BLOBKEY) for an index or WITHOUT ROWID table. |
drh | 0f3f766 | 2017-08-18 14:34:28 +0000 | [diff] [blame] | 6580 | ** 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] | 6581 | */ |
drh | 0f3f766 | 2017-08-18 14:34:28 +0000 | [diff] [blame] | 6582 | case OP_CreateBtree: { /* out2 */ |
drh | abc3815 | 2020-07-22 13:38:04 +0000 | [diff] [blame] | 6583 | Pgno pgno; |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 6584 | Db *pDb; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 6585 | |
drh | 4031baf | 2018-05-28 17:31:20 +0000 | [diff] [blame] | 6586 | sqlite3VdbeIncrWriteCounter(p, 0); |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 6587 | pOut = out2Prerelease(p, pOp); |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 6588 | pgno = 0; |
drh | 0f3f766 | 2017-08-18 14:34:28 +0000 | [diff] [blame] | 6589 | assert( pOp->p3==BTREE_INTKEY || pOp->p3==BTREE_BLOBKEY ); |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 6590 | assert( pOp->p1>=0 && pOp->p1<db->nDb ); |
drh | a7ab6d8 | 2014-07-21 15:44:39 +0000 | [diff] [blame] | 6591 | assert( DbMaskTest(p->btreeMask, pOp->p1) ); |
drh | 9e92a47 | 2013-06-27 17:40:30 +0000 | [diff] [blame] | 6592 | assert( p->readOnly==0 ); |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 6593 | pDb = &db->aDb[pOp->p1]; |
| 6594 | assert( pDb->pBt!=0 ); |
drh | 0f3f766 | 2017-08-18 14:34:28 +0000 | [diff] [blame] | 6595 | rc = sqlite3BtreeCreateTable(pDb->pBt, &pgno, pOp->p3); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 6596 | if( rc ) goto abort_due_to_error; |
drh | 88a003e | 2008-12-11 16:17:03 +0000 | [diff] [blame] | 6597 | pOut->u.i = pgno; |
drh | 5b2fd56 | 2001-09-13 15:21:31 +0000 | [diff] [blame] | 6598 | break; |
| 6599 | } |
| 6600 | |
drh | 4a54bb5 | 2017-02-18 15:58:52 +0000 | [diff] [blame] | 6601 | /* Opcode: SqlExec * * * P4 * |
| 6602 | ** |
| 6603 | ** Run the SQL statement or statements specified in the P4 string. |
| 6604 | */ |
| 6605 | case OP_SqlExec: { |
drh | 4031baf | 2018-05-28 17:31:20 +0000 | [diff] [blame] | 6606 | sqlite3VdbeIncrWriteCounter(p, 0); |
drh | bce0414 | 2017-02-23 00:58:36 +0000 | [diff] [blame] | 6607 | db->nSqlExec++; |
drh | 4a54bb5 | 2017-02-18 15:58:52 +0000 | [diff] [blame] | 6608 | rc = sqlite3_exec(db, pOp->p4.z, 0, 0, 0); |
drh | bce0414 | 2017-02-23 00:58:36 +0000 | [diff] [blame] | 6609 | db->nSqlExec--; |
drh | 4a54bb5 | 2017-02-18 15:58:52 +0000 | [diff] [blame] | 6610 | if( rc ) goto abort_due_to_error; |
| 6611 | break; |
| 6612 | } |
| 6613 | |
drh | 2264584 | 2011-03-24 01:34:03 +0000 | [diff] [blame] | 6614 | /* Opcode: ParseSchema P1 * * P4 * |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 6615 | ** |
drh | 346a70c | 2020-06-15 20:27:35 +0000 | [diff] [blame] | 6616 | ** Read and parse all entries from the schema table of database P1 |
drh | 1595abc | 2018-08-14 19:27:51 +0000 | [diff] [blame] | 6617 | ** that match the WHERE clause P4. If P4 is a NULL pointer, then the |
| 6618 | ** entire schema for P1 is reparsed. |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 6619 | ** |
| 6620 | ** This opcode invokes the parser to create a new virtual machine, |
shane | 21e7feb | 2008-05-30 15:59:49 +0000 | [diff] [blame] | 6621 | ** then runs the new virtual machine. It is thus a re-entrant opcode. |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 6622 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 6623 | case OP_ParseSchema: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 6624 | int iDb; |
drh | 067b92b | 2020-06-19 15:24:12 +0000 | [diff] [blame] | 6625 | const char *zSchema; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 6626 | char *zSql; |
| 6627 | InitData initData; |
| 6628 | |
drh | bdaec52 | 2011-04-04 00:14:43 +0000 | [diff] [blame] | 6629 | /* Any prepared statement that invokes this opcode will hold mutexes |
| 6630 | ** on every btree. This is a prerequisite for invoking |
| 6631 | ** sqlite3InitCallback(). |
| 6632 | */ |
| 6633 | #ifdef SQLITE_DEBUG |
| 6634 | for(iDb=0; iDb<db->nDb; iDb++){ |
| 6635 | assert( iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) ); |
| 6636 | } |
| 6637 | #endif |
drh | bdaec52 | 2011-04-04 00:14:43 +0000 | [diff] [blame] | 6638 | |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 6639 | iDb = pOp->p1; |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 6640 | assert( iDb>=0 && iDb<db->nDb ); |
drh | fe97234 | 2021-06-07 11:50:23 +0000 | [diff] [blame] | 6641 | assert( DbHasProperty(db, iDb, DB_SchemaLoaded) |
| 6642 | || db->mallocFailed |
| 6643 | || (CORRUPT_DB && (db->flags & SQLITE_NoSchemaError)!=0) ); |
dan | e325ffe | 2018-08-11 13:40:20 +0000 | [diff] [blame] | 6644 | |
| 6645 | #ifndef SQLITE_OMIT_ALTERTABLE |
| 6646 | if( pOp->p4.z==0 ){ |
| 6647 | sqlite3SchemaClear(db->aDb[iDb].pSchema); |
dan | b0c7920 | 2018-08-11 18:34:25 +0000 | [diff] [blame] | 6648 | db->mDbFlags &= ~DBFLAG_SchemaKnownOk; |
dan | 6a5a13d | 2021-02-17 20:08:22 +0000 | [diff] [blame] | 6649 | rc = sqlite3InitOne(db, iDb, &p->zErrMsg, pOp->p5); |
dan | e325ffe | 2018-08-11 13:40:20 +0000 | [diff] [blame] | 6650 | db->mDbFlags |= DBFLAG_SchemaChange; |
dan | 0d5fa6b | 2018-08-24 17:55:49 +0000 | [diff] [blame] | 6651 | p->expired = 0; |
dan | e325ffe | 2018-08-11 13:40:20 +0000 | [diff] [blame] | 6652 | }else |
| 6653 | #endif |
drh | 1595abc | 2018-08-14 19:27:51 +0000 | [diff] [blame] | 6654 | { |
drh | a4a871c | 2021-11-04 14:04:20 +0000 | [diff] [blame] | 6655 | zSchema = LEGACY_SCHEMA_TABLE; |
danielk1977 | a8bbef8 | 2009-03-23 17:11:26 +0000 | [diff] [blame] | 6656 | initData.db = db; |
mistachkin | 1c06b47 | 2018-09-27 00:04:31 +0000 | [diff] [blame] | 6657 | initData.iDb = iDb; |
danielk1977 | a8bbef8 | 2009-03-23 17:11:26 +0000 | [diff] [blame] | 6658 | initData.pzErrMsg = &p->zErrMsg; |
drh | 9fd88e8 | 2018-09-07 11:08:31 +0000 | [diff] [blame] | 6659 | initData.mInitFlags = 0; |
drh | 3b3ddba | 2020-07-22 18:03:56 +0000 | [diff] [blame] | 6660 | initData.mxPage = sqlite3BtreeLastPage(db->aDb[iDb].pBt); |
danielk1977 | a8bbef8 | 2009-03-23 17:11:26 +0000 | [diff] [blame] | 6661 | zSql = sqlite3MPrintf(db, |
drh | c5a93d4 | 2019-08-12 00:08:07 +0000 | [diff] [blame] | 6662 | "SELECT*FROM\"%w\".%s WHERE %s ORDER BY rowid", |
drh | 067b92b | 2020-06-19 15:24:12 +0000 | [diff] [blame] | 6663 | db->aDb[iDb].zDbSName, zSchema, pOp->p4.z); |
danielk1977 | a8bbef8 | 2009-03-23 17:11:26 +0000 | [diff] [blame] | 6664 | if( zSql==0 ){ |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 6665 | rc = SQLITE_NOMEM_BKPT; |
danielk1977 | a8bbef8 | 2009-03-23 17:11:26 +0000 | [diff] [blame] | 6666 | }else{ |
danielk1977 | a8bbef8 | 2009-03-23 17:11:26 +0000 | [diff] [blame] | 6667 | assert( db->init.busy==0 ); |
| 6668 | db->init.busy = 1; |
| 6669 | initData.rc = SQLITE_OK; |
drh | 6b86e51 | 2019-01-05 21:09:37 +0000 | [diff] [blame] | 6670 | initData.nInitRow = 0; |
danielk1977 | a8bbef8 | 2009-03-23 17:11:26 +0000 | [diff] [blame] | 6671 | assert( !db->mallocFailed ); |
| 6672 | rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0); |
| 6673 | if( rc==SQLITE_OK ) rc = initData.rc; |
drh | 6b86e51 | 2019-01-05 21:09:37 +0000 | [diff] [blame] | 6674 | if( rc==SQLITE_OK && initData.nInitRow==0 ){ |
| 6675 | /* The OP_ParseSchema opcode with a non-NULL P4 argument should parse |
| 6676 | ** at least one SQL statement. Any less than that indicates that |
drh | 1e32bed | 2020-06-19 13:33:53 +0000 | [diff] [blame] | 6677 | ** the sqlite_schema table is corrupt. */ |
drh | 6b86e51 | 2019-01-05 21:09:37 +0000 | [diff] [blame] | 6678 | rc = SQLITE_CORRUPT_BKPT; |
| 6679 | } |
drh | dbd6a7d | 2017-04-05 12:39:49 +0000 | [diff] [blame] | 6680 | sqlite3DbFreeNN(db, zSql); |
danielk1977 | a8bbef8 | 2009-03-23 17:11:26 +0000 | [diff] [blame] | 6681 | db->init.busy = 0; |
danielk1977 | a8bbef8 | 2009-03-23 17:11:26 +0000 | [diff] [blame] | 6682 | } |
drh | 3c23a88 | 2007-01-09 14:01:13 +0000 | [diff] [blame] | 6683 | } |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 6684 | if( rc ){ |
| 6685 | sqlite3ResetAllSchemasOfConnection(db); |
| 6686 | if( rc==SQLITE_NOMEM ){ |
| 6687 | goto no_mem; |
| 6688 | } |
| 6689 | goto abort_due_to_error; |
danielk1977 | 261919c | 2005-12-06 12:52:59 +0000 | [diff] [blame] | 6690 | } |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 6691 | break; |
| 6692 | } |
| 6693 | |
drh | 8bfdf72 | 2009-06-19 14:06:03 +0000 | [diff] [blame] | 6694 | #if !defined(SQLITE_OMIT_ANALYZE) |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 6695 | /* Opcode: LoadAnalysis P1 * * * * |
drh | 497e446 | 2005-07-23 03:18:40 +0000 | [diff] [blame] | 6696 | ** |
| 6697 | ** Read the sqlite_stat1 table for database P1 and load the content |
| 6698 | ** of that table into the internal index hash table. This will cause |
| 6699 | ** the analysis to be used when preparing all subsequent queries. |
| 6700 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 6701 | case OP_LoadAnalysis: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 6702 | assert( pOp->p1>=0 && pOp->p1<db->nDb ); |
| 6703 | rc = sqlite3AnalysisLoad(db, pOp->p1); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 6704 | if( rc ) goto abort_due_to_error; |
drh | 497e446 | 2005-07-23 03:18:40 +0000 | [diff] [blame] | 6705 | break; |
| 6706 | } |
drh | 8bfdf72 | 2009-06-19 14:06:03 +0000 | [diff] [blame] | 6707 | #endif /* !defined(SQLITE_OMIT_ANALYZE) */ |
drh | 497e446 | 2005-07-23 03:18:40 +0000 | [diff] [blame] | 6708 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 6709 | /* Opcode: DropTable P1 * * P4 * |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 6710 | ** |
| 6711 | ** Remove the internal (in-memory) data structures that describe |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 6712 | ** the table named P4 in database P1. This is called after a table |
drh | 5dad9a3 | 2014-07-25 18:37:42 +0000 | [diff] [blame] | 6713 | ** is dropped from disk (using the Destroy opcode) in order to keep |
| 6714 | ** the internal representation of the |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 6715 | ** schema consistent with what is on disk. |
| 6716 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 6717 | case OP_DropTable: { |
drh | 4031baf | 2018-05-28 17:31:20 +0000 | [diff] [blame] | 6718 | sqlite3VdbeIncrWriteCounter(p, 0); |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 6719 | sqlite3UnlinkAndDeleteTable(db, pOp->p1, pOp->p4.z); |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 6720 | break; |
| 6721 | } |
| 6722 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 6723 | /* Opcode: DropIndex P1 * * P4 * |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 6724 | ** |
| 6725 | ** Remove the internal (in-memory) data structures that describe |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 6726 | ** the index named P4 in database P1. This is called after an index |
drh | 5dad9a3 | 2014-07-25 18:37:42 +0000 | [diff] [blame] | 6727 | ** is dropped from disk (using the Destroy opcode) |
| 6728 | ** in order to keep the internal representation of the |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 6729 | ** schema consistent with what is on disk. |
| 6730 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 6731 | case OP_DropIndex: { |
drh | 4031baf | 2018-05-28 17:31:20 +0000 | [diff] [blame] | 6732 | sqlite3VdbeIncrWriteCounter(p, 0); |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 6733 | sqlite3UnlinkAndDeleteIndex(db, pOp->p1, pOp->p4.z); |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 6734 | break; |
| 6735 | } |
| 6736 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 6737 | /* Opcode: DropTrigger P1 * * P4 * |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 6738 | ** |
| 6739 | ** Remove the internal (in-memory) data structures that describe |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 6740 | ** the trigger named P4 in database P1. This is called after a trigger |
drh | 5dad9a3 | 2014-07-25 18:37:42 +0000 | [diff] [blame] | 6741 | ** is dropped from disk (using the Destroy opcode) in order to keep |
| 6742 | ** the internal representation of the |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 6743 | ** schema consistent with what is on disk. |
| 6744 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 6745 | case OP_DropTrigger: { |
drh | 4031baf | 2018-05-28 17:31:20 +0000 | [diff] [blame] | 6746 | sqlite3VdbeIncrWriteCounter(p, 0); |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 6747 | sqlite3UnlinkAndDeleteTrigger(db, pOp->p1, pOp->p4.z); |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 6748 | break; |
| 6749 | } |
| 6750 | |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 6751 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 6752 | #ifndef SQLITE_OMIT_INTEGRITY_CHECK |
drh | 98968b2 | 2016-03-15 22:00:39 +0000 | [diff] [blame] | 6753 | /* Opcode: IntegrityCk P1 P2 P3 P4 P5 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 6754 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 6755 | ** Do an analysis of the currently open database. Store in |
| 6756 | ** register P1 the text of an error message describing any problems. |
| 6757 | ** If no problems are found, store a NULL in register P1. |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 6758 | ** |
drh | 66accfc | 2017-02-22 18:04:42 +0000 | [diff] [blame] | 6759 | ** The register P3 contains one less than the maximum number of allowed errors. |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 6760 | ** At most reg(P3) errors will be reported. |
| 6761 | ** In other words, the analysis stops as soon as reg(P1) errors are |
| 6762 | ** seen. Reg(P1) is updated with the number of errors remaining. |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 6763 | ** |
drh | 98968b2 | 2016-03-15 22:00:39 +0000 | [diff] [blame] | 6764 | ** The root page numbers of all tables in the database are integers |
| 6765 | ** stored in P4_INTARRAY argument. |
drh | 2150432 | 2002-06-25 13:16:02 +0000 | [diff] [blame] | 6766 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 6767 | ** If P5 is not zero, the check is done on the auxiliary database |
drh | 2150432 | 2002-06-25 13:16:02 +0000 | [diff] [blame] | 6768 | ** file, not the main database file. |
drh | 1dd397f | 2002-02-03 03:34:07 +0000 | [diff] [blame] | 6769 | ** |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 6770 | ** This opcode is used to implement the integrity_check pragma. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 6771 | */ |
drh | aaab572 | 2002-02-19 13:39:21 +0000 | [diff] [blame] | 6772 | case OP_IntegrityCk: { |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 6773 | int nRoot; /* Number of tables to check. (Number of root pages.) */ |
drh | abc3815 | 2020-07-22 13:38:04 +0000 | [diff] [blame] | 6774 | Pgno *aRoot; /* Array of rootpage numbers for tables to be checked */ |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 6775 | int nErr; /* Number of errors reported */ |
| 6776 | char *z; /* Text of the error report */ |
| 6777 | Mem *pnErr; /* Register keeping track of errors remaining */ |
drh | 9e92a47 | 2013-06-27 17:40:30 +0000 | [diff] [blame] | 6778 | |
drh | 1713afb | 2013-06-28 01:24:57 +0000 | [diff] [blame] | 6779 | assert( p->bIsReader ); |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 6780 | nRoot = pOp->p2; |
drh | 98968b2 | 2016-03-15 22:00:39 +0000 | [diff] [blame] | 6781 | aRoot = pOp->p4.ai; |
drh | 7906975 | 2004-05-22 21:30:40 +0000 | [diff] [blame] | 6782 | assert( nRoot>0 ); |
mistachkin | cec5f1d | 2020-08-04 16:11:37 +0000 | [diff] [blame] | 6783 | assert( aRoot[0]==(Pgno)nRoot ); |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 6784 | assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) ); |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 6785 | pnErr = &aMem[pOp->p3]; |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 6786 | assert( (pnErr->flags & MEM_Int)!=0 ); |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 6787 | assert( (pnErr->flags & (MEM_Str|MEM_Blob))==0 ); |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 6788 | pIn1 = &aMem[pOp->p1]; |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 6789 | assert( pOp->p5<db->nDb ); |
drh | a7ab6d8 | 2014-07-21 15:44:39 +0000 | [diff] [blame] | 6790 | assert( DbMaskTest(p->btreeMask, pOp->p5) ); |
drh | 21f6daa | 2019-10-11 14:21:48 +0000 | [diff] [blame] | 6791 | z = sqlite3BtreeIntegrityCheck(db, db->aDb[pOp->p5].pBt, &aRoot[1], nRoot, |
drh | 66accfc | 2017-02-22 18:04:42 +0000 | [diff] [blame] | 6792 | (int)pnErr->u.i+1, &nErr); |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 6793 | sqlite3VdbeMemSetNull(pIn1); |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 6794 | if( nErr==0 ){ |
| 6795 | assert( z==0 ); |
drh | c890fec | 2008-08-01 20:10:08 +0000 | [diff] [blame] | 6796 | }else if( z==0 ){ |
| 6797 | goto no_mem; |
drh | 1dd397f | 2002-02-03 03:34:07 +0000 | [diff] [blame] | 6798 | }else{ |
drh | 66accfc | 2017-02-22 18:04:42 +0000 | [diff] [blame] | 6799 | pnErr->u.i -= nErr-1; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 6800 | sqlite3VdbeMemSetStr(pIn1, z, -1, SQLITE_UTF8, sqlite3_free); |
danielk1977 | 8a6b541 | 2004-05-24 07:04:25 +0000 | [diff] [blame] | 6801 | } |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 6802 | UPDATE_MAX_BLOBSIZE(pIn1); |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 6803 | sqlite3VdbeChangeEncoding(pIn1, encoding); |
drh | 21f6daa | 2019-10-11 14:21:48 +0000 | [diff] [blame] | 6804 | goto check_for_interrupt; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 6805 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 6806 | #endif /* SQLITE_OMIT_INTEGRITY_CHECK */ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 6807 | |
drh | 3d4501e | 2008-12-04 20:40:10 +0000 | [diff] [blame] | 6808 | /* Opcode: RowSetAdd P1 P2 * * * |
drh | 72e26de | 2016-08-24 21:24:04 +0000 | [diff] [blame] | 6809 | ** Synopsis: rowset(P1)=r[P2] |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 6810 | ** |
drh | bb6783b | 2017-04-29 18:02:49 +0000 | [diff] [blame] | 6811 | ** Insert the integer value held by register P2 into a RowSet object |
drh | 3d4501e | 2008-12-04 20:40:10 +0000 | [diff] [blame] | 6812 | ** held in register P1. |
| 6813 | ** |
| 6814 | ** An assertion fails if P2 is not an integer. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 6815 | */ |
drh | 93952eb | 2009-11-13 19:43:43 +0000 | [diff] [blame] | 6816 | case OP_RowSetAdd: { /* in1, in2 */ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 6817 | pIn1 = &aMem[pOp->p1]; |
| 6818 | pIn2 = &aMem[pOp->p2]; |
drh | 93952eb | 2009-11-13 19:43:43 +0000 | [diff] [blame] | 6819 | assert( (pIn2->flags & MEM_Int)!=0 ); |
drh | 9d67afc | 2018-08-29 20:24:03 +0000 | [diff] [blame] | 6820 | if( (pIn1->flags & MEM_Blob)==0 ){ |
| 6821 | if( sqlite3VdbeMemSetRowSet(pIn1) ) goto no_mem; |
drh | 3d4501e | 2008-12-04 20:40:10 +0000 | [diff] [blame] | 6822 | } |
drh | 9d67afc | 2018-08-29 20:24:03 +0000 | [diff] [blame] | 6823 | assert( sqlite3VdbeMemIsRowSet(pIn1) ); |
| 6824 | sqlite3RowSetInsert((RowSet*)pIn1->z, pIn2->u.i); |
drh | 3d4501e | 2008-12-04 20:40:10 +0000 | [diff] [blame] | 6825 | break; |
| 6826 | } |
| 6827 | |
| 6828 | /* Opcode: RowSetRead P1 P2 P3 * * |
drh | 72e26de | 2016-08-24 21:24:04 +0000 | [diff] [blame] | 6829 | ** Synopsis: r[P3]=rowset(P1) |
drh | 3d4501e | 2008-12-04 20:40:10 +0000 | [diff] [blame] | 6830 | ** |
drh | bb6783b | 2017-04-29 18:02:49 +0000 | [diff] [blame] | 6831 | ** Extract the smallest value from the RowSet object in P1 |
| 6832 | ** and put that value into register P3. |
| 6833 | ** Or, if RowSet object P1 is initially empty, leave P3 |
drh | 3d4501e | 2008-12-04 20:40:10 +0000 | [diff] [blame] | 6834 | ** unchanged and jump to instruction P2. |
| 6835 | */ |
drh | 93952eb | 2009-11-13 19:43:43 +0000 | [diff] [blame] | 6836 | case OP_RowSetRead: { /* jump, in1, out3 */ |
drh | 3d4501e | 2008-12-04 20:40:10 +0000 | [diff] [blame] | 6837 | i64 val; |
drh | 49afe3a | 2013-07-10 03:05:14 +0000 | [diff] [blame] | 6838 | |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 6839 | pIn1 = &aMem[pOp->p1]; |
drh | 9d67afc | 2018-08-29 20:24:03 +0000 | [diff] [blame] | 6840 | assert( (pIn1->flags & MEM_Blob)==0 || sqlite3VdbeMemIsRowSet(pIn1) ); |
| 6841 | if( (pIn1->flags & MEM_Blob)==0 |
| 6842 | || sqlite3RowSetNext((RowSet*)pIn1->z, &val)==0 |
drh | 3d4501e | 2008-12-04 20:40:10 +0000 | [diff] [blame] | 6843 | ){ |
| 6844 | /* The boolean index is empty */ |
drh | 93952eb | 2009-11-13 19:43:43 +0000 | [diff] [blame] | 6845 | sqlite3VdbeMemSetNull(pIn1); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 6846 | VdbeBranchTaken(1,2); |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 6847 | goto jump_to_p2_and_check_for_interrupt; |
drh | 3d4501e | 2008-12-04 20:40:10 +0000 | [diff] [blame] | 6848 | }else{ |
| 6849 | /* A value was pulled from the index */ |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 6850 | VdbeBranchTaken(0,2); |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 6851 | sqlite3VdbeMemSetInt64(&aMem[pOp->p3], val); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 6852 | } |
drh | 49afe3a | 2013-07-10 03:05:14 +0000 | [diff] [blame] | 6853 | goto check_for_interrupt; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 6854 | } |
| 6855 | |
drh | 1b26c7c | 2009-04-22 02:15:47 +0000 | [diff] [blame] | 6856 | /* Opcode: RowSetTest P1 P2 P3 P4 |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 6857 | ** Synopsis: if r[P3] in rowset(P1) goto P2 |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 6858 | ** |
drh | ade9760 | 2009-04-21 15:05:18 +0000 | [diff] [blame] | 6859 | ** 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] | 6860 | ** contains a RowSet object and that RowSet object contains |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 6861 | ** the value held in P3, jump to register P2. Otherwise, insert the |
drh | 1b26c7c | 2009-04-22 02:15:47 +0000 | [diff] [blame] | 6862 | ** integer in P3 into the RowSet and continue on to the |
drh | ade9760 | 2009-04-21 15:05:18 +0000 | [diff] [blame] | 6863 | ** next opcode. |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 6864 | ** |
drh | bb6783b | 2017-04-29 18:02:49 +0000 | [diff] [blame] | 6865 | ** The RowSet object is optimized for the case where sets of integers |
| 6866 | ** are inserted in distinct phases, which each set contains no duplicates. |
| 6867 | ** Each set is identified by a unique P4 value. The first set |
| 6868 | ** must have P4==0, the final set must have P4==-1, and for all other sets |
| 6869 | ** must have P4>0. |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 6870 | ** |
| 6871 | ** This allows optimizations: (a) when P4==0 there is no need to test |
drh | bb6783b | 2017-04-29 18:02:49 +0000 | [diff] [blame] | 6872 | ** the RowSet object for P3, as it is guaranteed not to contain it, |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 6873 | ** (b) when P4==-1 there is no need to insert the value, as it will |
| 6874 | ** never be tested for, and (c) when a value that is part of set X is |
| 6875 | ** inserted, there is no need to search to see if the same value was |
| 6876 | ** previously inserted as part of set X (only if it was previously |
| 6877 | ** inserted as part of some other set). |
| 6878 | */ |
drh | 1b26c7c | 2009-04-22 02:15:47 +0000 | [diff] [blame] | 6879 | case OP_RowSetTest: { /* jump, in1, in3 */ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 6880 | int iSet; |
| 6881 | int exists; |
| 6882 | |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 6883 | pIn1 = &aMem[pOp->p1]; |
| 6884 | pIn3 = &aMem[pOp->p3]; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 6885 | iSet = pOp->p4.i; |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 6886 | assert( pIn3->flags&MEM_Int ); |
| 6887 | |
drh | 1b26c7c | 2009-04-22 02:15:47 +0000 | [diff] [blame] | 6888 | /* If there is anything other than a rowset object in memory cell P1, |
| 6889 | ** delete it now and initialize P1 with an empty rowset |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 6890 | */ |
drh | 9d67afc | 2018-08-29 20:24:03 +0000 | [diff] [blame] | 6891 | if( (pIn1->flags & MEM_Blob)==0 ){ |
| 6892 | if( sqlite3VdbeMemSetRowSet(pIn1) ) goto no_mem; |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 6893 | } |
drh | 9d67afc | 2018-08-29 20:24:03 +0000 | [diff] [blame] | 6894 | assert( sqlite3VdbeMemIsRowSet(pIn1) ); |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 6895 | assert( pOp->p4type==P4_INT32 ); |
drh | 1b26c7c | 2009-04-22 02:15:47 +0000 | [diff] [blame] | 6896 | assert( iSet==-1 || iSet>=0 ); |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 6897 | if( iSet ){ |
drh | 9d67afc | 2018-08-29 20:24:03 +0000 | [diff] [blame] | 6898 | exists = sqlite3RowSetTest((RowSet*)pIn1->z, iSet, pIn3->u.i); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 6899 | VdbeBranchTaken(exists!=0,2); |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 6900 | if( exists ) goto jump_to_p2; |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 6901 | } |
| 6902 | if( iSet>=0 ){ |
drh | 9d67afc | 2018-08-29 20:24:03 +0000 | [diff] [blame] | 6903 | sqlite3RowSetInsert((RowSet*)pIn1->z, pIn3->u.i); |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 6904 | } |
| 6905 | break; |
| 6906 | } |
| 6907 | |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 6908 | |
danielk1977 | 93758c8 | 2005-01-21 08:13:14 +0000 | [diff] [blame] | 6909 | #ifndef SQLITE_OMIT_TRIGGER |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 6910 | |
drh | 0fd6135 | 2014-02-07 02:29:45 +0000 | [diff] [blame] | 6911 | /* Opcode: Program P1 P2 P3 P4 P5 |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 6912 | ** |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 6913 | ** Execute the trigger program passed as P4 (type P4_SUBPROGRAM). |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 6914 | ** |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 6915 | ** P1 contains the address of the memory cell that contains the first memory |
| 6916 | ** cell in an array of values used as arguments to the sub-program. P2 |
| 6917 | ** contains the address to jump to if the sub-program throws an IGNORE |
| 6918 | ** exception using the RAISE() function. Register P3 contains the address |
| 6919 | ** of a memory cell in this (the parent) VM that is used to allocate the |
| 6920 | ** memory required by the sub-vdbe at runtime. |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 6921 | ** |
| 6922 | ** P4 is a pointer to the VM containing the trigger program. |
drh | 0fd6135 | 2014-02-07 02:29:45 +0000 | [diff] [blame] | 6923 | ** |
| 6924 | ** If P5 is non-zero, then recursive program invocation is enabled. |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 6925 | */ |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 6926 | case OP_Program: { /* jump */ |
dan | 65a7cd1 | 2009-09-01 12:16:01 +0000 | [diff] [blame] | 6927 | int nMem; /* Number of memory registers for sub-program */ |
| 6928 | int nByte; /* Bytes of runtime space required for sub-program */ |
| 6929 | Mem *pRt; /* Register to allocate runtime space */ |
| 6930 | Mem *pMem; /* Used to iterate through memory cells */ |
| 6931 | Mem *pEnd; /* Last memory cell in new array */ |
| 6932 | VdbeFrame *pFrame; /* New vdbe frame to execute in */ |
| 6933 | SubProgram *pProgram; /* Sub-program to execute */ |
| 6934 | void *t; /* Token identifying trigger */ |
| 6935 | |
| 6936 | pProgram = pOp->p4.pProgram; |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 6937 | pRt = &aMem[pOp->p3]; |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 6938 | assert( pProgram->nOp>0 ); |
| 6939 | |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 6940 | /* If the p5 flag is clear, then recursive invocation of triggers is |
| 6941 | ** disabled for backwards compatibility (p5 is set if this sub-program |
| 6942 | ** is really a trigger, not a foreign key action, and the flag set |
| 6943 | ** and cleared by the "PRAGMA recursive_triggers" command is clear). |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 6944 | ** |
| 6945 | ** It is recursive invocation of triggers, at the SQL level, that is |
| 6946 | ** disabled. In some cases a single trigger may generate more than one |
| 6947 | ** SubProgram (if the trigger may be executed with more than one different |
| 6948 | ** ON CONFLICT algorithm). SubProgram structures associated with a |
| 6949 | ** single trigger all have the same value for the SubProgram.token |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 6950 | ** variable. */ |
| 6951 | if( pOp->p5 ){ |
dan | 65a7cd1 | 2009-09-01 12:16:01 +0000 | [diff] [blame] | 6952 | t = pProgram->token; |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 6953 | for(pFrame=p->pFrame; pFrame && pFrame->token!=t; pFrame=pFrame->pParent); |
| 6954 | if( pFrame ) break; |
| 6955 | } |
| 6956 | |
dan | f589450 | 2009-10-07 18:41:19 +0000 | [diff] [blame] | 6957 | if( p->nFrame>=db->aLimit[SQLITE_LIMIT_TRIGGER_DEPTH] ){ |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 6958 | rc = SQLITE_ERROR; |
drh | 22c17b8 | 2015-05-15 04:13:15 +0000 | [diff] [blame] | 6959 | sqlite3VdbeError(p, "too many levels of trigger recursion"); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 6960 | goto abort_due_to_error; |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 6961 | } |
| 6962 | |
| 6963 | /* Register pRt is used to store the memory required to save the state |
| 6964 | ** of the current program, and the memory required at runtime to execute |
| 6965 | ** the trigger program. If this trigger has been fired before, then pRt |
| 6966 | ** is already allocated. Otherwise, it must be initialized. */ |
drh | 72f56ef | 2018-08-29 18:47:22 +0000 | [diff] [blame] | 6967 | if( (pRt->flags&MEM_Blob)==0 ){ |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 6968 | /* SubProgram.nMem is set to the number of memory cells used by the |
| 6969 | ** program stored in SubProgram.aOp. As well as these, one memory |
| 6970 | ** cell is required for each cursor used by the program. Set local |
| 6971 | ** variable nMem (and later, VdbeFrame.nChildMem) to this value. |
| 6972 | */ |
dan | 65a7cd1 | 2009-09-01 12:16:01 +0000 | [diff] [blame] | 6973 | nMem = pProgram->nMem + pProgram->nCsr; |
drh | 3cdce92 | 2016-03-21 00:30:40 +0000 | [diff] [blame] | 6974 | assert( nMem>0 ); |
| 6975 | if( pProgram->nCsr==0 ) nMem++; |
dan | 65a7cd1 | 2009-09-01 12:16:01 +0000 | [diff] [blame] | 6976 | nByte = ROUND8(sizeof(VdbeFrame)) |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 6977 | + nMem * sizeof(Mem) |
drh | ab087d4 | 2017-03-24 17:59:56 +0000 | [diff] [blame] | 6978 | + pProgram->nCsr * sizeof(VdbeCursor*) |
| 6979 | + (pProgram->nOp + 7)/8; |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 6980 | pFrame = sqlite3DbMallocZero(db, nByte); |
| 6981 | if( !pFrame ){ |
| 6982 | goto no_mem; |
| 6983 | } |
| 6984 | sqlite3VdbeMemRelease(pRt); |
drh | 72f56ef | 2018-08-29 18:47:22 +0000 | [diff] [blame] | 6985 | pRt->flags = MEM_Blob|MEM_Dyn; |
| 6986 | pRt->z = (char*)pFrame; |
| 6987 | pRt->n = nByte; |
| 6988 | pRt->xDel = sqlite3VdbeFrameMemDel; |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 6989 | |
| 6990 | pFrame->v = p; |
| 6991 | pFrame->nChildMem = nMem; |
| 6992 | pFrame->nChildCsr = pProgram->nCsr; |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 6993 | pFrame->pc = (int)(pOp - aOp); |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 6994 | pFrame->aMem = p->aMem; |
| 6995 | pFrame->nMem = p->nMem; |
| 6996 | pFrame->apCsr = p->apCsr; |
| 6997 | pFrame->nCursor = p->nCursor; |
| 6998 | pFrame->aOp = p->aOp; |
| 6999 | pFrame->nOp = p->nOp; |
| 7000 | pFrame->token = pProgram->token; |
dan | e2f771b | 2014-11-03 15:33:17 +0000 | [diff] [blame] | 7001 | #ifdef SQLITE_ENABLE_STMT_SCANSTATUS |
dan | 43764a8 | 2014-11-01 21:00:04 +0000 | [diff] [blame] | 7002 | pFrame->anExec = p->anExec; |
dan | e2f771b | 2014-11-03 15:33:17 +0000 | [diff] [blame] | 7003 | #endif |
drh | 72f56ef | 2018-08-29 18:47:22 +0000 | [diff] [blame] | 7004 | #ifdef SQLITE_DEBUG |
| 7005 | pFrame->iFrameMagic = SQLITE_FRAME_MAGIC; |
| 7006 | #endif |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 7007 | |
| 7008 | pEnd = &VdbeFrameMem(pFrame)[pFrame->nChildMem]; |
| 7009 | for(pMem=VdbeFrameMem(pFrame); pMem!=pEnd; pMem++){ |
drh | a5750cf | 2014-02-07 13:20:31 +0000 | [diff] [blame] | 7010 | pMem->flags = MEM_Undefined; |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 7011 | pMem->db = db; |
| 7012 | } |
| 7013 | }else{ |
drh | 72f56ef | 2018-08-29 18:47:22 +0000 | [diff] [blame] | 7014 | pFrame = (VdbeFrame*)pRt->z; |
| 7015 | assert( pRt->xDel==sqlite3VdbeFrameMemDel ); |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 7016 | assert( pProgram->nMem+pProgram->nCsr==pFrame->nChildMem |
| 7017 | || (pProgram->nCsr==0 && pProgram->nMem+1==pFrame->nChildMem) ); |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 7018 | assert( pProgram->nCsr==pFrame->nChildCsr ); |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 7019 | assert( (int)(pOp - aOp)==pFrame->pc ); |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 7020 | } |
| 7021 | |
| 7022 | p->nFrame++; |
| 7023 | pFrame->pParent = p->pFrame; |
drh | fae58d5 | 2017-01-26 17:26:44 +0000 | [diff] [blame] | 7024 | pFrame->lastRowid = db->lastRowid; |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 7025 | pFrame->nChange = p->nChange; |
dan | c3da667 | 2014-10-28 18:24:16 +0000 | [diff] [blame] | 7026 | pFrame->nDbChange = p->db->nChange; |
dan | 3200132 | 2016-02-19 18:54:29 +0000 | [diff] [blame] | 7027 | assert( pFrame->pAuxData==0 ); |
| 7028 | pFrame->pAuxData = p->pAuxData; |
| 7029 | p->pAuxData = 0; |
dan | 2832ad4 | 2009-08-31 15:27:27 +0000 | [diff] [blame] | 7030 | p->nChange = 0; |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 7031 | p->pFrame = pFrame; |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 7032 | p->aMem = aMem = VdbeFrameMem(pFrame); |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 7033 | p->nMem = pFrame->nChildMem; |
shane | cea72b2 | 2009-09-07 04:38:36 +0000 | [diff] [blame] | 7034 | p->nCursor = (u16)pFrame->nChildCsr; |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 7035 | p->apCsr = (VdbeCursor **)&aMem[p->nMem]; |
drh | ab087d4 | 2017-03-24 17:59:56 +0000 | [diff] [blame] | 7036 | pFrame->aOnce = (u8*)&p->apCsr[pProgram->nCsr]; |
drh | 18333ef | 2017-03-24 18:38:41 +0000 | [diff] [blame] | 7037 | memset(pFrame->aOnce, 0, (pProgram->nOp + 7)/8); |
drh | bbe879d | 2009-11-14 18:04:35 +0000 | [diff] [blame] | 7038 | p->aOp = aOp = pProgram->aOp; |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 7039 | p->nOp = pProgram->nOp; |
dan | e2f771b | 2014-11-03 15:33:17 +0000 | [diff] [blame] | 7040 | #ifdef SQLITE_ENABLE_STMT_SCANSTATUS |
dan | 43764a8 | 2014-11-01 21:00:04 +0000 | [diff] [blame] | 7041 | p->anExec = 0; |
dan | e2f771b | 2014-11-03 15:33:17 +0000 | [diff] [blame] | 7042 | #endif |
drh | b2e61bc | 2019-01-25 19:29:01 +0000 | [diff] [blame] | 7043 | #ifdef SQLITE_DEBUG |
| 7044 | /* Verify that second and subsequent executions of the same trigger do not |
| 7045 | ** try to reuse register values from the first use. */ |
| 7046 | { |
| 7047 | int i; |
| 7048 | for(i=0; i<p->nMem; i++){ |
| 7049 | aMem[i].pScopyFrom = 0; /* Prevent false-positive AboutToChange() errs */ |
drh | f5cfe6f | 2020-03-03 20:48:12 +0000 | [diff] [blame] | 7050 | MemSetTypeFlag(&aMem[i], MEM_Undefined); /* Fault if this reg is reused */ |
drh | b2e61bc | 2019-01-25 19:29:01 +0000 | [diff] [blame] | 7051 | } |
| 7052 | } |
| 7053 | #endif |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 7054 | pOp = &aOp[-1]; |
drh | b1af9c6 | 2019-02-20 13:55:45 +0000 | [diff] [blame] | 7055 | goto check_for_interrupt; |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 7056 | } |
| 7057 | |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 7058 | /* Opcode: Param P1 P2 * * * |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 7059 | ** |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 7060 | ** This opcode is only ever present in sub-programs called via the |
| 7061 | ** OP_Program instruction. Copy a value currently stored in a memory |
| 7062 | ** cell of the calling (parent) frame to cell P2 in the current frames |
| 7063 | ** address space. This is used by trigger programs to access the new.* |
| 7064 | ** and old.* values. |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 7065 | ** |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 7066 | ** The address of the cell in the parent frame is determined by adding |
| 7067 | ** the value of the P1 argument to the value of the P1 argument to the |
| 7068 | ** calling OP_Program instruction. |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 7069 | */ |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 7070 | case OP_Param: { /* out2 */ |
dan | 65a7cd1 | 2009-09-01 12:16:01 +0000 | [diff] [blame] | 7071 | VdbeFrame *pFrame; |
| 7072 | Mem *pIn; |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 7073 | pOut = out2Prerelease(p, pOp); |
dan | 65a7cd1 | 2009-09-01 12:16:01 +0000 | [diff] [blame] | 7074 | pFrame = p->pFrame; |
| 7075 | pIn = &pFrame->aMem[pOp->p1 + pFrame->aOp[pFrame->pc].p1]; |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 7076 | sqlite3VdbeMemShallowCopy(pOut, pIn, MEM_Ephem); |
| 7077 | break; |
| 7078 | } |
| 7079 | |
danielk1977 | 93758c8 | 2005-01-21 08:13:14 +0000 | [diff] [blame] | 7080 | #endif /* #ifndef SQLITE_OMIT_TRIGGER */ |
rdc | b0c374f | 2004-02-20 22:53:38 +0000 | [diff] [blame] | 7081 | |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 7082 | #ifndef SQLITE_OMIT_FOREIGN_KEY |
dan | 32b09f2 | 2009-09-23 17:29:59 +0000 | [diff] [blame] | 7083 | /* Opcode: FkCounter P1 P2 * * * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 7084 | ** Synopsis: fkctr[P1]+=P2 |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 7085 | ** |
dan | 0ff297e | 2009-09-25 17:03:14 +0000 | [diff] [blame] | 7086 | ** Increment a "constraint counter" by P2 (P2 may be negative or positive). |
| 7087 | ** If P1 is non-zero, the database constraint counter is incremented |
| 7088 | ** (deferred foreign key constraints). Otherwise, if P1 is zero, the |
dan | 32b09f2 | 2009-09-23 17:29:59 +0000 | [diff] [blame] | 7089 | ** statement counter is incremented (immediate foreign key constraints). |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 7090 | */ |
dan | 32b09f2 | 2009-09-23 17:29:59 +0000 | [diff] [blame] | 7091 | case OP_FkCounter: { |
drh | 963c74d | 2013-07-11 12:19:12 +0000 | [diff] [blame] | 7092 | if( db->flags & SQLITE_DeferFKs ){ |
dan | cb3e4b7 | 2013-07-03 19:53:05 +0000 | [diff] [blame] | 7093 | db->nDeferredImmCons += pOp->p2; |
| 7094 | }else if( pOp->p1 ){ |
dan | 0ff297e | 2009-09-25 17:03:14 +0000 | [diff] [blame] | 7095 | db->nDeferredCons += pOp->p2; |
dan | 32b09f2 | 2009-09-23 17:29:59 +0000 | [diff] [blame] | 7096 | }else{ |
dan | 0ff297e | 2009-09-25 17:03:14 +0000 | [diff] [blame] | 7097 | p->nFkConstraint += pOp->p2; |
| 7098 | } |
| 7099 | break; |
| 7100 | } |
| 7101 | |
| 7102 | /* Opcode: FkIfZero P1 P2 * * * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 7103 | ** Synopsis: if fkctr[P1]==0 goto P2 |
dan | 0ff297e | 2009-09-25 17:03:14 +0000 | [diff] [blame] | 7104 | ** |
| 7105 | ** This opcode tests if a foreign key constraint-counter is currently zero. |
| 7106 | ** If so, jump to instruction P2. Otherwise, fall through to the next |
| 7107 | ** instruction. |
| 7108 | ** |
| 7109 | ** If P1 is non-zero, then the jump is taken if the database constraint-counter |
| 7110 | ** is zero (the one that counts deferred constraint violations). If P1 is |
| 7111 | ** zero, the jump is taken if the statement constraint-counter is zero |
| 7112 | ** (immediate foreign key constraint violations). |
| 7113 | */ |
| 7114 | case OP_FkIfZero: { /* jump */ |
| 7115 | if( pOp->p1 ){ |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 7116 | VdbeBranchTaken(db->nDeferredCons==0 && db->nDeferredImmCons==0, 2); |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 7117 | if( db->nDeferredCons==0 && db->nDeferredImmCons==0 ) goto jump_to_p2; |
dan | 0ff297e | 2009-09-25 17:03:14 +0000 | [diff] [blame] | 7118 | }else{ |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 7119 | VdbeBranchTaken(p->nFkConstraint==0 && db->nDeferredImmCons==0, 2); |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 7120 | if( p->nFkConstraint==0 && db->nDeferredImmCons==0 ) goto jump_to_p2; |
dan | 32b09f2 | 2009-09-23 17:29:59 +0000 | [diff] [blame] | 7121 | } |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 7122 | break; |
| 7123 | } |
| 7124 | #endif /* #ifndef SQLITE_OMIT_FOREIGN_KEY */ |
| 7125 | |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 7126 | #ifndef SQLITE_OMIT_AUTOINCREMENT |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 7127 | /* Opcode: MemMax P1 P2 * * * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 7128 | ** Synopsis: r[P1]=max(r[P1],r[P2]) |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 7129 | ** |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 7130 | ** P1 is a register in the root frame of this VM (the root frame is |
| 7131 | ** different from the current frame if this instruction is being executed |
| 7132 | ** within a sub-program). Set the value of register P1 to the maximum of |
| 7133 | ** its current value and the value in register P2. |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 7134 | ** |
| 7135 | ** This instruction throws an error if the memory cell is not initially |
| 7136 | ** an integer. |
| 7137 | */ |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 7138 | case OP_MemMax: { /* in2 */ |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 7139 | VdbeFrame *pFrame; |
| 7140 | if( p->pFrame ){ |
| 7141 | for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent); |
| 7142 | pIn1 = &pFrame->aMem[pOp->p1]; |
| 7143 | }else{ |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 7144 | pIn1 = &aMem[pOp->p1]; |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 7145 | } |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 7146 | assert( memIsValid(pIn1) ); |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 7147 | sqlite3VdbeMemIntegerify(pIn1); |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 7148 | pIn2 = &aMem[pOp->p2]; |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 7149 | sqlite3VdbeMemIntegerify(pIn2); |
| 7150 | if( pIn1->u.i<pIn2->u.i){ |
| 7151 | pIn1->u.i = pIn2->u.i; |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 7152 | } |
| 7153 | break; |
| 7154 | } |
| 7155 | #endif /* SQLITE_OMIT_AUTOINCREMENT */ |
| 7156 | |
drh | 8b0cf38 | 2015-10-06 21:07:06 +0000 | [diff] [blame] | 7157 | /* Opcode: IfPos P1 P2 P3 * * |
| 7158 | ** Synopsis: if r[P1]>0 then r[P1]-=P3, goto P2 |
danielk1977 | a2dc3b1 | 2005-02-05 12:48:48 +0000 | [diff] [blame] | 7159 | ** |
drh | 1689707 | 2015-03-07 00:57:37 +0000 | [diff] [blame] | 7160 | ** Register P1 must contain an integer. |
mistachkin | 91a3ecb | 2015-10-06 21:49:55 +0000 | [diff] [blame] | 7161 | ** 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] | 7162 | ** value in P1 and jump to P2. |
drh | 6f58f70 | 2006-01-08 05:26:41 +0000 | [diff] [blame] | 7163 | ** |
drh | 1689707 | 2015-03-07 00:57:37 +0000 | [diff] [blame] | 7164 | ** If the initial value of register P1 is less than 1, then the |
| 7165 | ** value is unchanged and control passes through to the next instruction. |
danielk1977 | a2dc3b1 | 2005-02-05 12:48:48 +0000 | [diff] [blame] | 7166 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 7167 | case OP_IfPos: { /* jump, in1 */ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 7168 | pIn1 = &aMem[pOp->p1]; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 7169 | assert( pIn1->flags&MEM_Int ); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 7170 | VdbeBranchTaken( pIn1->u.i>0, 2); |
drh | 8b0cf38 | 2015-10-06 21:07:06 +0000 | [diff] [blame] | 7171 | if( pIn1->u.i>0 ){ |
| 7172 | pIn1->u.i -= pOp->p3; |
| 7173 | goto jump_to_p2; |
| 7174 | } |
drh | ec7429a | 2005-10-06 16:53:14 +0000 | [diff] [blame] | 7175 | break; |
| 7176 | } |
| 7177 | |
drh | cc2fa4c | 2016-01-25 15:57:29 +0000 | [diff] [blame] | 7178 | /* Opcode: OffsetLimit P1 P2 P3 * * |
| 7179 | ** 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] | 7180 | ** |
drh | cc2fa4c | 2016-01-25 15:57:29 +0000 | [diff] [blame] | 7181 | ** This opcode performs a commonly used computation associated with |
| 7182 | ** LIMIT and OFFSET process. r[P1] holds the limit counter. r[P3] |
| 7183 | ** holds the offset counter. The opcode computes the combined value |
| 7184 | ** of the LIMIT and OFFSET and stores that value in r[P2]. The r[P2] |
| 7185 | ** value computed is the total number of rows that will need to be |
| 7186 | ** visited in order to complete the query. |
| 7187 | ** |
| 7188 | ** If r[P3] is zero or negative, that means there is no OFFSET |
| 7189 | ** and r[P2] is set to be the value of the LIMIT, r[P1]. |
| 7190 | ** |
| 7191 | ** if r[P1] is zero or negative, that means there is no LIMIT |
| 7192 | ** and r[P2] is set to -1. |
| 7193 | ** |
| 7194 | ** 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] | 7195 | */ |
drh | cc2fa4c | 2016-01-25 15:57:29 +0000 | [diff] [blame] | 7196 | case OP_OffsetLimit: { /* in1, out2, in3 */ |
drh | 719da30 | 2016-12-10 04:06:49 +0000 | [diff] [blame] | 7197 | i64 x; |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 7198 | pIn1 = &aMem[pOp->p1]; |
drh | cc2fa4c | 2016-01-25 15:57:29 +0000 | [diff] [blame] | 7199 | pIn3 = &aMem[pOp->p3]; |
| 7200 | pOut = out2Prerelease(p, pOp); |
| 7201 | assert( pIn1->flags & MEM_Int ); |
| 7202 | assert( pIn3->flags & MEM_Int ); |
drh | 719da30 | 2016-12-10 04:06:49 +0000 | [diff] [blame] | 7203 | x = pIn1->u.i; |
| 7204 | if( x<=0 || sqlite3AddInt64(&x, pIn3->u.i>0?pIn3->u.i:0) ){ |
| 7205 | /* If the LIMIT is less than or equal to zero, loop forever. This |
| 7206 | ** is documented. But also, if the LIMIT+OFFSET exceeds 2^63 then |
| 7207 | ** also loop forever. This is undocumented. In fact, one could argue |
| 7208 | ** that the loop should terminate. But assuming 1 billion iterations |
| 7209 | ** per second (far exceeding the capabilities of any current hardware) |
| 7210 | ** it would take nearly 300 years to actually reach the limit. So |
| 7211 | ** looping forever is a reasonable approximation. */ |
| 7212 | pOut->u.i = -1; |
| 7213 | }else{ |
| 7214 | pOut->u.i = x; |
| 7215 | } |
drh | 15007a9 | 2006-01-08 18:10:17 +0000 | [diff] [blame] | 7216 | break; |
| 7217 | } |
| 7218 | |
drh | f99dd35 | 2016-12-18 17:42:00 +0000 | [diff] [blame] | 7219 | /* Opcode: IfNotZero P1 P2 * * * |
| 7220 | ** Synopsis: if r[P1]!=0 then r[P1]--, goto P2 |
drh | ec7429a | 2005-10-06 16:53:14 +0000 | [diff] [blame] | 7221 | ** |
drh | 1689707 | 2015-03-07 00:57:37 +0000 | [diff] [blame] | 7222 | ** Register P1 must contain an integer. If the content of register P1 is |
drh | f99dd35 | 2016-12-18 17:42:00 +0000 | [diff] [blame] | 7223 | ** initially greater than zero, then decrement the value in register P1. |
| 7224 | ** If it is non-zero (negative or positive) and then also jump to P2. |
| 7225 | ** If register P1 is initially zero, leave it unchanged and fall through. |
drh | ec7429a | 2005-10-06 16:53:14 +0000 | [diff] [blame] | 7226 | */ |
drh | 1689707 | 2015-03-07 00:57:37 +0000 | [diff] [blame] | 7227 | case OP_IfNotZero: { /* jump, in1 */ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 7228 | pIn1 = &aMem[pOp->p1]; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 7229 | assert( pIn1->flags&MEM_Int ); |
drh | 1689707 | 2015-03-07 00:57:37 +0000 | [diff] [blame] | 7230 | VdbeBranchTaken(pIn1->u.i<0, 2); |
| 7231 | if( pIn1->u.i ){ |
drh | f99dd35 | 2016-12-18 17:42:00 +0000 | [diff] [blame] | 7232 | if( pIn1->u.i>0 ) pIn1->u.i--; |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 7233 | goto jump_to_p2; |
drh | 1689707 | 2015-03-07 00:57:37 +0000 | [diff] [blame] | 7234 | } |
| 7235 | break; |
| 7236 | } |
| 7237 | |
| 7238 | /* Opcode: DecrJumpZero P1 P2 * * * |
| 7239 | ** Synopsis: if (--r[P1])==0 goto P2 |
| 7240 | ** |
drh | ab5be2e | 2016-11-30 05:08:59 +0000 | [diff] [blame] | 7241 | ** Register P1 must hold an integer. Decrement the value in P1 |
| 7242 | ** and jump to P2 if the new value is exactly zero. |
drh | 1689707 | 2015-03-07 00:57:37 +0000 | [diff] [blame] | 7243 | */ |
| 7244 | case OP_DecrJumpZero: { /* jump, in1 */ |
| 7245 | pIn1 = &aMem[pOp->p1]; |
| 7246 | assert( pIn1->flags&MEM_Int ); |
drh | ab5be2e | 2016-11-30 05:08:59 +0000 | [diff] [blame] | 7247 | if( pIn1->u.i>SMALLEST_INT64 ) pIn1->u.i--; |
| 7248 | VdbeBranchTaken(pIn1->u.i==0, 2); |
| 7249 | if( pIn1->u.i==0 ) goto jump_to_p2; |
drh | a2a49dc | 2008-01-02 14:28:13 +0000 | [diff] [blame] | 7250 | break; |
| 7251 | } |
| 7252 | |
drh | 1689707 | 2015-03-07 00:57:37 +0000 | [diff] [blame] | 7253 | |
drh | 8f26da6 | 2018-07-05 21:22:57 +0000 | [diff] [blame] | 7254 | /* Opcode: AggStep * P2 P3 P4 P5 |
drh | f63552b | 2013-10-30 00:25:03 +0000 | [diff] [blame] | 7255 | ** Synopsis: accum=r[P3] step(r[P2@P5]) |
drh | e509535 | 2002-02-24 03:25:14 +0000 | [diff] [blame] | 7256 | ** |
drh | 8f26da6 | 2018-07-05 21:22:57 +0000 | [diff] [blame] | 7257 | ** Execute the xStep function for an aggregate. |
| 7258 | ** The function has P5 arguments. P4 is a pointer to the |
dan | 9a94722 | 2018-06-14 19:06:36 +0000 | [diff] [blame] | 7259 | ** FuncDef structure that specifies the function. Register P3 is the |
drh | e2d9e7c | 2015-06-26 18:47:53 +0000 | [diff] [blame] | 7260 | ** accumulator. |
drh | e509535 | 2002-02-24 03:25:14 +0000 | [diff] [blame] | 7261 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 7262 | ** The P5 arguments are taken from register P2 and its |
| 7263 | ** successors. |
drh | e509535 | 2002-02-24 03:25:14 +0000 | [diff] [blame] | 7264 | */ |
drh | 8f26da6 | 2018-07-05 21:22:57 +0000 | [diff] [blame] | 7265 | /* Opcode: AggInverse * P2 P3 P4 P5 |
| 7266 | ** Synopsis: accum=r[P3] inverse(r[P2@P5]) |
| 7267 | ** |
| 7268 | ** Execute the xInverse function for an aggregate. |
| 7269 | ** The function has P5 arguments. P4 is a pointer to the |
| 7270 | ** FuncDef structure that specifies the function. Register P3 is the |
| 7271 | ** accumulator. |
| 7272 | ** |
| 7273 | ** The P5 arguments are taken from register P2 and its |
| 7274 | ** successors. |
| 7275 | */ |
| 7276 | /* Opcode: AggStep1 P1 P2 P3 P4 P5 |
drh | e2d9e7c | 2015-06-26 18:47:53 +0000 | [diff] [blame] | 7277 | ** Synopsis: accum=r[P3] step(r[P2@P5]) |
| 7278 | ** |
dan | 9a94722 | 2018-06-14 19:06:36 +0000 | [diff] [blame] | 7279 | ** Execute the xStep (if P1==0) or xInverse (if P1!=0) function for an |
| 7280 | ** aggregate. The function has P5 arguments. P4 is a pointer to the |
| 7281 | ** FuncDef structure that specifies the function. Register P3 is the |
| 7282 | ** accumulator. |
drh | e2d9e7c | 2015-06-26 18:47:53 +0000 | [diff] [blame] | 7283 | ** |
| 7284 | ** The P5 arguments are taken from register P2 and its |
| 7285 | ** successors. |
| 7286 | ** |
| 7287 | ** This opcode is initially coded as OP_AggStep0. On first evaluation, |
| 7288 | ** the FuncDef stored in P4 is converted into an sqlite3_context and |
| 7289 | ** the opcode is changed. In this way, the initialization of the |
| 7290 | ** sqlite3_context only happens once, instead of on each call to the |
| 7291 | ** step function. |
| 7292 | */ |
drh | 8f26da6 | 2018-07-05 21:22:57 +0000 | [diff] [blame] | 7293 | case OP_AggInverse: |
| 7294 | case OP_AggStep: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 7295 | int n; |
drh | 9c7c913 | 2015-06-26 18:16:52 +0000 | [diff] [blame] | 7296 | sqlite3_context *pCtx; |
drh | e509535 | 2002-02-24 03:25:14 +0000 | [diff] [blame] | 7297 | |
drh | 9c7c913 | 2015-06-26 18:16:52 +0000 | [diff] [blame] | 7298 | assert( pOp->p4type==P4_FUNCDEF ); |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 7299 | n = pOp->p5; |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 7300 | assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) ); |
| 7301 | 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] | 7302 | assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n ); |
drh | f09ac0b | 2018-01-23 03:44:06 +0000 | [diff] [blame] | 7303 | pCtx = sqlite3DbMallocRawNN(db, n*sizeof(sqlite3_value*) + |
| 7304 | (sizeof(pCtx[0]) + sizeof(Mem) - sizeof(sqlite3_value*))); |
drh | 9c7c913 | 2015-06-26 18:16:52 +0000 | [diff] [blame] | 7305 | if( pCtx==0 ) goto no_mem; |
| 7306 | pCtx->pMem = 0; |
drh | f09ac0b | 2018-01-23 03:44:06 +0000 | [diff] [blame] | 7307 | pCtx->pOut = (Mem*)&(pCtx->argv[n]); |
| 7308 | sqlite3VdbeMemInit(pCtx->pOut, db, MEM_Null); |
drh | 9c7c913 | 2015-06-26 18:16:52 +0000 | [diff] [blame] | 7309 | pCtx->pFunc = pOp->p4.pFunc; |
| 7310 | pCtx->iOp = (int)(pOp - aOp); |
| 7311 | pCtx->pVdbe = p; |
drh | f09ac0b | 2018-01-23 03:44:06 +0000 | [diff] [blame] | 7312 | pCtx->skipFlag = 0; |
| 7313 | pCtx->isError = 0; |
drh | 659fdb4 | 2022-04-01 15:31:58 +0000 | [diff] [blame] | 7314 | pCtx->enc = encoding; |
drh | 9c7c913 | 2015-06-26 18:16:52 +0000 | [diff] [blame] | 7315 | pCtx->argc = n; |
| 7316 | pOp->p4type = P4_FUNCCTX; |
| 7317 | pOp->p4.pCtx = pCtx; |
drh | 2c885d0 | 2018-07-07 19:36:04 +0000 | [diff] [blame] | 7318 | |
| 7319 | /* OP_AggInverse must have P1==1 and OP_AggStep must have P1==0 */ |
drh | 8f26da6 | 2018-07-05 21:22:57 +0000 | [diff] [blame] | 7320 | assert( pOp->p1==(pOp->opcode==OP_AggInverse) ); |
drh | 2c885d0 | 2018-07-07 19:36:04 +0000 | [diff] [blame] | 7321 | |
drh | 8f26da6 | 2018-07-05 21:22:57 +0000 | [diff] [blame] | 7322 | pOp->opcode = OP_AggStep1; |
drh | 9c7c913 | 2015-06-26 18:16:52 +0000 | [diff] [blame] | 7323 | /* Fall through into OP_AggStep */ |
drh | 08b9208 | 2020-08-10 14:18:00 +0000 | [diff] [blame] | 7324 | /* no break */ deliberate_fall_through |
drh | 9c7c913 | 2015-06-26 18:16:52 +0000 | [diff] [blame] | 7325 | } |
drh | 8f26da6 | 2018-07-05 21:22:57 +0000 | [diff] [blame] | 7326 | case OP_AggStep1: { |
drh | 9c7c913 | 2015-06-26 18:16:52 +0000 | [diff] [blame] | 7327 | int i; |
| 7328 | sqlite3_context *pCtx; |
| 7329 | Mem *pMem; |
drh | 9c7c913 | 2015-06-26 18:16:52 +0000 | [diff] [blame] | 7330 | |
| 7331 | assert( pOp->p4type==P4_FUNCCTX ); |
| 7332 | pCtx = pOp->p4.pCtx; |
| 7333 | pMem = &aMem[pOp->p3]; |
| 7334 | |
drh | 2c885d0 | 2018-07-07 19:36:04 +0000 | [diff] [blame] | 7335 | #ifdef SQLITE_DEBUG |
| 7336 | if( pOp->p1 ){ |
| 7337 | /* This is an OP_AggInverse call. Verify that xStep has always |
| 7338 | ** been called at least once prior to any xInverse call. */ |
| 7339 | assert( pMem->uTemp==0x1122e0e3 ); |
| 7340 | }else{ |
| 7341 | /* This is an OP_AggStep call. Mark it as such. */ |
| 7342 | pMem->uTemp = 0x1122e0e3; |
| 7343 | } |
| 7344 | #endif |
| 7345 | |
drh | 9c7c913 | 2015-06-26 18:16:52 +0000 | [diff] [blame] | 7346 | /* If this function is inside of a trigger, the register array in aMem[] |
| 7347 | ** might change from one evaluation to the next. The next block of code |
| 7348 | ** checks to see if the register array has changed, and if so it |
| 7349 | ** reinitializes the relavant parts of the sqlite3_context object */ |
| 7350 | if( pCtx->pMem != pMem ){ |
| 7351 | pCtx->pMem = pMem; |
| 7352 | for(i=pCtx->argc-1; i>=0; i--) pCtx->argv[i] = &aMem[pOp->p2+i]; |
| 7353 | } |
| 7354 | |
| 7355 | #ifdef SQLITE_DEBUG |
| 7356 | for(i=0; i<pCtx->argc; i++){ |
| 7357 | assert( memIsValid(pCtx->argv[i]) ); |
| 7358 | REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]); |
| 7359 | } |
| 7360 | #endif |
| 7361 | |
drh | abfcea2 | 2005-09-06 20:36:48 +0000 | [diff] [blame] | 7362 | pMem->n++; |
drh | f09ac0b | 2018-01-23 03:44:06 +0000 | [diff] [blame] | 7363 | assert( pCtx->pOut->flags==MEM_Null ); |
| 7364 | assert( pCtx->isError==0 ); |
| 7365 | assert( pCtx->skipFlag==0 ); |
dan | 67a9b8e | 2018-06-22 20:51:35 +0000 | [diff] [blame] | 7366 | #ifndef SQLITE_OMIT_WINDOWFUNC |
| 7367 | if( pOp->p1 ){ |
| 7368 | (pCtx->pFunc->xInverse)(pCtx,pCtx->argc,pCtx->argv); |
| 7369 | }else |
| 7370 | #endif |
| 7371 | (pCtx->pFunc->xSFunc)(pCtx,pCtx->argc,pCtx->argv); /* IMP: R-24505-23230 */ |
| 7372 | |
drh | f09ac0b | 2018-01-23 03:44:06 +0000 | [diff] [blame] | 7373 | if( pCtx->isError ){ |
| 7374 | if( pCtx->isError>0 ){ |
| 7375 | sqlite3VdbeError(p, "%s", sqlite3_value_text(pCtx->pOut)); |
drh | 9c7c913 | 2015-06-26 18:16:52 +0000 | [diff] [blame] | 7376 | rc = pCtx->isError; |
| 7377 | } |
drh | f09ac0b | 2018-01-23 03:44:06 +0000 | [diff] [blame] | 7378 | if( pCtx->skipFlag ){ |
| 7379 | assert( pOp[-1].opcode==OP_CollSeq ); |
| 7380 | i = pOp[-1].p1; |
| 7381 | if( i ) sqlite3VdbeMemSetInt64(&aMem[i], 1); |
| 7382 | pCtx->skipFlag = 0; |
| 7383 | } |
| 7384 | sqlite3VdbeMemRelease(pCtx->pOut); |
| 7385 | pCtx->pOut->flags = MEM_Null; |
| 7386 | pCtx->isError = 0; |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 7387 | if( rc ) goto abort_due_to_error; |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 7388 | } |
drh | f09ac0b | 2018-01-23 03:44:06 +0000 | [diff] [blame] | 7389 | assert( pCtx->pOut->flags==MEM_Null ); |
| 7390 | assert( pCtx->skipFlag==0 ); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 7391 | break; |
| 7392 | } |
| 7393 | |
drh | 8f26da6 | 2018-07-05 21:22:57 +0000 | [diff] [blame] | 7394 | /* Opcode: AggFinal P1 P2 * P4 * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 7395 | ** Synopsis: accum=r[P1] N=P2 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 7396 | ** |
dan | 9a94722 | 2018-06-14 19:06:36 +0000 | [diff] [blame] | 7397 | ** P1 is the memory location that is the accumulator for an aggregate |
drh | 8f26da6 | 2018-07-05 21:22:57 +0000 | [diff] [blame] | 7398 | ** or window function. Execute the finalizer function |
| 7399 | ** for an aggregate and store the result in P1. |
drh | a10a34b | 2005-09-07 22:09:48 +0000 | [diff] [blame] | 7400 | ** |
| 7401 | ** P2 is the number of arguments that the step function takes and |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 7402 | ** P4 is a pointer to the FuncDef for this function. The P2 |
drh | a10a34b | 2005-09-07 22:09:48 +0000 | [diff] [blame] | 7403 | ** argument is not used by this opcode. It is only there to disambiguate |
| 7404 | ** functions that can take varying numbers of arguments. The |
drh | 8be47a7 | 2018-07-05 20:05:29 +0000 | [diff] [blame] | 7405 | ** P4 argument is only needed for the case where |
drh | a10a34b | 2005-09-07 22:09:48 +0000 | [diff] [blame] | 7406 | ** the step function was not previously called. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 7407 | */ |
drh | 8f26da6 | 2018-07-05 21:22:57 +0000 | [diff] [blame] | 7408 | /* Opcode: AggValue * P2 P3 P4 * |
| 7409 | ** Synopsis: r[P3]=value N=P2 |
| 7410 | ** |
| 7411 | ** Invoke the xValue() function and store the result in register P3. |
| 7412 | ** |
| 7413 | ** P2 is the number of arguments that the step function takes and |
| 7414 | ** P4 is a pointer to the FuncDef for this function. The P2 |
| 7415 | ** argument is not used by this opcode. It is only there to disambiguate |
| 7416 | ** functions that can take varying numbers of arguments. The |
| 7417 | ** P4 argument is only needed for the case where |
| 7418 | ** the step function was not previously called. |
| 7419 | */ |
| 7420 | case OP_AggValue: |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 7421 | case OP_AggFinal: { |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 7422 | Mem *pMem; |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 7423 | assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) ); |
drh | 8f26da6 | 2018-07-05 21:22:57 +0000 | [diff] [blame] | 7424 | assert( pOp->p3==0 || pOp->opcode==OP_AggValue ); |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 7425 | pMem = &aMem[pOp->p1]; |
drh | a10a34b | 2005-09-07 22:09:48 +0000 | [diff] [blame] | 7426 | assert( (pMem->flags & ~(MEM_Null|MEM_Agg))==0 ); |
dan | 67a9b8e | 2018-06-22 20:51:35 +0000 | [diff] [blame] | 7427 | #ifndef SQLITE_OMIT_WINDOWFUNC |
dan | 86fb6e1 | 2018-05-16 20:58:07 +0000 | [diff] [blame] | 7428 | if( pOp->p3 ){ |
dan | 108e6b2 | 2019-03-18 18:55:35 +0000 | [diff] [blame] | 7429 | memAboutToChange(p, &aMem[pOp->p3]); |
dan | 86fb6e1 | 2018-05-16 20:58:07 +0000 | [diff] [blame] | 7430 | rc = sqlite3VdbeMemAggValue(pMem, &aMem[pOp->p3], pOp->p4.pFunc); |
dan | 660af93 | 2018-06-18 16:55:22 +0000 | [diff] [blame] | 7431 | pMem = &aMem[pOp->p3]; |
dan | 67a9b8e | 2018-06-22 20:51:35 +0000 | [diff] [blame] | 7432 | }else |
| 7433 | #endif |
drh | 8f26da6 | 2018-07-05 21:22:57 +0000 | [diff] [blame] | 7434 | { |
| 7435 | rc = sqlite3VdbeMemFinalize(pMem, pOp->p4.pFunc); |
| 7436 | } |
dan | 67a9b8e | 2018-06-22 20:51:35 +0000 | [diff] [blame] | 7437 | |
drh | 4c8555f | 2009-06-25 01:47:11 +0000 | [diff] [blame] | 7438 | if( rc ){ |
drh | 22c17b8 | 2015-05-15 04:13:15 +0000 | [diff] [blame] | 7439 | sqlite3VdbeError(p, "%s", sqlite3_value_text(pMem)); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 7440 | goto abort_due_to_error; |
drh | 90669c1 | 2006-01-20 15:45:36 +0000 | [diff] [blame] | 7441 | } |
drh | 2dca868 | 2008-03-21 17:13:13 +0000 | [diff] [blame] | 7442 | sqlite3VdbeChangeEncoding(pMem, encoding); |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 7443 | UPDATE_MAX_BLOBSIZE(pMem); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 7444 | break; |
| 7445 | } |
| 7446 | |
dan | 5cf5353 | 2010-05-01 16:40:20 +0000 | [diff] [blame] | 7447 | #ifndef SQLITE_OMIT_WAL |
dan | cdc1f04 | 2010-11-18 12:11:05 +0000 | [diff] [blame] | 7448 | /* Opcode: Checkpoint P1 P2 P3 * * |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 7449 | ** |
| 7450 | ** 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] | 7451 | ** WAL mode. Parameter P2 is one of SQLITE_CHECKPOINT_PASSIVE, FULL, |
| 7452 | ** 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] | 7453 | ** SQLITE_BUSY or not, respectively. Write the number of pages in the |
| 7454 | ** WAL after the checkpoint into mem[P3+1] and the number of pages |
| 7455 | ** in the WAL that have been checkpointed after the checkpoint |
| 7456 | ** completes into mem[P3+2]. However on an error, mem[P3+1] and |
| 7457 | ** mem[P3+2] are initialized to -1. |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 7458 | */ |
| 7459 | case OP_Checkpoint: { |
drh | 30aa3b9 | 2011-02-07 23:56:01 +0000 | [diff] [blame] | 7460 | int i; /* Loop counter */ |
| 7461 | int aRes[3]; /* Results */ |
| 7462 | Mem *pMem; /* Write results here */ |
| 7463 | |
drh | 9e92a47 | 2013-06-27 17:40:30 +0000 | [diff] [blame] | 7464 | assert( p->readOnly==0 ); |
drh | 30aa3b9 | 2011-02-07 23:56:01 +0000 | [diff] [blame] | 7465 | aRes[0] = 0; |
| 7466 | aRes[1] = aRes[2] = -1; |
dan | cdc1f04 | 2010-11-18 12:11:05 +0000 | [diff] [blame] | 7467 | assert( pOp->p2==SQLITE_CHECKPOINT_PASSIVE |
| 7468 | || pOp->p2==SQLITE_CHECKPOINT_FULL |
| 7469 | || pOp->p2==SQLITE_CHECKPOINT_RESTART |
dan | f26a154 | 2014-12-02 19:04:54 +0000 | [diff] [blame] | 7470 | || pOp->p2==SQLITE_CHECKPOINT_TRUNCATE |
dan | cdc1f04 | 2010-11-18 12:11:05 +0000 | [diff] [blame] | 7471 | ); |
drh | 30aa3b9 | 2011-02-07 23:56:01 +0000 | [diff] [blame] | 7472 | rc = sqlite3Checkpoint(db, pOp->p1, pOp->p2, &aRes[1], &aRes[2]); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 7473 | if( rc ){ |
| 7474 | if( rc!=SQLITE_BUSY ) goto abort_due_to_error; |
dan | cdc1f04 | 2010-11-18 12:11:05 +0000 | [diff] [blame] | 7475 | rc = SQLITE_OK; |
drh | 30aa3b9 | 2011-02-07 23:56:01 +0000 | [diff] [blame] | 7476 | aRes[0] = 1; |
dan | cdc1f04 | 2010-11-18 12:11:05 +0000 | [diff] [blame] | 7477 | } |
drh | 30aa3b9 | 2011-02-07 23:56:01 +0000 | [diff] [blame] | 7478 | for(i=0, pMem = &aMem[pOp->p3]; i<3; i++, pMem++){ |
| 7479 | sqlite3VdbeMemSetInt64(pMem, (i64)aRes[i]); |
| 7480 | } |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 7481 | break; |
| 7482 | }; |
dan | 5cf5353 | 2010-05-01 16:40:20 +0000 | [diff] [blame] | 7483 | #endif |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 7484 | |
drh | cac29a6 | 2010-07-02 19:36:52 +0000 | [diff] [blame] | 7485 | #ifndef SQLITE_OMIT_PRAGMA |
drh | 0fd6135 | 2014-02-07 02:29:45 +0000 | [diff] [blame] | 7486 | /* Opcode: JournalMode P1 P2 P3 * * |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 7487 | ** |
| 7488 | ** Change the journal mode of database P1 to P3. P3 must be one of the |
| 7489 | ** PAGER_JOURNALMODE_XXX values. If changing between the various rollback |
| 7490 | ** modes (delete, truncate, persist, off and memory), this is a simple |
| 7491 | ** operation. No IO is required. |
| 7492 | ** |
| 7493 | ** If changing into or out of WAL mode the procedure is more complicated. |
| 7494 | ** |
| 7495 | ** Write a string containing the final journal-mode to register P2. |
| 7496 | */ |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 7497 | case OP_JournalMode: { /* out2 */ |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 7498 | Btree *pBt; /* Btree to change journal mode of */ |
| 7499 | Pager *pPager; /* Pager associated with pBt */ |
drh | d80b233 | 2010-05-01 00:59:37 +0000 | [diff] [blame] | 7500 | int eNew; /* New journal mode */ |
| 7501 | int eOld; /* The old journal mode */ |
mistachkin | 59ee77c | 2012-09-13 15:26:44 +0000 | [diff] [blame] | 7502 | #ifndef SQLITE_OMIT_WAL |
drh | d80b233 | 2010-05-01 00:59:37 +0000 | [diff] [blame] | 7503 | const char *zFilename; /* Name of database file for pPager */ |
mistachkin | 59ee77c | 2012-09-13 15:26:44 +0000 | [diff] [blame] | 7504 | #endif |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 7505 | |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 7506 | pOut = out2Prerelease(p, pOp); |
drh | d80b233 | 2010-05-01 00:59:37 +0000 | [diff] [blame] | 7507 | eNew = pOp->p3; |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 7508 | assert( eNew==PAGER_JOURNALMODE_DELETE |
| 7509 | || eNew==PAGER_JOURNALMODE_TRUNCATE |
| 7510 | || eNew==PAGER_JOURNALMODE_PERSIST |
| 7511 | || eNew==PAGER_JOURNALMODE_OFF |
| 7512 | || eNew==PAGER_JOURNALMODE_MEMORY |
| 7513 | || eNew==PAGER_JOURNALMODE_WAL |
| 7514 | || eNew==PAGER_JOURNALMODE_QUERY |
| 7515 | ); |
| 7516 | assert( pOp->p1>=0 && pOp->p1<db->nDb ); |
drh | 9e92a47 | 2013-06-27 17:40:30 +0000 | [diff] [blame] | 7517 | assert( p->readOnly==0 ); |
drh | 3ebaee9 | 2010-05-06 21:37:22 +0000 | [diff] [blame] | 7518 | |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 7519 | pBt = db->aDb[pOp->p1].pBt; |
| 7520 | pPager = sqlite3BtreePager(pBt); |
drh | 0b9b430 | 2010-06-11 17:01:24 +0000 | [diff] [blame] | 7521 | eOld = sqlite3PagerGetJournalMode(pPager); |
| 7522 | if( eNew==PAGER_JOURNALMODE_QUERY ) eNew = eOld; |
dan | a8f249f | 2021-05-20 11:42:51 +0000 | [diff] [blame] | 7523 | assert( sqlite3BtreeHoldsMutex(pBt) ); |
drh | 0b9b430 | 2010-06-11 17:01:24 +0000 | [diff] [blame] | 7524 | if( !sqlite3PagerOkToChangeJournalMode(pPager) ) eNew = eOld; |
dan | 5cf5353 | 2010-05-01 16:40:20 +0000 | [diff] [blame] | 7525 | |
| 7526 | #ifndef SQLITE_OMIT_WAL |
drh | d4e0bb0 | 2012-05-27 01:19:04 +0000 | [diff] [blame] | 7527 | zFilename = sqlite3PagerFilename(pPager, 1); |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 7528 | |
drh | d80b233 | 2010-05-01 00:59:37 +0000 | [diff] [blame] | 7529 | /* Do not allow a transition to journal_mode=WAL for a database |
drh | 6e1f482 | 2010-07-13 23:41:40 +0000 | [diff] [blame] | 7530 | ** in temporary storage or if the VFS does not support shared memory |
drh | d80b233 | 2010-05-01 00:59:37 +0000 | [diff] [blame] | 7531 | */ |
| 7532 | if( eNew==PAGER_JOURNALMODE_WAL |
drh | 057fc81 | 2011-10-17 23:15:31 +0000 | [diff] [blame] | 7533 | && (sqlite3Strlen30(zFilename)==0 /* Temp file */ |
drh | 6e1f482 | 2010-07-13 23:41:40 +0000 | [diff] [blame] | 7534 | || !sqlite3PagerWalSupported(pPager)) /* No shared-memory support */ |
dan | e180c29 | 2010-04-26 17:42:56 +0000 | [diff] [blame] | 7535 | ){ |
drh | 0b9b430 | 2010-06-11 17:01:24 +0000 | [diff] [blame] | 7536 | eNew = eOld; |
dan | e180c29 | 2010-04-26 17:42:56 +0000 | [diff] [blame] | 7537 | } |
| 7538 | |
drh | 0b9b430 | 2010-06-11 17:01:24 +0000 | [diff] [blame] | 7539 | if( (eNew!=eOld) |
| 7540 | && (eOld==PAGER_JOURNALMODE_WAL || eNew==PAGER_JOURNALMODE_WAL) |
| 7541 | ){ |
dan | c0537fe | 2013-06-28 19:41:43 +0000 | [diff] [blame] | 7542 | if( !db->autoCommit || db->nVdbeRead>1 ){ |
drh | 0b9b430 | 2010-06-11 17:01:24 +0000 | [diff] [blame] | 7543 | rc = SQLITE_ERROR; |
drh | 22c17b8 | 2015-05-15 04:13:15 +0000 | [diff] [blame] | 7544 | sqlite3VdbeError(p, |
drh | 0b9b430 | 2010-06-11 17:01:24 +0000 | [diff] [blame] | 7545 | "cannot change %s wal mode from within a transaction", |
| 7546 | (eNew==PAGER_JOURNALMODE_WAL ? "into" : "out of") |
| 7547 | ); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 7548 | goto abort_due_to_error; |
drh | 0b9b430 | 2010-06-11 17:01:24 +0000 | [diff] [blame] | 7549 | }else{ |
| 7550 | |
| 7551 | if( eOld==PAGER_JOURNALMODE_WAL ){ |
| 7552 | /* If leaving WAL mode, close the log file. If successful, the call |
| 7553 | ** to PagerCloseWal() checkpoints and deletes the write-ahead-log |
| 7554 | ** file. An EXCLUSIVE lock may still be held on the database file |
| 7555 | ** after a successful return. |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 7556 | */ |
dan | 7fb8990 | 2016-08-12 16:21:15 +0000 | [diff] [blame] | 7557 | rc = sqlite3PagerCloseWal(pPager, db); |
drh | ab9b744 | 2010-05-10 11:20:05 +0000 | [diff] [blame] | 7558 | if( rc==SQLITE_OK ){ |
drh | 0b9b430 | 2010-06-11 17:01:24 +0000 | [diff] [blame] | 7559 | sqlite3PagerSetJournalMode(pPager, eNew); |
drh | 89c3f2f | 2010-05-15 01:09:38 +0000 | [diff] [blame] | 7560 | } |
drh | 242c4f7 | 2010-06-22 14:49:39 +0000 | [diff] [blame] | 7561 | }else if( eOld==PAGER_JOURNALMODE_MEMORY ){ |
| 7562 | /* Cannot transition directly from MEMORY to WAL. Use mode OFF |
| 7563 | ** as an intermediate */ |
| 7564 | sqlite3PagerSetJournalMode(pPager, PAGER_JOURNALMODE_OFF); |
drh | 0b9b430 | 2010-06-11 17:01:24 +0000 | [diff] [blame] | 7565 | } |
| 7566 | |
| 7567 | /* Open a transaction on the database file. Regardless of the journal |
| 7568 | ** mode, this transaction always uses a rollback journal. |
| 7569 | */ |
drh | 99744fa | 2020-08-25 19:09:07 +0000 | [diff] [blame] | 7570 | assert( sqlite3BtreeTxnState(pBt)!=SQLITE_TXN_WRITE ); |
drh | 0b9b430 | 2010-06-11 17:01:24 +0000 | [diff] [blame] | 7571 | if( rc==SQLITE_OK ){ |
dan | 731bf5b | 2010-06-17 16:44:21 +0000 | [diff] [blame] | 7572 | rc = sqlite3BtreeSetVersion(pBt, (eNew==PAGER_JOURNALMODE_WAL ? 2 : 1)); |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 7573 | } |
| 7574 | } |
| 7575 | } |
dan | 5cf5353 | 2010-05-01 16:40:20 +0000 | [diff] [blame] | 7576 | #endif /* ifndef SQLITE_OMIT_WAL */ |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 7577 | |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 7578 | if( rc ) eNew = eOld; |
drh | 0b9b430 | 2010-06-11 17:01:24 +0000 | [diff] [blame] | 7579 | eNew = sqlite3PagerSetJournalMode(pPager, eNew); |
dan | 731bf5b | 2010-06-17 16:44:21 +0000 | [diff] [blame] | 7580 | |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 7581 | pOut->flags = MEM_Str|MEM_Static|MEM_Term; |
dan | b978002 | 2010-04-21 18:37:57 +0000 | [diff] [blame] | 7582 | pOut->z = (char *)sqlite3JournalModename(eNew); |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 7583 | pOut->n = sqlite3Strlen30(pOut->z); |
| 7584 | pOut->enc = SQLITE_UTF8; |
| 7585 | sqlite3VdbeChangeEncoding(pOut, encoding); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 7586 | if( rc ) goto abort_due_to_error; |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 7587 | break; |
drh | cac29a6 | 2010-07-02 19:36:52 +0000 | [diff] [blame] | 7588 | }; |
| 7589 | #endif /* SQLITE_OMIT_PRAGMA */ |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 7590 | |
drh | fdbcdee | 2007-03-27 14:44:50 +0000 | [diff] [blame] | 7591 | #if !defined(SQLITE_OMIT_VACUUM) && !defined(SQLITE_OMIT_ATTACH) |
drh | 2f6239e | 2018-12-08 00:43:08 +0000 | [diff] [blame] | 7592 | /* Opcode: Vacuum P1 P2 * * * |
drh | 6f8c91c | 2003-12-07 00:24:35 +0000 | [diff] [blame] | 7593 | ** |
drh | 9ef5e77 | 2016-08-19 14:20:56 +0000 | [diff] [blame] | 7594 | ** Vacuum the entire database P1. P1 is 0 for "main", and 2 or more |
| 7595 | ** for an attached database. The "temp" database may not be vacuumed. |
drh | b0b7db9 | 2018-12-07 17:28:28 +0000 | [diff] [blame] | 7596 | ** |
drh | 2f6239e | 2018-12-08 00:43:08 +0000 | [diff] [blame] | 7597 | ** If P2 is not zero, then it is a register holding a string which is |
| 7598 | ** the file into which the result of vacuum should be written. When |
| 7599 | ** P2 is zero, the vacuum overwrites the original database. |
drh | 6f8c91c | 2003-12-07 00:24:35 +0000 | [diff] [blame] | 7600 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 7601 | case OP_Vacuum: { |
drh | 9e92a47 | 2013-06-27 17:40:30 +0000 | [diff] [blame] | 7602 | assert( p->readOnly==0 ); |
drh | 2f6239e | 2018-12-08 00:43:08 +0000 | [diff] [blame] | 7603 | rc = sqlite3RunVacuum(&p->zErrMsg, db, pOp->p1, |
| 7604 | pOp->p2 ? &aMem[pOp->p2] : 0); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 7605 | if( rc ) goto abort_due_to_error; |
drh | 6f8c91c | 2003-12-07 00:24:35 +0000 | [diff] [blame] | 7606 | break; |
| 7607 | } |
drh | 154d4b2 | 2006-09-21 11:02:16 +0000 | [diff] [blame] | 7608 | #endif |
drh | 6f8c91c | 2003-12-07 00:24:35 +0000 | [diff] [blame] | 7609 | |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 7610 | #if !defined(SQLITE_OMIT_AUTOVACUUM) |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 7611 | /* Opcode: IncrVacuum P1 P2 * * * |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 7612 | ** |
| 7613 | ** Perform a single step of the incremental vacuum procedure on |
drh | ca5557f | 2007-05-04 18:30:40 +0000 | [diff] [blame] | 7614 | ** the P1 database. If the vacuum has finished, jump to instruction |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 7615 | ** P2. Otherwise, fall through to the next instruction. |
| 7616 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 7617 | case OP_IncrVacuum: { /* jump */ |
drh | ca5557f | 2007-05-04 18:30:40 +0000 | [diff] [blame] | 7618 | Btree *pBt; |
| 7619 | |
| 7620 | assert( pOp->p1>=0 && pOp->p1<db->nDb ); |
drh | a7ab6d8 | 2014-07-21 15:44:39 +0000 | [diff] [blame] | 7621 | assert( DbMaskTest(p->btreeMask, pOp->p1) ); |
drh | 9e92a47 | 2013-06-27 17:40:30 +0000 | [diff] [blame] | 7622 | assert( p->readOnly==0 ); |
drh | ca5557f | 2007-05-04 18:30:40 +0000 | [diff] [blame] | 7623 | pBt = db->aDb[pOp->p1].pBt; |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 7624 | rc = sqlite3BtreeIncrVacuum(pBt); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 7625 | VdbeBranchTaken(rc==SQLITE_DONE,2); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 7626 | if( rc ){ |
| 7627 | if( rc!=SQLITE_DONE ) goto abort_due_to_error; |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 7628 | rc = SQLITE_OK; |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 7629 | goto jump_to_p2; |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 7630 | } |
| 7631 | break; |
| 7632 | } |
| 7633 | #endif |
| 7634 | |
drh | ba968db | 2018-07-24 22:02:12 +0000 | [diff] [blame] | 7635 | /* Opcode: Expire P1 P2 * * * |
danielk1977 | a21c6b6 | 2005-01-24 10:25:59 +0000 | [diff] [blame] | 7636 | ** |
drh | 25df48d | 2014-07-22 14:58:12 +0000 | [diff] [blame] | 7637 | ** Cause precompiled statements to expire. When an expired statement |
| 7638 | ** is executed using sqlite3_step() it will either automatically |
| 7639 | ** reprepare itself (if it was originally created using sqlite3_prepare_v2()) |
| 7640 | ** or it will fail with SQLITE_SCHEMA. |
danielk1977 | a21c6b6 | 2005-01-24 10:25:59 +0000 | [diff] [blame] | 7641 | ** |
| 7642 | ** 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] | 7643 | ** then only the currently executing statement is expired. |
drh | ba968db | 2018-07-24 22:02:12 +0000 | [diff] [blame] | 7644 | ** |
| 7645 | ** If P2 is 0, then SQL statements are expired immediately. If P2 is 1, |
| 7646 | ** then running SQL statements are allowed to continue to run to completion. |
| 7647 | ** The P2==1 case occurs when a CREATE INDEX or similar schema change happens |
| 7648 | ** that might help the statement run faster but which does not affect the |
| 7649 | ** correctness of operation. |
danielk1977 | a21c6b6 | 2005-01-24 10:25:59 +0000 | [diff] [blame] | 7650 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 7651 | case OP_Expire: { |
drh | ba968db | 2018-07-24 22:02:12 +0000 | [diff] [blame] | 7652 | assert( pOp->p2==0 || pOp->p2==1 ); |
danielk1977 | a21c6b6 | 2005-01-24 10:25:59 +0000 | [diff] [blame] | 7653 | if( !pOp->p1 ){ |
drh | ba968db | 2018-07-24 22:02:12 +0000 | [diff] [blame] | 7654 | sqlite3ExpirePreparedStatements(db, pOp->p2); |
danielk1977 | a21c6b6 | 2005-01-24 10:25:59 +0000 | [diff] [blame] | 7655 | }else{ |
drh | ba968db | 2018-07-24 22:02:12 +0000 | [diff] [blame] | 7656 | p->expired = pOp->p2+1; |
danielk1977 | a21c6b6 | 2005-01-24 10:25:59 +0000 | [diff] [blame] | 7657 | } |
| 7658 | break; |
| 7659 | } |
| 7660 | |
drh | 7b14b65 | 2019-12-29 22:08:20 +0000 | [diff] [blame] | 7661 | /* Opcode: CursorLock P1 * * * * |
| 7662 | ** |
| 7663 | ** Lock the btree to which cursor P1 is pointing so that the btree cannot be |
| 7664 | ** written by an other cursor. |
| 7665 | */ |
| 7666 | case OP_CursorLock: { |
| 7667 | VdbeCursor *pC; |
| 7668 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 7669 | pC = p->apCsr[pOp->p1]; |
| 7670 | assert( pC!=0 ); |
| 7671 | assert( pC->eCurType==CURTYPE_BTREE ); |
| 7672 | sqlite3BtreeCursorPin(pC->uc.pCursor); |
| 7673 | break; |
| 7674 | } |
| 7675 | |
| 7676 | /* Opcode: CursorUnlock P1 * * * * |
| 7677 | ** |
| 7678 | ** Unlock the btree to which cursor P1 is pointing so that it can be |
| 7679 | ** written by other cursors. |
| 7680 | */ |
| 7681 | case OP_CursorUnlock: { |
| 7682 | VdbeCursor *pC; |
| 7683 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 7684 | pC = p->apCsr[pOp->p1]; |
| 7685 | assert( pC!=0 ); |
| 7686 | assert( pC->eCurType==CURTYPE_BTREE ); |
| 7687 | sqlite3BtreeCursorUnpin(pC->uc.pCursor); |
| 7688 | break; |
| 7689 | } |
| 7690 | |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 7691 | #ifndef SQLITE_OMIT_SHARED_CACHE |
drh | 6a9ad3d | 2008-04-02 16:29:30 +0000 | [diff] [blame] | 7692 | /* Opcode: TableLock P1 P2 P3 P4 * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 7693 | ** Synopsis: iDb=P1 root=P2 write=P3 |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 7694 | ** |
| 7695 | ** Obtain a lock on a particular table. This instruction is only used when |
| 7696 | ** the shared-cache feature is enabled. |
| 7697 | ** |
danielk1977 | 96d48e9 | 2009-06-29 06:00:37 +0000 | [diff] [blame] | 7698 | ** P1 is the index of the database in sqlite3.aDb[] of the database |
drh | 6a9ad3d | 2008-04-02 16:29:30 +0000 | [diff] [blame] | 7699 | ** on which the lock is acquired. A readlock is obtained if P3==0 or |
| 7700 | ** a write lock if P3==1. |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 7701 | ** |
| 7702 | ** P2 contains the root-page of the table to lock. |
| 7703 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 7704 | ** 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] | 7705 | ** used to generate an error message if the lock cannot be obtained. |
| 7706 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 7707 | case OP_TableLock: { |
danielk1977 | e0d9e6f | 2009-07-03 16:25:06 +0000 | [diff] [blame] | 7708 | u8 isWriteLock = (u8)pOp->p3; |
drh | 169dd92 | 2017-06-26 13:57:49 +0000 | [diff] [blame] | 7709 | if( isWriteLock || 0==(db->flags&SQLITE_ReadUncommit) ){ |
danielk1977 | e0d9e6f | 2009-07-03 16:25:06 +0000 | [diff] [blame] | 7710 | int p1 = pOp->p1; |
| 7711 | assert( p1>=0 && p1<db->nDb ); |
drh | a7ab6d8 | 2014-07-21 15:44:39 +0000 | [diff] [blame] | 7712 | assert( DbMaskTest(p->btreeMask, p1) ); |
danielk1977 | e0d9e6f | 2009-07-03 16:25:06 +0000 | [diff] [blame] | 7713 | assert( isWriteLock==0 || isWriteLock==1 ); |
| 7714 | rc = sqlite3BtreeLockTable(db->aDb[p1].pBt, pOp->p2, isWriteLock); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 7715 | if( rc ){ |
| 7716 | if( (rc&0xFF)==SQLITE_LOCKED ){ |
| 7717 | const char *z = pOp->p4.z; |
| 7718 | sqlite3VdbeError(p, "database table is locked: %s", z); |
| 7719 | } |
| 7720 | goto abort_due_to_error; |
danielk1977 | e0d9e6f | 2009-07-03 16:25:06 +0000 | [diff] [blame] | 7721 | } |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 7722 | } |
| 7723 | break; |
| 7724 | } |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 7725 | #endif /* SQLITE_OMIT_SHARED_CACHE */ |
| 7726 | |
| 7727 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 7728 | /* Opcode: VBegin * * * P4 * |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 7729 | ** |
danielk1977 | 3e3a84d | 2008-08-01 17:37:40 +0000 | [diff] [blame] | 7730 | ** P4 may be a pointer to an sqlite3_vtab structure. If so, call the |
| 7731 | ** xBegin method for that table. |
| 7732 | ** |
| 7733 | ** 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] | 7734 | ** within a callback to a virtual table xSync() method. If it is, the error |
| 7735 | ** code will be set to SQLITE_LOCKED. |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 7736 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 7737 | case OP_VBegin: { |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 7738 | VTable *pVTab; |
| 7739 | pVTab = pOp->p4.pVtab; |
| 7740 | rc = sqlite3VtabBegin(db, pVTab); |
dan | 016f781 | 2013-08-21 17:35:48 +0000 | [diff] [blame] | 7741 | if( pVTab ) sqlite3VtabImportErrmsg(p, pVTab->pVtab); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 7742 | if( rc ) goto abort_due_to_error; |
danielk1977 | f9e7dda | 2006-06-16 16:08:53 +0000 | [diff] [blame] | 7743 | break; |
| 7744 | } |
| 7745 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 7746 | |
| 7747 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
dan | 7377945 | 2015-03-19 18:56:17 +0000 | [diff] [blame] | 7748 | /* Opcode: VCreate P1 P2 * * * |
danielk1977 | f9e7dda | 2006-06-16 16:08:53 +0000 | [diff] [blame] | 7749 | ** |
dan | 7377945 | 2015-03-19 18:56:17 +0000 | [diff] [blame] | 7750 | ** P2 is a register that holds the name of a virtual table in database |
| 7751 | ** P1. Call the xCreate method for that table. |
danielk1977 | f9e7dda | 2006-06-16 16:08:53 +0000 | [diff] [blame] | 7752 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 7753 | case OP_VCreate: { |
dan | 7377945 | 2015-03-19 18:56:17 +0000 | [diff] [blame] | 7754 | Mem sMem; /* For storing the record being decoded */ |
drh | 4746406 | 2015-03-21 12:22:16 +0000 | [diff] [blame] | 7755 | const char *zTab; /* Name of the virtual table */ |
| 7756 | |
dan | 7377945 | 2015-03-19 18:56:17 +0000 | [diff] [blame] | 7757 | memset(&sMem, 0, sizeof(sMem)); |
| 7758 | sMem.db = db; |
drh | 4746406 | 2015-03-21 12:22:16 +0000 | [diff] [blame] | 7759 | /* Because P2 is always a static string, it is impossible for the |
| 7760 | ** sqlite3VdbeMemCopy() to fail */ |
| 7761 | assert( (aMem[pOp->p2].flags & MEM_Str)!=0 ); |
| 7762 | assert( (aMem[pOp->p2].flags & MEM_Static)!=0 ); |
dan | 7377945 | 2015-03-19 18:56:17 +0000 | [diff] [blame] | 7763 | rc = sqlite3VdbeMemCopy(&sMem, &aMem[pOp->p2]); |
drh | 4746406 | 2015-03-21 12:22:16 +0000 | [diff] [blame] | 7764 | assert( rc==SQLITE_OK ); |
| 7765 | zTab = (const char*)sqlite3_value_text(&sMem); |
| 7766 | assert( zTab || db->mallocFailed ); |
| 7767 | if( zTab ){ |
| 7768 | rc = sqlite3VtabCallCreate(db, pOp->p1, zTab, &p->zErrMsg); |
dan | 7377945 | 2015-03-19 18:56:17 +0000 | [diff] [blame] | 7769 | } |
| 7770 | sqlite3VdbeMemRelease(&sMem); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 7771 | if( rc ) goto abort_due_to_error; |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 7772 | break; |
| 7773 | } |
| 7774 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 7775 | |
| 7776 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 7777 | /* Opcode: VDestroy P1 * * P4 * |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 7778 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 7779 | ** 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] | 7780 | ** of that table. |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 7781 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 7782 | case OP_VDestroy: { |
drh | 086723a | 2015-03-24 12:51:52 +0000 | [diff] [blame] | 7783 | db->nVDestroy++; |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 7784 | rc = sqlite3VtabCallDestroy(db, pOp->p1, pOp->p4.z); |
drh | 086723a | 2015-03-24 12:51:52 +0000 | [diff] [blame] | 7785 | db->nVDestroy--; |
dan | 1d4b164 | 2018-12-28 17:45:08 +0000 | [diff] [blame] | 7786 | assert( p->errorAction==OE_Abort && p->usesStmtJournal ); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 7787 | if( rc ) goto abort_due_to_error; |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 7788 | break; |
| 7789 | } |
| 7790 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 7791 | |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 7792 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 7793 | /* Opcode: VOpen P1 * * P4 * |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 7794 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 7795 | ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure. |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 7796 | ** P1 is a cursor number. This opcode opens a cursor to the virtual |
| 7797 | ** table and stores that cursor in P1. |
| 7798 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 7799 | case OP_VOpen: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 7800 | VdbeCursor *pCur; |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 7801 | sqlite3_vtab_cursor *pVCur; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 7802 | sqlite3_vtab *pVtab; |
drh | f496a7d | 2015-03-24 14:05:50 +0000 | [diff] [blame] | 7803 | const sqlite3_module *pModule; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 7804 | |
drh | 1713afb | 2013-06-28 01:24:57 +0000 | [diff] [blame] | 7805 | assert( p->bIsReader ); |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 7806 | pCur = 0; |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 7807 | pVCur = 0; |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 7808 | pVtab = pOp->p4.pVtab->pVtab; |
drh | f496a7d | 2015-03-24 14:05:50 +0000 | [diff] [blame] | 7809 | if( pVtab==0 || NEVER(pVtab->pModule==0) ){ |
| 7810 | rc = SQLITE_LOCKED; |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 7811 | goto abort_due_to_error; |
drh | f496a7d | 2015-03-24 14:05:50 +0000 | [diff] [blame] | 7812 | } |
| 7813 | pModule = pVtab->pModule; |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 7814 | rc = pModule->xOpen(pVtab, &pVCur); |
dan | 016f781 | 2013-08-21 17:35:48 +0000 | [diff] [blame] | 7815 | sqlite3VtabImportErrmsg(p, pVtab); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 7816 | if( rc ) goto abort_due_to_error; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 7817 | |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 7818 | /* Initialize sqlite3_vtab_cursor base class */ |
| 7819 | pVCur->pVtab = pVtab; |
| 7820 | |
| 7821 | /* Initialize vdbe cursor object */ |
drh | b248668 | 2022-01-03 01:43:28 +0000 | [diff] [blame] | 7822 | pCur = allocateCursor(p, pOp->p1, 0, CURTYPE_VTAB); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 7823 | if( pCur ){ |
| 7824 | pCur->uc.pVCur = pVCur; |
| 7825 | pVtab->nRef++; |
| 7826 | }else{ |
| 7827 | assert( db->mallocFailed ); |
| 7828 | pModule->xClose(pVCur); |
| 7829 | goto no_mem; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 7830 | } |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 7831 | break; |
| 7832 | } |
| 7833 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 7834 | |
| 7835 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | 0fe7e7d | 2022-02-01 14:58:29 +0000 | [diff] [blame] | 7836 | /* Opcode: VInitIn P1 P2 P3 * * |
drh | 30e314e | 2022-02-02 14:36:58 +0000 | [diff] [blame] | 7837 | ** Synopsis: r[P2]=ValueList(P1,P3) |
drh | 0fe7e7d | 2022-02-01 14:58:29 +0000 | [diff] [blame] | 7838 | ** |
drh | 30e314e | 2022-02-02 14:36:58 +0000 | [diff] [blame] | 7839 | ** Set register P2 to be a pointer to a ValueList object for cursor P1 |
| 7840 | ** with cache register P3 and output register P3+1. This ValueList object |
| 7841 | ** can be used as the first argument to sqlite3_vtab_in_first() and |
| 7842 | ** sqlite3_vtab_in_next() to extract all of the values stored in the P1 |
| 7843 | ** cursor. Register P3 is used to hold the values returned by |
| 7844 | ** sqlite3_vtab_in_first() and sqlite3_vtab_in_next(). |
drh | 0fe7e7d | 2022-02-01 14:58:29 +0000 | [diff] [blame] | 7845 | */ |
| 7846 | case OP_VInitIn: { /* out2 */ |
drh | 30e314e | 2022-02-02 14:36:58 +0000 | [diff] [blame] | 7847 | VdbeCursor *pC; /* The cursor containing the RHS values */ |
| 7848 | ValueList *pRhs; /* New ValueList object to put in reg[P2] */ |
| 7849 | |
drh | 0fe7e7d | 2022-02-01 14:58:29 +0000 | [diff] [blame] | 7850 | pC = p->apCsr[pOp->p1]; |
drh | 30e314e | 2022-02-02 14:36:58 +0000 | [diff] [blame] | 7851 | pRhs = sqlite3_malloc64( sizeof(*pRhs) ); |
| 7852 | if( pRhs==0 ) goto no_mem; |
| 7853 | pRhs->pCsr = pC->uc.pCursor; |
| 7854 | pRhs->pOut = &aMem[pOp->p3]; |
drh | 0fe7e7d | 2022-02-01 14:58:29 +0000 | [diff] [blame] | 7855 | pOut = out2Prerelease(p, pOp); |
drh | 0fe7e7d | 2022-02-01 14:58:29 +0000 | [diff] [blame] | 7856 | pOut->flags = MEM_Null; |
drh | 30e314e | 2022-02-02 14:36:58 +0000 | [diff] [blame] | 7857 | sqlite3VdbeMemSetPointer(pOut, pRhs, "ValueList", sqlite3_free); |
drh | 0fe7e7d | 2022-02-01 14:58:29 +0000 | [diff] [blame] | 7858 | break; |
| 7859 | } |
| 7860 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 7861 | |
| 7862 | |
| 7863 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
danielk1977 | 6dbee81 | 2008-01-03 18:39:41 +0000 | [diff] [blame] | 7864 | /* Opcode: VFilter P1 P2 P3 P4 * |
drh | 831116d | 2014-04-03 14:31:00 +0000 | [diff] [blame] | 7865 | ** Synopsis: iplan=r[P3] zplan='P4' |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 7866 | ** |
| 7867 | ** P1 is a cursor opened using VOpen. P2 is an address to jump to if |
| 7868 | ** the filtered result set is empty. |
| 7869 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 7870 | ** P4 is either NULL or a string that was generated by the xBestIndex |
| 7871 | ** method of the module. The interpretation of the P4 string is left |
drh | 4be8b51 | 2006-06-13 23:51:34 +0000 | [diff] [blame] | 7872 | ** to the module implementation. |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 7873 | ** |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 7874 | ** This opcode invokes the xFilter method on the virtual table specified |
danielk1977 | 6dbee81 | 2008-01-03 18:39:41 +0000 | [diff] [blame] | 7875 | ** by P1. The integer query plan parameter to xFilter is stored in register |
| 7876 | ** P3. Register P3+1 stores the argc parameter to be passed to the |
drh | 174edc6 | 2008-05-29 05:23:41 +0000 | [diff] [blame] | 7877 | ** xFilter method. Registers P3+2..P3+1+argc are the argc |
| 7878 | ** additional parameters which are passed to |
danielk1977 | 6dbee81 | 2008-01-03 18:39:41 +0000 | [diff] [blame] | 7879 | ** xFilter as argv. Register P3+2 becomes argv[0] when passed to xFilter. |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 7880 | ** |
danielk1977 | 6dbee81 | 2008-01-03 18:39:41 +0000 | [diff] [blame] | 7881 | ** 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] | 7882 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 7883 | case OP_VFilter: { /* jump */ |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 7884 | int nArg; |
danielk1977 | 6dbee81 | 2008-01-03 18:39:41 +0000 | [diff] [blame] | 7885 | int iQuery; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 7886 | const sqlite3_module *pModule; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 7887 | Mem *pQuery; |
| 7888 | Mem *pArgc; |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 7889 | sqlite3_vtab_cursor *pVCur; |
drh | 4dc754d | 2008-07-23 18:17:32 +0000 | [diff] [blame] | 7890 | sqlite3_vtab *pVtab; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 7891 | VdbeCursor *pCur; |
| 7892 | int res; |
| 7893 | int i; |
| 7894 | Mem **apArg; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 7895 | |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 7896 | pQuery = &aMem[pOp->p3]; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 7897 | pArgc = &pQuery[1]; |
| 7898 | pCur = p->apCsr[pOp->p1]; |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 7899 | assert( memIsValid(pQuery) ); |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 7900 | REGISTER_TRACE(pOp->p3, pQuery); |
drh | 3ab4ffc | 2021-11-11 11:23:08 +0000 | [diff] [blame] | 7901 | assert( pCur!=0 ); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 7902 | assert( pCur->eCurType==CURTYPE_VTAB ); |
| 7903 | pVCur = pCur->uc.pVCur; |
| 7904 | pVtab = pVCur->pVtab; |
drh | 4dc754d | 2008-07-23 18:17:32 +0000 | [diff] [blame] | 7905 | pModule = pVtab->pModule; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 7906 | |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 7907 | /* Grab the index number and argc parameters */ |
danielk1977 | 6dbee81 | 2008-01-03 18:39:41 +0000 | [diff] [blame] | 7908 | assert( (pQuery->flags&MEM_Int)!=0 && pArgc->flags==MEM_Int ); |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 7909 | nArg = (int)pArgc->u.i; |
| 7910 | iQuery = (int)pQuery->u.i; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 7911 | |
drh | 644a529 | 2006-12-20 14:53:38 +0000 | [diff] [blame] | 7912 | /* Invoke the xFilter method */ |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 7913 | apArg = p->apArg; |
| 7914 | for(i = 0; i<nArg; i++){ |
| 7915 | apArg[i] = &pArgc[i+1]; |
| 7916 | } |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 7917 | rc = pModule->xFilter(pVCur, iQuery, pOp->p4.z, nArg, apArg); |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 7918 | sqlite3VtabImportErrmsg(p, pVtab); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 7919 | if( rc ) goto abort_due_to_error; |
| 7920 | res = pModule->xEof(pVCur); |
drh | 1d454a3 | 2008-01-31 19:34:51 +0000 | [diff] [blame] | 7921 | pCur->nullRow = 0; |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 7922 | VdbeBranchTaken(res!=0,2); |
| 7923 | if( res ) goto jump_to_p2; |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 7924 | break; |
| 7925 | } |
| 7926 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 7927 | |
| 7928 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | ce2fbd1 | 2018-01-12 21:00:14 +0000 | [diff] [blame] | 7929 | /* Opcode: VColumn P1 P2 P3 * P5 |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 7930 | ** Synopsis: r[P3]=vcolumn(P2) |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 7931 | ** |
drh | 6f390be | 2018-01-11 17:04:26 +0000 | [diff] [blame] | 7932 | ** Store in register P3 the value of the P2-th column of |
| 7933 | ** the current row of the virtual-table of cursor P1. |
| 7934 | ** |
| 7935 | ** If the VColumn opcode is being used to fetch the value of |
drh | ce2fbd1 | 2018-01-12 21:00:14 +0000 | [diff] [blame] | 7936 | ** an unchanging column during an UPDATE operation, then the P5 |
drh | 09d00b2 | 2018-09-27 20:20:01 +0000 | [diff] [blame] | 7937 | ** value is OPFLAG_NOCHNG. This will cause the sqlite3_vtab_nochange() |
| 7938 | ** function to return true inside the xColumn method of the virtual |
| 7939 | ** table implementation. The P5 column might also contain other |
| 7940 | ** bits (OPFLAG_LENGTHARG or OPFLAG_TYPEOFARG) but those bits are |
| 7941 | ** unused by OP_VColumn. |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 7942 | */ |
| 7943 | case OP_VColumn: { |
danielk1977 | 3e3a84d | 2008-08-01 17:37:40 +0000 | [diff] [blame] | 7944 | sqlite3_vtab *pVtab; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 7945 | const sqlite3_module *pModule; |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 7946 | Mem *pDest; |
| 7947 | sqlite3_context sContext; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 7948 | |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 7949 | VdbeCursor *pCur = p->apCsr[pOp->p1]; |
drh | 3ab4ffc | 2021-11-11 11:23:08 +0000 | [diff] [blame] | 7950 | assert( pCur!=0 ); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 7951 | assert( pCur->eCurType==CURTYPE_VTAB ); |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 7952 | assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) ); |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 7953 | pDest = &aMem[pOp->p3]; |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 7954 | memAboutToChange(p, pDest); |
drh | 2945b4a | 2008-01-31 15:53:45 +0000 | [diff] [blame] | 7955 | if( pCur->nullRow ){ |
| 7956 | sqlite3VdbeMemSetNull(pDest); |
| 7957 | break; |
| 7958 | } |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 7959 | pVtab = pCur->uc.pVCur->pVtab; |
danielk1977 | 3e3a84d | 2008-08-01 17:37:40 +0000 | [diff] [blame] | 7960 | pModule = pVtab->pModule; |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 7961 | assert( pModule->xColumn ); |
| 7962 | memset(&sContext, 0, sizeof(sContext)); |
drh | 9bd038f | 2014-08-27 14:14:06 +0000 | [diff] [blame] | 7963 | sContext.pOut = pDest; |
drh | 659fdb4 | 2022-04-01 15:31:58 +0000 | [diff] [blame] | 7964 | sContext.enc = encoding; |
drh | 75f1076 | 2019-12-14 18:08:22 +0000 | [diff] [blame] | 7965 | assert( pOp->p5==OPFLAG_NOCHNG || pOp->p5==0 ); |
drh | 09d00b2 | 2018-09-27 20:20:01 +0000 | [diff] [blame] | 7966 | if( pOp->p5 & OPFLAG_NOCHNG ){ |
drh | ce2fbd1 | 2018-01-12 21:00:14 +0000 | [diff] [blame] | 7967 | sqlite3VdbeMemSetNull(pDest); |
| 7968 | pDest->flags = MEM_Null|MEM_Zero; |
| 7969 | pDest->u.nZero = 0; |
| 7970 | }else{ |
| 7971 | MemSetTypeFlag(pDest, MEM_Null); |
| 7972 | } |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 7973 | rc = pModule->xColumn(pCur->uc.pVCur, &sContext, pOp->p2); |
dan | 016f781 | 2013-08-21 17:35:48 +0000 | [diff] [blame] | 7974 | sqlite3VtabImportErrmsg(p, pVtab); |
drh | f09ac0b | 2018-01-23 03:44:06 +0000 | [diff] [blame] | 7975 | if( sContext.isError>0 ){ |
dan | 099fa84 | 2018-01-30 18:33:23 +0000 | [diff] [blame] | 7976 | sqlite3VdbeError(p, "%s", sqlite3_value_text(pDest)); |
drh | 4c8555f | 2009-06-25 01:47:11 +0000 | [diff] [blame] | 7977 | rc = sContext.isError; |
| 7978 | } |
drh | 9bd038f | 2014-08-27 14:14:06 +0000 | [diff] [blame] | 7979 | sqlite3VdbeChangeEncoding(pDest, encoding); |
drh | 5ff4437 | 2009-11-24 16:26:17 +0000 | [diff] [blame] | 7980 | REGISTER_TRACE(pOp->p3, pDest); |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 7981 | UPDATE_MAX_BLOBSIZE(pDest); |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 7982 | |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 7983 | if( rc ) goto abort_due_to_error; |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 7984 | break; |
| 7985 | } |
| 7986 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 7987 | |
| 7988 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 7989 | /* Opcode: VNext P1 P2 * * * |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 7990 | ** |
| 7991 | ** Advance virtual table P1 to the next row in its result set and |
| 7992 | ** jump to instruction P2. Or, if the virtual table has reached |
| 7993 | ** the end of its result set, then fall through to the next instruction. |
| 7994 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 7995 | case OP_VNext: { /* jump */ |
danielk1977 | 3e3a84d | 2008-08-01 17:37:40 +0000 | [diff] [blame] | 7996 | sqlite3_vtab *pVtab; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 7997 | const sqlite3_module *pModule; |
drh | c54a617 | 2009-06-02 16:06:03 +0000 | [diff] [blame] | 7998 | int res; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 7999 | VdbeCursor *pCur; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 8000 | |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 8001 | pCur = p->apCsr[pOp->p1]; |
drh | 3ab4ffc | 2021-11-11 11:23:08 +0000 | [diff] [blame] | 8002 | assert( pCur!=0 ); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 8003 | assert( pCur->eCurType==CURTYPE_VTAB ); |
drh | 2945b4a | 2008-01-31 15:53:45 +0000 | [diff] [blame] | 8004 | if( pCur->nullRow ){ |
| 8005 | break; |
| 8006 | } |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 8007 | pVtab = pCur->uc.pVCur->pVtab; |
danielk1977 | 3e3a84d | 2008-08-01 17:37:40 +0000 | [diff] [blame] | 8008 | pModule = pVtab->pModule; |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 8009 | assert( pModule->xNext ); |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 8010 | |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 8011 | /* Invoke the xNext() method of the module. There is no way for the |
| 8012 | ** underlying implementation to return an error if one occurs during |
| 8013 | ** xNext(). Instead, if an error occurs, true is returned (indicating that |
| 8014 | ** data is available) and the error code returned when xColumn or |
| 8015 | ** some other method is next invoked on the save virtual table cursor. |
| 8016 | */ |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 8017 | rc = pModule->xNext(pCur->uc.pVCur); |
dan | 016f781 | 2013-08-21 17:35:48 +0000 | [diff] [blame] | 8018 | sqlite3VtabImportErrmsg(p, pVtab); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 8019 | if( rc ) goto abort_due_to_error; |
| 8020 | res = pModule->xEof(pCur->uc.pVCur); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 8021 | VdbeBranchTaken(!res,2); |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 8022 | if( !res ){ |
| 8023 | /* If there is data, jump to P2 */ |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 8024 | goto jump_to_p2_and_check_for_interrupt; |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 8025 | } |
drh | 49afe3a | 2013-07-10 03:05:14 +0000 | [diff] [blame] | 8026 | goto check_for_interrupt; |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 8027 | } |
| 8028 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 8029 | |
danielk1977 | 182c4ba | 2007-06-27 15:53:34 +0000 | [diff] [blame] | 8030 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 8031 | /* Opcode: VRename P1 * * P4 * |
danielk1977 | 182c4ba | 2007-06-27 15:53:34 +0000 | [diff] [blame] | 8032 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 8033 | ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure. |
danielk1977 | 182c4ba | 2007-06-27 15:53:34 +0000 | [diff] [blame] | 8034 | ** This opcode invokes the corresponding xRename method. The value |
danielk1977 | 6dbee81 | 2008-01-03 18:39:41 +0000 | [diff] [blame] | 8035 | ** in register P1 is passed as the zName argument to the xRename method. |
danielk1977 | 182c4ba | 2007-06-27 15:53:34 +0000 | [diff] [blame] | 8036 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 8037 | case OP_VRename: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 8038 | sqlite3_vtab *pVtab; |
| 8039 | Mem *pName; |
dan | 34566c4 | 2018-09-20 17:21:21 +0000 | [diff] [blame] | 8040 | int isLegacy; |
| 8041 | |
| 8042 | isLegacy = (db->flags & SQLITE_LegacyAlter); |
| 8043 | db->flags |= SQLITE_LegacyAlter; |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 8044 | pVtab = pOp->p4.pVtab->pVtab; |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 8045 | pName = &aMem[pOp->p1]; |
danielk1977 | 182c4ba | 2007-06-27 15:53:34 +0000 | [diff] [blame] | 8046 | assert( pVtab->pModule->xRename ); |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 8047 | assert( memIsValid(pName) ); |
drh | 9e92a47 | 2013-06-27 17:40:30 +0000 | [diff] [blame] | 8048 | assert( p->readOnly==0 ); |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 8049 | REGISTER_TRACE(pOp->p1, pName); |
drh | 35f6b93 | 2009-06-23 14:15:04 +0000 | [diff] [blame] | 8050 | assert( pName->flags & MEM_Str ); |
drh | 98655a6 | 2011-10-18 22:07:47 +0000 | [diff] [blame] | 8051 | testcase( pName->enc==SQLITE_UTF8 ); |
| 8052 | testcase( pName->enc==SQLITE_UTF16BE ); |
| 8053 | testcase( pName->enc==SQLITE_UTF16LE ); |
| 8054 | rc = sqlite3VdbeChangeEncoding(pName, SQLITE_UTF8); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 8055 | if( rc ) goto abort_due_to_error; |
| 8056 | rc = pVtab->pModule->xRename(pVtab, pName->z); |
drh | d5b44d6 | 2018-12-06 17:06:02 +0000 | [diff] [blame] | 8057 | if( isLegacy==0 ) db->flags &= ~(u64)SQLITE_LegacyAlter; |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 8058 | sqlite3VtabImportErrmsg(p, pVtab); |
| 8059 | p->expired = 0; |
| 8060 | if( rc ) goto abort_due_to_error; |
danielk1977 | 182c4ba | 2007-06-27 15:53:34 +0000 | [diff] [blame] | 8061 | break; |
| 8062 | } |
| 8063 | #endif |
drh | 4cbdda9 | 2006-06-14 19:00:20 +0000 | [diff] [blame] | 8064 | |
| 8065 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | 0fd6135 | 2014-02-07 02:29:45 +0000 | [diff] [blame] | 8066 | /* Opcode: VUpdate P1 P2 P3 P4 P5 |
drh | f63552b | 2013-10-30 00:25:03 +0000 | [diff] [blame] | 8067 | ** Synopsis: data=r[P3@P2] |
danielk1977 | 399918f | 2006-06-14 13:03:23 +0000 | [diff] [blame] | 8068 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 8069 | ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure. |
danielk1977 | 399918f | 2006-06-14 13:03:23 +0000 | [diff] [blame] | 8070 | ** This opcode invokes the corresponding xUpdate method. P2 values |
danielk1977 | 2a339ff | 2008-01-03 17:31:44 +0000 | [diff] [blame] | 8071 | ** are contiguous memory cells starting at P3 to pass to the xUpdate |
| 8072 | ** invocation. The value in register (P3+P2-1) corresponds to the |
| 8073 | ** p2th element of the argv array passed to xUpdate. |
drh | 4cbdda9 | 2006-06-14 19:00:20 +0000 | [diff] [blame] | 8074 | ** |
| 8075 | ** The xUpdate method will do a DELETE or an INSERT or both. |
danielk1977 | 2a339ff | 2008-01-03 17:31:44 +0000 | [diff] [blame] | 8076 | ** The argv[0] element (which corresponds to memory cell P3) |
| 8077 | ** is the rowid of a row to delete. If argv[0] is NULL then no |
| 8078 | ** deletion occurs. The argv[1] element is the rowid of the new |
| 8079 | ** row. This can be NULL to have the virtual table select the new |
| 8080 | ** rowid for itself. The subsequent elements in the array are |
| 8081 | ** the values of columns in the new row. |
drh | 4cbdda9 | 2006-06-14 19:00:20 +0000 | [diff] [blame] | 8082 | ** |
| 8083 | ** If P2==1 then no insert is performed. argv[0] is the rowid of |
| 8084 | ** a row to delete. |
danielk1977 | 1f6eec5 | 2006-06-16 06:17:47 +0000 | [diff] [blame] | 8085 | ** |
| 8086 | ** P1 is a boolean flag. If it is set to true and the xUpdate call |
| 8087 | ** is successful, then the value returned by sqlite3_last_insert_rowid() |
| 8088 | ** is set to the value of the rowid for the row just inserted. |
drh | 0fd6135 | 2014-02-07 02:29:45 +0000 | [diff] [blame] | 8089 | ** |
| 8090 | ** P5 is the error actions (OE_Replace, OE_Fail, OE_Ignore, etc) to |
| 8091 | ** apply in the case of a constraint failure on an insert or update. |
danielk1977 | 399918f | 2006-06-14 13:03:23 +0000 | [diff] [blame] | 8092 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 8093 | case OP_VUpdate: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 8094 | sqlite3_vtab *pVtab; |
drh | f496a7d | 2015-03-24 14:05:50 +0000 | [diff] [blame] | 8095 | const sqlite3_module *pModule; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 8096 | int nArg; |
| 8097 | int i; |
drh | 9135ebb | 2021-11-11 23:52:44 +0000 | [diff] [blame] | 8098 | sqlite_int64 rowid = 0; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 8099 | Mem **apArg; |
| 8100 | Mem *pX; |
| 8101 | |
dan | b061d05 | 2011-04-25 18:49:57 +0000 | [diff] [blame] | 8102 | assert( pOp->p2==1 || pOp->p5==OE_Fail || pOp->p5==OE_Rollback |
| 8103 | || pOp->p5==OE_Abort || pOp->p5==OE_Ignore || pOp->p5==OE_Replace |
| 8104 | ); |
drh | 9e92a47 | 2013-06-27 17:40:30 +0000 | [diff] [blame] | 8105 | assert( p->readOnly==0 ); |
dan | 466ea9b | 2018-06-13 11:11:13 +0000 | [diff] [blame] | 8106 | if( db->mallocFailed ) goto no_mem; |
drh | 4031baf | 2018-05-28 17:31:20 +0000 | [diff] [blame] | 8107 | sqlite3VdbeIncrWriteCounter(p, 0); |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 8108 | pVtab = pOp->p4.pVtab->pVtab; |
drh | f496a7d | 2015-03-24 14:05:50 +0000 | [diff] [blame] | 8109 | if( pVtab==0 || NEVER(pVtab->pModule==0) ){ |
| 8110 | rc = SQLITE_LOCKED; |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 8111 | goto abort_due_to_error; |
drh | f496a7d | 2015-03-24 14:05:50 +0000 | [diff] [blame] | 8112 | } |
| 8113 | pModule = pVtab->pModule; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 8114 | nArg = pOp->p2; |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 8115 | assert( pOp->p4type==P4_VTAB ); |
drh | 35f6b93 | 2009-06-23 14:15:04 +0000 | [diff] [blame] | 8116 | if( ALWAYS(pModule->xUpdate) ){ |
dan | b061d05 | 2011-04-25 18:49:57 +0000 | [diff] [blame] | 8117 | u8 vtabOnConflict = db->vtabOnConflict; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 8118 | apArg = p->apArg; |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 8119 | pX = &aMem[pOp->p3]; |
danielk1977 | 2a339ff | 2008-01-03 17:31:44 +0000 | [diff] [blame] | 8120 | for(i=0; i<nArg; i++){ |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 8121 | assert( memIsValid(pX) ); |
| 8122 | memAboutToChange(p, pX); |
drh | 9c41938 | 2006-06-16 21:13:21 +0000 | [diff] [blame] | 8123 | apArg[i] = pX; |
danielk1977 | 2a339ff | 2008-01-03 17:31:44 +0000 | [diff] [blame] | 8124 | pX++; |
danielk1977 | 399918f | 2006-06-14 13:03:23 +0000 | [diff] [blame] | 8125 | } |
dan | b061d05 | 2011-04-25 18:49:57 +0000 | [diff] [blame] | 8126 | db->vtabOnConflict = pOp->p5; |
danielk1977 | 1f6eec5 | 2006-06-16 06:17:47 +0000 | [diff] [blame] | 8127 | rc = pModule->xUpdate(pVtab, nArg, apArg, &rowid); |
dan | b061d05 | 2011-04-25 18:49:57 +0000 | [diff] [blame] | 8128 | db->vtabOnConflict = vtabOnConflict; |
dan | 016f781 | 2013-08-21 17:35:48 +0000 | [diff] [blame] | 8129 | sqlite3VtabImportErrmsg(p, pVtab); |
drh | 35f6b93 | 2009-06-23 14:15:04 +0000 | [diff] [blame] | 8130 | if( rc==SQLITE_OK && pOp->p1 ){ |
danielk1977 | 1f6eec5 | 2006-06-16 06:17:47 +0000 | [diff] [blame] | 8131 | assert( nArg>1 && apArg[0] && (apArg[0]->flags&MEM_Null) ); |
drh | fae58d5 | 2017-01-26 17:26:44 +0000 | [diff] [blame] | 8132 | db->lastRowid = rowid; |
danielk1977 | 1f6eec5 | 2006-06-16 06:17:47 +0000 | [diff] [blame] | 8133 | } |
drh | d91c1a1 | 2013-02-09 13:58:25 +0000 | [diff] [blame] | 8134 | if( (rc&0xff)==SQLITE_CONSTRAINT && pOp->p4.pVtab->bConstraint ){ |
dan | b061d05 | 2011-04-25 18:49:57 +0000 | [diff] [blame] | 8135 | if( pOp->p5==OE_Ignore ){ |
| 8136 | rc = SQLITE_OK; |
| 8137 | }else{ |
| 8138 | p->errorAction = ((pOp->p5==OE_Replace) ? OE_Abort : pOp->p5); |
| 8139 | } |
| 8140 | }else{ |
| 8141 | p->nChange++; |
| 8142 | } |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 8143 | if( rc ) goto abort_due_to_error; |
danielk1977 | 399918f | 2006-06-14 13:03:23 +0000 | [diff] [blame] | 8144 | } |
drh | 4cbdda9 | 2006-06-14 19:00:20 +0000 | [diff] [blame] | 8145 | break; |
danielk1977 | 399918f | 2006-06-14 13:03:23 +0000 | [diff] [blame] | 8146 | } |
| 8147 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 8148 | |
danielk1977 | 59a9379 | 2008-05-15 17:48:20 +0000 | [diff] [blame] | 8149 | #ifndef SQLITE_OMIT_PAGER_PRAGMAS |
| 8150 | /* Opcode: Pagecount P1 P2 * * * |
| 8151 | ** |
| 8152 | ** Write the current number of pages in database P1 to memory cell P2. |
| 8153 | */ |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 8154 | case OP_Pagecount: { /* out2 */ |
| 8155 | pOut = out2Prerelease(p, pOp); |
drh | b129915 | 2010-03-30 22:58:33 +0000 | [diff] [blame] | 8156 | pOut->u.i = sqlite3BtreeLastPage(db->aDb[pOp->p1].pBt); |
danielk1977 | 59a9379 | 2008-05-15 17:48:20 +0000 | [diff] [blame] | 8157 | break; |
| 8158 | } |
| 8159 | #endif |
| 8160 | |
drh | 60ac3f4 | 2010-11-23 18:59:27 +0000 | [diff] [blame] | 8161 | |
| 8162 | #ifndef SQLITE_OMIT_PAGER_PRAGMAS |
| 8163 | /* Opcode: MaxPgcnt P1 P2 P3 * * |
| 8164 | ** |
| 8165 | ** 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] | 8166 | ** Do not let the maximum page count fall below the current page count and |
| 8167 | ** do not change the maximum page count value if P3==0. |
| 8168 | ** |
drh | 60ac3f4 | 2010-11-23 18:59:27 +0000 | [diff] [blame] | 8169 | ** Store the maximum page count after the change in register P2. |
| 8170 | */ |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 8171 | case OP_MaxPgcnt: { /* out2 */ |
drh | c84e033 | 2010-11-23 20:25:08 +0000 | [diff] [blame] | 8172 | unsigned int newMax; |
drh | 60ac3f4 | 2010-11-23 18:59:27 +0000 | [diff] [blame] | 8173 | Btree *pBt; |
| 8174 | |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 8175 | pOut = out2Prerelease(p, pOp); |
drh | 60ac3f4 | 2010-11-23 18:59:27 +0000 | [diff] [blame] | 8176 | pBt = db->aDb[pOp->p1].pBt; |
drh | c84e033 | 2010-11-23 20:25:08 +0000 | [diff] [blame] | 8177 | newMax = 0; |
| 8178 | if( pOp->p3 ){ |
| 8179 | newMax = sqlite3BtreeLastPage(pBt); |
drh | 6ea28d6 | 2010-11-26 16:49:59 +0000 | [diff] [blame] | 8180 | if( newMax < (unsigned)pOp->p3 ) newMax = (unsigned)pOp->p3; |
drh | c84e033 | 2010-11-23 20:25:08 +0000 | [diff] [blame] | 8181 | } |
| 8182 | pOut->u.i = sqlite3BtreeMaxPageCount(pBt, newMax); |
drh | 60ac3f4 | 2010-11-23 18:59:27 +0000 | [diff] [blame] | 8183 | break; |
| 8184 | } |
| 8185 | #endif |
| 8186 | |
drh | 920cf59 | 2019-10-30 16:29:02 +0000 | [diff] [blame] | 8187 | /* Opcode: Function P1 P2 P3 P4 * |
drh | d7b10d7 | 2020-02-01 17:38:24 +0000 | [diff] [blame] | 8188 | ** Synopsis: r[P3]=func(r[P2@NP]) |
drh | 3e34eab | 2017-07-19 19:48:40 +0000 | [diff] [blame] | 8189 | ** |
| 8190 | ** Invoke a user function (P4 is a pointer to an sqlite3_context object that |
drh | 920cf59 | 2019-10-30 16:29:02 +0000 | [diff] [blame] | 8191 | ** contains a pointer to the function to be run) with arguments taken |
| 8192 | ** from register P2 and successors. The number of arguments is in |
| 8193 | ** the sqlite3_context object that P4 points to. |
| 8194 | ** The result of the function is stored |
drh | 3e34eab | 2017-07-19 19:48:40 +0000 | [diff] [blame] | 8195 | ** in register P3. Register P3 must not be one of the function inputs. |
| 8196 | ** |
| 8197 | ** P1 is a 32-bit bitmask indicating whether or not each argument to the |
| 8198 | ** function was determined to be constant at compile time. If the first |
| 8199 | ** argument was constant then bit 0 of P1 is set. This is used to determine |
| 8200 | ** whether meta data associated with a user function argument using the |
| 8201 | ** sqlite3_set_auxdata() API may be safely retained until the next |
| 8202 | ** invocation of this opcode. |
| 8203 | ** |
drh | 920cf59 | 2019-10-30 16:29:02 +0000 | [diff] [blame] | 8204 | ** See also: AggStep, AggFinal, PureFunc |
drh | 3e34eab | 2017-07-19 19:48:40 +0000 | [diff] [blame] | 8205 | */ |
drh | 920cf59 | 2019-10-30 16:29:02 +0000 | [diff] [blame] | 8206 | /* Opcode: PureFunc P1 P2 P3 P4 * |
drh | d7b10d7 | 2020-02-01 17:38:24 +0000 | [diff] [blame] | 8207 | ** Synopsis: r[P3]=func(r[P2@NP]) |
drh | 920cf59 | 2019-10-30 16:29:02 +0000 | [diff] [blame] | 8208 | ** |
| 8209 | ** Invoke a user function (P4 is a pointer to an sqlite3_context object that |
| 8210 | ** contains a pointer to the function to be run) with arguments taken |
| 8211 | ** from register P2 and successors. The number of arguments is in |
| 8212 | ** the sqlite3_context object that P4 points to. |
| 8213 | ** The result of the function is stored |
| 8214 | ** in register P3. Register P3 must not be one of the function inputs. |
| 8215 | ** |
| 8216 | ** P1 is a 32-bit bitmask indicating whether or not each argument to the |
| 8217 | ** function was determined to be constant at compile time. If the first |
| 8218 | ** argument was constant then bit 0 of P1 is set. This is used to determine |
| 8219 | ** whether meta data associated with a user function argument using the |
| 8220 | ** sqlite3_set_auxdata() API may be safely retained until the next |
| 8221 | ** invocation of this opcode. |
| 8222 | ** |
| 8223 | ** This opcode works exactly like OP_Function. The only difference is in |
| 8224 | ** its name. This opcode is used in places where the function must be |
| 8225 | ** purely non-deterministic. Some built-in date/time functions can be |
| 8226 | ** either determinitic of non-deterministic, depending on their arguments. |
| 8227 | ** When those function are used in a non-deterministic way, they will check |
| 8228 | ** to see if they were called using OP_PureFunc instead of OP_Function, and |
| 8229 | ** if they were, they throw an error. |
| 8230 | ** |
| 8231 | ** See also: AggStep, AggFinal, Function |
| 8232 | */ |
mistachkin | 758784d | 2018-07-25 15:12:29 +0000 | [diff] [blame] | 8233 | case OP_PureFunc: /* group */ |
| 8234 | case OP_Function: { /* group */ |
drh | 3e34eab | 2017-07-19 19:48:40 +0000 | [diff] [blame] | 8235 | int i; |
| 8236 | sqlite3_context *pCtx; |
| 8237 | |
| 8238 | assert( pOp->p4type==P4_FUNCCTX ); |
| 8239 | pCtx = pOp->p4.pCtx; |
| 8240 | |
| 8241 | /* If this function is inside of a trigger, the register array in aMem[] |
| 8242 | ** might change from one evaluation to the next. The next block of code |
| 8243 | ** checks to see if the register array has changed, and if so it |
| 8244 | ** reinitializes the relavant parts of the sqlite3_context object */ |
| 8245 | pOut = &aMem[pOp->p3]; |
| 8246 | if( pCtx->pOut != pOut ){ |
drh | 920cf59 | 2019-10-30 16:29:02 +0000 | [diff] [blame] | 8247 | pCtx->pVdbe = p; |
drh | 3e34eab | 2017-07-19 19:48:40 +0000 | [diff] [blame] | 8248 | pCtx->pOut = pOut; |
drh | 659fdb4 | 2022-04-01 15:31:58 +0000 | [diff] [blame] | 8249 | pCtx->enc = encoding; |
drh | 3e34eab | 2017-07-19 19:48:40 +0000 | [diff] [blame] | 8250 | for(i=pCtx->argc-1; i>=0; i--) pCtx->argv[i] = &aMem[pOp->p2+i]; |
| 8251 | } |
drh | 920cf59 | 2019-10-30 16:29:02 +0000 | [diff] [blame] | 8252 | assert( pCtx->pVdbe==p ); |
drh | 3e34eab | 2017-07-19 19:48:40 +0000 | [diff] [blame] | 8253 | |
| 8254 | memAboutToChange(p, pOut); |
| 8255 | #ifdef SQLITE_DEBUG |
| 8256 | for(i=0; i<pCtx->argc; i++){ |
| 8257 | assert( memIsValid(pCtx->argv[i]) ); |
| 8258 | REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]); |
| 8259 | } |
| 8260 | #endif |
| 8261 | MemSetTypeFlag(pOut, MEM_Null); |
drh | f09ac0b | 2018-01-23 03:44:06 +0000 | [diff] [blame] | 8262 | assert( pCtx->isError==0 ); |
drh | 3e34eab | 2017-07-19 19:48:40 +0000 | [diff] [blame] | 8263 | (*pCtx->pFunc->xSFunc)(pCtx, pCtx->argc, pCtx->argv);/* IMP: R-24505-23230 */ |
| 8264 | |
| 8265 | /* If the function returned an error, throw an exception */ |
drh | f09ac0b | 2018-01-23 03:44:06 +0000 | [diff] [blame] | 8266 | if( pCtx->isError ){ |
| 8267 | if( pCtx->isError>0 ){ |
drh | 3e34eab | 2017-07-19 19:48:40 +0000 | [diff] [blame] | 8268 | sqlite3VdbeError(p, "%s", sqlite3_value_text(pOut)); |
| 8269 | rc = pCtx->isError; |
| 8270 | } |
| 8271 | sqlite3VdbeDeleteAuxData(db, &p->pAuxData, pCtx->iOp, pOp->p1); |
drh | f09ac0b | 2018-01-23 03:44:06 +0000 | [diff] [blame] | 8272 | pCtx->isError = 0; |
drh | 3e34eab | 2017-07-19 19:48:40 +0000 | [diff] [blame] | 8273 | if( rc ) goto abort_due_to_error; |
| 8274 | } |
| 8275 | |
drh | fb92e07 | 2022-03-29 01:43:09 +0000 | [diff] [blame] | 8276 | assert( (pOut->flags&MEM_Str)==0 |
| 8277 | || pOut->enc==encoding |
| 8278 | || db->mallocFailed ); |
| 8279 | assert( !sqlite3VdbeMemTooBig(pOut) ); |
drh | 3e34eab | 2017-07-19 19:48:40 +0000 | [diff] [blame] | 8280 | |
| 8281 | REGISTER_TRACE(pOp->p3, pOut); |
| 8282 | UPDATE_MAX_BLOBSIZE(pOut); |
| 8283 | break; |
| 8284 | } |
| 8285 | |
drh | 2db144c | 2021-12-01 16:31:02 +0000 | [diff] [blame] | 8286 | /* Opcode: FilterAdd P1 * P3 P4 * |
| 8287 | ** Synopsis: filter(P1) += key(P3@P4) |
| 8288 | ** |
| 8289 | ** Compute a hash on the P4 registers starting with r[P3] and |
| 8290 | ** add that hash to the bloom filter contained in r[P1]. |
| 8291 | */ |
| 8292 | case OP_FilterAdd: { |
drh | 5baaf40 | 2021-12-06 13:07:28 +0000 | [diff] [blame] | 8293 | u64 h; |
drh | 2db144c | 2021-12-01 16:31:02 +0000 | [diff] [blame] | 8294 | |
| 8295 | assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) ); |
| 8296 | pIn1 = &aMem[pOp->p1]; |
| 8297 | assert( pIn1->flags & MEM_Blob ); |
drh | 5baaf40 | 2021-12-06 13:07:28 +0000 | [diff] [blame] | 8298 | assert( pIn1->n>0 ); |
drh | 2db144c | 2021-12-01 16:31:02 +0000 | [diff] [blame] | 8299 | h = filterHash(aMem, pOp); |
| 8300 | #ifdef SQLITE_DEBUG |
| 8301 | if( db->flags&SQLITE_VdbeTrace ){ |
| 8302 | int ii; |
| 8303 | for(ii=pOp->p3; ii<pOp->p3+pOp->p4.i; ii++){ |
| 8304 | registerTrace(ii, &aMem[ii]); |
| 8305 | } |
drh | 5baaf40 | 2021-12-06 13:07:28 +0000 | [diff] [blame] | 8306 | printf("hash: %llu modulo %d -> %u\n", h, pIn1->n, (int)(h%pIn1->n)); |
drh | 2db144c | 2021-12-01 16:31:02 +0000 | [diff] [blame] | 8307 | } |
| 8308 | #endif |
drh | 5baaf40 | 2021-12-06 13:07:28 +0000 | [diff] [blame] | 8309 | h %= pIn1->n; |
drh | 2db144c | 2021-12-01 16:31:02 +0000 | [diff] [blame] | 8310 | pIn1->z[h/8] |= 1<<(h&7); |
| 8311 | break; |
| 8312 | } |
| 8313 | |
| 8314 | /* Opcode: Filter P1 P2 P3 P4 * |
| 8315 | ** Synopsis: if key(P3@P4) not in filter(P1) goto P2 |
| 8316 | ** |
| 8317 | ** Compute a hash on the key contained in the P4 registers starting |
| 8318 | ** with r[P3]. Check to see if that hash is found in the |
| 8319 | ** bloom filter hosted by register P1. If it is not present then |
| 8320 | ** maybe jump to P2. Otherwise fall through. |
| 8321 | ** |
| 8322 | ** False negatives are harmless. It is always safe to fall through, |
| 8323 | ** even if the value is in the bloom filter. A false negative causes |
| 8324 | ** more CPU cycles to be used, but it should still yield the correct |
| 8325 | ** answer. However, an incorrect answer may well arise from a |
| 8326 | ** false positive - if the jump is taken when it should fall through. |
| 8327 | */ |
| 8328 | case OP_Filter: { /* jump */ |
drh | 5baaf40 | 2021-12-06 13:07:28 +0000 | [diff] [blame] | 8329 | u64 h; |
drh | 2db144c | 2021-12-01 16:31:02 +0000 | [diff] [blame] | 8330 | |
| 8331 | assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) ); |
| 8332 | pIn1 = &aMem[pOp->p1]; |
drh | 7e910f6 | 2021-12-09 01:28:15 +0000 | [diff] [blame] | 8333 | assert( (pIn1->flags & MEM_Blob)!=0 ); |
| 8334 | assert( pIn1->n >= 1 ); |
drh | 2db144c | 2021-12-01 16:31:02 +0000 | [diff] [blame] | 8335 | h = filterHash(aMem, pOp); |
| 8336 | #ifdef SQLITE_DEBUG |
| 8337 | if( db->flags&SQLITE_VdbeTrace ){ |
| 8338 | int ii; |
| 8339 | for(ii=pOp->p3; ii<pOp->p3+pOp->p4.i; ii++){ |
| 8340 | registerTrace(ii, &aMem[ii]); |
| 8341 | } |
drh | 5baaf40 | 2021-12-06 13:07:28 +0000 | [diff] [blame] | 8342 | printf("hash: %llu modulo %d -> %u\n", h, pIn1->n, (int)(h%pIn1->n)); |
drh | 2db144c | 2021-12-01 16:31:02 +0000 | [diff] [blame] | 8343 | } |
| 8344 | #endif |
drh | 5baaf40 | 2021-12-06 13:07:28 +0000 | [diff] [blame] | 8345 | h %= pIn1->n; |
drh | 067c60c | 2021-12-04 18:45:08 +0000 | [diff] [blame] | 8346 | if( (pIn1->z[h/8] & (1<<(h&7)))==0 ){ |
| 8347 | VdbeBranchTaken(1, 2); |
drh | 23d41e6 | 2021-12-06 21:45:31 +0000 | [diff] [blame] | 8348 | p->aCounter[SQLITE_STMTSTATUS_FILTER_HIT]++; |
drh | 067c60c | 2021-12-04 18:45:08 +0000 | [diff] [blame] | 8349 | goto jump_to_p2; |
| 8350 | }else{ |
drh | 23d41e6 | 2021-12-06 21:45:31 +0000 | [diff] [blame] | 8351 | p->aCounter[SQLITE_STMTSTATUS_FILTER_MISS]++; |
drh | 067c60c | 2021-12-04 18:45:08 +0000 | [diff] [blame] | 8352 | VdbeBranchTaken(0, 2); |
| 8353 | } |
drh | 2db144c | 2021-12-01 16:31:02 +0000 | [diff] [blame] | 8354 | break; |
| 8355 | } |
| 8356 | |
drh | f259df5 | 2017-12-27 20:38:35 +0000 | [diff] [blame] | 8357 | /* Opcode: Trace P1 P2 * P4 * |
| 8358 | ** |
| 8359 | ** Write P4 on the statement trace output if statement tracing is |
| 8360 | ** enabled. |
| 8361 | ** |
| 8362 | ** Operand P1 must be 0x7fffffff and P2 must positive. |
| 8363 | */ |
drh | 74588ce | 2017-09-13 00:13:05 +0000 | [diff] [blame] | 8364 | /* Opcode: Init P1 P2 P3 P4 * |
drh | 72e26de | 2016-08-24 21:24:04 +0000 | [diff] [blame] | 8365 | ** Synopsis: Start at P2 |
drh | aceb31b | 2014-02-08 01:40:27 +0000 | [diff] [blame] | 8366 | ** |
| 8367 | ** Programs contain a single instance of this opcode as the very first |
| 8368 | ** opcode. |
drh | 949f9cd | 2008-01-12 21:35:57 +0000 | [diff] [blame] | 8369 | ** |
| 8370 | ** If tracing is enabled (by the sqlite3_trace()) interface, then |
| 8371 | ** the UTF-8 string contained in P4 is emitted on the trace callback. |
drh | aceb31b | 2014-02-08 01:40:27 +0000 | [diff] [blame] | 8372 | ** Or if P4 is blank, use the string returned by sqlite3_sql(). |
| 8373 | ** |
| 8374 | ** If P2 is not zero, jump to instruction P2. |
drh | 9e5eb9c | 2016-09-18 16:08:10 +0000 | [diff] [blame] | 8375 | ** |
| 8376 | ** Increment the value of P1 so that OP_Once opcodes will jump the |
| 8377 | ** first time they are evaluated for this run. |
drh | 74588ce | 2017-09-13 00:13:05 +0000 | [diff] [blame] | 8378 | ** |
| 8379 | ** If P3 is not zero, then it is an address to jump to if an SQLITE_CORRUPT |
| 8380 | ** error is encountered. |
drh | 949f9cd | 2008-01-12 21:35:57 +0000 | [diff] [blame] | 8381 | */ |
drh | f259df5 | 2017-12-27 20:38:35 +0000 | [diff] [blame] | 8382 | case OP_Trace: |
drh | aceb31b | 2014-02-08 01:40:27 +0000 | [diff] [blame] | 8383 | case OP_Init: { /* jump */ |
drh | 9e5eb9c | 2016-09-18 16:08:10 +0000 | [diff] [blame] | 8384 | int i; |
drh | b9f4799 | 2018-01-24 12:14:43 +0000 | [diff] [blame] | 8385 | #ifndef SQLITE_OMIT_TRACE |
| 8386 | char *zTrace; |
| 8387 | #endif |
drh | 5fe63bf | 2016-07-25 02:42:22 +0000 | [diff] [blame] | 8388 | |
| 8389 | /* If the P4 argument is not NULL, then it must be an SQL comment string. |
| 8390 | ** The "--" string is broken up to prevent false-positives with srcck1.c. |
| 8391 | ** |
| 8392 | ** This assert() provides evidence for: |
| 8393 | ** EVIDENCE-OF: R-50676-09860 The callback can compute the same text that |
| 8394 | ** would have been returned by the legacy sqlite3_trace() interface by |
| 8395 | ** using the X argument when X begins with "--" and invoking |
| 8396 | ** sqlite3_expanded_sql(P) otherwise. |
| 8397 | */ |
| 8398 | assert( pOp->p4.z==0 || strncmp(pOp->p4.z, "-" "- ", 3)==0 ); |
drh | f259df5 | 2017-12-27 20:38:35 +0000 | [diff] [blame] | 8399 | |
| 8400 | /* OP_Init is always instruction 0 */ |
| 8401 | assert( pOp==p->aOp || pOp->opcode==OP_Trace ); |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 8402 | |
drh | aceb31b | 2014-02-08 01:40:27 +0000 | [diff] [blame] | 8403 | #ifndef SQLITE_OMIT_TRACE |
drh | fca760c | 2016-07-14 01:09:08 +0000 | [diff] [blame] | 8404 | if( (db->mTrace & (SQLITE_TRACE_STMT|SQLITE_TRACE_LEGACY))!=0 |
drh | a24832b | 2022-04-01 19:04:13 +0000 | [diff] [blame] | 8405 | && p->minWriteFileFormat!=254 /* tag-20220401a */ |
drh | 37f58e9 | 2012-09-04 21:34:26 +0000 | [diff] [blame] | 8406 | && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0 |
| 8407 | ){ |
drh | 3d2a529 | 2016-07-13 22:55:01 +0000 | [diff] [blame] | 8408 | #ifndef SQLITE_OMIT_DEPRECATED |
drh | fca760c | 2016-07-14 01:09:08 +0000 | [diff] [blame] | 8409 | if( db->mTrace & SQLITE_TRACE_LEGACY ){ |
drh | 5fe63bf | 2016-07-25 02:42:22 +0000 | [diff] [blame] | 8410 | char *z = sqlite3VdbeExpandSql(p, zTrace); |
drh | 08b9208 | 2020-08-10 14:18:00 +0000 | [diff] [blame] | 8411 | db->trace.xLegacy(db->pTraceArg, z); |
drh | bd441f7 | 2016-07-25 02:31:48 +0000 | [diff] [blame] | 8412 | sqlite3_free(z); |
drh | fca760c | 2016-07-14 01:09:08 +0000 | [diff] [blame] | 8413 | }else |
drh | 3d2a529 | 2016-07-13 22:55:01 +0000 | [diff] [blame] | 8414 | #endif |
drh | 7adbcff | 2017-03-20 15:29:28 +0000 | [diff] [blame] | 8415 | if( db->nVdbeExec>1 ){ |
| 8416 | char *z = sqlite3MPrintf(db, "-- %s", zTrace); |
drh | 08b9208 | 2020-08-10 14:18:00 +0000 | [diff] [blame] | 8417 | (void)db->trace.xV2(SQLITE_TRACE_STMT, db->pTraceArg, p, z); |
drh | 7adbcff | 2017-03-20 15:29:28 +0000 | [diff] [blame] | 8418 | sqlite3DbFree(db, z); |
| 8419 | }else{ |
drh | 08b9208 | 2020-08-10 14:18:00 +0000 | [diff] [blame] | 8420 | (void)db->trace.xV2(SQLITE_TRACE_STMT, db->pTraceArg, p, zTrace); |
drh | 3d2a529 | 2016-07-13 22:55:01 +0000 | [diff] [blame] | 8421 | } |
drh | 949f9cd | 2008-01-12 21:35:57 +0000 | [diff] [blame] | 8422 | } |
drh | 8f8b231 | 2013-10-18 20:03:43 +0000 | [diff] [blame] | 8423 | #ifdef SQLITE_USE_FCNTL_TRACE |
| 8424 | zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql); |
| 8425 | if( zTrace ){ |
mistachkin | d8992ce | 2016-09-20 17:49:01 +0000 | [diff] [blame] | 8426 | int j; |
| 8427 | for(j=0; j<db->nDb; j++){ |
| 8428 | if( DbMaskTest(p->btreeMask, j)==0 ) continue; |
| 8429 | sqlite3_file_control(db, db->aDb[j].zDbSName, SQLITE_FCNTL_TRACE, zTrace); |
drh | 8f8b231 | 2013-10-18 20:03:43 +0000 | [diff] [blame] | 8430 | } |
| 8431 | } |
| 8432 | #endif /* SQLITE_USE_FCNTL_TRACE */ |
drh | c3f1d5f | 2011-05-30 23:42:16 +0000 | [diff] [blame] | 8433 | #ifdef SQLITE_DEBUG |
| 8434 | if( (db->flags & SQLITE_SqlTrace)!=0 |
| 8435 | && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0 |
| 8436 | ){ |
| 8437 | sqlite3DebugPrintf("SQL-trace: %s\n", zTrace); |
| 8438 | } |
| 8439 | #endif /* SQLITE_DEBUG */ |
drh | aceb31b | 2014-02-08 01:40:27 +0000 | [diff] [blame] | 8440 | #endif /* SQLITE_OMIT_TRACE */ |
drh | 4910a76 | 2016-09-03 01:46:15 +0000 | [diff] [blame] | 8441 | assert( pOp->p2>0 ); |
drh | 9e5eb9c | 2016-09-18 16:08:10 +0000 | [diff] [blame] | 8442 | if( pOp->p1>=sqlite3GlobalConfig.iOnceResetThreshold ){ |
drh | f259df5 | 2017-12-27 20:38:35 +0000 | [diff] [blame] | 8443 | if( pOp->opcode==OP_Trace ) break; |
drh | 9e5eb9c | 2016-09-18 16:08:10 +0000 | [diff] [blame] | 8444 | for(i=1; i<p->nOp; i++){ |
| 8445 | if( p->aOp[i].opcode==OP_Once ) p->aOp[i].p1 = 0; |
| 8446 | } |
| 8447 | pOp->p1 = 0; |
| 8448 | } |
| 8449 | pOp->p1++; |
drh | 00d11d4 | 2017-06-29 12:49:18 +0000 | [diff] [blame] | 8450 | p->aCounter[SQLITE_STMTSTATUS_RUN]++; |
drh | 4910a76 | 2016-09-03 01:46:15 +0000 | [diff] [blame] | 8451 | goto jump_to_p2; |
drh | 949f9cd | 2008-01-12 21:35:57 +0000 | [diff] [blame] | 8452 | } |
drh | 949f9cd | 2008-01-12 21:35:57 +0000 | [diff] [blame] | 8453 | |
drh | 2893536 | 2013-12-07 20:39:19 +0000 | [diff] [blame] | 8454 | #ifdef SQLITE_ENABLE_CURSOR_HINTS |
drh | 0df5701 | 2015-08-14 15:05:55 +0000 | [diff] [blame] | 8455 | /* Opcode: CursorHint P1 * * P4 * |
drh | 2893536 | 2013-12-07 20:39:19 +0000 | [diff] [blame] | 8456 | ** |
| 8457 | ** 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] | 8458 | ** satisfy the Expr in P4. TK_REGISTER terms in the P4 expression refer |
| 8459 | ** to values currently held in registers. TK_COLUMN terms in the P4 |
| 8460 | ** 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] | 8461 | */ |
| 8462 | case OP_CursorHint: { |
| 8463 | VdbeCursor *pC; |
| 8464 | |
| 8465 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 8466 | assert( pOp->p4type==P4_EXPR ); |
| 8467 | pC = p->apCsr[pOp->p1]; |
dan | 91d3a61 | 2014-07-15 11:59:44 +0000 | [diff] [blame] | 8468 | if( pC ){ |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 8469 | assert( pC->eCurType==CURTYPE_BTREE ); |
drh | 62aaa6c | 2015-11-21 17:27:42 +0000 | [diff] [blame] | 8470 | sqlite3BtreeCursorHint(pC->uc.pCursor, BTREE_HINT_RANGE, |
| 8471 | pOp->p4.pExpr, aMem); |
dan | 91d3a61 | 2014-07-15 11:59:44 +0000 | [diff] [blame] | 8472 | } |
drh | 2893536 | 2013-12-07 20:39:19 +0000 | [diff] [blame] | 8473 | break; |
| 8474 | } |
| 8475 | #endif /* SQLITE_ENABLE_CURSOR_HINTS */ |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 8476 | |
drh | 4031baf | 2018-05-28 17:31:20 +0000 | [diff] [blame] | 8477 | #ifdef SQLITE_DEBUG |
| 8478 | /* Opcode: Abortable * * * * * |
| 8479 | ** |
| 8480 | ** Verify that an Abort can happen. Assert if an Abort at this point |
| 8481 | ** might cause database corruption. This opcode only appears in debugging |
| 8482 | ** builds. |
| 8483 | ** |
| 8484 | ** An Abort is safe if either there have been no writes, or if there is |
| 8485 | ** an active statement journal. |
| 8486 | */ |
| 8487 | case OP_Abortable: { |
| 8488 | sqlite3VdbeAssertAbortable(p); |
| 8489 | break; |
| 8490 | } |
| 8491 | #endif |
| 8492 | |
drh | 13d7950 | 2019-12-23 02:18:49 +0000 | [diff] [blame] | 8493 | #ifdef SQLITE_DEBUG |
drh | 3aef2fb | 2020-01-02 17:46:02 +0000 | [diff] [blame] | 8494 | /* Opcode: ReleaseReg P1 P2 P3 * P5 |
drh | 13d7950 | 2019-12-23 02:18:49 +0000 | [diff] [blame] | 8495 | ** Synopsis: release r[P1@P2] mask P3 |
| 8496 | ** |
| 8497 | ** Release registers from service. Any content that was in the |
| 8498 | ** the registers is unreliable after this opcode completes. |
| 8499 | ** |
| 8500 | ** The registers released will be the P2 registers starting at P1, |
| 8501 | ** except if bit ii of P3 set, then do not release register P1+ii. |
| 8502 | ** In other words, P3 is a mask of registers to preserve. |
| 8503 | ** |
| 8504 | ** Releasing a register clears the Mem.pScopyFrom pointer. That means |
| 8505 | ** that if the content of the released register was set using OP_SCopy, |
| 8506 | ** a change to the value of the source register for the OP_SCopy will no longer |
| 8507 | ** generate an assertion fault in sqlite3VdbeMemAboutToChange(). |
| 8508 | ** |
drh | 3aef2fb | 2020-01-02 17:46:02 +0000 | [diff] [blame] | 8509 | ** If P5 is set, then all released registers have their type set |
| 8510 | ** to MEM_Undefined so that any subsequent attempt to read the released |
drh | 13d7950 | 2019-12-23 02:18:49 +0000 | [diff] [blame] | 8511 | ** register (before it is reinitialized) will generate an assertion fault. |
drh | 3aef2fb | 2020-01-02 17:46:02 +0000 | [diff] [blame] | 8512 | ** |
| 8513 | ** P5 ought to be set on every call to this opcode. |
| 8514 | ** However, there are places in the code generator will release registers |
drh | 13d7950 | 2019-12-23 02:18:49 +0000 | [diff] [blame] | 8515 | ** before their are used, under the (valid) assumption that the registers |
| 8516 | ** will not be reallocated for some other purpose before they are used and |
| 8517 | ** hence are safe to release. |
| 8518 | ** |
| 8519 | ** This opcode is only available in testing and debugging builds. It is |
| 8520 | ** not generated for release builds. The purpose of this opcode is to help |
| 8521 | ** validate the generated bytecode. This opcode does not actually contribute |
| 8522 | ** to computing an answer. |
| 8523 | */ |
| 8524 | case OP_ReleaseReg: { |
| 8525 | Mem *pMem; |
| 8526 | int i; |
| 8527 | u32 constMask; |
| 8528 | assert( pOp->p1>0 ); |
| 8529 | assert( pOp->p1+pOp->p2<=(p->nMem+1 - p->nCursor)+1 ); |
| 8530 | pMem = &aMem[pOp->p1]; |
| 8531 | constMask = pOp->p3; |
| 8532 | for(i=0; i<pOp->p2; i++, pMem++){ |
drh | 7edce5e | 2019-12-23 13:24:34 +0000 | [diff] [blame] | 8533 | if( i>=32 || (constMask & MASKBIT32(i))==0 ){ |
drh | 13d7950 | 2019-12-23 02:18:49 +0000 | [diff] [blame] | 8534 | pMem->pScopyFrom = 0; |
drh | 3aef2fb | 2020-01-02 17:46:02 +0000 | [diff] [blame] | 8535 | if( i<32 && pOp->p5 ) MemSetTypeFlag(pMem, MEM_Undefined); |
drh | 13d7950 | 2019-12-23 02:18:49 +0000 | [diff] [blame] | 8536 | } |
| 8537 | } |
| 8538 | break; |
| 8539 | } |
| 8540 | #endif |
| 8541 | |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 8542 | /* Opcode: Noop * * * * * |
| 8543 | ** |
| 8544 | ** Do nothing. This instruction is often useful as a jump |
| 8545 | ** destination. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 8546 | */ |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 8547 | /* |
| 8548 | ** The magic Explain opcode are only inserted when explain==2 (which |
| 8549 | ** is to say when the EXPLAIN QUERY PLAN syntax is used.) |
| 8550 | ** This opcode records information from the optimizer. It is the |
| 8551 | ** the same as a no-op. This opcodesnever appears in a real VM program. |
| 8552 | */ |
drh | 4031baf | 2018-05-28 17:31:20 +0000 | [diff] [blame] | 8553 | default: { /* This is really OP_Noop, OP_Explain */ |
drh | 13573c7 | 2010-01-12 17:04:07 +0000 | [diff] [blame] | 8554 | assert( pOp->opcode==OP_Noop || pOp->opcode==OP_Explain ); |
drh | 4031baf | 2018-05-28 17:31:20 +0000 | [diff] [blame] | 8555 | |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 8556 | break; |
| 8557 | } |
| 8558 | |
| 8559 | /***************************************************************************** |
| 8560 | ** The cases of the switch statement above this line should all be indented |
| 8561 | ** by 6 spaces. But the left-most 6 spaces have been removed to improve the |
| 8562 | ** readability. From this point on down, the normal indentation rules are |
| 8563 | ** restored. |
| 8564 | *****************************************************************************/ |
| 8565 | } |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 8566 | |
drh | 7b39686 | 2003-01-01 23:06:20 +0000 | [diff] [blame] | 8567 | #ifdef VDBE_PROFILE |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 8568 | { |
drh | 35043cc | 2018-02-12 20:27:34 +0000 | [diff] [blame] | 8569 | u64 endTime = sqlite3NProfileCnt ? sqlite3NProfileCnt : sqlite3Hwtime(); |
drh | 6dc4148 | 2015-04-16 17:31:02 +0000 | [diff] [blame] | 8570 | if( endTime>start ) pOrigOp->cycles += endTime - start; |
| 8571 | pOrigOp->cnt++; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 8572 | } |
drh | 7b39686 | 2003-01-01 23:06:20 +0000 | [diff] [blame] | 8573 | #endif |
| 8574 | |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 8575 | /* The following code adds nothing to the actual functionality |
| 8576 | ** of the program. It is only here for testing and debugging. |
| 8577 | ** On the other hand, it does burn CPU cycles every time through |
| 8578 | ** the evaluator loop. So we can leave it out when NDEBUG is defined. |
| 8579 | */ |
| 8580 | #ifndef NDEBUG |
drh | 6dc4148 | 2015-04-16 17:31:02 +0000 | [diff] [blame] | 8581 | assert( pOp>=&aOp[-1] && pOp<&aOp[p->nOp-1] ); |
drh | ae7e151 | 2007-05-02 16:51:59 +0000 | [diff] [blame] | 8582 | |
drh | cf1023c | 2007-05-08 20:59:49 +0000 | [diff] [blame] | 8583 | #ifdef SQLITE_DEBUG |
drh | 84e55a8 | 2013-11-13 17:58:23 +0000 | [diff] [blame] | 8584 | if( db->flags & SQLITE_VdbeTrace ){ |
drh | 7cc84c2 | 2016-04-11 13:36:42 +0000 | [diff] [blame] | 8585 | u8 opProperty = sqlite3OpcodeProperty[pOrigOp->opcode]; |
drh | 84e55a8 | 2013-11-13 17:58:23 +0000 | [diff] [blame] | 8586 | if( rc!=0 ) printf("rc=%d\n",rc); |
drh | 7cc84c2 | 2016-04-11 13:36:42 +0000 | [diff] [blame] | 8587 | if( opProperty & (OPFLG_OUT2) ){ |
drh | 6dc4148 | 2015-04-16 17:31:02 +0000 | [diff] [blame] | 8588 | registerTrace(pOrigOp->p2, &aMem[pOrigOp->p2]); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 8589 | } |
drh | 7cc84c2 | 2016-04-11 13:36:42 +0000 | [diff] [blame] | 8590 | if( opProperty & OPFLG_OUT3 ){ |
drh | 6dc4148 | 2015-04-16 17:31:02 +0000 | [diff] [blame] | 8591 | registerTrace(pOrigOp->p3, &aMem[pOrigOp->p3]); |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 8592 | } |
drh | 17aceeb | 2020-01-04 19:12:13 +0000 | [diff] [blame] | 8593 | if( opProperty==0xff ){ |
| 8594 | /* Never happens. This code exists to avoid a harmless linkage |
| 8595 | ** warning aboud sqlite3VdbeRegisterDump() being defined but not |
| 8596 | ** used. */ |
| 8597 | sqlite3VdbeRegisterDump(p); |
| 8598 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 8599 | } |
danielk1977 | b5402fb | 2005-01-12 07:15:04 +0000 | [diff] [blame] | 8600 | #endif /* SQLITE_DEBUG */ |
| 8601 | #endif /* NDEBUG */ |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 8602 | } /* The end of the for(;;) loop the loops through opcodes */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 8603 | |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 8604 | /* If we reach this point, it means that execution is finished with |
| 8605 | ** an error of some kind. |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 8606 | */ |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 8607 | abort_due_to_error: |
drh | f56a4bf | 2020-11-18 21:50:05 +0000 | [diff] [blame] | 8608 | if( db->mallocFailed ){ |
| 8609 | rc = SQLITE_NOMEM_BKPT; |
| 8610 | }else if( rc==SQLITE_IOERR_CORRUPTFS ){ |
| 8611 | rc = SQLITE_CORRUPT_BKPT; |
| 8612 | } |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 8613 | assert( rc ); |
drh | 11eb9c6 | 2021-09-16 12:33:53 +0000 | [diff] [blame] | 8614 | #ifdef SQLITE_DEBUG |
| 8615 | if( db->flags & SQLITE_VdbeTrace ){ |
drh | 5b001cc | 2021-11-15 13:22:42 +0000 | [diff] [blame] | 8616 | const char *zTrace = p->zSql; |
| 8617 | if( zTrace==0 ){ |
| 8618 | if( aOp[0].opcode==OP_Trace ){ |
| 8619 | zTrace = aOp[0].p4.z; |
| 8620 | } |
| 8621 | if( zTrace==0 ) zTrace = "???"; |
| 8622 | } |
| 8623 | printf("ABORT-due-to-error (rc=%d): %s\n", rc, zTrace); |
drh | 11eb9c6 | 2021-09-16 12:33:53 +0000 | [diff] [blame] | 8624 | } |
| 8625 | #endif |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 8626 | if( p->zErrMsg==0 && rc!=SQLITE_IOERR_NOMEM ){ |
| 8627 | sqlite3VdbeError(p, "%s", sqlite3ErrStr(rc)); |
| 8628 | } |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 8629 | p->rc = rc; |
drh | f68521c | 2016-03-21 12:28:02 +0000 | [diff] [blame] | 8630 | sqlite3SystemError(db, rc); |
drh | a64fa91 | 2010-03-04 00:53:32 +0000 | [diff] [blame] | 8631 | testcase( sqlite3GlobalConfig.xLog!=0 ); |
| 8632 | sqlite3_log(rc, "statement aborts at %d: [%s] %s", |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 8633 | (int)(pOp - aOp), p->zSql, p->zErrMsg); |
drh | 8703edd | 2022-04-03 22:35:13 +0000 | [diff] [blame] | 8634 | if( p->eVdbeState==VDBE_RUN_STATE ) sqlite3VdbeHalt(p); |
drh | 4a642b6 | 2016-02-05 01:55:27 +0000 | [diff] [blame] | 8635 | if( rc==SQLITE_IOERR_NOMEM ) sqlite3OomFault(db); |
drh | 46c425b | 2021-11-10 10:59:10 +0000 | [diff] [blame] | 8636 | if( rc==SQLITE_CORRUPT && db->autoCommit==0 ){ |
| 8637 | db->flags |= SQLITE_CorruptRdOnly; |
| 8638 | } |
danielk1977 | 7eaabcd | 2008-07-07 14:56:56 +0000 | [diff] [blame] | 8639 | rc = SQLITE_ERROR; |
drh | cdf011d | 2011-04-04 21:25:28 +0000 | [diff] [blame] | 8640 | if( resetSchemaOnFault>0 ){ |
drh | 81028a4 | 2012-05-15 18:28:27 +0000 | [diff] [blame] | 8641 | sqlite3ResetOneSchema(db, resetSchemaOnFault-1); |
drh | bdaec52 | 2011-04-04 00:14:43 +0000 | [diff] [blame] | 8642 | } |
drh | 900b31e | 2007-08-28 02:27:51 +0000 | [diff] [blame] | 8643 | |
| 8644 | /* This is the only way out of this procedure. We have to |
| 8645 | ** release the mutexes on btrees that were acquired at the |
| 8646 | ** top. */ |
| 8647 | vdbe_return: |
drh | c332e04 | 2019-02-12 21:04:33 +0000 | [diff] [blame] | 8648 | #ifndef SQLITE_OMIT_PROGRESS_CALLBACK |
drh | b1af9c6 | 2019-02-20 13:55:45 +0000 | [diff] [blame] | 8649 | while( nVmStep>=nProgressLimit && db->xProgress!=0 ){ |
| 8650 | nProgressLimit += db->nProgressOps; |
drh | c332e04 | 2019-02-12 21:04:33 +0000 | [diff] [blame] | 8651 | if( db->xProgress(db->pProgressArg) ){ |
drh | d1d8914 | 2020-07-06 12:13:05 +0000 | [diff] [blame] | 8652 | nProgressLimit = LARGEST_UINT64; |
drh | c332e04 | 2019-02-12 21:04:33 +0000 | [diff] [blame] | 8653 | rc = SQLITE_INTERRUPT; |
| 8654 | goto abort_due_to_error; |
| 8655 | } |
| 8656 | } |
| 8657 | #endif |
drh | 9b47ee3 | 2013-08-20 03:13:51 +0000 | [diff] [blame] | 8658 | p->aCounter[SQLITE_STMTSTATUS_VM_STEP] += (int)nVmStep; |
drh | bdaec52 | 2011-04-04 00:14:43 +0000 | [diff] [blame] | 8659 | sqlite3VdbeLeave(p); |
dan | 83f0ab8 | 2016-01-29 18:04:31 +0000 | [diff] [blame] | 8660 | assert( rc!=SQLITE_OK || nExtraDelete==0 |
| 8661 | || sqlite3_strlike("DELETE%",p->zSql,0)!=0 |
| 8662 | ); |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 8663 | return rc; |
| 8664 | |
drh | 023ae03 | 2007-05-08 12:12:16 +0000 | [diff] [blame] | 8665 | /* Jump to here if a string or blob larger than SQLITE_MAX_LENGTH |
| 8666 | ** is encountered. |
| 8667 | */ |
| 8668 | too_big: |
drh | 22c17b8 | 2015-05-15 04:13:15 +0000 | [diff] [blame] | 8669 | sqlite3VdbeError(p, "string or blob too big"); |
drh | 023ae03 | 2007-05-08 12:12:16 +0000 | [diff] [blame] | 8670 | rc = SQLITE_TOOBIG; |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 8671 | goto abort_due_to_error; |
drh | 023ae03 | 2007-05-08 12:12:16 +0000 | [diff] [blame] | 8672 | |
drh | 98640a3 | 2007-06-07 19:08:32 +0000 | [diff] [blame] | 8673 | /* Jump to here if a malloc() fails. |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 8674 | */ |
| 8675 | no_mem: |
drh | 4a642b6 | 2016-02-05 01:55:27 +0000 | [diff] [blame] | 8676 | sqlite3OomFault(db); |
drh | 22c17b8 | 2015-05-15 04:13:15 +0000 | [diff] [blame] | 8677 | sqlite3VdbeError(p, "out of memory"); |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 8678 | rc = SQLITE_NOMEM_BKPT; |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 8679 | goto abort_due_to_error; |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 8680 | |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 8681 | /* Jump to here if the sqlite3_interrupt() API sets the interrupt |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 8682 | ** flag. |
| 8683 | */ |
| 8684 | abort_due_to_interrupt: |
dan | 892edb6 | 2020-03-30 13:35:05 +0000 | [diff] [blame] | 8685 | assert( AtomicLoad(&db->u1.isInterrupted) ); |
drh | 56f1873 | 2020-06-03 15:59:22 +0000 | [diff] [blame] | 8686 | rc = SQLITE_INTERRUPT; |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 8687 | goto abort_due_to_error; |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 8688 | } |