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){ |
drh | a3a4da0 | 2022-08-08 12:19:13 +0000 | [diff] [blame] | 317 | i64 iValue; |
drh | 26e817f | 2022-08-08 16:25:13 +0000 | [diff] [blame] | 318 | iValue = sqlite3RealToI64(rValue); |
drh | 8a3884e | 2019-05-29 21:18:27 +0000 | [diff] [blame] | 319 | if( sqlite3RealSameAsInt(rValue,iValue) ){ |
drh | c285ded | 2019-06-10 18:33:16 +0000 | [diff] [blame] | 320 | *piValue = iValue; |
| 321 | return 1; |
drh | 8a3884e | 2019-05-29 21:18:27 +0000 | [diff] [blame] | 322 | } |
| 323 | return 0==sqlite3Atoi64(pRec->z, piValue, pRec->n, pRec->enc); |
| 324 | } |
| 325 | |
| 326 | /* |
drh | 29d7210 | 2006-02-09 22:13:41 +0000 | [diff] [blame] | 327 | ** Try to convert a value into a numeric representation if we can |
| 328 | ** do so without loss of information. In other words, if the string |
| 329 | ** looks like a number, convert it into a number. If it does not |
| 330 | ** look like a number, leave it alone. |
drh | bd9507c | 2014-08-23 17:21:37 +0000 | [diff] [blame] | 331 | ** |
| 332 | ** If the bTryForInt flag is true, then extra effort is made to give |
| 333 | ** an integer representation. Strings that look like floating point |
| 334 | ** values but which have no fractional component (example: '48.00') |
| 335 | ** will have a MEM_Int representation when bTryForInt is true. |
| 336 | ** |
| 337 | ** If bTryForInt is false, then if the input string contains a decimal |
| 338 | ** point or exponential notation, the result is only MEM_Real, even |
| 339 | ** if there is an exact integer representation of the quantity. |
drh | 29d7210 | 2006-02-09 22:13:41 +0000 | [diff] [blame] | 340 | */ |
drh | bd9507c | 2014-08-23 17:21:37 +0000 | [diff] [blame] | 341 | static void applyNumericAffinity(Mem *pRec, int bTryForInt){ |
drh | 975b4c6 | 2014-07-26 16:47:23 +0000 | [diff] [blame] | 342 | double rValue; |
drh | 975b4c6 | 2014-07-26 16:47:23 +0000 | [diff] [blame] | 343 | u8 enc = pRec->enc; |
drh | 8a3884e | 2019-05-29 21:18:27 +0000 | [diff] [blame] | 344 | int rc; |
drh | 169f077 | 2019-05-02 21:36:26 +0000 | [diff] [blame] | 345 | assert( (pRec->flags & (MEM_Str|MEM_Int|MEM_Real|MEM_IntReal))==MEM_Str ); |
drh | 8a3884e | 2019-05-29 21:18:27 +0000 | [diff] [blame] | 346 | rc = sqlite3AtoF(pRec->z, &rValue, pRec->n, enc); |
drh | 9a27822 | 2019-06-07 22:26:08 +0000 | [diff] [blame] | 347 | if( rc<=0 ) return; |
drh | 8a3884e | 2019-05-29 21:18:27 +0000 | [diff] [blame] | 348 | if( rc==1 && alsoAnInt(pRec, rValue, &pRec->u.i) ){ |
drh | 975b4c6 | 2014-07-26 16:47:23 +0000 | [diff] [blame] | 349 | pRec->flags |= MEM_Int; |
| 350 | }else{ |
drh | 74eaba4 | 2014-09-18 17:52:15 +0000 | [diff] [blame] | 351 | pRec->u.r = rValue; |
drh | 975b4c6 | 2014-07-26 16:47:23 +0000 | [diff] [blame] | 352 | pRec->flags |= MEM_Real; |
drh | bd9507c | 2014-08-23 17:21:37 +0000 | [diff] [blame] | 353 | if( bTryForInt ) sqlite3VdbeIntegerAffinity(pRec); |
drh | 29d7210 | 2006-02-09 22:13:41 +0000 | [diff] [blame] | 354 | } |
drh | 06b3bd5 | 2018-02-01 01:13:33 +0000 | [diff] [blame] | 355 | /* TEXT->NUMERIC is many->one. Hence, it is important to invalidate the |
| 356 | ** string representation after computing a numeric equivalent, because the |
| 357 | ** string representation might not be the canonical representation for the |
| 358 | ** numeric value. Ticket [343634942dd54ab57b7024] 2018-01-31. */ |
| 359 | pRec->flags &= ~MEM_Str; |
drh | 29d7210 | 2006-02-09 22:13:41 +0000 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | /* |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 363 | ** Processing is determine by the affinity parameter: |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 364 | ** |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 365 | ** SQLITE_AFF_INTEGER: |
| 366 | ** SQLITE_AFF_REAL: |
| 367 | ** SQLITE_AFF_NUMERIC: |
| 368 | ** Try to convert pRec to an integer representation or a |
| 369 | ** floating-point representation if an integer representation |
| 370 | ** is not possible. Note that the integer representation is |
| 371 | ** always preferred, even if the affinity is REAL, because |
| 372 | ** an integer representation is more space efficient on disk. |
| 373 | ** |
| 374 | ** SQLITE_AFF_TEXT: |
| 375 | ** Convert pRec to a text representation. |
| 376 | ** |
drh | 05883a3 | 2015-06-02 15:32:08 +0000 | [diff] [blame] | 377 | ** SQLITE_AFF_BLOB: |
drh | 96fb16e | 2019-08-06 14:37:24 +0000 | [diff] [blame] | 378 | ** SQLITE_AFF_NONE: |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 379 | ** No-op. pRec is unchanged. |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 380 | */ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 381 | static void applyAffinity( |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 382 | Mem *pRec, /* The value to apply affinity to */ |
| 383 | char affinity, /* The affinity to be applied */ |
| 384 | u8 enc /* Use this text encoding */ |
| 385 | ){ |
drh | 7ea31cc | 2014-09-18 14:36:00 +0000 | [diff] [blame] | 386 | if( affinity>=SQLITE_AFF_NUMERIC ){ |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 387 | assert( affinity==SQLITE_AFF_INTEGER || affinity==SQLITE_AFF_REAL |
| 388 | || affinity==SQLITE_AFF_NUMERIC ); |
drh | a3fa140 | 2016-04-29 02:55:05 +0000 | [diff] [blame] | 389 | if( (pRec->flags & MEM_Int)==0 ){ /*OPTIMIZATION-IF-FALSE*/ |
drh | bd9507c | 2014-08-23 17:21:37 +0000 | [diff] [blame] | 390 | if( (pRec->flags & MEM_Real)==0 ){ |
drh | 11a6eee | 2014-09-19 22:01:54 +0000 | [diff] [blame] | 391 | if( pRec->flags & MEM_Str ) applyNumericAffinity(pRec,1); |
drh | bd9507c | 2014-08-23 17:21:37 +0000 | [diff] [blame] | 392 | }else{ |
| 393 | sqlite3VdbeIntegerAffinity(pRec); |
| 394 | } |
drh | 17c4029 | 2004-07-21 02:53:29 +0000 | [diff] [blame] | 395 | } |
drh | 7ea31cc | 2014-09-18 14:36:00 +0000 | [diff] [blame] | 396 | }else if( affinity==SQLITE_AFF_TEXT ){ |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 397 | /* Only attempt the conversion to TEXT if there is an integer or real |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 398 | ** representation (blob and NULL do not get converted) but no string |
drh | a3fa140 | 2016-04-29 02:55:05 +0000 | [diff] [blame] | 399 | ** representation. It would be harmless to repeat the conversion if |
| 400 | ** there is already a string rep, but it is pointless to waste those |
| 401 | ** CPU cycles. */ |
| 402 | if( 0==(pRec->flags&MEM_Str) ){ /*OPTIMIZATION-IF-FALSE*/ |
drh | 169f077 | 2019-05-02 21:36:26 +0000 | [diff] [blame] | 403 | if( (pRec->flags&(MEM_Real|MEM_Int|MEM_IntReal)) ){ |
drh | 3242c69 | 2019-05-04 01:29:13 +0000 | [diff] [blame] | 404 | testcase( pRec->flags & MEM_Int ); |
| 405 | testcase( pRec->flags & MEM_Real ); |
| 406 | testcase( pRec->flags & MEM_IntReal ); |
drh | a3fa140 | 2016-04-29 02:55:05 +0000 | [diff] [blame] | 407 | sqlite3VdbeMemStringify(pRec, enc, 1); |
| 408 | } |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 409 | } |
drh | 169f077 | 2019-05-02 21:36:26 +0000 | [diff] [blame] | 410 | pRec->flags &= ~(MEM_Real|MEM_Int|MEM_IntReal); |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 411 | } |
| 412 | } |
| 413 | |
danielk1977 | aee18ef | 2005-03-09 12:26:50 +0000 | [diff] [blame] | 414 | /* |
drh | 29d7210 | 2006-02-09 22:13:41 +0000 | [diff] [blame] | 415 | ** Try to convert the type of a function argument or a result column |
| 416 | ** into a numeric representation. Use either INTEGER or REAL whichever |
| 417 | ** is appropriate. But only do the conversion if it is possible without |
| 418 | ** loss of information and return the revised type of the argument. |
drh | 29d7210 | 2006-02-09 22:13:41 +0000 | [diff] [blame] | 419 | */ |
| 420 | int sqlite3_value_numeric_type(sqlite3_value *pVal){ |
drh | 1b27b8c | 2014-02-10 03:21:57 +0000 | [diff] [blame] | 421 | int eType = sqlite3_value_type(pVal); |
| 422 | if( eType==SQLITE_TEXT ){ |
| 423 | Mem *pMem = (Mem*)pVal; |
drh | bd9507c | 2014-08-23 17:21:37 +0000 | [diff] [blame] | 424 | applyNumericAffinity(pMem, 0); |
drh | 1b27b8c | 2014-02-10 03:21:57 +0000 | [diff] [blame] | 425 | eType = sqlite3_value_type(pVal); |
drh | e5a8a1d | 2010-11-18 12:31:24 +0000 | [diff] [blame] | 426 | } |
drh | 1b27b8c | 2014-02-10 03:21:57 +0000 | [diff] [blame] | 427 | return eType; |
drh | 29d7210 | 2006-02-09 22:13:41 +0000 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | /* |
danielk1977 | aee18ef | 2005-03-09 12:26:50 +0000 | [diff] [blame] | 431 | ** Exported version of applyAffinity(). This one works on sqlite3_value*, |
| 432 | ** not the internal Mem* type. |
| 433 | */ |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 434 | void sqlite3ValueApplyAffinity( |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 435 | sqlite3_value *pVal, |
| 436 | u8 affinity, |
| 437 | u8 enc |
| 438 | ){ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 439 | applyAffinity((Mem *)pVal, affinity, enc); |
danielk1977 | aee18ef | 2005-03-09 12:26:50 +0000 | [diff] [blame] | 440 | } |
| 441 | |
drh | 3d1d90a | 2014-03-24 15:00:15 +0000 | [diff] [blame] | 442 | /* |
drh | f1a89ed | 2014-08-23 17:41:15 +0000 | [diff] [blame] | 443 | ** pMem currently only holds a string type (or maybe a BLOB that we can |
| 444 | ** interpret as a string if we want to). Compute its corresponding |
drh | 74eaba4 | 2014-09-18 17:52:15 +0000 | [diff] [blame] | 445 | ** 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] | 446 | ** accordingly. |
| 447 | */ |
| 448 | static u16 SQLITE_NOINLINE computeNumericType(Mem *pMem){ |
drh | 9a27822 | 2019-06-07 22:26:08 +0000 | [diff] [blame] | 449 | int rc; |
| 450 | sqlite3_int64 ix; |
drh | 169f077 | 2019-05-02 21:36:26 +0000 | [diff] [blame] | 451 | assert( (pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal))==0 ); |
drh | f1a89ed | 2014-08-23 17:41:15 +0000 | [diff] [blame] | 452 | assert( (pMem->flags & (MEM_Str|MEM_Blob))!=0 ); |
drh | 1fd1cc4 | 2021-04-10 15:34:30 +0000 | [diff] [blame] | 453 | if( ExpandBlob(pMem) ){ |
| 454 | pMem->u.i = 0; |
| 455 | return MEM_Int; |
| 456 | } |
drh | 9a27822 | 2019-06-07 22:26:08 +0000 | [diff] [blame] | 457 | rc = sqlite3AtoF(pMem->z, &pMem->u.r, pMem->n, pMem->enc); |
| 458 | if( rc<=0 ){ |
| 459 | if( rc==0 && sqlite3Atoi64(pMem->z, &ix, pMem->n, pMem->enc)<=1 ){ |
| 460 | pMem->u.i = ix; |
| 461 | return MEM_Int; |
| 462 | }else{ |
| 463 | return MEM_Real; |
| 464 | } |
| 465 | }else if( rc==1 && sqlite3Atoi64(pMem->z, &ix, pMem->n, pMem->enc)==0 ){ |
| 466 | pMem->u.i = ix; |
drh | f1a89ed | 2014-08-23 17:41:15 +0000 | [diff] [blame] | 467 | return MEM_Int; |
| 468 | } |
| 469 | return MEM_Real; |
| 470 | } |
| 471 | |
| 472 | /* |
drh | 3d1d90a | 2014-03-24 15:00:15 +0000 | [diff] [blame] | 473 | ** Return the numeric type for pMem, either MEM_Int or MEM_Real or both or |
| 474 | ** none. |
| 475 | ** |
| 476 | ** Unlike applyNumericAffinity(), this routine does not modify pMem->flags. |
drh | 74eaba4 | 2014-09-18 17:52:15 +0000 | [diff] [blame] | 477 | ** But it does set pMem->u.r and pMem->u.i appropriately. |
drh | 3d1d90a | 2014-03-24 15:00:15 +0000 | [diff] [blame] | 478 | */ |
| 479 | static u16 numericType(Mem *pMem){ |
drh | 5e10d89 | 2022-08-08 13:04:08 +0000 | [diff] [blame] | 480 | assert( (pMem->flags & MEM_Null)==0 |
| 481 | || pMem->db==0 || pMem->db->mallocFailed ); |
| 482 | if( pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal|MEM_Null) ){ |
drh | 3242c69 | 2019-05-04 01:29:13 +0000 | [diff] [blame] | 483 | testcase( pMem->flags & MEM_Int ); |
| 484 | testcase( pMem->flags & MEM_Real ); |
| 485 | testcase( pMem->flags & MEM_IntReal ); |
drh | 5e10d89 | 2022-08-08 13:04:08 +0000 | [diff] [blame] | 486 | return pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal|MEM_Null); |
drh | 3d1d90a | 2014-03-24 15:00:15 +0000 | [diff] [blame] | 487 | } |
drh | 5e10d89 | 2022-08-08 13:04:08 +0000 | [diff] [blame] | 488 | assert( pMem->flags & (MEM_Str|MEM_Blob) ); |
| 489 | testcase( pMem->flags & MEM_Str ); |
| 490 | testcase( pMem->flags & MEM_Blob ); |
| 491 | return computeNumericType(pMem); |
drh | 3d1d90a | 2014-03-24 15:00:15 +0000 | [diff] [blame] | 492 | return 0; |
| 493 | } |
| 494 | |
danielk1977 | b5402fb | 2005-01-12 07:15:04 +0000 | [diff] [blame] | 495 | #ifdef SQLITE_DEBUG |
drh | b6f5452 | 2004-05-20 02:42:16 +0000 | [diff] [blame] | 496 | /* |
danielk1977 | ca6b291 | 2004-05-21 10:49:47 +0000 | [diff] [blame] | 497 | ** Write a nice string representation of the contents of cell pMem |
| 498 | ** into buffer zBuf, length nBuf. |
| 499 | */ |
drh | 5ca0632 | 2020-01-06 19:23:41 +0000 | [diff] [blame] | 500 | void sqlite3VdbeMemPrettyPrint(Mem *pMem, StrAccum *pStr){ |
danielk1977 | ca6b291 | 2004-05-21 10:49:47 +0000 | [diff] [blame] | 501 | int f = pMem->flags; |
drh | 5719628 | 2004-10-06 15:41:16 +0000 | [diff] [blame] | 502 | static const char *const encnames[] = {"(X)", "(8)", "(16LE)", "(16BE)"}; |
danielk1977 | ca6b291 | 2004-05-21 10:49:47 +0000 | [diff] [blame] | 503 | if( f&MEM_Blob ){ |
| 504 | int i; |
| 505 | char c; |
| 506 | if( f & MEM_Dyn ){ |
| 507 | c = 'z'; |
| 508 | assert( (f & (MEM_Static|MEM_Ephem))==0 ); |
| 509 | }else if( f & MEM_Static ){ |
| 510 | c = 't'; |
| 511 | assert( (f & (MEM_Dyn|MEM_Ephem))==0 ); |
| 512 | }else if( f & MEM_Ephem ){ |
| 513 | c = 'e'; |
| 514 | assert( (f & (MEM_Static|MEM_Dyn))==0 ); |
| 515 | }else{ |
| 516 | c = 's'; |
| 517 | } |
drh | ded33cc | 2020-01-08 11:36:30 +0000 | [diff] [blame] | 518 | sqlite3_str_appendf(pStr, "%cx[", c); |
drh | efb5f9a | 2019-08-30 21:52:13 +0000 | [diff] [blame] | 519 | for(i=0; i<25 && i<pMem->n; i++){ |
drh | 5ca0632 | 2020-01-06 19:23:41 +0000 | [diff] [blame] | 520 | sqlite3_str_appendf(pStr, "%02X", ((int)pMem->z[i] & 0xFF)); |
danielk1977 | ca6b291 | 2004-05-21 10:49:47 +0000 | [diff] [blame] | 521 | } |
drh | 5ca0632 | 2020-01-06 19:23:41 +0000 | [diff] [blame] | 522 | sqlite3_str_appendf(pStr, "|"); |
drh | efb5f9a | 2019-08-30 21:52:13 +0000 | [diff] [blame] | 523 | for(i=0; i<25 && i<pMem->n; i++){ |
danielk1977 | ca6b291 | 2004-05-21 10:49:47 +0000 | [diff] [blame] | 524 | char z = pMem->z[i]; |
drh | 5ca0632 | 2020-01-06 19:23:41 +0000 | [diff] [blame] | 525 | sqlite3_str_appendchar(pStr, 1, (z<32||z>126)?'.':z); |
danielk1977 | ca6b291 | 2004-05-21 10:49:47 +0000 | [diff] [blame] | 526 | } |
drh | 5ca0632 | 2020-01-06 19:23:41 +0000 | [diff] [blame] | 527 | sqlite3_str_appendf(pStr,"]"); |
drh | fdf972a | 2007-05-02 13:30:27 +0000 | [diff] [blame] | 528 | if( f & MEM_Zero ){ |
drh | 5ca0632 | 2020-01-06 19:23:41 +0000 | [diff] [blame] | 529 | sqlite3_str_appendf(pStr, "+%dz",pMem->u.nZero); |
drh | fdf972a | 2007-05-02 13:30:27 +0000 | [diff] [blame] | 530 | } |
danielk1977 | b1bc953 | 2004-05-22 03:05:33 +0000 | [diff] [blame] | 531 | }else if( f & MEM_Str ){ |
drh | 5ca0632 | 2020-01-06 19:23:41 +0000 | [diff] [blame] | 532 | int j; |
mistachkin | 5917117 | 2020-01-18 19:02:20 +0000 | [diff] [blame] | 533 | u8 c; |
danielk1977 | b1bc953 | 2004-05-22 03:05:33 +0000 | [diff] [blame] | 534 | if( f & MEM_Dyn ){ |
drh | 5ca0632 | 2020-01-06 19:23:41 +0000 | [diff] [blame] | 535 | c = 'z'; |
danielk1977 | b1bc953 | 2004-05-22 03:05:33 +0000 | [diff] [blame] | 536 | assert( (f & (MEM_Static|MEM_Ephem))==0 ); |
| 537 | }else if( f & MEM_Static ){ |
drh | 5ca0632 | 2020-01-06 19:23:41 +0000 | [diff] [blame] | 538 | c = 't'; |
danielk1977 | b1bc953 | 2004-05-22 03:05:33 +0000 | [diff] [blame] | 539 | assert( (f & (MEM_Dyn|MEM_Ephem))==0 ); |
| 540 | }else if( f & MEM_Ephem ){ |
drh | 5ca0632 | 2020-01-06 19:23:41 +0000 | [diff] [blame] | 541 | c = 'e'; |
danielk1977 | b1bc953 | 2004-05-22 03:05:33 +0000 | [diff] [blame] | 542 | assert( (f & (MEM_Static|MEM_Dyn))==0 ); |
| 543 | }else{ |
drh | 5ca0632 | 2020-01-06 19:23:41 +0000 | [diff] [blame] | 544 | c = 's'; |
danielk1977 | b1bc953 | 2004-05-22 03:05:33 +0000 | [diff] [blame] | 545 | } |
drh | 5ca0632 | 2020-01-06 19:23:41 +0000 | [diff] [blame] | 546 | sqlite3_str_appendf(pStr, " %c%d[", c, pMem->n); |
drh | efb5f9a | 2019-08-30 21:52:13 +0000 | [diff] [blame] | 547 | for(j=0; j<25 && j<pMem->n; j++){ |
mistachkin | 5917117 | 2020-01-18 19:02:20 +0000 | [diff] [blame] | 548 | c = pMem->z[j]; |
drh | 5ca0632 | 2020-01-06 19:23:41 +0000 | [diff] [blame] | 549 | sqlite3_str_appendchar(pStr, 1, (c>=0x20&&c<=0x7f) ? c : '.'); |
danielk1977 | b1bc953 | 2004-05-22 03:05:33 +0000 | [diff] [blame] | 550 | } |
drh | 5ca0632 | 2020-01-06 19:23:41 +0000 | [diff] [blame] | 551 | sqlite3_str_appendf(pStr, "]%s", encnames[pMem->enc]); |
danielk1977 | ca6b291 | 2004-05-21 10:49:47 +0000 | [diff] [blame] | 552 | } |
danielk1977 | ca6b291 | 2004-05-21 10:49:47 +0000 | [diff] [blame] | 553 | } |
| 554 | #endif |
| 555 | |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 556 | #ifdef SQLITE_DEBUG |
| 557 | /* |
| 558 | ** Print the value of a register for tracing purposes: |
| 559 | */ |
drh | 84e55a8 | 2013-11-13 17:58:23 +0000 | [diff] [blame] | 560 | static void memTracePrint(Mem *p){ |
drh | a5750cf | 2014-02-07 13:20:31 +0000 | [diff] [blame] | 561 | if( p->flags & MEM_Undefined ){ |
drh | 84e55a8 | 2013-11-13 17:58:23 +0000 | [diff] [blame] | 562 | printf(" undefined"); |
drh | 953f761 | 2012-12-07 22:18:54 +0000 | [diff] [blame] | 563 | }else if( p->flags & MEM_Null ){ |
drh | ce2fbd1 | 2018-01-12 21:00:14 +0000 | [diff] [blame] | 564 | printf(p->flags & MEM_Zero ? " NULL-nochng" : " NULL"); |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 565 | }else if( (p->flags & (MEM_Int|MEM_Str))==(MEM_Int|MEM_Str) ){ |
drh | 84e55a8 | 2013-11-13 17:58:23 +0000 | [diff] [blame] | 566 | printf(" si:%lld", p->u.i); |
drh | 169f077 | 2019-05-02 21:36:26 +0000 | [diff] [blame] | 567 | }else if( (p->flags & (MEM_IntReal))!=0 ){ |
drh | 83a1daf | 2019-05-01 18:59:33 +0000 | [diff] [blame] | 568 | printf(" ir:%lld", p->u.i); |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 569 | }else if( p->flags & MEM_Int ){ |
drh | 84e55a8 | 2013-11-13 17:58:23 +0000 | [diff] [blame] | 570 | printf(" i:%lld", p->u.i); |
drh | 0b3bf92 | 2009-06-15 20:45:34 +0000 | [diff] [blame] | 571 | #ifndef SQLITE_OMIT_FLOATING_POINT |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 572 | }else if( p->flags & MEM_Real ){ |
drh | d1c472d | 2019-10-03 14:51:59 +0000 | [diff] [blame] | 573 | printf(" r:%.17g", p->u.r); |
drh | 0b3bf92 | 2009-06-15 20:45:34 +0000 | [diff] [blame] | 574 | #endif |
drh | 9d67afc | 2018-08-29 20:24:03 +0000 | [diff] [blame] | 575 | }else if( sqlite3VdbeMemIsRowSet(p) ){ |
drh | 84e55a8 | 2013-11-13 17:58:23 +0000 | [diff] [blame] | 576 | printf(" (rowset)"); |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 577 | }else{ |
drh | 5ca0632 | 2020-01-06 19:23:41 +0000 | [diff] [blame] | 578 | StrAccum acc; |
| 579 | char zBuf[1000]; |
| 580 | sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0); |
| 581 | sqlite3VdbeMemPrettyPrint(p, &acc); |
| 582 | printf(" %s", sqlite3StrAccumFinish(&acc)); |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 583 | } |
dan | 5b6c8e4 | 2016-01-30 15:46:03 +0000 | [diff] [blame] | 584 | if( p->flags & MEM_Subtype ) printf(" subtype=0x%02x", p->eSubtype); |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 585 | } |
drh | 84e55a8 | 2013-11-13 17:58:23 +0000 | [diff] [blame] | 586 | static void registerTrace(int iReg, Mem *p){ |
drh | 22e95fb | 2020-01-02 14:42:42 +0000 | [diff] [blame] | 587 | printf("R[%d] = ", iReg); |
drh | 84e55a8 | 2013-11-13 17:58:23 +0000 | [diff] [blame] | 588 | memTracePrint(p); |
drh | 22e95fb | 2020-01-02 14:42:42 +0000 | [diff] [blame] | 589 | if( p->pScopyFrom ){ |
| 590 | printf(" <== R[%d]", (int)(p->pScopyFrom - &p[-iReg])); |
| 591 | } |
drh | 84e55a8 | 2013-11-13 17:58:23 +0000 | [diff] [blame] | 592 | printf("\n"); |
drh | e2bc655 | 2017-04-17 20:50:34 +0000 | [diff] [blame] | 593 | sqlite3VdbeCheckMemInvariants(p); |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 594 | } |
drh | 93ffb50 | 2021-05-18 19:10:10 +0000 | [diff] [blame] | 595 | /**/ void sqlite3PrintMem(Mem *pMem){ |
drh | 59df3e9 | 2021-05-05 19:46:50 +0000 | [diff] [blame] | 596 | memTracePrint(pMem); |
| 597 | printf("\n"); |
| 598 | fflush(stdout); |
| 599 | } |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 600 | #endif |
| 601 | |
| 602 | #ifdef SQLITE_DEBUG |
drh | 22e95fb | 2020-01-02 14:42:42 +0000 | [diff] [blame] | 603 | /* |
| 604 | ** Show the values of all registers in the virtual machine. Used for |
| 605 | ** interactive debugging. |
| 606 | */ |
| 607 | void sqlite3VdbeRegisterDump(Vdbe *v){ |
| 608 | int i; |
| 609 | for(i=1; i<v->nMem; i++) registerTrace(i, v->aMem+i); |
| 610 | } |
| 611 | #endif /* SQLITE_DEBUG */ |
| 612 | |
| 613 | |
| 614 | #ifdef SQLITE_DEBUG |
drh | 84e55a8 | 2013-11-13 17:58:23 +0000 | [diff] [blame] | 615 | # define REGISTER_TRACE(R,M) if(db->flags&SQLITE_VdbeTrace)registerTrace(R,M) |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 616 | #else |
| 617 | # define REGISTER_TRACE(R,M) |
| 618 | #endif |
| 619 | |
danielk1977 | 84ac9d0 | 2004-05-18 09:58:06 +0000 | [diff] [blame] | 620 | |
drh | 7b39686 | 2003-01-01 23:06:20 +0000 | [diff] [blame] | 621 | #ifdef VDBE_PROFILE |
shane | 9bcbdad | 2008-05-29 20:22:37 +0000 | [diff] [blame] | 622 | |
| 623 | /* |
| 624 | ** hwtime.h contains inline assembler code for implementing |
| 625 | ** high-performance timing routines. |
drh | 7b39686 | 2003-01-01 23:06:20 +0000 | [diff] [blame] | 626 | */ |
shane | 9bcbdad | 2008-05-29 20:22:37 +0000 | [diff] [blame] | 627 | #include "hwtime.h" |
| 628 | |
drh | 7b39686 | 2003-01-01 23:06:20 +0000 | [diff] [blame] | 629 | #endif |
| 630 | |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 631 | #ifndef NDEBUG |
| 632 | /* |
| 633 | ** This function is only called from within an assert() expression. It |
| 634 | ** checks that the sqlite3.nTransaction variable is correctly set to |
| 635 | ** the number of non-transaction savepoints currently in the |
| 636 | ** linked list starting at sqlite3.pSavepoint. |
| 637 | ** |
| 638 | ** Usage: |
| 639 | ** |
| 640 | ** assert( checkSavepointCount(db) ); |
| 641 | */ |
| 642 | static int checkSavepointCount(sqlite3 *db){ |
| 643 | int n = 0; |
| 644 | Savepoint *p; |
| 645 | for(p=db->pSavepoint; p; p=p->pNext) n++; |
| 646 | assert( n==(db->nSavepoint + db->isTransactionSavepoint) ); |
| 647 | return 1; |
| 648 | } |
| 649 | #endif |
| 650 | |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 651 | /* |
| 652 | ** Return the register of pOp->p2 after first preparing it to be |
| 653 | ** overwritten with an integer value. |
drh | 9eef8c6 | 2015-10-15 17:31:41 +0000 | [diff] [blame] | 654 | */ |
| 655 | static SQLITE_NOINLINE Mem *out2PrereleaseWithClear(Mem *pOut){ |
| 656 | sqlite3VdbeMemSetNull(pOut); |
| 657 | pOut->flags = MEM_Int; |
| 658 | return pOut; |
| 659 | } |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 660 | static Mem *out2Prerelease(Vdbe *p, VdbeOp *pOp){ |
| 661 | Mem *pOut; |
| 662 | assert( pOp->p2>0 ); |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 663 | assert( pOp->p2<=(p->nMem+1 - p->nCursor) ); |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 664 | pOut = &p->aMem[pOp->p2]; |
| 665 | memAboutToChange(p, pOut); |
drh | a3fa140 | 2016-04-29 02:55:05 +0000 | [diff] [blame] | 666 | if( VdbeMemDynamic(pOut) ){ /*OPTIMIZATION-IF-FALSE*/ |
drh | 9eef8c6 | 2015-10-15 17:31:41 +0000 | [diff] [blame] | 667 | return out2PrereleaseWithClear(pOut); |
| 668 | }else{ |
| 669 | pOut->flags = MEM_Int; |
| 670 | return pOut; |
| 671 | } |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 672 | } |
| 673 | |
drh | faf9c77 | 2021-08-20 08:05:42 +0000 | [diff] [blame] | 674 | /* |
drh | 2db144c | 2021-12-01 16:31:02 +0000 | [diff] [blame] | 675 | ** Compute a bloom filter hash using pOp->p4.i registers from aMem[] beginning |
| 676 | ** with pOp->p3. Return the hash. |
| 677 | */ |
drh | 5baaf40 | 2021-12-06 13:07:28 +0000 | [diff] [blame] | 678 | static u64 filterHash(const Mem *aMem, const Op *pOp){ |
drh | 2db144c | 2021-12-01 16:31:02 +0000 | [diff] [blame] | 679 | int i, mx; |
drh | 5baaf40 | 2021-12-06 13:07:28 +0000 | [diff] [blame] | 680 | u64 h = 0; |
drh | 2db144c | 2021-12-01 16:31:02 +0000 | [diff] [blame] | 681 | |
drh | 2db144c | 2021-12-01 16:31:02 +0000 | [diff] [blame] | 682 | assert( pOp->p4type==P4_INT32 ); |
drh | 2db144c | 2021-12-01 16:31:02 +0000 | [diff] [blame] | 683 | for(i=pOp->p3, mx=i+pOp->p4.i; i<mx; i++){ |
| 684 | const Mem *p = &aMem[i]; |
| 685 | if( p->flags & (MEM_Int|MEM_IntReal) ){ |
drh | 5baaf40 | 2021-12-06 13:07:28 +0000 | [diff] [blame] | 686 | h += p->u.i; |
drh | 2db144c | 2021-12-01 16:31:02 +0000 | [diff] [blame] | 687 | }else if( p->flags & MEM_Real ){ |
drh | 5baaf40 | 2021-12-06 13:07:28 +0000 | [diff] [blame] | 688 | h += sqlite3VdbeIntValue(p); |
drh | 2db144c | 2021-12-01 16:31:02 +0000 | [diff] [blame] | 689 | }else if( p->flags & (MEM_Str|MEM_Blob) ){ |
| 690 | h += p->n; |
drh | 067c60c | 2021-12-04 18:45:08 +0000 | [diff] [blame] | 691 | if( p->flags & MEM_Zero ) h += p->u.nZero; |
drh | 2db144c | 2021-12-01 16:31:02 +0000 | [diff] [blame] | 692 | } |
| 693 | } |
drh | 5baaf40 | 2021-12-06 13:07:28 +0000 | [diff] [blame] | 694 | return h; |
drh | 2db144c | 2021-12-01 16:31:02 +0000 | [diff] [blame] | 695 | } |
| 696 | |
| 697 | /* |
drh | faf9c77 | 2021-08-20 08:05:42 +0000 | [diff] [blame] | 698 | ** Return the symbolic name for the data type of a pMem |
| 699 | */ |
| 700 | static const char *vdbeMemTypeName(Mem *pMem){ |
| 701 | static const char *azTypes[] = { |
| 702 | /* SQLITE_INTEGER */ "INT", |
| 703 | /* SQLITE_FLOAT */ "REAL", |
| 704 | /* SQLITE_TEXT */ "TEXT", |
| 705 | /* SQLITE_BLOB */ "BLOB", |
| 706 | /* SQLITE_NULL */ "NULL" |
| 707 | }; |
| 708 | return azTypes[sqlite3_value_type(pMem)-1]; |
| 709 | } |
drh | b975598 | 2010-07-24 16:34:37 +0000 | [diff] [blame] | 710 | |
| 711 | /* |
drh | 0fd6135 | 2014-02-07 02:29:45 +0000 | [diff] [blame] | 712 | ** Execute as much of a VDBE program as we can. |
| 713 | ** This is the core of sqlite3_step(). |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 714 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 715 | int sqlite3VdbeExec( |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 716 | Vdbe *p /* The VDBE */ |
| 717 | ){ |
drh | bbe879d | 2009-11-14 18:04:35 +0000 | [diff] [blame] | 718 | Op *aOp = p->aOp; /* Copy of p->aOp */ |
mistachkin | 5f7b95f | 2017-02-01 23:03:54 +0000 | [diff] [blame] | 719 | Op *pOp = aOp; /* Current operation */ |
drh | 6dc4148 | 2015-04-16 17:31:02 +0000 | [diff] [blame] | 720 | #if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE) |
| 721 | Op *pOrigOp; /* Value of pOp at the top of the loop */ |
| 722 | #endif |
drh | b89aeb6 | 2016-01-27 15:49:32 +0000 | [diff] [blame] | 723 | #ifdef SQLITE_DEBUG |
drh | def19e3 | 2016-01-27 16:26:25 +0000 | [diff] [blame] | 724 | int nExtraDelete = 0; /* Verifies FORDELETE and AUXDELETE flags */ |
drh | b89aeb6 | 2016-01-27 15:49:32 +0000 | [diff] [blame] | 725 | #endif |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 726 | int rc = SQLITE_OK; /* Value to return */ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 727 | sqlite3 *db = p->db; /* The database */ |
drh | cdf011d | 2011-04-04 21:25:28 +0000 | [diff] [blame] | 728 | u8 resetSchemaOnFault = 0; /* Reset schema after an error if positive */ |
drh | 8079a0d | 2006-01-12 17:20:50 +0000 | [diff] [blame] | 729 | u8 encoding = ENC(db); /* The database encoding */ |
drh | 0f825a7 | 2016-08-13 14:17:02 +0000 | [diff] [blame] | 730 | int iCompare = 0; /* Result of last comparison */ |
drh | d1d8914 | 2020-07-06 12:13:05 +0000 | [diff] [blame] | 731 | u64 nVmStep = 0; /* Number of virtual machine steps */ |
drh | 49afe3a | 2013-07-10 03:05:14 +0000 | [diff] [blame] | 732 | #ifndef SQLITE_OMIT_PROGRESS_CALLBACK |
drh | d1d8914 | 2020-07-06 12:13:05 +0000 | [diff] [blame] | 733 | u64 nProgressLimit; /* Invoke xProgress() when nVmStep reaches this */ |
drh | 49afe3a | 2013-07-10 03:05:14 +0000 | [diff] [blame] | 734 | #endif |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 735 | Mem *aMem = p->aMem; /* Copy of p->aMem */ |
drh | b27b7f5 | 2008-12-10 18:03:45 +0000 | [diff] [blame] | 736 | Mem *pIn1 = 0; /* 1st input operand */ |
| 737 | Mem *pIn2 = 0; /* 2nd input operand */ |
| 738 | Mem *pIn3 = 0; /* 3rd input operand */ |
| 739 | Mem *pOut = 0; /* Output operand */ |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 740 | #ifdef VDBE_PROFILE |
shane | 9bcbdad | 2008-05-29 20:22:37 +0000 | [diff] [blame] | 741 | u64 start; /* CPU clock count at start of opcode */ |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 742 | #endif |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 743 | /*** INSERT STACK UNION HERE ***/ |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 744 | |
drh | 66181ce | 2022-03-31 20:04:49 +0000 | [diff] [blame] | 745 | assert( p->eVdbeState==VDBE_RUN_STATE ); /* sqlite3_step() verifies this */ |
drh | bdaec52 | 2011-04-04 00:14:43 +0000 | [diff] [blame] | 746 | sqlite3VdbeEnter(p); |
drh | 82642f8 | 2019-02-12 22:58:32 +0000 | [diff] [blame] | 747 | #ifndef SQLITE_OMIT_PROGRESS_CALLBACK |
| 748 | if( db->xProgress ){ |
| 749 | u32 iPrior = p->aCounter[SQLITE_STMTSTATUS_VM_STEP]; |
| 750 | assert( 0 < db->nProgressOps ); |
| 751 | nProgressLimit = db->nProgressOps - (iPrior % db->nProgressOps); |
| 752 | }else{ |
drh | d1d8914 | 2020-07-06 12:13:05 +0000 | [diff] [blame] | 753 | nProgressLimit = LARGEST_UINT64; |
drh | 82642f8 | 2019-02-12 22:58:32 +0000 | [diff] [blame] | 754 | } |
| 755 | #endif |
danielk1977 | 2e588c7 | 2005-12-09 14:25:08 +0000 | [diff] [blame] | 756 | if( p->rc==SQLITE_NOMEM ){ |
| 757 | /* This happens if a malloc() inside a call to sqlite3_column_text() or |
| 758 | ** sqlite3_column_text16() failed. */ |
| 759 | goto no_mem; |
| 760 | } |
drh | cbd8db3 | 2015-08-20 17:18:32 +0000 | [diff] [blame] | 761 | assert( p->rc==SQLITE_OK || (p->rc&0xff)==SQLITE_BUSY ); |
drh | a5f3fb3 | 2020-06-03 19:28:10 +0000 | [diff] [blame] | 762 | testcase( p->rc!=SQLITE_OK ); |
| 763 | p->rc = SQLITE_OK; |
drh | 1713afb | 2013-06-28 01:24:57 +0000 | [diff] [blame] | 764 | assert( p->bIsReader || p->readOnly!=0 ); |
drh | 95a7b3e | 2013-09-16 12:57:19 +0000 | [diff] [blame] | 765 | p->iCurrentTime = 0; |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 766 | assert( p->explain==0 ); |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 767 | p->pResultSet = 0; |
drh | a4afb65 | 2005-07-09 02:16:02 +0000 | [diff] [blame] | 768 | db->busyHandler.nBusy = 0; |
dan | 892edb6 | 2020-03-30 13:35:05 +0000 | [diff] [blame] | 769 | if( AtomicLoad(&db->u1.isInterrupted) ) goto abort_due_to_interrupt; |
drh | 602c237 | 2007-03-01 00:29:13 +0000 | [diff] [blame] | 770 | sqlite3VdbeIOTraceSql(p); |
drh | 3c23a88 | 2007-01-09 14:01:13 +0000 | [diff] [blame] | 771 | #ifdef SQLITE_DEBUG |
danielk1977 | 2d1d86f | 2008-06-20 14:59:51 +0000 | [diff] [blame] | 772 | sqlite3BeginBenignMalloc(); |
drh | 84e55a8 | 2013-11-13 17:58:23 +0000 | [diff] [blame] | 773 | if( p->pc==0 |
| 774 | && (p->db->flags & (SQLITE_VdbeListing|SQLITE_VdbeEQP|SQLITE_VdbeTrace))!=0 |
| 775 | ){ |
drh | 3c23a88 | 2007-01-09 14:01:13 +0000 | [diff] [blame] | 776 | int i; |
drh | 84e55a8 | 2013-11-13 17:58:23 +0000 | [diff] [blame] | 777 | int once = 1; |
drh | 3c23a88 | 2007-01-09 14:01:13 +0000 | [diff] [blame] | 778 | sqlite3VdbePrintSql(p); |
drh | 84e55a8 | 2013-11-13 17:58:23 +0000 | [diff] [blame] | 779 | if( p->db->flags & SQLITE_VdbeListing ){ |
| 780 | printf("VDBE Program Listing:\n"); |
| 781 | for(i=0; i<p->nOp; i++){ |
| 782 | sqlite3VdbePrintOp(stdout, i, &aOp[i]); |
| 783 | } |
drh | 3c23a88 | 2007-01-09 14:01:13 +0000 | [diff] [blame] | 784 | } |
drh | 84e55a8 | 2013-11-13 17:58:23 +0000 | [diff] [blame] | 785 | if( p->db->flags & SQLITE_VdbeEQP ){ |
| 786 | for(i=0; i<p->nOp; i++){ |
| 787 | if( aOp[i].opcode==OP_Explain ){ |
| 788 | if( once ) printf("VDBE Query Plan:\n"); |
| 789 | printf("%s\n", aOp[i].p4.z); |
| 790 | once = 0; |
| 791 | } |
| 792 | } |
| 793 | } |
| 794 | if( p->db->flags & SQLITE_VdbeTrace ) printf("VDBE Trace:\n"); |
drh | 3c23a88 | 2007-01-09 14:01:13 +0000 | [diff] [blame] | 795 | } |
danielk1977 | 2d1d86f | 2008-06-20 14:59:51 +0000 | [diff] [blame] | 796 | sqlite3EndBenignMalloc(); |
drh | 3c23a88 | 2007-01-09 14:01:13 +0000 | [diff] [blame] | 797 | #endif |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 798 | for(pOp=&aOp[p->pc]; 1; pOp++){ |
| 799 | /* Errors are detected by individual opcodes, with an immediate |
| 800 | ** jumps to abort_due_to_error. */ |
| 801 | assert( rc==SQLITE_OK ); |
| 802 | |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 803 | assert( pOp>=aOp && pOp<&aOp[p->nOp]); |
drh | 7b39686 | 2003-01-01 23:06:20 +0000 | [diff] [blame] | 804 | #ifdef VDBE_PROFILE |
drh | 35043cc | 2018-02-12 20:27:34 +0000 | [diff] [blame] | 805 | start = sqlite3NProfileCnt ? sqlite3NProfileCnt : sqlite3Hwtime(); |
drh | 7b39686 | 2003-01-01 23:06:20 +0000 | [diff] [blame] | 806 | #endif |
drh | bf159fa | 2013-06-25 22:01:22 +0000 | [diff] [blame] | 807 | nVmStep++; |
dan | 6f9702e | 2014-11-01 20:38:06 +0000 | [diff] [blame] | 808 | #ifdef SQLITE_ENABLE_STMT_SCANSTATUS |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 809 | if( p->anExec ) p->anExec[(int)(pOp-aOp)]++; |
dan | 6f9702e | 2014-11-01 20:38:06 +0000 | [diff] [blame] | 810 | #endif |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 811 | |
danielk1977 | 8b60e0f | 2005-01-12 09:10:39 +0000 | [diff] [blame] | 812 | /* Only allow tracing if SQLITE_DEBUG is defined. |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 813 | */ |
danielk1977 | 8b60e0f | 2005-01-12 09:10:39 +0000 | [diff] [blame] | 814 | #ifdef SQLITE_DEBUG |
drh | 84e55a8 | 2013-11-13 17:58:23 +0000 | [diff] [blame] | 815 | if( db->flags & SQLITE_VdbeTrace ){ |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 816 | sqlite3VdbePrintOp(stdout, (int)(pOp - aOp), pOp); |
drh | 22e95fb | 2020-01-02 14:42:42 +0000 | [diff] [blame] | 817 | test_trace_breakpoint((int)(pOp - aOp),pOp,p); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 818 | } |
drh | 3f7d4e4 | 2004-07-24 14:35:58 +0000 | [diff] [blame] | 819 | #endif |
| 820 | |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 821 | |
drh | f603871 | 2004-02-08 18:07:34 +0000 | [diff] [blame] | 822 | /* Check to see if we need to simulate an interrupt. This only happens |
| 823 | ** if we have a special test build. |
| 824 | */ |
| 825 | #ifdef SQLITE_TEST |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 826 | if( sqlite3_interrupt_count>0 ){ |
| 827 | sqlite3_interrupt_count--; |
| 828 | if( sqlite3_interrupt_count==0 ){ |
| 829 | sqlite3_interrupt(db); |
drh | f603871 | 2004-02-08 18:07:34 +0000 | [diff] [blame] | 830 | } |
| 831 | } |
| 832 | #endif |
| 833 | |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 834 | /* Sanity checking on other operands */ |
| 835 | #ifdef SQLITE_DEBUG |
drh | 7cc84c2 | 2016-04-11 13:36:42 +0000 | [diff] [blame] | 836 | { |
| 837 | u8 opProperty = sqlite3OpcodeProperty[pOp->opcode]; |
| 838 | if( (opProperty & OPFLG_IN1)!=0 ){ |
| 839 | assert( pOp->p1>0 ); |
| 840 | assert( pOp->p1<=(p->nMem+1 - p->nCursor) ); |
| 841 | assert( memIsValid(&aMem[pOp->p1]) ); |
| 842 | assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p1]) ); |
| 843 | REGISTER_TRACE(pOp->p1, &aMem[pOp->p1]); |
| 844 | } |
| 845 | if( (opProperty & OPFLG_IN2)!=0 ){ |
| 846 | assert( pOp->p2>0 ); |
| 847 | assert( pOp->p2<=(p->nMem+1 - p->nCursor) ); |
| 848 | assert( memIsValid(&aMem[pOp->p2]) ); |
| 849 | assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p2]) ); |
| 850 | REGISTER_TRACE(pOp->p2, &aMem[pOp->p2]); |
| 851 | } |
| 852 | if( (opProperty & OPFLG_IN3)!=0 ){ |
| 853 | assert( pOp->p3>0 ); |
| 854 | assert( pOp->p3<=(p->nMem+1 - p->nCursor) ); |
| 855 | assert( memIsValid(&aMem[pOp->p3]) ); |
| 856 | assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p3]) ); |
| 857 | REGISTER_TRACE(pOp->p3, &aMem[pOp->p3]); |
| 858 | } |
| 859 | if( (opProperty & OPFLG_OUT2)!=0 ){ |
| 860 | assert( pOp->p2>0 ); |
| 861 | assert( pOp->p2<=(p->nMem+1 - p->nCursor) ); |
| 862 | memAboutToChange(p, &aMem[pOp->p2]); |
| 863 | } |
| 864 | if( (opProperty & OPFLG_OUT3)!=0 ){ |
| 865 | assert( pOp->p3>0 ); |
| 866 | assert( pOp->p3<=(p->nMem+1 - p->nCursor) ); |
| 867 | memAboutToChange(p, &aMem[pOp->p3]); |
| 868 | } |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 869 | } |
| 870 | #endif |
drh | 6dc4148 | 2015-04-16 17:31:02 +0000 | [diff] [blame] | 871 | #if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE) |
| 872 | pOrigOp = pOp; |
| 873 | #endif |
drh | 93952eb | 2009-11-13 19:43:43 +0000 | [diff] [blame] | 874 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 875 | switch( pOp->opcode ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 876 | |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 877 | /***************************************************************************** |
| 878 | ** What follows is a massive switch statement where each case implements a |
| 879 | ** separate instruction in the virtual machine. If we follow the usual |
| 880 | ** indentation conventions, each case should be indented by 6 spaces. But |
| 881 | ** that is a lot of wasted space on the left margin. So the code within |
| 882 | ** the switch statement will break with convention and be flush-left. Another |
| 883 | ** big comment (similar to this one) will mark the point in the code where |
| 884 | ** we transition back to normal indentation. |
drh | ac82fcf | 2002-09-08 17:23:41 +0000 | [diff] [blame] | 885 | ** |
| 886 | ** The formatting of each case is important. The makefile for SQLite |
| 887 | ** generates two C files "opcodes.h" and "opcodes.c" by scanning this |
| 888 | ** file looking for lines that begin with "case OP_". The opcodes.h files |
| 889 | ** will be filled with #defines that give unique integer values to each |
| 890 | ** 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] | 891 | ** each string is the symbolic name for the corresponding opcode. If the |
| 892 | ** case statement is followed by a comment of the form "/# same as ... #/" |
| 893 | ** that comment is used to determine the particular value of the opcode. |
drh | ac82fcf | 2002-09-08 17:23:41 +0000 | [diff] [blame] | 894 | ** |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 895 | ** Other keywords in the comment that follows each case are used to |
| 896 | ** construct the OPFLG_INITIALIZER value that initializes opcodeProperty[]. |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 897 | ** Keywords include: in1, in2, in3, out2, out3. See |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 898 | ** the mkopcodeh.awk script for additional information. |
danielk1977 | bc04f85 | 2005-03-29 08:26:13 +0000 | [diff] [blame] | 899 | ** |
drh | ac82fcf | 2002-09-08 17:23:41 +0000 | [diff] [blame] | 900 | ** Documentation about VDBE opcodes is generated by scanning this file |
| 901 | ** for lines of that contain "Opcode:". That line and all subsequent |
| 902 | ** comment lines are used in the generation of the opcode.html documentation |
| 903 | ** file. |
| 904 | ** |
| 905 | ** SUMMARY: |
| 906 | ** |
| 907 | ** Formatting is important to scripts that scan this file. |
| 908 | ** Do not deviate from the formatting style currently in use. |
| 909 | ** |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 910 | *****************************************************************************/ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 911 | |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 912 | /* Opcode: Goto * P2 * * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 913 | ** |
| 914 | ** An unconditional jump to address P2. |
| 915 | ** The next instruction executed will be |
| 916 | ** the one at index P2 from the beginning of |
| 917 | ** the program. |
drh | fe70510 | 2014-03-06 13:38:37 +0000 | [diff] [blame] | 918 | ** |
| 919 | ** The P1 parameter is not actually used by this opcode. However, it |
| 920 | ** is sometimes set to 1 instead of 0 as a hint to the command-line shell |
| 921 | ** that this Goto is the bottom of a loop and that the lines from P2 down |
| 922 | ** to the current line should be indented for EXPLAIN output. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 923 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 924 | case OP_Goto: { /* jump */ |
drh | d9670ab | 2019-12-28 01:52:46 +0000 | [diff] [blame] | 925 | |
| 926 | #ifdef SQLITE_DEBUG |
| 927 | /* In debuggging mode, when the p5 flags is set on an OP_Goto, that |
| 928 | ** means we should really jump back to the preceeding OP_ReleaseReg |
| 929 | ** instruction. */ |
| 930 | if( pOp->p5 ){ |
| 931 | assert( pOp->p2 < (int)(pOp - aOp) ); |
| 932 | assert( pOp->p2 > 1 ); |
| 933 | pOp = &aOp[pOp->p2 - 2]; |
| 934 | assert( pOp[1].opcode==OP_ReleaseReg ); |
| 935 | goto check_for_interrupt; |
| 936 | } |
| 937 | #endif |
| 938 | |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 939 | jump_to_p2_and_check_for_interrupt: |
| 940 | pOp = &aOp[pOp->p2 - 1]; |
drh | 49afe3a | 2013-07-10 03:05:14 +0000 | [diff] [blame] | 941 | |
| 942 | /* 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] | 943 | ** OP_VNext, or OP_SorterNext) all jump here upon |
drh | 49afe3a | 2013-07-10 03:05:14 +0000 | [diff] [blame] | 944 | ** completion. Check to see if sqlite3_interrupt() has been called |
| 945 | ** or if the progress callback needs to be invoked. |
| 946 | ** |
| 947 | ** This code uses unstructured "goto" statements and does not look clean. |
| 948 | ** But that is not due to sloppy coding habits. The code is written this |
| 949 | ** way for performance, to avoid having to run the interrupt and progress |
| 950 | ** checks on every opcode. This helps sqlite3_step() to run about 1.5% |
| 951 | ** faster according to "valgrind --tool=cachegrind" */ |
| 952 | check_for_interrupt: |
dan | 892edb6 | 2020-03-30 13:35:05 +0000 | [diff] [blame] | 953 | if( AtomicLoad(&db->u1.isInterrupted) ) goto abort_due_to_interrupt; |
drh | 49afe3a | 2013-07-10 03:05:14 +0000 | [diff] [blame] | 954 | #ifndef SQLITE_OMIT_PROGRESS_CALLBACK |
| 955 | /* Call the progress callback if it is configured and the required number |
| 956 | ** of VDBE ops have been executed (either since this invocation of |
| 957 | ** sqlite3VdbeExec() or since last time the progress callback was called). |
| 958 | ** If the progress callback returns non-zero, exit the virtual machine with |
| 959 | ** a return code SQLITE_ABORT. |
| 960 | */ |
drh | b1af9c6 | 2019-02-20 13:55:45 +0000 | [diff] [blame] | 961 | while( nVmStep>=nProgressLimit && db->xProgress!=0 ){ |
drh | 400fcba | 2013-11-14 00:09:48 +0000 | [diff] [blame] | 962 | assert( db->nProgressOps!=0 ); |
drh | b1af9c6 | 2019-02-20 13:55:45 +0000 | [diff] [blame] | 963 | nProgressLimit += db->nProgressOps; |
drh | 400fcba | 2013-11-14 00:09:48 +0000 | [diff] [blame] | 964 | if( db->xProgress(db->pProgressArg) ){ |
drh | d1d8914 | 2020-07-06 12:13:05 +0000 | [diff] [blame] | 965 | nProgressLimit = LARGEST_UINT64; |
drh | 49afe3a | 2013-07-10 03:05:14 +0000 | [diff] [blame] | 966 | rc = SQLITE_INTERRUPT; |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 967 | goto abort_due_to_error; |
drh | 49afe3a | 2013-07-10 03:05:14 +0000 | [diff] [blame] | 968 | } |
drh | 49afe3a | 2013-07-10 03:05:14 +0000 | [diff] [blame] | 969 | } |
| 970 | #endif |
| 971 | |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 972 | break; |
| 973 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 974 | |
drh | 2eb9537 | 2008-06-06 15:04:36 +0000 | [diff] [blame] | 975 | /* Opcode: Gosub P1 P2 * * * |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 976 | ** |
drh | 2eb9537 | 2008-06-06 15:04:36 +0000 | [diff] [blame] | 977 | ** Write the current address onto register P1 |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 978 | ** and then jump to address P2. |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 979 | */ |
drh | b8475df | 2011-12-09 16:21:19 +0000 | [diff] [blame] | 980 | case OP_Gosub: { /* jump */ |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 981 | assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) ); |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 982 | pIn1 = &aMem[pOp->p1]; |
drh | c91b2fd | 2014-03-01 18:13:23 +0000 | [diff] [blame] | 983 | assert( VdbeMemDynamic(pIn1)==0 ); |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 984 | memAboutToChange(p, pIn1); |
drh | 2eb9537 | 2008-06-06 15:04:36 +0000 | [diff] [blame] | 985 | pIn1->flags = MEM_Int; |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 986 | pIn1->u.i = (int)(pOp-aOp); |
drh | 2eb9537 | 2008-06-06 15:04:36 +0000 | [diff] [blame] | 987 | REGISTER_TRACE(pOp->p1, pIn1); |
drh | d549a70 | 2022-04-14 18:19:06 +0000 | [diff] [blame] | 988 | goto jump_to_p2_and_check_for_interrupt; |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 989 | } |
| 990 | |
drh | e603ab0 | 2022-04-07 19:06:31 +0000 | [diff] [blame] | 991 | /* Opcode: Return P1 P2 P3 * * |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 992 | ** |
drh | 2bd9f44 | 2022-04-17 20:30:52 +0000 | [diff] [blame] | 993 | ** Jump to the address stored in register P1. If P1 is a return address |
| 994 | ** register, then this accomplishes a return from a subroutine. |
drh | 6134b2d | 2022-04-11 17:27:38 +0000 | [diff] [blame] | 995 | ** |
drh | 2bd9f44 | 2022-04-17 20:30:52 +0000 | [diff] [blame] | 996 | ** If P3 is 1, then the jump is only taken if register P1 holds an integer |
| 997 | ** values, otherwise execution falls through to the next opcode, and the |
| 998 | ** OP_Return becomes a no-op. If P3 is 0, then register P1 must hold an |
| 999 | ** integer or else an assert() is raised. P3 should be set to 1 when |
| 1000 | ** this opcode is used in combination with OP_BeginSubrtn, and set to 0 |
| 1001 | ** otherwise. |
| 1002 | ** |
| 1003 | ** The value in register P1 is unchanged by this opcode. |
drh | 1902516 | 2022-03-03 15:00:44 +0000 | [diff] [blame] | 1004 | ** |
drh | e603ab0 | 2022-04-07 19:06:31 +0000 | [diff] [blame] | 1005 | ** P2 is not used by the byte-code engine. However, if P2 is positive |
| 1006 | ** and also less than the current address, then the "EXPLAIN" output |
| 1007 | ** formatter in the CLI will indent all opcodes from the P2 opcode up |
| 1008 | ** to be not including the current Return. P2 should be the first opcode |
drh | 2591cfb | 2022-06-22 14:25:12 +0000 | [diff] [blame] | 1009 | ** in the subroutine from which this opcode is returning. Thus the P2 |
drh | e603ab0 | 2022-04-07 19:06:31 +0000 | [diff] [blame] | 1010 | ** value is a byte-code indentation hint. See tag-20220407a in |
| 1011 | ** wherecode.c and shell.c. |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 1012 | */ |
drh | 2eb9537 | 2008-06-06 15:04:36 +0000 | [diff] [blame] | 1013 | case OP_Return: { /* in1 */ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 1014 | pIn1 = &aMem[pOp->p1]; |
drh | 2bd9f44 | 2022-04-17 20:30:52 +0000 | [diff] [blame] | 1015 | if( pIn1->flags & MEM_Int ){ |
drh | 6dab33b | 2022-04-21 19:25:51 +0000 | [diff] [blame] | 1016 | if( pOp->p3 ){ VdbeBranchTaken(1, 2); } |
drh | 2bd9f44 | 2022-04-17 20:30:52 +0000 | [diff] [blame] | 1017 | pOp = &aOp[pIn1->u.i]; |
| 1018 | }else if( ALWAYS(pOp->p3) ){ |
| 1019 | VdbeBranchTaken(0, 2); |
| 1020 | } |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 1021 | break; |
| 1022 | } |
| 1023 | |
drh | ed71a83 | 2014-02-07 19:18:10 +0000 | [diff] [blame] | 1024 | /* Opcode: InitCoroutine P1 P2 P3 * * |
drh | 81cf13e | 2014-02-07 18:27:53 +0000 | [diff] [blame] | 1025 | ** |
drh | 5dad9a3 | 2014-07-25 18:37:42 +0000 | [diff] [blame] | 1026 | ** Set up register P1 so that it will Yield to the coroutine |
drh | ed71a83 | 2014-02-07 19:18:10 +0000 | [diff] [blame] | 1027 | ** located at address P3. |
| 1028 | ** |
drh | 5dad9a3 | 2014-07-25 18:37:42 +0000 | [diff] [blame] | 1029 | ** If P2!=0 then the coroutine implementation immediately follows |
| 1030 | ** this opcode. So jump over the coroutine implementation to |
drh | ed71a83 | 2014-02-07 19:18:10 +0000 | [diff] [blame] | 1031 | ** address P2. |
drh | 5dad9a3 | 2014-07-25 18:37:42 +0000 | [diff] [blame] | 1032 | ** |
| 1033 | ** See also: EndCoroutine |
drh | 81cf13e | 2014-02-07 18:27:53 +0000 | [diff] [blame] | 1034 | */ |
| 1035 | case OP_InitCoroutine: { /* jump */ |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 1036 | assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) ); |
drh | ed71a83 | 2014-02-07 19:18:10 +0000 | [diff] [blame] | 1037 | assert( pOp->p2>=0 && pOp->p2<p->nOp ); |
| 1038 | assert( pOp->p3>=0 && pOp->p3<p->nOp ); |
drh | 81cf13e | 2014-02-07 18:27:53 +0000 | [diff] [blame] | 1039 | pOut = &aMem[pOp->p1]; |
drh | ed71a83 | 2014-02-07 19:18:10 +0000 | [diff] [blame] | 1040 | assert( !VdbeMemDynamic(pOut) ); |
| 1041 | pOut->u.i = pOp->p3 - 1; |
drh | 81cf13e | 2014-02-07 18:27:53 +0000 | [diff] [blame] | 1042 | pOut->flags = MEM_Int; |
drh | d549a70 | 2022-04-14 18:19:06 +0000 | [diff] [blame] | 1043 | if( pOp->p2==0 ) break; |
| 1044 | |
| 1045 | /* Most jump operations do a goto to this spot in order to update |
| 1046 | ** the pOp pointer. */ |
| 1047 | jump_to_p2: |
drh | 207f626 | 2022-05-03 12:11:16 +0000 | [diff] [blame] | 1048 | assert( pOp->p2>0 ); /* There are never any jumps to instruction 0 */ |
| 1049 | assert( pOp->p2<p->nOp ); /* Jumps must be in range */ |
drh | d549a70 | 2022-04-14 18:19:06 +0000 | [diff] [blame] | 1050 | pOp = &aOp[pOp->p2 - 1]; |
drh | 81cf13e | 2014-02-07 18:27:53 +0000 | [diff] [blame] | 1051 | break; |
| 1052 | } |
| 1053 | |
| 1054 | /* Opcode: EndCoroutine P1 * * * * |
| 1055 | ** |
drh | bc5cf38 | 2014-08-06 01:08:07 +0000 | [diff] [blame] | 1056 | ** The instruction at the address in register P1 is a Yield. |
drh | 5dad9a3 | 2014-07-25 18:37:42 +0000 | [diff] [blame] | 1057 | ** Jump to the P2 parameter of that Yield. |
drh | 81cf13e | 2014-02-07 18:27:53 +0000 | [diff] [blame] | 1058 | ** After the jump, register P1 becomes undefined. |
drh | 5dad9a3 | 2014-07-25 18:37:42 +0000 | [diff] [blame] | 1059 | ** |
| 1060 | ** See also: InitCoroutine |
drh | 81cf13e | 2014-02-07 18:27:53 +0000 | [diff] [blame] | 1061 | */ |
| 1062 | case OP_EndCoroutine: { /* in1 */ |
| 1063 | VdbeOp *pCaller; |
| 1064 | pIn1 = &aMem[pOp->p1]; |
| 1065 | assert( pIn1->flags==MEM_Int ); |
| 1066 | assert( pIn1->u.i>=0 && pIn1->u.i<p->nOp ); |
| 1067 | pCaller = &aOp[pIn1->u.i]; |
| 1068 | assert( pCaller->opcode==OP_Yield ); |
| 1069 | assert( pCaller->p2>=0 && pCaller->p2<p->nOp ); |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 1070 | pOp = &aOp[pCaller->p2 - 1]; |
drh | 81cf13e | 2014-02-07 18:27:53 +0000 | [diff] [blame] | 1071 | pIn1->flags = MEM_Undefined; |
| 1072 | break; |
| 1073 | } |
| 1074 | |
| 1075 | /* Opcode: Yield P1 P2 * * * |
drh | e00ee6e | 2008-06-20 15:24:01 +0000 | [diff] [blame] | 1076 | ** |
drh | 5dad9a3 | 2014-07-25 18:37:42 +0000 | [diff] [blame] | 1077 | ** Swap the program counter with the value in register P1. This |
| 1078 | ** has the effect of yielding to a coroutine. |
drh | 81cf13e | 2014-02-07 18:27:53 +0000 | [diff] [blame] | 1079 | ** |
drh | 5dad9a3 | 2014-07-25 18:37:42 +0000 | [diff] [blame] | 1080 | ** If the coroutine that is launched by this instruction ends with |
| 1081 | ** Yield or Return then continue to the next instruction. But if |
| 1082 | ** the coroutine launched by this instruction ends with |
| 1083 | ** EndCoroutine, then jump to P2 rather than continuing with the |
| 1084 | ** next instruction. |
| 1085 | ** |
| 1086 | ** See also: InitCoroutine |
drh | e00ee6e | 2008-06-20 15:24:01 +0000 | [diff] [blame] | 1087 | */ |
drh | 81cf13e | 2014-02-07 18:27:53 +0000 | [diff] [blame] | 1088 | case OP_Yield: { /* in1, jump */ |
drh | e00ee6e | 2008-06-20 15:24:01 +0000 | [diff] [blame] | 1089 | int pcDest; |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 1090 | pIn1 = &aMem[pOp->p1]; |
drh | c91b2fd | 2014-03-01 18:13:23 +0000 | [diff] [blame] | 1091 | assert( VdbeMemDynamic(pIn1)==0 ); |
drh | e00ee6e | 2008-06-20 15:24:01 +0000 | [diff] [blame] | 1092 | pIn1->flags = MEM_Int; |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 1093 | pcDest = (int)pIn1->u.i; |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 1094 | pIn1->u.i = (int)(pOp - aOp); |
drh | e00ee6e | 2008-06-20 15:24:01 +0000 | [diff] [blame] | 1095 | REGISTER_TRACE(pOp->p1, pIn1); |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 1096 | pOp = &aOp[pcDest]; |
drh | e00ee6e | 2008-06-20 15:24:01 +0000 | [diff] [blame] | 1097 | break; |
| 1098 | } |
| 1099 | |
drh | f9c8ce3 | 2013-11-05 13:33:55 +0000 | [diff] [blame] | 1100 | /* Opcode: HaltIfNull P1 P2 P3 P4 P5 |
drh | 72e26de | 2016-08-24 21:24:04 +0000 | [diff] [blame] | 1101 | ** Synopsis: if r[P3]=null halt |
drh | 5053a79 | 2009-02-20 03:02:23 +0000 | [diff] [blame] | 1102 | ** |
drh | ef8662b | 2011-06-20 21:47:58 +0000 | [diff] [blame] | 1103 | ** Check the value in register P3. If it is NULL then Halt using |
drh | 5053a79 | 2009-02-20 03:02:23 +0000 | [diff] [blame] | 1104 | ** parameter P1, P2, and P4 as if this were a Halt instruction. If the |
| 1105 | ** 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] | 1106 | ** The P5 parameter should be 1. |
drh | 5053a79 | 2009-02-20 03:02:23 +0000 | [diff] [blame] | 1107 | */ |
| 1108 | case OP_HaltIfNull: { /* in3 */ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 1109 | pIn3 = &aMem[pOp->p3]; |
drh | 4031baf | 2018-05-28 17:31:20 +0000 | [diff] [blame] | 1110 | #ifdef SQLITE_DEBUG |
| 1111 | if( pOp->p2==OE_Abort ){ sqlite3VdbeAssertAbortable(p); } |
| 1112 | #endif |
drh | 5053a79 | 2009-02-20 03:02:23 +0000 | [diff] [blame] | 1113 | if( (pIn3->flags & MEM_Null)==0 ) break; |
| 1114 | /* Fall through into OP_Halt */ |
drh | 08b9208 | 2020-08-10 14:18:00 +0000 | [diff] [blame] | 1115 | /* no break */ deliberate_fall_through |
drh | 5053a79 | 2009-02-20 03:02:23 +0000 | [diff] [blame] | 1116 | } |
drh | e00ee6e | 2008-06-20 15:24:01 +0000 | [diff] [blame] | 1117 | |
drh | f9c8ce3 | 2013-11-05 13:33:55 +0000 | [diff] [blame] | 1118 | /* Opcode: Halt P1 P2 * P4 P5 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1119 | ** |
drh | 3d4501e | 2008-12-04 20:40:10 +0000 | [diff] [blame] | 1120 | ** Exit immediately. All open cursors, etc are closed |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1121 | ** automatically. |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 1122 | ** |
drh | 92f02c3 | 2004-09-02 14:57:08 +0000 | [diff] [blame] | 1123 | ** P1 is the result code returned by sqlite3_exec(), sqlite3_reset(), |
| 1124 | ** or sqlite3_finalize(). For a normal halt, this should be SQLITE_OK (0). |
| 1125 | ** For errors, it can be some other value. If P1!=0 then P2 will determine |
| 1126 | ** whether or not to rollback the current transaction. Do not rollback |
| 1127 | ** if P2==OE_Fail. Do the rollback if P2==OE_Rollback. If P2==OE_Abort, |
| 1128 | ** then back out all changes that have occurred during this execution of the |
drh | b798fa6 | 2002-09-03 19:43:23 +0000 | [diff] [blame] | 1129 | ** VDBE, but do not rollback the transaction. |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 1130 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 1131 | ** If P4 is not null then it is an error message string. |
drh | 7f057c9 | 2005-06-24 03:53:06 +0000 | [diff] [blame] | 1132 | ** |
drh | f9c8ce3 | 2013-11-05 13:33:55 +0000 | [diff] [blame] | 1133 | ** P5 is a value between 0 and 4, inclusive, that modifies the P4 string. |
| 1134 | ** |
| 1135 | ** 0: (no change) |
| 1136 | ** 1: NOT NULL contraint failed: P4 |
| 1137 | ** 2: UNIQUE constraint failed: P4 |
| 1138 | ** 3: CHECK constraint failed: P4 |
| 1139 | ** 4: FOREIGN KEY constraint failed: P4 |
| 1140 | ** |
| 1141 | ** If P5 is not zero and P4 is NULL, then everything after the ":" is |
| 1142 | ** omitted. |
| 1143 | ** |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 1144 | ** 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] | 1145 | ** every program. So a jump past the last instruction of the program |
| 1146 | ** is the same as executing Halt. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1147 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1148 | case OP_Halt: { |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 1149 | VdbeFrame *pFrame; |
| 1150 | int pcx; |
drh | f9c8ce3 | 2013-11-05 13:33:55 +0000 | [diff] [blame] | 1151 | |
drh | 4031baf | 2018-05-28 17:31:20 +0000 | [diff] [blame] | 1152 | #ifdef SQLITE_DEBUG |
| 1153 | if( pOp->p2==OE_Abort ){ sqlite3VdbeAssertAbortable(p); } |
| 1154 | #endif |
drh | 8bb93da | 2022-04-03 20:39:48 +0000 | [diff] [blame] | 1155 | if( p->pFrame && pOp->p1==SQLITE_OK ){ |
dan | 2832ad4 | 2009-08-31 15:27:27 +0000 | [diff] [blame] | 1156 | /* Halt the sub-program. Return control to the parent frame. */ |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 1157 | pFrame = p->pFrame; |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 1158 | p->pFrame = pFrame->pParent; |
| 1159 | p->nFrame--; |
dan | 2832ad4 | 2009-08-31 15:27:27 +0000 | [diff] [blame] | 1160 | sqlite3VdbeSetChanges(db, p->nChange); |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 1161 | pcx = sqlite3VdbeFrameRestore(pFrame); |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 1162 | if( pOp->p2==OE_Ignore ){ |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 1163 | /* Instruction pcx is the OP_Program that invoked the sub-program |
dan | 2832ad4 | 2009-08-31 15:27:27 +0000 | [diff] [blame] | 1164 | ** currently being halted. If the p2 instruction of this OP_Halt |
| 1165 | ** instruction is set to OE_Ignore, then the sub-program is throwing |
| 1166 | ** an IGNORE exception. In this case jump to the address specified |
| 1167 | ** as the p2 of the calling OP_Program. */ |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 1168 | pcx = p->aOp[pcx].p2-1; |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 1169 | } |
drh | bbe879d | 2009-11-14 18:04:35 +0000 | [diff] [blame] | 1170 | aOp = p->aOp; |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 1171 | aMem = p->aMem; |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 1172 | pOp = &aOp[pcx]; |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 1173 | break; |
| 1174 | } |
drh | 92f02c3 | 2004-09-02 14:57:08 +0000 | [diff] [blame] | 1175 | p->rc = pOp->p1; |
shane | 36840fd | 2009-06-26 16:32:13 +0000 | [diff] [blame] | 1176 | p->errorAction = (u8)pOp->p2; |
drh | fb4e3a3 | 2016-12-30 00:09:14 +0000 | [diff] [blame] | 1177 | assert( pOp->p5<=4 ); |
drh | f9c8ce3 | 2013-11-05 13:33:55 +0000 | [diff] [blame] | 1178 | if( p->rc ){ |
drh | d9b7ec9 | 2013-11-06 14:05:21 +0000 | [diff] [blame] | 1179 | if( pOp->p5 ){ |
| 1180 | static const char * const azType[] = { "NOT NULL", "UNIQUE", "CHECK", |
| 1181 | "FOREIGN KEY" }; |
drh | d9b7ec9 | 2013-11-06 14:05:21 +0000 | [diff] [blame] | 1182 | testcase( pOp->p5==1 ); |
| 1183 | testcase( pOp->p5==2 ); |
| 1184 | testcase( pOp->p5==3 ); |
| 1185 | testcase( pOp->p5==4 ); |
drh | 99f5de7 | 2016-04-30 02:59:15 +0000 | [diff] [blame] | 1186 | sqlite3VdbeError(p, "%s constraint failed", azType[pOp->p5-1]); |
| 1187 | if( pOp->p4.z ){ |
| 1188 | p->zErrMsg = sqlite3MPrintf(db, "%z: %s", p->zErrMsg, pOp->p4.z); |
| 1189 | } |
drh | d9b7ec9 | 2013-11-06 14:05:21 +0000 | [diff] [blame] | 1190 | }else{ |
drh | 22c17b8 | 2015-05-15 04:13:15 +0000 | [diff] [blame] | 1191 | sqlite3VdbeError(p, "%s", pOp->p4.z); |
drh | f9c8ce3 | 2013-11-05 13:33:55 +0000 | [diff] [blame] | 1192 | } |
drh | 8bb93da | 2022-04-03 20:39:48 +0000 | [diff] [blame] | 1193 | pcx = (int)(pOp - aOp); |
drh | 99f5de7 | 2016-04-30 02:59:15 +0000 | [diff] [blame] | 1194 | 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] | 1195 | } |
drh | 92f02c3 | 2004-09-02 14:57:08 +0000 | [diff] [blame] | 1196 | rc = sqlite3VdbeHalt(p); |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 1197 | assert( rc==SQLITE_BUSY || rc==SQLITE_OK || rc==SQLITE_ERROR ); |
drh | 92f02c3 | 2004-09-02 14:57:08 +0000 | [diff] [blame] | 1198 | if( rc==SQLITE_BUSY ){ |
drh | 99f5de7 | 2016-04-30 02:59:15 +0000 | [diff] [blame] | 1199 | p->rc = SQLITE_BUSY; |
drh | 900b31e | 2007-08-28 02:27:51 +0000 | [diff] [blame] | 1200 | }else{ |
drh | d91c1a1 | 2013-02-09 13:58:25 +0000 | [diff] [blame] | 1201 | assert( rc==SQLITE_OK || (p->rc&0xff)==SQLITE_CONSTRAINT ); |
dan | cb3e4b7 | 2013-07-03 19:53:05 +0000 | [diff] [blame] | 1202 | assert( rc==SQLITE_OK || db->nDeferredCons>0 || db->nDeferredImmCons>0 ); |
drh | 900b31e | 2007-08-28 02:27:51 +0000 | [diff] [blame] | 1203 | rc = p->rc ? SQLITE_ERROR : SQLITE_DONE; |
drh | 92f02c3 | 2004-09-02 14:57:08 +0000 | [diff] [blame] | 1204 | } |
drh | 900b31e | 2007-08-28 02:27:51 +0000 | [diff] [blame] | 1205 | goto vdbe_return; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1206 | } |
drh | c61053b | 2000-06-04 12:58:36 +0000 | [diff] [blame] | 1207 | |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 1208 | /* Opcode: Integer P1 P2 * * * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 1209 | ** Synopsis: r[P2]=P1 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1210 | ** |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1211 | ** The 32-bit integer value P1 is written into register P2. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1212 | */ |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 1213 | case OP_Integer: { /* out2 */ |
| 1214 | pOut = out2Prerelease(p, pOp); |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 1215 | pOut->u.i = pOp->p1; |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 1216 | break; |
| 1217 | } |
| 1218 | |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 1219 | /* Opcode: Int64 * P2 * P4 * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 1220 | ** Synopsis: r[P2]=P4 |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 1221 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 1222 | ** P4 is a pointer to a 64-bit integer value. |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1223 | ** Write that value into register P2. |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 1224 | */ |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 1225 | case OP_Int64: { /* out2 */ |
| 1226 | pOut = out2Prerelease(p, pOp); |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 1227 | assert( pOp->p4.pI64!=0 ); |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 1228 | pOut->u.i = *pOp->p4.pI64; |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 1229 | break; |
| 1230 | } |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 1231 | |
drh | 13573c7 | 2010-01-12 17:04:07 +0000 | [diff] [blame] | 1232 | #ifndef SQLITE_OMIT_FLOATING_POINT |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 1233 | /* Opcode: Real * P2 * P4 * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 1234 | ** Synopsis: r[P2]=P4 |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 1235 | ** |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 1236 | ** P4 is a pointer to a 64-bit floating point value. |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1237 | ** Write that value into register P2. |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 1238 | */ |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 1239 | case OP_Real: { /* same as TK_FLOAT, out2 */ |
| 1240 | pOut = out2Prerelease(p, pOp); |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 1241 | pOut->flags = MEM_Real; |
drh | 2eaf93d | 2008-04-29 00:15:20 +0000 | [diff] [blame] | 1242 | assert( !sqlite3IsNaN(*pOp->p4.pReal) ); |
drh | 74eaba4 | 2014-09-18 17:52:15 +0000 | [diff] [blame] | 1243 | pOut->u.r = *pOp->p4.pReal; |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 1244 | break; |
| 1245 | } |
drh | 13573c7 | 2010-01-12 17:04:07 +0000 | [diff] [blame] | 1246 | #endif |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 1247 | |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 1248 | /* Opcode: String8 * P2 * P4 * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 1249 | ** Synopsis: r[P2]='P4' |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 1250 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 1251 | ** P4 points to a nul terminated UTF-8 string. This opcode is transformed |
drh | f07cf6e | 2015-03-06 16:45:16 +0000 | [diff] [blame] | 1252 | ** into a String opcode before it is executed for the first time. During |
drh | 0fd6135 | 2014-02-07 02:29:45 +0000 | [diff] [blame] | 1253 | ** this transformation, the length of string P4 is computed and stored |
| 1254 | ** as the P1 parameter. |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 1255 | */ |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 1256 | case OP_String8: { /* same as TK_STRING, out2 */ |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 1257 | assert( pOp->p4.z!=0 ); |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 1258 | pOut = out2Prerelease(p, pOp); |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 1259 | pOp->p1 = sqlite3Strlen30(pOp->p4.z); |
drh | ed2df7f | 2005-11-16 04:34:32 +0000 | [diff] [blame] | 1260 | |
| 1261 | #ifndef SQLITE_OMIT_UTF16 |
drh | 8079a0d | 2006-01-12 17:20:50 +0000 | [diff] [blame] | 1262 | if( encoding!=SQLITE_UTF8 ){ |
drh | 3a9cf17 | 2009-06-17 21:42:33 +0000 | [diff] [blame] | 1263 | rc = sqlite3VdbeMemSetStr(pOut, pOp->p4.z, -1, SQLITE_UTF8, SQLITE_STATIC); |
drh | 2f55511 | 2016-04-30 18:10:34 +0000 | [diff] [blame] | 1264 | assert( rc==SQLITE_OK || rc==SQLITE_TOOBIG ); |
drh | dbdddc9 | 2019-02-21 16:41:34 +0000 | [diff] [blame] | 1265 | if( rc ) goto too_big; |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 1266 | if( SQLITE_OK!=sqlite3VdbeChangeEncoding(pOut, encoding) ) goto no_mem; |
drh | 17bcb10 | 2014-09-18 21:25:33 +0000 | [diff] [blame] | 1267 | assert( pOut->szMalloc>0 && pOut->zMalloc==pOut->z ); |
drh | c91b2fd | 2014-03-01 18:13:23 +0000 | [diff] [blame] | 1268 | assert( VdbeMemDynamic(pOut)==0 ); |
drh | 17bcb10 | 2014-09-18 21:25:33 +0000 | [diff] [blame] | 1269 | pOut->szMalloc = 0; |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 1270 | pOut->flags |= MEM_Static; |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 1271 | if( pOp->p4type==P4_DYNAMIC ){ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 1272 | sqlite3DbFree(db, pOp->p4.z); |
danielk1977 | e004840 | 2004-06-15 16:51:01 +0000 | [diff] [blame] | 1273 | } |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 1274 | pOp->p4type = P4_DYNAMIC; |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 1275 | pOp->p4.z = pOut->z; |
| 1276 | pOp->p1 = pOut->n; |
danielk1977 | 0f69c1e | 2004-05-29 11:24:50 +0000 | [diff] [blame] | 1277 | } |
danielk1977 | 93758c8 | 2005-01-21 08:13:14 +0000 | [diff] [blame] | 1278 | #endif |
drh | bb4957f | 2008-03-20 14:03:29 +0000 | [diff] [blame] | 1279 | if( pOp->p1>db->aLimit[SQLITE_LIMIT_LENGTH] ){ |
drh | cbd2da9 | 2007-12-17 16:20:06 +0000 | [diff] [blame] | 1280 | goto too_big; |
| 1281 | } |
drh | ec722c1 | 2019-09-17 21:28:54 +0000 | [diff] [blame] | 1282 | pOp->opcode = OP_String; |
drh | 2f55511 | 2016-04-30 18:10:34 +0000 | [diff] [blame] | 1283 | assert( rc==SQLITE_OK ); |
drh | cbd2da9 | 2007-12-17 16:20:06 +0000 | [diff] [blame] | 1284 | /* Fall through to the next case, OP_String */ |
drh | 08b9208 | 2020-08-10 14:18:00 +0000 | [diff] [blame] | 1285 | /* no break */ deliberate_fall_through |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 1286 | } |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 1287 | |
drh | f07cf6e | 2015-03-06 16:45:16 +0000 | [diff] [blame] | 1288 | /* Opcode: String P1 P2 P3 P4 P5 |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 1289 | ** Synopsis: r[P2]='P4' (len=P1) |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 1290 | ** |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1291 | ** The string value P4 of length P1 (bytes) is stored in register P2. |
drh | f07cf6e | 2015-03-06 16:45:16 +0000 | [diff] [blame] | 1292 | ** |
drh | 44aebff | 2016-05-02 10:25:42 +0000 | [diff] [blame] | 1293 | ** 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] | 1294 | ** the datatype of the register P2 is converted to BLOB. The content is |
| 1295 | ** the same sequence of bytes, it is merely interpreted as a BLOB instead |
drh | 44aebff | 2016-05-02 10:25:42 +0000 | [diff] [blame] | 1296 | ** of a string, as if it had been CAST. In other words: |
| 1297 | ** |
| 1298 | ** 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] | 1299 | */ |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 1300 | case OP_String: { /* out2 */ |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 1301 | assert( pOp->p4.z!=0 ); |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 1302 | pOut = out2Prerelease(p, pOp); |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 1303 | pOut->flags = MEM_Str|MEM_Static|MEM_Term; |
| 1304 | pOut->z = pOp->p4.z; |
| 1305 | pOut->n = pOp->p1; |
| 1306 | pOut->enc = encoding; |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 1307 | UPDATE_MAX_BLOBSIZE(pOut); |
drh | 41d2e66 | 2015-12-01 21:23:07 +0000 | [diff] [blame] | 1308 | #ifndef SQLITE_LIKE_DOESNT_MATCH_BLOBS |
drh | 44aebff | 2016-05-02 10:25:42 +0000 | [diff] [blame] | 1309 | if( pOp->p3>0 ){ |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 1310 | assert( pOp->p3<=(p->nMem+1 - p->nCursor) ); |
drh | f07cf6e | 2015-03-06 16:45:16 +0000 | [diff] [blame] | 1311 | pIn3 = &aMem[pOp->p3]; |
| 1312 | assert( pIn3->flags & MEM_Int ); |
drh | 44aebff | 2016-05-02 10:25:42 +0000 | [diff] [blame] | 1313 | 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] | 1314 | } |
drh | 41d2e66 | 2015-12-01 21:23:07 +0000 | [diff] [blame] | 1315 | #endif |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 1316 | break; |
| 1317 | } |
| 1318 | |
drh | 2bd9f44 | 2022-04-17 20:30:52 +0000 | [diff] [blame] | 1319 | /* Opcode: BeginSubrtn * P2 * * * |
| 1320 | ** Synopsis: r[P2]=NULL |
| 1321 | ** |
| 1322 | ** Mark the beginning of a subroutine that can be entered in-line |
| 1323 | ** or that can be called using OP_Gosub. The subroutine should |
| 1324 | ** be terminated by an OP_Return instruction that has a P1 operand that |
| 1325 | ** is the same as the P2 operand to this opcode and that has P3 set to 1. |
| 1326 | ** If the subroutine is entered in-line, then the OP_Return will simply |
| 1327 | ** fall through. But if the subroutine is entered using OP_Gosub, then |
| 1328 | ** the OP_Return will jump back to the first instruction after the OP_Gosub. |
| 1329 | ** |
| 1330 | ** This routine works by loading a NULL into the P2 register. When the |
| 1331 | ** return address register contains a NULL, the OP_Return instruction is |
| 1332 | ** a no-op that simply falls through to the next instruction (assuming that |
| 1333 | ** the OP_Return opcode has a P3 value of 1). Thus if the subroutine is |
| 1334 | ** entered in-line, then the OP_Return will cause in-line execution to |
| 1335 | ** continue. But if the subroutine is entered via OP_Gosub, then the |
| 1336 | ** OP_Return will cause a return to the address following the OP_Gosub. |
| 1337 | ** |
| 1338 | ** This opcode is identical to OP_Null. It has a different name |
| 1339 | ** only to make the byte code easier to read and verify. |
| 1340 | */ |
drh | 053a128 | 2012-09-19 21:15:46 +0000 | [diff] [blame] | 1341 | /* Opcode: Null P1 P2 P3 * * |
drh | 72e26de | 2016-08-24 21:24:04 +0000 | [diff] [blame] | 1342 | ** Synopsis: r[P2..P3]=NULL |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 1343 | ** |
drh | b8475df | 2011-12-09 16:21:19 +0000 | [diff] [blame] | 1344 | ** 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] | 1345 | ** 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] | 1346 | ** is less than P2 (typically P3 is zero) then only register P2 is |
drh | 053a128 | 2012-09-19 21:15:46 +0000 | [diff] [blame] | 1347 | ** set to NULL. |
| 1348 | ** |
| 1349 | ** If the P1 value is non-zero, then also set the MEM_Cleared flag so that |
| 1350 | ** NULL values will not compare equal even if SQLITE_NULLEQ is set on |
| 1351 | ** OP_Ne or OP_Eq. |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 1352 | */ |
drh | 2bd9f44 | 2022-04-17 20:30:52 +0000 | [diff] [blame] | 1353 | case OP_BeginSubrtn: |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 1354 | case OP_Null: { /* out2 */ |
drh | b8475df | 2011-12-09 16:21:19 +0000 | [diff] [blame] | 1355 | int cnt; |
drh | 053a128 | 2012-09-19 21:15:46 +0000 | [diff] [blame] | 1356 | u16 nullFlag; |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 1357 | pOut = out2Prerelease(p, pOp); |
drh | b8475df | 2011-12-09 16:21:19 +0000 | [diff] [blame] | 1358 | cnt = pOp->p3-pOp->p2; |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 1359 | assert( pOp->p3<=(p->nMem+1 - p->nCursor) ); |
drh | 053a128 | 2012-09-19 21:15:46 +0000 | [diff] [blame] | 1360 | pOut->flags = nullFlag = pOp->p1 ? (MEM_Null|MEM_Cleared) : MEM_Null; |
drh | 2a1df93 | 2016-09-30 17:46:44 +0000 | [diff] [blame] | 1361 | pOut->n = 0; |
drh | 2c885d0 | 2018-07-07 19:36:04 +0000 | [diff] [blame] | 1362 | #ifdef SQLITE_DEBUG |
| 1363 | pOut->uTemp = 0; |
| 1364 | #endif |
drh | b8475df | 2011-12-09 16:21:19 +0000 | [diff] [blame] | 1365 | while( cnt>0 ){ |
| 1366 | pOut++; |
| 1367 | memAboutToChange(p, pOut); |
drh | 0725cab | 2014-09-17 14:52:46 +0000 | [diff] [blame] | 1368 | sqlite3VdbeMemSetNull(pOut); |
drh | 053a128 | 2012-09-19 21:15:46 +0000 | [diff] [blame] | 1369 | pOut->flags = nullFlag; |
drh | 2a1df93 | 2016-09-30 17:46:44 +0000 | [diff] [blame] | 1370 | pOut->n = 0; |
drh | b8475df | 2011-12-09 16:21:19 +0000 | [diff] [blame] | 1371 | cnt--; |
| 1372 | } |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 1373 | break; |
| 1374 | } |
| 1375 | |
drh | 05a86c5 | 2014-02-16 01:55:49 +0000 | [diff] [blame] | 1376 | /* Opcode: SoftNull P1 * * * * |
drh | 72e26de | 2016-08-24 21:24:04 +0000 | [diff] [blame] | 1377 | ** Synopsis: r[P1]=NULL |
drh | 05a86c5 | 2014-02-16 01:55:49 +0000 | [diff] [blame] | 1378 | ** |
| 1379 | ** Set register P1 to have the value NULL as seen by the OP_MakeRecord |
| 1380 | ** instruction, but do not free any string or blob memory associated with |
| 1381 | ** the register, so that if the value was a string or blob that was |
| 1382 | ** previously copied using OP_SCopy, the copies will continue to be valid. |
| 1383 | */ |
| 1384 | case OP_SoftNull: { |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 1385 | assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) ); |
drh | 05a86c5 | 2014-02-16 01:55:49 +0000 | [diff] [blame] | 1386 | pOut = &aMem[pOp->p1]; |
drh | e2bc655 | 2017-04-17 20:50:34 +0000 | [diff] [blame] | 1387 | pOut->flags = (pOut->flags&~(MEM_Undefined|MEM_AffMask))|MEM_Null; |
drh | 05a86c5 | 2014-02-16 01:55:49 +0000 | [diff] [blame] | 1388 | break; |
| 1389 | } |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 1390 | |
drh | a5750cf | 2014-02-07 13:20:31 +0000 | [diff] [blame] | 1391 | /* Opcode: Blob P1 P2 * P4 * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 1392 | ** Synopsis: r[P2]=P4 (len=P1) |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 1393 | ** |
drh | 9de221d | 2008-01-05 06:51:30 +0000 | [diff] [blame] | 1394 | ** P4 points to a blob of data P1 bytes long. Store this |
drh | 50fb7e0 | 2021-12-06 20:16:53 +0000 | [diff] [blame] | 1395 | ** blob in register P2. If P4 is a NULL pointer, then construct |
| 1396 | ** a zero-filled blob that is P1 bytes long in P2. |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 1397 | */ |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 1398 | case OP_Blob: { /* out2 */ |
drh | cbd2da9 | 2007-12-17 16:20:06 +0000 | [diff] [blame] | 1399 | assert( pOp->p1 <= SQLITE_MAX_LENGTH ); |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 1400 | pOut = out2Prerelease(p, pOp); |
drh | 50fb7e0 | 2021-12-06 20:16:53 +0000 | [diff] [blame] | 1401 | if( pOp->p4.z==0 ){ |
| 1402 | sqlite3VdbeMemSetZeroBlob(pOut, pOp->p1); |
| 1403 | if( sqlite3VdbeMemExpandBlob(pOut) ) goto no_mem; |
| 1404 | }else{ |
| 1405 | sqlite3VdbeMemSetStr(pOut, pOp->p4.z, pOp->p1, 0, 0); |
| 1406 | } |
drh | 9de221d | 2008-01-05 06:51:30 +0000 | [diff] [blame] | 1407 | pOut->enc = encoding; |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 1408 | UPDATE_MAX_BLOBSIZE(pOut); |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 1409 | break; |
| 1410 | } |
| 1411 | |
drh | eaf52d8 | 2010-05-12 13:50:23 +0000 | [diff] [blame] | 1412 | /* Opcode: Variable P1 P2 * P4 * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 1413 | ** Synopsis: r[P2]=parameter(P1,P4) |
drh | 5045789 | 2003-09-06 01:10:47 +0000 | [diff] [blame] | 1414 | ** |
drh | eaf52d8 | 2010-05-12 13:50:23 +0000 | [diff] [blame] | 1415 | ** Transfer the values of bound parameter P1 into register P2 |
drh | 08de149 | 2009-02-20 03:55:05 +0000 | [diff] [blame] | 1416 | ** |
drh | 0fd6135 | 2014-02-07 02:29:45 +0000 | [diff] [blame] | 1417 | ** If the parameter is named, then its name appears in P4. |
drh | 08de149 | 2009-02-20 03:55:05 +0000 | [diff] [blame] | 1418 | ** The P4 value is used by sqlite3_bind_parameter_name(). |
drh | 5045789 | 2003-09-06 01:10:47 +0000 | [diff] [blame] | 1419 | */ |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 1420 | case OP_Variable: { /* out2 */ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1421 | Mem *pVar; /* Value being transferred */ |
| 1422 | |
drh | eaf52d8 | 2010-05-12 13:50:23 +0000 | [diff] [blame] | 1423 | assert( pOp->p1>0 && pOp->p1<=p->nVar ); |
drh | 9bf755c | 2016-12-23 03:59:31 +0000 | [diff] [blame] | 1424 | assert( pOp->p4.z==0 || pOp->p4.z==sqlite3VListNumToName(p->pVList,pOp->p1) ); |
drh | eaf52d8 | 2010-05-12 13:50:23 +0000 | [diff] [blame] | 1425 | pVar = &p->aVar[pOp->p1 - 1]; |
| 1426 | if( sqlite3VdbeMemTooBig(pVar) ){ |
| 1427 | goto too_big; |
drh | 023ae03 | 2007-05-08 12:12:16 +0000 | [diff] [blame] | 1428 | } |
drh | 7441df7 | 2017-01-09 19:27:04 +0000 | [diff] [blame] | 1429 | pOut = &aMem[pOp->p2]; |
drh | e0f20b4 | 2019-04-01 20:57:11 +0000 | [diff] [blame] | 1430 | if( VdbeMemDynamic(pOut) ) sqlite3VdbeMemSetNull(pOut); |
| 1431 | memcpy(pOut, pVar, MEMCELLSIZE); |
| 1432 | pOut->flags &= ~(MEM_Dyn|MEM_Ephem); |
| 1433 | pOut->flags |= MEM_Static|MEM_FromBind; |
drh | eaf52d8 | 2010-05-12 13:50:23 +0000 | [diff] [blame] | 1434 | UPDATE_MAX_BLOBSIZE(pOut); |
danielk1977 | 93d4675 | 2004-05-23 13:30:58 +0000 | [diff] [blame] | 1435 | break; |
| 1436 | } |
danielk1977 | 295ba55 | 2004-05-19 10:34:51 +0000 | [diff] [blame] | 1437 | |
drh | b21e7c7 | 2008-06-22 12:37:57 +0000 | [diff] [blame] | 1438 | /* Opcode: Move P1 P2 P3 * * |
drh | 72e26de | 2016-08-24 21:24:04 +0000 | [diff] [blame] | 1439 | ** Synopsis: r[P2@P3]=r[P1@P3] |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1440 | ** |
drh | 079a307 | 2014-03-19 14:10:55 +0000 | [diff] [blame] | 1441 | ** Move the P3 values in register P1..P1+P3-1 over into |
| 1442 | ** registers P2..P2+P3-1. Registers P1..P1+P3-1 are |
drh | b21e7c7 | 2008-06-22 12:37:57 +0000 | [diff] [blame] | 1443 | ** left holding a NULL. It is an error for register ranges |
drh | 079a307 | 2014-03-19 14:10:55 +0000 | [diff] [blame] | 1444 | ** P1..P1+P3-1 and P2..P2+P3-1 to overlap. It is an error |
| 1445 | ** for P3 to be less than 1. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1446 | */ |
drh | e1349cb | 2008-04-01 00:36:10 +0000 | [diff] [blame] | 1447 | case OP_Move: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1448 | int n; /* Number of registers left to copy */ |
| 1449 | int p1; /* Register to copy from */ |
| 1450 | int p2; /* Register to copy to */ |
| 1451 | |
drh | e09f43f | 2013-11-21 04:18:31 +0000 | [diff] [blame] | 1452 | n = pOp->p3; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1453 | p1 = pOp->p1; |
| 1454 | p2 = pOp->p2; |
drh | 079a307 | 2014-03-19 14:10:55 +0000 | [diff] [blame] | 1455 | assert( n>0 && p1>0 && p2>0 ); |
drh | b21e7c7 | 2008-06-22 12:37:57 +0000 | [diff] [blame] | 1456 | assert( p1+n<=p2 || p2+n<=p1 ); |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1457 | |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 1458 | pIn1 = &aMem[p1]; |
| 1459 | pOut = &aMem[p2]; |
drh | e09f43f | 2013-11-21 04:18:31 +0000 | [diff] [blame] | 1460 | do{ |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 1461 | assert( pOut<=&aMem[(p->nMem+1 - p->nCursor)] ); |
| 1462 | assert( pIn1<=&aMem[(p->nMem+1 - p->nCursor)] ); |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 1463 | assert( memIsValid(pIn1) ); |
| 1464 | memAboutToChange(p, pOut); |
drh | 17bcb10 | 2014-09-18 21:25:33 +0000 | [diff] [blame] | 1465 | sqlite3VdbeMemMove(pOut, pIn1); |
drh | 52043d7 | 2011-08-03 16:40:15 +0000 | [diff] [blame] | 1466 | #ifdef SQLITE_DEBUG |
drh | 4cbd847 | 2020-01-02 15:02:08 +0000 | [diff] [blame] | 1467 | pIn1->pScopyFrom = 0; |
| 1468 | { int i; |
| 1469 | for(i=1; i<p->nMem; i++){ |
| 1470 | if( aMem[i].pScopyFrom==pIn1 ){ |
| 1471 | aMem[i].pScopyFrom = pOut; |
| 1472 | } |
| 1473 | } |
drh | 52043d7 | 2011-08-03 16:40:15 +0000 | [diff] [blame] | 1474 | } |
| 1475 | #endif |
drh | bd6789e | 2015-04-28 14:00:02 +0000 | [diff] [blame] | 1476 | Deephemeralize(pOut); |
drh | b21e7c7 | 2008-06-22 12:37:57 +0000 | [diff] [blame] | 1477 | REGISTER_TRACE(p2++, pOut); |
| 1478 | pIn1++; |
| 1479 | pOut++; |
drh | 079a307 | 2014-03-19 14:10:55 +0000 | [diff] [blame] | 1480 | }while( --n ); |
drh | e1349cb | 2008-04-01 00:36:10 +0000 | [diff] [blame] | 1481 | break; |
| 1482 | } |
| 1483 | |
drh | e5dea28 | 2022-06-09 17:17:14 +0000 | [diff] [blame] | 1484 | /* Opcode: Copy P1 P2 P3 * P5 |
drh | 4eded60 | 2013-12-20 15:59:20 +0000 | [diff] [blame] | 1485 | ** Synopsis: r[P2@P3+1]=r[P1@P3+1] |
drh | b1fdb2a | 2008-01-05 04:06:03 +0000 | [diff] [blame] | 1486 | ** |
drh | e8e4af7 | 2012-09-21 00:04:28 +0000 | [diff] [blame] | 1487 | ** Make a copy of registers P1..P1+P3 into registers P2..P2+P3. |
drh | b1fdb2a | 2008-01-05 04:06:03 +0000 | [diff] [blame] | 1488 | ** |
drh | e5dea28 | 2022-06-09 17:17:14 +0000 | [diff] [blame] | 1489 | ** If the 0x0002 bit of P5 is set then also clear the MEM_Subtype flag in the |
| 1490 | ** destination. The 0x0001 bit of P5 indicates that this Copy opcode cannot |
| 1491 | ** be merged. The 0x0001 bit is used by the query planner and does not |
| 1492 | ** come into play during query execution. |
| 1493 | ** |
drh | b1fdb2a | 2008-01-05 04:06:03 +0000 | [diff] [blame] | 1494 | ** This instruction makes a deep copy of the value. A duplicate |
| 1495 | ** is made of any string or blob constant. See also OP_SCopy. |
| 1496 | */ |
drh | e8e4af7 | 2012-09-21 00:04:28 +0000 | [diff] [blame] | 1497 | case OP_Copy: { |
| 1498 | int n; |
| 1499 | |
| 1500 | n = pOp->p3; |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 1501 | pIn1 = &aMem[pOp->p1]; |
| 1502 | pOut = &aMem[pOp->p2]; |
drh | e1349cb | 2008-04-01 00:36:10 +0000 | [diff] [blame] | 1503 | assert( pOut!=pIn1 ); |
drh | e8e4af7 | 2012-09-21 00:04:28 +0000 | [diff] [blame] | 1504 | while( 1 ){ |
drh | 58773a5 | 2018-06-12 13:52:23 +0000 | [diff] [blame] | 1505 | memAboutToChange(p, pOut); |
drh | e8e4af7 | 2012-09-21 00:04:28 +0000 | [diff] [blame] | 1506 | sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem); |
| 1507 | Deephemeralize(pOut); |
drh | e5dea28 | 2022-06-09 17:17:14 +0000 | [diff] [blame] | 1508 | if( (pOut->flags & MEM_Subtype)!=0 && (pOp->p5 & 0x0002)!=0 ){ |
| 1509 | pOut->flags &= ~MEM_Subtype; |
| 1510 | } |
drh | 953f761 | 2012-12-07 22:18:54 +0000 | [diff] [blame] | 1511 | #ifdef SQLITE_DEBUG |
| 1512 | pOut->pScopyFrom = 0; |
| 1513 | #endif |
drh | e8e4af7 | 2012-09-21 00:04:28 +0000 | [diff] [blame] | 1514 | REGISTER_TRACE(pOp->p2+pOp->p3-n, pOut); |
| 1515 | if( (n--)==0 ) break; |
| 1516 | pOut++; |
| 1517 | pIn1++; |
| 1518 | } |
drh | e1349cb | 2008-04-01 00:36:10 +0000 | [diff] [blame] | 1519 | break; |
| 1520 | } |
| 1521 | |
drh | b1fdb2a | 2008-01-05 04:06:03 +0000 | [diff] [blame] | 1522 | /* Opcode: SCopy P1 P2 * * * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 1523 | ** Synopsis: r[P2]=r[P1] |
drh | b1fdb2a | 2008-01-05 04:06:03 +0000 | [diff] [blame] | 1524 | ** |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1525 | ** Make a shallow copy of register P1 into register P2. |
drh | b1fdb2a | 2008-01-05 04:06:03 +0000 | [diff] [blame] | 1526 | ** |
| 1527 | ** This instruction makes a shallow copy of the value. If the value |
| 1528 | ** is a string or blob, then the copy is only a pointer to the |
| 1529 | ** original and hence if the original changes so will the copy. |
| 1530 | ** Worse, if the original is deallocated, the copy becomes invalid. |
| 1531 | ** Thus the program must guarantee that the original will not change |
| 1532 | ** during the lifetime of the copy. Use OP_Copy to make a complete |
| 1533 | ** copy. |
| 1534 | */ |
drh | 26198bb | 2013-10-31 11:15:09 +0000 | [diff] [blame] | 1535 | case OP_SCopy: { /* out2 */ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 1536 | pIn1 = &aMem[pOp->p1]; |
| 1537 | pOut = &aMem[pOp->p2]; |
drh | 2d401ab | 2008-01-10 23:50:11 +0000 | [diff] [blame] | 1538 | assert( pOut!=pIn1 ); |
drh | e1349cb | 2008-04-01 00:36:10 +0000 | [diff] [blame] | 1539 | sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem); |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 1540 | #ifdef SQLITE_DEBUG |
drh | 58773a5 | 2018-06-12 13:52:23 +0000 | [diff] [blame] | 1541 | pOut->pScopyFrom = pIn1; |
| 1542 | pOut->mScopyFlags = pIn1->flags; |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 1543 | #endif |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1544 | break; |
| 1545 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1546 | |
drh | fed7ac6 | 2015-10-15 18:04:59 +0000 | [diff] [blame] | 1547 | /* Opcode: IntCopy P1 P2 * * * |
| 1548 | ** Synopsis: r[P2]=r[P1] |
| 1549 | ** |
| 1550 | ** Transfer the integer value held in register P1 into register P2. |
| 1551 | ** |
| 1552 | ** This is an optimized version of SCopy that works only for integer |
| 1553 | ** values. |
| 1554 | */ |
| 1555 | case OP_IntCopy: { /* out2 */ |
| 1556 | pIn1 = &aMem[pOp->p1]; |
| 1557 | assert( (pIn1->flags & MEM_Int)!=0 ); |
| 1558 | pOut = &aMem[pOp->p2]; |
| 1559 | sqlite3VdbeMemSetInt64(pOut, pIn1->u.i); |
| 1560 | break; |
| 1561 | } |
| 1562 | |
drh | 3b26b2b | 2021-12-01 19:17:14 +0000 | [diff] [blame] | 1563 | /* Opcode: FkCheck * * * * * |
drh | 18e5607 | 2021-01-31 15:50:36 +0000 | [diff] [blame] | 1564 | ** |
drh | 3b26b2b | 2021-12-01 19:17:14 +0000 | [diff] [blame] | 1565 | ** Halt with an SQLITE_CONSTRAINT error if there are any unresolved |
| 1566 | ** foreign key constraint violations. If there are no foreign key |
| 1567 | ** constraint violations, this is a no-op. |
drh | 18e5607 | 2021-01-31 15:50:36 +0000 | [diff] [blame] | 1568 | ** |
drh | 3b26b2b | 2021-12-01 19:17:14 +0000 | [diff] [blame] | 1569 | ** FK constraint violations are also checked when the prepared statement |
| 1570 | ** exits. This opcode is used to raise foreign key constraint errors prior |
| 1571 | ** to returning results such as a row change count or the result of a |
| 1572 | ** RETURNING clause. |
drh | 18e5607 | 2021-01-31 15:50:36 +0000 | [diff] [blame] | 1573 | */ |
drh | 3b26b2b | 2021-12-01 19:17:14 +0000 | [diff] [blame] | 1574 | case OP_FkCheck: { |
drh | 18e5607 | 2021-01-31 15:50:36 +0000 | [diff] [blame] | 1575 | if( (rc = sqlite3VdbeCheckFk(p,0))!=SQLITE_OK ){ |
| 1576 | goto abort_due_to_error; |
| 1577 | } |
drh | 3b26b2b | 2021-12-01 19:17:14 +0000 | [diff] [blame] | 1578 | break; |
drh | 18e5607 | 2021-01-31 15:50:36 +0000 | [diff] [blame] | 1579 | } |
| 1580 | |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1581 | /* Opcode: ResultRow P1 P2 * * * |
drh | 72e26de | 2016-08-24 21:24:04 +0000 | [diff] [blame] | 1582 | ** Synopsis: output=r[P1@P2] |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 1583 | ** |
shane | 21e7feb | 2008-05-30 15:59:49 +0000 | [diff] [blame] | 1584 | ** The registers P1 through P1+P2-1 contain a single row of |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 1585 | ** results. This opcode causes the sqlite3_step() call to terminate |
| 1586 | ** with an SQLITE_ROW return code and it sets up the sqlite3_stmt |
drh | 4d87aae | 2014-02-20 19:42:00 +0000 | [diff] [blame] | 1587 | ** 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] | 1588 | ** the result row. |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 1589 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1590 | case OP_ResultRow: { |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 1591 | assert( p->nResColumn==pOp->p2 ); |
drh | 9ce612a | 2021-04-05 22:30:56 +0000 | [diff] [blame] | 1592 | assert( pOp->p1>0 || CORRUPT_DB ); |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 1593 | assert( pOp->p1+pOp->p2<=(p->nMem+1 - p->nCursor)+1 ); |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 1594 | |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 1595 | p->cacheCtr = (p->cacheCtr + 2)|1; |
drh | 3b8b5be | 2022-04-01 20:19:36 +0000 | [diff] [blame] | 1596 | p->pResultSet = &aMem[pOp->p1]; |
drh | 02ff747 | 2019-12-31 12:18:24 +0000 | [diff] [blame] | 1597 | #ifdef SQLITE_DEBUG |
drh | 3b8b5be | 2022-04-01 20:19:36 +0000 | [diff] [blame] | 1598 | { |
| 1599 | Mem *pMem = p->pResultSet; |
| 1600 | int i; |
| 1601 | for(i=0; i<pOp->p2; i++){ |
| 1602 | assert( memIsValid(&pMem[i]) ); |
| 1603 | REGISTER_TRACE(pOp->p1+i, &pMem[i]); |
| 1604 | /* The registers in the result will not be used again when the |
| 1605 | ** prepared statement restarts. This is because sqlite3_column() |
| 1606 | ** APIs might have caused type conversions of made other changes to |
| 1607 | ** the register values. Therefore, we can go ahead and break any |
| 1608 | ** OP_SCopy dependencies. */ |
| 1609 | pMem[i].pScopyFrom = 0; |
| 1610 | } |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 1611 | } |
drh | 3b8b5be | 2022-04-01 20:19:36 +0000 | [diff] [blame] | 1612 | #endif |
drh | 2803969 | 2008-03-17 16:54:01 +0000 | [diff] [blame] | 1613 | if( db->mallocFailed ) goto no_mem; |
drh | 3d2a529 | 2016-07-13 22:55:01 +0000 | [diff] [blame] | 1614 | if( db->mTrace & SQLITE_TRACE_ROW ){ |
drh | 08b9208 | 2020-08-10 14:18:00 +0000 | [diff] [blame] | 1615 | db->trace.xV2(SQLITE_TRACE_ROW, db->pTraceArg, p, 0); |
drh | 3d2a529 | 2016-07-13 22:55:01 +0000 | [diff] [blame] | 1616 | } |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 1617 | p->pc = (int)(pOp - aOp) + 1; |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 1618 | rc = SQLITE_ROW; |
| 1619 | goto vdbe_return; |
| 1620 | } |
| 1621 | |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1622 | /* Opcode: Concat P1 P2 P3 * * |
drh | 313619f | 2013-10-31 20:34:06 +0000 | [diff] [blame] | 1623 | ** Synopsis: r[P3]=r[P2]+r[P1] |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1624 | ** |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1625 | ** Add the text in register P1 onto the end of the text in |
| 1626 | ** register P2 and store the result in register P3. |
| 1627 | ** 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] | 1628 | ** |
| 1629 | ** P3 = P2 || P1 |
| 1630 | ** |
| 1631 | ** It is illegal for P1 and P3 to be the same register. Sometimes, |
| 1632 | ** if P3 is the same register as P2, the implementation is able |
| 1633 | ** to avoid a memcpy(). |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1634 | */ |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1635 | case OP_Concat: { /* same as TK_CONCAT, in1, in2, out3 */ |
drh | 8a7e11f | 2019-05-01 15:32:40 +0000 | [diff] [blame] | 1636 | i64 nByte; /* Total size of the output string or blob */ |
| 1637 | u16 flags1; /* Initial flags for P1 */ |
| 1638 | u16 flags2; /* Initial flags for P2 */ |
danielk1977 | 8a6b541 | 2004-05-24 07:04:25 +0000 | [diff] [blame] | 1639 | |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 1640 | pIn1 = &aMem[pOp->p1]; |
| 1641 | pIn2 = &aMem[pOp->p2]; |
| 1642 | pOut = &aMem[pOp->p3]; |
drh | 8a7e11f | 2019-05-01 15:32:40 +0000 | [diff] [blame] | 1643 | testcase( pOut==pIn2 ); |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1644 | assert( pIn1!=pOut ); |
drh | 8a7e11f | 2019-05-01 15:32:40 +0000 | [diff] [blame] | 1645 | flags1 = pIn1->flags; |
| 1646 | testcase( flags1 & MEM_Null ); |
| 1647 | testcase( pIn2->flags & MEM_Null ); |
| 1648 | if( (flags1 | pIn2->flags) & MEM_Null ){ |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1649 | sqlite3VdbeMemSetNull(pOut); |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1650 | break; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1651 | } |
drh | 8a7e11f | 2019-05-01 15:32:40 +0000 | [diff] [blame] | 1652 | if( (flags1 & (MEM_Str|MEM_Blob))==0 ){ |
| 1653 | if( sqlite3VdbeMemStringify(pIn1,encoding,0) ) goto no_mem; |
drh | 01325a3 | 2019-05-02 00:52:50 +0000 | [diff] [blame] | 1654 | flags1 = pIn1->flags & ~MEM_Str; |
drh | 8a7e11f | 2019-05-01 15:32:40 +0000 | [diff] [blame] | 1655 | }else if( (flags1 & MEM_Zero)!=0 ){ |
| 1656 | if( sqlite3VdbeMemExpandBlob(pIn1) ) goto no_mem; |
drh | 01325a3 | 2019-05-02 00:52:50 +0000 | [diff] [blame] | 1657 | flags1 = pIn1->flags & ~MEM_Str; |
drh | 8a7e11f | 2019-05-01 15:32:40 +0000 | [diff] [blame] | 1658 | } |
| 1659 | flags2 = pIn2->flags; |
| 1660 | if( (flags2 & (MEM_Str|MEM_Blob))==0 ){ |
| 1661 | if( sqlite3VdbeMemStringify(pIn2,encoding,0) ) goto no_mem; |
drh | 01325a3 | 2019-05-02 00:52:50 +0000 | [diff] [blame] | 1662 | flags2 = pIn2->flags & ~MEM_Str; |
drh | 8a7e11f | 2019-05-01 15:32:40 +0000 | [diff] [blame] | 1663 | }else if( (flags2 & MEM_Zero)!=0 ){ |
| 1664 | if( sqlite3VdbeMemExpandBlob(pIn2) ) goto no_mem; |
drh | 01325a3 | 2019-05-02 00:52:50 +0000 | [diff] [blame] | 1665 | flags2 = pIn2->flags & ~MEM_Str; |
drh | 8a7e11f | 2019-05-01 15:32:40 +0000 | [diff] [blame] | 1666 | } |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1667 | nByte = pIn1->n + pIn2->n; |
drh | bb4957f | 2008-03-20 14:03:29 +0000 | [diff] [blame] | 1668 | if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){ |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1669 | goto too_big; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1670 | } |
drh | 01156ec | 2022-06-17 21:31:30 +0000 | [diff] [blame] | 1671 | if( sqlite3VdbeMemGrow(pOut, (int)nByte+2, pOut==pIn2) ){ |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1672 | goto no_mem; |
| 1673 | } |
drh | c91b2fd | 2014-03-01 18:13:23 +0000 | [diff] [blame] | 1674 | MemSetTypeFlag(pOut, MEM_Str); |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1675 | if( pOut!=pIn2 ){ |
| 1676 | memcpy(pOut->z, pIn2->z, pIn2->n); |
drh | 8a7e11f | 2019-05-01 15:32:40 +0000 | [diff] [blame] | 1677 | assert( (pIn2->flags & MEM_Dyn) == (flags2 & MEM_Dyn) ); |
| 1678 | pIn2->flags = flags2; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1679 | } |
| 1680 | memcpy(&pOut->z[pIn2->n], pIn1->z, pIn1->n); |
drh | 8a7e11f | 2019-05-01 15:32:40 +0000 | [diff] [blame] | 1681 | assert( (pIn1->flags & MEM_Dyn) == (flags1 & MEM_Dyn) ); |
| 1682 | pIn1->flags = flags1; |
drh | 01156ec | 2022-06-17 21:31:30 +0000 | [diff] [blame] | 1683 | if( encoding>SQLITE_UTF8 ) nByte &= ~1; |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 1684 | pOut->z[nByte]=0; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1685 | pOut->z[nByte+1] = 0; |
| 1686 | pOut->flags |= MEM_Term; |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 1687 | pOut->n = (int)nByte; |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1688 | pOut->enc = encoding; |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 1689 | UPDATE_MAX_BLOBSIZE(pOut); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1690 | break; |
| 1691 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1692 | |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 1693 | /* Opcode: Add P1 P2 P3 * * |
drh | 72e26de | 2016-08-24 21:24:04 +0000 | [diff] [blame] | 1694 | ** Synopsis: r[P3]=r[P1]+r[P2] |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1695 | ** |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1696 | ** Add the value in register P1 to the value in register P2 |
shane | 21e7feb | 2008-05-30 15:59:49 +0000 | [diff] [blame] | 1697 | ** and store the result in register P3. |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1698 | ** If either input is NULL, the result is NULL. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1699 | */ |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 1700 | /* Opcode: Multiply P1 P2 P3 * * |
drh | 72e26de | 2016-08-24 21:24:04 +0000 | [diff] [blame] | 1701 | ** Synopsis: r[P3]=r[P1]*r[P2] |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1702 | ** |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 1703 | ** |
shane | 21e7feb | 2008-05-30 15:59:49 +0000 | [diff] [blame] | 1704 | ** Multiply the value in register P1 by the value in register P2 |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1705 | ** and store the result in register P3. |
| 1706 | ** If either input is NULL, the result is NULL. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1707 | */ |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 1708 | /* Opcode: Subtract P1 P2 P3 * * |
drh | 72e26de | 2016-08-24 21:24:04 +0000 | [diff] [blame] | 1709 | ** Synopsis: r[P3]=r[P2]-r[P1] |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1710 | ** |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1711 | ** Subtract the value in register P1 from the value in register P2 |
| 1712 | ** and store the result in register P3. |
| 1713 | ** If either input is NULL, the result is NULL. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1714 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1715 | /* Opcode: Divide P1 P2 P3 * * |
drh | 72e26de | 2016-08-24 21:24:04 +0000 | [diff] [blame] | 1716 | ** Synopsis: r[P3]=r[P2]/r[P1] |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1717 | ** |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1718 | ** Divide the value in register P1 by the value in register P2 |
dan | e275dc3 | 2009-08-18 16:24:58 +0000 | [diff] [blame] | 1719 | ** and store the result in register P3 (P3=P2/P1). If the value in |
| 1720 | ** register P1 is zero, then the result is NULL. If either input is |
| 1721 | ** NULL, the result is NULL. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1722 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1723 | /* Opcode: Remainder P1 P2 P3 * * |
drh | 72e26de | 2016-08-24 21:24:04 +0000 | [diff] [blame] | 1724 | ** Synopsis: r[P3]=r[P2]%r[P1] |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1725 | ** |
drh | 40864a1 | 2013-11-15 18:58:37 +0000 | [diff] [blame] | 1726 | ** Compute the remainder after integer register P2 is divided by |
| 1727 | ** register P1 and store the result in register P3. |
| 1728 | ** If the value in register P1 is zero the result is NULL. |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1729 | ** If either operand is NULL, the result is NULL. |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1730 | */ |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1731 | case OP_Add: /* same as TK_PLUS, in1, in2, out3 */ |
| 1732 | case OP_Subtract: /* same as TK_MINUS, in1, in2, out3 */ |
| 1733 | case OP_Multiply: /* same as TK_STAR, in1, in2, out3 */ |
| 1734 | case OP_Divide: /* same as TK_SLASH, in1, in2, out3 */ |
| 1735 | case OP_Remainder: { /* same as TK_REM, in1, in2, out3 */ |
drh | 3d1d90a | 2014-03-24 15:00:15 +0000 | [diff] [blame] | 1736 | u16 type1; /* Numeric type of left operand */ |
| 1737 | u16 type2; /* Numeric type of right operand */ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1738 | i64 iA; /* Integer value of left operand */ |
| 1739 | i64 iB; /* Integer value of right operand */ |
| 1740 | double rA; /* Real value of left operand */ |
| 1741 | double rB; /* Real value of right operand */ |
| 1742 | |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 1743 | pIn1 = &aMem[pOp->p1]; |
drh | 3cf46ee | 2022-08-03 19:53:54 +0000 | [diff] [blame] | 1744 | type1 = pIn1->flags; |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 1745 | pIn2 = &aMem[pOp->p2]; |
drh | 3cf46ee | 2022-08-03 19:53:54 +0000 | [diff] [blame] | 1746 | type2 = pIn2->flags; |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 1747 | pOut = &aMem[pOp->p3]; |
drh | 3d1d90a | 2014-03-24 15:00:15 +0000 | [diff] [blame] | 1748 | if( (type1 & type2 & MEM_Int)!=0 ){ |
drh | 3cf46ee | 2022-08-03 19:53:54 +0000 | [diff] [blame] | 1749 | int_math: |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1750 | iA = pIn1->u.i; |
| 1751 | iB = pIn2->u.i; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1752 | switch( pOp->opcode ){ |
drh | 158b9cb | 2011-03-05 20:59:46 +0000 | [diff] [blame] | 1753 | case OP_Add: if( sqlite3AddInt64(&iB,iA) ) goto fp_math; break; |
| 1754 | case OP_Subtract: if( sqlite3SubInt64(&iB,iA) ) goto fp_math; break; |
| 1755 | case OP_Multiply: if( sqlite3MulInt64(&iB,iA) ) goto fp_math; break; |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1756 | case OP_Divide: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1757 | if( iA==0 ) goto arithmetic_result_is_null; |
drh | 158b9cb | 2011-03-05 20:59:46 +0000 | [diff] [blame] | 1758 | if( iA==-1 && iB==SMALLEST_INT64 ) goto fp_math; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1759 | iB /= iA; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1760 | break; |
| 1761 | } |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1762 | default: { |
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 | iB %= iA; |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1766 | break; |
| 1767 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1768 | } |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1769 | pOut->u.i = iB; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1770 | MemSetTypeFlag(pOut, MEM_Int); |
drh | 3cf46ee | 2022-08-03 19:53:54 +0000 | [diff] [blame] | 1771 | }else if( ((type1 | type2) & MEM_Null)!=0 ){ |
drh | cfcca02 | 2017-04-17 23:23:17 +0000 | [diff] [blame] | 1772 | goto arithmetic_result_is_null; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1773 | }else{ |
drh | 3cf46ee | 2022-08-03 19:53:54 +0000 | [diff] [blame] | 1774 | type1 = numericType(pIn1); |
| 1775 | type2 = numericType(pIn2); |
| 1776 | if( (type1 & type2 & MEM_Int)!=0 ) goto int_math; |
drh | 158b9cb | 2011-03-05 20:59:46 +0000 | [diff] [blame] | 1777 | fp_math: |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1778 | rA = sqlite3VdbeRealValue(pIn1); |
| 1779 | rB = sqlite3VdbeRealValue(pIn2); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1780 | switch( pOp->opcode ){ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1781 | case OP_Add: rB += rA; break; |
| 1782 | case OP_Subtract: rB -= rA; break; |
| 1783 | case OP_Multiply: rB *= rA; break; |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1784 | case OP_Divide: { |
shane | fbd60f8 | 2009-02-04 03:59:25 +0000 | [diff] [blame] | 1785 | /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1786 | if( rA==(double)0 ) goto arithmetic_result_is_null; |
| 1787 | rB /= rA; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1788 | break; |
| 1789 | } |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1790 | default: { |
drh | e3b89d2 | 2019-01-18 17:53:50 +0000 | [diff] [blame] | 1791 | iA = sqlite3VdbeIntValue(pIn1); |
| 1792 | iB = sqlite3VdbeIntValue(pIn2); |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1793 | if( iA==0 ) goto arithmetic_result_is_null; |
| 1794 | if( iA==-1 ) iA = 1; |
| 1795 | rB = (double)(iB % iA); |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1796 | break; |
| 1797 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1798 | } |
drh | c5a7b51 | 2010-01-13 16:25:42 +0000 | [diff] [blame] | 1799 | #ifdef SQLITE_OMIT_FLOATING_POINT |
| 1800 | pOut->u.i = rB; |
| 1801 | MemSetTypeFlag(pOut, MEM_Int); |
| 1802 | #else |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1803 | if( sqlite3IsNaN(rB) ){ |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 1804 | goto arithmetic_result_is_null; |
drh | 53c1402 | 2007-05-10 17:23:11 +0000 | [diff] [blame] | 1805 | } |
drh | 74eaba4 | 2014-09-18 17:52:15 +0000 | [diff] [blame] | 1806 | pOut->u.r = rB; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1807 | MemSetTypeFlag(pOut, MEM_Real); |
drh | c5a7b51 | 2010-01-13 16:25:42 +0000 | [diff] [blame] | 1808 | #endif |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1809 | } |
| 1810 | break; |
| 1811 | |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 1812 | arithmetic_result_is_null: |
| 1813 | sqlite3VdbeMemSetNull(pOut); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1814 | break; |
| 1815 | } |
| 1816 | |
drh | 7a95789 | 2012-02-02 17:35:43 +0000 | [diff] [blame] | 1817 | /* Opcode: CollSeq P1 * * P4 |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 1818 | ** |
drh | bb6783b | 2017-04-29 18:02:49 +0000 | [diff] [blame] | 1819 | ** 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] | 1820 | ** or aggregate calls sqlite3GetFuncCollSeq(), this collation sequence will |
| 1821 | ** be returned. This is used by the built-in min(), max() and nullif() |
drh | e6f85e7 | 2004-12-25 01:03:13 +0000 | [diff] [blame] | 1822 | ** functions. |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 1823 | ** |
drh | 7a95789 | 2012-02-02 17:35:43 +0000 | [diff] [blame] | 1824 | ** If P1 is not zero, then it is a register that a subsequent min() or |
| 1825 | ** max() aggregate will set to 1 if the current row is not the minimum or |
| 1826 | ** maximum. The P1 register is initialized to 0 by this instruction. |
| 1827 | ** |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 1828 | ** The interface used by the implementation of the aforementioned functions |
| 1829 | ** to retrieve the collation sequence set by this opcode is not available |
drh | 0a0d056 | 2015-03-12 05:08:34 +0000 | [diff] [blame] | 1830 | ** publicly. Only built-in functions have access to this feature. |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 1831 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1832 | case OP_CollSeq: { |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 1833 | assert( pOp->p4type==P4_COLLSEQ ); |
drh | 7a95789 | 2012-02-02 17:35:43 +0000 | [diff] [blame] | 1834 | if( pOp->p1 ){ |
| 1835 | sqlite3VdbeMemSetInt64(&aMem[pOp->p1], 0); |
| 1836 | } |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 1837 | break; |
| 1838 | } |
| 1839 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1840 | /* Opcode: BitAnd P1 P2 P3 * * |
drh | 72e26de | 2016-08-24 21:24:04 +0000 | [diff] [blame] | 1841 | ** Synopsis: r[P3]=r[P1]&r[P2] |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1842 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1843 | ** Take the bit-wise AND of the values in register P1 and P2 and |
| 1844 | ** store the result in register P3. |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1845 | ** If either input is NULL, the result is NULL. |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1846 | */ |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1847 | /* Opcode: BitOr P1 P2 P3 * * |
drh | 72e26de | 2016-08-24 21:24:04 +0000 | [diff] [blame] | 1848 | ** Synopsis: r[P3]=r[P1]|r[P2] |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1849 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1850 | ** Take the bit-wise OR of the values in register P1 and P2 and |
| 1851 | ** store the result in register P3. |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1852 | ** If either input is NULL, the result is NULL. |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1853 | */ |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1854 | /* Opcode: ShiftLeft P1 P2 P3 * * |
drh | 72e26de | 2016-08-24 21:24:04 +0000 | [diff] [blame] | 1855 | ** Synopsis: r[P3]=r[P2]<<r[P1] |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1856 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1857 | ** Shift the integer value in register P2 to the left by the |
drh | 710c484 | 2010-08-30 01:17:20 +0000 | [diff] [blame] | 1858 | ** number of bits specified by the integer in register P1. |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1859 | ** Store the result in register P3. |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1860 | ** If either input is NULL, the result is NULL. |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1861 | */ |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1862 | /* Opcode: ShiftRight P1 P2 P3 * * |
drh | 72e26de | 2016-08-24 21:24:04 +0000 | [diff] [blame] | 1863 | ** Synopsis: r[P3]=r[P2]>>r[P1] |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1864 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1865 | ** Shift the integer value in register P2 to the right by the |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1866 | ** number of bits specified by the integer in register P1. |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1867 | ** Store the result in register P3. |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1868 | ** If either input is NULL, the result is NULL. |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1869 | */ |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1870 | case OP_BitAnd: /* same as TK_BITAND, in1, in2, out3 */ |
| 1871 | case OP_BitOr: /* same as TK_BITOR, in1, in2, out3 */ |
| 1872 | case OP_ShiftLeft: /* same as TK_LSHIFT, in1, in2, out3 */ |
| 1873 | case OP_ShiftRight: { /* same as TK_RSHIFT, in1, in2, out3 */ |
drh | 158b9cb | 2011-03-05 20:59:46 +0000 | [diff] [blame] | 1874 | i64 iA; |
| 1875 | u64 uA; |
| 1876 | i64 iB; |
| 1877 | u8 op; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1878 | |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 1879 | pIn1 = &aMem[pOp->p1]; |
| 1880 | pIn2 = &aMem[pOp->p2]; |
| 1881 | pOut = &aMem[pOp->p3]; |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1882 | if( (pIn1->flags | pIn2->flags) & MEM_Null ){ |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 1883 | sqlite3VdbeMemSetNull(pOut); |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1884 | break; |
| 1885 | } |
drh | 158b9cb | 2011-03-05 20:59:46 +0000 | [diff] [blame] | 1886 | iA = sqlite3VdbeIntValue(pIn2); |
| 1887 | iB = sqlite3VdbeIntValue(pIn1); |
| 1888 | op = pOp->opcode; |
| 1889 | if( op==OP_BitAnd ){ |
| 1890 | iA &= iB; |
| 1891 | }else if( op==OP_BitOr ){ |
| 1892 | iA |= iB; |
| 1893 | }else if( iB!=0 ){ |
| 1894 | assert( op==OP_ShiftRight || op==OP_ShiftLeft ); |
| 1895 | |
| 1896 | /* If shifting by a negative amount, shift in the other direction */ |
| 1897 | if( iB<0 ){ |
| 1898 | assert( OP_ShiftRight==OP_ShiftLeft+1 ); |
| 1899 | op = 2*OP_ShiftLeft + 1 - op; |
| 1900 | iB = iB>(-64) ? -iB : 64; |
| 1901 | } |
| 1902 | |
| 1903 | if( iB>=64 ){ |
| 1904 | iA = (iA>=0 || op==OP_ShiftLeft) ? 0 : -1; |
| 1905 | }else{ |
| 1906 | memcpy(&uA, &iA, sizeof(uA)); |
| 1907 | if( op==OP_ShiftLeft ){ |
| 1908 | uA <<= iB; |
| 1909 | }else{ |
| 1910 | uA >>= iB; |
| 1911 | /* Sign-extend on a right shift of a negative number */ |
| 1912 | if( iA<0 ) uA |= ((((u64)0xffffffff)<<32)|0xffffffff) << (64-iB); |
| 1913 | } |
| 1914 | memcpy(&iA, &uA, sizeof(iA)); |
| 1915 | } |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1916 | } |
drh | 158b9cb | 2011-03-05 20:59:46 +0000 | [diff] [blame] | 1917 | pOut->u.i = iA; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1918 | MemSetTypeFlag(pOut, MEM_Int); |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1919 | break; |
| 1920 | } |
| 1921 | |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1922 | /* Opcode: AddImm P1 P2 * * * |
drh | 72e26de | 2016-08-24 21:24:04 +0000 | [diff] [blame] | 1923 | ** Synopsis: r[P1]=r[P1]+P2 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1924 | ** |
danielk1977 | 0cdc022 | 2008-06-26 18:04:03 +0000 | [diff] [blame] | 1925 | ** Add the constant P2 to the value in register P1. |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1926 | ** The result is always an integer. |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 1927 | ** |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1928 | ** To force any register to be an integer, just add 0. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1929 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1930 | case OP_AddImm: { /* in1 */ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 1931 | pIn1 = &aMem[pOp->p1]; |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 1932 | memAboutToChange(p, pIn1); |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1933 | sqlite3VdbeMemIntegerify(pIn1); |
| 1934 | pIn1->u.i += pOp->p2; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1935 | break; |
| 1936 | } |
| 1937 | |
dan | e5166e0 | 2019-03-19 11:56:39 +0000 | [diff] [blame] | 1938 | /* Opcode: MustBeInt P1 P2 * * * |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 1939 | ** |
dan | e5166e0 | 2019-03-19 11:56:39 +0000 | [diff] [blame] | 1940 | ** Force the value in register P1 to be an integer. If the value |
| 1941 | ** in P1 is not an integer and cannot be converted into an integer |
| 1942 | ** without data loss, then jump immediately to P2, or if P2==0 |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 1943 | ** raise an SQLITE_MISMATCH exception. |
| 1944 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1945 | case OP_MustBeInt: { /* jump, in1 */ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 1946 | pIn1 = &aMem[pOp->p1]; |
dan | e5166e0 | 2019-03-19 11:56:39 +0000 | [diff] [blame] | 1947 | if( (pIn1->flags & MEM_Int)==0 ){ |
drh | 83b301b | 2013-11-20 00:59:02 +0000 | [diff] [blame] | 1948 | applyAffinity(pIn1, SQLITE_AFF_NUMERIC, encoding); |
dan | e5166e0 | 2019-03-19 11:56:39 +0000 | [diff] [blame] | 1949 | if( (pIn1->flags & MEM_Int)==0 ){ |
drh | c906533 | 2019-04-01 14:01:21 +0000 | [diff] [blame] | 1950 | VdbeBranchTaken(1, 2); |
drh | 83b301b | 2013-11-20 00:59:02 +0000 | [diff] [blame] | 1951 | if( pOp->p2==0 ){ |
| 1952 | rc = SQLITE_MISMATCH; |
| 1953 | goto abort_due_to_error; |
| 1954 | }else{ |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 1955 | goto jump_to_p2; |
drh | 83b301b | 2013-11-20 00:59:02 +0000 | [diff] [blame] | 1956 | } |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 1957 | } |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 1958 | } |
drh | c906533 | 2019-04-01 14:01:21 +0000 | [diff] [blame] | 1959 | VdbeBranchTaken(0, 2); |
dan | e5166e0 | 2019-03-19 11:56:39 +0000 | [diff] [blame] | 1960 | MemSetTypeFlag(pIn1, MEM_Int); |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 1961 | break; |
| 1962 | } |
| 1963 | |
drh | 13573c7 | 2010-01-12 17:04:07 +0000 | [diff] [blame] | 1964 | #ifndef SQLITE_OMIT_FLOATING_POINT |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1965 | /* Opcode: RealAffinity P1 * * * * |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 1966 | ** |
drh | 2133d82 | 2008-01-03 18:44:59 +0000 | [diff] [blame] | 1967 | ** If register P1 holds an integer convert it to a real value. |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 1968 | ** |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1969 | ** This opcode is used when extracting information from a column that |
| 1970 | ** has REAL affinity. Such column values may still be stored as |
| 1971 | ** integers, for space efficiency, but after extraction we want them |
| 1972 | ** to have only a real value. |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 1973 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1974 | case OP_RealAffinity: { /* in1 */ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 1975 | pIn1 = &aMem[pOp->p1]; |
drh | 169f077 | 2019-05-02 21:36:26 +0000 | [diff] [blame] | 1976 | if( pIn1->flags & (MEM_Int|MEM_IntReal) ){ |
drh | 3242c69 | 2019-05-04 01:29:13 +0000 | [diff] [blame] | 1977 | testcase( pIn1->flags & MEM_Int ); |
| 1978 | testcase( pIn1->flags & MEM_IntReal ); |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1979 | sqlite3VdbeMemRealify(pIn1); |
drh | efb5f9a | 2019-08-30 21:52:13 +0000 | [diff] [blame] | 1980 | REGISTER_TRACE(pOp->p1, pIn1); |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1981 | } |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 1982 | break; |
| 1983 | } |
drh | 13573c7 | 2010-01-12 17:04:07 +0000 | [diff] [blame] | 1984 | #endif |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 1985 | |
drh | 8df447f | 2005-11-01 15:48:24 +0000 | [diff] [blame] | 1986 | #ifndef SQLITE_OMIT_CAST |
drh | 4169e43 | 2014-08-25 20:11:52 +0000 | [diff] [blame] | 1987 | /* Opcode: Cast P1 P2 * * * |
mistachkin | a1dc42a | 2014-08-27 17:53:40 +0000 | [diff] [blame] | 1988 | ** Synopsis: affinity(r[P1]) |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 1989 | ** |
drh | 4169e43 | 2014-08-25 20:11:52 +0000 | [diff] [blame] | 1990 | ** Force the value in register P1 to be the type defined by P2. |
| 1991 | ** |
| 1992 | ** <ul> |
drh | bb6783b | 2017-04-29 18:02:49 +0000 | [diff] [blame] | 1993 | ** <li> P2=='A' → BLOB |
| 1994 | ** <li> P2=='B' → TEXT |
| 1995 | ** <li> P2=='C' → NUMERIC |
| 1996 | ** <li> P2=='D' → INTEGER |
| 1997 | ** <li> P2=='E' → REAL |
drh | 4169e43 | 2014-08-25 20:11:52 +0000 | [diff] [blame] | 1998 | ** </ul> |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 1999 | ** |
| 2000 | ** A NULL value is not changed by this routine. It remains NULL. |
| 2001 | */ |
drh | 4169e43 | 2014-08-25 20:11:52 +0000 | [diff] [blame] | 2002 | case OP_Cast: { /* in1 */ |
drh | 05883a3 | 2015-06-02 15:32:08 +0000 | [diff] [blame] | 2003 | assert( pOp->p2>=SQLITE_AFF_BLOB && pOp->p2<=SQLITE_AFF_REAL ); |
drh | 05bbb2e | 2014-08-25 22:37:19 +0000 | [diff] [blame] | 2004 | testcase( pOp->p2==SQLITE_AFF_TEXT ); |
drh | 05883a3 | 2015-06-02 15:32:08 +0000 | [diff] [blame] | 2005 | testcase( pOp->p2==SQLITE_AFF_BLOB ); |
drh | 05bbb2e | 2014-08-25 22:37:19 +0000 | [diff] [blame] | 2006 | testcase( pOp->p2==SQLITE_AFF_NUMERIC ); |
| 2007 | testcase( pOp->p2==SQLITE_AFF_INTEGER ); |
| 2008 | testcase( pOp->p2==SQLITE_AFF_REAL ); |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 2009 | pIn1 = &aMem[pOp->p1]; |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 2010 | memAboutToChange(p, pIn1); |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 2011 | rc = ExpandBlob(pIn1); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 2012 | if( rc ) goto abort_due_to_error; |
drh | 0af6ddd | 2019-12-23 03:37:46 +0000 | [diff] [blame] | 2013 | rc = sqlite3VdbeMemCast(pIn1, pOp->p2, encoding); |
| 2014 | if( rc ) goto abort_due_to_error; |
| 2015 | UPDATE_MAX_BLOBSIZE(pIn1); |
drh | 5d73272 | 2019-12-20 17:25:10 +0000 | [diff] [blame] | 2016 | REGISTER_TRACE(pOp->p1, pIn1); |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 2017 | break; |
| 2018 | } |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 2019 | #endif /* SQLITE_OMIT_CAST */ |
| 2020 | |
drh | 79752b6 | 2016-08-13 10:02:17 +0000 | [diff] [blame] | 2021 | /* Opcode: Eq P1 P2 P3 P4 P5 |
drh | 88e665f | 2016-08-27 01:41:53 +0000 | [diff] [blame] | 2022 | ** Synopsis: IF r[P3]==r[P1] |
drh | 79752b6 | 2016-08-13 10:02:17 +0000 | [diff] [blame] | 2023 | ** |
| 2024 | ** 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] | 2025 | ** jump to address P2. |
drh | 79752b6 | 2016-08-13 10:02:17 +0000 | [diff] [blame] | 2026 | ** |
| 2027 | ** The SQLITE_AFF_MASK portion of P5 must be an affinity character - |
| 2028 | ** SQLITE_AFF_TEXT, SQLITE_AFF_INTEGER, and so forth. An attempt is made |
| 2029 | ** to coerce both inputs according to this affinity before the |
| 2030 | ** comparison is made. If the SQLITE_AFF_MASK is 0x00, then numeric |
| 2031 | ** affinity is used. Note that the affinity conversions are stored |
| 2032 | ** back into the input registers P1 and P3. So this opcode can cause |
| 2033 | ** persistent changes to registers P1 and P3. |
| 2034 | ** |
| 2035 | ** Once any conversions have taken place, and neither value is NULL, |
| 2036 | ** the values are compared. If both values are blobs then memcmp() is |
| 2037 | ** used to determine the results of the comparison. If both values |
| 2038 | ** are text, then the appropriate collating function specified in |
| 2039 | ** P4 is used to do the comparison. If P4 is not specified then |
| 2040 | ** memcmp() is used to compare text string. If both values are |
| 2041 | ** numeric, then a numeric comparison is used. If the two values |
| 2042 | ** are of different types, then numbers are considered less than |
| 2043 | ** strings and strings are considered less than blobs. |
| 2044 | ** |
| 2045 | ** If SQLITE_NULLEQ is set in P5 then the result of comparison is always either |
| 2046 | ** true or false and is never NULL. If both operands are NULL then the result |
| 2047 | ** of comparison is true. If either operand is NULL then the result is false. |
| 2048 | ** If neither operand is NULL the result is the same as it would be if |
| 2049 | ** the SQLITE_NULLEQ flag were omitted from P5. |
| 2050 | ** |
drh | 1f97d26 | 2021-03-29 13:47:20 +0000 | [diff] [blame] | 2051 | ** This opcode saves the result of comparison for use by the new |
| 2052 | ** OP_Jump opcode. |
drh | 79752b6 | 2016-08-13 10:02:17 +0000 | [diff] [blame] | 2053 | */ |
| 2054 | /* Opcode: Ne P1 P2 P3 P4 P5 |
drh | 88e665f | 2016-08-27 01:41:53 +0000 | [diff] [blame] | 2055 | ** Synopsis: IF r[P3]!=r[P1] |
drh | 79752b6 | 2016-08-13 10:02:17 +0000 | [diff] [blame] | 2056 | ** |
| 2057 | ** This works just like the Eq opcode except that the jump is taken if |
| 2058 | ** the operands in registers P1 and P3 are not equal. See the Eq opcode for |
| 2059 | ** additional information. |
drh | 79752b6 | 2016-08-13 10:02:17 +0000 | [diff] [blame] | 2060 | */ |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 2061 | /* Opcode: Lt P1 P2 P3 P4 P5 |
drh | 88e665f | 2016-08-27 01:41:53 +0000 | [diff] [blame] | 2062 | ** Synopsis: IF r[P3]<r[P1] |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2063 | ** |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 2064 | ** 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] | 2065 | ** jump to address P2. |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 2066 | ** |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 2067 | ** 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] | 2068 | ** reg(P3) is NULL then the take the jump. If the SQLITE_JUMPIFNULL |
drh | 710c484 | 2010-08-30 01:17:20 +0000 | [diff] [blame] | 2069 | ** bit is clear then fall through if either operand is NULL. |
drh | 4f68623 | 2005-09-20 13:55:18 +0000 | [diff] [blame] | 2070 | ** |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 2071 | ** The SQLITE_AFF_MASK portion of P5 must be an affinity character - |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 2072 | ** SQLITE_AFF_TEXT, SQLITE_AFF_INTEGER, and so forth. An attempt is made |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 2073 | ** to coerce both inputs according to this affinity before the |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 2074 | ** comparison is made. If the SQLITE_AFF_MASK is 0x00, then numeric |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 2075 | ** affinity is used. Note that the affinity conversions are stored |
| 2076 | ** back into the input registers P1 and P3. So this opcode can cause |
| 2077 | ** persistent changes to registers P1 and P3. |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 2078 | ** |
| 2079 | ** Once any conversions have taken place, and neither value is NULL, |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 2080 | ** the values are compared. If both values are blobs then memcmp() is |
| 2081 | ** used to determine the results of the comparison. If both values |
| 2082 | ** are text, then the appropriate collating function specified in |
| 2083 | ** P4 is used to do the comparison. If P4 is not specified then |
| 2084 | ** memcmp() is used to compare text string. If both values are |
| 2085 | ** numeric, then a numeric comparison is used. If the two values |
| 2086 | ** are of different types, then numbers are considered less than |
| 2087 | ** strings and strings are considered less than blobs. |
drh | 1f97d26 | 2021-03-29 13:47:20 +0000 | [diff] [blame] | 2088 | ** |
| 2089 | ** This opcode saves the result of comparison for use by the new |
| 2090 | ** OP_Jump opcode. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2091 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2092 | /* Opcode: Le P1 P2 P3 P4 P5 |
drh | 88e665f | 2016-08-27 01:41:53 +0000 | [diff] [blame] | 2093 | ** Synopsis: IF r[P3]<=r[P1] |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2094 | ** |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 2095 | ** This works just like the Lt opcode except that the jump is taken if |
| 2096 | ** the content of register P3 is less than or equal to the content of |
| 2097 | ** register P1. See the Lt opcode for additional information. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2098 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2099 | /* Opcode: Gt P1 P2 P3 P4 P5 |
drh | 88e665f | 2016-08-27 01:41:53 +0000 | [diff] [blame] | 2100 | ** Synopsis: IF r[P3]>r[P1] |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2101 | ** |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 2102 | ** This works just like the Lt opcode except that the jump is taken if |
| 2103 | ** the content of register P3 is greater than the content of |
| 2104 | ** register P1. See the Lt opcode for additional information. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2105 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2106 | /* Opcode: Ge P1 P2 P3 P4 P5 |
drh | 88e665f | 2016-08-27 01:41:53 +0000 | [diff] [blame] | 2107 | ** Synopsis: IF r[P3]>=r[P1] |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2108 | ** |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 2109 | ** This works just like the Lt opcode except that the jump is taken if |
| 2110 | ** the content of register P3 is greater than or equal to the content of |
| 2111 | ** register P1. See the Lt opcode for additional information. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2112 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2113 | case OP_Eq: /* same as TK_EQ, jump, in1, in3 */ |
| 2114 | case OP_Ne: /* same as TK_NE, jump, in1, in3 */ |
| 2115 | case OP_Lt: /* same as TK_LT, jump, in1, in3 */ |
| 2116 | case OP_Le: /* same as TK_LE, jump, in1, in3 */ |
| 2117 | case OP_Gt: /* same as TK_GT, jump, in1, in3 */ |
| 2118 | case OP_Ge: { /* same as TK_GE, jump, in1, in3 */ |
drh | 4910a76 | 2016-09-03 01:46:15 +0000 | [diff] [blame] | 2119 | int res, res2; /* Result of the comparison of pIn1 against pIn3 */ |
drh | 6a2fe09 | 2009-09-23 02:29:36 +0000 | [diff] [blame] | 2120 | char affinity; /* Affinity to use for comparison */ |
dan | b7dca7d | 2010-03-05 16:32:12 +0000 | [diff] [blame] | 2121 | u16 flags1; /* Copy of initial value of pIn1->flags */ |
| 2122 | u16 flags3; /* Copy of initial value of pIn3->flags */ |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 2123 | |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 2124 | pIn1 = &aMem[pOp->p1]; |
| 2125 | pIn3 = &aMem[pOp->p3]; |
dan | b7dca7d | 2010-03-05 16:32:12 +0000 | [diff] [blame] | 2126 | flags1 = pIn1->flags; |
| 2127 | flags3 = pIn3->flags; |
drh | 8546497 | 2021-05-17 16:54:52 +0000 | [diff] [blame] | 2128 | if( (flags1 & flags3 & MEM_Int)!=0 ){ |
drh | 8ed1da1 | 2021-05-18 00:52:06 +0000 | [diff] [blame] | 2129 | assert( (pOp->p5 & SQLITE_AFF_MASK)!=SQLITE_AFF_TEXT || CORRUPT_DB ); |
drh | 8546497 | 2021-05-17 16:54:52 +0000 | [diff] [blame] | 2130 | /* Common case of comparison of two integers */ |
| 2131 | if( pIn3->u.i > pIn1->u.i ){ |
drh | 8546497 | 2021-05-17 16:54:52 +0000 | [diff] [blame] | 2132 | if( sqlite3aGTb[pOp->opcode] ){ |
| 2133 | VdbeBranchTaken(1, (pOp->p5 & SQLITE_NULLEQ)?2:3); |
| 2134 | goto jump_to_p2; |
| 2135 | } |
drh | a81a9f7 | 2022-04-04 11:38:49 +0000 | [diff] [blame] | 2136 | iCompare = +1; |
drh | 8546497 | 2021-05-17 16:54:52 +0000 | [diff] [blame] | 2137 | }else if( pIn3->u.i < pIn1->u.i ){ |
drh | 8546497 | 2021-05-17 16:54:52 +0000 | [diff] [blame] | 2138 | if( sqlite3aLTb[pOp->opcode] ){ |
| 2139 | VdbeBranchTaken(1, (pOp->p5 & SQLITE_NULLEQ)?2:3); |
| 2140 | goto jump_to_p2; |
| 2141 | } |
drh | a81a9f7 | 2022-04-04 11:38:49 +0000 | [diff] [blame] | 2142 | iCompare = -1; |
drh | 8546497 | 2021-05-17 16:54:52 +0000 | [diff] [blame] | 2143 | }else{ |
drh | 8546497 | 2021-05-17 16:54:52 +0000 | [diff] [blame] | 2144 | if( sqlite3aEQb[pOp->opcode] ){ |
| 2145 | VdbeBranchTaken(1, (pOp->p5 & SQLITE_NULLEQ)?2:3); |
| 2146 | goto jump_to_p2; |
| 2147 | } |
drh | a81a9f7 | 2022-04-04 11:38:49 +0000 | [diff] [blame] | 2148 | iCompare = 0; |
drh | 8546497 | 2021-05-17 16:54:52 +0000 | [diff] [blame] | 2149 | } |
| 2150 | VdbeBranchTaken(0, (pOp->p5 & SQLITE_NULLEQ)?2:3); |
| 2151 | break; |
| 2152 | } |
drh | c3f1d5f | 2011-05-30 23:42:16 +0000 | [diff] [blame] | 2153 | if( (flags1 | flags3)&MEM_Null ){ |
drh | 6a2fe09 | 2009-09-23 02:29:36 +0000 | [diff] [blame] | 2154 | /* One or both operands are NULL */ |
| 2155 | if( pOp->p5 & SQLITE_NULLEQ ){ |
| 2156 | /* If SQLITE_NULLEQ is set (which will only happen if the operator is |
| 2157 | ** OP_Eq or OP_Ne) then take the jump or not depending on whether |
| 2158 | ** or not both operands are null. |
| 2159 | */ |
drh | 053a128 | 2012-09-19 21:15:46 +0000 | [diff] [blame] | 2160 | assert( (flags1 & MEM_Cleared)==0 ); |
drh | a42325e | 2018-12-22 00:34:30 +0000 | [diff] [blame] | 2161 | assert( (pOp->p5 & SQLITE_JUMPIFNULL)==0 || CORRUPT_DB ); |
| 2162 | testcase( (pOp->p5 & SQLITE_JUMPIFNULL)!=0 ); |
drh | c3191d2 | 2016-10-18 16:36:15 +0000 | [diff] [blame] | 2163 | if( (flags1&flags3&MEM_Null)!=0 |
drh | 053a128 | 2012-09-19 21:15:46 +0000 | [diff] [blame] | 2164 | && (flags3&MEM_Cleared)==0 |
| 2165 | ){ |
drh | 4910a76 | 2016-09-03 01:46:15 +0000 | [diff] [blame] | 2166 | res = 0; /* Operands are equal */ |
drh | 053a128 | 2012-09-19 21:15:46 +0000 | [diff] [blame] | 2167 | }else{ |
dan | bdabe74 | 2019-03-18 16:51:24 +0000 | [diff] [blame] | 2168 | res = ((flags3 & MEM_Null) ? -1 : +1); /* Operands are not equal */ |
drh | 053a128 | 2012-09-19 21:15:46 +0000 | [diff] [blame] | 2169 | } |
drh | 6a2fe09 | 2009-09-23 02:29:36 +0000 | [diff] [blame] | 2170 | }else{ |
| 2171 | /* SQLITE_NULLEQ is clear and at least one operand is NULL, |
| 2172 | ** then the result is always NULL. |
| 2173 | ** The jump is taken if the SQLITE_JUMPIFNULL bit is set. |
| 2174 | */ |
drh | 4bc2045 | 2021-03-29 18:53:47 +0000 | [diff] [blame] | 2175 | VdbeBranchTaken(2,3); |
| 2176 | if( pOp->p5 & SQLITE_JUMPIFNULL ){ |
| 2177 | goto jump_to_p2; |
drh | 6a2fe09 | 2009-09-23 02:29:36 +0000 | [diff] [blame] | 2178 | } |
drh | a81a9f7 | 2022-04-04 11:38:49 +0000 | [diff] [blame] | 2179 | iCompare = 1; /* Operands are not equal */ |
drh | 6a2fe09 | 2009-09-23 02:29:36 +0000 | [diff] [blame] | 2180 | break; |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 2181 | } |
drh | 6a2fe09 | 2009-09-23 02:29:36 +0000 | [diff] [blame] | 2182 | }else{ |
drh | 8546497 | 2021-05-17 16:54:52 +0000 | [diff] [blame] | 2183 | /* Neither operand is NULL and we couldn't do the special high-speed |
| 2184 | ** integer comparison case. So do a general-case comparison. */ |
drh | 6a2fe09 | 2009-09-23 02:29:36 +0000 | [diff] [blame] | 2185 | affinity = pOp->p5 & SQLITE_AFF_MASK; |
drh | 24a0962 | 2014-09-18 16:28:59 +0000 | [diff] [blame] | 2186 | if( affinity>=SQLITE_AFF_NUMERIC ){ |
drh | 5fd0c12 | 2016-04-04 13:46:24 +0000 | [diff] [blame] | 2187 | if( (flags1 | flags3)&MEM_Str ){ |
drh | 169f077 | 2019-05-02 21:36:26 +0000 | [diff] [blame] | 2188 | if( (flags1 & (MEM_Int|MEM_IntReal|MEM_Real|MEM_Str))==MEM_Str ){ |
drh | 5fd0c12 | 2016-04-04 13:46:24 +0000 | [diff] [blame] | 2189 | applyNumericAffinity(pIn1,0); |
drh | 86d2de2 | 2020-06-14 13:40:13 +0000 | [diff] [blame] | 2190 | testcase( flags3==pIn3->flags ); |
drh | 4b37cd4 | 2016-06-25 11:43:47 +0000 | [diff] [blame] | 2191 | flags3 = pIn3->flags; |
drh | 5fd0c12 | 2016-04-04 13:46:24 +0000 | [diff] [blame] | 2192 | } |
drh | 169f077 | 2019-05-02 21:36:26 +0000 | [diff] [blame] | 2193 | if( (flags3 & (MEM_Int|MEM_IntReal|MEM_Real|MEM_Str))==MEM_Str ){ |
drh | 5fd0c12 | 2016-04-04 13:46:24 +0000 | [diff] [blame] | 2194 | applyNumericAffinity(pIn3,0); |
| 2195 | } |
drh | 24a0962 | 2014-09-18 16:28:59 +0000 | [diff] [blame] | 2196 | } |
| 2197 | }else if( affinity==SQLITE_AFF_TEXT ){ |
drh | 169f077 | 2019-05-02 21:36:26 +0000 | [diff] [blame] | 2198 | if( (flags1 & MEM_Str)==0 && (flags1&(MEM_Int|MEM_Real|MEM_IntReal))!=0 ){ |
drh | e7a3466 | 2014-09-19 22:44:20 +0000 | [diff] [blame] | 2199 | testcase( pIn1->flags & MEM_Int ); |
| 2200 | testcase( pIn1->flags & MEM_Real ); |
drh | 169f077 | 2019-05-02 21:36:26 +0000 | [diff] [blame] | 2201 | testcase( pIn1->flags & MEM_IntReal ); |
drh | 24a0962 | 2014-09-18 16:28:59 +0000 | [diff] [blame] | 2202 | sqlite3VdbeMemStringify(pIn1, encoding, 1); |
drh | bc8a6b3 | 2015-03-31 11:42:23 +0000 | [diff] [blame] | 2203 | testcase( (flags1&MEM_Dyn) != (pIn1->flags&MEM_Dyn) ); |
| 2204 | flags1 = (pIn1->flags & ~MEM_TypeMask) | (flags1 & MEM_TypeMask); |
drh | c4bb999 | 2022-01-14 21:34:49 +0000 | [diff] [blame] | 2205 | if( pIn1==pIn3 ) flags3 = flags1 | MEM_Str; |
drh | 24a0962 | 2014-09-18 16:28:59 +0000 | [diff] [blame] | 2206 | } |
drh | b44fec6 | 2019-12-24 21:42:22 +0000 | [diff] [blame] | 2207 | if( (flags3 & MEM_Str)==0 && (flags3&(MEM_Int|MEM_Real|MEM_IntReal))!=0 ){ |
drh | e7a3466 | 2014-09-19 22:44:20 +0000 | [diff] [blame] | 2208 | testcase( pIn3->flags & MEM_Int ); |
| 2209 | testcase( pIn3->flags & MEM_Real ); |
drh | 169f077 | 2019-05-02 21:36:26 +0000 | [diff] [blame] | 2210 | testcase( pIn3->flags & MEM_IntReal ); |
drh | 24a0962 | 2014-09-18 16:28:59 +0000 | [diff] [blame] | 2211 | sqlite3VdbeMemStringify(pIn3, encoding, 1); |
drh | bc8a6b3 | 2015-03-31 11:42:23 +0000 | [diff] [blame] | 2212 | testcase( (flags3&MEM_Dyn) != (pIn3->flags&MEM_Dyn) ); |
| 2213 | flags3 = (pIn3->flags & ~MEM_TypeMask) | (flags3 & MEM_TypeMask); |
drh | 24a0962 | 2014-09-18 16:28:59 +0000 | [diff] [blame] | 2214 | } |
drh | 6a2fe09 | 2009-09-23 02:29:36 +0000 | [diff] [blame] | 2215 | } |
drh | 6a2fe09 | 2009-09-23 02:29:36 +0000 | [diff] [blame] | 2216 | assert( pOp->p4type==P4_COLLSEQ || pOp->p4.pColl==0 ); |
drh | 4910a76 | 2016-09-03 01:46:15 +0000 | [diff] [blame] | 2217 | res = sqlite3MemCompare(pIn3, pIn1, pOp->p4.pColl); |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 2218 | } |
drh | 8546497 | 2021-05-17 16:54:52 +0000 | [diff] [blame] | 2219 | |
drh | 5859636 | 2017-08-03 00:29:23 +0000 | [diff] [blame] | 2220 | /* At this point, res is negative, zero, or positive if reg[P1] is |
| 2221 | ** less than, equal to, or greater than reg[P3], respectively. Compute |
| 2222 | ** the answer to this operator in res2, depending on what the comparison |
| 2223 | ** operator actually is. The next block of code depends on the fact |
| 2224 | ** that the 6 comparison operators are consecutive integers in this |
| 2225 | ** order: NE, EQ, GT, LE, LT, GE */ |
| 2226 | assert( OP_Eq==OP_Ne+1 ); assert( OP_Gt==OP_Ne+2 ); assert( OP_Le==OP_Ne+3 ); |
| 2227 | assert( OP_Lt==OP_Ne+4 ); assert( OP_Ge==OP_Ne+5 ); |
drh | 1af3fd5 | 2021-03-28 23:37:56 +0000 | [diff] [blame] | 2228 | if( res<0 ){ |
| 2229 | res2 = sqlite3aLTb[pOp->opcode]; |
drh | 5859636 | 2017-08-03 00:29:23 +0000 | [diff] [blame] | 2230 | }else if( res==0 ){ |
drh | 1af3fd5 | 2021-03-28 23:37:56 +0000 | [diff] [blame] | 2231 | res2 = sqlite3aEQb[pOp->opcode]; |
drh | 5859636 | 2017-08-03 00:29:23 +0000 | [diff] [blame] | 2232 | }else{ |
drh | 1af3fd5 | 2021-03-28 23:37:56 +0000 | [diff] [blame] | 2233 | res2 = sqlite3aGTb[pOp->opcode]; |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 2234 | } |
drh | 1f97d26 | 2021-03-29 13:47:20 +0000 | [diff] [blame] | 2235 | iCompare = res; |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 2236 | |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 2237 | /* Undo any changes made by applyAffinity() to the input registers. */ |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 2238 | assert( (pIn3->flags & MEM_Dyn) == (flags3 & MEM_Dyn) ); |
| 2239 | pIn3->flags = flags3; |
drh | b44fec6 | 2019-12-24 21:42:22 +0000 | [diff] [blame] | 2240 | assert( (pIn1->flags & MEM_Dyn) == (flags1 & MEM_Dyn) ); |
| 2241 | pIn1->flags = flags1; |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 2242 | |
drh | 4bc2045 | 2021-03-29 18:53:47 +0000 | [diff] [blame] | 2243 | VdbeBranchTaken(res2!=0, (pOp->p5 & SQLITE_NULLEQ)?2:3); |
| 2244 | if( res2 ){ |
| 2245 | goto jump_to_p2; |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 2246 | } |
| 2247 | break; |
| 2248 | } |
drh | c9b84a1 | 2002-06-20 11:36:48 +0000 | [diff] [blame] | 2249 | |
drh | 4bc2045 | 2021-03-29 18:53:47 +0000 | [diff] [blame] | 2250 | /* Opcode: ElseEq * P2 * * * |
drh | 79752b6 | 2016-08-13 10:02:17 +0000 | [diff] [blame] | 2251 | ** |
drh | 13d7950 | 2019-12-23 02:18:49 +0000 | [diff] [blame] | 2252 | ** This opcode must follow an OP_Lt or OP_Gt comparison operator. There |
| 2253 | ** can be zero or more OP_ReleaseReg opcodes intervening, but no other |
| 2254 | ** opcodes are allowed to occur between this instruction and the previous |
drh | 4bc2045 | 2021-03-29 18:53:47 +0000 | [diff] [blame] | 2255 | ** OP_Lt or OP_Gt. |
drh | 13d7950 | 2019-12-23 02:18:49 +0000 | [diff] [blame] | 2256 | ** |
| 2257 | ** 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] | 2258 | ** prior OP_Lt or OP_Gt would have been true, then jump to P2. |
| 2259 | ** If the result of an OP_Eq comparison on the two previous |
| 2260 | ** operands would have been false or NULL, then fall through. |
drh | 79752b6 | 2016-08-13 10:02:17 +0000 | [diff] [blame] | 2261 | */ |
drh | 4bc2045 | 2021-03-29 18:53:47 +0000 | [diff] [blame] | 2262 | case OP_ElseEq: { /* same as TK_ESCAPE, jump */ |
drh | 13d7950 | 2019-12-23 02:18:49 +0000 | [diff] [blame] | 2263 | |
| 2264 | #ifdef SQLITE_DEBUG |
| 2265 | /* Verify the preconditions of this opcode - that it follows an OP_Lt or |
drh | 4bc2045 | 2021-03-29 18:53:47 +0000 | [diff] [blame] | 2266 | ** OP_Gt with zero or more intervening OP_ReleaseReg opcodes */ |
drh | 13d7950 | 2019-12-23 02:18:49 +0000 | [diff] [blame] | 2267 | int iAddr; |
| 2268 | for(iAddr = (int)(pOp - aOp) - 1; ALWAYS(iAddr>=0); iAddr--){ |
| 2269 | if( aOp[iAddr].opcode==OP_ReleaseReg ) continue; |
| 2270 | assert( aOp[iAddr].opcode==OP_Lt || aOp[iAddr].opcode==OP_Gt ); |
drh | 13d7950 | 2019-12-23 02:18:49 +0000 | [diff] [blame] | 2271 | break; |
| 2272 | } |
| 2273 | #endif /* SQLITE_DEBUG */ |
drh | 4bc2045 | 2021-03-29 18:53:47 +0000 | [diff] [blame] | 2274 | VdbeBranchTaken(iCompare==0, 2); |
| 2275 | if( iCompare==0 ) goto jump_to_p2; |
drh | 79752b6 | 2016-08-13 10:02:17 +0000 | [diff] [blame] | 2276 | break; |
| 2277 | } |
| 2278 | |
| 2279 | |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 2280 | /* Opcode: Permutation * * * P4 * |
| 2281 | ** |
drh | b7dab70 | 2017-01-26 18:00:00 +0000 | [diff] [blame] | 2282 | ** Set the permutation used by the OP_Compare operator in the next |
| 2283 | ** instruction. The permutation is stored in the P4 operand. |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 2284 | ** |
drh | a81a9f7 | 2022-04-04 11:38:49 +0000 | [diff] [blame] | 2285 | ** The permutation is only valid for the next opcode which must be |
| 2286 | ** an OP_Compare that has the OPFLAG_PERMUTE bit set in P5. |
drh | b170202 | 2016-01-30 00:45:18 +0000 | [diff] [blame] | 2287 | ** |
| 2288 | ** The first integer in the P4 integer array is the length of the array |
| 2289 | ** and does not become part of the permutation. |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 2290 | */ |
| 2291 | case OP_Permutation: { |
| 2292 | assert( pOp->p4type==P4_INTARRAY ); |
| 2293 | assert( pOp->p4.ai ); |
drh | b7dab70 | 2017-01-26 18:00:00 +0000 | [diff] [blame] | 2294 | assert( pOp[1].opcode==OP_Compare ); |
| 2295 | assert( pOp[1].p5 & OPFLAG_PERMUTE ); |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 2296 | break; |
| 2297 | } |
| 2298 | |
drh | 953f761 | 2012-12-07 22:18:54 +0000 | [diff] [blame] | 2299 | /* Opcode: Compare P1 P2 P3 P4 P5 |
drh | 079a307 | 2014-03-19 14:10:55 +0000 | [diff] [blame] | 2300 | ** Synopsis: r[P1@P3] <-> r[P2@P3] |
drh | 16ee60f | 2008-06-20 18:13:25 +0000 | [diff] [blame] | 2301 | ** |
drh | 710c484 | 2010-08-30 01:17:20 +0000 | [diff] [blame] | 2302 | ** Compare two vectors of registers in reg(P1)..reg(P1+P3-1) (call this |
| 2303 | ** 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] | 2304 | ** the comparison for use by the next OP_Jump instruct. |
| 2305 | ** |
drh | 0ca10df | 2012-12-08 13:26:23 +0000 | [diff] [blame] | 2306 | ** If P5 has the OPFLAG_PERMUTE bit set, then the order of comparison is |
| 2307 | ** determined by the most recent OP_Permutation operator. If the |
| 2308 | ** OPFLAG_PERMUTE bit is clear, then register are compared in sequential |
| 2309 | ** order. |
| 2310 | ** |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 2311 | ** P4 is a KeyInfo structure that defines collating sequences and sort |
| 2312 | ** orders for the comparison. The permutation applies to registers |
| 2313 | ** only. The KeyInfo elements are used sequentially. |
| 2314 | ** |
| 2315 | ** The comparison is a sort comparison, so NULLs compare equal, |
| 2316 | ** NULLs are less than numbers, numbers are less than strings, |
drh | 16ee60f | 2008-06-20 18:13:25 +0000 | [diff] [blame] | 2317 | ** and strings are less than blobs. |
drh | a81a9f7 | 2022-04-04 11:38:49 +0000 | [diff] [blame] | 2318 | ** |
| 2319 | ** This opcode must be immediately followed by an OP_Jump opcode. |
drh | 16ee60f | 2008-06-20 18:13:25 +0000 | [diff] [blame] | 2320 | */ |
| 2321 | case OP_Compare: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2322 | int n; |
| 2323 | int i; |
| 2324 | int p1; |
| 2325 | int p2; |
| 2326 | const KeyInfo *pKeyInfo; |
drh | abc3815 | 2020-07-22 13:38:04 +0000 | [diff] [blame] | 2327 | u32 idx; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2328 | CollSeq *pColl; /* Collating sequence to use on this term */ |
| 2329 | int bRev; /* True for DESCENDING sort order */ |
drh | abc3815 | 2020-07-22 13:38:04 +0000 | [diff] [blame] | 2330 | u32 *aPermute; /* The permutation */ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2331 | |
drh | b7dab70 | 2017-01-26 18:00:00 +0000 | [diff] [blame] | 2332 | if( (pOp->p5 & OPFLAG_PERMUTE)==0 ){ |
| 2333 | aPermute = 0; |
| 2334 | }else{ |
| 2335 | assert( pOp>aOp ); |
| 2336 | assert( pOp[-1].opcode==OP_Permutation ); |
| 2337 | assert( pOp[-1].p4type==P4_INTARRAY ); |
| 2338 | aPermute = pOp[-1].p4.ai + 1; |
| 2339 | assert( aPermute!=0 ); |
| 2340 | } |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2341 | n = pOp->p3; |
| 2342 | pKeyInfo = pOp->p4.pKeyInfo; |
drh | 16ee60f | 2008-06-20 18:13:25 +0000 | [diff] [blame] | 2343 | assert( n>0 ); |
drh | 93a960a | 2008-07-10 00:32:42 +0000 | [diff] [blame] | 2344 | assert( pKeyInfo!=0 ); |
drh | 16ee60f | 2008-06-20 18:13:25 +0000 | [diff] [blame] | 2345 | p1 = pOp->p1; |
drh | 16ee60f | 2008-06-20 18:13:25 +0000 | [diff] [blame] | 2346 | p2 = pOp->p2; |
drh | d879e3e | 2017-02-13 13:35:55 +0000 | [diff] [blame] | 2347 | #ifdef SQLITE_DEBUG |
drh | 6a2fe09 | 2009-09-23 02:29:36 +0000 | [diff] [blame] | 2348 | if( aPermute ){ |
| 2349 | int k, mx = 0; |
mistachkin | cec5f1d | 2020-08-04 16:11:37 +0000 | [diff] [blame] | 2350 | 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] | 2351 | assert( p1>0 && p1+mx<=(p->nMem+1 - p->nCursor)+1 ); |
| 2352 | assert( p2>0 && p2+mx<=(p->nMem+1 - p->nCursor)+1 ); |
drh | 6a2fe09 | 2009-09-23 02:29:36 +0000 | [diff] [blame] | 2353 | }else{ |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 2354 | assert( p1>0 && p1+n<=(p->nMem+1 - p->nCursor)+1 ); |
| 2355 | assert( p2>0 && p2+n<=(p->nMem+1 - p->nCursor)+1 ); |
drh | 6a2fe09 | 2009-09-23 02:29:36 +0000 | [diff] [blame] | 2356 | } |
| 2357 | #endif /* SQLITE_DEBUG */ |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 2358 | for(i=0; i<n; i++){ |
drh | 8deae5a | 2020-07-29 12:23:20 +0000 | [diff] [blame] | 2359 | idx = aPermute ? aPermute[i] : (u32)i; |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 2360 | assert( memIsValid(&aMem[p1+idx]) ); |
| 2361 | assert( memIsValid(&aMem[p2+idx]) ); |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 2362 | REGISTER_TRACE(p1+idx, &aMem[p1+idx]); |
| 2363 | REGISTER_TRACE(p2+idx, &aMem[p2+idx]); |
drh | a485ad1 | 2017-08-02 22:43:14 +0000 | [diff] [blame] | 2364 | assert( i<pKeyInfo->nKeyField ); |
drh | 93a960a | 2008-07-10 00:32:42 +0000 | [diff] [blame] | 2365 | pColl = pKeyInfo->aColl[i]; |
dan | 6e11892 | 2019-08-12 16:36:38 +0000 | [diff] [blame] | 2366 | bRev = (pKeyInfo->aSortFlags[i] & KEYINFO_ORDER_DESC); |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 2367 | iCompare = sqlite3MemCompare(&aMem[p1+idx], &aMem[p2+idx], pColl); |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 2368 | if( iCompare ){ |
dan | 6e11892 | 2019-08-12 16:36:38 +0000 | [diff] [blame] | 2369 | if( (pKeyInfo->aSortFlags[i] & KEYINFO_ORDER_BIGNULL) |
| 2370 | && ((aMem[p1+idx].flags & MEM_Null) || (aMem[p2+idx].flags & MEM_Null)) |
| 2371 | ){ |
| 2372 | iCompare = -iCompare; |
| 2373 | } |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 2374 | if( bRev ) iCompare = -iCompare; |
| 2375 | break; |
| 2376 | } |
drh | 16ee60f | 2008-06-20 18:13:25 +0000 | [diff] [blame] | 2377 | } |
drh | a81a9f7 | 2022-04-04 11:38:49 +0000 | [diff] [blame] | 2378 | assert( pOp[1].opcode==OP_Jump ); |
drh | 16ee60f | 2008-06-20 18:13:25 +0000 | [diff] [blame] | 2379 | break; |
| 2380 | } |
| 2381 | |
| 2382 | /* Opcode: Jump P1 P2 P3 * * |
| 2383 | ** |
| 2384 | ** Jump to the instruction at address P1, P2, or P3 depending on whether |
| 2385 | ** in the most recent OP_Compare instruction the P1 vector was less than |
| 2386 | ** equal to, or greater than the P2 vector, respectively. |
drh | a81a9f7 | 2022-04-04 11:38:49 +0000 | [diff] [blame] | 2387 | ** |
| 2388 | ** This opcode must immediately follow an OP_Compare opcode. |
drh | 16ee60f | 2008-06-20 18:13:25 +0000 | [diff] [blame] | 2389 | */ |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 2390 | case OP_Jump: { /* jump */ |
drh | a81a9f7 | 2022-04-04 11:38:49 +0000 | [diff] [blame] | 2391 | assert( pOp>aOp && pOp[-1].opcode==OP_Compare ); |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 2392 | if( iCompare<0 ){ |
drh | 7083a48 | 2018-07-10 16:04:04 +0000 | [diff] [blame] | 2393 | VdbeBranchTaken(0,4); pOp = &aOp[pOp->p1 - 1]; |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 2394 | }else if( iCompare==0 ){ |
drh | 7083a48 | 2018-07-10 16:04:04 +0000 | [diff] [blame] | 2395 | VdbeBranchTaken(1,4); pOp = &aOp[pOp->p2 - 1]; |
drh | 16ee60f | 2008-06-20 18:13:25 +0000 | [diff] [blame] | 2396 | }else{ |
drh | 7083a48 | 2018-07-10 16:04:04 +0000 | [diff] [blame] | 2397 | VdbeBranchTaken(2,4); pOp = &aOp[pOp->p3 - 1]; |
drh | 16ee60f | 2008-06-20 18:13:25 +0000 | [diff] [blame] | 2398 | } |
| 2399 | break; |
| 2400 | } |
| 2401 | |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 2402 | /* Opcode: And P1 P2 P3 * * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 2403 | ** Synopsis: r[P3]=(r[P1] && r[P2]) |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2404 | ** |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 2405 | ** Take the logical AND of the values in registers P1 and P2 and |
| 2406 | ** write the result into register P3. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2407 | ** |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 2408 | ** If either P1 or P2 is 0 (false) then the result is 0 even if |
| 2409 | ** the other input is NULL. A NULL and true or two NULLs give |
| 2410 | ** a NULL output. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2411 | */ |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 2412 | /* Opcode: Or P1 P2 P3 * * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 2413 | ** Synopsis: r[P3]=(r[P1] || r[P2]) |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 2414 | ** |
| 2415 | ** Take the logical OR of the values in register P1 and P2 and |
| 2416 | ** store the answer in register P3. |
| 2417 | ** |
| 2418 | ** If either P1 or P2 is nonzero (true) then the result is 1 (true) |
| 2419 | ** even if the other input is NULL. A NULL and false or two NULLs |
| 2420 | ** give a NULL output. |
| 2421 | */ |
| 2422 | case OP_And: /* same as TK_AND, in1, in2, out3 */ |
| 2423 | case OP_Or: { /* same as TK_OR, in1, in2, out3 */ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2424 | int v1; /* Left operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */ |
| 2425 | int v2; /* Right operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */ |
drh | bb11351 | 2002-05-27 01:04:51 +0000 | [diff] [blame] | 2426 | |
drh | 1fcfa72 | 2018-02-26 15:27:31 +0000 | [diff] [blame] | 2427 | v1 = sqlite3VdbeBooleanValue(&aMem[pOp->p1], 2); |
| 2428 | v2 = sqlite3VdbeBooleanValue(&aMem[pOp->p2], 2); |
drh | bb11351 | 2002-05-27 01:04:51 +0000 | [diff] [blame] | 2429 | if( pOp->opcode==OP_And ){ |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 2430 | 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] | 2431 | v1 = and_logic[v1*3+v2]; |
| 2432 | }else{ |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 2433 | 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] | 2434 | v1 = or_logic[v1*3+v2]; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2435 | } |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 2436 | pOut = &aMem[pOp->p3]; |
drh | bb11351 | 2002-05-27 01:04:51 +0000 | [diff] [blame] | 2437 | if( v1==2 ){ |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 2438 | MemSetTypeFlag(pOut, MEM_Null); |
drh | bb11351 | 2002-05-27 01:04:51 +0000 | [diff] [blame] | 2439 | }else{ |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 2440 | pOut->u.i = v1; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 2441 | MemSetTypeFlag(pOut, MEM_Int); |
drh | bb11351 | 2002-05-27 01:04:51 +0000 | [diff] [blame] | 2442 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2443 | break; |
| 2444 | } |
| 2445 | |
drh | 8abed7b | 2018-02-26 18:49:05 +0000 | [diff] [blame] | 2446 | /* Opcode: IsTrue P1 P2 P3 P4 * |
| 2447 | ** Synopsis: r[P2] = coalesce(r[P1]==TRUE,P3) ^ P4 |
| 2448 | ** |
| 2449 | ** This opcode implements the IS TRUE, IS FALSE, IS NOT TRUE, and |
| 2450 | ** IS NOT FALSE operators. |
| 2451 | ** |
drh | 96acafb | 2018-02-27 14:49:25 +0000 | [diff] [blame] | 2452 | ** Interpret the value in register P1 as a boolean value. Store that |
drh | 8abed7b | 2018-02-26 18:49:05 +0000 | [diff] [blame] | 2453 | ** boolean (a 0 or 1) in register P2. Or if the value in register P1 is |
| 2454 | ** NULL, then the P3 is stored in register P2. Invert the answer if P4 |
| 2455 | ** is 1. |
| 2456 | ** |
| 2457 | ** The logic is summarized like this: |
| 2458 | ** |
| 2459 | ** <ul> |
drh | 96acafb | 2018-02-27 14:49:25 +0000 | [diff] [blame] | 2460 | ** <li> If P3==0 and P4==0 then r[P2] := r[P1] IS TRUE |
| 2461 | ** <li> If P3==1 and P4==1 then r[P2] := r[P1] IS FALSE |
| 2462 | ** <li> If P3==0 and P4==1 then r[P2] := r[P1] IS NOT TRUE |
| 2463 | ** <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] | 2464 | ** </ul> |
| 2465 | */ |
| 2466 | case OP_IsTrue: { /* in1, out2 */ |
| 2467 | assert( pOp->p4type==P4_INT32 ); |
| 2468 | assert( pOp->p4.i==0 || pOp->p4.i==1 ); |
drh | 96acafb | 2018-02-27 14:49:25 +0000 | [diff] [blame] | 2469 | assert( pOp->p3==0 || pOp->p3==1 ); |
drh | 8abed7b | 2018-02-26 18:49:05 +0000 | [diff] [blame] | 2470 | sqlite3VdbeMemSetInt64(&aMem[pOp->p2], |
| 2471 | sqlite3VdbeBooleanValue(&aMem[pOp->p1], pOp->p3) ^ pOp->p4.i); |
| 2472 | break; |
| 2473 | } |
| 2474 | |
drh | e99fa2a | 2008-12-15 15:27:51 +0000 | [diff] [blame] | 2475 | /* Opcode: Not P1 P2 * * * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 2476 | ** Synopsis: r[P2]= !r[P1] |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2477 | ** |
drh | e99fa2a | 2008-12-15 15:27:51 +0000 | [diff] [blame] | 2478 | ** Interpret the value in register P1 as a boolean value. Store the |
| 2479 | ** boolean complement in register P2. If the value in register P1 is |
| 2480 | ** NULL, then a NULL is stored in P2. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2481 | */ |
drh | 93952eb | 2009-11-13 19:43:43 +0000 | [diff] [blame] | 2482 | case OP_Not: { /* same as TK_NOT, in1, out2 */ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 2483 | pIn1 = &aMem[pOp->p1]; |
| 2484 | pOut = &aMem[pOp->p2]; |
drh | 0725cab | 2014-09-17 14:52:46 +0000 | [diff] [blame] | 2485 | if( (pIn1->flags & MEM_Null)==0 ){ |
drh | bc8f68a | 2018-02-26 15:31:39 +0000 | [diff] [blame] | 2486 | sqlite3VdbeMemSetInt64(pOut, !sqlite3VdbeBooleanValue(pIn1,0)); |
drh | 007c843 | 2018-02-26 03:20:18 +0000 | [diff] [blame] | 2487 | }else{ |
| 2488 | sqlite3VdbeMemSetNull(pOut); |
drh | e99fa2a | 2008-12-15 15:27:51 +0000 | [diff] [blame] | 2489 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2490 | break; |
| 2491 | } |
| 2492 | |
drh | e99fa2a | 2008-12-15 15:27:51 +0000 | [diff] [blame] | 2493 | /* Opcode: BitNot P1 P2 * * * |
drh | cd9e014 | 2018-06-12 13:16:57 +0000 | [diff] [blame] | 2494 | ** Synopsis: r[P2]= ~r[P1] |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 2495 | ** |
drh | e99fa2a | 2008-12-15 15:27:51 +0000 | [diff] [blame] | 2496 | ** Interpret the content of register P1 as an integer. Store the |
| 2497 | ** ones-complement of the P1 value into register P2. If P1 holds |
| 2498 | ** a NULL then store a NULL in P2. |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 2499 | */ |
drh | 93952eb | 2009-11-13 19:43:43 +0000 | [diff] [blame] | 2500 | case OP_BitNot: { /* same as TK_BITNOT, in1, out2 */ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 2501 | pIn1 = &aMem[pOp->p1]; |
| 2502 | pOut = &aMem[pOp->p2]; |
drh | 0725cab | 2014-09-17 14:52:46 +0000 | [diff] [blame] | 2503 | sqlite3VdbeMemSetNull(pOut); |
| 2504 | if( (pIn1->flags & MEM_Null)==0 ){ |
| 2505 | pOut->flags = MEM_Int; |
| 2506 | pOut->u.i = ~sqlite3VdbeIntValue(pIn1); |
drh | e99fa2a | 2008-12-15 15:27:51 +0000 | [diff] [blame] | 2507 | } |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 2508 | break; |
| 2509 | } |
| 2510 | |
drh | 48f2d3b | 2011-09-16 01:34:43 +0000 | [diff] [blame] | 2511 | /* Opcode: Once P1 P2 * * * |
| 2512 | ** |
drh | ab087d4 | 2017-03-24 17:59:56 +0000 | [diff] [blame] | 2513 | ** Fall through to the next instruction the first time this opcode is |
| 2514 | ** encountered on each invocation of the byte-code program. Jump to P2 |
| 2515 | ** on the second and all subsequent encounters during the same invocation. |
| 2516 | ** |
| 2517 | ** Top-level programs determine first invocation by comparing the P1 |
| 2518 | ** operand against the P1 operand on the OP_Init opcode at the beginning |
| 2519 | ** of the program. If the P1 values differ, then fall through and make |
| 2520 | ** the P1 of this opcode equal to the P1 of OP_Init. If P1 values are |
| 2521 | ** the same then take the jump. |
| 2522 | ** |
| 2523 | ** For subprograms, there is a bitmask in the VdbeFrame that determines |
| 2524 | ** whether or not the jump should be taken. The bitmask is necessary |
| 2525 | ** because the self-altering code trick does not work for recursive |
| 2526 | ** triggers. |
drh | 48f2d3b | 2011-09-16 01:34:43 +0000 | [diff] [blame] | 2527 | */ |
dan | 1d8cb21 | 2011-12-09 13:24:16 +0000 | [diff] [blame] | 2528 | case OP_Once: { /* jump */ |
drh | ab087d4 | 2017-03-24 17:59:56 +0000 | [diff] [blame] | 2529 | u32 iAddr; /* Address of this instruction */ |
drh | 9e5eb9c | 2016-09-18 16:08:10 +0000 | [diff] [blame] | 2530 | assert( p->aOp[0].opcode==OP_Init ); |
drh | ab087d4 | 2017-03-24 17:59:56 +0000 | [diff] [blame] | 2531 | if( p->pFrame ){ |
| 2532 | iAddr = (int)(pOp - p->aOp); |
| 2533 | if( (p->pFrame->aOnce[iAddr/8] & (1<<(iAddr & 7)))!=0 ){ |
| 2534 | VdbeBranchTaken(1, 2); |
drh | ab087d4 | 2017-03-24 17:59:56 +0000 | [diff] [blame] | 2535 | goto jump_to_p2; |
| 2536 | } |
drh | 18333ef | 2017-03-24 18:38:41 +0000 | [diff] [blame] | 2537 | p->pFrame->aOnce[iAddr/8] |= 1<<(iAddr & 7); |
dan | 1d8cb21 | 2011-12-09 13:24:16 +0000 | [diff] [blame] | 2538 | }else{ |
drh | ab087d4 | 2017-03-24 17:59:56 +0000 | [diff] [blame] | 2539 | if( p->aOp[0].p1==pOp->p1 ){ |
| 2540 | VdbeBranchTaken(1, 2); |
| 2541 | goto jump_to_p2; |
| 2542 | } |
dan | 1d8cb21 | 2011-12-09 13:24:16 +0000 | [diff] [blame] | 2543 | } |
drh | ab087d4 | 2017-03-24 17:59:56 +0000 | [diff] [blame] | 2544 | VdbeBranchTaken(0, 2); |
| 2545 | pOp->p1 = p->aOp[0].p1; |
dan | 1d8cb21 | 2011-12-09 13:24:16 +0000 | [diff] [blame] | 2546 | break; |
| 2547 | } |
| 2548 | |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 2549 | /* Opcode: If P1 P2 P3 * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2550 | ** |
drh | ef8662b | 2011-06-20 21:47:58 +0000 | [diff] [blame] | 2551 | ** Jump to P2 if the value in register P1 is true. The value |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 2552 | ** is considered true if it is numeric and non-zero. If the value |
drh | e21a6e1 | 2014-08-01 18:00:24 +0000 | [diff] [blame] | 2553 | ** 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] | 2554 | */ |
drh | 1fcfa72 | 2018-02-26 15:27:31 +0000 | [diff] [blame] | 2555 | case OP_If: { /* jump, in1 */ |
| 2556 | int c; |
| 2557 | c = sqlite3VdbeBooleanValue(&aMem[pOp->p1], pOp->p3); |
| 2558 | VdbeBranchTaken(c!=0, 2); |
| 2559 | if( c ) goto jump_to_p2; |
| 2560 | break; |
| 2561 | } |
| 2562 | |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 2563 | /* Opcode: IfNot P1 P2 P3 * * |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 2564 | ** |
drh | ef8662b | 2011-06-20 21:47:58 +0000 | [diff] [blame] | 2565 | ** Jump to P2 if the value in register P1 is False. The value |
drh | b8475df | 2011-12-09 16:21:19 +0000 | [diff] [blame] | 2566 | ** 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] | 2567 | ** 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] | 2568 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2569 | case OP_IfNot: { /* jump, in1 */ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2570 | int c; |
drh | 1fcfa72 | 2018-02-26 15:27:31 +0000 | [diff] [blame] | 2571 | c = !sqlite3VdbeBooleanValue(&aMem[pOp->p1], !pOp->p3); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 2572 | VdbeBranchTaken(c!=0, 2); |
drh | 1fcfa72 | 2018-02-26 15:27:31 +0000 | [diff] [blame] | 2573 | if( c ) goto jump_to_p2; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2574 | break; |
| 2575 | } |
| 2576 | |
drh | 830ecf9 | 2009-06-18 00:41:55 +0000 | [diff] [blame] | 2577 | /* Opcode: IsNull P1 P2 * * * |
drh | 72e26de | 2016-08-24 21:24:04 +0000 | [diff] [blame] | 2578 | ** Synopsis: if r[P1]==NULL goto P2 |
drh | 477df4b | 2008-01-05 18:48:24 +0000 | [diff] [blame] | 2579 | ** |
drh | 830ecf9 | 2009-06-18 00:41:55 +0000 | [diff] [blame] | 2580 | ** Jump to P2 if the value in register P1 is NULL. |
drh | 477df4b | 2008-01-05 18:48:24 +0000 | [diff] [blame] | 2581 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2582 | case OP_IsNull: { /* same as TK_ISNULL, jump, in1 */ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 2583 | pIn1 = &aMem[pOp->p1]; |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 2584 | VdbeBranchTaken( (pIn1->flags & MEM_Null)!=0, 2); |
drh | 830ecf9 | 2009-06-18 00:41:55 +0000 | [diff] [blame] | 2585 | if( (pIn1->flags & MEM_Null)!=0 ){ |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 2586 | goto jump_to_p2; |
drh | 830ecf9 | 2009-06-18 00:41:55 +0000 | [diff] [blame] | 2587 | } |
drh | 477df4b | 2008-01-05 18:48:24 +0000 | [diff] [blame] | 2588 | break; |
| 2589 | } |
| 2590 | |
drh | 49d77ee | 2022-10-10 18:25:05 +0000 | [diff] [blame] | 2591 | /* Opcode: IsType P1 P2 P3 P4 P5 |
| 2592 | ** Synopsis: if typeof(P1.P3) in P5 goto P2 |
drh | 71c770f | 2021-08-19 16:29:33 +0000 | [diff] [blame] | 2593 | ** |
drh | 49d77ee | 2022-10-10 18:25:05 +0000 | [diff] [blame] | 2594 | ** Jump to P2 if the type of a column in a btree is one of the types specified |
| 2595 | ** by the P5 bitmask. |
| 2596 | ** |
| 2597 | ** P1 is normally a cursor on a btree for which the row decode cache is |
| 2598 | ** valid through at least column P3. In other words, there should have been |
| 2599 | ** a prior OP_Column for column P3 or greater. If the cursor is not valid, |
| 2600 | ** then this opcode might give spurious results. |
| 2601 | ** The the btree row has fewer than P3 columns, then use P4 as the |
| 2602 | ** datatype. |
| 2603 | ** |
| 2604 | ** If P1 is -1, then P3 is a register number and the datatype is taken |
| 2605 | ** from the value in that register. |
| 2606 | ** |
| 2607 | ** P5 is a bitmask of data types. SQLITE_INTEGER is the least significant |
| 2608 | ** (0x01) bit. SQLITE_FLOAT is the 0x02 bit. SQLITE_TEXT is 0x04. |
| 2609 | ** SQLITE_BLOB is 0x08. SQLITE_NULL is 0x10. |
| 2610 | ** |
| 2611 | ** Take the jump to address P2 if and only if the datatype of the |
| 2612 | ** value determined by P1 and P3 corresponds to one of the bits in the |
| 2613 | ** P5 bitmask. |
| 2614 | ** |
drh | 71c770f | 2021-08-19 16:29:33 +0000 | [diff] [blame] | 2615 | */ |
drh | c9ef12f | 2022-10-10 21:21:04 +0000 | [diff] [blame] | 2616 | case OP_IsType: { /* jump */ |
drh | 49d77ee | 2022-10-10 18:25:05 +0000 | [diff] [blame] | 2617 | VdbeCursor *pC; |
| 2618 | u16 typeMask; |
| 2619 | u32 serialType; |
| 2620 | |
| 2621 | assert( pOp->p1>=(-1) && pOp->p1<p->nCursor ); |
| 2622 | assert( pOp->p1>=0 || (pOp->p3>=0 && pOp->p3<=(p->nMem+1 - p->nCursor)) ); |
| 2623 | if( pOp->p1>=0 ){ |
| 2624 | pC = p->apCsr[pOp->p1]; |
| 2625 | assert( pC!=0 ); |
| 2626 | assert( pOp->p3>=0 ); |
drh | c9ef12f | 2022-10-10 21:21:04 +0000 | [diff] [blame] | 2627 | if( pOp->p3<pC->nHdrParsed ){ |
drh | 49d77ee | 2022-10-10 18:25:05 +0000 | [diff] [blame] | 2628 | serialType = pC->aType[pOp->p3]; |
drh | c2777ab | 2022-10-11 13:57:55 +0000 | [diff] [blame] | 2629 | if( serialType>=12 ){ |
| 2630 | if( serialType&1 ){ |
| 2631 | typeMask = 0x04; /* SQLITE_TEXT */ |
| 2632 | }else{ |
| 2633 | typeMask = 0x08; /* SQLITE_BLOB */ |
| 2634 | } |
drh | 49d77ee | 2022-10-10 18:25:05 +0000 | [diff] [blame] | 2635 | }else{ |
drh | c2777ab | 2022-10-11 13:57:55 +0000 | [diff] [blame] | 2636 | static const unsigned char aMask[] = { |
| 2637 | 0x10, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x2, |
| 2638 | 0x01, 0x01, 0x10, 0x10 |
| 2639 | }; |
| 2640 | testcase( serialType==0 ); |
| 2641 | testcase( serialType==1 ); |
| 2642 | testcase( serialType==2 ); |
| 2643 | testcase( serialType==3 ); |
| 2644 | testcase( serialType==4 ); |
| 2645 | testcase( serialType==5 ); |
| 2646 | testcase( serialType==6 ); |
| 2647 | testcase( serialType==7 ); |
| 2648 | testcase( serialType==8 ); |
| 2649 | testcase( serialType==9 ); |
| 2650 | testcase( serialType==10 ); |
| 2651 | testcase( serialType==11 ); |
| 2652 | typeMask = aMask[serialType]; |
drh | 49d77ee | 2022-10-10 18:25:05 +0000 | [diff] [blame] | 2653 | } |
| 2654 | }else{ |
| 2655 | typeMask = 1 << (pOp->p4.i - 1); |
drh | c2777ab | 2022-10-11 13:57:55 +0000 | [diff] [blame] | 2656 | testcase( typeMask==0x01 ); |
| 2657 | testcase( typeMask==0x02 ); |
| 2658 | testcase( typeMask==0x04 ); |
| 2659 | testcase( typeMask==0x08 ); |
| 2660 | testcase( typeMask==0x10 ); |
drh | 49d77ee | 2022-10-10 18:25:05 +0000 | [diff] [blame] | 2661 | } |
| 2662 | }else{ |
| 2663 | assert( memIsValid(&aMem[pOp->p3]) ); |
| 2664 | typeMask = 1 << (sqlite3_value_type((sqlite3_value*)&aMem[pOp->p3])-1); |
drh | c2777ab | 2022-10-11 13:57:55 +0000 | [diff] [blame] | 2665 | testcase( typeMask==0x01 ); |
| 2666 | testcase( typeMask==0x02 ); |
| 2667 | testcase( typeMask==0x04 ); |
| 2668 | testcase( typeMask==0x08 ); |
| 2669 | testcase( typeMask==0x10 ); |
drh | 49d77ee | 2022-10-10 18:25:05 +0000 | [diff] [blame] | 2670 | } |
| 2671 | VdbeBranchTaken( (typeMask & pOp->p5)!=0, 2); |
| 2672 | if( typeMask & pOp->p5 ){ |
| 2673 | goto jump_to_p2; |
| 2674 | } |
drh | 71c770f | 2021-08-19 16:29:33 +0000 | [diff] [blame] | 2675 | break; |
| 2676 | } |
| 2677 | |
drh | 871e7ff | 2021-03-29 14:40:48 +0000 | [diff] [blame] | 2678 | /* Opcode: ZeroOrNull P1 P2 P3 * * |
drh | 4bc2045 | 2021-03-29 18:53:47 +0000 | [diff] [blame] | 2679 | ** Synopsis: r[P2] = 0 OR NULL |
drh | 871e7ff | 2021-03-29 14:40:48 +0000 | [diff] [blame] | 2680 | ** |
drh | 4bc2045 | 2021-03-29 18:53:47 +0000 | [diff] [blame] | 2681 | ** If all both registers P1 and P3 are NOT NULL, then store a zero in |
| 2682 | ** register P2. If either registers P1 or P3 are NULL then put |
| 2683 | ** a NULL in register P2. |
drh | 871e7ff | 2021-03-29 14:40:48 +0000 | [diff] [blame] | 2684 | */ |
drh | 4bc2045 | 2021-03-29 18:53:47 +0000 | [diff] [blame] | 2685 | case OP_ZeroOrNull: { /* in1, in2, out2, in3 */ |
drh | 871e7ff | 2021-03-29 14:40:48 +0000 | [diff] [blame] | 2686 | if( (aMem[pOp->p1].flags & MEM_Null)!=0 |
| 2687 | || (aMem[pOp->p3].flags & MEM_Null)!=0 |
| 2688 | ){ |
| 2689 | sqlite3VdbeMemSetNull(aMem + pOp->p2); |
| 2690 | }else{ |
| 2691 | sqlite3VdbeMemSetInt64(aMem + pOp->p2, 0); |
| 2692 | } |
| 2693 | break; |
| 2694 | } |
| 2695 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 2696 | /* Opcode: NotNull P1 P2 * * * |
drh | fc8d4f9 | 2013-11-08 15:19:46 +0000 | [diff] [blame] | 2697 | ** Synopsis: if r[P1]!=NULL goto P2 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2698 | ** |
drh | 6a288a3 | 2008-01-07 19:20:24 +0000 | [diff] [blame] | 2699 | ** Jump to P2 if the value in register P1 is not NULL. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2700 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2701 | case OP_NotNull: { /* same as TK_NOTNULL, jump, in1 */ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 2702 | pIn1 = &aMem[pOp->p1]; |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 2703 | VdbeBranchTaken( (pIn1->flags & MEM_Null)==0, 2); |
drh | 6a288a3 | 2008-01-07 19:20:24 +0000 | [diff] [blame] | 2704 | if( (pIn1->flags & MEM_Null)==0 ){ |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 2705 | goto jump_to_p2; |
drh | 6a288a3 | 2008-01-07 19:20:24 +0000 | [diff] [blame] | 2706 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2707 | break; |
| 2708 | } |
| 2709 | |
drh | 31d6fd5 | 2017-04-14 19:03:10 +0000 | [diff] [blame] | 2710 | /* Opcode: IfNullRow P1 P2 P3 * * |
| 2711 | ** Synopsis: if P1.nullRow then r[P3]=NULL, goto P2 |
| 2712 | ** |
| 2713 | ** Check the cursor P1 to see if it is currently pointing at a NULL row. |
| 2714 | ** If it is, then set register P3 to NULL and jump immediately to P2. |
| 2715 | ** If P1 is not on a NULL row, then fall through without making any |
| 2716 | ** changes. |
drh | d198183 | 2022-06-23 15:15:03 +0000 | [diff] [blame] | 2717 | ** |
| 2718 | ** If P1 is not an open cursor, then this opcode is a no-op. |
drh | 31d6fd5 | 2017-04-14 19:03:10 +0000 | [diff] [blame] | 2719 | */ |
| 2720 | case OP_IfNullRow: { /* jump */ |
drh | d198183 | 2022-06-23 15:15:03 +0000 | [diff] [blame] | 2721 | VdbeCursor *pC; |
drh | 31d6fd5 | 2017-04-14 19:03:10 +0000 | [diff] [blame] | 2722 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
drh | d198183 | 2022-06-23 15:15:03 +0000 | [diff] [blame] | 2723 | pC = p->apCsr[pOp->p1]; |
drh | 54f1fc4 | 2022-06-25 20:32:29 +0000 | [diff] [blame] | 2724 | if( ALWAYS(pC) && pC->nullRow ){ |
drh | 31d6fd5 | 2017-04-14 19:03:10 +0000 | [diff] [blame] | 2725 | sqlite3VdbeMemSetNull(aMem + pOp->p3); |
| 2726 | goto jump_to_p2; |
| 2727 | } |
| 2728 | break; |
| 2729 | } |
| 2730 | |
drh | 092457b | 2017-12-29 15:04:49 +0000 | [diff] [blame] | 2731 | #ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC |
| 2732 | /* Opcode: Offset P1 P2 P3 * * |
| 2733 | ** Synopsis: r[P3] = sqlite_offset(P1) |
drh | 2fc865c | 2017-12-16 20:20:37 +0000 | [diff] [blame] | 2734 | ** |
drh | 092457b | 2017-12-29 15:04:49 +0000 | [diff] [blame] | 2735 | ** 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] | 2736 | ** start of the payload for the record at which that cursor P1 is currently |
| 2737 | ** pointing. |
drh | fe6d20e | 2017-12-29 14:33:54 +0000 | [diff] [blame] | 2738 | ** |
drh | 092457b | 2017-12-29 15:04:49 +0000 | [diff] [blame] | 2739 | ** P2 is the column number for the argument to the sqlite_offset() function. |
drh | fe6d20e | 2017-12-29 14:33:54 +0000 | [diff] [blame] | 2740 | ** This opcode does not use P2 itself, but the P2 value is used by the |
| 2741 | ** code generator. The P1, P2, and P3 operands to this opcode are the |
mistachkin | 5e9825e | 2018-03-01 18:09:02 +0000 | [diff] [blame] | 2742 | ** same as for OP_Column. |
drh | 092457b | 2017-12-29 15:04:49 +0000 | [diff] [blame] | 2743 | ** |
| 2744 | ** This opcode is only available if SQLite is compiled with the |
| 2745 | ** -DSQLITE_ENABLE_OFFSET_SQL_FUNC option. |
drh | 2fc865c | 2017-12-16 20:20:37 +0000 | [diff] [blame] | 2746 | */ |
drh | 092457b | 2017-12-29 15:04:49 +0000 | [diff] [blame] | 2747 | case OP_Offset: { /* out3 */ |
drh | 2fc865c | 2017-12-16 20:20:37 +0000 | [diff] [blame] | 2748 | VdbeCursor *pC; /* The VDBE cursor */ |
| 2749 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 2750 | pC = p->apCsr[pOp->p1]; |
drh | fe6d20e | 2017-12-29 14:33:54 +0000 | [diff] [blame] | 2751 | pOut = &p->aMem[pOp->p3]; |
drh | 8f32d92 | 2022-03-05 19:36:29 +0000 | [diff] [blame] | 2752 | if( pC==0 || pC->eCurType!=CURTYPE_BTREE ){ |
drh | fe6d20e | 2017-12-29 14:33:54 +0000 | [diff] [blame] | 2753 | sqlite3VdbeMemSetNull(pOut); |
drh | 2fc865c | 2017-12-16 20:20:37 +0000 | [diff] [blame] | 2754 | }else{ |
drh | 8f32d92 | 2022-03-05 19:36:29 +0000 | [diff] [blame] | 2755 | if( pC->deferredMoveto ){ |
| 2756 | rc = sqlite3VdbeFinishMoveto(pC); |
| 2757 | if( rc ) goto abort_due_to_error; |
| 2758 | } |
drh | dc95136 | 2022-03-05 23:52:05 +0000 | [diff] [blame] | 2759 | if( sqlite3BtreeEof(pC->uc.pCursor) ){ |
drh | 8f32d92 | 2022-03-05 19:36:29 +0000 | [diff] [blame] | 2760 | sqlite3VdbeMemSetNull(pOut); |
| 2761 | }else{ |
| 2762 | sqlite3VdbeMemSetInt64(pOut, sqlite3BtreeOffset(pC->uc.pCursor)); |
| 2763 | } |
drh | 2fc865c | 2017-12-16 20:20:37 +0000 | [diff] [blame] | 2764 | } |
| 2765 | break; |
| 2766 | } |
drh | 092457b | 2017-12-29 15:04:49 +0000 | [diff] [blame] | 2767 | #endif /* SQLITE_ENABLE_OFFSET_SQL_FUNC */ |
drh | 2fc865c | 2017-12-16 20:20:37 +0000 | [diff] [blame] | 2768 | |
drh | 3e9ca09 | 2009-09-08 01:14:48 +0000 | [diff] [blame] | 2769 | /* Opcode: Column P1 P2 P3 P4 P5 |
drh | 088b615 | 2022-04-18 13:57:57 +0000 | [diff] [blame] | 2770 | ** Synopsis: r[P3]=PX cursor P1 column P2 |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2771 | ** |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 2772 | ** Interpret the data that cursor P1 points to as a structure built using |
| 2773 | ** the MakeRecord instruction. (See the MakeRecord opcode for additional |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 2774 | ** information about the format of the data.) Extract the P2-th column |
drh | 7ca4af6 | 2022-10-13 14:01:11 +0000 | [diff] [blame] | 2775 | ** from this record. If there are less than (P2+1) |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 2776 | ** values in the record, extract a NULL. |
| 2777 | ** |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2778 | ** The value extracted is stored in register P3. |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2779 | ** |
drh | 1cc3a36 | 2017-04-03 13:17:31 +0000 | [diff] [blame] | 2780 | ** If the record contains fewer than P2 fields, then extract a NULL. Or, |
danielk1977 | 1f4aa33 | 2008-01-03 09:51:55 +0000 | [diff] [blame] | 2781 | ** if the P4 argument is a P4_MEM use the value of the P4 argument as |
| 2782 | ** the result. |
drh | 3e9ca09 | 2009-09-08 01:14:48 +0000 | [diff] [blame] | 2783 | ** |
drh | eddfa98 | 2022-10-13 14:54:32 +0000 | [diff] [blame] | 2784 | ** If the OPFLAG_LENGTHARG bit is set in P5 then the result is guaranteed |
| 2785 | ** to only be used by the length() function or the equivalent. The content |
| 2786 | ** of large blobs is not loaded, thus saving CPU cycles. If the |
| 2787 | ** OPFLAG_TYPEOFARG bit is set then the result will only be used by the |
| 2788 | ** typeof() function or the IS NULL or IS NOT NULL operators or the |
| 2789 | ** equivalent. In this case, all content loading can be omitted. |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2790 | */ |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 2791 | case OP_Column: { |
drh | abc3815 | 2020-07-22 13:38:04 +0000 | [diff] [blame] | 2792 | u32 p2; /* column number to retrieve */ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2793 | VdbeCursor *pC; /* The VDBE cursor */ |
drh | fc56950 | 2022-02-25 20:11:59 +0000 | [diff] [blame] | 2794 | BtCursor *pCrsr; /* The B-Tree cursor corresponding to pC */ |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 2795 | 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] | 2796 | int len; /* The length of the serialized data for the column */ |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 2797 | int i; /* Loop counter */ |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 2798 | Mem *pDest; /* Where to write the extracted value */ |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 2799 | Mem sMem; /* For storing the record being decoded */ |
drh | 399af1d | 2013-11-20 17:25:55 +0000 | [diff] [blame] | 2800 | const u8 *zData; /* Part of the record being decoded */ |
| 2801 | const u8 *zHdr; /* Next unparsed byte of the header */ |
| 2802 | const u8 *zEndHdr; /* Pointer to first byte after the header */ |
drh | c6ce3883 | 2015-10-15 21:30:24 +0000 | [diff] [blame] | 2803 | u64 offset64; /* 64-bit offset */ |
drh | 5a077b7 | 2011-08-29 02:16:18 +0000 | [diff] [blame] | 2804 | u32 t; /* A type code from the record header */ |
drh | 3e9ca09 | 2009-09-08 01:14:48 +0000 | [diff] [blame] | 2805 | Mem *pReg; /* PseudoTable input register */ |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2806 | |
drh | 8c7715d | 2019-12-20 14:37:56 +0000 | [diff] [blame] | 2807 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
drh | fc56950 | 2022-02-25 20:11:59 +0000 | [diff] [blame] | 2808 | assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) ); |
dan | de892d9 | 2016-01-29 19:29:45 +0000 | [diff] [blame] | 2809 | pC = p->apCsr[pOp->p1]; |
drh | abc3815 | 2020-07-22 13:38:04 +0000 | [diff] [blame] | 2810 | p2 = (u32)pOp->p2; |
dan | de892d9 | 2016-01-29 19:29:45 +0000 | [diff] [blame] | 2811 | |
drh | fc56950 | 2022-02-25 20:11:59 +0000 | [diff] [blame] | 2812 | op_column_restart: |
danielk1977 | 6c92409 | 2007-11-12 08:09:34 +0000 | [diff] [blame] | 2813 | assert( pC!=0 ); |
drh | 7c96039 | 2022-04-13 18:20:23 +0000 | [diff] [blame] | 2814 | assert( p2<(u32)pC->nField |
| 2815 | || (pC->eCurType==CURTYPE_PSEUDO && pC->seekResult==0) ); |
drh | b53a5a9 | 2014-10-12 22:37:22 +0000 | [diff] [blame] | 2816 | aOffset = pC->aOffset; |
drh | b248668 | 2022-01-03 01:43:28 +0000 | [diff] [blame] | 2817 | assert( aOffset==pC->aType+pC->nField ); |
drh | 62aaa6c | 2015-11-21 17:27:42 +0000 | [diff] [blame] | 2818 | assert( pC->eCurType!=CURTYPE_VTAB ); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 2819 | assert( pC->eCurType!=CURTYPE_PSEUDO || pC->nullRow ); |
| 2820 | assert( pC->eCurType!=CURTYPE_SORTER ); |
drh | 399af1d | 2013-11-20 17:25:55 +0000 | [diff] [blame] | 2821 | |
drh | a43a02e | 2016-05-19 17:51:19 +0000 | [diff] [blame] | 2822 | if( pC->cacheStatus!=p->cacheCtr ){ /*OPTIMIZATION-IF-FALSE*/ |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2823 | if( pC->nullRow ){ |
drh | 3ac6243 | 2022-04-13 17:41:03 +0000 | [diff] [blame] | 2824 | if( pC->eCurType==CURTYPE_PSEUDO && pC->seekResult>0 ){ |
drh | fe0cf7a | 2017-08-16 19:20:20 +0000 | [diff] [blame] | 2825 | /* For the special case of as pseudo-cursor, the seekResult field |
| 2826 | ** identifies the register that holds the record */ |
drh | fe0cf7a | 2017-08-16 19:20:20 +0000 | [diff] [blame] | 2827 | pReg = &aMem[pC->seekResult]; |
drh | c8606e4 | 2013-11-20 19:28:03 +0000 | [diff] [blame] | 2828 | assert( pReg->flags & MEM_Blob ); |
| 2829 | assert( memIsValid(pReg) ); |
drh | 6cd8c8c | 2017-08-15 14:14:36 +0000 | [diff] [blame] | 2830 | pC->payloadSize = pC->szRow = pReg->n; |
drh | c8606e4 | 2013-11-20 19:28:03 +0000 | [diff] [blame] | 2831 | pC->aRow = (u8*)pReg->z; |
| 2832 | }else{ |
drh | fc56950 | 2022-02-25 20:11:59 +0000 | [diff] [blame] | 2833 | pDest = &aMem[pOp->p3]; |
| 2834 | memAboutToChange(p, pDest); |
drh | 6b5631e | 2014-11-05 15:57:39 +0000 | [diff] [blame] | 2835 | sqlite3VdbeMemSetNull(pDest); |
drh | 399af1d | 2013-11-20 17:25:55 +0000 | [diff] [blame] | 2836 | goto op_column_out; |
| 2837 | } |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2838 | }else{ |
drh | 06a09a8 | 2016-11-25 17:03:03 +0000 | [diff] [blame] | 2839 | pCrsr = pC->uc.pCursor; |
drh | fc56950 | 2022-02-25 20:11:59 +0000 | [diff] [blame] | 2840 | if( pC->deferredMoveto ){ |
| 2841 | u32 iMap; |
| 2842 | assert( !pC->isEphemeral ); |
| 2843 | if( pC->ub.aAltMap && (iMap = pC->ub.aAltMap[1+p2])>0 ){ |
| 2844 | pC = pC->pAltCursor; |
| 2845 | p2 = iMap - 1; |
| 2846 | goto op_column_restart; |
| 2847 | } |
| 2848 | rc = sqlite3VdbeFinishMoveto(pC); |
| 2849 | if( rc ) goto abort_due_to_error; |
| 2850 | }else if( sqlite3BtreeCursorHasMoved(pCrsr) ){ |
| 2851 | rc = sqlite3VdbeHandleMovedCursor(pC); |
| 2852 | if( rc ) goto abort_due_to_error; |
| 2853 | goto op_column_restart; |
| 2854 | } |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 2855 | assert( pC->eCurType==CURTYPE_BTREE ); |
drh | c8606e4 | 2013-11-20 19:28:03 +0000 | [diff] [blame] | 2856 | assert( pCrsr ); |
drh | a7c90c4 | 2016-06-04 20:37:10 +0000 | [diff] [blame] | 2857 | assert( sqlite3BtreeCursorIsValid(pCrsr) ); |
| 2858 | pC->payloadSize = sqlite3BtreePayloadSize(pCrsr); |
drh | 6cd8c8c | 2017-08-15 14:14:36 +0000 | [diff] [blame] | 2859 | pC->aRow = sqlite3BtreePayloadFetch(pCrsr, &pC->szRow); |
| 2860 | assert( pC->szRow<=pC->payloadSize ); |
| 2861 | assert( pC->szRow<=65536 ); /* Maximum page size is 64KiB */ |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2862 | } |
drh | b73857f | 2006-03-17 00:25:59 +0000 | [diff] [blame] | 2863 | pC->cacheStatus = p->cacheCtr; |
drh | c2808f3 | 2022-04-02 22:47:47 +0000 | [diff] [blame] | 2864 | if( (aOffset[0] = pC->aRow[0])<0x80 ){ |
| 2865 | pC->iHdrOffset = 1; |
| 2866 | }else{ |
| 2867 | pC->iHdrOffset = sqlite3GetVarint32(pC->aRow, aOffset); |
| 2868 | } |
drh | 399af1d | 2013-11-20 17:25:55 +0000 | [diff] [blame] | 2869 | pC->nHdrParsed = 0; |
drh | 35cd643 | 2009-06-05 14:17:21 +0000 | [diff] [blame] | 2870 | |
drh | 1f613c4 | 2017-08-16 14:16:19 +0000 | [diff] [blame] | 2871 | if( pC->szRow<aOffset[0] ){ /*OPTIMIZATION-IF-FALSE*/ |
drh | c81aa2e | 2014-10-11 23:31:52 +0000 | [diff] [blame] | 2872 | /* pC->aRow does not have to hold the entire row, but it does at least |
| 2873 | ** need to cover the header of the record. If pC->aRow does not contain |
| 2874 | ** the complete header, then set it to zero, forcing the header to be |
| 2875 | ** dynamically allocated. */ |
| 2876 | pC->aRow = 0; |
| 2877 | pC->szRow = 0; |
drh | 848a332 | 2015-10-16 12:53:47 +0000 | [diff] [blame] | 2878 | |
| 2879 | /* Make sure a corrupt database has not given us an oversize header. |
| 2880 | ** Do this now to avoid an oversize memory allocation. |
| 2881 | ** |
| 2882 | ** Type entries can be between 1 and 5 bytes each. But 4 and 5 byte |
| 2883 | ** types use so much data space that there can only be 4096 and 32 of |
| 2884 | ** them, respectively. So the maximum header length results from a |
| 2885 | ** 3-byte type for each of the maximum of 32768 columns plus three |
| 2886 | ** extra bytes for the header length itself. 32768*3 + 3 = 98307. |
| 2887 | */ |
drh | 1f613c4 | 2017-08-16 14:16:19 +0000 | [diff] [blame] | 2888 | if( aOffset[0] > 98307 || aOffset[0] > pC->payloadSize ){ |
drh | 74588ce | 2017-09-13 00:13:05 +0000 | [diff] [blame] | 2889 | goto op_column_corrupt; |
drh | 848a332 | 2015-10-16 12:53:47 +0000 | [diff] [blame] | 2890 | } |
drh | 95b225a | 2017-08-16 11:04:22 +0000 | [diff] [blame] | 2891 | }else{ |
| 2892 | /* This is an optimization. By skipping over the first few tests |
| 2893 | ** (ex: pC->nHdrParsed<=p2) in the next section, we achieve a |
| 2894 | ** measurable performance gain. |
| 2895 | ** |
drh | 1f613c4 | 2017-08-16 14:16:19 +0000 | [diff] [blame] | 2896 | ** 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] | 2897 | ** generated by SQLite, and could be considered corruption, but we |
drh | 1f613c4 | 2017-08-16 14:16:19 +0000 | [diff] [blame] | 2898 | ** accept it for historical reasons. When aOffset[0]==0, the code this |
drh | 95b225a | 2017-08-16 11:04:22 +0000 | [diff] [blame] | 2899 | ** branch jumps to reads past the end of the record, but never more |
| 2900 | ** than a few bytes. Even if the record occurs at the end of the page |
| 2901 | ** content area, the "page header" comes after the page content and so |
| 2902 | ** this overread is harmless. Similar overreads can occur for a corrupt |
| 2903 | ** database file. |
drh | 0eda6cd | 2016-05-19 16:58:42 +0000 | [diff] [blame] | 2904 | */ |
| 2905 | zData = pC->aRow; |
| 2906 | assert( pC->nHdrParsed<=p2 ); /* Conditional skipped */ |
drh | 1f613c4 | 2017-08-16 14:16:19 +0000 | [diff] [blame] | 2907 | testcase( aOffset[0]==0 ); |
drh | 0eda6cd | 2016-05-19 16:58:42 +0000 | [diff] [blame] | 2908 | goto op_column_read_header; |
drh | c81aa2e | 2014-10-11 23:31:52 +0000 | [diff] [blame] | 2909 | } |
drh | fc56950 | 2022-02-25 20:11:59 +0000 | [diff] [blame] | 2910 | }else if( sqlite3BtreeCursorHasMoved(pC->uc.pCursor) ){ |
| 2911 | rc = sqlite3VdbeHandleMovedCursor(pC); |
| 2912 | if( rc ) goto abort_due_to_error; |
| 2913 | goto op_column_restart; |
drh | 399af1d | 2013-11-20 17:25:55 +0000 | [diff] [blame] | 2914 | } |
drh | 35cd643 | 2009-06-05 14:17:21 +0000 | [diff] [blame] | 2915 | |
drh | 399af1d | 2013-11-20 17:25:55 +0000 | [diff] [blame] | 2916 | /* 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] | 2917 | ** parsed and valid information is in aOffset[] and pC->aType[]. |
drh | 399af1d | 2013-11-20 17:25:55 +0000 | [diff] [blame] | 2918 | */ |
drh | c8606e4 | 2013-11-20 19:28:03 +0000 | [diff] [blame] | 2919 | if( pC->nHdrParsed<=p2 ){ |
drh | 380d685 | 2013-11-20 20:58:00 +0000 | [diff] [blame] | 2920 | /* If there is more header available for parsing in the record, try |
| 2921 | ** to extract additional fields up through the p2+1-th field |
drh | 35cd643 | 2009-06-05 14:17:21 +0000 | [diff] [blame] | 2922 | */ |
drh | c8606e4 | 2013-11-20 19:28:03 +0000 | [diff] [blame] | 2923 | if( pC->iHdrOffset<aOffset[0] ){ |
| 2924 | /* Make sure zData points to enough of the record to cover the header. */ |
| 2925 | if( pC->aRow==0 ){ |
| 2926 | memset(&sMem, 0, sizeof(sMem)); |
drh | 2a74006 | 2020-02-05 18:28:17 +0000 | [diff] [blame] | 2927 | rc = sqlite3VdbeMemFromBtreeZeroOffset(pC->uc.pCursor,aOffset[0],&sMem); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 2928 | if( rc!=SQLITE_OK ) goto abort_due_to_error; |
drh | c8606e4 | 2013-11-20 19:28:03 +0000 | [diff] [blame] | 2929 | zData = (u8*)sMem.z; |
| 2930 | }else{ |
| 2931 | zData = pC->aRow; |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2932 | } |
drh | c8606e4 | 2013-11-20 19:28:03 +0000 | [diff] [blame] | 2933 | |
drh | 0c8f760 | 2014-09-19 16:56:45 +0000 | [diff] [blame] | 2934 | /* 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] | 2935 | op_column_read_header: |
drh | c8606e4 | 2013-11-20 19:28:03 +0000 | [diff] [blame] | 2936 | i = pC->nHdrParsed; |
drh | c6ce3883 | 2015-10-15 21:30:24 +0000 | [diff] [blame] | 2937 | offset64 = aOffset[i]; |
drh | c8606e4 | 2013-11-20 19:28:03 +0000 | [diff] [blame] | 2938 | zHdr = zData + pC->iHdrOffset; |
| 2939 | zEndHdr = zData + aOffset[0]; |
drh | 95b225a | 2017-08-16 11:04:22 +0000 | [diff] [blame] | 2940 | testcase( zHdr>=zEndHdr ); |
drh | c8606e4 | 2013-11-20 19:28:03 +0000 | [diff] [blame] | 2941 | do{ |
drh | c332e04 | 2019-02-12 21:04:33 +0000 | [diff] [blame] | 2942 | if( (pC->aType[i] = t = zHdr[0])<0x80 ){ |
drh | c8606e4 | 2013-11-20 19:28:03 +0000 | [diff] [blame] | 2943 | zHdr++; |
drh | faf3727 | 2015-10-16 14:23:42 +0000 | [diff] [blame] | 2944 | offset64 += sqlite3VdbeOneByteSerialTypeLen(t); |
drh | 5a077b7 | 2011-08-29 02:16:18 +0000 | [diff] [blame] | 2945 | }else{ |
drh | c8606e4 | 2013-11-20 19:28:03 +0000 | [diff] [blame] | 2946 | zHdr += sqlite3GetVarint32(zHdr, &t); |
drh | c332e04 | 2019-02-12 21:04:33 +0000 | [diff] [blame] | 2947 | pC->aType[i] = t; |
drh | faf3727 | 2015-10-16 14:23:42 +0000 | [diff] [blame] | 2948 | offset64 += sqlite3VdbeSerialTypeLen(t); |
drh | 5a077b7 | 2011-08-29 02:16:18 +0000 | [diff] [blame] | 2949 | } |
drh | c332e04 | 2019-02-12 21:04:33 +0000 | [diff] [blame] | 2950 | aOffset[++i] = (u32)(offset64 & 0xffffffff); |
drh | 8deae5a | 2020-07-29 12:23:20 +0000 | [diff] [blame] | 2951 | }while( (u32)i<=p2 && zHdr<zEndHdr ); |
drh | 170c276 | 2016-05-20 21:40:11 +0000 | [diff] [blame] | 2952 | |
drh | 8dd8362 | 2014-10-13 23:39:02 +0000 | [diff] [blame] | 2953 | /* The record is corrupt if any of the following are true: |
| 2954 | ** (1) the bytes of the header extend past the declared header size |
drh | 8dd8362 | 2014-10-13 23:39:02 +0000 | [diff] [blame] | 2955 | ** (2) the entire header was used but not all data was used |
drh | 8dd8362 | 2014-10-13 23:39:02 +0000 | [diff] [blame] | 2956 | ** (3) the end of the data extends beyond the end of the record. |
drh | c8606e4 | 2013-11-20 19:28:03 +0000 | [diff] [blame] | 2957 | */ |
drh | c6ce3883 | 2015-10-15 21:30:24 +0000 | [diff] [blame] | 2958 | if( (zHdr>=zEndHdr && (zHdr>zEndHdr || offset64!=pC->payloadSize)) |
| 2959 | || (offset64 > pC->payloadSize) |
drh | c8606e4 | 2013-11-20 19:28:03 +0000 | [diff] [blame] | 2960 | ){ |
drh | 95b225a | 2017-08-16 11:04:22 +0000 | [diff] [blame] | 2961 | if( aOffset[0]==0 ){ |
| 2962 | i = 0; |
| 2963 | zHdr = zEndHdr; |
| 2964 | }else{ |
| 2965 | if( pC->aRow==0 ) sqlite3VdbeMemRelease(&sMem); |
drh | 74588ce | 2017-09-13 00:13:05 +0000 | [diff] [blame] | 2966 | goto op_column_corrupt; |
drh | 95b225a | 2017-08-16 11:04:22 +0000 | [diff] [blame] | 2967 | } |
danielk1977 | dedf45b | 2006-01-13 17:12:01 +0000 | [diff] [blame] | 2968 | } |
drh | ddb2b4a | 2016-03-25 12:10:32 +0000 | [diff] [blame] | 2969 | |
drh | 170c276 | 2016-05-20 21:40:11 +0000 | [diff] [blame] | 2970 | pC->nHdrParsed = i; |
| 2971 | pC->iHdrOffset = (u32)(zHdr - zData); |
| 2972 | if( pC->aRow==0 ) sqlite3VdbeMemRelease(&sMem); |
mistachkin | 8c7cd6a | 2015-12-16 21:09:53 +0000 | [diff] [blame] | 2973 | }else{ |
drh | 9fbc885 | 2016-01-04 03:48:46 +0000 | [diff] [blame] | 2974 | t = 0; |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2975 | } |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 2976 | |
drh | f2db338 | 2015-04-30 20:33:25 +0000 | [diff] [blame] | 2977 | /* If after trying to extract new entries from the header, nHdrParsed is |
drh | 380d685 | 2013-11-20 20:58:00 +0000 | [diff] [blame] | 2978 | ** still not up to p2, that means that the record has fewer than p2 |
| 2979 | ** columns. So the result will be either the default value or a NULL. |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 2980 | */ |
drh | c8606e4 | 2013-11-20 19:28:03 +0000 | [diff] [blame] | 2981 | if( pC->nHdrParsed<=p2 ){ |
drh | fc56950 | 2022-02-25 20:11:59 +0000 | [diff] [blame] | 2982 | pDest = &aMem[pOp->p3]; |
| 2983 | memAboutToChange(p, pDest); |
drh | c8606e4 | 2013-11-20 19:28:03 +0000 | [diff] [blame] | 2984 | if( pOp->p4type==P4_MEM ){ |
| 2985 | sqlite3VdbeMemShallowCopy(pDest, pOp->p4.pMem, MEM_Static); |
| 2986 | }else{ |
drh | 22e8d83 | 2014-10-29 00:58:38 +0000 | [diff] [blame] | 2987 | sqlite3VdbeMemSetNull(pDest); |
drh | c8606e4 | 2013-11-20 19:28:03 +0000 | [diff] [blame] | 2988 | } |
danielk1977 | 3c9cc8d | 2005-01-17 03:40:08 +0000 | [diff] [blame] | 2989 | goto op_column_out; |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 2990 | } |
drh | 95fa606 | 2015-10-16 13:50:08 +0000 | [diff] [blame] | 2991 | }else{ |
| 2992 | t = pC->aType[p2]; |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 2993 | } |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2994 | |
drh | 380d685 | 2013-11-20 20:58:00 +0000 | [diff] [blame] | 2995 | /* Extract the content for the p2+1-th column. Control can only |
drh | 0c8f760 | 2014-09-19 16:56:45 +0000 | [diff] [blame] | 2996 | ** 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] | 2997 | ** all valid. |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2998 | */ |
drh | c8606e4 | 2013-11-20 19:28:03 +0000 | [diff] [blame] | 2999 | assert( p2<pC->nHdrParsed ); |
| 3000 | assert( rc==SQLITE_OK ); |
drh | fc56950 | 2022-02-25 20:11:59 +0000 | [diff] [blame] | 3001 | pDest = &aMem[pOp->p3]; |
| 3002 | memAboutToChange(p, pDest); |
drh | 75fd054 | 2014-03-01 16:24:44 +0000 | [diff] [blame] | 3003 | assert( sqlite3VdbeCheckMemInvariants(pDest) ); |
drh | a1851ef | 2016-05-20 19:51:28 +0000 | [diff] [blame] | 3004 | if( VdbeMemDynamic(pDest) ){ |
| 3005 | sqlite3VdbeMemSetNull(pDest); |
| 3006 | } |
drh | 95fa606 | 2015-10-16 13:50:08 +0000 | [diff] [blame] | 3007 | assert( t==pC->aType[p2] ); |
drh | c8606e4 | 2013-11-20 19:28:03 +0000 | [diff] [blame] | 3008 | if( pC->szRow>=aOffset[p2+1] ){ |
drh | 380d685 | 2013-11-20 20:58:00 +0000 | [diff] [blame] | 3009 | /* This is the common case where the desired content fits on the original |
| 3010 | ** page - where the content is not on an overflow page */ |
drh | 69f6e25 | 2016-01-11 18:05:00 +0000 | [diff] [blame] | 3011 | zData = pC->aRow + aOffset[p2]; |
| 3012 | if( t<12 ){ |
| 3013 | sqlite3VdbeSerialGet(zData, t, pDest); |
| 3014 | }else{ |
| 3015 | /* If the column value is a string, we need a persistent value, not |
| 3016 | ** a MEM_Ephem value. This branch is a fast short-cut that is equivalent |
| 3017 | ** to calling sqlite3VdbeSerialGet() and sqlite3VdbeDeephemeralize(). |
| 3018 | */ |
| 3019 | static const u16 aFlag[] = { MEM_Blob, MEM_Str|MEM_Term }; |
| 3020 | pDest->n = len = (t-12)/2; |
drh | a1851ef | 2016-05-20 19:51:28 +0000 | [diff] [blame] | 3021 | pDest->enc = encoding; |
drh | 69f6e25 | 2016-01-11 18:05:00 +0000 | [diff] [blame] | 3022 | if( pDest->szMalloc < len+2 ){ |
drh | 9090df6 | 2022-02-26 14:39:08 +0000 | [diff] [blame] | 3023 | if( len>db->aLimit[SQLITE_LIMIT_LENGTH] ) goto too_big; |
drh | 69f6e25 | 2016-01-11 18:05:00 +0000 | [diff] [blame] | 3024 | pDest->flags = MEM_Null; |
| 3025 | if( sqlite3VdbeMemGrow(pDest, len+2, 0) ) goto no_mem; |
| 3026 | }else{ |
| 3027 | pDest->z = pDest->zMalloc; |
| 3028 | } |
| 3029 | memcpy(pDest->z, zData, len); |
| 3030 | pDest->z[len] = 0; |
| 3031 | pDest->z[len+1] = 0; |
| 3032 | pDest->flags = aFlag[t&1]; |
| 3033 | } |
danielk1977 | 36963fd | 2005-02-19 08:18:05 +0000 | [diff] [blame] | 3034 | }else{ |
drh | a1851ef | 2016-05-20 19:51:28 +0000 | [diff] [blame] | 3035 | pDest->enc = encoding; |
drh | 58c9608 | 2013-12-23 11:33:32 +0000 | [diff] [blame] | 3036 | /* This branch happens only when content is on overflow pages */ |
drh | 380d685 | 2013-11-20 20:58:00 +0000 | [diff] [blame] | 3037 | if( ((pOp->p5 & (OPFLAG_LENGTHARG|OPFLAG_TYPEOFARG))!=0 |
| 3038 | && ((t>=12 && (t&1)==0) || (pOp->p5 & OPFLAG_TYPEOFARG)!=0)) |
| 3039 | || (len = sqlite3VdbeSerialTypeLen(t))==0 |
drh | c8606e4 | 2013-11-20 19:28:03 +0000 | [diff] [blame] | 3040 | ){ |
drh | 2a2a696 | 2014-09-16 18:22:44 +0000 | [diff] [blame] | 3041 | /* Content is irrelevant for |
| 3042 | ** 1. the typeof() function, |
| 3043 | ** 2. the length(X) function if X is a blob, and |
| 3044 | ** 3. if the content length is zero. |
| 3045 | ** So we might as well use bogus content rather than reading |
dan | 1f9144e | 2017-03-17 13:59:06 +0000 | [diff] [blame] | 3046 | ** content from disk. |
| 3047 | ** |
| 3048 | ** Although sqlite3VdbeSerialGet() may read at most 8 bytes from the |
| 3049 | ** buffer passed to it, debugging function VdbeMemPrettyPrint() may |
drh | cbae3f8 | 2020-01-06 20:48:45 +0000 | [diff] [blame] | 3050 | ** read more. Use the global constant sqlite3CtypeMap[] as the array, |
| 3051 | ** as that array is 256 bytes long (plenty for VdbeMemPrettyPrint()) |
| 3052 | ** and it begins with a bunch of zeros. |
dan | 1f9144e | 2017-03-17 13:59:06 +0000 | [diff] [blame] | 3053 | */ |
drh | cbae3f8 | 2020-01-06 20:48:45 +0000 | [diff] [blame] | 3054 | sqlite3VdbeSerialGet((u8*)sqlite3CtypeMap, t, pDest); |
danielk1977 | aee18ef | 2005-03-09 12:26:50 +0000 | [diff] [blame] | 3055 | }else{ |
drh | 9090df6 | 2022-02-26 14:39:08 +0000 | [diff] [blame] | 3056 | if( len>db->aLimit[SQLITE_LIMIT_LENGTH] ) goto too_big; |
drh | cb3cabd | 2016-11-25 19:18:28 +0000 | [diff] [blame] | 3057 | rc = sqlite3VdbeMemFromBtree(pC->uc.pCursor, aOffset[p2], len, pDest); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 3058 | if( rc!=SQLITE_OK ) goto abort_due_to_error; |
| 3059 | sqlite3VdbeSerialGet((const u8*)pDest->z, t, pDest); |
| 3060 | pDest->flags &= ~MEM_Ephem; |
danielk1977 | aee18ef | 2005-03-09 12:26:50 +0000 | [diff] [blame] | 3061 | } |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 3062 | } |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 3063 | |
danielk1977 | 3c9cc8d | 2005-01-17 03:40:08 +0000 | [diff] [blame] | 3064 | op_column_out: |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 3065 | UPDATE_MAX_BLOBSIZE(pDest); |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 3066 | REGISTER_TRACE(pOp->p3, pDest); |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 3067 | break; |
drh | 74588ce | 2017-09-13 00:13:05 +0000 | [diff] [blame] | 3068 | |
| 3069 | op_column_corrupt: |
| 3070 | if( aOp[0].p3>0 ){ |
| 3071 | pOp = &aOp[aOp[0].p3-1]; |
| 3072 | break; |
| 3073 | }else{ |
| 3074 | rc = SQLITE_CORRUPT_BKPT; |
| 3075 | goto abort_due_to_error; |
| 3076 | } |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 3077 | } |
| 3078 | |
drh | 926aac5 | 2021-11-03 14:02:48 +0000 | [diff] [blame] | 3079 | /* Opcode: TypeCheck P1 P2 P3 P4 * |
drh | 72532f5 | 2021-08-18 19:22:27 +0000 | [diff] [blame] | 3080 | ** Synopsis: typecheck(r[P1@P2]) |
| 3081 | ** |
| 3082 | ** Apply affinities to the range of P2 registers beginning with P1. |
| 3083 | ** Take the affinities from the Table object in P4. If any value |
| 3084 | ** cannot be coerced into the correct type, then raise an error. |
| 3085 | ** |
| 3086 | ** This opcode is similar to OP_Affinity except that this opcode |
| 3087 | ** forces the register type to the Table column type. This is used |
| 3088 | ** to implement "strict affinity". |
drh | 71c770f | 2021-08-19 16:29:33 +0000 | [diff] [blame] | 3089 | ** |
drh | 926aac5 | 2021-11-03 14:02:48 +0000 | [diff] [blame] | 3090 | ** GENERATED ALWAYS AS ... STATIC columns are only checked if P3 |
| 3091 | ** is zero. When P3 is non-zero, no type checking occurs for |
| 3092 | ** static generated columns. Virtual columns are computed at query time |
| 3093 | ** and so they are never checked. |
| 3094 | ** |
drh | 71c770f | 2021-08-19 16:29:33 +0000 | [diff] [blame] | 3095 | ** Preconditions: |
| 3096 | ** |
| 3097 | ** <ul> |
| 3098 | ** <li> P2 should be the number of non-virtual columns in the |
| 3099 | ** table of P4. |
| 3100 | ** <li> Table P4 should be a STRICT table. |
| 3101 | ** </ul> |
| 3102 | ** |
| 3103 | ** If any precondition is false, an assertion fault occurs. |
drh | 72532f5 | 2021-08-18 19:22:27 +0000 | [diff] [blame] | 3104 | */ |
| 3105 | case OP_TypeCheck: { |
| 3106 | Table *pTab; |
| 3107 | Column *aCol; |
| 3108 | int i; |
| 3109 | |
| 3110 | assert( pOp->p4type==P4_TABLE ); |
| 3111 | pTab = pOp->p4.pTab; |
| 3112 | assert( pTab->tabFlags & TF_Strict ); |
drh | 71c770f | 2021-08-19 16:29:33 +0000 | [diff] [blame] | 3113 | assert( pTab->nNVCol==pOp->p2 ); |
drh | 72532f5 | 2021-08-18 19:22:27 +0000 | [diff] [blame] | 3114 | aCol = pTab->aCol; |
| 3115 | pIn1 = &aMem[pOp->p1]; |
| 3116 | for(i=0; i<pTab->nCol; i++){ |
drh | 926aac5 | 2021-11-03 14:02:48 +0000 | [diff] [blame] | 3117 | if( aCol[i].colFlags & COLFLAG_GENERATED ){ |
| 3118 | if( aCol[i].colFlags & COLFLAG_VIRTUAL ) continue; |
| 3119 | if( pOp->p3 ){ pIn1++; continue; } |
| 3120 | } |
drh | 72532f5 | 2021-08-18 19:22:27 +0000 | [diff] [blame] | 3121 | assert( pIn1 < &aMem[pOp->p1+pOp->p2] ); |
| 3122 | applyAffinity(pIn1, aCol[i].affinity, encoding); |
| 3123 | if( (pIn1->flags & MEM_Null)==0 ){ |
| 3124 | switch( aCol[i].eCType ){ |
| 3125 | case COLTYPE_BLOB: { |
| 3126 | if( (pIn1->flags & MEM_Blob)==0 ) goto vdbe_type_error; |
| 3127 | break; |
| 3128 | } |
| 3129 | case COLTYPE_INTEGER: |
| 3130 | case COLTYPE_INT: { |
| 3131 | if( (pIn1->flags & MEM_Int)==0 ) goto vdbe_type_error; |
| 3132 | break; |
| 3133 | } |
| 3134 | case COLTYPE_TEXT: { |
| 3135 | if( (pIn1->flags & MEM_Str)==0 ) goto vdbe_type_error; |
| 3136 | break; |
| 3137 | } |
drh | 2a0eefd | 2021-08-21 20:54:19 +0000 | [diff] [blame] | 3138 | case COLTYPE_REAL: { |
drh | 1f3366c | 2022-01-17 23:37:25 +0000 | [diff] [blame] | 3139 | testcase( (pIn1->flags & (MEM_Real|MEM_IntReal))==MEM_Real ); |
| 3140 | testcase( (pIn1->flags & (MEM_Real|MEM_IntReal))==MEM_IntReal ); |
drh | 72532f5 | 2021-08-18 19:22:27 +0000 | [diff] [blame] | 3141 | if( pIn1->flags & MEM_Int ){ |
| 3142 | /* When applying REAL affinity, if the result is still an MEM_Int |
| 3143 | ** that will fit in 6 bytes, then change the type to MEM_IntReal |
| 3144 | ** so that we keep the high-resolution integer value but know that |
| 3145 | ** the type really wants to be REAL. */ |
| 3146 | testcase( pIn1->u.i==140737488355328LL ); |
| 3147 | testcase( pIn1->u.i==140737488355327LL ); |
| 3148 | testcase( pIn1->u.i==-140737488355328LL ); |
| 3149 | testcase( pIn1->u.i==-140737488355329LL ); |
| 3150 | if( pIn1->u.i<=140737488355327LL && pIn1->u.i>=-140737488355328LL){ |
| 3151 | pIn1->flags |= MEM_IntReal; |
| 3152 | pIn1->flags &= ~MEM_Int; |
| 3153 | }else{ |
| 3154 | pIn1->u.r = (double)pIn1->u.i; |
| 3155 | pIn1->flags |= MEM_Real; |
| 3156 | pIn1->flags &= ~MEM_Int; |
| 3157 | } |
drh | 1f3366c | 2022-01-17 23:37:25 +0000 | [diff] [blame] | 3158 | }else if( (pIn1->flags & (MEM_Real|MEM_IntReal))==0 ){ |
drh | 72532f5 | 2021-08-18 19:22:27 +0000 | [diff] [blame] | 3159 | goto vdbe_type_error; |
| 3160 | } |
| 3161 | break; |
| 3162 | } |
drh | 2a0eefd | 2021-08-21 20:54:19 +0000 | [diff] [blame] | 3163 | default: { |
drh | b9fd010 | 2021-08-23 10:28:02 +0000 | [diff] [blame] | 3164 | /* COLTYPE_ANY. Accept anything. */ |
drh | 2a0eefd | 2021-08-21 20:54:19 +0000 | [diff] [blame] | 3165 | break; |
| 3166 | } |
drh | 72532f5 | 2021-08-18 19:22:27 +0000 | [diff] [blame] | 3167 | } |
| 3168 | } |
| 3169 | REGISTER_TRACE((int)(pIn1-aMem), pIn1); |
| 3170 | pIn1++; |
| 3171 | } |
| 3172 | assert( pIn1 == &aMem[pOp->p1+pOp->p2] ); |
| 3173 | break; |
| 3174 | |
| 3175 | vdbe_type_error: |
drh | faf9c77 | 2021-08-20 08:05:42 +0000 | [diff] [blame] | 3176 | sqlite3VdbeError(p, "cannot store %s value in %s column %s.%s", |
| 3177 | vdbeMemTypeName(pIn1), sqlite3StdType[aCol[i].eCType-1], |
| 3178 | pTab->zName, aCol[i].zCnName); |
drh | 72532f5 | 2021-08-18 19:22:27 +0000 | [diff] [blame] | 3179 | rc = SQLITE_CONSTRAINT_DATATYPE; |
| 3180 | goto abort_due_to_error; |
| 3181 | } |
| 3182 | |
danielk1977 | 751de56 | 2008-04-18 09:01:15 +0000 | [diff] [blame] | 3183 | /* Opcode: Affinity P1 P2 * P4 * |
drh | f63552b | 2013-10-30 00:25:03 +0000 | [diff] [blame] | 3184 | ** Synopsis: affinity(r[P1@P2]) |
danielk1977 | 751de56 | 2008-04-18 09:01:15 +0000 | [diff] [blame] | 3185 | ** |
| 3186 | ** Apply affinities to a range of P2 registers starting with P1. |
| 3187 | ** |
drh | bb6783b | 2017-04-29 18:02:49 +0000 | [diff] [blame] | 3188 | ** P4 is a string that is P2 characters long. The N-th character of the |
| 3189 | ** string indicates the column affinity that should be used for the N-th |
danielk1977 | 751de56 | 2008-04-18 09:01:15 +0000 | [diff] [blame] | 3190 | ** memory cell in the range. |
| 3191 | */ |
| 3192 | case OP_Affinity: { |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 3193 | const char *zAffinity; /* The affinity to be applied */ |
danielk1977 | 751de56 | 2008-04-18 09:01:15 +0000 | [diff] [blame] | 3194 | |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3195 | zAffinity = pOp->p4.z; |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 3196 | assert( zAffinity!=0 ); |
drh | 662c50e | 2017-04-01 20:14:01 +0000 | [diff] [blame] | 3197 | assert( pOp->p2>0 ); |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 3198 | assert( zAffinity[pOp->p2]==0 ); |
| 3199 | pIn1 = &aMem[pOp->p1]; |
drh | 122c514 | 2019-07-29 05:23:01 +0000 | [diff] [blame] | 3200 | while( 1 /*exit-by-break*/ ){ |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 3201 | assert( pIn1 <= &p->aMem[(p->nMem+1 - p->nCursor)] ); |
drh | b5f6243 | 2019-12-10 02:48:41 +0000 | [diff] [blame] | 3202 | assert( zAffinity[0]==SQLITE_AFF_NONE || memIsValid(pIn1) ); |
drh | 83a1daf | 2019-05-01 18:59:33 +0000 | [diff] [blame] | 3203 | applyAffinity(pIn1, zAffinity[0], encoding); |
| 3204 | if( zAffinity[0]==SQLITE_AFF_REAL && (pIn1->flags & MEM_Int)!=0 ){ |
drh | 337cc39 | 2019-07-29 06:06:53 +0000 | [diff] [blame] | 3205 | /* When applying REAL affinity, if the result is still an MEM_Int |
| 3206 | ** that will fit in 6 bytes, then change the type to MEM_IntReal |
| 3207 | ** so that we keep the high-resolution integer value but know that |
| 3208 | ** the type really wants to be REAL. */ |
| 3209 | testcase( pIn1->u.i==140737488355328LL ); |
| 3210 | testcase( pIn1->u.i==140737488355327LL ); |
| 3211 | testcase( pIn1->u.i==-140737488355328LL ); |
| 3212 | testcase( pIn1->u.i==-140737488355329LL ); |
| 3213 | if( pIn1->u.i<=140737488355327LL && pIn1->u.i>=-140737488355328LL ){ |
| 3214 | pIn1->flags |= MEM_IntReal; |
| 3215 | pIn1->flags &= ~MEM_Int; |
| 3216 | }else{ |
| 3217 | pIn1->u.r = (double)pIn1->u.i; |
| 3218 | pIn1->flags |= MEM_Real; |
| 3219 | pIn1->flags &= ~MEM_Int; |
| 3220 | } |
drh | 83a1daf | 2019-05-01 18:59:33 +0000 | [diff] [blame] | 3221 | } |
drh | 6fcc1ec | 2019-05-01 14:41:47 +0000 | [diff] [blame] | 3222 | REGISTER_TRACE((int)(pIn1-aMem), pIn1); |
drh | 83a1daf | 2019-05-01 18:59:33 +0000 | [diff] [blame] | 3223 | zAffinity++; |
| 3224 | if( zAffinity[0]==0 ) break; |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 3225 | pIn1++; |
drh | 83a1daf | 2019-05-01 18:59:33 +0000 | [diff] [blame] | 3226 | } |
danielk1977 | 751de56 | 2008-04-18 09:01:15 +0000 | [diff] [blame] | 3227 | break; |
| 3228 | } |
| 3229 | |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 3230 | /* Opcode: MakeRecord P1 P2 P3 P4 * |
drh | f63552b | 2013-10-30 00:25:03 +0000 | [diff] [blame] | 3231 | ** Synopsis: r[P3]=mkrec(r[P1@P2]) |
drh | 7a224de | 2004-06-02 01:22:02 +0000 | [diff] [blame] | 3232 | ** |
drh | 710c484 | 2010-08-30 01:17:20 +0000 | [diff] [blame] | 3233 | ** Convert P2 registers beginning with P1 into the [record format] |
| 3234 | ** use as a data record in a database table or as a key |
| 3235 | ** in an index. The OP_Column opcode can decode the record later. |
drh | 7a224de | 2004-06-02 01:22:02 +0000 | [diff] [blame] | 3236 | ** |
drh | bb6783b | 2017-04-29 18:02:49 +0000 | [diff] [blame] | 3237 | ** P4 may be a string that is P2 characters long. The N-th character of the |
| 3238 | ** string indicates the column affinity that should be used for the N-th |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3239 | ** field of the index key. |
drh | 7a224de | 2004-06-02 01:22:02 +0000 | [diff] [blame] | 3240 | ** |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 3241 | ** The mapping from character to affinity is given by the SQLITE_AFF_ |
| 3242 | ** macros defined in sqliteInt.h. |
drh | 7a224de | 2004-06-02 01:22:02 +0000 | [diff] [blame] | 3243 | ** |
drh | 05883a3 | 2015-06-02 15:32:08 +0000 | [diff] [blame] | 3244 | ** If P4 is NULL then all index fields have the affinity BLOB. |
drh | da36933 | 2020-06-29 20:09:04 +0000 | [diff] [blame] | 3245 | ** |
| 3246 | ** The meaning of P5 depends on whether or not the SQLITE_ENABLE_NULL_TRIM |
| 3247 | ** compile-time option is enabled: |
| 3248 | ** |
| 3249 | ** * If SQLITE_ENABLE_NULL_TRIM is enabled, then the P5 is the index |
| 3250 | ** of the right-most table that can be null-trimmed. |
| 3251 | ** |
| 3252 | ** * If SQLITE_ENABLE_NULL_TRIM is omitted, then P5 has the value |
| 3253 | ** OPFLAG_NOCHNG_MAGIC if the OP_MakeRecord opcode is allowed to |
| 3254 | ** accept no-change records with serial_type 10. This value is |
| 3255 | ** only used inside an assert() and does not affect the end result. |
drh | 7f057c9 | 2005-06-24 03:53:06 +0000 | [diff] [blame] | 3256 | */ |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 3257 | case OP_MakeRecord: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3258 | Mem *pRec; /* The new record */ |
| 3259 | u64 nData; /* Number of bytes of data space */ |
| 3260 | int nHdr; /* Number of bytes of header space */ |
| 3261 | i64 nByte; /* Data space required for this record */ |
drh | 4a33507 | 2015-04-11 02:08:48 +0000 | [diff] [blame] | 3262 | i64 nZero; /* Number of zero bytes at the end of the record */ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3263 | int nVarint; /* Number of bytes in a varint */ |
| 3264 | u32 serial_type; /* Type field */ |
| 3265 | Mem *pData0; /* First field to be combined into the record */ |
| 3266 | Mem *pLast; /* Last field of the record */ |
| 3267 | int nField; /* Number of fields in the record */ |
| 3268 | char *zAffinity; /* The affinity string for the record */ |
drh | be37c12 | 2015-10-16 14:54:17 +0000 | [diff] [blame] | 3269 | u32 len; /* Length of a field */ |
drh | b70b0df | 2019-04-30 01:08:42 +0000 | [diff] [blame] | 3270 | u8 *zHdr; /* Where to write next byte of the header */ |
| 3271 | u8 *zPayload; /* Where to write next byte of the payload */ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3272 | |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 3273 | /* Assuming the record contains N fields, the record format looks |
| 3274 | ** like this: |
| 3275 | ** |
drh | 7a224de | 2004-06-02 01:22:02 +0000 | [diff] [blame] | 3276 | ** ------------------------------------------------------------------------ |
| 3277 | ** | hdr-size | type 0 | type 1 | ... | type N-1 | data0 | ... | data N-1 | |
| 3278 | ** ------------------------------------------------------------------------ |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 3279 | ** |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3280 | ** 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] | 3281 | ** and so forth. |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 3282 | ** |
| 3283 | ** Each type field is a varint representing the serial type of the |
| 3284 | ** corresponding data element (see sqlite3VdbeSerialType()). The |
drh | 7a224de | 2004-06-02 01:22:02 +0000 | [diff] [blame] | 3285 | ** hdr-size field is also a varint which is the offset from the beginning |
| 3286 | ** of the record to data0. |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 3287 | */ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3288 | nData = 0; /* Number of bytes of data space */ |
| 3289 | nHdr = 0; /* Number of bytes of header space */ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3290 | nZero = 0; /* Number of zero bytes at the end of the record */ |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 3291 | nField = pOp->p1; |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 3292 | zAffinity = pOp->p4.z; |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 3293 | 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] | 3294 | pData0 = &aMem[nField]; |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 3295 | nField = pOp->p2; |
| 3296 | pLast = &pData0[nField-1]; |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 3297 | |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 3298 | /* Identify the output register */ |
| 3299 | assert( pOp->p3<pOp->p1 || pOp->p3>=pOp->p1+pOp->p2 ); |
| 3300 | pOut = &aMem[pOp->p3]; |
| 3301 | memAboutToChange(p, pOut); |
| 3302 | |
drh | 3e6c060 | 2013-12-10 20:53:01 +0000 | [diff] [blame] | 3303 | /* Apply the requested affinity to all inputs |
| 3304 | */ |
| 3305 | assert( pData0<=pLast ); |
| 3306 | if( zAffinity ){ |
| 3307 | pRec = pData0; |
| 3308 | do{ |
drh | 5ad1251 | 2019-05-09 16:22:51 +0000 | [diff] [blame] | 3309 | applyAffinity(pRec, zAffinity[0], encoding); |
dan | be81262 | 2019-05-17 15:59:11 +0000 | [diff] [blame] | 3310 | if( zAffinity[0]==SQLITE_AFF_REAL && (pRec->flags & MEM_Int) ){ |
| 3311 | pRec->flags |= MEM_IntReal; |
| 3312 | pRec->flags &= ~(MEM_Int); |
| 3313 | } |
drh | 5ad1251 | 2019-05-09 16:22:51 +0000 | [diff] [blame] | 3314 | REGISTER_TRACE((int)(pRec-aMem), pRec); |
| 3315 | zAffinity++; |
| 3316 | pRec++; |
drh | 57bf4a8 | 2014-02-17 14:59:22 +0000 | [diff] [blame] | 3317 | assert( zAffinity[0]==0 || pRec<=pLast ); |
| 3318 | }while( zAffinity[0] ); |
drh | 3e6c060 | 2013-12-10 20:53:01 +0000 | [diff] [blame] | 3319 | } |
| 3320 | |
drh | d447dce | 2017-01-25 20:55:11 +0000 | [diff] [blame] | 3321 | #ifdef SQLITE_ENABLE_NULL_TRIM |
drh | 585ce19 | 2017-01-25 14:58:27 +0000 | [diff] [blame] | 3322 | /* NULLs can be safely trimmed from the end of the record, as long as |
| 3323 | ** as the schema format is 2 or more and none of the omitted columns |
| 3324 | ** have a non-NULL default value. Also, the record must be left with |
| 3325 | ** at least one field. If P5>0 then it will be one more than the |
| 3326 | ** index of the right-most column with a non-NULL default value */ |
| 3327 | if( pOp->p5 ){ |
| 3328 | while( (pLast->flags & MEM_Null)!=0 && nField>pOp->p5 ){ |
| 3329 | pLast--; |
| 3330 | nField--; |
| 3331 | } |
| 3332 | } |
drh | d447dce | 2017-01-25 20:55:11 +0000 | [diff] [blame] | 3333 | #endif |
drh | 585ce19 | 2017-01-25 14:58:27 +0000 | [diff] [blame] | 3334 | |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 3335 | /* Loop through the elements that will make up the record to figure |
drh | 76fd7be | 2019-07-11 19:50:18 +0000 | [diff] [blame] | 3336 | ** out how much space is required for the new record. After this loop, |
| 3337 | ** the Mem.uTemp field of each term should hold the serial-type that will |
| 3338 | ** be used for that term in the generated record: |
| 3339 | ** |
| 3340 | ** Mem.uTemp value type |
| 3341 | ** --------------- --------------- |
| 3342 | ** 0 NULL |
| 3343 | ** 1 1-byte signed integer |
| 3344 | ** 2 2-byte signed integer |
| 3345 | ** 3 3-byte signed integer |
| 3346 | ** 4 4-byte signed integer |
| 3347 | ** 5 6-byte signed integer |
| 3348 | ** 6 8-byte signed integer |
| 3349 | ** 7 IEEE float |
| 3350 | ** 8 Integer constant 0 |
| 3351 | ** 9 Integer constant 1 |
| 3352 | ** 10,11 reserved for expansion |
| 3353 | ** N>=12 and even BLOB |
| 3354 | ** N>=13 and odd text |
| 3355 | ** |
| 3356 | ** The following additional values are computed: |
| 3357 | ** nHdr Number of bytes needed for the record header |
| 3358 | ** nData Number of bytes of data space needed for the record |
| 3359 | ** nZero Zero bytes at the end of the record |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 3360 | */ |
drh | 038b7bc | 2013-12-09 23:17:22 +0000 | [diff] [blame] | 3361 | pRec = pLast; |
drh | 59bf00c | 2013-12-08 23:33:28 +0000 | [diff] [blame] | 3362 | do{ |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 3363 | assert( memIsValid(pRec) ); |
drh | c1da439 | 2019-07-11 19:22:36 +0000 | [diff] [blame] | 3364 | if( pRec->flags & MEM_Null ){ |
| 3365 | if( pRec->flags & MEM_Zero ){ |
drh | 41fb367 | 2018-01-12 23:18:38 +0000 | [diff] [blame] | 3366 | /* Values with MEM_Null and MEM_Zero are created by xColumn virtual |
| 3367 | ** table methods that never invoke sqlite3_result_xxxxx() while |
| 3368 | ** computing an unchanging column value in an UPDATE statement. |
| 3369 | ** Give such values a special internal-use-only serial-type of 10 |
| 3370 | ** so that they can be passed through to xUpdate and have |
| 3371 | ** a true sqlite3_value_nochange(). */ |
drh | da36933 | 2020-06-29 20:09:04 +0000 | [diff] [blame] | 3372 | #ifndef SQLITE_ENABLE_NULL_TRIM |
drh | 41fb367 | 2018-01-12 23:18:38 +0000 | [diff] [blame] | 3373 | assert( pOp->p5==OPFLAG_NOCHNG_MAGIC || CORRUPT_DB ); |
drh | da36933 | 2020-06-29 20:09:04 +0000 | [diff] [blame] | 3374 | #endif |
drh | c1da439 | 2019-07-11 19:22:36 +0000 | [diff] [blame] | 3375 | pRec->uTemp = 10; |
drh | 038b7bc | 2013-12-09 23:17:22 +0000 | [diff] [blame] | 3376 | }else{ |
drh | 76fd7be | 2019-07-11 19:50:18 +0000 | [diff] [blame] | 3377 | pRec->uTemp = 0; |
drh | 038b7bc | 2013-12-09 23:17:22 +0000 | [diff] [blame] | 3378 | } |
drh | c1da439 | 2019-07-11 19:22:36 +0000 | [diff] [blame] | 3379 | nHdr++; |
| 3380 | }else if( pRec->flags & (MEM_Int|MEM_IntReal) ){ |
| 3381 | /* Figure out whether to use 1, 2, 4, 6 or 8 bytes. */ |
| 3382 | i64 i = pRec->u.i; |
drh | 9c3bb59 | 2019-07-30 21:00:13 +0000 | [diff] [blame] | 3383 | u64 uu; |
drh | c1da439 | 2019-07-11 19:22:36 +0000 | [diff] [blame] | 3384 | testcase( pRec->flags & MEM_Int ); |
| 3385 | testcase( pRec->flags & MEM_IntReal ); |
| 3386 | if( i<0 ){ |
drh | 9c3bb59 | 2019-07-30 21:00:13 +0000 | [diff] [blame] | 3387 | uu = ~i; |
drh | c1da439 | 2019-07-11 19:22:36 +0000 | [diff] [blame] | 3388 | }else{ |
drh | 9c3bb59 | 2019-07-30 21:00:13 +0000 | [diff] [blame] | 3389 | uu = i; |
drh | c1da439 | 2019-07-11 19:22:36 +0000 | [diff] [blame] | 3390 | } |
| 3391 | nHdr++; |
drh | 9c3bb59 | 2019-07-30 21:00:13 +0000 | [diff] [blame] | 3392 | testcase( uu==127 ); testcase( uu==128 ); |
| 3393 | testcase( uu==32767 ); testcase( uu==32768 ); |
| 3394 | testcase( uu==8388607 ); testcase( uu==8388608 ); |
drh | 16f56e8 | 2022-02-21 14:30:59 +0000 | [diff] [blame] | 3395 | testcase( uu==2147483647 ); testcase( uu==2147483648LL ); |
drh | 9c3bb59 | 2019-07-30 21:00:13 +0000 | [diff] [blame] | 3396 | testcase( uu==140737488355327LL ); testcase( uu==140737488355328LL ); |
| 3397 | if( uu<=127 ){ |
drh | a0318fd | 2022-02-26 23:01:25 +0000 | [diff] [blame] | 3398 | if( (i&1)==i && p->minWriteFileFormat>=4 ){ |
drh | 9c3bb59 | 2019-07-30 21:00:13 +0000 | [diff] [blame] | 3399 | pRec->uTemp = 8+(u32)uu; |
drh | c1da439 | 2019-07-11 19:22:36 +0000 | [diff] [blame] | 3400 | }else{ |
| 3401 | nData++; |
| 3402 | pRec->uTemp = 1; |
| 3403 | } |
drh | 9c3bb59 | 2019-07-30 21:00:13 +0000 | [diff] [blame] | 3404 | }else if( uu<=32767 ){ |
drh | c1da439 | 2019-07-11 19:22:36 +0000 | [diff] [blame] | 3405 | nData += 2; |
| 3406 | pRec->uTemp = 2; |
drh | 9c3bb59 | 2019-07-30 21:00:13 +0000 | [diff] [blame] | 3407 | }else if( uu<=8388607 ){ |
drh | c1da439 | 2019-07-11 19:22:36 +0000 | [diff] [blame] | 3408 | nData += 3; |
| 3409 | pRec->uTemp = 3; |
drh | 9c3bb59 | 2019-07-30 21:00:13 +0000 | [diff] [blame] | 3410 | }else if( uu<=2147483647 ){ |
drh | c1da439 | 2019-07-11 19:22:36 +0000 | [diff] [blame] | 3411 | nData += 4; |
| 3412 | pRec->uTemp = 4; |
drh | 9c3bb59 | 2019-07-30 21:00:13 +0000 | [diff] [blame] | 3413 | }else if( uu<=140737488355327LL ){ |
drh | c1da439 | 2019-07-11 19:22:36 +0000 | [diff] [blame] | 3414 | nData += 6; |
| 3415 | pRec->uTemp = 5; |
| 3416 | }else{ |
| 3417 | nData += 8; |
| 3418 | if( pRec->flags & MEM_IntReal ){ |
| 3419 | /* If the value is IntReal and is going to take up 8 bytes to store |
| 3420 | ** as an integer, then we might as well make it an 8-byte floating |
| 3421 | ** point value */ |
| 3422 | pRec->u.r = (double)pRec->u.i; |
| 3423 | pRec->flags &= ~MEM_IntReal; |
| 3424 | pRec->flags |= MEM_Real; |
| 3425 | pRec->uTemp = 7; |
| 3426 | }else{ |
| 3427 | pRec->uTemp = 6; |
| 3428 | } |
| 3429 | } |
| 3430 | }else if( pRec->flags & MEM_Real ){ |
| 3431 | nHdr++; |
| 3432 | nData += 8; |
| 3433 | pRec->uTemp = 7; |
| 3434 | }else{ |
| 3435 | assert( db->mallocFailed || pRec->flags&(MEM_Str|MEM_Blob) ); |
| 3436 | assert( pRec->n>=0 ); |
| 3437 | len = (u32)pRec->n; |
| 3438 | serial_type = (len*2) + 12 + ((pRec->flags & MEM_Str)!=0); |
| 3439 | if( pRec->flags & MEM_Zero ){ |
| 3440 | serial_type += pRec->u.nZero*2; |
| 3441 | if( nData ){ |
| 3442 | if( sqlite3VdbeMemExpandBlob(pRec) ) goto no_mem; |
| 3443 | len += pRec->u.nZero; |
| 3444 | }else{ |
| 3445 | nZero += pRec->u.nZero; |
| 3446 | } |
| 3447 | } |
| 3448 | nData += len; |
| 3449 | nHdr += sqlite3VarintLen(serial_type); |
| 3450 | pRec->uTemp = serial_type; |
drh | fdf972a | 2007-05-02 13:30:27 +0000 | [diff] [blame] | 3451 | } |
drh | 45c3c66 | 2016-04-07 14:16:16 +0000 | [diff] [blame] | 3452 | if( pRec==pData0 ) break; |
| 3453 | pRec--; |
| 3454 | }while(1); |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 3455 | |
drh | 654858d | 2014-11-20 02:18:14 +0000 | [diff] [blame] | 3456 | /* EVIDENCE-OF: R-22564-11647 The header begins with a single varint |
| 3457 | ** which determines the total number of bytes in the header. The varint |
| 3458 | ** value is the size of the header in bytes including the size varint |
| 3459 | ** itself. */ |
drh | 59bf00c | 2013-12-08 23:33:28 +0000 | [diff] [blame] | 3460 | testcase( nHdr==126 ); |
| 3461 | testcase( nHdr==127 ); |
drh | 2a24287 | 2013-12-08 22:59:29 +0000 | [diff] [blame] | 3462 | if( nHdr<=126 ){ |
| 3463 | /* The common case */ |
| 3464 | nHdr += 1; |
| 3465 | }else{ |
| 3466 | /* Rare case of a really large header */ |
| 3467 | nVarint = sqlite3VarintLen(nHdr); |
| 3468 | nHdr += nVarint; |
| 3469 | if( nVarint<sqlite3VarintLen(nHdr) ) nHdr++; |
drh | cb9882a | 2005-03-17 03:15:40 +0000 | [diff] [blame] | 3470 | } |
drh | 038b7bc | 2013-12-09 23:17:22 +0000 | [diff] [blame] | 3471 | nByte = nHdr+nData; |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 3472 | |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 3473 | /* Make sure the output register has a buffer large enough to store |
| 3474 | ** the new record. The output register (pOp->p3) is not allowed to |
| 3475 | ** be one of the input registers (because the following call to |
drh | 322f285 | 2014-09-19 00:43:39 +0000 | [diff] [blame] | 3476 | ** sqlite3VdbeMemClearAndResize() could clobber the value before it is used). |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 3477 | */ |
drh | 0d7f0cc | 2018-09-21 13:07:14 +0000 | [diff] [blame] | 3478 | if( nByte+nZero<=pOut->szMalloc ){ |
| 3479 | /* The output register is already large enough to hold the record. |
| 3480 | ** No error checks or buffer enlargement is required */ |
| 3481 | pOut->z = pOut->zMalloc; |
| 3482 | }else{ |
| 3483 | /* Need to make sure that the output is not too big and then enlarge |
| 3484 | ** the output register to hold the full result */ |
| 3485 | if( nByte+nZero>db->aLimit[SQLITE_LIMIT_LENGTH] ){ |
| 3486 | goto too_big; |
| 3487 | } |
| 3488 | if( sqlite3VdbeMemClearAndResize(pOut, (int)nByte) ){ |
| 3489 | goto no_mem; |
| 3490 | } |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 3491 | } |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 3492 | pOut->n = (int)nByte; |
drh | c91b2fd | 2014-03-01 18:13:23 +0000 | [diff] [blame] | 3493 | pOut->flags = MEM_Blob; |
drh | fdf972a | 2007-05-02 13:30:27 +0000 | [diff] [blame] | 3494 | if( nZero ){ |
drh | 8df3284 | 2008-12-09 02:51:23 +0000 | [diff] [blame] | 3495 | pOut->u.nZero = nZero; |
drh | 477df4b | 2008-01-05 18:48:24 +0000 | [diff] [blame] | 3496 | pOut->flags |= MEM_Zero; |
drh | fdf972a | 2007-05-02 13:30:27 +0000 | [diff] [blame] | 3497 | } |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 3498 | UPDATE_MAX_BLOBSIZE(pOut); |
drh | b70b0df | 2019-04-30 01:08:42 +0000 | [diff] [blame] | 3499 | zHdr = (u8 *)pOut->z; |
| 3500 | zPayload = zHdr + nHdr; |
| 3501 | |
| 3502 | /* Write the record */ |
drh | b47b1f6 | 2022-04-01 21:01:37 +0000 | [diff] [blame] | 3503 | if( nHdr<0x80 ){ |
| 3504 | *(zHdr++) = nHdr; |
| 3505 | }else{ |
| 3506 | zHdr += sqlite3PutVarint(zHdr,nHdr); |
| 3507 | } |
drh | b70b0df | 2019-04-30 01:08:42 +0000 | [diff] [blame] | 3508 | assert( pData0<=pLast ); |
| 3509 | pRec = pData0; |
drh | 759e507 | 2022-04-01 20:39:40 +0000 | [diff] [blame] | 3510 | while( 1 /*exit-by-break*/ ){ |
drh | b70b0df | 2019-04-30 01:08:42 +0000 | [diff] [blame] | 3511 | serial_type = pRec->uTemp; |
| 3512 | /* EVIDENCE-OF: R-06529-47362 Following the size varint are one or more |
drh | d859dc2 | 2022-04-02 14:30:58 +0000 | [diff] [blame] | 3513 | ** additional varints, one per column. |
| 3514 | ** EVIDENCE-OF: R-64536-51728 The values for each column in the record |
| 3515 | ** immediately follow the header. */ |
| 3516 | if( serial_type<=7 ){ |
drh | b47b1f6 | 2022-04-01 21:01:37 +0000 | [diff] [blame] | 3517 | *(zHdr++) = serial_type; |
drh | d859dc2 | 2022-04-02 14:30:58 +0000 | [diff] [blame] | 3518 | if( serial_type==0 ){ |
| 3519 | /* NULL value. No change in zPayload */ |
| 3520 | }else{ |
| 3521 | u64 v; |
drh | 1f416db | 2022-04-02 20:08:48 +0000 | [diff] [blame] | 3522 | u32 i; |
drh | d859dc2 | 2022-04-02 14:30:58 +0000 | [diff] [blame] | 3523 | if( serial_type==7 ){ |
| 3524 | assert( sizeof(v)==sizeof(pRec->u.r) ); |
| 3525 | memcpy(&v, &pRec->u.r, sizeof(v)); |
| 3526 | swapMixedEndianFloat(v); |
| 3527 | }else{ |
| 3528 | v = pRec->u.i; |
| 3529 | } |
| 3530 | len = i = sqlite3SmallTypeSizes[serial_type]; |
| 3531 | assert( i>0 ); |
drh | 2c144b0 | 2022-04-02 15:19:02 +0000 | [diff] [blame] | 3532 | while( 1 /*exit-by-break*/ ){ |
drh | d859dc2 | 2022-04-02 14:30:58 +0000 | [diff] [blame] | 3533 | zPayload[--i] = (u8)(v&0xFF); |
drh | 2c144b0 | 2022-04-02 15:19:02 +0000 | [diff] [blame] | 3534 | if( i==0 ) break; |
drh | d859dc2 | 2022-04-02 14:30:58 +0000 | [diff] [blame] | 3535 | v >>= 8; |
drh | 2c144b0 | 2022-04-02 15:19:02 +0000 | [diff] [blame] | 3536 | } |
drh | d859dc2 | 2022-04-02 14:30:58 +0000 | [diff] [blame] | 3537 | zPayload += len; |
| 3538 | } |
| 3539 | }else if( serial_type<0x80 ){ |
| 3540 | *(zHdr++) = serial_type; |
drh | d13527d | 2022-04-02 19:21:58 +0000 | [diff] [blame] | 3541 | if( serial_type>=14 && pRec->n>0 ){ |
| 3542 | assert( pRec->z!=0 ); |
drh | d859dc2 | 2022-04-02 14:30:58 +0000 | [diff] [blame] | 3543 | memcpy(zPayload, pRec->z, pRec->n); |
| 3544 | zPayload += pRec->n; |
| 3545 | } |
drh | b47b1f6 | 2022-04-01 21:01:37 +0000 | [diff] [blame] | 3546 | }else{ |
| 3547 | zHdr += sqlite3PutVarint(zHdr, serial_type); |
drh | d13527d | 2022-04-02 19:21:58 +0000 | [diff] [blame] | 3548 | if( pRec->n ){ |
| 3549 | assert( pRec->z!=0 ); |
| 3550 | memcpy(zPayload, pRec->z, pRec->n); |
| 3551 | zPayload += pRec->n; |
| 3552 | } |
drh | b47b1f6 | 2022-04-01 21:01:37 +0000 | [diff] [blame] | 3553 | } |
drh | 759e507 | 2022-04-01 20:39:40 +0000 | [diff] [blame] | 3554 | if( pRec==pLast ) break; |
| 3555 | pRec++; |
| 3556 | } |
drh | b70b0df | 2019-04-30 01:08:42 +0000 | [diff] [blame] | 3557 | assert( nHdr==(int)(zHdr - (u8*)pOut->z) ); |
| 3558 | assert( nByte==(int)(zPayload - (u8*)pOut->z) ); |
| 3559 | |
| 3560 | assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) ); |
| 3561 | REGISTER_TRACE(pOp->p3, pOut); |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 3562 | break; |
| 3563 | } |
| 3564 | |
drh | 50fb7e0 | 2021-12-06 20:16:53 +0000 | [diff] [blame] | 3565 | /* Opcode: Count P1 P2 P3 * * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 3566 | ** Synopsis: r[P2]=count() |
danielk1977 | a553316 | 2009-02-24 10:01:51 +0000 | [diff] [blame] | 3567 | ** |
| 3568 | ** Store the number of entries (an integer value) in the table or index |
drh | 9f27463 | 2020-03-17 17:11:23 +0000 | [diff] [blame] | 3569 | ** opened by cursor P1 in register P2. |
| 3570 | ** |
| 3571 | ** If P3==0, then an exact count is obtained, which involves visiting |
| 3572 | ** every btree page of the table. But if P3 is non-zero, an estimate |
| 3573 | ** is returned based on the current cursor position. |
danielk1977 | a553316 | 2009-02-24 10:01:51 +0000 | [diff] [blame] | 3574 | */ |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 3575 | case OP_Count: { /* out2 */ |
danielk1977 | a553316 | 2009-02-24 10:01:51 +0000 | [diff] [blame] | 3576 | i64 nEntry; |
drh | c54a617 | 2009-06-02 16:06:03 +0000 | [diff] [blame] | 3577 | BtCursor *pCrsr; |
| 3578 | |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 3579 | assert( p->apCsr[pOp->p1]->eCurType==CURTYPE_BTREE ); |
| 3580 | pCrsr = p->apCsr[pOp->p1]->uc.pCursor; |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 3581 | assert( pCrsr ); |
drh | 9f27463 | 2020-03-17 17:11:23 +0000 | [diff] [blame] | 3582 | if( pOp->p3 ){ |
| 3583 | nEntry = sqlite3BtreeRowCountEst(pCrsr); |
| 3584 | }else{ |
| 3585 | nEntry = 0; /* Not needed. Only used to silence a warning. */ |
| 3586 | rc = sqlite3BtreeCount(db, pCrsr, &nEntry); |
| 3587 | if( rc ) goto abort_due_to_error; |
| 3588 | } |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 3589 | pOut = out2Prerelease(p, pOp); |
danielk1977 | a553316 | 2009-02-24 10:01:51 +0000 | [diff] [blame] | 3590 | pOut->u.i = nEntry; |
drh | 21f6daa | 2019-10-11 14:21:48 +0000 | [diff] [blame] | 3591 | goto check_for_interrupt; |
danielk1977 | a553316 | 2009-02-24 10:01:51 +0000 | [diff] [blame] | 3592 | } |
danielk1977 | a553316 | 2009-02-24 10:01:51 +0000 | [diff] [blame] | 3593 | |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3594 | /* Opcode: Savepoint P1 * * P4 * |
| 3595 | ** |
| 3596 | ** Open, release or rollback the savepoint named by parameter P4, depending |
drh | 2ce9b6b | 2019-05-10 14:03:07 +0000 | [diff] [blame] | 3597 | ** on the value of P1. To open a new savepoint set P1==0 (SAVEPOINT_BEGIN). |
| 3598 | ** To release (commit) an existing savepoint set P1==1 (SAVEPOINT_RELEASE). |
| 3599 | ** To rollback an existing savepoint set P1==2 (SAVEPOINT_ROLLBACK). |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3600 | */ |
| 3601 | case OP_Savepoint: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3602 | int p1; /* Value of P1 operand */ |
| 3603 | char *zName; /* Name of savepoint */ |
| 3604 | int nName; |
| 3605 | Savepoint *pNew; |
| 3606 | Savepoint *pSavepoint; |
| 3607 | Savepoint *pTmp; |
| 3608 | int iSavepoint; |
| 3609 | int ii; |
| 3610 | |
| 3611 | p1 = pOp->p1; |
| 3612 | zName = pOp->p4.z; |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3613 | |
| 3614 | /* Assert that the p1 parameter is valid. Also that if there is no open |
| 3615 | ** transaction, then there cannot be any savepoints. |
| 3616 | */ |
| 3617 | assert( db->pSavepoint==0 || db->autoCommit==0 ); |
| 3618 | assert( p1==SAVEPOINT_BEGIN||p1==SAVEPOINT_RELEASE||p1==SAVEPOINT_ROLLBACK ); |
| 3619 | assert( db->pSavepoint || db->isTransactionSavepoint==0 ); |
| 3620 | assert( checkSavepointCount(db) ); |
dan | c0537fe | 2013-06-28 19:41:43 +0000 | [diff] [blame] | 3621 | assert( p->bIsReader ); |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3622 | |
| 3623 | if( p1==SAVEPOINT_BEGIN ){ |
drh | 4f7d3a5 | 2013-06-27 23:54:02 +0000 | [diff] [blame] | 3624 | if( db->nVdbeWrite>0 ){ |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3625 | /* A new savepoint cannot be created if there are active write |
| 3626 | ** statements (i.e. open read/write incremental blob handles). |
| 3627 | */ |
drh | 22c17b8 | 2015-05-15 04:13:15 +0000 | [diff] [blame] | 3628 | sqlite3VdbeError(p, "cannot open savepoint - SQL statements in progress"); |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3629 | rc = SQLITE_BUSY; |
| 3630 | }else{ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3631 | nName = sqlite3Strlen30(zName); |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3632 | |
drh | be07ec5 | 2011-06-03 12:15:26 +0000 | [diff] [blame] | 3633 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
dan | d9495cd | 2011-04-27 12:08:04 +0000 | [diff] [blame] | 3634 | /* This call is Ok even if this savepoint is actually a transaction |
| 3635 | ** savepoint (and therefore should not prompt xSavepoint()) callbacks. |
| 3636 | ** If this is a transaction savepoint being opened, it is guaranteed |
| 3637 | ** that the db->aVTrans[] array is empty. */ |
| 3638 | assert( db->autoCommit==0 || db->nVTrans==0 ); |
drh | a24bc9c | 2011-05-24 00:35:56 +0000 | [diff] [blame] | 3639 | rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN, |
| 3640 | db->nStatement+db->nSavepoint); |
dan | d9495cd | 2011-04-27 12:08:04 +0000 | [diff] [blame] | 3641 | if( rc!=SQLITE_OK ) goto abort_due_to_error; |
drh | 305ebab | 2011-05-26 14:19:14 +0000 | [diff] [blame] | 3642 | #endif |
dan | d9495cd | 2011-04-27 12:08:04 +0000 | [diff] [blame] | 3643 | |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3644 | /* Create a new savepoint structure. */ |
drh | 575fad6 | 2016-02-05 13:38:36 +0000 | [diff] [blame] | 3645 | pNew = sqlite3DbMallocRawNN(db, sizeof(Savepoint)+nName+1); |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3646 | if( pNew ){ |
| 3647 | pNew->zName = (char *)&pNew[1]; |
| 3648 | memcpy(pNew->zName, zName, nName+1); |
| 3649 | |
| 3650 | /* If there is no open transaction, then mark this as a special |
| 3651 | ** "transaction savepoint". */ |
| 3652 | if( db->autoCommit ){ |
| 3653 | db->autoCommit = 0; |
| 3654 | db->isTransactionSavepoint = 1; |
| 3655 | }else{ |
| 3656 | db->nSavepoint++; |
danielk1977 | d829335 | 2009-04-30 09:10:37 +0000 | [diff] [blame] | 3657 | } |
dan | 21e8d01 | 2011-03-03 20:05:59 +0000 | [diff] [blame] | 3658 | |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3659 | /* Link the new savepoint into the database handle's list. */ |
| 3660 | pNew->pNext = db->pSavepoint; |
| 3661 | db->pSavepoint = pNew; |
dan | ba9108b | 2009-09-22 07:13:42 +0000 | [diff] [blame] | 3662 | pNew->nDeferredCons = db->nDeferredCons; |
dan | cb3e4b7 | 2013-07-03 19:53:05 +0000 | [diff] [blame] | 3663 | pNew->nDeferredImmCons = db->nDeferredImmCons; |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3664 | } |
| 3665 | } |
| 3666 | }else{ |
drh | 2ce9b6b | 2019-05-10 14:03:07 +0000 | [diff] [blame] | 3667 | assert( p1==SAVEPOINT_RELEASE || p1==SAVEPOINT_ROLLBACK ); |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3668 | iSavepoint = 0; |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3669 | |
| 3670 | /* Find the named savepoint. If there is no such savepoint, then an |
| 3671 | ** an error is returned to the user. */ |
| 3672 | for( |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3673 | pSavepoint = db->pSavepoint; |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3674 | pSavepoint && sqlite3StrICmp(pSavepoint->zName, zName); |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3675 | pSavepoint = pSavepoint->pNext |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3676 | ){ |
| 3677 | iSavepoint++; |
| 3678 | } |
| 3679 | if( !pSavepoint ){ |
drh | 22c17b8 | 2015-05-15 04:13:15 +0000 | [diff] [blame] | 3680 | sqlite3VdbeError(p, "no such savepoint: %s", zName); |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3681 | rc = SQLITE_ERROR; |
drh | 4f7d3a5 | 2013-06-27 23:54:02 +0000 | [diff] [blame] | 3682 | }else if( db->nVdbeWrite>0 && p1==SAVEPOINT_RELEASE ){ |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3683 | /* It is not possible to release (commit) a savepoint if there are |
drh | 0f198a7 | 2012-02-13 16:43:16 +0000 | [diff] [blame] | 3684 | ** active write statements. |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3685 | */ |
drh | 22c17b8 | 2015-05-15 04:13:15 +0000 | [diff] [blame] | 3686 | sqlite3VdbeError(p, "cannot release savepoint - " |
| 3687 | "SQL statements in progress"); |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3688 | rc = SQLITE_BUSY; |
| 3689 | }else{ |
| 3690 | |
| 3691 | /* Determine whether or not this is a transaction savepoint. If so, |
danielk1977 | 34cf35d | 2008-12-18 18:31:38 +0000 | [diff] [blame] | 3692 | ** and this is a RELEASE command, then the current transaction |
| 3693 | ** is committed. |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3694 | */ |
| 3695 | int isTransaction = pSavepoint->pNext==0 && db->isTransactionSavepoint; |
| 3696 | if( isTransaction && p1==SAVEPOINT_RELEASE ){ |
dan | 32b09f2 | 2009-09-23 17:29:59 +0000 | [diff] [blame] | 3697 | if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){ |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 3698 | goto vdbe_return; |
| 3699 | } |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3700 | db->autoCommit = 1; |
| 3701 | if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){ |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 3702 | p->pc = (int)(pOp - aOp); |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3703 | db->autoCommit = 0; |
| 3704 | p->rc = rc = SQLITE_BUSY; |
| 3705 | goto vdbe_return; |
| 3706 | } |
danielk1977 | 34cf35d | 2008-12-18 18:31:38 +0000 | [diff] [blame] | 3707 | rc = p->rc; |
drh | 94649b6 | 2019-12-18 02:12:04 +0000 | [diff] [blame] | 3708 | if( rc ){ |
| 3709 | db->autoCommit = 0; |
| 3710 | }else{ |
| 3711 | db->isTransactionSavepoint = 0; |
| 3712 | } |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3713 | }else{ |
drh | 47b7fc7 | 2014-11-11 01:33:57 +0000 | [diff] [blame] | 3714 | int isSchemaChange; |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3715 | iSavepoint = db->nSavepoint - iSavepoint - 1; |
drh | 31f1005 | 2012-03-31 17:17:26 +0000 | [diff] [blame] | 3716 | if( p1==SAVEPOINT_ROLLBACK ){ |
drh | 8257aa8 | 2017-07-26 19:59:13 +0000 | [diff] [blame] | 3717 | isSchemaChange = (db->mDbFlags & DBFLAG_SchemaChange)!=0; |
drh | 31f1005 | 2012-03-31 17:17:26 +0000 | [diff] [blame] | 3718 | for(ii=0; ii<db->nDb; ii++){ |
drh | 77b1dee | 2014-11-17 17:13:06 +0000 | [diff] [blame] | 3719 | rc = sqlite3BtreeTripAllCursors(db->aDb[ii].pBt, |
| 3720 | SQLITE_ABORT_ROLLBACK, |
drh | 47b7fc7 | 2014-11-11 01:33:57 +0000 | [diff] [blame] | 3721 | isSchemaChange==0); |
dan | 8023104 | 2014-11-12 14:56:02 +0000 | [diff] [blame] | 3722 | if( rc!=SQLITE_OK ) goto abort_due_to_error; |
drh | 31f1005 | 2012-03-31 17:17:26 +0000 | [diff] [blame] | 3723 | } |
drh | 47b7fc7 | 2014-11-11 01:33:57 +0000 | [diff] [blame] | 3724 | }else{ |
drh | 2ce9b6b | 2019-05-10 14:03:07 +0000 | [diff] [blame] | 3725 | assert( p1==SAVEPOINT_RELEASE ); |
drh | 47b7fc7 | 2014-11-11 01:33:57 +0000 | [diff] [blame] | 3726 | isSchemaChange = 0; |
drh | 0f198a7 | 2012-02-13 16:43:16 +0000 | [diff] [blame] | 3727 | } |
| 3728 | for(ii=0; ii<db->nDb; ii++){ |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3729 | rc = sqlite3BtreeSavepoint(db->aDb[ii].pBt, p1, iSavepoint); |
| 3730 | if( rc!=SQLITE_OK ){ |
| 3731 | goto abort_due_to_error; |
danielk1977 | bd43455 | 2009-03-18 10:33:00 +0000 | [diff] [blame] | 3732 | } |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3733 | } |
drh | 47b7fc7 | 2014-11-11 01:33:57 +0000 | [diff] [blame] | 3734 | if( isSchemaChange ){ |
drh | ba968db | 2018-07-24 22:02:12 +0000 | [diff] [blame] | 3735 | sqlite3ExpirePreparedStatements(db, 0); |
drh | 81028a4 | 2012-05-15 18:28:27 +0000 | [diff] [blame] | 3736 | sqlite3ResetAllSchemasOfConnection(db); |
drh | 8257aa8 | 2017-07-26 19:59:13 +0000 | [diff] [blame] | 3737 | db->mDbFlags |= DBFLAG_SchemaChange; |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3738 | } |
| 3739 | } |
drh | 95866af | 2019-12-15 00:36:33 +0000 | [diff] [blame] | 3740 | if( rc ) goto abort_due_to_error; |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3741 | |
| 3742 | /* Regardless of whether this is a RELEASE or ROLLBACK, destroy all |
| 3743 | ** savepoints nested inside of the savepoint being operated on. */ |
| 3744 | while( db->pSavepoint!=pSavepoint ){ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3745 | pTmp = db->pSavepoint; |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3746 | db->pSavepoint = pTmp->pNext; |
| 3747 | sqlite3DbFree(db, pTmp); |
| 3748 | db->nSavepoint--; |
| 3749 | } |
| 3750 | |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 3751 | /* If it is a RELEASE, then destroy the savepoint being operated on |
| 3752 | ** too. If it is a ROLLBACK TO, then set the number of deferred |
| 3753 | ** constraint violations present in the database to the value stored |
| 3754 | ** when the savepoint was created. */ |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3755 | if( p1==SAVEPOINT_RELEASE ){ |
| 3756 | assert( pSavepoint==db->pSavepoint ); |
| 3757 | db->pSavepoint = pSavepoint->pNext; |
| 3758 | sqlite3DbFree(db, pSavepoint); |
| 3759 | if( !isTransaction ){ |
| 3760 | db->nSavepoint--; |
| 3761 | } |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 3762 | }else{ |
drh | 2ce9b6b | 2019-05-10 14:03:07 +0000 | [diff] [blame] | 3763 | assert( p1==SAVEPOINT_ROLLBACK ); |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 3764 | db->nDeferredCons = pSavepoint->nDeferredCons; |
dan | cb3e4b7 | 2013-07-03 19:53:05 +0000 | [diff] [blame] | 3765 | db->nDeferredImmCons = pSavepoint->nDeferredImmCons; |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3766 | } |
dan | d9495cd | 2011-04-27 12:08:04 +0000 | [diff] [blame] | 3767 | |
dan | ea8562e | 2015-04-18 16:25:54 +0000 | [diff] [blame] | 3768 | if( !isTransaction || p1==SAVEPOINT_ROLLBACK ){ |
dan | d9495cd | 2011-04-27 12:08:04 +0000 | [diff] [blame] | 3769 | rc = sqlite3VtabSavepoint(db, p1, iSavepoint); |
| 3770 | if( rc!=SQLITE_OK ) goto abort_due_to_error; |
| 3771 | } |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3772 | } |
| 3773 | } |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 3774 | if( rc ) goto abort_due_to_error; |
drh | 8703edd | 2022-04-03 22:35:13 +0000 | [diff] [blame] | 3775 | if( p->eVdbeState==VDBE_HALT_STATE ){ |
| 3776 | rc = SQLITE_DONE; |
| 3777 | goto vdbe_return; |
| 3778 | } |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3779 | break; |
| 3780 | } |
| 3781 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 3782 | /* Opcode: AutoCommit P1 P2 * * * |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 3783 | ** |
| 3784 | ** 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] | 3785 | ** back any currently active btree transactions. If there are any active |
drh | c25eabe | 2009-02-24 18:57:31 +0000 | [diff] [blame] | 3786 | ** VMs (apart from this one), then a ROLLBACK fails. A COMMIT fails if |
| 3787 | ** there are active writing VMs or active VMs that use shared cache. |
drh | 92f02c3 | 2004-09-02 14:57:08 +0000 | [diff] [blame] | 3788 | ** |
| 3789 | ** This instruction causes the VM to halt. |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 3790 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3791 | case OP_AutoCommit: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3792 | int desiredAutoCommit; |
shane | 68c0273 | 2009-06-09 18:14:18 +0000 | [diff] [blame] | 3793 | int iRollback; |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 3794 | |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3795 | desiredAutoCommit = pOp->p1; |
shane | 68c0273 | 2009-06-09 18:14:18 +0000 | [diff] [blame] | 3796 | iRollback = pOp->p2; |
drh | ad4a4b8 | 2008-11-05 16:37:34 +0000 | [diff] [blame] | 3797 | assert( desiredAutoCommit==1 || desiredAutoCommit==0 ); |
shane | 68c0273 | 2009-06-09 18:14:18 +0000 | [diff] [blame] | 3798 | assert( desiredAutoCommit==1 || iRollback==0 ); |
drh | 4f7d3a5 | 2013-06-27 23:54:02 +0000 | [diff] [blame] | 3799 | assert( db->nVdbeActive>0 ); /* At least this one VM is active */ |
dan | c0537fe | 2013-06-28 19:41:43 +0000 | [diff] [blame] | 3800 | assert( p->bIsReader ); |
danielk1977 | 46c43ed | 2004-06-30 06:30:25 +0000 | [diff] [blame] | 3801 | |
drh | b0c8865 | 2016-02-01 13:21:13 +0000 | [diff] [blame] | 3802 | if( desiredAutoCommit!=db->autoCommit ){ |
shane | 68c0273 | 2009-06-09 18:14:18 +0000 | [diff] [blame] | 3803 | if( iRollback ){ |
drh | ad4a4b8 | 2008-11-05 16:37:34 +0000 | [diff] [blame] | 3804 | assert( desiredAutoCommit==1 ); |
drh | 21021a5 | 2012-02-13 17:01:51 +0000 | [diff] [blame] | 3805 | sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK); |
danielk1977 | f3f06bb | 2005-12-16 15:24:28 +0000 | [diff] [blame] | 3806 | db->autoCommit = 1; |
drh | b0c8865 | 2016-02-01 13:21:13 +0000 | [diff] [blame] | 3807 | }else if( desiredAutoCommit && db->nVdbeWrite>0 ){ |
| 3808 | /* If this instruction implements a COMMIT and other VMs are writing |
| 3809 | ** return an error indicating that the other VMs must complete first. |
| 3810 | */ |
| 3811 | sqlite3VdbeError(p, "cannot commit transaction - " |
| 3812 | "SQL statements in progress"); |
| 3813 | rc = SQLITE_BUSY; |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 3814 | goto abort_due_to_error; |
dan | 32b09f2 | 2009-09-23 17:29:59 +0000 | [diff] [blame] | 3815 | }else if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){ |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 3816 | goto vdbe_return; |
danielk1977 | f3f06bb | 2005-12-16 15:24:28 +0000 | [diff] [blame] | 3817 | }else{ |
shane | 7d3846a | 2008-12-11 02:58:26 +0000 | [diff] [blame] | 3818 | db->autoCommit = (u8)desiredAutoCommit; |
drh | 8ff2587 | 2015-07-31 18:59:56 +0000 | [diff] [blame] | 3819 | } |
| 3820 | if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){ |
| 3821 | p->pc = (int)(pOp - aOp); |
| 3822 | db->autoCommit = (u8)(1-desiredAutoCommit); |
| 3823 | p->rc = rc = SQLITE_BUSY; |
| 3824 | goto vdbe_return; |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 3825 | } |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3826 | sqlite3CloseSavepoints(db); |
drh | 83968c4 | 2007-04-18 16:45:24 +0000 | [diff] [blame] | 3827 | if( p->rc==SQLITE_OK ){ |
drh | 900b31e | 2007-08-28 02:27:51 +0000 | [diff] [blame] | 3828 | rc = SQLITE_DONE; |
drh | 83968c4 | 2007-04-18 16:45:24 +0000 | [diff] [blame] | 3829 | }else{ |
drh | 900b31e | 2007-08-28 02:27:51 +0000 | [diff] [blame] | 3830 | rc = SQLITE_ERROR; |
drh | 83968c4 | 2007-04-18 16:45:24 +0000 | [diff] [blame] | 3831 | } |
drh | 900b31e | 2007-08-28 02:27:51 +0000 | [diff] [blame] | 3832 | goto vdbe_return; |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 3833 | }else{ |
drh | 22c17b8 | 2015-05-15 04:13:15 +0000 | [diff] [blame] | 3834 | sqlite3VdbeError(p, |
drh | ad4a4b8 | 2008-11-05 16:37:34 +0000 | [diff] [blame] | 3835 | (!desiredAutoCommit)?"cannot start a transaction within a transaction":( |
shane | 68c0273 | 2009-06-09 18:14:18 +0000 | [diff] [blame] | 3836 | (iRollback)?"cannot rollback - no transaction is active": |
drh | f089aa4 | 2008-07-08 19:34:06 +0000 | [diff] [blame] | 3837 | "cannot commit - no transaction is active")); |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 3838 | |
| 3839 | rc = SQLITE_ERROR; |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 3840 | goto abort_due_to_error; |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 3841 | } |
drh | 8616cff | 2019-07-13 16:15:23 +0000 | [diff] [blame] | 3842 | /*NOTREACHED*/ assert(0); |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 3843 | } |
| 3844 | |
drh | b22f7c8 | 2014-02-06 23:56:27 +0000 | [diff] [blame] | 3845 | /* Opcode: Transaction P1 P2 P3 P4 P5 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3846 | ** |
drh | 05a86c5 | 2014-02-16 01:55:49 +0000 | [diff] [blame] | 3847 | ** Begin a transaction on database P1 if a transaction is not already |
| 3848 | ** active. |
| 3849 | ** If P2 is non-zero, then a write-transaction is started, or if a |
| 3850 | ** read-transaction is already active, it is upgraded to a write-transaction. |
drh | 1ca037f | 2020-10-12 13:24:00 +0000 | [diff] [blame] | 3851 | ** If P2 is zero, then a read-transaction is started. If P2 is 2 or more |
| 3852 | ** then an exclusive transaction is started. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3853 | ** |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 3854 | ** P1 is the index of the database file on which the transaction is |
| 3855 | ** started. Index 0 is the main database file and index 1 is the |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 3856 | ** file used for temporary tables. Indices of 2 or more are used for |
| 3857 | ** attached databases. |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 3858 | ** |
dan | e0af83a | 2009-09-08 19:15:01 +0000 | [diff] [blame] | 3859 | ** If a write-transaction is started and the Vdbe.usesStmtJournal flag is |
| 3860 | ** true (this flag is set if the Vdbe may modify more than one row and may |
| 3861 | ** throw an ABORT exception), a statement transaction may also be opened. |
| 3862 | ** More specifically, a statement transaction is opened iff the database |
| 3863 | ** connection is currently not in autocommit mode, or if there are other |
drh | a451017 | 2012-02-02 15:50:17 +0000 | [diff] [blame] | 3864 | ** active statements. A statement transaction allows the changes made by this |
dan | e0af83a | 2009-09-08 19:15:01 +0000 | [diff] [blame] | 3865 | ** VDBE to be rolled back after an error without having to roll back the |
| 3866 | ** entire transaction. If no error is encountered, the statement transaction |
| 3867 | ** will automatically commit when the VDBE halts. |
| 3868 | ** |
drh | b22f7c8 | 2014-02-06 23:56:27 +0000 | [diff] [blame] | 3869 | ** If P5!=0 then this opcode also checks the schema cookie against P3 |
| 3870 | ** and the schema generation counter against P4. |
| 3871 | ** The cookie changes its value whenever the database schema changes. |
| 3872 | ** This operation is used to detect when that the cookie has changed |
drh | 05a86c5 | 2014-02-16 01:55:49 +0000 | [diff] [blame] | 3873 | ** and that the current process needs to reread the schema. If the schema |
| 3874 | ** cookie in P3 differs from the schema cookie in the database header or |
| 3875 | ** if the schema generation counter in P4 differs from the current |
| 3876 | ** generation counter, then an SQLITE_SCHEMA error is raised and execution |
| 3877 | ** halts. The sqlite3_step() wrapper function might then reprepare the |
| 3878 | ** statement and rerun it from the beginning. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3879 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3880 | case OP_Transaction: { |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 3881 | Btree *pBt; |
drh | 8ee75f7 | 2022-04-03 10:42:06 +0000 | [diff] [blame] | 3882 | Db *pDb; |
drh | bb2d9b1 | 2018-06-06 16:28:40 +0000 | [diff] [blame] | 3883 | int iMeta = 0; |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 3884 | |
drh | 1713afb | 2013-06-28 01:24:57 +0000 | [diff] [blame] | 3885 | assert( p->bIsReader ); |
drh | 9e92a47 | 2013-06-27 17:40:30 +0000 | [diff] [blame] | 3886 | assert( p->readOnly==0 || pOp->p2==0 ); |
drh | 1ca037f | 2020-10-12 13:24:00 +0000 | [diff] [blame] | 3887 | assert( pOp->p2>=0 && pOp->p2<=2 ); |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 3888 | assert( pOp->p1>=0 && pOp->p1<db->nDb ); |
drh | a7ab6d8 | 2014-07-21 15:44:39 +0000 | [diff] [blame] | 3889 | assert( DbMaskTest(p->btreeMask, pOp->p1) ); |
drh | 8cb63f5 | 2021-10-29 09:59:06 +0000 | [diff] [blame] | 3890 | assert( rc==SQLITE_OK ); |
drh | 46c425b | 2021-11-10 10:59:10 +0000 | [diff] [blame] | 3891 | if( pOp->p2 && (db->flags & (SQLITE_QueryOnly|SQLITE_CorruptRdOnly))!=0 ){ |
| 3892 | if( db->flags & SQLITE_QueryOnly ){ |
| 3893 | /* Writes prohibited by the "PRAGMA query_only=TRUE" statement */ |
| 3894 | rc = SQLITE_READONLY; |
| 3895 | }else{ |
| 3896 | /* Writes prohibited due to a prior SQLITE_CORRUPT in the current |
| 3897 | ** transaction */ |
| 3898 | rc = SQLITE_CORRUPT; |
| 3899 | } |
drh | 13447bf | 2013-07-10 13:33:49 +0000 | [diff] [blame] | 3900 | goto abort_due_to_error; |
| 3901 | } |
drh | 8ee75f7 | 2022-04-03 10:42:06 +0000 | [diff] [blame] | 3902 | pDb = &db->aDb[pOp->p1]; |
| 3903 | pBt = pDb->pBt; |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 3904 | |
danielk1977 | 24162fe | 2004-06-04 06:22:00 +0000 | [diff] [blame] | 3905 | if( pBt ){ |
drh | bb2d9b1 | 2018-06-06 16:28:40 +0000 | [diff] [blame] | 3906 | rc = sqlite3BtreeBeginTrans(pBt, pOp->p2, &iMeta); |
drh | cbd8db3 | 2015-08-20 17:18:32 +0000 | [diff] [blame] | 3907 | testcase( rc==SQLITE_BUSY_SNAPSHOT ); |
| 3908 | testcase( rc==SQLITE_BUSY_RECOVERY ); |
drh | 9e9f1bd | 2009-10-13 15:36:51 +0000 | [diff] [blame] | 3909 | if( rc!=SQLITE_OK ){ |
drh | fadd2b1 | 2016-09-19 23:39:34 +0000 | [diff] [blame] | 3910 | if( (rc&0xff)==SQLITE_BUSY ){ |
| 3911 | p->pc = (int)(pOp - aOp); |
| 3912 | p->rc = rc; |
| 3913 | goto vdbe_return; |
| 3914 | } |
danielk1977 | 24162fe | 2004-06-04 06:22:00 +0000 | [diff] [blame] | 3915 | goto abort_due_to_error; |
drh | 90bfcda | 2001-09-23 19:46:51 +0000 | [diff] [blame] | 3916 | } |
dan | e0af83a | 2009-09-08 19:15:01 +0000 | [diff] [blame] | 3917 | |
drh | 4d29448 | 2019-10-05 15:28:24 +0000 | [diff] [blame] | 3918 | if( p->usesStmtJournal |
| 3919 | && pOp->p2 |
dan | c0537fe | 2013-06-28 19:41:43 +0000 | [diff] [blame] | 3920 | && (db->autoCommit==0 || db->nVdbeRead>1) |
dan | e0af83a | 2009-09-08 19:15:01 +0000 | [diff] [blame] | 3921 | ){ |
drh | 99744fa | 2020-08-25 19:09:07 +0000 | [diff] [blame] | 3922 | assert( sqlite3BtreeTxnState(pBt)==SQLITE_TXN_WRITE ); |
dan | e0af83a | 2009-09-08 19:15:01 +0000 | [diff] [blame] | 3923 | if( p->iStatement==0 ){ |
| 3924 | assert( db->nStatement>=0 && db->nSavepoint>=0 ); |
| 3925 | db->nStatement++; |
| 3926 | p->iStatement = db->nSavepoint + db->nStatement; |
| 3927 | } |
dan | a311b80 | 2011-04-26 19:21:34 +0000 | [diff] [blame] | 3928 | |
drh | 346506f | 2011-05-25 01:16:42 +0000 | [diff] [blame] | 3929 | rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN, p->iStatement-1); |
dan | a311b80 | 2011-04-26 19:21:34 +0000 | [diff] [blame] | 3930 | if( rc==SQLITE_OK ){ |
| 3931 | rc = sqlite3BtreeBeginStmt(pBt, p->iStatement); |
| 3932 | } |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 3933 | |
| 3934 | /* Store the current value of the database handles deferred constraint |
| 3935 | ** counter. If the statement transaction needs to be rolled back, |
| 3936 | ** the value of this counter needs to be restored too. */ |
| 3937 | p->nStmtDefCons = db->nDeferredCons; |
dan | cb3e4b7 | 2013-07-03 19:53:05 +0000 | [diff] [blame] | 3938 | p->nStmtDefImmCons = db->nDeferredImmCons; |
dan | e0af83a | 2009-09-08 19:15:01 +0000 | [diff] [blame] | 3939 | } |
drh | 397776a | 2018-06-06 17:45:51 +0000 | [diff] [blame] | 3940 | } |
| 3941 | assert( pOp->p5==0 || pOp->p4type==P4_INT32 ); |
drh | e4e1af5 | 2021-10-29 16:19:03 +0000 | [diff] [blame] | 3942 | if( rc==SQLITE_OK |
| 3943 | && pOp->p5 |
drh | 8ee75f7 | 2022-04-03 10:42:06 +0000 | [diff] [blame] | 3944 | && (iMeta!=pOp->p3 || pDb->pSchema->iGeneration!=pOp->p4.i) |
drh | 397776a | 2018-06-06 17:45:51 +0000 | [diff] [blame] | 3945 | ){ |
dan | d2ffc97 | 2020-12-10 19:20:15 +0000 | [diff] [blame] | 3946 | /* |
| 3947 | ** IMPLEMENTATION-OF: R-03189-51135 As each SQL statement runs, the schema |
| 3948 | ** version is checked to ensure that the schema has not changed since the |
| 3949 | ** SQL statement was prepared. |
| 3950 | */ |
| 3951 | sqlite3DbFree(db, p->zErrMsg); |
| 3952 | p->zErrMsg = sqlite3DbStrDup(db, "database schema has changed"); |
drh | b22f7c8 | 2014-02-06 23:56:27 +0000 | [diff] [blame] | 3953 | /* If the schema-cookie from the database file matches the cookie |
| 3954 | ** stored with the in-memory representation of the schema, do |
| 3955 | ** not reload the schema from the database file. |
| 3956 | ** |
| 3957 | ** If virtual-tables are in use, this is not just an optimization. |
| 3958 | ** Often, v-tables store their data in other SQLite tables, which |
| 3959 | ** are queried from within xNext() and other v-table methods using |
| 3960 | ** prepared queries. If such a query is out-of-date, we do not want to |
| 3961 | ** discard the database schema, as the user code implementing the |
| 3962 | ** v-table would have to be ready for the sqlite3_vtab structure itself |
| 3963 | ** to be invalidated whenever sqlite3_step() is called from within |
| 3964 | ** a v-table method. |
| 3965 | */ |
| 3966 | if( db->aDb[pOp->p1].pSchema->schema_cookie!=iMeta ){ |
| 3967 | sqlite3ResetOneSchema(db, pOp->p1); |
| 3968 | } |
| 3969 | p->expired = 1; |
| 3970 | rc = SQLITE_SCHEMA; |
dan | 0a841a2 | 2022-06-08 18:20:36 +0000 | [diff] [blame] | 3971 | |
| 3972 | /* Set changeCntOn to 0 to prevent the value returned by sqlite3_changes() |
| 3973 | ** from being modified in sqlite3VdbeHalt(). If this statement is |
| 3974 | ** reprepared, changeCntOn will be set again. */ |
| 3975 | p->changeCntOn = 0; |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 3976 | } |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 3977 | if( rc ) goto abort_due_to_error; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3978 | break; |
| 3979 | } |
| 3980 | |
drh | b1fdb2a | 2008-01-05 04:06:03 +0000 | [diff] [blame] | 3981 | /* Opcode: ReadCookie P1 P2 P3 * * |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 3982 | ** |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3983 | ** Read cookie number P3 from database P1 and write it into register P2. |
danielk1977 | 0d19f7a | 2009-06-03 11:25:07 +0000 | [diff] [blame] | 3984 | ** P3==1 is the schema version. P3==2 is the database format. |
| 3985 | ** 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] | 3986 | ** the main database file and P1==1 is the database file used to store |
| 3987 | ** temporary tables. |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 3988 | ** |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 3989 | ** There must be a read-lock on the database (either a transaction |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 3990 | ** must be started or there must be an open cursor) before |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 3991 | ** executing this instruction. |
| 3992 | */ |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 3993 | case OP_ReadCookie: { /* out2 */ |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 3994 | int iMeta; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3995 | int iDb; |
| 3996 | int iCookie; |
danielk1977 | 180b56a | 2007-06-24 08:00:42 +0000 | [diff] [blame] | 3997 | |
drh | 1713afb | 2013-06-28 01:24:57 +0000 | [diff] [blame] | 3998 | assert( p->bIsReader ); |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3999 | iDb = pOp->p1; |
| 4000 | iCookie = pOp->p3; |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 4001 | assert( pOp->p3<SQLITE_N_BTREE_META ); |
danielk1977 | 180b56a | 2007-06-24 08:00:42 +0000 | [diff] [blame] | 4002 | assert( iDb>=0 && iDb<db->nDb ); |
| 4003 | assert( db->aDb[iDb].pBt!=0 ); |
drh | a7ab6d8 | 2014-07-21 15:44:39 +0000 | [diff] [blame] | 4004 | assert( DbMaskTest(p->btreeMask, iDb) ); |
danielk1977 | 0d19f7a | 2009-06-03 11:25:07 +0000 | [diff] [blame] | 4005 | |
danielk1977 | 602b466 | 2009-07-02 07:47:33 +0000 | [diff] [blame] | 4006 | sqlite3BtreeGetMeta(db->aDb[iDb].pBt, iCookie, (u32 *)&iMeta); |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 4007 | pOut = out2Prerelease(p, pOp); |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 4008 | pOut->u.i = iMeta; |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 4009 | break; |
| 4010 | } |
| 4011 | |
drh | e3863b5 | 2020-07-01 16:19:14 +0000 | [diff] [blame] | 4012 | /* Opcode: SetCookie P1 P2 P3 * P5 |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 4013 | ** |
drh | 1861afc | 2016-02-01 21:48:34 +0000 | [diff] [blame] | 4014 | ** Write the integer value P3 into cookie number P2 of database P1. |
| 4015 | ** P2==1 is the schema version. P2==2 is the database format. |
| 4016 | ** P2==3 is the recommended pager cache |
danielk1977 | 0d19f7a | 2009-06-03 11:25:07 +0000 | [diff] [blame] | 4017 | ** size, and so forth. P1==0 is the main database file and P1==1 is the |
| 4018 | ** database file used to store temporary tables. |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 4019 | ** |
| 4020 | ** A transaction must be started before executing this opcode. |
drh | e3863b5 | 2020-07-01 16:19:14 +0000 | [diff] [blame] | 4021 | ** |
| 4022 | ** If P2 is the SCHEMA_VERSION cookie (cookie number 1) then the internal |
| 4023 | ** schema version is set to P3-P5. The "PRAGMA schema_version=N" statement |
| 4024 | ** has P5 set to 1, so that the internal schema version will be different |
| 4025 | ** from the database schema version, resulting in a schema reset. |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 4026 | */ |
drh | 1861afc | 2016-02-01 21:48:34 +0000 | [diff] [blame] | 4027 | case OP_SetCookie: { |
drh | 3f7d4e4 | 2004-07-24 14:35:58 +0000 | [diff] [blame] | 4028 | Db *pDb; |
drh | 4031baf | 2018-05-28 17:31:20 +0000 | [diff] [blame] | 4029 | |
| 4030 | sqlite3VdbeIncrWriteCounter(p, 0); |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 4031 | assert( pOp->p2<SQLITE_N_BTREE_META ); |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 4032 | assert( pOp->p1>=0 && pOp->p1<db->nDb ); |
drh | a7ab6d8 | 2014-07-21 15:44:39 +0000 | [diff] [blame] | 4033 | assert( DbMaskTest(p->btreeMask, pOp->p1) ); |
drh | 9e92a47 | 2013-06-27 17:40:30 +0000 | [diff] [blame] | 4034 | assert( p->readOnly==0 ); |
drh | 3f7d4e4 | 2004-07-24 14:35:58 +0000 | [diff] [blame] | 4035 | pDb = &db->aDb[pOp->p1]; |
| 4036 | assert( pDb->pBt!=0 ); |
drh | 2120608 | 2011-04-04 18:22:02 +0000 | [diff] [blame] | 4037 | assert( sqlite3SchemaMutexHeld(db, pOp->p1, 0) ); |
drh | a3b321d | 2004-05-11 09:31:31 +0000 | [diff] [blame] | 4038 | /* See note about index shifting on OP_ReadCookie */ |
drh | 1861afc | 2016-02-01 21:48:34 +0000 | [diff] [blame] | 4039 | rc = sqlite3BtreeUpdateMeta(pDb->pBt, pOp->p2, pOp->p3); |
danielk1977 | 0d19f7a | 2009-06-03 11:25:07 +0000 | [diff] [blame] | 4040 | if( pOp->p2==BTREE_SCHEMA_VERSION ){ |
drh | 3f7d4e4 | 2004-07-24 14:35:58 +0000 | [diff] [blame] | 4041 | /* When the schema cookie changes, record the new cookie internally */ |
drh | 86bc5e4 | 2022-06-24 12:56:48 +0000 | [diff] [blame] | 4042 | *(u32*)&pDb->pSchema->schema_cookie = *(u32*)&pOp->p3 - pOp->p5; |
drh | 8257aa8 | 2017-07-26 19:59:13 +0000 | [diff] [blame] | 4043 | db->mDbFlags |= DBFLAG_SchemaChange; |
drh | 44a5c02 | 2022-01-02 12:01:03 +0000 | [diff] [blame] | 4044 | sqlite3FkClearTriggerCache(db, pOp->p1); |
danielk1977 | 0d19f7a | 2009-06-03 11:25:07 +0000 | [diff] [blame] | 4045 | }else if( pOp->p2==BTREE_FILE_FORMAT ){ |
drh | d28bcb3 | 2005-12-21 14:43:11 +0000 | [diff] [blame] | 4046 | /* Record changes in the file format */ |
drh | 1861afc | 2016-02-01 21:48:34 +0000 | [diff] [blame] | 4047 | pDb->pSchema->file_format = pOp->p3; |
drh | 3f7d4e4 | 2004-07-24 14:35:58 +0000 | [diff] [blame] | 4048 | } |
drh | fd426c6 | 2006-01-30 15:34:22 +0000 | [diff] [blame] | 4049 | if( pOp->p1==1 ){ |
| 4050 | /* Invalidate all prepared statements whenever the TEMP database |
| 4051 | ** schema is changed. Ticket #1644 */ |
drh | ba968db | 2018-07-24 22:02:12 +0000 | [diff] [blame] | 4052 | sqlite3ExpirePreparedStatements(db, 0); |
dan | fa401de | 2009-10-16 14:55:03 +0000 | [diff] [blame] | 4053 | p->expired = 0; |
drh | fd426c6 | 2006-01-30 15:34:22 +0000 | [diff] [blame] | 4054 | } |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 4055 | if( rc ) goto abort_due_to_error; |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 4056 | break; |
| 4057 | } |
| 4058 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4059 | /* Opcode: OpenRead P1 P2 P3 P4 P5 |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 4060 | ** Synopsis: root=P2 iDb=P3 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4061 | ** |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 4062 | ** Open a read-only cursor for the database table whose root page is |
danielk1977 | 207872a | 2008-01-03 07:54:23 +0000 | [diff] [blame] | 4063 | ** P2 in a database file. The database file is determined by P3. |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 4064 | ** P3==0 means the main database, P3==1 means the database used for |
| 4065 | ** temporary tables, and P3>1 means used the corresponding attached |
| 4066 | ** database. Give the new cursor an identifier of P1. The P1 |
danielk1977 | 207872a | 2008-01-03 07:54:23 +0000 | [diff] [blame] | 4067 | ** values need not be contiguous but all P1 values should be small integers. |
| 4068 | ** It is an error for P1 to be negative. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4069 | ** |
drh | 8e9deb6 | 2018-06-05 13:43:02 +0000 | [diff] [blame] | 4070 | ** Allowed P5 bits: |
| 4071 | ** <ul> |
| 4072 | ** <li> <b>0x02 OPFLAG_SEEKEQ</b>: This cursor will only be used for |
| 4073 | ** equality lookups (implemented as a pair of opcodes OP_SeekGE/OP_IdxGT |
drh | 576d0a9 | 2020-03-12 17:28:27 +0000 | [diff] [blame] | 4074 | ** of OP_SeekLE/OP_IdxLT) |
drh | 8e9deb6 | 2018-06-05 13:43:02 +0000 | [diff] [blame] | 4075 | ** </ul> |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 4076 | ** |
danielk1977 | d336e22 | 2009-02-20 10:58:41 +0000 | [diff] [blame] | 4077 | ** The P4 value may be either an integer (P4_INT32) or a pointer to |
| 4078 | ** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo |
drh | 8e9deb6 | 2018-06-05 13:43:02 +0000 | [diff] [blame] | 4079 | ** object, then table being opened must be an [index b-tree] where the |
| 4080 | ** KeyInfo object defines the content and collating |
| 4081 | ** sequence of that index b-tree. Otherwise, if P4 is an integer |
| 4082 | ** value, then the table being opened must be a [table b-tree] with a |
| 4083 | ** number of columns no less than the value of P4. |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 4084 | ** |
drh | 3526319 | 2014-07-22 20:02:19 +0000 | [diff] [blame] | 4085 | ** See also: OpenWrite, ReopenIdx |
| 4086 | */ |
| 4087 | /* Opcode: ReopenIdx P1 P2 P3 P4 P5 |
| 4088 | ** Synopsis: root=P2 iDb=P3 |
| 4089 | ** |
drh | 8e9deb6 | 2018-06-05 13:43:02 +0000 | [diff] [blame] | 4090 | ** The ReopenIdx opcode works like OP_OpenRead except that it first |
| 4091 | ** checks to see if the cursor on P1 is already open on the same |
| 4092 | ** 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] | 4093 | ** if the cursor is already open, do not reopen it. |
| 4094 | ** |
drh | 8e9deb6 | 2018-06-05 13:43:02 +0000 | [diff] [blame] | 4095 | ** The ReopenIdx opcode may only be used with P5==0 or P5==OPFLAG_SEEKEQ |
| 4096 | ** and with P4 being a P4_KEYINFO object. Furthermore, the P3 value must |
| 4097 | ** be the same as every other ReopenIdx or OpenRead for the same cursor |
| 4098 | ** number. |
drh | 3526319 | 2014-07-22 20:02:19 +0000 | [diff] [blame] | 4099 | ** |
drh | 8e9deb6 | 2018-06-05 13:43:02 +0000 | [diff] [blame] | 4100 | ** Allowed P5 bits: |
| 4101 | ** <ul> |
| 4102 | ** <li> <b>0x02 OPFLAG_SEEKEQ</b>: This cursor will only be used for |
| 4103 | ** equality lookups (implemented as a pair of opcodes OP_SeekGE/OP_IdxGT |
drh | 576d0a9 | 2020-03-12 17:28:27 +0000 | [diff] [blame] | 4104 | ** of OP_SeekLE/OP_IdxLT) |
drh | 8e9deb6 | 2018-06-05 13:43:02 +0000 | [diff] [blame] | 4105 | ** </ul> |
| 4106 | ** |
| 4107 | ** See also: OP_OpenRead, OP_OpenWrite |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4108 | */ |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4109 | /* Opcode: OpenWrite P1 P2 P3 P4 P5 |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 4110 | ** Synopsis: root=P2 iDb=P3 |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 4111 | ** |
| 4112 | ** 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] | 4113 | ** page is P2 (or whose root page is held in register P2 if the |
| 4114 | ** OPFLAG_P2ISREG bit is set in P5 - see below). |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 4115 | ** |
danielk1977 | d336e22 | 2009-02-20 10:58:41 +0000 | [diff] [blame] | 4116 | ** The P4 value may be either an integer (P4_INT32) or a pointer to |
| 4117 | ** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo |
drh | 8e9deb6 | 2018-06-05 13:43:02 +0000 | [diff] [blame] | 4118 | ** object, then table being opened must be an [index b-tree] where the |
| 4119 | ** KeyInfo object defines the content and collating |
| 4120 | ** sequence of that index b-tree. Otherwise, if P4 is an integer |
| 4121 | ** value, then the table being opened must be a [table b-tree] with a |
| 4122 | ** number of columns no less than the value of P4. |
jplyon | 5a56422 | 2003-06-02 06:15:58 +0000 | [diff] [blame] | 4123 | ** |
drh | 8e9deb6 | 2018-06-05 13:43:02 +0000 | [diff] [blame] | 4124 | ** Allowed P5 bits: |
| 4125 | ** <ul> |
| 4126 | ** <li> <b>0x02 OPFLAG_SEEKEQ</b>: This cursor will only be used for |
| 4127 | ** equality lookups (implemented as a pair of opcodes OP_SeekGE/OP_IdxGT |
drh | 576d0a9 | 2020-03-12 17:28:27 +0000 | [diff] [blame] | 4128 | ** of OP_SeekLE/OP_IdxLT) |
drh | 8e9deb6 | 2018-06-05 13:43:02 +0000 | [diff] [blame] | 4129 | ** <li> <b>0x08 OPFLAG_FORDELETE</b>: This cursor is used only to seek |
| 4130 | ** and subsequently delete entries in an index btree. This is a |
| 4131 | ** hint to the storage engine that the storage engine is allowed to |
| 4132 | ** ignore. The hint is not used by the official SQLite b*tree storage |
| 4133 | ** engine, but is used by COMDB2. |
| 4134 | ** <li> <b>0x10 OPFLAG_P2ISREG</b>: Use the content of register P2 |
| 4135 | ** as the root page, not the value of P2 itself. |
| 4136 | ** </ul> |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 4137 | ** |
drh | 8e9deb6 | 2018-06-05 13:43:02 +0000 | [diff] [blame] | 4138 | ** This instruction works like OpenRead except that it opens the cursor |
| 4139 | ** in read/write mode. |
| 4140 | ** |
| 4141 | ** See also: OP_OpenRead, OP_ReopenIdx |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 4142 | */ |
drh | 3526319 | 2014-07-22 20:02:19 +0000 | [diff] [blame] | 4143 | case OP_ReopenIdx: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 4144 | int nField; |
| 4145 | KeyInfo *pKeyInfo; |
drh | abc3815 | 2020-07-22 13:38:04 +0000 | [diff] [blame] | 4146 | u32 p2; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 4147 | int iDb; |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 4148 | int wrFlag; |
| 4149 | Btree *pX; |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 4150 | VdbeCursor *pCur; |
drh | d946db0 | 2005-12-29 19:23:06 +0000 | [diff] [blame] | 4151 | Db *pDb; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 4152 | |
drh | e0997b3 | 2015-03-20 14:57:50 +0000 | [diff] [blame] | 4153 | assert( pOp->p5==0 || pOp->p5==OPFLAG_SEEKEQ ); |
drh | 3526319 | 2014-07-22 20:02:19 +0000 | [diff] [blame] | 4154 | assert( pOp->p4type==P4_KEYINFO ); |
| 4155 | pCur = p->apCsr[pOp->p1]; |
drh | e8f2c9d | 2014-08-06 17:49:13 +0000 | [diff] [blame] | 4156 | if( pCur && pCur->pgnoRoot==(u32)pOp->p2 ){ |
drh | 3526319 | 2014-07-22 20:02:19 +0000 | [diff] [blame] | 4157 | assert( pCur->iDb==pOp->p3 ); /* Guaranteed by the code generator */ |
drh | 4422b3a | 2021-06-25 14:48:24 +0000 | [diff] [blame] | 4158 | assert( pCur->eCurType==CURTYPE_BTREE ); |
| 4159 | sqlite3BtreeClearCursor(pCur->uc.pCursor); |
drh | e0997b3 | 2015-03-20 14:57:50 +0000 | [diff] [blame] | 4160 | goto open_cursor_set_hints; |
drh | 3526319 | 2014-07-22 20:02:19 +0000 | [diff] [blame] | 4161 | } |
| 4162 | /* If the cursor is not currently open or is open on a different |
| 4163 | ** index, then fall through into OP_OpenRead to force a reopen */ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4164 | case OP_OpenRead: |
drh | 1fa509a | 2015-03-20 16:34:49 +0000 | [diff] [blame] | 4165 | case OP_OpenWrite: |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 4166 | |
drh | e0997b3 | 2015-03-20 14:57:50 +0000 | [diff] [blame] | 4167 | assert( pOp->opcode==OP_OpenWrite || pOp->p5==0 || pOp->p5==OPFLAG_SEEKEQ ); |
drh | 1713afb | 2013-06-28 01:24:57 +0000 | [diff] [blame] | 4168 | assert( p->bIsReader ); |
drh | 3526319 | 2014-07-22 20:02:19 +0000 | [diff] [blame] | 4169 | assert( pOp->opcode==OP_OpenRead || pOp->opcode==OP_ReopenIdx |
| 4170 | || p->readOnly==0 ); |
dan | 428c218 | 2012-08-06 18:50:11 +0000 | [diff] [blame] | 4171 | |
drh | ba968db | 2018-07-24 22:02:12 +0000 | [diff] [blame] | 4172 | if( p->expired==1 ){ |
drh | 47b7fc7 | 2014-11-11 01:33:57 +0000 | [diff] [blame] | 4173 | rc = SQLITE_ABORT_ROLLBACK; |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 4174 | goto abort_due_to_error; |
dan | fa401de | 2009-10-16 14:55:03 +0000 | [diff] [blame] | 4175 | } |
| 4176 | |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 4177 | nField = 0; |
| 4178 | pKeyInfo = 0; |
drh | abc3815 | 2020-07-22 13:38:04 +0000 | [diff] [blame] | 4179 | p2 = (u32)pOp->p2; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 4180 | iDb = pOp->p3; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 4181 | assert( iDb>=0 && iDb<db->nDb ); |
drh | a7ab6d8 | 2014-07-21 15:44:39 +0000 | [diff] [blame] | 4182 | assert( DbMaskTest(p->btreeMask, iDb) ); |
drh | d946db0 | 2005-12-29 19:23:06 +0000 | [diff] [blame] | 4183 | pDb = &db->aDb[iDb]; |
| 4184 | pX = pDb->pBt; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 4185 | assert( pX!=0 ); |
drh | d946db0 | 2005-12-29 19:23:06 +0000 | [diff] [blame] | 4186 | if( pOp->opcode==OP_OpenWrite ){ |
dan | fd261ec | 2015-10-22 20:54:33 +0000 | [diff] [blame] | 4187 | assert( OPFLAG_FORDELETE==BTREE_FORDELETE ); |
| 4188 | wrFlag = BTREE_WRCSR | (pOp->p5 & OPFLAG_FORDELETE); |
drh | 2120608 | 2011-04-04 18:22:02 +0000 | [diff] [blame] | 4189 | assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 4190 | if( pDb->pSchema->file_format < p->minWriteFileFormat ){ |
| 4191 | p->minWriteFileFormat = pDb->pSchema->file_format; |
drh | d946db0 | 2005-12-29 19:23:06 +0000 | [diff] [blame] | 4192 | } |
| 4193 | }else{ |
| 4194 | wrFlag = 0; |
| 4195 | } |
dan | 428c218 | 2012-08-06 18:50:11 +0000 | [diff] [blame] | 4196 | if( pOp->p5 & OPFLAG_P2ISREG ){ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4197 | assert( p2>0 ); |
mistachkin | cec5f1d | 2020-08-04 16:11:37 +0000 | [diff] [blame] | 4198 | assert( p2<=(u32)(p->nMem+1 - p->nCursor) ); |
drh | 8e9deb6 | 2018-06-05 13:43:02 +0000 | [diff] [blame] | 4199 | assert( pOp->opcode==OP_OpenWrite ); |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 4200 | pIn2 = &aMem[p2]; |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 4201 | assert( memIsValid(pIn2) ); |
| 4202 | assert( (pIn2->flags & MEM_Int)!=0 ); |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4203 | sqlite3VdbeMemIntegerify(pIn2); |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 4204 | p2 = (int)pIn2->u.i; |
drh | 0f3f766 | 2017-08-18 14:34:28 +0000 | [diff] [blame] | 4205 | /* The p2 value always comes from a prior OP_CreateBtree opcode and |
drh | 9a65f2c | 2009-06-22 19:05:40 +0000 | [diff] [blame] | 4206 | ** that opcode will always set the p2 value to 2 or more or else fail. |
| 4207 | ** If there were a failure, the prepared statement would have halted |
| 4208 | ** before reaching this instruction. */ |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 4209 | assert( p2>=2 ); |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 4210 | } |
danielk1977 | d336e22 | 2009-02-20 10:58:41 +0000 | [diff] [blame] | 4211 | if( pOp->p4type==P4_KEYINFO ){ |
| 4212 | pKeyInfo = pOp->p4.pKeyInfo; |
drh | 41e13e1 | 2013-11-07 14:09:39 +0000 | [diff] [blame] | 4213 | assert( pKeyInfo->enc==ENC(db) ); |
| 4214 | assert( pKeyInfo->db==db ); |
drh | a485ad1 | 2017-08-02 22:43:14 +0000 | [diff] [blame] | 4215 | nField = pKeyInfo->nAllField; |
danielk1977 | d336e22 | 2009-02-20 10:58:41 +0000 | [diff] [blame] | 4216 | }else if( pOp->p4type==P4_INT32 ){ |
| 4217 | nField = pOp->p4.i; |
| 4218 | } |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 4219 | assert( pOp->p1>=0 ); |
drh | 399af1d | 2013-11-20 17:25:55 +0000 | [diff] [blame] | 4220 | assert( nField>=0 ); |
| 4221 | testcase( nField==0 ); /* Table with INTEGER PRIMARY KEY and nothing else */ |
drh | b248668 | 2022-01-03 01:43:28 +0000 | [diff] [blame] | 4222 | pCur = allocateCursor(p, pOp->p1, nField, CURTYPE_BTREE); |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 4223 | if( pCur==0 ) goto no_mem; |
drh | b248668 | 2022-01-03 01:43:28 +0000 | [diff] [blame] | 4224 | pCur->iDb = iDb; |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 4225 | pCur->nullRow = 1; |
drh | d4187c7 | 2010-08-30 22:15:45 +0000 | [diff] [blame] | 4226 | pCur->isOrdered = 1; |
drh | 3526319 | 2014-07-22 20:02:19 +0000 | [diff] [blame] | 4227 | pCur->pgnoRoot = p2; |
drh | b89aeb6 | 2016-01-27 15:49:32 +0000 | [diff] [blame] | 4228 | #ifdef SQLITE_DEBUG |
| 4229 | pCur->wrFlag = wrFlag; |
| 4230 | #endif |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 4231 | rc = sqlite3BtreeCursor(pX, p2, wrFlag, pKeyInfo, pCur->uc.pCursor); |
danielk1977 | d336e22 | 2009-02-20 10:58:41 +0000 | [diff] [blame] | 4232 | pCur->pKeyInfo = pKeyInfo; |
drh | 14da87f | 2013-11-20 21:51:33 +0000 | [diff] [blame] | 4233 | /* Set the VdbeCursor.isTable variable. Previous versions of |
danielk1977 | 172114a | 2009-07-07 15:47:12 +0000 | [diff] [blame] | 4234 | ** SQLite used to check if the root-page flags were sane at this point |
| 4235 | ** and report database corruption if they were not, but this check has |
| 4236 | ** since moved into the btree layer. */ |
| 4237 | pCur->isTable = pOp->p4type!=P4_KEYINFO; |
drh | e0997b3 | 2015-03-20 14:57:50 +0000 | [diff] [blame] | 4238 | |
| 4239 | open_cursor_set_hints: |
| 4240 | assert( OPFLAG_BULKCSR==BTREE_BULKLOAD ); |
| 4241 | assert( OPFLAG_SEEKEQ==BTREE_SEEK_EQ ); |
drh | 0403cb3 | 2015-08-14 23:57:04 +0000 | [diff] [blame] | 4242 | testcase( pOp->p5 & OPFLAG_BULKCSR ); |
drh | 0403cb3 | 2015-08-14 23:57:04 +0000 | [diff] [blame] | 4243 | testcase( pOp->p2 & OPFLAG_SEEKEQ ); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 4244 | sqlite3BtreeCursorHintFlags(pCur->uc.pCursor, |
drh | f7854c7 | 2015-10-27 13:24:37 +0000 | [diff] [blame] | 4245 | (pOp->p5 & (OPFLAG_BULKCSR|OPFLAG_SEEKEQ))); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 4246 | if( rc ) goto abort_due_to_error; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4247 | break; |
| 4248 | } |
| 4249 | |
drh | e08e8d6 | 2017-05-01 15:15:41 +0000 | [diff] [blame] | 4250 | /* Opcode: OpenDup P1 P2 * * * |
| 4251 | ** |
| 4252 | ** Open a new cursor P1 that points to the same ephemeral table as |
| 4253 | ** cursor P2. The P2 cursor must have been opened by a prior OP_OpenEphemeral |
| 4254 | ** opcode. Only ephemeral cursors may be duplicated. |
| 4255 | ** |
| 4256 | ** Duplicate ephemeral cursors are used for self-joins of materialized views. |
| 4257 | */ |
| 4258 | case OP_OpenDup: { |
| 4259 | VdbeCursor *pOrig; /* The original cursor to be duplicated */ |
| 4260 | VdbeCursor *pCx; /* The new cursor */ |
| 4261 | |
| 4262 | pOrig = p->apCsr[pOp->p2]; |
dan | 2811ea6 | 2019-12-23 14:20:46 +0000 | [diff] [blame] | 4263 | assert( pOrig ); |
drh | 5a4a15f | 2021-03-18 15:42:59 +0000 | [diff] [blame] | 4264 | assert( pOrig->isEphemeral ); /* Only ephemeral cursors can be duplicated */ |
drh | e08e8d6 | 2017-05-01 15:15:41 +0000 | [diff] [blame] | 4265 | |
drh | b248668 | 2022-01-03 01:43:28 +0000 | [diff] [blame] | 4266 | pCx = allocateCursor(p, pOp->p1, pOrig->nField, CURTYPE_BTREE); |
drh | e08e8d6 | 2017-05-01 15:15:41 +0000 | [diff] [blame] | 4267 | if( pCx==0 ) goto no_mem; |
| 4268 | pCx->nullRow = 1; |
| 4269 | pCx->isEphemeral = 1; |
| 4270 | pCx->pKeyInfo = pOrig->pKeyInfo; |
| 4271 | pCx->isTable = pOrig->isTable; |
drh | 2c04131 | 2018-12-24 02:34:49 +0000 | [diff] [blame] | 4272 | pCx->pgnoRoot = pOrig->pgnoRoot; |
dan | a0f6b83 | 2019-03-14 16:36:20 +0000 | [diff] [blame] | 4273 | pCx->isOrdered = pOrig->isOrdered; |
drh | b248668 | 2022-01-03 01:43:28 +0000 | [diff] [blame] | 4274 | pCx->ub.pBtx = pOrig->ub.pBtx; |
drh | 27a242c | 2022-06-14 22:21:23 +0000 | [diff] [blame] | 4275 | pCx->noReuse = 1; |
| 4276 | pOrig->noReuse = 1; |
drh | b248668 | 2022-01-03 01:43:28 +0000 | [diff] [blame] | 4277 | rc = sqlite3BtreeCursor(pCx->ub.pBtx, pCx->pgnoRoot, BTREE_WRCSR, |
drh | e08e8d6 | 2017-05-01 15:15:41 +0000 | [diff] [blame] | 4278 | pCx->pKeyInfo, pCx->uc.pCursor); |
drh | 3f4df4c | 2017-05-02 17:54:19 +0000 | [diff] [blame] | 4279 | /* The sqlite3BtreeCursor() routine can only fail for the first cursor |
| 4280 | ** opened for a database. Since there is already an open cursor when this |
| 4281 | ** opcode is run, the sqlite3BtreeCursor() cannot fail */ |
| 4282 | assert( rc==SQLITE_OK ); |
drh | e08e8d6 | 2017-05-01 15:15:41 +0000 | [diff] [blame] | 4283 | break; |
| 4284 | } |
| 4285 | |
| 4286 | |
drh | 32881be | 2020-11-17 21:26:13 +0000 | [diff] [blame] | 4287 | /* Opcode: OpenEphemeral P1 P2 P3 P4 P5 |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 4288 | ** Synopsis: nColumn=P2 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4289 | ** |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 4290 | ** Open a new cursor P1 to a transient table. |
drh | 9170dd7 | 2005-07-08 17:13:46 +0000 | [diff] [blame] | 4291 | ** The cursor is always opened read/write even if |
drh | 25d3adb | 2010-04-05 15:11:08 +0000 | [diff] [blame] | 4292 | ** the main database is read-only. The ephemeral |
drh | 9170dd7 | 2005-07-08 17:13:46 +0000 | [diff] [blame] | 4293 | ** table is deleted automatically when the cursor is closed. |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 4294 | ** |
drh | dfe3b58 | 2019-01-04 12:35:50 +0000 | [diff] [blame] | 4295 | ** If the cursor P1 is already opened on an ephemeral table, the table |
drh | 4afdfa1 | 2018-12-31 16:36:42 +0000 | [diff] [blame] | 4296 | ** is cleared (all content is erased). |
| 4297 | ** |
drh | 25d3adb | 2010-04-05 15:11:08 +0000 | [diff] [blame] | 4298 | ** P2 is the number of columns in the ephemeral table. |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 4299 | ** The cursor points to a BTree table if P4==0 and to a BTree index |
| 4300 | ** 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] | 4301 | ** that defines the format of keys in the index. |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 4302 | ** |
drh | 2a5d990 | 2011-08-26 00:34:45 +0000 | [diff] [blame] | 4303 | ** The P5 parameter can be a mask of the BTREE_* flags defined |
| 4304 | ** in btree.h. These flags control aspects of the operation of |
| 4305 | ** the btree. The BTREE_OMIT_JOURNAL and BTREE_SINGLE flags are |
| 4306 | ** added automatically. |
drh | 32881be | 2020-11-17 21:26:13 +0000 | [diff] [blame] | 4307 | ** |
| 4308 | ** If P3 is positive, then reg[P3] is modified slightly so that it |
| 4309 | ** can be used as zero-length data for OP_Insert. This is an optimization |
| 4310 | ** that avoids an extra OP_Blob opcode to initialize that register. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4311 | */ |
drh | a21a64d | 2010-04-06 22:33:55 +0000 | [diff] [blame] | 4312 | /* Opcode: OpenAutoindex P1 P2 * P4 * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 4313 | ** Synopsis: nColumn=P2 |
drh | a21a64d | 2010-04-06 22:33:55 +0000 | [diff] [blame] | 4314 | ** |
| 4315 | ** This opcode works the same as OP_OpenEphemeral. It has a |
| 4316 | ** different name to distinguish its use. Tables created using |
| 4317 | ** by this opcode will be used for automatically created transient |
| 4318 | ** indices in joins. |
| 4319 | */ |
| 4320 | case OP_OpenAutoindex: |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4321 | case OP_OpenEphemeral: { |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 4322 | VdbeCursor *pCx; |
drh | 41e13e1 | 2013-11-07 14:09:39 +0000 | [diff] [blame] | 4323 | KeyInfo *pKeyInfo; |
| 4324 | |
drh | d4187c7 | 2010-08-30 22:15:45 +0000 | [diff] [blame] | 4325 | static const int vfsFlags = |
drh | 33f4e02 | 2007-09-03 15:19:34 +0000 | [diff] [blame] | 4326 | SQLITE_OPEN_READWRITE | |
| 4327 | SQLITE_OPEN_CREATE | |
| 4328 | SQLITE_OPEN_EXCLUSIVE | |
| 4329 | SQLITE_OPEN_DELETEONCLOSE | |
| 4330 | SQLITE_OPEN_TRANSIENT_DB; |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 4331 | assert( pOp->p1>=0 ); |
drh | 399af1d | 2013-11-20 17:25:55 +0000 | [diff] [blame] | 4332 | assert( pOp->p2>=0 ); |
drh | 32881be | 2020-11-17 21:26:13 +0000 | [diff] [blame] | 4333 | if( pOp->p3>0 ){ |
| 4334 | /* Make register reg[P3] into a value that can be used as the data |
| 4335 | ** form sqlite3BtreeInsert() where the length of the data is zero. */ |
| 4336 | assert( pOp->p2==0 ); /* Only used when number of columns is zero */ |
| 4337 | assert( pOp->opcode==OP_OpenEphemeral ); |
| 4338 | assert( aMem[pOp->p3].flags & MEM_Null ); |
| 4339 | aMem[pOp->p3].n = 0; |
| 4340 | aMem[pOp->p3].z = ""; |
| 4341 | } |
drh | 4afdfa1 | 2018-12-31 16:36:42 +0000 | [diff] [blame] | 4342 | pCx = p->apCsr[pOp->p1]; |
drh | 27a242c | 2022-06-14 22:21:23 +0000 | [diff] [blame] | 4343 | if( pCx && !pCx->noReuse && ALWAYS(pOp->p2<=pCx->nField) ){ |
drh | 5a4a15f | 2021-03-18 15:42:59 +0000 | [diff] [blame] | 4344 | /* If the ephermeral table is already open and has no duplicates from |
| 4345 | ** OP_OpenDup, then erase all existing content so that the table is |
| 4346 | ** empty again, rather than creating a new table. */ |
dan | a512972 | 2019-05-03 18:50:24 +0000 | [diff] [blame] | 4347 | assert( pCx->isEphemeral ); |
dan | 855b5d1 | 2019-06-26 21:04:30 +0000 | [diff] [blame] | 4348 | pCx->seqCount = 0; |
| 4349 | pCx->cacheStatus = CACHE_STALE; |
drh | b248668 | 2022-01-03 01:43:28 +0000 | [diff] [blame] | 4350 | rc = sqlite3BtreeClearTable(pCx->ub.pBtx, pCx->pgnoRoot, 0); |
drh | d0fb796 | 2018-12-31 17:58:05 +0000 | [diff] [blame] | 4351 | }else{ |
drh | b248668 | 2022-01-03 01:43:28 +0000 | [diff] [blame] | 4352 | pCx = allocateCursor(p, pOp->p1, pOp->p2, CURTYPE_BTREE); |
drh | d0fb796 | 2018-12-31 17:58:05 +0000 | [diff] [blame] | 4353 | if( pCx==0 ) goto no_mem; |
drh | d0fb796 | 2018-12-31 17:58:05 +0000 | [diff] [blame] | 4354 | pCx->isEphemeral = 1; |
drh | b248668 | 2022-01-03 01:43:28 +0000 | [diff] [blame] | 4355 | rc = sqlite3BtreeOpen(db->pVfs, 0, db, &pCx->ub.pBtx, |
drh | d0fb796 | 2018-12-31 17:58:05 +0000 | [diff] [blame] | 4356 | BTREE_OMIT_JOURNAL | BTREE_SINGLE | pOp->p5, |
| 4357 | vfsFlags); |
| 4358 | if( rc==SQLITE_OK ){ |
drh | b248668 | 2022-01-03 01:43:28 +0000 | [diff] [blame] | 4359 | rc = sqlite3BtreeBeginTrans(pCx->ub.pBtx, 1, 0); |
dan | eeee8a5 | 2021-03-18 14:31:37 +0000 | [diff] [blame] | 4360 | if( rc==SQLITE_OK ){ |
| 4361 | /* If a transient index is required, create it by calling |
| 4362 | ** sqlite3BtreeCreateTable() with the BTREE_BLOBKEY flag before |
| 4363 | ** opening it. If a transient table is required, just use the |
| 4364 | ** automatically created table with root-page 1 (an BLOB_INTKEY table). |
| 4365 | */ |
| 4366 | if( (pCx->pKeyInfo = pKeyInfo = pOp->p4.pKeyInfo)!=0 ){ |
| 4367 | assert( pOp->p4type==P4_KEYINFO ); |
drh | b248668 | 2022-01-03 01:43:28 +0000 | [diff] [blame] | 4368 | rc = sqlite3BtreeCreateTable(pCx->ub.pBtx, &pCx->pgnoRoot, |
dan | eeee8a5 | 2021-03-18 14:31:37 +0000 | [diff] [blame] | 4369 | BTREE_BLOBKEY | pOp->p5); |
| 4370 | if( rc==SQLITE_OK ){ |
| 4371 | assert( pCx->pgnoRoot==SCHEMA_ROOT+1 ); |
| 4372 | assert( pKeyInfo->db==db ); |
| 4373 | assert( pKeyInfo->enc==ENC(db) ); |
drh | b248668 | 2022-01-03 01:43:28 +0000 | [diff] [blame] | 4374 | rc = sqlite3BtreeCursor(pCx->ub.pBtx, pCx->pgnoRoot, BTREE_WRCSR, |
dan | eeee8a5 | 2021-03-18 14:31:37 +0000 | [diff] [blame] | 4375 | pKeyInfo, pCx->uc.pCursor); |
| 4376 | } |
| 4377 | pCx->isTable = 0; |
| 4378 | }else{ |
| 4379 | pCx->pgnoRoot = SCHEMA_ROOT; |
drh | b248668 | 2022-01-03 01:43:28 +0000 | [diff] [blame] | 4380 | rc = sqlite3BtreeCursor(pCx->ub.pBtx, SCHEMA_ROOT, BTREE_WRCSR, |
dan | eeee8a5 | 2021-03-18 14:31:37 +0000 | [diff] [blame] | 4381 | 0, pCx->uc.pCursor); |
| 4382 | pCx->isTable = 1; |
drh | d0fb796 | 2018-12-31 17:58:05 +0000 | [diff] [blame] | 4383 | } |
dan | eeee8a5 | 2021-03-18 14:31:37 +0000 | [diff] [blame] | 4384 | } |
| 4385 | pCx->isOrdered = (pOp->p5!=BTREE_UNORDERED); |
| 4386 | if( rc ){ |
drh | b248668 | 2022-01-03 01:43:28 +0000 | [diff] [blame] | 4387 | sqlite3BtreeClose(pCx->ub.pBtx); |
drh | d0fb796 | 2018-12-31 17:58:05 +0000 | [diff] [blame] | 4388 | } |
| 4389 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4390 | } |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 4391 | if( rc ) goto abort_due_to_error; |
dan | 855b5d1 | 2019-06-26 21:04:30 +0000 | [diff] [blame] | 4392 | pCx->nullRow = 1; |
dan | 5134d13 | 2011-09-02 10:31:11 +0000 | [diff] [blame] | 4393 | break; |
| 4394 | } |
| 4395 | |
dan | fad9f9a | 2014-04-01 18:41:51 +0000 | [diff] [blame] | 4396 | /* Opcode: SorterOpen P1 P2 P3 P4 * |
dan | 5134d13 | 2011-09-02 10:31:11 +0000 | [diff] [blame] | 4397 | ** |
| 4398 | ** This opcode works like OP_OpenEphemeral except that it opens |
| 4399 | ** a transient index that is specifically designed to sort large |
| 4400 | ** tables using an external merge-sort algorithm. |
dan | fad9f9a | 2014-04-01 18:41:51 +0000 | [diff] [blame] | 4401 | ** |
| 4402 | ** If argument P3 is non-zero, then it indicates that the sorter may |
| 4403 | ** assume that a stable sort considering the first P3 fields of each |
| 4404 | ** key is sufficient to produce the required results. |
dan | 5134d13 | 2011-09-02 10:31:11 +0000 | [diff] [blame] | 4405 | */ |
drh | ca892a7 | 2011-09-03 00:17:51 +0000 | [diff] [blame] | 4406 | case OP_SorterOpen: { |
dan | 5134d13 | 2011-09-02 10:31:11 +0000 | [diff] [blame] | 4407 | VdbeCursor *pCx; |
drh | 3a94987 | 2012-09-18 13:20:13 +0000 | [diff] [blame] | 4408 | |
drh | 399af1d | 2013-11-20 17:25:55 +0000 | [diff] [blame] | 4409 | assert( pOp->p1>=0 ); |
| 4410 | assert( pOp->p2>=0 ); |
drh | b248668 | 2022-01-03 01:43:28 +0000 | [diff] [blame] | 4411 | pCx = allocateCursor(p, pOp->p1, pOp->p2, CURTYPE_SORTER); |
dan | 5134d13 | 2011-09-02 10:31:11 +0000 | [diff] [blame] | 4412 | if( pCx==0 ) goto no_mem; |
| 4413 | pCx->pKeyInfo = pOp->p4.pKeyInfo; |
drh | 41e13e1 | 2013-11-07 14:09:39 +0000 | [diff] [blame] | 4414 | assert( pCx->pKeyInfo->db==db ); |
| 4415 | assert( pCx->pKeyInfo->enc==ENC(db) ); |
dan | fad9f9a | 2014-04-01 18:41:51 +0000 | [diff] [blame] | 4416 | rc = sqlite3VdbeSorterInit(db, pOp->p3, pCx); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 4417 | if( rc ) goto abort_due_to_error; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4418 | break; |
| 4419 | } |
| 4420 | |
dan | 78d5843 | 2014-03-25 15:04:07 +0000 | [diff] [blame] | 4421 | /* Opcode: SequenceTest P1 P2 * * * |
| 4422 | ** Synopsis: if( cursor[P1].ctr++ ) pc = P2 |
| 4423 | ** |
| 4424 | ** P1 is a sorter cursor. If the sequence counter is currently zero, jump |
| 4425 | ** to P2. Regardless of whether or not the jump is taken, increment the |
| 4426 | ** the sequence value. |
| 4427 | */ |
| 4428 | case OP_SequenceTest: { |
| 4429 | VdbeCursor *pC; |
| 4430 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 4431 | pC = p->apCsr[pOp->p1]; |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 4432 | assert( isSorter(pC) ); |
dan | 78d5843 | 2014-03-25 15:04:07 +0000 | [diff] [blame] | 4433 | if( (pC->seqCount++)==0 ){ |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 4434 | goto jump_to_p2; |
dan | 78d5843 | 2014-03-25 15:04:07 +0000 | [diff] [blame] | 4435 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4436 | break; |
| 4437 | } |
| 4438 | |
drh | 5f61229 | 2014-02-08 23:20:32 +0000 | [diff] [blame] | 4439 | /* Opcode: OpenPseudo P1 P2 P3 * * |
drh | 60830e3 | 2014-02-10 15:56:34 +0000 | [diff] [blame] | 4440 | ** Synopsis: P3 columns in r[P2] |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 4441 | ** |
| 4442 | ** 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] | 4443 | ** row of data. The content of that one row is the content of memory |
| 4444 | ** register P2. In other words, cursor P1 becomes an alias for the |
| 4445 | ** MEM_Blob content contained in register P2. |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 4446 | ** |
drh | 2d8d7ce | 2010-02-15 15:17:05 +0000 | [diff] [blame] | 4447 | ** A pseudo-table created by this opcode is used to hold a single |
drh | cdd536f | 2006-03-17 00:04:03 +0000 | [diff] [blame] | 4448 | ** row output from the sorter so that the row can be decomposed into |
drh | 3e9ca09 | 2009-09-08 01:14:48 +0000 | [diff] [blame] | 4449 | ** individual columns using the OP_Column opcode. The OP_Column opcode |
| 4450 | ** is the only cursor opcode that works with a pseudo-table. |
danielk1977 | d336e22 | 2009-02-20 10:58:41 +0000 | [diff] [blame] | 4451 | ** |
| 4452 | ** P3 is the number of fields in the records that will be stored by |
| 4453 | ** the pseudo-table. |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 4454 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4455 | case OP_OpenPseudo: { |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 4456 | VdbeCursor *pCx; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 4457 | |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 4458 | assert( pOp->p1>=0 ); |
drh | 399af1d | 2013-11-20 17:25:55 +0000 | [diff] [blame] | 4459 | assert( pOp->p3>=0 ); |
drh | b248668 | 2022-01-03 01:43:28 +0000 | [diff] [blame] | 4460 | pCx = allocateCursor(p, pOp->p1, pOp->p3, CURTYPE_PSEUDO); |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 4461 | if( pCx==0 ) goto no_mem; |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 4462 | pCx->nullRow = 1; |
drh | fe0cf7a | 2017-08-16 19:20:20 +0000 | [diff] [blame] | 4463 | pCx->seekResult = pOp->p2; |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 4464 | pCx->isTable = 1; |
drh | fe0cf7a | 2017-08-16 19:20:20 +0000 | [diff] [blame] | 4465 | /* Give this pseudo-cursor a fake BtCursor pointer so that pCx |
| 4466 | ** can be safely passed to sqlite3VdbeCursorMoveto(). This avoids a test |
| 4467 | ** for pCx->eCurType==CURTYPE_BTREE inside of sqlite3VdbeCursorMoveto() |
| 4468 | ** which is a performance optimization */ |
| 4469 | pCx->uc.pCursor = sqlite3BtreeFakeValidCursor(); |
drh | 5f61229 | 2014-02-08 23:20:32 +0000 | [diff] [blame] | 4470 | assert( pOp->p5==0 ); |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 4471 | break; |
| 4472 | } |
| 4473 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4474 | /* Opcode: Close P1 * * * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4475 | ** |
| 4476 | ** Close a cursor previously opened as P1. If P1 is not |
| 4477 | ** currently open, this instruction is a no-op. |
| 4478 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4479 | case OP_Close: { |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 4480 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 4481 | sqlite3VdbeFreeCursor(p, p->apCsr[pOp->p1]); |
| 4482 | p->apCsr[pOp->p1] = 0; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4483 | break; |
| 4484 | } |
| 4485 | |
drh | 97bae79 | 2015-06-05 15:59:57 +0000 | [diff] [blame] | 4486 | #ifdef SQLITE_ENABLE_COLUMN_USED_MASK |
| 4487 | /* Opcode: ColumnsUsed P1 * * P4 * |
| 4488 | ** |
| 4489 | ** This opcode (which only exists if SQLite was compiled with |
| 4490 | ** SQLITE_ENABLE_COLUMN_USED_MASK) identifies which columns of the |
| 4491 | ** table or index for cursor P1 are used. P4 is a 64-bit integer |
| 4492 | ** (P4_INT64) in which the first 63 bits are one for each of the |
| 4493 | ** first 63 columns of the table or index that are actually used |
| 4494 | ** by the cursor. The high-order bit is set if any column after |
| 4495 | ** the 64th is used. |
| 4496 | */ |
| 4497 | case OP_ColumnsUsed: { |
| 4498 | VdbeCursor *pC; |
| 4499 | pC = p->apCsr[pOp->p1]; |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 4500 | assert( pC->eCurType==CURTYPE_BTREE ); |
drh | 97bae79 | 2015-06-05 15:59:57 +0000 | [diff] [blame] | 4501 | pC->maskUsed = *(u64*)pOp->p4.pI64; |
| 4502 | break; |
| 4503 | } |
| 4504 | #endif |
| 4505 | |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 4506 | /* Opcode: SeekGE P1 P2 P3 P4 * |
drh | f63552b | 2013-10-30 00:25:03 +0000 | [diff] [blame] | 4507 | ** Synopsis: key=r[P3@P4] |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4508 | ** |
danielk1977 | b790c6c | 2008-04-18 10:25:24 +0000 | [diff] [blame] | 4509 | ** 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] | 4510 | ** use the value in register P3 as the key. If cursor P1 refers |
danielk1977 | b790c6c | 2008-04-18 10:25:24 +0000 | [diff] [blame] | 4511 | ** to an SQL index, then P3 is the first in an array of P4 registers |
| 4512 | ** that are used as an unpacked index key. |
| 4513 | ** |
| 4514 | ** Reposition cursor P1 so that it points to the smallest entry that |
| 4515 | ** is greater than or equal to the key value. If there are no records |
| 4516 | ** 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] | 4517 | ** |
drh | b1d607d | 2015-11-05 22:30:54 +0000 | [diff] [blame] | 4518 | ** If the cursor P1 was opened using the OPFLAG_SEEKEQ flag, then this |
drh | 576d0a9 | 2020-03-12 17:28:27 +0000 | [diff] [blame] | 4519 | ** opcode will either land on a record that exactly matches the key, or |
| 4520 | ** else it will cause a jump to P2. When the cursor is OPFLAG_SEEKEQ, |
| 4521 | ** this opcode must be followed by an IdxLE opcode with the same arguments. |
| 4522 | ** The IdxGT opcode will be skipped if this opcode succeeds, but the |
| 4523 | ** IdxGT opcode will be used on subsequent loop iterations. The |
| 4524 | ** OPFLAG_SEEKEQ flags is a hint to the btree layer to say that this |
| 4525 | ** is an equality search. |
drh | b1d607d | 2015-11-05 22:30:54 +0000 | [diff] [blame] | 4526 | ** |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 4527 | ** This opcode leaves the cursor configured to move in forward order, |
drh | bc5cf38 | 2014-08-06 01:08:07 +0000 | [diff] [blame] | 4528 | ** from the beginning toward the end. In other words, the cursor is |
drh | 5dad9a3 | 2014-07-25 18:37:42 +0000 | [diff] [blame] | 4529 | ** configured to use Next, not Prev. |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 4530 | ** |
drh | 935850e | 2014-05-24 17:15:15 +0000 | [diff] [blame] | 4531 | ** See also: Found, NotFound, SeekLt, SeekGt, SeekLe |
drh | 7cf6e4d | 2004-05-19 14:56:55 +0000 | [diff] [blame] | 4532 | */ |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 4533 | /* Opcode: SeekGT P1 P2 P3 P4 * |
drh | f63552b | 2013-10-30 00:25:03 +0000 | [diff] [blame] | 4534 | ** Synopsis: key=r[P3@P4] |
drh | 7cf6e4d | 2004-05-19 14:56:55 +0000 | [diff] [blame] | 4535 | ** |
danielk1977 | b790c6c | 2008-04-18 10:25:24 +0000 | [diff] [blame] | 4536 | ** 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] | 4537 | ** use the value in register P3 as a key. If cursor P1 refers |
danielk1977 | b790c6c | 2008-04-18 10:25:24 +0000 | [diff] [blame] | 4538 | ** to an SQL index, then P3 is the first in an array of P4 registers |
| 4539 | ** that are used as an unpacked index key. |
| 4540 | ** |
drh | 576d0a9 | 2020-03-12 17:28:27 +0000 | [diff] [blame] | 4541 | ** Reposition cursor P1 so that it points to the smallest entry that |
danielk1977 | b790c6c | 2008-04-18 10:25:24 +0000 | [diff] [blame] | 4542 | ** is greater than the key value. If there are no records greater than |
| 4543 | ** the key and P2 is not zero, then jump to P2. |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 4544 | ** |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 4545 | ** This opcode leaves the cursor configured to move in forward order, |
drh | 4ed2fb9 | 2014-08-14 13:06:25 +0000 | [diff] [blame] | 4546 | ** from the beginning toward the end. In other words, the cursor is |
drh | 5dad9a3 | 2014-07-25 18:37:42 +0000 | [diff] [blame] | 4547 | ** configured to use Next, not Prev. |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 4548 | ** |
drh | 935850e | 2014-05-24 17:15:15 +0000 | [diff] [blame] | 4549 | ** See also: Found, NotFound, SeekLt, SeekGe, SeekLe |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4550 | */ |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 4551 | /* Opcode: SeekLT P1 P2 P3 P4 * |
drh | f63552b | 2013-10-30 00:25:03 +0000 | [diff] [blame] | 4552 | ** Synopsis: key=r[P3@P4] |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 4553 | ** |
danielk1977 | b790c6c | 2008-04-18 10:25:24 +0000 | [diff] [blame] | 4554 | ** 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] | 4555 | ** use the value in register P3 as a key. If cursor P1 refers |
danielk1977 | b790c6c | 2008-04-18 10:25:24 +0000 | [diff] [blame] | 4556 | ** to an SQL index, then P3 is the first in an array of P4 registers |
| 4557 | ** that are used as an unpacked index key. |
| 4558 | ** |
| 4559 | ** Reposition cursor P1 so that it points to the largest entry that |
| 4560 | ** is less than the key value. If there are no records less than |
| 4561 | ** the key and P2 is not zero, then jump to P2. |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 4562 | ** |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 4563 | ** This opcode leaves the cursor configured to move in reverse order, |
| 4564 | ** from the end toward the beginning. In other words, the cursor is |
drh | 5dad9a3 | 2014-07-25 18:37:42 +0000 | [diff] [blame] | 4565 | ** configured to use Prev, not Next. |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 4566 | ** |
drh | 935850e | 2014-05-24 17:15:15 +0000 | [diff] [blame] | 4567 | ** See also: Found, NotFound, SeekGt, SeekGe, SeekLe |
drh | 7cf6e4d | 2004-05-19 14:56:55 +0000 | [diff] [blame] | 4568 | */ |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 4569 | /* Opcode: SeekLE P1 P2 P3 P4 * |
drh | f63552b | 2013-10-30 00:25:03 +0000 | [diff] [blame] | 4570 | ** Synopsis: key=r[P3@P4] |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 4571 | ** |
danielk1977 | b790c6c | 2008-04-18 10:25:24 +0000 | [diff] [blame] | 4572 | ** 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] | 4573 | ** use the value in register P3 as a key. If cursor P1 refers |
danielk1977 | b790c6c | 2008-04-18 10:25:24 +0000 | [diff] [blame] | 4574 | ** to an SQL index, then P3 is the first in an array of P4 registers |
| 4575 | ** that are used as an unpacked index key. |
danielk1977 | 751de56 | 2008-04-18 09:01:15 +0000 | [diff] [blame] | 4576 | ** |
danielk1977 | b790c6c | 2008-04-18 10:25:24 +0000 | [diff] [blame] | 4577 | ** Reposition cursor P1 so that it points to the largest entry that |
| 4578 | ** is less than or equal to the key value. If there are no records |
| 4579 | ** 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] | 4580 | ** |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 4581 | ** This opcode leaves the cursor configured to move in reverse order, |
| 4582 | ** from the end toward the beginning. In other words, the cursor is |
drh | 5dad9a3 | 2014-07-25 18:37:42 +0000 | [diff] [blame] | 4583 | ** configured to use Prev, not Next. |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 4584 | ** |
drh | b1d607d | 2015-11-05 22:30:54 +0000 | [diff] [blame] | 4585 | ** If the cursor P1 was opened using the OPFLAG_SEEKEQ flag, then this |
drh | 576d0a9 | 2020-03-12 17:28:27 +0000 | [diff] [blame] | 4586 | ** opcode will either land on a record that exactly matches the key, or |
| 4587 | ** else it will cause a jump to P2. When the cursor is OPFLAG_SEEKEQ, |
| 4588 | ** this opcode must be followed by an IdxLE opcode with the same arguments. |
drh | b1d607d | 2015-11-05 22:30:54 +0000 | [diff] [blame] | 4589 | ** The IdxGE opcode will be skipped if this opcode succeeds, but the |
drh | 576d0a9 | 2020-03-12 17:28:27 +0000 | [diff] [blame] | 4590 | ** IdxGE opcode will be used on subsequent loop iterations. The |
| 4591 | ** OPFLAG_SEEKEQ flags is a hint to the btree layer to say that this |
| 4592 | ** is an equality search. |
drh | b1d607d | 2015-11-05 22:30:54 +0000 | [diff] [blame] | 4593 | ** |
drh | 935850e | 2014-05-24 17:15:15 +0000 | [diff] [blame] | 4594 | ** See also: Found, NotFound, SeekGt, SeekGe, SeekLt |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 4595 | */ |
mistachkin | 758784d | 2018-07-25 15:12:29 +0000 | [diff] [blame] | 4596 | case OP_SeekLT: /* jump, in3, group */ |
| 4597 | case OP_SeekLE: /* jump, in3, group */ |
| 4598 | case OP_SeekGE: /* jump, in3, group */ |
| 4599 | case OP_SeekGT: { /* jump, in3, group */ |
drh | b1d607d | 2015-11-05 22:30:54 +0000 | [diff] [blame] | 4600 | int res; /* Comparison result */ |
| 4601 | int oc; /* Opcode */ |
| 4602 | VdbeCursor *pC; /* The cursor to seek */ |
| 4603 | UnpackedRecord r; /* The key to seek for */ |
| 4604 | int nField; /* Number of columns or fields in the key */ |
| 4605 | i64 iKey; /* The rowid we are to seek to */ |
drh | d6b7946 | 2015-11-07 01:19:00 +0000 | [diff] [blame] | 4606 | int eqOnly; /* Only interested in == results */ |
drh | 80ff32f | 2001-11-04 18:32:46 +0000 | [diff] [blame] | 4607 | |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 4608 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
drh | 959403f | 2008-12-12 17:56:16 +0000 | [diff] [blame] | 4609 | assert( pOp->p2!=0 ); |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 4610 | pC = p->apCsr[pOp->p1]; |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 4611 | assert( pC!=0 ); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 4612 | assert( pC->eCurType==CURTYPE_BTREE ); |
drh | 4a1d365 | 2014-02-14 15:13:36 +0000 | [diff] [blame] | 4613 | assert( OP_SeekLE == OP_SeekLT+1 ); |
| 4614 | assert( OP_SeekGE == OP_SeekLT+2 ); |
| 4615 | assert( OP_SeekGT == OP_SeekLT+3 ); |
drh | d4187c7 | 2010-08-30 22:15:45 +0000 | [diff] [blame] | 4616 | assert( pC->isOrdered ); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 4617 | assert( pC->uc.pCursor!=0 ); |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 4618 | oc = pOp->opcode; |
drh | d6b7946 | 2015-11-07 01:19:00 +0000 | [diff] [blame] | 4619 | eqOnly = 0; |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 4620 | pC->nullRow = 0; |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 4621 | #ifdef SQLITE_DEBUG |
| 4622 | pC->seekOp = pOp->opcode; |
| 4623 | #endif |
drh | e0997b3 | 2015-03-20 14:57:50 +0000 | [diff] [blame] | 4624 | |
dan | a40cb96 | 2019-05-14 20:25:22 +0000 | [diff] [blame] | 4625 | pC->deferredMoveto = 0; |
| 4626 | pC->cacheStatus = CACHE_STALE; |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 4627 | if( pC->isTable ){ |
drh | 3e36480 | 2019-08-22 00:53:16 +0000 | [diff] [blame] | 4628 | u16 flags3, newType; |
drh | 576d0a9 | 2020-03-12 17:28:27 +0000 | [diff] [blame] | 4629 | /* The OPFLAG_SEEKEQ/BTREE_SEEK_EQ flag is only set on index cursors */ |
drh | 218c66e | 2016-12-27 12:35:36 +0000 | [diff] [blame] | 4630 | assert( sqlite3BtreeCursorHasHint(pC->uc.pCursor, BTREE_SEEK_EQ)==0 |
| 4631 | || CORRUPT_DB ); |
drh | d6b7946 | 2015-11-07 01:19:00 +0000 | [diff] [blame] | 4632 | |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 4633 | /* The input value in P3 might be of any type: integer, real, string, |
| 4634 | ** 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] | 4635 | ** the seek, so convert it. */ |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 4636 | pIn3 = &aMem[pOp->p3]; |
drh | 3e36480 | 2019-08-22 00:53:16 +0000 | [diff] [blame] | 4637 | flags3 = pIn3->flags; |
| 4638 | if( (flags3 & (MEM_Int|MEM_Real|MEM_IntReal|MEM_Str))==MEM_Str ){ |
drh | bd9507c | 2014-08-23 17:21:37 +0000 | [diff] [blame] | 4639 | applyNumericAffinity(pIn3, 0); |
| 4640 | } |
drh | 3e36480 | 2019-08-22 00:53:16 +0000 | [diff] [blame] | 4641 | iKey = sqlite3VdbeIntValue(pIn3); /* Get the integer key value */ |
| 4642 | newType = pIn3->flags; /* Record the type after applying numeric affinity */ |
| 4643 | pIn3->flags = flags3; /* But convert the type back to its original */ |
drh | 959403f | 2008-12-12 17:56:16 +0000 | [diff] [blame] | 4644 | |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 4645 | /* If the P3 value could not be converted into an integer without |
| 4646 | ** loss of information, then special processing is required... */ |
drh | 3e36480 | 2019-08-22 00:53:16 +0000 | [diff] [blame] | 4647 | if( (newType & (MEM_Int|MEM_IntReal))==0 ){ |
drh | de32461 | 2021-07-19 20:52:31 +0000 | [diff] [blame] | 4648 | int c; |
drh | 3e36480 | 2019-08-22 00:53:16 +0000 | [diff] [blame] | 4649 | if( (newType & MEM_Real)==0 ){ |
| 4650 | if( (newType & MEM_Null) || oc>=OP_SeekGE ){ |
drh | 8616cff | 2019-07-13 16:15:23 +0000 | [diff] [blame] | 4651 | VdbeBranchTaken(1,2); |
| 4652 | goto jump_to_p2; |
dan | 9edd8c1 | 2019-05-08 11:42:49 +0000 | [diff] [blame] | 4653 | }else{ |
dan | 873b019 | 2019-05-09 11:19:27 +0000 | [diff] [blame] | 4654 | rc = sqlite3BtreeLast(pC->uc.pCursor, &res); |
| 4655 | if( rc!=SQLITE_OK ) goto abort_due_to_error; |
dan | 9edd8c1 | 2019-05-08 11:42:49 +0000 | [diff] [blame] | 4656 | goto seek_not_found; |
| 4657 | } |
drh | de32461 | 2021-07-19 20:52:31 +0000 | [diff] [blame] | 4658 | } |
| 4659 | c = sqlite3IntFloatCompare(iKey, pIn3->u.r); |
drh | 959403f | 2008-12-12 17:56:16 +0000 | [diff] [blame] | 4660 | |
dan | aa1776f | 2013-11-26 18:22:59 +0000 | [diff] [blame] | 4661 | /* If the approximation iKey is larger than the actual real search |
| 4662 | ** term, substitute >= for > and < for <=. e.g. if the search term |
| 4663 | ** is 4.9 and the integer approximation 5: |
| 4664 | ** |
| 4665 | ** (x > 4.9) -> (x >= 5) |
| 4666 | ** (x <= 4.9) -> (x < 5) |
| 4667 | */ |
drh | de32461 | 2021-07-19 20:52:31 +0000 | [diff] [blame] | 4668 | if( c>0 ){ |
drh | 4a1d365 | 2014-02-14 15:13:36 +0000 | [diff] [blame] | 4669 | assert( OP_SeekGE==(OP_SeekGT-1) ); |
| 4670 | assert( OP_SeekLT==(OP_SeekLE-1) ); |
| 4671 | assert( (OP_SeekLE & 0x0001)==(OP_SeekGT & 0x0001) ); |
| 4672 | if( (oc & 0x0001)==(OP_SeekGT & 0x0001) ) oc--; |
dan | aa1776f | 2013-11-26 18:22:59 +0000 | [diff] [blame] | 4673 | } |
| 4674 | |
| 4675 | /* If the approximation iKey is smaller than the actual real search |
| 4676 | ** term, substitute <= for < and > for >=. */ |
drh | de32461 | 2021-07-19 20:52:31 +0000 | [diff] [blame] | 4677 | else if( c<0 ){ |
drh | 4a1d365 | 2014-02-14 15:13:36 +0000 | [diff] [blame] | 4678 | assert( OP_SeekLE==(OP_SeekLT+1) ); |
| 4679 | assert( OP_SeekGT==(OP_SeekGE+1) ); |
| 4680 | assert( (OP_SeekLT & 0x0001)==(OP_SeekGE & 0x0001) ); |
| 4681 | if( (oc & 0x0001)==(OP_SeekLT & 0x0001) ) oc++; |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 4682 | } |
dan | 9edd8c1 | 2019-05-08 11:42:49 +0000 | [diff] [blame] | 4683 | } |
drh | 42a410d | 2021-06-19 18:32:20 +0000 | [diff] [blame] | 4684 | rc = sqlite3BtreeTableMoveto(pC->uc.pCursor, (u64)iKey, 0, &res); |
drh | b53a5a9 | 2014-10-12 22:37:22 +0000 | [diff] [blame] | 4685 | pC->movetoTarget = iKey; /* Used by OP_Delete */ |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 4686 | if( rc!=SQLITE_OK ){ |
| 4687 | goto abort_due_to_error; |
drh | 1af3fdb | 2004-07-18 21:33:01 +0000 | [diff] [blame] | 4688 | } |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 4689 | }else{ |
drh | 576d0a9 | 2020-03-12 17:28:27 +0000 | [diff] [blame] | 4690 | /* For a cursor with the OPFLAG_SEEKEQ/BTREE_SEEK_EQ hint, only the |
| 4691 | ** OP_SeekGE and OP_SeekLE opcodes are allowed, and these must be |
| 4692 | ** immediately followed by an OP_IdxGT or OP_IdxLT opcode, respectively, |
| 4693 | ** with the same key. |
drh | d6b7946 | 2015-11-07 01:19:00 +0000 | [diff] [blame] | 4694 | */ |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 4695 | if( sqlite3BtreeCursorHasHint(pC->uc.pCursor, BTREE_SEEK_EQ) ){ |
drh | d6b7946 | 2015-11-07 01:19:00 +0000 | [diff] [blame] | 4696 | eqOnly = 1; |
| 4697 | assert( pOp->opcode==OP_SeekGE || pOp->opcode==OP_SeekLE ); |
| 4698 | assert( pOp[1].opcode==OP_IdxLT || pOp[1].opcode==OP_IdxGT ); |
drh | 576d0a9 | 2020-03-12 17:28:27 +0000 | [diff] [blame] | 4699 | assert( pOp->opcode==OP_SeekGE || pOp[1].opcode==OP_IdxLT ); |
| 4700 | assert( pOp->opcode==OP_SeekLE || pOp[1].opcode==OP_IdxGT ); |
drh | d6b7946 | 2015-11-07 01:19:00 +0000 | [diff] [blame] | 4701 | assert( pOp[1].p1==pOp[0].p1 ); |
| 4702 | assert( pOp[1].p2==pOp[0].p2 ); |
| 4703 | assert( pOp[1].p3==pOp[0].p3 ); |
| 4704 | assert( pOp[1].p4.i==pOp[0].p4.i ); |
| 4705 | } |
| 4706 | |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 4707 | nField = pOp->p4.i; |
| 4708 | assert( pOp->p4type==P4_INT32 ); |
| 4709 | assert( nField>0 ); |
| 4710 | r.pKeyInfo = pC->pKeyInfo; |
| 4711 | r.nField = (u16)nField; |
| 4712 | |
| 4713 | /* The next line of code computes as follows, only faster: |
drh | 4a1d365 | 2014-02-14 15:13:36 +0000 | [diff] [blame] | 4714 | ** if( oc==OP_SeekGT || oc==OP_SeekLE ){ |
dan | 1fed5da | 2014-02-25 21:01:25 +0000 | [diff] [blame] | 4715 | ** r.default_rc = -1; |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 4716 | ** }else{ |
dan | 1fed5da | 2014-02-25 21:01:25 +0000 | [diff] [blame] | 4717 | ** r.default_rc = +1; |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 4718 | ** } |
danielk1977 | f7b9d66 | 2008-06-23 18:49:43 +0000 | [diff] [blame] | 4719 | */ |
dan | 1fed5da | 2014-02-25 21:01:25 +0000 | [diff] [blame] | 4720 | r.default_rc = ((1 & (oc - OP_SeekLT)) ? -1 : +1); |
| 4721 | assert( oc!=OP_SeekGT || r.default_rc==-1 ); |
| 4722 | assert( oc!=OP_SeekLE || r.default_rc==-1 ); |
| 4723 | assert( oc!=OP_SeekGE || r.default_rc==+1 ); |
| 4724 | assert( oc!=OP_SeekLT || r.default_rc==+1 ); |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 4725 | |
| 4726 | r.aMem = &aMem[pOp->p3]; |
| 4727 | #ifdef SQLITE_DEBUG |
drh | 8a2254f | 2022-10-07 15:55:35 +0000 | [diff] [blame] | 4728 | { |
| 4729 | int i; |
| 4730 | for(i=0; i<r.nField; i++){ |
| 4731 | assert( memIsValid(&r.aMem[i]) ); |
| 4732 | if( i>0 ) REGISTER_TRACE(pOp->p3+i, &r.aMem[i]); |
| 4733 | } |
| 4734 | } |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 4735 | #endif |
drh | 70528d7 | 2015-11-05 20:25:09 +0000 | [diff] [blame] | 4736 | r.eqSeen = 0; |
drh | 42a410d | 2021-06-19 18:32:20 +0000 | [diff] [blame] | 4737 | rc = sqlite3BtreeIndexMoveto(pC->uc.pCursor, &r, &res); |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 4738 | if( rc!=SQLITE_OK ){ |
| 4739 | goto abort_due_to_error; |
| 4740 | } |
drh | b1d607d | 2015-11-05 22:30:54 +0000 | [diff] [blame] | 4741 | if( eqOnly && r.eqSeen==0 ){ |
| 4742 | assert( res!=0 ); |
| 4743 | goto seek_not_found; |
drh | 70528d7 | 2015-11-05 20:25:09 +0000 | [diff] [blame] | 4744 | } |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 4745 | } |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 4746 | #ifdef SQLITE_TEST |
| 4747 | sqlite3_search_count++; |
| 4748 | #endif |
drh | 4a1d365 | 2014-02-14 15:13:36 +0000 | [diff] [blame] | 4749 | if( oc>=OP_SeekGE ){ assert( oc==OP_SeekGE || oc==OP_SeekGT ); |
| 4750 | if( res<0 || (res==0 && oc==OP_SeekGT) ){ |
drh | e39a732 | 2014-02-03 14:04:11 +0000 | [diff] [blame] | 4751 | res = 0; |
drh | 2ab792e | 2017-05-30 18:34:07 +0000 | [diff] [blame] | 4752 | rc = sqlite3BtreeNext(pC->uc.pCursor, 0); |
| 4753 | if( rc!=SQLITE_OK ){ |
| 4754 | if( rc==SQLITE_DONE ){ |
| 4755 | rc = SQLITE_OK; |
| 4756 | res = 1; |
| 4757 | }else{ |
| 4758 | goto abort_due_to_error; |
| 4759 | } |
| 4760 | } |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 4761 | }else{ |
| 4762 | res = 0; |
| 4763 | } |
| 4764 | }else{ |
drh | 4a1d365 | 2014-02-14 15:13:36 +0000 | [diff] [blame] | 4765 | assert( oc==OP_SeekLT || oc==OP_SeekLE ); |
| 4766 | if( res>0 || (res==0 && oc==OP_SeekLT) ){ |
drh | e39a732 | 2014-02-03 14:04:11 +0000 | [diff] [blame] | 4767 | res = 0; |
drh | 2ab792e | 2017-05-30 18:34:07 +0000 | [diff] [blame] | 4768 | rc = sqlite3BtreePrevious(pC->uc.pCursor, 0); |
| 4769 | if( rc!=SQLITE_OK ){ |
| 4770 | if( rc==SQLITE_DONE ){ |
| 4771 | rc = SQLITE_OK; |
| 4772 | res = 1; |
| 4773 | }else{ |
| 4774 | goto abort_due_to_error; |
| 4775 | } |
| 4776 | } |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 4777 | }else{ |
| 4778 | /* res might be negative because the table is empty. Check to |
| 4779 | ** see if this is the case. |
| 4780 | */ |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 4781 | res = sqlite3BtreeEof(pC->uc.pCursor); |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 4782 | } |
| 4783 | } |
drh | b1d607d | 2015-11-05 22:30:54 +0000 | [diff] [blame] | 4784 | seek_not_found: |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 4785 | assert( pOp->p2>0 ); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 4786 | VdbeBranchTaken(res!=0,2); |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 4787 | if( res ){ |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 4788 | goto jump_to_p2; |
drh | b1d607d | 2015-11-05 22:30:54 +0000 | [diff] [blame] | 4789 | }else if( eqOnly ){ |
| 4790 | assert( pOp[1].opcode==OP_IdxLT || pOp[1].opcode==OP_IdxGT ); |
| 4791 | pOp++; /* Skip the OP_IdxLt or OP_IdxGT that follows */ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4792 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4793 | break; |
| 4794 | } |
dan | 71c57db | 2016-07-09 20:23:55 +0000 | [diff] [blame] | 4795 | |
drh | 68cf0ac | 2020-09-28 19:51:54 +0000 | [diff] [blame] | 4796 | |
dan | 73c586b | 2022-10-07 18:57:15 +0000 | [diff] [blame] | 4797 | /* Opcode: SeekScan P1 P2 * * P5 |
drh | 68cf0ac | 2020-09-28 19:51:54 +0000 | [diff] [blame] | 4798 | ** Synopsis: Scan-ahead up to P1 rows |
| 4799 | ** |
drh | dfbaae7 | 2020-09-29 17:29:11 +0000 | [diff] [blame] | 4800 | ** This opcode is a prefix opcode to OP_SeekGE. In other words, this |
drh | 04e70ce | 2020-10-02 11:55:07 +0000 | [diff] [blame] | 4801 | ** opcode must be immediately followed by OP_SeekGE. This constraint is |
drh | dfbaae7 | 2020-09-29 17:29:11 +0000 | [diff] [blame] | 4802 | ** checked by assert() statements. |
| 4803 | ** |
| 4804 | ** This opcode uses the P1 through P4 operands of the subsequent |
| 4805 | ** OP_SeekGE. In the text that follows, the operands of the subsequent |
| 4806 | ** OP_SeekGE opcode are denoted as SeekOP.P1 through SeekOP.P4. Only |
dan | 73c586b | 2022-10-07 18:57:15 +0000 | [diff] [blame] | 4807 | ** the P1, P2 and P5 operands of this opcode are also used, and are called |
| 4808 | ** This.P1, This.P2 and This.P5. |
drh | 68cf0ac | 2020-09-28 19:51:54 +0000 | [diff] [blame] | 4809 | ** |
| 4810 | ** This opcode helps to optimize IN operators on a multi-column index |
drh | dfbaae7 | 2020-09-29 17:29:11 +0000 | [diff] [blame] | 4811 | ** where the IN operator is on the later terms of the index by avoiding |
| 4812 | ** unnecessary seeks on the btree, substituting steps to the next row |
| 4813 | ** of the b-tree instead. A correct answer is obtained if this opcode |
| 4814 | ** is omitted or is a no-op. |
drh | 68cf0ac | 2020-09-28 19:51:54 +0000 | [diff] [blame] | 4815 | ** |
drh | dfbaae7 | 2020-09-29 17:29:11 +0000 | [diff] [blame] | 4816 | ** The SeekGE.P3 and SeekGE.P4 operands identify an unpacked key which |
| 4817 | ** is the desired entry that we want the cursor SeekGE.P1 to be pointing |
dan | 73c586b | 2022-10-07 18:57:15 +0000 | [diff] [blame] | 4818 | ** to. Call this SeekGE.P3/P4 row the "target". |
drh | 68cf0ac | 2020-09-28 19:51:54 +0000 | [diff] [blame] | 4819 | ** |
drh | a54e1b1 | 2020-09-29 23:52:25 +0000 | [diff] [blame] | 4820 | ** If the SeekGE.P1 cursor is not currently pointing to a valid row, |
| 4821 | ** 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] | 4822 | ** |
drh | dfbaae7 | 2020-09-29 17:29:11 +0000 | [diff] [blame] | 4823 | ** If the SeekGE.P1 cursor is pointing to a valid row, then that row |
| 4824 | ** might be the target row, or it might be near and slightly before the |
dan | 73c586b | 2022-10-07 18:57:15 +0000 | [diff] [blame] | 4825 | ** target row, or it might be after the target row. If the cursor is |
| 4826 | ** currently before the target row, then this opcode attempts to position |
| 4827 | ** the cursor on or after the target row by invoking sqlite3BtreeStep() |
| 4828 | ** on the cursor between 1 and This.P1 times. |
drh | 68cf0ac | 2020-09-28 19:51:54 +0000 | [diff] [blame] | 4829 | ** |
dan | 73c586b | 2022-10-07 18:57:15 +0000 | [diff] [blame] | 4830 | ** The This.P5 parameter is a flag that indicates what to do if the |
| 4831 | ** cursor ends up pointing at a valid row that is past the target |
| 4832 | ** row. If This.P5 is false (0) then a jump is made to SeekGE.P2. If |
| 4833 | ** This.P5 is true (non-zero) then a jump is made to This.P2. The P5==0 |
| 4834 | ** case occurs when there are no inequality constraints to the right of |
| 4835 | ** the IN constraing. The jump to SeekGE.P2 ends the loop. The P5!=0 case |
| 4836 | ** occurs when there are inequality constraints to the right of the IN |
| 4837 | ** operator. In that case, the This.P2 will point either directly to or |
| 4838 | ** to setup code prior to the OP_IdxGT or OP_IdxGE opcode that checks for |
| 4839 | ** loop terminate. |
drh | 68cf0ac | 2020-09-28 19:51:54 +0000 | [diff] [blame] | 4840 | ** |
dan | 73c586b | 2022-10-07 18:57:15 +0000 | [diff] [blame] | 4841 | ** Possible outcomes from this opcode:<ol> |
drh | 68cf0ac | 2020-09-28 19:51:54 +0000 | [diff] [blame] | 4842 | ** |
dan | 73c586b | 2022-10-07 18:57:15 +0000 | [diff] [blame] | 4843 | ** <li> If the cursor is initally not pointed to any valid row, then |
| 4844 | ** fall through into the subsequent OP_SeekGE opcode. |
drh | dfbaae7 | 2020-09-29 17:29:11 +0000 | [diff] [blame] | 4845 | ** |
dan | 73c586b | 2022-10-07 18:57:15 +0000 | [diff] [blame] | 4846 | ** <li> If the cursor is left pointing to a row that is before the target |
| 4847 | ** row, even after making as many as This.P1 calls to |
| 4848 | ** sqlite3BtreeNext(), then also fall through into OP_SeekGE. |
| 4849 | ** |
| 4850 | ** <li> If the cursor is left pointing at the target row, either because it |
| 4851 | ** was at the target row to begin with or because one or more |
| 4852 | ** sqlite3BtreeNext() calls moved the cursor to the target row, |
| 4853 | ** then jump to This.P2.., |
| 4854 | ** |
| 4855 | ** <li> If the cursor started out before the target row and a call to |
| 4856 | ** to sqlite3BtreeNext() moved the cursor off the end of the index |
| 4857 | ** (indicating that the target row definitely does not exist in the |
| 4858 | ** btree) then jump to SeekGE.P2, ending the loop. |
| 4859 | ** |
| 4860 | ** <li> If the cursor ends up on a valid row that is past the target row |
| 4861 | ** (indicating that the target row does not exist in the btree) then |
| 4862 | ** jump to SeekOP.P2 if This.P5==0 or to This.P2 if This.P5>0. |
drh | dfbaae7 | 2020-09-29 17:29:11 +0000 | [diff] [blame] | 4863 | ** </ol> |
drh | 68cf0ac | 2020-09-28 19:51:54 +0000 | [diff] [blame] | 4864 | */ |
| 4865 | case OP_SeekScan: { |
drh | f761d93 | 2020-09-29 01:48:46 +0000 | [diff] [blame] | 4866 | VdbeCursor *pC; |
| 4867 | int res; |
drh | deaa610 | 2020-10-01 15:46:21 +0000 | [diff] [blame] | 4868 | int nStep; |
drh | f761d93 | 2020-09-29 01:48:46 +0000 | [diff] [blame] | 4869 | UnpackedRecord r; |
| 4870 | |
drh | 68cf0ac | 2020-09-28 19:51:54 +0000 | [diff] [blame] | 4871 | assert( pOp[1].opcode==OP_SeekGE ); |
drh | 04e70ce | 2020-10-02 11:55:07 +0000 | [diff] [blame] | 4872 | |
dan | 73c586b | 2022-10-07 18:57:15 +0000 | [diff] [blame] | 4873 | /* If pOp->p5 is clear, then pOp->p2 points to the first instruction past the |
| 4874 | ** OP_IdxGT that follows the OP_SeekGE. Otherwise, it points to the first |
| 4875 | ** opcode past the OP_SeekGE itself. */ |
drh | 04e70ce | 2020-10-02 11:55:07 +0000 | [diff] [blame] | 4876 | assert( pOp->p2>=(int)(pOp-aOp)+2 ); |
dan | 73c586b | 2022-10-07 18:57:15 +0000 | [diff] [blame] | 4877 | #ifdef SQLITE_DEBUG |
| 4878 | if( pOp->p5==0 ){ |
| 4879 | /* There are no inequality constraints following the IN constraint. */ |
| 4880 | assert( pOp[1].p1==aOp[pOp->p2-1].p1 ); |
| 4881 | assert( pOp[1].p2==aOp[pOp->p2-1].p2 ); |
| 4882 | assert( pOp[1].p3==aOp[pOp->p2-1].p3 ); |
| 4883 | assert( aOp[pOp->p2-1].opcode==OP_IdxGT |
| 4884 | || aOp[pOp->p2-1].opcode==OP_IdxGE ); |
| 4885 | testcase( aOp[pOp->p2-1].opcode==OP_IdxGE ); |
| 4886 | }else{ |
| 4887 | /* There are inequality constraints. */ |
| 4888 | assert( pOp->p2==(int)(pOp-aOp)+2 ); |
| 4889 | assert( aOp[pOp->p2-1].opcode==OP_SeekGE ); |
| 4890 | } |
| 4891 | #endif |
drh | 04e70ce | 2020-10-02 11:55:07 +0000 | [diff] [blame] | 4892 | |
drh | 68cf0ac | 2020-09-28 19:51:54 +0000 | [diff] [blame] | 4893 | assert( pOp->p1>0 ); |
drh | f761d93 | 2020-09-29 01:48:46 +0000 | [diff] [blame] | 4894 | pC = p->apCsr[pOp[1].p1]; |
| 4895 | assert( pC!=0 ); |
| 4896 | assert( pC->eCurType==CURTYPE_BTREE ); |
| 4897 | assert( !pC->isTable ); |
drh | a54e1b1 | 2020-09-29 23:52:25 +0000 | [diff] [blame] | 4898 | if( !sqlite3BtreeCursorIsValidNN(pC->uc.pCursor) ){ |
drh | f761d93 | 2020-09-29 01:48:46 +0000 | [diff] [blame] | 4899 | #ifdef SQLITE_DEBUG |
| 4900 | if( db->flags&SQLITE_VdbeTrace ){ |
drh | a54e1b1 | 2020-09-29 23:52:25 +0000 | [diff] [blame] | 4901 | printf("... cursor not valid - fall through\n"); |
drh | f761d93 | 2020-09-29 01:48:46 +0000 | [diff] [blame] | 4902 | } |
| 4903 | #endif |
| 4904 | break; |
| 4905 | } |
drh | deaa610 | 2020-10-01 15:46:21 +0000 | [diff] [blame] | 4906 | nStep = pOp->p1; |
| 4907 | assert( nStep>=1 ); |
drh | f761d93 | 2020-09-29 01:48:46 +0000 | [diff] [blame] | 4908 | r.pKeyInfo = pC->pKeyInfo; |
| 4909 | r.nField = (u16)pOp[1].p4.i; |
| 4910 | r.default_rc = 0; |
| 4911 | r.aMem = &aMem[pOp[1].p3]; |
| 4912 | #ifdef SQLITE_DEBUG |
| 4913 | { |
| 4914 | int i; |
| 4915 | for(i=0; i<r.nField; i++){ |
| 4916 | assert( memIsValid(&r.aMem[i]) ); |
| 4917 | REGISTER_TRACE(pOp[1].p3+i, &aMem[pOp[1].p3+i]); |
| 4918 | } |
| 4919 | } |
| 4920 | #endif |
| 4921 | res = 0; /* Not needed. Only used to silence a warning. */ |
| 4922 | while(1){ |
| 4923 | rc = sqlite3VdbeIdxKeyCompare(db, pC, &r, &res); |
| 4924 | if( rc ) goto abort_due_to_error; |
dan | 73c586b | 2022-10-07 18:57:15 +0000 | [diff] [blame] | 4925 | if( res>0 && pOp->p5==0 ){ |
drh | 0b2949c | 2020-09-29 20:22:19 +0000 | [diff] [blame] | 4926 | seekscan_search_fail: |
dan | 73c586b | 2022-10-07 18:57:15 +0000 | [diff] [blame] | 4927 | /* Jump to SeekGE.P2, ending the loop */ |
drh | f761d93 | 2020-09-29 01:48:46 +0000 | [diff] [blame] | 4928 | #ifdef SQLITE_DEBUG |
drh | 0b2949c | 2020-09-29 20:22:19 +0000 | [diff] [blame] | 4929 | if( db->flags&SQLITE_VdbeTrace ){ |
drh | deaa610 | 2020-10-01 15:46:21 +0000 | [diff] [blame] | 4930 | printf("... %d steps and then skip\n", pOp->p1 - nStep); |
drh | 0b2949c | 2020-09-29 20:22:19 +0000 | [diff] [blame] | 4931 | } |
drh | f761d93 | 2020-09-29 01:48:46 +0000 | [diff] [blame] | 4932 | #endif |
drh | 0b2949c | 2020-09-29 20:22:19 +0000 | [diff] [blame] | 4933 | VdbeBranchTaken(1,3); |
drh | f287d00 | 2020-09-30 00:10:22 +0000 | [diff] [blame] | 4934 | pOp++; |
drh | f761d93 | 2020-09-29 01:48:46 +0000 | [diff] [blame] | 4935 | goto jump_to_p2; |
| 4936 | } |
dan | 73c586b | 2022-10-07 18:57:15 +0000 | [diff] [blame] | 4937 | if( res>=0 ){ |
| 4938 | /* Jump to This.P2, bypassing the OP_SeekGE opcode */ |
drh | f761d93 | 2020-09-29 01:48:46 +0000 | [diff] [blame] | 4939 | #ifdef SQLITE_DEBUG |
drh | 0b2949c | 2020-09-29 20:22:19 +0000 | [diff] [blame] | 4940 | if( db->flags&SQLITE_VdbeTrace ){ |
drh | deaa610 | 2020-10-01 15:46:21 +0000 | [diff] [blame] | 4941 | printf("... %d steps and then success\n", pOp->p1 - nStep); |
drh | 0b2949c | 2020-09-29 20:22:19 +0000 | [diff] [blame] | 4942 | } |
drh | f761d93 | 2020-09-29 01:48:46 +0000 | [diff] [blame] | 4943 | #endif |
drh | 0b2949c | 2020-09-29 20:22:19 +0000 | [diff] [blame] | 4944 | VdbeBranchTaken(2,3); |
drh | 04e70ce | 2020-10-02 11:55:07 +0000 | [diff] [blame] | 4945 | goto jump_to_p2; |
drh | f761d93 | 2020-09-29 01:48:46 +0000 | [diff] [blame] | 4946 | break; |
| 4947 | } |
drh | deaa610 | 2020-10-01 15:46:21 +0000 | [diff] [blame] | 4948 | if( nStep<=0 ){ |
drh | 0b2949c | 2020-09-29 20:22:19 +0000 | [diff] [blame] | 4949 | #ifdef SQLITE_DEBUG |
| 4950 | if( db->flags&SQLITE_VdbeTrace ){ |
| 4951 | printf("... fall through after %d steps\n", pOp->p1); |
| 4952 | } |
| 4953 | #endif |
| 4954 | VdbeBranchTaken(0,3); |
| 4955 | break; |
| 4956 | } |
drh | deaa610 | 2020-10-01 15:46:21 +0000 | [diff] [blame] | 4957 | nStep--; |
drh | f761d93 | 2020-09-29 01:48:46 +0000 | [diff] [blame] | 4958 | rc = sqlite3BtreeNext(pC->uc.pCursor, 0); |
drh | 0b2949c | 2020-09-29 20:22:19 +0000 | [diff] [blame] | 4959 | if( rc ){ |
| 4960 | if( rc==SQLITE_DONE ){ |
| 4961 | rc = SQLITE_OK; |
| 4962 | goto seekscan_search_fail; |
| 4963 | }else{ |
| 4964 | goto abort_due_to_error; |
| 4965 | } |
| 4966 | } |
drh | f761d93 | 2020-09-29 01:48:46 +0000 | [diff] [blame] | 4967 | } |
drh | 0b2949c | 2020-09-29 20:22:19 +0000 | [diff] [blame] | 4968 | |
drh | f761d93 | 2020-09-29 01:48:46 +0000 | [diff] [blame] | 4969 | break; |
drh | 68cf0ac | 2020-09-28 19:51:54 +0000 | [diff] [blame] | 4970 | } |
| 4971 | |
| 4972 | |
drh | fa17e13 | 2020-09-01 01:52:03 +0000 | [diff] [blame] | 4973 | /* Opcode: SeekHit P1 P2 P3 * * |
| 4974 | ** Synopsis: set P2<=seekHit<=P3 |
drh | 8c2b6d7 | 2018-06-05 20:45:20 +0000 | [diff] [blame] | 4975 | ** |
drh | fa17e13 | 2020-09-01 01:52:03 +0000 | [diff] [blame] | 4976 | ** Increase or decrease the seekHit value for cursor P1, if necessary, |
| 4977 | ** so that it is no less than P2 and no greater than P3. |
drh | 8c2b6d7 | 2018-06-05 20:45:20 +0000 | [diff] [blame] | 4978 | ** |
drh | fa17e13 | 2020-09-01 01:52:03 +0000 | [diff] [blame] | 4979 | ** The seekHit integer represents the maximum of terms in an index for which |
| 4980 | ** there is known to be at least one match. If the seekHit value is smaller |
| 4981 | ** than the total number of equality terms in an index lookup, then the |
| 4982 | ** OP_IfNoHope opcode might run to see if the IN loop can be abandoned |
| 4983 | ** early, thus saving work. This is part of the IN-early-out optimization. |
| 4984 | ** |
| 4985 | ** P1 must be a valid b-tree cursor. |
drh | 8c2b6d7 | 2018-06-05 20:45:20 +0000 | [diff] [blame] | 4986 | */ |
| 4987 | case OP_SeekHit: { |
| 4988 | VdbeCursor *pC; |
| 4989 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 4990 | pC = p->apCsr[pOp->p1]; |
| 4991 | assert( pC!=0 ); |
drh | fa17e13 | 2020-09-01 01:52:03 +0000 | [diff] [blame] | 4992 | assert( pOp->p3>=pOp->p2 ); |
| 4993 | if( pC->seekHit<pOp->p2 ){ |
drh | 7bfccfe | 2021-04-29 13:58:28 +0000 | [diff] [blame] | 4994 | #ifdef SQLITE_DEBUG |
| 4995 | if( db->flags&SQLITE_VdbeTrace ){ |
| 4996 | printf("seekHit changes from %d to %d\n", pC->seekHit, pOp->p2); |
| 4997 | } |
| 4998 | #endif |
drh | fa17e13 | 2020-09-01 01:52:03 +0000 | [diff] [blame] | 4999 | pC->seekHit = pOp->p2; |
| 5000 | }else if( pC->seekHit>pOp->p3 ){ |
drh | 7bfccfe | 2021-04-29 13:58:28 +0000 | [diff] [blame] | 5001 | #ifdef SQLITE_DEBUG |
| 5002 | if( db->flags&SQLITE_VdbeTrace ){ |
| 5003 | printf("seekHit changes from %d to %d\n", pC->seekHit, pOp->p3); |
| 5004 | } |
| 5005 | #endif |
drh | fa17e13 | 2020-09-01 01:52:03 +0000 | [diff] [blame] | 5006 | pC->seekHit = pOp->p3; |
| 5007 | } |
drh | 8c2b6d7 | 2018-06-05 20:45:20 +0000 | [diff] [blame] | 5008 | break; |
| 5009 | } |
| 5010 | |
dan | 74ebaad | 2020-01-04 16:55:57 +0000 | [diff] [blame] | 5011 | /* Opcode: IfNotOpen P1 P2 * * * |
| 5012 | ** Synopsis: if( !csr[P1] ) goto P2 |
| 5013 | ** |
drh | 8b9a3d1 | 2022-09-20 19:22:17 +0000 | [diff] [blame] | 5014 | ** If cursor P1 is not open or if P1 is set to a NULL row using the |
| 5015 | ** OP_NullRow opcode, then jump to instruction P2. Otherwise, fall through. |
dan | 74ebaad | 2020-01-04 16:55:57 +0000 | [diff] [blame] | 5016 | */ |
| 5017 | case OP_IfNotOpen: { /* jump */ |
drh | 8b9a3d1 | 2022-09-20 19:22:17 +0000 | [diff] [blame] | 5018 | VdbeCursor *pCur; |
| 5019 | |
dan | 74ebaad | 2020-01-04 16:55:57 +0000 | [diff] [blame] | 5020 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
drh | 8b9a3d1 | 2022-09-20 19:22:17 +0000 | [diff] [blame] | 5021 | pCur = p->apCsr[pOp->p1]; |
| 5022 | VdbeBranchTaken(pCur==0 || pCur->nullRow, 2); |
| 5023 | if( pCur==0 || pCur->nullRow ){ |
dan | 74ebaad | 2020-01-04 16:55:57 +0000 | [diff] [blame] | 5024 | goto jump_to_p2_and_check_for_interrupt; |
| 5025 | } |
| 5026 | break; |
| 5027 | } |
| 5028 | |
drh | 8cff69d | 2009-11-12 19:59:44 +0000 | [diff] [blame] | 5029 | /* Opcode: Found P1 P2 P3 P4 * |
drh | f63552b | 2013-10-30 00:25:03 +0000 | [diff] [blame] | 5030 | ** Synopsis: key=r[P3@P4] |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5031 | ** |
drh | 8cff69d | 2009-11-12 19:59:44 +0000 | [diff] [blame] | 5032 | ** If P4==0 then register P3 holds a blob constructed by MakeRecord. If |
| 5033 | ** P4>0 then register P3 is the first of P4 registers that form an unpacked |
| 5034 | ** record. |
| 5035 | ** |
| 5036 | ** Cursor P1 is on an index btree. If the record identified by P3 and P4 |
| 5037 | ** 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] | 5038 | ** P1 is left pointing at the matching entry. |
drh | 6f225d0 | 2013-10-26 13:36:51 +0000 | [diff] [blame] | 5039 | ** |
drh | cefc87f | 2014-08-01 01:40:33 +0000 | [diff] [blame] | 5040 | ** This operation leaves the cursor in a state where it can be |
| 5041 | ** advanced in the forward direction. The Next instruction will work, |
| 5042 | ** but not the Prev instruction. |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 5043 | ** |
drh | 6f225d0 | 2013-10-26 13:36:51 +0000 | [diff] [blame] | 5044 | ** See also: NotFound, NoConflict, NotExists. SeekGe |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5045 | */ |
drh | 8cff69d | 2009-11-12 19:59:44 +0000 | [diff] [blame] | 5046 | /* Opcode: NotFound P1 P2 P3 P4 * |
drh | f63552b | 2013-10-30 00:25:03 +0000 | [diff] [blame] | 5047 | ** Synopsis: key=r[P3@P4] |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5048 | ** |
drh | 8cff69d | 2009-11-12 19:59:44 +0000 | [diff] [blame] | 5049 | ** If P4==0 then register P3 holds a blob constructed by MakeRecord. If |
| 5050 | ** P4>0 then register P3 is the first of P4 registers that form an unpacked |
| 5051 | ** record. |
| 5052 | ** |
| 5053 | ** Cursor P1 is on an index btree. If the record identified by P3 and P4 |
| 5054 | ** is not the prefix of any entry in P1 then a jump is made to P2. If P1 |
| 5055 | ** does contain an entry whose prefix matches the P3/P4 record then control |
| 5056 | ** falls through to the next instruction and P1 is left pointing at the |
| 5057 | ** matching entry. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5058 | ** |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 5059 | ** This operation leaves the cursor in a state where it cannot be |
| 5060 | ** advanced in either direction. In other words, the Next and Prev |
| 5061 | ** opcodes do not work after this operation. |
| 5062 | ** |
drh | 8c2b6d7 | 2018-06-05 20:45:20 +0000 | [diff] [blame] | 5063 | ** See also: Found, NotExists, NoConflict, IfNoHope |
| 5064 | */ |
| 5065 | /* Opcode: IfNoHope P1 P2 P3 P4 * |
| 5066 | ** Synopsis: key=r[P3@P4] |
| 5067 | ** |
| 5068 | ** Register P3 is the first of P4 registers that form an unpacked |
drh | fa17e13 | 2020-09-01 01:52:03 +0000 | [diff] [blame] | 5069 | ** record. Cursor P1 is an index btree. P2 is a jump destination. |
| 5070 | ** In other words, the operands to this opcode are the same as the |
| 5071 | ** operands to OP_NotFound and OP_IdxGT. |
drh | 8c2b6d7 | 2018-06-05 20:45:20 +0000 | [diff] [blame] | 5072 | ** |
drh | fa17e13 | 2020-09-01 01:52:03 +0000 | [diff] [blame] | 5073 | ** This opcode is an optimization attempt only. If this opcode always |
| 5074 | ** falls through, the correct answer is still obtained, but extra works |
| 5075 | ** is performed. |
drh | 8c2b6d7 | 2018-06-05 20:45:20 +0000 | [diff] [blame] | 5076 | ** |
drh | fa17e13 | 2020-09-01 01:52:03 +0000 | [diff] [blame] | 5077 | ** A value of N in the seekHit flag of cursor P1 means that there exists |
| 5078 | ** a key P3:N that will match some record in the index. We want to know |
| 5079 | ** if it is possible for a record P3:P4 to match some record in the |
| 5080 | ** index. If it is not possible, we can skips some work. So if seekHit |
| 5081 | ** is less than P4, attempt to find out if a match is possible by running |
| 5082 | ** OP_NotFound. |
drh | 8c2b6d7 | 2018-06-05 20:45:20 +0000 | [diff] [blame] | 5083 | ** |
| 5084 | ** This opcode is used in IN clause processing for a multi-column key. |
| 5085 | ** If an IN clause is attached to an element of the key other than the |
| 5086 | ** left-most element, and if there are no matches on the most recent |
| 5087 | ** seek over the whole key, then it might be that one of the key element |
| 5088 | ** to the left is prohibiting a match, and hence there is "no hope" of |
| 5089 | ** any match regardless of how many IN clause elements are checked. |
| 5090 | ** In such a case, we abandon the IN clause search early, using this |
| 5091 | ** opcode. The opcode name comes from the fact that the |
| 5092 | ** jump is taken if there is "no hope" of achieving a match. |
| 5093 | ** |
| 5094 | ** See also: NotFound, SeekHit |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5095 | */ |
drh | 6f225d0 | 2013-10-26 13:36:51 +0000 | [diff] [blame] | 5096 | /* Opcode: NoConflict P1 P2 P3 P4 * |
drh | 4af5bee | 2013-10-30 02:37:50 +0000 | [diff] [blame] | 5097 | ** Synopsis: key=r[P3@P4] |
drh | 6f225d0 | 2013-10-26 13:36:51 +0000 | [diff] [blame] | 5098 | ** |
| 5099 | ** If P4==0 then register P3 holds a blob constructed by MakeRecord. If |
| 5100 | ** P4>0 then register P3 is the first of P4 registers that form an unpacked |
| 5101 | ** record. |
| 5102 | ** |
| 5103 | ** Cursor P1 is on an index btree. If the record identified by P3 and P4 |
| 5104 | ** contains any NULL value, jump immediately to P2. If all terms of the |
| 5105 | ** record are not-NULL then a check is done to determine if any row in the |
| 5106 | ** P1 index btree has a matching key prefix. If there are no matches, jump |
| 5107 | ** immediately to P2. If there is a match, fall through and leave the P1 |
| 5108 | ** cursor pointing to the matching row. |
| 5109 | ** |
| 5110 | ** This opcode is similar to OP_NotFound with the exceptions that the |
| 5111 | ** branch is always taken if any part of the search key input is NULL. |
| 5112 | ** |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 5113 | ** This operation leaves the cursor in a state where it cannot be |
| 5114 | ** advanced in either direction. In other words, the Next and Prev |
| 5115 | ** opcodes do not work after this operation. |
| 5116 | ** |
drh | 6f225d0 | 2013-10-26 13:36:51 +0000 | [diff] [blame] | 5117 | ** See also: NotFound, Found, NotExists |
| 5118 | */ |
drh | 8c2b6d7 | 2018-06-05 20:45:20 +0000 | [diff] [blame] | 5119 | case OP_IfNoHope: { /* jump, in3 */ |
| 5120 | VdbeCursor *pC; |
| 5121 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 5122 | pC = p->apCsr[pOp->p1]; |
| 5123 | assert( pC!=0 ); |
drh | 7bfccfe | 2021-04-29 13:58:28 +0000 | [diff] [blame] | 5124 | #ifdef SQLITE_DEBUG |
| 5125 | if( db->flags&SQLITE_VdbeTrace ){ |
| 5126 | printf("seekHit is %d\n", pC->seekHit); |
| 5127 | } |
| 5128 | #endif |
drh | fa17e13 | 2020-09-01 01:52:03 +0000 | [diff] [blame] | 5129 | if( pC->seekHit>=pOp->p4.i ) break; |
drh | 8c2b6d7 | 2018-06-05 20:45:20 +0000 | [diff] [blame] | 5130 | /* Fall through into OP_NotFound */ |
drh | 08b9208 | 2020-08-10 14:18:00 +0000 | [diff] [blame] | 5131 | /* no break */ deliberate_fall_through |
drh | 8c2b6d7 | 2018-06-05 20:45:20 +0000 | [diff] [blame] | 5132 | } |
drh | 6f225d0 | 2013-10-26 13:36:51 +0000 | [diff] [blame] | 5133 | case OP_NoConflict: /* jump, in3 */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 5134 | case OP_NotFound: /* jump, in3 */ |
| 5135 | case OP_Found: { /* jump, in3 */ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 5136 | int alreadyExists; |
drh | 6f225d0 | 2013-10-26 13:36:51 +0000 | [diff] [blame] | 5137 | int ii; |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 5138 | VdbeCursor *pC; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 5139 | UnpackedRecord *pIdxKey; |
drh | 8cff69d | 2009-11-12 19:59:44 +0000 | [diff] [blame] | 5140 | UnpackedRecord r; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 5141 | |
dan | 0ff297e | 2009-09-25 17:03:14 +0000 | [diff] [blame] | 5142 | #ifdef SQLITE_TEST |
drh | 6f225d0 | 2013-10-26 13:36:51 +0000 | [diff] [blame] | 5143 | if( pOp->opcode!=OP_NoConflict ) sqlite3_found_count++; |
dan | 0ff297e | 2009-09-25 17:03:14 +0000 | [diff] [blame] | 5144 | #endif |
| 5145 | |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 5146 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
drh | 8cff69d | 2009-11-12 19:59:44 +0000 | [diff] [blame] | 5147 | assert( pOp->p4type==P4_INT32 ); |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 5148 | pC = p->apCsr[pOp->p1]; |
| 5149 | assert( pC!=0 ); |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 5150 | #ifdef SQLITE_DEBUG |
drh | cefc87f | 2014-08-01 01:40:33 +0000 | [diff] [blame] | 5151 | pC->seekOp = pOp->opcode; |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 5152 | #endif |
drh | 95b1036 | 2022-04-13 19:00:57 +0000 | [diff] [blame] | 5153 | r.aMem = &aMem[pOp->p3]; |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 5154 | assert( pC->eCurType==CURTYPE_BTREE ); |
| 5155 | assert( pC->uc.pCursor!=0 ); |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 5156 | assert( pC->isTable==0 ); |
drh | 95b1036 | 2022-04-13 19:00:57 +0000 | [diff] [blame] | 5157 | r.nField = (u16)pOp->p4.i; |
| 5158 | if( r.nField>0 ){ |
drh | b834e0d | 2022-04-04 19:43:57 +0000 | [diff] [blame] | 5159 | /* Key values in an array of registers */ |
drh | b834e0d | 2022-04-04 19:43:57 +0000 | [diff] [blame] | 5160 | r.pKeyInfo = pC->pKeyInfo; |
drh | b834e0d | 2022-04-04 19:43:57 +0000 | [diff] [blame] | 5161 | r.default_rc = 0; |
drh | 8aaf7bc | 2016-09-20 01:19:18 +0000 | [diff] [blame] | 5162 | #ifdef SQLITE_DEBUG |
drh | 826af37 | 2014-02-08 19:12:21 +0000 | [diff] [blame] | 5163 | for(ii=0; ii<r.nField; ii++){ |
| 5164 | assert( memIsValid(&r.aMem[ii]) ); |
drh | 8aaf7bc | 2016-09-20 01:19:18 +0000 | [diff] [blame] | 5165 | assert( (r.aMem[ii].flags & MEM_Zero)==0 || r.aMem[ii].n==0 ); |
drh | 826af37 | 2014-02-08 19:12:21 +0000 | [diff] [blame] | 5166 | if( ii ) REGISTER_TRACE(pOp->p3+ii, &r.aMem[ii]); |
drh | 826af37 | 2014-02-08 19:12:21 +0000 | [diff] [blame] | 5167 | } |
drh | 8aaf7bc | 2016-09-20 01:19:18 +0000 | [diff] [blame] | 5168 | #endif |
drh | ec534e6 | 2022-04-04 20:20:22 +0000 | [diff] [blame] | 5169 | rc = sqlite3BtreeIndexMoveto(pC->uc.pCursor, &r, &pC->seekResult); |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 5170 | }else{ |
drh | b834e0d | 2022-04-04 19:43:57 +0000 | [diff] [blame] | 5171 | /* Composite key generated by OP_MakeRecord */ |
drh | 95b1036 | 2022-04-13 19:00:57 +0000 | [diff] [blame] | 5172 | assert( r.aMem->flags & MEM_Blob ); |
drh | b834e0d | 2022-04-04 19:43:57 +0000 | [diff] [blame] | 5173 | assert( pOp->opcode!=OP_NoConflict ); |
drh | 95b1036 | 2022-04-13 19:00:57 +0000 | [diff] [blame] | 5174 | rc = ExpandBlob(r.aMem); |
drh | e46515b | 2017-05-19 22:51:00 +0000 | [diff] [blame] | 5175 | assert( rc==SQLITE_OK || rc==SQLITE_NOMEM ); |
| 5176 | if( rc ) goto no_mem; |
drh | b834e0d | 2022-04-04 19:43:57 +0000 | [diff] [blame] | 5177 | pIdxKey = sqlite3VdbeAllocUnpackedRecord(pC->pKeyInfo); |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 5178 | if( pIdxKey==0 ) goto no_mem; |
drh | 95b1036 | 2022-04-13 19:00:57 +0000 | [diff] [blame] | 5179 | sqlite3VdbeRecordUnpack(pC->pKeyInfo, r.aMem->n, r.aMem->z, pIdxKey); |
drh | b834e0d | 2022-04-04 19:43:57 +0000 | [diff] [blame] | 5180 | pIdxKey->default_rc = 0; |
drh | ec534e6 | 2022-04-04 20:20:22 +0000 | [diff] [blame] | 5181 | rc = sqlite3BtreeIndexMoveto(pC->uc.pCursor, pIdxKey, &pC->seekResult); |
drh | b834e0d | 2022-04-04 19:43:57 +0000 | [diff] [blame] | 5182 | sqlite3DbFreeNN(db, pIdxKey); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5183 | } |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 5184 | if( rc!=SQLITE_OK ){ |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 5185 | goto abort_due_to_error; |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 5186 | } |
drh | ec534e6 | 2022-04-04 20:20:22 +0000 | [diff] [blame] | 5187 | alreadyExists = (pC->seekResult==0); |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 5188 | pC->nullRow = 1-alreadyExists; |
| 5189 | pC->deferredMoveto = 0; |
| 5190 | pC->cacheStatus = CACHE_STALE; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5191 | if( pOp->opcode==OP_Found ){ |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 5192 | VdbeBranchTaken(alreadyExists!=0,2); |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 5193 | if( alreadyExists ) goto jump_to_p2; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5194 | }else{ |
drh | b834e0d | 2022-04-04 19:43:57 +0000 | [diff] [blame] | 5195 | if( !alreadyExists ){ |
| 5196 | VdbeBranchTaken(1,2); |
| 5197 | goto jump_to_p2; |
| 5198 | } |
| 5199 | if( pOp->opcode==OP_NoConflict ){ |
| 5200 | /* For the OP_NoConflict opcode, take the jump if any of the |
| 5201 | ** input fields are NULL, since any key with a NULL will not |
| 5202 | ** conflict */ |
| 5203 | for(ii=0; ii<r.nField; ii++){ |
| 5204 | if( r.aMem[ii].flags & MEM_Null ){ |
| 5205 | VdbeBranchTaken(1,2); |
| 5206 | goto jump_to_p2; |
| 5207 | } |
| 5208 | } |
| 5209 | } |
| 5210 | VdbeBranchTaken(0,2); |
| 5211 | if( pOp->opcode==OP_IfNoHope ){ |
| 5212 | pC->seekHit = pOp->p4.i; |
| 5213 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5214 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5215 | break; |
| 5216 | } |
| 5217 | |
drh | eeb9565 | 2016-05-26 20:56:38 +0000 | [diff] [blame] | 5218 | /* Opcode: SeekRowid P1 P2 P3 * * |
| 5219 | ** Synopsis: intkey=r[P3] |
| 5220 | ** |
| 5221 | ** P1 is the index of a cursor open on an SQL table btree (with integer |
| 5222 | ** keys). If register P3 does not contain an integer or if P1 does not |
| 5223 | ** contain a record with rowid P3 then jump immediately to P2. |
| 5224 | ** Or, if P2 is 0, raise an SQLITE_CORRUPT error. If P1 does contain |
| 5225 | ** a record with rowid P3 then |
| 5226 | ** leave the cursor pointing at that record and fall through to the next |
| 5227 | ** instruction. |
| 5228 | ** |
| 5229 | ** The OP_NotExists opcode performs the same operation, but with OP_NotExists |
| 5230 | ** the P3 register must be guaranteed to contain an integer value. With this |
| 5231 | ** opcode, register P3 might not contain an integer. |
| 5232 | ** |
| 5233 | ** The OP_NotFound opcode performs the same operation on index btrees |
| 5234 | ** (with arbitrary multi-value keys). |
| 5235 | ** |
| 5236 | ** This opcode leaves the cursor in a state where it cannot be advanced |
| 5237 | ** in either direction. In other words, the Next and Prev opcodes will |
| 5238 | ** not work following this opcode. |
| 5239 | ** |
| 5240 | ** See also: Found, NotFound, NoConflict, SeekRowid |
| 5241 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 5242 | /* Opcode: NotExists P1 P2 P3 * * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 5243 | ** Synopsis: intkey=r[P3] |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 5244 | ** |
drh | 261c02d | 2013-10-25 14:46:15 +0000 | [diff] [blame] | 5245 | ** P1 is the index of a cursor open on an SQL table btree (with integer |
| 5246 | ** 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] | 5247 | ** rowid P3 then jump immediately to P2. Or, if P2 is 0, raise an |
| 5248 | ** SQLITE_CORRUPT error. If P1 does contain a record with rowid P3 then |
| 5249 | ** leave the cursor pointing at that record and fall through to the next |
| 5250 | ** instruction. |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 5251 | ** |
drh | eeb9565 | 2016-05-26 20:56:38 +0000 | [diff] [blame] | 5252 | ** The OP_SeekRowid opcode performs the same operation but also allows the |
| 5253 | ** P3 register to contain a non-integer value, in which case the jump is |
| 5254 | ** always taken. This opcode requires that P3 always contain an integer. |
| 5255 | ** |
drh | 261c02d | 2013-10-25 14:46:15 +0000 | [diff] [blame] | 5256 | ** The OP_NotFound opcode performs the same operation on index btrees |
| 5257 | ** (with arbitrary multi-value keys). |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 5258 | ** |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 5259 | ** This opcode leaves the cursor in a state where it cannot be advanced |
| 5260 | ** in either direction. In other words, the Next and Prev opcodes will |
| 5261 | ** not work following this opcode. |
| 5262 | ** |
drh | eeb9565 | 2016-05-26 20:56:38 +0000 | [diff] [blame] | 5263 | ** See also: Found, NotFound, NoConflict, SeekRowid |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 5264 | */ |
drh | eeb9565 | 2016-05-26 20:56:38 +0000 | [diff] [blame] | 5265 | case OP_SeekRowid: { /* jump, in3 */ |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 5266 | VdbeCursor *pC; |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 5267 | BtCursor *pCrsr; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 5268 | int res; |
| 5269 | u64 iKey; |
| 5270 | |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 5271 | pIn3 = &aMem[pOp->p3]; |
drh | 3242c69 | 2019-05-04 01:29:13 +0000 | [diff] [blame] | 5272 | testcase( pIn3->flags & MEM_Int ); |
| 5273 | testcase( pIn3->flags & MEM_IntReal ); |
drh | b29ef5e | 2019-10-07 01:05:57 +0000 | [diff] [blame] | 5274 | testcase( pIn3->flags & MEM_Real ); |
| 5275 | testcase( (pIn3->flags & (MEM_Str|MEM_Int))==MEM_Str ); |
drh | 169f077 | 2019-05-02 21:36:26 +0000 | [diff] [blame] | 5276 | if( (pIn3->flags & (MEM_Int|MEM_IntReal))==0 ){ |
drh | b29ef5e | 2019-10-07 01:05:57 +0000 | [diff] [blame] | 5277 | /* If pIn3->u.i does not contain an integer, compute iKey as the |
| 5278 | ** integer value of pIn3. Jump to P2 if pIn3 cannot be converted |
| 5279 | ** into an integer without loss of information. Take care to avoid |
| 5280 | ** changing the datatype of pIn3, however, as it is used by other |
| 5281 | ** parts of the prepared statement. */ |
| 5282 | Mem x = pIn3[0]; |
| 5283 | applyAffinity(&x, SQLITE_AFF_NUMERIC, encoding); |
| 5284 | if( (x.flags & MEM_Int)==0 ) goto jump_to_p2; |
| 5285 | iKey = x.u.i; |
| 5286 | goto notExistsWithKey; |
drh | eeb9565 | 2016-05-26 20:56:38 +0000 | [diff] [blame] | 5287 | } |
| 5288 | /* Fall through into OP_NotExists */ |
drh | 08b9208 | 2020-08-10 14:18:00 +0000 | [diff] [blame] | 5289 | /* no break */ deliberate_fall_through |
drh | eeb9565 | 2016-05-26 20:56:38 +0000 | [diff] [blame] | 5290 | case OP_NotExists: /* jump, in3 */ |
| 5291 | pIn3 = &aMem[pOp->p3]; |
drh | e4fe6d4 | 2018-08-03 15:58:07 +0000 | [diff] [blame] | 5292 | assert( (pIn3->flags & MEM_Int)!=0 || pOp->opcode==OP_SeekRowid ); |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 5293 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
drh | b29ef5e | 2019-10-07 01:05:57 +0000 | [diff] [blame] | 5294 | iKey = pIn3->u.i; |
| 5295 | notExistsWithKey: |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 5296 | pC = p->apCsr[pOp->p1]; |
| 5297 | assert( pC!=0 ); |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 5298 | #ifdef SQLITE_DEBUG |
drh | 94f4f87 | 2018-12-20 22:08:32 +0000 | [diff] [blame] | 5299 | if( pOp->opcode==OP_SeekRowid ) pC->seekOp = OP_SeekRowid; |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 5300 | #endif |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 5301 | assert( pC->isTable ); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 5302 | assert( pC->eCurType==CURTYPE_BTREE ); |
| 5303 | pCrsr = pC->uc.pCursor; |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 5304 | assert( pCrsr!=0 ); |
| 5305 | res = 0; |
drh | 42a410d | 2021-06-19 18:32:20 +0000 | [diff] [blame] | 5306 | rc = sqlite3BtreeTableMoveto(pCrsr, iKey, 0, &res); |
drh | b79d552 | 2015-09-14 19:26:37 +0000 | [diff] [blame] | 5307 | assert( rc==SQLITE_OK || res==0 ); |
drh | b53a5a9 | 2014-10-12 22:37:22 +0000 | [diff] [blame] | 5308 | pC->movetoTarget = iKey; /* Used by OP_Delete */ |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 5309 | pC->nullRow = 0; |
| 5310 | pC->cacheStatus = CACHE_STALE; |
| 5311 | pC->deferredMoveto = 0; |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 5312 | VdbeBranchTaken(res!=0,2); |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 5313 | pC->seekResult = res; |
dan | c6157e1 | 2015-09-14 09:23:47 +0000 | [diff] [blame] | 5314 | if( res!=0 ){ |
drh | b79d552 | 2015-09-14 19:26:37 +0000 | [diff] [blame] | 5315 | assert( rc==SQLITE_OK ); |
| 5316 | if( pOp->p2==0 ){ |
| 5317 | rc = SQLITE_CORRUPT_BKPT; |
| 5318 | }else{ |
| 5319 | goto jump_to_p2; |
| 5320 | } |
dan | c6157e1 | 2015-09-14 09:23:47 +0000 | [diff] [blame] | 5321 | } |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 5322 | if( rc ) goto abort_due_to_error; |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 5323 | break; |
| 5324 | } |
| 5325 | |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 5326 | /* Opcode: Sequence P1 P2 * * * |
drh | 079a307 | 2014-03-19 14:10:55 +0000 | [diff] [blame] | 5327 | ** Synopsis: r[P2]=cursor[P1].ctr++ |
drh | 4db38a7 | 2005-09-01 12:16:28 +0000 | [diff] [blame] | 5328 | ** |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 5329 | ** Find the next available sequence number for cursor P1. |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 5330 | ** Write the sequence number into register P2. |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 5331 | ** The sequence number on the cursor is incremented after this |
| 5332 | ** instruction. |
drh | 4db38a7 | 2005-09-01 12:16:28 +0000 | [diff] [blame] | 5333 | */ |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 5334 | case OP_Sequence: { /* out2 */ |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 5335 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 5336 | assert( p->apCsr[pOp->p1]!=0 ); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 5337 | assert( p->apCsr[pOp->p1]->eCurType!=CURTYPE_VTAB ); |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 5338 | pOut = out2Prerelease(p, pOp); |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 5339 | pOut->u.i = p->apCsr[pOp->p1]->seqCount++; |
drh | 4db38a7 | 2005-09-01 12:16:28 +0000 | [diff] [blame] | 5340 | break; |
| 5341 | } |
| 5342 | |
| 5343 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 5344 | /* Opcode: NewRowid P1 P2 P3 * * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 5345 | ** Synopsis: r[P2]=rowid |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5346 | ** |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 5347 | ** 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] | 5348 | ** The record number is not previously used as a key in the database |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 5349 | ** table that cursor P1 points to. The new record number is written |
| 5350 | ** written to register P2. |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 5351 | ** |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 5352 | ** If P3>0 then P3 is a register in the root frame of this VDBE that holds |
| 5353 | ** the largest previously generated record number. No new record numbers are |
| 5354 | ** allowed to be less than this value. When this value reaches its maximum, |
drh | ef8662b | 2011-06-20 21:47:58 +0000 | [diff] [blame] | 5355 | ** an SQLITE_FULL error is generated. The P3 register is updated with the ' |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 5356 | ** generated record number. This P3 mechanism is used to help implement the |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 5357 | ** AUTOINCREMENT feature. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5358 | */ |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 5359 | case OP_NewRowid: { /* out2 */ |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 5360 | i64 v; /* The new rowid */ |
| 5361 | VdbeCursor *pC; /* Cursor of table to get the new rowid */ |
| 5362 | int res; /* Result of an sqlite3BtreeLast() */ |
| 5363 | int cnt; /* Counter to limit the number of searches */ |
mistachkin | d6665c5 | 2021-01-18 19:28:56 +0000 | [diff] [blame] | 5364 | #ifndef SQLITE_OMIT_AUTOINCREMENT |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 5365 | Mem *pMem; /* Register holding largest rowid for AUTOINCREMENT */ |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 5366 | VdbeFrame *pFrame; /* Root frame of VDBE */ |
mistachkin | d6665c5 | 2021-01-18 19:28:56 +0000 | [diff] [blame] | 5367 | #endif |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 5368 | |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 5369 | v = 0; |
| 5370 | res = 0; |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 5371 | pOut = out2Prerelease(p, pOp); |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 5372 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 5373 | pC = p->apCsr[pOp->p1]; |
| 5374 | assert( pC!=0 ); |
drh | 4c57e32 | 2018-05-23 17:53:07 +0000 | [diff] [blame] | 5375 | assert( pC->isTable ); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 5376 | assert( pC->eCurType==CURTYPE_BTREE ); |
| 5377 | assert( pC->uc.pCursor!=0 ); |
drh | 98ef0f6 | 2015-06-30 01:25:52 +0000 | [diff] [blame] | 5378 | { |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 5379 | /* The next rowid or record number (different terms for the same |
| 5380 | ** thing) is obtained in a two-step algorithm. |
| 5381 | ** |
| 5382 | ** First we attempt to find the largest existing rowid and add one |
| 5383 | ** to that. But if the largest existing rowid is already the maximum |
| 5384 | ** positive integer, we have to fall through to the second |
| 5385 | ** probabilistic algorithm |
| 5386 | ** |
| 5387 | ** The second algorithm is to select a rowid at random and see if |
| 5388 | ** it already exists in the table. If it does not exist, we have |
| 5389 | ** succeeded. If the random rowid does exist, we select a new one |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 5390 | ** and try again, up to 100 times. |
drh | db5ed6d | 2001-09-18 22:17:44 +0000 | [diff] [blame] | 5391 | */ |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 5392 | assert( pC->isTable ); |
drh | fe2093d | 2005-01-20 22:48:47 +0000 | [diff] [blame] | 5393 | |
drh | 75f86a4 | 2005-02-17 00:03:06 +0000 | [diff] [blame] | 5394 | #ifdef SQLITE_32BIT_ROWID |
| 5395 | # define MAX_ROWID 0x7fffffff |
| 5396 | #else |
drh | fe2093d | 2005-01-20 22:48:47 +0000 | [diff] [blame] | 5397 | /* Some compilers complain about constants of the form 0x7fffffffffffffff. |
| 5398 | ** Others complain about 0x7ffffffffffffffffLL. The following macro seems |
| 5399 | ** to provide the constant while making all compilers happy. |
| 5400 | */ |
danielk1977 | 64202cf | 2008-11-17 15:31:47 +0000 | [diff] [blame] | 5401 | # define MAX_ROWID (i64)( (((u64)0x7fffffff)<<32) | (u64)0xffffffff ) |
drh | 75f86a4 | 2005-02-17 00:03:06 +0000 | [diff] [blame] | 5402 | #endif |
drh | fe2093d | 2005-01-20 22:48:47 +0000 | [diff] [blame] | 5403 | |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 5404 | if( !pC->useRandomRowid ){ |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 5405 | rc = sqlite3BtreeLast(pC->uc.pCursor, &res); |
drh | e0670b6 | 2014-02-12 21:31:12 +0000 | [diff] [blame] | 5406 | if( rc!=SQLITE_OK ){ |
| 5407 | goto abort_due_to_error; |
| 5408 | } |
| 5409 | if( res ){ |
| 5410 | v = 1; /* IMP: R-61914-48074 */ |
| 5411 | }else{ |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 5412 | assert( sqlite3BtreeCursorIsValid(pC->uc.pCursor) ); |
drh | a7c90c4 | 2016-06-04 20:37:10 +0000 | [diff] [blame] | 5413 | v = sqlite3BtreeIntegerKey(pC->uc.pCursor); |
drh | e0670b6 | 2014-02-12 21:31:12 +0000 | [diff] [blame] | 5414 | if( v>=MAX_ROWID ){ |
| 5415 | pC->useRandomRowid = 1; |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 5416 | }else{ |
drh | e0670b6 | 2014-02-12 21:31:12 +0000 | [diff] [blame] | 5417 | v++; /* IMP: R-29538-34987 */ |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 5418 | } |
drh | 3fc190c | 2001-09-14 03:24:23 +0000 | [diff] [blame] | 5419 | } |
drh | e0670b6 | 2014-02-12 21:31:12 +0000 | [diff] [blame] | 5420 | } |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 5421 | |
| 5422 | #ifndef SQLITE_OMIT_AUTOINCREMENT |
drh | e0670b6 | 2014-02-12 21:31:12 +0000 | [diff] [blame] | 5423 | if( pOp->p3 ){ |
| 5424 | /* Assert that P3 is a valid memory cell. */ |
| 5425 | assert( pOp->p3>0 ); |
| 5426 | if( p->pFrame ){ |
| 5427 | for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent); |
shane | abc6b89 | 2009-09-10 19:09:03 +0000 | [diff] [blame] | 5428 | /* Assert that P3 is a valid memory cell. */ |
drh | e0670b6 | 2014-02-12 21:31:12 +0000 | [diff] [blame] | 5429 | assert( pOp->p3<=pFrame->nMem ); |
| 5430 | pMem = &pFrame->aMem[pOp->p3]; |
| 5431 | }else{ |
| 5432 | /* Assert that P3 is a valid memory cell. */ |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 5433 | assert( pOp->p3<=(p->nMem+1 - p->nCursor) ); |
drh | e0670b6 | 2014-02-12 21:31:12 +0000 | [diff] [blame] | 5434 | pMem = &aMem[pOp->p3]; |
| 5435 | memAboutToChange(p, pMem); |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 5436 | } |
drh | e0670b6 | 2014-02-12 21:31:12 +0000 | [diff] [blame] | 5437 | assert( memIsValid(pMem) ); |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 5438 | |
drh | e0670b6 | 2014-02-12 21:31:12 +0000 | [diff] [blame] | 5439 | REGISTER_TRACE(pOp->p3, pMem); |
| 5440 | sqlite3VdbeMemIntegerify(pMem); |
| 5441 | assert( (pMem->flags & MEM_Int)!=0 ); /* mem(P3) holds an integer */ |
| 5442 | if( pMem->u.i==MAX_ROWID || pC->useRandomRowid ){ |
drh | e77caa1 | 2016-11-02 13:18:46 +0000 | [diff] [blame] | 5443 | rc = SQLITE_FULL; /* IMP: R-17817-00630 */ |
drh | e0670b6 | 2014-02-12 21:31:12 +0000 | [diff] [blame] | 5444 | goto abort_due_to_error; |
| 5445 | } |
| 5446 | if( v<pMem->u.i+1 ){ |
| 5447 | v = pMem->u.i + 1; |
| 5448 | } |
| 5449 | pMem->u.i = v; |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 5450 | } |
drh | e0670b6 | 2014-02-12 21:31:12 +0000 | [diff] [blame] | 5451 | #endif |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 5452 | if( pC->useRandomRowid ){ |
drh | 748a52c | 2010-09-01 11:50:08 +0000 | [diff] [blame] | 5453 | /* IMPLEMENTATION-OF: R-07677-41881 If the largest ROWID is equal to the |
drh | c79c761 | 2010-01-01 18:57:48 +0000 | [diff] [blame] | 5454 | ** largest possible integer (9223372036854775807) then the database |
drh | 748a52c | 2010-09-01 11:50:08 +0000 | [diff] [blame] | 5455 | ** engine starts picking positive candidate ROWIDs at random until |
| 5456 | ** it finds one that is not previously used. */ |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 5457 | assert( pOp->p3==0 ); /* We cannot be in random rowid mode if this is |
| 5458 | ** an AUTOINCREMENT table. */ |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 5459 | cnt = 0; |
drh | 2c4dc63 | 2014-09-25 12:31:28 +0000 | [diff] [blame] | 5460 | do{ |
| 5461 | sqlite3_randomness(sizeof(v), &v); |
drh | d863346 | 2014-09-25 17:42:41 +0000 | [diff] [blame] | 5462 | v &= (MAX_ROWID>>1); v++; /* Ensure that v is greater than zero */ |
drh | 42a410d | 2021-06-19 18:32:20 +0000 | [diff] [blame] | 5463 | }while( ((rc = sqlite3BtreeTableMoveto(pC->uc.pCursor, (u64)v, |
drh | 748a52c | 2010-09-01 11:50:08 +0000 | [diff] [blame] | 5464 | 0, &res))==SQLITE_OK) |
shaneh | c4d340a | 2010-09-01 02:37:56 +0000 | [diff] [blame] | 5465 | && (res==0) |
drh | 2c4dc63 | 2014-09-25 12:31:28 +0000 | [diff] [blame] | 5466 | && (++cnt<100)); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 5467 | if( rc ) goto abort_due_to_error; |
| 5468 | if( res==0 ){ |
drh | c79c761 | 2010-01-01 18:57:48 +0000 | [diff] [blame] | 5469 | rc = SQLITE_FULL; /* IMP: R-38219-53002 */ |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 5470 | goto abort_due_to_error; |
| 5471 | } |
drh | 748a52c | 2010-09-01 11:50:08 +0000 | [diff] [blame] | 5472 | assert( v>0 ); /* EV: R-40812-03570 */ |
drh | 1eaa269 | 2001-09-18 02:02:23 +0000 | [diff] [blame] | 5473 | } |
drh | a11846b | 2004-01-07 18:52:56 +0000 | [diff] [blame] | 5474 | pC->deferredMoveto = 0; |
drh | 76873ab | 2006-01-07 18:48:26 +0000 | [diff] [blame] | 5475 | pC->cacheStatus = CACHE_STALE; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5476 | } |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 5477 | pOut->u.i = v; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5478 | break; |
| 5479 | } |
| 5480 | |
danielk1977 | 1f4aa33 | 2008-01-03 09:51:55 +0000 | [diff] [blame] | 5481 | /* Opcode: Insert P1 P2 P3 P4 P5 |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 5482 | ** Synopsis: intkey=r[P3] data=r[P2] |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5483 | ** |
jplyon | 5a56422 | 2003-06-02 06:15:58 +0000 | [diff] [blame] | 5484 | ** Write an entry into the table of cursor P1. A new entry is |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 5485 | ** created if it doesn't already exist or the data for an existing |
drh | 3e9ca09 | 2009-09-08 01:14:48 +0000 | [diff] [blame] | 5486 | ** entry is overwritten. The data is the value MEM_Blob stored in register |
danielk1977 | 1f4aa33 | 2008-01-03 09:51:55 +0000 | [diff] [blame] | 5487 | ** number P2. The key is stored in register P3. The key must |
drh | 3e9ca09 | 2009-09-08 01:14:48 +0000 | [diff] [blame] | 5488 | ** be a MEM_Int. |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 5489 | ** |
danielk1977 | 1f4aa33 | 2008-01-03 09:51:55 +0000 | [diff] [blame] | 5490 | ** If the OPFLAG_NCHANGE flag of P5 is set, then the row change count is |
| 5491 | ** incremented (otherwise not). If the OPFLAG_LASTROWID flag of P5 is set, |
danielk1977 | b28af71 | 2004-06-21 06:50:26 +0000 | [diff] [blame] | 5492 | ** then rowid is stored for subsequent return by the |
drh | 85b623f | 2007-12-13 21:54:09 +0000 | [diff] [blame] | 5493 | ** sqlite3_last_insert_rowid() function (otherwise it is unmodified). |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 5494 | ** |
drh | eaf6ae2 | 2016-11-09 20:14:34 +0000 | [diff] [blame] | 5495 | ** If the OPFLAG_USESEEKRESULT flag of P5 is set, the implementation might |
| 5496 | ** run faster by avoiding an unnecessary seek on cursor P1. However, |
| 5497 | ** the OPFLAG_USESEEKRESULT flag must only be set if there have been no prior |
| 5498 | ** 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] | 5499 | ** |
| 5500 | ** If the OPFLAG_ISUPDATE flag is set, then this opcode is part of an |
| 5501 | ** UPDATE operation. Otherwise (if the flag is clear) then this opcode |
| 5502 | ** is part of an INSERT operation. The difference is only important to |
| 5503 | ** the update hook. |
| 5504 | ** |
dan | 319eeb7 | 2011-03-19 08:38:50 +0000 | [diff] [blame] | 5505 | ** Parameter P4 may point to a Table structure, or may be NULL. If it is |
| 5506 | ** not NULL, then the update-hook (sqlite3.xUpdateCallback) is invoked |
| 5507 | ** following a successful insert. |
danielk1977 | 1f6eec5 | 2006-06-16 06:17:47 +0000 | [diff] [blame] | 5508 | ** |
drh | 93aed5a | 2008-01-16 17:46:38 +0000 | [diff] [blame] | 5509 | ** (WARNING/TODO: If P1 is a pseudo-cursor and P2 is dynamically |
| 5510 | ** allocated, then ownership of P2 is transferred to the pseudo-cursor |
| 5511 | ** and register P2 becomes ephemeral. If the cursor is changed, the |
| 5512 | ** value of register P2 will then change. Make sure this does not |
| 5513 | ** cause any problems.) |
| 5514 | ** |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 5515 | ** This instruction only works on tables. The equivalent instruction |
| 5516 | ** for indices is OP_IdxInsert. |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 5517 | */ |
drh | 50ef671 | 2019-02-22 23:29:56 +0000 | [diff] [blame] | 5518 | case OP_Insert: { |
drh | 3e9ca09 | 2009-09-08 01:14:48 +0000 | [diff] [blame] | 5519 | Mem *pData; /* MEM cell holding data for the record to be inserted */ |
| 5520 | Mem *pKey; /* MEM cell holding key for the record */ |
drh | 3e9ca09 | 2009-09-08 01:14:48 +0000 | [diff] [blame] | 5521 | VdbeCursor *pC; /* Cursor to table into which insert is written */ |
drh | 3e9ca09 | 2009-09-08 01:14:48 +0000 | [diff] [blame] | 5522 | int seekResult; /* Result of prior seek or 0 if no USESEEKRESULT flag */ |
| 5523 | const char *zDb; /* database name - used by the update hook */ |
dan | 319eeb7 | 2011-03-19 08:38:50 +0000 | [diff] [blame] | 5524 | Table *pTab; /* Table structure - used by update and pre-update hooks */ |
drh | 8eeb446 | 2016-05-21 20:03:42 +0000 | [diff] [blame] | 5525 | BtreePayload x; /* Payload to be inserted */ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 5526 | |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 5527 | pData = &aMem[pOp->p2]; |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 5528 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 5529 | assert( memIsValid(pData) ); |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 5530 | pC = p->apCsr[pOp->p1]; |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 5531 | assert( pC!=0 ); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 5532 | assert( pC->eCurType==CURTYPE_BTREE ); |
drh | be3da24 | 2019-12-29 00:52:41 +0000 | [diff] [blame] | 5533 | assert( pC->deferredMoveto==0 ); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 5534 | assert( pC->uc.pCursor!=0 ); |
dan | cb9a364 | 2017-01-30 19:44:53 +0000 | [diff] [blame] | 5535 | assert( (pOp->p5 & OPFLAG_ISNOOP) || pC->isTable ); |
drh | cbf1b8e | 2013-11-11 22:55:26 +0000 | [diff] [blame] | 5536 | assert( pOp->p4type==P4_TABLE || pOp->p4type>=P4_STATIC ); |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 5537 | REGISTER_TRACE(pOp->p2, pData); |
drh | 4031baf | 2018-05-28 17:31:20 +0000 | [diff] [blame] | 5538 | sqlite3VdbeIncrWriteCounter(p, pC); |
danielk1977 | 5f8d8a8 | 2004-05-11 00:28:42 +0000 | [diff] [blame] | 5539 | |
drh | 50ef671 | 2019-02-22 23:29:56 +0000 | [diff] [blame] | 5540 | pKey = &aMem[pOp->p3]; |
| 5541 | assert( pKey->flags & MEM_Int ); |
| 5542 | assert( memIsValid(pKey) ); |
| 5543 | REGISTER_TRACE(pOp->p3, pKey); |
| 5544 | x.nKey = pKey->u.i; |
drh | e05c929 | 2009-10-29 13:48:10 +0000 | [diff] [blame] | 5545 | |
drh | 9b1c62d | 2011-03-30 21:04:43 +0000 | [diff] [blame] | 5546 | if( pOp->p4type==P4_TABLE && HAS_UPDATE_HOOK(db) ){ |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 5547 | assert( pC->iDb>=0 ); |
drh | 69c3382 | 2016-08-18 14:33:11 +0000 | [diff] [blame] | 5548 | zDb = db->aDb[pC->iDb].zDbSName; |
dan | 319eeb7 | 2011-03-19 08:38:50 +0000 | [diff] [blame] | 5549 | pTab = pOp->p4.pTab; |
dan | cb9a364 | 2017-01-30 19:44:53 +0000 | [diff] [blame] | 5550 | assert( (pOp->p5 & OPFLAG_ISNOOP) || HasRowid(pTab) ); |
drh | 74c3302 | 2016-03-30 12:56:55 +0000 | [diff] [blame] | 5551 | }else{ |
drh | 4ec6f3a | 2018-01-12 19:33:18 +0000 | [diff] [blame] | 5552 | pTab = 0; |
drh | e1e1a43 | 2021-10-05 11:11:43 +0000 | [diff] [blame] | 5553 | zDb = 0; |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 5554 | } |
| 5555 | |
drh | 9b1c62d | 2011-03-30 21:04:43 +0000 | [diff] [blame] | 5556 | #ifdef SQLITE_ENABLE_PREUPDATE_HOOK |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 5557 | /* Invoke the pre-update hook, if any */ |
drh | 4ec6f3a | 2018-01-12 19:33:18 +0000 | [diff] [blame] | 5558 | if( pTab ){ |
drh | 84ebe2b | 2018-01-12 18:46:52 +0000 | [diff] [blame] | 5559 | if( db->xPreUpdateCallback && !(pOp->p5 & OPFLAG_ISUPDATE) ){ |
dan | a23a873 | 2021-04-21 20:52:17 +0000 | [diff] [blame] | 5560 | sqlite3VdbePreUpdateHook(p,pC,SQLITE_INSERT,zDb,pTab,x.nKey,pOp->p2,-1); |
drh | 84ebe2b | 2018-01-12 18:46:52 +0000 | [diff] [blame] | 5561 | } |
drh | 4ec6f3a | 2018-01-12 19:33:18 +0000 | [diff] [blame] | 5562 | if( db->xUpdateCallback==0 || pTab->aCol==0 ){ |
| 5563 | /* Prevent post-update hook from running in cases when it should not */ |
| 5564 | pTab = 0; |
drh | 84ebe2b | 2018-01-12 18:46:52 +0000 | [diff] [blame] | 5565 | } |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 5566 | } |
dan | cb9a364 | 2017-01-30 19:44:53 +0000 | [diff] [blame] | 5567 | if( pOp->p5 & OPFLAG_ISNOOP ) break; |
drh | 9b1c62d | 2011-03-30 21:04:43 +0000 | [diff] [blame] | 5568 | #endif |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 5569 | |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 5570 | if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++; |
drh | fae58d5 | 2017-01-26 17:26:44 +0000 | [diff] [blame] | 5571 | if( pOp->p5 & OPFLAG_LASTROWID ) db->lastRowid = x.nKey; |
drh | 32881be | 2020-11-17 21:26:13 +0000 | [diff] [blame] | 5572 | assert( (pData->flags & (MEM_Blob|MEM_Str))!=0 || pData->n==0 ); |
dan | 21cd29a | 2017-10-23 16:03:54 +0000 | [diff] [blame] | 5573 | x.pData = pData->z; |
| 5574 | x.nData = pData->n; |
drh | 3e9ca09 | 2009-09-08 01:14:48 +0000 | [diff] [blame] | 5575 | seekResult = ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0); |
| 5576 | if( pData->flags & MEM_Zero ){ |
drh | 8eeb446 | 2016-05-21 20:03:42 +0000 | [diff] [blame] | 5577 | x.nZero = pData->u.nZero; |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 5578 | }else{ |
drh | 8eeb446 | 2016-05-21 20:03:42 +0000 | [diff] [blame] | 5579 | x.nZero = 0; |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 5580 | } |
drh | 8eeb446 | 2016-05-21 20:03:42 +0000 | [diff] [blame] | 5581 | x.pKey = 0; |
| 5582 | rc = sqlite3BtreeInsert(pC->uc.pCursor, &x, |
dan | 7aae735 | 2020-12-10 18:06:24 +0000 | [diff] [blame] | 5583 | (pOp->p5 & (OPFLAG_APPEND|OPFLAG_SAVEPOSITION|OPFLAG_PREFORMAT)), |
| 5584 | seekResult |
drh | 3e9ca09 | 2009-09-08 01:14:48 +0000 | [diff] [blame] | 5585 | ); |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 5586 | pC->deferredMoveto = 0; |
| 5587 | pC->cacheStatus = CACHE_STALE; |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 +0000 | [diff] [blame] | 5588 | |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 5589 | /* Invoke the update-hook if required. */ |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 5590 | if( rc ) goto abort_due_to_error; |
drh | 4ec6f3a | 2018-01-12 19:33:18 +0000 | [diff] [blame] | 5591 | if( pTab ){ |
| 5592 | assert( db->xUpdateCallback!=0 ); |
| 5593 | assert( pTab->aCol!=0 ); |
| 5594 | db->xUpdateCallback(db->pUpdateArg, |
| 5595 | (pOp->p5 & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_INSERT, |
| 5596 | zDb, pTab->zName, x.nKey); |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 5597 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5598 | break; |
| 5599 | } |
| 5600 | |
dan | 7aae735 | 2020-12-10 18:06:24 +0000 | [diff] [blame] | 5601 | /* Opcode: RowCell P1 P2 P3 * * |
dan | d2ffc97 | 2020-12-10 19:20:15 +0000 | [diff] [blame] | 5602 | ** |
| 5603 | ** P1 and P2 are both open cursors. Both must be opened on the same type |
| 5604 | ** of table - intkey or index. This opcode is used as part of copying |
| 5605 | ** the current row from P2 into P1. If the cursors are opened on intkey |
| 5606 | ** tables, register P3 contains the rowid to use with the new record in |
| 5607 | ** P1. If they are opened on index tables, P3 is not used. |
| 5608 | ** |
| 5609 | ** This opcode must be followed by either an Insert or InsertIdx opcode |
| 5610 | ** with the OPFLAG_PREFORMAT flag set to complete the insert operation. |
dan | 036e067 | 2020-12-08 20:19:07 +0000 | [diff] [blame] | 5611 | */ |
dan | 7aae735 | 2020-12-10 18:06:24 +0000 | [diff] [blame] | 5612 | case OP_RowCell: { |
dan | 036e067 | 2020-12-08 20:19:07 +0000 | [diff] [blame] | 5613 | VdbeCursor *pDest; /* Cursor to write to */ |
| 5614 | VdbeCursor *pSrc; /* Cursor to read from */ |
| 5615 | i64 iKey; /* Rowid value to insert with */ |
dan | 7aae735 | 2020-12-10 18:06:24 +0000 | [diff] [blame] | 5616 | assert( pOp[1].opcode==OP_Insert || pOp[1].opcode==OP_IdxInsert ); |
drh | a06eafc | 2020-12-29 15:06:26 +0000 | [diff] [blame] | 5617 | assert( pOp[1].opcode==OP_Insert || pOp->p3==0 ); |
| 5618 | assert( pOp[1].opcode==OP_IdxInsert || pOp->p3>0 ); |
dan | d2ffc97 | 2020-12-10 19:20:15 +0000 | [diff] [blame] | 5619 | assert( pOp[1].p5 & OPFLAG_PREFORMAT ); |
dan | 036e067 | 2020-12-08 20:19:07 +0000 | [diff] [blame] | 5620 | pDest = p->apCsr[pOp->p1]; |
| 5621 | pSrc = p->apCsr[pOp->p2]; |
dan | cd1b2d0 | 2020-12-09 20:33:51 +0000 | [diff] [blame] | 5622 | iKey = pOp->p3 ? aMem[pOp->p3].u.i : 0; |
dan | 7aae735 | 2020-12-10 18:06:24 +0000 | [diff] [blame] | 5623 | rc = sqlite3BtreeTransferRow(pDest->uc.pCursor, pSrc->uc.pCursor, iKey); |
dan | 036e067 | 2020-12-08 20:19:07 +0000 | [diff] [blame] | 5624 | if( rc!=SQLITE_OK ) goto abort_due_to_error; |
| 5625 | break; |
dan | 7aae735 | 2020-12-10 18:06:24 +0000 | [diff] [blame] | 5626 | }; |
dan | 036e067 | 2020-12-08 20:19:07 +0000 | [diff] [blame] | 5627 | |
dan | 438b881 | 2015-09-15 15:55:15 +0000 | [diff] [blame] | 5628 | /* Opcode: Delete P1 P2 P3 P4 P5 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5629 | ** |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 5630 | ** Delete the record at which the P1 cursor is currently pointing. |
| 5631 | ** |
drh | e807bdb | 2016-01-21 17:06:33 +0000 | [diff] [blame] | 5632 | ** If the OPFLAG_SAVEPOSITION bit of the P5 parameter is set, then |
| 5633 | ** the cursor will be left pointing at either the next or the previous |
| 5634 | ** record in the table. If it is left pointing at the next record, then |
| 5635 | ** the next Next instruction will be a no-op. As a result, in this case |
| 5636 | ** it is ok to delete a record from within a Next loop. If |
| 5637 | ** OPFLAG_SAVEPOSITION bit of P5 is clear, then the cursor will be |
| 5638 | ** left in an undefined state. |
drh | c8d30ac | 2002-04-12 10:08:59 +0000 | [diff] [blame] | 5639 | ** |
drh | def19e3 | 2016-01-27 16:26:25 +0000 | [diff] [blame] | 5640 | ** If the OPFLAG_AUXDELETE bit is set on P5, that indicates that this |
| 5641 | ** delete one of several associated with deleting a table row and all its |
| 5642 | ** associated index entries. Exactly one of those deletes is the "primary" |
| 5643 | ** delete. The others are all on OPFLAG_FORDELETE cursors or else are |
| 5644 | ** marked with the AUXDELETE flag. |
drh | e807bdb | 2016-01-21 17:06:33 +0000 | [diff] [blame] | 5645 | ** |
| 5646 | ** If the OPFLAG_NCHANGE flag of P2 (NB: P2 not P5) is set, then the row |
| 5647 | ** change count is incremented (otherwise not). |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 5648 | ** |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 5649 | ** P1 must not be pseudo-table. It has to be a real table with |
| 5650 | ** multiple rows. |
| 5651 | ** |
drh | 5e769a5 | 2016-09-28 16:05:53 +0000 | [diff] [blame] | 5652 | ** 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] | 5653 | ** the update or pre-update hook, or both, may be invoked. The P1 cursor must |
| 5654 | ** have been positioned using OP_NotFound prior to invoking this opcode in |
| 5655 | ** this case. Specifically, if one is configured, the pre-update hook is |
| 5656 | ** invoked if P4 is not NULL. The update-hook is invoked if one is configured, |
| 5657 | ** P4 is not NULL, and the OPFLAG_NCHANGE flag is set in P2. |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 5658 | ** |
| 5659 | ** If the OPFLAG_ISUPDATE flag is set in P2, then P3 contains the address |
| 5660 | ** of the memory cell that contains the value that the rowid of the row will |
| 5661 | ** be set to by the update. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5662 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 5663 | case OP_Delete: { |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 5664 | VdbeCursor *pC; |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 5665 | const char *zDb; |
dan | 319eeb7 | 2011-03-19 08:38:50 +0000 | [diff] [blame] | 5666 | Table *pTab; |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 5667 | int opflags; |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 5668 | |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 5669 | opflags = pOp->p2; |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 5670 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 5671 | pC = p->apCsr[pOp->p1]; |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 5672 | assert( pC!=0 ); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 5673 | assert( pC->eCurType==CURTYPE_BTREE ); |
| 5674 | assert( pC->uc.pCursor!=0 ); |
drh | 9a65f2c | 2009-06-22 19:05:40 +0000 | [diff] [blame] | 5675 | assert( pC->deferredMoveto==0 ); |
drh | 4031baf | 2018-05-28 17:31:20 +0000 | [diff] [blame] | 5676 | sqlite3VdbeIncrWriteCounter(p, pC); |
drh | 9a65f2c | 2009-06-22 19:05:40 +0000 | [diff] [blame] | 5677 | |
drh | b53a5a9 | 2014-10-12 22:37:22 +0000 | [diff] [blame] | 5678 | #ifdef SQLITE_DEBUG |
drh | 6b559f3 | 2020-01-02 19:50:50 +0000 | [diff] [blame] | 5679 | if( pOp->p4type==P4_TABLE |
| 5680 | && HasRowid(pOp->p4.pTab) |
| 5681 | && pOp->p5==0 |
| 5682 | && sqlite3BtreeCursorIsValidNN(pC->uc.pCursor) |
| 5683 | ){ |
dan | 438b881 | 2015-09-15 15:55:15 +0000 | [diff] [blame] | 5684 | /* If p5 is zero, the seek operation that positioned the cursor prior to |
| 5685 | ** OP_Delete will have also set the pC->movetoTarget field to the rowid of |
| 5686 | ** the row that is being deleted */ |
drh | a7c90c4 | 2016-06-04 20:37:10 +0000 | [diff] [blame] | 5687 | i64 iKey = sqlite3BtreeIntegerKey(pC->uc.pCursor); |
dan | 0971ef4 | 2019-05-16 20:13:32 +0000 | [diff] [blame] | 5688 | assert( CORRUPT_DB || pC->movetoTarget==iKey ); |
drh | b53a5a9 | 2014-10-12 22:37:22 +0000 | [diff] [blame] | 5689 | } |
| 5690 | #endif |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 5691 | |
dan | 438b881 | 2015-09-15 15:55:15 +0000 | [diff] [blame] | 5692 | /* If the update-hook or pre-update-hook will be invoked, set zDb to |
| 5693 | ** the name of the db to pass as to it. Also set local pTab to a copy |
| 5694 | ** of p4.pTab. Finally, if p5 is true, indicating that this cursor was |
| 5695 | ** last moved with OP_Next or OP_Prev, not Seek or NotFound, set |
| 5696 | ** VdbeCursor.movetoTarget to the current rowid. */ |
drh | c556f3c | 2016-03-30 15:30:07 +0000 | [diff] [blame] | 5697 | if( pOp->p4type==P4_TABLE && HAS_UPDATE_HOOK(db) ){ |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 5698 | assert( pC->iDb>=0 ); |
drh | c556f3c | 2016-03-30 15:30:07 +0000 | [diff] [blame] | 5699 | assert( pOp->p4.pTab!=0 ); |
drh | 69c3382 | 2016-08-18 14:33:11 +0000 | [diff] [blame] | 5700 | zDb = db->aDb[pC->iDb].zDbSName; |
dan | 319eeb7 | 2011-03-19 08:38:50 +0000 | [diff] [blame] | 5701 | pTab = pOp->p4.pTab; |
drh | c556f3c | 2016-03-30 15:30:07 +0000 | [diff] [blame] | 5702 | if( (pOp->p5 & OPFLAG_SAVEPOSITION)!=0 && pC->isTable ){ |
drh | a7c90c4 | 2016-06-04 20:37:10 +0000 | [diff] [blame] | 5703 | pC->movetoTarget = sqlite3BtreeIntegerKey(pC->uc.pCursor); |
dan | 438b881 | 2015-09-15 15:55:15 +0000 | [diff] [blame] | 5704 | } |
drh | 74c3302 | 2016-03-30 12:56:55 +0000 | [diff] [blame] | 5705 | }else{ |
drh | e1e1a43 | 2021-10-05 11:11:43 +0000 | [diff] [blame] | 5706 | zDb = 0; |
| 5707 | pTab = 0; |
drh | 92fe38e | 2014-10-14 13:41:32 +0000 | [diff] [blame] | 5708 | } |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 5709 | |
drh | 9b1c62d | 2011-03-30 21:04:43 +0000 | [diff] [blame] | 5710 | #ifdef SQLITE_ENABLE_PREUPDATE_HOOK |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 5711 | /* Invoke the pre-update-hook if required. */ |
drh | e1e1a43 | 2021-10-05 11:11:43 +0000 | [diff] [blame] | 5712 | assert( db->xPreUpdateCallback==0 || pTab==pOp->p4.pTab ); |
| 5713 | if( db->xPreUpdateCallback && pTab ){ |
dan | cb9a364 | 2017-01-30 19:44:53 +0000 | [diff] [blame] | 5714 | assert( !(opflags & OPFLAG_ISUPDATE) |
| 5715 | || HasRowid(pTab)==0 |
| 5716 | || (aMem[pOp->p3].flags & MEM_Int) |
| 5717 | ); |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 5718 | sqlite3VdbePreUpdateHook(p, pC, |
| 5719 | (opflags & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_DELETE, |
drh | 92fe38e | 2014-10-14 13:41:32 +0000 | [diff] [blame] | 5720 | zDb, pTab, pC->movetoTarget, |
dan | a23a873 | 2021-04-21 20:52:17 +0000 | [diff] [blame] | 5721 | pOp->p3, -1 |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 5722 | ); |
| 5723 | } |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 5724 | if( opflags & OPFLAG_ISNOOP ) break; |
drh | c556f3c | 2016-03-30 15:30:07 +0000 | [diff] [blame] | 5725 | #endif |
drh | b53a5a9 | 2014-10-12 22:37:22 +0000 | [diff] [blame] | 5726 | |
drh | def19e3 | 2016-01-27 16:26:25 +0000 | [diff] [blame] | 5727 | /* Only flags that can be set are SAVEPOISTION and AUXDELETE */ |
| 5728 | assert( (pOp->p5 & ~(OPFLAG_SAVEPOSITION|OPFLAG_AUXDELETE))==0 ); |
drh | e807bdb | 2016-01-21 17:06:33 +0000 | [diff] [blame] | 5729 | assert( OPFLAG_SAVEPOSITION==BTREE_SAVEPOSITION ); |
drh | def19e3 | 2016-01-27 16:26:25 +0000 | [diff] [blame] | 5730 | assert( OPFLAG_AUXDELETE==BTREE_AUXDELETE ); |
drh | b89aeb6 | 2016-01-27 15:49:32 +0000 | [diff] [blame] | 5731 | |
| 5732 | #ifdef SQLITE_DEBUG |
dan | e61bbf4 | 2016-01-28 17:06:17 +0000 | [diff] [blame] | 5733 | if( p->pFrame==0 ){ |
| 5734 | if( pC->isEphemeral==0 |
| 5735 | && (pOp->p5 & OPFLAG_AUXDELETE)==0 |
| 5736 | && (pC->wrFlag & OPFLAG_FORDELETE)==0 |
| 5737 | ){ |
| 5738 | nExtraDelete++; |
| 5739 | } |
| 5740 | if( pOp->p2 & OPFLAG_NCHANGE ){ |
| 5741 | nExtraDelete--; |
| 5742 | } |
drh | b89aeb6 | 2016-01-27 15:49:32 +0000 | [diff] [blame] | 5743 | } |
| 5744 | #endif |
| 5745 | |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 5746 | rc = sqlite3BtreeDelete(pC->uc.pCursor, pOp->p5); |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 5747 | pC->cacheStatus = CACHE_STALE; |
dan | 3b908d4 | 2016-11-08 19:22:32 +0000 | [diff] [blame] | 5748 | pC->seekResult = 0; |
drh | d3e1af4 | 2016-02-25 18:54:30 +0000 | [diff] [blame] | 5749 | if( rc ) goto abort_due_to_error; |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 +0000 | [diff] [blame] | 5750 | |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 5751 | /* Invoke the update-hook if required. */ |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 5752 | if( opflags & OPFLAG_NCHANGE ){ |
| 5753 | p->nChange++; |
drh | 7d4c94b | 2021-10-04 22:34:38 +0000 | [diff] [blame] | 5754 | if( db->xUpdateCallback && ALWAYS(pTab!=0) && HasRowid(pTab) ){ |
drh | 92fe38e | 2014-10-14 13:41:32 +0000 | [diff] [blame] | 5755 | db->xUpdateCallback(db->pUpdateArg, SQLITE_DELETE, zDb, pTab->zName, |
dan | 438b881 | 2015-09-15 15:55:15 +0000 | [diff] [blame] | 5756 | pC->movetoTarget); |
| 5757 | assert( pC->iDb>=0 ); |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 5758 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5759 | } |
dan | 438b881 | 2015-09-15 15:55:15 +0000 | [diff] [blame] | 5760 | |
rdc | b0c374f | 2004-02-20 22:53:38 +0000 | [diff] [blame] | 5761 | break; |
| 5762 | } |
drh | b7f1d9a | 2009-09-08 02:27:58 +0000 | [diff] [blame] | 5763 | /* Opcode: ResetCount * * * * * |
rdc | b0c374f | 2004-02-20 22:53:38 +0000 | [diff] [blame] | 5764 | ** |
drh | b7f1d9a | 2009-09-08 02:27:58 +0000 | [diff] [blame] | 5765 | ** The value of the change counter is copied to the database handle |
| 5766 | ** change counter (returned by subsequent calls to sqlite3_changes()). |
| 5767 | ** Then the VMs internal change counter resets to 0. |
| 5768 | ** This is used by trigger programs. |
rdc | b0c374f | 2004-02-20 22:53:38 +0000 | [diff] [blame] | 5769 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 5770 | case OP_ResetCount: { |
drh | b7f1d9a | 2009-09-08 02:27:58 +0000 | [diff] [blame] | 5771 | sqlite3VdbeSetChanges(db, p->nChange); |
danielk1977 | b28af71 | 2004-06-21 06:50:26 +0000 | [diff] [blame] | 5772 | p->nChange = 0; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5773 | break; |
| 5774 | } |
| 5775 | |
drh | 1153c7b | 2013-11-01 22:02:56 +0000 | [diff] [blame] | 5776 | /* Opcode: SorterCompare P1 P2 P3 P4 |
drh | 72e26de | 2016-08-24 21:24:04 +0000 | [diff] [blame] | 5777 | ** Synopsis: if key(P1)!=trim(r[P3],P4) goto P2 |
dan | 5134d13 | 2011-09-02 10:31:11 +0000 | [diff] [blame] | 5778 | ** |
drh | 1153c7b | 2013-11-01 22:02:56 +0000 | [diff] [blame] | 5779 | ** P1 is a sorter cursor. This instruction compares a prefix of the |
drh | bc5cf38 | 2014-08-06 01:08:07 +0000 | [diff] [blame] | 5780 | ** record blob in register P3 against a prefix of the entry that |
drh | ac50232 | 2014-07-30 13:56:48 +0000 | [diff] [blame] | 5781 | ** the sorter cursor currently points to. Only the first P4 fields |
| 5782 | ** of r[P3] and the sorter record are compared. |
drh | 1153c7b | 2013-11-01 22:02:56 +0000 | [diff] [blame] | 5783 | ** |
| 5784 | ** If either P3 or the sorter contains a NULL in one of their significant |
| 5785 | ** fields (not counting the P4 fields at the end which are ignored) then |
| 5786 | ** the comparison is assumed to be equal. |
| 5787 | ** |
| 5788 | ** Fall through to next instruction if the two records compare equal to |
| 5789 | ** each other. Jump to P2 if they are different. |
dan | 5134d13 | 2011-09-02 10:31:11 +0000 | [diff] [blame] | 5790 | */ |
| 5791 | case OP_SorterCompare: { |
| 5792 | VdbeCursor *pC; |
| 5793 | int res; |
drh | ac50232 | 2014-07-30 13:56:48 +0000 | [diff] [blame] | 5794 | int nKeyCol; |
dan | 5134d13 | 2011-09-02 10:31:11 +0000 | [diff] [blame] | 5795 | |
| 5796 | pC = p->apCsr[pOp->p1]; |
| 5797 | assert( isSorter(pC) ); |
drh | 1153c7b | 2013-11-01 22:02:56 +0000 | [diff] [blame] | 5798 | assert( pOp->p4type==P4_INT32 ); |
dan | 5134d13 | 2011-09-02 10:31:11 +0000 | [diff] [blame] | 5799 | pIn3 = &aMem[pOp->p3]; |
drh | ac50232 | 2014-07-30 13:56:48 +0000 | [diff] [blame] | 5800 | nKeyCol = pOp->p4.i; |
drh | 958d261 | 2014-04-18 13:40:07 +0000 | [diff] [blame] | 5801 | res = 0; |
drh | ac50232 | 2014-07-30 13:56:48 +0000 | [diff] [blame] | 5802 | rc = sqlite3VdbeSorterCompare(pC, pIn3, nKeyCol, &res); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 5803 | VdbeBranchTaken(res!=0,2); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 5804 | if( rc ) goto abort_due_to_error; |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 5805 | if( res ) goto jump_to_p2; |
dan | 5134d13 | 2011-09-02 10:31:11 +0000 | [diff] [blame] | 5806 | break; |
| 5807 | }; |
| 5808 | |
drh | 6cf4a7d | 2014-10-13 13:00:58 +0000 | [diff] [blame] | 5809 | /* Opcode: SorterData P1 P2 P3 * * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 5810 | ** Synopsis: r[P2]=data |
dan | 5134d13 | 2011-09-02 10:31:11 +0000 | [diff] [blame] | 5811 | ** |
| 5812 | ** Write into register P2 the current sorter data for sorter cursor P1. |
drh | 6cf4a7d | 2014-10-13 13:00:58 +0000 | [diff] [blame] | 5813 | ** Then clear the column header cache on cursor P3. |
| 5814 | ** |
| 5815 | ** This opcode is normally use to move a record out of the sorter and into |
| 5816 | ** a register that is the source for a pseudo-table cursor created using |
| 5817 | ** OpenPseudo. That pseudo-table cursor is the one that is identified by |
| 5818 | ** parameter P3. Clearing the P3 column cache as part of this opcode saves |
| 5819 | ** us from having to issue a separate NullRow instruction to clear that cache. |
dan | 5134d13 | 2011-09-02 10:31:11 +0000 | [diff] [blame] | 5820 | */ |
| 5821 | case OP_SorterData: { |
| 5822 | VdbeCursor *pC; |
drh | 3a94987 | 2012-09-18 13:20:13 +0000 | [diff] [blame] | 5823 | |
dan | 5134d13 | 2011-09-02 10:31:11 +0000 | [diff] [blame] | 5824 | pOut = &aMem[pOp->p2]; |
| 5825 | pC = p->apCsr[pOp->p1]; |
drh | 14da87f | 2013-11-20 21:51:33 +0000 | [diff] [blame] | 5826 | assert( isSorter(pC) ); |
dan | 5134d13 | 2011-09-02 10:31:11 +0000 | [diff] [blame] | 5827 | rc = sqlite3VdbeSorterRowkey(pC, pOut); |
dan | 3852413 | 2014-05-01 20:26:48 +0000 | [diff] [blame] | 5828 | assert( rc!=SQLITE_OK || (pOut->flags & MEM_Blob) ); |
drh | 6cf4a7d | 2014-10-13 13:00:58 +0000 | [diff] [blame] | 5829 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 5830 | if( rc ) goto abort_due_to_error; |
drh | 6cf4a7d | 2014-10-13 13:00:58 +0000 | [diff] [blame] | 5831 | p->apCsr[pOp->p3]->cacheStatus = CACHE_STALE; |
dan | 5134d13 | 2011-09-02 10:31:11 +0000 | [diff] [blame] | 5832 | break; |
| 5833 | } |
| 5834 | |
drh | e7b554d | 2017-01-09 15:44:25 +0000 | [diff] [blame] | 5835 | /* Opcode: RowData P1 P2 P3 * * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 5836 | ** Synopsis: r[P2]=data |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 5837 | ** |
drh | 9057fc7 | 2016-11-25 19:32:32 +0000 | [diff] [blame] | 5838 | ** Write into register P2 the complete row content for the row at |
| 5839 | ** which cursor P1 is currently pointing. |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 5840 | ** There is no interpretation of the data. |
| 5841 | ** It is just copied onto the P2 register exactly as |
danielk1977 | 96cb76f | 2008-01-04 13:24:28 +0000 | [diff] [blame] | 5842 | ** it is found in the database file. |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 5843 | ** |
drh | 9057fc7 | 2016-11-25 19:32:32 +0000 | [diff] [blame] | 5844 | ** If cursor P1 is an index, then the content is the key of the row. |
| 5845 | ** If cursor P2 is a table, then the content extracted is the data. |
drh | 143f3c4 | 2004-01-07 20:37:52 +0000 | [diff] [blame] | 5846 | ** |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 5847 | ** If the P1 cursor must be pointing to a valid row (not a NULL row) |
| 5848 | ** of a real table, not a pseudo-table. |
drh | e7b554d | 2017-01-09 15:44:25 +0000 | [diff] [blame] | 5849 | ** |
drh | 8cdafc3 | 2018-05-31 19:00:20 +0000 | [diff] [blame] | 5850 | ** If P3!=0 then this opcode is allowed to make an ephemeral pointer |
drh | e7b554d | 2017-01-09 15:44:25 +0000 | [diff] [blame] | 5851 | ** into the database page. That means that the content of the output |
| 5852 | ** register will be invalidated as soon as the cursor moves - including |
drh | 416a801 | 2018-05-31 19:14:52 +0000 | [diff] [blame] | 5853 | ** moves caused by other cursors that "save" the current cursors |
drh | e7b554d | 2017-01-09 15:44:25 +0000 | [diff] [blame] | 5854 | ** position in order that they can write to the same table. If P3==0 |
| 5855 | ** then a copy of the data is made into memory. P3!=0 is faster, but |
| 5856 | ** P3==0 is safer. |
| 5857 | ** |
| 5858 | ** If P3!=0 then the content of the P2 register is unsuitable for use |
| 5859 | ** in OP_Result and any OP_Result will invalidate the P2 register content. |
mistachkin | ab61cf7 | 2017-01-09 18:22:54 +0000 | [diff] [blame] | 5860 | ** The P2 register content is invalidated by opcodes like OP_Function or |
drh | e7b554d | 2017-01-09 15:44:25 +0000 | [diff] [blame] | 5861 | ** by any use of another cursor pointing to the same table. |
drh | 143f3c4 | 2004-01-07 20:37:52 +0000 | [diff] [blame] | 5862 | */ |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 5863 | case OP_RowData: { |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 5864 | VdbeCursor *pC; |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 5865 | BtCursor *pCrsr; |
danielk1977 | e0d4b06 | 2004-06-28 01:11:46 +0000 | [diff] [blame] | 5866 | u32 n; |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 5867 | |
drh | e7b554d | 2017-01-09 15:44:25 +0000 | [diff] [blame] | 5868 | pOut = out2Prerelease(p, pOp); |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 5869 | |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 5870 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 5871 | pC = p->apCsr[pOp->p1]; |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 5872 | assert( pC!=0 ); |
| 5873 | assert( pC->eCurType==CURTYPE_BTREE ); |
drh | 14da87f | 2013-11-20 21:51:33 +0000 | [diff] [blame] | 5874 | assert( isSorter(pC)==0 ); |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 5875 | assert( pC->nullRow==0 ); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 5876 | assert( pC->uc.pCursor!=0 ); |
| 5877 | pCrsr = pC->uc.pCursor; |
drh | 9a65f2c | 2009-06-22 19:05:40 +0000 | [diff] [blame] | 5878 | |
drh | 9057fc7 | 2016-11-25 19:32:32 +0000 | [diff] [blame] | 5879 | /* The OP_RowData opcodes always follow OP_NotExists or |
drh | eeb9565 | 2016-05-26 20:56:38 +0000 | [diff] [blame] | 5880 | ** OP_SeekRowid or OP_Rewind/Op_Next with no intervening instructions |
| 5881 | ** that might invalidate the cursor. |
| 5882 | ** If this where not the case, on of the following assert()s |
drh | c22284f | 2014-10-13 16:02:20 +0000 | [diff] [blame] | 5883 | ** would fail. Should this ever change (because of changes in the code |
| 5884 | ** generator) then the fix would be to insert a call to |
| 5885 | ** sqlite3VdbeCursorMoveto(). |
drh | 9a65f2c | 2009-06-22 19:05:40 +0000 | [diff] [blame] | 5886 | */ |
| 5887 | assert( pC->deferredMoveto==0 ); |
drh | c22284f | 2014-10-13 16:02:20 +0000 | [diff] [blame] | 5888 | assert( sqlite3BtreeCursorIsValid(pCrsr) ); |
drh | 9a65f2c | 2009-06-22 19:05:40 +0000 | [diff] [blame] | 5889 | |
drh | a7c90c4 | 2016-06-04 20:37:10 +0000 | [diff] [blame] | 5890 | n = sqlite3BtreePayloadSize(pCrsr); |
drh | d66c4f8 | 2016-06-04 20:58:35 +0000 | [diff] [blame] | 5891 | if( n>(u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){ |
drh | a7c90c4 | 2016-06-04 20:37:10 +0000 | [diff] [blame] | 5892 | goto too_big; |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 5893 | } |
drh | 722246e | 2014-10-07 23:02:24 +0000 | [diff] [blame] | 5894 | testcase( n==0 ); |
drh | 2a74006 | 2020-02-05 18:28:17 +0000 | [diff] [blame] | 5895 | rc = sqlite3VdbeMemFromBtreeZeroOffset(pCrsr, n, pOut); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 5896 | if( rc ) goto abort_due_to_error; |
drh | e7b554d | 2017-01-09 15:44:25 +0000 | [diff] [blame] | 5897 | if( !pOp->p3 ) Deephemeralize(pOut); |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 5898 | UPDATE_MAX_BLOBSIZE(pOut); |
drh | ee0ec8e | 2013-10-31 17:38:01 +0000 | [diff] [blame] | 5899 | REGISTER_TRACE(pOp->p2, pOut); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5900 | break; |
| 5901 | } |
| 5902 | |
drh | 2133d82 | 2008-01-03 18:44:59 +0000 | [diff] [blame] | 5903 | /* Opcode: Rowid P1 P2 * * * |
drh | 088b615 | 2022-04-18 13:57:57 +0000 | [diff] [blame] | 5904 | ** Synopsis: r[P2]=PX rowid of P1 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5905 | ** |
drh | 2133d82 | 2008-01-03 18:44:59 +0000 | [diff] [blame] | 5906 | ** 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] | 5907 | ** P1 is currently point to. |
drh | 044925b | 2009-04-22 17:15:02 +0000 | [diff] [blame] | 5908 | ** |
| 5909 | ** P1 can be either an ordinary table or a virtual table. There used to |
| 5910 | ** be a separate OP_VRowid opcode for use with virtual tables, but this |
| 5911 | ** one opcode now works for both table types. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5912 | */ |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 5913 | case OP_Rowid: { /* out2 */ |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 5914 | VdbeCursor *pC; |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 5915 | i64 v; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 5916 | sqlite3_vtab *pVtab; |
| 5917 | const sqlite3_module *pModule; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5918 | |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 5919 | pOut = out2Prerelease(p, pOp); |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 5920 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 5921 | pC = p->apCsr[pOp->p1]; |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 5922 | assert( pC!=0 ); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 5923 | assert( pC->eCurType!=CURTYPE_PSEUDO || pC->nullRow ); |
drh | 044925b | 2009-04-22 17:15:02 +0000 | [diff] [blame] | 5924 | if( pC->nullRow ){ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 5925 | pOut->flags = MEM_Null; |
drh | 044925b | 2009-04-22 17:15:02 +0000 | [diff] [blame] | 5926 | break; |
| 5927 | }else if( pC->deferredMoveto ){ |
drh | 6149526 | 2009-04-22 15:32:59 +0000 | [diff] [blame] | 5928 | v = pC->movetoTarget; |
drh | 044925b | 2009-04-22 17:15:02 +0000 | [diff] [blame] | 5929 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 5930 | }else if( pC->eCurType==CURTYPE_VTAB ){ |
| 5931 | assert( pC->uc.pVCur!=0 ); |
| 5932 | pVtab = pC->uc.pVCur->pVtab; |
drh | 044925b | 2009-04-22 17:15:02 +0000 | [diff] [blame] | 5933 | pModule = pVtab->pModule; |
| 5934 | assert( pModule->xRowid ); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 5935 | rc = pModule->xRowid(pC->uc.pVCur, &v); |
dan | 016f781 | 2013-08-21 17:35:48 +0000 | [diff] [blame] | 5936 | sqlite3VtabImportErrmsg(p, pVtab); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 5937 | if( rc ) goto abort_due_to_error; |
drh | 044925b | 2009-04-22 17:15:02 +0000 | [diff] [blame] | 5938 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 5939 | }else{ |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 5940 | assert( pC->eCurType==CURTYPE_BTREE ); |
| 5941 | assert( pC->uc.pCursor!=0 ); |
drh | c22284f | 2014-10-13 16:02:20 +0000 | [diff] [blame] | 5942 | rc = sqlite3VdbeCursorRestore(pC); |
drh | 6149526 | 2009-04-22 15:32:59 +0000 | [diff] [blame] | 5943 | if( rc ) goto abort_due_to_error; |
dan | 2b8669a | 2014-11-17 19:42:48 +0000 | [diff] [blame] | 5944 | if( pC->nullRow ){ |
| 5945 | pOut->flags = MEM_Null; |
| 5946 | break; |
| 5947 | } |
drh | a7c90c4 | 2016-06-04 20:37:10 +0000 | [diff] [blame] | 5948 | v = sqlite3BtreeIntegerKey(pC->uc.pCursor); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5949 | } |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 5950 | pOut->u.i = v; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5951 | break; |
| 5952 | } |
| 5953 | |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 5954 | /* Opcode: NullRow P1 * * * * |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 5955 | ** |
| 5956 | ** Move the cursor P1 to a null row. Any OP_Column operations |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 5957 | ** that occur while the cursor is on the null row will always |
| 5958 | ** write a NULL. |
drh | a10be3d | 2022-02-25 18:51:09 +0000 | [diff] [blame] | 5959 | ** |
drh | 3ac6243 | 2022-04-13 17:41:03 +0000 | [diff] [blame] | 5960 | ** If cursor P1 is not previously opened, open it now to a special |
| 5961 | ** pseudo-cursor that always returns NULL for every column. |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 5962 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 5963 | case OP_NullRow: { |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 5964 | VdbeCursor *pC; |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 5965 | |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 5966 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 5967 | pC = p->apCsr[pOp->p1]; |
drh | 3ac6243 | 2022-04-13 17:41:03 +0000 | [diff] [blame] | 5968 | if( pC==0 ){ |
| 5969 | /* If the cursor is not already open, create a special kind of |
| 5970 | ** pseudo-cursor that always gives null rows. */ |
| 5971 | pC = allocateCursor(p, pOp->p1, 1, CURTYPE_PSEUDO); |
| 5972 | if( pC==0 ) goto no_mem; |
| 5973 | pC->seekResult = 0; |
| 5974 | pC->isTable = 1; |
drh | 27a242c | 2022-06-14 22:21:23 +0000 | [diff] [blame] | 5975 | pC->noReuse = 1; |
drh | 3ac6243 | 2022-04-13 17:41:03 +0000 | [diff] [blame] | 5976 | pC->uc.pCursor = sqlite3BtreeFakeValidCursor(); |
| 5977 | } |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame] | 5978 | pC->nullRow = 1; |
drh | 399af1d | 2013-11-20 17:25:55 +0000 | [diff] [blame] | 5979 | pC->cacheStatus = CACHE_STALE; |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 5980 | if( pC->eCurType==CURTYPE_BTREE ){ |
| 5981 | assert( pC->uc.pCursor!=0 ); |
| 5982 | sqlite3BtreeClearCursor(pC->uc.pCursor); |
danielk1977 | be51a65 | 2008-10-08 17:58:48 +0000 | [diff] [blame] | 5983 | } |
drh | cf025a8 | 2018-06-07 18:01:21 +0000 | [diff] [blame] | 5984 | #ifdef SQLITE_DEBUG |
| 5985 | if( pC->seekOp==0 ) pC->seekOp = OP_NullRow; |
| 5986 | #endif |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 5987 | break; |
| 5988 | } |
| 5989 | |
drh | 86b40df | 2017-08-01 19:53:43 +0000 | [diff] [blame] | 5990 | /* Opcode: SeekEnd P1 * * * * |
| 5991 | ** |
| 5992 | ** Position cursor P1 at the end of the btree for the purpose of |
| 5993 | ** appending a new entry onto the btree. |
| 5994 | ** |
| 5995 | ** It is assumed that the cursor is used only for appending and so |
| 5996 | ** if the cursor is valid, then the cursor must already be pointing |
| 5997 | ** at the end of the btree and so no changes are made to |
| 5998 | ** the cursor. |
| 5999 | */ |
| 6000 | /* Opcode: Last P1 P2 * * * |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 6001 | ** |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 6002 | ** The next use of the Rowid or Column or Prev instruction for P1 |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 6003 | ** will refer to the last entry in the database table or index. |
| 6004 | ** If the table or index is empty and P2>0, then jump immediately to P2. |
| 6005 | ** If P2 is 0 or if the table or index is not empty, fall through |
| 6006 | ** to the following instruction. |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 6007 | ** |
| 6008 | ** This opcode leaves the cursor configured to move in reverse order, |
| 6009 | ** from the end toward the beginning. In other words, the cursor is |
drh | 5dad9a3 | 2014-07-25 18:37:42 +0000 | [diff] [blame] | 6010 | ** configured to use Prev, not Next. |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 6011 | */ |
drh | 86b40df | 2017-08-01 19:53:43 +0000 | [diff] [blame] | 6012 | case OP_SeekEnd: |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 6013 | case OP_Last: { /* jump */ |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 6014 | VdbeCursor *pC; |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 6015 | BtCursor *pCrsr; |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 6016 | int res; |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 6017 | |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 6018 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 6019 | pC = p->apCsr[pOp->p1]; |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 6020 | assert( pC!=0 ); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 6021 | assert( pC->eCurType==CURTYPE_BTREE ); |
| 6022 | pCrsr = pC->uc.pCursor; |
drh | 7abc540 | 2011-10-22 21:00:46 +0000 | [diff] [blame] | 6023 | res = 0; |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 6024 | assert( pCrsr!=0 ); |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 6025 | #ifdef SQLITE_DEBUG |
drh | 86b40df | 2017-08-01 19:53:43 +0000 | [diff] [blame] | 6026 | pC->seekOp = pOp->opcode; |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 6027 | #endif |
drh | 86b40df | 2017-08-01 19:53:43 +0000 | [diff] [blame] | 6028 | if( pOp->opcode==OP_SeekEnd ){ |
drh | d6ef5af | 2016-11-15 04:00:24 +0000 | [diff] [blame] | 6029 | assert( pOp->p2==0 ); |
drh | 86b40df | 2017-08-01 19:53:43 +0000 | [diff] [blame] | 6030 | pC->seekResult = -1; |
| 6031 | if( sqlite3BtreeCursorIsValidNN(pCrsr) ){ |
| 6032 | break; |
| 6033 | } |
| 6034 | } |
| 6035 | rc = sqlite3BtreeLast(pCrsr, &res); |
| 6036 | pC->nullRow = (u8)res; |
| 6037 | pC->deferredMoveto = 0; |
| 6038 | pC->cacheStatus = CACHE_STALE; |
| 6039 | if( rc ) goto abort_due_to_error; |
| 6040 | if( pOp->p2>0 ){ |
| 6041 | VdbeBranchTaken(res!=0,2); |
| 6042 | if( res ) goto jump_to_p2; |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 6043 | } |
| 6044 | break; |
| 6045 | } |
| 6046 | |
drh | 5e98e83 | 2017-02-17 19:24:06 +0000 | [diff] [blame] | 6047 | /* Opcode: IfSmaller P1 P2 P3 * * |
| 6048 | ** |
| 6049 | ** Estimate the number of rows in the table P1. Jump to P2 if that |
| 6050 | ** estimate is less than approximately 2**(0.1*P3). |
| 6051 | */ |
| 6052 | case OP_IfSmaller: { /* jump */ |
| 6053 | VdbeCursor *pC; |
| 6054 | BtCursor *pCrsr; |
| 6055 | int res; |
| 6056 | i64 sz; |
| 6057 | |
| 6058 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 6059 | pC = p->apCsr[pOp->p1]; |
| 6060 | assert( pC!=0 ); |
| 6061 | pCrsr = pC->uc.pCursor; |
| 6062 | assert( pCrsr ); |
| 6063 | rc = sqlite3BtreeFirst(pCrsr, &res); |
| 6064 | if( rc ) goto abort_due_to_error; |
| 6065 | if( res==0 ){ |
| 6066 | sz = sqlite3BtreeRowCountEst(pCrsr); |
| 6067 | if( ALWAYS(sz>=0) && sqlite3LogEst((u64)sz)<pOp->p3 ) res = 1; |
| 6068 | } |
| 6069 | VdbeBranchTaken(res!=0,2); |
| 6070 | if( res ) goto jump_to_p2; |
| 6071 | break; |
| 6072 | } |
| 6073 | |
drh | 0342b1f | 2005-09-01 03:07:44 +0000 | [diff] [blame] | 6074 | |
drh | 6bd4dc6 | 2016-12-23 16:05:22 +0000 | [diff] [blame] | 6075 | /* Opcode: SorterSort P1 P2 * * * |
| 6076 | ** |
| 6077 | ** After all records have been inserted into the Sorter object |
| 6078 | ** identified by P1, invoke this opcode to actually do the sorting. |
| 6079 | ** Jump to P2 if there are no records to be sorted. |
| 6080 | ** |
| 6081 | ** This opcode is an alias for OP_Sort and OP_Rewind that is used |
| 6082 | ** for Sorter objects. |
| 6083 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 6084 | /* Opcode: Sort P1 P2 * * * |
drh | 0342b1f | 2005-09-01 03:07:44 +0000 | [diff] [blame] | 6085 | ** |
| 6086 | ** This opcode does exactly the same thing as OP_Rewind except that |
| 6087 | ** it increments an undocumented global variable used for testing. |
| 6088 | ** |
| 6089 | ** Sorting is accomplished by writing records into a sorting index, |
| 6090 | ** then rewinding that index and playing it back from beginning to |
| 6091 | ** end. We use the OP_Sort opcode instead of OP_Rewind to do the |
| 6092 | ** rewinding so that the global variable will be incremented and |
| 6093 | ** regression tests can determine whether or not the optimizer is |
| 6094 | ** correctly optimizing out sorts. |
| 6095 | */ |
drh | c6aff30 | 2011-09-01 15:32:47 +0000 | [diff] [blame] | 6096 | case OP_SorterSort: /* jump */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 6097 | case OP_Sort: { /* jump */ |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 6098 | #ifdef SQLITE_TEST |
drh | 0342b1f | 2005-09-01 03:07:44 +0000 | [diff] [blame] | 6099 | sqlite3_sort_count++; |
drh | 4db38a7 | 2005-09-01 12:16:28 +0000 | [diff] [blame] | 6100 | sqlite3_search_count--; |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 6101 | #endif |
drh | 9b47ee3 | 2013-08-20 03:13:51 +0000 | [diff] [blame] | 6102 | p->aCounter[SQLITE_STMTSTATUS_SORT]++; |
drh | 0342b1f | 2005-09-01 03:07:44 +0000 | [diff] [blame] | 6103 | /* Fall through into OP_Rewind */ |
drh | 08b9208 | 2020-08-10 14:18:00 +0000 | [diff] [blame] | 6104 | /* no break */ deliberate_fall_through |
drh | 0342b1f | 2005-09-01 03:07:44 +0000 | [diff] [blame] | 6105 | } |
drh | 038ebf6 | 2019-03-29 15:21:22 +0000 | [diff] [blame] | 6106 | /* Opcode: Rewind P1 P2 * * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 6107 | ** |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 6108 | ** The next use of the Rowid or Column or Next instruction for P1 |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 6109 | ** will refer to the first entry in the database table or index. |
dan | 04489b6 | 2014-10-31 20:11:32 +0000 | [diff] [blame] | 6110 | ** If the table or index is empty, jump immediately to P2. |
| 6111 | ** If the table or index is not empty, fall through to the following |
| 6112 | ** instruction. |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 6113 | ** |
| 6114 | ** This opcode leaves the cursor configured to move in forward order, |
drh | 4ed2fb9 | 2014-08-14 13:06:25 +0000 | [diff] [blame] | 6115 | ** from the beginning toward the end. In other words, the cursor is |
drh | 5dad9a3 | 2014-07-25 18:37:42 +0000 | [diff] [blame] | 6116 | ** configured to use Next, not Prev. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 6117 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 6118 | case OP_Rewind: { /* jump */ |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 6119 | VdbeCursor *pC; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 6120 | BtCursor *pCrsr; |
drh | f4dada7 | 2004-05-11 09:57:35 +0000 | [diff] [blame] | 6121 | int res; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 6122 | |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 6123 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
drh | 038ebf6 | 2019-03-29 15:21:22 +0000 | [diff] [blame] | 6124 | assert( pOp->p5==0 ); |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 6125 | pC = p->apCsr[pOp->p1]; |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 6126 | assert( pC!=0 ); |
drh | 14da87f | 2013-11-20 21:51:33 +0000 | [diff] [blame] | 6127 | assert( isSorter(pC)==(pOp->opcode==OP_SorterSort) ); |
dan | 2411dea | 2010-07-03 05:56:09 +0000 | [diff] [blame] | 6128 | res = 1; |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 6129 | #ifdef SQLITE_DEBUG |
| 6130 | pC->seekOp = OP_Rewind; |
| 6131 | #endif |
dan | 689ab89 | 2011-08-12 15:02:00 +0000 | [diff] [blame] | 6132 | if( isSorter(pC) ){ |
drh | 958d261 | 2014-04-18 13:40:07 +0000 | [diff] [blame] | 6133 | rc = sqlite3VdbeSorterRewind(pC, &res); |
dan | a205a48 | 2011-08-27 18:48:57 +0000 | [diff] [blame] | 6134 | }else{ |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 6135 | assert( pC->eCurType==CURTYPE_BTREE ); |
| 6136 | pCrsr = pC->uc.pCursor; |
dan | a205a48 | 2011-08-27 18:48:57 +0000 | [diff] [blame] | 6137 | assert( pCrsr ); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 6138 | rc = sqlite3BtreeFirst(pCrsr, &res); |
drh | a11846b | 2004-01-07 18:52:56 +0000 | [diff] [blame] | 6139 | pC->deferredMoveto = 0; |
drh | 76873ab | 2006-01-07 18:48:26 +0000 | [diff] [blame] | 6140 | pC->cacheStatus = CACHE_STALE; |
drh | f4dada7 | 2004-05-11 09:57:35 +0000 | [diff] [blame] | 6141 | } |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 6142 | if( rc ) goto abort_due_to_error; |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 6143 | pC->nullRow = (u8)res; |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 6144 | assert( pOp->p2>0 && pOp->p2<p->nOp ); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 6145 | VdbeBranchTaken(res!=0,2); |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 6146 | if( res ) goto jump_to_p2; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 6147 | break; |
| 6148 | } |
| 6149 | |
drh | a7c9dd5 | 2022-02-24 14:44:23 +0000 | [diff] [blame] | 6150 | /* Opcode: Next P1 P2 P3 * P5 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 6151 | ** |
| 6152 | ** 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] | 6153 | ** table or index. If there are no more key/value pairs then fall through |
| 6154 | ** to the following instruction. But if the cursor advance was successful, |
| 6155 | ** jump immediately to P2. |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 6156 | ** |
drh | 5dad9a3 | 2014-07-25 18:37:42 +0000 | [diff] [blame] | 6157 | ** The Next opcode is only valid following an SeekGT, SeekGE, or |
| 6158 | ** OP_Rewind opcode used to position the cursor. Next is not allowed |
| 6159 | ** to follow SeekLT, SeekLE, or OP_Last. |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 6160 | ** |
drh | f93cd94 | 2013-11-21 03:12:25 +0000 | [diff] [blame] | 6161 | ** The P1 cursor must be for a real table, not a pseudo-table. P1 must have |
| 6162 | ** been opened prior to this opcode or the program will segfault. |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 6163 | ** |
drh | e39a732 | 2014-02-03 14:04:11 +0000 | [diff] [blame] | 6164 | ** The P3 value is a hint to the btree implementation. If P3==1, that |
| 6165 | ** means P1 is an SQL index and that this instruction could have been |
| 6166 | ** omitted if that index had been unique. P3 is usually 0. P3 is |
| 6167 | ** always either 0 or 1. |
| 6168 | ** |
drh | afc266a | 2010-03-31 17:47:44 +0000 | [diff] [blame] | 6169 | ** If P5 is positive and the jump is taken, then event counter |
| 6170 | ** number P5-1 in the prepared statement is incremented. |
| 6171 | ** |
drh | f1949b6 | 2018-06-07 17:32:59 +0000 | [diff] [blame] | 6172 | ** See also: Prev |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 6173 | */ |
drh | a7c9dd5 | 2022-02-24 14:44:23 +0000 | [diff] [blame] | 6174 | /* Opcode: Prev P1 P2 P3 * P5 |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 6175 | ** |
| 6176 | ** Back up cursor P1 so that it points to the previous key/data pair in its |
| 6177 | ** table or index. If there is no previous key/value pairs then fall through |
| 6178 | ** to the following instruction. But if the cursor backup was successful, |
| 6179 | ** jump immediately to P2. |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 6180 | ** |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 6181 | ** |
drh | 5dad9a3 | 2014-07-25 18:37:42 +0000 | [diff] [blame] | 6182 | ** The Prev opcode is only valid following an SeekLT, SeekLE, or |
| 6183 | ** OP_Last opcode used to position the cursor. Prev is not allowed |
| 6184 | ** to follow SeekGT, SeekGE, or OP_Rewind. |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 6185 | ** |
drh | f93cd94 | 2013-11-21 03:12:25 +0000 | [diff] [blame] | 6186 | ** The P1 cursor must be for a real table, not a pseudo-table. If P1 is |
| 6187 | ** not open then the behavior is undefined. |
drh | afc266a | 2010-03-31 17:47:44 +0000 | [diff] [blame] | 6188 | ** |
drh | e39a732 | 2014-02-03 14:04:11 +0000 | [diff] [blame] | 6189 | ** The P3 value is a hint to the btree implementation. If P3==1, that |
| 6190 | ** means P1 is an SQL index and that this instruction could have been |
| 6191 | ** omitted if that index had been unique. P3 is usually 0. P3 is |
| 6192 | ** always either 0 or 1. |
| 6193 | ** |
drh | afc266a | 2010-03-31 17:47:44 +0000 | [diff] [blame] | 6194 | ** If P5 is positive and the jump is taken, then event counter |
| 6195 | ** number P5-1 in the prepared statement is incremented. |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 6196 | */ |
drh | 6bd4dc6 | 2016-12-23 16:05:22 +0000 | [diff] [blame] | 6197 | /* Opcode: SorterNext P1 P2 * * P5 |
| 6198 | ** |
| 6199 | ** This opcode works just like OP_Next except that P1 must be a |
| 6200 | ** sorter object for which the OP_SorterSort opcode has been |
| 6201 | ** invoked. This opcode advances the cursor to the next sorted |
| 6202 | ** record, or jumps to P2 if there are no more sorted records. |
| 6203 | */ |
drh | f93cd94 | 2013-11-21 03:12:25 +0000 | [diff] [blame] | 6204 | case OP_SorterNext: { /* jump */ |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 6205 | VdbeCursor *pC; |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 6206 | |
drh | f93cd94 | 2013-11-21 03:12:25 +0000 | [diff] [blame] | 6207 | pC = p->apCsr[pOp->p1]; |
| 6208 | assert( isSorter(pC) ); |
drh | 2ab792e | 2017-05-30 18:34:07 +0000 | [diff] [blame] | 6209 | rc = sqlite3VdbeSorterNext(db, pC); |
drh | f93cd94 | 2013-11-21 03:12:25 +0000 | [diff] [blame] | 6210 | goto next_tail; |
drh | a7c9dd5 | 2022-02-24 14:44:23 +0000 | [diff] [blame] | 6211 | |
drh | f93cd94 | 2013-11-21 03:12:25 +0000 | [diff] [blame] | 6212 | case OP_Prev: /* jump */ |
drh | a7c9dd5 | 2022-02-24 14:44:23 +0000 | [diff] [blame] | 6213 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
drh | 35908b1 | 2022-08-23 17:51:39 +0000 | [diff] [blame] | 6214 | assert( pOp->p5==0 |
| 6215 | || pOp->p5==SQLITE_STMTSTATUS_FULLSCAN_STEP |
| 6216 | || pOp->p5==SQLITE_STMTSTATUS_AUTOINDEX); |
drh | a7c9dd5 | 2022-02-24 14:44:23 +0000 | [diff] [blame] | 6217 | pC = p->apCsr[pOp->p1]; |
| 6218 | assert( pC!=0 ); |
| 6219 | assert( pC->deferredMoveto==0 ); |
| 6220 | assert( pC->eCurType==CURTYPE_BTREE ); |
| 6221 | assert( pC->seekOp==OP_SeekLT || pC->seekOp==OP_SeekLE |
| 6222 | || pC->seekOp==OP_Last || pC->seekOp==OP_IfNoHope |
| 6223 | || pC->seekOp==OP_NullRow); |
| 6224 | rc = sqlite3BtreePrevious(pC->uc.pCursor, pOp->p3); |
| 6225 | goto next_tail; |
| 6226 | |
drh | f93cd94 | 2013-11-21 03:12:25 +0000 | [diff] [blame] | 6227 | case OP_Next: /* jump */ |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 6228 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
drh | 35908b1 | 2022-08-23 17:51:39 +0000 | [diff] [blame] | 6229 | assert( pOp->p5==0 |
| 6230 | || pOp->p5==SQLITE_STMTSTATUS_FULLSCAN_STEP |
| 6231 | || pOp->p5==SQLITE_STMTSTATUS_AUTOINDEX); |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame] | 6232 | pC = p->apCsr[pOp->p1]; |
drh | f93cd94 | 2013-11-21 03:12:25 +0000 | [diff] [blame] | 6233 | assert( pC!=0 ); |
| 6234 | assert( pC->deferredMoveto==0 ); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 6235 | assert( pC->eCurType==CURTYPE_BTREE ); |
drh | a7c9dd5 | 2022-02-24 14:44:23 +0000 | [diff] [blame] | 6236 | assert( pC->seekOp==OP_SeekGT || pC->seekOp==OP_SeekGE |
drh | 790b37a | 2019-08-27 17:01:07 +0000 | [diff] [blame] | 6237 | || pC->seekOp==OP_Rewind || pC->seekOp==OP_Found |
| 6238 | || pC->seekOp==OP_NullRow|| pC->seekOp==OP_SeekRowid |
| 6239 | || pC->seekOp==OP_IfNoHope); |
drh | a7c9dd5 | 2022-02-24 14:44:23 +0000 | [diff] [blame] | 6240 | rc = sqlite3BtreeNext(pC->uc.pCursor, pOp->p3); |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 6241 | |
drh | f93cd94 | 2013-11-21 03:12:25 +0000 | [diff] [blame] | 6242 | next_tail: |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 6243 | pC->cacheStatus = CACHE_STALE; |
drh | 2ab792e | 2017-05-30 18:34:07 +0000 | [diff] [blame] | 6244 | VdbeBranchTaken(rc==SQLITE_OK,2); |
| 6245 | if( rc==SQLITE_OK ){ |
drh | f93cd94 | 2013-11-21 03:12:25 +0000 | [diff] [blame] | 6246 | pC->nullRow = 0; |
drh | 9b47ee3 | 2013-08-20 03:13:51 +0000 | [diff] [blame] | 6247 | p->aCounter[pOp->p5]++; |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 6248 | #ifdef SQLITE_TEST |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 6249 | sqlite3_search_count++; |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 6250 | #endif |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 6251 | goto jump_to_p2_and_check_for_interrupt; |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 6252 | } |
drh | 2ab792e | 2017-05-30 18:34:07 +0000 | [diff] [blame] | 6253 | if( rc!=SQLITE_DONE ) goto abort_due_to_error; |
| 6254 | rc = SQLITE_OK; |
| 6255 | pC->nullRow = 1; |
drh | 49afe3a | 2013-07-10 03:05:14 +0000 | [diff] [blame] | 6256 | goto check_for_interrupt; |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 6257 | } |
| 6258 | |
drh | 9b4eaeb | 2016-11-09 00:10:33 +0000 | [diff] [blame] | 6259 | /* Opcode: IdxInsert P1 P2 P3 P4 P5 |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 6260 | ** Synopsis: key=r[P2] |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 6261 | ** |
drh | ef8662b | 2011-06-20 21:47:58 +0000 | [diff] [blame] | 6262 | ** Register P2 holds an SQL index key made using the |
drh | 9437bd2 | 2009-02-01 00:29:56 +0000 | [diff] [blame] | 6263 | ** MakeRecord instructions. This opcode writes that key |
drh | ee32e0a | 2006-01-10 19:45:49 +0000 | [diff] [blame] | 6264 | ** into the index P1. Data for the entry is nil. |
drh | 717e640 | 2001-09-27 03:22:32 +0000 | [diff] [blame] | 6265 | ** |
drh | fb8c56f | 2016-11-09 01:19:25 +0000 | [diff] [blame] | 6266 | ** 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] | 6267 | ** key of reg(P2). In that case, P3 is the index of the first register |
| 6268 | ** for the unpacked key. The availability of the unpacked key can sometimes |
| 6269 | ** be an optimization. |
| 6270 | ** |
| 6271 | ** If P5 has the OPFLAG_APPEND bit set, that is a hint to the b-tree layer |
| 6272 | ** that this insert is likely to be an append. |
drh | e4d9081 | 2007-03-29 05:51:49 +0000 | [diff] [blame] | 6273 | ** |
mistachkin | 21a919f | 2014-02-07 03:28:02 +0000 | [diff] [blame] | 6274 | ** If P5 has the OPFLAG_NCHANGE bit set, then the change counter is |
| 6275 | ** incremented by this instruction. If the OPFLAG_NCHANGE bit is clear, |
| 6276 | ** then the change counter is unchanged. |
drh | 0fd6135 | 2014-02-07 02:29:45 +0000 | [diff] [blame] | 6277 | ** |
drh | eaf6ae2 | 2016-11-09 20:14:34 +0000 | [diff] [blame] | 6278 | ** If the OPFLAG_USESEEKRESULT flag of P5 is set, the implementation might |
| 6279 | ** run faster by avoiding an unnecessary seek on cursor P1. However, |
| 6280 | ** the OPFLAG_USESEEKRESULT flag must only be set if there have been no prior |
| 6281 | ** seeks on the cursor or if the most recent seek used a key equivalent |
| 6282 | ** to P2. |
drh | 0fd6135 | 2014-02-07 02:29:45 +0000 | [diff] [blame] | 6283 | ** |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 6284 | ** This instruction only works for indices. The equivalent instruction |
| 6285 | ** for tables is OP_Insert. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 6286 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 6287 | case OP_IdxInsert: { /* in2 */ |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 6288 | VdbeCursor *pC; |
drh | 8eeb446 | 2016-05-21 20:03:42 +0000 | [diff] [blame] | 6289 | BtreePayload x; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 6290 | |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 6291 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 6292 | pC = p->apCsr[pOp->p1]; |
drh | 4031baf | 2018-05-28 17:31:20 +0000 | [diff] [blame] | 6293 | sqlite3VdbeIncrWriteCounter(p, pC); |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 6294 | assert( pC!=0 ); |
drh | c879c4e | 2020-02-06 13:57:08 +0000 | [diff] [blame] | 6295 | assert( !isSorter(pC) ); |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 6296 | pIn2 = &aMem[pOp->p2]; |
dan | 7aae735 | 2020-12-10 18:06:24 +0000 | [diff] [blame] | 6297 | assert( (pIn2->flags & MEM_Blob) || (pOp->p5 & OPFLAG_PREFORMAT) ); |
drh | 6546af1 | 2013-11-04 15:23:25 +0000 | [diff] [blame] | 6298 | if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++; |
drh | c879c4e | 2020-02-06 13:57:08 +0000 | [diff] [blame] | 6299 | assert( pC->eCurType==CURTYPE_BTREE ); |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 6300 | assert( pC->isTable==0 ); |
| 6301 | rc = ExpandBlob(pIn2); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 6302 | if( rc ) goto abort_due_to_error; |
drh | c879c4e | 2020-02-06 13:57:08 +0000 | [diff] [blame] | 6303 | x.nKey = pIn2->n; |
| 6304 | x.pKey = pIn2->z; |
| 6305 | x.aMem = aMem + pOp->p3; |
| 6306 | x.nMem = (u16)pOp->p4.i; |
| 6307 | rc = sqlite3BtreeInsert(pC->uc.pCursor, &x, |
dan | 7aae735 | 2020-12-10 18:06:24 +0000 | [diff] [blame] | 6308 | (pOp->p5 & (OPFLAG_APPEND|OPFLAG_SAVEPOSITION|OPFLAG_PREFORMAT)), |
drh | c879c4e | 2020-02-06 13:57:08 +0000 | [diff] [blame] | 6309 | ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0) |
| 6310 | ); |
| 6311 | assert( pC->deferredMoveto==0 ); |
| 6312 | pC->cacheStatus = CACHE_STALE; |
| 6313 | if( rc) goto abort_due_to_error; |
| 6314 | break; |
| 6315 | } |
| 6316 | |
| 6317 | /* Opcode: SorterInsert P1 P2 * * * |
| 6318 | ** Synopsis: key=r[P2] |
| 6319 | ** |
| 6320 | ** Register P2 holds an SQL index key made using the |
| 6321 | ** MakeRecord instructions. This opcode writes that key |
| 6322 | ** into the sorter P1. Data for the entry is nil. |
| 6323 | */ |
| 6324 | case OP_SorterInsert: { /* in2 */ |
| 6325 | VdbeCursor *pC; |
| 6326 | |
| 6327 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 6328 | pC = p->apCsr[pOp->p1]; |
| 6329 | sqlite3VdbeIncrWriteCounter(p, pC); |
| 6330 | assert( pC!=0 ); |
| 6331 | assert( isSorter(pC) ); |
| 6332 | pIn2 = &aMem[pOp->p2]; |
| 6333 | assert( pIn2->flags & MEM_Blob ); |
| 6334 | assert( pC->isTable==0 ); |
| 6335 | rc = ExpandBlob(pIn2); |
| 6336 | if( rc ) goto abort_due_to_error; |
| 6337 | rc = sqlite3VdbeSorterWrite(pC, pIn2); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 6338 | if( rc) goto abort_due_to_error; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 6339 | break; |
| 6340 | } |
| 6341 | |
drh | 85bd353 | 2020-05-05 18:42:49 +0000 | [diff] [blame] | 6342 | /* Opcode: IdxDelete P1 P2 P3 * P5 |
drh | f63552b | 2013-10-30 00:25:03 +0000 | [diff] [blame] | 6343 | ** Synopsis: key=r[P2@P3] |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 6344 | ** |
drh | e14006d | 2008-03-25 17:23:32 +0000 | [diff] [blame] | 6345 | ** The content of P3 registers starting at register P2 form |
| 6346 | ** an unpacked index key. This opcode removes that entry from the |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 6347 | ** index opened by cursor P1. |
drh | 85bd353 | 2020-05-05 18:42:49 +0000 | [diff] [blame] | 6348 | ** |
| 6349 | ** If P5 is not zero, then raise an SQLITE_CORRUPT_INDEX error |
| 6350 | ** if no matching index entry is found. This happens when running |
| 6351 | ** an UPDATE or DELETE statement and the index entry to be updated |
| 6352 | ** or deleted is not found. For some uses of IdxDelete |
| 6353 | ** (example: the EXCEPT operator) it does not matter that no matching |
drh | a76b151 | 2021-08-11 18:43:54 +0000 | [diff] [blame] | 6354 | ** entry is found. For those cases, P5 is zero. Also, do not raise |
| 6355 | ** this (self-correcting and non-critical) error if in writable_schema mode. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 6356 | */ |
drh | e14006d | 2008-03-25 17:23:32 +0000 | [diff] [blame] | 6357 | case OP_IdxDelete: { |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 6358 | VdbeCursor *pC; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 6359 | BtCursor *pCrsr; |
drh | 9a65f2c | 2009-06-22 19:05:40 +0000 | [diff] [blame] | 6360 | int res; |
| 6361 | UnpackedRecord r; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 6362 | |
drh | e14006d | 2008-03-25 17:23:32 +0000 | [diff] [blame] | 6363 | assert( pOp->p3>0 ); |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 6364 | 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] | 6365 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 6366 | pC = p->apCsr[pOp->p1]; |
| 6367 | assert( pC!=0 ); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 6368 | assert( pC->eCurType==CURTYPE_BTREE ); |
drh | 4031baf | 2018-05-28 17:31:20 +0000 | [diff] [blame] | 6369 | sqlite3VdbeIncrWriteCounter(p, pC); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 6370 | pCrsr = pC->uc.pCursor; |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 6371 | assert( pCrsr!=0 ); |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 6372 | r.pKeyInfo = pC->pKeyInfo; |
| 6373 | r.nField = (u16)pOp->p3; |
dan | 1fed5da | 2014-02-25 21:01:25 +0000 | [diff] [blame] | 6374 | r.default_rc = 0; |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 6375 | r.aMem = &aMem[pOp->p2]; |
drh | 42a410d | 2021-06-19 18:32:20 +0000 | [diff] [blame] | 6376 | rc = sqlite3BtreeIndexMoveto(pCrsr, &r, &res); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 6377 | if( rc ) goto abort_due_to_error; |
| 6378 | if( res==0 ){ |
dan | e61bbf4 | 2016-01-28 17:06:17 +0000 | [diff] [blame] | 6379 | rc = sqlite3BtreeDelete(pCrsr, BTREE_AUXDELETE); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 6380 | if( rc ) goto abort_due_to_error; |
drh | a76b151 | 2021-08-11 18:43:54 +0000 | [diff] [blame] | 6381 | }else if( pOp->p5 && !sqlite3WritableSchema(db) ){ |
drh | e5ceaac | 2021-01-25 21:24:14 +0000 | [diff] [blame] | 6382 | rc = sqlite3ReportError(SQLITE_CORRUPT_INDEX, __LINE__, "index corruption"); |
drh | 85bd353 | 2020-05-05 18:42:49 +0000 | [diff] [blame] | 6383 | goto abort_due_to_error; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 6384 | } |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 6385 | assert( pC->deferredMoveto==0 ); |
| 6386 | pC->cacheStatus = CACHE_STALE; |
dan | 3b908d4 | 2016-11-08 19:22:32 +0000 | [diff] [blame] | 6387 | pC->seekResult = 0; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 6388 | break; |
| 6389 | } |
| 6390 | |
drh | 170ad68 | 2017-06-02 15:44:22 +0000 | [diff] [blame] | 6391 | /* Opcode: DeferredSeek P1 * P3 P4 * |
| 6392 | ** Synopsis: Move P3 to P1.rowid if needed |
drh | 784c1b9 | 2016-01-30 16:59:56 +0000 | [diff] [blame] | 6393 | ** |
| 6394 | ** P1 is an open index cursor and P3 is a cursor on the corresponding |
| 6395 | ** table. This opcode does a deferred seek of the P3 table cursor |
| 6396 | ** to the row that corresponds to the current row of P1. |
| 6397 | ** |
| 6398 | ** This is a deferred seek. Nothing actually happens until |
| 6399 | ** the cursor is used to read a record. That way, if no reads |
| 6400 | ** occur, no unnecessary I/O happens. |
| 6401 | ** |
| 6402 | ** P4 may be an array of integers (type P4_INTARRAY) containing |
drh | 19d720d | 2016-02-03 19:52:06 +0000 | [diff] [blame] | 6403 | ** one entry for each column in the P3 table. If array entry a(i) |
| 6404 | ** is non-zero, then reading column a(i)-1 from cursor P3 is |
drh | 784c1b9 | 2016-01-30 16:59:56 +0000 | [diff] [blame] | 6405 | ** equivalent to performing the deferred seek and then reading column i |
| 6406 | ** from P1. This information is stored in P3 and used to redirect |
| 6407 | ** reads against P3 over to P1, thus possibly avoiding the need to |
| 6408 | ** seek and read cursor P3. |
| 6409 | */ |
drh | 2133d82 | 2008-01-03 18:44:59 +0000 | [diff] [blame] | 6410 | /* Opcode: IdxRowid P1 P2 * * * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 6411 | ** Synopsis: r[P2]=rowid |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 6412 | ** |
drh | 2133d82 | 2008-01-03 18:44:59 +0000 | [diff] [blame] | 6413 | ** 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] | 6414 | ** the end of the index key pointed to by cursor P1. This integer should be |
| 6415 | ** the rowid of the table entry to which this index entry points. |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 6416 | ** |
drh | 9437bd2 | 2009-02-01 00:29:56 +0000 | [diff] [blame] | 6417 | ** See also: Rowid, MakeRecord. |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 6418 | */ |
drh | 170ad68 | 2017-06-02 15:44:22 +0000 | [diff] [blame] | 6419 | case OP_DeferredSeek: |
| 6420 | case OP_IdxRowid: { /* out2 */ |
| 6421 | VdbeCursor *pC; /* The P1 index cursor */ |
| 6422 | VdbeCursor *pTabCur; /* The P2 table cursor (OP_DeferredSeek only) */ |
| 6423 | i64 rowid; /* Rowid that P1 current points to */ |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 6424 | |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 6425 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 6426 | pC = p->apCsr[pOp->p1]; |
| 6427 | assert( pC!=0 ); |
drh | eab6c12 | 2022-04-14 12:59:25 +0000 | [diff] [blame] | 6428 | assert( pC->eCurType==CURTYPE_BTREE || IsNullCursor(pC) ); |
drh | 784c1b9 | 2016-01-30 16:59:56 +0000 | [diff] [blame] | 6429 | assert( pC->uc.pCursor!=0 ); |
drh | c504f67 | 2022-04-14 14:19:23 +0000 | [diff] [blame] | 6430 | assert( pC->isTable==0 || IsNullCursor(pC) ); |
drh | c22284f | 2014-10-13 16:02:20 +0000 | [diff] [blame] | 6431 | assert( pC->deferredMoveto==0 ); |
drh | 784c1b9 | 2016-01-30 16:59:56 +0000 | [diff] [blame] | 6432 | assert( !pC->nullRow || pOp->opcode==OP_IdxRowid ); |
| 6433 | |
| 6434 | /* The IdxRowid and Seek opcodes are combined because of the commonality |
| 6435 | ** of sqlite3VdbeCursorRestore() and sqlite3VdbeIdxRowid(). */ |
| 6436 | rc = sqlite3VdbeCursorRestore(pC); |
drh | c22284f | 2014-10-13 16:02:20 +0000 | [diff] [blame] | 6437 | |
dan | aa07b36 | 2022-08-25 13:32:55 +0000 | [diff] [blame] | 6438 | /* sqlite3VdbeCursorRestore() may fail if the cursor has been disturbed |
| 6439 | ** since it was last positioned and an error (e.g. OOM or an IO error) |
| 6440 | ** occurs while trying to reposition it. */ |
| 6441 | if( rc!=SQLITE_OK ) goto abort_due_to_error; |
drh | c22284f | 2014-10-13 16:02:20 +0000 | [diff] [blame] | 6442 | |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 6443 | if( !pC->nullRow ){ |
drh | 2dc0648 | 2013-12-11 00:59:10 +0000 | [diff] [blame] | 6444 | rowid = 0; /* Not needed. Only used to silence a warning. */ |
drh | 784c1b9 | 2016-01-30 16:59:56 +0000 | [diff] [blame] | 6445 | rc = sqlite3VdbeIdxRowid(db, pC->uc.pCursor, &rowid); |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 6446 | if( rc!=SQLITE_OK ){ |
| 6447 | goto abort_due_to_error; |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 6448 | } |
drh | 170ad68 | 2017-06-02 15:44:22 +0000 | [diff] [blame] | 6449 | if( pOp->opcode==OP_DeferredSeek ){ |
drh | 784c1b9 | 2016-01-30 16:59:56 +0000 | [diff] [blame] | 6450 | assert( pOp->p3>=0 && pOp->p3<p->nCursor ); |
| 6451 | pTabCur = p->apCsr[pOp->p3]; |
| 6452 | assert( pTabCur!=0 ); |
| 6453 | assert( pTabCur->eCurType==CURTYPE_BTREE ); |
| 6454 | assert( pTabCur->uc.pCursor!=0 ); |
| 6455 | assert( pTabCur->isTable ); |
| 6456 | pTabCur->nullRow = 0; |
| 6457 | pTabCur->movetoTarget = rowid; |
| 6458 | pTabCur->deferredMoveto = 1; |
drh | fc56950 | 2022-02-25 20:11:59 +0000 | [diff] [blame] | 6459 | pTabCur->cacheStatus = CACHE_STALE; |
drh | 784c1b9 | 2016-01-30 16:59:56 +0000 | [diff] [blame] | 6460 | assert( pOp->p4type==P4_INTARRAY || pOp->p4.ai==0 ); |
drh | e44ac38 | 2021-03-18 13:19:41 +0000 | [diff] [blame] | 6461 | assert( !pTabCur->isEphemeral ); |
drh | b248668 | 2022-01-03 01:43:28 +0000 | [diff] [blame] | 6462 | pTabCur->ub.aAltMap = pOp->p4.ai; |
| 6463 | assert( !pC->isEphemeral ); |
drh | 784c1b9 | 2016-01-30 16:59:56 +0000 | [diff] [blame] | 6464 | pTabCur->pAltCursor = pC; |
| 6465 | }else{ |
| 6466 | pOut = out2Prerelease(p, pOp); |
| 6467 | pOut->u.i = rowid; |
drh | 784c1b9 | 2016-01-30 16:59:56 +0000 | [diff] [blame] | 6468 | } |
| 6469 | }else{ |
| 6470 | assert( pOp->opcode==OP_IdxRowid ); |
| 6471 | sqlite3VdbeMemSetNull(&aMem[pOp->p2]); |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 6472 | } |
| 6473 | break; |
| 6474 | } |
| 6475 | |
drh | be3da24 | 2019-12-29 00:52:41 +0000 | [diff] [blame] | 6476 | /* Opcode: FinishSeek P1 * * * * |
| 6477 | ** |
| 6478 | ** If cursor P1 was previously moved via OP_DeferredSeek, complete that |
| 6479 | ** seek operation now, without further delay. If the cursor seek has |
| 6480 | ** already occurred, this instruction is a no-op. |
| 6481 | */ |
| 6482 | case OP_FinishSeek: { |
| 6483 | VdbeCursor *pC; /* The P1 index cursor */ |
| 6484 | |
| 6485 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 6486 | pC = p->apCsr[pOp->p1]; |
| 6487 | if( pC->deferredMoveto ){ |
| 6488 | rc = sqlite3VdbeFinishMoveto(pC); |
| 6489 | if( rc ) goto abort_due_to_error; |
| 6490 | } |
| 6491 | break; |
| 6492 | } |
| 6493 | |
drh | c51ceeb | 2020-08-31 12:29:03 +0000 | [diff] [blame] | 6494 | /* Opcode: IdxGE P1 P2 P3 P4 * |
drh | f63552b | 2013-10-30 00:25:03 +0000 | [diff] [blame] | 6495 | ** Synopsis: key=r[P3@P4] |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 6496 | ** |
danielk1977 | 61dd583 | 2008-04-18 11:31:12 +0000 | [diff] [blame] | 6497 | ** The P4 register values beginning with P3 form an unpacked index |
drh | 4a1d365 | 2014-02-14 15:13:36 +0000 | [diff] [blame] | 6498 | ** key that omits the PRIMARY KEY. Compare this key value against the index |
| 6499 | ** that P1 is currently pointing to, ignoring the PRIMARY KEY or ROWID |
| 6500 | ** fields at the end. |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 6501 | ** |
danielk1977 | 61dd583 | 2008-04-18 11:31:12 +0000 | [diff] [blame] | 6502 | ** If the P1 index entry is greater than or equal to the key value |
| 6503 | ** then jump to P2. Otherwise fall through to the next instruction. |
drh | 4a1d365 | 2014-02-14 15:13:36 +0000 | [diff] [blame] | 6504 | */ |
drh | c51ceeb | 2020-08-31 12:29:03 +0000 | [diff] [blame] | 6505 | /* Opcode: IdxGT P1 P2 P3 P4 * |
drh | 4a1d365 | 2014-02-14 15:13:36 +0000 | [diff] [blame] | 6506 | ** Synopsis: key=r[P3@P4] |
drh | 772ae62 | 2004-05-19 13:13:08 +0000 | [diff] [blame] | 6507 | ** |
drh | 4a1d365 | 2014-02-14 15:13:36 +0000 | [diff] [blame] | 6508 | ** The P4 register values beginning with P3 form an unpacked index |
| 6509 | ** key that omits the PRIMARY KEY. Compare this key value against the index |
| 6510 | ** that P1 is currently pointing to, ignoring the PRIMARY KEY or ROWID |
| 6511 | ** fields at the end. |
| 6512 | ** |
| 6513 | ** If the P1 index entry is greater than the key value |
| 6514 | ** then jump to P2. Otherwise fall through to the next instruction. |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 6515 | */ |
drh | c51ceeb | 2020-08-31 12:29:03 +0000 | [diff] [blame] | 6516 | /* Opcode: IdxLT P1 P2 P3 P4 * |
drh | f63552b | 2013-10-30 00:25:03 +0000 | [diff] [blame] | 6517 | ** Synopsis: key=r[P3@P4] |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 6518 | ** |
danielk1977 | 61dd583 | 2008-04-18 11:31:12 +0000 | [diff] [blame] | 6519 | ** The P4 register values beginning with P3 form an unpacked index |
drh | 4a1d365 | 2014-02-14 15:13:36 +0000 | [diff] [blame] | 6520 | ** key that omits the PRIMARY KEY or ROWID. Compare this key value against |
| 6521 | ** the index that P1 is currently pointing to, ignoring the PRIMARY KEY or |
| 6522 | ** ROWID on the P1 index. |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 6523 | ** |
danielk1977 | 61dd583 | 2008-04-18 11:31:12 +0000 | [diff] [blame] | 6524 | ** If the P1 index entry is less than the key value then jump to P2. |
| 6525 | ** Otherwise fall through to the next instruction. |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 6526 | */ |
drh | c51ceeb | 2020-08-31 12:29:03 +0000 | [diff] [blame] | 6527 | /* Opcode: IdxLE P1 P2 P3 P4 * |
drh | 4a1d365 | 2014-02-14 15:13:36 +0000 | [diff] [blame] | 6528 | ** Synopsis: key=r[P3@P4] |
| 6529 | ** |
| 6530 | ** The P4 register values beginning with P3 form an unpacked index |
| 6531 | ** key that omits the PRIMARY KEY or ROWID. Compare this key value against |
| 6532 | ** the index that P1 is currently pointing to, ignoring the PRIMARY KEY or |
| 6533 | ** ROWID on the P1 index. |
| 6534 | ** |
| 6535 | ** If the P1 index entry is less than or equal to the key value then jump |
| 6536 | ** to P2. Otherwise fall through to the next instruction. |
| 6537 | */ |
| 6538 | case OP_IdxLE: /* jump */ |
| 6539 | case OP_IdxGT: /* jump */ |
drh | 93952eb | 2009-11-13 19:43:43 +0000 | [diff] [blame] | 6540 | case OP_IdxLT: /* jump */ |
drh | 4a1d365 | 2014-02-14 15:13:36 +0000 | [diff] [blame] | 6541 | case OP_IdxGE: { /* jump */ |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 6542 | VdbeCursor *pC; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 6543 | int res; |
| 6544 | UnpackedRecord r; |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 6545 | |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 6546 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 6547 | pC = p->apCsr[pOp->p1]; |
| 6548 | assert( pC!=0 ); |
drh | d4187c7 | 2010-08-30 22:15:45 +0000 | [diff] [blame] | 6549 | assert( pC->isOrdered ); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 6550 | assert( pC->eCurType==CURTYPE_BTREE ); |
| 6551 | assert( pC->uc.pCursor!=0); |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 6552 | assert( pC->deferredMoveto==0 ); |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 6553 | assert( pOp->p4type==P4_INT32 ); |
| 6554 | r.pKeyInfo = pC->pKeyInfo; |
| 6555 | r.nField = (u16)pOp->p4.i; |
drh | 4a1d365 | 2014-02-14 15:13:36 +0000 | [diff] [blame] | 6556 | if( pOp->opcode<OP_IdxLT ){ |
| 6557 | assert( pOp->opcode==OP_IdxLE || pOp->opcode==OP_IdxGT ); |
dan | 1fed5da | 2014-02-25 21:01:25 +0000 | [diff] [blame] | 6558 | r.default_rc = -1; |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 6559 | }else{ |
drh | 4a1d365 | 2014-02-14 15:13:36 +0000 | [diff] [blame] | 6560 | assert( pOp->opcode==OP_IdxGE || pOp->opcode==OP_IdxLT ); |
dan | 1fed5da | 2014-02-25 21:01:25 +0000 | [diff] [blame] | 6561 | r.default_rc = 0; |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 6562 | } |
| 6563 | r.aMem = &aMem[pOp->p3]; |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 6564 | #ifdef SQLITE_DEBUG |
drh | 5eae974 | 2018-08-03 13:56:26 +0000 | [diff] [blame] | 6565 | { |
| 6566 | int i; |
| 6567 | for(i=0; i<r.nField; i++){ |
| 6568 | assert( memIsValid(&r.aMem[i]) ); |
| 6569 | REGISTER_TRACE(pOp->p3+i, &aMem[pOp->p3+i]); |
| 6570 | } |
| 6571 | } |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 6572 | #endif |
drh | c40076a | 2020-09-29 16:05:09 +0000 | [diff] [blame] | 6573 | |
| 6574 | /* Inlined version of sqlite3VdbeIdxKeyCompare() */ |
| 6575 | { |
| 6576 | i64 nCellKey = 0; |
| 6577 | BtCursor *pCur; |
| 6578 | Mem m; |
| 6579 | |
| 6580 | assert( pC->eCurType==CURTYPE_BTREE ); |
| 6581 | pCur = pC->uc.pCursor; |
| 6582 | assert( sqlite3BtreeCursorIsValid(pCur) ); |
| 6583 | nCellKey = sqlite3BtreePayloadSize(pCur); |
| 6584 | /* nCellKey will always be between 0 and 0xffffffff because of the way |
| 6585 | ** that btreeParseCellPtr() and sqlite3GetVarint32() are implemented */ |
| 6586 | if( nCellKey<=0 || nCellKey>0x7fffffff ){ |
| 6587 | rc = SQLITE_CORRUPT_BKPT; |
| 6588 | goto abort_due_to_error; |
| 6589 | } |
| 6590 | sqlite3VdbeMemInit(&m, db, 0); |
| 6591 | rc = sqlite3VdbeMemFromBtreeZeroOffset(pCur, (u32)nCellKey, &m); |
| 6592 | if( rc ) goto abort_due_to_error; |
| 6593 | res = sqlite3VdbeRecordCompareWithSkip(m.n, m.z, &r, 0); |
drh | fc85450 | 2022-03-02 17:50:59 +0000 | [diff] [blame] | 6594 | sqlite3VdbeMemReleaseMalloc(&m); |
drh | c40076a | 2020-09-29 16:05:09 +0000 | [diff] [blame] | 6595 | } |
| 6596 | /* End of inlined sqlite3VdbeIdxKeyCompare() */ |
| 6597 | |
drh | 4a1d365 | 2014-02-14 15:13:36 +0000 | [diff] [blame] | 6598 | assert( (OP_IdxLE&1)==(OP_IdxLT&1) && (OP_IdxGE&1)==(OP_IdxGT&1) ); |
| 6599 | if( (pOp->opcode&1)==(OP_IdxLT&1) ){ |
| 6600 | assert( pOp->opcode==OP_IdxLE || pOp->opcode==OP_IdxLT ); |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 6601 | res = -res; |
| 6602 | }else{ |
drh | 4a1d365 | 2014-02-14 15:13:36 +0000 | [diff] [blame] | 6603 | assert( pOp->opcode==OP_IdxGE || pOp->opcode==OP_IdxGT ); |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 6604 | res++; |
| 6605 | } |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 6606 | VdbeBranchTaken(res>0,2); |
drh | c40076a | 2020-09-29 16:05:09 +0000 | [diff] [blame] | 6607 | assert( rc==SQLITE_OK ); |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 6608 | if( res>0 ) goto jump_to_p2; |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 6609 | break; |
| 6610 | } |
| 6611 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 6612 | /* Opcode: Destroy P1 P2 P3 * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 6613 | ** |
| 6614 | ** Delete an entire database table or index whose root page in the database |
| 6615 | ** file is given by P1. |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 6616 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 6617 | ** The table being destroyed is in the main database file if P3==0. If |
| 6618 | ** 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] | 6619 | ** that is used to store tables create using CREATE TEMPORARY TABLE. |
| 6620 | ** |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 6621 | ** If AUTOVACUUM is enabled then it is possible that another root page |
| 6622 | ** might be moved into the newly deleted root page in order to keep all |
| 6623 | ** root pages contiguous at the beginning of the database. The former |
| 6624 | ** value of the root page that moved - its value before the move occurred - |
dan | a34adaf | 2017-04-08 14:11:47 +0000 | [diff] [blame] | 6625 | ** is stored in register P2. If no page movement was required (because the |
| 6626 | ** table being dropped was already the last one in the database) then a |
| 6627 | ** zero is stored in register P2. If AUTOVACUUM is disabled then a zero |
| 6628 | ** is stored in register P2. |
| 6629 | ** |
| 6630 | ** This opcode throws an error if there are any active reader VMs when |
| 6631 | ** it is invoked. This is done to avoid the difficulty associated with |
| 6632 | ** updating existing cursors when a root page is moved in an AUTOVACUUM |
| 6633 | ** database. This error is thrown even if the database is not an AUTOVACUUM |
| 6634 | ** db in order to avoid introducing an incompatibility between autovacuum |
| 6635 | ** and non-autovacuum modes. |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 6636 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 6637 | ** See also: Clear |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 6638 | */ |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 6639 | case OP_Destroy: { /* out2 */ |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6640 | int iMoved; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 6641 | int iDb; |
drh | 3a94987 | 2012-09-18 13:20:13 +0000 | [diff] [blame] | 6642 | |
drh | 4031baf | 2018-05-28 17:31:20 +0000 | [diff] [blame] | 6643 | sqlite3VdbeIncrWriteCounter(p, 0); |
drh | 9e92a47 | 2013-06-27 17:40:30 +0000 | [diff] [blame] | 6644 | assert( p->readOnly==0 ); |
drh | 055f298 | 2016-01-15 15:06:41 +0000 | [diff] [blame] | 6645 | assert( pOp->p1>1 ); |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 6646 | pOut = out2Prerelease(p, pOp); |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 6647 | pOut->flags = MEM_Null; |
drh | 086723a | 2015-03-24 12:51:52 +0000 | [diff] [blame] | 6648 | if( db->nVdbeRead > db->nVDestroy+1 ){ |
danielk1977 | e6efa74 | 2004-11-10 11:55:10 +0000 | [diff] [blame] | 6649 | rc = SQLITE_LOCKED; |
drh | 77658e2 | 2007-12-04 16:54:52 +0000 | [diff] [blame] | 6650 | p->errorAction = OE_Abort; |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 6651 | goto abort_due_to_error; |
danielk1977 | e6efa74 | 2004-11-10 11:55:10 +0000 | [diff] [blame] | 6652 | }else{ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 6653 | iDb = pOp->p3; |
drh | a7ab6d8 | 2014-07-21 15:44:39 +0000 | [diff] [blame] | 6654 | assert( DbMaskTest(p->btreeMask, iDb) ); |
drh | 2dc0648 | 2013-12-11 00:59:10 +0000 | [diff] [blame] | 6655 | iMoved = 0; /* Not needed. Only to silence a warning. */ |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 6656 | rc = sqlite3BtreeDropTable(db->aDb[iDb].pBt, pOp->p1, &iMoved); |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 6657 | pOut->flags = MEM_Int; |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 6658 | pOut->u.i = iMoved; |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 6659 | if( rc ) goto abort_due_to_error; |
drh | 3765df4 | 2006-06-28 18:18:09 +0000 | [diff] [blame] | 6660 | #ifndef SQLITE_OMIT_AUTOVACUUM |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 6661 | if( iMoved!=0 ){ |
drh | cdf011d | 2011-04-04 21:25:28 +0000 | [diff] [blame] | 6662 | sqlite3RootPageMoved(db, iDb, iMoved, pOp->p1); |
| 6663 | /* All OP_Destroy operations occur on the same btree */ |
| 6664 | assert( resetSchemaOnFault==0 || resetSchemaOnFault==iDb+1 ); |
| 6665 | resetSchemaOnFault = iDb+1; |
danielk1977 | e6efa74 | 2004-11-10 11:55:10 +0000 | [diff] [blame] | 6666 | } |
drh | 3765df4 | 2006-06-28 18:18:09 +0000 | [diff] [blame] | 6667 | #endif |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6668 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 6669 | break; |
| 6670 | } |
| 6671 | |
danielk1977 | c7af484 | 2008-10-27 13:59:33 +0000 | [diff] [blame] | 6672 | /* Opcode: Clear P1 P2 P3 |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 6673 | ** |
| 6674 | ** Delete all contents of the database table or index whose root page |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 6675 | ** in the database file is given by P1. But, unlike Destroy, do not |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 6676 | ** remove the table or index from the database file. |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 6677 | ** |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 6678 | ** The table being clear is in the main database file if P2==0. If |
| 6679 | ** P2==1 then the table to be clear is in the auxiliary database file |
| 6680 | ** that is used to store tables create using CREATE TEMPORARY TABLE. |
| 6681 | ** |
drh | a6df0e6 | 2021-06-03 18:51:51 +0000 | [diff] [blame] | 6682 | ** If the P3 value is non-zero, then the row change count is incremented |
| 6683 | ** by the number of rows in the table being cleared. If P3 is greater |
| 6684 | ** than zero, then the value stored in register P3 is also incremented |
| 6685 | ** by the number of rows in the table being cleared. |
danielk1977 | c7af484 | 2008-10-27 13:59:33 +0000 | [diff] [blame] | 6686 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 6687 | ** See also: Destroy |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 6688 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 6689 | case OP_Clear: { |
dan | 2c71887 | 2021-06-22 18:32:05 +0000 | [diff] [blame] | 6690 | i64 nChange; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 6691 | |
drh | 4031baf | 2018-05-28 17:31:20 +0000 | [diff] [blame] | 6692 | sqlite3VdbeIncrWriteCounter(p, 0); |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 6693 | nChange = 0; |
drh | 9e92a47 | 2013-06-27 17:40:30 +0000 | [diff] [blame] | 6694 | assert( p->readOnly==0 ); |
drh | a7ab6d8 | 2014-07-21 15:44:39 +0000 | [diff] [blame] | 6695 | assert( DbMaskTest(p->btreeMask, pOp->p2) ); |
drh | a6df0e6 | 2021-06-03 18:51:51 +0000 | [diff] [blame] | 6696 | rc = sqlite3BtreeClearTable(db->aDb[pOp->p2].pBt, (u32)pOp->p1, &nChange); |
danielk1977 | c7af484 | 2008-10-27 13:59:33 +0000 | [diff] [blame] | 6697 | if( pOp->p3 ){ |
| 6698 | p->nChange += nChange; |
| 6699 | if( pOp->p3>0 ){ |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 6700 | assert( memIsValid(&aMem[pOp->p3]) ); |
| 6701 | memAboutToChange(p, &aMem[pOp->p3]); |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 6702 | aMem[pOp->p3].u.i += nChange; |
danielk1977 | c7af484 | 2008-10-27 13:59:33 +0000 | [diff] [blame] | 6703 | } |
| 6704 | } |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 6705 | if( rc ) goto abort_due_to_error; |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 6706 | break; |
| 6707 | } |
| 6708 | |
drh | 65ea12c | 2014-03-19 17:41:36 +0000 | [diff] [blame] | 6709 | /* Opcode: ResetSorter P1 * * * * |
drh | 079a307 | 2014-03-19 14:10:55 +0000 | [diff] [blame] | 6710 | ** |
drh | 65ea12c | 2014-03-19 17:41:36 +0000 | [diff] [blame] | 6711 | ** Delete all contents from the ephemeral table or sorter |
| 6712 | ** that is open on cursor P1. |
drh | 079a307 | 2014-03-19 14:10:55 +0000 | [diff] [blame] | 6713 | ** |
drh | 65ea12c | 2014-03-19 17:41:36 +0000 | [diff] [blame] | 6714 | ** This opcode only works for cursors used for sorting and |
| 6715 | ** opened with OP_OpenEphemeral or OP_SorterOpen. |
drh | 079a307 | 2014-03-19 14:10:55 +0000 | [diff] [blame] | 6716 | */ |
drh | 65ea12c | 2014-03-19 17:41:36 +0000 | [diff] [blame] | 6717 | case OP_ResetSorter: { |
drh | 079a307 | 2014-03-19 14:10:55 +0000 | [diff] [blame] | 6718 | VdbeCursor *pC; |
| 6719 | |
| 6720 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 6721 | pC = p->apCsr[pOp->p1]; |
| 6722 | assert( pC!=0 ); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 6723 | if( isSorter(pC) ){ |
| 6724 | sqlite3VdbeSorterReset(db, pC->uc.pSorter); |
drh | 65ea12c | 2014-03-19 17:41:36 +0000 | [diff] [blame] | 6725 | }else{ |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 6726 | assert( pC->eCurType==CURTYPE_BTREE ); |
drh | 65ea12c | 2014-03-19 17:41:36 +0000 | [diff] [blame] | 6727 | assert( pC->isEphemeral ); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 6728 | rc = sqlite3BtreeClearTableOfCursor(pC->uc.pCursor); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 6729 | if( rc ) goto abort_due_to_error; |
drh | 65ea12c | 2014-03-19 17:41:36 +0000 | [diff] [blame] | 6730 | } |
drh | 079a307 | 2014-03-19 14:10:55 +0000 | [diff] [blame] | 6731 | break; |
| 6732 | } |
| 6733 | |
drh | 0f3f766 | 2017-08-18 14:34:28 +0000 | [diff] [blame] | 6734 | /* Opcode: CreateBtree P1 P2 P3 * * |
| 6735 | ** Synopsis: r[P2]=root iDb=P1 flags=P3 |
drh | 5b2fd56 | 2001-09-13 15:21:31 +0000 | [diff] [blame] | 6736 | ** |
drh | 0f3f766 | 2017-08-18 14:34:28 +0000 | [diff] [blame] | 6737 | ** Allocate a new b-tree in the main database file if P1==0 or in the |
| 6738 | ** TEMP database file if P1==1 or in an attached database if |
| 6739 | ** 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] | 6740 | ** it must be 2 (BTREE_BLOBKEY) for an index or WITHOUT ROWID table. |
drh | 0f3f766 | 2017-08-18 14:34:28 +0000 | [diff] [blame] | 6741 | ** 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] | 6742 | */ |
drh | 0f3f766 | 2017-08-18 14:34:28 +0000 | [diff] [blame] | 6743 | case OP_CreateBtree: { /* out2 */ |
drh | abc3815 | 2020-07-22 13:38:04 +0000 | [diff] [blame] | 6744 | Pgno pgno; |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 6745 | Db *pDb; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 6746 | |
drh | 4031baf | 2018-05-28 17:31:20 +0000 | [diff] [blame] | 6747 | sqlite3VdbeIncrWriteCounter(p, 0); |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 6748 | pOut = out2Prerelease(p, pOp); |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 6749 | pgno = 0; |
drh | 0f3f766 | 2017-08-18 14:34:28 +0000 | [diff] [blame] | 6750 | assert( pOp->p3==BTREE_INTKEY || pOp->p3==BTREE_BLOBKEY ); |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 6751 | assert( pOp->p1>=0 && pOp->p1<db->nDb ); |
drh | a7ab6d8 | 2014-07-21 15:44:39 +0000 | [diff] [blame] | 6752 | assert( DbMaskTest(p->btreeMask, pOp->p1) ); |
drh | 9e92a47 | 2013-06-27 17:40:30 +0000 | [diff] [blame] | 6753 | assert( p->readOnly==0 ); |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 6754 | pDb = &db->aDb[pOp->p1]; |
| 6755 | assert( pDb->pBt!=0 ); |
drh | 0f3f766 | 2017-08-18 14:34:28 +0000 | [diff] [blame] | 6756 | rc = sqlite3BtreeCreateTable(pDb->pBt, &pgno, pOp->p3); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 6757 | if( rc ) goto abort_due_to_error; |
drh | 88a003e | 2008-12-11 16:17:03 +0000 | [diff] [blame] | 6758 | pOut->u.i = pgno; |
drh | 5b2fd56 | 2001-09-13 15:21:31 +0000 | [diff] [blame] | 6759 | break; |
| 6760 | } |
| 6761 | |
drh | 4a54bb5 | 2017-02-18 15:58:52 +0000 | [diff] [blame] | 6762 | /* Opcode: SqlExec * * * P4 * |
| 6763 | ** |
| 6764 | ** Run the SQL statement or statements specified in the P4 string. |
| 6765 | */ |
| 6766 | case OP_SqlExec: { |
drh | 4031baf | 2018-05-28 17:31:20 +0000 | [diff] [blame] | 6767 | sqlite3VdbeIncrWriteCounter(p, 0); |
drh | bce0414 | 2017-02-23 00:58:36 +0000 | [diff] [blame] | 6768 | db->nSqlExec++; |
drh | 4a54bb5 | 2017-02-18 15:58:52 +0000 | [diff] [blame] | 6769 | rc = sqlite3_exec(db, pOp->p4.z, 0, 0, 0); |
drh | bce0414 | 2017-02-23 00:58:36 +0000 | [diff] [blame] | 6770 | db->nSqlExec--; |
drh | 4a54bb5 | 2017-02-18 15:58:52 +0000 | [diff] [blame] | 6771 | if( rc ) goto abort_due_to_error; |
| 6772 | break; |
| 6773 | } |
| 6774 | |
drh | 2264584 | 2011-03-24 01:34:03 +0000 | [diff] [blame] | 6775 | /* Opcode: ParseSchema P1 * * P4 * |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 6776 | ** |
drh | 346a70c | 2020-06-15 20:27:35 +0000 | [diff] [blame] | 6777 | ** Read and parse all entries from the schema table of database P1 |
drh | 1595abc | 2018-08-14 19:27:51 +0000 | [diff] [blame] | 6778 | ** that match the WHERE clause P4. If P4 is a NULL pointer, then the |
| 6779 | ** entire schema for P1 is reparsed. |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 6780 | ** |
| 6781 | ** This opcode invokes the parser to create a new virtual machine, |
shane | 21e7feb | 2008-05-30 15:59:49 +0000 | [diff] [blame] | 6782 | ** then runs the new virtual machine. It is thus a re-entrant opcode. |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 6783 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 6784 | case OP_ParseSchema: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 6785 | int iDb; |
drh | 067b92b | 2020-06-19 15:24:12 +0000 | [diff] [blame] | 6786 | const char *zSchema; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 6787 | char *zSql; |
| 6788 | InitData initData; |
| 6789 | |
drh | bdaec52 | 2011-04-04 00:14:43 +0000 | [diff] [blame] | 6790 | /* Any prepared statement that invokes this opcode will hold mutexes |
| 6791 | ** on every btree. This is a prerequisite for invoking |
| 6792 | ** sqlite3InitCallback(). |
| 6793 | */ |
| 6794 | #ifdef SQLITE_DEBUG |
| 6795 | for(iDb=0; iDb<db->nDb; iDb++){ |
| 6796 | assert( iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) ); |
| 6797 | } |
| 6798 | #endif |
drh | bdaec52 | 2011-04-04 00:14:43 +0000 | [diff] [blame] | 6799 | |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 6800 | iDb = pOp->p1; |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 6801 | assert( iDb>=0 && iDb<db->nDb ); |
drh | fe97234 | 2021-06-07 11:50:23 +0000 | [diff] [blame] | 6802 | assert( DbHasProperty(db, iDb, DB_SchemaLoaded) |
| 6803 | || db->mallocFailed |
| 6804 | || (CORRUPT_DB && (db->flags & SQLITE_NoSchemaError)!=0) ); |
dan | e325ffe | 2018-08-11 13:40:20 +0000 | [diff] [blame] | 6805 | |
| 6806 | #ifndef SQLITE_OMIT_ALTERTABLE |
| 6807 | if( pOp->p4.z==0 ){ |
| 6808 | sqlite3SchemaClear(db->aDb[iDb].pSchema); |
dan | b0c7920 | 2018-08-11 18:34:25 +0000 | [diff] [blame] | 6809 | db->mDbFlags &= ~DBFLAG_SchemaKnownOk; |
dan | 6a5a13d | 2021-02-17 20:08:22 +0000 | [diff] [blame] | 6810 | rc = sqlite3InitOne(db, iDb, &p->zErrMsg, pOp->p5); |
dan | e325ffe | 2018-08-11 13:40:20 +0000 | [diff] [blame] | 6811 | db->mDbFlags |= DBFLAG_SchemaChange; |
dan | 0d5fa6b | 2018-08-24 17:55:49 +0000 | [diff] [blame] | 6812 | p->expired = 0; |
dan | e325ffe | 2018-08-11 13:40:20 +0000 | [diff] [blame] | 6813 | }else |
| 6814 | #endif |
drh | 1595abc | 2018-08-14 19:27:51 +0000 | [diff] [blame] | 6815 | { |
drh | a4a871c | 2021-11-04 14:04:20 +0000 | [diff] [blame] | 6816 | zSchema = LEGACY_SCHEMA_TABLE; |
danielk1977 | a8bbef8 | 2009-03-23 17:11:26 +0000 | [diff] [blame] | 6817 | initData.db = db; |
mistachkin | 1c06b47 | 2018-09-27 00:04:31 +0000 | [diff] [blame] | 6818 | initData.iDb = iDb; |
danielk1977 | a8bbef8 | 2009-03-23 17:11:26 +0000 | [diff] [blame] | 6819 | initData.pzErrMsg = &p->zErrMsg; |
drh | 9fd88e8 | 2018-09-07 11:08:31 +0000 | [diff] [blame] | 6820 | initData.mInitFlags = 0; |
drh | 3b3ddba | 2020-07-22 18:03:56 +0000 | [diff] [blame] | 6821 | initData.mxPage = sqlite3BtreeLastPage(db->aDb[iDb].pBt); |
danielk1977 | a8bbef8 | 2009-03-23 17:11:26 +0000 | [diff] [blame] | 6822 | zSql = sqlite3MPrintf(db, |
drh | c5a93d4 | 2019-08-12 00:08:07 +0000 | [diff] [blame] | 6823 | "SELECT*FROM\"%w\".%s WHERE %s ORDER BY rowid", |
drh | 067b92b | 2020-06-19 15:24:12 +0000 | [diff] [blame] | 6824 | db->aDb[iDb].zDbSName, zSchema, pOp->p4.z); |
danielk1977 | a8bbef8 | 2009-03-23 17:11:26 +0000 | [diff] [blame] | 6825 | if( zSql==0 ){ |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 6826 | rc = SQLITE_NOMEM_BKPT; |
danielk1977 | a8bbef8 | 2009-03-23 17:11:26 +0000 | [diff] [blame] | 6827 | }else{ |
danielk1977 | a8bbef8 | 2009-03-23 17:11:26 +0000 | [diff] [blame] | 6828 | assert( db->init.busy==0 ); |
| 6829 | db->init.busy = 1; |
| 6830 | initData.rc = SQLITE_OK; |
drh | 6b86e51 | 2019-01-05 21:09:37 +0000 | [diff] [blame] | 6831 | initData.nInitRow = 0; |
danielk1977 | a8bbef8 | 2009-03-23 17:11:26 +0000 | [diff] [blame] | 6832 | assert( !db->mallocFailed ); |
| 6833 | rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0); |
| 6834 | if( rc==SQLITE_OK ) rc = initData.rc; |
drh | 6b86e51 | 2019-01-05 21:09:37 +0000 | [diff] [blame] | 6835 | if( rc==SQLITE_OK && initData.nInitRow==0 ){ |
| 6836 | /* The OP_ParseSchema opcode with a non-NULL P4 argument should parse |
| 6837 | ** at least one SQL statement. Any less than that indicates that |
drh | 1e32bed | 2020-06-19 13:33:53 +0000 | [diff] [blame] | 6838 | ** the sqlite_schema table is corrupt. */ |
drh | 6b86e51 | 2019-01-05 21:09:37 +0000 | [diff] [blame] | 6839 | rc = SQLITE_CORRUPT_BKPT; |
| 6840 | } |
drh | dbd6a7d | 2017-04-05 12:39:49 +0000 | [diff] [blame] | 6841 | sqlite3DbFreeNN(db, zSql); |
danielk1977 | a8bbef8 | 2009-03-23 17:11:26 +0000 | [diff] [blame] | 6842 | db->init.busy = 0; |
danielk1977 | a8bbef8 | 2009-03-23 17:11:26 +0000 | [diff] [blame] | 6843 | } |
drh | 3c23a88 | 2007-01-09 14:01:13 +0000 | [diff] [blame] | 6844 | } |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 6845 | if( rc ){ |
| 6846 | sqlite3ResetAllSchemasOfConnection(db); |
| 6847 | if( rc==SQLITE_NOMEM ){ |
| 6848 | goto no_mem; |
| 6849 | } |
| 6850 | goto abort_due_to_error; |
danielk1977 | 261919c | 2005-12-06 12:52:59 +0000 | [diff] [blame] | 6851 | } |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 6852 | break; |
| 6853 | } |
| 6854 | |
drh | 8bfdf72 | 2009-06-19 14:06:03 +0000 | [diff] [blame] | 6855 | #if !defined(SQLITE_OMIT_ANALYZE) |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 6856 | /* Opcode: LoadAnalysis P1 * * * * |
drh | 497e446 | 2005-07-23 03:18:40 +0000 | [diff] [blame] | 6857 | ** |
| 6858 | ** Read the sqlite_stat1 table for database P1 and load the content |
| 6859 | ** of that table into the internal index hash table. This will cause |
| 6860 | ** the analysis to be used when preparing all subsequent queries. |
| 6861 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 6862 | case OP_LoadAnalysis: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 6863 | assert( pOp->p1>=0 && pOp->p1<db->nDb ); |
| 6864 | rc = sqlite3AnalysisLoad(db, pOp->p1); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 6865 | if( rc ) goto abort_due_to_error; |
drh | 497e446 | 2005-07-23 03:18:40 +0000 | [diff] [blame] | 6866 | break; |
| 6867 | } |
drh | 8bfdf72 | 2009-06-19 14:06:03 +0000 | [diff] [blame] | 6868 | #endif /* !defined(SQLITE_OMIT_ANALYZE) */ |
drh | 497e446 | 2005-07-23 03:18:40 +0000 | [diff] [blame] | 6869 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 6870 | /* Opcode: DropTable P1 * * P4 * |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 6871 | ** |
| 6872 | ** Remove the internal (in-memory) data structures that describe |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 6873 | ** the table named P4 in database P1. This is called after a table |
drh | 5dad9a3 | 2014-07-25 18:37:42 +0000 | [diff] [blame] | 6874 | ** is dropped from disk (using the Destroy opcode) in order to keep |
| 6875 | ** the internal representation of the |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 6876 | ** schema consistent with what is on disk. |
| 6877 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 6878 | case OP_DropTable: { |
drh | 4031baf | 2018-05-28 17:31:20 +0000 | [diff] [blame] | 6879 | sqlite3VdbeIncrWriteCounter(p, 0); |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 6880 | sqlite3UnlinkAndDeleteTable(db, pOp->p1, pOp->p4.z); |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 6881 | break; |
| 6882 | } |
| 6883 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 6884 | /* Opcode: DropIndex P1 * * P4 * |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 6885 | ** |
| 6886 | ** Remove the internal (in-memory) data structures that describe |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 6887 | ** the index named P4 in database P1. This is called after an index |
drh | 5dad9a3 | 2014-07-25 18:37:42 +0000 | [diff] [blame] | 6888 | ** is dropped from disk (using the Destroy opcode) |
| 6889 | ** in order to keep the internal representation of the |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 6890 | ** schema consistent with what is on disk. |
| 6891 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 6892 | case OP_DropIndex: { |
drh | 4031baf | 2018-05-28 17:31:20 +0000 | [diff] [blame] | 6893 | sqlite3VdbeIncrWriteCounter(p, 0); |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 6894 | sqlite3UnlinkAndDeleteIndex(db, pOp->p1, pOp->p4.z); |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 6895 | break; |
| 6896 | } |
| 6897 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 6898 | /* Opcode: DropTrigger P1 * * P4 * |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 6899 | ** |
| 6900 | ** Remove the internal (in-memory) data structures that describe |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 6901 | ** the trigger named P4 in database P1. This is called after a trigger |
drh | 5dad9a3 | 2014-07-25 18:37:42 +0000 | [diff] [blame] | 6902 | ** is dropped from disk (using the Destroy opcode) in order to keep |
| 6903 | ** the internal representation of the |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 6904 | ** schema consistent with what is on disk. |
| 6905 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 6906 | case OP_DropTrigger: { |
drh | 4031baf | 2018-05-28 17:31:20 +0000 | [diff] [blame] | 6907 | sqlite3VdbeIncrWriteCounter(p, 0); |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 6908 | sqlite3UnlinkAndDeleteTrigger(db, pOp->p1, pOp->p4.z); |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 6909 | break; |
| 6910 | } |
| 6911 | |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 6912 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 6913 | #ifndef SQLITE_OMIT_INTEGRITY_CHECK |
drh | 98968b2 | 2016-03-15 22:00:39 +0000 | [diff] [blame] | 6914 | /* Opcode: IntegrityCk P1 P2 P3 P4 P5 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 6915 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 6916 | ** Do an analysis of the currently open database. Store in |
| 6917 | ** register P1 the text of an error message describing any problems. |
| 6918 | ** If no problems are found, store a NULL in register P1. |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 6919 | ** |
drh | 66accfc | 2017-02-22 18:04:42 +0000 | [diff] [blame] | 6920 | ** The register P3 contains one less than the maximum number of allowed errors. |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 6921 | ** At most reg(P3) errors will be reported. |
| 6922 | ** In other words, the analysis stops as soon as reg(P1) errors are |
| 6923 | ** seen. Reg(P1) is updated with the number of errors remaining. |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 6924 | ** |
drh | 98968b2 | 2016-03-15 22:00:39 +0000 | [diff] [blame] | 6925 | ** The root page numbers of all tables in the database are integers |
| 6926 | ** stored in P4_INTARRAY argument. |
drh | 2150432 | 2002-06-25 13:16:02 +0000 | [diff] [blame] | 6927 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 6928 | ** If P5 is not zero, the check is done on the auxiliary database |
drh | 2150432 | 2002-06-25 13:16:02 +0000 | [diff] [blame] | 6929 | ** file, not the main database file. |
drh | 1dd397f | 2002-02-03 03:34:07 +0000 | [diff] [blame] | 6930 | ** |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 6931 | ** This opcode is used to implement the integrity_check pragma. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 6932 | */ |
drh | aaab572 | 2002-02-19 13:39:21 +0000 | [diff] [blame] | 6933 | case OP_IntegrityCk: { |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 6934 | int nRoot; /* Number of tables to check. (Number of root pages.) */ |
drh | abc3815 | 2020-07-22 13:38:04 +0000 | [diff] [blame] | 6935 | Pgno *aRoot; /* Array of rootpage numbers for tables to be checked */ |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 6936 | int nErr; /* Number of errors reported */ |
| 6937 | char *z; /* Text of the error report */ |
| 6938 | Mem *pnErr; /* Register keeping track of errors remaining */ |
drh | 9e92a47 | 2013-06-27 17:40:30 +0000 | [diff] [blame] | 6939 | |
drh | 1713afb | 2013-06-28 01:24:57 +0000 | [diff] [blame] | 6940 | assert( p->bIsReader ); |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 6941 | nRoot = pOp->p2; |
drh | 98968b2 | 2016-03-15 22:00:39 +0000 | [diff] [blame] | 6942 | aRoot = pOp->p4.ai; |
drh | 7906975 | 2004-05-22 21:30:40 +0000 | [diff] [blame] | 6943 | assert( nRoot>0 ); |
mistachkin | cec5f1d | 2020-08-04 16:11:37 +0000 | [diff] [blame] | 6944 | assert( aRoot[0]==(Pgno)nRoot ); |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 6945 | assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) ); |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 6946 | pnErr = &aMem[pOp->p3]; |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 6947 | assert( (pnErr->flags & MEM_Int)!=0 ); |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 6948 | assert( (pnErr->flags & (MEM_Str|MEM_Blob))==0 ); |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 6949 | pIn1 = &aMem[pOp->p1]; |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 6950 | assert( pOp->p5<db->nDb ); |
drh | a7ab6d8 | 2014-07-21 15:44:39 +0000 | [diff] [blame] | 6951 | assert( DbMaskTest(p->btreeMask, pOp->p5) ); |
drh | 21f6daa | 2019-10-11 14:21:48 +0000 | [diff] [blame] | 6952 | z = sqlite3BtreeIntegrityCheck(db, db->aDb[pOp->p5].pBt, &aRoot[1], nRoot, |
drh | 66accfc | 2017-02-22 18:04:42 +0000 | [diff] [blame] | 6953 | (int)pnErr->u.i+1, &nErr); |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 6954 | sqlite3VdbeMemSetNull(pIn1); |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 6955 | if( nErr==0 ){ |
| 6956 | assert( z==0 ); |
drh | c890fec | 2008-08-01 20:10:08 +0000 | [diff] [blame] | 6957 | }else if( z==0 ){ |
| 6958 | goto no_mem; |
drh | 1dd397f | 2002-02-03 03:34:07 +0000 | [diff] [blame] | 6959 | }else{ |
drh | 66accfc | 2017-02-22 18:04:42 +0000 | [diff] [blame] | 6960 | pnErr->u.i -= nErr-1; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 6961 | sqlite3VdbeMemSetStr(pIn1, z, -1, SQLITE_UTF8, sqlite3_free); |
danielk1977 | 8a6b541 | 2004-05-24 07:04:25 +0000 | [diff] [blame] | 6962 | } |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 6963 | UPDATE_MAX_BLOBSIZE(pIn1); |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 6964 | sqlite3VdbeChangeEncoding(pIn1, encoding); |
drh | 21f6daa | 2019-10-11 14:21:48 +0000 | [diff] [blame] | 6965 | goto check_for_interrupt; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 6966 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 6967 | #endif /* SQLITE_OMIT_INTEGRITY_CHECK */ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 6968 | |
drh | 3d4501e | 2008-12-04 20:40:10 +0000 | [diff] [blame] | 6969 | /* Opcode: RowSetAdd P1 P2 * * * |
drh | 72e26de | 2016-08-24 21:24:04 +0000 | [diff] [blame] | 6970 | ** Synopsis: rowset(P1)=r[P2] |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 6971 | ** |
drh | bb6783b | 2017-04-29 18:02:49 +0000 | [diff] [blame] | 6972 | ** Insert the integer value held by register P2 into a RowSet object |
drh | 3d4501e | 2008-12-04 20:40:10 +0000 | [diff] [blame] | 6973 | ** held in register P1. |
| 6974 | ** |
| 6975 | ** An assertion fails if P2 is not an integer. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 6976 | */ |
drh | 93952eb | 2009-11-13 19:43:43 +0000 | [diff] [blame] | 6977 | case OP_RowSetAdd: { /* in1, in2 */ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 6978 | pIn1 = &aMem[pOp->p1]; |
| 6979 | pIn2 = &aMem[pOp->p2]; |
drh | 93952eb | 2009-11-13 19:43:43 +0000 | [diff] [blame] | 6980 | assert( (pIn2->flags & MEM_Int)!=0 ); |
drh | 9d67afc | 2018-08-29 20:24:03 +0000 | [diff] [blame] | 6981 | if( (pIn1->flags & MEM_Blob)==0 ){ |
| 6982 | if( sqlite3VdbeMemSetRowSet(pIn1) ) goto no_mem; |
drh | 3d4501e | 2008-12-04 20:40:10 +0000 | [diff] [blame] | 6983 | } |
drh | 9d67afc | 2018-08-29 20:24:03 +0000 | [diff] [blame] | 6984 | assert( sqlite3VdbeMemIsRowSet(pIn1) ); |
| 6985 | sqlite3RowSetInsert((RowSet*)pIn1->z, pIn2->u.i); |
drh | 3d4501e | 2008-12-04 20:40:10 +0000 | [diff] [blame] | 6986 | break; |
| 6987 | } |
| 6988 | |
| 6989 | /* Opcode: RowSetRead P1 P2 P3 * * |
drh | 72e26de | 2016-08-24 21:24:04 +0000 | [diff] [blame] | 6990 | ** Synopsis: r[P3]=rowset(P1) |
drh | 3d4501e | 2008-12-04 20:40:10 +0000 | [diff] [blame] | 6991 | ** |
drh | bb6783b | 2017-04-29 18:02:49 +0000 | [diff] [blame] | 6992 | ** Extract the smallest value from the RowSet object in P1 |
| 6993 | ** and put that value into register P3. |
| 6994 | ** Or, if RowSet object P1 is initially empty, leave P3 |
drh | 3d4501e | 2008-12-04 20:40:10 +0000 | [diff] [blame] | 6995 | ** unchanged and jump to instruction P2. |
| 6996 | */ |
drh | 93952eb | 2009-11-13 19:43:43 +0000 | [diff] [blame] | 6997 | case OP_RowSetRead: { /* jump, in1, out3 */ |
drh | 3d4501e | 2008-12-04 20:40:10 +0000 | [diff] [blame] | 6998 | i64 val; |
drh | 49afe3a | 2013-07-10 03:05:14 +0000 | [diff] [blame] | 6999 | |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 7000 | pIn1 = &aMem[pOp->p1]; |
drh | 9d67afc | 2018-08-29 20:24:03 +0000 | [diff] [blame] | 7001 | assert( (pIn1->flags & MEM_Blob)==0 || sqlite3VdbeMemIsRowSet(pIn1) ); |
| 7002 | if( (pIn1->flags & MEM_Blob)==0 |
| 7003 | || sqlite3RowSetNext((RowSet*)pIn1->z, &val)==0 |
drh | 3d4501e | 2008-12-04 20:40:10 +0000 | [diff] [blame] | 7004 | ){ |
| 7005 | /* The boolean index is empty */ |
drh | 93952eb | 2009-11-13 19:43:43 +0000 | [diff] [blame] | 7006 | sqlite3VdbeMemSetNull(pIn1); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 7007 | VdbeBranchTaken(1,2); |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 7008 | goto jump_to_p2_and_check_for_interrupt; |
drh | 3d4501e | 2008-12-04 20:40:10 +0000 | [diff] [blame] | 7009 | }else{ |
| 7010 | /* A value was pulled from the index */ |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 7011 | VdbeBranchTaken(0,2); |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 7012 | sqlite3VdbeMemSetInt64(&aMem[pOp->p3], val); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 7013 | } |
drh | 49afe3a | 2013-07-10 03:05:14 +0000 | [diff] [blame] | 7014 | goto check_for_interrupt; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 7015 | } |
| 7016 | |
drh | 1b26c7c | 2009-04-22 02:15:47 +0000 | [diff] [blame] | 7017 | /* Opcode: RowSetTest P1 P2 P3 P4 |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 7018 | ** Synopsis: if r[P3] in rowset(P1) goto P2 |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 7019 | ** |
drh | ade9760 | 2009-04-21 15:05:18 +0000 | [diff] [blame] | 7020 | ** 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] | 7021 | ** contains a RowSet object and that RowSet object contains |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 7022 | ** the value held in P3, jump to register P2. Otherwise, insert the |
drh | 1b26c7c | 2009-04-22 02:15:47 +0000 | [diff] [blame] | 7023 | ** integer in P3 into the RowSet and continue on to the |
drh | ade9760 | 2009-04-21 15:05:18 +0000 | [diff] [blame] | 7024 | ** next opcode. |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 7025 | ** |
drh | bb6783b | 2017-04-29 18:02:49 +0000 | [diff] [blame] | 7026 | ** The RowSet object is optimized for the case where sets of integers |
| 7027 | ** are inserted in distinct phases, which each set contains no duplicates. |
| 7028 | ** Each set is identified by a unique P4 value. The first set |
| 7029 | ** must have P4==0, the final set must have P4==-1, and for all other sets |
| 7030 | ** must have P4>0. |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 7031 | ** |
| 7032 | ** This allows optimizations: (a) when P4==0 there is no need to test |
drh | bb6783b | 2017-04-29 18:02:49 +0000 | [diff] [blame] | 7033 | ** the RowSet object for P3, as it is guaranteed not to contain it, |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 7034 | ** (b) when P4==-1 there is no need to insert the value, as it will |
| 7035 | ** never be tested for, and (c) when a value that is part of set X is |
| 7036 | ** inserted, there is no need to search to see if the same value was |
| 7037 | ** previously inserted as part of set X (only if it was previously |
| 7038 | ** inserted as part of some other set). |
| 7039 | */ |
drh | 1b26c7c | 2009-04-22 02:15:47 +0000 | [diff] [blame] | 7040 | case OP_RowSetTest: { /* jump, in1, in3 */ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 7041 | int iSet; |
| 7042 | int exists; |
| 7043 | |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 7044 | pIn1 = &aMem[pOp->p1]; |
| 7045 | pIn3 = &aMem[pOp->p3]; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 7046 | iSet = pOp->p4.i; |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 7047 | assert( pIn3->flags&MEM_Int ); |
| 7048 | |
drh | 1b26c7c | 2009-04-22 02:15:47 +0000 | [diff] [blame] | 7049 | /* If there is anything other than a rowset object in memory cell P1, |
| 7050 | ** delete it now and initialize P1 with an empty rowset |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 7051 | */ |
drh | 9d67afc | 2018-08-29 20:24:03 +0000 | [diff] [blame] | 7052 | if( (pIn1->flags & MEM_Blob)==0 ){ |
| 7053 | if( sqlite3VdbeMemSetRowSet(pIn1) ) goto no_mem; |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 7054 | } |
drh | 9d67afc | 2018-08-29 20:24:03 +0000 | [diff] [blame] | 7055 | assert( sqlite3VdbeMemIsRowSet(pIn1) ); |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 7056 | assert( pOp->p4type==P4_INT32 ); |
drh | 1b26c7c | 2009-04-22 02:15:47 +0000 | [diff] [blame] | 7057 | assert( iSet==-1 || iSet>=0 ); |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 7058 | if( iSet ){ |
drh | 9d67afc | 2018-08-29 20:24:03 +0000 | [diff] [blame] | 7059 | exists = sqlite3RowSetTest((RowSet*)pIn1->z, iSet, pIn3->u.i); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 7060 | VdbeBranchTaken(exists!=0,2); |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 7061 | if( exists ) goto jump_to_p2; |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 7062 | } |
| 7063 | if( iSet>=0 ){ |
drh | 9d67afc | 2018-08-29 20:24:03 +0000 | [diff] [blame] | 7064 | sqlite3RowSetInsert((RowSet*)pIn1->z, pIn3->u.i); |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 7065 | } |
| 7066 | break; |
| 7067 | } |
| 7068 | |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 7069 | |
danielk1977 | 93758c8 | 2005-01-21 08:13:14 +0000 | [diff] [blame] | 7070 | #ifndef SQLITE_OMIT_TRIGGER |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 7071 | |
drh | 0fd6135 | 2014-02-07 02:29:45 +0000 | [diff] [blame] | 7072 | /* Opcode: Program P1 P2 P3 P4 P5 |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 7073 | ** |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 7074 | ** Execute the trigger program passed as P4 (type P4_SUBPROGRAM). |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 7075 | ** |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 7076 | ** P1 contains the address of the memory cell that contains the first memory |
| 7077 | ** cell in an array of values used as arguments to the sub-program. P2 |
| 7078 | ** contains the address to jump to if the sub-program throws an IGNORE |
| 7079 | ** exception using the RAISE() function. Register P3 contains the address |
| 7080 | ** of a memory cell in this (the parent) VM that is used to allocate the |
| 7081 | ** memory required by the sub-vdbe at runtime. |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 7082 | ** |
| 7083 | ** P4 is a pointer to the VM containing the trigger program. |
drh | 0fd6135 | 2014-02-07 02:29:45 +0000 | [diff] [blame] | 7084 | ** |
| 7085 | ** If P5 is non-zero, then recursive program invocation is enabled. |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 7086 | */ |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 7087 | case OP_Program: { /* jump */ |
dan | 65a7cd1 | 2009-09-01 12:16:01 +0000 | [diff] [blame] | 7088 | int nMem; /* Number of memory registers for sub-program */ |
| 7089 | int nByte; /* Bytes of runtime space required for sub-program */ |
| 7090 | Mem *pRt; /* Register to allocate runtime space */ |
| 7091 | Mem *pMem; /* Used to iterate through memory cells */ |
| 7092 | Mem *pEnd; /* Last memory cell in new array */ |
| 7093 | VdbeFrame *pFrame; /* New vdbe frame to execute in */ |
| 7094 | SubProgram *pProgram; /* Sub-program to execute */ |
| 7095 | void *t; /* Token identifying trigger */ |
| 7096 | |
| 7097 | pProgram = pOp->p4.pProgram; |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 7098 | pRt = &aMem[pOp->p3]; |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 7099 | assert( pProgram->nOp>0 ); |
| 7100 | |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 7101 | /* If the p5 flag is clear, then recursive invocation of triggers is |
| 7102 | ** disabled for backwards compatibility (p5 is set if this sub-program |
| 7103 | ** is really a trigger, not a foreign key action, and the flag set |
| 7104 | ** and cleared by the "PRAGMA recursive_triggers" command is clear). |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 7105 | ** |
| 7106 | ** It is recursive invocation of triggers, at the SQL level, that is |
| 7107 | ** disabled. In some cases a single trigger may generate more than one |
| 7108 | ** SubProgram (if the trigger may be executed with more than one different |
| 7109 | ** ON CONFLICT algorithm). SubProgram structures associated with a |
| 7110 | ** single trigger all have the same value for the SubProgram.token |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 7111 | ** variable. */ |
| 7112 | if( pOp->p5 ){ |
dan | 65a7cd1 | 2009-09-01 12:16:01 +0000 | [diff] [blame] | 7113 | t = pProgram->token; |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 7114 | for(pFrame=p->pFrame; pFrame && pFrame->token!=t; pFrame=pFrame->pParent); |
| 7115 | if( pFrame ) break; |
| 7116 | } |
| 7117 | |
dan | f589450 | 2009-10-07 18:41:19 +0000 | [diff] [blame] | 7118 | if( p->nFrame>=db->aLimit[SQLITE_LIMIT_TRIGGER_DEPTH] ){ |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 7119 | rc = SQLITE_ERROR; |
drh | 22c17b8 | 2015-05-15 04:13:15 +0000 | [diff] [blame] | 7120 | sqlite3VdbeError(p, "too many levels of trigger recursion"); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 7121 | goto abort_due_to_error; |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 7122 | } |
| 7123 | |
| 7124 | /* Register pRt is used to store the memory required to save the state |
| 7125 | ** of the current program, and the memory required at runtime to execute |
| 7126 | ** the trigger program. If this trigger has been fired before, then pRt |
| 7127 | ** is already allocated. Otherwise, it must be initialized. */ |
drh | 72f56ef | 2018-08-29 18:47:22 +0000 | [diff] [blame] | 7128 | if( (pRt->flags&MEM_Blob)==0 ){ |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 7129 | /* SubProgram.nMem is set to the number of memory cells used by the |
| 7130 | ** program stored in SubProgram.aOp. As well as these, one memory |
| 7131 | ** cell is required for each cursor used by the program. Set local |
| 7132 | ** variable nMem (and later, VdbeFrame.nChildMem) to this value. |
| 7133 | */ |
dan | 65a7cd1 | 2009-09-01 12:16:01 +0000 | [diff] [blame] | 7134 | nMem = pProgram->nMem + pProgram->nCsr; |
drh | 3cdce92 | 2016-03-21 00:30:40 +0000 | [diff] [blame] | 7135 | assert( nMem>0 ); |
| 7136 | if( pProgram->nCsr==0 ) nMem++; |
dan | 65a7cd1 | 2009-09-01 12:16:01 +0000 | [diff] [blame] | 7137 | nByte = ROUND8(sizeof(VdbeFrame)) |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 7138 | + nMem * sizeof(Mem) |
drh | ab087d4 | 2017-03-24 17:59:56 +0000 | [diff] [blame] | 7139 | + pProgram->nCsr * sizeof(VdbeCursor*) |
| 7140 | + (pProgram->nOp + 7)/8; |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 7141 | pFrame = sqlite3DbMallocZero(db, nByte); |
| 7142 | if( !pFrame ){ |
| 7143 | goto no_mem; |
| 7144 | } |
| 7145 | sqlite3VdbeMemRelease(pRt); |
drh | 72f56ef | 2018-08-29 18:47:22 +0000 | [diff] [blame] | 7146 | pRt->flags = MEM_Blob|MEM_Dyn; |
| 7147 | pRt->z = (char*)pFrame; |
| 7148 | pRt->n = nByte; |
| 7149 | pRt->xDel = sqlite3VdbeFrameMemDel; |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 7150 | |
| 7151 | pFrame->v = p; |
| 7152 | pFrame->nChildMem = nMem; |
| 7153 | pFrame->nChildCsr = pProgram->nCsr; |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 7154 | pFrame->pc = (int)(pOp - aOp); |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 7155 | pFrame->aMem = p->aMem; |
| 7156 | pFrame->nMem = p->nMem; |
| 7157 | pFrame->apCsr = p->apCsr; |
| 7158 | pFrame->nCursor = p->nCursor; |
| 7159 | pFrame->aOp = p->aOp; |
| 7160 | pFrame->nOp = p->nOp; |
| 7161 | pFrame->token = pProgram->token; |
dan | e2f771b | 2014-11-03 15:33:17 +0000 | [diff] [blame] | 7162 | #ifdef SQLITE_ENABLE_STMT_SCANSTATUS |
dan | 43764a8 | 2014-11-01 21:00:04 +0000 | [diff] [blame] | 7163 | pFrame->anExec = p->anExec; |
dan | e2f771b | 2014-11-03 15:33:17 +0000 | [diff] [blame] | 7164 | #endif |
drh | 72f56ef | 2018-08-29 18:47:22 +0000 | [diff] [blame] | 7165 | #ifdef SQLITE_DEBUG |
| 7166 | pFrame->iFrameMagic = SQLITE_FRAME_MAGIC; |
| 7167 | #endif |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 7168 | |
| 7169 | pEnd = &VdbeFrameMem(pFrame)[pFrame->nChildMem]; |
| 7170 | for(pMem=VdbeFrameMem(pFrame); pMem!=pEnd; pMem++){ |
drh | a5750cf | 2014-02-07 13:20:31 +0000 | [diff] [blame] | 7171 | pMem->flags = MEM_Undefined; |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 7172 | pMem->db = db; |
| 7173 | } |
| 7174 | }else{ |
drh | 72f56ef | 2018-08-29 18:47:22 +0000 | [diff] [blame] | 7175 | pFrame = (VdbeFrame*)pRt->z; |
| 7176 | assert( pRt->xDel==sqlite3VdbeFrameMemDel ); |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 7177 | assert( pProgram->nMem+pProgram->nCsr==pFrame->nChildMem |
| 7178 | || (pProgram->nCsr==0 && pProgram->nMem+1==pFrame->nChildMem) ); |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 7179 | assert( pProgram->nCsr==pFrame->nChildCsr ); |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 7180 | assert( (int)(pOp - aOp)==pFrame->pc ); |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 7181 | } |
| 7182 | |
| 7183 | p->nFrame++; |
| 7184 | pFrame->pParent = p->pFrame; |
drh | fae58d5 | 2017-01-26 17:26:44 +0000 | [diff] [blame] | 7185 | pFrame->lastRowid = db->lastRowid; |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 7186 | pFrame->nChange = p->nChange; |
dan | c3da667 | 2014-10-28 18:24:16 +0000 | [diff] [blame] | 7187 | pFrame->nDbChange = p->db->nChange; |
dan | 3200132 | 2016-02-19 18:54:29 +0000 | [diff] [blame] | 7188 | assert( pFrame->pAuxData==0 ); |
| 7189 | pFrame->pAuxData = p->pAuxData; |
| 7190 | p->pAuxData = 0; |
dan | 2832ad4 | 2009-08-31 15:27:27 +0000 | [diff] [blame] | 7191 | p->nChange = 0; |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 7192 | p->pFrame = pFrame; |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 7193 | p->aMem = aMem = VdbeFrameMem(pFrame); |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 7194 | p->nMem = pFrame->nChildMem; |
shane | cea72b2 | 2009-09-07 04:38:36 +0000 | [diff] [blame] | 7195 | p->nCursor = (u16)pFrame->nChildCsr; |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 7196 | p->apCsr = (VdbeCursor **)&aMem[p->nMem]; |
drh | ab087d4 | 2017-03-24 17:59:56 +0000 | [diff] [blame] | 7197 | pFrame->aOnce = (u8*)&p->apCsr[pProgram->nCsr]; |
drh | 18333ef | 2017-03-24 18:38:41 +0000 | [diff] [blame] | 7198 | memset(pFrame->aOnce, 0, (pProgram->nOp + 7)/8); |
drh | bbe879d | 2009-11-14 18:04:35 +0000 | [diff] [blame] | 7199 | p->aOp = aOp = pProgram->aOp; |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 7200 | p->nOp = pProgram->nOp; |
dan | e2f771b | 2014-11-03 15:33:17 +0000 | [diff] [blame] | 7201 | #ifdef SQLITE_ENABLE_STMT_SCANSTATUS |
dan | 43764a8 | 2014-11-01 21:00:04 +0000 | [diff] [blame] | 7202 | p->anExec = 0; |
dan | e2f771b | 2014-11-03 15:33:17 +0000 | [diff] [blame] | 7203 | #endif |
drh | b2e61bc | 2019-01-25 19:29:01 +0000 | [diff] [blame] | 7204 | #ifdef SQLITE_DEBUG |
| 7205 | /* Verify that second and subsequent executions of the same trigger do not |
| 7206 | ** try to reuse register values from the first use. */ |
| 7207 | { |
| 7208 | int i; |
| 7209 | for(i=0; i<p->nMem; i++){ |
| 7210 | aMem[i].pScopyFrom = 0; /* Prevent false-positive AboutToChange() errs */ |
drh | f5cfe6f | 2020-03-03 20:48:12 +0000 | [diff] [blame] | 7211 | MemSetTypeFlag(&aMem[i], MEM_Undefined); /* Fault if this reg is reused */ |
drh | b2e61bc | 2019-01-25 19:29:01 +0000 | [diff] [blame] | 7212 | } |
| 7213 | } |
| 7214 | #endif |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 7215 | pOp = &aOp[-1]; |
drh | b1af9c6 | 2019-02-20 13:55:45 +0000 | [diff] [blame] | 7216 | goto check_for_interrupt; |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 7217 | } |
| 7218 | |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 7219 | /* Opcode: Param P1 P2 * * * |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 7220 | ** |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 7221 | ** This opcode is only ever present in sub-programs called via the |
| 7222 | ** OP_Program instruction. Copy a value currently stored in a memory |
| 7223 | ** cell of the calling (parent) frame to cell P2 in the current frames |
| 7224 | ** address space. This is used by trigger programs to access the new.* |
| 7225 | ** and old.* values. |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 7226 | ** |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 7227 | ** The address of the cell in the parent frame is determined by adding |
| 7228 | ** the value of the P1 argument to the value of the P1 argument to the |
| 7229 | ** calling OP_Program instruction. |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 7230 | */ |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 7231 | case OP_Param: { /* out2 */ |
dan | 65a7cd1 | 2009-09-01 12:16:01 +0000 | [diff] [blame] | 7232 | VdbeFrame *pFrame; |
| 7233 | Mem *pIn; |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 7234 | pOut = out2Prerelease(p, pOp); |
dan | 65a7cd1 | 2009-09-01 12:16:01 +0000 | [diff] [blame] | 7235 | pFrame = p->pFrame; |
| 7236 | pIn = &pFrame->aMem[pOp->p1 + pFrame->aOp[pFrame->pc].p1]; |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 7237 | sqlite3VdbeMemShallowCopy(pOut, pIn, MEM_Ephem); |
| 7238 | break; |
| 7239 | } |
| 7240 | |
danielk1977 | 93758c8 | 2005-01-21 08:13:14 +0000 | [diff] [blame] | 7241 | #endif /* #ifndef SQLITE_OMIT_TRIGGER */ |
rdc | b0c374f | 2004-02-20 22:53:38 +0000 | [diff] [blame] | 7242 | |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 7243 | #ifndef SQLITE_OMIT_FOREIGN_KEY |
dan | 32b09f2 | 2009-09-23 17:29:59 +0000 | [diff] [blame] | 7244 | /* Opcode: FkCounter P1 P2 * * * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 7245 | ** Synopsis: fkctr[P1]+=P2 |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 7246 | ** |
dan | 0ff297e | 2009-09-25 17:03:14 +0000 | [diff] [blame] | 7247 | ** Increment a "constraint counter" by P2 (P2 may be negative or positive). |
| 7248 | ** If P1 is non-zero, the database constraint counter is incremented |
| 7249 | ** (deferred foreign key constraints). Otherwise, if P1 is zero, the |
dan | 32b09f2 | 2009-09-23 17:29:59 +0000 | [diff] [blame] | 7250 | ** statement counter is incremented (immediate foreign key constraints). |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 7251 | */ |
dan | 32b09f2 | 2009-09-23 17:29:59 +0000 | [diff] [blame] | 7252 | case OP_FkCounter: { |
drh | 963c74d | 2013-07-11 12:19:12 +0000 | [diff] [blame] | 7253 | if( db->flags & SQLITE_DeferFKs ){ |
dan | cb3e4b7 | 2013-07-03 19:53:05 +0000 | [diff] [blame] | 7254 | db->nDeferredImmCons += pOp->p2; |
| 7255 | }else if( pOp->p1 ){ |
dan | 0ff297e | 2009-09-25 17:03:14 +0000 | [diff] [blame] | 7256 | db->nDeferredCons += pOp->p2; |
dan | 32b09f2 | 2009-09-23 17:29:59 +0000 | [diff] [blame] | 7257 | }else{ |
dan | 0ff297e | 2009-09-25 17:03:14 +0000 | [diff] [blame] | 7258 | p->nFkConstraint += pOp->p2; |
| 7259 | } |
| 7260 | break; |
| 7261 | } |
| 7262 | |
| 7263 | /* Opcode: FkIfZero P1 P2 * * * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 7264 | ** Synopsis: if fkctr[P1]==0 goto P2 |
dan | 0ff297e | 2009-09-25 17:03:14 +0000 | [diff] [blame] | 7265 | ** |
| 7266 | ** This opcode tests if a foreign key constraint-counter is currently zero. |
| 7267 | ** If so, jump to instruction P2. Otherwise, fall through to the next |
| 7268 | ** instruction. |
| 7269 | ** |
| 7270 | ** If P1 is non-zero, then the jump is taken if the database constraint-counter |
| 7271 | ** is zero (the one that counts deferred constraint violations). If P1 is |
| 7272 | ** zero, the jump is taken if the statement constraint-counter is zero |
| 7273 | ** (immediate foreign key constraint violations). |
| 7274 | */ |
| 7275 | case OP_FkIfZero: { /* jump */ |
| 7276 | if( pOp->p1 ){ |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 7277 | VdbeBranchTaken(db->nDeferredCons==0 && db->nDeferredImmCons==0, 2); |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 7278 | if( db->nDeferredCons==0 && db->nDeferredImmCons==0 ) goto jump_to_p2; |
dan | 0ff297e | 2009-09-25 17:03:14 +0000 | [diff] [blame] | 7279 | }else{ |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 7280 | VdbeBranchTaken(p->nFkConstraint==0 && db->nDeferredImmCons==0, 2); |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 7281 | if( p->nFkConstraint==0 && db->nDeferredImmCons==0 ) goto jump_to_p2; |
dan | 32b09f2 | 2009-09-23 17:29:59 +0000 | [diff] [blame] | 7282 | } |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 7283 | break; |
| 7284 | } |
| 7285 | #endif /* #ifndef SQLITE_OMIT_FOREIGN_KEY */ |
| 7286 | |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 7287 | #ifndef SQLITE_OMIT_AUTOINCREMENT |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 7288 | /* Opcode: MemMax P1 P2 * * * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 7289 | ** Synopsis: r[P1]=max(r[P1],r[P2]) |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 7290 | ** |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 7291 | ** P1 is a register in the root frame of this VM (the root frame is |
| 7292 | ** different from the current frame if this instruction is being executed |
| 7293 | ** within a sub-program). Set the value of register P1 to the maximum of |
| 7294 | ** its current value and the value in register P2. |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 7295 | ** |
| 7296 | ** This instruction throws an error if the memory cell is not initially |
| 7297 | ** an integer. |
| 7298 | */ |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 7299 | case OP_MemMax: { /* in2 */ |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 7300 | VdbeFrame *pFrame; |
| 7301 | if( p->pFrame ){ |
| 7302 | for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent); |
| 7303 | pIn1 = &pFrame->aMem[pOp->p1]; |
| 7304 | }else{ |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 7305 | pIn1 = &aMem[pOp->p1]; |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 7306 | } |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 7307 | assert( memIsValid(pIn1) ); |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 7308 | sqlite3VdbeMemIntegerify(pIn1); |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 7309 | pIn2 = &aMem[pOp->p2]; |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 7310 | sqlite3VdbeMemIntegerify(pIn2); |
| 7311 | if( pIn1->u.i<pIn2->u.i){ |
| 7312 | pIn1->u.i = pIn2->u.i; |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 7313 | } |
| 7314 | break; |
| 7315 | } |
| 7316 | #endif /* SQLITE_OMIT_AUTOINCREMENT */ |
| 7317 | |
drh | 8b0cf38 | 2015-10-06 21:07:06 +0000 | [diff] [blame] | 7318 | /* Opcode: IfPos P1 P2 P3 * * |
| 7319 | ** Synopsis: if r[P1]>0 then r[P1]-=P3, goto P2 |
danielk1977 | a2dc3b1 | 2005-02-05 12:48:48 +0000 | [diff] [blame] | 7320 | ** |
drh | 1689707 | 2015-03-07 00:57:37 +0000 | [diff] [blame] | 7321 | ** Register P1 must contain an integer. |
mistachkin | 91a3ecb | 2015-10-06 21:49:55 +0000 | [diff] [blame] | 7322 | ** 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] | 7323 | ** value in P1 and jump to P2. |
drh | 6f58f70 | 2006-01-08 05:26:41 +0000 | [diff] [blame] | 7324 | ** |
drh | 1689707 | 2015-03-07 00:57:37 +0000 | [diff] [blame] | 7325 | ** If the initial value of register P1 is less than 1, then the |
| 7326 | ** value is unchanged and control passes through to the next instruction. |
danielk1977 | a2dc3b1 | 2005-02-05 12:48:48 +0000 | [diff] [blame] | 7327 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 7328 | case OP_IfPos: { /* jump, in1 */ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 7329 | pIn1 = &aMem[pOp->p1]; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 7330 | assert( pIn1->flags&MEM_Int ); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 7331 | VdbeBranchTaken( pIn1->u.i>0, 2); |
drh | 8b0cf38 | 2015-10-06 21:07:06 +0000 | [diff] [blame] | 7332 | if( pIn1->u.i>0 ){ |
| 7333 | pIn1->u.i -= pOp->p3; |
| 7334 | goto jump_to_p2; |
| 7335 | } |
drh | ec7429a | 2005-10-06 16:53:14 +0000 | [diff] [blame] | 7336 | break; |
| 7337 | } |
| 7338 | |
drh | cc2fa4c | 2016-01-25 15:57:29 +0000 | [diff] [blame] | 7339 | /* Opcode: OffsetLimit P1 P2 P3 * * |
| 7340 | ** 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] | 7341 | ** |
drh | cc2fa4c | 2016-01-25 15:57:29 +0000 | [diff] [blame] | 7342 | ** This opcode performs a commonly used computation associated with |
drh | e24a6f5 | 2022-08-04 14:02:35 +0000 | [diff] [blame] | 7343 | ** LIMIT and OFFSET processing. r[P1] holds the limit counter. r[P3] |
drh | cc2fa4c | 2016-01-25 15:57:29 +0000 | [diff] [blame] | 7344 | ** holds the offset counter. The opcode computes the combined value |
| 7345 | ** of the LIMIT and OFFSET and stores that value in r[P2]. The r[P2] |
| 7346 | ** value computed is the total number of rows that will need to be |
| 7347 | ** visited in order to complete the query. |
| 7348 | ** |
| 7349 | ** If r[P3] is zero or negative, that means there is no OFFSET |
| 7350 | ** and r[P2] is set to be the value of the LIMIT, r[P1]. |
| 7351 | ** |
| 7352 | ** if r[P1] is zero or negative, that means there is no LIMIT |
| 7353 | ** and r[P2] is set to -1. |
| 7354 | ** |
| 7355 | ** 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] | 7356 | */ |
drh | cc2fa4c | 2016-01-25 15:57:29 +0000 | [diff] [blame] | 7357 | case OP_OffsetLimit: { /* in1, out2, in3 */ |
drh | 719da30 | 2016-12-10 04:06:49 +0000 | [diff] [blame] | 7358 | i64 x; |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 7359 | pIn1 = &aMem[pOp->p1]; |
drh | cc2fa4c | 2016-01-25 15:57:29 +0000 | [diff] [blame] | 7360 | pIn3 = &aMem[pOp->p3]; |
| 7361 | pOut = out2Prerelease(p, pOp); |
| 7362 | assert( pIn1->flags & MEM_Int ); |
| 7363 | assert( pIn3->flags & MEM_Int ); |
drh | 719da30 | 2016-12-10 04:06:49 +0000 | [diff] [blame] | 7364 | x = pIn1->u.i; |
| 7365 | if( x<=0 || sqlite3AddInt64(&x, pIn3->u.i>0?pIn3->u.i:0) ){ |
| 7366 | /* If the LIMIT is less than or equal to zero, loop forever. This |
| 7367 | ** is documented. But also, if the LIMIT+OFFSET exceeds 2^63 then |
| 7368 | ** also loop forever. This is undocumented. In fact, one could argue |
| 7369 | ** that the loop should terminate. But assuming 1 billion iterations |
| 7370 | ** per second (far exceeding the capabilities of any current hardware) |
| 7371 | ** it would take nearly 300 years to actually reach the limit. So |
| 7372 | ** looping forever is a reasonable approximation. */ |
| 7373 | pOut->u.i = -1; |
| 7374 | }else{ |
| 7375 | pOut->u.i = x; |
| 7376 | } |
drh | 15007a9 | 2006-01-08 18:10:17 +0000 | [diff] [blame] | 7377 | break; |
| 7378 | } |
| 7379 | |
drh | f99dd35 | 2016-12-18 17:42:00 +0000 | [diff] [blame] | 7380 | /* Opcode: IfNotZero P1 P2 * * * |
| 7381 | ** Synopsis: if r[P1]!=0 then r[P1]--, goto P2 |
drh | ec7429a | 2005-10-06 16:53:14 +0000 | [diff] [blame] | 7382 | ** |
drh | 1689707 | 2015-03-07 00:57:37 +0000 | [diff] [blame] | 7383 | ** Register P1 must contain an integer. If the content of register P1 is |
drh | f99dd35 | 2016-12-18 17:42:00 +0000 | [diff] [blame] | 7384 | ** initially greater than zero, then decrement the value in register P1. |
| 7385 | ** If it is non-zero (negative or positive) and then also jump to P2. |
| 7386 | ** If register P1 is initially zero, leave it unchanged and fall through. |
drh | ec7429a | 2005-10-06 16:53:14 +0000 | [diff] [blame] | 7387 | */ |
drh | 1689707 | 2015-03-07 00:57:37 +0000 | [diff] [blame] | 7388 | case OP_IfNotZero: { /* jump, in1 */ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 7389 | pIn1 = &aMem[pOp->p1]; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 7390 | assert( pIn1->flags&MEM_Int ); |
drh | 1689707 | 2015-03-07 00:57:37 +0000 | [diff] [blame] | 7391 | VdbeBranchTaken(pIn1->u.i<0, 2); |
| 7392 | if( pIn1->u.i ){ |
drh | f99dd35 | 2016-12-18 17:42:00 +0000 | [diff] [blame] | 7393 | if( pIn1->u.i>0 ) pIn1->u.i--; |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 7394 | goto jump_to_p2; |
drh | 1689707 | 2015-03-07 00:57:37 +0000 | [diff] [blame] | 7395 | } |
| 7396 | break; |
| 7397 | } |
| 7398 | |
| 7399 | /* Opcode: DecrJumpZero P1 P2 * * * |
| 7400 | ** Synopsis: if (--r[P1])==0 goto P2 |
| 7401 | ** |
drh | ab5be2e | 2016-11-30 05:08:59 +0000 | [diff] [blame] | 7402 | ** Register P1 must hold an integer. Decrement the value in P1 |
| 7403 | ** and jump to P2 if the new value is exactly zero. |
drh | 1689707 | 2015-03-07 00:57:37 +0000 | [diff] [blame] | 7404 | */ |
| 7405 | case OP_DecrJumpZero: { /* jump, in1 */ |
| 7406 | pIn1 = &aMem[pOp->p1]; |
| 7407 | assert( pIn1->flags&MEM_Int ); |
drh | ab5be2e | 2016-11-30 05:08:59 +0000 | [diff] [blame] | 7408 | if( pIn1->u.i>SMALLEST_INT64 ) pIn1->u.i--; |
| 7409 | VdbeBranchTaken(pIn1->u.i==0, 2); |
| 7410 | if( pIn1->u.i==0 ) goto jump_to_p2; |
drh | a2a49dc | 2008-01-02 14:28:13 +0000 | [diff] [blame] | 7411 | break; |
| 7412 | } |
| 7413 | |
drh | 1689707 | 2015-03-07 00:57:37 +0000 | [diff] [blame] | 7414 | |
drh | 8f26da6 | 2018-07-05 21:22:57 +0000 | [diff] [blame] | 7415 | /* Opcode: AggStep * P2 P3 P4 P5 |
drh | f63552b | 2013-10-30 00:25:03 +0000 | [diff] [blame] | 7416 | ** Synopsis: accum=r[P3] step(r[P2@P5]) |
drh | e509535 | 2002-02-24 03:25:14 +0000 | [diff] [blame] | 7417 | ** |
drh | 8f26da6 | 2018-07-05 21:22:57 +0000 | [diff] [blame] | 7418 | ** Execute the xStep function for an aggregate. |
| 7419 | ** The function has P5 arguments. P4 is a pointer to the |
dan | 9a94722 | 2018-06-14 19:06:36 +0000 | [diff] [blame] | 7420 | ** FuncDef structure that specifies the function. Register P3 is the |
drh | e2d9e7c | 2015-06-26 18:47:53 +0000 | [diff] [blame] | 7421 | ** accumulator. |
drh | e509535 | 2002-02-24 03:25:14 +0000 | [diff] [blame] | 7422 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 7423 | ** The P5 arguments are taken from register P2 and its |
| 7424 | ** successors. |
drh | e509535 | 2002-02-24 03:25:14 +0000 | [diff] [blame] | 7425 | */ |
drh | 8f26da6 | 2018-07-05 21:22:57 +0000 | [diff] [blame] | 7426 | /* Opcode: AggInverse * P2 P3 P4 P5 |
| 7427 | ** Synopsis: accum=r[P3] inverse(r[P2@P5]) |
| 7428 | ** |
| 7429 | ** Execute the xInverse function for an aggregate. |
| 7430 | ** The function has P5 arguments. P4 is a pointer to the |
| 7431 | ** FuncDef structure that specifies the function. Register P3 is the |
| 7432 | ** accumulator. |
| 7433 | ** |
| 7434 | ** The P5 arguments are taken from register P2 and its |
| 7435 | ** successors. |
| 7436 | */ |
| 7437 | /* Opcode: AggStep1 P1 P2 P3 P4 P5 |
drh | e2d9e7c | 2015-06-26 18:47:53 +0000 | [diff] [blame] | 7438 | ** Synopsis: accum=r[P3] step(r[P2@P5]) |
| 7439 | ** |
dan | 9a94722 | 2018-06-14 19:06:36 +0000 | [diff] [blame] | 7440 | ** Execute the xStep (if P1==0) or xInverse (if P1!=0) function for an |
| 7441 | ** aggregate. The function has P5 arguments. P4 is a pointer to the |
| 7442 | ** FuncDef structure that specifies the function. Register P3 is the |
| 7443 | ** accumulator. |
drh | e2d9e7c | 2015-06-26 18:47:53 +0000 | [diff] [blame] | 7444 | ** |
| 7445 | ** The P5 arguments are taken from register P2 and its |
| 7446 | ** successors. |
| 7447 | ** |
| 7448 | ** This opcode is initially coded as OP_AggStep0. On first evaluation, |
| 7449 | ** the FuncDef stored in P4 is converted into an sqlite3_context and |
| 7450 | ** the opcode is changed. In this way, the initialization of the |
| 7451 | ** sqlite3_context only happens once, instead of on each call to the |
| 7452 | ** step function. |
| 7453 | */ |
drh | 8f26da6 | 2018-07-05 21:22:57 +0000 | [diff] [blame] | 7454 | case OP_AggInverse: |
| 7455 | case OP_AggStep: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 7456 | int n; |
drh | 9c7c913 | 2015-06-26 18:16:52 +0000 | [diff] [blame] | 7457 | sqlite3_context *pCtx; |
drh | e509535 | 2002-02-24 03:25:14 +0000 | [diff] [blame] | 7458 | |
drh | 9c7c913 | 2015-06-26 18:16:52 +0000 | [diff] [blame] | 7459 | assert( pOp->p4type==P4_FUNCDEF ); |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 7460 | n = pOp->p5; |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 7461 | assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) ); |
| 7462 | 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] | 7463 | assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n ); |
drh | f09ac0b | 2018-01-23 03:44:06 +0000 | [diff] [blame] | 7464 | pCtx = sqlite3DbMallocRawNN(db, n*sizeof(sqlite3_value*) + |
| 7465 | (sizeof(pCtx[0]) + sizeof(Mem) - sizeof(sqlite3_value*))); |
drh | 9c7c913 | 2015-06-26 18:16:52 +0000 | [diff] [blame] | 7466 | if( pCtx==0 ) goto no_mem; |
| 7467 | pCtx->pMem = 0; |
drh | f09ac0b | 2018-01-23 03:44:06 +0000 | [diff] [blame] | 7468 | pCtx->pOut = (Mem*)&(pCtx->argv[n]); |
| 7469 | sqlite3VdbeMemInit(pCtx->pOut, db, MEM_Null); |
drh | 9c7c913 | 2015-06-26 18:16:52 +0000 | [diff] [blame] | 7470 | pCtx->pFunc = pOp->p4.pFunc; |
| 7471 | pCtx->iOp = (int)(pOp - aOp); |
| 7472 | pCtx->pVdbe = p; |
drh | f09ac0b | 2018-01-23 03:44:06 +0000 | [diff] [blame] | 7473 | pCtx->skipFlag = 0; |
| 7474 | pCtx->isError = 0; |
drh | 659fdb4 | 2022-04-01 15:31:58 +0000 | [diff] [blame] | 7475 | pCtx->enc = encoding; |
drh | 9c7c913 | 2015-06-26 18:16:52 +0000 | [diff] [blame] | 7476 | pCtx->argc = n; |
| 7477 | pOp->p4type = P4_FUNCCTX; |
| 7478 | pOp->p4.pCtx = pCtx; |
drh | 2c885d0 | 2018-07-07 19:36:04 +0000 | [diff] [blame] | 7479 | |
| 7480 | /* OP_AggInverse must have P1==1 and OP_AggStep must have P1==0 */ |
drh | 8f26da6 | 2018-07-05 21:22:57 +0000 | [diff] [blame] | 7481 | assert( pOp->p1==(pOp->opcode==OP_AggInverse) ); |
drh | 2c885d0 | 2018-07-07 19:36:04 +0000 | [diff] [blame] | 7482 | |
drh | 8f26da6 | 2018-07-05 21:22:57 +0000 | [diff] [blame] | 7483 | pOp->opcode = OP_AggStep1; |
drh | 9c7c913 | 2015-06-26 18:16:52 +0000 | [diff] [blame] | 7484 | /* Fall through into OP_AggStep */ |
drh | 08b9208 | 2020-08-10 14:18:00 +0000 | [diff] [blame] | 7485 | /* no break */ deliberate_fall_through |
drh | 9c7c913 | 2015-06-26 18:16:52 +0000 | [diff] [blame] | 7486 | } |
drh | 8f26da6 | 2018-07-05 21:22:57 +0000 | [diff] [blame] | 7487 | case OP_AggStep1: { |
drh | 9c7c913 | 2015-06-26 18:16:52 +0000 | [diff] [blame] | 7488 | int i; |
| 7489 | sqlite3_context *pCtx; |
| 7490 | Mem *pMem; |
drh | 9c7c913 | 2015-06-26 18:16:52 +0000 | [diff] [blame] | 7491 | |
| 7492 | assert( pOp->p4type==P4_FUNCCTX ); |
| 7493 | pCtx = pOp->p4.pCtx; |
| 7494 | pMem = &aMem[pOp->p3]; |
| 7495 | |
drh | 2c885d0 | 2018-07-07 19:36:04 +0000 | [diff] [blame] | 7496 | #ifdef SQLITE_DEBUG |
| 7497 | if( pOp->p1 ){ |
| 7498 | /* This is an OP_AggInverse call. Verify that xStep has always |
| 7499 | ** been called at least once prior to any xInverse call. */ |
| 7500 | assert( pMem->uTemp==0x1122e0e3 ); |
| 7501 | }else{ |
| 7502 | /* This is an OP_AggStep call. Mark it as such. */ |
| 7503 | pMem->uTemp = 0x1122e0e3; |
| 7504 | } |
| 7505 | #endif |
| 7506 | |
drh | 9c7c913 | 2015-06-26 18:16:52 +0000 | [diff] [blame] | 7507 | /* If this function is inside of a trigger, the register array in aMem[] |
| 7508 | ** might change from one evaluation to the next. The next block of code |
| 7509 | ** checks to see if the register array has changed, and if so it |
| 7510 | ** reinitializes the relavant parts of the sqlite3_context object */ |
| 7511 | if( pCtx->pMem != pMem ){ |
| 7512 | pCtx->pMem = pMem; |
| 7513 | for(i=pCtx->argc-1; i>=0; i--) pCtx->argv[i] = &aMem[pOp->p2+i]; |
| 7514 | } |
| 7515 | |
| 7516 | #ifdef SQLITE_DEBUG |
| 7517 | for(i=0; i<pCtx->argc; i++){ |
| 7518 | assert( memIsValid(pCtx->argv[i]) ); |
| 7519 | REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]); |
| 7520 | } |
| 7521 | #endif |
| 7522 | |
drh | abfcea2 | 2005-09-06 20:36:48 +0000 | [diff] [blame] | 7523 | pMem->n++; |
drh | f09ac0b | 2018-01-23 03:44:06 +0000 | [diff] [blame] | 7524 | assert( pCtx->pOut->flags==MEM_Null ); |
| 7525 | assert( pCtx->isError==0 ); |
| 7526 | assert( pCtx->skipFlag==0 ); |
dan | 67a9b8e | 2018-06-22 20:51:35 +0000 | [diff] [blame] | 7527 | #ifndef SQLITE_OMIT_WINDOWFUNC |
| 7528 | if( pOp->p1 ){ |
| 7529 | (pCtx->pFunc->xInverse)(pCtx,pCtx->argc,pCtx->argv); |
| 7530 | }else |
| 7531 | #endif |
| 7532 | (pCtx->pFunc->xSFunc)(pCtx,pCtx->argc,pCtx->argv); /* IMP: R-24505-23230 */ |
| 7533 | |
drh | f09ac0b | 2018-01-23 03:44:06 +0000 | [diff] [blame] | 7534 | if( pCtx->isError ){ |
| 7535 | if( pCtx->isError>0 ){ |
| 7536 | sqlite3VdbeError(p, "%s", sqlite3_value_text(pCtx->pOut)); |
drh | 9c7c913 | 2015-06-26 18:16:52 +0000 | [diff] [blame] | 7537 | rc = pCtx->isError; |
| 7538 | } |
drh | f09ac0b | 2018-01-23 03:44:06 +0000 | [diff] [blame] | 7539 | if( pCtx->skipFlag ){ |
| 7540 | assert( pOp[-1].opcode==OP_CollSeq ); |
| 7541 | i = pOp[-1].p1; |
| 7542 | if( i ) sqlite3VdbeMemSetInt64(&aMem[i], 1); |
| 7543 | pCtx->skipFlag = 0; |
| 7544 | } |
| 7545 | sqlite3VdbeMemRelease(pCtx->pOut); |
| 7546 | pCtx->pOut->flags = MEM_Null; |
| 7547 | pCtx->isError = 0; |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 7548 | if( rc ) goto abort_due_to_error; |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 7549 | } |
drh | f09ac0b | 2018-01-23 03:44:06 +0000 | [diff] [blame] | 7550 | assert( pCtx->pOut->flags==MEM_Null ); |
| 7551 | assert( pCtx->skipFlag==0 ); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 7552 | break; |
| 7553 | } |
| 7554 | |
drh | 8f26da6 | 2018-07-05 21:22:57 +0000 | [diff] [blame] | 7555 | /* Opcode: AggFinal P1 P2 * P4 * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 7556 | ** Synopsis: accum=r[P1] N=P2 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 7557 | ** |
dan | 9a94722 | 2018-06-14 19:06:36 +0000 | [diff] [blame] | 7558 | ** P1 is the memory location that is the accumulator for an aggregate |
drh | 8f26da6 | 2018-07-05 21:22:57 +0000 | [diff] [blame] | 7559 | ** or window function. Execute the finalizer function |
| 7560 | ** for an aggregate and store the result in P1. |
drh | a10a34b | 2005-09-07 22:09:48 +0000 | [diff] [blame] | 7561 | ** |
| 7562 | ** P2 is the number of arguments that the step function takes and |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 7563 | ** P4 is a pointer to the FuncDef for this function. The P2 |
drh | a10a34b | 2005-09-07 22:09:48 +0000 | [diff] [blame] | 7564 | ** argument is not used by this opcode. It is only there to disambiguate |
| 7565 | ** functions that can take varying numbers of arguments. The |
drh | 8be47a7 | 2018-07-05 20:05:29 +0000 | [diff] [blame] | 7566 | ** P4 argument is only needed for the case where |
drh | a10a34b | 2005-09-07 22:09:48 +0000 | [diff] [blame] | 7567 | ** the step function was not previously called. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 7568 | */ |
drh | 8f26da6 | 2018-07-05 21:22:57 +0000 | [diff] [blame] | 7569 | /* Opcode: AggValue * P2 P3 P4 * |
| 7570 | ** Synopsis: r[P3]=value N=P2 |
| 7571 | ** |
| 7572 | ** Invoke the xValue() function and store the result in register P3. |
| 7573 | ** |
| 7574 | ** P2 is the number of arguments that the step function takes and |
| 7575 | ** P4 is a pointer to the FuncDef for this function. The P2 |
| 7576 | ** argument is not used by this opcode. It is only there to disambiguate |
| 7577 | ** functions that can take varying numbers of arguments. The |
| 7578 | ** P4 argument is only needed for the case where |
| 7579 | ** the step function was not previously called. |
| 7580 | */ |
| 7581 | case OP_AggValue: |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 7582 | case OP_AggFinal: { |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 7583 | Mem *pMem; |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 7584 | assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) ); |
drh | 8f26da6 | 2018-07-05 21:22:57 +0000 | [diff] [blame] | 7585 | assert( pOp->p3==0 || pOp->opcode==OP_AggValue ); |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 7586 | pMem = &aMem[pOp->p1]; |
drh | a10a34b | 2005-09-07 22:09:48 +0000 | [diff] [blame] | 7587 | assert( (pMem->flags & ~(MEM_Null|MEM_Agg))==0 ); |
dan | 67a9b8e | 2018-06-22 20:51:35 +0000 | [diff] [blame] | 7588 | #ifndef SQLITE_OMIT_WINDOWFUNC |
dan | 86fb6e1 | 2018-05-16 20:58:07 +0000 | [diff] [blame] | 7589 | if( pOp->p3 ){ |
dan | 108e6b2 | 2019-03-18 18:55:35 +0000 | [diff] [blame] | 7590 | memAboutToChange(p, &aMem[pOp->p3]); |
dan | 86fb6e1 | 2018-05-16 20:58:07 +0000 | [diff] [blame] | 7591 | rc = sqlite3VdbeMemAggValue(pMem, &aMem[pOp->p3], pOp->p4.pFunc); |
dan | 660af93 | 2018-06-18 16:55:22 +0000 | [diff] [blame] | 7592 | pMem = &aMem[pOp->p3]; |
dan | 67a9b8e | 2018-06-22 20:51:35 +0000 | [diff] [blame] | 7593 | }else |
| 7594 | #endif |
drh | 8f26da6 | 2018-07-05 21:22:57 +0000 | [diff] [blame] | 7595 | { |
| 7596 | rc = sqlite3VdbeMemFinalize(pMem, pOp->p4.pFunc); |
| 7597 | } |
dan | 67a9b8e | 2018-06-22 20:51:35 +0000 | [diff] [blame] | 7598 | |
drh | 4c8555f | 2009-06-25 01:47:11 +0000 | [diff] [blame] | 7599 | if( rc ){ |
drh | 22c17b8 | 2015-05-15 04:13:15 +0000 | [diff] [blame] | 7600 | sqlite3VdbeError(p, "%s", sqlite3_value_text(pMem)); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 7601 | goto abort_due_to_error; |
drh | 90669c1 | 2006-01-20 15:45:36 +0000 | [diff] [blame] | 7602 | } |
drh | 2dca868 | 2008-03-21 17:13:13 +0000 | [diff] [blame] | 7603 | sqlite3VdbeChangeEncoding(pMem, encoding); |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 7604 | UPDATE_MAX_BLOBSIZE(pMem); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 7605 | break; |
| 7606 | } |
| 7607 | |
dan | 5cf5353 | 2010-05-01 16:40:20 +0000 | [diff] [blame] | 7608 | #ifndef SQLITE_OMIT_WAL |
dan | cdc1f04 | 2010-11-18 12:11:05 +0000 | [diff] [blame] | 7609 | /* Opcode: Checkpoint P1 P2 P3 * * |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 7610 | ** |
| 7611 | ** 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] | 7612 | ** WAL mode. Parameter P2 is one of SQLITE_CHECKPOINT_PASSIVE, FULL, |
| 7613 | ** 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] | 7614 | ** SQLITE_BUSY or not, respectively. Write the number of pages in the |
| 7615 | ** WAL after the checkpoint into mem[P3+1] and the number of pages |
| 7616 | ** in the WAL that have been checkpointed after the checkpoint |
| 7617 | ** completes into mem[P3+2]. However on an error, mem[P3+1] and |
| 7618 | ** mem[P3+2] are initialized to -1. |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 7619 | */ |
| 7620 | case OP_Checkpoint: { |
drh | 30aa3b9 | 2011-02-07 23:56:01 +0000 | [diff] [blame] | 7621 | int i; /* Loop counter */ |
| 7622 | int aRes[3]; /* Results */ |
| 7623 | Mem *pMem; /* Write results here */ |
| 7624 | |
drh | 9e92a47 | 2013-06-27 17:40:30 +0000 | [diff] [blame] | 7625 | assert( p->readOnly==0 ); |
drh | 30aa3b9 | 2011-02-07 23:56:01 +0000 | [diff] [blame] | 7626 | aRes[0] = 0; |
| 7627 | aRes[1] = aRes[2] = -1; |
dan | cdc1f04 | 2010-11-18 12:11:05 +0000 | [diff] [blame] | 7628 | assert( pOp->p2==SQLITE_CHECKPOINT_PASSIVE |
| 7629 | || pOp->p2==SQLITE_CHECKPOINT_FULL |
| 7630 | || pOp->p2==SQLITE_CHECKPOINT_RESTART |
dan | f26a154 | 2014-12-02 19:04:54 +0000 | [diff] [blame] | 7631 | || pOp->p2==SQLITE_CHECKPOINT_TRUNCATE |
dan | cdc1f04 | 2010-11-18 12:11:05 +0000 | [diff] [blame] | 7632 | ); |
drh | 30aa3b9 | 2011-02-07 23:56:01 +0000 | [diff] [blame] | 7633 | rc = sqlite3Checkpoint(db, pOp->p1, pOp->p2, &aRes[1], &aRes[2]); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 7634 | if( rc ){ |
| 7635 | if( rc!=SQLITE_BUSY ) goto abort_due_to_error; |
dan | cdc1f04 | 2010-11-18 12:11:05 +0000 | [diff] [blame] | 7636 | rc = SQLITE_OK; |
drh | 30aa3b9 | 2011-02-07 23:56:01 +0000 | [diff] [blame] | 7637 | aRes[0] = 1; |
dan | cdc1f04 | 2010-11-18 12:11:05 +0000 | [diff] [blame] | 7638 | } |
drh | 30aa3b9 | 2011-02-07 23:56:01 +0000 | [diff] [blame] | 7639 | for(i=0, pMem = &aMem[pOp->p3]; i<3; i++, pMem++){ |
| 7640 | sqlite3VdbeMemSetInt64(pMem, (i64)aRes[i]); |
| 7641 | } |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 7642 | break; |
| 7643 | }; |
dan | 5cf5353 | 2010-05-01 16:40:20 +0000 | [diff] [blame] | 7644 | #endif |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 7645 | |
drh | cac29a6 | 2010-07-02 19:36:52 +0000 | [diff] [blame] | 7646 | #ifndef SQLITE_OMIT_PRAGMA |
drh | 0fd6135 | 2014-02-07 02:29:45 +0000 | [diff] [blame] | 7647 | /* Opcode: JournalMode P1 P2 P3 * * |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 7648 | ** |
| 7649 | ** Change the journal mode of database P1 to P3. P3 must be one of the |
| 7650 | ** PAGER_JOURNALMODE_XXX values. If changing between the various rollback |
| 7651 | ** modes (delete, truncate, persist, off and memory), this is a simple |
| 7652 | ** operation. No IO is required. |
| 7653 | ** |
| 7654 | ** If changing into or out of WAL mode the procedure is more complicated. |
| 7655 | ** |
| 7656 | ** Write a string containing the final journal-mode to register P2. |
| 7657 | */ |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 7658 | case OP_JournalMode: { /* out2 */ |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 7659 | Btree *pBt; /* Btree to change journal mode of */ |
| 7660 | Pager *pPager; /* Pager associated with pBt */ |
drh | d80b233 | 2010-05-01 00:59:37 +0000 | [diff] [blame] | 7661 | int eNew; /* New journal mode */ |
| 7662 | int eOld; /* The old journal mode */ |
mistachkin | 59ee77c | 2012-09-13 15:26:44 +0000 | [diff] [blame] | 7663 | #ifndef SQLITE_OMIT_WAL |
drh | d80b233 | 2010-05-01 00:59:37 +0000 | [diff] [blame] | 7664 | const char *zFilename; /* Name of database file for pPager */ |
mistachkin | 59ee77c | 2012-09-13 15:26:44 +0000 | [diff] [blame] | 7665 | #endif |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 7666 | |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 7667 | pOut = out2Prerelease(p, pOp); |
drh | d80b233 | 2010-05-01 00:59:37 +0000 | [diff] [blame] | 7668 | eNew = pOp->p3; |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 7669 | assert( eNew==PAGER_JOURNALMODE_DELETE |
| 7670 | || eNew==PAGER_JOURNALMODE_TRUNCATE |
| 7671 | || eNew==PAGER_JOURNALMODE_PERSIST |
| 7672 | || eNew==PAGER_JOURNALMODE_OFF |
| 7673 | || eNew==PAGER_JOURNALMODE_MEMORY |
| 7674 | || eNew==PAGER_JOURNALMODE_WAL |
| 7675 | || eNew==PAGER_JOURNALMODE_QUERY |
| 7676 | ); |
| 7677 | assert( pOp->p1>=0 && pOp->p1<db->nDb ); |
drh | 9e92a47 | 2013-06-27 17:40:30 +0000 | [diff] [blame] | 7678 | assert( p->readOnly==0 ); |
drh | 3ebaee9 | 2010-05-06 21:37:22 +0000 | [diff] [blame] | 7679 | |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 7680 | pBt = db->aDb[pOp->p1].pBt; |
| 7681 | pPager = sqlite3BtreePager(pBt); |
drh | 0b9b430 | 2010-06-11 17:01:24 +0000 | [diff] [blame] | 7682 | eOld = sqlite3PagerGetJournalMode(pPager); |
| 7683 | if( eNew==PAGER_JOURNALMODE_QUERY ) eNew = eOld; |
dan | a8f249f | 2021-05-20 11:42:51 +0000 | [diff] [blame] | 7684 | assert( sqlite3BtreeHoldsMutex(pBt) ); |
drh | 0b9b430 | 2010-06-11 17:01:24 +0000 | [diff] [blame] | 7685 | if( !sqlite3PagerOkToChangeJournalMode(pPager) ) eNew = eOld; |
dan | 5cf5353 | 2010-05-01 16:40:20 +0000 | [diff] [blame] | 7686 | |
| 7687 | #ifndef SQLITE_OMIT_WAL |
drh | d4e0bb0 | 2012-05-27 01:19:04 +0000 | [diff] [blame] | 7688 | zFilename = sqlite3PagerFilename(pPager, 1); |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 7689 | |
drh | d80b233 | 2010-05-01 00:59:37 +0000 | [diff] [blame] | 7690 | /* Do not allow a transition to journal_mode=WAL for a database |
drh | 6e1f482 | 2010-07-13 23:41:40 +0000 | [diff] [blame] | 7691 | ** in temporary storage or if the VFS does not support shared memory |
drh | d80b233 | 2010-05-01 00:59:37 +0000 | [diff] [blame] | 7692 | */ |
| 7693 | if( eNew==PAGER_JOURNALMODE_WAL |
drh | 057fc81 | 2011-10-17 23:15:31 +0000 | [diff] [blame] | 7694 | && (sqlite3Strlen30(zFilename)==0 /* Temp file */ |
drh | 6e1f482 | 2010-07-13 23:41:40 +0000 | [diff] [blame] | 7695 | || !sqlite3PagerWalSupported(pPager)) /* No shared-memory support */ |
dan | e180c29 | 2010-04-26 17:42:56 +0000 | [diff] [blame] | 7696 | ){ |
drh | 0b9b430 | 2010-06-11 17:01:24 +0000 | [diff] [blame] | 7697 | eNew = eOld; |
dan | e180c29 | 2010-04-26 17:42:56 +0000 | [diff] [blame] | 7698 | } |
| 7699 | |
drh | 0b9b430 | 2010-06-11 17:01:24 +0000 | [diff] [blame] | 7700 | if( (eNew!=eOld) |
| 7701 | && (eOld==PAGER_JOURNALMODE_WAL || eNew==PAGER_JOURNALMODE_WAL) |
| 7702 | ){ |
dan | c0537fe | 2013-06-28 19:41:43 +0000 | [diff] [blame] | 7703 | if( !db->autoCommit || db->nVdbeRead>1 ){ |
drh | 0b9b430 | 2010-06-11 17:01:24 +0000 | [diff] [blame] | 7704 | rc = SQLITE_ERROR; |
drh | 22c17b8 | 2015-05-15 04:13:15 +0000 | [diff] [blame] | 7705 | sqlite3VdbeError(p, |
drh | 0b9b430 | 2010-06-11 17:01:24 +0000 | [diff] [blame] | 7706 | "cannot change %s wal mode from within a transaction", |
| 7707 | (eNew==PAGER_JOURNALMODE_WAL ? "into" : "out of") |
| 7708 | ); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 7709 | goto abort_due_to_error; |
drh | 0b9b430 | 2010-06-11 17:01:24 +0000 | [diff] [blame] | 7710 | }else{ |
| 7711 | |
| 7712 | if( eOld==PAGER_JOURNALMODE_WAL ){ |
| 7713 | /* If leaving WAL mode, close the log file. If successful, the call |
| 7714 | ** to PagerCloseWal() checkpoints and deletes the write-ahead-log |
| 7715 | ** file. An EXCLUSIVE lock may still be held on the database file |
| 7716 | ** after a successful return. |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 7717 | */ |
dan | 7fb8990 | 2016-08-12 16:21:15 +0000 | [diff] [blame] | 7718 | rc = sqlite3PagerCloseWal(pPager, db); |
drh | ab9b744 | 2010-05-10 11:20:05 +0000 | [diff] [blame] | 7719 | if( rc==SQLITE_OK ){ |
drh | 0b9b430 | 2010-06-11 17:01:24 +0000 | [diff] [blame] | 7720 | sqlite3PagerSetJournalMode(pPager, eNew); |
drh | 89c3f2f | 2010-05-15 01:09:38 +0000 | [diff] [blame] | 7721 | } |
drh | 242c4f7 | 2010-06-22 14:49:39 +0000 | [diff] [blame] | 7722 | }else if( eOld==PAGER_JOURNALMODE_MEMORY ){ |
| 7723 | /* Cannot transition directly from MEMORY to WAL. Use mode OFF |
| 7724 | ** as an intermediate */ |
| 7725 | sqlite3PagerSetJournalMode(pPager, PAGER_JOURNALMODE_OFF); |
drh | 0b9b430 | 2010-06-11 17:01:24 +0000 | [diff] [blame] | 7726 | } |
| 7727 | |
| 7728 | /* Open a transaction on the database file. Regardless of the journal |
| 7729 | ** mode, this transaction always uses a rollback journal. |
| 7730 | */ |
drh | 99744fa | 2020-08-25 19:09:07 +0000 | [diff] [blame] | 7731 | assert( sqlite3BtreeTxnState(pBt)!=SQLITE_TXN_WRITE ); |
drh | 0b9b430 | 2010-06-11 17:01:24 +0000 | [diff] [blame] | 7732 | if( rc==SQLITE_OK ){ |
dan | 731bf5b | 2010-06-17 16:44:21 +0000 | [diff] [blame] | 7733 | rc = sqlite3BtreeSetVersion(pBt, (eNew==PAGER_JOURNALMODE_WAL ? 2 : 1)); |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 7734 | } |
| 7735 | } |
| 7736 | } |
dan | 5cf5353 | 2010-05-01 16:40:20 +0000 | [diff] [blame] | 7737 | #endif /* ifndef SQLITE_OMIT_WAL */ |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 7738 | |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 7739 | if( rc ) eNew = eOld; |
drh | 0b9b430 | 2010-06-11 17:01:24 +0000 | [diff] [blame] | 7740 | eNew = sqlite3PagerSetJournalMode(pPager, eNew); |
dan | 731bf5b | 2010-06-17 16:44:21 +0000 | [diff] [blame] | 7741 | |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 7742 | pOut->flags = MEM_Str|MEM_Static|MEM_Term; |
dan | b978002 | 2010-04-21 18:37:57 +0000 | [diff] [blame] | 7743 | pOut->z = (char *)sqlite3JournalModename(eNew); |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 7744 | pOut->n = sqlite3Strlen30(pOut->z); |
| 7745 | pOut->enc = SQLITE_UTF8; |
| 7746 | sqlite3VdbeChangeEncoding(pOut, encoding); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 7747 | if( rc ) goto abort_due_to_error; |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 7748 | break; |
drh | cac29a6 | 2010-07-02 19:36:52 +0000 | [diff] [blame] | 7749 | }; |
| 7750 | #endif /* SQLITE_OMIT_PRAGMA */ |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 7751 | |
drh | fdbcdee | 2007-03-27 14:44:50 +0000 | [diff] [blame] | 7752 | #if !defined(SQLITE_OMIT_VACUUM) && !defined(SQLITE_OMIT_ATTACH) |
drh | 2f6239e | 2018-12-08 00:43:08 +0000 | [diff] [blame] | 7753 | /* Opcode: Vacuum P1 P2 * * * |
drh | 6f8c91c | 2003-12-07 00:24:35 +0000 | [diff] [blame] | 7754 | ** |
drh | 9ef5e77 | 2016-08-19 14:20:56 +0000 | [diff] [blame] | 7755 | ** Vacuum the entire database P1. P1 is 0 for "main", and 2 or more |
| 7756 | ** for an attached database. The "temp" database may not be vacuumed. |
drh | b0b7db9 | 2018-12-07 17:28:28 +0000 | [diff] [blame] | 7757 | ** |
drh | 2f6239e | 2018-12-08 00:43:08 +0000 | [diff] [blame] | 7758 | ** If P2 is not zero, then it is a register holding a string which is |
| 7759 | ** the file into which the result of vacuum should be written. When |
| 7760 | ** P2 is zero, the vacuum overwrites the original database. |
drh | 6f8c91c | 2003-12-07 00:24:35 +0000 | [diff] [blame] | 7761 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 7762 | case OP_Vacuum: { |
drh | 9e92a47 | 2013-06-27 17:40:30 +0000 | [diff] [blame] | 7763 | assert( p->readOnly==0 ); |
drh | 2f6239e | 2018-12-08 00:43:08 +0000 | [diff] [blame] | 7764 | rc = sqlite3RunVacuum(&p->zErrMsg, db, pOp->p1, |
| 7765 | pOp->p2 ? &aMem[pOp->p2] : 0); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 7766 | if( rc ) goto abort_due_to_error; |
drh | 6f8c91c | 2003-12-07 00:24:35 +0000 | [diff] [blame] | 7767 | break; |
| 7768 | } |
drh | 154d4b2 | 2006-09-21 11:02:16 +0000 | [diff] [blame] | 7769 | #endif |
drh | 6f8c91c | 2003-12-07 00:24:35 +0000 | [diff] [blame] | 7770 | |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 7771 | #if !defined(SQLITE_OMIT_AUTOVACUUM) |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 7772 | /* Opcode: IncrVacuum P1 P2 * * * |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 7773 | ** |
| 7774 | ** Perform a single step of the incremental vacuum procedure on |
drh | ca5557f | 2007-05-04 18:30:40 +0000 | [diff] [blame] | 7775 | ** the P1 database. If the vacuum has finished, jump to instruction |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 7776 | ** P2. Otherwise, fall through to the next instruction. |
| 7777 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 7778 | case OP_IncrVacuum: { /* jump */ |
drh | ca5557f | 2007-05-04 18:30:40 +0000 | [diff] [blame] | 7779 | Btree *pBt; |
| 7780 | |
| 7781 | assert( pOp->p1>=0 && pOp->p1<db->nDb ); |
drh | a7ab6d8 | 2014-07-21 15:44:39 +0000 | [diff] [blame] | 7782 | assert( DbMaskTest(p->btreeMask, pOp->p1) ); |
drh | 9e92a47 | 2013-06-27 17:40:30 +0000 | [diff] [blame] | 7783 | assert( p->readOnly==0 ); |
drh | ca5557f | 2007-05-04 18:30:40 +0000 | [diff] [blame] | 7784 | pBt = db->aDb[pOp->p1].pBt; |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 7785 | rc = sqlite3BtreeIncrVacuum(pBt); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 7786 | VdbeBranchTaken(rc==SQLITE_DONE,2); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 7787 | if( rc ){ |
| 7788 | if( rc!=SQLITE_DONE ) goto abort_due_to_error; |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 7789 | rc = SQLITE_OK; |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 7790 | goto jump_to_p2; |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 7791 | } |
| 7792 | break; |
| 7793 | } |
| 7794 | #endif |
| 7795 | |
drh | ba968db | 2018-07-24 22:02:12 +0000 | [diff] [blame] | 7796 | /* Opcode: Expire P1 P2 * * * |
danielk1977 | a21c6b6 | 2005-01-24 10:25:59 +0000 | [diff] [blame] | 7797 | ** |
drh | 25df48d | 2014-07-22 14:58:12 +0000 | [diff] [blame] | 7798 | ** Cause precompiled statements to expire. When an expired statement |
| 7799 | ** is executed using sqlite3_step() it will either automatically |
| 7800 | ** reprepare itself (if it was originally created using sqlite3_prepare_v2()) |
| 7801 | ** or it will fail with SQLITE_SCHEMA. |
danielk1977 | a21c6b6 | 2005-01-24 10:25:59 +0000 | [diff] [blame] | 7802 | ** |
| 7803 | ** 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] | 7804 | ** then only the currently executing statement is expired. |
drh | ba968db | 2018-07-24 22:02:12 +0000 | [diff] [blame] | 7805 | ** |
| 7806 | ** If P2 is 0, then SQL statements are expired immediately. If P2 is 1, |
| 7807 | ** then running SQL statements are allowed to continue to run to completion. |
| 7808 | ** The P2==1 case occurs when a CREATE INDEX or similar schema change happens |
| 7809 | ** that might help the statement run faster but which does not affect the |
| 7810 | ** correctness of operation. |
danielk1977 | a21c6b6 | 2005-01-24 10:25:59 +0000 | [diff] [blame] | 7811 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 7812 | case OP_Expire: { |
drh | ba968db | 2018-07-24 22:02:12 +0000 | [diff] [blame] | 7813 | assert( pOp->p2==0 || pOp->p2==1 ); |
danielk1977 | a21c6b6 | 2005-01-24 10:25:59 +0000 | [diff] [blame] | 7814 | if( !pOp->p1 ){ |
drh | ba968db | 2018-07-24 22:02:12 +0000 | [diff] [blame] | 7815 | sqlite3ExpirePreparedStatements(db, pOp->p2); |
danielk1977 | a21c6b6 | 2005-01-24 10:25:59 +0000 | [diff] [blame] | 7816 | }else{ |
drh | ba968db | 2018-07-24 22:02:12 +0000 | [diff] [blame] | 7817 | p->expired = pOp->p2+1; |
danielk1977 | a21c6b6 | 2005-01-24 10:25:59 +0000 | [diff] [blame] | 7818 | } |
| 7819 | break; |
| 7820 | } |
| 7821 | |
drh | 7b14b65 | 2019-12-29 22:08:20 +0000 | [diff] [blame] | 7822 | /* Opcode: CursorLock P1 * * * * |
| 7823 | ** |
| 7824 | ** Lock the btree to which cursor P1 is pointing so that the btree cannot be |
| 7825 | ** written by an other cursor. |
| 7826 | */ |
| 7827 | case OP_CursorLock: { |
| 7828 | VdbeCursor *pC; |
| 7829 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 7830 | pC = p->apCsr[pOp->p1]; |
| 7831 | assert( pC!=0 ); |
| 7832 | assert( pC->eCurType==CURTYPE_BTREE ); |
| 7833 | sqlite3BtreeCursorPin(pC->uc.pCursor); |
| 7834 | break; |
| 7835 | } |
| 7836 | |
| 7837 | /* Opcode: CursorUnlock P1 * * * * |
| 7838 | ** |
| 7839 | ** Unlock the btree to which cursor P1 is pointing so that it can be |
| 7840 | ** written by other cursors. |
| 7841 | */ |
| 7842 | case OP_CursorUnlock: { |
| 7843 | VdbeCursor *pC; |
| 7844 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 7845 | pC = p->apCsr[pOp->p1]; |
| 7846 | assert( pC!=0 ); |
| 7847 | assert( pC->eCurType==CURTYPE_BTREE ); |
| 7848 | sqlite3BtreeCursorUnpin(pC->uc.pCursor); |
| 7849 | break; |
| 7850 | } |
| 7851 | |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 7852 | #ifndef SQLITE_OMIT_SHARED_CACHE |
drh | 6a9ad3d | 2008-04-02 16:29:30 +0000 | [diff] [blame] | 7853 | /* Opcode: TableLock P1 P2 P3 P4 * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 7854 | ** Synopsis: iDb=P1 root=P2 write=P3 |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 7855 | ** |
| 7856 | ** Obtain a lock on a particular table. This instruction is only used when |
| 7857 | ** the shared-cache feature is enabled. |
| 7858 | ** |
danielk1977 | 96d48e9 | 2009-06-29 06:00:37 +0000 | [diff] [blame] | 7859 | ** P1 is the index of the database in sqlite3.aDb[] of the database |
drh | 6a9ad3d | 2008-04-02 16:29:30 +0000 | [diff] [blame] | 7860 | ** on which the lock is acquired. A readlock is obtained if P3==0 or |
| 7861 | ** a write lock if P3==1. |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 7862 | ** |
| 7863 | ** P2 contains the root-page of the table to lock. |
| 7864 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 7865 | ** 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] | 7866 | ** used to generate an error message if the lock cannot be obtained. |
| 7867 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 7868 | case OP_TableLock: { |
danielk1977 | e0d9e6f | 2009-07-03 16:25:06 +0000 | [diff] [blame] | 7869 | u8 isWriteLock = (u8)pOp->p3; |
drh | 169dd92 | 2017-06-26 13:57:49 +0000 | [diff] [blame] | 7870 | if( isWriteLock || 0==(db->flags&SQLITE_ReadUncommit) ){ |
danielk1977 | e0d9e6f | 2009-07-03 16:25:06 +0000 | [diff] [blame] | 7871 | int p1 = pOp->p1; |
| 7872 | assert( p1>=0 && p1<db->nDb ); |
drh | a7ab6d8 | 2014-07-21 15:44:39 +0000 | [diff] [blame] | 7873 | assert( DbMaskTest(p->btreeMask, p1) ); |
danielk1977 | e0d9e6f | 2009-07-03 16:25:06 +0000 | [diff] [blame] | 7874 | assert( isWriteLock==0 || isWriteLock==1 ); |
| 7875 | rc = sqlite3BtreeLockTable(db->aDb[p1].pBt, pOp->p2, isWriteLock); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 7876 | if( rc ){ |
| 7877 | if( (rc&0xFF)==SQLITE_LOCKED ){ |
| 7878 | const char *z = pOp->p4.z; |
| 7879 | sqlite3VdbeError(p, "database table is locked: %s", z); |
| 7880 | } |
| 7881 | goto abort_due_to_error; |
danielk1977 | e0d9e6f | 2009-07-03 16:25:06 +0000 | [diff] [blame] | 7882 | } |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 7883 | } |
| 7884 | break; |
| 7885 | } |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 7886 | #endif /* SQLITE_OMIT_SHARED_CACHE */ |
| 7887 | |
| 7888 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 7889 | /* Opcode: VBegin * * * P4 * |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 7890 | ** |
danielk1977 | 3e3a84d | 2008-08-01 17:37:40 +0000 | [diff] [blame] | 7891 | ** P4 may be a pointer to an sqlite3_vtab structure. If so, call the |
| 7892 | ** xBegin method for that table. |
| 7893 | ** |
| 7894 | ** 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] | 7895 | ** within a callback to a virtual table xSync() method. If it is, the error |
| 7896 | ** code will be set to SQLITE_LOCKED. |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 7897 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 7898 | case OP_VBegin: { |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 7899 | VTable *pVTab; |
| 7900 | pVTab = pOp->p4.pVtab; |
| 7901 | rc = sqlite3VtabBegin(db, pVTab); |
dan | 016f781 | 2013-08-21 17:35:48 +0000 | [diff] [blame] | 7902 | if( pVTab ) sqlite3VtabImportErrmsg(p, pVTab->pVtab); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 7903 | if( rc ) goto abort_due_to_error; |
danielk1977 | f9e7dda | 2006-06-16 16:08:53 +0000 | [diff] [blame] | 7904 | break; |
| 7905 | } |
| 7906 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 7907 | |
| 7908 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
dan | 7377945 | 2015-03-19 18:56:17 +0000 | [diff] [blame] | 7909 | /* Opcode: VCreate P1 P2 * * * |
danielk1977 | f9e7dda | 2006-06-16 16:08:53 +0000 | [diff] [blame] | 7910 | ** |
dan | 7377945 | 2015-03-19 18:56:17 +0000 | [diff] [blame] | 7911 | ** P2 is a register that holds the name of a virtual table in database |
| 7912 | ** P1. Call the xCreate method for that table. |
danielk1977 | f9e7dda | 2006-06-16 16:08:53 +0000 | [diff] [blame] | 7913 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 7914 | case OP_VCreate: { |
dan | 7377945 | 2015-03-19 18:56:17 +0000 | [diff] [blame] | 7915 | Mem sMem; /* For storing the record being decoded */ |
drh | 4746406 | 2015-03-21 12:22:16 +0000 | [diff] [blame] | 7916 | const char *zTab; /* Name of the virtual table */ |
| 7917 | |
dan | 7377945 | 2015-03-19 18:56:17 +0000 | [diff] [blame] | 7918 | memset(&sMem, 0, sizeof(sMem)); |
| 7919 | sMem.db = db; |
drh | 4746406 | 2015-03-21 12:22:16 +0000 | [diff] [blame] | 7920 | /* Because P2 is always a static string, it is impossible for the |
| 7921 | ** sqlite3VdbeMemCopy() to fail */ |
| 7922 | assert( (aMem[pOp->p2].flags & MEM_Str)!=0 ); |
| 7923 | assert( (aMem[pOp->p2].flags & MEM_Static)!=0 ); |
dan | 7377945 | 2015-03-19 18:56:17 +0000 | [diff] [blame] | 7924 | rc = sqlite3VdbeMemCopy(&sMem, &aMem[pOp->p2]); |
drh | 4746406 | 2015-03-21 12:22:16 +0000 | [diff] [blame] | 7925 | assert( rc==SQLITE_OK ); |
| 7926 | zTab = (const char*)sqlite3_value_text(&sMem); |
| 7927 | assert( zTab || db->mallocFailed ); |
| 7928 | if( zTab ){ |
| 7929 | rc = sqlite3VtabCallCreate(db, pOp->p1, zTab, &p->zErrMsg); |
dan | 7377945 | 2015-03-19 18:56:17 +0000 | [diff] [blame] | 7930 | } |
| 7931 | sqlite3VdbeMemRelease(&sMem); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 7932 | if( rc ) goto abort_due_to_error; |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 7933 | break; |
| 7934 | } |
| 7935 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 7936 | |
| 7937 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 7938 | /* Opcode: VDestroy P1 * * P4 * |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 7939 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 7940 | ** 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] | 7941 | ** of that table. |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 7942 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 7943 | case OP_VDestroy: { |
drh | 086723a | 2015-03-24 12:51:52 +0000 | [diff] [blame] | 7944 | db->nVDestroy++; |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 7945 | rc = sqlite3VtabCallDestroy(db, pOp->p1, pOp->p4.z); |
drh | 086723a | 2015-03-24 12:51:52 +0000 | [diff] [blame] | 7946 | db->nVDestroy--; |
dan | 1d4b164 | 2018-12-28 17:45:08 +0000 | [diff] [blame] | 7947 | assert( p->errorAction==OE_Abort && p->usesStmtJournal ); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 7948 | if( rc ) goto abort_due_to_error; |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 7949 | break; |
| 7950 | } |
| 7951 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 7952 | |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 7953 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 7954 | /* Opcode: VOpen P1 * * P4 * |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 7955 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 7956 | ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure. |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 7957 | ** P1 is a cursor number. This opcode opens a cursor to the virtual |
| 7958 | ** table and stores that cursor in P1. |
| 7959 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 7960 | case OP_VOpen: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 7961 | VdbeCursor *pCur; |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 7962 | sqlite3_vtab_cursor *pVCur; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 7963 | sqlite3_vtab *pVtab; |
drh | f496a7d | 2015-03-24 14:05:50 +0000 | [diff] [blame] | 7964 | const sqlite3_module *pModule; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 7965 | |
drh | 1713afb | 2013-06-28 01:24:57 +0000 | [diff] [blame] | 7966 | assert( p->bIsReader ); |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 7967 | pCur = 0; |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 7968 | pVCur = 0; |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 7969 | pVtab = pOp->p4.pVtab->pVtab; |
drh | f496a7d | 2015-03-24 14:05:50 +0000 | [diff] [blame] | 7970 | if( pVtab==0 || NEVER(pVtab->pModule==0) ){ |
| 7971 | rc = SQLITE_LOCKED; |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 7972 | goto abort_due_to_error; |
drh | f496a7d | 2015-03-24 14:05:50 +0000 | [diff] [blame] | 7973 | } |
| 7974 | pModule = pVtab->pModule; |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 7975 | rc = pModule->xOpen(pVtab, &pVCur); |
dan | 016f781 | 2013-08-21 17:35:48 +0000 | [diff] [blame] | 7976 | sqlite3VtabImportErrmsg(p, pVtab); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 7977 | if( rc ) goto abort_due_to_error; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 7978 | |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 7979 | /* Initialize sqlite3_vtab_cursor base class */ |
| 7980 | pVCur->pVtab = pVtab; |
| 7981 | |
| 7982 | /* Initialize vdbe cursor object */ |
drh | b248668 | 2022-01-03 01:43:28 +0000 | [diff] [blame] | 7983 | pCur = allocateCursor(p, pOp->p1, 0, CURTYPE_VTAB); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 7984 | if( pCur ){ |
| 7985 | pCur->uc.pVCur = pVCur; |
| 7986 | pVtab->nRef++; |
| 7987 | }else{ |
| 7988 | assert( db->mallocFailed ); |
| 7989 | pModule->xClose(pVCur); |
| 7990 | goto no_mem; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 7991 | } |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 7992 | break; |
| 7993 | } |
| 7994 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 7995 | |
| 7996 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | 0fe7e7d | 2022-02-01 14:58:29 +0000 | [diff] [blame] | 7997 | /* Opcode: VInitIn P1 P2 P3 * * |
drh | 30e314e | 2022-02-02 14:36:58 +0000 | [diff] [blame] | 7998 | ** Synopsis: r[P2]=ValueList(P1,P3) |
drh | 0fe7e7d | 2022-02-01 14:58:29 +0000 | [diff] [blame] | 7999 | ** |
drh | 30e314e | 2022-02-02 14:36:58 +0000 | [diff] [blame] | 8000 | ** Set register P2 to be a pointer to a ValueList object for cursor P1 |
| 8001 | ** with cache register P3 and output register P3+1. This ValueList object |
| 8002 | ** can be used as the first argument to sqlite3_vtab_in_first() and |
| 8003 | ** sqlite3_vtab_in_next() to extract all of the values stored in the P1 |
| 8004 | ** cursor. Register P3 is used to hold the values returned by |
| 8005 | ** sqlite3_vtab_in_first() and sqlite3_vtab_in_next(). |
drh | 0fe7e7d | 2022-02-01 14:58:29 +0000 | [diff] [blame] | 8006 | */ |
| 8007 | case OP_VInitIn: { /* out2 */ |
drh | 30e314e | 2022-02-02 14:36:58 +0000 | [diff] [blame] | 8008 | VdbeCursor *pC; /* The cursor containing the RHS values */ |
| 8009 | ValueList *pRhs; /* New ValueList object to put in reg[P2] */ |
| 8010 | |
drh | 0fe7e7d | 2022-02-01 14:58:29 +0000 | [diff] [blame] | 8011 | pC = p->apCsr[pOp->p1]; |
drh | 30e314e | 2022-02-02 14:36:58 +0000 | [diff] [blame] | 8012 | pRhs = sqlite3_malloc64( sizeof(*pRhs) ); |
| 8013 | if( pRhs==0 ) goto no_mem; |
| 8014 | pRhs->pCsr = pC->uc.pCursor; |
| 8015 | pRhs->pOut = &aMem[pOp->p3]; |
drh | 0fe7e7d | 2022-02-01 14:58:29 +0000 | [diff] [blame] | 8016 | pOut = out2Prerelease(p, pOp); |
drh | 0fe7e7d | 2022-02-01 14:58:29 +0000 | [diff] [blame] | 8017 | pOut->flags = MEM_Null; |
drh | 30e314e | 2022-02-02 14:36:58 +0000 | [diff] [blame] | 8018 | sqlite3VdbeMemSetPointer(pOut, pRhs, "ValueList", sqlite3_free); |
drh | 0fe7e7d | 2022-02-01 14:58:29 +0000 | [diff] [blame] | 8019 | break; |
| 8020 | } |
| 8021 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 8022 | |
| 8023 | |
| 8024 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
danielk1977 | 6dbee81 | 2008-01-03 18:39:41 +0000 | [diff] [blame] | 8025 | /* Opcode: VFilter P1 P2 P3 P4 * |
drh | 831116d | 2014-04-03 14:31:00 +0000 | [diff] [blame] | 8026 | ** Synopsis: iplan=r[P3] zplan='P4' |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 8027 | ** |
| 8028 | ** P1 is a cursor opened using VOpen. P2 is an address to jump to if |
| 8029 | ** the filtered result set is empty. |
| 8030 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 8031 | ** P4 is either NULL or a string that was generated by the xBestIndex |
| 8032 | ** method of the module. The interpretation of the P4 string is left |
drh | 4be8b51 | 2006-06-13 23:51:34 +0000 | [diff] [blame] | 8033 | ** to the module implementation. |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 8034 | ** |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 8035 | ** This opcode invokes the xFilter method on the virtual table specified |
danielk1977 | 6dbee81 | 2008-01-03 18:39:41 +0000 | [diff] [blame] | 8036 | ** by P1. The integer query plan parameter to xFilter is stored in register |
| 8037 | ** P3. Register P3+1 stores the argc parameter to be passed to the |
drh | 174edc6 | 2008-05-29 05:23:41 +0000 | [diff] [blame] | 8038 | ** xFilter method. Registers P3+2..P3+1+argc are the argc |
| 8039 | ** additional parameters which are passed to |
danielk1977 | 6dbee81 | 2008-01-03 18:39:41 +0000 | [diff] [blame] | 8040 | ** xFilter as argv. Register P3+2 becomes argv[0] when passed to xFilter. |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 8041 | ** |
danielk1977 | 6dbee81 | 2008-01-03 18:39:41 +0000 | [diff] [blame] | 8042 | ** 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] | 8043 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 8044 | case OP_VFilter: { /* jump */ |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 8045 | int nArg; |
danielk1977 | 6dbee81 | 2008-01-03 18:39:41 +0000 | [diff] [blame] | 8046 | int iQuery; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 8047 | const sqlite3_module *pModule; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 8048 | Mem *pQuery; |
| 8049 | Mem *pArgc; |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 8050 | sqlite3_vtab_cursor *pVCur; |
drh | 4dc754d | 2008-07-23 18:17:32 +0000 | [diff] [blame] | 8051 | sqlite3_vtab *pVtab; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 8052 | VdbeCursor *pCur; |
| 8053 | int res; |
| 8054 | int i; |
| 8055 | Mem **apArg; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 8056 | |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 8057 | pQuery = &aMem[pOp->p3]; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 8058 | pArgc = &pQuery[1]; |
| 8059 | pCur = p->apCsr[pOp->p1]; |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 8060 | assert( memIsValid(pQuery) ); |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 8061 | REGISTER_TRACE(pOp->p3, pQuery); |
drh | 3ab4ffc | 2021-11-11 11:23:08 +0000 | [diff] [blame] | 8062 | assert( pCur!=0 ); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 8063 | assert( pCur->eCurType==CURTYPE_VTAB ); |
| 8064 | pVCur = pCur->uc.pVCur; |
| 8065 | pVtab = pVCur->pVtab; |
drh | 4dc754d | 2008-07-23 18:17:32 +0000 | [diff] [blame] | 8066 | pModule = pVtab->pModule; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 8067 | |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 8068 | /* Grab the index number and argc parameters */ |
danielk1977 | 6dbee81 | 2008-01-03 18:39:41 +0000 | [diff] [blame] | 8069 | assert( (pQuery->flags&MEM_Int)!=0 && pArgc->flags==MEM_Int ); |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 8070 | nArg = (int)pArgc->u.i; |
| 8071 | iQuery = (int)pQuery->u.i; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 8072 | |
drh | 644a529 | 2006-12-20 14:53:38 +0000 | [diff] [blame] | 8073 | /* Invoke the xFilter method */ |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 8074 | apArg = p->apArg; |
| 8075 | for(i = 0; i<nArg; i++){ |
| 8076 | apArg[i] = &pArgc[i+1]; |
| 8077 | } |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 8078 | rc = pModule->xFilter(pVCur, iQuery, pOp->p4.z, nArg, apArg); |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 8079 | sqlite3VtabImportErrmsg(p, pVtab); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 8080 | if( rc ) goto abort_due_to_error; |
| 8081 | res = pModule->xEof(pVCur); |
drh | 1d454a3 | 2008-01-31 19:34:51 +0000 | [diff] [blame] | 8082 | pCur->nullRow = 0; |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 8083 | VdbeBranchTaken(res!=0,2); |
| 8084 | if( res ) goto jump_to_p2; |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 8085 | break; |
| 8086 | } |
| 8087 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 8088 | |
| 8089 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | ce2fbd1 | 2018-01-12 21:00:14 +0000 | [diff] [blame] | 8090 | /* Opcode: VColumn P1 P2 P3 * P5 |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 8091 | ** Synopsis: r[P3]=vcolumn(P2) |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 8092 | ** |
drh | 6f390be | 2018-01-11 17:04:26 +0000 | [diff] [blame] | 8093 | ** Store in register P3 the value of the P2-th column of |
| 8094 | ** the current row of the virtual-table of cursor P1. |
| 8095 | ** |
| 8096 | ** If the VColumn opcode is being used to fetch the value of |
drh | ce2fbd1 | 2018-01-12 21:00:14 +0000 | [diff] [blame] | 8097 | ** an unchanging column during an UPDATE operation, then the P5 |
drh | 09d00b2 | 2018-09-27 20:20:01 +0000 | [diff] [blame] | 8098 | ** value is OPFLAG_NOCHNG. This will cause the sqlite3_vtab_nochange() |
| 8099 | ** function to return true inside the xColumn method of the virtual |
| 8100 | ** table implementation. The P5 column might also contain other |
| 8101 | ** bits (OPFLAG_LENGTHARG or OPFLAG_TYPEOFARG) but those bits are |
| 8102 | ** unused by OP_VColumn. |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 8103 | */ |
| 8104 | case OP_VColumn: { |
danielk1977 | 3e3a84d | 2008-08-01 17:37:40 +0000 | [diff] [blame] | 8105 | sqlite3_vtab *pVtab; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 8106 | const sqlite3_module *pModule; |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 8107 | Mem *pDest; |
| 8108 | sqlite3_context sContext; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 8109 | |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 8110 | VdbeCursor *pCur = p->apCsr[pOp->p1]; |
drh | 3ab4ffc | 2021-11-11 11:23:08 +0000 | [diff] [blame] | 8111 | assert( pCur!=0 ); |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 8112 | assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) ); |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 8113 | pDest = &aMem[pOp->p3]; |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 8114 | memAboutToChange(p, pDest); |
drh | 2945b4a | 2008-01-31 15:53:45 +0000 | [diff] [blame] | 8115 | if( pCur->nullRow ){ |
| 8116 | sqlite3VdbeMemSetNull(pDest); |
| 8117 | break; |
| 8118 | } |
drh | a27e350 | 2022-06-10 10:10:31 +0000 | [diff] [blame] | 8119 | assert( pCur->eCurType==CURTYPE_VTAB ); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 8120 | pVtab = pCur->uc.pVCur->pVtab; |
danielk1977 | 3e3a84d | 2008-08-01 17:37:40 +0000 | [diff] [blame] | 8121 | pModule = pVtab->pModule; |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 8122 | assert( pModule->xColumn ); |
| 8123 | memset(&sContext, 0, sizeof(sContext)); |
drh | 9bd038f | 2014-08-27 14:14:06 +0000 | [diff] [blame] | 8124 | sContext.pOut = pDest; |
drh | 659fdb4 | 2022-04-01 15:31:58 +0000 | [diff] [blame] | 8125 | sContext.enc = encoding; |
drh | 75f1076 | 2019-12-14 18:08:22 +0000 | [diff] [blame] | 8126 | assert( pOp->p5==OPFLAG_NOCHNG || pOp->p5==0 ); |
drh | 09d00b2 | 2018-09-27 20:20:01 +0000 | [diff] [blame] | 8127 | if( pOp->p5 & OPFLAG_NOCHNG ){ |
drh | ce2fbd1 | 2018-01-12 21:00:14 +0000 | [diff] [blame] | 8128 | sqlite3VdbeMemSetNull(pDest); |
| 8129 | pDest->flags = MEM_Null|MEM_Zero; |
| 8130 | pDest->u.nZero = 0; |
| 8131 | }else{ |
| 8132 | MemSetTypeFlag(pDest, MEM_Null); |
| 8133 | } |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 8134 | rc = pModule->xColumn(pCur->uc.pVCur, &sContext, pOp->p2); |
dan | 016f781 | 2013-08-21 17:35:48 +0000 | [diff] [blame] | 8135 | sqlite3VtabImportErrmsg(p, pVtab); |
drh | f09ac0b | 2018-01-23 03:44:06 +0000 | [diff] [blame] | 8136 | if( sContext.isError>0 ){ |
dan | 099fa84 | 2018-01-30 18:33:23 +0000 | [diff] [blame] | 8137 | sqlite3VdbeError(p, "%s", sqlite3_value_text(pDest)); |
drh | 4c8555f | 2009-06-25 01:47:11 +0000 | [diff] [blame] | 8138 | rc = sContext.isError; |
| 8139 | } |
drh | 9bd038f | 2014-08-27 14:14:06 +0000 | [diff] [blame] | 8140 | sqlite3VdbeChangeEncoding(pDest, encoding); |
drh | 5ff4437 | 2009-11-24 16:26:17 +0000 | [diff] [blame] | 8141 | REGISTER_TRACE(pOp->p3, pDest); |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 8142 | UPDATE_MAX_BLOBSIZE(pDest); |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 8143 | |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 8144 | if( rc ) goto abort_due_to_error; |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 8145 | break; |
| 8146 | } |
| 8147 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 8148 | |
| 8149 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 8150 | /* Opcode: VNext P1 P2 * * * |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 8151 | ** |
| 8152 | ** Advance virtual table P1 to the next row in its result set and |
| 8153 | ** jump to instruction P2. Or, if the virtual table has reached |
| 8154 | ** the end of its result set, then fall through to the next instruction. |
| 8155 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 8156 | case OP_VNext: { /* jump */ |
danielk1977 | 3e3a84d | 2008-08-01 17:37:40 +0000 | [diff] [blame] | 8157 | sqlite3_vtab *pVtab; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 8158 | const sqlite3_module *pModule; |
drh | c54a617 | 2009-06-02 16:06:03 +0000 | [diff] [blame] | 8159 | int res; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 8160 | VdbeCursor *pCur; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 8161 | |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 8162 | pCur = p->apCsr[pOp->p1]; |
drh | 3ab4ffc | 2021-11-11 11:23:08 +0000 | [diff] [blame] | 8163 | assert( pCur!=0 ); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 8164 | assert( pCur->eCurType==CURTYPE_VTAB ); |
drh | 2945b4a | 2008-01-31 15:53:45 +0000 | [diff] [blame] | 8165 | if( pCur->nullRow ){ |
| 8166 | break; |
| 8167 | } |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 8168 | pVtab = pCur->uc.pVCur->pVtab; |
danielk1977 | 3e3a84d | 2008-08-01 17:37:40 +0000 | [diff] [blame] | 8169 | pModule = pVtab->pModule; |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 8170 | assert( pModule->xNext ); |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 8171 | |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 8172 | /* Invoke the xNext() method of the module. There is no way for the |
| 8173 | ** underlying implementation to return an error if one occurs during |
| 8174 | ** xNext(). Instead, if an error occurs, true is returned (indicating that |
| 8175 | ** data is available) and the error code returned when xColumn or |
| 8176 | ** some other method is next invoked on the save virtual table cursor. |
| 8177 | */ |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 8178 | rc = pModule->xNext(pCur->uc.pVCur); |
dan | 016f781 | 2013-08-21 17:35:48 +0000 | [diff] [blame] | 8179 | sqlite3VtabImportErrmsg(p, pVtab); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 8180 | if( rc ) goto abort_due_to_error; |
| 8181 | res = pModule->xEof(pCur->uc.pVCur); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 8182 | VdbeBranchTaken(!res,2); |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 8183 | if( !res ){ |
| 8184 | /* If there is data, jump to P2 */ |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 8185 | goto jump_to_p2_and_check_for_interrupt; |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 8186 | } |
drh | 49afe3a | 2013-07-10 03:05:14 +0000 | [diff] [blame] | 8187 | goto check_for_interrupt; |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 8188 | } |
| 8189 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 8190 | |
danielk1977 | 182c4ba | 2007-06-27 15:53:34 +0000 | [diff] [blame] | 8191 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 8192 | /* Opcode: VRename P1 * * P4 * |
danielk1977 | 182c4ba | 2007-06-27 15:53:34 +0000 | [diff] [blame] | 8193 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 8194 | ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure. |
danielk1977 | 182c4ba | 2007-06-27 15:53:34 +0000 | [diff] [blame] | 8195 | ** This opcode invokes the corresponding xRename method. The value |
danielk1977 | 6dbee81 | 2008-01-03 18:39:41 +0000 | [diff] [blame] | 8196 | ** in register P1 is passed as the zName argument to the xRename method. |
danielk1977 | 182c4ba | 2007-06-27 15:53:34 +0000 | [diff] [blame] | 8197 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 8198 | case OP_VRename: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 8199 | sqlite3_vtab *pVtab; |
| 8200 | Mem *pName; |
dan | 34566c4 | 2018-09-20 17:21:21 +0000 | [diff] [blame] | 8201 | int isLegacy; |
| 8202 | |
| 8203 | isLegacy = (db->flags & SQLITE_LegacyAlter); |
| 8204 | db->flags |= SQLITE_LegacyAlter; |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 8205 | pVtab = pOp->p4.pVtab->pVtab; |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 8206 | pName = &aMem[pOp->p1]; |
danielk1977 | 182c4ba | 2007-06-27 15:53:34 +0000 | [diff] [blame] | 8207 | assert( pVtab->pModule->xRename ); |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 8208 | assert( memIsValid(pName) ); |
drh | 9e92a47 | 2013-06-27 17:40:30 +0000 | [diff] [blame] | 8209 | assert( p->readOnly==0 ); |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 8210 | REGISTER_TRACE(pOp->p1, pName); |
drh | 35f6b93 | 2009-06-23 14:15:04 +0000 | [diff] [blame] | 8211 | assert( pName->flags & MEM_Str ); |
drh | 98655a6 | 2011-10-18 22:07:47 +0000 | [diff] [blame] | 8212 | testcase( pName->enc==SQLITE_UTF8 ); |
| 8213 | testcase( pName->enc==SQLITE_UTF16BE ); |
| 8214 | testcase( pName->enc==SQLITE_UTF16LE ); |
| 8215 | rc = sqlite3VdbeChangeEncoding(pName, SQLITE_UTF8); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 8216 | if( rc ) goto abort_due_to_error; |
| 8217 | rc = pVtab->pModule->xRename(pVtab, pName->z); |
drh | d5b44d6 | 2018-12-06 17:06:02 +0000 | [diff] [blame] | 8218 | if( isLegacy==0 ) db->flags &= ~(u64)SQLITE_LegacyAlter; |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 8219 | sqlite3VtabImportErrmsg(p, pVtab); |
| 8220 | p->expired = 0; |
| 8221 | if( rc ) goto abort_due_to_error; |
danielk1977 | 182c4ba | 2007-06-27 15:53:34 +0000 | [diff] [blame] | 8222 | break; |
| 8223 | } |
| 8224 | #endif |
drh | 4cbdda9 | 2006-06-14 19:00:20 +0000 | [diff] [blame] | 8225 | |
| 8226 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | 0fd6135 | 2014-02-07 02:29:45 +0000 | [diff] [blame] | 8227 | /* Opcode: VUpdate P1 P2 P3 P4 P5 |
drh | f63552b | 2013-10-30 00:25:03 +0000 | [diff] [blame] | 8228 | ** Synopsis: data=r[P3@P2] |
danielk1977 | 399918f | 2006-06-14 13:03:23 +0000 | [diff] [blame] | 8229 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 8230 | ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure. |
danielk1977 | 399918f | 2006-06-14 13:03:23 +0000 | [diff] [blame] | 8231 | ** This opcode invokes the corresponding xUpdate method. P2 values |
danielk1977 | 2a339ff | 2008-01-03 17:31:44 +0000 | [diff] [blame] | 8232 | ** are contiguous memory cells starting at P3 to pass to the xUpdate |
| 8233 | ** invocation. The value in register (P3+P2-1) corresponds to the |
| 8234 | ** p2th element of the argv array passed to xUpdate. |
drh | 4cbdda9 | 2006-06-14 19:00:20 +0000 | [diff] [blame] | 8235 | ** |
| 8236 | ** The xUpdate method will do a DELETE or an INSERT or both. |
danielk1977 | 2a339ff | 2008-01-03 17:31:44 +0000 | [diff] [blame] | 8237 | ** The argv[0] element (which corresponds to memory cell P3) |
| 8238 | ** is the rowid of a row to delete. If argv[0] is NULL then no |
| 8239 | ** deletion occurs. The argv[1] element is the rowid of the new |
| 8240 | ** row. This can be NULL to have the virtual table select the new |
| 8241 | ** rowid for itself. The subsequent elements in the array are |
| 8242 | ** the values of columns in the new row. |
drh | 4cbdda9 | 2006-06-14 19:00:20 +0000 | [diff] [blame] | 8243 | ** |
| 8244 | ** If P2==1 then no insert is performed. argv[0] is the rowid of |
| 8245 | ** a row to delete. |
danielk1977 | 1f6eec5 | 2006-06-16 06:17:47 +0000 | [diff] [blame] | 8246 | ** |
| 8247 | ** P1 is a boolean flag. If it is set to true and the xUpdate call |
| 8248 | ** is successful, then the value returned by sqlite3_last_insert_rowid() |
| 8249 | ** is set to the value of the rowid for the row just inserted. |
drh | 0fd6135 | 2014-02-07 02:29:45 +0000 | [diff] [blame] | 8250 | ** |
| 8251 | ** P5 is the error actions (OE_Replace, OE_Fail, OE_Ignore, etc) to |
| 8252 | ** apply in the case of a constraint failure on an insert or update. |
danielk1977 | 399918f | 2006-06-14 13:03:23 +0000 | [diff] [blame] | 8253 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 8254 | case OP_VUpdate: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 8255 | sqlite3_vtab *pVtab; |
drh | f496a7d | 2015-03-24 14:05:50 +0000 | [diff] [blame] | 8256 | const sqlite3_module *pModule; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 8257 | int nArg; |
| 8258 | int i; |
drh | 9135ebb | 2021-11-11 23:52:44 +0000 | [diff] [blame] | 8259 | sqlite_int64 rowid = 0; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 8260 | Mem **apArg; |
| 8261 | Mem *pX; |
| 8262 | |
dan | b061d05 | 2011-04-25 18:49:57 +0000 | [diff] [blame] | 8263 | assert( pOp->p2==1 || pOp->p5==OE_Fail || pOp->p5==OE_Rollback |
| 8264 | || pOp->p5==OE_Abort || pOp->p5==OE_Ignore || pOp->p5==OE_Replace |
| 8265 | ); |
drh | 9e92a47 | 2013-06-27 17:40:30 +0000 | [diff] [blame] | 8266 | assert( p->readOnly==0 ); |
dan | 466ea9b | 2018-06-13 11:11:13 +0000 | [diff] [blame] | 8267 | if( db->mallocFailed ) goto no_mem; |
drh | 4031baf | 2018-05-28 17:31:20 +0000 | [diff] [blame] | 8268 | sqlite3VdbeIncrWriteCounter(p, 0); |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 8269 | pVtab = pOp->p4.pVtab->pVtab; |
drh | f496a7d | 2015-03-24 14:05:50 +0000 | [diff] [blame] | 8270 | if( pVtab==0 || NEVER(pVtab->pModule==0) ){ |
| 8271 | rc = SQLITE_LOCKED; |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 8272 | goto abort_due_to_error; |
drh | f496a7d | 2015-03-24 14:05:50 +0000 | [diff] [blame] | 8273 | } |
| 8274 | pModule = pVtab->pModule; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 8275 | nArg = pOp->p2; |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 8276 | assert( pOp->p4type==P4_VTAB ); |
drh | 35f6b93 | 2009-06-23 14:15:04 +0000 | [diff] [blame] | 8277 | if( ALWAYS(pModule->xUpdate) ){ |
dan | b061d05 | 2011-04-25 18:49:57 +0000 | [diff] [blame] | 8278 | u8 vtabOnConflict = db->vtabOnConflict; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 8279 | apArg = p->apArg; |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 8280 | pX = &aMem[pOp->p3]; |
danielk1977 | 2a339ff | 2008-01-03 17:31:44 +0000 | [diff] [blame] | 8281 | for(i=0; i<nArg; i++){ |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 8282 | assert( memIsValid(pX) ); |
| 8283 | memAboutToChange(p, pX); |
drh | 9c41938 | 2006-06-16 21:13:21 +0000 | [diff] [blame] | 8284 | apArg[i] = pX; |
danielk1977 | 2a339ff | 2008-01-03 17:31:44 +0000 | [diff] [blame] | 8285 | pX++; |
danielk1977 | 399918f | 2006-06-14 13:03:23 +0000 | [diff] [blame] | 8286 | } |
dan | b061d05 | 2011-04-25 18:49:57 +0000 | [diff] [blame] | 8287 | db->vtabOnConflict = pOp->p5; |
danielk1977 | 1f6eec5 | 2006-06-16 06:17:47 +0000 | [diff] [blame] | 8288 | rc = pModule->xUpdate(pVtab, nArg, apArg, &rowid); |
dan | b061d05 | 2011-04-25 18:49:57 +0000 | [diff] [blame] | 8289 | db->vtabOnConflict = vtabOnConflict; |
dan | 016f781 | 2013-08-21 17:35:48 +0000 | [diff] [blame] | 8290 | sqlite3VtabImportErrmsg(p, pVtab); |
drh | 35f6b93 | 2009-06-23 14:15:04 +0000 | [diff] [blame] | 8291 | if( rc==SQLITE_OK && pOp->p1 ){ |
danielk1977 | 1f6eec5 | 2006-06-16 06:17:47 +0000 | [diff] [blame] | 8292 | assert( nArg>1 && apArg[0] && (apArg[0]->flags&MEM_Null) ); |
drh | fae58d5 | 2017-01-26 17:26:44 +0000 | [diff] [blame] | 8293 | db->lastRowid = rowid; |
danielk1977 | 1f6eec5 | 2006-06-16 06:17:47 +0000 | [diff] [blame] | 8294 | } |
drh | d91c1a1 | 2013-02-09 13:58:25 +0000 | [diff] [blame] | 8295 | if( (rc&0xff)==SQLITE_CONSTRAINT && pOp->p4.pVtab->bConstraint ){ |
dan | b061d05 | 2011-04-25 18:49:57 +0000 | [diff] [blame] | 8296 | if( pOp->p5==OE_Ignore ){ |
| 8297 | rc = SQLITE_OK; |
| 8298 | }else{ |
| 8299 | p->errorAction = ((pOp->p5==OE_Replace) ? OE_Abort : pOp->p5); |
| 8300 | } |
| 8301 | }else{ |
| 8302 | p->nChange++; |
| 8303 | } |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 8304 | if( rc ) goto abort_due_to_error; |
danielk1977 | 399918f | 2006-06-14 13:03:23 +0000 | [diff] [blame] | 8305 | } |
drh | 4cbdda9 | 2006-06-14 19:00:20 +0000 | [diff] [blame] | 8306 | break; |
danielk1977 | 399918f | 2006-06-14 13:03:23 +0000 | [diff] [blame] | 8307 | } |
| 8308 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 8309 | |
danielk1977 | 59a9379 | 2008-05-15 17:48:20 +0000 | [diff] [blame] | 8310 | #ifndef SQLITE_OMIT_PAGER_PRAGMAS |
| 8311 | /* Opcode: Pagecount P1 P2 * * * |
| 8312 | ** |
| 8313 | ** Write the current number of pages in database P1 to memory cell P2. |
| 8314 | */ |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 8315 | case OP_Pagecount: { /* out2 */ |
| 8316 | pOut = out2Prerelease(p, pOp); |
drh | b129915 | 2010-03-30 22:58:33 +0000 | [diff] [blame] | 8317 | pOut->u.i = sqlite3BtreeLastPage(db->aDb[pOp->p1].pBt); |
danielk1977 | 59a9379 | 2008-05-15 17:48:20 +0000 | [diff] [blame] | 8318 | break; |
| 8319 | } |
| 8320 | #endif |
| 8321 | |
drh | 60ac3f4 | 2010-11-23 18:59:27 +0000 | [diff] [blame] | 8322 | |
| 8323 | #ifndef SQLITE_OMIT_PAGER_PRAGMAS |
| 8324 | /* Opcode: MaxPgcnt P1 P2 P3 * * |
| 8325 | ** |
| 8326 | ** 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] | 8327 | ** Do not let the maximum page count fall below the current page count and |
| 8328 | ** do not change the maximum page count value if P3==0. |
| 8329 | ** |
drh | 60ac3f4 | 2010-11-23 18:59:27 +0000 | [diff] [blame] | 8330 | ** Store the maximum page count after the change in register P2. |
| 8331 | */ |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 8332 | case OP_MaxPgcnt: { /* out2 */ |
drh | c84e033 | 2010-11-23 20:25:08 +0000 | [diff] [blame] | 8333 | unsigned int newMax; |
drh | 60ac3f4 | 2010-11-23 18:59:27 +0000 | [diff] [blame] | 8334 | Btree *pBt; |
| 8335 | |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 8336 | pOut = out2Prerelease(p, pOp); |
drh | 60ac3f4 | 2010-11-23 18:59:27 +0000 | [diff] [blame] | 8337 | pBt = db->aDb[pOp->p1].pBt; |
drh | c84e033 | 2010-11-23 20:25:08 +0000 | [diff] [blame] | 8338 | newMax = 0; |
| 8339 | if( pOp->p3 ){ |
| 8340 | newMax = sqlite3BtreeLastPage(pBt); |
drh | 6ea28d6 | 2010-11-26 16:49:59 +0000 | [diff] [blame] | 8341 | if( newMax < (unsigned)pOp->p3 ) newMax = (unsigned)pOp->p3; |
drh | c84e033 | 2010-11-23 20:25:08 +0000 | [diff] [blame] | 8342 | } |
| 8343 | pOut->u.i = sqlite3BtreeMaxPageCount(pBt, newMax); |
drh | 60ac3f4 | 2010-11-23 18:59:27 +0000 | [diff] [blame] | 8344 | break; |
| 8345 | } |
| 8346 | #endif |
| 8347 | |
drh | 920cf59 | 2019-10-30 16:29:02 +0000 | [diff] [blame] | 8348 | /* Opcode: Function P1 P2 P3 P4 * |
drh | d7b10d7 | 2020-02-01 17:38:24 +0000 | [diff] [blame] | 8349 | ** Synopsis: r[P3]=func(r[P2@NP]) |
drh | 3e34eab | 2017-07-19 19:48:40 +0000 | [diff] [blame] | 8350 | ** |
| 8351 | ** 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] | 8352 | ** contains a pointer to the function to be run) with arguments taken |
| 8353 | ** from register P2 and successors. The number of arguments is in |
| 8354 | ** the sqlite3_context object that P4 points to. |
| 8355 | ** The result of the function is stored |
drh | 3e34eab | 2017-07-19 19:48:40 +0000 | [diff] [blame] | 8356 | ** in register P3. Register P3 must not be one of the function inputs. |
| 8357 | ** |
| 8358 | ** P1 is a 32-bit bitmask indicating whether or not each argument to the |
| 8359 | ** function was determined to be constant at compile time. If the first |
| 8360 | ** argument was constant then bit 0 of P1 is set. This is used to determine |
| 8361 | ** whether meta data associated with a user function argument using the |
| 8362 | ** sqlite3_set_auxdata() API may be safely retained until the next |
| 8363 | ** invocation of this opcode. |
| 8364 | ** |
drh | 920cf59 | 2019-10-30 16:29:02 +0000 | [diff] [blame] | 8365 | ** See also: AggStep, AggFinal, PureFunc |
drh | 3e34eab | 2017-07-19 19:48:40 +0000 | [diff] [blame] | 8366 | */ |
drh | 920cf59 | 2019-10-30 16:29:02 +0000 | [diff] [blame] | 8367 | /* Opcode: PureFunc P1 P2 P3 P4 * |
drh | d7b10d7 | 2020-02-01 17:38:24 +0000 | [diff] [blame] | 8368 | ** Synopsis: r[P3]=func(r[P2@NP]) |
drh | 920cf59 | 2019-10-30 16:29:02 +0000 | [diff] [blame] | 8369 | ** |
| 8370 | ** Invoke a user function (P4 is a pointer to an sqlite3_context object that |
| 8371 | ** contains a pointer to the function to be run) with arguments taken |
| 8372 | ** from register P2 and successors. The number of arguments is in |
| 8373 | ** the sqlite3_context object that P4 points to. |
| 8374 | ** The result of the function is stored |
| 8375 | ** in register P3. Register P3 must not be one of the function inputs. |
| 8376 | ** |
| 8377 | ** P1 is a 32-bit bitmask indicating whether or not each argument to the |
| 8378 | ** function was determined to be constant at compile time. If the first |
| 8379 | ** argument was constant then bit 0 of P1 is set. This is used to determine |
| 8380 | ** whether meta data associated with a user function argument using the |
| 8381 | ** sqlite3_set_auxdata() API may be safely retained until the next |
| 8382 | ** invocation of this opcode. |
| 8383 | ** |
| 8384 | ** This opcode works exactly like OP_Function. The only difference is in |
| 8385 | ** its name. This opcode is used in places where the function must be |
| 8386 | ** purely non-deterministic. Some built-in date/time functions can be |
| 8387 | ** either determinitic of non-deterministic, depending on their arguments. |
| 8388 | ** When those function are used in a non-deterministic way, they will check |
| 8389 | ** to see if they were called using OP_PureFunc instead of OP_Function, and |
| 8390 | ** if they were, they throw an error. |
| 8391 | ** |
| 8392 | ** See also: AggStep, AggFinal, Function |
| 8393 | */ |
mistachkin | 758784d | 2018-07-25 15:12:29 +0000 | [diff] [blame] | 8394 | case OP_PureFunc: /* group */ |
| 8395 | case OP_Function: { /* group */ |
drh | 3e34eab | 2017-07-19 19:48:40 +0000 | [diff] [blame] | 8396 | int i; |
| 8397 | sqlite3_context *pCtx; |
| 8398 | |
| 8399 | assert( pOp->p4type==P4_FUNCCTX ); |
| 8400 | pCtx = pOp->p4.pCtx; |
| 8401 | |
| 8402 | /* If this function is inside of a trigger, the register array in aMem[] |
| 8403 | ** might change from one evaluation to the next. The next block of code |
| 8404 | ** checks to see if the register array has changed, and if so it |
| 8405 | ** reinitializes the relavant parts of the sqlite3_context object */ |
| 8406 | pOut = &aMem[pOp->p3]; |
| 8407 | if( pCtx->pOut != pOut ){ |
drh | 920cf59 | 2019-10-30 16:29:02 +0000 | [diff] [blame] | 8408 | pCtx->pVdbe = p; |
drh | 3e34eab | 2017-07-19 19:48:40 +0000 | [diff] [blame] | 8409 | pCtx->pOut = pOut; |
drh | 659fdb4 | 2022-04-01 15:31:58 +0000 | [diff] [blame] | 8410 | pCtx->enc = encoding; |
drh | 3e34eab | 2017-07-19 19:48:40 +0000 | [diff] [blame] | 8411 | for(i=pCtx->argc-1; i>=0; i--) pCtx->argv[i] = &aMem[pOp->p2+i]; |
| 8412 | } |
drh | 920cf59 | 2019-10-30 16:29:02 +0000 | [diff] [blame] | 8413 | assert( pCtx->pVdbe==p ); |
drh | 3e34eab | 2017-07-19 19:48:40 +0000 | [diff] [blame] | 8414 | |
| 8415 | memAboutToChange(p, pOut); |
| 8416 | #ifdef SQLITE_DEBUG |
| 8417 | for(i=0; i<pCtx->argc; i++){ |
| 8418 | assert( memIsValid(pCtx->argv[i]) ); |
| 8419 | REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]); |
| 8420 | } |
| 8421 | #endif |
| 8422 | MemSetTypeFlag(pOut, MEM_Null); |
drh | f09ac0b | 2018-01-23 03:44:06 +0000 | [diff] [blame] | 8423 | assert( pCtx->isError==0 ); |
drh | 3e34eab | 2017-07-19 19:48:40 +0000 | [diff] [blame] | 8424 | (*pCtx->pFunc->xSFunc)(pCtx, pCtx->argc, pCtx->argv);/* IMP: R-24505-23230 */ |
| 8425 | |
| 8426 | /* If the function returned an error, throw an exception */ |
drh | f09ac0b | 2018-01-23 03:44:06 +0000 | [diff] [blame] | 8427 | if( pCtx->isError ){ |
| 8428 | if( pCtx->isError>0 ){ |
drh | 3e34eab | 2017-07-19 19:48:40 +0000 | [diff] [blame] | 8429 | sqlite3VdbeError(p, "%s", sqlite3_value_text(pOut)); |
| 8430 | rc = pCtx->isError; |
| 8431 | } |
| 8432 | sqlite3VdbeDeleteAuxData(db, &p->pAuxData, pCtx->iOp, pOp->p1); |
drh | f09ac0b | 2018-01-23 03:44:06 +0000 | [diff] [blame] | 8433 | pCtx->isError = 0; |
drh | 3e34eab | 2017-07-19 19:48:40 +0000 | [diff] [blame] | 8434 | if( rc ) goto abort_due_to_error; |
| 8435 | } |
| 8436 | |
drh | fb92e07 | 2022-03-29 01:43:09 +0000 | [diff] [blame] | 8437 | assert( (pOut->flags&MEM_Str)==0 |
| 8438 | || pOut->enc==encoding |
| 8439 | || db->mallocFailed ); |
| 8440 | assert( !sqlite3VdbeMemTooBig(pOut) ); |
drh | 3e34eab | 2017-07-19 19:48:40 +0000 | [diff] [blame] | 8441 | |
| 8442 | REGISTER_TRACE(pOp->p3, pOut); |
| 8443 | UPDATE_MAX_BLOBSIZE(pOut); |
| 8444 | break; |
| 8445 | } |
| 8446 | |
drh | 8878f8a | 2022-06-09 16:19:01 +0000 | [diff] [blame] | 8447 | /* Opcode: ClrSubtype P1 * * * * |
| 8448 | ** Synopsis: r[P1].subtype = 0 |
| 8449 | ** |
| 8450 | ** Clear the subtype from register P1. |
| 8451 | */ |
| 8452 | case OP_ClrSubtype: { /* in1 */ |
| 8453 | pIn1 = &aMem[pOp->p1]; |
| 8454 | pIn1->flags &= ~MEM_Subtype; |
| 8455 | break; |
| 8456 | } |
| 8457 | |
drh | 2db144c | 2021-12-01 16:31:02 +0000 | [diff] [blame] | 8458 | /* Opcode: FilterAdd P1 * P3 P4 * |
| 8459 | ** Synopsis: filter(P1) += key(P3@P4) |
| 8460 | ** |
| 8461 | ** Compute a hash on the P4 registers starting with r[P3] and |
| 8462 | ** add that hash to the bloom filter contained in r[P1]. |
| 8463 | */ |
| 8464 | case OP_FilterAdd: { |
drh | 5baaf40 | 2021-12-06 13:07:28 +0000 | [diff] [blame] | 8465 | u64 h; |
drh | 2db144c | 2021-12-01 16:31:02 +0000 | [diff] [blame] | 8466 | |
| 8467 | assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) ); |
| 8468 | pIn1 = &aMem[pOp->p1]; |
| 8469 | assert( pIn1->flags & MEM_Blob ); |
drh | 5baaf40 | 2021-12-06 13:07:28 +0000 | [diff] [blame] | 8470 | assert( pIn1->n>0 ); |
drh | 2db144c | 2021-12-01 16:31:02 +0000 | [diff] [blame] | 8471 | h = filterHash(aMem, pOp); |
| 8472 | #ifdef SQLITE_DEBUG |
| 8473 | if( db->flags&SQLITE_VdbeTrace ){ |
| 8474 | int ii; |
| 8475 | for(ii=pOp->p3; ii<pOp->p3+pOp->p4.i; ii++){ |
| 8476 | registerTrace(ii, &aMem[ii]); |
| 8477 | } |
drh | 5baaf40 | 2021-12-06 13:07:28 +0000 | [diff] [blame] | 8478 | 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] | 8479 | } |
| 8480 | #endif |
drh | 5baaf40 | 2021-12-06 13:07:28 +0000 | [diff] [blame] | 8481 | h %= pIn1->n; |
drh | 2db144c | 2021-12-01 16:31:02 +0000 | [diff] [blame] | 8482 | pIn1->z[h/8] |= 1<<(h&7); |
| 8483 | break; |
| 8484 | } |
| 8485 | |
| 8486 | /* Opcode: Filter P1 P2 P3 P4 * |
| 8487 | ** Synopsis: if key(P3@P4) not in filter(P1) goto P2 |
| 8488 | ** |
| 8489 | ** Compute a hash on the key contained in the P4 registers starting |
| 8490 | ** with r[P3]. Check to see if that hash is found in the |
| 8491 | ** bloom filter hosted by register P1. If it is not present then |
| 8492 | ** maybe jump to P2. Otherwise fall through. |
| 8493 | ** |
| 8494 | ** False negatives are harmless. It is always safe to fall through, |
| 8495 | ** even if the value is in the bloom filter. A false negative causes |
| 8496 | ** more CPU cycles to be used, but it should still yield the correct |
| 8497 | ** answer. However, an incorrect answer may well arise from a |
| 8498 | ** false positive - if the jump is taken when it should fall through. |
| 8499 | */ |
| 8500 | case OP_Filter: { /* jump */ |
drh | 5baaf40 | 2021-12-06 13:07:28 +0000 | [diff] [blame] | 8501 | u64 h; |
drh | 2db144c | 2021-12-01 16:31:02 +0000 | [diff] [blame] | 8502 | |
| 8503 | assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) ); |
| 8504 | pIn1 = &aMem[pOp->p1]; |
drh | 7e910f6 | 2021-12-09 01:28:15 +0000 | [diff] [blame] | 8505 | assert( (pIn1->flags & MEM_Blob)!=0 ); |
| 8506 | assert( pIn1->n >= 1 ); |
drh | 2db144c | 2021-12-01 16:31:02 +0000 | [diff] [blame] | 8507 | h = filterHash(aMem, pOp); |
| 8508 | #ifdef SQLITE_DEBUG |
| 8509 | if( db->flags&SQLITE_VdbeTrace ){ |
| 8510 | int ii; |
| 8511 | for(ii=pOp->p3; ii<pOp->p3+pOp->p4.i; ii++){ |
| 8512 | registerTrace(ii, &aMem[ii]); |
| 8513 | } |
drh | 5baaf40 | 2021-12-06 13:07:28 +0000 | [diff] [blame] | 8514 | 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] | 8515 | } |
| 8516 | #endif |
drh | 5baaf40 | 2021-12-06 13:07:28 +0000 | [diff] [blame] | 8517 | h %= pIn1->n; |
drh | 067c60c | 2021-12-04 18:45:08 +0000 | [diff] [blame] | 8518 | if( (pIn1->z[h/8] & (1<<(h&7)))==0 ){ |
| 8519 | VdbeBranchTaken(1, 2); |
drh | 23d41e6 | 2021-12-06 21:45:31 +0000 | [diff] [blame] | 8520 | p->aCounter[SQLITE_STMTSTATUS_FILTER_HIT]++; |
drh | 067c60c | 2021-12-04 18:45:08 +0000 | [diff] [blame] | 8521 | goto jump_to_p2; |
| 8522 | }else{ |
drh | 23d41e6 | 2021-12-06 21:45:31 +0000 | [diff] [blame] | 8523 | p->aCounter[SQLITE_STMTSTATUS_FILTER_MISS]++; |
drh | 067c60c | 2021-12-04 18:45:08 +0000 | [diff] [blame] | 8524 | VdbeBranchTaken(0, 2); |
| 8525 | } |
drh | 2db144c | 2021-12-01 16:31:02 +0000 | [diff] [blame] | 8526 | break; |
| 8527 | } |
| 8528 | |
drh | f259df5 | 2017-12-27 20:38:35 +0000 | [diff] [blame] | 8529 | /* Opcode: Trace P1 P2 * P4 * |
| 8530 | ** |
| 8531 | ** Write P4 on the statement trace output if statement tracing is |
| 8532 | ** enabled. |
| 8533 | ** |
| 8534 | ** Operand P1 must be 0x7fffffff and P2 must positive. |
| 8535 | */ |
drh | 74588ce | 2017-09-13 00:13:05 +0000 | [diff] [blame] | 8536 | /* Opcode: Init P1 P2 P3 P4 * |
drh | 72e26de | 2016-08-24 21:24:04 +0000 | [diff] [blame] | 8537 | ** Synopsis: Start at P2 |
drh | aceb31b | 2014-02-08 01:40:27 +0000 | [diff] [blame] | 8538 | ** |
| 8539 | ** Programs contain a single instance of this opcode as the very first |
| 8540 | ** opcode. |
drh | 949f9cd | 2008-01-12 21:35:57 +0000 | [diff] [blame] | 8541 | ** |
| 8542 | ** If tracing is enabled (by the sqlite3_trace()) interface, then |
| 8543 | ** the UTF-8 string contained in P4 is emitted on the trace callback. |
drh | aceb31b | 2014-02-08 01:40:27 +0000 | [diff] [blame] | 8544 | ** Or if P4 is blank, use the string returned by sqlite3_sql(). |
| 8545 | ** |
| 8546 | ** If P2 is not zero, jump to instruction P2. |
drh | 9e5eb9c | 2016-09-18 16:08:10 +0000 | [diff] [blame] | 8547 | ** |
| 8548 | ** Increment the value of P1 so that OP_Once opcodes will jump the |
| 8549 | ** first time they are evaluated for this run. |
drh | 74588ce | 2017-09-13 00:13:05 +0000 | [diff] [blame] | 8550 | ** |
| 8551 | ** If P3 is not zero, then it is an address to jump to if an SQLITE_CORRUPT |
| 8552 | ** error is encountered. |
drh | 949f9cd | 2008-01-12 21:35:57 +0000 | [diff] [blame] | 8553 | */ |
drh | f259df5 | 2017-12-27 20:38:35 +0000 | [diff] [blame] | 8554 | case OP_Trace: |
drh | aceb31b | 2014-02-08 01:40:27 +0000 | [diff] [blame] | 8555 | case OP_Init: { /* jump */ |
drh | 9e5eb9c | 2016-09-18 16:08:10 +0000 | [diff] [blame] | 8556 | int i; |
drh | b9f4799 | 2018-01-24 12:14:43 +0000 | [diff] [blame] | 8557 | #ifndef SQLITE_OMIT_TRACE |
| 8558 | char *zTrace; |
| 8559 | #endif |
drh | 5fe63bf | 2016-07-25 02:42:22 +0000 | [diff] [blame] | 8560 | |
| 8561 | /* If the P4 argument is not NULL, then it must be an SQL comment string. |
| 8562 | ** The "--" string is broken up to prevent false-positives with srcck1.c. |
| 8563 | ** |
| 8564 | ** This assert() provides evidence for: |
| 8565 | ** EVIDENCE-OF: R-50676-09860 The callback can compute the same text that |
| 8566 | ** would have been returned by the legacy sqlite3_trace() interface by |
| 8567 | ** using the X argument when X begins with "--" and invoking |
| 8568 | ** sqlite3_expanded_sql(P) otherwise. |
| 8569 | */ |
| 8570 | assert( pOp->p4.z==0 || strncmp(pOp->p4.z, "-" "- ", 3)==0 ); |
drh | f259df5 | 2017-12-27 20:38:35 +0000 | [diff] [blame] | 8571 | |
| 8572 | /* OP_Init is always instruction 0 */ |
| 8573 | assert( pOp==p->aOp || pOp->opcode==OP_Trace ); |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 8574 | |
drh | aceb31b | 2014-02-08 01:40:27 +0000 | [diff] [blame] | 8575 | #ifndef SQLITE_OMIT_TRACE |
drh | fca760c | 2016-07-14 01:09:08 +0000 | [diff] [blame] | 8576 | if( (db->mTrace & (SQLITE_TRACE_STMT|SQLITE_TRACE_LEGACY))!=0 |
drh | a24832b | 2022-04-01 19:04:13 +0000 | [diff] [blame] | 8577 | && p->minWriteFileFormat!=254 /* tag-20220401a */ |
drh | 37f58e9 | 2012-09-04 21:34:26 +0000 | [diff] [blame] | 8578 | && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0 |
| 8579 | ){ |
drh | 3d2a529 | 2016-07-13 22:55:01 +0000 | [diff] [blame] | 8580 | #ifndef SQLITE_OMIT_DEPRECATED |
drh | fca760c | 2016-07-14 01:09:08 +0000 | [diff] [blame] | 8581 | if( db->mTrace & SQLITE_TRACE_LEGACY ){ |
drh | 5fe63bf | 2016-07-25 02:42:22 +0000 | [diff] [blame] | 8582 | char *z = sqlite3VdbeExpandSql(p, zTrace); |
drh | 08b9208 | 2020-08-10 14:18:00 +0000 | [diff] [blame] | 8583 | db->trace.xLegacy(db->pTraceArg, z); |
drh | bd441f7 | 2016-07-25 02:31:48 +0000 | [diff] [blame] | 8584 | sqlite3_free(z); |
drh | fca760c | 2016-07-14 01:09:08 +0000 | [diff] [blame] | 8585 | }else |
drh | 3d2a529 | 2016-07-13 22:55:01 +0000 | [diff] [blame] | 8586 | #endif |
drh | 7adbcff | 2017-03-20 15:29:28 +0000 | [diff] [blame] | 8587 | if( db->nVdbeExec>1 ){ |
| 8588 | char *z = sqlite3MPrintf(db, "-- %s", zTrace); |
drh | 08b9208 | 2020-08-10 14:18:00 +0000 | [diff] [blame] | 8589 | (void)db->trace.xV2(SQLITE_TRACE_STMT, db->pTraceArg, p, z); |
drh | 7adbcff | 2017-03-20 15:29:28 +0000 | [diff] [blame] | 8590 | sqlite3DbFree(db, z); |
| 8591 | }else{ |
drh | 08b9208 | 2020-08-10 14:18:00 +0000 | [diff] [blame] | 8592 | (void)db->trace.xV2(SQLITE_TRACE_STMT, db->pTraceArg, p, zTrace); |
drh | 3d2a529 | 2016-07-13 22:55:01 +0000 | [diff] [blame] | 8593 | } |
drh | 949f9cd | 2008-01-12 21:35:57 +0000 | [diff] [blame] | 8594 | } |
drh | 8f8b231 | 2013-10-18 20:03:43 +0000 | [diff] [blame] | 8595 | #ifdef SQLITE_USE_FCNTL_TRACE |
| 8596 | zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql); |
| 8597 | if( zTrace ){ |
mistachkin | d8992ce | 2016-09-20 17:49:01 +0000 | [diff] [blame] | 8598 | int j; |
| 8599 | for(j=0; j<db->nDb; j++){ |
| 8600 | if( DbMaskTest(p->btreeMask, j)==0 ) continue; |
| 8601 | sqlite3_file_control(db, db->aDb[j].zDbSName, SQLITE_FCNTL_TRACE, zTrace); |
drh | 8f8b231 | 2013-10-18 20:03:43 +0000 | [diff] [blame] | 8602 | } |
| 8603 | } |
| 8604 | #endif /* SQLITE_USE_FCNTL_TRACE */ |
drh | c3f1d5f | 2011-05-30 23:42:16 +0000 | [diff] [blame] | 8605 | #ifdef SQLITE_DEBUG |
| 8606 | if( (db->flags & SQLITE_SqlTrace)!=0 |
| 8607 | && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0 |
| 8608 | ){ |
| 8609 | sqlite3DebugPrintf("SQL-trace: %s\n", zTrace); |
| 8610 | } |
| 8611 | #endif /* SQLITE_DEBUG */ |
drh | aceb31b | 2014-02-08 01:40:27 +0000 | [diff] [blame] | 8612 | #endif /* SQLITE_OMIT_TRACE */ |
drh | 4910a76 | 2016-09-03 01:46:15 +0000 | [diff] [blame] | 8613 | assert( pOp->p2>0 ); |
drh | 9e5eb9c | 2016-09-18 16:08:10 +0000 | [diff] [blame] | 8614 | if( pOp->p1>=sqlite3GlobalConfig.iOnceResetThreshold ){ |
drh | f259df5 | 2017-12-27 20:38:35 +0000 | [diff] [blame] | 8615 | if( pOp->opcode==OP_Trace ) break; |
drh | 9e5eb9c | 2016-09-18 16:08:10 +0000 | [diff] [blame] | 8616 | for(i=1; i<p->nOp; i++){ |
| 8617 | if( p->aOp[i].opcode==OP_Once ) p->aOp[i].p1 = 0; |
| 8618 | } |
| 8619 | pOp->p1 = 0; |
| 8620 | } |
| 8621 | pOp->p1++; |
drh | 00d11d4 | 2017-06-29 12:49:18 +0000 | [diff] [blame] | 8622 | p->aCounter[SQLITE_STMTSTATUS_RUN]++; |
drh | 4910a76 | 2016-09-03 01:46:15 +0000 | [diff] [blame] | 8623 | goto jump_to_p2; |
drh | 949f9cd | 2008-01-12 21:35:57 +0000 | [diff] [blame] | 8624 | } |
drh | 949f9cd | 2008-01-12 21:35:57 +0000 | [diff] [blame] | 8625 | |
drh | 2893536 | 2013-12-07 20:39:19 +0000 | [diff] [blame] | 8626 | #ifdef SQLITE_ENABLE_CURSOR_HINTS |
drh | 0df5701 | 2015-08-14 15:05:55 +0000 | [diff] [blame] | 8627 | /* Opcode: CursorHint P1 * * P4 * |
drh | 2893536 | 2013-12-07 20:39:19 +0000 | [diff] [blame] | 8628 | ** |
| 8629 | ** 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] | 8630 | ** satisfy the Expr in P4. TK_REGISTER terms in the P4 expression refer |
| 8631 | ** to values currently held in registers. TK_COLUMN terms in the P4 |
| 8632 | ** 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] | 8633 | */ |
| 8634 | case OP_CursorHint: { |
| 8635 | VdbeCursor *pC; |
| 8636 | |
| 8637 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 8638 | assert( pOp->p4type==P4_EXPR ); |
| 8639 | pC = p->apCsr[pOp->p1]; |
dan | 91d3a61 | 2014-07-15 11:59:44 +0000 | [diff] [blame] | 8640 | if( pC ){ |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 8641 | assert( pC->eCurType==CURTYPE_BTREE ); |
drh | 62aaa6c | 2015-11-21 17:27:42 +0000 | [diff] [blame] | 8642 | sqlite3BtreeCursorHint(pC->uc.pCursor, BTREE_HINT_RANGE, |
| 8643 | pOp->p4.pExpr, aMem); |
dan | 91d3a61 | 2014-07-15 11:59:44 +0000 | [diff] [blame] | 8644 | } |
drh | 2893536 | 2013-12-07 20:39:19 +0000 | [diff] [blame] | 8645 | break; |
| 8646 | } |
| 8647 | #endif /* SQLITE_ENABLE_CURSOR_HINTS */ |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 8648 | |
drh | 4031baf | 2018-05-28 17:31:20 +0000 | [diff] [blame] | 8649 | #ifdef SQLITE_DEBUG |
| 8650 | /* Opcode: Abortable * * * * * |
| 8651 | ** |
| 8652 | ** Verify that an Abort can happen. Assert if an Abort at this point |
| 8653 | ** might cause database corruption. This opcode only appears in debugging |
| 8654 | ** builds. |
| 8655 | ** |
| 8656 | ** An Abort is safe if either there have been no writes, or if there is |
| 8657 | ** an active statement journal. |
| 8658 | */ |
| 8659 | case OP_Abortable: { |
| 8660 | sqlite3VdbeAssertAbortable(p); |
| 8661 | break; |
| 8662 | } |
| 8663 | #endif |
| 8664 | |
drh | 13d7950 | 2019-12-23 02:18:49 +0000 | [diff] [blame] | 8665 | #ifdef SQLITE_DEBUG |
drh | 3aef2fb | 2020-01-02 17:46:02 +0000 | [diff] [blame] | 8666 | /* Opcode: ReleaseReg P1 P2 P3 * P5 |
drh | 13d7950 | 2019-12-23 02:18:49 +0000 | [diff] [blame] | 8667 | ** Synopsis: release r[P1@P2] mask P3 |
| 8668 | ** |
| 8669 | ** Release registers from service. Any content that was in the |
| 8670 | ** the registers is unreliable after this opcode completes. |
| 8671 | ** |
| 8672 | ** The registers released will be the P2 registers starting at P1, |
| 8673 | ** except if bit ii of P3 set, then do not release register P1+ii. |
| 8674 | ** In other words, P3 is a mask of registers to preserve. |
| 8675 | ** |
| 8676 | ** Releasing a register clears the Mem.pScopyFrom pointer. That means |
| 8677 | ** that if the content of the released register was set using OP_SCopy, |
| 8678 | ** a change to the value of the source register for the OP_SCopy will no longer |
| 8679 | ** generate an assertion fault in sqlite3VdbeMemAboutToChange(). |
| 8680 | ** |
drh | 3aef2fb | 2020-01-02 17:46:02 +0000 | [diff] [blame] | 8681 | ** If P5 is set, then all released registers have their type set |
| 8682 | ** to MEM_Undefined so that any subsequent attempt to read the released |
drh | 13d7950 | 2019-12-23 02:18:49 +0000 | [diff] [blame] | 8683 | ** register (before it is reinitialized) will generate an assertion fault. |
drh | 3aef2fb | 2020-01-02 17:46:02 +0000 | [diff] [blame] | 8684 | ** |
| 8685 | ** P5 ought to be set on every call to this opcode. |
| 8686 | ** However, there are places in the code generator will release registers |
drh | 13d7950 | 2019-12-23 02:18:49 +0000 | [diff] [blame] | 8687 | ** before their are used, under the (valid) assumption that the registers |
| 8688 | ** will not be reallocated for some other purpose before they are used and |
| 8689 | ** hence are safe to release. |
| 8690 | ** |
| 8691 | ** This opcode is only available in testing and debugging builds. It is |
| 8692 | ** not generated for release builds. The purpose of this opcode is to help |
| 8693 | ** validate the generated bytecode. This opcode does not actually contribute |
| 8694 | ** to computing an answer. |
| 8695 | */ |
| 8696 | case OP_ReleaseReg: { |
| 8697 | Mem *pMem; |
| 8698 | int i; |
| 8699 | u32 constMask; |
| 8700 | assert( pOp->p1>0 ); |
| 8701 | assert( pOp->p1+pOp->p2<=(p->nMem+1 - p->nCursor)+1 ); |
| 8702 | pMem = &aMem[pOp->p1]; |
| 8703 | constMask = pOp->p3; |
| 8704 | for(i=0; i<pOp->p2; i++, pMem++){ |
drh | 7edce5e | 2019-12-23 13:24:34 +0000 | [diff] [blame] | 8705 | if( i>=32 || (constMask & MASKBIT32(i))==0 ){ |
drh | 13d7950 | 2019-12-23 02:18:49 +0000 | [diff] [blame] | 8706 | pMem->pScopyFrom = 0; |
drh | 3aef2fb | 2020-01-02 17:46:02 +0000 | [diff] [blame] | 8707 | if( i<32 && pOp->p5 ) MemSetTypeFlag(pMem, MEM_Undefined); |
drh | 13d7950 | 2019-12-23 02:18:49 +0000 | [diff] [blame] | 8708 | } |
| 8709 | } |
| 8710 | break; |
| 8711 | } |
| 8712 | #endif |
| 8713 | |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 8714 | /* Opcode: Noop * * * * * |
| 8715 | ** |
| 8716 | ** Do nothing. This instruction is often useful as a jump |
| 8717 | ** destination. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 8718 | */ |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 8719 | /* |
| 8720 | ** The magic Explain opcode are only inserted when explain==2 (which |
| 8721 | ** is to say when the EXPLAIN QUERY PLAN syntax is used.) |
| 8722 | ** This opcode records information from the optimizer. It is the |
| 8723 | ** the same as a no-op. This opcodesnever appears in a real VM program. |
| 8724 | */ |
drh | 4031baf | 2018-05-28 17:31:20 +0000 | [diff] [blame] | 8725 | default: { /* This is really OP_Noop, OP_Explain */ |
drh | 13573c7 | 2010-01-12 17:04:07 +0000 | [diff] [blame] | 8726 | assert( pOp->opcode==OP_Noop || pOp->opcode==OP_Explain ); |
drh | 4031baf | 2018-05-28 17:31:20 +0000 | [diff] [blame] | 8727 | |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 8728 | break; |
| 8729 | } |
| 8730 | |
| 8731 | /***************************************************************************** |
| 8732 | ** The cases of the switch statement above this line should all be indented |
| 8733 | ** by 6 spaces. But the left-most 6 spaces have been removed to improve the |
| 8734 | ** readability. From this point on down, the normal indentation rules are |
| 8735 | ** restored. |
| 8736 | *****************************************************************************/ |
| 8737 | } |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 8738 | |
drh | 7b39686 | 2003-01-01 23:06:20 +0000 | [diff] [blame] | 8739 | #ifdef VDBE_PROFILE |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 8740 | { |
drh | 35043cc | 2018-02-12 20:27:34 +0000 | [diff] [blame] | 8741 | u64 endTime = sqlite3NProfileCnt ? sqlite3NProfileCnt : sqlite3Hwtime(); |
drh | 6dc4148 | 2015-04-16 17:31:02 +0000 | [diff] [blame] | 8742 | if( endTime>start ) pOrigOp->cycles += endTime - start; |
| 8743 | pOrigOp->cnt++; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 8744 | } |
drh | 7b39686 | 2003-01-01 23:06:20 +0000 | [diff] [blame] | 8745 | #endif |
| 8746 | |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 8747 | /* The following code adds nothing to the actual functionality |
| 8748 | ** of the program. It is only here for testing and debugging. |
| 8749 | ** On the other hand, it does burn CPU cycles every time through |
| 8750 | ** the evaluator loop. So we can leave it out when NDEBUG is defined. |
| 8751 | */ |
| 8752 | #ifndef NDEBUG |
drh | 6dc4148 | 2015-04-16 17:31:02 +0000 | [diff] [blame] | 8753 | assert( pOp>=&aOp[-1] && pOp<&aOp[p->nOp-1] ); |
drh | ae7e151 | 2007-05-02 16:51:59 +0000 | [diff] [blame] | 8754 | |
drh | cf1023c | 2007-05-08 20:59:49 +0000 | [diff] [blame] | 8755 | #ifdef SQLITE_DEBUG |
drh | 84e55a8 | 2013-11-13 17:58:23 +0000 | [diff] [blame] | 8756 | if( db->flags & SQLITE_VdbeTrace ){ |
drh | 7cc84c2 | 2016-04-11 13:36:42 +0000 | [diff] [blame] | 8757 | u8 opProperty = sqlite3OpcodeProperty[pOrigOp->opcode]; |
drh | 84e55a8 | 2013-11-13 17:58:23 +0000 | [diff] [blame] | 8758 | if( rc!=0 ) printf("rc=%d\n",rc); |
drh | 7cc84c2 | 2016-04-11 13:36:42 +0000 | [diff] [blame] | 8759 | if( opProperty & (OPFLG_OUT2) ){ |
drh | 6dc4148 | 2015-04-16 17:31:02 +0000 | [diff] [blame] | 8760 | registerTrace(pOrigOp->p2, &aMem[pOrigOp->p2]); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 8761 | } |
drh | 7cc84c2 | 2016-04-11 13:36:42 +0000 | [diff] [blame] | 8762 | if( opProperty & OPFLG_OUT3 ){ |
drh | 6dc4148 | 2015-04-16 17:31:02 +0000 | [diff] [blame] | 8763 | registerTrace(pOrigOp->p3, &aMem[pOrigOp->p3]); |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 8764 | } |
drh | 17aceeb | 2020-01-04 19:12:13 +0000 | [diff] [blame] | 8765 | if( opProperty==0xff ){ |
| 8766 | /* Never happens. This code exists to avoid a harmless linkage |
| 8767 | ** warning aboud sqlite3VdbeRegisterDump() being defined but not |
| 8768 | ** used. */ |
| 8769 | sqlite3VdbeRegisterDump(p); |
| 8770 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 8771 | } |
danielk1977 | b5402fb | 2005-01-12 07:15:04 +0000 | [diff] [blame] | 8772 | #endif /* SQLITE_DEBUG */ |
| 8773 | #endif /* NDEBUG */ |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 8774 | } /* The end of the for(;;) loop the loops through opcodes */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 8775 | |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 8776 | /* If we reach this point, it means that execution is finished with |
| 8777 | ** an error of some kind. |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 8778 | */ |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 8779 | abort_due_to_error: |
drh | f56a4bf | 2020-11-18 21:50:05 +0000 | [diff] [blame] | 8780 | if( db->mallocFailed ){ |
| 8781 | rc = SQLITE_NOMEM_BKPT; |
| 8782 | }else if( rc==SQLITE_IOERR_CORRUPTFS ){ |
| 8783 | rc = SQLITE_CORRUPT_BKPT; |
| 8784 | } |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 8785 | assert( rc ); |
drh | 11eb9c6 | 2021-09-16 12:33:53 +0000 | [diff] [blame] | 8786 | #ifdef SQLITE_DEBUG |
| 8787 | if( db->flags & SQLITE_VdbeTrace ){ |
drh | 5b001cc | 2021-11-15 13:22:42 +0000 | [diff] [blame] | 8788 | const char *zTrace = p->zSql; |
| 8789 | if( zTrace==0 ){ |
| 8790 | if( aOp[0].opcode==OP_Trace ){ |
| 8791 | zTrace = aOp[0].p4.z; |
| 8792 | } |
| 8793 | if( zTrace==0 ) zTrace = "???"; |
| 8794 | } |
| 8795 | printf("ABORT-due-to-error (rc=%d): %s\n", rc, zTrace); |
drh | 11eb9c6 | 2021-09-16 12:33:53 +0000 | [diff] [blame] | 8796 | } |
| 8797 | #endif |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 8798 | if( p->zErrMsg==0 && rc!=SQLITE_IOERR_NOMEM ){ |
| 8799 | sqlite3VdbeError(p, "%s", sqlite3ErrStr(rc)); |
| 8800 | } |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 8801 | p->rc = rc; |
drh | f68521c | 2016-03-21 12:28:02 +0000 | [diff] [blame] | 8802 | sqlite3SystemError(db, rc); |
drh | a64fa91 | 2010-03-04 00:53:32 +0000 | [diff] [blame] | 8803 | testcase( sqlite3GlobalConfig.xLog!=0 ); |
| 8804 | sqlite3_log(rc, "statement aborts at %d: [%s] %s", |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 8805 | (int)(pOp - aOp), p->zSql, p->zErrMsg); |
drh | 8703edd | 2022-04-03 22:35:13 +0000 | [diff] [blame] | 8806 | if( p->eVdbeState==VDBE_RUN_STATE ) sqlite3VdbeHalt(p); |
drh | 4a642b6 | 2016-02-05 01:55:27 +0000 | [diff] [blame] | 8807 | if( rc==SQLITE_IOERR_NOMEM ) sqlite3OomFault(db); |
drh | 46c425b | 2021-11-10 10:59:10 +0000 | [diff] [blame] | 8808 | if( rc==SQLITE_CORRUPT && db->autoCommit==0 ){ |
| 8809 | db->flags |= SQLITE_CorruptRdOnly; |
| 8810 | } |
danielk1977 | 7eaabcd | 2008-07-07 14:56:56 +0000 | [diff] [blame] | 8811 | rc = SQLITE_ERROR; |
drh | cdf011d | 2011-04-04 21:25:28 +0000 | [diff] [blame] | 8812 | if( resetSchemaOnFault>0 ){ |
drh | 81028a4 | 2012-05-15 18:28:27 +0000 | [diff] [blame] | 8813 | sqlite3ResetOneSchema(db, resetSchemaOnFault-1); |
drh | bdaec52 | 2011-04-04 00:14:43 +0000 | [diff] [blame] | 8814 | } |
drh | 900b31e | 2007-08-28 02:27:51 +0000 | [diff] [blame] | 8815 | |
| 8816 | /* This is the only way out of this procedure. We have to |
| 8817 | ** release the mutexes on btrees that were acquired at the |
| 8818 | ** top. */ |
| 8819 | vdbe_return: |
drh | c332e04 | 2019-02-12 21:04:33 +0000 | [diff] [blame] | 8820 | #ifndef SQLITE_OMIT_PROGRESS_CALLBACK |
drh | b1af9c6 | 2019-02-20 13:55:45 +0000 | [diff] [blame] | 8821 | while( nVmStep>=nProgressLimit && db->xProgress!=0 ){ |
| 8822 | nProgressLimit += db->nProgressOps; |
drh | c332e04 | 2019-02-12 21:04:33 +0000 | [diff] [blame] | 8823 | if( db->xProgress(db->pProgressArg) ){ |
drh | d1d8914 | 2020-07-06 12:13:05 +0000 | [diff] [blame] | 8824 | nProgressLimit = LARGEST_UINT64; |
drh | c332e04 | 2019-02-12 21:04:33 +0000 | [diff] [blame] | 8825 | rc = SQLITE_INTERRUPT; |
| 8826 | goto abort_due_to_error; |
| 8827 | } |
| 8828 | } |
| 8829 | #endif |
drh | 9b47ee3 | 2013-08-20 03:13:51 +0000 | [diff] [blame] | 8830 | p->aCounter[SQLITE_STMTSTATUS_VM_STEP] += (int)nVmStep; |
drh | bdaec52 | 2011-04-04 00:14:43 +0000 | [diff] [blame] | 8831 | sqlite3VdbeLeave(p); |
dan | 83f0ab8 | 2016-01-29 18:04:31 +0000 | [diff] [blame] | 8832 | assert( rc!=SQLITE_OK || nExtraDelete==0 |
| 8833 | || sqlite3_strlike("DELETE%",p->zSql,0)!=0 |
| 8834 | ); |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 8835 | return rc; |
| 8836 | |
drh | 023ae03 | 2007-05-08 12:12:16 +0000 | [diff] [blame] | 8837 | /* Jump to here if a string or blob larger than SQLITE_MAX_LENGTH |
| 8838 | ** is encountered. |
| 8839 | */ |
| 8840 | too_big: |
drh | 22c17b8 | 2015-05-15 04:13:15 +0000 | [diff] [blame] | 8841 | sqlite3VdbeError(p, "string or blob too big"); |
drh | 023ae03 | 2007-05-08 12:12:16 +0000 | [diff] [blame] | 8842 | rc = SQLITE_TOOBIG; |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 8843 | goto abort_due_to_error; |
drh | 023ae03 | 2007-05-08 12:12:16 +0000 | [diff] [blame] | 8844 | |
drh | 98640a3 | 2007-06-07 19:08:32 +0000 | [diff] [blame] | 8845 | /* Jump to here if a malloc() fails. |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 8846 | */ |
| 8847 | no_mem: |
drh | 4a642b6 | 2016-02-05 01:55:27 +0000 | [diff] [blame] | 8848 | sqlite3OomFault(db); |
drh | 22c17b8 | 2015-05-15 04:13:15 +0000 | [diff] [blame] | 8849 | sqlite3VdbeError(p, "out of memory"); |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 8850 | rc = SQLITE_NOMEM_BKPT; |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 8851 | goto abort_due_to_error; |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 8852 | |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 8853 | /* Jump to here if the sqlite3_interrupt() API sets the interrupt |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 8854 | ** flag. |
| 8855 | */ |
| 8856 | abort_due_to_interrupt: |
dan | 892edb6 | 2020-03-30 13:35:05 +0000 | [diff] [blame] | 8857 | assert( AtomicLoad(&db->u1.isInterrupted) ); |
drh | 56f1873 | 2020-06-03 15:59:22 +0000 | [diff] [blame] | 8858 | rc = SQLITE_INTERRUPT; |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 8859 | goto abort_due_to_error; |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 8860 | } |