drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1 | /* |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2 | ** 2001 September 15 |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 4 | ** The author disclaims copyright to this source code. In place of |
| 5 | ** a legal notice, here is a blessing: |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 7 | ** May you do good and not evil. |
| 8 | ** May you find forgiveness for yourself and forgive others. |
| 9 | ** May you share freely, never taking more than you give. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 10 | ** |
| 11 | ************************************************************************* |
drh | 0fd6135 | 2014-02-07 02:29:45 +0000 | [diff] [blame] | 12 | ** The code in this file implements the function that runs the |
| 13 | ** bytecode of a prepared statement. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 14 | ** |
drh | ac82fcf | 2002-09-08 17:23:41 +0000 | [diff] [blame] | 15 | ** Various scripts scan this source file in order to generate HTML |
| 16 | ** documentation, headers files, or other derived files. The formatting |
| 17 | ** of the code in this file is, therefore, important. See other comments |
| 18 | ** in this file for details. If in doubt, do not deviate from existing |
| 19 | ** commenting and indentation practices when changing or adding code. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 20 | */ |
| 21 | #include "sqliteInt.h" |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 22 | #include "vdbeInt.h" |
drh | 8f619cc | 2002-09-08 00:04:50 +0000 | [diff] [blame] | 23 | |
| 24 | /* |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 25 | ** Invoke this macro on memory cells just prior to changing the |
| 26 | ** value of the cell. This macro verifies that shallow copies are |
drh | 0fd6135 | 2014-02-07 02:29:45 +0000 | [diff] [blame] | 27 | ** not misused. A shallow copy of a string or blob just copies a |
| 28 | ** pointer to the string or blob, not the content. If the original |
| 29 | ** is changed while the copy is still in use, the string or blob might |
| 30 | ** be changed out from under the copy. This macro verifies that nothing |
drh | b6e8fd1 | 2014-03-06 01:56:33 +0000 | [diff] [blame] | 31 | ** like that ever happens. |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 32 | */ |
| 33 | #ifdef SQLITE_DEBUG |
drh | e4c88c0 | 2012-01-04 12:57:45 +0000 | [diff] [blame] | 34 | # define memAboutToChange(P,M) sqlite3VdbeMemAboutToChange(P,M) |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 35 | #else |
| 36 | # define memAboutToChange(P,M) |
| 37 | #endif |
| 38 | |
| 39 | /* |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 40 | ** The following global variable is incremented every time a cursor |
drh | 959403f | 2008-12-12 17:56:16 +0000 | [diff] [blame] | 41 | ** moves, either by the OP_SeekXX, OP_Next, or OP_Prev opcodes. The test |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 42 | ** procedures use this information to make sure that indices are |
drh | ac82fcf | 2002-09-08 17:23:41 +0000 | [diff] [blame] | 43 | ** working correctly. This variable has no function other than to |
| 44 | ** help verify the correct operation of the library. |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 45 | */ |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 46 | #ifdef SQLITE_TEST |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 47 | int sqlite3_search_count = 0; |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 48 | #endif |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 49 | |
drh | f603871 | 2004-02-08 18:07:34 +0000 | [diff] [blame] | 50 | /* |
| 51 | ** When this global variable is positive, it gets decremented once before |
drh | e4c88c0 | 2012-01-04 12:57:45 +0000 | [diff] [blame] | 52 | ** each instruction in the VDBE. When it reaches zero, the u1.isInterrupted |
| 53 | ** field of the sqlite3 structure is set in order to simulate an interrupt. |
drh | f603871 | 2004-02-08 18:07:34 +0000 | [diff] [blame] | 54 | ** |
| 55 | ** This facility is used for testing purposes only. It does not function |
| 56 | ** in an ordinary build. |
| 57 | */ |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 58 | #ifdef SQLITE_TEST |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 59 | int sqlite3_interrupt_count = 0; |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 60 | #endif |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 61 | |
danielk1977 | 7e18c25 | 2004-05-25 11:47:24 +0000 | [diff] [blame] | 62 | /* |
drh | 6bf8957 | 2004-11-03 16:27:01 +0000 | [diff] [blame] | 63 | ** The next global variable is incremented each type the OP_Sort opcode |
| 64 | ** is executed. The test procedures use this information to make sure that |
shane | 21e7feb | 2008-05-30 15:59:49 +0000 | [diff] [blame] | 65 | ** sorting is occurring or not occurring at appropriate times. This variable |
drh | 6bf8957 | 2004-11-03 16:27:01 +0000 | [diff] [blame] | 66 | ** has no function other than to help verify the correct operation of the |
| 67 | ** library. |
| 68 | */ |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 69 | #ifdef SQLITE_TEST |
drh | 6bf8957 | 2004-11-03 16:27:01 +0000 | [diff] [blame] | 70 | int sqlite3_sort_count = 0; |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 71 | #endif |
drh | 6bf8957 | 2004-11-03 16:27:01 +0000 | [diff] [blame] | 72 | |
| 73 | /* |
drh | ae7e151 | 2007-05-02 16:51:59 +0000 | [diff] [blame] | 74 | ** The next global variable records the size of the largest MEM_Blob |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 75 | ** or MEM_Str that has been used by a VDBE opcode. The test procedures |
drh | ae7e151 | 2007-05-02 16:51:59 +0000 | [diff] [blame] | 76 | ** use this information to make sure that the zero-blob functionality |
| 77 | ** is working correctly. This variable has no function other than to |
| 78 | ** help verify the correct operation of the library. |
| 79 | */ |
| 80 | #ifdef SQLITE_TEST |
| 81 | int sqlite3_max_blobsize = 0; |
drh | ca48c90 | 2008-01-18 14:08:24 +0000 | [diff] [blame] | 82 | static void updateMaxBlobsize(Mem *p){ |
| 83 | if( (p->flags & (MEM_Str|MEM_Blob))!=0 && p->n>sqlite3_max_blobsize ){ |
| 84 | sqlite3_max_blobsize = p->n; |
| 85 | } |
| 86 | } |
drh | ae7e151 | 2007-05-02 16:51:59 +0000 | [diff] [blame] | 87 | #endif |
| 88 | |
| 89 | /* |
drh | 9b1c62d | 2011-03-30 21:04:43 +0000 | [diff] [blame] | 90 | ** This macro evaluates to true if either the update hook or the preupdate |
| 91 | ** hook are enabled for database connect DB. |
| 92 | */ |
| 93 | #ifdef SQLITE_ENABLE_PREUPDATE_HOOK |
drh | 74c3302 | 2016-03-30 12:56:55 +0000 | [diff] [blame] | 94 | # define HAS_UPDATE_HOOK(DB) ((DB)->xPreUpdateCallback||(DB)->xUpdateCallback) |
drh | 9b1c62d | 2011-03-30 21:04:43 +0000 | [diff] [blame] | 95 | #else |
drh | 74c3302 | 2016-03-30 12:56:55 +0000 | [diff] [blame] | 96 | # define HAS_UPDATE_HOOK(DB) ((DB)->xUpdateCallback) |
drh | 9b1c62d | 2011-03-30 21:04:43 +0000 | [diff] [blame] | 97 | #endif |
| 98 | |
| 99 | /* |
drh | 0fd6135 | 2014-02-07 02:29:45 +0000 | [diff] [blame] | 100 | ** The next global variable is incremented each time the OP_Found opcode |
dan | 0ff297e | 2009-09-25 17:03:14 +0000 | [diff] [blame] | 101 | ** is executed. This is used to test whether or not the foreign key |
| 102 | ** operation implemented using OP_FkIsZero is working. This variable |
| 103 | ** has no function other than to help verify the correct operation of the |
| 104 | ** library. |
| 105 | */ |
| 106 | #ifdef SQLITE_TEST |
| 107 | int sqlite3_found_count = 0; |
| 108 | #endif |
| 109 | |
| 110 | /* |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 111 | ** Test a register to see if it exceeds the current maximum blob size. |
| 112 | ** If it does, record the new maximum blob size. |
| 113 | */ |
drh | d12602a | 2016-12-07 15:49:02 +0000 | [diff] [blame] | 114 | #if defined(SQLITE_TEST) && !defined(SQLITE_UNTESTABLE) |
drh | ca48c90 | 2008-01-18 14:08:24 +0000 | [diff] [blame] | 115 | # define UPDATE_MAX_BLOBSIZE(P) updateMaxBlobsize(P) |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 116 | #else |
| 117 | # define UPDATE_MAX_BLOBSIZE(P) |
| 118 | #endif |
| 119 | |
| 120 | /* |
drh | 5655c54 | 2014-02-19 19:14:34 +0000 | [diff] [blame] | 121 | ** Invoke the VDBE coverage callback, if that callback is defined. This |
| 122 | ** feature is used for test suite validation only and does not appear an |
| 123 | ** production builds. |
| 124 | ** |
| 125 | ** M is an integer, 2 or 3, that indices how many different ways the |
| 126 | ** branch can go. It is usually 2. "I" is the direction the branch |
| 127 | ** goes. 0 means falls through. 1 means branch is taken. 2 means the |
| 128 | ** second alternative branch is taken. |
drh | 4336b0e | 2014-08-05 00:53:51 +0000 | [diff] [blame] | 129 | ** |
| 130 | ** iSrcLine is the source code line (from the __LINE__ macro) that |
| 131 | ** generated the VDBE instruction. This instrumentation assumes that all |
| 132 | ** source code is in a single file (the amalgamation). Special values 1 |
| 133 | ** and 2 for the iSrcLine parameter mean that this particular branch is |
| 134 | ** always taken or never taken, respectively. |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 135 | */ |
| 136 | #if !defined(SQLITE_VDBE_COVERAGE) |
| 137 | # define VdbeBranchTaken(I,M) |
| 138 | #else |
drh | 5655c54 | 2014-02-19 19:14:34 +0000 | [diff] [blame] | 139 | # define VdbeBranchTaken(I,M) vdbeTakeBranch(pOp->iSrcLine,I,M) |
| 140 | static void vdbeTakeBranch(int iSrcLine, u8 I, u8 M){ |
| 141 | if( iSrcLine<=2 && ALWAYS(iSrcLine>0) ){ |
| 142 | M = iSrcLine; |
| 143 | /* Assert the truth of VdbeCoverageAlwaysTaken() and |
| 144 | ** VdbeCoverageNeverTaken() */ |
| 145 | assert( (M & I)==I ); |
| 146 | }else{ |
| 147 | if( sqlite3GlobalConfig.xVdbeBranch==0 ) return; /*NO_TEST*/ |
| 148 | sqlite3GlobalConfig.xVdbeBranch(sqlite3GlobalConfig.pVdbeBranchArg, |
| 149 | iSrcLine,I,M); |
| 150 | } |
| 151 | } |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 152 | #endif |
| 153 | |
| 154 | /* |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 155 | ** Convert the given register into a string if it isn't one |
danielk1977 | bd7e460 | 2004-05-24 07:34:48 +0000 | [diff] [blame] | 156 | ** already. Return non-zero if a malloc() fails. |
| 157 | */ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 158 | #define Stringify(P, enc) \ |
drh | bd9507c | 2014-08-23 17:21:37 +0000 | [diff] [blame] | 159 | if(((P)->flags&(MEM_Str|MEM_Blob))==0 && sqlite3VdbeMemStringify(P,enc,0)) \ |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 160 | { goto no_mem; } |
danielk1977 | bd7e460 | 2004-05-24 07:34:48 +0000 | [diff] [blame] | 161 | |
| 162 | /* |
danielk1977 | bd7e460 | 2004-05-24 07:34:48 +0000 | [diff] [blame] | 163 | ** An ephemeral string value (signified by the MEM_Ephem flag) contains |
| 164 | ** a pointer to a dynamically allocated string where some other entity |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 165 | ** is responsible for deallocating that string. Because the register |
| 166 | ** does not control the string, it might be deleted without the register |
| 167 | ** knowing it. |
danielk1977 | bd7e460 | 2004-05-24 07:34:48 +0000 | [diff] [blame] | 168 | ** |
| 169 | ** This routine converts an ephemeral string into a dynamically allocated |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 170 | ** string that the register itself controls. In other words, it |
drh | c91b2fd | 2014-03-01 18:13:23 +0000 | [diff] [blame] | 171 | ** converts an MEM_Ephem string into a string with P.z==P.zMalloc. |
danielk1977 | bd7e460 | 2004-05-24 07:34:48 +0000 | [diff] [blame] | 172 | */ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 173 | #define Deephemeralize(P) \ |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 174 | if( ((P)->flags&MEM_Ephem)!=0 \ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 175 | && sqlite3VdbeMemMakeWriteable(P) ){ goto no_mem;} |
danielk1977 | 93d4675 | 2004-05-23 13:30:58 +0000 | [diff] [blame] | 176 | |
dan | 689ab89 | 2011-08-12 15:02:00 +0000 | [diff] [blame] | 177 | /* Return true if the cursor was opened using the OP_OpenSorter opcode. */ |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 178 | #define isSorter(x) ((x)->eCurType==CURTYPE_SORTER) |
danielk1977 | 8a6b541 | 2004-05-24 07:04:25 +0000 | [diff] [blame] | 179 | |
| 180 | /* |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 181 | ** Allocate VdbeCursor number iCur. Return a pointer to it. Return NULL |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 182 | ** if we run out of memory. |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 183 | */ |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 184 | static VdbeCursor *allocateCursor( |
| 185 | Vdbe *p, /* The virtual machine */ |
| 186 | int iCur, /* Index of the new VdbeCursor */ |
danielk1977 | d336e22 | 2009-02-20 10:58:41 +0000 | [diff] [blame] | 187 | int nField, /* Number of fields in the table or index */ |
drh | e4c88c0 | 2012-01-04 12:57:45 +0000 | [diff] [blame] | 188 | int iDb, /* Database the cursor belongs to, or -1 */ |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 189 | u8 eCurType /* Type of the new cursor */ |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 190 | ){ |
| 191 | /* 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] | 192 | ** required for this VdbeCursor structure. It is convenient to use a |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 193 | ** vdbe memory cell to manage the memory allocation required for a |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 194 | ** VdbeCursor structure for the following reasons: |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 195 | ** |
| 196 | ** * Sometimes cursor numbers are used for a couple of different |
| 197 | ** purposes in a vdbe program. The different uses might require |
| 198 | ** different sized allocations. Memory cells provide growable |
| 199 | ** allocations. |
| 200 | ** |
| 201 | ** * When using ENABLE_MEMORY_MANAGEMENT, memory cell buffers can |
| 202 | ** be freed lazily via the sqlite3_release_memory() API. This |
| 203 | ** minimizes the number of malloc calls made by the system. |
| 204 | ** |
drh | 3cdce92 | 2016-03-21 00:30:40 +0000 | [diff] [blame] | 205 | ** 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] | 206 | ** the top of the register space. Cursor 1 is at Mem[p->nMem-1]. |
| 207 | ** Cursor 2 is at Mem[p->nMem-2]. And so forth. |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 208 | */ |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 209 | Mem *pMem = iCur>0 ? &p->aMem[p->nMem-iCur] : p->aMem; |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 210 | |
danielk1977 | 5f09613 | 2008-03-28 15:44:09 +0000 | [diff] [blame] | 211 | int nByte; |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 212 | VdbeCursor *pCx = 0; |
danielk1977 | 5f09613 | 2008-03-28 15:44:09 +0000 | [diff] [blame] | 213 | nByte = |
drh | 5cc1023 | 2013-11-21 01:04:02 +0000 | [diff] [blame] | 214 | ROUND8(sizeof(VdbeCursor)) + 2*sizeof(u32)*nField + |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 215 | (eCurType==CURTYPE_BTREE?sqlite3BtreeCursorSize():0); |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 216 | |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 217 | assert( iCur>=0 && iCur<p->nCursor ); |
drh | a3fa140 | 2016-04-29 02:55:05 +0000 | [diff] [blame] | 218 | if( p->apCsr[iCur] ){ /*OPTIMIZATION-IF-FALSE*/ |
danielk1977 | be71889 | 2006-06-23 08:05:19 +0000 | [diff] [blame] | 219 | sqlite3VdbeFreeCursor(p, p->apCsr[iCur]); |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 220 | p->apCsr[iCur] = 0; |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 221 | } |
drh | 322f285 | 2014-09-19 00:43:39 +0000 | [diff] [blame] | 222 | if( SQLITE_OK==sqlite3VdbeMemClearAndResize(pMem, nByte) ){ |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 223 | p->apCsr[iCur] = pCx = (VdbeCursor*)pMem->z; |
drh | fbd8cbd | 2016-12-10 12:58:15 +0000 | [diff] [blame] | 224 | memset(pCx, 0, offsetof(VdbeCursor,pAltCursor)); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 225 | pCx->eCurType = eCurType; |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 +0000 | [diff] [blame] | 226 | pCx->iDb = iDb; |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 227 | pCx->nField = nField; |
drh | b53a5a9 | 2014-10-12 22:37:22 +0000 | [diff] [blame] | 228 | pCx->aOffset = &pCx->aType[nField]; |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 229 | if( eCurType==CURTYPE_BTREE ){ |
| 230 | pCx->uc.pCursor = (BtCursor*) |
drh | 5cc1023 | 2013-11-21 01:04:02 +0000 | [diff] [blame] | 231 | &pMem->z[ROUND8(sizeof(VdbeCursor))+2*sizeof(u32)*nField]; |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 232 | sqlite3BtreeCursorZero(pCx->uc.pCursor); |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 233 | } |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 +0000 | [diff] [blame] | 234 | } |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 235 | return pCx; |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 236 | } |
| 237 | |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 238 | /* |
drh | 29d7210 | 2006-02-09 22:13:41 +0000 | [diff] [blame] | 239 | ** Try to convert a value into a numeric representation if we can |
| 240 | ** do so without loss of information. In other words, if the string |
| 241 | ** looks like a number, convert it into a number. If it does not |
| 242 | ** look like a number, leave it alone. |
drh | bd9507c | 2014-08-23 17:21:37 +0000 | [diff] [blame] | 243 | ** |
| 244 | ** If the bTryForInt flag is true, then extra effort is made to give |
| 245 | ** an integer representation. Strings that look like floating point |
| 246 | ** values but which have no fractional component (example: '48.00') |
| 247 | ** will have a MEM_Int representation when bTryForInt is true. |
| 248 | ** |
| 249 | ** If bTryForInt is false, then if the input string contains a decimal |
| 250 | ** point or exponential notation, the result is only MEM_Real, even |
| 251 | ** if there is an exact integer representation of the quantity. |
drh | 29d7210 | 2006-02-09 22:13:41 +0000 | [diff] [blame] | 252 | */ |
drh | bd9507c | 2014-08-23 17:21:37 +0000 | [diff] [blame] | 253 | static void applyNumericAffinity(Mem *pRec, int bTryForInt){ |
drh | 975b4c6 | 2014-07-26 16:47:23 +0000 | [diff] [blame] | 254 | double rValue; |
| 255 | i64 iValue; |
| 256 | u8 enc = pRec->enc; |
drh | 11a6eee | 2014-09-19 22:01:54 +0000 | [diff] [blame] | 257 | assert( (pRec->flags & (MEM_Str|MEM_Int|MEM_Real))==MEM_Str ); |
drh | 975b4c6 | 2014-07-26 16:47:23 +0000 | [diff] [blame] | 258 | if( sqlite3AtoF(pRec->z, &rValue, pRec->n, enc)==0 ) return; |
| 259 | if( 0==sqlite3Atoi64(pRec->z, &iValue, pRec->n, enc) ){ |
| 260 | pRec->u.i = iValue; |
| 261 | pRec->flags |= MEM_Int; |
| 262 | }else{ |
drh | 74eaba4 | 2014-09-18 17:52:15 +0000 | [diff] [blame] | 263 | pRec->u.r = rValue; |
drh | 975b4c6 | 2014-07-26 16:47:23 +0000 | [diff] [blame] | 264 | pRec->flags |= MEM_Real; |
drh | bd9507c | 2014-08-23 17:21:37 +0000 | [diff] [blame] | 265 | if( bTryForInt ) sqlite3VdbeIntegerAffinity(pRec); |
drh | 29d7210 | 2006-02-09 22:13:41 +0000 | [diff] [blame] | 266 | } |
drh | 06b3bd5 | 2018-02-01 01:13:33 +0000 | [diff] [blame] | 267 | /* TEXT->NUMERIC is many->one. Hence, it is important to invalidate the |
| 268 | ** string representation after computing a numeric equivalent, because the |
| 269 | ** string representation might not be the canonical representation for the |
| 270 | ** numeric value. Ticket [343634942dd54ab57b7024] 2018-01-31. */ |
| 271 | pRec->flags &= ~MEM_Str; |
drh | 29d7210 | 2006-02-09 22:13:41 +0000 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | /* |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 275 | ** Processing is determine by the affinity parameter: |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 276 | ** |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 277 | ** SQLITE_AFF_INTEGER: |
| 278 | ** SQLITE_AFF_REAL: |
| 279 | ** SQLITE_AFF_NUMERIC: |
| 280 | ** Try to convert pRec to an integer representation or a |
| 281 | ** floating-point representation if an integer representation |
| 282 | ** is not possible. Note that the integer representation is |
| 283 | ** always preferred, even if the affinity is REAL, because |
| 284 | ** an integer representation is more space efficient on disk. |
| 285 | ** |
| 286 | ** SQLITE_AFF_TEXT: |
| 287 | ** Convert pRec to a text representation. |
| 288 | ** |
drh | 05883a3 | 2015-06-02 15:32:08 +0000 | [diff] [blame] | 289 | ** SQLITE_AFF_BLOB: |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 290 | ** No-op. pRec is unchanged. |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 291 | */ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 292 | static void applyAffinity( |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 293 | Mem *pRec, /* The value to apply affinity to */ |
| 294 | char affinity, /* The affinity to be applied */ |
| 295 | u8 enc /* Use this text encoding */ |
| 296 | ){ |
drh | 7ea31cc | 2014-09-18 14:36:00 +0000 | [diff] [blame] | 297 | if( affinity>=SQLITE_AFF_NUMERIC ){ |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 298 | assert( affinity==SQLITE_AFF_INTEGER || affinity==SQLITE_AFF_REAL |
| 299 | || affinity==SQLITE_AFF_NUMERIC ); |
drh | a3fa140 | 2016-04-29 02:55:05 +0000 | [diff] [blame] | 300 | if( (pRec->flags & MEM_Int)==0 ){ /*OPTIMIZATION-IF-FALSE*/ |
drh | bd9507c | 2014-08-23 17:21:37 +0000 | [diff] [blame] | 301 | if( (pRec->flags & MEM_Real)==0 ){ |
drh | 11a6eee | 2014-09-19 22:01:54 +0000 | [diff] [blame] | 302 | if( pRec->flags & MEM_Str ) applyNumericAffinity(pRec,1); |
drh | bd9507c | 2014-08-23 17:21:37 +0000 | [diff] [blame] | 303 | }else{ |
| 304 | sqlite3VdbeIntegerAffinity(pRec); |
| 305 | } |
drh | 17c4029 | 2004-07-21 02:53:29 +0000 | [diff] [blame] | 306 | } |
drh | 7ea31cc | 2014-09-18 14:36:00 +0000 | [diff] [blame] | 307 | }else if( affinity==SQLITE_AFF_TEXT ){ |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 308 | /* Only attempt the conversion to TEXT if there is an integer or real |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 309 | ** representation (blob and NULL do not get converted) but no string |
drh | a3fa140 | 2016-04-29 02:55:05 +0000 | [diff] [blame] | 310 | ** representation. It would be harmless to repeat the conversion if |
| 311 | ** there is already a string rep, but it is pointless to waste those |
| 312 | ** CPU cycles. */ |
| 313 | if( 0==(pRec->flags&MEM_Str) ){ /*OPTIMIZATION-IF-FALSE*/ |
| 314 | if( (pRec->flags&(MEM_Real|MEM_Int)) ){ |
| 315 | sqlite3VdbeMemStringify(pRec, enc, 1); |
| 316 | } |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 317 | } |
dan | dde548c | 2015-05-19 19:44:25 +0000 | [diff] [blame] | 318 | pRec->flags &= ~(MEM_Real|MEM_Int); |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 319 | } |
| 320 | } |
| 321 | |
danielk1977 | aee18ef | 2005-03-09 12:26:50 +0000 | [diff] [blame] | 322 | /* |
drh | 29d7210 | 2006-02-09 22:13:41 +0000 | [diff] [blame] | 323 | ** Try to convert the type of a function argument or a result column |
| 324 | ** into a numeric representation. Use either INTEGER or REAL whichever |
| 325 | ** is appropriate. But only do the conversion if it is possible without |
| 326 | ** loss of information and return the revised type of the argument. |
drh | 29d7210 | 2006-02-09 22:13:41 +0000 | [diff] [blame] | 327 | */ |
| 328 | int sqlite3_value_numeric_type(sqlite3_value *pVal){ |
drh | 1b27b8c | 2014-02-10 03:21:57 +0000 | [diff] [blame] | 329 | int eType = sqlite3_value_type(pVal); |
| 330 | if( eType==SQLITE_TEXT ){ |
| 331 | Mem *pMem = (Mem*)pVal; |
drh | bd9507c | 2014-08-23 17:21:37 +0000 | [diff] [blame] | 332 | applyNumericAffinity(pMem, 0); |
drh | 1b27b8c | 2014-02-10 03:21:57 +0000 | [diff] [blame] | 333 | eType = sqlite3_value_type(pVal); |
drh | e5a8a1d | 2010-11-18 12:31:24 +0000 | [diff] [blame] | 334 | } |
drh | 1b27b8c | 2014-02-10 03:21:57 +0000 | [diff] [blame] | 335 | return eType; |
drh | 29d7210 | 2006-02-09 22:13:41 +0000 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | /* |
danielk1977 | aee18ef | 2005-03-09 12:26:50 +0000 | [diff] [blame] | 339 | ** Exported version of applyAffinity(). This one works on sqlite3_value*, |
| 340 | ** not the internal Mem* type. |
| 341 | */ |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 342 | void sqlite3ValueApplyAffinity( |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 343 | sqlite3_value *pVal, |
| 344 | u8 affinity, |
| 345 | u8 enc |
| 346 | ){ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 347 | applyAffinity((Mem *)pVal, affinity, enc); |
danielk1977 | aee18ef | 2005-03-09 12:26:50 +0000 | [diff] [blame] | 348 | } |
| 349 | |
drh | 3d1d90a | 2014-03-24 15:00:15 +0000 | [diff] [blame] | 350 | /* |
drh | f1a89ed | 2014-08-23 17:41:15 +0000 | [diff] [blame] | 351 | ** pMem currently only holds a string type (or maybe a BLOB that we can |
| 352 | ** interpret as a string if we want to). Compute its corresponding |
drh | 74eaba4 | 2014-09-18 17:52:15 +0000 | [diff] [blame] | 353 | ** 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] | 354 | ** accordingly. |
| 355 | */ |
| 356 | static u16 SQLITE_NOINLINE computeNumericType(Mem *pMem){ |
| 357 | assert( (pMem->flags & (MEM_Int|MEM_Real))==0 ); |
| 358 | assert( (pMem->flags & (MEM_Str|MEM_Blob))!=0 ); |
drh | 74eaba4 | 2014-09-18 17:52:15 +0000 | [diff] [blame] | 359 | if( sqlite3AtoF(pMem->z, &pMem->u.r, pMem->n, pMem->enc)==0 ){ |
drh | f1a89ed | 2014-08-23 17:41:15 +0000 | [diff] [blame] | 360 | return 0; |
| 361 | } |
drh | 84d4f1a | 2017-09-20 10:47:10 +0000 | [diff] [blame] | 362 | if( sqlite3Atoi64(pMem->z, &pMem->u.i, pMem->n, pMem->enc)==0 ){ |
drh | f1a89ed | 2014-08-23 17:41:15 +0000 | [diff] [blame] | 363 | return MEM_Int; |
| 364 | } |
| 365 | return MEM_Real; |
| 366 | } |
| 367 | |
| 368 | /* |
drh | 3d1d90a | 2014-03-24 15:00:15 +0000 | [diff] [blame] | 369 | ** Return the numeric type for pMem, either MEM_Int or MEM_Real or both or |
| 370 | ** none. |
| 371 | ** |
| 372 | ** Unlike applyNumericAffinity(), this routine does not modify pMem->flags. |
drh | 74eaba4 | 2014-09-18 17:52:15 +0000 | [diff] [blame] | 373 | ** But it does set pMem->u.r and pMem->u.i appropriately. |
drh | 3d1d90a | 2014-03-24 15:00:15 +0000 | [diff] [blame] | 374 | */ |
| 375 | static u16 numericType(Mem *pMem){ |
| 376 | if( pMem->flags & (MEM_Int|MEM_Real) ){ |
| 377 | return pMem->flags & (MEM_Int|MEM_Real); |
| 378 | } |
| 379 | if( pMem->flags & (MEM_Str|MEM_Blob) ){ |
drh | f1a89ed | 2014-08-23 17:41:15 +0000 | [diff] [blame] | 380 | return computeNumericType(pMem); |
drh | 3d1d90a | 2014-03-24 15:00:15 +0000 | [diff] [blame] | 381 | } |
| 382 | return 0; |
| 383 | } |
| 384 | |
danielk1977 | b5402fb | 2005-01-12 07:15:04 +0000 | [diff] [blame] | 385 | #ifdef SQLITE_DEBUG |
drh | b6f5452 | 2004-05-20 02:42:16 +0000 | [diff] [blame] | 386 | /* |
danielk1977 | ca6b291 | 2004-05-21 10:49:47 +0000 | [diff] [blame] | 387 | ** Write a nice string representation of the contents of cell pMem |
| 388 | ** into buffer zBuf, length nBuf. |
| 389 | */ |
drh | 7416170 | 2006-02-24 02:53:49 +0000 | [diff] [blame] | 390 | void sqlite3VdbeMemPrettyPrint(Mem *pMem, char *zBuf){ |
danielk1977 | ca6b291 | 2004-05-21 10:49:47 +0000 | [diff] [blame] | 391 | char *zCsr = zBuf; |
| 392 | int f = pMem->flags; |
| 393 | |
drh | 5719628 | 2004-10-06 15:41:16 +0000 | [diff] [blame] | 394 | static const char *const encnames[] = {"(X)", "(8)", "(16LE)", "(16BE)"}; |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame] | 395 | |
danielk1977 | ca6b291 | 2004-05-21 10:49:47 +0000 | [diff] [blame] | 396 | if( f&MEM_Blob ){ |
| 397 | int i; |
| 398 | char c; |
| 399 | if( f & MEM_Dyn ){ |
| 400 | c = 'z'; |
| 401 | assert( (f & (MEM_Static|MEM_Ephem))==0 ); |
| 402 | }else if( f & MEM_Static ){ |
| 403 | c = 't'; |
| 404 | assert( (f & (MEM_Dyn|MEM_Ephem))==0 ); |
| 405 | }else if( f & MEM_Ephem ){ |
| 406 | c = 'e'; |
| 407 | assert( (f & (MEM_Static|MEM_Dyn))==0 ); |
| 408 | }else{ |
| 409 | c = 's'; |
| 410 | } |
drh | 85c2dc0 | 2017-03-16 13:30:58 +0000 | [diff] [blame] | 411 | *(zCsr++) = c; |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 412 | sqlite3_snprintf(100, zCsr, "%d[", pMem->n); |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 413 | zCsr += sqlite3Strlen30(zCsr); |
danielk1977 | ca6b291 | 2004-05-21 10:49:47 +0000 | [diff] [blame] | 414 | for(i=0; i<16 && i<pMem->n; i++){ |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 415 | sqlite3_snprintf(100, zCsr, "%02X", ((int)pMem->z[i] & 0xFF)); |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 416 | zCsr += sqlite3Strlen30(zCsr); |
danielk1977 | ca6b291 | 2004-05-21 10:49:47 +0000 | [diff] [blame] | 417 | } |
| 418 | for(i=0; i<16 && i<pMem->n; i++){ |
| 419 | char z = pMem->z[i]; |
| 420 | if( z<32 || z>126 ) *zCsr++ = '.'; |
| 421 | else *zCsr++ = z; |
| 422 | } |
drh | 85c2dc0 | 2017-03-16 13:30:58 +0000 | [diff] [blame] | 423 | *(zCsr++) = ']'; |
drh | fdf972a | 2007-05-02 13:30:27 +0000 | [diff] [blame] | 424 | if( f & MEM_Zero ){ |
drh | 8df3284 | 2008-12-09 02:51:23 +0000 | [diff] [blame] | 425 | sqlite3_snprintf(100, zCsr,"+%dz",pMem->u.nZero); |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 426 | zCsr += sqlite3Strlen30(zCsr); |
drh | fdf972a | 2007-05-02 13:30:27 +0000 | [diff] [blame] | 427 | } |
danielk1977 | b1bc953 | 2004-05-22 03:05:33 +0000 | [diff] [blame] | 428 | *zCsr = '\0'; |
| 429 | }else if( f & MEM_Str ){ |
| 430 | int j, k; |
| 431 | zBuf[0] = ' '; |
| 432 | if( f & MEM_Dyn ){ |
| 433 | zBuf[1] = 'z'; |
| 434 | assert( (f & (MEM_Static|MEM_Ephem))==0 ); |
| 435 | }else if( f & MEM_Static ){ |
| 436 | zBuf[1] = 't'; |
| 437 | assert( (f & (MEM_Dyn|MEM_Ephem))==0 ); |
| 438 | }else if( f & MEM_Ephem ){ |
| 439 | zBuf[1] = 'e'; |
| 440 | assert( (f & (MEM_Static|MEM_Dyn))==0 ); |
| 441 | }else{ |
| 442 | zBuf[1] = 's'; |
| 443 | } |
| 444 | k = 2; |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 445 | sqlite3_snprintf(100, &zBuf[k], "%d", pMem->n); |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 446 | k += sqlite3Strlen30(&zBuf[k]); |
danielk1977 | b1bc953 | 2004-05-22 03:05:33 +0000 | [diff] [blame] | 447 | zBuf[k++] = '['; |
| 448 | for(j=0; j<15 && j<pMem->n; j++){ |
| 449 | u8 c = pMem->z[j]; |
danielk1977 | b1bc953 | 2004-05-22 03:05:33 +0000 | [diff] [blame] | 450 | if( c>=0x20 && c<0x7f ){ |
| 451 | zBuf[k++] = c; |
| 452 | }else{ |
| 453 | zBuf[k++] = '.'; |
| 454 | } |
| 455 | } |
| 456 | zBuf[k++] = ']'; |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 457 | sqlite3_snprintf(100,&zBuf[k], encnames[pMem->enc]); |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 458 | k += sqlite3Strlen30(&zBuf[k]); |
danielk1977 | b1bc953 | 2004-05-22 03:05:33 +0000 | [diff] [blame] | 459 | zBuf[k++] = 0; |
danielk1977 | ca6b291 | 2004-05-21 10:49:47 +0000 | [diff] [blame] | 460 | } |
danielk1977 | ca6b291 | 2004-05-21 10:49:47 +0000 | [diff] [blame] | 461 | } |
| 462 | #endif |
| 463 | |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 464 | #ifdef SQLITE_DEBUG |
| 465 | /* |
| 466 | ** Print the value of a register for tracing purposes: |
| 467 | */ |
drh | 84e55a8 | 2013-11-13 17:58:23 +0000 | [diff] [blame] | 468 | static void memTracePrint(Mem *p){ |
drh | a5750cf | 2014-02-07 13:20:31 +0000 | [diff] [blame] | 469 | if( p->flags & MEM_Undefined ){ |
drh | 84e55a8 | 2013-11-13 17:58:23 +0000 | [diff] [blame] | 470 | printf(" undefined"); |
drh | 953f761 | 2012-12-07 22:18:54 +0000 | [diff] [blame] | 471 | }else if( p->flags & MEM_Null ){ |
drh | ce2fbd1 | 2018-01-12 21:00:14 +0000 | [diff] [blame] | 472 | printf(p->flags & MEM_Zero ? " NULL-nochng" : " NULL"); |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 473 | }else if( (p->flags & (MEM_Int|MEM_Str))==(MEM_Int|MEM_Str) ){ |
drh | 84e55a8 | 2013-11-13 17:58:23 +0000 | [diff] [blame] | 474 | printf(" si:%lld", p->u.i); |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 475 | }else if( p->flags & MEM_Int ){ |
drh | 84e55a8 | 2013-11-13 17:58:23 +0000 | [diff] [blame] | 476 | printf(" i:%lld", p->u.i); |
drh | 0b3bf92 | 2009-06-15 20:45:34 +0000 | [diff] [blame] | 477 | #ifndef SQLITE_OMIT_FLOATING_POINT |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 478 | }else if( p->flags & MEM_Real ){ |
drh | 74eaba4 | 2014-09-18 17:52:15 +0000 | [diff] [blame] | 479 | printf(" r:%g", p->u.r); |
drh | 0b3bf92 | 2009-06-15 20:45:34 +0000 | [diff] [blame] | 480 | #endif |
drh | 733bf1b | 2009-04-22 00:47:00 +0000 | [diff] [blame] | 481 | }else if( p->flags & MEM_RowSet ){ |
drh | 84e55a8 | 2013-11-13 17:58:23 +0000 | [diff] [blame] | 482 | printf(" (rowset)"); |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 483 | }else{ |
| 484 | char zBuf[200]; |
| 485 | sqlite3VdbeMemPrettyPrint(p, zBuf); |
drh | 84e55a8 | 2013-11-13 17:58:23 +0000 | [diff] [blame] | 486 | printf(" %s", zBuf); |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 487 | } |
dan | 5b6c8e4 | 2016-01-30 15:46:03 +0000 | [diff] [blame] | 488 | if( p->flags & MEM_Subtype ) printf(" subtype=0x%02x", p->eSubtype); |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 489 | } |
drh | 84e55a8 | 2013-11-13 17:58:23 +0000 | [diff] [blame] | 490 | static void registerTrace(int iReg, Mem *p){ |
| 491 | printf("REG[%d] = ", iReg); |
| 492 | memTracePrint(p); |
| 493 | printf("\n"); |
drh | e2bc655 | 2017-04-17 20:50:34 +0000 | [diff] [blame] | 494 | sqlite3VdbeCheckMemInvariants(p); |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 495 | } |
| 496 | #endif |
| 497 | |
| 498 | #ifdef SQLITE_DEBUG |
drh | 84e55a8 | 2013-11-13 17:58:23 +0000 | [diff] [blame] | 499 | # define REGISTER_TRACE(R,M) if(db->flags&SQLITE_VdbeTrace)registerTrace(R,M) |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 500 | #else |
| 501 | # define REGISTER_TRACE(R,M) |
| 502 | #endif |
| 503 | |
danielk1977 | 84ac9d0 | 2004-05-18 09:58:06 +0000 | [diff] [blame] | 504 | |
drh | 7b39686 | 2003-01-01 23:06:20 +0000 | [diff] [blame] | 505 | #ifdef VDBE_PROFILE |
shane | 9bcbdad | 2008-05-29 20:22:37 +0000 | [diff] [blame] | 506 | |
| 507 | /* |
| 508 | ** hwtime.h contains inline assembler code for implementing |
| 509 | ** high-performance timing routines. |
drh | 7b39686 | 2003-01-01 23:06:20 +0000 | [diff] [blame] | 510 | */ |
shane | 9bcbdad | 2008-05-29 20:22:37 +0000 | [diff] [blame] | 511 | #include "hwtime.h" |
| 512 | |
drh | 7b39686 | 2003-01-01 23:06:20 +0000 | [diff] [blame] | 513 | #endif |
| 514 | |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 515 | #ifndef NDEBUG |
| 516 | /* |
| 517 | ** This function is only called from within an assert() expression. It |
| 518 | ** checks that the sqlite3.nTransaction variable is correctly set to |
| 519 | ** the number of non-transaction savepoints currently in the |
| 520 | ** linked list starting at sqlite3.pSavepoint. |
| 521 | ** |
| 522 | ** Usage: |
| 523 | ** |
| 524 | ** assert( checkSavepointCount(db) ); |
| 525 | */ |
| 526 | static int checkSavepointCount(sqlite3 *db){ |
| 527 | int n = 0; |
| 528 | Savepoint *p; |
| 529 | for(p=db->pSavepoint; p; p=p->pNext) n++; |
| 530 | assert( n==(db->nSavepoint + db->isTransactionSavepoint) ); |
| 531 | return 1; |
| 532 | } |
| 533 | #endif |
| 534 | |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 535 | /* |
| 536 | ** Return the register of pOp->p2 after first preparing it to be |
| 537 | ** overwritten with an integer value. |
drh | 9eef8c6 | 2015-10-15 17:31:41 +0000 | [diff] [blame] | 538 | */ |
| 539 | static SQLITE_NOINLINE Mem *out2PrereleaseWithClear(Mem *pOut){ |
| 540 | sqlite3VdbeMemSetNull(pOut); |
| 541 | pOut->flags = MEM_Int; |
| 542 | return pOut; |
| 543 | } |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 544 | static Mem *out2Prerelease(Vdbe *p, VdbeOp *pOp){ |
| 545 | Mem *pOut; |
| 546 | assert( pOp->p2>0 ); |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 547 | assert( pOp->p2<=(p->nMem+1 - p->nCursor) ); |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 548 | pOut = &p->aMem[pOp->p2]; |
| 549 | memAboutToChange(p, pOut); |
drh | a3fa140 | 2016-04-29 02:55:05 +0000 | [diff] [blame] | 550 | if( VdbeMemDynamic(pOut) ){ /*OPTIMIZATION-IF-FALSE*/ |
drh | 9eef8c6 | 2015-10-15 17:31:41 +0000 | [diff] [blame] | 551 | return out2PrereleaseWithClear(pOut); |
| 552 | }else{ |
| 553 | pOut->flags = MEM_Int; |
| 554 | return pOut; |
| 555 | } |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 556 | } |
| 557 | |
drh | b975598 | 2010-07-24 16:34:37 +0000 | [diff] [blame] | 558 | |
| 559 | /* |
drh | 0fd6135 | 2014-02-07 02:29:45 +0000 | [diff] [blame] | 560 | ** Execute as much of a VDBE program as we can. |
| 561 | ** This is the core of sqlite3_step(). |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 562 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 563 | int sqlite3VdbeExec( |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 564 | Vdbe *p /* The VDBE */ |
| 565 | ){ |
drh | bbe879d | 2009-11-14 18:04:35 +0000 | [diff] [blame] | 566 | Op *aOp = p->aOp; /* Copy of p->aOp */ |
mistachkin | 5f7b95f | 2017-02-01 23:03:54 +0000 | [diff] [blame] | 567 | Op *pOp = aOp; /* Current operation */ |
drh | 6dc4148 | 2015-04-16 17:31:02 +0000 | [diff] [blame] | 568 | #if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE) |
| 569 | Op *pOrigOp; /* Value of pOp at the top of the loop */ |
| 570 | #endif |
drh | b89aeb6 | 2016-01-27 15:49:32 +0000 | [diff] [blame] | 571 | #ifdef SQLITE_DEBUG |
drh | def19e3 | 2016-01-27 16:26:25 +0000 | [diff] [blame] | 572 | int nExtraDelete = 0; /* Verifies FORDELETE and AUXDELETE flags */ |
drh | b89aeb6 | 2016-01-27 15:49:32 +0000 | [diff] [blame] | 573 | #endif |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 574 | int rc = SQLITE_OK; /* Value to return */ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 575 | sqlite3 *db = p->db; /* The database */ |
drh | cdf011d | 2011-04-04 21:25:28 +0000 | [diff] [blame] | 576 | u8 resetSchemaOnFault = 0; /* Reset schema after an error if positive */ |
drh | 8079a0d | 2006-01-12 17:20:50 +0000 | [diff] [blame] | 577 | u8 encoding = ENC(db); /* The database encoding */ |
drh | 0f825a7 | 2016-08-13 14:17:02 +0000 | [diff] [blame] | 578 | int iCompare = 0; /* Result of last comparison */ |
drh | bf159fa | 2013-06-25 22:01:22 +0000 | [diff] [blame] | 579 | unsigned nVmStep = 0; /* Number of virtual machine steps */ |
drh | 49afe3a | 2013-07-10 03:05:14 +0000 | [diff] [blame] | 580 | #ifndef SQLITE_OMIT_PROGRESS_CALLBACK |
drh | 2ab792e | 2017-05-30 18:34:07 +0000 | [diff] [blame] | 581 | unsigned nProgressLimit; /* Invoke xProgress() when nVmStep reaches this */ |
drh | 49afe3a | 2013-07-10 03:05:14 +0000 | [diff] [blame] | 582 | #endif |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 583 | Mem *aMem = p->aMem; /* Copy of p->aMem */ |
drh | b27b7f5 | 2008-12-10 18:03:45 +0000 | [diff] [blame] | 584 | Mem *pIn1 = 0; /* 1st input operand */ |
| 585 | Mem *pIn2 = 0; /* 2nd input operand */ |
| 586 | Mem *pIn3 = 0; /* 3rd input operand */ |
| 587 | Mem *pOut = 0; /* Output operand */ |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 588 | #ifdef VDBE_PROFILE |
shane | 9bcbdad | 2008-05-29 20:22:37 +0000 | [diff] [blame] | 589 | u64 start; /* CPU clock count at start of opcode */ |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 590 | #endif |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 591 | /*** INSERT STACK UNION HERE ***/ |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 592 | |
drh | ca48c90 | 2008-01-18 14:08:24 +0000 | [diff] [blame] | 593 | assert( p->magic==VDBE_MAGIC_RUN ); /* sqlite3_step() verifies this */ |
drh | bdaec52 | 2011-04-04 00:14:43 +0000 | [diff] [blame] | 594 | sqlite3VdbeEnter(p); |
danielk1977 | 2e588c7 | 2005-12-09 14:25:08 +0000 | [diff] [blame] | 595 | if( p->rc==SQLITE_NOMEM ){ |
| 596 | /* This happens if a malloc() inside a call to sqlite3_column_text() or |
| 597 | ** sqlite3_column_text16() failed. */ |
| 598 | goto no_mem; |
| 599 | } |
drh | cbd8db3 | 2015-08-20 17:18:32 +0000 | [diff] [blame] | 600 | assert( p->rc==SQLITE_OK || (p->rc&0xff)==SQLITE_BUSY ); |
drh | 1713afb | 2013-06-28 01:24:57 +0000 | [diff] [blame] | 601 | assert( p->bIsReader || p->readOnly!=0 ); |
drh | 95a7b3e | 2013-09-16 12:57:19 +0000 | [diff] [blame] | 602 | p->iCurrentTime = 0; |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 603 | assert( p->explain==0 ); |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 604 | p->pResultSet = 0; |
drh | a4afb65 | 2005-07-09 02:16:02 +0000 | [diff] [blame] | 605 | db->busyHandler.nBusy = 0; |
drh | 0fd6135 | 2014-02-07 02:29:45 +0000 | [diff] [blame] | 606 | if( db->u1.isInterrupted ) goto abort_due_to_interrupt; |
drh | 602c237 | 2007-03-01 00:29:13 +0000 | [diff] [blame] | 607 | sqlite3VdbeIOTraceSql(p); |
drh | 0d1961e | 2013-07-25 16:27:51 +0000 | [diff] [blame] | 608 | #ifndef SQLITE_OMIT_PROGRESS_CALLBACK |
| 609 | if( db->xProgress ){ |
drh | 6cbbdb0 | 2015-06-24 14:36:27 +0000 | [diff] [blame] | 610 | u32 iPrior = p->aCounter[SQLITE_STMTSTATUS_VM_STEP]; |
drh | 0d1961e | 2013-07-25 16:27:51 +0000 | [diff] [blame] | 611 | assert( 0 < db->nProgressOps ); |
drh | 6cbbdb0 | 2015-06-24 14:36:27 +0000 | [diff] [blame] | 612 | nProgressLimit = db->nProgressOps - (iPrior % db->nProgressOps); |
drh | 2ab792e | 2017-05-30 18:34:07 +0000 | [diff] [blame] | 613 | }else{ |
| 614 | nProgressLimit = 0xffffffff; |
drh | 0d1961e | 2013-07-25 16:27:51 +0000 | [diff] [blame] | 615 | } |
| 616 | #endif |
drh | 3c23a88 | 2007-01-09 14:01:13 +0000 | [diff] [blame] | 617 | #ifdef SQLITE_DEBUG |
danielk1977 | 2d1d86f | 2008-06-20 14:59:51 +0000 | [diff] [blame] | 618 | sqlite3BeginBenignMalloc(); |
drh | 84e55a8 | 2013-11-13 17:58:23 +0000 | [diff] [blame] | 619 | if( p->pc==0 |
| 620 | && (p->db->flags & (SQLITE_VdbeListing|SQLITE_VdbeEQP|SQLITE_VdbeTrace))!=0 |
| 621 | ){ |
drh | 3c23a88 | 2007-01-09 14:01:13 +0000 | [diff] [blame] | 622 | int i; |
drh | 84e55a8 | 2013-11-13 17:58:23 +0000 | [diff] [blame] | 623 | int once = 1; |
drh | 3c23a88 | 2007-01-09 14:01:13 +0000 | [diff] [blame] | 624 | sqlite3VdbePrintSql(p); |
drh | 84e55a8 | 2013-11-13 17:58:23 +0000 | [diff] [blame] | 625 | if( p->db->flags & SQLITE_VdbeListing ){ |
| 626 | printf("VDBE Program Listing:\n"); |
| 627 | for(i=0; i<p->nOp; i++){ |
| 628 | sqlite3VdbePrintOp(stdout, i, &aOp[i]); |
| 629 | } |
drh | 3c23a88 | 2007-01-09 14:01:13 +0000 | [diff] [blame] | 630 | } |
drh | 84e55a8 | 2013-11-13 17:58:23 +0000 | [diff] [blame] | 631 | if( p->db->flags & SQLITE_VdbeEQP ){ |
| 632 | for(i=0; i<p->nOp; i++){ |
| 633 | if( aOp[i].opcode==OP_Explain ){ |
| 634 | if( once ) printf("VDBE Query Plan:\n"); |
| 635 | printf("%s\n", aOp[i].p4.z); |
| 636 | once = 0; |
| 637 | } |
| 638 | } |
| 639 | } |
| 640 | if( p->db->flags & SQLITE_VdbeTrace ) printf("VDBE Trace:\n"); |
drh | 3c23a88 | 2007-01-09 14:01:13 +0000 | [diff] [blame] | 641 | } |
danielk1977 | 2d1d86f | 2008-06-20 14:59:51 +0000 | [diff] [blame] | 642 | sqlite3EndBenignMalloc(); |
drh | 3c23a88 | 2007-01-09 14:01:13 +0000 | [diff] [blame] | 643 | #endif |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 644 | for(pOp=&aOp[p->pc]; 1; pOp++){ |
| 645 | /* Errors are detected by individual opcodes, with an immediate |
| 646 | ** jumps to abort_due_to_error. */ |
| 647 | assert( rc==SQLITE_OK ); |
| 648 | |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 649 | assert( pOp>=aOp && pOp<&aOp[p->nOp]); |
drh | 7b39686 | 2003-01-01 23:06:20 +0000 | [diff] [blame] | 650 | #ifdef VDBE_PROFILE |
shane | 9bcbdad | 2008-05-29 20:22:37 +0000 | [diff] [blame] | 651 | start = sqlite3Hwtime(); |
drh | 7b39686 | 2003-01-01 23:06:20 +0000 | [diff] [blame] | 652 | #endif |
drh | bf159fa | 2013-06-25 22:01:22 +0000 | [diff] [blame] | 653 | nVmStep++; |
dan | 6f9702e | 2014-11-01 20:38:06 +0000 | [diff] [blame] | 654 | #ifdef SQLITE_ENABLE_STMT_SCANSTATUS |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 655 | if( p->anExec ) p->anExec[(int)(pOp-aOp)]++; |
dan | 6f9702e | 2014-11-01 20:38:06 +0000 | [diff] [blame] | 656 | #endif |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 657 | |
danielk1977 | 8b60e0f | 2005-01-12 09:10:39 +0000 | [diff] [blame] | 658 | /* Only allow tracing if SQLITE_DEBUG is defined. |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 659 | */ |
danielk1977 | 8b60e0f | 2005-01-12 09:10:39 +0000 | [diff] [blame] | 660 | #ifdef SQLITE_DEBUG |
drh | 84e55a8 | 2013-11-13 17:58:23 +0000 | [diff] [blame] | 661 | if( db->flags & SQLITE_VdbeTrace ){ |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 662 | sqlite3VdbePrintOp(stdout, (int)(pOp - aOp), pOp); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 663 | } |
drh | 3f7d4e4 | 2004-07-24 14:35:58 +0000 | [diff] [blame] | 664 | #endif |
| 665 | |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 666 | |
drh | f603871 | 2004-02-08 18:07:34 +0000 | [diff] [blame] | 667 | /* Check to see if we need to simulate an interrupt. This only happens |
| 668 | ** if we have a special test build. |
| 669 | */ |
| 670 | #ifdef SQLITE_TEST |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 671 | if( sqlite3_interrupt_count>0 ){ |
| 672 | sqlite3_interrupt_count--; |
| 673 | if( sqlite3_interrupt_count==0 ){ |
| 674 | sqlite3_interrupt(db); |
drh | f603871 | 2004-02-08 18:07:34 +0000 | [diff] [blame] | 675 | } |
| 676 | } |
| 677 | #endif |
| 678 | |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 679 | /* Sanity checking on other operands */ |
| 680 | #ifdef SQLITE_DEBUG |
drh | 7cc84c2 | 2016-04-11 13:36:42 +0000 | [diff] [blame] | 681 | { |
| 682 | u8 opProperty = sqlite3OpcodeProperty[pOp->opcode]; |
| 683 | if( (opProperty & OPFLG_IN1)!=0 ){ |
| 684 | assert( pOp->p1>0 ); |
| 685 | assert( pOp->p1<=(p->nMem+1 - p->nCursor) ); |
| 686 | assert( memIsValid(&aMem[pOp->p1]) ); |
| 687 | assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p1]) ); |
| 688 | REGISTER_TRACE(pOp->p1, &aMem[pOp->p1]); |
| 689 | } |
| 690 | if( (opProperty & OPFLG_IN2)!=0 ){ |
| 691 | assert( pOp->p2>0 ); |
| 692 | assert( pOp->p2<=(p->nMem+1 - p->nCursor) ); |
| 693 | assert( memIsValid(&aMem[pOp->p2]) ); |
| 694 | assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p2]) ); |
| 695 | REGISTER_TRACE(pOp->p2, &aMem[pOp->p2]); |
| 696 | } |
| 697 | if( (opProperty & OPFLG_IN3)!=0 ){ |
| 698 | assert( pOp->p3>0 ); |
| 699 | assert( pOp->p3<=(p->nMem+1 - p->nCursor) ); |
| 700 | assert( memIsValid(&aMem[pOp->p3]) ); |
| 701 | assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p3]) ); |
| 702 | REGISTER_TRACE(pOp->p3, &aMem[pOp->p3]); |
| 703 | } |
| 704 | if( (opProperty & OPFLG_OUT2)!=0 ){ |
| 705 | assert( pOp->p2>0 ); |
| 706 | assert( pOp->p2<=(p->nMem+1 - p->nCursor) ); |
| 707 | memAboutToChange(p, &aMem[pOp->p2]); |
| 708 | } |
| 709 | if( (opProperty & OPFLG_OUT3)!=0 ){ |
| 710 | assert( pOp->p3>0 ); |
| 711 | assert( pOp->p3<=(p->nMem+1 - p->nCursor) ); |
| 712 | memAboutToChange(p, &aMem[pOp->p3]); |
| 713 | } |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 714 | } |
| 715 | #endif |
drh | 6dc4148 | 2015-04-16 17:31:02 +0000 | [diff] [blame] | 716 | #if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE) |
| 717 | pOrigOp = pOp; |
| 718 | #endif |
drh | 93952eb | 2009-11-13 19:43:43 +0000 | [diff] [blame] | 719 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 720 | switch( pOp->opcode ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 721 | |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 722 | /***************************************************************************** |
| 723 | ** What follows is a massive switch statement where each case implements a |
| 724 | ** separate instruction in the virtual machine. If we follow the usual |
| 725 | ** indentation conventions, each case should be indented by 6 spaces. But |
| 726 | ** that is a lot of wasted space on the left margin. So the code within |
| 727 | ** the switch statement will break with convention and be flush-left. Another |
| 728 | ** big comment (similar to this one) will mark the point in the code where |
| 729 | ** we transition back to normal indentation. |
drh | ac82fcf | 2002-09-08 17:23:41 +0000 | [diff] [blame] | 730 | ** |
| 731 | ** The formatting of each case is important. The makefile for SQLite |
| 732 | ** generates two C files "opcodes.h" and "opcodes.c" by scanning this |
| 733 | ** file looking for lines that begin with "case OP_". The opcodes.h files |
| 734 | ** will be filled with #defines that give unique integer values to each |
| 735 | ** 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] | 736 | ** each string is the symbolic name for the corresponding opcode. If the |
| 737 | ** case statement is followed by a comment of the form "/# same as ... #/" |
| 738 | ** that comment is used to determine the particular value of the opcode. |
drh | ac82fcf | 2002-09-08 17:23:41 +0000 | [diff] [blame] | 739 | ** |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 740 | ** Other keywords in the comment that follows each case are used to |
| 741 | ** construct the OPFLG_INITIALIZER value that initializes opcodeProperty[]. |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 742 | ** Keywords include: in1, in2, in3, out2, out3. See |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 743 | ** the mkopcodeh.awk script for additional information. |
danielk1977 | bc04f85 | 2005-03-29 08:26:13 +0000 | [diff] [blame] | 744 | ** |
drh | ac82fcf | 2002-09-08 17:23:41 +0000 | [diff] [blame] | 745 | ** Documentation about VDBE opcodes is generated by scanning this file |
| 746 | ** for lines of that contain "Opcode:". That line and all subsequent |
| 747 | ** comment lines are used in the generation of the opcode.html documentation |
| 748 | ** file. |
| 749 | ** |
| 750 | ** SUMMARY: |
| 751 | ** |
| 752 | ** Formatting is important to scripts that scan this file. |
| 753 | ** Do not deviate from the formatting style currently in use. |
| 754 | ** |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 755 | *****************************************************************************/ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 756 | |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 757 | /* Opcode: Goto * P2 * * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 758 | ** |
| 759 | ** An unconditional jump to address P2. |
| 760 | ** The next instruction executed will be |
| 761 | ** the one at index P2 from the beginning of |
| 762 | ** the program. |
drh | fe70510 | 2014-03-06 13:38:37 +0000 | [diff] [blame] | 763 | ** |
| 764 | ** The P1 parameter is not actually used by this opcode. However, it |
| 765 | ** is sometimes set to 1 instead of 0 as a hint to the command-line shell |
| 766 | ** that this Goto is the bottom of a loop and that the lines from P2 down |
| 767 | ** to the current line should be indented for EXPLAIN output. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 768 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 769 | case OP_Goto: { /* jump */ |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 770 | jump_to_p2_and_check_for_interrupt: |
| 771 | pOp = &aOp[pOp->p2 - 1]; |
drh | 49afe3a | 2013-07-10 03:05:14 +0000 | [diff] [blame] | 772 | |
| 773 | /* 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] | 774 | ** OP_VNext, or OP_SorterNext) all jump here upon |
drh | 49afe3a | 2013-07-10 03:05:14 +0000 | [diff] [blame] | 775 | ** completion. Check to see if sqlite3_interrupt() has been called |
| 776 | ** or if the progress callback needs to be invoked. |
| 777 | ** |
| 778 | ** This code uses unstructured "goto" statements and does not look clean. |
| 779 | ** But that is not due to sloppy coding habits. The code is written this |
| 780 | ** way for performance, to avoid having to run the interrupt and progress |
| 781 | ** checks on every opcode. This helps sqlite3_step() to run about 1.5% |
| 782 | ** faster according to "valgrind --tool=cachegrind" */ |
| 783 | check_for_interrupt: |
drh | 0fd6135 | 2014-02-07 02:29:45 +0000 | [diff] [blame] | 784 | if( db->u1.isInterrupted ) goto abort_due_to_interrupt; |
drh | 49afe3a | 2013-07-10 03:05:14 +0000 | [diff] [blame] | 785 | #ifndef SQLITE_OMIT_PROGRESS_CALLBACK |
| 786 | /* Call the progress callback if it is configured and the required number |
| 787 | ** of VDBE ops have been executed (either since this invocation of |
| 788 | ** sqlite3VdbeExec() or since last time the progress callback was called). |
| 789 | ** If the progress callback returns non-zero, exit the virtual machine with |
| 790 | ** a return code SQLITE_ABORT. |
| 791 | */ |
drh | 2ab792e | 2017-05-30 18:34:07 +0000 | [diff] [blame] | 792 | if( nVmStep>=nProgressLimit && db->xProgress!=0 ){ |
drh | 400fcba | 2013-11-14 00:09:48 +0000 | [diff] [blame] | 793 | assert( db->nProgressOps!=0 ); |
| 794 | nProgressLimit = nVmStep + db->nProgressOps - (nVmStep%db->nProgressOps); |
| 795 | if( db->xProgress(db->pProgressArg) ){ |
drh | 49afe3a | 2013-07-10 03:05:14 +0000 | [diff] [blame] | 796 | rc = SQLITE_INTERRUPT; |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 797 | goto abort_due_to_error; |
drh | 49afe3a | 2013-07-10 03:05:14 +0000 | [diff] [blame] | 798 | } |
drh | 49afe3a | 2013-07-10 03:05:14 +0000 | [diff] [blame] | 799 | } |
| 800 | #endif |
| 801 | |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 802 | break; |
| 803 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 804 | |
drh | 2eb9537 | 2008-06-06 15:04:36 +0000 | [diff] [blame] | 805 | /* Opcode: Gosub P1 P2 * * * |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 806 | ** |
drh | 2eb9537 | 2008-06-06 15:04:36 +0000 | [diff] [blame] | 807 | ** Write the current address onto register P1 |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 808 | ** and then jump to address P2. |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 809 | */ |
drh | b8475df | 2011-12-09 16:21:19 +0000 | [diff] [blame] | 810 | case OP_Gosub: { /* jump */ |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 811 | assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) ); |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 812 | pIn1 = &aMem[pOp->p1]; |
drh | c91b2fd | 2014-03-01 18:13:23 +0000 | [diff] [blame] | 813 | assert( VdbeMemDynamic(pIn1)==0 ); |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 814 | memAboutToChange(p, pIn1); |
drh | 2eb9537 | 2008-06-06 15:04:36 +0000 | [diff] [blame] | 815 | pIn1->flags = MEM_Int; |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 816 | pIn1->u.i = (int)(pOp-aOp); |
drh | 2eb9537 | 2008-06-06 15:04:36 +0000 | [diff] [blame] | 817 | REGISTER_TRACE(pOp->p1, pIn1); |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 818 | |
| 819 | /* Most jump operations do a goto to this spot in order to update |
| 820 | ** the pOp pointer. */ |
| 821 | jump_to_p2: |
| 822 | pOp = &aOp[pOp->p2 - 1]; |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 823 | break; |
| 824 | } |
| 825 | |
drh | 2eb9537 | 2008-06-06 15:04:36 +0000 | [diff] [blame] | 826 | /* Opcode: Return P1 * * * * |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 827 | ** |
drh | 81cf13e | 2014-02-07 18:27:53 +0000 | [diff] [blame] | 828 | ** Jump to the next instruction after the address in register P1. After |
| 829 | ** the jump, register P1 becomes undefined. |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 830 | */ |
drh | 2eb9537 | 2008-06-06 15:04:36 +0000 | [diff] [blame] | 831 | case OP_Return: { /* in1 */ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 832 | pIn1 = &aMem[pOp->p1]; |
drh | 81cf13e | 2014-02-07 18:27:53 +0000 | [diff] [blame] | 833 | assert( pIn1->flags==MEM_Int ); |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 834 | pOp = &aOp[pIn1->u.i]; |
drh | 81cf13e | 2014-02-07 18:27:53 +0000 | [diff] [blame] | 835 | pIn1->flags = MEM_Undefined; |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 836 | break; |
| 837 | } |
| 838 | |
drh | ed71a83 | 2014-02-07 19:18:10 +0000 | [diff] [blame] | 839 | /* Opcode: InitCoroutine P1 P2 P3 * * |
drh | 81cf13e | 2014-02-07 18:27:53 +0000 | [diff] [blame] | 840 | ** |
drh | 5dad9a3 | 2014-07-25 18:37:42 +0000 | [diff] [blame] | 841 | ** Set up register P1 so that it will Yield to the coroutine |
drh | ed71a83 | 2014-02-07 19:18:10 +0000 | [diff] [blame] | 842 | ** located at address P3. |
| 843 | ** |
drh | 5dad9a3 | 2014-07-25 18:37:42 +0000 | [diff] [blame] | 844 | ** If P2!=0 then the coroutine implementation immediately follows |
| 845 | ** this opcode. So jump over the coroutine implementation to |
drh | ed71a83 | 2014-02-07 19:18:10 +0000 | [diff] [blame] | 846 | ** address P2. |
drh | 5dad9a3 | 2014-07-25 18:37:42 +0000 | [diff] [blame] | 847 | ** |
| 848 | ** See also: EndCoroutine |
drh | 81cf13e | 2014-02-07 18:27:53 +0000 | [diff] [blame] | 849 | */ |
| 850 | case OP_InitCoroutine: { /* jump */ |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 851 | assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) ); |
drh | ed71a83 | 2014-02-07 19:18:10 +0000 | [diff] [blame] | 852 | assert( pOp->p2>=0 && pOp->p2<p->nOp ); |
| 853 | assert( pOp->p3>=0 && pOp->p3<p->nOp ); |
drh | 81cf13e | 2014-02-07 18:27:53 +0000 | [diff] [blame] | 854 | pOut = &aMem[pOp->p1]; |
drh | ed71a83 | 2014-02-07 19:18:10 +0000 | [diff] [blame] | 855 | assert( !VdbeMemDynamic(pOut) ); |
| 856 | pOut->u.i = pOp->p3 - 1; |
drh | 81cf13e | 2014-02-07 18:27:53 +0000 | [diff] [blame] | 857 | pOut->flags = MEM_Int; |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 858 | if( pOp->p2 ) goto jump_to_p2; |
drh | 81cf13e | 2014-02-07 18:27:53 +0000 | [diff] [blame] | 859 | break; |
| 860 | } |
| 861 | |
| 862 | /* Opcode: EndCoroutine P1 * * * * |
| 863 | ** |
drh | bc5cf38 | 2014-08-06 01:08:07 +0000 | [diff] [blame] | 864 | ** The instruction at the address in register P1 is a Yield. |
drh | 5dad9a3 | 2014-07-25 18:37:42 +0000 | [diff] [blame] | 865 | ** Jump to the P2 parameter of that Yield. |
drh | 81cf13e | 2014-02-07 18:27:53 +0000 | [diff] [blame] | 866 | ** After the jump, register P1 becomes undefined. |
drh | 5dad9a3 | 2014-07-25 18:37:42 +0000 | [diff] [blame] | 867 | ** |
| 868 | ** See also: InitCoroutine |
drh | 81cf13e | 2014-02-07 18:27:53 +0000 | [diff] [blame] | 869 | */ |
| 870 | case OP_EndCoroutine: { /* in1 */ |
| 871 | VdbeOp *pCaller; |
| 872 | pIn1 = &aMem[pOp->p1]; |
| 873 | assert( pIn1->flags==MEM_Int ); |
| 874 | assert( pIn1->u.i>=0 && pIn1->u.i<p->nOp ); |
| 875 | pCaller = &aOp[pIn1->u.i]; |
| 876 | assert( pCaller->opcode==OP_Yield ); |
| 877 | assert( pCaller->p2>=0 && pCaller->p2<p->nOp ); |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 878 | pOp = &aOp[pCaller->p2 - 1]; |
drh | 81cf13e | 2014-02-07 18:27:53 +0000 | [diff] [blame] | 879 | pIn1->flags = MEM_Undefined; |
| 880 | break; |
| 881 | } |
| 882 | |
| 883 | /* Opcode: Yield P1 P2 * * * |
drh | e00ee6e | 2008-06-20 15:24:01 +0000 | [diff] [blame] | 884 | ** |
drh | 5dad9a3 | 2014-07-25 18:37:42 +0000 | [diff] [blame] | 885 | ** Swap the program counter with the value in register P1. This |
| 886 | ** has the effect of yielding to a coroutine. |
drh | 81cf13e | 2014-02-07 18:27:53 +0000 | [diff] [blame] | 887 | ** |
drh | 5dad9a3 | 2014-07-25 18:37:42 +0000 | [diff] [blame] | 888 | ** If the coroutine that is launched by this instruction ends with |
| 889 | ** Yield or Return then continue to the next instruction. But if |
| 890 | ** the coroutine launched by this instruction ends with |
| 891 | ** EndCoroutine, then jump to P2 rather than continuing with the |
| 892 | ** next instruction. |
| 893 | ** |
| 894 | ** See also: InitCoroutine |
drh | e00ee6e | 2008-06-20 15:24:01 +0000 | [diff] [blame] | 895 | */ |
drh | 81cf13e | 2014-02-07 18:27:53 +0000 | [diff] [blame] | 896 | case OP_Yield: { /* in1, jump */ |
drh | e00ee6e | 2008-06-20 15:24:01 +0000 | [diff] [blame] | 897 | int pcDest; |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 898 | pIn1 = &aMem[pOp->p1]; |
drh | c91b2fd | 2014-03-01 18:13:23 +0000 | [diff] [blame] | 899 | assert( VdbeMemDynamic(pIn1)==0 ); |
drh | e00ee6e | 2008-06-20 15:24:01 +0000 | [diff] [blame] | 900 | pIn1->flags = MEM_Int; |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 901 | pcDest = (int)pIn1->u.i; |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 902 | pIn1->u.i = (int)(pOp - aOp); |
drh | e00ee6e | 2008-06-20 15:24:01 +0000 | [diff] [blame] | 903 | REGISTER_TRACE(pOp->p1, pIn1); |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 904 | pOp = &aOp[pcDest]; |
drh | e00ee6e | 2008-06-20 15:24:01 +0000 | [diff] [blame] | 905 | break; |
| 906 | } |
| 907 | |
drh | f9c8ce3 | 2013-11-05 13:33:55 +0000 | [diff] [blame] | 908 | /* Opcode: HaltIfNull P1 P2 P3 P4 P5 |
drh | 72e26de | 2016-08-24 21:24:04 +0000 | [diff] [blame] | 909 | ** Synopsis: if r[P3]=null halt |
drh | 5053a79 | 2009-02-20 03:02:23 +0000 | [diff] [blame] | 910 | ** |
drh | ef8662b | 2011-06-20 21:47:58 +0000 | [diff] [blame] | 911 | ** Check the value in register P3. If it is NULL then Halt using |
drh | 5053a79 | 2009-02-20 03:02:23 +0000 | [diff] [blame] | 912 | ** parameter P1, P2, and P4 as if this were a Halt instruction. If the |
| 913 | ** 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] | 914 | ** The P5 parameter should be 1. |
drh | 5053a79 | 2009-02-20 03:02:23 +0000 | [diff] [blame] | 915 | */ |
| 916 | case OP_HaltIfNull: { /* in3 */ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 917 | pIn3 = &aMem[pOp->p3]; |
drh | 5053a79 | 2009-02-20 03:02:23 +0000 | [diff] [blame] | 918 | if( (pIn3->flags & MEM_Null)==0 ) break; |
| 919 | /* Fall through into OP_Halt */ |
| 920 | } |
drh | e00ee6e | 2008-06-20 15:24:01 +0000 | [diff] [blame] | 921 | |
drh | f9c8ce3 | 2013-11-05 13:33:55 +0000 | [diff] [blame] | 922 | /* Opcode: Halt P1 P2 * P4 P5 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 923 | ** |
drh | 3d4501e | 2008-12-04 20:40:10 +0000 | [diff] [blame] | 924 | ** Exit immediately. All open cursors, etc are closed |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 925 | ** automatically. |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 926 | ** |
drh | 92f02c3 | 2004-09-02 14:57:08 +0000 | [diff] [blame] | 927 | ** P1 is the result code returned by sqlite3_exec(), sqlite3_reset(), |
| 928 | ** or sqlite3_finalize(). For a normal halt, this should be SQLITE_OK (0). |
| 929 | ** For errors, it can be some other value. If P1!=0 then P2 will determine |
| 930 | ** whether or not to rollback the current transaction. Do not rollback |
| 931 | ** if P2==OE_Fail. Do the rollback if P2==OE_Rollback. If P2==OE_Abort, |
| 932 | ** then back out all changes that have occurred during this execution of the |
drh | b798fa6 | 2002-09-03 19:43:23 +0000 | [diff] [blame] | 933 | ** VDBE, but do not rollback the transaction. |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 934 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 935 | ** If P4 is not null then it is an error message string. |
drh | 7f057c9 | 2005-06-24 03:53:06 +0000 | [diff] [blame] | 936 | ** |
drh | f9c8ce3 | 2013-11-05 13:33:55 +0000 | [diff] [blame] | 937 | ** P5 is a value between 0 and 4, inclusive, that modifies the P4 string. |
| 938 | ** |
| 939 | ** 0: (no change) |
| 940 | ** 1: NOT NULL contraint failed: P4 |
| 941 | ** 2: UNIQUE constraint failed: P4 |
| 942 | ** 3: CHECK constraint failed: P4 |
| 943 | ** 4: FOREIGN KEY constraint failed: P4 |
| 944 | ** |
| 945 | ** If P5 is not zero and P4 is NULL, then everything after the ":" is |
| 946 | ** omitted. |
| 947 | ** |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 948 | ** 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] | 949 | ** every program. So a jump past the last instruction of the program |
| 950 | ** is the same as executing Halt. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 951 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 952 | case OP_Halt: { |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 953 | VdbeFrame *pFrame; |
| 954 | int pcx; |
drh | f9c8ce3 | 2013-11-05 13:33:55 +0000 | [diff] [blame] | 955 | |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 956 | pcx = (int)(pOp - aOp); |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 957 | if( pOp->p1==SQLITE_OK && p->pFrame ){ |
dan | 2832ad4 | 2009-08-31 15:27:27 +0000 | [diff] [blame] | 958 | /* Halt the sub-program. Return control to the parent frame. */ |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 959 | pFrame = p->pFrame; |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 960 | p->pFrame = pFrame->pParent; |
| 961 | p->nFrame--; |
dan | 2832ad4 | 2009-08-31 15:27:27 +0000 | [diff] [blame] | 962 | sqlite3VdbeSetChanges(db, p->nChange); |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 963 | pcx = sqlite3VdbeFrameRestore(pFrame); |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 964 | if( pOp->p2==OE_Ignore ){ |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 965 | /* Instruction pcx is the OP_Program that invoked the sub-program |
dan | 2832ad4 | 2009-08-31 15:27:27 +0000 | [diff] [blame] | 966 | ** currently being halted. If the p2 instruction of this OP_Halt |
| 967 | ** instruction is set to OE_Ignore, then the sub-program is throwing |
| 968 | ** an IGNORE exception. In this case jump to the address specified |
| 969 | ** as the p2 of the calling OP_Program. */ |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 970 | pcx = p->aOp[pcx].p2-1; |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 971 | } |
drh | bbe879d | 2009-11-14 18:04:35 +0000 | [diff] [blame] | 972 | aOp = p->aOp; |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 973 | aMem = p->aMem; |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 974 | pOp = &aOp[pcx]; |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 975 | break; |
| 976 | } |
drh | 92f02c3 | 2004-09-02 14:57:08 +0000 | [diff] [blame] | 977 | p->rc = pOp->p1; |
shane | 36840fd | 2009-06-26 16:32:13 +0000 | [diff] [blame] | 978 | p->errorAction = (u8)pOp->p2; |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 979 | p->pc = pcx; |
drh | fb4e3a3 | 2016-12-30 00:09:14 +0000 | [diff] [blame] | 980 | assert( pOp->p5<=4 ); |
drh | f9c8ce3 | 2013-11-05 13:33:55 +0000 | [diff] [blame] | 981 | if( p->rc ){ |
drh | d9b7ec9 | 2013-11-06 14:05:21 +0000 | [diff] [blame] | 982 | if( pOp->p5 ){ |
| 983 | static const char * const azType[] = { "NOT NULL", "UNIQUE", "CHECK", |
| 984 | "FOREIGN KEY" }; |
drh | d9b7ec9 | 2013-11-06 14:05:21 +0000 | [diff] [blame] | 985 | testcase( pOp->p5==1 ); |
| 986 | testcase( pOp->p5==2 ); |
| 987 | testcase( pOp->p5==3 ); |
| 988 | testcase( pOp->p5==4 ); |
drh | 99f5de7 | 2016-04-30 02:59:15 +0000 | [diff] [blame] | 989 | sqlite3VdbeError(p, "%s constraint failed", azType[pOp->p5-1]); |
| 990 | if( pOp->p4.z ){ |
| 991 | p->zErrMsg = sqlite3MPrintf(db, "%z: %s", p->zErrMsg, pOp->p4.z); |
| 992 | } |
drh | d9b7ec9 | 2013-11-06 14:05:21 +0000 | [diff] [blame] | 993 | }else{ |
drh | 22c17b8 | 2015-05-15 04:13:15 +0000 | [diff] [blame] | 994 | sqlite3VdbeError(p, "%s", pOp->p4.z); |
drh | f9c8ce3 | 2013-11-05 13:33:55 +0000 | [diff] [blame] | 995 | } |
drh | 99f5de7 | 2016-04-30 02:59:15 +0000 | [diff] [blame] | 996 | 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] | 997 | } |
drh | 92f02c3 | 2004-09-02 14:57:08 +0000 | [diff] [blame] | 998 | rc = sqlite3VdbeHalt(p); |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 999 | assert( rc==SQLITE_BUSY || rc==SQLITE_OK || rc==SQLITE_ERROR ); |
drh | 92f02c3 | 2004-09-02 14:57:08 +0000 | [diff] [blame] | 1000 | if( rc==SQLITE_BUSY ){ |
drh | 99f5de7 | 2016-04-30 02:59:15 +0000 | [diff] [blame] | 1001 | p->rc = SQLITE_BUSY; |
drh | 900b31e | 2007-08-28 02:27:51 +0000 | [diff] [blame] | 1002 | }else{ |
drh | d91c1a1 | 2013-02-09 13:58:25 +0000 | [diff] [blame] | 1003 | assert( rc==SQLITE_OK || (p->rc&0xff)==SQLITE_CONSTRAINT ); |
dan | cb3e4b7 | 2013-07-03 19:53:05 +0000 | [diff] [blame] | 1004 | assert( rc==SQLITE_OK || db->nDeferredCons>0 || db->nDeferredImmCons>0 ); |
drh | 900b31e | 2007-08-28 02:27:51 +0000 | [diff] [blame] | 1005 | rc = p->rc ? SQLITE_ERROR : SQLITE_DONE; |
drh | 92f02c3 | 2004-09-02 14:57:08 +0000 | [diff] [blame] | 1006 | } |
drh | 900b31e | 2007-08-28 02:27:51 +0000 | [diff] [blame] | 1007 | goto vdbe_return; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1008 | } |
drh | c61053b | 2000-06-04 12:58:36 +0000 | [diff] [blame] | 1009 | |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 1010 | /* Opcode: Integer P1 P2 * * * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 1011 | ** Synopsis: r[P2]=P1 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1012 | ** |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1013 | ** The 32-bit integer value P1 is written into register P2. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1014 | */ |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 1015 | case OP_Integer: { /* out2 */ |
| 1016 | pOut = out2Prerelease(p, pOp); |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 1017 | pOut->u.i = pOp->p1; |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 1018 | break; |
| 1019 | } |
| 1020 | |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 1021 | /* Opcode: Int64 * P2 * P4 * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 1022 | ** Synopsis: r[P2]=P4 |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 1023 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 1024 | ** P4 is a pointer to a 64-bit integer value. |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1025 | ** Write that value into register P2. |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 1026 | */ |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 1027 | case OP_Int64: { /* out2 */ |
| 1028 | pOut = out2Prerelease(p, pOp); |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 1029 | assert( pOp->p4.pI64!=0 ); |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 1030 | pOut->u.i = *pOp->p4.pI64; |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 1031 | break; |
| 1032 | } |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 1033 | |
drh | 13573c7 | 2010-01-12 17:04:07 +0000 | [diff] [blame] | 1034 | #ifndef SQLITE_OMIT_FLOATING_POINT |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 1035 | /* Opcode: Real * P2 * P4 * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 1036 | ** Synopsis: r[P2]=P4 |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 1037 | ** |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 1038 | ** P4 is a pointer to a 64-bit floating point value. |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1039 | ** Write that value into register P2. |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 1040 | */ |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 1041 | case OP_Real: { /* same as TK_FLOAT, out2 */ |
| 1042 | pOut = out2Prerelease(p, pOp); |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 1043 | pOut->flags = MEM_Real; |
drh | 2eaf93d | 2008-04-29 00:15:20 +0000 | [diff] [blame] | 1044 | assert( !sqlite3IsNaN(*pOp->p4.pReal) ); |
drh | 74eaba4 | 2014-09-18 17:52:15 +0000 | [diff] [blame] | 1045 | pOut->u.r = *pOp->p4.pReal; |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 1046 | break; |
| 1047 | } |
drh | 13573c7 | 2010-01-12 17:04:07 +0000 | [diff] [blame] | 1048 | #endif |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 1049 | |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 1050 | /* Opcode: String8 * P2 * P4 * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 1051 | ** Synopsis: r[P2]='P4' |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 1052 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 1053 | ** P4 points to a nul terminated UTF-8 string. This opcode is transformed |
drh | f07cf6e | 2015-03-06 16:45:16 +0000 | [diff] [blame] | 1054 | ** into a String opcode before it is executed for the first time. During |
drh | 0fd6135 | 2014-02-07 02:29:45 +0000 | [diff] [blame] | 1055 | ** this transformation, the length of string P4 is computed and stored |
| 1056 | ** as the P1 parameter. |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 1057 | */ |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 1058 | case OP_String8: { /* same as TK_STRING, out2 */ |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 1059 | assert( pOp->p4.z!=0 ); |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 1060 | pOut = out2Prerelease(p, pOp); |
drh | ed2df7f | 2005-11-16 04:34:32 +0000 | [diff] [blame] | 1061 | pOp->opcode = OP_String; |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 1062 | pOp->p1 = sqlite3Strlen30(pOp->p4.z); |
drh | ed2df7f | 2005-11-16 04:34:32 +0000 | [diff] [blame] | 1063 | |
| 1064 | #ifndef SQLITE_OMIT_UTF16 |
drh | 8079a0d | 2006-01-12 17:20:50 +0000 | [diff] [blame] | 1065 | if( encoding!=SQLITE_UTF8 ){ |
drh | 3a9cf17 | 2009-06-17 21:42:33 +0000 | [diff] [blame] | 1066 | rc = sqlite3VdbeMemSetStr(pOut, pOp->p4.z, -1, SQLITE_UTF8, SQLITE_STATIC); |
drh | 2f55511 | 2016-04-30 18:10:34 +0000 | [diff] [blame] | 1067 | assert( rc==SQLITE_OK || rc==SQLITE_TOOBIG ); |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 1068 | if( SQLITE_OK!=sqlite3VdbeChangeEncoding(pOut, encoding) ) goto no_mem; |
drh | 17bcb10 | 2014-09-18 21:25:33 +0000 | [diff] [blame] | 1069 | assert( pOut->szMalloc>0 && pOut->zMalloc==pOut->z ); |
drh | c91b2fd | 2014-03-01 18:13:23 +0000 | [diff] [blame] | 1070 | assert( VdbeMemDynamic(pOut)==0 ); |
drh | 17bcb10 | 2014-09-18 21:25:33 +0000 | [diff] [blame] | 1071 | pOut->szMalloc = 0; |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 1072 | pOut->flags |= MEM_Static; |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 1073 | if( pOp->p4type==P4_DYNAMIC ){ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 1074 | sqlite3DbFree(db, pOp->p4.z); |
danielk1977 | e004840 | 2004-06-15 16:51:01 +0000 | [diff] [blame] | 1075 | } |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 1076 | pOp->p4type = P4_DYNAMIC; |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 1077 | pOp->p4.z = pOut->z; |
| 1078 | pOp->p1 = pOut->n; |
danielk1977 | 0f69c1e | 2004-05-29 11:24:50 +0000 | [diff] [blame] | 1079 | } |
drh | 2f55511 | 2016-04-30 18:10:34 +0000 | [diff] [blame] | 1080 | testcase( rc==SQLITE_TOOBIG ); |
danielk1977 | 93758c8 | 2005-01-21 08:13:14 +0000 | [diff] [blame] | 1081 | #endif |
drh | bb4957f | 2008-03-20 14:03:29 +0000 | [diff] [blame] | 1082 | if( pOp->p1>db->aLimit[SQLITE_LIMIT_LENGTH] ){ |
drh | cbd2da9 | 2007-12-17 16:20:06 +0000 | [diff] [blame] | 1083 | goto too_big; |
| 1084 | } |
drh | 2f55511 | 2016-04-30 18:10:34 +0000 | [diff] [blame] | 1085 | assert( rc==SQLITE_OK ); |
drh | cbd2da9 | 2007-12-17 16:20:06 +0000 | [diff] [blame] | 1086 | /* Fall through to the next case, OP_String */ |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 1087 | } |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 1088 | |
drh | f07cf6e | 2015-03-06 16:45:16 +0000 | [diff] [blame] | 1089 | /* Opcode: String P1 P2 P3 P4 P5 |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 1090 | ** Synopsis: r[P2]='P4' (len=P1) |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 1091 | ** |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1092 | ** The string value P4 of length P1 (bytes) is stored in register P2. |
drh | f07cf6e | 2015-03-06 16:45:16 +0000 | [diff] [blame] | 1093 | ** |
drh | 44aebff | 2016-05-02 10:25:42 +0000 | [diff] [blame] | 1094 | ** 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] | 1095 | ** the datatype of the register P2 is converted to BLOB. The content is |
| 1096 | ** the same sequence of bytes, it is merely interpreted as a BLOB instead |
drh | 44aebff | 2016-05-02 10:25:42 +0000 | [diff] [blame] | 1097 | ** of a string, as if it had been CAST. In other words: |
| 1098 | ** |
| 1099 | ** 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] | 1100 | */ |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 1101 | case OP_String: { /* out2 */ |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 1102 | assert( pOp->p4.z!=0 ); |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 1103 | pOut = out2Prerelease(p, pOp); |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 1104 | pOut->flags = MEM_Str|MEM_Static|MEM_Term; |
| 1105 | pOut->z = pOp->p4.z; |
| 1106 | pOut->n = pOp->p1; |
| 1107 | pOut->enc = encoding; |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 1108 | UPDATE_MAX_BLOBSIZE(pOut); |
drh | 41d2e66 | 2015-12-01 21:23:07 +0000 | [diff] [blame] | 1109 | #ifndef SQLITE_LIKE_DOESNT_MATCH_BLOBS |
drh | 44aebff | 2016-05-02 10:25:42 +0000 | [diff] [blame] | 1110 | if( pOp->p3>0 ){ |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 1111 | assert( pOp->p3<=(p->nMem+1 - p->nCursor) ); |
drh | f07cf6e | 2015-03-06 16:45:16 +0000 | [diff] [blame] | 1112 | pIn3 = &aMem[pOp->p3]; |
| 1113 | assert( pIn3->flags & MEM_Int ); |
drh | 44aebff | 2016-05-02 10:25:42 +0000 | [diff] [blame] | 1114 | 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] | 1115 | } |
drh | 41d2e66 | 2015-12-01 21:23:07 +0000 | [diff] [blame] | 1116 | #endif |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 1117 | break; |
| 1118 | } |
| 1119 | |
drh | 053a128 | 2012-09-19 21:15:46 +0000 | [diff] [blame] | 1120 | /* Opcode: Null P1 P2 P3 * * |
drh | 72e26de | 2016-08-24 21:24:04 +0000 | [diff] [blame] | 1121 | ** Synopsis: r[P2..P3]=NULL |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 1122 | ** |
drh | b8475df | 2011-12-09 16:21:19 +0000 | [diff] [blame] | 1123 | ** 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] | 1124 | ** 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] | 1125 | ** is less than P2 (typically P3 is zero) then only register P2 is |
drh | 053a128 | 2012-09-19 21:15:46 +0000 | [diff] [blame] | 1126 | ** set to NULL. |
| 1127 | ** |
| 1128 | ** If the P1 value is non-zero, then also set the MEM_Cleared flag so that |
| 1129 | ** NULL values will not compare equal even if SQLITE_NULLEQ is set on |
| 1130 | ** OP_Ne or OP_Eq. |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 1131 | */ |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 1132 | case OP_Null: { /* out2 */ |
drh | b8475df | 2011-12-09 16:21:19 +0000 | [diff] [blame] | 1133 | int cnt; |
drh | 053a128 | 2012-09-19 21:15:46 +0000 | [diff] [blame] | 1134 | u16 nullFlag; |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 1135 | pOut = out2Prerelease(p, pOp); |
drh | b8475df | 2011-12-09 16:21:19 +0000 | [diff] [blame] | 1136 | cnt = pOp->p3-pOp->p2; |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 1137 | assert( pOp->p3<=(p->nMem+1 - p->nCursor) ); |
drh | 053a128 | 2012-09-19 21:15:46 +0000 | [diff] [blame] | 1138 | pOut->flags = nullFlag = pOp->p1 ? (MEM_Null|MEM_Cleared) : MEM_Null; |
drh | 2a1df93 | 2016-09-30 17:46:44 +0000 | [diff] [blame] | 1139 | pOut->n = 0; |
drh | b8475df | 2011-12-09 16:21:19 +0000 | [diff] [blame] | 1140 | while( cnt>0 ){ |
| 1141 | pOut++; |
| 1142 | memAboutToChange(p, pOut); |
drh | 0725cab | 2014-09-17 14:52:46 +0000 | [diff] [blame] | 1143 | sqlite3VdbeMemSetNull(pOut); |
drh | 053a128 | 2012-09-19 21:15:46 +0000 | [diff] [blame] | 1144 | pOut->flags = nullFlag; |
drh | 2a1df93 | 2016-09-30 17:46:44 +0000 | [diff] [blame] | 1145 | pOut->n = 0; |
drh | b8475df | 2011-12-09 16:21:19 +0000 | [diff] [blame] | 1146 | cnt--; |
| 1147 | } |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 1148 | break; |
| 1149 | } |
| 1150 | |
drh | 05a86c5 | 2014-02-16 01:55:49 +0000 | [diff] [blame] | 1151 | /* Opcode: SoftNull P1 * * * * |
drh | 72e26de | 2016-08-24 21:24:04 +0000 | [diff] [blame] | 1152 | ** Synopsis: r[P1]=NULL |
drh | 05a86c5 | 2014-02-16 01:55:49 +0000 | [diff] [blame] | 1153 | ** |
| 1154 | ** Set register P1 to have the value NULL as seen by the OP_MakeRecord |
| 1155 | ** instruction, but do not free any string or blob memory associated with |
| 1156 | ** the register, so that if the value was a string or blob that was |
| 1157 | ** previously copied using OP_SCopy, the copies will continue to be valid. |
| 1158 | */ |
| 1159 | case OP_SoftNull: { |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 1160 | assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) ); |
drh | 05a86c5 | 2014-02-16 01:55:49 +0000 | [diff] [blame] | 1161 | pOut = &aMem[pOp->p1]; |
drh | e2bc655 | 2017-04-17 20:50:34 +0000 | [diff] [blame] | 1162 | pOut->flags = (pOut->flags&~(MEM_Undefined|MEM_AffMask))|MEM_Null; |
drh | 05a86c5 | 2014-02-16 01:55:49 +0000 | [diff] [blame] | 1163 | break; |
| 1164 | } |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 1165 | |
drh | a5750cf | 2014-02-07 13:20:31 +0000 | [diff] [blame] | 1166 | /* Opcode: Blob P1 P2 * P4 * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 1167 | ** Synopsis: r[P2]=P4 (len=P1) |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 1168 | ** |
drh | 9de221d | 2008-01-05 06:51:30 +0000 | [diff] [blame] | 1169 | ** P4 points to a blob of data P1 bytes long. Store this |
drh | 710c484 | 2010-08-30 01:17:20 +0000 | [diff] [blame] | 1170 | ** blob in register P2. |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 1171 | */ |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 1172 | case OP_Blob: { /* out2 */ |
drh | cbd2da9 | 2007-12-17 16:20:06 +0000 | [diff] [blame] | 1173 | assert( pOp->p1 <= SQLITE_MAX_LENGTH ); |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 1174 | pOut = out2Prerelease(p, pOp); |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 1175 | sqlite3VdbeMemSetStr(pOut, pOp->p4.z, pOp->p1, 0, 0); |
drh | 9de221d | 2008-01-05 06:51:30 +0000 | [diff] [blame] | 1176 | pOut->enc = encoding; |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 1177 | UPDATE_MAX_BLOBSIZE(pOut); |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 1178 | break; |
| 1179 | } |
| 1180 | |
drh | eaf52d8 | 2010-05-12 13:50:23 +0000 | [diff] [blame] | 1181 | /* Opcode: Variable P1 P2 * P4 * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 1182 | ** Synopsis: r[P2]=parameter(P1,P4) |
drh | 5045789 | 2003-09-06 01:10:47 +0000 | [diff] [blame] | 1183 | ** |
drh | eaf52d8 | 2010-05-12 13:50:23 +0000 | [diff] [blame] | 1184 | ** Transfer the values of bound parameter P1 into register P2 |
drh | 08de149 | 2009-02-20 03:55:05 +0000 | [diff] [blame] | 1185 | ** |
drh | 0fd6135 | 2014-02-07 02:29:45 +0000 | [diff] [blame] | 1186 | ** If the parameter is named, then its name appears in P4. |
drh | 08de149 | 2009-02-20 03:55:05 +0000 | [diff] [blame] | 1187 | ** The P4 value is used by sqlite3_bind_parameter_name(). |
drh | 5045789 | 2003-09-06 01:10:47 +0000 | [diff] [blame] | 1188 | */ |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 1189 | case OP_Variable: { /* out2 */ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1190 | Mem *pVar; /* Value being transferred */ |
| 1191 | |
drh | eaf52d8 | 2010-05-12 13:50:23 +0000 | [diff] [blame] | 1192 | assert( pOp->p1>0 && pOp->p1<=p->nVar ); |
drh | 9bf755c | 2016-12-23 03:59:31 +0000 | [diff] [blame] | 1193 | assert( pOp->p4.z==0 || pOp->p4.z==sqlite3VListNumToName(p->pVList,pOp->p1) ); |
drh | eaf52d8 | 2010-05-12 13:50:23 +0000 | [diff] [blame] | 1194 | pVar = &p->aVar[pOp->p1 - 1]; |
| 1195 | if( sqlite3VdbeMemTooBig(pVar) ){ |
| 1196 | goto too_big; |
drh | 023ae03 | 2007-05-08 12:12:16 +0000 | [diff] [blame] | 1197 | } |
drh | 7441df7 | 2017-01-09 19:27:04 +0000 | [diff] [blame] | 1198 | pOut = &aMem[pOp->p2]; |
drh | eaf52d8 | 2010-05-12 13:50:23 +0000 | [diff] [blame] | 1199 | sqlite3VdbeMemShallowCopy(pOut, pVar, MEM_Static); |
| 1200 | UPDATE_MAX_BLOBSIZE(pOut); |
danielk1977 | 93d4675 | 2004-05-23 13:30:58 +0000 | [diff] [blame] | 1201 | break; |
| 1202 | } |
danielk1977 | 295ba55 | 2004-05-19 10:34:51 +0000 | [diff] [blame] | 1203 | |
drh | b21e7c7 | 2008-06-22 12:37:57 +0000 | [diff] [blame] | 1204 | /* Opcode: Move P1 P2 P3 * * |
drh | 72e26de | 2016-08-24 21:24:04 +0000 | [diff] [blame] | 1205 | ** Synopsis: r[P2@P3]=r[P1@P3] |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1206 | ** |
drh | 079a307 | 2014-03-19 14:10:55 +0000 | [diff] [blame] | 1207 | ** Move the P3 values in register P1..P1+P3-1 over into |
| 1208 | ** registers P2..P2+P3-1. Registers P1..P1+P3-1 are |
drh | b21e7c7 | 2008-06-22 12:37:57 +0000 | [diff] [blame] | 1209 | ** left holding a NULL. It is an error for register ranges |
drh | 079a307 | 2014-03-19 14:10:55 +0000 | [diff] [blame] | 1210 | ** P1..P1+P3-1 and P2..P2+P3-1 to overlap. It is an error |
| 1211 | ** for P3 to be less than 1. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1212 | */ |
drh | e1349cb | 2008-04-01 00:36:10 +0000 | [diff] [blame] | 1213 | case OP_Move: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1214 | int n; /* Number of registers left to copy */ |
| 1215 | int p1; /* Register to copy from */ |
| 1216 | int p2; /* Register to copy to */ |
| 1217 | |
drh | e09f43f | 2013-11-21 04:18:31 +0000 | [diff] [blame] | 1218 | n = pOp->p3; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1219 | p1 = pOp->p1; |
| 1220 | p2 = pOp->p2; |
drh | 079a307 | 2014-03-19 14:10:55 +0000 | [diff] [blame] | 1221 | assert( n>0 && p1>0 && p2>0 ); |
drh | b21e7c7 | 2008-06-22 12:37:57 +0000 | [diff] [blame] | 1222 | assert( p1+n<=p2 || p2+n<=p1 ); |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1223 | |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 1224 | pIn1 = &aMem[p1]; |
| 1225 | pOut = &aMem[p2]; |
drh | e09f43f | 2013-11-21 04:18:31 +0000 | [diff] [blame] | 1226 | do{ |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 1227 | assert( pOut<=&aMem[(p->nMem+1 - p->nCursor)] ); |
| 1228 | assert( pIn1<=&aMem[(p->nMem+1 - p->nCursor)] ); |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 1229 | assert( memIsValid(pIn1) ); |
| 1230 | memAboutToChange(p, pOut); |
drh | 17bcb10 | 2014-09-18 21:25:33 +0000 | [diff] [blame] | 1231 | sqlite3VdbeMemMove(pOut, pIn1); |
drh | 52043d7 | 2011-08-03 16:40:15 +0000 | [diff] [blame] | 1232 | #ifdef SQLITE_DEBUG |
drh | bd6789e | 2015-04-28 14:00:02 +0000 | [diff] [blame] | 1233 | if( pOut->pScopyFrom>=&aMem[p1] && pOut->pScopyFrom<pOut ){ |
drh | 5fb7125 | 2015-04-28 12:44:55 +0000 | [diff] [blame] | 1234 | pOut->pScopyFrom += pOp->p2 - p1; |
drh | 52043d7 | 2011-08-03 16:40:15 +0000 | [diff] [blame] | 1235 | } |
| 1236 | #endif |
drh | bd6789e | 2015-04-28 14:00:02 +0000 | [diff] [blame] | 1237 | Deephemeralize(pOut); |
drh | b21e7c7 | 2008-06-22 12:37:57 +0000 | [diff] [blame] | 1238 | REGISTER_TRACE(p2++, pOut); |
| 1239 | pIn1++; |
| 1240 | pOut++; |
drh | 079a307 | 2014-03-19 14:10:55 +0000 | [diff] [blame] | 1241 | }while( --n ); |
drh | e1349cb | 2008-04-01 00:36:10 +0000 | [diff] [blame] | 1242 | break; |
| 1243 | } |
| 1244 | |
drh | e8e4af7 | 2012-09-21 00:04:28 +0000 | [diff] [blame] | 1245 | /* Opcode: Copy P1 P2 P3 * * |
drh | 4eded60 | 2013-12-20 15:59:20 +0000 | [diff] [blame] | 1246 | ** Synopsis: r[P2@P3+1]=r[P1@P3+1] |
drh | b1fdb2a | 2008-01-05 04:06:03 +0000 | [diff] [blame] | 1247 | ** |
drh | e8e4af7 | 2012-09-21 00:04:28 +0000 | [diff] [blame] | 1248 | ** Make a copy of registers P1..P1+P3 into registers P2..P2+P3. |
drh | b1fdb2a | 2008-01-05 04:06:03 +0000 | [diff] [blame] | 1249 | ** |
| 1250 | ** This instruction makes a deep copy of the value. A duplicate |
| 1251 | ** is made of any string or blob constant. See also OP_SCopy. |
| 1252 | */ |
drh | e8e4af7 | 2012-09-21 00:04:28 +0000 | [diff] [blame] | 1253 | case OP_Copy: { |
| 1254 | int n; |
| 1255 | |
| 1256 | n = pOp->p3; |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 1257 | pIn1 = &aMem[pOp->p1]; |
| 1258 | pOut = &aMem[pOp->p2]; |
drh | e1349cb | 2008-04-01 00:36:10 +0000 | [diff] [blame] | 1259 | assert( pOut!=pIn1 ); |
drh | e8e4af7 | 2012-09-21 00:04:28 +0000 | [diff] [blame] | 1260 | while( 1 ){ |
| 1261 | sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem); |
| 1262 | Deephemeralize(pOut); |
drh | 953f761 | 2012-12-07 22:18:54 +0000 | [diff] [blame] | 1263 | #ifdef SQLITE_DEBUG |
| 1264 | pOut->pScopyFrom = 0; |
| 1265 | #endif |
drh | e8e4af7 | 2012-09-21 00:04:28 +0000 | [diff] [blame] | 1266 | REGISTER_TRACE(pOp->p2+pOp->p3-n, pOut); |
| 1267 | if( (n--)==0 ) break; |
| 1268 | pOut++; |
| 1269 | pIn1++; |
| 1270 | } |
drh | e1349cb | 2008-04-01 00:36:10 +0000 | [diff] [blame] | 1271 | break; |
| 1272 | } |
| 1273 | |
drh | b1fdb2a | 2008-01-05 04:06:03 +0000 | [diff] [blame] | 1274 | /* Opcode: SCopy P1 P2 * * * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 1275 | ** Synopsis: r[P2]=r[P1] |
drh | b1fdb2a | 2008-01-05 04:06:03 +0000 | [diff] [blame] | 1276 | ** |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1277 | ** Make a shallow copy of register P1 into register P2. |
drh | b1fdb2a | 2008-01-05 04:06:03 +0000 | [diff] [blame] | 1278 | ** |
| 1279 | ** This instruction makes a shallow copy of the value. If the value |
| 1280 | ** is a string or blob, then the copy is only a pointer to the |
| 1281 | ** original and hence if the original changes so will the copy. |
| 1282 | ** Worse, if the original is deallocated, the copy becomes invalid. |
| 1283 | ** Thus the program must guarantee that the original will not change |
| 1284 | ** during the lifetime of the copy. Use OP_Copy to make a complete |
| 1285 | ** copy. |
| 1286 | */ |
drh | 26198bb | 2013-10-31 11:15:09 +0000 | [diff] [blame] | 1287 | case OP_SCopy: { /* out2 */ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 1288 | pIn1 = &aMem[pOp->p1]; |
| 1289 | pOut = &aMem[pOp->p2]; |
drh | 2d401ab | 2008-01-10 23:50:11 +0000 | [diff] [blame] | 1290 | assert( pOut!=pIn1 ); |
drh | e1349cb | 2008-04-01 00:36:10 +0000 | [diff] [blame] | 1291 | sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem); |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 1292 | #ifdef SQLITE_DEBUG |
| 1293 | if( pOut->pScopyFrom==0 ) pOut->pScopyFrom = pIn1; |
| 1294 | #endif |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1295 | break; |
| 1296 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1297 | |
drh | fed7ac6 | 2015-10-15 18:04:59 +0000 | [diff] [blame] | 1298 | /* Opcode: IntCopy P1 P2 * * * |
| 1299 | ** Synopsis: r[P2]=r[P1] |
| 1300 | ** |
| 1301 | ** Transfer the integer value held in register P1 into register P2. |
| 1302 | ** |
| 1303 | ** This is an optimized version of SCopy that works only for integer |
| 1304 | ** values. |
| 1305 | */ |
| 1306 | case OP_IntCopy: { /* out2 */ |
| 1307 | pIn1 = &aMem[pOp->p1]; |
| 1308 | assert( (pIn1->flags & MEM_Int)!=0 ); |
| 1309 | pOut = &aMem[pOp->p2]; |
| 1310 | sqlite3VdbeMemSetInt64(pOut, pIn1->u.i); |
| 1311 | break; |
| 1312 | } |
| 1313 | |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1314 | /* Opcode: ResultRow P1 P2 * * * |
drh | 72e26de | 2016-08-24 21:24:04 +0000 | [diff] [blame] | 1315 | ** Synopsis: output=r[P1@P2] |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 1316 | ** |
shane | 21e7feb | 2008-05-30 15:59:49 +0000 | [diff] [blame] | 1317 | ** The registers P1 through P1+P2-1 contain a single row of |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 1318 | ** results. This opcode causes the sqlite3_step() call to terminate |
| 1319 | ** with an SQLITE_ROW return code and it sets up the sqlite3_stmt |
drh | 4d87aae | 2014-02-20 19:42:00 +0000 | [diff] [blame] | 1320 | ** 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] | 1321 | ** the result row. |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 1322 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1323 | case OP_ResultRow: { |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 1324 | Mem *pMem; |
| 1325 | int i; |
| 1326 | assert( p->nResColumn==pOp->p2 ); |
drh | 0a07c10 | 2008-01-03 18:03:08 +0000 | [diff] [blame] | 1327 | assert( pOp->p1>0 ); |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 1328 | assert( pOp->p1+pOp->p2<=(p->nMem+1 - p->nCursor)+1 ); |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 1329 | |
drh | e6400b9 | 2013-11-13 23:48:46 +0000 | [diff] [blame] | 1330 | #ifndef SQLITE_OMIT_PROGRESS_CALLBACK |
| 1331 | /* Run the progress counter just before returning. |
| 1332 | */ |
| 1333 | if( db->xProgress!=0 |
drh | 2ab792e | 2017-05-30 18:34:07 +0000 | [diff] [blame] | 1334 | && nVmStep>=nProgressLimit |
drh | e6400b9 | 2013-11-13 23:48:46 +0000 | [diff] [blame] | 1335 | && db->xProgress(db->pProgressArg)!=0 |
| 1336 | ){ |
| 1337 | rc = SQLITE_INTERRUPT; |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 1338 | goto abort_due_to_error; |
drh | e6400b9 | 2013-11-13 23:48:46 +0000 | [diff] [blame] | 1339 | } |
| 1340 | #endif |
| 1341 | |
dan | 32b09f2 | 2009-09-23 17:29:59 +0000 | [diff] [blame] | 1342 | /* If this statement has violated immediate foreign key constraints, do |
| 1343 | ** not return the number of rows modified. And do not RELEASE the statement |
| 1344 | ** transaction. It needs to be rolled back. */ |
| 1345 | if( SQLITE_OK!=(rc = sqlite3VdbeCheckFk(p, 0)) ){ |
| 1346 | assert( db->flags&SQLITE_CountRows ); |
| 1347 | assert( p->usesStmtJournal ); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 1348 | goto abort_due_to_error; |
dan | 32b09f2 | 2009-09-23 17:29:59 +0000 | [diff] [blame] | 1349 | } |
| 1350 | |
danielk1977 | bd43455 | 2009-03-18 10:33:00 +0000 | [diff] [blame] | 1351 | /* If the SQLITE_CountRows flag is set in sqlite3.flags mask, then |
| 1352 | ** DML statements invoke this opcode to return the number of rows |
| 1353 | ** modified to the user. This is the only way that a VM that |
| 1354 | ** opens a statement transaction may invoke this opcode. |
| 1355 | ** |
| 1356 | ** In case this is such a statement, close any statement transaction |
| 1357 | ** opened by this VM before returning control to the user. This is to |
| 1358 | ** ensure that statement-transactions are always nested, not overlapping. |
| 1359 | ** If the open statement-transaction is not closed here, then the user |
| 1360 | ** may step another VM that opens its own statement transaction. This |
| 1361 | ** may lead to overlapping statement transactions. |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 1362 | ** |
| 1363 | ** The statement transaction is never a top-level transaction. Hence |
| 1364 | ** the RELEASE call below can never fail. |
danielk1977 | bd43455 | 2009-03-18 10:33:00 +0000 | [diff] [blame] | 1365 | */ |
| 1366 | assert( p->iStatement==0 || db->flags&SQLITE_CountRows ); |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 1367 | rc = sqlite3VdbeCloseStatement(p, SAVEPOINT_RELEASE); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 1368 | assert( rc==SQLITE_OK ); |
danielk1977 | bd43455 | 2009-03-18 10:33:00 +0000 | [diff] [blame] | 1369 | |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 1370 | /* Invalidate all ephemeral cursor row caches */ |
| 1371 | p->cacheCtr = (p->cacheCtr + 2)|1; |
| 1372 | |
| 1373 | /* Make sure the results of the current row are \000 terminated |
shane | 21e7feb | 2008-05-30 15:59:49 +0000 | [diff] [blame] | 1374 | ** and have an assigned type. The results are de-ephemeralized as |
drh | b8a45bb | 2011-12-31 21:51:55 +0000 | [diff] [blame] | 1375 | ** a side effect. |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 1376 | */ |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 1377 | pMem = p->pResultSet = &aMem[pOp->p1]; |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 1378 | for(i=0; i<pOp->p2; i++){ |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 1379 | assert( memIsValid(&pMem[i]) ); |
drh | ebc1671 | 2010-09-28 00:25:58 +0000 | [diff] [blame] | 1380 | Deephemeralize(&pMem[i]); |
drh | 746fd9c | 2010-09-28 06:00:47 +0000 | [diff] [blame] | 1381 | assert( (pMem[i].flags & MEM_Ephem)==0 |
| 1382 | || (pMem[i].flags & (MEM_Str|MEM_Blob))==0 ); |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 1383 | sqlite3VdbeMemNulTerminate(&pMem[i]); |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 1384 | REGISTER_TRACE(pOp->p1+i, &pMem[i]); |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 1385 | } |
drh | 2803969 | 2008-03-17 16:54:01 +0000 | [diff] [blame] | 1386 | if( db->mallocFailed ) goto no_mem; |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 1387 | |
drh | 3d2a529 | 2016-07-13 22:55:01 +0000 | [diff] [blame] | 1388 | if( db->mTrace & SQLITE_TRACE_ROW ){ |
| 1389 | db->xTrace(SQLITE_TRACE_ROW, db->pTraceArg, p, 0); |
| 1390 | } |
| 1391 | |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 1392 | /* Return SQLITE_ROW |
| 1393 | */ |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 1394 | p->pc = (int)(pOp - aOp) + 1; |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 1395 | rc = SQLITE_ROW; |
| 1396 | goto vdbe_return; |
| 1397 | } |
| 1398 | |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1399 | /* Opcode: Concat P1 P2 P3 * * |
drh | 313619f | 2013-10-31 20:34:06 +0000 | [diff] [blame] | 1400 | ** Synopsis: r[P3]=r[P2]+r[P1] |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1401 | ** |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1402 | ** Add the text in register P1 onto the end of the text in |
| 1403 | ** register P2 and store the result in register P3. |
| 1404 | ** 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] | 1405 | ** |
| 1406 | ** P3 = P2 || P1 |
| 1407 | ** |
| 1408 | ** It is illegal for P1 and P3 to be the same register. Sometimes, |
| 1409 | ** if P3 is the same register as P2, the implementation is able |
| 1410 | ** to avoid a memcpy(). |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1411 | */ |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1412 | case OP_Concat: { /* same as TK_CONCAT, in1, in2, out3 */ |
drh | 023ae03 | 2007-05-08 12:12:16 +0000 | [diff] [blame] | 1413 | i64 nByte; |
danielk1977 | 8a6b541 | 2004-05-24 07:04:25 +0000 | [diff] [blame] | 1414 | |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 1415 | pIn1 = &aMem[pOp->p1]; |
| 1416 | pIn2 = &aMem[pOp->p2]; |
| 1417 | pOut = &aMem[pOp->p3]; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1418 | assert( pIn1!=pOut ); |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1419 | if( (pIn1->flags | pIn2->flags) & MEM_Null ){ |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1420 | sqlite3VdbeMemSetNull(pOut); |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1421 | break; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1422 | } |
drh | a0c0652 | 2009-06-17 22:50:41 +0000 | [diff] [blame] | 1423 | if( ExpandBlob(pIn1) || ExpandBlob(pIn2) ) goto no_mem; |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1424 | Stringify(pIn1, encoding); |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1425 | Stringify(pIn2, encoding); |
| 1426 | nByte = pIn1->n + pIn2->n; |
drh | bb4957f | 2008-03-20 14:03:29 +0000 | [diff] [blame] | 1427 | if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){ |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1428 | goto too_big; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1429 | } |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 1430 | if( sqlite3VdbeMemGrow(pOut, (int)nByte+2, pOut==pIn2) ){ |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1431 | goto no_mem; |
| 1432 | } |
drh | c91b2fd | 2014-03-01 18:13:23 +0000 | [diff] [blame] | 1433 | MemSetTypeFlag(pOut, MEM_Str); |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1434 | if( pOut!=pIn2 ){ |
| 1435 | memcpy(pOut->z, pIn2->z, pIn2->n); |
| 1436 | } |
| 1437 | memcpy(&pOut->z[pIn2->n], pIn1->z, pIn1->n); |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 1438 | pOut->z[nByte]=0; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1439 | pOut->z[nByte+1] = 0; |
| 1440 | pOut->flags |= MEM_Term; |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 1441 | pOut->n = (int)nByte; |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1442 | pOut->enc = encoding; |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 1443 | UPDATE_MAX_BLOBSIZE(pOut); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1444 | break; |
| 1445 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1446 | |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 1447 | /* Opcode: Add P1 P2 P3 * * |
drh | 72e26de | 2016-08-24 21:24:04 +0000 | [diff] [blame] | 1448 | ** Synopsis: r[P3]=r[P1]+r[P2] |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1449 | ** |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1450 | ** Add the value in register P1 to the value in register P2 |
shane | 21e7feb | 2008-05-30 15:59:49 +0000 | [diff] [blame] | 1451 | ** and store the result in register P3. |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1452 | ** If either input is NULL, the result is NULL. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1453 | */ |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 1454 | /* Opcode: Multiply P1 P2 P3 * * |
drh | 72e26de | 2016-08-24 21:24:04 +0000 | [diff] [blame] | 1455 | ** Synopsis: r[P3]=r[P1]*r[P2] |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1456 | ** |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 1457 | ** |
shane | 21e7feb | 2008-05-30 15:59:49 +0000 | [diff] [blame] | 1458 | ** Multiply the value in register P1 by the value in register P2 |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1459 | ** and store the result in register P3. |
| 1460 | ** If either input is NULL, the result is NULL. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1461 | */ |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 1462 | /* Opcode: Subtract P1 P2 P3 * * |
drh | 72e26de | 2016-08-24 21:24:04 +0000 | [diff] [blame] | 1463 | ** Synopsis: r[P3]=r[P2]-r[P1] |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1464 | ** |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1465 | ** Subtract the value in register P1 from the value in register P2 |
| 1466 | ** and store the result in register P3. |
| 1467 | ** If either input is NULL, the result is NULL. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1468 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1469 | /* Opcode: Divide P1 P2 P3 * * |
drh | 72e26de | 2016-08-24 21:24:04 +0000 | [diff] [blame] | 1470 | ** Synopsis: r[P3]=r[P2]/r[P1] |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1471 | ** |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1472 | ** Divide the value in register P1 by the value in register P2 |
dan | e275dc3 | 2009-08-18 16:24:58 +0000 | [diff] [blame] | 1473 | ** and store the result in register P3 (P3=P2/P1). If the value in |
| 1474 | ** register P1 is zero, then the result is NULL. If either input is |
| 1475 | ** NULL, the result is NULL. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1476 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1477 | /* Opcode: Remainder P1 P2 P3 * * |
drh | 72e26de | 2016-08-24 21:24:04 +0000 | [diff] [blame] | 1478 | ** Synopsis: r[P3]=r[P2]%r[P1] |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1479 | ** |
drh | 40864a1 | 2013-11-15 18:58:37 +0000 | [diff] [blame] | 1480 | ** Compute the remainder after integer register P2 is divided by |
| 1481 | ** register P1 and store the result in register P3. |
| 1482 | ** If the value in register P1 is zero the result is NULL. |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1483 | ** If either operand is NULL, the result is NULL. |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1484 | */ |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1485 | case OP_Add: /* same as TK_PLUS, in1, in2, out3 */ |
| 1486 | case OP_Subtract: /* same as TK_MINUS, in1, in2, out3 */ |
| 1487 | case OP_Multiply: /* same as TK_STAR, in1, in2, out3 */ |
| 1488 | case OP_Divide: /* same as TK_SLASH, in1, in2, out3 */ |
| 1489 | case OP_Remainder: { /* same as TK_REM, in1, in2, out3 */ |
drh | be707b3 | 2012-12-10 22:19:14 +0000 | [diff] [blame] | 1490 | char bIntint; /* Started out as two integer operands */ |
drh | 3d1d90a | 2014-03-24 15:00:15 +0000 | [diff] [blame] | 1491 | u16 flags; /* Combined MEM_* flags from both inputs */ |
| 1492 | u16 type1; /* Numeric type of left operand */ |
| 1493 | u16 type2; /* Numeric type of right operand */ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1494 | i64 iA; /* Integer value of left operand */ |
| 1495 | i64 iB; /* Integer value of right operand */ |
| 1496 | double rA; /* Real value of left operand */ |
| 1497 | double rB; /* Real value of right operand */ |
| 1498 | |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 1499 | pIn1 = &aMem[pOp->p1]; |
drh | 3d1d90a | 2014-03-24 15:00:15 +0000 | [diff] [blame] | 1500 | type1 = numericType(pIn1); |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 1501 | pIn2 = &aMem[pOp->p2]; |
drh | 3d1d90a | 2014-03-24 15:00:15 +0000 | [diff] [blame] | 1502 | type2 = numericType(pIn2); |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 1503 | pOut = &aMem[pOp->p3]; |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1504 | flags = pIn1->flags | pIn2->flags; |
drh | 3d1d90a | 2014-03-24 15:00:15 +0000 | [diff] [blame] | 1505 | if( (type1 & type2 & MEM_Int)!=0 ){ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1506 | iA = pIn1->u.i; |
| 1507 | iB = pIn2->u.i; |
drh | be707b3 | 2012-12-10 22:19:14 +0000 | [diff] [blame] | 1508 | bIntint = 1; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1509 | switch( pOp->opcode ){ |
drh | 158b9cb | 2011-03-05 20:59:46 +0000 | [diff] [blame] | 1510 | case OP_Add: if( sqlite3AddInt64(&iB,iA) ) goto fp_math; break; |
| 1511 | case OP_Subtract: if( sqlite3SubInt64(&iB,iA) ) goto fp_math; break; |
| 1512 | case OP_Multiply: if( sqlite3MulInt64(&iB,iA) ) goto fp_math; break; |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1513 | case OP_Divide: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1514 | if( iA==0 ) goto arithmetic_result_is_null; |
drh | 158b9cb | 2011-03-05 20:59:46 +0000 | [diff] [blame] | 1515 | if( iA==-1 && iB==SMALLEST_INT64 ) goto fp_math; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1516 | iB /= iA; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1517 | break; |
| 1518 | } |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1519 | default: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1520 | if( iA==0 ) goto arithmetic_result_is_null; |
| 1521 | if( iA==-1 ) iA = 1; |
| 1522 | iB %= iA; |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1523 | break; |
| 1524 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1525 | } |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1526 | pOut->u.i = iB; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1527 | MemSetTypeFlag(pOut, MEM_Int); |
drh | cfcca02 | 2017-04-17 23:23:17 +0000 | [diff] [blame] | 1528 | }else if( (flags & MEM_Null)!=0 ){ |
| 1529 | goto arithmetic_result_is_null; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1530 | }else{ |
drh | be707b3 | 2012-12-10 22:19:14 +0000 | [diff] [blame] | 1531 | bIntint = 0; |
drh | 158b9cb | 2011-03-05 20:59:46 +0000 | [diff] [blame] | 1532 | fp_math: |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1533 | rA = sqlite3VdbeRealValue(pIn1); |
| 1534 | rB = sqlite3VdbeRealValue(pIn2); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1535 | switch( pOp->opcode ){ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1536 | case OP_Add: rB += rA; break; |
| 1537 | case OP_Subtract: rB -= rA; break; |
| 1538 | case OP_Multiply: rB *= rA; break; |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1539 | case OP_Divide: { |
shane | fbd60f8 | 2009-02-04 03:59:25 +0000 | [diff] [blame] | 1540 | /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1541 | if( rA==(double)0 ) goto arithmetic_result_is_null; |
| 1542 | rB /= rA; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1543 | break; |
| 1544 | } |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1545 | default: { |
shane | 75ac1de | 2009-06-09 18:58:52 +0000 | [diff] [blame] | 1546 | iA = (i64)rA; |
| 1547 | iB = (i64)rB; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1548 | if( iA==0 ) goto arithmetic_result_is_null; |
| 1549 | if( iA==-1 ) iA = 1; |
| 1550 | rB = (double)(iB % iA); |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1551 | break; |
| 1552 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1553 | } |
drh | c5a7b51 | 2010-01-13 16:25:42 +0000 | [diff] [blame] | 1554 | #ifdef SQLITE_OMIT_FLOATING_POINT |
| 1555 | pOut->u.i = rB; |
| 1556 | MemSetTypeFlag(pOut, MEM_Int); |
| 1557 | #else |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1558 | if( sqlite3IsNaN(rB) ){ |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 1559 | goto arithmetic_result_is_null; |
drh | 53c1402 | 2007-05-10 17:23:11 +0000 | [diff] [blame] | 1560 | } |
drh | 74eaba4 | 2014-09-18 17:52:15 +0000 | [diff] [blame] | 1561 | pOut->u.r = rB; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1562 | MemSetTypeFlag(pOut, MEM_Real); |
drh | 3d1d90a | 2014-03-24 15:00:15 +0000 | [diff] [blame] | 1563 | if( ((type1|type2)&MEM_Real)==0 && !bIntint ){ |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1564 | sqlite3VdbeIntegerAffinity(pOut); |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1565 | } |
drh | c5a7b51 | 2010-01-13 16:25:42 +0000 | [diff] [blame] | 1566 | #endif |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1567 | } |
| 1568 | break; |
| 1569 | |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 1570 | arithmetic_result_is_null: |
| 1571 | sqlite3VdbeMemSetNull(pOut); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1572 | break; |
| 1573 | } |
| 1574 | |
drh | 7a95789 | 2012-02-02 17:35:43 +0000 | [diff] [blame] | 1575 | /* Opcode: CollSeq P1 * * P4 |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 1576 | ** |
drh | bb6783b | 2017-04-29 18:02:49 +0000 | [diff] [blame] | 1577 | ** 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] | 1578 | ** or aggregate calls sqlite3GetFuncCollSeq(), this collation sequence will |
| 1579 | ** be returned. This is used by the built-in min(), max() and nullif() |
drh | e6f85e7 | 2004-12-25 01:03:13 +0000 | [diff] [blame] | 1580 | ** functions. |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 1581 | ** |
drh | 7a95789 | 2012-02-02 17:35:43 +0000 | [diff] [blame] | 1582 | ** If P1 is not zero, then it is a register that a subsequent min() or |
| 1583 | ** max() aggregate will set to 1 if the current row is not the minimum or |
| 1584 | ** maximum. The P1 register is initialized to 0 by this instruction. |
| 1585 | ** |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 1586 | ** The interface used by the implementation of the aforementioned functions |
| 1587 | ** to retrieve the collation sequence set by this opcode is not available |
drh | 0a0d056 | 2015-03-12 05:08:34 +0000 | [diff] [blame] | 1588 | ** publicly. Only built-in functions have access to this feature. |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 1589 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1590 | case OP_CollSeq: { |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 1591 | assert( pOp->p4type==P4_COLLSEQ ); |
drh | 7a95789 | 2012-02-02 17:35:43 +0000 | [diff] [blame] | 1592 | if( pOp->p1 ){ |
| 1593 | sqlite3VdbeMemSetInt64(&aMem[pOp->p1], 0); |
| 1594 | } |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 1595 | break; |
| 1596 | } |
| 1597 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1598 | /* Opcode: BitAnd P1 P2 P3 * * |
drh | 72e26de | 2016-08-24 21:24:04 +0000 | [diff] [blame] | 1599 | ** Synopsis: r[P3]=r[P1]&r[P2] |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1600 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1601 | ** Take the bit-wise AND of the values in register P1 and P2 and |
| 1602 | ** store the result in register P3. |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1603 | ** If either input is NULL, the result is NULL. |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1604 | */ |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1605 | /* Opcode: BitOr P1 P2 P3 * * |
drh | 72e26de | 2016-08-24 21:24:04 +0000 | [diff] [blame] | 1606 | ** Synopsis: r[P3]=r[P1]|r[P2] |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1607 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1608 | ** Take the bit-wise OR of the values in register P1 and P2 and |
| 1609 | ** store the result in register P3. |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1610 | ** If either input is NULL, the result is NULL. |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1611 | */ |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1612 | /* Opcode: ShiftLeft P1 P2 P3 * * |
drh | 72e26de | 2016-08-24 21:24:04 +0000 | [diff] [blame] | 1613 | ** Synopsis: r[P3]=r[P2]<<r[P1] |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1614 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1615 | ** Shift the integer value in register P2 to the left by the |
drh | 710c484 | 2010-08-30 01:17:20 +0000 | [diff] [blame] | 1616 | ** number of bits specified by the integer in register P1. |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1617 | ** Store the result in register P3. |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1618 | ** If either input is NULL, the result is NULL. |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1619 | */ |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1620 | /* Opcode: ShiftRight P1 P2 P3 * * |
drh | 72e26de | 2016-08-24 21:24:04 +0000 | [diff] [blame] | 1621 | ** Synopsis: r[P3]=r[P2]>>r[P1] |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1622 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1623 | ** Shift the integer value in register P2 to the right by the |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1624 | ** number of bits specified by the integer in register P1. |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1625 | ** Store the result in register P3. |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1626 | ** If either input is NULL, the result is NULL. |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1627 | */ |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1628 | case OP_BitAnd: /* same as TK_BITAND, in1, in2, out3 */ |
| 1629 | case OP_BitOr: /* same as TK_BITOR, in1, in2, out3 */ |
| 1630 | case OP_ShiftLeft: /* same as TK_LSHIFT, in1, in2, out3 */ |
| 1631 | case OP_ShiftRight: { /* same as TK_RSHIFT, in1, in2, out3 */ |
drh | 158b9cb | 2011-03-05 20:59:46 +0000 | [diff] [blame] | 1632 | i64 iA; |
| 1633 | u64 uA; |
| 1634 | i64 iB; |
| 1635 | u8 op; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1636 | |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 1637 | pIn1 = &aMem[pOp->p1]; |
| 1638 | pIn2 = &aMem[pOp->p2]; |
| 1639 | pOut = &aMem[pOp->p3]; |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1640 | if( (pIn1->flags | pIn2->flags) & MEM_Null ){ |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 1641 | sqlite3VdbeMemSetNull(pOut); |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1642 | break; |
| 1643 | } |
drh | 158b9cb | 2011-03-05 20:59:46 +0000 | [diff] [blame] | 1644 | iA = sqlite3VdbeIntValue(pIn2); |
| 1645 | iB = sqlite3VdbeIntValue(pIn1); |
| 1646 | op = pOp->opcode; |
| 1647 | if( op==OP_BitAnd ){ |
| 1648 | iA &= iB; |
| 1649 | }else if( op==OP_BitOr ){ |
| 1650 | iA |= iB; |
| 1651 | }else if( iB!=0 ){ |
| 1652 | assert( op==OP_ShiftRight || op==OP_ShiftLeft ); |
| 1653 | |
| 1654 | /* If shifting by a negative amount, shift in the other direction */ |
| 1655 | if( iB<0 ){ |
| 1656 | assert( OP_ShiftRight==OP_ShiftLeft+1 ); |
| 1657 | op = 2*OP_ShiftLeft + 1 - op; |
| 1658 | iB = iB>(-64) ? -iB : 64; |
| 1659 | } |
| 1660 | |
| 1661 | if( iB>=64 ){ |
| 1662 | iA = (iA>=0 || op==OP_ShiftLeft) ? 0 : -1; |
| 1663 | }else{ |
| 1664 | memcpy(&uA, &iA, sizeof(uA)); |
| 1665 | if( op==OP_ShiftLeft ){ |
| 1666 | uA <<= iB; |
| 1667 | }else{ |
| 1668 | uA >>= iB; |
| 1669 | /* Sign-extend on a right shift of a negative number */ |
| 1670 | if( iA<0 ) uA |= ((((u64)0xffffffff)<<32)|0xffffffff) << (64-iB); |
| 1671 | } |
| 1672 | memcpy(&iA, &uA, sizeof(iA)); |
| 1673 | } |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1674 | } |
drh | 158b9cb | 2011-03-05 20:59:46 +0000 | [diff] [blame] | 1675 | pOut->u.i = iA; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1676 | MemSetTypeFlag(pOut, MEM_Int); |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1677 | break; |
| 1678 | } |
| 1679 | |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1680 | /* Opcode: AddImm P1 P2 * * * |
drh | 72e26de | 2016-08-24 21:24:04 +0000 | [diff] [blame] | 1681 | ** Synopsis: r[P1]=r[P1]+P2 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1682 | ** |
danielk1977 | 0cdc022 | 2008-06-26 18:04:03 +0000 | [diff] [blame] | 1683 | ** Add the constant P2 to the value in register P1. |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1684 | ** The result is always an integer. |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 1685 | ** |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1686 | ** To force any register to be an integer, just add 0. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1687 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1688 | case OP_AddImm: { /* in1 */ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 1689 | pIn1 = &aMem[pOp->p1]; |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 1690 | memAboutToChange(p, pIn1); |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1691 | sqlite3VdbeMemIntegerify(pIn1); |
| 1692 | pIn1->u.i += pOp->p2; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1693 | break; |
| 1694 | } |
| 1695 | |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1696 | /* Opcode: MustBeInt P1 P2 * * * |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 1697 | ** |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1698 | ** Force the value in register P1 to be an integer. If the value |
| 1699 | ** in P1 is not an integer and cannot be converted into an integer |
danielk1977 | 9a96b66 | 2007-11-29 17:05:18 +0000 | [diff] [blame] | 1700 | ** without data loss, then jump immediately to P2, or if P2==0 |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 1701 | ** raise an SQLITE_MISMATCH exception. |
| 1702 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1703 | case OP_MustBeInt: { /* jump, in1 */ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 1704 | pIn1 = &aMem[pOp->p1]; |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 1705 | if( (pIn1->flags & MEM_Int)==0 ){ |
drh | 83b301b | 2013-11-20 00:59:02 +0000 | [diff] [blame] | 1706 | applyAffinity(pIn1, SQLITE_AFF_NUMERIC, encoding); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 1707 | VdbeBranchTaken((pIn1->flags&MEM_Int)==0, 2); |
drh | 83b301b | 2013-11-20 00:59:02 +0000 | [diff] [blame] | 1708 | if( (pIn1->flags & MEM_Int)==0 ){ |
| 1709 | if( pOp->p2==0 ){ |
| 1710 | rc = SQLITE_MISMATCH; |
| 1711 | goto abort_due_to_error; |
| 1712 | }else{ |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 1713 | goto jump_to_p2; |
drh | 83b301b | 2013-11-20 00:59:02 +0000 | [diff] [blame] | 1714 | } |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 1715 | } |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 1716 | } |
drh | 83b301b | 2013-11-20 00:59:02 +0000 | [diff] [blame] | 1717 | MemSetTypeFlag(pIn1, MEM_Int); |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 1718 | break; |
| 1719 | } |
| 1720 | |
drh | 13573c7 | 2010-01-12 17:04:07 +0000 | [diff] [blame] | 1721 | #ifndef SQLITE_OMIT_FLOATING_POINT |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1722 | /* Opcode: RealAffinity P1 * * * * |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 1723 | ** |
drh | 2133d82 | 2008-01-03 18:44:59 +0000 | [diff] [blame] | 1724 | ** If register P1 holds an integer convert it to a real value. |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 1725 | ** |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1726 | ** This opcode is used when extracting information from a column that |
| 1727 | ** has REAL affinity. Such column values may still be stored as |
| 1728 | ** integers, for space efficiency, but after extraction we want them |
| 1729 | ** to have only a real value. |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 1730 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1731 | case OP_RealAffinity: { /* in1 */ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 1732 | pIn1 = &aMem[pOp->p1]; |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1733 | if( pIn1->flags & MEM_Int ){ |
| 1734 | sqlite3VdbeMemRealify(pIn1); |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1735 | } |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 1736 | break; |
| 1737 | } |
drh | 13573c7 | 2010-01-12 17:04:07 +0000 | [diff] [blame] | 1738 | #endif |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 1739 | |
drh | 8df447f | 2005-11-01 15:48:24 +0000 | [diff] [blame] | 1740 | #ifndef SQLITE_OMIT_CAST |
drh | 4169e43 | 2014-08-25 20:11:52 +0000 | [diff] [blame] | 1741 | /* Opcode: Cast P1 P2 * * * |
mistachkin | a1dc42a | 2014-08-27 17:53:40 +0000 | [diff] [blame] | 1742 | ** Synopsis: affinity(r[P1]) |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 1743 | ** |
drh | 4169e43 | 2014-08-25 20:11:52 +0000 | [diff] [blame] | 1744 | ** Force the value in register P1 to be the type defined by P2. |
| 1745 | ** |
| 1746 | ** <ul> |
drh | bb6783b | 2017-04-29 18:02:49 +0000 | [diff] [blame] | 1747 | ** <li> P2=='A' → BLOB |
| 1748 | ** <li> P2=='B' → TEXT |
| 1749 | ** <li> P2=='C' → NUMERIC |
| 1750 | ** <li> P2=='D' → INTEGER |
| 1751 | ** <li> P2=='E' → REAL |
drh | 4169e43 | 2014-08-25 20:11:52 +0000 | [diff] [blame] | 1752 | ** </ul> |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 1753 | ** |
| 1754 | ** A NULL value is not changed by this routine. It remains NULL. |
| 1755 | */ |
drh | 4169e43 | 2014-08-25 20:11:52 +0000 | [diff] [blame] | 1756 | case OP_Cast: { /* in1 */ |
drh | 05883a3 | 2015-06-02 15:32:08 +0000 | [diff] [blame] | 1757 | assert( pOp->p2>=SQLITE_AFF_BLOB && pOp->p2<=SQLITE_AFF_REAL ); |
drh | 05bbb2e | 2014-08-25 22:37:19 +0000 | [diff] [blame] | 1758 | testcase( pOp->p2==SQLITE_AFF_TEXT ); |
drh | 05883a3 | 2015-06-02 15:32:08 +0000 | [diff] [blame] | 1759 | testcase( pOp->p2==SQLITE_AFF_BLOB ); |
drh | 05bbb2e | 2014-08-25 22:37:19 +0000 | [diff] [blame] | 1760 | testcase( pOp->p2==SQLITE_AFF_NUMERIC ); |
| 1761 | testcase( pOp->p2==SQLITE_AFF_INTEGER ); |
| 1762 | testcase( pOp->p2==SQLITE_AFF_REAL ); |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 1763 | pIn1 = &aMem[pOp->p1]; |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 1764 | memAboutToChange(p, pIn1); |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1765 | rc = ExpandBlob(pIn1); |
drh | 4169e43 | 2014-08-25 20:11:52 +0000 | [diff] [blame] | 1766 | sqlite3VdbeMemCast(pIn1, pOp->p2, encoding); |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 1767 | UPDATE_MAX_BLOBSIZE(pIn1); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 1768 | if( rc ) goto abort_due_to_error; |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 1769 | break; |
| 1770 | } |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1771 | #endif /* SQLITE_OMIT_CAST */ |
| 1772 | |
drh | 79752b6 | 2016-08-13 10:02:17 +0000 | [diff] [blame] | 1773 | /* Opcode: Eq P1 P2 P3 P4 P5 |
drh | 88e665f | 2016-08-27 01:41:53 +0000 | [diff] [blame] | 1774 | ** Synopsis: IF r[P3]==r[P1] |
drh | 79752b6 | 2016-08-13 10:02:17 +0000 | [diff] [blame] | 1775 | ** |
| 1776 | ** Compare the values in register P1 and P3. If reg(P3)==reg(P1) then |
| 1777 | ** jump to address P2. Or if the SQLITE_STOREP2 flag is set in P5, then |
| 1778 | ** store the result of comparison in register P2. |
| 1779 | ** |
| 1780 | ** The SQLITE_AFF_MASK portion of P5 must be an affinity character - |
| 1781 | ** SQLITE_AFF_TEXT, SQLITE_AFF_INTEGER, and so forth. An attempt is made |
| 1782 | ** to coerce both inputs according to this affinity before the |
| 1783 | ** comparison is made. If the SQLITE_AFF_MASK is 0x00, then numeric |
| 1784 | ** affinity is used. Note that the affinity conversions are stored |
| 1785 | ** back into the input registers P1 and P3. So this opcode can cause |
| 1786 | ** persistent changes to registers P1 and P3. |
| 1787 | ** |
| 1788 | ** Once any conversions have taken place, and neither value is NULL, |
| 1789 | ** the values are compared. If both values are blobs then memcmp() is |
| 1790 | ** used to determine the results of the comparison. If both values |
| 1791 | ** are text, then the appropriate collating function specified in |
| 1792 | ** P4 is used to do the comparison. If P4 is not specified then |
| 1793 | ** memcmp() is used to compare text string. If both values are |
| 1794 | ** numeric, then a numeric comparison is used. If the two values |
| 1795 | ** are of different types, then numbers are considered less than |
| 1796 | ** strings and strings are considered less than blobs. |
| 1797 | ** |
| 1798 | ** If SQLITE_NULLEQ is set in P5 then the result of comparison is always either |
| 1799 | ** true or false and is never NULL. If both operands are NULL then the result |
| 1800 | ** of comparison is true. If either operand is NULL then the result is false. |
| 1801 | ** If neither operand is NULL the result is the same as it would be if |
| 1802 | ** the SQLITE_NULLEQ flag were omitted from P5. |
| 1803 | ** |
| 1804 | ** If both SQLITE_STOREP2 and SQLITE_KEEPNULL flags are set then the |
drh | 3fffbf9 | 2016-09-05 15:02:41 +0000 | [diff] [blame] | 1805 | ** content of r[P2] is only changed if the new value is NULL or 0 (false). |
| 1806 | ** 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] | 1807 | */ |
| 1808 | /* Opcode: Ne P1 P2 P3 P4 P5 |
drh | 88e665f | 2016-08-27 01:41:53 +0000 | [diff] [blame] | 1809 | ** Synopsis: IF r[P3]!=r[P1] |
drh | 79752b6 | 2016-08-13 10:02:17 +0000 | [diff] [blame] | 1810 | ** |
| 1811 | ** This works just like the Eq opcode except that the jump is taken if |
| 1812 | ** the operands in registers P1 and P3 are not equal. See the Eq opcode for |
| 1813 | ** additional information. |
| 1814 | ** |
| 1815 | ** If both SQLITE_STOREP2 and SQLITE_KEEPNULL flags are set then the |
drh | 3fffbf9 | 2016-09-05 15:02:41 +0000 | [diff] [blame] | 1816 | ** content of r[P2] is only changed if the new value is NULL or 1 (true). |
| 1817 | ** 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] | 1818 | */ |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 1819 | /* Opcode: Lt P1 P2 P3 P4 P5 |
drh | 88e665f | 2016-08-27 01:41:53 +0000 | [diff] [blame] | 1820 | ** Synopsis: IF r[P3]<r[P1] |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1821 | ** |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 1822 | ** 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] | 1823 | ** jump to address P2. Or if the SQLITE_STOREP2 flag is set in P5 store |
| 1824 | ** the result of comparison (0 or 1 or NULL) into register P2. |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1825 | ** |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 1826 | ** 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] | 1827 | ** reg(P3) is NULL then the take the jump. If the SQLITE_JUMPIFNULL |
drh | 710c484 | 2010-08-30 01:17:20 +0000 | [diff] [blame] | 1828 | ** bit is clear then fall through if either operand is NULL. |
drh | 4f68623 | 2005-09-20 13:55:18 +0000 | [diff] [blame] | 1829 | ** |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 1830 | ** The SQLITE_AFF_MASK portion of P5 must be an affinity character - |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1831 | ** SQLITE_AFF_TEXT, SQLITE_AFF_INTEGER, and so forth. An attempt is made |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1832 | ** to coerce both inputs according to this affinity before the |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 1833 | ** comparison is made. If the SQLITE_AFF_MASK is 0x00, then numeric |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1834 | ** affinity is used. Note that the affinity conversions are stored |
| 1835 | ** back into the input registers P1 and P3. So this opcode can cause |
| 1836 | ** persistent changes to registers P1 and P3. |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 1837 | ** |
| 1838 | ** Once any conversions have taken place, and neither value is NULL, |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 1839 | ** the values are compared. If both values are blobs then memcmp() is |
| 1840 | ** used to determine the results of the comparison. If both values |
| 1841 | ** are text, then the appropriate collating function specified in |
| 1842 | ** P4 is used to do the comparison. If P4 is not specified then |
| 1843 | ** memcmp() is used to compare text string. If both values are |
| 1844 | ** numeric, then a numeric comparison is used. If the two values |
| 1845 | ** are of different types, then numbers are considered less than |
| 1846 | ** strings and strings are considered less than blobs. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1847 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1848 | /* Opcode: Le P1 P2 P3 P4 P5 |
drh | 88e665f | 2016-08-27 01:41:53 +0000 | [diff] [blame] | 1849 | ** Synopsis: IF r[P3]<=r[P1] |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1850 | ** |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 1851 | ** This works just like the Lt opcode except that the jump is taken if |
| 1852 | ** the content of register P3 is less than or equal to the content of |
| 1853 | ** register P1. See the Lt opcode for additional information. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1854 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1855 | /* Opcode: Gt P1 P2 P3 P4 P5 |
drh | 88e665f | 2016-08-27 01:41:53 +0000 | [diff] [blame] | 1856 | ** Synopsis: IF r[P3]>r[P1] |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1857 | ** |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 1858 | ** This works just like the Lt opcode except that the jump is taken if |
| 1859 | ** the content of register P3 is greater than the content of |
| 1860 | ** register P1. See the Lt opcode for additional information. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1861 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1862 | /* Opcode: Ge P1 P2 P3 P4 P5 |
drh | 88e665f | 2016-08-27 01:41:53 +0000 | [diff] [blame] | 1863 | ** Synopsis: IF r[P3]>=r[P1] |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1864 | ** |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 1865 | ** This works just like the Lt opcode except that the jump is taken if |
| 1866 | ** the content of register P3 is greater than or equal to the content of |
| 1867 | ** register P1. See the Lt opcode for additional information. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1868 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1869 | case OP_Eq: /* same as TK_EQ, jump, in1, in3 */ |
| 1870 | case OP_Ne: /* same as TK_NE, jump, in1, in3 */ |
| 1871 | case OP_Lt: /* same as TK_LT, jump, in1, in3 */ |
| 1872 | case OP_Le: /* same as TK_LE, jump, in1, in3 */ |
| 1873 | case OP_Gt: /* same as TK_GT, jump, in1, in3 */ |
| 1874 | case OP_Ge: { /* same as TK_GE, jump, in1, in3 */ |
drh | 4910a76 | 2016-09-03 01:46:15 +0000 | [diff] [blame] | 1875 | int res, res2; /* Result of the comparison of pIn1 against pIn3 */ |
drh | 6a2fe09 | 2009-09-23 02:29:36 +0000 | [diff] [blame] | 1876 | char affinity; /* Affinity to use for comparison */ |
dan | b7dca7d | 2010-03-05 16:32:12 +0000 | [diff] [blame] | 1877 | u16 flags1; /* Copy of initial value of pIn1->flags */ |
| 1878 | u16 flags3; /* Copy of initial value of pIn3->flags */ |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 1879 | |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 1880 | pIn1 = &aMem[pOp->p1]; |
| 1881 | pIn3 = &aMem[pOp->p3]; |
dan | b7dca7d | 2010-03-05 16:32:12 +0000 | [diff] [blame] | 1882 | flags1 = pIn1->flags; |
| 1883 | flags3 = pIn3->flags; |
drh | c3f1d5f | 2011-05-30 23:42:16 +0000 | [diff] [blame] | 1884 | if( (flags1 | flags3)&MEM_Null ){ |
drh | 6a2fe09 | 2009-09-23 02:29:36 +0000 | [diff] [blame] | 1885 | /* One or both operands are NULL */ |
| 1886 | if( pOp->p5 & SQLITE_NULLEQ ){ |
| 1887 | /* If SQLITE_NULLEQ is set (which will only happen if the operator is |
| 1888 | ** OP_Eq or OP_Ne) then take the jump or not depending on whether |
| 1889 | ** or not both operands are null. |
| 1890 | */ |
| 1891 | assert( pOp->opcode==OP_Eq || pOp->opcode==OP_Ne ); |
drh | 053a128 | 2012-09-19 21:15:46 +0000 | [diff] [blame] | 1892 | assert( (flags1 & MEM_Cleared)==0 ); |
drh | 3d77dee | 2014-02-19 14:20:49 +0000 | [diff] [blame] | 1893 | assert( (pOp->p5 & SQLITE_JUMPIFNULL)==0 ); |
drh | c3191d2 | 2016-10-18 16:36:15 +0000 | [diff] [blame] | 1894 | if( (flags1&flags3&MEM_Null)!=0 |
drh | 053a128 | 2012-09-19 21:15:46 +0000 | [diff] [blame] | 1895 | && (flags3&MEM_Cleared)==0 |
| 1896 | ){ |
drh | 4910a76 | 2016-09-03 01:46:15 +0000 | [diff] [blame] | 1897 | res = 0; /* Operands are equal */ |
drh | 053a128 | 2012-09-19 21:15:46 +0000 | [diff] [blame] | 1898 | }else{ |
drh | 4910a76 | 2016-09-03 01:46:15 +0000 | [diff] [blame] | 1899 | res = 1; /* Operands are not equal */ |
drh | 053a128 | 2012-09-19 21:15:46 +0000 | [diff] [blame] | 1900 | } |
drh | 6a2fe09 | 2009-09-23 02:29:36 +0000 | [diff] [blame] | 1901 | }else{ |
| 1902 | /* SQLITE_NULLEQ is clear and at least one operand is NULL, |
| 1903 | ** then the result is always NULL. |
| 1904 | ** The jump is taken if the SQLITE_JUMPIFNULL bit is set. |
| 1905 | */ |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 1906 | if( pOp->p5 & SQLITE_STOREP2 ){ |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 1907 | pOut = &aMem[pOp->p2]; |
drh | 4910a76 | 2016-09-03 01:46:15 +0000 | [diff] [blame] | 1908 | iCompare = 1; /* Operands are not equal */ |
dan | b1d6b53 | 2015-12-14 19:42:19 +0000 | [diff] [blame] | 1909 | memAboutToChange(p, pOut); |
drh | 6a2fe09 | 2009-09-23 02:29:36 +0000 | [diff] [blame] | 1910 | MemSetTypeFlag(pOut, MEM_Null); |
| 1911 | REGISTER_TRACE(pOp->p2, pOut); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 1912 | }else{ |
drh | f4345e4 | 2014-02-18 11:31:59 +0000 | [diff] [blame] | 1913 | VdbeBranchTaken(2,3); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 1914 | if( pOp->p5 & SQLITE_JUMPIFNULL ){ |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 1915 | goto jump_to_p2; |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 1916 | } |
drh | 6a2fe09 | 2009-09-23 02:29:36 +0000 | [diff] [blame] | 1917 | } |
| 1918 | break; |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 1919 | } |
drh | 6a2fe09 | 2009-09-23 02:29:36 +0000 | [diff] [blame] | 1920 | }else{ |
| 1921 | /* Neither operand is NULL. Do a comparison. */ |
| 1922 | affinity = pOp->p5 & SQLITE_AFF_MASK; |
drh | 24a0962 | 2014-09-18 16:28:59 +0000 | [diff] [blame] | 1923 | if( affinity>=SQLITE_AFF_NUMERIC ){ |
drh | 5fd0c12 | 2016-04-04 13:46:24 +0000 | [diff] [blame] | 1924 | if( (flags1 | flags3)&MEM_Str ){ |
| 1925 | if( (flags1 & (MEM_Int|MEM_Real|MEM_Str))==MEM_Str ){ |
| 1926 | applyNumericAffinity(pIn1,0); |
drh | 64caee4 | 2016-09-09 19:33:00 +0000 | [diff] [blame] | 1927 | testcase( flags3!=pIn3->flags ); /* Possible if pIn1==pIn3 */ |
drh | 4b37cd4 | 2016-06-25 11:43:47 +0000 | [diff] [blame] | 1928 | flags3 = pIn3->flags; |
drh | 5fd0c12 | 2016-04-04 13:46:24 +0000 | [diff] [blame] | 1929 | } |
| 1930 | if( (flags3 & (MEM_Int|MEM_Real|MEM_Str))==MEM_Str ){ |
| 1931 | applyNumericAffinity(pIn3,0); |
| 1932 | } |
drh | 24a0962 | 2014-09-18 16:28:59 +0000 | [diff] [blame] | 1933 | } |
drh | 64caee4 | 2016-09-09 19:33:00 +0000 | [diff] [blame] | 1934 | /* Handle the common case of integer comparison here, as an |
| 1935 | ** optimization, to avoid a call to sqlite3MemCompare() */ |
| 1936 | if( (pIn1->flags & pIn3->flags & MEM_Int)!=0 ){ |
| 1937 | if( pIn3->u.i > pIn1->u.i ){ res = +1; goto compare_op; } |
| 1938 | if( pIn3->u.i < pIn1->u.i ){ res = -1; goto compare_op; } |
| 1939 | res = 0; |
| 1940 | goto compare_op; |
| 1941 | } |
drh | 24a0962 | 2014-09-18 16:28:59 +0000 | [diff] [blame] | 1942 | }else if( affinity==SQLITE_AFF_TEXT ){ |
drh | e5520e2 | 2015-12-31 04:34:26 +0000 | [diff] [blame] | 1943 | if( (flags1 & MEM_Str)==0 && (flags1 & (MEM_Int|MEM_Real))!=0 ){ |
drh | e7a3466 | 2014-09-19 22:44:20 +0000 | [diff] [blame] | 1944 | testcase( pIn1->flags & MEM_Int ); |
| 1945 | testcase( pIn1->flags & MEM_Real ); |
drh | 24a0962 | 2014-09-18 16:28:59 +0000 | [diff] [blame] | 1946 | sqlite3VdbeMemStringify(pIn1, encoding, 1); |
drh | bc8a6b3 | 2015-03-31 11:42:23 +0000 | [diff] [blame] | 1947 | testcase( (flags1&MEM_Dyn) != (pIn1->flags&MEM_Dyn) ); |
| 1948 | flags1 = (pIn1->flags & ~MEM_TypeMask) | (flags1 & MEM_TypeMask); |
drh | 21e19b4 | 2016-09-15 14:54:51 +0000 | [diff] [blame] | 1949 | assert( pIn1!=pIn3 ); |
drh | 24a0962 | 2014-09-18 16:28:59 +0000 | [diff] [blame] | 1950 | } |
drh | e5520e2 | 2015-12-31 04:34:26 +0000 | [diff] [blame] | 1951 | if( (flags3 & MEM_Str)==0 && (flags3 & (MEM_Int|MEM_Real))!=0 ){ |
drh | e7a3466 | 2014-09-19 22:44:20 +0000 | [diff] [blame] | 1952 | testcase( pIn3->flags & MEM_Int ); |
| 1953 | testcase( pIn3->flags & MEM_Real ); |
drh | 24a0962 | 2014-09-18 16:28:59 +0000 | [diff] [blame] | 1954 | sqlite3VdbeMemStringify(pIn3, encoding, 1); |
drh | bc8a6b3 | 2015-03-31 11:42:23 +0000 | [diff] [blame] | 1955 | testcase( (flags3&MEM_Dyn) != (pIn3->flags&MEM_Dyn) ); |
| 1956 | flags3 = (pIn3->flags & ~MEM_TypeMask) | (flags3 & MEM_TypeMask); |
drh | 24a0962 | 2014-09-18 16:28:59 +0000 | [diff] [blame] | 1957 | } |
drh | 6a2fe09 | 2009-09-23 02:29:36 +0000 | [diff] [blame] | 1958 | } |
drh | 6a2fe09 | 2009-09-23 02:29:36 +0000 | [diff] [blame] | 1959 | assert( pOp->p4type==P4_COLLSEQ || pOp->p4.pColl==0 ); |
drh | 4910a76 | 2016-09-03 01:46:15 +0000 | [diff] [blame] | 1960 | res = sqlite3MemCompare(pIn3, pIn1, pOp->p4.pColl); |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 1961 | } |
drh | 64caee4 | 2016-09-09 19:33:00 +0000 | [diff] [blame] | 1962 | compare_op: |
drh | 5859636 | 2017-08-03 00:29:23 +0000 | [diff] [blame] | 1963 | /* At this point, res is negative, zero, or positive if reg[P1] is |
| 1964 | ** less than, equal to, or greater than reg[P3], respectively. Compute |
| 1965 | ** the answer to this operator in res2, depending on what the comparison |
| 1966 | ** operator actually is. The next block of code depends on the fact |
| 1967 | ** that the 6 comparison operators are consecutive integers in this |
| 1968 | ** order: NE, EQ, GT, LE, LT, GE */ |
| 1969 | assert( OP_Eq==OP_Ne+1 ); assert( OP_Gt==OP_Ne+2 ); assert( OP_Le==OP_Ne+3 ); |
| 1970 | assert( OP_Lt==OP_Ne+4 ); assert( OP_Ge==OP_Ne+5 ); |
| 1971 | if( res<0 ){ /* ne, eq, gt, le, lt, ge */ |
| 1972 | static const unsigned char aLTb[] = { 1, 0, 0, 1, 1, 0 }; |
| 1973 | res2 = aLTb[pOp->opcode - OP_Ne]; |
| 1974 | }else if( res==0 ){ |
| 1975 | static const unsigned char aEQb[] = { 0, 1, 0, 1, 0, 1 }; |
| 1976 | res2 = aEQb[pOp->opcode - OP_Ne]; |
| 1977 | }else{ |
| 1978 | static const unsigned char aGTb[] = { 1, 0, 1, 0, 0, 1 }; |
| 1979 | res2 = aGTb[pOp->opcode - OP_Ne]; |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 1980 | } |
| 1981 | |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 1982 | /* Undo any changes made by applyAffinity() to the input registers. */ |
| 1983 | assert( (pIn1->flags & MEM_Dyn) == (flags1 & MEM_Dyn) ); |
| 1984 | pIn1->flags = flags1; |
| 1985 | assert( (pIn3->flags & MEM_Dyn) == (flags3 & MEM_Dyn) ); |
| 1986 | pIn3->flags = flags3; |
| 1987 | |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 1988 | if( pOp->p5 & SQLITE_STOREP2 ){ |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 1989 | pOut = &aMem[pOp->p2]; |
drh | 4910a76 | 2016-09-03 01:46:15 +0000 | [diff] [blame] | 1990 | iCompare = res; |
drh | 3fffbf9 | 2016-09-05 15:02:41 +0000 | [diff] [blame] | 1991 | if( (pOp->p5 & SQLITE_KEEPNULL)!=0 ){ |
drh | 79752b6 | 2016-08-13 10:02:17 +0000 | [diff] [blame] | 1992 | /* The KEEPNULL flag prevents OP_Eq from overwriting a NULL with 1 |
drh | 3fffbf9 | 2016-09-05 15:02:41 +0000 | [diff] [blame] | 1993 | ** and prevents OP_Ne from overwriting NULL with 0. This flag |
| 1994 | ** is only used in contexts where either: |
| 1995 | ** (1) op==OP_Eq && (r[P2]==NULL || r[P2]==0) |
| 1996 | ** (2) op==OP_Ne && (r[P2]==NULL || r[P2]==1) |
| 1997 | ** Therefore it is not necessary to check the content of r[P2] for |
| 1998 | ** NULL. */ |
drh | 79752b6 | 2016-08-13 10:02:17 +0000 | [diff] [blame] | 1999 | assert( pOp->opcode==OP_Ne || pOp->opcode==OP_Eq ); |
drh | 4910a76 | 2016-09-03 01:46:15 +0000 | [diff] [blame] | 2000 | assert( res2==0 || res2==1 ); |
drh | 3fffbf9 | 2016-09-05 15:02:41 +0000 | [diff] [blame] | 2001 | testcase( res2==0 && pOp->opcode==OP_Eq ); |
| 2002 | testcase( res2==1 && pOp->opcode==OP_Eq ); |
| 2003 | testcase( res2==0 && pOp->opcode==OP_Ne ); |
| 2004 | testcase( res2==1 && pOp->opcode==OP_Ne ); |
drh | 4910a76 | 2016-09-03 01:46:15 +0000 | [diff] [blame] | 2005 | if( (pOp->opcode==OP_Eq)==res2 ) break; |
drh | 79752b6 | 2016-08-13 10:02:17 +0000 | [diff] [blame] | 2006 | } |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 2007 | memAboutToChange(p, pOut); |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 2008 | MemSetTypeFlag(pOut, MEM_Int); |
drh | 4910a76 | 2016-09-03 01:46:15 +0000 | [diff] [blame] | 2009 | pOut->u.i = res2; |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 2010 | REGISTER_TRACE(pOp->p2, pOut); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 2011 | }else{ |
drh | f4345e4 | 2014-02-18 11:31:59 +0000 | [diff] [blame] | 2012 | VdbeBranchTaken(res!=0, (pOp->p5 & SQLITE_NULLEQ)?2:3); |
drh | 4910a76 | 2016-09-03 01:46:15 +0000 | [diff] [blame] | 2013 | if( res2 ){ |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 2014 | goto jump_to_p2; |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 2015 | } |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 2016 | } |
| 2017 | break; |
| 2018 | } |
drh | c9b84a1 | 2002-06-20 11:36:48 +0000 | [diff] [blame] | 2019 | |
drh | 79752b6 | 2016-08-13 10:02:17 +0000 | [diff] [blame] | 2020 | /* Opcode: ElseNotEq * P2 * * * |
| 2021 | ** |
drh | fd7459e | 2016-09-17 17:39:01 +0000 | [diff] [blame] | 2022 | ** This opcode must immediately follow an OP_Lt or OP_Gt comparison operator. |
| 2023 | ** If result of an OP_Eq comparison on the same two operands |
| 2024 | ** would have be NULL or false (0), then then jump to P2. |
| 2025 | ** If the result of an OP_Eq comparison on the two previous operands |
| 2026 | ** would have been true (1), then fall through. |
drh | 79752b6 | 2016-08-13 10:02:17 +0000 | [diff] [blame] | 2027 | */ |
| 2028 | case OP_ElseNotEq: { /* same as TK_ESCAPE, jump */ |
| 2029 | assert( pOp>aOp ); |
| 2030 | assert( pOp[-1].opcode==OP_Lt || pOp[-1].opcode==OP_Gt ); |
drh | 4910a76 | 2016-09-03 01:46:15 +0000 | [diff] [blame] | 2031 | assert( pOp[-1].p5 & SQLITE_STOREP2 ); |
drh | 0f825a7 | 2016-08-13 14:17:02 +0000 | [diff] [blame] | 2032 | VdbeBranchTaken(iCompare!=0, 2); |
| 2033 | if( iCompare!=0 ) goto jump_to_p2; |
drh | 79752b6 | 2016-08-13 10:02:17 +0000 | [diff] [blame] | 2034 | break; |
| 2035 | } |
| 2036 | |
| 2037 | |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 2038 | /* Opcode: Permutation * * * P4 * |
| 2039 | ** |
drh | b7dab70 | 2017-01-26 18:00:00 +0000 | [diff] [blame] | 2040 | ** Set the permutation used by the OP_Compare operator in the next |
| 2041 | ** instruction. The permutation is stored in the P4 operand. |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 2042 | ** |
drh | 953f761 | 2012-12-07 22:18:54 +0000 | [diff] [blame] | 2043 | ** The permutation is only valid until the next OP_Compare that has |
| 2044 | ** the OPFLAG_PERMUTE bit set in P5. Typically the OP_Permutation should |
| 2045 | ** occur immediately prior to the OP_Compare. |
drh | b170202 | 2016-01-30 00:45:18 +0000 | [diff] [blame] | 2046 | ** |
| 2047 | ** The first integer in the P4 integer array is the length of the array |
| 2048 | ** and does not become part of the permutation. |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 2049 | */ |
| 2050 | case OP_Permutation: { |
| 2051 | assert( pOp->p4type==P4_INTARRAY ); |
| 2052 | assert( pOp->p4.ai ); |
drh | b7dab70 | 2017-01-26 18:00:00 +0000 | [diff] [blame] | 2053 | assert( pOp[1].opcode==OP_Compare ); |
| 2054 | assert( pOp[1].p5 & OPFLAG_PERMUTE ); |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 2055 | break; |
| 2056 | } |
| 2057 | |
drh | 953f761 | 2012-12-07 22:18:54 +0000 | [diff] [blame] | 2058 | /* Opcode: Compare P1 P2 P3 P4 P5 |
drh | 079a307 | 2014-03-19 14:10:55 +0000 | [diff] [blame] | 2059 | ** Synopsis: r[P1@P3] <-> r[P2@P3] |
drh | 16ee60f | 2008-06-20 18:13:25 +0000 | [diff] [blame] | 2060 | ** |
drh | 710c484 | 2010-08-30 01:17:20 +0000 | [diff] [blame] | 2061 | ** Compare two vectors of registers in reg(P1)..reg(P1+P3-1) (call this |
| 2062 | ** 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] | 2063 | ** the comparison for use by the next OP_Jump instruct. |
| 2064 | ** |
drh | 0ca10df | 2012-12-08 13:26:23 +0000 | [diff] [blame] | 2065 | ** If P5 has the OPFLAG_PERMUTE bit set, then the order of comparison is |
| 2066 | ** determined by the most recent OP_Permutation operator. If the |
| 2067 | ** OPFLAG_PERMUTE bit is clear, then register are compared in sequential |
| 2068 | ** order. |
| 2069 | ** |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 2070 | ** P4 is a KeyInfo structure that defines collating sequences and sort |
| 2071 | ** orders for the comparison. The permutation applies to registers |
| 2072 | ** only. The KeyInfo elements are used sequentially. |
| 2073 | ** |
| 2074 | ** The comparison is a sort comparison, so NULLs compare equal, |
| 2075 | ** NULLs are less than numbers, numbers are less than strings, |
drh | 16ee60f | 2008-06-20 18:13:25 +0000 | [diff] [blame] | 2076 | ** and strings are less than blobs. |
| 2077 | */ |
| 2078 | case OP_Compare: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2079 | int n; |
| 2080 | int i; |
| 2081 | int p1; |
| 2082 | int p2; |
| 2083 | const KeyInfo *pKeyInfo; |
| 2084 | int idx; |
| 2085 | CollSeq *pColl; /* Collating sequence to use on this term */ |
| 2086 | int bRev; /* True for DESCENDING sort order */ |
drh | b7dab70 | 2017-01-26 18:00:00 +0000 | [diff] [blame] | 2087 | int *aPermute; /* The permutation */ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2088 | |
drh | b7dab70 | 2017-01-26 18:00:00 +0000 | [diff] [blame] | 2089 | if( (pOp->p5 & OPFLAG_PERMUTE)==0 ){ |
| 2090 | aPermute = 0; |
| 2091 | }else{ |
| 2092 | assert( pOp>aOp ); |
| 2093 | assert( pOp[-1].opcode==OP_Permutation ); |
| 2094 | assert( pOp[-1].p4type==P4_INTARRAY ); |
| 2095 | aPermute = pOp[-1].p4.ai + 1; |
| 2096 | assert( aPermute!=0 ); |
| 2097 | } |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2098 | n = pOp->p3; |
| 2099 | pKeyInfo = pOp->p4.pKeyInfo; |
drh | 16ee60f | 2008-06-20 18:13:25 +0000 | [diff] [blame] | 2100 | assert( n>0 ); |
drh | 93a960a | 2008-07-10 00:32:42 +0000 | [diff] [blame] | 2101 | assert( pKeyInfo!=0 ); |
drh | 16ee60f | 2008-06-20 18:13:25 +0000 | [diff] [blame] | 2102 | p1 = pOp->p1; |
drh | 16ee60f | 2008-06-20 18:13:25 +0000 | [diff] [blame] | 2103 | p2 = pOp->p2; |
drh | d879e3e | 2017-02-13 13:35:55 +0000 | [diff] [blame] | 2104 | #ifdef SQLITE_DEBUG |
drh | 6a2fe09 | 2009-09-23 02:29:36 +0000 | [diff] [blame] | 2105 | if( aPermute ){ |
| 2106 | int k, mx = 0; |
| 2107 | for(k=0; k<n; k++) if( aPermute[k]>mx ) mx = aPermute[k]; |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 2108 | assert( p1>0 && p1+mx<=(p->nMem+1 - p->nCursor)+1 ); |
| 2109 | assert( p2>0 && p2+mx<=(p->nMem+1 - p->nCursor)+1 ); |
drh | 6a2fe09 | 2009-09-23 02:29:36 +0000 | [diff] [blame] | 2110 | }else{ |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 2111 | assert( p1>0 && p1+n<=(p->nMem+1 - p->nCursor)+1 ); |
| 2112 | assert( p2>0 && p2+n<=(p->nMem+1 - p->nCursor)+1 ); |
drh | 6a2fe09 | 2009-09-23 02:29:36 +0000 | [diff] [blame] | 2113 | } |
| 2114 | #endif /* SQLITE_DEBUG */ |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 2115 | for(i=0; i<n; i++){ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2116 | idx = aPermute ? aPermute[i] : i; |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 2117 | assert( memIsValid(&aMem[p1+idx]) ); |
| 2118 | assert( memIsValid(&aMem[p2+idx]) ); |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 2119 | REGISTER_TRACE(p1+idx, &aMem[p1+idx]); |
| 2120 | REGISTER_TRACE(p2+idx, &aMem[p2+idx]); |
drh | a485ad1 | 2017-08-02 22:43:14 +0000 | [diff] [blame] | 2121 | assert( i<pKeyInfo->nKeyField ); |
drh | 93a960a | 2008-07-10 00:32:42 +0000 | [diff] [blame] | 2122 | pColl = pKeyInfo->aColl[i]; |
| 2123 | bRev = pKeyInfo->aSortOrder[i]; |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 2124 | iCompare = sqlite3MemCompare(&aMem[p1+idx], &aMem[p2+idx], pColl); |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 2125 | if( iCompare ){ |
| 2126 | if( bRev ) iCompare = -iCompare; |
| 2127 | break; |
| 2128 | } |
drh | 16ee60f | 2008-06-20 18:13:25 +0000 | [diff] [blame] | 2129 | } |
| 2130 | break; |
| 2131 | } |
| 2132 | |
| 2133 | /* Opcode: Jump P1 P2 P3 * * |
| 2134 | ** |
| 2135 | ** Jump to the instruction at address P1, P2, or P3 depending on whether |
| 2136 | ** in the most recent OP_Compare instruction the P1 vector was less than |
| 2137 | ** equal to, or greater than the P2 vector, respectively. |
| 2138 | */ |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 2139 | case OP_Jump: { /* jump */ |
| 2140 | if( iCompare<0 ){ |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 2141 | VdbeBranchTaken(0,3); pOp = &aOp[pOp->p1 - 1]; |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 2142 | }else if( iCompare==0 ){ |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 2143 | VdbeBranchTaken(1,3); pOp = &aOp[pOp->p2 - 1]; |
drh | 16ee60f | 2008-06-20 18:13:25 +0000 | [diff] [blame] | 2144 | }else{ |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 2145 | VdbeBranchTaken(2,3); pOp = &aOp[pOp->p3 - 1]; |
drh | 16ee60f | 2008-06-20 18:13:25 +0000 | [diff] [blame] | 2146 | } |
| 2147 | break; |
| 2148 | } |
| 2149 | |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 2150 | /* Opcode: And P1 P2 P3 * * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 2151 | ** Synopsis: r[P3]=(r[P1] && r[P2]) |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2152 | ** |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 2153 | ** Take the logical AND of the values in registers P1 and P2 and |
| 2154 | ** write the result into register P3. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2155 | ** |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 2156 | ** If either P1 or P2 is 0 (false) then the result is 0 even if |
| 2157 | ** the other input is NULL. A NULL and true or two NULLs give |
| 2158 | ** a NULL output. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2159 | */ |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 2160 | /* Opcode: Or P1 P2 P3 * * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 2161 | ** Synopsis: r[P3]=(r[P1] || r[P2]) |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 2162 | ** |
| 2163 | ** Take the logical OR of the values in register P1 and P2 and |
| 2164 | ** store the answer in register P3. |
| 2165 | ** |
| 2166 | ** If either P1 or P2 is nonzero (true) then the result is 1 (true) |
| 2167 | ** even if the other input is NULL. A NULL and false or two NULLs |
| 2168 | ** give a NULL output. |
| 2169 | */ |
| 2170 | case OP_And: /* same as TK_AND, in1, in2, out3 */ |
| 2171 | case OP_Or: { /* same as TK_OR, in1, in2, out3 */ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2172 | int v1; /* Left operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */ |
| 2173 | int v2; /* Right operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */ |
drh | bb11351 | 2002-05-27 01:04:51 +0000 | [diff] [blame] | 2174 | |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 2175 | pIn1 = &aMem[pOp->p1]; |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 2176 | if( pIn1->flags & MEM_Null ){ |
drh | bb11351 | 2002-05-27 01:04:51 +0000 | [diff] [blame] | 2177 | v1 = 2; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2178 | }else{ |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 2179 | v1 = sqlite3VdbeIntValue(pIn1)!=0; |
drh | bb11351 | 2002-05-27 01:04:51 +0000 | [diff] [blame] | 2180 | } |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 2181 | pIn2 = &aMem[pOp->p2]; |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 2182 | if( pIn2->flags & MEM_Null ){ |
drh | bb11351 | 2002-05-27 01:04:51 +0000 | [diff] [blame] | 2183 | v2 = 2; |
| 2184 | }else{ |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 2185 | v2 = sqlite3VdbeIntValue(pIn2)!=0; |
drh | bb11351 | 2002-05-27 01:04:51 +0000 | [diff] [blame] | 2186 | } |
| 2187 | if( pOp->opcode==OP_And ){ |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 2188 | 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] | 2189 | v1 = and_logic[v1*3+v2]; |
| 2190 | }else{ |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 2191 | 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] | 2192 | v1 = or_logic[v1*3+v2]; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2193 | } |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 2194 | pOut = &aMem[pOp->p3]; |
drh | bb11351 | 2002-05-27 01:04:51 +0000 | [diff] [blame] | 2195 | if( v1==2 ){ |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 2196 | MemSetTypeFlag(pOut, MEM_Null); |
drh | bb11351 | 2002-05-27 01:04:51 +0000 | [diff] [blame] | 2197 | }else{ |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 2198 | pOut->u.i = v1; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 2199 | MemSetTypeFlag(pOut, MEM_Int); |
drh | bb11351 | 2002-05-27 01:04:51 +0000 | [diff] [blame] | 2200 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2201 | break; |
| 2202 | } |
| 2203 | |
drh | e99fa2a | 2008-12-15 15:27:51 +0000 | [diff] [blame] | 2204 | /* Opcode: Not P1 P2 * * * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 2205 | ** Synopsis: r[P2]= !r[P1] |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2206 | ** |
drh | e99fa2a | 2008-12-15 15:27:51 +0000 | [diff] [blame] | 2207 | ** Interpret the value in register P1 as a boolean value. Store the |
| 2208 | ** boolean complement in register P2. If the value in register P1 is |
| 2209 | ** NULL, then a NULL is stored in P2. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2210 | */ |
drh | 93952eb | 2009-11-13 19:43:43 +0000 | [diff] [blame] | 2211 | case OP_Not: { /* same as TK_NOT, in1, out2 */ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 2212 | pIn1 = &aMem[pOp->p1]; |
| 2213 | pOut = &aMem[pOp->p2]; |
drh | 0725cab | 2014-09-17 14:52:46 +0000 | [diff] [blame] | 2214 | sqlite3VdbeMemSetNull(pOut); |
| 2215 | if( (pIn1->flags & MEM_Null)==0 ){ |
| 2216 | pOut->flags = MEM_Int; |
| 2217 | pOut->u.i = !sqlite3VdbeIntValue(pIn1); |
drh | e99fa2a | 2008-12-15 15:27:51 +0000 | [diff] [blame] | 2218 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2219 | break; |
| 2220 | } |
| 2221 | |
drh | e99fa2a | 2008-12-15 15:27:51 +0000 | [diff] [blame] | 2222 | /* Opcode: BitNot P1 P2 * * * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 2223 | ** Synopsis: r[P1]= ~r[P1] |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 2224 | ** |
drh | e99fa2a | 2008-12-15 15:27:51 +0000 | [diff] [blame] | 2225 | ** Interpret the content of register P1 as an integer. Store the |
| 2226 | ** ones-complement of the P1 value into register P2. If P1 holds |
| 2227 | ** a NULL then store a NULL in P2. |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 2228 | */ |
drh | 93952eb | 2009-11-13 19:43:43 +0000 | [diff] [blame] | 2229 | case OP_BitNot: { /* same as TK_BITNOT, in1, out2 */ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 2230 | pIn1 = &aMem[pOp->p1]; |
| 2231 | pOut = &aMem[pOp->p2]; |
drh | 0725cab | 2014-09-17 14:52:46 +0000 | [diff] [blame] | 2232 | sqlite3VdbeMemSetNull(pOut); |
| 2233 | if( (pIn1->flags & MEM_Null)==0 ){ |
| 2234 | pOut->flags = MEM_Int; |
| 2235 | pOut->u.i = ~sqlite3VdbeIntValue(pIn1); |
drh | e99fa2a | 2008-12-15 15:27:51 +0000 | [diff] [blame] | 2236 | } |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 2237 | break; |
| 2238 | } |
| 2239 | |
drh | 48f2d3b | 2011-09-16 01:34:43 +0000 | [diff] [blame] | 2240 | /* Opcode: Once P1 P2 * * * |
| 2241 | ** |
drh | ab087d4 | 2017-03-24 17:59:56 +0000 | [diff] [blame] | 2242 | ** Fall through to the next instruction the first time this opcode is |
| 2243 | ** encountered on each invocation of the byte-code program. Jump to P2 |
| 2244 | ** on the second and all subsequent encounters during the same invocation. |
| 2245 | ** |
| 2246 | ** Top-level programs determine first invocation by comparing the P1 |
| 2247 | ** operand against the P1 operand on the OP_Init opcode at the beginning |
| 2248 | ** of the program. If the P1 values differ, then fall through and make |
| 2249 | ** the P1 of this opcode equal to the P1 of OP_Init. If P1 values are |
| 2250 | ** the same then take the jump. |
| 2251 | ** |
| 2252 | ** For subprograms, there is a bitmask in the VdbeFrame that determines |
| 2253 | ** whether or not the jump should be taken. The bitmask is necessary |
| 2254 | ** because the self-altering code trick does not work for recursive |
| 2255 | ** triggers. |
drh | 48f2d3b | 2011-09-16 01:34:43 +0000 | [diff] [blame] | 2256 | */ |
dan | 1d8cb21 | 2011-12-09 13:24:16 +0000 | [diff] [blame] | 2257 | case OP_Once: { /* jump */ |
drh | ab087d4 | 2017-03-24 17:59:56 +0000 | [diff] [blame] | 2258 | u32 iAddr; /* Address of this instruction */ |
drh | 9e5eb9c | 2016-09-18 16:08:10 +0000 | [diff] [blame] | 2259 | assert( p->aOp[0].opcode==OP_Init ); |
drh | ab087d4 | 2017-03-24 17:59:56 +0000 | [diff] [blame] | 2260 | if( p->pFrame ){ |
| 2261 | iAddr = (int)(pOp - p->aOp); |
| 2262 | if( (p->pFrame->aOnce[iAddr/8] & (1<<(iAddr & 7)))!=0 ){ |
| 2263 | VdbeBranchTaken(1, 2); |
drh | ab087d4 | 2017-03-24 17:59:56 +0000 | [diff] [blame] | 2264 | goto jump_to_p2; |
| 2265 | } |
drh | 18333ef | 2017-03-24 18:38:41 +0000 | [diff] [blame] | 2266 | p->pFrame->aOnce[iAddr/8] |= 1<<(iAddr & 7); |
dan | 1d8cb21 | 2011-12-09 13:24:16 +0000 | [diff] [blame] | 2267 | }else{ |
drh | ab087d4 | 2017-03-24 17:59:56 +0000 | [diff] [blame] | 2268 | if( p->aOp[0].p1==pOp->p1 ){ |
| 2269 | VdbeBranchTaken(1, 2); |
| 2270 | goto jump_to_p2; |
| 2271 | } |
dan | 1d8cb21 | 2011-12-09 13:24:16 +0000 | [diff] [blame] | 2272 | } |
drh | ab087d4 | 2017-03-24 17:59:56 +0000 | [diff] [blame] | 2273 | VdbeBranchTaken(0, 2); |
| 2274 | pOp->p1 = p->aOp[0].p1; |
dan | 1d8cb21 | 2011-12-09 13:24:16 +0000 | [diff] [blame] | 2275 | break; |
| 2276 | } |
| 2277 | |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 2278 | /* Opcode: If P1 P2 P3 * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2279 | ** |
drh | ef8662b | 2011-06-20 21:47:58 +0000 | [diff] [blame] | 2280 | ** Jump to P2 if the value in register P1 is true. The value |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 2281 | ** is considered true if it is numeric and non-zero. If the value |
drh | e21a6e1 | 2014-08-01 18:00:24 +0000 | [diff] [blame] | 2282 | ** 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] | 2283 | */ |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 2284 | /* Opcode: IfNot P1 P2 P3 * * |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 2285 | ** |
drh | ef8662b | 2011-06-20 21:47:58 +0000 | [diff] [blame] | 2286 | ** Jump to P2 if the value in register P1 is False. The value |
drh | b8475df | 2011-12-09 16:21:19 +0000 | [diff] [blame] | 2287 | ** 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] | 2288 | ** 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] | 2289 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2290 | case OP_If: /* jump, in1 */ |
| 2291 | case OP_IfNot: { /* jump, in1 */ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2292 | int c; |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 2293 | pIn1 = &aMem[pOp->p1]; |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 2294 | if( pIn1->flags & MEM_Null ){ |
| 2295 | c = pOp->p3; |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 2296 | }else{ |
drh | cad4283 | 2018-01-25 01:20:29 +0000 | [diff] [blame] | 2297 | if( pIn1->flags & MEM_Int ){ |
| 2298 | c = pIn1->u.i!=0; |
| 2299 | }else{ |
| 2300 | c = sqlite3VdbeRealValue(pIn1)!=0.0; |
| 2301 | } |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 2302 | if( pOp->opcode==OP_IfNot ) c = !c; |
| 2303 | } |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 2304 | VdbeBranchTaken(c!=0, 2); |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 2305 | if( c ){ |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 2306 | goto jump_to_p2; |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 2307 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2308 | break; |
| 2309 | } |
| 2310 | |
drh | 830ecf9 | 2009-06-18 00:41:55 +0000 | [diff] [blame] | 2311 | /* Opcode: IsNull P1 P2 * * * |
drh | 72e26de | 2016-08-24 21:24:04 +0000 | [diff] [blame] | 2312 | ** Synopsis: if r[P1]==NULL goto P2 |
drh | 477df4b | 2008-01-05 18:48:24 +0000 | [diff] [blame] | 2313 | ** |
drh | 830ecf9 | 2009-06-18 00:41:55 +0000 | [diff] [blame] | 2314 | ** Jump to P2 if the value in register P1 is NULL. |
drh | 477df4b | 2008-01-05 18:48:24 +0000 | [diff] [blame] | 2315 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2316 | case OP_IsNull: { /* same as TK_ISNULL, jump, in1 */ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 2317 | pIn1 = &aMem[pOp->p1]; |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 2318 | VdbeBranchTaken( (pIn1->flags & MEM_Null)!=0, 2); |
drh | 830ecf9 | 2009-06-18 00:41:55 +0000 | [diff] [blame] | 2319 | if( (pIn1->flags & MEM_Null)!=0 ){ |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 2320 | goto jump_to_p2; |
drh | 830ecf9 | 2009-06-18 00:41:55 +0000 | [diff] [blame] | 2321 | } |
drh | 477df4b | 2008-01-05 18:48:24 +0000 | [diff] [blame] | 2322 | break; |
| 2323 | } |
| 2324 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 2325 | /* Opcode: NotNull P1 P2 * * * |
drh | fc8d4f9 | 2013-11-08 15:19:46 +0000 | [diff] [blame] | 2326 | ** Synopsis: if r[P1]!=NULL goto P2 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2327 | ** |
drh | 6a288a3 | 2008-01-07 19:20:24 +0000 | [diff] [blame] | 2328 | ** Jump to P2 if the value in register P1 is not NULL. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2329 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2330 | case OP_NotNull: { /* same as TK_NOTNULL, jump, in1 */ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 2331 | pIn1 = &aMem[pOp->p1]; |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 2332 | VdbeBranchTaken( (pIn1->flags & MEM_Null)==0, 2); |
drh | 6a288a3 | 2008-01-07 19:20:24 +0000 | [diff] [blame] | 2333 | if( (pIn1->flags & MEM_Null)==0 ){ |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 2334 | goto jump_to_p2; |
drh | 6a288a3 | 2008-01-07 19:20:24 +0000 | [diff] [blame] | 2335 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2336 | break; |
| 2337 | } |
| 2338 | |
drh | 31d6fd5 | 2017-04-14 19:03:10 +0000 | [diff] [blame] | 2339 | /* Opcode: IfNullRow P1 P2 P3 * * |
| 2340 | ** Synopsis: if P1.nullRow then r[P3]=NULL, goto P2 |
| 2341 | ** |
| 2342 | ** Check the cursor P1 to see if it is currently pointing at a NULL row. |
| 2343 | ** If it is, then set register P3 to NULL and jump immediately to P2. |
| 2344 | ** If P1 is not on a NULL row, then fall through without making any |
| 2345 | ** changes. |
| 2346 | */ |
| 2347 | case OP_IfNullRow: { /* jump */ |
| 2348 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
drh | 3f1e9e0 | 2017-05-23 01:21:07 +0000 | [diff] [blame] | 2349 | assert( p->apCsr[pOp->p1]!=0 ); |
drh | 31d6fd5 | 2017-04-14 19:03:10 +0000 | [diff] [blame] | 2350 | if( p->apCsr[pOp->p1]->nullRow ){ |
| 2351 | sqlite3VdbeMemSetNull(aMem + pOp->p3); |
| 2352 | goto jump_to_p2; |
| 2353 | } |
| 2354 | break; |
| 2355 | } |
| 2356 | |
drh | 092457b | 2017-12-29 15:04:49 +0000 | [diff] [blame] | 2357 | #ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC |
| 2358 | /* Opcode: Offset P1 P2 P3 * * |
| 2359 | ** Synopsis: r[P3] = sqlite_offset(P1) |
drh | 2fc865c | 2017-12-16 20:20:37 +0000 | [diff] [blame] | 2360 | ** |
drh | 092457b | 2017-12-29 15:04:49 +0000 | [diff] [blame] | 2361 | ** 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] | 2362 | ** start of the payload for the record at which that cursor P1 is currently |
| 2363 | ** pointing. |
drh | fe6d20e | 2017-12-29 14:33:54 +0000 | [diff] [blame] | 2364 | ** |
drh | 092457b | 2017-12-29 15:04:49 +0000 | [diff] [blame] | 2365 | ** P2 is the column number for the argument to the sqlite_offset() function. |
drh | fe6d20e | 2017-12-29 14:33:54 +0000 | [diff] [blame] | 2366 | ** This opcode does not use P2 itself, but the P2 value is used by the |
| 2367 | ** code generator. The P1, P2, and P3 operands to this opcode are the |
| 2368 | ** as as for OP_Column. |
drh | 092457b | 2017-12-29 15:04:49 +0000 | [diff] [blame] | 2369 | ** |
| 2370 | ** This opcode is only available if SQLite is compiled with the |
| 2371 | ** -DSQLITE_ENABLE_OFFSET_SQL_FUNC option. |
drh | 2fc865c | 2017-12-16 20:20:37 +0000 | [diff] [blame] | 2372 | */ |
drh | 092457b | 2017-12-29 15:04:49 +0000 | [diff] [blame] | 2373 | case OP_Offset: { /* out3 */ |
drh | 2fc865c | 2017-12-16 20:20:37 +0000 | [diff] [blame] | 2374 | VdbeCursor *pC; /* The VDBE cursor */ |
| 2375 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 2376 | pC = p->apCsr[pOp->p1]; |
drh | fe6d20e | 2017-12-29 14:33:54 +0000 | [diff] [blame] | 2377 | pOut = &p->aMem[pOp->p3]; |
drh | c64487b | 2017-12-29 17:21:21 +0000 | [diff] [blame] | 2378 | if( NEVER(pC==0) || pC->eCurType!=CURTYPE_BTREE ){ |
drh | fe6d20e | 2017-12-29 14:33:54 +0000 | [diff] [blame] | 2379 | sqlite3VdbeMemSetNull(pOut); |
drh | 2fc865c | 2017-12-16 20:20:37 +0000 | [diff] [blame] | 2380 | }else{ |
drh | 092457b | 2017-12-29 15:04:49 +0000 | [diff] [blame] | 2381 | sqlite3VdbeMemSetInt64(pOut, sqlite3BtreeOffset(pC->uc.pCursor)); |
drh | 2fc865c | 2017-12-16 20:20:37 +0000 | [diff] [blame] | 2382 | } |
| 2383 | break; |
| 2384 | } |
drh | 092457b | 2017-12-29 15:04:49 +0000 | [diff] [blame] | 2385 | #endif /* SQLITE_ENABLE_OFFSET_SQL_FUNC */ |
drh | 2fc865c | 2017-12-16 20:20:37 +0000 | [diff] [blame] | 2386 | |
drh | 3e9ca09 | 2009-09-08 01:14:48 +0000 | [diff] [blame] | 2387 | /* Opcode: Column P1 P2 P3 P4 P5 |
drh | 72e26de | 2016-08-24 21:24:04 +0000 | [diff] [blame] | 2388 | ** Synopsis: r[P3]=PX |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2389 | ** |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 2390 | ** Interpret the data that cursor P1 points to as a structure built using |
| 2391 | ** the MakeRecord instruction. (See the MakeRecord opcode for additional |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 2392 | ** information about the format of the data.) Extract the P2-th column |
| 2393 | ** from this record. If there are less that (P2+1) |
| 2394 | ** values in the record, extract a NULL. |
| 2395 | ** |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2396 | ** The value extracted is stored in register P3. |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2397 | ** |
drh | 1cc3a36 | 2017-04-03 13:17:31 +0000 | [diff] [blame] | 2398 | ** If the record contains fewer than P2 fields, then extract a NULL. Or, |
danielk1977 | 1f4aa33 | 2008-01-03 09:51:55 +0000 | [diff] [blame] | 2399 | ** if the P4 argument is a P4_MEM use the value of the P4 argument as |
| 2400 | ** the result. |
drh | 3e9ca09 | 2009-09-08 01:14:48 +0000 | [diff] [blame] | 2401 | ** |
| 2402 | ** If the OPFLAG_CLEARCACHE bit is set on P5 and P1 is a pseudo-table cursor, |
| 2403 | ** then the cache of the cursor is reset prior to extracting the column. |
| 2404 | ** The first OP_Column against a pseudo-table after the value of the content |
| 2405 | ** register has changed should have this bit set. |
drh | a748fdc | 2012-03-28 01:34:47 +0000 | [diff] [blame] | 2406 | ** |
drh | 1cc3a36 | 2017-04-03 13:17:31 +0000 | [diff] [blame] | 2407 | ** If the OPFLAG_LENGTHARG and OPFLAG_TYPEOFARG bits are set on P5 then |
drh | dda5c08 | 2012-03-28 13:41:10 +0000 | [diff] [blame] | 2408 | ** the result is guaranteed to only be used as the argument of a length() |
| 2409 | ** or typeof() function, respectively. The loading of large blobs can be |
| 2410 | ** skipped for length() and all content loading can be skipped for typeof(). |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2411 | */ |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 2412 | case OP_Column: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2413 | int p2; /* column number to retrieve */ |
| 2414 | VdbeCursor *pC; /* The VDBE cursor */ |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 2415 | BtCursor *pCrsr; /* The BTree cursor */ |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 2416 | 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] | 2417 | int len; /* The length of the serialized data for the column */ |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 2418 | int i; /* Loop counter */ |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 2419 | Mem *pDest; /* Where to write the extracted value */ |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 2420 | Mem sMem; /* For storing the record being decoded */ |
drh | 399af1d | 2013-11-20 17:25:55 +0000 | [diff] [blame] | 2421 | const u8 *zData; /* Part of the record being decoded */ |
| 2422 | const u8 *zHdr; /* Next unparsed byte of the header */ |
| 2423 | const u8 *zEndHdr; /* Pointer to first byte after the header */ |
drh | c6ce3883 | 2015-10-15 21:30:24 +0000 | [diff] [blame] | 2424 | u64 offset64; /* 64-bit offset */ |
drh | 5a077b7 | 2011-08-29 02:16:18 +0000 | [diff] [blame] | 2425 | u32 t; /* A type code from the record header */ |
drh | 3e9ca09 | 2009-09-08 01:14:48 +0000 | [diff] [blame] | 2426 | Mem *pReg; /* PseudoTable input register */ |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2427 | |
dan | de892d9 | 2016-01-29 19:29:45 +0000 | [diff] [blame] | 2428 | pC = p->apCsr[pOp->p1]; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2429 | p2 = pOp->p2; |
dan | de892d9 | 2016-01-29 19:29:45 +0000 | [diff] [blame] | 2430 | |
drh | 170ad68 | 2017-06-02 15:44:22 +0000 | [diff] [blame] | 2431 | /* If the cursor cache is stale (meaning it is not currently point at |
| 2432 | ** the correct row) then bring it up-to-date by doing the necessary |
| 2433 | ** B-Tree seek. */ |
dan | de892d9 | 2016-01-29 19:29:45 +0000 | [diff] [blame] | 2434 | rc = sqlite3VdbeCursorMoveto(&pC, &p2); |
drh | 4ca239f | 2016-05-19 11:12:43 +0000 | [diff] [blame] | 2435 | if( rc ) goto abort_due_to_error; |
dan | de892d9 | 2016-01-29 19:29:45 +0000 | [diff] [blame] | 2436 | |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 2437 | assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) ); |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 2438 | pDest = &aMem[pOp->p3]; |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 2439 | memAboutToChange(p, pDest); |
drh | c8606e4 | 2013-11-20 19:28:03 +0000 | [diff] [blame] | 2440 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
danielk1977 | 6c92409 | 2007-11-12 08:09:34 +0000 | [diff] [blame] | 2441 | assert( pC!=0 ); |
drh | c8606e4 | 2013-11-20 19:28:03 +0000 | [diff] [blame] | 2442 | assert( p2<pC->nField ); |
drh | b53a5a9 | 2014-10-12 22:37:22 +0000 | [diff] [blame] | 2443 | aOffset = pC->aOffset; |
drh | 62aaa6c | 2015-11-21 17:27:42 +0000 | [diff] [blame] | 2444 | assert( pC->eCurType!=CURTYPE_VTAB ); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 2445 | assert( pC->eCurType!=CURTYPE_PSEUDO || pC->nullRow ); |
| 2446 | assert( pC->eCurType!=CURTYPE_SORTER ); |
drh | 399af1d | 2013-11-20 17:25:55 +0000 | [diff] [blame] | 2447 | |
drh | a43a02e | 2016-05-19 17:51:19 +0000 | [diff] [blame] | 2448 | if( pC->cacheStatus!=p->cacheCtr ){ /*OPTIMIZATION-IF-FALSE*/ |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2449 | if( pC->nullRow ){ |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 2450 | if( pC->eCurType==CURTYPE_PSEUDO ){ |
drh | fe0cf7a | 2017-08-16 19:20:20 +0000 | [diff] [blame] | 2451 | /* For the special case of as pseudo-cursor, the seekResult field |
| 2452 | ** identifies the register that holds the record */ |
| 2453 | assert( pC->seekResult>0 ); |
| 2454 | pReg = &aMem[pC->seekResult]; |
drh | c8606e4 | 2013-11-20 19:28:03 +0000 | [diff] [blame] | 2455 | assert( pReg->flags & MEM_Blob ); |
| 2456 | assert( memIsValid(pReg) ); |
drh | 6cd8c8c | 2017-08-15 14:14:36 +0000 | [diff] [blame] | 2457 | pC->payloadSize = pC->szRow = pReg->n; |
drh | c8606e4 | 2013-11-20 19:28:03 +0000 | [diff] [blame] | 2458 | pC->aRow = (u8*)pReg->z; |
| 2459 | }else{ |
drh | 6b5631e | 2014-11-05 15:57:39 +0000 | [diff] [blame] | 2460 | sqlite3VdbeMemSetNull(pDest); |
drh | 399af1d | 2013-11-20 17:25:55 +0000 | [diff] [blame] | 2461 | goto op_column_out; |
| 2462 | } |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2463 | }else{ |
drh | 06a09a8 | 2016-11-25 17:03:03 +0000 | [diff] [blame] | 2464 | pCrsr = pC->uc.pCursor; |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 2465 | assert( pC->eCurType==CURTYPE_BTREE ); |
drh | c8606e4 | 2013-11-20 19:28:03 +0000 | [diff] [blame] | 2466 | assert( pCrsr ); |
drh | a7c90c4 | 2016-06-04 20:37:10 +0000 | [diff] [blame] | 2467 | assert( sqlite3BtreeCursorIsValid(pCrsr) ); |
| 2468 | pC->payloadSize = sqlite3BtreePayloadSize(pCrsr); |
drh | 6cd8c8c | 2017-08-15 14:14:36 +0000 | [diff] [blame] | 2469 | pC->aRow = sqlite3BtreePayloadFetch(pCrsr, &pC->szRow); |
| 2470 | assert( pC->szRow<=pC->payloadSize ); |
| 2471 | assert( pC->szRow<=65536 ); /* Maximum page size is 64KiB */ |
| 2472 | if( pC->payloadSize > (u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){ |
drh | 5f7dacb | 2015-11-20 13:33:56 +0000 | [diff] [blame] | 2473 | goto too_big; |
drh | 399af1d | 2013-11-20 17:25:55 +0000 | [diff] [blame] | 2474 | } |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2475 | } |
drh | b73857f | 2006-03-17 00:25:59 +0000 | [diff] [blame] | 2476 | pC->cacheStatus = p->cacheCtr; |
drh | 1f613c4 | 2017-08-16 14:16:19 +0000 | [diff] [blame] | 2477 | pC->iHdrOffset = getVarint32(pC->aRow, aOffset[0]); |
drh | 399af1d | 2013-11-20 17:25:55 +0000 | [diff] [blame] | 2478 | pC->nHdrParsed = 0; |
drh | 35cd643 | 2009-06-05 14:17:21 +0000 | [diff] [blame] | 2479 | |
drh | c81aa2e | 2014-10-11 23:31:52 +0000 | [diff] [blame] | 2480 | |
drh | 1f613c4 | 2017-08-16 14:16:19 +0000 | [diff] [blame] | 2481 | if( pC->szRow<aOffset[0] ){ /*OPTIMIZATION-IF-FALSE*/ |
drh | c81aa2e | 2014-10-11 23:31:52 +0000 | [diff] [blame] | 2482 | /* pC->aRow does not have to hold the entire row, but it does at least |
| 2483 | ** need to cover the header of the record. If pC->aRow does not contain |
| 2484 | ** the complete header, then set it to zero, forcing the header to be |
| 2485 | ** dynamically allocated. */ |
| 2486 | pC->aRow = 0; |
| 2487 | pC->szRow = 0; |
drh | 848a332 | 2015-10-16 12:53:47 +0000 | [diff] [blame] | 2488 | |
| 2489 | /* Make sure a corrupt database has not given us an oversize header. |
| 2490 | ** Do this now to avoid an oversize memory allocation. |
| 2491 | ** |
| 2492 | ** Type entries can be between 1 and 5 bytes each. But 4 and 5 byte |
| 2493 | ** types use so much data space that there can only be 4096 and 32 of |
| 2494 | ** them, respectively. So the maximum header length results from a |
| 2495 | ** 3-byte type for each of the maximum of 32768 columns plus three |
| 2496 | ** extra bytes for the header length itself. 32768*3 + 3 = 98307. |
| 2497 | */ |
drh | 1f613c4 | 2017-08-16 14:16:19 +0000 | [diff] [blame] | 2498 | if( aOffset[0] > 98307 || aOffset[0] > pC->payloadSize ){ |
drh | 74588ce | 2017-09-13 00:13:05 +0000 | [diff] [blame] | 2499 | goto op_column_corrupt; |
drh | 848a332 | 2015-10-16 12:53:47 +0000 | [diff] [blame] | 2500 | } |
drh | 95b225a | 2017-08-16 11:04:22 +0000 | [diff] [blame] | 2501 | }else{ |
| 2502 | /* This is an optimization. By skipping over the first few tests |
| 2503 | ** (ex: pC->nHdrParsed<=p2) in the next section, we achieve a |
| 2504 | ** measurable performance gain. |
| 2505 | ** |
drh | 1f613c4 | 2017-08-16 14:16:19 +0000 | [diff] [blame] | 2506 | ** 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] | 2507 | ** generated by SQLite, and could be considered corruption, but we |
drh | 1f613c4 | 2017-08-16 14:16:19 +0000 | [diff] [blame] | 2508 | ** accept it for historical reasons. When aOffset[0]==0, the code this |
drh | 95b225a | 2017-08-16 11:04:22 +0000 | [diff] [blame] | 2509 | ** branch jumps to reads past the end of the record, but never more |
| 2510 | ** than a few bytes. Even if the record occurs at the end of the page |
| 2511 | ** content area, the "page header" comes after the page content and so |
| 2512 | ** this overread is harmless. Similar overreads can occur for a corrupt |
| 2513 | ** database file. |
drh | 0eda6cd | 2016-05-19 16:58:42 +0000 | [diff] [blame] | 2514 | */ |
| 2515 | zData = pC->aRow; |
| 2516 | assert( pC->nHdrParsed<=p2 ); /* Conditional skipped */ |
drh | 1f613c4 | 2017-08-16 14:16:19 +0000 | [diff] [blame] | 2517 | testcase( aOffset[0]==0 ); |
drh | 0eda6cd | 2016-05-19 16:58:42 +0000 | [diff] [blame] | 2518 | goto op_column_read_header; |
drh | c81aa2e | 2014-10-11 23:31:52 +0000 | [diff] [blame] | 2519 | } |
drh | 399af1d | 2013-11-20 17:25:55 +0000 | [diff] [blame] | 2520 | } |
drh | 35cd643 | 2009-06-05 14:17:21 +0000 | [diff] [blame] | 2521 | |
drh | 399af1d | 2013-11-20 17:25:55 +0000 | [diff] [blame] | 2522 | /* 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] | 2523 | ** parsed and valid information is in aOffset[] and pC->aType[]. |
drh | 399af1d | 2013-11-20 17:25:55 +0000 | [diff] [blame] | 2524 | */ |
drh | c8606e4 | 2013-11-20 19:28:03 +0000 | [diff] [blame] | 2525 | if( pC->nHdrParsed<=p2 ){ |
drh | 380d685 | 2013-11-20 20:58:00 +0000 | [diff] [blame] | 2526 | /* If there is more header available for parsing in the record, try |
| 2527 | ** to extract additional fields up through the p2+1-th field |
drh | 35cd643 | 2009-06-05 14:17:21 +0000 | [diff] [blame] | 2528 | */ |
drh | c8606e4 | 2013-11-20 19:28:03 +0000 | [diff] [blame] | 2529 | if( pC->iHdrOffset<aOffset[0] ){ |
| 2530 | /* Make sure zData points to enough of the record to cover the header. */ |
| 2531 | if( pC->aRow==0 ){ |
| 2532 | memset(&sMem, 0, sizeof(sMem)); |
drh | cb3cabd | 2016-11-25 19:18:28 +0000 | [diff] [blame] | 2533 | rc = sqlite3VdbeMemFromBtree(pC->uc.pCursor, 0, aOffset[0], &sMem); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 2534 | if( rc!=SQLITE_OK ) goto abort_due_to_error; |
drh | c8606e4 | 2013-11-20 19:28:03 +0000 | [diff] [blame] | 2535 | zData = (u8*)sMem.z; |
| 2536 | }else{ |
| 2537 | zData = pC->aRow; |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2538 | } |
drh | c8606e4 | 2013-11-20 19:28:03 +0000 | [diff] [blame] | 2539 | |
drh | 0c8f760 | 2014-09-19 16:56:45 +0000 | [diff] [blame] | 2540 | /* 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] | 2541 | op_column_read_header: |
drh | c8606e4 | 2013-11-20 19:28:03 +0000 | [diff] [blame] | 2542 | i = pC->nHdrParsed; |
drh | c6ce3883 | 2015-10-15 21:30:24 +0000 | [diff] [blame] | 2543 | offset64 = aOffset[i]; |
drh | c8606e4 | 2013-11-20 19:28:03 +0000 | [diff] [blame] | 2544 | zHdr = zData + pC->iHdrOffset; |
| 2545 | zEndHdr = zData + aOffset[0]; |
drh | 95b225a | 2017-08-16 11:04:22 +0000 | [diff] [blame] | 2546 | testcase( zHdr>=zEndHdr ); |
drh | c8606e4 | 2013-11-20 19:28:03 +0000 | [diff] [blame] | 2547 | do{ |
drh | 95fa606 | 2015-10-16 13:50:08 +0000 | [diff] [blame] | 2548 | if( (t = zHdr[0])<0x80 ){ |
drh | c8606e4 | 2013-11-20 19:28:03 +0000 | [diff] [blame] | 2549 | zHdr++; |
drh | faf3727 | 2015-10-16 14:23:42 +0000 | [diff] [blame] | 2550 | offset64 += sqlite3VdbeOneByteSerialTypeLen(t); |
drh | 5a077b7 | 2011-08-29 02:16:18 +0000 | [diff] [blame] | 2551 | }else{ |
drh | c8606e4 | 2013-11-20 19:28:03 +0000 | [diff] [blame] | 2552 | zHdr += sqlite3GetVarint32(zHdr, &t); |
drh | faf3727 | 2015-10-16 14:23:42 +0000 | [diff] [blame] | 2553 | offset64 += sqlite3VdbeSerialTypeLen(t); |
drh | 5a077b7 | 2011-08-29 02:16:18 +0000 | [diff] [blame] | 2554 | } |
drh | faf3727 | 2015-10-16 14:23:42 +0000 | [diff] [blame] | 2555 | pC->aType[i++] = t; |
drh | c6ce3883 | 2015-10-15 21:30:24 +0000 | [diff] [blame] | 2556 | aOffset[i] = (u32)(offset64 & 0xffffffff); |
drh | c8606e4 | 2013-11-20 19:28:03 +0000 | [diff] [blame] | 2557 | }while( i<=p2 && zHdr<zEndHdr ); |
drh | 170c276 | 2016-05-20 21:40:11 +0000 | [diff] [blame] | 2558 | |
drh | 8dd8362 | 2014-10-13 23:39:02 +0000 | [diff] [blame] | 2559 | /* The record is corrupt if any of the following are true: |
| 2560 | ** (1) the bytes of the header extend past the declared header size |
drh | 8dd8362 | 2014-10-13 23:39:02 +0000 | [diff] [blame] | 2561 | ** (2) the entire header was used but not all data was used |
drh | 8dd8362 | 2014-10-13 23:39:02 +0000 | [diff] [blame] | 2562 | ** (3) the end of the data extends beyond the end of the record. |
drh | c8606e4 | 2013-11-20 19:28:03 +0000 | [diff] [blame] | 2563 | */ |
drh | c6ce3883 | 2015-10-15 21:30:24 +0000 | [diff] [blame] | 2564 | if( (zHdr>=zEndHdr && (zHdr>zEndHdr || offset64!=pC->payloadSize)) |
| 2565 | || (offset64 > pC->payloadSize) |
drh | c8606e4 | 2013-11-20 19:28:03 +0000 | [diff] [blame] | 2566 | ){ |
drh | 95b225a | 2017-08-16 11:04:22 +0000 | [diff] [blame] | 2567 | if( aOffset[0]==0 ){ |
| 2568 | i = 0; |
| 2569 | zHdr = zEndHdr; |
| 2570 | }else{ |
| 2571 | if( pC->aRow==0 ) sqlite3VdbeMemRelease(&sMem); |
drh | 74588ce | 2017-09-13 00:13:05 +0000 | [diff] [blame] | 2572 | goto op_column_corrupt; |
drh | 95b225a | 2017-08-16 11:04:22 +0000 | [diff] [blame] | 2573 | } |
danielk1977 | dedf45b | 2006-01-13 17:12:01 +0000 | [diff] [blame] | 2574 | } |
drh | ddb2b4a | 2016-03-25 12:10:32 +0000 | [diff] [blame] | 2575 | |
drh | 170c276 | 2016-05-20 21:40:11 +0000 | [diff] [blame] | 2576 | pC->nHdrParsed = i; |
| 2577 | pC->iHdrOffset = (u32)(zHdr - zData); |
| 2578 | if( pC->aRow==0 ) sqlite3VdbeMemRelease(&sMem); |
mistachkin | 8c7cd6a | 2015-12-16 21:09:53 +0000 | [diff] [blame] | 2579 | }else{ |
drh | 9fbc885 | 2016-01-04 03:48:46 +0000 | [diff] [blame] | 2580 | t = 0; |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2581 | } |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 2582 | |
drh | f2db338 | 2015-04-30 20:33:25 +0000 | [diff] [blame] | 2583 | /* If after trying to extract new entries from the header, nHdrParsed is |
drh | 380d685 | 2013-11-20 20:58:00 +0000 | [diff] [blame] | 2584 | ** still not up to p2, that means that the record has fewer than p2 |
| 2585 | ** columns. So the result will be either the default value or a NULL. |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 2586 | */ |
drh | c8606e4 | 2013-11-20 19:28:03 +0000 | [diff] [blame] | 2587 | if( pC->nHdrParsed<=p2 ){ |
| 2588 | if( pOp->p4type==P4_MEM ){ |
| 2589 | sqlite3VdbeMemShallowCopy(pDest, pOp->p4.pMem, MEM_Static); |
| 2590 | }else{ |
drh | 22e8d83 | 2014-10-29 00:58:38 +0000 | [diff] [blame] | 2591 | sqlite3VdbeMemSetNull(pDest); |
drh | c8606e4 | 2013-11-20 19:28:03 +0000 | [diff] [blame] | 2592 | } |
danielk1977 | 3c9cc8d | 2005-01-17 03:40:08 +0000 | [diff] [blame] | 2593 | goto op_column_out; |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 2594 | } |
drh | 95fa606 | 2015-10-16 13:50:08 +0000 | [diff] [blame] | 2595 | }else{ |
| 2596 | t = pC->aType[p2]; |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 2597 | } |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2598 | |
drh | 380d685 | 2013-11-20 20:58:00 +0000 | [diff] [blame] | 2599 | /* Extract the content for the p2+1-th column. Control can only |
drh | 0c8f760 | 2014-09-19 16:56:45 +0000 | [diff] [blame] | 2600 | ** 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] | 2601 | ** all valid. |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2602 | */ |
drh | c8606e4 | 2013-11-20 19:28:03 +0000 | [diff] [blame] | 2603 | assert( p2<pC->nHdrParsed ); |
| 2604 | assert( rc==SQLITE_OK ); |
drh | 75fd054 | 2014-03-01 16:24:44 +0000 | [diff] [blame] | 2605 | assert( sqlite3VdbeCheckMemInvariants(pDest) ); |
drh | a1851ef | 2016-05-20 19:51:28 +0000 | [diff] [blame] | 2606 | if( VdbeMemDynamic(pDest) ){ |
| 2607 | sqlite3VdbeMemSetNull(pDest); |
| 2608 | } |
drh | 95fa606 | 2015-10-16 13:50:08 +0000 | [diff] [blame] | 2609 | assert( t==pC->aType[p2] ); |
drh | c8606e4 | 2013-11-20 19:28:03 +0000 | [diff] [blame] | 2610 | if( pC->szRow>=aOffset[p2+1] ){ |
drh | 380d685 | 2013-11-20 20:58:00 +0000 | [diff] [blame] | 2611 | /* This is the common case where the desired content fits on the original |
| 2612 | ** page - where the content is not on an overflow page */ |
drh | 69f6e25 | 2016-01-11 18:05:00 +0000 | [diff] [blame] | 2613 | zData = pC->aRow + aOffset[p2]; |
| 2614 | if( t<12 ){ |
| 2615 | sqlite3VdbeSerialGet(zData, t, pDest); |
| 2616 | }else{ |
| 2617 | /* If the column value is a string, we need a persistent value, not |
| 2618 | ** a MEM_Ephem value. This branch is a fast short-cut that is equivalent |
| 2619 | ** to calling sqlite3VdbeSerialGet() and sqlite3VdbeDeephemeralize(). |
| 2620 | */ |
| 2621 | static const u16 aFlag[] = { MEM_Blob, MEM_Str|MEM_Term }; |
| 2622 | pDest->n = len = (t-12)/2; |
drh | a1851ef | 2016-05-20 19:51:28 +0000 | [diff] [blame] | 2623 | pDest->enc = encoding; |
drh | 69f6e25 | 2016-01-11 18:05:00 +0000 | [diff] [blame] | 2624 | if( pDest->szMalloc < len+2 ){ |
| 2625 | pDest->flags = MEM_Null; |
| 2626 | if( sqlite3VdbeMemGrow(pDest, len+2, 0) ) goto no_mem; |
| 2627 | }else{ |
| 2628 | pDest->z = pDest->zMalloc; |
| 2629 | } |
| 2630 | memcpy(pDest->z, zData, len); |
| 2631 | pDest->z[len] = 0; |
| 2632 | pDest->z[len+1] = 0; |
| 2633 | pDest->flags = aFlag[t&1]; |
| 2634 | } |
danielk1977 | 36963fd | 2005-02-19 08:18:05 +0000 | [diff] [blame] | 2635 | }else{ |
drh | a1851ef | 2016-05-20 19:51:28 +0000 | [diff] [blame] | 2636 | pDest->enc = encoding; |
drh | 58c9608 | 2013-12-23 11:33:32 +0000 | [diff] [blame] | 2637 | /* This branch happens only when content is on overflow pages */ |
drh | 380d685 | 2013-11-20 20:58:00 +0000 | [diff] [blame] | 2638 | if( ((pOp->p5 & (OPFLAG_LENGTHARG|OPFLAG_TYPEOFARG))!=0 |
| 2639 | && ((t>=12 && (t&1)==0) || (pOp->p5 & OPFLAG_TYPEOFARG)!=0)) |
| 2640 | || (len = sqlite3VdbeSerialTypeLen(t))==0 |
drh | c8606e4 | 2013-11-20 19:28:03 +0000 | [diff] [blame] | 2641 | ){ |
drh | 2a2a696 | 2014-09-16 18:22:44 +0000 | [diff] [blame] | 2642 | /* Content is irrelevant for |
| 2643 | ** 1. the typeof() function, |
| 2644 | ** 2. the length(X) function if X is a blob, and |
| 2645 | ** 3. if the content length is zero. |
| 2646 | ** So we might as well use bogus content rather than reading |
dan | 1f9144e | 2017-03-17 13:59:06 +0000 | [diff] [blame] | 2647 | ** content from disk. |
| 2648 | ** |
| 2649 | ** Although sqlite3VdbeSerialGet() may read at most 8 bytes from the |
| 2650 | ** buffer passed to it, debugging function VdbeMemPrettyPrint() may |
| 2651 | ** read up to 16. So 16 bytes of bogus content is supplied. |
| 2652 | */ |
| 2653 | static u8 aZero[16]; /* This is the bogus content */ |
drh | 69f6e25 | 2016-01-11 18:05:00 +0000 | [diff] [blame] | 2654 | sqlite3VdbeSerialGet(aZero, t, pDest); |
danielk1977 | aee18ef | 2005-03-09 12:26:50 +0000 | [diff] [blame] | 2655 | }else{ |
drh | cb3cabd | 2016-11-25 19:18:28 +0000 | [diff] [blame] | 2656 | rc = sqlite3VdbeMemFromBtree(pC->uc.pCursor, aOffset[p2], len, pDest); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 2657 | if( rc!=SQLITE_OK ) goto abort_due_to_error; |
| 2658 | sqlite3VdbeSerialGet((const u8*)pDest->z, t, pDest); |
| 2659 | pDest->flags &= ~MEM_Ephem; |
danielk1977 | aee18ef | 2005-03-09 12:26:50 +0000 | [diff] [blame] | 2660 | } |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 2661 | } |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 2662 | |
danielk1977 | 3c9cc8d | 2005-01-17 03:40:08 +0000 | [diff] [blame] | 2663 | op_column_out: |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 2664 | UPDATE_MAX_BLOBSIZE(pDest); |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 2665 | REGISTER_TRACE(pOp->p3, pDest); |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2666 | break; |
drh | 74588ce | 2017-09-13 00:13:05 +0000 | [diff] [blame] | 2667 | |
| 2668 | op_column_corrupt: |
| 2669 | if( aOp[0].p3>0 ){ |
| 2670 | pOp = &aOp[aOp[0].p3-1]; |
| 2671 | break; |
| 2672 | }else{ |
| 2673 | rc = SQLITE_CORRUPT_BKPT; |
| 2674 | goto abort_due_to_error; |
| 2675 | } |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2676 | } |
| 2677 | |
danielk1977 | 751de56 | 2008-04-18 09:01:15 +0000 | [diff] [blame] | 2678 | /* Opcode: Affinity P1 P2 * P4 * |
drh | f63552b | 2013-10-30 00:25:03 +0000 | [diff] [blame] | 2679 | ** Synopsis: affinity(r[P1@P2]) |
danielk1977 | 751de56 | 2008-04-18 09:01:15 +0000 | [diff] [blame] | 2680 | ** |
| 2681 | ** Apply affinities to a range of P2 registers starting with P1. |
| 2682 | ** |
drh | bb6783b | 2017-04-29 18:02:49 +0000 | [diff] [blame] | 2683 | ** P4 is a string that is P2 characters long. The N-th character of the |
| 2684 | ** string indicates the column affinity that should be used for the N-th |
danielk1977 | 751de56 | 2008-04-18 09:01:15 +0000 | [diff] [blame] | 2685 | ** memory cell in the range. |
| 2686 | */ |
| 2687 | case OP_Affinity: { |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 2688 | const char *zAffinity; /* The affinity to be applied */ |
danielk1977 | 751de56 | 2008-04-18 09:01:15 +0000 | [diff] [blame] | 2689 | |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2690 | zAffinity = pOp->p4.z; |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 2691 | assert( zAffinity!=0 ); |
drh | 662c50e | 2017-04-01 20:14:01 +0000 | [diff] [blame] | 2692 | assert( pOp->p2>0 ); |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 2693 | assert( zAffinity[pOp->p2]==0 ); |
| 2694 | pIn1 = &aMem[pOp->p1]; |
drh | 662c50e | 2017-04-01 20:14:01 +0000 | [diff] [blame] | 2695 | do{ |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 2696 | assert( pIn1 <= &p->aMem[(p->nMem+1 - p->nCursor)] ); |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 2697 | assert( memIsValid(pIn1) ); |
drh | 662c50e | 2017-04-01 20:14:01 +0000 | [diff] [blame] | 2698 | applyAffinity(pIn1, *(zAffinity++), encoding); |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 2699 | pIn1++; |
drh | 662c50e | 2017-04-01 20:14:01 +0000 | [diff] [blame] | 2700 | }while( zAffinity[0] ); |
danielk1977 | 751de56 | 2008-04-18 09:01:15 +0000 | [diff] [blame] | 2701 | break; |
| 2702 | } |
| 2703 | |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2704 | /* Opcode: MakeRecord P1 P2 P3 P4 * |
drh | f63552b | 2013-10-30 00:25:03 +0000 | [diff] [blame] | 2705 | ** Synopsis: r[P3]=mkrec(r[P1@P2]) |
drh | 7a224de | 2004-06-02 01:22:02 +0000 | [diff] [blame] | 2706 | ** |
drh | 710c484 | 2010-08-30 01:17:20 +0000 | [diff] [blame] | 2707 | ** Convert P2 registers beginning with P1 into the [record format] |
| 2708 | ** use as a data record in a database table or as a key |
| 2709 | ** in an index. The OP_Column opcode can decode the record later. |
drh | 7a224de | 2004-06-02 01:22:02 +0000 | [diff] [blame] | 2710 | ** |
drh | bb6783b | 2017-04-29 18:02:49 +0000 | [diff] [blame] | 2711 | ** P4 may be a string that is P2 characters long. The N-th character of the |
| 2712 | ** string indicates the column affinity that should be used for the N-th |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2713 | ** field of the index key. |
drh | 7a224de | 2004-06-02 01:22:02 +0000 | [diff] [blame] | 2714 | ** |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 2715 | ** The mapping from character to affinity is given by the SQLITE_AFF_ |
| 2716 | ** macros defined in sqliteInt.h. |
drh | 7a224de | 2004-06-02 01:22:02 +0000 | [diff] [blame] | 2717 | ** |
drh | 05883a3 | 2015-06-02 15:32:08 +0000 | [diff] [blame] | 2718 | ** If P4 is NULL then all index fields have the affinity BLOB. |
drh | 7f057c9 | 2005-06-24 03:53:06 +0000 | [diff] [blame] | 2719 | */ |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2720 | case OP_MakeRecord: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2721 | u8 *zNewRecord; /* A buffer to hold the data for the new record */ |
| 2722 | Mem *pRec; /* The new record */ |
| 2723 | u64 nData; /* Number of bytes of data space */ |
| 2724 | int nHdr; /* Number of bytes of header space */ |
| 2725 | i64 nByte; /* Data space required for this record */ |
drh | 4a33507 | 2015-04-11 02:08:48 +0000 | [diff] [blame] | 2726 | i64 nZero; /* Number of zero bytes at the end of the record */ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2727 | int nVarint; /* Number of bytes in a varint */ |
| 2728 | u32 serial_type; /* Type field */ |
| 2729 | Mem *pData0; /* First field to be combined into the record */ |
| 2730 | Mem *pLast; /* Last field of the record */ |
| 2731 | int nField; /* Number of fields in the record */ |
| 2732 | char *zAffinity; /* The affinity string for the record */ |
| 2733 | int file_format; /* File format to use for encoding */ |
drh | 59bf00c | 2013-12-08 23:33:28 +0000 | [diff] [blame] | 2734 | int i; /* Space used in zNewRecord[] header */ |
| 2735 | int j; /* Space used in zNewRecord[] content */ |
drh | be37c12 | 2015-10-16 14:54:17 +0000 | [diff] [blame] | 2736 | u32 len; /* Length of a field */ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2737 | |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 2738 | /* Assuming the record contains N fields, the record format looks |
| 2739 | ** like this: |
| 2740 | ** |
drh | 7a224de | 2004-06-02 01:22:02 +0000 | [diff] [blame] | 2741 | ** ------------------------------------------------------------------------ |
| 2742 | ** | hdr-size | type 0 | type 1 | ... | type N-1 | data0 | ... | data N-1 | |
| 2743 | ** ------------------------------------------------------------------------ |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 2744 | ** |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2745 | ** 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] | 2746 | ** and so forth. |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 2747 | ** |
| 2748 | ** Each type field is a varint representing the serial type of the |
| 2749 | ** corresponding data element (see sqlite3VdbeSerialType()). The |
drh | 7a224de | 2004-06-02 01:22:02 +0000 | [diff] [blame] | 2750 | ** hdr-size field is also a varint which is the offset from the beginning |
| 2751 | ** of the record to data0. |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 2752 | */ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2753 | nData = 0; /* Number of bytes of data space */ |
| 2754 | nHdr = 0; /* Number of bytes of header space */ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2755 | nZero = 0; /* Number of zero bytes at the end of the record */ |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2756 | nField = pOp->p1; |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 2757 | zAffinity = pOp->p4.z; |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 2758 | 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] | 2759 | pData0 = &aMem[nField]; |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2760 | nField = pOp->p2; |
| 2761 | pLast = &pData0[nField-1]; |
drh | d946db0 | 2005-12-29 19:23:06 +0000 | [diff] [blame] | 2762 | file_format = p->minWriteFileFormat; |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 2763 | |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 2764 | /* Identify the output register */ |
| 2765 | assert( pOp->p3<pOp->p1 || pOp->p3>=pOp->p1+pOp->p2 ); |
| 2766 | pOut = &aMem[pOp->p3]; |
| 2767 | memAboutToChange(p, pOut); |
| 2768 | |
drh | 3e6c060 | 2013-12-10 20:53:01 +0000 | [diff] [blame] | 2769 | /* Apply the requested affinity to all inputs |
| 2770 | */ |
| 2771 | assert( pData0<=pLast ); |
| 2772 | if( zAffinity ){ |
| 2773 | pRec = pData0; |
| 2774 | do{ |
drh | 57bf4a8 | 2014-02-17 14:59:22 +0000 | [diff] [blame] | 2775 | applyAffinity(pRec++, *(zAffinity++), encoding); |
| 2776 | assert( zAffinity[0]==0 || pRec<=pLast ); |
| 2777 | }while( zAffinity[0] ); |
drh | 3e6c060 | 2013-12-10 20:53:01 +0000 | [diff] [blame] | 2778 | } |
| 2779 | |
drh | d447dce | 2017-01-25 20:55:11 +0000 | [diff] [blame] | 2780 | #ifdef SQLITE_ENABLE_NULL_TRIM |
drh | 585ce19 | 2017-01-25 14:58:27 +0000 | [diff] [blame] | 2781 | /* NULLs can be safely trimmed from the end of the record, as long as |
| 2782 | ** as the schema format is 2 or more and none of the omitted columns |
| 2783 | ** have a non-NULL default value. Also, the record must be left with |
| 2784 | ** at least one field. If P5>0 then it will be one more than the |
| 2785 | ** index of the right-most column with a non-NULL default value */ |
| 2786 | if( pOp->p5 ){ |
| 2787 | while( (pLast->flags & MEM_Null)!=0 && nField>pOp->p5 ){ |
| 2788 | pLast--; |
| 2789 | nField--; |
| 2790 | } |
| 2791 | } |
drh | d447dce | 2017-01-25 20:55:11 +0000 | [diff] [blame] | 2792 | #endif |
drh | 585ce19 | 2017-01-25 14:58:27 +0000 | [diff] [blame] | 2793 | |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 2794 | /* Loop through the elements that will make up the record to figure |
| 2795 | ** out how much space is required for the new record. |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 2796 | */ |
drh | 038b7bc | 2013-12-09 23:17:22 +0000 | [diff] [blame] | 2797 | pRec = pLast; |
drh | 59bf00c | 2013-12-08 23:33:28 +0000 | [diff] [blame] | 2798 | do{ |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 2799 | assert( memIsValid(pRec) ); |
drh | 41fb367 | 2018-01-12 23:18:38 +0000 | [diff] [blame] | 2800 | serial_type = sqlite3VdbeSerialType(pRec, file_format, &len); |
drh | fdf972a | 2007-05-02 13:30:27 +0000 | [diff] [blame] | 2801 | if( pRec->flags & MEM_Zero ){ |
drh | ce2fbd1 | 2018-01-12 21:00:14 +0000 | [diff] [blame] | 2802 | if( serial_type==0 ){ |
drh | 41fb367 | 2018-01-12 23:18:38 +0000 | [diff] [blame] | 2803 | /* Values with MEM_Null and MEM_Zero are created by xColumn virtual |
| 2804 | ** table methods that never invoke sqlite3_result_xxxxx() while |
| 2805 | ** computing an unchanging column value in an UPDATE statement. |
| 2806 | ** Give such values a special internal-use-only serial-type of 10 |
| 2807 | ** so that they can be passed through to xUpdate and have |
| 2808 | ** a true sqlite3_value_nochange(). */ |
| 2809 | assert( pOp->p5==OPFLAG_NOCHNG_MAGIC || CORRUPT_DB ); |
| 2810 | serial_type = 10; |
drh | ce2fbd1 | 2018-01-12 21:00:14 +0000 | [diff] [blame] | 2811 | }else if( nData ){ |
drh | 53e66c3 | 2015-07-24 15:49:23 +0000 | [diff] [blame] | 2812 | if( sqlite3VdbeMemExpandBlob(pRec) ) goto no_mem; |
drh | 038b7bc | 2013-12-09 23:17:22 +0000 | [diff] [blame] | 2813 | }else{ |
| 2814 | nZero += pRec->u.nZero; |
| 2815 | len -= pRec->u.nZero; |
| 2816 | } |
drh | fdf972a | 2007-05-02 13:30:27 +0000 | [diff] [blame] | 2817 | } |
drh | 8079a0d | 2006-01-12 17:20:50 +0000 | [diff] [blame] | 2818 | nData += len; |
drh | 59bf00c | 2013-12-08 23:33:28 +0000 | [diff] [blame] | 2819 | testcase( serial_type==127 ); |
| 2820 | testcase( serial_type==128 ); |
drh | 2a24287 | 2013-12-08 22:59:29 +0000 | [diff] [blame] | 2821 | nHdr += serial_type<=127 ? 1 : sqlite3VarintLen(serial_type); |
drh | 41fb367 | 2018-01-12 23:18:38 +0000 | [diff] [blame] | 2822 | pRec->uTemp = serial_type; |
drh | 45c3c66 | 2016-04-07 14:16:16 +0000 | [diff] [blame] | 2823 | if( pRec==pData0 ) break; |
| 2824 | pRec--; |
| 2825 | }while(1); |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 2826 | |
drh | 654858d | 2014-11-20 02:18:14 +0000 | [diff] [blame] | 2827 | /* EVIDENCE-OF: R-22564-11647 The header begins with a single varint |
| 2828 | ** which determines the total number of bytes in the header. The varint |
| 2829 | ** value is the size of the header in bytes including the size varint |
| 2830 | ** itself. */ |
drh | 59bf00c | 2013-12-08 23:33:28 +0000 | [diff] [blame] | 2831 | testcase( nHdr==126 ); |
| 2832 | testcase( nHdr==127 ); |
drh | 2a24287 | 2013-12-08 22:59:29 +0000 | [diff] [blame] | 2833 | if( nHdr<=126 ){ |
| 2834 | /* The common case */ |
| 2835 | nHdr += 1; |
| 2836 | }else{ |
| 2837 | /* Rare case of a really large header */ |
| 2838 | nVarint = sqlite3VarintLen(nHdr); |
| 2839 | nHdr += nVarint; |
| 2840 | if( nVarint<sqlite3VarintLen(nHdr) ) nHdr++; |
drh | cb9882a | 2005-03-17 03:15:40 +0000 | [diff] [blame] | 2841 | } |
drh | 038b7bc | 2013-12-09 23:17:22 +0000 | [diff] [blame] | 2842 | nByte = nHdr+nData; |
drh | 4a33507 | 2015-04-11 02:08:48 +0000 | [diff] [blame] | 2843 | if( nByte+nZero>db->aLimit[SQLITE_LIMIT_LENGTH] ){ |
drh | 023ae03 | 2007-05-08 12:12:16 +0000 | [diff] [blame] | 2844 | goto too_big; |
| 2845 | } |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 2846 | |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 2847 | /* Make sure the output register has a buffer large enough to store |
| 2848 | ** the new record. The output register (pOp->p3) is not allowed to |
| 2849 | ** be one of the input registers (because the following call to |
drh | 322f285 | 2014-09-19 00:43:39 +0000 | [diff] [blame] | 2850 | ** sqlite3VdbeMemClearAndResize() could clobber the value before it is used). |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 2851 | */ |
drh | 322f285 | 2014-09-19 00:43:39 +0000 | [diff] [blame] | 2852 | if( sqlite3VdbeMemClearAndResize(pOut, (int)nByte) ){ |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 2853 | goto no_mem; |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 2854 | } |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 2855 | zNewRecord = (u8 *)pOut->z; |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 2856 | |
| 2857 | /* Write the record */ |
shane | 3f8d5cf | 2008-04-24 19:15:09 +0000 | [diff] [blame] | 2858 | i = putVarint32(zNewRecord, nHdr); |
drh | 59bf00c | 2013-12-08 23:33:28 +0000 | [diff] [blame] | 2859 | j = nHdr; |
| 2860 | assert( pData0<=pLast ); |
| 2861 | pRec = pData0; |
| 2862 | do{ |
drh | facf47a | 2014-10-13 20:12:47 +0000 | [diff] [blame] | 2863 | serial_type = pRec->uTemp; |
drh | 654858d | 2014-11-20 02:18:14 +0000 | [diff] [blame] | 2864 | /* EVIDENCE-OF: R-06529-47362 Following the size varint are one or more |
| 2865 | ** additional varints, one per column. */ |
drh | 038b7bc | 2013-12-09 23:17:22 +0000 | [diff] [blame] | 2866 | i += putVarint32(&zNewRecord[i], serial_type); /* serial type */ |
drh | 654858d | 2014-11-20 02:18:14 +0000 | [diff] [blame] | 2867 | /* EVIDENCE-OF: R-64536-51728 The values for each column in the record |
| 2868 | ** immediately follow the header. */ |
drh | a9ab481 | 2013-12-11 11:00:44 +0000 | [diff] [blame] | 2869 | j += sqlite3VdbeSerialPut(&zNewRecord[j], pRec, serial_type); /* content */ |
drh | 59bf00c | 2013-12-08 23:33:28 +0000 | [diff] [blame] | 2870 | }while( (++pRec)<=pLast ); |
| 2871 | assert( i==nHdr ); |
| 2872 | assert( j==nByte ); |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 2873 | |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 2874 | assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) ); |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 2875 | pOut->n = (int)nByte; |
drh | c91b2fd | 2014-03-01 18:13:23 +0000 | [diff] [blame] | 2876 | pOut->flags = MEM_Blob; |
drh | fdf972a | 2007-05-02 13:30:27 +0000 | [diff] [blame] | 2877 | if( nZero ){ |
drh | 8df3284 | 2008-12-09 02:51:23 +0000 | [diff] [blame] | 2878 | pOut->u.nZero = nZero; |
drh | 477df4b | 2008-01-05 18:48:24 +0000 | [diff] [blame] | 2879 | pOut->flags |= MEM_Zero; |
drh | fdf972a | 2007-05-02 13:30:27 +0000 | [diff] [blame] | 2880 | } |
drh | 1013c93 | 2008-01-06 00:25:21 +0000 | [diff] [blame] | 2881 | REGISTER_TRACE(pOp->p3, pOut); |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 2882 | UPDATE_MAX_BLOBSIZE(pOut); |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 2883 | break; |
| 2884 | } |
| 2885 | |
danielk1977 | a553316 | 2009-02-24 10:01:51 +0000 | [diff] [blame] | 2886 | /* Opcode: Count P1 P2 * * * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 2887 | ** Synopsis: r[P2]=count() |
danielk1977 | a553316 | 2009-02-24 10:01:51 +0000 | [diff] [blame] | 2888 | ** |
| 2889 | ** Store the number of entries (an integer value) in the table or index |
| 2890 | ** opened by cursor P1 in register P2 |
| 2891 | */ |
| 2892 | #ifndef SQLITE_OMIT_BTREECOUNT |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 2893 | case OP_Count: { /* out2 */ |
danielk1977 | a553316 | 2009-02-24 10:01:51 +0000 | [diff] [blame] | 2894 | i64 nEntry; |
drh | c54a617 | 2009-06-02 16:06:03 +0000 | [diff] [blame] | 2895 | BtCursor *pCrsr; |
| 2896 | |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 2897 | assert( p->apCsr[pOp->p1]->eCurType==CURTYPE_BTREE ); |
| 2898 | pCrsr = p->apCsr[pOp->p1]->uc.pCursor; |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 2899 | assert( pCrsr ); |
drh | 2dc0648 | 2013-12-11 00:59:10 +0000 | [diff] [blame] | 2900 | nEntry = 0; /* Not needed. Only used to silence a warning. */ |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 2901 | rc = sqlite3BtreeCount(pCrsr, &nEntry); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 2902 | if( rc ) goto abort_due_to_error; |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 2903 | pOut = out2Prerelease(p, pOp); |
danielk1977 | a553316 | 2009-02-24 10:01:51 +0000 | [diff] [blame] | 2904 | pOut->u.i = nEntry; |
| 2905 | break; |
| 2906 | } |
| 2907 | #endif |
| 2908 | |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2909 | /* Opcode: Savepoint P1 * * P4 * |
| 2910 | ** |
| 2911 | ** Open, release or rollback the savepoint named by parameter P4, depending |
| 2912 | ** on the value of P1. To open a new savepoint, P1==0. To release (commit) an |
| 2913 | ** existing savepoint, P1==1, or to rollback an existing savepoint P1==2. |
| 2914 | */ |
| 2915 | case OP_Savepoint: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2916 | int p1; /* Value of P1 operand */ |
| 2917 | char *zName; /* Name of savepoint */ |
| 2918 | int nName; |
| 2919 | Savepoint *pNew; |
| 2920 | Savepoint *pSavepoint; |
| 2921 | Savepoint *pTmp; |
| 2922 | int iSavepoint; |
| 2923 | int ii; |
| 2924 | |
| 2925 | p1 = pOp->p1; |
| 2926 | zName = pOp->p4.z; |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2927 | |
| 2928 | /* Assert that the p1 parameter is valid. Also that if there is no open |
| 2929 | ** transaction, then there cannot be any savepoints. |
| 2930 | */ |
| 2931 | assert( db->pSavepoint==0 || db->autoCommit==0 ); |
| 2932 | assert( p1==SAVEPOINT_BEGIN||p1==SAVEPOINT_RELEASE||p1==SAVEPOINT_ROLLBACK ); |
| 2933 | assert( db->pSavepoint || db->isTransactionSavepoint==0 ); |
| 2934 | assert( checkSavepointCount(db) ); |
dan | c0537fe | 2013-06-28 19:41:43 +0000 | [diff] [blame] | 2935 | assert( p->bIsReader ); |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2936 | |
| 2937 | if( p1==SAVEPOINT_BEGIN ){ |
drh | 4f7d3a5 | 2013-06-27 23:54:02 +0000 | [diff] [blame] | 2938 | if( db->nVdbeWrite>0 ){ |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2939 | /* A new savepoint cannot be created if there are active write |
| 2940 | ** statements (i.e. open read/write incremental blob handles). |
| 2941 | */ |
drh | 22c17b8 | 2015-05-15 04:13:15 +0000 | [diff] [blame] | 2942 | sqlite3VdbeError(p, "cannot open savepoint - SQL statements in progress"); |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2943 | rc = SQLITE_BUSY; |
| 2944 | }else{ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2945 | nName = sqlite3Strlen30(zName); |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2946 | |
drh | be07ec5 | 2011-06-03 12:15:26 +0000 | [diff] [blame] | 2947 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
dan | d9495cd | 2011-04-27 12:08:04 +0000 | [diff] [blame] | 2948 | /* This call is Ok even if this savepoint is actually a transaction |
| 2949 | ** savepoint (and therefore should not prompt xSavepoint()) callbacks. |
| 2950 | ** If this is a transaction savepoint being opened, it is guaranteed |
| 2951 | ** that the db->aVTrans[] array is empty. */ |
| 2952 | assert( db->autoCommit==0 || db->nVTrans==0 ); |
drh | a24bc9c | 2011-05-24 00:35:56 +0000 | [diff] [blame] | 2953 | rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN, |
| 2954 | db->nStatement+db->nSavepoint); |
dan | d9495cd | 2011-04-27 12:08:04 +0000 | [diff] [blame] | 2955 | if( rc!=SQLITE_OK ) goto abort_due_to_error; |
drh | 305ebab | 2011-05-26 14:19:14 +0000 | [diff] [blame] | 2956 | #endif |
dan | d9495cd | 2011-04-27 12:08:04 +0000 | [diff] [blame] | 2957 | |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2958 | /* Create a new savepoint structure. */ |
drh | 575fad6 | 2016-02-05 13:38:36 +0000 | [diff] [blame] | 2959 | pNew = sqlite3DbMallocRawNN(db, sizeof(Savepoint)+nName+1); |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2960 | if( pNew ){ |
| 2961 | pNew->zName = (char *)&pNew[1]; |
| 2962 | memcpy(pNew->zName, zName, nName+1); |
| 2963 | |
| 2964 | /* If there is no open transaction, then mark this as a special |
| 2965 | ** "transaction savepoint". */ |
| 2966 | if( db->autoCommit ){ |
| 2967 | db->autoCommit = 0; |
| 2968 | db->isTransactionSavepoint = 1; |
| 2969 | }else{ |
| 2970 | db->nSavepoint++; |
danielk1977 | d829335 | 2009-04-30 09:10:37 +0000 | [diff] [blame] | 2971 | } |
dan | 21e8d01 | 2011-03-03 20:05:59 +0000 | [diff] [blame] | 2972 | |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2973 | /* Link the new savepoint into the database handle's list. */ |
| 2974 | pNew->pNext = db->pSavepoint; |
| 2975 | db->pSavepoint = pNew; |
dan | ba9108b | 2009-09-22 07:13:42 +0000 | [diff] [blame] | 2976 | pNew->nDeferredCons = db->nDeferredCons; |
dan | cb3e4b7 | 2013-07-03 19:53:05 +0000 | [diff] [blame] | 2977 | pNew->nDeferredImmCons = db->nDeferredImmCons; |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2978 | } |
| 2979 | } |
| 2980 | }else{ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2981 | iSavepoint = 0; |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2982 | |
| 2983 | /* Find the named savepoint. If there is no such savepoint, then an |
| 2984 | ** an error is returned to the user. */ |
| 2985 | for( |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2986 | pSavepoint = db->pSavepoint; |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2987 | pSavepoint && sqlite3StrICmp(pSavepoint->zName, zName); |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2988 | pSavepoint = pSavepoint->pNext |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2989 | ){ |
| 2990 | iSavepoint++; |
| 2991 | } |
| 2992 | if( !pSavepoint ){ |
drh | 22c17b8 | 2015-05-15 04:13:15 +0000 | [diff] [blame] | 2993 | sqlite3VdbeError(p, "no such savepoint: %s", zName); |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2994 | rc = SQLITE_ERROR; |
drh | 4f7d3a5 | 2013-06-27 23:54:02 +0000 | [diff] [blame] | 2995 | }else if( db->nVdbeWrite>0 && p1==SAVEPOINT_RELEASE ){ |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2996 | /* It is not possible to release (commit) a savepoint if there are |
drh | 0f198a7 | 2012-02-13 16:43:16 +0000 | [diff] [blame] | 2997 | ** active write statements. |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2998 | */ |
drh | 22c17b8 | 2015-05-15 04:13:15 +0000 | [diff] [blame] | 2999 | sqlite3VdbeError(p, "cannot release savepoint - " |
| 3000 | "SQL statements in progress"); |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3001 | rc = SQLITE_BUSY; |
| 3002 | }else{ |
| 3003 | |
| 3004 | /* Determine whether or not this is a transaction savepoint. If so, |
danielk1977 | 34cf35d | 2008-12-18 18:31:38 +0000 | [diff] [blame] | 3005 | ** and this is a RELEASE command, then the current transaction |
| 3006 | ** is committed. |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3007 | */ |
| 3008 | int isTransaction = pSavepoint->pNext==0 && db->isTransactionSavepoint; |
| 3009 | if( isTransaction && p1==SAVEPOINT_RELEASE ){ |
dan | 32b09f2 | 2009-09-23 17:29:59 +0000 | [diff] [blame] | 3010 | if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){ |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 3011 | goto vdbe_return; |
| 3012 | } |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3013 | db->autoCommit = 1; |
| 3014 | if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){ |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 3015 | p->pc = (int)(pOp - aOp); |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3016 | db->autoCommit = 0; |
| 3017 | p->rc = rc = SQLITE_BUSY; |
| 3018 | goto vdbe_return; |
| 3019 | } |
danielk1977 | 34cf35d | 2008-12-18 18:31:38 +0000 | [diff] [blame] | 3020 | db->isTransactionSavepoint = 0; |
| 3021 | rc = p->rc; |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3022 | }else{ |
drh | 47b7fc7 | 2014-11-11 01:33:57 +0000 | [diff] [blame] | 3023 | int isSchemaChange; |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3024 | iSavepoint = db->nSavepoint - iSavepoint - 1; |
drh | 31f1005 | 2012-03-31 17:17:26 +0000 | [diff] [blame] | 3025 | if( p1==SAVEPOINT_ROLLBACK ){ |
drh | 8257aa8 | 2017-07-26 19:59:13 +0000 | [diff] [blame] | 3026 | isSchemaChange = (db->mDbFlags & DBFLAG_SchemaChange)!=0; |
drh | 31f1005 | 2012-03-31 17:17:26 +0000 | [diff] [blame] | 3027 | for(ii=0; ii<db->nDb; ii++){ |
drh | 77b1dee | 2014-11-17 17:13:06 +0000 | [diff] [blame] | 3028 | rc = sqlite3BtreeTripAllCursors(db->aDb[ii].pBt, |
| 3029 | SQLITE_ABORT_ROLLBACK, |
drh | 47b7fc7 | 2014-11-11 01:33:57 +0000 | [diff] [blame] | 3030 | isSchemaChange==0); |
dan | 8023104 | 2014-11-12 14:56:02 +0000 | [diff] [blame] | 3031 | if( rc!=SQLITE_OK ) goto abort_due_to_error; |
drh | 31f1005 | 2012-03-31 17:17:26 +0000 | [diff] [blame] | 3032 | } |
drh | 47b7fc7 | 2014-11-11 01:33:57 +0000 | [diff] [blame] | 3033 | }else{ |
| 3034 | isSchemaChange = 0; |
drh | 0f198a7 | 2012-02-13 16:43:16 +0000 | [diff] [blame] | 3035 | } |
| 3036 | for(ii=0; ii<db->nDb; ii++){ |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3037 | rc = sqlite3BtreeSavepoint(db->aDb[ii].pBt, p1, iSavepoint); |
| 3038 | if( rc!=SQLITE_OK ){ |
| 3039 | goto abort_due_to_error; |
danielk1977 | bd43455 | 2009-03-18 10:33:00 +0000 | [diff] [blame] | 3040 | } |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3041 | } |
drh | 47b7fc7 | 2014-11-11 01:33:57 +0000 | [diff] [blame] | 3042 | if( isSchemaChange ){ |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3043 | sqlite3ExpirePreparedStatements(db); |
drh | 81028a4 | 2012-05-15 18:28:27 +0000 | [diff] [blame] | 3044 | sqlite3ResetAllSchemasOfConnection(db); |
drh | 8257aa8 | 2017-07-26 19:59:13 +0000 | [diff] [blame] | 3045 | db->mDbFlags |= DBFLAG_SchemaChange; |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3046 | } |
| 3047 | } |
| 3048 | |
| 3049 | /* Regardless of whether this is a RELEASE or ROLLBACK, destroy all |
| 3050 | ** savepoints nested inside of the savepoint being operated on. */ |
| 3051 | while( db->pSavepoint!=pSavepoint ){ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3052 | pTmp = db->pSavepoint; |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3053 | db->pSavepoint = pTmp->pNext; |
| 3054 | sqlite3DbFree(db, pTmp); |
| 3055 | db->nSavepoint--; |
| 3056 | } |
| 3057 | |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 3058 | /* If it is a RELEASE, then destroy the savepoint being operated on |
| 3059 | ** too. If it is a ROLLBACK TO, then set the number of deferred |
| 3060 | ** constraint violations present in the database to the value stored |
| 3061 | ** when the savepoint was created. */ |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3062 | if( p1==SAVEPOINT_RELEASE ){ |
| 3063 | assert( pSavepoint==db->pSavepoint ); |
| 3064 | db->pSavepoint = pSavepoint->pNext; |
| 3065 | sqlite3DbFree(db, pSavepoint); |
| 3066 | if( !isTransaction ){ |
| 3067 | db->nSavepoint--; |
| 3068 | } |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 3069 | }else{ |
| 3070 | db->nDeferredCons = pSavepoint->nDeferredCons; |
dan | cb3e4b7 | 2013-07-03 19:53:05 +0000 | [diff] [blame] | 3071 | db->nDeferredImmCons = pSavepoint->nDeferredImmCons; |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3072 | } |
dan | d9495cd | 2011-04-27 12:08:04 +0000 | [diff] [blame] | 3073 | |
dan | ea8562e | 2015-04-18 16:25:54 +0000 | [diff] [blame] | 3074 | if( !isTransaction || p1==SAVEPOINT_ROLLBACK ){ |
dan | d9495cd | 2011-04-27 12:08:04 +0000 | [diff] [blame] | 3075 | rc = sqlite3VtabSavepoint(db, p1, iSavepoint); |
| 3076 | if( rc!=SQLITE_OK ) goto abort_due_to_error; |
| 3077 | } |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3078 | } |
| 3079 | } |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 3080 | if( rc ) goto abort_due_to_error; |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3081 | |
| 3082 | break; |
| 3083 | } |
| 3084 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 3085 | /* Opcode: AutoCommit P1 P2 * * * |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 3086 | ** |
| 3087 | ** 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] | 3088 | ** back any currently active btree transactions. If there are any active |
drh | c25eabe | 2009-02-24 18:57:31 +0000 | [diff] [blame] | 3089 | ** VMs (apart from this one), then a ROLLBACK fails. A COMMIT fails if |
| 3090 | ** there are active writing VMs or active VMs that use shared cache. |
drh | 92f02c3 | 2004-09-02 14:57:08 +0000 | [diff] [blame] | 3091 | ** |
| 3092 | ** This instruction causes the VM to halt. |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 3093 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3094 | case OP_AutoCommit: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3095 | int desiredAutoCommit; |
shane | 68c0273 | 2009-06-09 18:14:18 +0000 | [diff] [blame] | 3096 | int iRollback; |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 3097 | |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3098 | desiredAutoCommit = pOp->p1; |
shane | 68c0273 | 2009-06-09 18:14:18 +0000 | [diff] [blame] | 3099 | iRollback = pOp->p2; |
drh | ad4a4b8 | 2008-11-05 16:37:34 +0000 | [diff] [blame] | 3100 | assert( desiredAutoCommit==1 || desiredAutoCommit==0 ); |
shane | 68c0273 | 2009-06-09 18:14:18 +0000 | [diff] [blame] | 3101 | assert( desiredAutoCommit==1 || iRollback==0 ); |
drh | 4f7d3a5 | 2013-06-27 23:54:02 +0000 | [diff] [blame] | 3102 | assert( db->nVdbeActive>0 ); /* At least this one VM is active */ |
dan | c0537fe | 2013-06-28 19:41:43 +0000 | [diff] [blame] | 3103 | assert( p->bIsReader ); |
danielk1977 | 46c43ed | 2004-06-30 06:30:25 +0000 | [diff] [blame] | 3104 | |
drh | b0c8865 | 2016-02-01 13:21:13 +0000 | [diff] [blame] | 3105 | if( desiredAutoCommit!=db->autoCommit ){ |
shane | 68c0273 | 2009-06-09 18:14:18 +0000 | [diff] [blame] | 3106 | if( iRollback ){ |
drh | ad4a4b8 | 2008-11-05 16:37:34 +0000 | [diff] [blame] | 3107 | assert( desiredAutoCommit==1 ); |
drh | 21021a5 | 2012-02-13 17:01:51 +0000 | [diff] [blame] | 3108 | sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK); |
danielk1977 | f3f06bb | 2005-12-16 15:24:28 +0000 | [diff] [blame] | 3109 | db->autoCommit = 1; |
drh | b0c8865 | 2016-02-01 13:21:13 +0000 | [diff] [blame] | 3110 | }else if( desiredAutoCommit && db->nVdbeWrite>0 ){ |
| 3111 | /* If this instruction implements a COMMIT and other VMs are writing |
| 3112 | ** return an error indicating that the other VMs must complete first. |
| 3113 | */ |
| 3114 | sqlite3VdbeError(p, "cannot commit transaction - " |
| 3115 | "SQL statements in progress"); |
| 3116 | rc = SQLITE_BUSY; |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 3117 | goto abort_due_to_error; |
dan | 32b09f2 | 2009-09-23 17:29:59 +0000 | [diff] [blame] | 3118 | }else if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){ |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 3119 | goto vdbe_return; |
danielk1977 | f3f06bb | 2005-12-16 15:24:28 +0000 | [diff] [blame] | 3120 | }else{ |
shane | 7d3846a | 2008-12-11 02:58:26 +0000 | [diff] [blame] | 3121 | db->autoCommit = (u8)desiredAutoCommit; |
drh | 8ff2587 | 2015-07-31 18:59:56 +0000 | [diff] [blame] | 3122 | } |
| 3123 | if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){ |
| 3124 | p->pc = (int)(pOp - aOp); |
| 3125 | db->autoCommit = (u8)(1-desiredAutoCommit); |
| 3126 | p->rc = rc = SQLITE_BUSY; |
| 3127 | goto vdbe_return; |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 3128 | } |
danielk1977 | bd43455 | 2009-03-18 10:33:00 +0000 | [diff] [blame] | 3129 | assert( db->nStatement==0 ); |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3130 | sqlite3CloseSavepoints(db); |
drh | 83968c4 | 2007-04-18 16:45:24 +0000 | [diff] [blame] | 3131 | if( p->rc==SQLITE_OK ){ |
drh | 900b31e | 2007-08-28 02:27:51 +0000 | [diff] [blame] | 3132 | rc = SQLITE_DONE; |
drh | 83968c4 | 2007-04-18 16:45:24 +0000 | [diff] [blame] | 3133 | }else{ |
drh | 900b31e | 2007-08-28 02:27:51 +0000 | [diff] [blame] | 3134 | rc = SQLITE_ERROR; |
drh | 83968c4 | 2007-04-18 16:45:24 +0000 | [diff] [blame] | 3135 | } |
drh | 900b31e | 2007-08-28 02:27:51 +0000 | [diff] [blame] | 3136 | goto vdbe_return; |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 3137 | }else{ |
drh | 22c17b8 | 2015-05-15 04:13:15 +0000 | [diff] [blame] | 3138 | sqlite3VdbeError(p, |
drh | ad4a4b8 | 2008-11-05 16:37:34 +0000 | [diff] [blame] | 3139 | (!desiredAutoCommit)?"cannot start a transaction within a transaction":( |
shane | 68c0273 | 2009-06-09 18:14:18 +0000 | [diff] [blame] | 3140 | (iRollback)?"cannot rollback - no transaction is active": |
drh | f089aa4 | 2008-07-08 19:34:06 +0000 | [diff] [blame] | 3141 | "cannot commit - no transaction is active")); |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 3142 | |
| 3143 | rc = SQLITE_ERROR; |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 3144 | goto abort_due_to_error; |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 3145 | } |
| 3146 | break; |
| 3147 | } |
| 3148 | |
drh | b22f7c8 | 2014-02-06 23:56:27 +0000 | [diff] [blame] | 3149 | /* Opcode: Transaction P1 P2 P3 P4 P5 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3150 | ** |
drh | 05a86c5 | 2014-02-16 01:55:49 +0000 | [diff] [blame] | 3151 | ** Begin a transaction on database P1 if a transaction is not already |
| 3152 | ** active. |
| 3153 | ** If P2 is non-zero, then a write-transaction is started, or if a |
| 3154 | ** read-transaction is already active, it is upgraded to a write-transaction. |
| 3155 | ** If P2 is zero, then a read-transaction is started. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3156 | ** |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 3157 | ** P1 is the index of the database file on which the transaction is |
| 3158 | ** started. Index 0 is the main database file and index 1 is the |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 3159 | ** file used for temporary tables. Indices of 2 or more are used for |
| 3160 | ** attached databases. |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 3161 | ** |
dan | e0af83a | 2009-09-08 19:15:01 +0000 | [diff] [blame] | 3162 | ** If a write-transaction is started and the Vdbe.usesStmtJournal flag is |
| 3163 | ** true (this flag is set if the Vdbe may modify more than one row and may |
| 3164 | ** throw an ABORT exception), a statement transaction may also be opened. |
| 3165 | ** More specifically, a statement transaction is opened iff the database |
| 3166 | ** connection is currently not in autocommit mode, or if there are other |
drh | a451017 | 2012-02-02 15:50:17 +0000 | [diff] [blame] | 3167 | ** active statements. A statement transaction allows the changes made by this |
dan | e0af83a | 2009-09-08 19:15:01 +0000 | [diff] [blame] | 3168 | ** VDBE to be rolled back after an error without having to roll back the |
| 3169 | ** entire transaction. If no error is encountered, the statement transaction |
| 3170 | ** will automatically commit when the VDBE halts. |
| 3171 | ** |
drh | b22f7c8 | 2014-02-06 23:56:27 +0000 | [diff] [blame] | 3172 | ** If P5!=0 then this opcode also checks the schema cookie against P3 |
| 3173 | ** and the schema generation counter against P4. |
| 3174 | ** The cookie changes its value whenever the database schema changes. |
| 3175 | ** This operation is used to detect when that the cookie has changed |
drh | 05a86c5 | 2014-02-16 01:55:49 +0000 | [diff] [blame] | 3176 | ** and that the current process needs to reread the schema. If the schema |
| 3177 | ** cookie in P3 differs from the schema cookie in the database header or |
| 3178 | ** if the schema generation counter in P4 differs from the current |
| 3179 | ** generation counter, then an SQLITE_SCHEMA error is raised and execution |
| 3180 | ** halts. The sqlite3_step() wrapper function might then reprepare the |
| 3181 | ** statement and rerun it from the beginning. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3182 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3183 | case OP_Transaction: { |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 3184 | Btree *pBt; |
drh | b22f7c8 | 2014-02-06 23:56:27 +0000 | [diff] [blame] | 3185 | int iMeta; |
| 3186 | int iGen; |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 3187 | |
drh | 1713afb | 2013-06-28 01:24:57 +0000 | [diff] [blame] | 3188 | assert( p->bIsReader ); |
drh | 9e92a47 | 2013-06-27 17:40:30 +0000 | [diff] [blame] | 3189 | assert( p->readOnly==0 || pOp->p2==0 ); |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 3190 | assert( pOp->p1>=0 && pOp->p1<db->nDb ); |
drh | a7ab6d8 | 2014-07-21 15:44:39 +0000 | [diff] [blame] | 3191 | assert( DbMaskTest(p->btreeMask, pOp->p1) ); |
drh | 13447bf | 2013-07-10 13:33:49 +0000 | [diff] [blame] | 3192 | if( pOp->p2 && (db->flags & SQLITE_QueryOnly)!=0 ){ |
| 3193 | rc = SQLITE_READONLY; |
| 3194 | goto abort_due_to_error; |
| 3195 | } |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 3196 | pBt = db->aDb[pOp->p1].pBt; |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 3197 | |
danielk1977 | 24162fe | 2004-06-04 06:22:00 +0000 | [diff] [blame] | 3198 | if( pBt ){ |
danielk1977 | 40b38dc | 2004-06-26 08:38:24 +0000 | [diff] [blame] | 3199 | rc = sqlite3BtreeBeginTrans(pBt, pOp->p2); |
drh | cbd8db3 | 2015-08-20 17:18:32 +0000 | [diff] [blame] | 3200 | testcase( rc==SQLITE_BUSY_SNAPSHOT ); |
| 3201 | testcase( rc==SQLITE_BUSY_RECOVERY ); |
drh | 9e9f1bd | 2009-10-13 15:36:51 +0000 | [diff] [blame] | 3202 | if( rc!=SQLITE_OK ){ |
drh | fadd2b1 | 2016-09-19 23:39:34 +0000 | [diff] [blame] | 3203 | if( (rc&0xff)==SQLITE_BUSY ){ |
| 3204 | p->pc = (int)(pOp - aOp); |
| 3205 | p->rc = rc; |
| 3206 | goto vdbe_return; |
| 3207 | } |
danielk1977 | 24162fe | 2004-06-04 06:22:00 +0000 | [diff] [blame] | 3208 | goto abort_due_to_error; |
drh | 90bfcda | 2001-09-23 19:46:51 +0000 | [diff] [blame] | 3209 | } |
dan | e0af83a | 2009-09-08 19:15:01 +0000 | [diff] [blame] | 3210 | |
| 3211 | if( pOp->p2 && p->usesStmtJournal |
dan | c0537fe | 2013-06-28 19:41:43 +0000 | [diff] [blame] | 3212 | && (db->autoCommit==0 || db->nVdbeRead>1) |
dan | e0af83a | 2009-09-08 19:15:01 +0000 | [diff] [blame] | 3213 | ){ |
| 3214 | assert( sqlite3BtreeIsInTrans(pBt) ); |
| 3215 | if( p->iStatement==0 ){ |
| 3216 | assert( db->nStatement>=0 && db->nSavepoint>=0 ); |
| 3217 | db->nStatement++; |
| 3218 | p->iStatement = db->nSavepoint + db->nStatement; |
| 3219 | } |
dan | a311b80 | 2011-04-26 19:21:34 +0000 | [diff] [blame] | 3220 | |
drh | 346506f | 2011-05-25 01:16:42 +0000 | [diff] [blame] | 3221 | rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN, p->iStatement-1); |
dan | a311b80 | 2011-04-26 19:21:34 +0000 | [diff] [blame] | 3222 | if( rc==SQLITE_OK ){ |
| 3223 | rc = sqlite3BtreeBeginStmt(pBt, p->iStatement); |
| 3224 | } |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 3225 | |
| 3226 | /* Store the current value of the database handles deferred constraint |
| 3227 | ** counter. If the statement transaction needs to be rolled back, |
| 3228 | ** the value of this counter needs to be restored too. */ |
| 3229 | p->nStmtDefCons = db->nDeferredCons; |
dan | cb3e4b7 | 2013-07-03 19:53:05 +0000 | [diff] [blame] | 3230 | p->nStmtDefImmCons = db->nDeferredImmCons; |
dan | e0af83a | 2009-09-08 19:15:01 +0000 | [diff] [blame] | 3231 | } |
drh | b22f7c8 | 2014-02-06 23:56:27 +0000 | [diff] [blame] | 3232 | |
drh | 51a74d4 | 2015-02-28 01:04:27 +0000 | [diff] [blame] | 3233 | /* Gather the schema version number for checking: |
drh | 96fdcb4 | 2016-09-27 00:09:33 +0000 | [diff] [blame] | 3234 | ** IMPLEMENTATION-OF: R-03189-51135 As each SQL statement runs, the schema |
| 3235 | ** version is checked to ensure that the schema has not changed since the |
| 3236 | ** SQL statement was prepared. |
drh | 51a74d4 | 2015-02-28 01:04:27 +0000 | [diff] [blame] | 3237 | */ |
drh | b22f7c8 | 2014-02-06 23:56:27 +0000 | [diff] [blame] | 3238 | sqlite3BtreeGetMeta(pBt, BTREE_SCHEMA_VERSION, (u32 *)&iMeta); |
| 3239 | iGen = db->aDb[pOp->p1].pSchema->iGeneration; |
| 3240 | }else{ |
| 3241 | iGen = iMeta = 0; |
| 3242 | } |
| 3243 | assert( pOp->p5==0 || pOp->p4type==P4_INT32 ); |
| 3244 | if( pOp->p5 && (iMeta!=pOp->p3 || iGen!=pOp->p4.i) ){ |
| 3245 | sqlite3DbFree(db, p->zErrMsg); |
| 3246 | p->zErrMsg = sqlite3DbStrDup(db, "database schema has changed"); |
| 3247 | /* If the schema-cookie from the database file matches the cookie |
| 3248 | ** stored with the in-memory representation of the schema, do |
| 3249 | ** not reload the schema from the database file. |
| 3250 | ** |
| 3251 | ** If virtual-tables are in use, this is not just an optimization. |
| 3252 | ** Often, v-tables store their data in other SQLite tables, which |
| 3253 | ** are queried from within xNext() and other v-table methods using |
| 3254 | ** prepared queries. If such a query is out-of-date, we do not want to |
| 3255 | ** discard the database schema, as the user code implementing the |
| 3256 | ** v-table would have to be ready for the sqlite3_vtab structure itself |
| 3257 | ** to be invalidated whenever sqlite3_step() is called from within |
| 3258 | ** a v-table method. |
| 3259 | */ |
| 3260 | if( db->aDb[pOp->p1].pSchema->schema_cookie!=iMeta ){ |
| 3261 | sqlite3ResetOneSchema(db, pOp->p1); |
| 3262 | } |
| 3263 | p->expired = 1; |
| 3264 | rc = SQLITE_SCHEMA; |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 3265 | } |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 3266 | if( rc ) goto abort_due_to_error; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3267 | break; |
| 3268 | } |
| 3269 | |
drh | b1fdb2a | 2008-01-05 04:06:03 +0000 | [diff] [blame] | 3270 | /* Opcode: ReadCookie P1 P2 P3 * * |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 3271 | ** |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3272 | ** Read cookie number P3 from database P1 and write it into register P2. |
danielk1977 | 0d19f7a | 2009-06-03 11:25:07 +0000 | [diff] [blame] | 3273 | ** P3==1 is the schema version. P3==2 is the database format. |
| 3274 | ** 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] | 3275 | ** the main database file and P1==1 is the database file used to store |
| 3276 | ** temporary tables. |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 3277 | ** |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 3278 | ** There must be a read-lock on the database (either a transaction |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 3279 | ** must be started or there must be an open cursor) before |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 3280 | ** executing this instruction. |
| 3281 | */ |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 3282 | case OP_ReadCookie: { /* out2 */ |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 3283 | int iMeta; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3284 | int iDb; |
| 3285 | int iCookie; |
danielk1977 | 180b56a | 2007-06-24 08:00:42 +0000 | [diff] [blame] | 3286 | |
drh | 1713afb | 2013-06-28 01:24:57 +0000 | [diff] [blame] | 3287 | assert( p->bIsReader ); |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3288 | iDb = pOp->p1; |
| 3289 | iCookie = pOp->p3; |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 3290 | assert( pOp->p3<SQLITE_N_BTREE_META ); |
danielk1977 | 180b56a | 2007-06-24 08:00:42 +0000 | [diff] [blame] | 3291 | assert( iDb>=0 && iDb<db->nDb ); |
| 3292 | assert( db->aDb[iDb].pBt!=0 ); |
drh | a7ab6d8 | 2014-07-21 15:44:39 +0000 | [diff] [blame] | 3293 | assert( DbMaskTest(p->btreeMask, iDb) ); |
danielk1977 | 0d19f7a | 2009-06-03 11:25:07 +0000 | [diff] [blame] | 3294 | |
danielk1977 | 602b466 | 2009-07-02 07:47:33 +0000 | [diff] [blame] | 3295 | sqlite3BtreeGetMeta(db->aDb[iDb].pBt, iCookie, (u32 *)&iMeta); |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 3296 | pOut = out2Prerelease(p, pOp); |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 3297 | pOut->u.i = iMeta; |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 3298 | break; |
| 3299 | } |
| 3300 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 3301 | /* Opcode: SetCookie P1 P2 P3 * * |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 3302 | ** |
drh | 1861afc | 2016-02-01 21:48:34 +0000 | [diff] [blame] | 3303 | ** Write the integer value P3 into cookie number P2 of database P1. |
| 3304 | ** P2==1 is the schema version. P2==2 is the database format. |
| 3305 | ** P2==3 is the recommended pager cache |
danielk1977 | 0d19f7a | 2009-06-03 11:25:07 +0000 | [diff] [blame] | 3306 | ** size, and so forth. P1==0 is the main database file and P1==1 is the |
| 3307 | ** database file used to store temporary tables. |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 3308 | ** |
| 3309 | ** A transaction must be started before executing this opcode. |
| 3310 | */ |
drh | 1861afc | 2016-02-01 21:48:34 +0000 | [diff] [blame] | 3311 | case OP_SetCookie: { |
drh | 3f7d4e4 | 2004-07-24 14:35:58 +0000 | [diff] [blame] | 3312 | Db *pDb; |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 3313 | assert( pOp->p2<SQLITE_N_BTREE_META ); |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 3314 | assert( pOp->p1>=0 && pOp->p1<db->nDb ); |
drh | a7ab6d8 | 2014-07-21 15:44:39 +0000 | [diff] [blame] | 3315 | assert( DbMaskTest(p->btreeMask, pOp->p1) ); |
drh | 9e92a47 | 2013-06-27 17:40:30 +0000 | [diff] [blame] | 3316 | assert( p->readOnly==0 ); |
drh | 3f7d4e4 | 2004-07-24 14:35:58 +0000 | [diff] [blame] | 3317 | pDb = &db->aDb[pOp->p1]; |
| 3318 | assert( pDb->pBt!=0 ); |
drh | 2120608 | 2011-04-04 18:22:02 +0000 | [diff] [blame] | 3319 | assert( sqlite3SchemaMutexHeld(db, pOp->p1, 0) ); |
drh | a3b321d | 2004-05-11 09:31:31 +0000 | [diff] [blame] | 3320 | /* See note about index shifting on OP_ReadCookie */ |
drh | 1861afc | 2016-02-01 21:48:34 +0000 | [diff] [blame] | 3321 | rc = sqlite3BtreeUpdateMeta(pDb->pBt, pOp->p2, pOp->p3); |
danielk1977 | 0d19f7a | 2009-06-03 11:25:07 +0000 | [diff] [blame] | 3322 | if( pOp->p2==BTREE_SCHEMA_VERSION ){ |
drh | 3f7d4e4 | 2004-07-24 14:35:58 +0000 | [diff] [blame] | 3323 | /* When the schema cookie changes, record the new cookie internally */ |
drh | 1861afc | 2016-02-01 21:48:34 +0000 | [diff] [blame] | 3324 | pDb->pSchema->schema_cookie = pOp->p3; |
drh | 8257aa8 | 2017-07-26 19:59:13 +0000 | [diff] [blame] | 3325 | db->mDbFlags |= DBFLAG_SchemaChange; |
danielk1977 | 0d19f7a | 2009-06-03 11:25:07 +0000 | [diff] [blame] | 3326 | }else if( pOp->p2==BTREE_FILE_FORMAT ){ |
drh | d28bcb3 | 2005-12-21 14:43:11 +0000 | [diff] [blame] | 3327 | /* Record changes in the file format */ |
drh | 1861afc | 2016-02-01 21:48:34 +0000 | [diff] [blame] | 3328 | pDb->pSchema->file_format = pOp->p3; |
drh | 3f7d4e4 | 2004-07-24 14:35:58 +0000 | [diff] [blame] | 3329 | } |
drh | fd426c6 | 2006-01-30 15:34:22 +0000 | [diff] [blame] | 3330 | if( pOp->p1==1 ){ |
| 3331 | /* Invalidate all prepared statements whenever the TEMP database |
| 3332 | ** schema is changed. Ticket #1644 */ |
| 3333 | sqlite3ExpirePreparedStatements(db); |
dan | fa401de | 2009-10-16 14:55:03 +0000 | [diff] [blame] | 3334 | p->expired = 0; |
drh | fd426c6 | 2006-01-30 15:34:22 +0000 | [diff] [blame] | 3335 | } |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 3336 | if( rc ) goto abort_due_to_error; |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 3337 | break; |
| 3338 | } |
| 3339 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 3340 | /* Opcode: OpenRead P1 P2 P3 P4 P5 |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 3341 | ** Synopsis: root=P2 iDb=P3 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3342 | ** |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 3343 | ** Open a read-only cursor for the database table whose root page is |
danielk1977 | 207872a | 2008-01-03 07:54:23 +0000 | [diff] [blame] | 3344 | ** P2 in a database file. The database file is determined by P3. |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 3345 | ** P3==0 means the main database, P3==1 means the database used for |
| 3346 | ** temporary tables, and P3>1 means used the corresponding attached |
| 3347 | ** database. Give the new cursor an identifier of P1. The P1 |
danielk1977 | 207872a | 2008-01-03 07:54:23 +0000 | [diff] [blame] | 3348 | ** values need not be contiguous but all P1 values should be small integers. |
| 3349 | ** It is an error for P1 to be negative. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3350 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 3351 | ** If P5!=0 then use the content of register P2 as the root page, not |
| 3352 | ** the value of P2 itself. |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 3353 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 3354 | ** There will be a read lock on the database whenever there is an |
| 3355 | ** open cursor. If the database was unlocked prior to this instruction |
| 3356 | ** then a read lock is acquired as part of this instruction. A read |
| 3357 | ** lock allows other processes to read the database but prohibits |
| 3358 | ** any other process from modifying the database. The read lock is |
| 3359 | ** released when all cursors are closed. If this instruction attempts |
| 3360 | ** to get a read lock but fails, the script terminates with an |
| 3361 | ** SQLITE_BUSY error code. |
| 3362 | ** |
danielk1977 | d336e22 | 2009-02-20 10:58:41 +0000 | [diff] [blame] | 3363 | ** The P4 value may be either an integer (P4_INT32) or a pointer to |
| 3364 | ** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo |
| 3365 | ** structure, then said structure defines the content and collating |
| 3366 | ** sequence of the index being opened. Otherwise, if P4 is an integer |
| 3367 | ** value, it is set to the number of columns in the table. |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 3368 | ** |
drh | 3526319 | 2014-07-22 20:02:19 +0000 | [diff] [blame] | 3369 | ** See also: OpenWrite, ReopenIdx |
| 3370 | */ |
| 3371 | /* Opcode: ReopenIdx P1 P2 P3 P4 P5 |
| 3372 | ** Synopsis: root=P2 iDb=P3 |
| 3373 | ** |
| 3374 | ** The ReopenIdx opcode works exactly like ReadOpen except that it first |
| 3375 | ** checks to see if the cursor on P1 is already open with a root page |
| 3376 | ** number of P2 and if it is this opcode becomes a no-op. In other words, |
| 3377 | ** if the cursor is already open, do not reopen it. |
| 3378 | ** |
| 3379 | ** The ReopenIdx opcode may only be used with P5==0 and with P4 being |
| 3380 | ** a P4_KEYINFO object. Furthermore, the P3 value must be the same as |
| 3381 | ** every other ReopenIdx or OpenRead for the same cursor number. |
| 3382 | ** |
| 3383 | ** See the OpenRead opcode documentation for additional information. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3384 | */ |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 3385 | /* Opcode: OpenWrite P1 P2 P3 P4 P5 |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 3386 | ** Synopsis: root=P2 iDb=P3 |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 3387 | ** |
| 3388 | ** Open a read/write cursor named P1 on the table or index whose root |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 3389 | ** page is P2. Or if P5!=0 use the content of register P2 to find the |
| 3390 | ** root page. |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 3391 | ** |
danielk1977 | d336e22 | 2009-02-20 10:58:41 +0000 | [diff] [blame] | 3392 | ** The P4 value may be either an integer (P4_INT32) or a pointer to |
| 3393 | ** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo |
| 3394 | ** structure, then said structure defines the content and collating |
| 3395 | ** sequence of the index being opened. Otherwise, if P4 is an integer |
drh | 35cd643 | 2009-06-05 14:17:21 +0000 | [diff] [blame] | 3396 | ** value, it is set to the number of columns in the table, or to the |
| 3397 | ** largest index of any column of the table that is actually used. |
jplyon | 5a56422 | 2003-06-02 06:15:58 +0000 | [diff] [blame] | 3398 | ** |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 3399 | ** This instruction works just like OpenRead except that it opens the cursor |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 3400 | ** in read/write mode. For a given table, there can be one or more read-only |
| 3401 | ** cursors or a single read/write cursor but not both. |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 3402 | ** |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 3403 | ** See also OpenRead. |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 3404 | */ |
drh | 3526319 | 2014-07-22 20:02:19 +0000 | [diff] [blame] | 3405 | case OP_ReopenIdx: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3406 | int nField; |
| 3407 | KeyInfo *pKeyInfo; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3408 | int p2; |
| 3409 | int iDb; |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 3410 | int wrFlag; |
| 3411 | Btree *pX; |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 3412 | VdbeCursor *pCur; |
drh | d946db0 | 2005-12-29 19:23:06 +0000 | [diff] [blame] | 3413 | Db *pDb; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3414 | |
drh | e0997b3 | 2015-03-20 14:57:50 +0000 | [diff] [blame] | 3415 | assert( pOp->p5==0 || pOp->p5==OPFLAG_SEEKEQ ); |
drh | 3526319 | 2014-07-22 20:02:19 +0000 | [diff] [blame] | 3416 | assert( pOp->p4type==P4_KEYINFO ); |
| 3417 | pCur = p->apCsr[pOp->p1]; |
drh | e8f2c9d | 2014-08-06 17:49:13 +0000 | [diff] [blame] | 3418 | if( pCur && pCur->pgnoRoot==(u32)pOp->p2 ){ |
drh | 3526319 | 2014-07-22 20:02:19 +0000 | [diff] [blame] | 3419 | assert( pCur->iDb==pOp->p3 ); /* Guaranteed by the code generator */ |
drh | e0997b3 | 2015-03-20 14:57:50 +0000 | [diff] [blame] | 3420 | goto open_cursor_set_hints; |
drh | 3526319 | 2014-07-22 20:02:19 +0000 | [diff] [blame] | 3421 | } |
| 3422 | /* If the cursor is not currently open or is open on a different |
| 3423 | ** index, then fall through into OP_OpenRead to force a reopen */ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3424 | case OP_OpenRead: |
drh | 1fa509a | 2015-03-20 16:34:49 +0000 | [diff] [blame] | 3425 | case OP_OpenWrite: |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3426 | |
drh | e0997b3 | 2015-03-20 14:57:50 +0000 | [diff] [blame] | 3427 | assert( pOp->opcode==OP_OpenWrite || pOp->p5==0 || pOp->p5==OPFLAG_SEEKEQ ); |
drh | 1713afb | 2013-06-28 01:24:57 +0000 | [diff] [blame] | 3428 | assert( p->bIsReader ); |
drh | 3526319 | 2014-07-22 20:02:19 +0000 | [diff] [blame] | 3429 | assert( pOp->opcode==OP_OpenRead || pOp->opcode==OP_ReopenIdx |
| 3430 | || p->readOnly==0 ); |
dan | 428c218 | 2012-08-06 18:50:11 +0000 | [diff] [blame] | 3431 | |
dan | fa401de | 2009-10-16 14:55:03 +0000 | [diff] [blame] | 3432 | if( p->expired ){ |
drh | 47b7fc7 | 2014-11-11 01:33:57 +0000 | [diff] [blame] | 3433 | rc = SQLITE_ABORT_ROLLBACK; |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 3434 | goto abort_due_to_error; |
dan | fa401de | 2009-10-16 14:55:03 +0000 | [diff] [blame] | 3435 | } |
| 3436 | |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3437 | nField = 0; |
| 3438 | pKeyInfo = 0; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3439 | p2 = pOp->p2; |
| 3440 | iDb = pOp->p3; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3441 | assert( iDb>=0 && iDb<db->nDb ); |
drh | a7ab6d8 | 2014-07-21 15:44:39 +0000 | [diff] [blame] | 3442 | assert( DbMaskTest(p->btreeMask, iDb) ); |
drh | d946db0 | 2005-12-29 19:23:06 +0000 | [diff] [blame] | 3443 | pDb = &db->aDb[iDb]; |
| 3444 | pX = pDb->pBt; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3445 | assert( pX!=0 ); |
drh | d946db0 | 2005-12-29 19:23:06 +0000 | [diff] [blame] | 3446 | if( pOp->opcode==OP_OpenWrite ){ |
dan | fd261ec | 2015-10-22 20:54:33 +0000 | [diff] [blame] | 3447 | assert( OPFLAG_FORDELETE==BTREE_FORDELETE ); |
| 3448 | wrFlag = BTREE_WRCSR | (pOp->p5 & OPFLAG_FORDELETE); |
drh | 2120608 | 2011-04-04 18:22:02 +0000 | [diff] [blame] | 3449 | assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3450 | if( pDb->pSchema->file_format < p->minWriteFileFormat ){ |
| 3451 | p->minWriteFileFormat = pDb->pSchema->file_format; |
drh | d946db0 | 2005-12-29 19:23:06 +0000 | [diff] [blame] | 3452 | } |
| 3453 | }else{ |
| 3454 | wrFlag = 0; |
| 3455 | } |
dan | 428c218 | 2012-08-06 18:50:11 +0000 | [diff] [blame] | 3456 | if( pOp->p5 & OPFLAG_P2ISREG ){ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3457 | assert( p2>0 ); |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 3458 | assert( p2<=(p->nMem+1 - p->nCursor) ); |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 3459 | pIn2 = &aMem[p2]; |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 3460 | assert( memIsValid(pIn2) ); |
| 3461 | assert( (pIn2->flags & MEM_Int)!=0 ); |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3462 | sqlite3VdbeMemIntegerify(pIn2); |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 3463 | p2 = (int)pIn2->u.i; |
drh | 0f3f766 | 2017-08-18 14:34:28 +0000 | [diff] [blame] | 3464 | /* The p2 value always comes from a prior OP_CreateBtree opcode and |
drh | 9a65f2c | 2009-06-22 19:05:40 +0000 | [diff] [blame] | 3465 | ** that opcode will always set the p2 value to 2 or more or else fail. |
| 3466 | ** If there were a failure, the prepared statement would have halted |
| 3467 | ** before reaching this instruction. */ |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 3468 | assert( p2>=2 ); |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 3469 | } |
danielk1977 | d336e22 | 2009-02-20 10:58:41 +0000 | [diff] [blame] | 3470 | if( pOp->p4type==P4_KEYINFO ){ |
| 3471 | pKeyInfo = pOp->p4.pKeyInfo; |
drh | 41e13e1 | 2013-11-07 14:09:39 +0000 | [diff] [blame] | 3472 | assert( pKeyInfo->enc==ENC(db) ); |
| 3473 | assert( pKeyInfo->db==db ); |
drh | a485ad1 | 2017-08-02 22:43:14 +0000 | [diff] [blame] | 3474 | nField = pKeyInfo->nAllField; |
danielk1977 | d336e22 | 2009-02-20 10:58:41 +0000 | [diff] [blame] | 3475 | }else if( pOp->p4type==P4_INT32 ){ |
| 3476 | nField = pOp->p4.i; |
| 3477 | } |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 3478 | assert( pOp->p1>=0 ); |
drh | 399af1d | 2013-11-20 17:25:55 +0000 | [diff] [blame] | 3479 | assert( nField>=0 ); |
| 3480 | testcase( nField==0 ); /* Table with INTEGER PRIMARY KEY and nothing else */ |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 3481 | pCur = allocateCursor(p, pOp->p1, nField, iDb, CURTYPE_BTREE); |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 3482 | if( pCur==0 ) goto no_mem; |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 3483 | pCur->nullRow = 1; |
drh | d4187c7 | 2010-08-30 22:15:45 +0000 | [diff] [blame] | 3484 | pCur->isOrdered = 1; |
drh | 3526319 | 2014-07-22 20:02:19 +0000 | [diff] [blame] | 3485 | pCur->pgnoRoot = p2; |
drh | b89aeb6 | 2016-01-27 15:49:32 +0000 | [diff] [blame] | 3486 | #ifdef SQLITE_DEBUG |
| 3487 | pCur->wrFlag = wrFlag; |
| 3488 | #endif |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 3489 | rc = sqlite3BtreeCursor(pX, p2, wrFlag, pKeyInfo, pCur->uc.pCursor); |
danielk1977 | d336e22 | 2009-02-20 10:58:41 +0000 | [diff] [blame] | 3490 | pCur->pKeyInfo = pKeyInfo; |
drh | 14da87f | 2013-11-20 21:51:33 +0000 | [diff] [blame] | 3491 | /* Set the VdbeCursor.isTable variable. Previous versions of |
danielk1977 | 172114a | 2009-07-07 15:47:12 +0000 | [diff] [blame] | 3492 | ** SQLite used to check if the root-page flags were sane at this point |
| 3493 | ** and report database corruption if they were not, but this check has |
| 3494 | ** since moved into the btree layer. */ |
| 3495 | pCur->isTable = pOp->p4type!=P4_KEYINFO; |
drh | e0997b3 | 2015-03-20 14:57:50 +0000 | [diff] [blame] | 3496 | |
| 3497 | open_cursor_set_hints: |
| 3498 | assert( OPFLAG_BULKCSR==BTREE_BULKLOAD ); |
| 3499 | assert( OPFLAG_SEEKEQ==BTREE_SEEK_EQ ); |
drh | 0403cb3 | 2015-08-14 23:57:04 +0000 | [diff] [blame] | 3500 | testcase( pOp->p5 & OPFLAG_BULKCSR ); |
drh | 9abe841 | 2016-01-02 05:00:31 +0000 | [diff] [blame] | 3501 | #ifdef SQLITE_ENABLE_CURSOR_HINTS |
drh | 0403cb3 | 2015-08-14 23:57:04 +0000 | [diff] [blame] | 3502 | testcase( pOp->p2 & OPFLAG_SEEKEQ ); |
| 3503 | #endif |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 3504 | sqlite3BtreeCursorHintFlags(pCur->uc.pCursor, |
drh | f7854c7 | 2015-10-27 13:24:37 +0000 | [diff] [blame] | 3505 | (pOp->p5 & (OPFLAG_BULKCSR|OPFLAG_SEEKEQ))); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 3506 | if( rc ) goto abort_due_to_error; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3507 | break; |
| 3508 | } |
| 3509 | |
drh | e08e8d6 | 2017-05-01 15:15:41 +0000 | [diff] [blame] | 3510 | /* Opcode: OpenDup P1 P2 * * * |
| 3511 | ** |
| 3512 | ** Open a new cursor P1 that points to the same ephemeral table as |
| 3513 | ** cursor P2. The P2 cursor must have been opened by a prior OP_OpenEphemeral |
| 3514 | ** opcode. Only ephemeral cursors may be duplicated. |
| 3515 | ** |
| 3516 | ** Duplicate ephemeral cursors are used for self-joins of materialized views. |
| 3517 | */ |
| 3518 | case OP_OpenDup: { |
| 3519 | VdbeCursor *pOrig; /* The original cursor to be duplicated */ |
| 3520 | VdbeCursor *pCx; /* The new cursor */ |
| 3521 | |
| 3522 | pOrig = p->apCsr[pOp->p2]; |
| 3523 | assert( pOrig->pBtx!=0 ); /* Only ephemeral cursors can be duplicated */ |
| 3524 | |
| 3525 | pCx = allocateCursor(p, pOp->p1, pOrig->nField, -1, CURTYPE_BTREE); |
| 3526 | if( pCx==0 ) goto no_mem; |
| 3527 | pCx->nullRow = 1; |
| 3528 | pCx->isEphemeral = 1; |
| 3529 | pCx->pKeyInfo = pOrig->pKeyInfo; |
| 3530 | pCx->isTable = pOrig->isTable; |
| 3531 | rc = sqlite3BtreeCursor(pOrig->pBtx, MASTER_ROOT, BTREE_WRCSR, |
| 3532 | pCx->pKeyInfo, pCx->uc.pCursor); |
drh | 3f4df4c | 2017-05-02 17:54:19 +0000 | [diff] [blame] | 3533 | /* The sqlite3BtreeCursor() routine can only fail for the first cursor |
| 3534 | ** opened for a database. Since there is already an open cursor when this |
| 3535 | ** opcode is run, the sqlite3BtreeCursor() cannot fail */ |
| 3536 | assert( rc==SQLITE_OK ); |
drh | e08e8d6 | 2017-05-01 15:15:41 +0000 | [diff] [blame] | 3537 | break; |
| 3538 | } |
| 3539 | |
| 3540 | |
drh | 2a5d990 | 2011-08-26 00:34:45 +0000 | [diff] [blame] | 3541 | /* Opcode: OpenEphemeral P1 P2 * P4 P5 |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 3542 | ** Synopsis: nColumn=P2 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3543 | ** |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 3544 | ** Open a new cursor P1 to a transient table. |
drh | 9170dd7 | 2005-07-08 17:13:46 +0000 | [diff] [blame] | 3545 | ** The cursor is always opened read/write even if |
drh | 25d3adb | 2010-04-05 15:11:08 +0000 | [diff] [blame] | 3546 | ** the main database is read-only. The ephemeral |
drh | 9170dd7 | 2005-07-08 17:13:46 +0000 | [diff] [blame] | 3547 | ** table is deleted automatically when the cursor is closed. |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 3548 | ** |
drh | 25d3adb | 2010-04-05 15:11:08 +0000 | [diff] [blame] | 3549 | ** P2 is the number of columns in the ephemeral table. |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 3550 | ** The cursor points to a BTree table if P4==0 and to a BTree index |
| 3551 | ** 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] | 3552 | ** that defines the format of keys in the index. |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 3553 | ** |
drh | 2a5d990 | 2011-08-26 00:34:45 +0000 | [diff] [blame] | 3554 | ** The P5 parameter can be a mask of the BTREE_* flags defined |
| 3555 | ** in btree.h. These flags control aspects of the operation of |
| 3556 | ** the btree. The BTREE_OMIT_JOURNAL and BTREE_SINGLE flags are |
| 3557 | ** added automatically. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3558 | */ |
drh | a21a64d | 2010-04-06 22:33:55 +0000 | [diff] [blame] | 3559 | /* Opcode: OpenAutoindex P1 P2 * P4 * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 3560 | ** Synopsis: nColumn=P2 |
drh | a21a64d | 2010-04-06 22:33:55 +0000 | [diff] [blame] | 3561 | ** |
| 3562 | ** This opcode works the same as OP_OpenEphemeral. It has a |
| 3563 | ** different name to distinguish its use. Tables created using |
| 3564 | ** by this opcode will be used for automatically created transient |
| 3565 | ** indices in joins. |
| 3566 | */ |
| 3567 | case OP_OpenAutoindex: |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3568 | case OP_OpenEphemeral: { |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 3569 | VdbeCursor *pCx; |
drh | 41e13e1 | 2013-11-07 14:09:39 +0000 | [diff] [blame] | 3570 | KeyInfo *pKeyInfo; |
| 3571 | |
drh | d4187c7 | 2010-08-30 22:15:45 +0000 | [diff] [blame] | 3572 | static const int vfsFlags = |
drh | 33f4e02 | 2007-09-03 15:19:34 +0000 | [diff] [blame] | 3573 | SQLITE_OPEN_READWRITE | |
| 3574 | SQLITE_OPEN_CREATE | |
| 3575 | SQLITE_OPEN_EXCLUSIVE | |
| 3576 | SQLITE_OPEN_DELETEONCLOSE | |
| 3577 | SQLITE_OPEN_TRANSIENT_DB; |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 3578 | assert( pOp->p1>=0 ); |
drh | 399af1d | 2013-11-20 17:25:55 +0000 | [diff] [blame] | 3579 | assert( pOp->p2>=0 ); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 3580 | pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, CURTYPE_BTREE); |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 3581 | if( pCx==0 ) goto no_mem; |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 3582 | pCx->nullRow = 1; |
drh | 079a307 | 2014-03-19 14:10:55 +0000 | [diff] [blame] | 3583 | pCx->isEphemeral = 1; |
drh | fbd8cbd | 2016-12-10 12:58:15 +0000 | [diff] [blame] | 3584 | rc = sqlite3BtreeOpen(db->pVfs, 0, db, &pCx->pBtx, |
drh | d4187c7 | 2010-08-30 22:15:45 +0000 | [diff] [blame] | 3585 | BTREE_OMIT_JOURNAL | BTREE_SINGLE | pOp->p5, vfsFlags); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3586 | if( rc==SQLITE_OK ){ |
drh | fbd8cbd | 2016-12-10 12:58:15 +0000 | [diff] [blame] | 3587 | rc = sqlite3BtreeBeginTrans(pCx->pBtx, 1); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3588 | } |
| 3589 | if( rc==SQLITE_OK ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3590 | /* If a transient index is required, create it by calling |
drh | d4187c7 | 2010-08-30 22:15:45 +0000 | [diff] [blame] | 3591 | ** sqlite3BtreeCreateTable() with the BTREE_BLOBKEY flag before |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3592 | ** opening it. If a transient table is required, just use the |
drh | d4187c7 | 2010-08-30 22:15:45 +0000 | [diff] [blame] | 3593 | ** automatically created table with root-page 1 (an BLOB_INTKEY table). |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3594 | */ |
drh | fbd8cbd | 2016-12-10 12:58:15 +0000 | [diff] [blame] | 3595 | if( (pCx->pKeyInfo = pKeyInfo = pOp->p4.pKeyInfo)!=0 ){ |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 3596 | int pgno; |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 3597 | assert( pOp->p4type==P4_KEYINFO ); |
drh | fbd8cbd | 2016-12-10 12:58:15 +0000 | [diff] [blame] | 3598 | rc = sqlite3BtreeCreateTable(pCx->pBtx, &pgno, BTREE_BLOBKEY | pOp->p5); |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 3599 | if( rc==SQLITE_OK ){ |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 3600 | assert( pgno==MASTER_ROOT+1 ); |
drh | 41e13e1 | 2013-11-07 14:09:39 +0000 | [diff] [blame] | 3601 | assert( pKeyInfo->db==db ); |
| 3602 | assert( pKeyInfo->enc==ENC(db) ); |
drh | fbd8cbd | 2016-12-10 12:58:15 +0000 | [diff] [blame] | 3603 | rc = sqlite3BtreeCursor(pCx->pBtx, pgno, BTREE_WRCSR, |
drh | 62aaa6c | 2015-11-21 17:27:42 +0000 | [diff] [blame] | 3604 | pKeyInfo, pCx->uc.pCursor); |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 3605 | } |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3606 | pCx->isTable = 0; |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 3607 | }else{ |
drh | fbd8cbd | 2016-12-10 12:58:15 +0000 | [diff] [blame] | 3608 | rc = sqlite3BtreeCursor(pCx->pBtx, MASTER_ROOT, BTREE_WRCSR, |
drh | 62aaa6c | 2015-11-21 17:27:42 +0000 | [diff] [blame] | 3609 | 0, pCx->uc.pCursor); |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3610 | pCx->isTable = 1; |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 3611 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3612 | } |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 3613 | if( rc ) goto abort_due_to_error; |
drh | d4187c7 | 2010-08-30 22:15:45 +0000 | [diff] [blame] | 3614 | pCx->isOrdered = (pOp->p5!=BTREE_UNORDERED); |
dan | 5134d13 | 2011-09-02 10:31:11 +0000 | [diff] [blame] | 3615 | break; |
| 3616 | } |
| 3617 | |
dan | fad9f9a | 2014-04-01 18:41:51 +0000 | [diff] [blame] | 3618 | /* Opcode: SorterOpen P1 P2 P3 P4 * |
dan | 5134d13 | 2011-09-02 10:31:11 +0000 | [diff] [blame] | 3619 | ** |
| 3620 | ** This opcode works like OP_OpenEphemeral except that it opens |
| 3621 | ** a transient index that is specifically designed to sort large |
| 3622 | ** tables using an external merge-sort algorithm. |
dan | fad9f9a | 2014-04-01 18:41:51 +0000 | [diff] [blame] | 3623 | ** |
| 3624 | ** If argument P3 is non-zero, then it indicates that the sorter may |
| 3625 | ** assume that a stable sort considering the first P3 fields of each |
| 3626 | ** key is sufficient to produce the required results. |
dan | 5134d13 | 2011-09-02 10:31:11 +0000 | [diff] [blame] | 3627 | */ |
drh | ca892a7 | 2011-09-03 00:17:51 +0000 | [diff] [blame] | 3628 | case OP_SorterOpen: { |
dan | 5134d13 | 2011-09-02 10:31:11 +0000 | [diff] [blame] | 3629 | VdbeCursor *pCx; |
drh | 3a94987 | 2012-09-18 13:20:13 +0000 | [diff] [blame] | 3630 | |
drh | 399af1d | 2013-11-20 17:25:55 +0000 | [diff] [blame] | 3631 | assert( pOp->p1>=0 ); |
| 3632 | assert( pOp->p2>=0 ); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 3633 | pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, CURTYPE_SORTER); |
dan | 5134d13 | 2011-09-02 10:31:11 +0000 | [diff] [blame] | 3634 | if( pCx==0 ) goto no_mem; |
| 3635 | pCx->pKeyInfo = pOp->p4.pKeyInfo; |
drh | 41e13e1 | 2013-11-07 14:09:39 +0000 | [diff] [blame] | 3636 | assert( pCx->pKeyInfo->db==db ); |
| 3637 | assert( pCx->pKeyInfo->enc==ENC(db) ); |
dan | fad9f9a | 2014-04-01 18:41:51 +0000 | [diff] [blame] | 3638 | rc = sqlite3VdbeSorterInit(db, pOp->p3, pCx); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 3639 | if( rc ) goto abort_due_to_error; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3640 | break; |
| 3641 | } |
| 3642 | |
dan | 78d5843 | 2014-03-25 15:04:07 +0000 | [diff] [blame] | 3643 | /* Opcode: SequenceTest P1 P2 * * * |
| 3644 | ** Synopsis: if( cursor[P1].ctr++ ) pc = P2 |
| 3645 | ** |
| 3646 | ** P1 is a sorter cursor. If the sequence counter is currently zero, jump |
| 3647 | ** to P2. Regardless of whether or not the jump is taken, increment the |
| 3648 | ** the sequence value. |
| 3649 | */ |
| 3650 | case OP_SequenceTest: { |
| 3651 | VdbeCursor *pC; |
| 3652 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 3653 | pC = p->apCsr[pOp->p1]; |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 3654 | assert( isSorter(pC) ); |
dan | 78d5843 | 2014-03-25 15:04:07 +0000 | [diff] [blame] | 3655 | if( (pC->seqCount++)==0 ){ |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 3656 | goto jump_to_p2; |
dan | 78d5843 | 2014-03-25 15:04:07 +0000 | [diff] [blame] | 3657 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3658 | break; |
| 3659 | } |
| 3660 | |
drh | 5f61229 | 2014-02-08 23:20:32 +0000 | [diff] [blame] | 3661 | /* Opcode: OpenPseudo P1 P2 P3 * * |
drh | 60830e3 | 2014-02-10 15:56:34 +0000 | [diff] [blame] | 3662 | ** Synopsis: P3 columns in r[P2] |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3663 | ** |
| 3664 | ** 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] | 3665 | ** row of data. The content of that one row is the content of memory |
| 3666 | ** register P2. In other words, cursor P1 becomes an alias for the |
| 3667 | ** MEM_Blob content contained in register P2. |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3668 | ** |
drh | 2d8d7ce | 2010-02-15 15:17:05 +0000 | [diff] [blame] | 3669 | ** A pseudo-table created by this opcode is used to hold a single |
drh | cdd536f | 2006-03-17 00:04:03 +0000 | [diff] [blame] | 3670 | ** row output from the sorter so that the row can be decomposed into |
drh | 3e9ca09 | 2009-09-08 01:14:48 +0000 | [diff] [blame] | 3671 | ** individual columns using the OP_Column opcode. The OP_Column opcode |
| 3672 | ** is the only cursor opcode that works with a pseudo-table. |
danielk1977 | d336e22 | 2009-02-20 10:58:41 +0000 | [diff] [blame] | 3673 | ** |
| 3674 | ** P3 is the number of fields in the records that will be stored by |
| 3675 | ** the pseudo-table. |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3676 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3677 | case OP_OpenPseudo: { |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 3678 | VdbeCursor *pCx; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3679 | |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 3680 | assert( pOp->p1>=0 ); |
drh | 399af1d | 2013-11-20 17:25:55 +0000 | [diff] [blame] | 3681 | assert( pOp->p3>=0 ); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 3682 | pCx = allocateCursor(p, pOp->p1, pOp->p3, -1, CURTYPE_PSEUDO); |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 3683 | if( pCx==0 ) goto no_mem; |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3684 | pCx->nullRow = 1; |
drh | fe0cf7a | 2017-08-16 19:20:20 +0000 | [diff] [blame] | 3685 | pCx->seekResult = pOp->p2; |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3686 | pCx->isTable = 1; |
drh | fe0cf7a | 2017-08-16 19:20:20 +0000 | [diff] [blame] | 3687 | /* Give this pseudo-cursor a fake BtCursor pointer so that pCx |
| 3688 | ** can be safely passed to sqlite3VdbeCursorMoveto(). This avoids a test |
| 3689 | ** for pCx->eCurType==CURTYPE_BTREE inside of sqlite3VdbeCursorMoveto() |
| 3690 | ** which is a performance optimization */ |
| 3691 | pCx->uc.pCursor = sqlite3BtreeFakeValidCursor(); |
drh | 5f61229 | 2014-02-08 23:20:32 +0000 | [diff] [blame] | 3692 | assert( pOp->p5==0 ); |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3693 | break; |
| 3694 | } |
| 3695 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 3696 | /* Opcode: Close P1 * * * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3697 | ** |
| 3698 | ** Close a cursor previously opened as P1. If P1 is not |
| 3699 | ** currently open, this instruction is a no-op. |
| 3700 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3701 | case OP_Close: { |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 3702 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 3703 | sqlite3VdbeFreeCursor(p, p->apCsr[pOp->p1]); |
| 3704 | p->apCsr[pOp->p1] = 0; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3705 | break; |
| 3706 | } |
| 3707 | |
drh | 97bae79 | 2015-06-05 15:59:57 +0000 | [diff] [blame] | 3708 | #ifdef SQLITE_ENABLE_COLUMN_USED_MASK |
| 3709 | /* Opcode: ColumnsUsed P1 * * P4 * |
| 3710 | ** |
| 3711 | ** This opcode (which only exists if SQLite was compiled with |
| 3712 | ** SQLITE_ENABLE_COLUMN_USED_MASK) identifies which columns of the |
| 3713 | ** table or index for cursor P1 are used. P4 is a 64-bit integer |
| 3714 | ** (P4_INT64) in which the first 63 bits are one for each of the |
| 3715 | ** first 63 columns of the table or index that are actually used |
| 3716 | ** by the cursor. The high-order bit is set if any column after |
| 3717 | ** the 64th is used. |
| 3718 | */ |
| 3719 | case OP_ColumnsUsed: { |
| 3720 | VdbeCursor *pC; |
| 3721 | pC = p->apCsr[pOp->p1]; |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 3722 | assert( pC->eCurType==CURTYPE_BTREE ); |
drh | 97bae79 | 2015-06-05 15:59:57 +0000 | [diff] [blame] | 3723 | pC->maskUsed = *(u64*)pOp->p4.pI64; |
| 3724 | break; |
| 3725 | } |
| 3726 | #endif |
| 3727 | |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 3728 | /* Opcode: SeekGE P1 P2 P3 P4 * |
drh | f63552b | 2013-10-30 00:25:03 +0000 | [diff] [blame] | 3729 | ** Synopsis: key=r[P3@P4] |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3730 | ** |
danielk1977 | b790c6c | 2008-04-18 10:25:24 +0000 | [diff] [blame] | 3731 | ** 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] | 3732 | ** use the value in register P3 as the key. If cursor P1 refers |
danielk1977 | b790c6c | 2008-04-18 10:25:24 +0000 | [diff] [blame] | 3733 | ** to an SQL index, then P3 is the first in an array of P4 registers |
| 3734 | ** that are used as an unpacked index key. |
| 3735 | ** |
| 3736 | ** Reposition cursor P1 so that it points to the smallest entry that |
| 3737 | ** is greater than or equal to the key value. If there are no records |
| 3738 | ** 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] | 3739 | ** |
drh | b1d607d | 2015-11-05 22:30:54 +0000 | [diff] [blame] | 3740 | ** If the cursor P1 was opened using the OPFLAG_SEEKEQ flag, then this |
| 3741 | ** opcode will always land on a record that equally equals the key, or |
| 3742 | ** else jump immediately to P2. When the cursor is OPFLAG_SEEKEQ, this |
| 3743 | ** opcode must be followed by an IdxLE opcode with the same arguments. |
| 3744 | ** The IdxLE opcode will be skipped if this opcode succeeds, but the |
| 3745 | ** IdxLE opcode will be used on subsequent loop iterations. |
| 3746 | ** |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 3747 | ** This opcode leaves the cursor configured to move in forward order, |
drh | bc5cf38 | 2014-08-06 01:08:07 +0000 | [diff] [blame] | 3748 | ** from the beginning toward the end. In other words, the cursor is |
drh | 5dad9a3 | 2014-07-25 18:37:42 +0000 | [diff] [blame] | 3749 | ** configured to use Next, not Prev. |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 3750 | ** |
drh | 935850e | 2014-05-24 17:15:15 +0000 | [diff] [blame] | 3751 | ** See also: Found, NotFound, SeekLt, SeekGt, SeekLe |
drh | 7cf6e4d | 2004-05-19 14:56:55 +0000 | [diff] [blame] | 3752 | */ |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 3753 | /* Opcode: SeekGT P1 P2 P3 P4 * |
drh | f63552b | 2013-10-30 00:25:03 +0000 | [diff] [blame] | 3754 | ** Synopsis: key=r[P3@P4] |
drh | 7cf6e4d | 2004-05-19 14:56:55 +0000 | [diff] [blame] | 3755 | ** |
danielk1977 | b790c6c | 2008-04-18 10:25:24 +0000 | [diff] [blame] | 3756 | ** 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] | 3757 | ** use the value in register P3 as a key. If cursor P1 refers |
danielk1977 | b790c6c | 2008-04-18 10:25:24 +0000 | [diff] [blame] | 3758 | ** to an SQL index, then P3 is the first in an array of P4 registers |
| 3759 | ** that are used as an unpacked index key. |
| 3760 | ** |
| 3761 | ** Reposition cursor P1 so that it points to the smallest entry that |
| 3762 | ** is greater than the key value. If there are no records greater than |
| 3763 | ** the key and P2 is not zero, then jump to P2. |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 3764 | ** |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 3765 | ** This opcode leaves the cursor configured to move in forward order, |
drh | 4ed2fb9 | 2014-08-14 13:06:25 +0000 | [diff] [blame] | 3766 | ** from the beginning toward the end. In other words, the cursor is |
drh | 5dad9a3 | 2014-07-25 18:37:42 +0000 | [diff] [blame] | 3767 | ** configured to use Next, not Prev. |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 3768 | ** |
drh | 935850e | 2014-05-24 17:15:15 +0000 | [diff] [blame] | 3769 | ** See also: Found, NotFound, SeekLt, SeekGe, SeekLe |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3770 | */ |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 3771 | /* Opcode: SeekLT P1 P2 P3 P4 * |
drh | f63552b | 2013-10-30 00:25:03 +0000 | [diff] [blame] | 3772 | ** Synopsis: key=r[P3@P4] |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 3773 | ** |
danielk1977 | b790c6c | 2008-04-18 10:25:24 +0000 | [diff] [blame] | 3774 | ** 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] | 3775 | ** use the value in register P3 as a key. If cursor P1 refers |
danielk1977 | b790c6c | 2008-04-18 10:25:24 +0000 | [diff] [blame] | 3776 | ** to an SQL index, then P3 is the first in an array of P4 registers |
| 3777 | ** that are used as an unpacked index key. |
| 3778 | ** |
| 3779 | ** Reposition cursor P1 so that it points to the largest entry that |
| 3780 | ** is less than the key value. If there are no records less than |
| 3781 | ** the key and P2 is not zero, then jump to P2. |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 3782 | ** |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 3783 | ** This opcode leaves the cursor configured to move in reverse order, |
| 3784 | ** from the end toward the beginning. In other words, the cursor is |
drh | 5dad9a3 | 2014-07-25 18:37:42 +0000 | [diff] [blame] | 3785 | ** configured to use Prev, not Next. |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 3786 | ** |
drh | 935850e | 2014-05-24 17:15:15 +0000 | [diff] [blame] | 3787 | ** See also: Found, NotFound, SeekGt, SeekGe, SeekLe |
drh | 7cf6e4d | 2004-05-19 14:56:55 +0000 | [diff] [blame] | 3788 | */ |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 3789 | /* Opcode: SeekLE P1 P2 P3 P4 * |
drh | f63552b | 2013-10-30 00:25:03 +0000 | [diff] [blame] | 3790 | ** Synopsis: key=r[P3@P4] |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 3791 | ** |
danielk1977 | b790c6c | 2008-04-18 10:25:24 +0000 | [diff] [blame] | 3792 | ** 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] | 3793 | ** use the value in register P3 as a key. If cursor P1 refers |
danielk1977 | b790c6c | 2008-04-18 10:25:24 +0000 | [diff] [blame] | 3794 | ** to an SQL index, then P3 is the first in an array of P4 registers |
| 3795 | ** that are used as an unpacked index key. |
danielk1977 | 751de56 | 2008-04-18 09:01:15 +0000 | [diff] [blame] | 3796 | ** |
danielk1977 | b790c6c | 2008-04-18 10:25:24 +0000 | [diff] [blame] | 3797 | ** Reposition cursor P1 so that it points to the largest entry that |
| 3798 | ** is less than or equal to the key value. If there are no records |
| 3799 | ** 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] | 3800 | ** |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 3801 | ** This opcode leaves the cursor configured to move in reverse order, |
| 3802 | ** from the end toward the beginning. In other words, the cursor is |
drh | 5dad9a3 | 2014-07-25 18:37:42 +0000 | [diff] [blame] | 3803 | ** configured to use Prev, not Next. |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 3804 | ** |
drh | b1d607d | 2015-11-05 22:30:54 +0000 | [diff] [blame] | 3805 | ** If the cursor P1 was opened using the OPFLAG_SEEKEQ flag, then this |
| 3806 | ** opcode will always land on a record that equally equals the key, or |
| 3807 | ** else jump immediately to P2. When the cursor is OPFLAG_SEEKEQ, this |
| 3808 | ** opcode must be followed by an IdxGE opcode with the same arguments. |
| 3809 | ** The IdxGE opcode will be skipped if this opcode succeeds, but the |
| 3810 | ** IdxGE opcode will be used on subsequent loop iterations. |
| 3811 | ** |
drh | 935850e | 2014-05-24 17:15:15 +0000 | [diff] [blame] | 3812 | ** See also: Found, NotFound, SeekGt, SeekGe, SeekLt |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 3813 | */ |
drh | 4a1d365 | 2014-02-14 15:13:36 +0000 | [diff] [blame] | 3814 | case OP_SeekLT: /* jump, in3 */ |
| 3815 | case OP_SeekLE: /* jump, in3 */ |
| 3816 | case OP_SeekGE: /* jump, in3 */ |
| 3817 | case OP_SeekGT: { /* jump, in3 */ |
drh | b1d607d | 2015-11-05 22:30:54 +0000 | [diff] [blame] | 3818 | int res; /* Comparison result */ |
| 3819 | int oc; /* Opcode */ |
| 3820 | VdbeCursor *pC; /* The cursor to seek */ |
| 3821 | UnpackedRecord r; /* The key to seek for */ |
| 3822 | int nField; /* Number of columns or fields in the key */ |
| 3823 | i64 iKey; /* The rowid we are to seek to */ |
drh | d6b7946 | 2015-11-07 01:19:00 +0000 | [diff] [blame] | 3824 | int eqOnly; /* Only interested in == results */ |
drh | 80ff32f | 2001-11-04 18:32:46 +0000 | [diff] [blame] | 3825 | |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 3826 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
drh | 959403f | 2008-12-12 17:56:16 +0000 | [diff] [blame] | 3827 | assert( pOp->p2!=0 ); |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 3828 | pC = p->apCsr[pOp->p1]; |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 3829 | assert( pC!=0 ); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 3830 | assert( pC->eCurType==CURTYPE_BTREE ); |
drh | 4a1d365 | 2014-02-14 15:13:36 +0000 | [diff] [blame] | 3831 | assert( OP_SeekLE == OP_SeekLT+1 ); |
| 3832 | assert( OP_SeekGE == OP_SeekLT+2 ); |
| 3833 | assert( OP_SeekGT == OP_SeekLT+3 ); |
drh | d4187c7 | 2010-08-30 22:15:45 +0000 | [diff] [blame] | 3834 | assert( pC->isOrdered ); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 3835 | assert( pC->uc.pCursor!=0 ); |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 3836 | oc = pOp->opcode; |
drh | d6b7946 | 2015-11-07 01:19:00 +0000 | [diff] [blame] | 3837 | eqOnly = 0; |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 3838 | pC->nullRow = 0; |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 3839 | #ifdef SQLITE_DEBUG |
| 3840 | pC->seekOp = pOp->opcode; |
| 3841 | #endif |
drh | e0997b3 | 2015-03-20 14:57:50 +0000 | [diff] [blame] | 3842 | |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 3843 | if( pC->isTable ){ |
drh | d6b7946 | 2015-11-07 01:19:00 +0000 | [diff] [blame] | 3844 | /* The BTREE_SEEK_EQ flag is only set on index cursors */ |
drh | 218c66e | 2016-12-27 12:35:36 +0000 | [diff] [blame] | 3845 | assert( sqlite3BtreeCursorHasHint(pC->uc.pCursor, BTREE_SEEK_EQ)==0 |
| 3846 | || CORRUPT_DB ); |
drh | d6b7946 | 2015-11-07 01:19:00 +0000 | [diff] [blame] | 3847 | |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 3848 | /* The input value in P3 might be of any type: integer, real, string, |
| 3849 | ** 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] | 3850 | ** the seek, so convert it. */ |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 3851 | pIn3 = &aMem[pOp->p3]; |
drh | 11a6eee | 2014-09-19 22:01:54 +0000 | [diff] [blame] | 3852 | if( (pIn3->flags & (MEM_Int|MEM_Real|MEM_Str))==MEM_Str ){ |
drh | bd9507c | 2014-08-23 17:21:37 +0000 | [diff] [blame] | 3853 | applyNumericAffinity(pIn3, 0); |
| 3854 | } |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 3855 | iKey = sqlite3VdbeIntValue(pIn3); |
drh | 959403f | 2008-12-12 17:56:16 +0000 | [diff] [blame] | 3856 | |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 3857 | /* If the P3 value could not be converted into an integer without |
| 3858 | ** loss of information, then special processing is required... */ |
| 3859 | if( (pIn3->flags & MEM_Int)==0 ){ |
| 3860 | if( (pIn3->flags & MEM_Real)==0 ){ |
| 3861 | /* If the P3 value cannot be converted into any kind of a number, |
| 3862 | ** then the seek is not possible, so jump to P2 */ |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 3863 | VdbeBranchTaken(1,2); goto jump_to_p2; |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 3864 | break; |
| 3865 | } |
drh | 959403f | 2008-12-12 17:56:16 +0000 | [diff] [blame] | 3866 | |
dan | aa1776f | 2013-11-26 18:22:59 +0000 | [diff] [blame] | 3867 | /* If the approximation iKey is larger than the actual real search |
| 3868 | ** term, substitute >= for > and < for <=. e.g. if the search term |
| 3869 | ** is 4.9 and the integer approximation 5: |
| 3870 | ** |
| 3871 | ** (x > 4.9) -> (x >= 5) |
| 3872 | ** (x <= 4.9) -> (x < 5) |
| 3873 | */ |
drh | 74eaba4 | 2014-09-18 17:52:15 +0000 | [diff] [blame] | 3874 | if( pIn3->u.r<(double)iKey ){ |
drh | 4a1d365 | 2014-02-14 15:13:36 +0000 | [diff] [blame] | 3875 | assert( OP_SeekGE==(OP_SeekGT-1) ); |
| 3876 | assert( OP_SeekLT==(OP_SeekLE-1) ); |
| 3877 | assert( (OP_SeekLE & 0x0001)==(OP_SeekGT & 0x0001) ); |
| 3878 | if( (oc & 0x0001)==(OP_SeekGT & 0x0001) ) oc--; |
dan | aa1776f | 2013-11-26 18:22:59 +0000 | [diff] [blame] | 3879 | } |
| 3880 | |
| 3881 | /* If the approximation iKey is smaller than the actual real search |
| 3882 | ** term, substitute <= for < and > for >=. */ |
drh | 74eaba4 | 2014-09-18 17:52:15 +0000 | [diff] [blame] | 3883 | else if( pIn3->u.r>(double)iKey ){ |
drh | 4a1d365 | 2014-02-14 15:13:36 +0000 | [diff] [blame] | 3884 | assert( OP_SeekLE==(OP_SeekLT+1) ); |
| 3885 | assert( OP_SeekGT==(OP_SeekGE+1) ); |
| 3886 | assert( (OP_SeekLT & 0x0001)==(OP_SeekGE & 0x0001) ); |
| 3887 | if( (oc & 0x0001)==(OP_SeekLT & 0x0001) ) oc++; |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 3888 | } |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 3889 | } |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 3890 | rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, 0, (u64)iKey, 0, &res); |
drh | b53a5a9 | 2014-10-12 22:37:22 +0000 | [diff] [blame] | 3891 | pC->movetoTarget = iKey; /* Used by OP_Delete */ |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 3892 | if( rc!=SQLITE_OK ){ |
| 3893 | goto abort_due_to_error; |
drh | 1af3fdb | 2004-07-18 21:33:01 +0000 | [diff] [blame] | 3894 | } |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 3895 | }else{ |
drh | d6b7946 | 2015-11-07 01:19:00 +0000 | [diff] [blame] | 3896 | /* For a cursor with the BTREE_SEEK_EQ hint, only the OP_SeekGE and |
| 3897 | ** OP_SeekLE opcodes are allowed, and these must be immediately followed |
| 3898 | ** by an OP_IdxGT or OP_IdxLT opcode, respectively, with the same key. |
| 3899 | */ |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 3900 | if( sqlite3BtreeCursorHasHint(pC->uc.pCursor, BTREE_SEEK_EQ) ){ |
drh | d6b7946 | 2015-11-07 01:19:00 +0000 | [diff] [blame] | 3901 | eqOnly = 1; |
| 3902 | assert( pOp->opcode==OP_SeekGE || pOp->opcode==OP_SeekLE ); |
| 3903 | assert( pOp[1].opcode==OP_IdxLT || pOp[1].opcode==OP_IdxGT ); |
| 3904 | assert( pOp[1].p1==pOp[0].p1 ); |
| 3905 | assert( pOp[1].p2==pOp[0].p2 ); |
| 3906 | assert( pOp[1].p3==pOp[0].p3 ); |
| 3907 | assert( pOp[1].p4.i==pOp[0].p4.i ); |
| 3908 | } |
| 3909 | |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 3910 | nField = pOp->p4.i; |
| 3911 | assert( pOp->p4type==P4_INT32 ); |
| 3912 | assert( nField>0 ); |
| 3913 | r.pKeyInfo = pC->pKeyInfo; |
| 3914 | r.nField = (u16)nField; |
| 3915 | |
| 3916 | /* The next line of code computes as follows, only faster: |
drh | 4a1d365 | 2014-02-14 15:13:36 +0000 | [diff] [blame] | 3917 | ** if( oc==OP_SeekGT || oc==OP_SeekLE ){ |
dan | 1fed5da | 2014-02-25 21:01:25 +0000 | [diff] [blame] | 3918 | ** r.default_rc = -1; |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 3919 | ** }else{ |
dan | 1fed5da | 2014-02-25 21:01:25 +0000 | [diff] [blame] | 3920 | ** r.default_rc = +1; |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 3921 | ** } |
danielk1977 | f7b9d66 | 2008-06-23 18:49:43 +0000 | [diff] [blame] | 3922 | */ |
dan | 1fed5da | 2014-02-25 21:01:25 +0000 | [diff] [blame] | 3923 | r.default_rc = ((1 & (oc - OP_SeekLT)) ? -1 : +1); |
| 3924 | assert( oc!=OP_SeekGT || r.default_rc==-1 ); |
| 3925 | assert( oc!=OP_SeekLE || r.default_rc==-1 ); |
| 3926 | assert( oc!=OP_SeekGE || r.default_rc==+1 ); |
| 3927 | assert( oc!=OP_SeekLT || r.default_rc==+1 ); |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 3928 | |
| 3929 | r.aMem = &aMem[pOp->p3]; |
| 3930 | #ifdef SQLITE_DEBUG |
| 3931 | { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); } |
| 3932 | #endif |
drh | 70528d7 | 2015-11-05 20:25:09 +0000 | [diff] [blame] | 3933 | r.eqSeen = 0; |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 3934 | rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, &r, 0, 0, &res); |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 3935 | if( rc!=SQLITE_OK ){ |
| 3936 | goto abort_due_to_error; |
| 3937 | } |
drh | b1d607d | 2015-11-05 22:30:54 +0000 | [diff] [blame] | 3938 | if( eqOnly && r.eqSeen==0 ){ |
| 3939 | assert( res!=0 ); |
| 3940 | goto seek_not_found; |
drh | 70528d7 | 2015-11-05 20:25:09 +0000 | [diff] [blame] | 3941 | } |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 3942 | } |
| 3943 | pC->deferredMoveto = 0; |
| 3944 | pC->cacheStatus = CACHE_STALE; |
| 3945 | #ifdef SQLITE_TEST |
| 3946 | sqlite3_search_count++; |
| 3947 | #endif |
drh | 4a1d365 | 2014-02-14 15:13:36 +0000 | [diff] [blame] | 3948 | if( oc>=OP_SeekGE ){ assert( oc==OP_SeekGE || oc==OP_SeekGT ); |
| 3949 | if( res<0 || (res==0 && oc==OP_SeekGT) ){ |
drh | e39a732 | 2014-02-03 14:04:11 +0000 | [diff] [blame] | 3950 | res = 0; |
drh | 2ab792e | 2017-05-30 18:34:07 +0000 | [diff] [blame] | 3951 | rc = sqlite3BtreeNext(pC->uc.pCursor, 0); |
| 3952 | if( rc!=SQLITE_OK ){ |
| 3953 | if( rc==SQLITE_DONE ){ |
| 3954 | rc = SQLITE_OK; |
| 3955 | res = 1; |
| 3956 | }else{ |
| 3957 | goto abort_due_to_error; |
| 3958 | } |
| 3959 | } |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 3960 | }else{ |
| 3961 | res = 0; |
| 3962 | } |
| 3963 | }else{ |
drh | 4a1d365 | 2014-02-14 15:13:36 +0000 | [diff] [blame] | 3964 | assert( oc==OP_SeekLT || oc==OP_SeekLE ); |
| 3965 | if( res>0 || (res==0 && oc==OP_SeekLT) ){ |
drh | e39a732 | 2014-02-03 14:04:11 +0000 | [diff] [blame] | 3966 | res = 0; |
drh | 2ab792e | 2017-05-30 18:34:07 +0000 | [diff] [blame] | 3967 | rc = sqlite3BtreePrevious(pC->uc.pCursor, 0); |
| 3968 | if( rc!=SQLITE_OK ){ |
| 3969 | if( rc==SQLITE_DONE ){ |
| 3970 | rc = SQLITE_OK; |
| 3971 | res = 1; |
| 3972 | }else{ |
| 3973 | goto abort_due_to_error; |
| 3974 | } |
| 3975 | } |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 3976 | }else{ |
| 3977 | /* res might be negative because the table is empty. Check to |
| 3978 | ** see if this is the case. |
| 3979 | */ |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 3980 | res = sqlite3BtreeEof(pC->uc.pCursor); |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 3981 | } |
| 3982 | } |
drh | b1d607d | 2015-11-05 22:30:54 +0000 | [diff] [blame] | 3983 | seek_not_found: |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 3984 | assert( pOp->p2>0 ); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 3985 | VdbeBranchTaken(res!=0,2); |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 3986 | if( res ){ |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 3987 | goto jump_to_p2; |
drh | b1d607d | 2015-11-05 22:30:54 +0000 | [diff] [blame] | 3988 | }else if( eqOnly ){ |
| 3989 | assert( pOp[1].opcode==OP_IdxLT || pOp[1].opcode==OP_IdxGT ); |
| 3990 | pOp++; /* Skip the OP_IdxLt or OP_IdxGT that follows */ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3991 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3992 | break; |
| 3993 | } |
dan | 71c57db | 2016-07-09 20:23:55 +0000 | [diff] [blame] | 3994 | |
drh | 8cff69d | 2009-11-12 19:59:44 +0000 | [diff] [blame] | 3995 | /* Opcode: Found P1 P2 P3 P4 * |
drh | f63552b | 2013-10-30 00:25:03 +0000 | [diff] [blame] | 3996 | ** Synopsis: key=r[P3@P4] |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3997 | ** |
drh | 8cff69d | 2009-11-12 19:59:44 +0000 | [diff] [blame] | 3998 | ** If P4==0 then register P3 holds a blob constructed by MakeRecord. If |
| 3999 | ** P4>0 then register P3 is the first of P4 registers that form an unpacked |
| 4000 | ** record. |
| 4001 | ** |
| 4002 | ** Cursor P1 is on an index btree. If the record identified by P3 and P4 |
| 4003 | ** 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] | 4004 | ** P1 is left pointing at the matching entry. |
drh | 6f225d0 | 2013-10-26 13:36:51 +0000 | [diff] [blame] | 4005 | ** |
drh | cefc87f | 2014-08-01 01:40:33 +0000 | [diff] [blame] | 4006 | ** This operation leaves the cursor in a state where it can be |
| 4007 | ** advanced in the forward direction. The Next instruction will work, |
| 4008 | ** but not the Prev instruction. |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 4009 | ** |
drh | 6f225d0 | 2013-10-26 13:36:51 +0000 | [diff] [blame] | 4010 | ** See also: NotFound, NoConflict, NotExists. SeekGe |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4011 | */ |
drh | 8cff69d | 2009-11-12 19:59:44 +0000 | [diff] [blame] | 4012 | /* Opcode: NotFound P1 P2 P3 P4 * |
drh | f63552b | 2013-10-30 00:25:03 +0000 | [diff] [blame] | 4013 | ** Synopsis: key=r[P3@P4] |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4014 | ** |
drh | 8cff69d | 2009-11-12 19:59:44 +0000 | [diff] [blame] | 4015 | ** If P4==0 then register P3 holds a blob constructed by MakeRecord. If |
| 4016 | ** P4>0 then register P3 is the first of P4 registers that form an unpacked |
| 4017 | ** record. |
| 4018 | ** |
| 4019 | ** Cursor P1 is on an index btree. If the record identified by P3 and P4 |
| 4020 | ** is not the prefix of any entry in P1 then a jump is made to P2. If P1 |
| 4021 | ** does contain an entry whose prefix matches the P3/P4 record then control |
| 4022 | ** falls through to the next instruction and P1 is left pointing at the |
| 4023 | ** matching entry. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4024 | ** |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 4025 | ** This operation leaves the cursor in a state where it cannot be |
| 4026 | ** advanced in either direction. In other words, the Next and Prev |
| 4027 | ** opcodes do not work after this operation. |
| 4028 | ** |
drh | 6f225d0 | 2013-10-26 13:36:51 +0000 | [diff] [blame] | 4029 | ** See also: Found, NotExists, NoConflict |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4030 | */ |
drh | 6f225d0 | 2013-10-26 13:36:51 +0000 | [diff] [blame] | 4031 | /* Opcode: NoConflict P1 P2 P3 P4 * |
drh | 4af5bee | 2013-10-30 02:37:50 +0000 | [diff] [blame] | 4032 | ** Synopsis: key=r[P3@P4] |
drh | 6f225d0 | 2013-10-26 13:36:51 +0000 | [diff] [blame] | 4033 | ** |
| 4034 | ** If P4==0 then register P3 holds a blob constructed by MakeRecord. If |
| 4035 | ** P4>0 then register P3 is the first of P4 registers that form an unpacked |
| 4036 | ** record. |
| 4037 | ** |
| 4038 | ** Cursor P1 is on an index btree. If the record identified by P3 and P4 |
| 4039 | ** contains any NULL value, jump immediately to P2. If all terms of the |
| 4040 | ** record are not-NULL then a check is done to determine if any row in the |
| 4041 | ** P1 index btree has a matching key prefix. If there are no matches, jump |
| 4042 | ** immediately to P2. If there is a match, fall through and leave the P1 |
| 4043 | ** cursor pointing to the matching row. |
| 4044 | ** |
| 4045 | ** This opcode is similar to OP_NotFound with the exceptions that the |
| 4046 | ** branch is always taken if any part of the search key input is NULL. |
| 4047 | ** |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 4048 | ** This operation leaves the cursor in a state where it cannot be |
| 4049 | ** advanced in either direction. In other words, the Next and Prev |
| 4050 | ** opcodes do not work after this operation. |
| 4051 | ** |
drh | 6f225d0 | 2013-10-26 13:36:51 +0000 | [diff] [blame] | 4052 | ** See also: NotFound, Found, NotExists |
| 4053 | */ |
| 4054 | case OP_NoConflict: /* jump, in3 */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4055 | case OP_NotFound: /* jump, in3 */ |
| 4056 | case OP_Found: { /* jump, in3 */ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 4057 | int alreadyExists; |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 4058 | int takeJump; |
drh | 6f225d0 | 2013-10-26 13:36:51 +0000 | [diff] [blame] | 4059 | int ii; |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 4060 | VdbeCursor *pC; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 4061 | int res; |
drh | a582b01 | 2016-12-21 19:45:54 +0000 | [diff] [blame] | 4062 | UnpackedRecord *pFree; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 4063 | UnpackedRecord *pIdxKey; |
drh | 8cff69d | 2009-11-12 19:59:44 +0000 | [diff] [blame] | 4064 | UnpackedRecord r; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 4065 | |
dan | 0ff297e | 2009-09-25 17:03:14 +0000 | [diff] [blame] | 4066 | #ifdef SQLITE_TEST |
drh | 6f225d0 | 2013-10-26 13:36:51 +0000 | [diff] [blame] | 4067 | if( pOp->opcode!=OP_NoConflict ) sqlite3_found_count++; |
dan | 0ff297e | 2009-09-25 17:03:14 +0000 | [diff] [blame] | 4068 | #endif |
| 4069 | |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 4070 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
drh | 8cff69d | 2009-11-12 19:59:44 +0000 | [diff] [blame] | 4071 | assert( pOp->p4type==P4_INT32 ); |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 4072 | pC = p->apCsr[pOp->p1]; |
| 4073 | assert( pC!=0 ); |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 4074 | #ifdef SQLITE_DEBUG |
drh | cefc87f | 2014-08-01 01:40:33 +0000 | [diff] [blame] | 4075 | pC->seekOp = pOp->opcode; |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 4076 | #endif |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 4077 | pIn3 = &aMem[pOp->p3]; |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 4078 | assert( pC->eCurType==CURTYPE_BTREE ); |
| 4079 | assert( pC->uc.pCursor!=0 ); |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 4080 | assert( pC->isTable==0 ); |
| 4081 | if( pOp->p4.i>0 ){ |
| 4082 | r.pKeyInfo = pC->pKeyInfo; |
| 4083 | r.nField = (u16)pOp->p4.i; |
| 4084 | r.aMem = pIn3; |
drh | 8aaf7bc | 2016-09-20 01:19:18 +0000 | [diff] [blame] | 4085 | #ifdef SQLITE_DEBUG |
drh | 826af37 | 2014-02-08 19:12:21 +0000 | [diff] [blame] | 4086 | for(ii=0; ii<r.nField; ii++){ |
| 4087 | assert( memIsValid(&r.aMem[ii]) ); |
drh | 8aaf7bc | 2016-09-20 01:19:18 +0000 | [diff] [blame] | 4088 | assert( (r.aMem[ii].flags & MEM_Zero)==0 || r.aMem[ii].n==0 ); |
drh | 826af37 | 2014-02-08 19:12:21 +0000 | [diff] [blame] | 4089 | if( ii ) REGISTER_TRACE(pOp->p3+ii, &r.aMem[ii]); |
drh | 826af37 | 2014-02-08 19:12:21 +0000 | [diff] [blame] | 4090 | } |
drh | 8aaf7bc | 2016-09-20 01:19:18 +0000 | [diff] [blame] | 4091 | #endif |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 4092 | pIdxKey = &r; |
drh | a582b01 | 2016-12-21 19:45:54 +0000 | [diff] [blame] | 4093 | pFree = 0; |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 4094 | }else{ |
drh | e46515b | 2017-05-19 22:51:00 +0000 | [diff] [blame] | 4095 | assert( pIn3->flags & MEM_Blob ); |
| 4096 | rc = ExpandBlob(pIn3); |
| 4097 | assert( rc==SQLITE_OK || rc==SQLITE_NOMEM ); |
| 4098 | if( rc ) goto no_mem; |
drh | a582b01 | 2016-12-21 19:45:54 +0000 | [diff] [blame] | 4099 | pFree = pIdxKey = sqlite3VdbeAllocUnpackedRecord(pC->pKeyInfo); |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 4100 | if( pIdxKey==0 ) goto no_mem; |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 4101 | sqlite3VdbeRecordUnpack(pC->pKeyInfo, pIn3->n, pIn3->z, pIdxKey); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4102 | } |
dan | 1fed5da | 2014-02-25 21:01:25 +0000 | [diff] [blame] | 4103 | pIdxKey->default_rc = 0; |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 4104 | takeJump = 0; |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 4105 | if( pOp->opcode==OP_NoConflict ){ |
| 4106 | /* For the OP_NoConflict opcode, take the jump if any of the |
| 4107 | ** input fields are NULL, since any key with a NULL will not |
| 4108 | ** conflict */ |
mistachkin | 7bb6e8e | 2015-01-12 18:52:41 +0000 | [diff] [blame] | 4109 | for(ii=0; ii<pIdxKey->nField; ii++){ |
| 4110 | if( pIdxKey->aMem[ii].flags & MEM_Null ){ |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 4111 | takeJump = 1; |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 4112 | break; |
drh | 6f225d0 | 2013-10-26 13:36:51 +0000 | [diff] [blame] | 4113 | } |
| 4114 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4115 | } |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 4116 | rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, pIdxKey, 0, 0, &res); |
drh | dbd6a7d | 2017-04-05 12:39:49 +0000 | [diff] [blame] | 4117 | if( pFree ) sqlite3DbFreeNN(db, pFree); |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 4118 | if( rc!=SQLITE_OK ){ |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 4119 | goto abort_due_to_error; |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 4120 | } |
| 4121 | pC->seekResult = res; |
| 4122 | alreadyExists = (res==0); |
| 4123 | pC->nullRow = 1-alreadyExists; |
| 4124 | pC->deferredMoveto = 0; |
| 4125 | pC->cacheStatus = CACHE_STALE; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4126 | if( pOp->opcode==OP_Found ){ |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 4127 | VdbeBranchTaken(alreadyExists!=0,2); |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 4128 | if( alreadyExists ) goto jump_to_p2; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4129 | }else{ |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 4130 | VdbeBranchTaken(takeJump||alreadyExists==0,2); |
| 4131 | if( takeJump || !alreadyExists ) goto jump_to_p2; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4132 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4133 | break; |
| 4134 | } |
| 4135 | |
drh | eeb9565 | 2016-05-26 20:56:38 +0000 | [diff] [blame] | 4136 | /* Opcode: SeekRowid P1 P2 P3 * * |
| 4137 | ** Synopsis: intkey=r[P3] |
| 4138 | ** |
| 4139 | ** P1 is the index of a cursor open on an SQL table btree (with integer |
| 4140 | ** keys). If register P3 does not contain an integer or if P1 does not |
| 4141 | ** contain a record with rowid P3 then jump immediately to P2. |
| 4142 | ** Or, if P2 is 0, raise an SQLITE_CORRUPT error. If P1 does contain |
| 4143 | ** a record with rowid P3 then |
| 4144 | ** leave the cursor pointing at that record and fall through to the next |
| 4145 | ** instruction. |
| 4146 | ** |
| 4147 | ** The OP_NotExists opcode performs the same operation, but with OP_NotExists |
| 4148 | ** the P3 register must be guaranteed to contain an integer value. With this |
| 4149 | ** opcode, register P3 might not contain an integer. |
| 4150 | ** |
| 4151 | ** The OP_NotFound opcode performs the same operation on index btrees |
| 4152 | ** (with arbitrary multi-value keys). |
| 4153 | ** |
| 4154 | ** This opcode leaves the cursor in a state where it cannot be advanced |
| 4155 | ** in either direction. In other words, the Next and Prev opcodes will |
| 4156 | ** not work following this opcode. |
| 4157 | ** |
| 4158 | ** See also: Found, NotFound, NoConflict, SeekRowid |
| 4159 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4160 | /* Opcode: NotExists P1 P2 P3 * * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 4161 | ** Synopsis: intkey=r[P3] |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 4162 | ** |
drh | 261c02d | 2013-10-25 14:46:15 +0000 | [diff] [blame] | 4163 | ** P1 is the index of a cursor open on an SQL table btree (with integer |
| 4164 | ** 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] | 4165 | ** rowid P3 then jump immediately to P2. Or, if P2 is 0, raise an |
| 4166 | ** SQLITE_CORRUPT error. If P1 does contain a record with rowid P3 then |
| 4167 | ** leave the cursor pointing at that record and fall through to the next |
| 4168 | ** instruction. |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 4169 | ** |
drh | eeb9565 | 2016-05-26 20:56:38 +0000 | [diff] [blame] | 4170 | ** The OP_SeekRowid opcode performs the same operation but also allows the |
| 4171 | ** P3 register to contain a non-integer value, in which case the jump is |
| 4172 | ** always taken. This opcode requires that P3 always contain an integer. |
| 4173 | ** |
drh | 261c02d | 2013-10-25 14:46:15 +0000 | [diff] [blame] | 4174 | ** The OP_NotFound opcode performs the same operation on index btrees |
| 4175 | ** (with arbitrary multi-value keys). |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 4176 | ** |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 4177 | ** This opcode leaves the cursor in a state where it cannot be advanced |
| 4178 | ** in either direction. In other words, the Next and Prev opcodes will |
| 4179 | ** not work following this opcode. |
| 4180 | ** |
drh | eeb9565 | 2016-05-26 20:56:38 +0000 | [diff] [blame] | 4181 | ** See also: Found, NotFound, NoConflict, SeekRowid |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 4182 | */ |
drh | eeb9565 | 2016-05-26 20:56:38 +0000 | [diff] [blame] | 4183 | case OP_SeekRowid: { /* jump, in3 */ |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 4184 | VdbeCursor *pC; |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 4185 | BtCursor *pCrsr; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 4186 | int res; |
| 4187 | u64 iKey; |
| 4188 | |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 4189 | pIn3 = &aMem[pOp->p3]; |
drh | eeb9565 | 2016-05-26 20:56:38 +0000 | [diff] [blame] | 4190 | if( (pIn3->flags & MEM_Int)==0 ){ |
| 4191 | applyAffinity(pIn3, SQLITE_AFF_NUMERIC, encoding); |
| 4192 | if( (pIn3->flags & MEM_Int)==0 ) goto jump_to_p2; |
| 4193 | } |
| 4194 | /* Fall through into OP_NotExists */ |
| 4195 | case OP_NotExists: /* jump, in3 */ |
| 4196 | pIn3 = &aMem[pOp->p3]; |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 4197 | assert( pIn3->flags & MEM_Int ); |
| 4198 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 4199 | pC = p->apCsr[pOp->p1]; |
| 4200 | assert( pC!=0 ); |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 4201 | #ifdef SQLITE_DEBUG |
| 4202 | pC->seekOp = 0; |
| 4203 | #endif |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 4204 | assert( pC->isTable ); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 4205 | assert( pC->eCurType==CURTYPE_BTREE ); |
| 4206 | pCrsr = pC->uc.pCursor; |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 4207 | assert( pCrsr!=0 ); |
| 4208 | res = 0; |
| 4209 | iKey = pIn3->u.i; |
| 4210 | rc = sqlite3BtreeMovetoUnpacked(pCrsr, 0, iKey, 0, &res); |
drh | b79d552 | 2015-09-14 19:26:37 +0000 | [diff] [blame] | 4211 | assert( rc==SQLITE_OK || res==0 ); |
drh | b53a5a9 | 2014-10-12 22:37:22 +0000 | [diff] [blame] | 4212 | pC->movetoTarget = iKey; /* Used by OP_Delete */ |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 4213 | pC->nullRow = 0; |
| 4214 | pC->cacheStatus = CACHE_STALE; |
| 4215 | pC->deferredMoveto = 0; |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 4216 | VdbeBranchTaken(res!=0,2); |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 4217 | pC->seekResult = res; |
dan | c6157e1 | 2015-09-14 09:23:47 +0000 | [diff] [blame] | 4218 | if( res!=0 ){ |
drh | b79d552 | 2015-09-14 19:26:37 +0000 | [diff] [blame] | 4219 | assert( rc==SQLITE_OK ); |
| 4220 | if( pOp->p2==0 ){ |
| 4221 | rc = SQLITE_CORRUPT_BKPT; |
| 4222 | }else{ |
| 4223 | goto jump_to_p2; |
| 4224 | } |
dan | c6157e1 | 2015-09-14 09:23:47 +0000 | [diff] [blame] | 4225 | } |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 4226 | if( rc ) goto abort_due_to_error; |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 4227 | break; |
| 4228 | } |
| 4229 | |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 4230 | /* Opcode: Sequence P1 P2 * * * |
drh | 079a307 | 2014-03-19 14:10:55 +0000 | [diff] [blame] | 4231 | ** Synopsis: r[P2]=cursor[P1].ctr++ |
drh | 4db38a7 | 2005-09-01 12:16:28 +0000 | [diff] [blame] | 4232 | ** |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 4233 | ** Find the next available sequence number for cursor P1. |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4234 | ** Write the sequence number into register P2. |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 4235 | ** The sequence number on the cursor is incremented after this |
| 4236 | ** instruction. |
drh | 4db38a7 | 2005-09-01 12:16:28 +0000 | [diff] [blame] | 4237 | */ |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 4238 | case OP_Sequence: { /* out2 */ |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 4239 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 4240 | assert( p->apCsr[pOp->p1]!=0 ); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 4241 | assert( p->apCsr[pOp->p1]->eCurType!=CURTYPE_VTAB ); |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 4242 | pOut = out2Prerelease(p, pOp); |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 4243 | pOut->u.i = p->apCsr[pOp->p1]->seqCount++; |
drh | 4db38a7 | 2005-09-01 12:16:28 +0000 | [diff] [blame] | 4244 | break; |
| 4245 | } |
| 4246 | |
| 4247 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4248 | /* Opcode: NewRowid P1 P2 P3 * * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 4249 | ** Synopsis: r[P2]=rowid |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4250 | ** |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 4251 | ** 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] | 4252 | ** The record number is not previously used as a key in the database |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4253 | ** table that cursor P1 points to. The new record number is written |
| 4254 | ** written to register P2. |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 4255 | ** |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 4256 | ** If P3>0 then P3 is a register in the root frame of this VDBE that holds |
| 4257 | ** the largest previously generated record number. No new record numbers are |
| 4258 | ** allowed to be less than this value. When this value reaches its maximum, |
drh | ef8662b | 2011-06-20 21:47:58 +0000 | [diff] [blame] | 4259 | ** an SQLITE_FULL error is generated. The P3 register is updated with the ' |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 4260 | ** generated record number. This P3 mechanism is used to help implement the |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 4261 | ** AUTOINCREMENT feature. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4262 | */ |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 4263 | case OP_NewRowid: { /* out2 */ |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 4264 | i64 v; /* The new rowid */ |
| 4265 | VdbeCursor *pC; /* Cursor of table to get the new rowid */ |
| 4266 | int res; /* Result of an sqlite3BtreeLast() */ |
| 4267 | int cnt; /* Counter to limit the number of searches */ |
| 4268 | Mem *pMem; /* Register holding largest rowid for AUTOINCREMENT */ |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 4269 | VdbeFrame *pFrame; /* Root frame of VDBE */ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 4270 | |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 4271 | v = 0; |
| 4272 | res = 0; |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 4273 | pOut = out2Prerelease(p, pOp); |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 4274 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 4275 | pC = p->apCsr[pOp->p1]; |
| 4276 | assert( pC!=0 ); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 4277 | assert( pC->eCurType==CURTYPE_BTREE ); |
| 4278 | assert( pC->uc.pCursor!=0 ); |
drh | 98ef0f6 | 2015-06-30 01:25:52 +0000 | [diff] [blame] | 4279 | { |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 4280 | /* The next rowid or record number (different terms for the same |
| 4281 | ** thing) is obtained in a two-step algorithm. |
| 4282 | ** |
| 4283 | ** First we attempt to find the largest existing rowid and add one |
| 4284 | ** to that. But if the largest existing rowid is already the maximum |
| 4285 | ** positive integer, we have to fall through to the second |
| 4286 | ** probabilistic algorithm |
| 4287 | ** |
| 4288 | ** The second algorithm is to select a rowid at random and see if |
| 4289 | ** it already exists in the table. If it does not exist, we have |
| 4290 | ** succeeded. If the random rowid does exist, we select a new one |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 4291 | ** and try again, up to 100 times. |
drh | db5ed6d | 2001-09-18 22:17:44 +0000 | [diff] [blame] | 4292 | */ |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 4293 | assert( pC->isTable ); |
drh | fe2093d | 2005-01-20 22:48:47 +0000 | [diff] [blame] | 4294 | |
drh | 75f86a4 | 2005-02-17 00:03:06 +0000 | [diff] [blame] | 4295 | #ifdef SQLITE_32BIT_ROWID |
| 4296 | # define MAX_ROWID 0x7fffffff |
| 4297 | #else |
drh | fe2093d | 2005-01-20 22:48:47 +0000 | [diff] [blame] | 4298 | /* Some compilers complain about constants of the form 0x7fffffffffffffff. |
| 4299 | ** Others complain about 0x7ffffffffffffffffLL. The following macro seems |
| 4300 | ** to provide the constant while making all compilers happy. |
| 4301 | */ |
danielk1977 | 64202cf | 2008-11-17 15:31:47 +0000 | [diff] [blame] | 4302 | # define MAX_ROWID (i64)( (((u64)0x7fffffff)<<32) | (u64)0xffffffff ) |
drh | 75f86a4 | 2005-02-17 00:03:06 +0000 | [diff] [blame] | 4303 | #endif |
drh | fe2093d | 2005-01-20 22:48:47 +0000 | [diff] [blame] | 4304 | |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 4305 | if( !pC->useRandomRowid ){ |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 4306 | rc = sqlite3BtreeLast(pC->uc.pCursor, &res); |
drh | e0670b6 | 2014-02-12 21:31:12 +0000 | [diff] [blame] | 4307 | if( rc!=SQLITE_OK ){ |
| 4308 | goto abort_due_to_error; |
| 4309 | } |
| 4310 | if( res ){ |
| 4311 | v = 1; /* IMP: R-61914-48074 */ |
| 4312 | }else{ |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 4313 | assert( sqlite3BtreeCursorIsValid(pC->uc.pCursor) ); |
drh | a7c90c4 | 2016-06-04 20:37:10 +0000 | [diff] [blame] | 4314 | v = sqlite3BtreeIntegerKey(pC->uc.pCursor); |
drh | e0670b6 | 2014-02-12 21:31:12 +0000 | [diff] [blame] | 4315 | if( v>=MAX_ROWID ){ |
| 4316 | pC->useRandomRowid = 1; |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 4317 | }else{ |
drh | e0670b6 | 2014-02-12 21:31:12 +0000 | [diff] [blame] | 4318 | v++; /* IMP: R-29538-34987 */ |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 4319 | } |
drh | 3fc190c | 2001-09-14 03:24:23 +0000 | [diff] [blame] | 4320 | } |
drh | e0670b6 | 2014-02-12 21:31:12 +0000 | [diff] [blame] | 4321 | } |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 4322 | |
| 4323 | #ifndef SQLITE_OMIT_AUTOINCREMENT |
drh | e0670b6 | 2014-02-12 21:31:12 +0000 | [diff] [blame] | 4324 | if( pOp->p3 ){ |
| 4325 | /* Assert that P3 is a valid memory cell. */ |
| 4326 | assert( pOp->p3>0 ); |
| 4327 | if( p->pFrame ){ |
| 4328 | for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent); |
shane | abc6b89 | 2009-09-10 19:09:03 +0000 | [diff] [blame] | 4329 | /* Assert that P3 is a valid memory cell. */ |
drh | e0670b6 | 2014-02-12 21:31:12 +0000 | [diff] [blame] | 4330 | assert( pOp->p3<=pFrame->nMem ); |
| 4331 | pMem = &pFrame->aMem[pOp->p3]; |
| 4332 | }else{ |
| 4333 | /* Assert that P3 is a valid memory cell. */ |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 4334 | assert( pOp->p3<=(p->nMem+1 - p->nCursor) ); |
drh | e0670b6 | 2014-02-12 21:31:12 +0000 | [diff] [blame] | 4335 | pMem = &aMem[pOp->p3]; |
| 4336 | memAboutToChange(p, pMem); |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 4337 | } |
drh | e0670b6 | 2014-02-12 21:31:12 +0000 | [diff] [blame] | 4338 | assert( memIsValid(pMem) ); |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 4339 | |
drh | e0670b6 | 2014-02-12 21:31:12 +0000 | [diff] [blame] | 4340 | REGISTER_TRACE(pOp->p3, pMem); |
| 4341 | sqlite3VdbeMemIntegerify(pMem); |
| 4342 | assert( (pMem->flags & MEM_Int)!=0 ); /* mem(P3) holds an integer */ |
| 4343 | if( pMem->u.i==MAX_ROWID || pC->useRandomRowid ){ |
drh | e77caa1 | 2016-11-02 13:18:46 +0000 | [diff] [blame] | 4344 | rc = SQLITE_FULL; /* IMP: R-17817-00630 */ |
drh | e0670b6 | 2014-02-12 21:31:12 +0000 | [diff] [blame] | 4345 | goto abort_due_to_error; |
| 4346 | } |
| 4347 | if( v<pMem->u.i+1 ){ |
| 4348 | v = pMem->u.i + 1; |
| 4349 | } |
| 4350 | pMem->u.i = v; |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 4351 | } |
drh | e0670b6 | 2014-02-12 21:31:12 +0000 | [diff] [blame] | 4352 | #endif |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 4353 | if( pC->useRandomRowid ){ |
drh | 748a52c | 2010-09-01 11:50:08 +0000 | [diff] [blame] | 4354 | /* IMPLEMENTATION-OF: R-07677-41881 If the largest ROWID is equal to the |
drh | c79c761 | 2010-01-01 18:57:48 +0000 | [diff] [blame] | 4355 | ** largest possible integer (9223372036854775807) then the database |
drh | 748a52c | 2010-09-01 11:50:08 +0000 | [diff] [blame] | 4356 | ** engine starts picking positive candidate ROWIDs at random until |
| 4357 | ** it finds one that is not previously used. */ |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 4358 | assert( pOp->p3==0 ); /* We cannot be in random rowid mode if this is |
| 4359 | ** an AUTOINCREMENT table. */ |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 4360 | cnt = 0; |
drh | 2c4dc63 | 2014-09-25 12:31:28 +0000 | [diff] [blame] | 4361 | do{ |
| 4362 | sqlite3_randomness(sizeof(v), &v); |
drh | d863346 | 2014-09-25 17:42:41 +0000 | [diff] [blame] | 4363 | v &= (MAX_ROWID>>1); v++; /* Ensure that v is greater than zero */ |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 4364 | }while( ((rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, 0, (u64)v, |
drh | 748a52c | 2010-09-01 11:50:08 +0000 | [diff] [blame] | 4365 | 0, &res))==SQLITE_OK) |
shaneh | c4d340a | 2010-09-01 02:37:56 +0000 | [diff] [blame] | 4366 | && (res==0) |
drh | 2c4dc63 | 2014-09-25 12:31:28 +0000 | [diff] [blame] | 4367 | && (++cnt<100)); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 4368 | if( rc ) goto abort_due_to_error; |
| 4369 | if( res==0 ){ |
drh | c79c761 | 2010-01-01 18:57:48 +0000 | [diff] [blame] | 4370 | rc = SQLITE_FULL; /* IMP: R-38219-53002 */ |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 4371 | goto abort_due_to_error; |
| 4372 | } |
drh | 748a52c | 2010-09-01 11:50:08 +0000 | [diff] [blame] | 4373 | assert( v>0 ); /* EV: R-40812-03570 */ |
drh | 1eaa269 | 2001-09-18 02:02:23 +0000 | [diff] [blame] | 4374 | } |
drh | a11846b | 2004-01-07 18:52:56 +0000 | [diff] [blame] | 4375 | pC->deferredMoveto = 0; |
drh | 76873ab | 2006-01-07 18:48:26 +0000 | [diff] [blame] | 4376 | pC->cacheStatus = CACHE_STALE; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4377 | } |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 4378 | pOut->u.i = v; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4379 | break; |
| 4380 | } |
| 4381 | |
danielk1977 | 1f4aa33 | 2008-01-03 09:51:55 +0000 | [diff] [blame] | 4382 | /* Opcode: Insert P1 P2 P3 P4 P5 |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 4383 | ** Synopsis: intkey=r[P3] data=r[P2] |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4384 | ** |
jplyon | 5a56422 | 2003-06-02 06:15:58 +0000 | [diff] [blame] | 4385 | ** Write an entry into the table of cursor P1. A new entry is |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 4386 | ** created if it doesn't already exist or the data for an existing |
drh | 3e9ca09 | 2009-09-08 01:14:48 +0000 | [diff] [blame] | 4387 | ** entry is overwritten. The data is the value MEM_Blob stored in register |
danielk1977 | 1f4aa33 | 2008-01-03 09:51:55 +0000 | [diff] [blame] | 4388 | ** number P2. The key is stored in register P3. The key must |
drh | 3e9ca09 | 2009-09-08 01:14:48 +0000 | [diff] [blame] | 4389 | ** be a MEM_Int. |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 4390 | ** |
danielk1977 | 1f4aa33 | 2008-01-03 09:51:55 +0000 | [diff] [blame] | 4391 | ** If the OPFLAG_NCHANGE flag of P5 is set, then the row change count is |
| 4392 | ** incremented (otherwise not). If the OPFLAG_LASTROWID flag of P5 is set, |
danielk1977 | b28af71 | 2004-06-21 06:50:26 +0000 | [diff] [blame] | 4393 | ** then rowid is stored for subsequent return by the |
drh | 85b623f | 2007-12-13 21:54:09 +0000 | [diff] [blame] | 4394 | ** sqlite3_last_insert_rowid() function (otherwise it is unmodified). |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 4395 | ** |
drh | eaf6ae2 | 2016-11-09 20:14:34 +0000 | [diff] [blame] | 4396 | ** If the OPFLAG_USESEEKRESULT flag of P5 is set, the implementation might |
| 4397 | ** run faster by avoiding an unnecessary seek on cursor P1. However, |
| 4398 | ** the OPFLAG_USESEEKRESULT flag must only be set if there have been no prior |
| 4399 | ** 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] | 4400 | ** |
| 4401 | ** If the OPFLAG_ISUPDATE flag is set, then this opcode is part of an |
| 4402 | ** UPDATE operation. Otherwise (if the flag is clear) then this opcode |
| 4403 | ** is part of an INSERT operation. The difference is only important to |
| 4404 | ** the update hook. |
| 4405 | ** |
dan | 319eeb7 | 2011-03-19 08:38:50 +0000 | [diff] [blame] | 4406 | ** Parameter P4 may point to a Table structure, or may be NULL. If it is |
| 4407 | ** not NULL, then the update-hook (sqlite3.xUpdateCallback) is invoked |
| 4408 | ** following a successful insert. |
danielk1977 | 1f6eec5 | 2006-06-16 06:17:47 +0000 | [diff] [blame] | 4409 | ** |
drh | 93aed5a | 2008-01-16 17:46:38 +0000 | [diff] [blame] | 4410 | ** (WARNING/TODO: If P1 is a pseudo-cursor and P2 is dynamically |
| 4411 | ** allocated, then ownership of P2 is transferred to the pseudo-cursor |
| 4412 | ** and register P2 becomes ephemeral. If the cursor is changed, the |
| 4413 | ** value of register P2 will then change. Make sure this does not |
| 4414 | ** cause any problems.) |
| 4415 | ** |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 4416 | ** This instruction only works on tables. The equivalent instruction |
| 4417 | ** for indices is OP_IdxInsert. |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 4418 | */ |
drh | e05c929 | 2009-10-29 13:48:10 +0000 | [diff] [blame] | 4419 | /* Opcode: InsertInt P1 P2 P3 P4 P5 |
drh | 72e26de | 2016-08-24 21:24:04 +0000 | [diff] [blame] | 4420 | ** Synopsis: intkey=P3 data=r[P2] |
drh | e05c929 | 2009-10-29 13:48:10 +0000 | [diff] [blame] | 4421 | ** |
| 4422 | ** This works exactly like OP_Insert except that the key is the |
| 4423 | ** integer value P3, not the value of the integer stored in register P3. |
| 4424 | */ |
| 4425 | case OP_Insert: |
| 4426 | case OP_InsertInt: { |
drh | 3e9ca09 | 2009-09-08 01:14:48 +0000 | [diff] [blame] | 4427 | Mem *pData; /* MEM cell holding data for the record to be inserted */ |
| 4428 | Mem *pKey; /* MEM cell holding key for the record */ |
drh | 3e9ca09 | 2009-09-08 01:14:48 +0000 | [diff] [blame] | 4429 | VdbeCursor *pC; /* Cursor to table into which insert is written */ |
drh | 3e9ca09 | 2009-09-08 01:14:48 +0000 | [diff] [blame] | 4430 | int seekResult; /* Result of prior seek or 0 if no USESEEKRESULT flag */ |
| 4431 | const char *zDb; /* database name - used by the update hook */ |
dan | 319eeb7 | 2011-03-19 08:38:50 +0000 | [diff] [blame] | 4432 | Table *pTab; /* Table structure - used by update and pre-update hooks */ |
drh | 8eeb446 | 2016-05-21 20:03:42 +0000 | [diff] [blame] | 4433 | BtreePayload x; /* Payload to be inserted */ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 4434 | |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 4435 | pData = &aMem[pOp->p2]; |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 4436 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 4437 | assert( memIsValid(pData) ); |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 4438 | pC = p->apCsr[pOp->p1]; |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 4439 | assert( pC!=0 ); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 4440 | assert( pC->eCurType==CURTYPE_BTREE ); |
| 4441 | assert( pC->uc.pCursor!=0 ); |
dan | cb9a364 | 2017-01-30 19:44:53 +0000 | [diff] [blame] | 4442 | assert( (pOp->p5 & OPFLAG_ISNOOP) || pC->isTable ); |
drh | cbf1b8e | 2013-11-11 22:55:26 +0000 | [diff] [blame] | 4443 | assert( pOp->p4type==P4_TABLE || pOp->p4type>=P4_STATIC ); |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 4444 | REGISTER_TRACE(pOp->p2, pData); |
danielk1977 | 5f8d8a8 | 2004-05-11 00:28:42 +0000 | [diff] [blame] | 4445 | |
drh | e05c929 | 2009-10-29 13:48:10 +0000 | [diff] [blame] | 4446 | if( pOp->opcode==OP_Insert ){ |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 4447 | pKey = &aMem[pOp->p3]; |
drh | e05c929 | 2009-10-29 13:48:10 +0000 | [diff] [blame] | 4448 | assert( pKey->flags & MEM_Int ); |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 4449 | assert( memIsValid(pKey) ); |
drh | e05c929 | 2009-10-29 13:48:10 +0000 | [diff] [blame] | 4450 | REGISTER_TRACE(pOp->p3, pKey); |
drh | 8eeb446 | 2016-05-21 20:03:42 +0000 | [diff] [blame] | 4451 | x.nKey = pKey->u.i; |
drh | e05c929 | 2009-10-29 13:48:10 +0000 | [diff] [blame] | 4452 | }else{ |
| 4453 | assert( pOp->opcode==OP_InsertInt ); |
drh | 8eeb446 | 2016-05-21 20:03:42 +0000 | [diff] [blame] | 4454 | x.nKey = pOp->p3; |
drh | e05c929 | 2009-10-29 13:48:10 +0000 | [diff] [blame] | 4455 | } |
| 4456 | |
drh | 9b1c62d | 2011-03-30 21:04:43 +0000 | [diff] [blame] | 4457 | if( pOp->p4type==P4_TABLE && HAS_UPDATE_HOOK(db) ){ |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 4458 | assert( pC->iDb>=0 ); |
drh | 69c3382 | 2016-08-18 14:33:11 +0000 | [diff] [blame] | 4459 | zDb = db->aDb[pC->iDb].zDbSName; |
dan | 319eeb7 | 2011-03-19 08:38:50 +0000 | [diff] [blame] | 4460 | pTab = pOp->p4.pTab; |
dan | cb9a364 | 2017-01-30 19:44:53 +0000 | [diff] [blame] | 4461 | assert( (pOp->p5 & OPFLAG_ISNOOP) || HasRowid(pTab) ); |
drh | 74c3302 | 2016-03-30 12:56:55 +0000 | [diff] [blame] | 4462 | }else{ |
drh | 4ec6f3a | 2018-01-12 19:33:18 +0000 | [diff] [blame] | 4463 | pTab = 0; |
drh | 74c3302 | 2016-03-30 12:56:55 +0000 | [diff] [blame] | 4464 | zDb = 0; /* Not needed. Silence a compiler warning. */ |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 4465 | } |
| 4466 | |
drh | 9b1c62d | 2011-03-30 21:04:43 +0000 | [diff] [blame] | 4467 | #ifdef SQLITE_ENABLE_PREUPDATE_HOOK |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 4468 | /* Invoke the pre-update hook, if any */ |
drh | 4ec6f3a | 2018-01-12 19:33:18 +0000 | [diff] [blame] | 4469 | if( pTab ){ |
drh | 84ebe2b | 2018-01-12 18:46:52 +0000 | [diff] [blame] | 4470 | if( db->xPreUpdateCallback && !(pOp->p5 & OPFLAG_ISUPDATE) ){ |
| 4471 | sqlite3VdbePreUpdateHook(p, pC, SQLITE_INSERT, zDb, pTab, x.nKey,pOp->p2); |
| 4472 | } |
drh | 4ec6f3a | 2018-01-12 19:33:18 +0000 | [diff] [blame] | 4473 | if( db->xUpdateCallback==0 || pTab->aCol==0 ){ |
| 4474 | /* Prevent post-update hook from running in cases when it should not */ |
| 4475 | pTab = 0; |
drh | 84ebe2b | 2018-01-12 18:46:52 +0000 | [diff] [blame] | 4476 | } |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 4477 | } |
dan | cb9a364 | 2017-01-30 19:44:53 +0000 | [diff] [blame] | 4478 | if( pOp->p5 & OPFLAG_ISNOOP ) break; |
drh | 9b1c62d | 2011-03-30 21:04:43 +0000 | [diff] [blame] | 4479 | #endif |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 4480 | |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 4481 | if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++; |
drh | fae58d5 | 2017-01-26 17:26:44 +0000 | [diff] [blame] | 4482 | if( pOp->p5 & OPFLAG_LASTROWID ) db->lastRowid = x.nKey; |
dan | 21cd29a | 2017-10-23 16:03:54 +0000 | [diff] [blame] | 4483 | assert( pData->flags & (MEM_Blob|MEM_Str) ); |
| 4484 | x.pData = pData->z; |
| 4485 | x.nData = pData->n; |
drh | 3e9ca09 | 2009-09-08 01:14:48 +0000 | [diff] [blame] | 4486 | seekResult = ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0); |
| 4487 | if( pData->flags & MEM_Zero ){ |
drh | 8eeb446 | 2016-05-21 20:03:42 +0000 | [diff] [blame] | 4488 | x.nZero = pData->u.nZero; |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 4489 | }else{ |
drh | 8eeb446 | 2016-05-21 20:03:42 +0000 | [diff] [blame] | 4490 | x.nZero = 0; |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 4491 | } |
drh | 8eeb446 | 2016-05-21 20:03:42 +0000 | [diff] [blame] | 4492 | x.pKey = 0; |
| 4493 | rc = sqlite3BtreeInsert(pC->uc.pCursor, &x, |
dan | f91c131 | 2017-01-10 20:04:38 +0000 | [diff] [blame] | 4494 | (pOp->p5 & (OPFLAG_APPEND|OPFLAG_SAVEPOSITION)), seekResult |
drh | 3e9ca09 | 2009-09-08 01:14:48 +0000 | [diff] [blame] | 4495 | ); |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 4496 | pC->deferredMoveto = 0; |
| 4497 | pC->cacheStatus = CACHE_STALE; |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 +0000 | [diff] [blame] | 4498 | |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 4499 | /* Invoke the update-hook if required. */ |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 4500 | if( rc ) goto abort_due_to_error; |
drh | 4ec6f3a | 2018-01-12 19:33:18 +0000 | [diff] [blame] | 4501 | if( pTab ){ |
| 4502 | assert( db->xUpdateCallback!=0 ); |
| 4503 | assert( pTab->aCol!=0 ); |
| 4504 | db->xUpdateCallback(db->pUpdateArg, |
| 4505 | (pOp->p5 & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_INSERT, |
| 4506 | zDb, pTab->zName, x.nKey); |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 4507 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4508 | break; |
| 4509 | } |
| 4510 | |
dan | 438b881 | 2015-09-15 15:55:15 +0000 | [diff] [blame] | 4511 | /* Opcode: Delete P1 P2 P3 P4 P5 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4512 | ** |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 4513 | ** Delete the record at which the P1 cursor is currently pointing. |
| 4514 | ** |
drh | e807bdb | 2016-01-21 17:06:33 +0000 | [diff] [blame] | 4515 | ** If the OPFLAG_SAVEPOSITION bit of the P5 parameter is set, then |
| 4516 | ** the cursor will be left pointing at either the next or the previous |
| 4517 | ** record in the table. If it is left pointing at the next record, then |
| 4518 | ** the next Next instruction will be a no-op. As a result, in this case |
| 4519 | ** it is ok to delete a record from within a Next loop. If |
| 4520 | ** OPFLAG_SAVEPOSITION bit of P5 is clear, then the cursor will be |
| 4521 | ** left in an undefined state. |
drh | c8d30ac | 2002-04-12 10:08:59 +0000 | [diff] [blame] | 4522 | ** |
drh | def19e3 | 2016-01-27 16:26:25 +0000 | [diff] [blame] | 4523 | ** If the OPFLAG_AUXDELETE bit is set on P5, that indicates that this |
| 4524 | ** delete one of several associated with deleting a table row and all its |
| 4525 | ** associated index entries. Exactly one of those deletes is the "primary" |
| 4526 | ** delete. The others are all on OPFLAG_FORDELETE cursors or else are |
| 4527 | ** marked with the AUXDELETE flag. |
drh | e807bdb | 2016-01-21 17:06:33 +0000 | [diff] [blame] | 4528 | ** |
| 4529 | ** If the OPFLAG_NCHANGE flag of P2 (NB: P2 not P5) is set, then the row |
| 4530 | ** change count is incremented (otherwise not). |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 4531 | ** |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 4532 | ** P1 must not be pseudo-table. It has to be a real table with |
| 4533 | ** multiple rows. |
| 4534 | ** |
drh | 5e769a5 | 2016-09-28 16:05:53 +0000 | [diff] [blame] | 4535 | ** 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] | 4536 | ** the update or pre-update hook, or both, may be invoked. The P1 cursor must |
| 4537 | ** have been positioned using OP_NotFound prior to invoking this opcode in |
| 4538 | ** this case. Specifically, if one is configured, the pre-update hook is |
| 4539 | ** invoked if P4 is not NULL. The update-hook is invoked if one is configured, |
| 4540 | ** P4 is not NULL, and the OPFLAG_NCHANGE flag is set in P2. |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 4541 | ** |
| 4542 | ** If the OPFLAG_ISUPDATE flag is set in P2, then P3 contains the address |
| 4543 | ** of the memory cell that contains the value that the rowid of the row will |
| 4544 | ** be set to by the update. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4545 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4546 | case OP_Delete: { |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 4547 | VdbeCursor *pC; |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 4548 | const char *zDb; |
dan | 319eeb7 | 2011-03-19 08:38:50 +0000 | [diff] [blame] | 4549 | Table *pTab; |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 4550 | int opflags; |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 4551 | |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 4552 | opflags = pOp->p2; |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 4553 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 4554 | pC = p->apCsr[pOp->p1]; |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 4555 | assert( pC!=0 ); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 4556 | assert( pC->eCurType==CURTYPE_BTREE ); |
| 4557 | assert( pC->uc.pCursor!=0 ); |
drh | 9a65f2c | 2009-06-22 19:05:40 +0000 | [diff] [blame] | 4558 | assert( pC->deferredMoveto==0 ); |
drh | 9a65f2c | 2009-06-22 19:05:40 +0000 | [diff] [blame] | 4559 | |
drh | b53a5a9 | 2014-10-12 22:37:22 +0000 | [diff] [blame] | 4560 | #ifdef SQLITE_DEBUG |
dan | 438b881 | 2015-09-15 15:55:15 +0000 | [diff] [blame] | 4561 | if( pOp->p4type==P4_TABLE && HasRowid(pOp->p4.pTab) && pOp->p5==0 ){ |
| 4562 | /* If p5 is zero, the seek operation that positioned the cursor prior to |
| 4563 | ** OP_Delete will have also set the pC->movetoTarget field to the rowid of |
| 4564 | ** the row that is being deleted */ |
drh | a7c90c4 | 2016-06-04 20:37:10 +0000 | [diff] [blame] | 4565 | i64 iKey = sqlite3BtreeIntegerKey(pC->uc.pCursor); |
drh | 92fe38e | 2014-10-14 13:41:32 +0000 | [diff] [blame] | 4566 | assert( pC->movetoTarget==iKey ); |
drh | b53a5a9 | 2014-10-12 22:37:22 +0000 | [diff] [blame] | 4567 | } |
| 4568 | #endif |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 4569 | |
dan | 438b881 | 2015-09-15 15:55:15 +0000 | [diff] [blame] | 4570 | /* If the update-hook or pre-update-hook will be invoked, set zDb to |
| 4571 | ** the name of the db to pass as to it. Also set local pTab to a copy |
| 4572 | ** of p4.pTab. Finally, if p5 is true, indicating that this cursor was |
| 4573 | ** last moved with OP_Next or OP_Prev, not Seek or NotFound, set |
| 4574 | ** VdbeCursor.movetoTarget to the current rowid. */ |
drh | c556f3c | 2016-03-30 15:30:07 +0000 | [diff] [blame] | 4575 | if( pOp->p4type==P4_TABLE && HAS_UPDATE_HOOK(db) ){ |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 4576 | assert( pC->iDb>=0 ); |
drh | c556f3c | 2016-03-30 15:30:07 +0000 | [diff] [blame] | 4577 | assert( pOp->p4.pTab!=0 ); |
drh | 69c3382 | 2016-08-18 14:33:11 +0000 | [diff] [blame] | 4578 | zDb = db->aDb[pC->iDb].zDbSName; |
dan | 319eeb7 | 2011-03-19 08:38:50 +0000 | [diff] [blame] | 4579 | pTab = pOp->p4.pTab; |
drh | c556f3c | 2016-03-30 15:30:07 +0000 | [diff] [blame] | 4580 | if( (pOp->p5 & OPFLAG_SAVEPOSITION)!=0 && pC->isTable ){ |
drh | a7c90c4 | 2016-06-04 20:37:10 +0000 | [diff] [blame] | 4581 | pC->movetoTarget = sqlite3BtreeIntegerKey(pC->uc.pCursor); |
dan | 438b881 | 2015-09-15 15:55:15 +0000 | [diff] [blame] | 4582 | } |
drh | 74c3302 | 2016-03-30 12:56:55 +0000 | [diff] [blame] | 4583 | }else{ |
| 4584 | zDb = 0; /* Not needed. Silence a compiler warning. */ |
| 4585 | pTab = 0; /* Not needed. Silence a compiler warning. */ |
drh | 92fe38e | 2014-10-14 13:41:32 +0000 | [diff] [blame] | 4586 | } |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 4587 | |
drh | 9b1c62d | 2011-03-30 21:04:43 +0000 | [diff] [blame] | 4588 | #ifdef SQLITE_ENABLE_PREUPDATE_HOOK |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 4589 | /* Invoke the pre-update-hook if required. */ |
dan | cb9a364 | 2017-01-30 19:44:53 +0000 | [diff] [blame] | 4590 | if( db->xPreUpdateCallback && pOp->p4.pTab ){ |
| 4591 | assert( !(opflags & OPFLAG_ISUPDATE) |
| 4592 | || HasRowid(pTab)==0 |
| 4593 | || (aMem[pOp->p3].flags & MEM_Int) |
| 4594 | ); |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 4595 | sqlite3VdbePreUpdateHook(p, pC, |
| 4596 | (opflags & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_DELETE, |
drh | 92fe38e | 2014-10-14 13:41:32 +0000 | [diff] [blame] | 4597 | zDb, pTab, pC->movetoTarget, |
dan | 37db03b | 2011-03-16 19:59:18 +0000 | [diff] [blame] | 4598 | pOp->p3 |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 4599 | ); |
| 4600 | } |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 4601 | if( opflags & OPFLAG_ISNOOP ) break; |
drh | c556f3c | 2016-03-30 15:30:07 +0000 | [diff] [blame] | 4602 | #endif |
drh | b53a5a9 | 2014-10-12 22:37:22 +0000 | [diff] [blame] | 4603 | |
drh | def19e3 | 2016-01-27 16:26:25 +0000 | [diff] [blame] | 4604 | /* Only flags that can be set are SAVEPOISTION and AUXDELETE */ |
| 4605 | assert( (pOp->p5 & ~(OPFLAG_SAVEPOSITION|OPFLAG_AUXDELETE))==0 ); |
drh | e807bdb | 2016-01-21 17:06:33 +0000 | [diff] [blame] | 4606 | assert( OPFLAG_SAVEPOSITION==BTREE_SAVEPOSITION ); |
drh | def19e3 | 2016-01-27 16:26:25 +0000 | [diff] [blame] | 4607 | assert( OPFLAG_AUXDELETE==BTREE_AUXDELETE ); |
drh | b89aeb6 | 2016-01-27 15:49:32 +0000 | [diff] [blame] | 4608 | |
| 4609 | #ifdef SQLITE_DEBUG |
dan | e61bbf4 | 2016-01-28 17:06:17 +0000 | [diff] [blame] | 4610 | if( p->pFrame==0 ){ |
| 4611 | if( pC->isEphemeral==0 |
| 4612 | && (pOp->p5 & OPFLAG_AUXDELETE)==0 |
| 4613 | && (pC->wrFlag & OPFLAG_FORDELETE)==0 |
| 4614 | ){ |
| 4615 | nExtraDelete++; |
| 4616 | } |
| 4617 | if( pOp->p2 & OPFLAG_NCHANGE ){ |
| 4618 | nExtraDelete--; |
| 4619 | } |
drh | b89aeb6 | 2016-01-27 15:49:32 +0000 | [diff] [blame] | 4620 | } |
| 4621 | #endif |
| 4622 | |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 4623 | rc = sqlite3BtreeDelete(pC->uc.pCursor, pOp->p5); |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 4624 | pC->cacheStatus = CACHE_STALE; |
dan | 3b908d4 | 2016-11-08 19:22:32 +0000 | [diff] [blame] | 4625 | pC->seekResult = 0; |
drh | d3e1af4 | 2016-02-25 18:54:30 +0000 | [diff] [blame] | 4626 | if( rc ) goto abort_due_to_error; |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 +0000 | [diff] [blame] | 4627 | |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 4628 | /* Invoke the update-hook if required. */ |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 4629 | if( opflags & OPFLAG_NCHANGE ){ |
| 4630 | p->nChange++; |
drh | c556f3c | 2016-03-30 15:30:07 +0000 | [diff] [blame] | 4631 | if( db->xUpdateCallback && HasRowid(pTab) ){ |
drh | 92fe38e | 2014-10-14 13:41:32 +0000 | [diff] [blame] | 4632 | db->xUpdateCallback(db->pUpdateArg, SQLITE_DELETE, zDb, pTab->zName, |
dan | 438b881 | 2015-09-15 15:55:15 +0000 | [diff] [blame] | 4633 | pC->movetoTarget); |
| 4634 | assert( pC->iDb>=0 ); |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 4635 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4636 | } |
dan | 438b881 | 2015-09-15 15:55:15 +0000 | [diff] [blame] | 4637 | |
rdc | b0c374f | 2004-02-20 22:53:38 +0000 | [diff] [blame] | 4638 | break; |
| 4639 | } |
drh | b7f1d9a | 2009-09-08 02:27:58 +0000 | [diff] [blame] | 4640 | /* Opcode: ResetCount * * * * * |
rdc | b0c374f | 2004-02-20 22:53:38 +0000 | [diff] [blame] | 4641 | ** |
drh | b7f1d9a | 2009-09-08 02:27:58 +0000 | [diff] [blame] | 4642 | ** The value of the change counter is copied to the database handle |
| 4643 | ** change counter (returned by subsequent calls to sqlite3_changes()). |
| 4644 | ** Then the VMs internal change counter resets to 0. |
| 4645 | ** This is used by trigger programs. |
rdc | b0c374f | 2004-02-20 22:53:38 +0000 | [diff] [blame] | 4646 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4647 | case OP_ResetCount: { |
drh | b7f1d9a | 2009-09-08 02:27:58 +0000 | [diff] [blame] | 4648 | sqlite3VdbeSetChanges(db, p->nChange); |
danielk1977 | b28af71 | 2004-06-21 06:50:26 +0000 | [diff] [blame] | 4649 | p->nChange = 0; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4650 | break; |
| 4651 | } |
| 4652 | |
drh | 1153c7b | 2013-11-01 22:02:56 +0000 | [diff] [blame] | 4653 | /* Opcode: SorterCompare P1 P2 P3 P4 |
drh | 72e26de | 2016-08-24 21:24:04 +0000 | [diff] [blame] | 4654 | ** Synopsis: if key(P1)!=trim(r[P3],P4) goto P2 |
dan | 5134d13 | 2011-09-02 10:31:11 +0000 | [diff] [blame] | 4655 | ** |
drh | 1153c7b | 2013-11-01 22:02:56 +0000 | [diff] [blame] | 4656 | ** P1 is a sorter cursor. This instruction compares a prefix of the |
drh | bc5cf38 | 2014-08-06 01:08:07 +0000 | [diff] [blame] | 4657 | ** record blob in register P3 against a prefix of the entry that |
drh | ac50232 | 2014-07-30 13:56:48 +0000 | [diff] [blame] | 4658 | ** the sorter cursor currently points to. Only the first P4 fields |
| 4659 | ** of r[P3] and the sorter record are compared. |
drh | 1153c7b | 2013-11-01 22:02:56 +0000 | [diff] [blame] | 4660 | ** |
| 4661 | ** If either P3 or the sorter contains a NULL in one of their significant |
| 4662 | ** fields (not counting the P4 fields at the end which are ignored) then |
| 4663 | ** the comparison is assumed to be equal. |
| 4664 | ** |
| 4665 | ** Fall through to next instruction if the two records compare equal to |
| 4666 | ** each other. Jump to P2 if they are different. |
dan | 5134d13 | 2011-09-02 10:31:11 +0000 | [diff] [blame] | 4667 | */ |
| 4668 | case OP_SorterCompare: { |
| 4669 | VdbeCursor *pC; |
| 4670 | int res; |
drh | ac50232 | 2014-07-30 13:56:48 +0000 | [diff] [blame] | 4671 | int nKeyCol; |
dan | 5134d13 | 2011-09-02 10:31:11 +0000 | [diff] [blame] | 4672 | |
| 4673 | pC = p->apCsr[pOp->p1]; |
| 4674 | assert( isSorter(pC) ); |
drh | 1153c7b | 2013-11-01 22:02:56 +0000 | [diff] [blame] | 4675 | assert( pOp->p4type==P4_INT32 ); |
dan | 5134d13 | 2011-09-02 10:31:11 +0000 | [diff] [blame] | 4676 | pIn3 = &aMem[pOp->p3]; |
drh | ac50232 | 2014-07-30 13:56:48 +0000 | [diff] [blame] | 4677 | nKeyCol = pOp->p4.i; |
drh | 958d261 | 2014-04-18 13:40:07 +0000 | [diff] [blame] | 4678 | res = 0; |
drh | ac50232 | 2014-07-30 13:56:48 +0000 | [diff] [blame] | 4679 | rc = sqlite3VdbeSorterCompare(pC, pIn3, nKeyCol, &res); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 4680 | VdbeBranchTaken(res!=0,2); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 4681 | if( rc ) goto abort_due_to_error; |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 4682 | if( res ) goto jump_to_p2; |
dan | 5134d13 | 2011-09-02 10:31:11 +0000 | [diff] [blame] | 4683 | break; |
| 4684 | }; |
| 4685 | |
drh | 6cf4a7d | 2014-10-13 13:00:58 +0000 | [diff] [blame] | 4686 | /* Opcode: SorterData P1 P2 P3 * * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 4687 | ** Synopsis: r[P2]=data |
dan | 5134d13 | 2011-09-02 10:31:11 +0000 | [diff] [blame] | 4688 | ** |
| 4689 | ** Write into register P2 the current sorter data for sorter cursor P1. |
drh | 6cf4a7d | 2014-10-13 13:00:58 +0000 | [diff] [blame] | 4690 | ** Then clear the column header cache on cursor P3. |
| 4691 | ** |
| 4692 | ** This opcode is normally use to move a record out of the sorter and into |
| 4693 | ** a register that is the source for a pseudo-table cursor created using |
| 4694 | ** OpenPseudo. That pseudo-table cursor is the one that is identified by |
| 4695 | ** parameter P3. Clearing the P3 column cache as part of this opcode saves |
| 4696 | ** us from having to issue a separate NullRow instruction to clear that cache. |
dan | 5134d13 | 2011-09-02 10:31:11 +0000 | [diff] [blame] | 4697 | */ |
| 4698 | case OP_SorterData: { |
| 4699 | VdbeCursor *pC; |
drh | 3a94987 | 2012-09-18 13:20:13 +0000 | [diff] [blame] | 4700 | |
dan | 5134d13 | 2011-09-02 10:31:11 +0000 | [diff] [blame] | 4701 | pOut = &aMem[pOp->p2]; |
| 4702 | pC = p->apCsr[pOp->p1]; |
drh | 14da87f | 2013-11-20 21:51:33 +0000 | [diff] [blame] | 4703 | assert( isSorter(pC) ); |
dan | 5134d13 | 2011-09-02 10:31:11 +0000 | [diff] [blame] | 4704 | rc = sqlite3VdbeSorterRowkey(pC, pOut); |
dan | 3852413 | 2014-05-01 20:26:48 +0000 | [diff] [blame] | 4705 | assert( rc!=SQLITE_OK || (pOut->flags & MEM_Blob) ); |
drh | 6cf4a7d | 2014-10-13 13:00:58 +0000 | [diff] [blame] | 4706 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 4707 | if( rc ) goto abort_due_to_error; |
drh | 6cf4a7d | 2014-10-13 13:00:58 +0000 | [diff] [blame] | 4708 | p->apCsr[pOp->p3]->cacheStatus = CACHE_STALE; |
dan | 5134d13 | 2011-09-02 10:31:11 +0000 | [diff] [blame] | 4709 | break; |
| 4710 | } |
| 4711 | |
drh | e7b554d | 2017-01-09 15:44:25 +0000 | [diff] [blame] | 4712 | /* Opcode: RowData P1 P2 P3 * * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 4713 | ** Synopsis: r[P2]=data |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 4714 | ** |
drh | 9057fc7 | 2016-11-25 19:32:32 +0000 | [diff] [blame] | 4715 | ** Write into register P2 the complete row content for the row at |
| 4716 | ** which cursor P1 is currently pointing. |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4717 | ** There is no interpretation of the data. |
| 4718 | ** It is just copied onto the P2 register exactly as |
danielk1977 | 96cb76f | 2008-01-04 13:24:28 +0000 | [diff] [blame] | 4719 | ** it is found in the database file. |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 4720 | ** |
drh | 9057fc7 | 2016-11-25 19:32:32 +0000 | [diff] [blame] | 4721 | ** If cursor P1 is an index, then the content is the key of the row. |
| 4722 | ** If cursor P2 is a table, then the content extracted is the data. |
drh | 143f3c4 | 2004-01-07 20:37:52 +0000 | [diff] [blame] | 4723 | ** |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 4724 | ** If the P1 cursor must be pointing to a valid row (not a NULL row) |
| 4725 | ** of a real table, not a pseudo-table. |
drh | e7b554d | 2017-01-09 15:44:25 +0000 | [diff] [blame] | 4726 | ** |
| 4727 | ** If P3!=0 then this opcode is allowed to make an ephermeral pointer |
| 4728 | ** into the database page. That means that the content of the output |
| 4729 | ** register will be invalidated as soon as the cursor moves - including |
| 4730 | ** moves caused by other cursors that "save" the the current cursors |
| 4731 | ** position in order that they can write to the same table. If P3==0 |
| 4732 | ** then a copy of the data is made into memory. P3!=0 is faster, but |
| 4733 | ** P3==0 is safer. |
| 4734 | ** |
| 4735 | ** If P3!=0 then the content of the P2 register is unsuitable for use |
| 4736 | ** in OP_Result and any OP_Result will invalidate the P2 register content. |
mistachkin | ab61cf7 | 2017-01-09 18:22:54 +0000 | [diff] [blame] | 4737 | ** The P2 register content is invalidated by opcodes like OP_Function or |
drh | e7b554d | 2017-01-09 15:44:25 +0000 | [diff] [blame] | 4738 | ** by any use of another cursor pointing to the same table. |
drh | 143f3c4 | 2004-01-07 20:37:52 +0000 | [diff] [blame] | 4739 | */ |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 4740 | case OP_RowData: { |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 4741 | VdbeCursor *pC; |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 4742 | BtCursor *pCrsr; |
danielk1977 | e0d4b06 | 2004-06-28 01:11:46 +0000 | [diff] [blame] | 4743 | u32 n; |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 4744 | |
drh | e7b554d | 2017-01-09 15:44:25 +0000 | [diff] [blame] | 4745 | pOut = out2Prerelease(p, pOp); |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 4746 | |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 4747 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 4748 | pC = p->apCsr[pOp->p1]; |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 4749 | assert( pC!=0 ); |
| 4750 | assert( pC->eCurType==CURTYPE_BTREE ); |
drh | 14da87f | 2013-11-20 21:51:33 +0000 | [diff] [blame] | 4751 | assert( isSorter(pC)==0 ); |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 4752 | assert( pC->nullRow==0 ); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 4753 | assert( pC->uc.pCursor!=0 ); |
| 4754 | pCrsr = pC->uc.pCursor; |
drh | 9a65f2c | 2009-06-22 19:05:40 +0000 | [diff] [blame] | 4755 | |
drh | 9057fc7 | 2016-11-25 19:32:32 +0000 | [diff] [blame] | 4756 | /* The OP_RowData opcodes always follow OP_NotExists or |
drh | eeb9565 | 2016-05-26 20:56:38 +0000 | [diff] [blame] | 4757 | ** OP_SeekRowid or OP_Rewind/Op_Next with no intervening instructions |
| 4758 | ** that might invalidate the cursor. |
| 4759 | ** If this where not the case, on of the following assert()s |
drh | c22284f | 2014-10-13 16:02:20 +0000 | [diff] [blame] | 4760 | ** would fail. Should this ever change (because of changes in the code |
| 4761 | ** generator) then the fix would be to insert a call to |
| 4762 | ** sqlite3VdbeCursorMoveto(). |
drh | 9a65f2c | 2009-06-22 19:05:40 +0000 | [diff] [blame] | 4763 | */ |
| 4764 | assert( pC->deferredMoveto==0 ); |
drh | c22284f | 2014-10-13 16:02:20 +0000 | [diff] [blame] | 4765 | assert( sqlite3BtreeCursorIsValid(pCrsr) ); |
| 4766 | #if 0 /* Not required due to the previous to assert() statements */ |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 4767 | rc = sqlite3VdbeCursorMoveto(pC); |
drh | c22284f | 2014-10-13 16:02:20 +0000 | [diff] [blame] | 4768 | if( rc!=SQLITE_OK ) goto abort_due_to_error; |
| 4769 | #endif |
drh | 9a65f2c | 2009-06-22 19:05:40 +0000 | [diff] [blame] | 4770 | |
drh | a7c90c4 | 2016-06-04 20:37:10 +0000 | [diff] [blame] | 4771 | n = sqlite3BtreePayloadSize(pCrsr); |
drh | d66c4f8 | 2016-06-04 20:58:35 +0000 | [diff] [blame] | 4772 | if( n>(u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){ |
drh | a7c90c4 | 2016-06-04 20:37:10 +0000 | [diff] [blame] | 4773 | goto too_big; |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 4774 | } |
drh | 722246e | 2014-10-07 23:02:24 +0000 | [diff] [blame] | 4775 | testcase( n==0 ); |
drh | e7b554d | 2017-01-09 15:44:25 +0000 | [diff] [blame] | 4776 | rc = sqlite3VdbeMemFromBtree(pCrsr, 0, n, pOut); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 4777 | if( rc ) goto abort_due_to_error; |
drh | e7b554d | 2017-01-09 15:44:25 +0000 | [diff] [blame] | 4778 | if( !pOp->p3 ) Deephemeralize(pOut); |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 4779 | UPDATE_MAX_BLOBSIZE(pOut); |
drh | ee0ec8e | 2013-10-31 17:38:01 +0000 | [diff] [blame] | 4780 | REGISTER_TRACE(pOp->p2, pOut); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4781 | break; |
| 4782 | } |
| 4783 | |
drh | 2133d82 | 2008-01-03 18:44:59 +0000 | [diff] [blame] | 4784 | /* Opcode: Rowid P1 P2 * * * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 4785 | ** Synopsis: r[P2]=rowid |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4786 | ** |
drh | 2133d82 | 2008-01-03 18:44:59 +0000 | [diff] [blame] | 4787 | ** 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] | 4788 | ** P1 is currently point to. |
drh | 044925b | 2009-04-22 17:15:02 +0000 | [diff] [blame] | 4789 | ** |
| 4790 | ** P1 can be either an ordinary table or a virtual table. There used to |
| 4791 | ** be a separate OP_VRowid opcode for use with virtual tables, but this |
| 4792 | ** one opcode now works for both table types. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4793 | */ |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 4794 | case OP_Rowid: { /* out2 */ |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 4795 | VdbeCursor *pC; |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 4796 | i64 v; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 4797 | sqlite3_vtab *pVtab; |
| 4798 | const sqlite3_module *pModule; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4799 | |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 4800 | pOut = out2Prerelease(p, pOp); |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 4801 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 4802 | pC = p->apCsr[pOp->p1]; |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 4803 | assert( pC!=0 ); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 4804 | assert( pC->eCurType!=CURTYPE_PSEUDO || pC->nullRow ); |
drh | 044925b | 2009-04-22 17:15:02 +0000 | [diff] [blame] | 4805 | if( pC->nullRow ){ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 4806 | pOut->flags = MEM_Null; |
drh | 044925b | 2009-04-22 17:15:02 +0000 | [diff] [blame] | 4807 | break; |
| 4808 | }else if( pC->deferredMoveto ){ |
drh | 6149526 | 2009-04-22 15:32:59 +0000 | [diff] [blame] | 4809 | v = pC->movetoTarget; |
drh | 044925b | 2009-04-22 17:15:02 +0000 | [diff] [blame] | 4810 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 4811 | }else if( pC->eCurType==CURTYPE_VTAB ){ |
| 4812 | assert( pC->uc.pVCur!=0 ); |
| 4813 | pVtab = pC->uc.pVCur->pVtab; |
drh | 044925b | 2009-04-22 17:15:02 +0000 | [diff] [blame] | 4814 | pModule = pVtab->pModule; |
| 4815 | assert( pModule->xRowid ); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 4816 | rc = pModule->xRowid(pC->uc.pVCur, &v); |
dan | 016f781 | 2013-08-21 17:35:48 +0000 | [diff] [blame] | 4817 | sqlite3VtabImportErrmsg(p, pVtab); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 4818 | if( rc ) goto abort_due_to_error; |
drh | 044925b | 2009-04-22 17:15:02 +0000 | [diff] [blame] | 4819 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 4820 | }else{ |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 4821 | assert( pC->eCurType==CURTYPE_BTREE ); |
| 4822 | assert( pC->uc.pCursor!=0 ); |
drh | c22284f | 2014-10-13 16:02:20 +0000 | [diff] [blame] | 4823 | rc = sqlite3VdbeCursorRestore(pC); |
drh | 6149526 | 2009-04-22 15:32:59 +0000 | [diff] [blame] | 4824 | if( rc ) goto abort_due_to_error; |
dan | 2b8669a | 2014-11-17 19:42:48 +0000 | [diff] [blame] | 4825 | if( pC->nullRow ){ |
| 4826 | pOut->flags = MEM_Null; |
| 4827 | break; |
| 4828 | } |
drh | a7c90c4 | 2016-06-04 20:37:10 +0000 | [diff] [blame] | 4829 | v = sqlite3BtreeIntegerKey(pC->uc.pCursor); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4830 | } |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 4831 | pOut->u.i = v; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4832 | break; |
| 4833 | } |
| 4834 | |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4835 | /* Opcode: NullRow P1 * * * * |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 4836 | ** |
| 4837 | ** Move the cursor P1 to a null row. Any OP_Column operations |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4838 | ** that occur while the cursor is on the null row will always |
| 4839 | ** write a NULL. |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 4840 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4841 | case OP_NullRow: { |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 4842 | VdbeCursor *pC; |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 4843 | |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 4844 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 4845 | pC = p->apCsr[pOp->p1]; |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 4846 | assert( pC!=0 ); |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame] | 4847 | pC->nullRow = 1; |
drh | 399af1d | 2013-11-20 17:25:55 +0000 | [diff] [blame] | 4848 | pC->cacheStatus = CACHE_STALE; |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 4849 | if( pC->eCurType==CURTYPE_BTREE ){ |
| 4850 | assert( pC->uc.pCursor!=0 ); |
| 4851 | sqlite3BtreeClearCursor(pC->uc.pCursor); |
danielk1977 | be51a65 | 2008-10-08 17:58:48 +0000 | [diff] [blame] | 4852 | } |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 4853 | break; |
| 4854 | } |
| 4855 | |
drh | 86b40df | 2017-08-01 19:53:43 +0000 | [diff] [blame] | 4856 | /* Opcode: SeekEnd P1 * * * * |
| 4857 | ** |
| 4858 | ** Position cursor P1 at the end of the btree for the purpose of |
| 4859 | ** appending a new entry onto the btree. |
| 4860 | ** |
| 4861 | ** It is assumed that the cursor is used only for appending and so |
| 4862 | ** if the cursor is valid, then the cursor must already be pointing |
| 4863 | ** at the end of the btree and so no changes are made to |
| 4864 | ** the cursor. |
| 4865 | */ |
| 4866 | /* Opcode: Last P1 P2 * * * |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 4867 | ** |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 4868 | ** The next use of the Rowid or Column or Prev instruction for P1 |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 4869 | ** will refer to the last entry in the database table or index. |
| 4870 | ** If the table or index is empty and P2>0, then jump immediately to P2. |
| 4871 | ** If P2 is 0 or if the table or index is not empty, fall through |
| 4872 | ** to the following instruction. |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 4873 | ** |
| 4874 | ** This opcode leaves the cursor configured to move in reverse order, |
| 4875 | ** from the end toward the beginning. In other words, the cursor is |
drh | 5dad9a3 | 2014-07-25 18:37:42 +0000 | [diff] [blame] | 4876 | ** configured to use Prev, not Next. |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 4877 | */ |
drh | 86b40df | 2017-08-01 19:53:43 +0000 | [diff] [blame] | 4878 | case OP_SeekEnd: |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4879 | case OP_Last: { /* jump */ |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 4880 | VdbeCursor *pC; |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 4881 | BtCursor *pCrsr; |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 4882 | int res; |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 4883 | |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 4884 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 4885 | pC = p->apCsr[pOp->p1]; |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 4886 | assert( pC!=0 ); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 4887 | assert( pC->eCurType==CURTYPE_BTREE ); |
| 4888 | pCrsr = pC->uc.pCursor; |
drh | 7abc540 | 2011-10-22 21:00:46 +0000 | [diff] [blame] | 4889 | res = 0; |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 4890 | assert( pCrsr!=0 ); |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 4891 | #ifdef SQLITE_DEBUG |
drh | 86b40df | 2017-08-01 19:53:43 +0000 | [diff] [blame] | 4892 | pC->seekOp = pOp->opcode; |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 4893 | #endif |
drh | 86b40df | 2017-08-01 19:53:43 +0000 | [diff] [blame] | 4894 | if( pOp->opcode==OP_SeekEnd ){ |
drh | d6ef5af | 2016-11-15 04:00:24 +0000 | [diff] [blame] | 4895 | assert( pOp->p2==0 ); |
drh | 86b40df | 2017-08-01 19:53:43 +0000 | [diff] [blame] | 4896 | pC->seekResult = -1; |
| 4897 | if( sqlite3BtreeCursorIsValidNN(pCrsr) ){ |
| 4898 | break; |
| 4899 | } |
| 4900 | } |
| 4901 | rc = sqlite3BtreeLast(pCrsr, &res); |
| 4902 | pC->nullRow = (u8)res; |
| 4903 | pC->deferredMoveto = 0; |
| 4904 | pC->cacheStatus = CACHE_STALE; |
| 4905 | if( rc ) goto abort_due_to_error; |
| 4906 | if( pOp->p2>0 ){ |
| 4907 | VdbeBranchTaken(res!=0,2); |
| 4908 | if( res ) goto jump_to_p2; |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 4909 | } |
| 4910 | break; |
| 4911 | } |
| 4912 | |
drh | 5e98e83 | 2017-02-17 19:24:06 +0000 | [diff] [blame] | 4913 | /* Opcode: IfSmaller P1 P2 P3 * * |
| 4914 | ** |
| 4915 | ** Estimate the number of rows in the table P1. Jump to P2 if that |
| 4916 | ** estimate is less than approximately 2**(0.1*P3). |
| 4917 | */ |
| 4918 | case OP_IfSmaller: { /* jump */ |
| 4919 | VdbeCursor *pC; |
| 4920 | BtCursor *pCrsr; |
| 4921 | int res; |
| 4922 | i64 sz; |
| 4923 | |
| 4924 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 4925 | pC = p->apCsr[pOp->p1]; |
| 4926 | assert( pC!=0 ); |
| 4927 | pCrsr = pC->uc.pCursor; |
| 4928 | assert( pCrsr ); |
| 4929 | rc = sqlite3BtreeFirst(pCrsr, &res); |
| 4930 | if( rc ) goto abort_due_to_error; |
| 4931 | if( res==0 ){ |
| 4932 | sz = sqlite3BtreeRowCountEst(pCrsr); |
| 4933 | if( ALWAYS(sz>=0) && sqlite3LogEst((u64)sz)<pOp->p3 ) res = 1; |
| 4934 | } |
| 4935 | VdbeBranchTaken(res!=0,2); |
| 4936 | if( res ) goto jump_to_p2; |
| 4937 | break; |
| 4938 | } |
| 4939 | |
drh | 0342b1f | 2005-09-01 03:07:44 +0000 | [diff] [blame] | 4940 | |
drh | 6bd4dc6 | 2016-12-23 16:05:22 +0000 | [diff] [blame] | 4941 | /* Opcode: SorterSort P1 P2 * * * |
| 4942 | ** |
| 4943 | ** After all records have been inserted into the Sorter object |
| 4944 | ** identified by P1, invoke this opcode to actually do the sorting. |
| 4945 | ** Jump to P2 if there are no records to be sorted. |
| 4946 | ** |
| 4947 | ** This opcode is an alias for OP_Sort and OP_Rewind that is used |
| 4948 | ** for Sorter objects. |
| 4949 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4950 | /* Opcode: Sort P1 P2 * * * |
drh | 0342b1f | 2005-09-01 03:07:44 +0000 | [diff] [blame] | 4951 | ** |
| 4952 | ** This opcode does exactly the same thing as OP_Rewind except that |
| 4953 | ** it increments an undocumented global variable used for testing. |
| 4954 | ** |
| 4955 | ** Sorting is accomplished by writing records into a sorting index, |
| 4956 | ** then rewinding that index and playing it back from beginning to |
| 4957 | ** end. We use the OP_Sort opcode instead of OP_Rewind to do the |
| 4958 | ** rewinding so that the global variable will be incremented and |
| 4959 | ** regression tests can determine whether or not the optimizer is |
| 4960 | ** correctly optimizing out sorts. |
| 4961 | */ |
drh | c6aff30 | 2011-09-01 15:32:47 +0000 | [diff] [blame] | 4962 | case OP_SorterSort: /* jump */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4963 | case OP_Sort: { /* jump */ |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 4964 | #ifdef SQLITE_TEST |
drh | 0342b1f | 2005-09-01 03:07:44 +0000 | [diff] [blame] | 4965 | sqlite3_sort_count++; |
drh | 4db38a7 | 2005-09-01 12:16:28 +0000 | [diff] [blame] | 4966 | sqlite3_search_count--; |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 4967 | #endif |
drh | 9b47ee3 | 2013-08-20 03:13:51 +0000 | [diff] [blame] | 4968 | p->aCounter[SQLITE_STMTSTATUS_SORT]++; |
drh | 0342b1f | 2005-09-01 03:07:44 +0000 | [diff] [blame] | 4969 | /* Fall through into OP_Rewind */ |
| 4970 | } |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4971 | /* Opcode: Rewind P1 P2 * * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4972 | ** |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 4973 | ** The next use of the Rowid or Column or Next instruction for P1 |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 4974 | ** will refer to the first entry in the database table or index. |
dan | 04489b6 | 2014-10-31 20:11:32 +0000 | [diff] [blame] | 4975 | ** If the table or index is empty, jump immediately to P2. |
| 4976 | ** If the table or index is not empty, fall through to the following |
| 4977 | ** instruction. |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 4978 | ** |
| 4979 | ** This opcode leaves the cursor configured to move in forward order, |
drh | 4ed2fb9 | 2014-08-14 13:06:25 +0000 | [diff] [blame] | 4980 | ** from the beginning toward the end. In other words, the cursor is |
drh | 5dad9a3 | 2014-07-25 18:37:42 +0000 | [diff] [blame] | 4981 | ** configured to use Next, not Prev. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4982 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4983 | case OP_Rewind: { /* jump */ |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 4984 | VdbeCursor *pC; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4985 | BtCursor *pCrsr; |
drh | f4dada7 | 2004-05-11 09:57:35 +0000 | [diff] [blame] | 4986 | int res; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4987 | |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 4988 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 4989 | pC = p->apCsr[pOp->p1]; |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 4990 | assert( pC!=0 ); |
drh | 14da87f | 2013-11-20 21:51:33 +0000 | [diff] [blame] | 4991 | assert( isSorter(pC)==(pOp->opcode==OP_SorterSort) ); |
dan | 2411dea | 2010-07-03 05:56:09 +0000 | [diff] [blame] | 4992 | res = 1; |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 4993 | #ifdef SQLITE_DEBUG |
| 4994 | pC->seekOp = OP_Rewind; |
| 4995 | #endif |
dan | 689ab89 | 2011-08-12 15:02:00 +0000 | [diff] [blame] | 4996 | if( isSorter(pC) ){ |
drh | 958d261 | 2014-04-18 13:40:07 +0000 | [diff] [blame] | 4997 | rc = sqlite3VdbeSorterRewind(pC, &res); |
dan | a205a48 | 2011-08-27 18:48:57 +0000 | [diff] [blame] | 4998 | }else{ |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 4999 | assert( pC->eCurType==CURTYPE_BTREE ); |
| 5000 | pCrsr = pC->uc.pCursor; |
dan | a205a48 | 2011-08-27 18:48:57 +0000 | [diff] [blame] | 5001 | assert( pCrsr ); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 5002 | rc = sqlite3BtreeFirst(pCrsr, &res); |
drh | a11846b | 2004-01-07 18:52:56 +0000 | [diff] [blame] | 5003 | pC->deferredMoveto = 0; |
drh | 76873ab | 2006-01-07 18:48:26 +0000 | [diff] [blame] | 5004 | pC->cacheStatus = CACHE_STALE; |
drh | f4dada7 | 2004-05-11 09:57:35 +0000 | [diff] [blame] | 5005 | } |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 5006 | if( rc ) goto abort_due_to_error; |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 5007 | pC->nullRow = (u8)res; |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 5008 | assert( pOp->p2>0 && pOp->p2<p->nOp ); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 5009 | VdbeBranchTaken(res!=0,2); |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 5010 | if( res ) goto jump_to_p2; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5011 | break; |
| 5012 | } |
| 5013 | |
drh | 0fd6135 | 2014-02-07 02:29:45 +0000 | [diff] [blame] | 5014 | /* Opcode: Next P1 P2 P3 P4 P5 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5015 | ** |
| 5016 | ** 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] | 5017 | ** table or index. If there are no more key/value pairs then fall through |
| 5018 | ** to the following instruction. But if the cursor advance was successful, |
| 5019 | ** jump immediately to P2. |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 5020 | ** |
drh | 5dad9a3 | 2014-07-25 18:37:42 +0000 | [diff] [blame] | 5021 | ** The Next opcode is only valid following an SeekGT, SeekGE, or |
| 5022 | ** OP_Rewind opcode used to position the cursor. Next is not allowed |
| 5023 | ** to follow SeekLT, SeekLE, or OP_Last. |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 5024 | ** |
drh | f93cd94 | 2013-11-21 03:12:25 +0000 | [diff] [blame] | 5025 | ** The P1 cursor must be for a real table, not a pseudo-table. P1 must have |
| 5026 | ** been opened prior to this opcode or the program will segfault. |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 5027 | ** |
drh | e39a732 | 2014-02-03 14:04:11 +0000 | [diff] [blame] | 5028 | ** The P3 value is a hint to the btree implementation. If P3==1, that |
| 5029 | ** means P1 is an SQL index and that this instruction could have been |
| 5030 | ** omitted if that index had been unique. P3 is usually 0. P3 is |
| 5031 | ** always either 0 or 1. |
| 5032 | ** |
dan | a205a48 | 2011-08-27 18:48:57 +0000 | [diff] [blame] | 5033 | ** P4 is always of type P4_ADVANCE. The function pointer points to |
| 5034 | ** sqlite3BtreeNext(). |
| 5035 | ** |
drh | afc266a | 2010-03-31 17:47:44 +0000 | [diff] [blame] | 5036 | ** If P5 is positive and the jump is taken, then event counter |
| 5037 | ** number P5-1 in the prepared statement is incremented. |
| 5038 | ** |
drh | f93cd94 | 2013-11-21 03:12:25 +0000 | [diff] [blame] | 5039 | ** See also: Prev, NextIfOpen |
| 5040 | */ |
drh | 0fd6135 | 2014-02-07 02:29:45 +0000 | [diff] [blame] | 5041 | /* Opcode: NextIfOpen P1 P2 P3 P4 P5 |
drh | f93cd94 | 2013-11-21 03:12:25 +0000 | [diff] [blame] | 5042 | ** |
drh | 5dad9a3 | 2014-07-25 18:37:42 +0000 | [diff] [blame] | 5043 | ** This opcode works just like Next except that if cursor P1 is not |
drh | f93cd94 | 2013-11-21 03:12:25 +0000 | [diff] [blame] | 5044 | ** open it behaves a no-op. |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 5045 | */ |
drh | 0fd6135 | 2014-02-07 02:29:45 +0000 | [diff] [blame] | 5046 | /* Opcode: Prev P1 P2 P3 P4 P5 |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 5047 | ** |
| 5048 | ** Back up cursor P1 so that it points to the previous key/data pair in its |
| 5049 | ** table or index. If there is no previous key/value pairs then fall through |
| 5050 | ** to the following instruction. But if the cursor backup was successful, |
| 5051 | ** jump immediately to P2. |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 5052 | ** |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 5053 | ** |
drh | 5dad9a3 | 2014-07-25 18:37:42 +0000 | [diff] [blame] | 5054 | ** The Prev opcode is only valid following an SeekLT, SeekLE, or |
| 5055 | ** OP_Last opcode used to position the cursor. Prev is not allowed |
| 5056 | ** to follow SeekGT, SeekGE, or OP_Rewind. |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 5057 | ** |
drh | f93cd94 | 2013-11-21 03:12:25 +0000 | [diff] [blame] | 5058 | ** The P1 cursor must be for a real table, not a pseudo-table. If P1 is |
| 5059 | ** not open then the behavior is undefined. |
drh | afc266a | 2010-03-31 17:47:44 +0000 | [diff] [blame] | 5060 | ** |
drh | e39a732 | 2014-02-03 14:04:11 +0000 | [diff] [blame] | 5061 | ** The P3 value is a hint to the btree implementation. If P3==1, that |
| 5062 | ** means P1 is an SQL index and that this instruction could have been |
| 5063 | ** omitted if that index had been unique. P3 is usually 0. P3 is |
| 5064 | ** always either 0 or 1. |
| 5065 | ** |
dan | a205a48 | 2011-08-27 18:48:57 +0000 | [diff] [blame] | 5066 | ** P4 is always of type P4_ADVANCE. The function pointer points to |
| 5067 | ** sqlite3BtreePrevious(). |
| 5068 | ** |
drh | afc266a | 2010-03-31 17:47:44 +0000 | [diff] [blame] | 5069 | ** If P5 is positive and the jump is taken, then event counter |
| 5070 | ** number P5-1 in the prepared statement is incremented. |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 5071 | */ |
drh | 0fd6135 | 2014-02-07 02:29:45 +0000 | [diff] [blame] | 5072 | /* Opcode: PrevIfOpen P1 P2 P3 P4 P5 |
drh | f93cd94 | 2013-11-21 03:12:25 +0000 | [diff] [blame] | 5073 | ** |
drh | 5dad9a3 | 2014-07-25 18:37:42 +0000 | [diff] [blame] | 5074 | ** This opcode works just like Prev except that if cursor P1 is not |
drh | f93cd94 | 2013-11-21 03:12:25 +0000 | [diff] [blame] | 5075 | ** open it behaves a no-op. |
| 5076 | */ |
drh | 6bd4dc6 | 2016-12-23 16:05:22 +0000 | [diff] [blame] | 5077 | /* Opcode: SorterNext P1 P2 * * P5 |
| 5078 | ** |
| 5079 | ** This opcode works just like OP_Next except that P1 must be a |
| 5080 | ** sorter object for which the OP_SorterSort opcode has been |
| 5081 | ** invoked. This opcode advances the cursor to the next sorted |
| 5082 | ** record, or jumps to P2 if there are no more sorted records. |
| 5083 | */ |
drh | f93cd94 | 2013-11-21 03:12:25 +0000 | [diff] [blame] | 5084 | case OP_SorterNext: { /* jump */ |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 5085 | VdbeCursor *pC; |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 5086 | |
drh | f93cd94 | 2013-11-21 03:12:25 +0000 | [diff] [blame] | 5087 | pC = p->apCsr[pOp->p1]; |
| 5088 | assert( isSorter(pC) ); |
drh | 2ab792e | 2017-05-30 18:34:07 +0000 | [diff] [blame] | 5089 | rc = sqlite3VdbeSorterNext(db, pC); |
drh | f93cd94 | 2013-11-21 03:12:25 +0000 | [diff] [blame] | 5090 | goto next_tail; |
| 5091 | case OP_PrevIfOpen: /* jump */ |
| 5092 | case OP_NextIfOpen: /* jump */ |
| 5093 | if( p->apCsr[pOp->p1]==0 ) break; |
| 5094 | /* Fall through */ |
| 5095 | case OP_Prev: /* jump */ |
| 5096 | case OP_Next: /* jump */ |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 5097 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
drh | 9b47ee3 | 2013-08-20 03:13:51 +0000 | [diff] [blame] | 5098 | assert( pOp->p5<ArraySize(p->aCounter) ); |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame] | 5099 | pC = p->apCsr[pOp->p1]; |
drh | f93cd94 | 2013-11-21 03:12:25 +0000 | [diff] [blame] | 5100 | assert( pC!=0 ); |
| 5101 | assert( pC->deferredMoveto==0 ); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 5102 | assert( pC->eCurType==CURTYPE_BTREE ); |
drh | f93cd94 | 2013-11-21 03:12:25 +0000 | [diff] [blame] | 5103 | assert( pOp->opcode!=OP_Next || pOp->p4.xAdvance==sqlite3BtreeNext ); |
| 5104 | assert( pOp->opcode!=OP_Prev || pOp->p4.xAdvance==sqlite3BtreePrevious ); |
| 5105 | assert( pOp->opcode!=OP_NextIfOpen || pOp->p4.xAdvance==sqlite3BtreeNext ); |
| 5106 | assert( pOp->opcode!=OP_PrevIfOpen || pOp->p4.xAdvance==sqlite3BtreePrevious); |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 5107 | |
| 5108 | /* The Next opcode is only used after SeekGT, SeekGE, and Rewind. |
| 5109 | ** The Prev opcode is only used after SeekLT, SeekLE, and Last. */ |
| 5110 | assert( pOp->opcode!=OP_Next || pOp->opcode!=OP_NextIfOpen |
| 5111 | || pC->seekOp==OP_SeekGT || pC->seekOp==OP_SeekGE |
drh | cefc87f | 2014-08-01 01:40:33 +0000 | [diff] [blame] | 5112 | || pC->seekOp==OP_Rewind || pC->seekOp==OP_Found); |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 5113 | assert( pOp->opcode!=OP_Prev || pOp->opcode!=OP_PrevIfOpen |
| 5114 | || pC->seekOp==OP_SeekLT || pC->seekOp==OP_SeekLE |
| 5115 | || pC->seekOp==OP_Last ); |
| 5116 | |
drh | 2ab792e | 2017-05-30 18:34:07 +0000 | [diff] [blame] | 5117 | rc = pOp->p4.xAdvance(pC->uc.pCursor, pOp->p3); |
drh | f93cd94 | 2013-11-21 03:12:25 +0000 | [diff] [blame] | 5118 | next_tail: |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 5119 | pC->cacheStatus = CACHE_STALE; |
drh | 2ab792e | 2017-05-30 18:34:07 +0000 | [diff] [blame] | 5120 | VdbeBranchTaken(rc==SQLITE_OK,2); |
| 5121 | if( rc==SQLITE_OK ){ |
drh | f93cd94 | 2013-11-21 03:12:25 +0000 | [diff] [blame] | 5122 | pC->nullRow = 0; |
drh | 9b47ee3 | 2013-08-20 03:13:51 +0000 | [diff] [blame] | 5123 | p->aCounter[pOp->p5]++; |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 5124 | #ifdef SQLITE_TEST |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 5125 | sqlite3_search_count++; |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 5126 | #endif |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 5127 | goto jump_to_p2_and_check_for_interrupt; |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 5128 | } |
drh | 2ab792e | 2017-05-30 18:34:07 +0000 | [diff] [blame] | 5129 | if( rc!=SQLITE_DONE ) goto abort_due_to_error; |
| 5130 | rc = SQLITE_OK; |
| 5131 | pC->nullRow = 1; |
drh | 49afe3a | 2013-07-10 03:05:14 +0000 | [diff] [blame] | 5132 | goto check_for_interrupt; |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 5133 | } |
| 5134 | |
drh | 9b4eaeb | 2016-11-09 00:10:33 +0000 | [diff] [blame] | 5135 | /* Opcode: IdxInsert P1 P2 P3 P4 P5 |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 5136 | ** Synopsis: key=r[P2] |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5137 | ** |
drh | ef8662b | 2011-06-20 21:47:58 +0000 | [diff] [blame] | 5138 | ** Register P2 holds an SQL index key made using the |
drh | 9437bd2 | 2009-02-01 00:29:56 +0000 | [diff] [blame] | 5139 | ** MakeRecord instructions. This opcode writes that key |
drh | ee32e0a | 2006-01-10 19:45:49 +0000 | [diff] [blame] | 5140 | ** into the index P1. Data for the entry is nil. |
drh | 717e640 | 2001-09-27 03:22:32 +0000 | [diff] [blame] | 5141 | ** |
drh | fb8c56f | 2016-11-09 01:19:25 +0000 | [diff] [blame] | 5142 | ** 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] | 5143 | ** key of reg(P2). In that case, P3 is the index of the first register |
| 5144 | ** for the unpacked key. The availability of the unpacked key can sometimes |
| 5145 | ** be an optimization. |
| 5146 | ** |
| 5147 | ** If P5 has the OPFLAG_APPEND bit set, that is a hint to the b-tree layer |
| 5148 | ** that this insert is likely to be an append. |
drh | e4d9081 | 2007-03-29 05:51:49 +0000 | [diff] [blame] | 5149 | ** |
mistachkin | 21a919f | 2014-02-07 03:28:02 +0000 | [diff] [blame] | 5150 | ** If P5 has the OPFLAG_NCHANGE bit set, then the change counter is |
| 5151 | ** incremented by this instruction. If the OPFLAG_NCHANGE bit is clear, |
| 5152 | ** then the change counter is unchanged. |
drh | 0fd6135 | 2014-02-07 02:29:45 +0000 | [diff] [blame] | 5153 | ** |
drh | eaf6ae2 | 2016-11-09 20:14:34 +0000 | [diff] [blame] | 5154 | ** If the OPFLAG_USESEEKRESULT flag of P5 is set, the implementation might |
| 5155 | ** run faster by avoiding an unnecessary seek on cursor P1. However, |
| 5156 | ** the OPFLAG_USESEEKRESULT flag must only be set if there have been no prior |
| 5157 | ** seeks on the cursor or if the most recent seek used a key equivalent |
| 5158 | ** to P2. |
drh | 0fd6135 | 2014-02-07 02:29:45 +0000 | [diff] [blame] | 5159 | ** |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 5160 | ** This instruction only works for indices. The equivalent instruction |
| 5161 | ** for tables is OP_Insert. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5162 | */ |
drh | f013e20 | 2016-10-15 18:37:05 +0000 | [diff] [blame] | 5163 | /* Opcode: SorterInsert P1 P2 * * * |
| 5164 | ** Synopsis: key=r[P2] |
| 5165 | ** |
| 5166 | ** Register P2 holds an SQL index key made using the |
| 5167 | ** MakeRecord instructions. This opcode writes that key |
| 5168 | ** into the sorter P1. Data for the entry is nil. |
| 5169 | */ |
drh | ca892a7 | 2011-09-03 00:17:51 +0000 | [diff] [blame] | 5170 | case OP_SorterInsert: /* in2 */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 5171 | case OP_IdxInsert: { /* in2 */ |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 5172 | VdbeCursor *pC; |
drh | 8eeb446 | 2016-05-21 20:03:42 +0000 | [diff] [blame] | 5173 | BtreePayload x; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 5174 | |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 5175 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 5176 | pC = p->apCsr[pOp->p1]; |
| 5177 | assert( pC!=0 ); |
drh | 14da87f | 2013-11-20 21:51:33 +0000 | [diff] [blame] | 5178 | assert( isSorter(pC)==(pOp->opcode==OP_SorterInsert) ); |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 5179 | pIn2 = &aMem[pOp->p2]; |
drh | aa9b896 | 2008-01-08 02:57:55 +0000 | [diff] [blame] | 5180 | assert( pIn2->flags & MEM_Blob ); |
drh | 6546af1 | 2013-11-04 15:23:25 +0000 | [diff] [blame] | 5181 | if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++; |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 5182 | assert( pC->eCurType==CURTYPE_BTREE || pOp->opcode==OP_SorterInsert ); |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 5183 | assert( pC->isTable==0 ); |
| 5184 | rc = ExpandBlob(pIn2); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 5185 | if( rc ) goto abort_due_to_error; |
| 5186 | if( pOp->opcode==OP_SorterInsert ){ |
| 5187 | rc = sqlite3VdbeSorterWrite(pC, pIn2); |
| 5188 | }else{ |
drh | 8eeb446 | 2016-05-21 20:03:42 +0000 | [diff] [blame] | 5189 | x.nKey = pIn2->n; |
| 5190 | x.pKey = pIn2->z; |
drh | 9b4eaeb | 2016-11-09 00:10:33 +0000 | [diff] [blame] | 5191 | x.aMem = aMem + pOp->p3; |
| 5192 | x.nMem = (u16)pOp->p4.i; |
| 5193 | rc = sqlite3BtreeInsert(pC->uc.pCursor, &x, |
dan | f91c131 | 2017-01-10 20:04:38 +0000 | [diff] [blame] | 5194 | (pOp->p5 & (OPFLAG_APPEND|OPFLAG_SAVEPOSITION)), |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 5195 | ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0) |
| 5196 | ); |
| 5197 | assert( pC->deferredMoveto==0 ); |
| 5198 | pC->cacheStatus = CACHE_STALE; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5199 | } |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 5200 | if( rc) goto abort_due_to_error; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5201 | break; |
| 5202 | } |
| 5203 | |
drh | d1d3848 | 2008-10-07 23:46:38 +0000 | [diff] [blame] | 5204 | /* Opcode: IdxDelete P1 P2 P3 * * |
drh | f63552b | 2013-10-30 00:25:03 +0000 | [diff] [blame] | 5205 | ** Synopsis: key=r[P2@P3] |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5206 | ** |
drh | e14006d | 2008-03-25 17:23:32 +0000 | [diff] [blame] | 5207 | ** The content of P3 registers starting at register P2 form |
| 5208 | ** an unpacked index key. This opcode removes that entry from the |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 5209 | ** index opened by cursor P1. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5210 | */ |
drh | e14006d | 2008-03-25 17:23:32 +0000 | [diff] [blame] | 5211 | case OP_IdxDelete: { |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 5212 | VdbeCursor *pC; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5213 | BtCursor *pCrsr; |
drh | 9a65f2c | 2009-06-22 19:05:40 +0000 | [diff] [blame] | 5214 | int res; |
| 5215 | UnpackedRecord r; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 5216 | |
drh | e14006d | 2008-03-25 17:23:32 +0000 | [diff] [blame] | 5217 | assert( pOp->p3>0 ); |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 5218 | 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] | 5219 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 5220 | pC = p->apCsr[pOp->p1]; |
| 5221 | assert( pC!=0 ); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 5222 | assert( pC->eCurType==CURTYPE_BTREE ); |
| 5223 | pCrsr = pC->uc.pCursor; |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 5224 | assert( pCrsr!=0 ); |
drh | 4308e34 | 2013-11-11 16:55:52 +0000 | [diff] [blame] | 5225 | assert( pOp->p5==0 ); |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 5226 | r.pKeyInfo = pC->pKeyInfo; |
| 5227 | r.nField = (u16)pOp->p3; |
dan | 1fed5da | 2014-02-25 21:01:25 +0000 | [diff] [blame] | 5228 | r.default_rc = 0; |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 5229 | r.aMem = &aMem[pOp->p2]; |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 5230 | rc = sqlite3BtreeMovetoUnpacked(pCrsr, &r, 0, 0, &res); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 5231 | if( rc ) goto abort_due_to_error; |
| 5232 | if( res==0 ){ |
dan | e61bbf4 | 2016-01-28 17:06:17 +0000 | [diff] [blame] | 5233 | rc = sqlite3BtreeDelete(pCrsr, BTREE_AUXDELETE); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 5234 | if( rc ) goto abort_due_to_error; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5235 | } |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 5236 | assert( pC->deferredMoveto==0 ); |
| 5237 | pC->cacheStatus = CACHE_STALE; |
dan | 3b908d4 | 2016-11-08 19:22:32 +0000 | [diff] [blame] | 5238 | pC->seekResult = 0; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5239 | break; |
| 5240 | } |
| 5241 | |
drh | 170ad68 | 2017-06-02 15:44:22 +0000 | [diff] [blame] | 5242 | /* Opcode: DeferredSeek P1 * P3 P4 * |
| 5243 | ** Synopsis: Move P3 to P1.rowid if needed |
drh | 784c1b9 | 2016-01-30 16:59:56 +0000 | [diff] [blame] | 5244 | ** |
| 5245 | ** P1 is an open index cursor and P3 is a cursor on the corresponding |
| 5246 | ** table. This opcode does a deferred seek of the P3 table cursor |
| 5247 | ** to the row that corresponds to the current row of P1. |
| 5248 | ** |
| 5249 | ** This is a deferred seek. Nothing actually happens until |
| 5250 | ** the cursor is used to read a record. That way, if no reads |
| 5251 | ** occur, no unnecessary I/O happens. |
| 5252 | ** |
| 5253 | ** P4 may be an array of integers (type P4_INTARRAY) containing |
drh | 19d720d | 2016-02-03 19:52:06 +0000 | [diff] [blame] | 5254 | ** one entry for each column in the P3 table. If array entry a(i) |
| 5255 | ** is non-zero, then reading column a(i)-1 from cursor P3 is |
drh | 784c1b9 | 2016-01-30 16:59:56 +0000 | [diff] [blame] | 5256 | ** equivalent to performing the deferred seek and then reading column i |
| 5257 | ** from P1. This information is stored in P3 and used to redirect |
| 5258 | ** reads against P3 over to P1, thus possibly avoiding the need to |
| 5259 | ** seek and read cursor P3. |
| 5260 | */ |
drh | 2133d82 | 2008-01-03 18:44:59 +0000 | [diff] [blame] | 5261 | /* Opcode: IdxRowid P1 P2 * * * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 5262 | ** Synopsis: r[P2]=rowid |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 5263 | ** |
drh | 2133d82 | 2008-01-03 18:44:59 +0000 | [diff] [blame] | 5264 | ** 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] | 5265 | ** the end of the index key pointed to by cursor P1. This integer should be |
| 5266 | ** the rowid of the table entry to which this index entry points. |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 5267 | ** |
drh | 9437bd2 | 2009-02-01 00:29:56 +0000 | [diff] [blame] | 5268 | ** See also: Rowid, MakeRecord. |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 5269 | */ |
drh | 170ad68 | 2017-06-02 15:44:22 +0000 | [diff] [blame] | 5270 | case OP_DeferredSeek: |
| 5271 | case OP_IdxRowid: { /* out2 */ |
| 5272 | VdbeCursor *pC; /* The P1 index cursor */ |
| 5273 | VdbeCursor *pTabCur; /* The P2 table cursor (OP_DeferredSeek only) */ |
| 5274 | i64 rowid; /* Rowid that P1 current points to */ |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 5275 | |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 5276 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 5277 | pC = p->apCsr[pOp->p1]; |
| 5278 | assert( pC!=0 ); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 5279 | assert( pC->eCurType==CURTYPE_BTREE ); |
drh | 784c1b9 | 2016-01-30 16:59:56 +0000 | [diff] [blame] | 5280 | assert( pC->uc.pCursor!=0 ); |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 5281 | assert( pC->isTable==0 ); |
drh | c22284f | 2014-10-13 16:02:20 +0000 | [diff] [blame] | 5282 | assert( pC->deferredMoveto==0 ); |
drh | 784c1b9 | 2016-01-30 16:59:56 +0000 | [diff] [blame] | 5283 | assert( !pC->nullRow || pOp->opcode==OP_IdxRowid ); |
| 5284 | |
| 5285 | /* The IdxRowid and Seek opcodes are combined because of the commonality |
| 5286 | ** of sqlite3VdbeCursorRestore() and sqlite3VdbeIdxRowid(). */ |
| 5287 | rc = sqlite3VdbeCursorRestore(pC); |
drh | c22284f | 2014-10-13 16:02:20 +0000 | [diff] [blame] | 5288 | |
| 5289 | /* sqlite3VbeCursorRestore() can only fail if the record has been deleted |
drh | 784c1b9 | 2016-01-30 16:59:56 +0000 | [diff] [blame] | 5290 | ** out from under the cursor. That will never happens for an IdxRowid |
| 5291 | ** or Seek opcode */ |
drh | c22284f | 2014-10-13 16:02:20 +0000 | [diff] [blame] | 5292 | if( NEVER(rc!=SQLITE_OK) ) goto abort_due_to_error; |
| 5293 | |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 5294 | if( !pC->nullRow ){ |
drh | 2dc0648 | 2013-12-11 00:59:10 +0000 | [diff] [blame] | 5295 | rowid = 0; /* Not needed. Only used to silence a warning. */ |
drh | 784c1b9 | 2016-01-30 16:59:56 +0000 | [diff] [blame] | 5296 | rc = sqlite3VdbeIdxRowid(db, pC->uc.pCursor, &rowid); |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 5297 | if( rc!=SQLITE_OK ){ |
| 5298 | goto abort_due_to_error; |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 5299 | } |
drh | 170ad68 | 2017-06-02 15:44:22 +0000 | [diff] [blame] | 5300 | if( pOp->opcode==OP_DeferredSeek ){ |
drh | 784c1b9 | 2016-01-30 16:59:56 +0000 | [diff] [blame] | 5301 | assert( pOp->p3>=0 && pOp->p3<p->nCursor ); |
| 5302 | pTabCur = p->apCsr[pOp->p3]; |
| 5303 | assert( pTabCur!=0 ); |
| 5304 | assert( pTabCur->eCurType==CURTYPE_BTREE ); |
| 5305 | assert( pTabCur->uc.pCursor!=0 ); |
| 5306 | assert( pTabCur->isTable ); |
| 5307 | pTabCur->nullRow = 0; |
| 5308 | pTabCur->movetoTarget = rowid; |
| 5309 | pTabCur->deferredMoveto = 1; |
| 5310 | assert( pOp->p4type==P4_INTARRAY || pOp->p4.ai==0 ); |
| 5311 | pTabCur->aAltMap = pOp->p4.ai; |
| 5312 | pTabCur->pAltCursor = pC; |
| 5313 | }else{ |
| 5314 | pOut = out2Prerelease(p, pOp); |
| 5315 | pOut->u.i = rowid; |
drh | 784c1b9 | 2016-01-30 16:59:56 +0000 | [diff] [blame] | 5316 | } |
| 5317 | }else{ |
| 5318 | assert( pOp->opcode==OP_IdxRowid ); |
| 5319 | sqlite3VdbeMemSetNull(&aMem[pOp->p2]); |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 5320 | } |
| 5321 | break; |
| 5322 | } |
| 5323 | |
danielk1977 | 61dd583 | 2008-04-18 11:31:12 +0000 | [diff] [blame] | 5324 | /* Opcode: IdxGE P1 P2 P3 P4 P5 |
drh | f63552b | 2013-10-30 00:25:03 +0000 | [diff] [blame] | 5325 | ** Synopsis: key=r[P3@P4] |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 5326 | ** |
danielk1977 | 61dd583 | 2008-04-18 11:31:12 +0000 | [diff] [blame] | 5327 | ** The P4 register values beginning with P3 form an unpacked index |
drh | 4a1d365 | 2014-02-14 15:13:36 +0000 | [diff] [blame] | 5328 | ** key that omits the PRIMARY KEY. Compare this key value against the index |
| 5329 | ** that P1 is currently pointing to, ignoring the PRIMARY KEY or ROWID |
| 5330 | ** fields at the end. |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 5331 | ** |
danielk1977 | 61dd583 | 2008-04-18 11:31:12 +0000 | [diff] [blame] | 5332 | ** If the P1 index entry is greater than or equal to the key value |
| 5333 | ** then jump to P2. Otherwise fall through to the next instruction. |
drh | 4a1d365 | 2014-02-14 15:13:36 +0000 | [diff] [blame] | 5334 | */ |
| 5335 | /* Opcode: IdxGT P1 P2 P3 P4 P5 |
| 5336 | ** Synopsis: key=r[P3@P4] |
drh | 772ae62 | 2004-05-19 13:13:08 +0000 | [diff] [blame] | 5337 | ** |
drh | 4a1d365 | 2014-02-14 15:13:36 +0000 | [diff] [blame] | 5338 | ** The P4 register values beginning with P3 form an unpacked index |
| 5339 | ** key that omits the PRIMARY KEY. Compare this key value against the index |
| 5340 | ** that P1 is currently pointing to, ignoring the PRIMARY KEY or ROWID |
| 5341 | ** fields at the end. |
| 5342 | ** |
| 5343 | ** If the P1 index entry is greater than the key value |
| 5344 | ** then jump to P2. Otherwise fall through to the next instruction. |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 5345 | */ |
drh | 3bb9b93 | 2010-08-06 02:10:00 +0000 | [diff] [blame] | 5346 | /* Opcode: IdxLT P1 P2 P3 P4 P5 |
drh | f63552b | 2013-10-30 00:25:03 +0000 | [diff] [blame] | 5347 | ** Synopsis: key=r[P3@P4] |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 5348 | ** |
danielk1977 | 61dd583 | 2008-04-18 11:31:12 +0000 | [diff] [blame] | 5349 | ** The P4 register values beginning with P3 form an unpacked index |
drh | 4a1d365 | 2014-02-14 15:13:36 +0000 | [diff] [blame] | 5350 | ** key that omits the PRIMARY KEY or ROWID. Compare this key value against |
| 5351 | ** the index that P1 is currently pointing to, ignoring the PRIMARY KEY or |
| 5352 | ** ROWID on the P1 index. |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 5353 | ** |
danielk1977 | 61dd583 | 2008-04-18 11:31:12 +0000 | [diff] [blame] | 5354 | ** If the P1 index entry is less than the key value then jump to P2. |
| 5355 | ** Otherwise fall through to the next instruction. |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 5356 | */ |
drh | 4a1d365 | 2014-02-14 15:13:36 +0000 | [diff] [blame] | 5357 | /* Opcode: IdxLE P1 P2 P3 P4 P5 |
| 5358 | ** Synopsis: key=r[P3@P4] |
| 5359 | ** |
| 5360 | ** The P4 register values beginning with P3 form an unpacked index |
| 5361 | ** key that omits the PRIMARY KEY or ROWID. Compare this key value against |
| 5362 | ** the index that P1 is currently pointing to, ignoring the PRIMARY KEY or |
| 5363 | ** ROWID on the P1 index. |
| 5364 | ** |
| 5365 | ** If the P1 index entry is less than or equal to the key value then jump |
| 5366 | ** to P2. Otherwise fall through to the next instruction. |
| 5367 | */ |
| 5368 | case OP_IdxLE: /* jump */ |
| 5369 | case OP_IdxGT: /* jump */ |
drh | 93952eb | 2009-11-13 19:43:43 +0000 | [diff] [blame] | 5370 | case OP_IdxLT: /* jump */ |
drh | 4a1d365 | 2014-02-14 15:13:36 +0000 | [diff] [blame] | 5371 | case OP_IdxGE: { /* jump */ |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 5372 | VdbeCursor *pC; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 5373 | int res; |
| 5374 | UnpackedRecord r; |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 5375 | |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 5376 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 5377 | pC = p->apCsr[pOp->p1]; |
| 5378 | assert( pC!=0 ); |
drh | d4187c7 | 2010-08-30 22:15:45 +0000 | [diff] [blame] | 5379 | assert( pC->isOrdered ); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 5380 | assert( pC->eCurType==CURTYPE_BTREE ); |
| 5381 | assert( pC->uc.pCursor!=0); |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 5382 | assert( pC->deferredMoveto==0 ); |
| 5383 | assert( pOp->p5==0 || pOp->p5==1 ); |
| 5384 | assert( pOp->p4type==P4_INT32 ); |
| 5385 | r.pKeyInfo = pC->pKeyInfo; |
| 5386 | r.nField = (u16)pOp->p4.i; |
drh | 4a1d365 | 2014-02-14 15:13:36 +0000 | [diff] [blame] | 5387 | if( pOp->opcode<OP_IdxLT ){ |
| 5388 | assert( pOp->opcode==OP_IdxLE || pOp->opcode==OP_IdxGT ); |
dan | 1fed5da | 2014-02-25 21:01:25 +0000 | [diff] [blame] | 5389 | r.default_rc = -1; |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 5390 | }else{ |
drh | 4a1d365 | 2014-02-14 15:13:36 +0000 | [diff] [blame] | 5391 | assert( pOp->opcode==OP_IdxGE || pOp->opcode==OP_IdxLT ); |
dan | 1fed5da | 2014-02-25 21:01:25 +0000 | [diff] [blame] | 5392 | r.default_rc = 0; |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 5393 | } |
| 5394 | r.aMem = &aMem[pOp->p3]; |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 5395 | #ifdef SQLITE_DEBUG |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 5396 | { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); } |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 5397 | #endif |
drh | 2dc0648 | 2013-12-11 00:59:10 +0000 | [diff] [blame] | 5398 | res = 0; /* Not needed. Only used to silence a warning. */ |
drh | d3b7420 | 2014-09-17 16:41:15 +0000 | [diff] [blame] | 5399 | rc = sqlite3VdbeIdxKeyCompare(db, pC, &r, &res); |
drh | 4a1d365 | 2014-02-14 15:13:36 +0000 | [diff] [blame] | 5400 | assert( (OP_IdxLE&1)==(OP_IdxLT&1) && (OP_IdxGE&1)==(OP_IdxGT&1) ); |
| 5401 | if( (pOp->opcode&1)==(OP_IdxLT&1) ){ |
| 5402 | assert( pOp->opcode==OP_IdxLE || pOp->opcode==OP_IdxLT ); |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 5403 | res = -res; |
| 5404 | }else{ |
drh | 4a1d365 | 2014-02-14 15:13:36 +0000 | [diff] [blame] | 5405 | assert( pOp->opcode==OP_IdxGE || pOp->opcode==OP_IdxGT ); |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 5406 | res++; |
| 5407 | } |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 5408 | VdbeBranchTaken(res>0,2); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 5409 | if( rc ) goto abort_due_to_error; |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 5410 | if( res>0 ) goto jump_to_p2; |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 5411 | break; |
| 5412 | } |
| 5413 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 5414 | /* Opcode: Destroy P1 P2 P3 * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5415 | ** |
| 5416 | ** Delete an entire database table or index whose root page in the database |
| 5417 | ** file is given by P1. |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 5418 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 5419 | ** The table being destroyed is in the main database file if P3==0. If |
| 5420 | ** 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] | 5421 | ** that is used to store tables create using CREATE TEMPORARY TABLE. |
| 5422 | ** |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 5423 | ** If AUTOVACUUM is enabled then it is possible that another root page |
| 5424 | ** might be moved into the newly deleted root page in order to keep all |
| 5425 | ** root pages contiguous at the beginning of the database. The former |
| 5426 | ** value of the root page that moved - its value before the move occurred - |
dan | a34adaf | 2017-04-08 14:11:47 +0000 | [diff] [blame] | 5427 | ** is stored in register P2. If no page movement was required (because the |
| 5428 | ** table being dropped was already the last one in the database) then a |
| 5429 | ** zero is stored in register P2. If AUTOVACUUM is disabled then a zero |
| 5430 | ** is stored in register P2. |
| 5431 | ** |
| 5432 | ** This opcode throws an error if there are any active reader VMs when |
| 5433 | ** it is invoked. This is done to avoid the difficulty associated with |
| 5434 | ** updating existing cursors when a root page is moved in an AUTOVACUUM |
| 5435 | ** database. This error is thrown even if the database is not an AUTOVACUUM |
| 5436 | ** db in order to avoid introducing an incompatibility between autovacuum |
| 5437 | ** and non-autovacuum modes. |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 5438 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 5439 | ** See also: Clear |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5440 | */ |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 5441 | case OP_Destroy: { /* out2 */ |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 5442 | int iMoved; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 5443 | int iDb; |
drh | 3a94987 | 2012-09-18 13:20:13 +0000 | [diff] [blame] | 5444 | |
drh | 9e92a47 | 2013-06-27 17:40:30 +0000 | [diff] [blame] | 5445 | assert( p->readOnly==0 ); |
drh | 055f298 | 2016-01-15 15:06:41 +0000 | [diff] [blame] | 5446 | assert( pOp->p1>1 ); |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 5447 | pOut = out2Prerelease(p, pOp); |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 5448 | pOut->flags = MEM_Null; |
drh | 086723a | 2015-03-24 12:51:52 +0000 | [diff] [blame] | 5449 | if( db->nVdbeRead > db->nVDestroy+1 ){ |
danielk1977 | e6efa74 | 2004-11-10 11:55:10 +0000 | [diff] [blame] | 5450 | rc = SQLITE_LOCKED; |
drh | 77658e2 | 2007-12-04 16:54:52 +0000 | [diff] [blame] | 5451 | p->errorAction = OE_Abort; |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 5452 | goto abort_due_to_error; |
danielk1977 | e6efa74 | 2004-11-10 11:55:10 +0000 | [diff] [blame] | 5453 | }else{ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 5454 | iDb = pOp->p3; |
drh | a7ab6d8 | 2014-07-21 15:44:39 +0000 | [diff] [blame] | 5455 | assert( DbMaskTest(p->btreeMask, iDb) ); |
drh | 2dc0648 | 2013-12-11 00:59:10 +0000 | [diff] [blame] | 5456 | iMoved = 0; /* Not needed. Only to silence a warning. */ |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 5457 | rc = sqlite3BtreeDropTable(db->aDb[iDb].pBt, pOp->p1, &iMoved); |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 5458 | pOut->flags = MEM_Int; |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 5459 | pOut->u.i = iMoved; |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 5460 | if( rc ) goto abort_due_to_error; |
drh | 3765df4 | 2006-06-28 18:18:09 +0000 | [diff] [blame] | 5461 | #ifndef SQLITE_OMIT_AUTOVACUUM |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 5462 | if( iMoved!=0 ){ |
drh | cdf011d | 2011-04-04 21:25:28 +0000 | [diff] [blame] | 5463 | sqlite3RootPageMoved(db, iDb, iMoved, pOp->p1); |
| 5464 | /* All OP_Destroy operations occur on the same btree */ |
| 5465 | assert( resetSchemaOnFault==0 || resetSchemaOnFault==iDb+1 ); |
| 5466 | resetSchemaOnFault = iDb+1; |
danielk1977 | e6efa74 | 2004-11-10 11:55:10 +0000 | [diff] [blame] | 5467 | } |
drh | 3765df4 | 2006-06-28 18:18:09 +0000 | [diff] [blame] | 5468 | #endif |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 5469 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5470 | break; |
| 5471 | } |
| 5472 | |
danielk1977 | c7af484 | 2008-10-27 13:59:33 +0000 | [diff] [blame] | 5473 | /* Opcode: Clear P1 P2 P3 |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 5474 | ** |
| 5475 | ** Delete all contents of the database table or index whose root page |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 5476 | ** in the database file is given by P1. But, unlike Destroy, do not |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 5477 | ** remove the table or index from the database file. |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 5478 | ** |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 5479 | ** The table being clear is in the main database file if P2==0. If |
| 5480 | ** P2==1 then the table to be clear is in the auxiliary database file |
| 5481 | ** that is used to store tables create using CREATE TEMPORARY TABLE. |
| 5482 | ** |
shane | be21779 | 2009-03-05 04:20:31 +0000 | [diff] [blame] | 5483 | ** 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] | 5484 | ** intkey table (an SQL table, not an index). In this case the row change |
| 5485 | ** count is incremented by the number of rows in the table being cleared. |
| 5486 | ** If P3 is greater than zero, then the value stored in register P3 is |
| 5487 | ** also incremented by the number of rows in the table being cleared. |
| 5488 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 5489 | ** See also: Destroy |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 5490 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 5491 | case OP_Clear: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 5492 | int nChange; |
| 5493 | |
| 5494 | nChange = 0; |
drh | 9e92a47 | 2013-06-27 17:40:30 +0000 | [diff] [blame] | 5495 | assert( p->readOnly==0 ); |
drh | a7ab6d8 | 2014-07-21 15:44:39 +0000 | [diff] [blame] | 5496 | assert( DbMaskTest(p->btreeMask, pOp->p2) ); |
danielk1977 | c7af484 | 2008-10-27 13:59:33 +0000 | [diff] [blame] | 5497 | rc = sqlite3BtreeClearTable( |
| 5498 | db->aDb[pOp->p2].pBt, pOp->p1, (pOp->p3 ? &nChange : 0) |
| 5499 | ); |
| 5500 | if( pOp->p3 ){ |
| 5501 | p->nChange += nChange; |
| 5502 | if( pOp->p3>0 ){ |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 5503 | assert( memIsValid(&aMem[pOp->p3]) ); |
| 5504 | memAboutToChange(p, &aMem[pOp->p3]); |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 5505 | aMem[pOp->p3].u.i += nChange; |
danielk1977 | c7af484 | 2008-10-27 13:59:33 +0000 | [diff] [blame] | 5506 | } |
| 5507 | } |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 5508 | if( rc ) goto abort_due_to_error; |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 5509 | break; |
| 5510 | } |
| 5511 | |
drh | 65ea12c | 2014-03-19 17:41:36 +0000 | [diff] [blame] | 5512 | /* Opcode: ResetSorter P1 * * * * |
drh | 079a307 | 2014-03-19 14:10:55 +0000 | [diff] [blame] | 5513 | ** |
drh | 65ea12c | 2014-03-19 17:41:36 +0000 | [diff] [blame] | 5514 | ** Delete all contents from the ephemeral table or sorter |
| 5515 | ** that is open on cursor P1. |
drh | 079a307 | 2014-03-19 14:10:55 +0000 | [diff] [blame] | 5516 | ** |
drh | 65ea12c | 2014-03-19 17:41:36 +0000 | [diff] [blame] | 5517 | ** This opcode only works for cursors used for sorting and |
| 5518 | ** opened with OP_OpenEphemeral or OP_SorterOpen. |
drh | 079a307 | 2014-03-19 14:10:55 +0000 | [diff] [blame] | 5519 | */ |
drh | 65ea12c | 2014-03-19 17:41:36 +0000 | [diff] [blame] | 5520 | case OP_ResetSorter: { |
drh | 079a307 | 2014-03-19 14:10:55 +0000 | [diff] [blame] | 5521 | VdbeCursor *pC; |
| 5522 | |
| 5523 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 5524 | pC = p->apCsr[pOp->p1]; |
| 5525 | assert( pC!=0 ); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 5526 | if( isSorter(pC) ){ |
| 5527 | sqlite3VdbeSorterReset(db, pC->uc.pSorter); |
drh | 65ea12c | 2014-03-19 17:41:36 +0000 | [diff] [blame] | 5528 | }else{ |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 5529 | assert( pC->eCurType==CURTYPE_BTREE ); |
drh | 65ea12c | 2014-03-19 17:41:36 +0000 | [diff] [blame] | 5530 | assert( pC->isEphemeral ); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 5531 | rc = sqlite3BtreeClearTableOfCursor(pC->uc.pCursor); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 5532 | if( rc ) goto abort_due_to_error; |
drh | 65ea12c | 2014-03-19 17:41:36 +0000 | [diff] [blame] | 5533 | } |
drh | 079a307 | 2014-03-19 14:10:55 +0000 | [diff] [blame] | 5534 | break; |
| 5535 | } |
| 5536 | |
drh | 0f3f766 | 2017-08-18 14:34:28 +0000 | [diff] [blame] | 5537 | /* Opcode: CreateBtree P1 P2 P3 * * |
| 5538 | ** Synopsis: r[P2]=root iDb=P1 flags=P3 |
drh | 5b2fd56 | 2001-09-13 15:21:31 +0000 | [diff] [blame] | 5539 | ** |
drh | 0f3f766 | 2017-08-18 14:34:28 +0000 | [diff] [blame] | 5540 | ** Allocate a new b-tree in the main database file if P1==0 or in the |
| 5541 | ** TEMP database file if P1==1 or in an attached database if |
| 5542 | ** P1>1. The P3 argument must be 1 (BTREE_INTKEY) for a rowid table |
| 5543 | ** it must be 2 (BTREE_BLOBKEY) for a index or WITHOUT ROWID table. |
| 5544 | ** 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] | 5545 | */ |
drh | 0f3f766 | 2017-08-18 14:34:28 +0000 | [diff] [blame] | 5546 | case OP_CreateBtree: { /* out2 */ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 5547 | int pgno; |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 5548 | Db *pDb; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 5549 | |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 5550 | pOut = out2Prerelease(p, pOp); |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 5551 | pgno = 0; |
drh | 0f3f766 | 2017-08-18 14:34:28 +0000 | [diff] [blame] | 5552 | assert( pOp->p3==BTREE_INTKEY || pOp->p3==BTREE_BLOBKEY ); |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 5553 | assert( pOp->p1>=0 && pOp->p1<db->nDb ); |
drh | a7ab6d8 | 2014-07-21 15:44:39 +0000 | [diff] [blame] | 5554 | assert( DbMaskTest(p->btreeMask, pOp->p1) ); |
drh | 9e92a47 | 2013-06-27 17:40:30 +0000 | [diff] [blame] | 5555 | assert( p->readOnly==0 ); |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 5556 | pDb = &db->aDb[pOp->p1]; |
| 5557 | assert( pDb->pBt!=0 ); |
drh | 0f3f766 | 2017-08-18 14:34:28 +0000 | [diff] [blame] | 5558 | rc = sqlite3BtreeCreateTable(pDb->pBt, &pgno, pOp->p3); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 5559 | if( rc ) goto abort_due_to_error; |
drh | 88a003e | 2008-12-11 16:17:03 +0000 | [diff] [blame] | 5560 | pOut->u.i = pgno; |
drh | 5b2fd56 | 2001-09-13 15:21:31 +0000 | [diff] [blame] | 5561 | break; |
| 5562 | } |
| 5563 | |
drh | 4a54bb5 | 2017-02-18 15:58:52 +0000 | [diff] [blame] | 5564 | /* Opcode: SqlExec * * * P4 * |
| 5565 | ** |
| 5566 | ** Run the SQL statement or statements specified in the P4 string. |
| 5567 | */ |
| 5568 | case OP_SqlExec: { |
drh | bce0414 | 2017-02-23 00:58:36 +0000 | [diff] [blame] | 5569 | db->nSqlExec++; |
drh | 4a54bb5 | 2017-02-18 15:58:52 +0000 | [diff] [blame] | 5570 | rc = sqlite3_exec(db, pOp->p4.z, 0, 0, 0); |
drh | bce0414 | 2017-02-23 00:58:36 +0000 | [diff] [blame] | 5571 | db->nSqlExec--; |
drh | 4a54bb5 | 2017-02-18 15:58:52 +0000 | [diff] [blame] | 5572 | if( rc ) goto abort_due_to_error; |
| 5573 | break; |
| 5574 | } |
| 5575 | |
drh | 2264584 | 2011-03-24 01:34:03 +0000 | [diff] [blame] | 5576 | /* Opcode: ParseSchema P1 * * P4 * |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 5577 | ** |
| 5578 | ** Read and parse all entries from the SQLITE_MASTER table of database P1 |
drh | 2264584 | 2011-03-24 01:34:03 +0000 | [diff] [blame] | 5579 | ** that match the WHERE clause P4. |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 5580 | ** |
| 5581 | ** This opcode invokes the parser to create a new virtual machine, |
shane | 21e7feb | 2008-05-30 15:59:49 +0000 | [diff] [blame] | 5582 | ** then runs the new virtual machine. It is thus a re-entrant opcode. |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 5583 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 5584 | case OP_ParseSchema: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 5585 | int iDb; |
| 5586 | const char *zMaster; |
| 5587 | char *zSql; |
| 5588 | InitData initData; |
| 5589 | |
drh | bdaec52 | 2011-04-04 00:14:43 +0000 | [diff] [blame] | 5590 | /* Any prepared statement that invokes this opcode will hold mutexes |
| 5591 | ** on every btree. This is a prerequisite for invoking |
| 5592 | ** sqlite3InitCallback(). |
| 5593 | */ |
| 5594 | #ifdef SQLITE_DEBUG |
| 5595 | for(iDb=0; iDb<db->nDb; iDb++){ |
| 5596 | assert( iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) ); |
| 5597 | } |
| 5598 | #endif |
drh | bdaec52 | 2011-04-04 00:14:43 +0000 | [diff] [blame] | 5599 | |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 5600 | iDb = pOp->p1; |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 5601 | assert( iDb>=0 && iDb<db->nDb ); |
dan | 6c15487 | 2011-04-02 09:44:43 +0000 | [diff] [blame] | 5602 | assert( DbHasProperty(db, iDb, DB_SchemaLoaded) ); |
drh | bdaec52 | 2011-04-04 00:14:43 +0000 | [diff] [blame] | 5603 | /* Used to be a conditional */ { |
drh | e0a04a3 | 2016-12-16 01:00:21 +0000 | [diff] [blame] | 5604 | zMaster = MASTER_NAME; |
danielk1977 | a8bbef8 | 2009-03-23 17:11:26 +0000 | [diff] [blame] | 5605 | initData.db = db; |
| 5606 | initData.iDb = pOp->p1; |
| 5607 | initData.pzErrMsg = &p->zErrMsg; |
| 5608 | zSql = sqlite3MPrintf(db, |
drh | 6a9c64b | 2010-01-12 23:54:14 +0000 | [diff] [blame] | 5609 | "SELECT name, rootpage, sql FROM '%q'.%s WHERE %s ORDER BY rowid", |
drh | 69c3382 | 2016-08-18 14:33:11 +0000 | [diff] [blame] | 5610 | db->aDb[iDb].zDbSName, zMaster, pOp->p4.z); |
danielk1977 | a8bbef8 | 2009-03-23 17:11:26 +0000 | [diff] [blame] | 5611 | if( zSql==0 ){ |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 5612 | rc = SQLITE_NOMEM_BKPT; |
danielk1977 | a8bbef8 | 2009-03-23 17:11:26 +0000 | [diff] [blame] | 5613 | }else{ |
danielk1977 | a8bbef8 | 2009-03-23 17:11:26 +0000 | [diff] [blame] | 5614 | assert( db->init.busy==0 ); |
| 5615 | db->init.busy = 1; |
| 5616 | initData.rc = SQLITE_OK; |
| 5617 | assert( !db->mallocFailed ); |
| 5618 | rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0); |
| 5619 | if( rc==SQLITE_OK ) rc = initData.rc; |
drh | dbd6a7d | 2017-04-05 12:39:49 +0000 | [diff] [blame] | 5620 | sqlite3DbFreeNN(db, zSql); |
danielk1977 | a8bbef8 | 2009-03-23 17:11:26 +0000 | [diff] [blame] | 5621 | db->init.busy = 0; |
danielk1977 | a8bbef8 | 2009-03-23 17:11:26 +0000 | [diff] [blame] | 5622 | } |
drh | 3c23a88 | 2007-01-09 14:01:13 +0000 | [diff] [blame] | 5623 | } |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 5624 | if( rc ){ |
| 5625 | sqlite3ResetAllSchemasOfConnection(db); |
| 5626 | if( rc==SQLITE_NOMEM ){ |
| 5627 | goto no_mem; |
| 5628 | } |
| 5629 | goto abort_due_to_error; |
danielk1977 | 261919c | 2005-12-06 12:52:59 +0000 | [diff] [blame] | 5630 | } |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 5631 | break; |
| 5632 | } |
| 5633 | |
drh | 8bfdf72 | 2009-06-19 14:06:03 +0000 | [diff] [blame] | 5634 | #if !defined(SQLITE_OMIT_ANALYZE) |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 5635 | /* Opcode: LoadAnalysis P1 * * * * |
drh | 497e446 | 2005-07-23 03:18:40 +0000 | [diff] [blame] | 5636 | ** |
| 5637 | ** Read the sqlite_stat1 table for database P1 and load the content |
| 5638 | ** of that table into the internal index hash table. This will cause |
| 5639 | ** the analysis to be used when preparing all subsequent queries. |
| 5640 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 5641 | case OP_LoadAnalysis: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 5642 | assert( pOp->p1>=0 && pOp->p1<db->nDb ); |
| 5643 | rc = sqlite3AnalysisLoad(db, pOp->p1); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 5644 | if( rc ) goto abort_due_to_error; |
drh | 497e446 | 2005-07-23 03:18:40 +0000 | [diff] [blame] | 5645 | break; |
| 5646 | } |
drh | 8bfdf72 | 2009-06-19 14:06:03 +0000 | [diff] [blame] | 5647 | #endif /* !defined(SQLITE_OMIT_ANALYZE) */ |
drh | 497e446 | 2005-07-23 03:18:40 +0000 | [diff] [blame] | 5648 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 5649 | /* Opcode: DropTable P1 * * P4 * |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 5650 | ** |
| 5651 | ** Remove the internal (in-memory) data structures that describe |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 5652 | ** the table named P4 in database P1. This is called after a table |
drh | 5dad9a3 | 2014-07-25 18:37:42 +0000 | [diff] [blame] | 5653 | ** is dropped from disk (using the Destroy opcode) in order to keep |
| 5654 | ** the internal representation of the |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 5655 | ** schema consistent with what is on disk. |
| 5656 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 5657 | case OP_DropTable: { |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 5658 | sqlite3UnlinkAndDeleteTable(db, pOp->p1, pOp->p4.z); |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 5659 | break; |
| 5660 | } |
| 5661 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 5662 | /* Opcode: DropIndex P1 * * P4 * |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 5663 | ** |
| 5664 | ** Remove the internal (in-memory) data structures that describe |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 5665 | ** the index named P4 in database P1. This is called after an index |
drh | 5dad9a3 | 2014-07-25 18:37:42 +0000 | [diff] [blame] | 5666 | ** is dropped from disk (using the Destroy opcode) |
| 5667 | ** in order to keep the internal representation of the |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 5668 | ** schema consistent with what is on disk. |
| 5669 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 5670 | case OP_DropIndex: { |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 5671 | sqlite3UnlinkAndDeleteIndex(db, pOp->p1, pOp->p4.z); |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 5672 | break; |
| 5673 | } |
| 5674 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 5675 | /* Opcode: DropTrigger P1 * * P4 * |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 5676 | ** |
| 5677 | ** Remove the internal (in-memory) data structures that describe |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 5678 | ** the trigger named P4 in database P1. This is called after a trigger |
drh | 5dad9a3 | 2014-07-25 18:37:42 +0000 | [diff] [blame] | 5679 | ** is dropped from disk (using the Destroy opcode) in order to keep |
| 5680 | ** the internal representation of the |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 5681 | ** schema consistent with what is on disk. |
| 5682 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 5683 | case OP_DropTrigger: { |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 5684 | sqlite3UnlinkAndDeleteTrigger(db, pOp->p1, pOp->p4.z); |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 5685 | break; |
| 5686 | } |
| 5687 | |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 5688 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 5689 | #ifndef SQLITE_OMIT_INTEGRITY_CHECK |
drh | 98968b2 | 2016-03-15 22:00:39 +0000 | [diff] [blame] | 5690 | /* Opcode: IntegrityCk P1 P2 P3 P4 P5 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5691 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 5692 | ** Do an analysis of the currently open database. Store in |
| 5693 | ** register P1 the text of an error message describing any problems. |
| 5694 | ** If no problems are found, store a NULL in register P1. |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 5695 | ** |
drh | 66accfc | 2017-02-22 18:04:42 +0000 | [diff] [blame] | 5696 | ** The register P3 contains one less than the maximum number of allowed errors. |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 5697 | ** At most reg(P3) errors will be reported. |
| 5698 | ** In other words, the analysis stops as soon as reg(P1) errors are |
| 5699 | ** seen. Reg(P1) is updated with the number of errors remaining. |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 5700 | ** |
drh | 98968b2 | 2016-03-15 22:00:39 +0000 | [diff] [blame] | 5701 | ** The root page numbers of all tables in the database are integers |
| 5702 | ** stored in P4_INTARRAY argument. |
drh | 2150432 | 2002-06-25 13:16:02 +0000 | [diff] [blame] | 5703 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 5704 | ** If P5 is not zero, the check is done on the auxiliary database |
drh | 2150432 | 2002-06-25 13:16:02 +0000 | [diff] [blame] | 5705 | ** file, not the main database file. |
drh | 1dd397f | 2002-02-03 03:34:07 +0000 | [diff] [blame] | 5706 | ** |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 5707 | ** This opcode is used to implement the integrity_check pragma. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5708 | */ |
drh | aaab572 | 2002-02-19 13:39:21 +0000 | [diff] [blame] | 5709 | case OP_IntegrityCk: { |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 5710 | int nRoot; /* Number of tables to check. (Number of root pages.) */ |
| 5711 | int *aRoot; /* Array of rootpage numbers for tables to be checked */ |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 5712 | int nErr; /* Number of errors reported */ |
| 5713 | char *z; /* Text of the error report */ |
| 5714 | Mem *pnErr; /* Register keeping track of errors remaining */ |
drh | 9e92a47 | 2013-06-27 17:40:30 +0000 | [diff] [blame] | 5715 | |
drh | 1713afb | 2013-06-28 01:24:57 +0000 | [diff] [blame] | 5716 | assert( p->bIsReader ); |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 5717 | nRoot = pOp->p2; |
drh | 98968b2 | 2016-03-15 22:00:39 +0000 | [diff] [blame] | 5718 | aRoot = pOp->p4.ai; |
drh | 7906975 | 2004-05-22 21:30:40 +0000 | [diff] [blame] | 5719 | assert( nRoot>0 ); |
drh | b5c1063 | 2017-09-21 00:49:15 +0000 | [diff] [blame] | 5720 | assert( aRoot[0]==nRoot ); |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 5721 | assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) ); |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 5722 | pnErr = &aMem[pOp->p3]; |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 5723 | assert( (pnErr->flags & MEM_Int)!=0 ); |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 5724 | assert( (pnErr->flags & (MEM_Str|MEM_Blob))==0 ); |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 5725 | pIn1 = &aMem[pOp->p1]; |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 5726 | assert( pOp->p5<db->nDb ); |
drh | a7ab6d8 | 2014-07-21 15:44:39 +0000 | [diff] [blame] | 5727 | assert( DbMaskTest(p->btreeMask, pOp->p5) ); |
drh | b5c1063 | 2017-09-21 00:49:15 +0000 | [diff] [blame] | 5728 | z = sqlite3BtreeIntegrityCheck(db->aDb[pOp->p5].pBt, &aRoot[1], nRoot, |
drh | 66accfc | 2017-02-22 18:04:42 +0000 | [diff] [blame] | 5729 | (int)pnErr->u.i+1, &nErr); |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 5730 | sqlite3VdbeMemSetNull(pIn1); |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 5731 | if( nErr==0 ){ |
| 5732 | assert( z==0 ); |
drh | c890fec | 2008-08-01 20:10:08 +0000 | [diff] [blame] | 5733 | }else if( z==0 ){ |
| 5734 | goto no_mem; |
drh | 1dd397f | 2002-02-03 03:34:07 +0000 | [diff] [blame] | 5735 | }else{ |
drh | 66accfc | 2017-02-22 18:04:42 +0000 | [diff] [blame] | 5736 | pnErr->u.i -= nErr-1; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 5737 | sqlite3VdbeMemSetStr(pIn1, z, -1, SQLITE_UTF8, sqlite3_free); |
danielk1977 | 8a6b541 | 2004-05-24 07:04:25 +0000 | [diff] [blame] | 5738 | } |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 5739 | UPDATE_MAX_BLOBSIZE(pIn1); |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 5740 | sqlite3VdbeChangeEncoding(pIn1, encoding); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5741 | break; |
| 5742 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 5743 | #endif /* SQLITE_OMIT_INTEGRITY_CHECK */ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5744 | |
drh | 3d4501e | 2008-12-04 20:40:10 +0000 | [diff] [blame] | 5745 | /* Opcode: RowSetAdd P1 P2 * * * |
drh | 72e26de | 2016-08-24 21:24:04 +0000 | [diff] [blame] | 5746 | ** Synopsis: rowset(P1)=r[P2] |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5747 | ** |
drh | bb6783b | 2017-04-29 18:02:49 +0000 | [diff] [blame] | 5748 | ** Insert the integer value held by register P2 into a RowSet object |
drh | 3d4501e | 2008-12-04 20:40:10 +0000 | [diff] [blame] | 5749 | ** held in register P1. |
| 5750 | ** |
| 5751 | ** An assertion fails if P2 is not an integer. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5752 | */ |
drh | 93952eb | 2009-11-13 19:43:43 +0000 | [diff] [blame] | 5753 | case OP_RowSetAdd: { /* in1, in2 */ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 5754 | pIn1 = &aMem[pOp->p1]; |
| 5755 | pIn2 = &aMem[pOp->p2]; |
drh | 93952eb | 2009-11-13 19:43:43 +0000 | [diff] [blame] | 5756 | assert( (pIn2->flags & MEM_Int)!=0 ); |
| 5757 | if( (pIn1->flags & MEM_RowSet)==0 ){ |
| 5758 | sqlite3VdbeMemSetRowSet(pIn1); |
| 5759 | if( (pIn1->flags & MEM_RowSet)==0 ) goto no_mem; |
drh | 3d4501e | 2008-12-04 20:40:10 +0000 | [diff] [blame] | 5760 | } |
drh | 93952eb | 2009-11-13 19:43:43 +0000 | [diff] [blame] | 5761 | sqlite3RowSetInsert(pIn1->u.pRowSet, pIn2->u.i); |
drh | 3d4501e | 2008-12-04 20:40:10 +0000 | [diff] [blame] | 5762 | break; |
| 5763 | } |
| 5764 | |
| 5765 | /* Opcode: RowSetRead P1 P2 P3 * * |
drh | 72e26de | 2016-08-24 21:24:04 +0000 | [diff] [blame] | 5766 | ** Synopsis: r[P3]=rowset(P1) |
drh | 3d4501e | 2008-12-04 20:40:10 +0000 | [diff] [blame] | 5767 | ** |
drh | bb6783b | 2017-04-29 18:02:49 +0000 | [diff] [blame] | 5768 | ** Extract the smallest value from the RowSet object in P1 |
| 5769 | ** and put that value into register P3. |
| 5770 | ** Or, if RowSet object P1 is initially empty, leave P3 |
drh | 3d4501e | 2008-12-04 20:40:10 +0000 | [diff] [blame] | 5771 | ** unchanged and jump to instruction P2. |
| 5772 | */ |
drh | 93952eb | 2009-11-13 19:43:43 +0000 | [diff] [blame] | 5773 | case OP_RowSetRead: { /* jump, in1, out3 */ |
drh | 3d4501e | 2008-12-04 20:40:10 +0000 | [diff] [blame] | 5774 | i64 val; |
drh | 49afe3a | 2013-07-10 03:05:14 +0000 | [diff] [blame] | 5775 | |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 5776 | pIn1 = &aMem[pOp->p1]; |
drh | 93952eb | 2009-11-13 19:43:43 +0000 | [diff] [blame] | 5777 | if( (pIn1->flags & MEM_RowSet)==0 |
| 5778 | || sqlite3RowSetNext(pIn1->u.pRowSet, &val)==0 |
drh | 3d4501e | 2008-12-04 20:40:10 +0000 | [diff] [blame] | 5779 | ){ |
| 5780 | /* The boolean index is empty */ |
drh | 93952eb | 2009-11-13 19:43:43 +0000 | [diff] [blame] | 5781 | sqlite3VdbeMemSetNull(pIn1); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 5782 | VdbeBranchTaken(1,2); |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 5783 | goto jump_to_p2_and_check_for_interrupt; |
drh | 3d4501e | 2008-12-04 20:40:10 +0000 | [diff] [blame] | 5784 | }else{ |
| 5785 | /* A value was pulled from the index */ |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 5786 | VdbeBranchTaken(0,2); |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 5787 | sqlite3VdbeMemSetInt64(&aMem[pOp->p3], val); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 5788 | } |
drh | 49afe3a | 2013-07-10 03:05:14 +0000 | [diff] [blame] | 5789 | goto check_for_interrupt; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5790 | } |
| 5791 | |
drh | 1b26c7c | 2009-04-22 02:15:47 +0000 | [diff] [blame] | 5792 | /* Opcode: RowSetTest P1 P2 P3 P4 |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 5793 | ** Synopsis: if r[P3] in rowset(P1) goto P2 |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 5794 | ** |
drh | ade9760 | 2009-04-21 15:05:18 +0000 | [diff] [blame] | 5795 | ** 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] | 5796 | ** contains a RowSet object and that RowSet object contains |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 5797 | ** the value held in P3, jump to register P2. Otherwise, insert the |
drh | 1b26c7c | 2009-04-22 02:15:47 +0000 | [diff] [blame] | 5798 | ** integer in P3 into the RowSet and continue on to the |
drh | ade9760 | 2009-04-21 15:05:18 +0000 | [diff] [blame] | 5799 | ** next opcode. |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 5800 | ** |
drh | bb6783b | 2017-04-29 18:02:49 +0000 | [diff] [blame] | 5801 | ** The RowSet object is optimized for the case where sets of integers |
| 5802 | ** are inserted in distinct phases, which each set contains no duplicates. |
| 5803 | ** Each set is identified by a unique P4 value. The first set |
| 5804 | ** must have P4==0, the final set must have P4==-1, and for all other sets |
| 5805 | ** must have P4>0. |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 5806 | ** |
| 5807 | ** This allows optimizations: (a) when P4==0 there is no need to test |
drh | bb6783b | 2017-04-29 18:02:49 +0000 | [diff] [blame] | 5808 | ** the RowSet object for P3, as it is guaranteed not to contain it, |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 5809 | ** (b) when P4==-1 there is no need to insert the value, as it will |
| 5810 | ** never be tested for, and (c) when a value that is part of set X is |
| 5811 | ** inserted, there is no need to search to see if the same value was |
| 5812 | ** previously inserted as part of set X (only if it was previously |
| 5813 | ** inserted as part of some other set). |
| 5814 | */ |
drh | 1b26c7c | 2009-04-22 02:15:47 +0000 | [diff] [blame] | 5815 | case OP_RowSetTest: { /* jump, in1, in3 */ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 5816 | int iSet; |
| 5817 | int exists; |
| 5818 | |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 5819 | pIn1 = &aMem[pOp->p1]; |
| 5820 | pIn3 = &aMem[pOp->p3]; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 5821 | iSet = pOp->p4.i; |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 5822 | assert( pIn3->flags&MEM_Int ); |
| 5823 | |
drh | 1b26c7c | 2009-04-22 02:15:47 +0000 | [diff] [blame] | 5824 | /* If there is anything other than a rowset object in memory cell P1, |
| 5825 | ** delete it now and initialize P1 with an empty rowset |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 5826 | */ |
drh | 733bf1b | 2009-04-22 00:47:00 +0000 | [diff] [blame] | 5827 | if( (pIn1->flags & MEM_RowSet)==0 ){ |
| 5828 | sqlite3VdbeMemSetRowSet(pIn1); |
| 5829 | if( (pIn1->flags & MEM_RowSet)==0 ) goto no_mem; |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 5830 | } |
| 5831 | |
| 5832 | assert( pOp->p4type==P4_INT32 ); |
drh | 1b26c7c | 2009-04-22 02:15:47 +0000 | [diff] [blame] | 5833 | assert( iSet==-1 || iSet>=0 ); |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 5834 | if( iSet ){ |
drh | d83cad2 | 2014-04-10 02:24:48 +0000 | [diff] [blame] | 5835 | exists = sqlite3RowSetTest(pIn1->u.pRowSet, iSet, pIn3->u.i); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 5836 | VdbeBranchTaken(exists!=0,2); |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 5837 | if( exists ) goto jump_to_p2; |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 5838 | } |
| 5839 | if( iSet>=0 ){ |
drh | 733bf1b | 2009-04-22 00:47:00 +0000 | [diff] [blame] | 5840 | sqlite3RowSetInsert(pIn1->u.pRowSet, pIn3->u.i); |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 5841 | } |
| 5842 | break; |
| 5843 | } |
| 5844 | |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5845 | |
danielk1977 | 93758c8 | 2005-01-21 08:13:14 +0000 | [diff] [blame] | 5846 | #ifndef SQLITE_OMIT_TRIGGER |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 5847 | |
drh | 0fd6135 | 2014-02-07 02:29:45 +0000 | [diff] [blame] | 5848 | /* Opcode: Program P1 P2 P3 P4 P5 |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 5849 | ** |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 5850 | ** Execute the trigger program passed as P4 (type P4_SUBPROGRAM). |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 5851 | ** |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 5852 | ** P1 contains the address of the memory cell that contains the first memory |
| 5853 | ** cell in an array of values used as arguments to the sub-program. P2 |
| 5854 | ** contains the address to jump to if the sub-program throws an IGNORE |
| 5855 | ** exception using the RAISE() function. Register P3 contains the address |
| 5856 | ** of a memory cell in this (the parent) VM that is used to allocate the |
| 5857 | ** memory required by the sub-vdbe at runtime. |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 5858 | ** |
| 5859 | ** P4 is a pointer to the VM containing the trigger program. |
drh | 0fd6135 | 2014-02-07 02:29:45 +0000 | [diff] [blame] | 5860 | ** |
| 5861 | ** If P5 is non-zero, then recursive program invocation is enabled. |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 5862 | */ |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 5863 | case OP_Program: { /* jump */ |
dan | 65a7cd1 | 2009-09-01 12:16:01 +0000 | [diff] [blame] | 5864 | int nMem; /* Number of memory registers for sub-program */ |
| 5865 | int nByte; /* Bytes of runtime space required for sub-program */ |
| 5866 | Mem *pRt; /* Register to allocate runtime space */ |
| 5867 | Mem *pMem; /* Used to iterate through memory cells */ |
| 5868 | Mem *pEnd; /* Last memory cell in new array */ |
| 5869 | VdbeFrame *pFrame; /* New vdbe frame to execute in */ |
| 5870 | SubProgram *pProgram; /* Sub-program to execute */ |
| 5871 | void *t; /* Token identifying trigger */ |
| 5872 | |
| 5873 | pProgram = pOp->p4.pProgram; |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 5874 | pRt = &aMem[pOp->p3]; |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 5875 | assert( pProgram->nOp>0 ); |
| 5876 | |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 5877 | /* If the p5 flag is clear, then recursive invocation of triggers is |
| 5878 | ** disabled for backwards compatibility (p5 is set if this sub-program |
| 5879 | ** is really a trigger, not a foreign key action, and the flag set |
| 5880 | ** and cleared by the "PRAGMA recursive_triggers" command is clear). |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 5881 | ** |
| 5882 | ** It is recursive invocation of triggers, at the SQL level, that is |
| 5883 | ** disabled. In some cases a single trigger may generate more than one |
| 5884 | ** SubProgram (if the trigger may be executed with more than one different |
| 5885 | ** ON CONFLICT algorithm). SubProgram structures associated with a |
| 5886 | ** single trigger all have the same value for the SubProgram.token |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 5887 | ** variable. */ |
| 5888 | if( pOp->p5 ){ |
dan | 65a7cd1 | 2009-09-01 12:16:01 +0000 | [diff] [blame] | 5889 | t = pProgram->token; |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 5890 | for(pFrame=p->pFrame; pFrame && pFrame->token!=t; pFrame=pFrame->pParent); |
| 5891 | if( pFrame ) break; |
| 5892 | } |
| 5893 | |
dan | f589450 | 2009-10-07 18:41:19 +0000 | [diff] [blame] | 5894 | if( p->nFrame>=db->aLimit[SQLITE_LIMIT_TRIGGER_DEPTH] ){ |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 5895 | rc = SQLITE_ERROR; |
drh | 22c17b8 | 2015-05-15 04:13:15 +0000 | [diff] [blame] | 5896 | sqlite3VdbeError(p, "too many levels of trigger recursion"); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 5897 | goto abort_due_to_error; |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 5898 | } |
| 5899 | |
| 5900 | /* Register pRt is used to store the memory required to save the state |
| 5901 | ** of the current program, and the memory required at runtime to execute |
| 5902 | ** the trigger program. If this trigger has been fired before, then pRt |
| 5903 | ** is already allocated. Otherwise, it must be initialized. */ |
| 5904 | if( (pRt->flags&MEM_Frame)==0 ){ |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 5905 | /* SubProgram.nMem is set to the number of memory cells used by the |
| 5906 | ** program stored in SubProgram.aOp. As well as these, one memory |
| 5907 | ** cell is required for each cursor used by the program. Set local |
| 5908 | ** variable nMem (and later, VdbeFrame.nChildMem) to this value. |
| 5909 | */ |
dan | 65a7cd1 | 2009-09-01 12:16:01 +0000 | [diff] [blame] | 5910 | nMem = pProgram->nMem + pProgram->nCsr; |
drh | 3cdce92 | 2016-03-21 00:30:40 +0000 | [diff] [blame] | 5911 | assert( nMem>0 ); |
| 5912 | if( pProgram->nCsr==0 ) nMem++; |
dan | 65a7cd1 | 2009-09-01 12:16:01 +0000 | [diff] [blame] | 5913 | nByte = ROUND8(sizeof(VdbeFrame)) |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 5914 | + nMem * sizeof(Mem) |
drh | ab087d4 | 2017-03-24 17:59:56 +0000 | [diff] [blame] | 5915 | + pProgram->nCsr * sizeof(VdbeCursor*) |
| 5916 | + (pProgram->nOp + 7)/8; |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 5917 | pFrame = sqlite3DbMallocZero(db, nByte); |
| 5918 | if( !pFrame ){ |
| 5919 | goto no_mem; |
| 5920 | } |
| 5921 | sqlite3VdbeMemRelease(pRt); |
| 5922 | pRt->flags = MEM_Frame; |
| 5923 | pRt->u.pFrame = pFrame; |
| 5924 | |
| 5925 | pFrame->v = p; |
| 5926 | pFrame->nChildMem = nMem; |
| 5927 | pFrame->nChildCsr = pProgram->nCsr; |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 5928 | pFrame->pc = (int)(pOp - aOp); |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 5929 | pFrame->aMem = p->aMem; |
| 5930 | pFrame->nMem = p->nMem; |
| 5931 | pFrame->apCsr = p->apCsr; |
| 5932 | pFrame->nCursor = p->nCursor; |
| 5933 | pFrame->aOp = p->aOp; |
| 5934 | pFrame->nOp = p->nOp; |
| 5935 | pFrame->token = pProgram->token; |
dan | e2f771b | 2014-11-03 15:33:17 +0000 | [diff] [blame] | 5936 | #ifdef SQLITE_ENABLE_STMT_SCANSTATUS |
dan | 43764a8 | 2014-11-01 21:00:04 +0000 | [diff] [blame] | 5937 | pFrame->anExec = p->anExec; |
dan | e2f771b | 2014-11-03 15:33:17 +0000 | [diff] [blame] | 5938 | #endif |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 5939 | |
| 5940 | pEnd = &VdbeFrameMem(pFrame)[pFrame->nChildMem]; |
| 5941 | for(pMem=VdbeFrameMem(pFrame); pMem!=pEnd; pMem++){ |
drh | a5750cf | 2014-02-07 13:20:31 +0000 | [diff] [blame] | 5942 | pMem->flags = MEM_Undefined; |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 5943 | pMem->db = db; |
| 5944 | } |
| 5945 | }else{ |
| 5946 | pFrame = pRt->u.pFrame; |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 5947 | assert( pProgram->nMem+pProgram->nCsr==pFrame->nChildMem |
| 5948 | || (pProgram->nCsr==0 && pProgram->nMem+1==pFrame->nChildMem) ); |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 5949 | assert( pProgram->nCsr==pFrame->nChildCsr ); |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 5950 | assert( (int)(pOp - aOp)==pFrame->pc ); |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 5951 | } |
| 5952 | |
| 5953 | p->nFrame++; |
| 5954 | pFrame->pParent = p->pFrame; |
drh | fae58d5 | 2017-01-26 17:26:44 +0000 | [diff] [blame] | 5955 | pFrame->lastRowid = db->lastRowid; |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 5956 | pFrame->nChange = p->nChange; |
dan | c3da667 | 2014-10-28 18:24:16 +0000 | [diff] [blame] | 5957 | pFrame->nDbChange = p->db->nChange; |
dan | 3200132 | 2016-02-19 18:54:29 +0000 | [diff] [blame] | 5958 | assert( pFrame->pAuxData==0 ); |
| 5959 | pFrame->pAuxData = p->pAuxData; |
| 5960 | p->pAuxData = 0; |
dan | 2832ad4 | 2009-08-31 15:27:27 +0000 | [diff] [blame] | 5961 | p->nChange = 0; |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 5962 | p->pFrame = pFrame; |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 5963 | p->aMem = aMem = VdbeFrameMem(pFrame); |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 5964 | p->nMem = pFrame->nChildMem; |
shane | cea72b2 | 2009-09-07 04:38:36 +0000 | [diff] [blame] | 5965 | p->nCursor = (u16)pFrame->nChildCsr; |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 5966 | p->apCsr = (VdbeCursor **)&aMem[p->nMem]; |
drh | ab087d4 | 2017-03-24 17:59:56 +0000 | [diff] [blame] | 5967 | pFrame->aOnce = (u8*)&p->apCsr[pProgram->nCsr]; |
drh | 18333ef | 2017-03-24 18:38:41 +0000 | [diff] [blame] | 5968 | memset(pFrame->aOnce, 0, (pProgram->nOp + 7)/8); |
drh | bbe879d | 2009-11-14 18:04:35 +0000 | [diff] [blame] | 5969 | p->aOp = aOp = pProgram->aOp; |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 5970 | p->nOp = pProgram->nOp; |
dan | e2f771b | 2014-11-03 15:33:17 +0000 | [diff] [blame] | 5971 | #ifdef SQLITE_ENABLE_STMT_SCANSTATUS |
dan | 43764a8 | 2014-11-01 21:00:04 +0000 | [diff] [blame] | 5972 | p->anExec = 0; |
dan | e2f771b | 2014-11-03 15:33:17 +0000 | [diff] [blame] | 5973 | #endif |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 5974 | pOp = &aOp[-1]; |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 5975 | |
| 5976 | break; |
| 5977 | } |
| 5978 | |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 5979 | /* Opcode: Param P1 P2 * * * |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 5980 | ** |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 5981 | ** This opcode is only ever present in sub-programs called via the |
| 5982 | ** OP_Program instruction. Copy a value currently stored in a memory |
| 5983 | ** cell of the calling (parent) frame to cell P2 in the current frames |
| 5984 | ** address space. This is used by trigger programs to access the new.* |
| 5985 | ** and old.* values. |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 5986 | ** |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 5987 | ** The address of the cell in the parent frame is determined by adding |
| 5988 | ** the value of the P1 argument to the value of the P1 argument to the |
| 5989 | ** calling OP_Program instruction. |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 5990 | */ |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 5991 | case OP_Param: { /* out2 */ |
dan | 65a7cd1 | 2009-09-01 12:16:01 +0000 | [diff] [blame] | 5992 | VdbeFrame *pFrame; |
| 5993 | Mem *pIn; |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 5994 | pOut = out2Prerelease(p, pOp); |
dan | 65a7cd1 | 2009-09-01 12:16:01 +0000 | [diff] [blame] | 5995 | pFrame = p->pFrame; |
| 5996 | pIn = &pFrame->aMem[pOp->p1 + pFrame->aOp[pFrame->pc].p1]; |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 5997 | sqlite3VdbeMemShallowCopy(pOut, pIn, MEM_Ephem); |
| 5998 | break; |
| 5999 | } |
| 6000 | |
danielk1977 | 93758c8 | 2005-01-21 08:13:14 +0000 | [diff] [blame] | 6001 | #endif /* #ifndef SQLITE_OMIT_TRIGGER */ |
rdc | b0c374f | 2004-02-20 22:53:38 +0000 | [diff] [blame] | 6002 | |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 6003 | #ifndef SQLITE_OMIT_FOREIGN_KEY |
dan | 32b09f2 | 2009-09-23 17:29:59 +0000 | [diff] [blame] | 6004 | /* Opcode: FkCounter P1 P2 * * * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 6005 | ** Synopsis: fkctr[P1]+=P2 |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 6006 | ** |
dan | 0ff297e | 2009-09-25 17:03:14 +0000 | [diff] [blame] | 6007 | ** Increment a "constraint counter" by P2 (P2 may be negative or positive). |
| 6008 | ** If P1 is non-zero, the database constraint counter is incremented |
| 6009 | ** (deferred foreign key constraints). Otherwise, if P1 is zero, the |
dan | 32b09f2 | 2009-09-23 17:29:59 +0000 | [diff] [blame] | 6010 | ** statement counter is incremented (immediate foreign key constraints). |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 6011 | */ |
dan | 32b09f2 | 2009-09-23 17:29:59 +0000 | [diff] [blame] | 6012 | case OP_FkCounter: { |
drh | 963c74d | 2013-07-11 12:19:12 +0000 | [diff] [blame] | 6013 | if( db->flags & SQLITE_DeferFKs ){ |
dan | cb3e4b7 | 2013-07-03 19:53:05 +0000 | [diff] [blame] | 6014 | db->nDeferredImmCons += pOp->p2; |
| 6015 | }else if( pOp->p1 ){ |
dan | 0ff297e | 2009-09-25 17:03:14 +0000 | [diff] [blame] | 6016 | db->nDeferredCons += pOp->p2; |
dan | 32b09f2 | 2009-09-23 17:29:59 +0000 | [diff] [blame] | 6017 | }else{ |
dan | 0ff297e | 2009-09-25 17:03:14 +0000 | [diff] [blame] | 6018 | p->nFkConstraint += pOp->p2; |
| 6019 | } |
| 6020 | break; |
| 6021 | } |
| 6022 | |
| 6023 | /* Opcode: FkIfZero P1 P2 * * * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 6024 | ** Synopsis: if fkctr[P1]==0 goto P2 |
dan | 0ff297e | 2009-09-25 17:03:14 +0000 | [diff] [blame] | 6025 | ** |
| 6026 | ** This opcode tests if a foreign key constraint-counter is currently zero. |
| 6027 | ** If so, jump to instruction P2. Otherwise, fall through to the next |
| 6028 | ** instruction. |
| 6029 | ** |
| 6030 | ** If P1 is non-zero, then the jump is taken if the database constraint-counter |
| 6031 | ** is zero (the one that counts deferred constraint violations). If P1 is |
| 6032 | ** zero, the jump is taken if the statement constraint-counter is zero |
| 6033 | ** (immediate foreign key constraint violations). |
| 6034 | */ |
| 6035 | case OP_FkIfZero: { /* jump */ |
| 6036 | if( pOp->p1 ){ |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 6037 | VdbeBranchTaken(db->nDeferredCons==0 && db->nDeferredImmCons==0, 2); |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 6038 | if( db->nDeferredCons==0 && db->nDeferredImmCons==0 ) goto jump_to_p2; |
dan | 0ff297e | 2009-09-25 17:03:14 +0000 | [diff] [blame] | 6039 | }else{ |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 6040 | VdbeBranchTaken(p->nFkConstraint==0 && db->nDeferredImmCons==0, 2); |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 6041 | if( p->nFkConstraint==0 && db->nDeferredImmCons==0 ) goto jump_to_p2; |
dan | 32b09f2 | 2009-09-23 17:29:59 +0000 | [diff] [blame] | 6042 | } |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 6043 | break; |
| 6044 | } |
| 6045 | #endif /* #ifndef SQLITE_OMIT_FOREIGN_KEY */ |
| 6046 | |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 6047 | #ifndef SQLITE_OMIT_AUTOINCREMENT |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 6048 | /* Opcode: MemMax P1 P2 * * * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 6049 | ** Synopsis: r[P1]=max(r[P1],r[P2]) |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 6050 | ** |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 6051 | ** P1 is a register in the root frame of this VM (the root frame is |
| 6052 | ** different from the current frame if this instruction is being executed |
| 6053 | ** within a sub-program). Set the value of register P1 to the maximum of |
| 6054 | ** its current value and the value in register P2. |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 6055 | ** |
| 6056 | ** This instruction throws an error if the memory cell is not initially |
| 6057 | ** an integer. |
| 6058 | */ |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 6059 | case OP_MemMax: { /* in2 */ |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 6060 | VdbeFrame *pFrame; |
| 6061 | if( p->pFrame ){ |
| 6062 | for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent); |
| 6063 | pIn1 = &pFrame->aMem[pOp->p1]; |
| 6064 | }else{ |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 6065 | pIn1 = &aMem[pOp->p1]; |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 6066 | } |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 6067 | assert( memIsValid(pIn1) ); |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 6068 | sqlite3VdbeMemIntegerify(pIn1); |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 6069 | pIn2 = &aMem[pOp->p2]; |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 6070 | sqlite3VdbeMemIntegerify(pIn2); |
| 6071 | if( pIn1->u.i<pIn2->u.i){ |
| 6072 | pIn1->u.i = pIn2->u.i; |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 6073 | } |
| 6074 | break; |
| 6075 | } |
| 6076 | #endif /* SQLITE_OMIT_AUTOINCREMENT */ |
| 6077 | |
drh | 8b0cf38 | 2015-10-06 21:07:06 +0000 | [diff] [blame] | 6078 | /* Opcode: IfPos P1 P2 P3 * * |
| 6079 | ** Synopsis: if r[P1]>0 then r[P1]-=P3, goto P2 |
danielk1977 | a2dc3b1 | 2005-02-05 12:48:48 +0000 | [diff] [blame] | 6080 | ** |
drh | 1689707 | 2015-03-07 00:57:37 +0000 | [diff] [blame] | 6081 | ** Register P1 must contain an integer. |
mistachkin | 91a3ecb | 2015-10-06 21:49:55 +0000 | [diff] [blame] | 6082 | ** 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] | 6083 | ** value in P1 and jump to P2. |
drh | 6f58f70 | 2006-01-08 05:26:41 +0000 | [diff] [blame] | 6084 | ** |
drh | 1689707 | 2015-03-07 00:57:37 +0000 | [diff] [blame] | 6085 | ** If the initial value of register P1 is less than 1, then the |
| 6086 | ** value is unchanged and control passes through to the next instruction. |
danielk1977 | a2dc3b1 | 2005-02-05 12:48:48 +0000 | [diff] [blame] | 6087 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 6088 | case OP_IfPos: { /* jump, in1 */ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 6089 | pIn1 = &aMem[pOp->p1]; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 6090 | assert( pIn1->flags&MEM_Int ); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 6091 | VdbeBranchTaken( pIn1->u.i>0, 2); |
drh | 8b0cf38 | 2015-10-06 21:07:06 +0000 | [diff] [blame] | 6092 | if( pIn1->u.i>0 ){ |
| 6093 | pIn1->u.i -= pOp->p3; |
| 6094 | goto jump_to_p2; |
| 6095 | } |
drh | ec7429a | 2005-10-06 16:53:14 +0000 | [diff] [blame] | 6096 | break; |
| 6097 | } |
| 6098 | |
drh | cc2fa4c | 2016-01-25 15:57:29 +0000 | [diff] [blame] | 6099 | /* Opcode: OffsetLimit P1 P2 P3 * * |
| 6100 | ** 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] | 6101 | ** |
drh | cc2fa4c | 2016-01-25 15:57:29 +0000 | [diff] [blame] | 6102 | ** This opcode performs a commonly used computation associated with |
| 6103 | ** LIMIT and OFFSET process. r[P1] holds the limit counter. r[P3] |
| 6104 | ** holds the offset counter. The opcode computes the combined value |
| 6105 | ** of the LIMIT and OFFSET and stores that value in r[P2]. The r[P2] |
| 6106 | ** value computed is the total number of rows that will need to be |
| 6107 | ** visited in order to complete the query. |
| 6108 | ** |
| 6109 | ** If r[P3] is zero or negative, that means there is no OFFSET |
| 6110 | ** and r[P2] is set to be the value of the LIMIT, r[P1]. |
| 6111 | ** |
| 6112 | ** if r[P1] is zero or negative, that means there is no LIMIT |
| 6113 | ** and r[P2] is set to -1. |
| 6114 | ** |
| 6115 | ** 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] | 6116 | */ |
drh | cc2fa4c | 2016-01-25 15:57:29 +0000 | [diff] [blame] | 6117 | case OP_OffsetLimit: { /* in1, out2, in3 */ |
drh | 719da30 | 2016-12-10 04:06:49 +0000 | [diff] [blame] | 6118 | i64 x; |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 6119 | pIn1 = &aMem[pOp->p1]; |
drh | cc2fa4c | 2016-01-25 15:57:29 +0000 | [diff] [blame] | 6120 | pIn3 = &aMem[pOp->p3]; |
| 6121 | pOut = out2Prerelease(p, pOp); |
| 6122 | assert( pIn1->flags & MEM_Int ); |
| 6123 | assert( pIn3->flags & MEM_Int ); |
drh | 719da30 | 2016-12-10 04:06:49 +0000 | [diff] [blame] | 6124 | x = pIn1->u.i; |
| 6125 | if( x<=0 || sqlite3AddInt64(&x, pIn3->u.i>0?pIn3->u.i:0) ){ |
| 6126 | /* If the LIMIT is less than or equal to zero, loop forever. This |
| 6127 | ** is documented. But also, if the LIMIT+OFFSET exceeds 2^63 then |
| 6128 | ** also loop forever. This is undocumented. In fact, one could argue |
| 6129 | ** that the loop should terminate. But assuming 1 billion iterations |
| 6130 | ** per second (far exceeding the capabilities of any current hardware) |
| 6131 | ** it would take nearly 300 years to actually reach the limit. So |
| 6132 | ** looping forever is a reasonable approximation. */ |
| 6133 | pOut->u.i = -1; |
| 6134 | }else{ |
| 6135 | pOut->u.i = x; |
| 6136 | } |
drh | 15007a9 | 2006-01-08 18:10:17 +0000 | [diff] [blame] | 6137 | break; |
| 6138 | } |
| 6139 | |
drh | f99dd35 | 2016-12-18 17:42:00 +0000 | [diff] [blame] | 6140 | /* Opcode: IfNotZero P1 P2 * * * |
| 6141 | ** Synopsis: if r[P1]!=0 then r[P1]--, goto P2 |
drh | ec7429a | 2005-10-06 16:53:14 +0000 | [diff] [blame] | 6142 | ** |
drh | 1689707 | 2015-03-07 00:57:37 +0000 | [diff] [blame] | 6143 | ** Register P1 must contain an integer. If the content of register P1 is |
drh | f99dd35 | 2016-12-18 17:42:00 +0000 | [diff] [blame] | 6144 | ** initially greater than zero, then decrement the value in register P1. |
| 6145 | ** If it is non-zero (negative or positive) and then also jump to P2. |
| 6146 | ** If register P1 is initially zero, leave it unchanged and fall through. |
drh | ec7429a | 2005-10-06 16:53:14 +0000 | [diff] [blame] | 6147 | */ |
drh | 1689707 | 2015-03-07 00:57:37 +0000 | [diff] [blame] | 6148 | case OP_IfNotZero: { /* jump, in1 */ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 6149 | pIn1 = &aMem[pOp->p1]; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 6150 | assert( pIn1->flags&MEM_Int ); |
drh | 1689707 | 2015-03-07 00:57:37 +0000 | [diff] [blame] | 6151 | VdbeBranchTaken(pIn1->u.i<0, 2); |
| 6152 | if( pIn1->u.i ){ |
drh | f99dd35 | 2016-12-18 17:42:00 +0000 | [diff] [blame] | 6153 | if( pIn1->u.i>0 ) pIn1->u.i--; |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 6154 | goto jump_to_p2; |
drh | 1689707 | 2015-03-07 00:57:37 +0000 | [diff] [blame] | 6155 | } |
| 6156 | break; |
| 6157 | } |
| 6158 | |
| 6159 | /* Opcode: DecrJumpZero P1 P2 * * * |
| 6160 | ** Synopsis: if (--r[P1])==0 goto P2 |
| 6161 | ** |
drh | ab5be2e | 2016-11-30 05:08:59 +0000 | [diff] [blame] | 6162 | ** Register P1 must hold an integer. Decrement the value in P1 |
| 6163 | ** and jump to P2 if the new value is exactly zero. |
drh | 1689707 | 2015-03-07 00:57:37 +0000 | [diff] [blame] | 6164 | */ |
| 6165 | case OP_DecrJumpZero: { /* jump, in1 */ |
| 6166 | pIn1 = &aMem[pOp->p1]; |
| 6167 | assert( pIn1->flags&MEM_Int ); |
drh | ab5be2e | 2016-11-30 05:08:59 +0000 | [diff] [blame] | 6168 | if( pIn1->u.i>SMALLEST_INT64 ) pIn1->u.i--; |
| 6169 | VdbeBranchTaken(pIn1->u.i==0, 2); |
| 6170 | if( pIn1->u.i==0 ) goto jump_to_p2; |
drh | a2a49dc | 2008-01-02 14:28:13 +0000 | [diff] [blame] | 6171 | break; |
| 6172 | } |
| 6173 | |
drh | 1689707 | 2015-03-07 00:57:37 +0000 | [diff] [blame] | 6174 | |
drh | e2d9e7c | 2015-06-26 18:47:53 +0000 | [diff] [blame] | 6175 | /* Opcode: AggStep0 * P2 P3 P4 P5 |
drh | f63552b | 2013-10-30 00:25:03 +0000 | [diff] [blame] | 6176 | ** Synopsis: accum=r[P3] step(r[P2@P5]) |
drh | e509535 | 2002-02-24 03:25:14 +0000 | [diff] [blame] | 6177 | ** |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 6178 | ** Execute the step function for an aggregate. The |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 6179 | ** function has P5 arguments. P4 is a pointer to the FuncDef |
drh | e2d9e7c | 2015-06-26 18:47:53 +0000 | [diff] [blame] | 6180 | ** structure that specifies the function. Register P3 is the |
| 6181 | ** accumulator. |
drh | e509535 | 2002-02-24 03:25:14 +0000 | [diff] [blame] | 6182 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 6183 | ** The P5 arguments are taken from register P2 and its |
| 6184 | ** successors. |
drh | e509535 | 2002-02-24 03:25:14 +0000 | [diff] [blame] | 6185 | */ |
drh | e2d9e7c | 2015-06-26 18:47:53 +0000 | [diff] [blame] | 6186 | /* Opcode: AggStep * P2 P3 P4 P5 |
| 6187 | ** Synopsis: accum=r[P3] step(r[P2@P5]) |
| 6188 | ** |
| 6189 | ** Execute the step function for an aggregate. The |
| 6190 | ** function has P5 arguments. P4 is a pointer to an sqlite3_context |
| 6191 | ** object that is used to run the function. Register P3 is |
| 6192 | ** as the accumulator. |
| 6193 | ** |
| 6194 | ** The P5 arguments are taken from register P2 and its |
| 6195 | ** successors. |
| 6196 | ** |
| 6197 | ** This opcode is initially coded as OP_AggStep0. On first evaluation, |
| 6198 | ** the FuncDef stored in P4 is converted into an sqlite3_context and |
| 6199 | ** the opcode is changed. In this way, the initialization of the |
| 6200 | ** sqlite3_context only happens once, instead of on each call to the |
| 6201 | ** step function. |
| 6202 | */ |
drh | 9c7c913 | 2015-06-26 18:16:52 +0000 | [diff] [blame] | 6203 | case OP_AggStep0: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 6204 | int n; |
drh | 9c7c913 | 2015-06-26 18:16:52 +0000 | [diff] [blame] | 6205 | sqlite3_context *pCtx; |
drh | e509535 | 2002-02-24 03:25:14 +0000 | [diff] [blame] | 6206 | |
drh | 9c7c913 | 2015-06-26 18:16:52 +0000 | [diff] [blame] | 6207 | assert( pOp->p4type==P4_FUNCDEF ); |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 6208 | n = pOp->p5; |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 6209 | assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) ); |
| 6210 | 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] | 6211 | assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n ); |
drh | f09ac0b | 2018-01-23 03:44:06 +0000 | [diff] [blame] | 6212 | pCtx = sqlite3DbMallocRawNN(db, n*sizeof(sqlite3_value*) + |
| 6213 | (sizeof(pCtx[0]) + sizeof(Mem) - sizeof(sqlite3_value*))); |
drh | 9c7c913 | 2015-06-26 18:16:52 +0000 | [diff] [blame] | 6214 | if( pCtx==0 ) goto no_mem; |
| 6215 | pCtx->pMem = 0; |
drh | f09ac0b | 2018-01-23 03:44:06 +0000 | [diff] [blame] | 6216 | pCtx->pOut = (Mem*)&(pCtx->argv[n]); |
| 6217 | sqlite3VdbeMemInit(pCtx->pOut, db, MEM_Null); |
drh | 9c7c913 | 2015-06-26 18:16:52 +0000 | [diff] [blame] | 6218 | pCtx->pFunc = pOp->p4.pFunc; |
| 6219 | pCtx->iOp = (int)(pOp - aOp); |
| 6220 | pCtx->pVdbe = p; |
drh | f09ac0b | 2018-01-23 03:44:06 +0000 | [diff] [blame] | 6221 | pCtx->skipFlag = 0; |
| 6222 | pCtx->isError = 0; |
drh | 9c7c913 | 2015-06-26 18:16:52 +0000 | [diff] [blame] | 6223 | pCtx->argc = n; |
| 6224 | pOp->p4type = P4_FUNCCTX; |
| 6225 | pOp->p4.pCtx = pCtx; |
| 6226 | pOp->opcode = OP_AggStep; |
| 6227 | /* Fall through into OP_AggStep */ |
| 6228 | } |
| 6229 | case OP_AggStep: { |
| 6230 | int i; |
| 6231 | sqlite3_context *pCtx; |
| 6232 | Mem *pMem; |
drh | 9c7c913 | 2015-06-26 18:16:52 +0000 | [diff] [blame] | 6233 | |
| 6234 | assert( pOp->p4type==P4_FUNCCTX ); |
| 6235 | pCtx = pOp->p4.pCtx; |
| 6236 | pMem = &aMem[pOp->p3]; |
| 6237 | |
| 6238 | /* If this function is inside of a trigger, the register array in aMem[] |
| 6239 | ** might change from one evaluation to the next. The next block of code |
| 6240 | ** checks to see if the register array has changed, and if so it |
| 6241 | ** reinitializes the relavant parts of the sqlite3_context object */ |
| 6242 | if( pCtx->pMem != pMem ){ |
| 6243 | pCtx->pMem = pMem; |
| 6244 | for(i=pCtx->argc-1; i>=0; i--) pCtx->argv[i] = &aMem[pOp->p2+i]; |
| 6245 | } |
| 6246 | |
| 6247 | #ifdef SQLITE_DEBUG |
| 6248 | for(i=0; i<pCtx->argc; i++){ |
| 6249 | assert( memIsValid(pCtx->argv[i]) ); |
| 6250 | REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]); |
| 6251 | } |
| 6252 | #endif |
| 6253 | |
drh | abfcea2 | 2005-09-06 20:36:48 +0000 | [diff] [blame] | 6254 | pMem->n++; |
drh | f09ac0b | 2018-01-23 03:44:06 +0000 | [diff] [blame] | 6255 | assert( pCtx->pOut->flags==MEM_Null ); |
| 6256 | assert( pCtx->isError==0 ); |
| 6257 | assert( pCtx->skipFlag==0 ); |
drh | 2d80151 | 2016-01-14 22:19:58 +0000 | [diff] [blame] | 6258 | (pCtx->pFunc->xSFunc)(pCtx,pCtx->argc,pCtx->argv); /* IMP: R-24505-23230 */ |
drh | f09ac0b | 2018-01-23 03:44:06 +0000 | [diff] [blame] | 6259 | if( pCtx->isError ){ |
| 6260 | if( pCtx->isError>0 ){ |
| 6261 | sqlite3VdbeError(p, "%s", sqlite3_value_text(pCtx->pOut)); |
drh | 9c7c913 | 2015-06-26 18:16:52 +0000 | [diff] [blame] | 6262 | rc = pCtx->isError; |
| 6263 | } |
drh | f09ac0b | 2018-01-23 03:44:06 +0000 | [diff] [blame] | 6264 | if( pCtx->skipFlag ){ |
| 6265 | assert( pOp[-1].opcode==OP_CollSeq ); |
| 6266 | i = pOp[-1].p1; |
| 6267 | if( i ) sqlite3VdbeMemSetInt64(&aMem[i], 1); |
| 6268 | pCtx->skipFlag = 0; |
| 6269 | } |
| 6270 | sqlite3VdbeMemRelease(pCtx->pOut); |
| 6271 | pCtx->pOut->flags = MEM_Null; |
| 6272 | pCtx->isError = 0; |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 6273 | if( rc ) goto abort_due_to_error; |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 6274 | } |
drh | f09ac0b | 2018-01-23 03:44:06 +0000 | [diff] [blame] | 6275 | assert( pCtx->pOut->flags==MEM_Null ); |
| 6276 | assert( pCtx->skipFlag==0 ); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 6277 | break; |
| 6278 | } |
| 6279 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 6280 | /* Opcode: AggFinal P1 P2 * P4 * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 6281 | ** Synopsis: accum=r[P1] N=P2 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 6282 | ** |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 6283 | ** Execute the finalizer function for an aggregate. P1 is |
| 6284 | ** the memory location that is the accumulator for the aggregate. |
drh | a10a34b | 2005-09-07 22:09:48 +0000 | [diff] [blame] | 6285 | ** |
| 6286 | ** P2 is the number of arguments that the step function takes and |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 6287 | ** P4 is a pointer to the FuncDef for this function. The P2 |
drh | a10a34b | 2005-09-07 22:09:48 +0000 | [diff] [blame] | 6288 | ** argument is not used by this opcode. It is only there to disambiguate |
| 6289 | ** functions that can take varying numbers of arguments. The |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 6290 | ** P4 argument is only needed for the degenerate case where |
drh | a10a34b | 2005-09-07 22:09:48 +0000 | [diff] [blame] | 6291 | ** the step function was not previously called. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 6292 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 6293 | case OP_AggFinal: { |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 6294 | Mem *pMem; |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 6295 | assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) ); |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 6296 | pMem = &aMem[pOp->p1]; |
drh | a10a34b | 2005-09-07 22:09:48 +0000 | [diff] [blame] | 6297 | assert( (pMem->flags & ~(MEM_Null|MEM_Agg))==0 ); |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 6298 | rc = sqlite3VdbeMemFinalize(pMem, pOp->p4.pFunc); |
drh | 4c8555f | 2009-06-25 01:47:11 +0000 | [diff] [blame] | 6299 | if( rc ){ |
drh | 22c17b8 | 2015-05-15 04:13:15 +0000 | [diff] [blame] | 6300 | sqlite3VdbeError(p, "%s", sqlite3_value_text(pMem)); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 6301 | goto abort_due_to_error; |
drh | 90669c1 | 2006-01-20 15:45:36 +0000 | [diff] [blame] | 6302 | } |
drh | 2dca868 | 2008-03-21 17:13:13 +0000 | [diff] [blame] | 6303 | sqlite3VdbeChangeEncoding(pMem, encoding); |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 6304 | UPDATE_MAX_BLOBSIZE(pMem); |
drh | 023ae03 | 2007-05-08 12:12:16 +0000 | [diff] [blame] | 6305 | if( sqlite3VdbeMemTooBig(pMem) ){ |
| 6306 | goto too_big; |
| 6307 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 6308 | break; |
| 6309 | } |
| 6310 | |
dan | 5cf5353 | 2010-05-01 16:40:20 +0000 | [diff] [blame] | 6311 | #ifndef SQLITE_OMIT_WAL |
dan | cdc1f04 | 2010-11-18 12:11:05 +0000 | [diff] [blame] | 6312 | /* Opcode: Checkpoint P1 P2 P3 * * |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 6313 | ** |
| 6314 | ** 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] | 6315 | ** WAL mode. Parameter P2 is one of SQLITE_CHECKPOINT_PASSIVE, FULL, |
| 6316 | ** 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] | 6317 | ** SQLITE_BUSY or not, respectively. Write the number of pages in the |
| 6318 | ** WAL after the checkpoint into mem[P3+1] and the number of pages |
| 6319 | ** in the WAL that have been checkpointed after the checkpoint |
| 6320 | ** completes into mem[P3+2]. However on an error, mem[P3+1] and |
| 6321 | ** mem[P3+2] are initialized to -1. |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 6322 | */ |
| 6323 | case OP_Checkpoint: { |
drh | 30aa3b9 | 2011-02-07 23:56:01 +0000 | [diff] [blame] | 6324 | int i; /* Loop counter */ |
| 6325 | int aRes[3]; /* Results */ |
| 6326 | Mem *pMem; /* Write results here */ |
| 6327 | |
drh | 9e92a47 | 2013-06-27 17:40:30 +0000 | [diff] [blame] | 6328 | assert( p->readOnly==0 ); |
drh | 30aa3b9 | 2011-02-07 23:56:01 +0000 | [diff] [blame] | 6329 | aRes[0] = 0; |
| 6330 | aRes[1] = aRes[2] = -1; |
dan | cdc1f04 | 2010-11-18 12:11:05 +0000 | [diff] [blame] | 6331 | assert( pOp->p2==SQLITE_CHECKPOINT_PASSIVE |
| 6332 | || pOp->p2==SQLITE_CHECKPOINT_FULL |
| 6333 | || pOp->p2==SQLITE_CHECKPOINT_RESTART |
dan | f26a154 | 2014-12-02 19:04:54 +0000 | [diff] [blame] | 6334 | || pOp->p2==SQLITE_CHECKPOINT_TRUNCATE |
dan | cdc1f04 | 2010-11-18 12:11:05 +0000 | [diff] [blame] | 6335 | ); |
drh | 30aa3b9 | 2011-02-07 23:56:01 +0000 | [diff] [blame] | 6336 | rc = sqlite3Checkpoint(db, pOp->p1, pOp->p2, &aRes[1], &aRes[2]); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 6337 | if( rc ){ |
| 6338 | if( rc!=SQLITE_BUSY ) goto abort_due_to_error; |
dan | cdc1f04 | 2010-11-18 12:11:05 +0000 | [diff] [blame] | 6339 | rc = SQLITE_OK; |
drh | 30aa3b9 | 2011-02-07 23:56:01 +0000 | [diff] [blame] | 6340 | aRes[0] = 1; |
dan | cdc1f04 | 2010-11-18 12:11:05 +0000 | [diff] [blame] | 6341 | } |
drh | 30aa3b9 | 2011-02-07 23:56:01 +0000 | [diff] [blame] | 6342 | for(i=0, pMem = &aMem[pOp->p3]; i<3; i++, pMem++){ |
| 6343 | sqlite3VdbeMemSetInt64(pMem, (i64)aRes[i]); |
| 6344 | } |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 6345 | break; |
| 6346 | }; |
dan | 5cf5353 | 2010-05-01 16:40:20 +0000 | [diff] [blame] | 6347 | #endif |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 6348 | |
drh | cac29a6 | 2010-07-02 19:36:52 +0000 | [diff] [blame] | 6349 | #ifndef SQLITE_OMIT_PRAGMA |
drh | 0fd6135 | 2014-02-07 02:29:45 +0000 | [diff] [blame] | 6350 | /* Opcode: JournalMode P1 P2 P3 * * |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 6351 | ** |
| 6352 | ** Change the journal mode of database P1 to P3. P3 must be one of the |
| 6353 | ** PAGER_JOURNALMODE_XXX values. If changing between the various rollback |
| 6354 | ** modes (delete, truncate, persist, off and memory), this is a simple |
| 6355 | ** operation. No IO is required. |
| 6356 | ** |
| 6357 | ** If changing into or out of WAL mode the procedure is more complicated. |
| 6358 | ** |
| 6359 | ** Write a string containing the final journal-mode to register P2. |
| 6360 | */ |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 6361 | case OP_JournalMode: { /* out2 */ |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 6362 | Btree *pBt; /* Btree to change journal mode of */ |
| 6363 | Pager *pPager; /* Pager associated with pBt */ |
drh | d80b233 | 2010-05-01 00:59:37 +0000 | [diff] [blame] | 6364 | int eNew; /* New journal mode */ |
| 6365 | int eOld; /* The old journal mode */ |
mistachkin | 59ee77c | 2012-09-13 15:26:44 +0000 | [diff] [blame] | 6366 | #ifndef SQLITE_OMIT_WAL |
drh | d80b233 | 2010-05-01 00:59:37 +0000 | [diff] [blame] | 6367 | const char *zFilename; /* Name of database file for pPager */ |
mistachkin | 59ee77c | 2012-09-13 15:26:44 +0000 | [diff] [blame] | 6368 | #endif |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 6369 | |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 6370 | pOut = out2Prerelease(p, pOp); |
drh | d80b233 | 2010-05-01 00:59:37 +0000 | [diff] [blame] | 6371 | eNew = pOp->p3; |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 6372 | assert( eNew==PAGER_JOURNALMODE_DELETE |
| 6373 | || eNew==PAGER_JOURNALMODE_TRUNCATE |
| 6374 | || eNew==PAGER_JOURNALMODE_PERSIST |
| 6375 | || eNew==PAGER_JOURNALMODE_OFF |
| 6376 | || eNew==PAGER_JOURNALMODE_MEMORY |
| 6377 | || eNew==PAGER_JOURNALMODE_WAL |
| 6378 | || eNew==PAGER_JOURNALMODE_QUERY |
| 6379 | ); |
| 6380 | assert( pOp->p1>=0 && pOp->p1<db->nDb ); |
drh | 9e92a47 | 2013-06-27 17:40:30 +0000 | [diff] [blame] | 6381 | assert( p->readOnly==0 ); |
drh | 3ebaee9 | 2010-05-06 21:37:22 +0000 | [diff] [blame] | 6382 | |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 6383 | pBt = db->aDb[pOp->p1].pBt; |
| 6384 | pPager = sqlite3BtreePager(pBt); |
drh | 0b9b430 | 2010-06-11 17:01:24 +0000 | [diff] [blame] | 6385 | eOld = sqlite3PagerGetJournalMode(pPager); |
| 6386 | if( eNew==PAGER_JOURNALMODE_QUERY ) eNew = eOld; |
| 6387 | if( !sqlite3PagerOkToChangeJournalMode(pPager) ) eNew = eOld; |
dan | 5cf5353 | 2010-05-01 16:40:20 +0000 | [diff] [blame] | 6388 | |
| 6389 | #ifndef SQLITE_OMIT_WAL |
drh | d4e0bb0 | 2012-05-27 01:19:04 +0000 | [diff] [blame] | 6390 | zFilename = sqlite3PagerFilename(pPager, 1); |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 6391 | |
drh | d80b233 | 2010-05-01 00:59:37 +0000 | [diff] [blame] | 6392 | /* Do not allow a transition to journal_mode=WAL for a database |
drh | 6e1f482 | 2010-07-13 23:41:40 +0000 | [diff] [blame] | 6393 | ** in temporary storage or if the VFS does not support shared memory |
drh | d80b233 | 2010-05-01 00:59:37 +0000 | [diff] [blame] | 6394 | */ |
| 6395 | if( eNew==PAGER_JOURNALMODE_WAL |
drh | 057fc81 | 2011-10-17 23:15:31 +0000 | [diff] [blame] | 6396 | && (sqlite3Strlen30(zFilename)==0 /* Temp file */ |
drh | 6e1f482 | 2010-07-13 23:41:40 +0000 | [diff] [blame] | 6397 | || !sqlite3PagerWalSupported(pPager)) /* No shared-memory support */ |
dan | e180c29 | 2010-04-26 17:42:56 +0000 | [diff] [blame] | 6398 | ){ |
drh | 0b9b430 | 2010-06-11 17:01:24 +0000 | [diff] [blame] | 6399 | eNew = eOld; |
dan | e180c29 | 2010-04-26 17:42:56 +0000 | [diff] [blame] | 6400 | } |
| 6401 | |
drh | 0b9b430 | 2010-06-11 17:01:24 +0000 | [diff] [blame] | 6402 | if( (eNew!=eOld) |
| 6403 | && (eOld==PAGER_JOURNALMODE_WAL || eNew==PAGER_JOURNALMODE_WAL) |
| 6404 | ){ |
dan | c0537fe | 2013-06-28 19:41:43 +0000 | [diff] [blame] | 6405 | if( !db->autoCommit || db->nVdbeRead>1 ){ |
drh | 0b9b430 | 2010-06-11 17:01:24 +0000 | [diff] [blame] | 6406 | rc = SQLITE_ERROR; |
drh | 22c17b8 | 2015-05-15 04:13:15 +0000 | [diff] [blame] | 6407 | sqlite3VdbeError(p, |
drh | 0b9b430 | 2010-06-11 17:01:24 +0000 | [diff] [blame] | 6408 | "cannot change %s wal mode from within a transaction", |
| 6409 | (eNew==PAGER_JOURNALMODE_WAL ? "into" : "out of") |
| 6410 | ); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 6411 | goto abort_due_to_error; |
drh | 0b9b430 | 2010-06-11 17:01:24 +0000 | [diff] [blame] | 6412 | }else{ |
| 6413 | |
| 6414 | if( eOld==PAGER_JOURNALMODE_WAL ){ |
| 6415 | /* If leaving WAL mode, close the log file. If successful, the call |
| 6416 | ** to PagerCloseWal() checkpoints and deletes the write-ahead-log |
| 6417 | ** file. An EXCLUSIVE lock may still be held on the database file |
| 6418 | ** after a successful return. |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 6419 | */ |
dan | 7fb8990 | 2016-08-12 16:21:15 +0000 | [diff] [blame] | 6420 | rc = sqlite3PagerCloseWal(pPager, db); |
drh | ab9b744 | 2010-05-10 11:20:05 +0000 | [diff] [blame] | 6421 | if( rc==SQLITE_OK ){ |
drh | 0b9b430 | 2010-06-11 17:01:24 +0000 | [diff] [blame] | 6422 | sqlite3PagerSetJournalMode(pPager, eNew); |
drh | 89c3f2f | 2010-05-15 01:09:38 +0000 | [diff] [blame] | 6423 | } |
drh | 242c4f7 | 2010-06-22 14:49:39 +0000 | [diff] [blame] | 6424 | }else if( eOld==PAGER_JOURNALMODE_MEMORY ){ |
| 6425 | /* Cannot transition directly from MEMORY to WAL. Use mode OFF |
| 6426 | ** as an intermediate */ |
| 6427 | sqlite3PagerSetJournalMode(pPager, PAGER_JOURNALMODE_OFF); |
drh | 0b9b430 | 2010-06-11 17:01:24 +0000 | [diff] [blame] | 6428 | } |
| 6429 | |
| 6430 | /* Open a transaction on the database file. Regardless of the journal |
| 6431 | ** mode, this transaction always uses a rollback journal. |
| 6432 | */ |
| 6433 | assert( sqlite3BtreeIsInTrans(pBt)==0 ); |
| 6434 | if( rc==SQLITE_OK ){ |
dan | 731bf5b | 2010-06-17 16:44:21 +0000 | [diff] [blame] | 6435 | rc = sqlite3BtreeSetVersion(pBt, (eNew==PAGER_JOURNALMODE_WAL ? 2 : 1)); |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 6436 | } |
| 6437 | } |
| 6438 | } |
dan | 5cf5353 | 2010-05-01 16:40:20 +0000 | [diff] [blame] | 6439 | #endif /* ifndef SQLITE_OMIT_WAL */ |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 6440 | |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 6441 | if( rc ) eNew = eOld; |
drh | 0b9b430 | 2010-06-11 17:01:24 +0000 | [diff] [blame] | 6442 | eNew = sqlite3PagerSetJournalMode(pPager, eNew); |
dan | 731bf5b | 2010-06-17 16:44:21 +0000 | [diff] [blame] | 6443 | |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 6444 | pOut->flags = MEM_Str|MEM_Static|MEM_Term; |
dan | b978002 | 2010-04-21 18:37:57 +0000 | [diff] [blame] | 6445 | pOut->z = (char *)sqlite3JournalModename(eNew); |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 6446 | pOut->n = sqlite3Strlen30(pOut->z); |
| 6447 | pOut->enc = SQLITE_UTF8; |
| 6448 | sqlite3VdbeChangeEncoding(pOut, encoding); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 6449 | if( rc ) goto abort_due_to_error; |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 6450 | break; |
drh | cac29a6 | 2010-07-02 19:36:52 +0000 | [diff] [blame] | 6451 | }; |
| 6452 | #endif /* SQLITE_OMIT_PRAGMA */ |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 6453 | |
drh | fdbcdee | 2007-03-27 14:44:50 +0000 | [diff] [blame] | 6454 | #if !defined(SQLITE_OMIT_VACUUM) && !defined(SQLITE_OMIT_ATTACH) |
drh | 9ef5e77 | 2016-08-19 14:20:56 +0000 | [diff] [blame] | 6455 | /* Opcode: Vacuum P1 * * * * |
drh | 6f8c91c | 2003-12-07 00:24:35 +0000 | [diff] [blame] | 6456 | ** |
drh | 9ef5e77 | 2016-08-19 14:20:56 +0000 | [diff] [blame] | 6457 | ** Vacuum the entire database P1. P1 is 0 for "main", and 2 or more |
| 6458 | ** for an attached database. The "temp" database may not be vacuumed. |
drh | 6f8c91c | 2003-12-07 00:24:35 +0000 | [diff] [blame] | 6459 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 6460 | case OP_Vacuum: { |
drh | 9e92a47 | 2013-06-27 17:40:30 +0000 | [diff] [blame] | 6461 | assert( p->readOnly==0 ); |
drh | 9ef5e77 | 2016-08-19 14:20:56 +0000 | [diff] [blame] | 6462 | rc = sqlite3RunVacuum(&p->zErrMsg, db, pOp->p1); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 6463 | if( rc ) goto abort_due_to_error; |
drh | 6f8c91c | 2003-12-07 00:24:35 +0000 | [diff] [blame] | 6464 | break; |
| 6465 | } |
drh | 154d4b2 | 2006-09-21 11:02:16 +0000 | [diff] [blame] | 6466 | #endif |
drh | 6f8c91c | 2003-12-07 00:24:35 +0000 | [diff] [blame] | 6467 | |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 6468 | #if !defined(SQLITE_OMIT_AUTOVACUUM) |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 6469 | /* Opcode: IncrVacuum P1 P2 * * * |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 6470 | ** |
| 6471 | ** Perform a single step of the incremental vacuum procedure on |
drh | ca5557f | 2007-05-04 18:30:40 +0000 | [diff] [blame] | 6472 | ** the P1 database. If the vacuum has finished, jump to instruction |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 6473 | ** P2. Otherwise, fall through to the next instruction. |
| 6474 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 6475 | case OP_IncrVacuum: { /* jump */ |
drh | ca5557f | 2007-05-04 18:30:40 +0000 | [diff] [blame] | 6476 | Btree *pBt; |
| 6477 | |
| 6478 | assert( pOp->p1>=0 && pOp->p1<db->nDb ); |
drh | a7ab6d8 | 2014-07-21 15:44:39 +0000 | [diff] [blame] | 6479 | assert( DbMaskTest(p->btreeMask, pOp->p1) ); |
drh | 9e92a47 | 2013-06-27 17:40:30 +0000 | [diff] [blame] | 6480 | assert( p->readOnly==0 ); |
drh | ca5557f | 2007-05-04 18:30:40 +0000 | [diff] [blame] | 6481 | pBt = db->aDb[pOp->p1].pBt; |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 6482 | rc = sqlite3BtreeIncrVacuum(pBt); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 6483 | VdbeBranchTaken(rc==SQLITE_DONE,2); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 6484 | if( rc ){ |
| 6485 | if( rc!=SQLITE_DONE ) goto abort_due_to_error; |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 6486 | rc = SQLITE_OK; |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 6487 | goto jump_to_p2; |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 6488 | } |
| 6489 | break; |
| 6490 | } |
| 6491 | #endif |
| 6492 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 6493 | /* Opcode: Expire P1 * * * * |
danielk1977 | a21c6b6 | 2005-01-24 10:25:59 +0000 | [diff] [blame] | 6494 | ** |
drh | 25df48d | 2014-07-22 14:58:12 +0000 | [diff] [blame] | 6495 | ** Cause precompiled statements to expire. When an expired statement |
| 6496 | ** is executed using sqlite3_step() it will either automatically |
| 6497 | ** reprepare itself (if it was originally created using sqlite3_prepare_v2()) |
| 6498 | ** or it will fail with SQLITE_SCHEMA. |
danielk1977 | a21c6b6 | 2005-01-24 10:25:59 +0000 | [diff] [blame] | 6499 | ** |
| 6500 | ** 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] | 6501 | ** then only the currently executing statement is expired. |
danielk1977 | a21c6b6 | 2005-01-24 10:25:59 +0000 | [diff] [blame] | 6502 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 6503 | case OP_Expire: { |
danielk1977 | a21c6b6 | 2005-01-24 10:25:59 +0000 | [diff] [blame] | 6504 | if( !pOp->p1 ){ |
| 6505 | sqlite3ExpirePreparedStatements(db); |
| 6506 | }else{ |
| 6507 | p->expired = 1; |
| 6508 | } |
| 6509 | break; |
| 6510 | } |
| 6511 | |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 6512 | #ifndef SQLITE_OMIT_SHARED_CACHE |
drh | 6a9ad3d | 2008-04-02 16:29:30 +0000 | [diff] [blame] | 6513 | /* Opcode: TableLock P1 P2 P3 P4 * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 6514 | ** Synopsis: iDb=P1 root=P2 write=P3 |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 6515 | ** |
| 6516 | ** Obtain a lock on a particular table. This instruction is only used when |
| 6517 | ** the shared-cache feature is enabled. |
| 6518 | ** |
danielk1977 | 96d48e9 | 2009-06-29 06:00:37 +0000 | [diff] [blame] | 6519 | ** P1 is the index of the database in sqlite3.aDb[] of the database |
drh | 6a9ad3d | 2008-04-02 16:29:30 +0000 | [diff] [blame] | 6520 | ** on which the lock is acquired. A readlock is obtained if P3==0 or |
| 6521 | ** a write lock if P3==1. |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 6522 | ** |
| 6523 | ** P2 contains the root-page of the table to lock. |
| 6524 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 6525 | ** 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] | 6526 | ** used to generate an error message if the lock cannot be obtained. |
| 6527 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 6528 | case OP_TableLock: { |
danielk1977 | e0d9e6f | 2009-07-03 16:25:06 +0000 | [diff] [blame] | 6529 | u8 isWriteLock = (u8)pOp->p3; |
drh | 169dd92 | 2017-06-26 13:57:49 +0000 | [diff] [blame] | 6530 | if( isWriteLock || 0==(db->flags&SQLITE_ReadUncommit) ){ |
danielk1977 | e0d9e6f | 2009-07-03 16:25:06 +0000 | [diff] [blame] | 6531 | int p1 = pOp->p1; |
| 6532 | assert( p1>=0 && p1<db->nDb ); |
drh | a7ab6d8 | 2014-07-21 15:44:39 +0000 | [diff] [blame] | 6533 | assert( DbMaskTest(p->btreeMask, p1) ); |
danielk1977 | e0d9e6f | 2009-07-03 16:25:06 +0000 | [diff] [blame] | 6534 | assert( isWriteLock==0 || isWriteLock==1 ); |
| 6535 | rc = sqlite3BtreeLockTable(db->aDb[p1].pBt, pOp->p2, isWriteLock); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 6536 | if( rc ){ |
| 6537 | if( (rc&0xFF)==SQLITE_LOCKED ){ |
| 6538 | const char *z = pOp->p4.z; |
| 6539 | sqlite3VdbeError(p, "database table is locked: %s", z); |
| 6540 | } |
| 6541 | goto abort_due_to_error; |
danielk1977 | e0d9e6f | 2009-07-03 16:25:06 +0000 | [diff] [blame] | 6542 | } |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 6543 | } |
| 6544 | break; |
| 6545 | } |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 6546 | #endif /* SQLITE_OMIT_SHARED_CACHE */ |
| 6547 | |
| 6548 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 6549 | /* Opcode: VBegin * * * P4 * |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 6550 | ** |
danielk1977 | 3e3a84d | 2008-08-01 17:37:40 +0000 | [diff] [blame] | 6551 | ** P4 may be a pointer to an sqlite3_vtab structure. If so, call the |
| 6552 | ** xBegin method for that table. |
| 6553 | ** |
| 6554 | ** 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] | 6555 | ** within a callback to a virtual table xSync() method. If it is, the error |
| 6556 | ** code will be set to SQLITE_LOCKED. |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 6557 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 6558 | case OP_VBegin: { |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 6559 | VTable *pVTab; |
| 6560 | pVTab = pOp->p4.pVtab; |
| 6561 | rc = sqlite3VtabBegin(db, pVTab); |
dan | 016f781 | 2013-08-21 17:35:48 +0000 | [diff] [blame] | 6562 | if( pVTab ) sqlite3VtabImportErrmsg(p, pVTab->pVtab); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 6563 | if( rc ) goto abort_due_to_error; |
danielk1977 | f9e7dda | 2006-06-16 16:08:53 +0000 | [diff] [blame] | 6564 | break; |
| 6565 | } |
| 6566 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 6567 | |
| 6568 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
dan | 7377945 | 2015-03-19 18:56:17 +0000 | [diff] [blame] | 6569 | /* Opcode: VCreate P1 P2 * * * |
danielk1977 | f9e7dda | 2006-06-16 16:08:53 +0000 | [diff] [blame] | 6570 | ** |
dan | 7377945 | 2015-03-19 18:56:17 +0000 | [diff] [blame] | 6571 | ** P2 is a register that holds the name of a virtual table in database |
| 6572 | ** P1. Call the xCreate method for that table. |
danielk1977 | f9e7dda | 2006-06-16 16:08:53 +0000 | [diff] [blame] | 6573 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 6574 | case OP_VCreate: { |
dan | 7377945 | 2015-03-19 18:56:17 +0000 | [diff] [blame] | 6575 | Mem sMem; /* For storing the record being decoded */ |
drh | 4746406 | 2015-03-21 12:22:16 +0000 | [diff] [blame] | 6576 | const char *zTab; /* Name of the virtual table */ |
| 6577 | |
dan | 7377945 | 2015-03-19 18:56:17 +0000 | [diff] [blame] | 6578 | memset(&sMem, 0, sizeof(sMem)); |
| 6579 | sMem.db = db; |
drh | 4746406 | 2015-03-21 12:22:16 +0000 | [diff] [blame] | 6580 | /* Because P2 is always a static string, it is impossible for the |
| 6581 | ** sqlite3VdbeMemCopy() to fail */ |
| 6582 | assert( (aMem[pOp->p2].flags & MEM_Str)!=0 ); |
| 6583 | assert( (aMem[pOp->p2].flags & MEM_Static)!=0 ); |
dan | 7377945 | 2015-03-19 18:56:17 +0000 | [diff] [blame] | 6584 | rc = sqlite3VdbeMemCopy(&sMem, &aMem[pOp->p2]); |
drh | 4746406 | 2015-03-21 12:22:16 +0000 | [diff] [blame] | 6585 | assert( rc==SQLITE_OK ); |
| 6586 | zTab = (const char*)sqlite3_value_text(&sMem); |
| 6587 | assert( zTab || db->mallocFailed ); |
| 6588 | if( zTab ){ |
| 6589 | rc = sqlite3VtabCallCreate(db, pOp->p1, zTab, &p->zErrMsg); |
dan | 7377945 | 2015-03-19 18:56:17 +0000 | [diff] [blame] | 6590 | } |
| 6591 | sqlite3VdbeMemRelease(&sMem); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 6592 | if( rc ) goto abort_due_to_error; |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 6593 | break; |
| 6594 | } |
| 6595 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 6596 | |
| 6597 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 6598 | /* Opcode: VDestroy P1 * * P4 * |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 6599 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 6600 | ** 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] | 6601 | ** of that table. |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 6602 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 6603 | case OP_VDestroy: { |
drh | 086723a | 2015-03-24 12:51:52 +0000 | [diff] [blame] | 6604 | db->nVDestroy++; |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 6605 | rc = sqlite3VtabCallDestroy(db, pOp->p1, pOp->p4.z); |
drh | 086723a | 2015-03-24 12:51:52 +0000 | [diff] [blame] | 6606 | db->nVDestroy--; |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 6607 | if( rc ) goto abort_due_to_error; |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 6608 | break; |
| 6609 | } |
| 6610 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 6611 | |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 6612 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 6613 | /* Opcode: VOpen P1 * * P4 * |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 6614 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 6615 | ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure. |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 6616 | ** P1 is a cursor number. This opcode opens a cursor to the virtual |
| 6617 | ** table and stores that cursor in P1. |
| 6618 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 6619 | case OP_VOpen: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 6620 | VdbeCursor *pCur; |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 6621 | sqlite3_vtab_cursor *pVCur; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 6622 | sqlite3_vtab *pVtab; |
drh | f496a7d | 2015-03-24 14:05:50 +0000 | [diff] [blame] | 6623 | const sqlite3_module *pModule; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 6624 | |
drh | 1713afb | 2013-06-28 01:24:57 +0000 | [diff] [blame] | 6625 | assert( p->bIsReader ); |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 6626 | pCur = 0; |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 6627 | pVCur = 0; |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 6628 | pVtab = pOp->p4.pVtab->pVtab; |
drh | f496a7d | 2015-03-24 14:05:50 +0000 | [diff] [blame] | 6629 | if( pVtab==0 || NEVER(pVtab->pModule==0) ){ |
| 6630 | rc = SQLITE_LOCKED; |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 6631 | goto abort_due_to_error; |
drh | f496a7d | 2015-03-24 14:05:50 +0000 | [diff] [blame] | 6632 | } |
| 6633 | pModule = pVtab->pModule; |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 6634 | rc = pModule->xOpen(pVtab, &pVCur); |
dan | 016f781 | 2013-08-21 17:35:48 +0000 | [diff] [blame] | 6635 | sqlite3VtabImportErrmsg(p, pVtab); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 6636 | if( rc ) goto abort_due_to_error; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 6637 | |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 6638 | /* Initialize sqlite3_vtab_cursor base class */ |
| 6639 | pVCur->pVtab = pVtab; |
| 6640 | |
| 6641 | /* Initialize vdbe cursor object */ |
| 6642 | pCur = allocateCursor(p, pOp->p1, 0, -1, CURTYPE_VTAB); |
| 6643 | if( pCur ){ |
| 6644 | pCur->uc.pVCur = pVCur; |
| 6645 | pVtab->nRef++; |
| 6646 | }else{ |
| 6647 | assert( db->mallocFailed ); |
| 6648 | pModule->xClose(pVCur); |
| 6649 | goto no_mem; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 6650 | } |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 6651 | break; |
| 6652 | } |
| 6653 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 6654 | |
| 6655 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
danielk1977 | 6dbee81 | 2008-01-03 18:39:41 +0000 | [diff] [blame] | 6656 | /* Opcode: VFilter P1 P2 P3 P4 * |
drh | 831116d | 2014-04-03 14:31:00 +0000 | [diff] [blame] | 6657 | ** Synopsis: iplan=r[P3] zplan='P4' |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 6658 | ** |
| 6659 | ** P1 is a cursor opened using VOpen. P2 is an address to jump to if |
| 6660 | ** the filtered result set is empty. |
| 6661 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 6662 | ** P4 is either NULL or a string that was generated by the xBestIndex |
| 6663 | ** method of the module. The interpretation of the P4 string is left |
drh | 4be8b51 | 2006-06-13 23:51:34 +0000 | [diff] [blame] | 6664 | ** to the module implementation. |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 6665 | ** |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 6666 | ** This opcode invokes the xFilter method on the virtual table specified |
danielk1977 | 6dbee81 | 2008-01-03 18:39:41 +0000 | [diff] [blame] | 6667 | ** by P1. The integer query plan parameter to xFilter is stored in register |
| 6668 | ** P3. Register P3+1 stores the argc parameter to be passed to the |
drh | 174edc6 | 2008-05-29 05:23:41 +0000 | [diff] [blame] | 6669 | ** xFilter method. Registers P3+2..P3+1+argc are the argc |
| 6670 | ** additional parameters which are passed to |
danielk1977 | 6dbee81 | 2008-01-03 18:39:41 +0000 | [diff] [blame] | 6671 | ** xFilter as argv. Register P3+2 becomes argv[0] when passed to xFilter. |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 6672 | ** |
danielk1977 | 6dbee81 | 2008-01-03 18:39:41 +0000 | [diff] [blame] | 6673 | ** 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] | 6674 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 6675 | case OP_VFilter: { /* jump */ |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 6676 | int nArg; |
danielk1977 | 6dbee81 | 2008-01-03 18:39:41 +0000 | [diff] [blame] | 6677 | int iQuery; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 6678 | const sqlite3_module *pModule; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 6679 | Mem *pQuery; |
| 6680 | Mem *pArgc; |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 6681 | sqlite3_vtab_cursor *pVCur; |
drh | 4dc754d | 2008-07-23 18:17:32 +0000 | [diff] [blame] | 6682 | sqlite3_vtab *pVtab; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 6683 | VdbeCursor *pCur; |
| 6684 | int res; |
| 6685 | int i; |
| 6686 | Mem **apArg; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 6687 | |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 6688 | pQuery = &aMem[pOp->p3]; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 6689 | pArgc = &pQuery[1]; |
| 6690 | pCur = p->apCsr[pOp->p1]; |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 6691 | assert( memIsValid(pQuery) ); |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 6692 | REGISTER_TRACE(pOp->p3, pQuery); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 6693 | assert( pCur->eCurType==CURTYPE_VTAB ); |
| 6694 | pVCur = pCur->uc.pVCur; |
| 6695 | pVtab = pVCur->pVtab; |
drh | 4dc754d | 2008-07-23 18:17:32 +0000 | [diff] [blame] | 6696 | pModule = pVtab->pModule; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 6697 | |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 6698 | /* Grab the index number and argc parameters */ |
danielk1977 | 6dbee81 | 2008-01-03 18:39:41 +0000 | [diff] [blame] | 6699 | assert( (pQuery->flags&MEM_Int)!=0 && pArgc->flags==MEM_Int ); |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 6700 | nArg = (int)pArgc->u.i; |
| 6701 | iQuery = (int)pQuery->u.i; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 6702 | |
drh | 644a529 | 2006-12-20 14:53:38 +0000 | [diff] [blame] | 6703 | /* Invoke the xFilter method */ |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 6704 | res = 0; |
| 6705 | apArg = p->apArg; |
| 6706 | for(i = 0; i<nArg; i++){ |
| 6707 | apArg[i] = &pArgc[i+1]; |
| 6708 | } |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 6709 | rc = pModule->xFilter(pVCur, iQuery, pOp->p4.z, nArg, apArg); |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 6710 | sqlite3VtabImportErrmsg(p, pVtab); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 6711 | if( rc ) goto abort_due_to_error; |
| 6712 | res = pModule->xEof(pVCur); |
drh | 1d454a3 | 2008-01-31 19:34:51 +0000 | [diff] [blame] | 6713 | pCur->nullRow = 0; |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 6714 | VdbeBranchTaken(res!=0,2); |
| 6715 | if( res ) goto jump_to_p2; |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 6716 | break; |
| 6717 | } |
| 6718 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 6719 | |
| 6720 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | ce2fbd1 | 2018-01-12 21:00:14 +0000 | [diff] [blame] | 6721 | /* Opcode: VColumn P1 P2 P3 * P5 |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 6722 | ** Synopsis: r[P3]=vcolumn(P2) |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 6723 | ** |
drh | 6f390be | 2018-01-11 17:04:26 +0000 | [diff] [blame] | 6724 | ** Store in register P3 the value of the P2-th column of |
| 6725 | ** the current row of the virtual-table of cursor P1. |
| 6726 | ** |
| 6727 | ** If the VColumn opcode is being used to fetch the value of |
drh | ce2fbd1 | 2018-01-12 21:00:14 +0000 | [diff] [blame] | 6728 | ** an unchanging column during an UPDATE operation, then the P5 |
| 6729 | ** value is 1. Otherwise, P5 is 0. The P5 value is returned |
drh | 6f390be | 2018-01-11 17:04:26 +0000 | [diff] [blame] | 6730 | ** by sqlite3_vtab_nochange() routine can can be used |
| 6731 | ** by virtual table implementations to return special "no-change" |
| 6732 | ** marks which can be more efficient, depending on the virtual table. |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 6733 | */ |
| 6734 | case OP_VColumn: { |
danielk1977 | 3e3a84d | 2008-08-01 17:37:40 +0000 | [diff] [blame] | 6735 | sqlite3_vtab *pVtab; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 6736 | const sqlite3_module *pModule; |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 6737 | Mem *pDest; |
| 6738 | sqlite3_context sContext; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 6739 | |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 6740 | VdbeCursor *pCur = p->apCsr[pOp->p1]; |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 6741 | assert( pCur->eCurType==CURTYPE_VTAB ); |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 6742 | assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) ); |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 6743 | pDest = &aMem[pOp->p3]; |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 6744 | memAboutToChange(p, pDest); |
drh | 2945b4a | 2008-01-31 15:53:45 +0000 | [diff] [blame] | 6745 | if( pCur->nullRow ){ |
| 6746 | sqlite3VdbeMemSetNull(pDest); |
| 6747 | break; |
| 6748 | } |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 6749 | pVtab = pCur->uc.pVCur->pVtab; |
danielk1977 | 3e3a84d | 2008-08-01 17:37:40 +0000 | [diff] [blame] | 6750 | pModule = pVtab->pModule; |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 6751 | assert( pModule->xColumn ); |
| 6752 | memset(&sContext, 0, sizeof(sContext)); |
drh | 9bd038f | 2014-08-27 14:14:06 +0000 | [diff] [blame] | 6753 | sContext.pOut = pDest; |
drh | ce2fbd1 | 2018-01-12 21:00:14 +0000 | [diff] [blame] | 6754 | if( pOp->p5 ){ |
| 6755 | sqlite3VdbeMemSetNull(pDest); |
| 6756 | pDest->flags = MEM_Null|MEM_Zero; |
| 6757 | pDest->u.nZero = 0; |
| 6758 | }else{ |
| 6759 | MemSetTypeFlag(pDest, MEM_Null); |
| 6760 | } |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 6761 | rc = pModule->xColumn(pCur->uc.pVCur, &sContext, pOp->p2); |
dan | 016f781 | 2013-08-21 17:35:48 +0000 | [diff] [blame] | 6762 | sqlite3VtabImportErrmsg(p, pVtab); |
drh | f09ac0b | 2018-01-23 03:44:06 +0000 | [diff] [blame] | 6763 | if( sContext.isError>0 ){ |
dan | 099fa84 | 2018-01-30 18:33:23 +0000 | [diff] [blame] | 6764 | sqlite3VdbeError(p, "%s", sqlite3_value_text(pDest)); |
drh | 4c8555f | 2009-06-25 01:47:11 +0000 | [diff] [blame] | 6765 | rc = sContext.isError; |
| 6766 | } |
drh | 9bd038f | 2014-08-27 14:14:06 +0000 | [diff] [blame] | 6767 | sqlite3VdbeChangeEncoding(pDest, encoding); |
drh | 5ff4437 | 2009-11-24 16:26:17 +0000 | [diff] [blame] | 6768 | REGISTER_TRACE(pOp->p3, pDest); |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 6769 | UPDATE_MAX_BLOBSIZE(pDest); |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 6770 | |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 6771 | if( sqlite3VdbeMemTooBig(pDest) ){ |
| 6772 | goto too_big; |
| 6773 | } |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 6774 | if( rc ) goto abort_due_to_error; |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 6775 | break; |
| 6776 | } |
| 6777 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 6778 | |
| 6779 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 6780 | /* Opcode: VNext P1 P2 * * * |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 6781 | ** |
| 6782 | ** Advance virtual table P1 to the next row in its result set and |
| 6783 | ** jump to instruction P2. Or, if the virtual table has reached |
| 6784 | ** the end of its result set, then fall through to the next instruction. |
| 6785 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 6786 | case OP_VNext: { /* jump */ |
danielk1977 | 3e3a84d | 2008-08-01 17:37:40 +0000 | [diff] [blame] | 6787 | sqlite3_vtab *pVtab; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 6788 | const sqlite3_module *pModule; |
drh | c54a617 | 2009-06-02 16:06:03 +0000 | [diff] [blame] | 6789 | int res; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 6790 | VdbeCursor *pCur; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 6791 | |
drh | c54a617 | 2009-06-02 16:06:03 +0000 | [diff] [blame] | 6792 | res = 0; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 6793 | pCur = p->apCsr[pOp->p1]; |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 6794 | assert( pCur->eCurType==CURTYPE_VTAB ); |
drh | 2945b4a | 2008-01-31 15:53:45 +0000 | [diff] [blame] | 6795 | if( pCur->nullRow ){ |
| 6796 | break; |
| 6797 | } |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 6798 | pVtab = pCur->uc.pVCur->pVtab; |
danielk1977 | 3e3a84d | 2008-08-01 17:37:40 +0000 | [diff] [blame] | 6799 | pModule = pVtab->pModule; |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 6800 | assert( pModule->xNext ); |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 6801 | |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 6802 | /* Invoke the xNext() method of the module. There is no way for the |
| 6803 | ** underlying implementation to return an error if one occurs during |
| 6804 | ** xNext(). Instead, if an error occurs, true is returned (indicating that |
| 6805 | ** data is available) and the error code returned when xColumn or |
| 6806 | ** some other method is next invoked on the save virtual table cursor. |
| 6807 | */ |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 6808 | rc = pModule->xNext(pCur->uc.pVCur); |
dan | 016f781 | 2013-08-21 17:35:48 +0000 | [diff] [blame] | 6809 | sqlite3VtabImportErrmsg(p, pVtab); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 6810 | if( rc ) goto abort_due_to_error; |
| 6811 | res = pModule->xEof(pCur->uc.pVCur); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 6812 | VdbeBranchTaken(!res,2); |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 6813 | if( !res ){ |
| 6814 | /* If there is data, jump to P2 */ |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 6815 | goto jump_to_p2_and_check_for_interrupt; |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 6816 | } |
drh | 49afe3a | 2013-07-10 03:05:14 +0000 | [diff] [blame] | 6817 | goto check_for_interrupt; |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 6818 | } |
| 6819 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 6820 | |
danielk1977 | 182c4ba | 2007-06-27 15:53:34 +0000 | [diff] [blame] | 6821 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 6822 | /* Opcode: VRename P1 * * P4 * |
danielk1977 | 182c4ba | 2007-06-27 15:53:34 +0000 | [diff] [blame] | 6823 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 6824 | ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure. |
danielk1977 | 182c4ba | 2007-06-27 15:53:34 +0000 | [diff] [blame] | 6825 | ** This opcode invokes the corresponding xRename method. The value |
danielk1977 | 6dbee81 | 2008-01-03 18:39:41 +0000 | [diff] [blame] | 6826 | ** in register P1 is passed as the zName argument to the xRename method. |
danielk1977 | 182c4ba | 2007-06-27 15:53:34 +0000 | [diff] [blame] | 6827 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 6828 | case OP_VRename: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 6829 | sqlite3_vtab *pVtab; |
| 6830 | Mem *pName; |
| 6831 | |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 6832 | pVtab = pOp->p4.pVtab->pVtab; |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 6833 | pName = &aMem[pOp->p1]; |
danielk1977 | 182c4ba | 2007-06-27 15:53:34 +0000 | [diff] [blame] | 6834 | assert( pVtab->pModule->xRename ); |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 6835 | assert( memIsValid(pName) ); |
drh | 9e92a47 | 2013-06-27 17:40:30 +0000 | [diff] [blame] | 6836 | assert( p->readOnly==0 ); |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 6837 | REGISTER_TRACE(pOp->p1, pName); |
drh | 35f6b93 | 2009-06-23 14:15:04 +0000 | [diff] [blame] | 6838 | assert( pName->flags & MEM_Str ); |
drh | 98655a6 | 2011-10-18 22:07:47 +0000 | [diff] [blame] | 6839 | testcase( pName->enc==SQLITE_UTF8 ); |
| 6840 | testcase( pName->enc==SQLITE_UTF16BE ); |
| 6841 | testcase( pName->enc==SQLITE_UTF16LE ); |
| 6842 | rc = sqlite3VdbeChangeEncoding(pName, SQLITE_UTF8); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 6843 | if( rc ) goto abort_due_to_error; |
| 6844 | rc = pVtab->pModule->xRename(pVtab, pName->z); |
| 6845 | sqlite3VtabImportErrmsg(p, pVtab); |
| 6846 | p->expired = 0; |
| 6847 | if( rc ) goto abort_due_to_error; |
danielk1977 | 182c4ba | 2007-06-27 15:53:34 +0000 | [diff] [blame] | 6848 | break; |
| 6849 | } |
| 6850 | #endif |
drh | 4cbdda9 | 2006-06-14 19:00:20 +0000 | [diff] [blame] | 6851 | |
| 6852 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | 0fd6135 | 2014-02-07 02:29:45 +0000 | [diff] [blame] | 6853 | /* Opcode: VUpdate P1 P2 P3 P4 P5 |
drh | f63552b | 2013-10-30 00:25:03 +0000 | [diff] [blame] | 6854 | ** Synopsis: data=r[P3@P2] |
danielk1977 | 399918f | 2006-06-14 13:03:23 +0000 | [diff] [blame] | 6855 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 6856 | ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure. |
danielk1977 | 399918f | 2006-06-14 13:03:23 +0000 | [diff] [blame] | 6857 | ** This opcode invokes the corresponding xUpdate method. P2 values |
danielk1977 | 2a339ff | 2008-01-03 17:31:44 +0000 | [diff] [blame] | 6858 | ** are contiguous memory cells starting at P3 to pass to the xUpdate |
| 6859 | ** invocation. The value in register (P3+P2-1) corresponds to the |
| 6860 | ** p2th element of the argv array passed to xUpdate. |
drh | 4cbdda9 | 2006-06-14 19:00:20 +0000 | [diff] [blame] | 6861 | ** |
| 6862 | ** The xUpdate method will do a DELETE or an INSERT or both. |
danielk1977 | 2a339ff | 2008-01-03 17:31:44 +0000 | [diff] [blame] | 6863 | ** The argv[0] element (which corresponds to memory cell P3) |
| 6864 | ** is the rowid of a row to delete. If argv[0] is NULL then no |
| 6865 | ** deletion occurs. The argv[1] element is the rowid of the new |
| 6866 | ** row. This can be NULL to have the virtual table select the new |
| 6867 | ** rowid for itself. The subsequent elements in the array are |
| 6868 | ** the values of columns in the new row. |
drh | 4cbdda9 | 2006-06-14 19:00:20 +0000 | [diff] [blame] | 6869 | ** |
| 6870 | ** If P2==1 then no insert is performed. argv[0] is the rowid of |
| 6871 | ** a row to delete. |
danielk1977 | 1f6eec5 | 2006-06-16 06:17:47 +0000 | [diff] [blame] | 6872 | ** |
| 6873 | ** P1 is a boolean flag. If it is set to true and the xUpdate call |
| 6874 | ** is successful, then the value returned by sqlite3_last_insert_rowid() |
| 6875 | ** is set to the value of the rowid for the row just inserted. |
drh | 0fd6135 | 2014-02-07 02:29:45 +0000 | [diff] [blame] | 6876 | ** |
| 6877 | ** P5 is the error actions (OE_Replace, OE_Fail, OE_Ignore, etc) to |
| 6878 | ** apply in the case of a constraint failure on an insert or update. |
danielk1977 | 399918f | 2006-06-14 13:03:23 +0000 | [diff] [blame] | 6879 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 6880 | case OP_VUpdate: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 6881 | sqlite3_vtab *pVtab; |
drh | f496a7d | 2015-03-24 14:05:50 +0000 | [diff] [blame] | 6882 | const sqlite3_module *pModule; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 6883 | int nArg; |
| 6884 | int i; |
| 6885 | sqlite_int64 rowid; |
| 6886 | Mem **apArg; |
| 6887 | Mem *pX; |
| 6888 | |
dan | b061d05 | 2011-04-25 18:49:57 +0000 | [diff] [blame] | 6889 | assert( pOp->p2==1 || pOp->p5==OE_Fail || pOp->p5==OE_Rollback |
| 6890 | || pOp->p5==OE_Abort || pOp->p5==OE_Ignore || pOp->p5==OE_Replace |
| 6891 | ); |
drh | 9e92a47 | 2013-06-27 17:40:30 +0000 | [diff] [blame] | 6892 | assert( p->readOnly==0 ); |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 6893 | pVtab = pOp->p4.pVtab->pVtab; |
drh | f496a7d | 2015-03-24 14:05:50 +0000 | [diff] [blame] | 6894 | if( pVtab==0 || NEVER(pVtab->pModule==0) ){ |
| 6895 | rc = SQLITE_LOCKED; |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 6896 | goto abort_due_to_error; |
drh | f496a7d | 2015-03-24 14:05:50 +0000 | [diff] [blame] | 6897 | } |
| 6898 | pModule = pVtab->pModule; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 6899 | nArg = pOp->p2; |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 6900 | assert( pOp->p4type==P4_VTAB ); |
drh | 35f6b93 | 2009-06-23 14:15:04 +0000 | [diff] [blame] | 6901 | if( ALWAYS(pModule->xUpdate) ){ |
dan | b061d05 | 2011-04-25 18:49:57 +0000 | [diff] [blame] | 6902 | u8 vtabOnConflict = db->vtabOnConflict; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 6903 | apArg = p->apArg; |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 6904 | pX = &aMem[pOp->p3]; |
danielk1977 | 2a339ff | 2008-01-03 17:31:44 +0000 | [diff] [blame] | 6905 | for(i=0; i<nArg; i++){ |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 6906 | assert( memIsValid(pX) ); |
| 6907 | memAboutToChange(p, pX); |
drh | 9c41938 | 2006-06-16 21:13:21 +0000 | [diff] [blame] | 6908 | apArg[i] = pX; |
danielk1977 | 2a339ff | 2008-01-03 17:31:44 +0000 | [diff] [blame] | 6909 | pX++; |
danielk1977 | 399918f | 2006-06-14 13:03:23 +0000 | [diff] [blame] | 6910 | } |
dan | b061d05 | 2011-04-25 18:49:57 +0000 | [diff] [blame] | 6911 | db->vtabOnConflict = pOp->p5; |
danielk1977 | 1f6eec5 | 2006-06-16 06:17:47 +0000 | [diff] [blame] | 6912 | rc = pModule->xUpdate(pVtab, nArg, apArg, &rowid); |
dan | b061d05 | 2011-04-25 18:49:57 +0000 | [diff] [blame] | 6913 | db->vtabOnConflict = vtabOnConflict; |
dan | 016f781 | 2013-08-21 17:35:48 +0000 | [diff] [blame] | 6914 | sqlite3VtabImportErrmsg(p, pVtab); |
drh | 35f6b93 | 2009-06-23 14:15:04 +0000 | [diff] [blame] | 6915 | if( rc==SQLITE_OK && pOp->p1 ){ |
danielk1977 | 1f6eec5 | 2006-06-16 06:17:47 +0000 | [diff] [blame] | 6916 | assert( nArg>1 && apArg[0] && (apArg[0]->flags&MEM_Null) ); |
drh | fae58d5 | 2017-01-26 17:26:44 +0000 | [diff] [blame] | 6917 | db->lastRowid = rowid; |
danielk1977 | 1f6eec5 | 2006-06-16 06:17:47 +0000 | [diff] [blame] | 6918 | } |
drh | d91c1a1 | 2013-02-09 13:58:25 +0000 | [diff] [blame] | 6919 | if( (rc&0xff)==SQLITE_CONSTRAINT && pOp->p4.pVtab->bConstraint ){ |
dan | b061d05 | 2011-04-25 18:49:57 +0000 | [diff] [blame] | 6920 | if( pOp->p5==OE_Ignore ){ |
| 6921 | rc = SQLITE_OK; |
| 6922 | }else{ |
| 6923 | p->errorAction = ((pOp->p5==OE_Replace) ? OE_Abort : pOp->p5); |
| 6924 | } |
| 6925 | }else{ |
| 6926 | p->nChange++; |
| 6927 | } |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 6928 | if( rc ) goto abort_due_to_error; |
danielk1977 | 399918f | 2006-06-14 13:03:23 +0000 | [diff] [blame] | 6929 | } |
drh | 4cbdda9 | 2006-06-14 19:00:20 +0000 | [diff] [blame] | 6930 | break; |
danielk1977 | 399918f | 2006-06-14 13:03:23 +0000 | [diff] [blame] | 6931 | } |
| 6932 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 6933 | |
danielk1977 | 59a9379 | 2008-05-15 17:48:20 +0000 | [diff] [blame] | 6934 | #ifndef SQLITE_OMIT_PAGER_PRAGMAS |
| 6935 | /* Opcode: Pagecount P1 P2 * * * |
| 6936 | ** |
| 6937 | ** Write the current number of pages in database P1 to memory cell P2. |
| 6938 | */ |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 6939 | case OP_Pagecount: { /* out2 */ |
| 6940 | pOut = out2Prerelease(p, pOp); |
drh | b129915 | 2010-03-30 22:58:33 +0000 | [diff] [blame] | 6941 | pOut->u.i = sqlite3BtreeLastPage(db->aDb[pOp->p1].pBt); |
danielk1977 | 59a9379 | 2008-05-15 17:48:20 +0000 | [diff] [blame] | 6942 | break; |
| 6943 | } |
| 6944 | #endif |
| 6945 | |
drh | 60ac3f4 | 2010-11-23 18:59:27 +0000 | [diff] [blame] | 6946 | |
| 6947 | #ifndef SQLITE_OMIT_PAGER_PRAGMAS |
| 6948 | /* Opcode: MaxPgcnt P1 P2 P3 * * |
| 6949 | ** |
| 6950 | ** 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] | 6951 | ** Do not let the maximum page count fall below the current page count and |
| 6952 | ** do not change the maximum page count value if P3==0. |
| 6953 | ** |
drh | 60ac3f4 | 2010-11-23 18:59:27 +0000 | [diff] [blame] | 6954 | ** Store the maximum page count after the change in register P2. |
| 6955 | */ |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 6956 | case OP_MaxPgcnt: { /* out2 */ |
drh | c84e033 | 2010-11-23 20:25:08 +0000 | [diff] [blame] | 6957 | unsigned int newMax; |
drh | 60ac3f4 | 2010-11-23 18:59:27 +0000 | [diff] [blame] | 6958 | Btree *pBt; |
| 6959 | |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 6960 | pOut = out2Prerelease(p, pOp); |
drh | 60ac3f4 | 2010-11-23 18:59:27 +0000 | [diff] [blame] | 6961 | pBt = db->aDb[pOp->p1].pBt; |
drh | c84e033 | 2010-11-23 20:25:08 +0000 | [diff] [blame] | 6962 | newMax = 0; |
| 6963 | if( pOp->p3 ){ |
| 6964 | newMax = sqlite3BtreeLastPage(pBt); |
drh | 6ea28d6 | 2010-11-26 16:49:59 +0000 | [diff] [blame] | 6965 | if( newMax < (unsigned)pOp->p3 ) newMax = (unsigned)pOp->p3; |
drh | c84e033 | 2010-11-23 20:25:08 +0000 | [diff] [blame] | 6966 | } |
| 6967 | pOut->u.i = sqlite3BtreeMaxPageCount(pBt, newMax); |
drh | 60ac3f4 | 2010-11-23 18:59:27 +0000 | [diff] [blame] | 6968 | break; |
| 6969 | } |
| 6970 | #endif |
| 6971 | |
drh | 3e34eab | 2017-07-19 19:48:40 +0000 | [diff] [blame] | 6972 | /* Opcode: Function0 P1 P2 P3 P4 P5 |
| 6973 | ** Synopsis: r[P3]=func(r[P2@P5]) |
| 6974 | ** |
| 6975 | ** Invoke a user function (P4 is a pointer to a FuncDef object that |
| 6976 | ** defines the function) with P5 arguments taken from register P2 and |
| 6977 | ** successors. The result of the function is stored in register P3. |
| 6978 | ** Register P3 must not be one of the function inputs. |
| 6979 | ** |
| 6980 | ** P1 is a 32-bit bitmask indicating whether or not each argument to the |
| 6981 | ** function was determined to be constant at compile time. If the first |
| 6982 | ** argument was constant then bit 0 of P1 is set. This is used to determine |
| 6983 | ** whether meta data associated with a user function argument using the |
| 6984 | ** sqlite3_set_auxdata() API may be safely retained until the next |
| 6985 | ** invocation of this opcode. |
| 6986 | ** |
| 6987 | ** See also: Function, AggStep, AggFinal |
| 6988 | */ |
| 6989 | /* Opcode: Function P1 P2 P3 P4 P5 |
| 6990 | ** Synopsis: r[P3]=func(r[P2@P5]) |
| 6991 | ** |
| 6992 | ** Invoke a user function (P4 is a pointer to an sqlite3_context object that |
| 6993 | ** contains a pointer to the function to be run) with P5 arguments taken |
| 6994 | ** from register P2 and successors. The result of the function is stored |
| 6995 | ** in register P3. Register P3 must not be one of the function inputs. |
| 6996 | ** |
| 6997 | ** P1 is a 32-bit bitmask indicating whether or not each argument to the |
| 6998 | ** function was determined to be constant at compile time. If the first |
| 6999 | ** argument was constant then bit 0 of P1 is set. This is used to determine |
| 7000 | ** whether meta data associated with a user function argument using the |
| 7001 | ** sqlite3_set_auxdata() API may be safely retained until the next |
| 7002 | ** invocation of this opcode. |
| 7003 | ** |
| 7004 | ** SQL functions are initially coded as OP_Function0 with P4 pointing |
| 7005 | ** to a FuncDef object. But on first evaluation, the P4 operand is |
| 7006 | ** automatically converted into an sqlite3_context object and the operation |
| 7007 | ** changed to this OP_Function opcode. In this way, the initialization of |
| 7008 | ** the sqlite3_context object occurs only once, rather than once for each |
| 7009 | ** evaluation of the function. |
| 7010 | ** |
| 7011 | ** See also: Function0, AggStep, AggFinal |
| 7012 | */ |
| 7013 | case OP_PureFunc0: |
| 7014 | case OP_Function0: { |
| 7015 | int n; |
| 7016 | sqlite3_context *pCtx; |
| 7017 | |
| 7018 | assert( pOp->p4type==P4_FUNCDEF ); |
| 7019 | n = pOp->p5; |
| 7020 | assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) ); |
| 7021 | assert( n==0 || (pOp->p2>0 && pOp->p2+n<=(p->nMem+1 - p->nCursor)+1) ); |
| 7022 | assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n ); |
| 7023 | pCtx = sqlite3DbMallocRawNN(db, sizeof(*pCtx) + (n-1)*sizeof(sqlite3_value*)); |
| 7024 | if( pCtx==0 ) goto no_mem; |
| 7025 | pCtx->pOut = 0; |
| 7026 | pCtx->pFunc = pOp->p4.pFunc; |
| 7027 | pCtx->iOp = (int)(pOp - aOp); |
| 7028 | pCtx->pVdbe = p; |
drh | f09ac0b | 2018-01-23 03:44:06 +0000 | [diff] [blame] | 7029 | pCtx->isError = 0; |
drh | 3e34eab | 2017-07-19 19:48:40 +0000 | [diff] [blame] | 7030 | pCtx->argc = n; |
| 7031 | pOp->p4type = P4_FUNCCTX; |
| 7032 | pOp->p4.pCtx = pCtx; |
| 7033 | assert( OP_PureFunc == OP_PureFunc0+2 ); |
| 7034 | assert( OP_Function == OP_Function0+2 ); |
| 7035 | pOp->opcode += 2; |
| 7036 | /* Fall through into OP_Function */ |
| 7037 | } |
| 7038 | case OP_PureFunc: |
| 7039 | case OP_Function: { |
| 7040 | int i; |
| 7041 | sqlite3_context *pCtx; |
| 7042 | |
| 7043 | assert( pOp->p4type==P4_FUNCCTX ); |
| 7044 | pCtx = pOp->p4.pCtx; |
| 7045 | |
| 7046 | /* If this function is inside of a trigger, the register array in aMem[] |
| 7047 | ** might change from one evaluation to the next. The next block of code |
| 7048 | ** checks to see if the register array has changed, and if so it |
| 7049 | ** reinitializes the relavant parts of the sqlite3_context object */ |
| 7050 | pOut = &aMem[pOp->p3]; |
| 7051 | if( pCtx->pOut != pOut ){ |
| 7052 | pCtx->pOut = pOut; |
| 7053 | for(i=pCtx->argc-1; i>=0; i--) pCtx->argv[i] = &aMem[pOp->p2+i]; |
| 7054 | } |
| 7055 | |
| 7056 | memAboutToChange(p, pOut); |
| 7057 | #ifdef SQLITE_DEBUG |
| 7058 | for(i=0; i<pCtx->argc; i++){ |
| 7059 | assert( memIsValid(pCtx->argv[i]) ); |
| 7060 | REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]); |
| 7061 | } |
| 7062 | #endif |
| 7063 | MemSetTypeFlag(pOut, MEM_Null); |
drh | f09ac0b | 2018-01-23 03:44:06 +0000 | [diff] [blame] | 7064 | assert( pCtx->isError==0 ); |
drh | 3e34eab | 2017-07-19 19:48:40 +0000 | [diff] [blame] | 7065 | (*pCtx->pFunc->xSFunc)(pCtx, pCtx->argc, pCtx->argv);/* IMP: R-24505-23230 */ |
| 7066 | |
| 7067 | /* If the function returned an error, throw an exception */ |
drh | f09ac0b | 2018-01-23 03:44:06 +0000 | [diff] [blame] | 7068 | if( pCtx->isError ){ |
| 7069 | if( pCtx->isError>0 ){ |
drh | 3e34eab | 2017-07-19 19:48:40 +0000 | [diff] [blame] | 7070 | sqlite3VdbeError(p, "%s", sqlite3_value_text(pOut)); |
| 7071 | rc = pCtx->isError; |
| 7072 | } |
| 7073 | sqlite3VdbeDeleteAuxData(db, &p->pAuxData, pCtx->iOp, pOp->p1); |
drh | f09ac0b | 2018-01-23 03:44:06 +0000 | [diff] [blame] | 7074 | pCtx->isError = 0; |
drh | 3e34eab | 2017-07-19 19:48:40 +0000 | [diff] [blame] | 7075 | if( rc ) goto abort_due_to_error; |
| 7076 | } |
| 7077 | |
| 7078 | /* Copy the result of the function into register P3 */ |
| 7079 | if( pOut->flags & (MEM_Str|MEM_Blob) ){ |
| 7080 | sqlite3VdbeChangeEncoding(pOut, encoding); |
| 7081 | if( sqlite3VdbeMemTooBig(pOut) ) goto too_big; |
| 7082 | } |
| 7083 | |
| 7084 | REGISTER_TRACE(pOp->p3, pOut); |
| 7085 | UPDATE_MAX_BLOBSIZE(pOut); |
| 7086 | break; |
| 7087 | } |
| 7088 | |
drh | f259df5 | 2017-12-27 20:38:35 +0000 | [diff] [blame] | 7089 | /* Opcode: Trace P1 P2 * P4 * |
| 7090 | ** |
| 7091 | ** Write P4 on the statement trace output if statement tracing is |
| 7092 | ** enabled. |
| 7093 | ** |
| 7094 | ** Operand P1 must be 0x7fffffff and P2 must positive. |
| 7095 | */ |
drh | 74588ce | 2017-09-13 00:13:05 +0000 | [diff] [blame] | 7096 | /* Opcode: Init P1 P2 P3 P4 * |
drh | 72e26de | 2016-08-24 21:24:04 +0000 | [diff] [blame] | 7097 | ** Synopsis: Start at P2 |
drh | aceb31b | 2014-02-08 01:40:27 +0000 | [diff] [blame] | 7098 | ** |
| 7099 | ** Programs contain a single instance of this opcode as the very first |
| 7100 | ** opcode. |
drh | 949f9cd | 2008-01-12 21:35:57 +0000 | [diff] [blame] | 7101 | ** |
| 7102 | ** If tracing is enabled (by the sqlite3_trace()) interface, then |
| 7103 | ** the UTF-8 string contained in P4 is emitted on the trace callback. |
drh | aceb31b | 2014-02-08 01:40:27 +0000 | [diff] [blame] | 7104 | ** Or if P4 is blank, use the string returned by sqlite3_sql(). |
| 7105 | ** |
| 7106 | ** If P2 is not zero, jump to instruction P2. |
drh | 9e5eb9c | 2016-09-18 16:08:10 +0000 | [diff] [blame] | 7107 | ** |
| 7108 | ** Increment the value of P1 so that OP_Once opcodes will jump the |
| 7109 | ** first time they are evaluated for this run. |
drh | 74588ce | 2017-09-13 00:13:05 +0000 | [diff] [blame] | 7110 | ** |
| 7111 | ** If P3 is not zero, then it is an address to jump to if an SQLITE_CORRUPT |
| 7112 | ** error is encountered. |
drh | 949f9cd | 2008-01-12 21:35:57 +0000 | [diff] [blame] | 7113 | */ |
drh | f259df5 | 2017-12-27 20:38:35 +0000 | [diff] [blame] | 7114 | case OP_Trace: |
drh | aceb31b | 2014-02-08 01:40:27 +0000 | [diff] [blame] | 7115 | case OP_Init: { /* jump */ |
drh | 9e5eb9c | 2016-09-18 16:08:10 +0000 | [diff] [blame] | 7116 | int i; |
drh | b9f4799 | 2018-01-24 12:14:43 +0000 | [diff] [blame] | 7117 | #ifndef SQLITE_OMIT_TRACE |
| 7118 | char *zTrace; |
| 7119 | #endif |
drh | 5fe63bf | 2016-07-25 02:42:22 +0000 | [diff] [blame] | 7120 | |
| 7121 | /* If the P4 argument is not NULL, then it must be an SQL comment string. |
| 7122 | ** The "--" string is broken up to prevent false-positives with srcck1.c. |
| 7123 | ** |
| 7124 | ** This assert() provides evidence for: |
| 7125 | ** EVIDENCE-OF: R-50676-09860 The callback can compute the same text that |
| 7126 | ** would have been returned by the legacy sqlite3_trace() interface by |
| 7127 | ** using the X argument when X begins with "--" and invoking |
| 7128 | ** sqlite3_expanded_sql(P) otherwise. |
| 7129 | */ |
| 7130 | assert( pOp->p4.z==0 || strncmp(pOp->p4.z, "-" "- ", 3)==0 ); |
drh | f259df5 | 2017-12-27 20:38:35 +0000 | [diff] [blame] | 7131 | |
| 7132 | /* OP_Init is always instruction 0 */ |
| 7133 | assert( pOp==p->aOp || pOp->opcode==OP_Trace ); |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 7134 | |
drh | aceb31b | 2014-02-08 01:40:27 +0000 | [diff] [blame] | 7135 | #ifndef SQLITE_OMIT_TRACE |
drh | fca760c | 2016-07-14 01:09:08 +0000 | [diff] [blame] | 7136 | if( (db->mTrace & (SQLITE_TRACE_STMT|SQLITE_TRACE_LEGACY))!=0 |
drh | 37f58e9 | 2012-09-04 21:34:26 +0000 | [diff] [blame] | 7137 | && !p->doingRerun |
| 7138 | && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0 |
| 7139 | ){ |
drh | 3d2a529 | 2016-07-13 22:55:01 +0000 | [diff] [blame] | 7140 | #ifndef SQLITE_OMIT_DEPRECATED |
drh | fca760c | 2016-07-14 01:09:08 +0000 | [diff] [blame] | 7141 | if( db->mTrace & SQLITE_TRACE_LEGACY ){ |
| 7142 | void (*x)(void*,const char*) = (void(*)(void*,const char*))db->xTrace; |
drh | 5fe63bf | 2016-07-25 02:42:22 +0000 | [diff] [blame] | 7143 | char *z = sqlite3VdbeExpandSql(p, zTrace); |
drh | fca760c | 2016-07-14 01:09:08 +0000 | [diff] [blame] | 7144 | x(db->pTraceArg, z); |
drh | bd441f7 | 2016-07-25 02:31:48 +0000 | [diff] [blame] | 7145 | sqlite3_free(z); |
drh | fca760c | 2016-07-14 01:09:08 +0000 | [diff] [blame] | 7146 | }else |
drh | 3d2a529 | 2016-07-13 22:55:01 +0000 | [diff] [blame] | 7147 | #endif |
drh | 7adbcff | 2017-03-20 15:29:28 +0000 | [diff] [blame] | 7148 | if( db->nVdbeExec>1 ){ |
| 7149 | char *z = sqlite3MPrintf(db, "-- %s", zTrace); |
| 7150 | (void)db->xTrace(SQLITE_TRACE_STMT, db->pTraceArg, p, z); |
| 7151 | sqlite3DbFree(db, z); |
| 7152 | }else{ |
drh | bd441f7 | 2016-07-25 02:31:48 +0000 | [diff] [blame] | 7153 | (void)db->xTrace(SQLITE_TRACE_STMT, db->pTraceArg, p, zTrace); |
drh | 3d2a529 | 2016-07-13 22:55:01 +0000 | [diff] [blame] | 7154 | } |
drh | 949f9cd | 2008-01-12 21:35:57 +0000 | [diff] [blame] | 7155 | } |
drh | 8f8b231 | 2013-10-18 20:03:43 +0000 | [diff] [blame] | 7156 | #ifdef SQLITE_USE_FCNTL_TRACE |
| 7157 | zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql); |
| 7158 | if( zTrace ){ |
mistachkin | d8992ce | 2016-09-20 17:49:01 +0000 | [diff] [blame] | 7159 | int j; |
| 7160 | for(j=0; j<db->nDb; j++){ |
| 7161 | if( DbMaskTest(p->btreeMask, j)==0 ) continue; |
| 7162 | sqlite3_file_control(db, db->aDb[j].zDbSName, SQLITE_FCNTL_TRACE, zTrace); |
drh | 8f8b231 | 2013-10-18 20:03:43 +0000 | [diff] [blame] | 7163 | } |
| 7164 | } |
| 7165 | #endif /* SQLITE_USE_FCNTL_TRACE */ |
drh | c3f1d5f | 2011-05-30 23:42:16 +0000 | [diff] [blame] | 7166 | #ifdef SQLITE_DEBUG |
| 7167 | if( (db->flags & SQLITE_SqlTrace)!=0 |
| 7168 | && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0 |
| 7169 | ){ |
| 7170 | sqlite3DebugPrintf("SQL-trace: %s\n", zTrace); |
| 7171 | } |
| 7172 | #endif /* SQLITE_DEBUG */ |
drh | aceb31b | 2014-02-08 01:40:27 +0000 | [diff] [blame] | 7173 | #endif /* SQLITE_OMIT_TRACE */ |
drh | 4910a76 | 2016-09-03 01:46:15 +0000 | [diff] [blame] | 7174 | assert( pOp->p2>0 ); |
drh | 9e5eb9c | 2016-09-18 16:08:10 +0000 | [diff] [blame] | 7175 | if( pOp->p1>=sqlite3GlobalConfig.iOnceResetThreshold ){ |
drh | f259df5 | 2017-12-27 20:38:35 +0000 | [diff] [blame] | 7176 | if( pOp->opcode==OP_Trace ) break; |
drh | 9e5eb9c | 2016-09-18 16:08:10 +0000 | [diff] [blame] | 7177 | for(i=1; i<p->nOp; i++){ |
| 7178 | if( p->aOp[i].opcode==OP_Once ) p->aOp[i].p1 = 0; |
| 7179 | } |
| 7180 | pOp->p1 = 0; |
| 7181 | } |
| 7182 | pOp->p1++; |
drh | 00d11d4 | 2017-06-29 12:49:18 +0000 | [diff] [blame] | 7183 | p->aCounter[SQLITE_STMTSTATUS_RUN]++; |
drh | 4910a76 | 2016-09-03 01:46:15 +0000 | [diff] [blame] | 7184 | goto jump_to_p2; |
drh | 949f9cd | 2008-01-12 21:35:57 +0000 | [diff] [blame] | 7185 | } |
drh | 949f9cd | 2008-01-12 21:35:57 +0000 | [diff] [blame] | 7186 | |
drh | 2893536 | 2013-12-07 20:39:19 +0000 | [diff] [blame] | 7187 | #ifdef SQLITE_ENABLE_CURSOR_HINTS |
drh | 0df5701 | 2015-08-14 15:05:55 +0000 | [diff] [blame] | 7188 | /* Opcode: CursorHint P1 * * P4 * |
drh | 2893536 | 2013-12-07 20:39:19 +0000 | [diff] [blame] | 7189 | ** |
| 7190 | ** 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] | 7191 | ** satisfy the Expr in P4. TK_REGISTER terms in the P4 expression refer |
| 7192 | ** to values currently held in registers. TK_COLUMN terms in the P4 |
| 7193 | ** 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] | 7194 | */ |
| 7195 | case OP_CursorHint: { |
| 7196 | VdbeCursor *pC; |
| 7197 | |
| 7198 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 7199 | assert( pOp->p4type==P4_EXPR ); |
| 7200 | pC = p->apCsr[pOp->p1]; |
dan | 91d3a61 | 2014-07-15 11:59:44 +0000 | [diff] [blame] | 7201 | if( pC ){ |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 7202 | assert( pC->eCurType==CURTYPE_BTREE ); |
drh | 62aaa6c | 2015-11-21 17:27:42 +0000 | [diff] [blame] | 7203 | sqlite3BtreeCursorHint(pC->uc.pCursor, BTREE_HINT_RANGE, |
| 7204 | pOp->p4.pExpr, aMem); |
dan | 91d3a61 | 2014-07-15 11:59:44 +0000 | [diff] [blame] | 7205 | } |
drh | 2893536 | 2013-12-07 20:39:19 +0000 | [diff] [blame] | 7206 | break; |
| 7207 | } |
| 7208 | #endif /* SQLITE_ENABLE_CURSOR_HINTS */ |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 7209 | |
| 7210 | /* Opcode: Noop * * * * * |
| 7211 | ** |
| 7212 | ** Do nothing. This instruction is often useful as a jump |
| 7213 | ** destination. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 7214 | */ |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 7215 | /* |
| 7216 | ** The magic Explain opcode are only inserted when explain==2 (which |
| 7217 | ** is to say when the EXPLAIN QUERY PLAN syntax is used.) |
| 7218 | ** This opcode records information from the optimizer. It is the |
| 7219 | ** the same as a no-op. This opcodesnever appears in a real VM program. |
| 7220 | */ |
| 7221 | default: { /* This is really OP_Noop and OP_Explain */ |
drh | 13573c7 | 2010-01-12 17:04:07 +0000 | [diff] [blame] | 7222 | assert( pOp->opcode==OP_Noop || pOp->opcode==OP_Explain ); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 7223 | break; |
| 7224 | } |
| 7225 | |
| 7226 | /***************************************************************************** |
| 7227 | ** The cases of the switch statement above this line should all be indented |
| 7228 | ** by 6 spaces. But the left-most 6 spaces have been removed to improve the |
| 7229 | ** readability. From this point on down, the normal indentation rules are |
| 7230 | ** restored. |
| 7231 | *****************************************************************************/ |
| 7232 | } |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 7233 | |
drh | 7b39686 | 2003-01-01 23:06:20 +0000 | [diff] [blame] | 7234 | #ifdef VDBE_PROFILE |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 7235 | { |
drh | a01c7c7 | 2014-04-25 12:35:31 +0000 | [diff] [blame] | 7236 | u64 endTime = sqlite3Hwtime(); |
drh | 6dc4148 | 2015-04-16 17:31:02 +0000 | [diff] [blame] | 7237 | if( endTime>start ) pOrigOp->cycles += endTime - start; |
| 7238 | pOrigOp->cnt++; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 7239 | } |
drh | 7b39686 | 2003-01-01 23:06:20 +0000 | [diff] [blame] | 7240 | #endif |
| 7241 | |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 7242 | /* The following code adds nothing to the actual functionality |
| 7243 | ** of the program. It is only here for testing and debugging. |
| 7244 | ** On the other hand, it does burn CPU cycles every time through |
| 7245 | ** the evaluator loop. So we can leave it out when NDEBUG is defined. |
| 7246 | */ |
| 7247 | #ifndef NDEBUG |
drh | 6dc4148 | 2015-04-16 17:31:02 +0000 | [diff] [blame] | 7248 | assert( pOp>=&aOp[-1] && pOp<&aOp[p->nOp-1] ); |
drh | ae7e151 | 2007-05-02 16:51:59 +0000 | [diff] [blame] | 7249 | |
drh | cf1023c | 2007-05-08 20:59:49 +0000 | [diff] [blame] | 7250 | #ifdef SQLITE_DEBUG |
drh | 84e55a8 | 2013-11-13 17:58:23 +0000 | [diff] [blame] | 7251 | if( db->flags & SQLITE_VdbeTrace ){ |
drh | 7cc84c2 | 2016-04-11 13:36:42 +0000 | [diff] [blame] | 7252 | u8 opProperty = sqlite3OpcodeProperty[pOrigOp->opcode]; |
drh | 84e55a8 | 2013-11-13 17:58:23 +0000 | [diff] [blame] | 7253 | if( rc!=0 ) printf("rc=%d\n",rc); |
drh | 7cc84c2 | 2016-04-11 13:36:42 +0000 | [diff] [blame] | 7254 | if( opProperty & (OPFLG_OUT2) ){ |
drh | 6dc4148 | 2015-04-16 17:31:02 +0000 | [diff] [blame] | 7255 | registerTrace(pOrigOp->p2, &aMem[pOrigOp->p2]); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 7256 | } |
drh | 7cc84c2 | 2016-04-11 13:36:42 +0000 | [diff] [blame] | 7257 | if( opProperty & OPFLG_OUT3 ){ |
drh | 6dc4148 | 2015-04-16 17:31:02 +0000 | [diff] [blame] | 7258 | registerTrace(pOrigOp->p3, &aMem[pOrigOp->p3]); |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 7259 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 7260 | } |
danielk1977 | b5402fb | 2005-01-12 07:15:04 +0000 | [diff] [blame] | 7261 | #endif /* SQLITE_DEBUG */ |
| 7262 | #endif /* NDEBUG */ |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 7263 | } /* The end of the for(;;) loop the loops through opcodes */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 7264 | |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 7265 | /* If we reach this point, it means that execution is finished with |
| 7266 | ** an error of some kind. |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 7267 | */ |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 7268 | abort_due_to_error: |
| 7269 | if( db->mallocFailed ) rc = SQLITE_NOMEM_BKPT; |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 7270 | assert( rc ); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 7271 | if( p->zErrMsg==0 && rc!=SQLITE_IOERR_NOMEM ){ |
| 7272 | sqlite3VdbeError(p, "%s", sqlite3ErrStr(rc)); |
| 7273 | } |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 7274 | p->rc = rc; |
drh | f68521c | 2016-03-21 12:28:02 +0000 | [diff] [blame] | 7275 | sqlite3SystemError(db, rc); |
drh | a64fa91 | 2010-03-04 00:53:32 +0000 | [diff] [blame] | 7276 | testcase( sqlite3GlobalConfig.xLog!=0 ); |
| 7277 | sqlite3_log(rc, "statement aborts at %d: [%s] %s", |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 7278 | (int)(pOp - aOp), p->zSql, p->zErrMsg); |
drh | 92f02c3 | 2004-09-02 14:57:08 +0000 | [diff] [blame] | 7279 | sqlite3VdbeHalt(p); |
drh | 4a642b6 | 2016-02-05 01:55:27 +0000 | [diff] [blame] | 7280 | if( rc==SQLITE_IOERR_NOMEM ) sqlite3OomFault(db); |
danielk1977 | 7eaabcd | 2008-07-07 14:56:56 +0000 | [diff] [blame] | 7281 | rc = SQLITE_ERROR; |
drh | cdf011d | 2011-04-04 21:25:28 +0000 | [diff] [blame] | 7282 | if( resetSchemaOnFault>0 ){ |
drh | 81028a4 | 2012-05-15 18:28:27 +0000 | [diff] [blame] | 7283 | sqlite3ResetOneSchema(db, resetSchemaOnFault-1); |
drh | bdaec52 | 2011-04-04 00:14:43 +0000 | [diff] [blame] | 7284 | } |
drh | 900b31e | 2007-08-28 02:27:51 +0000 | [diff] [blame] | 7285 | |
| 7286 | /* This is the only way out of this procedure. We have to |
| 7287 | ** release the mutexes on btrees that were acquired at the |
| 7288 | ** top. */ |
| 7289 | vdbe_return: |
drh | 77dfd5b | 2013-08-19 11:15:48 +0000 | [diff] [blame] | 7290 | testcase( nVmStep>0 ); |
drh | 9b47ee3 | 2013-08-20 03:13:51 +0000 | [diff] [blame] | 7291 | p->aCounter[SQLITE_STMTSTATUS_VM_STEP] += (int)nVmStep; |
drh | bdaec52 | 2011-04-04 00:14:43 +0000 | [diff] [blame] | 7292 | sqlite3VdbeLeave(p); |
dan | 83f0ab8 | 2016-01-29 18:04:31 +0000 | [diff] [blame] | 7293 | assert( rc!=SQLITE_OK || nExtraDelete==0 |
| 7294 | || sqlite3_strlike("DELETE%",p->zSql,0)!=0 |
| 7295 | ); |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 7296 | return rc; |
| 7297 | |
drh | 023ae03 | 2007-05-08 12:12:16 +0000 | [diff] [blame] | 7298 | /* Jump to here if a string or blob larger than SQLITE_MAX_LENGTH |
| 7299 | ** is encountered. |
| 7300 | */ |
| 7301 | too_big: |
drh | 22c17b8 | 2015-05-15 04:13:15 +0000 | [diff] [blame] | 7302 | sqlite3VdbeError(p, "string or blob too big"); |
drh | 023ae03 | 2007-05-08 12:12:16 +0000 | [diff] [blame] | 7303 | rc = SQLITE_TOOBIG; |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 7304 | goto abort_due_to_error; |
drh | 023ae03 | 2007-05-08 12:12:16 +0000 | [diff] [blame] | 7305 | |
drh | 98640a3 | 2007-06-07 19:08:32 +0000 | [diff] [blame] | 7306 | /* Jump to here if a malloc() fails. |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 7307 | */ |
| 7308 | no_mem: |
drh | 4a642b6 | 2016-02-05 01:55:27 +0000 | [diff] [blame] | 7309 | sqlite3OomFault(db); |
drh | 22c17b8 | 2015-05-15 04:13:15 +0000 | [diff] [blame] | 7310 | sqlite3VdbeError(p, "out of memory"); |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 7311 | rc = SQLITE_NOMEM_BKPT; |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 7312 | goto abort_due_to_error; |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 7313 | |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 7314 | /* Jump to here if the sqlite3_interrupt() API sets the interrupt |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 7315 | ** flag. |
| 7316 | */ |
| 7317 | abort_due_to_interrupt: |
drh | 881feaa | 2006-07-26 01:39:30 +0000 | [diff] [blame] | 7318 | assert( db->u1.isInterrupted ); |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 7319 | rc = db->mallocFailed ? SQLITE_NOMEM_BKPT : SQLITE_INTERRUPT; |
danielk1977 | 026d270 | 2004-06-14 13:14:59 +0000 | [diff] [blame] | 7320 | p->rc = rc; |
drh | 22c17b8 | 2015-05-15 04:13:15 +0000 | [diff] [blame] | 7321 | sqlite3VdbeError(p, "%s", sqlite3ErrStr(rc)); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 7322 | goto abort_due_to_error; |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 7323 | } |