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 | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 12 | ** The code in this file implements execution method of the |
| 13 | ** Virtual Database Engine (VDBE). A separate file ("vdbeaux.c") |
| 14 | ** handles housekeeping details such as creating and deleting |
| 15 | ** VDBE instances. This file is solely interested in executing |
| 16 | ** the VDBE program. |
| 17 | ** |
danielk1977 | fc57d7b | 2004-05-26 02:04:57 +0000 | [diff] [blame] | 18 | ** In the external interface, an "sqlite3_stmt*" is an opaque pointer |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 19 | ** to a VDBE. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 20 | ** |
| 21 | ** The SQL parser generates a program which is then executed by |
| 22 | ** the VDBE to do the work of the SQL statement. VDBE programs are |
| 23 | ** similar in form to assembly language. The program consists of |
| 24 | ** a linear sequence of operations. Each operation has an opcode |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 25 | ** and 5 operands. Operands P1, P2, and P3 are integers. Operand P4 |
| 26 | ** is a null-terminated string. Operand P5 is an unsigned character. |
| 27 | ** Few opcodes use all 5 operands. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 28 | ** |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 29 | ** Computation results are stored on a set of registers numbered beginning |
| 30 | ** with 1 and going up to Vdbe.nMem. Each register can store |
| 31 | ** either an integer, a null-terminated string, a floating point |
shane | 21e7feb | 2008-05-30 15:59:49 +0000 | [diff] [blame] | 32 | ** number, or the SQL "NULL" value. An implicit conversion from one |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 33 | ** type to the other occurs as necessary. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 34 | ** |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 35 | ** Most of the code in this file is taken up by the sqlite3VdbeExec() |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 36 | ** function which does the work of interpreting a VDBE program. |
| 37 | ** But other routines are also provided to help in building up |
| 38 | ** a program instruction by instruction. |
| 39 | ** |
drh | ac82fcf | 2002-09-08 17:23:41 +0000 | [diff] [blame] | 40 | ** Various scripts scan this source file in order to generate HTML |
| 41 | ** documentation, headers files, or other derived files. The formatting |
| 42 | ** of the code in this file is, therefore, important. See other comments |
| 43 | ** in this file for details. If in doubt, do not deviate from existing |
| 44 | ** commenting and indentation practices when changing or adding code. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 45 | */ |
| 46 | #include "sqliteInt.h" |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 47 | #include "vdbeInt.h" |
drh | 8f619cc | 2002-09-08 00:04:50 +0000 | [diff] [blame] | 48 | |
| 49 | /* |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 50 | ** The following global variable is incremented every time a cursor |
drh | 959403f | 2008-12-12 17:56:16 +0000 | [diff] [blame] | 51 | ** 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] | 52 | ** procedures use this information to make sure that indices are |
drh | ac82fcf | 2002-09-08 17:23:41 +0000 | [diff] [blame] | 53 | ** working correctly. This variable has no function other than to |
| 54 | ** help verify the correct operation of the library. |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 55 | */ |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 56 | #ifdef SQLITE_TEST |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 57 | int sqlite3_search_count = 0; |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 58 | #endif |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 59 | |
drh | f603871 | 2004-02-08 18:07:34 +0000 | [diff] [blame] | 60 | /* |
| 61 | ** When this global variable is positive, it gets decremented once before |
drh | 881feaa | 2006-07-26 01:39:30 +0000 | [diff] [blame] | 62 | ** each instruction in the VDBE. When reaches zero, the u1.isInterrupted |
| 63 | ** field of the sqlite3 structure is set in order to simulate and interrupt. |
drh | f603871 | 2004-02-08 18:07:34 +0000 | [diff] [blame] | 64 | ** |
| 65 | ** This facility is used for testing purposes only. It does not function |
| 66 | ** in an ordinary build. |
| 67 | */ |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 68 | #ifdef SQLITE_TEST |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 69 | int sqlite3_interrupt_count = 0; |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 70 | #endif |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 71 | |
danielk1977 | 7e18c25 | 2004-05-25 11:47:24 +0000 | [diff] [blame] | 72 | /* |
drh | 6bf8957 | 2004-11-03 16:27:01 +0000 | [diff] [blame] | 73 | ** The next global variable is incremented each type the OP_Sort opcode |
| 74 | ** is executed. The test procedures use this information to make sure that |
shane | 21e7feb | 2008-05-30 15:59:49 +0000 | [diff] [blame] | 75 | ** sorting is occurring or not occurring at appropriate times. This variable |
drh | 6bf8957 | 2004-11-03 16:27:01 +0000 | [diff] [blame] | 76 | ** has no function other than to help verify the correct operation of the |
| 77 | ** library. |
| 78 | */ |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 79 | #ifdef SQLITE_TEST |
drh | 6bf8957 | 2004-11-03 16:27:01 +0000 | [diff] [blame] | 80 | int sqlite3_sort_count = 0; |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 81 | #endif |
drh | 6bf8957 | 2004-11-03 16:27:01 +0000 | [diff] [blame] | 82 | |
| 83 | /* |
drh | ae7e151 | 2007-05-02 16:51:59 +0000 | [diff] [blame] | 84 | ** The next global variable records the size of the largest MEM_Blob |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 85 | ** 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] | 86 | ** use this information to make sure that the zero-blob functionality |
| 87 | ** is working correctly. This variable has no function other than to |
| 88 | ** help verify the correct operation of the library. |
| 89 | */ |
| 90 | #ifdef SQLITE_TEST |
| 91 | int sqlite3_max_blobsize = 0; |
drh | ca48c90 | 2008-01-18 14:08:24 +0000 | [diff] [blame] | 92 | static void updateMaxBlobsize(Mem *p){ |
| 93 | if( (p->flags & (MEM_Str|MEM_Blob))!=0 && p->n>sqlite3_max_blobsize ){ |
| 94 | sqlite3_max_blobsize = p->n; |
| 95 | } |
| 96 | } |
drh | ae7e151 | 2007-05-02 16:51:59 +0000 | [diff] [blame] | 97 | #endif |
| 98 | |
| 99 | /* |
dan | 0ff297e | 2009-09-25 17:03:14 +0000 | [diff] [blame] | 100 | ** The next global variable is incremented each type the OP_Found opcode |
| 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 | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 121 | ** Convert the given register into a string if it isn't one |
danielk1977 | bd7e460 | 2004-05-24 07:34:48 +0000 | [diff] [blame] | 122 | ** already. Return non-zero if a malloc() fails. |
| 123 | */ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 124 | #define Stringify(P, enc) \ |
| 125 | if(((P)->flags&(MEM_Str|MEM_Blob))==0 && sqlite3VdbeMemStringify(P,enc)) \ |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 126 | { goto no_mem; } |
danielk1977 | bd7e460 | 2004-05-24 07:34:48 +0000 | [diff] [blame] | 127 | |
| 128 | /* |
danielk1977 | bd7e460 | 2004-05-24 07:34:48 +0000 | [diff] [blame] | 129 | ** An ephemeral string value (signified by the MEM_Ephem flag) contains |
| 130 | ** a pointer to a dynamically allocated string where some other entity |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 131 | ** is responsible for deallocating that string. Because the register |
| 132 | ** does not control the string, it might be deleted without the register |
| 133 | ** knowing it. |
danielk1977 | bd7e460 | 2004-05-24 07:34:48 +0000 | [diff] [blame] | 134 | ** |
| 135 | ** This routine converts an ephemeral string into a dynamically allocated |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 136 | ** string that the register itself controls. In other words, it |
danielk1977 | bd7e460 | 2004-05-24 07:34:48 +0000 | [diff] [blame] | 137 | ** converts an MEM_Ephem string into an MEM_Dyn string. |
| 138 | */ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 139 | #define Deephemeralize(P) \ |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 140 | if( ((P)->flags&MEM_Ephem)!=0 \ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 141 | && sqlite3VdbeMemMakeWriteable(P) ){ goto no_mem;} |
danielk1977 | 93d4675 | 2004-05-23 13:30:58 +0000 | [diff] [blame] | 142 | |
| 143 | /* |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 144 | ** Call sqlite3VdbeMemExpandBlob() on the supplied value (type Mem*) |
| 145 | ** P if required. |
| 146 | */ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 147 | #define ExpandBlob(P) (((P)->flags&MEM_Zero)?sqlite3VdbeMemExpandBlob(P):0) |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 148 | |
| 149 | /* |
shane | 21e7feb | 2008-05-30 15:59:49 +0000 | [diff] [blame] | 150 | ** Argument pMem points at a register that will be passed to a |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 151 | ** user-defined function or returned to the user as the result of a query. |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 152 | ** This routine sets the pMem->type variable used by the sqlite3_value_*() |
| 153 | ** routines. |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 154 | */ |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 155 | void sqlite3VdbeMemStoreType(Mem *pMem){ |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 156 | int flags = pMem->flags; |
| 157 | if( flags & MEM_Null ){ |
drh | 9c05483 | 2004-05-31 18:51:57 +0000 | [diff] [blame] | 158 | pMem->type = SQLITE_NULL; |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 159 | } |
| 160 | else if( flags & MEM_Int ){ |
drh | 9c05483 | 2004-05-31 18:51:57 +0000 | [diff] [blame] | 161 | pMem->type = SQLITE_INTEGER; |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 162 | } |
| 163 | else if( flags & MEM_Real ){ |
drh | 9c05483 | 2004-05-31 18:51:57 +0000 | [diff] [blame] | 164 | pMem->type = SQLITE_FLOAT; |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 165 | } |
| 166 | else if( flags & MEM_Str ){ |
drh | 9c05483 | 2004-05-31 18:51:57 +0000 | [diff] [blame] | 167 | pMem->type = SQLITE_TEXT; |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 168 | }else{ |
drh | 9c05483 | 2004-05-31 18:51:57 +0000 | [diff] [blame] | 169 | pMem->type = SQLITE_BLOB; |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 170 | } |
| 171 | } |
danielk1977 | 8a6b541 | 2004-05-24 07:04:25 +0000 | [diff] [blame] | 172 | |
| 173 | /* |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 174 | ** Allocate VdbeCursor number iCur. Return a pointer to it. Return NULL |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 175 | ** if we run out of memory. |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 176 | */ |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 177 | static VdbeCursor *allocateCursor( |
| 178 | Vdbe *p, /* The virtual machine */ |
| 179 | int iCur, /* Index of the new VdbeCursor */ |
danielk1977 | d336e22 | 2009-02-20 10:58:41 +0000 | [diff] [blame] | 180 | int nField, /* Number of fields in the table or index */ |
drh | 3d4501e | 2008-12-04 20:40:10 +0000 | [diff] [blame] | 181 | int iDb, /* When database the cursor belongs to, or -1 */ |
drh | 3e9ca09 | 2009-09-08 01:14:48 +0000 | [diff] [blame] | 182 | int isBtreeCursor /* True for B-Tree. False for pseudo-table or vtab */ |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 183 | ){ |
| 184 | /* 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] | 185 | ** required for this VdbeCursor structure. It is convenient to use a |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 186 | ** vdbe memory cell to manage the memory allocation required for a |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 187 | ** VdbeCursor structure for the following reasons: |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 188 | ** |
| 189 | ** * Sometimes cursor numbers are used for a couple of different |
| 190 | ** purposes in a vdbe program. The different uses might require |
| 191 | ** different sized allocations. Memory cells provide growable |
| 192 | ** allocations. |
| 193 | ** |
| 194 | ** * When using ENABLE_MEMORY_MANAGEMENT, memory cell buffers can |
| 195 | ** be freed lazily via the sqlite3_release_memory() API. This |
| 196 | ** minimizes the number of malloc calls made by the system. |
| 197 | ** |
| 198 | ** Memory cells for cursors are allocated at the top of the address |
| 199 | ** space. Memory cell (p->nMem) corresponds to cursor 0. Space for |
| 200 | ** cursor 1 is managed by memory cell (p->nMem-1), etc. |
| 201 | */ |
| 202 | Mem *pMem = &p->aMem[p->nMem-iCur]; |
| 203 | |
danielk1977 | 5f09613 | 2008-03-28 15:44:09 +0000 | [diff] [blame] | 204 | int nByte; |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 205 | VdbeCursor *pCx = 0; |
danielk1977 | 5f09613 | 2008-03-28 15:44:09 +0000 | [diff] [blame] | 206 | nByte = |
drh | c54055b | 2009-11-13 17:05:53 +0000 | [diff] [blame] | 207 | ROUND8(sizeof(VdbeCursor)) + |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 208 | (isBtreeCursor?sqlite3BtreeCursorSize():0) + |
| 209 | 2*nField*sizeof(u32); |
| 210 | |
drh | 290c194 | 2004-08-21 17:54:45 +0000 | [diff] [blame] | 211 | assert( iCur<p->nCursor ); |
| 212 | if( p->apCsr[iCur] ){ |
danielk1977 | be71889 | 2006-06-23 08:05:19 +0000 | [diff] [blame] | 213 | sqlite3VdbeFreeCursor(p, p->apCsr[iCur]); |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 214 | p->apCsr[iCur] = 0; |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 215 | } |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 216 | if( SQLITE_OK==sqlite3VdbeMemGrow(pMem, nByte, 0) ){ |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 217 | p->apCsr[iCur] = pCx = (VdbeCursor*)pMem->z; |
drh | f25a507 | 2009-11-18 23:01:25 +0000 | [diff] [blame] | 218 | memset(pCx, 0, sizeof(VdbeCursor)); |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 +0000 | [diff] [blame] | 219 | pCx->iDb = iDb; |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 220 | pCx->nField = nField; |
| 221 | if( nField ){ |
drh | c54055b | 2009-11-13 17:05:53 +0000 | [diff] [blame] | 222 | pCx->aType = (u32 *)&pMem->z[ROUND8(sizeof(VdbeCursor))]; |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 223 | } |
| 224 | if( isBtreeCursor ){ |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 225 | pCx->pCursor = (BtCursor*) |
drh | c54055b | 2009-11-13 17:05:53 +0000 | [diff] [blame] | 226 | &pMem->z[ROUND8(sizeof(VdbeCursor))+2*nField*sizeof(u32)]; |
drh | f25a507 | 2009-11-18 23:01:25 +0000 | [diff] [blame] | 227 | sqlite3BtreeCursorZero(pCx->pCursor); |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 228 | } |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 +0000 | [diff] [blame] | 229 | } |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 230 | return pCx; |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 231 | } |
| 232 | |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 233 | /* |
drh | 29d7210 | 2006-02-09 22:13:41 +0000 | [diff] [blame] | 234 | ** Try to convert a value into a numeric representation if we can |
| 235 | ** do so without loss of information. In other words, if the string |
| 236 | ** looks like a number, convert it into a number. If it does not |
| 237 | ** look like a number, leave it alone. |
| 238 | */ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 239 | static void applyNumericAffinity(Mem *pRec){ |
drh | 29d7210 | 2006-02-09 22:13:41 +0000 | [diff] [blame] | 240 | if( (pRec->flags & (MEM_Real|MEM_Int))==0 ){ |
| 241 | int realnum; |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 242 | sqlite3VdbeMemNulTerminate(pRec); |
drh | 29d7210 | 2006-02-09 22:13:41 +0000 | [diff] [blame] | 243 | if( (pRec->flags&MEM_Str) |
| 244 | && sqlite3IsNumber(pRec->z, &realnum, pRec->enc) ){ |
| 245 | i64 value; |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 246 | sqlite3VdbeChangeEncoding(pRec, SQLITE_UTF8); |
drh | b6a9ece | 2007-06-26 00:37:27 +0000 | [diff] [blame] | 247 | if( !realnum && sqlite3Atoi64(pRec->z, &value) ){ |
drh | 3c024d6 | 2007-03-30 11:23:45 +0000 | [diff] [blame] | 248 | pRec->u.i = value; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 249 | MemSetTypeFlag(pRec, MEM_Int); |
drh | 29d7210 | 2006-02-09 22:13:41 +0000 | [diff] [blame] | 250 | }else{ |
| 251 | sqlite3VdbeMemRealify(pRec); |
| 252 | } |
| 253 | } |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | /* |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 258 | ** Processing is determine by the affinity parameter: |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 259 | ** |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 260 | ** SQLITE_AFF_INTEGER: |
| 261 | ** SQLITE_AFF_REAL: |
| 262 | ** SQLITE_AFF_NUMERIC: |
| 263 | ** Try to convert pRec to an integer representation or a |
| 264 | ** floating-point representation if an integer representation |
| 265 | ** is not possible. Note that the integer representation is |
| 266 | ** always preferred, even if the affinity is REAL, because |
| 267 | ** an integer representation is more space efficient on disk. |
| 268 | ** |
| 269 | ** SQLITE_AFF_TEXT: |
| 270 | ** Convert pRec to a text representation. |
| 271 | ** |
| 272 | ** SQLITE_AFF_NONE: |
| 273 | ** No-op. pRec is unchanged. |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 274 | */ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 275 | static void applyAffinity( |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 276 | Mem *pRec, /* The value to apply affinity to */ |
| 277 | char affinity, /* The affinity to be applied */ |
| 278 | u8 enc /* Use this text encoding */ |
| 279 | ){ |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 280 | if( affinity==SQLITE_AFF_TEXT ){ |
drh | 17c4029 | 2004-07-21 02:53:29 +0000 | [diff] [blame] | 281 | /* Only attempt the conversion to TEXT if there is an integer or real |
| 282 | ** representation (blob and NULL do not get converted) but no string |
| 283 | ** representation. |
| 284 | */ |
| 285 | if( 0==(pRec->flags&MEM_Str) && (pRec->flags&(MEM_Real|MEM_Int)) ){ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 286 | sqlite3VdbeMemStringify(pRec, enc); |
drh | 17c4029 | 2004-07-21 02:53:29 +0000 | [diff] [blame] | 287 | } |
| 288 | pRec->flags &= ~(MEM_Real|MEM_Int); |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 289 | }else if( affinity!=SQLITE_AFF_NONE ){ |
| 290 | assert( affinity==SQLITE_AFF_INTEGER || affinity==SQLITE_AFF_REAL |
| 291 | || affinity==SQLITE_AFF_NUMERIC ); |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 292 | applyNumericAffinity(pRec); |
drh | 29d7210 | 2006-02-09 22:13:41 +0000 | [diff] [blame] | 293 | if( pRec->flags & MEM_Real ){ |
drh | 8df447f | 2005-11-01 15:48:24 +0000 | [diff] [blame] | 294 | sqlite3VdbeIntegerAffinity(pRec); |
drh | 17c4029 | 2004-07-21 02:53:29 +0000 | [diff] [blame] | 295 | } |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 296 | } |
| 297 | } |
| 298 | |
danielk1977 | aee18ef | 2005-03-09 12:26:50 +0000 | [diff] [blame] | 299 | /* |
drh | 29d7210 | 2006-02-09 22:13:41 +0000 | [diff] [blame] | 300 | ** Try to convert the type of a function argument or a result column |
| 301 | ** into a numeric representation. Use either INTEGER or REAL whichever |
| 302 | ** is appropriate. But only do the conversion if it is possible without |
| 303 | ** loss of information and return the revised type of the argument. |
| 304 | ** |
| 305 | ** This is an EXPERIMENTAL api and is subject to change or removal. |
| 306 | */ |
| 307 | int sqlite3_value_numeric_type(sqlite3_value *pVal){ |
| 308 | Mem *pMem = (Mem*)pVal; |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 309 | applyNumericAffinity(pMem); |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 310 | sqlite3VdbeMemStoreType(pMem); |
drh | 29d7210 | 2006-02-09 22:13:41 +0000 | [diff] [blame] | 311 | return pMem->type; |
| 312 | } |
| 313 | |
| 314 | /* |
danielk1977 | aee18ef | 2005-03-09 12:26:50 +0000 | [diff] [blame] | 315 | ** Exported version of applyAffinity(). This one works on sqlite3_value*, |
| 316 | ** not the internal Mem* type. |
| 317 | */ |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 318 | void sqlite3ValueApplyAffinity( |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 319 | sqlite3_value *pVal, |
| 320 | u8 affinity, |
| 321 | u8 enc |
| 322 | ){ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 323 | applyAffinity((Mem *)pVal, affinity, enc); |
danielk1977 | aee18ef | 2005-03-09 12:26:50 +0000 | [diff] [blame] | 324 | } |
| 325 | |
danielk1977 | b5402fb | 2005-01-12 07:15:04 +0000 | [diff] [blame] | 326 | #ifdef SQLITE_DEBUG |
drh | b6f5452 | 2004-05-20 02:42:16 +0000 | [diff] [blame] | 327 | /* |
danielk1977 | ca6b291 | 2004-05-21 10:49:47 +0000 | [diff] [blame] | 328 | ** Write a nice string representation of the contents of cell pMem |
| 329 | ** into buffer zBuf, length nBuf. |
| 330 | */ |
drh | 7416170 | 2006-02-24 02:53:49 +0000 | [diff] [blame] | 331 | void sqlite3VdbeMemPrettyPrint(Mem *pMem, char *zBuf){ |
danielk1977 | ca6b291 | 2004-05-21 10:49:47 +0000 | [diff] [blame] | 332 | char *zCsr = zBuf; |
| 333 | int f = pMem->flags; |
| 334 | |
drh | 5719628 | 2004-10-06 15:41:16 +0000 | [diff] [blame] | 335 | static const char *const encnames[] = {"(X)", "(8)", "(16LE)", "(16BE)"}; |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame] | 336 | |
danielk1977 | ca6b291 | 2004-05-21 10:49:47 +0000 | [diff] [blame] | 337 | if( f&MEM_Blob ){ |
| 338 | int i; |
| 339 | char c; |
| 340 | if( f & MEM_Dyn ){ |
| 341 | c = 'z'; |
| 342 | assert( (f & (MEM_Static|MEM_Ephem))==0 ); |
| 343 | }else if( f & MEM_Static ){ |
| 344 | c = 't'; |
| 345 | assert( (f & (MEM_Dyn|MEM_Ephem))==0 ); |
| 346 | }else if( f & MEM_Ephem ){ |
| 347 | c = 'e'; |
| 348 | assert( (f & (MEM_Static|MEM_Dyn))==0 ); |
| 349 | }else{ |
| 350 | c = 's'; |
| 351 | } |
| 352 | |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 353 | sqlite3_snprintf(100, zCsr, "%c", c); |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 354 | zCsr += sqlite3Strlen30(zCsr); |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 355 | sqlite3_snprintf(100, zCsr, "%d[", pMem->n); |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 356 | zCsr += sqlite3Strlen30(zCsr); |
danielk1977 | ca6b291 | 2004-05-21 10:49:47 +0000 | [diff] [blame] | 357 | for(i=0; i<16 && i<pMem->n; i++){ |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 358 | sqlite3_snprintf(100, zCsr, "%02X", ((int)pMem->z[i] & 0xFF)); |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 359 | zCsr += sqlite3Strlen30(zCsr); |
danielk1977 | ca6b291 | 2004-05-21 10:49:47 +0000 | [diff] [blame] | 360 | } |
| 361 | for(i=0; i<16 && i<pMem->n; i++){ |
| 362 | char z = pMem->z[i]; |
| 363 | if( z<32 || z>126 ) *zCsr++ = '.'; |
| 364 | else *zCsr++ = z; |
| 365 | } |
| 366 | |
drh | e718efe | 2007-05-10 21:14:03 +0000 | [diff] [blame] | 367 | sqlite3_snprintf(100, zCsr, "]%s", encnames[pMem->enc]); |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 368 | zCsr += sqlite3Strlen30(zCsr); |
drh | fdf972a | 2007-05-02 13:30:27 +0000 | [diff] [blame] | 369 | if( f & MEM_Zero ){ |
drh | 8df3284 | 2008-12-09 02:51:23 +0000 | [diff] [blame] | 370 | sqlite3_snprintf(100, zCsr,"+%dz",pMem->u.nZero); |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 371 | zCsr += sqlite3Strlen30(zCsr); |
drh | fdf972a | 2007-05-02 13:30:27 +0000 | [diff] [blame] | 372 | } |
danielk1977 | b1bc953 | 2004-05-22 03:05:33 +0000 | [diff] [blame] | 373 | *zCsr = '\0'; |
| 374 | }else if( f & MEM_Str ){ |
| 375 | int j, k; |
| 376 | zBuf[0] = ' '; |
| 377 | if( f & MEM_Dyn ){ |
| 378 | zBuf[1] = 'z'; |
| 379 | assert( (f & (MEM_Static|MEM_Ephem))==0 ); |
| 380 | }else if( f & MEM_Static ){ |
| 381 | zBuf[1] = 't'; |
| 382 | assert( (f & (MEM_Dyn|MEM_Ephem))==0 ); |
| 383 | }else if( f & MEM_Ephem ){ |
| 384 | zBuf[1] = 'e'; |
| 385 | assert( (f & (MEM_Static|MEM_Dyn))==0 ); |
| 386 | }else{ |
| 387 | zBuf[1] = 's'; |
| 388 | } |
| 389 | k = 2; |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 390 | sqlite3_snprintf(100, &zBuf[k], "%d", pMem->n); |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 391 | k += sqlite3Strlen30(&zBuf[k]); |
danielk1977 | b1bc953 | 2004-05-22 03:05:33 +0000 | [diff] [blame] | 392 | zBuf[k++] = '['; |
| 393 | for(j=0; j<15 && j<pMem->n; j++){ |
| 394 | u8 c = pMem->z[j]; |
danielk1977 | b1bc953 | 2004-05-22 03:05:33 +0000 | [diff] [blame] | 395 | if( c>=0x20 && c<0x7f ){ |
| 396 | zBuf[k++] = c; |
| 397 | }else{ |
| 398 | zBuf[k++] = '.'; |
| 399 | } |
| 400 | } |
| 401 | zBuf[k++] = ']'; |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 402 | sqlite3_snprintf(100,&zBuf[k], encnames[pMem->enc]); |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 403 | k += sqlite3Strlen30(&zBuf[k]); |
danielk1977 | b1bc953 | 2004-05-22 03:05:33 +0000 | [diff] [blame] | 404 | zBuf[k++] = 0; |
danielk1977 | ca6b291 | 2004-05-21 10:49:47 +0000 | [diff] [blame] | 405 | } |
danielk1977 | ca6b291 | 2004-05-21 10:49:47 +0000 | [diff] [blame] | 406 | } |
| 407 | #endif |
| 408 | |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 409 | #ifdef SQLITE_DEBUG |
| 410 | /* |
| 411 | ** Print the value of a register for tracing purposes: |
| 412 | */ |
| 413 | static void memTracePrint(FILE *out, Mem *p){ |
| 414 | if( p->flags & MEM_Null ){ |
| 415 | fprintf(out, " NULL"); |
| 416 | }else if( (p->flags & (MEM_Int|MEM_Str))==(MEM_Int|MEM_Str) ){ |
| 417 | fprintf(out, " si:%lld", p->u.i); |
| 418 | }else if( p->flags & MEM_Int ){ |
| 419 | fprintf(out, " i:%lld", p->u.i); |
drh | 0b3bf92 | 2009-06-15 20:45:34 +0000 | [diff] [blame] | 420 | #ifndef SQLITE_OMIT_FLOATING_POINT |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 421 | }else if( p->flags & MEM_Real ){ |
| 422 | fprintf(out, " r:%g", p->r); |
drh | 0b3bf92 | 2009-06-15 20:45:34 +0000 | [diff] [blame] | 423 | #endif |
drh | 733bf1b | 2009-04-22 00:47:00 +0000 | [diff] [blame] | 424 | }else if( p->flags & MEM_RowSet ){ |
| 425 | fprintf(out, " (rowset)"); |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 426 | }else{ |
| 427 | char zBuf[200]; |
| 428 | sqlite3VdbeMemPrettyPrint(p, zBuf); |
| 429 | fprintf(out, " "); |
| 430 | fprintf(out, "%s", zBuf); |
| 431 | } |
| 432 | } |
| 433 | static void registerTrace(FILE *out, int iReg, Mem *p){ |
| 434 | fprintf(out, "REG[%d] = ", iReg); |
| 435 | memTracePrint(out, p); |
| 436 | fprintf(out, "\n"); |
| 437 | } |
| 438 | #endif |
| 439 | |
| 440 | #ifdef SQLITE_DEBUG |
drh | b21e7c7 | 2008-06-22 12:37:57 +0000 | [diff] [blame] | 441 | # define REGISTER_TRACE(R,M) if(p->trace)registerTrace(p->trace,R,M) |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 442 | #else |
| 443 | # define REGISTER_TRACE(R,M) |
| 444 | #endif |
| 445 | |
danielk1977 | 84ac9d0 | 2004-05-18 09:58:06 +0000 | [diff] [blame] | 446 | |
drh | 7b39686 | 2003-01-01 23:06:20 +0000 | [diff] [blame] | 447 | #ifdef VDBE_PROFILE |
shane | 9bcbdad | 2008-05-29 20:22:37 +0000 | [diff] [blame] | 448 | |
| 449 | /* |
| 450 | ** hwtime.h contains inline assembler code for implementing |
| 451 | ** high-performance timing routines. |
drh | 7b39686 | 2003-01-01 23:06:20 +0000 | [diff] [blame] | 452 | */ |
shane | 9bcbdad | 2008-05-29 20:22:37 +0000 | [diff] [blame] | 453 | #include "hwtime.h" |
| 454 | |
drh | 7b39686 | 2003-01-01 23:06:20 +0000 | [diff] [blame] | 455 | #endif |
| 456 | |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 457 | /* |
drh | caec2f1 | 2003-01-07 02:47:47 +0000 | [diff] [blame] | 458 | ** The CHECK_FOR_INTERRUPT macro defined here looks to see if the |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 459 | ** sqlite3_interrupt() routine has been called. If it has been, then |
drh | caec2f1 | 2003-01-07 02:47:47 +0000 | [diff] [blame] | 460 | ** processing of the VDBE program is interrupted. |
| 461 | ** |
| 462 | ** This macro added to every instruction that does a jump in order to |
| 463 | ** implement a loop. This test used to be on every single instruction, |
| 464 | ** but that meant we more testing that we needed. By only testing the |
| 465 | ** flag on jump instructions, we get a (small) speed improvement. |
| 466 | */ |
| 467 | #define CHECK_FOR_INTERRUPT \ |
drh | 881feaa | 2006-07-26 01:39:30 +0000 | [diff] [blame] | 468 | if( db->u1.isInterrupted ) goto abort_due_to_interrupt; |
drh | caec2f1 | 2003-01-07 02:47:47 +0000 | [diff] [blame] | 469 | |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 470 | #ifdef SQLITE_DEBUG |
| 471 | static int fileExists(sqlite3 *db, const char *zFile){ |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame] | 472 | int res = 0; |
| 473 | int rc = SQLITE_OK; |
| 474 | #ifdef SQLITE_TEST |
| 475 | /* If we are currently testing IO errors, then do not call OsAccess() to |
| 476 | ** test for the presence of zFile. This is because any IO error that |
| 477 | ** occurs here will not be reported, causing the test to fail. |
| 478 | */ |
| 479 | extern int sqlite3_io_error_pending; |
| 480 | if( sqlite3_io_error_pending<=0 ) |
| 481 | #endif |
| 482 | rc = sqlite3OsAccess(db->pVfs, zFile, SQLITE_ACCESS_EXISTS, &res); |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 483 | return (res && rc==SQLITE_OK); |
| 484 | } |
| 485 | #endif |
drh | caec2f1 | 2003-01-07 02:47:47 +0000 | [diff] [blame] | 486 | |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 487 | #ifndef NDEBUG |
| 488 | /* |
| 489 | ** This function is only called from within an assert() expression. It |
| 490 | ** checks that the sqlite3.nTransaction variable is correctly set to |
| 491 | ** the number of non-transaction savepoints currently in the |
| 492 | ** linked list starting at sqlite3.pSavepoint. |
| 493 | ** |
| 494 | ** Usage: |
| 495 | ** |
| 496 | ** assert( checkSavepointCount(db) ); |
| 497 | */ |
| 498 | static int checkSavepointCount(sqlite3 *db){ |
| 499 | int n = 0; |
| 500 | Savepoint *p; |
| 501 | for(p=db->pSavepoint; p; p=p->pNext) n++; |
| 502 | assert( n==(db->nSavepoint + db->isTransactionSavepoint) ); |
| 503 | return 1; |
| 504 | } |
| 505 | #endif |
| 506 | |
drh | caec2f1 | 2003-01-07 02:47:47 +0000 | [diff] [blame] | 507 | /* |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 508 | ** Execute as much of a VDBE program as we can then return. |
| 509 | ** |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 510 | ** sqlite3VdbeMakeReady() must be called before this routine in order to |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 511 | ** close the program with a final OP_Halt and to set up the callbacks |
| 512 | ** and the error message pointer. |
| 513 | ** |
| 514 | ** Whenever a row or result data is available, this routine will either |
| 515 | ** invoke the result callback (if there is one) or return with |
drh | 326dce7 | 2003-01-29 14:06:07 +0000 | [diff] [blame] | 516 | ** SQLITE_ROW. |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 517 | ** |
| 518 | ** If an attempt is made to open a locked database, then this routine |
| 519 | ** will either invoke the busy callback (if there is one) or it will |
| 520 | ** return SQLITE_BUSY. |
| 521 | ** |
| 522 | ** If an error occurs, an error message is written to memory obtained |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 523 | ** from sqlite3_malloc() and p->zErrMsg is made to point to that memory. |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 524 | ** The error code is stored in p->rc and this routine returns SQLITE_ERROR. |
| 525 | ** |
| 526 | ** If the callback ever returns non-zero, then the program exits |
| 527 | ** immediately. There will be no error message but the p->rc field is |
| 528 | ** set to SQLITE_ABORT and this routine will return SQLITE_ERROR. |
| 529 | ** |
drh | 9468c7f | 2003-03-07 19:50:07 +0000 | [diff] [blame] | 530 | ** A memory allocation error causes p->rc to be set to SQLITE_NOMEM and this |
| 531 | ** routine to return SQLITE_ERROR. |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 532 | ** |
| 533 | ** Other fatal errors return SQLITE_ERROR. |
| 534 | ** |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 535 | ** After this routine has finished, sqlite3VdbeFinalize() should be |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 536 | ** used to clean up the mess that was left behind. |
| 537 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 538 | int sqlite3VdbeExec( |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 539 | Vdbe *p /* The VDBE */ |
| 540 | ){ |
| 541 | int pc; /* The program counter */ |
drh | bbe879d | 2009-11-14 18:04:35 +0000 | [diff] [blame] | 542 | Op *aOp = p->aOp; /* Copy of p->aOp */ |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 543 | Op *pOp; /* Current operation */ |
| 544 | int rc = SQLITE_OK; /* Value to return */ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 545 | sqlite3 *db = p->db; /* The database */ |
drh | 3278315 | 2009-11-20 15:02:34 +0000 | [diff] [blame] | 546 | u8 resetSchemaOnFault = 0; /* Reset schema after an error if true */ |
drh | 8079a0d | 2006-01-12 17:20:50 +0000 | [diff] [blame] | 547 | u8 encoding = ENC(db); /* The database encoding */ |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 548 | #ifndef SQLITE_OMIT_PROGRESS_CALLBACK |
shaneh | 5e17e8b | 2009-12-03 04:40:47 +0000 | [diff] [blame] | 549 | int checkProgress; /* True if progress callbacks are enabled */ |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 550 | int nProgressOps = 0; /* Opcodes executed since progress callback. */ |
| 551 | #endif |
| 552 | Mem *aMem = p->aMem; /* Copy of p->aMem */ |
drh | b27b7f5 | 2008-12-10 18:03:45 +0000 | [diff] [blame] | 553 | Mem *pIn1 = 0; /* 1st input operand */ |
| 554 | Mem *pIn2 = 0; /* 2nd input operand */ |
| 555 | Mem *pIn3 = 0; /* 3rd input operand */ |
| 556 | Mem *pOut = 0; /* Output operand */ |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 557 | int iCompare = 0; /* Result of last OP_Compare operation */ |
shane | be21779 | 2009-03-05 04:20:31 +0000 | [diff] [blame] | 558 | int *aPermute = 0; /* Permutation of columns for OP_Compare */ |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 559 | #ifdef VDBE_PROFILE |
shane | 9bcbdad | 2008-05-29 20:22:37 +0000 | [diff] [blame] | 560 | u64 start; /* CPU clock count at start of opcode */ |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 561 | int origPc; /* Program counter at start of opcode */ |
| 562 | #endif |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 563 | /*** INSERT STACK UNION HERE ***/ |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 564 | |
drh | ca48c90 | 2008-01-18 14:08:24 +0000 | [diff] [blame] | 565 | assert( p->magic==VDBE_MAGIC_RUN ); /* sqlite3_step() verifies this */ |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 566 | assert( db->magic==SQLITE_MAGIC_BUSY ); |
danielk1977 | f7590db | 2009-04-10 12:55:16 +0000 | [diff] [blame] | 567 | sqlite3VdbeMutexArrayEnter(p); |
danielk1977 | 2e588c7 | 2005-12-09 14:25:08 +0000 | [diff] [blame] | 568 | if( p->rc==SQLITE_NOMEM ){ |
| 569 | /* This happens if a malloc() inside a call to sqlite3_column_text() or |
| 570 | ** sqlite3_column_text16() failed. */ |
| 571 | goto no_mem; |
| 572 | } |
drh | 3a84069 | 2003-01-29 22:58:26 +0000 | [diff] [blame] | 573 | assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY ); |
| 574 | p->rc = SQLITE_OK; |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 575 | assert( p->explain==0 ); |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 576 | p->pResultSet = 0; |
drh | a4afb65 | 2005-07-09 02:16:02 +0000 | [diff] [blame] | 577 | db->busyHandler.nBusy = 0; |
drh | 9358164 | 2004-02-12 13:02:55 +0000 | [diff] [blame] | 578 | CHECK_FOR_INTERRUPT; |
drh | 602c237 | 2007-03-01 00:29:13 +0000 | [diff] [blame] | 579 | sqlite3VdbeIOTraceSql(p); |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 580 | #ifndef SQLITE_OMIT_PROGRESS_CALLBACK |
| 581 | checkProgress = db->xProgress!=0; |
| 582 | #endif |
drh | 3c23a88 | 2007-01-09 14:01:13 +0000 | [diff] [blame] | 583 | #ifdef SQLITE_DEBUG |
danielk1977 | 2d1d86f | 2008-06-20 14:59:51 +0000 | [diff] [blame] | 584 | sqlite3BeginBenignMalloc(); |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 585 | if( p->pc==0 |
| 586 | && ((p->db->flags & SQLITE_VdbeListing) || fileExists(db, "vdbe_explain")) |
drh | 3c23a88 | 2007-01-09 14:01:13 +0000 | [diff] [blame] | 587 | ){ |
| 588 | int i; |
| 589 | printf("VDBE Program Listing:\n"); |
| 590 | sqlite3VdbePrintSql(p); |
| 591 | for(i=0; i<p->nOp; i++){ |
drh | bbe879d | 2009-11-14 18:04:35 +0000 | [diff] [blame] | 592 | sqlite3VdbePrintOp(stdout, i, &aOp[i]); |
drh | 3c23a88 | 2007-01-09 14:01:13 +0000 | [diff] [blame] | 593 | } |
| 594 | } |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 595 | if( fileExists(db, "vdbe_trace") ){ |
drh | 3c23a88 | 2007-01-09 14:01:13 +0000 | [diff] [blame] | 596 | p->trace = stdout; |
| 597 | } |
danielk1977 | 2d1d86f | 2008-06-20 14:59:51 +0000 | [diff] [blame] | 598 | sqlite3EndBenignMalloc(); |
drh | 3c23a88 | 2007-01-09 14:01:13 +0000 | [diff] [blame] | 599 | #endif |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 600 | for(pc=p->pc; rc==SQLITE_OK; pc++){ |
drh | caec2f1 | 2003-01-07 02:47:47 +0000 | [diff] [blame] | 601 | assert( pc>=0 && pc<p->nOp ); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 602 | if( db->mallocFailed ) goto no_mem; |
drh | 7b39686 | 2003-01-01 23:06:20 +0000 | [diff] [blame] | 603 | #ifdef VDBE_PROFILE |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 604 | origPc = pc; |
shane | 9bcbdad | 2008-05-29 20:22:37 +0000 | [diff] [blame] | 605 | start = sqlite3Hwtime(); |
drh | 7b39686 | 2003-01-01 23:06:20 +0000 | [diff] [blame] | 606 | #endif |
drh | bbe879d | 2009-11-14 18:04:35 +0000 | [diff] [blame] | 607 | pOp = &aOp[pc]; |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 608 | |
danielk1977 | 8b60e0f | 2005-01-12 09:10:39 +0000 | [diff] [blame] | 609 | /* Only allow tracing if SQLITE_DEBUG is defined. |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 610 | */ |
danielk1977 | 8b60e0f | 2005-01-12 09:10:39 +0000 | [diff] [blame] | 611 | #ifdef SQLITE_DEBUG |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 612 | if( p->trace ){ |
drh | 3f7d4e4 | 2004-07-24 14:35:58 +0000 | [diff] [blame] | 613 | if( pc==0 ){ |
| 614 | printf("VDBE Execution Trace:\n"); |
| 615 | sqlite3VdbePrintSql(p); |
| 616 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 617 | sqlite3VdbePrintOp(p->trace, pc, pOp); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 618 | } |
drh | 19db935 | 2008-03-27 22:42:51 +0000 | [diff] [blame] | 619 | if( p->trace==0 && pc==0 ){ |
danielk1977 | 2d1d86f | 2008-06-20 14:59:51 +0000 | [diff] [blame] | 620 | sqlite3BeginBenignMalloc(); |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 621 | if( fileExists(db, "vdbe_sqltrace") ){ |
drh | 19db935 | 2008-03-27 22:42:51 +0000 | [diff] [blame] | 622 | sqlite3VdbePrintSql(p); |
| 623 | } |
danielk1977 | 2d1d86f | 2008-06-20 14:59:51 +0000 | [diff] [blame] | 624 | sqlite3EndBenignMalloc(); |
drh | 3f7d4e4 | 2004-07-24 14:35:58 +0000 | [diff] [blame] | 625 | } |
| 626 | #endif |
| 627 | |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 628 | |
drh | f603871 | 2004-02-08 18:07:34 +0000 | [diff] [blame] | 629 | /* Check to see if we need to simulate an interrupt. This only happens |
| 630 | ** if we have a special test build. |
| 631 | */ |
| 632 | #ifdef SQLITE_TEST |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 633 | if( sqlite3_interrupt_count>0 ){ |
| 634 | sqlite3_interrupt_count--; |
| 635 | if( sqlite3_interrupt_count==0 ){ |
| 636 | sqlite3_interrupt(db); |
drh | f603871 | 2004-02-08 18:07:34 +0000 | [diff] [blame] | 637 | } |
| 638 | } |
| 639 | #endif |
| 640 | |
danielk1977 | 348bb5d | 2003-10-18 09:37:26 +0000 | [diff] [blame] | 641 | #ifndef SQLITE_OMIT_PROGRESS_CALLBACK |
| 642 | /* Call the progress callback if it is configured and the required number |
| 643 | ** of VDBE ops have been executed (either since this invocation of |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 644 | ** sqlite3VdbeExec() or since last time the progress callback was called). |
danielk1977 | 348bb5d | 2003-10-18 09:37:26 +0000 | [diff] [blame] | 645 | ** If the progress callback returns non-zero, exit the virtual machine with |
| 646 | ** a return code SQLITE_ABORT. |
| 647 | */ |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 648 | if( checkProgress ){ |
drh | 3914aed | 2004-01-31 20:40:42 +0000 | [diff] [blame] | 649 | if( db->nProgressOps==nProgressOps ){ |
danielk1977 | de523ac | 2007-06-15 14:53:53 +0000 | [diff] [blame] | 650 | int prc; |
drh | f8888bb | 2006-05-26 19:57:19 +0000 | [diff] [blame] | 651 | if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse; |
danielk1977 | de523ac | 2007-06-15 14:53:53 +0000 | [diff] [blame] | 652 | prc =db->xProgress(db->pProgressArg); |
drh | f8888bb | 2006-05-26 19:57:19 +0000 | [diff] [blame] | 653 | if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse; |
danielk1977 | de523ac | 2007-06-15 14:53:53 +0000 | [diff] [blame] | 654 | if( prc!=0 ){ |
| 655 | rc = SQLITE_INTERRUPT; |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 656 | goto vdbe_error_halt; |
danielk1977 | de523ac | 2007-06-15 14:53:53 +0000 | [diff] [blame] | 657 | } |
danielk1977 | 3fe11f3 | 2007-06-13 16:49:48 +0000 | [diff] [blame] | 658 | nProgressOps = 0; |
danielk1977 | 348bb5d | 2003-10-18 09:37:26 +0000 | [diff] [blame] | 659 | } |
drh | 3914aed | 2004-01-31 20:40:42 +0000 | [diff] [blame] | 660 | nProgressOps++; |
danielk1977 | 348bb5d | 2003-10-18 09:37:26 +0000 | [diff] [blame] | 661 | } |
danielk1977 | 348bb5d | 2003-10-18 09:37:26 +0000 | [diff] [blame] | 662 | #endif |
| 663 | |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 664 | /* On any opcode with the "out2-prerelase" tag, free any |
| 665 | ** external allocations out of mem[p2] and set mem[p2] to be |
| 666 | ** an undefined integer. Opcodes will either fill in the integer |
| 667 | ** value or convert mem[p2] to a different type. |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 668 | */ |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 669 | assert( pOp->opflags==sqlite3OpcodeProperty[pOp->opcode] ); |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 670 | if( pOp->opflags & OPFLG_OUT2_PRERELEASE ){ |
| 671 | assert( pOp->p2>0 ); |
| 672 | assert( pOp->p2<=p->nMem ); |
| 673 | pOut = &aMem[pOp->p2]; |
| 674 | sqlite3VdbeMemReleaseExternal(pOut); |
| 675 | pOut->flags = MEM_Int; |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 676 | } |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 677 | |
| 678 | /* Sanity checking on other operands */ |
| 679 | #ifdef SQLITE_DEBUG |
| 680 | if( (pOp->opflags & OPFLG_IN1)!=0 ){ |
| 681 | assert( pOp->p1>0 ); |
| 682 | assert( pOp->p1<=p->nMem ); |
| 683 | REGISTER_TRACE(pOp->p1, &aMem[pOp->p1]); |
| 684 | } |
| 685 | if( (pOp->opflags & OPFLG_IN2)!=0 ){ |
| 686 | assert( pOp->p2>0 ); |
| 687 | assert( pOp->p2<=p->nMem ); |
| 688 | REGISTER_TRACE(pOp->p2, &aMem[pOp->p2]); |
| 689 | } |
| 690 | if( (pOp->opflags & OPFLG_IN3)!=0 ){ |
| 691 | assert( pOp->p3>0 ); |
| 692 | assert( pOp->p3<=p->nMem ); |
| 693 | REGISTER_TRACE(pOp->p3, &aMem[pOp->p3]); |
| 694 | } |
| 695 | if( (pOp->opflags & OPFLG_OUT2)!=0 ){ |
| 696 | assert( pOp->p2>0 ); |
| 697 | assert( pOp->p2<=p->nMem ); |
| 698 | } |
| 699 | if( (pOp->opflags & OPFLG_OUT3)!=0 ){ |
| 700 | assert( pOp->p3>0 ); |
| 701 | assert( pOp->p3<=p->nMem ); |
| 702 | } |
| 703 | #endif |
drh | 93952eb | 2009-11-13 19:43:43 +0000 | [diff] [blame] | 704 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 705 | switch( pOp->opcode ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 706 | |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 707 | /***************************************************************************** |
| 708 | ** What follows is a massive switch statement where each case implements a |
| 709 | ** separate instruction in the virtual machine. If we follow the usual |
| 710 | ** indentation conventions, each case should be indented by 6 spaces. But |
| 711 | ** that is a lot of wasted space on the left margin. So the code within |
| 712 | ** the switch statement will break with convention and be flush-left. Another |
| 713 | ** big comment (similar to this one) will mark the point in the code where |
| 714 | ** we transition back to normal indentation. |
drh | ac82fcf | 2002-09-08 17:23:41 +0000 | [diff] [blame] | 715 | ** |
| 716 | ** The formatting of each case is important. The makefile for SQLite |
| 717 | ** generates two C files "opcodes.h" and "opcodes.c" by scanning this |
| 718 | ** file looking for lines that begin with "case OP_". The opcodes.h files |
| 719 | ** will be filled with #defines that give unique integer values to each |
| 720 | ** 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] | 721 | ** each string is the symbolic name for the corresponding opcode. If the |
| 722 | ** case statement is followed by a comment of the form "/# same as ... #/" |
| 723 | ** that comment is used to determine the particular value of the opcode. |
drh | ac82fcf | 2002-09-08 17:23:41 +0000 | [diff] [blame] | 724 | ** |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 725 | ** Other keywords in the comment that follows each case are used to |
| 726 | ** construct the OPFLG_INITIALIZER value that initializes opcodeProperty[]. |
| 727 | ** Keywords include: in1, in2, in3, out2_prerelease, out2, out3. See |
| 728 | ** the mkopcodeh.awk script for additional information. |
danielk1977 | bc04f85 | 2005-03-29 08:26:13 +0000 | [diff] [blame] | 729 | ** |
drh | ac82fcf | 2002-09-08 17:23:41 +0000 | [diff] [blame] | 730 | ** Documentation about VDBE opcodes is generated by scanning this file |
| 731 | ** for lines of that contain "Opcode:". That line and all subsequent |
| 732 | ** comment lines are used in the generation of the opcode.html documentation |
| 733 | ** file. |
| 734 | ** |
| 735 | ** SUMMARY: |
| 736 | ** |
| 737 | ** Formatting is important to scripts that scan this file. |
| 738 | ** Do not deviate from the formatting style currently in use. |
| 739 | ** |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 740 | *****************************************************************************/ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 741 | |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 742 | /* Opcode: Goto * P2 * * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 743 | ** |
| 744 | ** An unconditional jump to address P2. |
| 745 | ** The next instruction executed will be |
| 746 | ** the one at index P2 from the beginning of |
| 747 | ** the program. |
| 748 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 749 | case OP_Goto: { /* jump */ |
drh | caec2f1 | 2003-01-07 02:47:47 +0000 | [diff] [blame] | 750 | CHECK_FOR_INTERRUPT; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 751 | pc = pOp->p2 - 1; |
| 752 | break; |
| 753 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 754 | |
drh | 2eb9537 | 2008-06-06 15:04:36 +0000 | [diff] [blame] | 755 | /* Opcode: Gosub P1 P2 * * * |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 756 | ** |
drh | 2eb9537 | 2008-06-06 15:04:36 +0000 | [diff] [blame] | 757 | ** Write the current address onto register P1 |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 758 | ** and then jump to address P2. |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 759 | */ |
drh | 93952eb | 2009-11-13 19:43:43 +0000 | [diff] [blame] | 760 | case OP_Gosub: { /* jump, in1 */ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 761 | pIn1 = &aMem[pOp->p1]; |
drh | 2eb9537 | 2008-06-06 15:04:36 +0000 | [diff] [blame] | 762 | assert( (pIn1->flags & MEM_Dyn)==0 ); |
| 763 | pIn1->flags = MEM_Int; |
| 764 | pIn1->u.i = pc; |
| 765 | REGISTER_TRACE(pOp->p1, pIn1); |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 766 | pc = pOp->p2 - 1; |
| 767 | break; |
| 768 | } |
| 769 | |
drh | 2eb9537 | 2008-06-06 15:04:36 +0000 | [diff] [blame] | 770 | /* Opcode: Return P1 * * * * |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 771 | ** |
drh | 2eb9537 | 2008-06-06 15:04:36 +0000 | [diff] [blame] | 772 | ** Jump to the next instruction after the address in register P1. |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 773 | */ |
drh | 2eb9537 | 2008-06-06 15:04:36 +0000 | [diff] [blame] | 774 | case OP_Return: { /* in1 */ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 775 | pIn1 = &aMem[pOp->p1]; |
drh | 2eb9537 | 2008-06-06 15:04:36 +0000 | [diff] [blame] | 776 | assert( pIn1->flags & MEM_Int ); |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 777 | pc = (int)pIn1->u.i; |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 778 | break; |
| 779 | } |
| 780 | |
drh | e00ee6e | 2008-06-20 15:24:01 +0000 | [diff] [blame] | 781 | /* Opcode: Yield P1 * * * * |
| 782 | ** |
| 783 | ** Swap the program counter with the value in register P1. |
| 784 | */ |
danielk1977 | f73ab8b | 2008-12-29 10:39:53 +0000 | [diff] [blame] | 785 | case OP_Yield: { /* in1 */ |
drh | e00ee6e | 2008-06-20 15:24:01 +0000 | [diff] [blame] | 786 | int pcDest; |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 787 | pIn1 = &aMem[pOp->p1]; |
drh | e00ee6e | 2008-06-20 15:24:01 +0000 | [diff] [blame] | 788 | assert( (pIn1->flags & MEM_Dyn)==0 ); |
| 789 | pIn1->flags = MEM_Int; |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 790 | pcDest = (int)pIn1->u.i; |
drh | e00ee6e | 2008-06-20 15:24:01 +0000 | [diff] [blame] | 791 | pIn1->u.i = pc; |
| 792 | REGISTER_TRACE(pOp->p1, pIn1); |
| 793 | pc = pcDest; |
| 794 | break; |
| 795 | } |
| 796 | |
drh | 5053a79 | 2009-02-20 03:02:23 +0000 | [diff] [blame] | 797 | /* Opcode: HaltIfNull P1 P2 P3 P4 * |
| 798 | ** |
| 799 | ** Check the value in register P3. If is is NULL then Halt using |
| 800 | ** parameter P1, P2, and P4 as if this were a Halt instruction. If the |
| 801 | ** value in register P3 is not NULL, then this routine is a no-op. |
| 802 | */ |
| 803 | case OP_HaltIfNull: { /* in3 */ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 804 | pIn3 = &aMem[pOp->p3]; |
drh | 5053a79 | 2009-02-20 03:02:23 +0000 | [diff] [blame] | 805 | if( (pIn3->flags & MEM_Null)==0 ) break; |
| 806 | /* Fall through into OP_Halt */ |
| 807 | } |
drh | e00ee6e | 2008-06-20 15:24:01 +0000 | [diff] [blame] | 808 | |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 809 | /* Opcode: Halt P1 P2 * P4 * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 810 | ** |
drh | 3d4501e | 2008-12-04 20:40:10 +0000 | [diff] [blame] | 811 | ** Exit immediately. All open cursors, etc are closed |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 812 | ** automatically. |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 813 | ** |
drh | 92f02c3 | 2004-09-02 14:57:08 +0000 | [diff] [blame] | 814 | ** P1 is the result code returned by sqlite3_exec(), sqlite3_reset(), |
| 815 | ** or sqlite3_finalize(). For a normal halt, this should be SQLITE_OK (0). |
| 816 | ** For errors, it can be some other value. If P1!=0 then P2 will determine |
| 817 | ** whether or not to rollback the current transaction. Do not rollback |
| 818 | ** if P2==OE_Fail. Do the rollback if P2==OE_Rollback. If P2==OE_Abort, |
| 819 | ** then back out all changes that have occurred during this execution of the |
drh | b798fa6 | 2002-09-03 19:43:23 +0000 | [diff] [blame] | 820 | ** VDBE, but do not rollback the transaction. |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 821 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 822 | ** If P4 is not null then it is an error message string. |
drh | 7f057c9 | 2005-06-24 03:53:06 +0000 | [diff] [blame] | 823 | ** |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 824 | ** 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] | 825 | ** every program. So a jump past the last instruction of the program |
| 826 | ** is the same as executing Halt. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 827 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 828 | case OP_Halt: { |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 829 | if( pOp->p1==SQLITE_OK && p->pFrame ){ |
dan | 2832ad4 | 2009-08-31 15:27:27 +0000 | [diff] [blame] | 830 | /* Halt the sub-program. Return control to the parent frame. */ |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 831 | VdbeFrame *pFrame = p->pFrame; |
| 832 | p->pFrame = pFrame->pParent; |
| 833 | p->nFrame--; |
dan | 2832ad4 | 2009-08-31 15:27:27 +0000 | [diff] [blame] | 834 | sqlite3VdbeSetChanges(db, p->nChange); |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 835 | pc = sqlite3VdbeFrameRestore(pFrame); |
| 836 | if( pOp->p2==OE_Ignore ){ |
dan | 2832ad4 | 2009-08-31 15:27:27 +0000 | [diff] [blame] | 837 | /* Instruction pc is the OP_Program that invoked the sub-program |
| 838 | ** currently being halted. If the p2 instruction of this OP_Halt |
| 839 | ** instruction is set to OE_Ignore, then the sub-program is throwing |
| 840 | ** an IGNORE exception. In this case jump to the address specified |
| 841 | ** as the p2 of the calling OP_Program. */ |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 842 | pc = p->aOp[pc].p2-1; |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 843 | } |
drh | bbe879d | 2009-11-14 18:04:35 +0000 | [diff] [blame] | 844 | aOp = p->aOp; |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 845 | aMem = p->aMem; |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 846 | break; |
| 847 | } |
dan | 2832ad4 | 2009-08-31 15:27:27 +0000 | [diff] [blame] | 848 | |
drh | 92f02c3 | 2004-09-02 14:57:08 +0000 | [diff] [blame] | 849 | p->rc = pOp->p1; |
shane | 36840fd | 2009-06-26 16:32:13 +0000 | [diff] [blame] | 850 | p->errorAction = (u8)pOp->p2; |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 851 | p->pc = pc; |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 852 | if( pOp->p4.z ){ |
drh | f089aa4 | 2008-07-08 19:34:06 +0000 | [diff] [blame] | 853 | sqlite3SetString(&p->zErrMsg, db, "%s", pOp->p4.z); |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 854 | } |
drh | 92f02c3 | 2004-09-02 14:57:08 +0000 | [diff] [blame] | 855 | rc = sqlite3VdbeHalt(p); |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 856 | assert( rc==SQLITE_BUSY || rc==SQLITE_OK || rc==SQLITE_ERROR ); |
drh | 92f02c3 | 2004-09-02 14:57:08 +0000 | [diff] [blame] | 857 | if( rc==SQLITE_BUSY ){ |
drh | 900b31e | 2007-08-28 02:27:51 +0000 | [diff] [blame] | 858 | p->rc = rc = SQLITE_BUSY; |
| 859 | }else{ |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 860 | assert( rc==SQLITE_OK || p->rc==SQLITE_CONSTRAINT ); |
| 861 | assert( rc==SQLITE_OK || db->nDeferredCons>0 ); |
drh | 900b31e | 2007-08-28 02:27:51 +0000 | [diff] [blame] | 862 | rc = p->rc ? SQLITE_ERROR : SQLITE_DONE; |
drh | 92f02c3 | 2004-09-02 14:57:08 +0000 | [diff] [blame] | 863 | } |
drh | 900b31e | 2007-08-28 02:27:51 +0000 | [diff] [blame] | 864 | goto vdbe_return; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 865 | } |
drh | c61053b | 2000-06-04 12:58:36 +0000 | [diff] [blame] | 866 | |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 867 | /* Opcode: Integer P1 P2 * * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 868 | ** |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 869 | ** The 32-bit integer value P1 is written into register P2. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 870 | */ |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 871 | case OP_Integer: { /* out2-prerelease */ |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 872 | pOut->u.i = pOp->p1; |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 873 | break; |
| 874 | } |
| 875 | |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 876 | /* Opcode: Int64 * P2 * P4 * |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 877 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 878 | ** P4 is a pointer to a 64-bit integer value. |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 879 | ** Write that value into register P2. |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 880 | */ |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 881 | case OP_Int64: { /* out2-prerelease */ |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 882 | assert( pOp->p4.pI64!=0 ); |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 883 | pOut->u.i = *pOp->p4.pI64; |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 884 | break; |
| 885 | } |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 886 | |
drh | 13573c7 | 2010-01-12 17:04:07 +0000 | [diff] [blame] | 887 | #ifndef SQLITE_OMIT_FLOATING_POINT |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 888 | /* Opcode: Real * P2 * P4 * |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 889 | ** |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 890 | ** P4 is a pointer to a 64-bit floating point value. |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 891 | ** Write that value into register P2. |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 892 | */ |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 893 | case OP_Real: { /* same as TK_FLOAT, out2-prerelease */ |
| 894 | pOut->flags = MEM_Real; |
drh | 2eaf93d | 2008-04-29 00:15:20 +0000 | [diff] [blame] | 895 | assert( !sqlite3IsNaN(*pOp->p4.pReal) ); |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 896 | pOut->r = *pOp->p4.pReal; |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 897 | break; |
| 898 | } |
drh | 13573c7 | 2010-01-12 17:04:07 +0000 | [diff] [blame] | 899 | #endif |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 900 | |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 901 | /* Opcode: String8 * P2 * P4 * |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 902 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 903 | ** P4 points to a nul terminated UTF-8 string. This opcode is transformed |
danielk1977 | 0f69c1e | 2004-05-29 11:24:50 +0000 | [diff] [blame] | 904 | ** into an OP_String before it is executed for the first time. |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 905 | */ |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 906 | case OP_String8: { /* same as TK_STRING, out2-prerelease */ |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 907 | assert( pOp->p4.z!=0 ); |
drh | ed2df7f | 2005-11-16 04:34:32 +0000 | [diff] [blame] | 908 | pOp->opcode = OP_String; |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 909 | pOp->p1 = sqlite3Strlen30(pOp->p4.z); |
drh | ed2df7f | 2005-11-16 04:34:32 +0000 | [diff] [blame] | 910 | |
| 911 | #ifndef SQLITE_OMIT_UTF16 |
drh | 8079a0d | 2006-01-12 17:20:50 +0000 | [diff] [blame] | 912 | if( encoding!=SQLITE_UTF8 ){ |
drh | 3a9cf17 | 2009-06-17 21:42:33 +0000 | [diff] [blame] | 913 | rc = sqlite3VdbeMemSetStr(pOut, pOp->p4.z, -1, SQLITE_UTF8, SQLITE_STATIC); |
| 914 | if( rc==SQLITE_TOOBIG ) goto too_big; |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 915 | if( SQLITE_OK!=sqlite3VdbeChangeEncoding(pOut, encoding) ) goto no_mem; |
drh | 3a9cf17 | 2009-06-17 21:42:33 +0000 | [diff] [blame] | 916 | assert( pOut->zMalloc==pOut->z ); |
| 917 | assert( pOut->flags & MEM_Dyn ); |
danielk1977 | 5f09613 | 2008-03-28 15:44:09 +0000 | [diff] [blame] | 918 | pOut->zMalloc = 0; |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 919 | pOut->flags |= MEM_Static; |
drh | 191b54c | 2008-04-15 12:14:21 +0000 | [diff] [blame] | 920 | pOut->flags &= ~MEM_Dyn; |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 921 | if( pOp->p4type==P4_DYNAMIC ){ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 922 | sqlite3DbFree(db, pOp->p4.z); |
danielk1977 | e004840 | 2004-06-15 16:51:01 +0000 | [diff] [blame] | 923 | } |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 924 | pOp->p4type = P4_DYNAMIC; |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 925 | pOp->p4.z = pOut->z; |
| 926 | pOp->p1 = pOut->n; |
danielk1977 | 0f69c1e | 2004-05-29 11:24:50 +0000 | [diff] [blame] | 927 | } |
danielk1977 | 93758c8 | 2005-01-21 08:13:14 +0000 | [diff] [blame] | 928 | #endif |
drh | bb4957f | 2008-03-20 14:03:29 +0000 | [diff] [blame] | 929 | if( pOp->p1>db->aLimit[SQLITE_LIMIT_LENGTH] ){ |
drh | cbd2da9 | 2007-12-17 16:20:06 +0000 | [diff] [blame] | 930 | goto too_big; |
| 931 | } |
| 932 | /* Fall through to the next case, OP_String */ |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 933 | } |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 934 | |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 935 | /* Opcode: String P1 P2 * P4 * |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 936 | ** |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 937 | ** The string value P4 of length P1 (bytes) is stored in register P2. |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 938 | */ |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 939 | case OP_String: { /* out2-prerelease */ |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 940 | assert( pOp->p4.z!=0 ); |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 941 | pOut->flags = MEM_Str|MEM_Static|MEM_Term; |
| 942 | pOut->z = pOp->p4.z; |
| 943 | pOut->n = pOp->p1; |
| 944 | pOut->enc = encoding; |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 945 | UPDATE_MAX_BLOBSIZE(pOut); |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 946 | break; |
| 947 | } |
| 948 | |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 949 | /* Opcode: Null * P2 * * * |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 950 | ** |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 951 | ** Write a NULL into register P2. |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 952 | */ |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 953 | case OP_Null: { /* out2-prerelease */ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 954 | pOut->flags = MEM_Null; |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 955 | break; |
| 956 | } |
| 957 | |
| 958 | |
drh | 9de221d | 2008-01-05 06:51:30 +0000 | [diff] [blame] | 959 | /* Opcode: Blob P1 P2 * P4 |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 960 | ** |
drh | 9de221d | 2008-01-05 06:51:30 +0000 | [diff] [blame] | 961 | ** P4 points to a blob of data P1 bytes long. Store this |
| 962 | ** blob in register P2. This instruction is not coded directly |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 963 | ** by the compiler. Instead, the compiler layer specifies |
| 964 | ** an OP_HexBlob opcode, with the hex string representation of |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 965 | ** the blob as P4. This opcode is transformed to an OP_Blob |
danielk1977 | 93758c8 | 2005-01-21 08:13:14 +0000 | [diff] [blame] | 966 | ** the first time it is executed. |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 967 | */ |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 968 | case OP_Blob: { /* out2-prerelease */ |
drh | cbd2da9 | 2007-12-17 16:20:06 +0000 | [diff] [blame] | 969 | assert( pOp->p1 <= SQLITE_MAX_LENGTH ); |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 970 | sqlite3VdbeMemSetStr(pOut, pOp->p4.z, pOp->p1, 0, 0); |
drh | 9de221d | 2008-01-05 06:51:30 +0000 | [diff] [blame] | 971 | pOut->enc = encoding; |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 972 | UPDATE_MAX_BLOBSIZE(pOut); |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 973 | break; |
| 974 | } |
| 975 | |
drh | 08de149 | 2009-02-20 03:55:05 +0000 | [diff] [blame] | 976 | /* Opcode: Variable P1 P2 P3 P4 * |
drh | 5045789 | 2003-09-06 01:10:47 +0000 | [diff] [blame] | 977 | ** |
drh | 08de149 | 2009-02-20 03:55:05 +0000 | [diff] [blame] | 978 | ** Transfer the values of bound parameters P1..P1+P3-1 into registers |
| 979 | ** P2..P2+P3-1. |
| 980 | ** |
| 981 | ** If the parameter is named, then its name appears in P4 and P3==1. |
| 982 | ** The P4 value is used by sqlite3_bind_parameter_name(). |
drh | 5045789 | 2003-09-06 01:10:47 +0000 | [diff] [blame] | 983 | */ |
drh | 08de149 | 2009-02-20 03:55:05 +0000 | [diff] [blame] | 984 | case OP_Variable: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 985 | int p1; /* Variable to copy from */ |
| 986 | int p2; /* Register to copy to */ |
| 987 | int n; /* Number of values left to copy */ |
| 988 | Mem *pVar; /* Value being transferred */ |
| 989 | |
| 990 | p1 = pOp->p1 - 1; |
| 991 | p2 = pOp->p2; |
| 992 | n = pOp->p3; |
| 993 | assert( p1>=0 && p1+n<=p->nVar ); |
| 994 | assert( p2>=1 && p2+n-1<=p->nMem ); |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 995 | assert( pOp->p4.z==0 || pOp->p3==1 || pOp->p3==0 ); |
danielk1977 | 295ba55 | 2004-05-19 10:34:51 +0000 | [diff] [blame] | 996 | |
drh | 08de149 | 2009-02-20 03:55:05 +0000 | [diff] [blame] | 997 | while( n-- > 0 ){ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 998 | pVar = &p->aVar[p1++]; |
drh | 08de149 | 2009-02-20 03:55:05 +0000 | [diff] [blame] | 999 | if( sqlite3VdbeMemTooBig(pVar) ){ |
| 1000 | goto too_big; |
| 1001 | } |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 1002 | pOut = &aMem[p2++]; |
drh | 08de149 | 2009-02-20 03:55:05 +0000 | [diff] [blame] | 1003 | sqlite3VdbeMemReleaseExternal(pOut); |
| 1004 | pOut->flags = MEM_Null; |
| 1005 | sqlite3VdbeMemShallowCopy(pOut, pVar, MEM_Static); |
| 1006 | UPDATE_MAX_BLOBSIZE(pOut); |
drh | 023ae03 | 2007-05-08 12:12:16 +0000 | [diff] [blame] | 1007 | } |
danielk1977 | 93d4675 | 2004-05-23 13:30:58 +0000 | [diff] [blame] | 1008 | break; |
| 1009 | } |
danielk1977 | 295ba55 | 2004-05-19 10:34:51 +0000 | [diff] [blame] | 1010 | |
drh | b21e7c7 | 2008-06-22 12:37:57 +0000 | [diff] [blame] | 1011 | /* Opcode: Move P1 P2 P3 * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1012 | ** |
drh | b21e7c7 | 2008-06-22 12:37:57 +0000 | [diff] [blame] | 1013 | ** Move the values in register P1..P1+P3-1 over into |
| 1014 | ** registers P2..P2+P3-1. Registers P1..P1+P1-1 are |
| 1015 | ** left holding a NULL. It is an error for register ranges |
| 1016 | ** P1..P1+P3-1 and P2..P2+P3-1 to overlap. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1017 | */ |
drh | e1349cb | 2008-04-01 00:36:10 +0000 | [diff] [blame] | 1018 | case OP_Move: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1019 | char *zMalloc; /* Holding variable for allocated memory */ |
| 1020 | int n; /* Number of registers left to copy */ |
| 1021 | int p1; /* Register to copy from */ |
| 1022 | int p2; /* Register to copy to */ |
| 1023 | |
| 1024 | n = pOp->p3; |
| 1025 | p1 = pOp->p1; |
| 1026 | p2 = pOp->p2; |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1027 | assert( n>0 && p1>0 && p2>0 ); |
drh | b21e7c7 | 2008-06-22 12:37:57 +0000 | [diff] [blame] | 1028 | assert( p1+n<=p2 || p2+n<=p1 ); |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1029 | |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 1030 | pIn1 = &aMem[p1]; |
| 1031 | pOut = &aMem[p2]; |
drh | b21e7c7 | 2008-06-22 12:37:57 +0000 | [diff] [blame] | 1032 | while( n-- ){ |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 1033 | assert( pOut<=&aMem[p->nMem] ); |
| 1034 | assert( pIn1<=&aMem[p->nMem] ); |
drh | b21e7c7 | 2008-06-22 12:37:57 +0000 | [diff] [blame] | 1035 | zMalloc = pOut->zMalloc; |
| 1036 | pOut->zMalloc = 0; |
| 1037 | sqlite3VdbeMemMove(pOut, pIn1); |
| 1038 | pIn1->zMalloc = zMalloc; |
| 1039 | REGISTER_TRACE(p2++, pOut); |
| 1040 | pIn1++; |
| 1041 | pOut++; |
| 1042 | } |
drh | e1349cb | 2008-04-01 00:36:10 +0000 | [diff] [blame] | 1043 | break; |
| 1044 | } |
| 1045 | |
drh | b1fdb2a | 2008-01-05 04:06:03 +0000 | [diff] [blame] | 1046 | /* Opcode: Copy P1 P2 * * * |
| 1047 | ** |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1048 | ** Make a copy of register P1 into register P2. |
drh | b1fdb2a | 2008-01-05 04:06:03 +0000 | [diff] [blame] | 1049 | ** |
| 1050 | ** This instruction makes a deep copy of the value. A duplicate |
| 1051 | ** is made of any string or blob constant. See also OP_SCopy. |
| 1052 | */ |
drh | 93952eb | 2009-11-13 19:43:43 +0000 | [diff] [blame] | 1053 | case OP_Copy: { /* in1, out2 */ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 1054 | pIn1 = &aMem[pOp->p1]; |
| 1055 | pOut = &aMem[pOp->p2]; |
drh | e1349cb | 2008-04-01 00:36:10 +0000 | [diff] [blame] | 1056 | assert( pOut!=pIn1 ); |
| 1057 | sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem); |
| 1058 | Deephemeralize(pOut); |
| 1059 | REGISTER_TRACE(pOp->p2, pOut); |
| 1060 | break; |
| 1061 | } |
| 1062 | |
drh | b1fdb2a | 2008-01-05 04:06:03 +0000 | [diff] [blame] | 1063 | /* Opcode: SCopy P1 P2 * * * |
| 1064 | ** |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1065 | ** Make a shallow copy of register P1 into register P2. |
drh | b1fdb2a | 2008-01-05 04:06:03 +0000 | [diff] [blame] | 1066 | ** |
| 1067 | ** This instruction makes a shallow copy of the value. If the value |
| 1068 | ** is a string or blob, then the copy is only a pointer to the |
| 1069 | ** original and hence if the original changes so will the copy. |
| 1070 | ** Worse, if the original is deallocated, the copy becomes invalid. |
| 1071 | ** Thus the program must guarantee that the original will not change |
| 1072 | ** during the lifetime of the copy. Use OP_Copy to make a complete |
| 1073 | ** copy. |
| 1074 | */ |
drh | 93952eb | 2009-11-13 19:43:43 +0000 | [diff] [blame] | 1075 | case OP_SCopy: { /* in1, out2 */ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 1076 | pIn1 = &aMem[pOp->p1]; |
| 1077 | pOut = &aMem[pOp->p2]; |
drh | 2d401ab | 2008-01-10 23:50:11 +0000 | [diff] [blame] | 1078 | assert( pOut!=pIn1 ); |
drh | e1349cb | 2008-04-01 00:36:10 +0000 | [diff] [blame] | 1079 | sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem); |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1080 | REGISTER_TRACE(pOp->p2, pOut); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1081 | break; |
| 1082 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1083 | |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1084 | /* Opcode: ResultRow P1 P2 * * * |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 1085 | ** |
shane | 21e7feb | 2008-05-30 15:59:49 +0000 | [diff] [blame] | 1086 | ** The registers P1 through P1+P2-1 contain a single row of |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 1087 | ** results. This opcode causes the sqlite3_step() call to terminate |
| 1088 | ** with an SQLITE_ROW return code and it sets up the sqlite3_stmt |
| 1089 | ** structure to provide access to the top P1 values as the result |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1090 | ** row. |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 1091 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1092 | case OP_ResultRow: { |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 1093 | Mem *pMem; |
| 1094 | int i; |
| 1095 | assert( p->nResColumn==pOp->p2 ); |
drh | 0a07c10 | 2008-01-03 18:03:08 +0000 | [diff] [blame] | 1096 | assert( pOp->p1>0 ); |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1097 | assert( pOp->p1+pOp->p2<=p->nMem+1 ); |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 1098 | |
dan | 32b09f2 | 2009-09-23 17:29:59 +0000 | [diff] [blame] | 1099 | /* If this statement has violated immediate foreign key constraints, do |
| 1100 | ** not return the number of rows modified. And do not RELEASE the statement |
| 1101 | ** transaction. It needs to be rolled back. */ |
| 1102 | if( SQLITE_OK!=(rc = sqlite3VdbeCheckFk(p, 0)) ){ |
| 1103 | assert( db->flags&SQLITE_CountRows ); |
| 1104 | assert( p->usesStmtJournal ); |
| 1105 | break; |
| 1106 | } |
| 1107 | |
danielk1977 | bd43455 | 2009-03-18 10:33:00 +0000 | [diff] [blame] | 1108 | /* If the SQLITE_CountRows flag is set in sqlite3.flags mask, then |
| 1109 | ** DML statements invoke this opcode to return the number of rows |
| 1110 | ** modified to the user. This is the only way that a VM that |
| 1111 | ** opens a statement transaction may invoke this opcode. |
| 1112 | ** |
| 1113 | ** In case this is such a statement, close any statement transaction |
| 1114 | ** opened by this VM before returning control to the user. This is to |
| 1115 | ** ensure that statement-transactions are always nested, not overlapping. |
| 1116 | ** If the open statement-transaction is not closed here, then the user |
| 1117 | ** may step another VM that opens its own statement transaction. This |
| 1118 | ** may lead to overlapping statement transactions. |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 1119 | ** |
| 1120 | ** The statement transaction is never a top-level transaction. Hence |
| 1121 | ** the RELEASE call below can never fail. |
danielk1977 | bd43455 | 2009-03-18 10:33:00 +0000 | [diff] [blame] | 1122 | */ |
| 1123 | assert( p->iStatement==0 || db->flags&SQLITE_CountRows ); |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 1124 | rc = sqlite3VdbeCloseStatement(p, SAVEPOINT_RELEASE); |
| 1125 | if( NEVER(rc!=SQLITE_OK) ){ |
danielk1977 | bd43455 | 2009-03-18 10:33:00 +0000 | [diff] [blame] | 1126 | break; |
| 1127 | } |
| 1128 | |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 1129 | /* Invalidate all ephemeral cursor row caches */ |
| 1130 | p->cacheCtr = (p->cacheCtr + 2)|1; |
| 1131 | |
| 1132 | /* Make sure the results of the current row are \000 terminated |
shane | 21e7feb | 2008-05-30 15:59:49 +0000 | [diff] [blame] | 1133 | ** and have an assigned type. The results are de-ephemeralized as |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 1134 | ** as side effect. |
| 1135 | */ |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 1136 | pMem = p->pResultSet = &aMem[pOp->p1]; |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 1137 | for(i=0; i<pOp->p2; i++){ |
| 1138 | sqlite3VdbeMemNulTerminate(&pMem[i]); |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 1139 | sqlite3VdbeMemStoreType(&pMem[i]); |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 1140 | REGISTER_TRACE(pOp->p1+i, &pMem[i]); |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 1141 | } |
drh | 2803969 | 2008-03-17 16:54:01 +0000 | [diff] [blame] | 1142 | if( db->mallocFailed ) goto no_mem; |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 1143 | |
| 1144 | /* Return SQLITE_ROW |
| 1145 | */ |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 1146 | p->pc = pc + 1; |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 1147 | rc = SQLITE_ROW; |
| 1148 | goto vdbe_return; |
| 1149 | } |
| 1150 | |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1151 | /* Opcode: Concat P1 P2 P3 * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1152 | ** |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1153 | ** Add the text in register P1 onto the end of the text in |
| 1154 | ** register P2 and store the result in register P3. |
| 1155 | ** 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] | 1156 | ** |
| 1157 | ** P3 = P2 || P1 |
| 1158 | ** |
| 1159 | ** It is illegal for P1 and P3 to be the same register. Sometimes, |
| 1160 | ** if P3 is the same register as P2, the implementation is able |
| 1161 | ** to avoid a memcpy(). |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1162 | */ |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1163 | case OP_Concat: { /* same as TK_CONCAT, in1, in2, out3 */ |
drh | 023ae03 | 2007-05-08 12:12:16 +0000 | [diff] [blame] | 1164 | i64 nByte; |
danielk1977 | 8a6b541 | 2004-05-24 07:04:25 +0000 | [diff] [blame] | 1165 | |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 1166 | pIn1 = &aMem[pOp->p1]; |
| 1167 | pIn2 = &aMem[pOp->p2]; |
| 1168 | pOut = &aMem[pOp->p3]; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1169 | assert( pIn1!=pOut ); |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1170 | if( (pIn1->flags | pIn2->flags) & MEM_Null ){ |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1171 | sqlite3VdbeMemSetNull(pOut); |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1172 | break; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1173 | } |
drh | a0c0652 | 2009-06-17 22:50:41 +0000 | [diff] [blame] | 1174 | if( ExpandBlob(pIn1) || ExpandBlob(pIn2) ) goto no_mem; |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1175 | Stringify(pIn1, encoding); |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1176 | Stringify(pIn2, encoding); |
| 1177 | nByte = pIn1->n + pIn2->n; |
drh | bb4957f | 2008-03-20 14:03:29 +0000 | [diff] [blame] | 1178 | if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){ |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1179 | goto too_big; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1180 | } |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1181 | MemSetTypeFlag(pOut, MEM_Str); |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 1182 | if( sqlite3VdbeMemGrow(pOut, (int)nByte+2, pOut==pIn2) ){ |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1183 | goto no_mem; |
| 1184 | } |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1185 | if( pOut!=pIn2 ){ |
| 1186 | memcpy(pOut->z, pIn2->z, pIn2->n); |
| 1187 | } |
| 1188 | memcpy(&pOut->z[pIn2->n], pIn1->z, pIn1->n); |
| 1189 | pOut->z[nByte] = 0; |
| 1190 | pOut->z[nByte+1] = 0; |
| 1191 | pOut->flags |= MEM_Term; |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 1192 | pOut->n = (int)nByte; |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1193 | pOut->enc = encoding; |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 1194 | UPDATE_MAX_BLOBSIZE(pOut); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1195 | break; |
| 1196 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1197 | |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 1198 | /* Opcode: Add P1 P2 P3 * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1199 | ** |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1200 | ** Add the value in register P1 to the value in register P2 |
shane | 21e7feb | 2008-05-30 15:59:49 +0000 | [diff] [blame] | 1201 | ** and store the result in register P3. |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1202 | ** If either input is NULL, the result is NULL. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1203 | */ |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 1204 | /* Opcode: Multiply P1 P2 P3 * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1205 | ** |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 1206 | ** |
shane | 21e7feb | 2008-05-30 15:59:49 +0000 | [diff] [blame] | 1207 | ** Multiply the value in register P1 by the value in register P2 |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1208 | ** and store the result in register P3. |
| 1209 | ** If either input is NULL, the result is NULL. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1210 | */ |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 1211 | /* Opcode: Subtract P1 P2 P3 * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1212 | ** |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1213 | ** Subtract the value in register P1 from the value in register P2 |
| 1214 | ** and store the result in register P3. |
| 1215 | ** If either input is NULL, the result is NULL. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1216 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1217 | /* Opcode: Divide P1 P2 P3 * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1218 | ** |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1219 | ** Divide the value in register P1 by the value in register P2 |
dan | e275dc3 | 2009-08-18 16:24:58 +0000 | [diff] [blame] | 1220 | ** and store the result in register P3 (P3=P2/P1). If the value in |
| 1221 | ** register P1 is zero, then the result is NULL. If either input is |
| 1222 | ** NULL, the result is NULL. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1223 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1224 | /* Opcode: Remainder P1 P2 P3 * * |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1225 | ** |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 1226 | ** Compute the remainder after integer division of the value in |
| 1227 | ** register P1 by the value in register P2 and store the result in P3. |
| 1228 | ** If the value in register P2 is zero the result is NULL. |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1229 | ** If either operand is NULL, the result is NULL. |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1230 | */ |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1231 | case OP_Add: /* same as TK_PLUS, in1, in2, out3 */ |
| 1232 | case OP_Subtract: /* same as TK_MINUS, in1, in2, out3 */ |
| 1233 | case OP_Multiply: /* same as TK_STAR, in1, in2, out3 */ |
| 1234 | case OP_Divide: /* same as TK_SLASH, in1, in2, out3 */ |
| 1235 | case OP_Remainder: { /* same as TK_REM, in1, in2, out3 */ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1236 | int flags; /* Combined MEM_* flags from both inputs */ |
| 1237 | i64 iA; /* Integer value of left operand */ |
| 1238 | i64 iB; /* Integer value of right operand */ |
| 1239 | double rA; /* Real value of left operand */ |
| 1240 | double rB; /* Real value of right operand */ |
| 1241 | |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 1242 | pIn1 = &aMem[pOp->p1]; |
drh | 61669b3 | 2008-07-30 13:27:10 +0000 | [diff] [blame] | 1243 | applyNumericAffinity(pIn1); |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 1244 | pIn2 = &aMem[pOp->p2]; |
drh | 61669b3 | 2008-07-30 13:27:10 +0000 | [diff] [blame] | 1245 | applyNumericAffinity(pIn2); |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 1246 | pOut = &aMem[pOp->p3]; |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1247 | flags = pIn1->flags | pIn2->flags; |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 1248 | if( (flags & MEM_Null)!=0 ) goto arithmetic_result_is_null; |
| 1249 | if( (pIn1->flags & pIn2->flags & MEM_Int)==MEM_Int ){ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1250 | iA = pIn1->u.i; |
| 1251 | iB = pIn2->u.i; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1252 | switch( pOp->opcode ){ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1253 | case OP_Add: iB += iA; break; |
| 1254 | case OP_Subtract: iB -= iA; break; |
| 1255 | case OP_Multiply: iB *= iA; break; |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1256 | case OP_Divide: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1257 | if( iA==0 ) goto arithmetic_result_is_null; |
danielk1977 | 42d4ef2 | 2007-06-26 11:13:25 +0000 | [diff] [blame] | 1258 | /* Dividing the largest possible negative 64-bit integer (1<<63) by |
drh | 0f05035 | 2008-05-09 18:03:13 +0000 | [diff] [blame] | 1259 | ** -1 returns an integer too large to store in a 64-bit data-type. On |
danielk1977 | 42d4ef2 | 2007-06-26 11:13:25 +0000 | [diff] [blame] | 1260 | ** some architectures, the value overflows to (1<<63). On others, |
| 1261 | ** a SIGFPE is issued. The following statement normalizes this |
shane | 21e7feb | 2008-05-30 15:59:49 +0000 | [diff] [blame] | 1262 | ** behavior so that all architectures behave as if integer |
| 1263 | ** overflow occurred. |
danielk1977 | 42d4ef2 | 2007-06-26 11:13:25 +0000 | [diff] [blame] | 1264 | */ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1265 | if( iA==-1 && iB==SMALLEST_INT64 ) iA = 1; |
| 1266 | iB /= iA; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1267 | break; |
| 1268 | } |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1269 | default: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1270 | if( iA==0 ) goto arithmetic_result_is_null; |
| 1271 | if( iA==-1 ) iA = 1; |
| 1272 | iB %= iA; |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1273 | break; |
| 1274 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1275 | } |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1276 | pOut->u.i = iB; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1277 | MemSetTypeFlag(pOut, MEM_Int); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1278 | }else{ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1279 | rA = sqlite3VdbeRealValue(pIn1); |
| 1280 | rB = sqlite3VdbeRealValue(pIn2); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1281 | switch( pOp->opcode ){ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1282 | case OP_Add: rB += rA; break; |
| 1283 | case OP_Subtract: rB -= rA; break; |
| 1284 | case OP_Multiply: rB *= rA; break; |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1285 | case OP_Divide: { |
shane | fbd60f8 | 2009-02-04 03:59:25 +0000 | [diff] [blame] | 1286 | /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1287 | if( rA==(double)0 ) goto arithmetic_result_is_null; |
| 1288 | rB /= rA; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1289 | break; |
| 1290 | } |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1291 | default: { |
shane | 75ac1de | 2009-06-09 18:58:52 +0000 | [diff] [blame] | 1292 | iA = (i64)rA; |
| 1293 | iB = (i64)rB; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1294 | if( iA==0 ) goto arithmetic_result_is_null; |
| 1295 | if( iA==-1 ) iA = 1; |
| 1296 | rB = (double)(iB % iA); |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1297 | break; |
| 1298 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1299 | } |
drh | c5a7b51 | 2010-01-13 16:25:42 +0000 | [diff] [blame] | 1300 | #ifdef SQLITE_OMIT_FLOATING_POINT |
| 1301 | pOut->u.i = rB; |
| 1302 | MemSetTypeFlag(pOut, MEM_Int); |
| 1303 | #else |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1304 | if( sqlite3IsNaN(rB) ){ |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 1305 | goto arithmetic_result_is_null; |
drh | 53c1402 | 2007-05-10 17:23:11 +0000 | [diff] [blame] | 1306 | } |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1307 | pOut->r = rB; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1308 | MemSetTypeFlag(pOut, MEM_Real); |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1309 | if( (flags & MEM_Real)==0 ){ |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1310 | sqlite3VdbeIntegerAffinity(pOut); |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1311 | } |
drh | c5a7b51 | 2010-01-13 16:25:42 +0000 | [diff] [blame] | 1312 | #endif |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1313 | } |
| 1314 | break; |
| 1315 | |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 1316 | arithmetic_result_is_null: |
| 1317 | sqlite3VdbeMemSetNull(pOut); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1318 | break; |
| 1319 | } |
| 1320 | |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 1321 | /* Opcode: CollSeq * * P4 |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 1322 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 1323 | ** 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] | 1324 | ** or aggregate calls sqlite3GetFuncCollSeq(), this collation sequence will |
| 1325 | ** be returned. This is used by the built-in min(), max() and nullif() |
drh | e6f85e7 | 2004-12-25 01:03:13 +0000 | [diff] [blame] | 1326 | ** functions. |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 1327 | ** |
| 1328 | ** The interface used by the implementation of the aforementioned functions |
| 1329 | ** to retrieve the collation sequence set by this opcode is not available |
| 1330 | ** publicly, only to user functions defined in func.c. |
| 1331 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1332 | case OP_CollSeq: { |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 1333 | assert( pOp->p4type==P4_COLLSEQ ); |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 1334 | break; |
| 1335 | } |
| 1336 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1337 | /* Opcode: Function P1 P2 P3 P4 P5 |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 1338 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 1339 | ** Invoke a user function (P4 is a pointer to a Function structure that |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1340 | ** defines the function) with P5 arguments taken from register P2 and |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1341 | ** successors. The result of the function is stored in register P3. |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1342 | ** Register P3 must not be one of the function inputs. |
danielk1977 | 682f68b | 2004-06-05 10:22:17 +0000 | [diff] [blame] | 1343 | ** |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 1344 | ** 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] | 1345 | ** function was determined to be constant at compile time. If the first |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 1346 | ** 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] | 1347 | ** whether meta data associated with a user function argument using the |
| 1348 | ** sqlite3_set_auxdata() API may be safely retained until the next |
| 1349 | ** invocation of this opcode. |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 1350 | ** |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 1351 | ** See also: AggStep and AggFinal |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 1352 | */ |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 1353 | case OP_Function: { |
danielk1977 | 51ad0ec | 2004-05-24 12:39:02 +0000 | [diff] [blame] | 1354 | int i; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1355 | Mem *pArg; |
danielk1977 | 22322fd | 2004-05-25 23:35:17 +0000 | [diff] [blame] | 1356 | sqlite3_context ctx; |
danielk1977 | 51ad0ec | 2004-05-24 12:39:02 +0000 | [diff] [blame] | 1357 | sqlite3_value **apVal; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1358 | int n; |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 1359 | |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1360 | n = pOp->p5; |
danielk1977 | 6ddcca5 | 2004-05-24 23:48:25 +0000 | [diff] [blame] | 1361 | apVal = p->apArg; |
danielk1977 | 51ad0ec | 2004-05-24 12:39:02 +0000 | [diff] [blame] | 1362 | assert( apVal || n==0 ); |
| 1363 | |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1364 | assert( n==0 || (pOp->p2>0 && pOp->p2+n<=p->nMem+1) ); |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1365 | assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n ); |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 1366 | pArg = &aMem[pOp->p2]; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1367 | for(i=0; i<n; i++, pArg++){ |
danielk1977 | 51ad0ec | 2004-05-24 12:39:02 +0000 | [diff] [blame] | 1368 | apVal[i] = pArg; |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 1369 | sqlite3VdbeMemStoreType(pArg); |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 1370 | REGISTER_TRACE(pOp->p2, pArg); |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 1371 | } |
danielk1977 | 51ad0ec | 2004-05-24 12:39:02 +0000 | [diff] [blame] | 1372 | |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 1373 | assert( pOp->p4type==P4_FUNCDEF || pOp->p4type==P4_VDBEFUNC ); |
| 1374 | if( pOp->p4type==P4_FUNCDEF ){ |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 1375 | ctx.pFunc = pOp->p4.pFunc; |
danielk1977 | 682f68b | 2004-06-05 10:22:17 +0000 | [diff] [blame] | 1376 | ctx.pVdbeFunc = 0; |
| 1377 | }else{ |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 1378 | ctx.pVdbeFunc = (VdbeFunc*)pOp->p4.pVdbeFunc; |
danielk1977 | 682f68b | 2004-06-05 10:22:17 +0000 | [diff] [blame] | 1379 | ctx.pFunc = ctx.pVdbeFunc->pFunc; |
| 1380 | } |
| 1381 | |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1382 | assert( pOp->p3>0 && pOp->p3<=p->nMem ); |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 1383 | pOut = &aMem[pOp->p3]; |
drh | 00706be | 2004-01-30 14:49:16 +0000 | [diff] [blame] | 1384 | ctx.s.flags = MEM_Null; |
drh | fa4a4b9 | 2008-03-19 21:45:51 +0000 | [diff] [blame] | 1385 | ctx.s.db = db; |
danielk1977 | 5f09613 | 2008-03-28 15:44:09 +0000 | [diff] [blame] | 1386 | ctx.s.xDel = 0; |
| 1387 | ctx.s.zMalloc = 0; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1388 | |
| 1389 | /* The output cell may already have a buffer allocated. Move |
| 1390 | ** the pointer to ctx.s so in case the user-function can use |
| 1391 | ** the already allocated buffer instead of allocating a new one. |
| 1392 | */ |
| 1393 | sqlite3VdbeMemMove(&ctx.s, pOut); |
| 1394 | MemSetTypeFlag(&ctx.s, MEM_Null); |
| 1395 | |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 1396 | ctx.isError = 0; |
drh | e82f5d0 | 2008-10-07 19:53:14 +0000 | [diff] [blame] | 1397 | if( ctx.pFunc->flags & SQLITE_FUNC_NEEDCOLL ){ |
drh | bbe879d | 2009-11-14 18:04:35 +0000 | [diff] [blame] | 1398 | assert( pOp>aOp ); |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 1399 | assert( pOp[-1].p4type==P4_COLLSEQ ); |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 1400 | assert( pOp[-1].opcode==OP_CollSeq ); |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 1401 | ctx.pColl = pOp[-1].p4.pColl; |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 1402 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1403 | if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse; |
danielk1977 | 51ad0ec | 2004-05-24 12:39:02 +0000 | [diff] [blame] | 1404 | (*ctx.pFunc->xFunc)(&ctx, n, apVal); |
danielk1977 | 75eb016 | 2008-03-28 19:16:33 +0000 | [diff] [blame] | 1405 | if( sqlite3SafetyOn(db) ){ |
| 1406 | sqlite3VdbeMemRelease(&ctx.s); |
| 1407 | goto abort_due_to_misuse; |
| 1408 | } |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 1409 | if( db->mallocFailed ){ |
danielk1977 | e0fc526 | 2007-07-26 06:50:05 +0000 | [diff] [blame] | 1410 | /* Even though a malloc() has failed, the implementation of the |
| 1411 | ** user function may have called an sqlite3_result_XXX() function |
| 1412 | ** to return a value. The following call releases any resources |
| 1413 | ** associated with such a value. |
| 1414 | ** |
| 1415 | ** Note: Maybe MemRelease() should be called if sqlite3SafetyOn() |
| 1416 | ** fails also (the if(...) statement above). But if people are |
| 1417 | ** misusing sqlite, they have bigger problems than a leaked value. |
| 1418 | */ |
| 1419 | sqlite3VdbeMemRelease(&ctx.s); |
| 1420 | goto no_mem; |
| 1421 | } |
danielk1977 | 7e18c25 | 2004-05-25 11:47:24 +0000 | [diff] [blame] | 1422 | |
shane | 21e7feb | 2008-05-30 15:59:49 +0000 | [diff] [blame] | 1423 | /* If any auxiliary data functions have been called by this user function, |
danielk1977 | 682f68b | 2004-06-05 10:22:17 +0000 | [diff] [blame] | 1424 | ** immediately call the destructor for any non-static values. |
| 1425 | */ |
| 1426 | if( ctx.pVdbeFunc ){ |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 1427 | sqlite3VdbeDeleteAuxData(ctx.pVdbeFunc, pOp->p1); |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 1428 | pOp->p4.pVdbeFunc = ctx.pVdbeFunc; |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 1429 | pOp->p4type = P4_VDBEFUNC; |
danielk1977 | 682f68b | 2004-06-05 10:22:17 +0000 | [diff] [blame] | 1430 | } |
| 1431 | |
drh | 90669c1 | 2006-01-20 15:45:36 +0000 | [diff] [blame] | 1432 | /* If the function returned an error, throw an exception */ |
| 1433 | if( ctx.isError ){ |
drh | f089aa4 | 2008-07-08 19:34:06 +0000 | [diff] [blame] | 1434 | sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(&ctx.s)); |
drh | 69544ec | 2008-02-06 14:11:34 +0000 | [diff] [blame] | 1435 | rc = ctx.isError; |
drh | 90669c1 | 2006-01-20 15:45:36 +0000 | [diff] [blame] | 1436 | } |
| 1437 | |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1438 | /* Copy the result of the function into register P3 */ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 1439 | sqlite3VdbeChangeEncoding(&ctx.s, encoding); |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1440 | sqlite3VdbeMemMove(pOut, &ctx.s); |
| 1441 | if( sqlite3VdbeMemTooBig(pOut) ){ |
drh | 023ae03 | 2007-05-08 12:12:16 +0000 | [diff] [blame] | 1442 | goto too_big; |
| 1443 | } |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 1444 | REGISTER_TRACE(pOp->p3, pOut); |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 1445 | UPDATE_MAX_BLOBSIZE(pOut); |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 1446 | break; |
| 1447 | } |
| 1448 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1449 | /* Opcode: BitAnd P1 P2 P3 * * |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1450 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1451 | ** Take the bit-wise AND of the values in register P1 and P2 and |
| 1452 | ** store the result in register P3. |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1453 | ** If either input is NULL, the result is NULL. |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1454 | */ |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1455 | /* Opcode: BitOr P1 P2 P3 * * |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1456 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1457 | ** Take the bit-wise OR of the values in register P1 and P2 and |
| 1458 | ** store the result in register P3. |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1459 | ** If either input is NULL, the result is NULL. |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1460 | */ |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1461 | /* Opcode: ShiftLeft P1 P2 P3 * * |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1462 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1463 | ** Shift the integer value in register P2 to the left by the |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1464 | ** number of bits specified by the integer in regiser P1. |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1465 | ** Store the result in register P3. |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1466 | ** If either input is NULL, the result is NULL. |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1467 | */ |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1468 | /* Opcode: ShiftRight P1 P2 P3 * * |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1469 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1470 | ** Shift the integer value in register P2 to the right by the |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1471 | ** number of bits specified by the integer in register P1. |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1472 | ** Store the result in register P3. |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1473 | ** If either input is NULL, the result is NULL. |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1474 | */ |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1475 | case OP_BitAnd: /* same as TK_BITAND, in1, in2, out3 */ |
| 1476 | case OP_BitOr: /* same as TK_BITOR, in1, in2, out3 */ |
| 1477 | case OP_ShiftLeft: /* same as TK_LSHIFT, in1, in2, out3 */ |
| 1478 | case OP_ShiftRight: { /* same as TK_RSHIFT, in1, in2, out3 */ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1479 | i64 a; |
| 1480 | i64 b; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1481 | |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 1482 | pIn1 = &aMem[pOp->p1]; |
| 1483 | pIn2 = &aMem[pOp->p2]; |
| 1484 | pOut = &aMem[pOp->p3]; |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1485 | if( (pIn1->flags | pIn2->flags) & MEM_Null ){ |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 1486 | sqlite3VdbeMemSetNull(pOut); |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1487 | break; |
| 1488 | } |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1489 | a = sqlite3VdbeIntValue(pIn2); |
| 1490 | b = sqlite3VdbeIntValue(pIn1); |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1491 | switch( pOp->opcode ){ |
| 1492 | case OP_BitAnd: a &= b; break; |
| 1493 | case OP_BitOr: a |= b; break; |
| 1494 | case OP_ShiftLeft: a <<= b; break; |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 1495 | default: assert( pOp->opcode==OP_ShiftRight ); |
| 1496 | a >>= b; break; |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1497 | } |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1498 | pOut->u.i = a; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1499 | MemSetTypeFlag(pOut, MEM_Int); |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1500 | break; |
| 1501 | } |
| 1502 | |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1503 | /* Opcode: AddImm P1 P2 * * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1504 | ** |
danielk1977 | 0cdc022 | 2008-06-26 18:04:03 +0000 | [diff] [blame] | 1505 | ** Add the constant P2 to the value in register P1. |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1506 | ** The result is always an integer. |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 1507 | ** |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1508 | ** To force any register to be an integer, just add 0. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1509 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1510 | case OP_AddImm: { /* in1 */ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 1511 | pIn1 = &aMem[pOp->p1]; |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1512 | sqlite3VdbeMemIntegerify(pIn1); |
| 1513 | pIn1->u.i += pOp->p2; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1514 | break; |
| 1515 | } |
| 1516 | |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1517 | /* Opcode: MustBeInt P1 P2 * * * |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 1518 | ** |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1519 | ** Force the value in register P1 to be an integer. If the value |
| 1520 | ** in P1 is not an integer and cannot be converted into an integer |
danielk1977 | 9a96b66 | 2007-11-29 17:05:18 +0000 | [diff] [blame] | 1521 | ** without data loss, then jump immediately to P2, or if P2==0 |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 1522 | ** raise an SQLITE_MISMATCH exception. |
| 1523 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1524 | case OP_MustBeInt: { /* jump, in1 */ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 1525 | pIn1 = &aMem[pOp->p1]; |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 1526 | applyAffinity(pIn1, SQLITE_AFF_NUMERIC, encoding); |
| 1527 | if( (pIn1->flags & MEM_Int)==0 ){ |
drh | 17c4029 | 2004-07-21 02:53:29 +0000 | [diff] [blame] | 1528 | if( pOp->p2==0 ){ |
| 1529 | rc = SQLITE_MISMATCH; |
| 1530 | goto abort_due_to_error; |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 1531 | }else{ |
drh | 17c4029 | 2004-07-21 02:53:29 +0000 | [diff] [blame] | 1532 | pc = pOp->p2 - 1; |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 1533 | } |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 1534 | }else{ |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1535 | MemSetTypeFlag(pIn1, MEM_Int); |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 1536 | } |
| 1537 | break; |
| 1538 | } |
| 1539 | |
drh | 13573c7 | 2010-01-12 17:04:07 +0000 | [diff] [blame] | 1540 | #ifndef SQLITE_OMIT_FLOATING_POINT |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1541 | /* Opcode: RealAffinity P1 * * * * |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 1542 | ** |
drh | 2133d82 | 2008-01-03 18:44:59 +0000 | [diff] [blame] | 1543 | ** If register P1 holds an integer convert it to a real value. |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 1544 | ** |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1545 | ** This opcode is used when extracting information from a column that |
| 1546 | ** has REAL affinity. Such column values may still be stored as |
| 1547 | ** integers, for space efficiency, but after extraction we want them |
| 1548 | ** to have only a real value. |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 1549 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1550 | case OP_RealAffinity: { /* in1 */ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 1551 | pIn1 = &aMem[pOp->p1]; |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1552 | if( pIn1->flags & MEM_Int ){ |
| 1553 | sqlite3VdbeMemRealify(pIn1); |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1554 | } |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 1555 | break; |
| 1556 | } |
drh | 13573c7 | 2010-01-12 17:04:07 +0000 | [diff] [blame] | 1557 | #endif |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 1558 | |
drh | 8df447f | 2005-11-01 15:48:24 +0000 | [diff] [blame] | 1559 | #ifndef SQLITE_OMIT_CAST |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1560 | /* Opcode: ToText P1 * * * * |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 1561 | ** |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1562 | ** Force the value in register P1 to be text. |
drh | 31beae9 | 2005-11-24 14:34:36 +0000 | [diff] [blame] | 1563 | ** If the value is numeric, convert it to a string using the |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 1564 | ** equivalent of printf(). Blob values are unchanged and |
| 1565 | ** are afterwards simply interpreted as text. |
| 1566 | ** |
| 1567 | ** A NULL value is not changed by this routine. It remains NULL. |
| 1568 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1569 | case OP_ToText: { /* same as TK_TO_TEXT, in1 */ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 1570 | pIn1 = &aMem[pOp->p1]; |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1571 | if( pIn1->flags & MEM_Null ) break; |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 1572 | assert( MEM_Str==(MEM_Blob>>3) ); |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1573 | pIn1->flags |= (pIn1->flags&MEM_Blob)>>3; |
| 1574 | applyAffinity(pIn1, SQLITE_AFF_TEXT, encoding); |
| 1575 | rc = ExpandBlob(pIn1); |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1576 | assert( pIn1->flags & MEM_Str || db->mallocFailed ); |
drh | 68ac65e | 2009-01-05 18:02:27 +0000 | [diff] [blame] | 1577 | pIn1->flags &= ~(MEM_Int|MEM_Real|MEM_Blob|MEM_Zero); |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 1578 | UPDATE_MAX_BLOBSIZE(pIn1); |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 1579 | break; |
| 1580 | } |
| 1581 | |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1582 | /* Opcode: ToBlob P1 * * * * |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 1583 | ** |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1584 | ** Force the value in register P1 to be a BLOB. |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 1585 | ** If the value is numeric, convert it to a string first. |
| 1586 | ** Strings are simply reinterpreted as blobs with no change |
| 1587 | ** to the underlying data. |
| 1588 | ** |
| 1589 | ** A NULL value is not changed by this routine. It remains NULL. |
| 1590 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1591 | case OP_ToBlob: { /* same as TK_TO_BLOB, in1 */ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 1592 | pIn1 = &aMem[pOp->p1]; |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1593 | if( pIn1->flags & MEM_Null ) break; |
| 1594 | if( (pIn1->flags & MEM_Blob)==0 ){ |
| 1595 | applyAffinity(pIn1, SQLITE_AFF_TEXT, encoding); |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1596 | assert( pIn1->flags & MEM_Str || db->mallocFailed ); |
drh | de58ddb | 2009-01-05 22:30:38 +0000 | [diff] [blame] | 1597 | MemSetTypeFlag(pIn1, MEM_Blob); |
| 1598 | }else{ |
| 1599 | pIn1->flags &= ~(MEM_TypeMask&~MEM_Blob); |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 1600 | } |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 1601 | UPDATE_MAX_BLOBSIZE(pIn1); |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 1602 | break; |
| 1603 | } |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1604 | |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1605 | /* Opcode: ToNumeric P1 * * * * |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1606 | ** |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1607 | ** Force the value in register P1 to be numeric (either an |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1608 | ** integer or a floating-point number.) |
| 1609 | ** If the value is text or blob, try to convert it to an using the |
| 1610 | ** equivalent of atoi() or atof() and store 0 if no such conversion |
| 1611 | ** is possible. |
| 1612 | ** |
| 1613 | ** A NULL value is not changed by this routine. It remains NULL. |
| 1614 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1615 | case OP_ToNumeric: { /* same as TK_TO_NUMERIC, in1 */ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 1616 | pIn1 = &aMem[pOp->p1]; |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1617 | if( (pIn1->flags & (MEM_Null|MEM_Int|MEM_Real))==0 ){ |
| 1618 | sqlite3VdbeMemNumerify(pIn1); |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1619 | } |
| 1620 | break; |
| 1621 | } |
| 1622 | #endif /* SQLITE_OMIT_CAST */ |
| 1623 | |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1624 | /* Opcode: ToInt P1 * * * * |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1625 | ** |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1626 | ** Force the value in register P1 be an integer. If |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1627 | ** The value is currently a real number, drop its fractional part. |
| 1628 | ** If the value is text or blob, try to convert it to an integer using the |
| 1629 | ** equivalent of atoi() and store 0 if no such conversion is possible. |
| 1630 | ** |
| 1631 | ** A NULL value is not changed by this routine. It remains NULL. |
| 1632 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1633 | case OP_ToInt: { /* same as TK_TO_INT, in1 */ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 1634 | pIn1 = &aMem[pOp->p1]; |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1635 | if( (pIn1->flags & MEM_Null)==0 ){ |
| 1636 | sqlite3VdbeMemIntegerify(pIn1); |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1637 | } |
| 1638 | break; |
| 1639 | } |
| 1640 | |
drh | 13573c7 | 2010-01-12 17:04:07 +0000 | [diff] [blame] | 1641 | #if !defined(SQLITE_OMIT_CAST) && !defined(SQLITE_OMIT_FLOATING_POINT) |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1642 | /* Opcode: ToReal P1 * * * * |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1643 | ** |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1644 | ** Force the value in register P1 to be a floating point number. |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1645 | ** If The value is currently an integer, convert it. |
| 1646 | ** If the value is text or blob, try to convert it to an integer using the |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1647 | ** equivalent of atoi() and store 0.0 if no such conversion is possible. |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1648 | ** |
| 1649 | ** A NULL value is not changed by this routine. It remains NULL. |
| 1650 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1651 | case OP_ToReal: { /* same as TK_TO_REAL, in1 */ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 1652 | pIn1 = &aMem[pOp->p1]; |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1653 | if( (pIn1->flags & MEM_Null)==0 ){ |
| 1654 | sqlite3VdbeMemRealify(pIn1); |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1655 | } |
| 1656 | break; |
| 1657 | } |
drh | 13573c7 | 2010-01-12 17:04:07 +0000 | [diff] [blame] | 1658 | #endif /* !defined(SQLITE_OMIT_CAST) && !defined(SQLITE_OMIT_FLOATING_POINT) */ |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 1659 | |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 1660 | /* Opcode: Lt P1 P2 P3 P4 P5 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1661 | ** |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 1662 | ** Compare the values in register P1 and P3. If reg(P3)<reg(P1) then |
| 1663 | ** jump to address P2. |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1664 | ** |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 1665 | ** If the SQLITE_JUMPIFNULL bit of P5 is set and either reg(P1) or |
| 1666 | ** reg(P3) is NULL then take the jump. If the SQLITE_JUMPIFNULL |
| 1667 | ** bit is clear then fall thru if either operand is NULL. |
drh | 4f68623 | 2005-09-20 13:55:18 +0000 | [diff] [blame] | 1668 | ** |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 1669 | ** The SQLITE_AFF_MASK portion of P5 must be an affinity character - |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1670 | ** SQLITE_AFF_TEXT, SQLITE_AFF_INTEGER, and so forth. An attempt is made |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1671 | ** to coerce both inputs according to this affinity before the |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 1672 | ** comparison is made. If the SQLITE_AFF_MASK is 0x00, then numeric |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1673 | ** affinity is used. Note that the affinity conversions are stored |
| 1674 | ** back into the input registers P1 and P3. So this opcode can cause |
| 1675 | ** persistent changes to registers P1 and P3. |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 1676 | ** |
| 1677 | ** Once any conversions have taken place, and neither value is NULL, |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 1678 | ** the values are compared. If both values are blobs then memcmp() is |
| 1679 | ** used to determine the results of the comparison. If both values |
| 1680 | ** are text, then the appropriate collating function specified in |
| 1681 | ** P4 is used to do the comparison. If P4 is not specified then |
| 1682 | ** memcmp() is used to compare text string. If both values are |
| 1683 | ** numeric, then a numeric comparison is used. If the two values |
| 1684 | ** are of different types, then numbers are considered less than |
| 1685 | ** strings and strings are considered less than blobs. |
drh | c9b84a1 | 2002-06-20 11:36:48 +0000 | [diff] [blame] | 1686 | ** |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 1687 | ** If the SQLITE_STOREP2 bit of P5 is set, then do not jump. Instead, |
| 1688 | ** store a boolean result (either 0, or 1, or NULL) in register P2. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1689 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1690 | /* Opcode: Ne P1 P2 P3 P4 P5 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1691 | ** |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 1692 | ** This works just like the Lt opcode except that the jump is taken if |
| 1693 | ** 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] | 1694 | ** additional information. |
drh | 6a2fe09 | 2009-09-23 02:29:36 +0000 | [diff] [blame] | 1695 | ** |
| 1696 | ** If SQLITE_NULLEQ is set in P5 then the result of comparison is always either |
| 1697 | ** true or false and is never NULL. If both operands are NULL then the result |
| 1698 | ** of comparison is false. If either operand is NULL then the result is true. |
| 1699 | ** If neither operand is NULL the the result is the same as it would be if |
| 1700 | ** the SQLITE_NULLEQ flag were omitted from P5. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1701 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1702 | /* Opcode: Eq P1 P2 P3 P4 P5 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1703 | ** |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 1704 | ** This works just like the Lt opcode except that the jump is taken if |
| 1705 | ** the operands in registers P1 and P3 are equal. |
| 1706 | ** See the Lt opcode for additional information. |
drh | 6a2fe09 | 2009-09-23 02:29:36 +0000 | [diff] [blame] | 1707 | ** |
| 1708 | ** If SQLITE_NULLEQ is set in P5 then the result of comparison is always either |
| 1709 | ** true or false and is never NULL. If both operands are NULL then the result |
| 1710 | ** of comparison is true. If either operand is NULL then the result is false. |
| 1711 | ** If neither operand is NULL the the result is the same as it would be if |
| 1712 | ** the SQLITE_NULLEQ flag were omitted from P5. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1713 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1714 | /* Opcode: Le P1 P2 P3 P4 P5 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1715 | ** |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 1716 | ** This works just like the Lt opcode except that the jump is taken if |
| 1717 | ** the content of register P3 is less than or equal to the content of |
| 1718 | ** register P1. See the Lt opcode for additional information. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1719 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1720 | /* Opcode: Gt P1 P2 P3 P4 P5 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1721 | ** |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 1722 | ** This works just like the Lt opcode except that the jump is taken if |
| 1723 | ** the content of register P3 is greater than the content of |
| 1724 | ** register P1. See the Lt opcode for additional information. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1725 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1726 | /* Opcode: Ge P1 P2 P3 P4 P5 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1727 | ** |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 1728 | ** This works just like the Lt opcode except that the jump is taken if |
| 1729 | ** the content of register P3 is greater than or equal to the content of |
| 1730 | ** register P1. See the Lt opcode for additional information. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1731 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1732 | case OP_Eq: /* same as TK_EQ, jump, in1, in3 */ |
| 1733 | case OP_Ne: /* same as TK_NE, jump, in1, in3 */ |
| 1734 | case OP_Lt: /* same as TK_LT, jump, in1, in3 */ |
| 1735 | case OP_Le: /* same as TK_LE, jump, in1, in3 */ |
| 1736 | case OP_Gt: /* same as TK_GT, jump, in1, in3 */ |
| 1737 | case OP_Ge: { /* same as TK_GE, jump, in1, in3 */ |
drh | 6a2fe09 | 2009-09-23 02:29:36 +0000 | [diff] [blame] | 1738 | int res; /* Result of the comparison of pIn1 against pIn3 */ |
| 1739 | char affinity; /* Affinity to use for comparison */ |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 1740 | |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 1741 | pIn1 = &aMem[pOp->p1]; |
| 1742 | pIn3 = &aMem[pOp->p3]; |
drh | 6a2fe09 | 2009-09-23 02:29:36 +0000 | [diff] [blame] | 1743 | if( (pIn1->flags | pIn3->flags)&MEM_Null ){ |
| 1744 | /* One or both operands are NULL */ |
| 1745 | if( pOp->p5 & SQLITE_NULLEQ ){ |
| 1746 | /* If SQLITE_NULLEQ is set (which will only happen if the operator is |
| 1747 | ** OP_Eq or OP_Ne) then take the jump or not depending on whether |
| 1748 | ** or not both operands are null. |
| 1749 | */ |
| 1750 | assert( pOp->opcode==OP_Eq || pOp->opcode==OP_Ne ); |
| 1751 | res = (pIn1->flags & pIn3->flags & MEM_Null)==0; |
| 1752 | }else{ |
| 1753 | /* SQLITE_NULLEQ is clear and at least one operand is NULL, |
| 1754 | ** then the result is always NULL. |
| 1755 | ** The jump is taken if the SQLITE_JUMPIFNULL bit is set. |
| 1756 | */ |
| 1757 | if( pOp->p5 & SQLITE_STOREP2 ){ |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 1758 | pOut = &aMem[pOp->p2]; |
drh | 6a2fe09 | 2009-09-23 02:29:36 +0000 | [diff] [blame] | 1759 | MemSetTypeFlag(pOut, MEM_Null); |
| 1760 | REGISTER_TRACE(pOp->p2, pOut); |
| 1761 | }else if( pOp->p5 & SQLITE_JUMPIFNULL ){ |
| 1762 | pc = pOp->p2-1; |
| 1763 | } |
| 1764 | break; |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 1765 | } |
drh | 6a2fe09 | 2009-09-23 02:29:36 +0000 | [diff] [blame] | 1766 | }else{ |
| 1767 | /* Neither operand is NULL. Do a comparison. */ |
| 1768 | affinity = pOp->p5 & SQLITE_AFF_MASK; |
| 1769 | if( affinity ){ |
| 1770 | applyAffinity(pIn1, affinity, encoding); |
| 1771 | applyAffinity(pIn3, affinity, encoding); |
| 1772 | if( db->mallocFailed ) goto no_mem; |
| 1773 | } |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 1774 | |
drh | 6a2fe09 | 2009-09-23 02:29:36 +0000 | [diff] [blame] | 1775 | assert( pOp->p4type==P4_COLLSEQ || pOp->p4.pColl==0 ); |
| 1776 | ExpandBlob(pIn1); |
| 1777 | ExpandBlob(pIn3); |
| 1778 | res = sqlite3MemCompare(pIn3, pIn1, pOp->p4.pColl); |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 1779 | } |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 1780 | switch( pOp->opcode ){ |
| 1781 | case OP_Eq: res = res==0; break; |
| 1782 | case OP_Ne: res = res!=0; break; |
| 1783 | case OP_Lt: res = res<0; break; |
| 1784 | case OP_Le: res = res<=0; break; |
| 1785 | case OP_Gt: res = res>0; break; |
| 1786 | default: res = res>=0; break; |
| 1787 | } |
| 1788 | |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 1789 | if( pOp->p5 & SQLITE_STOREP2 ){ |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 1790 | pOut = &aMem[pOp->p2]; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1791 | MemSetTypeFlag(pOut, MEM_Int); |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 1792 | pOut->u.i = res; |
| 1793 | REGISTER_TRACE(pOp->p2, pOut); |
| 1794 | }else if( res ){ |
| 1795 | pc = pOp->p2-1; |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 1796 | } |
| 1797 | break; |
| 1798 | } |
drh | c9b84a1 | 2002-06-20 11:36:48 +0000 | [diff] [blame] | 1799 | |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 1800 | /* Opcode: Permutation * * * P4 * |
| 1801 | ** |
shane | be21779 | 2009-03-05 04:20:31 +0000 | [diff] [blame] | 1802 | ** Set the permutation used by the OP_Compare operator to be the array |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 1803 | ** of integers in P4. |
| 1804 | ** |
| 1805 | ** The permutation is only valid until the next OP_Permutation, OP_Compare, |
| 1806 | ** OP_Halt, or OP_ResultRow. Typically the OP_Permutation should occur |
| 1807 | ** immediately prior to the OP_Compare. |
| 1808 | */ |
| 1809 | case OP_Permutation: { |
| 1810 | assert( pOp->p4type==P4_INTARRAY ); |
| 1811 | assert( pOp->p4.ai ); |
| 1812 | aPermute = pOp->p4.ai; |
| 1813 | break; |
| 1814 | } |
| 1815 | |
drh | 16ee60f | 2008-06-20 18:13:25 +0000 | [diff] [blame] | 1816 | /* Opcode: Compare P1 P2 P3 P4 * |
| 1817 | ** |
| 1818 | ** Compare to vectors of registers in reg(P1)..reg(P1+P3-1) (all this |
| 1819 | ** one "A") and in reg(P2)..reg(P2+P3-1) ("B"). Save the result of |
| 1820 | ** the comparison for use by the next OP_Jump instruct. |
| 1821 | ** |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 1822 | ** P4 is a KeyInfo structure that defines collating sequences and sort |
| 1823 | ** orders for the comparison. The permutation applies to registers |
| 1824 | ** only. The KeyInfo elements are used sequentially. |
| 1825 | ** |
| 1826 | ** The comparison is a sort comparison, so NULLs compare equal, |
| 1827 | ** NULLs are less than numbers, numbers are less than strings, |
drh | 16ee60f | 2008-06-20 18:13:25 +0000 | [diff] [blame] | 1828 | ** and strings are less than blobs. |
| 1829 | */ |
| 1830 | case OP_Compare: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1831 | int n; |
| 1832 | int i; |
| 1833 | int p1; |
| 1834 | int p2; |
| 1835 | const KeyInfo *pKeyInfo; |
| 1836 | int idx; |
| 1837 | CollSeq *pColl; /* Collating sequence to use on this term */ |
| 1838 | int bRev; /* True for DESCENDING sort order */ |
| 1839 | |
| 1840 | n = pOp->p3; |
| 1841 | pKeyInfo = pOp->p4.pKeyInfo; |
drh | 16ee60f | 2008-06-20 18:13:25 +0000 | [diff] [blame] | 1842 | assert( n>0 ); |
drh | 93a960a | 2008-07-10 00:32:42 +0000 | [diff] [blame] | 1843 | assert( pKeyInfo!=0 ); |
drh | 16ee60f | 2008-06-20 18:13:25 +0000 | [diff] [blame] | 1844 | p1 = pOp->p1; |
drh | 16ee60f | 2008-06-20 18:13:25 +0000 | [diff] [blame] | 1845 | p2 = pOp->p2; |
drh | 6a2fe09 | 2009-09-23 02:29:36 +0000 | [diff] [blame] | 1846 | #if SQLITE_DEBUG |
| 1847 | if( aPermute ){ |
| 1848 | int k, mx = 0; |
| 1849 | for(k=0; k<n; k++) if( aPermute[k]>mx ) mx = aPermute[k]; |
| 1850 | assert( p1>0 && p1+mx<=p->nMem+1 ); |
| 1851 | assert( p2>0 && p2+mx<=p->nMem+1 ); |
| 1852 | }else{ |
| 1853 | assert( p1>0 && p1+n<=p->nMem+1 ); |
| 1854 | assert( p2>0 && p2+n<=p->nMem+1 ); |
| 1855 | } |
| 1856 | #endif /* SQLITE_DEBUG */ |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 1857 | for(i=0; i<n; i++){ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1858 | idx = aPermute ? aPermute[i] : i; |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 1859 | REGISTER_TRACE(p1+idx, &aMem[p1+idx]); |
| 1860 | REGISTER_TRACE(p2+idx, &aMem[p2+idx]); |
drh | 93a960a | 2008-07-10 00:32:42 +0000 | [diff] [blame] | 1861 | assert( i<pKeyInfo->nField ); |
| 1862 | pColl = pKeyInfo->aColl[i]; |
| 1863 | bRev = pKeyInfo->aSortOrder[i]; |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 1864 | iCompare = sqlite3MemCompare(&aMem[p1+idx], &aMem[p2+idx], pColl); |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 1865 | if( iCompare ){ |
| 1866 | if( bRev ) iCompare = -iCompare; |
| 1867 | break; |
| 1868 | } |
drh | 16ee60f | 2008-06-20 18:13:25 +0000 | [diff] [blame] | 1869 | } |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 1870 | aPermute = 0; |
drh | 16ee60f | 2008-06-20 18:13:25 +0000 | [diff] [blame] | 1871 | break; |
| 1872 | } |
| 1873 | |
| 1874 | /* Opcode: Jump P1 P2 P3 * * |
| 1875 | ** |
| 1876 | ** Jump to the instruction at address P1, P2, or P3 depending on whether |
| 1877 | ** in the most recent OP_Compare instruction the P1 vector was less than |
| 1878 | ** equal to, or greater than the P2 vector, respectively. |
| 1879 | */ |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 1880 | case OP_Jump: { /* jump */ |
| 1881 | if( iCompare<0 ){ |
drh | 16ee60f | 2008-06-20 18:13:25 +0000 | [diff] [blame] | 1882 | pc = pOp->p1 - 1; |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 1883 | }else if( iCompare==0 ){ |
drh | 16ee60f | 2008-06-20 18:13:25 +0000 | [diff] [blame] | 1884 | pc = pOp->p2 - 1; |
| 1885 | }else{ |
| 1886 | pc = pOp->p3 - 1; |
| 1887 | } |
| 1888 | break; |
| 1889 | } |
| 1890 | |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1891 | /* Opcode: And P1 P2 P3 * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1892 | ** |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1893 | ** Take the logical AND of the values in registers P1 and P2 and |
| 1894 | ** write the result into register P3. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1895 | ** |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1896 | ** If either P1 or P2 is 0 (false) then the result is 0 even if |
| 1897 | ** the other input is NULL. A NULL and true or two NULLs give |
| 1898 | ** a NULL output. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1899 | */ |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1900 | /* Opcode: Or P1 P2 P3 * * |
| 1901 | ** |
| 1902 | ** Take the logical OR of the values in register P1 and P2 and |
| 1903 | ** store the answer in register P3. |
| 1904 | ** |
| 1905 | ** If either P1 or P2 is nonzero (true) then the result is 1 (true) |
| 1906 | ** even if the other input is NULL. A NULL and false or two NULLs |
| 1907 | ** give a NULL output. |
| 1908 | */ |
| 1909 | case OP_And: /* same as TK_AND, in1, in2, out3 */ |
| 1910 | case OP_Or: { /* same as TK_OR, in1, in2, out3 */ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1911 | int v1; /* Left operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */ |
| 1912 | int v2; /* Right operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */ |
drh | bb11351 | 2002-05-27 01:04:51 +0000 | [diff] [blame] | 1913 | |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 1914 | pIn1 = &aMem[pOp->p1]; |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1915 | if( pIn1->flags & MEM_Null ){ |
drh | bb11351 | 2002-05-27 01:04:51 +0000 | [diff] [blame] | 1916 | v1 = 2; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1917 | }else{ |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1918 | v1 = sqlite3VdbeIntValue(pIn1)!=0; |
drh | bb11351 | 2002-05-27 01:04:51 +0000 | [diff] [blame] | 1919 | } |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 1920 | pIn2 = &aMem[pOp->p2]; |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1921 | if( pIn2->flags & MEM_Null ){ |
drh | bb11351 | 2002-05-27 01:04:51 +0000 | [diff] [blame] | 1922 | v2 = 2; |
| 1923 | }else{ |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1924 | v2 = sqlite3VdbeIntValue(pIn2)!=0; |
drh | bb11351 | 2002-05-27 01:04:51 +0000 | [diff] [blame] | 1925 | } |
| 1926 | if( pOp->opcode==OP_And ){ |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1927 | 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] | 1928 | v1 = and_logic[v1*3+v2]; |
| 1929 | }else{ |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1930 | 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] | 1931 | v1 = or_logic[v1*3+v2]; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1932 | } |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 1933 | pOut = &aMem[pOp->p3]; |
drh | bb11351 | 2002-05-27 01:04:51 +0000 | [diff] [blame] | 1934 | if( v1==2 ){ |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1935 | MemSetTypeFlag(pOut, MEM_Null); |
drh | bb11351 | 2002-05-27 01:04:51 +0000 | [diff] [blame] | 1936 | }else{ |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1937 | pOut->u.i = v1; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1938 | MemSetTypeFlag(pOut, MEM_Int); |
drh | bb11351 | 2002-05-27 01:04:51 +0000 | [diff] [blame] | 1939 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1940 | break; |
| 1941 | } |
| 1942 | |
drh | e99fa2a | 2008-12-15 15:27:51 +0000 | [diff] [blame] | 1943 | /* Opcode: Not P1 P2 * * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1944 | ** |
drh | e99fa2a | 2008-12-15 15:27:51 +0000 | [diff] [blame] | 1945 | ** Interpret the value in register P1 as a boolean value. Store the |
| 1946 | ** boolean complement in register P2. If the value in register P1 is |
| 1947 | ** NULL, then a NULL is stored in P2. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1948 | */ |
drh | 93952eb | 2009-11-13 19:43:43 +0000 | [diff] [blame] | 1949 | case OP_Not: { /* same as TK_NOT, in1, out2 */ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 1950 | pIn1 = &aMem[pOp->p1]; |
| 1951 | pOut = &aMem[pOp->p2]; |
drh | e99fa2a | 2008-12-15 15:27:51 +0000 | [diff] [blame] | 1952 | if( pIn1->flags & MEM_Null ){ |
| 1953 | sqlite3VdbeMemSetNull(pOut); |
| 1954 | }else{ |
| 1955 | sqlite3VdbeMemSetInt64(pOut, !sqlite3VdbeIntValue(pIn1)); |
| 1956 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1957 | break; |
| 1958 | } |
| 1959 | |
drh | e99fa2a | 2008-12-15 15:27:51 +0000 | [diff] [blame] | 1960 | /* Opcode: BitNot P1 P2 * * * |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1961 | ** |
drh | e99fa2a | 2008-12-15 15:27:51 +0000 | [diff] [blame] | 1962 | ** Interpret the content of register P1 as an integer. Store the |
| 1963 | ** ones-complement of the P1 value into register P2. If P1 holds |
| 1964 | ** a NULL then store a NULL in P2. |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1965 | */ |
drh | 93952eb | 2009-11-13 19:43:43 +0000 | [diff] [blame] | 1966 | case OP_BitNot: { /* same as TK_BITNOT, in1, out2 */ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 1967 | pIn1 = &aMem[pOp->p1]; |
| 1968 | pOut = &aMem[pOp->p2]; |
drh | e99fa2a | 2008-12-15 15:27:51 +0000 | [diff] [blame] | 1969 | if( pIn1->flags & MEM_Null ){ |
| 1970 | sqlite3VdbeMemSetNull(pOut); |
| 1971 | }else{ |
| 1972 | sqlite3VdbeMemSetInt64(pOut, ~sqlite3VdbeIntValue(pIn1)); |
| 1973 | } |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1974 | break; |
| 1975 | } |
| 1976 | |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 1977 | /* Opcode: If P1 P2 P3 * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1978 | ** |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 1979 | ** Jump to P2 if the value in register P1 is true. The value is |
| 1980 | ** is considered true if it is numeric and non-zero. If the value |
| 1981 | ** in P1 is NULL then take the jump if P3 is true. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1982 | */ |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 1983 | /* Opcode: IfNot P1 P2 P3 * * |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1984 | ** |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 1985 | ** Jump to P2 if the value in register P1 is False. The value is |
| 1986 | ** is considered true if it has a numeric value of zero. If the value |
| 1987 | ** in P1 is NULL then take the jump if P3 is true. |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1988 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1989 | case OP_If: /* jump, in1 */ |
| 1990 | case OP_IfNot: { /* jump, in1 */ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1991 | int c; |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 1992 | pIn1 = &aMem[pOp->p1]; |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 1993 | if( pIn1->flags & MEM_Null ){ |
| 1994 | c = pOp->p3; |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1995 | }else{ |
drh | ba0232a | 2005-06-06 17:27:19 +0000 | [diff] [blame] | 1996 | #ifdef SQLITE_OMIT_FLOATING_POINT |
shane | fbd60f8 | 2009-02-04 03:59:25 +0000 | [diff] [blame] | 1997 | c = sqlite3VdbeIntValue(pIn1)!=0; |
drh | ba0232a | 2005-06-06 17:27:19 +0000 | [diff] [blame] | 1998 | #else |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 1999 | c = sqlite3VdbeRealValue(pIn1)!=0.0; |
drh | ba0232a | 2005-06-06 17:27:19 +0000 | [diff] [blame] | 2000 | #endif |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 2001 | if( pOp->opcode==OP_IfNot ) c = !c; |
| 2002 | } |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 2003 | if( c ){ |
| 2004 | pc = pOp->p2-1; |
| 2005 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2006 | break; |
| 2007 | } |
| 2008 | |
drh | 830ecf9 | 2009-06-18 00:41:55 +0000 | [diff] [blame] | 2009 | /* Opcode: IsNull P1 P2 * * * |
drh | 477df4b | 2008-01-05 18:48:24 +0000 | [diff] [blame] | 2010 | ** |
drh | 830ecf9 | 2009-06-18 00:41:55 +0000 | [diff] [blame] | 2011 | ** Jump to P2 if the value in register P1 is NULL. |
drh | 477df4b | 2008-01-05 18:48:24 +0000 | [diff] [blame] | 2012 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2013 | case OP_IsNull: { /* same as TK_ISNULL, jump, in1 */ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 2014 | pIn1 = &aMem[pOp->p1]; |
drh | 830ecf9 | 2009-06-18 00:41:55 +0000 | [diff] [blame] | 2015 | if( (pIn1->flags & MEM_Null)!=0 ){ |
| 2016 | pc = pOp->p2 - 1; |
| 2017 | } |
drh | 477df4b | 2008-01-05 18:48:24 +0000 | [diff] [blame] | 2018 | break; |
| 2019 | } |
| 2020 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 2021 | /* Opcode: NotNull P1 P2 * * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2022 | ** |
drh | 6a288a3 | 2008-01-07 19:20:24 +0000 | [diff] [blame] | 2023 | ** Jump to P2 if the value in register P1 is not NULL. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2024 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2025 | case OP_NotNull: { /* same as TK_NOTNULL, jump, in1 */ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 2026 | pIn1 = &aMem[pOp->p1]; |
drh | 6a288a3 | 2008-01-07 19:20:24 +0000 | [diff] [blame] | 2027 | if( (pIn1->flags & MEM_Null)==0 ){ |
| 2028 | pc = pOp->p2 - 1; |
| 2029 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2030 | break; |
| 2031 | } |
| 2032 | |
drh | 3e9ca09 | 2009-09-08 01:14:48 +0000 | [diff] [blame] | 2033 | /* Opcode: Column P1 P2 P3 P4 P5 |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2034 | ** |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 2035 | ** Interpret the data that cursor P1 points to as a structure built using |
| 2036 | ** the MakeRecord instruction. (See the MakeRecord opcode for additional |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 2037 | ** information about the format of the data.) Extract the P2-th column |
| 2038 | ** from this record. If there are less that (P2+1) |
| 2039 | ** values in the record, extract a NULL. |
| 2040 | ** |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2041 | ** The value extracted is stored in register P3. |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2042 | ** |
danielk1977 | 1f4aa33 | 2008-01-03 09:51:55 +0000 | [diff] [blame] | 2043 | ** If the column contains fewer than P2 fields, then extract a NULL. Or, |
| 2044 | ** if the P4 argument is a P4_MEM use the value of the P4 argument as |
| 2045 | ** the result. |
drh | 3e9ca09 | 2009-09-08 01:14:48 +0000 | [diff] [blame] | 2046 | ** |
| 2047 | ** If the OPFLAG_CLEARCACHE bit is set on P5 and P1 is a pseudo-table cursor, |
| 2048 | ** then the cache of the cursor is reset prior to extracting the column. |
| 2049 | ** The first OP_Column against a pseudo-table after the value of the content |
| 2050 | ** register has changed should have this bit set. |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2051 | */ |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 2052 | case OP_Column: { |
drh | 35cd643 | 2009-06-05 14:17:21 +0000 | [diff] [blame] | 2053 | u32 payloadSize; /* Number of bytes in the record */ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2054 | i64 payloadSize64; /* Number of bytes in the record */ |
| 2055 | int p1; /* P1 value of the opcode */ |
| 2056 | int p2; /* column number to retrieve */ |
| 2057 | VdbeCursor *pC; /* The VDBE cursor */ |
drh | e61cffc | 2004-06-12 18:12:15 +0000 | [diff] [blame] | 2058 | char *zRec; /* Pointer to complete record-data */ |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 2059 | BtCursor *pCrsr; /* The BTree cursor */ |
| 2060 | u32 *aType; /* aType[i] holds the numeric type of the i-th column */ |
| 2061 | u32 *aOffset; /* aOffset[i] is offset to start of data for i-th column */ |
danielk1977 | 64202cf | 2008-11-17 15:31:47 +0000 | [diff] [blame] | 2062 | int nField; /* number of fields in the record */ |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 2063 | int len; /* The length of the serialized data for the column */ |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 2064 | int i; /* Loop counter */ |
| 2065 | char *zData; /* Part of the record being decoded */ |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 2066 | Mem *pDest; /* Where to write the extracted value */ |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 2067 | Mem sMem; /* For storing the record being decoded */ |
drh | 35cd643 | 2009-06-05 14:17:21 +0000 | [diff] [blame] | 2068 | u8 *zIdx; /* Index into header */ |
| 2069 | u8 *zEndHdr; /* Pointer to first byte after the header */ |
| 2070 | u32 offset; /* Offset into the data */ |
drh | 6658cd9 | 2010-02-05 14:12:53 +0000 | [diff] [blame^] | 2071 | u32 szField; /* Number of bytes in the content of a field */ |
drh | 35cd643 | 2009-06-05 14:17:21 +0000 | [diff] [blame] | 2072 | int szHdr; /* Size of the header size field at start of record */ |
| 2073 | int avail; /* Number of bytes of available data */ |
drh | 3e9ca09 | 2009-09-08 01:14:48 +0000 | [diff] [blame] | 2074 | Mem *pReg; /* PseudoTable input register */ |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2075 | |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2076 | |
| 2077 | p1 = pOp->p1; |
| 2078 | p2 = pOp->p2; |
| 2079 | pC = 0; |
drh | b27b7f5 | 2008-12-10 18:03:45 +0000 | [diff] [blame] | 2080 | memset(&sMem, 0, sizeof(sMem)); |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 2081 | assert( p1<p->nCursor ); |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2082 | assert( pOp->p3>0 && pOp->p3<=p->nMem ); |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 2083 | pDest = &aMem[pOp->p3]; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 2084 | MemSetTypeFlag(pDest, MEM_Null); |
shane | 36840fd | 2009-06-26 16:32:13 +0000 | [diff] [blame] | 2085 | zRec = 0; |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 2086 | |
drh | e61cffc | 2004-06-12 18:12:15 +0000 | [diff] [blame] | 2087 | /* This block sets the variable payloadSize to be the total number of |
| 2088 | ** bytes in the record. |
| 2089 | ** |
| 2090 | ** zRec is set to be the complete text of the record if it is available. |
drh | b73857f | 2006-03-17 00:25:59 +0000 | [diff] [blame] | 2091 | ** The complete record text is always available for pseudo-tables |
| 2092 | ** If the record is stored in a cursor, the complete record text |
| 2093 | ** might be available in the pC->aRow cache. Or it might not be. |
| 2094 | ** If the data is unavailable, zRec is set to NULL. |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 2095 | ** |
| 2096 | ** We also compute the number of columns in the record. For cursors, |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 2097 | ** the number of columns is stored in the VdbeCursor.nField element. |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 2098 | */ |
drh | b73857f | 2006-03-17 00:25:59 +0000 | [diff] [blame] | 2099 | pC = p->apCsr[p1]; |
danielk1977 | 6c92409 | 2007-11-12 08:09:34 +0000 | [diff] [blame] | 2100 | assert( pC!=0 ); |
danielk1977 | 0817d0d | 2007-02-14 09:19:36 +0000 | [diff] [blame] | 2101 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 2102 | assert( pC->pVtabCursor==0 ); |
| 2103 | #endif |
shane | 36840fd | 2009-06-26 16:32:13 +0000 | [diff] [blame] | 2104 | pCrsr = pC->pCursor; |
| 2105 | if( pCrsr!=0 ){ |
drh | e61cffc | 2004-06-12 18:12:15 +0000 | [diff] [blame] | 2106 | /* The record is stored in a B-Tree */ |
drh | 536065a | 2005-01-26 21:55:31 +0000 | [diff] [blame] | 2107 | rc = sqlite3VdbeCursorMoveto(pC); |
drh | 52f159e | 2005-01-27 00:33:21 +0000 | [diff] [blame] | 2108 | if( rc ) goto abort_due_to_error; |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2109 | if( pC->nullRow ){ |
| 2110 | payloadSize = 0; |
drh | 76873ab | 2006-01-07 18:48:26 +0000 | [diff] [blame] | 2111 | }else if( pC->cacheStatus==p->cacheCtr ){ |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2112 | payloadSize = pC->payloadSize; |
drh | 2646da7 | 2005-12-09 20:02:05 +0000 | [diff] [blame] | 2113 | zRec = (char*)pC->aRow; |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 2114 | }else if( pC->isIndex ){ |
drh | ea8ffdf | 2009-07-22 00:35:23 +0000 | [diff] [blame] | 2115 | assert( sqlite3BtreeCursorIsValid(pCrsr) ); |
drh | c27ae61 | 2009-07-14 18:35:44 +0000 | [diff] [blame] | 2116 | rc = sqlite3BtreeKeySize(pCrsr, &payloadSize64); |
| 2117 | assert( rc==SQLITE_OK ); /* True because of CursorMoveto() call above */ |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 2118 | /* sqlite3BtreeParseCellPtr() uses getVarint32() to extract the |
| 2119 | ** payload size, so it is impossible for payloadSize64 to be |
| 2120 | ** larger than 32 bits. */ |
| 2121 | assert( (payloadSize64 & SQLITE_MAX_U32)==(u64)payloadSize64 ); |
drh | 35cd643 | 2009-06-05 14:17:21 +0000 | [diff] [blame] | 2122 | payloadSize = (u32)payloadSize64; |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2123 | }else{ |
drh | ea8ffdf | 2009-07-22 00:35:23 +0000 | [diff] [blame] | 2124 | assert( sqlite3BtreeCursorIsValid(pCrsr) ); |
drh | c27ae61 | 2009-07-14 18:35:44 +0000 | [diff] [blame] | 2125 | rc = sqlite3BtreeDataSize(pCrsr, &payloadSize); |
drh | ea8ffdf | 2009-07-22 00:35:23 +0000 | [diff] [blame] | 2126 | assert( rc==SQLITE_OK ); /* DataSize() cannot fail */ |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2127 | } |
drh | 3e9ca09 | 2009-09-08 01:14:48 +0000 | [diff] [blame] | 2128 | }else if( pC->pseudoTableReg>0 ){ |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 2129 | pReg = &aMem[pC->pseudoTableReg]; |
drh | 3e9ca09 | 2009-09-08 01:14:48 +0000 | [diff] [blame] | 2130 | assert( pReg->flags & MEM_Blob ); |
| 2131 | payloadSize = pReg->n; |
| 2132 | zRec = pReg->z; |
| 2133 | pC->cacheStatus = (pOp->p5&OPFLAG_CLEARCACHE) ? CACHE_STALE : p->cacheCtr; |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2134 | assert( payloadSize==0 || zRec!=0 ); |
drh | 9a65f2c | 2009-06-22 19:05:40 +0000 | [diff] [blame] | 2135 | }else{ |
| 2136 | /* Consider the row to be NULL */ |
| 2137 | payloadSize = 0; |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2138 | } |
| 2139 | |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2140 | /* If payloadSize is 0, then just store a NULL */ |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2141 | if( payloadSize==0 ){ |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 2142 | assert( pDest->flags&MEM_Null ); |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 2143 | goto op_column_out; |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2144 | } |
drh | 35cd643 | 2009-06-05 14:17:21 +0000 | [diff] [blame] | 2145 | assert( db->aLimit[SQLITE_LIMIT_LENGTH]>=0 ); |
| 2146 | if( payloadSize > (u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){ |
drh | 023ae03 | 2007-05-08 12:12:16 +0000 | [diff] [blame] | 2147 | goto too_big; |
| 2148 | } |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2149 | |
shane | 36840fd | 2009-06-26 16:32:13 +0000 | [diff] [blame] | 2150 | nField = pC->nField; |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 2151 | assert( p2<nField ); |
danielk1977 | b4964b7 | 2004-05-18 01:23:38 +0000 | [diff] [blame] | 2152 | |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2153 | /* Read and parse the table header. Store the results of the parse |
| 2154 | ** into the record header cache fields of the cursor. |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2155 | */ |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 2156 | aType = pC->aType; |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 2157 | if( pC->cacheStatus==p->cacheCtr ){ |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 2158 | aOffset = pC->aOffset; |
| 2159 | }else{ |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 2160 | assert(aType); |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2161 | avail = 0; |
drh | b73857f | 2006-03-17 00:25:59 +0000 | [diff] [blame] | 2162 | pC->aOffset = aOffset = &aType[nField]; |
| 2163 | pC->payloadSize = payloadSize; |
| 2164 | pC->cacheStatus = p->cacheCtr; |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2165 | |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 2166 | /* Figure out how many bytes are in the header */ |
danielk1977 | 84ac9d0 | 2004-05-18 09:58:06 +0000 | [diff] [blame] | 2167 | if( zRec ){ |
| 2168 | zData = zRec; |
| 2169 | }else{ |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 2170 | if( pC->isIndex ){ |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 2171 | zData = (char*)sqlite3BtreeKeyFetch(pCrsr, &avail); |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 2172 | }else{ |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 2173 | zData = (char*)sqlite3BtreeDataFetch(pCrsr, &avail); |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2174 | } |
drh | e61cffc | 2004-06-12 18:12:15 +0000 | [diff] [blame] | 2175 | /* If KeyFetch()/DataFetch() managed to get the entire payload, |
| 2176 | ** save the payload in the pC->aRow cache. That will save us from |
| 2177 | ** having to make additional calls to fetch the content portion of |
| 2178 | ** the record. |
| 2179 | */ |
drh | 35cd643 | 2009-06-05 14:17:21 +0000 | [diff] [blame] | 2180 | assert( avail>=0 ); |
| 2181 | if( payloadSize <= (u32)avail ){ |
drh | 2646da7 | 2005-12-09 20:02:05 +0000 | [diff] [blame] | 2182 | zRec = zData; |
| 2183 | pC->aRow = (u8*)zData; |
drh | e61cffc | 2004-06-12 18:12:15 +0000 | [diff] [blame] | 2184 | }else{ |
| 2185 | pC->aRow = 0; |
| 2186 | } |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 2187 | } |
drh | 588f5bc | 2007-01-02 18:41:54 +0000 | [diff] [blame] | 2188 | /* The following assert is true in all cases accept when |
| 2189 | ** the database file has been corrupted externally. |
| 2190 | ** assert( zRec!=0 || avail>=payloadSize || avail>=9 ); */ |
drh | 35cd643 | 2009-06-05 14:17:21 +0000 | [diff] [blame] | 2191 | szHdr = getVarint32((u8*)zData, offset); |
| 2192 | |
| 2193 | /* Make sure a corrupt database has not given us an oversize header. |
| 2194 | ** Do this now to avoid an oversize memory allocation. |
| 2195 | ** |
| 2196 | ** Type entries can be between 1 and 5 bytes each. But 4 and 5 byte |
| 2197 | ** types use so much data space that there can only be 4096 and 32 of |
| 2198 | ** them, respectively. So the maximum header length results from a |
| 2199 | ** 3-byte type for each of the maximum of 32768 columns plus three |
| 2200 | ** extra bytes for the header length itself. 32768*3 + 3 = 98307. |
| 2201 | */ |
| 2202 | if( offset > 98307 ){ |
| 2203 | rc = SQLITE_CORRUPT_BKPT; |
| 2204 | goto op_column_out; |
| 2205 | } |
| 2206 | |
| 2207 | /* Compute in len the number of bytes of data we need to read in order |
| 2208 | ** to get nField type values. offset is an upper bound on this. But |
| 2209 | ** nField might be significantly less than the true number of columns |
| 2210 | ** in the table, and in that case, 5*nField+3 might be smaller than offset. |
| 2211 | ** We want to minimize len in order to limit the size of the memory |
| 2212 | ** allocation, especially if a corrupt database file has caused offset |
| 2213 | ** to be oversized. Offset is limited to 98307 above. But 98307 might |
| 2214 | ** still exceed Robson memory allocation limits on some configurations. |
| 2215 | ** On systems that cannot tolerate large memory allocations, nField*5+3 |
| 2216 | ** will likely be much smaller since nField will likely be less than |
| 2217 | ** 20 or so. This insures that Robson memory allocation limits are |
| 2218 | ** not exceeded even for corrupt database files. |
| 2219 | */ |
| 2220 | len = nField*5 + 3; |
shane | 75ac1de | 2009-06-09 18:58:52 +0000 | [diff] [blame] | 2221 | if( len > (int)offset ) len = (int)offset; |
drh | e61cffc | 2004-06-12 18:12:15 +0000 | [diff] [blame] | 2222 | |
| 2223 | /* The KeyFetch() or DataFetch() above are fast and will get the entire |
| 2224 | ** record header in most cases. But they will fail to get the complete |
| 2225 | ** record header if the record header does not fit on a single page |
| 2226 | ** in the B-Tree. When that happens, use sqlite3VdbeMemFromBtree() to |
| 2227 | ** acquire the complete header text. |
| 2228 | */ |
drh | 35cd643 | 2009-06-05 14:17:21 +0000 | [diff] [blame] | 2229 | if( !zRec && avail<len ){ |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 2230 | sMem.flags = 0; |
| 2231 | sMem.db = 0; |
drh | 35cd643 | 2009-06-05 14:17:21 +0000 | [diff] [blame] | 2232 | rc = sqlite3VdbeMemFromBtree(pCrsr, 0, len, pC->isIndex, &sMem); |
danielk1977 | 84ac9d0 | 2004-05-18 09:58:06 +0000 | [diff] [blame] | 2233 | if( rc!=SQLITE_OK ){ |
danielk1977 | 3c9cc8d | 2005-01-17 03:40:08 +0000 | [diff] [blame] | 2234 | goto op_column_out; |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2235 | } |
drh | b6f5452 | 2004-05-20 02:42:16 +0000 | [diff] [blame] | 2236 | zData = sMem.z; |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2237 | } |
drh | 35cd643 | 2009-06-05 14:17:21 +0000 | [diff] [blame] | 2238 | zEndHdr = (u8 *)&zData[len]; |
| 2239 | zIdx = (u8 *)&zData[szHdr]; |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2240 | |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 2241 | /* Scan the header and use it to fill in the aType[] and aOffset[] |
| 2242 | ** arrays. aType[i] will contain the type integer for the i-th |
| 2243 | ** column and aOffset[i] will contain the offset from the beginning |
| 2244 | ** of the record to the start of the data for the i-th column |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2245 | */ |
danielk1977 | dedf45b | 2006-01-13 17:12:01 +0000 | [diff] [blame] | 2246 | for(i=0; i<nField; i++){ |
| 2247 | if( zIdx<zEndHdr ){ |
drh | 6658cd9 | 2010-02-05 14:12:53 +0000 | [diff] [blame^] | 2248 | aOffset[i] = offset; |
shane | 3f8d5cf | 2008-04-24 19:15:09 +0000 | [diff] [blame] | 2249 | zIdx += getVarint32(zIdx, aType[i]); |
drh | 6658cd9 | 2010-02-05 14:12:53 +0000 | [diff] [blame^] | 2250 | szField = sqlite3VdbeSerialTypeLen(aType[i]); |
| 2251 | offset += szField; |
| 2252 | if( offset<szField ){ /* True if offset overflows */ |
| 2253 | zIdx = &zEndHdr[1]; /* Forces SQLITE_CORRUPT return below */ |
| 2254 | break; |
| 2255 | } |
danielk1977 | dedf45b | 2006-01-13 17:12:01 +0000 | [diff] [blame] | 2256 | }else{ |
| 2257 | /* If i is less that nField, then there are less fields in this |
| 2258 | ** record than SetNumColumns indicated there are columns in the |
| 2259 | ** table. Set the offset for any extra columns not present in |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2260 | ** the record to 0. This tells code below to store a NULL |
| 2261 | ** instead of deserializing a value from the record. |
danielk1977 | dedf45b | 2006-01-13 17:12:01 +0000 | [diff] [blame] | 2262 | */ |
| 2263 | aOffset[i] = 0; |
| 2264 | } |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2265 | } |
danielk1977 | 5f09613 | 2008-03-28 15:44:09 +0000 | [diff] [blame] | 2266 | sqlite3VdbeMemRelease(&sMem); |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 2267 | sMem.flags = MEM_Null; |
| 2268 | |
danielk1977 | 9792eef | 2006-01-13 15:58:43 +0000 | [diff] [blame] | 2269 | /* If we have read more header data than was contained in the header, |
| 2270 | ** or if the end of the last field appears to be past the end of the |
shane | 2ca8bc0 | 2008-05-07 18:59:28 +0000 | [diff] [blame] | 2271 | ** record, or if the end of the last field appears to be before the end |
| 2272 | ** of the record (when all fields present), then we must be dealing |
| 2273 | ** with a corrupt database. |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 2274 | */ |
drh | 6658cd9 | 2010-02-05 14:12:53 +0000 | [diff] [blame^] | 2275 | if( (zIdx > zEndHdr) || (offset > payloadSize) |
| 2276 | || (zIdx==zEndHdr && offset!=payloadSize) ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 2277 | rc = SQLITE_CORRUPT_BKPT; |
danielk1977 | 3c9cc8d | 2005-01-17 03:40:08 +0000 | [diff] [blame] | 2278 | goto op_column_out; |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 2279 | } |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 2280 | } |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2281 | |
danielk1977 | 36963fd | 2005-02-19 08:18:05 +0000 | [diff] [blame] | 2282 | /* Get the column information. If aOffset[p2] is non-zero, then |
| 2283 | ** deserialize the value from the record. If aOffset[p2] is zero, |
| 2284 | ** then there are not enough fields in the record to satisfy the |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 2285 | ** request. In this case, set the value NULL or to P4 if P4 is |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 2286 | ** a pointer to a Mem object. |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2287 | */ |
danielk1977 | 36963fd | 2005-02-19 08:18:05 +0000 | [diff] [blame] | 2288 | if( aOffset[p2] ){ |
| 2289 | assert( rc==SQLITE_OK ); |
| 2290 | if( zRec ){ |
danielk1977 | 808ec7c | 2008-07-29 10:18:57 +0000 | [diff] [blame] | 2291 | sqlite3VdbeMemReleaseExternal(pDest); |
| 2292 | sqlite3VdbeSerialGet((u8 *)&zRec[aOffset[p2]], aType[p2], pDest); |
danielk1977 | 36963fd | 2005-02-19 08:18:05 +0000 | [diff] [blame] | 2293 | }else{ |
| 2294 | len = sqlite3VdbeSerialTypeLen(aType[p2]); |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 2295 | sqlite3VdbeMemMove(&sMem, pDest); |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 2296 | rc = sqlite3VdbeMemFromBtree(pCrsr, aOffset[p2], len, pC->isIndex, &sMem); |
danielk1977 | 36963fd | 2005-02-19 08:18:05 +0000 | [diff] [blame] | 2297 | if( rc!=SQLITE_OK ){ |
| 2298 | goto op_column_out; |
| 2299 | } |
| 2300 | zData = sMem.z; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 2301 | sqlite3VdbeSerialGet((u8*)zData, aType[p2], pDest); |
danielk1977 | 7701e81 | 2005-01-10 12:59:51 +0000 | [diff] [blame] | 2302 | } |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 2303 | pDest->enc = encoding; |
danielk1977 | 36963fd | 2005-02-19 08:18:05 +0000 | [diff] [blame] | 2304 | }else{ |
danielk1977 | 60585dd | 2008-01-03 08:08:40 +0000 | [diff] [blame] | 2305 | if( pOp->p4type==P4_MEM ){ |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 2306 | sqlite3VdbeMemShallowCopy(pDest, pOp->p4.pMem, MEM_Static); |
danielk1977 | aee18ef | 2005-03-09 12:26:50 +0000 | [diff] [blame] | 2307 | }else{ |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 2308 | assert( pDest->flags&MEM_Null ); |
danielk1977 | aee18ef | 2005-03-09 12:26:50 +0000 | [diff] [blame] | 2309 | } |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 2310 | } |
drh | febe106 | 2004-08-28 18:17:48 +0000 | [diff] [blame] | 2311 | |
| 2312 | /* If we dynamically allocated space to hold the data (in the |
| 2313 | ** sqlite3VdbeMemFromBtree() call above) then transfer control of that |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 2314 | ** dynamically allocated space over to the pDest structure. |
drh | febe106 | 2004-08-28 18:17:48 +0000 | [diff] [blame] | 2315 | ** This prevents a memory copy. |
| 2316 | */ |
danielk1977 | 5f09613 | 2008-03-28 15:44:09 +0000 | [diff] [blame] | 2317 | if( sMem.zMalloc ){ |
| 2318 | assert( sMem.z==sMem.zMalloc ); |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 2319 | assert( !(pDest->flags & MEM_Dyn) ); |
| 2320 | assert( !(pDest->flags & (MEM_Blob|MEM_Str)) || pDest->z==sMem.z ); |
| 2321 | pDest->flags &= ~(MEM_Ephem|MEM_Static); |
danielk1977 | 5f09613 | 2008-03-28 15:44:09 +0000 | [diff] [blame] | 2322 | pDest->flags |= MEM_Term; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 2323 | pDest->z = sMem.z; |
danielk1977 | 5f09613 | 2008-03-28 15:44:09 +0000 | [diff] [blame] | 2324 | pDest->zMalloc = sMem.zMalloc; |
danielk1977 | b1bc953 | 2004-05-22 03:05:33 +0000 | [diff] [blame] | 2325 | } |
drh | febe106 | 2004-08-28 18:17:48 +0000 | [diff] [blame] | 2326 | |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 2327 | rc = sqlite3VdbeMemMakeWriteable(pDest); |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 2328 | |
danielk1977 | 3c9cc8d | 2005-01-17 03:40:08 +0000 | [diff] [blame] | 2329 | op_column_out: |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 2330 | UPDATE_MAX_BLOBSIZE(pDest); |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 2331 | REGISTER_TRACE(pOp->p3, pDest); |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2332 | break; |
| 2333 | } |
| 2334 | |
danielk1977 | 751de56 | 2008-04-18 09:01:15 +0000 | [diff] [blame] | 2335 | /* Opcode: Affinity P1 P2 * P4 * |
| 2336 | ** |
| 2337 | ** Apply affinities to a range of P2 registers starting with P1. |
| 2338 | ** |
| 2339 | ** P4 is a string that is P2 characters long. The nth character of the |
| 2340 | ** string indicates the column affinity that should be used for the nth |
| 2341 | ** memory cell in the range. |
| 2342 | */ |
| 2343 | case OP_Affinity: { |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 2344 | const char *zAffinity; /* The affinity to be applied */ |
| 2345 | char cAff; /* A single character of affinity */ |
danielk1977 | 751de56 | 2008-04-18 09:01:15 +0000 | [diff] [blame] | 2346 | |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2347 | zAffinity = pOp->p4.z; |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 2348 | assert( zAffinity!=0 ); |
| 2349 | assert( zAffinity[pOp->p2]==0 ); |
| 2350 | pIn1 = &aMem[pOp->p1]; |
| 2351 | while( (cAff = *(zAffinity++))!=0 ){ |
| 2352 | assert( pIn1 <= &p->aMem[p->nMem] ); |
| 2353 | ExpandBlob(pIn1); |
| 2354 | applyAffinity(pIn1, cAff, encoding); |
| 2355 | pIn1++; |
danielk1977 | 751de56 | 2008-04-18 09:01:15 +0000 | [diff] [blame] | 2356 | } |
| 2357 | break; |
| 2358 | } |
| 2359 | |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2360 | /* Opcode: MakeRecord P1 P2 P3 P4 * |
drh | 7a224de | 2004-06-02 01:22:02 +0000 | [diff] [blame] | 2361 | ** |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2362 | ** Convert P2 registers beginning with P1 into a single entry |
drh | 7a224de | 2004-06-02 01:22:02 +0000 | [diff] [blame] | 2363 | ** suitable for use as a data record in a database table or as a key |
shane | 21e7feb | 2008-05-30 15:59:49 +0000 | [diff] [blame] | 2364 | ** in an index. The details of the format are irrelevant as long as |
drh | 1e968a0 | 2008-03-25 00:22:21 +0000 | [diff] [blame] | 2365 | ** the OP_Column opcode can decode the record later. |
| 2366 | ** Refer to source code comments for the details of the record |
drh | 7a224de | 2004-06-02 01:22:02 +0000 | [diff] [blame] | 2367 | ** format. |
| 2368 | ** |
danielk1977 | 751de56 | 2008-04-18 09:01:15 +0000 | [diff] [blame] | 2369 | ** 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] | 2370 | ** string indicates the column affinity that should be used for the nth |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2371 | ** field of the index key. |
drh | 7a224de | 2004-06-02 01:22:02 +0000 | [diff] [blame] | 2372 | ** |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 2373 | ** The mapping from character to affinity is given by the SQLITE_AFF_ |
| 2374 | ** macros defined in sqliteInt.h. |
drh | 7a224de | 2004-06-02 01:22:02 +0000 | [diff] [blame] | 2375 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 2376 | ** If P4 is NULL then all index fields have the affinity NONE. |
drh | 7f057c9 | 2005-06-24 03:53:06 +0000 | [diff] [blame] | 2377 | */ |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2378 | case OP_MakeRecord: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2379 | u8 *zNewRecord; /* A buffer to hold the data for the new record */ |
| 2380 | Mem *pRec; /* The new record */ |
| 2381 | u64 nData; /* Number of bytes of data space */ |
| 2382 | int nHdr; /* Number of bytes of header space */ |
| 2383 | i64 nByte; /* Data space required for this record */ |
| 2384 | int nZero; /* Number of zero bytes at the end of the record */ |
| 2385 | int nVarint; /* Number of bytes in a varint */ |
| 2386 | u32 serial_type; /* Type field */ |
| 2387 | Mem *pData0; /* First field to be combined into the record */ |
| 2388 | Mem *pLast; /* Last field of the record */ |
| 2389 | int nField; /* Number of fields in the record */ |
| 2390 | char *zAffinity; /* The affinity string for the record */ |
| 2391 | int file_format; /* File format to use for encoding */ |
| 2392 | int i; /* Space used in zNewRecord[] */ |
| 2393 | int len; /* Length of a field */ |
| 2394 | |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 2395 | /* Assuming the record contains N fields, the record format looks |
| 2396 | ** like this: |
| 2397 | ** |
drh | 7a224de | 2004-06-02 01:22:02 +0000 | [diff] [blame] | 2398 | ** ------------------------------------------------------------------------ |
| 2399 | ** | hdr-size | type 0 | type 1 | ... | type N-1 | data0 | ... | data N-1 | |
| 2400 | ** ------------------------------------------------------------------------ |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 2401 | ** |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2402 | ** Data(0) is taken from register P1. Data(1) comes from register P1+1 |
| 2403 | ** and so froth. |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 2404 | ** |
| 2405 | ** Each type field is a varint representing the serial type of the |
| 2406 | ** corresponding data element (see sqlite3VdbeSerialType()). The |
drh | 7a224de | 2004-06-02 01:22:02 +0000 | [diff] [blame] | 2407 | ** hdr-size field is also a varint which is the offset from the beginning |
| 2408 | ** of the record to data0. |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 2409 | */ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2410 | nData = 0; /* Number of bytes of data space */ |
| 2411 | nHdr = 0; /* Number of bytes of header space */ |
| 2412 | nByte = 0; /* Data space required for this record */ |
| 2413 | nZero = 0; /* Number of zero bytes at the end of the record */ |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2414 | nField = pOp->p1; |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 2415 | zAffinity = pOp->p4.z; |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 2416 | assert( nField>0 && pOp->p2>0 && pOp->p2+nField<=p->nMem+1 ); |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 2417 | pData0 = &aMem[nField]; |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2418 | nField = pOp->p2; |
| 2419 | pLast = &pData0[nField-1]; |
drh | d946db0 | 2005-12-29 19:23:06 +0000 | [diff] [blame] | 2420 | file_format = p->minWriteFileFormat; |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 2421 | |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 2422 | /* Loop through the elements that will make up the record to figure |
| 2423 | ** out how much space is required for the new record. |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 2424 | */ |
drh | a2a49dc | 2008-01-02 14:28:13 +0000 | [diff] [blame] | 2425 | for(pRec=pData0; pRec<=pLast; pRec++){ |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 2426 | if( zAffinity ){ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 2427 | applyAffinity(pRec, zAffinity[pRec-pData0], encoding); |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 2428 | } |
danielk1977 | d908f5a | 2007-05-11 07:08:28 +0000 | [diff] [blame] | 2429 | if( pRec->flags&MEM_Zero && pRec->n>0 ){ |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 2430 | sqlite3VdbeMemExpandBlob(pRec); |
danielk1977 | d908f5a | 2007-05-11 07:08:28 +0000 | [diff] [blame] | 2431 | } |
drh | d946db0 | 2005-12-29 19:23:06 +0000 | [diff] [blame] | 2432 | serial_type = sqlite3VdbeSerialType(pRec, file_format); |
drh | ae7e151 | 2007-05-02 16:51:59 +0000 | [diff] [blame] | 2433 | len = sqlite3VdbeSerialTypeLen(serial_type); |
| 2434 | nData += len; |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 2435 | nHdr += sqlite3VarintLen(serial_type); |
drh | fdf972a | 2007-05-02 13:30:27 +0000 | [diff] [blame] | 2436 | if( pRec->flags & MEM_Zero ){ |
| 2437 | /* Only pure zero-filled BLOBs can be input to this Opcode. |
| 2438 | ** We do not allow blobs with a prefix and a zero-filled tail. */ |
drh | 8df3284 | 2008-12-09 02:51:23 +0000 | [diff] [blame] | 2439 | nZero += pRec->u.nZero; |
drh | ae7e151 | 2007-05-02 16:51:59 +0000 | [diff] [blame] | 2440 | }else if( len ){ |
drh | fdf972a | 2007-05-02 13:30:27 +0000 | [diff] [blame] | 2441 | nZero = 0; |
| 2442 | } |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 2443 | } |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 2444 | |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 2445 | /* Add the initial header varint and total the size */ |
drh | cb9882a | 2005-03-17 03:15:40 +0000 | [diff] [blame] | 2446 | nHdr += nVarint = sqlite3VarintLen(nHdr); |
| 2447 | if( nVarint<sqlite3VarintLen(nHdr) ){ |
| 2448 | nHdr++; |
| 2449 | } |
drh | fdf972a | 2007-05-02 13:30:27 +0000 | [diff] [blame] | 2450 | nByte = nHdr+nData-nZero; |
drh | bb4957f | 2008-03-20 14:03:29 +0000 | [diff] [blame] | 2451 | if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){ |
drh | 023ae03 | 2007-05-08 12:12:16 +0000 | [diff] [blame] | 2452 | goto too_big; |
| 2453 | } |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 2454 | |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 2455 | /* Make sure the output register has a buffer large enough to store |
| 2456 | ** the new record. The output register (pOp->p3) is not allowed to |
| 2457 | ** be one of the input registers (because the following call to |
| 2458 | ** sqlite3VdbeMemGrow() could clobber the value before it is used). |
| 2459 | */ |
| 2460 | assert( pOp->p3<pOp->p1 || pOp->p3>=pOp->p1+pOp->p2 ); |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 2461 | pOut = &aMem[pOp->p3]; |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 2462 | if( sqlite3VdbeMemGrow(pOut, (int)nByte, 0) ){ |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 2463 | goto no_mem; |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 2464 | } |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 2465 | zNewRecord = (u8 *)pOut->z; |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 2466 | |
| 2467 | /* Write the record */ |
shane | 3f8d5cf | 2008-04-24 19:15:09 +0000 | [diff] [blame] | 2468 | i = putVarint32(zNewRecord, nHdr); |
drh | a2a49dc | 2008-01-02 14:28:13 +0000 | [diff] [blame] | 2469 | for(pRec=pData0; pRec<=pLast; pRec++){ |
drh | d946db0 | 2005-12-29 19:23:06 +0000 | [diff] [blame] | 2470 | serial_type = sqlite3VdbeSerialType(pRec, file_format); |
shane | 3f8d5cf | 2008-04-24 19:15:09 +0000 | [diff] [blame] | 2471 | i += putVarint32(&zNewRecord[i], serial_type); /* serial type */ |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 2472 | } |
drh | a2a49dc | 2008-01-02 14:28:13 +0000 | [diff] [blame] | 2473 | for(pRec=pData0; pRec<=pLast; pRec++){ /* serial data */ |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 2474 | i += sqlite3VdbeSerialPut(&zNewRecord[i], (int)(nByte-i), pRec,file_format); |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 2475 | } |
drh | fdf972a | 2007-05-02 13:30:27 +0000 | [diff] [blame] | 2476 | assert( i==nByte ); |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 2477 | |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2478 | assert( pOp->p3>0 && pOp->p3<=p->nMem ); |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 2479 | pOut->n = (int)nByte; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 2480 | pOut->flags = MEM_Blob | MEM_Dyn; |
| 2481 | pOut->xDel = 0; |
drh | fdf972a | 2007-05-02 13:30:27 +0000 | [diff] [blame] | 2482 | if( nZero ){ |
drh | 8df3284 | 2008-12-09 02:51:23 +0000 | [diff] [blame] | 2483 | pOut->u.nZero = nZero; |
drh | 477df4b | 2008-01-05 18:48:24 +0000 | [diff] [blame] | 2484 | pOut->flags |= MEM_Zero; |
drh | fdf972a | 2007-05-02 13:30:27 +0000 | [diff] [blame] | 2485 | } |
drh | 477df4b | 2008-01-05 18:48:24 +0000 | [diff] [blame] | 2486 | pOut->enc = SQLITE_UTF8; /* In case the blob is ever converted to text */ |
drh | 1013c93 | 2008-01-06 00:25:21 +0000 | [diff] [blame] | 2487 | REGISTER_TRACE(pOp->p3, pOut); |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 2488 | UPDATE_MAX_BLOBSIZE(pOut); |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 2489 | break; |
| 2490 | } |
| 2491 | |
danielk1977 | a553316 | 2009-02-24 10:01:51 +0000 | [diff] [blame] | 2492 | /* Opcode: Count P1 P2 * * * |
| 2493 | ** |
| 2494 | ** Store the number of entries (an integer value) in the table or index |
| 2495 | ** opened by cursor P1 in register P2 |
| 2496 | */ |
| 2497 | #ifndef SQLITE_OMIT_BTREECOUNT |
| 2498 | case OP_Count: { /* out2-prerelease */ |
| 2499 | i64 nEntry; |
drh | c54a617 | 2009-06-02 16:06:03 +0000 | [diff] [blame] | 2500 | BtCursor *pCrsr; |
| 2501 | |
| 2502 | pCrsr = p->apCsr[pOp->p1]->pCursor; |
drh | 818e39a | 2009-04-02 20:27:28 +0000 | [diff] [blame] | 2503 | if( pCrsr ){ |
| 2504 | rc = sqlite3BtreeCount(pCrsr, &nEntry); |
| 2505 | }else{ |
| 2506 | nEntry = 0; |
| 2507 | } |
danielk1977 | a553316 | 2009-02-24 10:01:51 +0000 | [diff] [blame] | 2508 | pOut->u.i = nEntry; |
| 2509 | break; |
| 2510 | } |
| 2511 | #endif |
| 2512 | |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2513 | /* Opcode: Savepoint P1 * * P4 * |
| 2514 | ** |
| 2515 | ** Open, release or rollback the savepoint named by parameter P4, depending |
| 2516 | ** on the value of P1. To open a new savepoint, P1==0. To release (commit) an |
| 2517 | ** existing savepoint, P1==1, or to rollback an existing savepoint P1==2. |
| 2518 | */ |
| 2519 | case OP_Savepoint: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2520 | int p1; /* Value of P1 operand */ |
| 2521 | char *zName; /* Name of savepoint */ |
| 2522 | int nName; |
| 2523 | Savepoint *pNew; |
| 2524 | Savepoint *pSavepoint; |
| 2525 | Savepoint *pTmp; |
| 2526 | int iSavepoint; |
| 2527 | int ii; |
| 2528 | |
| 2529 | p1 = pOp->p1; |
| 2530 | zName = pOp->p4.z; |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2531 | |
| 2532 | /* Assert that the p1 parameter is valid. Also that if there is no open |
| 2533 | ** transaction, then there cannot be any savepoints. |
| 2534 | */ |
| 2535 | assert( db->pSavepoint==0 || db->autoCommit==0 ); |
| 2536 | assert( p1==SAVEPOINT_BEGIN||p1==SAVEPOINT_RELEASE||p1==SAVEPOINT_ROLLBACK ); |
| 2537 | assert( db->pSavepoint || db->isTransactionSavepoint==0 ); |
| 2538 | assert( checkSavepointCount(db) ); |
| 2539 | |
| 2540 | if( p1==SAVEPOINT_BEGIN ){ |
danielk1977 | 34cf35d | 2008-12-18 18:31:38 +0000 | [diff] [blame] | 2541 | if( db->writeVdbeCnt>0 ){ |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2542 | /* A new savepoint cannot be created if there are active write |
| 2543 | ** statements (i.e. open read/write incremental blob handles). |
| 2544 | */ |
| 2545 | sqlite3SetString(&p->zErrMsg, db, "cannot open savepoint - " |
| 2546 | "SQL statements in progress"); |
| 2547 | rc = SQLITE_BUSY; |
| 2548 | }else{ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2549 | nName = sqlite3Strlen30(zName); |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2550 | |
| 2551 | /* Create a new savepoint structure. */ |
| 2552 | pNew = sqlite3DbMallocRaw(db, sizeof(Savepoint)+nName+1); |
| 2553 | if( pNew ){ |
| 2554 | pNew->zName = (char *)&pNew[1]; |
| 2555 | memcpy(pNew->zName, zName, nName+1); |
| 2556 | |
| 2557 | /* If there is no open transaction, then mark this as a special |
| 2558 | ** "transaction savepoint". */ |
| 2559 | if( db->autoCommit ){ |
| 2560 | db->autoCommit = 0; |
| 2561 | db->isTransactionSavepoint = 1; |
| 2562 | }else{ |
| 2563 | db->nSavepoint++; |
danielk1977 | d829335 | 2009-04-30 09:10:37 +0000 | [diff] [blame] | 2564 | } |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2565 | |
| 2566 | /* Link the new savepoint into the database handle's list. */ |
| 2567 | pNew->pNext = db->pSavepoint; |
| 2568 | db->pSavepoint = pNew; |
dan | ba9108b | 2009-09-22 07:13:42 +0000 | [diff] [blame] | 2569 | pNew->nDeferredCons = db->nDeferredCons; |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2570 | } |
| 2571 | } |
| 2572 | }else{ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2573 | iSavepoint = 0; |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2574 | |
| 2575 | /* Find the named savepoint. If there is no such savepoint, then an |
| 2576 | ** an error is returned to the user. */ |
| 2577 | for( |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2578 | pSavepoint = db->pSavepoint; |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2579 | pSavepoint && sqlite3StrICmp(pSavepoint->zName, zName); |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2580 | pSavepoint = pSavepoint->pNext |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2581 | ){ |
| 2582 | iSavepoint++; |
| 2583 | } |
| 2584 | if( !pSavepoint ){ |
| 2585 | sqlite3SetString(&p->zErrMsg, db, "no such savepoint: %s", zName); |
| 2586 | rc = SQLITE_ERROR; |
| 2587 | }else if( |
| 2588 | db->writeVdbeCnt>0 || (p1==SAVEPOINT_ROLLBACK && db->activeVdbeCnt>1) |
| 2589 | ){ |
| 2590 | /* It is not possible to release (commit) a savepoint if there are |
| 2591 | ** active write statements. It is not possible to rollback a savepoint |
| 2592 | ** if there are any active statements at all. |
| 2593 | */ |
| 2594 | sqlite3SetString(&p->zErrMsg, db, |
| 2595 | "cannot %s savepoint - SQL statements in progress", |
| 2596 | (p1==SAVEPOINT_ROLLBACK ? "rollback": "release") |
| 2597 | ); |
| 2598 | rc = SQLITE_BUSY; |
| 2599 | }else{ |
| 2600 | |
| 2601 | /* Determine whether or not this is a transaction savepoint. If so, |
danielk1977 | 34cf35d | 2008-12-18 18:31:38 +0000 | [diff] [blame] | 2602 | ** and this is a RELEASE command, then the current transaction |
| 2603 | ** is committed. |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2604 | */ |
| 2605 | int isTransaction = pSavepoint->pNext==0 && db->isTransactionSavepoint; |
| 2606 | if( isTransaction && p1==SAVEPOINT_RELEASE ){ |
dan | 32b09f2 | 2009-09-23 17:29:59 +0000 | [diff] [blame] | 2607 | if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){ |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 2608 | goto vdbe_return; |
| 2609 | } |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2610 | db->autoCommit = 1; |
| 2611 | if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){ |
| 2612 | p->pc = pc; |
| 2613 | db->autoCommit = 0; |
| 2614 | p->rc = rc = SQLITE_BUSY; |
| 2615 | goto vdbe_return; |
| 2616 | } |
danielk1977 | 34cf35d | 2008-12-18 18:31:38 +0000 | [diff] [blame] | 2617 | db->isTransactionSavepoint = 0; |
| 2618 | rc = p->rc; |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2619 | }else{ |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2620 | iSavepoint = db->nSavepoint - iSavepoint - 1; |
| 2621 | for(ii=0; ii<db->nDb; ii++){ |
| 2622 | rc = sqlite3BtreeSavepoint(db->aDb[ii].pBt, p1, iSavepoint); |
| 2623 | if( rc!=SQLITE_OK ){ |
| 2624 | goto abort_due_to_error; |
danielk1977 | bd43455 | 2009-03-18 10:33:00 +0000 | [diff] [blame] | 2625 | } |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2626 | } |
drh | 9f0bbf9 | 2009-01-02 21:08:09 +0000 | [diff] [blame] | 2627 | if( p1==SAVEPOINT_ROLLBACK && (db->flags&SQLITE_InternChanges)!=0 ){ |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2628 | sqlite3ExpirePreparedStatements(db); |
| 2629 | sqlite3ResetInternalSchema(db, 0); |
| 2630 | } |
| 2631 | } |
| 2632 | |
| 2633 | /* Regardless of whether this is a RELEASE or ROLLBACK, destroy all |
| 2634 | ** savepoints nested inside of the savepoint being operated on. */ |
| 2635 | while( db->pSavepoint!=pSavepoint ){ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2636 | pTmp = db->pSavepoint; |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2637 | db->pSavepoint = pTmp->pNext; |
| 2638 | sqlite3DbFree(db, pTmp); |
| 2639 | db->nSavepoint--; |
| 2640 | } |
| 2641 | |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 2642 | /* If it is a RELEASE, then destroy the savepoint being operated on |
| 2643 | ** too. If it is a ROLLBACK TO, then set the number of deferred |
| 2644 | ** constraint violations present in the database to the value stored |
| 2645 | ** when the savepoint was created. */ |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2646 | if( p1==SAVEPOINT_RELEASE ){ |
| 2647 | assert( pSavepoint==db->pSavepoint ); |
| 2648 | db->pSavepoint = pSavepoint->pNext; |
| 2649 | sqlite3DbFree(db, pSavepoint); |
| 2650 | if( !isTransaction ){ |
| 2651 | db->nSavepoint--; |
| 2652 | } |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 2653 | }else{ |
| 2654 | db->nDeferredCons = pSavepoint->nDeferredCons; |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2655 | } |
| 2656 | } |
| 2657 | } |
| 2658 | |
| 2659 | break; |
| 2660 | } |
| 2661 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 2662 | /* Opcode: AutoCommit P1 P2 * * * |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 2663 | ** |
| 2664 | ** 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] | 2665 | ** back any currently active btree transactions. If there are any active |
drh | c25eabe | 2009-02-24 18:57:31 +0000 | [diff] [blame] | 2666 | ** VMs (apart from this one), then a ROLLBACK fails. A COMMIT fails if |
| 2667 | ** there are active writing VMs or active VMs that use shared cache. |
drh | 92f02c3 | 2004-09-02 14:57:08 +0000 | [diff] [blame] | 2668 | ** |
| 2669 | ** This instruction causes the VM to halt. |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 2670 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2671 | case OP_AutoCommit: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2672 | int desiredAutoCommit; |
shane | 68c0273 | 2009-06-09 18:14:18 +0000 | [diff] [blame] | 2673 | int iRollback; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2674 | int turnOnAC; |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 2675 | |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2676 | desiredAutoCommit = pOp->p1; |
shane | 68c0273 | 2009-06-09 18:14:18 +0000 | [diff] [blame] | 2677 | iRollback = pOp->p2; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2678 | turnOnAC = desiredAutoCommit && !db->autoCommit; |
drh | ad4a4b8 | 2008-11-05 16:37:34 +0000 | [diff] [blame] | 2679 | assert( desiredAutoCommit==1 || desiredAutoCommit==0 ); |
shane | 68c0273 | 2009-06-09 18:14:18 +0000 | [diff] [blame] | 2680 | assert( desiredAutoCommit==1 || iRollback==0 ); |
drh | 92f02c3 | 2004-09-02 14:57:08 +0000 | [diff] [blame] | 2681 | assert( db->activeVdbeCnt>0 ); /* At least this one VM is active */ |
danielk1977 | 46c43ed | 2004-06-30 06:30:25 +0000 | [diff] [blame] | 2682 | |
shane | 68c0273 | 2009-06-09 18:14:18 +0000 | [diff] [blame] | 2683 | if( turnOnAC && iRollback && db->activeVdbeCnt>1 ){ |
drh | ad4a4b8 | 2008-11-05 16:37:34 +0000 | [diff] [blame] | 2684 | /* If this instruction implements a ROLLBACK and other VMs are |
danielk1977 | 46c43ed | 2004-06-30 06:30:25 +0000 | [diff] [blame] | 2685 | ** still running, and a transaction is active, return an error indicating |
| 2686 | ** that the other VMs must complete first. |
| 2687 | */ |
drh | ad4a4b8 | 2008-11-05 16:37:34 +0000 | [diff] [blame] | 2688 | sqlite3SetString(&p->zErrMsg, db, "cannot rollback transaction - " |
| 2689 | "SQL statements in progress"); |
drh | 99dfe5e | 2008-10-30 15:03:15 +0000 | [diff] [blame] | 2690 | rc = SQLITE_BUSY; |
drh | 9eb8cbe | 2009-06-19 22:23:41 +0000 | [diff] [blame] | 2691 | }else if( turnOnAC && !iRollback && db->writeVdbeCnt>0 ){ |
drh | ad4a4b8 | 2008-11-05 16:37:34 +0000 | [diff] [blame] | 2692 | /* If this instruction implements a COMMIT and other VMs are writing |
| 2693 | ** return an error indicating that the other VMs must complete first. |
| 2694 | */ |
| 2695 | sqlite3SetString(&p->zErrMsg, db, "cannot commit transaction - " |
| 2696 | "SQL statements in progress"); |
| 2697 | rc = SQLITE_BUSY; |
| 2698 | }else if( desiredAutoCommit!=db->autoCommit ){ |
shane | 68c0273 | 2009-06-09 18:14:18 +0000 | [diff] [blame] | 2699 | if( iRollback ){ |
drh | ad4a4b8 | 2008-11-05 16:37:34 +0000 | [diff] [blame] | 2700 | assert( desiredAutoCommit==1 ); |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 2701 | sqlite3RollbackAll(db); |
danielk1977 | f3f06bb | 2005-12-16 15:24:28 +0000 | [diff] [blame] | 2702 | db->autoCommit = 1; |
dan | 32b09f2 | 2009-09-23 17:29:59 +0000 | [diff] [blame] | 2703 | }else if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){ |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 2704 | goto vdbe_return; |
danielk1977 | f3f06bb | 2005-12-16 15:24:28 +0000 | [diff] [blame] | 2705 | }else{ |
shane | 7d3846a | 2008-12-11 02:58:26 +0000 | [diff] [blame] | 2706 | db->autoCommit = (u8)desiredAutoCommit; |
danielk1977 | f3f06bb | 2005-12-16 15:24:28 +0000 | [diff] [blame] | 2707 | if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){ |
danielk1977 | f3f06bb | 2005-12-16 15:24:28 +0000 | [diff] [blame] | 2708 | p->pc = pc; |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 2709 | db->autoCommit = (u8)(1-desiredAutoCommit); |
drh | 900b31e | 2007-08-28 02:27:51 +0000 | [diff] [blame] | 2710 | p->rc = rc = SQLITE_BUSY; |
| 2711 | goto vdbe_return; |
danielk1977 | f3f06bb | 2005-12-16 15:24:28 +0000 | [diff] [blame] | 2712 | } |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 2713 | } |
danielk1977 | bd43455 | 2009-03-18 10:33:00 +0000 | [diff] [blame] | 2714 | assert( db->nStatement==0 ); |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2715 | sqlite3CloseSavepoints(db); |
drh | 83968c4 | 2007-04-18 16:45:24 +0000 | [diff] [blame] | 2716 | if( p->rc==SQLITE_OK ){ |
drh | 900b31e | 2007-08-28 02:27:51 +0000 | [diff] [blame] | 2717 | rc = SQLITE_DONE; |
drh | 83968c4 | 2007-04-18 16:45:24 +0000 | [diff] [blame] | 2718 | }else{ |
drh | 900b31e | 2007-08-28 02:27:51 +0000 | [diff] [blame] | 2719 | rc = SQLITE_ERROR; |
drh | 83968c4 | 2007-04-18 16:45:24 +0000 | [diff] [blame] | 2720 | } |
drh | 900b31e | 2007-08-28 02:27:51 +0000 | [diff] [blame] | 2721 | goto vdbe_return; |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 2722 | }else{ |
drh | f089aa4 | 2008-07-08 19:34:06 +0000 | [diff] [blame] | 2723 | sqlite3SetString(&p->zErrMsg, db, |
drh | ad4a4b8 | 2008-11-05 16:37:34 +0000 | [diff] [blame] | 2724 | (!desiredAutoCommit)?"cannot start a transaction within a transaction":( |
shane | 68c0273 | 2009-06-09 18:14:18 +0000 | [diff] [blame] | 2725 | (iRollback)?"cannot rollback - no transaction is active": |
drh | f089aa4 | 2008-07-08 19:34:06 +0000 | [diff] [blame] | 2726 | "cannot commit - no transaction is active")); |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 2727 | |
| 2728 | rc = SQLITE_ERROR; |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2729 | } |
| 2730 | break; |
| 2731 | } |
| 2732 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 2733 | /* Opcode: Transaction P1 P2 * * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2734 | ** |
| 2735 | ** Begin a transaction. The transaction ends when a Commit or Rollback |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2736 | ** opcode is encountered. Depending on the ON CONFLICT setting, the |
| 2737 | ** transaction might also be rolled back if an error is encountered. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2738 | ** |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2739 | ** P1 is the index of the database file on which the transaction is |
| 2740 | ** started. Index 0 is the main database file and index 1 is the |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 2741 | ** file used for temporary tables. Indices of 2 or more are used for |
| 2742 | ** attached databases. |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 2743 | ** |
drh | 8024205 | 2004-06-09 00:48:12 +0000 | [diff] [blame] | 2744 | ** If P2 is non-zero, then a write-transaction is started. A RESERVED lock is |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 2745 | ** obtained on the database file when a write-transaction is started. No |
drh | 8024205 | 2004-06-09 00:48:12 +0000 | [diff] [blame] | 2746 | ** other process can start another write transaction while this transaction is |
| 2747 | ** underway. Starting a write transaction also creates a rollback journal. A |
| 2748 | ** write transaction must be started before any changes can be made to the |
drh | 684917c | 2004-10-05 02:41:42 +0000 | [diff] [blame] | 2749 | ** database. If P2 is 2 or greater then an EXCLUSIVE lock is also obtained |
| 2750 | ** on the file. |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 2751 | ** |
dan | e0af83a | 2009-09-08 19:15:01 +0000 | [diff] [blame] | 2752 | ** If a write-transaction is started and the Vdbe.usesStmtJournal flag is |
| 2753 | ** true (this flag is set if the Vdbe may modify more than one row and may |
| 2754 | ** throw an ABORT exception), a statement transaction may also be opened. |
| 2755 | ** More specifically, a statement transaction is opened iff the database |
| 2756 | ** connection is currently not in autocommit mode, or if there are other |
| 2757 | ** active statements. A statement transaction allows the affects of this |
| 2758 | ** VDBE to be rolled back after an error without having to roll back the |
| 2759 | ** entire transaction. If no error is encountered, the statement transaction |
| 2760 | ** will automatically commit when the VDBE halts. |
| 2761 | ** |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 2762 | ** If P2 is zero, then a read-lock is obtained on the database file. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2763 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2764 | case OP_Transaction: { |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 2765 | Btree *pBt; |
| 2766 | |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 2767 | assert( pOp->p1>=0 && pOp->p1<db->nDb ); |
| 2768 | assert( (p->btreeMask & (1<<pOp->p1))!=0 ); |
| 2769 | pBt = db->aDb[pOp->p1].pBt; |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 2770 | |
danielk1977 | 24162fe | 2004-06-04 06:22:00 +0000 | [diff] [blame] | 2771 | if( pBt ){ |
danielk1977 | 40b38dc | 2004-06-26 08:38:24 +0000 | [diff] [blame] | 2772 | rc = sqlite3BtreeBeginTrans(pBt, pOp->p2); |
danielk1977 | 24162fe | 2004-06-04 06:22:00 +0000 | [diff] [blame] | 2773 | if( rc==SQLITE_BUSY ){ |
danielk1977 | 2a764eb | 2004-06-12 01:43:26 +0000 | [diff] [blame] | 2774 | p->pc = pc; |
drh | 900b31e | 2007-08-28 02:27:51 +0000 | [diff] [blame] | 2775 | p->rc = rc = SQLITE_BUSY; |
drh | 900b31e | 2007-08-28 02:27:51 +0000 | [diff] [blame] | 2776 | goto vdbe_return; |
danielk1977 | 24162fe | 2004-06-04 06:22:00 +0000 | [diff] [blame] | 2777 | } |
drh | 9e9f1bd | 2009-10-13 15:36:51 +0000 | [diff] [blame] | 2778 | if( rc!=SQLITE_OK ){ |
danielk1977 | 24162fe | 2004-06-04 06:22:00 +0000 | [diff] [blame] | 2779 | goto abort_due_to_error; |
drh | 90bfcda | 2001-09-23 19:46:51 +0000 | [diff] [blame] | 2780 | } |
dan | e0af83a | 2009-09-08 19:15:01 +0000 | [diff] [blame] | 2781 | |
| 2782 | if( pOp->p2 && p->usesStmtJournal |
| 2783 | && (db->autoCommit==0 || db->activeVdbeCnt>1) |
| 2784 | ){ |
| 2785 | assert( sqlite3BtreeIsInTrans(pBt) ); |
| 2786 | if( p->iStatement==0 ){ |
| 2787 | assert( db->nStatement>=0 && db->nSavepoint>=0 ); |
| 2788 | db->nStatement++; |
| 2789 | p->iStatement = db->nSavepoint + db->nStatement; |
| 2790 | } |
| 2791 | rc = sqlite3BtreeBeginStmt(pBt, p->iStatement); |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 2792 | |
| 2793 | /* Store the current value of the database handles deferred constraint |
| 2794 | ** counter. If the statement transaction needs to be rolled back, |
| 2795 | ** the value of this counter needs to be restored too. */ |
| 2796 | p->nStmtDefCons = db->nDeferredCons; |
dan | e0af83a | 2009-09-08 19:15:01 +0000 | [diff] [blame] | 2797 | } |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 2798 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2799 | break; |
| 2800 | } |
| 2801 | |
drh | b1fdb2a | 2008-01-05 04:06:03 +0000 | [diff] [blame] | 2802 | /* Opcode: ReadCookie P1 P2 P3 * * |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 2803 | ** |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2804 | ** Read cookie number P3 from database P1 and write it into register P2. |
danielk1977 | 0d19f7a | 2009-06-03 11:25:07 +0000 | [diff] [blame] | 2805 | ** P3==1 is the schema version. P3==2 is the database format. |
| 2806 | ** 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] | 2807 | ** the main database file and P1==1 is the database file used to store |
| 2808 | ** temporary tables. |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 2809 | ** |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 2810 | ** There must be a read-lock on the database (either a transaction |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2811 | ** must be started or there must be an open cursor) before |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 2812 | ** executing this instruction. |
| 2813 | */ |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 2814 | case OP_ReadCookie: { /* out2-prerelease */ |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 2815 | int iMeta; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2816 | int iDb; |
| 2817 | int iCookie; |
danielk1977 | 180b56a | 2007-06-24 08:00:42 +0000 | [diff] [blame] | 2818 | |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2819 | iDb = pOp->p1; |
| 2820 | iCookie = pOp->p3; |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 2821 | assert( pOp->p3<SQLITE_N_BTREE_META ); |
danielk1977 | 180b56a | 2007-06-24 08:00:42 +0000 | [diff] [blame] | 2822 | assert( iDb>=0 && iDb<db->nDb ); |
| 2823 | assert( db->aDb[iDb].pBt!=0 ); |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 2824 | assert( (p->btreeMask & (1<<iDb))!=0 ); |
danielk1977 | 0d19f7a | 2009-06-03 11:25:07 +0000 | [diff] [blame] | 2825 | |
danielk1977 | 602b466 | 2009-07-02 07:47:33 +0000 | [diff] [blame] | 2826 | sqlite3BtreeGetMeta(db->aDb[iDb].pBt, iCookie, (u32 *)&iMeta); |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 2827 | pOut->u.i = iMeta; |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 2828 | break; |
| 2829 | } |
| 2830 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 2831 | /* Opcode: SetCookie P1 P2 P3 * * |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 2832 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 2833 | ** Write the content of register P3 (interpreted as an integer) |
danielk1977 | 0d19f7a | 2009-06-03 11:25:07 +0000 | [diff] [blame] | 2834 | ** into cookie number P2 of database P1. P2==1 is the schema version. |
| 2835 | ** P2==2 is the database format. P2==3 is the recommended pager cache |
| 2836 | ** size, and so forth. P1==0 is the main database file and P1==1 is the |
| 2837 | ** database file used to store temporary tables. |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 2838 | ** |
| 2839 | ** A transaction must be started before executing this opcode. |
| 2840 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2841 | case OP_SetCookie: { /* in3 */ |
drh | 3f7d4e4 | 2004-07-24 14:35:58 +0000 | [diff] [blame] | 2842 | Db *pDb; |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 2843 | assert( pOp->p2<SQLITE_N_BTREE_META ); |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2844 | assert( pOp->p1>=0 && pOp->p1<db->nDb ); |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 2845 | assert( (p->btreeMask & (1<<pOp->p1))!=0 ); |
drh | 3f7d4e4 | 2004-07-24 14:35:58 +0000 | [diff] [blame] | 2846 | pDb = &db->aDb[pOp->p1]; |
| 2847 | assert( pDb->pBt!=0 ); |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 2848 | pIn3 = &aMem[pOp->p3]; |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 2849 | sqlite3VdbeMemIntegerify(pIn3); |
drh | a3b321d | 2004-05-11 09:31:31 +0000 | [diff] [blame] | 2850 | /* See note about index shifting on OP_ReadCookie */ |
danielk1977 | 0d19f7a | 2009-06-03 11:25:07 +0000 | [diff] [blame] | 2851 | rc = sqlite3BtreeUpdateMeta(pDb->pBt, pOp->p2, (int)pIn3->u.i); |
| 2852 | if( pOp->p2==BTREE_SCHEMA_VERSION ){ |
drh | 3f7d4e4 | 2004-07-24 14:35:58 +0000 | [diff] [blame] | 2853 | /* When the schema cookie changes, record the new cookie internally */ |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 2854 | pDb->pSchema->schema_cookie = (int)pIn3->u.i; |
drh | 3f7d4e4 | 2004-07-24 14:35:58 +0000 | [diff] [blame] | 2855 | db->flags |= SQLITE_InternChanges; |
danielk1977 | 0d19f7a | 2009-06-03 11:25:07 +0000 | [diff] [blame] | 2856 | }else if( pOp->p2==BTREE_FILE_FORMAT ){ |
drh | d28bcb3 | 2005-12-21 14:43:11 +0000 | [diff] [blame] | 2857 | /* Record changes in the file format */ |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 2858 | pDb->pSchema->file_format = (u8)pIn3->u.i; |
drh | 3f7d4e4 | 2004-07-24 14:35:58 +0000 | [diff] [blame] | 2859 | } |
drh | fd426c6 | 2006-01-30 15:34:22 +0000 | [diff] [blame] | 2860 | if( pOp->p1==1 ){ |
| 2861 | /* Invalidate all prepared statements whenever the TEMP database |
| 2862 | ** schema is changed. Ticket #1644 */ |
| 2863 | sqlite3ExpirePreparedStatements(db); |
dan | fa401de | 2009-10-16 14:55:03 +0000 | [diff] [blame] | 2864 | p->expired = 0; |
drh | fd426c6 | 2006-01-30 15:34:22 +0000 | [diff] [blame] | 2865 | } |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 2866 | break; |
| 2867 | } |
| 2868 | |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 2869 | /* Opcode: VerifyCookie P1 P2 * |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 2870 | ** |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2871 | ** Check the value of global database parameter number 0 (the |
| 2872 | ** schema version) and make sure it is equal to P2. |
| 2873 | ** P1 is the database number which is 0 for the main database file |
| 2874 | ** and 1 for the file holding temporary tables and some higher number |
| 2875 | ** for auxiliary databases. |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 2876 | ** |
| 2877 | ** The cookie changes its value whenever the database schema changes. |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2878 | ** This operation is used to detect when that the cookie has changed |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 2879 | ** and that the current process needs to reread the schema. |
| 2880 | ** |
| 2881 | ** Either a transaction needs to have been started or an OP_Open needs |
| 2882 | ** to be executed (to establish a read lock) before this opcode is |
| 2883 | ** invoked. |
| 2884 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2885 | case OP_VerifyCookie: { |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 2886 | int iMeta; |
drh | c275b4e | 2004-07-19 17:25:24 +0000 | [diff] [blame] | 2887 | Btree *pBt; |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2888 | assert( pOp->p1>=0 && pOp->p1<db->nDb ); |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 2889 | assert( (p->btreeMask & (1<<pOp->p1))!=0 ); |
drh | c275b4e | 2004-07-19 17:25:24 +0000 | [diff] [blame] | 2890 | pBt = db->aDb[pOp->p1].pBt; |
| 2891 | if( pBt ){ |
danielk1977 | 602b466 | 2009-07-02 07:47:33 +0000 | [diff] [blame] | 2892 | sqlite3BtreeGetMeta(pBt, BTREE_SCHEMA_VERSION, (u32 *)&iMeta); |
drh | c275b4e | 2004-07-19 17:25:24 +0000 | [diff] [blame] | 2893 | }else{ |
drh | c275b4e | 2004-07-19 17:25:24 +0000 | [diff] [blame] | 2894 | iMeta = 0; |
| 2895 | } |
danielk1977 | 602b466 | 2009-07-02 07:47:33 +0000 | [diff] [blame] | 2896 | if( iMeta!=pOp->p2 ){ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 2897 | sqlite3DbFree(db, p->zErrMsg); |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 2898 | p->zErrMsg = sqlite3DbStrDup(db, "database schema has changed"); |
danielk1977 | 896e792 | 2007-04-17 08:32:33 +0000 | [diff] [blame] | 2899 | /* If the schema-cookie from the database file matches the cookie |
| 2900 | ** stored with the in-memory representation of the schema, do |
| 2901 | ** not reload the schema from the database file. |
| 2902 | ** |
shane | 21e7feb | 2008-05-30 15:59:49 +0000 | [diff] [blame] | 2903 | ** If virtual-tables are in use, this is not just an optimization. |
danielk1977 | 896e792 | 2007-04-17 08:32:33 +0000 | [diff] [blame] | 2904 | ** Often, v-tables store their data in other SQLite tables, which |
| 2905 | ** are queried from within xNext() and other v-table methods using |
| 2906 | ** prepared queries. If such a query is out-of-date, we do not want to |
| 2907 | ** discard the database schema, as the user code implementing the |
| 2908 | ** v-table would have to be ready for the sqlite3_vtab structure itself |
| 2909 | ** to be invalidated whenever sqlite3_step() is called from within |
| 2910 | ** a v-table method. |
| 2911 | */ |
| 2912 | if( db->aDb[pOp->p1].pSchema->schema_cookie!=iMeta ){ |
| 2913 | sqlite3ResetInternalSchema(db, pOp->p1); |
| 2914 | } |
| 2915 | |
drh | f6d8ab8 | 2007-01-12 23:43:42 +0000 | [diff] [blame] | 2916 | sqlite3ExpirePreparedStatements(db); |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 2917 | rc = SQLITE_SCHEMA; |
| 2918 | } |
| 2919 | break; |
| 2920 | } |
| 2921 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 2922 | /* Opcode: OpenRead P1 P2 P3 P4 P5 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2923 | ** |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2924 | ** Open a read-only cursor for the database table whose root page is |
danielk1977 | 207872a | 2008-01-03 07:54:23 +0000 | [diff] [blame] | 2925 | ** P2 in a database file. The database file is determined by P3. |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 2926 | ** P3==0 means the main database, P3==1 means the database used for |
| 2927 | ** temporary tables, and P3>1 means used the corresponding attached |
| 2928 | ** database. Give the new cursor an identifier of P1. The P1 |
danielk1977 | 207872a | 2008-01-03 07:54:23 +0000 | [diff] [blame] | 2929 | ** values need not be contiguous but all P1 values should be small integers. |
| 2930 | ** It is an error for P1 to be negative. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2931 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 2932 | ** If P5!=0 then use the content of register P2 as the root page, not |
| 2933 | ** the value of P2 itself. |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 2934 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2935 | ** There will be a read lock on the database whenever there is an |
| 2936 | ** open cursor. If the database was unlocked prior to this instruction |
| 2937 | ** then a read lock is acquired as part of this instruction. A read |
| 2938 | ** lock allows other processes to read the database but prohibits |
| 2939 | ** any other process from modifying the database. The read lock is |
| 2940 | ** released when all cursors are closed. If this instruction attempts |
| 2941 | ** to get a read lock but fails, the script terminates with an |
| 2942 | ** SQLITE_BUSY error code. |
| 2943 | ** |
danielk1977 | d336e22 | 2009-02-20 10:58:41 +0000 | [diff] [blame] | 2944 | ** The P4 value may be either an integer (P4_INT32) or a pointer to |
| 2945 | ** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo |
| 2946 | ** structure, then said structure defines the content and collating |
| 2947 | ** sequence of the index being opened. Otherwise, if P4 is an integer |
| 2948 | ** value, it is set to the number of columns in the table. |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 2949 | ** |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2950 | ** See also OpenWrite. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2951 | */ |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 2952 | /* Opcode: OpenWrite P1 P2 P3 P4 P5 |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2953 | ** |
| 2954 | ** 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] | 2955 | ** page is P2. Or if P5!=0 use the content of register P2 to find the |
| 2956 | ** root page. |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2957 | ** |
danielk1977 | d336e22 | 2009-02-20 10:58:41 +0000 | [diff] [blame] | 2958 | ** The P4 value may be either an integer (P4_INT32) or a pointer to |
| 2959 | ** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo |
| 2960 | ** structure, then said structure defines the content and collating |
| 2961 | ** sequence of the index being opened. Otherwise, if P4 is an integer |
drh | 35cd643 | 2009-06-05 14:17:21 +0000 | [diff] [blame] | 2962 | ** value, it is set to the number of columns in the table, or to the |
| 2963 | ** largest index of any column of the table that is actually used. |
jplyon | 5a56422 | 2003-06-02 06:15:58 +0000 | [diff] [blame] | 2964 | ** |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2965 | ** This instruction works just like OpenRead except that it opens the cursor |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2966 | ** in read/write mode. For a given table, there can be one or more read-only |
| 2967 | ** cursors or a single read/write cursor but not both. |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 2968 | ** |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2969 | ** See also OpenRead. |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2970 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2971 | case OP_OpenRead: |
| 2972 | case OP_OpenWrite: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2973 | int nField; |
| 2974 | KeyInfo *pKeyInfo; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2975 | int p2; |
| 2976 | int iDb; |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 2977 | int wrFlag; |
| 2978 | Btree *pX; |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 2979 | VdbeCursor *pCur; |
drh | d946db0 | 2005-12-29 19:23:06 +0000 | [diff] [blame] | 2980 | Db *pDb; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2981 | |
dan | fa401de | 2009-10-16 14:55:03 +0000 | [diff] [blame] | 2982 | if( p->expired ){ |
| 2983 | rc = SQLITE_ABORT; |
| 2984 | break; |
| 2985 | } |
| 2986 | |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2987 | nField = 0; |
| 2988 | pKeyInfo = 0; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2989 | p2 = pOp->p2; |
| 2990 | iDb = pOp->p3; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 2991 | assert( iDb>=0 && iDb<db->nDb ); |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 2992 | assert( (p->btreeMask & (1<<iDb))!=0 ); |
drh | d946db0 | 2005-12-29 19:23:06 +0000 | [diff] [blame] | 2993 | pDb = &db->aDb[iDb]; |
| 2994 | pX = pDb->pBt; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 2995 | assert( pX!=0 ); |
drh | d946db0 | 2005-12-29 19:23:06 +0000 | [diff] [blame] | 2996 | if( pOp->opcode==OP_OpenWrite ){ |
| 2997 | wrFlag = 1; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2998 | if( pDb->pSchema->file_format < p->minWriteFileFormat ){ |
| 2999 | p->minWriteFileFormat = pDb->pSchema->file_format; |
drh | d946db0 | 2005-12-29 19:23:06 +0000 | [diff] [blame] | 3000 | } |
| 3001 | }else{ |
| 3002 | wrFlag = 0; |
| 3003 | } |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 3004 | if( pOp->p5 ){ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3005 | assert( p2>0 ); |
| 3006 | assert( p2<=p->nMem ); |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 3007 | pIn2 = &aMem[p2]; |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3008 | sqlite3VdbeMemIntegerify(pIn2); |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 3009 | p2 = (int)pIn2->u.i; |
drh | 9a65f2c | 2009-06-22 19:05:40 +0000 | [diff] [blame] | 3010 | /* The p2 value always comes from a prior OP_CreateTable opcode and |
| 3011 | ** that opcode will always set the p2 value to 2 or more or else fail. |
| 3012 | ** If there were a failure, the prepared statement would have halted |
| 3013 | ** before reaching this instruction. */ |
drh | 27731d7 | 2009-06-22 12:05:10 +0000 | [diff] [blame] | 3014 | if( NEVER(p2<2) ) { |
shane | dcc50b7 | 2008-11-13 18:29:50 +0000 | [diff] [blame] | 3015 | rc = SQLITE_CORRUPT_BKPT; |
| 3016 | goto abort_due_to_error; |
| 3017 | } |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 3018 | } |
danielk1977 | d336e22 | 2009-02-20 10:58:41 +0000 | [diff] [blame] | 3019 | if( pOp->p4type==P4_KEYINFO ){ |
| 3020 | pKeyInfo = pOp->p4.pKeyInfo; |
| 3021 | pKeyInfo->enc = ENC(p->db); |
| 3022 | nField = pKeyInfo->nField+1; |
| 3023 | }else if( pOp->p4type==P4_INT32 ){ |
| 3024 | nField = pOp->p4.i; |
| 3025 | } |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 3026 | assert( pOp->p1>=0 ); |
| 3027 | pCur = allocateCursor(p, pOp->p1, nField, iDb, 1); |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 3028 | if( pCur==0 ) goto no_mem; |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 3029 | pCur->nullRow = 1; |
danielk1977 | d336e22 | 2009-02-20 10:58:41 +0000 | [diff] [blame] | 3030 | rc = sqlite3BtreeCursor(pX, p2, wrFlag, pKeyInfo, pCur->pCursor); |
| 3031 | pCur->pKeyInfo = pKeyInfo; |
| 3032 | |
danielk1977 | 172114a | 2009-07-07 15:47:12 +0000 | [diff] [blame] | 3033 | /* Since it performs no memory allocation or IO, the only values that |
| 3034 | ** sqlite3BtreeCursor() may return are SQLITE_EMPTY and SQLITE_OK. |
| 3035 | ** SQLITE_EMPTY is only returned when attempting to open the table |
| 3036 | ** rooted at page 1 of a zero-byte database. */ |
| 3037 | assert( rc==SQLITE_EMPTY || rc==SQLITE_OK ); |
| 3038 | if( rc==SQLITE_EMPTY ){ |
| 3039 | pCur->pCursor = 0; |
| 3040 | rc = SQLITE_OK; |
danielk1977 | 24162fe | 2004-06-04 06:22:00 +0000 | [diff] [blame] | 3041 | } |
danielk1977 | 172114a | 2009-07-07 15:47:12 +0000 | [diff] [blame] | 3042 | |
| 3043 | /* Set the VdbeCursor.isTable and isIndex variables. Previous versions of |
| 3044 | ** SQLite used to check if the root-page flags were sane at this point |
| 3045 | ** and report database corruption if they were not, but this check has |
| 3046 | ** since moved into the btree layer. */ |
| 3047 | pCur->isTable = pOp->p4type!=P4_KEYINFO; |
| 3048 | pCur->isIndex = !pCur->isTable; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3049 | break; |
| 3050 | } |
| 3051 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 3052 | /* Opcode: OpenEphemeral P1 P2 * P4 * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3053 | ** |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 3054 | ** Open a new cursor P1 to a transient table. |
drh | 9170dd7 | 2005-07-08 17:13:46 +0000 | [diff] [blame] | 3055 | ** The cursor is always opened read/write even if |
| 3056 | ** the main database is read-only. The transient or virtual |
| 3057 | ** table is deleted automatically when the cursor is closed. |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 3058 | ** |
drh | 0342b1f | 2005-09-01 03:07:44 +0000 | [diff] [blame] | 3059 | ** P2 is the number of columns in the virtual table. |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 3060 | ** The cursor points to a BTree table if P4==0 and to a BTree index |
| 3061 | ** 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] | 3062 | ** that defines the format of keys in the index. |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 3063 | ** |
| 3064 | ** This opcode was once called OpenTemp. But that created |
| 3065 | ** confusion because the term "temp table", might refer either |
| 3066 | ** to a TEMP table at the SQL level, or to a table opened by |
| 3067 | ** this opcode. Then this opcode was call OpenVirtual. But |
| 3068 | ** that created confusion with the whole virtual-table idea. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3069 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3070 | case OP_OpenEphemeral: { |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 3071 | VdbeCursor *pCx; |
drh | 33f4e02 | 2007-09-03 15:19:34 +0000 | [diff] [blame] | 3072 | static const int openFlags = |
| 3073 | SQLITE_OPEN_READWRITE | |
| 3074 | SQLITE_OPEN_CREATE | |
| 3075 | SQLITE_OPEN_EXCLUSIVE | |
| 3076 | SQLITE_OPEN_DELETEONCLOSE | |
| 3077 | SQLITE_OPEN_TRANSIENT_DB; |
| 3078 | |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 3079 | assert( pOp->p1>=0 ); |
| 3080 | pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, 1); |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 3081 | if( pCx==0 ) goto no_mem; |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 3082 | pCx->nullRow = 1; |
drh | 33f4e02 | 2007-09-03 15:19:34 +0000 | [diff] [blame] | 3083 | rc = sqlite3BtreeFactory(db, 0, 1, SQLITE_DEFAULT_TEMP_CACHE_SIZE, openFlags, |
| 3084 | &pCx->pBt); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3085 | if( rc==SQLITE_OK ){ |
danielk1977 | 40b38dc | 2004-06-26 08:38:24 +0000 | [diff] [blame] | 3086 | rc = sqlite3BtreeBeginTrans(pCx->pBt, 1); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3087 | } |
| 3088 | if( rc==SQLITE_OK ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3089 | /* If a transient index is required, create it by calling |
| 3090 | ** sqlite3BtreeCreateTable() with the BTREE_ZERODATA flag before |
| 3091 | ** opening it. If a transient table is required, just use the |
danielk1977 | 0dbe72b | 2004-05-11 04:54:49 +0000 | [diff] [blame] | 3092 | ** automatically created table with root-page 1 (an INTKEY table). |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3093 | */ |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 3094 | if( pOp->p4.pKeyInfo ){ |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 3095 | int pgno; |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 3096 | assert( pOp->p4type==P4_KEYINFO ); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3097 | rc = sqlite3BtreeCreateTable(pCx->pBt, &pgno, BTREE_ZERODATA); |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 3098 | if( rc==SQLITE_OK ){ |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 3099 | assert( pgno==MASTER_ROOT+1 ); |
drh | 1e968a0 | 2008-03-25 00:22:21 +0000 | [diff] [blame] | 3100 | rc = sqlite3BtreeCursor(pCx->pBt, pgno, 1, |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 3101 | (KeyInfo*)pOp->p4.z, pCx->pCursor); |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 3102 | pCx->pKeyInfo = pOp->p4.pKeyInfo; |
danielk1977 | 14db266 | 2006-01-09 16:12:04 +0000 | [diff] [blame] | 3103 | pCx->pKeyInfo->enc = ENC(p->db); |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 3104 | } |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3105 | pCx->isTable = 0; |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 3106 | }else{ |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 3107 | rc = sqlite3BtreeCursor(pCx->pBt, MASTER_ROOT, 1, 0, pCx->pCursor); |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3108 | pCx->isTable = 1; |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 3109 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3110 | } |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3111 | pCx->isIndex = !pCx->isTable; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3112 | break; |
| 3113 | } |
| 3114 | |
danielk1977 | d336e22 | 2009-02-20 10:58:41 +0000 | [diff] [blame] | 3115 | /* Opcode: OpenPseudo P1 P2 P3 * * |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3116 | ** |
| 3117 | ** Open a new cursor that points to a fake table that contains a single |
drh | 3e9ca09 | 2009-09-08 01:14:48 +0000 | [diff] [blame] | 3118 | ** row of data. The content of that one row in the content of memory |
| 3119 | ** register P2. In other words, cursor P1 becomes an alias for the |
| 3120 | ** MEM_Blob content contained in register P2. |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3121 | ** |
drh | 3e9ca09 | 2009-09-08 01:14:48 +0000 | [diff] [blame] | 3122 | ** A pseudo-table created by this opcode is used to hold the a single |
drh | cdd536f | 2006-03-17 00:04:03 +0000 | [diff] [blame] | 3123 | ** row output from the sorter so that the row can be decomposed into |
drh | 3e9ca09 | 2009-09-08 01:14:48 +0000 | [diff] [blame] | 3124 | ** individual columns using the OP_Column opcode. The OP_Column opcode |
| 3125 | ** is the only cursor opcode that works with a pseudo-table. |
danielk1977 | d336e22 | 2009-02-20 10:58:41 +0000 | [diff] [blame] | 3126 | ** |
| 3127 | ** P3 is the number of fields in the records that will be stored by |
| 3128 | ** the pseudo-table. |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3129 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3130 | case OP_OpenPseudo: { |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 3131 | VdbeCursor *pCx; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3132 | |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 3133 | assert( pOp->p1>=0 ); |
| 3134 | pCx = allocateCursor(p, pOp->p1, pOp->p3, -1, 0); |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 3135 | if( pCx==0 ) goto no_mem; |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3136 | pCx->nullRow = 1; |
drh | 3e9ca09 | 2009-09-08 01:14:48 +0000 | [diff] [blame] | 3137 | pCx->pseudoTableReg = pOp->p2; |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3138 | pCx->isTable = 1; |
| 3139 | pCx->isIndex = 0; |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3140 | break; |
| 3141 | } |
| 3142 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 3143 | /* Opcode: Close P1 * * * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3144 | ** |
| 3145 | ** Close a cursor previously opened as P1. If P1 is not |
| 3146 | ** currently open, this instruction is a no-op. |
| 3147 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3148 | case OP_Close: { |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 3149 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 3150 | sqlite3VdbeFreeCursor(p, p->apCsr[pOp->p1]); |
| 3151 | p->apCsr[pOp->p1] = 0; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3152 | break; |
| 3153 | } |
| 3154 | |
drh | 959403f | 2008-12-12 17:56:16 +0000 | [diff] [blame] | 3155 | /* Opcode: SeekGe P1 P2 P3 P4 * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3156 | ** |
danielk1977 | b790c6c | 2008-04-18 10:25:24 +0000 | [diff] [blame] | 3157 | ** 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] | 3158 | ** use the value in register P3 as the key. If cursor P1 refers |
danielk1977 | b790c6c | 2008-04-18 10:25:24 +0000 | [diff] [blame] | 3159 | ** to an SQL index, then P3 is the first in an array of P4 registers |
| 3160 | ** that are used as an unpacked index key. |
| 3161 | ** |
| 3162 | ** Reposition cursor P1 so that it points to the smallest entry that |
| 3163 | ** is greater than or equal to the key value. If there are no records |
| 3164 | ** 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] | 3165 | ** |
drh | 959403f | 2008-12-12 17:56:16 +0000 | [diff] [blame] | 3166 | ** See also: Found, NotFound, Distinct, SeekLt, SeekGt, SeekLe |
drh | 7cf6e4d | 2004-05-19 14:56:55 +0000 | [diff] [blame] | 3167 | */ |
drh | 959403f | 2008-12-12 17:56:16 +0000 | [diff] [blame] | 3168 | /* Opcode: SeekGt P1 P2 P3 P4 * |
drh | 7cf6e4d | 2004-05-19 14:56:55 +0000 | [diff] [blame] | 3169 | ** |
danielk1977 | b790c6c | 2008-04-18 10:25:24 +0000 | [diff] [blame] | 3170 | ** 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] | 3171 | ** use the value in register P3 as a key. If cursor P1 refers |
danielk1977 | b790c6c | 2008-04-18 10:25:24 +0000 | [diff] [blame] | 3172 | ** to an SQL index, then P3 is the first in an array of P4 registers |
| 3173 | ** that are used as an unpacked index key. |
| 3174 | ** |
| 3175 | ** Reposition cursor P1 so that it points to the smallest entry that |
| 3176 | ** is greater than the key value. If there are no records greater than |
| 3177 | ** the key and P2 is not zero, then jump to P2. |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 3178 | ** |
drh | 959403f | 2008-12-12 17:56:16 +0000 | [diff] [blame] | 3179 | ** See also: Found, NotFound, Distinct, SeekLt, SeekGe, SeekLe |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3180 | */ |
drh | 959403f | 2008-12-12 17:56:16 +0000 | [diff] [blame] | 3181 | /* Opcode: SeekLt P1 P2 P3 P4 * |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 3182 | ** |
danielk1977 | b790c6c | 2008-04-18 10:25:24 +0000 | [diff] [blame] | 3183 | ** 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] | 3184 | ** use the value in register P3 as a key. If cursor P1 refers |
danielk1977 | b790c6c | 2008-04-18 10:25:24 +0000 | [diff] [blame] | 3185 | ** to an SQL index, then P3 is the first in an array of P4 registers |
| 3186 | ** that are used as an unpacked index key. |
| 3187 | ** |
| 3188 | ** Reposition cursor P1 so that it points to the largest entry that |
| 3189 | ** is less than the key value. If there are no records less than |
| 3190 | ** the key and P2 is not zero, then jump to P2. |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 3191 | ** |
drh | 959403f | 2008-12-12 17:56:16 +0000 | [diff] [blame] | 3192 | ** See also: Found, NotFound, Distinct, SeekGt, SeekGe, SeekLe |
drh | 7cf6e4d | 2004-05-19 14:56:55 +0000 | [diff] [blame] | 3193 | */ |
drh | 959403f | 2008-12-12 17:56:16 +0000 | [diff] [blame] | 3194 | /* Opcode: SeekLe P1 P2 P3 P4 * |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 3195 | ** |
danielk1977 | b790c6c | 2008-04-18 10:25:24 +0000 | [diff] [blame] | 3196 | ** 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] | 3197 | ** use the value in register P3 as a key. If cursor P1 refers |
danielk1977 | b790c6c | 2008-04-18 10:25:24 +0000 | [diff] [blame] | 3198 | ** to an SQL index, then P3 is the first in an array of P4 registers |
| 3199 | ** that are used as an unpacked index key. |
danielk1977 | 751de56 | 2008-04-18 09:01:15 +0000 | [diff] [blame] | 3200 | ** |
danielk1977 | b790c6c | 2008-04-18 10:25:24 +0000 | [diff] [blame] | 3201 | ** Reposition cursor P1 so that it points to the largest entry that |
| 3202 | ** is less than or equal to the key value. If there are no records |
| 3203 | ** 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] | 3204 | ** |
drh | 959403f | 2008-12-12 17:56:16 +0000 | [diff] [blame] | 3205 | ** See also: Found, NotFound, Distinct, SeekGt, SeekGe, SeekLt |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 3206 | */ |
drh | 959403f | 2008-12-12 17:56:16 +0000 | [diff] [blame] | 3207 | case OP_SeekLt: /* jump, in3 */ |
| 3208 | case OP_SeekLe: /* jump, in3 */ |
| 3209 | case OP_SeekGe: /* jump, in3 */ |
| 3210 | case OP_SeekGt: { /* jump, in3 */ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3211 | int res; |
| 3212 | int oc; |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 3213 | VdbeCursor *pC; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3214 | UnpackedRecord r; |
| 3215 | int nField; |
| 3216 | i64 iKey; /* The rowid we are to seek to */ |
drh | 80ff32f | 2001-11-04 18:32:46 +0000 | [diff] [blame] | 3217 | |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 3218 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
drh | 959403f | 2008-12-12 17:56:16 +0000 | [diff] [blame] | 3219 | assert( pOp->p2!=0 ); |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 3220 | pC = p->apCsr[pOp->p1]; |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 3221 | assert( pC!=0 ); |
drh | 3e9ca09 | 2009-09-08 01:14:48 +0000 | [diff] [blame] | 3222 | assert( pC->pseudoTableReg==0 ); |
drh | 1f35012 | 2009-11-13 20:52:43 +0000 | [diff] [blame] | 3223 | assert( OP_SeekLe == OP_SeekLt+1 ); |
| 3224 | assert( OP_SeekGe == OP_SeekLt+2 ); |
| 3225 | assert( OP_SeekGt == OP_SeekLt+3 ); |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3226 | if( pC->pCursor!=0 ){ |
drh | 7cf6e4d | 2004-05-19 14:56:55 +0000 | [diff] [blame] | 3227 | oc = pOp->opcode; |
drh | a11846b | 2004-01-07 18:52:56 +0000 | [diff] [blame] | 3228 | pC->nullRow = 0; |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3229 | if( pC->isTable ){ |
drh | 959403f | 2008-12-12 17:56:16 +0000 | [diff] [blame] | 3230 | /* The input value in P3 might be of any type: integer, real, string, |
| 3231 | ** blob, or NULL. But it needs to be an integer before we can do |
| 3232 | ** the seek, so covert it. */ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 3233 | pIn3 = &aMem[pOp->p3]; |
drh | 959403f | 2008-12-12 17:56:16 +0000 | [diff] [blame] | 3234 | applyNumericAffinity(pIn3); |
| 3235 | iKey = sqlite3VdbeIntValue(pIn3); |
| 3236 | pC->rowidIsValid = 0; |
| 3237 | |
| 3238 | /* If the P3 value could not be converted into an integer without |
| 3239 | ** loss of information, then special processing is required... */ |
| 3240 | if( (pIn3->flags & MEM_Int)==0 ){ |
| 3241 | if( (pIn3->flags & MEM_Real)==0 ){ |
| 3242 | /* If the P3 value cannot be converted into any kind of a number, |
| 3243 | ** then the seek is not possible, so jump to P2 */ |
| 3244 | pc = pOp->p2 - 1; |
| 3245 | break; |
| 3246 | } |
| 3247 | /* If we reach this point, then the P3 value must be a floating |
| 3248 | ** point number. */ |
| 3249 | assert( (pIn3->flags & MEM_Real)!=0 ); |
| 3250 | |
| 3251 | if( iKey==SMALLEST_INT64 && (pIn3->r<(double)iKey || pIn3->r>0) ){ |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 3252 | /* The P3 value is too large in magnitude to be expressed as an |
drh | 959403f | 2008-12-12 17:56:16 +0000 | [diff] [blame] | 3253 | ** integer. */ |
| 3254 | res = 1; |
| 3255 | if( pIn3->r<0 ){ |
drh | 1f35012 | 2009-11-13 20:52:43 +0000 | [diff] [blame] | 3256 | if( oc>=OP_SeekGe ){ assert( oc==OP_SeekGe || oc==OP_SeekGt ); |
drh | 959403f | 2008-12-12 17:56:16 +0000 | [diff] [blame] | 3257 | rc = sqlite3BtreeFirst(pC->pCursor, &res); |
| 3258 | if( rc!=SQLITE_OK ) goto abort_due_to_error; |
| 3259 | } |
| 3260 | }else{ |
drh | 1f35012 | 2009-11-13 20:52:43 +0000 | [diff] [blame] | 3261 | if( oc<=OP_SeekLe ){ assert( oc==OP_SeekLt || oc==OP_SeekLe ); |
drh | 959403f | 2008-12-12 17:56:16 +0000 | [diff] [blame] | 3262 | rc = sqlite3BtreeLast(pC->pCursor, &res); |
| 3263 | if( rc!=SQLITE_OK ) goto abort_due_to_error; |
| 3264 | } |
| 3265 | } |
| 3266 | if( res ){ |
| 3267 | pc = pOp->p2 - 1; |
| 3268 | } |
| 3269 | break; |
| 3270 | }else if( oc==OP_SeekLt || oc==OP_SeekGe ){ |
| 3271 | /* Use the ceiling() function to convert real->int */ |
| 3272 | if( pIn3->r > (double)iKey ) iKey++; |
| 3273 | }else{ |
| 3274 | /* Use the floor() function to convert real->int */ |
| 3275 | assert( oc==OP_SeekLe || oc==OP_SeekGt ); |
| 3276 | if( pIn3->r < (double)iKey ) iKey--; |
| 3277 | } |
| 3278 | } |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 3279 | rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, 0, (u64)iKey, 0, &res); |
danielk1977 | 2812956 | 2005-01-11 10:25:06 +0000 | [diff] [blame] | 3280 | if( rc!=SQLITE_OK ){ |
| 3281 | goto abort_due_to_error; |
| 3282 | } |
drh | 959403f | 2008-12-12 17:56:16 +0000 | [diff] [blame] | 3283 | if( res==0 ){ |
| 3284 | pC->rowidIsValid = 1; |
| 3285 | pC->lastRowid = iKey; |
| 3286 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3287 | }else{ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3288 | nField = pOp->p4.i; |
danielk1977 | b790c6c | 2008-04-18 10:25:24 +0000 | [diff] [blame] | 3289 | assert( pOp->p4type==P4_INT32 ); |
| 3290 | assert( nField>0 ); |
| 3291 | r.pKeyInfo = pC->pKeyInfo; |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 3292 | r.nField = (u16)nField; |
drh | 1f35012 | 2009-11-13 20:52:43 +0000 | [diff] [blame] | 3293 | |
| 3294 | /* The next line of code computes as follows, only faster: |
| 3295 | ** if( oc==OP_SeekGt || oc==OP_SeekLe ){ |
| 3296 | ** r.flags = UNPACKED_INCRKEY; |
| 3297 | ** }else{ |
| 3298 | ** r.flags = 0; |
| 3299 | ** } |
| 3300 | */ |
shaneh | 5e17e8b | 2009-12-03 04:40:47 +0000 | [diff] [blame] | 3301 | r.flags = (u16)(UNPACKED_INCRKEY * (1 & (oc - OP_SeekLt))); |
drh | 1f35012 | 2009-11-13 20:52:43 +0000 | [diff] [blame] | 3302 | assert( oc!=OP_SeekGt || r.flags==UNPACKED_INCRKEY ); |
| 3303 | assert( oc!=OP_SeekLe || r.flags==UNPACKED_INCRKEY ); |
| 3304 | assert( oc!=OP_SeekGe || r.flags==0 ); |
| 3305 | assert( oc!=OP_SeekLt || r.flags==0 ); |
| 3306 | |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 3307 | r.aMem = &aMem[pOp->p3]; |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 3308 | ExpandBlob(r.aMem); |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 3309 | rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, &r, 0, 0, &res); |
danielk1977 | 2812956 | 2005-01-11 10:25:06 +0000 | [diff] [blame] | 3310 | if( rc!=SQLITE_OK ){ |
| 3311 | goto abort_due_to_error; |
| 3312 | } |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3313 | pC->rowidIsValid = 0; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3314 | } |
drh | a11846b | 2004-01-07 18:52:56 +0000 | [diff] [blame] | 3315 | pC->deferredMoveto = 0; |
drh | 76873ab | 2006-01-07 18:48:26 +0000 | [diff] [blame] | 3316 | pC->cacheStatus = CACHE_STALE; |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 3317 | #ifdef SQLITE_TEST |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 3318 | sqlite3_search_count++; |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 3319 | #endif |
drh | 1f35012 | 2009-11-13 20:52:43 +0000 | [diff] [blame] | 3320 | if( oc>=OP_SeekGe ){ assert( oc==OP_SeekGe || oc==OP_SeekGt ); |
drh | 959403f | 2008-12-12 17:56:16 +0000 | [diff] [blame] | 3321 | if( res<0 || (res==0 && oc==OP_SeekGt) ){ |
danielk1977 | 2812956 | 2005-01-11 10:25:06 +0000 | [diff] [blame] | 3322 | rc = sqlite3BtreeNext(pC->pCursor, &res); |
danielk1977 | 01427a6 | 2005-01-11 13:02:33 +0000 | [diff] [blame] | 3323 | if( rc!=SQLITE_OK ) goto abort_due_to_error; |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3324 | pC->rowidIsValid = 0; |
drh | 1af3fdb | 2004-07-18 21:33:01 +0000 | [diff] [blame] | 3325 | }else{ |
| 3326 | res = 0; |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 3327 | } |
drh | 7cf6e4d | 2004-05-19 14:56:55 +0000 | [diff] [blame] | 3328 | }else{ |
drh | 959403f | 2008-12-12 17:56:16 +0000 | [diff] [blame] | 3329 | assert( oc==OP_SeekLt || oc==OP_SeekLe ); |
| 3330 | if( res>0 || (res==0 && oc==OP_SeekLt) ){ |
danielk1977 | 01427a6 | 2005-01-11 13:02:33 +0000 | [diff] [blame] | 3331 | rc = sqlite3BtreePrevious(pC->pCursor, &res); |
| 3332 | if( rc!=SQLITE_OK ) goto abort_due_to_error; |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3333 | pC->rowidIsValid = 0; |
drh | 1a844c3 | 2002-12-04 22:29:28 +0000 | [diff] [blame] | 3334 | }else{ |
| 3335 | /* res might be negative because the table is empty. Check to |
| 3336 | ** see if this is the case. |
| 3337 | */ |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 3338 | res = sqlite3BtreeEof(pC->pCursor); |
drh | 1a844c3 | 2002-12-04 22:29:28 +0000 | [diff] [blame] | 3339 | } |
drh | 1af3fdb | 2004-07-18 21:33:01 +0000 | [diff] [blame] | 3340 | } |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 3341 | assert( pOp->p2>0 ); |
drh | 1af3fdb | 2004-07-18 21:33:01 +0000 | [diff] [blame] | 3342 | if( res ){ |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 3343 | pc = pOp->p2 - 1; |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 3344 | } |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 3345 | }else{ |
danielk1977 | f7b9d66 | 2008-06-23 18:49:43 +0000 | [diff] [blame] | 3346 | /* This happens when attempting to open the sqlite3_master table |
| 3347 | ** for read access returns SQLITE_EMPTY. In this case always |
| 3348 | ** take the jump (since there are no records in the table). |
| 3349 | */ |
| 3350 | pc = pOp->p2 - 1; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3351 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3352 | break; |
| 3353 | } |
| 3354 | |
drh | 959403f | 2008-12-12 17:56:16 +0000 | [diff] [blame] | 3355 | /* Opcode: Seek P1 P2 * * * |
| 3356 | ** |
| 3357 | ** P1 is an open table cursor and P2 is a rowid integer. Arrange |
| 3358 | ** for P1 to move so that it points to the rowid given by P2. |
| 3359 | ** |
| 3360 | ** This is actually a deferred seek. Nothing actually happens until |
| 3361 | ** the cursor is used to read a record. That way, if no reads |
| 3362 | ** occur, no unnecessary I/O happens. |
| 3363 | */ |
| 3364 | case OP_Seek: { /* in2 */ |
drh | 959403f | 2008-12-12 17:56:16 +0000 | [diff] [blame] | 3365 | VdbeCursor *pC; |
| 3366 | |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 3367 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 3368 | pC = p->apCsr[pOp->p1]; |
drh | 959403f | 2008-12-12 17:56:16 +0000 | [diff] [blame] | 3369 | assert( pC!=0 ); |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 3370 | if( ALWAYS(pC->pCursor!=0) ){ |
drh | 959403f | 2008-12-12 17:56:16 +0000 | [diff] [blame] | 3371 | assert( pC->isTable ); |
| 3372 | pC->nullRow = 0; |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 3373 | pIn2 = &aMem[pOp->p2]; |
drh | 959403f | 2008-12-12 17:56:16 +0000 | [diff] [blame] | 3374 | pC->movetoTarget = sqlite3VdbeIntValue(pIn2); |
| 3375 | pC->rowidIsValid = 0; |
| 3376 | pC->deferredMoveto = 1; |
| 3377 | } |
| 3378 | break; |
| 3379 | } |
| 3380 | |
| 3381 | |
drh | 8cff69d | 2009-11-12 19:59:44 +0000 | [diff] [blame] | 3382 | /* Opcode: Found P1 P2 P3 P4 * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3383 | ** |
drh | 8cff69d | 2009-11-12 19:59:44 +0000 | [diff] [blame] | 3384 | ** If P4==0 then register P3 holds a blob constructed by MakeRecord. If |
| 3385 | ** P4>0 then register P3 is the first of P4 registers that form an unpacked |
| 3386 | ** record. |
| 3387 | ** |
| 3388 | ** Cursor P1 is on an index btree. If the record identified by P3 and P4 |
| 3389 | ** 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] | 3390 | ** P1 is left pointing at the matching entry. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3391 | */ |
drh | 8cff69d | 2009-11-12 19:59:44 +0000 | [diff] [blame] | 3392 | /* Opcode: NotFound P1 P2 P3 P4 * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3393 | ** |
drh | 8cff69d | 2009-11-12 19:59:44 +0000 | [diff] [blame] | 3394 | ** If P4==0 then register P3 holds a blob constructed by MakeRecord. If |
| 3395 | ** P4>0 then register P3 is the first of P4 registers that form an unpacked |
| 3396 | ** record. |
| 3397 | ** |
| 3398 | ** Cursor P1 is on an index btree. If the record identified by P3 and P4 |
| 3399 | ** is not the prefix of any entry in P1 then a jump is made to P2. If P1 |
| 3400 | ** does contain an entry whose prefix matches the P3/P4 record then control |
| 3401 | ** falls through to the next instruction and P1 is left pointing at the |
| 3402 | ** matching entry. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3403 | ** |
drh | cb6d50e | 2008-08-21 19:28:30 +0000 | [diff] [blame] | 3404 | ** See also: Found, NotExists, IsUnique |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3405 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3406 | case OP_NotFound: /* jump, in3 */ |
| 3407 | case OP_Found: { /* jump, in3 */ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3408 | int alreadyExists; |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 3409 | VdbeCursor *pC; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3410 | int res; |
| 3411 | UnpackedRecord *pIdxKey; |
drh | 8cff69d | 2009-11-12 19:59:44 +0000 | [diff] [blame] | 3412 | UnpackedRecord r; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3413 | char aTempRec[ROUND8(sizeof(UnpackedRecord)) + sizeof(Mem)*3 + 7]; |
| 3414 | |
dan | 0ff297e | 2009-09-25 17:03:14 +0000 | [diff] [blame] | 3415 | #ifdef SQLITE_TEST |
| 3416 | sqlite3_found_count++; |
| 3417 | #endif |
| 3418 | |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3419 | alreadyExists = 0; |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 3420 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
drh | 8cff69d | 2009-11-12 19:59:44 +0000 | [diff] [blame] | 3421 | assert( pOp->p4type==P4_INT32 ); |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 3422 | pC = p->apCsr[pOp->p1]; |
| 3423 | assert( pC!=0 ); |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 3424 | pIn3 = &aMem[pOp->p3]; |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 3425 | if( ALWAYS(pC->pCursor!=0) ){ |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 3426 | |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3427 | assert( pC->isTable==0 ); |
drh | 8cff69d | 2009-11-12 19:59:44 +0000 | [diff] [blame] | 3428 | if( pOp->p4.i>0 ){ |
| 3429 | r.pKeyInfo = pC->pKeyInfo; |
shaneh | 5e17e8b | 2009-12-03 04:40:47 +0000 | [diff] [blame] | 3430 | r.nField = (u16)pOp->p4.i; |
drh | 8cff69d | 2009-11-12 19:59:44 +0000 | [diff] [blame] | 3431 | r.aMem = pIn3; |
| 3432 | r.flags = UNPACKED_PREFIX_MATCH; |
| 3433 | pIdxKey = &r; |
| 3434 | }else{ |
| 3435 | assert( pIn3->flags & MEM_Blob ); |
| 3436 | ExpandBlob(pIn3); |
| 3437 | pIdxKey = sqlite3VdbeRecordUnpack(pC->pKeyInfo, pIn3->n, pIn3->z, |
| 3438 | aTempRec, sizeof(aTempRec)); |
| 3439 | if( pIdxKey==0 ){ |
| 3440 | goto no_mem; |
| 3441 | } |
| 3442 | pIdxKey->flags |= UNPACKED_PREFIX_MATCH; |
danielk1977 | 9a96b66 | 2007-11-29 17:05:18 +0000 | [diff] [blame] | 3443 | } |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 3444 | rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, pIdxKey, 0, 0, &res); |
drh | 8cff69d | 2009-11-12 19:59:44 +0000 | [diff] [blame] | 3445 | if( pOp->p4.i==0 ){ |
| 3446 | sqlite3VdbeDeleteUnpackedRecord(pIdxKey); |
| 3447 | } |
danielk1977 | 7751940 | 2007-08-30 11:48:31 +0000 | [diff] [blame] | 3448 | if( rc!=SQLITE_OK ){ |
| 3449 | break; |
| 3450 | } |
| 3451 | alreadyExists = (res==0); |
drh | a11846b | 2004-01-07 18:52:56 +0000 | [diff] [blame] | 3452 | pC->deferredMoveto = 0; |
drh | 76873ab | 2006-01-07 18:48:26 +0000 | [diff] [blame] | 3453 | pC->cacheStatus = CACHE_STALE; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3454 | } |
| 3455 | if( pOp->opcode==OP_Found ){ |
| 3456 | if( alreadyExists ) pc = pOp->p2 - 1; |
| 3457 | }else{ |
| 3458 | if( !alreadyExists ) pc = pOp->p2 - 1; |
| 3459 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3460 | break; |
| 3461 | } |
| 3462 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 3463 | /* Opcode: IsUnique P1 P2 P3 P4 * |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 3464 | ** |
drh | 8cff69d | 2009-11-12 19:59:44 +0000 | [diff] [blame] | 3465 | ** Cursor P1 is open on an index b-tree - that is to say, a btree which |
| 3466 | ** no data and where the key are records generated by OP_MakeRecord with |
| 3467 | ** the list field being the integer ROWID of the entry that the index |
| 3468 | ** entry refers to. |
danielk1977 | de63035 | 2009-05-04 11:42:29 +0000 | [diff] [blame] | 3469 | ** |
| 3470 | ** The P3 register contains an integer record number. Call this record |
| 3471 | ** number R. Register P4 is the first in a set of N contiguous registers |
| 3472 | ** that make up an unpacked index key that can be used with cursor P1. |
| 3473 | ** The value of N can be inferred from the cursor. N includes the rowid |
| 3474 | ** value appended to the end of the index record. This rowid value may |
| 3475 | ** or may not be the same as R. |
| 3476 | ** |
| 3477 | ** If any of the N registers beginning with register P4 contains a NULL |
| 3478 | ** value, jump immediately to P2. |
| 3479 | ** |
| 3480 | ** Otherwise, this instruction checks if cursor P1 contains an entry |
| 3481 | ** where the first (N-1) fields match but the rowid value at the end |
| 3482 | ** of the index entry is not R. If there is no such entry, control jumps |
| 3483 | ** to instruction P2. Otherwise, the rowid of the conflicting index |
| 3484 | ** entry is copied to register P3 and control falls through to the next |
| 3485 | ** instruction. |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 3486 | ** |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3487 | ** See also: NotFound, NotExists, Found |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 3488 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3489 | case OP_IsUnique: { /* jump, in3 */ |
shane | 60a4b53 | 2009-05-06 18:57:09 +0000 | [diff] [blame] | 3490 | u16 ii; |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 3491 | VdbeCursor *pCx; |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 3492 | BtCursor *pCrsr; |
shane | 60a4b53 | 2009-05-06 18:57:09 +0000 | [diff] [blame] | 3493 | u16 nField; |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 3494 | Mem *aMx; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3495 | UnpackedRecord r; /* B-Tree index search key */ |
| 3496 | i64 R; /* Rowid stored in register P3 */ |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 3497 | |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 3498 | pIn3 = &aMem[pOp->p3]; |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 3499 | aMx = &aMem[pOp->p4.i]; |
danielk1977 | de63035 | 2009-05-04 11:42:29 +0000 | [diff] [blame] | 3500 | /* Assert that the values of parameters P1 and P4 are in range. */ |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 3501 | assert( pOp->p4type==P4_INT32 ); |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3502 | assert( pOp->p4.i>0 && pOp->p4.i<=p->nMem ); |
danielk1977 | de63035 | 2009-05-04 11:42:29 +0000 | [diff] [blame] | 3503 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 3504 | |
| 3505 | /* Find the index cursor. */ |
| 3506 | pCx = p->apCsr[pOp->p1]; |
| 3507 | assert( pCx->deferredMoveto==0 ); |
| 3508 | pCx->seekResult = 0; |
| 3509 | pCx->cacheStatus = CACHE_STALE; |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 3510 | pCrsr = pCx->pCursor; |
danielk1977 | de63035 | 2009-05-04 11:42:29 +0000 | [diff] [blame] | 3511 | |
| 3512 | /* If any of the values are NULL, take the jump. */ |
| 3513 | nField = pCx->pKeyInfo->nField; |
| 3514 | for(ii=0; ii<nField; ii++){ |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 3515 | if( aMx[ii].flags & MEM_Null ){ |
danielk1977 | de63035 | 2009-05-04 11:42:29 +0000 | [diff] [blame] | 3516 | pc = pOp->p2 - 1; |
| 3517 | pCrsr = 0; |
| 3518 | break; |
| 3519 | } |
| 3520 | } |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 3521 | assert( (aMx[nField].flags & MEM_Null)==0 ); |
danielk1977 | de63035 | 2009-05-04 11:42:29 +0000 | [diff] [blame] | 3522 | |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 3523 | if( pCrsr!=0 ){ |
danielk1977 | de63035 | 2009-05-04 11:42:29 +0000 | [diff] [blame] | 3524 | /* Populate the index search key. */ |
| 3525 | r.pKeyInfo = pCx->pKeyInfo; |
| 3526 | r.nField = nField + 1; |
| 3527 | r.flags = UNPACKED_PREFIX_SEARCH; |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 3528 | r.aMem = aMx; |
danielk1977 | 452c989 | 2004-05-13 05:16:15 +0000 | [diff] [blame] | 3529 | |
danielk1977 | de63035 | 2009-05-04 11:42:29 +0000 | [diff] [blame] | 3530 | /* Extract the value of R from register P3. */ |
| 3531 | sqlite3VdbeMemIntegerify(pIn3); |
| 3532 | R = pIn3->u.i; |
| 3533 | |
| 3534 | /* Search the B-Tree index. If no conflicting record is found, jump |
| 3535 | ** to P2. Otherwise, copy the rowid of the conflicting record to |
| 3536 | ** register P3 and fall through to the next instruction. */ |
| 3537 | rc = sqlite3BtreeMovetoUnpacked(pCrsr, &r, 0, 0, &pCx->seekResult); |
| 3538 | if( (r.flags & UNPACKED_PREFIX_SEARCH) || r.rowid==R ){ |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 3539 | pc = pOp->p2 - 1; |
danielk1977 | de63035 | 2009-05-04 11:42:29 +0000 | [diff] [blame] | 3540 | }else{ |
| 3541 | pIn3->u.i = r.rowid; |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 3542 | } |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 3543 | } |
| 3544 | break; |
| 3545 | } |
| 3546 | |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3547 | /* Opcode: NotExists P1 P2 P3 * * |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 3548 | ** |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3549 | ** Use the content of register P3 as a integer key. If a record |
danielk1977 | 96cb76f | 2008-01-04 13:24:28 +0000 | [diff] [blame] | 3550 | ** with that key does not exist in table of P1, then jump to P2. |
| 3551 | ** If the record does exist, then fall thru. The cursor is left |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3552 | ** pointing to the record if it exists. |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 3553 | ** |
| 3554 | ** The difference between this operation and NotFound is that this |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3555 | ** operation assumes the key is an integer and that P1 is a table whereas |
| 3556 | ** NotFound assumes key is a blob constructed from MakeRecord and |
| 3557 | ** P1 is an index. |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 3558 | ** |
drh | cb6d50e | 2008-08-21 19:28:30 +0000 | [diff] [blame] | 3559 | ** See also: Found, NotFound, IsUnique |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 3560 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3561 | case OP_NotExists: { /* jump, in3 */ |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 3562 | VdbeCursor *pC; |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 3563 | BtCursor *pCrsr; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3564 | int res; |
| 3565 | u64 iKey; |
| 3566 | |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 3567 | pIn3 = &aMem[pOp->p3]; |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 3568 | assert( pIn3->flags & MEM_Int ); |
| 3569 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 3570 | pC = p->apCsr[pOp->p1]; |
| 3571 | assert( pC!=0 ); |
| 3572 | assert( pC->isTable ); |
drh | 3e9ca09 | 2009-09-08 01:14:48 +0000 | [diff] [blame] | 3573 | assert( pC->pseudoTableReg==0 ); |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 3574 | pCrsr = pC->pCursor; |
| 3575 | if( pCrsr!=0 ){ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3576 | res = 0; |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 3577 | iKey = pIn3->u.i; |
danielk1977 | de63035 | 2009-05-04 11:42:29 +0000 | [diff] [blame] | 3578 | rc = sqlite3BtreeMovetoUnpacked(pCrsr, 0, iKey, 0, &res); |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 3579 | pC->lastRowid = pIn3->u.i; |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 3580 | pC->rowidIsValid = res==0 ?1:0; |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 3581 | pC->nullRow = 0; |
drh | 76873ab | 2006-01-07 18:48:26 +0000 | [diff] [blame] | 3582 | pC->cacheStatus = CACHE_STALE; |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3583 | pC->deferredMoveto = 0; |
danielk1977 | 2812956 | 2005-01-11 10:25:06 +0000 | [diff] [blame] | 3584 | if( res!=0 ){ |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 3585 | pc = pOp->p2 - 1; |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 3586 | assert( pC->rowidIsValid==0 ); |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 3587 | } |
danielk1977 | de63035 | 2009-05-04 11:42:29 +0000 | [diff] [blame] | 3588 | pC->seekResult = res; |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 3589 | }else{ |
danielk1977 | f7b9d66 | 2008-06-23 18:49:43 +0000 | [diff] [blame] | 3590 | /* This happens when an attempt to open a read cursor on the |
| 3591 | ** sqlite_master table returns SQLITE_EMPTY. |
| 3592 | */ |
danielk1977 | f7b9d66 | 2008-06-23 18:49:43 +0000 | [diff] [blame] | 3593 | pc = pOp->p2 - 1; |
| 3594 | assert( pC->rowidIsValid==0 ); |
danielk1977 | de63035 | 2009-05-04 11:42:29 +0000 | [diff] [blame] | 3595 | pC->seekResult = 0; |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 3596 | } |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 3597 | break; |
| 3598 | } |
| 3599 | |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 3600 | /* Opcode: Sequence P1 P2 * * * |
drh | 4db38a7 | 2005-09-01 12:16:28 +0000 | [diff] [blame] | 3601 | ** |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 3602 | ** Find the next available sequence number for cursor P1. |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3603 | ** Write the sequence number into register P2. |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 3604 | ** The sequence number on the cursor is incremented after this |
| 3605 | ** instruction. |
drh | 4db38a7 | 2005-09-01 12:16:28 +0000 | [diff] [blame] | 3606 | */ |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 3607 | case OP_Sequence: { /* out2-prerelease */ |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 3608 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 3609 | assert( p->apCsr[pOp->p1]!=0 ); |
| 3610 | pOut->u.i = p->apCsr[pOp->p1]->seqCount++; |
drh | 4db38a7 | 2005-09-01 12:16:28 +0000 | [diff] [blame] | 3611 | break; |
| 3612 | } |
| 3613 | |
| 3614 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 3615 | /* Opcode: NewRowid P1 P2 P3 * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3616 | ** |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3617 | ** 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] | 3618 | ** The record number is not previously used as a key in the database |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3619 | ** table that cursor P1 points to. The new record number is written |
| 3620 | ** written to register P2. |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 3621 | ** |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 3622 | ** If P3>0 then P3 is a register in the root frame of this VDBE that holds |
| 3623 | ** the largest previously generated record number. No new record numbers are |
| 3624 | ** allowed to be less than this value. When this value reaches its maximum, |
| 3625 | ** a SQLITE_FULL error is generated. The P3 register is updated with the ' |
| 3626 | ** generated record number. This P3 mechanism is used to help implement the |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 3627 | ** AUTOINCREMENT feature. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3628 | */ |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 3629 | case OP_NewRowid: { /* out2-prerelease */ |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 3630 | i64 v; /* The new rowid */ |
| 3631 | VdbeCursor *pC; /* Cursor of table to get the new rowid */ |
| 3632 | int res; /* Result of an sqlite3BtreeLast() */ |
| 3633 | int cnt; /* Counter to limit the number of searches */ |
| 3634 | Mem *pMem; /* Register holding largest rowid for AUTOINCREMENT */ |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 3635 | VdbeFrame *pFrame; /* Root frame of VDBE */ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3636 | |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3637 | v = 0; |
| 3638 | res = 0; |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 3639 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 3640 | pC = p->apCsr[pOp->p1]; |
| 3641 | assert( pC!=0 ); |
| 3642 | if( NEVER(pC->pCursor==0) ){ |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 3643 | /* The zero initialization above is all that is needed */ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3644 | }else{ |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 3645 | /* The next rowid or record number (different terms for the same |
| 3646 | ** thing) is obtained in a two-step algorithm. |
| 3647 | ** |
| 3648 | ** First we attempt to find the largest existing rowid and add one |
| 3649 | ** to that. But if the largest existing rowid is already the maximum |
| 3650 | ** positive integer, we have to fall through to the second |
| 3651 | ** probabilistic algorithm |
| 3652 | ** |
| 3653 | ** The second algorithm is to select a rowid at random and see if |
| 3654 | ** it already exists in the table. If it does not exist, we have |
| 3655 | ** succeeded. If the random rowid does exist, we select a new one |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 3656 | ** and try again, up to 100 times. |
drh | db5ed6d | 2001-09-18 22:17:44 +0000 | [diff] [blame] | 3657 | */ |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 3658 | assert( pC->isTable ); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3659 | cnt = 0; |
drh | fe2093d | 2005-01-20 22:48:47 +0000 | [diff] [blame] | 3660 | |
drh | 75f86a4 | 2005-02-17 00:03:06 +0000 | [diff] [blame] | 3661 | #ifdef SQLITE_32BIT_ROWID |
| 3662 | # define MAX_ROWID 0x7fffffff |
| 3663 | #else |
drh | fe2093d | 2005-01-20 22:48:47 +0000 | [diff] [blame] | 3664 | /* Some compilers complain about constants of the form 0x7fffffffffffffff. |
| 3665 | ** Others complain about 0x7ffffffffffffffffLL. The following macro seems |
| 3666 | ** to provide the constant while making all compilers happy. |
| 3667 | */ |
danielk1977 | 64202cf | 2008-11-17 15:31:47 +0000 | [diff] [blame] | 3668 | # define MAX_ROWID (i64)( (((u64)0x7fffffff)<<32) | (u64)0xffffffff ) |
drh | 75f86a4 | 2005-02-17 00:03:06 +0000 | [diff] [blame] | 3669 | #endif |
drh | fe2093d | 2005-01-20 22:48:47 +0000 | [diff] [blame] | 3670 | |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 3671 | if( !pC->useRandomRowid ){ |
drh | 7f75122 | 2009-03-17 22:33:00 +0000 | [diff] [blame] | 3672 | v = sqlite3BtreeGetCachedRowid(pC->pCursor); |
| 3673 | if( v==0 ){ |
danielk1977 | 261919c | 2005-12-06 12:52:59 +0000 | [diff] [blame] | 3674 | rc = sqlite3BtreeLast(pC->pCursor, &res); |
| 3675 | if( rc!=SQLITE_OK ){ |
| 3676 | goto abort_due_to_error; |
| 3677 | } |
drh | 32fbe34 | 2002-10-19 20:16:37 +0000 | [diff] [blame] | 3678 | if( res ){ |
drh | c79c761 | 2010-01-01 18:57:48 +0000 | [diff] [blame] | 3679 | v = 1; /* IMP: R-61914-48074 */ |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 3680 | }else{ |
drh | ea8ffdf | 2009-07-22 00:35:23 +0000 | [diff] [blame] | 3681 | assert( sqlite3BtreeCursorIsValid(pC->pCursor) ); |
drh | c27ae61 | 2009-07-14 18:35:44 +0000 | [diff] [blame] | 3682 | rc = sqlite3BtreeKeySize(pC->pCursor, &v); |
| 3683 | assert( rc==SQLITE_OK ); /* Cannot fail following BtreeLast() */ |
drh | 75f86a4 | 2005-02-17 00:03:06 +0000 | [diff] [blame] | 3684 | if( v==MAX_ROWID ){ |
drh | 32fbe34 | 2002-10-19 20:16:37 +0000 | [diff] [blame] | 3685 | pC->useRandomRowid = 1; |
| 3686 | }else{ |
drh | c79c761 | 2010-01-01 18:57:48 +0000 | [diff] [blame] | 3687 | v++; /* IMP: R-29538-34987 */ |
drh | 32fbe34 | 2002-10-19 20:16:37 +0000 | [diff] [blame] | 3688 | } |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 3689 | } |
drh | 3fc190c | 2001-09-14 03:24:23 +0000 | [diff] [blame] | 3690 | } |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 3691 | |
| 3692 | #ifndef SQLITE_OMIT_AUTOINCREMENT |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 3693 | if( pOp->p3 ){ |
shane | abc6b89 | 2009-09-10 19:09:03 +0000 | [diff] [blame] | 3694 | /* Assert that P3 is a valid memory cell. */ |
| 3695 | assert( pOp->p3>0 ); |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 3696 | if( p->pFrame ){ |
| 3697 | for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent); |
shane | abc6b89 | 2009-09-10 19:09:03 +0000 | [diff] [blame] | 3698 | /* Assert that P3 is a valid memory cell. */ |
| 3699 | assert( pOp->p3<=pFrame->nMem ); |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 3700 | pMem = &pFrame->aMem[pOp->p3]; |
| 3701 | }else{ |
shane | abc6b89 | 2009-09-10 19:09:03 +0000 | [diff] [blame] | 3702 | /* Assert that P3 is a valid memory cell. */ |
| 3703 | assert( pOp->p3<=p->nMem ); |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 3704 | pMem = &aMem[pOp->p3]; |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 3705 | } |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 3706 | |
| 3707 | REGISTER_TRACE(pOp->p3, pMem); |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 3708 | sqlite3VdbeMemIntegerify(pMem); |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 3709 | assert( (pMem->flags & MEM_Int)!=0 ); /* mem(P3) holds an integer */ |
drh | 3c024d6 | 2007-03-30 11:23:45 +0000 | [diff] [blame] | 3710 | if( pMem->u.i==MAX_ROWID || pC->useRandomRowid ){ |
drh | c79c761 | 2010-01-01 18:57:48 +0000 | [diff] [blame] | 3711 | rc = SQLITE_FULL; /* IMP: R-12275-61338 */ |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 3712 | goto abort_due_to_error; |
| 3713 | } |
drh | 3c024d6 | 2007-03-30 11:23:45 +0000 | [diff] [blame] | 3714 | if( v<pMem->u.i+1 ){ |
| 3715 | v = pMem->u.i + 1; |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 3716 | } |
drh | 3c024d6 | 2007-03-30 11:23:45 +0000 | [diff] [blame] | 3717 | pMem->u.i = v; |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 3718 | } |
| 3719 | #endif |
| 3720 | |
drh | 7f75122 | 2009-03-17 22:33:00 +0000 | [diff] [blame] | 3721 | sqlite3BtreeSetCachedRowid(pC->pCursor, v<MAX_ROWID ? v+1 : 0); |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 3722 | } |
| 3723 | if( pC->useRandomRowid ){ |
drh | c79c761 | 2010-01-01 18:57:48 +0000 | [diff] [blame] | 3724 | /* IMPLEMENTATION-OF: R-48598-02938 If the largest ROWID is equal to the |
| 3725 | ** largest possible integer (9223372036854775807) then the database |
| 3726 | ** engine starts picking candidate ROWIDs at random until it finds one |
| 3727 | ** that is not previously used. |
| 3728 | */ |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 3729 | assert( pOp->p3==0 ); /* We cannot be in random rowid mode if this is |
| 3730 | ** an AUTOINCREMENT table. */ |
drh | 9ed7a99 | 2009-06-26 15:14:55 +0000 | [diff] [blame] | 3731 | v = db->lastRowid; |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 3732 | cnt = 0; |
| 3733 | do{ |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 3734 | if( cnt==0 && (v&0xffffff)==v ){ |
| 3735 | v++; |
| 3736 | }else{ |
drh | 2fa1868 | 2008-03-19 14:15:34 +0000 | [diff] [blame] | 3737 | sqlite3_randomness(sizeof(v), &v); |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 3738 | if( cnt<5 ) v &= 0xffffff; |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 3739 | } |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 3740 | rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, 0, (u64)v, 0, &res); |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 3741 | cnt++; |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 3742 | }while( cnt<100 && rc==SQLITE_OK && res==0 ); |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 3743 | if( rc==SQLITE_OK && res==0 ){ |
drh | c79c761 | 2010-01-01 18:57:48 +0000 | [diff] [blame] | 3744 | rc = SQLITE_FULL; /* IMP: R-38219-53002 */ |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 3745 | goto abort_due_to_error; |
| 3746 | } |
drh | 1eaa269 | 2001-09-18 02:02:23 +0000 | [diff] [blame] | 3747 | } |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3748 | pC->rowidIsValid = 0; |
drh | a11846b | 2004-01-07 18:52:56 +0000 | [diff] [blame] | 3749 | pC->deferredMoveto = 0; |
drh | 76873ab | 2006-01-07 18:48:26 +0000 | [diff] [blame] | 3750 | pC->cacheStatus = CACHE_STALE; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3751 | } |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 3752 | pOut->u.i = v; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3753 | break; |
| 3754 | } |
| 3755 | |
danielk1977 | 1f4aa33 | 2008-01-03 09:51:55 +0000 | [diff] [blame] | 3756 | /* Opcode: Insert P1 P2 P3 P4 P5 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3757 | ** |
jplyon | 5a56422 | 2003-06-02 06:15:58 +0000 | [diff] [blame] | 3758 | ** Write an entry into the table of cursor P1. A new entry is |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 3759 | ** created if it doesn't already exist or the data for an existing |
drh | 3e9ca09 | 2009-09-08 01:14:48 +0000 | [diff] [blame] | 3760 | ** entry is overwritten. The data is the value MEM_Blob stored in register |
danielk1977 | 1f4aa33 | 2008-01-03 09:51:55 +0000 | [diff] [blame] | 3761 | ** number P2. The key is stored in register P3. The key must |
drh | 3e9ca09 | 2009-09-08 01:14:48 +0000 | [diff] [blame] | 3762 | ** be a MEM_Int. |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 3763 | ** |
danielk1977 | 1f4aa33 | 2008-01-03 09:51:55 +0000 | [diff] [blame] | 3764 | ** If the OPFLAG_NCHANGE flag of P5 is set, then the row change count is |
| 3765 | ** incremented (otherwise not). If the OPFLAG_LASTROWID flag of P5 is set, |
danielk1977 | b28af71 | 2004-06-21 06:50:26 +0000 | [diff] [blame] | 3766 | ** then rowid is stored for subsequent return by the |
drh | 85b623f | 2007-12-13 21:54:09 +0000 | [diff] [blame] | 3767 | ** sqlite3_last_insert_rowid() function (otherwise it is unmodified). |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 3768 | ** |
drh | 3e9ca09 | 2009-09-08 01:14:48 +0000 | [diff] [blame] | 3769 | ** If the OPFLAG_USESEEKRESULT flag of P5 is set and if the result of |
| 3770 | ** the last seek operation (OP_NotExists) was a success, then this |
| 3771 | ** operation will not attempt to find the appropriate row before doing |
| 3772 | ** the insert but will instead overwrite the row that the cursor is |
| 3773 | ** currently pointing to. Presumably, the prior OP_NotExists opcode |
| 3774 | ** has already positioned the cursor correctly. This is an optimization |
| 3775 | ** that boosts performance by avoiding redundant seeks. |
| 3776 | ** |
| 3777 | ** If the OPFLAG_ISUPDATE flag is set, then this opcode is part of an |
| 3778 | ** UPDATE operation. Otherwise (if the flag is clear) then this opcode |
| 3779 | ** is part of an INSERT operation. The difference is only important to |
| 3780 | ** the update hook. |
| 3781 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 3782 | ** Parameter P4 may point to a string containing the table-name, or |
danielk1977 | 1f6eec5 | 2006-06-16 06:17:47 +0000 | [diff] [blame] | 3783 | ** may be NULL. If it is not NULL, then the update-hook |
| 3784 | ** (sqlite3.xUpdateCallback) is invoked following a successful insert. |
| 3785 | ** |
drh | 93aed5a | 2008-01-16 17:46:38 +0000 | [diff] [blame] | 3786 | ** (WARNING/TODO: If P1 is a pseudo-cursor and P2 is dynamically |
| 3787 | ** allocated, then ownership of P2 is transferred to the pseudo-cursor |
| 3788 | ** and register P2 becomes ephemeral. If the cursor is changed, the |
| 3789 | ** value of register P2 will then change. Make sure this does not |
| 3790 | ** cause any problems.) |
| 3791 | ** |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3792 | ** This instruction only works on tables. The equivalent instruction |
| 3793 | ** for indices is OP_IdxInsert. |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 3794 | */ |
drh | e05c929 | 2009-10-29 13:48:10 +0000 | [diff] [blame] | 3795 | /* Opcode: InsertInt P1 P2 P3 P4 P5 |
| 3796 | ** |
| 3797 | ** This works exactly like OP_Insert except that the key is the |
| 3798 | ** integer value P3, not the value of the integer stored in register P3. |
| 3799 | */ |
| 3800 | case OP_Insert: |
| 3801 | case OP_InsertInt: { |
drh | 3e9ca09 | 2009-09-08 01:14:48 +0000 | [diff] [blame] | 3802 | Mem *pData; /* MEM cell holding data for the record to be inserted */ |
| 3803 | Mem *pKey; /* MEM cell holding key for the record */ |
| 3804 | i64 iKey; /* The integer ROWID or key for the record to be inserted */ |
| 3805 | VdbeCursor *pC; /* Cursor to table into which insert is written */ |
| 3806 | int nZero; /* Number of zero-bytes to append */ |
| 3807 | int seekResult; /* Result of prior seek or 0 if no USESEEKRESULT flag */ |
| 3808 | const char *zDb; /* database name - used by the update hook */ |
| 3809 | const char *zTbl; /* Table name - used by the opdate hook */ |
| 3810 | int op; /* Opcode for update hook: SQLITE_UPDATE or SQLITE_INSERT */ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3811 | |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 3812 | pData = &aMem[pOp->p2]; |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 3813 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 3814 | pC = p->apCsr[pOp->p1]; |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 3815 | assert( pC!=0 ); |
drh | 3e9ca09 | 2009-09-08 01:14:48 +0000 | [diff] [blame] | 3816 | assert( pC->pCursor!=0 ); |
| 3817 | assert( pC->pseudoTableReg==0 ); |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 3818 | assert( pC->isTable ); |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 3819 | REGISTER_TRACE(pOp->p2, pData); |
danielk1977 | 5f8d8a8 | 2004-05-11 00:28:42 +0000 | [diff] [blame] | 3820 | |
drh | e05c929 | 2009-10-29 13:48:10 +0000 | [diff] [blame] | 3821 | if( pOp->opcode==OP_Insert ){ |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 3822 | pKey = &aMem[pOp->p3]; |
drh | e05c929 | 2009-10-29 13:48:10 +0000 | [diff] [blame] | 3823 | assert( pKey->flags & MEM_Int ); |
| 3824 | REGISTER_TRACE(pOp->p3, pKey); |
| 3825 | iKey = pKey->u.i; |
| 3826 | }else{ |
| 3827 | assert( pOp->opcode==OP_InsertInt ); |
| 3828 | iKey = pOp->p3; |
| 3829 | } |
| 3830 | |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 3831 | if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++; |
drh | e05c929 | 2009-10-29 13:48:10 +0000 | [diff] [blame] | 3832 | if( pOp->p5 & OPFLAG_LASTROWID ) db->lastRowid = iKey; |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 3833 | if( pData->flags & MEM_Null ){ |
| 3834 | pData->z = 0; |
| 3835 | pData->n = 0; |
| 3836 | }else{ |
| 3837 | assert( pData->flags & (MEM_Blob|MEM_Str) ); |
| 3838 | } |
drh | 3e9ca09 | 2009-09-08 01:14:48 +0000 | [diff] [blame] | 3839 | seekResult = ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0); |
| 3840 | if( pData->flags & MEM_Zero ){ |
| 3841 | nZero = pData->u.nZero; |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 3842 | }else{ |
drh | 3e9ca09 | 2009-09-08 01:14:48 +0000 | [diff] [blame] | 3843 | nZero = 0; |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 3844 | } |
drh | 3e9ca09 | 2009-09-08 01:14:48 +0000 | [diff] [blame] | 3845 | sqlite3BtreeSetCachedRowid(pC->pCursor, 0); |
| 3846 | rc = sqlite3BtreeInsert(pC->pCursor, 0, iKey, |
| 3847 | pData->z, pData->n, nZero, |
| 3848 | pOp->p5 & OPFLAG_APPEND, seekResult |
| 3849 | ); |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 3850 | pC->rowidIsValid = 0; |
| 3851 | pC->deferredMoveto = 0; |
| 3852 | pC->cacheStatus = CACHE_STALE; |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 +0000 | [diff] [blame] | 3853 | |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 3854 | /* Invoke the update-hook if required. */ |
| 3855 | if( rc==SQLITE_OK && db->xUpdateCallback && pOp->p4.z ){ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3856 | zDb = db->aDb[pC->iDb].zName; |
| 3857 | zTbl = pOp->p4.z; |
| 3858 | op = ((pOp->p5 & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_INSERT); |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 3859 | assert( pC->isTable ); |
| 3860 | db->xUpdateCallback(db->pUpdateArg, op, zDb, zTbl, iKey); |
| 3861 | assert( pC->iDb>=0 ); |
| 3862 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3863 | break; |
| 3864 | } |
| 3865 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 3866 | /* Opcode: Delete P1 P2 * P4 * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3867 | ** |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 3868 | ** Delete the record at which the P1 cursor is currently pointing. |
| 3869 | ** |
| 3870 | ** The cursor will be left pointing at either the next or the previous |
| 3871 | ** record in the table. If it is left pointing at the next record, then |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 3872 | ** the next Next instruction will be a no-op. Hence it is OK to delete |
| 3873 | ** a record from within an Next loop. |
drh | c8d30ac | 2002-04-12 10:08:59 +0000 | [diff] [blame] | 3874 | ** |
rdc | b0c374f | 2004-02-20 22:53:38 +0000 | [diff] [blame] | 3875 | ** If the OPFLAG_NCHANGE flag of P2 is set, then the row change count is |
danielk1977 | b28af71 | 2004-06-21 06:50:26 +0000 | [diff] [blame] | 3876 | ** incremented (otherwise not). |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3877 | ** |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 3878 | ** P1 must not be pseudo-table. It has to be a real table with |
| 3879 | ** multiple rows. |
| 3880 | ** |
| 3881 | ** If P4 is not NULL, then it is the name of the table that P1 is |
| 3882 | ** pointing to. The update hook will be invoked, if it exists. |
| 3883 | ** If P4 is not NULL then the P1 cursor must have been positioned |
| 3884 | ** using OP_NotFound prior to invoking this opcode. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3885 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3886 | case OP_Delete: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3887 | i64 iKey; |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 3888 | VdbeCursor *pC; |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 3889 | |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3890 | iKey = 0; |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 3891 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 3892 | pC = p->apCsr[pOp->p1]; |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 3893 | assert( pC!=0 ); |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 3894 | assert( pC->pCursor!=0 ); /* Only valid for real tables, no pseudotables */ |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 +0000 | [diff] [blame] | 3895 | |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 3896 | /* If the update-hook will be invoked, set iKey to the rowid of the |
| 3897 | ** row being deleted. |
| 3898 | */ |
| 3899 | if( db->xUpdateCallback && pOp->p4.z ){ |
| 3900 | assert( pC->isTable ); |
| 3901 | assert( pC->rowidIsValid ); /* lastRowid set by previous OP_NotFound */ |
| 3902 | iKey = pC->lastRowid; |
| 3903 | } |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 +0000 | [diff] [blame] | 3904 | |
drh | 9a65f2c | 2009-06-22 19:05:40 +0000 | [diff] [blame] | 3905 | /* The OP_Delete opcode always follows an OP_NotExists or OP_Last or |
| 3906 | ** OP_Column on the same table without any intervening operations that |
| 3907 | ** might move or invalidate the cursor. Hence cursor pC is always pointing |
| 3908 | ** to the row to be deleted and the sqlite3VdbeCursorMoveto() operation |
| 3909 | ** below is always a no-op and cannot fail. We will run it anyhow, though, |
| 3910 | ** to guard against future changes to the code generator. |
| 3911 | **/ |
| 3912 | assert( pC->deferredMoveto==0 ); |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 3913 | rc = sqlite3VdbeCursorMoveto(pC); |
drh | 9a65f2c | 2009-06-22 19:05:40 +0000 | [diff] [blame] | 3914 | if( NEVER(rc!=SQLITE_OK) ) goto abort_due_to_error; |
| 3915 | |
drh | 7f75122 | 2009-03-17 22:33:00 +0000 | [diff] [blame] | 3916 | sqlite3BtreeSetCachedRowid(pC->pCursor, 0); |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 3917 | rc = sqlite3BtreeDelete(pC->pCursor); |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 3918 | pC->cacheStatus = CACHE_STALE; |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 +0000 | [diff] [blame] | 3919 | |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 3920 | /* Invoke the update-hook if required. */ |
| 3921 | if( rc==SQLITE_OK && db->xUpdateCallback && pOp->p4.z ){ |
| 3922 | const char *zDb = db->aDb[pC->iDb].zName; |
| 3923 | const char *zTbl = pOp->p4.z; |
| 3924 | db->xUpdateCallback(db->pUpdateArg, SQLITE_DELETE, zDb, zTbl, iKey); |
| 3925 | assert( pC->iDb>=0 ); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3926 | } |
danielk1977 | b28af71 | 2004-06-21 06:50:26 +0000 | [diff] [blame] | 3927 | if( pOp->p2 & OPFLAG_NCHANGE ) p->nChange++; |
rdc | b0c374f | 2004-02-20 22:53:38 +0000 | [diff] [blame] | 3928 | break; |
| 3929 | } |
drh | b7f1d9a | 2009-09-08 02:27:58 +0000 | [diff] [blame] | 3930 | /* Opcode: ResetCount * * * * * |
rdc | b0c374f | 2004-02-20 22:53:38 +0000 | [diff] [blame] | 3931 | ** |
drh | b7f1d9a | 2009-09-08 02:27:58 +0000 | [diff] [blame] | 3932 | ** The value of the change counter is copied to the database handle |
| 3933 | ** change counter (returned by subsequent calls to sqlite3_changes()). |
| 3934 | ** Then the VMs internal change counter resets to 0. |
| 3935 | ** This is used by trigger programs. |
rdc | b0c374f | 2004-02-20 22:53:38 +0000 | [diff] [blame] | 3936 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3937 | case OP_ResetCount: { |
drh | b7f1d9a | 2009-09-08 02:27:58 +0000 | [diff] [blame] | 3938 | sqlite3VdbeSetChanges(db, p->nChange); |
danielk1977 | b28af71 | 2004-06-21 06:50:26 +0000 | [diff] [blame] | 3939 | p->nChange = 0; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3940 | break; |
| 3941 | } |
| 3942 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 3943 | /* Opcode: RowData P1 P2 * * * |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3944 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 3945 | ** Write into register P2 the complete row data for cursor P1. |
| 3946 | ** There is no interpretation of the data. |
| 3947 | ** It is just copied onto the P2 register exactly as |
danielk1977 | 96cb76f | 2008-01-04 13:24:28 +0000 | [diff] [blame] | 3948 | ** it is found in the database file. |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3949 | ** |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 3950 | ** If the P1 cursor must be pointing to a valid row (not a NULL row) |
| 3951 | ** of a real table, not a pseudo-table. |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3952 | */ |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 3953 | /* Opcode: RowKey P1 P2 * * * |
drh | 143f3c4 | 2004-01-07 20:37:52 +0000 | [diff] [blame] | 3954 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 3955 | ** Write into register P2 the complete row key for cursor P1. |
| 3956 | ** There is no interpretation of the data. |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3957 | ** The key is copied onto the P3 register exactly as |
danielk1977 | 96cb76f | 2008-01-04 13:24:28 +0000 | [diff] [blame] | 3958 | ** it is found in the database file. |
drh | 143f3c4 | 2004-01-07 20:37:52 +0000 | [diff] [blame] | 3959 | ** |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 3960 | ** If the P1 cursor must be pointing to a valid row (not a NULL row) |
| 3961 | ** of a real table, not a pseudo-table. |
drh | 143f3c4 | 2004-01-07 20:37:52 +0000 | [diff] [blame] | 3962 | */ |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 3963 | case OP_RowKey: |
| 3964 | case OP_RowData: { |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 3965 | VdbeCursor *pC; |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 3966 | BtCursor *pCrsr; |
danielk1977 | e0d4b06 | 2004-06-28 01:11:46 +0000 | [diff] [blame] | 3967 | u32 n; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3968 | i64 n64; |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3969 | |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 3970 | pOut = &aMem[pOp->p2]; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 3971 | |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3972 | /* Note that RowKey and RowData are really exactly the same instruction */ |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 3973 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 3974 | pC = p->apCsr[pOp->p1]; |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3975 | assert( pC->isTable || pOp->opcode==OP_RowKey ); |
| 3976 | assert( pC->isIndex || pOp->opcode==OP_RowData ); |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 3977 | assert( pC!=0 ); |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 3978 | assert( pC->nullRow==0 ); |
drh | 3e9ca09 | 2009-09-08 01:14:48 +0000 | [diff] [blame] | 3979 | assert( pC->pseudoTableReg==0 ); |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 3980 | assert( pC->pCursor!=0 ); |
| 3981 | pCrsr = pC->pCursor; |
drh | ea8ffdf | 2009-07-22 00:35:23 +0000 | [diff] [blame] | 3982 | assert( sqlite3BtreeCursorIsValid(pCrsr) ); |
drh | 9a65f2c | 2009-06-22 19:05:40 +0000 | [diff] [blame] | 3983 | |
| 3984 | /* The OP_RowKey and OP_RowData opcodes always follow OP_NotExists or |
| 3985 | ** OP_Rewind/Op_Next with no intervening instructions that might invalidate |
| 3986 | ** the cursor. Hence the following sqlite3VdbeCursorMoveto() call is always |
| 3987 | ** a no-op and can never fail. But we leave it in place as a safety. |
| 3988 | */ |
| 3989 | assert( pC->deferredMoveto==0 ); |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 3990 | rc = sqlite3VdbeCursorMoveto(pC); |
drh | 9a65f2c | 2009-06-22 19:05:40 +0000 | [diff] [blame] | 3991 | if( NEVER(rc!=SQLITE_OK) ) goto abort_due_to_error; |
| 3992 | |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 3993 | if( pC->isIndex ){ |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 3994 | assert( !pC->isTable ); |
drh | c27ae61 | 2009-07-14 18:35:44 +0000 | [diff] [blame] | 3995 | rc = sqlite3BtreeKeySize(pCrsr, &n64); |
| 3996 | assert( rc==SQLITE_OK ); /* True because of CursorMoveto() call above */ |
drh | bb4957f | 2008-03-20 14:03:29 +0000 | [diff] [blame] | 3997 | if( n64>db->aLimit[SQLITE_LIMIT_LENGTH] ){ |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 3998 | goto too_big; |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3999 | } |
drh | bfb19dc | 2009-06-05 16:46:53 +0000 | [diff] [blame] | 4000 | n = (u32)n64; |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 4001 | }else{ |
drh | c27ae61 | 2009-07-14 18:35:44 +0000 | [diff] [blame] | 4002 | rc = sqlite3BtreeDataSize(pCrsr, &n); |
drh | ea8ffdf | 2009-07-22 00:35:23 +0000 | [diff] [blame] | 4003 | assert( rc==SQLITE_OK ); /* DataSize() cannot fail */ |
shane | 75ac1de | 2009-06-09 18:58:52 +0000 | [diff] [blame] | 4004 | if( n>(u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){ |
drh | 023ae03 | 2007-05-08 12:12:16 +0000 | [diff] [blame] | 4005 | goto too_big; |
| 4006 | } |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 4007 | } |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 4008 | if( sqlite3VdbeMemGrow(pOut, n, 0) ){ |
| 4009 | goto no_mem; |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 4010 | } |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 4011 | pOut->n = n; |
| 4012 | MemSetTypeFlag(pOut, MEM_Blob); |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 4013 | if( pC->isIndex ){ |
| 4014 | rc = sqlite3BtreeKey(pCrsr, 0, n, pOut->z); |
| 4015 | }else{ |
| 4016 | rc = sqlite3BtreeData(pCrsr, 0, n, pOut->z); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4017 | } |
danielk1977 | 96cb76f | 2008-01-04 13:24:28 +0000 | [diff] [blame] | 4018 | pOut->enc = SQLITE_UTF8; /* In case the blob is ever cast to text */ |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 4019 | UPDATE_MAX_BLOBSIZE(pOut); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4020 | break; |
| 4021 | } |
| 4022 | |
drh | 2133d82 | 2008-01-03 18:44:59 +0000 | [diff] [blame] | 4023 | /* Opcode: Rowid P1 P2 * * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4024 | ** |
drh | 2133d82 | 2008-01-03 18:44:59 +0000 | [diff] [blame] | 4025 | ** 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] | 4026 | ** P1 is currently point to. |
drh | 044925b | 2009-04-22 17:15:02 +0000 | [diff] [blame] | 4027 | ** |
| 4028 | ** P1 can be either an ordinary table or a virtual table. There used to |
| 4029 | ** be a separate OP_VRowid opcode for use with virtual tables, but this |
| 4030 | ** one opcode now works for both table types. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4031 | */ |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 4032 | case OP_Rowid: { /* out2-prerelease */ |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 4033 | VdbeCursor *pC; |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 4034 | i64 v; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 4035 | sqlite3_vtab *pVtab; |
| 4036 | const sqlite3_module *pModule; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4037 | |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 4038 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 4039 | pC = p->apCsr[pOp->p1]; |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 4040 | assert( pC!=0 ); |
drh | 3e9ca09 | 2009-09-08 01:14:48 +0000 | [diff] [blame] | 4041 | assert( pC->pseudoTableReg==0 ); |
drh | 044925b | 2009-04-22 17:15:02 +0000 | [diff] [blame] | 4042 | if( pC->nullRow ){ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 4043 | pOut->flags = MEM_Null; |
drh | 044925b | 2009-04-22 17:15:02 +0000 | [diff] [blame] | 4044 | break; |
| 4045 | }else if( pC->deferredMoveto ){ |
drh | 6149526 | 2009-04-22 15:32:59 +0000 | [diff] [blame] | 4046 | v = pC->movetoTarget; |
drh | 044925b | 2009-04-22 17:15:02 +0000 | [diff] [blame] | 4047 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 4048 | }else if( pC->pVtabCursor ){ |
drh | 044925b | 2009-04-22 17:15:02 +0000 | [diff] [blame] | 4049 | pVtab = pC->pVtabCursor->pVtab; |
| 4050 | pModule = pVtab->pModule; |
| 4051 | assert( pModule->xRowid ); |
| 4052 | if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse; |
| 4053 | rc = pModule->xRowid(pC->pVtabCursor, &v); |
| 4054 | sqlite3DbFree(db, p->zErrMsg); |
| 4055 | p->zErrMsg = pVtab->zErrMsg; |
| 4056 | pVtab->zErrMsg = 0; |
| 4057 | if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse; |
| 4058 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 4059 | }else{ |
drh | 6be240e | 2009-07-14 02:33:02 +0000 | [diff] [blame] | 4060 | assert( pC->pCursor!=0 ); |
drh | 6149526 | 2009-04-22 15:32:59 +0000 | [diff] [blame] | 4061 | rc = sqlite3VdbeCursorMoveto(pC); |
| 4062 | if( rc ) goto abort_due_to_error; |
| 4063 | if( pC->rowidIsValid ){ |
| 4064 | v = pC->lastRowid; |
drh | 6149526 | 2009-04-22 15:32:59 +0000 | [diff] [blame] | 4065 | }else{ |
drh | c27ae61 | 2009-07-14 18:35:44 +0000 | [diff] [blame] | 4066 | rc = sqlite3BtreeKeySize(pC->pCursor, &v); |
| 4067 | assert( rc==SQLITE_OK ); /* Always so because of CursorMoveto() above */ |
drh | 6149526 | 2009-04-22 15:32:59 +0000 | [diff] [blame] | 4068 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4069 | } |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 4070 | pOut->u.i = v; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4071 | break; |
| 4072 | } |
| 4073 | |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4074 | /* Opcode: NullRow P1 * * * * |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 4075 | ** |
| 4076 | ** Move the cursor P1 to a null row. Any OP_Column operations |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4077 | ** that occur while the cursor is on the null row will always |
| 4078 | ** write a NULL. |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 4079 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4080 | case OP_NullRow: { |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 4081 | VdbeCursor *pC; |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 4082 | |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 4083 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 4084 | pC = p->apCsr[pOp->p1]; |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 4085 | assert( pC!=0 ); |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame] | 4086 | pC->nullRow = 1; |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 4087 | pC->rowidIsValid = 0; |
danielk1977 | be51a65 | 2008-10-08 17:58:48 +0000 | [diff] [blame] | 4088 | if( pC->pCursor ){ |
| 4089 | sqlite3BtreeClearCursor(pC->pCursor); |
| 4090 | } |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 4091 | break; |
| 4092 | } |
| 4093 | |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4094 | /* Opcode: Last P1 P2 * * * |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 4095 | ** |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 4096 | ** The next use of the Rowid or Column or Next instruction for P1 |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 4097 | ** will refer to the last entry in the database table or index. |
| 4098 | ** If the table or index is empty and P2>0, then jump immediately to P2. |
| 4099 | ** If P2 is 0 or if the table or index is not empty, fall through |
| 4100 | ** to the following instruction. |
| 4101 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4102 | case OP_Last: { /* jump */ |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 4103 | VdbeCursor *pC; |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 4104 | BtCursor *pCrsr; |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 4105 | int res; |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 4106 | |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 4107 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 4108 | pC = p->apCsr[pOp->p1]; |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 4109 | assert( pC!=0 ); |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 4110 | pCrsr = pC->pCursor; |
drh | 9a65f2c | 2009-06-22 19:05:40 +0000 | [diff] [blame] | 4111 | if( pCrsr==0 ){ |
| 4112 | res = 1; |
| 4113 | }else{ |
| 4114 | rc = sqlite3BtreeLast(pCrsr, &res); |
| 4115 | } |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 4116 | pC->nullRow = (u8)res; |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 4117 | pC->deferredMoveto = 0; |
drh | a7e7706 | 2009-01-14 00:55:09 +0000 | [diff] [blame] | 4118 | pC->rowidIsValid = 0; |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 4119 | pC->cacheStatus = CACHE_STALE; |
drh | 9a65f2c | 2009-06-22 19:05:40 +0000 | [diff] [blame] | 4120 | if( pOp->p2>0 && res ){ |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 4121 | pc = pOp->p2 - 1; |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 4122 | } |
| 4123 | break; |
| 4124 | } |
| 4125 | |
drh | 0342b1f | 2005-09-01 03:07:44 +0000 | [diff] [blame] | 4126 | |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4127 | /* Opcode: Sort P1 P2 * * * |
drh | 0342b1f | 2005-09-01 03:07:44 +0000 | [diff] [blame] | 4128 | ** |
| 4129 | ** This opcode does exactly the same thing as OP_Rewind except that |
| 4130 | ** it increments an undocumented global variable used for testing. |
| 4131 | ** |
| 4132 | ** Sorting is accomplished by writing records into a sorting index, |
| 4133 | ** then rewinding that index and playing it back from beginning to |
| 4134 | ** end. We use the OP_Sort opcode instead of OP_Rewind to do the |
| 4135 | ** rewinding so that the global variable will be incremented and |
| 4136 | ** regression tests can determine whether or not the optimizer is |
| 4137 | ** correctly optimizing out sorts. |
| 4138 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4139 | case OP_Sort: { /* jump */ |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 4140 | #ifdef SQLITE_TEST |
drh | 0342b1f | 2005-09-01 03:07:44 +0000 | [diff] [blame] | 4141 | sqlite3_sort_count++; |
drh | 4db38a7 | 2005-09-01 12:16:28 +0000 | [diff] [blame] | 4142 | sqlite3_search_count--; |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 4143 | #endif |
drh | d1d3848 | 2008-10-07 23:46:38 +0000 | [diff] [blame] | 4144 | p->aCounter[SQLITE_STMTSTATUS_SORT-1]++; |
drh | 0342b1f | 2005-09-01 03:07:44 +0000 | [diff] [blame] | 4145 | /* Fall through into OP_Rewind */ |
| 4146 | } |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4147 | /* Opcode: Rewind P1 P2 * * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4148 | ** |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 4149 | ** The next use of the Rowid or Column or Next instruction for P1 |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 4150 | ** will refer to the first entry in the database table or index. |
| 4151 | ** If the table or index is empty and P2>0, then jump immediately to P2. |
| 4152 | ** If P2 is 0 or if the table or index is not empty, fall through |
| 4153 | ** to the following instruction. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4154 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4155 | case OP_Rewind: { /* jump */ |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 4156 | VdbeCursor *pC; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4157 | BtCursor *pCrsr; |
drh | f4dada7 | 2004-05-11 09:57:35 +0000 | [diff] [blame] | 4158 | int res; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4159 | |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 4160 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 4161 | pC = p->apCsr[pOp->p1]; |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 4162 | assert( pC!=0 ); |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 4163 | if( (pCrsr = pC->pCursor)!=0 ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 4164 | rc = sqlite3BtreeFirst(pCrsr, &res); |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 4165 | pC->atFirst = res==0 ?1:0; |
drh | a11846b | 2004-01-07 18:52:56 +0000 | [diff] [blame] | 4166 | pC->deferredMoveto = 0; |
drh | 76873ab | 2006-01-07 18:48:26 +0000 | [diff] [blame] | 4167 | pC->cacheStatus = CACHE_STALE; |
drh | a7e7706 | 2009-01-14 00:55:09 +0000 | [diff] [blame] | 4168 | pC->rowidIsValid = 0; |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 4169 | }else{ |
drh | f4dada7 | 2004-05-11 09:57:35 +0000 | [diff] [blame] | 4170 | res = 1; |
| 4171 | } |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 4172 | pC->nullRow = (u8)res; |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 4173 | assert( pOp->p2>0 && pOp->p2<p->nOp ); |
| 4174 | if( res ){ |
drh | f4dada7 | 2004-05-11 09:57:35 +0000 | [diff] [blame] | 4175 | pc = pOp->p2 - 1; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4176 | } |
| 4177 | break; |
| 4178 | } |
| 4179 | |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4180 | /* Opcode: Next P1 P2 * * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4181 | ** |
| 4182 | ** 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] | 4183 | ** table or index. If there are no more key/value pairs then fall through |
| 4184 | ** to the following instruction. But if the cursor advance was successful, |
| 4185 | ** jump immediately to P2. |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 4186 | ** |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 4187 | ** The P1 cursor must be for a real table, not a pseudo-table. |
| 4188 | ** |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 4189 | ** See also: Prev |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 4190 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4191 | /* Opcode: Prev P1 P2 * * * |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 4192 | ** |
| 4193 | ** Back up cursor P1 so that it points to the previous key/data pair in its |
| 4194 | ** table or index. If there is no previous key/value pairs then fall through |
| 4195 | ** to the following instruction. But if the cursor backup was successful, |
| 4196 | ** jump immediately to P2. |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 4197 | ** |
| 4198 | ** The P1 cursor must be for a real table, not a pseudo-table. |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 4199 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4200 | case OP_Prev: /* jump */ |
| 4201 | case OP_Next: { /* jump */ |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 4202 | VdbeCursor *pC; |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 4203 | BtCursor *pCrsr; |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 4204 | int res; |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 4205 | |
drh | caec2f1 | 2003-01-07 02:47:47 +0000 | [diff] [blame] | 4206 | CHECK_FOR_INTERRUPT; |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 4207 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame] | 4208 | pC = p->apCsr[pOp->p1]; |
drh | 72e8fa4 | 2007-03-28 14:30:06 +0000 | [diff] [blame] | 4209 | if( pC==0 ){ |
| 4210 | break; /* See ticket #2273 */ |
| 4211 | } |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 4212 | pCrsr = pC->pCursor; |
drh | 9a65f2c | 2009-06-22 19:05:40 +0000 | [diff] [blame] | 4213 | if( pCrsr==0 ){ |
| 4214 | pC->nullRow = 1; |
| 4215 | break; |
| 4216 | } |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 4217 | res = 1; |
| 4218 | assert( pC->deferredMoveto==0 ); |
| 4219 | rc = pOp->opcode==OP_Next ? sqlite3BtreeNext(pCrsr, &res) : |
| 4220 | sqlite3BtreePrevious(pCrsr, &res); |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 4221 | pC->nullRow = (u8)res; |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 4222 | pC->cacheStatus = CACHE_STALE; |
| 4223 | if( res==0 ){ |
| 4224 | pc = pOp->p2 - 1; |
drh | d1d3848 | 2008-10-07 23:46:38 +0000 | [diff] [blame] | 4225 | if( pOp->p5 ) p->aCounter[pOp->p5-1]++; |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 4226 | #ifdef SQLITE_TEST |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 4227 | sqlite3_search_count++; |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 4228 | #endif |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 4229 | } |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 4230 | pC->rowidIsValid = 0; |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 4231 | break; |
| 4232 | } |
| 4233 | |
danielk1977 | de63035 | 2009-05-04 11:42:29 +0000 | [diff] [blame] | 4234 | /* Opcode: IdxInsert P1 P2 P3 * P5 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4235 | ** |
drh | aa9b896 | 2008-01-08 02:57:55 +0000 | [diff] [blame] | 4236 | ** Register P2 holds a SQL index key made using the |
drh | 9437bd2 | 2009-02-01 00:29:56 +0000 | [diff] [blame] | 4237 | ** MakeRecord instructions. This opcode writes that key |
drh | ee32e0a | 2006-01-10 19:45:49 +0000 | [diff] [blame] | 4238 | ** into the index P1. Data for the entry is nil. |
drh | 717e640 | 2001-09-27 03:22:32 +0000 | [diff] [blame] | 4239 | ** |
drh | aa9b896 | 2008-01-08 02:57:55 +0000 | [diff] [blame] | 4240 | ** 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] | 4241 | ** insert is likely to be an append. |
| 4242 | ** |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 4243 | ** This instruction only works for indices. The equivalent instruction |
| 4244 | ** for tables is OP_Insert. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4245 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4246 | case OP_IdxInsert: { /* in2 */ |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 4247 | VdbeCursor *pC; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4248 | BtCursor *pCrsr; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 4249 | int nKey; |
| 4250 | const char *zKey; |
| 4251 | |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 4252 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 4253 | pC = p->apCsr[pOp->p1]; |
| 4254 | assert( pC!=0 ); |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 4255 | pIn2 = &aMem[pOp->p2]; |
drh | aa9b896 | 2008-01-08 02:57:55 +0000 | [diff] [blame] | 4256 | assert( pIn2->flags & MEM_Blob ); |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 4257 | pCrsr = pC->pCursor; |
drh | 9a65f2c | 2009-06-22 19:05:40 +0000 | [diff] [blame] | 4258 | if( ALWAYS(pCrsr!=0) ){ |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 4259 | assert( pC->isTable==0 ); |
drh | aa9b896 | 2008-01-08 02:57:55 +0000 | [diff] [blame] | 4260 | rc = ExpandBlob(pIn2); |
danielk1977 | d908f5a | 2007-05-11 07:08:28 +0000 | [diff] [blame] | 4261 | if( rc==SQLITE_OK ){ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 4262 | nKey = pIn2->n; |
| 4263 | zKey = pIn2->z; |
danielk1977 | de63035 | 2009-05-04 11:42:29 +0000 | [diff] [blame] | 4264 | rc = sqlite3BtreeInsert(pCrsr, zKey, nKey, "", 0, 0, pOp->p3, |
| 4265 | ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0) |
| 4266 | ); |
danielk1977 | d908f5a | 2007-05-11 07:08:28 +0000 | [diff] [blame] | 4267 | assert( pC->deferredMoveto==0 ); |
| 4268 | pC->cacheStatus = CACHE_STALE; |
| 4269 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4270 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4271 | break; |
| 4272 | } |
| 4273 | |
drh | d1d3848 | 2008-10-07 23:46:38 +0000 | [diff] [blame] | 4274 | /* Opcode: IdxDelete P1 P2 P3 * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4275 | ** |
drh | e14006d | 2008-03-25 17:23:32 +0000 | [diff] [blame] | 4276 | ** The content of P3 registers starting at register P2 form |
| 4277 | ** an unpacked index key. This opcode removes that entry from the |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 4278 | ** index opened by cursor P1. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4279 | */ |
drh | e14006d | 2008-03-25 17:23:32 +0000 | [diff] [blame] | 4280 | case OP_IdxDelete: { |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 4281 | VdbeCursor *pC; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4282 | BtCursor *pCrsr; |
drh | 9a65f2c | 2009-06-22 19:05:40 +0000 | [diff] [blame] | 4283 | int res; |
| 4284 | UnpackedRecord r; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 4285 | |
drh | e14006d | 2008-03-25 17:23:32 +0000 | [diff] [blame] | 4286 | assert( pOp->p3>0 ); |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 4287 | assert( pOp->p2>0 && pOp->p2+pOp->p3<=p->nMem+1 ); |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 4288 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 4289 | pC = p->apCsr[pOp->p1]; |
| 4290 | assert( pC!=0 ); |
| 4291 | pCrsr = pC->pCursor; |
drh | 9a65f2c | 2009-06-22 19:05:40 +0000 | [diff] [blame] | 4292 | if( ALWAYS(pCrsr!=0) ){ |
drh | e14006d | 2008-03-25 17:23:32 +0000 | [diff] [blame] | 4293 | r.pKeyInfo = pC->pKeyInfo; |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 4294 | r.nField = (u16)pOp->p3; |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 4295 | r.flags = 0; |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 4296 | r.aMem = &aMem[pOp->p2]; |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 4297 | rc = sqlite3BtreeMovetoUnpacked(pCrsr, &r, 0, 0, &res); |
danielk1977 | 75bab7d | 2006-01-23 13:09:45 +0000 | [diff] [blame] | 4298 | if( rc==SQLITE_OK && res==0 ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 4299 | rc = sqlite3BtreeDelete(pCrsr); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4300 | } |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 4301 | assert( pC->deferredMoveto==0 ); |
drh | 76873ab | 2006-01-07 18:48:26 +0000 | [diff] [blame] | 4302 | pC->cacheStatus = CACHE_STALE; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4303 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4304 | break; |
| 4305 | } |
| 4306 | |
drh | 2133d82 | 2008-01-03 18:44:59 +0000 | [diff] [blame] | 4307 | /* Opcode: IdxRowid P1 P2 * * * |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 4308 | ** |
drh | 2133d82 | 2008-01-03 18:44:59 +0000 | [diff] [blame] | 4309 | ** 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] | 4310 | ** the end of the index key pointed to by cursor P1. This integer should be |
| 4311 | ** the rowid of the table entry to which this index entry points. |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 4312 | ** |
drh | 9437bd2 | 2009-02-01 00:29:56 +0000 | [diff] [blame] | 4313 | ** See also: Rowid, MakeRecord. |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 4314 | */ |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 4315 | case OP_IdxRowid: { /* out2-prerelease */ |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 4316 | BtCursor *pCrsr; |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 4317 | VdbeCursor *pC; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 4318 | i64 rowid; |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 4319 | |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 4320 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 4321 | pC = p->apCsr[pOp->p1]; |
| 4322 | assert( pC!=0 ); |
| 4323 | pCrsr = pC->pCursor; |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 4324 | pOut->flags = MEM_Null; |
drh | 9a65f2c | 2009-06-22 19:05:40 +0000 | [diff] [blame] | 4325 | if( ALWAYS(pCrsr!=0) ){ |
danielk1977 | c4d201c | 2009-04-07 09:16:56 +0000 | [diff] [blame] | 4326 | rc = sqlite3VdbeCursorMoveto(pC); |
drh | 9a65f2c | 2009-06-22 19:05:40 +0000 | [diff] [blame] | 4327 | if( NEVER(rc) ) goto abort_due_to_error; |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame] | 4328 | assert( pC->deferredMoveto==0 ); |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 4329 | assert( pC->isTable==0 ); |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 4330 | if( !pC->nullRow ){ |
drh | 35f6b93 | 2009-06-23 14:15:04 +0000 | [diff] [blame] | 4331 | rc = sqlite3VdbeIdxRowid(db, pCrsr, &rowid); |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 4332 | if( rc!=SQLITE_OK ){ |
| 4333 | goto abort_due_to_error; |
| 4334 | } |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 4335 | pOut->u.i = rowid; |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 4336 | pOut->flags = MEM_Int; |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 4337 | } |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 4338 | } |
| 4339 | break; |
| 4340 | } |
| 4341 | |
danielk1977 | 61dd583 | 2008-04-18 11:31:12 +0000 | [diff] [blame] | 4342 | /* Opcode: IdxGE P1 P2 P3 P4 P5 |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 4343 | ** |
danielk1977 | 61dd583 | 2008-04-18 11:31:12 +0000 | [diff] [blame] | 4344 | ** The P4 register values beginning with P3 form an unpacked index |
| 4345 | ** key that omits the ROWID. Compare this key value against the index |
| 4346 | ** that P1 is currently pointing to, ignoring the ROWID on the P1 index. |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 4347 | ** |
danielk1977 | 61dd583 | 2008-04-18 11:31:12 +0000 | [diff] [blame] | 4348 | ** If the P1 index entry is greater than or equal to the key value |
| 4349 | ** then jump to P2. Otherwise fall through to the next instruction. |
drh | 772ae62 | 2004-05-19 13:13:08 +0000 | [diff] [blame] | 4350 | ** |
danielk1977 | 61dd583 | 2008-04-18 11:31:12 +0000 | [diff] [blame] | 4351 | ** If P5 is non-zero then the key value is increased by an epsilon |
| 4352 | ** prior to the comparison. This make the opcode work like IdxGT except |
| 4353 | ** that if the key from register P3 is a prefix of the key in the cursor, |
| 4354 | ** the result is false whereas it would be true with IdxGT. |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 4355 | */ |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4356 | /* Opcode: IdxLT P1 P2 P3 * P5 |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 4357 | ** |
danielk1977 | 61dd583 | 2008-04-18 11:31:12 +0000 | [diff] [blame] | 4358 | ** The P4 register values beginning with P3 form an unpacked index |
| 4359 | ** key that omits the ROWID. Compare this key value against the index |
| 4360 | ** that P1 is currently pointing to, ignoring the ROWID on the P1 index. |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 4361 | ** |
danielk1977 | 61dd583 | 2008-04-18 11:31:12 +0000 | [diff] [blame] | 4362 | ** If the P1 index entry is less than the key value then jump to P2. |
| 4363 | ** Otherwise fall through to the next instruction. |
drh | 772ae62 | 2004-05-19 13:13:08 +0000 | [diff] [blame] | 4364 | ** |
danielk1977 | 61dd583 | 2008-04-18 11:31:12 +0000 | [diff] [blame] | 4365 | ** If P5 is non-zero then the key value is increased by an epsilon prior |
| 4366 | ** to the comparison. This makes the opcode work like IdxLE. |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 4367 | */ |
drh | 93952eb | 2009-11-13 19:43:43 +0000 | [diff] [blame] | 4368 | case OP_IdxLT: /* jump */ |
| 4369 | case OP_IdxGE: { /* jump */ |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 4370 | VdbeCursor *pC; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 4371 | int res; |
| 4372 | UnpackedRecord r; |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 4373 | |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 4374 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 4375 | pC = p->apCsr[pOp->p1]; |
| 4376 | assert( pC!=0 ); |
drh | 9a65f2c | 2009-06-22 19:05:40 +0000 | [diff] [blame] | 4377 | if( ALWAYS(pC->pCursor!=0) ){ |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame] | 4378 | assert( pC->deferredMoveto==0 ); |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 4379 | assert( pOp->p5==0 || pOp->p5==1 ); |
danielk1977 | 61dd583 | 2008-04-18 11:31:12 +0000 | [diff] [blame] | 4380 | assert( pOp->p4type==P4_INT32 ); |
| 4381 | r.pKeyInfo = pC->pKeyInfo; |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 4382 | r.nField = (u16)pOp->p4.i; |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 4383 | if( pOp->p5 ){ |
| 4384 | r.flags = UNPACKED_INCRKEY | UNPACKED_IGNORE_ROWID; |
| 4385 | }else{ |
| 4386 | r.flags = UNPACKED_IGNORE_ROWID; |
| 4387 | } |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 4388 | r.aMem = &aMem[pOp->p3]; |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 4389 | rc = sqlite3VdbeIdxKeyCompare(pC, &r, &res); |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 4390 | if( pOp->opcode==OP_IdxLT ){ |
| 4391 | res = -res; |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 4392 | }else{ |
| 4393 | assert( pOp->opcode==OP_IdxGE ); |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 4394 | res++; |
| 4395 | } |
| 4396 | if( res>0 ){ |
| 4397 | pc = pOp->p2 - 1 ; |
| 4398 | } |
| 4399 | } |
| 4400 | break; |
| 4401 | } |
| 4402 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4403 | /* Opcode: Destroy P1 P2 P3 * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4404 | ** |
| 4405 | ** Delete an entire database table or index whose root page in the database |
| 4406 | ** file is given by P1. |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 4407 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4408 | ** The table being destroyed is in the main database file if P3==0. If |
| 4409 | ** 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] | 4410 | ** that is used to store tables create using CREATE TEMPORARY TABLE. |
| 4411 | ** |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 4412 | ** If AUTOVACUUM is enabled then it is possible that another root page |
| 4413 | ** might be moved into the newly deleted root page in order to keep all |
| 4414 | ** root pages contiguous at the beginning of the database. The former |
| 4415 | ** value of the root page that moved - its value before the move occurred - |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4416 | ** is stored in register P2. If no page |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4417 | ** movement was required (because the table being dropped was already |
| 4418 | ** the last one in the database) then a zero is stored in register P2. |
| 4419 | ** If AUTOVACUUM is disabled then a zero is stored in register P2. |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 4420 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 4421 | ** See also: Clear |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4422 | */ |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4423 | case OP_Destroy: { /* out2-prerelease */ |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 4424 | int iMoved; |
drh | 3765df4 | 2006-06-28 18:18:09 +0000 | [diff] [blame] | 4425 | int iCnt; |
drh | 5a91a53 | 2007-01-05 16:39:43 +0000 | [diff] [blame] | 4426 | Vdbe *pVdbe; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 4427 | int iDb; |
| 4428 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
danielk1977 | 212b218 | 2006-06-23 14:32:08 +0000 | [diff] [blame] | 4429 | iCnt = 0; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 4430 | for(pVdbe=db->pVdbe; pVdbe; pVdbe = pVdbe->pNext){ |
danielk1977 | 212b218 | 2006-06-23 14:32:08 +0000 | [diff] [blame] | 4431 | if( pVdbe->magic==VDBE_MAGIC_RUN && pVdbe->inVtabMethod<2 && pVdbe->pc>=0 ){ |
| 4432 | iCnt++; |
| 4433 | } |
| 4434 | } |
drh | 3765df4 | 2006-06-28 18:18:09 +0000 | [diff] [blame] | 4435 | #else |
| 4436 | iCnt = db->activeVdbeCnt; |
danielk1977 | 212b218 | 2006-06-23 14:32:08 +0000 | [diff] [blame] | 4437 | #endif |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 4438 | pOut->flags = MEM_Null; |
danielk1977 | 212b218 | 2006-06-23 14:32:08 +0000 | [diff] [blame] | 4439 | if( iCnt>1 ){ |
danielk1977 | e6efa74 | 2004-11-10 11:55:10 +0000 | [diff] [blame] | 4440 | rc = SQLITE_LOCKED; |
drh | 77658e2 | 2007-12-04 16:54:52 +0000 | [diff] [blame] | 4441 | p->errorAction = OE_Abort; |
danielk1977 | e6efa74 | 2004-11-10 11:55:10 +0000 | [diff] [blame] | 4442 | }else{ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 4443 | iDb = pOp->p3; |
danielk1977 | 212b218 | 2006-06-23 14:32:08 +0000 | [diff] [blame] | 4444 | assert( iCnt==1 ); |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4445 | assert( (p->btreeMask & (1<<iDb))!=0 ); |
| 4446 | rc = sqlite3BtreeDropTable(db->aDb[iDb].pBt, pOp->p1, &iMoved); |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 4447 | pOut->flags = MEM_Int; |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4448 | pOut->u.i = iMoved; |
drh | 3765df4 | 2006-06-28 18:18:09 +0000 | [diff] [blame] | 4449 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | e6efa74 | 2004-11-10 11:55:10 +0000 | [diff] [blame] | 4450 | if( rc==SQLITE_OK && iMoved!=0 ){ |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4451 | sqlite3RootPageMoved(&db->aDb[iDb], iMoved, pOp->p1); |
drh | 3278315 | 2009-11-20 15:02:34 +0000 | [diff] [blame] | 4452 | resetSchemaOnFault = 1; |
danielk1977 | e6efa74 | 2004-11-10 11:55:10 +0000 | [diff] [blame] | 4453 | } |
drh | 3765df4 | 2006-06-28 18:18:09 +0000 | [diff] [blame] | 4454 | #endif |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 4455 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4456 | break; |
| 4457 | } |
| 4458 | |
danielk1977 | c7af484 | 2008-10-27 13:59:33 +0000 | [diff] [blame] | 4459 | /* Opcode: Clear P1 P2 P3 |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 4460 | ** |
| 4461 | ** Delete all contents of the database table or index whose root page |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 4462 | ** in the database file is given by P1. But, unlike Destroy, do not |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 4463 | ** remove the table or index from the database file. |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 4464 | ** |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 4465 | ** The table being clear is in the main database file if P2==0. If |
| 4466 | ** P2==1 then the table to be clear is in the auxiliary database file |
| 4467 | ** that is used to store tables create using CREATE TEMPORARY TABLE. |
| 4468 | ** |
shane | be21779 | 2009-03-05 04:20:31 +0000 | [diff] [blame] | 4469 | ** 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] | 4470 | ** intkey table (an SQL table, not an index). In this case the row change |
| 4471 | ** count is incremented by the number of rows in the table being cleared. |
| 4472 | ** If P3 is greater than zero, then the value stored in register P3 is |
| 4473 | ** also incremented by the number of rows in the table being cleared. |
| 4474 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 4475 | ** See also: Destroy |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 4476 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4477 | case OP_Clear: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 4478 | int nChange; |
| 4479 | |
| 4480 | nChange = 0; |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 4481 | assert( (p->btreeMask & (1<<pOp->p2))!=0 ); |
danielk1977 | c7af484 | 2008-10-27 13:59:33 +0000 | [diff] [blame] | 4482 | rc = sqlite3BtreeClearTable( |
| 4483 | db->aDb[pOp->p2].pBt, pOp->p1, (pOp->p3 ? &nChange : 0) |
| 4484 | ); |
| 4485 | if( pOp->p3 ){ |
| 4486 | p->nChange += nChange; |
| 4487 | if( pOp->p3>0 ){ |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 4488 | aMem[pOp->p3].u.i += nChange; |
danielk1977 | c7af484 | 2008-10-27 13:59:33 +0000 | [diff] [blame] | 4489 | } |
| 4490 | } |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 4491 | break; |
| 4492 | } |
| 4493 | |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 4494 | /* Opcode: CreateTable P1 P2 * * * |
drh | 5b2fd56 | 2001-09-13 15:21:31 +0000 | [diff] [blame] | 4495 | ** |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 4496 | ** Allocate a new table in the main database file if P1==0 or in the |
| 4497 | ** auxiliary database file if P1==1 or in an attached database if |
| 4498 | ** P1>1. Write the root page number of the new table into |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4499 | ** register P2 |
drh | 5b2fd56 | 2001-09-13 15:21:31 +0000 | [diff] [blame] | 4500 | ** |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 4501 | ** The difference between a table and an index is this: A table must |
| 4502 | ** have a 4-byte integer key and can have arbitrary data. An index |
| 4503 | ** has an arbitrary key but no data. |
| 4504 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 4505 | ** See also: CreateIndex |
drh | 5b2fd56 | 2001-09-13 15:21:31 +0000 | [diff] [blame] | 4506 | */ |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 4507 | /* Opcode: CreateIndex P1 P2 * * * |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 4508 | ** |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 4509 | ** Allocate a new index in the main database file if P1==0 or in the |
| 4510 | ** auxiliary database file if P1==1 or in an attached database if |
| 4511 | ** P1>1. Write the root page number of the new table into |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4512 | ** register P2. |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 4513 | ** |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 4514 | ** See documentation on OP_CreateTable for additional information. |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 4515 | */ |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 4516 | case OP_CreateIndex: /* out2-prerelease */ |
| 4517 | case OP_CreateTable: { /* out2-prerelease */ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 4518 | int pgno; |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 4519 | int flags; |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 4520 | Db *pDb; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 4521 | |
| 4522 | pgno = 0; |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 4523 | assert( pOp->p1>=0 && pOp->p1<db->nDb ); |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 4524 | assert( (p->btreeMask & (1<<pOp->p1))!=0 ); |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 4525 | pDb = &db->aDb[pOp->p1]; |
| 4526 | assert( pDb->pBt!=0 ); |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 4527 | if( pOp->opcode==OP_CreateTable ){ |
danielk1977 | 9407625 | 2004-05-14 12:16:11 +0000 | [diff] [blame] | 4528 | /* flags = BTREE_INTKEY; */ |
| 4529 | flags = BTREE_LEAFDATA|BTREE_INTKEY; |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 4530 | }else{ |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 4531 | flags = BTREE_ZERODATA; |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 4532 | } |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 4533 | rc = sqlite3BtreeCreateTable(pDb->pBt, &pgno, flags); |
drh | 88a003e | 2008-12-11 16:17:03 +0000 | [diff] [blame] | 4534 | pOut->u.i = pgno; |
drh | 5b2fd56 | 2001-09-13 15:21:31 +0000 | [diff] [blame] | 4535 | break; |
| 4536 | } |
| 4537 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4538 | /* Opcode: ParseSchema P1 P2 * P4 * |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 4539 | ** |
| 4540 | ** Read and parse all entries from the SQLITE_MASTER table of database P1 |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 4541 | ** that match the WHERE clause P4. P2 is the "force" flag. Always do |
drh | 3c23a88 | 2007-01-09 14:01:13 +0000 | [diff] [blame] | 4542 | ** the parsing if P2 is true. If P2 is false, then this routine is a |
| 4543 | ** no-op if the schema is not currently loaded. In other words, if P2 |
| 4544 | ** is false, the SQLITE_MASTER table is only parsed if the rest of the |
| 4545 | ** schema is already loaded into the symbol table. |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 4546 | ** |
| 4547 | ** This opcode invokes the parser to create a new virtual machine, |
shane | 21e7feb | 2008-05-30 15:59:49 +0000 | [diff] [blame] | 4548 | ** then runs the new virtual machine. It is thus a re-entrant opcode. |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 4549 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4550 | case OP_ParseSchema: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 4551 | int iDb; |
| 4552 | const char *zMaster; |
| 4553 | char *zSql; |
| 4554 | InitData initData; |
| 4555 | |
| 4556 | iDb = pOp->p1; |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 4557 | assert( iDb>=0 && iDb<db->nDb ); |
danielk1977 | a8bbef8 | 2009-03-23 17:11:26 +0000 | [diff] [blame] | 4558 | |
| 4559 | /* If pOp->p2 is 0, then this opcode is being executed to read a |
| 4560 | ** single row, for example the row corresponding to a new index |
| 4561 | ** created by this VDBE, from the sqlite_master table. It only |
| 4562 | ** does this if the corresponding in-memory schema is currently |
| 4563 | ** loaded. Otherwise, the new index definition can be loaded along |
| 4564 | ** with the rest of the schema when it is required. |
| 4565 | ** |
| 4566 | ** Although the mutex on the BtShared object that corresponds to |
| 4567 | ** database iDb (the database containing the sqlite_master table |
| 4568 | ** read by this instruction) is currently held, it is necessary to |
| 4569 | ** obtain the mutexes on all attached databases before checking if |
| 4570 | ** the schema of iDb is loaded. This is because, at the start of |
| 4571 | ** the sqlite3_exec() call below, SQLite will invoke |
| 4572 | ** sqlite3BtreeEnterAll(). If all mutexes are not already held, the |
| 4573 | ** iDb mutex may be temporarily released to avoid deadlock. If |
| 4574 | ** this happens, then some other thread may delete the in-memory |
| 4575 | ** schema of database iDb before the SQL statement runs. The schema |
| 4576 | ** will not be reloaded becuase the db->init.busy flag is set. This |
| 4577 | ** can result in a "no such table: sqlite_master" or "malformed |
| 4578 | ** database schema" error being returned to the user. |
| 4579 | */ |
| 4580 | assert( sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) ); |
| 4581 | sqlite3BtreeEnterAll(db); |
drh | 46bbabd | 2009-06-24 13:16:03 +0000 | [diff] [blame] | 4582 | if( pOp->p2 || DbHasProperty(db, iDb, DB_SchemaLoaded) ){ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 4583 | zMaster = SCHEMA_TABLE(iDb); |
danielk1977 | a8bbef8 | 2009-03-23 17:11:26 +0000 | [diff] [blame] | 4584 | initData.db = db; |
| 4585 | initData.iDb = pOp->p1; |
| 4586 | initData.pzErrMsg = &p->zErrMsg; |
| 4587 | zSql = sqlite3MPrintf(db, |
drh | 6a9c64b | 2010-01-12 23:54:14 +0000 | [diff] [blame] | 4588 | "SELECT name, rootpage, sql FROM '%q'.%s WHERE %s ORDER BY rowid", |
danielk1977 | a8bbef8 | 2009-03-23 17:11:26 +0000 | [diff] [blame] | 4589 | db->aDb[iDb].zName, zMaster, pOp->p4.z); |
| 4590 | if( zSql==0 ){ |
| 4591 | rc = SQLITE_NOMEM; |
| 4592 | }else{ |
| 4593 | (void)sqlite3SafetyOff(db); |
| 4594 | assert( db->init.busy==0 ); |
| 4595 | db->init.busy = 1; |
| 4596 | initData.rc = SQLITE_OK; |
| 4597 | assert( !db->mallocFailed ); |
| 4598 | rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0); |
| 4599 | if( rc==SQLITE_OK ) rc = initData.rc; |
| 4600 | sqlite3DbFree(db, zSql); |
| 4601 | db->init.busy = 0; |
| 4602 | (void)sqlite3SafetyOn(db); |
| 4603 | } |
drh | 3c23a88 | 2007-01-09 14:01:13 +0000 | [diff] [blame] | 4604 | } |
danielk1977 | a8bbef8 | 2009-03-23 17:11:26 +0000 | [diff] [blame] | 4605 | sqlite3BtreeLeaveAll(db); |
danielk1977 | 261919c | 2005-12-06 12:52:59 +0000 | [diff] [blame] | 4606 | if( rc==SQLITE_NOMEM ){ |
danielk1977 | 261919c | 2005-12-06 12:52:59 +0000 | [diff] [blame] | 4607 | goto no_mem; |
| 4608 | } |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 4609 | break; |
| 4610 | } |
| 4611 | |
drh | 8bfdf72 | 2009-06-19 14:06:03 +0000 | [diff] [blame] | 4612 | #if !defined(SQLITE_OMIT_ANALYZE) |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4613 | /* Opcode: LoadAnalysis P1 * * * * |
drh | 497e446 | 2005-07-23 03:18:40 +0000 | [diff] [blame] | 4614 | ** |
| 4615 | ** Read the sqlite_stat1 table for database P1 and load the content |
| 4616 | ** of that table into the internal index hash table. This will cause |
| 4617 | ** the analysis to be used when preparing all subsequent queries. |
| 4618 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4619 | case OP_LoadAnalysis: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 4620 | assert( pOp->p1>=0 && pOp->p1<db->nDb ); |
| 4621 | rc = sqlite3AnalysisLoad(db, pOp->p1); |
drh | 497e446 | 2005-07-23 03:18:40 +0000 | [diff] [blame] | 4622 | break; |
| 4623 | } |
drh | 8bfdf72 | 2009-06-19 14:06:03 +0000 | [diff] [blame] | 4624 | #endif /* !defined(SQLITE_OMIT_ANALYZE) */ |
drh | 497e446 | 2005-07-23 03:18:40 +0000 | [diff] [blame] | 4625 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4626 | /* Opcode: DropTable P1 * * P4 * |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 4627 | ** |
| 4628 | ** Remove the internal (in-memory) data structures that describe |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 4629 | ** the table named P4 in database P1. This is called after a table |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 4630 | ** is dropped in order to keep the internal representation of the |
| 4631 | ** schema consistent with what is on disk. |
| 4632 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4633 | case OP_DropTable: { |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 4634 | sqlite3UnlinkAndDeleteTable(db, pOp->p1, pOp->p4.z); |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 4635 | break; |
| 4636 | } |
| 4637 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4638 | /* Opcode: DropIndex P1 * * P4 * |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 4639 | ** |
| 4640 | ** Remove the internal (in-memory) data structures that describe |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 4641 | ** the index named P4 in database P1. This is called after an index |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 4642 | ** is dropped in order to keep the internal representation of the |
| 4643 | ** schema consistent with what is on disk. |
| 4644 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4645 | case OP_DropIndex: { |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 4646 | sqlite3UnlinkAndDeleteIndex(db, pOp->p1, pOp->p4.z); |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 4647 | break; |
| 4648 | } |
| 4649 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4650 | /* Opcode: DropTrigger P1 * * P4 * |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 4651 | ** |
| 4652 | ** Remove the internal (in-memory) data structures that describe |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 4653 | ** the trigger named P4 in database P1. This is called after a trigger |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 4654 | ** is dropped in order to keep the internal representation of the |
| 4655 | ** schema consistent with what is on disk. |
| 4656 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4657 | case OP_DropTrigger: { |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 4658 | sqlite3UnlinkAndDeleteTrigger(db, pOp->p1, pOp->p4.z); |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 4659 | break; |
| 4660 | } |
| 4661 | |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 4662 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 4663 | #ifndef SQLITE_OMIT_INTEGRITY_CHECK |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4664 | /* Opcode: IntegrityCk P1 P2 P3 * P5 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4665 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4666 | ** Do an analysis of the currently open database. Store in |
| 4667 | ** register P1 the text of an error message describing any problems. |
| 4668 | ** If no problems are found, store a NULL in register P1. |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 4669 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4670 | ** The register P3 contains the maximum number of allowed errors. |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 4671 | ** At most reg(P3) errors will be reported. |
| 4672 | ** In other words, the analysis stops as soon as reg(P1) errors are |
| 4673 | ** seen. Reg(P1) is updated with the number of errors remaining. |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 4674 | ** |
drh | 7906975 | 2004-05-22 21:30:40 +0000 | [diff] [blame] | 4675 | ** The root page numbers of all tables in the database are integer |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 4676 | ** stored in reg(P1), reg(P1+1), reg(P1+2), .... There are P2 tables |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4677 | ** total. |
drh | 2150432 | 2002-06-25 13:16:02 +0000 | [diff] [blame] | 4678 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4679 | ** If P5 is not zero, the check is done on the auxiliary database |
drh | 2150432 | 2002-06-25 13:16:02 +0000 | [diff] [blame] | 4680 | ** file, not the main database file. |
drh | 1dd397f | 2002-02-03 03:34:07 +0000 | [diff] [blame] | 4681 | ** |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 4682 | ** This opcode is used to implement the integrity_check pragma. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4683 | */ |
drh | aaab572 | 2002-02-19 13:39:21 +0000 | [diff] [blame] | 4684 | case OP_IntegrityCk: { |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4685 | int nRoot; /* Number of tables to check. (Number of root pages.) */ |
| 4686 | int *aRoot; /* Array of rootpage numbers for tables to be checked */ |
| 4687 | int j; /* Loop counter */ |
| 4688 | int nErr; /* Number of errors reported */ |
| 4689 | char *z; /* Text of the error report */ |
| 4690 | Mem *pnErr; /* Register keeping track of errors remaining */ |
| 4691 | |
| 4692 | nRoot = pOp->p2; |
drh | 7906975 | 2004-05-22 21:30:40 +0000 | [diff] [blame] | 4693 | assert( nRoot>0 ); |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 4694 | aRoot = sqlite3DbMallocRaw(db, sizeof(int)*(nRoot+1) ); |
drh | caec2f1 | 2003-01-07 02:47:47 +0000 | [diff] [blame] | 4695 | if( aRoot==0 ) goto no_mem; |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4696 | assert( pOp->p3>0 && pOp->p3<=p->nMem ); |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 4697 | pnErr = &aMem[pOp->p3]; |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 4698 | assert( (pnErr->flags & MEM_Int)!=0 ); |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4699 | assert( (pnErr->flags & (MEM_Str|MEM_Blob))==0 ); |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 4700 | pIn1 = &aMem[pOp->p1]; |
drh | 7906975 | 2004-05-22 21:30:40 +0000 | [diff] [blame] | 4701 | for(j=0; j<nRoot; j++){ |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 4702 | aRoot[j] = (int)sqlite3VdbeIntValue(&pIn1[j]); |
drh | 1dd397f | 2002-02-03 03:34:07 +0000 | [diff] [blame] | 4703 | } |
| 4704 | aRoot[j] = 0; |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4705 | assert( pOp->p5<db->nDb ); |
| 4706 | assert( (p->btreeMask & (1<<pOp->p5))!=0 ); |
| 4707 | z = sqlite3BtreeIntegrityCheck(db->aDb[pOp->p5].pBt, aRoot, nRoot, |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 4708 | (int)pnErr->u.i, &nErr); |
drh | c890fec | 2008-08-01 20:10:08 +0000 | [diff] [blame] | 4709 | sqlite3DbFree(db, aRoot); |
drh | 3c024d6 | 2007-03-30 11:23:45 +0000 | [diff] [blame] | 4710 | pnErr->u.i -= nErr; |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 4711 | sqlite3VdbeMemSetNull(pIn1); |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 4712 | if( nErr==0 ){ |
| 4713 | assert( z==0 ); |
drh | c890fec | 2008-08-01 20:10:08 +0000 | [diff] [blame] | 4714 | }else if( z==0 ){ |
| 4715 | goto no_mem; |
drh | 1dd397f | 2002-02-03 03:34:07 +0000 | [diff] [blame] | 4716 | }else{ |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 4717 | sqlite3VdbeMemSetStr(pIn1, z, -1, SQLITE_UTF8, sqlite3_free); |
danielk1977 | 8a6b541 | 2004-05-24 07:04:25 +0000 | [diff] [blame] | 4718 | } |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 4719 | UPDATE_MAX_BLOBSIZE(pIn1); |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4720 | sqlite3VdbeChangeEncoding(pIn1, encoding); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4721 | break; |
| 4722 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 4723 | #endif /* SQLITE_OMIT_INTEGRITY_CHECK */ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4724 | |
drh | 3d4501e | 2008-12-04 20:40:10 +0000 | [diff] [blame] | 4725 | /* Opcode: RowSetAdd P1 P2 * * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4726 | ** |
drh | 3d4501e | 2008-12-04 20:40:10 +0000 | [diff] [blame] | 4727 | ** Insert the integer value held by register P2 into a boolean index |
| 4728 | ** held in register P1. |
| 4729 | ** |
| 4730 | ** An assertion fails if P2 is not an integer. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4731 | */ |
drh | 93952eb | 2009-11-13 19:43:43 +0000 | [diff] [blame] | 4732 | case OP_RowSetAdd: { /* in1, in2 */ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 4733 | pIn1 = &aMem[pOp->p1]; |
| 4734 | pIn2 = &aMem[pOp->p2]; |
drh | 93952eb | 2009-11-13 19:43:43 +0000 | [diff] [blame] | 4735 | assert( (pIn2->flags & MEM_Int)!=0 ); |
| 4736 | if( (pIn1->flags & MEM_RowSet)==0 ){ |
| 4737 | sqlite3VdbeMemSetRowSet(pIn1); |
| 4738 | if( (pIn1->flags & MEM_RowSet)==0 ) goto no_mem; |
drh | 3d4501e | 2008-12-04 20:40:10 +0000 | [diff] [blame] | 4739 | } |
drh | 93952eb | 2009-11-13 19:43:43 +0000 | [diff] [blame] | 4740 | sqlite3RowSetInsert(pIn1->u.pRowSet, pIn2->u.i); |
drh | 3d4501e | 2008-12-04 20:40:10 +0000 | [diff] [blame] | 4741 | break; |
| 4742 | } |
| 4743 | |
| 4744 | /* Opcode: RowSetRead P1 P2 P3 * * |
| 4745 | ** |
| 4746 | ** Extract the smallest value from boolean index P1 and put that value into |
| 4747 | ** register P3. Or, if boolean index P1 is initially empty, leave P3 |
| 4748 | ** unchanged and jump to instruction P2. |
| 4749 | */ |
drh | 93952eb | 2009-11-13 19:43:43 +0000 | [diff] [blame] | 4750 | case OP_RowSetRead: { /* jump, in1, out3 */ |
drh | 3d4501e | 2008-12-04 20:40:10 +0000 | [diff] [blame] | 4751 | i64 val; |
drh | 3d4501e | 2008-12-04 20:40:10 +0000 | [diff] [blame] | 4752 | CHECK_FOR_INTERRUPT; |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 4753 | pIn1 = &aMem[pOp->p1]; |
drh | 93952eb | 2009-11-13 19:43:43 +0000 | [diff] [blame] | 4754 | if( (pIn1->flags & MEM_RowSet)==0 |
| 4755 | || sqlite3RowSetNext(pIn1->u.pRowSet, &val)==0 |
drh | 3d4501e | 2008-12-04 20:40:10 +0000 | [diff] [blame] | 4756 | ){ |
| 4757 | /* The boolean index is empty */ |
drh | 93952eb | 2009-11-13 19:43:43 +0000 | [diff] [blame] | 4758 | sqlite3VdbeMemSetNull(pIn1); |
drh | 3d4501e | 2008-12-04 20:40:10 +0000 | [diff] [blame] | 4759 | pc = pOp->p2 - 1; |
| 4760 | }else{ |
| 4761 | /* A value was pulled from the index */ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 4762 | sqlite3VdbeMemSetInt64(&aMem[pOp->p3], val); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 4763 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4764 | break; |
| 4765 | } |
| 4766 | |
drh | 1b26c7c | 2009-04-22 02:15:47 +0000 | [diff] [blame] | 4767 | /* Opcode: RowSetTest P1 P2 P3 P4 |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 4768 | ** |
drh | ade9760 | 2009-04-21 15:05:18 +0000 | [diff] [blame] | 4769 | ** 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] | 4770 | ** contains a RowSet object and that RowSet object contains |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 4771 | ** the value held in P3, jump to register P2. Otherwise, insert the |
drh | 1b26c7c | 2009-04-22 02:15:47 +0000 | [diff] [blame] | 4772 | ** integer in P3 into the RowSet and continue on to the |
drh | ade9760 | 2009-04-21 15:05:18 +0000 | [diff] [blame] | 4773 | ** next opcode. |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 4774 | ** |
drh | 1b26c7c | 2009-04-22 02:15:47 +0000 | [diff] [blame] | 4775 | ** The RowSet object is optimized for the case where successive sets |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 4776 | ** of integers, where each set contains no duplicates. Each set |
| 4777 | ** of values is identified by a unique P4 value. The first set |
drh | 1b26c7c | 2009-04-22 02:15:47 +0000 | [diff] [blame] | 4778 | ** must have P4==0, the final set P4=-1. P4 must be either -1 or |
| 4779 | ** non-negative. For non-negative values of P4 only the lower 4 |
| 4780 | ** bits are significant. |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 4781 | ** |
| 4782 | ** This allows optimizations: (a) when P4==0 there is no need to test |
drh | 1b26c7c | 2009-04-22 02:15:47 +0000 | [diff] [blame] | 4783 | ** the rowset object for P3, as it is guaranteed not to contain it, |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 4784 | ** (b) when P4==-1 there is no need to insert the value, as it will |
| 4785 | ** never be tested for, and (c) when a value that is part of set X is |
| 4786 | ** inserted, there is no need to search to see if the same value was |
| 4787 | ** previously inserted as part of set X (only if it was previously |
| 4788 | ** inserted as part of some other set). |
| 4789 | */ |
drh | 1b26c7c | 2009-04-22 02:15:47 +0000 | [diff] [blame] | 4790 | case OP_RowSetTest: { /* jump, in1, in3 */ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 4791 | int iSet; |
| 4792 | int exists; |
| 4793 | |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 4794 | pIn1 = &aMem[pOp->p1]; |
| 4795 | pIn3 = &aMem[pOp->p3]; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 4796 | iSet = pOp->p4.i; |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 4797 | assert( pIn3->flags&MEM_Int ); |
| 4798 | |
drh | 1b26c7c | 2009-04-22 02:15:47 +0000 | [diff] [blame] | 4799 | /* If there is anything other than a rowset object in memory cell P1, |
| 4800 | ** delete it now and initialize P1 with an empty rowset |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 4801 | */ |
drh | 733bf1b | 2009-04-22 00:47:00 +0000 | [diff] [blame] | 4802 | if( (pIn1->flags & MEM_RowSet)==0 ){ |
| 4803 | sqlite3VdbeMemSetRowSet(pIn1); |
| 4804 | if( (pIn1->flags & MEM_RowSet)==0 ) goto no_mem; |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 4805 | } |
| 4806 | |
| 4807 | assert( pOp->p4type==P4_INT32 ); |
drh | 1b26c7c | 2009-04-22 02:15:47 +0000 | [diff] [blame] | 4808 | assert( iSet==-1 || iSet>=0 ); |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 4809 | if( iSet ){ |
shane | 60a4b53 | 2009-05-06 18:57:09 +0000 | [diff] [blame] | 4810 | exists = sqlite3RowSetTest(pIn1->u.pRowSet, |
| 4811 | (u8)(iSet>=0 ? iSet & 0xf : 0xff), |
drh | 733bf1b | 2009-04-22 00:47:00 +0000 | [diff] [blame] | 4812 | pIn3->u.i); |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 4813 | if( exists ){ |
| 4814 | pc = pOp->p2 - 1; |
| 4815 | break; |
| 4816 | } |
| 4817 | } |
| 4818 | if( iSet>=0 ){ |
drh | 733bf1b | 2009-04-22 00:47:00 +0000 | [diff] [blame] | 4819 | sqlite3RowSetInsert(pIn1->u.pRowSet, pIn3->u.i); |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 4820 | } |
| 4821 | break; |
| 4822 | } |
| 4823 | |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4824 | |
danielk1977 | 93758c8 | 2005-01-21 08:13:14 +0000 | [diff] [blame] | 4825 | #ifndef SQLITE_OMIT_TRIGGER |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 4826 | |
| 4827 | /* Opcode: Program P1 P2 P3 P4 * |
| 4828 | ** |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 4829 | ** Execute the trigger program passed as P4 (type P4_SUBPROGRAM). |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 4830 | ** |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 4831 | ** P1 contains the address of the memory cell that contains the first memory |
| 4832 | ** cell in an array of values used as arguments to the sub-program. P2 |
| 4833 | ** contains the address to jump to if the sub-program throws an IGNORE |
| 4834 | ** exception using the RAISE() function. Register P3 contains the address |
| 4835 | ** of a memory cell in this (the parent) VM that is used to allocate the |
| 4836 | ** memory required by the sub-vdbe at runtime. |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 4837 | ** |
| 4838 | ** P4 is a pointer to the VM containing the trigger program. |
| 4839 | */ |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 4840 | case OP_Program: { /* jump */ |
dan | 65a7cd1 | 2009-09-01 12:16:01 +0000 | [diff] [blame] | 4841 | int nMem; /* Number of memory registers for sub-program */ |
| 4842 | int nByte; /* Bytes of runtime space required for sub-program */ |
| 4843 | Mem *pRt; /* Register to allocate runtime space */ |
| 4844 | Mem *pMem; /* Used to iterate through memory cells */ |
| 4845 | Mem *pEnd; /* Last memory cell in new array */ |
| 4846 | VdbeFrame *pFrame; /* New vdbe frame to execute in */ |
| 4847 | SubProgram *pProgram; /* Sub-program to execute */ |
| 4848 | void *t; /* Token identifying trigger */ |
| 4849 | |
| 4850 | pProgram = pOp->p4.pProgram; |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 4851 | pRt = &aMem[pOp->p3]; |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 4852 | assert( pProgram->nOp>0 ); |
| 4853 | |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 4854 | /* If the p5 flag is clear, then recursive invocation of triggers is |
| 4855 | ** disabled for backwards compatibility (p5 is set if this sub-program |
| 4856 | ** is really a trigger, not a foreign key action, and the flag set |
| 4857 | ** and cleared by the "PRAGMA recursive_triggers" command is clear). |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 4858 | ** |
| 4859 | ** It is recursive invocation of triggers, at the SQL level, that is |
| 4860 | ** disabled. In some cases a single trigger may generate more than one |
| 4861 | ** SubProgram (if the trigger may be executed with more than one different |
| 4862 | ** ON CONFLICT algorithm). SubProgram structures associated with a |
| 4863 | ** single trigger all have the same value for the SubProgram.token |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 4864 | ** variable. */ |
| 4865 | if( pOp->p5 ){ |
dan | 65a7cd1 | 2009-09-01 12:16:01 +0000 | [diff] [blame] | 4866 | t = pProgram->token; |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 4867 | for(pFrame=p->pFrame; pFrame && pFrame->token!=t; pFrame=pFrame->pParent); |
| 4868 | if( pFrame ) break; |
| 4869 | } |
| 4870 | |
dan | f589450 | 2009-10-07 18:41:19 +0000 | [diff] [blame] | 4871 | if( p->nFrame>=db->aLimit[SQLITE_LIMIT_TRIGGER_DEPTH] ){ |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 4872 | rc = SQLITE_ERROR; |
| 4873 | sqlite3SetString(&p->zErrMsg, db, "too many levels of trigger recursion"); |
| 4874 | break; |
| 4875 | } |
| 4876 | |
| 4877 | /* Register pRt is used to store the memory required to save the state |
| 4878 | ** of the current program, and the memory required at runtime to execute |
| 4879 | ** the trigger program. If this trigger has been fired before, then pRt |
| 4880 | ** is already allocated. Otherwise, it must be initialized. */ |
| 4881 | if( (pRt->flags&MEM_Frame)==0 ){ |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 4882 | /* SubProgram.nMem is set to the number of memory cells used by the |
| 4883 | ** program stored in SubProgram.aOp. As well as these, one memory |
| 4884 | ** cell is required for each cursor used by the program. Set local |
| 4885 | ** variable nMem (and later, VdbeFrame.nChildMem) to this value. |
| 4886 | */ |
dan | 65a7cd1 | 2009-09-01 12:16:01 +0000 | [diff] [blame] | 4887 | nMem = pProgram->nMem + pProgram->nCsr; |
| 4888 | nByte = ROUND8(sizeof(VdbeFrame)) |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 4889 | + nMem * sizeof(Mem) |
| 4890 | + pProgram->nCsr * sizeof(VdbeCursor *); |
| 4891 | pFrame = sqlite3DbMallocZero(db, nByte); |
| 4892 | if( !pFrame ){ |
| 4893 | goto no_mem; |
| 4894 | } |
| 4895 | sqlite3VdbeMemRelease(pRt); |
| 4896 | pRt->flags = MEM_Frame; |
| 4897 | pRt->u.pFrame = pFrame; |
| 4898 | |
| 4899 | pFrame->v = p; |
| 4900 | pFrame->nChildMem = nMem; |
| 4901 | pFrame->nChildCsr = pProgram->nCsr; |
| 4902 | pFrame->pc = pc; |
| 4903 | pFrame->aMem = p->aMem; |
| 4904 | pFrame->nMem = p->nMem; |
| 4905 | pFrame->apCsr = p->apCsr; |
| 4906 | pFrame->nCursor = p->nCursor; |
| 4907 | pFrame->aOp = p->aOp; |
| 4908 | pFrame->nOp = p->nOp; |
| 4909 | pFrame->token = pProgram->token; |
| 4910 | |
| 4911 | pEnd = &VdbeFrameMem(pFrame)[pFrame->nChildMem]; |
| 4912 | for(pMem=VdbeFrameMem(pFrame); pMem!=pEnd; pMem++){ |
| 4913 | pMem->flags = MEM_Null; |
| 4914 | pMem->db = db; |
| 4915 | } |
| 4916 | }else{ |
| 4917 | pFrame = pRt->u.pFrame; |
| 4918 | assert( pProgram->nMem+pProgram->nCsr==pFrame->nChildMem ); |
| 4919 | assert( pProgram->nCsr==pFrame->nChildCsr ); |
| 4920 | assert( pc==pFrame->pc ); |
| 4921 | } |
| 4922 | |
| 4923 | p->nFrame++; |
| 4924 | pFrame->pParent = p->pFrame; |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 4925 | pFrame->lastRowid = db->lastRowid; |
| 4926 | pFrame->nChange = p->nChange; |
dan | 2832ad4 | 2009-08-31 15:27:27 +0000 | [diff] [blame] | 4927 | p->nChange = 0; |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 4928 | p->pFrame = pFrame; |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 4929 | p->aMem = aMem = &VdbeFrameMem(pFrame)[-1]; |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 4930 | p->nMem = pFrame->nChildMem; |
shane | cea72b2 | 2009-09-07 04:38:36 +0000 | [diff] [blame] | 4931 | p->nCursor = (u16)pFrame->nChildCsr; |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 4932 | p->apCsr = (VdbeCursor **)&aMem[p->nMem+1]; |
drh | bbe879d | 2009-11-14 18:04:35 +0000 | [diff] [blame] | 4933 | p->aOp = aOp = pProgram->aOp; |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 4934 | p->nOp = pProgram->nOp; |
| 4935 | pc = -1; |
| 4936 | |
| 4937 | break; |
| 4938 | } |
| 4939 | |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 4940 | /* Opcode: Param P1 P2 * * * |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 4941 | ** |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 4942 | ** This opcode is only ever present in sub-programs called via the |
| 4943 | ** OP_Program instruction. Copy a value currently stored in a memory |
| 4944 | ** cell of the calling (parent) frame to cell P2 in the current frames |
| 4945 | ** address space. This is used by trigger programs to access the new.* |
| 4946 | ** and old.* values. |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 4947 | ** |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 4948 | ** The address of the cell in the parent frame is determined by adding |
| 4949 | ** the value of the P1 argument to the value of the P1 argument to the |
| 4950 | ** calling OP_Program instruction. |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 4951 | */ |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 4952 | case OP_Param: { /* out2-prerelease */ |
dan | 65a7cd1 | 2009-09-01 12:16:01 +0000 | [diff] [blame] | 4953 | VdbeFrame *pFrame; |
| 4954 | Mem *pIn; |
| 4955 | pFrame = p->pFrame; |
| 4956 | pIn = &pFrame->aMem[pOp->p1 + pFrame->aOp[pFrame->pc].p1]; |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 4957 | sqlite3VdbeMemShallowCopy(pOut, pIn, MEM_Ephem); |
| 4958 | break; |
| 4959 | } |
| 4960 | |
danielk1977 | 93758c8 | 2005-01-21 08:13:14 +0000 | [diff] [blame] | 4961 | #endif /* #ifndef SQLITE_OMIT_TRIGGER */ |
rdc | b0c374f | 2004-02-20 22:53:38 +0000 | [diff] [blame] | 4962 | |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 4963 | #ifndef SQLITE_OMIT_FOREIGN_KEY |
dan | 32b09f2 | 2009-09-23 17:29:59 +0000 | [diff] [blame] | 4964 | /* Opcode: FkCounter P1 P2 * * * |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 4965 | ** |
dan | 0ff297e | 2009-09-25 17:03:14 +0000 | [diff] [blame] | 4966 | ** Increment a "constraint counter" by P2 (P2 may be negative or positive). |
| 4967 | ** If P1 is non-zero, the database constraint counter is incremented |
| 4968 | ** (deferred foreign key constraints). Otherwise, if P1 is zero, the |
dan | 32b09f2 | 2009-09-23 17:29:59 +0000 | [diff] [blame] | 4969 | ** statement counter is incremented (immediate foreign key constraints). |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 4970 | */ |
dan | 32b09f2 | 2009-09-23 17:29:59 +0000 | [diff] [blame] | 4971 | case OP_FkCounter: { |
dan | 0ff297e | 2009-09-25 17:03:14 +0000 | [diff] [blame] | 4972 | if( pOp->p1 ){ |
| 4973 | db->nDeferredCons += pOp->p2; |
dan | 32b09f2 | 2009-09-23 17:29:59 +0000 | [diff] [blame] | 4974 | }else{ |
dan | 0ff297e | 2009-09-25 17:03:14 +0000 | [diff] [blame] | 4975 | p->nFkConstraint += pOp->p2; |
| 4976 | } |
| 4977 | break; |
| 4978 | } |
| 4979 | |
| 4980 | /* Opcode: FkIfZero P1 P2 * * * |
| 4981 | ** |
| 4982 | ** This opcode tests if a foreign key constraint-counter is currently zero. |
| 4983 | ** If so, jump to instruction P2. Otherwise, fall through to the next |
| 4984 | ** instruction. |
| 4985 | ** |
| 4986 | ** If P1 is non-zero, then the jump is taken if the database constraint-counter |
| 4987 | ** is zero (the one that counts deferred constraint violations). If P1 is |
| 4988 | ** zero, the jump is taken if the statement constraint-counter is zero |
| 4989 | ** (immediate foreign key constraint violations). |
| 4990 | */ |
| 4991 | case OP_FkIfZero: { /* jump */ |
| 4992 | if( pOp->p1 ){ |
| 4993 | if( db->nDeferredCons==0 ) pc = pOp->p2-1; |
| 4994 | }else{ |
| 4995 | if( p->nFkConstraint==0 ) pc = pOp->p2-1; |
dan | 32b09f2 | 2009-09-23 17:29:59 +0000 | [diff] [blame] | 4996 | } |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 4997 | break; |
| 4998 | } |
| 4999 | #endif /* #ifndef SQLITE_OMIT_FOREIGN_KEY */ |
| 5000 | |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 5001 | #ifndef SQLITE_OMIT_AUTOINCREMENT |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 5002 | /* Opcode: MemMax P1 P2 * * * |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 5003 | ** |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 5004 | ** P1 is a register in the root frame of this VM (the root frame is |
| 5005 | ** different from the current frame if this instruction is being executed |
| 5006 | ** within a sub-program). Set the value of register P1 to the maximum of |
| 5007 | ** its current value and the value in register P2. |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 5008 | ** |
| 5009 | ** This instruction throws an error if the memory cell is not initially |
| 5010 | ** an integer. |
| 5011 | */ |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 5012 | case OP_MemMax: { /* in2 */ |
| 5013 | Mem *pIn1; |
| 5014 | VdbeFrame *pFrame; |
| 5015 | if( p->pFrame ){ |
| 5016 | for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent); |
| 5017 | pIn1 = &pFrame->aMem[pOp->p1]; |
| 5018 | }else{ |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 5019 | pIn1 = &aMem[pOp->p1]; |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 5020 | } |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 5021 | sqlite3VdbeMemIntegerify(pIn1); |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 5022 | pIn2 = &aMem[pOp->p2]; |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 5023 | sqlite3VdbeMemIntegerify(pIn2); |
| 5024 | if( pIn1->u.i<pIn2->u.i){ |
| 5025 | pIn1->u.i = pIn2->u.i; |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 5026 | } |
| 5027 | break; |
| 5028 | } |
| 5029 | #endif /* SQLITE_OMIT_AUTOINCREMENT */ |
| 5030 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 5031 | /* Opcode: IfPos P1 P2 * * * |
danielk1977 | a2dc3b1 | 2005-02-05 12:48:48 +0000 | [diff] [blame] | 5032 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 5033 | ** If the value of register P1 is 1 or greater, jump to P2. |
drh | 6f58f70 | 2006-01-08 05:26:41 +0000 | [diff] [blame] | 5034 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 5035 | ** It is illegal to use this instruction on a register that does |
drh | 6f58f70 | 2006-01-08 05:26:41 +0000 | [diff] [blame] | 5036 | ** not contain an integer. An assertion fault will result if you try. |
danielk1977 | a2dc3b1 | 2005-02-05 12:48:48 +0000 | [diff] [blame] | 5037 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 5038 | case OP_IfPos: { /* jump, in1 */ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 5039 | pIn1 = &aMem[pOp->p1]; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 5040 | assert( pIn1->flags&MEM_Int ); |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 5041 | if( pIn1->u.i>0 ){ |
drh | ec7429a | 2005-10-06 16:53:14 +0000 | [diff] [blame] | 5042 | pc = pOp->p2 - 1; |
| 5043 | } |
| 5044 | break; |
| 5045 | } |
| 5046 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 5047 | /* Opcode: IfNeg P1 P2 * * * |
drh | 15007a9 | 2006-01-08 18:10:17 +0000 | [diff] [blame] | 5048 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 5049 | ** If the value of register P1 is less than zero, jump to P2. |
drh | 15007a9 | 2006-01-08 18:10:17 +0000 | [diff] [blame] | 5050 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 5051 | ** It is illegal to use this instruction on a register that does |
drh | 15007a9 | 2006-01-08 18:10:17 +0000 | [diff] [blame] | 5052 | ** not contain an integer. An assertion fault will result if you try. |
| 5053 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 5054 | case OP_IfNeg: { /* jump, in1 */ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 5055 | pIn1 = &aMem[pOp->p1]; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 5056 | assert( pIn1->flags&MEM_Int ); |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 5057 | if( pIn1->u.i<0 ){ |
drh | 15007a9 | 2006-01-08 18:10:17 +0000 | [diff] [blame] | 5058 | pc = pOp->p2 - 1; |
| 5059 | } |
| 5060 | break; |
| 5061 | } |
| 5062 | |
drh | 9b918ed | 2009-11-12 03:13:26 +0000 | [diff] [blame] | 5063 | /* Opcode: IfZero P1 P2 P3 * * |
drh | ec7429a | 2005-10-06 16:53:14 +0000 | [diff] [blame] | 5064 | ** |
drh | 9b918ed | 2009-11-12 03:13:26 +0000 | [diff] [blame] | 5065 | ** The register P1 must contain an integer. Add literal P3 to the |
| 5066 | ** value in register P1. If the result is exactly 0, jump to P2. |
drh | 6f58f70 | 2006-01-08 05:26:41 +0000 | [diff] [blame] | 5067 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 5068 | ** It is illegal to use this instruction on a register that does |
drh | 6f58f70 | 2006-01-08 05:26:41 +0000 | [diff] [blame] | 5069 | ** not contain an integer. An assertion fault will result if you try. |
drh | ec7429a | 2005-10-06 16:53:14 +0000 | [diff] [blame] | 5070 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 5071 | case OP_IfZero: { /* jump, in1 */ |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 5072 | pIn1 = &aMem[pOp->p1]; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 5073 | assert( pIn1->flags&MEM_Int ); |
drh | 9b918ed | 2009-11-12 03:13:26 +0000 | [diff] [blame] | 5074 | pIn1->u.i += pOp->p3; |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 5075 | if( pIn1->u.i==0 ){ |
drh | a2a49dc | 2008-01-02 14:28:13 +0000 | [diff] [blame] | 5076 | pc = pOp->p2 - 1; |
| 5077 | } |
| 5078 | break; |
| 5079 | } |
| 5080 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 5081 | /* Opcode: AggStep * P2 P3 P4 P5 |
drh | e509535 | 2002-02-24 03:25:14 +0000 | [diff] [blame] | 5082 | ** |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 5083 | ** Execute the step function for an aggregate. The |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 5084 | ** function has P5 arguments. P4 is a pointer to the FuncDef |
| 5085 | ** structure that specifies the function. Use register |
| 5086 | ** P3 as the accumulator. |
drh | e509535 | 2002-02-24 03:25:14 +0000 | [diff] [blame] | 5087 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 5088 | ** The P5 arguments are taken from register P2 and its |
| 5089 | ** successors. |
drh | e509535 | 2002-02-24 03:25:14 +0000 | [diff] [blame] | 5090 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 5091 | case OP_AggStep: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 5092 | int n; |
drh | e509535 | 2002-02-24 03:25:14 +0000 | [diff] [blame] | 5093 | int i; |
drh | c54a617 | 2009-06-02 16:06:03 +0000 | [diff] [blame] | 5094 | Mem *pMem; |
| 5095 | Mem *pRec; |
danielk1977 | 22322fd | 2004-05-25 23:35:17 +0000 | [diff] [blame] | 5096 | sqlite3_context ctx; |
danielk1977 | 6ddcca5 | 2004-05-24 23:48:25 +0000 | [diff] [blame] | 5097 | sqlite3_value **apVal; |
drh | e509535 | 2002-02-24 03:25:14 +0000 | [diff] [blame] | 5098 | |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 5099 | n = pOp->p5; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 5100 | assert( n>=0 ); |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 5101 | pRec = &aMem[pOp->p2]; |
danielk1977 | 6ddcca5 | 2004-05-24 23:48:25 +0000 | [diff] [blame] | 5102 | apVal = p->apArg; |
| 5103 | assert( apVal || n==0 ); |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 5104 | for(i=0; i<n; i++, pRec++){ |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 5105 | apVal[i] = pRec; |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 5106 | sqlite3VdbeMemStoreType(pRec); |
drh | e509535 | 2002-02-24 03:25:14 +0000 | [diff] [blame] | 5107 | } |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 5108 | ctx.pFunc = pOp->p4.pFunc; |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 5109 | assert( pOp->p3>0 && pOp->p3<=p->nMem ); |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 5110 | ctx.pMem = pMem = &aMem[pOp->p3]; |
drh | abfcea2 | 2005-09-06 20:36:48 +0000 | [diff] [blame] | 5111 | pMem->n++; |
drh | 90669c1 | 2006-01-20 15:45:36 +0000 | [diff] [blame] | 5112 | ctx.s.flags = MEM_Null; |
| 5113 | ctx.s.z = 0; |
danielk1977 | 5f09613 | 2008-03-28 15:44:09 +0000 | [diff] [blame] | 5114 | ctx.s.zMalloc = 0; |
drh | 90669c1 | 2006-01-20 15:45:36 +0000 | [diff] [blame] | 5115 | ctx.s.xDel = 0; |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 5116 | ctx.s.db = db; |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 5117 | ctx.isError = 0; |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 5118 | ctx.pColl = 0; |
drh | e82f5d0 | 2008-10-07 19:53:14 +0000 | [diff] [blame] | 5119 | if( ctx.pFunc->flags & SQLITE_FUNC_NEEDCOLL ){ |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 5120 | assert( pOp>p->aOp ); |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 5121 | assert( pOp[-1].p4type==P4_COLLSEQ ); |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 5122 | assert( pOp[-1].opcode==OP_CollSeq ); |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 5123 | ctx.pColl = pOp[-1].p4.pColl; |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 5124 | } |
danielk1977 | 6ddcca5 | 2004-05-24 23:48:25 +0000 | [diff] [blame] | 5125 | (ctx.pFunc->xStep)(&ctx, n, apVal); |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 5126 | if( ctx.isError ){ |
drh | f089aa4 | 2008-07-08 19:34:06 +0000 | [diff] [blame] | 5127 | sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(&ctx.s)); |
drh | 69544ec | 2008-02-06 14:11:34 +0000 | [diff] [blame] | 5128 | rc = ctx.isError; |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 5129 | } |
drh | 90669c1 | 2006-01-20 15:45:36 +0000 | [diff] [blame] | 5130 | sqlite3VdbeMemRelease(&ctx.s); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5131 | break; |
| 5132 | } |
| 5133 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 5134 | /* Opcode: AggFinal P1 P2 * P4 * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5135 | ** |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 5136 | ** Execute the finalizer function for an aggregate. P1 is |
| 5137 | ** the memory location that is the accumulator for the aggregate. |
drh | a10a34b | 2005-09-07 22:09:48 +0000 | [diff] [blame] | 5138 | ** |
| 5139 | ** P2 is the number of arguments that the step function takes and |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 5140 | ** P4 is a pointer to the FuncDef for this function. The P2 |
drh | a10a34b | 2005-09-07 22:09:48 +0000 | [diff] [blame] | 5141 | ** argument is not used by this opcode. It is only there to disambiguate |
| 5142 | ** functions that can take varying numbers of arguments. The |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 5143 | ** P4 argument is only needed for the degenerate case where |
drh | a10a34b | 2005-09-07 22:09:48 +0000 | [diff] [blame] | 5144 | ** the step function was not previously called. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5145 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 5146 | case OP_AggFinal: { |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 5147 | Mem *pMem; |
drh | 0a07c10 | 2008-01-03 18:03:08 +0000 | [diff] [blame] | 5148 | assert( pOp->p1>0 && pOp->p1<=p->nMem ); |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 5149 | pMem = &aMem[pOp->p1]; |
drh | a10a34b | 2005-09-07 22:09:48 +0000 | [diff] [blame] | 5150 | assert( (pMem->flags & ~(MEM_Null|MEM_Agg))==0 ); |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 5151 | rc = sqlite3VdbeMemFinalize(pMem, pOp->p4.pFunc); |
drh | 4c8555f | 2009-06-25 01:47:11 +0000 | [diff] [blame] | 5152 | if( rc ){ |
drh | f089aa4 | 2008-07-08 19:34:06 +0000 | [diff] [blame] | 5153 | sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(pMem)); |
drh | 90669c1 | 2006-01-20 15:45:36 +0000 | [diff] [blame] | 5154 | } |
drh | 2dca868 | 2008-03-21 17:13:13 +0000 | [diff] [blame] | 5155 | sqlite3VdbeChangeEncoding(pMem, encoding); |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 5156 | UPDATE_MAX_BLOBSIZE(pMem); |
drh | 023ae03 | 2007-05-08 12:12:16 +0000 | [diff] [blame] | 5157 | if( sqlite3VdbeMemTooBig(pMem) ){ |
| 5158 | goto too_big; |
| 5159 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5160 | break; |
| 5161 | } |
| 5162 | |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5163 | |
drh | fdbcdee | 2007-03-27 14:44:50 +0000 | [diff] [blame] | 5164 | #if !defined(SQLITE_OMIT_VACUUM) && !defined(SQLITE_OMIT_ATTACH) |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 5165 | /* Opcode: Vacuum * * * * * |
drh | 6f8c91c | 2003-12-07 00:24:35 +0000 | [diff] [blame] | 5166 | ** |
| 5167 | ** Vacuum the entire database. This opcode will cause other virtual |
| 5168 | ** machines to be created and run. It may not be called from within |
| 5169 | ** a transaction. |
| 5170 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 5171 | case OP_Vacuum: { |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 5172 | if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse; |
| 5173 | rc = sqlite3RunVacuum(&p->zErrMsg, db); |
| 5174 | if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse; |
drh | 6f8c91c | 2003-12-07 00:24:35 +0000 | [diff] [blame] | 5175 | break; |
| 5176 | } |
drh | 154d4b2 | 2006-09-21 11:02:16 +0000 | [diff] [blame] | 5177 | #endif |
drh | 6f8c91c | 2003-12-07 00:24:35 +0000 | [diff] [blame] | 5178 | |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 5179 | #if !defined(SQLITE_OMIT_AUTOVACUUM) |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 5180 | /* Opcode: IncrVacuum P1 P2 * * * |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 5181 | ** |
| 5182 | ** Perform a single step of the incremental vacuum procedure on |
drh | ca5557f | 2007-05-04 18:30:40 +0000 | [diff] [blame] | 5183 | ** the P1 database. If the vacuum has finished, jump to instruction |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 5184 | ** P2. Otherwise, fall through to the next instruction. |
| 5185 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 5186 | case OP_IncrVacuum: { /* jump */ |
drh | ca5557f | 2007-05-04 18:30:40 +0000 | [diff] [blame] | 5187 | Btree *pBt; |
| 5188 | |
| 5189 | assert( pOp->p1>=0 && pOp->p1<db->nDb ); |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 5190 | assert( (p->btreeMask & (1<<pOp->p1))!=0 ); |
drh | ca5557f | 2007-05-04 18:30:40 +0000 | [diff] [blame] | 5191 | pBt = db->aDb[pOp->p1].pBt; |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 5192 | rc = sqlite3BtreeIncrVacuum(pBt); |
| 5193 | if( rc==SQLITE_DONE ){ |
| 5194 | pc = pOp->p2 - 1; |
| 5195 | rc = SQLITE_OK; |
| 5196 | } |
| 5197 | break; |
| 5198 | } |
| 5199 | #endif |
| 5200 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 5201 | /* Opcode: Expire P1 * * * * |
danielk1977 | a21c6b6 | 2005-01-24 10:25:59 +0000 | [diff] [blame] | 5202 | ** |
| 5203 | ** Cause precompiled statements to become expired. An expired statement |
| 5204 | ** fails with an error code of SQLITE_SCHEMA if it is ever executed |
| 5205 | ** (via sqlite3_step()). |
| 5206 | ** |
| 5207 | ** If P1 is 0, then all SQL statements become expired. If P1 is non-zero, |
| 5208 | ** then only the currently executing statement is affected. |
| 5209 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 5210 | case OP_Expire: { |
danielk1977 | a21c6b6 | 2005-01-24 10:25:59 +0000 | [diff] [blame] | 5211 | if( !pOp->p1 ){ |
| 5212 | sqlite3ExpirePreparedStatements(db); |
| 5213 | }else{ |
| 5214 | p->expired = 1; |
| 5215 | } |
| 5216 | break; |
| 5217 | } |
| 5218 | |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 5219 | #ifndef SQLITE_OMIT_SHARED_CACHE |
drh | 6a9ad3d | 2008-04-02 16:29:30 +0000 | [diff] [blame] | 5220 | /* Opcode: TableLock P1 P2 P3 P4 * |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 5221 | ** |
| 5222 | ** Obtain a lock on a particular table. This instruction is only used when |
| 5223 | ** the shared-cache feature is enabled. |
| 5224 | ** |
danielk1977 | 96d48e9 | 2009-06-29 06:00:37 +0000 | [diff] [blame] | 5225 | ** P1 is the index of the database in sqlite3.aDb[] of the database |
drh | 6a9ad3d | 2008-04-02 16:29:30 +0000 | [diff] [blame] | 5226 | ** on which the lock is acquired. A readlock is obtained if P3==0 or |
| 5227 | ** a write lock if P3==1. |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 5228 | ** |
| 5229 | ** P2 contains the root-page of the table to lock. |
| 5230 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 5231 | ** 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] | 5232 | ** used to generate an error message if the lock cannot be obtained. |
| 5233 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 5234 | case OP_TableLock: { |
danielk1977 | e0d9e6f | 2009-07-03 16:25:06 +0000 | [diff] [blame] | 5235 | u8 isWriteLock = (u8)pOp->p3; |
| 5236 | if( isWriteLock || 0==(db->flags&SQLITE_ReadUncommitted) ){ |
| 5237 | int p1 = pOp->p1; |
| 5238 | assert( p1>=0 && p1<db->nDb ); |
| 5239 | assert( (p->btreeMask & (1<<p1))!=0 ); |
| 5240 | assert( isWriteLock==0 || isWriteLock==1 ); |
| 5241 | rc = sqlite3BtreeLockTable(db->aDb[p1].pBt, pOp->p2, isWriteLock); |
| 5242 | if( (rc&0xFF)==SQLITE_LOCKED ){ |
| 5243 | const char *z = pOp->p4.z; |
| 5244 | sqlite3SetString(&p->zErrMsg, db, "database table is locked: %s", z); |
| 5245 | } |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 5246 | } |
| 5247 | break; |
| 5248 | } |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 5249 | #endif /* SQLITE_OMIT_SHARED_CACHE */ |
| 5250 | |
| 5251 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 5252 | /* Opcode: VBegin * * * P4 * |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 5253 | ** |
danielk1977 | 3e3a84d | 2008-08-01 17:37:40 +0000 | [diff] [blame] | 5254 | ** P4 may be a pointer to an sqlite3_vtab structure. If so, call the |
| 5255 | ** xBegin method for that table. |
| 5256 | ** |
| 5257 | ** 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] | 5258 | ** within a callback to a virtual table xSync() method. If it is, the error |
| 5259 | ** code will be set to SQLITE_LOCKED. |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 5260 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 5261 | case OP_VBegin: { |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 5262 | VTable *pVTab; |
| 5263 | pVTab = pOp->p4.pVtab; |
| 5264 | rc = sqlite3VtabBegin(db, pVTab); |
| 5265 | if( pVTab ){ |
danielk1977 | 3e3a84d | 2008-08-01 17:37:40 +0000 | [diff] [blame] | 5266 | sqlite3DbFree(db, p->zErrMsg); |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 5267 | p->zErrMsg = pVTab->pVtab->zErrMsg; |
| 5268 | pVTab->pVtab->zErrMsg = 0; |
danielk1977 | 3e3a84d | 2008-08-01 17:37:40 +0000 | [diff] [blame] | 5269 | } |
danielk1977 | f9e7dda | 2006-06-16 16:08:53 +0000 | [diff] [blame] | 5270 | break; |
| 5271 | } |
| 5272 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 5273 | |
| 5274 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 5275 | /* Opcode: VCreate P1 * * P4 * |
danielk1977 | f9e7dda | 2006-06-16 16:08:53 +0000 | [diff] [blame] | 5276 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 5277 | ** P4 is the name of a virtual table in database P1. Call the xCreate method |
danielk1977 | f9e7dda | 2006-06-16 16:08:53 +0000 | [diff] [blame] | 5278 | ** for that table. |
| 5279 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 5280 | case OP_VCreate: { |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 5281 | rc = sqlite3VtabCallCreate(db, pOp->p1, pOp->p4.z, &p->zErrMsg); |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 5282 | break; |
| 5283 | } |
| 5284 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 5285 | |
| 5286 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 5287 | /* Opcode: VDestroy P1 * * P4 * |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 5288 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 5289 | ** 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] | 5290 | ** of that table. |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 5291 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 5292 | case OP_VDestroy: { |
danielk1977 | 212b218 | 2006-06-23 14:32:08 +0000 | [diff] [blame] | 5293 | p->inVtabMethod = 2; |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 5294 | rc = sqlite3VtabCallDestroy(db, pOp->p1, pOp->p4.z); |
danielk1977 | 212b218 | 2006-06-23 14:32:08 +0000 | [diff] [blame] | 5295 | p->inVtabMethod = 0; |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 5296 | break; |
| 5297 | } |
| 5298 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 5299 | |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 5300 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 5301 | /* Opcode: VOpen P1 * * P4 * |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 5302 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 5303 | ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure. |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 5304 | ** P1 is a cursor number. This opcode opens a cursor to the virtual |
| 5305 | ** table and stores that cursor in P1. |
| 5306 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 5307 | case OP_VOpen: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 5308 | VdbeCursor *pCur; |
| 5309 | sqlite3_vtab_cursor *pVtabCursor; |
| 5310 | sqlite3_vtab *pVtab; |
| 5311 | sqlite3_module *pModule; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 5312 | |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 5313 | pCur = 0; |
| 5314 | pVtabCursor = 0; |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 5315 | pVtab = pOp->p4.pVtab->pVtab; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 5316 | pModule = (sqlite3_module *)pVtab->pModule; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 5317 | assert(pVtab && pModule); |
| 5318 | if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse; |
| 5319 | rc = pModule->xOpen(pVtab, &pVtabCursor); |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 5320 | sqlite3DbFree(db, p->zErrMsg); |
drh | 80cc85b | 2008-07-23 21:07:25 +0000 | [diff] [blame] | 5321 | p->zErrMsg = pVtab->zErrMsg; |
| 5322 | pVtab->zErrMsg = 0; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 5323 | if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse; |
| 5324 | if( SQLITE_OK==rc ){ |
shane | 21e7feb | 2008-05-30 15:59:49 +0000 | [diff] [blame] | 5325 | /* Initialize sqlite3_vtab_cursor base class */ |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 5326 | pVtabCursor->pVtab = pVtab; |
| 5327 | |
| 5328 | /* Initialise vdbe cursor object */ |
danielk1977 | d336e22 | 2009-02-20 10:58:41 +0000 | [diff] [blame] | 5329 | pCur = allocateCursor(p, pOp->p1, 0, -1, 0); |
danielk1977 | be71889 | 2006-06-23 08:05:19 +0000 | [diff] [blame] | 5330 | if( pCur ){ |
| 5331 | pCur->pVtabCursor = pVtabCursor; |
| 5332 | pCur->pModule = pVtabCursor->pVtab->pModule; |
danielk1977 | b7a2f2e | 2006-06-23 11:34:54 +0000 | [diff] [blame] | 5333 | }else{ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 5334 | db->mallocFailed = 1; |
danielk1977 | b7a2f2e | 2006-06-23 11:34:54 +0000 | [diff] [blame] | 5335 | pModule->xClose(pVtabCursor); |
danielk1977 | be71889 | 2006-06-23 08:05:19 +0000 | [diff] [blame] | 5336 | } |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 5337 | } |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 5338 | break; |
| 5339 | } |
| 5340 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 5341 | |
| 5342 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
danielk1977 | 6dbee81 | 2008-01-03 18:39:41 +0000 | [diff] [blame] | 5343 | /* Opcode: VFilter P1 P2 P3 P4 * |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 5344 | ** |
| 5345 | ** P1 is a cursor opened using VOpen. P2 is an address to jump to if |
| 5346 | ** the filtered result set is empty. |
| 5347 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 5348 | ** P4 is either NULL or a string that was generated by the xBestIndex |
| 5349 | ** method of the module. The interpretation of the P4 string is left |
drh | 4be8b51 | 2006-06-13 23:51:34 +0000 | [diff] [blame] | 5350 | ** to the module implementation. |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 5351 | ** |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 5352 | ** This opcode invokes the xFilter method on the virtual table specified |
danielk1977 | 6dbee81 | 2008-01-03 18:39:41 +0000 | [diff] [blame] | 5353 | ** by P1. The integer query plan parameter to xFilter is stored in register |
| 5354 | ** P3. Register P3+1 stores the argc parameter to be passed to the |
drh | 174edc6 | 2008-05-29 05:23:41 +0000 | [diff] [blame] | 5355 | ** xFilter method. Registers P3+2..P3+1+argc are the argc |
| 5356 | ** additional parameters which are passed to |
danielk1977 | 6dbee81 | 2008-01-03 18:39:41 +0000 | [diff] [blame] | 5357 | ** xFilter as argv. Register P3+2 becomes argv[0] when passed to xFilter. |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 5358 | ** |
danielk1977 | 6dbee81 | 2008-01-03 18:39:41 +0000 | [diff] [blame] | 5359 | ** 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] | 5360 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 5361 | case OP_VFilter: { /* jump */ |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 5362 | int nArg; |
danielk1977 | 6dbee81 | 2008-01-03 18:39:41 +0000 | [diff] [blame] | 5363 | int iQuery; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 5364 | const sqlite3_module *pModule; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 5365 | Mem *pQuery; |
| 5366 | Mem *pArgc; |
drh | 4dc754d | 2008-07-23 18:17:32 +0000 | [diff] [blame] | 5367 | sqlite3_vtab_cursor *pVtabCursor; |
| 5368 | sqlite3_vtab *pVtab; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 5369 | VdbeCursor *pCur; |
| 5370 | int res; |
| 5371 | int i; |
| 5372 | Mem **apArg; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 5373 | |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 5374 | pQuery = &aMem[pOp->p3]; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 5375 | pArgc = &pQuery[1]; |
| 5376 | pCur = p->apCsr[pOp->p1]; |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 5377 | REGISTER_TRACE(pOp->p3, pQuery); |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 5378 | assert( pCur->pVtabCursor ); |
drh | 4dc754d | 2008-07-23 18:17:32 +0000 | [diff] [blame] | 5379 | pVtabCursor = pCur->pVtabCursor; |
| 5380 | pVtab = pVtabCursor->pVtab; |
| 5381 | pModule = pVtab->pModule; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 5382 | |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 5383 | /* Grab the index number and argc parameters */ |
danielk1977 | 6dbee81 | 2008-01-03 18:39:41 +0000 | [diff] [blame] | 5384 | assert( (pQuery->flags&MEM_Int)!=0 && pArgc->flags==MEM_Int ); |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 5385 | nArg = (int)pArgc->u.i; |
| 5386 | iQuery = (int)pQuery->u.i; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 5387 | |
drh | 644a529 | 2006-12-20 14:53:38 +0000 | [diff] [blame] | 5388 | /* Invoke the xFilter method */ |
| 5389 | { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 5390 | res = 0; |
| 5391 | apArg = p->apArg; |
drh | 4be8b51 | 2006-06-13 23:51:34 +0000 | [diff] [blame] | 5392 | for(i = 0; i<nArg; i++){ |
danielk1977 | 6dbee81 | 2008-01-03 18:39:41 +0000 | [diff] [blame] | 5393 | apArg[i] = &pArgc[i+1]; |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 5394 | sqlite3VdbeMemStoreType(apArg[i]); |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 5395 | } |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 5396 | |
| 5397 | if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse; |
danielk1977 | be71889 | 2006-06-23 08:05:19 +0000 | [diff] [blame] | 5398 | p->inVtabMethod = 1; |
drh | 4dc754d | 2008-07-23 18:17:32 +0000 | [diff] [blame] | 5399 | rc = pModule->xFilter(pVtabCursor, iQuery, pOp->p4.z, nArg, apArg); |
danielk1977 | be71889 | 2006-06-23 08:05:19 +0000 | [diff] [blame] | 5400 | p->inVtabMethod = 0; |
danielk1977 | 3e3a84d | 2008-08-01 17:37:40 +0000 | [diff] [blame] | 5401 | sqlite3DbFree(db, p->zErrMsg); |
| 5402 | p->zErrMsg = pVtab->zErrMsg; |
| 5403 | pVtab->zErrMsg = 0; |
danielk1977 | a298e90 | 2006-06-22 09:53:48 +0000 | [diff] [blame] | 5404 | if( rc==SQLITE_OK ){ |
drh | 4dc754d | 2008-07-23 18:17:32 +0000 | [diff] [blame] | 5405 | res = pModule->xEof(pVtabCursor); |
danielk1977 | a298e90 | 2006-06-22 09:53:48 +0000 | [diff] [blame] | 5406 | } |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 5407 | if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse; |
| 5408 | |
danielk1977 | a298e90 | 2006-06-22 09:53:48 +0000 | [diff] [blame] | 5409 | if( res ){ |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 5410 | pc = pOp->p2 - 1; |
| 5411 | } |
| 5412 | } |
drh | 1d454a3 | 2008-01-31 19:34:51 +0000 | [diff] [blame] | 5413 | pCur->nullRow = 0; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 5414 | |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 5415 | break; |
| 5416 | } |
| 5417 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 5418 | |
| 5419 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 5420 | /* Opcode: VColumn P1 P2 P3 * * |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 5421 | ** |
drh | 2133d82 | 2008-01-03 18:44:59 +0000 | [diff] [blame] | 5422 | ** Store the value of the P2-th column of |
| 5423 | ** the row of the virtual-table that the |
| 5424 | ** P1 cursor is pointing to into register P3. |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 5425 | */ |
| 5426 | case OP_VColumn: { |
danielk1977 | 3e3a84d | 2008-08-01 17:37:40 +0000 | [diff] [blame] | 5427 | sqlite3_vtab *pVtab; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 5428 | const sqlite3_module *pModule; |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 5429 | Mem *pDest; |
| 5430 | sqlite3_context sContext; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 5431 | |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 5432 | VdbeCursor *pCur = p->apCsr[pOp->p1]; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 5433 | assert( pCur->pVtabCursor ); |
drh | 2945b4a | 2008-01-31 15:53:45 +0000 | [diff] [blame] | 5434 | assert( pOp->p3>0 && pOp->p3<=p->nMem ); |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 5435 | pDest = &aMem[pOp->p3]; |
drh | 2945b4a | 2008-01-31 15:53:45 +0000 | [diff] [blame] | 5436 | if( pCur->nullRow ){ |
| 5437 | sqlite3VdbeMemSetNull(pDest); |
| 5438 | break; |
| 5439 | } |
danielk1977 | 3e3a84d | 2008-08-01 17:37:40 +0000 | [diff] [blame] | 5440 | pVtab = pCur->pVtabCursor->pVtab; |
| 5441 | pModule = pVtab->pModule; |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 5442 | assert( pModule->xColumn ); |
| 5443 | memset(&sContext, 0, sizeof(sContext)); |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 5444 | |
| 5445 | /* The output cell may already have a buffer allocated. Move |
| 5446 | ** the current contents to sContext.s so in case the user-function |
| 5447 | ** can use the already allocated buffer instead of allocating a |
| 5448 | ** new one. |
| 5449 | */ |
| 5450 | sqlite3VdbeMemMove(&sContext.s, pDest); |
| 5451 | MemSetTypeFlag(&sContext.s, MEM_Null); |
| 5452 | |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 5453 | if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse; |
| 5454 | rc = pModule->xColumn(pCur->pVtabCursor, &sContext, pOp->p2); |
danielk1977 | 3e3a84d | 2008-08-01 17:37:40 +0000 | [diff] [blame] | 5455 | sqlite3DbFree(db, p->zErrMsg); |
| 5456 | p->zErrMsg = pVtab->zErrMsg; |
| 5457 | pVtab->zErrMsg = 0; |
drh | 4c8555f | 2009-06-25 01:47:11 +0000 | [diff] [blame] | 5458 | if( sContext.isError ){ |
| 5459 | rc = sContext.isError; |
| 5460 | } |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 5461 | |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 5462 | /* Copy the result of the function to the P3 register. We |
shane | be21779 | 2009-03-05 04:20:31 +0000 | [diff] [blame] | 5463 | ** do this regardless of whether or not an error occurred to ensure any |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 5464 | ** dynamic allocation in sContext.s (a Mem struct) is released. |
| 5465 | */ |
| 5466 | sqlite3VdbeChangeEncoding(&sContext.s, encoding); |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 5467 | sqlite3VdbeMemMove(pDest, &sContext.s); |
drh | 5ff4437 | 2009-11-24 16:26:17 +0000 | [diff] [blame] | 5468 | REGISTER_TRACE(pOp->p3, pDest); |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 5469 | UPDATE_MAX_BLOBSIZE(pDest); |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 5470 | |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 5471 | if( sqlite3SafetyOn(db) ){ |
| 5472 | goto abort_due_to_misuse; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 5473 | } |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 5474 | if( sqlite3VdbeMemTooBig(pDest) ){ |
| 5475 | goto too_big; |
| 5476 | } |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 5477 | break; |
| 5478 | } |
| 5479 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 5480 | |
| 5481 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 5482 | /* Opcode: VNext P1 P2 * * * |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 5483 | ** |
| 5484 | ** Advance virtual table P1 to the next row in its result set and |
| 5485 | ** jump to instruction P2. Or, if the virtual table has reached |
| 5486 | ** the end of its result set, then fall through to the next instruction. |
| 5487 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 5488 | case OP_VNext: { /* jump */ |
danielk1977 | 3e3a84d | 2008-08-01 17:37:40 +0000 | [diff] [blame] | 5489 | sqlite3_vtab *pVtab; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 5490 | const sqlite3_module *pModule; |
drh | c54a617 | 2009-06-02 16:06:03 +0000 | [diff] [blame] | 5491 | int res; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 5492 | VdbeCursor *pCur; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 5493 | |
drh | c54a617 | 2009-06-02 16:06:03 +0000 | [diff] [blame] | 5494 | res = 0; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 5495 | pCur = p->apCsr[pOp->p1]; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 5496 | assert( pCur->pVtabCursor ); |
drh | 2945b4a | 2008-01-31 15:53:45 +0000 | [diff] [blame] | 5497 | if( pCur->nullRow ){ |
| 5498 | break; |
| 5499 | } |
danielk1977 | 3e3a84d | 2008-08-01 17:37:40 +0000 | [diff] [blame] | 5500 | pVtab = pCur->pVtabCursor->pVtab; |
| 5501 | pModule = pVtab->pModule; |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 5502 | assert( pModule->xNext ); |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 5503 | |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 5504 | /* Invoke the xNext() method of the module. There is no way for the |
| 5505 | ** underlying implementation to return an error if one occurs during |
| 5506 | ** xNext(). Instead, if an error occurs, true is returned (indicating that |
| 5507 | ** data is available) and the error code returned when xColumn or |
| 5508 | ** some other method is next invoked on the save virtual table cursor. |
| 5509 | */ |
| 5510 | if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse; |
| 5511 | p->inVtabMethod = 1; |
| 5512 | rc = pModule->xNext(pCur->pVtabCursor); |
| 5513 | p->inVtabMethod = 0; |
danielk1977 | 3e3a84d | 2008-08-01 17:37:40 +0000 | [diff] [blame] | 5514 | sqlite3DbFree(db, p->zErrMsg); |
| 5515 | p->zErrMsg = pVtab->zErrMsg; |
| 5516 | pVtab->zErrMsg = 0; |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 5517 | if( rc==SQLITE_OK ){ |
| 5518 | res = pModule->xEof(pCur->pVtabCursor); |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 5519 | } |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 5520 | if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 5521 | |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 5522 | if( !res ){ |
| 5523 | /* If there is data, jump to P2 */ |
| 5524 | pc = pOp->p2 - 1; |
| 5525 | } |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 5526 | break; |
| 5527 | } |
| 5528 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 5529 | |
danielk1977 | 182c4ba | 2007-06-27 15:53:34 +0000 | [diff] [blame] | 5530 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 5531 | /* Opcode: VRename P1 * * P4 * |
danielk1977 | 182c4ba | 2007-06-27 15:53:34 +0000 | [diff] [blame] | 5532 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 5533 | ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure. |
danielk1977 | 182c4ba | 2007-06-27 15:53:34 +0000 | [diff] [blame] | 5534 | ** This opcode invokes the corresponding xRename method. The value |
danielk1977 | 6dbee81 | 2008-01-03 18:39:41 +0000 | [diff] [blame] | 5535 | ** in register P1 is passed as the zName argument to the xRename method. |
danielk1977 | 182c4ba | 2007-06-27 15:53:34 +0000 | [diff] [blame] | 5536 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 5537 | case OP_VRename: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 5538 | sqlite3_vtab *pVtab; |
| 5539 | Mem *pName; |
| 5540 | |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 5541 | pVtab = pOp->p4.pVtab->pVtab; |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 5542 | pName = &aMem[pOp->p1]; |
danielk1977 | 182c4ba | 2007-06-27 15:53:34 +0000 | [diff] [blame] | 5543 | assert( pVtab->pModule->xRename ); |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 5544 | REGISTER_TRACE(pOp->p1, pName); |
drh | 35f6b93 | 2009-06-23 14:15:04 +0000 | [diff] [blame] | 5545 | assert( pName->flags & MEM_Str ); |
danielk1977 | 182c4ba | 2007-06-27 15:53:34 +0000 | [diff] [blame] | 5546 | if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse; |
danielk1977 | 6dbee81 | 2008-01-03 18:39:41 +0000 | [diff] [blame] | 5547 | rc = pVtab->pModule->xRename(pVtab, pName->z); |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 5548 | sqlite3DbFree(db, p->zErrMsg); |
drh | 80cc85b | 2008-07-23 21:07:25 +0000 | [diff] [blame] | 5549 | p->zErrMsg = pVtab->zErrMsg; |
| 5550 | pVtab->zErrMsg = 0; |
danielk1977 | 182c4ba | 2007-06-27 15:53:34 +0000 | [diff] [blame] | 5551 | if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse; |
| 5552 | |
danielk1977 | 182c4ba | 2007-06-27 15:53:34 +0000 | [diff] [blame] | 5553 | break; |
| 5554 | } |
| 5555 | #endif |
drh | 4cbdda9 | 2006-06-14 19:00:20 +0000 | [diff] [blame] | 5556 | |
| 5557 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 5558 | /* Opcode: VUpdate P1 P2 P3 P4 * |
danielk1977 | 399918f | 2006-06-14 13:03:23 +0000 | [diff] [blame] | 5559 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 5560 | ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure. |
danielk1977 | 399918f | 2006-06-14 13:03:23 +0000 | [diff] [blame] | 5561 | ** This opcode invokes the corresponding xUpdate method. P2 values |
danielk1977 | 2a339ff | 2008-01-03 17:31:44 +0000 | [diff] [blame] | 5562 | ** are contiguous memory cells starting at P3 to pass to the xUpdate |
| 5563 | ** invocation. The value in register (P3+P2-1) corresponds to the |
| 5564 | ** p2th element of the argv array passed to xUpdate. |
drh | 4cbdda9 | 2006-06-14 19:00:20 +0000 | [diff] [blame] | 5565 | ** |
| 5566 | ** The xUpdate method will do a DELETE or an INSERT or both. |
danielk1977 | 2a339ff | 2008-01-03 17:31:44 +0000 | [diff] [blame] | 5567 | ** The argv[0] element (which corresponds to memory cell P3) |
| 5568 | ** is the rowid of a row to delete. If argv[0] is NULL then no |
| 5569 | ** deletion occurs. The argv[1] element is the rowid of the new |
| 5570 | ** row. This can be NULL to have the virtual table select the new |
| 5571 | ** rowid for itself. The subsequent elements in the array are |
| 5572 | ** the values of columns in the new row. |
drh | 4cbdda9 | 2006-06-14 19:00:20 +0000 | [diff] [blame] | 5573 | ** |
| 5574 | ** If P2==1 then no insert is performed. argv[0] is the rowid of |
| 5575 | ** a row to delete. |
danielk1977 | 1f6eec5 | 2006-06-16 06:17:47 +0000 | [diff] [blame] | 5576 | ** |
| 5577 | ** P1 is a boolean flag. If it is set to true and the xUpdate call |
| 5578 | ** is successful, then the value returned by sqlite3_last_insert_rowid() |
| 5579 | ** is set to the value of the rowid for the row just inserted. |
danielk1977 | 399918f | 2006-06-14 13:03:23 +0000 | [diff] [blame] | 5580 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 5581 | case OP_VUpdate: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 5582 | sqlite3_vtab *pVtab; |
| 5583 | sqlite3_module *pModule; |
| 5584 | int nArg; |
| 5585 | int i; |
| 5586 | sqlite_int64 rowid; |
| 5587 | Mem **apArg; |
| 5588 | Mem *pX; |
| 5589 | |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 5590 | pVtab = pOp->p4.pVtab->pVtab; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 5591 | pModule = (sqlite3_module *)pVtab->pModule; |
| 5592 | nArg = pOp->p2; |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 5593 | assert( pOp->p4type==P4_VTAB ); |
drh | 35f6b93 | 2009-06-23 14:15:04 +0000 | [diff] [blame] | 5594 | if( ALWAYS(pModule->xUpdate) ){ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 5595 | apArg = p->apArg; |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 5596 | pX = &aMem[pOp->p3]; |
danielk1977 | 2a339ff | 2008-01-03 17:31:44 +0000 | [diff] [blame] | 5597 | for(i=0; i<nArg; i++){ |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 5598 | sqlite3VdbeMemStoreType(pX); |
drh | 9c41938 | 2006-06-16 21:13:21 +0000 | [diff] [blame] | 5599 | apArg[i] = pX; |
danielk1977 | 2a339ff | 2008-01-03 17:31:44 +0000 | [diff] [blame] | 5600 | pX++; |
danielk1977 | 399918f | 2006-06-14 13:03:23 +0000 | [diff] [blame] | 5601 | } |
danielk1977 | c7d5410 | 2006-06-15 07:29:00 +0000 | [diff] [blame] | 5602 | if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse; |
danielk1977 | 1f6eec5 | 2006-06-16 06:17:47 +0000 | [diff] [blame] | 5603 | rc = pModule->xUpdate(pVtab, nArg, apArg, &rowid); |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 5604 | sqlite3DbFree(db, p->zErrMsg); |
drh | 80cc85b | 2008-07-23 21:07:25 +0000 | [diff] [blame] | 5605 | p->zErrMsg = pVtab->zErrMsg; |
| 5606 | pVtab->zErrMsg = 0; |
danielk1977 | c7d5410 | 2006-06-15 07:29:00 +0000 | [diff] [blame] | 5607 | if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse; |
drh | 35f6b93 | 2009-06-23 14:15:04 +0000 | [diff] [blame] | 5608 | if( rc==SQLITE_OK && pOp->p1 ){ |
danielk1977 | 1f6eec5 | 2006-06-16 06:17:47 +0000 | [diff] [blame] | 5609 | assert( nArg>1 && apArg[0] && (apArg[0]->flags&MEM_Null) ); |
| 5610 | db->lastRowid = rowid; |
| 5611 | } |
drh | b5df144 | 2008-04-10 14:00:09 +0000 | [diff] [blame] | 5612 | p->nChange++; |
danielk1977 | 399918f | 2006-06-14 13:03:23 +0000 | [diff] [blame] | 5613 | } |
drh | 4cbdda9 | 2006-06-14 19:00:20 +0000 | [diff] [blame] | 5614 | break; |
danielk1977 | 399918f | 2006-06-14 13:03:23 +0000 | [diff] [blame] | 5615 | } |
| 5616 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 5617 | |
danielk1977 | 59a9379 | 2008-05-15 17:48:20 +0000 | [diff] [blame] | 5618 | #ifndef SQLITE_OMIT_PAGER_PRAGMAS |
| 5619 | /* Opcode: Pagecount P1 P2 * * * |
| 5620 | ** |
| 5621 | ** Write the current number of pages in database P1 to memory cell P2. |
| 5622 | */ |
| 5623 | case OP_Pagecount: { /* out2-prerelease */ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 5624 | int p1; |
danielk1977 | 59a9379 | 2008-05-15 17:48:20 +0000 | [diff] [blame] | 5625 | int nPage; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 5626 | Pager *pPager; |
danielk1977 | 59a9379 | 2008-05-15 17:48:20 +0000 | [diff] [blame] | 5627 | |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 5628 | p1 = pOp->p1; |
| 5629 | pPager = sqlite3BtreePager(db->aDb[p1].pBt); |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame] | 5630 | rc = sqlite3PagerPagecount(pPager, &nPage); |
drh | 35f6b93 | 2009-06-23 14:15:04 +0000 | [diff] [blame] | 5631 | /* OP_Pagecount is always called from within a read transaction. The |
| 5632 | ** page count has already been successfully read and cached. So the |
| 5633 | ** sqlite3PagerPagecount() call above cannot fail. */ |
| 5634 | if( ALWAYS(rc==SQLITE_OK) ){ |
danielk1977 | 59a9379 | 2008-05-15 17:48:20 +0000 | [diff] [blame] | 5635 | pOut->u.i = nPage; |
| 5636 | } |
| 5637 | break; |
| 5638 | } |
| 5639 | #endif |
| 5640 | |
drh | 949f9cd | 2008-01-12 21:35:57 +0000 | [diff] [blame] | 5641 | #ifndef SQLITE_OMIT_TRACE |
| 5642 | /* Opcode: Trace * * * P4 * |
| 5643 | ** |
| 5644 | ** If tracing is enabled (by the sqlite3_trace()) interface, then |
| 5645 | ** the UTF-8 string contained in P4 is emitted on the trace callback. |
| 5646 | */ |
| 5647 | case OP_Trace: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 5648 | char *zTrace; |
| 5649 | |
| 5650 | zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql); |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 5651 | if( zTrace ){ |
drh | 949f9cd | 2008-01-12 21:35:57 +0000 | [diff] [blame] | 5652 | if( db->xTrace ){ |
drh | c7bc4fd | 2009-11-25 18:03:42 +0000 | [diff] [blame] | 5653 | char *z = sqlite3VdbeExpandSql(p, zTrace); |
| 5654 | db->xTrace(db->pTraceArg, z); |
| 5655 | sqlite3DbFree(db, z); |
drh | 949f9cd | 2008-01-12 21:35:57 +0000 | [diff] [blame] | 5656 | } |
| 5657 | #ifdef SQLITE_DEBUG |
| 5658 | if( (db->flags & SQLITE_SqlTrace)!=0 ){ |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 5659 | sqlite3DebugPrintf("SQL-trace: %s\n", zTrace); |
drh | 949f9cd | 2008-01-12 21:35:57 +0000 | [diff] [blame] | 5660 | } |
| 5661 | #endif /* SQLITE_DEBUG */ |
| 5662 | } |
| 5663 | break; |
| 5664 | } |
| 5665 | #endif |
| 5666 | |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 5667 | |
| 5668 | /* Opcode: Noop * * * * * |
| 5669 | ** |
| 5670 | ** Do nothing. This instruction is often useful as a jump |
| 5671 | ** destination. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5672 | */ |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 5673 | /* |
| 5674 | ** The magic Explain opcode are only inserted when explain==2 (which |
| 5675 | ** is to say when the EXPLAIN QUERY PLAN syntax is used.) |
| 5676 | ** This opcode records information from the optimizer. It is the |
| 5677 | ** the same as a no-op. This opcodesnever appears in a real VM program. |
| 5678 | */ |
| 5679 | default: { /* This is really OP_Noop and OP_Explain */ |
drh | 13573c7 | 2010-01-12 17:04:07 +0000 | [diff] [blame] | 5680 | assert( pOp->opcode==OP_Noop || pOp->opcode==OP_Explain ); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5681 | break; |
| 5682 | } |
| 5683 | |
| 5684 | /***************************************************************************** |
| 5685 | ** The cases of the switch statement above this line should all be indented |
| 5686 | ** by 6 spaces. But the left-most 6 spaces have been removed to improve the |
| 5687 | ** readability. From this point on down, the normal indentation rules are |
| 5688 | ** restored. |
| 5689 | *****************************************************************************/ |
| 5690 | } |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 5691 | |
drh | 7b39686 | 2003-01-01 23:06:20 +0000 | [diff] [blame] | 5692 | #ifdef VDBE_PROFILE |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 5693 | { |
shane | 9bcbdad | 2008-05-29 20:22:37 +0000 | [diff] [blame] | 5694 | u64 elapsed = sqlite3Hwtime() - start; |
| 5695 | pOp->cycles += elapsed; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 5696 | pOp->cnt++; |
| 5697 | #if 0 |
shane | 9bcbdad | 2008-05-29 20:22:37 +0000 | [diff] [blame] | 5698 | fprintf(stdout, "%10llu ", elapsed); |
drh | bbe879d | 2009-11-14 18:04:35 +0000 | [diff] [blame] | 5699 | sqlite3VdbePrintOp(stdout, origPc, &aOp[origPc]); |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 5700 | #endif |
| 5701 | } |
drh | 7b39686 | 2003-01-01 23:06:20 +0000 | [diff] [blame] | 5702 | #endif |
| 5703 | |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 5704 | /* The following code adds nothing to the actual functionality |
| 5705 | ** of the program. It is only here for testing and debugging. |
| 5706 | ** On the other hand, it does burn CPU cycles every time through |
| 5707 | ** the evaluator loop. So we can leave it out when NDEBUG is defined. |
| 5708 | */ |
| 5709 | #ifndef NDEBUG |
drh | a611040 | 2005-07-28 20:51:19 +0000 | [diff] [blame] | 5710 | assert( pc>=-1 && pc<p->nOp ); |
drh | ae7e151 | 2007-05-02 16:51:59 +0000 | [diff] [blame] | 5711 | |
drh | cf1023c | 2007-05-08 20:59:49 +0000 | [diff] [blame] | 5712 | #ifdef SQLITE_DEBUG |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 5713 | if( p->trace ){ |
| 5714 | if( rc!=0 ) fprintf(p->trace,"rc=%d\n",rc); |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 5715 | if( pOp->opflags & (OPFLG_OUT2_PRERELEASE|OPFLG_OUT2) ){ |
| 5716 | registerTrace(p->trace, pOp->p2, &aMem[pOp->p2]); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5717 | } |
drh | 3c65721 | 2009-11-17 23:59:58 +0000 | [diff] [blame] | 5718 | if( pOp->opflags & OPFLG_OUT3 ){ |
| 5719 | registerTrace(p->trace, pOp->p3, &aMem[pOp->p3]); |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 5720 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5721 | } |
danielk1977 | b5402fb | 2005-01-12 07:15:04 +0000 | [diff] [blame] | 5722 | #endif /* SQLITE_DEBUG */ |
| 5723 | #endif /* NDEBUG */ |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 5724 | } /* The end of the for(;;) loop the loops through opcodes */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5725 | |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 5726 | /* If we reach this point, it means that execution is finished with |
| 5727 | ** an error of some kind. |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 5728 | */ |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 5729 | vdbe_error_halt: |
| 5730 | assert( rc ); |
| 5731 | p->rc = rc; |
drh | 92f02c3 | 2004-09-02 14:57:08 +0000 | [diff] [blame] | 5732 | sqlite3VdbeHalt(p); |
danielk1977 | 7eaabcd | 2008-07-07 14:56:56 +0000 | [diff] [blame] | 5733 | if( rc==SQLITE_IOERR_NOMEM ) db->mallocFailed = 1; |
| 5734 | rc = SQLITE_ERROR; |
drh | 3278315 | 2009-11-20 15:02:34 +0000 | [diff] [blame] | 5735 | if( resetSchemaOnFault ) sqlite3ResetInternalSchema(db, 0); |
drh | 900b31e | 2007-08-28 02:27:51 +0000 | [diff] [blame] | 5736 | |
| 5737 | /* This is the only way out of this procedure. We have to |
| 5738 | ** release the mutexes on btrees that were acquired at the |
| 5739 | ** top. */ |
| 5740 | vdbe_return: |
drh | 4cf7c7f | 2007-08-28 23:28:07 +0000 | [diff] [blame] | 5741 | sqlite3BtreeMutexArrayLeave(&p->aMutex); |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 5742 | return rc; |
| 5743 | |
drh | 023ae03 | 2007-05-08 12:12:16 +0000 | [diff] [blame] | 5744 | /* Jump to here if a string or blob larger than SQLITE_MAX_LENGTH |
| 5745 | ** is encountered. |
| 5746 | */ |
| 5747 | too_big: |
drh | f089aa4 | 2008-07-08 19:34:06 +0000 | [diff] [blame] | 5748 | sqlite3SetString(&p->zErrMsg, db, "string or blob too big"); |
drh | 023ae03 | 2007-05-08 12:12:16 +0000 | [diff] [blame] | 5749 | rc = SQLITE_TOOBIG; |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 5750 | goto vdbe_error_halt; |
drh | 023ae03 | 2007-05-08 12:12:16 +0000 | [diff] [blame] | 5751 | |
drh | 98640a3 | 2007-06-07 19:08:32 +0000 | [diff] [blame] | 5752 | /* Jump to here if a malloc() fails. |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 5753 | */ |
| 5754 | no_mem: |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 5755 | db->mallocFailed = 1; |
drh | f089aa4 | 2008-07-08 19:34:06 +0000 | [diff] [blame] | 5756 | sqlite3SetString(&p->zErrMsg, db, "out of memory"); |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 5757 | rc = SQLITE_NOMEM; |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 5758 | goto vdbe_error_halt; |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 5759 | |
| 5760 | /* Jump to here for an SQLITE_MISUSE error. |
| 5761 | */ |
| 5762 | abort_due_to_misuse: |
| 5763 | rc = SQLITE_MISUSE; |
| 5764 | /* Fall thru into abort_due_to_error */ |
| 5765 | |
| 5766 | /* Jump to here for any other kind of fatal error. The "rc" variable |
| 5767 | ** should hold the error number. |
| 5768 | */ |
| 5769 | abort_due_to_error: |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 5770 | assert( p->zErrMsg==0 ); |
| 5771 | if( db->mallocFailed ) rc = SQLITE_NOMEM; |
danielk1977 | 7eaabcd | 2008-07-07 14:56:56 +0000 | [diff] [blame] | 5772 | if( rc!=SQLITE_IOERR_NOMEM ){ |
drh | f089aa4 | 2008-07-08 19:34:06 +0000 | [diff] [blame] | 5773 | sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3ErrStr(rc)); |
danielk1977 | 7eaabcd | 2008-07-07 14:56:56 +0000 | [diff] [blame] | 5774 | } |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 5775 | goto vdbe_error_halt; |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 5776 | |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 5777 | /* Jump to here if the sqlite3_interrupt() API sets the interrupt |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 5778 | ** flag. |
| 5779 | */ |
| 5780 | abort_due_to_interrupt: |
drh | 881feaa | 2006-07-26 01:39:30 +0000 | [diff] [blame] | 5781 | assert( db->u1.isInterrupted ); |
drh | 7e8b848 | 2008-01-23 03:03:05 +0000 | [diff] [blame] | 5782 | rc = SQLITE_INTERRUPT; |
danielk1977 | 026d270 | 2004-06-14 13:14:59 +0000 | [diff] [blame] | 5783 | p->rc = rc; |
drh | f089aa4 | 2008-07-08 19:34:06 +0000 | [diff] [blame] | 5784 | sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3ErrStr(rc)); |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 5785 | goto vdbe_error_halt; |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 5786 | } |