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 | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 114 | #if defined(SQLITE_TEST) && !defined(SQLITE_OMIT_BUILTIN_TEST) |
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 | 290c194 | 2004-08-21 17:54:45 +0000 | [diff] [blame] | 218 | if( p->apCsr[iCur] ){ |
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 | f25a507 | 2009-11-18 23:01:25 +0000 | [diff] [blame] | 224 | memset(pCx, 0, sizeof(VdbeCursor)); |
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 | } |
| 267 | } |
| 268 | |
| 269 | /* |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 270 | ** Processing is determine by the affinity parameter: |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 271 | ** |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 272 | ** SQLITE_AFF_INTEGER: |
| 273 | ** SQLITE_AFF_REAL: |
| 274 | ** SQLITE_AFF_NUMERIC: |
| 275 | ** Try to convert pRec to an integer representation or a |
| 276 | ** floating-point representation if an integer representation |
| 277 | ** is not possible. Note that the integer representation is |
| 278 | ** always preferred, even if the affinity is REAL, because |
| 279 | ** an integer representation is more space efficient on disk. |
| 280 | ** |
| 281 | ** SQLITE_AFF_TEXT: |
| 282 | ** Convert pRec to a text representation. |
| 283 | ** |
drh | 05883a3 | 2015-06-02 15:32:08 +0000 | [diff] [blame] | 284 | ** SQLITE_AFF_BLOB: |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 285 | ** No-op. pRec is unchanged. |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 286 | */ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 287 | static void applyAffinity( |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 288 | Mem *pRec, /* The value to apply affinity to */ |
| 289 | char affinity, /* The affinity to be applied */ |
| 290 | u8 enc /* Use this text encoding */ |
| 291 | ){ |
drh | 7ea31cc | 2014-09-18 14:36:00 +0000 | [diff] [blame] | 292 | if( affinity>=SQLITE_AFF_NUMERIC ){ |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 293 | assert( affinity==SQLITE_AFF_INTEGER || affinity==SQLITE_AFF_REAL |
| 294 | || affinity==SQLITE_AFF_NUMERIC ); |
drh | bd9507c | 2014-08-23 17:21:37 +0000 | [diff] [blame] | 295 | if( (pRec->flags & MEM_Int)==0 ){ |
| 296 | if( (pRec->flags & MEM_Real)==0 ){ |
drh | 11a6eee | 2014-09-19 22:01:54 +0000 | [diff] [blame] | 297 | if( pRec->flags & MEM_Str ) applyNumericAffinity(pRec,1); |
drh | bd9507c | 2014-08-23 17:21:37 +0000 | [diff] [blame] | 298 | }else{ |
| 299 | sqlite3VdbeIntegerAffinity(pRec); |
| 300 | } |
drh | 17c4029 | 2004-07-21 02:53:29 +0000 | [diff] [blame] | 301 | } |
drh | 7ea31cc | 2014-09-18 14:36:00 +0000 | [diff] [blame] | 302 | }else if( affinity==SQLITE_AFF_TEXT ){ |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 303 | /* Only attempt the conversion to TEXT if there is an integer or real |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 304 | ** representation (blob and NULL do not get converted) but no string |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 305 | ** representation. |
| 306 | */ |
| 307 | if( 0==(pRec->flags&MEM_Str) && (pRec->flags&(MEM_Real|MEM_Int)) ){ |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 308 | sqlite3VdbeMemStringify(pRec, enc, 1); |
| 309 | } |
dan | dde548c | 2015-05-19 19:44:25 +0000 | [diff] [blame] | 310 | pRec->flags &= ~(MEM_Real|MEM_Int); |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 311 | } |
| 312 | } |
| 313 | |
danielk1977 | aee18ef | 2005-03-09 12:26:50 +0000 | [diff] [blame] | 314 | /* |
drh | 29d7210 | 2006-02-09 22:13:41 +0000 | [diff] [blame] | 315 | ** Try to convert the type of a function argument or a result column |
| 316 | ** into a numeric representation. Use either INTEGER or REAL whichever |
| 317 | ** is appropriate. But only do the conversion if it is possible without |
| 318 | ** loss of information and return the revised type of the argument. |
drh | 29d7210 | 2006-02-09 22:13:41 +0000 | [diff] [blame] | 319 | */ |
| 320 | int sqlite3_value_numeric_type(sqlite3_value *pVal){ |
drh | 1b27b8c | 2014-02-10 03:21:57 +0000 | [diff] [blame] | 321 | int eType = sqlite3_value_type(pVal); |
| 322 | if( eType==SQLITE_TEXT ){ |
| 323 | Mem *pMem = (Mem*)pVal; |
drh | bd9507c | 2014-08-23 17:21:37 +0000 | [diff] [blame] | 324 | applyNumericAffinity(pMem, 0); |
drh | 1b27b8c | 2014-02-10 03:21:57 +0000 | [diff] [blame] | 325 | eType = sqlite3_value_type(pVal); |
drh | e5a8a1d | 2010-11-18 12:31:24 +0000 | [diff] [blame] | 326 | } |
drh | 1b27b8c | 2014-02-10 03:21:57 +0000 | [diff] [blame] | 327 | return eType; |
drh | 29d7210 | 2006-02-09 22:13:41 +0000 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | /* |
danielk1977 | aee18ef | 2005-03-09 12:26:50 +0000 | [diff] [blame] | 331 | ** Exported version of applyAffinity(). This one works on sqlite3_value*, |
| 332 | ** not the internal Mem* type. |
| 333 | */ |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 334 | void sqlite3ValueApplyAffinity( |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 335 | sqlite3_value *pVal, |
| 336 | u8 affinity, |
| 337 | u8 enc |
| 338 | ){ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 339 | applyAffinity((Mem *)pVal, affinity, enc); |
danielk1977 | aee18ef | 2005-03-09 12:26:50 +0000 | [diff] [blame] | 340 | } |
| 341 | |
drh | 3d1d90a | 2014-03-24 15:00:15 +0000 | [diff] [blame] | 342 | /* |
drh | f1a89ed | 2014-08-23 17:41:15 +0000 | [diff] [blame] | 343 | ** pMem currently only holds a string type (or maybe a BLOB that we can |
| 344 | ** interpret as a string if we want to). Compute its corresponding |
drh | 74eaba4 | 2014-09-18 17:52:15 +0000 | [diff] [blame] | 345 | ** 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] | 346 | ** accordingly. |
| 347 | */ |
| 348 | static u16 SQLITE_NOINLINE computeNumericType(Mem *pMem){ |
| 349 | assert( (pMem->flags & (MEM_Int|MEM_Real))==0 ); |
| 350 | assert( (pMem->flags & (MEM_Str|MEM_Blob))!=0 ); |
drh | 74eaba4 | 2014-09-18 17:52:15 +0000 | [diff] [blame] | 351 | if( sqlite3AtoF(pMem->z, &pMem->u.r, pMem->n, pMem->enc)==0 ){ |
drh | f1a89ed | 2014-08-23 17:41:15 +0000 | [diff] [blame] | 352 | return 0; |
| 353 | } |
| 354 | if( sqlite3Atoi64(pMem->z, &pMem->u.i, pMem->n, pMem->enc)==SQLITE_OK ){ |
| 355 | return MEM_Int; |
| 356 | } |
| 357 | return MEM_Real; |
| 358 | } |
| 359 | |
| 360 | /* |
drh | 3d1d90a | 2014-03-24 15:00:15 +0000 | [diff] [blame] | 361 | ** Return the numeric type for pMem, either MEM_Int or MEM_Real or both or |
| 362 | ** none. |
| 363 | ** |
| 364 | ** Unlike applyNumericAffinity(), this routine does not modify pMem->flags. |
drh | 74eaba4 | 2014-09-18 17:52:15 +0000 | [diff] [blame] | 365 | ** But it does set pMem->u.r and pMem->u.i appropriately. |
drh | 3d1d90a | 2014-03-24 15:00:15 +0000 | [diff] [blame] | 366 | */ |
| 367 | static u16 numericType(Mem *pMem){ |
| 368 | if( pMem->flags & (MEM_Int|MEM_Real) ){ |
| 369 | return pMem->flags & (MEM_Int|MEM_Real); |
| 370 | } |
| 371 | if( pMem->flags & (MEM_Str|MEM_Blob) ){ |
drh | f1a89ed | 2014-08-23 17:41:15 +0000 | [diff] [blame] | 372 | return computeNumericType(pMem); |
drh | 3d1d90a | 2014-03-24 15:00:15 +0000 | [diff] [blame] | 373 | } |
| 374 | return 0; |
| 375 | } |
| 376 | |
danielk1977 | b5402fb | 2005-01-12 07:15:04 +0000 | [diff] [blame] | 377 | #ifdef SQLITE_DEBUG |
drh | b6f5452 | 2004-05-20 02:42:16 +0000 | [diff] [blame] | 378 | /* |
danielk1977 | ca6b291 | 2004-05-21 10:49:47 +0000 | [diff] [blame] | 379 | ** Write a nice string representation of the contents of cell pMem |
| 380 | ** into buffer zBuf, length nBuf. |
| 381 | */ |
drh | 7416170 | 2006-02-24 02:53:49 +0000 | [diff] [blame] | 382 | void sqlite3VdbeMemPrettyPrint(Mem *pMem, char *zBuf){ |
danielk1977 | ca6b291 | 2004-05-21 10:49:47 +0000 | [diff] [blame] | 383 | char *zCsr = zBuf; |
| 384 | int f = pMem->flags; |
| 385 | |
drh | 5719628 | 2004-10-06 15:41:16 +0000 | [diff] [blame] | 386 | static const char *const encnames[] = {"(X)", "(8)", "(16LE)", "(16BE)"}; |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame] | 387 | |
danielk1977 | ca6b291 | 2004-05-21 10:49:47 +0000 | [diff] [blame] | 388 | if( f&MEM_Blob ){ |
| 389 | int i; |
| 390 | char c; |
| 391 | if( f & MEM_Dyn ){ |
| 392 | c = 'z'; |
| 393 | assert( (f & (MEM_Static|MEM_Ephem))==0 ); |
| 394 | }else if( f & MEM_Static ){ |
| 395 | c = 't'; |
| 396 | assert( (f & (MEM_Dyn|MEM_Ephem))==0 ); |
| 397 | }else if( f & MEM_Ephem ){ |
| 398 | c = 'e'; |
| 399 | assert( (f & (MEM_Static|MEM_Dyn))==0 ); |
| 400 | }else{ |
| 401 | c = 's'; |
| 402 | } |
| 403 | |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 404 | sqlite3_snprintf(100, zCsr, "%c", c); |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 405 | zCsr += sqlite3Strlen30(zCsr); |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 406 | sqlite3_snprintf(100, zCsr, "%d[", pMem->n); |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 407 | zCsr += sqlite3Strlen30(zCsr); |
danielk1977 | ca6b291 | 2004-05-21 10:49:47 +0000 | [diff] [blame] | 408 | for(i=0; i<16 && i<pMem->n; i++){ |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 409 | sqlite3_snprintf(100, zCsr, "%02X", ((int)pMem->z[i] & 0xFF)); |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 410 | zCsr += sqlite3Strlen30(zCsr); |
danielk1977 | ca6b291 | 2004-05-21 10:49:47 +0000 | [diff] [blame] | 411 | } |
| 412 | for(i=0; i<16 && i<pMem->n; i++){ |
| 413 | char z = pMem->z[i]; |
| 414 | if( z<32 || z>126 ) *zCsr++ = '.'; |
| 415 | else *zCsr++ = z; |
| 416 | } |
| 417 | |
drh | e718efe | 2007-05-10 21:14:03 +0000 | [diff] [blame] | 418 | sqlite3_snprintf(100, zCsr, "]%s", encnames[pMem->enc]); |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 419 | zCsr += sqlite3Strlen30(zCsr); |
drh | fdf972a | 2007-05-02 13:30:27 +0000 | [diff] [blame] | 420 | if( f & MEM_Zero ){ |
drh | 8df3284 | 2008-12-09 02:51:23 +0000 | [diff] [blame] | 421 | sqlite3_snprintf(100, zCsr,"+%dz",pMem->u.nZero); |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 422 | zCsr += sqlite3Strlen30(zCsr); |
drh | fdf972a | 2007-05-02 13:30:27 +0000 | [diff] [blame] | 423 | } |
danielk1977 | b1bc953 | 2004-05-22 03:05:33 +0000 | [diff] [blame] | 424 | *zCsr = '\0'; |
| 425 | }else if( f & MEM_Str ){ |
| 426 | int j, k; |
| 427 | zBuf[0] = ' '; |
| 428 | if( f & MEM_Dyn ){ |
| 429 | zBuf[1] = 'z'; |
| 430 | assert( (f & (MEM_Static|MEM_Ephem))==0 ); |
| 431 | }else if( f & MEM_Static ){ |
| 432 | zBuf[1] = 't'; |
| 433 | assert( (f & (MEM_Dyn|MEM_Ephem))==0 ); |
| 434 | }else if( f & MEM_Ephem ){ |
| 435 | zBuf[1] = 'e'; |
| 436 | assert( (f & (MEM_Static|MEM_Dyn))==0 ); |
| 437 | }else{ |
| 438 | zBuf[1] = 's'; |
| 439 | } |
| 440 | k = 2; |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 441 | sqlite3_snprintf(100, &zBuf[k], "%d", pMem->n); |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 442 | k += sqlite3Strlen30(&zBuf[k]); |
danielk1977 | b1bc953 | 2004-05-22 03:05:33 +0000 | [diff] [blame] | 443 | zBuf[k++] = '['; |
| 444 | for(j=0; j<15 && j<pMem->n; j++){ |
| 445 | u8 c = pMem->z[j]; |
danielk1977 | b1bc953 | 2004-05-22 03:05:33 +0000 | [diff] [blame] | 446 | if( c>=0x20 && c<0x7f ){ |
| 447 | zBuf[k++] = c; |
| 448 | }else{ |
| 449 | zBuf[k++] = '.'; |
| 450 | } |
| 451 | } |
| 452 | zBuf[k++] = ']'; |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 453 | sqlite3_snprintf(100,&zBuf[k], encnames[pMem->enc]); |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 454 | k += sqlite3Strlen30(&zBuf[k]); |
danielk1977 | b1bc953 | 2004-05-22 03:05:33 +0000 | [diff] [blame] | 455 | zBuf[k++] = 0; |
danielk1977 | ca6b291 | 2004-05-21 10:49:47 +0000 | [diff] [blame] | 456 | } |
danielk1977 | ca6b291 | 2004-05-21 10:49:47 +0000 | [diff] [blame] | 457 | } |
| 458 | #endif |
| 459 | |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 460 | #ifdef SQLITE_DEBUG |
| 461 | /* |
| 462 | ** Print the value of a register for tracing purposes: |
| 463 | */ |
drh | 84e55a8 | 2013-11-13 17:58:23 +0000 | [diff] [blame] | 464 | static void memTracePrint(Mem *p){ |
drh | a5750cf | 2014-02-07 13:20:31 +0000 | [diff] [blame] | 465 | if( p->flags & MEM_Undefined ){ |
drh | 84e55a8 | 2013-11-13 17:58:23 +0000 | [diff] [blame] | 466 | printf(" undefined"); |
drh | 953f761 | 2012-12-07 22:18:54 +0000 | [diff] [blame] | 467 | }else if( p->flags & MEM_Null ){ |
drh | 84e55a8 | 2013-11-13 17:58:23 +0000 | [diff] [blame] | 468 | printf(" NULL"); |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 469 | }else if( (p->flags & (MEM_Int|MEM_Str))==(MEM_Int|MEM_Str) ){ |
drh | 84e55a8 | 2013-11-13 17:58:23 +0000 | [diff] [blame] | 470 | printf(" si:%lld", p->u.i); |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 471 | }else if( p->flags & MEM_Int ){ |
drh | 84e55a8 | 2013-11-13 17:58:23 +0000 | [diff] [blame] | 472 | printf(" i:%lld", p->u.i); |
drh | 0b3bf92 | 2009-06-15 20:45:34 +0000 | [diff] [blame] | 473 | #ifndef SQLITE_OMIT_FLOATING_POINT |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 474 | }else if( p->flags & MEM_Real ){ |
drh | 74eaba4 | 2014-09-18 17:52:15 +0000 | [diff] [blame] | 475 | printf(" r:%g", p->u.r); |
drh | 0b3bf92 | 2009-06-15 20:45:34 +0000 | [diff] [blame] | 476 | #endif |
drh | 733bf1b | 2009-04-22 00:47:00 +0000 | [diff] [blame] | 477 | }else if( p->flags & MEM_RowSet ){ |
drh | 84e55a8 | 2013-11-13 17:58:23 +0000 | [diff] [blame] | 478 | printf(" (rowset)"); |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 479 | }else{ |
| 480 | char zBuf[200]; |
| 481 | sqlite3VdbeMemPrettyPrint(p, zBuf); |
drh | 84e55a8 | 2013-11-13 17:58:23 +0000 | [diff] [blame] | 482 | printf(" %s", zBuf); |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 483 | } |
dan | 5b6c8e4 | 2016-01-30 15:46:03 +0000 | [diff] [blame] | 484 | if( p->flags & MEM_Subtype ) printf(" subtype=0x%02x", p->eSubtype); |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 485 | } |
drh | 84e55a8 | 2013-11-13 17:58:23 +0000 | [diff] [blame] | 486 | static void registerTrace(int iReg, Mem *p){ |
| 487 | printf("REG[%d] = ", iReg); |
| 488 | memTracePrint(p); |
| 489 | printf("\n"); |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 490 | } |
| 491 | #endif |
| 492 | |
| 493 | #ifdef SQLITE_DEBUG |
drh | 84e55a8 | 2013-11-13 17:58:23 +0000 | [diff] [blame] | 494 | # define REGISTER_TRACE(R,M) if(db->flags&SQLITE_VdbeTrace)registerTrace(R,M) |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 495 | #else |
| 496 | # define REGISTER_TRACE(R,M) |
| 497 | #endif |
| 498 | |
danielk1977 | 84ac9d0 | 2004-05-18 09:58:06 +0000 | [diff] [blame] | 499 | |
drh | 7b39686 | 2003-01-01 23:06:20 +0000 | [diff] [blame] | 500 | #ifdef VDBE_PROFILE |
shane | 9bcbdad | 2008-05-29 20:22:37 +0000 | [diff] [blame] | 501 | |
| 502 | /* |
| 503 | ** hwtime.h contains inline assembler code for implementing |
| 504 | ** high-performance timing routines. |
drh | 7b39686 | 2003-01-01 23:06:20 +0000 | [diff] [blame] | 505 | */ |
shane | 9bcbdad | 2008-05-29 20:22:37 +0000 | [diff] [blame] | 506 | #include "hwtime.h" |
| 507 | |
drh | 7b39686 | 2003-01-01 23:06:20 +0000 | [diff] [blame] | 508 | #endif |
| 509 | |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 510 | #ifndef NDEBUG |
| 511 | /* |
| 512 | ** This function is only called from within an assert() expression. It |
| 513 | ** checks that the sqlite3.nTransaction variable is correctly set to |
| 514 | ** the number of non-transaction savepoints currently in the |
| 515 | ** linked list starting at sqlite3.pSavepoint. |
| 516 | ** |
| 517 | ** Usage: |
| 518 | ** |
| 519 | ** assert( checkSavepointCount(db) ); |
| 520 | */ |
| 521 | static int checkSavepointCount(sqlite3 *db){ |
| 522 | int n = 0; |
| 523 | Savepoint *p; |
| 524 | for(p=db->pSavepoint; p; p=p->pNext) n++; |
| 525 | assert( n==(db->nSavepoint + db->isTransactionSavepoint) ); |
| 526 | return 1; |
| 527 | } |
| 528 | #endif |
| 529 | |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 530 | /* |
| 531 | ** Return the register of pOp->p2 after first preparing it to be |
| 532 | ** overwritten with an integer value. |
drh | 9eef8c6 | 2015-10-15 17:31:41 +0000 | [diff] [blame] | 533 | */ |
| 534 | static SQLITE_NOINLINE Mem *out2PrereleaseWithClear(Mem *pOut){ |
| 535 | sqlite3VdbeMemSetNull(pOut); |
| 536 | pOut->flags = MEM_Int; |
| 537 | return pOut; |
| 538 | } |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 539 | static Mem *out2Prerelease(Vdbe *p, VdbeOp *pOp){ |
| 540 | Mem *pOut; |
| 541 | assert( pOp->p2>0 ); |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 542 | assert( pOp->p2<=(p->nMem+1 - p->nCursor) ); |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 543 | pOut = &p->aMem[pOp->p2]; |
| 544 | memAboutToChange(p, pOut); |
drh | 9eef8c6 | 2015-10-15 17:31:41 +0000 | [diff] [blame] | 545 | if( VdbeMemDynamic(pOut) ){ |
| 546 | return out2PrereleaseWithClear(pOut); |
| 547 | }else{ |
| 548 | pOut->flags = MEM_Int; |
| 549 | return pOut; |
| 550 | } |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 551 | } |
| 552 | |
drh | b975598 | 2010-07-24 16:34:37 +0000 | [diff] [blame] | 553 | |
| 554 | /* |
drh | 0fd6135 | 2014-02-07 02:29:45 +0000 | [diff] [blame] | 555 | ** Execute as much of a VDBE program as we can. |
| 556 | ** This is the core of sqlite3_step(). |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 557 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 558 | int sqlite3VdbeExec( |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 559 | Vdbe *p /* The VDBE */ |
| 560 | ){ |
drh | bbe879d | 2009-11-14 18:04:35 +0000 | [diff] [blame] | 561 | Op *aOp = p->aOp; /* Copy of p->aOp */ |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 562 | Op *pOp = aOp; /* Current operation */ |
drh | 6dc4148 | 2015-04-16 17:31:02 +0000 | [diff] [blame] | 563 | #if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE) |
| 564 | Op *pOrigOp; /* Value of pOp at the top of the loop */ |
| 565 | #endif |
drh | b89aeb6 | 2016-01-27 15:49:32 +0000 | [diff] [blame] | 566 | #ifdef SQLITE_DEBUG |
drh | def19e3 | 2016-01-27 16:26:25 +0000 | [diff] [blame] | 567 | int nExtraDelete = 0; /* Verifies FORDELETE and AUXDELETE flags */ |
drh | b89aeb6 | 2016-01-27 15:49:32 +0000 | [diff] [blame] | 568 | #endif |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 569 | int rc = SQLITE_OK; /* Value to return */ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 570 | sqlite3 *db = p->db; /* The database */ |
drh | cdf011d | 2011-04-04 21:25:28 +0000 | [diff] [blame] | 571 | u8 resetSchemaOnFault = 0; /* Reset schema after an error if positive */ |
drh | 8079a0d | 2006-01-12 17:20:50 +0000 | [diff] [blame] | 572 | u8 encoding = ENC(db); /* The database encoding */ |
drh | bf159fa | 2013-06-25 22:01:22 +0000 | [diff] [blame] | 573 | int iCompare = 0; /* Result of last OP_Compare operation */ |
| 574 | unsigned nVmStep = 0; /* Number of virtual machine steps */ |
drh | 49afe3a | 2013-07-10 03:05:14 +0000 | [diff] [blame] | 575 | #ifndef SQLITE_OMIT_PROGRESS_CALLBACK |
drh | 323df79 | 2013-08-05 19:11:29 +0000 | [diff] [blame] | 576 | unsigned nProgressLimit = 0;/* Invoke xProgress() when nVmStep reaches this */ |
drh | 49afe3a | 2013-07-10 03:05:14 +0000 | [diff] [blame] | 577 | #endif |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 578 | Mem *aMem = p->aMem; /* Copy of p->aMem */ |
drh | b27b7f5 | 2008-12-10 18:03:45 +0000 | [diff] [blame] | 579 | Mem *pIn1 = 0; /* 1st input operand */ |
| 580 | Mem *pIn2 = 0; /* 2nd input operand */ |
| 581 | Mem *pIn3 = 0; /* 3rd input operand */ |
| 582 | Mem *pOut = 0; /* Output operand */ |
shane | be21779 | 2009-03-05 04:20:31 +0000 | [diff] [blame] | 583 | int *aPermute = 0; /* Permutation of columns for OP_Compare */ |
drh | 99a6692 | 2011-05-13 18:51:42 +0000 | [diff] [blame] | 584 | i64 lastRowid = db->lastRowid; /* Saved value of the last insert ROWID */ |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 585 | #ifdef VDBE_PROFILE |
shane | 9bcbdad | 2008-05-29 20:22:37 +0000 | [diff] [blame] | 586 | u64 start; /* CPU clock count at start of opcode */ |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 587 | #endif |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 588 | /*** INSERT STACK UNION HERE ***/ |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 589 | |
drh | ca48c90 | 2008-01-18 14:08:24 +0000 | [diff] [blame] | 590 | assert( p->magic==VDBE_MAGIC_RUN ); /* sqlite3_step() verifies this */ |
drh | bdaec52 | 2011-04-04 00:14:43 +0000 | [diff] [blame] | 591 | sqlite3VdbeEnter(p); |
danielk1977 | 2e588c7 | 2005-12-09 14:25:08 +0000 | [diff] [blame] | 592 | if( p->rc==SQLITE_NOMEM ){ |
| 593 | /* This happens if a malloc() inside a call to sqlite3_column_text() or |
| 594 | ** sqlite3_column_text16() failed. */ |
| 595 | goto no_mem; |
| 596 | } |
drh | cbd8db3 | 2015-08-20 17:18:32 +0000 | [diff] [blame] | 597 | assert( p->rc==SQLITE_OK || (p->rc&0xff)==SQLITE_BUSY ); |
drh | 1713afb | 2013-06-28 01:24:57 +0000 | [diff] [blame] | 598 | assert( p->bIsReader || p->readOnly!=0 ); |
drh | 3a84069 | 2003-01-29 22:58:26 +0000 | [diff] [blame] | 599 | p->rc = SQLITE_OK; |
drh | 95a7b3e | 2013-09-16 12:57:19 +0000 | [diff] [blame] | 600 | p->iCurrentTime = 0; |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 601 | assert( p->explain==0 ); |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 602 | p->pResultSet = 0; |
drh | a4afb65 | 2005-07-09 02:16:02 +0000 | [diff] [blame] | 603 | db->busyHandler.nBusy = 0; |
drh | 0fd6135 | 2014-02-07 02:29:45 +0000 | [diff] [blame] | 604 | if( db->u1.isInterrupted ) goto abort_due_to_interrupt; |
drh | 602c237 | 2007-03-01 00:29:13 +0000 | [diff] [blame] | 605 | sqlite3VdbeIOTraceSql(p); |
drh | 0d1961e | 2013-07-25 16:27:51 +0000 | [diff] [blame] | 606 | #ifndef SQLITE_OMIT_PROGRESS_CALLBACK |
| 607 | if( db->xProgress ){ |
drh | 6cbbdb0 | 2015-06-24 14:36:27 +0000 | [diff] [blame] | 608 | u32 iPrior = p->aCounter[SQLITE_STMTSTATUS_VM_STEP]; |
drh | 0d1961e | 2013-07-25 16:27:51 +0000 | [diff] [blame] | 609 | assert( 0 < db->nProgressOps ); |
drh | 6cbbdb0 | 2015-06-24 14:36:27 +0000 | [diff] [blame] | 610 | nProgressLimit = db->nProgressOps - (iPrior % db->nProgressOps); |
drh | 0d1961e | 2013-07-25 16:27:51 +0000 | [diff] [blame] | 611 | } |
| 612 | #endif |
drh | 3c23a88 | 2007-01-09 14:01:13 +0000 | [diff] [blame] | 613 | #ifdef SQLITE_DEBUG |
danielk1977 | 2d1d86f | 2008-06-20 14:59:51 +0000 | [diff] [blame] | 614 | sqlite3BeginBenignMalloc(); |
drh | 84e55a8 | 2013-11-13 17:58:23 +0000 | [diff] [blame] | 615 | if( p->pc==0 |
| 616 | && (p->db->flags & (SQLITE_VdbeListing|SQLITE_VdbeEQP|SQLITE_VdbeTrace))!=0 |
| 617 | ){ |
drh | 3c23a88 | 2007-01-09 14:01:13 +0000 | [diff] [blame] | 618 | int i; |
drh | 84e55a8 | 2013-11-13 17:58:23 +0000 | [diff] [blame] | 619 | int once = 1; |
drh | 3c23a88 | 2007-01-09 14:01:13 +0000 | [diff] [blame] | 620 | sqlite3VdbePrintSql(p); |
drh | 84e55a8 | 2013-11-13 17:58:23 +0000 | [diff] [blame] | 621 | if( p->db->flags & SQLITE_VdbeListing ){ |
| 622 | printf("VDBE Program Listing:\n"); |
| 623 | for(i=0; i<p->nOp; i++){ |
| 624 | sqlite3VdbePrintOp(stdout, i, &aOp[i]); |
| 625 | } |
drh | 3c23a88 | 2007-01-09 14:01:13 +0000 | [diff] [blame] | 626 | } |
drh | 84e55a8 | 2013-11-13 17:58:23 +0000 | [diff] [blame] | 627 | if( p->db->flags & SQLITE_VdbeEQP ){ |
| 628 | for(i=0; i<p->nOp; i++){ |
| 629 | if( aOp[i].opcode==OP_Explain ){ |
| 630 | if( once ) printf("VDBE Query Plan:\n"); |
| 631 | printf("%s\n", aOp[i].p4.z); |
| 632 | once = 0; |
| 633 | } |
| 634 | } |
| 635 | } |
| 636 | if( p->db->flags & SQLITE_VdbeTrace ) printf("VDBE Trace:\n"); |
drh | 3c23a88 | 2007-01-09 14:01:13 +0000 | [diff] [blame] | 637 | } |
danielk1977 | 2d1d86f | 2008-06-20 14:59:51 +0000 | [diff] [blame] | 638 | sqlite3EndBenignMalloc(); |
drh | 3c23a88 | 2007-01-09 14:01:13 +0000 | [diff] [blame] | 639 | #endif |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 640 | for(pOp=&aOp[p->pc]; 1; pOp++){ |
| 641 | /* Errors are detected by individual opcodes, with an immediate |
| 642 | ** jumps to abort_due_to_error. */ |
| 643 | assert( rc==SQLITE_OK ); |
| 644 | |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 645 | assert( pOp>=aOp && pOp<&aOp[p->nOp]); |
drh | 7b39686 | 2003-01-01 23:06:20 +0000 | [diff] [blame] | 646 | #ifdef VDBE_PROFILE |
shane | 9bcbdad | 2008-05-29 20:22:37 +0000 | [diff] [blame] | 647 | start = sqlite3Hwtime(); |
drh | 7b39686 | 2003-01-01 23:06:20 +0000 | [diff] [blame] | 648 | #endif |
drh | bf159fa | 2013-06-25 22:01:22 +0000 | [diff] [blame] | 649 | nVmStep++; |
dan | 6f9702e | 2014-11-01 20:38:06 +0000 | [diff] [blame] | 650 | #ifdef SQLITE_ENABLE_STMT_SCANSTATUS |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 651 | if( p->anExec ) p->anExec[(int)(pOp-aOp)]++; |
dan | 6f9702e | 2014-11-01 20:38:06 +0000 | [diff] [blame] | 652 | #endif |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 653 | |
danielk1977 | 8b60e0f | 2005-01-12 09:10:39 +0000 | [diff] [blame] | 654 | /* Only allow tracing if SQLITE_DEBUG is defined. |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 655 | */ |
danielk1977 | 8b60e0f | 2005-01-12 09:10:39 +0000 | [diff] [blame] | 656 | #ifdef SQLITE_DEBUG |
drh | 84e55a8 | 2013-11-13 17:58:23 +0000 | [diff] [blame] | 657 | if( db->flags & SQLITE_VdbeTrace ){ |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 658 | sqlite3VdbePrintOp(stdout, (int)(pOp - aOp), pOp); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 659 | } |
drh | 3f7d4e4 | 2004-07-24 14:35:58 +0000 | [diff] [blame] | 660 | #endif |
| 661 | |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 662 | |
drh | f603871 | 2004-02-08 18:07:34 +0000 | [diff] [blame] | 663 | /* Check to see if we need to simulate an interrupt. This only happens |
| 664 | ** if we have a special test build. |
| 665 | */ |
| 666 | #ifdef SQLITE_TEST |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 667 | if( sqlite3_interrupt_count>0 ){ |
| 668 | sqlite3_interrupt_count--; |
| 669 | if( sqlite3_interrupt_count==0 ){ |
| 670 | sqlite3_interrupt(db); |
drh | f603871 | 2004-02-08 18:07:34 +0000 | [diff] [blame] | 671 | } |
| 672 | } |
| 673 | #endif |
| 674 | |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 675 | /* Sanity checking on other operands */ |
| 676 | #ifdef SQLITE_DEBUG |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 677 | assert( pOp->opflags==sqlite3OpcodeProperty[pOp->opcode] ); |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 678 | if( (pOp->opflags & OPFLG_IN1)!=0 ){ |
| 679 | assert( pOp->p1>0 ); |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 680 | assert( pOp->p1<=(p->nMem+1 - p->nCursor) ); |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 681 | assert( memIsValid(&aMem[pOp->p1]) ); |
drh | 75fd054 | 2014-03-01 16:24:44 +0000 | [diff] [blame] | 682 | assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p1]) ); |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 683 | REGISTER_TRACE(pOp->p1, &aMem[pOp->p1]); |
| 684 | } |
| 685 | if( (pOp->opflags & OPFLG_IN2)!=0 ){ |
| 686 | assert( pOp->p2>0 ); |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 687 | assert( pOp->p2<=(p->nMem+1 - p->nCursor) ); |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 688 | assert( memIsValid(&aMem[pOp->p2]) ); |
drh | 75fd054 | 2014-03-01 16:24:44 +0000 | [diff] [blame] | 689 | assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p2]) ); |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 690 | REGISTER_TRACE(pOp->p2, &aMem[pOp->p2]); |
| 691 | } |
| 692 | if( (pOp->opflags & OPFLG_IN3)!=0 ){ |
| 693 | assert( pOp->p3>0 ); |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 694 | assert( pOp->p3<=(p->nMem+1 - p->nCursor) ); |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 695 | assert( memIsValid(&aMem[pOp->p3]) ); |
drh | 75fd054 | 2014-03-01 16:24:44 +0000 | [diff] [blame] | 696 | assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p3]) ); |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 697 | REGISTER_TRACE(pOp->p3, &aMem[pOp->p3]); |
| 698 | } |
| 699 | if( (pOp->opflags & OPFLG_OUT2)!=0 ){ |
| 700 | assert( pOp->p2>0 ); |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 701 | assert( pOp->p2<=(p->nMem+1 - p->nCursor) ); |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 702 | memAboutToChange(p, &aMem[pOp->p2]); |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 703 | } |
| 704 | if( (pOp->opflags & OPFLG_OUT3)!=0 ){ |
| 705 | assert( pOp->p3>0 ); |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 706 | assert( pOp->p3<=(p->nMem+1 - p->nCursor) ); |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 707 | memAboutToChange(p, &aMem[pOp->p3]); |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 708 | } |
| 709 | #endif |
drh | 6dc4148 | 2015-04-16 17:31:02 +0000 | [diff] [blame] | 710 | #if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE) |
| 711 | pOrigOp = pOp; |
| 712 | #endif |
drh | 93952eb | 2009-11-13 19:43:43 +0000 | [diff] [blame] | 713 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 714 | switch( pOp->opcode ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 715 | |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 716 | /***************************************************************************** |
| 717 | ** What follows is a massive switch statement where each case implements a |
| 718 | ** separate instruction in the virtual machine. If we follow the usual |
| 719 | ** indentation conventions, each case should be indented by 6 spaces. But |
| 720 | ** that is a lot of wasted space on the left margin. So the code within |
| 721 | ** the switch statement will break with convention and be flush-left. Another |
| 722 | ** big comment (similar to this one) will mark the point in the code where |
| 723 | ** we transition back to normal indentation. |
drh | ac82fcf | 2002-09-08 17:23:41 +0000 | [diff] [blame] | 724 | ** |
| 725 | ** The formatting of each case is important. The makefile for SQLite |
| 726 | ** generates two C files "opcodes.h" and "opcodes.c" by scanning this |
| 727 | ** file looking for lines that begin with "case OP_". The opcodes.h files |
| 728 | ** will be filled with #defines that give unique integer values to each |
| 729 | ** 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] | 730 | ** each string is the symbolic name for the corresponding opcode. If the |
| 731 | ** case statement is followed by a comment of the form "/# same as ... #/" |
| 732 | ** that comment is used to determine the particular value of the opcode. |
drh | ac82fcf | 2002-09-08 17:23:41 +0000 | [diff] [blame] | 733 | ** |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 734 | ** Other keywords in the comment that follows each case are used to |
| 735 | ** construct the OPFLG_INITIALIZER value that initializes opcodeProperty[]. |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 736 | ** Keywords include: in1, in2, in3, out2, out3. See |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 737 | ** the mkopcodeh.awk script for additional information. |
danielk1977 | bc04f85 | 2005-03-29 08:26:13 +0000 | [diff] [blame] | 738 | ** |
drh | ac82fcf | 2002-09-08 17:23:41 +0000 | [diff] [blame] | 739 | ** Documentation about VDBE opcodes is generated by scanning this file |
| 740 | ** for lines of that contain "Opcode:". That line and all subsequent |
| 741 | ** comment lines are used in the generation of the opcode.html documentation |
| 742 | ** file. |
| 743 | ** |
| 744 | ** SUMMARY: |
| 745 | ** |
| 746 | ** Formatting is important to scripts that scan this file. |
| 747 | ** Do not deviate from the formatting style currently in use. |
| 748 | ** |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 749 | *****************************************************************************/ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 750 | |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 751 | /* Opcode: Goto * P2 * * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 752 | ** |
| 753 | ** An unconditional jump to address P2. |
| 754 | ** The next instruction executed will be |
| 755 | ** the one at index P2 from the beginning of |
| 756 | ** the program. |
drh | fe70510 | 2014-03-06 13:38:37 +0000 | [diff] [blame] | 757 | ** |
| 758 | ** The P1 parameter is not actually used by this opcode. However, it |
| 759 | ** is sometimes set to 1 instead of 0 as a hint to the command-line shell |
| 760 | ** that this Goto is the bottom of a loop and that the lines from P2 down |
| 761 | ** to the current line should be indented for EXPLAIN output. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 762 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 763 | case OP_Goto: { /* jump */ |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 764 | jump_to_p2_and_check_for_interrupt: |
| 765 | pOp = &aOp[pOp->p2 - 1]; |
drh | 49afe3a | 2013-07-10 03:05:14 +0000 | [diff] [blame] | 766 | |
| 767 | /* Opcodes that are used as the bottom of a loop (OP_Next, OP_Prev, |
| 768 | ** OP_VNext, OP_RowSetNext, or OP_SorterNext) all jump here upon |
| 769 | ** completion. Check to see if sqlite3_interrupt() has been called |
| 770 | ** or if the progress callback needs to be invoked. |
| 771 | ** |
| 772 | ** This code uses unstructured "goto" statements and does not look clean. |
| 773 | ** But that is not due to sloppy coding habits. The code is written this |
| 774 | ** way for performance, to avoid having to run the interrupt and progress |
| 775 | ** checks on every opcode. This helps sqlite3_step() to run about 1.5% |
| 776 | ** faster according to "valgrind --tool=cachegrind" */ |
| 777 | check_for_interrupt: |
drh | 0fd6135 | 2014-02-07 02:29:45 +0000 | [diff] [blame] | 778 | if( db->u1.isInterrupted ) goto abort_due_to_interrupt; |
drh | 49afe3a | 2013-07-10 03:05:14 +0000 | [diff] [blame] | 779 | #ifndef SQLITE_OMIT_PROGRESS_CALLBACK |
| 780 | /* Call the progress callback if it is configured and the required number |
| 781 | ** of VDBE ops have been executed (either since this invocation of |
| 782 | ** sqlite3VdbeExec() or since last time the progress callback was called). |
| 783 | ** If the progress callback returns non-zero, exit the virtual machine with |
| 784 | ** a return code SQLITE_ABORT. |
| 785 | */ |
drh | 0d1961e | 2013-07-25 16:27:51 +0000 | [diff] [blame] | 786 | if( db->xProgress!=0 && nVmStep>=nProgressLimit ){ |
drh | 400fcba | 2013-11-14 00:09:48 +0000 | [diff] [blame] | 787 | assert( db->nProgressOps!=0 ); |
| 788 | nProgressLimit = nVmStep + db->nProgressOps - (nVmStep%db->nProgressOps); |
| 789 | if( db->xProgress(db->pProgressArg) ){ |
drh | 49afe3a | 2013-07-10 03:05:14 +0000 | [diff] [blame] | 790 | rc = SQLITE_INTERRUPT; |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 791 | goto abort_due_to_error; |
drh | 49afe3a | 2013-07-10 03:05:14 +0000 | [diff] [blame] | 792 | } |
drh | 49afe3a | 2013-07-10 03:05:14 +0000 | [diff] [blame] | 793 | } |
| 794 | #endif |
| 795 | |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 796 | break; |
| 797 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 798 | |
drh | 2eb9537 | 2008-06-06 15:04:36 +0000 | [diff] [blame] | 799 | /* Opcode: Gosub P1 P2 * * * |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 800 | ** |
drh | 2eb9537 | 2008-06-06 15:04:36 +0000 | [diff] [blame] | 801 | ** Write the current address onto register P1 |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 802 | ** and then jump to address P2. |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 803 | */ |
drh | b8475df | 2011-12-09 16:21:19 +0000 | [diff] [blame] | 804 | case OP_Gosub: { /* jump */ |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 805 | assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) ); |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 806 | pIn1 = &aMem[pOp->p1]; |
drh | c91b2fd | 2014-03-01 18:13:23 +0000 | [diff] [blame] | 807 | assert( VdbeMemDynamic(pIn1)==0 ); |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 808 | memAboutToChange(p, pIn1); |
drh | 2eb9537 | 2008-06-06 15:04:36 +0000 | [diff] [blame] | 809 | pIn1->flags = MEM_Int; |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 810 | pIn1->u.i = (int)(pOp-aOp); |
drh | 2eb9537 | 2008-06-06 15:04:36 +0000 | [diff] [blame] | 811 | REGISTER_TRACE(pOp->p1, pIn1); |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 812 | |
| 813 | /* Most jump operations do a goto to this spot in order to update |
| 814 | ** the pOp pointer. */ |
| 815 | jump_to_p2: |
| 816 | pOp = &aOp[pOp->p2 - 1]; |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 817 | break; |
| 818 | } |
| 819 | |
drh | 2eb9537 | 2008-06-06 15:04:36 +0000 | [diff] [blame] | 820 | /* Opcode: Return P1 * * * * |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 821 | ** |
drh | 81cf13e | 2014-02-07 18:27:53 +0000 | [diff] [blame] | 822 | ** Jump to the next instruction after the address in register P1. After |
| 823 | ** the jump, register P1 becomes undefined. |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 824 | */ |
drh | 2eb9537 | 2008-06-06 15:04:36 +0000 | [diff] [blame] | 825 | case OP_Return: { /* in1 */ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 826 | pIn1 = &aMem[pOp->p1]; |
drh | 81cf13e | 2014-02-07 18:27:53 +0000 | [diff] [blame] | 827 | assert( pIn1->flags==MEM_Int ); |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 828 | pOp = &aOp[pIn1->u.i]; |
drh | 81cf13e | 2014-02-07 18:27:53 +0000 | [diff] [blame] | 829 | pIn1->flags = MEM_Undefined; |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 830 | break; |
| 831 | } |
| 832 | |
drh | ed71a83 | 2014-02-07 19:18:10 +0000 | [diff] [blame] | 833 | /* Opcode: InitCoroutine P1 P2 P3 * * |
drh | 81cf13e | 2014-02-07 18:27:53 +0000 | [diff] [blame] | 834 | ** |
drh | 5dad9a3 | 2014-07-25 18:37:42 +0000 | [diff] [blame] | 835 | ** Set up register P1 so that it will Yield to the coroutine |
drh | ed71a83 | 2014-02-07 19:18:10 +0000 | [diff] [blame] | 836 | ** located at address P3. |
| 837 | ** |
drh | 5dad9a3 | 2014-07-25 18:37:42 +0000 | [diff] [blame] | 838 | ** If P2!=0 then the coroutine implementation immediately follows |
| 839 | ** this opcode. So jump over the coroutine implementation to |
drh | ed71a83 | 2014-02-07 19:18:10 +0000 | [diff] [blame] | 840 | ** address P2. |
drh | 5dad9a3 | 2014-07-25 18:37:42 +0000 | [diff] [blame] | 841 | ** |
| 842 | ** See also: EndCoroutine |
drh | 81cf13e | 2014-02-07 18:27:53 +0000 | [diff] [blame] | 843 | */ |
| 844 | case OP_InitCoroutine: { /* jump */ |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 845 | assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) ); |
drh | ed71a83 | 2014-02-07 19:18:10 +0000 | [diff] [blame] | 846 | assert( pOp->p2>=0 && pOp->p2<p->nOp ); |
| 847 | assert( pOp->p3>=0 && pOp->p3<p->nOp ); |
drh | 81cf13e | 2014-02-07 18:27:53 +0000 | [diff] [blame] | 848 | pOut = &aMem[pOp->p1]; |
drh | ed71a83 | 2014-02-07 19:18:10 +0000 | [diff] [blame] | 849 | assert( !VdbeMemDynamic(pOut) ); |
| 850 | pOut->u.i = pOp->p3 - 1; |
drh | 81cf13e | 2014-02-07 18:27:53 +0000 | [diff] [blame] | 851 | pOut->flags = MEM_Int; |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 852 | if( pOp->p2 ) goto jump_to_p2; |
drh | 81cf13e | 2014-02-07 18:27:53 +0000 | [diff] [blame] | 853 | break; |
| 854 | } |
| 855 | |
| 856 | /* Opcode: EndCoroutine P1 * * * * |
| 857 | ** |
drh | bc5cf38 | 2014-08-06 01:08:07 +0000 | [diff] [blame] | 858 | ** The instruction at the address in register P1 is a Yield. |
drh | 5dad9a3 | 2014-07-25 18:37:42 +0000 | [diff] [blame] | 859 | ** Jump to the P2 parameter of that Yield. |
drh | 81cf13e | 2014-02-07 18:27:53 +0000 | [diff] [blame] | 860 | ** After the jump, register P1 becomes undefined. |
drh | 5dad9a3 | 2014-07-25 18:37:42 +0000 | [diff] [blame] | 861 | ** |
| 862 | ** See also: InitCoroutine |
drh | 81cf13e | 2014-02-07 18:27:53 +0000 | [diff] [blame] | 863 | */ |
| 864 | case OP_EndCoroutine: { /* in1 */ |
| 865 | VdbeOp *pCaller; |
| 866 | pIn1 = &aMem[pOp->p1]; |
| 867 | assert( pIn1->flags==MEM_Int ); |
| 868 | assert( pIn1->u.i>=0 && pIn1->u.i<p->nOp ); |
| 869 | pCaller = &aOp[pIn1->u.i]; |
| 870 | assert( pCaller->opcode==OP_Yield ); |
| 871 | assert( pCaller->p2>=0 && pCaller->p2<p->nOp ); |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 872 | pOp = &aOp[pCaller->p2 - 1]; |
drh | 81cf13e | 2014-02-07 18:27:53 +0000 | [diff] [blame] | 873 | pIn1->flags = MEM_Undefined; |
| 874 | break; |
| 875 | } |
| 876 | |
| 877 | /* Opcode: Yield P1 P2 * * * |
drh | e00ee6e | 2008-06-20 15:24:01 +0000 | [diff] [blame] | 878 | ** |
drh | 5dad9a3 | 2014-07-25 18:37:42 +0000 | [diff] [blame] | 879 | ** Swap the program counter with the value in register P1. This |
| 880 | ** has the effect of yielding to a coroutine. |
drh | 81cf13e | 2014-02-07 18:27:53 +0000 | [diff] [blame] | 881 | ** |
drh | 5dad9a3 | 2014-07-25 18:37:42 +0000 | [diff] [blame] | 882 | ** If the coroutine that is launched by this instruction ends with |
| 883 | ** Yield or Return then continue to the next instruction. But if |
| 884 | ** the coroutine launched by this instruction ends with |
| 885 | ** EndCoroutine, then jump to P2 rather than continuing with the |
| 886 | ** next instruction. |
| 887 | ** |
| 888 | ** See also: InitCoroutine |
drh | e00ee6e | 2008-06-20 15:24:01 +0000 | [diff] [blame] | 889 | */ |
drh | 81cf13e | 2014-02-07 18:27:53 +0000 | [diff] [blame] | 890 | case OP_Yield: { /* in1, jump */ |
drh | e00ee6e | 2008-06-20 15:24:01 +0000 | [diff] [blame] | 891 | int pcDest; |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 892 | pIn1 = &aMem[pOp->p1]; |
drh | c91b2fd | 2014-03-01 18:13:23 +0000 | [diff] [blame] | 893 | assert( VdbeMemDynamic(pIn1)==0 ); |
drh | e00ee6e | 2008-06-20 15:24:01 +0000 | [diff] [blame] | 894 | pIn1->flags = MEM_Int; |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 895 | pcDest = (int)pIn1->u.i; |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 896 | pIn1->u.i = (int)(pOp - aOp); |
drh | e00ee6e | 2008-06-20 15:24:01 +0000 | [diff] [blame] | 897 | REGISTER_TRACE(pOp->p1, pIn1); |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 898 | pOp = &aOp[pcDest]; |
drh | e00ee6e | 2008-06-20 15:24:01 +0000 | [diff] [blame] | 899 | break; |
| 900 | } |
| 901 | |
drh | f9c8ce3 | 2013-11-05 13:33:55 +0000 | [diff] [blame] | 902 | /* Opcode: HaltIfNull P1 P2 P3 P4 P5 |
drh | 0fd6135 | 2014-02-07 02:29:45 +0000 | [diff] [blame] | 903 | ** Synopsis: if r[P3]=null halt |
drh | 5053a79 | 2009-02-20 03:02:23 +0000 | [diff] [blame] | 904 | ** |
drh | ef8662b | 2011-06-20 21:47:58 +0000 | [diff] [blame] | 905 | ** Check the value in register P3. If it is NULL then Halt using |
drh | 5053a79 | 2009-02-20 03:02:23 +0000 | [diff] [blame] | 906 | ** parameter P1, P2, and P4 as if this were a Halt instruction. If the |
| 907 | ** 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] | 908 | ** The P5 parameter should be 1. |
drh | 5053a79 | 2009-02-20 03:02:23 +0000 | [diff] [blame] | 909 | */ |
| 910 | case OP_HaltIfNull: { /* in3 */ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 911 | pIn3 = &aMem[pOp->p3]; |
drh | 5053a79 | 2009-02-20 03:02:23 +0000 | [diff] [blame] | 912 | if( (pIn3->flags & MEM_Null)==0 ) break; |
| 913 | /* Fall through into OP_Halt */ |
| 914 | } |
drh | e00ee6e | 2008-06-20 15:24:01 +0000 | [diff] [blame] | 915 | |
drh | f9c8ce3 | 2013-11-05 13:33:55 +0000 | [diff] [blame] | 916 | /* Opcode: Halt P1 P2 * P4 P5 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 917 | ** |
drh | 3d4501e | 2008-12-04 20:40:10 +0000 | [diff] [blame] | 918 | ** Exit immediately. All open cursors, etc are closed |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 919 | ** automatically. |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 920 | ** |
drh | 92f02c3 | 2004-09-02 14:57:08 +0000 | [diff] [blame] | 921 | ** P1 is the result code returned by sqlite3_exec(), sqlite3_reset(), |
| 922 | ** or sqlite3_finalize(). For a normal halt, this should be SQLITE_OK (0). |
| 923 | ** For errors, it can be some other value. If P1!=0 then P2 will determine |
| 924 | ** whether or not to rollback the current transaction. Do not rollback |
| 925 | ** if P2==OE_Fail. Do the rollback if P2==OE_Rollback. If P2==OE_Abort, |
| 926 | ** then back out all changes that have occurred during this execution of the |
drh | b798fa6 | 2002-09-03 19:43:23 +0000 | [diff] [blame] | 927 | ** VDBE, but do not rollback the transaction. |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 928 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 929 | ** If P4 is not null then it is an error message string. |
drh | 7f057c9 | 2005-06-24 03:53:06 +0000 | [diff] [blame] | 930 | ** |
drh | f9c8ce3 | 2013-11-05 13:33:55 +0000 | [diff] [blame] | 931 | ** P5 is a value between 0 and 4, inclusive, that modifies the P4 string. |
| 932 | ** |
| 933 | ** 0: (no change) |
| 934 | ** 1: NOT NULL contraint failed: P4 |
| 935 | ** 2: UNIQUE constraint failed: P4 |
| 936 | ** 3: CHECK constraint failed: P4 |
| 937 | ** 4: FOREIGN KEY constraint failed: P4 |
| 938 | ** |
| 939 | ** If P5 is not zero and P4 is NULL, then everything after the ":" is |
| 940 | ** omitted. |
| 941 | ** |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 942 | ** 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] | 943 | ** every program. So a jump past the last instruction of the program |
| 944 | ** is the same as executing Halt. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 945 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 946 | case OP_Halt: { |
drh | f9c8ce3 | 2013-11-05 13:33:55 +0000 | [diff] [blame] | 947 | const char *zType; |
| 948 | const char *zLogFmt; |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 949 | VdbeFrame *pFrame; |
| 950 | int pcx; |
drh | f9c8ce3 | 2013-11-05 13:33:55 +0000 | [diff] [blame] | 951 | |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 952 | pcx = (int)(pOp - aOp); |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 953 | if( pOp->p1==SQLITE_OK && p->pFrame ){ |
dan | 2832ad4 | 2009-08-31 15:27:27 +0000 | [diff] [blame] | 954 | /* Halt the sub-program. Return control to the parent frame. */ |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 955 | pFrame = p->pFrame; |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 956 | p->pFrame = pFrame->pParent; |
| 957 | p->nFrame--; |
dan | 2832ad4 | 2009-08-31 15:27:27 +0000 | [diff] [blame] | 958 | sqlite3VdbeSetChanges(db, p->nChange); |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 959 | pcx = sqlite3VdbeFrameRestore(pFrame); |
drh | 99a6692 | 2011-05-13 18:51:42 +0000 | [diff] [blame] | 960 | lastRowid = db->lastRowid; |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 961 | if( pOp->p2==OE_Ignore ){ |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 962 | /* Instruction pcx is the OP_Program that invoked the sub-program |
dan | 2832ad4 | 2009-08-31 15:27:27 +0000 | [diff] [blame] | 963 | ** currently being halted. If the p2 instruction of this OP_Halt |
| 964 | ** instruction is set to OE_Ignore, then the sub-program is throwing |
| 965 | ** an IGNORE exception. In this case jump to the address specified |
| 966 | ** as the p2 of the calling OP_Program. */ |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 967 | pcx = p->aOp[pcx].p2-1; |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 968 | } |
drh | bbe879d | 2009-11-14 18:04:35 +0000 | [diff] [blame] | 969 | aOp = p->aOp; |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 970 | aMem = p->aMem; |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 971 | pOp = &aOp[pcx]; |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 972 | break; |
| 973 | } |
drh | 92f02c3 | 2004-09-02 14:57:08 +0000 | [diff] [blame] | 974 | p->rc = pOp->p1; |
shane | 36840fd | 2009-06-26 16:32:13 +0000 | [diff] [blame] | 975 | p->errorAction = (u8)pOp->p2; |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 976 | p->pc = pcx; |
drh | f9c8ce3 | 2013-11-05 13:33:55 +0000 | [diff] [blame] | 977 | if( p->rc ){ |
drh | d9b7ec9 | 2013-11-06 14:05:21 +0000 | [diff] [blame] | 978 | if( pOp->p5 ){ |
| 979 | static const char * const azType[] = { "NOT NULL", "UNIQUE", "CHECK", |
| 980 | "FOREIGN KEY" }; |
| 981 | assert( pOp->p5>=1 && pOp->p5<=4 ); |
| 982 | testcase( pOp->p5==1 ); |
| 983 | testcase( pOp->p5==2 ); |
| 984 | testcase( pOp->p5==3 ); |
| 985 | testcase( pOp->p5==4 ); |
| 986 | zType = azType[pOp->p5-1]; |
| 987 | }else{ |
| 988 | zType = 0; |
| 989 | } |
drh | 4308e34 | 2013-11-11 16:55:52 +0000 | [diff] [blame] | 990 | assert( zType!=0 || pOp->p4.z!=0 ); |
drh | f9c8ce3 | 2013-11-05 13:33:55 +0000 | [diff] [blame] | 991 | zLogFmt = "abort at %d in [%s]: %s"; |
| 992 | if( zType && pOp->p4.z ){ |
drh | 22c17b8 | 2015-05-15 04:13:15 +0000 | [diff] [blame] | 993 | sqlite3VdbeError(p, "%s constraint failed: %s", zType, pOp->p4.z); |
drh | f9c8ce3 | 2013-11-05 13:33:55 +0000 | [diff] [blame] | 994 | }else if( pOp->p4.z ){ |
drh | 22c17b8 | 2015-05-15 04:13:15 +0000 | [diff] [blame] | 995 | sqlite3VdbeError(p, "%s", pOp->p4.z); |
drh | f9c8ce3 | 2013-11-05 13:33:55 +0000 | [diff] [blame] | 996 | }else{ |
drh | 22c17b8 | 2015-05-15 04:13:15 +0000 | [diff] [blame] | 997 | sqlite3VdbeError(p, "%s constraint failed", zType); |
drh | f9c8ce3 | 2013-11-05 13:33:55 +0000 | [diff] [blame] | 998 | } |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 999 | sqlite3_log(pOp->p1, zLogFmt, pcx, p->zSql, p->zErrMsg); |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 1000 | } |
drh | 92f02c3 | 2004-09-02 14:57:08 +0000 | [diff] [blame] | 1001 | rc = sqlite3VdbeHalt(p); |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 1002 | assert( rc==SQLITE_BUSY || rc==SQLITE_OK || rc==SQLITE_ERROR ); |
drh | 92f02c3 | 2004-09-02 14:57:08 +0000 | [diff] [blame] | 1003 | if( rc==SQLITE_BUSY ){ |
drh | 900b31e | 2007-08-28 02:27:51 +0000 | [diff] [blame] | 1004 | p->rc = rc = SQLITE_BUSY; |
| 1005 | }else{ |
drh | d91c1a1 | 2013-02-09 13:58:25 +0000 | [diff] [blame] | 1006 | assert( rc==SQLITE_OK || (p->rc&0xff)==SQLITE_CONSTRAINT ); |
dan | cb3e4b7 | 2013-07-03 19:53:05 +0000 | [diff] [blame] | 1007 | assert( rc==SQLITE_OK || db->nDeferredCons>0 || db->nDeferredImmCons>0 ); |
drh | 900b31e | 2007-08-28 02:27:51 +0000 | [diff] [blame] | 1008 | rc = p->rc ? SQLITE_ERROR : SQLITE_DONE; |
drh | 92f02c3 | 2004-09-02 14:57:08 +0000 | [diff] [blame] | 1009 | } |
drh | 900b31e | 2007-08-28 02:27:51 +0000 | [diff] [blame] | 1010 | goto vdbe_return; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1011 | } |
drh | c61053b | 2000-06-04 12:58:36 +0000 | [diff] [blame] | 1012 | |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 1013 | /* Opcode: Integer P1 P2 * * * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 1014 | ** Synopsis: r[P2]=P1 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1015 | ** |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1016 | ** The 32-bit integer value P1 is written into register P2. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1017 | */ |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 1018 | case OP_Integer: { /* out2 */ |
| 1019 | pOut = out2Prerelease(p, pOp); |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 1020 | pOut->u.i = pOp->p1; |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 1021 | break; |
| 1022 | } |
| 1023 | |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 1024 | /* Opcode: Int64 * P2 * P4 * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 1025 | ** Synopsis: r[P2]=P4 |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 1026 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 1027 | ** P4 is a pointer to a 64-bit integer value. |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1028 | ** Write that value into register P2. |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 1029 | */ |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 1030 | case OP_Int64: { /* out2 */ |
| 1031 | pOut = out2Prerelease(p, pOp); |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 1032 | assert( pOp->p4.pI64!=0 ); |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 1033 | pOut->u.i = *pOp->p4.pI64; |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 1034 | break; |
| 1035 | } |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 1036 | |
drh | 13573c7 | 2010-01-12 17:04:07 +0000 | [diff] [blame] | 1037 | #ifndef SQLITE_OMIT_FLOATING_POINT |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 1038 | /* Opcode: Real * P2 * P4 * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 1039 | ** Synopsis: r[P2]=P4 |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 1040 | ** |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 1041 | ** P4 is a pointer to a 64-bit floating point value. |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1042 | ** Write that value into register P2. |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 1043 | */ |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 1044 | case OP_Real: { /* same as TK_FLOAT, out2 */ |
| 1045 | pOut = out2Prerelease(p, pOp); |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 1046 | pOut->flags = MEM_Real; |
drh | 2eaf93d | 2008-04-29 00:15:20 +0000 | [diff] [blame] | 1047 | assert( !sqlite3IsNaN(*pOp->p4.pReal) ); |
drh | 74eaba4 | 2014-09-18 17:52:15 +0000 | [diff] [blame] | 1048 | pOut->u.r = *pOp->p4.pReal; |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 1049 | break; |
| 1050 | } |
drh | 13573c7 | 2010-01-12 17:04:07 +0000 | [diff] [blame] | 1051 | #endif |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 1052 | |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 1053 | /* Opcode: String8 * P2 * P4 * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 1054 | ** Synopsis: r[P2]='P4' |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 1055 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 1056 | ** P4 points to a nul terminated UTF-8 string. This opcode is transformed |
drh | f07cf6e | 2015-03-06 16:45:16 +0000 | [diff] [blame] | 1057 | ** into a String opcode before it is executed for the first time. During |
drh | 0fd6135 | 2014-02-07 02:29:45 +0000 | [diff] [blame] | 1058 | ** this transformation, the length of string P4 is computed and stored |
| 1059 | ** as the P1 parameter. |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 1060 | */ |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 1061 | case OP_String8: { /* same as TK_STRING, out2 */ |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 1062 | assert( pOp->p4.z!=0 ); |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 1063 | pOut = out2Prerelease(p, pOp); |
drh | ed2df7f | 2005-11-16 04:34:32 +0000 | [diff] [blame] | 1064 | pOp->opcode = OP_String; |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 1065 | pOp->p1 = sqlite3Strlen30(pOp->p4.z); |
drh | ed2df7f | 2005-11-16 04:34:32 +0000 | [diff] [blame] | 1066 | |
| 1067 | #ifndef SQLITE_OMIT_UTF16 |
drh | 8079a0d | 2006-01-12 17:20:50 +0000 | [diff] [blame] | 1068 | if( encoding!=SQLITE_UTF8 ){ |
drh | 3a9cf17 | 2009-06-17 21:42:33 +0000 | [diff] [blame] | 1069 | rc = sqlite3VdbeMemSetStr(pOut, pOp->p4.z, -1, SQLITE_UTF8, SQLITE_STATIC); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 1070 | if( rc ){ |
| 1071 | assert( rc==SQLITE_TOOBIG ); /* This is the only possible error here */ |
| 1072 | goto too_big; |
| 1073 | } |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 1074 | if( SQLITE_OK!=sqlite3VdbeChangeEncoding(pOut, encoding) ) goto no_mem; |
drh | 17bcb10 | 2014-09-18 21:25:33 +0000 | [diff] [blame] | 1075 | assert( pOut->szMalloc>0 && pOut->zMalloc==pOut->z ); |
drh | c91b2fd | 2014-03-01 18:13:23 +0000 | [diff] [blame] | 1076 | assert( VdbeMemDynamic(pOut)==0 ); |
drh | 17bcb10 | 2014-09-18 21:25:33 +0000 | [diff] [blame] | 1077 | pOut->szMalloc = 0; |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 1078 | pOut->flags |= MEM_Static; |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 1079 | if( pOp->p4type==P4_DYNAMIC ){ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 1080 | sqlite3DbFree(db, pOp->p4.z); |
danielk1977 | e004840 | 2004-06-15 16:51:01 +0000 | [diff] [blame] | 1081 | } |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 1082 | pOp->p4type = P4_DYNAMIC; |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 1083 | pOp->p4.z = pOut->z; |
| 1084 | pOp->p1 = pOut->n; |
danielk1977 | 0f69c1e | 2004-05-29 11:24:50 +0000 | [diff] [blame] | 1085 | } |
danielk1977 | 93758c8 | 2005-01-21 08:13:14 +0000 | [diff] [blame] | 1086 | #endif |
drh | bb4957f | 2008-03-20 14:03:29 +0000 | [diff] [blame] | 1087 | if( pOp->p1>db->aLimit[SQLITE_LIMIT_LENGTH] ){ |
drh | cbd2da9 | 2007-12-17 16:20:06 +0000 | [diff] [blame] | 1088 | goto too_big; |
| 1089 | } |
| 1090 | /* Fall through to the next case, OP_String */ |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 1091 | } |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 1092 | |
drh | f07cf6e | 2015-03-06 16:45:16 +0000 | [diff] [blame] | 1093 | /* Opcode: String P1 P2 P3 P4 P5 |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 1094 | ** Synopsis: r[P2]='P4' (len=P1) |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 1095 | ** |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1096 | ** The string value P4 of length P1 (bytes) is stored in register P2. |
drh | f07cf6e | 2015-03-06 16:45:16 +0000 | [diff] [blame] | 1097 | ** |
| 1098 | ** If P5!=0 and the content of register P3 is greater than zero, then |
drh | a9c18a9 | 2015-03-06 20:49:52 +0000 | [diff] [blame] | 1099 | ** the datatype of the register P2 is converted to BLOB. The content is |
| 1100 | ** the same sequence of bytes, it is merely interpreted as a BLOB instead |
| 1101 | ** of a string, as if it had been CAST. |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 1102 | */ |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 1103 | case OP_String: { /* out2 */ |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 1104 | assert( pOp->p4.z!=0 ); |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 1105 | pOut = out2Prerelease(p, pOp); |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 1106 | pOut->flags = MEM_Str|MEM_Static|MEM_Term; |
| 1107 | pOut->z = pOp->p4.z; |
| 1108 | pOut->n = pOp->p1; |
| 1109 | pOut->enc = encoding; |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 1110 | UPDATE_MAX_BLOBSIZE(pOut); |
drh | 41d2e66 | 2015-12-01 21:23:07 +0000 | [diff] [blame] | 1111 | #ifndef SQLITE_LIKE_DOESNT_MATCH_BLOBS |
drh | f07cf6e | 2015-03-06 16:45:16 +0000 | [diff] [blame] | 1112 | if( pOp->p5 ){ |
| 1113 | assert( pOp->p3>0 ); |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 1114 | assert( pOp->p3<=(p->nMem+1 - p->nCursor) ); |
drh | f07cf6e | 2015-03-06 16:45:16 +0000 | [diff] [blame] | 1115 | pIn3 = &aMem[pOp->p3]; |
| 1116 | assert( pIn3->flags & MEM_Int ); |
| 1117 | if( pIn3->u.i ) pOut->flags = MEM_Blob|MEM_Static|MEM_Term; |
| 1118 | } |
drh | 41d2e66 | 2015-12-01 21:23:07 +0000 | [diff] [blame] | 1119 | #endif |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 1120 | break; |
| 1121 | } |
| 1122 | |
drh | 053a128 | 2012-09-19 21:15:46 +0000 | [diff] [blame] | 1123 | /* Opcode: Null P1 P2 P3 * * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 1124 | ** Synopsis: r[P2..P3]=NULL |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 1125 | ** |
drh | b8475df | 2011-12-09 16:21:19 +0000 | [diff] [blame] | 1126 | ** 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] | 1127 | ** 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] | 1128 | ** is less than P2 (typically P3 is zero) then only register P2 is |
drh | 053a128 | 2012-09-19 21:15:46 +0000 | [diff] [blame] | 1129 | ** set to NULL. |
| 1130 | ** |
| 1131 | ** If the P1 value is non-zero, then also set the MEM_Cleared flag so that |
| 1132 | ** NULL values will not compare equal even if SQLITE_NULLEQ is set on |
| 1133 | ** OP_Ne or OP_Eq. |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 1134 | */ |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 1135 | case OP_Null: { /* out2 */ |
drh | b8475df | 2011-12-09 16:21:19 +0000 | [diff] [blame] | 1136 | int cnt; |
drh | 053a128 | 2012-09-19 21:15:46 +0000 | [diff] [blame] | 1137 | u16 nullFlag; |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 1138 | pOut = out2Prerelease(p, pOp); |
drh | b8475df | 2011-12-09 16:21:19 +0000 | [diff] [blame] | 1139 | cnt = pOp->p3-pOp->p2; |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 1140 | assert( pOp->p3<=(p->nMem+1 - p->nCursor) ); |
drh | 053a128 | 2012-09-19 21:15:46 +0000 | [diff] [blame] | 1141 | pOut->flags = nullFlag = pOp->p1 ? (MEM_Null|MEM_Cleared) : MEM_Null; |
drh | b8475df | 2011-12-09 16:21:19 +0000 | [diff] [blame] | 1142 | while( cnt>0 ){ |
| 1143 | pOut++; |
| 1144 | memAboutToChange(p, pOut); |
drh | 0725cab | 2014-09-17 14:52:46 +0000 | [diff] [blame] | 1145 | sqlite3VdbeMemSetNull(pOut); |
drh | 053a128 | 2012-09-19 21:15:46 +0000 | [diff] [blame] | 1146 | pOut->flags = nullFlag; |
drh | b8475df | 2011-12-09 16:21:19 +0000 | [diff] [blame] | 1147 | cnt--; |
| 1148 | } |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 1149 | break; |
| 1150 | } |
| 1151 | |
drh | 05a86c5 | 2014-02-16 01:55:49 +0000 | [diff] [blame] | 1152 | /* Opcode: SoftNull P1 * * * * |
| 1153 | ** Synopsis: r[P1]=NULL |
| 1154 | ** |
| 1155 | ** Set register P1 to have the value NULL as seen by the OP_MakeRecord |
| 1156 | ** instruction, but do not free any string or blob memory associated with |
| 1157 | ** the register, so that if the value was a string or blob that was |
| 1158 | ** previously copied using OP_SCopy, the copies will continue to be valid. |
| 1159 | */ |
| 1160 | case OP_SoftNull: { |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 1161 | assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) ); |
drh | 05a86c5 | 2014-02-16 01:55:49 +0000 | [diff] [blame] | 1162 | pOut = &aMem[pOp->p1]; |
| 1163 | pOut->flags = (pOut->flags|MEM_Null)&~MEM_Undefined; |
| 1164 | break; |
| 1165 | } |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 1166 | |
drh | a5750cf | 2014-02-07 13:20:31 +0000 | [diff] [blame] | 1167 | /* Opcode: Blob P1 P2 * P4 * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 1168 | ** Synopsis: r[P2]=P4 (len=P1) |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 1169 | ** |
drh | 9de221d | 2008-01-05 06:51:30 +0000 | [diff] [blame] | 1170 | ** P4 points to a blob of data P1 bytes long. Store this |
drh | 710c484 | 2010-08-30 01:17:20 +0000 | [diff] [blame] | 1171 | ** blob in register P2. |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 1172 | */ |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 1173 | case OP_Blob: { /* out2 */ |
drh | cbd2da9 | 2007-12-17 16:20:06 +0000 | [diff] [blame] | 1174 | assert( pOp->p1 <= SQLITE_MAX_LENGTH ); |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 1175 | pOut = out2Prerelease(p, pOp); |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 1176 | sqlite3VdbeMemSetStr(pOut, pOp->p4.z, pOp->p1, 0, 0); |
drh | 9de221d | 2008-01-05 06:51:30 +0000 | [diff] [blame] | 1177 | pOut->enc = encoding; |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 1178 | UPDATE_MAX_BLOBSIZE(pOut); |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 1179 | break; |
| 1180 | } |
| 1181 | |
drh | eaf52d8 | 2010-05-12 13:50:23 +0000 | [diff] [blame] | 1182 | /* Opcode: Variable P1 P2 * P4 * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 1183 | ** Synopsis: r[P2]=parameter(P1,P4) |
drh | 5045789 | 2003-09-06 01:10:47 +0000 | [diff] [blame] | 1184 | ** |
drh | eaf52d8 | 2010-05-12 13:50:23 +0000 | [diff] [blame] | 1185 | ** Transfer the values of bound parameter P1 into register P2 |
drh | 08de149 | 2009-02-20 03:55:05 +0000 | [diff] [blame] | 1186 | ** |
drh | 0fd6135 | 2014-02-07 02:29:45 +0000 | [diff] [blame] | 1187 | ** If the parameter is named, then its name appears in P4. |
drh | 08de149 | 2009-02-20 03:55:05 +0000 | [diff] [blame] | 1188 | ** The P4 value is used by sqlite3_bind_parameter_name(). |
drh | 5045789 | 2003-09-06 01:10:47 +0000 | [diff] [blame] | 1189 | */ |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 1190 | case OP_Variable: { /* out2 */ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1191 | Mem *pVar; /* Value being transferred */ |
| 1192 | |
drh | eaf52d8 | 2010-05-12 13:50:23 +0000 | [diff] [blame] | 1193 | assert( pOp->p1>0 && pOp->p1<=p->nVar ); |
drh | 04e9eea | 2011-06-01 19:16:06 +0000 | [diff] [blame] | 1194 | assert( pOp->p4.z==0 || pOp->p4.z==p->azVar[pOp->p1-1] ); |
drh | eaf52d8 | 2010-05-12 13:50:23 +0000 | [diff] [blame] | 1195 | pVar = &p->aVar[pOp->p1 - 1]; |
| 1196 | if( sqlite3VdbeMemTooBig(pVar) ){ |
| 1197 | goto too_big; |
drh | 023ae03 | 2007-05-08 12:12:16 +0000 | [diff] [blame] | 1198 | } |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 1199 | pOut = out2Prerelease(p, pOp); |
drh | eaf52d8 | 2010-05-12 13:50:23 +0000 | [diff] [blame] | 1200 | sqlite3VdbeMemShallowCopy(pOut, pVar, MEM_Static); |
| 1201 | UPDATE_MAX_BLOBSIZE(pOut); |
danielk1977 | 93d4675 | 2004-05-23 13:30:58 +0000 | [diff] [blame] | 1202 | break; |
| 1203 | } |
danielk1977 | 295ba55 | 2004-05-19 10:34:51 +0000 | [diff] [blame] | 1204 | |
drh | b21e7c7 | 2008-06-22 12:37:57 +0000 | [diff] [blame] | 1205 | /* Opcode: Move P1 P2 P3 * * |
drh | f63552b | 2013-10-30 00:25:03 +0000 | [diff] [blame] | 1206 | ** Synopsis: r[P2@P3]=r[P1@P3] |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1207 | ** |
drh | 079a307 | 2014-03-19 14:10:55 +0000 | [diff] [blame] | 1208 | ** Move the P3 values in register P1..P1+P3-1 over into |
| 1209 | ** registers P2..P2+P3-1. Registers P1..P1+P3-1 are |
drh | b21e7c7 | 2008-06-22 12:37:57 +0000 | [diff] [blame] | 1210 | ** left holding a NULL. It is an error for register ranges |
drh | 079a307 | 2014-03-19 14:10:55 +0000 | [diff] [blame] | 1211 | ** P1..P1+P3-1 and P2..P2+P3-1 to overlap. It is an error |
| 1212 | ** for P3 to be less than 1. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1213 | */ |
drh | e1349cb | 2008-04-01 00:36:10 +0000 | [diff] [blame] | 1214 | case OP_Move: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1215 | int n; /* Number of registers left to copy */ |
| 1216 | int p1; /* Register to copy from */ |
| 1217 | int p2; /* Register to copy to */ |
| 1218 | |
drh | e09f43f | 2013-11-21 04:18:31 +0000 | [diff] [blame] | 1219 | n = pOp->p3; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1220 | p1 = pOp->p1; |
| 1221 | p2 = pOp->p2; |
drh | 079a307 | 2014-03-19 14:10:55 +0000 | [diff] [blame] | 1222 | assert( n>0 && p1>0 && p2>0 ); |
drh | b21e7c7 | 2008-06-22 12:37:57 +0000 | [diff] [blame] | 1223 | assert( p1+n<=p2 || p2+n<=p1 ); |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1224 | |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 1225 | pIn1 = &aMem[p1]; |
| 1226 | pOut = &aMem[p2]; |
drh | e09f43f | 2013-11-21 04:18:31 +0000 | [diff] [blame] | 1227 | do{ |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 1228 | assert( pOut<=&aMem[(p->nMem+1 - p->nCursor)] ); |
| 1229 | assert( pIn1<=&aMem[(p->nMem+1 - p->nCursor)] ); |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 1230 | assert( memIsValid(pIn1) ); |
| 1231 | memAboutToChange(p, pOut); |
drh | 17bcb10 | 2014-09-18 21:25:33 +0000 | [diff] [blame] | 1232 | sqlite3VdbeMemMove(pOut, pIn1); |
drh | 52043d7 | 2011-08-03 16:40:15 +0000 | [diff] [blame] | 1233 | #ifdef SQLITE_DEBUG |
drh | bd6789e | 2015-04-28 14:00:02 +0000 | [diff] [blame] | 1234 | if( pOut->pScopyFrom>=&aMem[p1] && pOut->pScopyFrom<pOut ){ |
drh | 5fb7125 | 2015-04-28 12:44:55 +0000 | [diff] [blame] | 1235 | pOut->pScopyFrom += pOp->p2 - p1; |
drh | 52043d7 | 2011-08-03 16:40:15 +0000 | [diff] [blame] | 1236 | } |
| 1237 | #endif |
drh | bd6789e | 2015-04-28 14:00:02 +0000 | [diff] [blame] | 1238 | Deephemeralize(pOut); |
drh | b21e7c7 | 2008-06-22 12:37:57 +0000 | [diff] [blame] | 1239 | REGISTER_TRACE(p2++, pOut); |
| 1240 | pIn1++; |
| 1241 | pOut++; |
drh | 079a307 | 2014-03-19 14:10:55 +0000 | [diff] [blame] | 1242 | }while( --n ); |
drh | e1349cb | 2008-04-01 00:36:10 +0000 | [diff] [blame] | 1243 | break; |
| 1244 | } |
| 1245 | |
drh | e8e4af7 | 2012-09-21 00:04:28 +0000 | [diff] [blame] | 1246 | /* Opcode: Copy P1 P2 P3 * * |
drh | 4eded60 | 2013-12-20 15:59:20 +0000 | [diff] [blame] | 1247 | ** Synopsis: r[P2@P3+1]=r[P1@P3+1] |
drh | b1fdb2a | 2008-01-05 04:06:03 +0000 | [diff] [blame] | 1248 | ** |
drh | e8e4af7 | 2012-09-21 00:04:28 +0000 | [diff] [blame] | 1249 | ** Make a copy of registers P1..P1+P3 into registers P2..P2+P3. |
drh | b1fdb2a | 2008-01-05 04:06:03 +0000 | [diff] [blame] | 1250 | ** |
| 1251 | ** This instruction makes a deep copy of the value. A duplicate |
| 1252 | ** is made of any string or blob constant. See also OP_SCopy. |
| 1253 | */ |
drh | e8e4af7 | 2012-09-21 00:04:28 +0000 | [diff] [blame] | 1254 | case OP_Copy: { |
| 1255 | int n; |
| 1256 | |
| 1257 | n = pOp->p3; |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 1258 | pIn1 = &aMem[pOp->p1]; |
| 1259 | pOut = &aMem[pOp->p2]; |
drh | e1349cb | 2008-04-01 00:36:10 +0000 | [diff] [blame] | 1260 | assert( pOut!=pIn1 ); |
drh | e8e4af7 | 2012-09-21 00:04:28 +0000 | [diff] [blame] | 1261 | while( 1 ){ |
| 1262 | sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem); |
| 1263 | Deephemeralize(pOut); |
drh | 953f761 | 2012-12-07 22:18:54 +0000 | [diff] [blame] | 1264 | #ifdef SQLITE_DEBUG |
| 1265 | pOut->pScopyFrom = 0; |
| 1266 | #endif |
drh | e8e4af7 | 2012-09-21 00:04:28 +0000 | [diff] [blame] | 1267 | REGISTER_TRACE(pOp->p2+pOp->p3-n, pOut); |
| 1268 | if( (n--)==0 ) break; |
| 1269 | pOut++; |
| 1270 | pIn1++; |
| 1271 | } |
drh | e1349cb | 2008-04-01 00:36:10 +0000 | [diff] [blame] | 1272 | break; |
| 1273 | } |
| 1274 | |
drh | b1fdb2a | 2008-01-05 04:06:03 +0000 | [diff] [blame] | 1275 | /* Opcode: SCopy P1 P2 * * * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 1276 | ** Synopsis: r[P2]=r[P1] |
drh | b1fdb2a | 2008-01-05 04:06:03 +0000 | [diff] [blame] | 1277 | ** |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1278 | ** Make a shallow copy of register P1 into register P2. |
drh | b1fdb2a | 2008-01-05 04:06:03 +0000 | [diff] [blame] | 1279 | ** |
| 1280 | ** This instruction makes a shallow copy of the value. If the value |
| 1281 | ** is a string or blob, then the copy is only a pointer to the |
| 1282 | ** original and hence if the original changes so will the copy. |
| 1283 | ** Worse, if the original is deallocated, the copy becomes invalid. |
| 1284 | ** Thus the program must guarantee that the original will not change |
| 1285 | ** during the lifetime of the copy. Use OP_Copy to make a complete |
| 1286 | ** copy. |
| 1287 | */ |
drh | 26198bb | 2013-10-31 11:15:09 +0000 | [diff] [blame] | 1288 | case OP_SCopy: { /* out2 */ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 1289 | pIn1 = &aMem[pOp->p1]; |
| 1290 | pOut = &aMem[pOp->p2]; |
drh | 2d401ab | 2008-01-10 23:50:11 +0000 | [diff] [blame] | 1291 | assert( pOut!=pIn1 ); |
drh | e1349cb | 2008-04-01 00:36:10 +0000 | [diff] [blame] | 1292 | sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem); |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 1293 | #ifdef SQLITE_DEBUG |
| 1294 | if( pOut->pScopyFrom==0 ) pOut->pScopyFrom = pIn1; |
| 1295 | #endif |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1296 | break; |
| 1297 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1298 | |
drh | fed7ac6 | 2015-10-15 18:04:59 +0000 | [diff] [blame] | 1299 | /* Opcode: IntCopy P1 P2 * * * |
| 1300 | ** Synopsis: r[P2]=r[P1] |
| 1301 | ** |
| 1302 | ** Transfer the integer value held in register P1 into register P2. |
| 1303 | ** |
| 1304 | ** This is an optimized version of SCopy that works only for integer |
| 1305 | ** values. |
| 1306 | */ |
| 1307 | case OP_IntCopy: { /* out2 */ |
| 1308 | pIn1 = &aMem[pOp->p1]; |
| 1309 | assert( (pIn1->flags & MEM_Int)!=0 ); |
| 1310 | pOut = &aMem[pOp->p2]; |
| 1311 | sqlite3VdbeMemSetInt64(pOut, pIn1->u.i); |
| 1312 | break; |
| 1313 | } |
| 1314 | |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1315 | /* Opcode: ResultRow P1 P2 * * * |
drh | 4af5bee | 2013-10-30 02:37:50 +0000 | [diff] [blame] | 1316 | ** Synopsis: output=r[P1@P2] |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 1317 | ** |
shane | 21e7feb | 2008-05-30 15:59:49 +0000 | [diff] [blame] | 1318 | ** The registers P1 through P1+P2-1 contain a single row of |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 1319 | ** results. This opcode causes the sqlite3_step() call to terminate |
| 1320 | ** with an SQLITE_ROW return code and it sets up the sqlite3_stmt |
drh | 4d87aae | 2014-02-20 19:42:00 +0000 | [diff] [blame] | 1321 | ** 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] | 1322 | ** the result row. |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 1323 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1324 | case OP_ResultRow: { |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 1325 | Mem *pMem; |
| 1326 | int i; |
| 1327 | assert( p->nResColumn==pOp->p2 ); |
drh | 0a07c10 | 2008-01-03 18:03:08 +0000 | [diff] [blame] | 1328 | assert( pOp->p1>0 ); |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 1329 | assert( pOp->p1+pOp->p2<=(p->nMem+1 - p->nCursor)+1 ); |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 1330 | |
drh | e6400b9 | 2013-11-13 23:48:46 +0000 | [diff] [blame] | 1331 | #ifndef SQLITE_OMIT_PROGRESS_CALLBACK |
| 1332 | /* Run the progress counter just before returning. |
| 1333 | */ |
| 1334 | if( db->xProgress!=0 |
| 1335 | && nVmStep>=nProgressLimit |
| 1336 | && db->xProgress(db->pProgressArg)!=0 |
| 1337 | ){ |
| 1338 | rc = SQLITE_INTERRUPT; |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 1339 | goto abort_due_to_error; |
drh | e6400b9 | 2013-11-13 23:48:46 +0000 | [diff] [blame] | 1340 | } |
| 1341 | #endif |
| 1342 | |
dan | 32b09f2 | 2009-09-23 17:29:59 +0000 | [diff] [blame] | 1343 | /* If this statement has violated immediate foreign key constraints, do |
| 1344 | ** not return the number of rows modified. And do not RELEASE the statement |
| 1345 | ** transaction. It needs to be rolled back. */ |
| 1346 | if( SQLITE_OK!=(rc = sqlite3VdbeCheckFk(p, 0)) ){ |
| 1347 | assert( db->flags&SQLITE_CountRows ); |
| 1348 | assert( p->usesStmtJournal ); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 1349 | goto abort_due_to_error; |
dan | 32b09f2 | 2009-09-23 17:29:59 +0000 | [diff] [blame] | 1350 | } |
| 1351 | |
danielk1977 | bd43455 | 2009-03-18 10:33:00 +0000 | [diff] [blame] | 1352 | /* If the SQLITE_CountRows flag is set in sqlite3.flags mask, then |
| 1353 | ** DML statements invoke this opcode to return the number of rows |
| 1354 | ** modified to the user. This is the only way that a VM that |
| 1355 | ** opens a statement transaction may invoke this opcode. |
| 1356 | ** |
| 1357 | ** In case this is such a statement, close any statement transaction |
| 1358 | ** opened by this VM before returning control to the user. This is to |
| 1359 | ** ensure that statement-transactions are always nested, not overlapping. |
| 1360 | ** If the open statement-transaction is not closed here, then the user |
| 1361 | ** may step another VM that opens its own statement transaction. This |
| 1362 | ** may lead to overlapping statement transactions. |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 1363 | ** |
| 1364 | ** The statement transaction is never a top-level transaction. Hence |
| 1365 | ** the RELEASE call below can never fail. |
danielk1977 | bd43455 | 2009-03-18 10:33:00 +0000 | [diff] [blame] | 1366 | */ |
| 1367 | assert( p->iStatement==0 || db->flags&SQLITE_CountRows ); |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 1368 | rc = sqlite3VdbeCloseStatement(p, SAVEPOINT_RELEASE); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 1369 | assert( rc==SQLITE_OK ); |
danielk1977 | bd43455 | 2009-03-18 10:33:00 +0000 | [diff] [blame] | 1370 | |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 1371 | /* Invalidate all ephemeral cursor row caches */ |
| 1372 | p->cacheCtr = (p->cacheCtr + 2)|1; |
| 1373 | |
| 1374 | /* Make sure the results of the current row are \000 terminated |
shane | 21e7feb | 2008-05-30 15:59:49 +0000 | [diff] [blame] | 1375 | ** and have an assigned type. The results are de-ephemeralized as |
drh | b8a45bb | 2011-12-31 21:51:55 +0000 | [diff] [blame] | 1376 | ** a side effect. |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 1377 | */ |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 1378 | pMem = p->pResultSet = &aMem[pOp->p1]; |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 1379 | for(i=0; i<pOp->p2; i++){ |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 1380 | assert( memIsValid(&pMem[i]) ); |
drh | ebc1671 | 2010-09-28 00:25:58 +0000 | [diff] [blame] | 1381 | Deephemeralize(&pMem[i]); |
drh | 746fd9c | 2010-09-28 06:00:47 +0000 | [diff] [blame] | 1382 | assert( (pMem[i].flags & MEM_Ephem)==0 |
| 1383 | || (pMem[i].flags & (MEM_Str|MEM_Blob))==0 ); |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 1384 | sqlite3VdbeMemNulTerminate(&pMem[i]); |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 1385 | REGISTER_TRACE(pOp->p1+i, &pMem[i]); |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 1386 | } |
drh | 2803969 | 2008-03-17 16:54:01 +0000 | [diff] [blame] | 1387 | if( db->mallocFailed ) goto no_mem; |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 1388 | |
| 1389 | /* Return SQLITE_ROW |
| 1390 | */ |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 1391 | p->pc = (int)(pOp - aOp) + 1; |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 1392 | rc = SQLITE_ROW; |
| 1393 | goto vdbe_return; |
| 1394 | } |
| 1395 | |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1396 | /* Opcode: Concat P1 P2 P3 * * |
drh | 313619f | 2013-10-31 20:34:06 +0000 | [diff] [blame] | 1397 | ** Synopsis: r[P3]=r[P2]+r[P1] |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1398 | ** |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1399 | ** Add the text in register P1 onto the end of the text in |
| 1400 | ** register P2 and store the result in register P3. |
| 1401 | ** 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] | 1402 | ** |
| 1403 | ** P3 = P2 || P1 |
| 1404 | ** |
| 1405 | ** It is illegal for P1 and P3 to be the same register. Sometimes, |
| 1406 | ** if P3 is the same register as P2, the implementation is able |
| 1407 | ** to avoid a memcpy(). |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1408 | */ |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1409 | case OP_Concat: { /* same as TK_CONCAT, in1, in2, out3 */ |
drh | 023ae03 | 2007-05-08 12:12:16 +0000 | [diff] [blame] | 1410 | i64 nByte; |
danielk1977 | 8a6b541 | 2004-05-24 07:04:25 +0000 | [diff] [blame] | 1411 | |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 1412 | pIn1 = &aMem[pOp->p1]; |
| 1413 | pIn2 = &aMem[pOp->p2]; |
| 1414 | pOut = &aMem[pOp->p3]; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1415 | assert( pIn1!=pOut ); |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1416 | if( (pIn1->flags | pIn2->flags) & MEM_Null ){ |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1417 | sqlite3VdbeMemSetNull(pOut); |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1418 | break; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1419 | } |
drh | a0c0652 | 2009-06-17 22:50:41 +0000 | [diff] [blame] | 1420 | if( ExpandBlob(pIn1) || ExpandBlob(pIn2) ) goto no_mem; |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1421 | Stringify(pIn1, encoding); |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1422 | Stringify(pIn2, encoding); |
| 1423 | nByte = pIn1->n + pIn2->n; |
drh | bb4957f | 2008-03-20 14:03:29 +0000 | [diff] [blame] | 1424 | if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){ |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1425 | goto too_big; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1426 | } |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 1427 | if( sqlite3VdbeMemGrow(pOut, (int)nByte+2, pOut==pIn2) ){ |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1428 | goto no_mem; |
| 1429 | } |
drh | c91b2fd | 2014-03-01 18:13:23 +0000 | [diff] [blame] | 1430 | MemSetTypeFlag(pOut, MEM_Str); |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1431 | if( pOut!=pIn2 ){ |
| 1432 | memcpy(pOut->z, pIn2->z, pIn2->n); |
| 1433 | } |
| 1434 | memcpy(&pOut->z[pIn2->n], pIn1->z, pIn1->n); |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 1435 | pOut->z[nByte]=0; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1436 | pOut->z[nByte+1] = 0; |
| 1437 | pOut->flags |= MEM_Term; |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 1438 | pOut->n = (int)nByte; |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1439 | pOut->enc = encoding; |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 1440 | UPDATE_MAX_BLOBSIZE(pOut); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1441 | break; |
| 1442 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1443 | |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 1444 | /* Opcode: Add P1 P2 P3 * * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 1445 | ** Synopsis: r[P3]=r[P1]+r[P2] |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1446 | ** |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1447 | ** Add the value in register P1 to the value in register P2 |
shane | 21e7feb | 2008-05-30 15:59:49 +0000 | [diff] [blame] | 1448 | ** and store the result in register P3. |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1449 | ** If either input is NULL, the result is NULL. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1450 | */ |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 1451 | /* Opcode: Multiply P1 P2 P3 * * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 1452 | ** Synopsis: r[P3]=r[P1]*r[P2] |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1453 | ** |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 1454 | ** |
shane | 21e7feb | 2008-05-30 15:59:49 +0000 | [diff] [blame] | 1455 | ** Multiply the value in register P1 by the value in register P2 |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1456 | ** and store the result in register P3. |
| 1457 | ** If either input is NULL, the result is NULL. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1458 | */ |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 1459 | /* Opcode: Subtract P1 P2 P3 * * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 1460 | ** Synopsis: r[P3]=r[P2]-r[P1] |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1461 | ** |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1462 | ** Subtract the value in register P1 from the value in register P2 |
| 1463 | ** and store the result in register P3. |
| 1464 | ** If either input is NULL, the result is NULL. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1465 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1466 | /* Opcode: Divide P1 P2 P3 * * |
drh | 40864a1 | 2013-11-15 18:58:37 +0000 | [diff] [blame] | 1467 | ** Synopsis: r[P3]=r[P2]/r[P1] |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1468 | ** |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1469 | ** Divide the value in register P1 by the value in register P2 |
dan | e275dc3 | 2009-08-18 16:24:58 +0000 | [diff] [blame] | 1470 | ** and store the result in register P3 (P3=P2/P1). If the value in |
| 1471 | ** register P1 is zero, then the result is NULL. If either input is |
| 1472 | ** NULL, the result is NULL. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1473 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1474 | /* Opcode: Remainder P1 P2 P3 * * |
drh | 40864a1 | 2013-11-15 18:58:37 +0000 | [diff] [blame] | 1475 | ** Synopsis: r[P3]=r[P2]%r[P1] |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1476 | ** |
drh | 40864a1 | 2013-11-15 18:58:37 +0000 | [diff] [blame] | 1477 | ** Compute the remainder after integer register P2 is divided by |
| 1478 | ** register P1 and store the result in register P3. |
| 1479 | ** If the value in register P1 is zero the result is NULL. |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1480 | ** If either operand is NULL, the result is NULL. |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1481 | */ |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1482 | case OP_Add: /* same as TK_PLUS, in1, in2, out3 */ |
| 1483 | case OP_Subtract: /* same as TK_MINUS, in1, in2, out3 */ |
| 1484 | case OP_Multiply: /* same as TK_STAR, in1, in2, out3 */ |
| 1485 | case OP_Divide: /* same as TK_SLASH, in1, in2, out3 */ |
| 1486 | case OP_Remainder: { /* same as TK_REM, in1, in2, out3 */ |
drh | be707b3 | 2012-12-10 22:19:14 +0000 | [diff] [blame] | 1487 | char bIntint; /* Started out as two integer operands */ |
drh | 3d1d90a | 2014-03-24 15:00:15 +0000 | [diff] [blame] | 1488 | u16 flags; /* Combined MEM_* flags from both inputs */ |
| 1489 | u16 type1; /* Numeric type of left operand */ |
| 1490 | u16 type2; /* Numeric type of right operand */ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1491 | i64 iA; /* Integer value of left operand */ |
| 1492 | i64 iB; /* Integer value of right operand */ |
| 1493 | double rA; /* Real value of left operand */ |
| 1494 | double rB; /* Real value of right operand */ |
| 1495 | |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 1496 | pIn1 = &aMem[pOp->p1]; |
drh | 3d1d90a | 2014-03-24 15:00:15 +0000 | [diff] [blame] | 1497 | type1 = numericType(pIn1); |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 1498 | pIn2 = &aMem[pOp->p2]; |
drh | 3d1d90a | 2014-03-24 15:00:15 +0000 | [diff] [blame] | 1499 | type2 = numericType(pIn2); |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 1500 | pOut = &aMem[pOp->p3]; |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1501 | flags = pIn1->flags | pIn2->flags; |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 1502 | if( (flags & MEM_Null)!=0 ) goto arithmetic_result_is_null; |
drh | 3d1d90a | 2014-03-24 15:00:15 +0000 | [diff] [blame] | 1503 | if( (type1 & type2 & MEM_Int)!=0 ){ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1504 | iA = pIn1->u.i; |
| 1505 | iB = pIn2->u.i; |
drh | be707b3 | 2012-12-10 22:19:14 +0000 | [diff] [blame] | 1506 | bIntint = 1; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1507 | switch( pOp->opcode ){ |
drh | 158b9cb | 2011-03-05 20:59:46 +0000 | [diff] [blame] | 1508 | case OP_Add: if( sqlite3AddInt64(&iB,iA) ) goto fp_math; break; |
| 1509 | case OP_Subtract: if( sqlite3SubInt64(&iB,iA) ) goto fp_math; break; |
| 1510 | case OP_Multiply: if( sqlite3MulInt64(&iB,iA) ) goto fp_math; break; |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1511 | case OP_Divide: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1512 | if( iA==0 ) goto arithmetic_result_is_null; |
drh | 158b9cb | 2011-03-05 20:59:46 +0000 | [diff] [blame] | 1513 | if( iA==-1 && iB==SMALLEST_INT64 ) goto fp_math; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1514 | iB /= iA; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1515 | break; |
| 1516 | } |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1517 | default: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1518 | if( iA==0 ) goto arithmetic_result_is_null; |
| 1519 | if( iA==-1 ) iA = 1; |
| 1520 | iB %= iA; |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1521 | break; |
| 1522 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1523 | } |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1524 | pOut->u.i = iB; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1525 | MemSetTypeFlag(pOut, MEM_Int); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1526 | }else{ |
drh | be707b3 | 2012-12-10 22:19:14 +0000 | [diff] [blame] | 1527 | bIntint = 0; |
drh | 158b9cb | 2011-03-05 20:59:46 +0000 | [diff] [blame] | 1528 | fp_math: |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1529 | rA = sqlite3VdbeRealValue(pIn1); |
| 1530 | rB = sqlite3VdbeRealValue(pIn2); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1531 | switch( pOp->opcode ){ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1532 | case OP_Add: rB += rA; break; |
| 1533 | case OP_Subtract: rB -= rA; break; |
| 1534 | case OP_Multiply: rB *= rA; break; |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1535 | case OP_Divide: { |
shane | fbd60f8 | 2009-02-04 03:59:25 +0000 | [diff] [blame] | 1536 | /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1537 | if( rA==(double)0 ) goto arithmetic_result_is_null; |
| 1538 | rB /= rA; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1539 | break; |
| 1540 | } |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1541 | default: { |
shane | 75ac1de | 2009-06-09 18:58:52 +0000 | [diff] [blame] | 1542 | iA = (i64)rA; |
| 1543 | iB = (i64)rB; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1544 | if( iA==0 ) goto arithmetic_result_is_null; |
| 1545 | if( iA==-1 ) iA = 1; |
| 1546 | rB = (double)(iB % iA); |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1547 | break; |
| 1548 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1549 | } |
drh | c5a7b51 | 2010-01-13 16:25:42 +0000 | [diff] [blame] | 1550 | #ifdef SQLITE_OMIT_FLOATING_POINT |
| 1551 | pOut->u.i = rB; |
| 1552 | MemSetTypeFlag(pOut, MEM_Int); |
| 1553 | #else |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1554 | if( sqlite3IsNaN(rB) ){ |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 1555 | goto arithmetic_result_is_null; |
drh | 53c1402 | 2007-05-10 17:23:11 +0000 | [diff] [blame] | 1556 | } |
drh | 74eaba4 | 2014-09-18 17:52:15 +0000 | [diff] [blame] | 1557 | pOut->u.r = rB; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1558 | MemSetTypeFlag(pOut, MEM_Real); |
drh | 3d1d90a | 2014-03-24 15:00:15 +0000 | [diff] [blame] | 1559 | if( ((type1|type2)&MEM_Real)==0 && !bIntint ){ |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1560 | sqlite3VdbeIntegerAffinity(pOut); |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1561 | } |
drh | c5a7b51 | 2010-01-13 16:25:42 +0000 | [diff] [blame] | 1562 | #endif |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1563 | } |
| 1564 | break; |
| 1565 | |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 1566 | arithmetic_result_is_null: |
| 1567 | sqlite3VdbeMemSetNull(pOut); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1568 | break; |
| 1569 | } |
| 1570 | |
drh | 7a95789 | 2012-02-02 17:35:43 +0000 | [diff] [blame] | 1571 | /* Opcode: CollSeq P1 * * P4 |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 1572 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 1573 | ** P4 is a pointer to a CollSeq struct. If the next call to a user function |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 1574 | ** or aggregate calls sqlite3GetFuncCollSeq(), this collation sequence will |
| 1575 | ** be returned. This is used by the built-in min(), max() and nullif() |
drh | e6f85e7 | 2004-12-25 01:03:13 +0000 | [diff] [blame] | 1576 | ** functions. |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 1577 | ** |
drh | 7a95789 | 2012-02-02 17:35:43 +0000 | [diff] [blame] | 1578 | ** If P1 is not zero, then it is a register that a subsequent min() or |
| 1579 | ** max() aggregate will set to 1 if the current row is not the minimum or |
| 1580 | ** maximum. The P1 register is initialized to 0 by this instruction. |
| 1581 | ** |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 1582 | ** The interface used by the implementation of the aforementioned functions |
| 1583 | ** to retrieve the collation sequence set by this opcode is not available |
drh | 0a0d056 | 2015-03-12 05:08:34 +0000 | [diff] [blame] | 1584 | ** publicly. Only built-in functions have access to this feature. |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 1585 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1586 | case OP_CollSeq: { |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 1587 | assert( pOp->p4type==P4_COLLSEQ ); |
drh | 7a95789 | 2012-02-02 17:35:43 +0000 | [diff] [blame] | 1588 | if( pOp->p1 ){ |
| 1589 | sqlite3VdbeMemSetInt64(&aMem[pOp->p1], 0); |
| 1590 | } |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 1591 | break; |
| 1592 | } |
| 1593 | |
drh | 9c7c913 | 2015-06-26 18:16:52 +0000 | [diff] [blame] | 1594 | /* Opcode: Function0 P1 P2 P3 P4 P5 |
drh | f63552b | 2013-10-30 00:25:03 +0000 | [diff] [blame] | 1595 | ** Synopsis: r[P3]=func(r[P2@P5]) |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 1596 | ** |
drh | e2d9e7c | 2015-06-26 18:47:53 +0000 | [diff] [blame] | 1597 | ** Invoke a user function (P4 is a pointer to a FuncDef object that |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1598 | ** defines the function) with P5 arguments taken from register P2 and |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1599 | ** successors. The result of the function is stored in register P3. |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1600 | ** Register P3 must not be one of the function inputs. |
danielk1977 | 682f68b | 2004-06-05 10:22:17 +0000 | [diff] [blame] | 1601 | ** |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 1602 | ** P1 is a 32-bit bitmask indicating whether or not each argument to the |
danielk1977 | 682f68b | 2004-06-05 10:22:17 +0000 | [diff] [blame] | 1603 | ** function was determined to be constant at compile time. If the first |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 1604 | ** argument was constant then bit 0 of P1 is set. This is used to determine |
danielk1977 | 682f68b | 2004-06-05 10:22:17 +0000 | [diff] [blame] | 1605 | ** whether meta data associated with a user function argument using the |
| 1606 | ** sqlite3_set_auxdata() API may be safely retained until the next |
| 1607 | ** invocation of this opcode. |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 1608 | ** |
drh | 9c7c913 | 2015-06-26 18:16:52 +0000 | [diff] [blame] | 1609 | ** See also: Function, AggStep, AggFinal |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 1610 | */ |
drh | 9c7c913 | 2015-06-26 18:16:52 +0000 | [diff] [blame] | 1611 | /* Opcode: Function P1 P2 P3 P4 P5 |
| 1612 | ** Synopsis: r[P3]=func(r[P2@P5]) |
| 1613 | ** |
| 1614 | ** Invoke a user function (P4 is a pointer to an sqlite3_context object that |
| 1615 | ** contains a pointer to the function to be run) with P5 arguments taken |
| 1616 | ** from register P2 and successors. The result of the function is stored |
| 1617 | ** in register P3. Register P3 must not be one of the function inputs. |
| 1618 | ** |
| 1619 | ** P1 is a 32-bit bitmask indicating whether or not each argument to the |
| 1620 | ** function was determined to be constant at compile time. If the first |
| 1621 | ** argument was constant then bit 0 of P1 is set. This is used to determine |
| 1622 | ** whether meta data associated with a user function argument using the |
| 1623 | ** sqlite3_set_auxdata() API may be safely retained until the next |
| 1624 | ** invocation of this opcode. |
| 1625 | ** |
| 1626 | ** SQL functions are initially coded as OP_Function0 with P4 pointing |
drh | e2d9e7c | 2015-06-26 18:47:53 +0000 | [diff] [blame] | 1627 | ** to a FuncDef object. But on first evaluation, the P4 operand is |
drh | 9c7c913 | 2015-06-26 18:16:52 +0000 | [diff] [blame] | 1628 | ** automatically converted into an sqlite3_context object and the operation |
| 1629 | ** changed to this OP_Function opcode. In this way, the initialization of |
| 1630 | ** the sqlite3_context object occurs only once, rather than once for each |
| 1631 | ** evaluation of the function. |
| 1632 | ** |
| 1633 | ** See also: Function0, AggStep, AggFinal |
| 1634 | */ |
| 1635 | case OP_Function0: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1636 | int n; |
drh | 9c7c913 | 2015-06-26 18:16:52 +0000 | [diff] [blame] | 1637 | sqlite3_context *pCtx; |
danielk1977 | 51ad0ec | 2004-05-24 12:39:02 +0000 | [diff] [blame] | 1638 | |
dan | 0c54779 | 2013-07-18 17:12:08 +0000 | [diff] [blame] | 1639 | assert( pOp->p4type==P4_FUNCDEF ); |
drh | 9c7c913 | 2015-06-26 18:16:52 +0000 | [diff] [blame] | 1640 | n = pOp->p5; |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 1641 | assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) ); |
| 1642 | 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] | 1643 | assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n ); |
drh | 575fad6 | 2016-02-05 13:38:36 +0000 | [diff] [blame] | 1644 | pCtx = sqlite3DbMallocRawNN(db, sizeof(*pCtx) + (n-1)*sizeof(sqlite3_value*)); |
drh | 9c7c913 | 2015-06-26 18:16:52 +0000 | [diff] [blame] | 1645 | if( pCtx==0 ) goto no_mem; |
| 1646 | pCtx->pOut = 0; |
| 1647 | pCtx->pFunc = pOp->p4.pFunc; |
| 1648 | pCtx->iOp = (int)(pOp - aOp); |
| 1649 | pCtx->pVdbe = p; |
| 1650 | pCtx->argc = n; |
| 1651 | pOp->p4type = P4_FUNCCTX; |
| 1652 | pOp->p4.pCtx = pCtx; |
| 1653 | pOp->opcode = OP_Function; |
| 1654 | /* Fall through into OP_Function */ |
| 1655 | } |
| 1656 | case OP_Function: { |
| 1657 | int i; |
| 1658 | sqlite3_context *pCtx; |
| 1659 | |
| 1660 | assert( pOp->p4type==P4_FUNCCTX ); |
| 1661 | pCtx = pOp->p4.pCtx; |
| 1662 | |
| 1663 | /* If this function is inside of a trigger, the register array in aMem[] |
| 1664 | ** might change from one evaluation to the next. The next block of code |
| 1665 | ** checks to see if the register array has changed, and if so it |
| 1666 | ** reinitializes the relavant parts of the sqlite3_context object */ |
drh | e2d9e7c | 2015-06-26 18:47:53 +0000 | [diff] [blame] | 1667 | pOut = &aMem[pOp->p3]; |
| 1668 | if( pCtx->pOut != pOut ){ |
| 1669 | pCtx->pOut = pOut; |
drh | 9c7c913 | 2015-06-26 18:16:52 +0000 | [diff] [blame] | 1670 | for(i=pCtx->argc-1; i>=0; i--) pCtx->argv[i] = &aMem[pOp->p2+i]; |
| 1671 | } |
| 1672 | |
| 1673 | memAboutToChange(p, pCtx->pOut); |
| 1674 | #ifdef SQLITE_DEBUG |
| 1675 | for(i=0; i<pCtx->argc; i++){ |
| 1676 | assert( memIsValid(pCtx->argv[i]) ); |
| 1677 | REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]); |
| 1678 | } |
| 1679 | #endif |
| 1680 | MemSetTypeFlag(pCtx->pOut, MEM_Null); |
| 1681 | pCtx->fErrorOrAux = 0; |
drh | f6aff80 | 2014-10-08 14:28:31 +0000 | [diff] [blame] | 1682 | db->lastRowid = lastRowid; |
drh | 2d80151 | 2016-01-14 22:19:58 +0000 | [diff] [blame] | 1683 | (*pCtx->pFunc->xSFunc)(pCtx, pCtx->argc, pCtx->argv);/* IMP: R-24505-23230 */ |
| 1684 | lastRowid = db->lastRowid; /* Remember rowid changes made by xSFunc */ |
danielk1977 | 7e18c25 | 2004-05-25 11:47:24 +0000 | [diff] [blame] | 1685 | |
drh | 90669c1 | 2006-01-20 15:45:36 +0000 | [diff] [blame] | 1686 | /* If the function returned an error, throw an exception */ |
drh | 9c7c913 | 2015-06-26 18:16:52 +0000 | [diff] [blame] | 1687 | if( pCtx->fErrorOrAux ){ |
| 1688 | if( pCtx->isError ){ |
| 1689 | sqlite3VdbeError(p, "%s", sqlite3_value_text(pCtx->pOut)); |
| 1690 | rc = pCtx->isError; |
drh | 9b47ee3 | 2013-08-20 03:13:51 +0000 | [diff] [blame] | 1691 | } |
drh | b9626cf | 2016-02-22 16:04:31 +0000 | [diff] [blame] | 1692 | sqlite3VdbeDeleteAuxData(db, &p->pAuxData, pCtx->iOp, pOp->p1); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 1693 | if( rc ) goto abort_due_to_error; |
drh | 90669c1 | 2006-01-20 15:45:36 +0000 | [diff] [blame] | 1694 | } |
| 1695 | |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1696 | /* Copy the result of the function into register P3 */ |
drh | e2d9e7c | 2015-06-26 18:47:53 +0000 | [diff] [blame] | 1697 | if( pOut->flags & (MEM_Str|MEM_Blob) ){ |
| 1698 | sqlite3VdbeChangeEncoding(pCtx->pOut, encoding); |
| 1699 | if( sqlite3VdbeMemTooBig(pCtx->pOut) ) goto too_big; |
drh | 023ae03 | 2007-05-08 12:12:16 +0000 | [diff] [blame] | 1700 | } |
drh | 7b94e7f | 2011-04-04 12:29:20 +0000 | [diff] [blame] | 1701 | |
drh | 9c7c913 | 2015-06-26 18:16:52 +0000 | [diff] [blame] | 1702 | REGISTER_TRACE(pOp->p3, pCtx->pOut); |
| 1703 | UPDATE_MAX_BLOBSIZE(pCtx->pOut); |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 1704 | break; |
| 1705 | } |
| 1706 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1707 | /* Opcode: BitAnd P1 P2 P3 * * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 1708 | ** Synopsis: r[P3]=r[P1]&r[P2] |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1709 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1710 | ** Take the bit-wise AND of the values in register P1 and P2 and |
| 1711 | ** store the result in register P3. |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1712 | ** If either input is NULL, the result is NULL. |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1713 | */ |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1714 | /* Opcode: BitOr P1 P2 P3 * * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 1715 | ** Synopsis: r[P3]=r[P1]|r[P2] |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1716 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1717 | ** Take the bit-wise OR of the values in register P1 and P2 and |
| 1718 | ** store the result in register P3. |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1719 | ** If either input is NULL, the result is NULL. |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1720 | */ |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1721 | /* Opcode: ShiftLeft P1 P2 P3 * * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 1722 | ** Synopsis: r[P3]=r[P2]<<r[P1] |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1723 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1724 | ** Shift the integer value in register P2 to the left by the |
drh | 710c484 | 2010-08-30 01:17:20 +0000 | [diff] [blame] | 1725 | ** number of bits specified by the integer in register P1. |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1726 | ** Store the result in register P3. |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1727 | ** If either input is NULL, the result is NULL. |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1728 | */ |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1729 | /* Opcode: ShiftRight P1 P2 P3 * * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 1730 | ** Synopsis: r[P3]=r[P2]>>r[P1] |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1731 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1732 | ** Shift the integer value in register P2 to the right by the |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1733 | ** number of bits specified by the integer in register P1. |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1734 | ** Store the result in register P3. |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1735 | ** If either input is NULL, the result is NULL. |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1736 | */ |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1737 | case OP_BitAnd: /* same as TK_BITAND, in1, in2, out3 */ |
| 1738 | case OP_BitOr: /* same as TK_BITOR, in1, in2, out3 */ |
| 1739 | case OP_ShiftLeft: /* same as TK_LSHIFT, in1, in2, out3 */ |
| 1740 | case OP_ShiftRight: { /* same as TK_RSHIFT, in1, in2, out3 */ |
drh | 158b9cb | 2011-03-05 20:59:46 +0000 | [diff] [blame] | 1741 | i64 iA; |
| 1742 | u64 uA; |
| 1743 | i64 iB; |
| 1744 | u8 op; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1745 | |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 1746 | pIn1 = &aMem[pOp->p1]; |
| 1747 | pIn2 = &aMem[pOp->p2]; |
| 1748 | pOut = &aMem[pOp->p3]; |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1749 | if( (pIn1->flags | pIn2->flags) & MEM_Null ){ |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 1750 | sqlite3VdbeMemSetNull(pOut); |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1751 | break; |
| 1752 | } |
drh | 158b9cb | 2011-03-05 20:59:46 +0000 | [diff] [blame] | 1753 | iA = sqlite3VdbeIntValue(pIn2); |
| 1754 | iB = sqlite3VdbeIntValue(pIn1); |
| 1755 | op = pOp->opcode; |
| 1756 | if( op==OP_BitAnd ){ |
| 1757 | iA &= iB; |
| 1758 | }else if( op==OP_BitOr ){ |
| 1759 | iA |= iB; |
| 1760 | }else if( iB!=0 ){ |
| 1761 | assert( op==OP_ShiftRight || op==OP_ShiftLeft ); |
| 1762 | |
| 1763 | /* If shifting by a negative amount, shift in the other direction */ |
| 1764 | if( iB<0 ){ |
| 1765 | assert( OP_ShiftRight==OP_ShiftLeft+1 ); |
| 1766 | op = 2*OP_ShiftLeft + 1 - op; |
| 1767 | iB = iB>(-64) ? -iB : 64; |
| 1768 | } |
| 1769 | |
| 1770 | if( iB>=64 ){ |
| 1771 | iA = (iA>=0 || op==OP_ShiftLeft) ? 0 : -1; |
| 1772 | }else{ |
| 1773 | memcpy(&uA, &iA, sizeof(uA)); |
| 1774 | if( op==OP_ShiftLeft ){ |
| 1775 | uA <<= iB; |
| 1776 | }else{ |
| 1777 | uA >>= iB; |
| 1778 | /* Sign-extend on a right shift of a negative number */ |
| 1779 | if( iA<0 ) uA |= ((((u64)0xffffffff)<<32)|0xffffffff) << (64-iB); |
| 1780 | } |
| 1781 | memcpy(&iA, &uA, sizeof(iA)); |
| 1782 | } |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1783 | } |
drh | 158b9cb | 2011-03-05 20:59:46 +0000 | [diff] [blame] | 1784 | pOut->u.i = iA; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1785 | MemSetTypeFlag(pOut, MEM_Int); |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1786 | break; |
| 1787 | } |
| 1788 | |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1789 | /* Opcode: AddImm P1 P2 * * * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 1790 | ** Synopsis: r[P1]=r[P1]+P2 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1791 | ** |
danielk1977 | 0cdc022 | 2008-06-26 18:04:03 +0000 | [diff] [blame] | 1792 | ** Add the constant P2 to the value in register P1. |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1793 | ** The result is always an integer. |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 1794 | ** |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1795 | ** To force any register to be an integer, just add 0. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1796 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1797 | case OP_AddImm: { /* in1 */ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 1798 | pIn1 = &aMem[pOp->p1]; |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 1799 | memAboutToChange(p, pIn1); |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1800 | sqlite3VdbeMemIntegerify(pIn1); |
| 1801 | pIn1->u.i += pOp->p2; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1802 | break; |
| 1803 | } |
| 1804 | |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1805 | /* Opcode: MustBeInt P1 P2 * * * |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 1806 | ** |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1807 | ** Force the value in register P1 to be an integer. If the value |
| 1808 | ** in P1 is not an integer and cannot be converted into an integer |
danielk1977 | 9a96b66 | 2007-11-29 17:05:18 +0000 | [diff] [blame] | 1809 | ** without data loss, then jump immediately to P2, or if P2==0 |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 1810 | ** raise an SQLITE_MISMATCH exception. |
| 1811 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1812 | case OP_MustBeInt: { /* jump, in1 */ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 1813 | pIn1 = &aMem[pOp->p1]; |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 1814 | if( (pIn1->flags & MEM_Int)==0 ){ |
drh | 83b301b | 2013-11-20 00:59:02 +0000 | [diff] [blame] | 1815 | applyAffinity(pIn1, SQLITE_AFF_NUMERIC, encoding); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 1816 | VdbeBranchTaken((pIn1->flags&MEM_Int)==0, 2); |
drh | 83b301b | 2013-11-20 00:59:02 +0000 | [diff] [blame] | 1817 | if( (pIn1->flags & MEM_Int)==0 ){ |
| 1818 | if( pOp->p2==0 ){ |
| 1819 | rc = SQLITE_MISMATCH; |
| 1820 | goto abort_due_to_error; |
| 1821 | }else{ |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 1822 | goto jump_to_p2; |
drh | 83b301b | 2013-11-20 00:59:02 +0000 | [diff] [blame] | 1823 | } |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 1824 | } |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 1825 | } |
drh | 83b301b | 2013-11-20 00:59:02 +0000 | [diff] [blame] | 1826 | MemSetTypeFlag(pIn1, MEM_Int); |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 1827 | break; |
| 1828 | } |
| 1829 | |
drh | 13573c7 | 2010-01-12 17:04:07 +0000 | [diff] [blame] | 1830 | #ifndef SQLITE_OMIT_FLOATING_POINT |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1831 | /* Opcode: RealAffinity P1 * * * * |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 1832 | ** |
drh | 2133d82 | 2008-01-03 18:44:59 +0000 | [diff] [blame] | 1833 | ** If register P1 holds an integer convert it to a real value. |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 1834 | ** |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1835 | ** This opcode is used when extracting information from a column that |
| 1836 | ** has REAL affinity. Such column values may still be stored as |
| 1837 | ** integers, for space efficiency, but after extraction we want them |
| 1838 | ** to have only a real value. |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 1839 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1840 | case OP_RealAffinity: { /* in1 */ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 1841 | pIn1 = &aMem[pOp->p1]; |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1842 | if( pIn1->flags & MEM_Int ){ |
| 1843 | sqlite3VdbeMemRealify(pIn1); |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1844 | } |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 1845 | break; |
| 1846 | } |
drh | 13573c7 | 2010-01-12 17:04:07 +0000 | [diff] [blame] | 1847 | #endif |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 1848 | |
drh | 8df447f | 2005-11-01 15:48:24 +0000 | [diff] [blame] | 1849 | #ifndef SQLITE_OMIT_CAST |
drh | 4169e43 | 2014-08-25 20:11:52 +0000 | [diff] [blame] | 1850 | /* Opcode: Cast P1 P2 * * * |
mistachkin | a1dc42a | 2014-08-27 17:53:40 +0000 | [diff] [blame] | 1851 | ** Synopsis: affinity(r[P1]) |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 1852 | ** |
drh | 4169e43 | 2014-08-25 20:11:52 +0000 | [diff] [blame] | 1853 | ** Force the value in register P1 to be the type defined by P2. |
| 1854 | ** |
| 1855 | ** <ul> |
| 1856 | ** <li value="97"> TEXT |
| 1857 | ** <li value="98"> BLOB |
| 1858 | ** <li value="99"> NUMERIC |
| 1859 | ** <li value="100"> INTEGER |
| 1860 | ** <li value="101"> REAL |
| 1861 | ** </ul> |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 1862 | ** |
| 1863 | ** A NULL value is not changed by this routine. It remains NULL. |
| 1864 | */ |
drh | 4169e43 | 2014-08-25 20:11:52 +0000 | [diff] [blame] | 1865 | case OP_Cast: { /* in1 */ |
drh | 05883a3 | 2015-06-02 15:32:08 +0000 | [diff] [blame] | 1866 | assert( pOp->p2>=SQLITE_AFF_BLOB && pOp->p2<=SQLITE_AFF_REAL ); |
drh | 05bbb2e | 2014-08-25 22:37:19 +0000 | [diff] [blame] | 1867 | testcase( pOp->p2==SQLITE_AFF_TEXT ); |
drh | 05883a3 | 2015-06-02 15:32:08 +0000 | [diff] [blame] | 1868 | testcase( pOp->p2==SQLITE_AFF_BLOB ); |
drh | 05bbb2e | 2014-08-25 22:37:19 +0000 | [diff] [blame] | 1869 | testcase( pOp->p2==SQLITE_AFF_NUMERIC ); |
| 1870 | testcase( pOp->p2==SQLITE_AFF_INTEGER ); |
| 1871 | testcase( pOp->p2==SQLITE_AFF_REAL ); |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 1872 | pIn1 = &aMem[pOp->p1]; |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 1873 | memAboutToChange(p, pIn1); |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1874 | rc = ExpandBlob(pIn1); |
drh | 4169e43 | 2014-08-25 20:11:52 +0000 | [diff] [blame] | 1875 | sqlite3VdbeMemCast(pIn1, pOp->p2, encoding); |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 1876 | UPDATE_MAX_BLOBSIZE(pIn1); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 1877 | if( rc ) goto abort_due_to_error; |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 1878 | break; |
| 1879 | } |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1880 | #endif /* SQLITE_OMIT_CAST */ |
| 1881 | |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 1882 | /* Opcode: Lt P1 P2 P3 P4 P5 |
drh | 72dbffd | 2013-11-15 03:21:43 +0000 | [diff] [blame] | 1883 | ** Synopsis: if r[P1]<r[P3] goto P2 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1884 | ** |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 1885 | ** Compare the values in register P1 and P3. If reg(P3)<reg(P1) then |
| 1886 | ** jump to address P2. |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1887 | ** |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 1888 | ** If the SQLITE_JUMPIFNULL bit of P5 is set and either reg(P1) or |
| 1889 | ** reg(P3) is NULL then take the jump. If the SQLITE_JUMPIFNULL |
drh | 710c484 | 2010-08-30 01:17:20 +0000 | [diff] [blame] | 1890 | ** bit is clear then fall through if either operand is NULL. |
drh | 4f68623 | 2005-09-20 13:55:18 +0000 | [diff] [blame] | 1891 | ** |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 1892 | ** The SQLITE_AFF_MASK portion of P5 must be an affinity character - |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1893 | ** SQLITE_AFF_TEXT, SQLITE_AFF_INTEGER, and so forth. An attempt is made |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1894 | ** to coerce both inputs according to this affinity before the |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 1895 | ** comparison is made. If the SQLITE_AFF_MASK is 0x00, then numeric |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1896 | ** affinity is used. Note that the affinity conversions are stored |
| 1897 | ** back into the input registers P1 and P3. So this opcode can cause |
| 1898 | ** persistent changes to registers P1 and P3. |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 1899 | ** |
| 1900 | ** Once any conversions have taken place, and neither value is NULL, |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 1901 | ** the values are compared. If both values are blobs then memcmp() is |
| 1902 | ** used to determine the results of the comparison. If both values |
| 1903 | ** are text, then the appropriate collating function specified in |
| 1904 | ** P4 is used to do the comparison. If P4 is not specified then |
| 1905 | ** memcmp() is used to compare text string. If both values are |
| 1906 | ** numeric, then a numeric comparison is used. If the two values |
| 1907 | ** are of different types, then numbers are considered less than |
| 1908 | ** strings and strings are considered less than blobs. |
drh | c9b84a1 | 2002-06-20 11:36:48 +0000 | [diff] [blame] | 1909 | ** |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 1910 | ** If the SQLITE_STOREP2 bit of P5 is set, then do not jump. Instead, |
| 1911 | ** store a boolean result (either 0, or 1, or NULL) in register P2. |
drh | 053a128 | 2012-09-19 21:15:46 +0000 | [diff] [blame] | 1912 | ** |
| 1913 | ** If the SQLITE_NULLEQ bit is set in P5, then NULL values are considered |
| 1914 | ** equal to one another, provided that they do not have their MEM_Cleared |
| 1915 | ** bit set. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1916 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1917 | /* Opcode: Ne P1 P2 P3 P4 P5 |
drh | 2552d43 | 2013-11-02 22:29:34 +0000 | [diff] [blame] | 1918 | ** Synopsis: if r[P1]!=r[P3] goto P2 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1919 | ** |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 1920 | ** This works just like the Lt opcode except that the jump is taken if |
| 1921 | ** the operands in registers P1 and P3 are not equal. See the Lt opcode for |
drh | 53db145 | 2004-05-20 13:54:53 +0000 | [diff] [blame] | 1922 | ** additional information. |
drh | 6a2fe09 | 2009-09-23 02:29:36 +0000 | [diff] [blame] | 1923 | ** |
| 1924 | ** If SQLITE_NULLEQ is set in P5 then the result of comparison is always either |
| 1925 | ** true or false and is never NULL. If both operands are NULL then the result |
| 1926 | ** of comparison is false. If either operand is NULL then the result is true. |
drh | ef8662b | 2011-06-20 21:47:58 +0000 | [diff] [blame] | 1927 | ** If neither operand is NULL the result is the same as it would be if |
drh | 6a2fe09 | 2009-09-23 02:29:36 +0000 | [diff] [blame] | 1928 | ** the SQLITE_NULLEQ flag were omitted from P5. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1929 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1930 | /* Opcode: Eq P1 P2 P3 P4 P5 |
drh | 2552d43 | 2013-11-02 22:29:34 +0000 | [diff] [blame] | 1931 | ** Synopsis: if r[P1]==r[P3] goto P2 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1932 | ** |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 1933 | ** This works just like the Lt opcode except that the jump is taken if |
| 1934 | ** the operands in registers P1 and P3 are equal. |
| 1935 | ** See the Lt opcode for additional information. |
drh | 6a2fe09 | 2009-09-23 02:29:36 +0000 | [diff] [blame] | 1936 | ** |
| 1937 | ** If SQLITE_NULLEQ is set in P5 then the result of comparison is always either |
| 1938 | ** true or false and is never NULL. If both operands are NULL then the result |
| 1939 | ** of comparison is true. If either operand is NULL then the result is false. |
drh | ef8662b | 2011-06-20 21:47:58 +0000 | [diff] [blame] | 1940 | ** If neither operand is NULL the result is the same as it would be if |
drh | 6a2fe09 | 2009-09-23 02:29:36 +0000 | [diff] [blame] | 1941 | ** the SQLITE_NULLEQ flag were omitted from P5. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1942 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1943 | /* Opcode: Le P1 P2 P3 P4 P5 |
drh | 2552d43 | 2013-11-02 22:29:34 +0000 | [diff] [blame] | 1944 | ** Synopsis: if r[P1]<=r[P3] goto P2 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1945 | ** |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 1946 | ** This works just like the Lt opcode except that the jump is taken if |
| 1947 | ** the content of register P3 is less than or equal to the content of |
| 1948 | ** register P1. See the Lt opcode for additional information. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1949 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1950 | /* Opcode: Gt P1 P2 P3 P4 P5 |
drh | 2552d43 | 2013-11-02 22:29:34 +0000 | [diff] [blame] | 1951 | ** Synopsis: if r[P1]>r[P3] goto P2 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1952 | ** |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 1953 | ** This works just like the Lt opcode except that the jump is taken if |
| 1954 | ** the content of register P3 is greater than the content of |
| 1955 | ** register P1. See the Lt opcode for additional information. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1956 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1957 | /* Opcode: Ge P1 P2 P3 P4 P5 |
drh | 2552d43 | 2013-11-02 22:29:34 +0000 | [diff] [blame] | 1958 | ** Synopsis: if r[P1]>=r[P3] goto P2 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1959 | ** |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 1960 | ** This works just like the Lt opcode except that the jump is taken if |
| 1961 | ** the content of register P3 is greater than or equal to the content of |
| 1962 | ** register P1. See the Lt opcode for additional information. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1963 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1964 | case OP_Eq: /* same as TK_EQ, jump, in1, in3 */ |
| 1965 | case OP_Ne: /* same as TK_NE, jump, in1, in3 */ |
| 1966 | case OP_Lt: /* same as TK_LT, jump, in1, in3 */ |
| 1967 | case OP_Le: /* same as TK_LE, jump, in1, in3 */ |
| 1968 | case OP_Gt: /* same as TK_GT, jump, in1, in3 */ |
| 1969 | case OP_Ge: { /* same as TK_GE, jump, in1, in3 */ |
drh | 6a2fe09 | 2009-09-23 02:29:36 +0000 | [diff] [blame] | 1970 | int res; /* Result of the comparison of pIn1 against pIn3 */ |
| 1971 | char affinity; /* Affinity to use for comparison */ |
dan | b7dca7d | 2010-03-05 16:32:12 +0000 | [diff] [blame] | 1972 | u16 flags1; /* Copy of initial value of pIn1->flags */ |
| 1973 | u16 flags3; /* Copy of initial value of pIn3->flags */ |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 1974 | |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 1975 | pIn1 = &aMem[pOp->p1]; |
| 1976 | pIn3 = &aMem[pOp->p3]; |
dan | b7dca7d | 2010-03-05 16:32:12 +0000 | [diff] [blame] | 1977 | flags1 = pIn1->flags; |
| 1978 | flags3 = pIn3->flags; |
drh | c3f1d5f | 2011-05-30 23:42:16 +0000 | [diff] [blame] | 1979 | if( (flags1 | flags3)&MEM_Null ){ |
drh | 6a2fe09 | 2009-09-23 02:29:36 +0000 | [diff] [blame] | 1980 | /* One or both operands are NULL */ |
| 1981 | if( pOp->p5 & SQLITE_NULLEQ ){ |
| 1982 | /* If SQLITE_NULLEQ is set (which will only happen if the operator is |
| 1983 | ** OP_Eq or OP_Ne) then take the jump or not depending on whether |
| 1984 | ** or not both operands are null. |
| 1985 | */ |
| 1986 | assert( pOp->opcode==OP_Eq || pOp->opcode==OP_Ne ); |
drh | 053a128 | 2012-09-19 21:15:46 +0000 | [diff] [blame] | 1987 | assert( (flags1 & MEM_Cleared)==0 ); |
drh | 3d77dee | 2014-02-19 14:20:49 +0000 | [diff] [blame] | 1988 | assert( (pOp->p5 & SQLITE_JUMPIFNULL)==0 ); |
drh | 053a128 | 2012-09-19 21:15:46 +0000 | [diff] [blame] | 1989 | if( (flags1&MEM_Null)!=0 |
| 1990 | && (flags3&MEM_Null)!=0 |
| 1991 | && (flags3&MEM_Cleared)==0 |
| 1992 | ){ |
| 1993 | res = 0; /* Results are equal */ |
| 1994 | }else{ |
| 1995 | res = 1; /* Results are not equal */ |
| 1996 | } |
drh | 6a2fe09 | 2009-09-23 02:29:36 +0000 | [diff] [blame] | 1997 | }else{ |
| 1998 | /* SQLITE_NULLEQ is clear and at least one operand is NULL, |
| 1999 | ** then the result is always NULL. |
| 2000 | ** The jump is taken if the SQLITE_JUMPIFNULL bit is set. |
| 2001 | */ |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 2002 | if( pOp->p5 & SQLITE_STOREP2 ){ |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 2003 | pOut = &aMem[pOp->p2]; |
dan | b1d6b53 | 2015-12-14 19:42:19 +0000 | [diff] [blame] | 2004 | memAboutToChange(p, pOut); |
drh | 6a2fe09 | 2009-09-23 02:29:36 +0000 | [diff] [blame] | 2005 | MemSetTypeFlag(pOut, MEM_Null); |
| 2006 | REGISTER_TRACE(pOp->p2, pOut); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 2007 | }else{ |
drh | f4345e4 | 2014-02-18 11:31:59 +0000 | [diff] [blame] | 2008 | VdbeBranchTaken(2,3); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 2009 | if( pOp->p5 & SQLITE_JUMPIFNULL ){ |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 2010 | goto jump_to_p2; |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 2011 | } |
drh | 6a2fe09 | 2009-09-23 02:29:36 +0000 | [diff] [blame] | 2012 | } |
| 2013 | break; |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 2014 | } |
drh | 6a2fe09 | 2009-09-23 02:29:36 +0000 | [diff] [blame] | 2015 | }else{ |
| 2016 | /* Neither operand is NULL. Do a comparison. */ |
| 2017 | affinity = pOp->p5 & SQLITE_AFF_MASK; |
drh | 24a0962 | 2014-09-18 16:28:59 +0000 | [diff] [blame] | 2018 | if( affinity>=SQLITE_AFF_NUMERIC ){ |
drh | e5520e2 | 2015-12-31 04:34:26 +0000 | [diff] [blame] | 2019 | if( (flags1 & (MEM_Int|MEM_Real|MEM_Str))==MEM_Str ){ |
drh | 24a0962 | 2014-09-18 16:28:59 +0000 | [diff] [blame] | 2020 | applyNumericAffinity(pIn1,0); |
| 2021 | } |
drh | e5520e2 | 2015-12-31 04:34:26 +0000 | [diff] [blame] | 2022 | if( (flags3 & (MEM_Int|MEM_Real|MEM_Str))==MEM_Str ){ |
drh | 24a0962 | 2014-09-18 16:28:59 +0000 | [diff] [blame] | 2023 | applyNumericAffinity(pIn3,0); |
| 2024 | } |
| 2025 | }else if( affinity==SQLITE_AFF_TEXT ){ |
drh | e5520e2 | 2015-12-31 04:34:26 +0000 | [diff] [blame] | 2026 | if( (flags1 & MEM_Str)==0 && (flags1 & (MEM_Int|MEM_Real))!=0 ){ |
drh | e7a3466 | 2014-09-19 22:44:20 +0000 | [diff] [blame] | 2027 | testcase( pIn1->flags & MEM_Int ); |
| 2028 | testcase( pIn1->flags & MEM_Real ); |
drh | 24a0962 | 2014-09-18 16:28:59 +0000 | [diff] [blame] | 2029 | sqlite3VdbeMemStringify(pIn1, encoding, 1); |
drh | bc8a6b3 | 2015-03-31 11:42:23 +0000 | [diff] [blame] | 2030 | testcase( (flags1&MEM_Dyn) != (pIn1->flags&MEM_Dyn) ); |
| 2031 | flags1 = (pIn1->flags & ~MEM_TypeMask) | (flags1 & MEM_TypeMask); |
drh | 24a0962 | 2014-09-18 16:28:59 +0000 | [diff] [blame] | 2032 | } |
drh | e5520e2 | 2015-12-31 04:34:26 +0000 | [diff] [blame] | 2033 | if( (flags3 & MEM_Str)==0 && (flags3 & (MEM_Int|MEM_Real))!=0 ){ |
drh | e7a3466 | 2014-09-19 22:44:20 +0000 | [diff] [blame] | 2034 | testcase( pIn3->flags & MEM_Int ); |
| 2035 | testcase( pIn3->flags & MEM_Real ); |
drh | 24a0962 | 2014-09-18 16:28:59 +0000 | [diff] [blame] | 2036 | sqlite3VdbeMemStringify(pIn3, encoding, 1); |
drh | bc8a6b3 | 2015-03-31 11:42:23 +0000 | [diff] [blame] | 2037 | testcase( (flags3&MEM_Dyn) != (pIn3->flags&MEM_Dyn) ); |
| 2038 | flags3 = (pIn3->flags & ~MEM_TypeMask) | (flags3 & MEM_TypeMask); |
drh | 24a0962 | 2014-09-18 16:28:59 +0000 | [diff] [blame] | 2039 | } |
drh | 6a2fe09 | 2009-09-23 02:29:36 +0000 | [diff] [blame] | 2040 | } |
drh | 6a2fe09 | 2009-09-23 02:29:36 +0000 | [diff] [blame] | 2041 | assert( pOp->p4type==P4_COLLSEQ || pOp->p4.pColl==0 ); |
drh | e5520e2 | 2015-12-31 04:34:26 +0000 | [diff] [blame] | 2042 | if( flags1 & MEM_Zero ){ |
drh | ca5506b | 2014-09-17 23:37:38 +0000 | [diff] [blame] | 2043 | sqlite3VdbeMemExpandBlob(pIn1); |
| 2044 | flags1 &= ~MEM_Zero; |
| 2045 | } |
drh | e5520e2 | 2015-12-31 04:34:26 +0000 | [diff] [blame] | 2046 | if( flags3 & MEM_Zero ){ |
drh | ca5506b | 2014-09-17 23:37:38 +0000 | [diff] [blame] | 2047 | sqlite3VdbeMemExpandBlob(pIn3); |
| 2048 | flags3 &= ~MEM_Zero; |
| 2049 | } |
drh | 6a2fe09 | 2009-09-23 02:29:36 +0000 | [diff] [blame] | 2050 | res = sqlite3MemCompare(pIn3, pIn1, pOp->p4.pColl); |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 2051 | } |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 2052 | switch( pOp->opcode ){ |
| 2053 | case OP_Eq: res = res==0; break; |
| 2054 | case OP_Ne: res = res!=0; break; |
| 2055 | case OP_Lt: res = res<0; break; |
| 2056 | case OP_Le: res = res<=0; break; |
| 2057 | case OP_Gt: res = res>0; break; |
| 2058 | default: res = res>=0; break; |
| 2059 | } |
| 2060 | |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 2061 | /* Undo any changes made by applyAffinity() to the input registers. */ |
| 2062 | assert( (pIn1->flags & MEM_Dyn) == (flags1 & MEM_Dyn) ); |
| 2063 | pIn1->flags = flags1; |
| 2064 | assert( (pIn3->flags & MEM_Dyn) == (flags3 & MEM_Dyn) ); |
| 2065 | pIn3->flags = flags3; |
| 2066 | |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 2067 | if( pOp->p5 & SQLITE_STOREP2 ){ |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 2068 | pOut = &aMem[pOp->p2]; |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 2069 | memAboutToChange(p, pOut); |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 2070 | MemSetTypeFlag(pOut, MEM_Int); |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 2071 | pOut->u.i = res; |
| 2072 | REGISTER_TRACE(pOp->p2, pOut); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 2073 | }else{ |
drh | f4345e4 | 2014-02-18 11:31:59 +0000 | [diff] [blame] | 2074 | VdbeBranchTaken(res!=0, (pOp->p5 & SQLITE_NULLEQ)?2:3); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 2075 | if( res ){ |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 2076 | goto jump_to_p2; |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 2077 | } |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 2078 | } |
| 2079 | break; |
| 2080 | } |
drh | c9b84a1 | 2002-06-20 11:36:48 +0000 | [diff] [blame] | 2081 | |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 2082 | /* Opcode: Permutation * * * P4 * |
| 2083 | ** |
shane | be21779 | 2009-03-05 04:20:31 +0000 | [diff] [blame] | 2084 | ** Set the permutation used by the OP_Compare operator to be the array |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 2085 | ** of integers in P4. |
| 2086 | ** |
drh | 953f761 | 2012-12-07 22:18:54 +0000 | [diff] [blame] | 2087 | ** The permutation is only valid until the next OP_Compare that has |
| 2088 | ** the OPFLAG_PERMUTE bit set in P5. Typically the OP_Permutation should |
| 2089 | ** occur immediately prior to the OP_Compare. |
drh | b170202 | 2016-01-30 00:45:18 +0000 | [diff] [blame] | 2090 | ** |
| 2091 | ** The first integer in the P4 integer array is the length of the array |
| 2092 | ** and does not become part of the permutation. |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 2093 | */ |
| 2094 | case OP_Permutation: { |
| 2095 | assert( pOp->p4type==P4_INTARRAY ); |
| 2096 | assert( pOp->p4.ai ); |
drh | b170202 | 2016-01-30 00:45:18 +0000 | [diff] [blame] | 2097 | aPermute = pOp->p4.ai + 1; |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 2098 | break; |
| 2099 | } |
| 2100 | |
drh | 953f761 | 2012-12-07 22:18:54 +0000 | [diff] [blame] | 2101 | /* Opcode: Compare P1 P2 P3 P4 P5 |
drh | 079a307 | 2014-03-19 14:10:55 +0000 | [diff] [blame] | 2102 | ** Synopsis: r[P1@P3] <-> r[P2@P3] |
drh | 16ee60f | 2008-06-20 18:13:25 +0000 | [diff] [blame] | 2103 | ** |
drh | 710c484 | 2010-08-30 01:17:20 +0000 | [diff] [blame] | 2104 | ** Compare two vectors of registers in reg(P1)..reg(P1+P3-1) (call this |
| 2105 | ** 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] | 2106 | ** the comparison for use by the next OP_Jump instruct. |
| 2107 | ** |
drh | 0ca10df | 2012-12-08 13:26:23 +0000 | [diff] [blame] | 2108 | ** If P5 has the OPFLAG_PERMUTE bit set, then the order of comparison is |
| 2109 | ** determined by the most recent OP_Permutation operator. If the |
| 2110 | ** OPFLAG_PERMUTE bit is clear, then register are compared in sequential |
| 2111 | ** order. |
| 2112 | ** |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 2113 | ** P4 is a KeyInfo structure that defines collating sequences and sort |
| 2114 | ** orders for the comparison. The permutation applies to registers |
| 2115 | ** only. The KeyInfo elements are used sequentially. |
| 2116 | ** |
| 2117 | ** The comparison is a sort comparison, so NULLs compare equal, |
| 2118 | ** NULLs are less than numbers, numbers are less than strings, |
drh | 16ee60f | 2008-06-20 18:13:25 +0000 | [diff] [blame] | 2119 | ** and strings are less than blobs. |
| 2120 | */ |
| 2121 | case OP_Compare: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2122 | int n; |
| 2123 | int i; |
| 2124 | int p1; |
| 2125 | int p2; |
| 2126 | const KeyInfo *pKeyInfo; |
| 2127 | int idx; |
| 2128 | CollSeq *pColl; /* Collating sequence to use on this term */ |
| 2129 | int bRev; /* True for DESCENDING sort order */ |
| 2130 | |
drh | 953f761 | 2012-12-07 22:18:54 +0000 | [diff] [blame] | 2131 | if( (pOp->p5 & OPFLAG_PERMUTE)==0 ) aPermute = 0; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2132 | n = pOp->p3; |
| 2133 | pKeyInfo = pOp->p4.pKeyInfo; |
drh | 16ee60f | 2008-06-20 18:13:25 +0000 | [diff] [blame] | 2134 | assert( n>0 ); |
drh | 93a960a | 2008-07-10 00:32:42 +0000 | [diff] [blame] | 2135 | assert( pKeyInfo!=0 ); |
drh | 16ee60f | 2008-06-20 18:13:25 +0000 | [diff] [blame] | 2136 | p1 = pOp->p1; |
drh | 16ee60f | 2008-06-20 18:13:25 +0000 | [diff] [blame] | 2137 | p2 = pOp->p2; |
drh | 6a2fe09 | 2009-09-23 02:29:36 +0000 | [diff] [blame] | 2138 | #if SQLITE_DEBUG |
| 2139 | if( aPermute ){ |
| 2140 | int k, mx = 0; |
| 2141 | for(k=0; k<n; k++) if( aPermute[k]>mx ) mx = aPermute[k]; |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 2142 | assert( p1>0 && p1+mx<=(p->nMem+1 - p->nCursor)+1 ); |
| 2143 | assert( p2>0 && p2+mx<=(p->nMem+1 - p->nCursor)+1 ); |
drh | 6a2fe09 | 2009-09-23 02:29:36 +0000 | [diff] [blame] | 2144 | }else{ |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 2145 | assert( p1>0 && p1+n<=(p->nMem+1 - p->nCursor)+1 ); |
| 2146 | assert( p2>0 && p2+n<=(p->nMem+1 - p->nCursor)+1 ); |
drh | 6a2fe09 | 2009-09-23 02:29:36 +0000 | [diff] [blame] | 2147 | } |
| 2148 | #endif /* SQLITE_DEBUG */ |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 2149 | for(i=0; i<n; i++){ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2150 | idx = aPermute ? aPermute[i] : i; |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 2151 | assert( memIsValid(&aMem[p1+idx]) ); |
| 2152 | assert( memIsValid(&aMem[p2+idx]) ); |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 2153 | REGISTER_TRACE(p1+idx, &aMem[p1+idx]); |
| 2154 | REGISTER_TRACE(p2+idx, &aMem[p2+idx]); |
drh | 93a960a | 2008-07-10 00:32:42 +0000 | [diff] [blame] | 2155 | assert( i<pKeyInfo->nField ); |
| 2156 | pColl = pKeyInfo->aColl[i]; |
| 2157 | bRev = pKeyInfo->aSortOrder[i]; |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 2158 | iCompare = sqlite3MemCompare(&aMem[p1+idx], &aMem[p2+idx], pColl); |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 2159 | if( iCompare ){ |
| 2160 | if( bRev ) iCompare = -iCompare; |
| 2161 | break; |
| 2162 | } |
drh | 16ee60f | 2008-06-20 18:13:25 +0000 | [diff] [blame] | 2163 | } |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 2164 | aPermute = 0; |
drh | 16ee60f | 2008-06-20 18:13:25 +0000 | [diff] [blame] | 2165 | break; |
| 2166 | } |
| 2167 | |
| 2168 | /* Opcode: Jump P1 P2 P3 * * |
| 2169 | ** |
| 2170 | ** Jump to the instruction at address P1, P2, or P3 depending on whether |
| 2171 | ** in the most recent OP_Compare instruction the P1 vector was less than |
| 2172 | ** equal to, or greater than the P2 vector, respectively. |
| 2173 | */ |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 2174 | case OP_Jump: { /* jump */ |
| 2175 | if( iCompare<0 ){ |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 2176 | VdbeBranchTaken(0,3); pOp = &aOp[pOp->p1 - 1]; |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 2177 | }else if( iCompare==0 ){ |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 2178 | VdbeBranchTaken(1,3); pOp = &aOp[pOp->p2 - 1]; |
drh | 16ee60f | 2008-06-20 18:13:25 +0000 | [diff] [blame] | 2179 | }else{ |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 2180 | VdbeBranchTaken(2,3); pOp = &aOp[pOp->p3 - 1]; |
drh | 16ee60f | 2008-06-20 18:13:25 +0000 | [diff] [blame] | 2181 | } |
| 2182 | break; |
| 2183 | } |
| 2184 | |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 2185 | /* Opcode: And P1 P2 P3 * * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 2186 | ** Synopsis: r[P3]=(r[P1] && r[P2]) |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2187 | ** |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 2188 | ** Take the logical AND of the values in registers P1 and P2 and |
| 2189 | ** write the result into register P3. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2190 | ** |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 2191 | ** If either P1 or P2 is 0 (false) then the result is 0 even if |
| 2192 | ** the other input is NULL. A NULL and true or two NULLs give |
| 2193 | ** a NULL output. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2194 | */ |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 2195 | /* Opcode: Or P1 P2 P3 * * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 2196 | ** Synopsis: r[P3]=(r[P1] || r[P2]) |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 2197 | ** |
| 2198 | ** Take the logical OR of the values in register P1 and P2 and |
| 2199 | ** store the answer in register P3. |
| 2200 | ** |
| 2201 | ** If either P1 or P2 is nonzero (true) then the result is 1 (true) |
| 2202 | ** even if the other input is NULL. A NULL and false or two NULLs |
| 2203 | ** give a NULL output. |
| 2204 | */ |
| 2205 | case OP_And: /* same as TK_AND, in1, in2, out3 */ |
| 2206 | case OP_Or: { /* same as TK_OR, in1, in2, out3 */ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2207 | int v1; /* Left operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */ |
| 2208 | int v2; /* Right operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */ |
drh | bb11351 | 2002-05-27 01:04:51 +0000 | [diff] [blame] | 2209 | |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 2210 | pIn1 = &aMem[pOp->p1]; |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 2211 | if( pIn1->flags & MEM_Null ){ |
drh | bb11351 | 2002-05-27 01:04:51 +0000 | [diff] [blame] | 2212 | v1 = 2; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2213 | }else{ |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 2214 | v1 = sqlite3VdbeIntValue(pIn1)!=0; |
drh | bb11351 | 2002-05-27 01:04:51 +0000 | [diff] [blame] | 2215 | } |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 2216 | pIn2 = &aMem[pOp->p2]; |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 2217 | if( pIn2->flags & MEM_Null ){ |
drh | bb11351 | 2002-05-27 01:04:51 +0000 | [diff] [blame] | 2218 | v2 = 2; |
| 2219 | }else{ |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 2220 | v2 = sqlite3VdbeIntValue(pIn2)!=0; |
drh | bb11351 | 2002-05-27 01:04:51 +0000 | [diff] [blame] | 2221 | } |
| 2222 | if( pOp->opcode==OP_And ){ |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 2223 | 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] | 2224 | v1 = and_logic[v1*3+v2]; |
| 2225 | }else{ |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 2226 | 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] | 2227 | v1 = or_logic[v1*3+v2]; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2228 | } |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 2229 | pOut = &aMem[pOp->p3]; |
drh | bb11351 | 2002-05-27 01:04:51 +0000 | [diff] [blame] | 2230 | if( v1==2 ){ |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 2231 | MemSetTypeFlag(pOut, MEM_Null); |
drh | bb11351 | 2002-05-27 01:04:51 +0000 | [diff] [blame] | 2232 | }else{ |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 2233 | pOut->u.i = v1; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 2234 | MemSetTypeFlag(pOut, MEM_Int); |
drh | bb11351 | 2002-05-27 01:04:51 +0000 | [diff] [blame] | 2235 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2236 | break; |
| 2237 | } |
| 2238 | |
drh | e99fa2a | 2008-12-15 15:27:51 +0000 | [diff] [blame] | 2239 | /* Opcode: Not P1 P2 * * * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 2240 | ** Synopsis: r[P2]= !r[P1] |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2241 | ** |
drh | e99fa2a | 2008-12-15 15:27:51 +0000 | [diff] [blame] | 2242 | ** Interpret the value in register P1 as a boolean value. Store the |
| 2243 | ** boolean complement in register P2. If the value in register P1 is |
| 2244 | ** NULL, then a NULL is stored in P2. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2245 | */ |
drh | 93952eb | 2009-11-13 19:43:43 +0000 | [diff] [blame] | 2246 | case OP_Not: { /* same as TK_NOT, in1, out2 */ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 2247 | pIn1 = &aMem[pOp->p1]; |
| 2248 | pOut = &aMem[pOp->p2]; |
drh | 0725cab | 2014-09-17 14:52:46 +0000 | [diff] [blame] | 2249 | sqlite3VdbeMemSetNull(pOut); |
| 2250 | if( (pIn1->flags & MEM_Null)==0 ){ |
| 2251 | pOut->flags = MEM_Int; |
| 2252 | pOut->u.i = !sqlite3VdbeIntValue(pIn1); |
drh | e99fa2a | 2008-12-15 15:27:51 +0000 | [diff] [blame] | 2253 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2254 | break; |
| 2255 | } |
| 2256 | |
drh | e99fa2a | 2008-12-15 15:27:51 +0000 | [diff] [blame] | 2257 | /* Opcode: BitNot P1 P2 * * * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 2258 | ** Synopsis: r[P1]= ~r[P1] |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 2259 | ** |
drh | e99fa2a | 2008-12-15 15:27:51 +0000 | [diff] [blame] | 2260 | ** Interpret the content of register P1 as an integer. Store the |
| 2261 | ** ones-complement of the P1 value into register P2. If P1 holds |
| 2262 | ** a NULL then store a NULL in P2. |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 2263 | */ |
drh | 93952eb | 2009-11-13 19:43:43 +0000 | [diff] [blame] | 2264 | case OP_BitNot: { /* same as TK_BITNOT, in1, out2 */ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 2265 | pIn1 = &aMem[pOp->p1]; |
| 2266 | pOut = &aMem[pOp->p2]; |
drh | 0725cab | 2014-09-17 14:52:46 +0000 | [diff] [blame] | 2267 | sqlite3VdbeMemSetNull(pOut); |
| 2268 | if( (pIn1->flags & MEM_Null)==0 ){ |
| 2269 | pOut->flags = MEM_Int; |
| 2270 | pOut->u.i = ~sqlite3VdbeIntValue(pIn1); |
drh | e99fa2a | 2008-12-15 15:27:51 +0000 | [diff] [blame] | 2271 | } |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 2272 | break; |
| 2273 | } |
| 2274 | |
drh | 48f2d3b | 2011-09-16 01:34:43 +0000 | [diff] [blame] | 2275 | /* Opcode: Once P1 P2 * * * |
| 2276 | ** |
drh | 5dad9a3 | 2014-07-25 18:37:42 +0000 | [diff] [blame] | 2277 | ** Check the "once" flag number P1. If it is set, jump to instruction P2. |
| 2278 | ** Otherwise, set the flag and fall through to the next instruction. |
| 2279 | ** In other words, this opcode causes all following opcodes up through P2 |
| 2280 | ** (but not including P2) to run just once and to be skipped on subsequent |
| 2281 | ** times through the loop. |
| 2282 | ** |
| 2283 | ** All "once" flags are initially cleared whenever a prepared statement |
| 2284 | ** first begins to run. |
drh | 48f2d3b | 2011-09-16 01:34:43 +0000 | [diff] [blame] | 2285 | */ |
dan | 1d8cb21 | 2011-12-09 13:24:16 +0000 | [diff] [blame] | 2286 | case OP_Once: { /* jump */ |
| 2287 | assert( pOp->p1<p->nOnceFlag ); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 2288 | VdbeBranchTaken(p->aOnceFlag[pOp->p1]!=0, 2); |
dan | 1d8cb21 | 2011-12-09 13:24:16 +0000 | [diff] [blame] | 2289 | if( p->aOnceFlag[pOp->p1] ){ |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 2290 | goto jump_to_p2; |
dan | 1d8cb21 | 2011-12-09 13:24:16 +0000 | [diff] [blame] | 2291 | }else{ |
| 2292 | p->aOnceFlag[pOp->p1] = 1; |
| 2293 | } |
| 2294 | break; |
| 2295 | } |
| 2296 | |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 2297 | /* Opcode: If P1 P2 P3 * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2298 | ** |
drh | ef8662b | 2011-06-20 21:47:58 +0000 | [diff] [blame] | 2299 | ** Jump to P2 if the value in register P1 is true. The value |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 2300 | ** is considered true if it is numeric and non-zero. If the value |
drh | e21a6e1 | 2014-08-01 18:00:24 +0000 | [diff] [blame] | 2301 | ** 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] | 2302 | */ |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 2303 | /* Opcode: IfNot P1 P2 P3 * * |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 2304 | ** |
drh | ef8662b | 2011-06-20 21:47:58 +0000 | [diff] [blame] | 2305 | ** Jump to P2 if the value in register P1 is False. The value |
drh | b8475df | 2011-12-09 16:21:19 +0000 | [diff] [blame] | 2306 | ** 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] | 2307 | ** 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] | 2308 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2309 | case OP_If: /* jump, in1 */ |
| 2310 | case OP_IfNot: { /* jump, in1 */ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2311 | int c; |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 2312 | pIn1 = &aMem[pOp->p1]; |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 2313 | if( pIn1->flags & MEM_Null ){ |
| 2314 | c = pOp->p3; |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 2315 | }else{ |
drh | ba0232a | 2005-06-06 17:27:19 +0000 | [diff] [blame] | 2316 | #ifdef SQLITE_OMIT_FLOATING_POINT |
shane | fbd60f8 | 2009-02-04 03:59:25 +0000 | [diff] [blame] | 2317 | c = sqlite3VdbeIntValue(pIn1)!=0; |
drh | ba0232a | 2005-06-06 17:27:19 +0000 | [diff] [blame] | 2318 | #else |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 2319 | c = sqlite3VdbeRealValue(pIn1)!=0.0; |
drh | ba0232a | 2005-06-06 17:27:19 +0000 | [diff] [blame] | 2320 | #endif |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 2321 | if( pOp->opcode==OP_IfNot ) c = !c; |
| 2322 | } |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 2323 | VdbeBranchTaken(c!=0, 2); |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 2324 | if( c ){ |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 2325 | goto jump_to_p2; |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 2326 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2327 | break; |
| 2328 | } |
| 2329 | |
drh | 830ecf9 | 2009-06-18 00:41:55 +0000 | [diff] [blame] | 2330 | /* Opcode: IsNull P1 P2 * * * |
drh | fc8d4f9 | 2013-11-08 15:19:46 +0000 | [diff] [blame] | 2331 | ** Synopsis: if r[P1]==NULL goto P2 |
drh | 477df4b | 2008-01-05 18:48:24 +0000 | [diff] [blame] | 2332 | ** |
drh | 830ecf9 | 2009-06-18 00:41:55 +0000 | [diff] [blame] | 2333 | ** Jump to P2 if the value in register P1 is NULL. |
drh | 477df4b | 2008-01-05 18:48:24 +0000 | [diff] [blame] | 2334 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2335 | case OP_IsNull: { /* same as TK_ISNULL, jump, in1 */ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 2336 | pIn1 = &aMem[pOp->p1]; |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 2337 | VdbeBranchTaken( (pIn1->flags & MEM_Null)!=0, 2); |
drh | 830ecf9 | 2009-06-18 00:41:55 +0000 | [diff] [blame] | 2338 | if( (pIn1->flags & MEM_Null)!=0 ){ |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 2339 | goto jump_to_p2; |
drh | 830ecf9 | 2009-06-18 00:41:55 +0000 | [diff] [blame] | 2340 | } |
drh | 477df4b | 2008-01-05 18:48:24 +0000 | [diff] [blame] | 2341 | break; |
| 2342 | } |
| 2343 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 2344 | /* Opcode: NotNull P1 P2 * * * |
drh | fc8d4f9 | 2013-11-08 15:19:46 +0000 | [diff] [blame] | 2345 | ** Synopsis: if r[P1]!=NULL goto P2 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2346 | ** |
drh | 6a288a3 | 2008-01-07 19:20:24 +0000 | [diff] [blame] | 2347 | ** Jump to P2 if the value in register P1 is not NULL. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2348 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2349 | case OP_NotNull: { /* same as TK_NOTNULL, jump, in1 */ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 2350 | pIn1 = &aMem[pOp->p1]; |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 2351 | VdbeBranchTaken( (pIn1->flags & MEM_Null)==0, 2); |
drh | 6a288a3 | 2008-01-07 19:20:24 +0000 | [diff] [blame] | 2352 | if( (pIn1->flags & MEM_Null)==0 ){ |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 2353 | goto jump_to_p2; |
drh | 6a288a3 | 2008-01-07 19:20:24 +0000 | [diff] [blame] | 2354 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2355 | break; |
| 2356 | } |
| 2357 | |
drh | 3e9ca09 | 2009-09-08 01:14:48 +0000 | [diff] [blame] | 2358 | /* Opcode: Column P1 P2 P3 P4 P5 |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 2359 | ** Synopsis: r[P3]=PX |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2360 | ** |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 2361 | ** Interpret the data that cursor P1 points to as a structure built using |
| 2362 | ** the MakeRecord instruction. (See the MakeRecord opcode for additional |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 2363 | ** information about the format of the data.) Extract the P2-th column |
| 2364 | ** from this record. If there are less that (P2+1) |
| 2365 | ** values in the record, extract a NULL. |
| 2366 | ** |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2367 | ** The value extracted is stored in register P3. |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2368 | ** |
danielk1977 | 1f4aa33 | 2008-01-03 09:51:55 +0000 | [diff] [blame] | 2369 | ** If the column contains fewer than P2 fields, then extract a NULL. Or, |
| 2370 | ** if the P4 argument is a P4_MEM use the value of the P4 argument as |
| 2371 | ** the result. |
drh | 3e9ca09 | 2009-09-08 01:14:48 +0000 | [diff] [blame] | 2372 | ** |
| 2373 | ** If the OPFLAG_CLEARCACHE bit is set on P5 and P1 is a pseudo-table cursor, |
| 2374 | ** then the cache of the cursor is reset prior to extracting the column. |
| 2375 | ** The first OP_Column against a pseudo-table after the value of the content |
| 2376 | ** register has changed should have this bit set. |
drh | a748fdc | 2012-03-28 01:34:47 +0000 | [diff] [blame] | 2377 | ** |
drh | dda5c08 | 2012-03-28 13:41:10 +0000 | [diff] [blame] | 2378 | ** If the OPFLAG_LENGTHARG and OPFLAG_TYPEOFARG bits are set on P5 when |
| 2379 | ** the result is guaranteed to only be used as the argument of a length() |
| 2380 | ** or typeof() function, respectively. The loading of large blobs can be |
| 2381 | ** skipped for length() and all content loading can be skipped for typeof(). |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2382 | */ |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 2383 | case OP_Column: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2384 | i64 payloadSize64; /* Number of bytes in the record */ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2385 | int p2; /* column number to retrieve */ |
| 2386 | VdbeCursor *pC; /* The VDBE cursor */ |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 2387 | BtCursor *pCrsr; /* The BTree cursor */ |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 2388 | 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] | 2389 | int len; /* The length of the serialized data for the column */ |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 2390 | int i; /* Loop counter */ |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 2391 | Mem *pDest; /* Where to write the extracted value */ |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 2392 | Mem sMem; /* For storing the record being decoded */ |
drh | 399af1d | 2013-11-20 17:25:55 +0000 | [diff] [blame] | 2393 | const u8 *zData; /* Part of the record being decoded */ |
| 2394 | const u8 *zHdr; /* Next unparsed byte of the header */ |
| 2395 | const u8 *zEndHdr; /* Pointer to first byte after the header */ |
drh | 35cd643 | 2009-06-05 14:17:21 +0000 | [diff] [blame] | 2396 | u32 offset; /* Offset into the data */ |
drh | c6ce3883 | 2015-10-15 21:30:24 +0000 | [diff] [blame] | 2397 | u64 offset64; /* 64-bit offset */ |
drh | 501932c | 2013-11-21 21:59:53 +0000 | [diff] [blame] | 2398 | u32 avail; /* Number of bytes of available data */ |
drh | 5a077b7 | 2011-08-29 02:16:18 +0000 | [diff] [blame] | 2399 | u32 t; /* A type code from the record header */ |
drh | 3e9ca09 | 2009-09-08 01:14:48 +0000 | [diff] [blame] | 2400 | Mem *pReg; /* PseudoTable input register */ |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2401 | |
dan | de892d9 | 2016-01-29 19:29:45 +0000 | [diff] [blame] | 2402 | pC = p->apCsr[pOp->p1]; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2403 | p2 = pOp->p2; |
dan | de892d9 | 2016-01-29 19:29:45 +0000 | [diff] [blame] | 2404 | |
| 2405 | /* If the cursor cache is stale, bring it up-to-date */ |
| 2406 | rc = sqlite3VdbeCursorMoveto(&pC, &p2); |
| 2407 | |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 2408 | assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) ); |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 2409 | pDest = &aMem[pOp->p3]; |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 2410 | memAboutToChange(p, pDest); |
drh | c8606e4 | 2013-11-20 19:28:03 +0000 | [diff] [blame] | 2411 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
danielk1977 | 6c92409 | 2007-11-12 08:09:34 +0000 | [diff] [blame] | 2412 | assert( pC!=0 ); |
drh | c8606e4 | 2013-11-20 19:28:03 +0000 | [diff] [blame] | 2413 | assert( p2<pC->nField ); |
drh | b53a5a9 | 2014-10-12 22:37:22 +0000 | [diff] [blame] | 2414 | aOffset = pC->aOffset; |
drh | 62aaa6c | 2015-11-21 17:27:42 +0000 | [diff] [blame] | 2415 | assert( pC->eCurType!=CURTYPE_VTAB ); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 2416 | assert( pC->eCurType!=CURTYPE_PSEUDO || pC->nullRow ); |
| 2417 | assert( pC->eCurType!=CURTYPE_SORTER ); |
| 2418 | pCrsr = pC->uc.pCursor; |
drh | 399af1d | 2013-11-20 17:25:55 +0000 | [diff] [blame] | 2419 | |
drh | 399af1d | 2013-11-20 17:25:55 +0000 | [diff] [blame] | 2420 | if( rc ) goto abort_due_to_error; |
drh | 6cf4a7d | 2014-10-13 13:00:58 +0000 | [diff] [blame] | 2421 | if( pC->cacheStatus!=p->cacheCtr ){ |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2422 | if( pC->nullRow ){ |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 2423 | if( pC->eCurType==CURTYPE_PSEUDO ){ |
| 2424 | assert( pC->uc.pseudoTableReg>0 ); |
| 2425 | pReg = &aMem[pC->uc.pseudoTableReg]; |
drh | c8606e4 | 2013-11-20 19:28:03 +0000 | [diff] [blame] | 2426 | assert( pReg->flags & MEM_Blob ); |
| 2427 | assert( memIsValid(pReg) ); |
| 2428 | pC->payloadSize = pC->szRow = avail = pReg->n; |
| 2429 | pC->aRow = (u8*)pReg->z; |
| 2430 | }else{ |
drh | 6b5631e | 2014-11-05 15:57:39 +0000 | [diff] [blame] | 2431 | sqlite3VdbeMemSetNull(pDest); |
drh | 399af1d | 2013-11-20 17:25:55 +0000 | [diff] [blame] | 2432 | goto op_column_out; |
| 2433 | } |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2434 | }else{ |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 2435 | assert( pC->eCurType==CURTYPE_BTREE ); |
drh | c8606e4 | 2013-11-20 19:28:03 +0000 | [diff] [blame] | 2436 | assert( pCrsr ); |
drh | 14da87f | 2013-11-20 21:51:33 +0000 | [diff] [blame] | 2437 | if( pC->isTable==0 ){ |
drh | 399af1d | 2013-11-20 17:25:55 +0000 | [diff] [blame] | 2438 | assert( sqlite3BtreeCursorIsValid(pCrsr) ); |
| 2439 | VVA_ONLY(rc =) sqlite3BtreeKeySize(pCrsr, &payloadSize64); |
| 2440 | assert( rc==SQLITE_OK ); /* True because of CursorMoveto() call above */ |
| 2441 | /* sqlite3BtreeParseCellPtr() uses getVarint32() to extract the |
| 2442 | ** payload size, so it is impossible for payloadSize64 to be |
| 2443 | ** larger than 32 bits. */ |
| 2444 | assert( (payloadSize64 & SQLITE_MAX_U32)==(u64)payloadSize64 ); |
| 2445 | pC->aRow = sqlite3BtreeKeyFetch(pCrsr, &avail); |
| 2446 | pC->payloadSize = (u32)payloadSize64; |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 2447 | }else{ |
drh | 399af1d | 2013-11-20 17:25:55 +0000 | [diff] [blame] | 2448 | assert( sqlite3BtreeCursorIsValid(pCrsr) ); |
| 2449 | VVA_ONLY(rc =) sqlite3BtreeDataSize(pCrsr, &pC->payloadSize); |
| 2450 | assert( rc==SQLITE_OK ); /* DataSize() cannot fail */ |
| 2451 | pC->aRow = sqlite3BtreeDataFetch(pCrsr, &avail); |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2452 | } |
drh | 399af1d | 2013-11-20 17:25:55 +0000 | [diff] [blame] | 2453 | assert( avail<=65536 ); /* Maximum page size is 64KiB */ |
| 2454 | if( pC->payloadSize <= (u32)avail ){ |
| 2455 | pC->szRow = pC->payloadSize; |
drh | 5f7dacb | 2015-11-20 13:33:56 +0000 | [diff] [blame] | 2456 | }else if( pC->payloadSize > (u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){ |
| 2457 | goto too_big; |
drh | e61cffc | 2004-06-12 18:12:15 +0000 | [diff] [blame] | 2458 | }else{ |
drh | 399af1d | 2013-11-20 17:25:55 +0000 | [diff] [blame] | 2459 | pC->szRow = avail; |
| 2460 | } |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 2461 | } |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2462 | pC->cacheStatus = p->cacheCtr; |
drh | 399af1d | 2013-11-20 17:25:55 +0000 | [diff] [blame] | 2463 | pC->iHdrOffset = getVarint32(pC->aRow, offset); |
| 2464 | pC->nHdrParsed = 0; |
| 2465 | aOffset[0] = offset; |
drh | 35cd643 | 2009-06-05 14:17:21 +0000 | [diff] [blame] | 2466 | |
drh | c81aa2e | 2014-10-11 23:31:52 +0000 | [diff] [blame] | 2467 | |
| 2468 | if( avail<offset ){ |
| 2469 | /* pC->aRow does not have to hold the entire row, but it does at least |
| 2470 | ** need to cover the header of the record. If pC->aRow does not contain |
| 2471 | ** the complete header, then set it to zero, forcing the header to be |
| 2472 | ** dynamically allocated. */ |
| 2473 | pC->aRow = 0; |
| 2474 | pC->szRow = 0; |
drh | 848a332 | 2015-10-16 12:53:47 +0000 | [diff] [blame] | 2475 | |
| 2476 | /* Make sure a corrupt database has not given us an oversize header. |
| 2477 | ** Do this now to avoid an oversize memory allocation. |
| 2478 | ** |
| 2479 | ** Type entries can be between 1 and 5 bytes each. But 4 and 5 byte |
| 2480 | ** types use so much data space that there can only be 4096 and 32 of |
| 2481 | ** them, respectively. So the maximum header length results from a |
| 2482 | ** 3-byte type for each of the maximum of 32768 columns plus three |
| 2483 | ** extra bytes for the header length itself. 32768*3 + 3 = 98307. |
| 2484 | */ |
| 2485 | if( offset > 98307 || offset > pC->payloadSize ){ |
| 2486 | rc = SQLITE_CORRUPT_BKPT; |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 2487 | goto abort_due_to_error; |
drh | 848a332 | 2015-10-16 12:53:47 +0000 | [diff] [blame] | 2488 | } |
drh | c81aa2e | 2014-10-11 23:31:52 +0000 | [diff] [blame] | 2489 | } |
| 2490 | |
| 2491 | /* The following goto is an optimization. It can be omitted and |
| 2492 | ** everything will still work. But OP_Column is measurably faster |
| 2493 | ** by skipping the subsequent conditional, which is always true. |
| 2494 | */ |
| 2495 | assert( pC->nHdrParsed<=p2 ); /* Conditional skipped */ |
| 2496 | goto op_column_read_header; |
drh | 399af1d | 2013-11-20 17:25:55 +0000 | [diff] [blame] | 2497 | } |
drh | 35cd643 | 2009-06-05 14:17:21 +0000 | [diff] [blame] | 2498 | |
drh | 399af1d | 2013-11-20 17:25:55 +0000 | [diff] [blame] | 2499 | /* 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] | 2500 | ** parsed and valid information is in aOffset[] and pC->aType[]. |
drh | 399af1d | 2013-11-20 17:25:55 +0000 | [diff] [blame] | 2501 | */ |
drh | c8606e4 | 2013-11-20 19:28:03 +0000 | [diff] [blame] | 2502 | if( pC->nHdrParsed<=p2 ){ |
drh | 380d685 | 2013-11-20 20:58:00 +0000 | [diff] [blame] | 2503 | /* If there is more header available for parsing in the record, try |
| 2504 | ** to extract additional fields up through the p2+1-th field |
drh | 35cd643 | 2009-06-05 14:17:21 +0000 | [diff] [blame] | 2505 | */ |
drh | c81aa2e | 2014-10-11 23:31:52 +0000 | [diff] [blame] | 2506 | op_column_read_header: |
drh | c8606e4 | 2013-11-20 19:28:03 +0000 | [diff] [blame] | 2507 | if( pC->iHdrOffset<aOffset[0] ){ |
| 2508 | /* Make sure zData points to enough of the record to cover the header. */ |
| 2509 | if( pC->aRow==0 ){ |
| 2510 | memset(&sMem, 0, sizeof(sMem)); |
drh | 95fa606 | 2015-10-16 13:50:08 +0000 | [diff] [blame] | 2511 | rc = sqlite3VdbeMemFromBtree(pCrsr, 0, aOffset[0], !pC->isTable, &sMem); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 2512 | if( rc!=SQLITE_OK ) goto abort_due_to_error; |
drh | c8606e4 | 2013-11-20 19:28:03 +0000 | [diff] [blame] | 2513 | zData = (u8*)sMem.z; |
| 2514 | }else{ |
| 2515 | zData = pC->aRow; |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2516 | } |
drh | c8606e4 | 2013-11-20 19:28:03 +0000 | [diff] [blame] | 2517 | |
drh | 0c8f760 | 2014-09-19 16:56:45 +0000 | [diff] [blame] | 2518 | /* Fill in pC->aType[i] and aOffset[i] values through the p2-th field. */ |
drh | c8606e4 | 2013-11-20 19:28:03 +0000 | [diff] [blame] | 2519 | i = pC->nHdrParsed; |
drh | c6ce3883 | 2015-10-15 21:30:24 +0000 | [diff] [blame] | 2520 | offset64 = aOffset[i]; |
drh | c8606e4 | 2013-11-20 19:28:03 +0000 | [diff] [blame] | 2521 | zHdr = zData + pC->iHdrOffset; |
| 2522 | zEndHdr = zData + aOffset[0]; |
| 2523 | assert( i<=p2 && zHdr<zEndHdr ); |
| 2524 | do{ |
drh | 95fa606 | 2015-10-16 13:50:08 +0000 | [diff] [blame] | 2525 | if( (t = zHdr[0])<0x80 ){ |
drh | c8606e4 | 2013-11-20 19:28:03 +0000 | [diff] [blame] | 2526 | zHdr++; |
drh | faf3727 | 2015-10-16 14:23:42 +0000 | [diff] [blame] | 2527 | offset64 += sqlite3VdbeOneByteSerialTypeLen(t); |
drh | 5a077b7 | 2011-08-29 02:16:18 +0000 | [diff] [blame] | 2528 | }else{ |
drh | c8606e4 | 2013-11-20 19:28:03 +0000 | [diff] [blame] | 2529 | zHdr += sqlite3GetVarint32(zHdr, &t); |
drh | faf3727 | 2015-10-16 14:23:42 +0000 | [diff] [blame] | 2530 | offset64 += sqlite3VdbeSerialTypeLen(t); |
drh | 5a077b7 | 2011-08-29 02:16:18 +0000 | [diff] [blame] | 2531 | } |
drh | faf3727 | 2015-10-16 14:23:42 +0000 | [diff] [blame] | 2532 | pC->aType[i++] = t; |
drh | c6ce3883 | 2015-10-15 21:30:24 +0000 | [diff] [blame] | 2533 | aOffset[i] = (u32)(offset64 & 0xffffffff); |
drh | c8606e4 | 2013-11-20 19:28:03 +0000 | [diff] [blame] | 2534 | }while( i<=p2 && zHdr<zEndHdr ); |
| 2535 | pC->nHdrParsed = i; |
| 2536 | pC->iHdrOffset = (u32)(zHdr - zData); |
drh | c8606e4 | 2013-11-20 19:28:03 +0000 | [diff] [blame] | 2537 | |
drh | 8dd8362 | 2014-10-13 23:39:02 +0000 | [diff] [blame] | 2538 | /* The record is corrupt if any of the following are true: |
| 2539 | ** (1) the bytes of the header extend past the declared header size |
drh | 8dd8362 | 2014-10-13 23:39:02 +0000 | [diff] [blame] | 2540 | ** (2) the entire header was used but not all data was used |
drh | 8dd8362 | 2014-10-13 23:39:02 +0000 | [diff] [blame] | 2541 | ** (3) the end of the data extends beyond the end of the record. |
drh | c8606e4 | 2013-11-20 19:28:03 +0000 | [diff] [blame] | 2542 | */ |
drh | c6ce3883 | 2015-10-15 21:30:24 +0000 | [diff] [blame] | 2543 | if( (zHdr>=zEndHdr && (zHdr>zEndHdr || offset64!=pC->payloadSize)) |
| 2544 | || (offset64 > pC->payloadSize) |
drh | c8606e4 | 2013-11-20 19:28:03 +0000 | [diff] [blame] | 2545 | ){ |
drh | ddb2b4a | 2016-03-25 12:10:32 +0000 | [diff] [blame] | 2546 | if( pC->aRow==0 ) sqlite3VdbeMemRelease(&sMem); |
drh | c8606e4 | 2013-11-20 19:28:03 +0000 | [diff] [blame] | 2547 | rc = SQLITE_CORRUPT_BKPT; |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 2548 | goto abort_due_to_error; |
danielk1977 | dedf45b | 2006-01-13 17:12:01 +0000 | [diff] [blame] | 2549 | } |
drh | ddb2b4a | 2016-03-25 12:10:32 +0000 | [diff] [blame] | 2550 | if( pC->aRow==0 ) sqlite3VdbeMemRelease(&sMem); |
| 2551 | |
mistachkin | 8c7cd6a | 2015-12-16 21:09:53 +0000 | [diff] [blame] | 2552 | }else{ |
drh | 9fbc885 | 2016-01-04 03:48:46 +0000 | [diff] [blame] | 2553 | t = 0; |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2554 | } |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 2555 | |
drh | f2db338 | 2015-04-30 20:33:25 +0000 | [diff] [blame] | 2556 | /* If after trying to extract new entries from the header, nHdrParsed is |
drh | 380d685 | 2013-11-20 20:58:00 +0000 | [diff] [blame] | 2557 | ** still not up to p2, that means that the record has fewer than p2 |
| 2558 | ** columns. So the result will be either the default value or a NULL. |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 2559 | */ |
drh | c8606e4 | 2013-11-20 19:28:03 +0000 | [diff] [blame] | 2560 | if( pC->nHdrParsed<=p2 ){ |
| 2561 | if( pOp->p4type==P4_MEM ){ |
| 2562 | sqlite3VdbeMemShallowCopy(pDest, pOp->p4.pMem, MEM_Static); |
| 2563 | }else{ |
drh | 22e8d83 | 2014-10-29 00:58:38 +0000 | [diff] [blame] | 2564 | sqlite3VdbeMemSetNull(pDest); |
drh | c8606e4 | 2013-11-20 19:28:03 +0000 | [diff] [blame] | 2565 | } |
danielk1977 | 3c9cc8d | 2005-01-17 03:40:08 +0000 | [diff] [blame] | 2566 | goto op_column_out; |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 2567 | } |
drh | 95fa606 | 2015-10-16 13:50:08 +0000 | [diff] [blame] | 2568 | }else{ |
| 2569 | t = pC->aType[p2]; |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 2570 | } |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2571 | |
drh | 380d685 | 2013-11-20 20:58:00 +0000 | [diff] [blame] | 2572 | /* Extract the content for the p2+1-th column. Control can only |
drh | 0c8f760 | 2014-09-19 16:56:45 +0000 | [diff] [blame] | 2573 | ** 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] | 2574 | ** all valid. |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2575 | */ |
drh | c8606e4 | 2013-11-20 19:28:03 +0000 | [diff] [blame] | 2576 | assert( p2<pC->nHdrParsed ); |
| 2577 | assert( rc==SQLITE_OK ); |
drh | 75fd054 | 2014-03-01 16:24:44 +0000 | [diff] [blame] | 2578 | assert( sqlite3VdbeCheckMemInvariants(pDest) ); |
drh | 0725cab | 2014-09-17 14:52:46 +0000 | [diff] [blame] | 2579 | if( VdbeMemDynamic(pDest) ) sqlite3VdbeMemSetNull(pDest); |
drh | 95fa606 | 2015-10-16 13:50:08 +0000 | [diff] [blame] | 2580 | assert( t==pC->aType[p2] ); |
drh | 69f6e25 | 2016-01-11 18:05:00 +0000 | [diff] [blame] | 2581 | pDest->enc = encoding; |
drh | c8606e4 | 2013-11-20 19:28:03 +0000 | [diff] [blame] | 2582 | if( pC->szRow>=aOffset[p2+1] ){ |
drh | 380d685 | 2013-11-20 20:58:00 +0000 | [diff] [blame] | 2583 | /* This is the common case where the desired content fits on the original |
| 2584 | ** page - where the content is not on an overflow page */ |
drh | 69f6e25 | 2016-01-11 18:05:00 +0000 | [diff] [blame] | 2585 | zData = pC->aRow + aOffset[p2]; |
| 2586 | if( t<12 ){ |
| 2587 | sqlite3VdbeSerialGet(zData, t, pDest); |
| 2588 | }else{ |
| 2589 | /* If the column value is a string, we need a persistent value, not |
| 2590 | ** a MEM_Ephem value. This branch is a fast short-cut that is equivalent |
| 2591 | ** to calling sqlite3VdbeSerialGet() and sqlite3VdbeDeephemeralize(). |
| 2592 | */ |
| 2593 | static const u16 aFlag[] = { MEM_Blob, MEM_Str|MEM_Term }; |
| 2594 | pDest->n = len = (t-12)/2; |
| 2595 | if( pDest->szMalloc < len+2 ){ |
| 2596 | pDest->flags = MEM_Null; |
| 2597 | if( sqlite3VdbeMemGrow(pDest, len+2, 0) ) goto no_mem; |
| 2598 | }else{ |
| 2599 | pDest->z = pDest->zMalloc; |
| 2600 | } |
| 2601 | memcpy(pDest->z, zData, len); |
| 2602 | pDest->z[len] = 0; |
| 2603 | pDest->z[len+1] = 0; |
| 2604 | pDest->flags = aFlag[t&1]; |
| 2605 | } |
danielk1977 | 36963fd | 2005-02-19 08:18:05 +0000 | [diff] [blame] | 2606 | }else{ |
drh | 58c9608 | 2013-12-23 11:33:32 +0000 | [diff] [blame] | 2607 | /* This branch happens only when content is on overflow pages */ |
drh | 380d685 | 2013-11-20 20:58:00 +0000 | [diff] [blame] | 2608 | if( ((pOp->p5 & (OPFLAG_LENGTHARG|OPFLAG_TYPEOFARG))!=0 |
| 2609 | && ((t>=12 && (t&1)==0) || (pOp->p5 & OPFLAG_TYPEOFARG)!=0)) |
| 2610 | || (len = sqlite3VdbeSerialTypeLen(t))==0 |
drh | c8606e4 | 2013-11-20 19:28:03 +0000 | [diff] [blame] | 2611 | ){ |
drh | 2a2a696 | 2014-09-16 18:22:44 +0000 | [diff] [blame] | 2612 | /* Content is irrelevant for |
| 2613 | ** 1. the typeof() function, |
| 2614 | ** 2. the length(X) function if X is a blob, and |
| 2615 | ** 3. if the content length is zero. |
| 2616 | ** So we might as well use bogus content rather than reading |
drh | 69f6e25 | 2016-01-11 18:05:00 +0000 | [diff] [blame] | 2617 | ** content from disk. */ |
| 2618 | static u8 aZero[8]; /* This is the bogus content */ |
| 2619 | sqlite3VdbeSerialGet(aZero, t, pDest); |
danielk1977 | aee18ef | 2005-03-09 12:26:50 +0000 | [diff] [blame] | 2620 | }else{ |
drh | 14da87f | 2013-11-20 21:51:33 +0000 | [diff] [blame] | 2621 | rc = sqlite3VdbeMemFromBtree(pCrsr, aOffset[p2], len, !pC->isTable, |
drh | 2a2a696 | 2014-09-16 18:22:44 +0000 | [diff] [blame] | 2622 | pDest); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 2623 | if( rc!=SQLITE_OK ) goto abort_due_to_error; |
| 2624 | sqlite3VdbeSerialGet((const u8*)pDest->z, t, pDest); |
| 2625 | pDest->flags &= ~MEM_Ephem; |
danielk1977 | aee18ef | 2005-03-09 12:26:50 +0000 | [diff] [blame] | 2626 | } |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 2627 | } |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 2628 | |
danielk1977 | 3c9cc8d | 2005-01-17 03:40:08 +0000 | [diff] [blame] | 2629 | op_column_out: |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 2630 | UPDATE_MAX_BLOBSIZE(pDest); |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 2631 | REGISTER_TRACE(pOp->p3, pDest); |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2632 | break; |
| 2633 | } |
| 2634 | |
danielk1977 | 751de56 | 2008-04-18 09:01:15 +0000 | [diff] [blame] | 2635 | /* Opcode: Affinity P1 P2 * P4 * |
drh | f63552b | 2013-10-30 00:25:03 +0000 | [diff] [blame] | 2636 | ** Synopsis: affinity(r[P1@P2]) |
danielk1977 | 751de56 | 2008-04-18 09:01:15 +0000 | [diff] [blame] | 2637 | ** |
| 2638 | ** Apply affinities to a range of P2 registers starting with P1. |
| 2639 | ** |
| 2640 | ** P4 is a string that is P2 characters long. The nth character of the |
| 2641 | ** string indicates the column affinity that should be used for the nth |
| 2642 | ** memory cell in the range. |
| 2643 | */ |
| 2644 | case OP_Affinity: { |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 2645 | const char *zAffinity; /* The affinity to be applied */ |
| 2646 | char cAff; /* A single character of affinity */ |
danielk1977 | 751de56 | 2008-04-18 09:01:15 +0000 | [diff] [blame] | 2647 | |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2648 | zAffinity = pOp->p4.z; |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 2649 | assert( zAffinity!=0 ); |
| 2650 | assert( zAffinity[pOp->p2]==0 ); |
| 2651 | pIn1 = &aMem[pOp->p1]; |
| 2652 | while( (cAff = *(zAffinity++))!=0 ){ |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 2653 | assert( pIn1 <= &p->aMem[(p->nMem+1 - p->nCursor)] ); |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 2654 | assert( memIsValid(pIn1) ); |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 2655 | applyAffinity(pIn1, cAff, encoding); |
| 2656 | pIn1++; |
danielk1977 | 751de56 | 2008-04-18 09:01:15 +0000 | [diff] [blame] | 2657 | } |
| 2658 | break; |
| 2659 | } |
| 2660 | |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2661 | /* Opcode: MakeRecord P1 P2 P3 P4 * |
drh | f63552b | 2013-10-30 00:25:03 +0000 | [diff] [blame] | 2662 | ** Synopsis: r[P3]=mkrec(r[P1@P2]) |
drh | 7a224de | 2004-06-02 01:22:02 +0000 | [diff] [blame] | 2663 | ** |
drh | 710c484 | 2010-08-30 01:17:20 +0000 | [diff] [blame] | 2664 | ** Convert P2 registers beginning with P1 into the [record format] |
| 2665 | ** use as a data record in a database table or as a key |
| 2666 | ** in an index. The OP_Column opcode can decode the record later. |
drh | 7a224de | 2004-06-02 01:22:02 +0000 | [diff] [blame] | 2667 | ** |
danielk1977 | 751de56 | 2008-04-18 09:01:15 +0000 | [diff] [blame] | 2668 | ** P4 may be a string that is P2 characters long. The nth character of the |
drh | 7a224de | 2004-06-02 01:22:02 +0000 | [diff] [blame] | 2669 | ** string indicates the column affinity that should be used for the nth |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2670 | ** field of the index key. |
drh | 7a224de | 2004-06-02 01:22:02 +0000 | [diff] [blame] | 2671 | ** |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 2672 | ** The mapping from character to affinity is given by the SQLITE_AFF_ |
| 2673 | ** macros defined in sqliteInt.h. |
drh | 7a224de | 2004-06-02 01:22:02 +0000 | [diff] [blame] | 2674 | ** |
drh | 05883a3 | 2015-06-02 15:32:08 +0000 | [diff] [blame] | 2675 | ** If P4 is NULL then all index fields have the affinity BLOB. |
drh | 7f057c9 | 2005-06-24 03:53:06 +0000 | [diff] [blame] | 2676 | */ |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2677 | case OP_MakeRecord: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2678 | u8 *zNewRecord; /* A buffer to hold the data for the new record */ |
| 2679 | Mem *pRec; /* The new record */ |
| 2680 | u64 nData; /* Number of bytes of data space */ |
| 2681 | int nHdr; /* Number of bytes of header space */ |
| 2682 | i64 nByte; /* Data space required for this record */ |
drh | 4a33507 | 2015-04-11 02:08:48 +0000 | [diff] [blame] | 2683 | i64 nZero; /* Number of zero bytes at the end of the record */ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2684 | int nVarint; /* Number of bytes in a varint */ |
| 2685 | u32 serial_type; /* Type field */ |
| 2686 | Mem *pData0; /* First field to be combined into the record */ |
| 2687 | Mem *pLast; /* Last field of the record */ |
| 2688 | int nField; /* Number of fields in the record */ |
| 2689 | char *zAffinity; /* The affinity string for the record */ |
| 2690 | int file_format; /* File format to use for encoding */ |
drh | 59bf00c | 2013-12-08 23:33:28 +0000 | [diff] [blame] | 2691 | int i; /* Space used in zNewRecord[] header */ |
| 2692 | int j; /* Space used in zNewRecord[] content */ |
drh | be37c12 | 2015-10-16 14:54:17 +0000 | [diff] [blame] | 2693 | u32 len; /* Length of a field */ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2694 | |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 2695 | /* Assuming the record contains N fields, the record format looks |
| 2696 | ** like this: |
| 2697 | ** |
drh | 7a224de | 2004-06-02 01:22:02 +0000 | [diff] [blame] | 2698 | ** ------------------------------------------------------------------------ |
| 2699 | ** | hdr-size | type 0 | type 1 | ... | type N-1 | data0 | ... | data N-1 | |
| 2700 | ** ------------------------------------------------------------------------ |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 2701 | ** |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2702 | ** 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] | 2703 | ** and so forth. |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 2704 | ** |
| 2705 | ** Each type field is a varint representing the serial type of the |
| 2706 | ** corresponding data element (see sqlite3VdbeSerialType()). The |
drh | 7a224de | 2004-06-02 01:22:02 +0000 | [diff] [blame] | 2707 | ** hdr-size field is also a varint which is the offset from the beginning |
| 2708 | ** of the record to data0. |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 2709 | */ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2710 | nData = 0; /* Number of bytes of data space */ |
| 2711 | nHdr = 0; /* Number of bytes of header space */ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2712 | nZero = 0; /* Number of zero bytes at the end of the record */ |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2713 | nField = pOp->p1; |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 2714 | zAffinity = pOp->p4.z; |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 2715 | 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] | 2716 | pData0 = &aMem[nField]; |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2717 | nField = pOp->p2; |
| 2718 | pLast = &pData0[nField-1]; |
drh | d946db0 | 2005-12-29 19:23:06 +0000 | [diff] [blame] | 2719 | file_format = p->minWriteFileFormat; |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 2720 | |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 2721 | /* Identify the output register */ |
| 2722 | assert( pOp->p3<pOp->p1 || pOp->p3>=pOp->p1+pOp->p2 ); |
| 2723 | pOut = &aMem[pOp->p3]; |
| 2724 | memAboutToChange(p, pOut); |
| 2725 | |
drh | 3e6c060 | 2013-12-10 20:53:01 +0000 | [diff] [blame] | 2726 | /* Apply the requested affinity to all inputs |
| 2727 | */ |
| 2728 | assert( pData0<=pLast ); |
| 2729 | if( zAffinity ){ |
| 2730 | pRec = pData0; |
| 2731 | do{ |
drh | 57bf4a8 | 2014-02-17 14:59:22 +0000 | [diff] [blame] | 2732 | applyAffinity(pRec++, *(zAffinity++), encoding); |
| 2733 | assert( zAffinity[0]==0 || pRec<=pLast ); |
| 2734 | }while( zAffinity[0] ); |
drh | 3e6c060 | 2013-12-10 20:53:01 +0000 | [diff] [blame] | 2735 | } |
| 2736 | |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 2737 | /* Loop through the elements that will make up the record to figure |
| 2738 | ** out how much space is required for the new record. |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 2739 | */ |
drh | 038b7bc | 2013-12-09 23:17:22 +0000 | [diff] [blame] | 2740 | pRec = pLast; |
drh | 59bf00c | 2013-12-08 23:33:28 +0000 | [diff] [blame] | 2741 | do{ |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 2742 | assert( memIsValid(pRec) ); |
drh | be37c12 | 2015-10-16 14:54:17 +0000 | [diff] [blame] | 2743 | pRec->uTemp = serial_type = sqlite3VdbeSerialType(pRec, file_format, &len); |
drh | fdf972a | 2007-05-02 13:30:27 +0000 | [diff] [blame] | 2744 | if( pRec->flags & MEM_Zero ){ |
drh | 038b7bc | 2013-12-09 23:17:22 +0000 | [diff] [blame] | 2745 | if( nData ){ |
drh | 53e66c3 | 2015-07-24 15:49:23 +0000 | [diff] [blame] | 2746 | if( sqlite3VdbeMemExpandBlob(pRec) ) goto no_mem; |
drh | 038b7bc | 2013-12-09 23:17:22 +0000 | [diff] [blame] | 2747 | }else{ |
| 2748 | nZero += pRec->u.nZero; |
| 2749 | len -= pRec->u.nZero; |
| 2750 | } |
drh | fdf972a | 2007-05-02 13:30:27 +0000 | [diff] [blame] | 2751 | } |
drh | 8079a0d | 2006-01-12 17:20:50 +0000 | [diff] [blame] | 2752 | nData += len; |
drh | 59bf00c | 2013-12-08 23:33:28 +0000 | [diff] [blame] | 2753 | testcase( serial_type==127 ); |
| 2754 | testcase( serial_type==128 ); |
drh | 2a24287 | 2013-12-08 22:59:29 +0000 | [diff] [blame] | 2755 | nHdr += serial_type<=127 ? 1 : sqlite3VarintLen(serial_type); |
drh | 038b7bc | 2013-12-09 23:17:22 +0000 | [diff] [blame] | 2756 | }while( (--pRec)>=pData0 ); |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 2757 | |
drh | 654858d | 2014-11-20 02:18:14 +0000 | [diff] [blame] | 2758 | /* EVIDENCE-OF: R-22564-11647 The header begins with a single varint |
| 2759 | ** which determines the total number of bytes in the header. The varint |
| 2760 | ** value is the size of the header in bytes including the size varint |
| 2761 | ** itself. */ |
drh | 59bf00c | 2013-12-08 23:33:28 +0000 | [diff] [blame] | 2762 | testcase( nHdr==126 ); |
| 2763 | testcase( nHdr==127 ); |
drh | 2a24287 | 2013-12-08 22:59:29 +0000 | [diff] [blame] | 2764 | if( nHdr<=126 ){ |
| 2765 | /* The common case */ |
| 2766 | nHdr += 1; |
| 2767 | }else{ |
| 2768 | /* Rare case of a really large header */ |
| 2769 | nVarint = sqlite3VarintLen(nHdr); |
| 2770 | nHdr += nVarint; |
| 2771 | if( nVarint<sqlite3VarintLen(nHdr) ) nHdr++; |
drh | cb9882a | 2005-03-17 03:15:40 +0000 | [diff] [blame] | 2772 | } |
drh | 038b7bc | 2013-12-09 23:17:22 +0000 | [diff] [blame] | 2773 | nByte = nHdr+nData; |
drh | 4a33507 | 2015-04-11 02:08:48 +0000 | [diff] [blame] | 2774 | if( nByte+nZero>db->aLimit[SQLITE_LIMIT_LENGTH] ){ |
drh | 023ae03 | 2007-05-08 12:12:16 +0000 | [diff] [blame] | 2775 | goto too_big; |
| 2776 | } |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 2777 | |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 2778 | /* Make sure the output register has a buffer large enough to store |
| 2779 | ** the new record. The output register (pOp->p3) is not allowed to |
| 2780 | ** be one of the input registers (because the following call to |
drh | 322f285 | 2014-09-19 00:43:39 +0000 | [diff] [blame] | 2781 | ** sqlite3VdbeMemClearAndResize() could clobber the value before it is used). |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 2782 | */ |
drh | 322f285 | 2014-09-19 00:43:39 +0000 | [diff] [blame] | 2783 | if( sqlite3VdbeMemClearAndResize(pOut, (int)nByte) ){ |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 2784 | goto no_mem; |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 2785 | } |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 2786 | zNewRecord = (u8 *)pOut->z; |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 2787 | |
| 2788 | /* Write the record */ |
shane | 3f8d5cf | 2008-04-24 19:15:09 +0000 | [diff] [blame] | 2789 | i = putVarint32(zNewRecord, nHdr); |
drh | 59bf00c | 2013-12-08 23:33:28 +0000 | [diff] [blame] | 2790 | j = nHdr; |
| 2791 | assert( pData0<=pLast ); |
| 2792 | pRec = pData0; |
| 2793 | do{ |
drh | facf47a | 2014-10-13 20:12:47 +0000 | [diff] [blame] | 2794 | serial_type = pRec->uTemp; |
drh | 654858d | 2014-11-20 02:18:14 +0000 | [diff] [blame] | 2795 | /* EVIDENCE-OF: R-06529-47362 Following the size varint are one or more |
| 2796 | ** additional varints, one per column. */ |
drh | 038b7bc | 2013-12-09 23:17:22 +0000 | [diff] [blame] | 2797 | i += putVarint32(&zNewRecord[i], serial_type); /* serial type */ |
drh | 654858d | 2014-11-20 02:18:14 +0000 | [diff] [blame] | 2798 | /* EVIDENCE-OF: R-64536-51728 The values for each column in the record |
| 2799 | ** immediately follow the header. */ |
drh | a9ab481 | 2013-12-11 11:00:44 +0000 | [diff] [blame] | 2800 | j += sqlite3VdbeSerialPut(&zNewRecord[j], pRec, serial_type); /* content */ |
drh | 59bf00c | 2013-12-08 23:33:28 +0000 | [diff] [blame] | 2801 | }while( (++pRec)<=pLast ); |
| 2802 | assert( i==nHdr ); |
| 2803 | assert( j==nByte ); |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 2804 | |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 2805 | assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) ); |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 2806 | pOut->n = (int)nByte; |
drh | c91b2fd | 2014-03-01 18:13:23 +0000 | [diff] [blame] | 2807 | pOut->flags = MEM_Blob; |
drh | fdf972a | 2007-05-02 13:30:27 +0000 | [diff] [blame] | 2808 | if( nZero ){ |
drh | 8df3284 | 2008-12-09 02:51:23 +0000 | [diff] [blame] | 2809 | pOut->u.nZero = nZero; |
drh | 477df4b | 2008-01-05 18:48:24 +0000 | [diff] [blame] | 2810 | pOut->flags |= MEM_Zero; |
drh | fdf972a | 2007-05-02 13:30:27 +0000 | [diff] [blame] | 2811 | } |
drh | 477df4b | 2008-01-05 18:48:24 +0000 | [diff] [blame] | 2812 | pOut->enc = SQLITE_UTF8; /* In case the blob is ever converted to text */ |
drh | 1013c93 | 2008-01-06 00:25:21 +0000 | [diff] [blame] | 2813 | REGISTER_TRACE(pOp->p3, pOut); |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 2814 | UPDATE_MAX_BLOBSIZE(pOut); |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 2815 | break; |
| 2816 | } |
| 2817 | |
danielk1977 | a553316 | 2009-02-24 10:01:51 +0000 | [diff] [blame] | 2818 | /* Opcode: Count P1 P2 * * * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 2819 | ** Synopsis: r[P2]=count() |
danielk1977 | a553316 | 2009-02-24 10:01:51 +0000 | [diff] [blame] | 2820 | ** |
| 2821 | ** Store the number of entries (an integer value) in the table or index |
| 2822 | ** opened by cursor P1 in register P2 |
| 2823 | */ |
| 2824 | #ifndef SQLITE_OMIT_BTREECOUNT |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 2825 | case OP_Count: { /* out2 */ |
danielk1977 | a553316 | 2009-02-24 10:01:51 +0000 | [diff] [blame] | 2826 | i64 nEntry; |
drh | c54a617 | 2009-06-02 16:06:03 +0000 | [diff] [blame] | 2827 | BtCursor *pCrsr; |
| 2828 | |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 2829 | assert( p->apCsr[pOp->p1]->eCurType==CURTYPE_BTREE ); |
| 2830 | pCrsr = p->apCsr[pOp->p1]->uc.pCursor; |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 2831 | assert( pCrsr ); |
drh | 2dc0648 | 2013-12-11 00:59:10 +0000 | [diff] [blame] | 2832 | nEntry = 0; /* Not needed. Only used to silence a warning. */ |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 2833 | rc = sqlite3BtreeCount(pCrsr, &nEntry); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 2834 | if( rc ) goto abort_due_to_error; |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 2835 | pOut = out2Prerelease(p, pOp); |
danielk1977 | a553316 | 2009-02-24 10:01:51 +0000 | [diff] [blame] | 2836 | pOut->u.i = nEntry; |
| 2837 | break; |
| 2838 | } |
| 2839 | #endif |
| 2840 | |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2841 | /* Opcode: Savepoint P1 * * P4 * |
| 2842 | ** |
| 2843 | ** Open, release or rollback the savepoint named by parameter P4, depending |
| 2844 | ** on the value of P1. To open a new savepoint, P1==0. To release (commit) an |
| 2845 | ** existing savepoint, P1==1, or to rollback an existing savepoint P1==2. |
| 2846 | */ |
| 2847 | case OP_Savepoint: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2848 | int p1; /* Value of P1 operand */ |
| 2849 | char *zName; /* Name of savepoint */ |
| 2850 | int nName; |
| 2851 | Savepoint *pNew; |
| 2852 | Savepoint *pSavepoint; |
| 2853 | Savepoint *pTmp; |
| 2854 | int iSavepoint; |
| 2855 | int ii; |
| 2856 | |
| 2857 | p1 = pOp->p1; |
| 2858 | zName = pOp->p4.z; |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2859 | |
| 2860 | /* Assert that the p1 parameter is valid. Also that if there is no open |
| 2861 | ** transaction, then there cannot be any savepoints. |
| 2862 | */ |
| 2863 | assert( db->pSavepoint==0 || db->autoCommit==0 ); |
| 2864 | assert( p1==SAVEPOINT_BEGIN||p1==SAVEPOINT_RELEASE||p1==SAVEPOINT_ROLLBACK ); |
| 2865 | assert( db->pSavepoint || db->isTransactionSavepoint==0 ); |
| 2866 | assert( checkSavepointCount(db) ); |
dan | c0537fe | 2013-06-28 19:41:43 +0000 | [diff] [blame] | 2867 | assert( p->bIsReader ); |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2868 | |
| 2869 | if( p1==SAVEPOINT_BEGIN ){ |
drh | 4f7d3a5 | 2013-06-27 23:54:02 +0000 | [diff] [blame] | 2870 | if( db->nVdbeWrite>0 ){ |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2871 | /* A new savepoint cannot be created if there are active write |
| 2872 | ** statements (i.e. open read/write incremental blob handles). |
| 2873 | */ |
drh | 22c17b8 | 2015-05-15 04:13:15 +0000 | [diff] [blame] | 2874 | sqlite3VdbeError(p, "cannot open savepoint - SQL statements in progress"); |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2875 | rc = SQLITE_BUSY; |
| 2876 | }else{ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2877 | nName = sqlite3Strlen30(zName); |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2878 | |
drh | be07ec5 | 2011-06-03 12:15:26 +0000 | [diff] [blame] | 2879 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
dan | d9495cd | 2011-04-27 12:08:04 +0000 | [diff] [blame] | 2880 | /* This call is Ok even if this savepoint is actually a transaction |
| 2881 | ** savepoint (and therefore should not prompt xSavepoint()) callbacks. |
| 2882 | ** If this is a transaction savepoint being opened, it is guaranteed |
| 2883 | ** that the db->aVTrans[] array is empty. */ |
| 2884 | assert( db->autoCommit==0 || db->nVTrans==0 ); |
drh | a24bc9c | 2011-05-24 00:35:56 +0000 | [diff] [blame] | 2885 | rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN, |
| 2886 | db->nStatement+db->nSavepoint); |
dan | d9495cd | 2011-04-27 12:08:04 +0000 | [diff] [blame] | 2887 | if( rc!=SQLITE_OK ) goto abort_due_to_error; |
drh | 305ebab | 2011-05-26 14:19:14 +0000 | [diff] [blame] | 2888 | #endif |
dan | d9495cd | 2011-04-27 12:08:04 +0000 | [diff] [blame] | 2889 | |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2890 | /* Create a new savepoint structure. */ |
drh | 575fad6 | 2016-02-05 13:38:36 +0000 | [diff] [blame] | 2891 | pNew = sqlite3DbMallocRawNN(db, sizeof(Savepoint)+nName+1); |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2892 | if( pNew ){ |
| 2893 | pNew->zName = (char *)&pNew[1]; |
| 2894 | memcpy(pNew->zName, zName, nName+1); |
| 2895 | |
| 2896 | /* If there is no open transaction, then mark this as a special |
| 2897 | ** "transaction savepoint". */ |
| 2898 | if( db->autoCommit ){ |
| 2899 | db->autoCommit = 0; |
| 2900 | db->isTransactionSavepoint = 1; |
| 2901 | }else{ |
| 2902 | db->nSavepoint++; |
danielk1977 | d829335 | 2009-04-30 09:10:37 +0000 | [diff] [blame] | 2903 | } |
dan | 21e8d01 | 2011-03-03 20:05:59 +0000 | [diff] [blame] | 2904 | |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2905 | /* Link the new savepoint into the database handle's list. */ |
| 2906 | pNew->pNext = db->pSavepoint; |
| 2907 | db->pSavepoint = pNew; |
dan | ba9108b | 2009-09-22 07:13:42 +0000 | [diff] [blame] | 2908 | pNew->nDeferredCons = db->nDeferredCons; |
dan | cb3e4b7 | 2013-07-03 19:53:05 +0000 | [diff] [blame] | 2909 | pNew->nDeferredImmCons = db->nDeferredImmCons; |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2910 | } |
| 2911 | } |
| 2912 | }else{ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2913 | iSavepoint = 0; |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2914 | |
| 2915 | /* Find the named savepoint. If there is no such savepoint, then an |
| 2916 | ** an error is returned to the user. */ |
| 2917 | for( |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2918 | pSavepoint = db->pSavepoint; |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2919 | pSavepoint && sqlite3StrICmp(pSavepoint->zName, zName); |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2920 | pSavepoint = pSavepoint->pNext |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2921 | ){ |
| 2922 | iSavepoint++; |
| 2923 | } |
| 2924 | if( !pSavepoint ){ |
drh | 22c17b8 | 2015-05-15 04:13:15 +0000 | [diff] [blame] | 2925 | sqlite3VdbeError(p, "no such savepoint: %s", zName); |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2926 | rc = SQLITE_ERROR; |
drh | 4f7d3a5 | 2013-06-27 23:54:02 +0000 | [diff] [blame] | 2927 | }else if( db->nVdbeWrite>0 && p1==SAVEPOINT_RELEASE ){ |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2928 | /* It is not possible to release (commit) a savepoint if there are |
drh | 0f198a7 | 2012-02-13 16:43:16 +0000 | [diff] [blame] | 2929 | ** active write statements. |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2930 | */ |
drh | 22c17b8 | 2015-05-15 04:13:15 +0000 | [diff] [blame] | 2931 | sqlite3VdbeError(p, "cannot release savepoint - " |
| 2932 | "SQL statements in progress"); |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2933 | rc = SQLITE_BUSY; |
| 2934 | }else{ |
| 2935 | |
| 2936 | /* Determine whether or not this is a transaction savepoint. If so, |
danielk1977 | 34cf35d | 2008-12-18 18:31:38 +0000 | [diff] [blame] | 2937 | ** and this is a RELEASE command, then the current transaction |
| 2938 | ** is committed. |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2939 | */ |
| 2940 | int isTransaction = pSavepoint->pNext==0 && db->isTransactionSavepoint; |
| 2941 | if( isTransaction && p1==SAVEPOINT_RELEASE ){ |
dan | 32b09f2 | 2009-09-23 17:29:59 +0000 | [diff] [blame] | 2942 | if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){ |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 2943 | goto vdbe_return; |
| 2944 | } |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2945 | db->autoCommit = 1; |
| 2946 | if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){ |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 2947 | p->pc = (int)(pOp - aOp); |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2948 | db->autoCommit = 0; |
| 2949 | p->rc = rc = SQLITE_BUSY; |
| 2950 | goto vdbe_return; |
| 2951 | } |
danielk1977 | 34cf35d | 2008-12-18 18:31:38 +0000 | [diff] [blame] | 2952 | db->isTransactionSavepoint = 0; |
| 2953 | rc = p->rc; |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2954 | }else{ |
drh | 47b7fc7 | 2014-11-11 01:33:57 +0000 | [diff] [blame] | 2955 | int isSchemaChange; |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2956 | iSavepoint = db->nSavepoint - iSavepoint - 1; |
drh | 31f1005 | 2012-03-31 17:17:26 +0000 | [diff] [blame] | 2957 | if( p1==SAVEPOINT_ROLLBACK ){ |
drh | 47b7fc7 | 2014-11-11 01:33:57 +0000 | [diff] [blame] | 2958 | isSchemaChange = (db->flags & SQLITE_InternChanges)!=0; |
drh | 31f1005 | 2012-03-31 17:17:26 +0000 | [diff] [blame] | 2959 | for(ii=0; ii<db->nDb; ii++){ |
drh | 77b1dee | 2014-11-17 17:13:06 +0000 | [diff] [blame] | 2960 | rc = sqlite3BtreeTripAllCursors(db->aDb[ii].pBt, |
| 2961 | SQLITE_ABORT_ROLLBACK, |
drh | 47b7fc7 | 2014-11-11 01:33:57 +0000 | [diff] [blame] | 2962 | isSchemaChange==0); |
dan | 8023104 | 2014-11-12 14:56:02 +0000 | [diff] [blame] | 2963 | if( rc!=SQLITE_OK ) goto abort_due_to_error; |
drh | 31f1005 | 2012-03-31 17:17:26 +0000 | [diff] [blame] | 2964 | } |
drh | 47b7fc7 | 2014-11-11 01:33:57 +0000 | [diff] [blame] | 2965 | }else{ |
| 2966 | isSchemaChange = 0; |
drh | 0f198a7 | 2012-02-13 16:43:16 +0000 | [diff] [blame] | 2967 | } |
| 2968 | for(ii=0; ii<db->nDb; ii++){ |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2969 | rc = sqlite3BtreeSavepoint(db->aDb[ii].pBt, p1, iSavepoint); |
| 2970 | if( rc!=SQLITE_OK ){ |
| 2971 | goto abort_due_to_error; |
danielk1977 | bd43455 | 2009-03-18 10:33:00 +0000 | [diff] [blame] | 2972 | } |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2973 | } |
drh | 47b7fc7 | 2014-11-11 01:33:57 +0000 | [diff] [blame] | 2974 | if( isSchemaChange ){ |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2975 | sqlite3ExpirePreparedStatements(db); |
drh | 81028a4 | 2012-05-15 18:28:27 +0000 | [diff] [blame] | 2976 | sqlite3ResetAllSchemasOfConnection(db); |
dan | c311fee | 2010-08-31 16:25:19 +0000 | [diff] [blame] | 2977 | db->flags = (db->flags | SQLITE_InternChanges); |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2978 | } |
| 2979 | } |
| 2980 | |
| 2981 | /* Regardless of whether this is a RELEASE or ROLLBACK, destroy all |
| 2982 | ** savepoints nested inside of the savepoint being operated on. */ |
| 2983 | while( db->pSavepoint!=pSavepoint ){ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2984 | pTmp = db->pSavepoint; |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2985 | db->pSavepoint = pTmp->pNext; |
| 2986 | sqlite3DbFree(db, pTmp); |
| 2987 | db->nSavepoint--; |
| 2988 | } |
| 2989 | |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 2990 | /* If it is a RELEASE, then destroy the savepoint being operated on |
| 2991 | ** too. If it is a ROLLBACK TO, then set the number of deferred |
| 2992 | ** constraint violations present in the database to the value stored |
| 2993 | ** when the savepoint was created. */ |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2994 | if( p1==SAVEPOINT_RELEASE ){ |
| 2995 | assert( pSavepoint==db->pSavepoint ); |
| 2996 | db->pSavepoint = pSavepoint->pNext; |
| 2997 | sqlite3DbFree(db, pSavepoint); |
| 2998 | if( !isTransaction ){ |
| 2999 | db->nSavepoint--; |
| 3000 | } |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 3001 | }else{ |
| 3002 | db->nDeferredCons = pSavepoint->nDeferredCons; |
dan | cb3e4b7 | 2013-07-03 19:53:05 +0000 | [diff] [blame] | 3003 | db->nDeferredImmCons = pSavepoint->nDeferredImmCons; |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3004 | } |
dan | d9495cd | 2011-04-27 12:08:04 +0000 | [diff] [blame] | 3005 | |
dan | ea8562e | 2015-04-18 16:25:54 +0000 | [diff] [blame] | 3006 | if( !isTransaction || p1==SAVEPOINT_ROLLBACK ){ |
dan | d9495cd | 2011-04-27 12:08:04 +0000 | [diff] [blame] | 3007 | rc = sqlite3VtabSavepoint(db, p1, iSavepoint); |
| 3008 | if( rc!=SQLITE_OK ) goto abort_due_to_error; |
| 3009 | } |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3010 | } |
| 3011 | } |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 3012 | if( rc ) goto abort_due_to_error; |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3013 | |
| 3014 | break; |
| 3015 | } |
| 3016 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 3017 | /* Opcode: AutoCommit P1 P2 * * * |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 3018 | ** |
| 3019 | ** 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] | 3020 | ** back any currently active btree transactions. If there are any active |
drh | c25eabe | 2009-02-24 18:57:31 +0000 | [diff] [blame] | 3021 | ** VMs (apart from this one), then a ROLLBACK fails. A COMMIT fails if |
| 3022 | ** there are active writing VMs or active VMs that use shared cache. |
drh | 92f02c3 | 2004-09-02 14:57:08 +0000 | [diff] [blame] | 3023 | ** |
| 3024 | ** This instruction causes the VM to halt. |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 3025 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3026 | case OP_AutoCommit: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3027 | int desiredAutoCommit; |
shane | 68c0273 | 2009-06-09 18:14:18 +0000 | [diff] [blame] | 3028 | int iRollback; |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 3029 | |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3030 | desiredAutoCommit = pOp->p1; |
shane | 68c0273 | 2009-06-09 18:14:18 +0000 | [diff] [blame] | 3031 | iRollback = pOp->p2; |
drh | ad4a4b8 | 2008-11-05 16:37:34 +0000 | [diff] [blame] | 3032 | assert( desiredAutoCommit==1 || desiredAutoCommit==0 ); |
shane | 68c0273 | 2009-06-09 18:14:18 +0000 | [diff] [blame] | 3033 | assert( desiredAutoCommit==1 || iRollback==0 ); |
drh | 4f7d3a5 | 2013-06-27 23:54:02 +0000 | [diff] [blame] | 3034 | assert( db->nVdbeActive>0 ); /* At least this one VM is active */ |
dan | c0537fe | 2013-06-28 19:41:43 +0000 | [diff] [blame] | 3035 | assert( p->bIsReader ); |
danielk1977 | 46c43ed | 2004-06-30 06:30:25 +0000 | [diff] [blame] | 3036 | |
drh | b0c8865 | 2016-02-01 13:21:13 +0000 | [diff] [blame] | 3037 | if( desiredAutoCommit!=db->autoCommit ){ |
shane | 68c0273 | 2009-06-09 18:14:18 +0000 | [diff] [blame] | 3038 | if( iRollback ){ |
drh | ad4a4b8 | 2008-11-05 16:37:34 +0000 | [diff] [blame] | 3039 | assert( desiredAutoCommit==1 ); |
drh | 21021a5 | 2012-02-13 17:01:51 +0000 | [diff] [blame] | 3040 | sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK); |
danielk1977 | f3f06bb | 2005-12-16 15:24:28 +0000 | [diff] [blame] | 3041 | db->autoCommit = 1; |
drh | b0c8865 | 2016-02-01 13:21:13 +0000 | [diff] [blame] | 3042 | }else if( desiredAutoCommit && db->nVdbeWrite>0 ){ |
| 3043 | /* If this instruction implements a COMMIT and other VMs are writing |
| 3044 | ** return an error indicating that the other VMs must complete first. |
| 3045 | */ |
| 3046 | sqlite3VdbeError(p, "cannot commit transaction - " |
| 3047 | "SQL statements in progress"); |
| 3048 | rc = SQLITE_BUSY; |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 3049 | goto abort_due_to_error; |
dan | 32b09f2 | 2009-09-23 17:29:59 +0000 | [diff] [blame] | 3050 | }else if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){ |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 3051 | goto vdbe_return; |
danielk1977 | f3f06bb | 2005-12-16 15:24:28 +0000 | [diff] [blame] | 3052 | }else{ |
shane | 7d3846a | 2008-12-11 02:58:26 +0000 | [diff] [blame] | 3053 | db->autoCommit = (u8)desiredAutoCommit; |
drh | 8ff2587 | 2015-07-31 18:59:56 +0000 | [diff] [blame] | 3054 | } |
| 3055 | if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){ |
| 3056 | p->pc = (int)(pOp - aOp); |
| 3057 | db->autoCommit = (u8)(1-desiredAutoCommit); |
| 3058 | p->rc = rc = SQLITE_BUSY; |
| 3059 | goto vdbe_return; |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 3060 | } |
danielk1977 | bd43455 | 2009-03-18 10:33:00 +0000 | [diff] [blame] | 3061 | assert( db->nStatement==0 ); |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3062 | sqlite3CloseSavepoints(db); |
drh | 83968c4 | 2007-04-18 16:45:24 +0000 | [diff] [blame] | 3063 | if( p->rc==SQLITE_OK ){ |
drh | 900b31e | 2007-08-28 02:27:51 +0000 | [diff] [blame] | 3064 | rc = SQLITE_DONE; |
drh | 83968c4 | 2007-04-18 16:45:24 +0000 | [diff] [blame] | 3065 | }else{ |
drh | 900b31e | 2007-08-28 02:27:51 +0000 | [diff] [blame] | 3066 | rc = SQLITE_ERROR; |
drh | 83968c4 | 2007-04-18 16:45:24 +0000 | [diff] [blame] | 3067 | } |
drh | 900b31e | 2007-08-28 02:27:51 +0000 | [diff] [blame] | 3068 | goto vdbe_return; |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 3069 | }else{ |
drh | 22c17b8 | 2015-05-15 04:13:15 +0000 | [diff] [blame] | 3070 | sqlite3VdbeError(p, |
drh | ad4a4b8 | 2008-11-05 16:37:34 +0000 | [diff] [blame] | 3071 | (!desiredAutoCommit)?"cannot start a transaction within a transaction":( |
shane | 68c0273 | 2009-06-09 18:14:18 +0000 | [diff] [blame] | 3072 | (iRollback)?"cannot rollback - no transaction is active": |
drh | f089aa4 | 2008-07-08 19:34:06 +0000 | [diff] [blame] | 3073 | "cannot commit - no transaction is active")); |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 3074 | |
| 3075 | rc = SQLITE_ERROR; |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 3076 | goto abort_due_to_error; |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 3077 | } |
| 3078 | break; |
| 3079 | } |
| 3080 | |
drh | b22f7c8 | 2014-02-06 23:56:27 +0000 | [diff] [blame] | 3081 | /* Opcode: Transaction P1 P2 P3 P4 P5 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3082 | ** |
drh | 05a86c5 | 2014-02-16 01:55:49 +0000 | [diff] [blame] | 3083 | ** Begin a transaction on database P1 if a transaction is not already |
| 3084 | ** active. |
| 3085 | ** If P2 is non-zero, then a write-transaction is started, or if a |
| 3086 | ** read-transaction is already active, it is upgraded to a write-transaction. |
| 3087 | ** If P2 is zero, then a read-transaction is started. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3088 | ** |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 3089 | ** P1 is the index of the database file on which the transaction is |
| 3090 | ** started. Index 0 is the main database file and index 1 is the |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 3091 | ** file used for temporary tables. Indices of 2 or more are used for |
| 3092 | ** attached databases. |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 3093 | ** |
dan | e0af83a | 2009-09-08 19:15:01 +0000 | [diff] [blame] | 3094 | ** If a write-transaction is started and the Vdbe.usesStmtJournal flag is |
| 3095 | ** true (this flag is set if the Vdbe may modify more than one row and may |
| 3096 | ** throw an ABORT exception), a statement transaction may also be opened. |
| 3097 | ** More specifically, a statement transaction is opened iff the database |
| 3098 | ** connection is currently not in autocommit mode, or if there are other |
drh | a451017 | 2012-02-02 15:50:17 +0000 | [diff] [blame] | 3099 | ** active statements. A statement transaction allows the changes made by this |
dan | e0af83a | 2009-09-08 19:15:01 +0000 | [diff] [blame] | 3100 | ** VDBE to be rolled back after an error without having to roll back the |
| 3101 | ** entire transaction. If no error is encountered, the statement transaction |
| 3102 | ** will automatically commit when the VDBE halts. |
| 3103 | ** |
drh | b22f7c8 | 2014-02-06 23:56:27 +0000 | [diff] [blame] | 3104 | ** If P5!=0 then this opcode also checks the schema cookie against P3 |
| 3105 | ** and the schema generation counter against P4. |
| 3106 | ** The cookie changes its value whenever the database schema changes. |
| 3107 | ** This operation is used to detect when that the cookie has changed |
drh | 05a86c5 | 2014-02-16 01:55:49 +0000 | [diff] [blame] | 3108 | ** and that the current process needs to reread the schema. If the schema |
| 3109 | ** cookie in P3 differs from the schema cookie in the database header or |
| 3110 | ** if the schema generation counter in P4 differs from the current |
| 3111 | ** generation counter, then an SQLITE_SCHEMA error is raised and execution |
| 3112 | ** halts. The sqlite3_step() wrapper function might then reprepare the |
| 3113 | ** statement and rerun it from the beginning. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3114 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3115 | case OP_Transaction: { |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 3116 | Btree *pBt; |
drh | b22f7c8 | 2014-02-06 23:56:27 +0000 | [diff] [blame] | 3117 | int iMeta; |
| 3118 | int iGen; |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 3119 | |
drh | 1713afb | 2013-06-28 01:24:57 +0000 | [diff] [blame] | 3120 | assert( p->bIsReader ); |
drh | 9e92a47 | 2013-06-27 17:40:30 +0000 | [diff] [blame] | 3121 | assert( p->readOnly==0 || pOp->p2==0 ); |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 3122 | assert( pOp->p1>=0 && pOp->p1<db->nDb ); |
drh | a7ab6d8 | 2014-07-21 15:44:39 +0000 | [diff] [blame] | 3123 | assert( DbMaskTest(p->btreeMask, pOp->p1) ); |
drh | 13447bf | 2013-07-10 13:33:49 +0000 | [diff] [blame] | 3124 | if( pOp->p2 && (db->flags & SQLITE_QueryOnly)!=0 ){ |
| 3125 | rc = SQLITE_READONLY; |
| 3126 | goto abort_due_to_error; |
| 3127 | } |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 3128 | pBt = db->aDb[pOp->p1].pBt; |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 3129 | |
danielk1977 | 24162fe | 2004-06-04 06:22:00 +0000 | [diff] [blame] | 3130 | if( pBt ){ |
danielk1977 | 40b38dc | 2004-06-26 08:38:24 +0000 | [diff] [blame] | 3131 | rc = sqlite3BtreeBeginTrans(pBt, pOp->p2); |
drh | cbd8db3 | 2015-08-20 17:18:32 +0000 | [diff] [blame] | 3132 | testcase( rc==SQLITE_BUSY_SNAPSHOT ); |
| 3133 | testcase( rc==SQLITE_BUSY_RECOVERY ); |
| 3134 | if( (rc&0xff)==SQLITE_BUSY ){ |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 3135 | p->pc = (int)(pOp - aOp); |
drh | cbd8db3 | 2015-08-20 17:18:32 +0000 | [diff] [blame] | 3136 | p->rc = rc; |
drh | 900b31e | 2007-08-28 02:27:51 +0000 | [diff] [blame] | 3137 | goto vdbe_return; |
danielk1977 | 24162fe | 2004-06-04 06:22:00 +0000 | [diff] [blame] | 3138 | } |
drh | 9e9f1bd | 2009-10-13 15:36:51 +0000 | [diff] [blame] | 3139 | if( rc!=SQLITE_OK ){ |
danielk1977 | 24162fe | 2004-06-04 06:22:00 +0000 | [diff] [blame] | 3140 | goto abort_due_to_error; |
drh | 90bfcda | 2001-09-23 19:46:51 +0000 | [diff] [blame] | 3141 | } |
dan | e0af83a | 2009-09-08 19:15:01 +0000 | [diff] [blame] | 3142 | |
| 3143 | if( pOp->p2 && p->usesStmtJournal |
dan | c0537fe | 2013-06-28 19:41:43 +0000 | [diff] [blame] | 3144 | && (db->autoCommit==0 || db->nVdbeRead>1) |
dan | e0af83a | 2009-09-08 19:15:01 +0000 | [diff] [blame] | 3145 | ){ |
| 3146 | assert( sqlite3BtreeIsInTrans(pBt) ); |
| 3147 | if( p->iStatement==0 ){ |
| 3148 | assert( db->nStatement>=0 && db->nSavepoint>=0 ); |
| 3149 | db->nStatement++; |
| 3150 | p->iStatement = db->nSavepoint + db->nStatement; |
| 3151 | } |
dan | a311b80 | 2011-04-26 19:21:34 +0000 | [diff] [blame] | 3152 | |
drh | 346506f | 2011-05-25 01:16:42 +0000 | [diff] [blame] | 3153 | rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN, p->iStatement-1); |
dan | a311b80 | 2011-04-26 19:21:34 +0000 | [diff] [blame] | 3154 | if( rc==SQLITE_OK ){ |
| 3155 | rc = sqlite3BtreeBeginStmt(pBt, p->iStatement); |
| 3156 | } |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 3157 | |
| 3158 | /* Store the current value of the database handles deferred constraint |
| 3159 | ** counter. If the statement transaction needs to be rolled back, |
| 3160 | ** the value of this counter needs to be restored too. */ |
| 3161 | p->nStmtDefCons = db->nDeferredCons; |
dan | cb3e4b7 | 2013-07-03 19:53:05 +0000 | [diff] [blame] | 3162 | p->nStmtDefImmCons = db->nDeferredImmCons; |
dan | e0af83a | 2009-09-08 19:15:01 +0000 | [diff] [blame] | 3163 | } |
drh | b22f7c8 | 2014-02-06 23:56:27 +0000 | [diff] [blame] | 3164 | |
drh | 51a74d4 | 2015-02-28 01:04:27 +0000 | [diff] [blame] | 3165 | /* Gather the schema version number for checking: |
| 3166 | ** IMPLEMENTATION-OF: R-32195-19465 The schema version is used by SQLite |
| 3167 | ** each time a query is executed to ensure that the internal cache of the |
| 3168 | ** schema used when compiling the SQL query matches the schema of the |
| 3169 | ** database against which the compiled query is actually executed. |
| 3170 | */ |
drh | b22f7c8 | 2014-02-06 23:56:27 +0000 | [diff] [blame] | 3171 | sqlite3BtreeGetMeta(pBt, BTREE_SCHEMA_VERSION, (u32 *)&iMeta); |
| 3172 | iGen = db->aDb[pOp->p1].pSchema->iGeneration; |
| 3173 | }else{ |
| 3174 | iGen = iMeta = 0; |
| 3175 | } |
| 3176 | assert( pOp->p5==0 || pOp->p4type==P4_INT32 ); |
| 3177 | if( pOp->p5 && (iMeta!=pOp->p3 || iGen!=pOp->p4.i) ){ |
| 3178 | sqlite3DbFree(db, p->zErrMsg); |
| 3179 | p->zErrMsg = sqlite3DbStrDup(db, "database schema has changed"); |
| 3180 | /* If the schema-cookie from the database file matches the cookie |
| 3181 | ** stored with the in-memory representation of the schema, do |
| 3182 | ** not reload the schema from the database file. |
| 3183 | ** |
| 3184 | ** If virtual-tables are in use, this is not just an optimization. |
| 3185 | ** Often, v-tables store their data in other SQLite tables, which |
| 3186 | ** are queried from within xNext() and other v-table methods using |
| 3187 | ** prepared queries. If such a query is out-of-date, we do not want to |
| 3188 | ** discard the database schema, as the user code implementing the |
| 3189 | ** v-table would have to be ready for the sqlite3_vtab structure itself |
| 3190 | ** to be invalidated whenever sqlite3_step() is called from within |
| 3191 | ** a v-table method. |
| 3192 | */ |
| 3193 | if( db->aDb[pOp->p1].pSchema->schema_cookie!=iMeta ){ |
| 3194 | sqlite3ResetOneSchema(db, pOp->p1); |
| 3195 | } |
| 3196 | p->expired = 1; |
| 3197 | rc = SQLITE_SCHEMA; |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 3198 | } |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 3199 | if( rc ) goto abort_due_to_error; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3200 | break; |
| 3201 | } |
| 3202 | |
drh | b1fdb2a | 2008-01-05 04:06:03 +0000 | [diff] [blame] | 3203 | /* Opcode: ReadCookie P1 P2 P3 * * |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 3204 | ** |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3205 | ** Read cookie number P3 from database P1 and write it into register P2. |
danielk1977 | 0d19f7a | 2009-06-03 11:25:07 +0000 | [diff] [blame] | 3206 | ** P3==1 is the schema version. P3==2 is the database format. |
| 3207 | ** 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] | 3208 | ** the main database file and P1==1 is the database file used to store |
| 3209 | ** temporary tables. |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 3210 | ** |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 3211 | ** There must be a read-lock on the database (either a transaction |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 3212 | ** must be started or there must be an open cursor) before |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 3213 | ** executing this instruction. |
| 3214 | */ |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 3215 | case OP_ReadCookie: { /* out2 */ |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 3216 | int iMeta; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3217 | int iDb; |
| 3218 | int iCookie; |
danielk1977 | 180b56a | 2007-06-24 08:00:42 +0000 | [diff] [blame] | 3219 | |
drh | 1713afb | 2013-06-28 01:24:57 +0000 | [diff] [blame] | 3220 | assert( p->bIsReader ); |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3221 | iDb = pOp->p1; |
| 3222 | iCookie = pOp->p3; |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 3223 | assert( pOp->p3<SQLITE_N_BTREE_META ); |
danielk1977 | 180b56a | 2007-06-24 08:00:42 +0000 | [diff] [blame] | 3224 | assert( iDb>=0 && iDb<db->nDb ); |
| 3225 | assert( db->aDb[iDb].pBt!=0 ); |
drh | a7ab6d8 | 2014-07-21 15:44:39 +0000 | [diff] [blame] | 3226 | assert( DbMaskTest(p->btreeMask, iDb) ); |
danielk1977 | 0d19f7a | 2009-06-03 11:25:07 +0000 | [diff] [blame] | 3227 | |
danielk1977 | 602b466 | 2009-07-02 07:47:33 +0000 | [diff] [blame] | 3228 | sqlite3BtreeGetMeta(db->aDb[iDb].pBt, iCookie, (u32 *)&iMeta); |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 3229 | pOut = out2Prerelease(p, pOp); |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 3230 | pOut->u.i = iMeta; |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 3231 | break; |
| 3232 | } |
| 3233 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 3234 | /* Opcode: SetCookie P1 P2 P3 * * |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 3235 | ** |
drh | 1861afc | 2016-02-01 21:48:34 +0000 | [diff] [blame] | 3236 | ** Write the integer value P3 into cookie number P2 of database P1. |
| 3237 | ** P2==1 is the schema version. P2==2 is the database format. |
| 3238 | ** P2==3 is the recommended pager cache |
danielk1977 | 0d19f7a | 2009-06-03 11:25:07 +0000 | [diff] [blame] | 3239 | ** size, and so forth. P1==0 is the main database file and P1==1 is the |
| 3240 | ** database file used to store temporary tables. |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 3241 | ** |
| 3242 | ** A transaction must be started before executing this opcode. |
| 3243 | */ |
drh | 1861afc | 2016-02-01 21:48:34 +0000 | [diff] [blame] | 3244 | case OP_SetCookie: { |
drh | 3f7d4e4 | 2004-07-24 14:35:58 +0000 | [diff] [blame] | 3245 | Db *pDb; |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 3246 | assert( pOp->p2<SQLITE_N_BTREE_META ); |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 3247 | assert( pOp->p1>=0 && pOp->p1<db->nDb ); |
drh | a7ab6d8 | 2014-07-21 15:44:39 +0000 | [diff] [blame] | 3248 | assert( DbMaskTest(p->btreeMask, pOp->p1) ); |
drh | 9e92a47 | 2013-06-27 17:40:30 +0000 | [diff] [blame] | 3249 | assert( p->readOnly==0 ); |
drh | 3f7d4e4 | 2004-07-24 14:35:58 +0000 | [diff] [blame] | 3250 | pDb = &db->aDb[pOp->p1]; |
| 3251 | assert( pDb->pBt!=0 ); |
drh | 2120608 | 2011-04-04 18:22:02 +0000 | [diff] [blame] | 3252 | assert( sqlite3SchemaMutexHeld(db, pOp->p1, 0) ); |
drh | a3b321d | 2004-05-11 09:31:31 +0000 | [diff] [blame] | 3253 | /* See note about index shifting on OP_ReadCookie */ |
drh | 1861afc | 2016-02-01 21:48:34 +0000 | [diff] [blame] | 3254 | rc = sqlite3BtreeUpdateMeta(pDb->pBt, pOp->p2, pOp->p3); |
danielk1977 | 0d19f7a | 2009-06-03 11:25:07 +0000 | [diff] [blame] | 3255 | if( pOp->p2==BTREE_SCHEMA_VERSION ){ |
drh | 3f7d4e4 | 2004-07-24 14:35:58 +0000 | [diff] [blame] | 3256 | /* When the schema cookie changes, record the new cookie internally */ |
drh | 1861afc | 2016-02-01 21:48:34 +0000 | [diff] [blame] | 3257 | pDb->pSchema->schema_cookie = pOp->p3; |
drh | 3f7d4e4 | 2004-07-24 14:35:58 +0000 | [diff] [blame] | 3258 | db->flags |= SQLITE_InternChanges; |
danielk1977 | 0d19f7a | 2009-06-03 11:25:07 +0000 | [diff] [blame] | 3259 | }else if( pOp->p2==BTREE_FILE_FORMAT ){ |
drh | d28bcb3 | 2005-12-21 14:43:11 +0000 | [diff] [blame] | 3260 | /* Record changes in the file format */ |
drh | 1861afc | 2016-02-01 21:48:34 +0000 | [diff] [blame] | 3261 | pDb->pSchema->file_format = pOp->p3; |
drh | 3f7d4e4 | 2004-07-24 14:35:58 +0000 | [diff] [blame] | 3262 | } |
drh | fd426c6 | 2006-01-30 15:34:22 +0000 | [diff] [blame] | 3263 | if( pOp->p1==1 ){ |
| 3264 | /* Invalidate all prepared statements whenever the TEMP database |
| 3265 | ** schema is changed. Ticket #1644 */ |
| 3266 | sqlite3ExpirePreparedStatements(db); |
dan | fa401de | 2009-10-16 14:55:03 +0000 | [diff] [blame] | 3267 | p->expired = 0; |
drh | fd426c6 | 2006-01-30 15:34:22 +0000 | [diff] [blame] | 3268 | } |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 3269 | if( rc ) goto abort_due_to_error; |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 3270 | break; |
| 3271 | } |
| 3272 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 3273 | /* Opcode: OpenRead P1 P2 P3 P4 P5 |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 3274 | ** Synopsis: root=P2 iDb=P3 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3275 | ** |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 3276 | ** Open a read-only cursor for the database table whose root page is |
danielk1977 | 207872a | 2008-01-03 07:54:23 +0000 | [diff] [blame] | 3277 | ** P2 in a database file. The database file is determined by P3. |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 3278 | ** P3==0 means the main database, P3==1 means the database used for |
| 3279 | ** temporary tables, and P3>1 means used the corresponding attached |
| 3280 | ** database. Give the new cursor an identifier of P1. The P1 |
danielk1977 | 207872a | 2008-01-03 07:54:23 +0000 | [diff] [blame] | 3281 | ** values need not be contiguous but all P1 values should be small integers. |
| 3282 | ** It is an error for P1 to be negative. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3283 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 3284 | ** If P5!=0 then use the content of register P2 as the root page, not |
| 3285 | ** the value of P2 itself. |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 3286 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 3287 | ** There will be a read lock on the database whenever there is an |
| 3288 | ** open cursor. If the database was unlocked prior to this instruction |
| 3289 | ** then a read lock is acquired as part of this instruction. A read |
| 3290 | ** lock allows other processes to read the database but prohibits |
| 3291 | ** any other process from modifying the database. The read lock is |
| 3292 | ** released when all cursors are closed. If this instruction attempts |
| 3293 | ** to get a read lock but fails, the script terminates with an |
| 3294 | ** SQLITE_BUSY error code. |
| 3295 | ** |
danielk1977 | d336e22 | 2009-02-20 10:58:41 +0000 | [diff] [blame] | 3296 | ** The P4 value may be either an integer (P4_INT32) or a pointer to |
| 3297 | ** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo |
| 3298 | ** structure, then said structure defines the content and collating |
| 3299 | ** sequence of the index being opened. Otherwise, if P4 is an integer |
| 3300 | ** value, it is set to the number of columns in the table. |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 3301 | ** |
drh | 3526319 | 2014-07-22 20:02:19 +0000 | [diff] [blame] | 3302 | ** See also: OpenWrite, ReopenIdx |
| 3303 | */ |
| 3304 | /* Opcode: ReopenIdx P1 P2 P3 P4 P5 |
| 3305 | ** Synopsis: root=P2 iDb=P3 |
| 3306 | ** |
| 3307 | ** The ReopenIdx opcode works exactly like ReadOpen except that it first |
| 3308 | ** checks to see if the cursor on P1 is already open with a root page |
| 3309 | ** number of P2 and if it is this opcode becomes a no-op. In other words, |
| 3310 | ** if the cursor is already open, do not reopen it. |
| 3311 | ** |
| 3312 | ** The ReopenIdx opcode may only be used with P5==0 and with P4 being |
| 3313 | ** a P4_KEYINFO object. Furthermore, the P3 value must be the same as |
| 3314 | ** every other ReopenIdx or OpenRead for the same cursor number. |
| 3315 | ** |
| 3316 | ** See the OpenRead opcode documentation for additional information. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3317 | */ |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 3318 | /* Opcode: OpenWrite P1 P2 P3 P4 P5 |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 3319 | ** Synopsis: root=P2 iDb=P3 |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 3320 | ** |
| 3321 | ** 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] | 3322 | ** page is P2. Or if P5!=0 use the content of register P2 to find the |
| 3323 | ** root page. |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 3324 | ** |
danielk1977 | d336e22 | 2009-02-20 10:58:41 +0000 | [diff] [blame] | 3325 | ** The P4 value may be either an integer (P4_INT32) or a pointer to |
| 3326 | ** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo |
| 3327 | ** structure, then said structure defines the content and collating |
| 3328 | ** sequence of the index being opened. Otherwise, if P4 is an integer |
drh | 35cd643 | 2009-06-05 14:17:21 +0000 | [diff] [blame] | 3329 | ** value, it is set to the number of columns in the table, or to the |
| 3330 | ** largest index of any column of the table that is actually used. |
jplyon | 5a56422 | 2003-06-02 06:15:58 +0000 | [diff] [blame] | 3331 | ** |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 3332 | ** This instruction works just like OpenRead except that it opens the cursor |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 3333 | ** in read/write mode. For a given table, there can be one or more read-only |
| 3334 | ** cursors or a single read/write cursor but not both. |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 3335 | ** |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 3336 | ** See also OpenRead. |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 3337 | */ |
drh | 3526319 | 2014-07-22 20:02:19 +0000 | [diff] [blame] | 3338 | case OP_ReopenIdx: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3339 | int nField; |
| 3340 | KeyInfo *pKeyInfo; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3341 | int p2; |
| 3342 | int iDb; |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 3343 | int wrFlag; |
| 3344 | Btree *pX; |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 3345 | VdbeCursor *pCur; |
drh | d946db0 | 2005-12-29 19:23:06 +0000 | [diff] [blame] | 3346 | Db *pDb; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3347 | |
drh | e0997b3 | 2015-03-20 14:57:50 +0000 | [diff] [blame] | 3348 | assert( pOp->p5==0 || pOp->p5==OPFLAG_SEEKEQ ); |
drh | 3526319 | 2014-07-22 20:02:19 +0000 | [diff] [blame] | 3349 | assert( pOp->p4type==P4_KEYINFO ); |
| 3350 | pCur = p->apCsr[pOp->p1]; |
drh | e8f2c9d | 2014-08-06 17:49:13 +0000 | [diff] [blame] | 3351 | if( pCur && pCur->pgnoRoot==(u32)pOp->p2 ){ |
drh | 3526319 | 2014-07-22 20:02:19 +0000 | [diff] [blame] | 3352 | assert( pCur->iDb==pOp->p3 ); /* Guaranteed by the code generator */ |
drh | e0997b3 | 2015-03-20 14:57:50 +0000 | [diff] [blame] | 3353 | goto open_cursor_set_hints; |
drh | 3526319 | 2014-07-22 20:02:19 +0000 | [diff] [blame] | 3354 | } |
| 3355 | /* If the cursor is not currently open or is open on a different |
| 3356 | ** index, then fall through into OP_OpenRead to force a reopen */ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3357 | case OP_OpenRead: |
drh | 1fa509a | 2015-03-20 16:34:49 +0000 | [diff] [blame] | 3358 | case OP_OpenWrite: |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3359 | |
drh | e0997b3 | 2015-03-20 14:57:50 +0000 | [diff] [blame] | 3360 | assert( pOp->opcode==OP_OpenWrite || pOp->p5==0 || pOp->p5==OPFLAG_SEEKEQ ); |
drh | 1713afb | 2013-06-28 01:24:57 +0000 | [diff] [blame] | 3361 | assert( p->bIsReader ); |
drh | 3526319 | 2014-07-22 20:02:19 +0000 | [diff] [blame] | 3362 | assert( pOp->opcode==OP_OpenRead || pOp->opcode==OP_ReopenIdx |
| 3363 | || p->readOnly==0 ); |
dan | 428c218 | 2012-08-06 18:50:11 +0000 | [diff] [blame] | 3364 | |
dan | fa401de | 2009-10-16 14:55:03 +0000 | [diff] [blame] | 3365 | if( p->expired ){ |
drh | 47b7fc7 | 2014-11-11 01:33:57 +0000 | [diff] [blame] | 3366 | rc = SQLITE_ABORT_ROLLBACK; |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 3367 | goto abort_due_to_error; |
dan | fa401de | 2009-10-16 14:55:03 +0000 | [diff] [blame] | 3368 | } |
| 3369 | |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3370 | nField = 0; |
| 3371 | pKeyInfo = 0; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3372 | p2 = pOp->p2; |
| 3373 | iDb = pOp->p3; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3374 | assert( iDb>=0 && iDb<db->nDb ); |
drh | a7ab6d8 | 2014-07-21 15:44:39 +0000 | [diff] [blame] | 3375 | assert( DbMaskTest(p->btreeMask, iDb) ); |
drh | d946db0 | 2005-12-29 19:23:06 +0000 | [diff] [blame] | 3376 | pDb = &db->aDb[iDb]; |
| 3377 | pX = pDb->pBt; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3378 | assert( pX!=0 ); |
drh | d946db0 | 2005-12-29 19:23:06 +0000 | [diff] [blame] | 3379 | if( pOp->opcode==OP_OpenWrite ){ |
dan | fd261ec | 2015-10-22 20:54:33 +0000 | [diff] [blame] | 3380 | assert( OPFLAG_FORDELETE==BTREE_FORDELETE ); |
| 3381 | wrFlag = BTREE_WRCSR | (pOp->p5 & OPFLAG_FORDELETE); |
drh | 2120608 | 2011-04-04 18:22:02 +0000 | [diff] [blame] | 3382 | assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3383 | if( pDb->pSchema->file_format < p->minWriteFileFormat ){ |
| 3384 | p->minWriteFileFormat = pDb->pSchema->file_format; |
drh | d946db0 | 2005-12-29 19:23:06 +0000 | [diff] [blame] | 3385 | } |
| 3386 | }else{ |
| 3387 | wrFlag = 0; |
| 3388 | } |
dan | 428c218 | 2012-08-06 18:50:11 +0000 | [diff] [blame] | 3389 | if( pOp->p5 & OPFLAG_P2ISREG ){ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3390 | assert( p2>0 ); |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 3391 | assert( p2<=(p->nMem+1 - p->nCursor) ); |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 3392 | pIn2 = &aMem[p2]; |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 3393 | assert( memIsValid(pIn2) ); |
| 3394 | assert( (pIn2->flags & MEM_Int)!=0 ); |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3395 | sqlite3VdbeMemIntegerify(pIn2); |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 3396 | p2 = (int)pIn2->u.i; |
drh | 9a65f2c | 2009-06-22 19:05:40 +0000 | [diff] [blame] | 3397 | /* The p2 value always comes from a prior OP_CreateTable opcode and |
| 3398 | ** that opcode will always set the p2 value to 2 or more or else fail. |
| 3399 | ** If there were a failure, the prepared statement would have halted |
| 3400 | ** before reaching this instruction. */ |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 3401 | assert( p2>=2 ); |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 3402 | } |
danielk1977 | d336e22 | 2009-02-20 10:58:41 +0000 | [diff] [blame] | 3403 | if( pOp->p4type==P4_KEYINFO ){ |
| 3404 | pKeyInfo = pOp->p4.pKeyInfo; |
drh | 41e13e1 | 2013-11-07 14:09:39 +0000 | [diff] [blame] | 3405 | assert( pKeyInfo->enc==ENC(db) ); |
| 3406 | assert( pKeyInfo->db==db ); |
drh | ad12432 | 2013-10-23 13:30:58 +0000 | [diff] [blame] | 3407 | nField = pKeyInfo->nField+pKeyInfo->nXField; |
danielk1977 | d336e22 | 2009-02-20 10:58:41 +0000 | [diff] [blame] | 3408 | }else if( pOp->p4type==P4_INT32 ){ |
| 3409 | nField = pOp->p4.i; |
| 3410 | } |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 3411 | assert( pOp->p1>=0 ); |
drh | 399af1d | 2013-11-20 17:25:55 +0000 | [diff] [blame] | 3412 | assert( nField>=0 ); |
| 3413 | testcase( nField==0 ); /* Table with INTEGER PRIMARY KEY and nothing else */ |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 3414 | pCur = allocateCursor(p, pOp->p1, nField, iDb, CURTYPE_BTREE); |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 3415 | if( pCur==0 ) goto no_mem; |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 3416 | pCur->nullRow = 1; |
drh | d4187c7 | 2010-08-30 22:15:45 +0000 | [diff] [blame] | 3417 | pCur->isOrdered = 1; |
drh | 3526319 | 2014-07-22 20:02:19 +0000 | [diff] [blame] | 3418 | pCur->pgnoRoot = p2; |
drh | b89aeb6 | 2016-01-27 15:49:32 +0000 | [diff] [blame] | 3419 | #ifdef SQLITE_DEBUG |
| 3420 | pCur->wrFlag = wrFlag; |
| 3421 | #endif |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 3422 | rc = sqlite3BtreeCursor(pX, p2, wrFlag, pKeyInfo, pCur->uc.pCursor); |
danielk1977 | d336e22 | 2009-02-20 10:58:41 +0000 | [diff] [blame] | 3423 | pCur->pKeyInfo = pKeyInfo; |
drh | 14da87f | 2013-11-20 21:51:33 +0000 | [diff] [blame] | 3424 | /* Set the VdbeCursor.isTable variable. Previous versions of |
danielk1977 | 172114a | 2009-07-07 15:47:12 +0000 | [diff] [blame] | 3425 | ** SQLite used to check if the root-page flags were sane at this point |
| 3426 | ** and report database corruption if they were not, but this check has |
| 3427 | ** since moved into the btree layer. */ |
| 3428 | pCur->isTable = pOp->p4type!=P4_KEYINFO; |
drh | e0997b3 | 2015-03-20 14:57:50 +0000 | [diff] [blame] | 3429 | |
| 3430 | open_cursor_set_hints: |
| 3431 | assert( OPFLAG_BULKCSR==BTREE_BULKLOAD ); |
| 3432 | assert( OPFLAG_SEEKEQ==BTREE_SEEK_EQ ); |
drh | 0403cb3 | 2015-08-14 23:57:04 +0000 | [diff] [blame] | 3433 | testcase( pOp->p5 & OPFLAG_BULKCSR ); |
drh | 9abe841 | 2016-01-02 05:00:31 +0000 | [diff] [blame] | 3434 | #ifdef SQLITE_ENABLE_CURSOR_HINTS |
drh | 0403cb3 | 2015-08-14 23:57:04 +0000 | [diff] [blame] | 3435 | testcase( pOp->p2 & OPFLAG_SEEKEQ ); |
| 3436 | #endif |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 3437 | sqlite3BtreeCursorHintFlags(pCur->uc.pCursor, |
drh | f7854c7 | 2015-10-27 13:24:37 +0000 | [diff] [blame] | 3438 | (pOp->p5 & (OPFLAG_BULKCSR|OPFLAG_SEEKEQ))); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 3439 | if( rc ) goto abort_due_to_error; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3440 | break; |
| 3441 | } |
| 3442 | |
drh | 2a5d990 | 2011-08-26 00:34:45 +0000 | [diff] [blame] | 3443 | /* Opcode: OpenEphemeral P1 P2 * P4 P5 |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 3444 | ** Synopsis: nColumn=P2 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3445 | ** |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 3446 | ** Open a new cursor P1 to a transient table. |
drh | 9170dd7 | 2005-07-08 17:13:46 +0000 | [diff] [blame] | 3447 | ** The cursor is always opened read/write even if |
drh | 25d3adb | 2010-04-05 15:11:08 +0000 | [diff] [blame] | 3448 | ** the main database is read-only. The ephemeral |
drh | 9170dd7 | 2005-07-08 17:13:46 +0000 | [diff] [blame] | 3449 | ** table is deleted automatically when the cursor is closed. |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 3450 | ** |
drh | 25d3adb | 2010-04-05 15:11:08 +0000 | [diff] [blame] | 3451 | ** P2 is the number of columns in the ephemeral table. |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 3452 | ** The cursor points to a BTree table if P4==0 and to a BTree index |
| 3453 | ** 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] | 3454 | ** that defines the format of keys in the index. |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 3455 | ** |
drh | 2a5d990 | 2011-08-26 00:34:45 +0000 | [diff] [blame] | 3456 | ** The P5 parameter can be a mask of the BTREE_* flags defined |
| 3457 | ** in btree.h. These flags control aspects of the operation of |
| 3458 | ** the btree. The BTREE_OMIT_JOURNAL and BTREE_SINGLE flags are |
| 3459 | ** added automatically. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3460 | */ |
drh | a21a64d | 2010-04-06 22:33:55 +0000 | [diff] [blame] | 3461 | /* Opcode: OpenAutoindex P1 P2 * P4 * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 3462 | ** Synopsis: nColumn=P2 |
drh | a21a64d | 2010-04-06 22:33:55 +0000 | [diff] [blame] | 3463 | ** |
| 3464 | ** This opcode works the same as OP_OpenEphemeral. It has a |
| 3465 | ** different name to distinguish its use. Tables created using |
| 3466 | ** by this opcode will be used for automatically created transient |
| 3467 | ** indices in joins. |
| 3468 | */ |
| 3469 | case OP_OpenAutoindex: |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3470 | case OP_OpenEphemeral: { |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 3471 | VdbeCursor *pCx; |
drh | 41e13e1 | 2013-11-07 14:09:39 +0000 | [diff] [blame] | 3472 | KeyInfo *pKeyInfo; |
| 3473 | |
drh | d4187c7 | 2010-08-30 22:15:45 +0000 | [diff] [blame] | 3474 | static const int vfsFlags = |
drh | 33f4e02 | 2007-09-03 15:19:34 +0000 | [diff] [blame] | 3475 | SQLITE_OPEN_READWRITE | |
| 3476 | SQLITE_OPEN_CREATE | |
| 3477 | SQLITE_OPEN_EXCLUSIVE | |
| 3478 | SQLITE_OPEN_DELETEONCLOSE | |
| 3479 | SQLITE_OPEN_TRANSIENT_DB; |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 3480 | assert( pOp->p1>=0 ); |
drh | 399af1d | 2013-11-20 17:25:55 +0000 | [diff] [blame] | 3481 | assert( pOp->p2>=0 ); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 3482 | pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, CURTYPE_BTREE); |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 3483 | if( pCx==0 ) goto no_mem; |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 3484 | pCx->nullRow = 1; |
drh | 079a307 | 2014-03-19 14:10:55 +0000 | [diff] [blame] | 3485 | pCx->isEphemeral = 1; |
dan | 3a6d8ae | 2011-04-23 15:54:54 +0000 | [diff] [blame] | 3486 | rc = sqlite3BtreeOpen(db->pVfs, 0, db, &pCx->pBt, |
drh | d4187c7 | 2010-08-30 22:15:45 +0000 | [diff] [blame] | 3487 | BTREE_OMIT_JOURNAL | BTREE_SINGLE | pOp->p5, vfsFlags); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3488 | if( rc==SQLITE_OK ){ |
danielk1977 | 40b38dc | 2004-06-26 08:38:24 +0000 | [diff] [blame] | 3489 | rc = sqlite3BtreeBeginTrans(pCx->pBt, 1); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3490 | } |
| 3491 | if( rc==SQLITE_OK ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3492 | /* If a transient index is required, create it by calling |
drh | d4187c7 | 2010-08-30 22:15:45 +0000 | [diff] [blame] | 3493 | ** sqlite3BtreeCreateTable() with the BTREE_BLOBKEY flag before |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3494 | ** opening it. If a transient table is required, just use the |
drh | d4187c7 | 2010-08-30 22:15:45 +0000 | [diff] [blame] | 3495 | ** automatically created table with root-page 1 (an BLOB_INTKEY table). |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3496 | */ |
drh | 41e13e1 | 2013-11-07 14:09:39 +0000 | [diff] [blame] | 3497 | if( (pKeyInfo = pOp->p4.pKeyInfo)!=0 ){ |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 3498 | int pgno; |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 3499 | assert( pOp->p4type==P4_KEYINFO ); |
drh | e1b4f0f | 2011-06-29 17:11:39 +0000 | [diff] [blame] | 3500 | rc = sqlite3BtreeCreateTable(pCx->pBt, &pgno, BTREE_BLOBKEY | pOp->p5); |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 3501 | if( rc==SQLITE_OK ){ |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 3502 | assert( pgno==MASTER_ROOT+1 ); |
drh | 41e13e1 | 2013-11-07 14:09:39 +0000 | [diff] [blame] | 3503 | assert( pKeyInfo->db==db ); |
| 3504 | assert( pKeyInfo->enc==ENC(db) ); |
| 3505 | pCx->pKeyInfo = pKeyInfo; |
drh | 62aaa6c | 2015-11-21 17:27:42 +0000 | [diff] [blame] | 3506 | rc = sqlite3BtreeCursor(pCx->pBt, pgno, BTREE_WRCSR, |
| 3507 | pKeyInfo, pCx->uc.pCursor); |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 3508 | } |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3509 | pCx->isTable = 0; |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 3510 | }else{ |
drh | 62aaa6c | 2015-11-21 17:27:42 +0000 | [diff] [blame] | 3511 | rc = sqlite3BtreeCursor(pCx->pBt, MASTER_ROOT, BTREE_WRCSR, |
| 3512 | 0, pCx->uc.pCursor); |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3513 | pCx->isTable = 1; |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 3514 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3515 | } |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 3516 | if( rc ) goto abort_due_to_error; |
drh | d4187c7 | 2010-08-30 22:15:45 +0000 | [diff] [blame] | 3517 | pCx->isOrdered = (pOp->p5!=BTREE_UNORDERED); |
dan | 5134d13 | 2011-09-02 10:31:11 +0000 | [diff] [blame] | 3518 | break; |
| 3519 | } |
| 3520 | |
dan | fad9f9a | 2014-04-01 18:41:51 +0000 | [diff] [blame] | 3521 | /* Opcode: SorterOpen P1 P2 P3 P4 * |
dan | 5134d13 | 2011-09-02 10:31:11 +0000 | [diff] [blame] | 3522 | ** |
| 3523 | ** This opcode works like OP_OpenEphemeral except that it opens |
| 3524 | ** a transient index that is specifically designed to sort large |
| 3525 | ** tables using an external merge-sort algorithm. |
dan | fad9f9a | 2014-04-01 18:41:51 +0000 | [diff] [blame] | 3526 | ** |
| 3527 | ** If argument P3 is non-zero, then it indicates that the sorter may |
| 3528 | ** assume that a stable sort considering the first P3 fields of each |
| 3529 | ** key is sufficient to produce the required results. |
dan | 5134d13 | 2011-09-02 10:31:11 +0000 | [diff] [blame] | 3530 | */ |
drh | ca892a7 | 2011-09-03 00:17:51 +0000 | [diff] [blame] | 3531 | case OP_SorterOpen: { |
dan | 5134d13 | 2011-09-02 10:31:11 +0000 | [diff] [blame] | 3532 | VdbeCursor *pCx; |
drh | 3a94987 | 2012-09-18 13:20:13 +0000 | [diff] [blame] | 3533 | |
drh | 399af1d | 2013-11-20 17:25:55 +0000 | [diff] [blame] | 3534 | assert( pOp->p1>=0 ); |
| 3535 | assert( pOp->p2>=0 ); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 3536 | pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, CURTYPE_SORTER); |
dan | 5134d13 | 2011-09-02 10:31:11 +0000 | [diff] [blame] | 3537 | if( pCx==0 ) goto no_mem; |
| 3538 | pCx->pKeyInfo = pOp->p4.pKeyInfo; |
drh | 41e13e1 | 2013-11-07 14:09:39 +0000 | [diff] [blame] | 3539 | assert( pCx->pKeyInfo->db==db ); |
| 3540 | assert( pCx->pKeyInfo->enc==ENC(db) ); |
dan | fad9f9a | 2014-04-01 18:41:51 +0000 | [diff] [blame] | 3541 | rc = sqlite3VdbeSorterInit(db, pOp->p3, pCx); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 3542 | if( rc ) goto abort_due_to_error; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3543 | break; |
| 3544 | } |
| 3545 | |
dan | 78d5843 | 2014-03-25 15:04:07 +0000 | [diff] [blame] | 3546 | /* Opcode: SequenceTest P1 P2 * * * |
| 3547 | ** Synopsis: if( cursor[P1].ctr++ ) pc = P2 |
| 3548 | ** |
| 3549 | ** P1 is a sorter cursor. If the sequence counter is currently zero, jump |
| 3550 | ** to P2. Regardless of whether or not the jump is taken, increment the |
| 3551 | ** the sequence value. |
| 3552 | */ |
| 3553 | case OP_SequenceTest: { |
| 3554 | VdbeCursor *pC; |
| 3555 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 3556 | pC = p->apCsr[pOp->p1]; |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 3557 | assert( isSorter(pC) ); |
dan | 78d5843 | 2014-03-25 15:04:07 +0000 | [diff] [blame] | 3558 | if( (pC->seqCount++)==0 ){ |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 3559 | goto jump_to_p2; |
dan | 78d5843 | 2014-03-25 15:04:07 +0000 | [diff] [blame] | 3560 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3561 | break; |
| 3562 | } |
| 3563 | |
drh | 5f61229 | 2014-02-08 23:20:32 +0000 | [diff] [blame] | 3564 | /* Opcode: OpenPseudo P1 P2 P3 * * |
drh | 60830e3 | 2014-02-10 15:56:34 +0000 | [diff] [blame] | 3565 | ** Synopsis: P3 columns in r[P2] |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3566 | ** |
| 3567 | ** 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] | 3568 | ** row of data. The content of that one row is the content of memory |
| 3569 | ** register P2. In other words, cursor P1 becomes an alias for the |
| 3570 | ** MEM_Blob content contained in register P2. |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3571 | ** |
drh | 2d8d7ce | 2010-02-15 15:17:05 +0000 | [diff] [blame] | 3572 | ** A pseudo-table created by this opcode is used to hold a single |
drh | cdd536f | 2006-03-17 00:04:03 +0000 | [diff] [blame] | 3573 | ** row output from the sorter so that the row can be decomposed into |
drh | 3e9ca09 | 2009-09-08 01:14:48 +0000 | [diff] [blame] | 3574 | ** individual columns using the OP_Column opcode. The OP_Column opcode |
| 3575 | ** is the only cursor opcode that works with a pseudo-table. |
danielk1977 | d336e22 | 2009-02-20 10:58:41 +0000 | [diff] [blame] | 3576 | ** |
| 3577 | ** P3 is the number of fields in the records that will be stored by |
| 3578 | ** the pseudo-table. |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3579 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3580 | case OP_OpenPseudo: { |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 3581 | VdbeCursor *pCx; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3582 | |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 3583 | assert( pOp->p1>=0 ); |
drh | 399af1d | 2013-11-20 17:25:55 +0000 | [diff] [blame] | 3584 | assert( pOp->p3>=0 ); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 3585 | pCx = allocateCursor(p, pOp->p1, pOp->p3, -1, CURTYPE_PSEUDO); |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 3586 | if( pCx==0 ) goto no_mem; |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3587 | pCx->nullRow = 1; |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 3588 | pCx->uc.pseudoTableReg = pOp->p2; |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3589 | pCx->isTable = 1; |
drh | 5f61229 | 2014-02-08 23:20:32 +0000 | [diff] [blame] | 3590 | assert( pOp->p5==0 ); |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3591 | break; |
| 3592 | } |
| 3593 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 3594 | /* Opcode: Close P1 * * * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3595 | ** |
| 3596 | ** Close a cursor previously opened as P1. If P1 is not |
| 3597 | ** currently open, this instruction is a no-op. |
| 3598 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3599 | case OP_Close: { |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 3600 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 3601 | sqlite3VdbeFreeCursor(p, p->apCsr[pOp->p1]); |
| 3602 | p->apCsr[pOp->p1] = 0; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3603 | break; |
| 3604 | } |
| 3605 | |
drh | 97bae79 | 2015-06-05 15:59:57 +0000 | [diff] [blame] | 3606 | #ifdef SQLITE_ENABLE_COLUMN_USED_MASK |
| 3607 | /* Opcode: ColumnsUsed P1 * * P4 * |
| 3608 | ** |
| 3609 | ** This opcode (which only exists if SQLite was compiled with |
| 3610 | ** SQLITE_ENABLE_COLUMN_USED_MASK) identifies which columns of the |
| 3611 | ** table or index for cursor P1 are used. P4 is a 64-bit integer |
| 3612 | ** (P4_INT64) in which the first 63 bits are one for each of the |
| 3613 | ** first 63 columns of the table or index that are actually used |
| 3614 | ** by the cursor. The high-order bit is set if any column after |
| 3615 | ** the 64th is used. |
| 3616 | */ |
| 3617 | case OP_ColumnsUsed: { |
| 3618 | VdbeCursor *pC; |
| 3619 | pC = p->apCsr[pOp->p1]; |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 3620 | assert( pC->eCurType==CURTYPE_BTREE ); |
drh | 97bae79 | 2015-06-05 15:59:57 +0000 | [diff] [blame] | 3621 | pC->maskUsed = *(u64*)pOp->p4.pI64; |
| 3622 | break; |
| 3623 | } |
| 3624 | #endif |
| 3625 | |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 3626 | /* Opcode: SeekGE P1 P2 P3 P4 * |
drh | f63552b | 2013-10-30 00:25:03 +0000 | [diff] [blame] | 3627 | ** Synopsis: key=r[P3@P4] |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3628 | ** |
danielk1977 | b790c6c | 2008-04-18 10:25:24 +0000 | [diff] [blame] | 3629 | ** 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] | 3630 | ** use the value in register P3 as the key. If cursor P1 refers |
danielk1977 | b790c6c | 2008-04-18 10:25:24 +0000 | [diff] [blame] | 3631 | ** to an SQL index, then P3 is the first in an array of P4 registers |
| 3632 | ** that are used as an unpacked index key. |
| 3633 | ** |
| 3634 | ** Reposition cursor P1 so that it points to the smallest entry that |
| 3635 | ** is greater than or equal to the key value. If there are no records |
| 3636 | ** 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] | 3637 | ** |
drh | b1d607d | 2015-11-05 22:30:54 +0000 | [diff] [blame] | 3638 | ** If the cursor P1 was opened using the OPFLAG_SEEKEQ flag, then this |
| 3639 | ** opcode will always land on a record that equally equals the key, or |
| 3640 | ** else jump immediately to P2. When the cursor is OPFLAG_SEEKEQ, this |
| 3641 | ** opcode must be followed by an IdxLE opcode with the same arguments. |
| 3642 | ** The IdxLE opcode will be skipped if this opcode succeeds, but the |
| 3643 | ** IdxLE opcode will be used on subsequent loop iterations. |
| 3644 | ** |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 3645 | ** This opcode leaves the cursor configured to move in forward order, |
drh | bc5cf38 | 2014-08-06 01:08:07 +0000 | [diff] [blame] | 3646 | ** from the beginning toward the end. In other words, the cursor is |
drh | 5dad9a3 | 2014-07-25 18:37:42 +0000 | [diff] [blame] | 3647 | ** configured to use Next, not Prev. |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 3648 | ** |
drh | 935850e | 2014-05-24 17:15:15 +0000 | [diff] [blame] | 3649 | ** See also: Found, NotFound, SeekLt, SeekGt, SeekLe |
drh | 7cf6e4d | 2004-05-19 14:56:55 +0000 | [diff] [blame] | 3650 | */ |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 3651 | /* Opcode: SeekGT P1 P2 P3 P4 * |
drh | f63552b | 2013-10-30 00:25:03 +0000 | [diff] [blame] | 3652 | ** Synopsis: key=r[P3@P4] |
drh | 7cf6e4d | 2004-05-19 14:56:55 +0000 | [diff] [blame] | 3653 | ** |
danielk1977 | b790c6c | 2008-04-18 10:25:24 +0000 | [diff] [blame] | 3654 | ** 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] | 3655 | ** use the value in register P3 as a key. If cursor P1 refers |
danielk1977 | b790c6c | 2008-04-18 10:25:24 +0000 | [diff] [blame] | 3656 | ** to an SQL index, then P3 is the first in an array of P4 registers |
| 3657 | ** that are used as an unpacked index key. |
| 3658 | ** |
| 3659 | ** Reposition cursor P1 so that it points to the smallest entry that |
| 3660 | ** is greater than the key value. If there are no records greater than |
| 3661 | ** the key and P2 is not zero, then jump to P2. |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 3662 | ** |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 3663 | ** This opcode leaves the cursor configured to move in forward order, |
drh | 4ed2fb9 | 2014-08-14 13:06:25 +0000 | [diff] [blame] | 3664 | ** from the beginning toward the end. In other words, the cursor is |
drh | 5dad9a3 | 2014-07-25 18:37:42 +0000 | [diff] [blame] | 3665 | ** configured to use Next, not Prev. |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 3666 | ** |
drh | 935850e | 2014-05-24 17:15:15 +0000 | [diff] [blame] | 3667 | ** See also: Found, NotFound, SeekLt, SeekGe, SeekLe |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3668 | */ |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 3669 | /* Opcode: SeekLT P1 P2 P3 P4 * |
drh | f63552b | 2013-10-30 00:25:03 +0000 | [diff] [blame] | 3670 | ** Synopsis: key=r[P3@P4] |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 3671 | ** |
danielk1977 | b790c6c | 2008-04-18 10:25:24 +0000 | [diff] [blame] | 3672 | ** 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] | 3673 | ** use the value in register P3 as a key. If cursor P1 refers |
danielk1977 | b790c6c | 2008-04-18 10:25:24 +0000 | [diff] [blame] | 3674 | ** to an SQL index, then P3 is the first in an array of P4 registers |
| 3675 | ** that are used as an unpacked index key. |
| 3676 | ** |
| 3677 | ** Reposition cursor P1 so that it points to the largest entry that |
| 3678 | ** is less than the key value. If there are no records less than |
| 3679 | ** the key and P2 is not zero, then jump to P2. |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 3680 | ** |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 3681 | ** This opcode leaves the cursor configured to move in reverse order, |
| 3682 | ** from the end toward the beginning. In other words, the cursor is |
drh | 5dad9a3 | 2014-07-25 18:37:42 +0000 | [diff] [blame] | 3683 | ** configured to use Prev, not Next. |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 3684 | ** |
drh | 935850e | 2014-05-24 17:15:15 +0000 | [diff] [blame] | 3685 | ** See also: Found, NotFound, SeekGt, SeekGe, SeekLe |
drh | 7cf6e4d | 2004-05-19 14:56:55 +0000 | [diff] [blame] | 3686 | */ |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 3687 | /* Opcode: SeekLE P1 P2 P3 P4 * |
drh | f63552b | 2013-10-30 00:25:03 +0000 | [diff] [blame] | 3688 | ** Synopsis: key=r[P3@P4] |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 3689 | ** |
danielk1977 | b790c6c | 2008-04-18 10:25:24 +0000 | [diff] [blame] | 3690 | ** 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] | 3691 | ** use the value in register P3 as a key. If cursor P1 refers |
danielk1977 | b790c6c | 2008-04-18 10:25:24 +0000 | [diff] [blame] | 3692 | ** to an SQL index, then P3 is the first in an array of P4 registers |
| 3693 | ** that are used as an unpacked index key. |
danielk1977 | 751de56 | 2008-04-18 09:01:15 +0000 | [diff] [blame] | 3694 | ** |
danielk1977 | b790c6c | 2008-04-18 10:25:24 +0000 | [diff] [blame] | 3695 | ** Reposition cursor P1 so that it points to the largest entry that |
| 3696 | ** is less than or equal to the key value. If there are no records |
| 3697 | ** 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] | 3698 | ** |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 3699 | ** This opcode leaves the cursor configured to move in reverse order, |
| 3700 | ** from the end toward the beginning. In other words, the cursor is |
drh | 5dad9a3 | 2014-07-25 18:37:42 +0000 | [diff] [blame] | 3701 | ** configured to use Prev, not Next. |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 3702 | ** |
drh | b1d607d | 2015-11-05 22:30:54 +0000 | [diff] [blame] | 3703 | ** If the cursor P1 was opened using the OPFLAG_SEEKEQ flag, then this |
| 3704 | ** opcode will always land on a record that equally equals the key, or |
| 3705 | ** else jump immediately to P2. When the cursor is OPFLAG_SEEKEQ, this |
| 3706 | ** opcode must be followed by an IdxGE opcode with the same arguments. |
| 3707 | ** The IdxGE opcode will be skipped if this opcode succeeds, but the |
| 3708 | ** IdxGE opcode will be used on subsequent loop iterations. |
| 3709 | ** |
drh | 935850e | 2014-05-24 17:15:15 +0000 | [diff] [blame] | 3710 | ** See also: Found, NotFound, SeekGt, SeekGe, SeekLt |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 3711 | */ |
drh | 4a1d365 | 2014-02-14 15:13:36 +0000 | [diff] [blame] | 3712 | case OP_SeekLT: /* jump, in3 */ |
| 3713 | case OP_SeekLE: /* jump, in3 */ |
| 3714 | case OP_SeekGE: /* jump, in3 */ |
| 3715 | case OP_SeekGT: { /* jump, in3 */ |
drh | b1d607d | 2015-11-05 22:30:54 +0000 | [diff] [blame] | 3716 | int res; /* Comparison result */ |
| 3717 | int oc; /* Opcode */ |
| 3718 | VdbeCursor *pC; /* The cursor to seek */ |
| 3719 | UnpackedRecord r; /* The key to seek for */ |
| 3720 | int nField; /* Number of columns or fields in the key */ |
| 3721 | i64 iKey; /* The rowid we are to seek to */ |
drh | d6b7946 | 2015-11-07 01:19:00 +0000 | [diff] [blame] | 3722 | int eqOnly; /* Only interested in == results */ |
drh | 80ff32f | 2001-11-04 18:32:46 +0000 | [diff] [blame] | 3723 | |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 3724 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
drh | 959403f | 2008-12-12 17:56:16 +0000 | [diff] [blame] | 3725 | assert( pOp->p2!=0 ); |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 3726 | pC = p->apCsr[pOp->p1]; |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 3727 | assert( pC!=0 ); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 3728 | assert( pC->eCurType==CURTYPE_BTREE ); |
drh | 4a1d365 | 2014-02-14 15:13:36 +0000 | [diff] [blame] | 3729 | assert( OP_SeekLE == OP_SeekLT+1 ); |
| 3730 | assert( OP_SeekGE == OP_SeekLT+2 ); |
| 3731 | assert( OP_SeekGT == OP_SeekLT+3 ); |
drh | d4187c7 | 2010-08-30 22:15:45 +0000 | [diff] [blame] | 3732 | assert( pC->isOrdered ); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 3733 | assert( pC->uc.pCursor!=0 ); |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 3734 | oc = pOp->opcode; |
drh | d6b7946 | 2015-11-07 01:19:00 +0000 | [diff] [blame] | 3735 | eqOnly = 0; |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 3736 | pC->nullRow = 0; |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 3737 | #ifdef SQLITE_DEBUG |
| 3738 | pC->seekOp = pOp->opcode; |
| 3739 | #endif |
drh | e0997b3 | 2015-03-20 14:57:50 +0000 | [diff] [blame] | 3740 | |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 3741 | if( pC->isTable ){ |
drh | d6b7946 | 2015-11-07 01:19:00 +0000 | [diff] [blame] | 3742 | /* The BTREE_SEEK_EQ flag is only set on index cursors */ |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 3743 | assert( sqlite3BtreeCursorHasHint(pC->uc.pCursor, BTREE_SEEK_EQ)==0 ); |
drh | d6b7946 | 2015-11-07 01:19:00 +0000 | [diff] [blame] | 3744 | |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 3745 | /* The input value in P3 might be of any type: integer, real, string, |
| 3746 | ** 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] | 3747 | ** the seek, so convert it. */ |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 3748 | pIn3 = &aMem[pOp->p3]; |
drh | 11a6eee | 2014-09-19 22:01:54 +0000 | [diff] [blame] | 3749 | if( (pIn3->flags & (MEM_Int|MEM_Real|MEM_Str))==MEM_Str ){ |
drh | bd9507c | 2014-08-23 17:21:37 +0000 | [diff] [blame] | 3750 | applyNumericAffinity(pIn3, 0); |
| 3751 | } |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 3752 | iKey = sqlite3VdbeIntValue(pIn3); |
drh | 959403f | 2008-12-12 17:56:16 +0000 | [diff] [blame] | 3753 | |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 3754 | /* If the P3 value could not be converted into an integer without |
| 3755 | ** loss of information, then special processing is required... */ |
| 3756 | if( (pIn3->flags & MEM_Int)==0 ){ |
| 3757 | if( (pIn3->flags & MEM_Real)==0 ){ |
| 3758 | /* If the P3 value cannot be converted into any kind of a number, |
| 3759 | ** then the seek is not possible, so jump to P2 */ |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 3760 | VdbeBranchTaken(1,2); goto jump_to_p2; |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 3761 | break; |
| 3762 | } |
drh | 959403f | 2008-12-12 17:56:16 +0000 | [diff] [blame] | 3763 | |
dan | aa1776f | 2013-11-26 18:22:59 +0000 | [diff] [blame] | 3764 | /* If the approximation iKey is larger than the actual real search |
| 3765 | ** term, substitute >= for > and < for <=. e.g. if the search term |
| 3766 | ** is 4.9 and the integer approximation 5: |
| 3767 | ** |
| 3768 | ** (x > 4.9) -> (x >= 5) |
| 3769 | ** (x <= 4.9) -> (x < 5) |
| 3770 | */ |
drh | 74eaba4 | 2014-09-18 17:52:15 +0000 | [diff] [blame] | 3771 | if( pIn3->u.r<(double)iKey ){ |
drh | 4a1d365 | 2014-02-14 15:13:36 +0000 | [diff] [blame] | 3772 | assert( OP_SeekGE==(OP_SeekGT-1) ); |
| 3773 | assert( OP_SeekLT==(OP_SeekLE-1) ); |
| 3774 | assert( (OP_SeekLE & 0x0001)==(OP_SeekGT & 0x0001) ); |
| 3775 | if( (oc & 0x0001)==(OP_SeekGT & 0x0001) ) oc--; |
dan | aa1776f | 2013-11-26 18:22:59 +0000 | [diff] [blame] | 3776 | } |
| 3777 | |
| 3778 | /* If the approximation iKey is smaller than the actual real search |
| 3779 | ** term, substitute <= for < and > for >=. */ |
drh | 74eaba4 | 2014-09-18 17:52:15 +0000 | [diff] [blame] | 3780 | else if( pIn3->u.r>(double)iKey ){ |
drh | 4a1d365 | 2014-02-14 15:13:36 +0000 | [diff] [blame] | 3781 | assert( OP_SeekLE==(OP_SeekLT+1) ); |
| 3782 | assert( OP_SeekGT==(OP_SeekGE+1) ); |
| 3783 | assert( (OP_SeekLT & 0x0001)==(OP_SeekGE & 0x0001) ); |
| 3784 | if( (oc & 0x0001)==(OP_SeekLT & 0x0001) ) oc++; |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 3785 | } |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 3786 | } |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 3787 | rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, 0, (u64)iKey, 0, &res); |
drh | b53a5a9 | 2014-10-12 22:37:22 +0000 | [diff] [blame] | 3788 | pC->movetoTarget = iKey; /* Used by OP_Delete */ |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 3789 | if( rc!=SQLITE_OK ){ |
| 3790 | goto abort_due_to_error; |
drh | 1af3fdb | 2004-07-18 21:33:01 +0000 | [diff] [blame] | 3791 | } |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 3792 | }else{ |
drh | d6b7946 | 2015-11-07 01:19:00 +0000 | [diff] [blame] | 3793 | /* For a cursor with the BTREE_SEEK_EQ hint, only the OP_SeekGE and |
| 3794 | ** OP_SeekLE opcodes are allowed, and these must be immediately followed |
| 3795 | ** by an OP_IdxGT or OP_IdxLT opcode, respectively, with the same key. |
| 3796 | */ |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 3797 | if( sqlite3BtreeCursorHasHint(pC->uc.pCursor, BTREE_SEEK_EQ) ){ |
drh | d6b7946 | 2015-11-07 01:19:00 +0000 | [diff] [blame] | 3798 | eqOnly = 1; |
| 3799 | assert( pOp->opcode==OP_SeekGE || pOp->opcode==OP_SeekLE ); |
| 3800 | assert( pOp[1].opcode==OP_IdxLT || pOp[1].opcode==OP_IdxGT ); |
| 3801 | assert( pOp[1].p1==pOp[0].p1 ); |
| 3802 | assert( pOp[1].p2==pOp[0].p2 ); |
| 3803 | assert( pOp[1].p3==pOp[0].p3 ); |
| 3804 | assert( pOp[1].p4.i==pOp[0].p4.i ); |
| 3805 | } |
| 3806 | |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 3807 | nField = pOp->p4.i; |
| 3808 | assert( pOp->p4type==P4_INT32 ); |
| 3809 | assert( nField>0 ); |
| 3810 | r.pKeyInfo = pC->pKeyInfo; |
| 3811 | r.nField = (u16)nField; |
| 3812 | |
| 3813 | /* The next line of code computes as follows, only faster: |
drh | 4a1d365 | 2014-02-14 15:13:36 +0000 | [diff] [blame] | 3814 | ** if( oc==OP_SeekGT || oc==OP_SeekLE ){ |
dan | 1fed5da | 2014-02-25 21:01:25 +0000 | [diff] [blame] | 3815 | ** r.default_rc = -1; |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 3816 | ** }else{ |
dan | 1fed5da | 2014-02-25 21:01:25 +0000 | [diff] [blame] | 3817 | ** r.default_rc = +1; |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 3818 | ** } |
danielk1977 | f7b9d66 | 2008-06-23 18:49:43 +0000 | [diff] [blame] | 3819 | */ |
dan | 1fed5da | 2014-02-25 21:01:25 +0000 | [diff] [blame] | 3820 | r.default_rc = ((1 & (oc - OP_SeekLT)) ? -1 : +1); |
| 3821 | assert( oc!=OP_SeekGT || r.default_rc==-1 ); |
| 3822 | assert( oc!=OP_SeekLE || r.default_rc==-1 ); |
| 3823 | assert( oc!=OP_SeekGE || r.default_rc==+1 ); |
| 3824 | assert( oc!=OP_SeekLT || r.default_rc==+1 ); |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 3825 | |
| 3826 | r.aMem = &aMem[pOp->p3]; |
| 3827 | #ifdef SQLITE_DEBUG |
| 3828 | { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); } |
| 3829 | #endif |
| 3830 | ExpandBlob(r.aMem); |
drh | 70528d7 | 2015-11-05 20:25:09 +0000 | [diff] [blame] | 3831 | r.eqSeen = 0; |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 3832 | rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, &r, 0, 0, &res); |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 3833 | if( rc!=SQLITE_OK ){ |
| 3834 | goto abort_due_to_error; |
| 3835 | } |
drh | b1d607d | 2015-11-05 22:30:54 +0000 | [diff] [blame] | 3836 | if( eqOnly && r.eqSeen==0 ){ |
| 3837 | assert( res!=0 ); |
| 3838 | goto seek_not_found; |
drh | 70528d7 | 2015-11-05 20:25:09 +0000 | [diff] [blame] | 3839 | } |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 3840 | } |
| 3841 | pC->deferredMoveto = 0; |
| 3842 | pC->cacheStatus = CACHE_STALE; |
| 3843 | #ifdef SQLITE_TEST |
| 3844 | sqlite3_search_count++; |
| 3845 | #endif |
drh | 4a1d365 | 2014-02-14 15:13:36 +0000 | [diff] [blame] | 3846 | if( oc>=OP_SeekGE ){ assert( oc==OP_SeekGE || oc==OP_SeekGT ); |
| 3847 | if( res<0 || (res==0 && oc==OP_SeekGT) ){ |
drh | e39a732 | 2014-02-03 14:04:11 +0000 | [diff] [blame] | 3848 | res = 0; |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 3849 | rc = sqlite3BtreeNext(pC->uc.pCursor, &res); |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 3850 | if( rc!=SQLITE_OK ) goto abort_due_to_error; |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 3851 | }else{ |
| 3852 | res = 0; |
| 3853 | } |
| 3854 | }else{ |
drh | 4a1d365 | 2014-02-14 15:13:36 +0000 | [diff] [blame] | 3855 | assert( oc==OP_SeekLT || oc==OP_SeekLE ); |
| 3856 | if( res>0 || (res==0 && oc==OP_SeekLT) ){ |
drh | e39a732 | 2014-02-03 14:04:11 +0000 | [diff] [blame] | 3857 | res = 0; |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 3858 | rc = sqlite3BtreePrevious(pC->uc.pCursor, &res); |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 3859 | if( rc!=SQLITE_OK ) goto abort_due_to_error; |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 3860 | }else{ |
| 3861 | /* res might be negative because the table is empty. Check to |
| 3862 | ** see if this is the case. |
| 3863 | */ |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 3864 | res = sqlite3BtreeEof(pC->uc.pCursor); |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 3865 | } |
| 3866 | } |
drh | b1d607d | 2015-11-05 22:30:54 +0000 | [diff] [blame] | 3867 | seek_not_found: |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 3868 | assert( pOp->p2>0 ); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 3869 | VdbeBranchTaken(res!=0,2); |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 3870 | if( res ){ |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 3871 | goto jump_to_p2; |
drh | b1d607d | 2015-11-05 22:30:54 +0000 | [diff] [blame] | 3872 | }else if( eqOnly ){ |
| 3873 | assert( pOp[1].opcode==OP_IdxLT || pOp[1].opcode==OP_IdxGT ); |
| 3874 | pOp++; /* Skip the OP_IdxLt or OP_IdxGT that follows */ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3875 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3876 | break; |
| 3877 | } |
drh | 959403f | 2008-12-12 17:56:16 +0000 | [diff] [blame] | 3878 | |
| 3879 | |
drh | 8cff69d | 2009-11-12 19:59:44 +0000 | [diff] [blame] | 3880 | /* Opcode: Found P1 P2 P3 P4 * |
drh | f63552b | 2013-10-30 00:25:03 +0000 | [diff] [blame] | 3881 | ** Synopsis: key=r[P3@P4] |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3882 | ** |
drh | 8cff69d | 2009-11-12 19:59:44 +0000 | [diff] [blame] | 3883 | ** If P4==0 then register P3 holds a blob constructed by MakeRecord. If |
| 3884 | ** P4>0 then register P3 is the first of P4 registers that form an unpacked |
| 3885 | ** record. |
| 3886 | ** |
| 3887 | ** Cursor P1 is on an index btree. If the record identified by P3 and P4 |
| 3888 | ** 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] | 3889 | ** P1 is left pointing at the matching entry. |
drh | 6f225d0 | 2013-10-26 13:36:51 +0000 | [diff] [blame] | 3890 | ** |
drh | cefc87f | 2014-08-01 01:40:33 +0000 | [diff] [blame] | 3891 | ** This operation leaves the cursor in a state where it can be |
| 3892 | ** advanced in the forward direction. The Next instruction will work, |
| 3893 | ** but not the Prev instruction. |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 3894 | ** |
drh | 6f225d0 | 2013-10-26 13:36:51 +0000 | [diff] [blame] | 3895 | ** See also: NotFound, NoConflict, NotExists. SeekGe |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3896 | */ |
drh | 8cff69d | 2009-11-12 19:59:44 +0000 | [diff] [blame] | 3897 | /* Opcode: NotFound P1 P2 P3 P4 * |
drh | f63552b | 2013-10-30 00:25:03 +0000 | [diff] [blame] | 3898 | ** Synopsis: key=r[P3@P4] |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3899 | ** |
drh | 8cff69d | 2009-11-12 19:59:44 +0000 | [diff] [blame] | 3900 | ** If P4==0 then register P3 holds a blob constructed by MakeRecord. If |
| 3901 | ** P4>0 then register P3 is the first of P4 registers that form an unpacked |
| 3902 | ** record. |
| 3903 | ** |
| 3904 | ** Cursor P1 is on an index btree. If the record identified by P3 and P4 |
| 3905 | ** is not the prefix of any entry in P1 then a jump is made to P2. If P1 |
| 3906 | ** does contain an entry whose prefix matches the P3/P4 record then control |
| 3907 | ** falls through to the next instruction and P1 is left pointing at the |
| 3908 | ** matching entry. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3909 | ** |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 3910 | ** This operation leaves the cursor in a state where it cannot be |
| 3911 | ** advanced in either direction. In other words, the Next and Prev |
| 3912 | ** opcodes do not work after this operation. |
| 3913 | ** |
drh | 6f225d0 | 2013-10-26 13:36:51 +0000 | [diff] [blame] | 3914 | ** See also: Found, NotExists, NoConflict |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3915 | */ |
drh | 6f225d0 | 2013-10-26 13:36:51 +0000 | [diff] [blame] | 3916 | /* Opcode: NoConflict P1 P2 P3 P4 * |
drh | 4af5bee | 2013-10-30 02:37:50 +0000 | [diff] [blame] | 3917 | ** Synopsis: key=r[P3@P4] |
drh | 6f225d0 | 2013-10-26 13:36:51 +0000 | [diff] [blame] | 3918 | ** |
| 3919 | ** If P4==0 then register P3 holds a blob constructed by MakeRecord. If |
| 3920 | ** P4>0 then register P3 is the first of P4 registers that form an unpacked |
| 3921 | ** record. |
| 3922 | ** |
| 3923 | ** Cursor P1 is on an index btree. If the record identified by P3 and P4 |
| 3924 | ** contains any NULL value, jump immediately to P2. If all terms of the |
| 3925 | ** record are not-NULL then a check is done to determine if any row in the |
| 3926 | ** P1 index btree has a matching key prefix. If there are no matches, jump |
| 3927 | ** immediately to P2. If there is a match, fall through and leave the P1 |
| 3928 | ** cursor pointing to the matching row. |
| 3929 | ** |
| 3930 | ** This opcode is similar to OP_NotFound with the exceptions that the |
| 3931 | ** branch is always taken if any part of the search key input is NULL. |
| 3932 | ** |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 3933 | ** This operation leaves the cursor in a state where it cannot be |
| 3934 | ** advanced in either direction. In other words, the Next and Prev |
| 3935 | ** opcodes do not work after this operation. |
| 3936 | ** |
drh | 6f225d0 | 2013-10-26 13:36:51 +0000 | [diff] [blame] | 3937 | ** See also: NotFound, Found, NotExists |
| 3938 | */ |
| 3939 | case OP_NoConflict: /* jump, in3 */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3940 | case OP_NotFound: /* jump, in3 */ |
| 3941 | case OP_Found: { /* jump, in3 */ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3942 | int alreadyExists; |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 3943 | int takeJump; |
drh | 6f225d0 | 2013-10-26 13:36:51 +0000 | [diff] [blame] | 3944 | int ii; |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 3945 | VdbeCursor *pC; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3946 | int res; |
dan | 03e9cfc | 2011-09-05 14:20:27 +0000 | [diff] [blame] | 3947 | char *pFree; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3948 | UnpackedRecord *pIdxKey; |
drh | 8cff69d | 2009-11-12 19:59:44 +0000 | [diff] [blame] | 3949 | UnpackedRecord r; |
drh | b413922 | 2013-11-06 14:36:08 +0000 | [diff] [blame] | 3950 | char aTempRec[ROUND8(sizeof(UnpackedRecord)) + sizeof(Mem)*4 + 7]; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3951 | |
dan | 0ff297e | 2009-09-25 17:03:14 +0000 | [diff] [blame] | 3952 | #ifdef SQLITE_TEST |
drh | 6f225d0 | 2013-10-26 13:36:51 +0000 | [diff] [blame] | 3953 | if( pOp->opcode!=OP_NoConflict ) sqlite3_found_count++; |
dan | 0ff297e | 2009-09-25 17:03:14 +0000 | [diff] [blame] | 3954 | #endif |
| 3955 | |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 3956 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
drh | 8cff69d | 2009-11-12 19:59:44 +0000 | [diff] [blame] | 3957 | assert( pOp->p4type==P4_INT32 ); |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 3958 | pC = p->apCsr[pOp->p1]; |
| 3959 | assert( pC!=0 ); |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 3960 | #ifdef SQLITE_DEBUG |
drh | cefc87f | 2014-08-01 01:40:33 +0000 | [diff] [blame] | 3961 | pC->seekOp = pOp->opcode; |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 3962 | #endif |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 3963 | pIn3 = &aMem[pOp->p3]; |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 3964 | assert( pC->eCurType==CURTYPE_BTREE ); |
| 3965 | assert( pC->uc.pCursor!=0 ); |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 3966 | assert( pC->isTable==0 ); |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 3967 | pFree = 0; |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 3968 | if( pOp->p4.i>0 ){ |
| 3969 | r.pKeyInfo = pC->pKeyInfo; |
| 3970 | r.nField = (u16)pOp->p4.i; |
| 3971 | r.aMem = pIn3; |
drh | 826af37 | 2014-02-08 19:12:21 +0000 | [diff] [blame] | 3972 | for(ii=0; ii<r.nField; ii++){ |
| 3973 | assert( memIsValid(&r.aMem[ii]) ); |
| 3974 | ExpandBlob(&r.aMem[ii]); |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 3975 | #ifdef SQLITE_DEBUG |
drh | 826af37 | 2014-02-08 19:12:21 +0000 | [diff] [blame] | 3976 | if( ii ) REGISTER_TRACE(pOp->p3+ii, &r.aMem[ii]); |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 3977 | #endif |
drh | 826af37 | 2014-02-08 19:12:21 +0000 | [diff] [blame] | 3978 | } |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 3979 | pIdxKey = &r; |
| 3980 | }else{ |
| 3981 | pIdxKey = sqlite3VdbeAllocUnpackedRecord( |
| 3982 | pC->pKeyInfo, aTempRec, sizeof(aTempRec), &pFree |
dan | b391b94 | 2014-11-07 14:41:11 +0000 | [diff] [blame] | 3983 | ); |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 3984 | if( pIdxKey==0 ) goto no_mem; |
| 3985 | assert( pIn3->flags & MEM_Blob ); |
dan | b391b94 | 2014-11-07 14:41:11 +0000 | [diff] [blame] | 3986 | ExpandBlob(pIn3); |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 3987 | sqlite3VdbeRecordUnpack(pC->pKeyInfo, pIn3->n, pIn3->z, pIdxKey); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3988 | } |
dan | 1fed5da | 2014-02-25 21:01:25 +0000 | [diff] [blame] | 3989 | pIdxKey->default_rc = 0; |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 3990 | takeJump = 0; |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 3991 | if( pOp->opcode==OP_NoConflict ){ |
| 3992 | /* For the OP_NoConflict opcode, take the jump if any of the |
| 3993 | ** input fields are NULL, since any key with a NULL will not |
| 3994 | ** conflict */ |
mistachkin | 7bb6e8e | 2015-01-12 18:52:41 +0000 | [diff] [blame] | 3995 | for(ii=0; ii<pIdxKey->nField; ii++){ |
| 3996 | if( pIdxKey->aMem[ii].flags & MEM_Null ){ |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 3997 | takeJump = 1; |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 3998 | break; |
drh | 6f225d0 | 2013-10-26 13:36:51 +0000 | [diff] [blame] | 3999 | } |
| 4000 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4001 | } |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 4002 | rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, pIdxKey, 0, 0, &res); |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 4003 | sqlite3DbFree(db, pFree); |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 4004 | if( rc!=SQLITE_OK ){ |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 4005 | goto abort_due_to_error; |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 4006 | } |
| 4007 | pC->seekResult = res; |
| 4008 | alreadyExists = (res==0); |
| 4009 | pC->nullRow = 1-alreadyExists; |
| 4010 | pC->deferredMoveto = 0; |
| 4011 | pC->cacheStatus = CACHE_STALE; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4012 | if( pOp->opcode==OP_Found ){ |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 4013 | VdbeBranchTaken(alreadyExists!=0,2); |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 4014 | if( alreadyExists ) goto jump_to_p2; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4015 | }else{ |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 4016 | VdbeBranchTaken(takeJump||alreadyExists==0,2); |
| 4017 | if( takeJump || !alreadyExists ) goto jump_to_p2; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4018 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4019 | break; |
| 4020 | } |
| 4021 | |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4022 | /* Opcode: NotExists P1 P2 P3 * * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 4023 | ** Synopsis: intkey=r[P3] |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 4024 | ** |
drh | 261c02d | 2013-10-25 14:46:15 +0000 | [diff] [blame] | 4025 | ** P1 is the index of a cursor open on an SQL table btree (with integer |
| 4026 | ** 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] | 4027 | ** rowid P3 then jump immediately to P2. Or, if P2 is 0, raise an |
| 4028 | ** SQLITE_CORRUPT error. If P1 does contain a record with rowid P3 then |
| 4029 | ** leave the cursor pointing at that record and fall through to the next |
| 4030 | ** instruction. |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 4031 | ** |
drh | 261c02d | 2013-10-25 14:46:15 +0000 | [diff] [blame] | 4032 | ** The OP_NotFound opcode performs the same operation on index btrees |
| 4033 | ** (with arbitrary multi-value keys). |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 4034 | ** |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 4035 | ** This opcode leaves the cursor in a state where it cannot be advanced |
| 4036 | ** in either direction. In other words, the Next and Prev opcodes will |
| 4037 | ** not work following this opcode. |
| 4038 | ** |
drh | 11e8527 | 2013-10-26 15:40:48 +0000 | [diff] [blame] | 4039 | ** See also: Found, NotFound, NoConflict |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 4040 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4041 | case OP_NotExists: { /* jump, in3 */ |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 4042 | VdbeCursor *pC; |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 4043 | BtCursor *pCrsr; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 4044 | int res; |
| 4045 | u64 iKey; |
| 4046 | |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 4047 | pIn3 = &aMem[pOp->p3]; |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 4048 | assert( pIn3->flags & MEM_Int ); |
| 4049 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 4050 | pC = p->apCsr[pOp->p1]; |
| 4051 | assert( pC!=0 ); |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 4052 | #ifdef SQLITE_DEBUG |
| 4053 | pC->seekOp = 0; |
| 4054 | #endif |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 4055 | assert( pC->isTable ); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 4056 | assert( pC->eCurType==CURTYPE_BTREE ); |
| 4057 | pCrsr = pC->uc.pCursor; |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 4058 | assert( pCrsr!=0 ); |
| 4059 | res = 0; |
| 4060 | iKey = pIn3->u.i; |
| 4061 | rc = sqlite3BtreeMovetoUnpacked(pCrsr, 0, iKey, 0, &res); |
drh | b79d552 | 2015-09-14 19:26:37 +0000 | [diff] [blame] | 4062 | assert( rc==SQLITE_OK || res==0 ); |
drh | b53a5a9 | 2014-10-12 22:37:22 +0000 | [diff] [blame] | 4063 | pC->movetoTarget = iKey; /* Used by OP_Delete */ |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 4064 | pC->nullRow = 0; |
| 4065 | pC->cacheStatus = CACHE_STALE; |
| 4066 | pC->deferredMoveto = 0; |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 4067 | VdbeBranchTaken(res!=0,2); |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 4068 | pC->seekResult = res; |
dan | c6157e1 | 2015-09-14 09:23:47 +0000 | [diff] [blame] | 4069 | if( res!=0 ){ |
drh | b79d552 | 2015-09-14 19:26:37 +0000 | [diff] [blame] | 4070 | assert( rc==SQLITE_OK ); |
| 4071 | if( pOp->p2==0 ){ |
| 4072 | rc = SQLITE_CORRUPT_BKPT; |
| 4073 | }else{ |
| 4074 | goto jump_to_p2; |
| 4075 | } |
dan | c6157e1 | 2015-09-14 09:23:47 +0000 | [diff] [blame] | 4076 | } |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 4077 | if( rc ) goto abort_due_to_error; |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 4078 | break; |
| 4079 | } |
| 4080 | |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 4081 | /* Opcode: Sequence P1 P2 * * * |
drh | 079a307 | 2014-03-19 14:10:55 +0000 | [diff] [blame] | 4082 | ** Synopsis: r[P2]=cursor[P1].ctr++ |
drh | 4db38a7 | 2005-09-01 12:16:28 +0000 | [diff] [blame] | 4083 | ** |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 4084 | ** Find the next available sequence number for cursor P1. |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4085 | ** Write the sequence number into register P2. |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 4086 | ** The sequence number on the cursor is incremented after this |
| 4087 | ** instruction. |
drh | 4db38a7 | 2005-09-01 12:16:28 +0000 | [diff] [blame] | 4088 | */ |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 4089 | case OP_Sequence: { /* out2 */ |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 4090 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 4091 | assert( p->apCsr[pOp->p1]!=0 ); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 4092 | assert( p->apCsr[pOp->p1]->eCurType!=CURTYPE_VTAB ); |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 4093 | pOut = out2Prerelease(p, pOp); |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 4094 | pOut->u.i = p->apCsr[pOp->p1]->seqCount++; |
drh | 4db38a7 | 2005-09-01 12:16:28 +0000 | [diff] [blame] | 4095 | break; |
| 4096 | } |
| 4097 | |
| 4098 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4099 | /* Opcode: NewRowid P1 P2 P3 * * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 4100 | ** Synopsis: r[P2]=rowid |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4101 | ** |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 4102 | ** 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] | 4103 | ** The record number is not previously used as a key in the database |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4104 | ** table that cursor P1 points to. The new record number is written |
| 4105 | ** written to register P2. |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 4106 | ** |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 4107 | ** If P3>0 then P3 is a register in the root frame of this VDBE that holds |
| 4108 | ** the largest previously generated record number. No new record numbers are |
| 4109 | ** allowed to be less than this value. When this value reaches its maximum, |
drh | ef8662b | 2011-06-20 21:47:58 +0000 | [diff] [blame] | 4110 | ** an SQLITE_FULL error is generated. The P3 register is updated with the ' |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 4111 | ** generated record number. This P3 mechanism is used to help implement the |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 4112 | ** AUTOINCREMENT feature. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4113 | */ |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 4114 | case OP_NewRowid: { /* out2 */ |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 4115 | i64 v; /* The new rowid */ |
| 4116 | VdbeCursor *pC; /* Cursor of table to get the new rowid */ |
| 4117 | int res; /* Result of an sqlite3BtreeLast() */ |
| 4118 | int cnt; /* Counter to limit the number of searches */ |
| 4119 | Mem *pMem; /* Register holding largest rowid for AUTOINCREMENT */ |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 4120 | VdbeFrame *pFrame; /* Root frame of VDBE */ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 4121 | |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 4122 | v = 0; |
| 4123 | res = 0; |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 4124 | pOut = out2Prerelease(p, pOp); |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 4125 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 4126 | pC = p->apCsr[pOp->p1]; |
| 4127 | assert( pC!=0 ); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 4128 | assert( pC->eCurType==CURTYPE_BTREE ); |
| 4129 | assert( pC->uc.pCursor!=0 ); |
drh | 98ef0f6 | 2015-06-30 01:25:52 +0000 | [diff] [blame] | 4130 | { |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 4131 | /* The next rowid or record number (different terms for the same |
| 4132 | ** thing) is obtained in a two-step algorithm. |
| 4133 | ** |
| 4134 | ** First we attempt to find the largest existing rowid and add one |
| 4135 | ** to that. But if the largest existing rowid is already the maximum |
| 4136 | ** positive integer, we have to fall through to the second |
| 4137 | ** probabilistic algorithm |
| 4138 | ** |
| 4139 | ** The second algorithm is to select a rowid at random and see if |
| 4140 | ** it already exists in the table. If it does not exist, we have |
| 4141 | ** succeeded. If the random rowid does exist, we select a new one |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 4142 | ** and try again, up to 100 times. |
drh | db5ed6d | 2001-09-18 22:17:44 +0000 | [diff] [blame] | 4143 | */ |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 4144 | assert( pC->isTable ); |
drh | fe2093d | 2005-01-20 22:48:47 +0000 | [diff] [blame] | 4145 | |
drh | 75f86a4 | 2005-02-17 00:03:06 +0000 | [diff] [blame] | 4146 | #ifdef SQLITE_32BIT_ROWID |
| 4147 | # define MAX_ROWID 0x7fffffff |
| 4148 | #else |
drh | fe2093d | 2005-01-20 22:48:47 +0000 | [diff] [blame] | 4149 | /* Some compilers complain about constants of the form 0x7fffffffffffffff. |
| 4150 | ** Others complain about 0x7ffffffffffffffffLL. The following macro seems |
| 4151 | ** to provide the constant while making all compilers happy. |
| 4152 | */ |
danielk1977 | 64202cf | 2008-11-17 15:31:47 +0000 | [diff] [blame] | 4153 | # define MAX_ROWID (i64)( (((u64)0x7fffffff)<<32) | (u64)0xffffffff ) |
drh | 75f86a4 | 2005-02-17 00:03:06 +0000 | [diff] [blame] | 4154 | #endif |
drh | fe2093d | 2005-01-20 22:48:47 +0000 | [diff] [blame] | 4155 | |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 4156 | if( !pC->useRandomRowid ){ |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 4157 | rc = sqlite3BtreeLast(pC->uc.pCursor, &res); |
drh | e0670b6 | 2014-02-12 21:31:12 +0000 | [diff] [blame] | 4158 | if( rc!=SQLITE_OK ){ |
| 4159 | goto abort_due_to_error; |
| 4160 | } |
| 4161 | if( res ){ |
| 4162 | v = 1; /* IMP: R-61914-48074 */ |
| 4163 | }else{ |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 4164 | assert( sqlite3BtreeCursorIsValid(pC->uc.pCursor) ); |
| 4165 | rc = sqlite3BtreeKeySize(pC->uc.pCursor, &v); |
drh | e0670b6 | 2014-02-12 21:31:12 +0000 | [diff] [blame] | 4166 | assert( rc==SQLITE_OK ); /* Cannot fail following BtreeLast() */ |
| 4167 | if( v>=MAX_ROWID ){ |
| 4168 | pC->useRandomRowid = 1; |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 4169 | }else{ |
drh | e0670b6 | 2014-02-12 21:31:12 +0000 | [diff] [blame] | 4170 | v++; /* IMP: R-29538-34987 */ |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 4171 | } |
drh | 3fc190c | 2001-09-14 03:24:23 +0000 | [diff] [blame] | 4172 | } |
drh | e0670b6 | 2014-02-12 21:31:12 +0000 | [diff] [blame] | 4173 | } |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 4174 | |
| 4175 | #ifndef SQLITE_OMIT_AUTOINCREMENT |
drh | e0670b6 | 2014-02-12 21:31:12 +0000 | [diff] [blame] | 4176 | if( pOp->p3 ){ |
| 4177 | /* Assert that P3 is a valid memory cell. */ |
| 4178 | assert( pOp->p3>0 ); |
| 4179 | if( p->pFrame ){ |
| 4180 | for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent); |
shane | abc6b89 | 2009-09-10 19:09:03 +0000 | [diff] [blame] | 4181 | /* Assert that P3 is a valid memory cell. */ |
drh | e0670b6 | 2014-02-12 21:31:12 +0000 | [diff] [blame] | 4182 | assert( pOp->p3<=pFrame->nMem ); |
| 4183 | pMem = &pFrame->aMem[pOp->p3]; |
| 4184 | }else{ |
| 4185 | /* Assert that P3 is a valid memory cell. */ |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 4186 | assert( pOp->p3<=(p->nMem+1 - p->nCursor) ); |
drh | e0670b6 | 2014-02-12 21:31:12 +0000 | [diff] [blame] | 4187 | pMem = &aMem[pOp->p3]; |
| 4188 | memAboutToChange(p, pMem); |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 4189 | } |
drh | e0670b6 | 2014-02-12 21:31:12 +0000 | [diff] [blame] | 4190 | assert( memIsValid(pMem) ); |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 4191 | |
drh | e0670b6 | 2014-02-12 21:31:12 +0000 | [diff] [blame] | 4192 | REGISTER_TRACE(pOp->p3, pMem); |
| 4193 | sqlite3VdbeMemIntegerify(pMem); |
| 4194 | assert( (pMem->flags & MEM_Int)!=0 ); /* mem(P3) holds an integer */ |
| 4195 | if( pMem->u.i==MAX_ROWID || pC->useRandomRowid ){ |
| 4196 | rc = SQLITE_FULL; /* IMP: R-12275-61338 */ |
| 4197 | goto abort_due_to_error; |
| 4198 | } |
| 4199 | if( v<pMem->u.i+1 ){ |
| 4200 | v = pMem->u.i + 1; |
| 4201 | } |
| 4202 | pMem->u.i = v; |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 4203 | } |
drh | e0670b6 | 2014-02-12 21:31:12 +0000 | [diff] [blame] | 4204 | #endif |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 4205 | if( pC->useRandomRowid ){ |
drh | 748a52c | 2010-09-01 11:50:08 +0000 | [diff] [blame] | 4206 | /* IMPLEMENTATION-OF: R-07677-41881 If the largest ROWID is equal to the |
drh | c79c761 | 2010-01-01 18:57:48 +0000 | [diff] [blame] | 4207 | ** largest possible integer (9223372036854775807) then the database |
drh | 748a52c | 2010-09-01 11:50:08 +0000 | [diff] [blame] | 4208 | ** engine starts picking positive candidate ROWIDs at random until |
| 4209 | ** it finds one that is not previously used. */ |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 4210 | assert( pOp->p3==0 ); /* We cannot be in random rowid mode if this is |
| 4211 | ** an AUTOINCREMENT table. */ |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 4212 | cnt = 0; |
drh | 2c4dc63 | 2014-09-25 12:31:28 +0000 | [diff] [blame] | 4213 | do{ |
| 4214 | sqlite3_randomness(sizeof(v), &v); |
drh | d863346 | 2014-09-25 17:42:41 +0000 | [diff] [blame] | 4215 | v &= (MAX_ROWID>>1); v++; /* Ensure that v is greater than zero */ |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 4216 | }while( ((rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, 0, (u64)v, |
drh | 748a52c | 2010-09-01 11:50:08 +0000 | [diff] [blame] | 4217 | 0, &res))==SQLITE_OK) |
shaneh | c4d340a | 2010-09-01 02:37:56 +0000 | [diff] [blame] | 4218 | && (res==0) |
drh | 2c4dc63 | 2014-09-25 12:31:28 +0000 | [diff] [blame] | 4219 | && (++cnt<100)); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 4220 | if( rc ) goto abort_due_to_error; |
| 4221 | if( res==0 ){ |
drh | c79c761 | 2010-01-01 18:57:48 +0000 | [diff] [blame] | 4222 | rc = SQLITE_FULL; /* IMP: R-38219-53002 */ |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 4223 | goto abort_due_to_error; |
| 4224 | } |
drh | 748a52c | 2010-09-01 11:50:08 +0000 | [diff] [blame] | 4225 | assert( v>0 ); /* EV: R-40812-03570 */ |
drh | 1eaa269 | 2001-09-18 02:02:23 +0000 | [diff] [blame] | 4226 | } |
drh | a11846b | 2004-01-07 18:52:56 +0000 | [diff] [blame] | 4227 | pC->deferredMoveto = 0; |
drh | 76873ab | 2006-01-07 18:48:26 +0000 | [diff] [blame] | 4228 | pC->cacheStatus = CACHE_STALE; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4229 | } |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 4230 | pOut->u.i = v; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4231 | break; |
| 4232 | } |
| 4233 | |
danielk1977 | 1f4aa33 | 2008-01-03 09:51:55 +0000 | [diff] [blame] | 4234 | /* Opcode: Insert P1 P2 P3 P4 P5 |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 4235 | ** Synopsis: intkey=r[P3] data=r[P2] |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4236 | ** |
jplyon | 5a56422 | 2003-06-02 06:15:58 +0000 | [diff] [blame] | 4237 | ** Write an entry into the table of cursor P1. A new entry is |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 4238 | ** created if it doesn't already exist or the data for an existing |
drh | 3e9ca09 | 2009-09-08 01:14:48 +0000 | [diff] [blame] | 4239 | ** entry is overwritten. The data is the value MEM_Blob stored in register |
danielk1977 | 1f4aa33 | 2008-01-03 09:51:55 +0000 | [diff] [blame] | 4240 | ** number P2. The key is stored in register P3. The key must |
drh | 3e9ca09 | 2009-09-08 01:14:48 +0000 | [diff] [blame] | 4241 | ** be a MEM_Int. |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 4242 | ** |
danielk1977 | 1f4aa33 | 2008-01-03 09:51:55 +0000 | [diff] [blame] | 4243 | ** If the OPFLAG_NCHANGE flag of P5 is set, then the row change count is |
| 4244 | ** incremented (otherwise not). If the OPFLAG_LASTROWID flag of P5 is set, |
danielk1977 | b28af71 | 2004-06-21 06:50:26 +0000 | [diff] [blame] | 4245 | ** then rowid is stored for subsequent return by the |
drh | 85b623f | 2007-12-13 21:54:09 +0000 | [diff] [blame] | 4246 | ** sqlite3_last_insert_rowid() function (otherwise it is unmodified). |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 4247 | ** |
drh | 3e9ca09 | 2009-09-08 01:14:48 +0000 | [diff] [blame] | 4248 | ** If the OPFLAG_USESEEKRESULT flag of P5 is set and if the result of |
| 4249 | ** the last seek operation (OP_NotExists) was a success, then this |
| 4250 | ** operation will not attempt to find the appropriate row before doing |
| 4251 | ** the insert but will instead overwrite the row that the cursor is |
| 4252 | ** currently pointing to. Presumably, the prior OP_NotExists opcode |
| 4253 | ** has already positioned the cursor correctly. This is an optimization |
| 4254 | ** that boosts performance by avoiding redundant seeks. |
| 4255 | ** |
| 4256 | ** If the OPFLAG_ISUPDATE flag is set, then this opcode is part of an |
| 4257 | ** UPDATE operation. Otherwise (if the flag is clear) then this opcode |
| 4258 | ** is part of an INSERT operation. The difference is only important to |
| 4259 | ** the update hook. |
| 4260 | ** |
dan | 319eeb7 | 2011-03-19 08:38:50 +0000 | [diff] [blame] | 4261 | ** Parameter P4 may point to a Table structure, or may be NULL. If it is |
| 4262 | ** not NULL, then the update-hook (sqlite3.xUpdateCallback) is invoked |
| 4263 | ** following a successful insert. |
danielk1977 | 1f6eec5 | 2006-06-16 06:17:47 +0000 | [diff] [blame] | 4264 | ** |
drh | 93aed5a | 2008-01-16 17:46:38 +0000 | [diff] [blame] | 4265 | ** (WARNING/TODO: If P1 is a pseudo-cursor and P2 is dynamically |
| 4266 | ** allocated, then ownership of P2 is transferred to the pseudo-cursor |
| 4267 | ** and register P2 becomes ephemeral. If the cursor is changed, the |
| 4268 | ** value of register P2 will then change. Make sure this does not |
| 4269 | ** cause any problems.) |
| 4270 | ** |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 4271 | ** This instruction only works on tables. The equivalent instruction |
| 4272 | ** for indices is OP_IdxInsert. |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 4273 | */ |
drh | e05c929 | 2009-10-29 13:48:10 +0000 | [diff] [blame] | 4274 | /* Opcode: InsertInt P1 P2 P3 P4 P5 |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 4275 | ** Synopsis: intkey=P3 data=r[P2] |
drh | e05c929 | 2009-10-29 13:48:10 +0000 | [diff] [blame] | 4276 | ** |
| 4277 | ** This works exactly like OP_Insert except that the key is the |
| 4278 | ** integer value P3, not the value of the integer stored in register P3. |
| 4279 | */ |
| 4280 | case OP_Insert: |
| 4281 | case OP_InsertInt: { |
drh | 3e9ca09 | 2009-09-08 01:14:48 +0000 | [diff] [blame] | 4282 | Mem *pData; /* MEM cell holding data for the record to be inserted */ |
| 4283 | Mem *pKey; /* MEM cell holding key for the record */ |
| 4284 | i64 iKey; /* The integer ROWID or key for the record to be inserted */ |
| 4285 | VdbeCursor *pC; /* Cursor to table into which insert is written */ |
| 4286 | int nZero; /* Number of zero-bytes to append */ |
| 4287 | int seekResult; /* Result of prior seek or 0 if no USESEEKRESULT flag */ |
| 4288 | const char *zDb; /* database name - used by the update hook */ |
dan | 319eeb7 | 2011-03-19 08:38:50 +0000 | [diff] [blame] | 4289 | Table *pTab; /* Table structure - used by update and pre-update hooks */ |
drh | 74c3302 | 2016-03-30 12:56:55 +0000 | [diff] [blame] | 4290 | int op; /* Opcode for update hook: SQLITE_UPDATE or SQLITE_INSERT */ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 4291 | |
drh | 74c3302 | 2016-03-30 12:56:55 +0000 | [diff] [blame] | 4292 | op = 0; |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 4293 | pData = &aMem[pOp->p2]; |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 4294 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 4295 | assert( memIsValid(pData) ); |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 4296 | pC = p->apCsr[pOp->p1]; |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 4297 | assert( pC!=0 ); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 4298 | assert( pC->eCurType==CURTYPE_BTREE ); |
| 4299 | assert( pC->uc.pCursor!=0 ); |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 4300 | assert( pC->isTable ); |
drh | cbf1b8e | 2013-11-11 22:55:26 +0000 | [diff] [blame] | 4301 | assert( pOp->p4type==P4_TABLE || pOp->p4type>=P4_STATIC ); |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 4302 | REGISTER_TRACE(pOp->p2, pData); |
danielk1977 | 5f8d8a8 | 2004-05-11 00:28:42 +0000 | [diff] [blame] | 4303 | |
drh | e05c929 | 2009-10-29 13:48:10 +0000 | [diff] [blame] | 4304 | if( pOp->opcode==OP_Insert ){ |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 4305 | pKey = &aMem[pOp->p3]; |
drh | e05c929 | 2009-10-29 13:48:10 +0000 | [diff] [blame] | 4306 | assert( pKey->flags & MEM_Int ); |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 4307 | assert( memIsValid(pKey) ); |
drh | e05c929 | 2009-10-29 13:48:10 +0000 | [diff] [blame] | 4308 | REGISTER_TRACE(pOp->p3, pKey); |
| 4309 | iKey = pKey->u.i; |
| 4310 | }else{ |
| 4311 | assert( pOp->opcode==OP_InsertInt ); |
| 4312 | iKey = pOp->p3; |
| 4313 | } |
| 4314 | |
drh | 9b1c62d | 2011-03-30 21:04:43 +0000 | [diff] [blame] | 4315 | if( pOp->p4type==P4_TABLE && HAS_UPDATE_HOOK(db) ){ |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 4316 | assert( pC->isTable ); |
| 4317 | assert( pC->iDb>=0 ); |
| 4318 | zDb = db->aDb[pC->iDb].zName; |
dan | 319eeb7 | 2011-03-19 08:38:50 +0000 | [diff] [blame] | 4319 | pTab = pOp->p4.pTab; |
drh | c556f3c | 2016-03-30 15:30:07 +0000 | [diff] [blame^] | 4320 | assert( HasRowid(pTab) ); |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 4321 | op = ((pOp->p5 & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_INSERT); |
drh | 74c3302 | 2016-03-30 12:56:55 +0000 | [diff] [blame] | 4322 | }else{ |
| 4323 | pTab = 0; /* Not needed. Silence a comiler warning. */ |
| 4324 | zDb = 0; /* Not needed. Silence a compiler warning. */ |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 4325 | } |
| 4326 | |
drh | 9b1c62d | 2011-03-30 21:04:43 +0000 | [diff] [blame] | 4327 | #ifdef SQLITE_ENABLE_PREUPDATE_HOOK |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 4328 | /* Invoke the pre-update hook, if any */ |
| 4329 | if( db->xPreUpdateCallback |
dan | 319eeb7 | 2011-03-19 08:38:50 +0000 | [diff] [blame] | 4330 | && pOp->p4type==P4_TABLE |
drh | 92fe38e | 2014-10-14 13:41:32 +0000 | [diff] [blame] | 4331 | && !(pOp->p5 & OPFLAG_ISUPDATE) |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 4332 | ){ |
dan | 319eeb7 | 2011-03-19 08:38:50 +0000 | [diff] [blame] | 4333 | sqlite3VdbePreUpdateHook(p, pC, SQLITE_INSERT, zDb, pTab, iKey, pOp->p2); |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 4334 | } |
drh | 9b1c62d | 2011-03-30 21:04:43 +0000 | [diff] [blame] | 4335 | #endif |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 4336 | |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 4337 | if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++; |
drh | 99a6692 | 2011-05-13 18:51:42 +0000 | [diff] [blame] | 4338 | if( pOp->p5 & OPFLAG_LASTROWID ) db->lastRowid = lastRowid = iKey; |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 4339 | if( pData->flags & MEM_Null ){ |
| 4340 | pData->z = 0; |
| 4341 | pData->n = 0; |
| 4342 | }else{ |
| 4343 | assert( pData->flags & (MEM_Blob|MEM_Str) ); |
| 4344 | } |
drh | 3e9ca09 | 2009-09-08 01:14:48 +0000 | [diff] [blame] | 4345 | seekResult = ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0); |
| 4346 | if( pData->flags & MEM_Zero ){ |
| 4347 | nZero = pData->u.nZero; |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 4348 | }else{ |
drh | 3e9ca09 | 2009-09-08 01:14:48 +0000 | [diff] [blame] | 4349 | nZero = 0; |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 4350 | } |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 4351 | rc = sqlite3BtreeInsert(pC->uc.pCursor, 0, iKey, |
drh | 3e9ca09 | 2009-09-08 01:14:48 +0000 | [diff] [blame] | 4352 | pData->z, pData->n, nZero, |
drh | ebf10b1 | 2013-11-25 17:38:26 +0000 | [diff] [blame] | 4353 | (pOp->p5 & OPFLAG_APPEND)!=0, seekResult |
drh | 3e9ca09 | 2009-09-08 01:14:48 +0000 | [diff] [blame] | 4354 | ); |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 4355 | pC->deferredMoveto = 0; |
| 4356 | pC->cacheStatus = CACHE_STALE; |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 +0000 | [diff] [blame] | 4357 | |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 4358 | /* Invoke the update-hook if required. */ |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 4359 | if( rc ) goto abort_due_to_error; |
drh | c556f3c | 2016-03-30 15:30:07 +0000 | [diff] [blame^] | 4360 | if( db->xUpdateCallback && op ){ |
dan | 319eeb7 | 2011-03-19 08:38:50 +0000 | [diff] [blame] | 4361 | db->xUpdateCallback(db->pUpdateArg, op, zDb, pTab->zName, iKey); |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 4362 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4363 | break; |
| 4364 | } |
| 4365 | |
dan | 438b881 | 2015-09-15 15:55:15 +0000 | [diff] [blame] | 4366 | /* Opcode: Delete P1 P2 P3 P4 P5 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4367 | ** |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 4368 | ** Delete the record at which the P1 cursor is currently pointing. |
| 4369 | ** |
drh | e807bdb | 2016-01-21 17:06:33 +0000 | [diff] [blame] | 4370 | ** If the OPFLAG_SAVEPOSITION bit of the P5 parameter is set, then |
| 4371 | ** the cursor will be left pointing at either the next or the previous |
| 4372 | ** record in the table. If it is left pointing at the next record, then |
| 4373 | ** the next Next instruction will be a no-op. As a result, in this case |
| 4374 | ** it is ok to delete a record from within a Next loop. If |
| 4375 | ** OPFLAG_SAVEPOSITION bit of P5 is clear, then the cursor will be |
| 4376 | ** left in an undefined state. |
drh | c8d30ac | 2002-04-12 10:08:59 +0000 | [diff] [blame] | 4377 | ** |
drh | def19e3 | 2016-01-27 16:26:25 +0000 | [diff] [blame] | 4378 | ** If the OPFLAG_AUXDELETE bit is set on P5, that indicates that this |
| 4379 | ** delete one of several associated with deleting a table row and all its |
| 4380 | ** associated index entries. Exactly one of those deletes is the "primary" |
| 4381 | ** delete. The others are all on OPFLAG_FORDELETE cursors or else are |
| 4382 | ** marked with the AUXDELETE flag. |
drh | e807bdb | 2016-01-21 17:06:33 +0000 | [diff] [blame] | 4383 | ** |
| 4384 | ** If the OPFLAG_NCHANGE flag of P2 (NB: P2 not P5) is set, then the row |
| 4385 | ** change count is incremented (otherwise not). |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 4386 | ** |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 4387 | ** P1 must not be pseudo-table. It has to be a real table with |
| 4388 | ** multiple rows. |
| 4389 | ** |
dan | 319eeb7 | 2011-03-19 08:38:50 +0000 | [diff] [blame] | 4390 | ** If P4 is not NULL then it points to a Table struture. In this case either |
| 4391 | ** the update or pre-update hook, or both, may be invoked. The P1 cursor must |
| 4392 | ** have been positioned using OP_NotFound prior to invoking this opcode in |
| 4393 | ** this case. Specifically, if one is configured, the pre-update hook is |
| 4394 | ** invoked if P4 is not NULL. The update-hook is invoked if one is configured, |
| 4395 | ** P4 is not NULL, and the OPFLAG_NCHANGE flag is set in P2. |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 4396 | ** |
| 4397 | ** If the OPFLAG_ISUPDATE flag is set in P2, then P3 contains the address |
| 4398 | ** of the memory cell that contains the value that the rowid of the row will |
| 4399 | ** be set to by the update. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4400 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4401 | case OP_Delete: { |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 4402 | VdbeCursor *pC; |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 4403 | const char *zDb; |
dan | 319eeb7 | 2011-03-19 08:38:50 +0000 | [diff] [blame] | 4404 | Table *pTab; |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 4405 | int opflags; |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 4406 | |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 4407 | opflags = pOp->p2; |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 4408 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 4409 | pC = p->apCsr[pOp->p1]; |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 4410 | assert( pC!=0 ); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 4411 | assert( pC->eCurType==CURTYPE_BTREE ); |
| 4412 | assert( pC->uc.pCursor!=0 ); |
drh | 9a65f2c | 2009-06-22 19:05:40 +0000 | [diff] [blame] | 4413 | assert( pC->deferredMoveto==0 ); |
drh | 9a65f2c | 2009-06-22 19:05:40 +0000 | [diff] [blame] | 4414 | |
drh | b53a5a9 | 2014-10-12 22:37:22 +0000 | [diff] [blame] | 4415 | #ifdef SQLITE_DEBUG |
dan | 438b881 | 2015-09-15 15:55:15 +0000 | [diff] [blame] | 4416 | if( pOp->p4type==P4_TABLE && HasRowid(pOp->p4.pTab) && pOp->p5==0 ){ |
| 4417 | /* If p5 is zero, the seek operation that positioned the cursor prior to |
| 4418 | ** OP_Delete will have also set the pC->movetoTarget field to the rowid of |
| 4419 | ** the row that is being deleted */ |
drh | b53a5a9 | 2014-10-12 22:37:22 +0000 | [diff] [blame] | 4420 | i64 iKey = 0; |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 4421 | sqlite3BtreeKeySize(pC->uc.pCursor, &iKey); |
drh | 92fe38e | 2014-10-14 13:41:32 +0000 | [diff] [blame] | 4422 | assert( pC->movetoTarget==iKey ); |
drh | b53a5a9 | 2014-10-12 22:37:22 +0000 | [diff] [blame] | 4423 | } |
| 4424 | #endif |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 4425 | |
dan | 438b881 | 2015-09-15 15:55:15 +0000 | [diff] [blame] | 4426 | /* If the update-hook or pre-update-hook will be invoked, set zDb to |
| 4427 | ** the name of the db to pass as to it. Also set local pTab to a copy |
| 4428 | ** of p4.pTab. Finally, if p5 is true, indicating that this cursor was |
| 4429 | ** last moved with OP_Next or OP_Prev, not Seek or NotFound, set |
| 4430 | ** VdbeCursor.movetoTarget to the current rowid. */ |
drh | c556f3c | 2016-03-30 15:30:07 +0000 | [diff] [blame^] | 4431 | if( pOp->p4type==P4_TABLE && HAS_UPDATE_HOOK(db) ){ |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 4432 | assert( pC->iDb>=0 ); |
drh | c556f3c | 2016-03-30 15:30:07 +0000 | [diff] [blame^] | 4433 | assert( pOp->p4.pTab!=0 ); |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 4434 | zDb = db->aDb[pC->iDb].zName; |
dan | 319eeb7 | 2011-03-19 08:38:50 +0000 | [diff] [blame] | 4435 | pTab = pOp->p4.pTab; |
drh | c556f3c | 2016-03-30 15:30:07 +0000 | [diff] [blame^] | 4436 | if( (pOp->p5 & OPFLAG_SAVEPOSITION)!=0 && pC->isTable ){ |
drh | 1bb15fc | 2015-12-02 20:40:26 +0000 | [diff] [blame] | 4437 | sqlite3BtreeKeySize(pC->uc.pCursor, &pC->movetoTarget); |
dan | 438b881 | 2015-09-15 15:55:15 +0000 | [diff] [blame] | 4438 | } |
drh | 74c3302 | 2016-03-30 12:56:55 +0000 | [diff] [blame] | 4439 | }else{ |
| 4440 | zDb = 0; /* Not needed. Silence a compiler warning. */ |
| 4441 | pTab = 0; /* Not needed. Silence a compiler warning. */ |
drh | 92fe38e | 2014-10-14 13:41:32 +0000 | [diff] [blame] | 4442 | } |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 4443 | |
drh | 9b1c62d | 2011-03-30 21:04:43 +0000 | [diff] [blame] | 4444 | #ifdef SQLITE_ENABLE_PREUPDATE_HOOK |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 4445 | /* Invoke the pre-update-hook if required. */ |
dan | 438b881 | 2015-09-15 15:55:15 +0000 | [diff] [blame] | 4446 | if( db->xPreUpdateCallback && pOp->p4.pTab && HasRowid(pTab) ){ |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 4447 | assert( !(opflags & OPFLAG_ISUPDATE) || (aMem[pOp->p3].flags & MEM_Int) ); |
| 4448 | sqlite3VdbePreUpdateHook(p, pC, |
| 4449 | (opflags & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_DELETE, |
drh | 92fe38e | 2014-10-14 13:41:32 +0000 | [diff] [blame] | 4450 | zDb, pTab, pC->movetoTarget, |
dan | 37db03b | 2011-03-16 19:59:18 +0000 | [diff] [blame] | 4451 | pOp->p3 |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 4452 | ); |
| 4453 | } |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 4454 | if( opflags & OPFLAG_ISNOOP ) break; |
drh | c556f3c | 2016-03-30 15:30:07 +0000 | [diff] [blame^] | 4455 | #endif |
drh | b53a5a9 | 2014-10-12 22:37:22 +0000 | [diff] [blame] | 4456 | |
drh | def19e3 | 2016-01-27 16:26:25 +0000 | [diff] [blame] | 4457 | /* Only flags that can be set are SAVEPOISTION and AUXDELETE */ |
| 4458 | assert( (pOp->p5 & ~(OPFLAG_SAVEPOSITION|OPFLAG_AUXDELETE))==0 ); |
drh | e807bdb | 2016-01-21 17:06:33 +0000 | [diff] [blame] | 4459 | assert( OPFLAG_SAVEPOSITION==BTREE_SAVEPOSITION ); |
drh | def19e3 | 2016-01-27 16:26:25 +0000 | [diff] [blame] | 4460 | assert( OPFLAG_AUXDELETE==BTREE_AUXDELETE ); |
drh | b89aeb6 | 2016-01-27 15:49:32 +0000 | [diff] [blame] | 4461 | |
| 4462 | #ifdef SQLITE_DEBUG |
dan | e61bbf4 | 2016-01-28 17:06:17 +0000 | [diff] [blame] | 4463 | if( p->pFrame==0 ){ |
| 4464 | if( pC->isEphemeral==0 |
| 4465 | && (pOp->p5 & OPFLAG_AUXDELETE)==0 |
| 4466 | && (pC->wrFlag & OPFLAG_FORDELETE)==0 |
| 4467 | ){ |
| 4468 | nExtraDelete++; |
| 4469 | } |
| 4470 | if( pOp->p2 & OPFLAG_NCHANGE ){ |
| 4471 | nExtraDelete--; |
| 4472 | } |
drh | b89aeb6 | 2016-01-27 15:49:32 +0000 | [diff] [blame] | 4473 | } |
| 4474 | #endif |
| 4475 | |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 4476 | rc = sqlite3BtreeDelete(pC->uc.pCursor, pOp->p5); |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 4477 | pC->cacheStatus = CACHE_STALE; |
drh | d3e1af4 | 2016-02-25 18:54:30 +0000 | [diff] [blame] | 4478 | if( rc ) goto abort_due_to_error; |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 +0000 | [diff] [blame] | 4479 | |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 4480 | /* Invoke the update-hook if required. */ |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 4481 | if( opflags & OPFLAG_NCHANGE ){ |
| 4482 | p->nChange++; |
drh | c556f3c | 2016-03-30 15:30:07 +0000 | [diff] [blame^] | 4483 | if( db->xUpdateCallback && HasRowid(pTab) ){ |
drh | 92fe38e | 2014-10-14 13:41:32 +0000 | [diff] [blame] | 4484 | db->xUpdateCallback(db->pUpdateArg, SQLITE_DELETE, zDb, pTab->zName, |
dan | 438b881 | 2015-09-15 15:55:15 +0000 | [diff] [blame] | 4485 | pC->movetoTarget); |
| 4486 | assert( pC->iDb>=0 ); |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 4487 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4488 | } |
dan | 438b881 | 2015-09-15 15:55:15 +0000 | [diff] [blame] | 4489 | |
rdc | b0c374f | 2004-02-20 22:53:38 +0000 | [diff] [blame] | 4490 | break; |
| 4491 | } |
drh | b7f1d9a | 2009-09-08 02:27:58 +0000 | [diff] [blame] | 4492 | /* Opcode: ResetCount * * * * * |
rdc | b0c374f | 2004-02-20 22:53:38 +0000 | [diff] [blame] | 4493 | ** |
drh | b7f1d9a | 2009-09-08 02:27:58 +0000 | [diff] [blame] | 4494 | ** The value of the change counter is copied to the database handle |
| 4495 | ** change counter (returned by subsequent calls to sqlite3_changes()). |
| 4496 | ** Then the VMs internal change counter resets to 0. |
| 4497 | ** This is used by trigger programs. |
rdc | b0c374f | 2004-02-20 22:53:38 +0000 | [diff] [blame] | 4498 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4499 | case OP_ResetCount: { |
drh | b7f1d9a | 2009-09-08 02:27:58 +0000 | [diff] [blame] | 4500 | sqlite3VdbeSetChanges(db, p->nChange); |
danielk1977 | b28af71 | 2004-06-21 06:50:26 +0000 | [diff] [blame] | 4501 | p->nChange = 0; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4502 | break; |
| 4503 | } |
| 4504 | |
drh | 1153c7b | 2013-11-01 22:02:56 +0000 | [diff] [blame] | 4505 | /* Opcode: SorterCompare P1 P2 P3 P4 |
drh | ac50232 | 2014-07-30 13:56:48 +0000 | [diff] [blame] | 4506 | ** Synopsis: if key(P1)!=trim(r[P3],P4) goto P2 |
dan | 5134d13 | 2011-09-02 10:31:11 +0000 | [diff] [blame] | 4507 | ** |
drh | 1153c7b | 2013-11-01 22:02:56 +0000 | [diff] [blame] | 4508 | ** P1 is a sorter cursor. This instruction compares a prefix of the |
drh | bc5cf38 | 2014-08-06 01:08:07 +0000 | [diff] [blame] | 4509 | ** record blob in register P3 against a prefix of the entry that |
drh | ac50232 | 2014-07-30 13:56:48 +0000 | [diff] [blame] | 4510 | ** the sorter cursor currently points to. Only the first P4 fields |
| 4511 | ** of r[P3] and the sorter record are compared. |
drh | 1153c7b | 2013-11-01 22:02:56 +0000 | [diff] [blame] | 4512 | ** |
| 4513 | ** If either P3 or the sorter contains a NULL in one of their significant |
| 4514 | ** fields (not counting the P4 fields at the end which are ignored) then |
| 4515 | ** the comparison is assumed to be equal. |
| 4516 | ** |
| 4517 | ** Fall through to next instruction if the two records compare equal to |
| 4518 | ** each other. Jump to P2 if they are different. |
dan | 5134d13 | 2011-09-02 10:31:11 +0000 | [diff] [blame] | 4519 | */ |
| 4520 | case OP_SorterCompare: { |
| 4521 | VdbeCursor *pC; |
| 4522 | int res; |
drh | ac50232 | 2014-07-30 13:56:48 +0000 | [diff] [blame] | 4523 | int nKeyCol; |
dan | 5134d13 | 2011-09-02 10:31:11 +0000 | [diff] [blame] | 4524 | |
| 4525 | pC = p->apCsr[pOp->p1]; |
| 4526 | assert( isSorter(pC) ); |
drh | 1153c7b | 2013-11-01 22:02:56 +0000 | [diff] [blame] | 4527 | assert( pOp->p4type==P4_INT32 ); |
dan | 5134d13 | 2011-09-02 10:31:11 +0000 | [diff] [blame] | 4528 | pIn3 = &aMem[pOp->p3]; |
drh | ac50232 | 2014-07-30 13:56:48 +0000 | [diff] [blame] | 4529 | nKeyCol = pOp->p4.i; |
drh | 958d261 | 2014-04-18 13:40:07 +0000 | [diff] [blame] | 4530 | res = 0; |
drh | ac50232 | 2014-07-30 13:56:48 +0000 | [diff] [blame] | 4531 | rc = sqlite3VdbeSorterCompare(pC, pIn3, nKeyCol, &res); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 4532 | VdbeBranchTaken(res!=0,2); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 4533 | if( rc ) goto abort_due_to_error; |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 4534 | if( res ) goto jump_to_p2; |
dan | 5134d13 | 2011-09-02 10:31:11 +0000 | [diff] [blame] | 4535 | break; |
| 4536 | }; |
| 4537 | |
drh | 6cf4a7d | 2014-10-13 13:00:58 +0000 | [diff] [blame] | 4538 | /* Opcode: SorterData P1 P2 P3 * * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 4539 | ** Synopsis: r[P2]=data |
dan | 5134d13 | 2011-09-02 10:31:11 +0000 | [diff] [blame] | 4540 | ** |
| 4541 | ** Write into register P2 the current sorter data for sorter cursor P1. |
drh | 6cf4a7d | 2014-10-13 13:00:58 +0000 | [diff] [blame] | 4542 | ** Then clear the column header cache on cursor P3. |
| 4543 | ** |
| 4544 | ** This opcode is normally use to move a record out of the sorter and into |
| 4545 | ** a register that is the source for a pseudo-table cursor created using |
| 4546 | ** OpenPseudo. That pseudo-table cursor is the one that is identified by |
| 4547 | ** parameter P3. Clearing the P3 column cache as part of this opcode saves |
| 4548 | ** us from having to issue a separate NullRow instruction to clear that cache. |
dan | 5134d13 | 2011-09-02 10:31:11 +0000 | [diff] [blame] | 4549 | */ |
| 4550 | case OP_SorterData: { |
| 4551 | VdbeCursor *pC; |
drh | 3a94987 | 2012-09-18 13:20:13 +0000 | [diff] [blame] | 4552 | |
dan | 5134d13 | 2011-09-02 10:31:11 +0000 | [diff] [blame] | 4553 | pOut = &aMem[pOp->p2]; |
| 4554 | pC = p->apCsr[pOp->p1]; |
drh | 14da87f | 2013-11-20 21:51:33 +0000 | [diff] [blame] | 4555 | assert( isSorter(pC) ); |
dan | 5134d13 | 2011-09-02 10:31:11 +0000 | [diff] [blame] | 4556 | rc = sqlite3VdbeSorterRowkey(pC, pOut); |
dan | 3852413 | 2014-05-01 20:26:48 +0000 | [diff] [blame] | 4557 | assert( rc!=SQLITE_OK || (pOut->flags & MEM_Blob) ); |
drh | 6cf4a7d | 2014-10-13 13:00:58 +0000 | [diff] [blame] | 4558 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 4559 | if( rc ) goto abort_due_to_error; |
drh | 6cf4a7d | 2014-10-13 13:00:58 +0000 | [diff] [blame] | 4560 | p->apCsr[pOp->p3]->cacheStatus = CACHE_STALE; |
dan | 5134d13 | 2011-09-02 10:31:11 +0000 | [diff] [blame] | 4561 | break; |
| 4562 | } |
| 4563 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4564 | /* Opcode: RowData P1 P2 * * * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 4565 | ** Synopsis: r[P2]=data |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 4566 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4567 | ** Write into register P2 the complete row data for cursor P1. |
| 4568 | ** There is no interpretation of the data. |
| 4569 | ** It is just copied onto the P2 register exactly as |
danielk1977 | 96cb76f | 2008-01-04 13:24:28 +0000 | [diff] [blame] | 4570 | ** it is found in the database file. |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 4571 | ** |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 4572 | ** If the P1 cursor must be pointing to a valid row (not a NULL row) |
| 4573 | ** of a real table, not a pseudo-table. |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 4574 | */ |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4575 | /* Opcode: RowKey P1 P2 * * * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 4576 | ** Synopsis: r[P2]=key |
drh | 143f3c4 | 2004-01-07 20:37:52 +0000 | [diff] [blame] | 4577 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4578 | ** Write into register P2 the complete row key for cursor P1. |
| 4579 | ** There is no interpretation of the data. |
drh | 0fd6135 | 2014-02-07 02:29:45 +0000 | [diff] [blame] | 4580 | ** The key is copied onto the P2 register exactly as |
danielk1977 | 96cb76f | 2008-01-04 13:24:28 +0000 | [diff] [blame] | 4581 | ** it is found in the database file. |
drh | 143f3c4 | 2004-01-07 20:37:52 +0000 | [diff] [blame] | 4582 | ** |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 4583 | ** If the P1 cursor must be pointing to a valid row (not a NULL row) |
| 4584 | ** of a real table, not a pseudo-table. |
drh | 143f3c4 | 2004-01-07 20:37:52 +0000 | [diff] [blame] | 4585 | */ |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 4586 | case OP_RowKey: |
| 4587 | case OP_RowData: { |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 4588 | VdbeCursor *pC; |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 4589 | BtCursor *pCrsr; |
danielk1977 | e0d4b06 | 2004-06-28 01:11:46 +0000 | [diff] [blame] | 4590 | u32 n; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 4591 | i64 n64; |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 4592 | |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 4593 | pOut = &aMem[pOp->p2]; |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 4594 | memAboutToChange(p, pOut); |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 4595 | |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 4596 | /* Note that RowKey and RowData are really exactly the same instruction */ |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 4597 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 4598 | pC = p->apCsr[pOp->p1]; |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 4599 | assert( pC!=0 ); |
| 4600 | assert( pC->eCurType==CURTYPE_BTREE ); |
drh | 14da87f | 2013-11-20 21:51:33 +0000 | [diff] [blame] | 4601 | assert( isSorter(pC)==0 ); |
drh | c6aff30 | 2011-09-01 15:32:47 +0000 | [diff] [blame] | 4602 | assert( pC->isTable || pOp->opcode!=OP_RowData ); |
drh | 14da87f | 2013-11-20 21:51:33 +0000 | [diff] [blame] | 4603 | assert( pC->isTable==0 || pOp->opcode==OP_RowData ); |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 4604 | assert( pC->nullRow==0 ); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 4605 | assert( pC->uc.pCursor!=0 ); |
| 4606 | pCrsr = pC->uc.pCursor; |
drh | 9a65f2c | 2009-06-22 19:05:40 +0000 | [diff] [blame] | 4607 | |
| 4608 | /* The OP_RowKey and OP_RowData opcodes always follow OP_NotExists or |
| 4609 | ** OP_Rewind/Op_Next with no intervening instructions that might invalidate |
drh | c22284f | 2014-10-13 16:02:20 +0000 | [diff] [blame] | 4610 | ** the cursor. If this where not the case, on of the following assert()s |
| 4611 | ** would fail. Should this ever change (because of changes in the code |
| 4612 | ** generator) then the fix would be to insert a call to |
| 4613 | ** sqlite3VdbeCursorMoveto(). |
drh | 9a65f2c | 2009-06-22 19:05:40 +0000 | [diff] [blame] | 4614 | */ |
| 4615 | assert( pC->deferredMoveto==0 ); |
drh | c22284f | 2014-10-13 16:02:20 +0000 | [diff] [blame] | 4616 | assert( sqlite3BtreeCursorIsValid(pCrsr) ); |
| 4617 | #if 0 /* Not required due to the previous to assert() statements */ |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 4618 | rc = sqlite3VdbeCursorMoveto(pC); |
drh | c22284f | 2014-10-13 16:02:20 +0000 | [diff] [blame] | 4619 | if( rc!=SQLITE_OK ) goto abort_due_to_error; |
| 4620 | #endif |
drh | 9a65f2c | 2009-06-22 19:05:40 +0000 | [diff] [blame] | 4621 | |
drh | 14da87f | 2013-11-20 21:51:33 +0000 | [diff] [blame] | 4622 | if( pC->isTable==0 ){ |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 4623 | assert( !pC->isTable ); |
drh | b07028f | 2011-10-14 21:49:18 +0000 | [diff] [blame] | 4624 | VVA_ONLY(rc =) sqlite3BtreeKeySize(pCrsr, &n64); |
drh | c27ae61 | 2009-07-14 18:35:44 +0000 | [diff] [blame] | 4625 | assert( rc==SQLITE_OK ); /* True because of CursorMoveto() call above */ |
drh | bb4957f | 2008-03-20 14:03:29 +0000 | [diff] [blame] | 4626 | if( n64>db->aLimit[SQLITE_LIMIT_LENGTH] ){ |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 4627 | goto too_big; |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 4628 | } |
drh | bfb19dc | 2009-06-05 16:46:53 +0000 | [diff] [blame] | 4629 | n = (u32)n64; |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 4630 | }else{ |
drh | b07028f | 2011-10-14 21:49:18 +0000 | [diff] [blame] | 4631 | VVA_ONLY(rc =) sqlite3BtreeDataSize(pCrsr, &n); |
drh | ea8ffdf | 2009-07-22 00:35:23 +0000 | [diff] [blame] | 4632 | assert( rc==SQLITE_OK ); /* DataSize() cannot fail */ |
shane | 75ac1de | 2009-06-09 18:58:52 +0000 | [diff] [blame] | 4633 | if( n>(u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){ |
drh | 023ae03 | 2007-05-08 12:12:16 +0000 | [diff] [blame] | 4634 | goto too_big; |
| 4635 | } |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 4636 | } |
drh | 722246e | 2014-10-07 23:02:24 +0000 | [diff] [blame] | 4637 | testcase( n==0 ); |
| 4638 | if( sqlite3VdbeMemClearAndResize(pOut, MAX(n,32)) ){ |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 4639 | goto no_mem; |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 4640 | } |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 4641 | pOut->n = n; |
| 4642 | MemSetTypeFlag(pOut, MEM_Blob); |
drh | 14da87f | 2013-11-20 21:51:33 +0000 | [diff] [blame] | 4643 | if( pC->isTable==0 ){ |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 4644 | rc = sqlite3BtreeKey(pCrsr, 0, n, pOut->z); |
| 4645 | }else{ |
| 4646 | rc = sqlite3BtreeData(pCrsr, 0, n, pOut->z); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4647 | } |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 4648 | if( rc ) goto abort_due_to_error; |
danielk1977 | 96cb76f | 2008-01-04 13:24:28 +0000 | [diff] [blame] | 4649 | pOut->enc = SQLITE_UTF8; /* In case the blob is ever cast to text */ |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 4650 | UPDATE_MAX_BLOBSIZE(pOut); |
drh | ee0ec8e | 2013-10-31 17:38:01 +0000 | [diff] [blame] | 4651 | REGISTER_TRACE(pOp->p2, pOut); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4652 | break; |
| 4653 | } |
| 4654 | |
drh | 2133d82 | 2008-01-03 18:44:59 +0000 | [diff] [blame] | 4655 | /* Opcode: Rowid P1 P2 * * * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 4656 | ** Synopsis: r[P2]=rowid |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4657 | ** |
drh | 2133d82 | 2008-01-03 18:44:59 +0000 | [diff] [blame] | 4658 | ** 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] | 4659 | ** P1 is currently point to. |
drh | 044925b | 2009-04-22 17:15:02 +0000 | [diff] [blame] | 4660 | ** |
| 4661 | ** P1 can be either an ordinary table or a virtual table. There used to |
| 4662 | ** be a separate OP_VRowid opcode for use with virtual tables, but this |
| 4663 | ** one opcode now works for both table types. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4664 | */ |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 4665 | case OP_Rowid: { /* out2 */ |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 4666 | VdbeCursor *pC; |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 4667 | i64 v; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 4668 | sqlite3_vtab *pVtab; |
| 4669 | const sqlite3_module *pModule; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4670 | |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 4671 | pOut = out2Prerelease(p, pOp); |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 4672 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 4673 | pC = p->apCsr[pOp->p1]; |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 4674 | assert( pC!=0 ); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 4675 | assert( pC->eCurType!=CURTYPE_PSEUDO || pC->nullRow ); |
drh | 044925b | 2009-04-22 17:15:02 +0000 | [diff] [blame] | 4676 | if( pC->nullRow ){ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 4677 | pOut->flags = MEM_Null; |
drh | 044925b | 2009-04-22 17:15:02 +0000 | [diff] [blame] | 4678 | break; |
| 4679 | }else if( pC->deferredMoveto ){ |
drh | 6149526 | 2009-04-22 15:32:59 +0000 | [diff] [blame] | 4680 | v = pC->movetoTarget; |
drh | 044925b | 2009-04-22 17:15:02 +0000 | [diff] [blame] | 4681 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 4682 | }else if( pC->eCurType==CURTYPE_VTAB ){ |
| 4683 | assert( pC->uc.pVCur!=0 ); |
| 4684 | pVtab = pC->uc.pVCur->pVtab; |
drh | 044925b | 2009-04-22 17:15:02 +0000 | [diff] [blame] | 4685 | pModule = pVtab->pModule; |
| 4686 | assert( pModule->xRowid ); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 4687 | rc = pModule->xRowid(pC->uc.pVCur, &v); |
dan | 016f781 | 2013-08-21 17:35:48 +0000 | [diff] [blame] | 4688 | sqlite3VtabImportErrmsg(p, pVtab); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 4689 | if( rc ) goto abort_due_to_error; |
drh | 044925b | 2009-04-22 17:15:02 +0000 | [diff] [blame] | 4690 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 4691 | }else{ |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 4692 | assert( pC->eCurType==CURTYPE_BTREE ); |
| 4693 | assert( pC->uc.pCursor!=0 ); |
drh | c22284f | 2014-10-13 16:02:20 +0000 | [diff] [blame] | 4694 | rc = sqlite3VdbeCursorRestore(pC); |
drh | 6149526 | 2009-04-22 15:32:59 +0000 | [diff] [blame] | 4695 | if( rc ) goto abort_due_to_error; |
dan | 2b8669a | 2014-11-17 19:42:48 +0000 | [diff] [blame] | 4696 | if( pC->nullRow ){ |
| 4697 | pOut->flags = MEM_Null; |
| 4698 | break; |
| 4699 | } |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 4700 | rc = sqlite3BtreeKeySize(pC->uc.pCursor, &v); |
drh | c22284f | 2014-10-13 16:02:20 +0000 | [diff] [blame] | 4701 | assert( rc==SQLITE_OK ); /* Always so because of CursorRestore() above */ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4702 | } |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 4703 | pOut->u.i = v; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4704 | break; |
| 4705 | } |
| 4706 | |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4707 | /* Opcode: NullRow P1 * * * * |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 4708 | ** |
| 4709 | ** Move the cursor P1 to a null row. Any OP_Column operations |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4710 | ** that occur while the cursor is on the null row will always |
| 4711 | ** write a NULL. |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 4712 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4713 | case OP_NullRow: { |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 4714 | VdbeCursor *pC; |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 4715 | |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 4716 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 4717 | pC = p->apCsr[pOp->p1]; |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 4718 | assert( pC!=0 ); |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame] | 4719 | pC->nullRow = 1; |
drh | 399af1d | 2013-11-20 17:25:55 +0000 | [diff] [blame] | 4720 | pC->cacheStatus = CACHE_STALE; |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 4721 | if( pC->eCurType==CURTYPE_BTREE ){ |
| 4722 | assert( pC->uc.pCursor!=0 ); |
| 4723 | sqlite3BtreeClearCursor(pC->uc.pCursor); |
danielk1977 | be51a65 | 2008-10-08 17:58:48 +0000 | [diff] [blame] | 4724 | } |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 4725 | break; |
| 4726 | } |
| 4727 | |
dan | b18e60b | 2015-04-01 16:18:00 +0000 | [diff] [blame] | 4728 | /* Opcode: Last P1 P2 P3 * * |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 4729 | ** |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 4730 | ** The next use of the Rowid or Column or Prev instruction for P1 |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 4731 | ** will refer to the last entry in the database table or index. |
| 4732 | ** If the table or index is empty and P2>0, then jump immediately to P2. |
| 4733 | ** If P2 is 0 or if the table or index is not empty, fall through |
| 4734 | ** to the following instruction. |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 4735 | ** |
| 4736 | ** This opcode leaves the cursor configured to move in reverse order, |
| 4737 | ** from the end toward the beginning. In other words, the cursor is |
drh | 5dad9a3 | 2014-07-25 18:37:42 +0000 | [diff] [blame] | 4738 | ** configured to use Prev, not Next. |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 4739 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4740 | case OP_Last: { /* jump */ |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 4741 | VdbeCursor *pC; |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 4742 | BtCursor *pCrsr; |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 4743 | int res; |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 4744 | |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 4745 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 4746 | pC = p->apCsr[pOp->p1]; |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 4747 | assert( pC!=0 ); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 4748 | assert( pC->eCurType==CURTYPE_BTREE ); |
| 4749 | pCrsr = pC->uc.pCursor; |
drh | 7abc540 | 2011-10-22 21:00:46 +0000 | [diff] [blame] | 4750 | res = 0; |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 4751 | assert( pCrsr!=0 ); |
| 4752 | rc = sqlite3BtreeLast(pCrsr, &res); |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 4753 | pC->nullRow = (u8)res; |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 4754 | pC->deferredMoveto = 0; |
| 4755 | pC->cacheStatus = CACHE_STALE; |
dan | b18e60b | 2015-04-01 16:18:00 +0000 | [diff] [blame] | 4756 | pC->seekResult = pOp->p3; |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 4757 | #ifdef SQLITE_DEBUG |
| 4758 | pC->seekOp = OP_Last; |
| 4759 | #endif |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 4760 | if( rc ) goto abort_due_to_error; |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 4761 | if( pOp->p2>0 ){ |
| 4762 | VdbeBranchTaken(res!=0,2); |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 4763 | if( res ) goto jump_to_p2; |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 4764 | } |
| 4765 | break; |
| 4766 | } |
| 4767 | |
drh | 0342b1f | 2005-09-01 03:07:44 +0000 | [diff] [blame] | 4768 | |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4769 | /* Opcode: Sort P1 P2 * * * |
drh | 0342b1f | 2005-09-01 03:07:44 +0000 | [diff] [blame] | 4770 | ** |
| 4771 | ** This opcode does exactly the same thing as OP_Rewind except that |
| 4772 | ** it increments an undocumented global variable used for testing. |
| 4773 | ** |
| 4774 | ** Sorting is accomplished by writing records into a sorting index, |
| 4775 | ** then rewinding that index and playing it back from beginning to |
| 4776 | ** end. We use the OP_Sort opcode instead of OP_Rewind to do the |
| 4777 | ** rewinding so that the global variable will be incremented and |
| 4778 | ** regression tests can determine whether or not the optimizer is |
| 4779 | ** correctly optimizing out sorts. |
| 4780 | */ |
drh | c6aff30 | 2011-09-01 15:32:47 +0000 | [diff] [blame] | 4781 | case OP_SorterSort: /* jump */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4782 | case OP_Sort: { /* jump */ |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 4783 | #ifdef SQLITE_TEST |
drh | 0342b1f | 2005-09-01 03:07:44 +0000 | [diff] [blame] | 4784 | sqlite3_sort_count++; |
drh | 4db38a7 | 2005-09-01 12:16:28 +0000 | [diff] [blame] | 4785 | sqlite3_search_count--; |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 4786 | #endif |
drh | 9b47ee3 | 2013-08-20 03:13:51 +0000 | [diff] [blame] | 4787 | p->aCounter[SQLITE_STMTSTATUS_SORT]++; |
drh | 0342b1f | 2005-09-01 03:07:44 +0000 | [diff] [blame] | 4788 | /* Fall through into OP_Rewind */ |
| 4789 | } |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4790 | /* Opcode: Rewind P1 P2 * * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4791 | ** |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 4792 | ** The next use of the Rowid or Column or Next instruction for P1 |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 4793 | ** will refer to the first entry in the database table or index. |
dan | 04489b6 | 2014-10-31 20:11:32 +0000 | [diff] [blame] | 4794 | ** If the table or index is empty, jump immediately to P2. |
| 4795 | ** If the table or index is not empty, fall through to the following |
| 4796 | ** instruction. |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 4797 | ** |
| 4798 | ** This opcode leaves the cursor configured to move in forward order, |
drh | 4ed2fb9 | 2014-08-14 13:06:25 +0000 | [diff] [blame] | 4799 | ** from the beginning toward the end. In other words, the cursor is |
drh | 5dad9a3 | 2014-07-25 18:37:42 +0000 | [diff] [blame] | 4800 | ** configured to use Next, not Prev. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4801 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4802 | case OP_Rewind: { /* jump */ |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 4803 | VdbeCursor *pC; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4804 | BtCursor *pCrsr; |
drh | f4dada7 | 2004-05-11 09:57:35 +0000 | [diff] [blame] | 4805 | int res; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4806 | |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 4807 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 4808 | pC = p->apCsr[pOp->p1]; |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 4809 | assert( pC!=0 ); |
drh | 14da87f | 2013-11-20 21:51:33 +0000 | [diff] [blame] | 4810 | assert( isSorter(pC)==(pOp->opcode==OP_SorterSort) ); |
dan | 2411dea | 2010-07-03 05:56:09 +0000 | [diff] [blame] | 4811 | res = 1; |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 4812 | #ifdef SQLITE_DEBUG |
| 4813 | pC->seekOp = OP_Rewind; |
| 4814 | #endif |
dan | 689ab89 | 2011-08-12 15:02:00 +0000 | [diff] [blame] | 4815 | if( isSorter(pC) ){ |
drh | 958d261 | 2014-04-18 13:40:07 +0000 | [diff] [blame] | 4816 | rc = sqlite3VdbeSorterRewind(pC, &res); |
dan | a205a48 | 2011-08-27 18:48:57 +0000 | [diff] [blame] | 4817 | }else{ |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 4818 | assert( pC->eCurType==CURTYPE_BTREE ); |
| 4819 | pCrsr = pC->uc.pCursor; |
dan | a205a48 | 2011-08-27 18:48:57 +0000 | [diff] [blame] | 4820 | assert( pCrsr ); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 4821 | rc = sqlite3BtreeFirst(pCrsr, &res); |
drh | a11846b | 2004-01-07 18:52:56 +0000 | [diff] [blame] | 4822 | pC->deferredMoveto = 0; |
drh | 76873ab | 2006-01-07 18:48:26 +0000 | [diff] [blame] | 4823 | pC->cacheStatus = CACHE_STALE; |
drh | f4dada7 | 2004-05-11 09:57:35 +0000 | [diff] [blame] | 4824 | } |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 4825 | if( rc ) goto abort_due_to_error; |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 4826 | pC->nullRow = (u8)res; |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 4827 | assert( pOp->p2>0 && pOp->p2<p->nOp ); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 4828 | VdbeBranchTaken(res!=0,2); |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 4829 | if( res ) goto jump_to_p2; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4830 | break; |
| 4831 | } |
| 4832 | |
drh | 0fd6135 | 2014-02-07 02:29:45 +0000 | [diff] [blame] | 4833 | /* Opcode: Next P1 P2 P3 P4 P5 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4834 | ** |
| 4835 | ** 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] | 4836 | ** table or index. If there are no more key/value pairs then fall through |
| 4837 | ** to the following instruction. But if the cursor advance was successful, |
| 4838 | ** jump immediately to P2. |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 4839 | ** |
drh | 5dad9a3 | 2014-07-25 18:37:42 +0000 | [diff] [blame] | 4840 | ** The Next opcode is only valid following an SeekGT, SeekGE, or |
| 4841 | ** OP_Rewind opcode used to position the cursor. Next is not allowed |
| 4842 | ** to follow SeekLT, SeekLE, or OP_Last. |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 4843 | ** |
drh | f93cd94 | 2013-11-21 03:12:25 +0000 | [diff] [blame] | 4844 | ** The P1 cursor must be for a real table, not a pseudo-table. P1 must have |
| 4845 | ** been opened prior to this opcode or the program will segfault. |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 4846 | ** |
drh | e39a732 | 2014-02-03 14:04:11 +0000 | [diff] [blame] | 4847 | ** The P3 value is a hint to the btree implementation. If P3==1, that |
| 4848 | ** means P1 is an SQL index and that this instruction could have been |
| 4849 | ** omitted if that index had been unique. P3 is usually 0. P3 is |
| 4850 | ** always either 0 or 1. |
| 4851 | ** |
dan | a205a48 | 2011-08-27 18:48:57 +0000 | [diff] [blame] | 4852 | ** P4 is always of type P4_ADVANCE. The function pointer points to |
| 4853 | ** sqlite3BtreeNext(). |
| 4854 | ** |
drh | afc266a | 2010-03-31 17:47:44 +0000 | [diff] [blame] | 4855 | ** If P5 is positive and the jump is taken, then event counter |
| 4856 | ** number P5-1 in the prepared statement is incremented. |
| 4857 | ** |
drh | f93cd94 | 2013-11-21 03:12:25 +0000 | [diff] [blame] | 4858 | ** See also: Prev, NextIfOpen |
| 4859 | */ |
drh | 0fd6135 | 2014-02-07 02:29:45 +0000 | [diff] [blame] | 4860 | /* Opcode: NextIfOpen P1 P2 P3 P4 P5 |
drh | f93cd94 | 2013-11-21 03:12:25 +0000 | [diff] [blame] | 4861 | ** |
drh | 5dad9a3 | 2014-07-25 18:37:42 +0000 | [diff] [blame] | 4862 | ** This opcode works just like Next except that if cursor P1 is not |
drh | f93cd94 | 2013-11-21 03:12:25 +0000 | [diff] [blame] | 4863 | ** open it behaves a no-op. |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 4864 | */ |
drh | 0fd6135 | 2014-02-07 02:29:45 +0000 | [diff] [blame] | 4865 | /* Opcode: Prev P1 P2 P3 P4 P5 |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 4866 | ** |
| 4867 | ** Back up cursor P1 so that it points to the previous key/data pair in its |
| 4868 | ** table or index. If there is no previous key/value pairs then fall through |
| 4869 | ** to the following instruction. But if the cursor backup was successful, |
| 4870 | ** jump immediately to P2. |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 4871 | ** |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 4872 | ** |
drh | 5dad9a3 | 2014-07-25 18:37:42 +0000 | [diff] [blame] | 4873 | ** The Prev opcode is only valid following an SeekLT, SeekLE, or |
| 4874 | ** OP_Last opcode used to position the cursor. Prev is not allowed |
| 4875 | ** to follow SeekGT, SeekGE, or OP_Rewind. |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 4876 | ** |
drh | f93cd94 | 2013-11-21 03:12:25 +0000 | [diff] [blame] | 4877 | ** The P1 cursor must be for a real table, not a pseudo-table. If P1 is |
| 4878 | ** not open then the behavior is undefined. |
drh | afc266a | 2010-03-31 17:47:44 +0000 | [diff] [blame] | 4879 | ** |
drh | e39a732 | 2014-02-03 14:04:11 +0000 | [diff] [blame] | 4880 | ** The P3 value is a hint to the btree implementation. If P3==1, that |
| 4881 | ** means P1 is an SQL index and that this instruction could have been |
| 4882 | ** omitted if that index had been unique. P3 is usually 0. P3 is |
| 4883 | ** always either 0 or 1. |
| 4884 | ** |
dan | a205a48 | 2011-08-27 18:48:57 +0000 | [diff] [blame] | 4885 | ** P4 is always of type P4_ADVANCE. The function pointer points to |
| 4886 | ** sqlite3BtreePrevious(). |
| 4887 | ** |
drh | afc266a | 2010-03-31 17:47:44 +0000 | [diff] [blame] | 4888 | ** If P5 is positive and the jump is taken, then event counter |
| 4889 | ** number P5-1 in the prepared statement is incremented. |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 4890 | */ |
drh | 0fd6135 | 2014-02-07 02:29:45 +0000 | [diff] [blame] | 4891 | /* Opcode: PrevIfOpen P1 P2 P3 P4 P5 |
drh | f93cd94 | 2013-11-21 03:12:25 +0000 | [diff] [blame] | 4892 | ** |
drh | 5dad9a3 | 2014-07-25 18:37:42 +0000 | [diff] [blame] | 4893 | ** This opcode works just like Prev except that if cursor P1 is not |
drh | f93cd94 | 2013-11-21 03:12:25 +0000 | [diff] [blame] | 4894 | ** open it behaves a no-op. |
| 4895 | */ |
| 4896 | case OP_SorterNext: { /* jump */ |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 4897 | VdbeCursor *pC; |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 4898 | int res; |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 4899 | |
drh | f93cd94 | 2013-11-21 03:12:25 +0000 | [diff] [blame] | 4900 | pC = p->apCsr[pOp->p1]; |
| 4901 | assert( isSorter(pC) ); |
drh | 323913c | 2014-03-23 16:29:23 +0000 | [diff] [blame] | 4902 | res = 0; |
drh | f93cd94 | 2013-11-21 03:12:25 +0000 | [diff] [blame] | 4903 | rc = sqlite3VdbeSorterNext(db, pC, &res); |
| 4904 | goto next_tail; |
| 4905 | case OP_PrevIfOpen: /* jump */ |
| 4906 | case OP_NextIfOpen: /* jump */ |
| 4907 | if( p->apCsr[pOp->p1]==0 ) break; |
| 4908 | /* Fall through */ |
| 4909 | case OP_Prev: /* jump */ |
| 4910 | case OP_Next: /* jump */ |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 4911 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
drh | 9b47ee3 | 2013-08-20 03:13:51 +0000 | [diff] [blame] | 4912 | assert( pOp->p5<ArraySize(p->aCounter) ); |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame] | 4913 | pC = p->apCsr[pOp->p1]; |
drh | e39a732 | 2014-02-03 14:04:11 +0000 | [diff] [blame] | 4914 | res = pOp->p3; |
drh | f93cd94 | 2013-11-21 03:12:25 +0000 | [diff] [blame] | 4915 | assert( pC!=0 ); |
| 4916 | assert( pC->deferredMoveto==0 ); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 4917 | assert( pC->eCurType==CURTYPE_BTREE ); |
drh | e39a732 | 2014-02-03 14:04:11 +0000 | [diff] [blame] | 4918 | assert( res==0 || (res==1 && pC->isTable==0) ); |
| 4919 | testcase( res==1 ); |
drh | f93cd94 | 2013-11-21 03:12:25 +0000 | [diff] [blame] | 4920 | assert( pOp->opcode!=OP_Next || pOp->p4.xAdvance==sqlite3BtreeNext ); |
| 4921 | assert( pOp->opcode!=OP_Prev || pOp->p4.xAdvance==sqlite3BtreePrevious ); |
| 4922 | assert( pOp->opcode!=OP_NextIfOpen || pOp->p4.xAdvance==sqlite3BtreeNext ); |
| 4923 | assert( pOp->opcode!=OP_PrevIfOpen || pOp->p4.xAdvance==sqlite3BtreePrevious); |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 4924 | |
| 4925 | /* The Next opcode is only used after SeekGT, SeekGE, and Rewind. |
| 4926 | ** The Prev opcode is only used after SeekLT, SeekLE, and Last. */ |
| 4927 | assert( pOp->opcode!=OP_Next || pOp->opcode!=OP_NextIfOpen |
| 4928 | || pC->seekOp==OP_SeekGT || pC->seekOp==OP_SeekGE |
drh | cefc87f | 2014-08-01 01:40:33 +0000 | [diff] [blame] | 4929 | || pC->seekOp==OP_Rewind || pC->seekOp==OP_Found); |
drh | 8af3f77 | 2014-07-25 18:01:06 +0000 | [diff] [blame] | 4930 | assert( pOp->opcode!=OP_Prev || pOp->opcode!=OP_PrevIfOpen |
| 4931 | || pC->seekOp==OP_SeekLT || pC->seekOp==OP_SeekLE |
| 4932 | || pC->seekOp==OP_Last ); |
| 4933 | |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 4934 | rc = pOp->p4.xAdvance(pC->uc.pCursor, &res); |
drh | f93cd94 | 2013-11-21 03:12:25 +0000 | [diff] [blame] | 4935 | next_tail: |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 4936 | pC->cacheStatus = CACHE_STALE; |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 4937 | VdbeBranchTaken(res==0,2); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 4938 | if( rc ) goto abort_due_to_error; |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 4939 | if( res==0 ){ |
drh | f93cd94 | 2013-11-21 03:12:25 +0000 | [diff] [blame] | 4940 | pC->nullRow = 0; |
drh | 9b47ee3 | 2013-08-20 03:13:51 +0000 | [diff] [blame] | 4941 | p->aCounter[pOp->p5]++; |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 4942 | #ifdef SQLITE_TEST |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 4943 | sqlite3_search_count++; |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 4944 | #endif |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 4945 | goto jump_to_p2_and_check_for_interrupt; |
drh | f93cd94 | 2013-11-21 03:12:25 +0000 | [diff] [blame] | 4946 | }else{ |
| 4947 | pC->nullRow = 1; |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 4948 | } |
drh | 49afe3a | 2013-07-10 03:05:14 +0000 | [diff] [blame] | 4949 | goto check_for_interrupt; |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 4950 | } |
| 4951 | |
danielk1977 | de63035 | 2009-05-04 11:42:29 +0000 | [diff] [blame] | 4952 | /* Opcode: IdxInsert P1 P2 P3 * P5 |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 4953 | ** Synopsis: key=r[P2] |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4954 | ** |
drh | ef8662b | 2011-06-20 21:47:58 +0000 | [diff] [blame] | 4955 | ** Register P2 holds an SQL index key made using the |
drh | 9437bd2 | 2009-02-01 00:29:56 +0000 | [diff] [blame] | 4956 | ** MakeRecord instructions. This opcode writes that key |
drh | ee32e0a | 2006-01-10 19:45:49 +0000 | [diff] [blame] | 4957 | ** into the index P1. Data for the entry is nil. |
drh | 717e640 | 2001-09-27 03:22:32 +0000 | [diff] [blame] | 4958 | ** |
drh | aa9b896 | 2008-01-08 02:57:55 +0000 | [diff] [blame] | 4959 | ** P3 is a flag that provides a hint to the b-tree layer that this |
drh | e4d9081 | 2007-03-29 05:51:49 +0000 | [diff] [blame] | 4960 | ** insert is likely to be an append. |
| 4961 | ** |
mistachkin | 21a919f | 2014-02-07 03:28:02 +0000 | [diff] [blame] | 4962 | ** If P5 has the OPFLAG_NCHANGE bit set, then the change counter is |
| 4963 | ** incremented by this instruction. If the OPFLAG_NCHANGE bit is clear, |
| 4964 | ** then the change counter is unchanged. |
drh | 0fd6135 | 2014-02-07 02:29:45 +0000 | [diff] [blame] | 4965 | ** |
mistachkin | 21a919f | 2014-02-07 03:28:02 +0000 | [diff] [blame] | 4966 | ** If P5 has the OPFLAG_USESEEKRESULT bit set, then the cursor must have |
| 4967 | ** just done a seek to the spot where the new entry is to be inserted. |
| 4968 | ** This flag avoids doing an extra seek. |
drh | 0fd6135 | 2014-02-07 02:29:45 +0000 | [diff] [blame] | 4969 | ** |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 4970 | ** This instruction only works for indices. The equivalent instruction |
| 4971 | ** for tables is OP_Insert. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4972 | */ |
drh | ca892a7 | 2011-09-03 00:17:51 +0000 | [diff] [blame] | 4973 | case OP_SorterInsert: /* in2 */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4974 | case OP_IdxInsert: { /* in2 */ |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 4975 | VdbeCursor *pC; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 4976 | int nKey; |
| 4977 | const char *zKey; |
| 4978 | |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 4979 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 4980 | pC = p->apCsr[pOp->p1]; |
| 4981 | assert( pC!=0 ); |
drh | 14da87f | 2013-11-20 21:51:33 +0000 | [diff] [blame] | 4982 | assert( isSorter(pC)==(pOp->opcode==OP_SorterInsert) ); |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 4983 | pIn2 = &aMem[pOp->p2]; |
drh | aa9b896 | 2008-01-08 02:57:55 +0000 | [diff] [blame] | 4984 | assert( pIn2->flags & MEM_Blob ); |
drh | 6546af1 | 2013-11-04 15:23:25 +0000 | [diff] [blame] | 4985 | if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++; |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 4986 | assert( pC->eCurType==CURTYPE_BTREE || pOp->opcode==OP_SorterInsert ); |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 4987 | assert( pC->isTable==0 ); |
| 4988 | rc = ExpandBlob(pIn2); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 4989 | if( rc ) goto abort_due_to_error; |
| 4990 | if( pOp->opcode==OP_SorterInsert ){ |
| 4991 | rc = sqlite3VdbeSorterWrite(pC, pIn2); |
| 4992 | }else{ |
| 4993 | nKey = pIn2->n; |
| 4994 | zKey = pIn2->z; |
| 4995 | rc = sqlite3BtreeInsert(pC->uc.pCursor, zKey, nKey, "", 0, 0, pOp->p3, |
| 4996 | ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0) |
| 4997 | ); |
| 4998 | assert( pC->deferredMoveto==0 ); |
| 4999 | pC->cacheStatus = CACHE_STALE; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5000 | } |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 5001 | if( rc) goto abort_due_to_error; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5002 | break; |
| 5003 | } |
| 5004 | |
drh | d1d3848 | 2008-10-07 23:46:38 +0000 | [diff] [blame] | 5005 | /* Opcode: IdxDelete P1 P2 P3 * * |
drh | f63552b | 2013-10-30 00:25:03 +0000 | [diff] [blame] | 5006 | ** Synopsis: key=r[P2@P3] |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5007 | ** |
drh | e14006d | 2008-03-25 17:23:32 +0000 | [diff] [blame] | 5008 | ** The content of P3 registers starting at register P2 form |
| 5009 | ** an unpacked index key. This opcode removes that entry from the |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 5010 | ** index opened by cursor P1. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5011 | */ |
drh | e14006d | 2008-03-25 17:23:32 +0000 | [diff] [blame] | 5012 | case OP_IdxDelete: { |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 5013 | VdbeCursor *pC; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5014 | BtCursor *pCrsr; |
drh | 9a65f2c | 2009-06-22 19:05:40 +0000 | [diff] [blame] | 5015 | int res; |
| 5016 | UnpackedRecord r; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 5017 | |
drh | e14006d | 2008-03-25 17:23:32 +0000 | [diff] [blame] | 5018 | assert( pOp->p3>0 ); |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 5019 | 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] | 5020 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 5021 | pC = p->apCsr[pOp->p1]; |
| 5022 | assert( pC!=0 ); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 5023 | assert( pC->eCurType==CURTYPE_BTREE ); |
| 5024 | pCrsr = pC->uc.pCursor; |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 5025 | assert( pCrsr!=0 ); |
drh | 4308e34 | 2013-11-11 16:55:52 +0000 | [diff] [blame] | 5026 | assert( pOp->p5==0 ); |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 5027 | r.pKeyInfo = pC->pKeyInfo; |
| 5028 | r.nField = (u16)pOp->p3; |
dan | 1fed5da | 2014-02-25 21:01:25 +0000 | [diff] [blame] | 5029 | r.default_rc = 0; |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 5030 | r.aMem = &aMem[pOp->p2]; |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 5031 | rc = sqlite3BtreeMovetoUnpacked(pCrsr, &r, 0, 0, &res); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 5032 | if( rc ) goto abort_due_to_error; |
| 5033 | if( res==0 ){ |
dan | e61bbf4 | 2016-01-28 17:06:17 +0000 | [diff] [blame] | 5034 | rc = sqlite3BtreeDelete(pCrsr, BTREE_AUXDELETE); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 5035 | if( rc ) goto abort_due_to_error; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5036 | } |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 5037 | assert( pC->deferredMoveto==0 ); |
| 5038 | pC->cacheStatus = CACHE_STALE; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5039 | break; |
| 5040 | } |
| 5041 | |
drh | 784c1b9 | 2016-01-30 16:59:56 +0000 | [diff] [blame] | 5042 | /* Opcode: Seek P1 * P3 P4 * |
| 5043 | ** Synopsis: Move P3 to P1.rowid |
| 5044 | ** |
| 5045 | ** P1 is an open index cursor and P3 is a cursor on the corresponding |
| 5046 | ** table. This opcode does a deferred seek of the P3 table cursor |
| 5047 | ** to the row that corresponds to the current row of P1. |
| 5048 | ** |
| 5049 | ** This is a deferred seek. Nothing actually happens until |
| 5050 | ** the cursor is used to read a record. That way, if no reads |
| 5051 | ** occur, no unnecessary I/O happens. |
| 5052 | ** |
| 5053 | ** P4 may be an array of integers (type P4_INTARRAY) containing |
drh | 19d720d | 2016-02-03 19:52:06 +0000 | [diff] [blame] | 5054 | ** one entry for each column in the P3 table. If array entry a(i) |
| 5055 | ** is non-zero, then reading column a(i)-1 from cursor P3 is |
drh | 784c1b9 | 2016-01-30 16:59:56 +0000 | [diff] [blame] | 5056 | ** equivalent to performing the deferred seek and then reading column i |
| 5057 | ** from P1. This information is stored in P3 and used to redirect |
| 5058 | ** reads against P3 over to P1, thus possibly avoiding the need to |
| 5059 | ** seek and read cursor P3. |
| 5060 | */ |
drh | 2133d82 | 2008-01-03 18:44:59 +0000 | [diff] [blame] | 5061 | /* Opcode: IdxRowid P1 P2 * * * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 5062 | ** Synopsis: r[P2]=rowid |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 5063 | ** |
drh | 2133d82 | 2008-01-03 18:44:59 +0000 | [diff] [blame] | 5064 | ** 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] | 5065 | ** the end of the index key pointed to by cursor P1. This integer should be |
| 5066 | ** the rowid of the table entry to which this index entry points. |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 5067 | ** |
drh | 9437bd2 | 2009-02-01 00:29:56 +0000 | [diff] [blame] | 5068 | ** See also: Rowid, MakeRecord. |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 5069 | */ |
drh | 784c1b9 | 2016-01-30 16:59:56 +0000 | [diff] [blame] | 5070 | case OP_Seek: |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 5071 | case OP_IdxRowid: { /* out2 */ |
drh | 784c1b9 | 2016-01-30 16:59:56 +0000 | [diff] [blame] | 5072 | VdbeCursor *pC; /* The P1 index cursor */ |
| 5073 | VdbeCursor *pTabCur; /* The P2 table cursor (OP_Seek only) */ |
| 5074 | i64 rowid; /* Rowid that P1 current points to */ |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 5075 | |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 5076 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 5077 | pC = p->apCsr[pOp->p1]; |
| 5078 | assert( pC!=0 ); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 5079 | assert( pC->eCurType==CURTYPE_BTREE ); |
drh | 784c1b9 | 2016-01-30 16:59:56 +0000 | [diff] [blame] | 5080 | assert( pC->uc.pCursor!=0 ); |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 5081 | assert( pC->isTable==0 ); |
drh | c22284f | 2014-10-13 16:02:20 +0000 | [diff] [blame] | 5082 | assert( pC->deferredMoveto==0 ); |
drh | 784c1b9 | 2016-01-30 16:59:56 +0000 | [diff] [blame] | 5083 | assert( !pC->nullRow || pOp->opcode==OP_IdxRowid ); |
| 5084 | |
| 5085 | /* The IdxRowid and Seek opcodes are combined because of the commonality |
| 5086 | ** of sqlite3VdbeCursorRestore() and sqlite3VdbeIdxRowid(). */ |
| 5087 | rc = sqlite3VdbeCursorRestore(pC); |
drh | c22284f | 2014-10-13 16:02:20 +0000 | [diff] [blame] | 5088 | |
| 5089 | /* sqlite3VbeCursorRestore() can only fail if the record has been deleted |
drh | 784c1b9 | 2016-01-30 16:59:56 +0000 | [diff] [blame] | 5090 | ** out from under the cursor. That will never happens for an IdxRowid |
| 5091 | ** or Seek opcode */ |
drh | c22284f | 2014-10-13 16:02:20 +0000 | [diff] [blame] | 5092 | if( NEVER(rc!=SQLITE_OK) ) goto abort_due_to_error; |
| 5093 | |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 5094 | if( !pC->nullRow ){ |
drh | 2dc0648 | 2013-12-11 00:59:10 +0000 | [diff] [blame] | 5095 | rowid = 0; /* Not needed. Only used to silence a warning. */ |
drh | 784c1b9 | 2016-01-30 16:59:56 +0000 | [diff] [blame] | 5096 | rc = sqlite3VdbeIdxRowid(db, pC->uc.pCursor, &rowid); |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 5097 | if( rc!=SQLITE_OK ){ |
| 5098 | goto abort_due_to_error; |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 5099 | } |
drh | 784c1b9 | 2016-01-30 16:59:56 +0000 | [diff] [blame] | 5100 | if( pOp->opcode==OP_Seek ){ |
| 5101 | assert( pOp->p3>=0 && pOp->p3<p->nCursor ); |
| 5102 | pTabCur = p->apCsr[pOp->p3]; |
| 5103 | assert( pTabCur!=0 ); |
| 5104 | assert( pTabCur->eCurType==CURTYPE_BTREE ); |
| 5105 | assert( pTabCur->uc.pCursor!=0 ); |
| 5106 | assert( pTabCur->isTable ); |
| 5107 | pTabCur->nullRow = 0; |
| 5108 | pTabCur->movetoTarget = rowid; |
| 5109 | pTabCur->deferredMoveto = 1; |
| 5110 | assert( pOp->p4type==P4_INTARRAY || pOp->p4.ai==0 ); |
| 5111 | pTabCur->aAltMap = pOp->p4.ai; |
| 5112 | pTabCur->pAltCursor = pC; |
| 5113 | }else{ |
| 5114 | pOut = out2Prerelease(p, pOp); |
| 5115 | pOut->u.i = rowid; |
| 5116 | pOut->flags = MEM_Int; |
| 5117 | } |
| 5118 | }else{ |
| 5119 | assert( pOp->opcode==OP_IdxRowid ); |
| 5120 | sqlite3VdbeMemSetNull(&aMem[pOp->p2]); |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 5121 | } |
| 5122 | break; |
| 5123 | } |
| 5124 | |
danielk1977 | 61dd583 | 2008-04-18 11:31:12 +0000 | [diff] [blame] | 5125 | /* Opcode: IdxGE P1 P2 P3 P4 P5 |
drh | f63552b | 2013-10-30 00:25:03 +0000 | [diff] [blame] | 5126 | ** Synopsis: key=r[P3@P4] |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 5127 | ** |
danielk1977 | 61dd583 | 2008-04-18 11:31:12 +0000 | [diff] [blame] | 5128 | ** The P4 register values beginning with P3 form an unpacked index |
drh | 4a1d365 | 2014-02-14 15:13:36 +0000 | [diff] [blame] | 5129 | ** key that omits the PRIMARY KEY. Compare this key value against the index |
| 5130 | ** that P1 is currently pointing to, ignoring the PRIMARY KEY or ROWID |
| 5131 | ** fields at the end. |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 5132 | ** |
danielk1977 | 61dd583 | 2008-04-18 11:31:12 +0000 | [diff] [blame] | 5133 | ** If the P1 index entry is greater than or equal to the key value |
| 5134 | ** then jump to P2. Otherwise fall through to the next instruction. |
drh | 4a1d365 | 2014-02-14 15:13:36 +0000 | [diff] [blame] | 5135 | */ |
| 5136 | /* Opcode: IdxGT P1 P2 P3 P4 P5 |
| 5137 | ** Synopsis: key=r[P3@P4] |
drh | 772ae62 | 2004-05-19 13:13:08 +0000 | [diff] [blame] | 5138 | ** |
drh | 4a1d365 | 2014-02-14 15:13:36 +0000 | [diff] [blame] | 5139 | ** The P4 register values beginning with P3 form an unpacked index |
| 5140 | ** key that omits the PRIMARY KEY. Compare this key value against the index |
| 5141 | ** that P1 is currently pointing to, ignoring the PRIMARY KEY or ROWID |
| 5142 | ** fields at the end. |
| 5143 | ** |
| 5144 | ** If the P1 index entry is greater than the key value |
| 5145 | ** then jump to P2. Otherwise fall through to the next instruction. |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 5146 | */ |
drh | 3bb9b93 | 2010-08-06 02:10:00 +0000 | [diff] [blame] | 5147 | /* Opcode: IdxLT P1 P2 P3 P4 P5 |
drh | f63552b | 2013-10-30 00:25:03 +0000 | [diff] [blame] | 5148 | ** Synopsis: key=r[P3@P4] |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 5149 | ** |
danielk1977 | 61dd583 | 2008-04-18 11:31:12 +0000 | [diff] [blame] | 5150 | ** The P4 register values beginning with P3 form an unpacked index |
drh | 4a1d365 | 2014-02-14 15:13:36 +0000 | [diff] [blame] | 5151 | ** key that omits the PRIMARY KEY or ROWID. Compare this key value against |
| 5152 | ** the index that P1 is currently pointing to, ignoring the PRIMARY KEY or |
| 5153 | ** ROWID on the P1 index. |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 5154 | ** |
danielk1977 | 61dd583 | 2008-04-18 11:31:12 +0000 | [diff] [blame] | 5155 | ** If the P1 index entry is less than the key value then jump to P2. |
| 5156 | ** Otherwise fall through to the next instruction. |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 5157 | */ |
drh | 4a1d365 | 2014-02-14 15:13:36 +0000 | [diff] [blame] | 5158 | /* Opcode: IdxLE P1 P2 P3 P4 P5 |
| 5159 | ** Synopsis: key=r[P3@P4] |
| 5160 | ** |
| 5161 | ** The P4 register values beginning with P3 form an unpacked index |
| 5162 | ** key that omits the PRIMARY KEY or ROWID. Compare this key value against |
| 5163 | ** the index that P1 is currently pointing to, ignoring the PRIMARY KEY or |
| 5164 | ** ROWID on the P1 index. |
| 5165 | ** |
| 5166 | ** If the P1 index entry is less than or equal to the key value then jump |
| 5167 | ** to P2. Otherwise fall through to the next instruction. |
| 5168 | */ |
| 5169 | case OP_IdxLE: /* jump */ |
| 5170 | case OP_IdxGT: /* jump */ |
drh | 93952eb | 2009-11-13 19:43:43 +0000 | [diff] [blame] | 5171 | case OP_IdxLT: /* jump */ |
drh | 4a1d365 | 2014-02-14 15:13:36 +0000 | [diff] [blame] | 5172 | case OP_IdxGE: { /* jump */ |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 5173 | VdbeCursor *pC; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 5174 | int res; |
| 5175 | UnpackedRecord r; |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 5176 | |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 5177 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 5178 | pC = p->apCsr[pOp->p1]; |
| 5179 | assert( pC!=0 ); |
drh | d4187c7 | 2010-08-30 22:15:45 +0000 | [diff] [blame] | 5180 | assert( pC->isOrdered ); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 5181 | assert( pC->eCurType==CURTYPE_BTREE ); |
| 5182 | assert( pC->uc.pCursor!=0); |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 5183 | assert( pC->deferredMoveto==0 ); |
| 5184 | assert( pOp->p5==0 || pOp->p5==1 ); |
| 5185 | assert( pOp->p4type==P4_INT32 ); |
| 5186 | r.pKeyInfo = pC->pKeyInfo; |
| 5187 | r.nField = (u16)pOp->p4.i; |
drh | 4a1d365 | 2014-02-14 15:13:36 +0000 | [diff] [blame] | 5188 | if( pOp->opcode<OP_IdxLT ){ |
| 5189 | assert( pOp->opcode==OP_IdxLE || pOp->opcode==OP_IdxGT ); |
dan | 1fed5da | 2014-02-25 21:01:25 +0000 | [diff] [blame] | 5190 | r.default_rc = -1; |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 5191 | }else{ |
drh | 4a1d365 | 2014-02-14 15:13:36 +0000 | [diff] [blame] | 5192 | assert( pOp->opcode==OP_IdxGE || pOp->opcode==OP_IdxLT ); |
dan | 1fed5da | 2014-02-25 21:01:25 +0000 | [diff] [blame] | 5193 | r.default_rc = 0; |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 5194 | } |
| 5195 | r.aMem = &aMem[pOp->p3]; |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 5196 | #ifdef SQLITE_DEBUG |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 5197 | { 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] | 5198 | #endif |
drh | 2dc0648 | 2013-12-11 00:59:10 +0000 | [diff] [blame] | 5199 | res = 0; /* Not needed. Only used to silence a warning. */ |
drh | d3b7420 | 2014-09-17 16:41:15 +0000 | [diff] [blame] | 5200 | rc = sqlite3VdbeIdxKeyCompare(db, pC, &r, &res); |
drh | 4a1d365 | 2014-02-14 15:13:36 +0000 | [diff] [blame] | 5201 | assert( (OP_IdxLE&1)==(OP_IdxLT&1) && (OP_IdxGE&1)==(OP_IdxGT&1) ); |
| 5202 | if( (pOp->opcode&1)==(OP_IdxLT&1) ){ |
| 5203 | assert( pOp->opcode==OP_IdxLE || pOp->opcode==OP_IdxLT ); |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 5204 | res = -res; |
| 5205 | }else{ |
drh | 4a1d365 | 2014-02-14 15:13:36 +0000 | [diff] [blame] | 5206 | assert( pOp->opcode==OP_IdxGE || pOp->opcode==OP_IdxGT ); |
drh | 3da046d | 2013-11-11 03:24:11 +0000 | [diff] [blame] | 5207 | res++; |
| 5208 | } |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 5209 | VdbeBranchTaken(res>0,2); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 5210 | if( rc ) goto abort_due_to_error; |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 5211 | if( res>0 ) goto jump_to_p2; |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 5212 | break; |
| 5213 | } |
| 5214 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 5215 | /* Opcode: Destroy P1 P2 P3 * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5216 | ** |
| 5217 | ** Delete an entire database table or index whose root page in the database |
| 5218 | ** file is given by P1. |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 5219 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 5220 | ** The table being destroyed is in the main database file if P3==0. If |
| 5221 | ** 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] | 5222 | ** that is used to store tables create using CREATE TEMPORARY TABLE. |
| 5223 | ** |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 5224 | ** If AUTOVACUUM is enabled then it is possible that another root page |
| 5225 | ** might be moved into the newly deleted root page in order to keep all |
| 5226 | ** root pages contiguous at the beginning of the database. The former |
| 5227 | ** value of the root page that moved - its value before the move occurred - |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 5228 | ** is stored in register P2. If no page |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 5229 | ** movement was required (because the table being dropped was already |
| 5230 | ** the last one in the database) then a zero is stored in register P2. |
| 5231 | ** If AUTOVACUUM is disabled then a zero is stored in register P2. |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 5232 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 5233 | ** See also: Clear |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5234 | */ |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 5235 | case OP_Destroy: { /* out2 */ |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 5236 | int iMoved; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 5237 | int iDb; |
drh | 3a94987 | 2012-09-18 13:20:13 +0000 | [diff] [blame] | 5238 | |
drh | 9e92a47 | 2013-06-27 17:40:30 +0000 | [diff] [blame] | 5239 | assert( p->readOnly==0 ); |
drh | 055f298 | 2016-01-15 15:06:41 +0000 | [diff] [blame] | 5240 | assert( pOp->p1>1 ); |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 5241 | pOut = out2Prerelease(p, pOp); |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 5242 | pOut->flags = MEM_Null; |
drh | 086723a | 2015-03-24 12:51:52 +0000 | [diff] [blame] | 5243 | if( db->nVdbeRead > db->nVDestroy+1 ){ |
danielk1977 | e6efa74 | 2004-11-10 11:55:10 +0000 | [diff] [blame] | 5244 | rc = SQLITE_LOCKED; |
drh | 77658e2 | 2007-12-04 16:54:52 +0000 | [diff] [blame] | 5245 | p->errorAction = OE_Abort; |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 5246 | goto abort_due_to_error; |
danielk1977 | e6efa74 | 2004-11-10 11:55:10 +0000 | [diff] [blame] | 5247 | }else{ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 5248 | iDb = pOp->p3; |
drh | a7ab6d8 | 2014-07-21 15:44:39 +0000 | [diff] [blame] | 5249 | assert( DbMaskTest(p->btreeMask, iDb) ); |
drh | 2dc0648 | 2013-12-11 00:59:10 +0000 | [diff] [blame] | 5250 | iMoved = 0; /* Not needed. Only to silence a warning. */ |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 5251 | rc = sqlite3BtreeDropTable(db->aDb[iDb].pBt, pOp->p1, &iMoved); |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 5252 | pOut->flags = MEM_Int; |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 5253 | pOut->u.i = iMoved; |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 5254 | if( rc ) goto abort_due_to_error; |
drh | 3765df4 | 2006-06-28 18:18:09 +0000 | [diff] [blame] | 5255 | #ifndef SQLITE_OMIT_AUTOVACUUM |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 5256 | if( iMoved!=0 ){ |
drh | cdf011d | 2011-04-04 21:25:28 +0000 | [diff] [blame] | 5257 | sqlite3RootPageMoved(db, iDb, iMoved, pOp->p1); |
| 5258 | /* All OP_Destroy operations occur on the same btree */ |
| 5259 | assert( resetSchemaOnFault==0 || resetSchemaOnFault==iDb+1 ); |
| 5260 | resetSchemaOnFault = iDb+1; |
danielk1977 | e6efa74 | 2004-11-10 11:55:10 +0000 | [diff] [blame] | 5261 | } |
drh | 3765df4 | 2006-06-28 18:18:09 +0000 | [diff] [blame] | 5262 | #endif |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 5263 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5264 | break; |
| 5265 | } |
| 5266 | |
danielk1977 | c7af484 | 2008-10-27 13:59:33 +0000 | [diff] [blame] | 5267 | /* Opcode: Clear P1 P2 P3 |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 5268 | ** |
| 5269 | ** Delete all contents of the database table or index whose root page |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 5270 | ** in the database file is given by P1. But, unlike Destroy, do not |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 5271 | ** remove the table or index from the database file. |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 5272 | ** |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 5273 | ** The table being clear is in the main database file if P2==0. If |
| 5274 | ** P2==1 then the table to be clear is in the auxiliary database file |
| 5275 | ** that is used to store tables create using CREATE TEMPORARY TABLE. |
| 5276 | ** |
shane | be21779 | 2009-03-05 04:20:31 +0000 | [diff] [blame] | 5277 | ** 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] | 5278 | ** intkey table (an SQL table, not an index). In this case the row change |
| 5279 | ** count is incremented by the number of rows in the table being cleared. |
| 5280 | ** If P3 is greater than zero, then the value stored in register P3 is |
| 5281 | ** also incremented by the number of rows in the table being cleared. |
| 5282 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 5283 | ** See also: Destroy |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 5284 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 5285 | case OP_Clear: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 5286 | int nChange; |
| 5287 | |
| 5288 | nChange = 0; |
drh | 9e92a47 | 2013-06-27 17:40:30 +0000 | [diff] [blame] | 5289 | assert( p->readOnly==0 ); |
drh | a7ab6d8 | 2014-07-21 15:44:39 +0000 | [diff] [blame] | 5290 | assert( DbMaskTest(p->btreeMask, pOp->p2) ); |
danielk1977 | c7af484 | 2008-10-27 13:59:33 +0000 | [diff] [blame] | 5291 | rc = sqlite3BtreeClearTable( |
| 5292 | db->aDb[pOp->p2].pBt, pOp->p1, (pOp->p3 ? &nChange : 0) |
| 5293 | ); |
| 5294 | if( pOp->p3 ){ |
| 5295 | p->nChange += nChange; |
| 5296 | if( pOp->p3>0 ){ |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 5297 | assert( memIsValid(&aMem[pOp->p3]) ); |
| 5298 | memAboutToChange(p, &aMem[pOp->p3]); |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 5299 | aMem[pOp->p3].u.i += nChange; |
danielk1977 | c7af484 | 2008-10-27 13:59:33 +0000 | [diff] [blame] | 5300 | } |
| 5301 | } |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 5302 | if( rc ) goto abort_due_to_error; |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 5303 | break; |
| 5304 | } |
| 5305 | |
drh | 65ea12c | 2014-03-19 17:41:36 +0000 | [diff] [blame] | 5306 | /* Opcode: ResetSorter P1 * * * * |
drh | 079a307 | 2014-03-19 14:10:55 +0000 | [diff] [blame] | 5307 | ** |
drh | 65ea12c | 2014-03-19 17:41:36 +0000 | [diff] [blame] | 5308 | ** Delete all contents from the ephemeral table or sorter |
| 5309 | ** that is open on cursor P1. |
drh | 079a307 | 2014-03-19 14:10:55 +0000 | [diff] [blame] | 5310 | ** |
drh | 65ea12c | 2014-03-19 17:41:36 +0000 | [diff] [blame] | 5311 | ** This opcode only works for cursors used for sorting and |
| 5312 | ** opened with OP_OpenEphemeral or OP_SorterOpen. |
drh | 079a307 | 2014-03-19 14:10:55 +0000 | [diff] [blame] | 5313 | */ |
drh | 65ea12c | 2014-03-19 17:41:36 +0000 | [diff] [blame] | 5314 | case OP_ResetSorter: { |
drh | 079a307 | 2014-03-19 14:10:55 +0000 | [diff] [blame] | 5315 | VdbeCursor *pC; |
| 5316 | |
| 5317 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 5318 | pC = p->apCsr[pOp->p1]; |
| 5319 | assert( pC!=0 ); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 5320 | if( isSorter(pC) ){ |
| 5321 | sqlite3VdbeSorterReset(db, pC->uc.pSorter); |
drh | 65ea12c | 2014-03-19 17:41:36 +0000 | [diff] [blame] | 5322 | }else{ |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 5323 | assert( pC->eCurType==CURTYPE_BTREE ); |
drh | 65ea12c | 2014-03-19 17:41:36 +0000 | [diff] [blame] | 5324 | assert( pC->isEphemeral ); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 5325 | rc = sqlite3BtreeClearTableOfCursor(pC->uc.pCursor); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 5326 | if( rc ) goto abort_due_to_error; |
drh | 65ea12c | 2014-03-19 17:41:36 +0000 | [diff] [blame] | 5327 | } |
drh | 079a307 | 2014-03-19 14:10:55 +0000 | [diff] [blame] | 5328 | break; |
| 5329 | } |
| 5330 | |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 5331 | /* Opcode: CreateTable P1 P2 * * * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 5332 | ** Synopsis: r[P2]=root iDb=P1 |
drh | 5b2fd56 | 2001-09-13 15:21:31 +0000 | [diff] [blame] | 5333 | ** |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 5334 | ** Allocate a new table in the main database file if P1==0 or in the |
| 5335 | ** auxiliary database file if P1==1 or in an attached database if |
| 5336 | ** P1>1. Write the root page number of the new table into |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 5337 | ** register P2 |
drh | 5b2fd56 | 2001-09-13 15:21:31 +0000 | [diff] [blame] | 5338 | ** |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 5339 | ** The difference between a table and an index is this: A table must |
| 5340 | ** have a 4-byte integer key and can have arbitrary data. An index |
| 5341 | ** has an arbitrary key but no data. |
| 5342 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 5343 | ** See also: CreateIndex |
drh | 5b2fd56 | 2001-09-13 15:21:31 +0000 | [diff] [blame] | 5344 | */ |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 5345 | /* Opcode: CreateIndex P1 P2 * * * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 5346 | ** Synopsis: r[P2]=root iDb=P1 |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 5347 | ** |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 5348 | ** Allocate a new index in the main database file if P1==0 or in the |
| 5349 | ** auxiliary database file if P1==1 or in an attached database if |
| 5350 | ** P1>1. Write the root page number of the new table into |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 5351 | ** register P2. |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 5352 | ** |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 5353 | ** See documentation on OP_CreateTable for additional information. |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 5354 | */ |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 5355 | case OP_CreateIndex: /* out2 */ |
| 5356 | case OP_CreateTable: { /* out2 */ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 5357 | int pgno; |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 5358 | int flags; |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 5359 | Db *pDb; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 5360 | |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 5361 | pOut = out2Prerelease(p, pOp); |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 5362 | pgno = 0; |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 5363 | assert( pOp->p1>=0 && pOp->p1<db->nDb ); |
drh | a7ab6d8 | 2014-07-21 15:44:39 +0000 | [diff] [blame] | 5364 | assert( DbMaskTest(p->btreeMask, pOp->p1) ); |
drh | 9e92a47 | 2013-06-27 17:40:30 +0000 | [diff] [blame] | 5365 | assert( p->readOnly==0 ); |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 5366 | pDb = &db->aDb[pOp->p1]; |
| 5367 | assert( pDb->pBt!=0 ); |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 5368 | if( pOp->opcode==OP_CreateTable ){ |
danielk1977 | 9407625 | 2004-05-14 12:16:11 +0000 | [diff] [blame] | 5369 | /* flags = BTREE_INTKEY; */ |
drh | d4187c7 | 2010-08-30 22:15:45 +0000 | [diff] [blame] | 5370 | flags = BTREE_INTKEY; |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 5371 | }else{ |
drh | d4187c7 | 2010-08-30 22:15:45 +0000 | [diff] [blame] | 5372 | flags = BTREE_BLOBKEY; |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 5373 | } |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 5374 | rc = sqlite3BtreeCreateTable(pDb->pBt, &pgno, flags); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 5375 | if( rc ) goto abort_due_to_error; |
drh | 88a003e | 2008-12-11 16:17:03 +0000 | [diff] [blame] | 5376 | pOut->u.i = pgno; |
drh | 5b2fd56 | 2001-09-13 15:21:31 +0000 | [diff] [blame] | 5377 | break; |
| 5378 | } |
| 5379 | |
drh | 2264584 | 2011-03-24 01:34:03 +0000 | [diff] [blame] | 5380 | /* Opcode: ParseSchema P1 * * P4 * |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 5381 | ** |
| 5382 | ** Read and parse all entries from the SQLITE_MASTER table of database P1 |
drh | 2264584 | 2011-03-24 01:34:03 +0000 | [diff] [blame] | 5383 | ** that match the WHERE clause P4. |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 5384 | ** |
| 5385 | ** This opcode invokes the parser to create a new virtual machine, |
shane | 21e7feb | 2008-05-30 15:59:49 +0000 | [diff] [blame] | 5386 | ** then runs the new virtual machine. It is thus a re-entrant opcode. |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 5387 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 5388 | case OP_ParseSchema: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 5389 | int iDb; |
| 5390 | const char *zMaster; |
| 5391 | char *zSql; |
| 5392 | InitData initData; |
| 5393 | |
drh | bdaec52 | 2011-04-04 00:14:43 +0000 | [diff] [blame] | 5394 | /* Any prepared statement that invokes this opcode will hold mutexes |
| 5395 | ** on every btree. This is a prerequisite for invoking |
| 5396 | ** sqlite3InitCallback(). |
| 5397 | */ |
| 5398 | #ifdef SQLITE_DEBUG |
| 5399 | for(iDb=0; iDb<db->nDb; iDb++){ |
| 5400 | assert( iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) ); |
| 5401 | } |
| 5402 | #endif |
drh | bdaec52 | 2011-04-04 00:14:43 +0000 | [diff] [blame] | 5403 | |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 5404 | iDb = pOp->p1; |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 5405 | assert( iDb>=0 && iDb<db->nDb ); |
dan | 6c15487 | 2011-04-02 09:44:43 +0000 | [diff] [blame] | 5406 | assert( DbHasProperty(db, iDb, DB_SchemaLoaded) ); |
drh | bdaec52 | 2011-04-04 00:14:43 +0000 | [diff] [blame] | 5407 | /* Used to be a conditional */ { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 5408 | zMaster = SCHEMA_TABLE(iDb); |
danielk1977 | a8bbef8 | 2009-03-23 17:11:26 +0000 | [diff] [blame] | 5409 | initData.db = db; |
| 5410 | initData.iDb = pOp->p1; |
| 5411 | initData.pzErrMsg = &p->zErrMsg; |
| 5412 | zSql = sqlite3MPrintf(db, |
drh | 6a9c64b | 2010-01-12 23:54:14 +0000 | [diff] [blame] | 5413 | "SELECT name, rootpage, sql FROM '%q'.%s WHERE %s ORDER BY rowid", |
danielk1977 | a8bbef8 | 2009-03-23 17:11:26 +0000 | [diff] [blame] | 5414 | db->aDb[iDb].zName, zMaster, pOp->p4.z); |
| 5415 | if( zSql==0 ){ |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 5416 | rc = SQLITE_NOMEM_BKPT; |
danielk1977 | a8bbef8 | 2009-03-23 17:11:26 +0000 | [diff] [blame] | 5417 | }else{ |
danielk1977 | a8bbef8 | 2009-03-23 17:11:26 +0000 | [diff] [blame] | 5418 | assert( db->init.busy==0 ); |
| 5419 | db->init.busy = 1; |
| 5420 | initData.rc = SQLITE_OK; |
| 5421 | assert( !db->mallocFailed ); |
| 5422 | rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0); |
| 5423 | if( rc==SQLITE_OK ) rc = initData.rc; |
| 5424 | sqlite3DbFree(db, zSql); |
| 5425 | db->init.busy = 0; |
danielk1977 | a8bbef8 | 2009-03-23 17:11:26 +0000 | [diff] [blame] | 5426 | } |
drh | 3c23a88 | 2007-01-09 14:01:13 +0000 | [diff] [blame] | 5427 | } |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 5428 | if( rc ){ |
| 5429 | sqlite3ResetAllSchemasOfConnection(db); |
| 5430 | if( rc==SQLITE_NOMEM ){ |
| 5431 | goto no_mem; |
| 5432 | } |
| 5433 | goto abort_due_to_error; |
danielk1977 | 261919c | 2005-12-06 12:52:59 +0000 | [diff] [blame] | 5434 | } |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 5435 | break; |
| 5436 | } |
| 5437 | |
drh | 8bfdf72 | 2009-06-19 14:06:03 +0000 | [diff] [blame] | 5438 | #if !defined(SQLITE_OMIT_ANALYZE) |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 5439 | /* Opcode: LoadAnalysis P1 * * * * |
drh | 497e446 | 2005-07-23 03:18:40 +0000 | [diff] [blame] | 5440 | ** |
| 5441 | ** Read the sqlite_stat1 table for database P1 and load the content |
| 5442 | ** of that table into the internal index hash table. This will cause |
| 5443 | ** the analysis to be used when preparing all subsequent queries. |
| 5444 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 5445 | case OP_LoadAnalysis: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 5446 | assert( pOp->p1>=0 && pOp->p1<db->nDb ); |
| 5447 | rc = sqlite3AnalysisLoad(db, pOp->p1); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 5448 | if( rc ) goto abort_due_to_error; |
drh | 497e446 | 2005-07-23 03:18:40 +0000 | [diff] [blame] | 5449 | break; |
| 5450 | } |
drh | 8bfdf72 | 2009-06-19 14:06:03 +0000 | [diff] [blame] | 5451 | #endif /* !defined(SQLITE_OMIT_ANALYZE) */ |
drh | 497e446 | 2005-07-23 03:18:40 +0000 | [diff] [blame] | 5452 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 5453 | /* Opcode: DropTable P1 * * P4 * |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 5454 | ** |
| 5455 | ** Remove the internal (in-memory) data structures that describe |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 5456 | ** the table named P4 in database P1. This is called after a table |
drh | 5dad9a3 | 2014-07-25 18:37:42 +0000 | [diff] [blame] | 5457 | ** is dropped from disk (using the Destroy opcode) in order to keep |
| 5458 | ** the internal representation of the |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 5459 | ** schema consistent with what is on disk. |
| 5460 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 5461 | case OP_DropTable: { |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 5462 | sqlite3UnlinkAndDeleteTable(db, pOp->p1, pOp->p4.z); |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 5463 | break; |
| 5464 | } |
| 5465 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 5466 | /* Opcode: DropIndex P1 * * P4 * |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 5467 | ** |
| 5468 | ** Remove the internal (in-memory) data structures that describe |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 5469 | ** the index named P4 in database P1. This is called after an index |
drh | 5dad9a3 | 2014-07-25 18:37:42 +0000 | [diff] [blame] | 5470 | ** is dropped from disk (using the Destroy opcode) |
| 5471 | ** in order to keep the internal representation of the |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 5472 | ** schema consistent with what is on disk. |
| 5473 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 5474 | case OP_DropIndex: { |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 5475 | sqlite3UnlinkAndDeleteIndex(db, pOp->p1, pOp->p4.z); |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 5476 | break; |
| 5477 | } |
| 5478 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 5479 | /* Opcode: DropTrigger P1 * * P4 * |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 5480 | ** |
| 5481 | ** Remove the internal (in-memory) data structures that describe |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 5482 | ** the trigger named P4 in database P1. This is called after a trigger |
drh | 5dad9a3 | 2014-07-25 18:37:42 +0000 | [diff] [blame] | 5483 | ** is dropped from disk (using the Destroy opcode) in order to keep |
| 5484 | ** the internal representation of the |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 5485 | ** schema consistent with what is on disk. |
| 5486 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 5487 | case OP_DropTrigger: { |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 5488 | sqlite3UnlinkAndDeleteTrigger(db, pOp->p1, pOp->p4.z); |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 5489 | break; |
| 5490 | } |
| 5491 | |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 5492 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 5493 | #ifndef SQLITE_OMIT_INTEGRITY_CHECK |
drh | 98968b2 | 2016-03-15 22:00:39 +0000 | [diff] [blame] | 5494 | /* Opcode: IntegrityCk P1 P2 P3 P4 P5 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5495 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 5496 | ** Do an analysis of the currently open database. Store in |
| 5497 | ** register P1 the text of an error message describing any problems. |
| 5498 | ** If no problems are found, store a NULL in register P1. |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 5499 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 5500 | ** The register P3 contains the maximum number of allowed errors. |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 5501 | ** At most reg(P3) errors will be reported. |
| 5502 | ** In other words, the analysis stops as soon as reg(P1) errors are |
| 5503 | ** seen. Reg(P1) is updated with the number of errors remaining. |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 5504 | ** |
drh | 98968b2 | 2016-03-15 22:00:39 +0000 | [diff] [blame] | 5505 | ** The root page numbers of all tables in the database are integers |
| 5506 | ** stored in P4_INTARRAY argument. |
drh | 2150432 | 2002-06-25 13:16:02 +0000 | [diff] [blame] | 5507 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 5508 | ** If P5 is not zero, the check is done on the auxiliary database |
drh | 2150432 | 2002-06-25 13:16:02 +0000 | [diff] [blame] | 5509 | ** file, not the main database file. |
drh | 1dd397f | 2002-02-03 03:34:07 +0000 | [diff] [blame] | 5510 | ** |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 5511 | ** This opcode is used to implement the integrity_check pragma. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5512 | */ |
drh | aaab572 | 2002-02-19 13:39:21 +0000 | [diff] [blame] | 5513 | case OP_IntegrityCk: { |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 5514 | int nRoot; /* Number of tables to check. (Number of root pages.) */ |
| 5515 | int *aRoot; /* Array of rootpage numbers for tables to be checked */ |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 5516 | int nErr; /* Number of errors reported */ |
| 5517 | char *z; /* Text of the error report */ |
| 5518 | Mem *pnErr; /* Register keeping track of errors remaining */ |
drh | 9e92a47 | 2013-06-27 17:40:30 +0000 | [diff] [blame] | 5519 | |
drh | 1713afb | 2013-06-28 01:24:57 +0000 | [diff] [blame] | 5520 | assert( p->bIsReader ); |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 5521 | nRoot = pOp->p2; |
drh | 98968b2 | 2016-03-15 22:00:39 +0000 | [diff] [blame] | 5522 | aRoot = pOp->p4.ai; |
drh | 7906975 | 2004-05-22 21:30:40 +0000 | [diff] [blame] | 5523 | assert( nRoot>0 ); |
drh | 98968b2 | 2016-03-15 22:00:39 +0000 | [diff] [blame] | 5524 | assert( aRoot[nRoot]==0 ); |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 5525 | assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) ); |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 5526 | pnErr = &aMem[pOp->p3]; |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 5527 | assert( (pnErr->flags & MEM_Int)!=0 ); |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 5528 | assert( (pnErr->flags & (MEM_Str|MEM_Blob))==0 ); |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 5529 | pIn1 = &aMem[pOp->p1]; |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 5530 | assert( pOp->p5<db->nDb ); |
drh | a7ab6d8 | 2014-07-21 15:44:39 +0000 | [diff] [blame] | 5531 | assert( DbMaskTest(p->btreeMask, pOp->p5) ); |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 5532 | z = sqlite3BtreeIntegrityCheck(db->aDb[pOp->p5].pBt, aRoot, nRoot, |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 5533 | (int)pnErr->u.i, &nErr); |
drh | 3c024d6 | 2007-03-30 11:23:45 +0000 | [diff] [blame] | 5534 | pnErr->u.i -= nErr; |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 5535 | sqlite3VdbeMemSetNull(pIn1); |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 5536 | if( nErr==0 ){ |
| 5537 | assert( z==0 ); |
drh | c890fec | 2008-08-01 20:10:08 +0000 | [diff] [blame] | 5538 | }else if( z==0 ){ |
| 5539 | goto no_mem; |
drh | 1dd397f | 2002-02-03 03:34:07 +0000 | [diff] [blame] | 5540 | }else{ |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 5541 | sqlite3VdbeMemSetStr(pIn1, z, -1, SQLITE_UTF8, sqlite3_free); |
danielk1977 | 8a6b541 | 2004-05-24 07:04:25 +0000 | [diff] [blame] | 5542 | } |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 5543 | UPDATE_MAX_BLOBSIZE(pIn1); |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 5544 | sqlite3VdbeChangeEncoding(pIn1, encoding); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5545 | break; |
| 5546 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 5547 | #endif /* SQLITE_OMIT_INTEGRITY_CHECK */ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5548 | |
drh | 3d4501e | 2008-12-04 20:40:10 +0000 | [diff] [blame] | 5549 | /* Opcode: RowSetAdd P1 P2 * * * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 5550 | ** Synopsis: rowset(P1)=r[P2] |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5551 | ** |
drh | 3d4501e | 2008-12-04 20:40:10 +0000 | [diff] [blame] | 5552 | ** Insert the integer value held by register P2 into a boolean index |
| 5553 | ** held in register P1. |
| 5554 | ** |
| 5555 | ** An assertion fails if P2 is not an integer. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5556 | */ |
drh | 93952eb | 2009-11-13 19:43:43 +0000 | [diff] [blame] | 5557 | case OP_RowSetAdd: { /* in1, in2 */ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 5558 | pIn1 = &aMem[pOp->p1]; |
| 5559 | pIn2 = &aMem[pOp->p2]; |
drh | 93952eb | 2009-11-13 19:43:43 +0000 | [diff] [blame] | 5560 | assert( (pIn2->flags & MEM_Int)!=0 ); |
| 5561 | if( (pIn1->flags & MEM_RowSet)==0 ){ |
| 5562 | sqlite3VdbeMemSetRowSet(pIn1); |
| 5563 | if( (pIn1->flags & MEM_RowSet)==0 ) goto no_mem; |
drh | 3d4501e | 2008-12-04 20:40:10 +0000 | [diff] [blame] | 5564 | } |
drh | 93952eb | 2009-11-13 19:43:43 +0000 | [diff] [blame] | 5565 | sqlite3RowSetInsert(pIn1->u.pRowSet, pIn2->u.i); |
drh | 3d4501e | 2008-12-04 20:40:10 +0000 | [diff] [blame] | 5566 | break; |
| 5567 | } |
| 5568 | |
| 5569 | /* Opcode: RowSetRead P1 P2 P3 * * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 5570 | ** Synopsis: r[P3]=rowset(P1) |
drh | 3d4501e | 2008-12-04 20:40:10 +0000 | [diff] [blame] | 5571 | ** |
| 5572 | ** Extract the smallest value from boolean index P1 and put that value into |
| 5573 | ** register P3. Or, if boolean index P1 is initially empty, leave P3 |
| 5574 | ** unchanged and jump to instruction P2. |
| 5575 | */ |
drh | 93952eb | 2009-11-13 19:43:43 +0000 | [diff] [blame] | 5576 | case OP_RowSetRead: { /* jump, in1, out3 */ |
drh | 3d4501e | 2008-12-04 20:40:10 +0000 | [diff] [blame] | 5577 | i64 val; |
drh | 49afe3a | 2013-07-10 03:05:14 +0000 | [diff] [blame] | 5578 | |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 5579 | pIn1 = &aMem[pOp->p1]; |
drh | 93952eb | 2009-11-13 19:43:43 +0000 | [diff] [blame] | 5580 | if( (pIn1->flags & MEM_RowSet)==0 |
| 5581 | || sqlite3RowSetNext(pIn1->u.pRowSet, &val)==0 |
drh | 3d4501e | 2008-12-04 20:40:10 +0000 | [diff] [blame] | 5582 | ){ |
| 5583 | /* The boolean index is empty */ |
drh | 93952eb | 2009-11-13 19:43:43 +0000 | [diff] [blame] | 5584 | sqlite3VdbeMemSetNull(pIn1); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 5585 | VdbeBranchTaken(1,2); |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 5586 | goto jump_to_p2_and_check_for_interrupt; |
drh | 3d4501e | 2008-12-04 20:40:10 +0000 | [diff] [blame] | 5587 | }else{ |
| 5588 | /* A value was pulled from the index */ |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 5589 | VdbeBranchTaken(0,2); |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 5590 | sqlite3VdbeMemSetInt64(&aMem[pOp->p3], val); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 5591 | } |
drh | 49afe3a | 2013-07-10 03:05:14 +0000 | [diff] [blame] | 5592 | goto check_for_interrupt; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5593 | } |
| 5594 | |
drh | 1b26c7c | 2009-04-22 02:15:47 +0000 | [diff] [blame] | 5595 | /* Opcode: RowSetTest P1 P2 P3 P4 |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 5596 | ** Synopsis: if r[P3] in rowset(P1) goto P2 |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 5597 | ** |
drh | ade9760 | 2009-04-21 15:05:18 +0000 | [diff] [blame] | 5598 | ** 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] | 5599 | ** contains a RowSet object and that RowSet object contains |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 5600 | ** the value held in P3, jump to register P2. Otherwise, insert the |
drh | 1b26c7c | 2009-04-22 02:15:47 +0000 | [diff] [blame] | 5601 | ** integer in P3 into the RowSet and continue on to the |
drh | ade9760 | 2009-04-21 15:05:18 +0000 | [diff] [blame] | 5602 | ** next opcode. |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 5603 | ** |
drh | 1b26c7c | 2009-04-22 02:15:47 +0000 | [diff] [blame] | 5604 | ** The RowSet object is optimized for the case where successive sets |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 5605 | ** of integers, where each set contains no duplicates. Each set |
| 5606 | ** of values is identified by a unique P4 value. The first set |
drh | 1b26c7c | 2009-04-22 02:15:47 +0000 | [diff] [blame] | 5607 | ** must have P4==0, the final set P4=-1. P4 must be either -1 or |
| 5608 | ** non-negative. For non-negative values of P4 only the lower 4 |
| 5609 | ** bits are significant. |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 5610 | ** |
| 5611 | ** This allows optimizations: (a) when P4==0 there is no need to test |
drh | 1b26c7c | 2009-04-22 02:15:47 +0000 | [diff] [blame] | 5612 | ** the rowset object for P3, as it is guaranteed not to contain it, |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 5613 | ** (b) when P4==-1 there is no need to insert the value, as it will |
| 5614 | ** never be tested for, and (c) when a value that is part of set X is |
| 5615 | ** inserted, there is no need to search to see if the same value was |
| 5616 | ** previously inserted as part of set X (only if it was previously |
| 5617 | ** inserted as part of some other set). |
| 5618 | */ |
drh | 1b26c7c | 2009-04-22 02:15:47 +0000 | [diff] [blame] | 5619 | case OP_RowSetTest: { /* jump, in1, in3 */ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 5620 | int iSet; |
| 5621 | int exists; |
| 5622 | |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 5623 | pIn1 = &aMem[pOp->p1]; |
| 5624 | pIn3 = &aMem[pOp->p3]; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 5625 | iSet = pOp->p4.i; |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 5626 | assert( pIn3->flags&MEM_Int ); |
| 5627 | |
drh | 1b26c7c | 2009-04-22 02:15:47 +0000 | [diff] [blame] | 5628 | /* If there is anything other than a rowset object in memory cell P1, |
| 5629 | ** delete it now and initialize P1 with an empty rowset |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 5630 | */ |
drh | 733bf1b | 2009-04-22 00:47:00 +0000 | [diff] [blame] | 5631 | if( (pIn1->flags & MEM_RowSet)==0 ){ |
| 5632 | sqlite3VdbeMemSetRowSet(pIn1); |
| 5633 | if( (pIn1->flags & MEM_RowSet)==0 ) goto no_mem; |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 5634 | } |
| 5635 | |
| 5636 | assert( pOp->p4type==P4_INT32 ); |
drh | 1b26c7c | 2009-04-22 02:15:47 +0000 | [diff] [blame] | 5637 | assert( iSet==-1 || iSet>=0 ); |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 5638 | if( iSet ){ |
drh | d83cad2 | 2014-04-10 02:24:48 +0000 | [diff] [blame] | 5639 | exists = sqlite3RowSetTest(pIn1->u.pRowSet, iSet, pIn3->u.i); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 5640 | VdbeBranchTaken(exists!=0,2); |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 5641 | if( exists ) goto jump_to_p2; |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 5642 | } |
| 5643 | if( iSet>=0 ){ |
drh | 733bf1b | 2009-04-22 00:47:00 +0000 | [diff] [blame] | 5644 | sqlite3RowSetInsert(pIn1->u.pRowSet, pIn3->u.i); |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 5645 | } |
| 5646 | break; |
| 5647 | } |
| 5648 | |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5649 | |
danielk1977 | 93758c8 | 2005-01-21 08:13:14 +0000 | [diff] [blame] | 5650 | #ifndef SQLITE_OMIT_TRIGGER |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 5651 | |
drh | 0fd6135 | 2014-02-07 02:29:45 +0000 | [diff] [blame] | 5652 | /* Opcode: Program P1 P2 P3 P4 P5 |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 5653 | ** |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 5654 | ** Execute the trigger program passed as P4 (type P4_SUBPROGRAM). |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 5655 | ** |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 5656 | ** P1 contains the address of the memory cell that contains the first memory |
| 5657 | ** cell in an array of values used as arguments to the sub-program. P2 |
| 5658 | ** contains the address to jump to if the sub-program throws an IGNORE |
| 5659 | ** exception using the RAISE() function. Register P3 contains the address |
| 5660 | ** of a memory cell in this (the parent) VM that is used to allocate the |
| 5661 | ** memory required by the sub-vdbe at runtime. |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 5662 | ** |
| 5663 | ** P4 is a pointer to the VM containing the trigger program. |
drh | 0fd6135 | 2014-02-07 02:29:45 +0000 | [diff] [blame] | 5664 | ** |
| 5665 | ** If P5 is non-zero, then recursive program invocation is enabled. |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 5666 | */ |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 5667 | case OP_Program: { /* jump */ |
dan | 65a7cd1 | 2009-09-01 12:16:01 +0000 | [diff] [blame] | 5668 | int nMem; /* Number of memory registers for sub-program */ |
| 5669 | int nByte; /* Bytes of runtime space required for sub-program */ |
| 5670 | Mem *pRt; /* Register to allocate runtime space */ |
| 5671 | Mem *pMem; /* Used to iterate through memory cells */ |
| 5672 | Mem *pEnd; /* Last memory cell in new array */ |
| 5673 | VdbeFrame *pFrame; /* New vdbe frame to execute in */ |
| 5674 | SubProgram *pProgram; /* Sub-program to execute */ |
| 5675 | void *t; /* Token identifying trigger */ |
| 5676 | |
| 5677 | pProgram = pOp->p4.pProgram; |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 5678 | pRt = &aMem[pOp->p3]; |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 5679 | assert( pProgram->nOp>0 ); |
| 5680 | |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 5681 | /* If the p5 flag is clear, then recursive invocation of triggers is |
| 5682 | ** disabled for backwards compatibility (p5 is set if this sub-program |
| 5683 | ** is really a trigger, not a foreign key action, and the flag set |
| 5684 | ** and cleared by the "PRAGMA recursive_triggers" command is clear). |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 5685 | ** |
| 5686 | ** It is recursive invocation of triggers, at the SQL level, that is |
| 5687 | ** disabled. In some cases a single trigger may generate more than one |
| 5688 | ** SubProgram (if the trigger may be executed with more than one different |
| 5689 | ** ON CONFLICT algorithm). SubProgram structures associated with a |
| 5690 | ** single trigger all have the same value for the SubProgram.token |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 5691 | ** variable. */ |
| 5692 | if( pOp->p5 ){ |
dan | 65a7cd1 | 2009-09-01 12:16:01 +0000 | [diff] [blame] | 5693 | t = pProgram->token; |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 5694 | for(pFrame=p->pFrame; pFrame && pFrame->token!=t; pFrame=pFrame->pParent); |
| 5695 | if( pFrame ) break; |
| 5696 | } |
| 5697 | |
dan | f589450 | 2009-10-07 18:41:19 +0000 | [diff] [blame] | 5698 | if( p->nFrame>=db->aLimit[SQLITE_LIMIT_TRIGGER_DEPTH] ){ |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 5699 | rc = SQLITE_ERROR; |
drh | 22c17b8 | 2015-05-15 04:13:15 +0000 | [diff] [blame] | 5700 | sqlite3VdbeError(p, "too many levels of trigger recursion"); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 5701 | goto abort_due_to_error; |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 5702 | } |
| 5703 | |
| 5704 | /* Register pRt is used to store the memory required to save the state |
| 5705 | ** of the current program, and the memory required at runtime to execute |
| 5706 | ** the trigger program. If this trigger has been fired before, then pRt |
| 5707 | ** is already allocated. Otherwise, it must be initialized. */ |
| 5708 | if( (pRt->flags&MEM_Frame)==0 ){ |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 5709 | /* SubProgram.nMem is set to the number of memory cells used by the |
| 5710 | ** program stored in SubProgram.aOp. As well as these, one memory |
| 5711 | ** cell is required for each cursor used by the program. Set local |
| 5712 | ** variable nMem (and later, VdbeFrame.nChildMem) to this value. |
| 5713 | */ |
dan | 65a7cd1 | 2009-09-01 12:16:01 +0000 | [diff] [blame] | 5714 | nMem = pProgram->nMem + pProgram->nCsr; |
drh | 3cdce92 | 2016-03-21 00:30:40 +0000 | [diff] [blame] | 5715 | assert( nMem>0 ); |
| 5716 | if( pProgram->nCsr==0 ) nMem++; |
dan | 65a7cd1 | 2009-09-01 12:16:01 +0000 | [diff] [blame] | 5717 | nByte = ROUND8(sizeof(VdbeFrame)) |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 5718 | + nMem * sizeof(Mem) |
dan | 1d8cb21 | 2011-12-09 13:24:16 +0000 | [diff] [blame] | 5719 | + pProgram->nCsr * sizeof(VdbeCursor *) |
| 5720 | + pProgram->nOnce * sizeof(u8); |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 5721 | pFrame = sqlite3DbMallocZero(db, nByte); |
| 5722 | if( !pFrame ){ |
| 5723 | goto no_mem; |
| 5724 | } |
| 5725 | sqlite3VdbeMemRelease(pRt); |
| 5726 | pRt->flags = MEM_Frame; |
| 5727 | pRt->u.pFrame = pFrame; |
| 5728 | |
| 5729 | pFrame->v = p; |
| 5730 | pFrame->nChildMem = nMem; |
| 5731 | pFrame->nChildCsr = pProgram->nCsr; |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 5732 | pFrame->pc = (int)(pOp - aOp); |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 5733 | pFrame->aMem = p->aMem; |
| 5734 | pFrame->nMem = p->nMem; |
| 5735 | pFrame->apCsr = p->apCsr; |
| 5736 | pFrame->nCursor = p->nCursor; |
| 5737 | pFrame->aOp = p->aOp; |
| 5738 | pFrame->nOp = p->nOp; |
| 5739 | pFrame->token = pProgram->token; |
dan | 1d8cb21 | 2011-12-09 13:24:16 +0000 | [diff] [blame] | 5740 | pFrame->aOnceFlag = p->aOnceFlag; |
| 5741 | pFrame->nOnceFlag = p->nOnceFlag; |
dan | e2f771b | 2014-11-03 15:33:17 +0000 | [diff] [blame] | 5742 | #ifdef SQLITE_ENABLE_STMT_SCANSTATUS |
dan | 43764a8 | 2014-11-01 21:00:04 +0000 | [diff] [blame] | 5743 | pFrame->anExec = p->anExec; |
dan | e2f771b | 2014-11-03 15:33:17 +0000 | [diff] [blame] | 5744 | #endif |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 5745 | |
| 5746 | pEnd = &VdbeFrameMem(pFrame)[pFrame->nChildMem]; |
| 5747 | for(pMem=VdbeFrameMem(pFrame); pMem!=pEnd; pMem++){ |
drh | a5750cf | 2014-02-07 13:20:31 +0000 | [diff] [blame] | 5748 | pMem->flags = MEM_Undefined; |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 5749 | pMem->db = db; |
| 5750 | } |
| 5751 | }else{ |
| 5752 | pFrame = pRt->u.pFrame; |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 5753 | assert( pProgram->nMem+pProgram->nCsr==pFrame->nChildMem |
| 5754 | || (pProgram->nCsr==0 && pProgram->nMem+1==pFrame->nChildMem) ); |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 5755 | assert( pProgram->nCsr==pFrame->nChildCsr ); |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 5756 | assert( (int)(pOp - aOp)==pFrame->pc ); |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 5757 | } |
| 5758 | |
| 5759 | p->nFrame++; |
| 5760 | pFrame->pParent = p->pFrame; |
drh | 99a6692 | 2011-05-13 18:51:42 +0000 | [diff] [blame] | 5761 | pFrame->lastRowid = lastRowid; |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 5762 | pFrame->nChange = p->nChange; |
dan | c3da667 | 2014-10-28 18:24:16 +0000 | [diff] [blame] | 5763 | pFrame->nDbChange = p->db->nChange; |
dan | 3200132 | 2016-02-19 18:54:29 +0000 | [diff] [blame] | 5764 | assert( pFrame->pAuxData==0 ); |
| 5765 | pFrame->pAuxData = p->pAuxData; |
| 5766 | p->pAuxData = 0; |
dan | 2832ad4 | 2009-08-31 15:27:27 +0000 | [diff] [blame] | 5767 | p->nChange = 0; |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 5768 | p->pFrame = pFrame; |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 5769 | p->aMem = aMem = VdbeFrameMem(pFrame); |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 5770 | p->nMem = pFrame->nChildMem; |
shane | cea72b2 | 2009-09-07 04:38:36 +0000 | [diff] [blame] | 5771 | p->nCursor = (u16)pFrame->nChildCsr; |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 5772 | p->apCsr = (VdbeCursor **)&aMem[p->nMem]; |
drh | bbe879d | 2009-11-14 18:04:35 +0000 | [diff] [blame] | 5773 | p->aOp = aOp = pProgram->aOp; |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 5774 | p->nOp = pProgram->nOp; |
dan | 1d8cb21 | 2011-12-09 13:24:16 +0000 | [diff] [blame] | 5775 | p->aOnceFlag = (u8 *)&p->apCsr[p->nCursor]; |
| 5776 | p->nOnceFlag = pProgram->nOnce; |
dan | e2f771b | 2014-11-03 15:33:17 +0000 | [diff] [blame] | 5777 | #ifdef SQLITE_ENABLE_STMT_SCANSTATUS |
dan | 43764a8 | 2014-11-01 21:00:04 +0000 | [diff] [blame] | 5778 | p->anExec = 0; |
dan | e2f771b | 2014-11-03 15:33:17 +0000 | [diff] [blame] | 5779 | #endif |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 5780 | pOp = &aOp[-1]; |
dan | 1d8cb21 | 2011-12-09 13:24:16 +0000 | [diff] [blame] | 5781 | memset(p->aOnceFlag, 0, p->nOnceFlag); |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 5782 | |
| 5783 | break; |
| 5784 | } |
| 5785 | |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 5786 | /* Opcode: Param P1 P2 * * * |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 5787 | ** |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 5788 | ** This opcode is only ever present in sub-programs called via the |
| 5789 | ** OP_Program instruction. Copy a value currently stored in a memory |
| 5790 | ** cell of the calling (parent) frame to cell P2 in the current frames |
| 5791 | ** address space. This is used by trigger programs to access the new.* |
| 5792 | ** and old.* values. |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 5793 | ** |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 5794 | ** The address of the cell in the parent frame is determined by adding |
| 5795 | ** the value of the P1 argument to the value of the P1 argument to the |
| 5796 | ** calling OP_Program instruction. |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 5797 | */ |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 5798 | case OP_Param: { /* out2 */ |
dan | 65a7cd1 | 2009-09-01 12:16:01 +0000 | [diff] [blame] | 5799 | VdbeFrame *pFrame; |
| 5800 | Mem *pIn; |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 5801 | pOut = out2Prerelease(p, pOp); |
dan | 65a7cd1 | 2009-09-01 12:16:01 +0000 | [diff] [blame] | 5802 | pFrame = p->pFrame; |
| 5803 | pIn = &pFrame->aMem[pOp->p1 + pFrame->aOp[pFrame->pc].p1]; |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 5804 | sqlite3VdbeMemShallowCopy(pOut, pIn, MEM_Ephem); |
| 5805 | break; |
| 5806 | } |
| 5807 | |
danielk1977 | 93758c8 | 2005-01-21 08:13:14 +0000 | [diff] [blame] | 5808 | #endif /* #ifndef SQLITE_OMIT_TRIGGER */ |
rdc | b0c374f | 2004-02-20 22:53:38 +0000 | [diff] [blame] | 5809 | |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 5810 | #ifndef SQLITE_OMIT_FOREIGN_KEY |
dan | 32b09f2 | 2009-09-23 17:29:59 +0000 | [diff] [blame] | 5811 | /* Opcode: FkCounter P1 P2 * * * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 5812 | ** Synopsis: fkctr[P1]+=P2 |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 5813 | ** |
dan | 0ff297e | 2009-09-25 17:03:14 +0000 | [diff] [blame] | 5814 | ** Increment a "constraint counter" by P2 (P2 may be negative or positive). |
| 5815 | ** If P1 is non-zero, the database constraint counter is incremented |
| 5816 | ** (deferred foreign key constraints). Otherwise, if P1 is zero, the |
dan | 32b09f2 | 2009-09-23 17:29:59 +0000 | [diff] [blame] | 5817 | ** statement counter is incremented (immediate foreign key constraints). |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 5818 | */ |
dan | 32b09f2 | 2009-09-23 17:29:59 +0000 | [diff] [blame] | 5819 | case OP_FkCounter: { |
drh | 963c74d | 2013-07-11 12:19:12 +0000 | [diff] [blame] | 5820 | if( db->flags & SQLITE_DeferFKs ){ |
dan | cb3e4b7 | 2013-07-03 19:53:05 +0000 | [diff] [blame] | 5821 | db->nDeferredImmCons += pOp->p2; |
| 5822 | }else if( pOp->p1 ){ |
dan | 0ff297e | 2009-09-25 17:03:14 +0000 | [diff] [blame] | 5823 | db->nDeferredCons += pOp->p2; |
dan | 32b09f2 | 2009-09-23 17:29:59 +0000 | [diff] [blame] | 5824 | }else{ |
dan | 0ff297e | 2009-09-25 17:03:14 +0000 | [diff] [blame] | 5825 | p->nFkConstraint += pOp->p2; |
| 5826 | } |
| 5827 | break; |
| 5828 | } |
| 5829 | |
| 5830 | /* Opcode: FkIfZero P1 P2 * * * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 5831 | ** Synopsis: if fkctr[P1]==0 goto P2 |
dan | 0ff297e | 2009-09-25 17:03:14 +0000 | [diff] [blame] | 5832 | ** |
| 5833 | ** This opcode tests if a foreign key constraint-counter is currently zero. |
| 5834 | ** If so, jump to instruction P2. Otherwise, fall through to the next |
| 5835 | ** instruction. |
| 5836 | ** |
| 5837 | ** If P1 is non-zero, then the jump is taken if the database constraint-counter |
| 5838 | ** is zero (the one that counts deferred constraint violations). If P1 is |
| 5839 | ** zero, the jump is taken if the statement constraint-counter is zero |
| 5840 | ** (immediate foreign key constraint violations). |
| 5841 | */ |
| 5842 | case OP_FkIfZero: { /* jump */ |
| 5843 | if( pOp->p1 ){ |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 5844 | VdbeBranchTaken(db->nDeferredCons==0 && db->nDeferredImmCons==0, 2); |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 5845 | if( db->nDeferredCons==0 && db->nDeferredImmCons==0 ) goto jump_to_p2; |
dan | 0ff297e | 2009-09-25 17:03:14 +0000 | [diff] [blame] | 5846 | }else{ |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 5847 | VdbeBranchTaken(p->nFkConstraint==0 && db->nDeferredImmCons==0, 2); |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 5848 | if( p->nFkConstraint==0 && db->nDeferredImmCons==0 ) goto jump_to_p2; |
dan | 32b09f2 | 2009-09-23 17:29:59 +0000 | [diff] [blame] | 5849 | } |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 5850 | break; |
| 5851 | } |
| 5852 | #endif /* #ifndef SQLITE_OMIT_FOREIGN_KEY */ |
| 5853 | |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 5854 | #ifndef SQLITE_OMIT_AUTOINCREMENT |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 5855 | /* Opcode: MemMax P1 P2 * * * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 5856 | ** Synopsis: r[P1]=max(r[P1],r[P2]) |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 5857 | ** |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 5858 | ** P1 is a register in the root frame of this VM (the root frame is |
| 5859 | ** different from the current frame if this instruction is being executed |
| 5860 | ** within a sub-program). Set the value of register P1 to the maximum of |
| 5861 | ** its current value and the value in register P2. |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 5862 | ** |
| 5863 | ** This instruction throws an error if the memory cell is not initially |
| 5864 | ** an integer. |
| 5865 | */ |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 5866 | case OP_MemMax: { /* in2 */ |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 5867 | VdbeFrame *pFrame; |
| 5868 | if( p->pFrame ){ |
| 5869 | for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent); |
| 5870 | pIn1 = &pFrame->aMem[pOp->p1]; |
| 5871 | }else{ |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 5872 | pIn1 = &aMem[pOp->p1]; |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 5873 | } |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 5874 | assert( memIsValid(pIn1) ); |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 5875 | sqlite3VdbeMemIntegerify(pIn1); |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 5876 | pIn2 = &aMem[pOp->p2]; |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 5877 | sqlite3VdbeMemIntegerify(pIn2); |
| 5878 | if( pIn1->u.i<pIn2->u.i){ |
| 5879 | pIn1->u.i = pIn2->u.i; |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 5880 | } |
| 5881 | break; |
| 5882 | } |
| 5883 | #endif /* SQLITE_OMIT_AUTOINCREMENT */ |
| 5884 | |
drh | 8b0cf38 | 2015-10-06 21:07:06 +0000 | [diff] [blame] | 5885 | /* Opcode: IfPos P1 P2 P3 * * |
| 5886 | ** Synopsis: if r[P1]>0 then r[P1]-=P3, goto P2 |
danielk1977 | a2dc3b1 | 2005-02-05 12:48:48 +0000 | [diff] [blame] | 5887 | ** |
drh | 1689707 | 2015-03-07 00:57:37 +0000 | [diff] [blame] | 5888 | ** Register P1 must contain an integer. |
mistachkin | 91a3ecb | 2015-10-06 21:49:55 +0000 | [diff] [blame] | 5889 | ** 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] | 5890 | ** value in P1 and jump to P2. |
drh | 6f58f70 | 2006-01-08 05:26:41 +0000 | [diff] [blame] | 5891 | ** |
drh | 1689707 | 2015-03-07 00:57:37 +0000 | [diff] [blame] | 5892 | ** If the initial value of register P1 is less than 1, then the |
| 5893 | ** value is unchanged and control passes through to the next instruction. |
danielk1977 | a2dc3b1 | 2005-02-05 12:48:48 +0000 | [diff] [blame] | 5894 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 5895 | case OP_IfPos: { /* jump, in1 */ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 5896 | pIn1 = &aMem[pOp->p1]; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 5897 | assert( pIn1->flags&MEM_Int ); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 5898 | VdbeBranchTaken( pIn1->u.i>0, 2); |
drh | 8b0cf38 | 2015-10-06 21:07:06 +0000 | [diff] [blame] | 5899 | if( pIn1->u.i>0 ){ |
| 5900 | pIn1->u.i -= pOp->p3; |
| 5901 | goto jump_to_p2; |
| 5902 | } |
drh | ec7429a | 2005-10-06 16:53:14 +0000 | [diff] [blame] | 5903 | break; |
| 5904 | } |
| 5905 | |
drh | cc2fa4c | 2016-01-25 15:57:29 +0000 | [diff] [blame] | 5906 | /* Opcode: OffsetLimit P1 P2 P3 * * |
| 5907 | ** 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] | 5908 | ** |
drh | cc2fa4c | 2016-01-25 15:57:29 +0000 | [diff] [blame] | 5909 | ** This opcode performs a commonly used computation associated with |
| 5910 | ** LIMIT and OFFSET process. r[P1] holds the limit counter. r[P3] |
| 5911 | ** holds the offset counter. The opcode computes the combined value |
| 5912 | ** of the LIMIT and OFFSET and stores that value in r[P2]. The r[P2] |
| 5913 | ** value computed is the total number of rows that will need to be |
| 5914 | ** visited in order to complete the query. |
| 5915 | ** |
| 5916 | ** If r[P3] is zero or negative, that means there is no OFFSET |
| 5917 | ** and r[P2] is set to be the value of the LIMIT, r[P1]. |
| 5918 | ** |
| 5919 | ** if r[P1] is zero or negative, that means there is no LIMIT |
| 5920 | ** and r[P2] is set to -1. |
| 5921 | ** |
| 5922 | ** 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] | 5923 | */ |
drh | cc2fa4c | 2016-01-25 15:57:29 +0000 | [diff] [blame] | 5924 | case OP_OffsetLimit: { /* in1, out2, in3 */ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 5925 | pIn1 = &aMem[pOp->p1]; |
drh | cc2fa4c | 2016-01-25 15:57:29 +0000 | [diff] [blame] | 5926 | pIn3 = &aMem[pOp->p3]; |
| 5927 | pOut = out2Prerelease(p, pOp); |
| 5928 | assert( pIn1->flags & MEM_Int ); |
| 5929 | assert( pIn3->flags & MEM_Int ); |
| 5930 | pOut->u.i = pIn1->u.i<=0 ? -1 : pIn1->u.i+(pIn3->u.i>0?pIn3->u.i:0); |
drh | 15007a9 | 2006-01-08 18:10:17 +0000 | [diff] [blame] | 5931 | break; |
| 5932 | } |
| 5933 | |
drh | 1689707 | 2015-03-07 00:57:37 +0000 | [diff] [blame] | 5934 | /* Opcode: IfNotZero P1 P2 P3 * * |
drh | 8b0cf38 | 2015-10-06 21:07:06 +0000 | [diff] [blame] | 5935 | ** Synopsis: if r[P1]!=0 then r[P1]-=P3, goto P2 |
drh | ec7429a | 2005-10-06 16:53:14 +0000 | [diff] [blame] | 5936 | ** |
drh | 1689707 | 2015-03-07 00:57:37 +0000 | [diff] [blame] | 5937 | ** Register P1 must contain an integer. If the content of register P1 is |
mistachkin | 91a3ecb | 2015-10-06 21:49:55 +0000 | [diff] [blame] | 5938 | ** initially nonzero, then subtract P3 from the value in register P1 and |
drh | 8b0cf38 | 2015-10-06 21:07:06 +0000 | [diff] [blame] | 5939 | ** jump to P2. If register P1 is initially zero, leave it unchanged |
| 5940 | ** and fall through. |
drh | ec7429a | 2005-10-06 16:53:14 +0000 | [diff] [blame] | 5941 | */ |
drh | 1689707 | 2015-03-07 00:57:37 +0000 | [diff] [blame] | 5942 | case OP_IfNotZero: { /* jump, in1 */ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 5943 | pIn1 = &aMem[pOp->p1]; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 5944 | assert( pIn1->flags&MEM_Int ); |
drh | 1689707 | 2015-03-07 00:57:37 +0000 | [diff] [blame] | 5945 | VdbeBranchTaken(pIn1->u.i<0, 2); |
| 5946 | if( pIn1->u.i ){ |
drh | 8b0cf38 | 2015-10-06 21:07:06 +0000 | [diff] [blame] | 5947 | pIn1->u.i -= pOp->p3; |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 5948 | goto jump_to_p2; |
drh | 1689707 | 2015-03-07 00:57:37 +0000 | [diff] [blame] | 5949 | } |
| 5950 | break; |
| 5951 | } |
| 5952 | |
| 5953 | /* Opcode: DecrJumpZero P1 P2 * * * |
| 5954 | ** Synopsis: if (--r[P1])==0 goto P2 |
| 5955 | ** |
| 5956 | ** Register P1 must hold an integer. Decrement the value in register P1 |
| 5957 | ** then jump to P2 if the new value is exactly zero. |
| 5958 | */ |
| 5959 | case OP_DecrJumpZero: { /* jump, in1 */ |
| 5960 | pIn1 = &aMem[pOp->p1]; |
| 5961 | assert( pIn1->flags&MEM_Int ); |
| 5962 | pIn1->u.i--; |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 5963 | VdbeBranchTaken(pIn1->u.i==0, 2); |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 5964 | if( pIn1->u.i==0 ) goto jump_to_p2; |
drh | a2a49dc | 2008-01-02 14:28:13 +0000 | [diff] [blame] | 5965 | break; |
| 5966 | } |
| 5967 | |
drh | 1689707 | 2015-03-07 00:57:37 +0000 | [diff] [blame] | 5968 | |
| 5969 | /* Opcode: JumpZeroIncr P1 P2 * * * |
| 5970 | ** Synopsis: if (r[P1]++)==0 ) goto P2 |
| 5971 | ** |
| 5972 | ** The register P1 must contain an integer. If register P1 is initially |
| 5973 | ** zero, then jump to P2. Increment register P1 regardless of whether or |
| 5974 | ** not the jump is taken. |
| 5975 | */ |
| 5976 | case OP_JumpZeroIncr: { /* jump, in1 */ |
| 5977 | pIn1 = &aMem[pOp->p1]; |
| 5978 | assert( pIn1->flags&MEM_Int ); |
| 5979 | VdbeBranchTaken(pIn1->u.i==0, 2); |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 5980 | if( (pIn1->u.i++)==0 ) goto jump_to_p2; |
drh | 1689707 | 2015-03-07 00:57:37 +0000 | [diff] [blame] | 5981 | break; |
| 5982 | } |
| 5983 | |
drh | e2d9e7c | 2015-06-26 18:47:53 +0000 | [diff] [blame] | 5984 | /* Opcode: AggStep0 * P2 P3 P4 P5 |
drh | f63552b | 2013-10-30 00:25:03 +0000 | [diff] [blame] | 5985 | ** Synopsis: accum=r[P3] step(r[P2@P5]) |
drh | e509535 | 2002-02-24 03:25:14 +0000 | [diff] [blame] | 5986 | ** |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 5987 | ** Execute the step function for an aggregate. The |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 5988 | ** function has P5 arguments. P4 is a pointer to the FuncDef |
drh | e2d9e7c | 2015-06-26 18:47:53 +0000 | [diff] [blame] | 5989 | ** structure that specifies the function. Register P3 is the |
| 5990 | ** accumulator. |
drh | e509535 | 2002-02-24 03:25:14 +0000 | [diff] [blame] | 5991 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 5992 | ** The P5 arguments are taken from register P2 and its |
| 5993 | ** successors. |
drh | e509535 | 2002-02-24 03:25:14 +0000 | [diff] [blame] | 5994 | */ |
drh | e2d9e7c | 2015-06-26 18:47:53 +0000 | [diff] [blame] | 5995 | /* Opcode: AggStep * P2 P3 P4 P5 |
| 5996 | ** Synopsis: accum=r[P3] step(r[P2@P5]) |
| 5997 | ** |
| 5998 | ** Execute the step function for an aggregate. The |
| 5999 | ** function has P5 arguments. P4 is a pointer to an sqlite3_context |
| 6000 | ** object that is used to run the function. Register P3 is |
| 6001 | ** as the accumulator. |
| 6002 | ** |
| 6003 | ** The P5 arguments are taken from register P2 and its |
| 6004 | ** successors. |
| 6005 | ** |
| 6006 | ** This opcode is initially coded as OP_AggStep0. On first evaluation, |
| 6007 | ** the FuncDef stored in P4 is converted into an sqlite3_context and |
| 6008 | ** the opcode is changed. In this way, the initialization of the |
| 6009 | ** sqlite3_context only happens once, instead of on each call to the |
| 6010 | ** step function. |
| 6011 | */ |
drh | 9c7c913 | 2015-06-26 18:16:52 +0000 | [diff] [blame] | 6012 | case OP_AggStep0: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 6013 | int n; |
drh | 9c7c913 | 2015-06-26 18:16:52 +0000 | [diff] [blame] | 6014 | sqlite3_context *pCtx; |
drh | e509535 | 2002-02-24 03:25:14 +0000 | [diff] [blame] | 6015 | |
drh | 9c7c913 | 2015-06-26 18:16:52 +0000 | [diff] [blame] | 6016 | assert( pOp->p4type==P4_FUNCDEF ); |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 6017 | n = pOp->p5; |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 6018 | assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) ); |
| 6019 | 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] | 6020 | assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n ); |
drh | 575fad6 | 2016-02-05 13:38:36 +0000 | [diff] [blame] | 6021 | pCtx = sqlite3DbMallocRawNN(db, sizeof(*pCtx) + (n-1)*sizeof(sqlite3_value*)); |
drh | 9c7c913 | 2015-06-26 18:16:52 +0000 | [diff] [blame] | 6022 | if( pCtx==0 ) goto no_mem; |
| 6023 | pCtx->pMem = 0; |
| 6024 | pCtx->pFunc = pOp->p4.pFunc; |
| 6025 | pCtx->iOp = (int)(pOp - aOp); |
| 6026 | pCtx->pVdbe = p; |
| 6027 | pCtx->argc = n; |
| 6028 | pOp->p4type = P4_FUNCCTX; |
| 6029 | pOp->p4.pCtx = pCtx; |
| 6030 | pOp->opcode = OP_AggStep; |
| 6031 | /* Fall through into OP_AggStep */ |
| 6032 | } |
| 6033 | case OP_AggStep: { |
| 6034 | int i; |
| 6035 | sqlite3_context *pCtx; |
| 6036 | Mem *pMem; |
| 6037 | Mem t; |
| 6038 | |
| 6039 | assert( pOp->p4type==P4_FUNCCTX ); |
| 6040 | pCtx = pOp->p4.pCtx; |
| 6041 | pMem = &aMem[pOp->p3]; |
| 6042 | |
| 6043 | /* If this function is inside of a trigger, the register array in aMem[] |
| 6044 | ** might change from one evaluation to the next. The next block of code |
| 6045 | ** checks to see if the register array has changed, and if so it |
| 6046 | ** reinitializes the relavant parts of the sqlite3_context object */ |
| 6047 | if( pCtx->pMem != pMem ){ |
| 6048 | pCtx->pMem = pMem; |
| 6049 | for(i=pCtx->argc-1; i>=0; i--) pCtx->argv[i] = &aMem[pOp->p2+i]; |
| 6050 | } |
| 6051 | |
| 6052 | #ifdef SQLITE_DEBUG |
| 6053 | for(i=0; i<pCtx->argc; i++){ |
| 6054 | assert( memIsValid(pCtx->argv[i]) ); |
| 6055 | REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]); |
| 6056 | } |
| 6057 | #endif |
| 6058 | |
drh | abfcea2 | 2005-09-06 20:36:48 +0000 | [diff] [blame] | 6059 | pMem->n++; |
drh | d3b7420 | 2014-09-17 16:41:15 +0000 | [diff] [blame] | 6060 | sqlite3VdbeMemInit(&t, db, MEM_Null); |
drh | 9c7c913 | 2015-06-26 18:16:52 +0000 | [diff] [blame] | 6061 | pCtx->pOut = &t; |
| 6062 | pCtx->fErrorOrAux = 0; |
| 6063 | pCtx->skipFlag = 0; |
drh | 2d80151 | 2016-01-14 22:19:58 +0000 | [diff] [blame] | 6064 | (pCtx->pFunc->xSFunc)(pCtx,pCtx->argc,pCtx->argv); /* IMP: R-24505-23230 */ |
drh | 9c7c913 | 2015-06-26 18:16:52 +0000 | [diff] [blame] | 6065 | if( pCtx->fErrorOrAux ){ |
| 6066 | if( pCtx->isError ){ |
| 6067 | sqlite3VdbeError(p, "%s", sqlite3_value_text(&t)); |
| 6068 | rc = pCtx->isError; |
| 6069 | } |
| 6070 | sqlite3VdbeMemRelease(&t); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 6071 | if( rc ) goto abort_due_to_error; |
drh | 9c7c913 | 2015-06-26 18:16:52 +0000 | [diff] [blame] | 6072 | }else{ |
| 6073 | assert( t.flags==MEM_Null ); |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 6074 | } |
drh | 9c7c913 | 2015-06-26 18:16:52 +0000 | [diff] [blame] | 6075 | if( pCtx->skipFlag ){ |
drh | 7a95789 | 2012-02-02 17:35:43 +0000 | [diff] [blame] | 6076 | assert( pOp[-1].opcode==OP_CollSeq ); |
| 6077 | i = pOp[-1].p1; |
| 6078 | if( i ) sqlite3VdbeMemSetInt64(&aMem[i], 1); |
| 6079 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 6080 | break; |
| 6081 | } |
| 6082 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 6083 | /* Opcode: AggFinal P1 P2 * P4 * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 6084 | ** Synopsis: accum=r[P1] N=P2 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 6085 | ** |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 6086 | ** Execute the finalizer function for an aggregate. P1 is |
| 6087 | ** the memory location that is the accumulator for the aggregate. |
drh | a10a34b | 2005-09-07 22:09:48 +0000 | [diff] [blame] | 6088 | ** |
| 6089 | ** P2 is the number of arguments that the step function takes and |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 6090 | ** P4 is a pointer to the FuncDef for this function. The P2 |
drh | a10a34b | 2005-09-07 22:09:48 +0000 | [diff] [blame] | 6091 | ** argument is not used by this opcode. It is only there to disambiguate |
| 6092 | ** functions that can take varying numbers of arguments. The |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 6093 | ** P4 argument is only needed for the degenerate case where |
drh | a10a34b | 2005-09-07 22:09:48 +0000 | [diff] [blame] | 6094 | ** the step function was not previously called. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 6095 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 6096 | case OP_AggFinal: { |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 6097 | Mem *pMem; |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 6098 | assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) ); |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 6099 | pMem = &aMem[pOp->p1]; |
drh | a10a34b | 2005-09-07 22:09:48 +0000 | [diff] [blame] | 6100 | assert( (pMem->flags & ~(MEM_Null|MEM_Agg))==0 ); |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 6101 | rc = sqlite3VdbeMemFinalize(pMem, pOp->p4.pFunc); |
drh | 4c8555f | 2009-06-25 01:47:11 +0000 | [diff] [blame] | 6102 | if( rc ){ |
drh | 22c17b8 | 2015-05-15 04:13:15 +0000 | [diff] [blame] | 6103 | sqlite3VdbeError(p, "%s", sqlite3_value_text(pMem)); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 6104 | goto abort_due_to_error; |
drh | 90669c1 | 2006-01-20 15:45:36 +0000 | [diff] [blame] | 6105 | } |
drh | 2dca868 | 2008-03-21 17:13:13 +0000 | [diff] [blame] | 6106 | sqlite3VdbeChangeEncoding(pMem, encoding); |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 6107 | UPDATE_MAX_BLOBSIZE(pMem); |
drh | 023ae03 | 2007-05-08 12:12:16 +0000 | [diff] [blame] | 6108 | if( sqlite3VdbeMemTooBig(pMem) ){ |
| 6109 | goto too_big; |
| 6110 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 6111 | break; |
| 6112 | } |
| 6113 | |
dan | 5cf5353 | 2010-05-01 16:40:20 +0000 | [diff] [blame] | 6114 | #ifndef SQLITE_OMIT_WAL |
dan | cdc1f04 | 2010-11-18 12:11:05 +0000 | [diff] [blame] | 6115 | /* Opcode: Checkpoint P1 P2 P3 * * |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 6116 | ** |
| 6117 | ** 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] | 6118 | ** WAL mode. Parameter P2 is one of SQLITE_CHECKPOINT_PASSIVE, FULL, |
| 6119 | ** 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] | 6120 | ** SQLITE_BUSY or not, respectively. Write the number of pages in the |
| 6121 | ** WAL after the checkpoint into mem[P3+1] and the number of pages |
| 6122 | ** in the WAL that have been checkpointed after the checkpoint |
| 6123 | ** completes into mem[P3+2]. However on an error, mem[P3+1] and |
| 6124 | ** mem[P3+2] are initialized to -1. |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 6125 | */ |
| 6126 | case OP_Checkpoint: { |
drh | 30aa3b9 | 2011-02-07 23:56:01 +0000 | [diff] [blame] | 6127 | int i; /* Loop counter */ |
| 6128 | int aRes[3]; /* Results */ |
| 6129 | Mem *pMem; /* Write results here */ |
| 6130 | |
drh | 9e92a47 | 2013-06-27 17:40:30 +0000 | [diff] [blame] | 6131 | assert( p->readOnly==0 ); |
drh | 30aa3b9 | 2011-02-07 23:56:01 +0000 | [diff] [blame] | 6132 | aRes[0] = 0; |
| 6133 | aRes[1] = aRes[2] = -1; |
dan | cdc1f04 | 2010-11-18 12:11:05 +0000 | [diff] [blame] | 6134 | assert( pOp->p2==SQLITE_CHECKPOINT_PASSIVE |
| 6135 | || pOp->p2==SQLITE_CHECKPOINT_FULL |
| 6136 | || pOp->p2==SQLITE_CHECKPOINT_RESTART |
dan | f26a154 | 2014-12-02 19:04:54 +0000 | [diff] [blame] | 6137 | || pOp->p2==SQLITE_CHECKPOINT_TRUNCATE |
dan | cdc1f04 | 2010-11-18 12:11:05 +0000 | [diff] [blame] | 6138 | ); |
drh | 30aa3b9 | 2011-02-07 23:56:01 +0000 | [diff] [blame] | 6139 | rc = sqlite3Checkpoint(db, pOp->p1, pOp->p2, &aRes[1], &aRes[2]); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 6140 | if( rc ){ |
| 6141 | if( rc!=SQLITE_BUSY ) goto abort_due_to_error; |
dan | cdc1f04 | 2010-11-18 12:11:05 +0000 | [diff] [blame] | 6142 | rc = SQLITE_OK; |
drh | 30aa3b9 | 2011-02-07 23:56:01 +0000 | [diff] [blame] | 6143 | aRes[0] = 1; |
dan | cdc1f04 | 2010-11-18 12:11:05 +0000 | [diff] [blame] | 6144 | } |
drh | 30aa3b9 | 2011-02-07 23:56:01 +0000 | [diff] [blame] | 6145 | for(i=0, pMem = &aMem[pOp->p3]; i<3; i++, pMem++){ |
| 6146 | sqlite3VdbeMemSetInt64(pMem, (i64)aRes[i]); |
| 6147 | } |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 6148 | break; |
| 6149 | }; |
dan | 5cf5353 | 2010-05-01 16:40:20 +0000 | [diff] [blame] | 6150 | #endif |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 6151 | |
drh | cac29a6 | 2010-07-02 19:36:52 +0000 | [diff] [blame] | 6152 | #ifndef SQLITE_OMIT_PRAGMA |
drh | 0fd6135 | 2014-02-07 02:29:45 +0000 | [diff] [blame] | 6153 | /* Opcode: JournalMode P1 P2 P3 * * |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 6154 | ** |
| 6155 | ** Change the journal mode of database P1 to P3. P3 must be one of the |
| 6156 | ** PAGER_JOURNALMODE_XXX values. If changing between the various rollback |
| 6157 | ** modes (delete, truncate, persist, off and memory), this is a simple |
| 6158 | ** operation. No IO is required. |
| 6159 | ** |
| 6160 | ** If changing into or out of WAL mode the procedure is more complicated. |
| 6161 | ** |
| 6162 | ** Write a string containing the final journal-mode to register P2. |
| 6163 | */ |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 6164 | case OP_JournalMode: { /* out2 */ |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 6165 | Btree *pBt; /* Btree to change journal mode of */ |
| 6166 | Pager *pPager; /* Pager associated with pBt */ |
drh | d80b233 | 2010-05-01 00:59:37 +0000 | [diff] [blame] | 6167 | int eNew; /* New journal mode */ |
| 6168 | int eOld; /* The old journal mode */ |
mistachkin | 59ee77c | 2012-09-13 15:26:44 +0000 | [diff] [blame] | 6169 | #ifndef SQLITE_OMIT_WAL |
drh | d80b233 | 2010-05-01 00:59:37 +0000 | [diff] [blame] | 6170 | const char *zFilename; /* Name of database file for pPager */ |
mistachkin | 59ee77c | 2012-09-13 15:26:44 +0000 | [diff] [blame] | 6171 | #endif |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 6172 | |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 6173 | pOut = out2Prerelease(p, pOp); |
drh | d80b233 | 2010-05-01 00:59:37 +0000 | [diff] [blame] | 6174 | eNew = pOp->p3; |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 6175 | assert( eNew==PAGER_JOURNALMODE_DELETE |
| 6176 | || eNew==PAGER_JOURNALMODE_TRUNCATE |
| 6177 | || eNew==PAGER_JOURNALMODE_PERSIST |
| 6178 | || eNew==PAGER_JOURNALMODE_OFF |
| 6179 | || eNew==PAGER_JOURNALMODE_MEMORY |
| 6180 | || eNew==PAGER_JOURNALMODE_WAL |
| 6181 | || eNew==PAGER_JOURNALMODE_QUERY |
| 6182 | ); |
| 6183 | assert( pOp->p1>=0 && pOp->p1<db->nDb ); |
drh | 9e92a47 | 2013-06-27 17:40:30 +0000 | [diff] [blame] | 6184 | assert( p->readOnly==0 ); |
drh | 3ebaee9 | 2010-05-06 21:37:22 +0000 | [diff] [blame] | 6185 | |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 6186 | pBt = db->aDb[pOp->p1].pBt; |
| 6187 | pPager = sqlite3BtreePager(pBt); |
drh | 0b9b430 | 2010-06-11 17:01:24 +0000 | [diff] [blame] | 6188 | eOld = sqlite3PagerGetJournalMode(pPager); |
| 6189 | if( eNew==PAGER_JOURNALMODE_QUERY ) eNew = eOld; |
| 6190 | if( !sqlite3PagerOkToChangeJournalMode(pPager) ) eNew = eOld; |
dan | 5cf5353 | 2010-05-01 16:40:20 +0000 | [diff] [blame] | 6191 | |
| 6192 | #ifndef SQLITE_OMIT_WAL |
drh | d4e0bb0 | 2012-05-27 01:19:04 +0000 | [diff] [blame] | 6193 | zFilename = sqlite3PagerFilename(pPager, 1); |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 6194 | |
drh | d80b233 | 2010-05-01 00:59:37 +0000 | [diff] [blame] | 6195 | /* Do not allow a transition to journal_mode=WAL for a database |
drh | 6e1f482 | 2010-07-13 23:41:40 +0000 | [diff] [blame] | 6196 | ** in temporary storage or if the VFS does not support shared memory |
drh | d80b233 | 2010-05-01 00:59:37 +0000 | [diff] [blame] | 6197 | */ |
| 6198 | if( eNew==PAGER_JOURNALMODE_WAL |
drh | 057fc81 | 2011-10-17 23:15:31 +0000 | [diff] [blame] | 6199 | && (sqlite3Strlen30(zFilename)==0 /* Temp file */ |
drh | 6e1f482 | 2010-07-13 23:41:40 +0000 | [diff] [blame] | 6200 | || !sqlite3PagerWalSupported(pPager)) /* No shared-memory support */ |
dan | e180c29 | 2010-04-26 17:42:56 +0000 | [diff] [blame] | 6201 | ){ |
drh | 0b9b430 | 2010-06-11 17:01:24 +0000 | [diff] [blame] | 6202 | eNew = eOld; |
dan | e180c29 | 2010-04-26 17:42:56 +0000 | [diff] [blame] | 6203 | } |
| 6204 | |
drh | 0b9b430 | 2010-06-11 17:01:24 +0000 | [diff] [blame] | 6205 | if( (eNew!=eOld) |
| 6206 | && (eOld==PAGER_JOURNALMODE_WAL || eNew==PAGER_JOURNALMODE_WAL) |
| 6207 | ){ |
dan | c0537fe | 2013-06-28 19:41:43 +0000 | [diff] [blame] | 6208 | if( !db->autoCommit || db->nVdbeRead>1 ){ |
drh | 0b9b430 | 2010-06-11 17:01:24 +0000 | [diff] [blame] | 6209 | rc = SQLITE_ERROR; |
drh | 22c17b8 | 2015-05-15 04:13:15 +0000 | [diff] [blame] | 6210 | sqlite3VdbeError(p, |
drh | 0b9b430 | 2010-06-11 17:01:24 +0000 | [diff] [blame] | 6211 | "cannot change %s wal mode from within a transaction", |
| 6212 | (eNew==PAGER_JOURNALMODE_WAL ? "into" : "out of") |
| 6213 | ); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 6214 | goto abort_due_to_error; |
drh | 0b9b430 | 2010-06-11 17:01:24 +0000 | [diff] [blame] | 6215 | }else{ |
| 6216 | |
| 6217 | if( eOld==PAGER_JOURNALMODE_WAL ){ |
| 6218 | /* If leaving WAL mode, close the log file. If successful, the call |
| 6219 | ** to PagerCloseWal() checkpoints and deletes the write-ahead-log |
| 6220 | ** file. An EXCLUSIVE lock may still be held on the database file |
| 6221 | ** after a successful return. |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 6222 | */ |
drh | 0b9b430 | 2010-06-11 17:01:24 +0000 | [diff] [blame] | 6223 | rc = sqlite3PagerCloseWal(pPager); |
drh | ab9b744 | 2010-05-10 11:20:05 +0000 | [diff] [blame] | 6224 | if( rc==SQLITE_OK ){ |
drh | 0b9b430 | 2010-06-11 17:01:24 +0000 | [diff] [blame] | 6225 | sqlite3PagerSetJournalMode(pPager, eNew); |
drh | 89c3f2f | 2010-05-15 01:09:38 +0000 | [diff] [blame] | 6226 | } |
drh | 242c4f7 | 2010-06-22 14:49:39 +0000 | [diff] [blame] | 6227 | }else if( eOld==PAGER_JOURNALMODE_MEMORY ){ |
| 6228 | /* Cannot transition directly from MEMORY to WAL. Use mode OFF |
| 6229 | ** as an intermediate */ |
| 6230 | sqlite3PagerSetJournalMode(pPager, PAGER_JOURNALMODE_OFF); |
drh | 0b9b430 | 2010-06-11 17:01:24 +0000 | [diff] [blame] | 6231 | } |
| 6232 | |
| 6233 | /* Open a transaction on the database file. Regardless of the journal |
| 6234 | ** mode, this transaction always uses a rollback journal. |
| 6235 | */ |
| 6236 | assert( sqlite3BtreeIsInTrans(pBt)==0 ); |
| 6237 | if( rc==SQLITE_OK ){ |
dan | 731bf5b | 2010-06-17 16:44:21 +0000 | [diff] [blame] | 6238 | rc = sqlite3BtreeSetVersion(pBt, (eNew==PAGER_JOURNALMODE_WAL ? 2 : 1)); |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 6239 | } |
| 6240 | } |
| 6241 | } |
dan | 5cf5353 | 2010-05-01 16:40:20 +0000 | [diff] [blame] | 6242 | #endif /* ifndef SQLITE_OMIT_WAL */ |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 6243 | |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 6244 | if( rc ) eNew = eOld; |
drh | 0b9b430 | 2010-06-11 17:01:24 +0000 | [diff] [blame] | 6245 | eNew = sqlite3PagerSetJournalMode(pPager, eNew); |
dan | 731bf5b | 2010-06-17 16:44:21 +0000 | [diff] [blame] | 6246 | |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 6247 | pOut->flags = MEM_Str|MEM_Static|MEM_Term; |
dan | b978002 | 2010-04-21 18:37:57 +0000 | [diff] [blame] | 6248 | pOut->z = (char *)sqlite3JournalModename(eNew); |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 6249 | pOut->n = sqlite3Strlen30(pOut->z); |
| 6250 | pOut->enc = SQLITE_UTF8; |
| 6251 | sqlite3VdbeChangeEncoding(pOut, encoding); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 6252 | if( rc ) goto abort_due_to_error; |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 6253 | break; |
drh | cac29a6 | 2010-07-02 19:36:52 +0000 | [diff] [blame] | 6254 | }; |
| 6255 | #endif /* SQLITE_OMIT_PRAGMA */ |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 6256 | |
drh | fdbcdee | 2007-03-27 14:44:50 +0000 | [diff] [blame] | 6257 | #if !defined(SQLITE_OMIT_VACUUM) && !defined(SQLITE_OMIT_ATTACH) |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 6258 | /* Opcode: Vacuum * * * * * |
drh | 6f8c91c | 2003-12-07 00:24:35 +0000 | [diff] [blame] | 6259 | ** |
| 6260 | ** Vacuum the entire database. This opcode will cause other virtual |
| 6261 | ** machines to be created and run. It may not be called from within |
| 6262 | ** a transaction. |
| 6263 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 6264 | case OP_Vacuum: { |
drh | 9e92a47 | 2013-06-27 17:40:30 +0000 | [diff] [blame] | 6265 | assert( p->readOnly==0 ); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 6266 | rc = sqlite3RunVacuum(&p->zErrMsg, db); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 6267 | if( rc ) goto abort_due_to_error; |
drh | 6f8c91c | 2003-12-07 00:24:35 +0000 | [diff] [blame] | 6268 | break; |
| 6269 | } |
drh | 154d4b2 | 2006-09-21 11:02:16 +0000 | [diff] [blame] | 6270 | #endif |
drh | 6f8c91c | 2003-12-07 00:24:35 +0000 | [diff] [blame] | 6271 | |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 6272 | #if !defined(SQLITE_OMIT_AUTOVACUUM) |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 6273 | /* Opcode: IncrVacuum P1 P2 * * * |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 6274 | ** |
| 6275 | ** Perform a single step of the incremental vacuum procedure on |
drh | ca5557f | 2007-05-04 18:30:40 +0000 | [diff] [blame] | 6276 | ** the P1 database. If the vacuum has finished, jump to instruction |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 6277 | ** P2. Otherwise, fall through to the next instruction. |
| 6278 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 6279 | case OP_IncrVacuum: { /* jump */ |
drh | ca5557f | 2007-05-04 18:30:40 +0000 | [diff] [blame] | 6280 | Btree *pBt; |
| 6281 | |
| 6282 | assert( pOp->p1>=0 && pOp->p1<db->nDb ); |
drh | a7ab6d8 | 2014-07-21 15:44:39 +0000 | [diff] [blame] | 6283 | assert( DbMaskTest(p->btreeMask, pOp->p1) ); |
drh | 9e92a47 | 2013-06-27 17:40:30 +0000 | [diff] [blame] | 6284 | assert( p->readOnly==0 ); |
drh | ca5557f | 2007-05-04 18:30:40 +0000 | [diff] [blame] | 6285 | pBt = db->aDb[pOp->p1].pBt; |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 6286 | rc = sqlite3BtreeIncrVacuum(pBt); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 6287 | VdbeBranchTaken(rc==SQLITE_DONE,2); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 6288 | if( rc ){ |
| 6289 | if( rc!=SQLITE_DONE ) goto abort_due_to_error; |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 6290 | rc = SQLITE_OK; |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 6291 | goto jump_to_p2; |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 6292 | } |
| 6293 | break; |
| 6294 | } |
| 6295 | #endif |
| 6296 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 6297 | /* Opcode: Expire P1 * * * * |
danielk1977 | a21c6b6 | 2005-01-24 10:25:59 +0000 | [diff] [blame] | 6298 | ** |
drh | 25df48d | 2014-07-22 14:58:12 +0000 | [diff] [blame] | 6299 | ** Cause precompiled statements to expire. When an expired statement |
| 6300 | ** is executed using sqlite3_step() it will either automatically |
| 6301 | ** reprepare itself (if it was originally created using sqlite3_prepare_v2()) |
| 6302 | ** or it will fail with SQLITE_SCHEMA. |
danielk1977 | a21c6b6 | 2005-01-24 10:25:59 +0000 | [diff] [blame] | 6303 | ** |
| 6304 | ** 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] | 6305 | ** then only the currently executing statement is expired. |
danielk1977 | a21c6b6 | 2005-01-24 10:25:59 +0000 | [diff] [blame] | 6306 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 6307 | case OP_Expire: { |
danielk1977 | a21c6b6 | 2005-01-24 10:25:59 +0000 | [diff] [blame] | 6308 | if( !pOp->p1 ){ |
| 6309 | sqlite3ExpirePreparedStatements(db); |
| 6310 | }else{ |
| 6311 | p->expired = 1; |
| 6312 | } |
| 6313 | break; |
| 6314 | } |
| 6315 | |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 6316 | #ifndef SQLITE_OMIT_SHARED_CACHE |
drh | 6a9ad3d | 2008-04-02 16:29:30 +0000 | [diff] [blame] | 6317 | /* Opcode: TableLock P1 P2 P3 P4 * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 6318 | ** Synopsis: iDb=P1 root=P2 write=P3 |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 6319 | ** |
| 6320 | ** Obtain a lock on a particular table. This instruction is only used when |
| 6321 | ** the shared-cache feature is enabled. |
| 6322 | ** |
danielk1977 | 96d48e9 | 2009-06-29 06:00:37 +0000 | [diff] [blame] | 6323 | ** P1 is the index of the database in sqlite3.aDb[] of the database |
drh | 6a9ad3d | 2008-04-02 16:29:30 +0000 | [diff] [blame] | 6324 | ** on which the lock is acquired. A readlock is obtained if P3==0 or |
| 6325 | ** a write lock if P3==1. |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 6326 | ** |
| 6327 | ** P2 contains the root-page of the table to lock. |
| 6328 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 6329 | ** 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] | 6330 | ** used to generate an error message if the lock cannot be obtained. |
| 6331 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 6332 | case OP_TableLock: { |
danielk1977 | e0d9e6f | 2009-07-03 16:25:06 +0000 | [diff] [blame] | 6333 | u8 isWriteLock = (u8)pOp->p3; |
| 6334 | if( isWriteLock || 0==(db->flags&SQLITE_ReadUncommitted) ){ |
| 6335 | int p1 = pOp->p1; |
| 6336 | assert( p1>=0 && p1<db->nDb ); |
drh | a7ab6d8 | 2014-07-21 15:44:39 +0000 | [diff] [blame] | 6337 | assert( DbMaskTest(p->btreeMask, p1) ); |
danielk1977 | e0d9e6f | 2009-07-03 16:25:06 +0000 | [diff] [blame] | 6338 | assert( isWriteLock==0 || isWriteLock==1 ); |
| 6339 | rc = sqlite3BtreeLockTable(db->aDb[p1].pBt, pOp->p2, isWriteLock); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 6340 | if( rc ){ |
| 6341 | if( (rc&0xFF)==SQLITE_LOCKED ){ |
| 6342 | const char *z = pOp->p4.z; |
| 6343 | sqlite3VdbeError(p, "database table is locked: %s", z); |
| 6344 | } |
| 6345 | goto abort_due_to_error; |
danielk1977 | e0d9e6f | 2009-07-03 16:25:06 +0000 | [diff] [blame] | 6346 | } |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 6347 | } |
| 6348 | break; |
| 6349 | } |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 6350 | #endif /* SQLITE_OMIT_SHARED_CACHE */ |
| 6351 | |
| 6352 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 6353 | /* Opcode: VBegin * * * P4 * |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 6354 | ** |
danielk1977 | 3e3a84d | 2008-08-01 17:37:40 +0000 | [diff] [blame] | 6355 | ** P4 may be a pointer to an sqlite3_vtab structure. If so, call the |
| 6356 | ** xBegin method for that table. |
| 6357 | ** |
| 6358 | ** 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] | 6359 | ** within a callback to a virtual table xSync() method. If it is, the error |
| 6360 | ** code will be set to SQLITE_LOCKED. |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 6361 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 6362 | case OP_VBegin: { |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 6363 | VTable *pVTab; |
| 6364 | pVTab = pOp->p4.pVtab; |
| 6365 | rc = sqlite3VtabBegin(db, pVTab); |
dan | 016f781 | 2013-08-21 17:35:48 +0000 | [diff] [blame] | 6366 | if( pVTab ) sqlite3VtabImportErrmsg(p, pVTab->pVtab); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 6367 | if( rc ) goto abort_due_to_error; |
danielk1977 | f9e7dda | 2006-06-16 16:08:53 +0000 | [diff] [blame] | 6368 | break; |
| 6369 | } |
| 6370 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 6371 | |
| 6372 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
dan | 7377945 | 2015-03-19 18:56:17 +0000 | [diff] [blame] | 6373 | /* Opcode: VCreate P1 P2 * * * |
danielk1977 | f9e7dda | 2006-06-16 16:08:53 +0000 | [diff] [blame] | 6374 | ** |
dan | 7377945 | 2015-03-19 18:56:17 +0000 | [diff] [blame] | 6375 | ** P2 is a register that holds the name of a virtual table in database |
| 6376 | ** P1. Call the xCreate method for that table. |
danielk1977 | f9e7dda | 2006-06-16 16:08:53 +0000 | [diff] [blame] | 6377 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 6378 | case OP_VCreate: { |
dan | 7377945 | 2015-03-19 18:56:17 +0000 | [diff] [blame] | 6379 | Mem sMem; /* For storing the record being decoded */ |
drh | 4746406 | 2015-03-21 12:22:16 +0000 | [diff] [blame] | 6380 | const char *zTab; /* Name of the virtual table */ |
| 6381 | |
dan | 7377945 | 2015-03-19 18:56:17 +0000 | [diff] [blame] | 6382 | memset(&sMem, 0, sizeof(sMem)); |
| 6383 | sMem.db = db; |
drh | 4746406 | 2015-03-21 12:22:16 +0000 | [diff] [blame] | 6384 | /* Because P2 is always a static string, it is impossible for the |
| 6385 | ** sqlite3VdbeMemCopy() to fail */ |
| 6386 | assert( (aMem[pOp->p2].flags & MEM_Str)!=0 ); |
| 6387 | assert( (aMem[pOp->p2].flags & MEM_Static)!=0 ); |
dan | 7377945 | 2015-03-19 18:56:17 +0000 | [diff] [blame] | 6388 | rc = sqlite3VdbeMemCopy(&sMem, &aMem[pOp->p2]); |
drh | 4746406 | 2015-03-21 12:22:16 +0000 | [diff] [blame] | 6389 | assert( rc==SQLITE_OK ); |
| 6390 | zTab = (const char*)sqlite3_value_text(&sMem); |
| 6391 | assert( zTab || db->mallocFailed ); |
| 6392 | if( zTab ){ |
| 6393 | rc = sqlite3VtabCallCreate(db, pOp->p1, zTab, &p->zErrMsg); |
dan | 7377945 | 2015-03-19 18:56:17 +0000 | [diff] [blame] | 6394 | } |
| 6395 | sqlite3VdbeMemRelease(&sMem); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 6396 | if( rc ) goto abort_due_to_error; |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 6397 | break; |
| 6398 | } |
| 6399 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 6400 | |
| 6401 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 6402 | /* Opcode: VDestroy P1 * * P4 * |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 6403 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 6404 | ** 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] | 6405 | ** of that table. |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 6406 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 6407 | case OP_VDestroy: { |
drh | 086723a | 2015-03-24 12:51:52 +0000 | [diff] [blame] | 6408 | db->nVDestroy++; |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 6409 | rc = sqlite3VtabCallDestroy(db, pOp->p1, pOp->p4.z); |
drh | 086723a | 2015-03-24 12:51:52 +0000 | [diff] [blame] | 6410 | db->nVDestroy--; |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 6411 | if( rc ) goto abort_due_to_error; |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 6412 | break; |
| 6413 | } |
| 6414 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 6415 | |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 6416 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 6417 | /* Opcode: VOpen P1 * * P4 * |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 6418 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 6419 | ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure. |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 6420 | ** P1 is a cursor number. This opcode opens a cursor to the virtual |
| 6421 | ** table and stores that cursor in P1. |
| 6422 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 6423 | case OP_VOpen: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 6424 | VdbeCursor *pCur; |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 6425 | sqlite3_vtab_cursor *pVCur; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 6426 | sqlite3_vtab *pVtab; |
drh | f496a7d | 2015-03-24 14:05:50 +0000 | [diff] [blame] | 6427 | const sqlite3_module *pModule; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 6428 | |
drh | 1713afb | 2013-06-28 01:24:57 +0000 | [diff] [blame] | 6429 | assert( p->bIsReader ); |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 6430 | pCur = 0; |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 6431 | pVCur = 0; |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 6432 | pVtab = pOp->p4.pVtab->pVtab; |
drh | f496a7d | 2015-03-24 14:05:50 +0000 | [diff] [blame] | 6433 | if( pVtab==0 || NEVER(pVtab->pModule==0) ){ |
| 6434 | rc = SQLITE_LOCKED; |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 6435 | goto abort_due_to_error; |
drh | f496a7d | 2015-03-24 14:05:50 +0000 | [diff] [blame] | 6436 | } |
| 6437 | pModule = pVtab->pModule; |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 6438 | rc = pModule->xOpen(pVtab, &pVCur); |
dan | 016f781 | 2013-08-21 17:35:48 +0000 | [diff] [blame] | 6439 | sqlite3VtabImportErrmsg(p, pVtab); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 6440 | if( rc ) goto abort_due_to_error; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 6441 | |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 6442 | /* Initialize sqlite3_vtab_cursor base class */ |
| 6443 | pVCur->pVtab = pVtab; |
| 6444 | |
| 6445 | /* Initialize vdbe cursor object */ |
| 6446 | pCur = allocateCursor(p, pOp->p1, 0, -1, CURTYPE_VTAB); |
| 6447 | if( pCur ){ |
| 6448 | pCur->uc.pVCur = pVCur; |
| 6449 | pVtab->nRef++; |
| 6450 | }else{ |
| 6451 | assert( db->mallocFailed ); |
| 6452 | pModule->xClose(pVCur); |
| 6453 | goto no_mem; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 6454 | } |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 6455 | break; |
| 6456 | } |
| 6457 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 6458 | |
| 6459 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
danielk1977 | 6dbee81 | 2008-01-03 18:39:41 +0000 | [diff] [blame] | 6460 | /* Opcode: VFilter P1 P2 P3 P4 * |
drh | 831116d | 2014-04-03 14:31:00 +0000 | [diff] [blame] | 6461 | ** Synopsis: iplan=r[P3] zplan='P4' |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 6462 | ** |
| 6463 | ** P1 is a cursor opened using VOpen. P2 is an address to jump to if |
| 6464 | ** the filtered result set is empty. |
| 6465 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 6466 | ** P4 is either NULL or a string that was generated by the xBestIndex |
| 6467 | ** method of the module. The interpretation of the P4 string is left |
drh | 4be8b51 | 2006-06-13 23:51:34 +0000 | [diff] [blame] | 6468 | ** to the module implementation. |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 6469 | ** |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 6470 | ** This opcode invokes the xFilter method on the virtual table specified |
danielk1977 | 6dbee81 | 2008-01-03 18:39:41 +0000 | [diff] [blame] | 6471 | ** by P1. The integer query plan parameter to xFilter is stored in register |
| 6472 | ** P3. Register P3+1 stores the argc parameter to be passed to the |
drh | 174edc6 | 2008-05-29 05:23:41 +0000 | [diff] [blame] | 6473 | ** xFilter method. Registers P3+2..P3+1+argc are the argc |
| 6474 | ** additional parameters which are passed to |
danielk1977 | 6dbee81 | 2008-01-03 18:39:41 +0000 | [diff] [blame] | 6475 | ** xFilter as argv. Register P3+2 becomes argv[0] when passed to xFilter. |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 6476 | ** |
danielk1977 | 6dbee81 | 2008-01-03 18:39:41 +0000 | [diff] [blame] | 6477 | ** 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] | 6478 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 6479 | case OP_VFilter: { /* jump */ |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 6480 | int nArg; |
danielk1977 | 6dbee81 | 2008-01-03 18:39:41 +0000 | [diff] [blame] | 6481 | int iQuery; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 6482 | const sqlite3_module *pModule; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 6483 | Mem *pQuery; |
| 6484 | Mem *pArgc; |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 6485 | sqlite3_vtab_cursor *pVCur; |
drh | 4dc754d | 2008-07-23 18:17:32 +0000 | [diff] [blame] | 6486 | sqlite3_vtab *pVtab; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 6487 | VdbeCursor *pCur; |
| 6488 | int res; |
| 6489 | int i; |
| 6490 | Mem **apArg; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 6491 | |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 6492 | pQuery = &aMem[pOp->p3]; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 6493 | pArgc = &pQuery[1]; |
| 6494 | pCur = p->apCsr[pOp->p1]; |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 6495 | assert( memIsValid(pQuery) ); |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 6496 | REGISTER_TRACE(pOp->p3, pQuery); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 6497 | assert( pCur->eCurType==CURTYPE_VTAB ); |
| 6498 | pVCur = pCur->uc.pVCur; |
| 6499 | pVtab = pVCur->pVtab; |
drh | 4dc754d | 2008-07-23 18:17:32 +0000 | [diff] [blame] | 6500 | pModule = pVtab->pModule; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 6501 | |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 6502 | /* Grab the index number and argc parameters */ |
danielk1977 | 6dbee81 | 2008-01-03 18:39:41 +0000 | [diff] [blame] | 6503 | assert( (pQuery->flags&MEM_Int)!=0 && pArgc->flags==MEM_Int ); |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 6504 | nArg = (int)pArgc->u.i; |
| 6505 | iQuery = (int)pQuery->u.i; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 6506 | |
drh | 644a529 | 2006-12-20 14:53:38 +0000 | [diff] [blame] | 6507 | /* Invoke the xFilter method */ |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 6508 | res = 0; |
| 6509 | apArg = p->apArg; |
| 6510 | for(i = 0; i<nArg; i++){ |
| 6511 | apArg[i] = &pArgc[i+1]; |
| 6512 | } |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 6513 | rc = pModule->xFilter(pVCur, iQuery, pOp->p4.z, nArg, apArg); |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 6514 | sqlite3VtabImportErrmsg(p, pVtab); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 6515 | if( rc ) goto abort_due_to_error; |
| 6516 | res = pModule->xEof(pVCur); |
drh | 1d454a3 | 2008-01-31 19:34:51 +0000 | [diff] [blame] | 6517 | pCur->nullRow = 0; |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 6518 | VdbeBranchTaken(res!=0,2); |
| 6519 | if( res ) goto jump_to_p2; |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 6520 | break; |
| 6521 | } |
| 6522 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 6523 | |
| 6524 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 6525 | /* Opcode: VColumn P1 P2 P3 * * |
drh | 81316f8 | 2013-10-29 20:40:47 +0000 | [diff] [blame] | 6526 | ** Synopsis: r[P3]=vcolumn(P2) |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 6527 | ** |
drh | 2133d82 | 2008-01-03 18:44:59 +0000 | [diff] [blame] | 6528 | ** Store the value of the P2-th column of |
| 6529 | ** the row of the virtual-table that the |
| 6530 | ** P1 cursor is pointing to into register P3. |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 6531 | */ |
| 6532 | case OP_VColumn: { |
danielk1977 | 3e3a84d | 2008-08-01 17:37:40 +0000 | [diff] [blame] | 6533 | sqlite3_vtab *pVtab; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 6534 | const sqlite3_module *pModule; |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 6535 | Mem *pDest; |
| 6536 | sqlite3_context sContext; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 6537 | |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 6538 | VdbeCursor *pCur = p->apCsr[pOp->p1]; |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 6539 | assert( pCur->eCurType==CURTYPE_VTAB ); |
drh | 9f6168b | 2016-03-19 23:32:58 +0000 | [diff] [blame] | 6540 | assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) ); |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 6541 | pDest = &aMem[pOp->p3]; |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 6542 | memAboutToChange(p, pDest); |
drh | 2945b4a | 2008-01-31 15:53:45 +0000 | [diff] [blame] | 6543 | if( pCur->nullRow ){ |
| 6544 | sqlite3VdbeMemSetNull(pDest); |
| 6545 | break; |
| 6546 | } |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 6547 | pVtab = pCur->uc.pVCur->pVtab; |
danielk1977 | 3e3a84d | 2008-08-01 17:37:40 +0000 | [diff] [blame] | 6548 | pModule = pVtab->pModule; |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 6549 | assert( pModule->xColumn ); |
| 6550 | memset(&sContext, 0, sizeof(sContext)); |
drh | 9bd038f | 2014-08-27 14:14:06 +0000 | [diff] [blame] | 6551 | sContext.pOut = pDest; |
| 6552 | MemSetTypeFlag(pDest, MEM_Null); |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 6553 | rc = pModule->xColumn(pCur->uc.pVCur, &sContext, pOp->p2); |
dan | 016f781 | 2013-08-21 17:35:48 +0000 | [diff] [blame] | 6554 | sqlite3VtabImportErrmsg(p, pVtab); |
drh | 4c8555f | 2009-06-25 01:47:11 +0000 | [diff] [blame] | 6555 | if( sContext.isError ){ |
| 6556 | rc = sContext.isError; |
| 6557 | } |
drh | 9bd038f | 2014-08-27 14:14:06 +0000 | [diff] [blame] | 6558 | sqlite3VdbeChangeEncoding(pDest, encoding); |
drh | 5ff4437 | 2009-11-24 16:26:17 +0000 | [diff] [blame] | 6559 | REGISTER_TRACE(pOp->p3, pDest); |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 6560 | UPDATE_MAX_BLOBSIZE(pDest); |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 6561 | |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 6562 | if( sqlite3VdbeMemTooBig(pDest) ){ |
| 6563 | goto too_big; |
| 6564 | } |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 6565 | if( rc ) goto abort_due_to_error; |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 6566 | break; |
| 6567 | } |
| 6568 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 6569 | |
| 6570 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 6571 | /* Opcode: VNext P1 P2 * * * |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 6572 | ** |
| 6573 | ** Advance virtual table P1 to the next row in its result set and |
| 6574 | ** jump to instruction P2. Or, if the virtual table has reached |
| 6575 | ** the end of its result set, then fall through to the next instruction. |
| 6576 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 6577 | case OP_VNext: { /* jump */ |
danielk1977 | 3e3a84d | 2008-08-01 17:37:40 +0000 | [diff] [blame] | 6578 | sqlite3_vtab *pVtab; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 6579 | const sqlite3_module *pModule; |
drh | c54a617 | 2009-06-02 16:06:03 +0000 | [diff] [blame] | 6580 | int res; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 6581 | VdbeCursor *pCur; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 6582 | |
drh | c54a617 | 2009-06-02 16:06:03 +0000 | [diff] [blame] | 6583 | res = 0; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 6584 | pCur = p->apCsr[pOp->p1]; |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 6585 | assert( pCur->eCurType==CURTYPE_VTAB ); |
drh | 2945b4a | 2008-01-31 15:53:45 +0000 | [diff] [blame] | 6586 | if( pCur->nullRow ){ |
| 6587 | break; |
| 6588 | } |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 6589 | pVtab = pCur->uc.pVCur->pVtab; |
danielk1977 | 3e3a84d | 2008-08-01 17:37:40 +0000 | [diff] [blame] | 6590 | pModule = pVtab->pModule; |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 6591 | assert( pModule->xNext ); |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 6592 | |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 6593 | /* Invoke the xNext() method of the module. There is no way for the |
| 6594 | ** underlying implementation to return an error if one occurs during |
| 6595 | ** xNext(). Instead, if an error occurs, true is returned (indicating that |
| 6596 | ** data is available) and the error code returned when xColumn or |
| 6597 | ** some other method is next invoked on the save virtual table cursor. |
| 6598 | */ |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 6599 | rc = pModule->xNext(pCur->uc.pVCur); |
dan | 016f781 | 2013-08-21 17:35:48 +0000 | [diff] [blame] | 6600 | sqlite3VtabImportErrmsg(p, pVtab); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 6601 | if( rc ) goto abort_due_to_error; |
| 6602 | res = pModule->xEof(pCur->uc.pVCur); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 6603 | VdbeBranchTaken(!res,2); |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 6604 | if( !res ){ |
| 6605 | /* If there is data, jump to P2 */ |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 6606 | goto jump_to_p2_and_check_for_interrupt; |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 6607 | } |
drh | 49afe3a | 2013-07-10 03:05:14 +0000 | [diff] [blame] | 6608 | goto check_for_interrupt; |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 6609 | } |
| 6610 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 6611 | |
danielk1977 | 182c4ba | 2007-06-27 15:53:34 +0000 | [diff] [blame] | 6612 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 6613 | /* Opcode: VRename P1 * * P4 * |
danielk1977 | 182c4ba | 2007-06-27 15:53:34 +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. |
danielk1977 | 182c4ba | 2007-06-27 15:53:34 +0000 | [diff] [blame] | 6616 | ** This opcode invokes the corresponding xRename method. The value |
danielk1977 | 6dbee81 | 2008-01-03 18:39:41 +0000 | [diff] [blame] | 6617 | ** in register P1 is passed as the zName argument to the xRename method. |
danielk1977 | 182c4ba | 2007-06-27 15:53:34 +0000 | [diff] [blame] | 6618 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 6619 | case OP_VRename: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 6620 | sqlite3_vtab *pVtab; |
| 6621 | Mem *pName; |
| 6622 | |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 6623 | pVtab = pOp->p4.pVtab->pVtab; |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 6624 | pName = &aMem[pOp->p1]; |
danielk1977 | 182c4ba | 2007-06-27 15:53:34 +0000 | [diff] [blame] | 6625 | assert( pVtab->pModule->xRename ); |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 6626 | assert( memIsValid(pName) ); |
drh | 9e92a47 | 2013-06-27 17:40:30 +0000 | [diff] [blame] | 6627 | assert( p->readOnly==0 ); |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 6628 | REGISTER_TRACE(pOp->p1, pName); |
drh | 35f6b93 | 2009-06-23 14:15:04 +0000 | [diff] [blame] | 6629 | assert( pName->flags & MEM_Str ); |
drh | 98655a6 | 2011-10-18 22:07:47 +0000 | [diff] [blame] | 6630 | testcase( pName->enc==SQLITE_UTF8 ); |
| 6631 | testcase( pName->enc==SQLITE_UTF16BE ); |
| 6632 | testcase( pName->enc==SQLITE_UTF16LE ); |
| 6633 | rc = sqlite3VdbeChangeEncoding(pName, SQLITE_UTF8); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 6634 | if( rc ) goto abort_due_to_error; |
| 6635 | rc = pVtab->pModule->xRename(pVtab, pName->z); |
| 6636 | sqlite3VtabImportErrmsg(p, pVtab); |
| 6637 | p->expired = 0; |
| 6638 | if( rc ) goto abort_due_to_error; |
danielk1977 | 182c4ba | 2007-06-27 15:53:34 +0000 | [diff] [blame] | 6639 | break; |
| 6640 | } |
| 6641 | #endif |
drh | 4cbdda9 | 2006-06-14 19:00:20 +0000 | [diff] [blame] | 6642 | |
| 6643 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | 0fd6135 | 2014-02-07 02:29:45 +0000 | [diff] [blame] | 6644 | /* Opcode: VUpdate P1 P2 P3 P4 P5 |
drh | f63552b | 2013-10-30 00:25:03 +0000 | [diff] [blame] | 6645 | ** Synopsis: data=r[P3@P2] |
danielk1977 | 399918f | 2006-06-14 13:03:23 +0000 | [diff] [blame] | 6646 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 6647 | ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure. |
danielk1977 | 399918f | 2006-06-14 13:03:23 +0000 | [diff] [blame] | 6648 | ** This opcode invokes the corresponding xUpdate method. P2 values |
danielk1977 | 2a339ff | 2008-01-03 17:31:44 +0000 | [diff] [blame] | 6649 | ** are contiguous memory cells starting at P3 to pass to the xUpdate |
| 6650 | ** invocation. The value in register (P3+P2-1) corresponds to the |
| 6651 | ** p2th element of the argv array passed to xUpdate. |
drh | 4cbdda9 | 2006-06-14 19:00:20 +0000 | [diff] [blame] | 6652 | ** |
| 6653 | ** The xUpdate method will do a DELETE or an INSERT or both. |
danielk1977 | 2a339ff | 2008-01-03 17:31:44 +0000 | [diff] [blame] | 6654 | ** The argv[0] element (which corresponds to memory cell P3) |
| 6655 | ** is the rowid of a row to delete. If argv[0] is NULL then no |
| 6656 | ** deletion occurs. The argv[1] element is the rowid of the new |
| 6657 | ** row. This can be NULL to have the virtual table select the new |
| 6658 | ** rowid for itself. The subsequent elements in the array are |
| 6659 | ** the values of columns in the new row. |
drh | 4cbdda9 | 2006-06-14 19:00:20 +0000 | [diff] [blame] | 6660 | ** |
| 6661 | ** If P2==1 then no insert is performed. argv[0] is the rowid of |
| 6662 | ** a row to delete. |
danielk1977 | 1f6eec5 | 2006-06-16 06:17:47 +0000 | [diff] [blame] | 6663 | ** |
| 6664 | ** P1 is a boolean flag. If it is set to true and the xUpdate call |
| 6665 | ** is successful, then the value returned by sqlite3_last_insert_rowid() |
| 6666 | ** is set to the value of the rowid for the row just inserted. |
drh | 0fd6135 | 2014-02-07 02:29:45 +0000 | [diff] [blame] | 6667 | ** |
| 6668 | ** P5 is the error actions (OE_Replace, OE_Fail, OE_Ignore, etc) to |
| 6669 | ** apply in the case of a constraint failure on an insert or update. |
danielk1977 | 399918f | 2006-06-14 13:03:23 +0000 | [diff] [blame] | 6670 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 6671 | case OP_VUpdate: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 6672 | sqlite3_vtab *pVtab; |
drh | f496a7d | 2015-03-24 14:05:50 +0000 | [diff] [blame] | 6673 | const sqlite3_module *pModule; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 6674 | int nArg; |
| 6675 | int i; |
| 6676 | sqlite_int64 rowid; |
| 6677 | Mem **apArg; |
| 6678 | Mem *pX; |
| 6679 | |
dan | b061d05 | 2011-04-25 18:49:57 +0000 | [diff] [blame] | 6680 | assert( pOp->p2==1 || pOp->p5==OE_Fail || pOp->p5==OE_Rollback |
| 6681 | || pOp->p5==OE_Abort || pOp->p5==OE_Ignore || pOp->p5==OE_Replace |
| 6682 | ); |
drh | 9e92a47 | 2013-06-27 17:40:30 +0000 | [diff] [blame] | 6683 | assert( p->readOnly==0 ); |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 6684 | pVtab = pOp->p4.pVtab->pVtab; |
drh | f496a7d | 2015-03-24 14:05:50 +0000 | [diff] [blame] | 6685 | if( pVtab==0 || NEVER(pVtab->pModule==0) ){ |
| 6686 | rc = SQLITE_LOCKED; |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 6687 | goto abort_due_to_error; |
drh | f496a7d | 2015-03-24 14:05:50 +0000 | [diff] [blame] | 6688 | } |
| 6689 | pModule = pVtab->pModule; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 6690 | nArg = pOp->p2; |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 6691 | assert( pOp->p4type==P4_VTAB ); |
drh | 35f6b93 | 2009-06-23 14:15:04 +0000 | [diff] [blame] | 6692 | if( ALWAYS(pModule->xUpdate) ){ |
dan | b061d05 | 2011-04-25 18:49:57 +0000 | [diff] [blame] | 6693 | u8 vtabOnConflict = db->vtabOnConflict; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 6694 | apArg = p->apArg; |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 6695 | pX = &aMem[pOp->p3]; |
danielk1977 | 2a339ff | 2008-01-03 17:31:44 +0000 | [diff] [blame] | 6696 | for(i=0; i<nArg; i++){ |
drh | 2b4ded9 | 2010-09-27 21:09:31 +0000 | [diff] [blame] | 6697 | assert( memIsValid(pX) ); |
| 6698 | memAboutToChange(p, pX); |
drh | 9c41938 | 2006-06-16 21:13:21 +0000 | [diff] [blame] | 6699 | apArg[i] = pX; |
danielk1977 | 2a339ff | 2008-01-03 17:31:44 +0000 | [diff] [blame] | 6700 | pX++; |
danielk1977 | 399918f | 2006-06-14 13:03:23 +0000 | [diff] [blame] | 6701 | } |
dan | b061d05 | 2011-04-25 18:49:57 +0000 | [diff] [blame] | 6702 | db->vtabOnConflict = pOp->p5; |
danielk1977 | 1f6eec5 | 2006-06-16 06:17:47 +0000 | [diff] [blame] | 6703 | rc = pModule->xUpdate(pVtab, nArg, apArg, &rowid); |
dan | b061d05 | 2011-04-25 18:49:57 +0000 | [diff] [blame] | 6704 | db->vtabOnConflict = vtabOnConflict; |
dan | 016f781 | 2013-08-21 17:35:48 +0000 | [diff] [blame] | 6705 | sqlite3VtabImportErrmsg(p, pVtab); |
drh | 35f6b93 | 2009-06-23 14:15:04 +0000 | [diff] [blame] | 6706 | if( rc==SQLITE_OK && pOp->p1 ){ |
danielk1977 | 1f6eec5 | 2006-06-16 06:17:47 +0000 | [diff] [blame] | 6707 | assert( nArg>1 && apArg[0] && (apArg[0]->flags&MEM_Null) ); |
drh | 99a6692 | 2011-05-13 18:51:42 +0000 | [diff] [blame] | 6708 | db->lastRowid = lastRowid = rowid; |
danielk1977 | 1f6eec5 | 2006-06-16 06:17:47 +0000 | [diff] [blame] | 6709 | } |
drh | d91c1a1 | 2013-02-09 13:58:25 +0000 | [diff] [blame] | 6710 | if( (rc&0xff)==SQLITE_CONSTRAINT && pOp->p4.pVtab->bConstraint ){ |
dan | b061d05 | 2011-04-25 18:49:57 +0000 | [diff] [blame] | 6711 | if( pOp->p5==OE_Ignore ){ |
| 6712 | rc = SQLITE_OK; |
| 6713 | }else{ |
| 6714 | p->errorAction = ((pOp->p5==OE_Replace) ? OE_Abort : pOp->p5); |
| 6715 | } |
| 6716 | }else{ |
| 6717 | p->nChange++; |
| 6718 | } |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 6719 | if( rc ) goto abort_due_to_error; |
danielk1977 | 399918f | 2006-06-14 13:03:23 +0000 | [diff] [blame] | 6720 | } |
drh | 4cbdda9 | 2006-06-14 19:00:20 +0000 | [diff] [blame] | 6721 | break; |
danielk1977 | 399918f | 2006-06-14 13:03:23 +0000 | [diff] [blame] | 6722 | } |
| 6723 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 6724 | |
danielk1977 | 59a9379 | 2008-05-15 17:48:20 +0000 | [diff] [blame] | 6725 | #ifndef SQLITE_OMIT_PAGER_PRAGMAS |
| 6726 | /* Opcode: Pagecount P1 P2 * * * |
| 6727 | ** |
| 6728 | ** Write the current number of pages in database P1 to memory cell P2. |
| 6729 | */ |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 6730 | case OP_Pagecount: { /* out2 */ |
| 6731 | pOut = out2Prerelease(p, pOp); |
drh | b129915 | 2010-03-30 22:58:33 +0000 | [diff] [blame] | 6732 | pOut->u.i = sqlite3BtreeLastPage(db->aDb[pOp->p1].pBt); |
danielk1977 | 59a9379 | 2008-05-15 17:48:20 +0000 | [diff] [blame] | 6733 | break; |
| 6734 | } |
| 6735 | #endif |
| 6736 | |
drh | 60ac3f4 | 2010-11-23 18:59:27 +0000 | [diff] [blame] | 6737 | |
| 6738 | #ifndef SQLITE_OMIT_PAGER_PRAGMAS |
| 6739 | /* Opcode: MaxPgcnt P1 P2 P3 * * |
| 6740 | ** |
| 6741 | ** 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] | 6742 | ** Do not let the maximum page count fall below the current page count and |
| 6743 | ** do not change the maximum page count value if P3==0. |
| 6744 | ** |
drh | 60ac3f4 | 2010-11-23 18:59:27 +0000 | [diff] [blame] | 6745 | ** Store the maximum page count after the change in register P2. |
| 6746 | */ |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 6747 | case OP_MaxPgcnt: { /* out2 */ |
drh | c84e033 | 2010-11-23 20:25:08 +0000 | [diff] [blame] | 6748 | unsigned int newMax; |
drh | 60ac3f4 | 2010-11-23 18:59:27 +0000 | [diff] [blame] | 6749 | Btree *pBt; |
| 6750 | |
drh | 27a348c | 2015-04-13 19:14:06 +0000 | [diff] [blame] | 6751 | pOut = out2Prerelease(p, pOp); |
drh | 60ac3f4 | 2010-11-23 18:59:27 +0000 | [diff] [blame] | 6752 | pBt = db->aDb[pOp->p1].pBt; |
drh | c84e033 | 2010-11-23 20:25:08 +0000 | [diff] [blame] | 6753 | newMax = 0; |
| 6754 | if( pOp->p3 ){ |
| 6755 | newMax = sqlite3BtreeLastPage(pBt); |
drh | 6ea28d6 | 2010-11-26 16:49:59 +0000 | [diff] [blame] | 6756 | if( newMax < (unsigned)pOp->p3 ) newMax = (unsigned)pOp->p3; |
drh | c84e033 | 2010-11-23 20:25:08 +0000 | [diff] [blame] | 6757 | } |
| 6758 | pOut->u.i = sqlite3BtreeMaxPageCount(pBt, newMax); |
drh | 60ac3f4 | 2010-11-23 18:59:27 +0000 | [diff] [blame] | 6759 | break; |
| 6760 | } |
| 6761 | #endif |
| 6762 | |
| 6763 | |
drh | aceb31b | 2014-02-08 01:40:27 +0000 | [diff] [blame] | 6764 | /* Opcode: Init * P2 * P4 * |
| 6765 | ** Synopsis: Start at P2 |
| 6766 | ** |
| 6767 | ** Programs contain a single instance of this opcode as the very first |
| 6768 | ** opcode. |
drh | 949f9cd | 2008-01-12 21:35:57 +0000 | [diff] [blame] | 6769 | ** |
| 6770 | ** If tracing is enabled (by the sqlite3_trace()) interface, then |
| 6771 | ** the UTF-8 string contained in P4 is emitted on the trace callback. |
drh | aceb31b | 2014-02-08 01:40:27 +0000 | [diff] [blame] | 6772 | ** Or if P4 is blank, use the string returned by sqlite3_sql(). |
| 6773 | ** |
| 6774 | ** If P2 is not zero, jump to instruction P2. |
drh | 949f9cd | 2008-01-12 21:35:57 +0000 | [diff] [blame] | 6775 | */ |
drh | aceb31b | 2014-02-08 01:40:27 +0000 | [diff] [blame] | 6776 | case OP_Init: { /* jump */ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 6777 | char *zTrace; |
drh | c3f1d5f | 2011-05-30 23:42:16 +0000 | [diff] [blame] | 6778 | char *z; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 6779 | |
drh | aceb31b | 2014-02-08 01:40:27 +0000 | [diff] [blame] | 6780 | #ifndef SQLITE_OMIT_TRACE |
drh | 37f58e9 | 2012-09-04 21:34:26 +0000 | [diff] [blame] | 6781 | if( db->xTrace |
| 6782 | && !p->doingRerun |
| 6783 | && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0 |
| 6784 | ){ |
drh | c3f1d5f | 2011-05-30 23:42:16 +0000 | [diff] [blame] | 6785 | z = sqlite3VdbeExpandSql(p, zTrace); |
| 6786 | db->xTrace(db->pTraceArg, z); |
| 6787 | sqlite3DbFree(db, z); |
drh | 949f9cd | 2008-01-12 21:35:57 +0000 | [diff] [blame] | 6788 | } |
drh | 8f8b231 | 2013-10-18 20:03:43 +0000 | [diff] [blame] | 6789 | #ifdef SQLITE_USE_FCNTL_TRACE |
| 6790 | zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql); |
| 6791 | if( zTrace ){ |
| 6792 | int i; |
| 6793 | for(i=0; i<db->nDb; i++){ |
drh | a7ab6d8 | 2014-07-21 15:44:39 +0000 | [diff] [blame] | 6794 | if( DbMaskTest(p->btreeMask, i)==0 ) continue; |
drh | 8f8b231 | 2013-10-18 20:03:43 +0000 | [diff] [blame] | 6795 | sqlite3_file_control(db, db->aDb[i].zName, SQLITE_FCNTL_TRACE, zTrace); |
| 6796 | } |
| 6797 | } |
| 6798 | #endif /* SQLITE_USE_FCNTL_TRACE */ |
drh | c3f1d5f | 2011-05-30 23:42:16 +0000 | [diff] [blame] | 6799 | #ifdef SQLITE_DEBUG |
| 6800 | if( (db->flags & SQLITE_SqlTrace)!=0 |
| 6801 | && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0 |
| 6802 | ){ |
| 6803 | sqlite3DebugPrintf("SQL-trace: %s\n", zTrace); |
| 6804 | } |
| 6805 | #endif /* SQLITE_DEBUG */ |
drh | aceb31b | 2014-02-08 01:40:27 +0000 | [diff] [blame] | 6806 | #endif /* SQLITE_OMIT_TRACE */ |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 6807 | if( pOp->p2 ) goto jump_to_p2; |
drh | 949f9cd | 2008-01-12 21:35:57 +0000 | [diff] [blame] | 6808 | break; |
| 6809 | } |
drh | 949f9cd | 2008-01-12 21:35:57 +0000 | [diff] [blame] | 6810 | |
drh | 2893536 | 2013-12-07 20:39:19 +0000 | [diff] [blame] | 6811 | #ifdef SQLITE_ENABLE_CURSOR_HINTS |
drh | 0df5701 | 2015-08-14 15:05:55 +0000 | [diff] [blame] | 6812 | /* Opcode: CursorHint P1 * * P4 * |
drh | 2893536 | 2013-12-07 20:39:19 +0000 | [diff] [blame] | 6813 | ** |
| 6814 | ** 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] | 6815 | ** satisfy the Expr in P4. TK_REGISTER terms in the P4 expression refer |
| 6816 | ** to values currently held in registers. TK_COLUMN terms in the P4 |
| 6817 | ** 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] | 6818 | */ |
| 6819 | case OP_CursorHint: { |
| 6820 | VdbeCursor *pC; |
| 6821 | |
| 6822 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 6823 | assert( pOp->p4type==P4_EXPR ); |
| 6824 | pC = p->apCsr[pOp->p1]; |
dan | 91d3a61 | 2014-07-15 11:59:44 +0000 | [diff] [blame] | 6825 | if( pC ){ |
drh | c960dcb | 2015-11-20 19:22:01 +0000 | [diff] [blame] | 6826 | assert( pC->eCurType==CURTYPE_BTREE ); |
drh | 62aaa6c | 2015-11-21 17:27:42 +0000 | [diff] [blame] | 6827 | sqlite3BtreeCursorHint(pC->uc.pCursor, BTREE_HINT_RANGE, |
| 6828 | pOp->p4.pExpr, aMem); |
dan | 91d3a61 | 2014-07-15 11:59:44 +0000 | [diff] [blame] | 6829 | } |
drh | 2893536 | 2013-12-07 20:39:19 +0000 | [diff] [blame] | 6830 | break; |
| 6831 | } |
| 6832 | #endif /* SQLITE_ENABLE_CURSOR_HINTS */ |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 6833 | |
| 6834 | /* Opcode: Noop * * * * * |
| 6835 | ** |
| 6836 | ** Do nothing. This instruction is often useful as a jump |
| 6837 | ** destination. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 6838 | */ |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 6839 | /* |
| 6840 | ** The magic Explain opcode are only inserted when explain==2 (which |
| 6841 | ** is to say when the EXPLAIN QUERY PLAN syntax is used.) |
| 6842 | ** This opcode records information from the optimizer. It is the |
| 6843 | ** the same as a no-op. This opcodesnever appears in a real VM program. |
| 6844 | */ |
| 6845 | default: { /* This is really OP_Noop and OP_Explain */ |
drh | 13573c7 | 2010-01-12 17:04:07 +0000 | [diff] [blame] | 6846 | assert( pOp->opcode==OP_Noop || pOp->opcode==OP_Explain ); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 6847 | break; |
| 6848 | } |
| 6849 | |
| 6850 | /***************************************************************************** |
| 6851 | ** The cases of the switch statement above this line should all be indented |
| 6852 | ** by 6 spaces. But the left-most 6 spaces have been removed to improve the |
| 6853 | ** readability. From this point on down, the normal indentation rules are |
| 6854 | ** restored. |
| 6855 | *****************************************************************************/ |
| 6856 | } |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 6857 | |
drh | 7b39686 | 2003-01-01 23:06:20 +0000 | [diff] [blame] | 6858 | #ifdef VDBE_PROFILE |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 6859 | { |
drh | a01c7c7 | 2014-04-25 12:35:31 +0000 | [diff] [blame] | 6860 | u64 endTime = sqlite3Hwtime(); |
drh | 6dc4148 | 2015-04-16 17:31:02 +0000 | [diff] [blame] | 6861 | if( endTime>start ) pOrigOp->cycles += endTime - start; |
| 6862 | pOrigOp->cnt++; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 6863 | } |
drh | 7b39686 | 2003-01-01 23:06:20 +0000 | [diff] [blame] | 6864 | #endif |
| 6865 | |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 6866 | /* The following code adds nothing to the actual functionality |
| 6867 | ** of the program. It is only here for testing and debugging. |
| 6868 | ** On the other hand, it does burn CPU cycles every time through |
| 6869 | ** the evaluator loop. So we can leave it out when NDEBUG is defined. |
| 6870 | */ |
| 6871 | #ifndef NDEBUG |
drh | 6dc4148 | 2015-04-16 17:31:02 +0000 | [diff] [blame] | 6872 | assert( pOp>=&aOp[-1] && pOp<&aOp[p->nOp-1] ); |
drh | ae7e151 | 2007-05-02 16:51:59 +0000 | [diff] [blame] | 6873 | |
drh | cf1023c | 2007-05-08 20:59:49 +0000 | [diff] [blame] | 6874 | #ifdef SQLITE_DEBUG |
drh | 84e55a8 | 2013-11-13 17:58:23 +0000 | [diff] [blame] | 6875 | if( db->flags & SQLITE_VdbeTrace ){ |
| 6876 | if( rc!=0 ) printf("rc=%d\n",rc); |
drh | 6dc4148 | 2015-04-16 17:31:02 +0000 | [diff] [blame] | 6877 | if( pOrigOp->opflags & (OPFLG_OUT2) ){ |
| 6878 | registerTrace(pOrigOp->p2, &aMem[pOrigOp->p2]); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6879 | } |
drh | 6dc4148 | 2015-04-16 17:31:02 +0000 | [diff] [blame] | 6880 | if( pOrigOp->opflags & OPFLG_OUT3 ){ |
| 6881 | registerTrace(pOrigOp->p3, &aMem[pOrigOp->p3]); |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 6882 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6883 | } |
danielk1977 | b5402fb | 2005-01-12 07:15:04 +0000 | [diff] [blame] | 6884 | #endif /* SQLITE_DEBUG */ |
| 6885 | #endif /* NDEBUG */ |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 6886 | } /* The end of the for(;;) loop the loops through opcodes */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6887 | |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 6888 | /* If we reach this point, it means that execution is finished with |
| 6889 | ** an error of some kind. |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 6890 | */ |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 6891 | abort_due_to_error: |
| 6892 | if( db->mallocFailed ) rc = SQLITE_NOMEM_BKPT; |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 6893 | assert( rc ); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 6894 | if( p->zErrMsg==0 && rc!=SQLITE_IOERR_NOMEM ){ |
| 6895 | sqlite3VdbeError(p, "%s", sqlite3ErrStr(rc)); |
| 6896 | } |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 6897 | p->rc = rc; |
drh | f68521c | 2016-03-21 12:28:02 +0000 | [diff] [blame] | 6898 | sqlite3SystemError(db, rc); |
drh | a64fa91 | 2010-03-04 00:53:32 +0000 | [diff] [blame] | 6899 | testcase( sqlite3GlobalConfig.xLog!=0 ); |
| 6900 | sqlite3_log(rc, "statement aborts at %d: [%s] %s", |
drh | f56fa46 | 2015-04-13 21:39:54 +0000 | [diff] [blame] | 6901 | (int)(pOp - aOp), p->zSql, p->zErrMsg); |
drh | 92f02c3 | 2004-09-02 14:57:08 +0000 | [diff] [blame] | 6902 | sqlite3VdbeHalt(p); |
drh | 4a642b6 | 2016-02-05 01:55:27 +0000 | [diff] [blame] | 6903 | if( rc==SQLITE_IOERR_NOMEM ) sqlite3OomFault(db); |
danielk1977 | 7eaabcd | 2008-07-07 14:56:56 +0000 | [diff] [blame] | 6904 | rc = SQLITE_ERROR; |
drh | cdf011d | 2011-04-04 21:25:28 +0000 | [diff] [blame] | 6905 | if( resetSchemaOnFault>0 ){ |
drh | 81028a4 | 2012-05-15 18:28:27 +0000 | [diff] [blame] | 6906 | sqlite3ResetOneSchema(db, resetSchemaOnFault-1); |
drh | bdaec52 | 2011-04-04 00:14:43 +0000 | [diff] [blame] | 6907 | } |
drh | 900b31e | 2007-08-28 02:27:51 +0000 | [diff] [blame] | 6908 | |
| 6909 | /* This is the only way out of this procedure. We have to |
| 6910 | ** release the mutexes on btrees that were acquired at the |
| 6911 | ** top. */ |
| 6912 | vdbe_return: |
drh | 99a6692 | 2011-05-13 18:51:42 +0000 | [diff] [blame] | 6913 | db->lastRowid = lastRowid; |
drh | 77dfd5b | 2013-08-19 11:15:48 +0000 | [diff] [blame] | 6914 | testcase( nVmStep>0 ); |
drh | 9b47ee3 | 2013-08-20 03:13:51 +0000 | [diff] [blame] | 6915 | p->aCounter[SQLITE_STMTSTATUS_VM_STEP] += (int)nVmStep; |
drh | bdaec52 | 2011-04-04 00:14:43 +0000 | [diff] [blame] | 6916 | sqlite3VdbeLeave(p); |
dan | 83f0ab8 | 2016-01-29 18:04:31 +0000 | [diff] [blame] | 6917 | assert( rc!=SQLITE_OK || nExtraDelete==0 |
| 6918 | || sqlite3_strlike("DELETE%",p->zSql,0)!=0 |
| 6919 | ); |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 6920 | return rc; |
| 6921 | |
drh | 023ae03 | 2007-05-08 12:12:16 +0000 | [diff] [blame] | 6922 | /* Jump to here if a string or blob larger than SQLITE_MAX_LENGTH |
| 6923 | ** is encountered. |
| 6924 | */ |
| 6925 | too_big: |
drh | 22c17b8 | 2015-05-15 04:13:15 +0000 | [diff] [blame] | 6926 | sqlite3VdbeError(p, "string or blob too big"); |
drh | 023ae03 | 2007-05-08 12:12:16 +0000 | [diff] [blame] | 6927 | rc = SQLITE_TOOBIG; |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 6928 | goto abort_due_to_error; |
drh | 023ae03 | 2007-05-08 12:12:16 +0000 | [diff] [blame] | 6929 | |
drh | 98640a3 | 2007-06-07 19:08:32 +0000 | [diff] [blame] | 6930 | /* Jump to here if a malloc() fails. |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 6931 | */ |
| 6932 | no_mem: |
drh | 4a642b6 | 2016-02-05 01:55:27 +0000 | [diff] [blame] | 6933 | sqlite3OomFault(db); |
drh | 22c17b8 | 2015-05-15 04:13:15 +0000 | [diff] [blame] | 6934 | sqlite3VdbeError(p, "out of memory"); |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 6935 | rc = SQLITE_NOMEM_BKPT; |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 6936 | goto abort_due_to_error; |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 6937 | |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 6938 | /* Jump to here if the sqlite3_interrupt() API sets the interrupt |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 6939 | ** flag. |
| 6940 | */ |
| 6941 | abort_due_to_interrupt: |
drh | 881feaa | 2006-07-26 01:39:30 +0000 | [diff] [blame] | 6942 | assert( db->u1.isInterrupted ); |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 6943 | rc = db->mallocFailed ? SQLITE_NOMEM_BKPT : SQLITE_INTERRUPT; |
danielk1977 | 026d270 | 2004-06-14 13:14:59 +0000 | [diff] [blame] | 6944 | p->rc = rc; |
drh | 22c17b8 | 2015-05-15 04:13:15 +0000 | [diff] [blame] | 6945 | sqlite3VdbeError(p, "%s", sqlite3ErrStr(rc)); |
drh | 9467abf | 2016-02-17 18:44:11 +0000 | [diff] [blame] | 6946 | goto abort_due_to_error; |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 6947 | } |