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 | e4c88c0 | 2012-01-04 12:57:45 +0000 | [diff] [blame] | 244 | int iDb, /* Database the cursor belongs to, or -1 */ |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 245 | u8 eCurType /* Type of the new cursor */ |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 246 | ){ |
| 247 | /* 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] | 248 | ** required for this VdbeCursor structure. It is convenient to use a |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 249 | ** vdbe memory cell to manage the memory allocation required for a |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 250 | ** VdbeCursor structure for the following reasons: |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 251 | ** |
| 252 | ** * Sometimes cursor numbers are used for a couple of different |
| 253 | ** purposes in a vdbe program. The different uses might require |
| 254 | ** different sized allocations. Memory cells provide growable |
| 255 | ** allocations. |
| 256 | ** |
| 257 | ** * When using ENABLE_MEMORY_MANAGEMENT, memory cell buffers can |
| 258 | ** be freed lazily via the sqlite3_release_memory() API. This |
| 259 | ** minimizes the number of malloc calls made by the system. |
| 260 | ** |
drh | 3cdce92 | 2016-03-21 00:30:40 +0000 | [diff] [blame] | 261 | ** 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] | 262 | ** the top of the register space. Cursor 1 is at Mem[p->nMem-1]. |
| 263 | ** Cursor 2 is at Mem[p->nMem-2]. And so forth. |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 264 | */ |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 265 | Mem *pMem = iCur>0 ? &p->aMem[p->nMem-iCur] : p->aMem; |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 266 | |
danielk1977 | 5f09613 | 2008-03-28 15:44:09 +0000 | [diff] [blame] | 267 | int nByte; |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 268 | VdbeCursor *pCx = 0; |
danielk1977 | 5f09613 | 2008-03-28 15:44:09 +0000 | [diff] [blame] | 269 | nByte = |
drh | 5cc1023 | 2013-11-21 01:04:02 +0000 | [diff] [blame] | 270 | ROUND8(sizeof(VdbeCursor)) + 2*sizeof(u32)*nField + |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 271 | (eCurType==CURTYPE_BTREE?sqlite3BtreeCursorSize():0); |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 272 | |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 273 | assert( iCur>=0 && iCur<p->nCursor ); |
drh | a3fa140 | 2016-04-29 02:55:05 +0000 | [diff] [blame] | 274 | if( p->apCsr[iCur] ){ /*OPTIMIZATION-IF-FALSE*/ |
dan | 97c8cb3 | 2019-01-01 18:00:17 +0000 | [diff] [blame] | 275 | /* Before calling sqlite3VdbeFreeCursor(), ensure the isEphemeral flag |
| 276 | ** is clear. Otherwise, if this is an ephemeral cursor created by |
| 277 | ** OP_OpenDup, the cursor will not be closed and will still be part |
| 278 | ** of a BtShared.pCursor list. */ |
dan | a512972 | 2019-05-03 18:50:24 +0000 | [diff] [blame] | 279 | if( p->apCsr[iCur]->pBtx==0 ) p->apCsr[iCur]->isEphemeral = 0; |
danielk1977 | be71889 | 2006-06-23 08:05:19 +0000 | [diff] [blame] | 280 | sqlite3VdbeFreeCursor(p, p->apCsr[iCur]); |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 281 | p->apCsr[iCur] = 0; |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 282 | } |
drh | 322f285 | 2014-09-19 00:43:39 +0000 | [diff] [blame] | 283 | if( SQLITE_OK==sqlite3VdbeMemClearAndResize(pMem, nByte) ){ |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 284 | p->apCsr[iCur] = pCx = (VdbeCursor*)pMem->z; |
drh | fbd8cbd | 2016-12-10 12:58:15 +0000 | [diff] [blame] | 285 | memset(pCx, 0, offsetof(VdbeCursor,pAltCursor)); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 286 | pCx->eCurType = eCurType; |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 +0000 | [diff] [blame] | 287 | pCx->iDb = iDb; |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 288 | pCx->nField = nField; |
drh | b53a5a9 | 2014-10-12 22:37:22 +0000 | [diff] [blame] | 289 | pCx->aOffset = &pCx->aType[nField]; |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 290 | if( eCurType==CURTYPE_BTREE ){ |
| 291 | pCx->uc.pCursor = (BtCursor*) |
drh | 5cc1023 | 2013-11-21 01:04:02 +0000 | [diff] [blame] | 292 | &pMem->z[ROUND8(sizeof(VdbeCursor))+2*sizeof(u32)*nField]; |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 293 | sqlite3BtreeCursorZero(pCx->uc.pCursor); |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 294 | } |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 +0000 | [diff] [blame] | 295 | } |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 296 | return pCx; |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 297 | } |
| 298 | |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 299 | /* |
drh | 8a3884e | 2019-05-29 21:18:27 +0000 | [diff] [blame] | 300 | ** The string in pRec is known to look like an integer and to have a |
| 301 | ** floating point value of rValue. Return true and set *piValue to the |
| 302 | ** integer value if the string is in range to be an integer. Otherwise, |
| 303 | ** return false. |
| 304 | */ |
| 305 | static int alsoAnInt(Mem *pRec, double rValue, i64 *piValue){ |
| 306 | i64 iValue = (double)rValue; |
| 307 | if( sqlite3RealSameAsInt(rValue,iValue) ){ |
drh | c285ded | 2019-06-10 18:33:16 +0000 | [diff] [blame] | 308 | *piValue = iValue; |
| 309 | return 1; |
drh | 8a3884e | 2019-05-29 21:18:27 +0000 | [diff] [blame] | 310 | } |
| 311 | return 0==sqlite3Atoi64(pRec->z, piValue, pRec->n, pRec->enc); |
| 312 | } |
| 313 | |
| 314 | /* |
drh | 29d7210 | 2006-02-09 22:13:41 +0000 | [diff] [blame] | 315 | ** Try to convert a value into a numeric representation if we can |
| 316 | ** do so without loss of information. In other words, if the string |
| 317 | ** looks like a number, convert it into a number. If it does not |
| 318 | ** look like a number, leave it alone. |
drh | bd9507c | 2014-08-23 17:21:37 +0000 | [diff] [blame] | 319 | ** |
| 320 | ** If the bTryForInt flag is true, then extra effort is made to give |
| 321 | ** an integer representation. Strings that look like floating point |
| 322 | ** values but which have no fractional component (example: '48.00') |
| 323 | ** will have a MEM_Int representation when bTryForInt is true. |
| 324 | ** |
| 325 | ** If bTryForInt is false, then if the input string contains a decimal |
| 326 | ** point or exponential notation, the result is only MEM_Real, even |
| 327 | ** if there is an exact integer representation of the quantity. |
drh | 29d7210 | 2006-02-09 22:13:41 +0000 | [diff] [blame] | 328 | */ |
drh | bd9507c | 2014-08-23 17:21:37 +0000 | [diff] [blame] | 329 | static void applyNumericAffinity(Mem *pRec, int bTryForInt){ |
drh | 975b4c6 | 2014-07-26 16:47:23 +0000 | [diff] [blame] | 330 | double rValue; |
drh | 975b4c6 | 2014-07-26 16:47:23 +0000 | [diff] [blame] | 331 | u8 enc = pRec->enc; |
drh | 8a3884e | 2019-05-29 21:18:27 +0000 | [diff] [blame] | 332 | int rc; |
drh | 169f077 | 2019-05-02 21:36:26 +0000 | [diff] [blame] | 333 | assert( (pRec->flags & (MEM_Str|MEM_Int|MEM_Real|MEM_IntReal))==MEM_Str ); |
drh | 8a3884e | 2019-05-29 21:18:27 +0000 | [diff] [blame] | 334 | rc = sqlite3AtoF(pRec->z, &rValue, pRec->n, enc); |
drh | 9a27822 | 2019-06-07 22:26:08 +0000 | [diff] [blame] | 335 | if( rc<=0 ) return; |
drh | 8a3884e | 2019-05-29 21:18:27 +0000 | [diff] [blame] | 336 | if( rc==1 && alsoAnInt(pRec, rValue, &pRec->u.i) ){ |
drh | 975b4c6 | 2014-07-26 16:47:23 +0000 | [diff] [blame] | 337 | pRec->flags |= MEM_Int; |
| 338 | }else{ |
drh | 74eaba4 | 2014-09-18 17:52:15 +0000 | [diff] [blame] | 339 | pRec->u.r = rValue; |
drh | 975b4c6 | 2014-07-26 16:47:23 +0000 | [diff] [blame] | 340 | pRec->flags |= MEM_Real; |
drh | bd9507c | 2014-08-23 17:21:37 +0000 | [diff] [blame] | 341 | if( bTryForInt ) sqlite3VdbeIntegerAffinity(pRec); |
drh | 29d7210 | 2006-02-09 22:13:41 +0000 | [diff] [blame] | 342 | } |
drh | 06b3bd5 | 2018-02-01 01:13:33 +0000 | [diff] [blame] | 343 | /* TEXT->NUMERIC is many->one. Hence, it is important to invalidate the |
| 344 | ** string representation after computing a numeric equivalent, because the |
| 345 | ** string representation might not be the canonical representation for the |
| 346 | ** numeric value. Ticket [343634942dd54ab57b7024] 2018-01-31. */ |
| 347 | pRec->flags &= ~MEM_Str; |
drh | 29d7210 | 2006-02-09 22:13:41 +0000 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | /* |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 351 | ** Processing is determine by the affinity parameter: |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 352 | ** |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 353 | ** SQLITE_AFF_INTEGER: |
| 354 | ** SQLITE_AFF_REAL: |
| 355 | ** SQLITE_AFF_NUMERIC: |
| 356 | ** Try to convert pRec to an integer representation or a |
| 357 | ** floating-point representation if an integer representation |
| 358 | ** is not possible. Note that the integer representation is |
| 359 | ** always preferred, even if the affinity is REAL, because |
| 360 | ** an integer representation is more space efficient on disk. |
| 361 | ** |
| 362 | ** SQLITE_AFF_TEXT: |
| 363 | ** Convert pRec to a text representation. |
| 364 | ** |
drh | 05883a3 | 2015-06-02 15:32:08 +0000 | [diff] [blame] | 365 | ** SQLITE_AFF_BLOB: |
drh | 96fb16e | 2019-08-06 14:37:24 +0000 | [diff] [blame] | 366 | ** SQLITE_AFF_NONE: |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 367 | ** No-op. pRec is unchanged. |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 368 | */ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 369 | static void applyAffinity( |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 370 | Mem *pRec, /* The value to apply affinity to */ |
| 371 | char affinity, /* The affinity to be applied */ |
| 372 | u8 enc /* Use this text encoding */ |
| 373 | ){ |
drh | 7ea31cc | 2014-09-18 14:36:00 +0000 | [diff] [blame] | 374 | if( affinity>=SQLITE_AFF_NUMERIC ){ |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 375 | assert( affinity==SQLITE_AFF_INTEGER || affinity==SQLITE_AFF_REAL |
| 376 | || affinity==SQLITE_AFF_NUMERIC ); |
drh | a3fa140 | 2016-04-29 02:55:05 +0000 | [diff] [blame] | 377 | if( (pRec->flags & MEM_Int)==0 ){ /*OPTIMIZATION-IF-FALSE*/ |
drh | bd9507c | 2014-08-23 17:21:37 +0000 | [diff] [blame] | 378 | if( (pRec->flags & MEM_Real)==0 ){ |
drh | 11a6eee | 2014-09-19 22:01:54 +0000 | [diff] [blame] | 379 | if( pRec->flags & MEM_Str ) applyNumericAffinity(pRec,1); |
drh | bd9507c | 2014-08-23 17:21:37 +0000 | [diff] [blame] | 380 | }else{ |
| 381 | sqlite3VdbeIntegerAffinity(pRec); |
| 382 | } |
drh | 17c4029 | 2004-07-21 02:53:29 +0000 | [diff] [blame] | 383 | } |
drh | 7ea31cc | 2014-09-18 14:36:00 +0000 | [diff] [blame] | 384 | }else if( affinity==SQLITE_AFF_TEXT ){ |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 385 | /* Only attempt the conversion to TEXT if there is an integer or real |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 386 | ** representation (blob and NULL do not get converted) but no string |
drh | a3fa140 | 2016-04-29 02:55:05 +0000 | [diff] [blame] | 387 | ** representation. It would be harmless to repeat the conversion if |
| 388 | ** there is already a string rep, but it is pointless to waste those |
| 389 | ** CPU cycles. */ |
| 390 | if( 0==(pRec->flags&MEM_Str) ){ /*OPTIMIZATION-IF-FALSE*/ |
drh | 169f077 | 2019-05-02 21:36:26 +0000 | [diff] [blame] | 391 | if( (pRec->flags&(MEM_Real|MEM_Int|MEM_IntReal)) ){ |
drh | 3242c69 | 2019-05-04 01:29:13 +0000 | [diff] [blame] | 392 | testcase( pRec->flags & MEM_Int ); |
| 393 | testcase( pRec->flags & MEM_Real ); |
| 394 | testcase( pRec->flags & MEM_IntReal ); |
drh | a3fa140 | 2016-04-29 02:55:05 +0000 | [diff] [blame] | 395 | sqlite3VdbeMemStringify(pRec, enc, 1); |
| 396 | } |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 397 | } |
drh | 169f077 | 2019-05-02 21:36:26 +0000 | [diff] [blame] | 398 | pRec->flags &= ~(MEM_Real|MEM_Int|MEM_IntReal); |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 399 | } |
| 400 | } |
| 401 | |
danielk1977 | aee18ef | 2005-03-09 12:26:50 +0000 | [diff] [blame] | 402 | /* |
drh | 29d7210 | 2006-02-09 22:13:41 +0000 | [diff] [blame] | 403 | ** Try to convert the type of a function argument or a result column |
| 404 | ** into a numeric representation. Use either INTEGER or REAL whichever |
| 405 | ** is appropriate. But only do the conversion if it is possible without |
| 406 | ** loss of information and return the revised type of the argument. |
drh | 29d7210 | 2006-02-09 22:13:41 +0000 | [diff] [blame] | 407 | */ |
| 408 | int sqlite3_value_numeric_type(sqlite3_value *pVal){ |
drh | 1b27b8c | 2014-02-10 03:21:57 +0000 | [diff] [blame] | 409 | int eType = sqlite3_value_type(pVal); |
| 410 | if( eType==SQLITE_TEXT ){ |
| 411 | Mem *pMem = (Mem*)pVal; |
drh | bd9507c | 2014-08-23 17:21:37 +0000 | [diff] [blame] | 412 | applyNumericAffinity(pMem, 0); |
drh | 1b27b8c | 2014-02-10 03:21:57 +0000 | [diff] [blame] | 413 | eType = sqlite3_value_type(pVal); |
drh | e5a8a1d | 2010-11-18 12:31:24 +0000 | [diff] [blame] | 414 | } |
drh | 1b27b8c | 2014-02-10 03:21:57 +0000 | [diff] [blame] | 415 | return eType; |
drh | 29d7210 | 2006-02-09 22:13:41 +0000 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | /* |
danielk1977 | aee18ef | 2005-03-09 12:26:50 +0000 | [diff] [blame] | 419 | ** Exported version of applyAffinity(). This one works on sqlite3_value*, |
| 420 | ** not the internal Mem* type. |
| 421 | */ |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 422 | void sqlite3ValueApplyAffinity( |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 423 | sqlite3_value *pVal, |
| 424 | u8 affinity, |
| 425 | u8 enc |
| 426 | ){ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 427 | applyAffinity((Mem *)pVal, affinity, enc); |
danielk1977 | aee18ef | 2005-03-09 12:26:50 +0000 | [diff] [blame] | 428 | } |
| 429 | |
drh | 3d1d90a | 2014-03-24 15:00:15 +0000 | [diff] [blame] | 430 | /* |
drh | f1a89ed | 2014-08-23 17:41:15 +0000 | [diff] [blame] | 431 | ** pMem currently only holds a string type (or maybe a BLOB that we can |
| 432 | ** interpret as a string if we want to). Compute its corresponding |
drh | 74eaba4 | 2014-09-18 17:52:15 +0000 | [diff] [blame] | 433 | ** 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] | 434 | ** accordingly. |
| 435 | */ |
| 436 | static u16 SQLITE_NOINLINE computeNumericType(Mem *pMem){ |
drh | 9a27822 | 2019-06-07 22:26:08 +0000 | [diff] [blame] | 437 | int rc; |
| 438 | sqlite3_int64 ix; |
drh | 169f077 | 2019-05-02 21:36:26 +0000 | [diff] [blame] | 439 | assert( (pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal))==0 ); |
drh | f1a89ed | 2014-08-23 17:41:15 +0000 | [diff] [blame] | 440 | assert( (pMem->flags & (MEM_Str|MEM_Blob))!=0 ); |
drh | 0814acd | 2019-01-25 20:09:04 +0000 | [diff] [blame] | 441 | ExpandBlob(pMem); |
drh | 9a27822 | 2019-06-07 22:26:08 +0000 | [diff] [blame] | 442 | rc = sqlite3AtoF(pMem->z, &pMem->u.r, pMem->n, pMem->enc); |
| 443 | if( rc<=0 ){ |
| 444 | if( rc==0 && sqlite3Atoi64(pMem->z, &ix, pMem->n, pMem->enc)<=1 ){ |
| 445 | pMem->u.i = ix; |
| 446 | return MEM_Int; |
| 447 | }else{ |
| 448 | return MEM_Real; |
| 449 | } |
| 450 | }else if( rc==1 && sqlite3Atoi64(pMem->z, &ix, pMem->n, pMem->enc)==0 ){ |
| 451 | pMem->u.i = ix; |
drh | f1a89ed | 2014-08-23 17:41:15 +0000 | [diff] [blame] | 452 | return MEM_Int; |
| 453 | } |
| 454 | return MEM_Real; |
| 455 | } |
| 456 | |
| 457 | /* |
drh | 3d1d90a | 2014-03-24 15:00:15 +0000 | [diff] [blame] | 458 | ** Return the numeric type for pMem, either MEM_Int or MEM_Real or both or |
| 459 | ** none. |
| 460 | ** |
| 461 | ** Unlike applyNumericAffinity(), this routine does not modify pMem->flags. |
drh | 74eaba4 | 2014-09-18 17:52:15 +0000 | [diff] [blame] | 462 | ** But it does set pMem->u.r and pMem->u.i appropriately. |
drh | 3d1d90a | 2014-03-24 15:00:15 +0000 | [diff] [blame] | 463 | */ |
| 464 | static u16 numericType(Mem *pMem){ |
drh | 169f077 | 2019-05-02 21:36:26 +0000 | [diff] [blame] | 465 | if( pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal) ){ |
drh | 3242c69 | 2019-05-04 01:29:13 +0000 | [diff] [blame] | 466 | testcase( pMem->flags & MEM_Int ); |
| 467 | testcase( pMem->flags & MEM_Real ); |
| 468 | testcase( pMem->flags & MEM_IntReal ); |
drh | 169f077 | 2019-05-02 21:36:26 +0000 | [diff] [blame] | 469 | return pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal); |
drh | 3d1d90a | 2014-03-24 15:00:15 +0000 | [diff] [blame] | 470 | } |
| 471 | if( pMem->flags & (MEM_Str|MEM_Blob) ){ |
drh | 3242c69 | 2019-05-04 01:29:13 +0000 | [diff] [blame] | 472 | testcase( pMem->flags & MEM_Str ); |
| 473 | testcase( pMem->flags & MEM_Blob ); |
drh | f1a89ed | 2014-08-23 17:41:15 +0000 | [diff] [blame] | 474 | return computeNumericType(pMem); |
drh | 3d1d90a | 2014-03-24 15:00:15 +0000 | [diff] [blame] | 475 | } |
| 476 | return 0; |
| 477 | } |
| 478 | |
danielk1977 | b5402fb | 2005-01-12 07:15:04 +0000 | [diff] [blame] | 479 | #ifdef SQLITE_DEBUG |
drh | b6f5452 | 2004-05-20 02:42:16 +0000 | [diff] [blame] | 480 | /* |
danielk1977 | ca6b291 | 2004-05-21 10:49:47 +0000 | [diff] [blame] | 481 | ** Write a nice string representation of the contents of cell pMem |
| 482 | ** into buffer zBuf, length nBuf. |
| 483 | */ |
drh | 5ca0632 | 2020-01-06 19:23:41 +0000 | [diff] [blame] | 484 | void sqlite3VdbeMemPrettyPrint(Mem *pMem, StrAccum *pStr){ |
danielk1977 | ca6b291 | 2004-05-21 10:49:47 +0000 | [diff] [blame] | 485 | int f = pMem->flags; |
drh | 5719628 | 2004-10-06 15:41:16 +0000 | [diff] [blame] | 486 | static const char *const encnames[] = {"(X)", "(8)", "(16LE)", "(16BE)"}; |
danielk1977 | ca6b291 | 2004-05-21 10:49:47 +0000 | [diff] [blame] | 487 | if( f&MEM_Blob ){ |
| 488 | int i; |
| 489 | char c; |
| 490 | if( f & MEM_Dyn ){ |
| 491 | c = 'z'; |
| 492 | assert( (f & (MEM_Static|MEM_Ephem))==0 ); |
| 493 | }else if( f & MEM_Static ){ |
| 494 | c = 't'; |
| 495 | assert( (f & (MEM_Dyn|MEM_Ephem))==0 ); |
| 496 | }else if( f & MEM_Ephem ){ |
| 497 | c = 'e'; |
| 498 | assert( (f & (MEM_Static|MEM_Dyn))==0 ); |
| 499 | }else{ |
| 500 | c = 's'; |
| 501 | } |
drh | ded33cc | 2020-01-08 11:36:30 +0000 | [diff] [blame] | 502 | sqlite3_str_appendf(pStr, "%cx[", c); |
drh | efb5f9a | 2019-08-30 21:52:13 +0000 | [diff] [blame] | 503 | for(i=0; i<25 && i<pMem->n; i++){ |
drh | 5ca0632 | 2020-01-06 19:23:41 +0000 | [diff] [blame] | 504 | sqlite3_str_appendf(pStr, "%02X", ((int)pMem->z[i] & 0xFF)); |
danielk1977 | ca6b291 | 2004-05-21 10:49:47 +0000 | [diff] [blame] | 505 | } |
drh | 5ca0632 | 2020-01-06 19:23:41 +0000 | [diff] [blame] | 506 | sqlite3_str_appendf(pStr, "|"); |
drh | efb5f9a | 2019-08-30 21:52:13 +0000 | [diff] [blame] | 507 | for(i=0; i<25 && i<pMem->n; i++){ |
danielk1977 | ca6b291 | 2004-05-21 10:49:47 +0000 | [diff] [blame] | 508 | char z = pMem->z[i]; |
drh | 5ca0632 | 2020-01-06 19:23:41 +0000 | [diff] [blame] | 509 | sqlite3_str_appendchar(pStr, 1, (z<32||z>126)?'.':z); |
danielk1977 | ca6b291 | 2004-05-21 10:49:47 +0000 | [diff] [blame] | 510 | } |
drh | 5ca0632 | 2020-01-06 19:23:41 +0000 | [diff] [blame] | 511 | sqlite3_str_appendf(pStr,"]"); |
drh | fdf972a | 2007-05-02 13:30:27 +0000 | [diff] [blame] | 512 | if( f & MEM_Zero ){ |
drh | 5ca0632 | 2020-01-06 19:23:41 +0000 | [diff] [blame] | 513 | sqlite3_str_appendf(pStr, "+%dz",pMem->u.nZero); |
drh | fdf972a | 2007-05-02 13:30:27 +0000 | [diff] [blame] | 514 | } |
danielk1977 | b1bc953 | 2004-05-22 03:05:33 +0000 | [diff] [blame] | 515 | }else if( f & MEM_Str ){ |
drh | 5ca0632 | 2020-01-06 19:23:41 +0000 | [diff] [blame] | 516 | int j; |
mistachkin | 5917117 | 2020-01-18 19:02:20 +0000 | [diff] [blame] | 517 | u8 c; |
danielk1977 | b1bc953 | 2004-05-22 03:05:33 +0000 | [diff] [blame] | 518 | if( f & MEM_Dyn ){ |
drh | 5ca0632 | 2020-01-06 19:23:41 +0000 | [diff] [blame] | 519 | c = 'z'; |
danielk1977 | b1bc953 | 2004-05-22 03:05:33 +0000 | [diff] [blame] | 520 | assert( (f & (MEM_Static|MEM_Ephem))==0 ); |
| 521 | }else if( f & MEM_Static ){ |
drh | 5ca0632 | 2020-01-06 19:23:41 +0000 | [diff] [blame] | 522 | c = 't'; |
danielk1977 | b1bc953 | 2004-05-22 03:05:33 +0000 | [diff] [blame] | 523 | assert( (f & (MEM_Dyn|MEM_Ephem))==0 ); |
| 524 | }else if( f & MEM_Ephem ){ |
drh | 5ca0632 | 2020-01-06 19:23:41 +0000 | [diff] [blame] | 525 | c = 'e'; |
danielk1977 | b1bc953 | 2004-05-22 03:05:33 +0000 | [diff] [blame] | 526 | assert( (f & (MEM_Static|MEM_Dyn))==0 ); |
| 527 | }else{ |
drh | 5ca0632 | 2020-01-06 19:23:41 +0000 | [diff] [blame] | 528 | c = 's'; |
danielk1977 | b1bc953 | 2004-05-22 03:05:33 +0000 | [diff] [blame] | 529 | } |
drh | 5ca0632 | 2020-01-06 19:23:41 +0000 | [diff] [blame] | 530 | sqlite3_str_appendf(pStr, " %c%d[", c, pMem->n); |
drh | efb5f9a | 2019-08-30 21:52:13 +0000 | [diff] [blame] | 531 | for(j=0; j<25 && j<pMem->n; j++){ |
mistachkin | 5917117 | 2020-01-18 19:02:20 +0000 | [diff] [blame] | 532 | c = pMem->z[j]; |
drh | 5ca0632 | 2020-01-06 19:23:41 +0000 | [diff] [blame] | 533 | sqlite3_str_appendchar(pStr, 1, (c>=0x20&&c<=0x7f) ? c : '.'); |
danielk1977 | b1bc953 | 2004-05-22 03:05:33 +0000 | [diff] [blame] | 534 | } |
drh | 5ca0632 | 2020-01-06 19:23:41 +0000 | [diff] [blame] | 535 | sqlite3_str_appendf(pStr, "]%s", encnames[pMem->enc]); |
danielk1977 | ca6b291 | 2004-05-21 10:49:47 +0000 | [diff] [blame] | 536 | } |
danielk1977 | ca6b291 | 2004-05-21 10:49:47 +0000 | [diff] [blame] | 537 | } |
| 538 | #endif |
| 539 | |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 540 | #ifdef SQLITE_DEBUG |
| 541 | /* |
| 542 | ** Print the value of a register for tracing purposes: |
| 543 | */ |
drh | 84e55a8 | 2013-11-13 17:58:23 +0000 | [diff] [blame] | 544 | static void memTracePrint(Mem *p){ |
drh | a5750cf | 2014-02-07 13:20:31 +0000 | [diff] [blame] | 545 | if( p->flags & MEM_Undefined ){ |
drh | 84e55a8 | 2013-11-13 17:58:23 +0000 | [diff] [blame] | 546 | printf(" undefined"); |
drh | 953f761 | 2012-12-07 22:18:54 +0000 | [diff] [blame] | 547 | }else if( p->flags & MEM_Null ){ |
drh | ce2fbd1 | 2018-01-12 21:00:14 +0000 | [diff] [blame] | 548 | printf(p->flags & MEM_Zero ? " NULL-nochng" : " NULL"); |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 549 | }else if( (p->flags & (MEM_Int|MEM_Str))==(MEM_Int|MEM_Str) ){ |
drh | 84e55a8 | 2013-11-13 17:58:23 +0000 | [diff] [blame] | 550 | printf(" si:%lld", p->u.i); |
drh | 169f077 | 2019-05-02 21:36:26 +0000 | [diff] [blame] | 551 | }else if( (p->flags & (MEM_IntReal))!=0 ){ |
drh | 83a1daf | 2019-05-01 18:59:33 +0000 | [diff] [blame] | 552 | printf(" ir:%lld", p->u.i); |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 553 | }else if( p->flags & MEM_Int ){ |
drh | 84e55a8 | 2013-11-13 17:58:23 +0000 | [diff] [blame] | 554 | printf(" i:%lld", p->u.i); |
drh | 0b3bf92 | 2009-06-15 20:45:34 +0000 | [diff] [blame] | 555 | #ifndef SQLITE_OMIT_FLOATING_POINT |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 556 | }else if( p->flags & MEM_Real ){ |
drh | d1c472d | 2019-10-03 14:51:59 +0000 | [diff] [blame] | 557 | printf(" r:%.17g", p->u.r); |
drh | 0b3bf92 | 2009-06-15 20:45:34 +0000 | [diff] [blame] | 558 | #endif |
drh | 9d67afc | 2018-08-29 20:24:03 +0000 | [diff] [blame] | 559 | }else if( sqlite3VdbeMemIsRowSet(p) ){ |
drh | 84e55a8 | 2013-11-13 17:58:23 +0000 | [diff] [blame] | 560 | printf(" (rowset)"); |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 561 | }else{ |
drh | 5ca0632 | 2020-01-06 19:23:41 +0000 | [diff] [blame] | 562 | StrAccum acc; |
| 563 | char zBuf[1000]; |
| 564 | sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0); |
| 565 | sqlite3VdbeMemPrettyPrint(p, &acc); |
| 566 | printf(" %s", sqlite3StrAccumFinish(&acc)); |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 567 | } |
dan | 5b6c8e4 | 2016-01-30 15:46:03 +0000 | [diff] [blame] | 568 | if( p->flags & MEM_Subtype ) printf(" subtype=0x%02x", p->eSubtype); |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 569 | } |
drh | 84e55a8 | 2013-11-13 17:58:23 +0000 | [diff] [blame] | 570 | static void registerTrace(int iReg, Mem *p){ |
drh | 22e95fb | 2020-01-02 14:42:42 +0000 | [diff] [blame] | 571 | printf("R[%d] = ", iReg); |
drh | 84e55a8 | 2013-11-13 17:58:23 +0000 | [diff] [blame] | 572 | memTracePrint(p); |
drh | 22e95fb | 2020-01-02 14:42:42 +0000 | [diff] [blame] | 573 | if( p->pScopyFrom ){ |
| 574 | printf(" <== R[%d]", (int)(p->pScopyFrom - &p[-iReg])); |
| 575 | } |
drh | 84e55a8 | 2013-11-13 17:58:23 +0000 | [diff] [blame] | 576 | printf("\n"); |
drh | e2bc655 | 2017-04-17 20:50:34 +0000 | [diff] [blame] | 577 | sqlite3VdbeCheckMemInvariants(p); |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 578 | } |
| 579 | #endif |
| 580 | |
| 581 | #ifdef SQLITE_DEBUG |
drh | 22e95fb | 2020-01-02 14:42:42 +0000 | [diff] [blame] | 582 | /* |
| 583 | ** Show the values of all registers in the virtual machine. Used for |
| 584 | ** interactive debugging. |
| 585 | */ |
| 586 | void sqlite3VdbeRegisterDump(Vdbe *v){ |
| 587 | int i; |
| 588 | for(i=1; i<v->nMem; i++) registerTrace(i, v->aMem+i); |
| 589 | } |
| 590 | #endif /* SQLITE_DEBUG */ |
| 591 | |
| 592 | |
| 593 | #ifdef SQLITE_DEBUG |
drh | 84e55a8 | 2013-11-13 17:58:23 +0000 | [diff] [blame] | 594 | # define REGISTER_TRACE(R,M) if(db->flags&SQLITE_VdbeTrace)registerTrace(R,M) |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 595 | #else |
| 596 | # define REGISTER_TRACE(R,M) |
| 597 | #endif |
| 598 | |
danielk1977 | 84ac9d0 | 2004-05-18 09:58:06 +0000 | [diff] [blame] | 599 | |
drh | 7b39686 | 2003-01-01 23:06:20 +0000 | [diff] [blame] | 600 | #ifdef VDBE_PROFILE |
shane | 9bcbdad | 2008-05-29 20:22:37 +0000 | [diff] [blame] | 601 | |
| 602 | /* |
| 603 | ** hwtime.h contains inline assembler code for implementing |
| 604 | ** high-performance timing routines. |
drh | 7b39686 | 2003-01-01 23:06:20 +0000 | [diff] [blame] | 605 | */ |
shane | 9bcbdad | 2008-05-29 20:22:37 +0000 | [diff] [blame] | 606 | #include "hwtime.h" |
| 607 | |
drh | 7b39686 | 2003-01-01 23:06:20 +0000 | [diff] [blame] | 608 | #endif |
| 609 | |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 610 | #ifndef NDEBUG |
| 611 | /* |
| 612 | ** This function is only called from within an assert() expression. It |
| 613 | ** checks that the sqlite3.nTransaction variable is correctly set to |
| 614 | ** the number of non-transaction savepoints currently in the |
| 615 | ** linked list starting at sqlite3.pSavepoint. |
| 616 | ** |
| 617 | ** Usage: |
| 618 | ** |
| 619 | ** assert( checkSavepointCount(db) ); |
| 620 | */ |
| 621 | static int checkSavepointCount(sqlite3 *db){ |
| 622 | int n = 0; |
| 623 | Savepoint *p; |
| 624 | for(p=db->pSavepoint; p; p=p->pNext) n++; |
| 625 | assert( n==(db->nSavepoint + db->isTransactionSavepoint) ); |
| 626 | return 1; |
| 627 | } |
| 628 | #endif |
| 629 | |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 630 | /* |
| 631 | ** Return the register of pOp->p2 after first preparing it to be |
| 632 | ** overwritten with an integer value. |
drh | 9eef8c6 | 2015-10-15 17:31:41 +0000 | [diff] [blame] | 633 | */ |
| 634 | static SQLITE_NOINLINE Mem *out2PrereleaseWithClear(Mem *pOut){ |
| 635 | sqlite3VdbeMemSetNull(pOut); |
| 636 | pOut->flags = MEM_Int; |
| 637 | return pOut; |
| 638 | } |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 639 | static Mem *out2Prerelease(Vdbe *p, VdbeOp *pOp){ |
| 640 | Mem *pOut; |
| 641 | assert( pOp->p2>0 ); |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 642 | assert( pOp->p2<=(p->nMem+1 - p->nCursor) ); |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 643 | pOut = &p->aMem[pOp->p2]; |
| 644 | memAboutToChange(p, pOut); |
drh | a3fa140 | 2016-04-29 02:55:05 +0000 | [diff] [blame] | 645 | if( VdbeMemDynamic(pOut) ){ /*OPTIMIZATION-IF-FALSE*/ |
drh | 9eef8c6 | 2015-10-15 17:31:41 +0000 | [diff] [blame] | 646 | return out2PrereleaseWithClear(pOut); |
| 647 | }else{ |
| 648 | pOut->flags = MEM_Int; |
| 649 | return pOut; |
| 650 | } |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 651 | } |
| 652 | |
drh | b975598 | 2010-07-24 16:34:37 +0000 | [diff] [blame] | 653 | |
| 654 | /* |
drh | 0fd6135 | 2014-02-07 02:29:45 +0000 | [diff] [blame] | 655 | ** Execute as much of a VDBE program as we can. |
| 656 | ** This is the core of sqlite3_step(). |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 657 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 658 | int sqlite3VdbeExec( |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 659 | Vdbe *p /* The VDBE */ |
| 660 | ){ |
drh | bbe879d | 2009-11-14 18:04:35 +0000 | [diff] [blame] | 661 | Op *aOp = p->aOp; /* Copy of p->aOp */ |
mistachkin | 5f7b95f | 2017-02-01 23:03:54 +0000 | [diff] [blame] | 662 | Op *pOp = aOp; /* Current operation */ |
drh | 6dc4148 | 2015-04-16 17:31:02 +0000 | [diff] [blame] | 663 | #if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE) |
| 664 | Op *pOrigOp; /* Value of pOp at the top of the loop */ |
| 665 | #endif |
drh | b89aeb6 | 2016-01-27 15:49:32 +0000 | [diff] [blame] | 666 | #ifdef SQLITE_DEBUG |
drh | def19e3 | 2016-01-27 16:26:25 +0000 | [diff] [blame] | 667 | int nExtraDelete = 0; /* Verifies FORDELETE and AUXDELETE flags */ |
drh | b89aeb6 | 2016-01-27 15:49:32 +0000 | [diff] [blame] | 668 | #endif |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 669 | int rc = SQLITE_OK; /* Value to return */ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 670 | sqlite3 *db = p->db; /* The database */ |
drh | cdf011d | 2011-04-04 21:25:28 +0000 | [diff] [blame] | 671 | u8 resetSchemaOnFault = 0; /* Reset schema after an error if positive */ |
drh | 8079a0d | 2006-01-12 17:20:50 +0000 | [diff] [blame] | 672 | u8 encoding = ENC(db); /* The database encoding */ |
drh | 0f825a7 | 2016-08-13 14:17:02 +0000 | [diff] [blame] | 673 | int iCompare = 0; /* Result of last comparison */ |
drh | bf159fa | 2013-06-25 22:01:22 +0000 | [diff] [blame] | 674 | unsigned nVmStep = 0; /* Number of virtual machine steps */ |
drh | 49afe3a | 2013-07-10 03:05:14 +0000 | [diff] [blame] | 675 | #ifndef SQLITE_OMIT_PROGRESS_CALLBACK |
drh | 2ab792e | 2017-05-30 18:34:07 +0000 | [diff] [blame] | 676 | unsigned nProgressLimit; /* Invoke xProgress() when nVmStep reaches this */ |
drh | 49afe3a | 2013-07-10 03:05:14 +0000 | [diff] [blame] | 677 | #endif |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 678 | Mem *aMem = p->aMem; /* Copy of p->aMem */ |
drh | b27b7f5 | 2008-12-10 18:03:45 +0000 | [diff] [blame] | 679 | Mem *pIn1 = 0; /* 1st input operand */ |
| 680 | Mem *pIn2 = 0; /* 2nd input operand */ |
| 681 | Mem *pIn3 = 0; /* 3rd input operand */ |
| 682 | Mem *pOut = 0; /* Output operand */ |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 683 | #ifdef VDBE_PROFILE |
shane | 9bcbdad | 2008-05-29 20:22:37 +0000 | [diff] [blame] | 684 | u64 start; /* CPU clock count at start of opcode */ |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 685 | #endif |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 686 | /*** INSERT STACK UNION HERE ***/ |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 687 | |
drh | ca48c90 | 2008-01-18 14:08:24 +0000 | [diff] [blame] | 688 | assert( p->magic==VDBE_MAGIC_RUN ); /* sqlite3_step() verifies this */ |
drh | bdaec52 | 2011-04-04 00:14:43 +0000 | [diff] [blame] | 689 | sqlite3VdbeEnter(p); |
drh | 82642f8 | 2019-02-12 22:58:32 +0000 | [diff] [blame] | 690 | #ifndef SQLITE_OMIT_PROGRESS_CALLBACK |
| 691 | if( db->xProgress ){ |
| 692 | u32 iPrior = p->aCounter[SQLITE_STMTSTATUS_VM_STEP]; |
| 693 | assert( 0 < db->nProgressOps ); |
| 694 | nProgressLimit = db->nProgressOps - (iPrior % db->nProgressOps); |
| 695 | }else{ |
| 696 | nProgressLimit = 0xffffffff; |
| 697 | } |
| 698 | #endif |
danielk1977 | 2e588c7 | 2005-12-09 14:25:08 +0000 | [diff] [blame] | 699 | if( p->rc==SQLITE_NOMEM ){ |
| 700 | /* This happens if a malloc() inside a call to sqlite3_column_text() or |
| 701 | ** sqlite3_column_text16() failed. */ |
| 702 | goto no_mem; |
| 703 | } |
drh | cbd8db3 | 2015-08-20 17:18:32 +0000 | [diff] [blame] | 704 | assert( p->rc==SQLITE_OK || (p->rc&0xff)==SQLITE_BUSY ); |
drh | 1713afb | 2013-06-28 01:24:57 +0000 | [diff] [blame] | 705 | assert( p->bIsReader || p->readOnly!=0 ); |
drh | 95a7b3e | 2013-09-16 12:57:19 +0000 | [diff] [blame] | 706 | p->iCurrentTime = 0; |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 707 | assert( p->explain==0 ); |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 708 | p->pResultSet = 0; |
drh | a4afb65 | 2005-07-09 02:16:02 +0000 | [diff] [blame] | 709 | db->busyHandler.nBusy = 0; |
dan | 892edb6 | 2020-03-30 13:35:05 +0000 | [diff] [blame] | 710 | if( AtomicLoad(&db->u1.isInterrupted) ) goto abort_due_to_interrupt; |
drh | 602c237 | 2007-03-01 00:29:13 +0000 | [diff] [blame] | 711 | sqlite3VdbeIOTraceSql(p); |
drh | 3c23a88 | 2007-01-09 14:01:13 +0000 | [diff] [blame] | 712 | #ifdef SQLITE_DEBUG |
danielk1977 | 2d1d86f | 2008-06-20 14:59:51 +0000 | [diff] [blame] | 713 | sqlite3BeginBenignMalloc(); |
drh | 84e55a8 | 2013-11-13 17:58:23 +0000 | [diff] [blame] | 714 | if( p->pc==0 |
| 715 | && (p->db->flags & (SQLITE_VdbeListing|SQLITE_VdbeEQP|SQLITE_VdbeTrace))!=0 |
| 716 | ){ |
drh | 3c23a88 | 2007-01-09 14:01:13 +0000 | [diff] [blame] | 717 | int i; |
drh | 84e55a8 | 2013-11-13 17:58:23 +0000 | [diff] [blame] | 718 | int once = 1; |
drh | 3c23a88 | 2007-01-09 14:01:13 +0000 | [diff] [blame] | 719 | sqlite3VdbePrintSql(p); |
drh | 84e55a8 | 2013-11-13 17:58:23 +0000 | [diff] [blame] | 720 | if( p->db->flags & SQLITE_VdbeListing ){ |
| 721 | printf("VDBE Program Listing:\n"); |
| 722 | for(i=0; i<p->nOp; i++){ |
| 723 | sqlite3VdbePrintOp(stdout, i, &aOp[i]); |
| 724 | } |
drh | 3c23a88 | 2007-01-09 14:01:13 +0000 | [diff] [blame] | 725 | } |
drh | 84e55a8 | 2013-11-13 17:58:23 +0000 | [diff] [blame] | 726 | if( p->db->flags & SQLITE_VdbeEQP ){ |
| 727 | for(i=0; i<p->nOp; i++){ |
| 728 | if( aOp[i].opcode==OP_Explain ){ |
| 729 | if( once ) printf("VDBE Query Plan:\n"); |
| 730 | printf("%s\n", aOp[i].p4.z); |
| 731 | once = 0; |
| 732 | } |
| 733 | } |
| 734 | } |
| 735 | if( p->db->flags & SQLITE_VdbeTrace ) printf("VDBE Trace:\n"); |
drh | 3c23a88 | 2007-01-09 14:01:13 +0000 | [diff] [blame] | 736 | } |
danielk1977 | 2d1d86f | 2008-06-20 14:59:51 +0000 | [diff] [blame] | 737 | sqlite3EndBenignMalloc(); |
drh | 3c23a88 | 2007-01-09 14:01:13 +0000 | [diff] [blame] | 738 | #endif |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 739 | for(pOp=&aOp[p->pc]; 1; pOp++){ |
| 740 | /* Errors are detected by individual opcodes, with an immediate |
| 741 | ** jumps to abort_due_to_error. */ |
| 742 | assert( rc==SQLITE_OK ); |
| 743 | |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 744 | assert( pOp>=aOp && pOp<&aOp[p->nOp]); |
drh | 7b39686 | 2003-01-01 23:06:20 +0000 | [diff] [blame] | 745 | #ifdef VDBE_PROFILE |
drh | 35043cc | 2018-02-12 20:27:34 +0000 | [diff] [blame] | 746 | start = sqlite3NProfileCnt ? sqlite3NProfileCnt : sqlite3Hwtime(); |
drh | 7b39686 | 2003-01-01 23:06:20 +0000 | [diff] [blame] | 747 | #endif |
drh | bf159fa | 2013-06-25 22:01:22 +0000 | [diff] [blame] | 748 | nVmStep++; |
dan | 6f9702e | 2014-11-01 20:38:06 +0000 | [diff] [blame] | 749 | #ifdef SQLITE_ENABLE_STMT_SCANSTATUS |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 750 | if( p->anExec ) p->anExec[(int)(pOp-aOp)]++; |
dan | 6f9702e | 2014-11-01 20:38:06 +0000 | [diff] [blame] | 751 | #endif |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 752 | |
danielk1977 | 8b60e0f | 2005-01-12 09:10:39 +0000 | [diff] [blame] | 753 | /* Only allow tracing if SQLITE_DEBUG is defined. |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 754 | */ |
danielk1977 | 8b60e0f | 2005-01-12 09:10:39 +0000 | [diff] [blame] | 755 | #ifdef SQLITE_DEBUG |
drh | 84e55a8 | 2013-11-13 17:58:23 +0000 | [diff] [blame] | 756 | if( db->flags & SQLITE_VdbeTrace ){ |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 757 | sqlite3VdbePrintOp(stdout, (int)(pOp - aOp), pOp); |
drh | 22e95fb | 2020-01-02 14:42:42 +0000 | [diff] [blame] | 758 | test_trace_breakpoint((int)(pOp - aOp),pOp,p); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 759 | } |
drh | 3f7d4e4 | 2004-07-24 14:35:58 +0000 | [diff] [blame] | 760 | #endif |
| 761 | |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 762 | |
drh | f603871 | 2004-02-08 18:07:34 +0000 | [diff] [blame] | 763 | /* Check to see if we need to simulate an interrupt. This only happens |
| 764 | ** if we have a special test build. |
| 765 | */ |
| 766 | #ifdef SQLITE_TEST |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 767 | if( sqlite3_interrupt_count>0 ){ |
| 768 | sqlite3_interrupt_count--; |
| 769 | if( sqlite3_interrupt_count==0 ){ |
| 770 | sqlite3_interrupt(db); |
drh | f603871 | 2004-02-08 18:07:34 +0000 | [diff] [blame] | 771 | } |
| 772 | } |
| 773 | #endif |
| 774 | |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 775 | /* Sanity checking on other operands */ |
| 776 | #ifdef SQLITE_DEBUG |
drh | 7cc84c2 | 2016-04-11 13:36:42 +0000 | [diff] [blame] | 777 | { |
| 778 | u8 opProperty = sqlite3OpcodeProperty[pOp->opcode]; |
| 779 | if( (opProperty & OPFLG_IN1)!=0 ){ |
| 780 | assert( pOp->p1>0 ); |
| 781 | assert( pOp->p1<=(p->nMem+1 - p->nCursor) ); |
| 782 | assert( memIsValid(&aMem[pOp->p1]) ); |
| 783 | assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p1]) ); |
| 784 | REGISTER_TRACE(pOp->p1, &aMem[pOp->p1]); |
| 785 | } |
| 786 | if( (opProperty & OPFLG_IN2)!=0 ){ |
| 787 | assert( pOp->p2>0 ); |
| 788 | assert( pOp->p2<=(p->nMem+1 - p->nCursor) ); |
| 789 | assert( memIsValid(&aMem[pOp->p2]) ); |
| 790 | assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p2]) ); |
| 791 | REGISTER_TRACE(pOp->p2, &aMem[pOp->p2]); |
| 792 | } |
| 793 | if( (opProperty & OPFLG_IN3)!=0 ){ |
| 794 | assert( pOp->p3>0 ); |
| 795 | assert( pOp->p3<=(p->nMem+1 - p->nCursor) ); |
| 796 | assert( memIsValid(&aMem[pOp->p3]) ); |
| 797 | assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p3]) ); |
| 798 | REGISTER_TRACE(pOp->p3, &aMem[pOp->p3]); |
| 799 | } |
| 800 | if( (opProperty & OPFLG_OUT2)!=0 ){ |
| 801 | assert( pOp->p2>0 ); |
| 802 | assert( pOp->p2<=(p->nMem+1 - p->nCursor) ); |
| 803 | memAboutToChange(p, &aMem[pOp->p2]); |
| 804 | } |
| 805 | if( (opProperty & OPFLG_OUT3)!=0 ){ |
| 806 | assert( pOp->p3>0 ); |
| 807 | assert( pOp->p3<=(p->nMem+1 - p->nCursor) ); |
| 808 | memAboutToChange(p, &aMem[pOp->p3]); |
| 809 | } |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 810 | } |
| 811 | #endif |
drh | 6dc4148 | 2015-04-16 17:31:02 +0000 | [diff] [blame] | 812 | #if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE) |
| 813 | pOrigOp = pOp; |
| 814 | #endif |
drh | 93952eb | 2009-11-13 19:43:43 +0000 | [diff] [blame] | 815 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 816 | switch( pOp->opcode ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 817 | |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 818 | /***************************************************************************** |
| 819 | ** What follows is a massive switch statement where each case implements a |
| 820 | ** separate instruction in the virtual machine. If we follow the usual |
| 821 | ** indentation conventions, each case should be indented by 6 spaces. But |
| 822 | ** that is a lot of wasted space on the left margin. So the code within |
| 823 | ** the switch statement will break with convention and be flush-left. Another |
| 824 | ** big comment (similar to this one) will mark the point in the code where |
| 825 | ** we transition back to normal indentation. |
drh | ac82fcf | 2002-09-08 17:23:41 +0000 | [diff] [blame] | 826 | ** |
| 827 | ** The formatting of each case is important. The makefile for SQLite |
| 828 | ** generates two C files "opcodes.h" and "opcodes.c" by scanning this |
| 829 | ** file looking for lines that begin with "case OP_". The opcodes.h files |
| 830 | ** will be filled with #defines that give unique integer values to each |
| 831 | ** 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] | 832 | ** each string is the symbolic name for the corresponding opcode. If the |
| 833 | ** case statement is followed by a comment of the form "/# same as ... #/" |
| 834 | ** that comment is used to determine the particular value of the opcode. |
drh | ac82fcf | 2002-09-08 17:23:41 +0000 | [diff] [blame] | 835 | ** |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 836 | ** Other keywords in the comment that follows each case are used to |
| 837 | ** construct the OPFLG_INITIALIZER value that initializes opcodeProperty[]. |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 838 | ** Keywords include: in1, in2, in3, out2, out3. See |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 839 | ** the mkopcodeh.awk script for additional information. |
danielk1977 | bc04f85 | 2005-03-29 08:26:13 +0000 | [diff] [blame] | 840 | ** |
drh | ac82fcf | 2002-09-08 17:23:41 +0000 | [diff] [blame] | 841 | ** Documentation about VDBE opcodes is generated by scanning this file |
| 842 | ** for lines of that contain "Opcode:". That line and all subsequent |
| 843 | ** comment lines are used in the generation of the opcode.html documentation |
| 844 | ** file. |
| 845 | ** |
| 846 | ** SUMMARY: |
| 847 | ** |
| 848 | ** Formatting is important to scripts that scan this file. |
| 849 | ** Do not deviate from the formatting style currently in use. |
| 850 | ** |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 851 | *****************************************************************************/ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 852 | |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 853 | /* Opcode: Goto * P2 * * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 854 | ** |
| 855 | ** An unconditional jump to address P2. |
| 856 | ** The next instruction executed will be |
| 857 | ** the one at index P2 from the beginning of |
| 858 | ** the program. |
drh | fe70510 | 2014-03-06 13:38:37 +0000 | [diff] [blame] | 859 | ** |
| 860 | ** The P1 parameter is not actually used by this opcode. However, it |
| 861 | ** is sometimes set to 1 instead of 0 as a hint to the command-line shell |
| 862 | ** that this Goto is the bottom of a loop and that the lines from P2 down |
| 863 | ** to the current line should be indented for EXPLAIN output. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 864 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 865 | case OP_Goto: { /* jump */ |
drh | d9670ab | 2019-12-28 01:52:46 +0000 | [diff] [blame] | 866 | |
| 867 | #ifdef SQLITE_DEBUG |
| 868 | /* In debuggging mode, when the p5 flags is set on an OP_Goto, that |
| 869 | ** means we should really jump back to the preceeding OP_ReleaseReg |
| 870 | ** instruction. */ |
| 871 | if( pOp->p5 ){ |
| 872 | assert( pOp->p2 < (int)(pOp - aOp) ); |
| 873 | assert( pOp->p2 > 1 ); |
| 874 | pOp = &aOp[pOp->p2 - 2]; |
| 875 | assert( pOp[1].opcode==OP_ReleaseReg ); |
| 876 | goto check_for_interrupt; |
| 877 | } |
| 878 | #endif |
| 879 | |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 880 | jump_to_p2_and_check_for_interrupt: |
| 881 | pOp = &aOp[pOp->p2 - 1]; |
drh | 49afe3a | 2013-07-10 03:05:14 +0000 | [diff] [blame] | 882 | |
| 883 | /* 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] | 884 | ** OP_VNext, or OP_SorterNext) all jump here upon |
drh | 49afe3a | 2013-07-10 03:05:14 +0000 | [diff] [blame] | 885 | ** completion. Check to see if sqlite3_interrupt() has been called |
| 886 | ** or if the progress callback needs to be invoked. |
| 887 | ** |
| 888 | ** This code uses unstructured "goto" statements and does not look clean. |
| 889 | ** But that is not due to sloppy coding habits. The code is written this |
| 890 | ** way for performance, to avoid having to run the interrupt and progress |
| 891 | ** checks on every opcode. This helps sqlite3_step() to run about 1.5% |
| 892 | ** faster according to "valgrind --tool=cachegrind" */ |
| 893 | check_for_interrupt: |
dan | 892edb6 | 2020-03-30 13:35:05 +0000 | [diff] [blame] | 894 | if( AtomicLoad(&db->u1.isInterrupted) ) goto abort_due_to_interrupt; |
drh | 49afe3a | 2013-07-10 03:05:14 +0000 | [diff] [blame] | 895 | #ifndef SQLITE_OMIT_PROGRESS_CALLBACK |
| 896 | /* Call the progress callback if it is configured and the required number |
| 897 | ** of VDBE ops have been executed (either since this invocation of |
| 898 | ** sqlite3VdbeExec() or since last time the progress callback was called). |
| 899 | ** If the progress callback returns non-zero, exit the virtual machine with |
| 900 | ** a return code SQLITE_ABORT. |
| 901 | */ |
drh | b1af9c6 | 2019-02-20 13:55:45 +0000 | [diff] [blame] | 902 | while( nVmStep>=nProgressLimit && db->xProgress!=0 ){ |
drh | 400fcba | 2013-11-14 00:09:48 +0000 | [diff] [blame] | 903 | assert( db->nProgressOps!=0 ); |
drh | b1af9c6 | 2019-02-20 13:55:45 +0000 | [diff] [blame] | 904 | nProgressLimit += db->nProgressOps; |
drh | 400fcba | 2013-11-14 00:09:48 +0000 | [diff] [blame] | 905 | if( db->xProgress(db->pProgressArg) ){ |
drh | c332e04 | 2019-02-12 21:04:33 +0000 | [diff] [blame] | 906 | nProgressLimit = 0xffffffff; |
drh | 49afe3a | 2013-07-10 03:05:14 +0000 | [diff] [blame] | 907 | rc = SQLITE_INTERRUPT; |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 908 | goto abort_due_to_error; |
drh | 49afe3a | 2013-07-10 03:05:14 +0000 | [diff] [blame] | 909 | } |
drh | 49afe3a | 2013-07-10 03:05:14 +0000 | [diff] [blame] | 910 | } |
| 911 | #endif |
| 912 | |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 913 | break; |
| 914 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 915 | |
drh | 2eb9537 | 2008-06-06 15:04:36 +0000 | [diff] [blame] | 916 | /* Opcode: Gosub P1 P2 * * * |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 917 | ** |
drh | 2eb9537 | 2008-06-06 15:04:36 +0000 | [diff] [blame] | 918 | ** Write the current address onto register P1 |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 919 | ** and then jump to address P2. |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 920 | */ |
drh | b8475df | 2011-12-09 16:21:19 +0000 | [diff] [blame] | 921 | case OP_Gosub: { /* jump */ |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 922 | assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) ); |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 923 | pIn1 = &aMem[pOp->p1]; |
drh | c91b2fd | 2014-03-01 18:13:23 +0000 | [diff] [blame] | 924 | assert( VdbeMemDynamic(pIn1)==0 ); |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 925 | memAboutToChange(p, pIn1); |
drh | 2eb9537 | 2008-06-06 15:04:36 +0000 | [diff] [blame] | 926 | pIn1->flags = MEM_Int; |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 927 | pIn1->u.i = (int)(pOp-aOp); |
drh | 2eb9537 | 2008-06-06 15:04:36 +0000 | [diff] [blame] | 928 | REGISTER_TRACE(pOp->p1, pIn1); |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 929 | |
| 930 | /* Most jump operations do a goto to this spot in order to update |
| 931 | ** the pOp pointer. */ |
| 932 | jump_to_p2: |
| 933 | pOp = &aOp[pOp->p2 - 1]; |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 934 | break; |
| 935 | } |
| 936 | |
drh | 2eb9537 | 2008-06-06 15:04:36 +0000 | [diff] [blame] | 937 | /* Opcode: Return P1 * * * * |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 938 | ** |
drh | 81cf13e | 2014-02-07 18:27:53 +0000 | [diff] [blame] | 939 | ** Jump to the next instruction after the address in register P1. After |
| 940 | ** the jump, register P1 becomes undefined. |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 941 | */ |
drh | 2eb9537 | 2008-06-06 15:04:36 +0000 | [diff] [blame] | 942 | case OP_Return: { /* in1 */ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 943 | pIn1 = &aMem[pOp->p1]; |
drh | 81cf13e | 2014-02-07 18:27:53 +0000 | [diff] [blame] | 944 | assert( pIn1->flags==MEM_Int ); |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 945 | pOp = &aOp[pIn1->u.i]; |
drh | 81cf13e | 2014-02-07 18:27:53 +0000 | [diff] [blame] | 946 | pIn1->flags = MEM_Undefined; |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 947 | break; |
| 948 | } |
| 949 | |
drh | ed71a83 | 2014-02-07 19:18:10 +0000 | [diff] [blame] | 950 | /* Opcode: InitCoroutine P1 P2 P3 * * |
drh | 81cf13e | 2014-02-07 18:27:53 +0000 | [diff] [blame] | 951 | ** |
drh | 5dad9a3 | 2014-07-25 18:37:42 +0000 | [diff] [blame] | 952 | ** Set up register P1 so that it will Yield to the coroutine |
drh | ed71a83 | 2014-02-07 19:18:10 +0000 | [diff] [blame] | 953 | ** located at address P3. |
| 954 | ** |
drh | 5dad9a3 | 2014-07-25 18:37:42 +0000 | [diff] [blame] | 955 | ** If P2!=0 then the coroutine implementation immediately follows |
| 956 | ** this opcode. So jump over the coroutine implementation to |
drh | ed71a83 | 2014-02-07 19:18:10 +0000 | [diff] [blame] | 957 | ** address P2. |
drh | 5dad9a3 | 2014-07-25 18:37:42 +0000 | [diff] [blame] | 958 | ** |
| 959 | ** See also: EndCoroutine |
drh | 81cf13e | 2014-02-07 18:27:53 +0000 | [diff] [blame] | 960 | */ |
| 961 | case OP_InitCoroutine: { /* jump */ |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 962 | assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) ); |
drh | ed71a83 | 2014-02-07 19:18:10 +0000 | [diff] [blame] | 963 | assert( pOp->p2>=0 && pOp->p2<p->nOp ); |
| 964 | assert( pOp->p3>=0 && pOp->p3<p->nOp ); |
drh | 81cf13e | 2014-02-07 18:27:53 +0000 | [diff] [blame] | 965 | pOut = &aMem[pOp->p1]; |
drh | ed71a83 | 2014-02-07 19:18:10 +0000 | [diff] [blame] | 966 | assert( !VdbeMemDynamic(pOut) ); |
| 967 | pOut->u.i = pOp->p3 - 1; |
drh | 81cf13e | 2014-02-07 18:27:53 +0000 | [diff] [blame] | 968 | pOut->flags = MEM_Int; |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 969 | if( pOp->p2 ) goto jump_to_p2; |
drh | 81cf13e | 2014-02-07 18:27:53 +0000 | [diff] [blame] | 970 | break; |
| 971 | } |
| 972 | |
| 973 | /* Opcode: EndCoroutine P1 * * * * |
| 974 | ** |
drh | bc5cf38 | 2014-08-06 01:08:07 +0000 | [diff] [blame] | 975 | ** The instruction at the address in register P1 is a Yield. |
drh | 5dad9a3 | 2014-07-25 18:37:42 +0000 | [diff] [blame] | 976 | ** Jump to the P2 parameter of that Yield. |
drh | 81cf13e | 2014-02-07 18:27:53 +0000 | [diff] [blame] | 977 | ** After the jump, register P1 becomes undefined. |
drh | 5dad9a3 | 2014-07-25 18:37:42 +0000 | [diff] [blame] | 978 | ** |
| 979 | ** See also: InitCoroutine |
drh | 81cf13e | 2014-02-07 18:27:53 +0000 | [diff] [blame] | 980 | */ |
| 981 | case OP_EndCoroutine: { /* in1 */ |
| 982 | VdbeOp *pCaller; |
| 983 | pIn1 = &aMem[pOp->p1]; |
| 984 | assert( pIn1->flags==MEM_Int ); |
| 985 | assert( pIn1->u.i>=0 && pIn1->u.i<p->nOp ); |
| 986 | pCaller = &aOp[pIn1->u.i]; |
| 987 | assert( pCaller->opcode==OP_Yield ); |
| 988 | assert( pCaller->p2>=0 && pCaller->p2<p->nOp ); |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 989 | pOp = &aOp[pCaller->p2 - 1]; |
drh | 81cf13e | 2014-02-07 18:27:53 +0000 | [diff] [blame] | 990 | pIn1->flags = MEM_Undefined; |
| 991 | break; |
| 992 | } |
| 993 | |
| 994 | /* Opcode: Yield P1 P2 * * * |
drh | e00ee6e | 2008-06-20 15:24:01 +0000 | [diff] [blame] | 995 | ** |
drh | 5dad9a3 | 2014-07-25 18:37:42 +0000 | [diff] [blame] | 996 | ** Swap the program counter with the value in register P1. This |
| 997 | ** has the effect of yielding to a coroutine. |
drh | 81cf13e | 2014-02-07 18:27:53 +0000 | [diff] [blame] | 998 | ** |
drh | 5dad9a3 | 2014-07-25 18:37:42 +0000 | [diff] [blame] | 999 | ** If the coroutine that is launched by this instruction ends with |
| 1000 | ** Yield or Return then continue to the next instruction. But if |
| 1001 | ** the coroutine launched by this instruction ends with |
| 1002 | ** EndCoroutine, then jump to P2 rather than continuing with the |
| 1003 | ** next instruction. |
| 1004 | ** |
| 1005 | ** See also: InitCoroutine |
drh | e00ee6e | 2008-06-20 15:24:01 +0000 | [diff] [blame] | 1006 | */ |
drh | 81cf13e | 2014-02-07 18:27:53 +0000 | [diff] [blame] | 1007 | case OP_Yield: { /* in1, jump */ |
drh | e00ee6e | 2008-06-20 15:24:01 +0000 | [diff] [blame] | 1008 | int pcDest; |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 1009 | pIn1 = &aMem[pOp->p1]; |
drh | c91b2fd | 2014-03-01 18:13:23 +0000 | [diff] [blame] | 1010 | assert( VdbeMemDynamic(pIn1)==0 ); |
drh | e00ee6e | 2008-06-20 15:24:01 +0000 | [diff] [blame] | 1011 | pIn1->flags = MEM_Int; |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 1012 | pcDest = (int)pIn1->u.i; |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 1013 | pIn1->u.i = (int)(pOp - aOp); |
drh | e00ee6e | 2008-06-20 15:24:01 +0000 | [diff] [blame] | 1014 | REGISTER_TRACE(pOp->p1, pIn1); |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 1015 | pOp = &aOp[pcDest]; |
drh | e00ee6e | 2008-06-20 15:24:01 +0000 | [diff] [blame] | 1016 | break; |
| 1017 | } |
| 1018 | |
drh | f9c8ce3 | 2013-11-05 13:33:55 +0000 | [diff] [blame] | 1019 | /* Opcode: HaltIfNull P1 P2 P3 P4 P5 |
drh | 72e26de | 2016-08-24 21:24:04 +0000 | [diff] [blame] | 1020 | ** Synopsis: if r[P3]=null halt |
drh | 5053a79 | 2009-02-20 03:02:23 +0000 | [diff] [blame] | 1021 | ** |
drh | ef8662b | 2011-06-20 21:47:58 +0000 | [diff] [blame] | 1022 | ** Check the value in register P3. If it is NULL then Halt using |
drh | 5053a79 | 2009-02-20 03:02:23 +0000 | [diff] [blame] | 1023 | ** parameter P1, P2, and P4 as if this were a Halt instruction. If the |
| 1024 | ** 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] | 1025 | ** The P5 parameter should be 1. |
drh | 5053a79 | 2009-02-20 03:02:23 +0000 | [diff] [blame] | 1026 | */ |
| 1027 | case OP_HaltIfNull: { /* in3 */ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 1028 | pIn3 = &aMem[pOp->p3]; |
drh | 4031baf | 2018-05-28 17:31:20 +0000 | [diff] [blame] | 1029 | #ifdef SQLITE_DEBUG |
| 1030 | if( pOp->p2==OE_Abort ){ sqlite3VdbeAssertAbortable(p); } |
| 1031 | #endif |
drh | 5053a79 | 2009-02-20 03:02:23 +0000 | [diff] [blame] | 1032 | if( (pIn3->flags & MEM_Null)==0 ) break; |
| 1033 | /* Fall through into OP_Halt */ |
| 1034 | } |
drh | e00ee6e | 2008-06-20 15:24:01 +0000 | [diff] [blame] | 1035 | |
drh | f9c8ce3 | 2013-11-05 13:33:55 +0000 | [diff] [blame] | 1036 | /* Opcode: Halt P1 P2 * P4 P5 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1037 | ** |
drh | 3d4501e | 2008-12-04 20:40:10 +0000 | [diff] [blame] | 1038 | ** Exit immediately. All open cursors, etc are closed |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1039 | ** automatically. |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 1040 | ** |
drh | 92f02c3 | 2004-09-02 14:57:08 +0000 | [diff] [blame] | 1041 | ** P1 is the result code returned by sqlite3_exec(), sqlite3_reset(), |
| 1042 | ** or sqlite3_finalize(). For a normal halt, this should be SQLITE_OK (0). |
| 1043 | ** For errors, it can be some other value. If P1!=0 then P2 will determine |
| 1044 | ** whether or not to rollback the current transaction. Do not rollback |
| 1045 | ** if P2==OE_Fail. Do the rollback if P2==OE_Rollback. If P2==OE_Abort, |
| 1046 | ** then back out all changes that have occurred during this execution of the |
drh | b798fa6 | 2002-09-03 19:43:23 +0000 | [diff] [blame] | 1047 | ** VDBE, but do not rollback the transaction. |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 1048 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 1049 | ** If P4 is not null then it is an error message string. |
drh | 7f057c9 | 2005-06-24 03:53:06 +0000 | [diff] [blame] | 1050 | ** |
drh | f9c8ce3 | 2013-11-05 13:33:55 +0000 | [diff] [blame] | 1051 | ** P5 is a value between 0 and 4, inclusive, that modifies the P4 string. |
| 1052 | ** |
| 1053 | ** 0: (no change) |
| 1054 | ** 1: NOT NULL contraint failed: P4 |
| 1055 | ** 2: UNIQUE constraint failed: P4 |
| 1056 | ** 3: CHECK constraint failed: P4 |
| 1057 | ** 4: FOREIGN KEY constraint failed: P4 |
| 1058 | ** |
| 1059 | ** If P5 is not zero and P4 is NULL, then everything after the ":" is |
| 1060 | ** omitted. |
| 1061 | ** |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 1062 | ** 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] | 1063 | ** every program. So a jump past the last instruction of the program |
| 1064 | ** is the same as executing Halt. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1065 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1066 | case OP_Halt: { |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 1067 | VdbeFrame *pFrame; |
| 1068 | int pcx; |
drh | f9c8ce3 | 2013-11-05 13:33:55 +0000 | [diff] [blame] | 1069 | |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 1070 | pcx = (int)(pOp - aOp); |
drh | 4031baf | 2018-05-28 17:31:20 +0000 | [diff] [blame] | 1071 | #ifdef SQLITE_DEBUG |
| 1072 | if( pOp->p2==OE_Abort ){ sqlite3VdbeAssertAbortable(p); } |
| 1073 | #endif |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 1074 | if( pOp->p1==SQLITE_OK && p->pFrame ){ |
dan | 2832ad4 | 2009-08-31 15:27:27 +0000 | [diff] [blame] | 1075 | /* Halt the sub-program. Return control to the parent frame. */ |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 1076 | pFrame = p->pFrame; |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 1077 | p->pFrame = pFrame->pParent; |
| 1078 | p->nFrame--; |
dan | 2832ad4 | 2009-08-31 15:27:27 +0000 | [diff] [blame] | 1079 | sqlite3VdbeSetChanges(db, p->nChange); |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 1080 | pcx = sqlite3VdbeFrameRestore(pFrame); |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 1081 | if( pOp->p2==OE_Ignore ){ |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 1082 | /* Instruction pcx is the OP_Program that invoked the sub-program |
dan | 2832ad4 | 2009-08-31 15:27:27 +0000 | [diff] [blame] | 1083 | ** currently being halted. If the p2 instruction of this OP_Halt |
| 1084 | ** instruction is set to OE_Ignore, then the sub-program is throwing |
| 1085 | ** an IGNORE exception. In this case jump to the address specified |
| 1086 | ** as the p2 of the calling OP_Program. */ |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 1087 | pcx = p->aOp[pcx].p2-1; |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 1088 | } |
drh | bbe879d | 2009-11-14 18:04:35 +0000 | [diff] [blame] | 1089 | aOp = p->aOp; |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 1090 | aMem = p->aMem; |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 1091 | pOp = &aOp[pcx]; |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 1092 | break; |
| 1093 | } |
drh | 92f02c3 | 2004-09-02 14:57:08 +0000 | [diff] [blame] | 1094 | p->rc = pOp->p1; |
shane | 36840fd | 2009-06-26 16:32:13 +0000 | [diff] [blame] | 1095 | p->errorAction = (u8)pOp->p2; |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 1096 | p->pc = pcx; |
drh | fb4e3a3 | 2016-12-30 00:09:14 +0000 | [diff] [blame] | 1097 | assert( pOp->p5<=4 ); |
drh | f9c8ce3 | 2013-11-05 13:33:55 +0000 | [diff] [blame] | 1098 | if( p->rc ){ |
drh | d9b7ec9 | 2013-11-06 14:05:21 +0000 | [diff] [blame] | 1099 | if( pOp->p5 ){ |
| 1100 | static const char * const azType[] = { "NOT NULL", "UNIQUE", "CHECK", |
| 1101 | "FOREIGN KEY" }; |
drh | d9b7ec9 | 2013-11-06 14:05:21 +0000 | [diff] [blame] | 1102 | testcase( pOp->p5==1 ); |
| 1103 | testcase( pOp->p5==2 ); |
| 1104 | testcase( pOp->p5==3 ); |
| 1105 | testcase( pOp->p5==4 ); |
drh | 99f5de7 | 2016-04-30 02:59:15 +0000 | [diff] [blame] | 1106 | sqlite3VdbeError(p, "%s constraint failed", azType[pOp->p5-1]); |
| 1107 | if( pOp->p4.z ){ |
| 1108 | p->zErrMsg = sqlite3MPrintf(db, "%z: %s", p->zErrMsg, pOp->p4.z); |
| 1109 | } |
drh | d9b7ec9 | 2013-11-06 14:05:21 +0000 | [diff] [blame] | 1110 | }else{ |
drh | 22c17b8 | 2015-05-15 04:13:15 +0000 | [diff] [blame] | 1111 | sqlite3VdbeError(p, "%s", pOp->p4.z); |
drh | f9c8ce3 | 2013-11-05 13:33:55 +0000 | [diff] [blame] | 1112 | } |
drh | 99f5de7 | 2016-04-30 02:59:15 +0000 | [diff] [blame] | 1113 | 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] | 1114 | } |
drh | 92f02c3 | 2004-09-02 14:57:08 +0000 | [diff] [blame] | 1115 | rc = sqlite3VdbeHalt(p); |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 1116 | assert( rc==SQLITE_BUSY || rc==SQLITE_OK || rc==SQLITE_ERROR ); |
drh | 92f02c3 | 2004-09-02 14:57:08 +0000 | [diff] [blame] | 1117 | if( rc==SQLITE_BUSY ){ |
drh | 99f5de7 | 2016-04-30 02:59:15 +0000 | [diff] [blame] | 1118 | p->rc = SQLITE_BUSY; |
drh | 900b31e | 2007-08-28 02:27:51 +0000 | [diff] [blame] | 1119 | }else{ |
drh | d91c1a1 | 2013-02-09 13:58:25 +0000 | [diff] [blame] | 1120 | assert( rc==SQLITE_OK || (p->rc&0xff)==SQLITE_CONSTRAINT ); |
dan | cb3e4b7 | 2013-07-03 19:53:05 +0000 | [diff] [blame] | 1121 | assert( rc==SQLITE_OK || db->nDeferredCons>0 || db->nDeferredImmCons>0 ); |
drh | 900b31e | 2007-08-28 02:27:51 +0000 | [diff] [blame] | 1122 | rc = p->rc ? SQLITE_ERROR : SQLITE_DONE; |
drh | 92f02c3 | 2004-09-02 14:57:08 +0000 | [diff] [blame] | 1123 | } |
drh | 900b31e | 2007-08-28 02:27:51 +0000 | [diff] [blame] | 1124 | goto vdbe_return; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1125 | } |
drh | c61053b | 2000-06-04 12:58:36 +0000 | [diff] [blame] | 1126 | |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 1127 | /* Opcode: Integer P1 P2 * * * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 1128 | ** Synopsis: r[P2]=P1 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1129 | ** |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1130 | ** The 32-bit integer value P1 is written into register P2. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1131 | */ |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 1132 | case OP_Integer: { /* out2 */ |
| 1133 | pOut = out2Prerelease(p, pOp); |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 1134 | pOut->u.i = pOp->p1; |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 1135 | break; |
| 1136 | } |
| 1137 | |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 1138 | /* Opcode: Int64 * P2 * P4 * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 1139 | ** Synopsis: r[P2]=P4 |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 1140 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 1141 | ** P4 is a pointer to a 64-bit integer value. |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1142 | ** Write that value into register P2. |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 1143 | */ |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 1144 | case OP_Int64: { /* out2 */ |
| 1145 | pOut = out2Prerelease(p, pOp); |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 1146 | assert( pOp->p4.pI64!=0 ); |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 1147 | pOut->u.i = *pOp->p4.pI64; |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 1148 | break; |
| 1149 | } |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 1150 | |
drh | 13573c7 | 2010-01-12 17:04:07 +0000 | [diff] [blame] | 1151 | #ifndef SQLITE_OMIT_FLOATING_POINT |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 1152 | /* Opcode: Real * P2 * P4 * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 1153 | ** Synopsis: r[P2]=P4 |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 1154 | ** |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 1155 | ** P4 is a pointer to a 64-bit floating point value. |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1156 | ** Write that value into register P2. |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 1157 | */ |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 1158 | case OP_Real: { /* same as TK_FLOAT, out2 */ |
| 1159 | pOut = out2Prerelease(p, pOp); |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 1160 | pOut->flags = MEM_Real; |
drh | 2eaf93d | 2008-04-29 00:15:20 +0000 | [diff] [blame] | 1161 | assert( !sqlite3IsNaN(*pOp->p4.pReal) ); |
drh | 74eaba4 | 2014-09-18 17:52:15 +0000 | [diff] [blame] | 1162 | pOut->u.r = *pOp->p4.pReal; |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 1163 | break; |
| 1164 | } |
drh | 13573c7 | 2010-01-12 17:04:07 +0000 | [diff] [blame] | 1165 | #endif |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 1166 | |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 1167 | /* Opcode: String8 * P2 * P4 * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 1168 | ** Synopsis: r[P2]='P4' |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 1169 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 1170 | ** P4 points to a nul terminated UTF-8 string. This opcode is transformed |
drh | f07cf6e | 2015-03-06 16:45:16 +0000 | [diff] [blame] | 1171 | ** into a String opcode before it is executed for the first time. During |
drh | 0fd6135 | 2014-02-07 02:29:45 +0000 | [diff] [blame] | 1172 | ** this transformation, the length of string P4 is computed and stored |
| 1173 | ** as the P1 parameter. |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 1174 | */ |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 1175 | case OP_String8: { /* same as TK_STRING, out2 */ |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 1176 | assert( pOp->p4.z!=0 ); |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 1177 | pOut = out2Prerelease(p, pOp); |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 1178 | pOp->p1 = sqlite3Strlen30(pOp->p4.z); |
drh | ed2df7f | 2005-11-16 04:34:32 +0000 | [diff] [blame] | 1179 | |
| 1180 | #ifndef SQLITE_OMIT_UTF16 |
drh | 8079a0d | 2006-01-12 17:20:50 +0000 | [diff] [blame] | 1181 | if( encoding!=SQLITE_UTF8 ){ |
drh | 3a9cf17 | 2009-06-17 21:42:33 +0000 | [diff] [blame] | 1182 | rc = sqlite3VdbeMemSetStr(pOut, pOp->p4.z, -1, SQLITE_UTF8, SQLITE_STATIC); |
drh | 2f55511 | 2016-04-30 18:10:34 +0000 | [diff] [blame] | 1183 | assert( rc==SQLITE_OK || rc==SQLITE_TOOBIG ); |
drh | dbdddc9 | 2019-02-21 16:41:34 +0000 | [diff] [blame] | 1184 | if( rc ) goto too_big; |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 1185 | if( SQLITE_OK!=sqlite3VdbeChangeEncoding(pOut, encoding) ) goto no_mem; |
drh | 17bcb10 | 2014-09-18 21:25:33 +0000 | [diff] [blame] | 1186 | assert( pOut->szMalloc>0 && pOut->zMalloc==pOut->z ); |
drh | c91b2fd | 2014-03-01 18:13:23 +0000 | [diff] [blame] | 1187 | assert( VdbeMemDynamic(pOut)==0 ); |
drh | 17bcb10 | 2014-09-18 21:25:33 +0000 | [diff] [blame] | 1188 | pOut->szMalloc = 0; |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 1189 | pOut->flags |= MEM_Static; |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 1190 | if( pOp->p4type==P4_DYNAMIC ){ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 1191 | sqlite3DbFree(db, pOp->p4.z); |
danielk1977 | e004840 | 2004-06-15 16:51:01 +0000 | [diff] [blame] | 1192 | } |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 1193 | pOp->p4type = P4_DYNAMIC; |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 1194 | pOp->p4.z = pOut->z; |
| 1195 | pOp->p1 = pOut->n; |
danielk1977 | 0f69c1e | 2004-05-29 11:24:50 +0000 | [diff] [blame] | 1196 | } |
danielk1977 | 93758c8 | 2005-01-21 08:13:14 +0000 | [diff] [blame] | 1197 | #endif |
drh | bb4957f | 2008-03-20 14:03:29 +0000 | [diff] [blame] | 1198 | if( pOp->p1>db->aLimit[SQLITE_LIMIT_LENGTH] ){ |
drh | cbd2da9 | 2007-12-17 16:20:06 +0000 | [diff] [blame] | 1199 | goto too_big; |
| 1200 | } |
drh | ec722c1 | 2019-09-17 21:28:54 +0000 | [diff] [blame] | 1201 | pOp->opcode = OP_String; |
drh | 2f55511 | 2016-04-30 18:10:34 +0000 | [diff] [blame] | 1202 | assert( rc==SQLITE_OK ); |
drh | cbd2da9 | 2007-12-17 16:20:06 +0000 | [diff] [blame] | 1203 | /* Fall through to the next case, OP_String */ |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 1204 | } |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 1205 | |
drh | f07cf6e | 2015-03-06 16:45:16 +0000 | [diff] [blame] | 1206 | /* Opcode: String P1 P2 P3 P4 P5 |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 1207 | ** Synopsis: r[P2]='P4' (len=P1) |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 1208 | ** |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1209 | ** The string value P4 of length P1 (bytes) is stored in register P2. |
drh | f07cf6e | 2015-03-06 16:45:16 +0000 | [diff] [blame] | 1210 | ** |
drh | 44aebff | 2016-05-02 10:25:42 +0000 | [diff] [blame] | 1211 | ** 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] | 1212 | ** the datatype of the register P2 is converted to BLOB. The content is |
| 1213 | ** the same sequence of bytes, it is merely interpreted as a BLOB instead |
drh | 44aebff | 2016-05-02 10:25:42 +0000 | [diff] [blame] | 1214 | ** of a string, as if it had been CAST. In other words: |
| 1215 | ** |
| 1216 | ** 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] | 1217 | */ |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 1218 | case OP_String: { /* out2 */ |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 1219 | assert( pOp->p4.z!=0 ); |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 1220 | pOut = out2Prerelease(p, pOp); |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 1221 | pOut->flags = MEM_Str|MEM_Static|MEM_Term; |
| 1222 | pOut->z = pOp->p4.z; |
| 1223 | pOut->n = pOp->p1; |
| 1224 | pOut->enc = encoding; |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 1225 | UPDATE_MAX_BLOBSIZE(pOut); |
drh | 41d2e66 | 2015-12-01 21:23:07 +0000 | [diff] [blame] | 1226 | #ifndef SQLITE_LIKE_DOESNT_MATCH_BLOBS |
drh | 44aebff | 2016-05-02 10:25:42 +0000 | [diff] [blame] | 1227 | if( pOp->p3>0 ){ |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 1228 | assert( pOp->p3<=(p->nMem+1 - p->nCursor) ); |
drh | f07cf6e | 2015-03-06 16:45:16 +0000 | [diff] [blame] | 1229 | pIn3 = &aMem[pOp->p3]; |
| 1230 | assert( pIn3->flags & MEM_Int ); |
drh | 44aebff | 2016-05-02 10:25:42 +0000 | [diff] [blame] | 1231 | 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] | 1232 | } |
drh | 41d2e66 | 2015-12-01 21:23:07 +0000 | [diff] [blame] | 1233 | #endif |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 1234 | break; |
| 1235 | } |
| 1236 | |
drh | 053a128 | 2012-09-19 21:15:46 +0000 | [diff] [blame] | 1237 | /* Opcode: Null P1 P2 P3 * * |
drh | 72e26de | 2016-08-24 21:24:04 +0000 | [diff] [blame] | 1238 | ** Synopsis: r[P2..P3]=NULL |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 1239 | ** |
drh | b8475df | 2011-12-09 16:21:19 +0000 | [diff] [blame] | 1240 | ** 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] | 1241 | ** 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] | 1242 | ** is less than P2 (typically P3 is zero) then only register P2 is |
drh | 053a128 | 2012-09-19 21:15:46 +0000 | [diff] [blame] | 1243 | ** set to NULL. |
| 1244 | ** |
| 1245 | ** If the P1 value is non-zero, then also set the MEM_Cleared flag so that |
| 1246 | ** NULL values will not compare equal even if SQLITE_NULLEQ is set on |
| 1247 | ** OP_Ne or OP_Eq. |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 1248 | */ |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 1249 | case OP_Null: { /* out2 */ |
drh | b8475df | 2011-12-09 16:21:19 +0000 | [diff] [blame] | 1250 | int cnt; |
drh | 053a128 | 2012-09-19 21:15:46 +0000 | [diff] [blame] | 1251 | u16 nullFlag; |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 1252 | pOut = out2Prerelease(p, pOp); |
drh | b8475df | 2011-12-09 16:21:19 +0000 | [diff] [blame] | 1253 | cnt = pOp->p3-pOp->p2; |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 1254 | assert( pOp->p3<=(p->nMem+1 - p->nCursor) ); |
drh | 053a128 | 2012-09-19 21:15:46 +0000 | [diff] [blame] | 1255 | pOut->flags = nullFlag = pOp->p1 ? (MEM_Null|MEM_Cleared) : MEM_Null; |
drh | 2a1df93 | 2016-09-30 17:46:44 +0000 | [diff] [blame] | 1256 | pOut->n = 0; |
drh | 2c885d0 | 2018-07-07 19:36:04 +0000 | [diff] [blame] | 1257 | #ifdef SQLITE_DEBUG |
| 1258 | pOut->uTemp = 0; |
| 1259 | #endif |
drh | b8475df | 2011-12-09 16:21:19 +0000 | [diff] [blame] | 1260 | while( cnt>0 ){ |
| 1261 | pOut++; |
| 1262 | memAboutToChange(p, pOut); |
drh | 0725cab | 2014-09-17 14:52:46 +0000 | [diff] [blame] | 1263 | sqlite3VdbeMemSetNull(pOut); |
drh | 053a128 | 2012-09-19 21:15:46 +0000 | [diff] [blame] | 1264 | pOut->flags = nullFlag; |
drh | 2a1df93 | 2016-09-30 17:46:44 +0000 | [diff] [blame] | 1265 | pOut->n = 0; |
drh | b8475df | 2011-12-09 16:21:19 +0000 | [diff] [blame] | 1266 | cnt--; |
| 1267 | } |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 1268 | break; |
| 1269 | } |
| 1270 | |
drh | 05a86c5 | 2014-02-16 01:55:49 +0000 | [diff] [blame] | 1271 | /* Opcode: SoftNull P1 * * * * |
drh | 72e26de | 2016-08-24 21:24:04 +0000 | [diff] [blame] | 1272 | ** Synopsis: r[P1]=NULL |
drh | 05a86c5 | 2014-02-16 01:55:49 +0000 | [diff] [blame] | 1273 | ** |
| 1274 | ** Set register P1 to have the value NULL as seen by the OP_MakeRecord |
| 1275 | ** instruction, but do not free any string or blob memory associated with |
| 1276 | ** the register, so that if the value was a string or blob that was |
| 1277 | ** previously copied using OP_SCopy, the copies will continue to be valid. |
| 1278 | */ |
| 1279 | case OP_SoftNull: { |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 1280 | assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) ); |
drh | 05a86c5 | 2014-02-16 01:55:49 +0000 | [diff] [blame] | 1281 | pOut = &aMem[pOp->p1]; |
drh | e2bc655 | 2017-04-17 20:50:34 +0000 | [diff] [blame] | 1282 | pOut->flags = (pOut->flags&~(MEM_Undefined|MEM_AffMask))|MEM_Null; |
drh | 05a86c5 | 2014-02-16 01:55:49 +0000 | [diff] [blame] | 1283 | break; |
| 1284 | } |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 1285 | |
drh | a5750cf | 2014-02-07 13:20:31 +0000 | [diff] [blame] | 1286 | /* Opcode: Blob P1 P2 * P4 * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 1287 | ** Synopsis: r[P2]=P4 (len=P1) |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 1288 | ** |
drh | 9de221d | 2008-01-05 06:51:30 +0000 | [diff] [blame] | 1289 | ** P4 points to a blob of data P1 bytes long. Store this |
drh | 710c484 | 2010-08-30 01:17:20 +0000 | [diff] [blame] | 1290 | ** blob in register P2. |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 1291 | */ |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 1292 | case OP_Blob: { /* out2 */ |
drh | cbd2da9 | 2007-12-17 16:20:06 +0000 | [diff] [blame] | 1293 | assert( pOp->p1 <= SQLITE_MAX_LENGTH ); |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 1294 | pOut = out2Prerelease(p, pOp); |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 1295 | sqlite3VdbeMemSetStr(pOut, pOp->p4.z, pOp->p1, 0, 0); |
drh | 9de221d | 2008-01-05 06:51:30 +0000 | [diff] [blame] | 1296 | pOut->enc = encoding; |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 1297 | UPDATE_MAX_BLOBSIZE(pOut); |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 1298 | break; |
| 1299 | } |
| 1300 | |
drh | eaf52d8 | 2010-05-12 13:50:23 +0000 | [diff] [blame] | 1301 | /* Opcode: Variable P1 P2 * P4 * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 1302 | ** Synopsis: r[P2]=parameter(P1,P4) |
drh | 5045789 | 2003-09-06 01:10:47 +0000 | [diff] [blame] | 1303 | ** |
drh | eaf52d8 | 2010-05-12 13:50:23 +0000 | [diff] [blame] | 1304 | ** Transfer the values of bound parameter P1 into register P2 |
drh | 08de149 | 2009-02-20 03:55:05 +0000 | [diff] [blame] | 1305 | ** |
drh | 0fd6135 | 2014-02-07 02:29:45 +0000 | [diff] [blame] | 1306 | ** If the parameter is named, then its name appears in P4. |
drh | 08de149 | 2009-02-20 03:55:05 +0000 | [diff] [blame] | 1307 | ** The P4 value is used by sqlite3_bind_parameter_name(). |
drh | 5045789 | 2003-09-06 01:10:47 +0000 | [diff] [blame] | 1308 | */ |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 1309 | case OP_Variable: { /* out2 */ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1310 | Mem *pVar; /* Value being transferred */ |
| 1311 | |
drh | eaf52d8 | 2010-05-12 13:50:23 +0000 | [diff] [blame] | 1312 | assert( pOp->p1>0 && pOp->p1<=p->nVar ); |
drh | 9bf755c | 2016-12-23 03:59:31 +0000 | [diff] [blame] | 1313 | assert( pOp->p4.z==0 || pOp->p4.z==sqlite3VListNumToName(p->pVList,pOp->p1) ); |
drh | eaf52d8 | 2010-05-12 13:50:23 +0000 | [diff] [blame] | 1314 | pVar = &p->aVar[pOp->p1 - 1]; |
| 1315 | if( sqlite3VdbeMemTooBig(pVar) ){ |
| 1316 | goto too_big; |
drh | 023ae03 | 2007-05-08 12:12:16 +0000 | [diff] [blame] | 1317 | } |
drh | 7441df7 | 2017-01-09 19:27:04 +0000 | [diff] [blame] | 1318 | pOut = &aMem[pOp->p2]; |
drh | e0f20b4 | 2019-04-01 20:57:11 +0000 | [diff] [blame] | 1319 | if( VdbeMemDynamic(pOut) ) sqlite3VdbeMemSetNull(pOut); |
| 1320 | memcpy(pOut, pVar, MEMCELLSIZE); |
| 1321 | pOut->flags &= ~(MEM_Dyn|MEM_Ephem); |
| 1322 | pOut->flags |= MEM_Static|MEM_FromBind; |
drh | eaf52d8 | 2010-05-12 13:50:23 +0000 | [diff] [blame] | 1323 | UPDATE_MAX_BLOBSIZE(pOut); |
danielk1977 | 93d4675 | 2004-05-23 13:30:58 +0000 | [diff] [blame] | 1324 | break; |
| 1325 | } |
danielk1977 | 295ba55 | 2004-05-19 10:34:51 +0000 | [diff] [blame] | 1326 | |
drh | b21e7c7 | 2008-06-22 12:37:57 +0000 | [diff] [blame] | 1327 | /* Opcode: Move P1 P2 P3 * * |
drh | 72e26de | 2016-08-24 21:24:04 +0000 | [diff] [blame] | 1328 | ** Synopsis: r[P2@P3]=r[P1@P3] |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1329 | ** |
drh | 079a307 | 2014-03-19 14:10:55 +0000 | [diff] [blame] | 1330 | ** Move the P3 values in register P1..P1+P3-1 over into |
| 1331 | ** registers P2..P2+P3-1. Registers P1..P1+P3-1 are |
drh | b21e7c7 | 2008-06-22 12:37:57 +0000 | [diff] [blame] | 1332 | ** left holding a NULL. It is an error for register ranges |
drh | 079a307 | 2014-03-19 14:10:55 +0000 | [diff] [blame] | 1333 | ** P1..P1+P3-1 and P2..P2+P3-1 to overlap. It is an error |
| 1334 | ** for P3 to be less than 1. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1335 | */ |
drh | e1349cb | 2008-04-01 00:36:10 +0000 | [diff] [blame] | 1336 | case OP_Move: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1337 | int n; /* Number of registers left to copy */ |
| 1338 | int p1; /* Register to copy from */ |
| 1339 | int p2; /* Register to copy to */ |
| 1340 | |
drh | e09f43f | 2013-11-21 04:18:31 +0000 | [diff] [blame] | 1341 | n = pOp->p3; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1342 | p1 = pOp->p1; |
| 1343 | p2 = pOp->p2; |
drh | 079a307 | 2014-03-19 14:10:55 +0000 | [diff] [blame] | 1344 | assert( n>0 && p1>0 && p2>0 ); |
drh | b21e7c7 | 2008-06-22 12:37:57 +0000 | [diff] [blame] | 1345 | assert( p1+n<=p2 || p2+n<=p1 ); |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1346 | |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 1347 | pIn1 = &aMem[p1]; |
| 1348 | pOut = &aMem[p2]; |
drh | e09f43f | 2013-11-21 04:18:31 +0000 | [diff] [blame] | 1349 | do{ |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 1350 | assert( pOut<=&aMem[(p->nMem+1 - p->nCursor)] ); |
| 1351 | assert( pIn1<=&aMem[(p->nMem+1 - p->nCursor)] ); |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 1352 | assert( memIsValid(pIn1) ); |
| 1353 | memAboutToChange(p, pOut); |
drh | 17bcb10 | 2014-09-18 21:25:33 +0000 | [diff] [blame] | 1354 | sqlite3VdbeMemMove(pOut, pIn1); |
drh | 52043d7 | 2011-08-03 16:40:15 +0000 | [diff] [blame] | 1355 | #ifdef SQLITE_DEBUG |
drh | 4cbd847 | 2020-01-02 15:02:08 +0000 | [diff] [blame] | 1356 | pIn1->pScopyFrom = 0; |
| 1357 | { int i; |
| 1358 | for(i=1; i<p->nMem; i++){ |
| 1359 | if( aMem[i].pScopyFrom==pIn1 ){ |
| 1360 | aMem[i].pScopyFrom = pOut; |
| 1361 | } |
| 1362 | } |
drh | 52043d7 | 2011-08-03 16:40:15 +0000 | [diff] [blame] | 1363 | } |
| 1364 | #endif |
drh | bd6789e | 2015-04-28 14:00:02 +0000 | [diff] [blame] | 1365 | Deephemeralize(pOut); |
drh | b21e7c7 | 2008-06-22 12:37:57 +0000 | [diff] [blame] | 1366 | REGISTER_TRACE(p2++, pOut); |
| 1367 | pIn1++; |
| 1368 | pOut++; |
drh | 079a307 | 2014-03-19 14:10:55 +0000 | [diff] [blame] | 1369 | }while( --n ); |
drh | e1349cb | 2008-04-01 00:36:10 +0000 | [diff] [blame] | 1370 | break; |
| 1371 | } |
| 1372 | |
drh | e8e4af7 | 2012-09-21 00:04:28 +0000 | [diff] [blame] | 1373 | /* Opcode: Copy P1 P2 P3 * * |
drh | 4eded60 | 2013-12-20 15:59:20 +0000 | [diff] [blame] | 1374 | ** Synopsis: r[P2@P3+1]=r[P1@P3+1] |
drh | b1fdb2a | 2008-01-05 04:06:03 +0000 | [diff] [blame] | 1375 | ** |
drh | e8e4af7 | 2012-09-21 00:04:28 +0000 | [diff] [blame] | 1376 | ** Make a copy of registers P1..P1+P3 into registers P2..P2+P3. |
drh | b1fdb2a | 2008-01-05 04:06:03 +0000 | [diff] [blame] | 1377 | ** |
| 1378 | ** This instruction makes a deep copy of the value. A duplicate |
| 1379 | ** is made of any string or blob constant. See also OP_SCopy. |
| 1380 | */ |
drh | e8e4af7 | 2012-09-21 00:04:28 +0000 | [diff] [blame] | 1381 | case OP_Copy: { |
| 1382 | int n; |
| 1383 | |
| 1384 | n = pOp->p3; |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 1385 | pIn1 = &aMem[pOp->p1]; |
| 1386 | pOut = &aMem[pOp->p2]; |
drh | e1349cb | 2008-04-01 00:36:10 +0000 | [diff] [blame] | 1387 | assert( pOut!=pIn1 ); |
drh | e8e4af7 | 2012-09-21 00:04:28 +0000 | [diff] [blame] | 1388 | while( 1 ){ |
drh | 58773a5 | 2018-06-12 13:52:23 +0000 | [diff] [blame] | 1389 | memAboutToChange(p, pOut); |
drh | e8e4af7 | 2012-09-21 00:04:28 +0000 | [diff] [blame] | 1390 | sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem); |
| 1391 | Deephemeralize(pOut); |
drh | 953f761 | 2012-12-07 22:18:54 +0000 | [diff] [blame] | 1392 | #ifdef SQLITE_DEBUG |
| 1393 | pOut->pScopyFrom = 0; |
| 1394 | #endif |
drh | e8e4af7 | 2012-09-21 00:04:28 +0000 | [diff] [blame] | 1395 | REGISTER_TRACE(pOp->p2+pOp->p3-n, pOut); |
| 1396 | if( (n--)==0 ) break; |
| 1397 | pOut++; |
| 1398 | pIn1++; |
| 1399 | } |
drh | e1349cb | 2008-04-01 00:36:10 +0000 | [diff] [blame] | 1400 | break; |
| 1401 | } |
| 1402 | |
drh | b1fdb2a | 2008-01-05 04:06:03 +0000 | [diff] [blame] | 1403 | /* Opcode: SCopy P1 P2 * * * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 1404 | ** Synopsis: r[P2]=r[P1] |
drh | b1fdb2a | 2008-01-05 04:06:03 +0000 | [diff] [blame] | 1405 | ** |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1406 | ** Make a shallow copy of register P1 into register P2. |
drh | b1fdb2a | 2008-01-05 04:06:03 +0000 | [diff] [blame] | 1407 | ** |
| 1408 | ** This instruction makes a shallow copy of the value. If the value |
| 1409 | ** is a string or blob, then the copy is only a pointer to the |
| 1410 | ** original and hence if the original changes so will the copy. |
| 1411 | ** Worse, if the original is deallocated, the copy becomes invalid. |
| 1412 | ** Thus the program must guarantee that the original will not change |
| 1413 | ** during the lifetime of the copy. Use OP_Copy to make a complete |
| 1414 | ** copy. |
| 1415 | */ |
drh | 26198bb | 2013-10-31 11:15:09 +0000 | [diff] [blame] | 1416 | case OP_SCopy: { /* out2 */ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 1417 | pIn1 = &aMem[pOp->p1]; |
| 1418 | pOut = &aMem[pOp->p2]; |
drh | 2d401ab | 2008-01-10 23:50:11 +0000 | [diff] [blame] | 1419 | assert( pOut!=pIn1 ); |
drh | e1349cb | 2008-04-01 00:36:10 +0000 | [diff] [blame] | 1420 | sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem); |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 1421 | #ifdef SQLITE_DEBUG |
drh | 58773a5 | 2018-06-12 13:52:23 +0000 | [diff] [blame] | 1422 | pOut->pScopyFrom = pIn1; |
| 1423 | pOut->mScopyFlags = pIn1->flags; |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 1424 | #endif |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1425 | break; |
| 1426 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1427 | |
drh | fed7ac6 | 2015-10-15 18:04:59 +0000 | [diff] [blame] | 1428 | /* Opcode: IntCopy P1 P2 * * * |
| 1429 | ** Synopsis: r[P2]=r[P1] |
| 1430 | ** |
| 1431 | ** Transfer the integer value held in register P1 into register P2. |
| 1432 | ** |
| 1433 | ** This is an optimized version of SCopy that works only for integer |
| 1434 | ** values. |
| 1435 | */ |
| 1436 | case OP_IntCopy: { /* out2 */ |
| 1437 | pIn1 = &aMem[pOp->p1]; |
| 1438 | assert( (pIn1->flags & MEM_Int)!=0 ); |
| 1439 | pOut = &aMem[pOp->p2]; |
| 1440 | sqlite3VdbeMemSetInt64(pOut, pIn1->u.i); |
| 1441 | break; |
| 1442 | } |
| 1443 | |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1444 | /* Opcode: ResultRow P1 P2 * * * |
drh | 72e26de | 2016-08-24 21:24:04 +0000 | [diff] [blame] | 1445 | ** Synopsis: output=r[P1@P2] |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 1446 | ** |
shane | 21e7feb | 2008-05-30 15:59:49 +0000 | [diff] [blame] | 1447 | ** The registers P1 through P1+P2-1 contain a single row of |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 1448 | ** results. This opcode causes the sqlite3_step() call to terminate |
| 1449 | ** with an SQLITE_ROW return code and it sets up the sqlite3_stmt |
drh | 4d87aae | 2014-02-20 19:42:00 +0000 | [diff] [blame] | 1450 | ** 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] | 1451 | ** the result row. |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 1452 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1453 | case OP_ResultRow: { |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 1454 | Mem *pMem; |
| 1455 | int i; |
| 1456 | assert( p->nResColumn==pOp->p2 ); |
drh | 0a07c10 | 2008-01-03 18:03:08 +0000 | [diff] [blame] | 1457 | assert( pOp->p1>0 ); |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 1458 | assert( pOp->p1+pOp->p2<=(p->nMem+1 - p->nCursor)+1 ); |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 1459 | |
dan | 32b09f2 | 2009-09-23 17:29:59 +0000 | [diff] [blame] | 1460 | /* If this statement has violated immediate foreign key constraints, do |
| 1461 | ** not return the number of rows modified. And do not RELEASE the statement |
| 1462 | ** transaction. It needs to be rolled back. */ |
| 1463 | if( SQLITE_OK!=(rc = sqlite3VdbeCheckFk(p, 0)) ){ |
| 1464 | assert( db->flags&SQLITE_CountRows ); |
| 1465 | assert( p->usesStmtJournal ); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 1466 | goto abort_due_to_error; |
dan | 32b09f2 | 2009-09-23 17:29:59 +0000 | [diff] [blame] | 1467 | } |
| 1468 | |
danielk1977 | bd43455 | 2009-03-18 10:33:00 +0000 | [diff] [blame] | 1469 | /* If the SQLITE_CountRows flag is set in sqlite3.flags mask, then |
| 1470 | ** DML statements invoke this opcode to return the number of rows |
| 1471 | ** modified to the user. This is the only way that a VM that |
| 1472 | ** opens a statement transaction may invoke this opcode. |
| 1473 | ** |
| 1474 | ** In case this is such a statement, close any statement transaction |
| 1475 | ** opened by this VM before returning control to the user. This is to |
| 1476 | ** ensure that statement-transactions are always nested, not overlapping. |
| 1477 | ** If the open statement-transaction is not closed here, then the user |
| 1478 | ** may step another VM that opens its own statement transaction. This |
| 1479 | ** may lead to overlapping statement transactions. |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 1480 | ** |
| 1481 | ** The statement transaction is never a top-level transaction. Hence |
| 1482 | ** the RELEASE call below can never fail. |
danielk1977 | bd43455 | 2009-03-18 10:33:00 +0000 | [diff] [blame] | 1483 | */ |
| 1484 | assert( p->iStatement==0 || db->flags&SQLITE_CountRows ); |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 1485 | rc = sqlite3VdbeCloseStatement(p, SAVEPOINT_RELEASE); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 1486 | assert( rc==SQLITE_OK ); |
danielk1977 | bd43455 | 2009-03-18 10:33:00 +0000 | [diff] [blame] | 1487 | |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 1488 | /* Invalidate all ephemeral cursor row caches */ |
| 1489 | p->cacheCtr = (p->cacheCtr + 2)|1; |
| 1490 | |
| 1491 | /* Make sure the results of the current row are \000 terminated |
shane | 21e7feb | 2008-05-30 15:59:49 +0000 | [diff] [blame] | 1492 | ** and have an assigned type. The results are de-ephemeralized as |
drh | b8a45bb | 2011-12-31 21:51:55 +0000 | [diff] [blame] | 1493 | ** a side effect. |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 1494 | */ |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 1495 | pMem = p->pResultSet = &aMem[pOp->p1]; |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 1496 | for(i=0; i<pOp->p2; i++){ |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 1497 | assert( memIsValid(&pMem[i]) ); |
drh | ebc1671 | 2010-09-28 00:25:58 +0000 | [diff] [blame] | 1498 | Deephemeralize(&pMem[i]); |
drh | 746fd9c | 2010-09-28 06:00:47 +0000 | [diff] [blame] | 1499 | assert( (pMem[i].flags & MEM_Ephem)==0 |
| 1500 | || (pMem[i].flags & (MEM_Str|MEM_Blob))==0 ); |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 1501 | sqlite3VdbeMemNulTerminate(&pMem[i]); |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 1502 | REGISTER_TRACE(pOp->p1+i, &pMem[i]); |
drh | 02ff747 | 2019-12-31 12:18:24 +0000 | [diff] [blame] | 1503 | #ifdef SQLITE_DEBUG |
| 1504 | /* The registers in the result will not be used again when the |
| 1505 | ** prepared statement restarts. This is because sqlite3_column() |
| 1506 | ** APIs might have caused type conversions of made other changes to |
| 1507 | ** the register values. Therefore, we can go ahead and break any |
| 1508 | ** OP_SCopy dependencies. */ |
| 1509 | pMem[i].pScopyFrom = 0; |
| 1510 | #endif |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 1511 | } |
drh | 2803969 | 2008-03-17 16:54:01 +0000 | [diff] [blame] | 1512 | if( db->mallocFailed ) goto no_mem; |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 1513 | |
drh | 3d2a529 | 2016-07-13 22:55:01 +0000 | [diff] [blame] | 1514 | if( db->mTrace & SQLITE_TRACE_ROW ){ |
| 1515 | db->xTrace(SQLITE_TRACE_ROW, db->pTraceArg, p, 0); |
| 1516 | } |
| 1517 | |
drh | 02ff747 | 2019-12-31 12:18:24 +0000 | [diff] [blame] | 1518 | |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 1519 | /* Return SQLITE_ROW |
| 1520 | */ |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 1521 | p->pc = (int)(pOp - aOp) + 1; |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 1522 | rc = SQLITE_ROW; |
| 1523 | goto vdbe_return; |
| 1524 | } |
| 1525 | |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1526 | /* Opcode: Concat P1 P2 P3 * * |
drh | 313619f | 2013-10-31 20:34:06 +0000 | [diff] [blame] | 1527 | ** Synopsis: r[P3]=r[P2]+r[P1] |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1528 | ** |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1529 | ** Add the text in register P1 onto the end of the text in |
| 1530 | ** register P2 and store the result in register P3. |
| 1531 | ** 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] | 1532 | ** |
| 1533 | ** P3 = P2 || P1 |
| 1534 | ** |
| 1535 | ** It is illegal for P1 and P3 to be the same register. Sometimes, |
| 1536 | ** if P3 is the same register as P2, the implementation is able |
| 1537 | ** to avoid a memcpy(). |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1538 | */ |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1539 | case OP_Concat: { /* same as TK_CONCAT, in1, in2, out3 */ |
drh | 8a7e11f | 2019-05-01 15:32:40 +0000 | [diff] [blame] | 1540 | i64 nByte; /* Total size of the output string or blob */ |
| 1541 | u16 flags1; /* Initial flags for P1 */ |
| 1542 | u16 flags2; /* Initial flags for P2 */ |
danielk1977 | 8a6b541 | 2004-05-24 07:04:25 +0000 | [diff] [blame] | 1543 | |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 1544 | pIn1 = &aMem[pOp->p1]; |
| 1545 | pIn2 = &aMem[pOp->p2]; |
| 1546 | pOut = &aMem[pOp->p3]; |
drh | 8a7e11f | 2019-05-01 15:32:40 +0000 | [diff] [blame] | 1547 | testcase( pOut==pIn2 ); |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1548 | assert( pIn1!=pOut ); |
drh | 8a7e11f | 2019-05-01 15:32:40 +0000 | [diff] [blame] | 1549 | flags1 = pIn1->flags; |
| 1550 | testcase( flags1 & MEM_Null ); |
| 1551 | testcase( pIn2->flags & MEM_Null ); |
| 1552 | if( (flags1 | pIn2->flags) & MEM_Null ){ |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1553 | sqlite3VdbeMemSetNull(pOut); |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1554 | break; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1555 | } |
drh | 8a7e11f | 2019-05-01 15:32:40 +0000 | [diff] [blame] | 1556 | if( (flags1 & (MEM_Str|MEM_Blob))==0 ){ |
| 1557 | if( sqlite3VdbeMemStringify(pIn1,encoding,0) ) goto no_mem; |
drh | 01325a3 | 2019-05-02 00:52:50 +0000 | [diff] [blame] | 1558 | flags1 = pIn1->flags & ~MEM_Str; |
drh | 8a7e11f | 2019-05-01 15:32:40 +0000 | [diff] [blame] | 1559 | }else if( (flags1 & MEM_Zero)!=0 ){ |
| 1560 | if( sqlite3VdbeMemExpandBlob(pIn1) ) goto no_mem; |
drh | 01325a3 | 2019-05-02 00:52:50 +0000 | [diff] [blame] | 1561 | flags1 = pIn1->flags & ~MEM_Str; |
drh | 8a7e11f | 2019-05-01 15:32:40 +0000 | [diff] [blame] | 1562 | } |
| 1563 | flags2 = pIn2->flags; |
| 1564 | if( (flags2 & (MEM_Str|MEM_Blob))==0 ){ |
| 1565 | if( sqlite3VdbeMemStringify(pIn2,encoding,0) ) goto no_mem; |
drh | 01325a3 | 2019-05-02 00:52:50 +0000 | [diff] [blame] | 1566 | flags2 = pIn2->flags & ~MEM_Str; |
drh | 8a7e11f | 2019-05-01 15:32:40 +0000 | [diff] [blame] | 1567 | }else if( (flags2 & MEM_Zero)!=0 ){ |
| 1568 | if( sqlite3VdbeMemExpandBlob(pIn2) ) goto no_mem; |
drh | 01325a3 | 2019-05-02 00:52:50 +0000 | [diff] [blame] | 1569 | flags2 = pIn2->flags & ~MEM_Str; |
drh | 8a7e11f | 2019-05-01 15:32:40 +0000 | [diff] [blame] | 1570 | } |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1571 | nByte = pIn1->n + pIn2->n; |
drh | bb4957f | 2008-03-20 14:03:29 +0000 | [diff] [blame] | 1572 | if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){ |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1573 | goto too_big; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1574 | } |
drh | df82afc | 2019-05-16 01:22:21 +0000 | [diff] [blame] | 1575 | if( sqlite3VdbeMemGrow(pOut, (int)nByte+3, pOut==pIn2) ){ |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1576 | goto no_mem; |
| 1577 | } |
drh | c91b2fd | 2014-03-01 18:13:23 +0000 | [diff] [blame] | 1578 | MemSetTypeFlag(pOut, MEM_Str); |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1579 | if( pOut!=pIn2 ){ |
| 1580 | memcpy(pOut->z, pIn2->z, pIn2->n); |
drh | 8a7e11f | 2019-05-01 15:32:40 +0000 | [diff] [blame] | 1581 | assert( (pIn2->flags & MEM_Dyn) == (flags2 & MEM_Dyn) ); |
| 1582 | pIn2->flags = flags2; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1583 | } |
| 1584 | memcpy(&pOut->z[pIn2->n], pIn1->z, pIn1->n); |
drh | 8a7e11f | 2019-05-01 15:32:40 +0000 | [diff] [blame] | 1585 | assert( (pIn1->flags & MEM_Dyn) == (flags1 & MEM_Dyn) ); |
| 1586 | pIn1->flags = flags1; |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 1587 | pOut->z[nByte]=0; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1588 | pOut->z[nByte+1] = 0; |
drh | df82afc | 2019-05-16 01:22:21 +0000 | [diff] [blame] | 1589 | pOut->z[nByte+2] = 0; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1590 | pOut->flags |= MEM_Term; |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 1591 | pOut->n = (int)nByte; |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1592 | pOut->enc = encoding; |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 1593 | UPDATE_MAX_BLOBSIZE(pOut); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1594 | break; |
| 1595 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1596 | |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 1597 | /* Opcode: Add P1 P2 P3 * * |
drh | 72e26de | 2016-08-24 21:24:04 +0000 | [diff] [blame] | 1598 | ** Synopsis: r[P3]=r[P1]+r[P2] |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1599 | ** |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1600 | ** Add the value in register P1 to the value in register P2 |
shane | 21e7feb | 2008-05-30 15:59:49 +0000 | [diff] [blame] | 1601 | ** and store the result in register P3. |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1602 | ** If either input is NULL, the result is NULL. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1603 | */ |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 1604 | /* Opcode: Multiply P1 P2 P3 * * |
drh | 72e26de | 2016-08-24 21:24:04 +0000 | [diff] [blame] | 1605 | ** Synopsis: r[P3]=r[P1]*r[P2] |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1606 | ** |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 1607 | ** |
shane | 21e7feb | 2008-05-30 15:59:49 +0000 | [diff] [blame] | 1608 | ** Multiply the value in register P1 by the value in register P2 |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1609 | ** and store the result in register P3. |
| 1610 | ** If either input is NULL, the result is NULL. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1611 | */ |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 1612 | /* Opcode: Subtract P1 P2 P3 * * |
drh | 72e26de | 2016-08-24 21:24:04 +0000 | [diff] [blame] | 1613 | ** Synopsis: r[P3]=r[P2]-r[P1] |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1614 | ** |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1615 | ** Subtract the value in register P1 from the value in register P2 |
| 1616 | ** and store the result in register P3. |
| 1617 | ** If either input is NULL, the result is NULL. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1618 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1619 | /* Opcode: Divide P1 P2 P3 * * |
drh | 72e26de | 2016-08-24 21:24:04 +0000 | [diff] [blame] | 1620 | ** Synopsis: r[P3]=r[P2]/r[P1] |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1621 | ** |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1622 | ** Divide the value in register P1 by the value in register P2 |
dan | e275dc3 | 2009-08-18 16:24:58 +0000 | [diff] [blame] | 1623 | ** and store the result in register P3 (P3=P2/P1). If the value in |
| 1624 | ** register P1 is zero, then the result is NULL. If either input is |
| 1625 | ** NULL, the result is NULL. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1626 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1627 | /* Opcode: Remainder P1 P2 P3 * * |
drh | 72e26de | 2016-08-24 21:24:04 +0000 | [diff] [blame] | 1628 | ** Synopsis: r[P3]=r[P2]%r[P1] |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1629 | ** |
drh | 40864a1 | 2013-11-15 18:58:37 +0000 | [diff] [blame] | 1630 | ** Compute the remainder after integer register P2 is divided by |
| 1631 | ** register P1 and store the result in register P3. |
| 1632 | ** If the value in register P1 is zero the result is NULL. |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1633 | ** If either operand is NULL, the result is NULL. |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1634 | */ |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1635 | case OP_Add: /* same as TK_PLUS, in1, in2, out3 */ |
| 1636 | case OP_Subtract: /* same as TK_MINUS, in1, in2, out3 */ |
| 1637 | case OP_Multiply: /* same as TK_STAR, in1, in2, out3 */ |
| 1638 | case OP_Divide: /* same as TK_SLASH, in1, in2, out3 */ |
| 1639 | case OP_Remainder: { /* same as TK_REM, in1, in2, out3 */ |
drh | 3d1d90a | 2014-03-24 15:00:15 +0000 | [diff] [blame] | 1640 | u16 flags; /* Combined MEM_* flags from both inputs */ |
| 1641 | u16 type1; /* Numeric type of left operand */ |
| 1642 | u16 type2; /* Numeric type of right operand */ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1643 | i64 iA; /* Integer value of left operand */ |
| 1644 | i64 iB; /* Integer value of right operand */ |
| 1645 | double rA; /* Real value of left operand */ |
| 1646 | double rB; /* Real value of right operand */ |
| 1647 | |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 1648 | pIn1 = &aMem[pOp->p1]; |
drh | 3d1d90a | 2014-03-24 15:00:15 +0000 | [diff] [blame] | 1649 | type1 = numericType(pIn1); |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 1650 | pIn2 = &aMem[pOp->p2]; |
drh | 3d1d90a | 2014-03-24 15:00:15 +0000 | [diff] [blame] | 1651 | type2 = numericType(pIn2); |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 1652 | pOut = &aMem[pOp->p3]; |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1653 | flags = pIn1->flags | pIn2->flags; |
drh | 3d1d90a | 2014-03-24 15:00:15 +0000 | [diff] [blame] | 1654 | if( (type1 & type2 & MEM_Int)!=0 ){ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1655 | iA = pIn1->u.i; |
| 1656 | iB = pIn2->u.i; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1657 | switch( pOp->opcode ){ |
drh | 158b9cb | 2011-03-05 20:59:46 +0000 | [diff] [blame] | 1658 | case OP_Add: if( sqlite3AddInt64(&iB,iA) ) goto fp_math; break; |
| 1659 | case OP_Subtract: if( sqlite3SubInt64(&iB,iA) ) goto fp_math; break; |
| 1660 | case OP_Multiply: if( sqlite3MulInt64(&iB,iA) ) goto fp_math; break; |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1661 | case OP_Divide: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1662 | if( iA==0 ) goto arithmetic_result_is_null; |
drh | 158b9cb | 2011-03-05 20:59:46 +0000 | [diff] [blame] | 1663 | if( iA==-1 && iB==SMALLEST_INT64 ) goto fp_math; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1664 | iB /= iA; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1665 | break; |
| 1666 | } |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1667 | default: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1668 | if( iA==0 ) goto arithmetic_result_is_null; |
| 1669 | if( iA==-1 ) iA = 1; |
| 1670 | iB %= iA; |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1671 | break; |
| 1672 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1673 | } |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1674 | pOut->u.i = iB; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1675 | MemSetTypeFlag(pOut, MEM_Int); |
drh | cfcca02 | 2017-04-17 23:23:17 +0000 | [diff] [blame] | 1676 | }else if( (flags & MEM_Null)!=0 ){ |
| 1677 | goto arithmetic_result_is_null; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1678 | }else{ |
drh | 158b9cb | 2011-03-05 20:59:46 +0000 | [diff] [blame] | 1679 | fp_math: |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1680 | rA = sqlite3VdbeRealValue(pIn1); |
| 1681 | rB = sqlite3VdbeRealValue(pIn2); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1682 | switch( pOp->opcode ){ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1683 | case OP_Add: rB += rA; break; |
| 1684 | case OP_Subtract: rB -= rA; break; |
| 1685 | case OP_Multiply: rB *= rA; break; |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1686 | case OP_Divide: { |
shane | fbd60f8 | 2009-02-04 03:59:25 +0000 | [diff] [blame] | 1687 | /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1688 | if( rA==(double)0 ) goto arithmetic_result_is_null; |
| 1689 | rB /= rA; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1690 | break; |
| 1691 | } |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1692 | default: { |
drh | e3b89d2 | 2019-01-18 17:53:50 +0000 | [diff] [blame] | 1693 | iA = sqlite3VdbeIntValue(pIn1); |
| 1694 | iB = sqlite3VdbeIntValue(pIn2); |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1695 | if( iA==0 ) goto arithmetic_result_is_null; |
| 1696 | if( iA==-1 ) iA = 1; |
| 1697 | rB = (double)(iB % iA); |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1698 | break; |
| 1699 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1700 | } |
drh | c5a7b51 | 2010-01-13 16:25:42 +0000 | [diff] [blame] | 1701 | #ifdef SQLITE_OMIT_FLOATING_POINT |
| 1702 | pOut->u.i = rB; |
| 1703 | MemSetTypeFlag(pOut, MEM_Int); |
| 1704 | #else |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1705 | if( sqlite3IsNaN(rB) ){ |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 1706 | goto arithmetic_result_is_null; |
drh | 53c1402 | 2007-05-10 17:23:11 +0000 | [diff] [blame] | 1707 | } |
drh | 74eaba4 | 2014-09-18 17:52:15 +0000 | [diff] [blame] | 1708 | pOut->u.r = rB; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1709 | MemSetTypeFlag(pOut, MEM_Real); |
drh | c5a7b51 | 2010-01-13 16:25:42 +0000 | [diff] [blame] | 1710 | #endif |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1711 | } |
| 1712 | break; |
| 1713 | |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 1714 | arithmetic_result_is_null: |
| 1715 | sqlite3VdbeMemSetNull(pOut); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1716 | break; |
| 1717 | } |
| 1718 | |
drh | 7a95789 | 2012-02-02 17:35:43 +0000 | [diff] [blame] | 1719 | /* Opcode: CollSeq P1 * * P4 |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 1720 | ** |
drh | bb6783b | 2017-04-29 18:02:49 +0000 | [diff] [blame] | 1721 | ** 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] | 1722 | ** or aggregate calls sqlite3GetFuncCollSeq(), this collation sequence will |
| 1723 | ** be returned. This is used by the built-in min(), max() and nullif() |
drh | e6f85e7 | 2004-12-25 01:03:13 +0000 | [diff] [blame] | 1724 | ** functions. |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 1725 | ** |
drh | 7a95789 | 2012-02-02 17:35:43 +0000 | [diff] [blame] | 1726 | ** If P1 is not zero, then it is a register that a subsequent min() or |
| 1727 | ** max() aggregate will set to 1 if the current row is not the minimum or |
| 1728 | ** maximum. The P1 register is initialized to 0 by this instruction. |
| 1729 | ** |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 1730 | ** The interface used by the implementation of the aforementioned functions |
| 1731 | ** to retrieve the collation sequence set by this opcode is not available |
drh | 0a0d056 | 2015-03-12 05:08:34 +0000 | [diff] [blame] | 1732 | ** publicly. Only built-in functions have access to this feature. |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 1733 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1734 | case OP_CollSeq: { |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 1735 | assert( pOp->p4type==P4_COLLSEQ ); |
drh | 7a95789 | 2012-02-02 17:35:43 +0000 | [diff] [blame] | 1736 | if( pOp->p1 ){ |
| 1737 | sqlite3VdbeMemSetInt64(&aMem[pOp->p1], 0); |
| 1738 | } |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 1739 | break; |
| 1740 | } |
| 1741 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1742 | /* Opcode: BitAnd P1 P2 P3 * * |
drh | 72e26de | 2016-08-24 21:24:04 +0000 | [diff] [blame] | 1743 | ** Synopsis: r[P3]=r[P1]&r[P2] |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1744 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1745 | ** Take the bit-wise AND of the values in register P1 and P2 and |
| 1746 | ** store the result in register P3. |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1747 | ** If either input is NULL, the result is NULL. |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1748 | */ |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1749 | /* Opcode: BitOr P1 P2 P3 * * |
drh | 72e26de | 2016-08-24 21:24:04 +0000 | [diff] [blame] | 1750 | ** Synopsis: r[P3]=r[P1]|r[P2] |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1751 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1752 | ** Take the bit-wise OR of the values in register P1 and P2 and |
| 1753 | ** store the result in register P3. |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1754 | ** If either input is NULL, the result is NULL. |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1755 | */ |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1756 | /* Opcode: ShiftLeft P1 P2 P3 * * |
drh | 72e26de | 2016-08-24 21:24:04 +0000 | [diff] [blame] | 1757 | ** Synopsis: r[P3]=r[P2]<<r[P1] |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1758 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1759 | ** Shift the integer value in register P2 to the left by the |
drh | 710c484 | 2010-08-30 01:17:20 +0000 | [diff] [blame] | 1760 | ** number of bits specified by the integer in register P1. |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1761 | ** Store the result in register P3. |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1762 | ** If either input is NULL, the result is NULL. |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1763 | */ |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1764 | /* Opcode: ShiftRight P1 P2 P3 * * |
drh | 72e26de | 2016-08-24 21:24:04 +0000 | [diff] [blame] | 1765 | ** Synopsis: r[P3]=r[P2]>>r[P1] |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1766 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1767 | ** Shift the integer value in register P2 to the right by the |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1768 | ** number of bits specified by the integer in register P1. |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1769 | ** Store the result in register P3. |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1770 | ** If either input is NULL, the result is NULL. |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1771 | */ |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1772 | case OP_BitAnd: /* same as TK_BITAND, in1, in2, out3 */ |
| 1773 | case OP_BitOr: /* same as TK_BITOR, in1, in2, out3 */ |
| 1774 | case OP_ShiftLeft: /* same as TK_LSHIFT, in1, in2, out3 */ |
| 1775 | case OP_ShiftRight: { /* same as TK_RSHIFT, in1, in2, out3 */ |
drh | 158b9cb | 2011-03-05 20:59:46 +0000 | [diff] [blame] | 1776 | i64 iA; |
| 1777 | u64 uA; |
| 1778 | i64 iB; |
| 1779 | u8 op; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1780 | |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 1781 | pIn1 = &aMem[pOp->p1]; |
| 1782 | pIn2 = &aMem[pOp->p2]; |
| 1783 | pOut = &aMem[pOp->p3]; |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1784 | if( (pIn1->flags | pIn2->flags) & MEM_Null ){ |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 1785 | sqlite3VdbeMemSetNull(pOut); |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1786 | break; |
| 1787 | } |
drh | 158b9cb | 2011-03-05 20:59:46 +0000 | [diff] [blame] | 1788 | iA = sqlite3VdbeIntValue(pIn2); |
| 1789 | iB = sqlite3VdbeIntValue(pIn1); |
| 1790 | op = pOp->opcode; |
| 1791 | if( op==OP_BitAnd ){ |
| 1792 | iA &= iB; |
| 1793 | }else if( op==OP_BitOr ){ |
| 1794 | iA |= iB; |
| 1795 | }else if( iB!=0 ){ |
| 1796 | assert( op==OP_ShiftRight || op==OP_ShiftLeft ); |
| 1797 | |
| 1798 | /* If shifting by a negative amount, shift in the other direction */ |
| 1799 | if( iB<0 ){ |
| 1800 | assert( OP_ShiftRight==OP_ShiftLeft+1 ); |
| 1801 | op = 2*OP_ShiftLeft + 1 - op; |
| 1802 | iB = iB>(-64) ? -iB : 64; |
| 1803 | } |
| 1804 | |
| 1805 | if( iB>=64 ){ |
| 1806 | iA = (iA>=0 || op==OP_ShiftLeft) ? 0 : -1; |
| 1807 | }else{ |
| 1808 | memcpy(&uA, &iA, sizeof(uA)); |
| 1809 | if( op==OP_ShiftLeft ){ |
| 1810 | uA <<= iB; |
| 1811 | }else{ |
| 1812 | uA >>= iB; |
| 1813 | /* Sign-extend on a right shift of a negative number */ |
| 1814 | if( iA<0 ) uA |= ((((u64)0xffffffff)<<32)|0xffffffff) << (64-iB); |
| 1815 | } |
| 1816 | memcpy(&iA, &uA, sizeof(iA)); |
| 1817 | } |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1818 | } |
drh | 158b9cb | 2011-03-05 20:59:46 +0000 | [diff] [blame] | 1819 | pOut->u.i = iA; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1820 | MemSetTypeFlag(pOut, MEM_Int); |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1821 | break; |
| 1822 | } |
| 1823 | |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1824 | /* Opcode: AddImm P1 P2 * * * |
drh | 72e26de | 2016-08-24 21:24:04 +0000 | [diff] [blame] | 1825 | ** Synopsis: r[P1]=r[P1]+P2 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1826 | ** |
danielk1977 | 0cdc022 | 2008-06-26 18:04:03 +0000 | [diff] [blame] | 1827 | ** Add the constant P2 to the value in register P1. |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1828 | ** The result is always an integer. |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 1829 | ** |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1830 | ** To force any register to be an integer, just add 0. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1831 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1832 | case OP_AddImm: { /* in1 */ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 1833 | pIn1 = &aMem[pOp->p1]; |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 1834 | memAboutToChange(p, pIn1); |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1835 | sqlite3VdbeMemIntegerify(pIn1); |
| 1836 | pIn1->u.i += pOp->p2; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1837 | break; |
| 1838 | } |
| 1839 | |
dan | e5166e0 | 2019-03-19 11:56:39 +0000 | [diff] [blame] | 1840 | /* Opcode: MustBeInt P1 P2 * * * |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 1841 | ** |
dan | e5166e0 | 2019-03-19 11:56:39 +0000 | [diff] [blame] | 1842 | ** Force the value in register P1 to be an integer. If the value |
| 1843 | ** in P1 is not an integer and cannot be converted into an integer |
| 1844 | ** without data loss, then jump immediately to P2, or if P2==0 |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 1845 | ** raise an SQLITE_MISMATCH exception. |
| 1846 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1847 | case OP_MustBeInt: { /* jump, in1 */ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 1848 | pIn1 = &aMem[pOp->p1]; |
dan | e5166e0 | 2019-03-19 11:56:39 +0000 | [diff] [blame] | 1849 | if( (pIn1->flags & MEM_Int)==0 ){ |
drh | 83b301b | 2013-11-20 00:59:02 +0000 | [diff] [blame] | 1850 | applyAffinity(pIn1, SQLITE_AFF_NUMERIC, encoding); |
dan | e5166e0 | 2019-03-19 11:56:39 +0000 | [diff] [blame] | 1851 | if( (pIn1->flags & MEM_Int)==0 ){ |
drh | c906533 | 2019-04-01 14:01:21 +0000 | [diff] [blame] | 1852 | VdbeBranchTaken(1, 2); |
drh | 83b301b | 2013-11-20 00:59:02 +0000 | [diff] [blame] | 1853 | if( pOp->p2==0 ){ |
| 1854 | rc = SQLITE_MISMATCH; |
| 1855 | goto abort_due_to_error; |
| 1856 | }else{ |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 1857 | goto jump_to_p2; |
drh | 83b301b | 2013-11-20 00:59:02 +0000 | [diff] [blame] | 1858 | } |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 1859 | } |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 1860 | } |
drh | c906533 | 2019-04-01 14:01:21 +0000 | [diff] [blame] | 1861 | VdbeBranchTaken(0, 2); |
dan | e5166e0 | 2019-03-19 11:56:39 +0000 | [diff] [blame] | 1862 | MemSetTypeFlag(pIn1, MEM_Int); |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 1863 | break; |
| 1864 | } |
| 1865 | |
drh | 13573c7 | 2010-01-12 17:04:07 +0000 | [diff] [blame] | 1866 | #ifndef SQLITE_OMIT_FLOATING_POINT |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1867 | /* Opcode: RealAffinity P1 * * * * |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 1868 | ** |
drh | 2133d82 | 2008-01-03 18:44:59 +0000 | [diff] [blame] | 1869 | ** If register P1 holds an integer convert it to a real value. |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 1870 | ** |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1871 | ** This opcode is used when extracting information from a column that |
| 1872 | ** has REAL affinity. Such column values may still be stored as |
| 1873 | ** integers, for space efficiency, but after extraction we want them |
| 1874 | ** to have only a real value. |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 1875 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1876 | case OP_RealAffinity: { /* in1 */ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 1877 | pIn1 = &aMem[pOp->p1]; |
drh | 169f077 | 2019-05-02 21:36:26 +0000 | [diff] [blame] | 1878 | if( pIn1->flags & (MEM_Int|MEM_IntReal) ){ |
drh | 3242c69 | 2019-05-04 01:29:13 +0000 | [diff] [blame] | 1879 | testcase( pIn1->flags & MEM_Int ); |
| 1880 | testcase( pIn1->flags & MEM_IntReal ); |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1881 | sqlite3VdbeMemRealify(pIn1); |
drh | efb5f9a | 2019-08-30 21:52:13 +0000 | [diff] [blame] | 1882 | REGISTER_TRACE(pOp->p1, pIn1); |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1883 | } |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 1884 | break; |
| 1885 | } |
drh | 13573c7 | 2010-01-12 17:04:07 +0000 | [diff] [blame] | 1886 | #endif |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 1887 | |
drh | 8df447f | 2005-11-01 15:48:24 +0000 | [diff] [blame] | 1888 | #ifndef SQLITE_OMIT_CAST |
drh | 4169e43 | 2014-08-25 20:11:52 +0000 | [diff] [blame] | 1889 | /* Opcode: Cast P1 P2 * * * |
mistachkin | a1dc42a | 2014-08-27 17:53:40 +0000 | [diff] [blame] | 1890 | ** Synopsis: affinity(r[P1]) |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 1891 | ** |
drh | 4169e43 | 2014-08-25 20:11:52 +0000 | [diff] [blame] | 1892 | ** Force the value in register P1 to be the type defined by P2. |
| 1893 | ** |
| 1894 | ** <ul> |
drh | bb6783b | 2017-04-29 18:02:49 +0000 | [diff] [blame] | 1895 | ** <li> P2=='A' → BLOB |
| 1896 | ** <li> P2=='B' → TEXT |
| 1897 | ** <li> P2=='C' → NUMERIC |
| 1898 | ** <li> P2=='D' → INTEGER |
| 1899 | ** <li> P2=='E' → REAL |
drh | 4169e43 | 2014-08-25 20:11:52 +0000 | [diff] [blame] | 1900 | ** </ul> |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 1901 | ** |
| 1902 | ** A NULL value is not changed by this routine. It remains NULL. |
| 1903 | */ |
drh | 4169e43 | 2014-08-25 20:11:52 +0000 | [diff] [blame] | 1904 | case OP_Cast: { /* in1 */ |
drh | 05883a3 | 2015-06-02 15:32:08 +0000 | [diff] [blame] | 1905 | assert( pOp->p2>=SQLITE_AFF_BLOB && pOp->p2<=SQLITE_AFF_REAL ); |
drh | 05bbb2e | 2014-08-25 22:37:19 +0000 | [diff] [blame] | 1906 | testcase( pOp->p2==SQLITE_AFF_TEXT ); |
drh | 05883a3 | 2015-06-02 15:32:08 +0000 | [diff] [blame] | 1907 | testcase( pOp->p2==SQLITE_AFF_BLOB ); |
drh | 05bbb2e | 2014-08-25 22:37:19 +0000 | [diff] [blame] | 1908 | testcase( pOp->p2==SQLITE_AFF_NUMERIC ); |
| 1909 | testcase( pOp->p2==SQLITE_AFF_INTEGER ); |
| 1910 | testcase( pOp->p2==SQLITE_AFF_REAL ); |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 1911 | pIn1 = &aMem[pOp->p1]; |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 1912 | memAboutToChange(p, pIn1); |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1913 | rc = ExpandBlob(pIn1); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 1914 | if( rc ) goto abort_due_to_error; |
drh | 0af6ddd | 2019-12-23 03:37:46 +0000 | [diff] [blame] | 1915 | rc = sqlite3VdbeMemCast(pIn1, pOp->p2, encoding); |
| 1916 | if( rc ) goto abort_due_to_error; |
| 1917 | UPDATE_MAX_BLOBSIZE(pIn1); |
drh | 5d73272 | 2019-12-20 17:25:10 +0000 | [diff] [blame] | 1918 | REGISTER_TRACE(pOp->p1, pIn1); |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 1919 | break; |
| 1920 | } |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1921 | #endif /* SQLITE_OMIT_CAST */ |
| 1922 | |
drh | 79752b6 | 2016-08-13 10:02:17 +0000 | [diff] [blame] | 1923 | /* Opcode: Eq P1 P2 P3 P4 P5 |
drh | 88e665f | 2016-08-27 01:41:53 +0000 | [diff] [blame] | 1924 | ** Synopsis: IF r[P3]==r[P1] |
drh | 79752b6 | 2016-08-13 10:02:17 +0000 | [diff] [blame] | 1925 | ** |
| 1926 | ** Compare the values in register P1 and P3. If reg(P3)==reg(P1) then |
| 1927 | ** jump to address P2. Or if the SQLITE_STOREP2 flag is set in P5, then |
| 1928 | ** store the result of comparison in register P2. |
| 1929 | ** |
| 1930 | ** The SQLITE_AFF_MASK portion of P5 must be an affinity character - |
| 1931 | ** SQLITE_AFF_TEXT, SQLITE_AFF_INTEGER, and so forth. An attempt is made |
| 1932 | ** to coerce both inputs according to this affinity before the |
| 1933 | ** comparison is made. If the SQLITE_AFF_MASK is 0x00, then numeric |
| 1934 | ** affinity is used. Note that the affinity conversions are stored |
| 1935 | ** back into the input registers P1 and P3. So this opcode can cause |
| 1936 | ** persistent changes to registers P1 and P3. |
| 1937 | ** |
| 1938 | ** Once any conversions have taken place, and neither value is NULL, |
| 1939 | ** the values are compared. If both values are blobs then memcmp() is |
| 1940 | ** used to determine the results of the comparison. If both values |
| 1941 | ** are text, then the appropriate collating function specified in |
| 1942 | ** P4 is used to do the comparison. If P4 is not specified then |
| 1943 | ** memcmp() is used to compare text string. If both values are |
| 1944 | ** numeric, then a numeric comparison is used. If the two values |
| 1945 | ** are of different types, then numbers are considered less than |
| 1946 | ** strings and strings are considered less than blobs. |
| 1947 | ** |
| 1948 | ** If SQLITE_NULLEQ is set in P5 then the result of comparison is always either |
| 1949 | ** true or false and is never NULL. If both operands are NULL then the result |
| 1950 | ** of comparison is true. If either operand is NULL then the result is false. |
| 1951 | ** If neither operand is NULL the result is the same as it would be if |
| 1952 | ** the SQLITE_NULLEQ flag were omitted from P5. |
| 1953 | ** |
| 1954 | ** If both SQLITE_STOREP2 and SQLITE_KEEPNULL flags are set then the |
drh | 3fffbf9 | 2016-09-05 15:02:41 +0000 | [diff] [blame] | 1955 | ** content of r[P2] is only changed if the new value is NULL or 0 (false). |
| 1956 | ** In other words, a prior r[P2] value will not be overwritten by 1 (true). |
drh | 79752b6 | 2016-08-13 10:02:17 +0000 | [diff] [blame] | 1957 | */ |
| 1958 | /* Opcode: Ne P1 P2 P3 P4 P5 |
drh | 88e665f | 2016-08-27 01:41:53 +0000 | [diff] [blame] | 1959 | ** Synopsis: IF r[P3]!=r[P1] |
drh | 79752b6 | 2016-08-13 10:02:17 +0000 | [diff] [blame] | 1960 | ** |
| 1961 | ** This works just like the Eq opcode except that the jump is taken if |
| 1962 | ** the operands in registers P1 and P3 are not equal. See the Eq opcode for |
| 1963 | ** additional information. |
| 1964 | ** |
| 1965 | ** If both SQLITE_STOREP2 and SQLITE_KEEPNULL flags are set then the |
drh | 3fffbf9 | 2016-09-05 15:02:41 +0000 | [diff] [blame] | 1966 | ** content of r[P2] is only changed if the new value is NULL or 1 (true). |
| 1967 | ** In other words, a prior r[P2] value will not be overwritten by 0 (false). |
drh | 79752b6 | 2016-08-13 10:02:17 +0000 | [diff] [blame] | 1968 | */ |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 1969 | /* Opcode: Lt P1 P2 P3 P4 P5 |
drh | 88e665f | 2016-08-27 01:41:53 +0000 | [diff] [blame] | 1970 | ** Synopsis: IF r[P3]<r[P1] |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1971 | ** |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 1972 | ** Compare the values in register P1 and P3. If reg(P3)<reg(P1) then |
drh | 79752b6 | 2016-08-13 10:02:17 +0000 | [diff] [blame] | 1973 | ** jump to address P2. Or if the SQLITE_STOREP2 flag is set in P5 store |
| 1974 | ** the result of comparison (0 or 1 or NULL) into register P2. |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1975 | ** |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 1976 | ** 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] | 1977 | ** reg(P3) is NULL then the take the jump. If the SQLITE_JUMPIFNULL |
drh | 710c484 | 2010-08-30 01:17:20 +0000 | [diff] [blame] | 1978 | ** bit is clear then fall through if either operand is NULL. |
drh | 4f68623 | 2005-09-20 13:55:18 +0000 | [diff] [blame] | 1979 | ** |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 1980 | ** The SQLITE_AFF_MASK portion of P5 must be an affinity character - |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1981 | ** SQLITE_AFF_TEXT, SQLITE_AFF_INTEGER, and so forth. An attempt is made |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1982 | ** to coerce both inputs according to this affinity before the |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 1983 | ** comparison is made. If the SQLITE_AFF_MASK is 0x00, then numeric |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1984 | ** affinity is used. Note that the affinity conversions are stored |
| 1985 | ** back into the input registers P1 and P3. So this opcode can cause |
| 1986 | ** persistent changes to registers P1 and P3. |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 1987 | ** |
| 1988 | ** Once any conversions have taken place, and neither value is NULL, |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 1989 | ** the values are compared. If both values are blobs then memcmp() is |
| 1990 | ** used to determine the results of the comparison. If both values |
| 1991 | ** are text, then the appropriate collating function specified in |
| 1992 | ** P4 is used to do the comparison. If P4 is not specified then |
| 1993 | ** memcmp() is used to compare text string. If both values are |
| 1994 | ** numeric, then a numeric comparison is used. If the two values |
| 1995 | ** are of different types, then numbers are considered less than |
| 1996 | ** strings and strings are considered less than blobs. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1997 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1998 | /* Opcode: Le P1 P2 P3 P4 P5 |
drh | 88e665f | 2016-08-27 01:41:53 +0000 | [diff] [blame] | 1999 | ** Synopsis: IF r[P3]<=r[P1] |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2000 | ** |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 2001 | ** This works just like the Lt opcode except that the jump is taken if |
| 2002 | ** the content of register P3 is less than or equal to the content of |
| 2003 | ** register P1. See the Lt opcode for additional information. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2004 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2005 | /* Opcode: Gt P1 P2 P3 P4 P5 |
drh | 88e665f | 2016-08-27 01:41:53 +0000 | [diff] [blame] | 2006 | ** Synopsis: IF r[P3]>r[P1] |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2007 | ** |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 2008 | ** This works just like the Lt opcode except that the jump is taken if |
| 2009 | ** the content of register P3 is greater than the content of |
| 2010 | ** register P1. See the Lt opcode for additional information. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2011 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2012 | /* Opcode: Ge P1 P2 P3 P4 P5 |
drh | 88e665f | 2016-08-27 01:41:53 +0000 | [diff] [blame] | 2013 | ** Synopsis: IF r[P3]>=r[P1] |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2014 | ** |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 2015 | ** This works just like the Lt opcode except that the jump is taken if |
| 2016 | ** the content of register P3 is greater than or equal to the content of |
| 2017 | ** register P1. See the Lt opcode for additional information. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2018 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2019 | case OP_Eq: /* same as TK_EQ, jump, in1, in3 */ |
| 2020 | case OP_Ne: /* same as TK_NE, jump, in1, in3 */ |
| 2021 | case OP_Lt: /* same as TK_LT, jump, in1, in3 */ |
| 2022 | case OP_Le: /* same as TK_LE, jump, in1, in3 */ |
| 2023 | case OP_Gt: /* same as TK_GT, jump, in1, in3 */ |
| 2024 | case OP_Ge: { /* same as TK_GE, jump, in1, in3 */ |
drh | 4910a76 | 2016-09-03 01:46:15 +0000 | [diff] [blame] | 2025 | int res, res2; /* Result of the comparison of pIn1 against pIn3 */ |
drh | 6a2fe09 | 2009-09-23 02:29:36 +0000 | [diff] [blame] | 2026 | char affinity; /* Affinity to use for comparison */ |
dan | b7dca7d | 2010-03-05 16:32:12 +0000 | [diff] [blame] | 2027 | u16 flags1; /* Copy of initial value of pIn1->flags */ |
| 2028 | u16 flags3; /* Copy of initial value of pIn3->flags */ |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 2029 | |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 2030 | pIn1 = &aMem[pOp->p1]; |
| 2031 | pIn3 = &aMem[pOp->p3]; |
dan | b7dca7d | 2010-03-05 16:32:12 +0000 | [diff] [blame] | 2032 | flags1 = pIn1->flags; |
| 2033 | flags3 = pIn3->flags; |
drh | c3f1d5f | 2011-05-30 23:42:16 +0000 | [diff] [blame] | 2034 | if( (flags1 | flags3)&MEM_Null ){ |
drh | 6a2fe09 | 2009-09-23 02:29:36 +0000 | [diff] [blame] | 2035 | /* One or both operands are NULL */ |
| 2036 | if( pOp->p5 & SQLITE_NULLEQ ){ |
| 2037 | /* If SQLITE_NULLEQ is set (which will only happen if the operator is |
| 2038 | ** OP_Eq or OP_Ne) then take the jump or not depending on whether |
| 2039 | ** or not both operands are null. |
| 2040 | */ |
drh | 053a128 | 2012-09-19 21:15:46 +0000 | [diff] [blame] | 2041 | assert( (flags1 & MEM_Cleared)==0 ); |
drh | a42325e | 2018-12-22 00:34:30 +0000 | [diff] [blame] | 2042 | assert( (pOp->p5 & SQLITE_JUMPIFNULL)==0 || CORRUPT_DB ); |
| 2043 | testcase( (pOp->p5 & SQLITE_JUMPIFNULL)!=0 ); |
drh | c3191d2 | 2016-10-18 16:36:15 +0000 | [diff] [blame] | 2044 | if( (flags1&flags3&MEM_Null)!=0 |
drh | 053a128 | 2012-09-19 21:15:46 +0000 | [diff] [blame] | 2045 | && (flags3&MEM_Cleared)==0 |
| 2046 | ){ |
drh | 4910a76 | 2016-09-03 01:46:15 +0000 | [diff] [blame] | 2047 | res = 0; /* Operands are equal */ |
drh | 053a128 | 2012-09-19 21:15:46 +0000 | [diff] [blame] | 2048 | }else{ |
dan | bdabe74 | 2019-03-18 16:51:24 +0000 | [diff] [blame] | 2049 | res = ((flags3 & MEM_Null) ? -1 : +1); /* Operands are not equal */ |
drh | 053a128 | 2012-09-19 21:15:46 +0000 | [diff] [blame] | 2050 | } |
drh | 6a2fe09 | 2009-09-23 02:29:36 +0000 | [diff] [blame] | 2051 | }else{ |
| 2052 | /* SQLITE_NULLEQ is clear and at least one operand is NULL, |
| 2053 | ** then the result is always NULL. |
| 2054 | ** The jump is taken if the SQLITE_JUMPIFNULL bit is set. |
| 2055 | */ |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 2056 | if( pOp->p5 & SQLITE_STOREP2 ){ |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 2057 | pOut = &aMem[pOp->p2]; |
drh | 4910a76 | 2016-09-03 01:46:15 +0000 | [diff] [blame] | 2058 | iCompare = 1; /* Operands are not equal */ |
dan | b1d6b53 | 2015-12-14 19:42:19 +0000 | [diff] [blame] | 2059 | memAboutToChange(p, pOut); |
drh | 6a2fe09 | 2009-09-23 02:29:36 +0000 | [diff] [blame] | 2060 | MemSetTypeFlag(pOut, MEM_Null); |
| 2061 | REGISTER_TRACE(pOp->p2, pOut); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 2062 | }else{ |
drh | f4345e4 | 2014-02-18 11:31:59 +0000 | [diff] [blame] | 2063 | VdbeBranchTaken(2,3); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 2064 | if( pOp->p5 & SQLITE_JUMPIFNULL ){ |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 2065 | goto jump_to_p2; |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 2066 | } |
drh | 6a2fe09 | 2009-09-23 02:29:36 +0000 | [diff] [blame] | 2067 | } |
| 2068 | break; |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 2069 | } |
drh | 6a2fe09 | 2009-09-23 02:29:36 +0000 | [diff] [blame] | 2070 | }else{ |
| 2071 | /* Neither operand is NULL. Do a comparison. */ |
| 2072 | affinity = pOp->p5 & SQLITE_AFF_MASK; |
drh | 24a0962 | 2014-09-18 16:28:59 +0000 | [diff] [blame] | 2073 | if( affinity>=SQLITE_AFF_NUMERIC ){ |
drh | 5fd0c12 | 2016-04-04 13:46:24 +0000 | [diff] [blame] | 2074 | if( (flags1 | flags3)&MEM_Str ){ |
drh | 169f077 | 2019-05-02 21:36:26 +0000 | [diff] [blame] | 2075 | if( (flags1 & (MEM_Int|MEM_IntReal|MEM_Real|MEM_Str))==MEM_Str ){ |
drh | 5fd0c12 | 2016-04-04 13:46:24 +0000 | [diff] [blame] | 2076 | applyNumericAffinity(pIn1,0); |
drh | 9dce0ef | 2020-02-01 21:03:27 +0000 | [diff] [blame] | 2077 | assert( flags3==pIn3->flags ); |
drh | 4b37cd4 | 2016-06-25 11:43:47 +0000 | [diff] [blame] | 2078 | flags3 = pIn3->flags; |
drh | 5fd0c12 | 2016-04-04 13:46:24 +0000 | [diff] [blame] | 2079 | } |
drh | 169f077 | 2019-05-02 21:36:26 +0000 | [diff] [blame] | 2080 | if( (flags3 & (MEM_Int|MEM_IntReal|MEM_Real|MEM_Str))==MEM_Str ){ |
drh | 5fd0c12 | 2016-04-04 13:46:24 +0000 | [diff] [blame] | 2081 | applyNumericAffinity(pIn3,0); |
| 2082 | } |
drh | 24a0962 | 2014-09-18 16:28:59 +0000 | [diff] [blame] | 2083 | } |
drh | 64caee4 | 2016-09-09 19:33:00 +0000 | [diff] [blame] | 2084 | /* Handle the common case of integer comparison here, as an |
| 2085 | ** optimization, to avoid a call to sqlite3MemCompare() */ |
| 2086 | if( (pIn1->flags & pIn3->flags & MEM_Int)!=0 ){ |
| 2087 | if( pIn3->u.i > pIn1->u.i ){ res = +1; goto compare_op; } |
| 2088 | if( pIn3->u.i < pIn1->u.i ){ res = -1; goto compare_op; } |
| 2089 | res = 0; |
| 2090 | goto compare_op; |
| 2091 | } |
drh | 24a0962 | 2014-09-18 16:28:59 +0000 | [diff] [blame] | 2092 | }else if( affinity==SQLITE_AFF_TEXT ){ |
drh | 169f077 | 2019-05-02 21:36:26 +0000 | [diff] [blame] | 2093 | if( (flags1 & MEM_Str)==0 && (flags1&(MEM_Int|MEM_Real|MEM_IntReal))!=0 ){ |
drh | e7a3466 | 2014-09-19 22:44:20 +0000 | [diff] [blame] | 2094 | testcase( pIn1->flags & MEM_Int ); |
| 2095 | testcase( pIn1->flags & MEM_Real ); |
drh | 169f077 | 2019-05-02 21:36:26 +0000 | [diff] [blame] | 2096 | testcase( pIn1->flags & MEM_IntReal ); |
drh | 24a0962 | 2014-09-18 16:28:59 +0000 | [diff] [blame] | 2097 | sqlite3VdbeMemStringify(pIn1, encoding, 1); |
drh | bc8a6b3 | 2015-03-31 11:42:23 +0000 | [diff] [blame] | 2098 | testcase( (flags1&MEM_Dyn) != (pIn1->flags&MEM_Dyn) ); |
| 2099 | flags1 = (pIn1->flags & ~MEM_TypeMask) | (flags1 & MEM_TypeMask); |
drh | 9dce0ef | 2020-02-01 21:03:27 +0000 | [diff] [blame] | 2100 | if( NEVER(pIn1==pIn3) ) flags3 = flags1 | MEM_Str; |
drh | 24a0962 | 2014-09-18 16:28:59 +0000 | [diff] [blame] | 2101 | } |
drh | b44fec6 | 2019-12-24 21:42:22 +0000 | [diff] [blame] | 2102 | if( (flags3 & MEM_Str)==0 && (flags3&(MEM_Int|MEM_Real|MEM_IntReal))!=0 ){ |
drh | e7a3466 | 2014-09-19 22:44:20 +0000 | [diff] [blame] | 2103 | testcase( pIn3->flags & MEM_Int ); |
| 2104 | testcase( pIn3->flags & MEM_Real ); |
drh | 169f077 | 2019-05-02 21:36:26 +0000 | [diff] [blame] | 2105 | testcase( pIn3->flags & MEM_IntReal ); |
drh | 24a0962 | 2014-09-18 16:28:59 +0000 | [diff] [blame] | 2106 | sqlite3VdbeMemStringify(pIn3, encoding, 1); |
drh | bc8a6b3 | 2015-03-31 11:42:23 +0000 | [diff] [blame] | 2107 | testcase( (flags3&MEM_Dyn) != (pIn3->flags&MEM_Dyn) ); |
| 2108 | flags3 = (pIn3->flags & ~MEM_TypeMask) | (flags3 & MEM_TypeMask); |
drh | 24a0962 | 2014-09-18 16:28:59 +0000 | [diff] [blame] | 2109 | } |
drh | 6a2fe09 | 2009-09-23 02:29:36 +0000 | [diff] [blame] | 2110 | } |
drh | 6a2fe09 | 2009-09-23 02:29:36 +0000 | [diff] [blame] | 2111 | assert( pOp->p4type==P4_COLLSEQ || pOp->p4.pColl==0 ); |
drh | 4910a76 | 2016-09-03 01:46:15 +0000 | [diff] [blame] | 2112 | res = sqlite3MemCompare(pIn3, pIn1, pOp->p4.pColl); |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 2113 | } |
drh | 64caee4 | 2016-09-09 19:33:00 +0000 | [diff] [blame] | 2114 | compare_op: |
drh | 5859636 | 2017-08-03 00:29:23 +0000 | [diff] [blame] | 2115 | /* At this point, res is negative, zero, or positive if reg[P1] is |
| 2116 | ** less than, equal to, or greater than reg[P3], respectively. Compute |
| 2117 | ** the answer to this operator in res2, depending on what the comparison |
| 2118 | ** operator actually is. The next block of code depends on the fact |
| 2119 | ** that the 6 comparison operators are consecutive integers in this |
| 2120 | ** order: NE, EQ, GT, LE, LT, GE */ |
| 2121 | assert( OP_Eq==OP_Ne+1 ); assert( OP_Gt==OP_Ne+2 ); assert( OP_Le==OP_Ne+3 ); |
| 2122 | assert( OP_Lt==OP_Ne+4 ); assert( OP_Ge==OP_Ne+5 ); |
| 2123 | if( res<0 ){ /* ne, eq, gt, le, lt, ge */ |
| 2124 | static const unsigned char aLTb[] = { 1, 0, 0, 1, 1, 0 }; |
| 2125 | res2 = aLTb[pOp->opcode - OP_Ne]; |
| 2126 | }else if( res==0 ){ |
| 2127 | static const unsigned char aEQb[] = { 0, 1, 0, 1, 0, 1 }; |
| 2128 | res2 = aEQb[pOp->opcode - OP_Ne]; |
| 2129 | }else{ |
| 2130 | static const unsigned char aGTb[] = { 1, 0, 1, 0, 0, 1 }; |
| 2131 | res2 = aGTb[pOp->opcode - OP_Ne]; |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 2132 | } |
| 2133 | |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 2134 | /* Undo any changes made by applyAffinity() to the input registers. */ |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 2135 | assert( (pIn3->flags & MEM_Dyn) == (flags3 & MEM_Dyn) ); |
| 2136 | pIn3->flags = flags3; |
drh | b44fec6 | 2019-12-24 21:42:22 +0000 | [diff] [blame] | 2137 | assert( (pIn1->flags & MEM_Dyn) == (flags1 & MEM_Dyn) ); |
| 2138 | pIn1->flags = flags1; |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 2139 | |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 2140 | if( pOp->p5 & SQLITE_STOREP2 ){ |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 2141 | pOut = &aMem[pOp->p2]; |
drh | 4910a76 | 2016-09-03 01:46:15 +0000 | [diff] [blame] | 2142 | iCompare = res; |
drh | 3fffbf9 | 2016-09-05 15:02:41 +0000 | [diff] [blame] | 2143 | if( (pOp->p5 & SQLITE_KEEPNULL)!=0 ){ |
drh | 79752b6 | 2016-08-13 10:02:17 +0000 | [diff] [blame] | 2144 | /* The KEEPNULL flag prevents OP_Eq from overwriting a NULL with 1 |
drh | 3fffbf9 | 2016-09-05 15:02:41 +0000 | [diff] [blame] | 2145 | ** and prevents OP_Ne from overwriting NULL with 0. This flag |
| 2146 | ** is only used in contexts where either: |
| 2147 | ** (1) op==OP_Eq && (r[P2]==NULL || r[P2]==0) |
| 2148 | ** (2) op==OP_Ne && (r[P2]==NULL || r[P2]==1) |
| 2149 | ** Therefore it is not necessary to check the content of r[P2] for |
| 2150 | ** NULL. */ |
drh | 79752b6 | 2016-08-13 10:02:17 +0000 | [diff] [blame] | 2151 | assert( pOp->opcode==OP_Ne || pOp->opcode==OP_Eq ); |
drh | 4910a76 | 2016-09-03 01:46:15 +0000 | [diff] [blame] | 2152 | assert( res2==0 || res2==1 ); |
drh | 3fffbf9 | 2016-09-05 15:02:41 +0000 | [diff] [blame] | 2153 | testcase( res2==0 && pOp->opcode==OP_Eq ); |
| 2154 | testcase( res2==1 && pOp->opcode==OP_Eq ); |
| 2155 | testcase( res2==0 && pOp->opcode==OP_Ne ); |
| 2156 | testcase( res2==1 && pOp->opcode==OP_Ne ); |
drh | 4910a76 | 2016-09-03 01:46:15 +0000 | [diff] [blame] | 2157 | if( (pOp->opcode==OP_Eq)==res2 ) break; |
drh | 79752b6 | 2016-08-13 10:02:17 +0000 | [diff] [blame] | 2158 | } |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 2159 | memAboutToChange(p, pOut); |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 2160 | MemSetTypeFlag(pOut, MEM_Int); |
drh | 4910a76 | 2016-09-03 01:46:15 +0000 | [diff] [blame] | 2161 | pOut->u.i = res2; |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 2162 | REGISTER_TRACE(pOp->p2, pOut); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 2163 | }else{ |
drh | 6cbbcd8 | 2019-04-01 13:06:19 +0000 | [diff] [blame] | 2164 | VdbeBranchTaken(res2!=0, (pOp->p5 & SQLITE_NULLEQ)?2:3); |
drh | 4910a76 | 2016-09-03 01:46:15 +0000 | [diff] [blame] | 2165 | if( res2 ){ |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 2166 | goto jump_to_p2; |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 2167 | } |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 2168 | } |
| 2169 | break; |
| 2170 | } |
drh | c9b84a1 | 2002-06-20 11:36:48 +0000 | [diff] [blame] | 2171 | |
drh | 79752b6 | 2016-08-13 10:02:17 +0000 | [diff] [blame] | 2172 | /* Opcode: ElseNotEq * P2 * * * |
| 2173 | ** |
drh | 13d7950 | 2019-12-23 02:18:49 +0000 | [diff] [blame] | 2174 | ** This opcode must follow an OP_Lt or OP_Gt comparison operator. There |
| 2175 | ** can be zero or more OP_ReleaseReg opcodes intervening, but no other |
| 2176 | ** opcodes are allowed to occur between this instruction and the previous |
| 2177 | ** OP_Lt or OP_Gt. Furthermore, the prior OP_Lt or OP_Gt must have the |
| 2178 | ** SQLITE_STOREP2 bit set in the P5 field. |
| 2179 | ** |
| 2180 | ** If result of an OP_Eq comparison on the same two operands as the |
| 2181 | ** prior OP_Lt or OP_Gt would have been NULL or false (0), then then |
| 2182 | ** jump to P2. If the result of an OP_Eq comparison on the two previous |
| 2183 | ** operands would have been true (1), then fall through. |
drh | 79752b6 | 2016-08-13 10:02:17 +0000 | [diff] [blame] | 2184 | */ |
| 2185 | case OP_ElseNotEq: { /* same as TK_ESCAPE, jump */ |
drh | 13d7950 | 2019-12-23 02:18:49 +0000 | [diff] [blame] | 2186 | |
| 2187 | #ifdef SQLITE_DEBUG |
| 2188 | /* Verify the preconditions of this opcode - that it follows an OP_Lt or |
| 2189 | ** OP_Gt with the SQLITE_STOREP2 flag set, with zero or more intervening |
| 2190 | ** OP_ReleaseReg opcodes */ |
| 2191 | int iAddr; |
| 2192 | for(iAddr = (int)(pOp - aOp) - 1; ALWAYS(iAddr>=0); iAddr--){ |
| 2193 | if( aOp[iAddr].opcode==OP_ReleaseReg ) continue; |
| 2194 | assert( aOp[iAddr].opcode==OP_Lt || aOp[iAddr].opcode==OP_Gt ); |
| 2195 | assert( aOp[iAddr].p5 & SQLITE_STOREP2 ); |
| 2196 | break; |
| 2197 | } |
| 2198 | #endif /* SQLITE_DEBUG */ |
drh | 0f825a7 | 2016-08-13 14:17:02 +0000 | [diff] [blame] | 2199 | VdbeBranchTaken(iCompare!=0, 2); |
| 2200 | if( iCompare!=0 ) goto jump_to_p2; |
drh | 79752b6 | 2016-08-13 10:02:17 +0000 | [diff] [blame] | 2201 | break; |
| 2202 | } |
| 2203 | |
| 2204 | |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 2205 | /* Opcode: Permutation * * * P4 * |
| 2206 | ** |
drh | b7dab70 | 2017-01-26 18:00:00 +0000 | [diff] [blame] | 2207 | ** Set the permutation used by the OP_Compare operator in the next |
| 2208 | ** instruction. The permutation is stored in the P4 operand. |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 2209 | ** |
drh | 953f761 | 2012-12-07 22:18:54 +0000 | [diff] [blame] | 2210 | ** The permutation is only valid until the next OP_Compare that has |
| 2211 | ** the OPFLAG_PERMUTE bit set in P5. Typically the OP_Permutation should |
| 2212 | ** occur immediately prior to the OP_Compare. |
drh | b170202 | 2016-01-30 00:45:18 +0000 | [diff] [blame] | 2213 | ** |
| 2214 | ** The first integer in the P4 integer array is the length of the array |
| 2215 | ** and does not become part of the permutation. |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 2216 | */ |
| 2217 | case OP_Permutation: { |
| 2218 | assert( pOp->p4type==P4_INTARRAY ); |
| 2219 | assert( pOp->p4.ai ); |
drh | b7dab70 | 2017-01-26 18:00:00 +0000 | [diff] [blame] | 2220 | assert( pOp[1].opcode==OP_Compare ); |
| 2221 | assert( pOp[1].p5 & OPFLAG_PERMUTE ); |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 2222 | break; |
| 2223 | } |
| 2224 | |
drh | 953f761 | 2012-12-07 22:18:54 +0000 | [diff] [blame] | 2225 | /* Opcode: Compare P1 P2 P3 P4 P5 |
drh | 079a307 | 2014-03-19 14:10:55 +0000 | [diff] [blame] | 2226 | ** Synopsis: r[P1@P3] <-> r[P2@P3] |
drh | 16ee60f | 2008-06-20 18:13:25 +0000 | [diff] [blame] | 2227 | ** |
drh | 710c484 | 2010-08-30 01:17:20 +0000 | [diff] [blame] | 2228 | ** Compare two vectors of registers in reg(P1)..reg(P1+P3-1) (call this |
| 2229 | ** 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] | 2230 | ** the comparison for use by the next OP_Jump instruct. |
| 2231 | ** |
drh | 0ca10df | 2012-12-08 13:26:23 +0000 | [diff] [blame] | 2232 | ** If P5 has the OPFLAG_PERMUTE bit set, then the order of comparison is |
| 2233 | ** determined by the most recent OP_Permutation operator. If the |
| 2234 | ** OPFLAG_PERMUTE bit is clear, then register are compared in sequential |
| 2235 | ** order. |
| 2236 | ** |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 2237 | ** P4 is a KeyInfo structure that defines collating sequences and sort |
| 2238 | ** orders for the comparison. The permutation applies to registers |
| 2239 | ** only. The KeyInfo elements are used sequentially. |
| 2240 | ** |
| 2241 | ** The comparison is a sort comparison, so NULLs compare equal, |
| 2242 | ** NULLs are less than numbers, numbers are less than strings, |
drh | 16ee60f | 2008-06-20 18:13:25 +0000 | [diff] [blame] | 2243 | ** and strings are less than blobs. |
| 2244 | */ |
| 2245 | case OP_Compare: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2246 | int n; |
| 2247 | int i; |
| 2248 | int p1; |
| 2249 | int p2; |
| 2250 | const KeyInfo *pKeyInfo; |
| 2251 | int idx; |
| 2252 | CollSeq *pColl; /* Collating sequence to use on this term */ |
| 2253 | int bRev; /* True for DESCENDING sort order */ |
drh | b7dab70 | 2017-01-26 18:00:00 +0000 | [diff] [blame] | 2254 | int *aPermute; /* The permutation */ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2255 | |
drh | b7dab70 | 2017-01-26 18:00:00 +0000 | [diff] [blame] | 2256 | if( (pOp->p5 & OPFLAG_PERMUTE)==0 ){ |
| 2257 | aPermute = 0; |
| 2258 | }else{ |
| 2259 | assert( pOp>aOp ); |
| 2260 | assert( pOp[-1].opcode==OP_Permutation ); |
| 2261 | assert( pOp[-1].p4type==P4_INTARRAY ); |
| 2262 | aPermute = pOp[-1].p4.ai + 1; |
| 2263 | assert( aPermute!=0 ); |
| 2264 | } |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2265 | n = pOp->p3; |
| 2266 | pKeyInfo = pOp->p4.pKeyInfo; |
drh | 16ee60f | 2008-06-20 18:13:25 +0000 | [diff] [blame] | 2267 | assert( n>0 ); |
drh | 93a960a | 2008-07-10 00:32:42 +0000 | [diff] [blame] | 2268 | assert( pKeyInfo!=0 ); |
drh | 16ee60f | 2008-06-20 18:13:25 +0000 | [diff] [blame] | 2269 | p1 = pOp->p1; |
drh | 16ee60f | 2008-06-20 18:13:25 +0000 | [diff] [blame] | 2270 | p2 = pOp->p2; |
drh | d879e3e | 2017-02-13 13:35:55 +0000 | [diff] [blame] | 2271 | #ifdef SQLITE_DEBUG |
drh | 6a2fe09 | 2009-09-23 02:29:36 +0000 | [diff] [blame] | 2272 | if( aPermute ){ |
| 2273 | int k, mx = 0; |
| 2274 | for(k=0; k<n; k++) if( aPermute[k]>mx ) mx = aPermute[k]; |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 2275 | assert( p1>0 && p1+mx<=(p->nMem+1 - p->nCursor)+1 ); |
| 2276 | assert( p2>0 && p2+mx<=(p->nMem+1 - p->nCursor)+1 ); |
drh | 6a2fe09 | 2009-09-23 02:29:36 +0000 | [diff] [blame] | 2277 | }else{ |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 2278 | assert( p1>0 && p1+n<=(p->nMem+1 - p->nCursor)+1 ); |
| 2279 | assert( p2>0 && p2+n<=(p->nMem+1 - p->nCursor)+1 ); |
drh | 6a2fe09 | 2009-09-23 02:29:36 +0000 | [diff] [blame] | 2280 | } |
| 2281 | #endif /* SQLITE_DEBUG */ |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 2282 | for(i=0; i<n; i++){ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2283 | idx = aPermute ? aPermute[i] : i; |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 2284 | assert( memIsValid(&aMem[p1+idx]) ); |
| 2285 | assert( memIsValid(&aMem[p2+idx]) ); |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 2286 | REGISTER_TRACE(p1+idx, &aMem[p1+idx]); |
| 2287 | REGISTER_TRACE(p2+idx, &aMem[p2+idx]); |
drh | a485ad1 | 2017-08-02 22:43:14 +0000 | [diff] [blame] | 2288 | assert( i<pKeyInfo->nKeyField ); |
drh | 93a960a | 2008-07-10 00:32:42 +0000 | [diff] [blame] | 2289 | pColl = pKeyInfo->aColl[i]; |
dan | 6e11892 | 2019-08-12 16:36:38 +0000 | [diff] [blame] | 2290 | bRev = (pKeyInfo->aSortFlags[i] & KEYINFO_ORDER_DESC); |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 2291 | iCompare = sqlite3MemCompare(&aMem[p1+idx], &aMem[p2+idx], pColl); |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 2292 | if( iCompare ){ |
dan | 6e11892 | 2019-08-12 16:36:38 +0000 | [diff] [blame] | 2293 | if( (pKeyInfo->aSortFlags[i] & KEYINFO_ORDER_BIGNULL) |
| 2294 | && ((aMem[p1+idx].flags & MEM_Null) || (aMem[p2+idx].flags & MEM_Null)) |
| 2295 | ){ |
| 2296 | iCompare = -iCompare; |
| 2297 | } |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 2298 | if( bRev ) iCompare = -iCompare; |
| 2299 | break; |
| 2300 | } |
drh | 16ee60f | 2008-06-20 18:13:25 +0000 | [diff] [blame] | 2301 | } |
| 2302 | break; |
| 2303 | } |
| 2304 | |
| 2305 | /* Opcode: Jump P1 P2 P3 * * |
| 2306 | ** |
| 2307 | ** Jump to the instruction at address P1, P2, or P3 depending on whether |
| 2308 | ** in the most recent OP_Compare instruction the P1 vector was less than |
| 2309 | ** equal to, or greater than the P2 vector, respectively. |
| 2310 | */ |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 2311 | case OP_Jump: { /* jump */ |
| 2312 | if( iCompare<0 ){ |
drh | 7083a48 | 2018-07-10 16:04:04 +0000 | [diff] [blame] | 2313 | VdbeBranchTaken(0,4); pOp = &aOp[pOp->p1 - 1]; |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 2314 | }else if( iCompare==0 ){ |
drh | 7083a48 | 2018-07-10 16:04:04 +0000 | [diff] [blame] | 2315 | VdbeBranchTaken(1,4); pOp = &aOp[pOp->p2 - 1]; |
drh | 16ee60f | 2008-06-20 18:13:25 +0000 | [diff] [blame] | 2316 | }else{ |
drh | 7083a48 | 2018-07-10 16:04:04 +0000 | [diff] [blame] | 2317 | VdbeBranchTaken(2,4); pOp = &aOp[pOp->p3 - 1]; |
drh | 16ee60f | 2008-06-20 18:13:25 +0000 | [diff] [blame] | 2318 | } |
| 2319 | break; |
| 2320 | } |
| 2321 | |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 2322 | /* Opcode: And P1 P2 P3 * * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 2323 | ** Synopsis: r[P3]=(r[P1] && r[P2]) |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2324 | ** |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 2325 | ** Take the logical AND of the values in registers P1 and P2 and |
| 2326 | ** write the result into register P3. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2327 | ** |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 2328 | ** If either P1 or P2 is 0 (false) then the result is 0 even if |
| 2329 | ** the other input is NULL. A NULL and true or two NULLs give |
| 2330 | ** a NULL output. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2331 | */ |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 2332 | /* Opcode: Or P1 P2 P3 * * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 2333 | ** Synopsis: r[P3]=(r[P1] || r[P2]) |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 2334 | ** |
| 2335 | ** Take the logical OR of the values in register P1 and P2 and |
| 2336 | ** store the answer in register P3. |
| 2337 | ** |
| 2338 | ** If either P1 or P2 is nonzero (true) then the result is 1 (true) |
| 2339 | ** even if the other input is NULL. A NULL and false or two NULLs |
| 2340 | ** give a NULL output. |
| 2341 | */ |
| 2342 | case OP_And: /* same as TK_AND, in1, in2, out3 */ |
| 2343 | case OP_Or: { /* same as TK_OR, in1, in2, out3 */ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2344 | int v1; /* Left operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */ |
| 2345 | int v2; /* Right operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */ |
drh | bb11351 | 2002-05-27 01:04:51 +0000 | [diff] [blame] | 2346 | |
drh | 1fcfa72 | 2018-02-26 15:27:31 +0000 | [diff] [blame] | 2347 | v1 = sqlite3VdbeBooleanValue(&aMem[pOp->p1], 2); |
| 2348 | v2 = sqlite3VdbeBooleanValue(&aMem[pOp->p2], 2); |
drh | bb11351 | 2002-05-27 01:04:51 +0000 | [diff] [blame] | 2349 | if( pOp->opcode==OP_And ){ |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 2350 | 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] | 2351 | v1 = and_logic[v1*3+v2]; |
| 2352 | }else{ |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 2353 | 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] | 2354 | v1 = or_logic[v1*3+v2]; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2355 | } |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 2356 | pOut = &aMem[pOp->p3]; |
drh | bb11351 | 2002-05-27 01:04:51 +0000 | [diff] [blame] | 2357 | if( v1==2 ){ |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 2358 | MemSetTypeFlag(pOut, MEM_Null); |
drh | bb11351 | 2002-05-27 01:04:51 +0000 | [diff] [blame] | 2359 | }else{ |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 2360 | pOut->u.i = v1; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 2361 | MemSetTypeFlag(pOut, MEM_Int); |
drh | bb11351 | 2002-05-27 01:04:51 +0000 | [diff] [blame] | 2362 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2363 | break; |
| 2364 | } |
| 2365 | |
drh | 8abed7b | 2018-02-26 18:49:05 +0000 | [diff] [blame] | 2366 | /* Opcode: IsTrue P1 P2 P3 P4 * |
| 2367 | ** Synopsis: r[P2] = coalesce(r[P1]==TRUE,P3) ^ P4 |
| 2368 | ** |
| 2369 | ** This opcode implements the IS TRUE, IS FALSE, IS NOT TRUE, and |
| 2370 | ** IS NOT FALSE operators. |
| 2371 | ** |
drh | 96acafb | 2018-02-27 14:49:25 +0000 | [diff] [blame] | 2372 | ** Interpret the value in register P1 as a boolean value. Store that |
drh | 8abed7b | 2018-02-26 18:49:05 +0000 | [diff] [blame] | 2373 | ** boolean (a 0 or 1) in register P2. Or if the value in register P1 is |
| 2374 | ** NULL, then the P3 is stored in register P2. Invert the answer if P4 |
| 2375 | ** is 1. |
| 2376 | ** |
| 2377 | ** The logic is summarized like this: |
| 2378 | ** |
| 2379 | ** <ul> |
drh | 96acafb | 2018-02-27 14:49:25 +0000 | [diff] [blame] | 2380 | ** <li> If P3==0 and P4==0 then r[P2] := r[P1] IS TRUE |
| 2381 | ** <li> If P3==1 and P4==1 then r[P2] := r[P1] IS FALSE |
| 2382 | ** <li> If P3==0 and P4==1 then r[P2] := r[P1] IS NOT TRUE |
| 2383 | ** <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] | 2384 | ** </ul> |
| 2385 | */ |
| 2386 | case OP_IsTrue: { /* in1, out2 */ |
| 2387 | assert( pOp->p4type==P4_INT32 ); |
| 2388 | assert( pOp->p4.i==0 || pOp->p4.i==1 ); |
drh | 96acafb | 2018-02-27 14:49:25 +0000 | [diff] [blame] | 2389 | assert( pOp->p3==0 || pOp->p3==1 ); |
drh | 8abed7b | 2018-02-26 18:49:05 +0000 | [diff] [blame] | 2390 | sqlite3VdbeMemSetInt64(&aMem[pOp->p2], |
| 2391 | sqlite3VdbeBooleanValue(&aMem[pOp->p1], pOp->p3) ^ pOp->p4.i); |
| 2392 | break; |
| 2393 | } |
| 2394 | |
drh | e99fa2a | 2008-12-15 15:27:51 +0000 | [diff] [blame] | 2395 | /* Opcode: Not P1 P2 * * * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 2396 | ** Synopsis: r[P2]= !r[P1] |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2397 | ** |
drh | e99fa2a | 2008-12-15 15:27:51 +0000 | [diff] [blame] | 2398 | ** Interpret the value in register P1 as a boolean value. Store the |
| 2399 | ** boolean complement in register P2. If the value in register P1 is |
| 2400 | ** NULL, then a NULL is stored in P2. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2401 | */ |
drh | 93952eb | 2009-11-13 19:43:43 +0000 | [diff] [blame] | 2402 | case OP_Not: { /* same as TK_NOT, in1, out2 */ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 2403 | pIn1 = &aMem[pOp->p1]; |
| 2404 | pOut = &aMem[pOp->p2]; |
drh | 0725cab | 2014-09-17 14:52:46 +0000 | [diff] [blame] | 2405 | if( (pIn1->flags & MEM_Null)==0 ){ |
drh | bc8f68a | 2018-02-26 15:31:39 +0000 | [diff] [blame] | 2406 | sqlite3VdbeMemSetInt64(pOut, !sqlite3VdbeBooleanValue(pIn1,0)); |
drh | 007c843 | 2018-02-26 03:20:18 +0000 | [diff] [blame] | 2407 | }else{ |
| 2408 | sqlite3VdbeMemSetNull(pOut); |
drh | e99fa2a | 2008-12-15 15:27:51 +0000 | [diff] [blame] | 2409 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2410 | break; |
| 2411 | } |
| 2412 | |
drh | e99fa2a | 2008-12-15 15:27:51 +0000 | [diff] [blame] | 2413 | /* Opcode: BitNot P1 P2 * * * |
drh | cd9e014 | 2018-06-12 13:16:57 +0000 | [diff] [blame] | 2414 | ** Synopsis: r[P2]= ~r[P1] |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 2415 | ** |
drh | e99fa2a | 2008-12-15 15:27:51 +0000 | [diff] [blame] | 2416 | ** Interpret the content of register P1 as an integer. Store the |
| 2417 | ** ones-complement of the P1 value into register P2. If P1 holds |
| 2418 | ** a NULL then store a NULL in P2. |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 2419 | */ |
drh | 93952eb | 2009-11-13 19:43:43 +0000 | [diff] [blame] | 2420 | case OP_BitNot: { /* same as TK_BITNOT, in1, out2 */ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 2421 | pIn1 = &aMem[pOp->p1]; |
| 2422 | pOut = &aMem[pOp->p2]; |
drh | 0725cab | 2014-09-17 14:52:46 +0000 | [diff] [blame] | 2423 | sqlite3VdbeMemSetNull(pOut); |
| 2424 | if( (pIn1->flags & MEM_Null)==0 ){ |
| 2425 | pOut->flags = MEM_Int; |
| 2426 | pOut->u.i = ~sqlite3VdbeIntValue(pIn1); |
drh | e99fa2a | 2008-12-15 15:27:51 +0000 | [diff] [blame] | 2427 | } |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 2428 | break; |
| 2429 | } |
| 2430 | |
drh | 48f2d3b | 2011-09-16 01:34:43 +0000 | [diff] [blame] | 2431 | /* Opcode: Once P1 P2 * * * |
| 2432 | ** |
drh | ab087d4 | 2017-03-24 17:59:56 +0000 | [diff] [blame] | 2433 | ** Fall through to the next instruction the first time this opcode is |
| 2434 | ** encountered on each invocation of the byte-code program. Jump to P2 |
| 2435 | ** on the second and all subsequent encounters during the same invocation. |
| 2436 | ** |
| 2437 | ** Top-level programs determine first invocation by comparing the P1 |
| 2438 | ** operand against the P1 operand on the OP_Init opcode at the beginning |
| 2439 | ** of the program. If the P1 values differ, then fall through and make |
| 2440 | ** the P1 of this opcode equal to the P1 of OP_Init. If P1 values are |
| 2441 | ** the same then take the jump. |
| 2442 | ** |
| 2443 | ** For subprograms, there is a bitmask in the VdbeFrame that determines |
| 2444 | ** whether or not the jump should be taken. The bitmask is necessary |
| 2445 | ** because the self-altering code trick does not work for recursive |
| 2446 | ** triggers. |
drh | 48f2d3b | 2011-09-16 01:34:43 +0000 | [diff] [blame] | 2447 | */ |
dan | 1d8cb21 | 2011-12-09 13:24:16 +0000 | [diff] [blame] | 2448 | case OP_Once: { /* jump */ |
drh | ab087d4 | 2017-03-24 17:59:56 +0000 | [diff] [blame] | 2449 | u32 iAddr; /* Address of this instruction */ |
drh | 9e5eb9c | 2016-09-18 16:08:10 +0000 | [diff] [blame] | 2450 | assert( p->aOp[0].opcode==OP_Init ); |
drh | ab087d4 | 2017-03-24 17:59:56 +0000 | [diff] [blame] | 2451 | if( p->pFrame ){ |
| 2452 | iAddr = (int)(pOp - p->aOp); |
| 2453 | if( (p->pFrame->aOnce[iAddr/8] & (1<<(iAddr & 7)))!=0 ){ |
| 2454 | VdbeBranchTaken(1, 2); |
drh | ab087d4 | 2017-03-24 17:59:56 +0000 | [diff] [blame] | 2455 | goto jump_to_p2; |
| 2456 | } |
drh | 18333ef | 2017-03-24 18:38:41 +0000 | [diff] [blame] | 2457 | p->pFrame->aOnce[iAddr/8] |= 1<<(iAddr & 7); |
dan | 1d8cb21 | 2011-12-09 13:24:16 +0000 | [diff] [blame] | 2458 | }else{ |
drh | ab087d4 | 2017-03-24 17:59:56 +0000 | [diff] [blame] | 2459 | if( p->aOp[0].p1==pOp->p1 ){ |
| 2460 | VdbeBranchTaken(1, 2); |
| 2461 | goto jump_to_p2; |
| 2462 | } |
dan | 1d8cb21 | 2011-12-09 13:24:16 +0000 | [diff] [blame] | 2463 | } |
drh | ab087d4 | 2017-03-24 17:59:56 +0000 | [diff] [blame] | 2464 | VdbeBranchTaken(0, 2); |
| 2465 | pOp->p1 = p->aOp[0].p1; |
dan | 1d8cb21 | 2011-12-09 13:24:16 +0000 | [diff] [blame] | 2466 | break; |
| 2467 | } |
| 2468 | |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 2469 | /* Opcode: If P1 P2 P3 * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2470 | ** |
drh | ef8662b | 2011-06-20 21:47:58 +0000 | [diff] [blame] | 2471 | ** Jump to P2 if the value in register P1 is true. The value |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 2472 | ** is considered true if it is numeric and non-zero. If the value |
drh | e21a6e1 | 2014-08-01 18:00:24 +0000 | [diff] [blame] | 2473 | ** 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] | 2474 | */ |
drh | 1fcfa72 | 2018-02-26 15:27:31 +0000 | [diff] [blame] | 2475 | case OP_If: { /* jump, in1 */ |
| 2476 | int c; |
| 2477 | c = sqlite3VdbeBooleanValue(&aMem[pOp->p1], pOp->p3); |
| 2478 | VdbeBranchTaken(c!=0, 2); |
| 2479 | if( c ) goto jump_to_p2; |
| 2480 | break; |
| 2481 | } |
| 2482 | |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 2483 | /* Opcode: IfNot P1 P2 P3 * * |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 2484 | ** |
drh | ef8662b | 2011-06-20 21:47:58 +0000 | [diff] [blame] | 2485 | ** Jump to P2 if the value in register P1 is False. The value |
drh | b8475df | 2011-12-09 16:21:19 +0000 | [diff] [blame] | 2486 | ** 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] | 2487 | ** 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] | 2488 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2489 | case OP_IfNot: { /* jump, in1 */ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2490 | int c; |
drh | 1fcfa72 | 2018-02-26 15:27:31 +0000 | [diff] [blame] | 2491 | c = !sqlite3VdbeBooleanValue(&aMem[pOp->p1], !pOp->p3); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 2492 | VdbeBranchTaken(c!=0, 2); |
drh | 1fcfa72 | 2018-02-26 15:27:31 +0000 | [diff] [blame] | 2493 | if( c ) goto jump_to_p2; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2494 | break; |
| 2495 | } |
| 2496 | |
drh | 830ecf9 | 2009-06-18 00:41:55 +0000 | [diff] [blame] | 2497 | /* Opcode: IsNull P1 P2 * * * |
drh | 72e26de | 2016-08-24 21:24:04 +0000 | [diff] [blame] | 2498 | ** Synopsis: if r[P1]==NULL goto P2 |
drh | 477df4b | 2008-01-05 18:48:24 +0000 | [diff] [blame] | 2499 | ** |
drh | 830ecf9 | 2009-06-18 00:41:55 +0000 | [diff] [blame] | 2500 | ** Jump to P2 if the value in register P1 is NULL. |
drh | 477df4b | 2008-01-05 18:48:24 +0000 | [diff] [blame] | 2501 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2502 | case OP_IsNull: { /* same as TK_ISNULL, jump, in1 */ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 2503 | pIn1 = &aMem[pOp->p1]; |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 2504 | VdbeBranchTaken( (pIn1->flags & MEM_Null)!=0, 2); |
drh | 830ecf9 | 2009-06-18 00:41:55 +0000 | [diff] [blame] | 2505 | if( (pIn1->flags & MEM_Null)!=0 ){ |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 2506 | goto jump_to_p2; |
drh | 830ecf9 | 2009-06-18 00:41:55 +0000 | [diff] [blame] | 2507 | } |
drh | 477df4b | 2008-01-05 18:48:24 +0000 | [diff] [blame] | 2508 | break; |
| 2509 | } |
| 2510 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 2511 | /* Opcode: NotNull P1 P2 * * * |
drh | fc8d4f9 | 2013-11-08 15:19:46 +0000 | [diff] [blame] | 2512 | ** Synopsis: if r[P1]!=NULL goto P2 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2513 | ** |
drh | 6a288a3 | 2008-01-07 19:20:24 +0000 | [diff] [blame] | 2514 | ** Jump to P2 if the value in register P1 is not NULL. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2515 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2516 | case OP_NotNull: { /* same as TK_NOTNULL, jump, in1 */ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 2517 | pIn1 = &aMem[pOp->p1]; |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 2518 | VdbeBranchTaken( (pIn1->flags & MEM_Null)==0, 2); |
drh | 6a288a3 | 2008-01-07 19:20:24 +0000 | [diff] [blame] | 2519 | if( (pIn1->flags & MEM_Null)==0 ){ |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 2520 | goto jump_to_p2; |
drh | 6a288a3 | 2008-01-07 19:20:24 +0000 | [diff] [blame] | 2521 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2522 | break; |
| 2523 | } |
| 2524 | |
drh | 31d6fd5 | 2017-04-14 19:03:10 +0000 | [diff] [blame] | 2525 | /* Opcode: IfNullRow P1 P2 P3 * * |
| 2526 | ** Synopsis: if P1.nullRow then r[P3]=NULL, goto P2 |
| 2527 | ** |
| 2528 | ** Check the cursor P1 to see if it is currently pointing at a NULL row. |
| 2529 | ** If it is, then set register P3 to NULL and jump immediately to P2. |
| 2530 | ** If P1 is not on a NULL row, then fall through without making any |
| 2531 | ** changes. |
| 2532 | */ |
| 2533 | case OP_IfNullRow: { /* jump */ |
| 2534 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
drh | 3f1e9e0 | 2017-05-23 01:21:07 +0000 | [diff] [blame] | 2535 | assert( p->apCsr[pOp->p1]!=0 ); |
drh | 31d6fd5 | 2017-04-14 19:03:10 +0000 | [diff] [blame] | 2536 | if( p->apCsr[pOp->p1]->nullRow ){ |
| 2537 | sqlite3VdbeMemSetNull(aMem + pOp->p3); |
| 2538 | goto jump_to_p2; |
| 2539 | } |
| 2540 | break; |
| 2541 | } |
| 2542 | |
drh | 092457b | 2017-12-29 15:04:49 +0000 | [diff] [blame] | 2543 | #ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC |
| 2544 | /* Opcode: Offset P1 P2 P3 * * |
| 2545 | ** Synopsis: r[P3] = sqlite_offset(P1) |
drh | 2fc865c | 2017-12-16 20:20:37 +0000 | [diff] [blame] | 2546 | ** |
drh | 092457b | 2017-12-29 15:04:49 +0000 | [diff] [blame] | 2547 | ** 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] | 2548 | ** start of the payload for the record at which that cursor P1 is currently |
| 2549 | ** pointing. |
drh | fe6d20e | 2017-12-29 14:33:54 +0000 | [diff] [blame] | 2550 | ** |
drh | 092457b | 2017-12-29 15:04:49 +0000 | [diff] [blame] | 2551 | ** P2 is the column number for the argument to the sqlite_offset() function. |
drh | fe6d20e | 2017-12-29 14:33:54 +0000 | [diff] [blame] | 2552 | ** This opcode does not use P2 itself, but the P2 value is used by the |
| 2553 | ** code generator. The P1, P2, and P3 operands to this opcode are the |
mistachkin | 5e9825e | 2018-03-01 18:09:02 +0000 | [diff] [blame] | 2554 | ** same as for OP_Column. |
drh | 092457b | 2017-12-29 15:04:49 +0000 | [diff] [blame] | 2555 | ** |
| 2556 | ** This opcode is only available if SQLite is compiled with the |
| 2557 | ** -DSQLITE_ENABLE_OFFSET_SQL_FUNC option. |
drh | 2fc865c | 2017-12-16 20:20:37 +0000 | [diff] [blame] | 2558 | */ |
drh | 092457b | 2017-12-29 15:04:49 +0000 | [diff] [blame] | 2559 | case OP_Offset: { /* out3 */ |
drh | 2fc865c | 2017-12-16 20:20:37 +0000 | [diff] [blame] | 2560 | VdbeCursor *pC; /* The VDBE cursor */ |
| 2561 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 2562 | pC = p->apCsr[pOp->p1]; |
drh | fe6d20e | 2017-12-29 14:33:54 +0000 | [diff] [blame] | 2563 | pOut = &p->aMem[pOp->p3]; |
drh | c64487b | 2017-12-29 17:21:21 +0000 | [diff] [blame] | 2564 | if( NEVER(pC==0) || pC->eCurType!=CURTYPE_BTREE ){ |
drh | fe6d20e | 2017-12-29 14:33:54 +0000 | [diff] [blame] | 2565 | sqlite3VdbeMemSetNull(pOut); |
drh | 2fc865c | 2017-12-16 20:20:37 +0000 | [diff] [blame] | 2566 | }else{ |
drh | 092457b | 2017-12-29 15:04:49 +0000 | [diff] [blame] | 2567 | sqlite3VdbeMemSetInt64(pOut, sqlite3BtreeOffset(pC->uc.pCursor)); |
drh | 2fc865c | 2017-12-16 20:20:37 +0000 | [diff] [blame] | 2568 | } |
| 2569 | break; |
| 2570 | } |
drh | 092457b | 2017-12-29 15:04:49 +0000 | [diff] [blame] | 2571 | #endif /* SQLITE_ENABLE_OFFSET_SQL_FUNC */ |
drh | 2fc865c | 2017-12-16 20:20:37 +0000 | [diff] [blame] | 2572 | |
drh | 3e9ca09 | 2009-09-08 01:14:48 +0000 | [diff] [blame] | 2573 | /* Opcode: Column P1 P2 P3 P4 P5 |
drh | 72e26de | 2016-08-24 21:24:04 +0000 | [diff] [blame] | 2574 | ** Synopsis: r[P3]=PX |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2575 | ** |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 2576 | ** Interpret the data that cursor P1 points to as a structure built using |
| 2577 | ** the MakeRecord instruction. (See the MakeRecord opcode for additional |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 2578 | ** information about the format of the data.) Extract the P2-th column |
| 2579 | ** from this record. If there are less that (P2+1) |
| 2580 | ** values in the record, extract a NULL. |
| 2581 | ** |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2582 | ** The value extracted is stored in register P3. |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2583 | ** |
drh | 1cc3a36 | 2017-04-03 13:17:31 +0000 | [diff] [blame] | 2584 | ** If the record contains fewer than P2 fields, then extract a NULL. Or, |
danielk1977 | 1f4aa33 | 2008-01-03 09:51:55 +0000 | [diff] [blame] | 2585 | ** if the P4 argument is a P4_MEM use the value of the P4 argument as |
| 2586 | ** the result. |
drh | 3e9ca09 | 2009-09-08 01:14:48 +0000 | [diff] [blame] | 2587 | ** |
drh | 1cc3a36 | 2017-04-03 13:17:31 +0000 | [diff] [blame] | 2588 | ** If the OPFLAG_LENGTHARG and OPFLAG_TYPEOFARG bits are set on P5 then |
drh | dda5c08 | 2012-03-28 13:41:10 +0000 | [diff] [blame] | 2589 | ** the result is guaranteed to only be used as the argument of a length() |
| 2590 | ** or typeof() function, respectively. The loading of large blobs can be |
| 2591 | ** skipped for length() and all content loading can be skipped for typeof(). |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2592 | */ |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 2593 | case OP_Column: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2594 | int p2; /* column number to retrieve */ |
| 2595 | VdbeCursor *pC; /* The VDBE cursor */ |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 2596 | BtCursor *pCrsr; /* The BTree cursor */ |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 2597 | 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] | 2598 | int len; /* The length of the serialized data for the column */ |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 2599 | int i; /* Loop counter */ |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 2600 | Mem *pDest; /* Where to write the extracted value */ |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 2601 | Mem sMem; /* For storing the record being decoded */ |
drh | 399af1d | 2013-11-20 17:25:55 +0000 | [diff] [blame] | 2602 | const u8 *zData; /* Part of the record being decoded */ |
| 2603 | const u8 *zHdr; /* Next unparsed byte of the header */ |
| 2604 | const u8 *zEndHdr; /* Pointer to first byte after the header */ |
drh | c6ce3883 | 2015-10-15 21:30:24 +0000 | [diff] [blame] | 2605 | u64 offset64; /* 64-bit offset */ |
drh | 5a077b7 | 2011-08-29 02:16:18 +0000 | [diff] [blame] | 2606 | u32 t; /* A type code from the record header */ |
drh | 3e9ca09 | 2009-09-08 01:14:48 +0000 | [diff] [blame] | 2607 | Mem *pReg; /* PseudoTable input register */ |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2608 | |
drh | 8c7715d | 2019-12-20 14:37:56 +0000 | [diff] [blame] | 2609 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
dan | de892d9 | 2016-01-29 19:29:45 +0000 | [diff] [blame] | 2610 | pC = p->apCsr[pOp->p1]; |
drh | 8c7715d | 2019-12-20 14:37:56 +0000 | [diff] [blame] | 2611 | assert( pC!=0 ); |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2612 | p2 = pOp->p2; |
dan | de892d9 | 2016-01-29 19:29:45 +0000 | [diff] [blame] | 2613 | |
drh | 170ad68 | 2017-06-02 15:44:22 +0000 | [diff] [blame] | 2614 | /* If the cursor cache is stale (meaning it is not currently point at |
| 2615 | ** the correct row) then bring it up-to-date by doing the necessary |
| 2616 | ** B-Tree seek. */ |
dan | de892d9 | 2016-01-29 19:29:45 +0000 | [diff] [blame] | 2617 | rc = sqlite3VdbeCursorMoveto(&pC, &p2); |
drh | 4ca239f | 2016-05-19 11:12:43 +0000 | [diff] [blame] | 2618 | if( rc ) goto abort_due_to_error; |
dan | de892d9 | 2016-01-29 19:29:45 +0000 | [diff] [blame] | 2619 | |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 2620 | assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) ); |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 2621 | pDest = &aMem[pOp->p3]; |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 2622 | memAboutToChange(p, pDest); |
danielk1977 | 6c92409 | 2007-11-12 08:09:34 +0000 | [diff] [blame] | 2623 | assert( pC!=0 ); |
drh | c8606e4 | 2013-11-20 19:28:03 +0000 | [diff] [blame] | 2624 | assert( p2<pC->nField ); |
drh | b53a5a9 | 2014-10-12 22:37:22 +0000 | [diff] [blame] | 2625 | aOffset = pC->aOffset; |
drh | 62aaa6c | 2015-11-21 17:27:42 +0000 | [diff] [blame] | 2626 | assert( pC->eCurType!=CURTYPE_VTAB ); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 2627 | assert( pC->eCurType!=CURTYPE_PSEUDO || pC->nullRow ); |
| 2628 | assert( pC->eCurType!=CURTYPE_SORTER ); |
drh | 399af1d | 2013-11-20 17:25:55 +0000 | [diff] [blame] | 2629 | |
drh | a43a02e | 2016-05-19 17:51:19 +0000 | [diff] [blame] | 2630 | if( pC->cacheStatus!=p->cacheCtr ){ /*OPTIMIZATION-IF-FALSE*/ |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2631 | if( pC->nullRow ){ |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 2632 | if( pC->eCurType==CURTYPE_PSEUDO ){ |
drh | fe0cf7a | 2017-08-16 19:20:20 +0000 | [diff] [blame] | 2633 | /* For the special case of as pseudo-cursor, the seekResult field |
| 2634 | ** identifies the register that holds the record */ |
| 2635 | assert( pC->seekResult>0 ); |
| 2636 | pReg = &aMem[pC->seekResult]; |
drh | c8606e4 | 2013-11-20 19:28:03 +0000 | [diff] [blame] | 2637 | assert( pReg->flags & MEM_Blob ); |
| 2638 | assert( memIsValid(pReg) ); |
drh | 6cd8c8c | 2017-08-15 14:14:36 +0000 | [diff] [blame] | 2639 | pC->payloadSize = pC->szRow = pReg->n; |
drh | c8606e4 | 2013-11-20 19:28:03 +0000 | [diff] [blame] | 2640 | pC->aRow = (u8*)pReg->z; |
| 2641 | }else{ |
drh | 6b5631e | 2014-11-05 15:57:39 +0000 | [diff] [blame] | 2642 | sqlite3VdbeMemSetNull(pDest); |
drh | 399af1d | 2013-11-20 17:25:55 +0000 | [diff] [blame] | 2643 | goto op_column_out; |
| 2644 | } |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2645 | }else{ |
drh | 06a09a8 | 2016-11-25 17:03:03 +0000 | [diff] [blame] | 2646 | pCrsr = pC->uc.pCursor; |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 2647 | assert( pC->eCurType==CURTYPE_BTREE ); |
drh | c8606e4 | 2013-11-20 19:28:03 +0000 | [diff] [blame] | 2648 | assert( pCrsr ); |
drh | a7c90c4 | 2016-06-04 20:37:10 +0000 | [diff] [blame] | 2649 | assert( sqlite3BtreeCursorIsValid(pCrsr) ); |
| 2650 | pC->payloadSize = sqlite3BtreePayloadSize(pCrsr); |
drh | 6cd8c8c | 2017-08-15 14:14:36 +0000 | [diff] [blame] | 2651 | pC->aRow = sqlite3BtreePayloadFetch(pCrsr, &pC->szRow); |
| 2652 | assert( pC->szRow<=pC->payloadSize ); |
| 2653 | assert( pC->szRow<=65536 ); /* Maximum page size is 64KiB */ |
| 2654 | if( pC->payloadSize > (u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){ |
drh | 5f7dacb | 2015-11-20 13:33:56 +0000 | [diff] [blame] | 2655 | goto too_big; |
drh | 399af1d | 2013-11-20 17:25:55 +0000 | [diff] [blame] | 2656 | } |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2657 | } |
drh | b73857f | 2006-03-17 00:25:59 +0000 | [diff] [blame] | 2658 | pC->cacheStatus = p->cacheCtr; |
drh | 1f613c4 | 2017-08-16 14:16:19 +0000 | [diff] [blame] | 2659 | pC->iHdrOffset = getVarint32(pC->aRow, aOffset[0]); |
drh | 399af1d | 2013-11-20 17:25:55 +0000 | [diff] [blame] | 2660 | pC->nHdrParsed = 0; |
drh | 35cd643 | 2009-06-05 14:17:21 +0000 | [diff] [blame] | 2661 | |
drh | c81aa2e | 2014-10-11 23:31:52 +0000 | [diff] [blame] | 2662 | |
drh | 1f613c4 | 2017-08-16 14:16:19 +0000 | [diff] [blame] | 2663 | if( pC->szRow<aOffset[0] ){ /*OPTIMIZATION-IF-FALSE*/ |
drh | c81aa2e | 2014-10-11 23:31:52 +0000 | [diff] [blame] | 2664 | /* pC->aRow does not have to hold the entire row, but it does at least |
| 2665 | ** need to cover the header of the record. If pC->aRow does not contain |
| 2666 | ** the complete header, then set it to zero, forcing the header to be |
| 2667 | ** dynamically allocated. */ |
| 2668 | pC->aRow = 0; |
| 2669 | pC->szRow = 0; |
drh | 848a332 | 2015-10-16 12:53:47 +0000 | [diff] [blame] | 2670 | |
| 2671 | /* Make sure a corrupt database has not given us an oversize header. |
| 2672 | ** Do this now to avoid an oversize memory allocation. |
| 2673 | ** |
| 2674 | ** Type entries can be between 1 and 5 bytes each. But 4 and 5 byte |
| 2675 | ** types use so much data space that there can only be 4096 and 32 of |
| 2676 | ** them, respectively. So the maximum header length results from a |
| 2677 | ** 3-byte type for each of the maximum of 32768 columns plus three |
| 2678 | ** extra bytes for the header length itself. 32768*3 + 3 = 98307. |
| 2679 | */ |
drh | 1f613c4 | 2017-08-16 14:16:19 +0000 | [diff] [blame] | 2680 | if( aOffset[0] > 98307 || aOffset[0] > pC->payloadSize ){ |
drh | 74588ce | 2017-09-13 00:13:05 +0000 | [diff] [blame] | 2681 | goto op_column_corrupt; |
drh | 848a332 | 2015-10-16 12:53:47 +0000 | [diff] [blame] | 2682 | } |
drh | 95b225a | 2017-08-16 11:04:22 +0000 | [diff] [blame] | 2683 | }else{ |
| 2684 | /* This is an optimization. By skipping over the first few tests |
| 2685 | ** (ex: pC->nHdrParsed<=p2) in the next section, we achieve a |
| 2686 | ** measurable performance gain. |
| 2687 | ** |
drh | 1f613c4 | 2017-08-16 14:16:19 +0000 | [diff] [blame] | 2688 | ** 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] | 2689 | ** generated by SQLite, and could be considered corruption, but we |
drh | 1f613c4 | 2017-08-16 14:16:19 +0000 | [diff] [blame] | 2690 | ** accept it for historical reasons. When aOffset[0]==0, the code this |
drh | 95b225a | 2017-08-16 11:04:22 +0000 | [diff] [blame] | 2691 | ** branch jumps to reads past the end of the record, but never more |
| 2692 | ** than a few bytes. Even if the record occurs at the end of the page |
| 2693 | ** content area, the "page header" comes after the page content and so |
| 2694 | ** this overread is harmless. Similar overreads can occur for a corrupt |
| 2695 | ** database file. |
drh | 0eda6cd | 2016-05-19 16:58:42 +0000 | [diff] [blame] | 2696 | */ |
| 2697 | zData = pC->aRow; |
| 2698 | assert( pC->nHdrParsed<=p2 ); /* Conditional skipped */ |
drh | 1f613c4 | 2017-08-16 14:16:19 +0000 | [diff] [blame] | 2699 | testcase( aOffset[0]==0 ); |
drh | 0eda6cd | 2016-05-19 16:58:42 +0000 | [diff] [blame] | 2700 | goto op_column_read_header; |
drh | c81aa2e | 2014-10-11 23:31:52 +0000 | [diff] [blame] | 2701 | } |
drh | 399af1d | 2013-11-20 17:25:55 +0000 | [diff] [blame] | 2702 | } |
drh | 35cd643 | 2009-06-05 14:17:21 +0000 | [diff] [blame] | 2703 | |
drh | 399af1d | 2013-11-20 17:25:55 +0000 | [diff] [blame] | 2704 | /* 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] | 2705 | ** parsed and valid information is in aOffset[] and pC->aType[]. |
drh | 399af1d | 2013-11-20 17:25:55 +0000 | [diff] [blame] | 2706 | */ |
drh | c8606e4 | 2013-11-20 19:28:03 +0000 | [diff] [blame] | 2707 | if( pC->nHdrParsed<=p2 ){ |
drh | 380d685 | 2013-11-20 20:58:00 +0000 | [diff] [blame] | 2708 | /* If there is more header available for parsing in the record, try |
| 2709 | ** to extract additional fields up through the p2+1-th field |
drh | 35cd643 | 2009-06-05 14:17:21 +0000 | [diff] [blame] | 2710 | */ |
drh | c8606e4 | 2013-11-20 19:28:03 +0000 | [diff] [blame] | 2711 | if( pC->iHdrOffset<aOffset[0] ){ |
| 2712 | /* Make sure zData points to enough of the record to cover the header. */ |
| 2713 | if( pC->aRow==0 ){ |
| 2714 | memset(&sMem, 0, sizeof(sMem)); |
drh | 2a74006 | 2020-02-05 18:28:17 +0000 | [diff] [blame] | 2715 | rc = sqlite3VdbeMemFromBtreeZeroOffset(pC->uc.pCursor,aOffset[0],&sMem); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 2716 | if( rc!=SQLITE_OK ) goto abort_due_to_error; |
drh | c8606e4 | 2013-11-20 19:28:03 +0000 | [diff] [blame] | 2717 | zData = (u8*)sMem.z; |
| 2718 | }else{ |
| 2719 | zData = pC->aRow; |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2720 | } |
drh | c8606e4 | 2013-11-20 19:28:03 +0000 | [diff] [blame] | 2721 | |
drh | 0c8f760 | 2014-09-19 16:56:45 +0000 | [diff] [blame] | 2722 | /* 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] | 2723 | op_column_read_header: |
drh | c8606e4 | 2013-11-20 19:28:03 +0000 | [diff] [blame] | 2724 | i = pC->nHdrParsed; |
drh | c6ce3883 | 2015-10-15 21:30:24 +0000 | [diff] [blame] | 2725 | offset64 = aOffset[i]; |
drh | c8606e4 | 2013-11-20 19:28:03 +0000 | [diff] [blame] | 2726 | zHdr = zData + pC->iHdrOffset; |
| 2727 | zEndHdr = zData + aOffset[0]; |
drh | 95b225a | 2017-08-16 11:04:22 +0000 | [diff] [blame] | 2728 | testcase( zHdr>=zEndHdr ); |
drh | c8606e4 | 2013-11-20 19:28:03 +0000 | [diff] [blame] | 2729 | do{ |
drh | c332e04 | 2019-02-12 21:04:33 +0000 | [diff] [blame] | 2730 | if( (pC->aType[i] = t = zHdr[0])<0x80 ){ |
drh | c8606e4 | 2013-11-20 19:28:03 +0000 | [diff] [blame] | 2731 | zHdr++; |
drh | faf3727 | 2015-10-16 14:23:42 +0000 | [diff] [blame] | 2732 | offset64 += sqlite3VdbeOneByteSerialTypeLen(t); |
drh | 5a077b7 | 2011-08-29 02:16:18 +0000 | [diff] [blame] | 2733 | }else{ |
drh | c8606e4 | 2013-11-20 19:28:03 +0000 | [diff] [blame] | 2734 | zHdr += sqlite3GetVarint32(zHdr, &t); |
drh | c332e04 | 2019-02-12 21:04:33 +0000 | [diff] [blame] | 2735 | pC->aType[i] = t; |
drh | faf3727 | 2015-10-16 14:23:42 +0000 | [diff] [blame] | 2736 | offset64 += sqlite3VdbeSerialTypeLen(t); |
drh | 5a077b7 | 2011-08-29 02:16:18 +0000 | [diff] [blame] | 2737 | } |
drh | c332e04 | 2019-02-12 21:04:33 +0000 | [diff] [blame] | 2738 | aOffset[++i] = (u32)(offset64 & 0xffffffff); |
drh | c8606e4 | 2013-11-20 19:28:03 +0000 | [diff] [blame] | 2739 | }while( i<=p2 && zHdr<zEndHdr ); |
drh | 170c276 | 2016-05-20 21:40:11 +0000 | [diff] [blame] | 2740 | |
drh | 8dd8362 | 2014-10-13 23:39:02 +0000 | [diff] [blame] | 2741 | /* The record is corrupt if any of the following are true: |
| 2742 | ** (1) the bytes of the header extend past the declared header size |
drh | 8dd8362 | 2014-10-13 23:39:02 +0000 | [diff] [blame] | 2743 | ** (2) the entire header was used but not all data was used |
drh | 8dd8362 | 2014-10-13 23:39:02 +0000 | [diff] [blame] | 2744 | ** (3) the end of the data extends beyond the end of the record. |
drh | c8606e4 | 2013-11-20 19:28:03 +0000 | [diff] [blame] | 2745 | */ |
drh | c6ce3883 | 2015-10-15 21:30:24 +0000 | [diff] [blame] | 2746 | if( (zHdr>=zEndHdr && (zHdr>zEndHdr || offset64!=pC->payloadSize)) |
| 2747 | || (offset64 > pC->payloadSize) |
drh | c8606e4 | 2013-11-20 19:28:03 +0000 | [diff] [blame] | 2748 | ){ |
drh | 95b225a | 2017-08-16 11:04:22 +0000 | [diff] [blame] | 2749 | if( aOffset[0]==0 ){ |
| 2750 | i = 0; |
| 2751 | zHdr = zEndHdr; |
| 2752 | }else{ |
| 2753 | if( pC->aRow==0 ) sqlite3VdbeMemRelease(&sMem); |
drh | 74588ce | 2017-09-13 00:13:05 +0000 | [diff] [blame] | 2754 | goto op_column_corrupt; |
drh | 95b225a | 2017-08-16 11:04:22 +0000 | [diff] [blame] | 2755 | } |
danielk1977 | dedf45b | 2006-01-13 17:12:01 +0000 | [diff] [blame] | 2756 | } |
drh | ddb2b4a | 2016-03-25 12:10:32 +0000 | [diff] [blame] | 2757 | |
drh | 170c276 | 2016-05-20 21:40:11 +0000 | [diff] [blame] | 2758 | pC->nHdrParsed = i; |
| 2759 | pC->iHdrOffset = (u32)(zHdr - zData); |
| 2760 | if( pC->aRow==0 ) sqlite3VdbeMemRelease(&sMem); |
mistachkin | 8c7cd6a | 2015-12-16 21:09:53 +0000 | [diff] [blame] | 2761 | }else{ |
drh | 9fbc885 | 2016-01-04 03:48:46 +0000 | [diff] [blame] | 2762 | t = 0; |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2763 | } |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 2764 | |
drh | f2db338 | 2015-04-30 20:33:25 +0000 | [diff] [blame] | 2765 | /* If after trying to extract new entries from the header, nHdrParsed is |
drh | 380d685 | 2013-11-20 20:58:00 +0000 | [diff] [blame] | 2766 | ** still not up to p2, that means that the record has fewer than p2 |
| 2767 | ** columns. So the result will be either the default value or a NULL. |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 2768 | */ |
drh | c8606e4 | 2013-11-20 19:28:03 +0000 | [diff] [blame] | 2769 | if( pC->nHdrParsed<=p2 ){ |
| 2770 | if( pOp->p4type==P4_MEM ){ |
| 2771 | sqlite3VdbeMemShallowCopy(pDest, pOp->p4.pMem, MEM_Static); |
| 2772 | }else{ |
drh | 22e8d83 | 2014-10-29 00:58:38 +0000 | [diff] [blame] | 2773 | sqlite3VdbeMemSetNull(pDest); |
drh | c8606e4 | 2013-11-20 19:28:03 +0000 | [diff] [blame] | 2774 | } |
danielk1977 | 3c9cc8d | 2005-01-17 03:40:08 +0000 | [diff] [blame] | 2775 | goto op_column_out; |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 2776 | } |
drh | 95fa606 | 2015-10-16 13:50:08 +0000 | [diff] [blame] | 2777 | }else{ |
| 2778 | t = pC->aType[p2]; |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 2779 | } |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2780 | |
drh | 380d685 | 2013-11-20 20:58:00 +0000 | [diff] [blame] | 2781 | /* Extract the content for the p2+1-th column. Control can only |
drh | 0c8f760 | 2014-09-19 16:56:45 +0000 | [diff] [blame] | 2782 | ** 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] | 2783 | ** all valid. |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2784 | */ |
drh | c8606e4 | 2013-11-20 19:28:03 +0000 | [diff] [blame] | 2785 | assert( p2<pC->nHdrParsed ); |
| 2786 | assert( rc==SQLITE_OK ); |
drh | 75fd054 | 2014-03-01 16:24:44 +0000 | [diff] [blame] | 2787 | assert( sqlite3VdbeCheckMemInvariants(pDest) ); |
drh | a1851ef | 2016-05-20 19:51:28 +0000 | [diff] [blame] | 2788 | if( VdbeMemDynamic(pDest) ){ |
| 2789 | sqlite3VdbeMemSetNull(pDest); |
| 2790 | } |
drh | 95fa606 | 2015-10-16 13:50:08 +0000 | [diff] [blame] | 2791 | assert( t==pC->aType[p2] ); |
drh | c8606e4 | 2013-11-20 19:28:03 +0000 | [diff] [blame] | 2792 | if( pC->szRow>=aOffset[p2+1] ){ |
drh | 380d685 | 2013-11-20 20:58:00 +0000 | [diff] [blame] | 2793 | /* This is the common case where the desired content fits on the original |
| 2794 | ** page - where the content is not on an overflow page */ |
drh | 69f6e25 | 2016-01-11 18:05:00 +0000 | [diff] [blame] | 2795 | zData = pC->aRow + aOffset[p2]; |
| 2796 | if( t<12 ){ |
| 2797 | sqlite3VdbeSerialGet(zData, t, pDest); |
| 2798 | }else{ |
| 2799 | /* If the column value is a string, we need a persistent value, not |
| 2800 | ** a MEM_Ephem value. This branch is a fast short-cut that is equivalent |
| 2801 | ** to calling sqlite3VdbeSerialGet() and sqlite3VdbeDeephemeralize(). |
| 2802 | */ |
| 2803 | static const u16 aFlag[] = { MEM_Blob, MEM_Str|MEM_Term }; |
| 2804 | pDest->n = len = (t-12)/2; |
drh | a1851ef | 2016-05-20 19:51:28 +0000 | [diff] [blame] | 2805 | pDest->enc = encoding; |
drh | 69f6e25 | 2016-01-11 18:05:00 +0000 | [diff] [blame] | 2806 | if( pDest->szMalloc < len+2 ){ |
| 2807 | pDest->flags = MEM_Null; |
| 2808 | if( sqlite3VdbeMemGrow(pDest, len+2, 0) ) goto no_mem; |
| 2809 | }else{ |
| 2810 | pDest->z = pDest->zMalloc; |
| 2811 | } |
| 2812 | memcpy(pDest->z, zData, len); |
| 2813 | pDest->z[len] = 0; |
| 2814 | pDest->z[len+1] = 0; |
| 2815 | pDest->flags = aFlag[t&1]; |
| 2816 | } |
danielk1977 | 36963fd | 2005-02-19 08:18:05 +0000 | [diff] [blame] | 2817 | }else{ |
drh | a1851ef | 2016-05-20 19:51:28 +0000 | [diff] [blame] | 2818 | pDest->enc = encoding; |
drh | 58c9608 | 2013-12-23 11:33:32 +0000 | [diff] [blame] | 2819 | /* This branch happens only when content is on overflow pages */ |
drh | 380d685 | 2013-11-20 20:58:00 +0000 | [diff] [blame] | 2820 | if( ((pOp->p5 & (OPFLAG_LENGTHARG|OPFLAG_TYPEOFARG))!=0 |
| 2821 | && ((t>=12 && (t&1)==0) || (pOp->p5 & OPFLAG_TYPEOFARG)!=0)) |
| 2822 | || (len = sqlite3VdbeSerialTypeLen(t))==0 |
drh | c8606e4 | 2013-11-20 19:28:03 +0000 | [diff] [blame] | 2823 | ){ |
drh | 2a2a696 | 2014-09-16 18:22:44 +0000 | [diff] [blame] | 2824 | /* Content is irrelevant for |
| 2825 | ** 1. the typeof() function, |
| 2826 | ** 2. the length(X) function if X is a blob, and |
| 2827 | ** 3. if the content length is zero. |
| 2828 | ** So we might as well use bogus content rather than reading |
dan | 1f9144e | 2017-03-17 13:59:06 +0000 | [diff] [blame] | 2829 | ** content from disk. |
| 2830 | ** |
| 2831 | ** Although sqlite3VdbeSerialGet() may read at most 8 bytes from the |
| 2832 | ** buffer passed to it, debugging function VdbeMemPrettyPrint() may |
drh | cbae3f8 | 2020-01-06 20:48:45 +0000 | [diff] [blame] | 2833 | ** read more. Use the global constant sqlite3CtypeMap[] as the array, |
| 2834 | ** as that array is 256 bytes long (plenty for VdbeMemPrettyPrint()) |
| 2835 | ** and it begins with a bunch of zeros. |
dan | 1f9144e | 2017-03-17 13:59:06 +0000 | [diff] [blame] | 2836 | */ |
drh | cbae3f8 | 2020-01-06 20:48:45 +0000 | [diff] [blame] | 2837 | sqlite3VdbeSerialGet((u8*)sqlite3CtypeMap, t, pDest); |
danielk1977 | aee18ef | 2005-03-09 12:26:50 +0000 | [diff] [blame] | 2838 | }else{ |
drh | cb3cabd | 2016-11-25 19:18:28 +0000 | [diff] [blame] | 2839 | rc = sqlite3VdbeMemFromBtree(pC->uc.pCursor, aOffset[p2], len, pDest); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 2840 | if( rc!=SQLITE_OK ) goto abort_due_to_error; |
| 2841 | sqlite3VdbeSerialGet((const u8*)pDest->z, t, pDest); |
| 2842 | pDest->flags &= ~MEM_Ephem; |
danielk1977 | aee18ef | 2005-03-09 12:26:50 +0000 | [diff] [blame] | 2843 | } |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 2844 | } |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 2845 | |
danielk1977 | 3c9cc8d | 2005-01-17 03:40:08 +0000 | [diff] [blame] | 2846 | op_column_out: |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 2847 | UPDATE_MAX_BLOBSIZE(pDest); |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 2848 | REGISTER_TRACE(pOp->p3, pDest); |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2849 | break; |
drh | 74588ce | 2017-09-13 00:13:05 +0000 | [diff] [blame] | 2850 | |
| 2851 | op_column_corrupt: |
| 2852 | if( aOp[0].p3>0 ){ |
| 2853 | pOp = &aOp[aOp[0].p3-1]; |
| 2854 | break; |
| 2855 | }else{ |
| 2856 | rc = SQLITE_CORRUPT_BKPT; |
| 2857 | goto abort_due_to_error; |
| 2858 | } |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2859 | } |
| 2860 | |
danielk1977 | 751de56 | 2008-04-18 09:01:15 +0000 | [diff] [blame] | 2861 | /* Opcode: Affinity P1 P2 * P4 * |
drh | f63552b | 2013-10-30 00:25:03 +0000 | [diff] [blame] | 2862 | ** Synopsis: affinity(r[P1@P2]) |
danielk1977 | 751de56 | 2008-04-18 09:01:15 +0000 | [diff] [blame] | 2863 | ** |
| 2864 | ** Apply affinities to a range of P2 registers starting with P1. |
| 2865 | ** |
drh | bb6783b | 2017-04-29 18:02:49 +0000 | [diff] [blame] | 2866 | ** P4 is a string that is P2 characters long. The N-th character of the |
| 2867 | ** string indicates the column affinity that should be used for the N-th |
danielk1977 | 751de56 | 2008-04-18 09:01:15 +0000 | [diff] [blame] | 2868 | ** memory cell in the range. |
| 2869 | */ |
| 2870 | case OP_Affinity: { |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 2871 | const char *zAffinity; /* The affinity to be applied */ |
danielk1977 | 751de56 | 2008-04-18 09:01:15 +0000 | [diff] [blame] | 2872 | |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2873 | zAffinity = pOp->p4.z; |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 2874 | assert( zAffinity!=0 ); |
drh | 662c50e | 2017-04-01 20:14:01 +0000 | [diff] [blame] | 2875 | assert( pOp->p2>0 ); |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 2876 | assert( zAffinity[pOp->p2]==0 ); |
| 2877 | pIn1 = &aMem[pOp->p1]; |
drh | 122c514 | 2019-07-29 05:23:01 +0000 | [diff] [blame] | 2878 | while( 1 /*exit-by-break*/ ){ |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 2879 | assert( pIn1 <= &p->aMem[(p->nMem+1 - p->nCursor)] ); |
drh | b5f6243 | 2019-12-10 02:48:41 +0000 | [diff] [blame] | 2880 | assert( zAffinity[0]==SQLITE_AFF_NONE || memIsValid(pIn1) ); |
drh | 83a1daf | 2019-05-01 18:59:33 +0000 | [diff] [blame] | 2881 | applyAffinity(pIn1, zAffinity[0], encoding); |
| 2882 | if( zAffinity[0]==SQLITE_AFF_REAL && (pIn1->flags & MEM_Int)!=0 ){ |
drh | 337cc39 | 2019-07-29 06:06:53 +0000 | [diff] [blame] | 2883 | /* When applying REAL affinity, if the result is still an MEM_Int |
| 2884 | ** that will fit in 6 bytes, then change the type to MEM_IntReal |
| 2885 | ** so that we keep the high-resolution integer value but know that |
| 2886 | ** the type really wants to be REAL. */ |
| 2887 | testcase( pIn1->u.i==140737488355328LL ); |
| 2888 | testcase( pIn1->u.i==140737488355327LL ); |
| 2889 | testcase( pIn1->u.i==-140737488355328LL ); |
| 2890 | testcase( pIn1->u.i==-140737488355329LL ); |
| 2891 | if( pIn1->u.i<=140737488355327LL && pIn1->u.i>=-140737488355328LL ){ |
| 2892 | pIn1->flags |= MEM_IntReal; |
| 2893 | pIn1->flags &= ~MEM_Int; |
| 2894 | }else{ |
| 2895 | pIn1->u.r = (double)pIn1->u.i; |
| 2896 | pIn1->flags |= MEM_Real; |
| 2897 | pIn1->flags &= ~MEM_Int; |
| 2898 | } |
drh | 83a1daf | 2019-05-01 18:59:33 +0000 | [diff] [blame] | 2899 | } |
drh | 6fcc1ec | 2019-05-01 14:41:47 +0000 | [diff] [blame] | 2900 | REGISTER_TRACE((int)(pIn1-aMem), pIn1); |
drh | 83a1daf | 2019-05-01 18:59:33 +0000 | [diff] [blame] | 2901 | zAffinity++; |
| 2902 | if( zAffinity[0]==0 ) break; |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 2903 | pIn1++; |
drh | 83a1daf | 2019-05-01 18:59:33 +0000 | [diff] [blame] | 2904 | } |
danielk1977 | 751de56 | 2008-04-18 09:01:15 +0000 | [diff] [blame] | 2905 | break; |
| 2906 | } |
| 2907 | |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2908 | /* Opcode: MakeRecord P1 P2 P3 P4 * |
drh | f63552b | 2013-10-30 00:25:03 +0000 | [diff] [blame] | 2909 | ** Synopsis: r[P3]=mkrec(r[P1@P2]) |
drh | 7a224de | 2004-06-02 01:22:02 +0000 | [diff] [blame] | 2910 | ** |
drh | 710c484 | 2010-08-30 01:17:20 +0000 | [diff] [blame] | 2911 | ** Convert P2 registers beginning with P1 into the [record format] |
| 2912 | ** use as a data record in a database table or as a key |
| 2913 | ** in an index. The OP_Column opcode can decode the record later. |
drh | 7a224de | 2004-06-02 01:22:02 +0000 | [diff] [blame] | 2914 | ** |
drh | bb6783b | 2017-04-29 18:02:49 +0000 | [diff] [blame] | 2915 | ** P4 may be a string that is P2 characters long. The N-th character of the |
| 2916 | ** string indicates the column affinity that should be used for the N-th |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2917 | ** field of the index key. |
drh | 7a224de | 2004-06-02 01:22:02 +0000 | [diff] [blame] | 2918 | ** |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 2919 | ** The mapping from character to affinity is given by the SQLITE_AFF_ |
| 2920 | ** macros defined in sqliteInt.h. |
drh | 7a224de | 2004-06-02 01:22:02 +0000 | [diff] [blame] | 2921 | ** |
drh | 05883a3 | 2015-06-02 15:32:08 +0000 | [diff] [blame] | 2922 | ** If P4 is NULL then all index fields have the affinity BLOB. |
drh | 7f057c9 | 2005-06-24 03:53:06 +0000 | [diff] [blame] | 2923 | */ |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2924 | case OP_MakeRecord: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2925 | Mem *pRec; /* The new record */ |
| 2926 | u64 nData; /* Number of bytes of data space */ |
| 2927 | int nHdr; /* Number of bytes of header space */ |
| 2928 | i64 nByte; /* Data space required for this record */ |
drh | 4a33507 | 2015-04-11 02:08:48 +0000 | [diff] [blame] | 2929 | i64 nZero; /* Number of zero bytes at the end of the record */ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2930 | int nVarint; /* Number of bytes in a varint */ |
| 2931 | u32 serial_type; /* Type field */ |
| 2932 | Mem *pData0; /* First field to be combined into the record */ |
| 2933 | Mem *pLast; /* Last field of the record */ |
| 2934 | int nField; /* Number of fields in the record */ |
| 2935 | char *zAffinity; /* The affinity string for the record */ |
| 2936 | int file_format; /* File format to use for encoding */ |
drh | be37c12 | 2015-10-16 14:54:17 +0000 | [diff] [blame] | 2937 | u32 len; /* Length of a field */ |
drh | b70b0df | 2019-04-30 01:08:42 +0000 | [diff] [blame] | 2938 | u8 *zHdr; /* Where to write next byte of the header */ |
| 2939 | u8 *zPayload; /* Where to write next byte of the payload */ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2940 | |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 2941 | /* Assuming the record contains N fields, the record format looks |
| 2942 | ** like this: |
| 2943 | ** |
drh | 7a224de | 2004-06-02 01:22:02 +0000 | [diff] [blame] | 2944 | ** ------------------------------------------------------------------------ |
| 2945 | ** | hdr-size | type 0 | type 1 | ... | type N-1 | data0 | ... | data N-1 | |
| 2946 | ** ------------------------------------------------------------------------ |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 2947 | ** |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2948 | ** 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] | 2949 | ** and so forth. |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 2950 | ** |
| 2951 | ** Each type field is a varint representing the serial type of the |
| 2952 | ** corresponding data element (see sqlite3VdbeSerialType()). The |
drh | 7a224de | 2004-06-02 01:22:02 +0000 | [diff] [blame] | 2953 | ** hdr-size field is also a varint which is the offset from the beginning |
| 2954 | ** of the record to data0. |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 2955 | */ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2956 | nData = 0; /* Number of bytes of data space */ |
| 2957 | nHdr = 0; /* Number of bytes of header space */ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2958 | nZero = 0; /* Number of zero bytes at the end of the record */ |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2959 | nField = pOp->p1; |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 2960 | zAffinity = pOp->p4.z; |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 2961 | 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] | 2962 | pData0 = &aMem[nField]; |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2963 | nField = pOp->p2; |
| 2964 | pLast = &pData0[nField-1]; |
drh | d946db0 | 2005-12-29 19:23:06 +0000 | [diff] [blame] | 2965 | file_format = p->minWriteFileFormat; |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 2966 | |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 2967 | /* Identify the output register */ |
| 2968 | assert( pOp->p3<pOp->p1 || pOp->p3>=pOp->p1+pOp->p2 ); |
| 2969 | pOut = &aMem[pOp->p3]; |
| 2970 | memAboutToChange(p, pOut); |
| 2971 | |
drh | 3e6c060 | 2013-12-10 20:53:01 +0000 | [diff] [blame] | 2972 | /* Apply the requested affinity to all inputs |
| 2973 | */ |
| 2974 | assert( pData0<=pLast ); |
| 2975 | if( zAffinity ){ |
| 2976 | pRec = pData0; |
| 2977 | do{ |
drh | 5ad1251 | 2019-05-09 16:22:51 +0000 | [diff] [blame] | 2978 | applyAffinity(pRec, zAffinity[0], encoding); |
dan | be81262 | 2019-05-17 15:59:11 +0000 | [diff] [blame] | 2979 | if( zAffinity[0]==SQLITE_AFF_REAL && (pRec->flags & MEM_Int) ){ |
| 2980 | pRec->flags |= MEM_IntReal; |
| 2981 | pRec->flags &= ~(MEM_Int); |
| 2982 | } |
drh | 5ad1251 | 2019-05-09 16:22:51 +0000 | [diff] [blame] | 2983 | REGISTER_TRACE((int)(pRec-aMem), pRec); |
| 2984 | zAffinity++; |
| 2985 | pRec++; |
drh | 57bf4a8 | 2014-02-17 14:59:22 +0000 | [diff] [blame] | 2986 | assert( zAffinity[0]==0 || pRec<=pLast ); |
| 2987 | }while( zAffinity[0] ); |
drh | 3e6c060 | 2013-12-10 20:53:01 +0000 | [diff] [blame] | 2988 | } |
| 2989 | |
drh | d447dce | 2017-01-25 20:55:11 +0000 | [diff] [blame] | 2990 | #ifdef SQLITE_ENABLE_NULL_TRIM |
drh | 585ce19 | 2017-01-25 14:58:27 +0000 | [diff] [blame] | 2991 | /* NULLs can be safely trimmed from the end of the record, as long as |
| 2992 | ** as the schema format is 2 or more and none of the omitted columns |
| 2993 | ** have a non-NULL default value. Also, the record must be left with |
| 2994 | ** at least one field. If P5>0 then it will be one more than the |
| 2995 | ** index of the right-most column with a non-NULL default value */ |
| 2996 | if( pOp->p5 ){ |
| 2997 | while( (pLast->flags & MEM_Null)!=0 && nField>pOp->p5 ){ |
| 2998 | pLast--; |
| 2999 | nField--; |
| 3000 | } |
| 3001 | } |
drh | d447dce | 2017-01-25 20:55:11 +0000 | [diff] [blame] | 3002 | #endif |
drh | 585ce19 | 2017-01-25 14:58:27 +0000 | [diff] [blame] | 3003 | |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 3004 | /* Loop through the elements that will make up the record to figure |
drh | 76fd7be | 2019-07-11 19:50:18 +0000 | [diff] [blame] | 3005 | ** out how much space is required for the new record. After this loop, |
| 3006 | ** the Mem.uTemp field of each term should hold the serial-type that will |
| 3007 | ** be used for that term in the generated record: |
| 3008 | ** |
| 3009 | ** Mem.uTemp value type |
| 3010 | ** --------------- --------------- |
| 3011 | ** 0 NULL |
| 3012 | ** 1 1-byte signed integer |
| 3013 | ** 2 2-byte signed integer |
| 3014 | ** 3 3-byte signed integer |
| 3015 | ** 4 4-byte signed integer |
| 3016 | ** 5 6-byte signed integer |
| 3017 | ** 6 8-byte signed integer |
| 3018 | ** 7 IEEE float |
| 3019 | ** 8 Integer constant 0 |
| 3020 | ** 9 Integer constant 1 |
| 3021 | ** 10,11 reserved for expansion |
| 3022 | ** N>=12 and even BLOB |
| 3023 | ** N>=13 and odd text |
| 3024 | ** |
| 3025 | ** The following additional values are computed: |
| 3026 | ** nHdr Number of bytes needed for the record header |
| 3027 | ** nData Number of bytes of data space needed for the record |
| 3028 | ** nZero Zero bytes at the end of the record |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 3029 | */ |
drh | 038b7bc | 2013-12-09 23:17:22 +0000 | [diff] [blame] | 3030 | pRec = pLast; |
drh | 59bf00c | 2013-12-08 23:33:28 +0000 | [diff] [blame] | 3031 | do{ |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 3032 | assert( memIsValid(pRec) ); |
drh | c1da439 | 2019-07-11 19:22:36 +0000 | [diff] [blame] | 3033 | if( pRec->flags & MEM_Null ){ |
| 3034 | if( pRec->flags & MEM_Zero ){ |
drh | 41fb367 | 2018-01-12 23:18:38 +0000 | [diff] [blame] | 3035 | /* Values with MEM_Null and MEM_Zero are created by xColumn virtual |
| 3036 | ** table methods that never invoke sqlite3_result_xxxxx() while |
| 3037 | ** computing an unchanging column value in an UPDATE statement. |
| 3038 | ** Give such values a special internal-use-only serial-type of 10 |
| 3039 | ** so that they can be passed through to xUpdate and have |
| 3040 | ** a true sqlite3_value_nochange(). */ |
| 3041 | assert( pOp->p5==OPFLAG_NOCHNG_MAGIC || CORRUPT_DB ); |
drh | c1da439 | 2019-07-11 19:22:36 +0000 | [diff] [blame] | 3042 | pRec->uTemp = 10; |
drh | 038b7bc | 2013-12-09 23:17:22 +0000 | [diff] [blame] | 3043 | }else{ |
drh | 76fd7be | 2019-07-11 19:50:18 +0000 | [diff] [blame] | 3044 | pRec->uTemp = 0; |
drh | 038b7bc | 2013-12-09 23:17:22 +0000 | [diff] [blame] | 3045 | } |
drh | c1da439 | 2019-07-11 19:22:36 +0000 | [diff] [blame] | 3046 | nHdr++; |
| 3047 | }else if( pRec->flags & (MEM_Int|MEM_IntReal) ){ |
| 3048 | /* Figure out whether to use 1, 2, 4, 6 or 8 bytes. */ |
| 3049 | i64 i = pRec->u.i; |
drh | 9c3bb59 | 2019-07-30 21:00:13 +0000 | [diff] [blame] | 3050 | u64 uu; |
drh | c1da439 | 2019-07-11 19:22:36 +0000 | [diff] [blame] | 3051 | testcase( pRec->flags & MEM_Int ); |
| 3052 | testcase( pRec->flags & MEM_IntReal ); |
| 3053 | if( i<0 ){ |
drh | 9c3bb59 | 2019-07-30 21:00:13 +0000 | [diff] [blame] | 3054 | uu = ~i; |
drh | c1da439 | 2019-07-11 19:22:36 +0000 | [diff] [blame] | 3055 | }else{ |
drh | 9c3bb59 | 2019-07-30 21:00:13 +0000 | [diff] [blame] | 3056 | uu = i; |
drh | c1da439 | 2019-07-11 19:22:36 +0000 | [diff] [blame] | 3057 | } |
| 3058 | nHdr++; |
drh | 9c3bb59 | 2019-07-30 21:00:13 +0000 | [diff] [blame] | 3059 | testcase( uu==127 ); testcase( uu==128 ); |
| 3060 | testcase( uu==32767 ); testcase( uu==32768 ); |
| 3061 | testcase( uu==8388607 ); testcase( uu==8388608 ); |
| 3062 | testcase( uu==2147483647 ); testcase( uu==2147483648 ); |
| 3063 | testcase( uu==140737488355327LL ); testcase( uu==140737488355328LL ); |
| 3064 | if( uu<=127 ){ |
drh | c1da439 | 2019-07-11 19:22:36 +0000 | [diff] [blame] | 3065 | if( (i&1)==i && file_format>=4 ){ |
drh | 9c3bb59 | 2019-07-30 21:00:13 +0000 | [diff] [blame] | 3066 | pRec->uTemp = 8+(u32)uu; |
drh | c1da439 | 2019-07-11 19:22:36 +0000 | [diff] [blame] | 3067 | }else{ |
| 3068 | nData++; |
| 3069 | pRec->uTemp = 1; |
| 3070 | } |
drh | 9c3bb59 | 2019-07-30 21:00:13 +0000 | [diff] [blame] | 3071 | }else if( uu<=32767 ){ |
drh | c1da439 | 2019-07-11 19:22:36 +0000 | [diff] [blame] | 3072 | nData += 2; |
| 3073 | pRec->uTemp = 2; |
drh | 9c3bb59 | 2019-07-30 21:00:13 +0000 | [diff] [blame] | 3074 | }else if( uu<=8388607 ){ |
drh | c1da439 | 2019-07-11 19:22:36 +0000 | [diff] [blame] | 3075 | nData += 3; |
| 3076 | pRec->uTemp = 3; |
drh | 9c3bb59 | 2019-07-30 21:00:13 +0000 | [diff] [blame] | 3077 | }else if( uu<=2147483647 ){ |
drh | c1da439 | 2019-07-11 19:22:36 +0000 | [diff] [blame] | 3078 | nData += 4; |
| 3079 | pRec->uTemp = 4; |
drh | 9c3bb59 | 2019-07-30 21:00:13 +0000 | [diff] [blame] | 3080 | }else if( uu<=140737488355327LL ){ |
drh | c1da439 | 2019-07-11 19:22:36 +0000 | [diff] [blame] | 3081 | nData += 6; |
| 3082 | pRec->uTemp = 5; |
| 3083 | }else{ |
| 3084 | nData += 8; |
| 3085 | if( pRec->flags & MEM_IntReal ){ |
| 3086 | /* If the value is IntReal and is going to take up 8 bytes to store |
| 3087 | ** as an integer, then we might as well make it an 8-byte floating |
| 3088 | ** point value */ |
| 3089 | pRec->u.r = (double)pRec->u.i; |
| 3090 | pRec->flags &= ~MEM_IntReal; |
| 3091 | pRec->flags |= MEM_Real; |
| 3092 | pRec->uTemp = 7; |
| 3093 | }else{ |
| 3094 | pRec->uTemp = 6; |
| 3095 | } |
| 3096 | } |
| 3097 | }else if( pRec->flags & MEM_Real ){ |
| 3098 | nHdr++; |
| 3099 | nData += 8; |
| 3100 | pRec->uTemp = 7; |
| 3101 | }else{ |
| 3102 | assert( db->mallocFailed || pRec->flags&(MEM_Str|MEM_Blob) ); |
| 3103 | assert( pRec->n>=0 ); |
| 3104 | len = (u32)pRec->n; |
| 3105 | serial_type = (len*2) + 12 + ((pRec->flags & MEM_Str)!=0); |
| 3106 | if( pRec->flags & MEM_Zero ){ |
| 3107 | serial_type += pRec->u.nZero*2; |
| 3108 | if( nData ){ |
| 3109 | if( sqlite3VdbeMemExpandBlob(pRec) ) goto no_mem; |
| 3110 | len += pRec->u.nZero; |
| 3111 | }else{ |
| 3112 | nZero += pRec->u.nZero; |
| 3113 | } |
| 3114 | } |
| 3115 | nData += len; |
| 3116 | nHdr += sqlite3VarintLen(serial_type); |
| 3117 | pRec->uTemp = serial_type; |
drh | fdf972a | 2007-05-02 13:30:27 +0000 | [diff] [blame] | 3118 | } |
drh | 45c3c66 | 2016-04-07 14:16:16 +0000 | [diff] [blame] | 3119 | if( pRec==pData0 ) break; |
| 3120 | pRec--; |
| 3121 | }while(1); |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 3122 | |
drh | 654858d | 2014-11-20 02:18:14 +0000 | [diff] [blame] | 3123 | /* EVIDENCE-OF: R-22564-11647 The header begins with a single varint |
| 3124 | ** which determines the total number of bytes in the header. The varint |
| 3125 | ** value is the size of the header in bytes including the size varint |
| 3126 | ** itself. */ |
drh | 59bf00c | 2013-12-08 23:33:28 +0000 | [diff] [blame] | 3127 | testcase( nHdr==126 ); |
| 3128 | testcase( nHdr==127 ); |
drh | 2a24287 | 2013-12-08 22:59:29 +0000 | [diff] [blame] | 3129 | if( nHdr<=126 ){ |
| 3130 | /* The common case */ |
| 3131 | nHdr += 1; |
| 3132 | }else{ |
| 3133 | /* Rare case of a really large header */ |
| 3134 | nVarint = sqlite3VarintLen(nHdr); |
| 3135 | nHdr += nVarint; |
| 3136 | if( nVarint<sqlite3VarintLen(nHdr) ) nHdr++; |
drh | cb9882a | 2005-03-17 03:15:40 +0000 | [diff] [blame] | 3137 | } |
drh | 038b7bc | 2013-12-09 23:17:22 +0000 | [diff] [blame] | 3138 | nByte = nHdr+nData; |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 3139 | |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 3140 | /* Make sure the output register has a buffer large enough to store |
| 3141 | ** the new record. The output register (pOp->p3) is not allowed to |
| 3142 | ** be one of the input registers (because the following call to |
drh | 322f285 | 2014-09-19 00:43:39 +0000 | [diff] [blame] | 3143 | ** sqlite3VdbeMemClearAndResize() could clobber the value before it is used). |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 3144 | */ |
drh | 0d7f0cc | 2018-09-21 13:07:14 +0000 | [diff] [blame] | 3145 | if( nByte+nZero<=pOut->szMalloc ){ |
| 3146 | /* The output register is already large enough to hold the record. |
| 3147 | ** No error checks or buffer enlargement is required */ |
| 3148 | pOut->z = pOut->zMalloc; |
| 3149 | }else{ |
| 3150 | /* Need to make sure that the output is not too big and then enlarge |
| 3151 | ** the output register to hold the full result */ |
| 3152 | if( nByte+nZero>db->aLimit[SQLITE_LIMIT_LENGTH] ){ |
| 3153 | goto too_big; |
| 3154 | } |
| 3155 | if( sqlite3VdbeMemClearAndResize(pOut, (int)nByte) ){ |
| 3156 | goto no_mem; |
| 3157 | } |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 3158 | } |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 3159 | pOut->n = (int)nByte; |
drh | c91b2fd | 2014-03-01 18:13:23 +0000 | [diff] [blame] | 3160 | pOut->flags = MEM_Blob; |
drh | fdf972a | 2007-05-02 13:30:27 +0000 | [diff] [blame] | 3161 | if( nZero ){ |
drh | 8df3284 | 2008-12-09 02:51:23 +0000 | [diff] [blame] | 3162 | pOut->u.nZero = nZero; |
drh | 477df4b | 2008-01-05 18:48:24 +0000 | [diff] [blame] | 3163 | pOut->flags |= MEM_Zero; |
drh | fdf972a | 2007-05-02 13:30:27 +0000 | [diff] [blame] | 3164 | } |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 3165 | UPDATE_MAX_BLOBSIZE(pOut); |
drh | b70b0df | 2019-04-30 01:08:42 +0000 | [diff] [blame] | 3166 | zHdr = (u8 *)pOut->z; |
| 3167 | zPayload = zHdr + nHdr; |
| 3168 | |
| 3169 | /* Write the record */ |
| 3170 | zHdr += putVarint32(zHdr, nHdr); |
| 3171 | assert( pData0<=pLast ); |
| 3172 | pRec = pData0; |
| 3173 | do{ |
| 3174 | serial_type = pRec->uTemp; |
| 3175 | /* EVIDENCE-OF: R-06529-47362 Following the size varint are one or more |
| 3176 | ** additional varints, one per column. */ |
| 3177 | zHdr += putVarint32(zHdr, serial_type); /* serial type */ |
| 3178 | /* EVIDENCE-OF: R-64536-51728 The values for each column in the record |
| 3179 | ** immediately follow the header. */ |
| 3180 | zPayload += sqlite3VdbeSerialPut(zPayload, pRec, serial_type); /* content */ |
| 3181 | }while( (++pRec)<=pLast ); |
| 3182 | assert( nHdr==(int)(zHdr - (u8*)pOut->z) ); |
| 3183 | assert( nByte==(int)(zPayload - (u8*)pOut->z) ); |
| 3184 | |
| 3185 | assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) ); |
| 3186 | REGISTER_TRACE(pOp->p3, pOut); |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 3187 | break; |
| 3188 | } |
| 3189 | |
drh | 9f27463 | 2020-03-17 17:11:23 +0000 | [diff] [blame] | 3190 | /* Opcode: Count P1 P2 p3 * * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 3191 | ** Synopsis: r[P2]=count() |
danielk1977 | a553316 | 2009-02-24 10:01:51 +0000 | [diff] [blame] | 3192 | ** |
| 3193 | ** Store the number of entries (an integer value) in the table or index |
drh | 9f27463 | 2020-03-17 17:11:23 +0000 | [diff] [blame] | 3194 | ** opened by cursor P1 in register P2. |
| 3195 | ** |
| 3196 | ** If P3==0, then an exact count is obtained, which involves visiting |
| 3197 | ** every btree page of the table. But if P3 is non-zero, an estimate |
| 3198 | ** is returned based on the current cursor position. |
danielk1977 | a553316 | 2009-02-24 10:01:51 +0000 | [diff] [blame] | 3199 | */ |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 3200 | case OP_Count: { /* out2 */ |
danielk1977 | a553316 | 2009-02-24 10:01:51 +0000 | [diff] [blame] | 3201 | i64 nEntry; |
drh | c54a617 | 2009-06-02 16:06:03 +0000 | [diff] [blame] | 3202 | BtCursor *pCrsr; |
| 3203 | |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 3204 | assert( p->apCsr[pOp->p1]->eCurType==CURTYPE_BTREE ); |
| 3205 | pCrsr = p->apCsr[pOp->p1]->uc.pCursor; |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 3206 | assert( pCrsr ); |
drh | 9f27463 | 2020-03-17 17:11:23 +0000 | [diff] [blame] | 3207 | if( pOp->p3 ){ |
| 3208 | nEntry = sqlite3BtreeRowCountEst(pCrsr); |
| 3209 | }else{ |
| 3210 | nEntry = 0; /* Not needed. Only used to silence a warning. */ |
| 3211 | rc = sqlite3BtreeCount(db, pCrsr, &nEntry); |
| 3212 | if( rc ) goto abort_due_to_error; |
| 3213 | } |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 3214 | pOut = out2Prerelease(p, pOp); |
danielk1977 | a553316 | 2009-02-24 10:01:51 +0000 | [diff] [blame] | 3215 | pOut->u.i = nEntry; |
drh | 21f6daa | 2019-10-11 14:21:48 +0000 | [diff] [blame] | 3216 | goto check_for_interrupt; |
danielk1977 | a553316 | 2009-02-24 10:01:51 +0000 | [diff] [blame] | 3217 | } |
danielk1977 | a553316 | 2009-02-24 10:01:51 +0000 | [diff] [blame] | 3218 | |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3219 | /* Opcode: Savepoint P1 * * P4 * |
| 3220 | ** |
| 3221 | ** Open, release or rollback the savepoint named by parameter P4, depending |
drh | 2ce9b6b | 2019-05-10 14:03:07 +0000 | [diff] [blame] | 3222 | ** on the value of P1. To open a new savepoint set P1==0 (SAVEPOINT_BEGIN). |
| 3223 | ** To release (commit) an existing savepoint set P1==1 (SAVEPOINT_RELEASE). |
| 3224 | ** To rollback an existing savepoint set P1==2 (SAVEPOINT_ROLLBACK). |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3225 | */ |
| 3226 | case OP_Savepoint: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3227 | int p1; /* Value of P1 operand */ |
| 3228 | char *zName; /* Name of savepoint */ |
| 3229 | int nName; |
| 3230 | Savepoint *pNew; |
| 3231 | Savepoint *pSavepoint; |
| 3232 | Savepoint *pTmp; |
| 3233 | int iSavepoint; |
| 3234 | int ii; |
| 3235 | |
| 3236 | p1 = pOp->p1; |
| 3237 | zName = pOp->p4.z; |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3238 | |
| 3239 | /* Assert that the p1 parameter is valid. Also that if there is no open |
| 3240 | ** transaction, then there cannot be any savepoints. |
| 3241 | */ |
| 3242 | assert( db->pSavepoint==0 || db->autoCommit==0 ); |
| 3243 | assert( p1==SAVEPOINT_BEGIN||p1==SAVEPOINT_RELEASE||p1==SAVEPOINT_ROLLBACK ); |
| 3244 | assert( db->pSavepoint || db->isTransactionSavepoint==0 ); |
| 3245 | assert( checkSavepointCount(db) ); |
dan | c0537fe | 2013-06-28 19:41:43 +0000 | [diff] [blame] | 3246 | assert( p->bIsReader ); |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3247 | |
| 3248 | if( p1==SAVEPOINT_BEGIN ){ |
drh | 4f7d3a5 | 2013-06-27 23:54:02 +0000 | [diff] [blame] | 3249 | if( db->nVdbeWrite>0 ){ |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3250 | /* A new savepoint cannot be created if there are active write |
| 3251 | ** statements (i.e. open read/write incremental blob handles). |
| 3252 | */ |
drh | 22c17b8 | 2015-05-15 04:13:15 +0000 | [diff] [blame] | 3253 | sqlite3VdbeError(p, "cannot open savepoint - SQL statements in progress"); |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3254 | rc = SQLITE_BUSY; |
| 3255 | }else{ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3256 | nName = sqlite3Strlen30(zName); |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3257 | |
drh | be07ec5 | 2011-06-03 12:15:26 +0000 | [diff] [blame] | 3258 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
dan | d9495cd | 2011-04-27 12:08:04 +0000 | [diff] [blame] | 3259 | /* This call is Ok even if this savepoint is actually a transaction |
| 3260 | ** savepoint (and therefore should not prompt xSavepoint()) callbacks. |
| 3261 | ** If this is a transaction savepoint being opened, it is guaranteed |
| 3262 | ** that the db->aVTrans[] array is empty. */ |
| 3263 | assert( db->autoCommit==0 || db->nVTrans==0 ); |
drh | a24bc9c | 2011-05-24 00:35:56 +0000 | [diff] [blame] | 3264 | rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN, |
| 3265 | db->nStatement+db->nSavepoint); |
dan | d9495cd | 2011-04-27 12:08:04 +0000 | [diff] [blame] | 3266 | if( rc!=SQLITE_OK ) goto abort_due_to_error; |
drh | 305ebab | 2011-05-26 14:19:14 +0000 | [diff] [blame] | 3267 | #endif |
dan | d9495cd | 2011-04-27 12:08:04 +0000 | [diff] [blame] | 3268 | |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3269 | /* Create a new savepoint structure. */ |
drh | 575fad6 | 2016-02-05 13:38:36 +0000 | [diff] [blame] | 3270 | pNew = sqlite3DbMallocRawNN(db, sizeof(Savepoint)+nName+1); |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3271 | if( pNew ){ |
| 3272 | pNew->zName = (char *)&pNew[1]; |
| 3273 | memcpy(pNew->zName, zName, nName+1); |
| 3274 | |
| 3275 | /* If there is no open transaction, then mark this as a special |
| 3276 | ** "transaction savepoint". */ |
| 3277 | if( db->autoCommit ){ |
| 3278 | db->autoCommit = 0; |
| 3279 | db->isTransactionSavepoint = 1; |
| 3280 | }else{ |
| 3281 | db->nSavepoint++; |
danielk1977 | d829335 | 2009-04-30 09:10:37 +0000 | [diff] [blame] | 3282 | } |
dan | 21e8d01 | 2011-03-03 20:05:59 +0000 | [diff] [blame] | 3283 | |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3284 | /* Link the new savepoint into the database handle's list. */ |
| 3285 | pNew->pNext = db->pSavepoint; |
| 3286 | db->pSavepoint = pNew; |
dan | ba9108b | 2009-09-22 07:13:42 +0000 | [diff] [blame] | 3287 | pNew->nDeferredCons = db->nDeferredCons; |
dan | cb3e4b7 | 2013-07-03 19:53:05 +0000 | [diff] [blame] | 3288 | pNew->nDeferredImmCons = db->nDeferredImmCons; |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3289 | } |
| 3290 | } |
| 3291 | }else{ |
drh | 2ce9b6b | 2019-05-10 14:03:07 +0000 | [diff] [blame] | 3292 | assert( p1==SAVEPOINT_RELEASE || p1==SAVEPOINT_ROLLBACK ); |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3293 | iSavepoint = 0; |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3294 | |
| 3295 | /* Find the named savepoint. If there is no such savepoint, then an |
| 3296 | ** an error is returned to the user. */ |
| 3297 | for( |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3298 | pSavepoint = db->pSavepoint; |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3299 | pSavepoint && sqlite3StrICmp(pSavepoint->zName, zName); |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3300 | pSavepoint = pSavepoint->pNext |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3301 | ){ |
| 3302 | iSavepoint++; |
| 3303 | } |
| 3304 | if( !pSavepoint ){ |
drh | 22c17b8 | 2015-05-15 04:13:15 +0000 | [diff] [blame] | 3305 | sqlite3VdbeError(p, "no such savepoint: %s", zName); |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3306 | rc = SQLITE_ERROR; |
drh | 4f7d3a5 | 2013-06-27 23:54:02 +0000 | [diff] [blame] | 3307 | }else if( db->nVdbeWrite>0 && p1==SAVEPOINT_RELEASE ){ |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3308 | /* It is not possible to release (commit) a savepoint if there are |
drh | 0f198a7 | 2012-02-13 16:43:16 +0000 | [diff] [blame] | 3309 | ** active write statements. |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3310 | */ |
drh | 22c17b8 | 2015-05-15 04:13:15 +0000 | [diff] [blame] | 3311 | sqlite3VdbeError(p, "cannot release savepoint - " |
| 3312 | "SQL statements in progress"); |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3313 | rc = SQLITE_BUSY; |
| 3314 | }else{ |
| 3315 | |
| 3316 | /* Determine whether or not this is a transaction savepoint. If so, |
danielk1977 | 34cf35d | 2008-12-18 18:31:38 +0000 | [diff] [blame] | 3317 | ** and this is a RELEASE command, then the current transaction |
| 3318 | ** is committed. |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3319 | */ |
| 3320 | int isTransaction = pSavepoint->pNext==0 && db->isTransactionSavepoint; |
| 3321 | if( isTransaction && p1==SAVEPOINT_RELEASE ){ |
dan | 32b09f2 | 2009-09-23 17:29:59 +0000 | [diff] [blame] | 3322 | if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){ |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 3323 | goto vdbe_return; |
| 3324 | } |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3325 | db->autoCommit = 1; |
| 3326 | if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){ |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 3327 | p->pc = (int)(pOp - aOp); |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3328 | db->autoCommit = 0; |
| 3329 | p->rc = rc = SQLITE_BUSY; |
| 3330 | goto vdbe_return; |
| 3331 | } |
danielk1977 | 34cf35d | 2008-12-18 18:31:38 +0000 | [diff] [blame] | 3332 | rc = p->rc; |
drh | 94649b6 | 2019-12-18 02:12:04 +0000 | [diff] [blame] | 3333 | if( rc ){ |
| 3334 | db->autoCommit = 0; |
| 3335 | }else{ |
| 3336 | db->isTransactionSavepoint = 0; |
| 3337 | } |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3338 | }else{ |
drh | 47b7fc7 | 2014-11-11 01:33:57 +0000 | [diff] [blame] | 3339 | int isSchemaChange; |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3340 | iSavepoint = db->nSavepoint - iSavepoint - 1; |
drh | 31f1005 | 2012-03-31 17:17:26 +0000 | [diff] [blame] | 3341 | if( p1==SAVEPOINT_ROLLBACK ){ |
drh | 8257aa8 | 2017-07-26 19:59:13 +0000 | [diff] [blame] | 3342 | isSchemaChange = (db->mDbFlags & DBFLAG_SchemaChange)!=0; |
drh | 31f1005 | 2012-03-31 17:17:26 +0000 | [diff] [blame] | 3343 | for(ii=0; ii<db->nDb; ii++){ |
drh | 77b1dee | 2014-11-17 17:13:06 +0000 | [diff] [blame] | 3344 | rc = sqlite3BtreeTripAllCursors(db->aDb[ii].pBt, |
| 3345 | SQLITE_ABORT_ROLLBACK, |
drh | 47b7fc7 | 2014-11-11 01:33:57 +0000 | [diff] [blame] | 3346 | isSchemaChange==0); |
dan | 8023104 | 2014-11-12 14:56:02 +0000 | [diff] [blame] | 3347 | if( rc!=SQLITE_OK ) goto abort_due_to_error; |
drh | 31f1005 | 2012-03-31 17:17:26 +0000 | [diff] [blame] | 3348 | } |
drh | 47b7fc7 | 2014-11-11 01:33:57 +0000 | [diff] [blame] | 3349 | }else{ |
drh | 2ce9b6b | 2019-05-10 14:03:07 +0000 | [diff] [blame] | 3350 | assert( p1==SAVEPOINT_RELEASE ); |
drh | 47b7fc7 | 2014-11-11 01:33:57 +0000 | [diff] [blame] | 3351 | isSchemaChange = 0; |
drh | 0f198a7 | 2012-02-13 16:43:16 +0000 | [diff] [blame] | 3352 | } |
| 3353 | for(ii=0; ii<db->nDb; ii++){ |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3354 | rc = sqlite3BtreeSavepoint(db->aDb[ii].pBt, p1, iSavepoint); |
| 3355 | if( rc!=SQLITE_OK ){ |
| 3356 | goto abort_due_to_error; |
danielk1977 | bd43455 | 2009-03-18 10:33:00 +0000 | [diff] [blame] | 3357 | } |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3358 | } |
drh | 47b7fc7 | 2014-11-11 01:33:57 +0000 | [diff] [blame] | 3359 | if( isSchemaChange ){ |
drh | ba968db | 2018-07-24 22:02:12 +0000 | [diff] [blame] | 3360 | sqlite3ExpirePreparedStatements(db, 0); |
drh | 81028a4 | 2012-05-15 18:28:27 +0000 | [diff] [blame] | 3361 | sqlite3ResetAllSchemasOfConnection(db); |
drh | 8257aa8 | 2017-07-26 19:59:13 +0000 | [diff] [blame] | 3362 | db->mDbFlags |= DBFLAG_SchemaChange; |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3363 | } |
| 3364 | } |
drh | 95866af | 2019-12-15 00:36:33 +0000 | [diff] [blame] | 3365 | if( rc ) goto abort_due_to_error; |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3366 | |
| 3367 | /* Regardless of whether this is a RELEASE or ROLLBACK, destroy all |
| 3368 | ** savepoints nested inside of the savepoint being operated on. */ |
| 3369 | while( db->pSavepoint!=pSavepoint ){ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3370 | pTmp = db->pSavepoint; |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3371 | db->pSavepoint = pTmp->pNext; |
| 3372 | sqlite3DbFree(db, pTmp); |
| 3373 | db->nSavepoint--; |
| 3374 | } |
| 3375 | |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 3376 | /* If it is a RELEASE, then destroy the savepoint being operated on |
| 3377 | ** too. If it is a ROLLBACK TO, then set the number of deferred |
| 3378 | ** constraint violations present in the database to the value stored |
| 3379 | ** when the savepoint was created. */ |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3380 | if( p1==SAVEPOINT_RELEASE ){ |
| 3381 | assert( pSavepoint==db->pSavepoint ); |
| 3382 | db->pSavepoint = pSavepoint->pNext; |
| 3383 | sqlite3DbFree(db, pSavepoint); |
| 3384 | if( !isTransaction ){ |
| 3385 | db->nSavepoint--; |
| 3386 | } |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 3387 | }else{ |
drh | 2ce9b6b | 2019-05-10 14:03:07 +0000 | [diff] [blame] | 3388 | assert( p1==SAVEPOINT_ROLLBACK ); |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 3389 | db->nDeferredCons = pSavepoint->nDeferredCons; |
dan | cb3e4b7 | 2013-07-03 19:53:05 +0000 | [diff] [blame] | 3390 | db->nDeferredImmCons = pSavepoint->nDeferredImmCons; |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3391 | } |
dan | d9495cd | 2011-04-27 12:08:04 +0000 | [diff] [blame] | 3392 | |
dan | ea8562e | 2015-04-18 16:25:54 +0000 | [diff] [blame] | 3393 | if( !isTransaction || p1==SAVEPOINT_ROLLBACK ){ |
dan | d9495cd | 2011-04-27 12:08:04 +0000 | [diff] [blame] | 3394 | rc = sqlite3VtabSavepoint(db, p1, iSavepoint); |
| 3395 | if( rc!=SQLITE_OK ) goto abort_due_to_error; |
| 3396 | } |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3397 | } |
| 3398 | } |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 3399 | if( rc ) goto abort_due_to_error; |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3400 | |
| 3401 | break; |
| 3402 | } |
| 3403 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 3404 | /* Opcode: AutoCommit P1 P2 * * * |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 3405 | ** |
| 3406 | ** 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] | 3407 | ** back any currently active btree transactions. If there are any active |
drh | c25eabe | 2009-02-24 18:57:31 +0000 | [diff] [blame] | 3408 | ** VMs (apart from this one), then a ROLLBACK fails. A COMMIT fails if |
| 3409 | ** there are active writing VMs or active VMs that use shared cache. |
drh | 92f02c3 | 2004-09-02 14:57:08 +0000 | [diff] [blame] | 3410 | ** |
| 3411 | ** This instruction causes the VM to halt. |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 3412 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3413 | case OP_AutoCommit: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3414 | int desiredAutoCommit; |
shane | 68c0273 | 2009-06-09 18:14:18 +0000 | [diff] [blame] | 3415 | int iRollback; |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 3416 | |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3417 | desiredAutoCommit = pOp->p1; |
shane | 68c0273 | 2009-06-09 18:14:18 +0000 | [diff] [blame] | 3418 | iRollback = pOp->p2; |
drh | ad4a4b8 | 2008-11-05 16:37:34 +0000 | [diff] [blame] | 3419 | assert( desiredAutoCommit==1 || desiredAutoCommit==0 ); |
shane | 68c0273 | 2009-06-09 18:14:18 +0000 | [diff] [blame] | 3420 | assert( desiredAutoCommit==1 || iRollback==0 ); |
drh | 4f7d3a5 | 2013-06-27 23:54:02 +0000 | [diff] [blame] | 3421 | assert( db->nVdbeActive>0 ); /* At least this one VM is active */ |
dan | c0537fe | 2013-06-28 19:41:43 +0000 | [diff] [blame] | 3422 | assert( p->bIsReader ); |
danielk1977 | 46c43ed | 2004-06-30 06:30:25 +0000 | [diff] [blame] | 3423 | |
drh | b0c8865 | 2016-02-01 13:21:13 +0000 | [diff] [blame] | 3424 | if( desiredAutoCommit!=db->autoCommit ){ |
shane | 68c0273 | 2009-06-09 18:14:18 +0000 | [diff] [blame] | 3425 | if( iRollback ){ |
drh | ad4a4b8 | 2008-11-05 16:37:34 +0000 | [diff] [blame] | 3426 | assert( desiredAutoCommit==1 ); |
drh | 21021a5 | 2012-02-13 17:01:51 +0000 | [diff] [blame] | 3427 | sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK); |
danielk1977 | f3f06bb | 2005-12-16 15:24:28 +0000 | [diff] [blame] | 3428 | db->autoCommit = 1; |
drh | b0c8865 | 2016-02-01 13:21:13 +0000 | [diff] [blame] | 3429 | }else if( desiredAutoCommit && db->nVdbeWrite>0 ){ |
| 3430 | /* If this instruction implements a COMMIT and other VMs are writing |
| 3431 | ** return an error indicating that the other VMs must complete first. |
| 3432 | */ |
| 3433 | sqlite3VdbeError(p, "cannot commit transaction - " |
| 3434 | "SQL statements in progress"); |
| 3435 | rc = SQLITE_BUSY; |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 3436 | goto abort_due_to_error; |
dan | 32b09f2 | 2009-09-23 17:29:59 +0000 | [diff] [blame] | 3437 | }else if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){ |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 3438 | goto vdbe_return; |
danielk1977 | f3f06bb | 2005-12-16 15:24:28 +0000 | [diff] [blame] | 3439 | }else{ |
shane | 7d3846a | 2008-12-11 02:58:26 +0000 | [diff] [blame] | 3440 | db->autoCommit = (u8)desiredAutoCommit; |
drh | 8ff2587 | 2015-07-31 18:59:56 +0000 | [diff] [blame] | 3441 | } |
| 3442 | if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){ |
| 3443 | p->pc = (int)(pOp - aOp); |
| 3444 | db->autoCommit = (u8)(1-desiredAutoCommit); |
| 3445 | p->rc = rc = SQLITE_BUSY; |
| 3446 | goto vdbe_return; |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 3447 | } |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3448 | sqlite3CloseSavepoints(db); |
drh | 83968c4 | 2007-04-18 16:45:24 +0000 | [diff] [blame] | 3449 | if( p->rc==SQLITE_OK ){ |
drh | 900b31e | 2007-08-28 02:27:51 +0000 | [diff] [blame] | 3450 | rc = SQLITE_DONE; |
drh | 83968c4 | 2007-04-18 16:45:24 +0000 | [diff] [blame] | 3451 | }else{ |
drh | 900b31e | 2007-08-28 02:27:51 +0000 | [diff] [blame] | 3452 | rc = SQLITE_ERROR; |
drh | 83968c4 | 2007-04-18 16:45:24 +0000 | [diff] [blame] | 3453 | } |
drh | 900b31e | 2007-08-28 02:27:51 +0000 | [diff] [blame] | 3454 | goto vdbe_return; |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 3455 | }else{ |
drh | 22c17b8 | 2015-05-15 04:13:15 +0000 | [diff] [blame] | 3456 | sqlite3VdbeError(p, |
drh | ad4a4b8 | 2008-11-05 16:37:34 +0000 | [diff] [blame] | 3457 | (!desiredAutoCommit)?"cannot start a transaction within a transaction":( |
shane | 68c0273 | 2009-06-09 18:14:18 +0000 | [diff] [blame] | 3458 | (iRollback)?"cannot rollback - no transaction is active": |
drh | f089aa4 | 2008-07-08 19:34:06 +0000 | [diff] [blame] | 3459 | "cannot commit - no transaction is active")); |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 3460 | |
| 3461 | rc = SQLITE_ERROR; |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 3462 | goto abort_due_to_error; |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 3463 | } |
drh | 8616cff | 2019-07-13 16:15:23 +0000 | [diff] [blame] | 3464 | /*NOTREACHED*/ assert(0); |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 3465 | } |
| 3466 | |
drh | b22f7c8 | 2014-02-06 23:56:27 +0000 | [diff] [blame] | 3467 | /* Opcode: Transaction P1 P2 P3 P4 P5 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3468 | ** |
drh | 05a86c5 | 2014-02-16 01:55:49 +0000 | [diff] [blame] | 3469 | ** Begin a transaction on database P1 if a transaction is not already |
| 3470 | ** active. |
| 3471 | ** If P2 is non-zero, then a write-transaction is started, or if a |
| 3472 | ** read-transaction is already active, it is upgraded to a write-transaction. |
| 3473 | ** If P2 is zero, then a read-transaction is started. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3474 | ** |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 3475 | ** P1 is the index of the database file on which the transaction is |
| 3476 | ** started. Index 0 is the main database file and index 1 is the |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 3477 | ** file used for temporary tables. Indices of 2 or more are used for |
| 3478 | ** attached databases. |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 3479 | ** |
dan | e0af83a | 2009-09-08 19:15:01 +0000 | [diff] [blame] | 3480 | ** If a write-transaction is started and the Vdbe.usesStmtJournal flag is |
| 3481 | ** true (this flag is set if the Vdbe may modify more than one row and may |
| 3482 | ** throw an ABORT exception), a statement transaction may also be opened. |
| 3483 | ** More specifically, a statement transaction is opened iff the database |
| 3484 | ** connection is currently not in autocommit mode, or if there are other |
drh | a451017 | 2012-02-02 15:50:17 +0000 | [diff] [blame] | 3485 | ** active statements. A statement transaction allows the changes made by this |
dan | e0af83a | 2009-09-08 19:15:01 +0000 | [diff] [blame] | 3486 | ** VDBE to be rolled back after an error without having to roll back the |
| 3487 | ** entire transaction. If no error is encountered, the statement transaction |
| 3488 | ** will automatically commit when the VDBE halts. |
| 3489 | ** |
drh | b22f7c8 | 2014-02-06 23:56:27 +0000 | [diff] [blame] | 3490 | ** If P5!=0 then this opcode also checks the schema cookie against P3 |
| 3491 | ** and the schema generation counter against P4. |
| 3492 | ** The cookie changes its value whenever the database schema changes. |
| 3493 | ** This operation is used to detect when that the cookie has changed |
drh | 05a86c5 | 2014-02-16 01:55:49 +0000 | [diff] [blame] | 3494 | ** and that the current process needs to reread the schema. If the schema |
| 3495 | ** cookie in P3 differs from the schema cookie in the database header or |
| 3496 | ** if the schema generation counter in P4 differs from the current |
| 3497 | ** generation counter, then an SQLITE_SCHEMA error is raised and execution |
| 3498 | ** halts. The sqlite3_step() wrapper function might then reprepare the |
| 3499 | ** statement and rerun it from the beginning. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3500 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3501 | case OP_Transaction: { |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 3502 | Btree *pBt; |
drh | bb2d9b1 | 2018-06-06 16:28:40 +0000 | [diff] [blame] | 3503 | int iMeta = 0; |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 3504 | |
drh | 1713afb | 2013-06-28 01:24:57 +0000 | [diff] [blame] | 3505 | assert( p->bIsReader ); |
drh | 9e92a47 | 2013-06-27 17:40:30 +0000 | [diff] [blame] | 3506 | assert( p->readOnly==0 || pOp->p2==0 ); |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 3507 | assert( pOp->p1>=0 && pOp->p1<db->nDb ); |
drh | a7ab6d8 | 2014-07-21 15:44:39 +0000 | [diff] [blame] | 3508 | assert( DbMaskTest(p->btreeMask, pOp->p1) ); |
drh | 13447bf | 2013-07-10 13:33:49 +0000 | [diff] [blame] | 3509 | if( pOp->p2 && (db->flags & SQLITE_QueryOnly)!=0 ){ |
| 3510 | rc = SQLITE_READONLY; |
| 3511 | goto abort_due_to_error; |
| 3512 | } |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 3513 | pBt = db->aDb[pOp->p1].pBt; |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 3514 | |
danielk1977 | 24162fe | 2004-06-04 06:22:00 +0000 | [diff] [blame] | 3515 | if( pBt ){ |
drh | bb2d9b1 | 2018-06-06 16:28:40 +0000 | [diff] [blame] | 3516 | rc = sqlite3BtreeBeginTrans(pBt, pOp->p2, &iMeta); |
drh | cbd8db3 | 2015-08-20 17:18:32 +0000 | [diff] [blame] | 3517 | testcase( rc==SQLITE_BUSY_SNAPSHOT ); |
| 3518 | testcase( rc==SQLITE_BUSY_RECOVERY ); |
drh | 9e9f1bd | 2009-10-13 15:36:51 +0000 | [diff] [blame] | 3519 | if( rc!=SQLITE_OK ){ |
drh | fadd2b1 | 2016-09-19 23:39:34 +0000 | [diff] [blame] | 3520 | if( (rc&0xff)==SQLITE_BUSY ){ |
| 3521 | p->pc = (int)(pOp - aOp); |
| 3522 | p->rc = rc; |
| 3523 | goto vdbe_return; |
| 3524 | } |
danielk1977 | 24162fe | 2004-06-04 06:22:00 +0000 | [diff] [blame] | 3525 | goto abort_due_to_error; |
drh | 90bfcda | 2001-09-23 19:46:51 +0000 | [diff] [blame] | 3526 | } |
dan | e0af83a | 2009-09-08 19:15:01 +0000 | [diff] [blame] | 3527 | |
drh | 4d29448 | 2019-10-05 15:28:24 +0000 | [diff] [blame] | 3528 | if( p->usesStmtJournal |
| 3529 | && pOp->p2 |
dan | c0537fe | 2013-06-28 19:41:43 +0000 | [diff] [blame] | 3530 | && (db->autoCommit==0 || db->nVdbeRead>1) |
dan | e0af83a | 2009-09-08 19:15:01 +0000 | [diff] [blame] | 3531 | ){ |
| 3532 | assert( sqlite3BtreeIsInTrans(pBt) ); |
| 3533 | if( p->iStatement==0 ){ |
| 3534 | assert( db->nStatement>=0 && db->nSavepoint>=0 ); |
| 3535 | db->nStatement++; |
| 3536 | p->iStatement = db->nSavepoint + db->nStatement; |
| 3537 | } |
dan | a311b80 | 2011-04-26 19:21:34 +0000 | [diff] [blame] | 3538 | |
drh | 346506f | 2011-05-25 01:16:42 +0000 | [diff] [blame] | 3539 | rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN, p->iStatement-1); |
dan | a311b80 | 2011-04-26 19:21:34 +0000 | [diff] [blame] | 3540 | if( rc==SQLITE_OK ){ |
| 3541 | rc = sqlite3BtreeBeginStmt(pBt, p->iStatement); |
| 3542 | } |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 3543 | |
| 3544 | /* Store the current value of the database handles deferred constraint |
| 3545 | ** counter. If the statement transaction needs to be rolled back, |
| 3546 | ** the value of this counter needs to be restored too. */ |
| 3547 | p->nStmtDefCons = db->nDeferredCons; |
dan | cb3e4b7 | 2013-07-03 19:53:05 +0000 | [diff] [blame] | 3548 | p->nStmtDefImmCons = db->nDeferredImmCons; |
dan | e0af83a | 2009-09-08 19:15:01 +0000 | [diff] [blame] | 3549 | } |
drh | 397776a | 2018-06-06 17:45:51 +0000 | [diff] [blame] | 3550 | } |
| 3551 | assert( pOp->p5==0 || pOp->p4type==P4_INT32 ); |
| 3552 | if( pOp->p5 |
| 3553 | && (iMeta!=pOp->p3 |
| 3554 | || db->aDb[pOp->p1].pSchema->iGeneration!=pOp->p4.i) |
| 3555 | ){ |
| 3556 | /* |
drh | 96fdcb4 | 2016-09-27 00:09:33 +0000 | [diff] [blame] | 3557 | ** IMPLEMENTATION-OF: R-03189-51135 As each SQL statement runs, the schema |
| 3558 | ** version is checked to ensure that the schema has not changed since the |
| 3559 | ** SQL statement was prepared. |
drh | 51a74d4 | 2015-02-28 01:04:27 +0000 | [diff] [blame] | 3560 | */ |
drh | b22f7c8 | 2014-02-06 23:56:27 +0000 | [diff] [blame] | 3561 | sqlite3DbFree(db, p->zErrMsg); |
| 3562 | p->zErrMsg = sqlite3DbStrDup(db, "database schema has changed"); |
| 3563 | /* If the schema-cookie from the database file matches the cookie |
| 3564 | ** stored with the in-memory representation of the schema, do |
| 3565 | ** not reload the schema from the database file. |
| 3566 | ** |
| 3567 | ** If virtual-tables are in use, this is not just an optimization. |
| 3568 | ** Often, v-tables store their data in other SQLite tables, which |
| 3569 | ** are queried from within xNext() and other v-table methods using |
| 3570 | ** prepared queries. If such a query is out-of-date, we do not want to |
| 3571 | ** discard the database schema, as the user code implementing the |
| 3572 | ** v-table would have to be ready for the sqlite3_vtab structure itself |
| 3573 | ** to be invalidated whenever sqlite3_step() is called from within |
| 3574 | ** a v-table method. |
| 3575 | */ |
| 3576 | if( db->aDb[pOp->p1].pSchema->schema_cookie!=iMeta ){ |
| 3577 | sqlite3ResetOneSchema(db, pOp->p1); |
| 3578 | } |
| 3579 | p->expired = 1; |
| 3580 | rc = SQLITE_SCHEMA; |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 3581 | } |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 3582 | if( rc ) goto abort_due_to_error; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3583 | break; |
| 3584 | } |
| 3585 | |
drh | b1fdb2a | 2008-01-05 04:06:03 +0000 | [diff] [blame] | 3586 | /* Opcode: ReadCookie P1 P2 P3 * * |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 3587 | ** |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3588 | ** Read cookie number P3 from database P1 and write it into register P2. |
danielk1977 | 0d19f7a | 2009-06-03 11:25:07 +0000 | [diff] [blame] | 3589 | ** P3==1 is the schema version. P3==2 is the database format. |
| 3590 | ** 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] | 3591 | ** the main database file and P1==1 is the database file used to store |
| 3592 | ** temporary tables. |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 3593 | ** |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 3594 | ** There must be a read-lock on the database (either a transaction |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 3595 | ** must be started or there must be an open cursor) before |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 3596 | ** executing this instruction. |
| 3597 | */ |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 3598 | case OP_ReadCookie: { /* out2 */ |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 3599 | int iMeta; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3600 | int iDb; |
| 3601 | int iCookie; |
danielk1977 | 180b56a | 2007-06-24 08:00:42 +0000 | [diff] [blame] | 3602 | |
drh | 1713afb | 2013-06-28 01:24:57 +0000 | [diff] [blame] | 3603 | assert( p->bIsReader ); |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3604 | iDb = pOp->p1; |
| 3605 | iCookie = pOp->p3; |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 3606 | assert( pOp->p3<SQLITE_N_BTREE_META ); |
danielk1977 | 180b56a | 2007-06-24 08:00:42 +0000 | [diff] [blame] | 3607 | assert( iDb>=0 && iDb<db->nDb ); |
| 3608 | assert( db->aDb[iDb].pBt!=0 ); |
drh | a7ab6d8 | 2014-07-21 15:44:39 +0000 | [diff] [blame] | 3609 | assert( DbMaskTest(p->btreeMask, iDb) ); |
danielk1977 | 0d19f7a | 2009-06-03 11:25:07 +0000 | [diff] [blame] | 3610 | |
danielk1977 | 602b466 | 2009-07-02 07:47:33 +0000 | [diff] [blame] | 3611 | sqlite3BtreeGetMeta(db->aDb[iDb].pBt, iCookie, (u32 *)&iMeta); |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 3612 | pOut = out2Prerelease(p, pOp); |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 3613 | pOut->u.i = iMeta; |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 3614 | break; |
| 3615 | } |
| 3616 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 3617 | /* Opcode: SetCookie P1 P2 P3 * * |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 3618 | ** |
drh | 1861afc | 2016-02-01 21:48:34 +0000 | [diff] [blame] | 3619 | ** Write the integer value P3 into cookie number P2 of database P1. |
| 3620 | ** P2==1 is the schema version. P2==2 is the database format. |
| 3621 | ** P2==3 is the recommended pager cache |
danielk1977 | 0d19f7a | 2009-06-03 11:25:07 +0000 | [diff] [blame] | 3622 | ** size, and so forth. P1==0 is the main database file and P1==1 is the |
| 3623 | ** database file used to store temporary tables. |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 3624 | ** |
| 3625 | ** A transaction must be started before executing this opcode. |
| 3626 | */ |
drh | 1861afc | 2016-02-01 21:48:34 +0000 | [diff] [blame] | 3627 | case OP_SetCookie: { |
drh | 3f7d4e4 | 2004-07-24 14:35:58 +0000 | [diff] [blame] | 3628 | Db *pDb; |
drh | 4031baf | 2018-05-28 17:31:20 +0000 | [diff] [blame] | 3629 | |
| 3630 | sqlite3VdbeIncrWriteCounter(p, 0); |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 3631 | assert( pOp->p2<SQLITE_N_BTREE_META ); |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 3632 | assert( pOp->p1>=0 && pOp->p1<db->nDb ); |
drh | a7ab6d8 | 2014-07-21 15:44:39 +0000 | [diff] [blame] | 3633 | assert( DbMaskTest(p->btreeMask, pOp->p1) ); |
drh | 9e92a47 | 2013-06-27 17:40:30 +0000 | [diff] [blame] | 3634 | assert( p->readOnly==0 ); |
drh | 3f7d4e4 | 2004-07-24 14:35:58 +0000 | [diff] [blame] | 3635 | pDb = &db->aDb[pOp->p1]; |
| 3636 | assert( pDb->pBt!=0 ); |
drh | 2120608 | 2011-04-04 18:22:02 +0000 | [diff] [blame] | 3637 | assert( sqlite3SchemaMutexHeld(db, pOp->p1, 0) ); |
drh | a3b321d | 2004-05-11 09:31:31 +0000 | [diff] [blame] | 3638 | /* See note about index shifting on OP_ReadCookie */ |
drh | 1861afc | 2016-02-01 21:48:34 +0000 | [diff] [blame] | 3639 | rc = sqlite3BtreeUpdateMeta(pDb->pBt, pOp->p2, pOp->p3); |
danielk1977 | 0d19f7a | 2009-06-03 11:25:07 +0000 | [diff] [blame] | 3640 | if( pOp->p2==BTREE_SCHEMA_VERSION ){ |
drh | 3f7d4e4 | 2004-07-24 14:35:58 +0000 | [diff] [blame] | 3641 | /* When the schema cookie changes, record the new cookie internally */ |
drh | 1861afc | 2016-02-01 21:48:34 +0000 | [diff] [blame] | 3642 | pDb->pSchema->schema_cookie = pOp->p3; |
drh | 8257aa8 | 2017-07-26 19:59:13 +0000 | [diff] [blame] | 3643 | db->mDbFlags |= DBFLAG_SchemaChange; |
danielk1977 | 0d19f7a | 2009-06-03 11:25:07 +0000 | [diff] [blame] | 3644 | }else if( pOp->p2==BTREE_FILE_FORMAT ){ |
drh | d28bcb3 | 2005-12-21 14:43:11 +0000 | [diff] [blame] | 3645 | /* Record changes in the file format */ |
drh | 1861afc | 2016-02-01 21:48:34 +0000 | [diff] [blame] | 3646 | pDb->pSchema->file_format = pOp->p3; |
drh | 3f7d4e4 | 2004-07-24 14:35:58 +0000 | [diff] [blame] | 3647 | } |
drh | fd426c6 | 2006-01-30 15:34:22 +0000 | [diff] [blame] | 3648 | if( pOp->p1==1 ){ |
| 3649 | /* Invalidate all prepared statements whenever the TEMP database |
| 3650 | ** schema is changed. Ticket #1644 */ |
drh | ba968db | 2018-07-24 22:02:12 +0000 | [diff] [blame] | 3651 | sqlite3ExpirePreparedStatements(db, 0); |
dan | fa401de | 2009-10-16 14:55:03 +0000 | [diff] [blame] | 3652 | p->expired = 0; |
drh | fd426c6 | 2006-01-30 15:34:22 +0000 | [diff] [blame] | 3653 | } |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 3654 | if( rc ) goto abort_due_to_error; |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 3655 | break; |
| 3656 | } |
| 3657 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 3658 | /* Opcode: OpenRead P1 P2 P3 P4 P5 |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 3659 | ** Synopsis: root=P2 iDb=P3 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3660 | ** |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 3661 | ** Open a read-only cursor for the database table whose root page is |
danielk1977 | 207872a | 2008-01-03 07:54:23 +0000 | [diff] [blame] | 3662 | ** P2 in a database file. The database file is determined by P3. |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 3663 | ** P3==0 means the main database, P3==1 means the database used for |
| 3664 | ** temporary tables, and P3>1 means used the corresponding attached |
| 3665 | ** database. Give the new cursor an identifier of P1. The P1 |
danielk1977 | 207872a | 2008-01-03 07:54:23 +0000 | [diff] [blame] | 3666 | ** values need not be contiguous but all P1 values should be small integers. |
| 3667 | ** It is an error for P1 to be negative. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3668 | ** |
drh | 8e9deb6 | 2018-06-05 13:43:02 +0000 | [diff] [blame] | 3669 | ** Allowed P5 bits: |
| 3670 | ** <ul> |
| 3671 | ** <li> <b>0x02 OPFLAG_SEEKEQ</b>: This cursor will only be used for |
| 3672 | ** equality lookups (implemented as a pair of opcodes OP_SeekGE/OP_IdxGT |
drh | 576d0a9 | 2020-03-12 17:28:27 +0000 | [diff] [blame] | 3673 | ** of OP_SeekLE/OP_IdxLT) |
drh | 8e9deb6 | 2018-06-05 13:43:02 +0000 | [diff] [blame] | 3674 | ** </ul> |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 3675 | ** |
danielk1977 | d336e22 | 2009-02-20 10:58:41 +0000 | [diff] [blame] | 3676 | ** The P4 value may be either an integer (P4_INT32) or a pointer to |
| 3677 | ** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo |
drh | 8e9deb6 | 2018-06-05 13:43:02 +0000 | [diff] [blame] | 3678 | ** object, then table being opened must be an [index b-tree] where the |
| 3679 | ** KeyInfo object defines the content and collating |
| 3680 | ** sequence of that index b-tree. Otherwise, if P4 is an integer |
| 3681 | ** value, then the table being opened must be a [table b-tree] with a |
| 3682 | ** number of columns no less than the value of P4. |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 3683 | ** |
drh | 3526319 | 2014-07-22 20:02:19 +0000 | [diff] [blame] | 3684 | ** See also: OpenWrite, ReopenIdx |
| 3685 | */ |
| 3686 | /* Opcode: ReopenIdx P1 P2 P3 P4 P5 |
| 3687 | ** Synopsis: root=P2 iDb=P3 |
| 3688 | ** |
drh | 8e9deb6 | 2018-06-05 13:43:02 +0000 | [diff] [blame] | 3689 | ** The ReopenIdx opcode works like OP_OpenRead except that it first |
| 3690 | ** checks to see if the cursor on P1 is already open on the same |
| 3691 | ** 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] | 3692 | ** if the cursor is already open, do not reopen it. |
| 3693 | ** |
drh | 8e9deb6 | 2018-06-05 13:43:02 +0000 | [diff] [blame] | 3694 | ** The ReopenIdx opcode may only be used with P5==0 or P5==OPFLAG_SEEKEQ |
| 3695 | ** and with P4 being a P4_KEYINFO object. Furthermore, the P3 value must |
| 3696 | ** be the same as every other ReopenIdx or OpenRead for the same cursor |
| 3697 | ** number. |
drh | 3526319 | 2014-07-22 20:02:19 +0000 | [diff] [blame] | 3698 | ** |
drh | 8e9deb6 | 2018-06-05 13:43:02 +0000 | [diff] [blame] | 3699 | ** Allowed P5 bits: |
| 3700 | ** <ul> |
| 3701 | ** <li> <b>0x02 OPFLAG_SEEKEQ</b>: This cursor will only be used for |
| 3702 | ** equality lookups (implemented as a pair of opcodes OP_SeekGE/OP_IdxGT |
drh | 576d0a9 | 2020-03-12 17:28:27 +0000 | [diff] [blame] | 3703 | ** of OP_SeekLE/OP_IdxLT) |
drh | 8e9deb6 | 2018-06-05 13:43:02 +0000 | [diff] [blame] | 3704 | ** </ul> |
| 3705 | ** |
| 3706 | ** See also: OP_OpenRead, OP_OpenWrite |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3707 | */ |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 3708 | /* Opcode: OpenWrite P1 P2 P3 P4 P5 |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 3709 | ** Synopsis: root=P2 iDb=P3 |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 3710 | ** |
| 3711 | ** 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] | 3712 | ** page is P2 (or whose root page is held in register P2 if the |
| 3713 | ** OPFLAG_P2ISREG bit is set in P5 - see below). |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 3714 | ** |
danielk1977 | d336e22 | 2009-02-20 10:58:41 +0000 | [diff] [blame] | 3715 | ** The P4 value may be either an integer (P4_INT32) or a pointer to |
| 3716 | ** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo |
drh | 8e9deb6 | 2018-06-05 13:43:02 +0000 | [diff] [blame] | 3717 | ** object, then table being opened must be an [index b-tree] where the |
| 3718 | ** KeyInfo object defines the content and collating |
| 3719 | ** sequence of that index b-tree. Otherwise, if P4 is an integer |
| 3720 | ** value, then the table being opened must be a [table b-tree] with a |
| 3721 | ** number of columns no less than the value of P4. |
jplyon | 5a56422 | 2003-06-02 06:15:58 +0000 | [diff] [blame] | 3722 | ** |
drh | 8e9deb6 | 2018-06-05 13:43:02 +0000 | [diff] [blame] | 3723 | ** Allowed P5 bits: |
| 3724 | ** <ul> |
| 3725 | ** <li> <b>0x02 OPFLAG_SEEKEQ</b>: This cursor will only be used for |
| 3726 | ** equality lookups (implemented as a pair of opcodes OP_SeekGE/OP_IdxGT |
drh | 576d0a9 | 2020-03-12 17:28:27 +0000 | [diff] [blame] | 3727 | ** of OP_SeekLE/OP_IdxLT) |
drh | 8e9deb6 | 2018-06-05 13:43:02 +0000 | [diff] [blame] | 3728 | ** <li> <b>0x08 OPFLAG_FORDELETE</b>: This cursor is used only to seek |
| 3729 | ** and subsequently delete entries in an index btree. This is a |
| 3730 | ** hint to the storage engine that the storage engine is allowed to |
| 3731 | ** ignore. The hint is not used by the official SQLite b*tree storage |
| 3732 | ** engine, but is used by COMDB2. |
| 3733 | ** <li> <b>0x10 OPFLAG_P2ISREG</b>: Use the content of register P2 |
| 3734 | ** as the root page, not the value of P2 itself. |
| 3735 | ** </ul> |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 3736 | ** |
drh | 8e9deb6 | 2018-06-05 13:43:02 +0000 | [diff] [blame] | 3737 | ** This instruction works like OpenRead except that it opens the cursor |
| 3738 | ** in read/write mode. |
| 3739 | ** |
| 3740 | ** See also: OP_OpenRead, OP_ReopenIdx |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 3741 | */ |
drh | 3526319 | 2014-07-22 20:02:19 +0000 | [diff] [blame] | 3742 | case OP_ReopenIdx: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3743 | int nField; |
| 3744 | KeyInfo *pKeyInfo; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3745 | int p2; |
| 3746 | int iDb; |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 3747 | int wrFlag; |
| 3748 | Btree *pX; |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 3749 | VdbeCursor *pCur; |
drh | d946db0 | 2005-12-29 19:23:06 +0000 | [diff] [blame] | 3750 | Db *pDb; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3751 | |
drh | e0997b3 | 2015-03-20 14:57:50 +0000 | [diff] [blame] | 3752 | assert( pOp->p5==0 || pOp->p5==OPFLAG_SEEKEQ ); |
drh | 3526319 | 2014-07-22 20:02:19 +0000 | [diff] [blame] | 3753 | assert( pOp->p4type==P4_KEYINFO ); |
| 3754 | pCur = p->apCsr[pOp->p1]; |
drh | e8f2c9d | 2014-08-06 17:49:13 +0000 | [diff] [blame] | 3755 | if( pCur && pCur->pgnoRoot==(u32)pOp->p2 ){ |
drh | 3526319 | 2014-07-22 20:02:19 +0000 | [diff] [blame] | 3756 | assert( pCur->iDb==pOp->p3 ); /* Guaranteed by the code generator */ |
drh | e0997b3 | 2015-03-20 14:57:50 +0000 | [diff] [blame] | 3757 | goto open_cursor_set_hints; |
drh | 3526319 | 2014-07-22 20:02:19 +0000 | [diff] [blame] | 3758 | } |
| 3759 | /* If the cursor is not currently open or is open on a different |
| 3760 | ** index, then fall through into OP_OpenRead to force a reopen */ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3761 | case OP_OpenRead: |
drh | 1fa509a | 2015-03-20 16:34:49 +0000 | [diff] [blame] | 3762 | case OP_OpenWrite: |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3763 | |
drh | e0997b3 | 2015-03-20 14:57:50 +0000 | [diff] [blame] | 3764 | assert( pOp->opcode==OP_OpenWrite || pOp->p5==0 || pOp->p5==OPFLAG_SEEKEQ ); |
drh | 1713afb | 2013-06-28 01:24:57 +0000 | [diff] [blame] | 3765 | assert( p->bIsReader ); |
drh | 3526319 | 2014-07-22 20:02:19 +0000 | [diff] [blame] | 3766 | assert( pOp->opcode==OP_OpenRead || pOp->opcode==OP_ReopenIdx |
| 3767 | || p->readOnly==0 ); |
dan | 428c218 | 2012-08-06 18:50:11 +0000 | [diff] [blame] | 3768 | |
drh | ba968db | 2018-07-24 22:02:12 +0000 | [diff] [blame] | 3769 | if( p->expired==1 ){ |
drh | 47b7fc7 | 2014-11-11 01:33:57 +0000 | [diff] [blame] | 3770 | rc = SQLITE_ABORT_ROLLBACK; |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 3771 | goto abort_due_to_error; |
dan | fa401de | 2009-10-16 14:55:03 +0000 | [diff] [blame] | 3772 | } |
| 3773 | |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3774 | nField = 0; |
| 3775 | pKeyInfo = 0; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3776 | p2 = pOp->p2; |
| 3777 | iDb = pOp->p3; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3778 | assert( iDb>=0 && iDb<db->nDb ); |
drh | a7ab6d8 | 2014-07-21 15:44:39 +0000 | [diff] [blame] | 3779 | assert( DbMaskTest(p->btreeMask, iDb) ); |
drh | d946db0 | 2005-12-29 19:23:06 +0000 | [diff] [blame] | 3780 | pDb = &db->aDb[iDb]; |
| 3781 | pX = pDb->pBt; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3782 | assert( pX!=0 ); |
drh | d946db0 | 2005-12-29 19:23:06 +0000 | [diff] [blame] | 3783 | if( pOp->opcode==OP_OpenWrite ){ |
dan | fd261ec | 2015-10-22 20:54:33 +0000 | [diff] [blame] | 3784 | assert( OPFLAG_FORDELETE==BTREE_FORDELETE ); |
| 3785 | wrFlag = BTREE_WRCSR | (pOp->p5 & OPFLAG_FORDELETE); |
drh | 2120608 | 2011-04-04 18:22:02 +0000 | [diff] [blame] | 3786 | assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3787 | if( pDb->pSchema->file_format < p->minWriteFileFormat ){ |
| 3788 | p->minWriteFileFormat = pDb->pSchema->file_format; |
drh | d946db0 | 2005-12-29 19:23:06 +0000 | [diff] [blame] | 3789 | } |
| 3790 | }else{ |
| 3791 | wrFlag = 0; |
| 3792 | } |
dan | 428c218 | 2012-08-06 18:50:11 +0000 | [diff] [blame] | 3793 | if( pOp->p5 & OPFLAG_P2ISREG ){ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3794 | assert( p2>0 ); |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 3795 | assert( p2<=(p->nMem+1 - p->nCursor) ); |
drh | 8e9deb6 | 2018-06-05 13:43:02 +0000 | [diff] [blame] | 3796 | assert( pOp->opcode==OP_OpenWrite ); |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 3797 | pIn2 = &aMem[p2]; |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 3798 | assert( memIsValid(pIn2) ); |
| 3799 | assert( (pIn2->flags & MEM_Int)!=0 ); |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3800 | sqlite3VdbeMemIntegerify(pIn2); |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 3801 | p2 = (int)pIn2->u.i; |
drh | 0f3f766 | 2017-08-18 14:34:28 +0000 | [diff] [blame] | 3802 | /* The p2 value always comes from a prior OP_CreateBtree opcode and |
drh | 9a65f2c | 2009-06-22 19:05:40 +0000 | [diff] [blame] | 3803 | ** that opcode will always set the p2 value to 2 or more or else fail. |
| 3804 | ** If there were a failure, the prepared statement would have halted |
| 3805 | ** before reaching this instruction. */ |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 3806 | assert( p2>=2 ); |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 3807 | } |
danielk1977 | d336e22 | 2009-02-20 10:58:41 +0000 | [diff] [blame] | 3808 | if( pOp->p4type==P4_KEYINFO ){ |
| 3809 | pKeyInfo = pOp->p4.pKeyInfo; |
drh | 41e13e1 | 2013-11-07 14:09:39 +0000 | [diff] [blame] | 3810 | assert( pKeyInfo->enc==ENC(db) ); |
| 3811 | assert( pKeyInfo->db==db ); |
drh | a485ad1 | 2017-08-02 22:43:14 +0000 | [diff] [blame] | 3812 | nField = pKeyInfo->nAllField; |
danielk1977 | d336e22 | 2009-02-20 10:58:41 +0000 | [diff] [blame] | 3813 | }else if( pOp->p4type==P4_INT32 ){ |
| 3814 | nField = pOp->p4.i; |
| 3815 | } |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 3816 | assert( pOp->p1>=0 ); |
drh | 399af1d | 2013-11-20 17:25:55 +0000 | [diff] [blame] | 3817 | assert( nField>=0 ); |
| 3818 | testcase( nField==0 ); /* Table with INTEGER PRIMARY KEY and nothing else */ |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 3819 | pCur = allocateCursor(p, pOp->p1, nField, iDb, CURTYPE_BTREE); |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 3820 | if( pCur==0 ) goto no_mem; |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 3821 | pCur->nullRow = 1; |
drh | d4187c7 | 2010-08-30 22:15:45 +0000 | [diff] [blame] | 3822 | pCur->isOrdered = 1; |
drh | 3526319 | 2014-07-22 20:02:19 +0000 | [diff] [blame] | 3823 | pCur->pgnoRoot = p2; |
drh | b89aeb6 | 2016-01-27 15:49:32 +0000 | [diff] [blame] | 3824 | #ifdef SQLITE_DEBUG |
| 3825 | pCur->wrFlag = wrFlag; |
| 3826 | #endif |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 3827 | rc = sqlite3BtreeCursor(pX, p2, wrFlag, pKeyInfo, pCur->uc.pCursor); |
danielk1977 | d336e22 | 2009-02-20 10:58:41 +0000 | [diff] [blame] | 3828 | pCur->pKeyInfo = pKeyInfo; |
drh | 14da87f | 2013-11-20 21:51:33 +0000 | [diff] [blame] | 3829 | /* Set the VdbeCursor.isTable variable. Previous versions of |
danielk1977 | 172114a | 2009-07-07 15:47:12 +0000 | [diff] [blame] | 3830 | ** SQLite used to check if the root-page flags were sane at this point |
| 3831 | ** and report database corruption if they were not, but this check has |
| 3832 | ** since moved into the btree layer. */ |
| 3833 | pCur->isTable = pOp->p4type!=P4_KEYINFO; |
drh | e0997b3 | 2015-03-20 14:57:50 +0000 | [diff] [blame] | 3834 | |
| 3835 | open_cursor_set_hints: |
| 3836 | assert( OPFLAG_BULKCSR==BTREE_BULKLOAD ); |
| 3837 | assert( OPFLAG_SEEKEQ==BTREE_SEEK_EQ ); |
drh | 0403cb3 | 2015-08-14 23:57:04 +0000 | [diff] [blame] | 3838 | testcase( pOp->p5 & OPFLAG_BULKCSR ); |
drh | 0403cb3 | 2015-08-14 23:57:04 +0000 | [diff] [blame] | 3839 | testcase( pOp->p2 & OPFLAG_SEEKEQ ); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 3840 | sqlite3BtreeCursorHintFlags(pCur->uc.pCursor, |
drh | f7854c7 | 2015-10-27 13:24:37 +0000 | [diff] [blame] | 3841 | (pOp->p5 & (OPFLAG_BULKCSR|OPFLAG_SEEKEQ))); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 3842 | if( rc ) goto abort_due_to_error; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3843 | break; |
| 3844 | } |
| 3845 | |
drh | e08e8d6 | 2017-05-01 15:15:41 +0000 | [diff] [blame] | 3846 | /* Opcode: OpenDup P1 P2 * * * |
| 3847 | ** |
| 3848 | ** Open a new cursor P1 that points to the same ephemeral table as |
| 3849 | ** cursor P2. The P2 cursor must have been opened by a prior OP_OpenEphemeral |
| 3850 | ** opcode. Only ephemeral cursors may be duplicated. |
| 3851 | ** |
| 3852 | ** Duplicate ephemeral cursors are used for self-joins of materialized views. |
| 3853 | */ |
| 3854 | case OP_OpenDup: { |
| 3855 | VdbeCursor *pOrig; /* The original cursor to be duplicated */ |
| 3856 | VdbeCursor *pCx; /* The new cursor */ |
| 3857 | |
| 3858 | pOrig = p->apCsr[pOp->p2]; |
dan | 2811ea6 | 2019-12-23 14:20:46 +0000 | [diff] [blame] | 3859 | assert( pOrig ); |
drh | e08e8d6 | 2017-05-01 15:15:41 +0000 | [diff] [blame] | 3860 | assert( pOrig->pBtx!=0 ); /* Only ephemeral cursors can be duplicated */ |
| 3861 | |
| 3862 | pCx = allocateCursor(p, pOp->p1, pOrig->nField, -1, CURTYPE_BTREE); |
| 3863 | if( pCx==0 ) goto no_mem; |
| 3864 | pCx->nullRow = 1; |
| 3865 | pCx->isEphemeral = 1; |
| 3866 | pCx->pKeyInfo = pOrig->pKeyInfo; |
| 3867 | pCx->isTable = pOrig->isTable; |
drh | 2c04131 | 2018-12-24 02:34:49 +0000 | [diff] [blame] | 3868 | pCx->pgnoRoot = pOrig->pgnoRoot; |
dan | a0f6b83 | 2019-03-14 16:36:20 +0000 | [diff] [blame] | 3869 | pCx->isOrdered = pOrig->isOrdered; |
drh | 2c04131 | 2018-12-24 02:34:49 +0000 | [diff] [blame] | 3870 | rc = sqlite3BtreeCursor(pOrig->pBtx, pCx->pgnoRoot, BTREE_WRCSR, |
drh | e08e8d6 | 2017-05-01 15:15:41 +0000 | [diff] [blame] | 3871 | pCx->pKeyInfo, pCx->uc.pCursor); |
drh | 3f4df4c | 2017-05-02 17:54:19 +0000 | [diff] [blame] | 3872 | /* The sqlite3BtreeCursor() routine can only fail for the first cursor |
| 3873 | ** opened for a database. Since there is already an open cursor when this |
| 3874 | ** opcode is run, the sqlite3BtreeCursor() cannot fail */ |
| 3875 | assert( rc==SQLITE_OK ); |
drh | e08e8d6 | 2017-05-01 15:15:41 +0000 | [diff] [blame] | 3876 | break; |
| 3877 | } |
| 3878 | |
| 3879 | |
drh | 2a5d990 | 2011-08-26 00:34:45 +0000 | [diff] [blame] | 3880 | /* Opcode: OpenEphemeral P1 P2 * P4 P5 |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 3881 | ** Synopsis: nColumn=P2 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3882 | ** |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 3883 | ** Open a new cursor P1 to a transient table. |
drh | 9170dd7 | 2005-07-08 17:13:46 +0000 | [diff] [blame] | 3884 | ** The cursor is always opened read/write even if |
drh | 25d3adb | 2010-04-05 15:11:08 +0000 | [diff] [blame] | 3885 | ** the main database is read-only. The ephemeral |
drh | 9170dd7 | 2005-07-08 17:13:46 +0000 | [diff] [blame] | 3886 | ** table is deleted automatically when the cursor is closed. |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 3887 | ** |
drh | dfe3b58 | 2019-01-04 12:35:50 +0000 | [diff] [blame] | 3888 | ** If the cursor P1 is already opened on an ephemeral table, the table |
drh | 4afdfa1 | 2018-12-31 16:36:42 +0000 | [diff] [blame] | 3889 | ** is cleared (all content is erased). |
| 3890 | ** |
drh | 25d3adb | 2010-04-05 15:11:08 +0000 | [diff] [blame] | 3891 | ** P2 is the number of columns in the ephemeral table. |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 3892 | ** The cursor points to a BTree table if P4==0 and to a BTree index |
| 3893 | ** 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] | 3894 | ** that defines the format of keys in the index. |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 3895 | ** |
drh | 2a5d990 | 2011-08-26 00:34:45 +0000 | [diff] [blame] | 3896 | ** The P5 parameter can be a mask of the BTREE_* flags defined |
| 3897 | ** in btree.h. These flags control aspects of the operation of |
| 3898 | ** the btree. The BTREE_OMIT_JOURNAL and BTREE_SINGLE flags are |
| 3899 | ** added automatically. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3900 | */ |
drh | a21a64d | 2010-04-06 22:33:55 +0000 | [diff] [blame] | 3901 | /* Opcode: OpenAutoindex P1 P2 * P4 * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 3902 | ** Synopsis: nColumn=P2 |
drh | a21a64d | 2010-04-06 22:33:55 +0000 | [diff] [blame] | 3903 | ** |
| 3904 | ** This opcode works the same as OP_OpenEphemeral. It has a |
| 3905 | ** different name to distinguish its use. Tables created using |
| 3906 | ** by this opcode will be used for automatically created transient |
| 3907 | ** indices in joins. |
| 3908 | */ |
| 3909 | case OP_OpenAutoindex: |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3910 | case OP_OpenEphemeral: { |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 3911 | VdbeCursor *pCx; |
drh | 41e13e1 | 2013-11-07 14:09:39 +0000 | [diff] [blame] | 3912 | KeyInfo *pKeyInfo; |
| 3913 | |
drh | d4187c7 | 2010-08-30 22:15:45 +0000 | [diff] [blame] | 3914 | static const int vfsFlags = |
drh | 33f4e02 | 2007-09-03 15:19:34 +0000 | [diff] [blame] | 3915 | SQLITE_OPEN_READWRITE | |
| 3916 | SQLITE_OPEN_CREATE | |
| 3917 | SQLITE_OPEN_EXCLUSIVE | |
| 3918 | SQLITE_OPEN_DELETEONCLOSE | |
| 3919 | SQLITE_OPEN_TRANSIENT_DB; |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 3920 | assert( pOp->p1>=0 ); |
drh | 399af1d | 2013-11-20 17:25:55 +0000 | [diff] [blame] | 3921 | assert( pOp->p2>=0 ); |
drh | 4afdfa1 | 2018-12-31 16:36:42 +0000 | [diff] [blame] | 3922 | pCx = p->apCsr[pOp->p1]; |
drh | 1ee02a1 | 2020-01-18 13:53:46 +0000 | [diff] [blame] | 3923 | if( pCx && pCx->pBtx ){ |
drh | 4afdfa1 | 2018-12-31 16:36:42 +0000 | [diff] [blame] | 3924 | /* If the ephermeral table is already open, erase all existing content |
| 3925 | ** so that the table is empty again, rather than creating a new table. */ |
dan | a512972 | 2019-05-03 18:50:24 +0000 | [diff] [blame] | 3926 | assert( pCx->isEphemeral ); |
dan | 855b5d1 | 2019-06-26 21:04:30 +0000 | [diff] [blame] | 3927 | pCx->seqCount = 0; |
| 3928 | pCx->cacheStatus = CACHE_STALE; |
drh | 1ee02a1 | 2020-01-18 13:53:46 +0000 | [diff] [blame] | 3929 | rc = sqlite3BtreeClearTable(pCx->pBtx, pCx->pgnoRoot, 0); |
drh | d0fb796 | 2018-12-31 17:58:05 +0000 | [diff] [blame] | 3930 | }else{ |
| 3931 | pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, CURTYPE_BTREE); |
| 3932 | if( pCx==0 ) goto no_mem; |
drh | d0fb796 | 2018-12-31 17:58:05 +0000 | [diff] [blame] | 3933 | pCx->isEphemeral = 1; |
| 3934 | rc = sqlite3BtreeOpen(db->pVfs, 0, db, &pCx->pBtx, |
| 3935 | BTREE_OMIT_JOURNAL | BTREE_SINGLE | pOp->p5, |
| 3936 | vfsFlags); |
| 3937 | if( rc==SQLITE_OK ){ |
| 3938 | rc = sqlite3BtreeBeginTrans(pCx->pBtx, 1, 0); |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 3939 | } |
drh | d0fb796 | 2018-12-31 17:58:05 +0000 | [diff] [blame] | 3940 | if( rc==SQLITE_OK ){ |
| 3941 | /* If a transient index is required, create it by calling |
| 3942 | ** sqlite3BtreeCreateTable() with the BTREE_BLOBKEY flag before |
| 3943 | ** opening it. If a transient table is required, just use the |
| 3944 | ** automatically created table with root-page 1 (an BLOB_INTKEY table). |
| 3945 | */ |
| 3946 | if( (pCx->pKeyInfo = pKeyInfo = pOp->p4.pKeyInfo)!=0 ){ |
| 3947 | assert( pOp->p4type==P4_KEYINFO ); |
| 3948 | rc = sqlite3BtreeCreateTable(pCx->pBtx, (int*)&pCx->pgnoRoot, |
| 3949 | BTREE_BLOBKEY | pOp->p5); |
| 3950 | if( rc==SQLITE_OK ){ |
| 3951 | assert( pCx->pgnoRoot==MASTER_ROOT+1 ); |
| 3952 | assert( pKeyInfo->db==db ); |
| 3953 | assert( pKeyInfo->enc==ENC(db) ); |
| 3954 | rc = sqlite3BtreeCursor(pCx->pBtx, pCx->pgnoRoot, BTREE_WRCSR, |
| 3955 | pKeyInfo, pCx->uc.pCursor); |
| 3956 | } |
| 3957 | pCx->isTable = 0; |
| 3958 | }else{ |
| 3959 | pCx->pgnoRoot = MASTER_ROOT; |
| 3960 | rc = sqlite3BtreeCursor(pCx->pBtx, MASTER_ROOT, BTREE_WRCSR, |
| 3961 | 0, pCx->uc.pCursor); |
| 3962 | pCx->isTable = 1; |
| 3963 | } |
| 3964 | } |
| 3965 | pCx->isOrdered = (pOp->p5!=BTREE_UNORDERED); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3966 | } |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 3967 | if( rc ) goto abort_due_to_error; |
dan | 855b5d1 | 2019-06-26 21:04:30 +0000 | [diff] [blame] | 3968 | pCx->nullRow = 1; |
dan | 5134d13 | 2011-09-02 10:31:11 +0000 | [diff] [blame] | 3969 | break; |
| 3970 | } |
| 3971 | |
dan | fad9f9a | 2014-04-01 18:41:51 +0000 | [diff] [blame] | 3972 | /* Opcode: SorterOpen P1 P2 P3 P4 * |
dan | 5134d13 | 2011-09-02 10:31:11 +0000 | [diff] [blame] | 3973 | ** |
| 3974 | ** This opcode works like OP_OpenEphemeral except that it opens |
| 3975 | ** a transient index that is specifically designed to sort large |
| 3976 | ** tables using an external merge-sort algorithm. |
dan | fad9f9a | 2014-04-01 18:41:51 +0000 | [diff] [blame] | 3977 | ** |
| 3978 | ** If argument P3 is non-zero, then it indicates that the sorter may |
| 3979 | ** assume that a stable sort considering the first P3 fields of each |
| 3980 | ** key is sufficient to produce the required results. |
dan | 5134d13 | 2011-09-02 10:31:11 +0000 | [diff] [blame] | 3981 | */ |
drh | ca892a7 | 2011-09-03 00:17:51 +0000 | [diff] [blame] | 3982 | case OP_SorterOpen: { |
dan | 5134d13 | 2011-09-02 10:31:11 +0000 | [diff] [blame] | 3983 | VdbeCursor *pCx; |
drh | 3a94987 | 2012-09-18 13:20:13 +0000 | [diff] [blame] | 3984 | |
drh | 399af1d | 2013-11-20 17:25:55 +0000 | [diff] [blame] | 3985 | assert( pOp->p1>=0 ); |
| 3986 | assert( pOp->p2>=0 ); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 3987 | pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, CURTYPE_SORTER); |
dan | 5134d13 | 2011-09-02 10:31:11 +0000 | [diff] [blame] | 3988 | if( pCx==0 ) goto no_mem; |
| 3989 | pCx->pKeyInfo = pOp->p4.pKeyInfo; |
drh | 41e13e1 | 2013-11-07 14:09:39 +0000 | [diff] [blame] | 3990 | assert( pCx->pKeyInfo->db==db ); |
| 3991 | assert( pCx->pKeyInfo->enc==ENC(db) ); |
dan | fad9f9a | 2014-04-01 18:41:51 +0000 | [diff] [blame] | 3992 | rc = sqlite3VdbeSorterInit(db, pOp->p3, pCx); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 3993 | if( rc ) goto abort_due_to_error; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3994 | break; |
| 3995 | } |
| 3996 | |
dan | 78d5843 | 2014-03-25 15:04:07 +0000 | [diff] [blame] | 3997 | /* Opcode: SequenceTest P1 P2 * * * |
| 3998 | ** Synopsis: if( cursor[P1].ctr++ ) pc = P2 |
| 3999 | ** |
| 4000 | ** P1 is a sorter cursor. If the sequence counter is currently zero, jump |
| 4001 | ** to P2. Regardless of whether or not the jump is taken, increment the |
| 4002 | ** the sequence value. |
| 4003 | */ |
| 4004 | case OP_SequenceTest: { |
| 4005 | VdbeCursor *pC; |
| 4006 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 4007 | pC = p->apCsr[pOp->p1]; |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 4008 | assert( isSorter(pC) ); |
dan | 78d5843 | 2014-03-25 15:04:07 +0000 | [diff] [blame] | 4009 | if( (pC->seqCount++)==0 ){ |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 4010 | goto jump_to_p2; |
dan | 78d5843 | 2014-03-25 15:04:07 +0000 | [diff] [blame] | 4011 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4012 | break; |
| 4013 | } |
| 4014 | |
drh | 5f61229 | 2014-02-08 23:20:32 +0000 | [diff] [blame] | 4015 | /* Opcode: OpenPseudo P1 P2 P3 * * |
drh | 60830e3 | 2014-02-10 15:56:34 +0000 | [diff] [blame] | 4016 | ** Synopsis: P3 columns in r[P2] |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 4017 | ** |
| 4018 | ** 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] | 4019 | ** row of data. The content of that one row is the content of memory |
| 4020 | ** register P2. In other words, cursor P1 becomes an alias for the |
| 4021 | ** MEM_Blob content contained in register P2. |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 4022 | ** |
drh | 2d8d7ce | 2010-02-15 15:17:05 +0000 | [diff] [blame] | 4023 | ** A pseudo-table created by this opcode is used to hold a single |
drh | cdd536f | 2006-03-17 00:04:03 +0000 | [diff] [blame] | 4024 | ** row output from the sorter so that the row can be decomposed into |
drh | 3e9ca09 | 2009-09-08 01:14:48 +0000 | [diff] [blame] | 4025 | ** individual columns using the OP_Column opcode. The OP_Column opcode |
| 4026 | ** is the only cursor opcode that works with a pseudo-table. |
danielk1977 | d336e22 | 2009-02-20 10:58:41 +0000 | [diff] [blame] | 4027 | ** |
| 4028 | ** P3 is the number of fields in the records that will be stored by |
| 4029 | ** the pseudo-table. |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 4030 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4031 | case OP_OpenPseudo: { |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 4032 | VdbeCursor *pCx; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 4033 | |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 4034 | assert( pOp->p1>=0 ); |
drh | 399af1d | 2013-11-20 17:25:55 +0000 | [diff] [blame] | 4035 | assert( pOp->p3>=0 ); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 4036 | pCx = allocateCursor(p, pOp->p1, pOp->p3, -1, CURTYPE_PSEUDO); |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 4037 | if( pCx==0 ) goto no_mem; |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 4038 | pCx->nullRow = 1; |
drh | fe0cf7a | 2017-08-16 19:20:20 +0000 | [diff] [blame] | 4039 | pCx->seekResult = pOp->p2; |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 4040 | pCx->isTable = 1; |
drh | fe0cf7a | 2017-08-16 19:20:20 +0000 | [diff] [blame] | 4041 | /* Give this pseudo-cursor a fake BtCursor pointer so that pCx |
| 4042 | ** can be safely passed to sqlite3VdbeCursorMoveto(). This avoids a test |
| 4043 | ** for pCx->eCurType==CURTYPE_BTREE inside of sqlite3VdbeCursorMoveto() |
| 4044 | ** which is a performance optimization */ |
| 4045 | pCx->uc.pCursor = sqlite3BtreeFakeValidCursor(); |
drh | 5f61229 | 2014-02-08 23:20:32 +0000 | [diff] [blame] | 4046 | assert( pOp->p5==0 ); |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 4047 | break; |
| 4048 | } |
| 4049 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4050 | /* Opcode: Close P1 * * * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4051 | ** |
| 4052 | ** Close a cursor previously opened as P1. If P1 is not |
| 4053 | ** currently open, this instruction is a no-op. |
| 4054 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4055 | case OP_Close: { |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 4056 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 4057 | sqlite3VdbeFreeCursor(p, p->apCsr[pOp->p1]); |
| 4058 | p->apCsr[pOp->p1] = 0; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4059 | break; |
| 4060 | } |
| 4061 | |
drh | 97bae79 | 2015-06-05 15:59:57 +0000 | [diff] [blame] | 4062 | #ifdef SQLITE_ENABLE_COLUMN_USED_MASK |
| 4063 | /* Opcode: ColumnsUsed P1 * * P4 * |
| 4064 | ** |
| 4065 | ** This opcode (which only exists if SQLite was compiled with |
| 4066 | ** SQLITE_ENABLE_COLUMN_USED_MASK) identifies which columns of the |
| 4067 | ** table or index for cursor P1 are used. P4 is a 64-bit integer |
| 4068 | ** (P4_INT64) in which the first 63 bits are one for each of the |
| 4069 | ** first 63 columns of the table or index that are actually used |
| 4070 | ** by the cursor. The high-order bit is set if any column after |
| 4071 | ** the 64th is used. |
| 4072 | */ |
| 4073 | case OP_ColumnsUsed: { |
| 4074 | VdbeCursor *pC; |
| 4075 | pC = p->apCsr[pOp->p1]; |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 4076 | assert( pC->eCurType==CURTYPE_BTREE ); |
drh | 97bae79 | 2015-06-05 15:59:57 +0000 | [diff] [blame] | 4077 | pC->maskUsed = *(u64*)pOp->p4.pI64; |
| 4078 | break; |
| 4079 | } |
| 4080 | #endif |
| 4081 | |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 4082 | /* Opcode: SeekGE P1 P2 P3 P4 * |
drh | f63552b | 2013-10-30 00:25:03 +0000 | [diff] [blame] | 4083 | ** Synopsis: key=r[P3@P4] |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4084 | ** |
danielk1977 | b790c6c | 2008-04-18 10:25:24 +0000 | [diff] [blame] | 4085 | ** 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] | 4086 | ** use the value in register P3 as the key. If cursor P1 refers |
danielk1977 | b790c6c | 2008-04-18 10:25:24 +0000 | [diff] [blame] | 4087 | ** to an SQL index, then P3 is the first in an array of P4 registers |
| 4088 | ** that are used as an unpacked index key. |
| 4089 | ** |
| 4090 | ** Reposition cursor P1 so that it points to the smallest entry that |
| 4091 | ** is greater than or equal to the key value. If there are no records |
| 4092 | ** 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] | 4093 | ** |
drh | b1d607d | 2015-11-05 22:30:54 +0000 | [diff] [blame] | 4094 | ** If the cursor P1 was opened using the OPFLAG_SEEKEQ flag, then this |
drh | 576d0a9 | 2020-03-12 17:28:27 +0000 | [diff] [blame] | 4095 | ** opcode will either land on a record that exactly matches the key, or |
| 4096 | ** else it will cause a jump to P2. When the cursor is OPFLAG_SEEKEQ, |
| 4097 | ** this opcode must be followed by an IdxLE opcode with the same arguments. |
| 4098 | ** The IdxGT opcode will be skipped if this opcode succeeds, but the |
| 4099 | ** IdxGT opcode will be used on subsequent loop iterations. The |
| 4100 | ** OPFLAG_SEEKEQ flags is a hint to the btree layer to say that this |
| 4101 | ** is an equality search. |
drh | b1d607d | 2015-11-05 22:30:54 +0000 | [diff] [blame] | 4102 | ** |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 4103 | ** This opcode leaves the cursor configured to move in forward order, |
drh | bc5cf38 | 2014-08-06 01:08:07 +0000 | [diff] [blame] | 4104 | ** from the beginning toward the end. In other words, the cursor is |
drh | 5dad9a3 | 2014-07-25 18:37:42 +0000 | [diff] [blame] | 4105 | ** configured to use Next, not Prev. |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 4106 | ** |
drh | 935850e | 2014-05-24 17:15:15 +0000 | [diff] [blame] | 4107 | ** See also: Found, NotFound, SeekLt, SeekGt, SeekLe |
drh | 7cf6e4d | 2004-05-19 14:56:55 +0000 | [diff] [blame] | 4108 | */ |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 4109 | /* Opcode: SeekGT P1 P2 P3 P4 * |
drh | f63552b | 2013-10-30 00:25:03 +0000 | [diff] [blame] | 4110 | ** Synopsis: key=r[P3@P4] |
drh | 7cf6e4d | 2004-05-19 14:56:55 +0000 | [diff] [blame] | 4111 | ** |
danielk1977 | b790c6c | 2008-04-18 10:25:24 +0000 | [diff] [blame] | 4112 | ** 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] | 4113 | ** use the value in register P3 as a key. If cursor P1 refers |
danielk1977 | b790c6c | 2008-04-18 10:25:24 +0000 | [diff] [blame] | 4114 | ** to an SQL index, then P3 is the first in an array of P4 registers |
| 4115 | ** that are used as an unpacked index key. |
| 4116 | ** |
drh | 576d0a9 | 2020-03-12 17:28:27 +0000 | [diff] [blame] | 4117 | ** Reposition cursor P1 so that it points to the smallest entry that |
danielk1977 | b790c6c | 2008-04-18 10:25:24 +0000 | [diff] [blame] | 4118 | ** is greater than the key value. If there are no records greater than |
| 4119 | ** the key and P2 is not zero, then jump to P2. |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 4120 | ** |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 4121 | ** This opcode leaves the cursor configured to move in forward order, |
drh | 4ed2fb9 | 2014-08-14 13:06:25 +0000 | [diff] [blame] | 4122 | ** from the beginning toward the end. In other words, the cursor is |
drh | 5dad9a3 | 2014-07-25 18:37:42 +0000 | [diff] [blame] | 4123 | ** configured to use Next, not Prev. |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 4124 | ** |
drh | 935850e | 2014-05-24 17:15:15 +0000 | [diff] [blame] | 4125 | ** See also: Found, NotFound, SeekLt, SeekGe, SeekLe |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4126 | */ |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 4127 | /* Opcode: SeekLT P1 P2 P3 P4 * |
drh | f63552b | 2013-10-30 00:25:03 +0000 | [diff] [blame] | 4128 | ** Synopsis: key=r[P3@P4] |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 4129 | ** |
danielk1977 | b790c6c | 2008-04-18 10:25:24 +0000 | [diff] [blame] | 4130 | ** 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] | 4131 | ** use the value in register P3 as a key. If cursor P1 refers |
danielk1977 | b790c6c | 2008-04-18 10:25:24 +0000 | [diff] [blame] | 4132 | ** to an SQL index, then P3 is the first in an array of P4 registers |
| 4133 | ** that are used as an unpacked index key. |
| 4134 | ** |
| 4135 | ** Reposition cursor P1 so that it points to the largest entry that |
| 4136 | ** is less than the key value. If there are no records less than |
| 4137 | ** the key and P2 is not zero, then jump to P2. |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 4138 | ** |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 4139 | ** This opcode leaves the cursor configured to move in reverse order, |
| 4140 | ** from the end toward the beginning. In other words, the cursor is |
drh | 5dad9a3 | 2014-07-25 18:37:42 +0000 | [diff] [blame] | 4141 | ** configured to use Prev, not Next. |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 4142 | ** |
drh | 935850e | 2014-05-24 17:15:15 +0000 | [diff] [blame] | 4143 | ** See also: Found, NotFound, SeekGt, SeekGe, SeekLe |
drh | 7cf6e4d | 2004-05-19 14:56:55 +0000 | [diff] [blame] | 4144 | */ |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 4145 | /* Opcode: SeekLE P1 P2 P3 P4 * |
drh | f63552b | 2013-10-30 00:25:03 +0000 | [diff] [blame] | 4146 | ** Synopsis: key=r[P3@P4] |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 4147 | ** |
danielk1977 | b790c6c | 2008-04-18 10:25:24 +0000 | [diff] [blame] | 4148 | ** 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] | 4149 | ** use the value in register P3 as a key. If cursor P1 refers |
danielk1977 | b790c6c | 2008-04-18 10:25:24 +0000 | [diff] [blame] | 4150 | ** to an SQL index, then P3 is the first in an array of P4 registers |
| 4151 | ** that are used as an unpacked index key. |
danielk1977 | 751de56 | 2008-04-18 09:01:15 +0000 | [diff] [blame] | 4152 | ** |
danielk1977 | b790c6c | 2008-04-18 10:25:24 +0000 | [diff] [blame] | 4153 | ** Reposition cursor P1 so that it points to the largest entry that |
| 4154 | ** is less than or equal to the key value. If there are no records |
| 4155 | ** 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] | 4156 | ** |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 4157 | ** This opcode leaves the cursor configured to move in reverse order, |
| 4158 | ** from the end toward the beginning. In other words, the cursor is |
drh | 5dad9a3 | 2014-07-25 18:37:42 +0000 | [diff] [blame] | 4159 | ** configured to use Prev, not Next. |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 4160 | ** |
drh | b1d607d | 2015-11-05 22:30:54 +0000 | [diff] [blame] | 4161 | ** If the cursor P1 was opened using the OPFLAG_SEEKEQ flag, then this |
drh | 576d0a9 | 2020-03-12 17:28:27 +0000 | [diff] [blame] | 4162 | ** opcode will either land on a record that exactly matches the key, or |
| 4163 | ** else it will cause a jump to P2. When the cursor is OPFLAG_SEEKEQ, |
| 4164 | ** this opcode must be followed by an IdxLE opcode with the same arguments. |
drh | b1d607d | 2015-11-05 22:30:54 +0000 | [diff] [blame] | 4165 | ** The IdxGE opcode will be skipped if this opcode succeeds, but the |
drh | 576d0a9 | 2020-03-12 17:28:27 +0000 | [diff] [blame] | 4166 | ** IdxGE opcode will be used on subsequent loop iterations. The |
| 4167 | ** OPFLAG_SEEKEQ flags is a hint to the btree layer to say that this |
| 4168 | ** is an equality search. |
drh | b1d607d | 2015-11-05 22:30:54 +0000 | [diff] [blame] | 4169 | ** |
drh | 935850e | 2014-05-24 17:15:15 +0000 | [diff] [blame] | 4170 | ** See also: Found, NotFound, SeekGt, SeekGe, SeekLt |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 4171 | */ |
mistachkin | 758784d | 2018-07-25 15:12:29 +0000 | [diff] [blame] | 4172 | case OP_SeekLT: /* jump, in3, group */ |
| 4173 | case OP_SeekLE: /* jump, in3, group */ |
| 4174 | case OP_SeekGE: /* jump, in3, group */ |
| 4175 | case OP_SeekGT: { /* jump, in3, group */ |
drh | b1d607d | 2015-11-05 22:30:54 +0000 | [diff] [blame] | 4176 | int res; /* Comparison result */ |
| 4177 | int oc; /* Opcode */ |
| 4178 | VdbeCursor *pC; /* The cursor to seek */ |
| 4179 | UnpackedRecord r; /* The key to seek for */ |
| 4180 | int nField; /* Number of columns or fields in the key */ |
| 4181 | i64 iKey; /* The rowid we are to seek to */ |
drh | d6b7946 | 2015-11-07 01:19:00 +0000 | [diff] [blame] | 4182 | int eqOnly; /* Only interested in == results */ |
drh | 80ff32f | 2001-11-04 18:32:46 +0000 | [diff] [blame] | 4183 | |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 4184 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
drh | 959403f | 2008-12-12 17:56:16 +0000 | [diff] [blame] | 4185 | assert( pOp->p2!=0 ); |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 4186 | pC = p->apCsr[pOp->p1]; |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 4187 | assert( pC!=0 ); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 4188 | assert( pC->eCurType==CURTYPE_BTREE ); |
drh | 4a1d365 | 2014-02-14 15:13:36 +0000 | [diff] [blame] | 4189 | assert( OP_SeekLE == OP_SeekLT+1 ); |
| 4190 | assert( OP_SeekGE == OP_SeekLT+2 ); |
| 4191 | assert( OP_SeekGT == OP_SeekLT+3 ); |
drh | d4187c7 | 2010-08-30 22:15:45 +0000 | [diff] [blame] | 4192 | assert( pC->isOrdered ); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 4193 | assert( pC->uc.pCursor!=0 ); |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 4194 | oc = pOp->opcode; |
drh | d6b7946 | 2015-11-07 01:19:00 +0000 | [diff] [blame] | 4195 | eqOnly = 0; |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 4196 | pC->nullRow = 0; |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 4197 | #ifdef SQLITE_DEBUG |
| 4198 | pC->seekOp = pOp->opcode; |
| 4199 | #endif |
drh | e0997b3 | 2015-03-20 14:57:50 +0000 | [diff] [blame] | 4200 | |
dan | a40cb96 | 2019-05-14 20:25:22 +0000 | [diff] [blame] | 4201 | pC->deferredMoveto = 0; |
| 4202 | pC->cacheStatus = CACHE_STALE; |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 4203 | if( pC->isTable ){ |
drh | 3e36480 | 2019-08-22 00:53:16 +0000 | [diff] [blame] | 4204 | u16 flags3, newType; |
drh | 576d0a9 | 2020-03-12 17:28:27 +0000 | [diff] [blame] | 4205 | /* The OPFLAG_SEEKEQ/BTREE_SEEK_EQ flag is only set on index cursors */ |
drh | 218c66e | 2016-12-27 12:35:36 +0000 | [diff] [blame] | 4206 | assert( sqlite3BtreeCursorHasHint(pC->uc.pCursor, BTREE_SEEK_EQ)==0 |
| 4207 | || CORRUPT_DB ); |
drh | d6b7946 | 2015-11-07 01:19:00 +0000 | [diff] [blame] | 4208 | |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 4209 | /* The input value in P3 might be of any type: integer, real, string, |
| 4210 | ** 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] | 4211 | ** the seek, so convert it. */ |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 4212 | pIn3 = &aMem[pOp->p3]; |
drh | 3e36480 | 2019-08-22 00:53:16 +0000 | [diff] [blame] | 4213 | flags3 = pIn3->flags; |
| 4214 | if( (flags3 & (MEM_Int|MEM_Real|MEM_IntReal|MEM_Str))==MEM_Str ){ |
drh | bd9507c | 2014-08-23 17:21:37 +0000 | [diff] [blame] | 4215 | applyNumericAffinity(pIn3, 0); |
| 4216 | } |
drh | 3e36480 | 2019-08-22 00:53:16 +0000 | [diff] [blame] | 4217 | iKey = sqlite3VdbeIntValue(pIn3); /* Get the integer key value */ |
| 4218 | newType = pIn3->flags; /* Record the type after applying numeric affinity */ |
| 4219 | pIn3->flags = flags3; /* But convert the type back to its original */ |
drh | 959403f | 2008-12-12 17:56:16 +0000 | [diff] [blame] | 4220 | |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 4221 | /* If the P3 value could not be converted into an integer without |
| 4222 | ** loss of information, then special processing is required... */ |
drh | 3e36480 | 2019-08-22 00:53:16 +0000 | [diff] [blame] | 4223 | if( (newType & (MEM_Int|MEM_IntReal))==0 ){ |
| 4224 | if( (newType & MEM_Real)==0 ){ |
| 4225 | if( (newType & MEM_Null) || oc>=OP_SeekGE ){ |
drh | 8616cff | 2019-07-13 16:15:23 +0000 | [diff] [blame] | 4226 | VdbeBranchTaken(1,2); |
| 4227 | goto jump_to_p2; |
dan | 9edd8c1 | 2019-05-08 11:42:49 +0000 | [diff] [blame] | 4228 | }else{ |
dan | 873b019 | 2019-05-09 11:19:27 +0000 | [diff] [blame] | 4229 | rc = sqlite3BtreeLast(pC->uc.pCursor, &res); |
| 4230 | if( rc!=SQLITE_OK ) goto abort_due_to_error; |
dan | 9edd8c1 | 2019-05-08 11:42:49 +0000 | [diff] [blame] | 4231 | goto seek_not_found; |
| 4232 | } |
| 4233 | }else |
drh | 959403f | 2008-12-12 17:56:16 +0000 | [diff] [blame] | 4234 | |
dan | aa1776f | 2013-11-26 18:22:59 +0000 | [diff] [blame] | 4235 | /* If the approximation iKey is larger than the actual real search |
| 4236 | ** term, substitute >= for > and < for <=. e.g. if the search term |
| 4237 | ** is 4.9 and the integer approximation 5: |
| 4238 | ** |
| 4239 | ** (x > 4.9) -> (x >= 5) |
| 4240 | ** (x <= 4.9) -> (x < 5) |
| 4241 | */ |
drh | 74eaba4 | 2014-09-18 17:52:15 +0000 | [diff] [blame] | 4242 | if( pIn3->u.r<(double)iKey ){ |
drh | 4a1d365 | 2014-02-14 15:13:36 +0000 | [diff] [blame] | 4243 | assert( OP_SeekGE==(OP_SeekGT-1) ); |
| 4244 | assert( OP_SeekLT==(OP_SeekLE-1) ); |
| 4245 | assert( (OP_SeekLE & 0x0001)==(OP_SeekGT & 0x0001) ); |
| 4246 | if( (oc & 0x0001)==(OP_SeekGT & 0x0001) ) oc--; |
dan | aa1776f | 2013-11-26 18:22:59 +0000 | [diff] [blame] | 4247 | } |
| 4248 | |
| 4249 | /* If the approximation iKey is smaller than the actual real search |
| 4250 | ** term, substitute <= for < and > for >=. */ |
drh | 74eaba4 | 2014-09-18 17:52:15 +0000 | [diff] [blame] | 4251 | else if( pIn3->u.r>(double)iKey ){ |
drh | 4a1d365 | 2014-02-14 15:13:36 +0000 | [diff] [blame] | 4252 | assert( OP_SeekLE==(OP_SeekLT+1) ); |
| 4253 | assert( OP_SeekGT==(OP_SeekGE+1) ); |
| 4254 | assert( (OP_SeekLT & 0x0001)==(OP_SeekGE & 0x0001) ); |
| 4255 | if( (oc & 0x0001)==(OP_SeekLT & 0x0001) ) oc++; |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 4256 | } |
dan | 9edd8c1 | 2019-05-08 11:42:49 +0000 | [diff] [blame] | 4257 | } |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 4258 | rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, 0, (u64)iKey, 0, &res); |
drh | b53a5a9 | 2014-10-12 22:37:22 +0000 | [diff] [blame] | 4259 | pC->movetoTarget = iKey; /* Used by OP_Delete */ |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 4260 | if( rc!=SQLITE_OK ){ |
| 4261 | goto abort_due_to_error; |
drh | 1af3fdb | 2004-07-18 21:33:01 +0000 | [diff] [blame] | 4262 | } |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 4263 | }else{ |
drh | 576d0a9 | 2020-03-12 17:28:27 +0000 | [diff] [blame] | 4264 | /* For a cursor with the OPFLAG_SEEKEQ/BTREE_SEEK_EQ hint, only the |
| 4265 | ** OP_SeekGE and OP_SeekLE opcodes are allowed, and these must be |
| 4266 | ** immediately followed by an OP_IdxGT or OP_IdxLT opcode, respectively, |
| 4267 | ** with the same key. |
drh | d6b7946 | 2015-11-07 01:19:00 +0000 | [diff] [blame] | 4268 | */ |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 4269 | if( sqlite3BtreeCursorHasHint(pC->uc.pCursor, BTREE_SEEK_EQ) ){ |
drh | d6b7946 | 2015-11-07 01:19:00 +0000 | [diff] [blame] | 4270 | eqOnly = 1; |
| 4271 | assert( pOp->opcode==OP_SeekGE || pOp->opcode==OP_SeekLE ); |
| 4272 | assert( pOp[1].opcode==OP_IdxLT || pOp[1].opcode==OP_IdxGT ); |
drh | 576d0a9 | 2020-03-12 17:28:27 +0000 | [diff] [blame] | 4273 | assert( pOp->opcode==OP_SeekGE || pOp[1].opcode==OP_IdxLT ); |
| 4274 | assert( pOp->opcode==OP_SeekLE || pOp[1].opcode==OP_IdxGT ); |
drh | d6b7946 | 2015-11-07 01:19:00 +0000 | [diff] [blame] | 4275 | assert( pOp[1].p1==pOp[0].p1 ); |
| 4276 | assert( pOp[1].p2==pOp[0].p2 ); |
| 4277 | assert( pOp[1].p3==pOp[0].p3 ); |
| 4278 | assert( pOp[1].p4.i==pOp[0].p4.i ); |
| 4279 | } |
| 4280 | |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 4281 | nField = pOp->p4.i; |
| 4282 | assert( pOp->p4type==P4_INT32 ); |
| 4283 | assert( nField>0 ); |
| 4284 | r.pKeyInfo = pC->pKeyInfo; |
| 4285 | r.nField = (u16)nField; |
| 4286 | |
| 4287 | /* The next line of code computes as follows, only faster: |
drh | 4a1d365 | 2014-02-14 15:13:36 +0000 | [diff] [blame] | 4288 | ** if( oc==OP_SeekGT || oc==OP_SeekLE ){ |
dan | 1fed5da | 2014-02-25 21:01:25 +0000 | [diff] [blame] | 4289 | ** r.default_rc = -1; |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 4290 | ** }else{ |
dan | 1fed5da | 2014-02-25 21:01:25 +0000 | [diff] [blame] | 4291 | ** r.default_rc = +1; |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 4292 | ** } |
danielk1977 | f7b9d66 | 2008-06-23 18:49:43 +0000 | [diff] [blame] | 4293 | */ |
dan | 1fed5da | 2014-02-25 21:01:25 +0000 | [diff] [blame] | 4294 | r.default_rc = ((1 & (oc - OP_SeekLT)) ? -1 : +1); |
| 4295 | assert( oc!=OP_SeekGT || r.default_rc==-1 ); |
| 4296 | assert( oc!=OP_SeekLE || r.default_rc==-1 ); |
| 4297 | assert( oc!=OP_SeekGE || r.default_rc==+1 ); |
| 4298 | assert( oc!=OP_SeekLT || r.default_rc==+1 ); |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 4299 | |
| 4300 | r.aMem = &aMem[pOp->p3]; |
| 4301 | #ifdef SQLITE_DEBUG |
| 4302 | { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); } |
| 4303 | #endif |
drh | 70528d7 | 2015-11-05 20:25:09 +0000 | [diff] [blame] | 4304 | r.eqSeen = 0; |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 4305 | rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, &r, 0, 0, &res); |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 4306 | if( rc!=SQLITE_OK ){ |
| 4307 | goto abort_due_to_error; |
| 4308 | } |
drh | b1d607d | 2015-11-05 22:30:54 +0000 | [diff] [blame] | 4309 | if( eqOnly && r.eqSeen==0 ){ |
| 4310 | assert( res!=0 ); |
| 4311 | goto seek_not_found; |
drh | 70528d7 | 2015-11-05 20:25:09 +0000 | [diff] [blame] | 4312 | } |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 4313 | } |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 4314 | #ifdef SQLITE_TEST |
| 4315 | sqlite3_search_count++; |
| 4316 | #endif |
drh | 4a1d365 | 2014-02-14 15:13:36 +0000 | [diff] [blame] | 4317 | if( oc>=OP_SeekGE ){ assert( oc==OP_SeekGE || oc==OP_SeekGT ); |
| 4318 | if( res<0 || (res==0 && oc==OP_SeekGT) ){ |
drh | e39a732 | 2014-02-03 14:04:11 +0000 | [diff] [blame] | 4319 | res = 0; |
drh | 2ab792e | 2017-05-30 18:34:07 +0000 | [diff] [blame] | 4320 | rc = sqlite3BtreeNext(pC->uc.pCursor, 0); |
| 4321 | if( rc!=SQLITE_OK ){ |
| 4322 | if( rc==SQLITE_DONE ){ |
| 4323 | rc = SQLITE_OK; |
| 4324 | res = 1; |
| 4325 | }else{ |
| 4326 | goto abort_due_to_error; |
| 4327 | } |
| 4328 | } |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 4329 | }else{ |
| 4330 | res = 0; |
| 4331 | } |
| 4332 | }else{ |
drh | 4a1d365 | 2014-02-14 15:13:36 +0000 | [diff] [blame] | 4333 | assert( oc==OP_SeekLT || oc==OP_SeekLE ); |
| 4334 | if( res>0 || (res==0 && oc==OP_SeekLT) ){ |
drh | e39a732 | 2014-02-03 14:04:11 +0000 | [diff] [blame] | 4335 | res = 0; |
drh | 2ab792e | 2017-05-30 18:34:07 +0000 | [diff] [blame] | 4336 | rc = sqlite3BtreePrevious(pC->uc.pCursor, 0); |
| 4337 | if( rc!=SQLITE_OK ){ |
| 4338 | if( rc==SQLITE_DONE ){ |
| 4339 | rc = SQLITE_OK; |
| 4340 | res = 1; |
| 4341 | }else{ |
| 4342 | goto abort_due_to_error; |
| 4343 | } |
| 4344 | } |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 4345 | }else{ |
| 4346 | /* res might be negative because the table is empty. Check to |
| 4347 | ** see if this is the case. |
| 4348 | */ |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 4349 | res = sqlite3BtreeEof(pC->uc.pCursor); |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 4350 | } |
| 4351 | } |
drh | b1d607d | 2015-11-05 22:30:54 +0000 | [diff] [blame] | 4352 | seek_not_found: |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 4353 | assert( pOp->p2>0 ); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 4354 | VdbeBranchTaken(res!=0,2); |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 4355 | if( res ){ |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 4356 | goto jump_to_p2; |
drh | b1d607d | 2015-11-05 22:30:54 +0000 | [diff] [blame] | 4357 | }else if( eqOnly ){ |
| 4358 | assert( pOp[1].opcode==OP_IdxLT || pOp[1].opcode==OP_IdxGT ); |
| 4359 | pOp++; /* Skip the OP_IdxLt or OP_IdxGT that follows */ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4360 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4361 | break; |
| 4362 | } |
dan | 71c57db | 2016-07-09 20:23:55 +0000 | [diff] [blame] | 4363 | |
drh | 8c2b6d7 | 2018-06-05 20:45:20 +0000 | [diff] [blame] | 4364 | /* Opcode: SeekHit P1 P2 * * * |
| 4365 | ** Synopsis: seekHit=P2 |
| 4366 | ** |
| 4367 | ** Set the seekHit flag on cursor P1 to the value in P2. |
dan | 74ebaad | 2020-01-04 16:55:57 +0000 | [diff] [blame] | 4368 | * The seekHit flag is used by the IfNoHope opcode. |
drh | 8c2b6d7 | 2018-06-05 20:45:20 +0000 | [diff] [blame] | 4369 | ** |
| 4370 | ** P1 must be a valid b-tree cursor. P2 must be a boolean value, |
| 4371 | ** either 0 or 1. |
| 4372 | */ |
| 4373 | case OP_SeekHit: { |
| 4374 | VdbeCursor *pC; |
| 4375 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 4376 | pC = p->apCsr[pOp->p1]; |
| 4377 | assert( pC!=0 ); |
| 4378 | assert( pOp->p2==0 || pOp->p2==1 ); |
| 4379 | pC->seekHit = pOp->p2 & 1; |
| 4380 | break; |
| 4381 | } |
| 4382 | |
dan | 74ebaad | 2020-01-04 16:55:57 +0000 | [diff] [blame] | 4383 | /* Opcode: IfNotOpen P1 P2 * * * |
| 4384 | ** Synopsis: if( !csr[P1] ) goto P2 |
| 4385 | ** |
| 4386 | ** If cursor P1 is not open, jump to instruction P2. Otherwise, fall through. |
| 4387 | */ |
| 4388 | case OP_IfNotOpen: { /* jump */ |
| 4389 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
drh | 56ea69b | 2020-01-04 18:33:20 +0000 | [diff] [blame] | 4390 | VdbeBranchTaken(p->apCsr[pOp->p1]==0, 2); |
dan | 74ebaad | 2020-01-04 16:55:57 +0000 | [diff] [blame] | 4391 | if( !p->apCsr[pOp->p1] ){ |
| 4392 | goto jump_to_p2_and_check_for_interrupt; |
| 4393 | } |
| 4394 | break; |
| 4395 | } |
| 4396 | |
drh | 8cff69d | 2009-11-12 19:59:44 +0000 | [diff] [blame] | 4397 | /* Opcode: Found P1 P2 P3 P4 * |
drh | f63552b | 2013-10-30 00:25:03 +0000 | [diff] [blame] | 4398 | ** Synopsis: key=r[P3@P4] |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4399 | ** |
drh | 8cff69d | 2009-11-12 19:59:44 +0000 | [diff] [blame] | 4400 | ** If P4==0 then register P3 holds a blob constructed by MakeRecord. If |
| 4401 | ** P4>0 then register P3 is the first of P4 registers that form an unpacked |
| 4402 | ** record. |
| 4403 | ** |
| 4404 | ** Cursor P1 is on an index btree. If the record identified by P3 and P4 |
| 4405 | ** 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] | 4406 | ** P1 is left pointing at the matching entry. |
drh | 6f225d0 | 2013-10-26 13:36:51 +0000 | [diff] [blame] | 4407 | ** |
drh | cefc87f | 2014-08-01 01:40:33 +0000 | [diff] [blame] | 4408 | ** This operation leaves the cursor in a state where it can be |
| 4409 | ** advanced in the forward direction. The Next instruction will work, |
| 4410 | ** but not the Prev instruction. |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 4411 | ** |
drh | 6f225d0 | 2013-10-26 13:36:51 +0000 | [diff] [blame] | 4412 | ** See also: NotFound, NoConflict, NotExists. SeekGe |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4413 | */ |
drh | 8cff69d | 2009-11-12 19:59:44 +0000 | [diff] [blame] | 4414 | /* Opcode: NotFound P1 P2 P3 P4 * |
drh | f63552b | 2013-10-30 00:25:03 +0000 | [diff] [blame] | 4415 | ** Synopsis: key=r[P3@P4] |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4416 | ** |
drh | 8cff69d | 2009-11-12 19:59:44 +0000 | [diff] [blame] | 4417 | ** If P4==0 then register P3 holds a blob constructed by MakeRecord. If |
| 4418 | ** P4>0 then register P3 is the first of P4 registers that form an unpacked |
| 4419 | ** record. |
| 4420 | ** |
| 4421 | ** Cursor P1 is on an index btree. If the record identified by P3 and P4 |
| 4422 | ** is not the prefix of any entry in P1 then a jump is made to P2. If P1 |
| 4423 | ** does contain an entry whose prefix matches the P3/P4 record then control |
| 4424 | ** falls through to the next instruction and P1 is left pointing at the |
| 4425 | ** matching entry. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4426 | ** |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 4427 | ** This operation leaves the cursor in a state where it cannot be |
| 4428 | ** advanced in either direction. In other words, the Next and Prev |
| 4429 | ** opcodes do not work after this operation. |
| 4430 | ** |
drh | 8c2b6d7 | 2018-06-05 20:45:20 +0000 | [diff] [blame] | 4431 | ** See also: Found, NotExists, NoConflict, IfNoHope |
| 4432 | */ |
| 4433 | /* Opcode: IfNoHope P1 P2 P3 P4 * |
| 4434 | ** Synopsis: key=r[P3@P4] |
| 4435 | ** |
| 4436 | ** Register P3 is the first of P4 registers that form an unpacked |
| 4437 | ** record. |
| 4438 | ** |
| 4439 | ** Cursor P1 is on an index btree. If the seekHit flag is set on P1, then |
| 4440 | ** this opcode is a no-op. But if the seekHit flag of P1 is clear, then |
| 4441 | ** check to see if there is any entry in P1 that matches the |
| 4442 | ** prefix identified by P3 and P4. If no entry matches the prefix, |
| 4443 | ** jump to P2. Otherwise fall through. |
| 4444 | ** |
| 4445 | ** This opcode behaves like OP_NotFound if the seekHit |
| 4446 | ** flag is clear and it behaves like OP_Noop if the seekHit flag is set. |
| 4447 | ** |
| 4448 | ** This opcode is used in IN clause processing for a multi-column key. |
| 4449 | ** If an IN clause is attached to an element of the key other than the |
| 4450 | ** left-most element, and if there are no matches on the most recent |
| 4451 | ** seek over the whole key, then it might be that one of the key element |
| 4452 | ** to the left is prohibiting a match, and hence there is "no hope" of |
| 4453 | ** any match regardless of how many IN clause elements are checked. |
| 4454 | ** In such a case, we abandon the IN clause search early, using this |
| 4455 | ** opcode. The opcode name comes from the fact that the |
| 4456 | ** jump is taken if there is "no hope" of achieving a match. |
| 4457 | ** |
| 4458 | ** See also: NotFound, SeekHit |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4459 | */ |
drh | 6f225d0 | 2013-10-26 13:36:51 +0000 | [diff] [blame] | 4460 | /* Opcode: NoConflict P1 P2 P3 P4 * |
drh | 4af5bee | 2013-10-30 02:37:50 +0000 | [diff] [blame] | 4461 | ** Synopsis: key=r[P3@P4] |
drh | 6f225d0 | 2013-10-26 13:36:51 +0000 | [diff] [blame] | 4462 | ** |
| 4463 | ** If P4==0 then register P3 holds a blob constructed by MakeRecord. If |
| 4464 | ** P4>0 then register P3 is the first of P4 registers that form an unpacked |
| 4465 | ** record. |
| 4466 | ** |
| 4467 | ** Cursor P1 is on an index btree. If the record identified by P3 and P4 |
| 4468 | ** contains any NULL value, jump immediately to P2. If all terms of the |
| 4469 | ** record are not-NULL then a check is done to determine if any row in the |
| 4470 | ** P1 index btree has a matching key prefix. If there are no matches, jump |
| 4471 | ** immediately to P2. If there is a match, fall through and leave the P1 |
| 4472 | ** cursor pointing to the matching row. |
| 4473 | ** |
| 4474 | ** This opcode is similar to OP_NotFound with the exceptions that the |
| 4475 | ** branch is always taken if any part of the search key input is NULL. |
| 4476 | ** |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 4477 | ** This operation leaves the cursor in a state where it cannot be |
| 4478 | ** advanced in either direction. In other words, the Next and Prev |
| 4479 | ** opcodes do not work after this operation. |
| 4480 | ** |
drh | 6f225d0 | 2013-10-26 13:36:51 +0000 | [diff] [blame] | 4481 | ** See also: NotFound, Found, NotExists |
| 4482 | */ |
drh | 8c2b6d7 | 2018-06-05 20:45:20 +0000 | [diff] [blame] | 4483 | case OP_IfNoHope: { /* jump, in3 */ |
| 4484 | VdbeCursor *pC; |
| 4485 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 4486 | pC = p->apCsr[pOp->p1]; |
| 4487 | assert( pC!=0 ); |
| 4488 | if( pC->seekHit ) break; |
| 4489 | /* Fall through into OP_NotFound */ |
| 4490 | } |
drh | 6f225d0 | 2013-10-26 13:36:51 +0000 | [diff] [blame] | 4491 | case OP_NoConflict: /* jump, in3 */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4492 | case OP_NotFound: /* jump, in3 */ |
| 4493 | case OP_Found: { /* jump, in3 */ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 4494 | int alreadyExists; |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 4495 | int takeJump; |
drh | 6f225d0 | 2013-10-26 13:36:51 +0000 | [diff] [blame] | 4496 | int ii; |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 4497 | VdbeCursor *pC; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 4498 | int res; |
drh | a582b01 | 2016-12-21 19:45:54 +0000 | [diff] [blame] | 4499 | UnpackedRecord *pFree; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 4500 | UnpackedRecord *pIdxKey; |
drh | 8cff69d | 2009-11-12 19:59:44 +0000 | [diff] [blame] | 4501 | UnpackedRecord r; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 4502 | |
dan | 0ff297e | 2009-09-25 17:03:14 +0000 | [diff] [blame] | 4503 | #ifdef SQLITE_TEST |
drh | 6f225d0 | 2013-10-26 13:36:51 +0000 | [diff] [blame] | 4504 | if( pOp->opcode!=OP_NoConflict ) sqlite3_found_count++; |
dan | 0ff297e | 2009-09-25 17:03:14 +0000 | [diff] [blame] | 4505 | #endif |
| 4506 | |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 4507 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
drh | 8cff69d | 2009-11-12 19:59:44 +0000 | [diff] [blame] | 4508 | assert( pOp->p4type==P4_INT32 ); |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 4509 | pC = p->apCsr[pOp->p1]; |
| 4510 | assert( pC!=0 ); |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 4511 | #ifdef SQLITE_DEBUG |
drh | cefc87f | 2014-08-01 01:40:33 +0000 | [diff] [blame] | 4512 | pC->seekOp = pOp->opcode; |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 4513 | #endif |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 4514 | pIn3 = &aMem[pOp->p3]; |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 4515 | assert( pC->eCurType==CURTYPE_BTREE ); |
| 4516 | assert( pC->uc.pCursor!=0 ); |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 4517 | assert( pC->isTable==0 ); |
| 4518 | if( pOp->p4.i>0 ){ |
| 4519 | r.pKeyInfo = pC->pKeyInfo; |
| 4520 | r.nField = (u16)pOp->p4.i; |
| 4521 | r.aMem = pIn3; |
drh | 8aaf7bc | 2016-09-20 01:19:18 +0000 | [diff] [blame] | 4522 | #ifdef SQLITE_DEBUG |
drh | 826af37 | 2014-02-08 19:12:21 +0000 | [diff] [blame] | 4523 | for(ii=0; ii<r.nField; ii++){ |
| 4524 | assert( memIsValid(&r.aMem[ii]) ); |
drh | 8aaf7bc | 2016-09-20 01:19:18 +0000 | [diff] [blame] | 4525 | assert( (r.aMem[ii].flags & MEM_Zero)==0 || r.aMem[ii].n==0 ); |
drh | 826af37 | 2014-02-08 19:12:21 +0000 | [diff] [blame] | 4526 | if( ii ) REGISTER_TRACE(pOp->p3+ii, &r.aMem[ii]); |
drh | 826af37 | 2014-02-08 19:12:21 +0000 | [diff] [blame] | 4527 | } |
drh | 8aaf7bc | 2016-09-20 01:19:18 +0000 | [diff] [blame] | 4528 | #endif |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 4529 | pIdxKey = &r; |
drh | a582b01 | 2016-12-21 19:45:54 +0000 | [diff] [blame] | 4530 | pFree = 0; |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 4531 | }else{ |
drh | e46515b | 2017-05-19 22:51:00 +0000 | [diff] [blame] | 4532 | assert( pIn3->flags & MEM_Blob ); |
| 4533 | rc = ExpandBlob(pIn3); |
| 4534 | assert( rc==SQLITE_OK || rc==SQLITE_NOMEM ); |
| 4535 | if( rc ) goto no_mem; |
drh | a582b01 | 2016-12-21 19:45:54 +0000 | [diff] [blame] | 4536 | pFree = pIdxKey = sqlite3VdbeAllocUnpackedRecord(pC->pKeyInfo); |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 4537 | if( pIdxKey==0 ) goto no_mem; |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 4538 | sqlite3VdbeRecordUnpack(pC->pKeyInfo, pIn3->n, pIn3->z, pIdxKey); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4539 | } |
dan | 1fed5da | 2014-02-25 21:01:25 +0000 | [diff] [blame] | 4540 | pIdxKey->default_rc = 0; |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 4541 | takeJump = 0; |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 4542 | if( pOp->opcode==OP_NoConflict ){ |
| 4543 | /* For the OP_NoConflict opcode, take the jump if any of the |
| 4544 | ** input fields are NULL, since any key with a NULL will not |
| 4545 | ** conflict */ |
mistachkin | 7bb6e8e | 2015-01-12 18:52:41 +0000 | [diff] [blame] | 4546 | for(ii=0; ii<pIdxKey->nField; ii++){ |
| 4547 | if( pIdxKey->aMem[ii].flags & MEM_Null ){ |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 4548 | takeJump = 1; |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 4549 | break; |
drh | 6f225d0 | 2013-10-26 13:36:51 +0000 | [diff] [blame] | 4550 | } |
| 4551 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4552 | } |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 4553 | rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, pIdxKey, 0, 0, &res); |
drh | dbd6a7d | 2017-04-05 12:39:49 +0000 | [diff] [blame] | 4554 | if( pFree ) sqlite3DbFreeNN(db, pFree); |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 4555 | if( rc!=SQLITE_OK ){ |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 4556 | goto abort_due_to_error; |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 4557 | } |
| 4558 | pC->seekResult = res; |
| 4559 | alreadyExists = (res==0); |
| 4560 | pC->nullRow = 1-alreadyExists; |
| 4561 | pC->deferredMoveto = 0; |
| 4562 | pC->cacheStatus = CACHE_STALE; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4563 | if( pOp->opcode==OP_Found ){ |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 4564 | VdbeBranchTaken(alreadyExists!=0,2); |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 4565 | if( alreadyExists ) goto jump_to_p2; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4566 | }else{ |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 4567 | VdbeBranchTaken(takeJump||alreadyExists==0,2); |
| 4568 | if( takeJump || !alreadyExists ) goto jump_to_p2; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4569 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4570 | break; |
| 4571 | } |
| 4572 | |
drh | eeb9565 | 2016-05-26 20:56:38 +0000 | [diff] [blame] | 4573 | /* Opcode: SeekRowid P1 P2 P3 * * |
| 4574 | ** Synopsis: intkey=r[P3] |
| 4575 | ** |
| 4576 | ** P1 is the index of a cursor open on an SQL table btree (with integer |
| 4577 | ** keys). If register P3 does not contain an integer or if P1 does not |
| 4578 | ** contain a record with rowid P3 then jump immediately to P2. |
| 4579 | ** Or, if P2 is 0, raise an SQLITE_CORRUPT error. If P1 does contain |
| 4580 | ** a record with rowid P3 then |
| 4581 | ** leave the cursor pointing at that record and fall through to the next |
| 4582 | ** instruction. |
| 4583 | ** |
| 4584 | ** The OP_NotExists opcode performs the same operation, but with OP_NotExists |
| 4585 | ** the P3 register must be guaranteed to contain an integer value. With this |
| 4586 | ** opcode, register P3 might not contain an integer. |
| 4587 | ** |
| 4588 | ** The OP_NotFound opcode performs the same operation on index btrees |
| 4589 | ** (with arbitrary multi-value keys). |
| 4590 | ** |
| 4591 | ** This opcode leaves the cursor in a state where it cannot be advanced |
| 4592 | ** in either direction. In other words, the Next and Prev opcodes will |
| 4593 | ** not work following this opcode. |
| 4594 | ** |
| 4595 | ** See also: Found, NotFound, NoConflict, SeekRowid |
| 4596 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4597 | /* Opcode: NotExists P1 P2 P3 * * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 4598 | ** Synopsis: intkey=r[P3] |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 4599 | ** |
drh | 261c02d | 2013-10-25 14:46:15 +0000 | [diff] [blame] | 4600 | ** P1 is the index of a cursor open on an SQL table btree (with integer |
| 4601 | ** 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] | 4602 | ** rowid P3 then jump immediately to P2. Or, if P2 is 0, raise an |
| 4603 | ** SQLITE_CORRUPT error. If P1 does contain a record with rowid P3 then |
| 4604 | ** leave the cursor pointing at that record and fall through to the next |
| 4605 | ** instruction. |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 4606 | ** |
drh | eeb9565 | 2016-05-26 20:56:38 +0000 | [diff] [blame] | 4607 | ** The OP_SeekRowid opcode performs the same operation but also allows the |
| 4608 | ** P3 register to contain a non-integer value, in which case the jump is |
| 4609 | ** always taken. This opcode requires that P3 always contain an integer. |
| 4610 | ** |
drh | 261c02d | 2013-10-25 14:46:15 +0000 | [diff] [blame] | 4611 | ** The OP_NotFound opcode performs the same operation on index btrees |
| 4612 | ** (with arbitrary multi-value keys). |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 4613 | ** |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 4614 | ** This opcode leaves the cursor in a state where it cannot be advanced |
| 4615 | ** in either direction. In other words, the Next and Prev opcodes will |
| 4616 | ** not work following this opcode. |
| 4617 | ** |
drh | eeb9565 | 2016-05-26 20:56:38 +0000 | [diff] [blame] | 4618 | ** See also: Found, NotFound, NoConflict, SeekRowid |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 4619 | */ |
drh | eeb9565 | 2016-05-26 20:56:38 +0000 | [diff] [blame] | 4620 | case OP_SeekRowid: { /* jump, in3 */ |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 4621 | VdbeCursor *pC; |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 4622 | BtCursor *pCrsr; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 4623 | int res; |
| 4624 | u64 iKey; |
| 4625 | |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 4626 | pIn3 = &aMem[pOp->p3]; |
drh | 3242c69 | 2019-05-04 01:29:13 +0000 | [diff] [blame] | 4627 | testcase( pIn3->flags & MEM_Int ); |
| 4628 | testcase( pIn3->flags & MEM_IntReal ); |
drh | b29ef5e | 2019-10-07 01:05:57 +0000 | [diff] [blame] | 4629 | testcase( pIn3->flags & MEM_Real ); |
| 4630 | testcase( (pIn3->flags & (MEM_Str|MEM_Int))==MEM_Str ); |
drh | 169f077 | 2019-05-02 21:36:26 +0000 | [diff] [blame] | 4631 | if( (pIn3->flags & (MEM_Int|MEM_IntReal))==0 ){ |
drh | b29ef5e | 2019-10-07 01:05:57 +0000 | [diff] [blame] | 4632 | /* If pIn3->u.i does not contain an integer, compute iKey as the |
| 4633 | ** integer value of pIn3. Jump to P2 if pIn3 cannot be converted |
| 4634 | ** into an integer without loss of information. Take care to avoid |
| 4635 | ** changing the datatype of pIn3, however, as it is used by other |
| 4636 | ** parts of the prepared statement. */ |
| 4637 | Mem x = pIn3[0]; |
| 4638 | applyAffinity(&x, SQLITE_AFF_NUMERIC, encoding); |
| 4639 | if( (x.flags & MEM_Int)==0 ) goto jump_to_p2; |
| 4640 | iKey = x.u.i; |
| 4641 | goto notExistsWithKey; |
drh | eeb9565 | 2016-05-26 20:56:38 +0000 | [diff] [blame] | 4642 | } |
| 4643 | /* Fall through into OP_NotExists */ |
| 4644 | case OP_NotExists: /* jump, in3 */ |
| 4645 | pIn3 = &aMem[pOp->p3]; |
drh | e4fe6d4 | 2018-08-03 15:58:07 +0000 | [diff] [blame] | 4646 | assert( (pIn3->flags & MEM_Int)!=0 || pOp->opcode==OP_SeekRowid ); |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 4647 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
drh | b29ef5e | 2019-10-07 01:05:57 +0000 | [diff] [blame] | 4648 | iKey = pIn3->u.i; |
| 4649 | notExistsWithKey: |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 4650 | pC = p->apCsr[pOp->p1]; |
| 4651 | assert( pC!=0 ); |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 4652 | #ifdef SQLITE_DEBUG |
drh | 94f4f87 | 2018-12-20 22:08:32 +0000 | [diff] [blame] | 4653 | if( pOp->opcode==OP_SeekRowid ) pC->seekOp = OP_SeekRowid; |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 4654 | #endif |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 4655 | assert( pC->isTable ); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 4656 | assert( pC->eCurType==CURTYPE_BTREE ); |
| 4657 | pCrsr = pC->uc.pCursor; |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 4658 | assert( pCrsr!=0 ); |
| 4659 | res = 0; |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 4660 | rc = sqlite3BtreeMovetoUnpacked(pCrsr, 0, iKey, 0, &res); |
drh | b79d552 | 2015-09-14 19:26:37 +0000 | [diff] [blame] | 4661 | assert( rc==SQLITE_OK || res==0 ); |
drh | b53a5a9 | 2014-10-12 22:37:22 +0000 | [diff] [blame] | 4662 | pC->movetoTarget = iKey; /* Used by OP_Delete */ |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 4663 | pC->nullRow = 0; |
| 4664 | pC->cacheStatus = CACHE_STALE; |
| 4665 | pC->deferredMoveto = 0; |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 4666 | VdbeBranchTaken(res!=0,2); |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 4667 | pC->seekResult = res; |
dan | c6157e1 | 2015-09-14 09:23:47 +0000 | [diff] [blame] | 4668 | if( res!=0 ){ |
drh | b79d552 | 2015-09-14 19:26:37 +0000 | [diff] [blame] | 4669 | assert( rc==SQLITE_OK ); |
| 4670 | if( pOp->p2==0 ){ |
| 4671 | rc = SQLITE_CORRUPT_BKPT; |
| 4672 | }else{ |
| 4673 | goto jump_to_p2; |
| 4674 | } |
dan | c6157e1 | 2015-09-14 09:23:47 +0000 | [diff] [blame] | 4675 | } |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 4676 | if( rc ) goto abort_due_to_error; |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 4677 | break; |
| 4678 | } |
| 4679 | |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 4680 | /* Opcode: Sequence P1 P2 * * * |
drh | 079a307 | 2014-03-19 14:10:55 +0000 | [diff] [blame] | 4681 | ** Synopsis: r[P2]=cursor[P1].ctr++ |
drh | 4db38a7 | 2005-09-01 12:16:28 +0000 | [diff] [blame] | 4682 | ** |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 4683 | ** Find the next available sequence number for cursor P1. |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4684 | ** Write the sequence number into register P2. |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 4685 | ** The sequence number on the cursor is incremented after this |
| 4686 | ** instruction. |
drh | 4db38a7 | 2005-09-01 12:16:28 +0000 | [diff] [blame] | 4687 | */ |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 4688 | case OP_Sequence: { /* out2 */ |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 4689 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 4690 | assert( p->apCsr[pOp->p1]!=0 ); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 4691 | assert( p->apCsr[pOp->p1]->eCurType!=CURTYPE_VTAB ); |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 4692 | pOut = out2Prerelease(p, pOp); |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 4693 | pOut->u.i = p->apCsr[pOp->p1]->seqCount++; |
drh | 4db38a7 | 2005-09-01 12:16:28 +0000 | [diff] [blame] | 4694 | break; |
| 4695 | } |
| 4696 | |
| 4697 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4698 | /* Opcode: NewRowid P1 P2 P3 * * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 4699 | ** Synopsis: r[P2]=rowid |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4700 | ** |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 4701 | ** 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] | 4702 | ** The record number is not previously used as a key in the database |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4703 | ** table that cursor P1 points to. The new record number is written |
| 4704 | ** written to register P2. |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 4705 | ** |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 4706 | ** If P3>0 then P3 is a register in the root frame of this VDBE that holds |
| 4707 | ** the largest previously generated record number. No new record numbers are |
| 4708 | ** allowed to be less than this value. When this value reaches its maximum, |
drh | ef8662b | 2011-06-20 21:47:58 +0000 | [diff] [blame] | 4709 | ** an SQLITE_FULL error is generated. The P3 register is updated with the ' |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 4710 | ** generated record number. This P3 mechanism is used to help implement the |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 4711 | ** AUTOINCREMENT feature. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4712 | */ |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 4713 | case OP_NewRowid: { /* out2 */ |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 4714 | i64 v; /* The new rowid */ |
| 4715 | VdbeCursor *pC; /* Cursor of table to get the new rowid */ |
| 4716 | int res; /* Result of an sqlite3BtreeLast() */ |
| 4717 | int cnt; /* Counter to limit the number of searches */ |
| 4718 | Mem *pMem; /* Register holding largest rowid for AUTOINCREMENT */ |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 4719 | VdbeFrame *pFrame; /* Root frame of VDBE */ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 4720 | |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 4721 | v = 0; |
| 4722 | res = 0; |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 4723 | pOut = out2Prerelease(p, pOp); |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 4724 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 4725 | pC = p->apCsr[pOp->p1]; |
| 4726 | assert( pC!=0 ); |
drh | 4c57e32 | 2018-05-23 17:53:07 +0000 | [diff] [blame] | 4727 | assert( pC->isTable ); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 4728 | assert( pC->eCurType==CURTYPE_BTREE ); |
| 4729 | assert( pC->uc.pCursor!=0 ); |
drh | 98ef0f6 | 2015-06-30 01:25:52 +0000 | [diff] [blame] | 4730 | { |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 4731 | /* The next rowid or record number (different terms for the same |
| 4732 | ** thing) is obtained in a two-step algorithm. |
| 4733 | ** |
| 4734 | ** First we attempt to find the largest existing rowid and add one |
| 4735 | ** to that. But if the largest existing rowid is already the maximum |
| 4736 | ** positive integer, we have to fall through to the second |
| 4737 | ** probabilistic algorithm |
| 4738 | ** |
| 4739 | ** The second algorithm is to select a rowid at random and see if |
| 4740 | ** it already exists in the table. If it does not exist, we have |
| 4741 | ** succeeded. If the random rowid does exist, we select a new one |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 4742 | ** and try again, up to 100 times. |
drh | db5ed6d | 2001-09-18 22:17:44 +0000 | [diff] [blame] | 4743 | */ |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 4744 | assert( pC->isTable ); |
drh | fe2093d | 2005-01-20 22:48:47 +0000 | [diff] [blame] | 4745 | |
drh | 75f86a4 | 2005-02-17 00:03:06 +0000 | [diff] [blame] | 4746 | #ifdef SQLITE_32BIT_ROWID |
| 4747 | # define MAX_ROWID 0x7fffffff |
| 4748 | #else |
drh | fe2093d | 2005-01-20 22:48:47 +0000 | [diff] [blame] | 4749 | /* Some compilers complain about constants of the form 0x7fffffffffffffff. |
| 4750 | ** Others complain about 0x7ffffffffffffffffLL. The following macro seems |
| 4751 | ** to provide the constant while making all compilers happy. |
| 4752 | */ |
danielk1977 | 64202cf | 2008-11-17 15:31:47 +0000 | [diff] [blame] | 4753 | # define MAX_ROWID (i64)( (((u64)0x7fffffff)<<32) | (u64)0xffffffff ) |
drh | 75f86a4 | 2005-02-17 00:03:06 +0000 | [diff] [blame] | 4754 | #endif |
drh | fe2093d | 2005-01-20 22:48:47 +0000 | [diff] [blame] | 4755 | |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 4756 | if( !pC->useRandomRowid ){ |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 4757 | rc = sqlite3BtreeLast(pC->uc.pCursor, &res); |
drh | e0670b6 | 2014-02-12 21:31:12 +0000 | [diff] [blame] | 4758 | if( rc!=SQLITE_OK ){ |
| 4759 | goto abort_due_to_error; |
| 4760 | } |
| 4761 | if( res ){ |
| 4762 | v = 1; /* IMP: R-61914-48074 */ |
| 4763 | }else{ |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 4764 | assert( sqlite3BtreeCursorIsValid(pC->uc.pCursor) ); |
drh | a7c90c4 | 2016-06-04 20:37:10 +0000 | [diff] [blame] | 4765 | v = sqlite3BtreeIntegerKey(pC->uc.pCursor); |
drh | e0670b6 | 2014-02-12 21:31:12 +0000 | [diff] [blame] | 4766 | if( v>=MAX_ROWID ){ |
| 4767 | pC->useRandomRowid = 1; |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 4768 | }else{ |
drh | e0670b6 | 2014-02-12 21:31:12 +0000 | [diff] [blame] | 4769 | v++; /* IMP: R-29538-34987 */ |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 4770 | } |
drh | 3fc190c | 2001-09-14 03:24:23 +0000 | [diff] [blame] | 4771 | } |
drh | e0670b6 | 2014-02-12 21:31:12 +0000 | [diff] [blame] | 4772 | } |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 4773 | |
| 4774 | #ifndef SQLITE_OMIT_AUTOINCREMENT |
drh | e0670b6 | 2014-02-12 21:31:12 +0000 | [diff] [blame] | 4775 | if( pOp->p3 ){ |
| 4776 | /* Assert that P3 is a valid memory cell. */ |
| 4777 | assert( pOp->p3>0 ); |
| 4778 | if( p->pFrame ){ |
| 4779 | for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent); |
shane | abc6b89 | 2009-09-10 19:09:03 +0000 | [diff] [blame] | 4780 | /* Assert that P3 is a valid memory cell. */ |
drh | e0670b6 | 2014-02-12 21:31:12 +0000 | [diff] [blame] | 4781 | assert( pOp->p3<=pFrame->nMem ); |
| 4782 | pMem = &pFrame->aMem[pOp->p3]; |
| 4783 | }else{ |
| 4784 | /* Assert that P3 is a valid memory cell. */ |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 4785 | assert( pOp->p3<=(p->nMem+1 - p->nCursor) ); |
drh | e0670b6 | 2014-02-12 21:31:12 +0000 | [diff] [blame] | 4786 | pMem = &aMem[pOp->p3]; |
| 4787 | memAboutToChange(p, pMem); |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 4788 | } |
drh | e0670b6 | 2014-02-12 21:31:12 +0000 | [diff] [blame] | 4789 | assert( memIsValid(pMem) ); |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 4790 | |
drh | e0670b6 | 2014-02-12 21:31:12 +0000 | [diff] [blame] | 4791 | REGISTER_TRACE(pOp->p3, pMem); |
| 4792 | sqlite3VdbeMemIntegerify(pMem); |
| 4793 | assert( (pMem->flags & MEM_Int)!=0 ); /* mem(P3) holds an integer */ |
| 4794 | if( pMem->u.i==MAX_ROWID || pC->useRandomRowid ){ |
drh | e77caa1 | 2016-11-02 13:18:46 +0000 | [diff] [blame] | 4795 | rc = SQLITE_FULL; /* IMP: R-17817-00630 */ |
drh | e0670b6 | 2014-02-12 21:31:12 +0000 | [diff] [blame] | 4796 | goto abort_due_to_error; |
| 4797 | } |
| 4798 | if( v<pMem->u.i+1 ){ |
| 4799 | v = pMem->u.i + 1; |
| 4800 | } |
| 4801 | pMem->u.i = v; |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 4802 | } |
drh | e0670b6 | 2014-02-12 21:31:12 +0000 | [diff] [blame] | 4803 | #endif |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 4804 | if( pC->useRandomRowid ){ |
drh | 748a52c | 2010-09-01 11:50:08 +0000 | [diff] [blame] | 4805 | /* IMPLEMENTATION-OF: R-07677-41881 If the largest ROWID is equal to the |
drh | c79c761 | 2010-01-01 18:57:48 +0000 | [diff] [blame] | 4806 | ** largest possible integer (9223372036854775807) then the database |
drh | 748a52c | 2010-09-01 11:50:08 +0000 | [diff] [blame] | 4807 | ** engine starts picking positive candidate ROWIDs at random until |
| 4808 | ** it finds one that is not previously used. */ |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 4809 | assert( pOp->p3==0 ); /* We cannot be in random rowid mode if this is |
| 4810 | ** an AUTOINCREMENT table. */ |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 4811 | cnt = 0; |
drh | 2c4dc63 | 2014-09-25 12:31:28 +0000 | [diff] [blame] | 4812 | do{ |
| 4813 | sqlite3_randomness(sizeof(v), &v); |
drh | d863346 | 2014-09-25 17:42:41 +0000 | [diff] [blame] | 4814 | v &= (MAX_ROWID>>1); v++; /* Ensure that v is greater than zero */ |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 4815 | }while( ((rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, 0, (u64)v, |
drh | 748a52c | 2010-09-01 11:50:08 +0000 | [diff] [blame] | 4816 | 0, &res))==SQLITE_OK) |
shaneh | c4d340a | 2010-09-01 02:37:56 +0000 | [diff] [blame] | 4817 | && (res==0) |
drh | 2c4dc63 | 2014-09-25 12:31:28 +0000 | [diff] [blame] | 4818 | && (++cnt<100)); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 4819 | if( rc ) goto abort_due_to_error; |
| 4820 | if( res==0 ){ |
drh | c79c761 | 2010-01-01 18:57:48 +0000 | [diff] [blame] | 4821 | rc = SQLITE_FULL; /* IMP: R-38219-53002 */ |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 4822 | goto abort_due_to_error; |
| 4823 | } |
drh | 748a52c | 2010-09-01 11:50:08 +0000 | [diff] [blame] | 4824 | assert( v>0 ); /* EV: R-40812-03570 */ |
drh | 1eaa269 | 2001-09-18 02:02:23 +0000 | [diff] [blame] | 4825 | } |
drh | a11846b | 2004-01-07 18:52:56 +0000 | [diff] [blame] | 4826 | pC->deferredMoveto = 0; |
drh | 76873ab | 2006-01-07 18:48:26 +0000 | [diff] [blame] | 4827 | pC->cacheStatus = CACHE_STALE; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4828 | } |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 4829 | pOut->u.i = v; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4830 | break; |
| 4831 | } |
| 4832 | |
danielk1977 | 1f4aa33 | 2008-01-03 09:51:55 +0000 | [diff] [blame] | 4833 | /* Opcode: Insert P1 P2 P3 P4 P5 |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 4834 | ** Synopsis: intkey=r[P3] data=r[P2] |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4835 | ** |
jplyon | 5a56422 | 2003-06-02 06:15:58 +0000 | [diff] [blame] | 4836 | ** Write an entry into the table of cursor P1. A new entry is |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 4837 | ** created if it doesn't already exist or the data for an existing |
drh | 3e9ca09 | 2009-09-08 01:14:48 +0000 | [diff] [blame] | 4838 | ** entry is overwritten. The data is the value MEM_Blob stored in register |
danielk1977 | 1f4aa33 | 2008-01-03 09:51:55 +0000 | [diff] [blame] | 4839 | ** number P2. The key is stored in register P3. The key must |
drh | 3e9ca09 | 2009-09-08 01:14:48 +0000 | [diff] [blame] | 4840 | ** be a MEM_Int. |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 4841 | ** |
danielk1977 | 1f4aa33 | 2008-01-03 09:51:55 +0000 | [diff] [blame] | 4842 | ** If the OPFLAG_NCHANGE flag of P5 is set, then the row change count is |
| 4843 | ** incremented (otherwise not). If the OPFLAG_LASTROWID flag of P5 is set, |
danielk1977 | b28af71 | 2004-06-21 06:50:26 +0000 | [diff] [blame] | 4844 | ** then rowid is stored for subsequent return by the |
drh | 85b623f | 2007-12-13 21:54:09 +0000 | [diff] [blame] | 4845 | ** sqlite3_last_insert_rowid() function (otherwise it is unmodified). |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 4846 | ** |
drh | eaf6ae2 | 2016-11-09 20:14:34 +0000 | [diff] [blame] | 4847 | ** If the OPFLAG_USESEEKRESULT flag of P5 is set, the implementation might |
| 4848 | ** run faster by avoiding an unnecessary seek on cursor P1. However, |
| 4849 | ** the OPFLAG_USESEEKRESULT flag must only be set if there have been no prior |
| 4850 | ** 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] | 4851 | ** |
| 4852 | ** If the OPFLAG_ISUPDATE flag is set, then this opcode is part of an |
| 4853 | ** UPDATE operation. Otherwise (if the flag is clear) then this opcode |
| 4854 | ** is part of an INSERT operation. The difference is only important to |
| 4855 | ** the update hook. |
| 4856 | ** |
dan | 319eeb7 | 2011-03-19 08:38:50 +0000 | [diff] [blame] | 4857 | ** Parameter P4 may point to a Table structure, or may be NULL. If it is |
| 4858 | ** not NULL, then the update-hook (sqlite3.xUpdateCallback) is invoked |
| 4859 | ** following a successful insert. |
danielk1977 | 1f6eec5 | 2006-06-16 06:17:47 +0000 | [diff] [blame] | 4860 | ** |
drh | 93aed5a | 2008-01-16 17:46:38 +0000 | [diff] [blame] | 4861 | ** (WARNING/TODO: If P1 is a pseudo-cursor and P2 is dynamically |
| 4862 | ** allocated, then ownership of P2 is transferred to the pseudo-cursor |
| 4863 | ** and register P2 becomes ephemeral. If the cursor is changed, the |
| 4864 | ** value of register P2 will then change. Make sure this does not |
| 4865 | ** cause any problems.) |
| 4866 | ** |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 4867 | ** This instruction only works on tables. The equivalent instruction |
| 4868 | ** for indices is OP_IdxInsert. |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 4869 | */ |
drh | 50ef671 | 2019-02-22 23:29:56 +0000 | [diff] [blame] | 4870 | case OP_Insert: { |
drh | 3e9ca09 | 2009-09-08 01:14:48 +0000 | [diff] [blame] | 4871 | Mem *pData; /* MEM cell holding data for the record to be inserted */ |
| 4872 | Mem *pKey; /* MEM cell holding key for the record */ |
drh | 3e9ca09 | 2009-09-08 01:14:48 +0000 | [diff] [blame] | 4873 | VdbeCursor *pC; /* Cursor to table into which insert is written */ |
drh | 3e9ca09 | 2009-09-08 01:14:48 +0000 | [diff] [blame] | 4874 | int seekResult; /* Result of prior seek or 0 if no USESEEKRESULT flag */ |
| 4875 | const char *zDb; /* database name - used by the update hook */ |
dan | 319eeb7 | 2011-03-19 08:38:50 +0000 | [diff] [blame] | 4876 | Table *pTab; /* Table structure - used by update and pre-update hooks */ |
drh | 8eeb446 | 2016-05-21 20:03:42 +0000 | [diff] [blame] | 4877 | BtreePayload x; /* Payload to be inserted */ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 4878 | |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 4879 | pData = &aMem[pOp->p2]; |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 4880 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 4881 | assert( memIsValid(pData) ); |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 4882 | pC = p->apCsr[pOp->p1]; |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 4883 | assert( pC!=0 ); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 4884 | assert( pC->eCurType==CURTYPE_BTREE ); |
drh | be3da24 | 2019-12-29 00:52:41 +0000 | [diff] [blame] | 4885 | assert( pC->deferredMoveto==0 ); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 4886 | assert( pC->uc.pCursor!=0 ); |
dan | cb9a364 | 2017-01-30 19:44:53 +0000 | [diff] [blame] | 4887 | assert( (pOp->p5 & OPFLAG_ISNOOP) || pC->isTable ); |
drh | cbf1b8e | 2013-11-11 22:55:26 +0000 | [diff] [blame] | 4888 | assert( pOp->p4type==P4_TABLE || pOp->p4type>=P4_STATIC ); |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 4889 | REGISTER_TRACE(pOp->p2, pData); |
drh | 4031baf | 2018-05-28 17:31:20 +0000 | [diff] [blame] | 4890 | sqlite3VdbeIncrWriteCounter(p, pC); |
danielk1977 | 5f8d8a8 | 2004-05-11 00:28:42 +0000 | [diff] [blame] | 4891 | |
drh | 50ef671 | 2019-02-22 23:29:56 +0000 | [diff] [blame] | 4892 | pKey = &aMem[pOp->p3]; |
| 4893 | assert( pKey->flags & MEM_Int ); |
| 4894 | assert( memIsValid(pKey) ); |
| 4895 | REGISTER_TRACE(pOp->p3, pKey); |
| 4896 | x.nKey = pKey->u.i; |
drh | e05c929 | 2009-10-29 13:48:10 +0000 | [diff] [blame] | 4897 | |
drh | 9b1c62d | 2011-03-30 21:04:43 +0000 | [diff] [blame] | 4898 | if( pOp->p4type==P4_TABLE && HAS_UPDATE_HOOK(db) ){ |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 4899 | assert( pC->iDb>=0 ); |
drh | 69c3382 | 2016-08-18 14:33:11 +0000 | [diff] [blame] | 4900 | zDb = db->aDb[pC->iDb].zDbSName; |
dan | 319eeb7 | 2011-03-19 08:38:50 +0000 | [diff] [blame] | 4901 | pTab = pOp->p4.pTab; |
dan | cb9a364 | 2017-01-30 19:44:53 +0000 | [diff] [blame] | 4902 | assert( (pOp->p5 & OPFLAG_ISNOOP) || HasRowid(pTab) ); |
drh | 74c3302 | 2016-03-30 12:56:55 +0000 | [diff] [blame] | 4903 | }else{ |
drh | 4ec6f3a | 2018-01-12 19:33:18 +0000 | [diff] [blame] | 4904 | pTab = 0; |
drh | 74c3302 | 2016-03-30 12:56:55 +0000 | [diff] [blame] | 4905 | zDb = 0; /* Not needed. Silence a compiler warning. */ |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 4906 | } |
| 4907 | |
drh | 9b1c62d | 2011-03-30 21:04:43 +0000 | [diff] [blame] | 4908 | #ifdef SQLITE_ENABLE_PREUPDATE_HOOK |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 4909 | /* Invoke the pre-update hook, if any */ |
drh | 4ec6f3a | 2018-01-12 19:33:18 +0000 | [diff] [blame] | 4910 | if( pTab ){ |
drh | 84ebe2b | 2018-01-12 18:46:52 +0000 | [diff] [blame] | 4911 | if( db->xPreUpdateCallback && !(pOp->p5 & OPFLAG_ISUPDATE) ){ |
| 4912 | sqlite3VdbePreUpdateHook(p, pC, SQLITE_INSERT, zDb, pTab, x.nKey,pOp->p2); |
| 4913 | } |
drh | 4ec6f3a | 2018-01-12 19:33:18 +0000 | [diff] [blame] | 4914 | if( db->xUpdateCallback==0 || pTab->aCol==0 ){ |
| 4915 | /* Prevent post-update hook from running in cases when it should not */ |
| 4916 | pTab = 0; |
drh | 84ebe2b | 2018-01-12 18:46:52 +0000 | [diff] [blame] | 4917 | } |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 4918 | } |
dan | cb9a364 | 2017-01-30 19:44:53 +0000 | [diff] [blame] | 4919 | if( pOp->p5 & OPFLAG_ISNOOP ) break; |
drh | 9b1c62d | 2011-03-30 21:04:43 +0000 | [diff] [blame] | 4920 | #endif |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 4921 | |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 4922 | if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++; |
drh | fae58d5 | 2017-01-26 17:26:44 +0000 | [diff] [blame] | 4923 | if( pOp->p5 & OPFLAG_LASTROWID ) db->lastRowid = x.nKey; |
dan | 21cd29a | 2017-10-23 16:03:54 +0000 | [diff] [blame] | 4924 | assert( pData->flags & (MEM_Blob|MEM_Str) ); |
| 4925 | x.pData = pData->z; |
| 4926 | x.nData = pData->n; |
drh | 3e9ca09 | 2009-09-08 01:14:48 +0000 | [diff] [blame] | 4927 | seekResult = ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0); |
| 4928 | if( pData->flags & MEM_Zero ){ |
drh | 8eeb446 | 2016-05-21 20:03:42 +0000 | [diff] [blame] | 4929 | x.nZero = pData->u.nZero; |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 4930 | }else{ |
drh | 8eeb446 | 2016-05-21 20:03:42 +0000 | [diff] [blame] | 4931 | x.nZero = 0; |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 4932 | } |
drh | 8eeb446 | 2016-05-21 20:03:42 +0000 | [diff] [blame] | 4933 | x.pKey = 0; |
| 4934 | rc = sqlite3BtreeInsert(pC->uc.pCursor, &x, |
dan | f91c131 | 2017-01-10 20:04:38 +0000 | [diff] [blame] | 4935 | (pOp->p5 & (OPFLAG_APPEND|OPFLAG_SAVEPOSITION)), seekResult |
drh | 3e9ca09 | 2009-09-08 01:14:48 +0000 | [diff] [blame] | 4936 | ); |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 4937 | pC->deferredMoveto = 0; |
| 4938 | pC->cacheStatus = CACHE_STALE; |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 +0000 | [diff] [blame] | 4939 | |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 4940 | /* Invoke the update-hook if required. */ |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 4941 | if( rc ) goto abort_due_to_error; |
drh | 4ec6f3a | 2018-01-12 19:33:18 +0000 | [diff] [blame] | 4942 | if( pTab ){ |
| 4943 | assert( db->xUpdateCallback!=0 ); |
| 4944 | assert( pTab->aCol!=0 ); |
| 4945 | db->xUpdateCallback(db->pUpdateArg, |
| 4946 | (pOp->p5 & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_INSERT, |
| 4947 | zDb, pTab->zName, x.nKey); |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 4948 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4949 | break; |
| 4950 | } |
| 4951 | |
dan | 438b881 | 2015-09-15 15:55:15 +0000 | [diff] [blame] | 4952 | /* Opcode: Delete P1 P2 P3 P4 P5 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4953 | ** |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 4954 | ** Delete the record at which the P1 cursor is currently pointing. |
| 4955 | ** |
drh | e807bdb | 2016-01-21 17:06:33 +0000 | [diff] [blame] | 4956 | ** If the OPFLAG_SAVEPOSITION bit of the P5 parameter is set, then |
| 4957 | ** the cursor will be left pointing at either the next or the previous |
| 4958 | ** record in the table. If it is left pointing at the next record, then |
| 4959 | ** the next Next instruction will be a no-op. As a result, in this case |
| 4960 | ** it is ok to delete a record from within a Next loop. If |
| 4961 | ** OPFLAG_SAVEPOSITION bit of P5 is clear, then the cursor will be |
| 4962 | ** left in an undefined state. |
drh | c8d30ac | 2002-04-12 10:08:59 +0000 | [diff] [blame] | 4963 | ** |
drh | def19e3 | 2016-01-27 16:26:25 +0000 | [diff] [blame] | 4964 | ** If the OPFLAG_AUXDELETE bit is set on P5, that indicates that this |
| 4965 | ** delete one of several associated with deleting a table row and all its |
| 4966 | ** associated index entries. Exactly one of those deletes is the "primary" |
| 4967 | ** delete. The others are all on OPFLAG_FORDELETE cursors or else are |
| 4968 | ** marked with the AUXDELETE flag. |
drh | e807bdb | 2016-01-21 17:06:33 +0000 | [diff] [blame] | 4969 | ** |
| 4970 | ** If the OPFLAG_NCHANGE flag of P2 (NB: P2 not P5) is set, then the row |
| 4971 | ** change count is incremented (otherwise not). |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 4972 | ** |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 4973 | ** P1 must not be pseudo-table. It has to be a real table with |
| 4974 | ** multiple rows. |
| 4975 | ** |
drh | 5e769a5 | 2016-09-28 16:05:53 +0000 | [diff] [blame] | 4976 | ** 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] | 4977 | ** the update or pre-update hook, or both, may be invoked. The P1 cursor must |
| 4978 | ** have been positioned using OP_NotFound prior to invoking this opcode in |
| 4979 | ** this case. Specifically, if one is configured, the pre-update hook is |
| 4980 | ** invoked if P4 is not NULL. The update-hook is invoked if one is configured, |
| 4981 | ** P4 is not NULL, and the OPFLAG_NCHANGE flag is set in P2. |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 4982 | ** |
| 4983 | ** If the OPFLAG_ISUPDATE flag is set in P2, then P3 contains the address |
| 4984 | ** of the memory cell that contains the value that the rowid of the row will |
| 4985 | ** be set to by the update. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4986 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4987 | case OP_Delete: { |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 4988 | VdbeCursor *pC; |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 4989 | const char *zDb; |
dan | 319eeb7 | 2011-03-19 08:38:50 +0000 | [diff] [blame] | 4990 | Table *pTab; |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 4991 | int opflags; |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 4992 | |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 4993 | opflags = pOp->p2; |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 4994 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 4995 | pC = p->apCsr[pOp->p1]; |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 4996 | assert( pC!=0 ); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 4997 | assert( pC->eCurType==CURTYPE_BTREE ); |
| 4998 | assert( pC->uc.pCursor!=0 ); |
drh | 9a65f2c | 2009-06-22 19:05:40 +0000 | [diff] [blame] | 4999 | assert( pC->deferredMoveto==0 ); |
drh | 4031baf | 2018-05-28 17:31:20 +0000 | [diff] [blame] | 5000 | sqlite3VdbeIncrWriteCounter(p, pC); |
drh | 9a65f2c | 2009-06-22 19:05:40 +0000 | [diff] [blame] | 5001 | |
drh | b53a5a9 | 2014-10-12 22:37:22 +0000 | [diff] [blame] | 5002 | #ifdef SQLITE_DEBUG |
drh | 6b559f3 | 2020-01-02 19:50:50 +0000 | [diff] [blame] | 5003 | if( pOp->p4type==P4_TABLE |
| 5004 | && HasRowid(pOp->p4.pTab) |
| 5005 | && pOp->p5==0 |
| 5006 | && sqlite3BtreeCursorIsValidNN(pC->uc.pCursor) |
| 5007 | ){ |
dan | 438b881 | 2015-09-15 15:55:15 +0000 | [diff] [blame] | 5008 | /* If p5 is zero, the seek operation that positioned the cursor prior to |
| 5009 | ** OP_Delete will have also set the pC->movetoTarget field to the rowid of |
| 5010 | ** the row that is being deleted */ |
drh | a7c90c4 | 2016-06-04 20:37:10 +0000 | [diff] [blame] | 5011 | i64 iKey = sqlite3BtreeIntegerKey(pC->uc.pCursor); |
dan | 0971ef4 | 2019-05-16 20:13:32 +0000 | [diff] [blame] | 5012 | assert( CORRUPT_DB || pC->movetoTarget==iKey ); |
drh | b53a5a9 | 2014-10-12 22:37:22 +0000 | [diff] [blame] | 5013 | } |
| 5014 | #endif |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 5015 | |
dan | 438b881 | 2015-09-15 15:55:15 +0000 | [diff] [blame] | 5016 | /* If the update-hook or pre-update-hook will be invoked, set zDb to |
| 5017 | ** the name of the db to pass as to it. Also set local pTab to a copy |
| 5018 | ** of p4.pTab. Finally, if p5 is true, indicating that this cursor was |
| 5019 | ** last moved with OP_Next or OP_Prev, not Seek or NotFound, set |
| 5020 | ** VdbeCursor.movetoTarget to the current rowid. */ |
drh | c556f3c | 2016-03-30 15:30:07 +0000 | [diff] [blame] | 5021 | if( pOp->p4type==P4_TABLE && HAS_UPDATE_HOOK(db) ){ |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 5022 | assert( pC->iDb>=0 ); |
drh | c556f3c | 2016-03-30 15:30:07 +0000 | [diff] [blame] | 5023 | assert( pOp->p4.pTab!=0 ); |
drh | 69c3382 | 2016-08-18 14:33:11 +0000 | [diff] [blame] | 5024 | zDb = db->aDb[pC->iDb].zDbSName; |
dan | 319eeb7 | 2011-03-19 08:38:50 +0000 | [diff] [blame] | 5025 | pTab = pOp->p4.pTab; |
drh | c556f3c | 2016-03-30 15:30:07 +0000 | [diff] [blame] | 5026 | if( (pOp->p5 & OPFLAG_SAVEPOSITION)!=0 && pC->isTable ){ |
drh | a7c90c4 | 2016-06-04 20:37:10 +0000 | [diff] [blame] | 5027 | pC->movetoTarget = sqlite3BtreeIntegerKey(pC->uc.pCursor); |
dan | 438b881 | 2015-09-15 15:55:15 +0000 | [diff] [blame] | 5028 | } |
drh | 74c3302 | 2016-03-30 12:56:55 +0000 | [diff] [blame] | 5029 | }else{ |
| 5030 | zDb = 0; /* Not needed. Silence a compiler warning. */ |
| 5031 | pTab = 0; /* Not needed. Silence a compiler warning. */ |
drh | 92fe38e | 2014-10-14 13:41:32 +0000 | [diff] [blame] | 5032 | } |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 5033 | |
drh | 9b1c62d | 2011-03-30 21:04:43 +0000 | [diff] [blame] | 5034 | #ifdef SQLITE_ENABLE_PREUPDATE_HOOK |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 5035 | /* Invoke the pre-update-hook if required. */ |
dan | cb9a364 | 2017-01-30 19:44:53 +0000 | [diff] [blame] | 5036 | if( db->xPreUpdateCallback && pOp->p4.pTab ){ |
| 5037 | assert( !(opflags & OPFLAG_ISUPDATE) |
| 5038 | || HasRowid(pTab)==0 |
| 5039 | || (aMem[pOp->p3].flags & MEM_Int) |
| 5040 | ); |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 5041 | sqlite3VdbePreUpdateHook(p, pC, |
| 5042 | (opflags & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_DELETE, |
drh | 92fe38e | 2014-10-14 13:41:32 +0000 | [diff] [blame] | 5043 | zDb, pTab, pC->movetoTarget, |
dan | 37db03b | 2011-03-16 19:59:18 +0000 | [diff] [blame] | 5044 | pOp->p3 |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 5045 | ); |
| 5046 | } |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 5047 | if( opflags & OPFLAG_ISNOOP ) break; |
drh | c556f3c | 2016-03-30 15:30:07 +0000 | [diff] [blame] | 5048 | #endif |
drh | b53a5a9 | 2014-10-12 22:37:22 +0000 | [diff] [blame] | 5049 | |
drh | def19e3 | 2016-01-27 16:26:25 +0000 | [diff] [blame] | 5050 | /* Only flags that can be set are SAVEPOISTION and AUXDELETE */ |
| 5051 | assert( (pOp->p5 & ~(OPFLAG_SAVEPOSITION|OPFLAG_AUXDELETE))==0 ); |
drh | e807bdb | 2016-01-21 17:06:33 +0000 | [diff] [blame] | 5052 | assert( OPFLAG_SAVEPOSITION==BTREE_SAVEPOSITION ); |
drh | def19e3 | 2016-01-27 16:26:25 +0000 | [diff] [blame] | 5053 | assert( OPFLAG_AUXDELETE==BTREE_AUXDELETE ); |
drh | b89aeb6 | 2016-01-27 15:49:32 +0000 | [diff] [blame] | 5054 | |
| 5055 | #ifdef SQLITE_DEBUG |
dan | e61bbf4 | 2016-01-28 17:06:17 +0000 | [diff] [blame] | 5056 | if( p->pFrame==0 ){ |
| 5057 | if( pC->isEphemeral==0 |
| 5058 | && (pOp->p5 & OPFLAG_AUXDELETE)==0 |
| 5059 | && (pC->wrFlag & OPFLAG_FORDELETE)==0 |
| 5060 | ){ |
| 5061 | nExtraDelete++; |
| 5062 | } |
| 5063 | if( pOp->p2 & OPFLAG_NCHANGE ){ |
| 5064 | nExtraDelete--; |
| 5065 | } |
drh | b89aeb6 | 2016-01-27 15:49:32 +0000 | [diff] [blame] | 5066 | } |
| 5067 | #endif |
| 5068 | |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 5069 | rc = sqlite3BtreeDelete(pC->uc.pCursor, pOp->p5); |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 5070 | pC->cacheStatus = CACHE_STALE; |
dan | 3b908d4 | 2016-11-08 19:22:32 +0000 | [diff] [blame] | 5071 | pC->seekResult = 0; |
drh | d3e1af4 | 2016-02-25 18:54:30 +0000 | [diff] [blame] | 5072 | if( rc ) goto abort_due_to_error; |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 +0000 | [diff] [blame] | 5073 | |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 5074 | /* Invoke the update-hook if required. */ |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 5075 | if( opflags & OPFLAG_NCHANGE ){ |
| 5076 | p->nChange++; |
drh | c556f3c | 2016-03-30 15:30:07 +0000 | [diff] [blame] | 5077 | if( db->xUpdateCallback && HasRowid(pTab) ){ |
drh | 92fe38e | 2014-10-14 13:41:32 +0000 | [diff] [blame] | 5078 | db->xUpdateCallback(db->pUpdateArg, SQLITE_DELETE, zDb, pTab->zName, |
dan | 438b881 | 2015-09-15 15:55:15 +0000 | [diff] [blame] | 5079 | pC->movetoTarget); |
| 5080 | assert( pC->iDb>=0 ); |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 5081 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5082 | } |
dan | 438b881 | 2015-09-15 15:55:15 +0000 | [diff] [blame] | 5083 | |
rdc | b0c374f | 2004-02-20 22:53:38 +0000 | [diff] [blame] | 5084 | break; |
| 5085 | } |
drh | b7f1d9a | 2009-09-08 02:27:58 +0000 | [diff] [blame] | 5086 | /* Opcode: ResetCount * * * * * |
rdc | b0c374f | 2004-02-20 22:53:38 +0000 | [diff] [blame] | 5087 | ** |
drh | b7f1d9a | 2009-09-08 02:27:58 +0000 | [diff] [blame] | 5088 | ** The value of the change counter is copied to the database handle |
| 5089 | ** change counter (returned by subsequent calls to sqlite3_changes()). |
| 5090 | ** Then the VMs internal change counter resets to 0. |
| 5091 | ** This is used by trigger programs. |
rdc | b0c374f | 2004-02-20 22:53:38 +0000 | [diff] [blame] | 5092 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 5093 | case OP_ResetCount: { |
drh | b7f1d9a | 2009-09-08 02:27:58 +0000 | [diff] [blame] | 5094 | sqlite3VdbeSetChanges(db, p->nChange); |
danielk1977 | b28af71 | 2004-06-21 06:50:26 +0000 | [diff] [blame] | 5095 | p->nChange = 0; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5096 | break; |
| 5097 | } |
| 5098 | |
drh | 1153c7b | 2013-11-01 22:02:56 +0000 | [diff] [blame] | 5099 | /* Opcode: SorterCompare P1 P2 P3 P4 |
drh | 72e26de | 2016-08-24 21:24:04 +0000 | [diff] [blame] | 5100 | ** Synopsis: if key(P1)!=trim(r[P3],P4) goto P2 |
dan | 5134d13 | 2011-09-02 10:31:11 +0000 | [diff] [blame] | 5101 | ** |
drh | 1153c7b | 2013-11-01 22:02:56 +0000 | [diff] [blame] | 5102 | ** P1 is a sorter cursor. This instruction compares a prefix of the |
drh | bc5cf38 | 2014-08-06 01:08:07 +0000 | [diff] [blame] | 5103 | ** record blob in register P3 against a prefix of the entry that |
drh | ac50232 | 2014-07-30 13:56:48 +0000 | [diff] [blame] | 5104 | ** the sorter cursor currently points to. Only the first P4 fields |
| 5105 | ** of r[P3] and the sorter record are compared. |
drh | 1153c7b | 2013-11-01 22:02:56 +0000 | [diff] [blame] | 5106 | ** |
| 5107 | ** If either P3 or the sorter contains a NULL in one of their significant |
| 5108 | ** fields (not counting the P4 fields at the end which are ignored) then |
| 5109 | ** the comparison is assumed to be equal. |
| 5110 | ** |
| 5111 | ** Fall through to next instruction if the two records compare equal to |
| 5112 | ** each other. Jump to P2 if they are different. |
dan | 5134d13 | 2011-09-02 10:31:11 +0000 | [diff] [blame] | 5113 | */ |
| 5114 | case OP_SorterCompare: { |
| 5115 | VdbeCursor *pC; |
| 5116 | int res; |
drh | ac50232 | 2014-07-30 13:56:48 +0000 | [diff] [blame] | 5117 | int nKeyCol; |
dan | 5134d13 | 2011-09-02 10:31:11 +0000 | [diff] [blame] | 5118 | |
| 5119 | pC = p->apCsr[pOp->p1]; |
| 5120 | assert( isSorter(pC) ); |
drh | 1153c7b | 2013-11-01 22:02:56 +0000 | [diff] [blame] | 5121 | assert( pOp->p4type==P4_INT32 ); |
dan | 5134d13 | 2011-09-02 10:31:11 +0000 | [diff] [blame] | 5122 | pIn3 = &aMem[pOp->p3]; |
drh | ac50232 | 2014-07-30 13:56:48 +0000 | [diff] [blame] | 5123 | nKeyCol = pOp->p4.i; |
drh | 958d261 | 2014-04-18 13:40:07 +0000 | [diff] [blame] | 5124 | res = 0; |
drh | ac50232 | 2014-07-30 13:56:48 +0000 | [diff] [blame] | 5125 | rc = sqlite3VdbeSorterCompare(pC, pIn3, nKeyCol, &res); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 5126 | VdbeBranchTaken(res!=0,2); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 5127 | if( rc ) goto abort_due_to_error; |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 5128 | if( res ) goto jump_to_p2; |
dan | 5134d13 | 2011-09-02 10:31:11 +0000 | [diff] [blame] | 5129 | break; |
| 5130 | }; |
| 5131 | |
drh | 6cf4a7d | 2014-10-13 13:00:58 +0000 | [diff] [blame] | 5132 | /* Opcode: SorterData P1 P2 P3 * * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 5133 | ** Synopsis: r[P2]=data |
dan | 5134d13 | 2011-09-02 10:31:11 +0000 | [diff] [blame] | 5134 | ** |
| 5135 | ** Write into register P2 the current sorter data for sorter cursor P1. |
drh | 6cf4a7d | 2014-10-13 13:00:58 +0000 | [diff] [blame] | 5136 | ** Then clear the column header cache on cursor P3. |
| 5137 | ** |
| 5138 | ** This opcode is normally use to move a record out of the sorter and into |
| 5139 | ** a register that is the source for a pseudo-table cursor created using |
| 5140 | ** OpenPseudo. That pseudo-table cursor is the one that is identified by |
| 5141 | ** parameter P3. Clearing the P3 column cache as part of this opcode saves |
| 5142 | ** us from having to issue a separate NullRow instruction to clear that cache. |
dan | 5134d13 | 2011-09-02 10:31:11 +0000 | [diff] [blame] | 5143 | */ |
| 5144 | case OP_SorterData: { |
| 5145 | VdbeCursor *pC; |
drh | 3a94987 | 2012-09-18 13:20:13 +0000 | [diff] [blame] | 5146 | |
dan | 5134d13 | 2011-09-02 10:31:11 +0000 | [diff] [blame] | 5147 | pOut = &aMem[pOp->p2]; |
| 5148 | pC = p->apCsr[pOp->p1]; |
drh | 14da87f | 2013-11-20 21:51:33 +0000 | [diff] [blame] | 5149 | assert( isSorter(pC) ); |
dan | 5134d13 | 2011-09-02 10:31:11 +0000 | [diff] [blame] | 5150 | rc = sqlite3VdbeSorterRowkey(pC, pOut); |
dan | 3852413 | 2014-05-01 20:26:48 +0000 | [diff] [blame] | 5151 | assert( rc!=SQLITE_OK || (pOut->flags & MEM_Blob) ); |
drh | 6cf4a7d | 2014-10-13 13:00:58 +0000 | [diff] [blame] | 5152 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 5153 | if( rc ) goto abort_due_to_error; |
drh | 6cf4a7d | 2014-10-13 13:00:58 +0000 | [diff] [blame] | 5154 | p->apCsr[pOp->p3]->cacheStatus = CACHE_STALE; |
dan | 5134d13 | 2011-09-02 10:31:11 +0000 | [diff] [blame] | 5155 | break; |
| 5156 | } |
| 5157 | |
drh | e7b554d | 2017-01-09 15:44:25 +0000 | [diff] [blame] | 5158 | /* Opcode: RowData P1 P2 P3 * * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 5159 | ** Synopsis: r[P2]=data |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 5160 | ** |
drh | 9057fc7 | 2016-11-25 19:32:32 +0000 | [diff] [blame] | 5161 | ** Write into register P2 the complete row content for the row at |
| 5162 | ** which cursor P1 is currently pointing. |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 5163 | ** There is no interpretation of the data. |
| 5164 | ** It is just copied onto the P2 register exactly as |
danielk1977 | 96cb76f | 2008-01-04 13:24:28 +0000 | [diff] [blame] | 5165 | ** it is found in the database file. |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 5166 | ** |
drh | 9057fc7 | 2016-11-25 19:32:32 +0000 | [diff] [blame] | 5167 | ** If cursor P1 is an index, then the content is the key of the row. |
| 5168 | ** If cursor P2 is a table, then the content extracted is the data. |
drh | 143f3c4 | 2004-01-07 20:37:52 +0000 | [diff] [blame] | 5169 | ** |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 5170 | ** If the P1 cursor must be pointing to a valid row (not a NULL row) |
| 5171 | ** of a real table, not a pseudo-table. |
drh | e7b554d | 2017-01-09 15:44:25 +0000 | [diff] [blame] | 5172 | ** |
drh | 8cdafc3 | 2018-05-31 19:00:20 +0000 | [diff] [blame] | 5173 | ** If P3!=0 then this opcode is allowed to make an ephemeral pointer |
drh | e7b554d | 2017-01-09 15:44:25 +0000 | [diff] [blame] | 5174 | ** into the database page. That means that the content of the output |
| 5175 | ** register will be invalidated as soon as the cursor moves - including |
drh | 416a801 | 2018-05-31 19:14:52 +0000 | [diff] [blame] | 5176 | ** moves caused by other cursors that "save" the current cursors |
drh | e7b554d | 2017-01-09 15:44:25 +0000 | [diff] [blame] | 5177 | ** position in order that they can write to the same table. If P3==0 |
| 5178 | ** then a copy of the data is made into memory. P3!=0 is faster, but |
| 5179 | ** P3==0 is safer. |
| 5180 | ** |
| 5181 | ** If P3!=0 then the content of the P2 register is unsuitable for use |
| 5182 | ** in OP_Result and any OP_Result will invalidate the P2 register content. |
mistachkin | ab61cf7 | 2017-01-09 18:22:54 +0000 | [diff] [blame] | 5183 | ** The P2 register content is invalidated by opcodes like OP_Function or |
drh | e7b554d | 2017-01-09 15:44:25 +0000 | [diff] [blame] | 5184 | ** by any use of another cursor pointing to the same table. |
drh | 143f3c4 | 2004-01-07 20:37:52 +0000 | [diff] [blame] | 5185 | */ |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 5186 | case OP_RowData: { |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 5187 | VdbeCursor *pC; |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 5188 | BtCursor *pCrsr; |
danielk1977 | e0d4b06 | 2004-06-28 01:11:46 +0000 | [diff] [blame] | 5189 | u32 n; |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 5190 | |
drh | e7b554d | 2017-01-09 15:44:25 +0000 | [diff] [blame] | 5191 | pOut = out2Prerelease(p, pOp); |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 5192 | |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 5193 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 5194 | pC = p->apCsr[pOp->p1]; |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 5195 | assert( pC!=0 ); |
| 5196 | assert( pC->eCurType==CURTYPE_BTREE ); |
drh | 14da87f | 2013-11-20 21:51:33 +0000 | [diff] [blame] | 5197 | assert( isSorter(pC)==0 ); |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 5198 | assert( pC->nullRow==0 ); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 5199 | assert( pC->uc.pCursor!=0 ); |
| 5200 | pCrsr = pC->uc.pCursor; |
drh | 9a65f2c | 2009-06-22 19:05:40 +0000 | [diff] [blame] | 5201 | |
drh | 9057fc7 | 2016-11-25 19:32:32 +0000 | [diff] [blame] | 5202 | /* The OP_RowData opcodes always follow OP_NotExists or |
drh | eeb9565 | 2016-05-26 20:56:38 +0000 | [diff] [blame] | 5203 | ** OP_SeekRowid or OP_Rewind/Op_Next with no intervening instructions |
| 5204 | ** that might invalidate the cursor. |
| 5205 | ** If this where not the case, on of the following assert()s |
drh | c22284f | 2014-10-13 16:02:20 +0000 | [diff] [blame] | 5206 | ** would fail. Should this ever change (because of changes in the code |
| 5207 | ** generator) then the fix would be to insert a call to |
| 5208 | ** sqlite3VdbeCursorMoveto(). |
drh | 9a65f2c | 2009-06-22 19:05:40 +0000 | [diff] [blame] | 5209 | */ |
| 5210 | assert( pC->deferredMoveto==0 ); |
drh | c22284f | 2014-10-13 16:02:20 +0000 | [diff] [blame] | 5211 | assert( sqlite3BtreeCursorIsValid(pCrsr) ); |
| 5212 | #if 0 /* Not required due to the previous to assert() statements */ |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 5213 | rc = sqlite3VdbeCursorMoveto(pC); |
drh | c22284f | 2014-10-13 16:02:20 +0000 | [diff] [blame] | 5214 | if( rc!=SQLITE_OK ) goto abort_due_to_error; |
| 5215 | #endif |
drh | 9a65f2c | 2009-06-22 19:05:40 +0000 | [diff] [blame] | 5216 | |
drh | a7c90c4 | 2016-06-04 20:37:10 +0000 | [diff] [blame] | 5217 | n = sqlite3BtreePayloadSize(pCrsr); |
drh | d66c4f8 | 2016-06-04 20:58:35 +0000 | [diff] [blame] | 5218 | if( n>(u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){ |
drh | a7c90c4 | 2016-06-04 20:37:10 +0000 | [diff] [blame] | 5219 | goto too_big; |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 5220 | } |
drh | 722246e | 2014-10-07 23:02:24 +0000 | [diff] [blame] | 5221 | testcase( n==0 ); |
drh | 2a74006 | 2020-02-05 18:28:17 +0000 | [diff] [blame] | 5222 | rc = sqlite3VdbeMemFromBtreeZeroOffset(pCrsr, n, pOut); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 5223 | if( rc ) goto abort_due_to_error; |
drh | e7b554d | 2017-01-09 15:44:25 +0000 | [diff] [blame] | 5224 | if( !pOp->p3 ) Deephemeralize(pOut); |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 5225 | UPDATE_MAX_BLOBSIZE(pOut); |
drh | ee0ec8e | 2013-10-31 17:38:01 +0000 | [diff] [blame] | 5226 | REGISTER_TRACE(pOp->p2, pOut); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5227 | break; |
| 5228 | } |
| 5229 | |
drh | 2133d82 | 2008-01-03 18:44:59 +0000 | [diff] [blame] | 5230 | /* Opcode: Rowid P1 P2 * * * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 5231 | ** Synopsis: r[P2]=rowid |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5232 | ** |
drh | 2133d82 | 2008-01-03 18:44:59 +0000 | [diff] [blame] | 5233 | ** 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] | 5234 | ** P1 is currently point to. |
drh | 044925b | 2009-04-22 17:15:02 +0000 | [diff] [blame] | 5235 | ** |
| 5236 | ** P1 can be either an ordinary table or a virtual table. There used to |
| 5237 | ** be a separate OP_VRowid opcode for use with virtual tables, but this |
| 5238 | ** one opcode now works for both table types. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5239 | */ |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 5240 | case OP_Rowid: { /* out2 */ |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 5241 | VdbeCursor *pC; |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 5242 | i64 v; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 5243 | sqlite3_vtab *pVtab; |
| 5244 | const sqlite3_module *pModule; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5245 | |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 5246 | pOut = out2Prerelease(p, pOp); |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 5247 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 5248 | pC = p->apCsr[pOp->p1]; |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 5249 | assert( pC!=0 ); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 5250 | assert( pC->eCurType!=CURTYPE_PSEUDO || pC->nullRow ); |
drh | 044925b | 2009-04-22 17:15:02 +0000 | [diff] [blame] | 5251 | if( pC->nullRow ){ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 5252 | pOut->flags = MEM_Null; |
drh | 044925b | 2009-04-22 17:15:02 +0000 | [diff] [blame] | 5253 | break; |
| 5254 | }else if( pC->deferredMoveto ){ |
drh | 6149526 | 2009-04-22 15:32:59 +0000 | [diff] [blame] | 5255 | v = pC->movetoTarget; |
drh | 044925b | 2009-04-22 17:15:02 +0000 | [diff] [blame] | 5256 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 5257 | }else if( pC->eCurType==CURTYPE_VTAB ){ |
| 5258 | assert( pC->uc.pVCur!=0 ); |
| 5259 | pVtab = pC->uc.pVCur->pVtab; |
drh | 044925b | 2009-04-22 17:15:02 +0000 | [diff] [blame] | 5260 | pModule = pVtab->pModule; |
| 5261 | assert( pModule->xRowid ); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 5262 | rc = pModule->xRowid(pC->uc.pVCur, &v); |
dan | 016f781 | 2013-08-21 17:35:48 +0000 | [diff] [blame] | 5263 | sqlite3VtabImportErrmsg(p, pVtab); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 5264 | if( rc ) goto abort_due_to_error; |
drh | 044925b | 2009-04-22 17:15:02 +0000 | [diff] [blame] | 5265 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 5266 | }else{ |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 5267 | assert( pC->eCurType==CURTYPE_BTREE ); |
| 5268 | assert( pC->uc.pCursor!=0 ); |
drh | c22284f | 2014-10-13 16:02:20 +0000 | [diff] [blame] | 5269 | rc = sqlite3VdbeCursorRestore(pC); |
drh | 6149526 | 2009-04-22 15:32:59 +0000 | [diff] [blame] | 5270 | if( rc ) goto abort_due_to_error; |
dan | 2b8669a | 2014-11-17 19:42:48 +0000 | [diff] [blame] | 5271 | if( pC->nullRow ){ |
| 5272 | pOut->flags = MEM_Null; |
| 5273 | break; |
| 5274 | } |
drh | a7c90c4 | 2016-06-04 20:37:10 +0000 | [diff] [blame] | 5275 | v = sqlite3BtreeIntegerKey(pC->uc.pCursor); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5276 | } |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 5277 | pOut->u.i = v; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5278 | break; |
| 5279 | } |
| 5280 | |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 5281 | /* Opcode: NullRow P1 * * * * |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 5282 | ** |
| 5283 | ** Move the cursor P1 to a null row. Any OP_Column operations |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 5284 | ** that occur while the cursor is on the null row will always |
| 5285 | ** write a NULL. |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 5286 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 5287 | case OP_NullRow: { |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 5288 | VdbeCursor *pC; |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 5289 | |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 5290 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 5291 | pC = p->apCsr[pOp->p1]; |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 5292 | assert( pC!=0 ); |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame] | 5293 | pC->nullRow = 1; |
drh | 399af1d | 2013-11-20 17:25:55 +0000 | [diff] [blame] | 5294 | pC->cacheStatus = CACHE_STALE; |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 5295 | if( pC->eCurType==CURTYPE_BTREE ){ |
| 5296 | assert( pC->uc.pCursor!=0 ); |
| 5297 | sqlite3BtreeClearCursor(pC->uc.pCursor); |
danielk1977 | be51a65 | 2008-10-08 17:58:48 +0000 | [diff] [blame] | 5298 | } |
drh | cf025a8 | 2018-06-07 18:01:21 +0000 | [diff] [blame] | 5299 | #ifdef SQLITE_DEBUG |
| 5300 | if( pC->seekOp==0 ) pC->seekOp = OP_NullRow; |
| 5301 | #endif |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 5302 | break; |
| 5303 | } |
| 5304 | |
drh | 86b40df | 2017-08-01 19:53:43 +0000 | [diff] [blame] | 5305 | /* Opcode: SeekEnd P1 * * * * |
| 5306 | ** |
| 5307 | ** Position cursor P1 at the end of the btree for the purpose of |
| 5308 | ** appending a new entry onto the btree. |
| 5309 | ** |
| 5310 | ** It is assumed that the cursor is used only for appending and so |
| 5311 | ** if the cursor is valid, then the cursor must already be pointing |
| 5312 | ** at the end of the btree and so no changes are made to |
| 5313 | ** the cursor. |
| 5314 | */ |
| 5315 | /* Opcode: Last P1 P2 * * * |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 5316 | ** |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 5317 | ** The next use of the Rowid or Column or Prev instruction for P1 |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 5318 | ** will refer to the last entry in the database table or index. |
| 5319 | ** If the table or index is empty and P2>0, then jump immediately to P2. |
| 5320 | ** If P2 is 0 or if the table or index is not empty, fall through |
| 5321 | ** to the following instruction. |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 5322 | ** |
| 5323 | ** This opcode leaves the cursor configured to move in reverse order, |
| 5324 | ** from the end toward the beginning. In other words, the cursor is |
drh | 5dad9a3 | 2014-07-25 18:37:42 +0000 | [diff] [blame] | 5325 | ** configured to use Prev, not Next. |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 5326 | */ |
drh | 86b40df | 2017-08-01 19:53:43 +0000 | [diff] [blame] | 5327 | case OP_SeekEnd: |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 5328 | case OP_Last: { /* jump */ |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 5329 | VdbeCursor *pC; |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 5330 | BtCursor *pCrsr; |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 5331 | int res; |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 5332 | |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 5333 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 5334 | pC = p->apCsr[pOp->p1]; |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 5335 | assert( pC!=0 ); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 5336 | assert( pC->eCurType==CURTYPE_BTREE ); |
| 5337 | pCrsr = pC->uc.pCursor; |
drh | 7abc540 | 2011-10-22 21:00:46 +0000 | [diff] [blame] | 5338 | res = 0; |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 5339 | assert( pCrsr!=0 ); |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 5340 | #ifdef SQLITE_DEBUG |
drh | 86b40df | 2017-08-01 19:53:43 +0000 | [diff] [blame] | 5341 | pC->seekOp = pOp->opcode; |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 5342 | #endif |
drh | 86b40df | 2017-08-01 19:53:43 +0000 | [diff] [blame] | 5343 | if( pOp->opcode==OP_SeekEnd ){ |
drh | d6ef5af | 2016-11-15 04:00:24 +0000 | [diff] [blame] | 5344 | assert( pOp->p2==0 ); |
drh | 86b40df | 2017-08-01 19:53:43 +0000 | [diff] [blame] | 5345 | pC->seekResult = -1; |
| 5346 | if( sqlite3BtreeCursorIsValidNN(pCrsr) ){ |
| 5347 | break; |
| 5348 | } |
| 5349 | } |
| 5350 | rc = sqlite3BtreeLast(pCrsr, &res); |
| 5351 | pC->nullRow = (u8)res; |
| 5352 | pC->deferredMoveto = 0; |
| 5353 | pC->cacheStatus = CACHE_STALE; |
| 5354 | if( rc ) goto abort_due_to_error; |
| 5355 | if( pOp->p2>0 ){ |
| 5356 | VdbeBranchTaken(res!=0,2); |
| 5357 | if( res ) goto jump_to_p2; |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 5358 | } |
| 5359 | break; |
| 5360 | } |
| 5361 | |
drh | 5e98e83 | 2017-02-17 19:24:06 +0000 | [diff] [blame] | 5362 | /* Opcode: IfSmaller P1 P2 P3 * * |
| 5363 | ** |
| 5364 | ** Estimate the number of rows in the table P1. Jump to P2 if that |
| 5365 | ** estimate is less than approximately 2**(0.1*P3). |
| 5366 | */ |
| 5367 | case OP_IfSmaller: { /* jump */ |
| 5368 | VdbeCursor *pC; |
| 5369 | BtCursor *pCrsr; |
| 5370 | int res; |
| 5371 | i64 sz; |
| 5372 | |
| 5373 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 5374 | pC = p->apCsr[pOp->p1]; |
| 5375 | assert( pC!=0 ); |
| 5376 | pCrsr = pC->uc.pCursor; |
| 5377 | assert( pCrsr ); |
| 5378 | rc = sqlite3BtreeFirst(pCrsr, &res); |
| 5379 | if( rc ) goto abort_due_to_error; |
| 5380 | if( res==0 ){ |
| 5381 | sz = sqlite3BtreeRowCountEst(pCrsr); |
| 5382 | if( ALWAYS(sz>=0) && sqlite3LogEst((u64)sz)<pOp->p3 ) res = 1; |
| 5383 | } |
| 5384 | VdbeBranchTaken(res!=0,2); |
| 5385 | if( res ) goto jump_to_p2; |
| 5386 | break; |
| 5387 | } |
| 5388 | |
drh | 0342b1f | 2005-09-01 03:07:44 +0000 | [diff] [blame] | 5389 | |
drh | 6bd4dc6 | 2016-12-23 16:05:22 +0000 | [diff] [blame] | 5390 | /* Opcode: SorterSort P1 P2 * * * |
| 5391 | ** |
| 5392 | ** After all records have been inserted into the Sorter object |
| 5393 | ** identified by P1, invoke this opcode to actually do the sorting. |
| 5394 | ** Jump to P2 if there are no records to be sorted. |
| 5395 | ** |
| 5396 | ** This opcode is an alias for OP_Sort and OP_Rewind that is used |
| 5397 | ** for Sorter objects. |
| 5398 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 5399 | /* Opcode: Sort P1 P2 * * * |
drh | 0342b1f | 2005-09-01 03:07:44 +0000 | [diff] [blame] | 5400 | ** |
| 5401 | ** This opcode does exactly the same thing as OP_Rewind except that |
| 5402 | ** it increments an undocumented global variable used for testing. |
| 5403 | ** |
| 5404 | ** Sorting is accomplished by writing records into a sorting index, |
| 5405 | ** then rewinding that index and playing it back from beginning to |
| 5406 | ** end. We use the OP_Sort opcode instead of OP_Rewind to do the |
| 5407 | ** rewinding so that the global variable will be incremented and |
| 5408 | ** regression tests can determine whether or not the optimizer is |
| 5409 | ** correctly optimizing out sorts. |
| 5410 | */ |
drh | c6aff30 | 2011-09-01 15:32:47 +0000 | [diff] [blame] | 5411 | case OP_SorterSort: /* jump */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 5412 | case OP_Sort: { /* jump */ |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 5413 | #ifdef SQLITE_TEST |
drh | 0342b1f | 2005-09-01 03:07:44 +0000 | [diff] [blame] | 5414 | sqlite3_sort_count++; |
drh | 4db38a7 | 2005-09-01 12:16:28 +0000 | [diff] [blame] | 5415 | sqlite3_search_count--; |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 5416 | #endif |
drh | 9b47ee3 | 2013-08-20 03:13:51 +0000 | [diff] [blame] | 5417 | p->aCounter[SQLITE_STMTSTATUS_SORT]++; |
drh | 0342b1f | 2005-09-01 03:07:44 +0000 | [diff] [blame] | 5418 | /* Fall through into OP_Rewind */ |
| 5419 | } |
drh | 038ebf6 | 2019-03-29 15:21:22 +0000 | [diff] [blame] | 5420 | /* Opcode: Rewind P1 P2 * * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5421 | ** |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 5422 | ** The next use of the Rowid or Column or Next instruction for P1 |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 5423 | ** will refer to the first entry in the database table or index. |
dan | 04489b6 | 2014-10-31 20:11:32 +0000 | [diff] [blame] | 5424 | ** If the table or index is empty, jump immediately to P2. |
| 5425 | ** If the table or index is not empty, fall through to the following |
| 5426 | ** instruction. |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 5427 | ** |
| 5428 | ** This opcode leaves the cursor configured to move in forward order, |
drh | 4ed2fb9 | 2014-08-14 13:06:25 +0000 | [diff] [blame] | 5429 | ** from the beginning toward the end. In other words, the cursor is |
drh | 5dad9a3 | 2014-07-25 18:37:42 +0000 | [diff] [blame] | 5430 | ** configured to use Next, not Prev. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5431 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 5432 | case OP_Rewind: { /* jump */ |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 5433 | VdbeCursor *pC; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5434 | BtCursor *pCrsr; |
drh | f4dada7 | 2004-05-11 09:57:35 +0000 | [diff] [blame] | 5435 | int res; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5436 | |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 5437 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
drh | 038ebf6 | 2019-03-29 15:21:22 +0000 | [diff] [blame] | 5438 | assert( pOp->p5==0 ); |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 5439 | pC = p->apCsr[pOp->p1]; |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 5440 | assert( pC!=0 ); |
drh | 14da87f | 2013-11-20 21:51:33 +0000 | [diff] [blame] | 5441 | assert( isSorter(pC)==(pOp->opcode==OP_SorterSort) ); |
dan | 2411dea | 2010-07-03 05:56:09 +0000 | [diff] [blame] | 5442 | res = 1; |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 5443 | #ifdef SQLITE_DEBUG |
| 5444 | pC->seekOp = OP_Rewind; |
| 5445 | #endif |
dan | 689ab89 | 2011-08-12 15:02:00 +0000 | [diff] [blame] | 5446 | if( isSorter(pC) ){ |
drh | 958d261 | 2014-04-18 13:40:07 +0000 | [diff] [blame] | 5447 | rc = sqlite3VdbeSorterRewind(pC, &res); |
dan | a205a48 | 2011-08-27 18:48:57 +0000 | [diff] [blame] | 5448 | }else{ |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 5449 | assert( pC->eCurType==CURTYPE_BTREE ); |
| 5450 | pCrsr = pC->uc.pCursor; |
dan | a205a48 | 2011-08-27 18:48:57 +0000 | [diff] [blame] | 5451 | assert( pCrsr ); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 5452 | rc = sqlite3BtreeFirst(pCrsr, &res); |
drh | a11846b | 2004-01-07 18:52:56 +0000 | [diff] [blame] | 5453 | pC->deferredMoveto = 0; |
drh | 76873ab | 2006-01-07 18:48:26 +0000 | [diff] [blame] | 5454 | pC->cacheStatus = CACHE_STALE; |
drh | f4dada7 | 2004-05-11 09:57:35 +0000 | [diff] [blame] | 5455 | } |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 5456 | if( rc ) goto abort_due_to_error; |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 5457 | pC->nullRow = (u8)res; |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 5458 | assert( pOp->p2>0 && pOp->p2<p->nOp ); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 5459 | VdbeBranchTaken(res!=0,2); |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 5460 | if( res ) goto jump_to_p2; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5461 | break; |
| 5462 | } |
| 5463 | |
drh | 0fd6135 | 2014-02-07 02:29:45 +0000 | [diff] [blame] | 5464 | /* Opcode: Next P1 P2 P3 P4 P5 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5465 | ** |
| 5466 | ** 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] | 5467 | ** table or index. If there are no more key/value pairs then fall through |
| 5468 | ** to the following instruction. But if the cursor advance was successful, |
| 5469 | ** jump immediately to P2. |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 5470 | ** |
drh | 5dad9a3 | 2014-07-25 18:37:42 +0000 | [diff] [blame] | 5471 | ** The Next opcode is only valid following an SeekGT, SeekGE, or |
| 5472 | ** OP_Rewind opcode used to position the cursor. Next is not allowed |
| 5473 | ** to follow SeekLT, SeekLE, or OP_Last. |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 5474 | ** |
drh | f93cd94 | 2013-11-21 03:12:25 +0000 | [diff] [blame] | 5475 | ** The P1 cursor must be for a real table, not a pseudo-table. P1 must have |
| 5476 | ** been opened prior to this opcode or the program will segfault. |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 5477 | ** |
drh | e39a732 | 2014-02-03 14:04:11 +0000 | [diff] [blame] | 5478 | ** The P3 value is a hint to the btree implementation. If P3==1, that |
| 5479 | ** means P1 is an SQL index and that this instruction could have been |
| 5480 | ** omitted if that index had been unique. P3 is usually 0. P3 is |
| 5481 | ** always either 0 or 1. |
| 5482 | ** |
dan | a205a48 | 2011-08-27 18:48:57 +0000 | [diff] [blame] | 5483 | ** P4 is always of type P4_ADVANCE. The function pointer points to |
| 5484 | ** sqlite3BtreeNext(). |
| 5485 | ** |
drh | afc266a | 2010-03-31 17:47:44 +0000 | [diff] [blame] | 5486 | ** If P5 is positive and the jump is taken, then event counter |
| 5487 | ** number P5-1 in the prepared statement is incremented. |
| 5488 | ** |
drh | f1949b6 | 2018-06-07 17:32:59 +0000 | [diff] [blame] | 5489 | ** See also: Prev |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 5490 | */ |
drh | 0fd6135 | 2014-02-07 02:29:45 +0000 | [diff] [blame] | 5491 | /* Opcode: Prev P1 P2 P3 P4 P5 |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 5492 | ** |
| 5493 | ** Back up cursor P1 so that it points to the previous key/data pair in its |
| 5494 | ** table or index. If there is no previous key/value pairs then fall through |
| 5495 | ** to the following instruction. But if the cursor backup was successful, |
| 5496 | ** jump immediately to P2. |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 5497 | ** |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 5498 | ** |
drh | 5dad9a3 | 2014-07-25 18:37:42 +0000 | [diff] [blame] | 5499 | ** The Prev opcode is only valid following an SeekLT, SeekLE, or |
| 5500 | ** OP_Last opcode used to position the cursor. Prev is not allowed |
| 5501 | ** to follow SeekGT, SeekGE, or OP_Rewind. |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 5502 | ** |
drh | f93cd94 | 2013-11-21 03:12:25 +0000 | [diff] [blame] | 5503 | ** The P1 cursor must be for a real table, not a pseudo-table. If P1 is |
| 5504 | ** not open then the behavior is undefined. |
drh | afc266a | 2010-03-31 17:47:44 +0000 | [diff] [blame] | 5505 | ** |
drh | e39a732 | 2014-02-03 14:04:11 +0000 | [diff] [blame] | 5506 | ** The P3 value is a hint to the btree implementation. If P3==1, that |
| 5507 | ** means P1 is an SQL index and that this instruction could have been |
| 5508 | ** omitted if that index had been unique. P3 is usually 0. P3 is |
| 5509 | ** always either 0 or 1. |
| 5510 | ** |
dan | a205a48 | 2011-08-27 18:48:57 +0000 | [diff] [blame] | 5511 | ** P4 is always of type P4_ADVANCE. The function pointer points to |
| 5512 | ** sqlite3BtreePrevious(). |
| 5513 | ** |
drh | afc266a | 2010-03-31 17:47:44 +0000 | [diff] [blame] | 5514 | ** If P5 is positive and the jump is taken, then event counter |
| 5515 | ** number P5-1 in the prepared statement is incremented. |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 5516 | */ |
drh | 6bd4dc6 | 2016-12-23 16:05:22 +0000 | [diff] [blame] | 5517 | /* Opcode: SorterNext P1 P2 * * P5 |
| 5518 | ** |
| 5519 | ** This opcode works just like OP_Next except that P1 must be a |
| 5520 | ** sorter object for which the OP_SorterSort opcode has been |
| 5521 | ** invoked. This opcode advances the cursor to the next sorted |
| 5522 | ** record, or jumps to P2 if there are no more sorted records. |
| 5523 | */ |
drh | f93cd94 | 2013-11-21 03:12:25 +0000 | [diff] [blame] | 5524 | case OP_SorterNext: { /* jump */ |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 5525 | VdbeCursor *pC; |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 5526 | |
drh | f93cd94 | 2013-11-21 03:12:25 +0000 | [diff] [blame] | 5527 | pC = p->apCsr[pOp->p1]; |
| 5528 | assert( isSorter(pC) ); |
drh | 2ab792e | 2017-05-30 18:34:07 +0000 | [diff] [blame] | 5529 | rc = sqlite3VdbeSorterNext(db, pC); |
drh | f93cd94 | 2013-11-21 03:12:25 +0000 | [diff] [blame] | 5530 | goto next_tail; |
drh | f93cd94 | 2013-11-21 03:12:25 +0000 | [diff] [blame] | 5531 | case OP_Prev: /* jump */ |
| 5532 | case OP_Next: /* jump */ |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 5533 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
drh | 9b47ee3 | 2013-08-20 03:13:51 +0000 | [diff] [blame] | 5534 | assert( pOp->p5<ArraySize(p->aCounter) ); |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame] | 5535 | pC = p->apCsr[pOp->p1]; |
drh | f93cd94 | 2013-11-21 03:12:25 +0000 | [diff] [blame] | 5536 | assert( pC!=0 ); |
| 5537 | assert( pC->deferredMoveto==0 ); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 5538 | assert( pC->eCurType==CURTYPE_BTREE ); |
drh | f93cd94 | 2013-11-21 03:12:25 +0000 | [diff] [blame] | 5539 | assert( pOp->opcode!=OP_Next || pOp->p4.xAdvance==sqlite3BtreeNext ); |
| 5540 | assert( pOp->opcode!=OP_Prev || pOp->p4.xAdvance==sqlite3BtreePrevious ); |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 5541 | |
drh | cf025a8 | 2018-06-07 18:01:21 +0000 | [diff] [blame] | 5542 | /* The Next opcode is only used after SeekGT, SeekGE, Rewind, and Found. |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 5543 | ** The Prev opcode is only used after SeekLT, SeekLE, and Last. */ |
drh | f1949b6 | 2018-06-07 17:32:59 +0000 | [diff] [blame] | 5544 | assert( pOp->opcode!=OP_Next |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 5545 | || pC->seekOp==OP_SeekGT || pC->seekOp==OP_SeekGE |
drh | 790b37a | 2019-08-27 17:01:07 +0000 | [diff] [blame] | 5546 | || pC->seekOp==OP_Rewind || pC->seekOp==OP_Found |
| 5547 | || pC->seekOp==OP_NullRow|| pC->seekOp==OP_SeekRowid |
| 5548 | || pC->seekOp==OP_IfNoHope); |
drh | f1949b6 | 2018-06-07 17:32:59 +0000 | [diff] [blame] | 5549 | assert( pOp->opcode!=OP_Prev |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 5550 | || pC->seekOp==OP_SeekLT || pC->seekOp==OP_SeekLE |
drh | 790b37a | 2019-08-27 17:01:07 +0000 | [diff] [blame] | 5551 | || pC->seekOp==OP_Last || pC->seekOp==OP_IfNoHope |
drh | cf025a8 | 2018-06-07 18:01:21 +0000 | [diff] [blame] | 5552 | || pC->seekOp==OP_NullRow); |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 5553 | |
drh | 2ab792e | 2017-05-30 18:34:07 +0000 | [diff] [blame] | 5554 | rc = pOp->p4.xAdvance(pC->uc.pCursor, pOp->p3); |
drh | f93cd94 | 2013-11-21 03:12:25 +0000 | [diff] [blame] | 5555 | next_tail: |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 5556 | pC->cacheStatus = CACHE_STALE; |
drh | 2ab792e | 2017-05-30 18:34:07 +0000 | [diff] [blame] | 5557 | VdbeBranchTaken(rc==SQLITE_OK,2); |
| 5558 | if( rc==SQLITE_OK ){ |
drh | f93cd94 | 2013-11-21 03:12:25 +0000 | [diff] [blame] | 5559 | pC->nullRow = 0; |
drh | 9b47ee3 | 2013-08-20 03:13:51 +0000 | [diff] [blame] | 5560 | p->aCounter[pOp->p5]++; |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 5561 | #ifdef SQLITE_TEST |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 5562 | sqlite3_search_count++; |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 5563 | #endif |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 5564 | goto jump_to_p2_and_check_for_interrupt; |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 5565 | } |
drh | 2ab792e | 2017-05-30 18:34:07 +0000 | [diff] [blame] | 5566 | if( rc!=SQLITE_DONE ) goto abort_due_to_error; |
| 5567 | rc = SQLITE_OK; |
| 5568 | pC->nullRow = 1; |
drh | 49afe3a | 2013-07-10 03:05:14 +0000 | [diff] [blame] | 5569 | goto check_for_interrupt; |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 5570 | } |
| 5571 | |
drh | 9b4eaeb | 2016-11-09 00:10:33 +0000 | [diff] [blame] | 5572 | /* Opcode: IdxInsert P1 P2 P3 P4 P5 |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 5573 | ** Synopsis: key=r[P2] |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5574 | ** |
drh | ef8662b | 2011-06-20 21:47:58 +0000 | [diff] [blame] | 5575 | ** Register P2 holds an SQL index key made using the |
drh | 9437bd2 | 2009-02-01 00:29:56 +0000 | [diff] [blame] | 5576 | ** MakeRecord instructions. This opcode writes that key |
drh | ee32e0a | 2006-01-10 19:45:49 +0000 | [diff] [blame] | 5577 | ** into the index P1. Data for the entry is nil. |
drh | 717e640 | 2001-09-27 03:22:32 +0000 | [diff] [blame] | 5578 | ** |
drh | fb8c56f | 2016-11-09 01:19:25 +0000 | [diff] [blame] | 5579 | ** 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] | 5580 | ** key of reg(P2). In that case, P3 is the index of the first register |
| 5581 | ** for the unpacked key. The availability of the unpacked key can sometimes |
| 5582 | ** be an optimization. |
| 5583 | ** |
| 5584 | ** If P5 has the OPFLAG_APPEND bit set, that is a hint to the b-tree layer |
| 5585 | ** that this insert is likely to be an append. |
drh | e4d9081 | 2007-03-29 05:51:49 +0000 | [diff] [blame] | 5586 | ** |
mistachkin | 21a919f | 2014-02-07 03:28:02 +0000 | [diff] [blame] | 5587 | ** If P5 has the OPFLAG_NCHANGE bit set, then the change counter is |
| 5588 | ** incremented by this instruction. If the OPFLAG_NCHANGE bit is clear, |
| 5589 | ** then the change counter is unchanged. |
drh | 0fd6135 | 2014-02-07 02:29:45 +0000 | [diff] [blame] | 5590 | ** |
drh | eaf6ae2 | 2016-11-09 20:14:34 +0000 | [diff] [blame] | 5591 | ** If the OPFLAG_USESEEKRESULT flag of P5 is set, the implementation might |
| 5592 | ** run faster by avoiding an unnecessary seek on cursor P1. However, |
| 5593 | ** the OPFLAG_USESEEKRESULT flag must only be set if there have been no prior |
| 5594 | ** seeks on the cursor or if the most recent seek used a key equivalent |
| 5595 | ** to P2. |
drh | 0fd6135 | 2014-02-07 02:29:45 +0000 | [diff] [blame] | 5596 | ** |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 5597 | ** This instruction only works for indices. The equivalent instruction |
| 5598 | ** for tables is OP_Insert. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5599 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 5600 | case OP_IdxInsert: { /* in2 */ |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 5601 | VdbeCursor *pC; |
drh | 8eeb446 | 2016-05-21 20:03:42 +0000 | [diff] [blame] | 5602 | BtreePayload x; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 5603 | |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 5604 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 5605 | pC = p->apCsr[pOp->p1]; |
drh | 4031baf | 2018-05-28 17:31:20 +0000 | [diff] [blame] | 5606 | sqlite3VdbeIncrWriteCounter(p, pC); |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 5607 | assert( pC!=0 ); |
drh | c879c4e | 2020-02-06 13:57:08 +0000 | [diff] [blame] | 5608 | assert( !isSorter(pC) ); |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 5609 | pIn2 = &aMem[pOp->p2]; |
drh | aa9b896 | 2008-01-08 02:57:55 +0000 | [diff] [blame] | 5610 | assert( pIn2->flags & MEM_Blob ); |
drh | 6546af1 | 2013-11-04 15:23:25 +0000 | [diff] [blame] | 5611 | if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++; |
drh | c879c4e | 2020-02-06 13:57:08 +0000 | [diff] [blame] | 5612 | assert( pC->eCurType==CURTYPE_BTREE ); |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 5613 | assert( pC->isTable==0 ); |
| 5614 | rc = ExpandBlob(pIn2); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 5615 | if( rc ) goto abort_due_to_error; |
drh | c879c4e | 2020-02-06 13:57:08 +0000 | [diff] [blame] | 5616 | x.nKey = pIn2->n; |
| 5617 | x.pKey = pIn2->z; |
| 5618 | x.aMem = aMem + pOp->p3; |
| 5619 | x.nMem = (u16)pOp->p4.i; |
| 5620 | rc = sqlite3BtreeInsert(pC->uc.pCursor, &x, |
| 5621 | (pOp->p5 & (OPFLAG_APPEND|OPFLAG_SAVEPOSITION)), |
| 5622 | ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0) |
| 5623 | ); |
| 5624 | assert( pC->deferredMoveto==0 ); |
| 5625 | pC->cacheStatus = CACHE_STALE; |
| 5626 | if( rc) goto abort_due_to_error; |
| 5627 | break; |
| 5628 | } |
| 5629 | |
| 5630 | /* Opcode: SorterInsert P1 P2 * * * |
| 5631 | ** Synopsis: key=r[P2] |
| 5632 | ** |
| 5633 | ** Register P2 holds an SQL index key made using the |
| 5634 | ** MakeRecord instructions. This opcode writes that key |
| 5635 | ** into the sorter P1. Data for the entry is nil. |
| 5636 | */ |
| 5637 | case OP_SorterInsert: { /* in2 */ |
| 5638 | VdbeCursor *pC; |
| 5639 | |
| 5640 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 5641 | pC = p->apCsr[pOp->p1]; |
| 5642 | sqlite3VdbeIncrWriteCounter(p, pC); |
| 5643 | assert( pC!=0 ); |
| 5644 | assert( isSorter(pC) ); |
| 5645 | pIn2 = &aMem[pOp->p2]; |
| 5646 | assert( pIn2->flags & MEM_Blob ); |
| 5647 | assert( pC->isTable==0 ); |
| 5648 | rc = ExpandBlob(pIn2); |
| 5649 | if( rc ) goto abort_due_to_error; |
| 5650 | rc = sqlite3VdbeSorterWrite(pC, pIn2); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 5651 | if( rc) goto abort_due_to_error; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5652 | break; |
| 5653 | } |
| 5654 | |
drh | 85bd353 | 2020-05-05 18:42:49 +0000 | [diff] [blame] | 5655 | /* Opcode: IdxDelete P1 P2 P3 * P5 |
drh | f63552b | 2013-10-30 00:25:03 +0000 | [diff] [blame] | 5656 | ** Synopsis: key=r[P2@P3] |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5657 | ** |
drh | e14006d | 2008-03-25 17:23:32 +0000 | [diff] [blame] | 5658 | ** The content of P3 registers starting at register P2 form |
| 5659 | ** an unpacked index key. This opcode removes that entry from the |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 5660 | ** index opened by cursor P1. |
drh | 85bd353 | 2020-05-05 18:42:49 +0000 | [diff] [blame] | 5661 | ** |
| 5662 | ** If P5 is not zero, then raise an SQLITE_CORRUPT_INDEX error |
| 5663 | ** if no matching index entry is found. This happens when running |
| 5664 | ** an UPDATE or DELETE statement and the index entry to be updated |
| 5665 | ** or deleted is not found. For some uses of IdxDelete |
| 5666 | ** (example: the EXCEPT operator) it does not matter that no matching |
| 5667 | ** entry is found. For those cases, P5 is zero. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5668 | */ |
drh | e14006d | 2008-03-25 17:23:32 +0000 | [diff] [blame] | 5669 | case OP_IdxDelete: { |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 5670 | VdbeCursor *pC; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5671 | BtCursor *pCrsr; |
drh | 9a65f2c | 2009-06-22 19:05:40 +0000 | [diff] [blame] | 5672 | int res; |
| 5673 | UnpackedRecord r; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 5674 | |
drh | e14006d | 2008-03-25 17:23:32 +0000 | [diff] [blame] | 5675 | assert( pOp->p3>0 ); |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 5676 | 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] | 5677 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 5678 | pC = p->apCsr[pOp->p1]; |
| 5679 | assert( pC!=0 ); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 5680 | assert( pC->eCurType==CURTYPE_BTREE ); |
drh | 4031baf | 2018-05-28 17:31:20 +0000 | [diff] [blame] | 5681 | sqlite3VdbeIncrWriteCounter(p, pC); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 5682 | pCrsr = pC->uc.pCursor; |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 5683 | assert( pCrsr!=0 ); |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 5684 | r.pKeyInfo = pC->pKeyInfo; |
| 5685 | r.nField = (u16)pOp->p3; |
dan | 1fed5da | 2014-02-25 21:01:25 +0000 | [diff] [blame] | 5686 | r.default_rc = 0; |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 5687 | r.aMem = &aMem[pOp->p2]; |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 5688 | rc = sqlite3BtreeMovetoUnpacked(pCrsr, &r, 0, 0, &res); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 5689 | if( rc ) goto abort_due_to_error; |
| 5690 | if( res==0 ){ |
dan | e61bbf4 | 2016-01-28 17:06:17 +0000 | [diff] [blame] | 5691 | rc = sqlite3BtreeDelete(pCrsr, BTREE_AUXDELETE); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 5692 | if( rc ) goto abort_due_to_error; |
drh | 85bd353 | 2020-05-05 18:42:49 +0000 | [diff] [blame] | 5693 | }else if( pOp->p5 ){ |
| 5694 | rc = SQLITE_CORRUPT_INDEX; |
| 5695 | goto abort_due_to_error; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5696 | } |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 5697 | assert( pC->deferredMoveto==0 ); |
| 5698 | pC->cacheStatus = CACHE_STALE; |
dan | 3b908d4 | 2016-11-08 19:22:32 +0000 | [diff] [blame] | 5699 | pC->seekResult = 0; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5700 | break; |
| 5701 | } |
| 5702 | |
drh | 170ad68 | 2017-06-02 15:44:22 +0000 | [diff] [blame] | 5703 | /* Opcode: DeferredSeek P1 * P3 P4 * |
| 5704 | ** Synopsis: Move P3 to P1.rowid if needed |
drh | 784c1b9 | 2016-01-30 16:59:56 +0000 | [diff] [blame] | 5705 | ** |
| 5706 | ** P1 is an open index cursor and P3 is a cursor on the corresponding |
| 5707 | ** table. This opcode does a deferred seek of the P3 table cursor |
| 5708 | ** to the row that corresponds to the current row of P1. |
| 5709 | ** |
| 5710 | ** This is a deferred seek. Nothing actually happens until |
| 5711 | ** the cursor is used to read a record. That way, if no reads |
| 5712 | ** occur, no unnecessary I/O happens. |
| 5713 | ** |
| 5714 | ** P4 may be an array of integers (type P4_INTARRAY) containing |
drh | 19d720d | 2016-02-03 19:52:06 +0000 | [diff] [blame] | 5715 | ** one entry for each column in the P3 table. If array entry a(i) |
| 5716 | ** is non-zero, then reading column a(i)-1 from cursor P3 is |
drh | 784c1b9 | 2016-01-30 16:59:56 +0000 | [diff] [blame] | 5717 | ** equivalent to performing the deferred seek and then reading column i |
| 5718 | ** from P1. This information is stored in P3 and used to redirect |
| 5719 | ** reads against P3 over to P1, thus possibly avoiding the need to |
| 5720 | ** seek and read cursor P3. |
| 5721 | */ |
drh | 2133d82 | 2008-01-03 18:44:59 +0000 | [diff] [blame] | 5722 | /* Opcode: IdxRowid P1 P2 * * * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 5723 | ** Synopsis: r[P2]=rowid |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 5724 | ** |
drh | 2133d82 | 2008-01-03 18:44:59 +0000 | [diff] [blame] | 5725 | ** 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] | 5726 | ** the end of the index key pointed to by cursor P1. This integer should be |
| 5727 | ** the rowid of the table entry to which this index entry points. |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 5728 | ** |
drh | 9437bd2 | 2009-02-01 00:29:56 +0000 | [diff] [blame] | 5729 | ** See also: Rowid, MakeRecord. |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 5730 | */ |
drh | 170ad68 | 2017-06-02 15:44:22 +0000 | [diff] [blame] | 5731 | case OP_DeferredSeek: |
| 5732 | case OP_IdxRowid: { /* out2 */ |
| 5733 | VdbeCursor *pC; /* The P1 index cursor */ |
| 5734 | VdbeCursor *pTabCur; /* The P2 table cursor (OP_DeferredSeek only) */ |
| 5735 | i64 rowid; /* Rowid that P1 current points to */ |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 5736 | |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 5737 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 5738 | pC = p->apCsr[pOp->p1]; |
| 5739 | assert( pC!=0 ); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 5740 | assert( pC->eCurType==CURTYPE_BTREE ); |
drh | 784c1b9 | 2016-01-30 16:59:56 +0000 | [diff] [blame] | 5741 | assert( pC->uc.pCursor!=0 ); |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 5742 | assert( pC->isTable==0 ); |
drh | c22284f | 2014-10-13 16:02:20 +0000 | [diff] [blame] | 5743 | assert( pC->deferredMoveto==0 ); |
drh | 784c1b9 | 2016-01-30 16:59:56 +0000 | [diff] [blame] | 5744 | assert( !pC->nullRow || pOp->opcode==OP_IdxRowid ); |
| 5745 | |
| 5746 | /* The IdxRowid and Seek opcodes are combined because of the commonality |
| 5747 | ** of sqlite3VdbeCursorRestore() and sqlite3VdbeIdxRowid(). */ |
| 5748 | rc = sqlite3VdbeCursorRestore(pC); |
drh | c22284f | 2014-10-13 16:02:20 +0000 | [diff] [blame] | 5749 | |
| 5750 | /* sqlite3VbeCursorRestore() can only fail if the record has been deleted |
drh | 784c1b9 | 2016-01-30 16:59:56 +0000 | [diff] [blame] | 5751 | ** out from under the cursor. That will never happens for an IdxRowid |
| 5752 | ** or Seek opcode */ |
drh | c22284f | 2014-10-13 16:02:20 +0000 | [diff] [blame] | 5753 | if( NEVER(rc!=SQLITE_OK) ) goto abort_due_to_error; |
| 5754 | |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 5755 | if( !pC->nullRow ){ |
drh | 2dc0648 | 2013-12-11 00:59:10 +0000 | [diff] [blame] | 5756 | rowid = 0; /* Not needed. Only used to silence a warning. */ |
drh | 784c1b9 | 2016-01-30 16:59:56 +0000 | [diff] [blame] | 5757 | rc = sqlite3VdbeIdxRowid(db, pC->uc.pCursor, &rowid); |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 5758 | if( rc!=SQLITE_OK ){ |
| 5759 | goto abort_due_to_error; |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 5760 | } |
drh | 170ad68 | 2017-06-02 15:44:22 +0000 | [diff] [blame] | 5761 | if( pOp->opcode==OP_DeferredSeek ){ |
drh | 784c1b9 | 2016-01-30 16:59:56 +0000 | [diff] [blame] | 5762 | assert( pOp->p3>=0 && pOp->p3<p->nCursor ); |
| 5763 | pTabCur = p->apCsr[pOp->p3]; |
| 5764 | assert( pTabCur!=0 ); |
| 5765 | assert( pTabCur->eCurType==CURTYPE_BTREE ); |
| 5766 | assert( pTabCur->uc.pCursor!=0 ); |
| 5767 | assert( pTabCur->isTable ); |
| 5768 | pTabCur->nullRow = 0; |
| 5769 | pTabCur->movetoTarget = rowid; |
| 5770 | pTabCur->deferredMoveto = 1; |
| 5771 | assert( pOp->p4type==P4_INTARRAY || pOp->p4.ai==0 ); |
| 5772 | pTabCur->aAltMap = pOp->p4.ai; |
| 5773 | pTabCur->pAltCursor = pC; |
| 5774 | }else{ |
| 5775 | pOut = out2Prerelease(p, pOp); |
| 5776 | pOut->u.i = rowid; |
drh | 784c1b9 | 2016-01-30 16:59:56 +0000 | [diff] [blame] | 5777 | } |
| 5778 | }else{ |
| 5779 | assert( pOp->opcode==OP_IdxRowid ); |
| 5780 | sqlite3VdbeMemSetNull(&aMem[pOp->p2]); |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 5781 | } |
| 5782 | break; |
| 5783 | } |
| 5784 | |
drh | be3da24 | 2019-12-29 00:52:41 +0000 | [diff] [blame] | 5785 | /* Opcode: FinishSeek P1 * * * * |
| 5786 | ** |
| 5787 | ** If cursor P1 was previously moved via OP_DeferredSeek, complete that |
| 5788 | ** seek operation now, without further delay. If the cursor seek has |
| 5789 | ** already occurred, this instruction is a no-op. |
| 5790 | */ |
| 5791 | case OP_FinishSeek: { |
| 5792 | VdbeCursor *pC; /* The P1 index cursor */ |
| 5793 | |
| 5794 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 5795 | pC = p->apCsr[pOp->p1]; |
| 5796 | if( pC->deferredMoveto ){ |
| 5797 | rc = sqlite3VdbeFinishMoveto(pC); |
| 5798 | if( rc ) goto abort_due_to_error; |
| 5799 | } |
| 5800 | break; |
| 5801 | } |
| 5802 | |
danielk1977 | 61dd583 | 2008-04-18 11:31:12 +0000 | [diff] [blame] | 5803 | /* Opcode: IdxGE P1 P2 P3 P4 P5 |
drh | f63552b | 2013-10-30 00:25:03 +0000 | [diff] [blame] | 5804 | ** Synopsis: key=r[P3@P4] |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 5805 | ** |
danielk1977 | 61dd583 | 2008-04-18 11:31:12 +0000 | [diff] [blame] | 5806 | ** The P4 register values beginning with P3 form an unpacked index |
drh | 4a1d365 | 2014-02-14 15:13:36 +0000 | [diff] [blame] | 5807 | ** key that omits the PRIMARY KEY. Compare this key value against the index |
| 5808 | ** that P1 is currently pointing to, ignoring the PRIMARY KEY or ROWID |
| 5809 | ** fields at the end. |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 5810 | ** |
danielk1977 | 61dd583 | 2008-04-18 11:31:12 +0000 | [diff] [blame] | 5811 | ** If the P1 index entry is greater than or equal to the key value |
| 5812 | ** then jump to P2. Otherwise fall through to the next instruction. |
drh | 4a1d365 | 2014-02-14 15:13:36 +0000 | [diff] [blame] | 5813 | */ |
| 5814 | /* Opcode: IdxGT P1 P2 P3 P4 P5 |
| 5815 | ** Synopsis: key=r[P3@P4] |
drh | 772ae62 | 2004-05-19 13:13:08 +0000 | [diff] [blame] | 5816 | ** |
drh | 4a1d365 | 2014-02-14 15:13:36 +0000 | [diff] [blame] | 5817 | ** The P4 register values beginning with P3 form an unpacked index |
| 5818 | ** key that omits the PRIMARY KEY. Compare this key value against the index |
| 5819 | ** that P1 is currently pointing to, ignoring the PRIMARY KEY or ROWID |
| 5820 | ** fields at the end. |
| 5821 | ** |
| 5822 | ** If the P1 index entry is greater than the key value |
| 5823 | ** then jump to P2. Otherwise fall through to the next instruction. |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 5824 | */ |
drh | 3bb9b93 | 2010-08-06 02:10:00 +0000 | [diff] [blame] | 5825 | /* Opcode: IdxLT P1 P2 P3 P4 P5 |
drh | f63552b | 2013-10-30 00:25:03 +0000 | [diff] [blame] | 5826 | ** Synopsis: key=r[P3@P4] |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 5827 | ** |
danielk1977 | 61dd583 | 2008-04-18 11:31:12 +0000 | [diff] [blame] | 5828 | ** The P4 register values beginning with P3 form an unpacked index |
drh | 4a1d365 | 2014-02-14 15:13:36 +0000 | [diff] [blame] | 5829 | ** key that omits the PRIMARY KEY or ROWID. Compare this key value against |
| 5830 | ** the index that P1 is currently pointing to, ignoring the PRIMARY KEY or |
| 5831 | ** ROWID on the P1 index. |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 5832 | ** |
danielk1977 | 61dd583 | 2008-04-18 11:31:12 +0000 | [diff] [blame] | 5833 | ** If the P1 index entry is less than the key value then jump to P2. |
| 5834 | ** Otherwise fall through to the next instruction. |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 5835 | */ |
drh | 4a1d365 | 2014-02-14 15:13:36 +0000 | [diff] [blame] | 5836 | /* Opcode: IdxLE P1 P2 P3 P4 P5 |
| 5837 | ** Synopsis: key=r[P3@P4] |
| 5838 | ** |
| 5839 | ** The P4 register values beginning with P3 form an unpacked index |
| 5840 | ** key that omits the PRIMARY KEY or ROWID. Compare this key value against |
| 5841 | ** the index that P1 is currently pointing to, ignoring the PRIMARY KEY or |
| 5842 | ** ROWID on the P1 index. |
| 5843 | ** |
| 5844 | ** If the P1 index entry is less than or equal to the key value then jump |
| 5845 | ** to P2. Otherwise fall through to the next instruction. |
| 5846 | */ |
| 5847 | case OP_IdxLE: /* jump */ |
| 5848 | case OP_IdxGT: /* jump */ |
drh | 93952eb | 2009-11-13 19:43:43 +0000 | [diff] [blame] | 5849 | case OP_IdxLT: /* jump */ |
drh | 4a1d365 | 2014-02-14 15:13:36 +0000 | [diff] [blame] | 5850 | case OP_IdxGE: { /* jump */ |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 5851 | VdbeCursor *pC; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 5852 | int res; |
| 5853 | UnpackedRecord r; |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 5854 | |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 5855 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 5856 | pC = p->apCsr[pOp->p1]; |
| 5857 | assert( pC!=0 ); |
drh | d4187c7 | 2010-08-30 22:15:45 +0000 | [diff] [blame] | 5858 | assert( pC->isOrdered ); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 5859 | assert( pC->eCurType==CURTYPE_BTREE ); |
| 5860 | assert( pC->uc.pCursor!=0); |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 5861 | assert( pC->deferredMoveto==0 ); |
| 5862 | assert( pOp->p5==0 || pOp->p5==1 ); |
| 5863 | assert( pOp->p4type==P4_INT32 ); |
| 5864 | r.pKeyInfo = pC->pKeyInfo; |
| 5865 | r.nField = (u16)pOp->p4.i; |
drh | 4a1d365 | 2014-02-14 15:13:36 +0000 | [diff] [blame] | 5866 | if( pOp->opcode<OP_IdxLT ){ |
| 5867 | assert( pOp->opcode==OP_IdxLE || pOp->opcode==OP_IdxGT ); |
dan | 1fed5da | 2014-02-25 21:01:25 +0000 | [diff] [blame] | 5868 | r.default_rc = -1; |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 5869 | }else{ |
drh | 4a1d365 | 2014-02-14 15:13:36 +0000 | [diff] [blame] | 5870 | assert( pOp->opcode==OP_IdxGE || pOp->opcode==OP_IdxLT ); |
dan | 1fed5da | 2014-02-25 21:01:25 +0000 | [diff] [blame] | 5871 | r.default_rc = 0; |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 5872 | } |
| 5873 | r.aMem = &aMem[pOp->p3]; |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 5874 | #ifdef SQLITE_DEBUG |
drh | 5eae974 | 2018-08-03 13:56:26 +0000 | [diff] [blame] | 5875 | { |
| 5876 | int i; |
| 5877 | for(i=0; i<r.nField; i++){ |
| 5878 | assert( memIsValid(&r.aMem[i]) ); |
| 5879 | REGISTER_TRACE(pOp->p3+i, &aMem[pOp->p3+i]); |
| 5880 | } |
| 5881 | } |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 5882 | #endif |
drh | 2dc0648 | 2013-12-11 00:59:10 +0000 | [diff] [blame] | 5883 | res = 0; /* Not needed. Only used to silence a warning. */ |
drh | d3b7420 | 2014-09-17 16:41:15 +0000 | [diff] [blame] | 5884 | rc = sqlite3VdbeIdxKeyCompare(db, pC, &r, &res); |
drh | 4a1d365 | 2014-02-14 15:13:36 +0000 | [diff] [blame] | 5885 | assert( (OP_IdxLE&1)==(OP_IdxLT&1) && (OP_IdxGE&1)==(OP_IdxGT&1) ); |
| 5886 | if( (pOp->opcode&1)==(OP_IdxLT&1) ){ |
| 5887 | assert( pOp->opcode==OP_IdxLE || pOp->opcode==OP_IdxLT ); |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 5888 | res = -res; |
| 5889 | }else{ |
drh | 4a1d365 | 2014-02-14 15:13:36 +0000 | [diff] [blame] | 5890 | assert( pOp->opcode==OP_IdxGE || pOp->opcode==OP_IdxGT ); |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 5891 | res++; |
| 5892 | } |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 5893 | VdbeBranchTaken(res>0,2); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 5894 | if( rc ) goto abort_due_to_error; |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 5895 | if( res>0 ) goto jump_to_p2; |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 5896 | break; |
| 5897 | } |
| 5898 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 5899 | /* Opcode: Destroy P1 P2 P3 * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5900 | ** |
| 5901 | ** Delete an entire database table or index whose root page in the database |
| 5902 | ** file is given by P1. |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 5903 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 5904 | ** The table being destroyed is in the main database file if P3==0. If |
| 5905 | ** 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] | 5906 | ** that is used to store tables create using CREATE TEMPORARY TABLE. |
| 5907 | ** |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 5908 | ** If AUTOVACUUM is enabled then it is possible that another root page |
| 5909 | ** might be moved into the newly deleted root page in order to keep all |
| 5910 | ** root pages contiguous at the beginning of the database. The former |
| 5911 | ** value of the root page that moved - its value before the move occurred - |
dan | a34adaf | 2017-04-08 14:11:47 +0000 | [diff] [blame] | 5912 | ** is stored in register P2. If no page movement was required (because the |
| 5913 | ** table being dropped was already the last one in the database) then a |
| 5914 | ** zero is stored in register P2. If AUTOVACUUM is disabled then a zero |
| 5915 | ** is stored in register P2. |
| 5916 | ** |
| 5917 | ** This opcode throws an error if there are any active reader VMs when |
| 5918 | ** it is invoked. This is done to avoid the difficulty associated with |
| 5919 | ** updating existing cursors when a root page is moved in an AUTOVACUUM |
| 5920 | ** database. This error is thrown even if the database is not an AUTOVACUUM |
| 5921 | ** db in order to avoid introducing an incompatibility between autovacuum |
| 5922 | ** and non-autovacuum modes. |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 5923 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 5924 | ** See also: Clear |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5925 | */ |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 5926 | case OP_Destroy: { /* out2 */ |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 5927 | int iMoved; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 5928 | int iDb; |
drh | 3a94987 | 2012-09-18 13:20:13 +0000 | [diff] [blame] | 5929 | |
drh | 4031baf | 2018-05-28 17:31:20 +0000 | [diff] [blame] | 5930 | sqlite3VdbeIncrWriteCounter(p, 0); |
drh | 9e92a47 | 2013-06-27 17:40:30 +0000 | [diff] [blame] | 5931 | assert( p->readOnly==0 ); |
drh | 055f298 | 2016-01-15 15:06:41 +0000 | [diff] [blame] | 5932 | assert( pOp->p1>1 ); |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 5933 | pOut = out2Prerelease(p, pOp); |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 5934 | pOut->flags = MEM_Null; |
drh | 086723a | 2015-03-24 12:51:52 +0000 | [diff] [blame] | 5935 | if( db->nVdbeRead > db->nVDestroy+1 ){ |
danielk1977 | e6efa74 | 2004-11-10 11:55:10 +0000 | [diff] [blame] | 5936 | rc = SQLITE_LOCKED; |
drh | 77658e2 | 2007-12-04 16:54:52 +0000 | [diff] [blame] | 5937 | p->errorAction = OE_Abort; |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 5938 | goto abort_due_to_error; |
danielk1977 | e6efa74 | 2004-11-10 11:55:10 +0000 | [diff] [blame] | 5939 | }else{ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 5940 | iDb = pOp->p3; |
drh | a7ab6d8 | 2014-07-21 15:44:39 +0000 | [diff] [blame] | 5941 | assert( DbMaskTest(p->btreeMask, iDb) ); |
drh | 2dc0648 | 2013-12-11 00:59:10 +0000 | [diff] [blame] | 5942 | iMoved = 0; /* Not needed. Only to silence a warning. */ |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 5943 | rc = sqlite3BtreeDropTable(db->aDb[iDb].pBt, pOp->p1, &iMoved); |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 5944 | pOut->flags = MEM_Int; |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 5945 | pOut->u.i = iMoved; |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 5946 | if( rc ) goto abort_due_to_error; |
drh | 3765df4 | 2006-06-28 18:18:09 +0000 | [diff] [blame] | 5947 | #ifndef SQLITE_OMIT_AUTOVACUUM |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 5948 | if( iMoved!=0 ){ |
drh | cdf011d | 2011-04-04 21:25:28 +0000 | [diff] [blame] | 5949 | sqlite3RootPageMoved(db, iDb, iMoved, pOp->p1); |
| 5950 | /* All OP_Destroy operations occur on the same btree */ |
| 5951 | assert( resetSchemaOnFault==0 || resetSchemaOnFault==iDb+1 ); |
| 5952 | resetSchemaOnFault = iDb+1; |
danielk1977 | e6efa74 | 2004-11-10 11:55:10 +0000 | [diff] [blame] | 5953 | } |
drh | 3765df4 | 2006-06-28 18:18:09 +0000 | [diff] [blame] | 5954 | #endif |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 5955 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5956 | break; |
| 5957 | } |
| 5958 | |
danielk1977 | c7af484 | 2008-10-27 13:59:33 +0000 | [diff] [blame] | 5959 | /* Opcode: Clear P1 P2 P3 |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 5960 | ** |
| 5961 | ** Delete all contents of the database table or index whose root page |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 5962 | ** in the database file is given by P1. But, unlike Destroy, do not |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 5963 | ** remove the table or index from the database file. |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 5964 | ** |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 5965 | ** The table being clear is in the main database file if P2==0. If |
| 5966 | ** P2==1 then the table to be clear is in the auxiliary database file |
| 5967 | ** that is used to store tables create using CREATE TEMPORARY TABLE. |
| 5968 | ** |
shane | be21779 | 2009-03-05 04:20:31 +0000 | [diff] [blame] | 5969 | ** If the P3 value is non-zero, then the table referred to must be an |
danielk1977 | c7af484 | 2008-10-27 13:59:33 +0000 | [diff] [blame] | 5970 | ** intkey table (an SQL table, not an index). In this case the row change |
| 5971 | ** count is incremented by the number of rows in the table being cleared. |
| 5972 | ** If P3 is greater than zero, then the value stored in register P3 is |
| 5973 | ** also incremented by the number of rows in the table being cleared. |
| 5974 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 5975 | ** See also: Destroy |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 5976 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 5977 | case OP_Clear: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 5978 | int nChange; |
| 5979 | |
drh | 4031baf | 2018-05-28 17:31:20 +0000 | [diff] [blame] | 5980 | sqlite3VdbeIncrWriteCounter(p, 0); |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 5981 | nChange = 0; |
drh | 9e92a47 | 2013-06-27 17:40:30 +0000 | [diff] [blame] | 5982 | assert( p->readOnly==0 ); |
drh | a7ab6d8 | 2014-07-21 15:44:39 +0000 | [diff] [blame] | 5983 | assert( DbMaskTest(p->btreeMask, pOp->p2) ); |
danielk1977 | c7af484 | 2008-10-27 13:59:33 +0000 | [diff] [blame] | 5984 | rc = sqlite3BtreeClearTable( |
| 5985 | db->aDb[pOp->p2].pBt, pOp->p1, (pOp->p3 ? &nChange : 0) |
| 5986 | ); |
| 5987 | if( pOp->p3 ){ |
| 5988 | p->nChange += nChange; |
| 5989 | if( pOp->p3>0 ){ |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 5990 | assert( memIsValid(&aMem[pOp->p3]) ); |
| 5991 | memAboutToChange(p, &aMem[pOp->p3]); |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 5992 | aMem[pOp->p3].u.i += nChange; |
danielk1977 | c7af484 | 2008-10-27 13:59:33 +0000 | [diff] [blame] | 5993 | } |
| 5994 | } |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 5995 | if( rc ) goto abort_due_to_error; |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 5996 | break; |
| 5997 | } |
| 5998 | |
drh | 65ea12c | 2014-03-19 17:41:36 +0000 | [diff] [blame] | 5999 | /* Opcode: ResetSorter P1 * * * * |
drh | 079a307 | 2014-03-19 14:10:55 +0000 | [diff] [blame] | 6000 | ** |
drh | 65ea12c | 2014-03-19 17:41:36 +0000 | [diff] [blame] | 6001 | ** Delete all contents from the ephemeral table or sorter |
| 6002 | ** that is open on cursor P1. |
drh | 079a307 | 2014-03-19 14:10:55 +0000 | [diff] [blame] | 6003 | ** |
drh | 65ea12c | 2014-03-19 17:41:36 +0000 | [diff] [blame] | 6004 | ** This opcode only works for cursors used for sorting and |
| 6005 | ** opened with OP_OpenEphemeral or OP_SorterOpen. |
drh | 079a307 | 2014-03-19 14:10:55 +0000 | [diff] [blame] | 6006 | */ |
drh | 65ea12c | 2014-03-19 17:41:36 +0000 | [diff] [blame] | 6007 | case OP_ResetSorter: { |
drh | 079a307 | 2014-03-19 14:10:55 +0000 | [diff] [blame] | 6008 | VdbeCursor *pC; |
| 6009 | |
| 6010 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 6011 | pC = p->apCsr[pOp->p1]; |
| 6012 | assert( pC!=0 ); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 6013 | if( isSorter(pC) ){ |
| 6014 | sqlite3VdbeSorterReset(db, pC->uc.pSorter); |
drh | 65ea12c | 2014-03-19 17:41:36 +0000 | [diff] [blame] | 6015 | }else{ |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 6016 | assert( pC->eCurType==CURTYPE_BTREE ); |
drh | 65ea12c | 2014-03-19 17:41:36 +0000 | [diff] [blame] | 6017 | assert( pC->isEphemeral ); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 6018 | rc = sqlite3BtreeClearTableOfCursor(pC->uc.pCursor); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 6019 | if( rc ) goto abort_due_to_error; |
drh | 65ea12c | 2014-03-19 17:41:36 +0000 | [diff] [blame] | 6020 | } |
drh | 079a307 | 2014-03-19 14:10:55 +0000 | [diff] [blame] | 6021 | break; |
| 6022 | } |
| 6023 | |
drh | 0f3f766 | 2017-08-18 14:34:28 +0000 | [diff] [blame] | 6024 | /* Opcode: CreateBtree P1 P2 P3 * * |
| 6025 | ** Synopsis: r[P2]=root iDb=P1 flags=P3 |
drh | 5b2fd56 | 2001-09-13 15:21:31 +0000 | [diff] [blame] | 6026 | ** |
drh | 0f3f766 | 2017-08-18 14:34:28 +0000 | [diff] [blame] | 6027 | ** Allocate a new b-tree in the main database file if P1==0 or in the |
| 6028 | ** TEMP database file if P1==1 or in an attached database if |
| 6029 | ** 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] | 6030 | ** it must be 2 (BTREE_BLOBKEY) for an index or WITHOUT ROWID table. |
drh | 0f3f766 | 2017-08-18 14:34:28 +0000 | [diff] [blame] | 6031 | ** 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] | 6032 | */ |
drh | 0f3f766 | 2017-08-18 14:34:28 +0000 | [diff] [blame] | 6033 | case OP_CreateBtree: { /* out2 */ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 6034 | int pgno; |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 6035 | Db *pDb; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 6036 | |
drh | 4031baf | 2018-05-28 17:31:20 +0000 | [diff] [blame] | 6037 | sqlite3VdbeIncrWriteCounter(p, 0); |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 6038 | pOut = out2Prerelease(p, pOp); |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 6039 | pgno = 0; |
drh | 0f3f766 | 2017-08-18 14:34:28 +0000 | [diff] [blame] | 6040 | assert( pOp->p3==BTREE_INTKEY || pOp->p3==BTREE_BLOBKEY ); |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 6041 | assert( pOp->p1>=0 && pOp->p1<db->nDb ); |
drh | a7ab6d8 | 2014-07-21 15:44:39 +0000 | [diff] [blame] | 6042 | assert( DbMaskTest(p->btreeMask, pOp->p1) ); |
drh | 9e92a47 | 2013-06-27 17:40:30 +0000 | [diff] [blame] | 6043 | assert( p->readOnly==0 ); |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 6044 | pDb = &db->aDb[pOp->p1]; |
| 6045 | assert( pDb->pBt!=0 ); |
drh | 0f3f766 | 2017-08-18 14:34:28 +0000 | [diff] [blame] | 6046 | rc = sqlite3BtreeCreateTable(pDb->pBt, &pgno, pOp->p3); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 6047 | if( rc ) goto abort_due_to_error; |
drh | 88a003e | 2008-12-11 16:17:03 +0000 | [diff] [blame] | 6048 | pOut->u.i = pgno; |
drh | 5b2fd56 | 2001-09-13 15:21:31 +0000 | [diff] [blame] | 6049 | break; |
| 6050 | } |
| 6051 | |
drh | 4a54bb5 | 2017-02-18 15:58:52 +0000 | [diff] [blame] | 6052 | /* Opcode: SqlExec * * * P4 * |
| 6053 | ** |
| 6054 | ** Run the SQL statement or statements specified in the P4 string. |
| 6055 | */ |
| 6056 | case OP_SqlExec: { |
drh | 4031baf | 2018-05-28 17:31:20 +0000 | [diff] [blame] | 6057 | sqlite3VdbeIncrWriteCounter(p, 0); |
drh | bce0414 | 2017-02-23 00:58:36 +0000 | [diff] [blame] | 6058 | db->nSqlExec++; |
drh | 4a54bb5 | 2017-02-18 15:58:52 +0000 | [diff] [blame] | 6059 | rc = sqlite3_exec(db, pOp->p4.z, 0, 0, 0); |
drh | bce0414 | 2017-02-23 00:58:36 +0000 | [diff] [blame] | 6060 | db->nSqlExec--; |
drh | 4a54bb5 | 2017-02-18 15:58:52 +0000 | [diff] [blame] | 6061 | if( rc ) goto abort_due_to_error; |
| 6062 | break; |
| 6063 | } |
| 6064 | |
drh | 2264584 | 2011-03-24 01:34:03 +0000 | [diff] [blame] | 6065 | /* Opcode: ParseSchema P1 * * P4 * |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 6066 | ** |
| 6067 | ** Read and parse all entries from the SQLITE_MASTER table of database P1 |
drh | 1595abc | 2018-08-14 19:27:51 +0000 | [diff] [blame] | 6068 | ** that match the WHERE clause P4. If P4 is a NULL pointer, then the |
| 6069 | ** entire schema for P1 is reparsed. |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 6070 | ** |
| 6071 | ** This opcode invokes the parser to create a new virtual machine, |
shane | 21e7feb | 2008-05-30 15:59:49 +0000 | [diff] [blame] | 6072 | ** then runs the new virtual machine. It is thus a re-entrant opcode. |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 6073 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 6074 | case OP_ParseSchema: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 6075 | int iDb; |
| 6076 | const char *zMaster; |
| 6077 | char *zSql; |
| 6078 | InitData initData; |
| 6079 | |
drh | bdaec52 | 2011-04-04 00:14:43 +0000 | [diff] [blame] | 6080 | /* Any prepared statement that invokes this opcode will hold mutexes |
| 6081 | ** on every btree. This is a prerequisite for invoking |
| 6082 | ** sqlite3InitCallback(). |
| 6083 | */ |
| 6084 | #ifdef SQLITE_DEBUG |
| 6085 | for(iDb=0; iDb<db->nDb; iDb++){ |
| 6086 | assert( iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) ); |
| 6087 | } |
| 6088 | #endif |
drh | bdaec52 | 2011-04-04 00:14:43 +0000 | [diff] [blame] | 6089 | |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 6090 | iDb = pOp->p1; |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 6091 | assert( iDb>=0 && iDb<db->nDb ); |
dan | 6c15487 | 2011-04-02 09:44:43 +0000 | [diff] [blame] | 6092 | assert( DbHasProperty(db, iDb, DB_SchemaLoaded) ); |
dan | e325ffe | 2018-08-11 13:40:20 +0000 | [diff] [blame] | 6093 | |
| 6094 | #ifndef SQLITE_OMIT_ALTERTABLE |
| 6095 | if( pOp->p4.z==0 ){ |
| 6096 | sqlite3SchemaClear(db->aDb[iDb].pSchema); |
dan | b0c7920 | 2018-08-11 18:34:25 +0000 | [diff] [blame] | 6097 | db->mDbFlags &= ~DBFLAG_SchemaKnownOk; |
drh | 1595abc | 2018-08-14 19:27:51 +0000 | [diff] [blame] | 6098 | rc = sqlite3InitOne(db, iDb, &p->zErrMsg, INITFLAG_AlterTable); |
dan | e325ffe | 2018-08-11 13:40:20 +0000 | [diff] [blame] | 6099 | db->mDbFlags |= DBFLAG_SchemaChange; |
dan | 0d5fa6b | 2018-08-24 17:55:49 +0000 | [diff] [blame] | 6100 | p->expired = 0; |
dan | e325ffe | 2018-08-11 13:40:20 +0000 | [diff] [blame] | 6101 | }else |
| 6102 | #endif |
drh | 1595abc | 2018-08-14 19:27:51 +0000 | [diff] [blame] | 6103 | { |
drh | e0a04a3 | 2016-12-16 01:00:21 +0000 | [diff] [blame] | 6104 | zMaster = MASTER_NAME; |
danielk1977 | a8bbef8 | 2009-03-23 17:11:26 +0000 | [diff] [blame] | 6105 | initData.db = db; |
mistachkin | 1c06b47 | 2018-09-27 00:04:31 +0000 | [diff] [blame] | 6106 | initData.iDb = iDb; |
danielk1977 | a8bbef8 | 2009-03-23 17:11:26 +0000 | [diff] [blame] | 6107 | initData.pzErrMsg = &p->zErrMsg; |
drh | 9fd88e8 | 2018-09-07 11:08:31 +0000 | [diff] [blame] | 6108 | initData.mInitFlags = 0; |
danielk1977 | a8bbef8 | 2009-03-23 17:11:26 +0000 | [diff] [blame] | 6109 | zSql = sqlite3MPrintf(db, |
drh | c5a93d4 | 2019-08-12 00:08:07 +0000 | [diff] [blame] | 6110 | "SELECT*FROM\"%w\".%s WHERE %s ORDER BY rowid", |
drh | 69c3382 | 2016-08-18 14:33:11 +0000 | [diff] [blame] | 6111 | db->aDb[iDb].zDbSName, zMaster, pOp->p4.z); |
danielk1977 | a8bbef8 | 2009-03-23 17:11:26 +0000 | [diff] [blame] | 6112 | if( zSql==0 ){ |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 6113 | rc = SQLITE_NOMEM_BKPT; |
danielk1977 | a8bbef8 | 2009-03-23 17:11:26 +0000 | [diff] [blame] | 6114 | }else{ |
danielk1977 | a8bbef8 | 2009-03-23 17:11:26 +0000 | [diff] [blame] | 6115 | assert( db->init.busy==0 ); |
| 6116 | db->init.busy = 1; |
| 6117 | initData.rc = SQLITE_OK; |
drh | 6b86e51 | 2019-01-05 21:09:37 +0000 | [diff] [blame] | 6118 | initData.nInitRow = 0; |
danielk1977 | a8bbef8 | 2009-03-23 17:11:26 +0000 | [diff] [blame] | 6119 | assert( !db->mallocFailed ); |
| 6120 | rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0); |
| 6121 | if( rc==SQLITE_OK ) rc = initData.rc; |
drh | 6b86e51 | 2019-01-05 21:09:37 +0000 | [diff] [blame] | 6122 | if( rc==SQLITE_OK && initData.nInitRow==0 ){ |
| 6123 | /* The OP_ParseSchema opcode with a non-NULL P4 argument should parse |
| 6124 | ** at least one SQL statement. Any less than that indicates that |
| 6125 | ** the sqlite_master table is corrupt. */ |
| 6126 | rc = SQLITE_CORRUPT_BKPT; |
| 6127 | } |
drh | dbd6a7d | 2017-04-05 12:39:49 +0000 | [diff] [blame] | 6128 | sqlite3DbFreeNN(db, zSql); |
danielk1977 | a8bbef8 | 2009-03-23 17:11:26 +0000 | [diff] [blame] | 6129 | db->init.busy = 0; |
danielk1977 | a8bbef8 | 2009-03-23 17:11:26 +0000 | [diff] [blame] | 6130 | } |
drh | 3c23a88 | 2007-01-09 14:01:13 +0000 | [diff] [blame] | 6131 | } |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 6132 | if( rc ){ |
| 6133 | sqlite3ResetAllSchemasOfConnection(db); |
| 6134 | if( rc==SQLITE_NOMEM ){ |
| 6135 | goto no_mem; |
| 6136 | } |
| 6137 | goto abort_due_to_error; |
danielk1977 | 261919c | 2005-12-06 12:52:59 +0000 | [diff] [blame] | 6138 | } |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 6139 | break; |
| 6140 | } |
| 6141 | |
drh | 8bfdf72 | 2009-06-19 14:06:03 +0000 | [diff] [blame] | 6142 | #if !defined(SQLITE_OMIT_ANALYZE) |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 6143 | /* Opcode: LoadAnalysis P1 * * * * |
drh | 497e446 | 2005-07-23 03:18:40 +0000 | [diff] [blame] | 6144 | ** |
| 6145 | ** Read the sqlite_stat1 table for database P1 and load the content |
| 6146 | ** of that table into the internal index hash table. This will cause |
| 6147 | ** the analysis to be used when preparing all subsequent queries. |
| 6148 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 6149 | case OP_LoadAnalysis: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 6150 | assert( pOp->p1>=0 && pOp->p1<db->nDb ); |
| 6151 | rc = sqlite3AnalysisLoad(db, pOp->p1); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 6152 | if( rc ) goto abort_due_to_error; |
drh | 497e446 | 2005-07-23 03:18:40 +0000 | [diff] [blame] | 6153 | break; |
| 6154 | } |
drh | 8bfdf72 | 2009-06-19 14:06:03 +0000 | [diff] [blame] | 6155 | #endif /* !defined(SQLITE_OMIT_ANALYZE) */ |
drh | 497e446 | 2005-07-23 03:18:40 +0000 | [diff] [blame] | 6156 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 6157 | /* Opcode: DropTable P1 * * P4 * |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 6158 | ** |
| 6159 | ** Remove the internal (in-memory) data structures that describe |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 6160 | ** the table named P4 in database P1. This is called after a table |
drh | 5dad9a3 | 2014-07-25 18:37:42 +0000 | [diff] [blame] | 6161 | ** is dropped from disk (using the Destroy opcode) in order to keep |
| 6162 | ** the internal representation of the |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 6163 | ** schema consistent with what is on disk. |
| 6164 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 6165 | case OP_DropTable: { |
drh | 4031baf | 2018-05-28 17:31:20 +0000 | [diff] [blame] | 6166 | sqlite3VdbeIncrWriteCounter(p, 0); |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 6167 | sqlite3UnlinkAndDeleteTable(db, pOp->p1, pOp->p4.z); |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 6168 | break; |
| 6169 | } |
| 6170 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 6171 | /* Opcode: DropIndex P1 * * P4 * |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 6172 | ** |
| 6173 | ** Remove the internal (in-memory) data structures that describe |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 6174 | ** the index named P4 in database P1. This is called after an index |
drh | 5dad9a3 | 2014-07-25 18:37:42 +0000 | [diff] [blame] | 6175 | ** is dropped from disk (using the Destroy opcode) |
| 6176 | ** in order to keep the internal representation of the |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 6177 | ** schema consistent with what is on disk. |
| 6178 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 6179 | case OP_DropIndex: { |
drh | 4031baf | 2018-05-28 17:31:20 +0000 | [diff] [blame] | 6180 | sqlite3VdbeIncrWriteCounter(p, 0); |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 6181 | sqlite3UnlinkAndDeleteIndex(db, pOp->p1, pOp->p4.z); |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 6182 | break; |
| 6183 | } |
| 6184 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 6185 | /* Opcode: DropTrigger P1 * * P4 * |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 6186 | ** |
| 6187 | ** Remove the internal (in-memory) data structures that describe |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 6188 | ** the trigger named P4 in database P1. This is called after a trigger |
drh | 5dad9a3 | 2014-07-25 18:37:42 +0000 | [diff] [blame] | 6189 | ** is dropped from disk (using the Destroy opcode) in order to keep |
| 6190 | ** the internal representation of the |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 6191 | ** schema consistent with what is on disk. |
| 6192 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 6193 | case OP_DropTrigger: { |
drh | 4031baf | 2018-05-28 17:31:20 +0000 | [diff] [blame] | 6194 | sqlite3VdbeIncrWriteCounter(p, 0); |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 6195 | sqlite3UnlinkAndDeleteTrigger(db, pOp->p1, pOp->p4.z); |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 6196 | break; |
| 6197 | } |
| 6198 | |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 6199 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 6200 | #ifndef SQLITE_OMIT_INTEGRITY_CHECK |
drh | 98968b2 | 2016-03-15 22:00:39 +0000 | [diff] [blame] | 6201 | /* Opcode: IntegrityCk P1 P2 P3 P4 P5 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 6202 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 6203 | ** Do an analysis of the currently open database. Store in |
| 6204 | ** register P1 the text of an error message describing any problems. |
| 6205 | ** If no problems are found, store a NULL in register P1. |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 6206 | ** |
drh | 66accfc | 2017-02-22 18:04:42 +0000 | [diff] [blame] | 6207 | ** The register P3 contains one less than the maximum number of allowed errors. |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 6208 | ** At most reg(P3) errors will be reported. |
| 6209 | ** In other words, the analysis stops as soon as reg(P1) errors are |
| 6210 | ** seen. Reg(P1) is updated with the number of errors remaining. |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 6211 | ** |
drh | 98968b2 | 2016-03-15 22:00:39 +0000 | [diff] [blame] | 6212 | ** The root page numbers of all tables in the database are integers |
| 6213 | ** stored in P4_INTARRAY argument. |
drh | 2150432 | 2002-06-25 13:16:02 +0000 | [diff] [blame] | 6214 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 6215 | ** If P5 is not zero, the check is done on the auxiliary database |
drh | 2150432 | 2002-06-25 13:16:02 +0000 | [diff] [blame] | 6216 | ** file, not the main database file. |
drh | 1dd397f | 2002-02-03 03:34:07 +0000 | [diff] [blame] | 6217 | ** |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 6218 | ** This opcode is used to implement the integrity_check pragma. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 6219 | */ |
drh | aaab572 | 2002-02-19 13:39:21 +0000 | [diff] [blame] | 6220 | case OP_IntegrityCk: { |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 6221 | int nRoot; /* Number of tables to check. (Number of root pages.) */ |
| 6222 | int *aRoot; /* Array of rootpage numbers for tables to be checked */ |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 6223 | int nErr; /* Number of errors reported */ |
| 6224 | char *z; /* Text of the error report */ |
| 6225 | Mem *pnErr; /* Register keeping track of errors remaining */ |
drh | 9e92a47 | 2013-06-27 17:40:30 +0000 | [diff] [blame] | 6226 | |
drh | 1713afb | 2013-06-28 01:24:57 +0000 | [diff] [blame] | 6227 | assert( p->bIsReader ); |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 6228 | nRoot = pOp->p2; |
drh | 98968b2 | 2016-03-15 22:00:39 +0000 | [diff] [blame] | 6229 | aRoot = pOp->p4.ai; |
drh | 7906975 | 2004-05-22 21:30:40 +0000 | [diff] [blame] | 6230 | assert( nRoot>0 ); |
drh | b5c1063 | 2017-09-21 00:49:15 +0000 | [diff] [blame] | 6231 | assert( aRoot[0]==nRoot ); |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 6232 | assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) ); |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 6233 | pnErr = &aMem[pOp->p3]; |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 6234 | assert( (pnErr->flags & MEM_Int)!=0 ); |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 6235 | assert( (pnErr->flags & (MEM_Str|MEM_Blob))==0 ); |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 6236 | pIn1 = &aMem[pOp->p1]; |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 6237 | assert( pOp->p5<db->nDb ); |
drh | a7ab6d8 | 2014-07-21 15:44:39 +0000 | [diff] [blame] | 6238 | assert( DbMaskTest(p->btreeMask, pOp->p5) ); |
drh | 21f6daa | 2019-10-11 14:21:48 +0000 | [diff] [blame] | 6239 | z = sqlite3BtreeIntegrityCheck(db, db->aDb[pOp->p5].pBt, &aRoot[1], nRoot, |
drh | 66accfc | 2017-02-22 18:04:42 +0000 | [diff] [blame] | 6240 | (int)pnErr->u.i+1, &nErr); |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 6241 | sqlite3VdbeMemSetNull(pIn1); |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 6242 | if( nErr==0 ){ |
| 6243 | assert( z==0 ); |
drh | c890fec | 2008-08-01 20:10:08 +0000 | [diff] [blame] | 6244 | }else if( z==0 ){ |
| 6245 | goto no_mem; |
drh | 1dd397f | 2002-02-03 03:34:07 +0000 | [diff] [blame] | 6246 | }else{ |
drh | 66accfc | 2017-02-22 18:04:42 +0000 | [diff] [blame] | 6247 | pnErr->u.i -= nErr-1; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 6248 | sqlite3VdbeMemSetStr(pIn1, z, -1, SQLITE_UTF8, sqlite3_free); |
danielk1977 | 8a6b541 | 2004-05-24 07:04:25 +0000 | [diff] [blame] | 6249 | } |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 6250 | UPDATE_MAX_BLOBSIZE(pIn1); |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 6251 | sqlite3VdbeChangeEncoding(pIn1, encoding); |
drh | 21f6daa | 2019-10-11 14:21:48 +0000 | [diff] [blame] | 6252 | goto check_for_interrupt; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 6253 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 6254 | #endif /* SQLITE_OMIT_INTEGRITY_CHECK */ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 6255 | |
drh | 3d4501e | 2008-12-04 20:40:10 +0000 | [diff] [blame] | 6256 | /* Opcode: RowSetAdd P1 P2 * * * |
drh | 72e26de | 2016-08-24 21:24:04 +0000 | [diff] [blame] | 6257 | ** Synopsis: rowset(P1)=r[P2] |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 6258 | ** |
drh | bb6783b | 2017-04-29 18:02:49 +0000 | [diff] [blame] | 6259 | ** Insert the integer value held by register P2 into a RowSet object |
drh | 3d4501e | 2008-12-04 20:40:10 +0000 | [diff] [blame] | 6260 | ** held in register P1. |
| 6261 | ** |
| 6262 | ** An assertion fails if P2 is not an integer. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 6263 | */ |
drh | 93952eb | 2009-11-13 19:43:43 +0000 | [diff] [blame] | 6264 | case OP_RowSetAdd: { /* in1, in2 */ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 6265 | pIn1 = &aMem[pOp->p1]; |
| 6266 | pIn2 = &aMem[pOp->p2]; |
drh | 93952eb | 2009-11-13 19:43:43 +0000 | [diff] [blame] | 6267 | assert( (pIn2->flags & MEM_Int)!=0 ); |
drh | 9d67afc | 2018-08-29 20:24:03 +0000 | [diff] [blame] | 6268 | if( (pIn1->flags & MEM_Blob)==0 ){ |
| 6269 | if( sqlite3VdbeMemSetRowSet(pIn1) ) goto no_mem; |
drh | 3d4501e | 2008-12-04 20:40:10 +0000 | [diff] [blame] | 6270 | } |
drh | 9d67afc | 2018-08-29 20:24:03 +0000 | [diff] [blame] | 6271 | assert( sqlite3VdbeMemIsRowSet(pIn1) ); |
| 6272 | sqlite3RowSetInsert((RowSet*)pIn1->z, pIn2->u.i); |
drh | 3d4501e | 2008-12-04 20:40:10 +0000 | [diff] [blame] | 6273 | break; |
| 6274 | } |
| 6275 | |
| 6276 | /* Opcode: RowSetRead P1 P2 P3 * * |
drh | 72e26de | 2016-08-24 21:24:04 +0000 | [diff] [blame] | 6277 | ** Synopsis: r[P3]=rowset(P1) |
drh | 3d4501e | 2008-12-04 20:40:10 +0000 | [diff] [blame] | 6278 | ** |
drh | bb6783b | 2017-04-29 18:02:49 +0000 | [diff] [blame] | 6279 | ** Extract the smallest value from the RowSet object in P1 |
| 6280 | ** and put that value into register P3. |
| 6281 | ** Or, if RowSet object P1 is initially empty, leave P3 |
drh | 3d4501e | 2008-12-04 20:40:10 +0000 | [diff] [blame] | 6282 | ** unchanged and jump to instruction P2. |
| 6283 | */ |
drh | 93952eb | 2009-11-13 19:43:43 +0000 | [diff] [blame] | 6284 | case OP_RowSetRead: { /* jump, in1, out3 */ |
drh | 3d4501e | 2008-12-04 20:40:10 +0000 | [diff] [blame] | 6285 | i64 val; |
drh | 49afe3a | 2013-07-10 03:05:14 +0000 | [diff] [blame] | 6286 | |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 6287 | pIn1 = &aMem[pOp->p1]; |
drh | 9d67afc | 2018-08-29 20:24:03 +0000 | [diff] [blame] | 6288 | assert( (pIn1->flags & MEM_Blob)==0 || sqlite3VdbeMemIsRowSet(pIn1) ); |
| 6289 | if( (pIn1->flags & MEM_Blob)==0 |
| 6290 | || sqlite3RowSetNext((RowSet*)pIn1->z, &val)==0 |
drh | 3d4501e | 2008-12-04 20:40:10 +0000 | [diff] [blame] | 6291 | ){ |
| 6292 | /* The boolean index is empty */ |
drh | 93952eb | 2009-11-13 19:43:43 +0000 | [diff] [blame] | 6293 | sqlite3VdbeMemSetNull(pIn1); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 6294 | VdbeBranchTaken(1,2); |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 6295 | goto jump_to_p2_and_check_for_interrupt; |
drh | 3d4501e | 2008-12-04 20:40:10 +0000 | [diff] [blame] | 6296 | }else{ |
| 6297 | /* A value was pulled from the index */ |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 6298 | VdbeBranchTaken(0,2); |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 6299 | sqlite3VdbeMemSetInt64(&aMem[pOp->p3], val); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 6300 | } |
drh | 49afe3a | 2013-07-10 03:05:14 +0000 | [diff] [blame] | 6301 | goto check_for_interrupt; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 6302 | } |
| 6303 | |
drh | 1b26c7c | 2009-04-22 02:15:47 +0000 | [diff] [blame] | 6304 | /* Opcode: RowSetTest P1 P2 P3 P4 |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 6305 | ** Synopsis: if r[P3] in rowset(P1) goto P2 |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 6306 | ** |
drh | ade9760 | 2009-04-21 15:05:18 +0000 | [diff] [blame] | 6307 | ** 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] | 6308 | ** contains a RowSet object and that RowSet object contains |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 6309 | ** the value held in P3, jump to register P2. Otherwise, insert the |
drh | 1b26c7c | 2009-04-22 02:15:47 +0000 | [diff] [blame] | 6310 | ** integer in P3 into the RowSet and continue on to the |
drh | ade9760 | 2009-04-21 15:05:18 +0000 | [diff] [blame] | 6311 | ** next opcode. |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 6312 | ** |
drh | bb6783b | 2017-04-29 18:02:49 +0000 | [diff] [blame] | 6313 | ** The RowSet object is optimized for the case where sets of integers |
| 6314 | ** are inserted in distinct phases, which each set contains no duplicates. |
| 6315 | ** Each set is identified by a unique P4 value. The first set |
| 6316 | ** must have P4==0, the final set must have P4==-1, and for all other sets |
| 6317 | ** must have P4>0. |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 6318 | ** |
| 6319 | ** This allows optimizations: (a) when P4==0 there is no need to test |
drh | bb6783b | 2017-04-29 18:02:49 +0000 | [diff] [blame] | 6320 | ** the RowSet object for P3, as it is guaranteed not to contain it, |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 6321 | ** (b) when P4==-1 there is no need to insert the value, as it will |
| 6322 | ** never be tested for, and (c) when a value that is part of set X is |
| 6323 | ** inserted, there is no need to search to see if the same value was |
| 6324 | ** previously inserted as part of set X (only if it was previously |
| 6325 | ** inserted as part of some other set). |
| 6326 | */ |
drh | 1b26c7c | 2009-04-22 02:15:47 +0000 | [diff] [blame] | 6327 | case OP_RowSetTest: { /* jump, in1, in3 */ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 6328 | int iSet; |
| 6329 | int exists; |
| 6330 | |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 6331 | pIn1 = &aMem[pOp->p1]; |
| 6332 | pIn3 = &aMem[pOp->p3]; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 6333 | iSet = pOp->p4.i; |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 6334 | assert( pIn3->flags&MEM_Int ); |
| 6335 | |
drh | 1b26c7c | 2009-04-22 02:15:47 +0000 | [diff] [blame] | 6336 | /* If there is anything other than a rowset object in memory cell P1, |
| 6337 | ** delete it now and initialize P1 with an empty rowset |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 6338 | */ |
drh | 9d67afc | 2018-08-29 20:24:03 +0000 | [diff] [blame] | 6339 | if( (pIn1->flags & MEM_Blob)==0 ){ |
| 6340 | if( sqlite3VdbeMemSetRowSet(pIn1) ) goto no_mem; |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 6341 | } |
drh | 9d67afc | 2018-08-29 20:24:03 +0000 | [diff] [blame] | 6342 | assert( sqlite3VdbeMemIsRowSet(pIn1) ); |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 6343 | assert( pOp->p4type==P4_INT32 ); |
drh | 1b26c7c | 2009-04-22 02:15:47 +0000 | [diff] [blame] | 6344 | assert( iSet==-1 || iSet>=0 ); |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 6345 | if( iSet ){ |
drh | 9d67afc | 2018-08-29 20:24:03 +0000 | [diff] [blame] | 6346 | exists = sqlite3RowSetTest((RowSet*)pIn1->z, iSet, pIn3->u.i); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 6347 | VdbeBranchTaken(exists!=0,2); |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 6348 | if( exists ) goto jump_to_p2; |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 6349 | } |
| 6350 | if( iSet>=0 ){ |
drh | 9d67afc | 2018-08-29 20:24:03 +0000 | [diff] [blame] | 6351 | sqlite3RowSetInsert((RowSet*)pIn1->z, pIn3->u.i); |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 6352 | } |
| 6353 | break; |
| 6354 | } |
| 6355 | |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 6356 | |
danielk1977 | 93758c8 | 2005-01-21 08:13:14 +0000 | [diff] [blame] | 6357 | #ifndef SQLITE_OMIT_TRIGGER |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 6358 | |
drh | 0fd6135 | 2014-02-07 02:29:45 +0000 | [diff] [blame] | 6359 | /* Opcode: Program P1 P2 P3 P4 P5 |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 6360 | ** |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 6361 | ** Execute the trigger program passed as P4 (type P4_SUBPROGRAM). |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 6362 | ** |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 6363 | ** P1 contains the address of the memory cell that contains the first memory |
| 6364 | ** cell in an array of values used as arguments to the sub-program. P2 |
| 6365 | ** contains the address to jump to if the sub-program throws an IGNORE |
| 6366 | ** exception using the RAISE() function. Register P3 contains the address |
| 6367 | ** of a memory cell in this (the parent) VM that is used to allocate the |
| 6368 | ** memory required by the sub-vdbe at runtime. |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 6369 | ** |
| 6370 | ** P4 is a pointer to the VM containing the trigger program. |
drh | 0fd6135 | 2014-02-07 02:29:45 +0000 | [diff] [blame] | 6371 | ** |
| 6372 | ** If P5 is non-zero, then recursive program invocation is enabled. |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 6373 | */ |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 6374 | case OP_Program: { /* jump */ |
dan | 65a7cd1 | 2009-09-01 12:16:01 +0000 | [diff] [blame] | 6375 | int nMem; /* Number of memory registers for sub-program */ |
| 6376 | int nByte; /* Bytes of runtime space required for sub-program */ |
| 6377 | Mem *pRt; /* Register to allocate runtime space */ |
| 6378 | Mem *pMem; /* Used to iterate through memory cells */ |
| 6379 | Mem *pEnd; /* Last memory cell in new array */ |
| 6380 | VdbeFrame *pFrame; /* New vdbe frame to execute in */ |
| 6381 | SubProgram *pProgram; /* Sub-program to execute */ |
| 6382 | void *t; /* Token identifying trigger */ |
| 6383 | |
| 6384 | pProgram = pOp->p4.pProgram; |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 6385 | pRt = &aMem[pOp->p3]; |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 6386 | assert( pProgram->nOp>0 ); |
| 6387 | |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 6388 | /* If the p5 flag is clear, then recursive invocation of triggers is |
| 6389 | ** disabled for backwards compatibility (p5 is set if this sub-program |
| 6390 | ** is really a trigger, not a foreign key action, and the flag set |
| 6391 | ** and cleared by the "PRAGMA recursive_triggers" command is clear). |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 6392 | ** |
| 6393 | ** It is recursive invocation of triggers, at the SQL level, that is |
| 6394 | ** disabled. In some cases a single trigger may generate more than one |
| 6395 | ** SubProgram (if the trigger may be executed with more than one different |
| 6396 | ** ON CONFLICT algorithm). SubProgram structures associated with a |
| 6397 | ** single trigger all have the same value for the SubProgram.token |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 6398 | ** variable. */ |
| 6399 | if( pOp->p5 ){ |
dan | 65a7cd1 | 2009-09-01 12:16:01 +0000 | [diff] [blame] | 6400 | t = pProgram->token; |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 6401 | for(pFrame=p->pFrame; pFrame && pFrame->token!=t; pFrame=pFrame->pParent); |
| 6402 | if( pFrame ) break; |
| 6403 | } |
| 6404 | |
dan | f589450 | 2009-10-07 18:41:19 +0000 | [diff] [blame] | 6405 | if( p->nFrame>=db->aLimit[SQLITE_LIMIT_TRIGGER_DEPTH] ){ |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 6406 | rc = SQLITE_ERROR; |
drh | 22c17b8 | 2015-05-15 04:13:15 +0000 | [diff] [blame] | 6407 | sqlite3VdbeError(p, "too many levels of trigger recursion"); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 6408 | goto abort_due_to_error; |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 6409 | } |
| 6410 | |
| 6411 | /* Register pRt is used to store the memory required to save the state |
| 6412 | ** of the current program, and the memory required at runtime to execute |
| 6413 | ** the trigger program. If this trigger has been fired before, then pRt |
| 6414 | ** is already allocated. Otherwise, it must be initialized. */ |
drh | 72f56ef | 2018-08-29 18:47:22 +0000 | [diff] [blame] | 6415 | if( (pRt->flags&MEM_Blob)==0 ){ |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 6416 | /* SubProgram.nMem is set to the number of memory cells used by the |
| 6417 | ** program stored in SubProgram.aOp. As well as these, one memory |
| 6418 | ** cell is required for each cursor used by the program. Set local |
| 6419 | ** variable nMem (and later, VdbeFrame.nChildMem) to this value. |
| 6420 | */ |
dan | 65a7cd1 | 2009-09-01 12:16:01 +0000 | [diff] [blame] | 6421 | nMem = pProgram->nMem + pProgram->nCsr; |
drh | 3cdce92 | 2016-03-21 00:30:40 +0000 | [diff] [blame] | 6422 | assert( nMem>0 ); |
| 6423 | if( pProgram->nCsr==0 ) nMem++; |
dan | 65a7cd1 | 2009-09-01 12:16:01 +0000 | [diff] [blame] | 6424 | nByte = ROUND8(sizeof(VdbeFrame)) |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 6425 | + nMem * sizeof(Mem) |
drh | ab087d4 | 2017-03-24 17:59:56 +0000 | [diff] [blame] | 6426 | + pProgram->nCsr * sizeof(VdbeCursor*) |
| 6427 | + (pProgram->nOp + 7)/8; |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 6428 | pFrame = sqlite3DbMallocZero(db, nByte); |
| 6429 | if( !pFrame ){ |
| 6430 | goto no_mem; |
| 6431 | } |
| 6432 | sqlite3VdbeMemRelease(pRt); |
drh | 72f56ef | 2018-08-29 18:47:22 +0000 | [diff] [blame] | 6433 | pRt->flags = MEM_Blob|MEM_Dyn; |
| 6434 | pRt->z = (char*)pFrame; |
| 6435 | pRt->n = nByte; |
| 6436 | pRt->xDel = sqlite3VdbeFrameMemDel; |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 6437 | |
| 6438 | pFrame->v = p; |
| 6439 | pFrame->nChildMem = nMem; |
| 6440 | pFrame->nChildCsr = pProgram->nCsr; |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 6441 | pFrame->pc = (int)(pOp - aOp); |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 6442 | pFrame->aMem = p->aMem; |
| 6443 | pFrame->nMem = p->nMem; |
| 6444 | pFrame->apCsr = p->apCsr; |
| 6445 | pFrame->nCursor = p->nCursor; |
| 6446 | pFrame->aOp = p->aOp; |
| 6447 | pFrame->nOp = p->nOp; |
| 6448 | pFrame->token = pProgram->token; |
dan | e2f771b | 2014-11-03 15:33:17 +0000 | [diff] [blame] | 6449 | #ifdef SQLITE_ENABLE_STMT_SCANSTATUS |
dan | 43764a8 | 2014-11-01 21:00:04 +0000 | [diff] [blame] | 6450 | pFrame->anExec = p->anExec; |
dan | e2f771b | 2014-11-03 15:33:17 +0000 | [diff] [blame] | 6451 | #endif |
drh | 72f56ef | 2018-08-29 18:47:22 +0000 | [diff] [blame] | 6452 | #ifdef SQLITE_DEBUG |
| 6453 | pFrame->iFrameMagic = SQLITE_FRAME_MAGIC; |
| 6454 | #endif |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 6455 | |
| 6456 | pEnd = &VdbeFrameMem(pFrame)[pFrame->nChildMem]; |
| 6457 | for(pMem=VdbeFrameMem(pFrame); pMem!=pEnd; pMem++){ |
drh | a5750cf | 2014-02-07 13:20:31 +0000 | [diff] [blame] | 6458 | pMem->flags = MEM_Undefined; |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 6459 | pMem->db = db; |
| 6460 | } |
| 6461 | }else{ |
drh | 72f56ef | 2018-08-29 18:47:22 +0000 | [diff] [blame] | 6462 | pFrame = (VdbeFrame*)pRt->z; |
| 6463 | assert( pRt->xDel==sqlite3VdbeFrameMemDel ); |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 6464 | assert( pProgram->nMem+pProgram->nCsr==pFrame->nChildMem |
| 6465 | || (pProgram->nCsr==0 && pProgram->nMem+1==pFrame->nChildMem) ); |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 6466 | assert( pProgram->nCsr==pFrame->nChildCsr ); |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 6467 | assert( (int)(pOp - aOp)==pFrame->pc ); |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 6468 | } |
| 6469 | |
| 6470 | p->nFrame++; |
| 6471 | pFrame->pParent = p->pFrame; |
drh | fae58d5 | 2017-01-26 17:26:44 +0000 | [diff] [blame] | 6472 | pFrame->lastRowid = db->lastRowid; |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 6473 | pFrame->nChange = p->nChange; |
dan | c3da667 | 2014-10-28 18:24:16 +0000 | [diff] [blame] | 6474 | pFrame->nDbChange = p->db->nChange; |
dan | 3200132 | 2016-02-19 18:54:29 +0000 | [diff] [blame] | 6475 | assert( pFrame->pAuxData==0 ); |
| 6476 | pFrame->pAuxData = p->pAuxData; |
| 6477 | p->pAuxData = 0; |
dan | 2832ad4 | 2009-08-31 15:27:27 +0000 | [diff] [blame] | 6478 | p->nChange = 0; |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 6479 | p->pFrame = pFrame; |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 6480 | p->aMem = aMem = VdbeFrameMem(pFrame); |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 6481 | p->nMem = pFrame->nChildMem; |
shane | cea72b2 | 2009-09-07 04:38:36 +0000 | [diff] [blame] | 6482 | p->nCursor = (u16)pFrame->nChildCsr; |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 6483 | p->apCsr = (VdbeCursor **)&aMem[p->nMem]; |
drh | ab087d4 | 2017-03-24 17:59:56 +0000 | [diff] [blame] | 6484 | pFrame->aOnce = (u8*)&p->apCsr[pProgram->nCsr]; |
drh | 18333ef | 2017-03-24 18:38:41 +0000 | [diff] [blame] | 6485 | memset(pFrame->aOnce, 0, (pProgram->nOp + 7)/8); |
drh | bbe879d | 2009-11-14 18:04:35 +0000 | [diff] [blame] | 6486 | p->aOp = aOp = pProgram->aOp; |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 6487 | p->nOp = pProgram->nOp; |
dan | e2f771b | 2014-11-03 15:33:17 +0000 | [diff] [blame] | 6488 | #ifdef SQLITE_ENABLE_STMT_SCANSTATUS |
dan | 43764a8 | 2014-11-01 21:00:04 +0000 | [diff] [blame] | 6489 | p->anExec = 0; |
dan | e2f771b | 2014-11-03 15:33:17 +0000 | [diff] [blame] | 6490 | #endif |
drh | b2e61bc | 2019-01-25 19:29:01 +0000 | [diff] [blame] | 6491 | #ifdef SQLITE_DEBUG |
| 6492 | /* Verify that second and subsequent executions of the same trigger do not |
| 6493 | ** try to reuse register values from the first use. */ |
| 6494 | { |
| 6495 | int i; |
| 6496 | for(i=0; i<p->nMem; i++){ |
| 6497 | aMem[i].pScopyFrom = 0; /* Prevent false-positive AboutToChange() errs */ |
drh | f5cfe6f | 2020-03-03 20:48:12 +0000 | [diff] [blame] | 6498 | MemSetTypeFlag(&aMem[i], MEM_Undefined); /* Fault if this reg is reused */ |
drh | b2e61bc | 2019-01-25 19:29:01 +0000 | [diff] [blame] | 6499 | } |
| 6500 | } |
| 6501 | #endif |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 6502 | pOp = &aOp[-1]; |
drh | b1af9c6 | 2019-02-20 13:55:45 +0000 | [diff] [blame] | 6503 | goto check_for_interrupt; |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 6504 | } |
| 6505 | |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 6506 | /* Opcode: Param P1 P2 * * * |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 6507 | ** |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 6508 | ** This opcode is only ever present in sub-programs called via the |
| 6509 | ** OP_Program instruction. Copy a value currently stored in a memory |
| 6510 | ** cell of the calling (parent) frame to cell P2 in the current frames |
| 6511 | ** address space. This is used by trigger programs to access the new.* |
| 6512 | ** and old.* values. |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 6513 | ** |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 6514 | ** The address of the cell in the parent frame is determined by adding |
| 6515 | ** the value of the P1 argument to the value of the P1 argument to the |
| 6516 | ** calling OP_Program instruction. |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 6517 | */ |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 6518 | case OP_Param: { /* out2 */ |
dan | 65a7cd1 | 2009-09-01 12:16:01 +0000 | [diff] [blame] | 6519 | VdbeFrame *pFrame; |
| 6520 | Mem *pIn; |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 6521 | pOut = out2Prerelease(p, pOp); |
dan | 65a7cd1 | 2009-09-01 12:16:01 +0000 | [diff] [blame] | 6522 | pFrame = p->pFrame; |
| 6523 | pIn = &pFrame->aMem[pOp->p1 + pFrame->aOp[pFrame->pc].p1]; |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 6524 | sqlite3VdbeMemShallowCopy(pOut, pIn, MEM_Ephem); |
| 6525 | break; |
| 6526 | } |
| 6527 | |
danielk1977 | 93758c8 | 2005-01-21 08:13:14 +0000 | [diff] [blame] | 6528 | #endif /* #ifndef SQLITE_OMIT_TRIGGER */ |
rdc | b0c374f | 2004-02-20 22:53:38 +0000 | [diff] [blame] | 6529 | |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 6530 | #ifndef SQLITE_OMIT_FOREIGN_KEY |
dan | 32b09f2 | 2009-09-23 17:29:59 +0000 | [diff] [blame] | 6531 | /* Opcode: FkCounter P1 P2 * * * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 6532 | ** Synopsis: fkctr[P1]+=P2 |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 6533 | ** |
dan | 0ff297e | 2009-09-25 17:03:14 +0000 | [diff] [blame] | 6534 | ** Increment a "constraint counter" by P2 (P2 may be negative or positive). |
| 6535 | ** If P1 is non-zero, the database constraint counter is incremented |
| 6536 | ** (deferred foreign key constraints). Otherwise, if P1 is zero, the |
dan | 32b09f2 | 2009-09-23 17:29:59 +0000 | [diff] [blame] | 6537 | ** statement counter is incremented (immediate foreign key constraints). |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 6538 | */ |
dan | 32b09f2 | 2009-09-23 17:29:59 +0000 | [diff] [blame] | 6539 | case OP_FkCounter: { |
drh | 963c74d | 2013-07-11 12:19:12 +0000 | [diff] [blame] | 6540 | if( db->flags & SQLITE_DeferFKs ){ |
dan | cb3e4b7 | 2013-07-03 19:53:05 +0000 | [diff] [blame] | 6541 | db->nDeferredImmCons += pOp->p2; |
| 6542 | }else if( pOp->p1 ){ |
dan | 0ff297e | 2009-09-25 17:03:14 +0000 | [diff] [blame] | 6543 | db->nDeferredCons += pOp->p2; |
dan | 32b09f2 | 2009-09-23 17:29:59 +0000 | [diff] [blame] | 6544 | }else{ |
dan | 0ff297e | 2009-09-25 17:03:14 +0000 | [diff] [blame] | 6545 | p->nFkConstraint += pOp->p2; |
| 6546 | } |
| 6547 | break; |
| 6548 | } |
| 6549 | |
| 6550 | /* Opcode: FkIfZero P1 P2 * * * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 6551 | ** Synopsis: if fkctr[P1]==0 goto P2 |
dan | 0ff297e | 2009-09-25 17:03:14 +0000 | [diff] [blame] | 6552 | ** |
| 6553 | ** This opcode tests if a foreign key constraint-counter is currently zero. |
| 6554 | ** If so, jump to instruction P2. Otherwise, fall through to the next |
| 6555 | ** instruction. |
| 6556 | ** |
| 6557 | ** If P1 is non-zero, then the jump is taken if the database constraint-counter |
| 6558 | ** is zero (the one that counts deferred constraint violations). If P1 is |
| 6559 | ** zero, the jump is taken if the statement constraint-counter is zero |
| 6560 | ** (immediate foreign key constraint violations). |
| 6561 | */ |
| 6562 | case OP_FkIfZero: { /* jump */ |
| 6563 | if( pOp->p1 ){ |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 6564 | VdbeBranchTaken(db->nDeferredCons==0 && db->nDeferredImmCons==0, 2); |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 6565 | if( db->nDeferredCons==0 && db->nDeferredImmCons==0 ) goto jump_to_p2; |
dan | 0ff297e | 2009-09-25 17:03:14 +0000 | [diff] [blame] | 6566 | }else{ |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 6567 | VdbeBranchTaken(p->nFkConstraint==0 && db->nDeferredImmCons==0, 2); |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 6568 | if( p->nFkConstraint==0 && db->nDeferredImmCons==0 ) goto jump_to_p2; |
dan | 32b09f2 | 2009-09-23 17:29:59 +0000 | [diff] [blame] | 6569 | } |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 6570 | break; |
| 6571 | } |
| 6572 | #endif /* #ifndef SQLITE_OMIT_FOREIGN_KEY */ |
| 6573 | |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 6574 | #ifndef SQLITE_OMIT_AUTOINCREMENT |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 6575 | /* Opcode: MemMax P1 P2 * * * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 6576 | ** Synopsis: r[P1]=max(r[P1],r[P2]) |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 6577 | ** |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 6578 | ** P1 is a register in the root frame of this VM (the root frame is |
| 6579 | ** different from the current frame if this instruction is being executed |
| 6580 | ** within a sub-program). Set the value of register P1 to the maximum of |
| 6581 | ** its current value and the value in register P2. |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 6582 | ** |
| 6583 | ** This instruction throws an error if the memory cell is not initially |
| 6584 | ** an integer. |
| 6585 | */ |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 6586 | case OP_MemMax: { /* in2 */ |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 6587 | VdbeFrame *pFrame; |
| 6588 | if( p->pFrame ){ |
| 6589 | for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent); |
| 6590 | pIn1 = &pFrame->aMem[pOp->p1]; |
| 6591 | }else{ |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 6592 | pIn1 = &aMem[pOp->p1]; |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 6593 | } |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 6594 | assert( memIsValid(pIn1) ); |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 6595 | sqlite3VdbeMemIntegerify(pIn1); |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 6596 | pIn2 = &aMem[pOp->p2]; |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 6597 | sqlite3VdbeMemIntegerify(pIn2); |
| 6598 | if( pIn1->u.i<pIn2->u.i){ |
| 6599 | pIn1->u.i = pIn2->u.i; |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 6600 | } |
| 6601 | break; |
| 6602 | } |
| 6603 | #endif /* SQLITE_OMIT_AUTOINCREMENT */ |
| 6604 | |
drh | 8b0cf38 | 2015-10-06 21:07:06 +0000 | [diff] [blame] | 6605 | /* Opcode: IfPos P1 P2 P3 * * |
| 6606 | ** Synopsis: if r[P1]>0 then r[P1]-=P3, goto P2 |
danielk1977 | a2dc3b1 | 2005-02-05 12:48:48 +0000 | [diff] [blame] | 6607 | ** |
drh | 1689707 | 2015-03-07 00:57:37 +0000 | [diff] [blame] | 6608 | ** Register P1 must contain an integer. |
mistachkin | 91a3ecb | 2015-10-06 21:49:55 +0000 | [diff] [blame] | 6609 | ** 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] | 6610 | ** value in P1 and jump to P2. |
drh | 6f58f70 | 2006-01-08 05:26:41 +0000 | [diff] [blame] | 6611 | ** |
drh | 1689707 | 2015-03-07 00:57:37 +0000 | [diff] [blame] | 6612 | ** If the initial value of register P1 is less than 1, then the |
| 6613 | ** value is unchanged and control passes through to the next instruction. |
danielk1977 | a2dc3b1 | 2005-02-05 12:48:48 +0000 | [diff] [blame] | 6614 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 6615 | case OP_IfPos: { /* jump, in1 */ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 6616 | pIn1 = &aMem[pOp->p1]; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 6617 | assert( pIn1->flags&MEM_Int ); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 6618 | VdbeBranchTaken( pIn1->u.i>0, 2); |
drh | 8b0cf38 | 2015-10-06 21:07:06 +0000 | [diff] [blame] | 6619 | if( pIn1->u.i>0 ){ |
| 6620 | pIn1->u.i -= pOp->p3; |
| 6621 | goto jump_to_p2; |
| 6622 | } |
drh | ec7429a | 2005-10-06 16:53:14 +0000 | [diff] [blame] | 6623 | break; |
| 6624 | } |
| 6625 | |
drh | cc2fa4c | 2016-01-25 15:57:29 +0000 | [diff] [blame] | 6626 | /* Opcode: OffsetLimit P1 P2 P3 * * |
| 6627 | ** 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] | 6628 | ** |
drh | cc2fa4c | 2016-01-25 15:57:29 +0000 | [diff] [blame] | 6629 | ** This opcode performs a commonly used computation associated with |
| 6630 | ** LIMIT and OFFSET process. r[P1] holds the limit counter. r[P3] |
| 6631 | ** holds the offset counter. The opcode computes the combined value |
| 6632 | ** of the LIMIT and OFFSET and stores that value in r[P2]. The r[P2] |
| 6633 | ** value computed is the total number of rows that will need to be |
| 6634 | ** visited in order to complete the query. |
| 6635 | ** |
| 6636 | ** If r[P3] is zero or negative, that means there is no OFFSET |
| 6637 | ** and r[P2] is set to be the value of the LIMIT, r[P1]. |
| 6638 | ** |
| 6639 | ** if r[P1] is zero or negative, that means there is no LIMIT |
| 6640 | ** and r[P2] is set to -1. |
| 6641 | ** |
| 6642 | ** 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] | 6643 | */ |
drh | cc2fa4c | 2016-01-25 15:57:29 +0000 | [diff] [blame] | 6644 | case OP_OffsetLimit: { /* in1, out2, in3 */ |
drh | 719da30 | 2016-12-10 04:06:49 +0000 | [diff] [blame] | 6645 | i64 x; |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 6646 | pIn1 = &aMem[pOp->p1]; |
drh | cc2fa4c | 2016-01-25 15:57:29 +0000 | [diff] [blame] | 6647 | pIn3 = &aMem[pOp->p3]; |
| 6648 | pOut = out2Prerelease(p, pOp); |
| 6649 | assert( pIn1->flags & MEM_Int ); |
| 6650 | assert( pIn3->flags & MEM_Int ); |
drh | 719da30 | 2016-12-10 04:06:49 +0000 | [diff] [blame] | 6651 | x = pIn1->u.i; |
| 6652 | if( x<=0 || sqlite3AddInt64(&x, pIn3->u.i>0?pIn3->u.i:0) ){ |
| 6653 | /* If the LIMIT is less than or equal to zero, loop forever. This |
| 6654 | ** is documented. But also, if the LIMIT+OFFSET exceeds 2^63 then |
| 6655 | ** also loop forever. This is undocumented. In fact, one could argue |
| 6656 | ** that the loop should terminate. But assuming 1 billion iterations |
| 6657 | ** per second (far exceeding the capabilities of any current hardware) |
| 6658 | ** it would take nearly 300 years to actually reach the limit. So |
| 6659 | ** looping forever is a reasonable approximation. */ |
| 6660 | pOut->u.i = -1; |
| 6661 | }else{ |
| 6662 | pOut->u.i = x; |
| 6663 | } |
drh | 15007a9 | 2006-01-08 18:10:17 +0000 | [diff] [blame] | 6664 | break; |
| 6665 | } |
| 6666 | |
drh | f99dd35 | 2016-12-18 17:42:00 +0000 | [diff] [blame] | 6667 | /* Opcode: IfNotZero P1 P2 * * * |
| 6668 | ** Synopsis: if r[P1]!=0 then r[P1]--, goto P2 |
drh | ec7429a | 2005-10-06 16:53:14 +0000 | [diff] [blame] | 6669 | ** |
drh | 1689707 | 2015-03-07 00:57:37 +0000 | [diff] [blame] | 6670 | ** Register P1 must contain an integer. If the content of register P1 is |
drh | f99dd35 | 2016-12-18 17:42:00 +0000 | [diff] [blame] | 6671 | ** initially greater than zero, then decrement the value in register P1. |
| 6672 | ** If it is non-zero (negative or positive) and then also jump to P2. |
| 6673 | ** If register P1 is initially zero, leave it unchanged and fall through. |
drh | ec7429a | 2005-10-06 16:53:14 +0000 | [diff] [blame] | 6674 | */ |
drh | 1689707 | 2015-03-07 00:57:37 +0000 | [diff] [blame] | 6675 | case OP_IfNotZero: { /* jump, in1 */ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 6676 | pIn1 = &aMem[pOp->p1]; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 6677 | assert( pIn1->flags&MEM_Int ); |
drh | 1689707 | 2015-03-07 00:57:37 +0000 | [diff] [blame] | 6678 | VdbeBranchTaken(pIn1->u.i<0, 2); |
| 6679 | if( pIn1->u.i ){ |
drh | f99dd35 | 2016-12-18 17:42:00 +0000 | [diff] [blame] | 6680 | if( pIn1->u.i>0 ) pIn1->u.i--; |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 6681 | goto jump_to_p2; |
drh | 1689707 | 2015-03-07 00:57:37 +0000 | [diff] [blame] | 6682 | } |
| 6683 | break; |
| 6684 | } |
| 6685 | |
| 6686 | /* Opcode: DecrJumpZero P1 P2 * * * |
| 6687 | ** Synopsis: if (--r[P1])==0 goto P2 |
| 6688 | ** |
drh | ab5be2e | 2016-11-30 05:08:59 +0000 | [diff] [blame] | 6689 | ** Register P1 must hold an integer. Decrement the value in P1 |
| 6690 | ** and jump to P2 if the new value is exactly zero. |
drh | 1689707 | 2015-03-07 00:57:37 +0000 | [diff] [blame] | 6691 | */ |
| 6692 | case OP_DecrJumpZero: { /* jump, in1 */ |
| 6693 | pIn1 = &aMem[pOp->p1]; |
| 6694 | assert( pIn1->flags&MEM_Int ); |
drh | ab5be2e | 2016-11-30 05:08:59 +0000 | [diff] [blame] | 6695 | if( pIn1->u.i>SMALLEST_INT64 ) pIn1->u.i--; |
| 6696 | VdbeBranchTaken(pIn1->u.i==0, 2); |
| 6697 | if( pIn1->u.i==0 ) goto jump_to_p2; |
drh | a2a49dc | 2008-01-02 14:28:13 +0000 | [diff] [blame] | 6698 | break; |
| 6699 | } |
| 6700 | |
drh | 1689707 | 2015-03-07 00:57:37 +0000 | [diff] [blame] | 6701 | |
drh | 8f26da6 | 2018-07-05 21:22:57 +0000 | [diff] [blame] | 6702 | /* Opcode: AggStep * P2 P3 P4 P5 |
drh | f63552b | 2013-10-30 00:25:03 +0000 | [diff] [blame] | 6703 | ** Synopsis: accum=r[P3] step(r[P2@P5]) |
drh | e509535 | 2002-02-24 03:25:14 +0000 | [diff] [blame] | 6704 | ** |
drh | 8f26da6 | 2018-07-05 21:22:57 +0000 | [diff] [blame] | 6705 | ** Execute the xStep function for an aggregate. |
| 6706 | ** The function has P5 arguments. P4 is a pointer to the |
dan | 9a94722 | 2018-06-14 19:06:36 +0000 | [diff] [blame] | 6707 | ** FuncDef structure that specifies the function. Register P3 is the |
drh | e2d9e7c | 2015-06-26 18:47:53 +0000 | [diff] [blame] | 6708 | ** accumulator. |
drh | e509535 | 2002-02-24 03:25:14 +0000 | [diff] [blame] | 6709 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 6710 | ** The P5 arguments are taken from register P2 and its |
| 6711 | ** successors. |
drh | e509535 | 2002-02-24 03:25:14 +0000 | [diff] [blame] | 6712 | */ |
drh | 8f26da6 | 2018-07-05 21:22:57 +0000 | [diff] [blame] | 6713 | /* Opcode: AggInverse * P2 P3 P4 P5 |
| 6714 | ** Synopsis: accum=r[P3] inverse(r[P2@P5]) |
| 6715 | ** |
| 6716 | ** Execute the xInverse function for an aggregate. |
| 6717 | ** The function has P5 arguments. P4 is a pointer to the |
| 6718 | ** FuncDef structure that specifies the function. Register P3 is the |
| 6719 | ** accumulator. |
| 6720 | ** |
| 6721 | ** The P5 arguments are taken from register P2 and its |
| 6722 | ** successors. |
| 6723 | */ |
| 6724 | /* Opcode: AggStep1 P1 P2 P3 P4 P5 |
drh | e2d9e7c | 2015-06-26 18:47:53 +0000 | [diff] [blame] | 6725 | ** Synopsis: accum=r[P3] step(r[P2@P5]) |
| 6726 | ** |
dan | 9a94722 | 2018-06-14 19:06:36 +0000 | [diff] [blame] | 6727 | ** Execute the xStep (if P1==0) or xInverse (if P1!=0) function for an |
| 6728 | ** aggregate. The function has P5 arguments. P4 is a pointer to the |
| 6729 | ** FuncDef structure that specifies the function. Register P3 is the |
| 6730 | ** accumulator. |
drh | e2d9e7c | 2015-06-26 18:47:53 +0000 | [diff] [blame] | 6731 | ** |
| 6732 | ** The P5 arguments are taken from register P2 and its |
| 6733 | ** successors. |
| 6734 | ** |
| 6735 | ** This opcode is initially coded as OP_AggStep0. On first evaluation, |
| 6736 | ** the FuncDef stored in P4 is converted into an sqlite3_context and |
| 6737 | ** the opcode is changed. In this way, the initialization of the |
| 6738 | ** sqlite3_context only happens once, instead of on each call to the |
| 6739 | ** step function. |
| 6740 | */ |
drh | 8f26da6 | 2018-07-05 21:22:57 +0000 | [diff] [blame] | 6741 | case OP_AggInverse: |
| 6742 | case OP_AggStep: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 6743 | int n; |
drh | 9c7c913 | 2015-06-26 18:16:52 +0000 | [diff] [blame] | 6744 | sqlite3_context *pCtx; |
drh | e509535 | 2002-02-24 03:25:14 +0000 | [diff] [blame] | 6745 | |
drh | 9c7c913 | 2015-06-26 18:16:52 +0000 | [diff] [blame] | 6746 | assert( pOp->p4type==P4_FUNCDEF ); |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 6747 | n = pOp->p5; |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 6748 | assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) ); |
| 6749 | 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] | 6750 | assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n ); |
drh | f09ac0b | 2018-01-23 03:44:06 +0000 | [diff] [blame] | 6751 | pCtx = sqlite3DbMallocRawNN(db, n*sizeof(sqlite3_value*) + |
| 6752 | (sizeof(pCtx[0]) + sizeof(Mem) - sizeof(sqlite3_value*))); |
drh | 9c7c913 | 2015-06-26 18:16:52 +0000 | [diff] [blame] | 6753 | if( pCtx==0 ) goto no_mem; |
| 6754 | pCtx->pMem = 0; |
drh | f09ac0b | 2018-01-23 03:44:06 +0000 | [diff] [blame] | 6755 | pCtx->pOut = (Mem*)&(pCtx->argv[n]); |
| 6756 | sqlite3VdbeMemInit(pCtx->pOut, db, MEM_Null); |
drh | 9c7c913 | 2015-06-26 18:16:52 +0000 | [diff] [blame] | 6757 | pCtx->pFunc = pOp->p4.pFunc; |
| 6758 | pCtx->iOp = (int)(pOp - aOp); |
| 6759 | pCtx->pVdbe = p; |
drh | f09ac0b | 2018-01-23 03:44:06 +0000 | [diff] [blame] | 6760 | pCtx->skipFlag = 0; |
| 6761 | pCtx->isError = 0; |
drh | 9c7c913 | 2015-06-26 18:16:52 +0000 | [diff] [blame] | 6762 | pCtx->argc = n; |
| 6763 | pOp->p4type = P4_FUNCCTX; |
| 6764 | pOp->p4.pCtx = pCtx; |
drh | 2c885d0 | 2018-07-07 19:36:04 +0000 | [diff] [blame] | 6765 | |
| 6766 | /* OP_AggInverse must have P1==1 and OP_AggStep must have P1==0 */ |
drh | 8f26da6 | 2018-07-05 21:22:57 +0000 | [diff] [blame] | 6767 | assert( pOp->p1==(pOp->opcode==OP_AggInverse) ); |
drh | 2c885d0 | 2018-07-07 19:36:04 +0000 | [diff] [blame] | 6768 | |
drh | 8f26da6 | 2018-07-05 21:22:57 +0000 | [diff] [blame] | 6769 | pOp->opcode = OP_AggStep1; |
drh | 9c7c913 | 2015-06-26 18:16:52 +0000 | [diff] [blame] | 6770 | /* Fall through into OP_AggStep */ |
| 6771 | } |
drh | 8f26da6 | 2018-07-05 21:22:57 +0000 | [diff] [blame] | 6772 | case OP_AggStep1: { |
drh | 9c7c913 | 2015-06-26 18:16:52 +0000 | [diff] [blame] | 6773 | int i; |
| 6774 | sqlite3_context *pCtx; |
| 6775 | Mem *pMem; |
drh | 9c7c913 | 2015-06-26 18:16:52 +0000 | [diff] [blame] | 6776 | |
| 6777 | assert( pOp->p4type==P4_FUNCCTX ); |
| 6778 | pCtx = pOp->p4.pCtx; |
| 6779 | pMem = &aMem[pOp->p3]; |
| 6780 | |
drh | 2c885d0 | 2018-07-07 19:36:04 +0000 | [diff] [blame] | 6781 | #ifdef SQLITE_DEBUG |
| 6782 | if( pOp->p1 ){ |
| 6783 | /* This is an OP_AggInverse call. Verify that xStep has always |
| 6784 | ** been called at least once prior to any xInverse call. */ |
| 6785 | assert( pMem->uTemp==0x1122e0e3 ); |
| 6786 | }else{ |
| 6787 | /* This is an OP_AggStep call. Mark it as such. */ |
| 6788 | pMem->uTemp = 0x1122e0e3; |
| 6789 | } |
| 6790 | #endif |
| 6791 | |
drh | 9c7c913 | 2015-06-26 18:16:52 +0000 | [diff] [blame] | 6792 | /* If this function is inside of a trigger, the register array in aMem[] |
| 6793 | ** might change from one evaluation to the next. The next block of code |
| 6794 | ** checks to see if the register array has changed, and if so it |
| 6795 | ** reinitializes the relavant parts of the sqlite3_context object */ |
| 6796 | if( pCtx->pMem != pMem ){ |
| 6797 | pCtx->pMem = pMem; |
| 6798 | for(i=pCtx->argc-1; i>=0; i--) pCtx->argv[i] = &aMem[pOp->p2+i]; |
| 6799 | } |
| 6800 | |
| 6801 | #ifdef SQLITE_DEBUG |
| 6802 | for(i=0; i<pCtx->argc; i++){ |
| 6803 | assert( memIsValid(pCtx->argv[i]) ); |
| 6804 | REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]); |
| 6805 | } |
| 6806 | #endif |
| 6807 | |
drh | abfcea2 | 2005-09-06 20:36:48 +0000 | [diff] [blame] | 6808 | pMem->n++; |
drh | f09ac0b | 2018-01-23 03:44:06 +0000 | [diff] [blame] | 6809 | assert( pCtx->pOut->flags==MEM_Null ); |
| 6810 | assert( pCtx->isError==0 ); |
| 6811 | assert( pCtx->skipFlag==0 ); |
dan | 67a9b8e | 2018-06-22 20:51:35 +0000 | [diff] [blame] | 6812 | #ifndef SQLITE_OMIT_WINDOWFUNC |
| 6813 | if( pOp->p1 ){ |
| 6814 | (pCtx->pFunc->xInverse)(pCtx,pCtx->argc,pCtx->argv); |
| 6815 | }else |
| 6816 | #endif |
| 6817 | (pCtx->pFunc->xSFunc)(pCtx,pCtx->argc,pCtx->argv); /* IMP: R-24505-23230 */ |
| 6818 | |
drh | f09ac0b | 2018-01-23 03:44:06 +0000 | [diff] [blame] | 6819 | if( pCtx->isError ){ |
| 6820 | if( pCtx->isError>0 ){ |
| 6821 | sqlite3VdbeError(p, "%s", sqlite3_value_text(pCtx->pOut)); |
drh | 9c7c913 | 2015-06-26 18:16:52 +0000 | [diff] [blame] | 6822 | rc = pCtx->isError; |
| 6823 | } |
drh | f09ac0b | 2018-01-23 03:44:06 +0000 | [diff] [blame] | 6824 | if( pCtx->skipFlag ){ |
| 6825 | assert( pOp[-1].opcode==OP_CollSeq ); |
| 6826 | i = pOp[-1].p1; |
| 6827 | if( i ) sqlite3VdbeMemSetInt64(&aMem[i], 1); |
| 6828 | pCtx->skipFlag = 0; |
| 6829 | } |
| 6830 | sqlite3VdbeMemRelease(pCtx->pOut); |
| 6831 | pCtx->pOut->flags = MEM_Null; |
| 6832 | pCtx->isError = 0; |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 6833 | if( rc ) goto abort_due_to_error; |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 6834 | } |
drh | f09ac0b | 2018-01-23 03:44:06 +0000 | [diff] [blame] | 6835 | assert( pCtx->pOut->flags==MEM_Null ); |
| 6836 | assert( pCtx->skipFlag==0 ); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 6837 | break; |
| 6838 | } |
| 6839 | |
drh | 8f26da6 | 2018-07-05 21:22:57 +0000 | [diff] [blame] | 6840 | /* Opcode: AggFinal P1 P2 * P4 * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 6841 | ** Synopsis: accum=r[P1] N=P2 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 6842 | ** |
dan | 9a94722 | 2018-06-14 19:06:36 +0000 | [diff] [blame] | 6843 | ** P1 is the memory location that is the accumulator for an aggregate |
drh | 8f26da6 | 2018-07-05 21:22:57 +0000 | [diff] [blame] | 6844 | ** or window function. Execute the finalizer function |
| 6845 | ** for an aggregate and store the result in P1. |
drh | a10a34b | 2005-09-07 22:09:48 +0000 | [diff] [blame] | 6846 | ** |
| 6847 | ** P2 is the number of arguments that the step function takes and |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 6848 | ** P4 is a pointer to the FuncDef for this function. The P2 |
drh | a10a34b | 2005-09-07 22:09:48 +0000 | [diff] [blame] | 6849 | ** argument is not used by this opcode. It is only there to disambiguate |
| 6850 | ** functions that can take varying numbers of arguments. The |
drh | 8be47a7 | 2018-07-05 20:05:29 +0000 | [diff] [blame] | 6851 | ** P4 argument is only needed for the case where |
drh | a10a34b | 2005-09-07 22:09:48 +0000 | [diff] [blame] | 6852 | ** the step function was not previously called. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 6853 | */ |
drh | 8f26da6 | 2018-07-05 21:22:57 +0000 | [diff] [blame] | 6854 | /* Opcode: AggValue * P2 P3 P4 * |
| 6855 | ** Synopsis: r[P3]=value N=P2 |
| 6856 | ** |
| 6857 | ** Invoke the xValue() function and store the result in register P3. |
| 6858 | ** |
| 6859 | ** P2 is the number of arguments that the step function takes and |
| 6860 | ** P4 is a pointer to the FuncDef for this function. The P2 |
| 6861 | ** argument is not used by this opcode. It is only there to disambiguate |
| 6862 | ** functions that can take varying numbers of arguments. The |
| 6863 | ** P4 argument is only needed for the case where |
| 6864 | ** the step function was not previously called. |
| 6865 | */ |
| 6866 | case OP_AggValue: |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 6867 | case OP_AggFinal: { |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 6868 | Mem *pMem; |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 6869 | assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) ); |
drh | 8f26da6 | 2018-07-05 21:22:57 +0000 | [diff] [blame] | 6870 | assert( pOp->p3==0 || pOp->opcode==OP_AggValue ); |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 6871 | pMem = &aMem[pOp->p1]; |
drh | a10a34b | 2005-09-07 22:09:48 +0000 | [diff] [blame] | 6872 | assert( (pMem->flags & ~(MEM_Null|MEM_Agg))==0 ); |
dan | 67a9b8e | 2018-06-22 20:51:35 +0000 | [diff] [blame] | 6873 | #ifndef SQLITE_OMIT_WINDOWFUNC |
dan | 86fb6e1 | 2018-05-16 20:58:07 +0000 | [diff] [blame] | 6874 | if( pOp->p3 ){ |
dan | 108e6b2 | 2019-03-18 18:55:35 +0000 | [diff] [blame] | 6875 | memAboutToChange(p, &aMem[pOp->p3]); |
dan | 86fb6e1 | 2018-05-16 20:58:07 +0000 | [diff] [blame] | 6876 | rc = sqlite3VdbeMemAggValue(pMem, &aMem[pOp->p3], pOp->p4.pFunc); |
dan | 660af93 | 2018-06-18 16:55:22 +0000 | [diff] [blame] | 6877 | pMem = &aMem[pOp->p3]; |
dan | 67a9b8e | 2018-06-22 20:51:35 +0000 | [diff] [blame] | 6878 | }else |
| 6879 | #endif |
drh | 8f26da6 | 2018-07-05 21:22:57 +0000 | [diff] [blame] | 6880 | { |
| 6881 | rc = sqlite3VdbeMemFinalize(pMem, pOp->p4.pFunc); |
| 6882 | } |
dan | 67a9b8e | 2018-06-22 20:51:35 +0000 | [diff] [blame] | 6883 | |
drh | 4c8555f | 2009-06-25 01:47:11 +0000 | [diff] [blame] | 6884 | if( rc ){ |
drh | 22c17b8 | 2015-05-15 04:13:15 +0000 | [diff] [blame] | 6885 | sqlite3VdbeError(p, "%s", sqlite3_value_text(pMem)); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 6886 | goto abort_due_to_error; |
drh | 90669c1 | 2006-01-20 15:45:36 +0000 | [diff] [blame] | 6887 | } |
drh | 2dca868 | 2008-03-21 17:13:13 +0000 | [diff] [blame] | 6888 | sqlite3VdbeChangeEncoding(pMem, encoding); |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 6889 | UPDATE_MAX_BLOBSIZE(pMem); |
drh | 023ae03 | 2007-05-08 12:12:16 +0000 | [diff] [blame] | 6890 | if( sqlite3VdbeMemTooBig(pMem) ){ |
| 6891 | goto too_big; |
| 6892 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 6893 | break; |
| 6894 | } |
| 6895 | |
dan | 5cf5353 | 2010-05-01 16:40:20 +0000 | [diff] [blame] | 6896 | #ifndef SQLITE_OMIT_WAL |
dan | cdc1f04 | 2010-11-18 12:11:05 +0000 | [diff] [blame] | 6897 | /* Opcode: Checkpoint P1 P2 P3 * * |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 6898 | ** |
| 6899 | ** 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] | 6900 | ** WAL mode. Parameter P2 is one of SQLITE_CHECKPOINT_PASSIVE, FULL, |
| 6901 | ** 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] | 6902 | ** SQLITE_BUSY or not, respectively. Write the number of pages in the |
| 6903 | ** WAL after the checkpoint into mem[P3+1] and the number of pages |
| 6904 | ** in the WAL that have been checkpointed after the checkpoint |
| 6905 | ** completes into mem[P3+2]. However on an error, mem[P3+1] and |
| 6906 | ** mem[P3+2] are initialized to -1. |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 6907 | */ |
| 6908 | case OP_Checkpoint: { |
drh | 30aa3b9 | 2011-02-07 23:56:01 +0000 | [diff] [blame] | 6909 | int i; /* Loop counter */ |
| 6910 | int aRes[3]; /* Results */ |
| 6911 | Mem *pMem; /* Write results here */ |
| 6912 | |
drh | 9e92a47 | 2013-06-27 17:40:30 +0000 | [diff] [blame] | 6913 | assert( p->readOnly==0 ); |
drh | 30aa3b9 | 2011-02-07 23:56:01 +0000 | [diff] [blame] | 6914 | aRes[0] = 0; |
| 6915 | aRes[1] = aRes[2] = -1; |
dan | cdc1f04 | 2010-11-18 12:11:05 +0000 | [diff] [blame] | 6916 | assert( pOp->p2==SQLITE_CHECKPOINT_PASSIVE |
| 6917 | || pOp->p2==SQLITE_CHECKPOINT_FULL |
| 6918 | || pOp->p2==SQLITE_CHECKPOINT_RESTART |
dan | f26a154 | 2014-12-02 19:04:54 +0000 | [diff] [blame] | 6919 | || pOp->p2==SQLITE_CHECKPOINT_TRUNCATE |
dan | cdc1f04 | 2010-11-18 12:11:05 +0000 | [diff] [blame] | 6920 | ); |
drh | 30aa3b9 | 2011-02-07 23:56:01 +0000 | [diff] [blame] | 6921 | rc = sqlite3Checkpoint(db, pOp->p1, pOp->p2, &aRes[1], &aRes[2]); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 6922 | if( rc ){ |
| 6923 | if( rc!=SQLITE_BUSY ) goto abort_due_to_error; |
dan | cdc1f04 | 2010-11-18 12:11:05 +0000 | [diff] [blame] | 6924 | rc = SQLITE_OK; |
drh | 30aa3b9 | 2011-02-07 23:56:01 +0000 | [diff] [blame] | 6925 | aRes[0] = 1; |
dan | cdc1f04 | 2010-11-18 12:11:05 +0000 | [diff] [blame] | 6926 | } |
drh | 30aa3b9 | 2011-02-07 23:56:01 +0000 | [diff] [blame] | 6927 | for(i=0, pMem = &aMem[pOp->p3]; i<3; i++, pMem++){ |
| 6928 | sqlite3VdbeMemSetInt64(pMem, (i64)aRes[i]); |
| 6929 | } |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 6930 | break; |
| 6931 | }; |
dan | 5cf5353 | 2010-05-01 16:40:20 +0000 | [diff] [blame] | 6932 | #endif |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 6933 | |
drh | cac29a6 | 2010-07-02 19:36:52 +0000 | [diff] [blame] | 6934 | #ifndef SQLITE_OMIT_PRAGMA |
drh | 0fd6135 | 2014-02-07 02:29:45 +0000 | [diff] [blame] | 6935 | /* Opcode: JournalMode P1 P2 P3 * * |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 6936 | ** |
| 6937 | ** Change the journal mode of database P1 to P3. P3 must be one of the |
| 6938 | ** PAGER_JOURNALMODE_XXX values. If changing between the various rollback |
| 6939 | ** modes (delete, truncate, persist, off and memory), this is a simple |
| 6940 | ** operation. No IO is required. |
| 6941 | ** |
| 6942 | ** If changing into or out of WAL mode the procedure is more complicated. |
| 6943 | ** |
| 6944 | ** Write a string containing the final journal-mode to register P2. |
| 6945 | */ |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 6946 | case OP_JournalMode: { /* out2 */ |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 6947 | Btree *pBt; /* Btree to change journal mode of */ |
| 6948 | Pager *pPager; /* Pager associated with pBt */ |
drh | d80b233 | 2010-05-01 00:59:37 +0000 | [diff] [blame] | 6949 | int eNew; /* New journal mode */ |
| 6950 | int eOld; /* The old journal mode */ |
mistachkin | 59ee77c | 2012-09-13 15:26:44 +0000 | [diff] [blame] | 6951 | #ifndef SQLITE_OMIT_WAL |
drh | d80b233 | 2010-05-01 00:59:37 +0000 | [diff] [blame] | 6952 | const char *zFilename; /* Name of database file for pPager */ |
mistachkin | 59ee77c | 2012-09-13 15:26:44 +0000 | [diff] [blame] | 6953 | #endif |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 6954 | |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 6955 | pOut = out2Prerelease(p, pOp); |
drh | d80b233 | 2010-05-01 00:59:37 +0000 | [diff] [blame] | 6956 | eNew = pOp->p3; |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 6957 | assert( eNew==PAGER_JOURNALMODE_DELETE |
| 6958 | || eNew==PAGER_JOURNALMODE_TRUNCATE |
| 6959 | || eNew==PAGER_JOURNALMODE_PERSIST |
| 6960 | || eNew==PAGER_JOURNALMODE_OFF |
| 6961 | || eNew==PAGER_JOURNALMODE_MEMORY |
| 6962 | || eNew==PAGER_JOURNALMODE_WAL |
| 6963 | || eNew==PAGER_JOURNALMODE_QUERY |
| 6964 | ); |
| 6965 | assert( pOp->p1>=0 && pOp->p1<db->nDb ); |
drh | 9e92a47 | 2013-06-27 17:40:30 +0000 | [diff] [blame] | 6966 | assert( p->readOnly==0 ); |
drh | 3ebaee9 | 2010-05-06 21:37:22 +0000 | [diff] [blame] | 6967 | |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 6968 | pBt = db->aDb[pOp->p1].pBt; |
| 6969 | pPager = sqlite3BtreePager(pBt); |
drh | 0b9b430 | 2010-06-11 17:01:24 +0000 | [diff] [blame] | 6970 | eOld = sqlite3PagerGetJournalMode(pPager); |
| 6971 | if( eNew==PAGER_JOURNALMODE_QUERY ) eNew = eOld; |
| 6972 | if( !sqlite3PagerOkToChangeJournalMode(pPager) ) eNew = eOld; |
dan | 5cf5353 | 2010-05-01 16:40:20 +0000 | [diff] [blame] | 6973 | |
| 6974 | #ifndef SQLITE_OMIT_WAL |
drh | d4e0bb0 | 2012-05-27 01:19:04 +0000 | [diff] [blame] | 6975 | zFilename = sqlite3PagerFilename(pPager, 1); |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 6976 | |
drh | d80b233 | 2010-05-01 00:59:37 +0000 | [diff] [blame] | 6977 | /* Do not allow a transition to journal_mode=WAL for a database |
drh | 6e1f482 | 2010-07-13 23:41:40 +0000 | [diff] [blame] | 6978 | ** in temporary storage or if the VFS does not support shared memory |
drh | d80b233 | 2010-05-01 00:59:37 +0000 | [diff] [blame] | 6979 | */ |
| 6980 | if( eNew==PAGER_JOURNALMODE_WAL |
drh | 057fc81 | 2011-10-17 23:15:31 +0000 | [diff] [blame] | 6981 | && (sqlite3Strlen30(zFilename)==0 /* Temp file */ |
drh | 6e1f482 | 2010-07-13 23:41:40 +0000 | [diff] [blame] | 6982 | || !sqlite3PagerWalSupported(pPager)) /* No shared-memory support */ |
dan | e180c29 | 2010-04-26 17:42:56 +0000 | [diff] [blame] | 6983 | ){ |
drh | 0b9b430 | 2010-06-11 17:01:24 +0000 | [diff] [blame] | 6984 | eNew = eOld; |
dan | e180c29 | 2010-04-26 17:42:56 +0000 | [diff] [blame] | 6985 | } |
| 6986 | |
drh | 0b9b430 | 2010-06-11 17:01:24 +0000 | [diff] [blame] | 6987 | if( (eNew!=eOld) |
| 6988 | && (eOld==PAGER_JOURNALMODE_WAL || eNew==PAGER_JOURNALMODE_WAL) |
| 6989 | ){ |
dan | c0537fe | 2013-06-28 19:41:43 +0000 | [diff] [blame] | 6990 | if( !db->autoCommit || db->nVdbeRead>1 ){ |
drh | 0b9b430 | 2010-06-11 17:01:24 +0000 | [diff] [blame] | 6991 | rc = SQLITE_ERROR; |
drh | 22c17b8 | 2015-05-15 04:13:15 +0000 | [diff] [blame] | 6992 | sqlite3VdbeError(p, |
drh | 0b9b430 | 2010-06-11 17:01:24 +0000 | [diff] [blame] | 6993 | "cannot change %s wal mode from within a transaction", |
| 6994 | (eNew==PAGER_JOURNALMODE_WAL ? "into" : "out of") |
| 6995 | ); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 6996 | goto abort_due_to_error; |
drh | 0b9b430 | 2010-06-11 17:01:24 +0000 | [diff] [blame] | 6997 | }else{ |
| 6998 | |
| 6999 | if( eOld==PAGER_JOURNALMODE_WAL ){ |
| 7000 | /* If leaving WAL mode, close the log file. If successful, the call |
| 7001 | ** to PagerCloseWal() checkpoints and deletes the write-ahead-log |
| 7002 | ** file. An EXCLUSIVE lock may still be held on the database file |
| 7003 | ** after a successful return. |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 7004 | */ |
dan | 7fb8990 | 2016-08-12 16:21:15 +0000 | [diff] [blame] | 7005 | rc = sqlite3PagerCloseWal(pPager, db); |
drh | ab9b744 | 2010-05-10 11:20:05 +0000 | [diff] [blame] | 7006 | if( rc==SQLITE_OK ){ |
drh | 0b9b430 | 2010-06-11 17:01:24 +0000 | [diff] [blame] | 7007 | sqlite3PagerSetJournalMode(pPager, eNew); |
drh | 89c3f2f | 2010-05-15 01:09:38 +0000 | [diff] [blame] | 7008 | } |
drh | 242c4f7 | 2010-06-22 14:49:39 +0000 | [diff] [blame] | 7009 | }else if( eOld==PAGER_JOURNALMODE_MEMORY ){ |
| 7010 | /* Cannot transition directly from MEMORY to WAL. Use mode OFF |
| 7011 | ** as an intermediate */ |
| 7012 | sqlite3PagerSetJournalMode(pPager, PAGER_JOURNALMODE_OFF); |
drh | 0b9b430 | 2010-06-11 17:01:24 +0000 | [diff] [blame] | 7013 | } |
| 7014 | |
| 7015 | /* Open a transaction on the database file. Regardless of the journal |
| 7016 | ** mode, this transaction always uses a rollback journal. |
| 7017 | */ |
| 7018 | assert( sqlite3BtreeIsInTrans(pBt)==0 ); |
| 7019 | if( rc==SQLITE_OK ){ |
dan | 731bf5b | 2010-06-17 16:44:21 +0000 | [diff] [blame] | 7020 | rc = sqlite3BtreeSetVersion(pBt, (eNew==PAGER_JOURNALMODE_WAL ? 2 : 1)); |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 7021 | } |
| 7022 | } |
| 7023 | } |
dan | 5cf5353 | 2010-05-01 16:40:20 +0000 | [diff] [blame] | 7024 | #endif /* ifndef SQLITE_OMIT_WAL */ |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 7025 | |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 7026 | if( rc ) eNew = eOld; |
drh | 0b9b430 | 2010-06-11 17:01:24 +0000 | [diff] [blame] | 7027 | eNew = sqlite3PagerSetJournalMode(pPager, eNew); |
dan | 731bf5b | 2010-06-17 16:44:21 +0000 | [diff] [blame] | 7028 | |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 7029 | pOut->flags = MEM_Str|MEM_Static|MEM_Term; |
dan | b978002 | 2010-04-21 18:37:57 +0000 | [diff] [blame] | 7030 | pOut->z = (char *)sqlite3JournalModename(eNew); |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 7031 | pOut->n = sqlite3Strlen30(pOut->z); |
| 7032 | pOut->enc = SQLITE_UTF8; |
| 7033 | sqlite3VdbeChangeEncoding(pOut, encoding); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 7034 | if( rc ) goto abort_due_to_error; |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 7035 | break; |
drh | cac29a6 | 2010-07-02 19:36:52 +0000 | [diff] [blame] | 7036 | }; |
| 7037 | #endif /* SQLITE_OMIT_PRAGMA */ |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 7038 | |
drh | fdbcdee | 2007-03-27 14:44:50 +0000 | [diff] [blame] | 7039 | #if !defined(SQLITE_OMIT_VACUUM) && !defined(SQLITE_OMIT_ATTACH) |
drh | 2f6239e | 2018-12-08 00:43:08 +0000 | [diff] [blame] | 7040 | /* Opcode: Vacuum P1 P2 * * * |
drh | 6f8c91c | 2003-12-07 00:24:35 +0000 | [diff] [blame] | 7041 | ** |
drh | 9ef5e77 | 2016-08-19 14:20:56 +0000 | [diff] [blame] | 7042 | ** Vacuum the entire database P1. P1 is 0 for "main", and 2 or more |
| 7043 | ** for an attached database. The "temp" database may not be vacuumed. |
drh | b0b7db9 | 2018-12-07 17:28:28 +0000 | [diff] [blame] | 7044 | ** |
drh | 2f6239e | 2018-12-08 00:43:08 +0000 | [diff] [blame] | 7045 | ** If P2 is not zero, then it is a register holding a string which is |
| 7046 | ** the file into which the result of vacuum should be written. When |
| 7047 | ** P2 is zero, the vacuum overwrites the original database. |
drh | 6f8c91c | 2003-12-07 00:24:35 +0000 | [diff] [blame] | 7048 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 7049 | case OP_Vacuum: { |
drh | 9e92a47 | 2013-06-27 17:40:30 +0000 | [diff] [blame] | 7050 | assert( p->readOnly==0 ); |
drh | 2f6239e | 2018-12-08 00:43:08 +0000 | [diff] [blame] | 7051 | rc = sqlite3RunVacuum(&p->zErrMsg, db, pOp->p1, |
| 7052 | pOp->p2 ? &aMem[pOp->p2] : 0); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 7053 | if( rc ) goto abort_due_to_error; |
drh | 6f8c91c | 2003-12-07 00:24:35 +0000 | [diff] [blame] | 7054 | break; |
| 7055 | } |
drh | 154d4b2 | 2006-09-21 11:02:16 +0000 | [diff] [blame] | 7056 | #endif |
drh | 6f8c91c | 2003-12-07 00:24:35 +0000 | [diff] [blame] | 7057 | |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 7058 | #if !defined(SQLITE_OMIT_AUTOVACUUM) |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 7059 | /* Opcode: IncrVacuum P1 P2 * * * |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 7060 | ** |
| 7061 | ** Perform a single step of the incremental vacuum procedure on |
drh | ca5557f | 2007-05-04 18:30:40 +0000 | [diff] [blame] | 7062 | ** the P1 database. If the vacuum has finished, jump to instruction |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 7063 | ** P2. Otherwise, fall through to the next instruction. |
| 7064 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 7065 | case OP_IncrVacuum: { /* jump */ |
drh | ca5557f | 2007-05-04 18:30:40 +0000 | [diff] [blame] | 7066 | Btree *pBt; |
| 7067 | |
| 7068 | assert( pOp->p1>=0 && pOp->p1<db->nDb ); |
drh | a7ab6d8 | 2014-07-21 15:44:39 +0000 | [diff] [blame] | 7069 | assert( DbMaskTest(p->btreeMask, pOp->p1) ); |
drh | 9e92a47 | 2013-06-27 17:40:30 +0000 | [diff] [blame] | 7070 | assert( p->readOnly==0 ); |
drh | ca5557f | 2007-05-04 18:30:40 +0000 | [diff] [blame] | 7071 | pBt = db->aDb[pOp->p1].pBt; |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 7072 | rc = sqlite3BtreeIncrVacuum(pBt); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 7073 | VdbeBranchTaken(rc==SQLITE_DONE,2); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 7074 | if( rc ){ |
| 7075 | if( rc!=SQLITE_DONE ) goto abort_due_to_error; |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 7076 | rc = SQLITE_OK; |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 7077 | goto jump_to_p2; |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 7078 | } |
| 7079 | break; |
| 7080 | } |
| 7081 | #endif |
| 7082 | |
drh | ba968db | 2018-07-24 22:02:12 +0000 | [diff] [blame] | 7083 | /* Opcode: Expire P1 P2 * * * |
danielk1977 | a21c6b6 | 2005-01-24 10:25:59 +0000 | [diff] [blame] | 7084 | ** |
drh | 25df48d | 2014-07-22 14:58:12 +0000 | [diff] [blame] | 7085 | ** Cause precompiled statements to expire. When an expired statement |
| 7086 | ** is executed using sqlite3_step() it will either automatically |
| 7087 | ** reprepare itself (if it was originally created using sqlite3_prepare_v2()) |
| 7088 | ** or it will fail with SQLITE_SCHEMA. |
danielk1977 | a21c6b6 | 2005-01-24 10:25:59 +0000 | [diff] [blame] | 7089 | ** |
| 7090 | ** 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] | 7091 | ** then only the currently executing statement is expired. |
drh | ba968db | 2018-07-24 22:02:12 +0000 | [diff] [blame] | 7092 | ** |
| 7093 | ** If P2 is 0, then SQL statements are expired immediately. If P2 is 1, |
| 7094 | ** then running SQL statements are allowed to continue to run to completion. |
| 7095 | ** The P2==1 case occurs when a CREATE INDEX or similar schema change happens |
| 7096 | ** that might help the statement run faster but which does not affect the |
| 7097 | ** correctness of operation. |
danielk1977 | a21c6b6 | 2005-01-24 10:25:59 +0000 | [diff] [blame] | 7098 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 7099 | case OP_Expire: { |
drh | ba968db | 2018-07-24 22:02:12 +0000 | [diff] [blame] | 7100 | assert( pOp->p2==0 || pOp->p2==1 ); |
danielk1977 | a21c6b6 | 2005-01-24 10:25:59 +0000 | [diff] [blame] | 7101 | if( !pOp->p1 ){ |
drh | ba968db | 2018-07-24 22:02:12 +0000 | [diff] [blame] | 7102 | sqlite3ExpirePreparedStatements(db, pOp->p2); |
danielk1977 | a21c6b6 | 2005-01-24 10:25:59 +0000 | [diff] [blame] | 7103 | }else{ |
drh | ba968db | 2018-07-24 22:02:12 +0000 | [diff] [blame] | 7104 | p->expired = pOp->p2+1; |
danielk1977 | a21c6b6 | 2005-01-24 10:25:59 +0000 | [diff] [blame] | 7105 | } |
| 7106 | break; |
| 7107 | } |
| 7108 | |
drh | 7b14b65 | 2019-12-29 22:08:20 +0000 | [diff] [blame] | 7109 | /* Opcode: CursorLock P1 * * * * |
| 7110 | ** |
| 7111 | ** Lock the btree to which cursor P1 is pointing so that the btree cannot be |
| 7112 | ** written by an other cursor. |
| 7113 | */ |
| 7114 | case OP_CursorLock: { |
| 7115 | VdbeCursor *pC; |
| 7116 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 7117 | pC = p->apCsr[pOp->p1]; |
| 7118 | assert( pC!=0 ); |
| 7119 | assert( pC->eCurType==CURTYPE_BTREE ); |
| 7120 | sqlite3BtreeCursorPin(pC->uc.pCursor); |
| 7121 | break; |
| 7122 | } |
| 7123 | |
| 7124 | /* Opcode: CursorUnlock P1 * * * * |
| 7125 | ** |
| 7126 | ** Unlock the btree to which cursor P1 is pointing so that it can be |
| 7127 | ** written by other cursors. |
| 7128 | */ |
| 7129 | case OP_CursorUnlock: { |
| 7130 | VdbeCursor *pC; |
| 7131 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 7132 | pC = p->apCsr[pOp->p1]; |
| 7133 | assert( pC!=0 ); |
| 7134 | assert( pC->eCurType==CURTYPE_BTREE ); |
| 7135 | sqlite3BtreeCursorUnpin(pC->uc.pCursor); |
| 7136 | break; |
| 7137 | } |
| 7138 | |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 7139 | #ifndef SQLITE_OMIT_SHARED_CACHE |
drh | 6a9ad3d | 2008-04-02 16:29:30 +0000 | [diff] [blame] | 7140 | /* Opcode: TableLock P1 P2 P3 P4 * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 7141 | ** Synopsis: iDb=P1 root=P2 write=P3 |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 7142 | ** |
| 7143 | ** Obtain a lock on a particular table. This instruction is only used when |
| 7144 | ** the shared-cache feature is enabled. |
| 7145 | ** |
danielk1977 | 96d48e9 | 2009-06-29 06:00:37 +0000 | [diff] [blame] | 7146 | ** P1 is the index of the database in sqlite3.aDb[] of the database |
drh | 6a9ad3d | 2008-04-02 16:29:30 +0000 | [diff] [blame] | 7147 | ** on which the lock is acquired. A readlock is obtained if P3==0 or |
| 7148 | ** a write lock if P3==1. |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 7149 | ** |
| 7150 | ** P2 contains the root-page of the table to lock. |
| 7151 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 7152 | ** 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] | 7153 | ** used to generate an error message if the lock cannot be obtained. |
| 7154 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 7155 | case OP_TableLock: { |
danielk1977 | e0d9e6f | 2009-07-03 16:25:06 +0000 | [diff] [blame] | 7156 | u8 isWriteLock = (u8)pOp->p3; |
drh | 169dd92 | 2017-06-26 13:57:49 +0000 | [diff] [blame] | 7157 | if( isWriteLock || 0==(db->flags&SQLITE_ReadUncommit) ){ |
danielk1977 | e0d9e6f | 2009-07-03 16:25:06 +0000 | [diff] [blame] | 7158 | int p1 = pOp->p1; |
| 7159 | assert( p1>=0 && p1<db->nDb ); |
drh | a7ab6d8 | 2014-07-21 15:44:39 +0000 | [diff] [blame] | 7160 | assert( DbMaskTest(p->btreeMask, p1) ); |
danielk1977 | e0d9e6f | 2009-07-03 16:25:06 +0000 | [diff] [blame] | 7161 | assert( isWriteLock==0 || isWriteLock==1 ); |
| 7162 | rc = sqlite3BtreeLockTable(db->aDb[p1].pBt, pOp->p2, isWriteLock); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 7163 | if( rc ){ |
| 7164 | if( (rc&0xFF)==SQLITE_LOCKED ){ |
| 7165 | const char *z = pOp->p4.z; |
| 7166 | sqlite3VdbeError(p, "database table is locked: %s", z); |
| 7167 | } |
| 7168 | goto abort_due_to_error; |
danielk1977 | e0d9e6f | 2009-07-03 16:25:06 +0000 | [diff] [blame] | 7169 | } |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 7170 | } |
| 7171 | break; |
| 7172 | } |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 7173 | #endif /* SQLITE_OMIT_SHARED_CACHE */ |
| 7174 | |
| 7175 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 7176 | /* Opcode: VBegin * * * P4 * |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 7177 | ** |
danielk1977 | 3e3a84d | 2008-08-01 17:37:40 +0000 | [diff] [blame] | 7178 | ** P4 may be a pointer to an sqlite3_vtab structure. If so, call the |
| 7179 | ** xBegin method for that table. |
| 7180 | ** |
| 7181 | ** 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] | 7182 | ** within a callback to a virtual table xSync() method. If it is, the error |
| 7183 | ** code will be set to SQLITE_LOCKED. |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 7184 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 7185 | case OP_VBegin: { |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 7186 | VTable *pVTab; |
| 7187 | pVTab = pOp->p4.pVtab; |
| 7188 | rc = sqlite3VtabBegin(db, pVTab); |
dan | 016f781 | 2013-08-21 17:35:48 +0000 | [diff] [blame] | 7189 | if( pVTab ) sqlite3VtabImportErrmsg(p, pVTab->pVtab); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 7190 | if( rc ) goto abort_due_to_error; |
danielk1977 | f9e7dda | 2006-06-16 16:08:53 +0000 | [diff] [blame] | 7191 | break; |
| 7192 | } |
| 7193 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 7194 | |
| 7195 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
dan | 7377945 | 2015-03-19 18:56:17 +0000 | [diff] [blame] | 7196 | /* Opcode: VCreate P1 P2 * * * |
danielk1977 | f9e7dda | 2006-06-16 16:08:53 +0000 | [diff] [blame] | 7197 | ** |
dan | 7377945 | 2015-03-19 18:56:17 +0000 | [diff] [blame] | 7198 | ** P2 is a register that holds the name of a virtual table in database |
| 7199 | ** P1. Call the xCreate method for that table. |
danielk1977 | f9e7dda | 2006-06-16 16:08:53 +0000 | [diff] [blame] | 7200 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 7201 | case OP_VCreate: { |
dan | 7377945 | 2015-03-19 18:56:17 +0000 | [diff] [blame] | 7202 | Mem sMem; /* For storing the record being decoded */ |
drh | 4746406 | 2015-03-21 12:22:16 +0000 | [diff] [blame] | 7203 | const char *zTab; /* Name of the virtual table */ |
| 7204 | |
dan | 7377945 | 2015-03-19 18:56:17 +0000 | [diff] [blame] | 7205 | memset(&sMem, 0, sizeof(sMem)); |
| 7206 | sMem.db = db; |
drh | 4746406 | 2015-03-21 12:22:16 +0000 | [diff] [blame] | 7207 | /* Because P2 is always a static string, it is impossible for the |
| 7208 | ** sqlite3VdbeMemCopy() to fail */ |
| 7209 | assert( (aMem[pOp->p2].flags & MEM_Str)!=0 ); |
| 7210 | assert( (aMem[pOp->p2].flags & MEM_Static)!=0 ); |
dan | 7377945 | 2015-03-19 18:56:17 +0000 | [diff] [blame] | 7211 | rc = sqlite3VdbeMemCopy(&sMem, &aMem[pOp->p2]); |
drh | 4746406 | 2015-03-21 12:22:16 +0000 | [diff] [blame] | 7212 | assert( rc==SQLITE_OK ); |
| 7213 | zTab = (const char*)sqlite3_value_text(&sMem); |
| 7214 | assert( zTab || db->mallocFailed ); |
| 7215 | if( zTab ){ |
| 7216 | rc = sqlite3VtabCallCreate(db, pOp->p1, zTab, &p->zErrMsg); |
dan | 7377945 | 2015-03-19 18:56:17 +0000 | [diff] [blame] | 7217 | } |
| 7218 | sqlite3VdbeMemRelease(&sMem); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 7219 | if( rc ) goto abort_due_to_error; |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 7220 | break; |
| 7221 | } |
| 7222 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 7223 | |
| 7224 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 7225 | /* Opcode: VDestroy P1 * * P4 * |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 7226 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 7227 | ** 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] | 7228 | ** of that table. |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 7229 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 7230 | case OP_VDestroy: { |
drh | 086723a | 2015-03-24 12:51:52 +0000 | [diff] [blame] | 7231 | db->nVDestroy++; |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 7232 | rc = sqlite3VtabCallDestroy(db, pOp->p1, pOp->p4.z); |
drh | 086723a | 2015-03-24 12:51:52 +0000 | [diff] [blame] | 7233 | db->nVDestroy--; |
dan | 1d4b164 | 2018-12-28 17:45:08 +0000 | [diff] [blame] | 7234 | assert( p->errorAction==OE_Abort && p->usesStmtJournal ); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 7235 | if( rc ) goto abort_due_to_error; |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 7236 | break; |
| 7237 | } |
| 7238 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 7239 | |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 7240 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 7241 | /* Opcode: VOpen P1 * * P4 * |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 7242 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 7243 | ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure. |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 7244 | ** P1 is a cursor number. This opcode opens a cursor to the virtual |
| 7245 | ** table and stores that cursor in P1. |
| 7246 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 7247 | case OP_VOpen: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 7248 | VdbeCursor *pCur; |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 7249 | sqlite3_vtab_cursor *pVCur; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 7250 | sqlite3_vtab *pVtab; |
drh | f496a7d | 2015-03-24 14:05:50 +0000 | [diff] [blame] | 7251 | const sqlite3_module *pModule; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 7252 | |
drh | 1713afb | 2013-06-28 01:24:57 +0000 | [diff] [blame] | 7253 | assert( p->bIsReader ); |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 7254 | pCur = 0; |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 7255 | pVCur = 0; |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 7256 | pVtab = pOp->p4.pVtab->pVtab; |
drh | f496a7d | 2015-03-24 14:05:50 +0000 | [diff] [blame] | 7257 | if( pVtab==0 || NEVER(pVtab->pModule==0) ){ |
| 7258 | rc = SQLITE_LOCKED; |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 7259 | goto abort_due_to_error; |
drh | f496a7d | 2015-03-24 14:05:50 +0000 | [diff] [blame] | 7260 | } |
| 7261 | pModule = pVtab->pModule; |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 7262 | rc = pModule->xOpen(pVtab, &pVCur); |
dan | 016f781 | 2013-08-21 17:35:48 +0000 | [diff] [blame] | 7263 | sqlite3VtabImportErrmsg(p, pVtab); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 7264 | if( rc ) goto abort_due_to_error; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 7265 | |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 7266 | /* Initialize sqlite3_vtab_cursor base class */ |
| 7267 | pVCur->pVtab = pVtab; |
| 7268 | |
| 7269 | /* Initialize vdbe cursor object */ |
| 7270 | pCur = allocateCursor(p, pOp->p1, 0, -1, CURTYPE_VTAB); |
| 7271 | if( pCur ){ |
| 7272 | pCur->uc.pVCur = pVCur; |
| 7273 | pVtab->nRef++; |
| 7274 | }else{ |
| 7275 | assert( db->mallocFailed ); |
| 7276 | pModule->xClose(pVCur); |
| 7277 | goto no_mem; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 7278 | } |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 7279 | break; |
| 7280 | } |
| 7281 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 7282 | |
| 7283 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
danielk1977 | 6dbee81 | 2008-01-03 18:39:41 +0000 | [diff] [blame] | 7284 | /* Opcode: VFilter P1 P2 P3 P4 * |
drh | 831116d | 2014-04-03 14:31:00 +0000 | [diff] [blame] | 7285 | ** Synopsis: iplan=r[P3] zplan='P4' |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 7286 | ** |
| 7287 | ** P1 is a cursor opened using VOpen. P2 is an address to jump to if |
| 7288 | ** the filtered result set is empty. |
| 7289 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 7290 | ** P4 is either NULL or a string that was generated by the xBestIndex |
| 7291 | ** method of the module. The interpretation of the P4 string is left |
drh | 4be8b51 | 2006-06-13 23:51:34 +0000 | [diff] [blame] | 7292 | ** to the module implementation. |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 7293 | ** |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 7294 | ** This opcode invokes the xFilter method on the virtual table specified |
danielk1977 | 6dbee81 | 2008-01-03 18:39:41 +0000 | [diff] [blame] | 7295 | ** by P1. The integer query plan parameter to xFilter is stored in register |
| 7296 | ** P3. Register P3+1 stores the argc parameter to be passed to the |
drh | 174edc6 | 2008-05-29 05:23:41 +0000 | [diff] [blame] | 7297 | ** xFilter method. Registers P3+2..P3+1+argc are the argc |
| 7298 | ** additional parameters which are passed to |
danielk1977 | 6dbee81 | 2008-01-03 18:39:41 +0000 | [diff] [blame] | 7299 | ** xFilter as argv. Register P3+2 becomes argv[0] when passed to xFilter. |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 7300 | ** |
danielk1977 | 6dbee81 | 2008-01-03 18:39:41 +0000 | [diff] [blame] | 7301 | ** 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] | 7302 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 7303 | case OP_VFilter: { /* jump */ |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 7304 | int nArg; |
danielk1977 | 6dbee81 | 2008-01-03 18:39:41 +0000 | [diff] [blame] | 7305 | int iQuery; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 7306 | const sqlite3_module *pModule; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 7307 | Mem *pQuery; |
| 7308 | Mem *pArgc; |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 7309 | sqlite3_vtab_cursor *pVCur; |
drh | 4dc754d | 2008-07-23 18:17:32 +0000 | [diff] [blame] | 7310 | sqlite3_vtab *pVtab; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 7311 | VdbeCursor *pCur; |
| 7312 | int res; |
| 7313 | int i; |
| 7314 | Mem **apArg; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 7315 | |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 7316 | pQuery = &aMem[pOp->p3]; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 7317 | pArgc = &pQuery[1]; |
| 7318 | pCur = p->apCsr[pOp->p1]; |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 7319 | assert( memIsValid(pQuery) ); |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 7320 | REGISTER_TRACE(pOp->p3, pQuery); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 7321 | assert( pCur->eCurType==CURTYPE_VTAB ); |
| 7322 | pVCur = pCur->uc.pVCur; |
| 7323 | pVtab = pVCur->pVtab; |
drh | 4dc754d | 2008-07-23 18:17:32 +0000 | [diff] [blame] | 7324 | pModule = pVtab->pModule; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 7325 | |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 7326 | /* Grab the index number and argc parameters */ |
danielk1977 | 6dbee81 | 2008-01-03 18:39:41 +0000 | [diff] [blame] | 7327 | assert( (pQuery->flags&MEM_Int)!=0 && pArgc->flags==MEM_Int ); |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 7328 | nArg = (int)pArgc->u.i; |
| 7329 | iQuery = (int)pQuery->u.i; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 7330 | |
drh | 644a529 | 2006-12-20 14:53:38 +0000 | [diff] [blame] | 7331 | /* Invoke the xFilter method */ |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 7332 | res = 0; |
| 7333 | apArg = p->apArg; |
| 7334 | for(i = 0; i<nArg; i++){ |
| 7335 | apArg[i] = &pArgc[i+1]; |
| 7336 | } |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 7337 | rc = pModule->xFilter(pVCur, iQuery, pOp->p4.z, nArg, apArg); |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 7338 | sqlite3VtabImportErrmsg(p, pVtab); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 7339 | if( rc ) goto abort_due_to_error; |
| 7340 | res = pModule->xEof(pVCur); |
drh | 1d454a3 | 2008-01-31 19:34:51 +0000 | [diff] [blame] | 7341 | pCur->nullRow = 0; |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 7342 | VdbeBranchTaken(res!=0,2); |
| 7343 | if( res ) goto jump_to_p2; |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 7344 | break; |
| 7345 | } |
| 7346 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 7347 | |
| 7348 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | ce2fbd1 | 2018-01-12 21:00:14 +0000 | [diff] [blame] | 7349 | /* Opcode: VColumn P1 P2 P3 * P5 |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 7350 | ** Synopsis: r[P3]=vcolumn(P2) |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 7351 | ** |
drh | 6f390be | 2018-01-11 17:04:26 +0000 | [diff] [blame] | 7352 | ** Store in register P3 the value of the P2-th column of |
| 7353 | ** the current row of the virtual-table of cursor P1. |
| 7354 | ** |
| 7355 | ** If the VColumn opcode is being used to fetch the value of |
drh | ce2fbd1 | 2018-01-12 21:00:14 +0000 | [diff] [blame] | 7356 | ** an unchanging column during an UPDATE operation, then the P5 |
drh | 09d00b2 | 2018-09-27 20:20:01 +0000 | [diff] [blame] | 7357 | ** value is OPFLAG_NOCHNG. This will cause the sqlite3_vtab_nochange() |
| 7358 | ** function to return true inside the xColumn method of the virtual |
| 7359 | ** table implementation. The P5 column might also contain other |
| 7360 | ** bits (OPFLAG_LENGTHARG or OPFLAG_TYPEOFARG) but those bits are |
| 7361 | ** unused by OP_VColumn. |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 7362 | */ |
| 7363 | case OP_VColumn: { |
danielk1977 | 3e3a84d | 2008-08-01 17:37:40 +0000 | [diff] [blame] | 7364 | sqlite3_vtab *pVtab; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 7365 | const sqlite3_module *pModule; |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 7366 | Mem *pDest; |
| 7367 | sqlite3_context sContext; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 7368 | |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 7369 | VdbeCursor *pCur = p->apCsr[pOp->p1]; |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 7370 | assert( pCur->eCurType==CURTYPE_VTAB ); |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 7371 | assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) ); |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 7372 | pDest = &aMem[pOp->p3]; |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 7373 | memAboutToChange(p, pDest); |
drh | 2945b4a | 2008-01-31 15:53:45 +0000 | [diff] [blame] | 7374 | if( pCur->nullRow ){ |
| 7375 | sqlite3VdbeMemSetNull(pDest); |
| 7376 | break; |
| 7377 | } |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 7378 | pVtab = pCur->uc.pVCur->pVtab; |
danielk1977 | 3e3a84d | 2008-08-01 17:37:40 +0000 | [diff] [blame] | 7379 | pModule = pVtab->pModule; |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 7380 | assert( pModule->xColumn ); |
| 7381 | memset(&sContext, 0, sizeof(sContext)); |
drh | 9bd038f | 2014-08-27 14:14:06 +0000 | [diff] [blame] | 7382 | sContext.pOut = pDest; |
drh | 75f1076 | 2019-12-14 18:08:22 +0000 | [diff] [blame] | 7383 | assert( pOp->p5==OPFLAG_NOCHNG || pOp->p5==0 ); |
drh | 09d00b2 | 2018-09-27 20:20:01 +0000 | [diff] [blame] | 7384 | if( pOp->p5 & OPFLAG_NOCHNG ){ |
drh | ce2fbd1 | 2018-01-12 21:00:14 +0000 | [diff] [blame] | 7385 | sqlite3VdbeMemSetNull(pDest); |
| 7386 | pDest->flags = MEM_Null|MEM_Zero; |
| 7387 | pDest->u.nZero = 0; |
| 7388 | }else{ |
| 7389 | MemSetTypeFlag(pDest, MEM_Null); |
| 7390 | } |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 7391 | rc = pModule->xColumn(pCur->uc.pVCur, &sContext, pOp->p2); |
dan | 016f781 | 2013-08-21 17:35:48 +0000 | [diff] [blame] | 7392 | sqlite3VtabImportErrmsg(p, pVtab); |
drh | f09ac0b | 2018-01-23 03:44:06 +0000 | [diff] [blame] | 7393 | if( sContext.isError>0 ){ |
dan | 099fa84 | 2018-01-30 18:33:23 +0000 | [diff] [blame] | 7394 | sqlite3VdbeError(p, "%s", sqlite3_value_text(pDest)); |
drh | 4c8555f | 2009-06-25 01:47:11 +0000 | [diff] [blame] | 7395 | rc = sContext.isError; |
| 7396 | } |
drh | 9bd038f | 2014-08-27 14:14:06 +0000 | [diff] [blame] | 7397 | sqlite3VdbeChangeEncoding(pDest, encoding); |
drh | 5ff4437 | 2009-11-24 16:26:17 +0000 | [diff] [blame] | 7398 | REGISTER_TRACE(pOp->p3, pDest); |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 7399 | UPDATE_MAX_BLOBSIZE(pDest); |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 7400 | |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 7401 | if( sqlite3VdbeMemTooBig(pDest) ){ |
| 7402 | goto too_big; |
| 7403 | } |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 7404 | if( rc ) goto abort_due_to_error; |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 7405 | break; |
| 7406 | } |
| 7407 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 7408 | |
| 7409 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 7410 | /* Opcode: VNext P1 P2 * * * |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 7411 | ** |
| 7412 | ** Advance virtual table P1 to the next row in its result set and |
| 7413 | ** jump to instruction P2. Or, if the virtual table has reached |
| 7414 | ** the end of its result set, then fall through to the next instruction. |
| 7415 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 7416 | case OP_VNext: { /* jump */ |
danielk1977 | 3e3a84d | 2008-08-01 17:37:40 +0000 | [diff] [blame] | 7417 | sqlite3_vtab *pVtab; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 7418 | const sqlite3_module *pModule; |
drh | c54a617 | 2009-06-02 16:06:03 +0000 | [diff] [blame] | 7419 | int res; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 7420 | VdbeCursor *pCur; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 7421 | |
drh | c54a617 | 2009-06-02 16:06:03 +0000 | [diff] [blame] | 7422 | res = 0; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 7423 | pCur = p->apCsr[pOp->p1]; |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 7424 | assert( pCur->eCurType==CURTYPE_VTAB ); |
drh | 2945b4a | 2008-01-31 15:53:45 +0000 | [diff] [blame] | 7425 | if( pCur->nullRow ){ |
| 7426 | break; |
| 7427 | } |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 7428 | pVtab = pCur->uc.pVCur->pVtab; |
danielk1977 | 3e3a84d | 2008-08-01 17:37:40 +0000 | [diff] [blame] | 7429 | pModule = pVtab->pModule; |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 7430 | assert( pModule->xNext ); |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 7431 | |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 7432 | /* Invoke the xNext() method of the module. There is no way for the |
| 7433 | ** underlying implementation to return an error if one occurs during |
| 7434 | ** xNext(). Instead, if an error occurs, true is returned (indicating that |
| 7435 | ** data is available) and the error code returned when xColumn or |
| 7436 | ** some other method is next invoked on the save virtual table cursor. |
| 7437 | */ |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 7438 | rc = pModule->xNext(pCur->uc.pVCur); |
dan | 016f781 | 2013-08-21 17:35:48 +0000 | [diff] [blame] | 7439 | sqlite3VtabImportErrmsg(p, pVtab); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 7440 | if( rc ) goto abort_due_to_error; |
| 7441 | res = pModule->xEof(pCur->uc.pVCur); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 7442 | VdbeBranchTaken(!res,2); |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 7443 | if( !res ){ |
| 7444 | /* If there is data, jump to P2 */ |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 7445 | goto jump_to_p2_and_check_for_interrupt; |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 7446 | } |
drh | 49afe3a | 2013-07-10 03:05:14 +0000 | [diff] [blame] | 7447 | goto check_for_interrupt; |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 7448 | } |
| 7449 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 7450 | |
danielk1977 | 182c4ba | 2007-06-27 15:53:34 +0000 | [diff] [blame] | 7451 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 7452 | /* Opcode: VRename P1 * * P4 * |
danielk1977 | 182c4ba | 2007-06-27 15:53:34 +0000 | [diff] [blame] | 7453 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 7454 | ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure. |
danielk1977 | 182c4ba | 2007-06-27 15:53:34 +0000 | [diff] [blame] | 7455 | ** This opcode invokes the corresponding xRename method. The value |
danielk1977 | 6dbee81 | 2008-01-03 18:39:41 +0000 | [diff] [blame] | 7456 | ** in register P1 is passed as the zName argument to the xRename method. |
danielk1977 | 182c4ba | 2007-06-27 15:53:34 +0000 | [diff] [blame] | 7457 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 7458 | case OP_VRename: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 7459 | sqlite3_vtab *pVtab; |
| 7460 | Mem *pName; |
dan | 34566c4 | 2018-09-20 17:21:21 +0000 | [diff] [blame] | 7461 | int isLegacy; |
| 7462 | |
| 7463 | isLegacy = (db->flags & SQLITE_LegacyAlter); |
| 7464 | db->flags |= SQLITE_LegacyAlter; |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 7465 | pVtab = pOp->p4.pVtab->pVtab; |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 7466 | pName = &aMem[pOp->p1]; |
danielk1977 | 182c4ba | 2007-06-27 15:53:34 +0000 | [diff] [blame] | 7467 | assert( pVtab->pModule->xRename ); |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 7468 | assert( memIsValid(pName) ); |
drh | 9e92a47 | 2013-06-27 17:40:30 +0000 | [diff] [blame] | 7469 | assert( p->readOnly==0 ); |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 7470 | REGISTER_TRACE(pOp->p1, pName); |
drh | 35f6b93 | 2009-06-23 14:15:04 +0000 | [diff] [blame] | 7471 | assert( pName->flags & MEM_Str ); |
drh | 98655a6 | 2011-10-18 22:07:47 +0000 | [diff] [blame] | 7472 | testcase( pName->enc==SQLITE_UTF8 ); |
| 7473 | testcase( pName->enc==SQLITE_UTF16BE ); |
| 7474 | testcase( pName->enc==SQLITE_UTF16LE ); |
| 7475 | rc = sqlite3VdbeChangeEncoding(pName, SQLITE_UTF8); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 7476 | if( rc ) goto abort_due_to_error; |
| 7477 | rc = pVtab->pModule->xRename(pVtab, pName->z); |
drh | d5b44d6 | 2018-12-06 17:06:02 +0000 | [diff] [blame] | 7478 | if( isLegacy==0 ) db->flags &= ~(u64)SQLITE_LegacyAlter; |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 7479 | sqlite3VtabImportErrmsg(p, pVtab); |
| 7480 | p->expired = 0; |
| 7481 | if( rc ) goto abort_due_to_error; |
danielk1977 | 182c4ba | 2007-06-27 15:53:34 +0000 | [diff] [blame] | 7482 | break; |
| 7483 | } |
| 7484 | #endif |
drh | 4cbdda9 | 2006-06-14 19:00:20 +0000 | [diff] [blame] | 7485 | |
| 7486 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | 0fd6135 | 2014-02-07 02:29:45 +0000 | [diff] [blame] | 7487 | /* Opcode: VUpdate P1 P2 P3 P4 P5 |
drh | f63552b | 2013-10-30 00:25:03 +0000 | [diff] [blame] | 7488 | ** Synopsis: data=r[P3@P2] |
danielk1977 | 399918f | 2006-06-14 13:03:23 +0000 | [diff] [blame] | 7489 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 7490 | ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure. |
danielk1977 | 399918f | 2006-06-14 13:03:23 +0000 | [diff] [blame] | 7491 | ** This opcode invokes the corresponding xUpdate method. P2 values |
danielk1977 | 2a339ff | 2008-01-03 17:31:44 +0000 | [diff] [blame] | 7492 | ** are contiguous memory cells starting at P3 to pass to the xUpdate |
| 7493 | ** invocation. The value in register (P3+P2-1) corresponds to the |
| 7494 | ** p2th element of the argv array passed to xUpdate. |
drh | 4cbdda9 | 2006-06-14 19:00:20 +0000 | [diff] [blame] | 7495 | ** |
| 7496 | ** The xUpdate method will do a DELETE or an INSERT or both. |
danielk1977 | 2a339ff | 2008-01-03 17:31:44 +0000 | [diff] [blame] | 7497 | ** The argv[0] element (which corresponds to memory cell P3) |
| 7498 | ** is the rowid of a row to delete. If argv[0] is NULL then no |
| 7499 | ** deletion occurs. The argv[1] element is the rowid of the new |
| 7500 | ** row. This can be NULL to have the virtual table select the new |
| 7501 | ** rowid for itself. The subsequent elements in the array are |
| 7502 | ** the values of columns in the new row. |
drh | 4cbdda9 | 2006-06-14 19:00:20 +0000 | [diff] [blame] | 7503 | ** |
| 7504 | ** If P2==1 then no insert is performed. argv[0] is the rowid of |
| 7505 | ** a row to delete. |
danielk1977 | 1f6eec5 | 2006-06-16 06:17:47 +0000 | [diff] [blame] | 7506 | ** |
| 7507 | ** P1 is a boolean flag. If it is set to true and the xUpdate call |
| 7508 | ** is successful, then the value returned by sqlite3_last_insert_rowid() |
| 7509 | ** is set to the value of the rowid for the row just inserted. |
drh | 0fd6135 | 2014-02-07 02:29:45 +0000 | [diff] [blame] | 7510 | ** |
| 7511 | ** P5 is the error actions (OE_Replace, OE_Fail, OE_Ignore, etc) to |
| 7512 | ** apply in the case of a constraint failure on an insert or update. |
danielk1977 | 399918f | 2006-06-14 13:03:23 +0000 | [diff] [blame] | 7513 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 7514 | case OP_VUpdate: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 7515 | sqlite3_vtab *pVtab; |
drh | f496a7d | 2015-03-24 14:05:50 +0000 | [diff] [blame] | 7516 | const sqlite3_module *pModule; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 7517 | int nArg; |
| 7518 | int i; |
| 7519 | sqlite_int64 rowid; |
| 7520 | Mem **apArg; |
| 7521 | Mem *pX; |
| 7522 | |
dan | b061d05 | 2011-04-25 18:49:57 +0000 | [diff] [blame] | 7523 | assert( pOp->p2==1 || pOp->p5==OE_Fail || pOp->p5==OE_Rollback |
| 7524 | || pOp->p5==OE_Abort || pOp->p5==OE_Ignore || pOp->p5==OE_Replace |
| 7525 | ); |
drh | 9e92a47 | 2013-06-27 17:40:30 +0000 | [diff] [blame] | 7526 | assert( p->readOnly==0 ); |
dan | 466ea9b | 2018-06-13 11:11:13 +0000 | [diff] [blame] | 7527 | if( db->mallocFailed ) goto no_mem; |
drh | 4031baf | 2018-05-28 17:31:20 +0000 | [diff] [blame] | 7528 | sqlite3VdbeIncrWriteCounter(p, 0); |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 7529 | pVtab = pOp->p4.pVtab->pVtab; |
drh | f496a7d | 2015-03-24 14:05:50 +0000 | [diff] [blame] | 7530 | if( pVtab==0 || NEVER(pVtab->pModule==0) ){ |
| 7531 | rc = SQLITE_LOCKED; |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 7532 | goto abort_due_to_error; |
drh | f496a7d | 2015-03-24 14:05:50 +0000 | [diff] [blame] | 7533 | } |
| 7534 | pModule = pVtab->pModule; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 7535 | nArg = pOp->p2; |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 7536 | assert( pOp->p4type==P4_VTAB ); |
drh | 35f6b93 | 2009-06-23 14:15:04 +0000 | [diff] [blame] | 7537 | if( ALWAYS(pModule->xUpdate) ){ |
dan | b061d05 | 2011-04-25 18:49:57 +0000 | [diff] [blame] | 7538 | u8 vtabOnConflict = db->vtabOnConflict; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 7539 | apArg = p->apArg; |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 7540 | pX = &aMem[pOp->p3]; |
danielk1977 | 2a339ff | 2008-01-03 17:31:44 +0000 | [diff] [blame] | 7541 | for(i=0; i<nArg; i++){ |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 7542 | assert( memIsValid(pX) ); |
| 7543 | memAboutToChange(p, pX); |
drh | 9c41938 | 2006-06-16 21:13:21 +0000 | [diff] [blame] | 7544 | apArg[i] = pX; |
danielk1977 | 2a339ff | 2008-01-03 17:31:44 +0000 | [diff] [blame] | 7545 | pX++; |
danielk1977 | 399918f | 2006-06-14 13:03:23 +0000 | [diff] [blame] | 7546 | } |
dan | b061d05 | 2011-04-25 18:49:57 +0000 | [diff] [blame] | 7547 | db->vtabOnConflict = pOp->p5; |
danielk1977 | 1f6eec5 | 2006-06-16 06:17:47 +0000 | [diff] [blame] | 7548 | rc = pModule->xUpdate(pVtab, nArg, apArg, &rowid); |
dan | b061d05 | 2011-04-25 18:49:57 +0000 | [diff] [blame] | 7549 | db->vtabOnConflict = vtabOnConflict; |
dan | 016f781 | 2013-08-21 17:35:48 +0000 | [diff] [blame] | 7550 | sqlite3VtabImportErrmsg(p, pVtab); |
drh | 35f6b93 | 2009-06-23 14:15:04 +0000 | [diff] [blame] | 7551 | if( rc==SQLITE_OK && pOp->p1 ){ |
danielk1977 | 1f6eec5 | 2006-06-16 06:17:47 +0000 | [diff] [blame] | 7552 | assert( nArg>1 && apArg[0] && (apArg[0]->flags&MEM_Null) ); |
drh | fae58d5 | 2017-01-26 17:26:44 +0000 | [diff] [blame] | 7553 | db->lastRowid = rowid; |
danielk1977 | 1f6eec5 | 2006-06-16 06:17:47 +0000 | [diff] [blame] | 7554 | } |
drh | d91c1a1 | 2013-02-09 13:58:25 +0000 | [diff] [blame] | 7555 | if( (rc&0xff)==SQLITE_CONSTRAINT && pOp->p4.pVtab->bConstraint ){ |
dan | b061d05 | 2011-04-25 18:49:57 +0000 | [diff] [blame] | 7556 | if( pOp->p5==OE_Ignore ){ |
| 7557 | rc = SQLITE_OK; |
| 7558 | }else{ |
| 7559 | p->errorAction = ((pOp->p5==OE_Replace) ? OE_Abort : pOp->p5); |
| 7560 | } |
| 7561 | }else{ |
| 7562 | p->nChange++; |
| 7563 | } |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 7564 | if( rc ) goto abort_due_to_error; |
danielk1977 | 399918f | 2006-06-14 13:03:23 +0000 | [diff] [blame] | 7565 | } |
drh | 4cbdda9 | 2006-06-14 19:00:20 +0000 | [diff] [blame] | 7566 | break; |
danielk1977 | 399918f | 2006-06-14 13:03:23 +0000 | [diff] [blame] | 7567 | } |
| 7568 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 7569 | |
danielk1977 | 59a9379 | 2008-05-15 17:48:20 +0000 | [diff] [blame] | 7570 | #ifndef SQLITE_OMIT_PAGER_PRAGMAS |
| 7571 | /* Opcode: Pagecount P1 P2 * * * |
| 7572 | ** |
| 7573 | ** Write the current number of pages in database P1 to memory cell P2. |
| 7574 | */ |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 7575 | case OP_Pagecount: { /* out2 */ |
| 7576 | pOut = out2Prerelease(p, pOp); |
drh | b129915 | 2010-03-30 22:58:33 +0000 | [diff] [blame] | 7577 | pOut->u.i = sqlite3BtreeLastPage(db->aDb[pOp->p1].pBt); |
danielk1977 | 59a9379 | 2008-05-15 17:48:20 +0000 | [diff] [blame] | 7578 | break; |
| 7579 | } |
| 7580 | #endif |
| 7581 | |
drh | 60ac3f4 | 2010-11-23 18:59:27 +0000 | [diff] [blame] | 7582 | |
| 7583 | #ifndef SQLITE_OMIT_PAGER_PRAGMAS |
| 7584 | /* Opcode: MaxPgcnt P1 P2 P3 * * |
| 7585 | ** |
| 7586 | ** 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] | 7587 | ** Do not let the maximum page count fall below the current page count and |
| 7588 | ** do not change the maximum page count value if P3==0. |
| 7589 | ** |
drh | 60ac3f4 | 2010-11-23 18:59:27 +0000 | [diff] [blame] | 7590 | ** Store the maximum page count after the change in register P2. |
| 7591 | */ |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 7592 | case OP_MaxPgcnt: { /* out2 */ |
drh | c84e033 | 2010-11-23 20:25:08 +0000 | [diff] [blame] | 7593 | unsigned int newMax; |
drh | 60ac3f4 | 2010-11-23 18:59:27 +0000 | [diff] [blame] | 7594 | Btree *pBt; |
| 7595 | |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 7596 | pOut = out2Prerelease(p, pOp); |
drh | 60ac3f4 | 2010-11-23 18:59:27 +0000 | [diff] [blame] | 7597 | pBt = db->aDb[pOp->p1].pBt; |
drh | c84e033 | 2010-11-23 20:25:08 +0000 | [diff] [blame] | 7598 | newMax = 0; |
| 7599 | if( pOp->p3 ){ |
| 7600 | newMax = sqlite3BtreeLastPage(pBt); |
drh | 6ea28d6 | 2010-11-26 16:49:59 +0000 | [diff] [blame] | 7601 | if( newMax < (unsigned)pOp->p3 ) newMax = (unsigned)pOp->p3; |
drh | c84e033 | 2010-11-23 20:25:08 +0000 | [diff] [blame] | 7602 | } |
| 7603 | pOut->u.i = sqlite3BtreeMaxPageCount(pBt, newMax); |
drh | 60ac3f4 | 2010-11-23 18:59:27 +0000 | [diff] [blame] | 7604 | break; |
| 7605 | } |
| 7606 | #endif |
| 7607 | |
drh | 920cf59 | 2019-10-30 16:29:02 +0000 | [diff] [blame] | 7608 | /* Opcode: Function P1 P2 P3 P4 * |
drh | d7b10d7 | 2020-02-01 17:38:24 +0000 | [diff] [blame] | 7609 | ** Synopsis: r[P3]=func(r[P2@NP]) |
drh | 3e34eab | 2017-07-19 19:48:40 +0000 | [diff] [blame] | 7610 | ** |
| 7611 | ** 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] | 7612 | ** contains a pointer to the function to be run) with arguments taken |
| 7613 | ** from register P2 and successors. The number of arguments is in |
| 7614 | ** the sqlite3_context object that P4 points to. |
| 7615 | ** The result of the function is stored |
drh | 3e34eab | 2017-07-19 19:48:40 +0000 | [diff] [blame] | 7616 | ** in register P3. Register P3 must not be one of the function inputs. |
| 7617 | ** |
| 7618 | ** P1 is a 32-bit bitmask indicating whether or not each argument to the |
| 7619 | ** function was determined to be constant at compile time. If the first |
| 7620 | ** argument was constant then bit 0 of P1 is set. This is used to determine |
| 7621 | ** whether meta data associated with a user function argument using the |
| 7622 | ** sqlite3_set_auxdata() API may be safely retained until the next |
| 7623 | ** invocation of this opcode. |
| 7624 | ** |
drh | 920cf59 | 2019-10-30 16:29:02 +0000 | [diff] [blame] | 7625 | ** See also: AggStep, AggFinal, PureFunc |
drh | 3e34eab | 2017-07-19 19:48:40 +0000 | [diff] [blame] | 7626 | */ |
drh | 920cf59 | 2019-10-30 16:29:02 +0000 | [diff] [blame] | 7627 | /* Opcode: PureFunc P1 P2 P3 P4 * |
drh | d7b10d7 | 2020-02-01 17:38:24 +0000 | [diff] [blame] | 7628 | ** Synopsis: r[P3]=func(r[P2@NP]) |
drh | 920cf59 | 2019-10-30 16:29:02 +0000 | [diff] [blame] | 7629 | ** |
| 7630 | ** Invoke a user function (P4 is a pointer to an sqlite3_context object that |
| 7631 | ** contains a pointer to the function to be run) with arguments taken |
| 7632 | ** from register P2 and successors. The number of arguments is in |
| 7633 | ** the sqlite3_context object that P4 points to. |
| 7634 | ** The result of the function is stored |
| 7635 | ** in register P3. Register P3 must not be one of the function inputs. |
| 7636 | ** |
| 7637 | ** P1 is a 32-bit bitmask indicating whether or not each argument to the |
| 7638 | ** function was determined to be constant at compile time. If the first |
| 7639 | ** argument was constant then bit 0 of P1 is set. This is used to determine |
| 7640 | ** whether meta data associated with a user function argument using the |
| 7641 | ** sqlite3_set_auxdata() API may be safely retained until the next |
| 7642 | ** invocation of this opcode. |
| 7643 | ** |
| 7644 | ** This opcode works exactly like OP_Function. The only difference is in |
| 7645 | ** its name. This opcode is used in places where the function must be |
| 7646 | ** purely non-deterministic. Some built-in date/time functions can be |
| 7647 | ** either determinitic of non-deterministic, depending on their arguments. |
| 7648 | ** When those function are used in a non-deterministic way, they will check |
| 7649 | ** to see if they were called using OP_PureFunc instead of OP_Function, and |
| 7650 | ** if they were, they throw an error. |
| 7651 | ** |
| 7652 | ** See also: AggStep, AggFinal, Function |
| 7653 | */ |
mistachkin | 758784d | 2018-07-25 15:12:29 +0000 | [diff] [blame] | 7654 | case OP_PureFunc: /* group */ |
| 7655 | case OP_Function: { /* group */ |
drh | 3e34eab | 2017-07-19 19:48:40 +0000 | [diff] [blame] | 7656 | int i; |
| 7657 | sqlite3_context *pCtx; |
| 7658 | |
| 7659 | assert( pOp->p4type==P4_FUNCCTX ); |
| 7660 | pCtx = pOp->p4.pCtx; |
| 7661 | |
| 7662 | /* If this function is inside of a trigger, the register array in aMem[] |
| 7663 | ** might change from one evaluation to the next. The next block of code |
| 7664 | ** checks to see if the register array has changed, and if so it |
| 7665 | ** reinitializes the relavant parts of the sqlite3_context object */ |
| 7666 | pOut = &aMem[pOp->p3]; |
| 7667 | if( pCtx->pOut != pOut ){ |
drh | 920cf59 | 2019-10-30 16:29:02 +0000 | [diff] [blame] | 7668 | pCtx->pVdbe = p; |
drh | 3e34eab | 2017-07-19 19:48:40 +0000 | [diff] [blame] | 7669 | pCtx->pOut = pOut; |
| 7670 | for(i=pCtx->argc-1; i>=0; i--) pCtx->argv[i] = &aMem[pOp->p2+i]; |
| 7671 | } |
drh | 920cf59 | 2019-10-30 16:29:02 +0000 | [diff] [blame] | 7672 | assert( pCtx->pVdbe==p ); |
drh | 3e34eab | 2017-07-19 19:48:40 +0000 | [diff] [blame] | 7673 | |
| 7674 | memAboutToChange(p, pOut); |
| 7675 | #ifdef SQLITE_DEBUG |
| 7676 | for(i=0; i<pCtx->argc; i++){ |
| 7677 | assert( memIsValid(pCtx->argv[i]) ); |
| 7678 | REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]); |
| 7679 | } |
| 7680 | #endif |
| 7681 | MemSetTypeFlag(pOut, MEM_Null); |
drh | f09ac0b | 2018-01-23 03:44:06 +0000 | [diff] [blame] | 7682 | assert( pCtx->isError==0 ); |
drh | 3e34eab | 2017-07-19 19:48:40 +0000 | [diff] [blame] | 7683 | (*pCtx->pFunc->xSFunc)(pCtx, pCtx->argc, pCtx->argv);/* IMP: R-24505-23230 */ |
| 7684 | |
| 7685 | /* If the function returned an error, throw an exception */ |
drh | f09ac0b | 2018-01-23 03:44:06 +0000 | [diff] [blame] | 7686 | if( pCtx->isError ){ |
| 7687 | if( pCtx->isError>0 ){ |
drh | 3e34eab | 2017-07-19 19:48:40 +0000 | [diff] [blame] | 7688 | sqlite3VdbeError(p, "%s", sqlite3_value_text(pOut)); |
| 7689 | rc = pCtx->isError; |
| 7690 | } |
| 7691 | sqlite3VdbeDeleteAuxData(db, &p->pAuxData, pCtx->iOp, pOp->p1); |
drh | f09ac0b | 2018-01-23 03:44:06 +0000 | [diff] [blame] | 7692 | pCtx->isError = 0; |
drh | 3e34eab | 2017-07-19 19:48:40 +0000 | [diff] [blame] | 7693 | if( rc ) goto abort_due_to_error; |
| 7694 | } |
| 7695 | |
| 7696 | /* Copy the result of the function into register P3 */ |
| 7697 | if( pOut->flags & (MEM_Str|MEM_Blob) ){ |
| 7698 | sqlite3VdbeChangeEncoding(pOut, encoding); |
| 7699 | if( sqlite3VdbeMemTooBig(pOut) ) goto too_big; |
| 7700 | } |
| 7701 | |
| 7702 | REGISTER_TRACE(pOp->p3, pOut); |
| 7703 | UPDATE_MAX_BLOBSIZE(pOut); |
| 7704 | break; |
| 7705 | } |
| 7706 | |
drh | f259df5 | 2017-12-27 20:38:35 +0000 | [diff] [blame] | 7707 | /* Opcode: Trace P1 P2 * P4 * |
| 7708 | ** |
| 7709 | ** Write P4 on the statement trace output if statement tracing is |
| 7710 | ** enabled. |
| 7711 | ** |
| 7712 | ** Operand P1 must be 0x7fffffff and P2 must positive. |
| 7713 | */ |
drh | 74588ce | 2017-09-13 00:13:05 +0000 | [diff] [blame] | 7714 | /* Opcode: Init P1 P2 P3 P4 * |
drh | 72e26de | 2016-08-24 21:24:04 +0000 | [diff] [blame] | 7715 | ** Synopsis: Start at P2 |
drh | aceb31b | 2014-02-08 01:40:27 +0000 | [diff] [blame] | 7716 | ** |
| 7717 | ** Programs contain a single instance of this opcode as the very first |
| 7718 | ** opcode. |
drh | 949f9cd | 2008-01-12 21:35:57 +0000 | [diff] [blame] | 7719 | ** |
| 7720 | ** If tracing is enabled (by the sqlite3_trace()) interface, then |
| 7721 | ** the UTF-8 string contained in P4 is emitted on the trace callback. |
drh | aceb31b | 2014-02-08 01:40:27 +0000 | [diff] [blame] | 7722 | ** Or if P4 is blank, use the string returned by sqlite3_sql(). |
| 7723 | ** |
| 7724 | ** If P2 is not zero, jump to instruction P2. |
drh | 9e5eb9c | 2016-09-18 16:08:10 +0000 | [diff] [blame] | 7725 | ** |
| 7726 | ** Increment the value of P1 so that OP_Once opcodes will jump the |
| 7727 | ** first time they are evaluated for this run. |
drh | 74588ce | 2017-09-13 00:13:05 +0000 | [diff] [blame] | 7728 | ** |
| 7729 | ** If P3 is not zero, then it is an address to jump to if an SQLITE_CORRUPT |
| 7730 | ** error is encountered. |
drh | 949f9cd | 2008-01-12 21:35:57 +0000 | [diff] [blame] | 7731 | */ |
drh | f259df5 | 2017-12-27 20:38:35 +0000 | [diff] [blame] | 7732 | case OP_Trace: |
drh | aceb31b | 2014-02-08 01:40:27 +0000 | [diff] [blame] | 7733 | case OP_Init: { /* jump */ |
drh | 9e5eb9c | 2016-09-18 16:08:10 +0000 | [diff] [blame] | 7734 | int i; |
drh | b9f4799 | 2018-01-24 12:14:43 +0000 | [diff] [blame] | 7735 | #ifndef SQLITE_OMIT_TRACE |
| 7736 | char *zTrace; |
| 7737 | #endif |
drh | 5fe63bf | 2016-07-25 02:42:22 +0000 | [diff] [blame] | 7738 | |
| 7739 | /* If the P4 argument is not NULL, then it must be an SQL comment string. |
| 7740 | ** The "--" string is broken up to prevent false-positives with srcck1.c. |
| 7741 | ** |
| 7742 | ** This assert() provides evidence for: |
| 7743 | ** EVIDENCE-OF: R-50676-09860 The callback can compute the same text that |
| 7744 | ** would have been returned by the legacy sqlite3_trace() interface by |
| 7745 | ** using the X argument when X begins with "--" and invoking |
| 7746 | ** sqlite3_expanded_sql(P) otherwise. |
| 7747 | */ |
| 7748 | assert( pOp->p4.z==0 || strncmp(pOp->p4.z, "-" "- ", 3)==0 ); |
drh | f259df5 | 2017-12-27 20:38:35 +0000 | [diff] [blame] | 7749 | |
| 7750 | /* OP_Init is always instruction 0 */ |
| 7751 | assert( pOp==p->aOp || pOp->opcode==OP_Trace ); |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 7752 | |
drh | aceb31b | 2014-02-08 01:40:27 +0000 | [diff] [blame] | 7753 | #ifndef SQLITE_OMIT_TRACE |
drh | fca760c | 2016-07-14 01:09:08 +0000 | [diff] [blame] | 7754 | if( (db->mTrace & (SQLITE_TRACE_STMT|SQLITE_TRACE_LEGACY))!=0 |
drh | 37f58e9 | 2012-09-04 21:34:26 +0000 | [diff] [blame] | 7755 | && !p->doingRerun |
| 7756 | && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0 |
| 7757 | ){ |
drh | 3d2a529 | 2016-07-13 22:55:01 +0000 | [diff] [blame] | 7758 | #ifndef SQLITE_OMIT_DEPRECATED |
drh | fca760c | 2016-07-14 01:09:08 +0000 | [diff] [blame] | 7759 | if( db->mTrace & SQLITE_TRACE_LEGACY ){ |
| 7760 | void (*x)(void*,const char*) = (void(*)(void*,const char*))db->xTrace; |
drh | 5fe63bf | 2016-07-25 02:42:22 +0000 | [diff] [blame] | 7761 | char *z = sqlite3VdbeExpandSql(p, zTrace); |
drh | fca760c | 2016-07-14 01:09:08 +0000 | [diff] [blame] | 7762 | x(db->pTraceArg, z); |
drh | bd441f7 | 2016-07-25 02:31:48 +0000 | [diff] [blame] | 7763 | sqlite3_free(z); |
drh | fca760c | 2016-07-14 01:09:08 +0000 | [diff] [blame] | 7764 | }else |
drh | 3d2a529 | 2016-07-13 22:55:01 +0000 | [diff] [blame] | 7765 | #endif |
drh | 7adbcff | 2017-03-20 15:29:28 +0000 | [diff] [blame] | 7766 | if( db->nVdbeExec>1 ){ |
| 7767 | char *z = sqlite3MPrintf(db, "-- %s", zTrace); |
| 7768 | (void)db->xTrace(SQLITE_TRACE_STMT, db->pTraceArg, p, z); |
| 7769 | sqlite3DbFree(db, z); |
| 7770 | }else{ |
drh | bd441f7 | 2016-07-25 02:31:48 +0000 | [diff] [blame] | 7771 | (void)db->xTrace(SQLITE_TRACE_STMT, db->pTraceArg, p, zTrace); |
drh | 3d2a529 | 2016-07-13 22:55:01 +0000 | [diff] [blame] | 7772 | } |
drh | 949f9cd | 2008-01-12 21:35:57 +0000 | [diff] [blame] | 7773 | } |
drh | 8f8b231 | 2013-10-18 20:03:43 +0000 | [diff] [blame] | 7774 | #ifdef SQLITE_USE_FCNTL_TRACE |
| 7775 | zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql); |
| 7776 | if( zTrace ){ |
mistachkin | d8992ce | 2016-09-20 17:49:01 +0000 | [diff] [blame] | 7777 | int j; |
| 7778 | for(j=0; j<db->nDb; j++){ |
| 7779 | if( DbMaskTest(p->btreeMask, j)==0 ) continue; |
| 7780 | sqlite3_file_control(db, db->aDb[j].zDbSName, SQLITE_FCNTL_TRACE, zTrace); |
drh | 8f8b231 | 2013-10-18 20:03:43 +0000 | [diff] [blame] | 7781 | } |
| 7782 | } |
| 7783 | #endif /* SQLITE_USE_FCNTL_TRACE */ |
drh | c3f1d5f | 2011-05-30 23:42:16 +0000 | [diff] [blame] | 7784 | #ifdef SQLITE_DEBUG |
| 7785 | if( (db->flags & SQLITE_SqlTrace)!=0 |
| 7786 | && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0 |
| 7787 | ){ |
| 7788 | sqlite3DebugPrintf("SQL-trace: %s\n", zTrace); |
| 7789 | } |
| 7790 | #endif /* SQLITE_DEBUG */ |
drh | aceb31b | 2014-02-08 01:40:27 +0000 | [diff] [blame] | 7791 | #endif /* SQLITE_OMIT_TRACE */ |
drh | 4910a76 | 2016-09-03 01:46:15 +0000 | [diff] [blame] | 7792 | assert( pOp->p2>0 ); |
drh | 9e5eb9c | 2016-09-18 16:08:10 +0000 | [diff] [blame] | 7793 | if( pOp->p1>=sqlite3GlobalConfig.iOnceResetThreshold ){ |
drh | f259df5 | 2017-12-27 20:38:35 +0000 | [diff] [blame] | 7794 | if( pOp->opcode==OP_Trace ) break; |
drh | 9e5eb9c | 2016-09-18 16:08:10 +0000 | [diff] [blame] | 7795 | for(i=1; i<p->nOp; i++){ |
| 7796 | if( p->aOp[i].opcode==OP_Once ) p->aOp[i].p1 = 0; |
| 7797 | } |
| 7798 | pOp->p1 = 0; |
| 7799 | } |
| 7800 | pOp->p1++; |
drh | 00d11d4 | 2017-06-29 12:49:18 +0000 | [diff] [blame] | 7801 | p->aCounter[SQLITE_STMTSTATUS_RUN]++; |
drh | 4910a76 | 2016-09-03 01:46:15 +0000 | [diff] [blame] | 7802 | goto jump_to_p2; |
drh | 949f9cd | 2008-01-12 21:35:57 +0000 | [diff] [blame] | 7803 | } |
drh | 949f9cd | 2008-01-12 21:35:57 +0000 | [diff] [blame] | 7804 | |
drh | 2893536 | 2013-12-07 20:39:19 +0000 | [diff] [blame] | 7805 | #ifdef SQLITE_ENABLE_CURSOR_HINTS |
drh | 0df5701 | 2015-08-14 15:05:55 +0000 | [diff] [blame] | 7806 | /* Opcode: CursorHint P1 * * P4 * |
drh | 2893536 | 2013-12-07 20:39:19 +0000 | [diff] [blame] | 7807 | ** |
| 7808 | ** 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] | 7809 | ** satisfy the Expr in P4. TK_REGISTER terms in the P4 expression refer |
| 7810 | ** to values currently held in registers. TK_COLUMN terms in the P4 |
| 7811 | ** 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] | 7812 | */ |
| 7813 | case OP_CursorHint: { |
| 7814 | VdbeCursor *pC; |
| 7815 | |
| 7816 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 7817 | assert( pOp->p4type==P4_EXPR ); |
| 7818 | pC = p->apCsr[pOp->p1]; |
dan | 91d3a61 | 2014-07-15 11:59:44 +0000 | [diff] [blame] | 7819 | if( pC ){ |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 7820 | assert( pC->eCurType==CURTYPE_BTREE ); |
drh | 62aaa6c | 2015-11-21 17:27:42 +0000 | [diff] [blame] | 7821 | sqlite3BtreeCursorHint(pC->uc.pCursor, BTREE_HINT_RANGE, |
| 7822 | pOp->p4.pExpr, aMem); |
dan | 91d3a61 | 2014-07-15 11:59:44 +0000 | [diff] [blame] | 7823 | } |
drh | 2893536 | 2013-12-07 20:39:19 +0000 | [diff] [blame] | 7824 | break; |
| 7825 | } |
| 7826 | #endif /* SQLITE_ENABLE_CURSOR_HINTS */ |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 7827 | |
drh | 4031baf | 2018-05-28 17:31:20 +0000 | [diff] [blame] | 7828 | #ifdef SQLITE_DEBUG |
| 7829 | /* Opcode: Abortable * * * * * |
| 7830 | ** |
| 7831 | ** Verify that an Abort can happen. Assert if an Abort at this point |
| 7832 | ** might cause database corruption. This opcode only appears in debugging |
| 7833 | ** builds. |
| 7834 | ** |
| 7835 | ** An Abort is safe if either there have been no writes, or if there is |
| 7836 | ** an active statement journal. |
| 7837 | */ |
| 7838 | case OP_Abortable: { |
| 7839 | sqlite3VdbeAssertAbortable(p); |
| 7840 | break; |
| 7841 | } |
| 7842 | #endif |
| 7843 | |
drh | 13d7950 | 2019-12-23 02:18:49 +0000 | [diff] [blame] | 7844 | #ifdef SQLITE_DEBUG |
drh | 3aef2fb | 2020-01-02 17:46:02 +0000 | [diff] [blame] | 7845 | /* Opcode: ReleaseReg P1 P2 P3 * P5 |
drh | 13d7950 | 2019-12-23 02:18:49 +0000 | [diff] [blame] | 7846 | ** Synopsis: release r[P1@P2] mask P3 |
| 7847 | ** |
| 7848 | ** Release registers from service. Any content that was in the |
| 7849 | ** the registers is unreliable after this opcode completes. |
| 7850 | ** |
| 7851 | ** The registers released will be the P2 registers starting at P1, |
| 7852 | ** except if bit ii of P3 set, then do not release register P1+ii. |
| 7853 | ** In other words, P3 is a mask of registers to preserve. |
| 7854 | ** |
| 7855 | ** Releasing a register clears the Mem.pScopyFrom pointer. That means |
| 7856 | ** that if the content of the released register was set using OP_SCopy, |
| 7857 | ** a change to the value of the source register for the OP_SCopy will no longer |
| 7858 | ** generate an assertion fault in sqlite3VdbeMemAboutToChange(). |
| 7859 | ** |
drh | 3aef2fb | 2020-01-02 17:46:02 +0000 | [diff] [blame] | 7860 | ** If P5 is set, then all released registers have their type set |
| 7861 | ** to MEM_Undefined so that any subsequent attempt to read the released |
drh | 13d7950 | 2019-12-23 02:18:49 +0000 | [diff] [blame] | 7862 | ** register (before it is reinitialized) will generate an assertion fault. |
drh | 3aef2fb | 2020-01-02 17:46:02 +0000 | [diff] [blame] | 7863 | ** |
| 7864 | ** P5 ought to be set on every call to this opcode. |
| 7865 | ** However, there are places in the code generator will release registers |
drh | 13d7950 | 2019-12-23 02:18:49 +0000 | [diff] [blame] | 7866 | ** before their are used, under the (valid) assumption that the registers |
| 7867 | ** will not be reallocated for some other purpose before they are used and |
| 7868 | ** hence are safe to release. |
| 7869 | ** |
| 7870 | ** This opcode is only available in testing and debugging builds. It is |
| 7871 | ** not generated for release builds. The purpose of this opcode is to help |
| 7872 | ** validate the generated bytecode. This opcode does not actually contribute |
| 7873 | ** to computing an answer. |
| 7874 | */ |
| 7875 | case OP_ReleaseReg: { |
| 7876 | Mem *pMem; |
| 7877 | int i; |
| 7878 | u32 constMask; |
| 7879 | assert( pOp->p1>0 ); |
| 7880 | assert( pOp->p1+pOp->p2<=(p->nMem+1 - p->nCursor)+1 ); |
| 7881 | pMem = &aMem[pOp->p1]; |
| 7882 | constMask = pOp->p3; |
| 7883 | for(i=0; i<pOp->p2; i++, pMem++){ |
drh | 7edce5e | 2019-12-23 13:24:34 +0000 | [diff] [blame] | 7884 | if( i>=32 || (constMask & MASKBIT32(i))==0 ){ |
drh | 13d7950 | 2019-12-23 02:18:49 +0000 | [diff] [blame] | 7885 | pMem->pScopyFrom = 0; |
drh | 3aef2fb | 2020-01-02 17:46:02 +0000 | [diff] [blame] | 7886 | if( i<32 && pOp->p5 ) MemSetTypeFlag(pMem, MEM_Undefined); |
drh | 13d7950 | 2019-12-23 02:18:49 +0000 | [diff] [blame] | 7887 | } |
| 7888 | } |
| 7889 | break; |
| 7890 | } |
| 7891 | #endif |
| 7892 | |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 7893 | /* Opcode: Noop * * * * * |
| 7894 | ** |
| 7895 | ** Do nothing. This instruction is often useful as a jump |
| 7896 | ** destination. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 7897 | */ |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 7898 | /* |
| 7899 | ** The magic Explain opcode are only inserted when explain==2 (which |
| 7900 | ** is to say when the EXPLAIN QUERY PLAN syntax is used.) |
| 7901 | ** This opcode records information from the optimizer. It is the |
| 7902 | ** the same as a no-op. This opcodesnever appears in a real VM program. |
| 7903 | */ |
drh | 4031baf | 2018-05-28 17:31:20 +0000 | [diff] [blame] | 7904 | default: { /* This is really OP_Noop, OP_Explain */ |
drh | 13573c7 | 2010-01-12 17:04:07 +0000 | [diff] [blame] | 7905 | assert( pOp->opcode==OP_Noop || pOp->opcode==OP_Explain ); |
drh | 4031baf | 2018-05-28 17:31:20 +0000 | [diff] [blame] | 7906 | |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 7907 | break; |
| 7908 | } |
| 7909 | |
| 7910 | /***************************************************************************** |
| 7911 | ** The cases of the switch statement above this line should all be indented |
| 7912 | ** by 6 spaces. But the left-most 6 spaces have been removed to improve the |
| 7913 | ** readability. From this point on down, the normal indentation rules are |
| 7914 | ** restored. |
| 7915 | *****************************************************************************/ |
| 7916 | } |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 7917 | |
drh | 7b39686 | 2003-01-01 23:06:20 +0000 | [diff] [blame] | 7918 | #ifdef VDBE_PROFILE |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 7919 | { |
drh | 35043cc | 2018-02-12 20:27:34 +0000 | [diff] [blame] | 7920 | u64 endTime = sqlite3NProfileCnt ? sqlite3NProfileCnt : sqlite3Hwtime(); |
drh | 6dc4148 | 2015-04-16 17:31:02 +0000 | [diff] [blame] | 7921 | if( endTime>start ) pOrigOp->cycles += endTime - start; |
| 7922 | pOrigOp->cnt++; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 7923 | } |
drh | 7b39686 | 2003-01-01 23:06:20 +0000 | [diff] [blame] | 7924 | #endif |
| 7925 | |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 7926 | /* The following code adds nothing to the actual functionality |
| 7927 | ** of the program. It is only here for testing and debugging. |
| 7928 | ** On the other hand, it does burn CPU cycles every time through |
| 7929 | ** the evaluator loop. So we can leave it out when NDEBUG is defined. |
| 7930 | */ |
| 7931 | #ifndef NDEBUG |
drh | 6dc4148 | 2015-04-16 17:31:02 +0000 | [diff] [blame] | 7932 | assert( pOp>=&aOp[-1] && pOp<&aOp[p->nOp-1] ); |
drh | ae7e151 | 2007-05-02 16:51:59 +0000 | [diff] [blame] | 7933 | |
drh | cf1023c | 2007-05-08 20:59:49 +0000 | [diff] [blame] | 7934 | #ifdef SQLITE_DEBUG |
drh | 84e55a8 | 2013-11-13 17:58:23 +0000 | [diff] [blame] | 7935 | if( db->flags & SQLITE_VdbeTrace ){ |
drh | 7cc84c2 | 2016-04-11 13:36:42 +0000 | [diff] [blame] | 7936 | u8 opProperty = sqlite3OpcodeProperty[pOrigOp->opcode]; |
drh | 84e55a8 | 2013-11-13 17:58:23 +0000 | [diff] [blame] | 7937 | if( rc!=0 ) printf("rc=%d\n",rc); |
drh | 7cc84c2 | 2016-04-11 13:36:42 +0000 | [diff] [blame] | 7938 | if( opProperty & (OPFLG_OUT2) ){ |
drh | 6dc4148 | 2015-04-16 17:31:02 +0000 | [diff] [blame] | 7939 | registerTrace(pOrigOp->p2, &aMem[pOrigOp->p2]); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 7940 | } |
drh | 7cc84c2 | 2016-04-11 13:36:42 +0000 | [diff] [blame] | 7941 | if( opProperty & OPFLG_OUT3 ){ |
drh | 6dc4148 | 2015-04-16 17:31:02 +0000 | [diff] [blame] | 7942 | registerTrace(pOrigOp->p3, &aMem[pOrigOp->p3]); |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 7943 | } |
drh | 17aceeb | 2020-01-04 19:12:13 +0000 | [diff] [blame] | 7944 | if( opProperty==0xff ){ |
| 7945 | /* Never happens. This code exists to avoid a harmless linkage |
| 7946 | ** warning aboud sqlite3VdbeRegisterDump() being defined but not |
| 7947 | ** used. */ |
| 7948 | sqlite3VdbeRegisterDump(p); |
| 7949 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 7950 | } |
danielk1977 | b5402fb | 2005-01-12 07:15:04 +0000 | [diff] [blame] | 7951 | #endif /* SQLITE_DEBUG */ |
| 7952 | #endif /* NDEBUG */ |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 7953 | } /* The end of the for(;;) loop the loops through opcodes */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 7954 | |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 7955 | /* If we reach this point, it means that execution is finished with |
| 7956 | ** an error of some kind. |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 7957 | */ |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 7958 | abort_due_to_error: |
| 7959 | if( db->mallocFailed ) rc = SQLITE_NOMEM_BKPT; |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 7960 | assert( rc ); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 7961 | if( p->zErrMsg==0 && rc!=SQLITE_IOERR_NOMEM ){ |
| 7962 | sqlite3VdbeError(p, "%s", sqlite3ErrStr(rc)); |
| 7963 | } |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 7964 | p->rc = rc; |
drh | f68521c | 2016-03-21 12:28:02 +0000 | [diff] [blame] | 7965 | sqlite3SystemError(db, rc); |
drh | a64fa91 | 2010-03-04 00:53:32 +0000 | [diff] [blame] | 7966 | testcase( sqlite3GlobalConfig.xLog!=0 ); |
| 7967 | sqlite3_log(rc, "statement aborts at %d: [%s] %s", |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 7968 | (int)(pOp - aOp), p->zSql, p->zErrMsg); |
drh | 92f02c3 | 2004-09-02 14:57:08 +0000 | [diff] [blame] | 7969 | sqlite3VdbeHalt(p); |
drh | 4a642b6 | 2016-02-05 01:55:27 +0000 | [diff] [blame] | 7970 | if( rc==SQLITE_IOERR_NOMEM ) sqlite3OomFault(db); |
danielk1977 | 7eaabcd | 2008-07-07 14:56:56 +0000 | [diff] [blame] | 7971 | rc = SQLITE_ERROR; |
drh | cdf011d | 2011-04-04 21:25:28 +0000 | [diff] [blame] | 7972 | if( resetSchemaOnFault>0 ){ |
drh | 81028a4 | 2012-05-15 18:28:27 +0000 | [diff] [blame] | 7973 | sqlite3ResetOneSchema(db, resetSchemaOnFault-1); |
drh | bdaec52 | 2011-04-04 00:14:43 +0000 | [diff] [blame] | 7974 | } |
drh | 900b31e | 2007-08-28 02:27:51 +0000 | [diff] [blame] | 7975 | |
| 7976 | /* This is the only way out of this procedure. We have to |
| 7977 | ** release the mutexes on btrees that were acquired at the |
| 7978 | ** top. */ |
| 7979 | vdbe_return: |
drh | c332e04 | 2019-02-12 21:04:33 +0000 | [diff] [blame] | 7980 | #ifndef SQLITE_OMIT_PROGRESS_CALLBACK |
drh | b1af9c6 | 2019-02-20 13:55:45 +0000 | [diff] [blame] | 7981 | while( nVmStep>=nProgressLimit && db->xProgress!=0 ){ |
| 7982 | nProgressLimit += db->nProgressOps; |
drh | c332e04 | 2019-02-12 21:04:33 +0000 | [diff] [blame] | 7983 | if( db->xProgress(db->pProgressArg) ){ |
| 7984 | nProgressLimit = 0xffffffff; |
| 7985 | rc = SQLITE_INTERRUPT; |
| 7986 | goto abort_due_to_error; |
| 7987 | } |
| 7988 | } |
| 7989 | #endif |
drh | 9b47ee3 | 2013-08-20 03:13:51 +0000 | [diff] [blame] | 7990 | p->aCounter[SQLITE_STMTSTATUS_VM_STEP] += (int)nVmStep; |
drh | bdaec52 | 2011-04-04 00:14:43 +0000 | [diff] [blame] | 7991 | sqlite3VdbeLeave(p); |
dan | 83f0ab8 | 2016-01-29 18:04:31 +0000 | [diff] [blame] | 7992 | assert( rc!=SQLITE_OK || nExtraDelete==0 |
| 7993 | || sqlite3_strlike("DELETE%",p->zSql,0)!=0 |
| 7994 | ); |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 7995 | return rc; |
| 7996 | |
drh | 023ae03 | 2007-05-08 12:12:16 +0000 | [diff] [blame] | 7997 | /* Jump to here if a string or blob larger than SQLITE_MAX_LENGTH |
| 7998 | ** is encountered. |
| 7999 | */ |
| 8000 | too_big: |
drh | 22c17b8 | 2015-05-15 04:13:15 +0000 | [diff] [blame] | 8001 | sqlite3VdbeError(p, "string or blob too big"); |
drh | 023ae03 | 2007-05-08 12:12:16 +0000 | [diff] [blame] | 8002 | rc = SQLITE_TOOBIG; |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 8003 | goto abort_due_to_error; |
drh | 023ae03 | 2007-05-08 12:12:16 +0000 | [diff] [blame] | 8004 | |
drh | 98640a3 | 2007-06-07 19:08:32 +0000 | [diff] [blame] | 8005 | /* Jump to here if a malloc() fails. |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 8006 | */ |
| 8007 | no_mem: |
drh | 4a642b6 | 2016-02-05 01:55:27 +0000 | [diff] [blame] | 8008 | sqlite3OomFault(db); |
drh | 22c17b8 | 2015-05-15 04:13:15 +0000 | [diff] [blame] | 8009 | sqlite3VdbeError(p, "out of memory"); |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 8010 | rc = SQLITE_NOMEM_BKPT; |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 8011 | goto abort_due_to_error; |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 8012 | |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 8013 | /* Jump to here if the sqlite3_interrupt() API sets the interrupt |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 8014 | ** flag. |
| 8015 | */ |
| 8016 | abort_due_to_interrupt: |
dan | 892edb6 | 2020-03-30 13:35:05 +0000 | [diff] [blame] | 8017 | assert( AtomicLoad(&db->u1.isInterrupted) ); |
drh | 56f1873 | 2020-06-03 15:59:22 +0000 | [diff] [blame^] | 8018 | rc = SQLITE_INTERRUPT; |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 8019 | goto abort_due_to_error; |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 8020 | } |