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. |
| 45 | ** |
drh | 27731d7 | 2009-06-22 12:05:10 +0000 | [diff] [blame^] | 46 | ** $Id: vdbe.c,v 1.860 2009/06/22 12:05:10 drh Exp $ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 47 | */ |
| 48 | #include "sqliteInt.h" |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 49 | #include "vdbeInt.h" |
drh | 8f619cc | 2002-09-08 00:04:50 +0000 | [diff] [blame] | 50 | |
| 51 | /* |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 52 | ** The following global variable is incremented every time a cursor |
drh | 959403f | 2008-12-12 17:56:16 +0000 | [diff] [blame] | 53 | ** 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] | 54 | ** procedures use this information to make sure that indices are |
drh | ac82fcf | 2002-09-08 17:23:41 +0000 | [diff] [blame] | 55 | ** working correctly. This variable has no function other than to |
| 56 | ** help verify the correct operation of the library. |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 57 | */ |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 58 | #ifdef SQLITE_TEST |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 59 | int sqlite3_search_count = 0; |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 60 | #endif |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 61 | |
drh | f603871 | 2004-02-08 18:07:34 +0000 | [diff] [blame] | 62 | /* |
| 63 | ** When this global variable is positive, it gets decremented once before |
drh | 881feaa | 2006-07-26 01:39:30 +0000 | [diff] [blame] | 64 | ** each instruction in the VDBE. When reaches zero, the u1.isInterrupted |
| 65 | ** field of the sqlite3 structure is set in order to simulate and interrupt. |
drh | f603871 | 2004-02-08 18:07:34 +0000 | [diff] [blame] | 66 | ** |
| 67 | ** This facility is used for testing purposes only. It does not function |
| 68 | ** in an ordinary build. |
| 69 | */ |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 70 | #ifdef SQLITE_TEST |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 71 | int sqlite3_interrupt_count = 0; |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 72 | #endif |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 73 | |
danielk1977 | 7e18c25 | 2004-05-25 11:47:24 +0000 | [diff] [blame] | 74 | /* |
drh | 6bf8957 | 2004-11-03 16:27:01 +0000 | [diff] [blame] | 75 | ** The next global variable is incremented each type the OP_Sort opcode |
| 76 | ** is executed. The test procedures use this information to make sure that |
shane | 21e7feb | 2008-05-30 15:59:49 +0000 | [diff] [blame] | 77 | ** sorting is occurring or not occurring at appropriate times. This variable |
drh | 6bf8957 | 2004-11-03 16:27:01 +0000 | [diff] [blame] | 78 | ** has no function other than to help verify the correct operation of the |
| 79 | ** library. |
| 80 | */ |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 81 | #ifdef SQLITE_TEST |
drh | 6bf8957 | 2004-11-03 16:27:01 +0000 | [diff] [blame] | 82 | int sqlite3_sort_count = 0; |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 83 | #endif |
drh | 6bf8957 | 2004-11-03 16:27:01 +0000 | [diff] [blame] | 84 | |
| 85 | /* |
drh | ae7e151 | 2007-05-02 16:51:59 +0000 | [diff] [blame] | 86 | ** The next global variable records the size of the largest MEM_Blob |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 87 | ** 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] | 88 | ** use this information to make sure that the zero-blob functionality |
| 89 | ** is working correctly. This variable has no function other than to |
| 90 | ** help verify the correct operation of the library. |
| 91 | */ |
| 92 | #ifdef SQLITE_TEST |
| 93 | int sqlite3_max_blobsize = 0; |
drh | ca48c90 | 2008-01-18 14:08:24 +0000 | [diff] [blame] | 94 | static void updateMaxBlobsize(Mem *p){ |
| 95 | if( (p->flags & (MEM_Str|MEM_Blob))!=0 && p->n>sqlite3_max_blobsize ){ |
| 96 | sqlite3_max_blobsize = p->n; |
| 97 | } |
| 98 | } |
drh | ae7e151 | 2007-05-02 16:51:59 +0000 | [diff] [blame] | 99 | #endif |
| 100 | |
| 101 | /* |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 102 | ** Test a register to see if it exceeds the current maximum blob size. |
| 103 | ** If it does, record the new maximum blob size. |
| 104 | */ |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 105 | #if defined(SQLITE_TEST) && !defined(SQLITE_OMIT_BUILTIN_TEST) |
drh | ca48c90 | 2008-01-18 14:08:24 +0000 | [diff] [blame] | 106 | # define UPDATE_MAX_BLOBSIZE(P) updateMaxBlobsize(P) |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 107 | #else |
| 108 | # define UPDATE_MAX_BLOBSIZE(P) |
| 109 | #endif |
| 110 | |
| 111 | /* |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 112 | ** Convert the given register into a string if it isn't one |
danielk1977 | bd7e460 | 2004-05-24 07:34:48 +0000 | [diff] [blame] | 113 | ** already. Return non-zero if a malloc() fails. |
| 114 | */ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 115 | #define Stringify(P, enc) \ |
| 116 | if(((P)->flags&(MEM_Str|MEM_Blob))==0 && sqlite3VdbeMemStringify(P,enc)) \ |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 117 | { goto no_mem; } |
danielk1977 | bd7e460 | 2004-05-24 07:34:48 +0000 | [diff] [blame] | 118 | |
| 119 | /* |
danielk1977 | bd7e460 | 2004-05-24 07:34:48 +0000 | [diff] [blame] | 120 | ** An ephemeral string value (signified by the MEM_Ephem flag) contains |
| 121 | ** a pointer to a dynamically allocated string where some other entity |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 122 | ** is responsible for deallocating that string. Because the register |
| 123 | ** does not control the string, it might be deleted without the register |
| 124 | ** knowing it. |
danielk1977 | bd7e460 | 2004-05-24 07:34:48 +0000 | [diff] [blame] | 125 | ** |
| 126 | ** This routine converts an ephemeral string into a dynamically allocated |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 127 | ** string that the register itself controls. In other words, it |
danielk1977 | bd7e460 | 2004-05-24 07:34:48 +0000 | [diff] [blame] | 128 | ** converts an MEM_Ephem string into an MEM_Dyn string. |
| 129 | */ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 130 | #define Deephemeralize(P) \ |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 131 | if( ((P)->flags&MEM_Ephem)!=0 \ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 132 | && sqlite3VdbeMemMakeWriteable(P) ){ goto no_mem;} |
danielk1977 | 93d4675 | 2004-05-23 13:30:58 +0000 | [diff] [blame] | 133 | |
| 134 | /* |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 135 | ** Call sqlite3VdbeMemExpandBlob() on the supplied value (type Mem*) |
| 136 | ** P if required. |
| 137 | */ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 138 | #define ExpandBlob(P) (((P)->flags&MEM_Zero)?sqlite3VdbeMemExpandBlob(P):0) |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 139 | |
| 140 | /* |
shane | 21e7feb | 2008-05-30 15:59:49 +0000 | [diff] [blame] | 141 | ** Argument pMem points at a register that will be passed to a |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 142 | ** user-defined function or returned to the user as the result of a query. |
| 143 | ** The second argument, 'db_enc' is the text encoding used by the vdbe for |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 144 | ** register variables. This routine sets the pMem->enc and pMem->type |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 145 | ** variables used by the sqlite3_value_*() routines. |
| 146 | */ |
drh | 3a41a3f | 2004-05-30 02:14:17 +0000 | [diff] [blame] | 147 | #define storeTypeInfo(A,B) _storeTypeInfo(A) |
| 148 | static void _storeTypeInfo(Mem *pMem){ |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 149 | int flags = pMem->flags; |
| 150 | if( flags & MEM_Null ){ |
drh | 9c05483 | 2004-05-31 18:51:57 +0000 | [diff] [blame] | 151 | pMem->type = SQLITE_NULL; |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 152 | } |
| 153 | else if( flags & MEM_Int ){ |
drh | 9c05483 | 2004-05-31 18:51:57 +0000 | [diff] [blame] | 154 | pMem->type = SQLITE_INTEGER; |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 155 | } |
| 156 | else if( flags & MEM_Real ){ |
drh | 9c05483 | 2004-05-31 18:51:57 +0000 | [diff] [blame] | 157 | pMem->type = SQLITE_FLOAT; |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 158 | } |
| 159 | else if( flags & MEM_Str ){ |
drh | 9c05483 | 2004-05-31 18:51:57 +0000 | [diff] [blame] | 160 | pMem->type = SQLITE_TEXT; |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 161 | }else{ |
drh | 9c05483 | 2004-05-31 18:51:57 +0000 | [diff] [blame] | 162 | pMem->type = SQLITE_BLOB; |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 163 | } |
| 164 | } |
danielk1977 | 8a6b541 | 2004-05-24 07:04:25 +0000 | [diff] [blame] | 165 | |
| 166 | /* |
drh | 3a40f69 | 2008-01-04 16:50:09 +0000 | [diff] [blame] | 167 | ** Properties of opcodes. The OPFLG_INITIALIZER macro is |
| 168 | ** created by mkopcodeh.awk during compilation. Data is obtained |
| 169 | ** from the comments following the "case OP_xxxx:" statements in |
| 170 | ** this file. |
drh | 3a40f69 | 2008-01-04 16:50:09 +0000 | [diff] [blame] | 171 | */ |
danielk1977 | 263ac19 | 2008-09-02 11:05:01 +0000 | [diff] [blame] | 172 | static const unsigned char opcodeProperty[] = OPFLG_INITIALIZER; |
drh | 3a40f69 | 2008-01-04 16:50:09 +0000 | [diff] [blame] | 173 | |
| 174 | /* |
| 175 | ** Return true if an opcode has any of the OPFLG_xxx properties |
| 176 | ** specified by mask. |
| 177 | */ |
| 178 | int sqlite3VdbeOpcodeHasProperty(int opcode, int mask){ |
danielk1977 | 64202cf | 2008-11-17 15:31:47 +0000 | [diff] [blame] | 179 | assert( opcode>0 && opcode<(int)sizeof(opcodeProperty) ); |
drh | 3a40f69 | 2008-01-04 16:50:09 +0000 | [diff] [blame] | 180 | return (opcodeProperty[opcode]&mask)!=0; |
| 181 | } |
| 182 | |
| 183 | /* |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 184 | ** Allocate VdbeCursor number iCur. Return a pointer to it. Return NULL |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 185 | ** if we run out of memory. |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 186 | */ |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 187 | static VdbeCursor *allocateCursor( |
| 188 | Vdbe *p, /* The virtual machine */ |
| 189 | int iCur, /* Index of the new VdbeCursor */ |
danielk1977 | d336e22 | 2009-02-20 10:58:41 +0000 | [diff] [blame] | 190 | int nField, /* Number of fields in the table or index */ |
drh | 3d4501e | 2008-12-04 20:40:10 +0000 | [diff] [blame] | 191 | int iDb, /* When database the cursor belongs to, or -1 */ |
drh | 94c3a2b | 2009-06-17 16:20:04 +0000 | [diff] [blame] | 192 | int isBtreeCursor /* True for B-Tree vs. pseudo-table or vtab */ |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 193 | ){ |
| 194 | /* 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] | 195 | ** required for this VdbeCursor structure. It is convenient to use a |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 196 | ** vdbe memory cell to manage the memory allocation required for a |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 197 | ** VdbeCursor structure for the following reasons: |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 198 | ** |
| 199 | ** * Sometimes cursor numbers are used for a couple of different |
| 200 | ** purposes in a vdbe program. The different uses might require |
| 201 | ** different sized allocations. Memory cells provide growable |
| 202 | ** allocations. |
| 203 | ** |
| 204 | ** * When using ENABLE_MEMORY_MANAGEMENT, memory cell buffers can |
| 205 | ** be freed lazily via the sqlite3_release_memory() API. This |
| 206 | ** minimizes the number of malloc calls made by the system. |
| 207 | ** |
| 208 | ** Memory cells for cursors are allocated at the top of the address |
| 209 | ** space. Memory cell (p->nMem) corresponds to cursor 0. Space for |
| 210 | ** cursor 1 is managed by memory cell (p->nMem-1), etc. |
| 211 | */ |
| 212 | Mem *pMem = &p->aMem[p->nMem-iCur]; |
| 213 | |
danielk1977 | 5f09613 | 2008-03-28 15:44:09 +0000 | [diff] [blame] | 214 | int nByte; |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 215 | VdbeCursor *pCx = 0; |
danielk1977 | 5f09613 | 2008-03-28 15:44:09 +0000 | [diff] [blame] | 216 | nByte = |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 217 | sizeof(VdbeCursor) + |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 218 | (isBtreeCursor?sqlite3BtreeCursorSize():0) + |
| 219 | 2*nField*sizeof(u32); |
| 220 | |
drh | 290c194 | 2004-08-21 17:54:45 +0000 | [diff] [blame] | 221 | assert( iCur<p->nCursor ); |
| 222 | if( p->apCsr[iCur] ){ |
danielk1977 | be71889 | 2006-06-23 08:05:19 +0000 | [diff] [blame] | 223 | sqlite3VdbeFreeCursor(p, p->apCsr[iCur]); |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 224 | p->apCsr[iCur] = 0; |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 225 | } |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 226 | if( SQLITE_OK==sqlite3VdbeMemGrow(pMem, nByte, 0) ){ |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 227 | p->apCsr[iCur] = pCx = (VdbeCursor*)pMem->z; |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 228 | memset(pMem->z, 0, nByte); |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 +0000 | [diff] [blame] | 229 | pCx->iDb = iDb; |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 230 | pCx->nField = nField; |
| 231 | if( nField ){ |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 232 | pCx->aType = (u32 *)&pMem->z[sizeof(VdbeCursor)]; |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 233 | } |
| 234 | if( isBtreeCursor ){ |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 235 | pCx->pCursor = (BtCursor*) |
| 236 | &pMem->z[sizeof(VdbeCursor)+2*nField*sizeof(u32)]; |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 237 | } |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 +0000 | [diff] [blame] | 238 | } |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 239 | return pCx; |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 240 | } |
| 241 | |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 242 | /* |
drh | 29d7210 | 2006-02-09 22:13:41 +0000 | [diff] [blame] | 243 | ** Try to convert a value into a numeric representation if we can |
| 244 | ** do so without loss of information. In other words, if the string |
| 245 | ** looks like a number, convert it into a number. If it does not |
| 246 | ** look like a number, leave it alone. |
| 247 | */ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 248 | static void applyNumericAffinity(Mem *pRec){ |
drh | 29d7210 | 2006-02-09 22:13:41 +0000 | [diff] [blame] | 249 | if( (pRec->flags & (MEM_Real|MEM_Int))==0 ){ |
| 250 | int realnum; |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 251 | sqlite3VdbeMemNulTerminate(pRec); |
drh | 29d7210 | 2006-02-09 22:13:41 +0000 | [diff] [blame] | 252 | if( (pRec->flags&MEM_Str) |
| 253 | && sqlite3IsNumber(pRec->z, &realnum, pRec->enc) ){ |
| 254 | i64 value; |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 255 | sqlite3VdbeChangeEncoding(pRec, SQLITE_UTF8); |
drh | b6a9ece | 2007-06-26 00:37:27 +0000 | [diff] [blame] | 256 | if( !realnum && sqlite3Atoi64(pRec->z, &value) ){ |
drh | 3c024d6 | 2007-03-30 11:23:45 +0000 | [diff] [blame] | 257 | pRec->u.i = value; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 258 | MemSetTypeFlag(pRec, MEM_Int); |
drh | 29d7210 | 2006-02-09 22:13:41 +0000 | [diff] [blame] | 259 | }else{ |
| 260 | sqlite3VdbeMemRealify(pRec); |
| 261 | } |
| 262 | } |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | /* |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 267 | ** Processing is determine by the affinity parameter: |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 268 | ** |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 269 | ** SQLITE_AFF_INTEGER: |
| 270 | ** SQLITE_AFF_REAL: |
| 271 | ** SQLITE_AFF_NUMERIC: |
| 272 | ** Try to convert pRec to an integer representation or a |
| 273 | ** floating-point representation if an integer representation |
| 274 | ** is not possible. Note that the integer representation is |
| 275 | ** always preferred, even if the affinity is REAL, because |
| 276 | ** an integer representation is more space efficient on disk. |
| 277 | ** |
| 278 | ** SQLITE_AFF_TEXT: |
| 279 | ** Convert pRec to a text representation. |
| 280 | ** |
| 281 | ** SQLITE_AFF_NONE: |
| 282 | ** No-op. pRec is unchanged. |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 283 | */ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 284 | static void applyAffinity( |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 285 | Mem *pRec, /* The value to apply affinity to */ |
| 286 | char affinity, /* The affinity to be applied */ |
| 287 | u8 enc /* Use this text encoding */ |
| 288 | ){ |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 289 | if( affinity==SQLITE_AFF_TEXT ){ |
drh | 17c4029 | 2004-07-21 02:53:29 +0000 | [diff] [blame] | 290 | /* Only attempt the conversion to TEXT if there is an integer or real |
| 291 | ** representation (blob and NULL do not get converted) but no string |
| 292 | ** representation. |
| 293 | */ |
| 294 | if( 0==(pRec->flags&MEM_Str) && (pRec->flags&(MEM_Real|MEM_Int)) ){ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 295 | sqlite3VdbeMemStringify(pRec, enc); |
drh | 17c4029 | 2004-07-21 02:53:29 +0000 | [diff] [blame] | 296 | } |
| 297 | pRec->flags &= ~(MEM_Real|MEM_Int); |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 298 | }else if( affinity!=SQLITE_AFF_NONE ){ |
| 299 | assert( affinity==SQLITE_AFF_INTEGER || affinity==SQLITE_AFF_REAL |
| 300 | || affinity==SQLITE_AFF_NUMERIC ); |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 301 | applyNumericAffinity(pRec); |
drh | 29d7210 | 2006-02-09 22:13:41 +0000 | [diff] [blame] | 302 | if( pRec->flags & MEM_Real ){ |
drh | 8df447f | 2005-11-01 15:48:24 +0000 | [diff] [blame] | 303 | sqlite3VdbeIntegerAffinity(pRec); |
drh | 17c4029 | 2004-07-21 02:53:29 +0000 | [diff] [blame] | 304 | } |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 305 | } |
| 306 | } |
| 307 | |
danielk1977 | aee18ef | 2005-03-09 12:26:50 +0000 | [diff] [blame] | 308 | /* |
drh | 29d7210 | 2006-02-09 22:13:41 +0000 | [diff] [blame] | 309 | ** Try to convert the type of a function argument or a result column |
| 310 | ** into a numeric representation. Use either INTEGER or REAL whichever |
| 311 | ** is appropriate. But only do the conversion if it is possible without |
| 312 | ** loss of information and return the revised type of the argument. |
| 313 | ** |
| 314 | ** This is an EXPERIMENTAL api and is subject to change or removal. |
| 315 | */ |
| 316 | int sqlite3_value_numeric_type(sqlite3_value *pVal){ |
| 317 | Mem *pMem = (Mem*)pVal; |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 318 | applyNumericAffinity(pMem); |
drh | 29d7210 | 2006-02-09 22:13:41 +0000 | [diff] [blame] | 319 | storeTypeInfo(pMem, 0); |
| 320 | return pMem->type; |
| 321 | } |
| 322 | |
| 323 | /* |
danielk1977 | aee18ef | 2005-03-09 12:26:50 +0000 | [diff] [blame] | 324 | ** Exported version of applyAffinity(). This one works on sqlite3_value*, |
| 325 | ** not the internal Mem* type. |
| 326 | */ |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 327 | void sqlite3ValueApplyAffinity( |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 328 | sqlite3_value *pVal, |
| 329 | u8 affinity, |
| 330 | u8 enc |
| 331 | ){ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 332 | applyAffinity((Mem *)pVal, affinity, enc); |
danielk1977 | aee18ef | 2005-03-09 12:26:50 +0000 | [diff] [blame] | 333 | } |
| 334 | |
danielk1977 | b5402fb | 2005-01-12 07:15:04 +0000 | [diff] [blame] | 335 | #ifdef SQLITE_DEBUG |
drh | b6f5452 | 2004-05-20 02:42:16 +0000 | [diff] [blame] | 336 | /* |
danielk1977 | ca6b291 | 2004-05-21 10:49:47 +0000 | [diff] [blame] | 337 | ** Write a nice string representation of the contents of cell pMem |
| 338 | ** into buffer zBuf, length nBuf. |
| 339 | */ |
drh | 7416170 | 2006-02-24 02:53:49 +0000 | [diff] [blame] | 340 | void sqlite3VdbeMemPrettyPrint(Mem *pMem, char *zBuf){ |
danielk1977 | ca6b291 | 2004-05-21 10:49:47 +0000 | [diff] [blame] | 341 | char *zCsr = zBuf; |
| 342 | int f = pMem->flags; |
| 343 | |
drh | 5719628 | 2004-10-06 15:41:16 +0000 | [diff] [blame] | 344 | static const char *const encnames[] = {"(X)", "(8)", "(16LE)", "(16BE)"}; |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame] | 345 | |
danielk1977 | ca6b291 | 2004-05-21 10:49:47 +0000 | [diff] [blame] | 346 | if( f&MEM_Blob ){ |
| 347 | int i; |
| 348 | char c; |
| 349 | if( f & MEM_Dyn ){ |
| 350 | c = 'z'; |
| 351 | assert( (f & (MEM_Static|MEM_Ephem))==0 ); |
| 352 | }else if( f & MEM_Static ){ |
| 353 | c = 't'; |
| 354 | assert( (f & (MEM_Dyn|MEM_Ephem))==0 ); |
| 355 | }else if( f & MEM_Ephem ){ |
| 356 | c = 'e'; |
| 357 | assert( (f & (MEM_Static|MEM_Dyn))==0 ); |
| 358 | }else{ |
| 359 | c = 's'; |
| 360 | } |
| 361 | |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 362 | sqlite3_snprintf(100, zCsr, "%c", c); |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 363 | zCsr += sqlite3Strlen30(zCsr); |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 364 | sqlite3_snprintf(100, zCsr, "%d[", pMem->n); |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 365 | zCsr += sqlite3Strlen30(zCsr); |
danielk1977 | ca6b291 | 2004-05-21 10:49:47 +0000 | [diff] [blame] | 366 | for(i=0; i<16 && i<pMem->n; i++){ |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 367 | sqlite3_snprintf(100, zCsr, "%02X", ((int)pMem->z[i] & 0xFF)); |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 368 | zCsr += sqlite3Strlen30(zCsr); |
danielk1977 | ca6b291 | 2004-05-21 10:49:47 +0000 | [diff] [blame] | 369 | } |
| 370 | for(i=0; i<16 && i<pMem->n; i++){ |
| 371 | char z = pMem->z[i]; |
| 372 | if( z<32 || z>126 ) *zCsr++ = '.'; |
| 373 | else *zCsr++ = z; |
| 374 | } |
| 375 | |
drh | e718efe | 2007-05-10 21:14:03 +0000 | [diff] [blame] | 376 | sqlite3_snprintf(100, zCsr, "]%s", encnames[pMem->enc]); |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 377 | zCsr += sqlite3Strlen30(zCsr); |
drh | fdf972a | 2007-05-02 13:30:27 +0000 | [diff] [blame] | 378 | if( f & MEM_Zero ){ |
drh | 8df3284 | 2008-12-09 02:51:23 +0000 | [diff] [blame] | 379 | sqlite3_snprintf(100, zCsr,"+%dz",pMem->u.nZero); |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 380 | zCsr += sqlite3Strlen30(zCsr); |
drh | fdf972a | 2007-05-02 13:30:27 +0000 | [diff] [blame] | 381 | } |
danielk1977 | b1bc953 | 2004-05-22 03:05:33 +0000 | [diff] [blame] | 382 | *zCsr = '\0'; |
| 383 | }else if( f & MEM_Str ){ |
| 384 | int j, k; |
| 385 | zBuf[0] = ' '; |
| 386 | if( f & MEM_Dyn ){ |
| 387 | zBuf[1] = 'z'; |
| 388 | assert( (f & (MEM_Static|MEM_Ephem))==0 ); |
| 389 | }else if( f & MEM_Static ){ |
| 390 | zBuf[1] = 't'; |
| 391 | assert( (f & (MEM_Dyn|MEM_Ephem))==0 ); |
| 392 | }else if( f & MEM_Ephem ){ |
| 393 | zBuf[1] = 'e'; |
| 394 | assert( (f & (MEM_Static|MEM_Dyn))==0 ); |
| 395 | }else{ |
| 396 | zBuf[1] = 's'; |
| 397 | } |
| 398 | k = 2; |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 399 | sqlite3_snprintf(100, &zBuf[k], "%d", pMem->n); |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 400 | k += sqlite3Strlen30(&zBuf[k]); |
danielk1977 | b1bc953 | 2004-05-22 03:05:33 +0000 | [diff] [blame] | 401 | zBuf[k++] = '['; |
| 402 | for(j=0; j<15 && j<pMem->n; j++){ |
| 403 | u8 c = pMem->z[j]; |
danielk1977 | b1bc953 | 2004-05-22 03:05:33 +0000 | [diff] [blame] | 404 | if( c>=0x20 && c<0x7f ){ |
| 405 | zBuf[k++] = c; |
| 406 | }else{ |
| 407 | zBuf[k++] = '.'; |
| 408 | } |
| 409 | } |
| 410 | zBuf[k++] = ']'; |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 411 | sqlite3_snprintf(100,&zBuf[k], encnames[pMem->enc]); |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 412 | k += sqlite3Strlen30(&zBuf[k]); |
danielk1977 | b1bc953 | 2004-05-22 03:05:33 +0000 | [diff] [blame] | 413 | zBuf[k++] = 0; |
danielk1977 | ca6b291 | 2004-05-21 10:49:47 +0000 | [diff] [blame] | 414 | } |
danielk1977 | ca6b291 | 2004-05-21 10:49:47 +0000 | [diff] [blame] | 415 | } |
| 416 | #endif |
| 417 | |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 418 | #ifdef SQLITE_DEBUG |
| 419 | /* |
| 420 | ** Print the value of a register for tracing purposes: |
| 421 | */ |
| 422 | static void memTracePrint(FILE *out, Mem *p){ |
| 423 | if( p->flags & MEM_Null ){ |
| 424 | fprintf(out, " NULL"); |
| 425 | }else if( (p->flags & (MEM_Int|MEM_Str))==(MEM_Int|MEM_Str) ){ |
| 426 | fprintf(out, " si:%lld", p->u.i); |
| 427 | }else if( p->flags & MEM_Int ){ |
| 428 | fprintf(out, " i:%lld", p->u.i); |
drh | 0b3bf92 | 2009-06-15 20:45:34 +0000 | [diff] [blame] | 429 | #ifndef SQLITE_OMIT_FLOATING_POINT |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 430 | }else if( p->flags & MEM_Real ){ |
| 431 | fprintf(out, " r:%g", p->r); |
drh | 0b3bf92 | 2009-06-15 20:45:34 +0000 | [diff] [blame] | 432 | #endif |
drh | 733bf1b | 2009-04-22 00:47:00 +0000 | [diff] [blame] | 433 | }else if( p->flags & MEM_RowSet ){ |
| 434 | fprintf(out, " (rowset)"); |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 435 | }else{ |
| 436 | char zBuf[200]; |
| 437 | sqlite3VdbeMemPrettyPrint(p, zBuf); |
| 438 | fprintf(out, " "); |
| 439 | fprintf(out, "%s", zBuf); |
| 440 | } |
| 441 | } |
| 442 | static void registerTrace(FILE *out, int iReg, Mem *p){ |
| 443 | fprintf(out, "REG[%d] = ", iReg); |
| 444 | memTracePrint(out, p); |
| 445 | fprintf(out, "\n"); |
| 446 | } |
| 447 | #endif |
| 448 | |
| 449 | #ifdef SQLITE_DEBUG |
drh | b21e7c7 | 2008-06-22 12:37:57 +0000 | [diff] [blame] | 450 | # define REGISTER_TRACE(R,M) if(p->trace)registerTrace(p->trace,R,M) |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 451 | #else |
| 452 | # define REGISTER_TRACE(R,M) |
| 453 | #endif |
| 454 | |
danielk1977 | 84ac9d0 | 2004-05-18 09:58:06 +0000 | [diff] [blame] | 455 | |
drh | 7b39686 | 2003-01-01 23:06:20 +0000 | [diff] [blame] | 456 | #ifdef VDBE_PROFILE |
shane | 9bcbdad | 2008-05-29 20:22:37 +0000 | [diff] [blame] | 457 | |
| 458 | /* |
| 459 | ** hwtime.h contains inline assembler code for implementing |
| 460 | ** high-performance timing routines. |
drh | 7b39686 | 2003-01-01 23:06:20 +0000 | [diff] [blame] | 461 | */ |
shane | 9bcbdad | 2008-05-29 20:22:37 +0000 | [diff] [blame] | 462 | #include "hwtime.h" |
| 463 | |
drh | 7b39686 | 2003-01-01 23:06:20 +0000 | [diff] [blame] | 464 | #endif |
| 465 | |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 466 | /* |
drh | caec2f1 | 2003-01-07 02:47:47 +0000 | [diff] [blame] | 467 | ** The CHECK_FOR_INTERRUPT macro defined here looks to see if the |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 468 | ** sqlite3_interrupt() routine has been called. If it has been, then |
drh | caec2f1 | 2003-01-07 02:47:47 +0000 | [diff] [blame] | 469 | ** processing of the VDBE program is interrupted. |
| 470 | ** |
| 471 | ** This macro added to every instruction that does a jump in order to |
| 472 | ** implement a loop. This test used to be on every single instruction, |
| 473 | ** but that meant we more testing that we needed. By only testing the |
| 474 | ** flag on jump instructions, we get a (small) speed improvement. |
| 475 | */ |
| 476 | #define CHECK_FOR_INTERRUPT \ |
drh | 881feaa | 2006-07-26 01:39:30 +0000 | [diff] [blame] | 477 | if( db->u1.isInterrupted ) goto abort_due_to_interrupt; |
drh | caec2f1 | 2003-01-07 02:47:47 +0000 | [diff] [blame] | 478 | |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 479 | #ifdef SQLITE_DEBUG |
| 480 | static int fileExists(sqlite3 *db, const char *zFile){ |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame] | 481 | int res = 0; |
| 482 | int rc = SQLITE_OK; |
| 483 | #ifdef SQLITE_TEST |
| 484 | /* If we are currently testing IO errors, then do not call OsAccess() to |
| 485 | ** test for the presence of zFile. This is because any IO error that |
| 486 | ** occurs here will not be reported, causing the test to fail. |
| 487 | */ |
| 488 | extern int sqlite3_io_error_pending; |
| 489 | if( sqlite3_io_error_pending<=0 ) |
| 490 | #endif |
| 491 | rc = sqlite3OsAccess(db->pVfs, zFile, SQLITE_ACCESS_EXISTS, &res); |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 492 | return (res && rc==SQLITE_OK); |
| 493 | } |
| 494 | #endif |
drh | caec2f1 | 2003-01-07 02:47:47 +0000 | [diff] [blame] | 495 | |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 496 | #ifndef NDEBUG |
| 497 | /* |
| 498 | ** This function is only called from within an assert() expression. It |
| 499 | ** checks that the sqlite3.nTransaction variable is correctly set to |
| 500 | ** the number of non-transaction savepoints currently in the |
| 501 | ** linked list starting at sqlite3.pSavepoint. |
| 502 | ** |
| 503 | ** Usage: |
| 504 | ** |
| 505 | ** assert( checkSavepointCount(db) ); |
| 506 | */ |
| 507 | static int checkSavepointCount(sqlite3 *db){ |
| 508 | int n = 0; |
| 509 | Savepoint *p; |
| 510 | for(p=db->pSavepoint; p; p=p->pNext) n++; |
| 511 | assert( n==(db->nSavepoint + db->isTransactionSavepoint) ); |
| 512 | return 1; |
| 513 | } |
| 514 | #endif |
| 515 | |
drh | caec2f1 | 2003-01-07 02:47:47 +0000 | [diff] [blame] | 516 | /* |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 517 | ** Execute as much of a VDBE program as we can then return. |
| 518 | ** |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 519 | ** sqlite3VdbeMakeReady() must be called before this routine in order to |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 520 | ** close the program with a final OP_Halt and to set up the callbacks |
| 521 | ** and the error message pointer. |
| 522 | ** |
| 523 | ** Whenever a row or result data is available, this routine will either |
| 524 | ** invoke the result callback (if there is one) or return with |
drh | 326dce7 | 2003-01-29 14:06:07 +0000 | [diff] [blame] | 525 | ** SQLITE_ROW. |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 526 | ** |
| 527 | ** If an attempt is made to open a locked database, then this routine |
| 528 | ** will either invoke the busy callback (if there is one) or it will |
| 529 | ** return SQLITE_BUSY. |
| 530 | ** |
| 531 | ** If an error occurs, an error message is written to memory obtained |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 532 | ** from sqlite3_malloc() and p->zErrMsg is made to point to that memory. |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 533 | ** The error code is stored in p->rc and this routine returns SQLITE_ERROR. |
| 534 | ** |
| 535 | ** If the callback ever returns non-zero, then the program exits |
| 536 | ** immediately. There will be no error message but the p->rc field is |
| 537 | ** set to SQLITE_ABORT and this routine will return SQLITE_ERROR. |
| 538 | ** |
drh | 9468c7f | 2003-03-07 19:50:07 +0000 | [diff] [blame] | 539 | ** A memory allocation error causes p->rc to be set to SQLITE_NOMEM and this |
| 540 | ** routine to return SQLITE_ERROR. |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 541 | ** |
| 542 | ** Other fatal errors return SQLITE_ERROR. |
| 543 | ** |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 544 | ** After this routine has finished, sqlite3VdbeFinalize() should be |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 545 | ** used to clean up the mess that was left behind. |
| 546 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 547 | int sqlite3VdbeExec( |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 548 | Vdbe *p /* The VDBE */ |
| 549 | ){ |
| 550 | int pc; /* The program counter */ |
| 551 | Op *pOp; /* Current operation */ |
| 552 | int rc = SQLITE_OK; /* Value to return */ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 553 | sqlite3 *db = p->db; /* The database */ |
drh | 8079a0d | 2006-01-12 17:20:50 +0000 | [diff] [blame] | 554 | u8 encoding = ENC(db); /* The database encoding */ |
drh | b27b7f5 | 2008-12-10 18:03:45 +0000 | [diff] [blame] | 555 | Mem *pIn1 = 0; /* 1st input operand */ |
| 556 | Mem *pIn2 = 0; /* 2nd input operand */ |
| 557 | Mem *pIn3 = 0; /* 3rd input operand */ |
| 558 | Mem *pOut = 0; /* Output operand */ |
drh | b1fdb2a | 2008-01-05 04:06:03 +0000 | [diff] [blame] | 559 | u8 opProperty; |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 560 | int iCompare = 0; /* Result of last OP_Compare operation */ |
shane | be21779 | 2009-03-05 04:20:31 +0000 | [diff] [blame] | 561 | int *aPermute = 0; /* Permutation of columns for OP_Compare */ |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 562 | #ifdef VDBE_PROFILE |
shane | 9bcbdad | 2008-05-29 20:22:37 +0000 | [diff] [blame] | 563 | u64 start; /* CPU clock count at start of opcode */ |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 564 | int origPc; /* Program counter at start of opcode */ |
| 565 | #endif |
danielk1977 | 348bb5d | 2003-10-18 09:37:26 +0000 | [diff] [blame] | 566 | #ifndef SQLITE_OMIT_PROGRESS_CALLBACK |
| 567 | int nProgressOps = 0; /* Opcodes executed since progress callback. */ |
| 568 | #endif |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 569 | /*** INSERT STACK UNION HERE ***/ |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 570 | |
drh | ca48c90 | 2008-01-18 14:08:24 +0000 | [diff] [blame] | 571 | assert( p->magic==VDBE_MAGIC_RUN ); /* sqlite3_step() verifies this */ |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 572 | assert( db->magic==SQLITE_MAGIC_BUSY ); |
danielk1977 | f7590db | 2009-04-10 12:55:16 +0000 | [diff] [blame] | 573 | sqlite3VdbeMutexArrayEnter(p); |
danielk1977 | 2e588c7 | 2005-12-09 14:25:08 +0000 | [diff] [blame] | 574 | if( p->rc==SQLITE_NOMEM ){ |
| 575 | /* This happens if a malloc() inside a call to sqlite3_column_text() or |
| 576 | ** sqlite3_column_text16() failed. */ |
| 577 | goto no_mem; |
| 578 | } |
drh | 3a84069 | 2003-01-29 22:58:26 +0000 | [diff] [blame] | 579 | assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY ); |
| 580 | p->rc = SQLITE_OK; |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 581 | assert( p->explain==0 ); |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 582 | p->pResultSet = 0; |
drh | a4afb65 | 2005-07-09 02:16:02 +0000 | [diff] [blame] | 583 | db->busyHandler.nBusy = 0; |
drh | 9358164 | 2004-02-12 13:02:55 +0000 | [diff] [blame] | 584 | CHECK_FOR_INTERRUPT; |
drh | 602c237 | 2007-03-01 00:29:13 +0000 | [diff] [blame] | 585 | sqlite3VdbeIOTraceSql(p); |
drh | 3c23a88 | 2007-01-09 14:01:13 +0000 | [diff] [blame] | 586 | #ifdef SQLITE_DEBUG |
danielk1977 | 2d1d86f | 2008-06-20 14:59:51 +0000 | [diff] [blame] | 587 | sqlite3BeginBenignMalloc(); |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 588 | if( p->pc==0 |
| 589 | && ((p->db->flags & SQLITE_VdbeListing) || fileExists(db, "vdbe_explain")) |
drh | 3c23a88 | 2007-01-09 14:01:13 +0000 | [diff] [blame] | 590 | ){ |
| 591 | int i; |
| 592 | printf("VDBE Program Listing:\n"); |
| 593 | sqlite3VdbePrintSql(p); |
| 594 | for(i=0; i<p->nOp; i++){ |
| 595 | sqlite3VdbePrintOp(stdout, i, &p->aOp[i]); |
| 596 | } |
| 597 | } |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 598 | if( fileExists(db, "vdbe_trace") ){ |
drh | 3c23a88 | 2007-01-09 14:01:13 +0000 | [diff] [blame] | 599 | p->trace = stdout; |
| 600 | } |
danielk1977 | 2d1d86f | 2008-06-20 14:59:51 +0000 | [diff] [blame] | 601 | sqlite3EndBenignMalloc(); |
drh | 3c23a88 | 2007-01-09 14:01:13 +0000 | [diff] [blame] | 602 | #endif |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 603 | for(pc=p->pc; rc==SQLITE_OK; pc++){ |
drh | caec2f1 | 2003-01-07 02:47:47 +0000 | [diff] [blame] | 604 | assert( pc>=0 && pc<p->nOp ); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 605 | if( db->mallocFailed ) goto no_mem; |
drh | 7b39686 | 2003-01-01 23:06:20 +0000 | [diff] [blame] | 606 | #ifdef VDBE_PROFILE |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 607 | origPc = pc; |
shane | 9bcbdad | 2008-05-29 20:22:37 +0000 | [diff] [blame] | 608 | start = sqlite3Hwtime(); |
drh | 7b39686 | 2003-01-01 23:06:20 +0000 | [diff] [blame] | 609 | #endif |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 610 | pOp = &p->aOp[pc]; |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 611 | |
danielk1977 | 8b60e0f | 2005-01-12 09:10:39 +0000 | [diff] [blame] | 612 | /* Only allow tracing if SQLITE_DEBUG is defined. |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 613 | */ |
danielk1977 | 8b60e0f | 2005-01-12 09:10:39 +0000 | [diff] [blame] | 614 | #ifdef SQLITE_DEBUG |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 615 | if( p->trace ){ |
drh | 3f7d4e4 | 2004-07-24 14:35:58 +0000 | [diff] [blame] | 616 | if( pc==0 ){ |
| 617 | printf("VDBE Execution Trace:\n"); |
| 618 | sqlite3VdbePrintSql(p); |
| 619 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 620 | sqlite3VdbePrintOp(p->trace, pc, pOp); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 621 | } |
drh | 19db935 | 2008-03-27 22:42:51 +0000 | [diff] [blame] | 622 | if( p->trace==0 && pc==0 ){ |
danielk1977 | 2d1d86f | 2008-06-20 14:59:51 +0000 | [diff] [blame] | 623 | sqlite3BeginBenignMalloc(); |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 624 | if( fileExists(db, "vdbe_sqltrace") ){ |
drh | 19db935 | 2008-03-27 22:42:51 +0000 | [diff] [blame] | 625 | sqlite3VdbePrintSql(p); |
| 626 | } |
danielk1977 | 2d1d86f | 2008-06-20 14:59:51 +0000 | [diff] [blame] | 627 | sqlite3EndBenignMalloc(); |
drh | 3f7d4e4 | 2004-07-24 14:35:58 +0000 | [diff] [blame] | 628 | } |
| 629 | #endif |
| 630 | |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 631 | |
drh | f603871 | 2004-02-08 18:07:34 +0000 | [diff] [blame] | 632 | /* Check to see if we need to simulate an interrupt. This only happens |
| 633 | ** if we have a special test build. |
| 634 | */ |
| 635 | #ifdef SQLITE_TEST |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 636 | if( sqlite3_interrupt_count>0 ){ |
| 637 | sqlite3_interrupt_count--; |
| 638 | if( sqlite3_interrupt_count==0 ){ |
| 639 | sqlite3_interrupt(db); |
drh | f603871 | 2004-02-08 18:07:34 +0000 | [diff] [blame] | 640 | } |
| 641 | } |
| 642 | #endif |
| 643 | |
danielk1977 | 348bb5d | 2003-10-18 09:37:26 +0000 | [diff] [blame] | 644 | #ifndef SQLITE_OMIT_PROGRESS_CALLBACK |
| 645 | /* Call the progress callback if it is configured and the required number |
| 646 | ** of VDBE ops have been executed (either since this invocation of |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 647 | ** sqlite3VdbeExec() or since last time the progress callback was called). |
danielk1977 | 348bb5d | 2003-10-18 09:37:26 +0000 | [diff] [blame] | 648 | ** If the progress callback returns non-zero, exit the virtual machine with |
| 649 | ** a return code SQLITE_ABORT. |
| 650 | */ |
drh | 3914aed | 2004-01-31 20:40:42 +0000 | [diff] [blame] | 651 | if( db->xProgress ){ |
| 652 | if( db->nProgressOps==nProgressOps ){ |
danielk1977 | de523ac | 2007-06-15 14:53:53 +0000 | [diff] [blame] | 653 | int prc; |
drh | f8888bb | 2006-05-26 19:57:19 +0000 | [diff] [blame] | 654 | if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse; |
danielk1977 | de523ac | 2007-06-15 14:53:53 +0000 | [diff] [blame] | 655 | prc =db->xProgress(db->pProgressArg); |
drh | f8888bb | 2006-05-26 19:57:19 +0000 | [diff] [blame] | 656 | if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse; |
danielk1977 | de523ac | 2007-06-15 14:53:53 +0000 | [diff] [blame] | 657 | if( prc!=0 ){ |
| 658 | rc = SQLITE_INTERRUPT; |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 659 | goto vdbe_error_halt; |
danielk1977 | de523ac | 2007-06-15 14:53:53 +0000 | [diff] [blame] | 660 | } |
danielk1977 | 3fe11f3 | 2007-06-13 16:49:48 +0000 | [diff] [blame] | 661 | nProgressOps = 0; |
danielk1977 | 348bb5d | 2003-10-18 09:37:26 +0000 | [diff] [blame] | 662 | } |
drh | 3914aed | 2004-01-31 20:40:42 +0000 | [diff] [blame] | 663 | nProgressOps++; |
danielk1977 | 348bb5d | 2003-10-18 09:37:26 +0000 | [diff] [blame] | 664 | } |
danielk1977 | 348bb5d | 2003-10-18 09:37:26 +0000 | [diff] [blame] | 665 | #endif |
| 666 | |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 667 | /* Do common setup processing for any opcode that is marked |
| 668 | ** with the "out2-prerelease" tag. Such opcodes have a single |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 669 | ** output which is specified by the P2 parameter. The P2 register |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 670 | ** is initialized to a NULL. |
| 671 | */ |
drh | b1fdb2a | 2008-01-05 04:06:03 +0000 | [diff] [blame] | 672 | opProperty = opcodeProperty[pOp->opcode]; |
| 673 | if( (opProperty & OPFLG_OUT2_PRERELEASE)!=0 ){ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 674 | assert( pOp->p2>0 ); |
| 675 | assert( pOp->p2<=p->nMem ); |
| 676 | pOut = &p->aMem[pOp->p2]; |
danielk1977 | 5f09613 | 2008-03-28 15:44:09 +0000 | [diff] [blame] | 677 | sqlite3VdbeMemReleaseExternal(pOut); |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 678 | pOut->flags = MEM_Null; |
drh | cdd0376 | 2009-05-07 12:17:33 +0000 | [diff] [blame] | 679 | pOut->n = 0; |
drh | b1fdb2a | 2008-01-05 04:06:03 +0000 | [diff] [blame] | 680 | }else |
| 681 | |
| 682 | /* Do common setup for opcodes marked with one of the following |
| 683 | ** combinations of properties. |
| 684 | ** |
| 685 | ** in1 |
| 686 | ** in1 in2 |
| 687 | ** in1 in2 out3 |
| 688 | ** in1 in3 |
drh | b1fdb2a | 2008-01-05 04:06:03 +0000 | [diff] [blame] | 689 | ** |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 690 | ** Variables pIn1, pIn2, and pIn3 are made to point to appropriate |
| 691 | ** registers for inputs. Variable pOut points to the output register. |
drh | b1fdb2a | 2008-01-05 04:06:03 +0000 | [diff] [blame] | 692 | */ |
| 693 | if( (opProperty & OPFLG_IN1)!=0 ){ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 694 | assert( pOp->p1>0 ); |
| 695 | assert( pOp->p1<=p->nMem ); |
| 696 | pIn1 = &p->aMem[pOp->p1]; |
| 697 | REGISTER_TRACE(pOp->p1, pIn1); |
drh | b1fdb2a | 2008-01-05 04:06:03 +0000 | [diff] [blame] | 698 | if( (opProperty & OPFLG_IN2)!=0 ){ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 699 | assert( pOp->p2>0 ); |
drh | aa9b896 | 2008-01-08 02:57:55 +0000 | [diff] [blame] | 700 | assert( pOp->p2<=p->nMem ); |
| 701 | pIn2 = &p->aMem[pOp->p2]; |
| 702 | REGISTER_TRACE(pOp->p2, pIn2); |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 703 | if( (opProperty & OPFLG_OUT3)!=0 ){ |
| 704 | assert( pOp->p3>0 ); |
| 705 | assert( pOp->p3<=p->nMem ); |
| 706 | pOut = &p->aMem[pOp->p3]; |
| 707 | } |
| 708 | }else if( (opProperty & OPFLG_IN3)!=0 ){ |
| 709 | assert( pOp->p3>0 ); |
drh | aa9b896 | 2008-01-08 02:57:55 +0000 | [diff] [blame] | 710 | assert( pOp->p3<=p->nMem ); |
| 711 | pIn3 = &p->aMem[pOp->p3]; |
| 712 | REGISTER_TRACE(pOp->p3, pIn3); |
| 713 | } |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 714 | }else if( (opProperty & OPFLG_IN2)!=0 ){ |
| 715 | assert( pOp->p2>0 ); |
| 716 | assert( pOp->p2<=p->nMem ); |
| 717 | pIn2 = &p->aMem[pOp->p2]; |
| 718 | REGISTER_TRACE(pOp->p2, pIn2); |
| 719 | }else if( (opProperty & OPFLG_IN3)!=0 ){ |
| 720 | assert( pOp->p3>0 ); |
| 721 | assert( pOp->p3<=p->nMem ); |
| 722 | pIn3 = &p->aMem[pOp->p3]; |
| 723 | REGISTER_TRACE(pOp->p3, pIn3); |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 724 | } |
| 725 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 726 | switch( pOp->opcode ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 727 | |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 728 | /***************************************************************************** |
| 729 | ** What follows is a massive switch statement where each case implements a |
| 730 | ** separate instruction in the virtual machine. If we follow the usual |
| 731 | ** indentation conventions, each case should be indented by 6 spaces. But |
| 732 | ** that is a lot of wasted space on the left margin. So the code within |
| 733 | ** the switch statement will break with convention and be flush-left. Another |
| 734 | ** big comment (similar to this one) will mark the point in the code where |
| 735 | ** we transition back to normal indentation. |
drh | ac82fcf | 2002-09-08 17:23:41 +0000 | [diff] [blame] | 736 | ** |
| 737 | ** The formatting of each case is important. The makefile for SQLite |
| 738 | ** generates two C files "opcodes.h" and "opcodes.c" by scanning this |
| 739 | ** file looking for lines that begin with "case OP_". The opcodes.h files |
| 740 | ** will be filled with #defines that give unique integer values to each |
| 741 | ** 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] | 742 | ** each string is the symbolic name for the corresponding opcode. If the |
| 743 | ** case statement is followed by a comment of the form "/# same as ... #/" |
| 744 | ** that comment is used to determine the particular value of the opcode. |
drh | ac82fcf | 2002-09-08 17:23:41 +0000 | [diff] [blame] | 745 | ** |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 746 | ** Other keywords in the comment that follows each case are used to |
| 747 | ** construct the OPFLG_INITIALIZER value that initializes opcodeProperty[]. |
| 748 | ** Keywords include: in1, in2, in3, out2_prerelease, out2, out3. See |
| 749 | ** the mkopcodeh.awk script for additional information. |
danielk1977 | bc04f85 | 2005-03-29 08:26:13 +0000 | [diff] [blame] | 750 | ** |
drh | ac82fcf | 2002-09-08 17:23:41 +0000 | [diff] [blame] | 751 | ** Documentation about VDBE opcodes is generated by scanning this file |
| 752 | ** for lines of that contain "Opcode:". That line and all subsequent |
| 753 | ** comment lines are used in the generation of the opcode.html documentation |
| 754 | ** file. |
| 755 | ** |
| 756 | ** SUMMARY: |
| 757 | ** |
| 758 | ** Formatting is important to scripts that scan this file. |
| 759 | ** Do not deviate from the formatting style currently in use. |
| 760 | ** |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 761 | *****************************************************************************/ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 762 | |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 763 | /* Opcode: Goto * P2 * * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 764 | ** |
| 765 | ** An unconditional jump to address P2. |
| 766 | ** The next instruction executed will be |
| 767 | ** the one at index P2 from the beginning of |
| 768 | ** the program. |
| 769 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 770 | case OP_Goto: { /* jump */ |
drh | caec2f1 | 2003-01-07 02:47:47 +0000 | [diff] [blame] | 771 | CHECK_FOR_INTERRUPT; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 772 | pc = pOp->p2 - 1; |
| 773 | break; |
| 774 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 775 | |
drh | 2eb9537 | 2008-06-06 15:04:36 +0000 | [diff] [blame] | 776 | /* Opcode: Gosub P1 P2 * * * |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 777 | ** |
drh | 2eb9537 | 2008-06-06 15:04:36 +0000 | [diff] [blame] | 778 | ** Write the current address onto register P1 |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 779 | ** and then jump to address P2. |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 780 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 781 | case OP_Gosub: { /* jump */ |
drh | 2eb9537 | 2008-06-06 15:04:36 +0000 | [diff] [blame] | 782 | assert( pOp->p1>0 ); |
| 783 | assert( pOp->p1<=p->nMem ); |
| 784 | pIn1 = &p->aMem[pOp->p1]; |
| 785 | assert( (pIn1->flags & MEM_Dyn)==0 ); |
| 786 | pIn1->flags = MEM_Int; |
| 787 | pIn1->u.i = pc; |
| 788 | REGISTER_TRACE(pOp->p1, pIn1); |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 789 | pc = pOp->p2 - 1; |
| 790 | break; |
| 791 | } |
| 792 | |
drh | 2eb9537 | 2008-06-06 15:04:36 +0000 | [diff] [blame] | 793 | /* Opcode: Return P1 * * * * |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 794 | ** |
drh | 2eb9537 | 2008-06-06 15:04:36 +0000 | [diff] [blame] | 795 | ** Jump to the next instruction after the address in register P1. |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 796 | */ |
drh | 2eb9537 | 2008-06-06 15:04:36 +0000 | [diff] [blame] | 797 | case OP_Return: { /* in1 */ |
| 798 | assert( pIn1->flags & MEM_Int ); |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 799 | pc = (int)pIn1->u.i; |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 800 | break; |
| 801 | } |
| 802 | |
drh | e00ee6e | 2008-06-20 15:24:01 +0000 | [diff] [blame] | 803 | /* Opcode: Yield P1 * * * * |
| 804 | ** |
| 805 | ** Swap the program counter with the value in register P1. |
| 806 | */ |
danielk1977 | f73ab8b | 2008-12-29 10:39:53 +0000 | [diff] [blame] | 807 | case OP_Yield: { /* in1 */ |
drh | e00ee6e | 2008-06-20 15:24:01 +0000 | [diff] [blame] | 808 | int pcDest; |
drh | e00ee6e | 2008-06-20 15:24:01 +0000 | [diff] [blame] | 809 | assert( (pIn1->flags & MEM_Dyn)==0 ); |
| 810 | pIn1->flags = MEM_Int; |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 811 | pcDest = (int)pIn1->u.i; |
drh | e00ee6e | 2008-06-20 15:24:01 +0000 | [diff] [blame] | 812 | pIn1->u.i = pc; |
| 813 | REGISTER_TRACE(pOp->p1, pIn1); |
| 814 | pc = pcDest; |
| 815 | break; |
| 816 | } |
| 817 | |
drh | 5053a79 | 2009-02-20 03:02:23 +0000 | [diff] [blame] | 818 | /* Opcode: HaltIfNull P1 P2 P3 P4 * |
| 819 | ** |
| 820 | ** Check the value in register P3. If is is NULL then Halt using |
| 821 | ** parameter P1, P2, and P4 as if this were a Halt instruction. If the |
| 822 | ** value in register P3 is not NULL, then this routine is a no-op. |
| 823 | */ |
| 824 | case OP_HaltIfNull: { /* in3 */ |
| 825 | if( (pIn3->flags & MEM_Null)==0 ) break; |
| 826 | /* Fall through into OP_Halt */ |
| 827 | } |
drh | e00ee6e | 2008-06-20 15:24:01 +0000 | [diff] [blame] | 828 | |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 829 | /* Opcode: Halt P1 P2 * P4 * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 830 | ** |
drh | 3d4501e | 2008-12-04 20:40:10 +0000 | [diff] [blame] | 831 | ** Exit immediately. All open cursors, etc are closed |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 832 | ** automatically. |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 833 | ** |
drh | 92f02c3 | 2004-09-02 14:57:08 +0000 | [diff] [blame] | 834 | ** P1 is the result code returned by sqlite3_exec(), sqlite3_reset(), |
| 835 | ** or sqlite3_finalize(). For a normal halt, this should be SQLITE_OK (0). |
| 836 | ** For errors, it can be some other value. If P1!=0 then P2 will determine |
| 837 | ** whether or not to rollback the current transaction. Do not rollback |
| 838 | ** if P2==OE_Fail. Do the rollback if P2==OE_Rollback. If P2==OE_Abort, |
| 839 | ** then back out all changes that have occurred during this execution of the |
drh | b798fa6 | 2002-09-03 19:43:23 +0000 | [diff] [blame] | 840 | ** VDBE, but do not rollback the transaction. |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 841 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 842 | ** If P4 is not null then it is an error message string. |
drh | 7f057c9 | 2005-06-24 03:53:06 +0000 | [diff] [blame] | 843 | ** |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 844 | ** 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] | 845 | ** every program. So a jump past the last instruction of the program |
| 846 | ** is the same as executing Halt. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 847 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 848 | case OP_Halt: { |
drh | 92f02c3 | 2004-09-02 14:57:08 +0000 | [diff] [blame] | 849 | p->rc = pOp->p1; |
| 850 | p->pc = pc; |
| 851 | p->errorAction = pOp->p2; |
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); |
danielk1977 | 01427a6 | 2005-01-11 13:02:33 +0000 | [diff] [blame] | 856 | assert( rc==SQLITE_BUSY || rc==SQLITE_OK ); |
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{ |
| 860 | rc = p->rc ? SQLITE_ERROR : SQLITE_DONE; |
drh | 92f02c3 | 2004-09-02 14:57:08 +0000 | [diff] [blame] | 861 | } |
drh | 900b31e | 2007-08-28 02:27:51 +0000 | [diff] [blame] | 862 | goto vdbe_return; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 863 | } |
drh | c61053b | 2000-06-04 12:58:36 +0000 | [diff] [blame] | 864 | |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 865 | /* Opcode: Integer P1 P2 * * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 866 | ** |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 867 | ** The 32-bit integer value P1 is written into register P2. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 868 | */ |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 869 | case OP_Integer: { /* out2-prerelease */ |
| 870 | pOut->flags = MEM_Int; |
| 871 | pOut->u.i = pOp->p1; |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 872 | break; |
| 873 | } |
| 874 | |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 875 | /* Opcode: Int64 * P2 * P4 * |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 876 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 877 | ** P4 is a pointer to a 64-bit integer value. |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 878 | ** Write that value into register P2. |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 879 | */ |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 880 | case OP_Int64: { /* out2-prerelease */ |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 881 | assert( pOp->p4.pI64!=0 ); |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 882 | pOut->flags = MEM_Int; |
| 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 | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 887 | /* Opcode: Real * P2 * P4 * |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 888 | ** |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 889 | ** P4 is a pointer to a 64-bit floating point value. |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 890 | ** Write that value into register P2. |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 891 | */ |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 892 | case OP_Real: { /* same as TK_FLOAT, out2-prerelease */ |
| 893 | pOut->flags = MEM_Real; |
drh | 2eaf93d | 2008-04-29 00:15:20 +0000 | [diff] [blame] | 894 | assert( !sqlite3IsNaN(*pOp->p4.pReal) ); |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 895 | pOut->r = *pOp->p4.pReal; |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 896 | break; |
| 897 | } |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 898 | |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 899 | /* Opcode: String8 * P2 * P4 * |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 900 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 901 | ** P4 points to a nul terminated UTF-8 string. This opcode is transformed |
danielk1977 | 0f69c1e | 2004-05-29 11:24:50 +0000 | [diff] [blame] | 902 | ** into an OP_String before it is executed for the first time. |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 903 | */ |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 904 | case OP_String8: { /* same as TK_STRING, out2-prerelease */ |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 905 | assert( pOp->p4.z!=0 ); |
drh | ed2df7f | 2005-11-16 04:34:32 +0000 | [diff] [blame] | 906 | pOp->opcode = OP_String; |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 907 | pOp->p1 = sqlite3Strlen30(pOp->p4.z); |
drh | ed2df7f | 2005-11-16 04:34:32 +0000 | [diff] [blame] | 908 | |
| 909 | #ifndef SQLITE_OMIT_UTF16 |
drh | 8079a0d | 2006-01-12 17:20:50 +0000 | [diff] [blame] | 910 | if( encoding!=SQLITE_UTF8 ){ |
drh | 3a9cf17 | 2009-06-17 21:42:33 +0000 | [diff] [blame] | 911 | rc = sqlite3VdbeMemSetStr(pOut, pOp->p4.z, -1, SQLITE_UTF8, SQLITE_STATIC); |
| 912 | if( rc==SQLITE_TOOBIG ) goto too_big; |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 913 | if( SQLITE_OK!=sqlite3VdbeChangeEncoding(pOut, encoding) ) goto no_mem; |
drh | 3a9cf17 | 2009-06-17 21:42:33 +0000 | [diff] [blame] | 914 | assert( pOut->zMalloc==pOut->z ); |
| 915 | assert( pOut->flags & MEM_Dyn ); |
danielk1977 | 5f09613 | 2008-03-28 15:44:09 +0000 | [diff] [blame] | 916 | pOut->zMalloc = 0; |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 917 | pOut->flags |= MEM_Static; |
drh | 191b54c | 2008-04-15 12:14:21 +0000 | [diff] [blame] | 918 | pOut->flags &= ~MEM_Dyn; |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 919 | if( pOp->p4type==P4_DYNAMIC ){ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 920 | sqlite3DbFree(db, pOp->p4.z); |
danielk1977 | e004840 | 2004-06-15 16:51:01 +0000 | [diff] [blame] | 921 | } |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 922 | pOp->p4type = P4_DYNAMIC; |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 923 | pOp->p4.z = pOut->z; |
| 924 | pOp->p1 = pOut->n; |
danielk1977 | 0f69c1e | 2004-05-29 11:24:50 +0000 | [diff] [blame] | 925 | } |
danielk1977 | 93758c8 | 2005-01-21 08:13:14 +0000 | [diff] [blame] | 926 | #endif |
drh | bb4957f | 2008-03-20 14:03:29 +0000 | [diff] [blame] | 927 | if( pOp->p1>db->aLimit[SQLITE_LIMIT_LENGTH] ){ |
drh | cbd2da9 | 2007-12-17 16:20:06 +0000 | [diff] [blame] | 928 | goto too_big; |
| 929 | } |
| 930 | /* Fall through to the next case, OP_String */ |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 931 | } |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 932 | |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 933 | /* Opcode: String P1 P2 * P4 * |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 934 | ** |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 935 | ** The string value P4 of length P1 (bytes) is stored in register P2. |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 936 | */ |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 937 | case OP_String: { /* out2-prerelease */ |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 938 | assert( pOp->p4.z!=0 ); |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 939 | pOut->flags = MEM_Str|MEM_Static|MEM_Term; |
| 940 | pOut->z = pOp->p4.z; |
| 941 | pOut->n = pOp->p1; |
| 942 | pOut->enc = encoding; |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 943 | UPDATE_MAX_BLOBSIZE(pOut); |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 944 | break; |
| 945 | } |
| 946 | |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 947 | /* Opcode: Null * P2 * * * |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 948 | ** |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 949 | ** Write a NULL into register P2. |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 950 | */ |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 951 | case OP_Null: { /* out2-prerelease */ |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 952 | break; |
| 953 | } |
| 954 | |
| 955 | |
drh | 9de221d | 2008-01-05 06:51:30 +0000 | [diff] [blame] | 956 | /* Opcode: Blob P1 P2 * P4 |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 957 | ** |
drh | 9de221d | 2008-01-05 06:51:30 +0000 | [diff] [blame] | 958 | ** P4 points to a blob of data P1 bytes long. Store this |
| 959 | ** blob in register P2. This instruction is not coded directly |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 960 | ** by the compiler. Instead, the compiler layer specifies |
| 961 | ** an OP_HexBlob opcode, with the hex string representation of |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 962 | ** the blob as P4. This opcode is transformed to an OP_Blob |
danielk1977 | 93758c8 | 2005-01-21 08:13:14 +0000 | [diff] [blame] | 963 | ** the first time it is executed. |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 964 | */ |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 965 | case OP_Blob: { /* out2-prerelease */ |
drh | cbd2da9 | 2007-12-17 16:20:06 +0000 | [diff] [blame] | 966 | assert( pOp->p1 <= SQLITE_MAX_LENGTH ); |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 967 | sqlite3VdbeMemSetStr(pOut, pOp->p4.z, pOp->p1, 0, 0); |
drh | 9de221d | 2008-01-05 06:51:30 +0000 | [diff] [blame] | 968 | pOut->enc = encoding; |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 969 | UPDATE_MAX_BLOBSIZE(pOut); |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 970 | break; |
| 971 | } |
| 972 | |
drh | 08de149 | 2009-02-20 03:55:05 +0000 | [diff] [blame] | 973 | /* Opcode: Variable P1 P2 P3 P4 * |
drh | 5045789 | 2003-09-06 01:10:47 +0000 | [diff] [blame] | 974 | ** |
drh | 08de149 | 2009-02-20 03:55:05 +0000 | [diff] [blame] | 975 | ** Transfer the values of bound parameters P1..P1+P3-1 into registers |
| 976 | ** P2..P2+P3-1. |
| 977 | ** |
| 978 | ** If the parameter is named, then its name appears in P4 and P3==1. |
| 979 | ** The P4 value is used by sqlite3_bind_parameter_name(). |
drh | 5045789 | 2003-09-06 01:10:47 +0000 | [diff] [blame] | 980 | */ |
drh | 08de149 | 2009-02-20 03:55:05 +0000 | [diff] [blame] | 981 | case OP_Variable: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 982 | int p1; /* Variable to copy from */ |
| 983 | int p2; /* Register to copy to */ |
| 984 | int n; /* Number of values left to copy */ |
| 985 | Mem *pVar; /* Value being transferred */ |
| 986 | |
| 987 | p1 = pOp->p1 - 1; |
| 988 | p2 = pOp->p2; |
| 989 | n = pOp->p3; |
| 990 | assert( p1>=0 && p1+n<=p->nVar ); |
| 991 | assert( p2>=1 && p2+n-1<=p->nMem ); |
drh | 08de149 | 2009-02-20 03:55:05 +0000 | [diff] [blame] | 992 | assert( pOp->p4.z==0 || pOp->p3==1 ); |
danielk1977 | 295ba55 | 2004-05-19 10:34:51 +0000 | [diff] [blame] | 993 | |
drh | 08de149 | 2009-02-20 03:55:05 +0000 | [diff] [blame] | 994 | while( n-- > 0 ){ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 995 | pVar = &p->aVar[p1++]; |
drh | 08de149 | 2009-02-20 03:55:05 +0000 | [diff] [blame] | 996 | if( sqlite3VdbeMemTooBig(pVar) ){ |
| 997 | goto too_big; |
| 998 | } |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 999 | pOut = &p->aMem[p2++]; |
drh | 08de149 | 2009-02-20 03:55:05 +0000 | [diff] [blame] | 1000 | sqlite3VdbeMemReleaseExternal(pOut); |
| 1001 | pOut->flags = MEM_Null; |
| 1002 | sqlite3VdbeMemShallowCopy(pOut, pVar, MEM_Static); |
| 1003 | UPDATE_MAX_BLOBSIZE(pOut); |
drh | 023ae03 | 2007-05-08 12:12:16 +0000 | [diff] [blame] | 1004 | } |
danielk1977 | 93d4675 | 2004-05-23 13:30:58 +0000 | [diff] [blame] | 1005 | break; |
| 1006 | } |
danielk1977 | 295ba55 | 2004-05-19 10:34:51 +0000 | [diff] [blame] | 1007 | |
drh | b21e7c7 | 2008-06-22 12:37:57 +0000 | [diff] [blame] | 1008 | /* Opcode: Move P1 P2 P3 * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1009 | ** |
drh | b21e7c7 | 2008-06-22 12:37:57 +0000 | [diff] [blame] | 1010 | ** Move the values in register P1..P1+P3-1 over into |
| 1011 | ** registers P2..P2+P3-1. Registers P1..P1+P1-1 are |
| 1012 | ** left holding a NULL. It is an error for register ranges |
| 1013 | ** P1..P1+P3-1 and P2..P2+P3-1 to overlap. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1014 | */ |
drh | e1349cb | 2008-04-01 00:36:10 +0000 | [diff] [blame] | 1015 | case OP_Move: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1016 | char *zMalloc; /* Holding variable for allocated memory */ |
| 1017 | int n; /* Number of registers left to copy */ |
| 1018 | int p1; /* Register to copy from */ |
| 1019 | int p2; /* Register to copy to */ |
| 1020 | |
| 1021 | n = pOp->p3; |
| 1022 | p1 = pOp->p1; |
| 1023 | p2 = pOp->p2; |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1024 | assert( n>0 && p1>0 && p2>0 ); |
drh | b21e7c7 | 2008-06-22 12:37:57 +0000 | [diff] [blame] | 1025 | assert( p1+n<=p2 || p2+n<=p1 ); |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1026 | |
| 1027 | pIn1 = &p->aMem[p1]; |
| 1028 | pOut = &p->aMem[p2]; |
drh | b21e7c7 | 2008-06-22 12:37:57 +0000 | [diff] [blame] | 1029 | while( n-- ){ |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1030 | assert( pOut<=&p->aMem[p->nMem] ); |
| 1031 | assert( pIn1<=&p->aMem[p->nMem] ); |
drh | b21e7c7 | 2008-06-22 12:37:57 +0000 | [diff] [blame] | 1032 | zMalloc = pOut->zMalloc; |
| 1033 | pOut->zMalloc = 0; |
| 1034 | sqlite3VdbeMemMove(pOut, pIn1); |
| 1035 | pIn1->zMalloc = zMalloc; |
| 1036 | REGISTER_TRACE(p2++, pOut); |
| 1037 | pIn1++; |
| 1038 | pOut++; |
| 1039 | } |
drh | e1349cb | 2008-04-01 00:36:10 +0000 | [diff] [blame] | 1040 | break; |
| 1041 | } |
| 1042 | |
drh | b1fdb2a | 2008-01-05 04:06:03 +0000 | [diff] [blame] | 1043 | /* Opcode: Copy P1 P2 * * * |
| 1044 | ** |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1045 | ** Make a copy of register P1 into register P2. |
drh | b1fdb2a | 2008-01-05 04:06:03 +0000 | [diff] [blame] | 1046 | ** |
| 1047 | ** This instruction makes a deep copy of the value. A duplicate |
| 1048 | ** is made of any string or blob constant. See also OP_SCopy. |
| 1049 | */ |
danielk1977 | f73ab8b | 2008-12-29 10:39:53 +0000 | [diff] [blame] | 1050 | case OP_Copy: { /* in1 */ |
drh | e1349cb | 2008-04-01 00:36:10 +0000 | [diff] [blame] | 1051 | assert( pOp->p2>0 ); |
| 1052 | assert( pOp->p2<=p->nMem ); |
| 1053 | pOut = &p->aMem[pOp->p2]; |
| 1054 | assert( pOut!=pIn1 ); |
| 1055 | sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem); |
| 1056 | Deephemeralize(pOut); |
| 1057 | REGISTER_TRACE(pOp->p2, pOut); |
| 1058 | break; |
| 1059 | } |
| 1060 | |
drh | b1fdb2a | 2008-01-05 04:06:03 +0000 | [diff] [blame] | 1061 | /* Opcode: SCopy P1 P2 * * * |
| 1062 | ** |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1063 | ** Make a shallow copy of register P1 into register P2. |
drh | b1fdb2a | 2008-01-05 04:06:03 +0000 | [diff] [blame] | 1064 | ** |
| 1065 | ** This instruction makes a shallow copy of the value. If the value |
| 1066 | ** is a string or blob, then the copy is only a pointer to the |
| 1067 | ** original and hence if the original changes so will the copy. |
| 1068 | ** Worse, if the original is deallocated, the copy becomes invalid. |
| 1069 | ** Thus the program must guarantee that the original will not change |
| 1070 | ** during the lifetime of the copy. Use OP_Copy to make a complete |
| 1071 | ** copy. |
| 1072 | */ |
danielk1977 | f73ab8b | 2008-12-29 10:39:53 +0000 | [diff] [blame] | 1073 | case OP_SCopy: { /* in1 */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1074 | REGISTER_TRACE(pOp->p1, pIn1); |
| 1075 | assert( pOp->p2>0 ); |
| 1076 | assert( pOp->p2<=p->nMem ); |
| 1077 | pOut = &p->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 | |
danielk1977 | bd43455 | 2009-03-18 10:33:00 +0000 | [diff] [blame] | 1099 | /* If the SQLITE_CountRows flag is set in sqlite3.flags mask, then |
| 1100 | ** DML statements invoke this opcode to return the number of rows |
| 1101 | ** modified to the user. This is the only way that a VM that |
| 1102 | ** opens a statement transaction may invoke this opcode. |
| 1103 | ** |
| 1104 | ** In case this is such a statement, close any statement transaction |
| 1105 | ** opened by this VM before returning control to the user. This is to |
| 1106 | ** ensure that statement-transactions are always nested, not overlapping. |
| 1107 | ** If the open statement-transaction is not closed here, then the user |
| 1108 | ** may step another VM that opens its own statement transaction. This |
| 1109 | ** may lead to overlapping statement transactions. |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 1110 | ** |
| 1111 | ** The statement transaction is never a top-level transaction. Hence |
| 1112 | ** the RELEASE call below can never fail. |
danielk1977 | bd43455 | 2009-03-18 10:33:00 +0000 | [diff] [blame] | 1113 | */ |
| 1114 | assert( p->iStatement==0 || db->flags&SQLITE_CountRows ); |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 1115 | rc = sqlite3VdbeCloseStatement(p, SAVEPOINT_RELEASE); |
| 1116 | if( NEVER(rc!=SQLITE_OK) ){ |
danielk1977 | bd43455 | 2009-03-18 10:33:00 +0000 | [diff] [blame] | 1117 | break; |
| 1118 | } |
| 1119 | |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 1120 | /* Invalidate all ephemeral cursor row caches */ |
| 1121 | p->cacheCtr = (p->cacheCtr + 2)|1; |
| 1122 | |
| 1123 | /* Make sure the results of the current row are \000 terminated |
shane | 21e7feb | 2008-05-30 15:59:49 +0000 | [diff] [blame] | 1124 | ** and have an assigned type. The results are de-ephemeralized as |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 1125 | ** as side effect. |
| 1126 | */ |
| 1127 | pMem = p->pResultSet = &p->aMem[pOp->p1]; |
| 1128 | for(i=0; i<pOp->p2; i++){ |
| 1129 | sqlite3VdbeMemNulTerminate(&pMem[i]); |
| 1130 | storeTypeInfo(&pMem[i], encoding); |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 1131 | REGISTER_TRACE(pOp->p1+i, &pMem[i]); |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 1132 | } |
drh | 2803969 | 2008-03-17 16:54:01 +0000 | [diff] [blame] | 1133 | if( db->mallocFailed ) goto no_mem; |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 1134 | |
| 1135 | /* Return SQLITE_ROW |
| 1136 | */ |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 1137 | p->pc = pc + 1; |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 1138 | rc = SQLITE_ROW; |
| 1139 | goto vdbe_return; |
| 1140 | } |
| 1141 | |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1142 | /* Opcode: Concat P1 P2 P3 * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1143 | ** |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1144 | ** Add the text in register P1 onto the end of the text in |
| 1145 | ** register P2 and store the result in register P3. |
| 1146 | ** 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] | 1147 | ** |
| 1148 | ** P3 = P2 || P1 |
| 1149 | ** |
| 1150 | ** It is illegal for P1 and P3 to be the same register. Sometimes, |
| 1151 | ** if P3 is the same register as P2, the implementation is able |
| 1152 | ** to avoid a memcpy(). |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1153 | */ |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1154 | case OP_Concat: { /* same as TK_CONCAT, in1, in2, out3 */ |
drh | 023ae03 | 2007-05-08 12:12:16 +0000 | [diff] [blame] | 1155 | i64 nByte; |
danielk1977 | 8a6b541 | 2004-05-24 07:04:25 +0000 | [diff] [blame] | 1156 | |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1157 | assert( pIn1!=pOut ); |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1158 | if( (pIn1->flags | pIn2->flags) & MEM_Null ){ |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1159 | sqlite3VdbeMemSetNull(pOut); |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1160 | break; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1161 | } |
drh | a0c0652 | 2009-06-17 22:50:41 +0000 | [diff] [blame] | 1162 | if( ExpandBlob(pIn1) || ExpandBlob(pIn2) ) goto no_mem; |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1163 | Stringify(pIn1, encoding); |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1164 | Stringify(pIn2, encoding); |
| 1165 | nByte = pIn1->n + pIn2->n; |
drh | bb4957f | 2008-03-20 14:03:29 +0000 | [diff] [blame] | 1166 | if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){ |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1167 | goto too_big; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1168 | } |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1169 | MemSetTypeFlag(pOut, MEM_Str); |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 1170 | if( sqlite3VdbeMemGrow(pOut, (int)nByte+2, pOut==pIn2) ){ |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1171 | goto no_mem; |
| 1172 | } |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1173 | if( pOut!=pIn2 ){ |
| 1174 | memcpy(pOut->z, pIn2->z, pIn2->n); |
| 1175 | } |
| 1176 | memcpy(&pOut->z[pIn2->n], pIn1->z, pIn1->n); |
| 1177 | pOut->z[nByte] = 0; |
| 1178 | pOut->z[nByte+1] = 0; |
| 1179 | pOut->flags |= MEM_Term; |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 1180 | pOut->n = (int)nByte; |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1181 | pOut->enc = encoding; |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 1182 | UPDATE_MAX_BLOBSIZE(pOut); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1183 | break; |
| 1184 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1185 | |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 1186 | /* Opcode: Add P1 P2 P3 * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1187 | ** |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1188 | ** Add the value in register P1 to the value in register P2 |
shane | 21e7feb | 2008-05-30 15:59:49 +0000 | [diff] [blame] | 1189 | ** and store the result in register P3. |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1190 | ** If either input is NULL, the result is NULL. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1191 | */ |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 1192 | /* Opcode: Multiply P1 P2 P3 * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1193 | ** |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 1194 | ** |
shane | 21e7feb | 2008-05-30 15:59:49 +0000 | [diff] [blame] | 1195 | ** Multiply the value in register P1 by the value in register P2 |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1196 | ** and store the result in register P3. |
| 1197 | ** If either input is NULL, the result is NULL. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1198 | */ |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 1199 | /* Opcode: Subtract P1 P2 P3 * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1200 | ** |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1201 | ** Subtract the value in register P1 from the value in register P2 |
| 1202 | ** and store the result in register P3. |
| 1203 | ** If either input is NULL, the result is NULL. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1204 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1205 | /* Opcode: Divide P1 P2 P3 * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1206 | ** |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1207 | ** Divide the value in register P1 by the value in register P2 |
| 1208 | ** and store the result in register P3. If the value in register P2 |
| 1209 | ** is zero, then the result is NULL. |
| 1210 | ** If either input is NULL, the result is NULL. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1211 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1212 | /* Opcode: Remainder P1 P2 P3 * * |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1213 | ** |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 1214 | ** Compute the remainder after integer division of the value in |
| 1215 | ** register P1 by the value in register P2 and store the result in P3. |
| 1216 | ** If the value in register P2 is zero the result is NULL. |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1217 | ** If either operand is NULL, the result is NULL. |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1218 | */ |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1219 | case OP_Add: /* same as TK_PLUS, in1, in2, out3 */ |
| 1220 | case OP_Subtract: /* same as TK_MINUS, in1, in2, out3 */ |
| 1221 | case OP_Multiply: /* same as TK_STAR, in1, in2, out3 */ |
| 1222 | case OP_Divide: /* same as TK_SLASH, in1, in2, out3 */ |
| 1223 | case OP_Remainder: { /* same as TK_REM, in1, in2, out3 */ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1224 | int flags; /* Combined MEM_* flags from both inputs */ |
| 1225 | i64 iA; /* Integer value of left operand */ |
| 1226 | i64 iB; /* Integer value of right operand */ |
| 1227 | double rA; /* Real value of left operand */ |
| 1228 | double rB; /* Real value of right operand */ |
| 1229 | |
drh | 61669b3 | 2008-07-30 13:27:10 +0000 | [diff] [blame] | 1230 | applyNumericAffinity(pIn1); |
| 1231 | applyNumericAffinity(pIn2); |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1232 | flags = pIn1->flags | pIn2->flags; |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 1233 | if( (flags & MEM_Null)!=0 ) goto arithmetic_result_is_null; |
| 1234 | if( (pIn1->flags & pIn2->flags & MEM_Int)==MEM_Int ){ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1235 | iA = pIn1->u.i; |
| 1236 | iB = pIn2->u.i; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1237 | switch( pOp->opcode ){ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1238 | case OP_Add: iB += iA; break; |
| 1239 | case OP_Subtract: iB -= iA; break; |
| 1240 | case OP_Multiply: iB *= iA; break; |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1241 | case OP_Divide: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1242 | if( iA==0 ) goto arithmetic_result_is_null; |
danielk1977 | 42d4ef2 | 2007-06-26 11:13:25 +0000 | [diff] [blame] | 1243 | /* Dividing the largest possible negative 64-bit integer (1<<63) by |
drh | 0f05035 | 2008-05-09 18:03:13 +0000 | [diff] [blame] | 1244 | ** -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] | 1245 | ** some architectures, the value overflows to (1<<63). On others, |
| 1246 | ** a SIGFPE is issued. The following statement normalizes this |
shane | 21e7feb | 2008-05-30 15:59:49 +0000 | [diff] [blame] | 1247 | ** behavior so that all architectures behave as if integer |
| 1248 | ** overflow occurred. |
danielk1977 | 42d4ef2 | 2007-06-26 11:13:25 +0000 | [diff] [blame] | 1249 | */ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1250 | if( iA==-1 && iB==SMALLEST_INT64 ) iA = 1; |
| 1251 | iB /= iA; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1252 | break; |
| 1253 | } |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1254 | default: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1255 | if( iA==0 ) goto arithmetic_result_is_null; |
| 1256 | if( iA==-1 ) iA = 1; |
| 1257 | iB %= iA; |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1258 | break; |
| 1259 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1260 | } |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1261 | pOut->u.i = iB; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1262 | MemSetTypeFlag(pOut, MEM_Int); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1263 | }else{ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1264 | rA = sqlite3VdbeRealValue(pIn1); |
| 1265 | rB = sqlite3VdbeRealValue(pIn2); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1266 | switch( pOp->opcode ){ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1267 | case OP_Add: rB += rA; break; |
| 1268 | case OP_Subtract: rB -= rA; break; |
| 1269 | case OP_Multiply: rB *= rA; break; |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1270 | case OP_Divide: { |
shane | fbd60f8 | 2009-02-04 03:59:25 +0000 | [diff] [blame] | 1271 | /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1272 | if( rA==(double)0 ) goto arithmetic_result_is_null; |
| 1273 | rB /= rA; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1274 | break; |
| 1275 | } |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1276 | default: { |
shane | 75ac1de | 2009-06-09 18:58:52 +0000 | [diff] [blame] | 1277 | iA = (i64)rA; |
| 1278 | iB = (i64)rB; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1279 | if( iA==0 ) goto arithmetic_result_is_null; |
| 1280 | if( iA==-1 ) iA = 1; |
| 1281 | rB = (double)(iB % iA); |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1282 | break; |
| 1283 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1284 | } |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1285 | if( sqlite3IsNaN(rB) ){ |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 1286 | goto arithmetic_result_is_null; |
drh | 53c1402 | 2007-05-10 17:23:11 +0000 | [diff] [blame] | 1287 | } |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1288 | pOut->r = rB; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1289 | MemSetTypeFlag(pOut, MEM_Real); |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1290 | if( (flags & MEM_Real)==0 ){ |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1291 | sqlite3VdbeIntegerAffinity(pOut); |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1292 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1293 | } |
| 1294 | break; |
| 1295 | |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 1296 | arithmetic_result_is_null: |
| 1297 | sqlite3VdbeMemSetNull(pOut); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1298 | break; |
| 1299 | } |
| 1300 | |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 1301 | /* Opcode: CollSeq * * P4 |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 1302 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 1303 | ** 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] | 1304 | ** or aggregate calls sqlite3GetFuncCollSeq(), this collation sequence will |
| 1305 | ** be returned. This is used by the built-in min(), max() and nullif() |
drh | e6f85e7 | 2004-12-25 01:03:13 +0000 | [diff] [blame] | 1306 | ** functions. |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 1307 | ** |
| 1308 | ** The interface used by the implementation of the aforementioned functions |
| 1309 | ** to retrieve the collation sequence set by this opcode is not available |
| 1310 | ** publicly, only to user functions defined in func.c. |
| 1311 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1312 | case OP_CollSeq: { |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 1313 | assert( pOp->p4type==P4_COLLSEQ ); |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 1314 | break; |
| 1315 | } |
| 1316 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1317 | /* Opcode: Function P1 P2 P3 P4 P5 |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 1318 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 1319 | ** Invoke a user function (P4 is a pointer to a Function structure that |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1320 | ** defines the function) with P5 arguments taken from register P2 and |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1321 | ** successors. The result of the function is stored in register P3. |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1322 | ** Register P3 must not be one of the function inputs. |
danielk1977 | 682f68b | 2004-06-05 10:22:17 +0000 | [diff] [blame] | 1323 | ** |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 1324 | ** 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] | 1325 | ** function was determined to be constant at compile time. If the first |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 1326 | ** 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] | 1327 | ** whether meta data associated with a user function argument using the |
| 1328 | ** sqlite3_set_auxdata() API may be safely retained until the next |
| 1329 | ** invocation of this opcode. |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 1330 | ** |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 1331 | ** See also: AggStep and AggFinal |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 1332 | */ |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 1333 | case OP_Function: { |
danielk1977 | 51ad0ec | 2004-05-24 12:39:02 +0000 | [diff] [blame] | 1334 | int i; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1335 | Mem *pArg; |
danielk1977 | 22322fd | 2004-05-25 23:35:17 +0000 | [diff] [blame] | 1336 | sqlite3_context ctx; |
danielk1977 | 51ad0ec | 2004-05-24 12:39:02 +0000 | [diff] [blame] | 1337 | sqlite3_value **apVal; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1338 | int n; |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 1339 | |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1340 | n = pOp->p5; |
danielk1977 | 6ddcca5 | 2004-05-24 23:48:25 +0000 | [diff] [blame] | 1341 | apVal = p->apArg; |
danielk1977 | 51ad0ec | 2004-05-24 12:39:02 +0000 | [diff] [blame] | 1342 | assert( apVal || n==0 ); |
| 1343 | |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1344 | assert( n==0 || (pOp->p2>0 && pOp->p2+n<=p->nMem+1) ); |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1345 | assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n ); |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1346 | pArg = &p->aMem[pOp->p2]; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1347 | for(i=0; i<n; i++, pArg++){ |
danielk1977 | 51ad0ec | 2004-05-24 12:39:02 +0000 | [diff] [blame] | 1348 | apVal[i] = pArg; |
drh | 8079a0d | 2006-01-12 17:20:50 +0000 | [diff] [blame] | 1349 | storeTypeInfo(pArg, encoding); |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 1350 | REGISTER_TRACE(pOp->p2, pArg); |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 1351 | } |
danielk1977 | 51ad0ec | 2004-05-24 12:39:02 +0000 | [diff] [blame] | 1352 | |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 1353 | assert( pOp->p4type==P4_FUNCDEF || pOp->p4type==P4_VDBEFUNC ); |
| 1354 | if( pOp->p4type==P4_FUNCDEF ){ |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 1355 | ctx.pFunc = pOp->p4.pFunc; |
danielk1977 | 682f68b | 2004-06-05 10:22:17 +0000 | [diff] [blame] | 1356 | ctx.pVdbeFunc = 0; |
| 1357 | }else{ |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 1358 | ctx.pVdbeFunc = (VdbeFunc*)pOp->p4.pVdbeFunc; |
danielk1977 | 682f68b | 2004-06-05 10:22:17 +0000 | [diff] [blame] | 1359 | ctx.pFunc = ctx.pVdbeFunc->pFunc; |
| 1360 | } |
| 1361 | |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1362 | assert( pOp->p3>0 && pOp->p3<=p->nMem ); |
| 1363 | pOut = &p->aMem[pOp->p3]; |
drh | 00706be | 2004-01-30 14:49:16 +0000 | [diff] [blame] | 1364 | ctx.s.flags = MEM_Null; |
drh | fa4a4b9 | 2008-03-19 21:45:51 +0000 | [diff] [blame] | 1365 | ctx.s.db = db; |
danielk1977 | 5f09613 | 2008-03-28 15:44:09 +0000 | [diff] [blame] | 1366 | ctx.s.xDel = 0; |
| 1367 | ctx.s.zMalloc = 0; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1368 | |
| 1369 | /* The output cell may already have a buffer allocated. Move |
| 1370 | ** the pointer to ctx.s so in case the user-function can use |
| 1371 | ** the already allocated buffer instead of allocating a new one. |
| 1372 | */ |
| 1373 | sqlite3VdbeMemMove(&ctx.s, pOut); |
| 1374 | MemSetTypeFlag(&ctx.s, MEM_Null); |
| 1375 | |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 1376 | ctx.isError = 0; |
drh | e82f5d0 | 2008-10-07 19:53:14 +0000 | [diff] [blame] | 1377 | if( ctx.pFunc->flags & SQLITE_FUNC_NEEDCOLL ){ |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 1378 | assert( pOp>p->aOp ); |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 1379 | assert( pOp[-1].p4type==P4_COLLSEQ ); |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 1380 | assert( pOp[-1].opcode==OP_CollSeq ); |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 1381 | ctx.pColl = pOp[-1].p4.pColl; |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 1382 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1383 | if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse; |
danielk1977 | 51ad0ec | 2004-05-24 12:39:02 +0000 | [diff] [blame] | 1384 | (*ctx.pFunc->xFunc)(&ctx, n, apVal); |
danielk1977 | 75eb016 | 2008-03-28 19:16:33 +0000 | [diff] [blame] | 1385 | if( sqlite3SafetyOn(db) ){ |
| 1386 | sqlite3VdbeMemRelease(&ctx.s); |
| 1387 | goto abort_due_to_misuse; |
| 1388 | } |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 1389 | if( db->mallocFailed ){ |
danielk1977 | e0fc526 | 2007-07-26 06:50:05 +0000 | [diff] [blame] | 1390 | /* Even though a malloc() has failed, the implementation of the |
| 1391 | ** user function may have called an sqlite3_result_XXX() function |
| 1392 | ** to return a value. The following call releases any resources |
| 1393 | ** associated with such a value. |
| 1394 | ** |
| 1395 | ** Note: Maybe MemRelease() should be called if sqlite3SafetyOn() |
| 1396 | ** fails also (the if(...) statement above). But if people are |
| 1397 | ** misusing sqlite, they have bigger problems than a leaked value. |
| 1398 | */ |
| 1399 | sqlite3VdbeMemRelease(&ctx.s); |
| 1400 | goto no_mem; |
| 1401 | } |
danielk1977 | 7e18c25 | 2004-05-25 11:47:24 +0000 | [diff] [blame] | 1402 | |
shane | 21e7feb | 2008-05-30 15:59:49 +0000 | [diff] [blame] | 1403 | /* If any auxiliary data functions have been called by this user function, |
danielk1977 | 682f68b | 2004-06-05 10:22:17 +0000 | [diff] [blame] | 1404 | ** immediately call the destructor for any non-static values. |
| 1405 | */ |
| 1406 | if( ctx.pVdbeFunc ){ |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 1407 | sqlite3VdbeDeleteAuxData(ctx.pVdbeFunc, pOp->p1); |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 1408 | pOp->p4.pVdbeFunc = ctx.pVdbeFunc; |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 1409 | pOp->p4type = P4_VDBEFUNC; |
danielk1977 | 682f68b | 2004-06-05 10:22:17 +0000 | [diff] [blame] | 1410 | } |
| 1411 | |
drh | 90669c1 | 2006-01-20 15:45:36 +0000 | [diff] [blame] | 1412 | /* If the function returned an error, throw an exception */ |
| 1413 | if( ctx.isError ){ |
drh | f089aa4 | 2008-07-08 19:34:06 +0000 | [diff] [blame] | 1414 | sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(&ctx.s)); |
drh | 69544ec | 2008-02-06 14:11:34 +0000 | [diff] [blame] | 1415 | rc = ctx.isError; |
drh | 90669c1 | 2006-01-20 15:45:36 +0000 | [diff] [blame] | 1416 | } |
| 1417 | |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1418 | /* Copy the result of the function into register P3 */ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 1419 | sqlite3VdbeChangeEncoding(&ctx.s, encoding); |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1420 | sqlite3VdbeMemMove(pOut, &ctx.s); |
| 1421 | if( sqlite3VdbeMemTooBig(pOut) ){ |
drh | 023ae03 | 2007-05-08 12:12:16 +0000 | [diff] [blame] | 1422 | goto too_big; |
| 1423 | } |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 1424 | REGISTER_TRACE(pOp->p3, pOut); |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 1425 | UPDATE_MAX_BLOBSIZE(pOut); |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 1426 | break; |
| 1427 | } |
| 1428 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1429 | /* Opcode: BitAnd P1 P2 P3 * * |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1430 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1431 | ** Take the bit-wise AND of the values in register P1 and P2 and |
| 1432 | ** store the result in register P3. |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1433 | ** If either input is NULL, the result is NULL. |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1434 | */ |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1435 | /* Opcode: BitOr P1 P2 P3 * * |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1436 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1437 | ** Take the bit-wise OR of the values in register P1 and P2 and |
| 1438 | ** store the result in register P3. |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1439 | ** If either input is NULL, the result is NULL. |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1440 | */ |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1441 | /* Opcode: ShiftLeft P1 P2 P3 * * |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1442 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1443 | ** Shift the integer value in register P2 to the left by the |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1444 | ** number of bits specified by the integer in regiser P1. |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1445 | ** Store the result in register P3. |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1446 | ** If either input is NULL, the result is NULL. |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1447 | */ |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1448 | /* Opcode: ShiftRight P1 P2 P3 * * |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1449 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1450 | ** Shift the integer value in register P2 to the right by the |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1451 | ** number of bits specified by the integer in register P1. |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 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 | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1455 | case OP_BitAnd: /* same as TK_BITAND, in1, in2, out3 */ |
| 1456 | case OP_BitOr: /* same as TK_BITOR, in1, in2, out3 */ |
| 1457 | case OP_ShiftLeft: /* same as TK_LSHIFT, in1, in2, out3 */ |
| 1458 | case OP_ShiftRight: { /* same as TK_RSHIFT, in1, in2, out3 */ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1459 | i64 a; |
| 1460 | i64 b; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1461 | |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1462 | if( (pIn1->flags | pIn2->flags) & MEM_Null ){ |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 1463 | sqlite3VdbeMemSetNull(pOut); |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1464 | break; |
| 1465 | } |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1466 | a = sqlite3VdbeIntValue(pIn2); |
| 1467 | b = sqlite3VdbeIntValue(pIn1); |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1468 | switch( pOp->opcode ){ |
| 1469 | case OP_BitAnd: a &= b; break; |
| 1470 | case OP_BitOr: a |= b; break; |
| 1471 | case OP_ShiftLeft: a <<= b; break; |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 1472 | default: assert( pOp->opcode==OP_ShiftRight ); |
| 1473 | a >>= b; break; |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1474 | } |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1475 | pOut->u.i = a; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1476 | MemSetTypeFlag(pOut, MEM_Int); |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1477 | break; |
| 1478 | } |
| 1479 | |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1480 | /* Opcode: AddImm P1 P2 * * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1481 | ** |
danielk1977 | 0cdc022 | 2008-06-26 18:04:03 +0000 | [diff] [blame] | 1482 | ** Add the constant P2 to the value in register P1. |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1483 | ** The result is always an integer. |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 1484 | ** |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1485 | ** To force any register to be an integer, just add 0. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1486 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1487 | case OP_AddImm: { /* in1 */ |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1488 | sqlite3VdbeMemIntegerify(pIn1); |
| 1489 | pIn1->u.i += pOp->p2; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1490 | break; |
| 1491 | } |
| 1492 | |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1493 | /* Opcode: MustBeInt P1 P2 * * * |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 1494 | ** |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1495 | ** Force the value in register P1 to be an integer. If the value |
| 1496 | ** in P1 is not an integer and cannot be converted into an integer |
danielk1977 | 9a96b66 | 2007-11-29 17:05:18 +0000 | [diff] [blame] | 1497 | ** without data loss, then jump immediately to P2, or if P2==0 |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 1498 | ** raise an SQLITE_MISMATCH exception. |
| 1499 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1500 | case OP_MustBeInt: { /* jump, in1 */ |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 1501 | applyAffinity(pIn1, SQLITE_AFF_NUMERIC, encoding); |
| 1502 | if( (pIn1->flags & MEM_Int)==0 ){ |
drh | 17c4029 | 2004-07-21 02:53:29 +0000 | [diff] [blame] | 1503 | if( pOp->p2==0 ){ |
| 1504 | rc = SQLITE_MISMATCH; |
| 1505 | goto abort_due_to_error; |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 1506 | }else{ |
drh | 17c4029 | 2004-07-21 02:53:29 +0000 | [diff] [blame] | 1507 | pc = pOp->p2 - 1; |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 1508 | } |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 1509 | }else{ |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1510 | MemSetTypeFlag(pIn1, MEM_Int); |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 1511 | } |
| 1512 | break; |
| 1513 | } |
| 1514 | |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1515 | /* Opcode: RealAffinity P1 * * * * |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 1516 | ** |
drh | 2133d82 | 2008-01-03 18:44:59 +0000 | [diff] [blame] | 1517 | ** If register P1 holds an integer convert it to a real value. |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 1518 | ** |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1519 | ** This opcode is used when extracting information from a column that |
| 1520 | ** has REAL affinity. Such column values may still be stored as |
| 1521 | ** integers, for space efficiency, but after extraction we want them |
| 1522 | ** to have only a real value. |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 1523 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1524 | case OP_RealAffinity: { /* in1 */ |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1525 | if( pIn1->flags & MEM_Int ){ |
| 1526 | sqlite3VdbeMemRealify(pIn1); |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1527 | } |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 1528 | break; |
| 1529 | } |
| 1530 | |
drh | 8df447f | 2005-11-01 15:48:24 +0000 | [diff] [blame] | 1531 | #ifndef SQLITE_OMIT_CAST |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1532 | /* Opcode: ToText P1 * * * * |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 1533 | ** |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1534 | ** Force the value in register P1 to be text. |
drh | 31beae9 | 2005-11-24 14:34:36 +0000 | [diff] [blame] | 1535 | ** If the value is numeric, convert it to a string using the |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 1536 | ** equivalent of printf(). Blob values are unchanged and |
| 1537 | ** are afterwards simply interpreted as text. |
| 1538 | ** |
| 1539 | ** A NULL value is not changed by this routine. It remains NULL. |
| 1540 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1541 | case OP_ToText: { /* same as TK_TO_TEXT, in1 */ |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1542 | if( pIn1->flags & MEM_Null ) break; |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 1543 | assert( MEM_Str==(MEM_Blob>>3) ); |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1544 | pIn1->flags |= (pIn1->flags&MEM_Blob)>>3; |
| 1545 | applyAffinity(pIn1, SQLITE_AFF_TEXT, encoding); |
| 1546 | rc = ExpandBlob(pIn1); |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1547 | assert( pIn1->flags & MEM_Str || db->mallocFailed ); |
drh | 68ac65e | 2009-01-05 18:02:27 +0000 | [diff] [blame] | 1548 | pIn1->flags &= ~(MEM_Int|MEM_Real|MEM_Blob|MEM_Zero); |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 1549 | UPDATE_MAX_BLOBSIZE(pIn1); |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 1550 | break; |
| 1551 | } |
| 1552 | |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1553 | /* Opcode: ToBlob P1 * * * * |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 1554 | ** |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1555 | ** Force the value in register P1 to be a BLOB. |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 1556 | ** If the value is numeric, convert it to a string first. |
| 1557 | ** Strings are simply reinterpreted as blobs with no change |
| 1558 | ** to the underlying data. |
| 1559 | ** |
| 1560 | ** A NULL value is not changed by this routine. It remains NULL. |
| 1561 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1562 | case OP_ToBlob: { /* same as TK_TO_BLOB, in1 */ |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1563 | if( pIn1->flags & MEM_Null ) break; |
| 1564 | if( (pIn1->flags & MEM_Blob)==0 ){ |
| 1565 | applyAffinity(pIn1, SQLITE_AFF_TEXT, encoding); |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1566 | assert( pIn1->flags & MEM_Str || db->mallocFailed ); |
drh | de58ddb | 2009-01-05 22:30:38 +0000 | [diff] [blame] | 1567 | MemSetTypeFlag(pIn1, MEM_Blob); |
| 1568 | }else{ |
| 1569 | pIn1->flags &= ~(MEM_TypeMask&~MEM_Blob); |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 1570 | } |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 1571 | UPDATE_MAX_BLOBSIZE(pIn1); |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 1572 | break; |
| 1573 | } |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1574 | |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1575 | /* Opcode: ToNumeric P1 * * * * |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1576 | ** |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1577 | ** Force the value in register P1 to be numeric (either an |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1578 | ** integer or a floating-point number.) |
| 1579 | ** If the value is text or blob, try to convert it to an using the |
| 1580 | ** equivalent of atoi() or atof() and store 0 if no such conversion |
| 1581 | ** is possible. |
| 1582 | ** |
| 1583 | ** A NULL value is not changed by this routine. It remains NULL. |
| 1584 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1585 | case OP_ToNumeric: { /* same as TK_TO_NUMERIC, in1 */ |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1586 | if( (pIn1->flags & (MEM_Null|MEM_Int|MEM_Real))==0 ){ |
| 1587 | sqlite3VdbeMemNumerify(pIn1); |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1588 | } |
| 1589 | break; |
| 1590 | } |
| 1591 | #endif /* SQLITE_OMIT_CAST */ |
| 1592 | |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1593 | /* Opcode: ToInt P1 * * * * |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1594 | ** |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1595 | ** Force the value in register P1 be an integer. If |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1596 | ** The value is currently a real number, drop its fractional part. |
| 1597 | ** If the value is text or blob, try to convert it to an integer using the |
| 1598 | ** equivalent of atoi() and store 0 if no such conversion is possible. |
| 1599 | ** |
| 1600 | ** A NULL value is not changed by this routine. It remains NULL. |
| 1601 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1602 | case OP_ToInt: { /* same as TK_TO_INT, in1 */ |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1603 | if( (pIn1->flags & MEM_Null)==0 ){ |
| 1604 | sqlite3VdbeMemIntegerify(pIn1); |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1605 | } |
| 1606 | break; |
| 1607 | } |
| 1608 | |
| 1609 | #ifndef SQLITE_OMIT_CAST |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1610 | /* Opcode: ToReal P1 * * * * |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1611 | ** |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1612 | ** Force the value in register P1 to be a floating point number. |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1613 | ** If The value is currently an integer, convert it. |
| 1614 | ** 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] | 1615 | ** equivalent of atoi() and store 0.0 if no such conversion is possible. |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1616 | ** |
| 1617 | ** A NULL value is not changed by this routine. It remains NULL. |
| 1618 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1619 | case OP_ToReal: { /* same as TK_TO_REAL, in1 */ |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1620 | if( (pIn1->flags & MEM_Null)==0 ){ |
| 1621 | sqlite3VdbeMemRealify(pIn1); |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1622 | } |
| 1623 | break; |
| 1624 | } |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 1625 | #endif /* SQLITE_OMIT_CAST */ |
| 1626 | |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 1627 | /* Opcode: Lt P1 P2 P3 P4 P5 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1628 | ** |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 1629 | ** Compare the values in register P1 and P3. If reg(P3)<reg(P1) then |
| 1630 | ** jump to address P2. |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1631 | ** |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 1632 | ** If the SQLITE_JUMPIFNULL bit of P5 is set and either reg(P1) or |
| 1633 | ** reg(P3) is NULL then take the jump. If the SQLITE_JUMPIFNULL |
| 1634 | ** bit is clear then fall thru if either operand is NULL. |
drh | 4f68623 | 2005-09-20 13:55:18 +0000 | [diff] [blame] | 1635 | ** |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 1636 | ** The SQLITE_AFF_MASK portion of P5 must be an affinity character - |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1637 | ** SQLITE_AFF_TEXT, SQLITE_AFF_INTEGER, and so forth. An attempt is made |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1638 | ** to coerce both inputs according to this affinity before the |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 1639 | ** comparison is made. If the SQLITE_AFF_MASK is 0x00, then numeric |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1640 | ** affinity is used. Note that the affinity conversions are stored |
| 1641 | ** back into the input registers P1 and P3. So this opcode can cause |
| 1642 | ** persistent changes to registers P1 and P3. |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 1643 | ** |
| 1644 | ** Once any conversions have taken place, and neither value is NULL, |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 1645 | ** the values are compared. If both values are blobs then memcmp() is |
| 1646 | ** used to determine the results of the comparison. If both values |
| 1647 | ** are text, then the appropriate collating function specified in |
| 1648 | ** P4 is used to do the comparison. If P4 is not specified then |
| 1649 | ** memcmp() is used to compare text string. If both values are |
| 1650 | ** numeric, then a numeric comparison is used. If the two values |
| 1651 | ** are of different types, then numbers are considered less than |
| 1652 | ** strings and strings are considered less than blobs. |
drh | c9b84a1 | 2002-06-20 11:36:48 +0000 | [diff] [blame] | 1653 | ** |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 1654 | ** If the SQLITE_STOREP2 bit of P5 is set, then do not jump. Instead, |
| 1655 | ** store a boolean result (either 0, or 1, or NULL) in register P2. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1656 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1657 | /* Opcode: Ne P1 P2 P3 P4 P5 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1658 | ** |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 1659 | ** This works just like the Lt opcode except that the jump is taken if |
| 1660 | ** 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] | 1661 | ** additional information. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1662 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1663 | /* Opcode: Eq P1 P2 P3 P4 P5 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1664 | ** |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 1665 | ** This works just like the Lt opcode except that the jump is taken if |
| 1666 | ** the operands in registers P1 and P3 are equal. |
| 1667 | ** See the Lt opcode for additional information. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1668 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1669 | /* Opcode: Le P1 P2 P3 P4 P5 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1670 | ** |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 1671 | ** This works just like the Lt opcode except that the jump is taken if |
| 1672 | ** the content of register P3 is less than or equal to the content of |
| 1673 | ** register P1. See the Lt opcode for additional information. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1674 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1675 | /* Opcode: Gt P1 P2 P3 P4 P5 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1676 | ** |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 1677 | ** This works just like the Lt opcode except that the jump is taken if |
| 1678 | ** the content of register P3 is greater than the content of |
| 1679 | ** register P1. See the Lt opcode for additional information. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1680 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1681 | /* Opcode: Ge P1 P2 P3 P4 P5 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1682 | ** |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 1683 | ** This works just like the Lt opcode except that the jump is taken if |
| 1684 | ** the content of register P3 is greater than or equal to the content of |
| 1685 | ** register P1. See the Lt opcode for additional information. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1686 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1687 | case OP_Eq: /* same as TK_EQ, jump, in1, in3 */ |
| 1688 | case OP_Ne: /* same as TK_NE, jump, in1, in3 */ |
| 1689 | case OP_Lt: /* same as TK_LT, jump, in1, in3 */ |
| 1690 | case OP_Le: /* same as TK_LE, jump, in1, in3 */ |
| 1691 | case OP_Gt: /* same as TK_GT, jump, in1, in3 */ |
| 1692 | case OP_Ge: { /* same as TK_GE, jump, in1, in3 */ |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 1693 | int flags; |
| 1694 | int res; |
| 1695 | char affinity; |
| 1696 | |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 1697 | flags = pIn1->flags|pIn3->flags; |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 1698 | |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 1699 | if( flags&MEM_Null ){ |
drh | 93a960a | 2008-07-10 00:32:42 +0000 | [diff] [blame] | 1700 | /* If either operand is NULL then the result is always NULL. |
| 1701 | ** The jump is taken if the SQLITE_JUMPIFNULL bit is set. |
| 1702 | */ |
| 1703 | if( pOp->p5 & SQLITE_STOREP2 ){ |
| 1704 | pOut = &p->aMem[pOp->p2]; |
| 1705 | MemSetTypeFlag(pOut, MEM_Null); |
| 1706 | REGISTER_TRACE(pOp->p2, pOut); |
| 1707 | }else if( pOp->p5 & SQLITE_JUMPIFNULL ){ |
| 1708 | pc = pOp->p2-1; |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 1709 | } |
drh | 93a960a | 2008-07-10 00:32:42 +0000 | [diff] [blame] | 1710 | break; |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 1711 | } |
| 1712 | |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 1713 | affinity = pOp->p5 & SQLITE_AFF_MASK; |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 1714 | if( affinity ){ |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 1715 | applyAffinity(pIn1, affinity, encoding); |
| 1716 | applyAffinity(pIn3, affinity, encoding); |
drh | bbce338 | 2008-12-06 16:46:13 +0000 | [diff] [blame] | 1717 | if( db->mallocFailed ) goto no_mem; |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 1718 | } |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 1719 | |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 1720 | assert( pOp->p4type==P4_COLLSEQ || pOp->p4.pColl==0 ); |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 1721 | ExpandBlob(pIn1); |
| 1722 | ExpandBlob(pIn3); |
| 1723 | res = sqlite3MemCompare(pIn3, pIn1, pOp->p4.pColl); |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 1724 | switch( pOp->opcode ){ |
| 1725 | case OP_Eq: res = res==0; break; |
| 1726 | case OP_Ne: res = res!=0; break; |
| 1727 | case OP_Lt: res = res<0; break; |
| 1728 | case OP_Le: res = res<=0; break; |
| 1729 | case OP_Gt: res = res>0; break; |
| 1730 | default: res = res>=0; break; |
| 1731 | } |
| 1732 | |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 1733 | if( pOp->p5 & SQLITE_STOREP2 ){ |
| 1734 | pOut = &p->aMem[pOp->p2]; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1735 | MemSetTypeFlag(pOut, MEM_Int); |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 1736 | pOut->u.i = res; |
| 1737 | REGISTER_TRACE(pOp->p2, pOut); |
| 1738 | }else if( res ){ |
| 1739 | pc = pOp->p2-1; |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 1740 | } |
| 1741 | break; |
| 1742 | } |
drh | c9b84a1 | 2002-06-20 11:36:48 +0000 | [diff] [blame] | 1743 | |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 1744 | /* Opcode: Permutation * * * P4 * |
| 1745 | ** |
shane | be21779 | 2009-03-05 04:20:31 +0000 | [diff] [blame] | 1746 | ** Set the permutation used by the OP_Compare operator to be the array |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 1747 | ** of integers in P4. |
| 1748 | ** |
| 1749 | ** The permutation is only valid until the next OP_Permutation, OP_Compare, |
| 1750 | ** OP_Halt, or OP_ResultRow. Typically the OP_Permutation should occur |
| 1751 | ** immediately prior to the OP_Compare. |
| 1752 | */ |
| 1753 | case OP_Permutation: { |
| 1754 | assert( pOp->p4type==P4_INTARRAY ); |
| 1755 | assert( pOp->p4.ai ); |
| 1756 | aPermute = pOp->p4.ai; |
| 1757 | break; |
| 1758 | } |
| 1759 | |
drh | 16ee60f | 2008-06-20 18:13:25 +0000 | [diff] [blame] | 1760 | /* Opcode: Compare P1 P2 P3 P4 * |
| 1761 | ** |
| 1762 | ** Compare to vectors of registers in reg(P1)..reg(P1+P3-1) (all this |
| 1763 | ** one "A") and in reg(P2)..reg(P2+P3-1) ("B"). Save the result of |
| 1764 | ** the comparison for use by the next OP_Jump instruct. |
| 1765 | ** |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 1766 | ** P4 is a KeyInfo structure that defines collating sequences and sort |
| 1767 | ** orders for the comparison. The permutation applies to registers |
| 1768 | ** only. The KeyInfo elements are used sequentially. |
| 1769 | ** |
| 1770 | ** The comparison is a sort comparison, so NULLs compare equal, |
| 1771 | ** NULLs are less than numbers, numbers are less than strings, |
drh | 16ee60f | 2008-06-20 18:13:25 +0000 | [diff] [blame] | 1772 | ** and strings are less than blobs. |
| 1773 | */ |
| 1774 | case OP_Compare: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1775 | int n; |
| 1776 | int i; |
| 1777 | int p1; |
| 1778 | int p2; |
| 1779 | const KeyInfo *pKeyInfo; |
| 1780 | int idx; |
| 1781 | CollSeq *pColl; /* Collating sequence to use on this term */ |
| 1782 | int bRev; /* True for DESCENDING sort order */ |
| 1783 | |
| 1784 | n = pOp->p3; |
| 1785 | pKeyInfo = pOp->p4.pKeyInfo; |
drh | 16ee60f | 2008-06-20 18:13:25 +0000 | [diff] [blame] | 1786 | assert( n>0 ); |
drh | 93a960a | 2008-07-10 00:32:42 +0000 | [diff] [blame] | 1787 | assert( pKeyInfo!=0 ); |
drh | 16ee60f | 2008-06-20 18:13:25 +0000 | [diff] [blame] | 1788 | p1 = pOp->p1; |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1789 | assert( p1>0 && p1+n<=p->nMem+1 ); |
drh | 16ee60f | 2008-06-20 18:13:25 +0000 | [diff] [blame] | 1790 | p2 = pOp->p2; |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1791 | assert( p2>0 && p2+n<=p->nMem+1 ); |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 1792 | for(i=0; i<n; i++){ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1793 | idx = aPermute ? aPermute[i] : i; |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 1794 | REGISTER_TRACE(p1+idx, &p->aMem[p1+idx]); |
| 1795 | REGISTER_TRACE(p2+idx, &p->aMem[p2+idx]); |
drh | 93a960a | 2008-07-10 00:32:42 +0000 | [diff] [blame] | 1796 | assert( i<pKeyInfo->nField ); |
| 1797 | pColl = pKeyInfo->aColl[i]; |
| 1798 | bRev = pKeyInfo->aSortOrder[i]; |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 1799 | iCompare = sqlite3MemCompare(&p->aMem[p1+idx], &p->aMem[p2+idx], pColl); |
| 1800 | if( iCompare ){ |
| 1801 | if( bRev ) iCompare = -iCompare; |
| 1802 | break; |
| 1803 | } |
drh | 16ee60f | 2008-06-20 18:13:25 +0000 | [diff] [blame] | 1804 | } |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 1805 | aPermute = 0; |
drh | 16ee60f | 2008-06-20 18:13:25 +0000 | [diff] [blame] | 1806 | break; |
| 1807 | } |
| 1808 | |
| 1809 | /* Opcode: Jump P1 P2 P3 * * |
| 1810 | ** |
| 1811 | ** Jump to the instruction at address P1, P2, or P3 depending on whether |
| 1812 | ** in the most recent OP_Compare instruction the P1 vector was less than |
| 1813 | ** equal to, or greater than the P2 vector, respectively. |
| 1814 | */ |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 1815 | case OP_Jump: { /* jump */ |
| 1816 | if( iCompare<0 ){ |
drh | 16ee60f | 2008-06-20 18:13:25 +0000 | [diff] [blame] | 1817 | pc = pOp->p1 - 1; |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 1818 | }else if( iCompare==0 ){ |
drh | 16ee60f | 2008-06-20 18:13:25 +0000 | [diff] [blame] | 1819 | pc = pOp->p2 - 1; |
| 1820 | }else{ |
| 1821 | pc = pOp->p3 - 1; |
| 1822 | } |
| 1823 | break; |
| 1824 | } |
| 1825 | |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1826 | /* Opcode: And P1 P2 P3 * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1827 | ** |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1828 | ** Take the logical AND of the values in registers P1 and P2 and |
| 1829 | ** write the result into register P3. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1830 | ** |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1831 | ** If either P1 or P2 is 0 (false) then the result is 0 even if |
| 1832 | ** the other input is NULL. A NULL and true or two NULLs give |
| 1833 | ** a NULL output. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1834 | */ |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1835 | /* Opcode: Or P1 P2 P3 * * |
| 1836 | ** |
| 1837 | ** Take the logical OR of the values in register P1 and P2 and |
| 1838 | ** store the answer in register P3. |
| 1839 | ** |
| 1840 | ** If either P1 or P2 is nonzero (true) then the result is 1 (true) |
| 1841 | ** even if the other input is NULL. A NULL and false or two NULLs |
| 1842 | ** give a NULL output. |
| 1843 | */ |
| 1844 | case OP_And: /* same as TK_AND, in1, in2, out3 */ |
| 1845 | case OP_Or: { /* same as TK_OR, in1, in2, out3 */ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1846 | int v1; /* Left operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */ |
| 1847 | int v2; /* Right operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */ |
drh | bb11351 | 2002-05-27 01:04:51 +0000 | [diff] [blame] | 1848 | |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1849 | if( pIn1->flags & MEM_Null ){ |
drh | bb11351 | 2002-05-27 01:04:51 +0000 | [diff] [blame] | 1850 | v1 = 2; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1851 | }else{ |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1852 | v1 = sqlite3VdbeIntValue(pIn1)!=0; |
drh | bb11351 | 2002-05-27 01:04:51 +0000 | [diff] [blame] | 1853 | } |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1854 | if( pIn2->flags & MEM_Null ){ |
drh | bb11351 | 2002-05-27 01:04:51 +0000 | [diff] [blame] | 1855 | v2 = 2; |
| 1856 | }else{ |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1857 | v2 = sqlite3VdbeIntValue(pIn2)!=0; |
drh | bb11351 | 2002-05-27 01:04:51 +0000 | [diff] [blame] | 1858 | } |
| 1859 | if( pOp->opcode==OP_And ){ |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1860 | 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] | 1861 | v1 = and_logic[v1*3+v2]; |
| 1862 | }else{ |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1863 | 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] | 1864 | v1 = or_logic[v1*3+v2]; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1865 | } |
drh | bb11351 | 2002-05-27 01:04:51 +0000 | [diff] [blame] | 1866 | if( v1==2 ){ |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1867 | MemSetTypeFlag(pOut, MEM_Null); |
drh | bb11351 | 2002-05-27 01:04:51 +0000 | [diff] [blame] | 1868 | }else{ |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1869 | pOut->u.i = v1; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1870 | MemSetTypeFlag(pOut, MEM_Int); |
drh | bb11351 | 2002-05-27 01:04:51 +0000 | [diff] [blame] | 1871 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1872 | break; |
| 1873 | } |
| 1874 | |
drh | e99fa2a | 2008-12-15 15:27:51 +0000 | [diff] [blame] | 1875 | /* Opcode: Not P1 P2 * * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1876 | ** |
drh | e99fa2a | 2008-12-15 15:27:51 +0000 | [diff] [blame] | 1877 | ** Interpret the value in register P1 as a boolean value. Store the |
| 1878 | ** boolean complement in register P2. If the value in register P1 is |
| 1879 | ** NULL, then a NULL is stored in P2. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1880 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1881 | case OP_Not: { /* same as TK_NOT, in1 */ |
drh | e99fa2a | 2008-12-15 15:27:51 +0000 | [diff] [blame] | 1882 | pOut = &p->aMem[pOp->p2]; |
| 1883 | if( pIn1->flags & MEM_Null ){ |
| 1884 | sqlite3VdbeMemSetNull(pOut); |
| 1885 | }else{ |
| 1886 | sqlite3VdbeMemSetInt64(pOut, !sqlite3VdbeIntValue(pIn1)); |
| 1887 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1888 | break; |
| 1889 | } |
| 1890 | |
drh | e99fa2a | 2008-12-15 15:27:51 +0000 | [diff] [blame] | 1891 | /* Opcode: BitNot P1 P2 * * * |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1892 | ** |
drh | e99fa2a | 2008-12-15 15:27:51 +0000 | [diff] [blame] | 1893 | ** Interpret the content of register P1 as an integer. Store the |
| 1894 | ** ones-complement of the P1 value into register P2. If P1 holds |
| 1895 | ** a NULL then store a NULL in P2. |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1896 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1897 | case OP_BitNot: { /* same as TK_BITNOT, in1 */ |
drh | e99fa2a | 2008-12-15 15:27:51 +0000 | [diff] [blame] | 1898 | pOut = &p->aMem[pOp->p2]; |
| 1899 | if( pIn1->flags & MEM_Null ){ |
| 1900 | sqlite3VdbeMemSetNull(pOut); |
| 1901 | }else{ |
| 1902 | sqlite3VdbeMemSetInt64(pOut, ~sqlite3VdbeIntValue(pIn1)); |
| 1903 | } |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1904 | break; |
| 1905 | } |
| 1906 | |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 1907 | /* Opcode: If P1 P2 P3 * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1908 | ** |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 1909 | ** Jump to P2 if the value in register P1 is true. The value is |
| 1910 | ** is considered true if it is numeric and non-zero. If the value |
| 1911 | ** in P1 is NULL then take the jump if P3 is true. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1912 | */ |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 1913 | /* Opcode: IfNot P1 P2 P3 * * |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1914 | ** |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 1915 | ** Jump to P2 if the value in register P1 is False. The value is |
| 1916 | ** is considered true if it has a numeric value of zero. If the value |
| 1917 | ** in P1 is NULL then take the jump if P3 is true. |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1918 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1919 | case OP_If: /* jump, in1 */ |
| 1920 | case OP_IfNot: { /* jump, in1 */ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1921 | int c; |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 1922 | if( pIn1->flags & MEM_Null ){ |
| 1923 | c = pOp->p3; |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1924 | }else{ |
drh | ba0232a | 2005-06-06 17:27:19 +0000 | [diff] [blame] | 1925 | #ifdef SQLITE_OMIT_FLOATING_POINT |
shane | fbd60f8 | 2009-02-04 03:59:25 +0000 | [diff] [blame] | 1926 | c = sqlite3VdbeIntValue(pIn1)!=0; |
drh | ba0232a | 2005-06-06 17:27:19 +0000 | [diff] [blame] | 1927 | #else |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 1928 | c = sqlite3VdbeRealValue(pIn1)!=0.0; |
drh | ba0232a | 2005-06-06 17:27:19 +0000 | [diff] [blame] | 1929 | #endif |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1930 | if( pOp->opcode==OP_IfNot ) c = !c; |
| 1931 | } |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 1932 | if( c ){ |
| 1933 | pc = pOp->p2-1; |
| 1934 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1935 | break; |
| 1936 | } |
| 1937 | |
drh | 830ecf9 | 2009-06-18 00:41:55 +0000 | [diff] [blame] | 1938 | /* Opcode: IsNull P1 P2 * * * |
drh | 477df4b | 2008-01-05 18:48:24 +0000 | [diff] [blame] | 1939 | ** |
drh | 830ecf9 | 2009-06-18 00:41:55 +0000 | [diff] [blame] | 1940 | ** Jump to P2 if the value in register P1 is NULL. |
drh | 477df4b | 2008-01-05 18:48:24 +0000 | [diff] [blame] | 1941 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1942 | case OP_IsNull: { /* same as TK_ISNULL, jump, in1 */ |
drh | 830ecf9 | 2009-06-18 00:41:55 +0000 | [diff] [blame] | 1943 | if( (pIn1->flags & MEM_Null)!=0 ){ |
| 1944 | pc = pOp->p2 - 1; |
| 1945 | } |
drh | 477df4b | 2008-01-05 18:48:24 +0000 | [diff] [blame] | 1946 | break; |
| 1947 | } |
| 1948 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1949 | /* Opcode: NotNull P1 P2 * * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1950 | ** |
drh | 6a288a3 | 2008-01-07 19:20:24 +0000 | [diff] [blame] | 1951 | ** Jump to P2 if the value in register P1 is not NULL. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1952 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1953 | case OP_NotNull: { /* same as TK_NOTNULL, jump, in1 */ |
drh | 6a288a3 | 2008-01-07 19:20:24 +0000 | [diff] [blame] | 1954 | if( (pIn1->flags & MEM_Null)==0 ){ |
| 1955 | pc = pOp->p2 - 1; |
| 1956 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1957 | break; |
| 1958 | } |
| 1959 | |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 1960 | /* Opcode: SetNumColumns * P2 * * * |
danielk1977 | b4964b7 | 2004-05-18 01:23:38 +0000 | [diff] [blame] | 1961 | ** |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 1962 | ** This opcode sets the number of columns for the cursor opened by the |
| 1963 | ** following instruction to P2. |
danielk1977 | b4964b7 | 2004-05-18 01:23:38 +0000 | [diff] [blame] | 1964 | ** |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 1965 | ** An OP_SetNumColumns is only useful if it occurs immediately before |
| 1966 | ** one of the following opcodes: |
danielk1977 | ac17178 | 2005-02-05 06:49:54 +0000 | [diff] [blame] | 1967 | ** |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 1968 | ** OpenRead |
| 1969 | ** OpenWrite |
| 1970 | ** OpenPseudo |
| 1971 | ** |
| 1972 | ** If the OP_Column opcode is to be executed on a cursor, then |
| 1973 | ** this opcode must be present immediately before the opcode that |
| 1974 | ** opens the cursor. |
danielk1977 | b4964b7 | 2004-05-18 01:23:38 +0000 | [diff] [blame] | 1975 | */ |
danielk1977 | d336e22 | 2009-02-20 10:58:41 +0000 | [diff] [blame] | 1976 | #if 0 |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1977 | case OP_SetNumColumns: { |
danielk1977 | b4964b7 | 2004-05-18 01:23:38 +0000 | [diff] [blame] | 1978 | break; |
| 1979 | } |
danielk1977 | d336e22 | 2009-02-20 10:58:41 +0000 | [diff] [blame] | 1980 | #endif |
danielk1977 | b4964b7 | 2004-05-18 01:23:38 +0000 | [diff] [blame] | 1981 | |
danielk1977 | 60585dd | 2008-01-03 08:08:40 +0000 | [diff] [blame] | 1982 | /* Opcode: Column P1 P2 P3 P4 * |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 1983 | ** |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1984 | ** Interpret the data that cursor P1 points to as a structure built using |
| 1985 | ** the MakeRecord instruction. (See the MakeRecord opcode for additional |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 1986 | ** information about the format of the data.) Extract the P2-th column |
| 1987 | ** from this record. If there are less that (P2+1) |
| 1988 | ** values in the record, extract a NULL. |
| 1989 | ** |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1990 | ** The value extracted is stored in register P3. |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 1991 | ** |
danielk1977 | 1f4aa33 | 2008-01-03 09:51:55 +0000 | [diff] [blame] | 1992 | ** If the column contains fewer than P2 fields, then extract a NULL. Or, |
| 1993 | ** if the P4 argument is a P4_MEM use the value of the P4 argument as |
| 1994 | ** the result. |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 1995 | */ |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1996 | case OP_Column: { |
drh | 35cd643 | 2009-06-05 14:17:21 +0000 | [diff] [blame] | 1997 | u32 payloadSize; /* Number of bytes in the record */ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 1998 | i64 payloadSize64; /* Number of bytes in the record */ |
| 1999 | int p1; /* P1 value of the opcode */ |
| 2000 | int p2; /* column number to retrieve */ |
| 2001 | VdbeCursor *pC; /* The VDBE cursor */ |
drh | e61cffc | 2004-06-12 18:12:15 +0000 | [diff] [blame] | 2002 | char *zRec; /* Pointer to complete record-data */ |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 2003 | BtCursor *pCrsr; /* The BTree cursor */ |
| 2004 | u32 *aType; /* aType[i] holds the numeric type of the i-th column */ |
| 2005 | 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] | 2006 | int nField; /* number of fields in the record */ |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 2007 | int len; /* The length of the serialized data for the column */ |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 2008 | int i; /* Loop counter */ |
| 2009 | char *zData; /* Part of the record being decoded */ |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 2010 | Mem *pDest; /* Where to write the extracted value */ |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 2011 | Mem sMem; /* For storing the record being decoded */ |
drh | 35cd643 | 2009-06-05 14:17:21 +0000 | [diff] [blame] | 2012 | u8 *zIdx; /* Index into header */ |
| 2013 | u8 *zEndHdr; /* Pointer to first byte after the header */ |
| 2014 | u32 offset; /* Offset into the data */ |
| 2015 | u64 offset64; /* 64-bit offset. 64 bits needed to catch overflow */ |
| 2016 | int szHdr; /* Size of the header size field at start of record */ |
| 2017 | int avail; /* Number of bytes of available data */ |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2018 | |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2019 | |
| 2020 | p1 = pOp->p1; |
| 2021 | p2 = pOp->p2; |
| 2022 | pC = 0; |
drh | b27b7f5 | 2008-12-10 18:03:45 +0000 | [diff] [blame] | 2023 | memset(&sMem, 0, sizeof(sMem)); |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 2024 | assert( p1<p->nCursor ); |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2025 | assert( pOp->p3>0 && pOp->p3<=p->nMem ); |
| 2026 | pDest = &p->aMem[pOp->p3]; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 2027 | MemSetTypeFlag(pDest, MEM_Null); |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 2028 | |
drh | e61cffc | 2004-06-12 18:12:15 +0000 | [diff] [blame] | 2029 | /* This block sets the variable payloadSize to be the total number of |
| 2030 | ** bytes in the record. |
| 2031 | ** |
| 2032 | ** 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] | 2033 | ** The complete record text is always available for pseudo-tables |
| 2034 | ** If the record is stored in a cursor, the complete record text |
| 2035 | ** might be available in the pC->aRow cache. Or it might not be. |
| 2036 | ** If the data is unavailable, zRec is set to NULL. |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 2037 | ** |
| 2038 | ** We also compute the number of columns in the record. For cursors, |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 2039 | ** the number of columns is stored in the VdbeCursor.nField element. |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 2040 | */ |
drh | b73857f | 2006-03-17 00:25:59 +0000 | [diff] [blame] | 2041 | pC = p->apCsr[p1]; |
danielk1977 | 6c92409 | 2007-11-12 08:09:34 +0000 | [diff] [blame] | 2042 | assert( pC!=0 ); |
danielk1977 | 0817d0d | 2007-02-14 09:19:36 +0000 | [diff] [blame] | 2043 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 2044 | assert( pC->pVtabCursor==0 ); |
| 2045 | #endif |
drh | b73857f | 2006-03-17 00:25:59 +0000 | [diff] [blame] | 2046 | if( pC->pCursor!=0 ){ |
drh | e61cffc | 2004-06-12 18:12:15 +0000 | [diff] [blame] | 2047 | /* The record is stored in a B-Tree */ |
drh | 536065a | 2005-01-26 21:55:31 +0000 | [diff] [blame] | 2048 | rc = sqlite3VdbeCursorMoveto(pC); |
drh | 52f159e | 2005-01-27 00:33:21 +0000 | [diff] [blame] | 2049 | if( rc ) goto abort_due_to_error; |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2050 | zRec = 0; |
| 2051 | pCrsr = pC->pCursor; |
| 2052 | if( pC->nullRow ){ |
| 2053 | payloadSize = 0; |
drh | 76873ab | 2006-01-07 18:48:26 +0000 | [diff] [blame] | 2054 | }else if( pC->cacheStatus==p->cacheCtr ){ |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2055 | payloadSize = pC->payloadSize; |
drh | 2646da7 | 2005-12-09 20:02:05 +0000 | [diff] [blame] | 2056 | zRec = (char*)pC->aRow; |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 2057 | }else if( pC->isIndex ){ |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2058 | sqlite3BtreeKeySize(pCrsr, &payloadSize64); |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 2059 | /* sqlite3BtreeParseCellPtr() uses getVarint32() to extract the |
| 2060 | ** payload size, so it is impossible for payloadSize64 to be |
| 2061 | ** larger than 32 bits. */ |
| 2062 | assert( (payloadSize64 & SQLITE_MAX_U32)==(u64)payloadSize64 ); |
drh | 35cd643 | 2009-06-05 14:17:21 +0000 | [diff] [blame] | 2063 | payloadSize = (u32)payloadSize64; |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2064 | }else{ |
drh | 35cd643 | 2009-06-05 14:17:21 +0000 | [diff] [blame] | 2065 | sqlite3BtreeDataSize(pCrsr, &payloadSize); |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2066 | } |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 2067 | nField = pC->nField; |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 2068 | }else{ |
| 2069 | assert( pC->pseudoTable ); |
drh | e61cffc | 2004-06-12 18:12:15 +0000 | [diff] [blame] | 2070 | /* The record is the sole entry of a pseudo-table */ |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2071 | payloadSize = pC->nData; |
| 2072 | zRec = pC->pData; |
drh | 76873ab | 2006-01-07 18:48:26 +0000 | [diff] [blame] | 2073 | pC->cacheStatus = CACHE_STALE; |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2074 | assert( payloadSize==0 || zRec!=0 ); |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 2075 | nField = pC->nField; |
danielk1977 | f7df9cc | 2004-06-16 12:02:47 +0000 | [diff] [blame] | 2076 | pCrsr = 0; |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2077 | } |
| 2078 | |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2079 | /* If payloadSize is 0, then just store a NULL */ |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2080 | if( payloadSize==0 ){ |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 2081 | assert( pDest->flags&MEM_Null ); |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 2082 | goto op_column_out; |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2083 | } |
drh | 35cd643 | 2009-06-05 14:17:21 +0000 | [diff] [blame] | 2084 | assert( db->aLimit[SQLITE_LIMIT_LENGTH]>=0 ); |
| 2085 | if( payloadSize > (u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){ |
drh | 023ae03 | 2007-05-08 12:12:16 +0000 | [diff] [blame] | 2086 | goto too_big; |
| 2087 | } |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2088 | |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 2089 | assert( p2<nField ); |
danielk1977 | b4964b7 | 2004-05-18 01:23:38 +0000 | [diff] [blame] | 2090 | |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2091 | /* Read and parse the table header. Store the results of the parse |
| 2092 | ** into the record header cache fields of the cursor. |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2093 | */ |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 2094 | aType = pC->aType; |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 2095 | if( pC->cacheStatus==p->cacheCtr ){ |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 2096 | aOffset = pC->aOffset; |
| 2097 | }else{ |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 2098 | assert(aType); |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2099 | avail = 0; |
drh | b73857f | 2006-03-17 00:25:59 +0000 | [diff] [blame] | 2100 | pC->aOffset = aOffset = &aType[nField]; |
| 2101 | pC->payloadSize = payloadSize; |
| 2102 | pC->cacheStatus = p->cacheCtr; |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2103 | |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 2104 | /* Figure out how many bytes are in the header */ |
danielk1977 | 84ac9d0 | 2004-05-18 09:58:06 +0000 | [diff] [blame] | 2105 | if( zRec ){ |
| 2106 | zData = zRec; |
| 2107 | }else{ |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 2108 | if( pC->isIndex ){ |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 2109 | zData = (char*)sqlite3BtreeKeyFetch(pCrsr, &avail); |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 2110 | }else{ |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 2111 | zData = (char*)sqlite3BtreeDataFetch(pCrsr, &avail); |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2112 | } |
drh | e61cffc | 2004-06-12 18:12:15 +0000 | [diff] [blame] | 2113 | /* If KeyFetch()/DataFetch() managed to get the entire payload, |
| 2114 | ** save the payload in the pC->aRow cache. That will save us from |
| 2115 | ** having to make additional calls to fetch the content portion of |
| 2116 | ** the record. |
| 2117 | */ |
drh | 35cd643 | 2009-06-05 14:17:21 +0000 | [diff] [blame] | 2118 | assert( avail>=0 ); |
| 2119 | if( payloadSize <= (u32)avail ){ |
drh | 2646da7 | 2005-12-09 20:02:05 +0000 | [diff] [blame] | 2120 | zRec = zData; |
| 2121 | pC->aRow = (u8*)zData; |
drh | e61cffc | 2004-06-12 18:12:15 +0000 | [diff] [blame] | 2122 | }else{ |
| 2123 | pC->aRow = 0; |
| 2124 | } |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 2125 | } |
drh | 588f5bc | 2007-01-02 18:41:54 +0000 | [diff] [blame] | 2126 | /* The following assert is true in all cases accept when |
| 2127 | ** the database file has been corrupted externally. |
| 2128 | ** assert( zRec!=0 || avail>=payloadSize || avail>=9 ); */ |
drh | 35cd643 | 2009-06-05 14:17:21 +0000 | [diff] [blame] | 2129 | szHdr = getVarint32((u8*)zData, offset); |
| 2130 | |
| 2131 | /* Make sure a corrupt database has not given us an oversize header. |
| 2132 | ** Do this now to avoid an oversize memory allocation. |
| 2133 | ** |
| 2134 | ** Type entries can be between 1 and 5 bytes each. But 4 and 5 byte |
| 2135 | ** types use so much data space that there can only be 4096 and 32 of |
| 2136 | ** them, respectively. So the maximum header length results from a |
| 2137 | ** 3-byte type for each of the maximum of 32768 columns plus three |
| 2138 | ** extra bytes for the header length itself. 32768*3 + 3 = 98307. |
| 2139 | */ |
| 2140 | if( offset > 98307 ){ |
| 2141 | rc = SQLITE_CORRUPT_BKPT; |
| 2142 | goto op_column_out; |
| 2143 | } |
| 2144 | |
| 2145 | /* Compute in len the number of bytes of data we need to read in order |
| 2146 | ** to get nField type values. offset is an upper bound on this. But |
| 2147 | ** nField might be significantly less than the true number of columns |
| 2148 | ** in the table, and in that case, 5*nField+3 might be smaller than offset. |
| 2149 | ** We want to minimize len in order to limit the size of the memory |
| 2150 | ** allocation, especially if a corrupt database file has caused offset |
| 2151 | ** to be oversized. Offset is limited to 98307 above. But 98307 might |
| 2152 | ** still exceed Robson memory allocation limits on some configurations. |
| 2153 | ** On systems that cannot tolerate large memory allocations, nField*5+3 |
| 2154 | ** will likely be much smaller since nField will likely be less than |
| 2155 | ** 20 or so. This insures that Robson memory allocation limits are |
| 2156 | ** not exceeded even for corrupt database files. |
| 2157 | */ |
| 2158 | len = nField*5 + 3; |
shane | 75ac1de | 2009-06-09 18:58:52 +0000 | [diff] [blame] | 2159 | if( len > (int)offset ) len = (int)offset; |
drh | e61cffc | 2004-06-12 18:12:15 +0000 | [diff] [blame] | 2160 | |
| 2161 | /* The KeyFetch() or DataFetch() above are fast and will get the entire |
| 2162 | ** record header in most cases. But they will fail to get the complete |
| 2163 | ** record header if the record header does not fit on a single page |
| 2164 | ** in the B-Tree. When that happens, use sqlite3VdbeMemFromBtree() to |
| 2165 | ** acquire the complete header text. |
| 2166 | */ |
drh | 35cd643 | 2009-06-05 14:17:21 +0000 | [diff] [blame] | 2167 | if( !zRec && avail<len ){ |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 2168 | sMem.flags = 0; |
| 2169 | sMem.db = 0; |
drh | 35cd643 | 2009-06-05 14:17:21 +0000 | [diff] [blame] | 2170 | rc = sqlite3VdbeMemFromBtree(pCrsr, 0, len, pC->isIndex, &sMem); |
danielk1977 | 84ac9d0 | 2004-05-18 09:58:06 +0000 | [diff] [blame] | 2171 | if( rc!=SQLITE_OK ){ |
danielk1977 | 3c9cc8d | 2005-01-17 03:40:08 +0000 | [diff] [blame] | 2172 | goto op_column_out; |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2173 | } |
drh | b6f5452 | 2004-05-20 02:42:16 +0000 | [diff] [blame] | 2174 | zData = sMem.z; |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2175 | } |
drh | 35cd643 | 2009-06-05 14:17:21 +0000 | [diff] [blame] | 2176 | zEndHdr = (u8 *)&zData[len]; |
| 2177 | zIdx = (u8 *)&zData[szHdr]; |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2178 | |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 2179 | /* Scan the header and use it to fill in the aType[] and aOffset[] |
| 2180 | ** arrays. aType[i] will contain the type integer for the i-th |
| 2181 | ** column and aOffset[i] will contain the offset from the beginning |
| 2182 | ** 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] | 2183 | */ |
drh | 35cd643 | 2009-06-05 14:17:21 +0000 | [diff] [blame] | 2184 | offset64 = offset; |
danielk1977 | dedf45b | 2006-01-13 17:12:01 +0000 | [diff] [blame] | 2185 | for(i=0; i<nField; i++){ |
| 2186 | if( zIdx<zEndHdr ){ |
drh | 35cd643 | 2009-06-05 14:17:21 +0000 | [diff] [blame] | 2187 | aOffset[i] = (u32)offset64; |
shane | 3f8d5cf | 2008-04-24 19:15:09 +0000 | [diff] [blame] | 2188 | zIdx += getVarint32(zIdx, aType[i]); |
drh | 35cd643 | 2009-06-05 14:17:21 +0000 | [diff] [blame] | 2189 | offset64 += sqlite3VdbeSerialTypeLen(aType[i]); |
danielk1977 | dedf45b | 2006-01-13 17:12:01 +0000 | [diff] [blame] | 2190 | }else{ |
| 2191 | /* If i is less that nField, then there are less fields in this |
| 2192 | ** record than SetNumColumns indicated there are columns in the |
| 2193 | ** table. Set the offset for any extra columns not present in |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2194 | ** the record to 0. This tells code below to store a NULL |
| 2195 | ** instead of deserializing a value from the record. |
danielk1977 | dedf45b | 2006-01-13 17:12:01 +0000 | [diff] [blame] | 2196 | */ |
| 2197 | aOffset[i] = 0; |
| 2198 | } |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2199 | } |
danielk1977 | 5f09613 | 2008-03-28 15:44:09 +0000 | [diff] [blame] | 2200 | sqlite3VdbeMemRelease(&sMem); |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 2201 | sMem.flags = MEM_Null; |
| 2202 | |
danielk1977 | 9792eef | 2006-01-13 15:58:43 +0000 | [diff] [blame] | 2203 | /* If we have read more header data than was contained in the header, |
| 2204 | ** 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] | 2205 | ** record, or if the end of the last field appears to be before the end |
| 2206 | ** of the record (when all fields present), then we must be dealing |
| 2207 | ** with a corrupt database. |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 2208 | */ |
drh | 35cd643 | 2009-06-05 14:17:21 +0000 | [diff] [blame] | 2209 | if( (zIdx > zEndHdr)|| (offset64 > payloadSize) |
| 2210 | || (zIdx==zEndHdr && offset64!=(u64)payloadSize) ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 2211 | rc = SQLITE_CORRUPT_BKPT; |
danielk1977 | 3c9cc8d | 2005-01-17 03:40:08 +0000 | [diff] [blame] | 2212 | goto op_column_out; |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 2213 | } |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 2214 | } |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2215 | |
danielk1977 | 36963fd | 2005-02-19 08:18:05 +0000 | [diff] [blame] | 2216 | /* Get the column information. If aOffset[p2] is non-zero, then |
| 2217 | ** deserialize the value from the record. If aOffset[p2] is zero, |
| 2218 | ** then there are not enough fields in the record to satisfy the |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 2219 | ** 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] | 2220 | ** a pointer to a Mem object. |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2221 | */ |
danielk1977 | 36963fd | 2005-02-19 08:18:05 +0000 | [diff] [blame] | 2222 | if( aOffset[p2] ){ |
| 2223 | assert( rc==SQLITE_OK ); |
| 2224 | if( zRec ){ |
danielk1977 | 808ec7c | 2008-07-29 10:18:57 +0000 | [diff] [blame] | 2225 | sqlite3VdbeMemReleaseExternal(pDest); |
| 2226 | sqlite3VdbeSerialGet((u8 *)&zRec[aOffset[p2]], aType[p2], pDest); |
danielk1977 | 36963fd | 2005-02-19 08:18:05 +0000 | [diff] [blame] | 2227 | }else{ |
| 2228 | len = sqlite3VdbeSerialTypeLen(aType[p2]); |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 2229 | sqlite3VdbeMemMove(&sMem, pDest); |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 2230 | rc = sqlite3VdbeMemFromBtree(pCrsr, aOffset[p2], len, pC->isIndex, &sMem); |
danielk1977 | 36963fd | 2005-02-19 08:18:05 +0000 | [diff] [blame] | 2231 | if( rc!=SQLITE_OK ){ |
| 2232 | goto op_column_out; |
| 2233 | } |
| 2234 | zData = sMem.z; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 2235 | sqlite3VdbeSerialGet((u8*)zData, aType[p2], pDest); |
danielk1977 | 7701e81 | 2005-01-10 12:59:51 +0000 | [diff] [blame] | 2236 | } |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 2237 | pDest->enc = encoding; |
danielk1977 | 36963fd | 2005-02-19 08:18:05 +0000 | [diff] [blame] | 2238 | }else{ |
danielk1977 | 60585dd | 2008-01-03 08:08:40 +0000 | [diff] [blame] | 2239 | if( pOp->p4type==P4_MEM ){ |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 2240 | sqlite3VdbeMemShallowCopy(pDest, pOp->p4.pMem, MEM_Static); |
danielk1977 | aee18ef | 2005-03-09 12:26:50 +0000 | [diff] [blame] | 2241 | }else{ |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 2242 | assert( pDest->flags&MEM_Null ); |
danielk1977 | aee18ef | 2005-03-09 12:26:50 +0000 | [diff] [blame] | 2243 | } |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 2244 | } |
drh | febe106 | 2004-08-28 18:17:48 +0000 | [diff] [blame] | 2245 | |
| 2246 | /* If we dynamically allocated space to hold the data (in the |
| 2247 | ** sqlite3VdbeMemFromBtree() call above) then transfer control of that |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 2248 | ** dynamically allocated space over to the pDest structure. |
drh | febe106 | 2004-08-28 18:17:48 +0000 | [diff] [blame] | 2249 | ** This prevents a memory copy. |
| 2250 | */ |
danielk1977 | 5f09613 | 2008-03-28 15:44:09 +0000 | [diff] [blame] | 2251 | if( sMem.zMalloc ){ |
| 2252 | assert( sMem.z==sMem.zMalloc ); |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 2253 | assert( !(pDest->flags & MEM_Dyn) ); |
| 2254 | assert( !(pDest->flags & (MEM_Blob|MEM_Str)) || pDest->z==sMem.z ); |
| 2255 | pDest->flags &= ~(MEM_Ephem|MEM_Static); |
danielk1977 | 5f09613 | 2008-03-28 15:44:09 +0000 | [diff] [blame] | 2256 | pDest->flags |= MEM_Term; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 2257 | pDest->z = sMem.z; |
danielk1977 | 5f09613 | 2008-03-28 15:44:09 +0000 | [diff] [blame] | 2258 | pDest->zMalloc = sMem.zMalloc; |
danielk1977 | b1bc953 | 2004-05-22 03:05:33 +0000 | [diff] [blame] | 2259 | } |
drh | febe106 | 2004-08-28 18:17:48 +0000 | [diff] [blame] | 2260 | |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 2261 | rc = sqlite3VdbeMemMakeWriteable(pDest); |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 2262 | |
danielk1977 | 3c9cc8d | 2005-01-17 03:40:08 +0000 | [diff] [blame] | 2263 | op_column_out: |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 2264 | UPDATE_MAX_BLOBSIZE(pDest); |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 2265 | REGISTER_TRACE(pOp->p3, pDest); |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2266 | break; |
| 2267 | } |
| 2268 | |
danielk1977 | 751de56 | 2008-04-18 09:01:15 +0000 | [diff] [blame] | 2269 | /* Opcode: Affinity P1 P2 * P4 * |
| 2270 | ** |
| 2271 | ** Apply affinities to a range of P2 registers starting with P1. |
| 2272 | ** |
| 2273 | ** P4 is a string that is P2 characters long. The nth character of the |
| 2274 | ** string indicates the column affinity that should be used for the nth |
| 2275 | ** memory cell in the range. |
| 2276 | */ |
| 2277 | case OP_Affinity: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2278 | char *zAffinity; /* The affinity to be applied */ |
| 2279 | Mem *pData0; /* First register to which to apply affinity */ |
| 2280 | Mem *pLast; /* Last register to which to apply affinity */ |
| 2281 | Mem *pRec; /* Current register */ |
danielk1977 | 751de56 | 2008-04-18 09:01:15 +0000 | [diff] [blame] | 2282 | |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2283 | zAffinity = pOp->p4.z; |
| 2284 | pData0 = &p->aMem[pOp->p1]; |
| 2285 | pLast = &pData0[pOp->p2-1]; |
danielk1977 | 751de56 | 2008-04-18 09:01:15 +0000 | [diff] [blame] | 2286 | for(pRec=pData0; pRec<=pLast; pRec++){ |
danielk1977 | b790c6c | 2008-04-18 10:25:24 +0000 | [diff] [blame] | 2287 | ExpandBlob(pRec); |
danielk1977 | 751de56 | 2008-04-18 09:01:15 +0000 | [diff] [blame] | 2288 | applyAffinity(pRec, zAffinity[pRec-pData0], encoding); |
| 2289 | } |
| 2290 | break; |
| 2291 | } |
| 2292 | |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2293 | /* Opcode: MakeRecord P1 P2 P3 P4 * |
drh | 7a224de | 2004-06-02 01:22:02 +0000 | [diff] [blame] | 2294 | ** |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2295 | ** Convert P2 registers beginning with P1 into a single entry |
drh | 7a224de | 2004-06-02 01:22:02 +0000 | [diff] [blame] | 2296 | ** 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] | 2297 | ** in an index. The details of the format are irrelevant as long as |
drh | 1e968a0 | 2008-03-25 00:22:21 +0000 | [diff] [blame] | 2298 | ** the OP_Column opcode can decode the record later. |
| 2299 | ** Refer to source code comments for the details of the record |
drh | 7a224de | 2004-06-02 01:22:02 +0000 | [diff] [blame] | 2300 | ** format. |
| 2301 | ** |
danielk1977 | 751de56 | 2008-04-18 09:01:15 +0000 | [diff] [blame] | 2302 | ** 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] | 2303 | ** string indicates the column affinity that should be used for the nth |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2304 | ** field of the index key. |
drh | 7a224de | 2004-06-02 01:22:02 +0000 | [diff] [blame] | 2305 | ** |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 2306 | ** The mapping from character to affinity is given by the SQLITE_AFF_ |
| 2307 | ** macros defined in sqliteInt.h. |
drh | 7a224de | 2004-06-02 01:22:02 +0000 | [diff] [blame] | 2308 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 2309 | ** If P4 is NULL then all index fields have the affinity NONE. |
drh | 7f057c9 | 2005-06-24 03:53:06 +0000 | [diff] [blame] | 2310 | */ |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2311 | case OP_MakeRecord: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2312 | u8 *zNewRecord; /* A buffer to hold the data for the new record */ |
| 2313 | Mem *pRec; /* The new record */ |
| 2314 | u64 nData; /* Number of bytes of data space */ |
| 2315 | int nHdr; /* Number of bytes of header space */ |
| 2316 | i64 nByte; /* Data space required for this record */ |
| 2317 | int nZero; /* Number of zero bytes at the end of the record */ |
| 2318 | int nVarint; /* Number of bytes in a varint */ |
| 2319 | u32 serial_type; /* Type field */ |
| 2320 | Mem *pData0; /* First field to be combined into the record */ |
| 2321 | Mem *pLast; /* Last field of the record */ |
| 2322 | int nField; /* Number of fields in the record */ |
| 2323 | char *zAffinity; /* The affinity string for the record */ |
| 2324 | int file_format; /* File format to use for encoding */ |
| 2325 | int i; /* Space used in zNewRecord[] */ |
| 2326 | int len; /* Length of a field */ |
| 2327 | |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 2328 | /* Assuming the record contains N fields, the record format looks |
| 2329 | ** like this: |
| 2330 | ** |
drh | 7a224de | 2004-06-02 01:22:02 +0000 | [diff] [blame] | 2331 | ** ------------------------------------------------------------------------ |
| 2332 | ** | hdr-size | type 0 | type 1 | ... | type N-1 | data0 | ... | data N-1 | |
| 2333 | ** ------------------------------------------------------------------------ |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 2334 | ** |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2335 | ** Data(0) is taken from register P1. Data(1) comes from register P1+1 |
| 2336 | ** and so froth. |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 2337 | ** |
| 2338 | ** Each type field is a varint representing the serial type of the |
| 2339 | ** corresponding data element (see sqlite3VdbeSerialType()). The |
drh | 7a224de | 2004-06-02 01:22:02 +0000 | [diff] [blame] | 2340 | ** hdr-size field is also a varint which is the offset from the beginning |
| 2341 | ** of the record to data0. |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 2342 | */ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2343 | nData = 0; /* Number of bytes of data space */ |
| 2344 | nHdr = 0; /* Number of bytes of header space */ |
| 2345 | nByte = 0; /* Data space required for this record */ |
| 2346 | nZero = 0; /* Number of zero bytes at the end of the record */ |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2347 | nField = pOp->p1; |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 2348 | zAffinity = pOp->p4.z; |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 2349 | assert( nField>0 && pOp->p2>0 && pOp->p2+nField<=p->nMem+1 ); |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2350 | pData0 = &p->aMem[nField]; |
| 2351 | nField = pOp->p2; |
| 2352 | pLast = &pData0[nField-1]; |
drh | d946db0 | 2005-12-29 19:23:06 +0000 | [diff] [blame] | 2353 | file_format = p->minWriteFileFormat; |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 2354 | |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 2355 | /* Loop through the elements that will make up the record to figure |
| 2356 | ** out how much space is required for the new record. |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 2357 | */ |
drh | a2a49dc | 2008-01-02 14:28:13 +0000 | [diff] [blame] | 2358 | for(pRec=pData0; pRec<=pLast; pRec++){ |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 2359 | if( zAffinity ){ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 2360 | applyAffinity(pRec, zAffinity[pRec-pData0], encoding); |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 2361 | } |
danielk1977 | d908f5a | 2007-05-11 07:08:28 +0000 | [diff] [blame] | 2362 | if( pRec->flags&MEM_Zero && pRec->n>0 ){ |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 2363 | sqlite3VdbeMemExpandBlob(pRec); |
danielk1977 | d908f5a | 2007-05-11 07:08:28 +0000 | [diff] [blame] | 2364 | } |
drh | d946db0 | 2005-12-29 19:23:06 +0000 | [diff] [blame] | 2365 | serial_type = sqlite3VdbeSerialType(pRec, file_format); |
drh | ae7e151 | 2007-05-02 16:51:59 +0000 | [diff] [blame] | 2366 | len = sqlite3VdbeSerialTypeLen(serial_type); |
| 2367 | nData += len; |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 2368 | nHdr += sqlite3VarintLen(serial_type); |
drh | fdf972a | 2007-05-02 13:30:27 +0000 | [diff] [blame] | 2369 | if( pRec->flags & MEM_Zero ){ |
| 2370 | /* Only pure zero-filled BLOBs can be input to this Opcode. |
| 2371 | ** We do not allow blobs with a prefix and a zero-filled tail. */ |
drh | 8df3284 | 2008-12-09 02:51:23 +0000 | [diff] [blame] | 2372 | nZero += pRec->u.nZero; |
drh | ae7e151 | 2007-05-02 16:51:59 +0000 | [diff] [blame] | 2373 | }else if( len ){ |
drh | fdf972a | 2007-05-02 13:30:27 +0000 | [diff] [blame] | 2374 | nZero = 0; |
| 2375 | } |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 2376 | } |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 2377 | |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 2378 | /* Add the initial header varint and total the size */ |
drh | cb9882a | 2005-03-17 03:15:40 +0000 | [diff] [blame] | 2379 | nHdr += nVarint = sqlite3VarintLen(nHdr); |
| 2380 | if( nVarint<sqlite3VarintLen(nHdr) ){ |
| 2381 | nHdr++; |
| 2382 | } |
drh | fdf972a | 2007-05-02 13:30:27 +0000 | [diff] [blame] | 2383 | nByte = nHdr+nData-nZero; |
drh | bb4957f | 2008-03-20 14:03:29 +0000 | [diff] [blame] | 2384 | if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){ |
drh | 023ae03 | 2007-05-08 12:12:16 +0000 | [diff] [blame] | 2385 | goto too_big; |
| 2386 | } |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 2387 | |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 2388 | /* Make sure the output register has a buffer large enough to store |
| 2389 | ** the new record. The output register (pOp->p3) is not allowed to |
| 2390 | ** be one of the input registers (because the following call to |
| 2391 | ** sqlite3VdbeMemGrow() could clobber the value before it is used). |
| 2392 | */ |
| 2393 | assert( pOp->p3<pOp->p1 || pOp->p3>=pOp->p1+pOp->p2 ); |
| 2394 | pOut = &p->aMem[pOp->p3]; |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 2395 | if( sqlite3VdbeMemGrow(pOut, (int)nByte, 0) ){ |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 2396 | goto no_mem; |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 2397 | } |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 2398 | zNewRecord = (u8 *)pOut->z; |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 2399 | |
| 2400 | /* Write the record */ |
shane | 3f8d5cf | 2008-04-24 19:15:09 +0000 | [diff] [blame] | 2401 | i = putVarint32(zNewRecord, nHdr); |
drh | a2a49dc | 2008-01-02 14:28:13 +0000 | [diff] [blame] | 2402 | for(pRec=pData0; pRec<=pLast; pRec++){ |
drh | d946db0 | 2005-12-29 19:23:06 +0000 | [diff] [blame] | 2403 | serial_type = sqlite3VdbeSerialType(pRec, file_format); |
shane | 3f8d5cf | 2008-04-24 19:15:09 +0000 | [diff] [blame] | 2404 | i += putVarint32(&zNewRecord[i], serial_type); /* serial type */ |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 2405 | } |
drh | a2a49dc | 2008-01-02 14:28:13 +0000 | [diff] [blame] | 2406 | for(pRec=pData0; pRec<=pLast; pRec++){ /* serial data */ |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 2407 | i += sqlite3VdbeSerialPut(&zNewRecord[i], (int)(nByte-i), pRec,file_format); |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 2408 | } |
drh | fdf972a | 2007-05-02 13:30:27 +0000 | [diff] [blame] | 2409 | assert( i==nByte ); |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 2410 | |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2411 | assert( pOp->p3>0 && pOp->p3<=p->nMem ); |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 2412 | pOut->n = (int)nByte; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 2413 | pOut->flags = MEM_Blob | MEM_Dyn; |
| 2414 | pOut->xDel = 0; |
drh | fdf972a | 2007-05-02 13:30:27 +0000 | [diff] [blame] | 2415 | if( nZero ){ |
drh | 8df3284 | 2008-12-09 02:51:23 +0000 | [diff] [blame] | 2416 | pOut->u.nZero = nZero; |
drh | 477df4b | 2008-01-05 18:48:24 +0000 | [diff] [blame] | 2417 | pOut->flags |= MEM_Zero; |
drh | fdf972a | 2007-05-02 13:30:27 +0000 | [diff] [blame] | 2418 | } |
drh | 477df4b | 2008-01-05 18:48:24 +0000 | [diff] [blame] | 2419 | pOut->enc = SQLITE_UTF8; /* In case the blob is ever converted to text */ |
drh | 1013c93 | 2008-01-06 00:25:21 +0000 | [diff] [blame] | 2420 | REGISTER_TRACE(pOp->p3, pOut); |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 2421 | UPDATE_MAX_BLOBSIZE(pOut); |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 2422 | break; |
| 2423 | } |
| 2424 | |
danielk1977 | a553316 | 2009-02-24 10:01:51 +0000 | [diff] [blame] | 2425 | /* Opcode: Count P1 P2 * * * |
| 2426 | ** |
| 2427 | ** Store the number of entries (an integer value) in the table or index |
| 2428 | ** opened by cursor P1 in register P2 |
| 2429 | */ |
| 2430 | #ifndef SQLITE_OMIT_BTREECOUNT |
| 2431 | case OP_Count: { /* out2-prerelease */ |
| 2432 | i64 nEntry; |
drh | c54a617 | 2009-06-02 16:06:03 +0000 | [diff] [blame] | 2433 | BtCursor *pCrsr; |
| 2434 | |
| 2435 | pCrsr = p->apCsr[pOp->p1]->pCursor; |
drh | 818e39a | 2009-04-02 20:27:28 +0000 | [diff] [blame] | 2436 | if( pCrsr ){ |
| 2437 | rc = sqlite3BtreeCount(pCrsr, &nEntry); |
| 2438 | }else{ |
| 2439 | nEntry = 0; |
| 2440 | } |
danielk1977 | a553316 | 2009-02-24 10:01:51 +0000 | [diff] [blame] | 2441 | pOut->flags = MEM_Int; |
| 2442 | pOut->u.i = nEntry; |
| 2443 | break; |
| 2444 | } |
| 2445 | #endif |
| 2446 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 2447 | /* Opcode: Statement P1 * * * * |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2448 | ** |
drh | 7f0f12e | 2004-05-21 13:39:50 +0000 | [diff] [blame] | 2449 | ** Begin an individual statement transaction which is part of a larger |
drh | 82ed1e5 | 2008-04-25 12:25:42 +0000 | [diff] [blame] | 2450 | ** transaction. This is needed so that the statement |
drh | 7f0f12e | 2004-05-21 13:39:50 +0000 | [diff] [blame] | 2451 | ** can be rolled back after an error without having to roll back the |
| 2452 | ** entire transaction. The statement transaction will automatically |
| 2453 | ** commit when the VDBE halts. |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2454 | ** |
drh | 82ed1e5 | 2008-04-25 12:25:42 +0000 | [diff] [blame] | 2455 | ** If the database connection is currently in autocommit mode (that |
| 2456 | ** is to say, if it is in between BEGIN and COMMIT) |
| 2457 | ** and if there are no other active statements on the same database |
| 2458 | ** connection, then this operation is a no-op. No statement transaction |
| 2459 | ** is needed since any error can use the normal ROLLBACK process to |
| 2460 | ** undo changes. |
| 2461 | ** |
| 2462 | ** If a statement transaction is started, then a statement journal file |
| 2463 | ** will be allocated and initialized. |
| 2464 | ** |
drh | 7f0f12e | 2004-05-21 13:39:50 +0000 | [diff] [blame] | 2465 | ** The statement is begun on the database file with index P1. The main |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2466 | ** database file has an index of 0 and the file used for temporary tables |
| 2467 | ** has an index of 1. |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2468 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2469 | case OP_Statement: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2470 | Btree *pBt; |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 2471 | if( db->autoCommit==0 || db->activeVdbeCnt>1 ){ |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 2472 | assert( pOp->p1>=0 && pOp->p1<db->nDb ); |
| 2473 | assert( db->aDb[pOp->p1].pBt!=0 ); |
| 2474 | pBt = db->aDb[pOp->p1].pBt; |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 2475 | assert( sqlite3BtreeIsInTrans(pBt) ); |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 2476 | assert( (p->btreeMask & (1<<pOp->p1))!=0 ); |
danielk1977 | bd43455 | 2009-03-18 10:33:00 +0000 | [diff] [blame] | 2477 | if( p->iStatement==0 ){ |
| 2478 | assert( db->nStatement>=0 && db->nSavepoint>=0 ); |
| 2479 | db->nStatement++; |
| 2480 | p->iStatement = db->nSavepoint + db->nStatement; |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 2481 | } |
danielk1977 | bd43455 | 2009-03-18 10:33:00 +0000 | [diff] [blame] | 2482 | rc = sqlite3BtreeBeginStmt(pBt, p->iStatement); |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 2483 | } |
| 2484 | break; |
| 2485 | } |
| 2486 | |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2487 | /* Opcode: Savepoint P1 * * P4 * |
| 2488 | ** |
| 2489 | ** Open, release or rollback the savepoint named by parameter P4, depending |
| 2490 | ** on the value of P1. To open a new savepoint, P1==0. To release (commit) an |
| 2491 | ** existing savepoint, P1==1, or to rollback an existing savepoint P1==2. |
| 2492 | */ |
| 2493 | case OP_Savepoint: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2494 | int p1; /* Value of P1 operand */ |
| 2495 | char *zName; /* Name of savepoint */ |
| 2496 | int nName; |
| 2497 | Savepoint *pNew; |
| 2498 | Savepoint *pSavepoint; |
| 2499 | Savepoint *pTmp; |
| 2500 | int iSavepoint; |
| 2501 | int ii; |
| 2502 | |
| 2503 | p1 = pOp->p1; |
| 2504 | zName = pOp->p4.z; |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2505 | |
| 2506 | /* Assert that the p1 parameter is valid. Also that if there is no open |
| 2507 | ** transaction, then there cannot be any savepoints. |
| 2508 | */ |
| 2509 | assert( db->pSavepoint==0 || db->autoCommit==0 ); |
| 2510 | assert( p1==SAVEPOINT_BEGIN||p1==SAVEPOINT_RELEASE||p1==SAVEPOINT_ROLLBACK ); |
| 2511 | assert( db->pSavepoint || db->isTransactionSavepoint==0 ); |
| 2512 | assert( checkSavepointCount(db) ); |
| 2513 | |
| 2514 | if( p1==SAVEPOINT_BEGIN ){ |
danielk1977 | 34cf35d | 2008-12-18 18:31:38 +0000 | [diff] [blame] | 2515 | if( db->writeVdbeCnt>0 ){ |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2516 | /* A new savepoint cannot be created if there are active write |
| 2517 | ** statements (i.e. open read/write incremental blob handles). |
| 2518 | */ |
| 2519 | sqlite3SetString(&p->zErrMsg, db, "cannot open savepoint - " |
| 2520 | "SQL statements in progress"); |
| 2521 | rc = SQLITE_BUSY; |
| 2522 | }else{ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2523 | nName = sqlite3Strlen30(zName); |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2524 | |
| 2525 | /* Create a new savepoint structure. */ |
| 2526 | pNew = sqlite3DbMallocRaw(db, sizeof(Savepoint)+nName+1); |
| 2527 | if( pNew ){ |
| 2528 | pNew->zName = (char *)&pNew[1]; |
| 2529 | memcpy(pNew->zName, zName, nName+1); |
| 2530 | |
| 2531 | /* If there is no open transaction, then mark this as a special |
| 2532 | ** "transaction savepoint". */ |
| 2533 | if( db->autoCommit ){ |
| 2534 | db->autoCommit = 0; |
| 2535 | db->isTransactionSavepoint = 1; |
| 2536 | }else{ |
| 2537 | db->nSavepoint++; |
danielk1977 | d829335 | 2009-04-30 09:10:37 +0000 | [diff] [blame] | 2538 | } |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2539 | |
| 2540 | /* Link the new savepoint into the database handle's list. */ |
| 2541 | pNew->pNext = db->pSavepoint; |
| 2542 | db->pSavepoint = pNew; |
| 2543 | } |
| 2544 | } |
| 2545 | }else{ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2546 | iSavepoint = 0; |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2547 | |
| 2548 | /* Find the named savepoint. If there is no such savepoint, then an |
| 2549 | ** an error is returned to the user. */ |
| 2550 | for( |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2551 | pSavepoint = db->pSavepoint; |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2552 | pSavepoint && sqlite3StrICmp(pSavepoint->zName, zName); |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2553 | pSavepoint = pSavepoint->pNext |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2554 | ){ |
| 2555 | iSavepoint++; |
| 2556 | } |
| 2557 | if( !pSavepoint ){ |
| 2558 | sqlite3SetString(&p->zErrMsg, db, "no such savepoint: %s", zName); |
| 2559 | rc = SQLITE_ERROR; |
| 2560 | }else if( |
| 2561 | db->writeVdbeCnt>0 || (p1==SAVEPOINT_ROLLBACK && db->activeVdbeCnt>1) |
| 2562 | ){ |
| 2563 | /* It is not possible to release (commit) a savepoint if there are |
| 2564 | ** active write statements. It is not possible to rollback a savepoint |
| 2565 | ** if there are any active statements at all. |
| 2566 | */ |
| 2567 | sqlite3SetString(&p->zErrMsg, db, |
| 2568 | "cannot %s savepoint - SQL statements in progress", |
| 2569 | (p1==SAVEPOINT_ROLLBACK ? "rollback": "release") |
| 2570 | ); |
| 2571 | rc = SQLITE_BUSY; |
| 2572 | }else{ |
| 2573 | |
| 2574 | /* Determine whether or not this is a transaction savepoint. If so, |
danielk1977 | 34cf35d | 2008-12-18 18:31:38 +0000 | [diff] [blame] | 2575 | ** and this is a RELEASE command, then the current transaction |
| 2576 | ** is committed. |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2577 | */ |
| 2578 | int isTransaction = pSavepoint->pNext==0 && db->isTransactionSavepoint; |
| 2579 | if( isTransaction && p1==SAVEPOINT_RELEASE ){ |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2580 | db->autoCommit = 1; |
| 2581 | if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){ |
| 2582 | p->pc = pc; |
| 2583 | db->autoCommit = 0; |
| 2584 | p->rc = rc = SQLITE_BUSY; |
| 2585 | goto vdbe_return; |
| 2586 | } |
danielk1977 | 34cf35d | 2008-12-18 18:31:38 +0000 | [diff] [blame] | 2587 | db->isTransactionSavepoint = 0; |
| 2588 | rc = p->rc; |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2589 | }else{ |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2590 | iSavepoint = db->nSavepoint - iSavepoint - 1; |
| 2591 | for(ii=0; ii<db->nDb; ii++){ |
| 2592 | rc = sqlite3BtreeSavepoint(db->aDb[ii].pBt, p1, iSavepoint); |
| 2593 | if( rc!=SQLITE_OK ){ |
| 2594 | goto abort_due_to_error; |
danielk1977 | bd43455 | 2009-03-18 10:33:00 +0000 | [diff] [blame] | 2595 | } |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2596 | } |
drh | 9f0bbf9 | 2009-01-02 21:08:09 +0000 | [diff] [blame] | 2597 | if( p1==SAVEPOINT_ROLLBACK && (db->flags&SQLITE_InternChanges)!=0 ){ |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2598 | sqlite3ExpirePreparedStatements(db); |
| 2599 | sqlite3ResetInternalSchema(db, 0); |
| 2600 | } |
| 2601 | } |
| 2602 | |
| 2603 | /* Regardless of whether this is a RELEASE or ROLLBACK, destroy all |
| 2604 | ** savepoints nested inside of the savepoint being operated on. */ |
| 2605 | while( db->pSavepoint!=pSavepoint ){ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2606 | pTmp = db->pSavepoint; |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2607 | db->pSavepoint = pTmp->pNext; |
| 2608 | sqlite3DbFree(db, pTmp); |
| 2609 | db->nSavepoint--; |
| 2610 | } |
| 2611 | |
| 2612 | /* If it is a RELEASE, then destroy the savepoint being operated on too */ |
| 2613 | if( p1==SAVEPOINT_RELEASE ){ |
| 2614 | assert( pSavepoint==db->pSavepoint ); |
| 2615 | db->pSavepoint = pSavepoint->pNext; |
| 2616 | sqlite3DbFree(db, pSavepoint); |
| 2617 | if( !isTransaction ){ |
| 2618 | db->nSavepoint--; |
| 2619 | } |
| 2620 | } |
| 2621 | } |
| 2622 | } |
| 2623 | |
| 2624 | break; |
| 2625 | } |
| 2626 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 2627 | /* Opcode: AutoCommit P1 P2 * * * |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 2628 | ** |
| 2629 | ** 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] | 2630 | ** back any currently active btree transactions. If there are any active |
drh | c25eabe | 2009-02-24 18:57:31 +0000 | [diff] [blame] | 2631 | ** VMs (apart from this one), then a ROLLBACK fails. A COMMIT fails if |
| 2632 | ** there are active writing VMs or active VMs that use shared cache. |
drh | 92f02c3 | 2004-09-02 14:57:08 +0000 | [diff] [blame] | 2633 | ** |
| 2634 | ** This instruction causes the VM to halt. |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 2635 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2636 | case OP_AutoCommit: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2637 | int desiredAutoCommit; |
shane | 68c0273 | 2009-06-09 18:14:18 +0000 | [diff] [blame] | 2638 | int iRollback; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2639 | int turnOnAC; |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 2640 | |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2641 | desiredAutoCommit = pOp->p1; |
shane | 68c0273 | 2009-06-09 18:14:18 +0000 | [diff] [blame] | 2642 | iRollback = pOp->p2; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2643 | turnOnAC = desiredAutoCommit && !db->autoCommit; |
drh | ad4a4b8 | 2008-11-05 16:37:34 +0000 | [diff] [blame] | 2644 | assert( desiredAutoCommit==1 || desiredAutoCommit==0 ); |
shane | 68c0273 | 2009-06-09 18:14:18 +0000 | [diff] [blame] | 2645 | assert( desiredAutoCommit==1 || iRollback==0 ); |
drh | 92f02c3 | 2004-09-02 14:57:08 +0000 | [diff] [blame] | 2646 | assert( db->activeVdbeCnt>0 ); /* At least this one VM is active */ |
danielk1977 | 46c43ed | 2004-06-30 06:30:25 +0000 | [diff] [blame] | 2647 | |
shane | 68c0273 | 2009-06-09 18:14:18 +0000 | [diff] [blame] | 2648 | if( turnOnAC && iRollback && db->activeVdbeCnt>1 ){ |
drh | ad4a4b8 | 2008-11-05 16:37:34 +0000 | [diff] [blame] | 2649 | /* If this instruction implements a ROLLBACK and other VMs are |
danielk1977 | 46c43ed | 2004-06-30 06:30:25 +0000 | [diff] [blame] | 2650 | ** still running, and a transaction is active, return an error indicating |
| 2651 | ** that the other VMs must complete first. |
| 2652 | */ |
drh | ad4a4b8 | 2008-11-05 16:37:34 +0000 | [diff] [blame] | 2653 | sqlite3SetString(&p->zErrMsg, db, "cannot rollback transaction - " |
| 2654 | "SQL statements in progress"); |
drh | 99dfe5e | 2008-10-30 15:03:15 +0000 | [diff] [blame] | 2655 | rc = SQLITE_BUSY; |
drh | 9eb8cbe | 2009-06-19 22:23:41 +0000 | [diff] [blame] | 2656 | }else if( turnOnAC && !iRollback && db->writeVdbeCnt>0 ){ |
drh | ad4a4b8 | 2008-11-05 16:37:34 +0000 | [diff] [blame] | 2657 | /* If this instruction implements a COMMIT and other VMs are writing |
| 2658 | ** return an error indicating that the other VMs must complete first. |
| 2659 | */ |
| 2660 | sqlite3SetString(&p->zErrMsg, db, "cannot commit transaction - " |
| 2661 | "SQL statements in progress"); |
| 2662 | rc = SQLITE_BUSY; |
| 2663 | }else if( desiredAutoCommit!=db->autoCommit ){ |
shane | 68c0273 | 2009-06-09 18:14:18 +0000 | [diff] [blame] | 2664 | if( iRollback ){ |
drh | ad4a4b8 | 2008-11-05 16:37:34 +0000 | [diff] [blame] | 2665 | assert( desiredAutoCommit==1 ); |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 2666 | sqlite3RollbackAll(db); |
danielk1977 | f3f06bb | 2005-12-16 15:24:28 +0000 | [diff] [blame] | 2667 | db->autoCommit = 1; |
| 2668 | }else{ |
shane | 7d3846a | 2008-12-11 02:58:26 +0000 | [diff] [blame] | 2669 | db->autoCommit = (u8)desiredAutoCommit; |
danielk1977 | f3f06bb | 2005-12-16 15:24:28 +0000 | [diff] [blame] | 2670 | if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){ |
danielk1977 | f3f06bb | 2005-12-16 15:24:28 +0000 | [diff] [blame] | 2671 | p->pc = pc; |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 2672 | db->autoCommit = (u8)(1-desiredAutoCommit); |
drh | 900b31e | 2007-08-28 02:27:51 +0000 | [diff] [blame] | 2673 | p->rc = rc = SQLITE_BUSY; |
| 2674 | goto vdbe_return; |
danielk1977 | f3f06bb | 2005-12-16 15:24:28 +0000 | [diff] [blame] | 2675 | } |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 2676 | } |
danielk1977 | bd43455 | 2009-03-18 10:33:00 +0000 | [diff] [blame] | 2677 | assert( db->nStatement==0 ); |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2678 | sqlite3CloseSavepoints(db); |
drh | 83968c4 | 2007-04-18 16:45:24 +0000 | [diff] [blame] | 2679 | if( p->rc==SQLITE_OK ){ |
drh | 900b31e | 2007-08-28 02:27:51 +0000 | [diff] [blame] | 2680 | rc = SQLITE_DONE; |
drh | 83968c4 | 2007-04-18 16:45:24 +0000 | [diff] [blame] | 2681 | }else{ |
drh | 900b31e | 2007-08-28 02:27:51 +0000 | [diff] [blame] | 2682 | rc = SQLITE_ERROR; |
drh | 83968c4 | 2007-04-18 16:45:24 +0000 | [diff] [blame] | 2683 | } |
drh | 900b31e | 2007-08-28 02:27:51 +0000 | [diff] [blame] | 2684 | goto vdbe_return; |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 2685 | }else{ |
drh | f089aa4 | 2008-07-08 19:34:06 +0000 | [diff] [blame] | 2686 | sqlite3SetString(&p->zErrMsg, db, |
drh | ad4a4b8 | 2008-11-05 16:37:34 +0000 | [diff] [blame] | 2687 | (!desiredAutoCommit)?"cannot start a transaction within a transaction":( |
shane | 68c0273 | 2009-06-09 18:14:18 +0000 | [diff] [blame] | 2688 | (iRollback)?"cannot rollback - no transaction is active": |
drh | f089aa4 | 2008-07-08 19:34:06 +0000 | [diff] [blame] | 2689 | "cannot commit - no transaction is active")); |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 2690 | |
| 2691 | rc = SQLITE_ERROR; |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2692 | } |
| 2693 | break; |
| 2694 | } |
| 2695 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 2696 | /* Opcode: Transaction P1 P2 * * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2697 | ** |
| 2698 | ** Begin a transaction. The transaction ends when a Commit or Rollback |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2699 | ** opcode is encountered. Depending on the ON CONFLICT setting, the |
| 2700 | ** transaction might also be rolled back if an error is encountered. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2701 | ** |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2702 | ** P1 is the index of the database file on which the transaction is |
| 2703 | ** started. Index 0 is the main database file and index 1 is the |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 2704 | ** file used for temporary tables. Indices of 2 or more are used for |
| 2705 | ** attached databases. |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 2706 | ** |
drh | 8024205 | 2004-06-09 00:48:12 +0000 | [diff] [blame] | 2707 | ** 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] | 2708 | ** obtained on the database file when a write-transaction is started. No |
drh | 8024205 | 2004-06-09 00:48:12 +0000 | [diff] [blame] | 2709 | ** other process can start another write transaction while this transaction is |
| 2710 | ** underway. Starting a write transaction also creates a rollback journal. A |
| 2711 | ** write transaction must be started before any changes can be made to the |
drh | 684917c | 2004-10-05 02:41:42 +0000 | [diff] [blame] | 2712 | ** database. If P2 is 2 or greater then an EXCLUSIVE lock is also obtained |
| 2713 | ** on the file. |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 2714 | ** |
| 2715 | ** 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] | 2716 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2717 | case OP_Transaction: { |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 2718 | Btree *pBt; |
| 2719 | |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 2720 | assert( pOp->p1>=0 && pOp->p1<db->nDb ); |
| 2721 | assert( (p->btreeMask & (1<<pOp->p1))!=0 ); |
| 2722 | pBt = db->aDb[pOp->p1].pBt; |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 2723 | |
danielk1977 | 24162fe | 2004-06-04 06:22:00 +0000 | [diff] [blame] | 2724 | if( pBt ){ |
danielk1977 | 40b38dc | 2004-06-26 08:38:24 +0000 | [diff] [blame] | 2725 | rc = sqlite3BtreeBeginTrans(pBt, pOp->p2); |
danielk1977 | 24162fe | 2004-06-04 06:22:00 +0000 | [diff] [blame] | 2726 | if( rc==SQLITE_BUSY ){ |
danielk1977 | 2a764eb | 2004-06-12 01:43:26 +0000 | [diff] [blame] | 2727 | p->pc = pc; |
drh | 900b31e | 2007-08-28 02:27:51 +0000 | [diff] [blame] | 2728 | p->rc = rc = SQLITE_BUSY; |
drh | 900b31e | 2007-08-28 02:27:51 +0000 | [diff] [blame] | 2729 | goto vdbe_return; |
danielk1977 | 24162fe | 2004-06-04 06:22:00 +0000 | [diff] [blame] | 2730 | } |
danielk1977 | 2ef6848 | 2008-07-07 17:13:08 +0000 | [diff] [blame] | 2731 | if( rc!=SQLITE_OK && rc!=SQLITE_READONLY /* && rc!=SQLITE_BUSY */ ){ |
danielk1977 | 24162fe | 2004-06-04 06:22:00 +0000 | [diff] [blame] | 2732 | goto abort_due_to_error; |
drh | 90bfcda | 2001-09-23 19:46:51 +0000 | [diff] [blame] | 2733 | } |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 2734 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2735 | break; |
| 2736 | } |
| 2737 | |
drh | b1fdb2a | 2008-01-05 04:06:03 +0000 | [diff] [blame] | 2738 | /* Opcode: ReadCookie P1 P2 P3 * * |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 2739 | ** |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2740 | ** Read cookie number P3 from database P1 and write it into register P2. |
danielk1977 | 0d19f7a | 2009-06-03 11:25:07 +0000 | [diff] [blame] | 2741 | ** P3==1 is the schema version. P3==2 is the database format. |
| 2742 | ** 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] | 2743 | ** the main database file and P1==1 is the database file used to store |
| 2744 | ** temporary tables. |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 2745 | ** |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 2746 | ** There must be a read-lock on the database (either a transaction |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2747 | ** must be started or there must be an open cursor) before |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 2748 | ** executing this instruction. |
| 2749 | */ |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 2750 | case OP_ReadCookie: { /* out2-prerelease */ |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 2751 | int iMeta; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2752 | int iDb; |
| 2753 | int iCookie; |
danielk1977 | 180b56a | 2007-06-24 08:00:42 +0000 | [diff] [blame] | 2754 | |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2755 | iDb = pOp->p1; |
| 2756 | iCookie = pOp->p3; |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 2757 | assert( pOp->p3<SQLITE_N_BTREE_META ); |
danielk1977 | 180b56a | 2007-06-24 08:00:42 +0000 | [diff] [blame] | 2758 | assert( iDb>=0 && iDb<db->nDb ); |
| 2759 | assert( db->aDb[iDb].pBt!=0 ); |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 2760 | assert( (p->btreeMask & (1<<iDb))!=0 ); |
danielk1977 | 0d19f7a | 2009-06-03 11:25:07 +0000 | [diff] [blame] | 2761 | |
| 2762 | rc = sqlite3BtreeGetMeta(db->aDb[iDb].pBt, iCookie, (u32 *)&iMeta); |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 2763 | pOut->u.i = iMeta; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 2764 | MemSetTypeFlag(pOut, MEM_Int); |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 2765 | break; |
| 2766 | } |
| 2767 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 2768 | /* Opcode: SetCookie P1 P2 P3 * * |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 2769 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 2770 | ** Write the content of register P3 (interpreted as an integer) |
danielk1977 | 0d19f7a | 2009-06-03 11:25:07 +0000 | [diff] [blame] | 2771 | ** into cookie number P2 of database P1. P2==1 is the schema version. |
| 2772 | ** P2==2 is the database format. P2==3 is the recommended pager cache |
| 2773 | ** size, and so forth. P1==0 is the main database file and P1==1 is the |
| 2774 | ** database file used to store temporary tables. |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 2775 | ** |
| 2776 | ** A transaction must be started before executing this opcode. |
| 2777 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2778 | case OP_SetCookie: { /* in3 */ |
drh | 3f7d4e4 | 2004-07-24 14:35:58 +0000 | [diff] [blame] | 2779 | Db *pDb; |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 2780 | assert( pOp->p2<SQLITE_N_BTREE_META ); |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2781 | assert( pOp->p1>=0 && pOp->p1<db->nDb ); |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 2782 | assert( (p->btreeMask & (1<<pOp->p1))!=0 ); |
drh | 3f7d4e4 | 2004-07-24 14:35:58 +0000 | [diff] [blame] | 2783 | pDb = &db->aDb[pOp->p1]; |
| 2784 | assert( pDb->pBt!=0 ); |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 2785 | sqlite3VdbeMemIntegerify(pIn3); |
drh | a3b321d | 2004-05-11 09:31:31 +0000 | [diff] [blame] | 2786 | /* See note about index shifting on OP_ReadCookie */ |
danielk1977 | 0d19f7a | 2009-06-03 11:25:07 +0000 | [diff] [blame] | 2787 | rc = sqlite3BtreeUpdateMeta(pDb->pBt, pOp->p2, (int)pIn3->u.i); |
| 2788 | if( pOp->p2==BTREE_SCHEMA_VERSION ){ |
drh | 3f7d4e4 | 2004-07-24 14:35:58 +0000 | [diff] [blame] | 2789 | /* When the schema cookie changes, record the new cookie internally */ |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 2790 | pDb->pSchema->schema_cookie = (int)pIn3->u.i; |
drh | 3f7d4e4 | 2004-07-24 14:35:58 +0000 | [diff] [blame] | 2791 | db->flags |= SQLITE_InternChanges; |
danielk1977 | 0d19f7a | 2009-06-03 11:25:07 +0000 | [diff] [blame] | 2792 | }else if( pOp->p2==BTREE_FILE_FORMAT ){ |
drh | d28bcb3 | 2005-12-21 14:43:11 +0000 | [diff] [blame] | 2793 | /* Record changes in the file format */ |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 2794 | pDb->pSchema->file_format = (u8)pIn3->u.i; |
drh | 3f7d4e4 | 2004-07-24 14:35:58 +0000 | [diff] [blame] | 2795 | } |
drh | fd426c6 | 2006-01-30 15:34:22 +0000 | [diff] [blame] | 2796 | if( pOp->p1==1 ){ |
| 2797 | /* Invalidate all prepared statements whenever the TEMP database |
| 2798 | ** schema is changed. Ticket #1644 */ |
| 2799 | sqlite3ExpirePreparedStatements(db); |
| 2800 | } |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 2801 | break; |
| 2802 | } |
| 2803 | |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 2804 | /* Opcode: VerifyCookie P1 P2 * |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 2805 | ** |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2806 | ** Check the value of global database parameter number 0 (the |
| 2807 | ** schema version) and make sure it is equal to P2. |
| 2808 | ** P1 is the database number which is 0 for the main database file |
| 2809 | ** and 1 for the file holding temporary tables and some higher number |
| 2810 | ** for auxiliary databases. |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 2811 | ** |
| 2812 | ** The cookie changes its value whenever the database schema changes. |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2813 | ** This operation is used to detect when that the cookie has changed |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 2814 | ** and that the current process needs to reread the schema. |
| 2815 | ** |
| 2816 | ** Either a transaction needs to have been started or an OP_Open needs |
| 2817 | ** to be executed (to establish a read lock) before this opcode is |
| 2818 | ** invoked. |
| 2819 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2820 | case OP_VerifyCookie: { |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 2821 | int iMeta; |
drh | c275b4e | 2004-07-19 17:25:24 +0000 | [diff] [blame] | 2822 | Btree *pBt; |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2823 | assert( pOp->p1>=0 && pOp->p1<db->nDb ); |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 2824 | assert( (p->btreeMask & (1<<pOp->p1))!=0 ); |
drh | c275b4e | 2004-07-19 17:25:24 +0000 | [diff] [blame] | 2825 | pBt = db->aDb[pOp->p1].pBt; |
| 2826 | if( pBt ){ |
danielk1977 | 0d19f7a | 2009-06-03 11:25:07 +0000 | [diff] [blame] | 2827 | rc = sqlite3BtreeGetMeta(pBt, BTREE_SCHEMA_VERSION, (u32 *)&iMeta); |
drh | c275b4e | 2004-07-19 17:25:24 +0000 | [diff] [blame] | 2828 | }else{ |
| 2829 | rc = SQLITE_OK; |
| 2830 | iMeta = 0; |
| 2831 | } |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 2832 | if( rc==SQLITE_OK && iMeta!=pOp->p2 ){ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 2833 | sqlite3DbFree(db, p->zErrMsg); |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 2834 | p->zErrMsg = sqlite3DbStrDup(db, "database schema has changed"); |
danielk1977 | 896e792 | 2007-04-17 08:32:33 +0000 | [diff] [blame] | 2835 | /* If the schema-cookie from the database file matches the cookie |
| 2836 | ** stored with the in-memory representation of the schema, do |
| 2837 | ** not reload the schema from the database file. |
| 2838 | ** |
shane | 21e7feb | 2008-05-30 15:59:49 +0000 | [diff] [blame] | 2839 | ** If virtual-tables are in use, this is not just an optimization. |
danielk1977 | 896e792 | 2007-04-17 08:32:33 +0000 | [diff] [blame] | 2840 | ** Often, v-tables store their data in other SQLite tables, which |
| 2841 | ** are queried from within xNext() and other v-table methods using |
| 2842 | ** prepared queries. If such a query is out-of-date, we do not want to |
| 2843 | ** discard the database schema, as the user code implementing the |
| 2844 | ** v-table would have to be ready for the sqlite3_vtab structure itself |
| 2845 | ** to be invalidated whenever sqlite3_step() is called from within |
| 2846 | ** a v-table method. |
| 2847 | */ |
| 2848 | if( db->aDb[pOp->p1].pSchema->schema_cookie!=iMeta ){ |
| 2849 | sqlite3ResetInternalSchema(db, pOp->p1); |
| 2850 | } |
| 2851 | |
drh | f6d8ab8 | 2007-01-12 23:43:42 +0000 | [diff] [blame] | 2852 | sqlite3ExpirePreparedStatements(db); |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 2853 | rc = SQLITE_SCHEMA; |
| 2854 | } |
| 2855 | break; |
| 2856 | } |
| 2857 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 2858 | /* Opcode: OpenRead P1 P2 P3 P4 P5 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2859 | ** |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2860 | ** Open a read-only cursor for the database table whose root page is |
danielk1977 | 207872a | 2008-01-03 07:54:23 +0000 | [diff] [blame] | 2861 | ** P2 in a database file. The database file is determined by P3. |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 2862 | ** P3==0 means the main database, P3==1 means the database used for |
| 2863 | ** temporary tables, and P3>1 means used the corresponding attached |
| 2864 | ** database. Give the new cursor an identifier of P1. The P1 |
danielk1977 | 207872a | 2008-01-03 07:54:23 +0000 | [diff] [blame] | 2865 | ** values need not be contiguous but all P1 values should be small integers. |
| 2866 | ** It is an error for P1 to be negative. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2867 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 2868 | ** If P5!=0 then use the content of register P2 as the root page, not |
| 2869 | ** the value of P2 itself. |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 2870 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2871 | ** There will be a read lock on the database whenever there is an |
| 2872 | ** open cursor. If the database was unlocked prior to this instruction |
| 2873 | ** then a read lock is acquired as part of this instruction. A read |
| 2874 | ** lock allows other processes to read the database but prohibits |
| 2875 | ** any other process from modifying the database. The read lock is |
| 2876 | ** released when all cursors are closed. If this instruction attempts |
| 2877 | ** to get a read lock but fails, the script terminates with an |
| 2878 | ** SQLITE_BUSY error code. |
| 2879 | ** |
danielk1977 | d336e22 | 2009-02-20 10:58:41 +0000 | [diff] [blame] | 2880 | ** The P4 value may be either an integer (P4_INT32) or a pointer to |
| 2881 | ** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo |
| 2882 | ** structure, then said structure defines the content and collating |
| 2883 | ** sequence of the index being opened. Otherwise, if P4 is an integer |
| 2884 | ** value, it is set to the number of columns in the table. |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 2885 | ** |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2886 | ** See also OpenWrite. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2887 | */ |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 2888 | /* Opcode: OpenWrite P1 P2 P3 P4 P5 |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2889 | ** |
| 2890 | ** 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] | 2891 | ** page is P2. Or if P5!=0 use the content of register P2 to find the |
| 2892 | ** root page. |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2893 | ** |
danielk1977 | d336e22 | 2009-02-20 10:58:41 +0000 | [diff] [blame] | 2894 | ** The P4 value may be either an integer (P4_INT32) or a pointer to |
| 2895 | ** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo |
| 2896 | ** structure, then said structure defines the content and collating |
| 2897 | ** sequence of the index being opened. Otherwise, if P4 is an integer |
drh | 35cd643 | 2009-06-05 14:17:21 +0000 | [diff] [blame] | 2898 | ** value, it is set to the number of columns in the table, or to the |
| 2899 | ** largest index of any column of the table that is actually used. |
jplyon | 5a56422 | 2003-06-02 06:15:58 +0000 | [diff] [blame] | 2900 | ** |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2901 | ** This instruction works just like OpenRead except that it opens the cursor |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2902 | ** in read/write mode. For a given table, there can be one or more read-only |
| 2903 | ** cursors or a single read/write cursor but not both. |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 2904 | ** |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2905 | ** See also OpenRead. |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2906 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2907 | case OP_OpenRead: |
| 2908 | case OP_OpenWrite: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2909 | int nField; |
| 2910 | KeyInfo *pKeyInfo; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2911 | int p2; |
| 2912 | int iDb; |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 2913 | int wrFlag; |
| 2914 | Btree *pX; |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 2915 | VdbeCursor *pCur; |
drh | d946db0 | 2005-12-29 19:23:06 +0000 | [diff] [blame] | 2916 | Db *pDb; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2917 | int flags; |
| 2918 | |
| 2919 | nField = 0; |
| 2920 | pKeyInfo = 0; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2921 | p2 = pOp->p2; |
| 2922 | iDb = pOp->p3; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 2923 | assert( iDb>=0 && iDb<db->nDb ); |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 2924 | assert( (p->btreeMask & (1<<iDb))!=0 ); |
drh | d946db0 | 2005-12-29 19:23:06 +0000 | [diff] [blame] | 2925 | pDb = &db->aDb[iDb]; |
| 2926 | pX = pDb->pBt; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 2927 | assert( pX!=0 ); |
drh | d946db0 | 2005-12-29 19:23:06 +0000 | [diff] [blame] | 2928 | if( pOp->opcode==OP_OpenWrite ){ |
| 2929 | wrFlag = 1; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2930 | if( pDb->pSchema->file_format < p->minWriteFileFormat ){ |
| 2931 | p->minWriteFileFormat = pDb->pSchema->file_format; |
drh | d946db0 | 2005-12-29 19:23:06 +0000 | [diff] [blame] | 2932 | } |
| 2933 | }else{ |
| 2934 | wrFlag = 0; |
| 2935 | } |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 2936 | if( pOp->p5 ){ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2937 | assert( p2>0 ); |
| 2938 | assert( p2<=p->nMem ); |
| 2939 | pIn2 = &p->aMem[p2]; |
| 2940 | sqlite3VdbeMemIntegerify(pIn2); |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 2941 | p2 = (int)pIn2->u.i; |
drh | 27731d7 | 2009-06-22 12:05:10 +0000 | [diff] [blame^] | 2942 | if( NEVER(p2<2) ) { |
shane | dcc50b7 | 2008-11-13 18:29:50 +0000 | [diff] [blame] | 2943 | rc = SQLITE_CORRUPT_BKPT; |
| 2944 | goto abort_due_to_error; |
| 2945 | } |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 2946 | } |
danielk1977 | d336e22 | 2009-02-20 10:58:41 +0000 | [diff] [blame] | 2947 | if( pOp->p4type==P4_KEYINFO ){ |
| 2948 | pKeyInfo = pOp->p4.pKeyInfo; |
| 2949 | pKeyInfo->enc = ENC(p->db); |
| 2950 | nField = pKeyInfo->nField+1; |
| 2951 | }else if( pOp->p4type==P4_INT32 ){ |
| 2952 | nField = pOp->p4.i; |
| 2953 | } |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 2954 | assert( pOp->p1>=0 ); |
| 2955 | pCur = allocateCursor(p, pOp->p1, nField, iDb, 1); |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 2956 | if( pCur==0 ) goto no_mem; |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 2957 | pCur->nullRow = 1; |
danielk1977 | d336e22 | 2009-02-20 10:58:41 +0000 | [diff] [blame] | 2958 | rc = sqlite3BtreeCursor(pX, p2, wrFlag, pKeyInfo, pCur->pCursor); |
| 2959 | pCur->pKeyInfo = pKeyInfo; |
| 2960 | |
danielk1977 | 24162fe | 2004-06-04 06:22:00 +0000 | [diff] [blame] | 2961 | switch( rc ){ |
danielk1977 | 24162fe | 2004-06-04 06:22:00 +0000 | [diff] [blame] | 2962 | case SQLITE_OK: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 2963 | flags = sqlite3BtreeFlags(pCur->pCursor); |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 2964 | |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 2965 | /* Sanity checking. Only the lower four bits of the flags byte should |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame] | 2966 | ** be used. Bit 3 (mask 0x08) is unpredictable. The lower 3 bits |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 2967 | ** (mask 0x07) should be either 5 (intkey+leafdata for tables) or |
| 2968 | ** 2 (zerodata for indices). If these conditions are not met it can |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 2969 | ** only mean that we are dealing with a corrupt database file. |
| 2970 | ** Note: All of the above is checked already in sqlite3BtreeCursor(). |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 2971 | */ |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 2972 | assert( (flags & 0xf0)==0 ); |
| 2973 | assert( (flags & 0x07)==5 || (flags & 0x07)==2 ); |
| 2974 | |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 2975 | pCur->isTable = (flags & BTREE_INTKEY)!=0 ?1:0; |
| 2976 | pCur->isIndex = (flags & BTREE_ZERODATA)!=0 ?1:0; |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 2977 | /* If P4==0 it means we are expected to open a table. If P4!=0 then |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 2978 | ** we expect to be opening an index. If this is not what happened, |
| 2979 | ** then the database is corrupt |
| 2980 | */ |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 2981 | if( (pCur->isTable && pOp->p4type==P4_KEYINFO) |
| 2982 | || (pCur->isIndex && pOp->p4type!=P4_KEYINFO) ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 2983 | rc = SQLITE_CORRUPT_BKPT; |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 2984 | goto abort_due_to_error; |
| 2985 | } |
danielk1977 | 24162fe | 2004-06-04 06:22:00 +0000 | [diff] [blame] | 2986 | break; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2987 | } |
danielk1977 | 24162fe | 2004-06-04 06:22:00 +0000 | [diff] [blame] | 2988 | case SQLITE_EMPTY: { |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 2989 | pCur->isTable = pOp->p4type!=P4_KEYINFO; |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 2990 | pCur->isIndex = !pCur->isTable; |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 2991 | pCur->pCursor = 0; |
danielk1977 | 24162fe | 2004-06-04 06:22:00 +0000 | [diff] [blame] | 2992 | rc = SQLITE_OK; |
| 2993 | break; |
| 2994 | } |
| 2995 | default: { |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 2996 | assert( rc!=SQLITE_BUSY ); /* Busy conditions detected earlier */ |
danielk1977 | 24162fe | 2004-06-04 06:22:00 +0000 | [diff] [blame] | 2997 | goto abort_due_to_error; |
| 2998 | } |
| 2999 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3000 | break; |
| 3001 | } |
| 3002 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 3003 | /* Opcode: OpenEphemeral P1 P2 * P4 * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3004 | ** |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 3005 | ** Open a new cursor P1 to a transient table. |
drh | 9170dd7 | 2005-07-08 17:13:46 +0000 | [diff] [blame] | 3006 | ** The cursor is always opened read/write even if |
| 3007 | ** the main database is read-only. The transient or virtual |
| 3008 | ** table is deleted automatically when the cursor is closed. |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 3009 | ** |
drh | 0342b1f | 2005-09-01 03:07:44 +0000 | [diff] [blame] | 3010 | ** P2 is the number of columns in the virtual table. |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 3011 | ** The cursor points to a BTree table if P4==0 and to a BTree index |
| 3012 | ** 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] | 3013 | ** that defines the format of keys in the index. |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 3014 | ** |
| 3015 | ** This opcode was once called OpenTemp. But that created |
| 3016 | ** confusion because the term "temp table", might refer either |
| 3017 | ** to a TEMP table at the SQL level, or to a table opened by |
| 3018 | ** this opcode. Then this opcode was call OpenVirtual. But |
| 3019 | ** that created confusion with the whole virtual-table idea. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3020 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3021 | case OP_OpenEphemeral: { |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 3022 | VdbeCursor *pCx; |
drh | 33f4e02 | 2007-09-03 15:19:34 +0000 | [diff] [blame] | 3023 | static const int openFlags = |
| 3024 | SQLITE_OPEN_READWRITE | |
| 3025 | SQLITE_OPEN_CREATE | |
| 3026 | SQLITE_OPEN_EXCLUSIVE | |
| 3027 | SQLITE_OPEN_DELETEONCLOSE | |
| 3028 | SQLITE_OPEN_TRANSIENT_DB; |
| 3029 | |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 3030 | assert( pOp->p1>=0 ); |
| 3031 | pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, 1); |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 3032 | if( pCx==0 ) goto no_mem; |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 3033 | pCx->nullRow = 1; |
drh | 33f4e02 | 2007-09-03 15:19:34 +0000 | [diff] [blame] | 3034 | rc = sqlite3BtreeFactory(db, 0, 1, SQLITE_DEFAULT_TEMP_CACHE_SIZE, openFlags, |
| 3035 | &pCx->pBt); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3036 | if( rc==SQLITE_OK ){ |
danielk1977 | 40b38dc | 2004-06-26 08:38:24 +0000 | [diff] [blame] | 3037 | rc = sqlite3BtreeBeginTrans(pCx->pBt, 1); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3038 | } |
| 3039 | if( rc==SQLITE_OK ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3040 | /* If a transient index is required, create it by calling |
| 3041 | ** sqlite3BtreeCreateTable() with the BTREE_ZERODATA flag before |
| 3042 | ** opening it. If a transient table is required, just use the |
danielk1977 | 0dbe72b | 2004-05-11 04:54:49 +0000 | [diff] [blame] | 3043 | ** automatically created table with root-page 1 (an INTKEY table). |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3044 | */ |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 3045 | if( pOp->p4.pKeyInfo ){ |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 3046 | int pgno; |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 3047 | assert( pOp->p4type==P4_KEYINFO ); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3048 | rc = sqlite3BtreeCreateTable(pCx->pBt, &pgno, BTREE_ZERODATA); |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 3049 | if( rc==SQLITE_OK ){ |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 3050 | assert( pgno==MASTER_ROOT+1 ); |
drh | 1e968a0 | 2008-03-25 00:22:21 +0000 | [diff] [blame] | 3051 | rc = sqlite3BtreeCursor(pCx->pBt, pgno, 1, |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 3052 | (KeyInfo*)pOp->p4.z, pCx->pCursor); |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 3053 | pCx->pKeyInfo = pOp->p4.pKeyInfo; |
danielk1977 | 14db266 | 2006-01-09 16:12:04 +0000 | [diff] [blame] | 3054 | pCx->pKeyInfo->enc = ENC(p->db); |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 3055 | } |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3056 | pCx->isTable = 0; |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 3057 | }else{ |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 3058 | rc = sqlite3BtreeCursor(pCx->pBt, MASTER_ROOT, 1, 0, pCx->pCursor); |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3059 | pCx->isTable = 1; |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 3060 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3061 | } |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3062 | pCx->isIndex = !pCx->isTable; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3063 | break; |
| 3064 | } |
| 3065 | |
danielk1977 | d336e22 | 2009-02-20 10:58:41 +0000 | [diff] [blame] | 3066 | /* Opcode: OpenPseudo P1 P2 P3 * * |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3067 | ** |
| 3068 | ** Open a new cursor that points to a fake table that contains a single |
| 3069 | ** row of data. Any attempt to write a second row of data causes the |
| 3070 | ** first row to be deleted. All data is deleted when the cursor is |
| 3071 | ** closed. |
| 3072 | ** |
| 3073 | ** A pseudo-table created by this opcode is useful for holding the |
drh | cdd536f | 2006-03-17 00:04:03 +0000 | [diff] [blame] | 3074 | ** NEW or OLD tables in a trigger. Also used to hold the a single |
| 3075 | ** row output from the sorter so that the row can be decomposed into |
| 3076 | ** individual columns using the OP_Column opcode. |
danielk1977 | 9882d99 | 2008-03-27 17:59:01 +0000 | [diff] [blame] | 3077 | ** |
| 3078 | ** When OP_Insert is executed to insert a row in to the pseudo table, |
| 3079 | ** the pseudo-table cursor may or may not make it's own copy of the |
| 3080 | ** original row data. If P2 is 0, then the pseudo-table will copy the |
| 3081 | ** original row data. Otherwise, a pointer to the original memory cell |
| 3082 | ** is stored. In this case, the vdbe program must ensure that the |
| 3083 | ** memory cell containing the row data is not overwritten until the |
| 3084 | ** pseudo table is closed (or a new row is inserted into it). |
danielk1977 | d336e22 | 2009-02-20 10:58:41 +0000 | [diff] [blame] | 3085 | ** |
| 3086 | ** P3 is the number of fields in the records that will be stored by |
| 3087 | ** the pseudo-table. |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3088 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3089 | case OP_OpenPseudo: { |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 3090 | VdbeCursor *pCx; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3091 | |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 3092 | assert( pOp->p1>=0 ); |
| 3093 | pCx = allocateCursor(p, pOp->p1, pOp->p3, -1, 0); |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 3094 | if( pCx==0 ) goto no_mem; |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3095 | pCx->nullRow = 1; |
| 3096 | pCx->pseudoTable = 1; |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 3097 | pCx->ephemPseudoTable = (u8)pOp->p2; |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3098 | pCx->isTable = 1; |
| 3099 | pCx->isIndex = 0; |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3100 | break; |
| 3101 | } |
| 3102 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 3103 | /* Opcode: Close P1 * * * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3104 | ** |
| 3105 | ** Close a cursor previously opened as P1. If P1 is not |
| 3106 | ** currently open, this instruction is a no-op. |
| 3107 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3108 | case OP_Close: { |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 3109 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 3110 | sqlite3VdbeFreeCursor(p, p->apCsr[pOp->p1]); |
| 3111 | p->apCsr[pOp->p1] = 0; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3112 | break; |
| 3113 | } |
| 3114 | |
drh | 959403f | 2008-12-12 17:56:16 +0000 | [diff] [blame] | 3115 | /* Opcode: SeekGe P1 P2 P3 P4 * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3116 | ** |
danielk1977 | b790c6c | 2008-04-18 10:25:24 +0000 | [diff] [blame] | 3117 | ** 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] | 3118 | ** use the value in register P3 as the key. If cursor P1 refers |
danielk1977 | b790c6c | 2008-04-18 10:25:24 +0000 | [diff] [blame] | 3119 | ** to an SQL index, then P3 is the first in an array of P4 registers |
| 3120 | ** that are used as an unpacked index key. |
| 3121 | ** |
| 3122 | ** Reposition cursor P1 so that it points to the smallest entry that |
| 3123 | ** is greater than or equal to the key value. If there are no records |
| 3124 | ** 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] | 3125 | ** |
drh | 959403f | 2008-12-12 17:56:16 +0000 | [diff] [blame] | 3126 | ** See also: Found, NotFound, Distinct, SeekLt, SeekGt, SeekLe |
drh | 7cf6e4d | 2004-05-19 14:56:55 +0000 | [diff] [blame] | 3127 | */ |
drh | 959403f | 2008-12-12 17:56:16 +0000 | [diff] [blame] | 3128 | /* Opcode: SeekGt P1 P2 P3 P4 * |
drh | 7cf6e4d | 2004-05-19 14:56:55 +0000 | [diff] [blame] | 3129 | ** |
danielk1977 | b790c6c | 2008-04-18 10:25:24 +0000 | [diff] [blame] | 3130 | ** 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] | 3131 | ** use the value in register P3 as a key. If cursor P1 refers |
danielk1977 | b790c6c | 2008-04-18 10:25:24 +0000 | [diff] [blame] | 3132 | ** to an SQL index, then P3 is the first in an array of P4 registers |
| 3133 | ** that are used as an unpacked index key. |
| 3134 | ** |
| 3135 | ** Reposition cursor P1 so that it points to the smallest entry that |
| 3136 | ** is greater than the key value. If there are no records greater than |
| 3137 | ** the key and P2 is not zero, then jump to P2. |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 3138 | ** |
drh | 959403f | 2008-12-12 17:56:16 +0000 | [diff] [blame] | 3139 | ** See also: Found, NotFound, Distinct, SeekLt, SeekGe, SeekLe |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3140 | */ |
drh | 959403f | 2008-12-12 17:56:16 +0000 | [diff] [blame] | 3141 | /* Opcode: SeekLt P1 P2 P3 P4 * |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 3142 | ** |
danielk1977 | b790c6c | 2008-04-18 10:25:24 +0000 | [diff] [blame] | 3143 | ** 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] | 3144 | ** use the value in register P3 as a key. If cursor P1 refers |
danielk1977 | b790c6c | 2008-04-18 10:25:24 +0000 | [diff] [blame] | 3145 | ** to an SQL index, then P3 is the first in an array of P4 registers |
| 3146 | ** that are used as an unpacked index key. |
| 3147 | ** |
| 3148 | ** Reposition cursor P1 so that it points to the largest entry that |
| 3149 | ** is less than the key value. If there are no records less than |
| 3150 | ** the key and P2 is not zero, then jump to P2. |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 3151 | ** |
drh | 959403f | 2008-12-12 17:56:16 +0000 | [diff] [blame] | 3152 | ** See also: Found, NotFound, Distinct, SeekGt, SeekGe, SeekLe |
drh | 7cf6e4d | 2004-05-19 14:56:55 +0000 | [diff] [blame] | 3153 | */ |
drh | 959403f | 2008-12-12 17:56:16 +0000 | [diff] [blame] | 3154 | /* Opcode: SeekLe P1 P2 P3 P4 * |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 3155 | ** |
danielk1977 | b790c6c | 2008-04-18 10:25:24 +0000 | [diff] [blame] | 3156 | ** 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] | 3157 | ** use the value in register P3 as a key. If cursor P1 refers |
danielk1977 | b790c6c | 2008-04-18 10:25:24 +0000 | [diff] [blame] | 3158 | ** to an SQL index, then P3 is the first in an array of P4 registers |
| 3159 | ** that are used as an unpacked index key. |
danielk1977 | 751de56 | 2008-04-18 09:01:15 +0000 | [diff] [blame] | 3160 | ** |
danielk1977 | b790c6c | 2008-04-18 10:25:24 +0000 | [diff] [blame] | 3161 | ** Reposition cursor P1 so that it points to the largest entry that |
| 3162 | ** is less than or equal to the key value. If there are no records |
| 3163 | ** 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] | 3164 | ** |
drh | 959403f | 2008-12-12 17:56:16 +0000 | [diff] [blame] | 3165 | ** See also: Found, NotFound, Distinct, SeekGt, SeekGe, SeekLt |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 3166 | */ |
drh | 959403f | 2008-12-12 17:56:16 +0000 | [diff] [blame] | 3167 | case OP_SeekLt: /* jump, in3 */ |
| 3168 | case OP_SeekLe: /* jump, in3 */ |
| 3169 | case OP_SeekGe: /* jump, in3 */ |
| 3170 | case OP_SeekGt: { /* jump, in3 */ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3171 | int res; |
| 3172 | int oc; |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 3173 | VdbeCursor *pC; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3174 | UnpackedRecord r; |
| 3175 | int nField; |
| 3176 | i64 iKey; /* The rowid we are to seek to */ |
drh | 80ff32f | 2001-11-04 18:32:46 +0000 | [diff] [blame] | 3177 | |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 3178 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
drh | 959403f | 2008-12-12 17:56:16 +0000 | [diff] [blame] | 3179 | assert( pOp->p2!=0 ); |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 3180 | pC = p->apCsr[pOp->p1]; |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 3181 | assert( pC!=0 ); |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3182 | if( pC->pCursor!=0 ){ |
drh | 7cf6e4d | 2004-05-19 14:56:55 +0000 | [diff] [blame] | 3183 | oc = pOp->opcode; |
drh | a11846b | 2004-01-07 18:52:56 +0000 | [diff] [blame] | 3184 | pC->nullRow = 0; |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3185 | if( pC->isTable ){ |
drh | 959403f | 2008-12-12 17:56:16 +0000 | [diff] [blame] | 3186 | /* The input value in P3 might be of any type: integer, real, string, |
| 3187 | ** blob, or NULL. But it needs to be an integer before we can do |
| 3188 | ** the seek, so covert it. */ |
| 3189 | applyNumericAffinity(pIn3); |
| 3190 | iKey = sqlite3VdbeIntValue(pIn3); |
| 3191 | pC->rowidIsValid = 0; |
| 3192 | |
| 3193 | /* If the P3 value could not be converted into an integer without |
| 3194 | ** loss of information, then special processing is required... */ |
| 3195 | if( (pIn3->flags & MEM_Int)==0 ){ |
| 3196 | if( (pIn3->flags & MEM_Real)==0 ){ |
| 3197 | /* If the P3 value cannot be converted into any kind of a number, |
| 3198 | ** then the seek is not possible, so jump to P2 */ |
| 3199 | pc = pOp->p2 - 1; |
| 3200 | break; |
| 3201 | } |
| 3202 | /* If we reach this point, then the P3 value must be a floating |
| 3203 | ** point number. */ |
| 3204 | assert( (pIn3->flags & MEM_Real)!=0 ); |
| 3205 | |
| 3206 | if( iKey==SMALLEST_INT64 && (pIn3->r<(double)iKey || pIn3->r>0) ){ |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 3207 | /* The P3 value is too large in magnitude to be expressed as an |
drh | 959403f | 2008-12-12 17:56:16 +0000 | [diff] [blame] | 3208 | ** integer. */ |
| 3209 | res = 1; |
| 3210 | if( pIn3->r<0 ){ |
| 3211 | if( oc==OP_SeekGt || oc==OP_SeekGe ){ |
| 3212 | rc = sqlite3BtreeFirst(pC->pCursor, &res); |
| 3213 | if( rc!=SQLITE_OK ) goto abort_due_to_error; |
| 3214 | } |
| 3215 | }else{ |
| 3216 | if( oc==OP_SeekLt || oc==OP_SeekLe ){ |
| 3217 | rc = sqlite3BtreeLast(pC->pCursor, &res); |
| 3218 | if( rc!=SQLITE_OK ) goto abort_due_to_error; |
| 3219 | } |
| 3220 | } |
| 3221 | if( res ){ |
| 3222 | pc = pOp->p2 - 1; |
| 3223 | } |
| 3224 | break; |
| 3225 | }else if( oc==OP_SeekLt || oc==OP_SeekGe ){ |
| 3226 | /* Use the ceiling() function to convert real->int */ |
| 3227 | if( pIn3->r > (double)iKey ) iKey++; |
| 3228 | }else{ |
| 3229 | /* Use the floor() function to convert real->int */ |
| 3230 | assert( oc==OP_SeekLe || oc==OP_SeekGt ); |
| 3231 | if( pIn3->r < (double)iKey ) iKey--; |
| 3232 | } |
| 3233 | } |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 3234 | rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, 0, (u64)iKey, 0, &res); |
danielk1977 | 2812956 | 2005-01-11 10:25:06 +0000 | [diff] [blame] | 3235 | if( rc!=SQLITE_OK ){ |
| 3236 | goto abort_due_to_error; |
| 3237 | } |
drh | 959403f | 2008-12-12 17:56:16 +0000 | [diff] [blame] | 3238 | if( res==0 ){ |
| 3239 | pC->rowidIsValid = 1; |
| 3240 | pC->lastRowid = iKey; |
| 3241 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3242 | }else{ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3243 | nField = pOp->p4.i; |
danielk1977 | b790c6c | 2008-04-18 10:25:24 +0000 | [diff] [blame] | 3244 | assert( pOp->p4type==P4_INT32 ); |
| 3245 | assert( nField>0 ); |
| 3246 | r.pKeyInfo = pC->pKeyInfo; |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 3247 | r.nField = (u16)nField; |
drh | 959403f | 2008-12-12 17:56:16 +0000 | [diff] [blame] | 3248 | if( oc==OP_SeekGt || oc==OP_SeekLe ){ |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 3249 | r.flags = UNPACKED_INCRKEY; |
| 3250 | }else{ |
| 3251 | r.flags = 0; |
| 3252 | } |
danielk1977 | b790c6c | 2008-04-18 10:25:24 +0000 | [diff] [blame] | 3253 | r.aMem = &p->aMem[pOp->p3]; |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 3254 | rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, &r, 0, 0, &res); |
danielk1977 | 2812956 | 2005-01-11 10:25:06 +0000 | [diff] [blame] | 3255 | if( rc!=SQLITE_OK ){ |
| 3256 | goto abort_due_to_error; |
| 3257 | } |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3258 | pC->rowidIsValid = 0; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3259 | } |
drh | a11846b | 2004-01-07 18:52:56 +0000 | [diff] [blame] | 3260 | pC->deferredMoveto = 0; |
drh | 76873ab | 2006-01-07 18:48:26 +0000 | [diff] [blame] | 3261 | pC->cacheStatus = CACHE_STALE; |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 3262 | #ifdef SQLITE_TEST |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 3263 | sqlite3_search_count++; |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 3264 | #endif |
drh | 959403f | 2008-12-12 17:56:16 +0000 | [diff] [blame] | 3265 | if( oc==OP_SeekGe || oc==OP_SeekGt ){ |
| 3266 | if( res<0 || (res==0 && oc==OP_SeekGt) ){ |
danielk1977 | 2812956 | 2005-01-11 10:25:06 +0000 | [diff] [blame] | 3267 | rc = sqlite3BtreeNext(pC->pCursor, &res); |
danielk1977 | 01427a6 | 2005-01-11 13:02:33 +0000 | [diff] [blame] | 3268 | if( rc!=SQLITE_OK ) goto abort_due_to_error; |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3269 | pC->rowidIsValid = 0; |
drh | 1af3fdb | 2004-07-18 21:33:01 +0000 | [diff] [blame] | 3270 | }else{ |
| 3271 | res = 0; |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 3272 | } |
drh | 7cf6e4d | 2004-05-19 14:56:55 +0000 | [diff] [blame] | 3273 | }else{ |
drh | 959403f | 2008-12-12 17:56:16 +0000 | [diff] [blame] | 3274 | assert( oc==OP_SeekLt || oc==OP_SeekLe ); |
| 3275 | if( res>0 || (res==0 && oc==OP_SeekLt) ){ |
danielk1977 | 01427a6 | 2005-01-11 13:02:33 +0000 | [diff] [blame] | 3276 | rc = sqlite3BtreePrevious(pC->pCursor, &res); |
| 3277 | if( rc!=SQLITE_OK ) goto abort_due_to_error; |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3278 | pC->rowidIsValid = 0; |
drh | 1a844c3 | 2002-12-04 22:29:28 +0000 | [diff] [blame] | 3279 | }else{ |
| 3280 | /* res might be negative because the table is empty. Check to |
| 3281 | ** see if this is the case. |
| 3282 | */ |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 3283 | res = sqlite3BtreeEof(pC->pCursor); |
drh | 1a844c3 | 2002-12-04 22:29:28 +0000 | [diff] [blame] | 3284 | } |
drh | 1af3fdb | 2004-07-18 21:33:01 +0000 | [diff] [blame] | 3285 | } |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 3286 | assert( pOp->p2>0 ); |
drh | 1af3fdb | 2004-07-18 21:33:01 +0000 | [diff] [blame] | 3287 | if( res ){ |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 3288 | pc = pOp->p2 - 1; |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 3289 | } |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 3290 | }else{ |
danielk1977 | f7b9d66 | 2008-06-23 18:49:43 +0000 | [diff] [blame] | 3291 | /* This happens when attempting to open the sqlite3_master table |
| 3292 | ** for read access returns SQLITE_EMPTY. In this case always |
| 3293 | ** take the jump (since there are no records in the table). |
| 3294 | */ |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 3295 | assert( pC->pseudoTable==0 ); |
danielk1977 | f7b9d66 | 2008-06-23 18:49:43 +0000 | [diff] [blame] | 3296 | pc = pOp->p2 - 1; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3297 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3298 | break; |
| 3299 | } |
| 3300 | |
drh | 959403f | 2008-12-12 17:56:16 +0000 | [diff] [blame] | 3301 | /* Opcode: Seek P1 P2 * * * |
| 3302 | ** |
| 3303 | ** P1 is an open table cursor and P2 is a rowid integer. Arrange |
| 3304 | ** for P1 to move so that it points to the rowid given by P2. |
| 3305 | ** |
| 3306 | ** This is actually a deferred seek. Nothing actually happens until |
| 3307 | ** the cursor is used to read a record. That way, if no reads |
| 3308 | ** occur, no unnecessary I/O happens. |
| 3309 | */ |
| 3310 | case OP_Seek: { /* in2 */ |
drh | 959403f | 2008-12-12 17:56:16 +0000 | [diff] [blame] | 3311 | VdbeCursor *pC; |
| 3312 | |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 3313 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 3314 | pC = p->apCsr[pOp->p1]; |
drh | 959403f | 2008-12-12 17:56:16 +0000 | [diff] [blame] | 3315 | assert( pC!=0 ); |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 3316 | if( ALWAYS(pC->pCursor!=0) ){ |
drh | 959403f | 2008-12-12 17:56:16 +0000 | [diff] [blame] | 3317 | assert( pC->isTable ); |
| 3318 | pC->nullRow = 0; |
| 3319 | pC->movetoTarget = sqlite3VdbeIntValue(pIn2); |
| 3320 | pC->rowidIsValid = 0; |
| 3321 | pC->deferredMoveto = 1; |
| 3322 | } |
| 3323 | break; |
| 3324 | } |
| 3325 | |
| 3326 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 3327 | /* Opcode: Found P1 P2 P3 * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3328 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 3329 | ** Register P3 holds a blob constructed by MakeRecord. P1 is an index. |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3330 | ** If an entry that matches the value in register p3 exists in P1 then |
| 3331 | ** jump to P2. If the P3 value does not match any entry in P1 |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3332 | ** then fall thru. The P1 cursor is left pointing at the matching entry |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 3333 | ** if it exists. |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3334 | ** |
| 3335 | ** This instruction is used to implement the IN operator where the |
danielk1977 | 9a96b66 | 2007-11-29 17:05:18 +0000 | [diff] [blame] | 3336 | ** left-hand side is a SELECT statement. P1 may be a true index, or it |
| 3337 | ** may be a temporary index that holds the results of the SELECT |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 3338 | ** statement. This instruction is also used to implement the |
| 3339 | ** DISTINCT keyword in SELECT statements. |
danielk1977 | 9a96b66 | 2007-11-29 17:05:18 +0000 | [diff] [blame] | 3340 | ** |
| 3341 | ** This instruction checks if index P1 contains a record for which |
shane | 21e7feb | 2008-05-30 15:59:49 +0000 | [diff] [blame] | 3342 | ** the first N serialized values exactly match the N serialized values |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3343 | ** in the record in register P3, where N is the total number of values in |
| 3344 | ** the P3 record (the P3 record is a prefix of the P1 record). |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 3345 | ** |
drh | cb6d50e | 2008-08-21 19:28:30 +0000 | [diff] [blame] | 3346 | ** See also: NotFound, IsUnique, NotExists |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3347 | */ |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 3348 | /* Opcode: NotFound P1 P2 P3 * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3349 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 3350 | ** Register P3 holds a blob constructed by MakeRecord. P1 is |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3351 | ** an index. If no entry exists in P1 that matches the blob then jump |
drh | 795ab9b | 2007-01-27 13:37:22 +0000 | [diff] [blame] | 3352 | ** to P2. If an entry does existing, fall through. The cursor is left |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3353 | ** pointing to the entry that matches. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3354 | ** |
drh | cb6d50e | 2008-08-21 19:28:30 +0000 | [diff] [blame] | 3355 | ** See also: Found, NotExists, IsUnique |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3356 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3357 | case OP_NotFound: /* jump, in3 */ |
| 3358 | case OP_Found: { /* jump, in3 */ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3359 | int alreadyExists; |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 3360 | VdbeCursor *pC; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3361 | int res; |
| 3362 | UnpackedRecord *pIdxKey; |
| 3363 | char aTempRec[ROUND8(sizeof(UnpackedRecord)) + sizeof(Mem)*3 + 7]; |
| 3364 | |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3365 | alreadyExists = 0; |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 3366 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 3367 | pC = p->apCsr[pOp->p1]; |
| 3368 | assert( pC!=0 ); |
| 3369 | if( ALWAYS(pC->pCursor!=0) ){ |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 3370 | |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3371 | assert( pC->isTable==0 ); |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 3372 | assert( pIn3->flags & MEM_Blob ); |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 3373 | pIdxKey = sqlite3VdbeRecordUnpack(pC->pKeyInfo, pIn3->n, pIn3->z, |
drh | 23f79d0 | 2008-08-20 22:06:47 +0000 | [diff] [blame] | 3374 | aTempRec, sizeof(aTempRec)); |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 3375 | if( pIdxKey==0 ){ |
| 3376 | goto no_mem; |
danielk1977 | 9a96b66 | 2007-11-29 17:05:18 +0000 | [diff] [blame] | 3377 | } |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 3378 | if( pOp->opcode==OP_Found ){ |
| 3379 | pIdxKey->flags |= UNPACKED_PREFIX_MATCH; |
| 3380 | } |
| 3381 | rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, pIdxKey, 0, 0, &res); |
| 3382 | sqlite3VdbeDeleteUnpackedRecord(pIdxKey); |
danielk1977 | 7751940 | 2007-08-30 11:48:31 +0000 | [diff] [blame] | 3383 | if( rc!=SQLITE_OK ){ |
| 3384 | break; |
| 3385 | } |
| 3386 | alreadyExists = (res==0); |
drh | a11846b | 2004-01-07 18:52:56 +0000 | [diff] [blame] | 3387 | pC->deferredMoveto = 0; |
drh | 76873ab | 2006-01-07 18:48:26 +0000 | [diff] [blame] | 3388 | pC->cacheStatus = CACHE_STALE; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3389 | } |
| 3390 | if( pOp->opcode==OP_Found ){ |
| 3391 | if( alreadyExists ) pc = pOp->p2 - 1; |
| 3392 | }else{ |
| 3393 | if( !alreadyExists ) pc = pOp->p2 - 1; |
| 3394 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3395 | break; |
| 3396 | } |
| 3397 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 3398 | /* Opcode: IsUnique P1 P2 P3 P4 * |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 3399 | ** |
danielk1977 | de63035 | 2009-05-04 11:42:29 +0000 | [diff] [blame] | 3400 | ** Cursor P1 is open on an index. So it has no data and its key consists |
| 3401 | ** of a record generated by OP_MakeRecord where the last field is the |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3402 | ** rowid of the entry that the index refers to. |
danielk1977 | de63035 | 2009-05-04 11:42:29 +0000 | [diff] [blame] | 3403 | ** |
| 3404 | ** The P3 register contains an integer record number. Call this record |
| 3405 | ** number R. Register P4 is the first in a set of N contiguous registers |
| 3406 | ** that make up an unpacked index key that can be used with cursor P1. |
| 3407 | ** The value of N can be inferred from the cursor. N includes the rowid |
| 3408 | ** value appended to the end of the index record. This rowid value may |
| 3409 | ** or may not be the same as R. |
| 3410 | ** |
| 3411 | ** If any of the N registers beginning with register P4 contains a NULL |
| 3412 | ** value, jump immediately to P2. |
| 3413 | ** |
| 3414 | ** Otherwise, this instruction checks if cursor P1 contains an entry |
| 3415 | ** where the first (N-1) fields match but the rowid value at the end |
| 3416 | ** of the index entry is not R. If there is no such entry, control jumps |
| 3417 | ** to instruction P2. Otherwise, the rowid of the conflicting index |
| 3418 | ** entry is copied to register P3 and control falls through to the next |
| 3419 | ** instruction. |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 3420 | ** |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3421 | ** See also: NotFound, NotExists, Found |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 3422 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3423 | case OP_IsUnique: { /* jump, in3 */ |
shane | 60a4b53 | 2009-05-06 18:57:09 +0000 | [diff] [blame] | 3424 | u16 ii; |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 3425 | VdbeCursor *pCx; |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 3426 | BtCursor *pCrsr; |
shane | 60a4b53 | 2009-05-06 18:57:09 +0000 | [diff] [blame] | 3427 | u16 nField; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3428 | Mem *aMem; |
| 3429 | UnpackedRecord r; /* B-Tree index search key */ |
| 3430 | i64 R; /* Rowid stored in register P3 */ |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 3431 | |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3432 | aMem = &p->aMem[pOp->p4.i]; |
danielk1977 | de63035 | 2009-05-04 11:42:29 +0000 | [diff] [blame] | 3433 | /* Assert that the values of parameters P1 and P4 are in range. */ |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 3434 | assert( pOp->p4type==P4_INT32 ); |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3435 | assert( pOp->p4.i>0 && pOp->p4.i<=p->nMem ); |
danielk1977 | de63035 | 2009-05-04 11:42:29 +0000 | [diff] [blame] | 3436 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 3437 | |
| 3438 | /* Find the index cursor. */ |
| 3439 | pCx = p->apCsr[pOp->p1]; |
| 3440 | assert( pCx->deferredMoveto==0 ); |
| 3441 | pCx->seekResult = 0; |
| 3442 | pCx->cacheStatus = CACHE_STALE; |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 3443 | pCrsr = pCx->pCursor; |
danielk1977 | de63035 | 2009-05-04 11:42:29 +0000 | [diff] [blame] | 3444 | |
| 3445 | /* If any of the values are NULL, take the jump. */ |
| 3446 | nField = pCx->pKeyInfo->nField; |
| 3447 | for(ii=0; ii<nField; ii++){ |
| 3448 | if( aMem[ii].flags & MEM_Null ){ |
| 3449 | pc = pOp->p2 - 1; |
| 3450 | pCrsr = 0; |
| 3451 | break; |
| 3452 | } |
| 3453 | } |
| 3454 | assert( (aMem[nField].flags & MEM_Null)==0 ); |
| 3455 | |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 3456 | if( pCrsr!=0 ){ |
danielk1977 | de63035 | 2009-05-04 11:42:29 +0000 | [diff] [blame] | 3457 | /* Populate the index search key. */ |
| 3458 | r.pKeyInfo = pCx->pKeyInfo; |
| 3459 | r.nField = nField + 1; |
| 3460 | r.flags = UNPACKED_PREFIX_SEARCH; |
| 3461 | r.aMem = aMem; |
danielk1977 | 452c989 | 2004-05-13 05:16:15 +0000 | [diff] [blame] | 3462 | |
danielk1977 | de63035 | 2009-05-04 11:42:29 +0000 | [diff] [blame] | 3463 | /* Extract the value of R from register P3. */ |
| 3464 | sqlite3VdbeMemIntegerify(pIn3); |
| 3465 | R = pIn3->u.i; |
| 3466 | |
| 3467 | /* Search the B-Tree index. If no conflicting record is found, jump |
| 3468 | ** to P2. Otherwise, copy the rowid of the conflicting record to |
| 3469 | ** register P3 and fall through to the next instruction. */ |
| 3470 | rc = sqlite3BtreeMovetoUnpacked(pCrsr, &r, 0, 0, &pCx->seekResult); |
| 3471 | if( (r.flags & UNPACKED_PREFIX_SEARCH) || r.rowid==R ){ |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 3472 | pc = pOp->p2 - 1; |
danielk1977 | de63035 | 2009-05-04 11:42:29 +0000 | [diff] [blame] | 3473 | }else{ |
| 3474 | pIn3->u.i = r.rowid; |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 3475 | } |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 3476 | } |
| 3477 | break; |
| 3478 | } |
| 3479 | |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3480 | /* Opcode: NotExists P1 P2 P3 * * |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 3481 | ** |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3482 | ** Use the content of register P3 as a integer key. If a record |
danielk1977 | 96cb76f | 2008-01-04 13:24:28 +0000 | [diff] [blame] | 3483 | ** with that key does not exist in table of P1, then jump to P2. |
| 3484 | ** If the record does exist, then fall thru. The cursor is left |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3485 | ** pointing to the record if it exists. |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 3486 | ** |
| 3487 | ** The difference between this operation and NotFound is that this |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3488 | ** operation assumes the key is an integer and that P1 is a table whereas |
| 3489 | ** NotFound assumes key is a blob constructed from MakeRecord and |
| 3490 | ** P1 is an index. |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 3491 | ** |
drh | cb6d50e | 2008-08-21 19:28:30 +0000 | [diff] [blame] | 3492 | ** See also: Found, NotFound, IsUnique |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 3493 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3494 | case OP_NotExists: { /* jump, in3 */ |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 3495 | VdbeCursor *pC; |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 3496 | BtCursor *pCrsr; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3497 | int res; |
| 3498 | u64 iKey; |
| 3499 | |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 3500 | assert( pIn3->flags & MEM_Int ); |
| 3501 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 3502 | pC = p->apCsr[pOp->p1]; |
| 3503 | assert( pC!=0 ); |
| 3504 | assert( pC->isTable ); |
| 3505 | pCrsr = pC->pCursor; |
| 3506 | if( pCrsr!=0 ){ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3507 | res = 0; |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 3508 | iKey = pIn3->u.i; |
danielk1977 | de63035 | 2009-05-04 11:42:29 +0000 | [diff] [blame] | 3509 | rc = sqlite3BtreeMovetoUnpacked(pCrsr, 0, iKey, 0, &res); |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 3510 | pC->lastRowid = pIn3->u.i; |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 3511 | pC->rowidIsValid = res==0 ?1:0; |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 3512 | pC->nullRow = 0; |
drh | 76873ab | 2006-01-07 18:48:26 +0000 | [diff] [blame] | 3513 | pC->cacheStatus = CACHE_STALE; |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3514 | pC->deferredMoveto = 0; |
danielk1977 | 2812956 | 2005-01-11 10:25:06 +0000 | [diff] [blame] | 3515 | if( res!=0 ){ |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 3516 | pc = pOp->p2 - 1; |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 3517 | assert( pC->rowidIsValid==0 ); |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 3518 | } |
danielk1977 | de63035 | 2009-05-04 11:42:29 +0000 | [diff] [blame] | 3519 | pC->seekResult = res; |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 3520 | }else{ |
danielk1977 | f7b9d66 | 2008-06-23 18:49:43 +0000 | [diff] [blame] | 3521 | /* This happens when an attempt to open a read cursor on the |
| 3522 | ** sqlite_master table returns SQLITE_EMPTY. |
| 3523 | */ |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 3524 | assert( !pC->pseudoTable ); |
danielk1977 | f7b9d66 | 2008-06-23 18:49:43 +0000 | [diff] [blame] | 3525 | assert( pC->isTable ); |
| 3526 | pc = pOp->p2 - 1; |
| 3527 | assert( pC->rowidIsValid==0 ); |
danielk1977 | de63035 | 2009-05-04 11:42:29 +0000 | [diff] [blame] | 3528 | pC->seekResult = 0; |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 3529 | } |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 3530 | break; |
| 3531 | } |
| 3532 | |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 3533 | /* Opcode: Sequence P1 P2 * * * |
drh | 4db38a7 | 2005-09-01 12:16:28 +0000 | [diff] [blame] | 3534 | ** |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 3535 | ** Find the next available sequence number for cursor P1. |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3536 | ** Write the sequence number into register P2. |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 3537 | ** The sequence number on the cursor is incremented after this |
| 3538 | ** instruction. |
drh | 4db38a7 | 2005-09-01 12:16:28 +0000 | [diff] [blame] | 3539 | */ |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 3540 | case OP_Sequence: { /* out2-prerelease */ |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 3541 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 3542 | assert( p->apCsr[pOp->p1]!=0 ); |
| 3543 | pOut->u.i = p->apCsr[pOp->p1]->seqCount++; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 3544 | MemSetTypeFlag(pOut, MEM_Int); |
drh | 4db38a7 | 2005-09-01 12:16:28 +0000 | [diff] [blame] | 3545 | break; |
| 3546 | } |
| 3547 | |
| 3548 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 3549 | /* Opcode: NewRowid P1 P2 P3 * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3550 | ** |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3551 | ** 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] | 3552 | ** The record number is not previously used as a key in the database |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3553 | ** table that cursor P1 points to. The new record number is written |
| 3554 | ** written to register P2. |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 3555 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 3556 | ** If P3>0 then P3 is a register that holds the largest previously |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 3557 | ** generated record number. No new record numbers are allowed to be less |
drh | 2958a4e | 2004-11-12 03:56:15 +0000 | [diff] [blame] | 3558 | ** than this value. When this value reaches its maximum, a SQLITE_FULL |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 3559 | ** error is generated. The P3 register is updated with the generated |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 3560 | ** record number. This P3 mechanism is used to help implement the |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 3561 | ** AUTOINCREMENT feature. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3562 | */ |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 3563 | case OP_NewRowid: { /* out2-prerelease */ |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 3564 | i64 v; /* The new rowid */ |
| 3565 | VdbeCursor *pC; /* Cursor of table to get the new rowid */ |
| 3566 | int res; /* Result of an sqlite3BtreeLast() */ |
| 3567 | int cnt; /* Counter to limit the number of searches */ |
| 3568 | Mem *pMem; /* Register holding largest rowid for AUTOINCREMENT */ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3569 | |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3570 | v = 0; |
| 3571 | res = 0; |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 3572 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 3573 | pC = p->apCsr[pOp->p1]; |
| 3574 | assert( pC!=0 ); |
| 3575 | if( NEVER(pC->pCursor==0) ){ |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 3576 | /* The zero initialization above is all that is needed */ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3577 | }else{ |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 3578 | /* The next rowid or record number (different terms for the same |
| 3579 | ** thing) is obtained in a two-step algorithm. |
| 3580 | ** |
| 3581 | ** First we attempt to find the largest existing rowid and add one |
| 3582 | ** to that. But if the largest existing rowid is already the maximum |
| 3583 | ** positive integer, we have to fall through to the second |
| 3584 | ** probabilistic algorithm |
| 3585 | ** |
| 3586 | ** The second algorithm is to select a rowid at random and see if |
| 3587 | ** it already exists in the table. If it does not exist, we have |
| 3588 | ** succeeded. If the random rowid does exist, we select a new one |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 3589 | ** and try again, up to 100 times. |
drh | db5ed6d | 2001-09-18 22:17:44 +0000 | [diff] [blame] | 3590 | */ |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 3591 | assert( pC->isTable ); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3592 | cnt = 0; |
drh | fe2093d | 2005-01-20 22:48:47 +0000 | [diff] [blame] | 3593 | |
drh | 75f86a4 | 2005-02-17 00:03:06 +0000 | [diff] [blame] | 3594 | #ifdef SQLITE_32BIT_ROWID |
| 3595 | # define MAX_ROWID 0x7fffffff |
| 3596 | #else |
drh | fe2093d | 2005-01-20 22:48:47 +0000 | [diff] [blame] | 3597 | /* Some compilers complain about constants of the form 0x7fffffffffffffff. |
| 3598 | ** Others complain about 0x7ffffffffffffffffLL. The following macro seems |
| 3599 | ** to provide the constant while making all compilers happy. |
| 3600 | */ |
danielk1977 | 64202cf | 2008-11-17 15:31:47 +0000 | [diff] [blame] | 3601 | # define MAX_ROWID (i64)( (((u64)0x7fffffff)<<32) | (u64)0xffffffff ) |
drh | 75f86a4 | 2005-02-17 00:03:06 +0000 | [diff] [blame] | 3602 | #endif |
drh | fe2093d | 2005-01-20 22:48:47 +0000 | [diff] [blame] | 3603 | |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 3604 | if( !pC->useRandomRowid ){ |
drh | 7f75122 | 2009-03-17 22:33:00 +0000 | [diff] [blame] | 3605 | v = sqlite3BtreeGetCachedRowid(pC->pCursor); |
| 3606 | if( v==0 ){ |
danielk1977 | 261919c | 2005-12-06 12:52:59 +0000 | [diff] [blame] | 3607 | rc = sqlite3BtreeLast(pC->pCursor, &res); |
| 3608 | if( rc!=SQLITE_OK ){ |
| 3609 | goto abort_due_to_error; |
| 3610 | } |
drh | 32fbe34 | 2002-10-19 20:16:37 +0000 | [diff] [blame] | 3611 | if( res ){ |
| 3612 | v = 1; |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 3613 | }else{ |
danielk1977 | e0d4b06 | 2004-06-28 01:11:46 +0000 | [diff] [blame] | 3614 | sqlite3BtreeKeySize(pC->pCursor, &v); |
drh | 75f86a4 | 2005-02-17 00:03:06 +0000 | [diff] [blame] | 3615 | if( v==MAX_ROWID ){ |
drh | 32fbe34 | 2002-10-19 20:16:37 +0000 | [diff] [blame] | 3616 | pC->useRandomRowid = 1; |
| 3617 | }else{ |
| 3618 | v++; |
| 3619 | } |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 3620 | } |
drh | 3fc190c | 2001-09-14 03:24:23 +0000 | [diff] [blame] | 3621 | } |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 3622 | |
| 3623 | #ifndef SQLITE_OMIT_AUTOINCREMENT |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 3624 | if( pOp->p3 ){ |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 3625 | assert( pOp->p3>0 && pOp->p3<=p->nMem ); /* P3 is a valid memory cell */ |
| 3626 | pMem = &p->aMem[pOp->p3]; |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 3627 | REGISTER_TRACE(pOp->p3, pMem); |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 3628 | sqlite3VdbeMemIntegerify(pMem); |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 3629 | assert( (pMem->flags & MEM_Int)!=0 ); /* mem(P3) holds an integer */ |
drh | 3c024d6 | 2007-03-30 11:23:45 +0000 | [diff] [blame] | 3630 | if( pMem->u.i==MAX_ROWID || pC->useRandomRowid ){ |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 3631 | rc = SQLITE_FULL; |
| 3632 | goto abort_due_to_error; |
| 3633 | } |
drh | 3c024d6 | 2007-03-30 11:23:45 +0000 | [diff] [blame] | 3634 | if( v<pMem->u.i+1 ){ |
| 3635 | v = pMem->u.i + 1; |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 3636 | } |
drh | 3c024d6 | 2007-03-30 11:23:45 +0000 | [diff] [blame] | 3637 | pMem->u.i = v; |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 3638 | } |
| 3639 | #endif |
| 3640 | |
drh | 7f75122 | 2009-03-17 22:33:00 +0000 | [diff] [blame] | 3641 | sqlite3BtreeSetCachedRowid(pC->pCursor, v<MAX_ROWID ? v+1 : 0); |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 3642 | } |
| 3643 | if( pC->useRandomRowid ){ |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 3644 | assert( pOp->p3==0 ); /* We cannot be in random rowid mode if this is |
| 3645 | ** an AUTOINCREMENT table. */ |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 3646 | v = db->priorNewRowid; |
| 3647 | cnt = 0; |
| 3648 | do{ |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 3649 | if( cnt==0 && (v&0xffffff)==v ){ |
| 3650 | v++; |
| 3651 | }else{ |
drh | 2fa1868 | 2008-03-19 14:15:34 +0000 | [diff] [blame] | 3652 | sqlite3_randomness(sizeof(v), &v); |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 3653 | if( cnt<5 ) v &= 0xffffff; |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 3654 | } |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 3655 | rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, 0, (u64)v, 0, &res); |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 3656 | cnt++; |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 3657 | }while( cnt<100 && rc==SQLITE_OK && res==0 ); |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 3658 | db->priorNewRowid = v; |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 3659 | if( rc==SQLITE_OK && res==0 ){ |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 3660 | rc = SQLITE_FULL; |
| 3661 | goto abort_due_to_error; |
| 3662 | } |
drh | 1eaa269 | 2001-09-18 02:02:23 +0000 | [diff] [blame] | 3663 | } |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3664 | pC->rowidIsValid = 0; |
drh | a11846b | 2004-01-07 18:52:56 +0000 | [diff] [blame] | 3665 | pC->deferredMoveto = 0; |
drh | 76873ab | 2006-01-07 18:48:26 +0000 | [diff] [blame] | 3666 | pC->cacheStatus = CACHE_STALE; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3667 | } |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 3668 | MemSetTypeFlag(pOut, MEM_Int); |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 3669 | pOut->u.i = v; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3670 | break; |
| 3671 | } |
| 3672 | |
danielk1977 | 1f4aa33 | 2008-01-03 09:51:55 +0000 | [diff] [blame] | 3673 | /* Opcode: Insert P1 P2 P3 P4 P5 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3674 | ** |
jplyon | 5a56422 | 2003-06-02 06:15:58 +0000 | [diff] [blame] | 3675 | ** Write an entry into the table of cursor P1. A new entry is |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 3676 | ** created if it doesn't already exist or the data for an existing |
danielk1977 | 1f4aa33 | 2008-01-03 09:51:55 +0000 | [diff] [blame] | 3677 | ** entry is overwritten. The data is the value stored register |
| 3678 | ** number P2. The key is stored in register P3. The key must |
| 3679 | ** be an integer. |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 3680 | ** |
danielk1977 | 1f4aa33 | 2008-01-03 09:51:55 +0000 | [diff] [blame] | 3681 | ** If the OPFLAG_NCHANGE flag of P5 is set, then the row change count is |
| 3682 | ** incremented (otherwise not). If the OPFLAG_LASTROWID flag of P5 is set, |
danielk1977 | b28af71 | 2004-06-21 06:50:26 +0000 | [diff] [blame] | 3683 | ** then rowid is stored for subsequent return by the |
drh | 85b623f | 2007-12-13 21:54:09 +0000 | [diff] [blame] | 3684 | ** sqlite3_last_insert_rowid() function (otherwise it is unmodified). |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 3685 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 3686 | ** Parameter P4 may point to a string containing the table-name, or |
danielk1977 | 1f6eec5 | 2006-06-16 06:17:47 +0000 | [diff] [blame] | 3687 | ** may be NULL. If it is not NULL, then the update-hook |
| 3688 | ** (sqlite3.xUpdateCallback) is invoked following a successful insert. |
| 3689 | ** |
drh | 93aed5a | 2008-01-16 17:46:38 +0000 | [diff] [blame] | 3690 | ** (WARNING/TODO: If P1 is a pseudo-cursor and P2 is dynamically |
| 3691 | ** allocated, then ownership of P2 is transferred to the pseudo-cursor |
| 3692 | ** and register P2 becomes ephemeral. If the cursor is changed, the |
| 3693 | ** value of register P2 will then change. Make sure this does not |
| 3694 | ** cause any problems.) |
| 3695 | ** |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3696 | ** This instruction only works on tables. The equivalent instruction |
| 3697 | ** for indices is OP_IdxInsert. |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 3698 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3699 | case OP_Insert: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3700 | Mem *pData; |
| 3701 | Mem *pKey; |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 3702 | i64 iKey; /* The integer ROWID or key for the record to be inserted */ |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 3703 | VdbeCursor *pC; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3704 | int nZero; |
| 3705 | int seekResult; |
| 3706 | const char *zDb; |
| 3707 | const char *zTbl; |
| 3708 | int op; |
| 3709 | |
| 3710 | pData = &p->aMem[pOp->p2]; |
| 3711 | pKey = &p->aMem[pOp->p3]; |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 3712 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 3713 | pC = p->apCsr[pOp->p1]; |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 3714 | assert( pC!=0 ); |
| 3715 | assert( pC->pCursor!=0 || pC->pseudoTable ); |
| 3716 | assert( pKey->flags & MEM_Int ); |
| 3717 | assert( pC->isTable ); |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 3718 | REGISTER_TRACE(pOp->p2, pData); |
| 3719 | REGISTER_TRACE(pOp->p3, pKey); |
danielk1977 | 5f8d8a8 | 2004-05-11 00:28:42 +0000 | [diff] [blame] | 3720 | |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 3721 | iKey = pKey->u.i; |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 3722 | if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++; |
| 3723 | if( pOp->p5 & OPFLAG_LASTROWID ) db->lastRowid = pKey->u.i; |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 3724 | if( pData->flags & MEM_Null ){ |
| 3725 | pData->z = 0; |
| 3726 | pData->n = 0; |
| 3727 | }else{ |
| 3728 | assert( pData->flags & (MEM_Blob|MEM_Str) ); |
| 3729 | } |
| 3730 | if( pC->pseudoTable ){ |
danielk1977 | 9882d99 | 2008-03-27 17:59:01 +0000 | [diff] [blame] | 3731 | if( !pC->ephemPseudoTable ){ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 3732 | sqlite3DbFree(db, pC->pData); |
danielk1977 | 9882d99 | 2008-03-27 17:59:01 +0000 | [diff] [blame] | 3733 | } |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 3734 | pC->iKey = iKey; |
| 3735 | pC->nData = pData->n; |
drh | 27731d7 | 2009-06-22 12:05:10 +0000 | [diff] [blame^] | 3736 | if( pC->ephemPseudoTable || pData->z==pData->zMalloc ){ |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 3737 | pC->pData = pData->z; |
danielk1977 | 9882d99 | 2008-03-27 17:59:01 +0000 | [diff] [blame] | 3738 | if( !pC->ephemPseudoTable ){ |
| 3739 | pData->flags &= ~MEM_Dyn; |
| 3740 | pData->flags |= MEM_Ephem; |
danielk1977 | 5f09613 | 2008-03-28 15:44:09 +0000 | [diff] [blame] | 3741 | pData->zMalloc = 0; |
danielk1977 | 9882d99 | 2008-03-27 17:59:01 +0000 | [diff] [blame] | 3742 | } |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 3743 | }else{ |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 3744 | pC->pData = sqlite3Malloc( pC->nData+2 ); |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 3745 | if( !pC->pData ) goto no_mem; |
| 3746 | memcpy(pC->pData, pData->z, pC->nData); |
| 3747 | pC->pData[pC->nData] = 0; |
| 3748 | pC->pData[pC->nData+1] = 0; |
| 3749 | } |
| 3750 | pC->nullRow = 0; |
| 3751 | }else{ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3752 | seekResult = ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0); |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 3753 | if( pData->flags & MEM_Zero ){ |
drh | 8df3284 | 2008-12-09 02:51:23 +0000 | [diff] [blame] | 3754 | nZero = pData->u.nZero; |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 3755 | }else{ |
| 3756 | nZero = 0; |
| 3757 | } |
drh | 7f75122 | 2009-03-17 22:33:00 +0000 | [diff] [blame] | 3758 | sqlite3BtreeSetCachedRowid(pC->pCursor, 0); |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 3759 | rc = sqlite3BtreeInsert(pC->pCursor, 0, iKey, |
| 3760 | pData->z, pData->n, nZero, |
danielk1977 | de63035 | 2009-05-04 11:42:29 +0000 | [diff] [blame] | 3761 | pOp->p5 & OPFLAG_APPEND, seekResult |
| 3762 | ); |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 3763 | } |
| 3764 | |
| 3765 | pC->rowidIsValid = 0; |
| 3766 | pC->deferredMoveto = 0; |
| 3767 | pC->cacheStatus = CACHE_STALE; |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 +0000 | [diff] [blame] | 3768 | |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 3769 | /* Invoke the update-hook if required. */ |
| 3770 | if( rc==SQLITE_OK && db->xUpdateCallback && pOp->p4.z ){ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3771 | zDb = db->aDb[pC->iDb].zName; |
| 3772 | zTbl = pOp->p4.z; |
| 3773 | op = ((pOp->p5 & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_INSERT); |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 3774 | assert( pC->isTable ); |
| 3775 | db->xUpdateCallback(db->pUpdateArg, op, zDb, zTbl, iKey); |
| 3776 | assert( pC->iDb>=0 ); |
| 3777 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3778 | break; |
| 3779 | } |
| 3780 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 3781 | /* Opcode: Delete P1 P2 * P4 * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3782 | ** |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 3783 | ** Delete the record at which the P1 cursor is currently pointing. |
| 3784 | ** |
| 3785 | ** The cursor will be left pointing at either the next or the previous |
| 3786 | ** 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] | 3787 | ** the next Next instruction will be a no-op. Hence it is OK to delete |
| 3788 | ** a record from within an Next loop. |
drh | c8d30ac | 2002-04-12 10:08:59 +0000 | [diff] [blame] | 3789 | ** |
rdc | b0c374f | 2004-02-20 22:53:38 +0000 | [diff] [blame] | 3790 | ** 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] | 3791 | ** incremented (otherwise not). |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3792 | ** |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 3793 | ** P1 must not be pseudo-table. It has to be a real table with |
| 3794 | ** multiple rows. |
| 3795 | ** |
| 3796 | ** If P4 is not NULL, then it is the name of the table that P1 is |
| 3797 | ** pointing to. The update hook will be invoked, if it exists. |
| 3798 | ** If P4 is not NULL then the P1 cursor must have been positioned |
| 3799 | ** using OP_NotFound prior to invoking this opcode. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3800 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3801 | case OP_Delete: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3802 | i64 iKey; |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 3803 | VdbeCursor *pC; |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 3804 | |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3805 | iKey = 0; |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 3806 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 3807 | pC = p->apCsr[pOp->p1]; |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 3808 | assert( pC!=0 ); |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 3809 | assert( pC->pCursor!=0 ); /* Only valid for real tables, no pseudotables */ |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 +0000 | [diff] [blame] | 3810 | |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 3811 | /* If the update-hook will be invoked, set iKey to the rowid of the |
| 3812 | ** row being deleted. |
| 3813 | */ |
| 3814 | if( db->xUpdateCallback && pOp->p4.z ){ |
| 3815 | assert( pC->isTable ); |
| 3816 | assert( pC->rowidIsValid ); /* lastRowid set by previous OP_NotFound */ |
| 3817 | iKey = pC->lastRowid; |
| 3818 | } |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 +0000 | [diff] [blame] | 3819 | |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 3820 | rc = sqlite3VdbeCursorMoveto(pC); |
| 3821 | if( rc ) goto abort_due_to_error; |
drh | 7f75122 | 2009-03-17 22:33:00 +0000 | [diff] [blame] | 3822 | sqlite3BtreeSetCachedRowid(pC->pCursor, 0); |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 3823 | rc = sqlite3BtreeDelete(pC->pCursor); |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 3824 | pC->cacheStatus = CACHE_STALE; |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 +0000 | [diff] [blame] | 3825 | |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 3826 | /* Invoke the update-hook if required. */ |
| 3827 | if( rc==SQLITE_OK && db->xUpdateCallback && pOp->p4.z ){ |
| 3828 | const char *zDb = db->aDb[pC->iDb].zName; |
| 3829 | const char *zTbl = pOp->p4.z; |
| 3830 | db->xUpdateCallback(db->pUpdateArg, SQLITE_DELETE, zDb, zTbl, iKey); |
| 3831 | assert( pC->iDb>=0 ); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3832 | } |
danielk1977 | b28af71 | 2004-06-21 06:50:26 +0000 | [diff] [blame] | 3833 | if( pOp->p2 & OPFLAG_NCHANGE ) p->nChange++; |
rdc | b0c374f | 2004-02-20 22:53:38 +0000 | [diff] [blame] | 3834 | break; |
| 3835 | } |
| 3836 | |
danielk1977 | b28af71 | 2004-06-21 06:50:26 +0000 | [diff] [blame] | 3837 | /* Opcode: ResetCount P1 * * |
rdc | b0c374f | 2004-02-20 22:53:38 +0000 | [diff] [blame] | 3838 | ** |
danielk1977 | b28af71 | 2004-06-21 06:50:26 +0000 | [diff] [blame] | 3839 | ** This opcode resets the VMs internal change counter to 0. If P1 is true, |
| 3840 | ** then the value of the change counter is copied to the database handle |
| 3841 | ** change counter (returned by subsequent calls to sqlite3_changes()) |
| 3842 | ** before it is reset. This is used by trigger programs. |
rdc | b0c374f | 2004-02-20 22:53:38 +0000 | [diff] [blame] | 3843 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3844 | case OP_ResetCount: { |
danielk1977 | b28af71 | 2004-06-21 06:50:26 +0000 | [diff] [blame] | 3845 | if( pOp->p1 ){ |
drh | 344737f | 2004-09-19 00:50:20 +0000 | [diff] [blame] | 3846 | sqlite3VdbeSetChanges(db, p->nChange); |
danielk1977 | b28af71 | 2004-06-21 06:50:26 +0000 | [diff] [blame] | 3847 | } |
| 3848 | p->nChange = 0; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3849 | break; |
| 3850 | } |
| 3851 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 3852 | /* Opcode: RowData P1 P2 * * * |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3853 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 3854 | ** Write into register P2 the complete row data for cursor P1. |
| 3855 | ** There is no interpretation of the data. |
| 3856 | ** It is just copied onto the P2 register exactly as |
danielk1977 | 96cb76f | 2008-01-04 13:24:28 +0000 | [diff] [blame] | 3857 | ** it is found in the database file. |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3858 | ** |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 3859 | ** If the P1 cursor must be pointing to a valid row (not a NULL row) |
| 3860 | ** of a real table, not a pseudo-table. |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3861 | */ |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 3862 | /* Opcode: RowKey P1 P2 * * * |
drh | 143f3c4 | 2004-01-07 20:37:52 +0000 | [diff] [blame] | 3863 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 3864 | ** Write into register P2 the complete row key for cursor P1. |
| 3865 | ** There is no interpretation of the data. |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3866 | ** The key is copied onto the P3 register exactly as |
danielk1977 | 96cb76f | 2008-01-04 13:24:28 +0000 | [diff] [blame] | 3867 | ** it is found in the database file. |
drh | 143f3c4 | 2004-01-07 20:37:52 +0000 | [diff] [blame] | 3868 | ** |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 3869 | ** If the P1 cursor must be pointing to a valid row (not a NULL row) |
| 3870 | ** of a real table, not a pseudo-table. |
drh | 143f3c4 | 2004-01-07 20:37:52 +0000 | [diff] [blame] | 3871 | */ |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 3872 | case OP_RowKey: |
| 3873 | case OP_RowData: { |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 3874 | VdbeCursor *pC; |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 3875 | BtCursor *pCrsr; |
danielk1977 | e0d4b06 | 2004-06-28 01:11:46 +0000 | [diff] [blame] | 3876 | u32 n; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3877 | i64 n64; |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3878 | |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 3879 | pOut = &p->aMem[pOp->p2]; |
| 3880 | |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3881 | /* Note that RowKey and RowData are really exactly the same instruction */ |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 3882 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 3883 | pC = p->apCsr[pOp->p1]; |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3884 | assert( pC->isTable || pOp->opcode==OP_RowKey ); |
| 3885 | assert( pC->isIndex || pOp->opcode==OP_RowData ); |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 3886 | assert( pC!=0 ); |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 3887 | assert( pC->nullRow==0 ); |
| 3888 | assert( pC->pseudoTable==0 ); |
| 3889 | assert( pC->pCursor!=0 ); |
| 3890 | pCrsr = pC->pCursor; |
| 3891 | rc = sqlite3VdbeCursorMoveto(pC); |
| 3892 | if( rc ) goto abort_due_to_error; |
| 3893 | if( pC->isIndex ){ |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 3894 | assert( !pC->isTable ); |
| 3895 | sqlite3BtreeKeySize(pCrsr, &n64); |
drh | bb4957f | 2008-03-20 14:03:29 +0000 | [diff] [blame] | 3896 | if( n64>db->aLimit[SQLITE_LIMIT_LENGTH] ){ |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 3897 | goto too_big; |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3898 | } |
drh | bfb19dc | 2009-06-05 16:46:53 +0000 | [diff] [blame] | 3899 | n = (u32)n64; |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 3900 | }else{ |
| 3901 | sqlite3BtreeDataSize(pCrsr, &n); |
shane | 75ac1de | 2009-06-09 18:58:52 +0000 | [diff] [blame] | 3902 | if( n>(u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){ |
drh | 023ae03 | 2007-05-08 12:12:16 +0000 | [diff] [blame] | 3903 | goto too_big; |
| 3904 | } |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 3905 | } |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 3906 | if( sqlite3VdbeMemGrow(pOut, n, 0) ){ |
| 3907 | goto no_mem; |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 3908 | } |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 3909 | pOut->n = n; |
| 3910 | MemSetTypeFlag(pOut, MEM_Blob); |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 3911 | if( pC->isIndex ){ |
| 3912 | rc = sqlite3BtreeKey(pCrsr, 0, n, pOut->z); |
| 3913 | }else{ |
| 3914 | rc = sqlite3BtreeData(pCrsr, 0, n, pOut->z); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3915 | } |
danielk1977 | 96cb76f | 2008-01-04 13:24:28 +0000 | [diff] [blame] | 3916 | pOut->enc = SQLITE_UTF8; /* In case the blob is ever cast to text */ |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 3917 | UPDATE_MAX_BLOBSIZE(pOut); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3918 | break; |
| 3919 | } |
| 3920 | |
drh | 2133d82 | 2008-01-03 18:44:59 +0000 | [diff] [blame] | 3921 | /* Opcode: Rowid P1 P2 * * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3922 | ** |
drh | 2133d82 | 2008-01-03 18:44:59 +0000 | [diff] [blame] | 3923 | ** 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] | 3924 | ** P1 is currently point to. |
drh | 044925b | 2009-04-22 17:15:02 +0000 | [diff] [blame] | 3925 | ** |
| 3926 | ** P1 can be either an ordinary table or a virtual table. There used to |
| 3927 | ** be a separate OP_VRowid opcode for use with virtual tables, but this |
| 3928 | ** one opcode now works for both table types. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3929 | */ |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 3930 | case OP_Rowid: { /* out2-prerelease */ |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 3931 | VdbeCursor *pC; |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 3932 | i64 v; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 3933 | sqlite3_vtab *pVtab; |
| 3934 | const sqlite3_module *pModule; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3935 | |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 3936 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 3937 | pC = p->apCsr[pOp->p1]; |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 3938 | assert( pC!=0 ); |
drh | 044925b | 2009-04-22 17:15:02 +0000 | [diff] [blame] | 3939 | if( pC->nullRow ){ |
| 3940 | /* Do nothing so that reg[P2] remains NULL */ |
| 3941 | break; |
| 3942 | }else if( pC->deferredMoveto ){ |
drh | 6149526 | 2009-04-22 15:32:59 +0000 | [diff] [blame] | 3943 | v = pC->movetoTarget; |
drh | 044925b | 2009-04-22 17:15:02 +0000 | [diff] [blame] | 3944 | }else if( pC->pseudoTable ){ |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 3945 | v = pC->iKey; |
drh | 044925b | 2009-04-22 17:15:02 +0000 | [diff] [blame] | 3946 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 3947 | }else if( pC->pVtabCursor ){ |
drh | 044925b | 2009-04-22 17:15:02 +0000 | [diff] [blame] | 3948 | pVtab = pC->pVtabCursor->pVtab; |
| 3949 | pModule = pVtab->pModule; |
| 3950 | assert( pModule->xRowid ); |
| 3951 | if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse; |
| 3952 | rc = pModule->xRowid(pC->pVtabCursor, &v); |
| 3953 | sqlite3DbFree(db, p->zErrMsg); |
| 3954 | p->zErrMsg = pVtab->zErrMsg; |
| 3955 | pVtab->zErrMsg = 0; |
| 3956 | if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse; |
| 3957 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3958 | }else{ |
drh | 6149526 | 2009-04-22 15:32:59 +0000 | [diff] [blame] | 3959 | rc = sqlite3VdbeCursorMoveto(pC); |
| 3960 | if( rc ) goto abort_due_to_error; |
| 3961 | if( pC->rowidIsValid ){ |
| 3962 | v = pC->lastRowid; |
drh | 6149526 | 2009-04-22 15:32:59 +0000 | [diff] [blame] | 3963 | }else{ |
| 3964 | assert( pC->pCursor!=0 ); |
| 3965 | sqlite3BtreeKeySize(pC->pCursor, &v); |
drh | 6149526 | 2009-04-22 15:32:59 +0000 | [diff] [blame] | 3966 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3967 | } |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 3968 | pOut->u.i = v; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 3969 | MemSetTypeFlag(pOut, MEM_Int); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3970 | break; |
| 3971 | } |
| 3972 | |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3973 | /* Opcode: NullRow P1 * * * * |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 3974 | ** |
| 3975 | ** Move the cursor P1 to a null row. Any OP_Column operations |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3976 | ** that occur while the cursor is on the null row will always |
| 3977 | ** write a NULL. |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 3978 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3979 | case OP_NullRow: { |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 3980 | VdbeCursor *pC; |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 3981 | |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 3982 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 3983 | pC = p->apCsr[pOp->p1]; |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 3984 | assert( pC!=0 ); |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame] | 3985 | pC->nullRow = 1; |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3986 | pC->rowidIsValid = 0; |
danielk1977 | be51a65 | 2008-10-08 17:58:48 +0000 | [diff] [blame] | 3987 | if( pC->pCursor ){ |
| 3988 | sqlite3BtreeClearCursor(pC->pCursor); |
| 3989 | } |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 3990 | break; |
| 3991 | } |
| 3992 | |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3993 | /* Opcode: Last P1 P2 * * * |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 3994 | ** |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3995 | ** The next use of the Rowid or Column or Next instruction for P1 |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 3996 | ** will refer to the last entry in the database table or index. |
| 3997 | ** If the table or index is empty and P2>0, then jump immediately to P2. |
| 3998 | ** If P2 is 0 or if the table or index is not empty, fall through |
| 3999 | ** to the following instruction. |
| 4000 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4001 | case OP_Last: { /* jump */ |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 4002 | VdbeCursor *pC; |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 4003 | BtCursor *pCrsr; |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 4004 | int res; |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 4005 | |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 4006 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 4007 | pC = p->apCsr[pOp->p1]; |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 4008 | assert( pC!=0 ); |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 4009 | pCrsr = pC->pCursor; |
| 4010 | assert( pCrsr!=0 ); |
| 4011 | rc = sqlite3BtreeLast(pCrsr, &res); |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 4012 | pC->nullRow = (u8)res; |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 4013 | pC->deferredMoveto = 0; |
drh | a7e7706 | 2009-01-14 00:55:09 +0000 | [diff] [blame] | 4014 | pC->rowidIsValid = 0; |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 4015 | pC->cacheStatus = CACHE_STALE; |
| 4016 | if( res && pOp->p2>0 ){ |
| 4017 | pc = pOp->p2 - 1; |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 4018 | } |
| 4019 | break; |
| 4020 | } |
| 4021 | |
drh | 0342b1f | 2005-09-01 03:07:44 +0000 | [diff] [blame] | 4022 | |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4023 | /* Opcode: Sort P1 P2 * * * |
drh | 0342b1f | 2005-09-01 03:07:44 +0000 | [diff] [blame] | 4024 | ** |
| 4025 | ** This opcode does exactly the same thing as OP_Rewind except that |
| 4026 | ** it increments an undocumented global variable used for testing. |
| 4027 | ** |
| 4028 | ** Sorting is accomplished by writing records into a sorting index, |
| 4029 | ** then rewinding that index and playing it back from beginning to |
| 4030 | ** end. We use the OP_Sort opcode instead of OP_Rewind to do the |
| 4031 | ** rewinding so that the global variable will be incremented and |
| 4032 | ** regression tests can determine whether or not the optimizer is |
| 4033 | ** correctly optimizing out sorts. |
| 4034 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4035 | case OP_Sort: { /* jump */ |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 4036 | #ifdef SQLITE_TEST |
drh | 0342b1f | 2005-09-01 03:07:44 +0000 | [diff] [blame] | 4037 | sqlite3_sort_count++; |
drh | 4db38a7 | 2005-09-01 12:16:28 +0000 | [diff] [blame] | 4038 | sqlite3_search_count--; |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 4039 | #endif |
drh | d1d3848 | 2008-10-07 23:46:38 +0000 | [diff] [blame] | 4040 | p->aCounter[SQLITE_STMTSTATUS_SORT-1]++; |
drh | 0342b1f | 2005-09-01 03:07:44 +0000 | [diff] [blame] | 4041 | /* Fall through into OP_Rewind */ |
| 4042 | } |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4043 | /* Opcode: Rewind P1 P2 * * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4044 | ** |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 4045 | ** The next use of the Rowid or Column or Next instruction for P1 |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 4046 | ** will refer to the first entry in the database table or index. |
| 4047 | ** If the table or index is empty and P2>0, then jump immediately to P2. |
| 4048 | ** If P2 is 0 or if the table or index is not empty, fall through |
| 4049 | ** to the following instruction. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4050 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4051 | case OP_Rewind: { /* jump */ |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 4052 | VdbeCursor *pC; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4053 | BtCursor *pCrsr; |
drh | f4dada7 | 2004-05-11 09:57:35 +0000 | [diff] [blame] | 4054 | int res; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4055 | |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 4056 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 4057 | pC = p->apCsr[pOp->p1]; |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 4058 | assert( pC!=0 ); |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 4059 | if( (pCrsr = pC->pCursor)!=0 ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 4060 | rc = sqlite3BtreeFirst(pCrsr, &res); |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 4061 | pC->atFirst = res==0 ?1:0; |
drh | a11846b | 2004-01-07 18:52:56 +0000 | [diff] [blame] | 4062 | pC->deferredMoveto = 0; |
drh | 76873ab | 2006-01-07 18:48:26 +0000 | [diff] [blame] | 4063 | pC->cacheStatus = CACHE_STALE; |
drh | a7e7706 | 2009-01-14 00:55:09 +0000 | [diff] [blame] | 4064 | pC->rowidIsValid = 0; |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 4065 | }else{ |
drh | f4dada7 | 2004-05-11 09:57:35 +0000 | [diff] [blame] | 4066 | res = 1; |
| 4067 | } |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 4068 | pC->nullRow = (u8)res; |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 4069 | assert( pOp->p2>0 && pOp->p2<p->nOp ); |
| 4070 | if( res ){ |
drh | f4dada7 | 2004-05-11 09:57:35 +0000 | [diff] [blame] | 4071 | pc = pOp->p2 - 1; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4072 | } |
| 4073 | break; |
| 4074 | } |
| 4075 | |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4076 | /* Opcode: Next P1 P2 * * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4077 | ** |
| 4078 | ** 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] | 4079 | ** table or index. If there are no more key/value pairs then fall through |
| 4080 | ** to the following instruction. But if the cursor advance was successful, |
| 4081 | ** jump immediately to P2. |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 4082 | ** |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 4083 | ** The P1 cursor must be for a real table, not a pseudo-table. |
| 4084 | ** |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 4085 | ** See also: Prev |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 4086 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4087 | /* Opcode: Prev P1 P2 * * * |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 4088 | ** |
| 4089 | ** Back up cursor P1 so that it points to the previous key/data pair in its |
| 4090 | ** table or index. If there is no previous key/value pairs then fall through |
| 4091 | ** to the following instruction. But if the cursor backup was successful, |
| 4092 | ** jump immediately to P2. |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 4093 | ** |
| 4094 | ** The P1 cursor must be for a real table, not a pseudo-table. |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 4095 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4096 | case OP_Prev: /* jump */ |
| 4097 | case OP_Next: { /* jump */ |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 4098 | VdbeCursor *pC; |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 4099 | BtCursor *pCrsr; |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 4100 | int res; |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 4101 | |
drh | caec2f1 | 2003-01-07 02:47:47 +0000 | [diff] [blame] | 4102 | CHECK_FOR_INTERRUPT; |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 4103 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame] | 4104 | pC = p->apCsr[pOp->p1]; |
drh | 72e8fa4 | 2007-03-28 14:30:06 +0000 | [diff] [blame] | 4105 | if( pC==0 ){ |
| 4106 | break; /* See ticket #2273 */ |
| 4107 | } |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 4108 | pCrsr = pC->pCursor; |
| 4109 | assert( pCrsr ); |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 4110 | res = 1; |
| 4111 | assert( pC->deferredMoveto==0 ); |
| 4112 | rc = pOp->opcode==OP_Next ? sqlite3BtreeNext(pCrsr, &res) : |
| 4113 | sqlite3BtreePrevious(pCrsr, &res); |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 4114 | pC->nullRow = (u8)res; |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 4115 | pC->cacheStatus = CACHE_STALE; |
| 4116 | if( res==0 ){ |
| 4117 | pc = pOp->p2 - 1; |
drh | d1d3848 | 2008-10-07 23:46:38 +0000 | [diff] [blame] | 4118 | if( pOp->p5 ) p->aCounter[pOp->p5-1]++; |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 4119 | #ifdef SQLITE_TEST |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 4120 | sqlite3_search_count++; |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 4121 | #endif |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 4122 | } |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 4123 | pC->rowidIsValid = 0; |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 4124 | break; |
| 4125 | } |
| 4126 | |
danielk1977 | de63035 | 2009-05-04 11:42:29 +0000 | [diff] [blame] | 4127 | /* Opcode: IdxInsert P1 P2 P3 * P5 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4128 | ** |
drh | aa9b896 | 2008-01-08 02:57:55 +0000 | [diff] [blame] | 4129 | ** Register P2 holds a SQL index key made using the |
drh | 9437bd2 | 2009-02-01 00:29:56 +0000 | [diff] [blame] | 4130 | ** MakeRecord instructions. This opcode writes that key |
drh | ee32e0a | 2006-01-10 19:45:49 +0000 | [diff] [blame] | 4131 | ** into the index P1. Data for the entry is nil. |
drh | 717e640 | 2001-09-27 03:22:32 +0000 | [diff] [blame] | 4132 | ** |
drh | aa9b896 | 2008-01-08 02:57:55 +0000 | [diff] [blame] | 4133 | ** 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] | 4134 | ** insert is likely to be an append. |
| 4135 | ** |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 4136 | ** This instruction only works for indices. The equivalent instruction |
| 4137 | ** for tables is OP_Insert. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4138 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4139 | case OP_IdxInsert: { /* in2 */ |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 4140 | VdbeCursor *pC; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4141 | BtCursor *pCrsr; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 4142 | int nKey; |
| 4143 | const char *zKey; |
| 4144 | |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 4145 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 4146 | pC = p->apCsr[pOp->p1]; |
| 4147 | assert( pC!=0 ); |
drh | aa9b896 | 2008-01-08 02:57:55 +0000 | [diff] [blame] | 4148 | assert( pIn2->flags & MEM_Blob ); |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 4149 | pCrsr = pC->pCursor; |
| 4150 | if( pCrsr!=0 ){ |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 4151 | assert( pC->isTable==0 ); |
drh | aa9b896 | 2008-01-08 02:57:55 +0000 | [diff] [blame] | 4152 | rc = ExpandBlob(pIn2); |
danielk1977 | d908f5a | 2007-05-11 07:08:28 +0000 | [diff] [blame] | 4153 | if( rc==SQLITE_OK ){ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 4154 | nKey = pIn2->n; |
| 4155 | zKey = pIn2->z; |
danielk1977 | de63035 | 2009-05-04 11:42:29 +0000 | [diff] [blame] | 4156 | rc = sqlite3BtreeInsert(pCrsr, zKey, nKey, "", 0, 0, pOp->p3, |
| 4157 | ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0) |
| 4158 | ); |
danielk1977 | d908f5a | 2007-05-11 07:08:28 +0000 | [diff] [blame] | 4159 | assert( pC->deferredMoveto==0 ); |
| 4160 | pC->cacheStatus = CACHE_STALE; |
| 4161 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4162 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4163 | break; |
| 4164 | } |
| 4165 | |
drh | d1d3848 | 2008-10-07 23:46:38 +0000 | [diff] [blame] | 4166 | /* Opcode: IdxDelete P1 P2 P3 * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4167 | ** |
drh | e14006d | 2008-03-25 17:23:32 +0000 | [diff] [blame] | 4168 | ** The content of P3 registers starting at register P2 form |
| 4169 | ** an unpacked index key. This opcode removes that entry from the |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 4170 | ** index opened by cursor P1. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4171 | */ |
drh | e14006d | 2008-03-25 17:23:32 +0000 | [diff] [blame] | 4172 | case OP_IdxDelete: { |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 4173 | VdbeCursor *pC; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4174 | BtCursor *pCrsr; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 4175 | |
drh | e14006d | 2008-03-25 17:23:32 +0000 | [diff] [blame] | 4176 | assert( pOp->p3>0 ); |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 4177 | assert( pOp->p2>0 && pOp->p2+pOp->p3<=p->nMem+1 ); |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 4178 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 4179 | pC = p->apCsr[pOp->p1]; |
| 4180 | assert( pC!=0 ); |
| 4181 | pCrsr = pC->pCursor; |
| 4182 | if( pCrsr!=0 ){ |
danielk1977 | 75bab7d | 2006-01-23 13:09:45 +0000 | [diff] [blame] | 4183 | int res; |
drh | e14006d | 2008-03-25 17:23:32 +0000 | [diff] [blame] | 4184 | UnpackedRecord r; |
| 4185 | r.pKeyInfo = pC->pKeyInfo; |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 4186 | r.nField = (u16)pOp->p3; |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 4187 | r.flags = 0; |
drh | e14006d | 2008-03-25 17:23:32 +0000 | [diff] [blame] | 4188 | r.aMem = &p->aMem[pOp->p2]; |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 4189 | rc = sqlite3BtreeMovetoUnpacked(pCrsr, &r, 0, 0, &res); |
danielk1977 | 75bab7d | 2006-01-23 13:09:45 +0000 | [diff] [blame] | 4190 | if( rc==SQLITE_OK && res==0 ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 4191 | rc = sqlite3BtreeDelete(pCrsr); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4192 | } |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 4193 | assert( pC->deferredMoveto==0 ); |
drh | 76873ab | 2006-01-07 18:48:26 +0000 | [diff] [blame] | 4194 | pC->cacheStatus = CACHE_STALE; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4195 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4196 | break; |
| 4197 | } |
| 4198 | |
drh | 2133d82 | 2008-01-03 18:44:59 +0000 | [diff] [blame] | 4199 | /* Opcode: IdxRowid P1 P2 * * * |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 4200 | ** |
drh | 2133d82 | 2008-01-03 18:44:59 +0000 | [diff] [blame] | 4201 | ** 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] | 4202 | ** the end of the index key pointed to by cursor P1. This integer should be |
| 4203 | ** the rowid of the table entry to which this index entry points. |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 4204 | ** |
drh | 9437bd2 | 2009-02-01 00:29:56 +0000 | [diff] [blame] | 4205 | ** See also: Rowid, MakeRecord. |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 4206 | */ |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 4207 | case OP_IdxRowid: { /* out2-prerelease */ |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 4208 | BtCursor *pCrsr; |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 4209 | VdbeCursor *pC; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 4210 | i64 rowid; |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 4211 | |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 4212 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 4213 | pC = p->apCsr[pOp->p1]; |
| 4214 | assert( pC!=0 ); |
| 4215 | pCrsr = pC->pCursor; |
| 4216 | if( pCrsr!=0 ){ |
danielk1977 | c4d201c | 2009-04-07 09:16:56 +0000 | [diff] [blame] | 4217 | rc = sqlite3VdbeCursorMoveto(pC); |
| 4218 | if( rc ) goto abort_due_to_error; |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame] | 4219 | assert( pC->deferredMoveto==0 ); |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 4220 | assert( pC->isTable==0 ); |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 4221 | if( !pC->nullRow ){ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 4222 | rc = sqlite3VdbeIdxRowid(pCrsr, &rowid); |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 4223 | if( rc!=SQLITE_OK ){ |
| 4224 | goto abort_due_to_error; |
| 4225 | } |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 4226 | MemSetTypeFlag(pOut, MEM_Int); |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 4227 | pOut->u.i = rowid; |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 4228 | } |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 4229 | } |
| 4230 | break; |
| 4231 | } |
| 4232 | |
danielk1977 | 61dd583 | 2008-04-18 11:31:12 +0000 | [diff] [blame] | 4233 | /* Opcode: IdxGE P1 P2 P3 P4 P5 |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 4234 | ** |
danielk1977 | 61dd583 | 2008-04-18 11:31:12 +0000 | [diff] [blame] | 4235 | ** The P4 register values beginning with P3 form an unpacked index |
| 4236 | ** key that omits the ROWID. Compare this key value against the index |
| 4237 | ** that P1 is currently pointing to, ignoring the ROWID on the P1 index. |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 4238 | ** |
danielk1977 | 61dd583 | 2008-04-18 11:31:12 +0000 | [diff] [blame] | 4239 | ** If the P1 index entry is greater than or equal to the key value |
| 4240 | ** then jump to P2. Otherwise fall through to the next instruction. |
drh | 772ae62 | 2004-05-19 13:13:08 +0000 | [diff] [blame] | 4241 | ** |
danielk1977 | 61dd583 | 2008-04-18 11:31:12 +0000 | [diff] [blame] | 4242 | ** If P5 is non-zero then the key value is increased by an epsilon |
| 4243 | ** prior to the comparison. This make the opcode work like IdxGT except |
| 4244 | ** that if the key from register P3 is a prefix of the key in the cursor, |
| 4245 | ** the result is false whereas it would be true with IdxGT. |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 4246 | */ |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4247 | /* Opcode: IdxLT P1 P2 P3 * P5 |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 4248 | ** |
danielk1977 | 61dd583 | 2008-04-18 11:31:12 +0000 | [diff] [blame] | 4249 | ** The P4 register values beginning with P3 form an unpacked index |
| 4250 | ** key that omits the ROWID. Compare this key value against the index |
| 4251 | ** that P1 is currently pointing to, ignoring the ROWID on the P1 index. |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 4252 | ** |
danielk1977 | 61dd583 | 2008-04-18 11:31:12 +0000 | [diff] [blame] | 4253 | ** If the P1 index entry is less than the key value then jump to P2. |
| 4254 | ** Otherwise fall through to the next instruction. |
drh | 772ae62 | 2004-05-19 13:13:08 +0000 | [diff] [blame] | 4255 | ** |
danielk1977 | 61dd583 | 2008-04-18 11:31:12 +0000 | [diff] [blame] | 4256 | ** If P5 is non-zero then the key value is increased by an epsilon prior |
| 4257 | ** to the comparison. This makes the opcode work like IdxLE. |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 4258 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4259 | case OP_IdxLT: /* jump, in3 */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4260 | case OP_IdxGE: { /* jump, in3 */ |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 4261 | VdbeCursor *pC; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 4262 | int res; |
| 4263 | UnpackedRecord r; |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 4264 | |
drh | 653b82a | 2009-06-22 11:10:47 +0000 | [diff] [blame] | 4265 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 4266 | pC = p->apCsr[pOp->p1]; |
| 4267 | assert( pC!=0 ); |
| 4268 | if( pC->pCursor!=0 ){ |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame] | 4269 | assert( pC->deferredMoveto==0 ); |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 4270 | assert( pOp->p5==0 || pOp->p5==1 ); |
danielk1977 | 61dd583 | 2008-04-18 11:31:12 +0000 | [diff] [blame] | 4271 | assert( pOp->p4type==P4_INT32 ); |
| 4272 | r.pKeyInfo = pC->pKeyInfo; |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 4273 | r.nField = (u16)pOp->p4.i; |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 4274 | if( pOp->p5 ){ |
| 4275 | r.flags = UNPACKED_INCRKEY | UNPACKED_IGNORE_ROWID; |
| 4276 | }else{ |
| 4277 | r.flags = UNPACKED_IGNORE_ROWID; |
| 4278 | } |
danielk1977 | 61dd583 | 2008-04-18 11:31:12 +0000 | [diff] [blame] | 4279 | r.aMem = &p->aMem[pOp->p3]; |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 4280 | rc = sqlite3VdbeIdxKeyCompare(pC, &r, &res); |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 4281 | if( pOp->opcode==OP_IdxLT ){ |
| 4282 | res = -res; |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 4283 | }else{ |
| 4284 | assert( pOp->opcode==OP_IdxGE ); |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 4285 | res++; |
| 4286 | } |
| 4287 | if( res>0 ){ |
| 4288 | pc = pOp->p2 - 1 ; |
| 4289 | } |
| 4290 | } |
| 4291 | break; |
| 4292 | } |
| 4293 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4294 | /* Opcode: Destroy P1 P2 P3 * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4295 | ** |
| 4296 | ** Delete an entire database table or index whose root page in the database |
| 4297 | ** file is given by P1. |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 4298 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4299 | ** The table being destroyed is in the main database file if P3==0. If |
| 4300 | ** 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] | 4301 | ** that is used to store tables create using CREATE TEMPORARY TABLE. |
| 4302 | ** |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 4303 | ** If AUTOVACUUM is enabled then it is possible that another root page |
| 4304 | ** might be moved into the newly deleted root page in order to keep all |
| 4305 | ** root pages contiguous at the beginning of the database. The former |
| 4306 | ** value of the root page that moved - its value before the move occurred - |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4307 | ** is stored in register P2. If no page |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4308 | ** movement was required (because the table being dropped was already |
| 4309 | ** the last one in the database) then a zero is stored in register P2. |
| 4310 | ** If AUTOVACUUM is disabled then a zero is stored in register P2. |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 4311 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 4312 | ** See also: Clear |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4313 | */ |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4314 | case OP_Destroy: { /* out2-prerelease */ |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 4315 | int iMoved; |
drh | 3765df4 | 2006-06-28 18:18:09 +0000 | [diff] [blame] | 4316 | int iCnt; |
drh | 5a91a53 | 2007-01-05 16:39:43 +0000 | [diff] [blame] | 4317 | Vdbe *pVdbe; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 4318 | int iDb; |
| 4319 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
danielk1977 | 212b218 | 2006-06-23 14:32:08 +0000 | [diff] [blame] | 4320 | iCnt = 0; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 4321 | for(pVdbe=db->pVdbe; pVdbe; pVdbe = pVdbe->pNext){ |
danielk1977 | 212b218 | 2006-06-23 14:32:08 +0000 | [diff] [blame] | 4322 | if( pVdbe->magic==VDBE_MAGIC_RUN && pVdbe->inVtabMethod<2 && pVdbe->pc>=0 ){ |
| 4323 | iCnt++; |
| 4324 | } |
| 4325 | } |
drh | 3765df4 | 2006-06-28 18:18:09 +0000 | [diff] [blame] | 4326 | #else |
| 4327 | iCnt = db->activeVdbeCnt; |
danielk1977 | 212b218 | 2006-06-23 14:32:08 +0000 | [diff] [blame] | 4328 | #endif |
| 4329 | if( iCnt>1 ){ |
danielk1977 | e6efa74 | 2004-11-10 11:55:10 +0000 | [diff] [blame] | 4330 | rc = SQLITE_LOCKED; |
drh | 77658e2 | 2007-12-04 16:54:52 +0000 | [diff] [blame] | 4331 | p->errorAction = OE_Abort; |
danielk1977 | e6efa74 | 2004-11-10 11:55:10 +0000 | [diff] [blame] | 4332 | }else{ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 4333 | iDb = pOp->p3; |
danielk1977 | 212b218 | 2006-06-23 14:32:08 +0000 | [diff] [blame] | 4334 | assert( iCnt==1 ); |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4335 | assert( (p->btreeMask & (1<<iDb))!=0 ); |
| 4336 | rc = sqlite3BtreeDropTable(db->aDb[iDb].pBt, pOp->p1, &iMoved); |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 4337 | MemSetTypeFlag(pOut, MEM_Int); |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4338 | pOut->u.i = iMoved; |
drh | 3765df4 | 2006-06-28 18:18:09 +0000 | [diff] [blame] | 4339 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | e6efa74 | 2004-11-10 11:55:10 +0000 | [diff] [blame] | 4340 | if( rc==SQLITE_OK && iMoved!=0 ){ |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4341 | sqlite3RootPageMoved(&db->aDb[iDb], iMoved, pOp->p1); |
danielk1977 | e6efa74 | 2004-11-10 11:55:10 +0000 | [diff] [blame] | 4342 | } |
drh | 3765df4 | 2006-06-28 18:18:09 +0000 | [diff] [blame] | 4343 | #endif |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 4344 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4345 | break; |
| 4346 | } |
| 4347 | |
danielk1977 | c7af484 | 2008-10-27 13:59:33 +0000 | [diff] [blame] | 4348 | /* Opcode: Clear P1 P2 P3 |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 4349 | ** |
| 4350 | ** Delete all contents of the database table or index whose root page |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 4351 | ** in the database file is given by P1. But, unlike Destroy, do not |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 4352 | ** remove the table or index from the database file. |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 4353 | ** |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 4354 | ** The table being clear is in the main database file if P2==0. If |
| 4355 | ** P2==1 then the table to be clear is in the auxiliary database file |
| 4356 | ** that is used to store tables create using CREATE TEMPORARY TABLE. |
| 4357 | ** |
shane | be21779 | 2009-03-05 04:20:31 +0000 | [diff] [blame] | 4358 | ** 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] | 4359 | ** intkey table (an SQL table, not an index). In this case the row change |
| 4360 | ** count is incremented by the number of rows in the table being cleared. |
| 4361 | ** If P3 is greater than zero, then the value stored in register P3 is |
| 4362 | ** also incremented by the number of rows in the table being cleared. |
| 4363 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 4364 | ** See also: Destroy |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 4365 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4366 | case OP_Clear: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 4367 | int nChange; |
| 4368 | |
| 4369 | nChange = 0; |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 4370 | assert( (p->btreeMask & (1<<pOp->p2))!=0 ); |
danielk1977 | c7af484 | 2008-10-27 13:59:33 +0000 | [diff] [blame] | 4371 | rc = sqlite3BtreeClearTable( |
| 4372 | db->aDb[pOp->p2].pBt, pOp->p1, (pOp->p3 ? &nChange : 0) |
| 4373 | ); |
| 4374 | if( pOp->p3 ){ |
| 4375 | p->nChange += nChange; |
| 4376 | if( pOp->p3>0 ){ |
| 4377 | p->aMem[pOp->p3].u.i += nChange; |
| 4378 | } |
| 4379 | } |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 4380 | break; |
| 4381 | } |
| 4382 | |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 4383 | /* Opcode: CreateTable P1 P2 * * * |
drh | 5b2fd56 | 2001-09-13 15:21:31 +0000 | [diff] [blame] | 4384 | ** |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 4385 | ** Allocate a new table in the main database file if P1==0 or in the |
| 4386 | ** auxiliary database file if P1==1 or in an attached database if |
| 4387 | ** P1>1. Write the root page number of the new table into |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4388 | ** register P2 |
drh | 5b2fd56 | 2001-09-13 15:21:31 +0000 | [diff] [blame] | 4389 | ** |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 4390 | ** The difference between a table and an index is this: A table must |
| 4391 | ** have a 4-byte integer key and can have arbitrary data. An index |
| 4392 | ** has an arbitrary key but no data. |
| 4393 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 4394 | ** See also: CreateIndex |
drh | 5b2fd56 | 2001-09-13 15:21:31 +0000 | [diff] [blame] | 4395 | */ |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 4396 | /* Opcode: CreateIndex P1 P2 * * * |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 4397 | ** |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 4398 | ** Allocate a new index in the main database file if P1==0 or in the |
| 4399 | ** auxiliary database file if P1==1 or in an attached database if |
| 4400 | ** P1>1. Write the root page number of the new table into |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4401 | ** register P2. |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 4402 | ** |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 4403 | ** See documentation on OP_CreateTable for additional information. |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 4404 | */ |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 4405 | case OP_CreateIndex: /* out2-prerelease */ |
| 4406 | case OP_CreateTable: { /* out2-prerelease */ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 4407 | int pgno; |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 4408 | int flags; |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 4409 | Db *pDb; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 4410 | |
| 4411 | pgno = 0; |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 4412 | assert( pOp->p1>=0 && pOp->p1<db->nDb ); |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 4413 | assert( (p->btreeMask & (1<<pOp->p1))!=0 ); |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 4414 | pDb = &db->aDb[pOp->p1]; |
| 4415 | assert( pDb->pBt!=0 ); |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 4416 | if( pOp->opcode==OP_CreateTable ){ |
danielk1977 | 9407625 | 2004-05-14 12:16:11 +0000 | [diff] [blame] | 4417 | /* flags = BTREE_INTKEY; */ |
| 4418 | flags = BTREE_LEAFDATA|BTREE_INTKEY; |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 4419 | }else{ |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 4420 | flags = BTREE_ZERODATA; |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 4421 | } |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 4422 | rc = sqlite3BtreeCreateTable(pDb->pBt, &pgno, flags); |
drh | 88a003e | 2008-12-11 16:17:03 +0000 | [diff] [blame] | 4423 | pOut->u.i = pgno; |
| 4424 | MemSetTypeFlag(pOut, MEM_Int); |
drh | 5b2fd56 | 2001-09-13 15:21:31 +0000 | [diff] [blame] | 4425 | break; |
| 4426 | } |
| 4427 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4428 | /* Opcode: ParseSchema P1 P2 * P4 * |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 4429 | ** |
| 4430 | ** Read and parse all entries from the SQLITE_MASTER table of database P1 |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 4431 | ** that match the WHERE clause P4. P2 is the "force" flag. Always do |
drh | 3c23a88 | 2007-01-09 14:01:13 +0000 | [diff] [blame] | 4432 | ** the parsing if P2 is true. If P2 is false, then this routine is a |
| 4433 | ** no-op if the schema is not currently loaded. In other words, if P2 |
| 4434 | ** is false, the SQLITE_MASTER table is only parsed if the rest of the |
| 4435 | ** schema is already loaded into the symbol table. |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 4436 | ** |
| 4437 | ** This opcode invokes the parser to create a new virtual machine, |
shane | 21e7feb | 2008-05-30 15:59:49 +0000 | [diff] [blame] | 4438 | ** then runs the new virtual machine. It is thus a re-entrant opcode. |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 4439 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4440 | case OP_ParseSchema: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 4441 | int iDb; |
| 4442 | const char *zMaster; |
| 4443 | char *zSql; |
| 4444 | InitData initData; |
| 4445 | |
| 4446 | iDb = pOp->p1; |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 4447 | assert( iDb>=0 && iDb<db->nDb ); |
danielk1977 | a8bbef8 | 2009-03-23 17:11:26 +0000 | [diff] [blame] | 4448 | |
| 4449 | /* If pOp->p2 is 0, then this opcode is being executed to read a |
| 4450 | ** single row, for example the row corresponding to a new index |
| 4451 | ** created by this VDBE, from the sqlite_master table. It only |
| 4452 | ** does this if the corresponding in-memory schema is currently |
| 4453 | ** loaded. Otherwise, the new index definition can be loaded along |
| 4454 | ** with the rest of the schema when it is required. |
| 4455 | ** |
| 4456 | ** Although the mutex on the BtShared object that corresponds to |
| 4457 | ** database iDb (the database containing the sqlite_master table |
| 4458 | ** read by this instruction) is currently held, it is necessary to |
| 4459 | ** obtain the mutexes on all attached databases before checking if |
| 4460 | ** the schema of iDb is loaded. This is because, at the start of |
| 4461 | ** the sqlite3_exec() call below, SQLite will invoke |
| 4462 | ** sqlite3BtreeEnterAll(). If all mutexes are not already held, the |
| 4463 | ** iDb mutex may be temporarily released to avoid deadlock. If |
| 4464 | ** this happens, then some other thread may delete the in-memory |
| 4465 | ** schema of database iDb before the SQL statement runs. The schema |
| 4466 | ** will not be reloaded becuase the db->init.busy flag is set. This |
| 4467 | ** can result in a "no such table: sqlite_master" or "malformed |
| 4468 | ** database schema" error being returned to the user. |
| 4469 | */ |
| 4470 | assert( sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) ); |
| 4471 | sqlite3BtreeEnterAll(db); |
| 4472 | if( pOp->p2 || DbHasProperty(db, iDb, DB_SchemaLoaded) ){ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 4473 | zMaster = SCHEMA_TABLE(iDb); |
danielk1977 | a8bbef8 | 2009-03-23 17:11:26 +0000 | [diff] [blame] | 4474 | initData.db = db; |
| 4475 | initData.iDb = pOp->p1; |
| 4476 | initData.pzErrMsg = &p->zErrMsg; |
| 4477 | zSql = sqlite3MPrintf(db, |
| 4478 | "SELECT name, rootpage, sql FROM '%q'.%s WHERE %s", |
| 4479 | db->aDb[iDb].zName, zMaster, pOp->p4.z); |
| 4480 | if( zSql==0 ){ |
| 4481 | rc = SQLITE_NOMEM; |
| 4482 | }else{ |
| 4483 | (void)sqlite3SafetyOff(db); |
| 4484 | assert( db->init.busy==0 ); |
| 4485 | db->init.busy = 1; |
| 4486 | initData.rc = SQLITE_OK; |
| 4487 | assert( !db->mallocFailed ); |
| 4488 | rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0); |
| 4489 | if( rc==SQLITE_OK ) rc = initData.rc; |
| 4490 | sqlite3DbFree(db, zSql); |
| 4491 | db->init.busy = 0; |
| 4492 | (void)sqlite3SafetyOn(db); |
| 4493 | } |
drh | 3c23a88 | 2007-01-09 14:01:13 +0000 | [diff] [blame] | 4494 | } |
danielk1977 | a8bbef8 | 2009-03-23 17:11:26 +0000 | [diff] [blame] | 4495 | sqlite3BtreeLeaveAll(db); |
danielk1977 | 261919c | 2005-12-06 12:52:59 +0000 | [diff] [blame] | 4496 | if( rc==SQLITE_NOMEM ){ |
danielk1977 | 261919c | 2005-12-06 12:52:59 +0000 | [diff] [blame] | 4497 | goto no_mem; |
| 4498 | } |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 4499 | break; |
| 4500 | } |
| 4501 | |
drh | 8bfdf72 | 2009-06-19 14:06:03 +0000 | [diff] [blame] | 4502 | #if !defined(SQLITE_OMIT_ANALYZE) |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4503 | /* Opcode: LoadAnalysis P1 * * * * |
drh | 497e446 | 2005-07-23 03:18:40 +0000 | [diff] [blame] | 4504 | ** |
| 4505 | ** Read the sqlite_stat1 table for database P1 and load the content |
| 4506 | ** of that table into the internal index hash table. This will cause |
| 4507 | ** the analysis to be used when preparing all subsequent queries. |
| 4508 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4509 | case OP_LoadAnalysis: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 4510 | assert( pOp->p1>=0 && pOp->p1<db->nDb ); |
| 4511 | rc = sqlite3AnalysisLoad(db, pOp->p1); |
drh | 497e446 | 2005-07-23 03:18:40 +0000 | [diff] [blame] | 4512 | break; |
| 4513 | } |
drh | 8bfdf72 | 2009-06-19 14:06:03 +0000 | [diff] [blame] | 4514 | #endif /* !defined(SQLITE_OMIT_ANALYZE) */ |
drh | 497e446 | 2005-07-23 03:18:40 +0000 | [diff] [blame] | 4515 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4516 | /* Opcode: DropTable P1 * * P4 * |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 4517 | ** |
| 4518 | ** Remove the internal (in-memory) data structures that describe |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 4519 | ** the table named P4 in database P1. This is called after a table |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 4520 | ** is dropped in order to keep the internal representation of the |
| 4521 | ** schema consistent with what is on disk. |
| 4522 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4523 | case OP_DropTable: { |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 4524 | sqlite3UnlinkAndDeleteTable(db, pOp->p1, pOp->p4.z); |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 4525 | break; |
| 4526 | } |
| 4527 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4528 | /* Opcode: DropIndex P1 * * P4 * |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 4529 | ** |
| 4530 | ** Remove the internal (in-memory) data structures that describe |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 4531 | ** the index named P4 in database P1. This is called after an index |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 4532 | ** is dropped in order to keep the internal representation of the |
| 4533 | ** schema consistent with what is on disk. |
| 4534 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4535 | case OP_DropIndex: { |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 4536 | sqlite3UnlinkAndDeleteIndex(db, pOp->p1, pOp->p4.z); |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 4537 | break; |
| 4538 | } |
| 4539 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4540 | /* Opcode: DropTrigger P1 * * P4 * |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 4541 | ** |
| 4542 | ** Remove the internal (in-memory) data structures that describe |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 4543 | ** the trigger named P4 in database P1. This is called after a trigger |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 4544 | ** is dropped in order to keep the internal representation of the |
| 4545 | ** schema consistent with what is on disk. |
| 4546 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4547 | case OP_DropTrigger: { |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 4548 | sqlite3UnlinkAndDeleteTrigger(db, pOp->p1, pOp->p4.z); |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 4549 | break; |
| 4550 | } |
| 4551 | |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 4552 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 4553 | #ifndef SQLITE_OMIT_INTEGRITY_CHECK |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4554 | /* Opcode: IntegrityCk P1 P2 P3 * P5 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4555 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4556 | ** Do an analysis of the currently open database. Store in |
| 4557 | ** register P1 the text of an error message describing any problems. |
| 4558 | ** If no problems are found, store a NULL in register P1. |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 4559 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4560 | ** The register P3 contains the maximum number of allowed errors. |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 4561 | ** At most reg(P3) errors will be reported. |
| 4562 | ** In other words, the analysis stops as soon as reg(P1) errors are |
| 4563 | ** seen. Reg(P1) is updated with the number of errors remaining. |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 4564 | ** |
drh | 7906975 | 2004-05-22 21:30:40 +0000 | [diff] [blame] | 4565 | ** The root page numbers of all tables in the database are integer |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 4566 | ** 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] | 4567 | ** total. |
drh | 2150432 | 2002-06-25 13:16:02 +0000 | [diff] [blame] | 4568 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4569 | ** If P5 is not zero, the check is done on the auxiliary database |
drh | 2150432 | 2002-06-25 13:16:02 +0000 | [diff] [blame] | 4570 | ** file, not the main database file. |
drh | 1dd397f | 2002-02-03 03:34:07 +0000 | [diff] [blame] | 4571 | ** |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 4572 | ** This opcode is used to implement the integrity_check pragma. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4573 | */ |
drh | aaab572 | 2002-02-19 13:39:21 +0000 | [diff] [blame] | 4574 | case OP_IntegrityCk: { |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4575 | int nRoot; /* Number of tables to check. (Number of root pages.) */ |
| 4576 | int *aRoot; /* Array of rootpage numbers for tables to be checked */ |
| 4577 | int j; /* Loop counter */ |
| 4578 | int nErr; /* Number of errors reported */ |
| 4579 | char *z; /* Text of the error report */ |
| 4580 | Mem *pnErr; /* Register keeping track of errors remaining */ |
| 4581 | |
| 4582 | nRoot = pOp->p2; |
drh | 7906975 | 2004-05-22 21:30:40 +0000 | [diff] [blame] | 4583 | assert( nRoot>0 ); |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 4584 | aRoot = sqlite3DbMallocRaw(db, sizeof(int)*(nRoot+1) ); |
drh | caec2f1 | 2003-01-07 02:47:47 +0000 | [diff] [blame] | 4585 | if( aRoot==0 ) goto no_mem; |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4586 | assert( pOp->p3>0 && pOp->p3<=p->nMem ); |
| 4587 | pnErr = &p->aMem[pOp->p3]; |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 4588 | assert( (pnErr->flags & MEM_Int)!=0 ); |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4589 | assert( (pnErr->flags & (MEM_Str|MEM_Blob))==0 ); |
| 4590 | pIn1 = &p->aMem[pOp->p1]; |
drh | 7906975 | 2004-05-22 21:30:40 +0000 | [diff] [blame] | 4591 | for(j=0; j<nRoot; j++){ |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 4592 | aRoot[j] = (int)sqlite3VdbeIntValue(&pIn1[j]); |
drh | 1dd397f | 2002-02-03 03:34:07 +0000 | [diff] [blame] | 4593 | } |
| 4594 | aRoot[j] = 0; |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4595 | assert( pOp->p5<db->nDb ); |
| 4596 | assert( (p->btreeMask & (1<<pOp->p5))!=0 ); |
| 4597 | z = sqlite3BtreeIntegrityCheck(db->aDb[pOp->p5].pBt, aRoot, nRoot, |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 4598 | (int)pnErr->u.i, &nErr); |
drh | c890fec | 2008-08-01 20:10:08 +0000 | [diff] [blame] | 4599 | sqlite3DbFree(db, aRoot); |
drh | 3c024d6 | 2007-03-30 11:23:45 +0000 | [diff] [blame] | 4600 | pnErr->u.i -= nErr; |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 4601 | sqlite3VdbeMemSetNull(pIn1); |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 4602 | if( nErr==0 ){ |
| 4603 | assert( z==0 ); |
drh | c890fec | 2008-08-01 20:10:08 +0000 | [diff] [blame] | 4604 | }else if( z==0 ){ |
| 4605 | goto no_mem; |
drh | 1dd397f | 2002-02-03 03:34:07 +0000 | [diff] [blame] | 4606 | }else{ |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 4607 | sqlite3VdbeMemSetStr(pIn1, z, -1, SQLITE_UTF8, sqlite3_free); |
danielk1977 | 8a6b541 | 2004-05-24 07:04:25 +0000 | [diff] [blame] | 4608 | } |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 4609 | UPDATE_MAX_BLOBSIZE(pIn1); |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4610 | sqlite3VdbeChangeEncoding(pIn1, encoding); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4611 | break; |
| 4612 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 4613 | #endif /* SQLITE_OMIT_INTEGRITY_CHECK */ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4614 | |
drh | 3d4501e | 2008-12-04 20:40:10 +0000 | [diff] [blame] | 4615 | /* Opcode: RowSetAdd P1 P2 * * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4616 | ** |
drh | 3d4501e | 2008-12-04 20:40:10 +0000 | [diff] [blame] | 4617 | ** Insert the integer value held by register P2 into a boolean index |
| 4618 | ** held in register P1. |
| 4619 | ** |
| 4620 | ** An assertion fails if P2 is not an integer. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4621 | */ |
drh | 3d4501e | 2008-12-04 20:40:10 +0000 | [diff] [blame] | 4622 | case OP_RowSetAdd: { /* in2 */ |
| 4623 | Mem *pIdx; |
| 4624 | Mem *pVal; |
| 4625 | assert( pOp->p1>0 && pOp->p1<=p->nMem ); |
| 4626 | pIdx = &p->aMem[pOp->p1]; |
| 4627 | assert( pOp->p2>0 && pOp->p2<=p->nMem ); |
| 4628 | pVal = &p->aMem[pOp->p2]; |
| 4629 | assert( (pVal->flags & MEM_Int)!=0 ); |
| 4630 | if( (pIdx->flags & MEM_RowSet)==0 ){ |
| 4631 | sqlite3VdbeMemSetRowSet(pIdx); |
drh | 8d99363 | 2008-12-04 22:17:55 +0000 | [diff] [blame] | 4632 | if( (pIdx->flags & MEM_RowSet)==0 ) goto no_mem; |
drh | 3d4501e | 2008-12-04 20:40:10 +0000 | [diff] [blame] | 4633 | } |
| 4634 | sqlite3RowSetInsert(pIdx->u.pRowSet, pVal->u.i); |
| 4635 | break; |
| 4636 | } |
| 4637 | |
| 4638 | /* Opcode: RowSetRead P1 P2 P3 * * |
| 4639 | ** |
| 4640 | ** Extract the smallest value from boolean index P1 and put that value into |
| 4641 | ** register P3. Or, if boolean index P1 is initially empty, leave P3 |
| 4642 | ** unchanged and jump to instruction P2. |
| 4643 | */ |
| 4644 | case OP_RowSetRead: { /* jump, out3 */ |
| 4645 | Mem *pIdx; |
| 4646 | i64 val; |
| 4647 | assert( pOp->p1>0 && pOp->p1<=p->nMem ); |
| 4648 | CHECK_FOR_INTERRUPT; |
| 4649 | pIdx = &p->aMem[pOp->p1]; |
drh | dd5f5a6 | 2008-12-23 13:35:23 +0000 | [diff] [blame] | 4650 | pOut = &p->aMem[pOp->p3]; |
drh | 3d4501e | 2008-12-04 20:40:10 +0000 | [diff] [blame] | 4651 | if( (pIdx->flags & MEM_RowSet)==0 |
| 4652 | || sqlite3RowSetNext(pIdx->u.pRowSet, &val)==0 |
| 4653 | ){ |
| 4654 | /* The boolean index is empty */ |
| 4655 | sqlite3VdbeMemSetNull(pIdx); |
| 4656 | pc = pOp->p2 - 1; |
| 4657 | }else{ |
| 4658 | /* A value was pulled from the index */ |
| 4659 | assert( pOp->p3>0 && pOp->p3<=p->nMem ); |
drh | 3d4501e | 2008-12-04 20:40:10 +0000 | [diff] [blame] | 4660 | sqlite3VdbeMemSetInt64(pOut, val); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 4661 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4662 | break; |
| 4663 | } |
| 4664 | |
drh | 1b26c7c | 2009-04-22 02:15:47 +0000 | [diff] [blame] | 4665 | /* Opcode: RowSetTest P1 P2 P3 P4 |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 4666 | ** |
drh | ade9760 | 2009-04-21 15:05:18 +0000 | [diff] [blame] | 4667 | ** 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] | 4668 | ** contains a RowSet object and that RowSet object contains |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 4669 | ** the value held in P3, jump to register P2. Otherwise, insert the |
drh | 1b26c7c | 2009-04-22 02:15:47 +0000 | [diff] [blame] | 4670 | ** integer in P3 into the RowSet and continue on to the |
drh | ade9760 | 2009-04-21 15:05:18 +0000 | [diff] [blame] | 4671 | ** next opcode. |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 4672 | ** |
drh | 1b26c7c | 2009-04-22 02:15:47 +0000 | [diff] [blame] | 4673 | ** The RowSet object is optimized for the case where successive sets |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 4674 | ** of integers, where each set contains no duplicates. Each set |
| 4675 | ** of values is identified by a unique P4 value. The first set |
drh | 1b26c7c | 2009-04-22 02:15:47 +0000 | [diff] [blame] | 4676 | ** must have P4==0, the final set P4=-1. P4 must be either -1 or |
| 4677 | ** non-negative. For non-negative values of P4 only the lower 4 |
| 4678 | ** bits are significant. |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 4679 | ** |
| 4680 | ** This allows optimizations: (a) when P4==0 there is no need to test |
drh | 1b26c7c | 2009-04-22 02:15:47 +0000 | [diff] [blame] | 4681 | ** the rowset object for P3, as it is guaranteed not to contain it, |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 4682 | ** (b) when P4==-1 there is no need to insert the value, as it will |
| 4683 | ** never be tested for, and (c) when a value that is part of set X is |
| 4684 | ** inserted, there is no need to search to see if the same value was |
| 4685 | ** previously inserted as part of set X (only if it was previously |
| 4686 | ** inserted as part of some other set). |
| 4687 | */ |
drh | 1b26c7c | 2009-04-22 02:15:47 +0000 | [diff] [blame] | 4688 | case OP_RowSetTest: { /* jump, in1, in3 */ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 4689 | int iSet; |
| 4690 | int exists; |
| 4691 | |
| 4692 | iSet = pOp->p4.i; |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 4693 | assert( pIn3->flags&MEM_Int ); |
| 4694 | |
drh | 1b26c7c | 2009-04-22 02:15:47 +0000 | [diff] [blame] | 4695 | /* If there is anything other than a rowset object in memory cell P1, |
| 4696 | ** delete it now and initialize P1 with an empty rowset |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 4697 | */ |
drh | 733bf1b | 2009-04-22 00:47:00 +0000 | [diff] [blame] | 4698 | if( (pIn1->flags & MEM_RowSet)==0 ){ |
| 4699 | sqlite3VdbeMemSetRowSet(pIn1); |
| 4700 | if( (pIn1->flags & MEM_RowSet)==0 ) goto no_mem; |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 4701 | } |
| 4702 | |
| 4703 | assert( pOp->p4type==P4_INT32 ); |
drh | 1b26c7c | 2009-04-22 02:15:47 +0000 | [diff] [blame] | 4704 | assert( iSet==-1 || iSet>=0 ); |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 4705 | if( iSet ){ |
shane | 60a4b53 | 2009-05-06 18:57:09 +0000 | [diff] [blame] | 4706 | exists = sqlite3RowSetTest(pIn1->u.pRowSet, |
| 4707 | (u8)(iSet>=0 ? iSet & 0xf : 0xff), |
drh | 733bf1b | 2009-04-22 00:47:00 +0000 | [diff] [blame] | 4708 | pIn3->u.i); |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 4709 | if( exists ){ |
| 4710 | pc = pOp->p2 - 1; |
| 4711 | break; |
| 4712 | } |
| 4713 | } |
| 4714 | if( iSet>=0 ){ |
drh | 733bf1b | 2009-04-22 00:47:00 +0000 | [diff] [blame] | 4715 | sqlite3RowSetInsert(pIn1->u.pRowSet, pIn3->u.i); |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 4716 | } |
| 4717 | break; |
| 4718 | } |
| 4719 | |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4720 | |
danielk1977 | 93758c8 | 2005-01-21 08:13:14 +0000 | [diff] [blame] | 4721 | #ifndef SQLITE_OMIT_TRIGGER |
rdc | b0c374f | 2004-02-20 22:53:38 +0000 | [diff] [blame] | 4722 | /* Opcode: ContextPush * * * |
| 4723 | ** |
| 4724 | ** Save the current Vdbe context such that it can be restored by a ContextPop |
| 4725 | ** opcode. The context stores the last insert row id, the last statement change |
| 4726 | ** count, and the current statement change count. |
| 4727 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4728 | case OP_ContextPush: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 4729 | int i; |
drh | 344737f | 2004-09-19 00:50:20 +0000 | [diff] [blame] | 4730 | Context *pContext; |
danielk1977 | b28af71 | 2004-06-21 06:50:26 +0000 | [diff] [blame] | 4731 | |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 4732 | i = p->contextStackTop++; |
drh | 344737f | 2004-09-19 00:50:20 +0000 | [diff] [blame] | 4733 | assert( i>=0 ); |
danielk1977 | b28af71 | 2004-06-21 06:50:26 +0000 | [diff] [blame] | 4734 | /* FIX ME: This should be allocated as part of the vdbe at compile-time */ |
drh | 344737f | 2004-09-19 00:50:20 +0000 | [diff] [blame] | 4735 | if( i>=p->contextStackDepth ){ |
| 4736 | p->contextStackDepth = i+1; |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 4737 | p->contextStack = sqlite3DbReallocOrFree(db, p->contextStack, |
drh | cf64372 | 2007-03-27 13:36:37 +0000 | [diff] [blame] | 4738 | sizeof(Context)*(i+1)); |
drh | 344737f | 2004-09-19 00:50:20 +0000 | [diff] [blame] | 4739 | if( p->contextStack==0 ) goto no_mem; |
| 4740 | } |
| 4741 | pContext = &p->contextStack[i]; |
| 4742 | pContext->lastRowid = db->lastRowid; |
| 4743 | pContext->nChange = p->nChange; |
rdc | b0c374f | 2004-02-20 22:53:38 +0000 | [diff] [blame] | 4744 | break; |
| 4745 | } |
| 4746 | |
| 4747 | /* Opcode: ContextPop * * * |
| 4748 | ** |
| 4749 | ** Restore the Vdbe context to the state it was in when contextPush was last |
| 4750 | ** executed. The context stores the last insert row id, the last statement |
| 4751 | ** change count, and the current statement change count. |
| 4752 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4753 | case OP_ContextPop: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 4754 | Context *pContext; |
| 4755 | pContext = &p->contextStack[--p->contextStackTop]; |
drh | 344737f | 2004-09-19 00:50:20 +0000 | [diff] [blame] | 4756 | assert( p->contextStackTop>=0 ); |
| 4757 | db->lastRowid = pContext->lastRowid; |
| 4758 | p->nChange = pContext->nChange; |
rdc | b0c374f | 2004-02-20 22:53:38 +0000 | [diff] [blame] | 4759 | break; |
| 4760 | } |
danielk1977 | 93758c8 | 2005-01-21 08:13:14 +0000 | [diff] [blame] | 4761 | #endif /* #ifndef SQLITE_OMIT_TRIGGER */ |
rdc | b0c374f | 2004-02-20 22:53:38 +0000 | [diff] [blame] | 4762 | |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 4763 | #ifndef SQLITE_OMIT_AUTOINCREMENT |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4764 | /* Opcode: MemMax P1 P2 * * * |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 4765 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4766 | ** Set the value of register P1 to the maximum of its current value |
| 4767 | ** and the value in register P2. |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 4768 | ** |
| 4769 | ** This instruction throws an error if the memory cell is not initially |
| 4770 | ** an integer. |
| 4771 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4772 | case OP_MemMax: { /* in1, in2 */ |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4773 | sqlite3VdbeMemIntegerify(pIn1); |
| 4774 | sqlite3VdbeMemIntegerify(pIn2); |
| 4775 | if( pIn1->u.i<pIn2->u.i){ |
| 4776 | pIn1->u.i = pIn2->u.i; |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 4777 | } |
| 4778 | break; |
| 4779 | } |
| 4780 | #endif /* SQLITE_OMIT_AUTOINCREMENT */ |
| 4781 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4782 | /* Opcode: IfPos P1 P2 * * * |
danielk1977 | a2dc3b1 | 2005-02-05 12:48:48 +0000 | [diff] [blame] | 4783 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4784 | ** If the value of register P1 is 1 or greater, jump to P2. |
drh | 6f58f70 | 2006-01-08 05:26:41 +0000 | [diff] [blame] | 4785 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4786 | ** It is illegal to use this instruction on a register that does |
drh | 6f58f70 | 2006-01-08 05:26:41 +0000 | [diff] [blame] | 4787 | ** not contain an integer. An assertion fault will result if you try. |
danielk1977 | a2dc3b1 | 2005-02-05 12:48:48 +0000 | [diff] [blame] | 4788 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4789 | case OP_IfPos: { /* jump, in1 */ |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 4790 | assert( pIn1->flags&MEM_Int ); |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 4791 | if( pIn1->u.i>0 ){ |
drh | ec7429a | 2005-10-06 16:53:14 +0000 | [diff] [blame] | 4792 | pc = pOp->p2 - 1; |
| 4793 | } |
| 4794 | break; |
| 4795 | } |
| 4796 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4797 | /* Opcode: IfNeg P1 P2 * * * |
drh | 15007a9 | 2006-01-08 18:10:17 +0000 | [diff] [blame] | 4798 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4799 | ** If the value of register P1 is less than zero, jump to P2. |
drh | 15007a9 | 2006-01-08 18:10:17 +0000 | [diff] [blame] | 4800 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4801 | ** It is illegal to use this instruction on a register that does |
drh | 15007a9 | 2006-01-08 18:10:17 +0000 | [diff] [blame] | 4802 | ** not contain an integer. An assertion fault will result if you try. |
| 4803 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4804 | case OP_IfNeg: { /* jump, in1 */ |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 4805 | assert( pIn1->flags&MEM_Int ); |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 4806 | if( pIn1->u.i<0 ){ |
drh | 15007a9 | 2006-01-08 18:10:17 +0000 | [diff] [blame] | 4807 | pc = pOp->p2 - 1; |
| 4808 | } |
| 4809 | break; |
| 4810 | } |
| 4811 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4812 | /* Opcode: IfZero P1 P2 * * * |
drh | ec7429a | 2005-10-06 16:53:14 +0000 | [diff] [blame] | 4813 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4814 | ** If the value of register P1 is exactly 0, jump to P2. |
drh | 6f58f70 | 2006-01-08 05:26:41 +0000 | [diff] [blame] | 4815 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4816 | ** It is illegal to use this instruction on a register that does |
drh | 6f58f70 | 2006-01-08 05:26:41 +0000 | [diff] [blame] | 4817 | ** not contain an integer. An assertion fault will result if you try. |
drh | ec7429a | 2005-10-06 16:53:14 +0000 | [diff] [blame] | 4818 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4819 | case OP_IfZero: { /* jump, in1 */ |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 4820 | assert( pIn1->flags&MEM_Int ); |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 4821 | if( pIn1->u.i==0 ){ |
drh | a2a49dc | 2008-01-02 14:28:13 +0000 | [diff] [blame] | 4822 | pc = pOp->p2 - 1; |
| 4823 | } |
| 4824 | break; |
| 4825 | } |
| 4826 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4827 | /* Opcode: AggStep * P2 P3 P4 P5 |
drh | e509535 | 2002-02-24 03:25:14 +0000 | [diff] [blame] | 4828 | ** |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 4829 | ** Execute the step function for an aggregate. The |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4830 | ** function has P5 arguments. P4 is a pointer to the FuncDef |
| 4831 | ** structure that specifies the function. Use register |
| 4832 | ** P3 as the accumulator. |
drh | e509535 | 2002-02-24 03:25:14 +0000 | [diff] [blame] | 4833 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4834 | ** The P5 arguments are taken from register P2 and its |
| 4835 | ** successors. |
drh | e509535 | 2002-02-24 03:25:14 +0000 | [diff] [blame] | 4836 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4837 | case OP_AggStep: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 4838 | int n; |
drh | e509535 | 2002-02-24 03:25:14 +0000 | [diff] [blame] | 4839 | int i; |
drh | c54a617 | 2009-06-02 16:06:03 +0000 | [diff] [blame] | 4840 | Mem *pMem; |
| 4841 | Mem *pRec; |
danielk1977 | 22322fd | 2004-05-25 23:35:17 +0000 | [diff] [blame] | 4842 | sqlite3_context ctx; |
danielk1977 | 6ddcca5 | 2004-05-24 23:48:25 +0000 | [diff] [blame] | 4843 | sqlite3_value **apVal; |
drh | e509535 | 2002-02-24 03:25:14 +0000 | [diff] [blame] | 4844 | |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 4845 | n = pOp->p5; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 4846 | assert( n>=0 ); |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4847 | pRec = &p->aMem[pOp->p2]; |
danielk1977 | 6ddcca5 | 2004-05-24 23:48:25 +0000 | [diff] [blame] | 4848 | apVal = p->apArg; |
| 4849 | assert( apVal || n==0 ); |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 4850 | for(i=0; i<n; i++, pRec++){ |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 4851 | apVal[i] = pRec; |
drh | 8079a0d | 2006-01-12 17:20:50 +0000 | [diff] [blame] | 4852 | storeTypeInfo(pRec, encoding); |
drh | e509535 | 2002-02-24 03:25:14 +0000 | [diff] [blame] | 4853 | } |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 4854 | ctx.pFunc = pOp->p4.pFunc; |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4855 | assert( pOp->p3>0 && pOp->p3<=p->nMem ); |
| 4856 | ctx.pMem = pMem = &p->aMem[pOp->p3]; |
drh | abfcea2 | 2005-09-06 20:36:48 +0000 | [diff] [blame] | 4857 | pMem->n++; |
drh | 90669c1 | 2006-01-20 15:45:36 +0000 | [diff] [blame] | 4858 | ctx.s.flags = MEM_Null; |
| 4859 | ctx.s.z = 0; |
danielk1977 | 5f09613 | 2008-03-28 15:44:09 +0000 | [diff] [blame] | 4860 | ctx.s.zMalloc = 0; |
drh | 90669c1 | 2006-01-20 15:45:36 +0000 | [diff] [blame] | 4861 | ctx.s.xDel = 0; |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 4862 | ctx.s.db = db; |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 4863 | ctx.isError = 0; |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 4864 | ctx.pColl = 0; |
drh | e82f5d0 | 2008-10-07 19:53:14 +0000 | [diff] [blame] | 4865 | if( ctx.pFunc->flags & SQLITE_FUNC_NEEDCOLL ){ |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 4866 | assert( pOp>p->aOp ); |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 4867 | assert( pOp[-1].p4type==P4_COLLSEQ ); |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 4868 | assert( pOp[-1].opcode==OP_CollSeq ); |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 4869 | ctx.pColl = pOp[-1].p4.pColl; |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 4870 | } |
danielk1977 | 6ddcca5 | 2004-05-24 23:48:25 +0000 | [diff] [blame] | 4871 | (ctx.pFunc->xStep)(&ctx, n, apVal); |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 4872 | if( ctx.isError ){ |
drh | f089aa4 | 2008-07-08 19:34:06 +0000 | [diff] [blame] | 4873 | sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(&ctx.s)); |
drh | 69544ec | 2008-02-06 14:11:34 +0000 | [diff] [blame] | 4874 | rc = ctx.isError; |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 4875 | } |
drh | 90669c1 | 2006-01-20 15:45:36 +0000 | [diff] [blame] | 4876 | sqlite3VdbeMemRelease(&ctx.s); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4877 | break; |
| 4878 | } |
| 4879 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4880 | /* Opcode: AggFinal P1 P2 * P4 * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4881 | ** |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 4882 | ** Execute the finalizer function for an aggregate. P1 is |
| 4883 | ** the memory location that is the accumulator for the aggregate. |
drh | a10a34b | 2005-09-07 22:09:48 +0000 | [diff] [blame] | 4884 | ** |
| 4885 | ** P2 is the number of arguments that the step function takes and |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 4886 | ** P4 is a pointer to the FuncDef for this function. The P2 |
drh | a10a34b | 2005-09-07 22:09:48 +0000 | [diff] [blame] | 4887 | ** argument is not used by this opcode. It is only there to disambiguate |
| 4888 | ** functions that can take varying numbers of arguments. The |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 4889 | ** P4 argument is only needed for the degenerate case where |
drh | a10a34b | 2005-09-07 22:09:48 +0000 | [diff] [blame] | 4890 | ** the step function was not previously called. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4891 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4892 | case OP_AggFinal: { |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 4893 | Mem *pMem; |
drh | 0a07c10 | 2008-01-03 18:03:08 +0000 | [diff] [blame] | 4894 | assert( pOp->p1>0 && pOp->p1<=p->nMem ); |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 4895 | pMem = &p->aMem[pOp->p1]; |
drh | a10a34b | 2005-09-07 22:09:48 +0000 | [diff] [blame] | 4896 | assert( (pMem->flags & ~(MEM_Null|MEM_Agg))==0 ); |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 4897 | rc = sqlite3VdbeMemFinalize(pMem, pOp->p4.pFunc); |
drh | 90669c1 | 2006-01-20 15:45:36 +0000 | [diff] [blame] | 4898 | if( rc==SQLITE_ERROR ){ |
drh | f089aa4 | 2008-07-08 19:34:06 +0000 | [diff] [blame] | 4899 | sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(pMem)); |
drh | 90669c1 | 2006-01-20 15:45:36 +0000 | [diff] [blame] | 4900 | } |
drh | 2dca868 | 2008-03-21 17:13:13 +0000 | [diff] [blame] | 4901 | sqlite3VdbeChangeEncoding(pMem, encoding); |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 4902 | UPDATE_MAX_BLOBSIZE(pMem); |
drh | 023ae03 | 2007-05-08 12:12:16 +0000 | [diff] [blame] | 4903 | if( sqlite3VdbeMemTooBig(pMem) ){ |
| 4904 | goto too_big; |
| 4905 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4906 | break; |
| 4907 | } |
| 4908 | |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4909 | |
drh | fdbcdee | 2007-03-27 14:44:50 +0000 | [diff] [blame] | 4910 | #if !defined(SQLITE_OMIT_VACUUM) && !defined(SQLITE_OMIT_ATTACH) |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4911 | /* Opcode: Vacuum * * * * * |
drh | 6f8c91c | 2003-12-07 00:24:35 +0000 | [diff] [blame] | 4912 | ** |
| 4913 | ** Vacuum the entire database. This opcode will cause other virtual |
| 4914 | ** machines to be created and run. It may not be called from within |
| 4915 | ** a transaction. |
| 4916 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4917 | case OP_Vacuum: { |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 4918 | if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse; |
| 4919 | rc = sqlite3RunVacuum(&p->zErrMsg, db); |
| 4920 | if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse; |
drh | 6f8c91c | 2003-12-07 00:24:35 +0000 | [diff] [blame] | 4921 | break; |
| 4922 | } |
drh | 154d4b2 | 2006-09-21 11:02:16 +0000 | [diff] [blame] | 4923 | #endif |
drh | 6f8c91c | 2003-12-07 00:24:35 +0000 | [diff] [blame] | 4924 | |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 4925 | #if !defined(SQLITE_OMIT_AUTOVACUUM) |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4926 | /* Opcode: IncrVacuum P1 P2 * * * |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 4927 | ** |
| 4928 | ** Perform a single step of the incremental vacuum procedure on |
drh | ca5557f | 2007-05-04 18:30:40 +0000 | [diff] [blame] | 4929 | ** the P1 database. If the vacuum has finished, jump to instruction |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 4930 | ** P2. Otherwise, fall through to the next instruction. |
| 4931 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4932 | case OP_IncrVacuum: { /* jump */ |
drh | ca5557f | 2007-05-04 18:30:40 +0000 | [diff] [blame] | 4933 | Btree *pBt; |
| 4934 | |
| 4935 | assert( pOp->p1>=0 && pOp->p1<db->nDb ); |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 4936 | assert( (p->btreeMask & (1<<pOp->p1))!=0 ); |
drh | ca5557f | 2007-05-04 18:30:40 +0000 | [diff] [blame] | 4937 | pBt = db->aDb[pOp->p1].pBt; |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 4938 | rc = sqlite3BtreeIncrVacuum(pBt); |
| 4939 | if( rc==SQLITE_DONE ){ |
| 4940 | pc = pOp->p2 - 1; |
| 4941 | rc = SQLITE_OK; |
| 4942 | } |
| 4943 | break; |
| 4944 | } |
| 4945 | #endif |
| 4946 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4947 | /* Opcode: Expire P1 * * * * |
danielk1977 | a21c6b6 | 2005-01-24 10:25:59 +0000 | [diff] [blame] | 4948 | ** |
| 4949 | ** Cause precompiled statements to become expired. An expired statement |
| 4950 | ** fails with an error code of SQLITE_SCHEMA if it is ever executed |
| 4951 | ** (via sqlite3_step()). |
| 4952 | ** |
| 4953 | ** If P1 is 0, then all SQL statements become expired. If P1 is non-zero, |
| 4954 | ** then only the currently executing statement is affected. |
| 4955 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4956 | case OP_Expire: { |
danielk1977 | a21c6b6 | 2005-01-24 10:25:59 +0000 | [diff] [blame] | 4957 | if( !pOp->p1 ){ |
| 4958 | sqlite3ExpirePreparedStatements(db); |
| 4959 | }else{ |
| 4960 | p->expired = 1; |
| 4961 | } |
| 4962 | break; |
| 4963 | } |
| 4964 | |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 4965 | #ifndef SQLITE_OMIT_SHARED_CACHE |
drh | 6a9ad3d | 2008-04-02 16:29:30 +0000 | [diff] [blame] | 4966 | /* Opcode: TableLock P1 P2 P3 P4 * |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 4967 | ** |
| 4968 | ** Obtain a lock on a particular table. This instruction is only used when |
| 4969 | ** the shared-cache feature is enabled. |
| 4970 | ** |
drh | 6a9ad3d | 2008-04-02 16:29:30 +0000 | [diff] [blame] | 4971 | ** If P1 is the index of the database in sqlite3.aDb[] of the database |
| 4972 | ** on which the lock is acquired. A readlock is obtained if P3==0 or |
| 4973 | ** a write lock if P3==1. |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 4974 | ** |
| 4975 | ** P2 contains the root-page of the table to lock. |
| 4976 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 4977 | ** 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] | 4978 | ** used to generate an error message if the lock cannot be obtained. |
| 4979 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4980 | case OP_TableLock: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 4981 | int p1; |
| 4982 | u8 isWriteLock; |
| 4983 | |
| 4984 | p1 = pOp->p1; |
| 4985 | isWriteLock = (u8)pOp->p3; |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 4986 | assert( p1>=0 && p1<db->nDb ); |
| 4987 | assert( (p->btreeMask & (1<<p1))!=0 ); |
drh | 6a9ad3d | 2008-04-02 16:29:30 +0000 | [diff] [blame] | 4988 | assert( isWriteLock==0 || isWriteLock==1 ); |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 4989 | rc = sqlite3BtreeLockTable(db->aDb[p1].pBt, pOp->p2, isWriteLock); |
danielk1977 | 404ca07 | 2009-03-16 13:19:36 +0000 | [diff] [blame] | 4990 | if( (rc&0xFF)==SQLITE_LOCKED ){ |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 4991 | const char *z = pOp->p4.z; |
drh | f089aa4 | 2008-07-08 19:34:06 +0000 | [diff] [blame] | 4992 | sqlite3SetString(&p->zErrMsg, db, "database table is locked: %s", z); |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 4993 | } |
| 4994 | break; |
| 4995 | } |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 4996 | #endif /* SQLITE_OMIT_SHARED_CACHE */ |
| 4997 | |
| 4998 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4999 | /* Opcode: VBegin * * * P4 * |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 5000 | ** |
danielk1977 | 3e3a84d | 2008-08-01 17:37:40 +0000 | [diff] [blame] | 5001 | ** P4 may be a pointer to an sqlite3_vtab structure. If so, call the |
| 5002 | ** xBegin method for that table. |
| 5003 | ** |
| 5004 | ** 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] | 5005 | ** within a callback to a virtual table xSync() method. If it is, the error |
| 5006 | ** code will be set to SQLITE_LOCKED. |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 5007 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 5008 | case OP_VBegin: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 5009 | sqlite3_vtab *pVtab; |
| 5010 | pVtab = pOp->p4.pVtab; |
danielk1977 | 3e3a84d | 2008-08-01 17:37:40 +0000 | [diff] [blame] | 5011 | rc = sqlite3VtabBegin(db, pVtab); |
| 5012 | if( pVtab ){ |
| 5013 | sqlite3DbFree(db, p->zErrMsg); |
| 5014 | p->zErrMsg = pVtab->zErrMsg; |
| 5015 | pVtab->zErrMsg = 0; |
| 5016 | } |
danielk1977 | f9e7dda | 2006-06-16 16:08:53 +0000 | [diff] [blame] | 5017 | break; |
| 5018 | } |
| 5019 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 5020 | |
| 5021 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 5022 | /* Opcode: VCreate P1 * * P4 * |
danielk1977 | f9e7dda | 2006-06-16 16:08:53 +0000 | [diff] [blame] | 5023 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 5024 | ** 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] | 5025 | ** for that table. |
| 5026 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 5027 | case OP_VCreate: { |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 5028 | rc = sqlite3VtabCallCreate(db, pOp->p1, pOp->p4.z, &p->zErrMsg); |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 5029 | break; |
| 5030 | } |
| 5031 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 5032 | |
| 5033 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 5034 | /* Opcode: VDestroy P1 * * P4 * |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 5035 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 5036 | ** 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] | 5037 | ** of that table. |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 5038 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 5039 | case OP_VDestroy: { |
danielk1977 | 212b218 | 2006-06-23 14:32:08 +0000 | [diff] [blame] | 5040 | p->inVtabMethod = 2; |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 5041 | rc = sqlite3VtabCallDestroy(db, pOp->p1, pOp->p4.z); |
danielk1977 | 212b218 | 2006-06-23 14:32:08 +0000 | [diff] [blame] | 5042 | p->inVtabMethod = 0; |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 5043 | break; |
| 5044 | } |
| 5045 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 5046 | |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 5047 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 5048 | /* Opcode: VOpen P1 * * P4 * |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 5049 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 5050 | ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure. |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 5051 | ** P1 is a cursor number. This opcode opens a cursor to the virtual |
| 5052 | ** table and stores that cursor in P1. |
| 5053 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 5054 | case OP_VOpen: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 5055 | VdbeCursor *pCur; |
| 5056 | sqlite3_vtab_cursor *pVtabCursor; |
| 5057 | sqlite3_vtab *pVtab; |
| 5058 | sqlite3_module *pModule; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 5059 | |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 5060 | pCur = 0; |
| 5061 | pVtabCursor = 0; |
| 5062 | pVtab = pOp->p4.pVtab; |
| 5063 | pModule = (sqlite3_module *)pVtab->pModule; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 5064 | assert(pVtab && pModule); |
| 5065 | if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse; |
| 5066 | rc = pModule->xOpen(pVtab, &pVtabCursor); |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 5067 | sqlite3DbFree(db, p->zErrMsg); |
drh | 80cc85b | 2008-07-23 21:07:25 +0000 | [diff] [blame] | 5068 | p->zErrMsg = pVtab->zErrMsg; |
| 5069 | pVtab->zErrMsg = 0; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 5070 | if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse; |
| 5071 | if( SQLITE_OK==rc ){ |
shane | 21e7feb | 2008-05-30 15:59:49 +0000 | [diff] [blame] | 5072 | /* Initialize sqlite3_vtab_cursor base class */ |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 5073 | pVtabCursor->pVtab = pVtab; |
| 5074 | |
| 5075 | /* Initialise vdbe cursor object */ |
danielk1977 | d336e22 | 2009-02-20 10:58:41 +0000 | [diff] [blame] | 5076 | pCur = allocateCursor(p, pOp->p1, 0, -1, 0); |
danielk1977 | be71889 | 2006-06-23 08:05:19 +0000 | [diff] [blame] | 5077 | if( pCur ){ |
| 5078 | pCur->pVtabCursor = pVtabCursor; |
| 5079 | pCur->pModule = pVtabCursor->pVtab->pModule; |
danielk1977 | b7a2f2e | 2006-06-23 11:34:54 +0000 | [diff] [blame] | 5080 | }else{ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 5081 | db->mallocFailed = 1; |
danielk1977 | b7a2f2e | 2006-06-23 11:34:54 +0000 | [diff] [blame] | 5082 | pModule->xClose(pVtabCursor); |
danielk1977 | be71889 | 2006-06-23 08:05:19 +0000 | [diff] [blame] | 5083 | } |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 5084 | } |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 5085 | break; |
| 5086 | } |
| 5087 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 5088 | |
| 5089 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
danielk1977 | 6dbee81 | 2008-01-03 18:39:41 +0000 | [diff] [blame] | 5090 | /* Opcode: VFilter P1 P2 P3 P4 * |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 5091 | ** |
| 5092 | ** P1 is a cursor opened using VOpen. P2 is an address to jump to if |
| 5093 | ** the filtered result set is empty. |
| 5094 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 5095 | ** P4 is either NULL or a string that was generated by the xBestIndex |
| 5096 | ** method of the module. The interpretation of the P4 string is left |
drh | 4be8b51 | 2006-06-13 23:51:34 +0000 | [diff] [blame] | 5097 | ** to the module implementation. |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 5098 | ** |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 5099 | ** This opcode invokes the xFilter method on the virtual table specified |
danielk1977 | 6dbee81 | 2008-01-03 18:39:41 +0000 | [diff] [blame] | 5100 | ** by P1. The integer query plan parameter to xFilter is stored in register |
| 5101 | ** P3. Register P3+1 stores the argc parameter to be passed to the |
drh | 174edc6 | 2008-05-29 05:23:41 +0000 | [diff] [blame] | 5102 | ** xFilter method. Registers P3+2..P3+1+argc are the argc |
| 5103 | ** additional parameters which are passed to |
danielk1977 | 6dbee81 | 2008-01-03 18:39:41 +0000 | [diff] [blame] | 5104 | ** xFilter as argv. Register P3+2 becomes argv[0] when passed to xFilter. |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 5105 | ** |
danielk1977 | 6dbee81 | 2008-01-03 18:39:41 +0000 | [diff] [blame] | 5106 | ** 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] | 5107 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 5108 | case OP_VFilter: { /* jump */ |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 5109 | int nArg; |
danielk1977 | 6dbee81 | 2008-01-03 18:39:41 +0000 | [diff] [blame] | 5110 | int iQuery; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 5111 | const sqlite3_module *pModule; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 5112 | Mem *pQuery; |
| 5113 | Mem *pArgc; |
drh | 4dc754d | 2008-07-23 18:17:32 +0000 | [diff] [blame] | 5114 | sqlite3_vtab_cursor *pVtabCursor; |
| 5115 | sqlite3_vtab *pVtab; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 5116 | VdbeCursor *pCur; |
| 5117 | int res; |
| 5118 | int i; |
| 5119 | Mem **apArg; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 5120 | |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 5121 | pQuery = &p->aMem[pOp->p3]; |
| 5122 | pArgc = &pQuery[1]; |
| 5123 | pCur = p->apCsr[pOp->p1]; |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 5124 | REGISTER_TRACE(pOp->p3, pQuery); |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 5125 | assert( pCur->pVtabCursor ); |
drh | 4dc754d | 2008-07-23 18:17:32 +0000 | [diff] [blame] | 5126 | pVtabCursor = pCur->pVtabCursor; |
| 5127 | pVtab = pVtabCursor->pVtab; |
| 5128 | pModule = pVtab->pModule; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 5129 | |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 5130 | /* Grab the index number and argc parameters */ |
danielk1977 | 6dbee81 | 2008-01-03 18:39:41 +0000 | [diff] [blame] | 5131 | assert( (pQuery->flags&MEM_Int)!=0 && pArgc->flags==MEM_Int ); |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 5132 | nArg = (int)pArgc->u.i; |
| 5133 | iQuery = (int)pQuery->u.i; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 5134 | |
drh | 644a529 | 2006-12-20 14:53:38 +0000 | [diff] [blame] | 5135 | /* Invoke the xFilter method */ |
| 5136 | { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 5137 | res = 0; |
| 5138 | apArg = p->apArg; |
drh | 4be8b51 | 2006-06-13 23:51:34 +0000 | [diff] [blame] | 5139 | for(i = 0; i<nArg; i++){ |
danielk1977 | 6dbee81 | 2008-01-03 18:39:41 +0000 | [diff] [blame] | 5140 | apArg[i] = &pArgc[i+1]; |
drh | 4be8b51 | 2006-06-13 23:51:34 +0000 | [diff] [blame] | 5141 | storeTypeInfo(apArg[i], 0); |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 5142 | } |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 5143 | |
| 5144 | if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse; |
danielk1977 | 5a114ca | 2008-08-02 15:10:08 +0000 | [diff] [blame] | 5145 | sqlite3VtabLock(pVtab); |
danielk1977 | be71889 | 2006-06-23 08:05:19 +0000 | [diff] [blame] | 5146 | p->inVtabMethod = 1; |
drh | 4dc754d | 2008-07-23 18:17:32 +0000 | [diff] [blame] | 5147 | rc = pModule->xFilter(pVtabCursor, iQuery, pOp->p4.z, nArg, apArg); |
danielk1977 | be71889 | 2006-06-23 08:05:19 +0000 | [diff] [blame] | 5148 | p->inVtabMethod = 0; |
danielk1977 | 3e3a84d | 2008-08-01 17:37:40 +0000 | [diff] [blame] | 5149 | sqlite3DbFree(db, p->zErrMsg); |
| 5150 | p->zErrMsg = pVtab->zErrMsg; |
| 5151 | pVtab->zErrMsg = 0; |
danielk1977 | 5a114ca | 2008-08-02 15:10:08 +0000 | [diff] [blame] | 5152 | sqlite3VtabUnlock(db, pVtab); |
danielk1977 | a298e90 | 2006-06-22 09:53:48 +0000 | [diff] [blame] | 5153 | if( rc==SQLITE_OK ){ |
drh | 4dc754d | 2008-07-23 18:17:32 +0000 | [diff] [blame] | 5154 | res = pModule->xEof(pVtabCursor); |
danielk1977 | a298e90 | 2006-06-22 09:53:48 +0000 | [diff] [blame] | 5155 | } |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 5156 | if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse; |
| 5157 | |
danielk1977 | a298e90 | 2006-06-22 09:53:48 +0000 | [diff] [blame] | 5158 | if( res ){ |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 5159 | pc = pOp->p2 - 1; |
| 5160 | } |
| 5161 | } |
drh | 1d454a3 | 2008-01-31 19:34:51 +0000 | [diff] [blame] | 5162 | pCur->nullRow = 0; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 5163 | |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 5164 | break; |
| 5165 | } |
| 5166 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 5167 | |
| 5168 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 5169 | /* Opcode: VColumn P1 P2 P3 * * |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 5170 | ** |
drh | 2133d82 | 2008-01-03 18:44:59 +0000 | [diff] [blame] | 5171 | ** Store the value of the P2-th column of |
| 5172 | ** the row of the virtual-table that the |
| 5173 | ** P1 cursor is pointing to into register P3. |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 5174 | */ |
| 5175 | case OP_VColumn: { |
danielk1977 | 3e3a84d | 2008-08-01 17:37:40 +0000 | [diff] [blame] | 5176 | sqlite3_vtab *pVtab; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 5177 | const sqlite3_module *pModule; |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 5178 | Mem *pDest; |
| 5179 | sqlite3_context sContext; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 5180 | |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 5181 | VdbeCursor *pCur = p->apCsr[pOp->p1]; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 5182 | assert( pCur->pVtabCursor ); |
drh | 2945b4a | 2008-01-31 15:53:45 +0000 | [diff] [blame] | 5183 | assert( pOp->p3>0 && pOp->p3<=p->nMem ); |
| 5184 | pDest = &p->aMem[pOp->p3]; |
| 5185 | if( pCur->nullRow ){ |
| 5186 | sqlite3VdbeMemSetNull(pDest); |
| 5187 | break; |
| 5188 | } |
danielk1977 | 3e3a84d | 2008-08-01 17:37:40 +0000 | [diff] [blame] | 5189 | pVtab = pCur->pVtabCursor->pVtab; |
| 5190 | pModule = pVtab->pModule; |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 5191 | assert( pModule->xColumn ); |
| 5192 | memset(&sContext, 0, sizeof(sContext)); |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 5193 | |
| 5194 | /* The output cell may already have a buffer allocated. Move |
| 5195 | ** the current contents to sContext.s so in case the user-function |
| 5196 | ** can use the already allocated buffer instead of allocating a |
| 5197 | ** new one. |
| 5198 | */ |
| 5199 | sqlite3VdbeMemMove(&sContext.s, pDest); |
| 5200 | MemSetTypeFlag(&sContext.s, MEM_Null); |
| 5201 | |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 5202 | if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse; |
| 5203 | rc = pModule->xColumn(pCur->pVtabCursor, &sContext, pOp->p2); |
danielk1977 | 3e3a84d | 2008-08-01 17:37:40 +0000 | [diff] [blame] | 5204 | sqlite3DbFree(db, p->zErrMsg); |
| 5205 | p->zErrMsg = pVtab->zErrMsg; |
| 5206 | pVtab->zErrMsg = 0; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 5207 | |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 5208 | /* Copy the result of the function to the P3 register. We |
shane | be21779 | 2009-03-05 04:20:31 +0000 | [diff] [blame] | 5209 | ** do this regardless of whether or not an error occurred to ensure any |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 5210 | ** dynamic allocation in sContext.s (a Mem struct) is released. |
| 5211 | */ |
| 5212 | sqlite3VdbeChangeEncoding(&sContext.s, encoding); |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 5213 | REGISTER_TRACE(pOp->p3, pDest); |
| 5214 | sqlite3VdbeMemMove(pDest, &sContext.s); |
| 5215 | UPDATE_MAX_BLOBSIZE(pDest); |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 5216 | |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 5217 | if( sqlite3SafetyOn(db) ){ |
| 5218 | goto abort_due_to_misuse; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 5219 | } |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 5220 | if( sqlite3VdbeMemTooBig(pDest) ){ |
| 5221 | goto too_big; |
| 5222 | } |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 5223 | break; |
| 5224 | } |
| 5225 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 5226 | |
| 5227 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 5228 | /* Opcode: VNext P1 P2 * * * |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 5229 | ** |
| 5230 | ** Advance virtual table P1 to the next row in its result set and |
| 5231 | ** jump to instruction P2. Or, if the virtual table has reached |
| 5232 | ** the end of its result set, then fall through to the next instruction. |
| 5233 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 5234 | case OP_VNext: { /* jump */ |
danielk1977 | 3e3a84d | 2008-08-01 17:37:40 +0000 | [diff] [blame] | 5235 | sqlite3_vtab *pVtab; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 5236 | const sqlite3_module *pModule; |
drh | c54a617 | 2009-06-02 16:06:03 +0000 | [diff] [blame] | 5237 | int res; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 5238 | VdbeCursor *pCur; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 5239 | |
drh | c54a617 | 2009-06-02 16:06:03 +0000 | [diff] [blame] | 5240 | res = 0; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 5241 | pCur = p->apCsr[pOp->p1]; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 5242 | assert( pCur->pVtabCursor ); |
drh | 2945b4a | 2008-01-31 15:53:45 +0000 | [diff] [blame] | 5243 | if( pCur->nullRow ){ |
| 5244 | break; |
| 5245 | } |
danielk1977 | 3e3a84d | 2008-08-01 17:37:40 +0000 | [diff] [blame] | 5246 | pVtab = pCur->pVtabCursor->pVtab; |
| 5247 | pModule = pVtab->pModule; |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 5248 | assert( pModule->xNext ); |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 5249 | |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 5250 | /* Invoke the xNext() method of the module. There is no way for the |
| 5251 | ** underlying implementation to return an error if one occurs during |
| 5252 | ** xNext(). Instead, if an error occurs, true is returned (indicating that |
| 5253 | ** data is available) and the error code returned when xColumn or |
| 5254 | ** some other method is next invoked on the save virtual table cursor. |
| 5255 | */ |
| 5256 | if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse; |
danielk1977 | 5a114ca | 2008-08-02 15:10:08 +0000 | [diff] [blame] | 5257 | sqlite3VtabLock(pVtab); |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 5258 | p->inVtabMethod = 1; |
| 5259 | rc = pModule->xNext(pCur->pVtabCursor); |
| 5260 | p->inVtabMethod = 0; |
danielk1977 | 3e3a84d | 2008-08-01 17:37:40 +0000 | [diff] [blame] | 5261 | sqlite3DbFree(db, p->zErrMsg); |
| 5262 | p->zErrMsg = pVtab->zErrMsg; |
| 5263 | pVtab->zErrMsg = 0; |
danielk1977 | 5a114ca | 2008-08-02 15:10:08 +0000 | [diff] [blame] | 5264 | sqlite3VtabUnlock(db, pVtab); |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 5265 | if( rc==SQLITE_OK ){ |
| 5266 | res = pModule->xEof(pCur->pVtabCursor); |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 5267 | } |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 5268 | if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 5269 | |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 5270 | if( !res ){ |
| 5271 | /* If there is data, jump to P2 */ |
| 5272 | pc = pOp->p2 - 1; |
| 5273 | } |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 5274 | break; |
| 5275 | } |
| 5276 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 5277 | |
danielk1977 | 182c4ba | 2007-06-27 15:53:34 +0000 | [diff] [blame] | 5278 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 5279 | /* Opcode: VRename P1 * * P4 * |
danielk1977 | 182c4ba | 2007-06-27 15:53:34 +0000 | [diff] [blame] | 5280 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 5281 | ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure. |
danielk1977 | 182c4ba | 2007-06-27 15:53:34 +0000 | [diff] [blame] | 5282 | ** This opcode invokes the corresponding xRename method. The value |
danielk1977 | 6dbee81 | 2008-01-03 18:39:41 +0000 | [diff] [blame] | 5283 | ** in register P1 is passed as the zName argument to the xRename method. |
danielk1977 | 182c4ba | 2007-06-27 15:53:34 +0000 | [diff] [blame] | 5284 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 5285 | case OP_VRename: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 5286 | sqlite3_vtab *pVtab; |
| 5287 | Mem *pName; |
| 5288 | |
| 5289 | pVtab = pOp->p4.pVtab; |
| 5290 | pName = &p->aMem[pOp->p1]; |
danielk1977 | 182c4ba | 2007-06-27 15:53:34 +0000 | [diff] [blame] | 5291 | assert( pVtab->pModule->xRename ); |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 5292 | REGISTER_TRACE(pOp->p1, pName); |
danielk1977 | 182c4ba | 2007-06-27 15:53:34 +0000 | [diff] [blame] | 5293 | |
danielk1977 | 6dbee81 | 2008-01-03 18:39:41 +0000 | [diff] [blame] | 5294 | Stringify(pName, encoding); |
danielk1977 | 182c4ba | 2007-06-27 15:53:34 +0000 | [diff] [blame] | 5295 | |
| 5296 | if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse; |
| 5297 | sqlite3VtabLock(pVtab); |
danielk1977 | 6dbee81 | 2008-01-03 18:39:41 +0000 | [diff] [blame] | 5298 | rc = pVtab->pModule->xRename(pVtab, pName->z); |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 5299 | sqlite3DbFree(db, p->zErrMsg); |
drh | 80cc85b | 2008-07-23 21:07:25 +0000 | [diff] [blame] | 5300 | p->zErrMsg = pVtab->zErrMsg; |
| 5301 | pVtab->zErrMsg = 0; |
danielk1977 | 182c4ba | 2007-06-27 15:53:34 +0000 | [diff] [blame] | 5302 | sqlite3VtabUnlock(db, pVtab); |
| 5303 | if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse; |
| 5304 | |
danielk1977 | 182c4ba | 2007-06-27 15:53:34 +0000 | [diff] [blame] | 5305 | break; |
| 5306 | } |
| 5307 | #endif |
drh | 4cbdda9 | 2006-06-14 19:00:20 +0000 | [diff] [blame] | 5308 | |
| 5309 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 5310 | /* Opcode: VUpdate P1 P2 P3 P4 * |
danielk1977 | 399918f | 2006-06-14 13:03:23 +0000 | [diff] [blame] | 5311 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 5312 | ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure. |
danielk1977 | 399918f | 2006-06-14 13:03:23 +0000 | [diff] [blame] | 5313 | ** This opcode invokes the corresponding xUpdate method. P2 values |
danielk1977 | 2a339ff | 2008-01-03 17:31:44 +0000 | [diff] [blame] | 5314 | ** are contiguous memory cells starting at P3 to pass to the xUpdate |
| 5315 | ** invocation. The value in register (P3+P2-1) corresponds to the |
| 5316 | ** p2th element of the argv array passed to xUpdate. |
drh | 4cbdda9 | 2006-06-14 19:00:20 +0000 | [diff] [blame] | 5317 | ** |
| 5318 | ** The xUpdate method will do a DELETE or an INSERT or both. |
danielk1977 | 2a339ff | 2008-01-03 17:31:44 +0000 | [diff] [blame] | 5319 | ** The argv[0] element (which corresponds to memory cell P3) |
| 5320 | ** is the rowid of a row to delete. If argv[0] is NULL then no |
| 5321 | ** deletion occurs. The argv[1] element is the rowid of the new |
| 5322 | ** row. This can be NULL to have the virtual table select the new |
| 5323 | ** rowid for itself. The subsequent elements in the array are |
| 5324 | ** the values of columns in the new row. |
drh | 4cbdda9 | 2006-06-14 19:00:20 +0000 | [diff] [blame] | 5325 | ** |
| 5326 | ** If P2==1 then no insert is performed. argv[0] is the rowid of |
| 5327 | ** a row to delete. |
danielk1977 | 1f6eec5 | 2006-06-16 06:17:47 +0000 | [diff] [blame] | 5328 | ** |
| 5329 | ** P1 is a boolean flag. If it is set to true and the xUpdate call |
| 5330 | ** is successful, then the value returned by sqlite3_last_insert_rowid() |
| 5331 | ** is set to the value of the rowid for the row just inserted. |
danielk1977 | 399918f | 2006-06-14 13:03:23 +0000 | [diff] [blame] | 5332 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 5333 | case OP_VUpdate: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 5334 | sqlite3_vtab *pVtab; |
| 5335 | sqlite3_module *pModule; |
| 5336 | int nArg; |
| 5337 | int i; |
| 5338 | sqlite_int64 rowid; |
| 5339 | Mem **apArg; |
| 5340 | Mem *pX; |
| 5341 | |
| 5342 | pVtab = pOp->p4.pVtab; |
| 5343 | pModule = (sqlite3_module *)pVtab->pModule; |
| 5344 | nArg = pOp->p2; |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 5345 | assert( pOp->p4type==P4_VTAB ); |
danielk1977 | 399918f | 2006-06-14 13:03:23 +0000 | [diff] [blame] | 5346 | if( pModule->xUpdate==0 ){ |
drh | f089aa4 | 2008-07-08 19:34:06 +0000 | [diff] [blame] | 5347 | sqlite3SetString(&p->zErrMsg, db, "read-only table"); |
danielk1977 | 399918f | 2006-06-14 13:03:23 +0000 | [diff] [blame] | 5348 | rc = SQLITE_ERROR; |
| 5349 | }else{ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 5350 | apArg = p->apArg; |
| 5351 | pX = &p->aMem[pOp->p3]; |
danielk1977 | 2a339ff | 2008-01-03 17:31:44 +0000 | [diff] [blame] | 5352 | for(i=0; i<nArg; i++){ |
drh | 9c41938 | 2006-06-16 21:13:21 +0000 | [diff] [blame] | 5353 | storeTypeInfo(pX, 0); |
| 5354 | apArg[i] = pX; |
danielk1977 | 2a339ff | 2008-01-03 17:31:44 +0000 | [diff] [blame] | 5355 | pX++; |
danielk1977 | 399918f | 2006-06-14 13:03:23 +0000 | [diff] [blame] | 5356 | } |
danielk1977 | c7d5410 | 2006-06-15 07:29:00 +0000 | [diff] [blame] | 5357 | if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse; |
drh | 189d4af | 2006-09-02 20:57:52 +0000 | [diff] [blame] | 5358 | sqlite3VtabLock(pVtab); |
danielk1977 | 1f6eec5 | 2006-06-16 06:17:47 +0000 | [diff] [blame] | 5359 | rc = pModule->xUpdate(pVtab, nArg, apArg, &rowid); |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 5360 | sqlite3DbFree(db, p->zErrMsg); |
drh | 80cc85b | 2008-07-23 21:07:25 +0000 | [diff] [blame] | 5361 | p->zErrMsg = pVtab->zErrMsg; |
| 5362 | pVtab->zErrMsg = 0; |
danielk1977 | a04a34f | 2007-04-16 15:06:25 +0000 | [diff] [blame] | 5363 | sqlite3VtabUnlock(db, pVtab); |
danielk1977 | c7d5410 | 2006-06-15 07:29:00 +0000 | [diff] [blame] | 5364 | if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse; |
danielk1977 | 1f6eec5 | 2006-06-16 06:17:47 +0000 | [diff] [blame] | 5365 | if( pOp->p1 && rc==SQLITE_OK ){ |
| 5366 | assert( nArg>1 && apArg[0] && (apArg[0]->flags&MEM_Null) ); |
| 5367 | db->lastRowid = rowid; |
| 5368 | } |
drh | b5df144 | 2008-04-10 14:00:09 +0000 | [diff] [blame] | 5369 | p->nChange++; |
danielk1977 | 399918f | 2006-06-14 13:03:23 +0000 | [diff] [blame] | 5370 | } |
drh | 4cbdda9 | 2006-06-14 19:00:20 +0000 | [diff] [blame] | 5371 | break; |
danielk1977 | 399918f | 2006-06-14 13:03:23 +0000 | [diff] [blame] | 5372 | } |
| 5373 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 5374 | |
danielk1977 | 59a9379 | 2008-05-15 17:48:20 +0000 | [diff] [blame] | 5375 | #ifndef SQLITE_OMIT_PAGER_PRAGMAS |
| 5376 | /* Opcode: Pagecount P1 P2 * * * |
| 5377 | ** |
| 5378 | ** Write the current number of pages in database P1 to memory cell P2. |
| 5379 | */ |
| 5380 | case OP_Pagecount: { /* out2-prerelease */ |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 5381 | int p1; |
danielk1977 | 59a9379 | 2008-05-15 17:48:20 +0000 | [diff] [blame] | 5382 | int nPage; |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 5383 | Pager *pPager; |
danielk1977 | 59a9379 | 2008-05-15 17:48:20 +0000 | [diff] [blame] | 5384 | |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 5385 | p1 = pOp->p1; |
| 5386 | pPager = sqlite3BtreePager(db->aDb[p1].pBt); |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame] | 5387 | rc = sqlite3PagerPagecount(pPager, &nPage); |
| 5388 | if( rc==SQLITE_OK ){ |
danielk1977 | 59a9379 | 2008-05-15 17:48:20 +0000 | [diff] [blame] | 5389 | pOut->flags = MEM_Int; |
| 5390 | pOut->u.i = nPage; |
| 5391 | } |
| 5392 | break; |
| 5393 | } |
| 5394 | #endif |
| 5395 | |
drh | 949f9cd | 2008-01-12 21:35:57 +0000 | [diff] [blame] | 5396 | #ifndef SQLITE_OMIT_TRACE |
| 5397 | /* Opcode: Trace * * * P4 * |
| 5398 | ** |
| 5399 | ** If tracing is enabled (by the sqlite3_trace()) interface, then |
| 5400 | ** the UTF-8 string contained in P4 is emitted on the trace callback. |
| 5401 | */ |
| 5402 | case OP_Trace: { |
drh | 856c103 | 2009-06-02 15:21:42 +0000 | [diff] [blame] | 5403 | char *zTrace; |
| 5404 | |
| 5405 | zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql); |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 5406 | if( zTrace ){ |
drh | 949f9cd | 2008-01-12 21:35:57 +0000 | [diff] [blame] | 5407 | if( db->xTrace ){ |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 5408 | db->xTrace(db->pTraceArg, zTrace); |
drh | 949f9cd | 2008-01-12 21:35:57 +0000 | [diff] [blame] | 5409 | } |
| 5410 | #ifdef SQLITE_DEBUG |
| 5411 | if( (db->flags & SQLITE_SqlTrace)!=0 ){ |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 5412 | sqlite3DebugPrintf("SQL-trace: %s\n", zTrace); |
drh | 949f9cd | 2008-01-12 21:35:57 +0000 | [diff] [blame] | 5413 | } |
| 5414 | #endif /* SQLITE_DEBUG */ |
| 5415 | } |
| 5416 | break; |
| 5417 | } |
| 5418 | #endif |
| 5419 | |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 5420 | |
| 5421 | /* Opcode: Noop * * * * * |
| 5422 | ** |
| 5423 | ** Do nothing. This instruction is often useful as a jump |
| 5424 | ** destination. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5425 | */ |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 5426 | /* |
| 5427 | ** The magic Explain opcode are only inserted when explain==2 (which |
| 5428 | ** is to say when the EXPLAIN QUERY PLAN syntax is used.) |
| 5429 | ** This opcode records information from the optimizer. It is the |
| 5430 | ** the same as a no-op. This opcodesnever appears in a real VM program. |
| 5431 | */ |
| 5432 | default: { /* This is really OP_Noop and OP_Explain */ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5433 | break; |
| 5434 | } |
| 5435 | |
| 5436 | /***************************************************************************** |
| 5437 | ** The cases of the switch statement above this line should all be indented |
| 5438 | ** by 6 spaces. But the left-most 6 spaces have been removed to improve the |
| 5439 | ** readability. From this point on down, the normal indentation rules are |
| 5440 | ** restored. |
| 5441 | *****************************************************************************/ |
| 5442 | } |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 5443 | |
drh | 7b39686 | 2003-01-01 23:06:20 +0000 | [diff] [blame] | 5444 | #ifdef VDBE_PROFILE |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 5445 | { |
shane | 9bcbdad | 2008-05-29 20:22:37 +0000 | [diff] [blame] | 5446 | u64 elapsed = sqlite3Hwtime() - start; |
| 5447 | pOp->cycles += elapsed; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 5448 | pOp->cnt++; |
| 5449 | #if 0 |
shane | 9bcbdad | 2008-05-29 20:22:37 +0000 | [diff] [blame] | 5450 | fprintf(stdout, "%10llu ", elapsed); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 5451 | sqlite3VdbePrintOp(stdout, origPc, &p->aOp[origPc]); |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 5452 | #endif |
| 5453 | } |
drh | 7b39686 | 2003-01-01 23:06:20 +0000 | [diff] [blame] | 5454 | #endif |
| 5455 | |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 5456 | /* The following code adds nothing to the actual functionality |
| 5457 | ** of the program. It is only here for testing and debugging. |
| 5458 | ** On the other hand, it does burn CPU cycles every time through |
| 5459 | ** the evaluator loop. So we can leave it out when NDEBUG is defined. |
| 5460 | */ |
| 5461 | #ifndef NDEBUG |
drh | a611040 | 2005-07-28 20:51:19 +0000 | [diff] [blame] | 5462 | assert( pc>=-1 && pc<p->nOp ); |
drh | ae7e151 | 2007-05-02 16:51:59 +0000 | [diff] [blame] | 5463 | |
drh | cf1023c | 2007-05-08 20:59:49 +0000 | [diff] [blame] | 5464 | #ifdef SQLITE_DEBUG |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 5465 | if( p->trace ){ |
| 5466 | if( rc!=0 ) fprintf(p->trace,"rc=%d\n",rc); |
drh | ca48c90 | 2008-01-18 14:08:24 +0000 | [diff] [blame] | 5467 | if( opProperty & OPFLG_OUT2_PRERELEASE ){ |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 5468 | registerTrace(p->trace, pOp->p2, pOut); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5469 | } |
drh | ca48c90 | 2008-01-18 14:08:24 +0000 | [diff] [blame] | 5470 | if( opProperty & OPFLG_OUT3 ){ |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 5471 | registerTrace(p->trace, pOp->p3, pOut); |
| 5472 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5473 | } |
danielk1977 | b5402fb | 2005-01-12 07:15:04 +0000 | [diff] [blame] | 5474 | #endif /* SQLITE_DEBUG */ |
| 5475 | #endif /* NDEBUG */ |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 5476 | } /* The end of the for(;;) loop the loops through opcodes */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5477 | |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 5478 | /* If we reach this point, it means that execution is finished with |
| 5479 | ** an error of some kind. |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 5480 | */ |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 5481 | vdbe_error_halt: |
| 5482 | assert( rc ); |
| 5483 | p->rc = rc; |
drh | 92f02c3 | 2004-09-02 14:57:08 +0000 | [diff] [blame] | 5484 | sqlite3VdbeHalt(p); |
danielk1977 | 7eaabcd | 2008-07-07 14:56:56 +0000 | [diff] [blame] | 5485 | if( rc==SQLITE_IOERR_NOMEM ) db->mallocFailed = 1; |
| 5486 | rc = SQLITE_ERROR; |
drh | 900b31e | 2007-08-28 02:27:51 +0000 | [diff] [blame] | 5487 | |
| 5488 | /* This is the only way out of this procedure. We have to |
| 5489 | ** release the mutexes on btrees that were acquired at the |
| 5490 | ** top. */ |
| 5491 | vdbe_return: |
drh | 4cf7c7f | 2007-08-28 23:28:07 +0000 | [diff] [blame] | 5492 | sqlite3BtreeMutexArrayLeave(&p->aMutex); |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 5493 | return rc; |
| 5494 | |
drh | 023ae03 | 2007-05-08 12:12:16 +0000 | [diff] [blame] | 5495 | /* Jump to here if a string or blob larger than SQLITE_MAX_LENGTH |
| 5496 | ** is encountered. |
| 5497 | */ |
| 5498 | too_big: |
drh | f089aa4 | 2008-07-08 19:34:06 +0000 | [diff] [blame] | 5499 | sqlite3SetString(&p->zErrMsg, db, "string or blob too big"); |
drh | 023ae03 | 2007-05-08 12:12:16 +0000 | [diff] [blame] | 5500 | rc = SQLITE_TOOBIG; |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 5501 | goto vdbe_error_halt; |
drh | 023ae03 | 2007-05-08 12:12:16 +0000 | [diff] [blame] | 5502 | |
drh | 98640a3 | 2007-06-07 19:08:32 +0000 | [diff] [blame] | 5503 | /* Jump to here if a malloc() fails. |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 5504 | */ |
| 5505 | no_mem: |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 5506 | db->mallocFailed = 1; |
drh | f089aa4 | 2008-07-08 19:34:06 +0000 | [diff] [blame] | 5507 | sqlite3SetString(&p->zErrMsg, db, "out of memory"); |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 5508 | rc = SQLITE_NOMEM; |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 5509 | goto vdbe_error_halt; |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 5510 | |
| 5511 | /* Jump to here for an SQLITE_MISUSE error. |
| 5512 | */ |
| 5513 | abort_due_to_misuse: |
| 5514 | rc = SQLITE_MISUSE; |
| 5515 | /* Fall thru into abort_due_to_error */ |
| 5516 | |
| 5517 | /* Jump to here for any other kind of fatal error. The "rc" variable |
| 5518 | ** should hold the error number. |
| 5519 | */ |
| 5520 | abort_due_to_error: |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 5521 | assert( p->zErrMsg==0 ); |
| 5522 | if( db->mallocFailed ) rc = SQLITE_NOMEM; |
danielk1977 | 7eaabcd | 2008-07-07 14:56:56 +0000 | [diff] [blame] | 5523 | if( rc!=SQLITE_IOERR_NOMEM ){ |
drh | f089aa4 | 2008-07-08 19:34:06 +0000 | [diff] [blame] | 5524 | sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3ErrStr(rc)); |
danielk1977 | 7eaabcd | 2008-07-07 14:56:56 +0000 | [diff] [blame] | 5525 | } |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 5526 | goto vdbe_error_halt; |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 5527 | |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 5528 | /* Jump to here if the sqlite3_interrupt() API sets the interrupt |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 5529 | ** flag. |
| 5530 | */ |
| 5531 | abort_due_to_interrupt: |
drh | 881feaa | 2006-07-26 01:39:30 +0000 | [diff] [blame] | 5532 | assert( db->u1.isInterrupted ); |
drh | 7e8b848 | 2008-01-23 03:03:05 +0000 | [diff] [blame] | 5533 | rc = SQLITE_INTERRUPT; |
danielk1977 | 026d270 | 2004-06-14 13:14:59 +0000 | [diff] [blame] | 5534 | p->rc = rc; |
drh | f089aa4 | 2008-07-08 19:34:06 +0000 | [diff] [blame] | 5535 | sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3ErrStr(rc)); |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 5536 | goto vdbe_error_halt; |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 5537 | } |