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 | ** |
danielk1977 | f73ab8b | 2008-12-29 10:39:53 +0000 | [diff] [blame^] | 46 | ** $Id: vdbe.c,v 1.807 2008/12/29 10:39:54 danielk1977 Exp $ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 47 | */ |
| 48 | #include "sqliteInt.h" |
drh | 7c68d60 | 2000-10-11 19:28:51 +0000 | [diff] [blame] | 49 | #include <ctype.h> |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 50 | #include "vdbeInt.h" |
drh | 8f619cc | 2002-09-08 00:04:50 +0000 | [diff] [blame] | 51 | |
| 52 | /* |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 53 | ** The following global variable is incremented every time a cursor |
drh | 959403f | 2008-12-12 17:56:16 +0000 | [diff] [blame] | 54 | ** 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] | 55 | ** procedures use this information to make sure that indices are |
drh | ac82fcf | 2002-09-08 17:23:41 +0000 | [diff] [blame] | 56 | ** working correctly. This variable has no function other than to |
| 57 | ** help verify the correct operation of the library. |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 58 | */ |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 59 | #ifdef SQLITE_TEST |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 60 | int sqlite3_search_count = 0; |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 61 | #endif |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 62 | |
drh | f603871 | 2004-02-08 18:07:34 +0000 | [diff] [blame] | 63 | /* |
| 64 | ** When this global variable is positive, it gets decremented once before |
drh | 881feaa | 2006-07-26 01:39:30 +0000 | [diff] [blame] | 65 | ** each instruction in the VDBE. When reaches zero, the u1.isInterrupted |
| 66 | ** field of the sqlite3 structure is set in order to simulate and interrupt. |
drh | f603871 | 2004-02-08 18:07:34 +0000 | [diff] [blame] | 67 | ** |
| 68 | ** This facility is used for testing purposes only. It does not function |
| 69 | ** in an ordinary build. |
| 70 | */ |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 71 | #ifdef SQLITE_TEST |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 72 | int sqlite3_interrupt_count = 0; |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 73 | #endif |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 74 | |
danielk1977 | 7e18c25 | 2004-05-25 11:47:24 +0000 | [diff] [blame] | 75 | /* |
drh | 6bf8957 | 2004-11-03 16:27:01 +0000 | [diff] [blame] | 76 | ** The next global variable is incremented each type the OP_Sort opcode |
| 77 | ** is executed. The test procedures use this information to make sure that |
shane | 21e7feb | 2008-05-30 15:59:49 +0000 | [diff] [blame] | 78 | ** sorting is occurring or not occurring at appropriate times. This variable |
drh | 6bf8957 | 2004-11-03 16:27:01 +0000 | [diff] [blame] | 79 | ** has no function other than to help verify the correct operation of the |
| 80 | ** library. |
| 81 | */ |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 82 | #ifdef SQLITE_TEST |
drh | 6bf8957 | 2004-11-03 16:27:01 +0000 | [diff] [blame] | 83 | int sqlite3_sort_count = 0; |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 84 | #endif |
drh | 6bf8957 | 2004-11-03 16:27:01 +0000 | [diff] [blame] | 85 | |
| 86 | /* |
drh | ae7e151 | 2007-05-02 16:51:59 +0000 | [diff] [blame] | 87 | ** The next global variable records the size of the largest MEM_Blob |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 88 | ** 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] | 89 | ** use this information to make sure that the zero-blob functionality |
| 90 | ** is working correctly. This variable has no function other than to |
| 91 | ** help verify the correct operation of the library. |
| 92 | */ |
| 93 | #ifdef SQLITE_TEST |
| 94 | int sqlite3_max_blobsize = 0; |
drh | ca48c90 | 2008-01-18 14:08:24 +0000 | [diff] [blame] | 95 | static void updateMaxBlobsize(Mem *p){ |
| 96 | if( (p->flags & (MEM_Str|MEM_Blob))!=0 && p->n>sqlite3_max_blobsize ){ |
| 97 | sqlite3_max_blobsize = p->n; |
| 98 | } |
| 99 | } |
drh | ae7e151 | 2007-05-02 16:51:59 +0000 | [diff] [blame] | 100 | #endif |
| 101 | |
| 102 | /* |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 103 | ** Test a register to see if it exceeds the current maximum blob size. |
| 104 | ** If it does, record the new maximum blob size. |
| 105 | */ |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 106 | #if defined(SQLITE_TEST) && !defined(SQLITE_OMIT_BUILTIN_TEST) |
drh | ca48c90 | 2008-01-18 14:08:24 +0000 | [diff] [blame] | 107 | # define UPDATE_MAX_BLOBSIZE(P) updateMaxBlobsize(P) |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 108 | #else |
| 109 | # define UPDATE_MAX_BLOBSIZE(P) |
| 110 | #endif |
| 111 | |
| 112 | /* |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 113 | ** Convert the given register into a string if it isn't one |
danielk1977 | bd7e460 | 2004-05-24 07:34:48 +0000 | [diff] [blame] | 114 | ** already. Return non-zero if a malloc() fails. |
| 115 | */ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 116 | #define Stringify(P, enc) \ |
| 117 | if(((P)->flags&(MEM_Str|MEM_Blob))==0 && sqlite3VdbeMemStringify(P,enc)) \ |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 118 | { goto no_mem; } |
danielk1977 | bd7e460 | 2004-05-24 07:34:48 +0000 | [diff] [blame] | 119 | |
| 120 | /* |
danielk1977 | bd7e460 | 2004-05-24 07:34:48 +0000 | [diff] [blame] | 121 | ** An ephemeral string value (signified by the MEM_Ephem flag) contains |
| 122 | ** a pointer to a dynamically allocated string where some other entity |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 123 | ** is responsible for deallocating that string. Because the register |
| 124 | ** does not control the string, it might be deleted without the register |
| 125 | ** knowing it. |
danielk1977 | bd7e460 | 2004-05-24 07:34:48 +0000 | [diff] [blame] | 126 | ** |
| 127 | ** This routine converts an ephemeral string into a dynamically allocated |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 128 | ** string that the register itself controls. In other words, it |
danielk1977 | bd7e460 | 2004-05-24 07:34:48 +0000 | [diff] [blame] | 129 | ** converts an MEM_Ephem string into an MEM_Dyn string. |
| 130 | */ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 131 | #define Deephemeralize(P) \ |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 132 | if( ((P)->flags&MEM_Ephem)!=0 \ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 133 | && sqlite3VdbeMemMakeWriteable(P) ){ goto no_mem;} |
danielk1977 | 93d4675 | 2004-05-23 13:30:58 +0000 | [diff] [blame] | 134 | |
| 135 | /* |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 136 | ** Call sqlite3VdbeMemExpandBlob() on the supplied value (type Mem*) |
| 137 | ** P if required. |
| 138 | */ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 139 | #define ExpandBlob(P) (((P)->flags&MEM_Zero)?sqlite3VdbeMemExpandBlob(P):0) |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 140 | |
| 141 | /* |
shane | 21e7feb | 2008-05-30 15:59:49 +0000 | [diff] [blame] | 142 | ** Argument pMem points at a register that will be passed to a |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 143 | ** user-defined function or returned to the user as the result of a query. |
| 144 | ** 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] | 145 | ** register variables. This routine sets the pMem->enc and pMem->type |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 146 | ** variables used by the sqlite3_value_*() routines. |
| 147 | */ |
drh | 3a41a3f | 2004-05-30 02:14:17 +0000 | [diff] [blame] | 148 | #define storeTypeInfo(A,B) _storeTypeInfo(A) |
| 149 | static void _storeTypeInfo(Mem *pMem){ |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 150 | int flags = pMem->flags; |
| 151 | if( flags & MEM_Null ){ |
drh | 9c05483 | 2004-05-31 18:51:57 +0000 | [diff] [blame] | 152 | pMem->type = SQLITE_NULL; |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 153 | } |
| 154 | else if( flags & MEM_Int ){ |
drh | 9c05483 | 2004-05-31 18:51:57 +0000 | [diff] [blame] | 155 | pMem->type = SQLITE_INTEGER; |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 156 | } |
| 157 | else if( flags & MEM_Real ){ |
drh | 9c05483 | 2004-05-31 18:51:57 +0000 | [diff] [blame] | 158 | pMem->type = SQLITE_FLOAT; |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 159 | } |
| 160 | else if( flags & MEM_Str ){ |
drh | 9c05483 | 2004-05-31 18:51:57 +0000 | [diff] [blame] | 161 | pMem->type = SQLITE_TEXT; |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 162 | }else{ |
drh | 9c05483 | 2004-05-31 18:51:57 +0000 | [diff] [blame] | 163 | pMem->type = SQLITE_BLOB; |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 164 | } |
| 165 | } |
danielk1977 | 8a6b541 | 2004-05-24 07:04:25 +0000 | [diff] [blame] | 166 | |
| 167 | /* |
drh | 3a40f69 | 2008-01-04 16:50:09 +0000 | [diff] [blame] | 168 | ** Properties of opcodes. The OPFLG_INITIALIZER macro is |
| 169 | ** created by mkopcodeh.awk during compilation. Data is obtained |
| 170 | ** from the comments following the "case OP_xxxx:" statements in |
| 171 | ** this file. |
drh | 3a40f69 | 2008-01-04 16:50:09 +0000 | [diff] [blame] | 172 | */ |
danielk1977 | 263ac19 | 2008-09-02 11:05:01 +0000 | [diff] [blame] | 173 | static const unsigned char opcodeProperty[] = OPFLG_INITIALIZER; |
drh | 3a40f69 | 2008-01-04 16:50:09 +0000 | [diff] [blame] | 174 | |
| 175 | /* |
| 176 | ** Return true if an opcode has any of the OPFLG_xxx properties |
| 177 | ** specified by mask. |
| 178 | */ |
| 179 | int sqlite3VdbeOpcodeHasProperty(int opcode, int mask){ |
danielk1977 | 64202cf | 2008-11-17 15:31:47 +0000 | [diff] [blame] | 180 | assert( opcode>0 && opcode<(int)sizeof(opcodeProperty) ); |
drh | 3a40f69 | 2008-01-04 16:50:09 +0000 | [diff] [blame] | 181 | return (opcodeProperty[opcode]&mask)!=0; |
| 182 | } |
| 183 | |
| 184 | /* |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 185 | ** Allocate VdbeCursor number iCur. Return a pointer to it. Return NULL |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 186 | ** if we run out of memory. |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 187 | */ |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 188 | static VdbeCursor *allocateCursor( |
| 189 | Vdbe *p, /* The virtual machine */ |
| 190 | int iCur, /* Index of the new VdbeCursor */ |
| 191 | Op *pOp, /* */ |
drh | 3d4501e | 2008-12-04 20:40:10 +0000 | [diff] [blame] | 192 | int iDb, /* When database the cursor belongs to, or -1 */ |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 193 | int isBtreeCursor /* */ |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 194 | ){ |
| 195 | /* 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] | 196 | ** required for this VdbeCursor structure. It is convenient to use a |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 197 | ** vdbe memory cell to manage the memory allocation required for a |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 198 | ** VdbeCursor structure for the following reasons: |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 199 | ** |
| 200 | ** * Sometimes cursor numbers are used for a couple of different |
| 201 | ** purposes in a vdbe program. The different uses might require |
| 202 | ** different sized allocations. Memory cells provide growable |
| 203 | ** allocations. |
| 204 | ** |
| 205 | ** * When using ENABLE_MEMORY_MANAGEMENT, memory cell buffers can |
| 206 | ** be freed lazily via the sqlite3_release_memory() API. This |
| 207 | ** minimizes the number of malloc calls made by the system. |
| 208 | ** |
| 209 | ** Memory cells for cursors are allocated at the top of the address |
| 210 | ** space. Memory cell (p->nMem) corresponds to cursor 0. Space for |
| 211 | ** cursor 1 is managed by memory cell (p->nMem-1), etc. |
| 212 | */ |
| 213 | Mem *pMem = &p->aMem[p->nMem-iCur]; |
| 214 | |
danielk1977 | 5f09613 | 2008-03-28 15:44:09 +0000 | [diff] [blame] | 215 | int nByte; |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 216 | VdbeCursor *pCx = 0; |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 217 | /* If the opcode of pOp is OP_SetNumColumns, then pOp->p2 contains |
| 218 | ** the number of fields in the records contained in the table or |
| 219 | ** index being opened. Use this to reserve space for the |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 220 | ** VdbeCursor.aType[] array. |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 221 | */ |
| 222 | int nField = 0; |
| 223 | if( pOp->opcode==OP_SetNumColumns || pOp->opcode==OP_OpenEphemeral ){ |
| 224 | nField = pOp->p2; |
| 225 | } |
danielk1977 | 5f09613 | 2008-03-28 15:44:09 +0000 | [diff] [blame] | 226 | nByte = |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 227 | sizeof(VdbeCursor) + |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 228 | (isBtreeCursor?sqlite3BtreeCursorSize():0) + |
| 229 | 2*nField*sizeof(u32); |
| 230 | |
drh | 290c194 | 2004-08-21 17:54:45 +0000 | [diff] [blame] | 231 | assert( iCur<p->nCursor ); |
| 232 | if( p->apCsr[iCur] ){ |
danielk1977 | be71889 | 2006-06-23 08:05:19 +0000 | [diff] [blame] | 233 | sqlite3VdbeFreeCursor(p, p->apCsr[iCur]); |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 234 | p->apCsr[iCur] = 0; |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 235 | } |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 236 | if( SQLITE_OK==sqlite3VdbeMemGrow(pMem, nByte, 0) ){ |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 237 | p->apCsr[iCur] = pCx = (VdbeCursor*)pMem->z; |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 238 | memset(pMem->z, 0, nByte); |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 +0000 | [diff] [blame] | 239 | pCx->iDb = iDb; |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 240 | pCx->nField = nField; |
| 241 | if( nField ){ |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 242 | pCx->aType = (u32 *)&pMem->z[sizeof(VdbeCursor)]; |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 243 | } |
| 244 | if( isBtreeCursor ){ |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 245 | pCx->pCursor = (BtCursor*) |
| 246 | &pMem->z[sizeof(VdbeCursor)+2*nField*sizeof(u32)]; |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 247 | } |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 +0000 | [diff] [blame] | 248 | } |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 249 | return pCx; |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 250 | } |
| 251 | |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 252 | /* |
drh | 29d7210 | 2006-02-09 22:13:41 +0000 | [diff] [blame] | 253 | ** Try to convert a value into a numeric representation if we can |
| 254 | ** do so without loss of information. In other words, if the string |
| 255 | ** looks like a number, convert it into a number. If it does not |
| 256 | ** look like a number, leave it alone. |
| 257 | */ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 258 | static void applyNumericAffinity(Mem *pRec){ |
drh | 29d7210 | 2006-02-09 22:13:41 +0000 | [diff] [blame] | 259 | if( (pRec->flags & (MEM_Real|MEM_Int))==0 ){ |
| 260 | int realnum; |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 261 | sqlite3VdbeMemNulTerminate(pRec); |
drh | 29d7210 | 2006-02-09 22:13:41 +0000 | [diff] [blame] | 262 | if( (pRec->flags&MEM_Str) |
| 263 | && sqlite3IsNumber(pRec->z, &realnum, pRec->enc) ){ |
| 264 | i64 value; |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 265 | sqlite3VdbeChangeEncoding(pRec, SQLITE_UTF8); |
drh | b6a9ece | 2007-06-26 00:37:27 +0000 | [diff] [blame] | 266 | if( !realnum && sqlite3Atoi64(pRec->z, &value) ){ |
drh | 3c024d6 | 2007-03-30 11:23:45 +0000 | [diff] [blame] | 267 | pRec->u.i = value; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 268 | MemSetTypeFlag(pRec, MEM_Int); |
drh | 29d7210 | 2006-02-09 22:13:41 +0000 | [diff] [blame] | 269 | }else{ |
| 270 | sqlite3VdbeMemRealify(pRec); |
| 271 | } |
| 272 | } |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | /* |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 277 | ** Processing is determine by the affinity parameter: |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 278 | ** |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 279 | ** SQLITE_AFF_INTEGER: |
| 280 | ** SQLITE_AFF_REAL: |
| 281 | ** SQLITE_AFF_NUMERIC: |
| 282 | ** Try to convert pRec to an integer representation or a |
| 283 | ** floating-point representation if an integer representation |
| 284 | ** is not possible. Note that the integer representation is |
| 285 | ** always preferred, even if the affinity is REAL, because |
| 286 | ** an integer representation is more space efficient on disk. |
| 287 | ** |
| 288 | ** SQLITE_AFF_TEXT: |
| 289 | ** Convert pRec to a text representation. |
| 290 | ** |
| 291 | ** SQLITE_AFF_NONE: |
| 292 | ** No-op. pRec is unchanged. |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 293 | */ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 294 | static void applyAffinity( |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 295 | Mem *pRec, /* The value to apply affinity to */ |
| 296 | char affinity, /* The affinity to be applied */ |
| 297 | u8 enc /* Use this text encoding */ |
| 298 | ){ |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 299 | if( affinity==SQLITE_AFF_TEXT ){ |
drh | 17c4029 | 2004-07-21 02:53:29 +0000 | [diff] [blame] | 300 | /* Only attempt the conversion to TEXT if there is an integer or real |
| 301 | ** representation (blob and NULL do not get converted) but no string |
| 302 | ** representation. |
| 303 | */ |
| 304 | if( 0==(pRec->flags&MEM_Str) && (pRec->flags&(MEM_Real|MEM_Int)) ){ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 305 | sqlite3VdbeMemStringify(pRec, enc); |
drh | 17c4029 | 2004-07-21 02:53:29 +0000 | [diff] [blame] | 306 | } |
| 307 | pRec->flags &= ~(MEM_Real|MEM_Int); |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 308 | }else if( affinity!=SQLITE_AFF_NONE ){ |
| 309 | assert( affinity==SQLITE_AFF_INTEGER || affinity==SQLITE_AFF_REAL |
| 310 | || affinity==SQLITE_AFF_NUMERIC ); |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 311 | applyNumericAffinity(pRec); |
drh | 29d7210 | 2006-02-09 22:13:41 +0000 | [diff] [blame] | 312 | if( pRec->flags & MEM_Real ){ |
drh | 8df447f | 2005-11-01 15:48:24 +0000 | [diff] [blame] | 313 | sqlite3VdbeIntegerAffinity(pRec); |
drh | 17c4029 | 2004-07-21 02:53:29 +0000 | [diff] [blame] | 314 | } |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 315 | } |
| 316 | } |
| 317 | |
danielk1977 | aee18ef | 2005-03-09 12:26:50 +0000 | [diff] [blame] | 318 | /* |
drh | 29d7210 | 2006-02-09 22:13:41 +0000 | [diff] [blame] | 319 | ** Try to convert the type of a function argument or a result column |
| 320 | ** into a numeric representation. Use either INTEGER or REAL whichever |
| 321 | ** is appropriate. But only do the conversion if it is possible without |
| 322 | ** loss of information and return the revised type of the argument. |
| 323 | ** |
| 324 | ** This is an EXPERIMENTAL api and is subject to change or removal. |
| 325 | */ |
| 326 | int sqlite3_value_numeric_type(sqlite3_value *pVal){ |
| 327 | Mem *pMem = (Mem*)pVal; |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 328 | applyNumericAffinity(pMem); |
drh | 29d7210 | 2006-02-09 22:13:41 +0000 | [diff] [blame] | 329 | storeTypeInfo(pMem, 0); |
| 330 | return pMem->type; |
| 331 | } |
| 332 | |
| 333 | /* |
danielk1977 | aee18ef | 2005-03-09 12:26:50 +0000 | [diff] [blame] | 334 | ** Exported version of applyAffinity(). This one works on sqlite3_value*, |
| 335 | ** not the internal Mem* type. |
| 336 | */ |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 337 | void sqlite3ValueApplyAffinity( |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 338 | sqlite3_value *pVal, |
| 339 | u8 affinity, |
| 340 | u8 enc |
| 341 | ){ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 342 | applyAffinity((Mem *)pVal, affinity, enc); |
danielk1977 | aee18ef | 2005-03-09 12:26:50 +0000 | [diff] [blame] | 343 | } |
| 344 | |
danielk1977 | b5402fb | 2005-01-12 07:15:04 +0000 | [diff] [blame] | 345 | #ifdef SQLITE_DEBUG |
drh | b6f5452 | 2004-05-20 02:42:16 +0000 | [diff] [blame] | 346 | /* |
danielk1977 | ca6b291 | 2004-05-21 10:49:47 +0000 | [diff] [blame] | 347 | ** Write a nice string representation of the contents of cell pMem |
| 348 | ** into buffer zBuf, length nBuf. |
| 349 | */ |
drh | 7416170 | 2006-02-24 02:53:49 +0000 | [diff] [blame] | 350 | void sqlite3VdbeMemPrettyPrint(Mem *pMem, char *zBuf){ |
danielk1977 | ca6b291 | 2004-05-21 10:49:47 +0000 | [diff] [blame] | 351 | char *zCsr = zBuf; |
| 352 | int f = pMem->flags; |
| 353 | |
drh | 5719628 | 2004-10-06 15:41:16 +0000 | [diff] [blame] | 354 | static const char *const encnames[] = {"(X)", "(8)", "(16LE)", "(16BE)"}; |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame] | 355 | |
danielk1977 | ca6b291 | 2004-05-21 10:49:47 +0000 | [diff] [blame] | 356 | if( f&MEM_Blob ){ |
| 357 | int i; |
| 358 | char c; |
| 359 | if( f & MEM_Dyn ){ |
| 360 | c = 'z'; |
| 361 | assert( (f & (MEM_Static|MEM_Ephem))==0 ); |
| 362 | }else if( f & MEM_Static ){ |
| 363 | c = 't'; |
| 364 | assert( (f & (MEM_Dyn|MEM_Ephem))==0 ); |
| 365 | }else if( f & MEM_Ephem ){ |
| 366 | c = 'e'; |
| 367 | assert( (f & (MEM_Static|MEM_Dyn))==0 ); |
| 368 | }else{ |
| 369 | c = 's'; |
| 370 | } |
| 371 | |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 372 | sqlite3_snprintf(100, zCsr, "%c", c); |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 373 | zCsr += sqlite3Strlen30(zCsr); |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 374 | sqlite3_snprintf(100, zCsr, "%d[", pMem->n); |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 375 | zCsr += sqlite3Strlen30(zCsr); |
danielk1977 | ca6b291 | 2004-05-21 10:49:47 +0000 | [diff] [blame] | 376 | for(i=0; i<16 && i<pMem->n; i++){ |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 377 | sqlite3_snprintf(100, zCsr, "%02X", ((int)pMem->z[i] & 0xFF)); |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 378 | zCsr += sqlite3Strlen30(zCsr); |
danielk1977 | ca6b291 | 2004-05-21 10:49:47 +0000 | [diff] [blame] | 379 | } |
| 380 | for(i=0; i<16 && i<pMem->n; i++){ |
| 381 | char z = pMem->z[i]; |
| 382 | if( z<32 || z>126 ) *zCsr++ = '.'; |
| 383 | else *zCsr++ = z; |
| 384 | } |
| 385 | |
drh | e718efe | 2007-05-10 21:14:03 +0000 | [diff] [blame] | 386 | sqlite3_snprintf(100, zCsr, "]%s", encnames[pMem->enc]); |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 387 | zCsr += sqlite3Strlen30(zCsr); |
drh | fdf972a | 2007-05-02 13:30:27 +0000 | [diff] [blame] | 388 | if( f & MEM_Zero ){ |
drh | 8df3284 | 2008-12-09 02:51:23 +0000 | [diff] [blame] | 389 | sqlite3_snprintf(100, zCsr,"+%dz",pMem->u.nZero); |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 390 | zCsr += sqlite3Strlen30(zCsr); |
drh | fdf972a | 2007-05-02 13:30:27 +0000 | [diff] [blame] | 391 | } |
danielk1977 | b1bc953 | 2004-05-22 03:05:33 +0000 | [diff] [blame] | 392 | *zCsr = '\0'; |
| 393 | }else if( f & MEM_Str ){ |
| 394 | int j, k; |
| 395 | zBuf[0] = ' '; |
| 396 | if( f & MEM_Dyn ){ |
| 397 | zBuf[1] = 'z'; |
| 398 | assert( (f & (MEM_Static|MEM_Ephem))==0 ); |
| 399 | }else if( f & MEM_Static ){ |
| 400 | zBuf[1] = 't'; |
| 401 | assert( (f & (MEM_Dyn|MEM_Ephem))==0 ); |
| 402 | }else if( f & MEM_Ephem ){ |
| 403 | zBuf[1] = 'e'; |
| 404 | assert( (f & (MEM_Static|MEM_Dyn))==0 ); |
| 405 | }else{ |
| 406 | zBuf[1] = 's'; |
| 407 | } |
| 408 | k = 2; |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 409 | sqlite3_snprintf(100, &zBuf[k], "%d", pMem->n); |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 410 | k += sqlite3Strlen30(&zBuf[k]); |
danielk1977 | b1bc953 | 2004-05-22 03:05:33 +0000 | [diff] [blame] | 411 | zBuf[k++] = '['; |
| 412 | for(j=0; j<15 && j<pMem->n; j++){ |
| 413 | u8 c = pMem->z[j]; |
danielk1977 | b1bc953 | 2004-05-22 03:05:33 +0000 | [diff] [blame] | 414 | if( c>=0x20 && c<0x7f ){ |
| 415 | zBuf[k++] = c; |
| 416 | }else{ |
| 417 | zBuf[k++] = '.'; |
| 418 | } |
| 419 | } |
| 420 | zBuf[k++] = ']'; |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 421 | sqlite3_snprintf(100,&zBuf[k], encnames[pMem->enc]); |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 422 | k += sqlite3Strlen30(&zBuf[k]); |
danielk1977 | b1bc953 | 2004-05-22 03:05:33 +0000 | [diff] [blame] | 423 | zBuf[k++] = 0; |
danielk1977 | ca6b291 | 2004-05-21 10:49:47 +0000 | [diff] [blame] | 424 | } |
danielk1977 | ca6b291 | 2004-05-21 10:49:47 +0000 | [diff] [blame] | 425 | } |
| 426 | #endif |
| 427 | |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 428 | #ifdef SQLITE_DEBUG |
| 429 | /* |
| 430 | ** Print the value of a register for tracing purposes: |
| 431 | */ |
| 432 | static void memTracePrint(FILE *out, Mem *p){ |
| 433 | if( p->flags & MEM_Null ){ |
| 434 | fprintf(out, " NULL"); |
| 435 | }else if( (p->flags & (MEM_Int|MEM_Str))==(MEM_Int|MEM_Str) ){ |
| 436 | fprintf(out, " si:%lld", p->u.i); |
| 437 | }else if( p->flags & MEM_Int ){ |
| 438 | fprintf(out, " i:%lld", p->u.i); |
| 439 | }else if( p->flags & MEM_Real ){ |
| 440 | fprintf(out, " r:%g", p->r); |
| 441 | }else{ |
| 442 | char zBuf[200]; |
| 443 | sqlite3VdbeMemPrettyPrint(p, zBuf); |
| 444 | fprintf(out, " "); |
| 445 | fprintf(out, "%s", zBuf); |
| 446 | } |
| 447 | } |
| 448 | static void registerTrace(FILE *out, int iReg, Mem *p){ |
| 449 | fprintf(out, "REG[%d] = ", iReg); |
| 450 | memTracePrint(out, p); |
| 451 | fprintf(out, "\n"); |
| 452 | } |
| 453 | #endif |
| 454 | |
| 455 | #ifdef SQLITE_DEBUG |
drh | b21e7c7 | 2008-06-22 12:37:57 +0000 | [diff] [blame] | 456 | # define REGISTER_TRACE(R,M) if(p->trace)registerTrace(p->trace,R,M) |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 457 | #else |
| 458 | # define REGISTER_TRACE(R,M) |
| 459 | #endif |
| 460 | |
danielk1977 | 84ac9d0 | 2004-05-18 09:58:06 +0000 | [diff] [blame] | 461 | |
drh | 7b39686 | 2003-01-01 23:06:20 +0000 | [diff] [blame] | 462 | #ifdef VDBE_PROFILE |
shane | 9bcbdad | 2008-05-29 20:22:37 +0000 | [diff] [blame] | 463 | |
| 464 | /* |
| 465 | ** hwtime.h contains inline assembler code for implementing |
| 466 | ** high-performance timing routines. |
drh | 7b39686 | 2003-01-01 23:06:20 +0000 | [diff] [blame] | 467 | */ |
shane | 9bcbdad | 2008-05-29 20:22:37 +0000 | [diff] [blame] | 468 | #include "hwtime.h" |
| 469 | |
drh | 7b39686 | 2003-01-01 23:06:20 +0000 | [diff] [blame] | 470 | #endif |
| 471 | |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 472 | /* |
drh | caec2f1 | 2003-01-07 02:47:47 +0000 | [diff] [blame] | 473 | ** The CHECK_FOR_INTERRUPT macro defined here looks to see if the |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 474 | ** sqlite3_interrupt() routine has been called. If it has been, then |
drh | caec2f1 | 2003-01-07 02:47:47 +0000 | [diff] [blame] | 475 | ** processing of the VDBE program is interrupted. |
| 476 | ** |
| 477 | ** This macro added to every instruction that does a jump in order to |
| 478 | ** implement a loop. This test used to be on every single instruction, |
| 479 | ** but that meant we more testing that we needed. By only testing the |
| 480 | ** flag on jump instructions, we get a (small) speed improvement. |
| 481 | */ |
| 482 | #define CHECK_FOR_INTERRUPT \ |
drh | 881feaa | 2006-07-26 01:39:30 +0000 | [diff] [blame] | 483 | if( db->u1.isInterrupted ) goto abort_due_to_interrupt; |
drh | caec2f1 | 2003-01-07 02:47:47 +0000 | [diff] [blame] | 484 | |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 485 | #ifdef SQLITE_DEBUG |
| 486 | static int fileExists(sqlite3 *db, const char *zFile){ |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame] | 487 | int res = 0; |
| 488 | int rc = SQLITE_OK; |
| 489 | #ifdef SQLITE_TEST |
| 490 | /* If we are currently testing IO errors, then do not call OsAccess() to |
| 491 | ** test for the presence of zFile. This is because any IO error that |
| 492 | ** occurs here will not be reported, causing the test to fail. |
| 493 | */ |
| 494 | extern int sqlite3_io_error_pending; |
| 495 | if( sqlite3_io_error_pending<=0 ) |
| 496 | #endif |
| 497 | rc = sqlite3OsAccess(db->pVfs, zFile, SQLITE_ACCESS_EXISTS, &res); |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 498 | return (res && rc==SQLITE_OK); |
| 499 | } |
| 500 | #endif |
drh | caec2f1 | 2003-01-07 02:47:47 +0000 | [diff] [blame] | 501 | |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 502 | #ifndef NDEBUG |
| 503 | /* |
| 504 | ** This function is only called from within an assert() expression. It |
| 505 | ** checks that the sqlite3.nTransaction variable is correctly set to |
| 506 | ** the number of non-transaction savepoints currently in the |
| 507 | ** linked list starting at sqlite3.pSavepoint. |
| 508 | ** |
| 509 | ** Usage: |
| 510 | ** |
| 511 | ** assert( checkSavepointCount(db) ); |
| 512 | */ |
| 513 | static int checkSavepointCount(sqlite3 *db){ |
| 514 | int n = 0; |
| 515 | Savepoint *p; |
| 516 | for(p=db->pSavepoint; p; p=p->pNext) n++; |
| 517 | assert( n==(db->nSavepoint + db->isTransactionSavepoint) ); |
| 518 | return 1; |
| 519 | } |
| 520 | #endif |
| 521 | |
drh | caec2f1 | 2003-01-07 02:47:47 +0000 | [diff] [blame] | 522 | /* |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 523 | ** Execute as much of a VDBE program as we can then return. |
| 524 | ** |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 525 | ** sqlite3VdbeMakeReady() must be called before this routine in order to |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 526 | ** close the program with a final OP_Halt and to set up the callbacks |
| 527 | ** and the error message pointer. |
| 528 | ** |
| 529 | ** Whenever a row or result data is available, this routine will either |
| 530 | ** invoke the result callback (if there is one) or return with |
drh | 326dce7 | 2003-01-29 14:06:07 +0000 | [diff] [blame] | 531 | ** SQLITE_ROW. |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 532 | ** |
| 533 | ** If an attempt is made to open a locked database, then this routine |
| 534 | ** will either invoke the busy callback (if there is one) or it will |
| 535 | ** return SQLITE_BUSY. |
| 536 | ** |
| 537 | ** If an error occurs, an error message is written to memory obtained |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 538 | ** from sqlite3_malloc() and p->zErrMsg is made to point to that memory. |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 539 | ** The error code is stored in p->rc and this routine returns SQLITE_ERROR. |
| 540 | ** |
| 541 | ** If the callback ever returns non-zero, then the program exits |
| 542 | ** immediately. There will be no error message but the p->rc field is |
| 543 | ** set to SQLITE_ABORT and this routine will return SQLITE_ERROR. |
| 544 | ** |
drh | 9468c7f | 2003-03-07 19:50:07 +0000 | [diff] [blame] | 545 | ** A memory allocation error causes p->rc to be set to SQLITE_NOMEM and this |
| 546 | ** routine to return SQLITE_ERROR. |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 547 | ** |
| 548 | ** Other fatal errors return SQLITE_ERROR. |
| 549 | ** |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 550 | ** After this routine has finished, sqlite3VdbeFinalize() should be |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 551 | ** used to clean up the mess that was left behind. |
| 552 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 553 | int sqlite3VdbeExec( |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 554 | Vdbe *p /* The VDBE */ |
| 555 | ){ |
| 556 | int pc; /* The program counter */ |
| 557 | Op *pOp; /* Current operation */ |
| 558 | int rc = SQLITE_OK; /* Value to return */ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 559 | sqlite3 *db = p->db; /* The database */ |
drh | 8079a0d | 2006-01-12 17:20:50 +0000 | [diff] [blame] | 560 | u8 encoding = ENC(db); /* The database encoding */ |
drh | b27b7f5 | 2008-12-10 18:03:45 +0000 | [diff] [blame] | 561 | Mem *pIn1 = 0; /* 1st input operand */ |
| 562 | Mem *pIn2 = 0; /* 2nd input operand */ |
| 563 | Mem *pIn3 = 0; /* 3rd input operand */ |
| 564 | Mem *pOut = 0; /* Output operand */ |
drh | b1fdb2a | 2008-01-05 04:06:03 +0000 | [diff] [blame] | 565 | u8 opProperty; |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 566 | int iCompare = 0; /* Result of last OP_Compare operation */ |
| 567 | int *aPermute = 0; /* Permuation of columns for OP_Compare */ |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 568 | #ifdef VDBE_PROFILE |
shane | 9bcbdad | 2008-05-29 20:22:37 +0000 | [diff] [blame] | 569 | u64 start; /* CPU clock count at start of opcode */ |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 570 | int origPc; /* Program counter at start of opcode */ |
| 571 | #endif |
danielk1977 | 348bb5d | 2003-10-18 09:37:26 +0000 | [diff] [blame] | 572 | #ifndef SQLITE_OMIT_PROGRESS_CALLBACK |
| 573 | int nProgressOps = 0; /* Opcodes executed since progress callback. */ |
| 574 | #endif |
drh | 23f79d0 | 2008-08-20 22:06:47 +0000 | [diff] [blame] | 575 | UnpackedRecord aTempRec[16]; /* Space to hold a transient UnpackedRecord */ |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 576 | |
drh | ca48c90 | 2008-01-18 14:08:24 +0000 | [diff] [blame] | 577 | assert( p->magic==VDBE_MAGIC_RUN ); /* sqlite3_step() verifies this */ |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 578 | assert( db->magic==SQLITE_MAGIC_BUSY ); |
drh | 4cf7c7f | 2007-08-28 23:28:07 +0000 | [diff] [blame] | 579 | sqlite3BtreeMutexArrayEnter(&p->aMutex); |
danielk1977 | 2e588c7 | 2005-12-09 14:25:08 +0000 | [diff] [blame] | 580 | if( p->rc==SQLITE_NOMEM ){ |
| 581 | /* This happens if a malloc() inside a call to sqlite3_column_text() or |
| 582 | ** sqlite3_column_text16() failed. */ |
| 583 | goto no_mem; |
| 584 | } |
drh | 3a84069 | 2003-01-29 22:58:26 +0000 | [diff] [blame] | 585 | assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY ); |
| 586 | p->rc = SQLITE_OK; |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 587 | assert( p->explain==0 ); |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 588 | p->pResultSet = 0; |
drh | a4afb65 | 2005-07-09 02:16:02 +0000 | [diff] [blame] | 589 | db->busyHandler.nBusy = 0; |
drh | 9358164 | 2004-02-12 13:02:55 +0000 | [diff] [blame] | 590 | CHECK_FOR_INTERRUPT; |
drh | 602c237 | 2007-03-01 00:29:13 +0000 | [diff] [blame] | 591 | sqlite3VdbeIOTraceSql(p); |
drh | 3c23a88 | 2007-01-09 14:01:13 +0000 | [diff] [blame] | 592 | #ifdef SQLITE_DEBUG |
danielk1977 | 2d1d86f | 2008-06-20 14:59:51 +0000 | [diff] [blame] | 593 | sqlite3BeginBenignMalloc(); |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 594 | if( p->pc==0 |
| 595 | && ((p->db->flags & SQLITE_VdbeListing) || fileExists(db, "vdbe_explain")) |
drh | 3c23a88 | 2007-01-09 14:01:13 +0000 | [diff] [blame] | 596 | ){ |
| 597 | int i; |
| 598 | printf("VDBE Program Listing:\n"); |
| 599 | sqlite3VdbePrintSql(p); |
| 600 | for(i=0; i<p->nOp; i++){ |
| 601 | sqlite3VdbePrintOp(stdout, i, &p->aOp[i]); |
| 602 | } |
| 603 | } |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 604 | if( fileExists(db, "vdbe_trace") ){ |
drh | 3c23a88 | 2007-01-09 14:01:13 +0000 | [diff] [blame] | 605 | p->trace = stdout; |
| 606 | } |
danielk1977 | 2d1d86f | 2008-06-20 14:59:51 +0000 | [diff] [blame] | 607 | sqlite3EndBenignMalloc(); |
drh | 3c23a88 | 2007-01-09 14:01:13 +0000 | [diff] [blame] | 608 | #endif |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 609 | for(pc=p->pc; rc==SQLITE_OK; pc++){ |
drh | caec2f1 | 2003-01-07 02:47:47 +0000 | [diff] [blame] | 610 | assert( pc>=0 && pc<p->nOp ); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 611 | if( db->mallocFailed ) goto no_mem; |
drh | 7b39686 | 2003-01-01 23:06:20 +0000 | [diff] [blame] | 612 | #ifdef VDBE_PROFILE |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 613 | origPc = pc; |
shane | 9bcbdad | 2008-05-29 20:22:37 +0000 | [diff] [blame] | 614 | start = sqlite3Hwtime(); |
drh | 7b39686 | 2003-01-01 23:06:20 +0000 | [diff] [blame] | 615 | #endif |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 616 | pOp = &p->aOp[pc]; |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 617 | |
danielk1977 | 8b60e0f | 2005-01-12 09:10:39 +0000 | [diff] [blame] | 618 | /* Only allow tracing if SQLITE_DEBUG is defined. |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 619 | */ |
danielk1977 | 8b60e0f | 2005-01-12 09:10:39 +0000 | [diff] [blame] | 620 | #ifdef SQLITE_DEBUG |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 621 | if( p->trace ){ |
drh | 3f7d4e4 | 2004-07-24 14:35:58 +0000 | [diff] [blame] | 622 | if( pc==0 ){ |
| 623 | printf("VDBE Execution Trace:\n"); |
| 624 | sqlite3VdbePrintSql(p); |
| 625 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 626 | sqlite3VdbePrintOp(p->trace, pc, pOp); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 627 | } |
drh | 19db935 | 2008-03-27 22:42:51 +0000 | [diff] [blame] | 628 | if( p->trace==0 && pc==0 ){ |
danielk1977 | 2d1d86f | 2008-06-20 14:59:51 +0000 | [diff] [blame] | 629 | sqlite3BeginBenignMalloc(); |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 630 | if( fileExists(db, "vdbe_sqltrace") ){ |
drh | 19db935 | 2008-03-27 22:42:51 +0000 | [diff] [blame] | 631 | sqlite3VdbePrintSql(p); |
| 632 | } |
danielk1977 | 2d1d86f | 2008-06-20 14:59:51 +0000 | [diff] [blame] | 633 | sqlite3EndBenignMalloc(); |
drh | 3f7d4e4 | 2004-07-24 14:35:58 +0000 | [diff] [blame] | 634 | } |
| 635 | #endif |
| 636 | |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 637 | |
drh | f603871 | 2004-02-08 18:07:34 +0000 | [diff] [blame] | 638 | /* Check to see if we need to simulate an interrupt. This only happens |
| 639 | ** if we have a special test build. |
| 640 | */ |
| 641 | #ifdef SQLITE_TEST |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 642 | if( sqlite3_interrupt_count>0 ){ |
| 643 | sqlite3_interrupt_count--; |
| 644 | if( sqlite3_interrupt_count==0 ){ |
| 645 | sqlite3_interrupt(db); |
drh | f603871 | 2004-02-08 18:07:34 +0000 | [diff] [blame] | 646 | } |
| 647 | } |
| 648 | #endif |
| 649 | |
danielk1977 | 348bb5d | 2003-10-18 09:37:26 +0000 | [diff] [blame] | 650 | #ifndef SQLITE_OMIT_PROGRESS_CALLBACK |
| 651 | /* Call the progress callback if it is configured and the required number |
| 652 | ** of VDBE ops have been executed (either since this invocation of |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 653 | ** sqlite3VdbeExec() or since last time the progress callback was called). |
danielk1977 | 348bb5d | 2003-10-18 09:37:26 +0000 | [diff] [blame] | 654 | ** If the progress callback returns non-zero, exit the virtual machine with |
| 655 | ** a return code SQLITE_ABORT. |
| 656 | */ |
drh | 3914aed | 2004-01-31 20:40:42 +0000 | [diff] [blame] | 657 | if( db->xProgress ){ |
| 658 | if( db->nProgressOps==nProgressOps ){ |
danielk1977 | de523ac | 2007-06-15 14:53:53 +0000 | [diff] [blame] | 659 | int prc; |
drh | f8888bb | 2006-05-26 19:57:19 +0000 | [diff] [blame] | 660 | if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse; |
danielk1977 | de523ac | 2007-06-15 14:53:53 +0000 | [diff] [blame] | 661 | prc =db->xProgress(db->pProgressArg); |
drh | f8888bb | 2006-05-26 19:57:19 +0000 | [diff] [blame] | 662 | if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse; |
danielk1977 | de523ac | 2007-06-15 14:53:53 +0000 | [diff] [blame] | 663 | if( prc!=0 ){ |
| 664 | rc = SQLITE_INTERRUPT; |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 665 | goto vdbe_error_halt; |
danielk1977 | de523ac | 2007-06-15 14:53:53 +0000 | [diff] [blame] | 666 | } |
danielk1977 | 3fe11f3 | 2007-06-13 16:49:48 +0000 | [diff] [blame] | 667 | nProgressOps = 0; |
danielk1977 | 348bb5d | 2003-10-18 09:37:26 +0000 | [diff] [blame] | 668 | } |
drh | 3914aed | 2004-01-31 20:40:42 +0000 | [diff] [blame] | 669 | nProgressOps++; |
danielk1977 | 348bb5d | 2003-10-18 09:37:26 +0000 | [diff] [blame] | 670 | } |
danielk1977 | 348bb5d | 2003-10-18 09:37:26 +0000 | [diff] [blame] | 671 | #endif |
| 672 | |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 673 | /* Do common setup processing for any opcode that is marked |
| 674 | ** with the "out2-prerelease" tag. Such opcodes have a single |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 675 | ** output which is specified by the P2 parameter. The P2 register |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 676 | ** is initialized to a NULL. |
| 677 | */ |
drh | b1fdb2a | 2008-01-05 04:06:03 +0000 | [diff] [blame] | 678 | opProperty = opcodeProperty[pOp->opcode]; |
| 679 | if( (opProperty & OPFLG_OUT2_PRERELEASE)!=0 ){ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 680 | assert( pOp->p2>0 ); |
| 681 | assert( pOp->p2<=p->nMem ); |
| 682 | pOut = &p->aMem[pOp->p2]; |
danielk1977 | 5f09613 | 2008-03-28 15:44:09 +0000 | [diff] [blame] | 683 | sqlite3VdbeMemReleaseExternal(pOut); |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 684 | pOut->flags = MEM_Null; |
drh | b1fdb2a | 2008-01-05 04:06:03 +0000 | [diff] [blame] | 685 | }else |
| 686 | |
| 687 | /* Do common setup for opcodes marked with one of the following |
| 688 | ** combinations of properties. |
| 689 | ** |
| 690 | ** in1 |
| 691 | ** in1 in2 |
| 692 | ** in1 in2 out3 |
| 693 | ** in1 in3 |
drh | b1fdb2a | 2008-01-05 04:06:03 +0000 | [diff] [blame] | 694 | ** |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 695 | ** Variables pIn1, pIn2, and pIn3 are made to point to appropriate |
| 696 | ** registers for inputs. Variable pOut points to the output register. |
drh | b1fdb2a | 2008-01-05 04:06:03 +0000 | [diff] [blame] | 697 | */ |
| 698 | if( (opProperty & OPFLG_IN1)!=0 ){ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 699 | assert( pOp->p1>0 ); |
| 700 | assert( pOp->p1<=p->nMem ); |
| 701 | pIn1 = &p->aMem[pOp->p1]; |
| 702 | REGISTER_TRACE(pOp->p1, pIn1); |
drh | b1fdb2a | 2008-01-05 04:06:03 +0000 | [diff] [blame] | 703 | if( (opProperty & OPFLG_IN2)!=0 ){ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 704 | assert( pOp->p2>0 ); |
drh | aa9b896 | 2008-01-08 02:57:55 +0000 | [diff] [blame] | 705 | assert( pOp->p2<=p->nMem ); |
| 706 | pIn2 = &p->aMem[pOp->p2]; |
| 707 | REGISTER_TRACE(pOp->p2, pIn2); |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 708 | if( (opProperty & OPFLG_OUT3)!=0 ){ |
| 709 | assert( pOp->p3>0 ); |
| 710 | assert( pOp->p3<=p->nMem ); |
| 711 | pOut = &p->aMem[pOp->p3]; |
| 712 | } |
| 713 | }else if( (opProperty & OPFLG_IN3)!=0 ){ |
| 714 | assert( pOp->p3>0 ); |
drh | aa9b896 | 2008-01-08 02:57:55 +0000 | [diff] [blame] | 715 | assert( pOp->p3<=p->nMem ); |
| 716 | pIn3 = &p->aMem[pOp->p3]; |
| 717 | REGISTER_TRACE(pOp->p3, pIn3); |
| 718 | } |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 719 | }else if( (opProperty & OPFLG_IN2)!=0 ){ |
| 720 | assert( pOp->p2>0 ); |
| 721 | assert( pOp->p2<=p->nMem ); |
| 722 | pIn2 = &p->aMem[pOp->p2]; |
| 723 | REGISTER_TRACE(pOp->p2, pIn2); |
| 724 | }else if( (opProperty & OPFLG_IN3)!=0 ){ |
| 725 | assert( pOp->p3>0 ); |
| 726 | assert( pOp->p3<=p->nMem ); |
| 727 | pIn3 = &p->aMem[pOp->p3]; |
| 728 | REGISTER_TRACE(pOp->p3, pIn3); |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 729 | } |
| 730 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 731 | switch( pOp->opcode ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 732 | |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 733 | /***************************************************************************** |
| 734 | ** What follows is a massive switch statement where each case implements a |
| 735 | ** separate instruction in the virtual machine. If we follow the usual |
| 736 | ** indentation conventions, each case should be indented by 6 spaces. But |
| 737 | ** that is a lot of wasted space on the left margin. So the code within |
| 738 | ** the switch statement will break with convention and be flush-left. Another |
| 739 | ** big comment (similar to this one) will mark the point in the code where |
| 740 | ** we transition back to normal indentation. |
drh | ac82fcf | 2002-09-08 17:23:41 +0000 | [diff] [blame] | 741 | ** |
| 742 | ** The formatting of each case is important. The makefile for SQLite |
| 743 | ** generates two C files "opcodes.h" and "opcodes.c" by scanning this |
| 744 | ** file looking for lines that begin with "case OP_". The opcodes.h files |
| 745 | ** will be filled with #defines that give unique integer values to each |
| 746 | ** 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] | 747 | ** each string is the symbolic name for the corresponding opcode. If the |
| 748 | ** case statement is followed by a comment of the form "/# same as ... #/" |
| 749 | ** that comment is used to determine the particular value of the opcode. |
drh | ac82fcf | 2002-09-08 17:23:41 +0000 | [diff] [blame] | 750 | ** |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 751 | ** Other keywords in the comment that follows each case are used to |
| 752 | ** construct the OPFLG_INITIALIZER value that initializes opcodeProperty[]. |
| 753 | ** Keywords include: in1, in2, in3, out2_prerelease, out2, out3. See |
| 754 | ** the mkopcodeh.awk script for additional information. |
danielk1977 | bc04f85 | 2005-03-29 08:26:13 +0000 | [diff] [blame] | 755 | ** |
drh | ac82fcf | 2002-09-08 17:23:41 +0000 | [diff] [blame] | 756 | ** Documentation about VDBE opcodes is generated by scanning this file |
| 757 | ** for lines of that contain "Opcode:". That line and all subsequent |
| 758 | ** comment lines are used in the generation of the opcode.html documentation |
| 759 | ** file. |
| 760 | ** |
| 761 | ** SUMMARY: |
| 762 | ** |
| 763 | ** Formatting is important to scripts that scan this file. |
| 764 | ** Do not deviate from the formatting style currently in use. |
| 765 | ** |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 766 | *****************************************************************************/ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 767 | |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 768 | /* Opcode: Goto * P2 * * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 769 | ** |
| 770 | ** An unconditional jump to address P2. |
| 771 | ** The next instruction executed will be |
| 772 | ** the one at index P2 from the beginning of |
| 773 | ** the program. |
| 774 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 775 | case OP_Goto: { /* jump */ |
drh | caec2f1 | 2003-01-07 02:47:47 +0000 | [diff] [blame] | 776 | CHECK_FOR_INTERRUPT; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 777 | pc = pOp->p2 - 1; |
| 778 | break; |
| 779 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 780 | |
drh | 2eb9537 | 2008-06-06 15:04:36 +0000 | [diff] [blame] | 781 | /* Opcode: Gosub P1 P2 * * * |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 782 | ** |
drh | 2eb9537 | 2008-06-06 15:04:36 +0000 | [diff] [blame] | 783 | ** Write the current address onto register P1 |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 784 | ** and then jump to address P2. |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 785 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 786 | case OP_Gosub: { /* jump */ |
drh | 2eb9537 | 2008-06-06 15:04:36 +0000 | [diff] [blame] | 787 | assert( pOp->p1>0 ); |
| 788 | assert( pOp->p1<=p->nMem ); |
| 789 | pIn1 = &p->aMem[pOp->p1]; |
| 790 | assert( (pIn1->flags & MEM_Dyn)==0 ); |
| 791 | pIn1->flags = MEM_Int; |
| 792 | pIn1->u.i = pc; |
| 793 | REGISTER_TRACE(pOp->p1, pIn1); |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 794 | pc = pOp->p2 - 1; |
| 795 | break; |
| 796 | } |
| 797 | |
drh | 2eb9537 | 2008-06-06 15:04:36 +0000 | [diff] [blame] | 798 | /* Opcode: Return P1 * * * * |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 799 | ** |
drh | 2eb9537 | 2008-06-06 15:04:36 +0000 | [diff] [blame] | 800 | ** Jump to the next instruction after the address in register P1. |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 801 | */ |
drh | 2eb9537 | 2008-06-06 15:04:36 +0000 | [diff] [blame] | 802 | case OP_Return: { /* in1 */ |
| 803 | assert( pIn1->flags & MEM_Int ); |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 804 | pc = (int)pIn1->u.i; |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 805 | break; |
| 806 | } |
| 807 | |
drh | e00ee6e | 2008-06-20 15:24:01 +0000 | [diff] [blame] | 808 | /* Opcode: Yield P1 * * * * |
| 809 | ** |
| 810 | ** Swap the program counter with the value in register P1. |
| 811 | */ |
danielk1977 | f73ab8b | 2008-12-29 10:39:53 +0000 | [diff] [blame^] | 812 | case OP_Yield: { /* in1 */ |
drh | e00ee6e | 2008-06-20 15:24:01 +0000 | [diff] [blame] | 813 | int pcDest; |
drh | e00ee6e | 2008-06-20 15:24:01 +0000 | [diff] [blame] | 814 | assert( (pIn1->flags & MEM_Dyn)==0 ); |
| 815 | pIn1->flags = MEM_Int; |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 816 | pcDest = (int)pIn1->u.i; |
drh | e00ee6e | 2008-06-20 15:24:01 +0000 | [diff] [blame] | 817 | pIn1->u.i = pc; |
| 818 | REGISTER_TRACE(pOp->p1, pIn1); |
| 819 | pc = pcDest; |
| 820 | break; |
| 821 | } |
| 822 | |
| 823 | |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 824 | /* Opcode: Halt P1 P2 * P4 * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 825 | ** |
drh | 3d4501e | 2008-12-04 20:40:10 +0000 | [diff] [blame] | 826 | ** Exit immediately. All open cursors, etc are closed |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 827 | ** automatically. |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 828 | ** |
drh | 92f02c3 | 2004-09-02 14:57:08 +0000 | [diff] [blame] | 829 | ** P1 is the result code returned by sqlite3_exec(), sqlite3_reset(), |
| 830 | ** or sqlite3_finalize(). For a normal halt, this should be SQLITE_OK (0). |
| 831 | ** For errors, it can be some other value. If P1!=0 then P2 will determine |
| 832 | ** whether or not to rollback the current transaction. Do not rollback |
| 833 | ** if P2==OE_Fail. Do the rollback if P2==OE_Rollback. If P2==OE_Abort, |
| 834 | ** then back out all changes that have occurred during this execution of the |
drh | b798fa6 | 2002-09-03 19:43:23 +0000 | [diff] [blame] | 835 | ** VDBE, but do not rollback the transaction. |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 836 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 837 | ** If P4 is not null then it is an error message string. |
drh | 7f057c9 | 2005-06-24 03:53:06 +0000 | [diff] [blame] | 838 | ** |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 839 | ** 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] | 840 | ** every program. So a jump past the last instruction of the program |
| 841 | ** is the same as executing Halt. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 842 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 843 | case OP_Halt: { |
drh | 92f02c3 | 2004-09-02 14:57:08 +0000 | [diff] [blame] | 844 | p->rc = pOp->p1; |
| 845 | p->pc = pc; |
| 846 | p->errorAction = pOp->p2; |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 847 | if( pOp->p4.z ){ |
drh | f089aa4 | 2008-07-08 19:34:06 +0000 | [diff] [blame] | 848 | sqlite3SetString(&p->zErrMsg, db, "%s", pOp->p4.z); |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 849 | } |
drh | 92f02c3 | 2004-09-02 14:57:08 +0000 | [diff] [blame] | 850 | rc = sqlite3VdbeHalt(p); |
danielk1977 | 01427a6 | 2005-01-11 13:02:33 +0000 | [diff] [blame] | 851 | assert( rc==SQLITE_BUSY || rc==SQLITE_OK ); |
drh | 92f02c3 | 2004-09-02 14:57:08 +0000 | [diff] [blame] | 852 | if( rc==SQLITE_BUSY ){ |
drh | 900b31e | 2007-08-28 02:27:51 +0000 | [diff] [blame] | 853 | p->rc = rc = SQLITE_BUSY; |
| 854 | }else{ |
| 855 | rc = p->rc ? SQLITE_ERROR : SQLITE_DONE; |
drh | 92f02c3 | 2004-09-02 14:57:08 +0000 | [diff] [blame] | 856 | } |
drh | 900b31e | 2007-08-28 02:27:51 +0000 | [diff] [blame] | 857 | goto vdbe_return; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 858 | } |
drh | c61053b | 2000-06-04 12:58:36 +0000 | [diff] [blame] | 859 | |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 860 | /* Opcode: Integer P1 P2 * * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 861 | ** |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 862 | ** The 32-bit integer value P1 is written into register P2. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 863 | */ |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 864 | case OP_Integer: { /* out2-prerelease */ |
| 865 | pOut->flags = MEM_Int; |
| 866 | pOut->u.i = pOp->p1; |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 867 | break; |
| 868 | } |
| 869 | |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 870 | /* Opcode: Int64 * P2 * P4 * |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 871 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 872 | ** P4 is a pointer to a 64-bit integer value. |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 873 | ** Write that value into register P2. |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 874 | */ |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 875 | case OP_Int64: { /* out2-prerelease */ |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 876 | assert( pOp->p4.pI64!=0 ); |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 877 | pOut->flags = MEM_Int; |
| 878 | pOut->u.i = *pOp->p4.pI64; |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 879 | break; |
| 880 | } |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 881 | |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 882 | /* Opcode: Real * P2 * P4 * |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 883 | ** |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 884 | ** P4 is a pointer to a 64-bit floating point value. |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 885 | ** Write that value into register P2. |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 886 | */ |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 887 | case OP_Real: { /* same as TK_FLOAT, out2-prerelease */ |
| 888 | pOut->flags = MEM_Real; |
drh | 2eaf93d | 2008-04-29 00:15:20 +0000 | [diff] [blame] | 889 | assert( !sqlite3IsNaN(*pOp->p4.pReal) ); |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 890 | pOut->r = *pOp->p4.pReal; |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 891 | break; |
| 892 | } |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 893 | |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 894 | /* Opcode: String8 * P2 * P4 * |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 895 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 896 | ** P4 points to a nul terminated UTF-8 string. This opcode is transformed |
danielk1977 | 0f69c1e | 2004-05-29 11:24:50 +0000 | [diff] [blame] | 897 | ** into an OP_String before it is executed for the first time. |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 898 | */ |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 899 | case OP_String8: { /* same as TK_STRING, out2-prerelease */ |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 900 | assert( pOp->p4.z!=0 ); |
drh | ed2df7f | 2005-11-16 04:34:32 +0000 | [diff] [blame] | 901 | pOp->opcode = OP_String; |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 902 | pOp->p1 = sqlite3Strlen30(pOp->p4.z); |
drh | ed2df7f | 2005-11-16 04:34:32 +0000 | [diff] [blame] | 903 | |
| 904 | #ifndef SQLITE_OMIT_UTF16 |
drh | 8079a0d | 2006-01-12 17:20:50 +0000 | [diff] [blame] | 905 | if( encoding!=SQLITE_UTF8 ){ |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 906 | sqlite3VdbeMemSetStr(pOut, pOp->p4.z, -1, SQLITE_UTF8, SQLITE_STATIC); |
| 907 | if( SQLITE_OK!=sqlite3VdbeChangeEncoding(pOut, encoding) ) goto no_mem; |
drh | dab898f | 2008-07-30 13:14:55 +0000 | [diff] [blame] | 908 | if( SQLITE_OK!=sqlite3VdbeMemMakeWriteable(pOut) ) goto no_mem; |
danielk1977 | 5f09613 | 2008-03-28 15:44:09 +0000 | [diff] [blame] | 909 | pOut->zMalloc = 0; |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 910 | pOut->flags |= MEM_Static; |
drh | 191b54c | 2008-04-15 12:14:21 +0000 | [diff] [blame] | 911 | pOut->flags &= ~MEM_Dyn; |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 912 | if( pOp->p4type==P4_DYNAMIC ){ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 913 | sqlite3DbFree(db, pOp->p4.z); |
danielk1977 | e004840 | 2004-06-15 16:51:01 +0000 | [diff] [blame] | 914 | } |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 915 | pOp->p4type = P4_DYNAMIC; |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 916 | pOp->p4.z = pOut->z; |
| 917 | pOp->p1 = pOut->n; |
drh | bb4957f | 2008-03-20 14:03:29 +0000 | [diff] [blame] | 918 | if( pOp->p1>db->aLimit[SQLITE_LIMIT_LENGTH] ){ |
drh | cbd2da9 | 2007-12-17 16:20:06 +0000 | [diff] [blame] | 919 | goto too_big; |
| 920 | } |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 921 | UPDATE_MAX_BLOBSIZE(pOut); |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame] | 922 | break; |
danielk1977 | 0f69c1e | 2004-05-29 11:24:50 +0000 | [diff] [blame] | 923 | } |
danielk1977 | 93758c8 | 2005-01-21 08:13:14 +0000 | [diff] [blame] | 924 | #endif |
drh | bb4957f | 2008-03-20 14:03:29 +0000 | [diff] [blame] | 925 | if( pOp->p1>db->aLimit[SQLITE_LIMIT_LENGTH] ){ |
drh | cbd2da9 | 2007-12-17 16:20:06 +0000 | [diff] [blame] | 926 | goto too_big; |
| 927 | } |
| 928 | /* Fall through to the next case, OP_String */ |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 929 | } |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 930 | |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 931 | /* Opcode: String P1 P2 * P4 * |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 932 | ** |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 933 | ** The string value P4 of length P1 (bytes) is stored in register P2. |
drh | f447950 | 2004-05-27 03:12:53 +0000 | [diff] [blame] | 934 | */ |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 935 | case OP_String: { /* out2-prerelease */ |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 936 | assert( pOp->p4.z!=0 ); |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 937 | pOut->flags = MEM_Str|MEM_Static|MEM_Term; |
| 938 | pOut->z = pOp->p4.z; |
| 939 | pOut->n = pOp->p1; |
| 940 | pOut->enc = encoding; |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 941 | UPDATE_MAX_BLOBSIZE(pOut); |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 942 | break; |
| 943 | } |
| 944 | |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 945 | /* Opcode: Null * P2 * * * |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 946 | ** |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 947 | ** Write a NULL into register P2. |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 948 | */ |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 949 | case OP_Null: { /* out2-prerelease */ |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 950 | break; |
| 951 | } |
| 952 | |
| 953 | |
drh | 9de221d | 2008-01-05 06:51:30 +0000 | [diff] [blame] | 954 | /* Opcode: Blob P1 P2 * P4 |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 955 | ** |
drh | 9de221d | 2008-01-05 06:51:30 +0000 | [diff] [blame] | 956 | ** P4 points to a blob of data P1 bytes long. Store this |
| 957 | ** blob in register P2. This instruction is not coded directly |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 958 | ** by the compiler. Instead, the compiler layer specifies |
| 959 | ** an OP_HexBlob opcode, with the hex string representation of |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 960 | ** the blob as P4. This opcode is transformed to an OP_Blob |
danielk1977 | 93758c8 | 2005-01-21 08:13:14 +0000 | [diff] [blame] | 961 | ** the first time it is executed. |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 962 | */ |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 963 | case OP_Blob: { /* out2-prerelease */ |
drh | cbd2da9 | 2007-12-17 16:20:06 +0000 | [diff] [blame] | 964 | assert( pOp->p1 <= SQLITE_MAX_LENGTH ); |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 965 | sqlite3VdbeMemSetStr(pOut, pOp->p4.z, pOp->p1, 0, 0); |
drh | 9de221d | 2008-01-05 06:51:30 +0000 | [diff] [blame] | 966 | pOut->enc = encoding; |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 967 | UPDATE_MAX_BLOBSIZE(pOut); |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 968 | break; |
| 969 | } |
| 970 | |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 971 | /* Opcode: Variable P1 P2 * * * |
drh | 5045789 | 2003-09-06 01:10:47 +0000 | [diff] [blame] | 972 | ** |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 973 | ** The value of variable P1 is written into register P2. A variable is |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 974 | ** an unknown in the original SQL string as handed to sqlite3_compile(). |
shane | 21e7feb | 2008-05-30 15:59:49 +0000 | [diff] [blame] | 975 | ** Any occurrence of the '?' character in the original SQL is considered |
drh | 7c972de | 2003-09-06 22:18:07 +0000 | [diff] [blame] | 976 | ** a variable. Variables in the SQL string are number from left to |
| 977 | ** right beginning with 1. The values of variables are set using the |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 978 | ** sqlite3_bind() API. |
drh | 5045789 | 2003-09-06 01:10:47 +0000 | [diff] [blame] | 979 | */ |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 980 | case OP_Variable: { /* out2-prerelease */ |
drh | 7c972de | 2003-09-06 22:18:07 +0000 | [diff] [blame] | 981 | int j = pOp->p1 - 1; |
drh | 023ae03 | 2007-05-08 12:12:16 +0000 | [diff] [blame] | 982 | Mem *pVar; |
danielk1977 | 295ba55 | 2004-05-19 10:34:51 +0000 | [diff] [blame] | 983 | assert( j>=0 && j<p->nVar ); |
| 984 | |
drh | 023ae03 | 2007-05-08 12:12:16 +0000 | [diff] [blame] | 985 | pVar = &p->aVar[j]; |
| 986 | if( sqlite3VdbeMemTooBig(pVar) ){ |
| 987 | goto too_big; |
| 988 | } |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 989 | sqlite3VdbeMemShallowCopy(pOut, &p->aVar[j], MEM_Static); |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 990 | UPDATE_MAX_BLOBSIZE(pOut); |
danielk1977 | 93d4675 | 2004-05-23 13:30:58 +0000 | [diff] [blame] | 991 | break; |
| 992 | } |
danielk1977 | 295ba55 | 2004-05-19 10:34:51 +0000 | [diff] [blame] | 993 | |
drh | b21e7c7 | 2008-06-22 12:37:57 +0000 | [diff] [blame] | 994 | /* Opcode: Move P1 P2 P3 * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 995 | ** |
drh | b21e7c7 | 2008-06-22 12:37:57 +0000 | [diff] [blame] | 996 | ** Move the values in register P1..P1+P3-1 over into |
| 997 | ** registers P2..P2+P3-1. Registers P1..P1+P1-1 are |
| 998 | ** left holding a NULL. It is an error for register ranges |
| 999 | ** P1..P1+P3-1 and P2..P2+P3-1 to overlap. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1000 | */ |
drh | e1349cb | 2008-04-01 00:36:10 +0000 | [diff] [blame] | 1001 | case OP_Move: { |
| 1002 | char *zMalloc; |
drh | b21e7c7 | 2008-06-22 12:37:57 +0000 | [diff] [blame] | 1003 | int n = pOp->p3; |
| 1004 | int p1 = pOp->p1; |
| 1005 | int p2 = pOp->p2; |
| 1006 | assert( n>0 ); |
| 1007 | assert( p1>0 ); |
| 1008 | assert( p1+n<p->nMem ); |
| 1009 | pIn1 = &p->aMem[p1]; |
| 1010 | assert( p2>0 ); |
| 1011 | assert( p2+n<p->nMem ); |
| 1012 | pOut = &p->aMem[p2]; |
| 1013 | assert( p1+n<=p2 || p2+n<=p1 ); |
| 1014 | while( n-- ){ |
drh | b21e7c7 | 2008-06-22 12:37:57 +0000 | [diff] [blame] | 1015 | zMalloc = pOut->zMalloc; |
| 1016 | pOut->zMalloc = 0; |
| 1017 | sqlite3VdbeMemMove(pOut, pIn1); |
| 1018 | pIn1->zMalloc = zMalloc; |
| 1019 | REGISTER_TRACE(p2++, pOut); |
| 1020 | pIn1++; |
| 1021 | pOut++; |
| 1022 | } |
drh | e1349cb | 2008-04-01 00:36:10 +0000 | [diff] [blame] | 1023 | break; |
| 1024 | } |
| 1025 | |
drh | b1fdb2a | 2008-01-05 04:06:03 +0000 | [diff] [blame] | 1026 | /* Opcode: Copy P1 P2 * * * |
| 1027 | ** |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1028 | ** Make a copy of register P1 into register P2. |
drh | b1fdb2a | 2008-01-05 04:06:03 +0000 | [diff] [blame] | 1029 | ** |
| 1030 | ** This instruction makes a deep copy of the value. A duplicate |
| 1031 | ** is made of any string or blob constant. See also OP_SCopy. |
| 1032 | */ |
danielk1977 | f73ab8b | 2008-12-29 10:39:53 +0000 | [diff] [blame^] | 1033 | case OP_Copy: { /* in1 */ |
drh | e1349cb | 2008-04-01 00:36:10 +0000 | [diff] [blame] | 1034 | assert( pOp->p2>0 ); |
| 1035 | assert( pOp->p2<=p->nMem ); |
| 1036 | pOut = &p->aMem[pOp->p2]; |
| 1037 | assert( pOut!=pIn1 ); |
| 1038 | sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem); |
| 1039 | Deephemeralize(pOut); |
| 1040 | REGISTER_TRACE(pOp->p2, pOut); |
| 1041 | break; |
| 1042 | } |
| 1043 | |
drh | b1fdb2a | 2008-01-05 04:06:03 +0000 | [diff] [blame] | 1044 | /* Opcode: SCopy P1 P2 * * * |
| 1045 | ** |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1046 | ** Make a shallow copy of register P1 into register P2. |
drh | b1fdb2a | 2008-01-05 04:06:03 +0000 | [diff] [blame] | 1047 | ** |
| 1048 | ** This instruction makes a shallow copy of the value. If the value |
| 1049 | ** is a string or blob, then the copy is only a pointer to the |
| 1050 | ** original and hence if the original changes so will the copy. |
| 1051 | ** Worse, if the original is deallocated, the copy becomes invalid. |
| 1052 | ** Thus the program must guarantee that the original will not change |
| 1053 | ** during the lifetime of the copy. Use OP_Copy to make a complete |
| 1054 | ** copy. |
| 1055 | */ |
danielk1977 | f73ab8b | 2008-12-29 10:39:53 +0000 | [diff] [blame^] | 1056 | case OP_SCopy: { /* in1 */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1057 | REGISTER_TRACE(pOp->p1, pIn1); |
| 1058 | assert( pOp->p2>0 ); |
| 1059 | assert( pOp->p2<=p->nMem ); |
| 1060 | pOut = &p->aMem[pOp->p2]; |
drh | 2d401ab | 2008-01-10 23:50:11 +0000 | [diff] [blame] | 1061 | assert( pOut!=pIn1 ); |
drh | e1349cb | 2008-04-01 00:36:10 +0000 | [diff] [blame] | 1062 | sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem); |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1063 | REGISTER_TRACE(pOp->p2, pOut); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1064 | break; |
| 1065 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1066 | |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1067 | /* Opcode: ResultRow P1 P2 * * * |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 1068 | ** |
shane | 21e7feb | 2008-05-30 15:59:49 +0000 | [diff] [blame] | 1069 | ** The registers P1 through P1+P2-1 contain a single row of |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 1070 | ** results. This opcode causes the sqlite3_step() call to terminate |
| 1071 | ** with an SQLITE_ROW return code and it sets up the sqlite3_stmt |
| 1072 | ** structure to provide access to the top P1 values as the result |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1073 | ** row. |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 1074 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1075 | case OP_ResultRow: { |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 1076 | Mem *pMem; |
| 1077 | int i; |
| 1078 | assert( p->nResColumn==pOp->p2 ); |
drh | 0a07c10 | 2008-01-03 18:03:08 +0000 | [diff] [blame] | 1079 | assert( pOp->p1>0 ); |
| 1080 | assert( pOp->p1+pOp->p2<=p->nMem ); |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 1081 | |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 1082 | /* Invalidate all ephemeral cursor row caches */ |
| 1083 | p->cacheCtr = (p->cacheCtr + 2)|1; |
| 1084 | |
| 1085 | /* Make sure the results of the current row are \000 terminated |
shane | 21e7feb | 2008-05-30 15:59:49 +0000 | [diff] [blame] | 1086 | ** and have an assigned type. The results are de-ephemeralized as |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 1087 | ** as side effect. |
| 1088 | */ |
| 1089 | pMem = p->pResultSet = &p->aMem[pOp->p1]; |
| 1090 | for(i=0; i<pOp->p2; i++){ |
| 1091 | sqlite3VdbeMemNulTerminate(&pMem[i]); |
| 1092 | storeTypeInfo(&pMem[i], encoding); |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 1093 | REGISTER_TRACE(pOp->p1+i, &pMem[i]); |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 1094 | } |
drh | 2803969 | 2008-03-17 16:54:01 +0000 | [diff] [blame] | 1095 | if( db->mallocFailed ) goto no_mem; |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 1096 | |
| 1097 | /* Return SQLITE_ROW |
| 1098 | */ |
| 1099 | p->nCallback++; |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 1100 | p->pc = pc + 1; |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 1101 | rc = SQLITE_ROW; |
| 1102 | goto vdbe_return; |
| 1103 | } |
| 1104 | |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1105 | /* Opcode: Concat P1 P2 P3 * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1106 | ** |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1107 | ** Add the text in register P1 onto the end of the text in |
| 1108 | ** register P2 and store the result in register P3. |
| 1109 | ** 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] | 1110 | ** |
| 1111 | ** P3 = P2 || P1 |
| 1112 | ** |
| 1113 | ** It is illegal for P1 and P3 to be the same register. Sometimes, |
| 1114 | ** if P3 is the same register as P2, the implementation is able |
| 1115 | ** to avoid a memcpy(). |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1116 | */ |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1117 | case OP_Concat: { /* same as TK_CONCAT, in1, in2, out3 */ |
drh | 023ae03 | 2007-05-08 12:12:16 +0000 | [diff] [blame] | 1118 | i64 nByte; |
danielk1977 | 8a6b541 | 2004-05-24 07:04:25 +0000 | [diff] [blame] | 1119 | |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1120 | assert( pIn1!=pOut ); |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1121 | if( (pIn1->flags | pIn2->flags) & MEM_Null ){ |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1122 | sqlite3VdbeMemSetNull(pOut); |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1123 | break; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1124 | } |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1125 | ExpandBlob(pIn1); |
| 1126 | Stringify(pIn1, encoding); |
| 1127 | ExpandBlob(pIn2); |
| 1128 | Stringify(pIn2, encoding); |
| 1129 | nByte = pIn1->n + pIn2->n; |
drh | bb4957f | 2008-03-20 14:03:29 +0000 | [diff] [blame] | 1130 | if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){ |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1131 | goto too_big; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1132 | } |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1133 | MemSetTypeFlag(pOut, MEM_Str); |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 1134 | if( sqlite3VdbeMemGrow(pOut, (int)nByte+2, pOut==pIn2) ){ |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1135 | goto no_mem; |
| 1136 | } |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1137 | if( pOut!=pIn2 ){ |
| 1138 | memcpy(pOut->z, pIn2->z, pIn2->n); |
| 1139 | } |
| 1140 | memcpy(&pOut->z[pIn2->n], pIn1->z, pIn1->n); |
| 1141 | pOut->z[nByte] = 0; |
| 1142 | pOut->z[nByte+1] = 0; |
| 1143 | pOut->flags |= MEM_Term; |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 1144 | pOut->n = (int)nByte; |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1145 | pOut->enc = encoding; |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 1146 | UPDATE_MAX_BLOBSIZE(pOut); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1147 | break; |
| 1148 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1149 | |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 1150 | /* Opcode: Add P1 P2 P3 * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1151 | ** |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1152 | ** Add the value in register P1 to the value in register P2 |
shane | 21e7feb | 2008-05-30 15:59:49 +0000 | [diff] [blame] | 1153 | ** and store the result in register P3. |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1154 | ** If either input is NULL, the result is NULL. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1155 | */ |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 1156 | /* Opcode: Multiply P1 P2 P3 * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1157 | ** |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 1158 | ** |
shane | 21e7feb | 2008-05-30 15:59:49 +0000 | [diff] [blame] | 1159 | ** Multiply the value in register P1 by the value in register P2 |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1160 | ** and store the result in register P3. |
| 1161 | ** If either input is NULL, the result is NULL. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1162 | */ |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 1163 | /* Opcode: Subtract P1 P2 P3 * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1164 | ** |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1165 | ** Subtract the value in register P1 from the value in register P2 |
| 1166 | ** and store the result in register P3. |
| 1167 | ** If either input is NULL, the result is NULL. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1168 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1169 | /* Opcode: Divide P1 P2 P3 * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1170 | ** |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1171 | ** Divide the value in register P1 by the value in register P2 |
| 1172 | ** and store the result in register P3. If the value in register P2 |
| 1173 | ** is zero, then the result is NULL. |
| 1174 | ** If either input is NULL, the result is NULL. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1175 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1176 | /* Opcode: Remainder P1 P2 P3 * * |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1177 | ** |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 1178 | ** Compute the remainder after integer division of the value in |
| 1179 | ** register P1 by the value in register P2 and store the result in P3. |
| 1180 | ** If the value in register P2 is zero the result is NULL. |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1181 | ** If either operand is NULL, the result is NULL. |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1182 | */ |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1183 | case OP_Add: /* same as TK_PLUS, in1, in2, out3 */ |
| 1184 | case OP_Subtract: /* same as TK_MINUS, in1, in2, out3 */ |
| 1185 | case OP_Multiply: /* same as TK_STAR, in1, in2, out3 */ |
| 1186 | case OP_Divide: /* same as TK_SLASH, in1, in2, out3 */ |
| 1187 | case OP_Remainder: { /* same as TK_REM, in1, in2, out3 */ |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1188 | int flags; |
drh | 61669b3 | 2008-07-30 13:27:10 +0000 | [diff] [blame] | 1189 | applyNumericAffinity(pIn1); |
| 1190 | applyNumericAffinity(pIn2); |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1191 | flags = pIn1->flags | pIn2->flags; |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 1192 | if( (flags & MEM_Null)!=0 ) goto arithmetic_result_is_null; |
| 1193 | if( (pIn1->flags & pIn2->flags & MEM_Int)==MEM_Int ){ |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1194 | i64 a, b; |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1195 | a = pIn1->u.i; |
| 1196 | b = pIn2->u.i; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1197 | switch( pOp->opcode ){ |
| 1198 | case OP_Add: b += a; break; |
| 1199 | case OP_Subtract: b -= a; break; |
| 1200 | case OP_Multiply: b *= a; break; |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1201 | case OP_Divide: { |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 1202 | if( a==0 ) goto arithmetic_result_is_null; |
danielk1977 | 42d4ef2 | 2007-06-26 11:13:25 +0000 | [diff] [blame] | 1203 | /* Dividing the largest possible negative 64-bit integer (1<<63) by |
drh | 0f05035 | 2008-05-09 18:03:13 +0000 | [diff] [blame] | 1204 | ** -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] | 1205 | ** some architectures, the value overflows to (1<<63). On others, |
| 1206 | ** a SIGFPE is issued. The following statement normalizes this |
shane | 21e7feb | 2008-05-30 15:59:49 +0000 | [diff] [blame] | 1207 | ** behavior so that all architectures behave as if integer |
| 1208 | ** overflow occurred. |
danielk1977 | 42d4ef2 | 2007-06-26 11:13:25 +0000 | [diff] [blame] | 1209 | */ |
drh | 0f05035 | 2008-05-09 18:03:13 +0000 | [diff] [blame] | 1210 | if( a==-1 && b==SMALLEST_INT64 ) a = 1; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1211 | b /= a; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1212 | break; |
| 1213 | } |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1214 | default: { |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 1215 | if( a==0 ) goto arithmetic_result_is_null; |
danielk1977 | 42d4ef2 | 2007-06-26 11:13:25 +0000 | [diff] [blame] | 1216 | if( a==-1 ) a = 1; |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1217 | b %= a; |
| 1218 | break; |
| 1219 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1220 | } |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1221 | pOut->u.i = b; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1222 | MemSetTypeFlag(pOut, MEM_Int); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1223 | }else{ |
| 1224 | double a, b; |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1225 | a = sqlite3VdbeRealValue(pIn1); |
| 1226 | b = sqlite3VdbeRealValue(pIn2); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1227 | switch( pOp->opcode ){ |
| 1228 | case OP_Add: b += a; break; |
| 1229 | case OP_Subtract: b -= a; break; |
| 1230 | case OP_Multiply: b *= a; break; |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1231 | case OP_Divide: { |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 1232 | if( a==0.0 ) goto arithmetic_result_is_null; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1233 | b /= a; |
| 1234 | break; |
| 1235 | } |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1236 | default: { |
danielk1977 | 4b5710e | 2007-05-08 13:57:34 +0000 | [diff] [blame] | 1237 | i64 ia = (i64)a; |
| 1238 | i64 ib = (i64)b; |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 1239 | if( ia==0 ) goto arithmetic_result_is_null; |
danielk1977 | 42d4ef2 | 2007-06-26 11:13:25 +0000 | [diff] [blame] | 1240 | if( ia==-1 ) ia = 1; |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 1241 | b = (double)(ib % ia); |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1242 | break; |
| 1243 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1244 | } |
drh | 0de3ae9 | 2008-04-28 16:55:26 +0000 | [diff] [blame] | 1245 | if( sqlite3IsNaN(b) ){ |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 1246 | goto arithmetic_result_is_null; |
drh | 53c1402 | 2007-05-10 17:23:11 +0000 | [diff] [blame] | 1247 | } |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1248 | pOut->r = b; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1249 | MemSetTypeFlag(pOut, MEM_Real); |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1250 | if( (flags & MEM_Real)==0 ){ |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1251 | sqlite3VdbeIntegerAffinity(pOut); |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1252 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1253 | } |
| 1254 | break; |
| 1255 | |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 1256 | arithmetic_result_is_null: |
| 1257 | sqlite3VdbeMemSetNull(pOut); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1258 | break; |
| 1259 | } |
| 1260 | |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 1261 | /* Opcode: CollSeq * * P4 |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 1262 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 1263 | ** 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] | 1264 | ** or aggregate calls sqlite3GetFuncCollSeq(), this collation sequence will |
| 1265 | ** be returned. This is used by the built-in min(), max() and nullif() |
drh | e6f85e7 | 2004-12-25 01:03:13 +0000 | [diff] [blame] | 1266 | ** functions. |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 1267 | ** |
| 1268 | ** The interface used by the implementation of the aforementioned functions |
| 1269 | ** to retrieve the collation sequence set by this opcode is not available |
| 1270 | ** publicly, only to user functions defined in func.c. |
| 1271 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1272 | case OP_CollSeq: { |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 1273 | assert( pOp->p4type==P4_COLLSEQ ); |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 1274 | break; |
| 1275 | } |
| 1276 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1277 | /* Opcode: Function P1 P2 P3 P4 P5 |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 1278 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 1279 | ** Invoke a user function (P4 is a pointer to a Function structure that |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1280 | ** defines the function) with P5 arguments taken from register P2 and |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1281 | ** successors. The result of the function is stored in register P3. |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1282 | ** Register P3 must not be one of the function inputs. |
danielk1977 | 682f68b | 2004-06-05 10:22:17 +0000 | [diff] [blame] | 1283 | ** |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 1284 | ** 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] | 1285 | ** function was determined to be constant at compile time. If the first |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 1286 | ** 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] | 1287 | ** whether meta data associated with a user function argument using the |
| 1288 | ** sqlite3_set_auxdata() API may be safely retained until the next |
| 1289 | ** invocation of this opcode. |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 1290 | ** |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 1291 | ** See also: AggStep and AggFinal |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 1292 | */ |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 1293 | case OP_Function: { |
danielk1977 | 51ad0ec | 2004-05-24 12:39:02 +0000 | [diff] [blame] | 1294 | int i; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1295 | Mem *pArg; |
danielk1977 | 22322fd | 2004-05-25 23:35:17 +0000 | [diff] [blame] | 1296 | sqlite3_context ctx; |
danielk1977 | 51ad0ec | 2004-05-24 12:39:02 +0000 | [diff] [blame] | 1297 | sqlite3_value **apVal; |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1298 | int n = pOp->p5; |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 1299 | |
danielk1977 | 6ddcca5 | 2004-05-24 23:48:25 +0000 | [diff] [blame] | 1300 | apVal = p->apArg; |
danielk1977 | 51ad0ec | 2004-05-24 12:39:02 +0000 | [diff] [blame] | 1301 | assert( apVal || n==0 ); |
| 1302 | |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1303 | assert( n==0 || (pOp->p2>0 && pOp->p2+n<=p->nMem) ); |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1304 | assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n ); |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1305 | pArg = &p->aMem[pOp->p2]; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1306 | for(i=0; i<n; i++, pArg++){ |
danielk1977 | 51ad0ec | 2004-05-24 12:39:02 +0000 | [diff] [blame] | 1307 | apVal[i] = pArg; |
drh | 8079a0d | 2006-01-12 17:20:50 +0000 | [diff] [blame] | 1308 | storeTypeInfo(pArg, encoding); |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 1309 | REGISTER_TRACE(pOp->p2, pArg); |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 1310 | } |
danielk1977 | 51ad0ec | 2004-05-24 12:39:02 +0000 | [diff] [blame] | 1311 | |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 1312 | assert( pOp->p4type==P4_FUNCDEF || pOp->p4type==P4_VDBEFUNC ); |
| 1313 | if( pOp->p4type==P4_FUNCDEF ){ |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 1314 | ctx.pFunc = pOp->p4.pFunc; |
danielk1977 | 682f68b | 2004-06-05 10:22:17 +0000 | [diff] [blame] | 1315 | ctx.pVdbeFunc = 0; |
| 1316 | }else{ |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 1317 | ctx.pVdbeFunc = (VdbeFunc*)pOp->p4.pVdbeFunc; |
danielk1977 | 682f68b | 2004-06-05 10:22:17 +0000 | [diff] [blame] | 1318 | ctx.pFunc = ctx.pVdbeFunc->pFunc; |
| 1319 | } |
| 1320 | |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1321 | assert( pOp->p3>0 && pOp->p3<=p->nMem ); |
| 1322 | pOut = &p->aMem[pOp->p3]; |
drh | 00706be | 2004-01-30 14:49:16 +0000 | [diff] [blame] | 1323 | ctx.s.flags = MEM_Null; |
drh | fa4a4b9 | 2008-03-19 21:45:51 +0000 | [diff] [blame] | 1324 | ctx.s.db = db; |
danielk1977 | 5f09613 | 2008-03-28 15:44:09 +0000 | [diff] [blame] | 1325 | ctx.s.xDel = 0; |
| 1326 | ctx.s.zMalloc = 0; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1327 | |
| 1328 | /* The output cell may already have a buffer allocated. Move |
| 1329 | ** the pointer to ctx.s so in case the user-function can use |
| 1330 | ** the already allocated buffer instead of allocating a new one. |
| 1331 | */ |
| 1332 | sqlite3VdbeMemMove(&ctx.s, pOut); |
| 1333 | MemSetTypeFlag(&ctx.s, MEM_Null); |
| 1334 | |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 1335 | ctx.isError = 0; |
drh | e82f5d0 | 2008-10-07 19:53:14 +0000 | [diff] [blame] | 1336 | if( ctx.pFunc->flags & SQLITE_FUNC_NEEDCOLL ){ |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 1337 | assert( pOp>p->aOp ); |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 1338 | assert( pOp[-1].p4type==P4_COLLSEQ ); |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 1339 | assert( pOp[-1].opcode==OP_CollSeq ); |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 1340 | ctx.pColl = pOp[-1].p4.pColl; |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 1341 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1342 | if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse; |
danielk1977 | 51ad0ec | 2004-05-24 12:39:02 +0000 | [diff] [blame] | 1343 | (*ctx.pFunc->xFunc)(&ctx, n, apVal); |
danielk1977 | 75eb016 | 2008-03-28 19:16:33 +0000 | [diff] [blame] | 1344 | if( sqlite3SafetyOn(db) ){ |
| 1345 | sqlite3VdbeMemRelease(&ctx.s); |
| 1346 | goto abort_due_to_misuse; |
| 1347 | } |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 1348 | if( db->mallocFailed ){ |
danielk1977 | e0fc526 | 2007-07-26 06:50:05 +0000 | [diff] [blame] | 1349 | /* Even though a malloc() has failed, the implementation of the |
| 1350 | ** user function may have called an sqlite3_result_XXX() function |
| 1351 | ** to return a value. The following call releases any resources |
| 1352 | ** associated with such a value. |
| 1353 | ** |
| 1354 | ** Note: Maybe MemRelease() should be called if sqlite3SafetyOn() |
| 1355 | ** fails also (the if(...) statement above). But if people are |
| 1356 | ** misusing sqlite, they have bigger problems than a leaked value. |
| 1357 | */ |
| 1358 | sqlite3VdbeMemRelease(&ctx.s); |
| 1359 | goto no_mem; |
| 1360 | } |
danielk1977 | 7e18c25 | 2004-05-25 11:47:24 +0000 | [diff] [blame] | 1361 | |
shane | 21e7feb | 2008-05-30 15:59:49 +0000 | [diff] [blame] | 1362 | /* If any auxiliary data functions have been called by this user function, |
danielk1977 | 682f68b | 2004-06-05 10:22:17 +0000 | [diff] [blame] | 1363 | ** immediately call the destructor for any non-static values. |
| 1364 | */ |
| 1365 | if( ctx.pVdbeFunc ){ |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 1366 | sqlite3VdbeDeleteAuxData(ctx.pVdbeFunc, pOp->p1); |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 1367 | pOp->p4.pVdbeFunc = ctx.pVdbeFunc; |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 1368 | pOp->p4type = P4_VDBEFUNC; |
danielk1977 | 682f68b | 2004-06-05 10:22:17 +0000 | [diff] [blame] | 1369 | } |
| 1370 | |
drh | 90669c1 | 2006-01-20 15:45:36 +0000 | [diff] [blame] | 1371 | /* If the function returned an error, throw an exception */ |
| 1372 | if( ctx.isError ){ |
drh | f089aa4 | 2008-07-08 19:34:06 +0000 | [diff] [blame] | 1373 | sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(&ctx.s)); |
drh | 69544ec | 2008-02-06 14:11:34 +0000 | [diff] [blame] | 1374 | rc = ctx.isError; |
drh | 90669c1 | 2006-01-20 15:45:36 +0000 | [diff] [blame] | 1375 | } |
| 1376 | |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1377 | /* Copy the result of the function into register P3 */ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 1378 | sqlite3VdbeChangeEncoding(&ctx.s, encoding); |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1379 | sqlite3VdbeMemMove(pOut, &ctx.s); |
| 1380 | if( sqlite3VdbeMemTooBig(pOut) ){ |
drh | 023ae03 | 2007-05-08 12:12:16 +0000 | [diff] [blame] | 1381 | goto too_big; |
| 1382 | } |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 1383 | REGISTER_TRACE(pOp->p3, pOut); |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 1384 | UPDATE_MAX_BLOBSIZE(pOut); |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 1385 | break; |
| 1386 | } |
| 1387 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1388 | /* Opcode: BitAnd P1 P2 P3 * * |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1389 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1390 | ** Take the bit-wise AND of the values in register P1 and P2 and |
| 1391 | ** store the result in register P3. |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1392 | ** If either input is NULL, the result is NULL. |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1393 | */ |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1394 | /* Opcode: BitOr P1 P2 P3 * * |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1395 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1396 | ** Take the bit-wise OR of the values in register P1 and P2 and |
| 1397 | ** store the result in register P3. |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1398 | ** If either input is NULL, the result is NULL. |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1399 | */ |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1400 | /* Opcode: ShiftLeft P1 P2 P3 * * |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1401 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1402 | ** Shift the integer value in register P2 to the left by the |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1403 | ** number of bits specified by the integer in regiser P1. |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1404 | ** Store the result in register P3. |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1405 | ** If either input is NULL, the result is NULL. |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1406 | */ |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1407 | /* Opcode: ShiftRight P1 P2 P3 * * |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1408 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1409 | ** Shift the integer value in register P2 to the right by the |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1410 | ** number of bits specified by the integer in register P1. |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1411 | ** Store the result in register P3. |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1412 | ** If either input is NULL, the result is NULL. |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1413 | */ |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1414 | case OP_BitAnd: /* same as TK_BITAND, in1, in2, out3 */ |
| 1415 | case OP_BitOr: /* same as TK_BITOR, in1, in2, out3 */ |
| 1416 | case OP_ShiftLeft: /* same as TK_LSHIFT, in1, in2, out3 */ |
| 1417 | case OP_ShiftRight: { /* same as TK_RSHIFT, in1, in2, out3 */ |
drh | b127612 | 2005-10-29 15:48:30 +0000 | [diff] [blame] | 1418 | i64 a, b; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 1419 | |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1420 | if( (pIn1->flags | pIn2->flags) & MEM_Null ){ |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 1421 | sqlite3VdbeMemSetNull(pOut); |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1422 | break; |
| 1423 | } |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1424 | a = sqlite3VdbeIntValue(pIn2); |
| 1425 | b = sqlite3VdbeIntValue(pIn1); |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1426 | switch( pOp->opcode ){ |
| 1427 | case OP_BitAnd: a &= b; break; |
| 1428 | case OP_BitOr: a |= b; break; |
| 1429 | case OP_ShiftLeft: a <<= b; break; |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 1430 | default: assert( pOp->opcode==OP_ShiftRight ); |
| 1431 | a >>= b; break; |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1432 | } |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1433 | pOut->u.i = a; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1434 | MemSetTypeFlag(pOut, MEM_Int); |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1435 | break; |
| 1436 | } |
| 1437 | |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1438 | /* Opcode: AddImm P1 P2 * * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1439 | ** |
danielk1977 | 0cdc022 | 2008-06-26 18:04:03 +0000 | [diff] [blame] | 1440 | ** Add the constant P2 to the value in register P1. |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1441 | ** The result is always an integer. |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 1442 | ** |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1443 | ** To force any register to be an integer, just add 0. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1444 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1445 | case OP_AddImm: { /* in1 */ |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1446 | sqlite3VdbeMemIntegerify(pIn1); |
| 1447 | pIn1->u.i += pOp->p2; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1448 | break; |
| 1449 | } |
| 1450 | |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1451 | /* Opcode: MustBeInt P1 P2 * * * |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 1452 | ** |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1453 | ** Force the value in register P1 to be an integer. If the value |
| 1454 | ** in P1 is not an integer and cannot be converted into an integer |
danielk1977 | 9a96b66 | 2007-11-29 17:05:18 +0000 | [diff] [blame] | 1455 | ** without data loss, then jump immediately to P2, or if P2==0 |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 1456 | ** raise an SQLITE_MISMATCH exception. |
| 1457 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1458 | case OP_MustBeInt: { /* jump, in1 */ |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 1459 | applyAffinity(pIn1, SQLITE_AFF_NUMERIC, encoding); |
| 1460 | if( (pIn1->flags & MEM_Int)==0 ){ |
drh | 17c4029 | 2004-07-21 02:53:29 +0000 | [diff] [blame] | 1461 | if( pOp->p2==0 ){ |
| 1462 | rc = SQLITE_MISMATCH; |
| 1463 | goto abort_due_to_error; |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 1464 | }else{ |
drh | 17c4029 | 2004-07-21 02:53:29 +0000 | [diff] [blame] | 1465 | pc = pOp->p2 - 1; |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 1466 | } |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 1467 | }else{ |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1468 | MemSetTypeFlag(pIn1, MEM_Int); |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 1469 | } |
| 1470 | break; |
| 1471 | } |
| 1472 | |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1473 | /* Opcode: RealAffinity P1 * * * * |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 1474 | ** |
drh | 2133d82 | 2008-01-03 18:44:59 +0000 | [diff] [blame] | 1475 | ** If register P1 holds an integer convert it to a real value. |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 1476 | ** |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1477 | ** This opcode is used when extracting information from a column that |
| 1478 | ** has REAL affinity. Such column values may still be stored as |
| 1479 | ** integers, for space efficiency, but after extraction we want them |
| 1480 | ** to have only a real value. |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 1481 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1482 | case OP_RealAffinity: { /* in1 */ |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1483 | if( pIn1->flags & MEM_Int ){ |
| 1484 | sqlite3VdbeMemRealify(pIn1); |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1485 | } |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 1486 | break; |
| 1487 | } |
| 1488 | |
drh | 8df447f | 2005-11-01 15:48:24 +0000 | [diff] [blame] | 1489 | #ifndef SQLITE_OMIT_CAST |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1490 | /* Opcode: ToText P1 * * * * |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 1491 | ** |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1492 | ** Force the value in register P1 to be text. |
drh | 31beae9 | 2005-11-24 14:34:36 +0000 | [diff] [blame] | 1493 | ** If the value is numeric, convert it to a string using the |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 1494 | ** equivalent of printf(). Blob values are unchanged and |
| 1495 | ** are afterwards simply interpreted as text. |
| 1496 | ** |
| 1497 | ** A NULL value is not changed by this routine. It remains NULL. |
| 1498 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1499 | case OP_ToText: { /* same as TK_TO_TEXT, in1 */ |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1500 | if( pIn1->flags & MEM_Null ) break; |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 1501 | assert( MEM_Str==(MEM_Blob>>3) ); |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1502 | pIn1->flags |= (pIn1->flags&MEM_Blob)>>3; |
| 1503 | applyAffinity(pIn1, SQLITE_AFF_TEXT, encoding); |
| 1504 | rc = ExpandBlob(pIn1); |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1505 | assert( pIn1->flags & MEM_Str || db->mallocFailed ); |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1506 | pIn1->flags &= ~(MEM_Int|MEM_Real|MEM_Blob); |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 1507 | UPDATE_MAX_BLOBSIZE(pIn1); |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 1508 | break; |
| 1509 | } |
| 1510 | |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1511 | /* Opcode: ToBlob P1 * * * * |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 1512 | ** |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1513 | ** Force the value in register P1 to be a BLOB. |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 1514 | ** If the value is numeric, convert it to a string first. |
| 1515 | ** Strings are simply reinterpreted as blobs with no change |
| 1516 | ** to the underlying data. |
| 1517 | ** |
| 1518 | ** A NULL value is not changed by this routine. It remains NULL. |
| 1519 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1520 | case OP_ToBlob: { /* same as TK_TO_BLOB, in1 */ |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1521 | if( pIn1->flags & MEM_Null ) break; |
| 1522 | if( (pIn1->flags & MEM_Blob)==0 ){ |
| 1523 | applyAffinity(pIn1, SQLITE_AFF_TEXT, encoding); |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1524 | assert( pIn1->flags & MEM_Str || db->mallocFailed ); |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 1525 | } |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1526 | MemSetTypeFlag(pIn1, MEM_Blob); |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 1527 | UPDATE_MAX_BLOBSIZE(pIn1); |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 1528 | break; |
| 1529 | } |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1530 | |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1531 | /* Opcode: ToNumeric P1 * * * * |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1532 | ** |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1533 | ** Force the value in register P1 to be numeric (either an |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1534 | ** integer or a floating-point number.) |
| 1535 | ** If the value is text or blob, try to convert it to an using the |
| 1536 | ** equivalent of atoi() or atof() and store 0 if no such conversion |
| 1537 | ** is possible. |
| 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_ToNumeric: { /* same as TK_TO_NUMERIC, in1 */ |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1542 | if( (pIn1->flags & (MEM_Null|MEM_Int|MEM_Real))==0 ){ |
| 1543 | sqlite3VdbeMemNumerify(pIn1); |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1544 | } |
| 1545 | break; |
| 1546 | } |
| 1547 | #endif /* SQLITE_OMIT_CAST */ |
| 1548 | |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1549 | /* Opcode: ToInt P1 * * * * |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1550 | ** |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1551 | ** Force the value in register P1 be an integer. If |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1552 | ** The value is currently a real number, drop its fractional part. |
| 1553 | ** If the value is text or blob, try to convert it to an integer using the |
| 1554 | ** equivalent of atoi() and store 0 if no such conversion is possible. |
| 1555 | ** |
| 1556 | ** A NULL value is not changed by this routine. It remains NULL. |
| 1557 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1558 | case OP_ToInt: { /* same as TK_TO_INT, in1 */ |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1559 | if( (pIn1->flags & MEM_Null)==0 ){ |
| 1560 | sqlite3VdbeMemIntegerify(pIn1); |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1561 | } |
| 1562 | break; |
| 1563 | } |
| 1564 | |
| 1565 | #ifndef SQLITE_OMIT_CAST |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1566 | /* Opcode: ToReal P1 * * * * |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1567 | ** |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1568 | ** Force the value in register P1 to be a floating point number. |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1569 | ** If The value is currently an integer, convert it. |
| 1570 | ** 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] | 1571 | ** equivalent of atoi() and store 0.0 if no such conversion is possible. |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1572 | ** |
| 1573 | ** A NULL value is not changed by this routine. It remains NULL. |
| 1574 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1575 | case OP_ToReal: { /* same as TK_TO_REAL, in1 */ |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1576 | if( (pIn1->flags & MEM_Null)==0 ){ |
| 1577 | sqlite3VdbeMemRealify(pIn1); |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1578 | } |
| 1579 | break; |
| 1580 | } |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 1581 | #endif /* SQLITE_OMIT_CAST */ |
| 1582 | |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 1583 | /* Opcode: Lt P1 P2 P3 P4 P5 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1584 | ** |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 1585 | ** Compare the values in register P1 and P3. If reg(P3)<reg(P1) then |
| 1586 | ** jump to address P2. |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1587 | ** |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 1588 | ** If the SQLITE_JUMPIFNULL bit of P5 is set and either reg(P1) or |
| 1589 | ** reg(P3) is NULL then take the jump. If the SQLITE_JUMPIFNULL |
| 1590 | ** bit is clear then fall thru if either operand is NULL. |
drh | 4f68623 | 2005-09-20 13:55:18 +0000 | [diff] [blame] | 1591 | ** |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 1592 | ** The SQLITE_AFF_MASK portion of P5 must be an affinity character - |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1593 | ** SQLITE_AFF_TEXT, SQLITE_AFF_INTEGER, and so forth. An attempt is made |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1594 | ** to coerce both inputs according to this affinity before the |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 1595 | ** comparison is made. If the SQLITE_AFF_MASK is 0x00, then numeric |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1596 | ** affinity is used. Note that the affinity conversions are stored |
| 1597 | ** back into the input registers P1 and P3. So this opcode can cause |
| 1598 | ** persistent changes to registers P1 and P3. |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 1599 | ** |
| 1600 | ** Once any conversions have taken place, and neither value is NULL, |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 1601 | ** the values are compared. If both values are blobs then memcmp() is |
| 1602 | ** used to determine the results of the comparison. If both values |
| 1603 | ** are text, then the appropriate collating function specified in |
| 1604 | ** P4 is used to do the comparison. If P4 is not specified then |
| 1605 | ** memcmp() is used to compare text string. If both values are |
| 1606 | ** numeric, then a numeric comparison is used. If the two values |
| 1607 | ** are of different types, then numbers are considered less than |
| 1608 | ** strings and strings are considered less than blobs. |
drh | c9b84a1 | 2002-06-20 11:36:48 +0000 | [diff] [blame] | 1609 | ** |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 1610 | ** If the SQLITE_STOREP2 bit of P5 is set, then do not jump. Instead, |
| 1611 | ** store a boolean result (either 0, or 1, or NULL) in register P2. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1612 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1613 | /* Opcode: Ne P1 P2 P3 P4 P5 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1614 | ** |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 1615 | ** This works just like the Lt opcode except that the jump is taken if |
| 1616 | ** 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] | 1617 | ** additional information. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1618 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1619 | /* Opcode: Eq P1 P2 P3 P4 P5 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1620 | ** |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 1621 | ** This works just like the Lt opcode except that the jump is taken if |
| 1622 | ** the operands in registers P1 and P3 are equal. |
| 1623 | ** See the Lt opcode for additional information. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1624 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1625 | /* Opcode: Le P1 P2 P3 P4 P5 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1626 | ** |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 1627 | ** This works just like the Lt opcode except that the jump is taken if |
| 1628 | ** the content of register P3 is less than or equal to the content of |
| 1629 | ** register P1. See the Lt opcode for additional information. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1630 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1631 | /* Opcode: Gt P1 P2 P3 P4 P5 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1632 | ** |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 1633 | ** This works just like the Lt opcode except that the jump is taken if |
| 1634 | ** the content of register P3 is greater than the content of |
| 1635 | ** register P1. See the Lt opcode for additional information. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1636 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1637 | /* Opcode: Ge P1 P2 P3 P4 P5 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1638 | ** |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 1639 | ** This works just like the Lt opcode except that the jump is taken if |
| 1640 | ** the content of register P3 is greater than or equal to the content of |
| 1641 | ** register P1. See the Lt opcode for additional information. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1642 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1643 | case OP_Eq: /* same as TK_EQ, jump, in1, in3 */ |
| 1644 | case OP_Ne: /* same as TK_NE, jump, in1, in3 */ |
| 1645 | case OP_Lt: /* same as TK_LT, jump, in1, in3 */ |
| 1646 | case OP_Le: /* same as TK_LE, jump, in1, in3 */ |
| 1647 | case OP_Gt: /* same as TK_GT, jump, in1, in3 */ |
| 1648 | case OP_Ge: { /* same as TK_GE, jump, in1, in3 */ |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 1649 | int flags; |
| 1650 | int res; |
| 1651 | char affinity; |
| 1652 | |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 1653 | flags = pIn1->flags|pIn3->flags; |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 1654 | |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 1655 | if( flags&MEM_Null ){ |
drh | 93a960a | 2008-07-10 00:32:42 +0000 | [diff] [blame] | 1656 | /* If either operand is NULL then the result is always NULL. |
| 1657 | ** The jump is taken if the SQLITE_JUMPIFNULL bit is set. |
| 1658 | */ |
| 1659 | if( pOp->p5 & SQLITE_STOREP2 ){ |
| 1660 | pOut = &p->aMem[pOp->p2]; |
| 1661 | MemSetTypeFlag(pOut, MEM_Null); |
| 1662 | REGISTER_TRACE(pOp->p2, pOut); |
| 1663 | }else if( pOp->p5 & SQLITE_JUMPIFNULL ){ |
| 1664 | pc = pOp->p2-1; |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 1665 | } |
drh | 93a960a | 2008-07-10 00:32:42 +0000 | [diff] [blame] | 1666 | break; |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 1667 | } |
| 1668 | |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 1669 | affinity = pOp->p5 & SQLITE_AFF_MASK; |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 1670 | if( affinity ){ |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 1671 | applyAffinity(pIn1, affinity, encoding); |
| 1672 | applyAffinity(pIn3, affinity, encoding); |
drh | bbce338 | 2008-12-06 16:46:13 +0000 | [diff] [blame] | 1673 | if( db->mallocFailed ) goto no_mem; |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 1674 | } |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 1675 | |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 1676 | assert( pOp->p4type==P4_COLLSEQ || pOp->p4.pColl==0 ); |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 1677 | ExpandBlob(pIn1); |
| 1678 | ExpandBlob(pIn3); |
| 1679 | res = sqlite3MemCompare(pIn3, pIn1, pOp->p4.pColl); |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 1680 | switch( pOp->opcode ){ |
| 1681 | case OP_Eq: res = res==0; break; |
| 1682 | case OP_Ne: res = res!=0; break; |
| 1683 | case OP_Lt: res = res<0; break; |
| 1684 | case OP_Le: res = res<=0; break; |
| 1685 | case OP_Gt: res = res>0; break; |
| 1686 | default: res = res>=0; break; |
| 1687 | } |
| 1688 | |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 1689 | if( pOp->p5 & SQLITE_STOREP2 ){ |
| 1690 | pOut = &p->aMem[pOp->p2]; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1691 | MemSetTypeFlag(pOut, MEM_Int); |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 1692 | pOut->u.i = res; |
| 1693 | REGISTER_TRACE(pOp->p2, pOut); |
| 1694 | }else if( res ){ |
| 1695 | pc = pOp->p2-1; |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 1696 | } |
| 1697 | break; |
| 1698 | } |
drh | c9b84a1 | 2002-06-20 11:36:48 +0000 | [diff] [blame] | 1699 | |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 1700 | /* Opcode: Permutation * * * P4 * |
| 1701 | ** |
| 1702 | ** Set the permuation used by the OP_Compare operator to be the array |
| 1703 | ** of integers in P4. |
| 1704 | ** |
| 1705 | ** The permutation is only valid until the next OP_Permutation, OP_Compare, |
| 1706 | ** OP_Halt, or OP_ResultRow. Typically the OP_Permutation should occur |
| 1707 | ** immediately prior to the OP_Compare. |
| 1708 | */ |
| 1709 | case OP_Permutation: { |
| 1710 | assert( pOp->p4type==P4_INTARRAY ); |
| 1711 | assert( pOp->p4.ai ); |
| 1712 | aPermute = pOp->p4.ai; |
| 1713 | break; |
| 1714 | } |
| 1715 | |
drh | 16ee60f | 2008-06-20 18:13:25 +0000 | [diff] [blame] | 1716 | /* Opcode: Compare P1 P2 P3 P4 * |
| 1717 | ** |
| 1718 | ** Compare to vectors of registers in reg(P1)..reg(P1+P3-1) (all this |
| 1719 | ** one "A") and in reg(P2)..reg(P2+P3-1) ("B"). Save the result of |
| 1720 | ** the comparison for use by the next OP_Jump instruct. |
| 1721 | ** |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 1722 | ** P4 is a KeyInfo structure that defines collating sequences and sort |
| 1723 | ** orders for the comparison. The permutation applies to registers |
| 1724 | ** only. The KeyInfo elements are used sequentially. |
| 1725 | ** |
| 1726 | ** The comparison is a sort comparison, so NULLs compare equal, |
| 1727 | ** NULLs are less than numbers, numbers are less than strings, |
drh | 16ee60f | 2008-06-20 18:13:25 +0000 | [diff] [blame] | 1728 | ** and strings are less than blobs. |
| 1729 | */ |
| 1730 | case OP_Compare: { |
| 1731 | int n = pOp->p3; |
| 1732 | int i, p1, p2; |
| 1733 | const KeyInfo *pKeyInfo = pOp->p4.pKeyInfo; |
| 1734 | assert( n>0 ); |
drh | 93a960a | 2008-07-10 00:32:42 +0000 | [diff] [blame] | 1735 | assert( pKeyInfo!=0 ); |
drh | 16ee60f | 2008-06-20 18:13:25 +0000 | [diff] [blame] | 1736 | p1 = pOp->p1; |
| 1737 | assert( p1>0 && p1+n-1<p->nMem ); |
| 1738 | p2 = pOp->p2; |
| 1739 | assert( p2>0 && p2+n-1<p->nMem ); |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 1740 | for(i=0; i<n; i++){ |
| 1741 | int idx = aPermute ? aPermute[i] : i; |
| 1742 | CollSeq *pColl; /* Collating sequence to use on this term */ |
| 1743 | int bRev; /* True for DESCENDING sort order */ |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 1744 | REGISTER_TRACE(p1+idx, &p->aMem[p1+idx]); |
| 1745 | REGISTER_TRACE(p2+idx, &p->aMem[p2+idx]); |
drh | 93a960a | 2008-07-10 00:32:42 +0000 | [diff] [blame] | 1746 | assert( i<pKeyInfo->nField ); |
| 1747 | pColl = pKeyInfo->aColl[i]; |
| 1748 | bRev = pKeyInfo->aSortOrder[i]; |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 1749 | iCompare = sqlite3MemCompare(&p->aMem[p1+idx], &p->aMem[p2+idx], pColl); |
| 1750 | if( iCompare ){ |
| 1751 | if( bRev ) iCompare = -iCompare; |
| 1752 | break; |
| 1753 | } |
drh | 16ee60f | 2008-06-20 18:13:25 +0000 | [diff] [blame] | 1754 | } |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 1755 | aPermute = 0; |
drh | 16ee60f | 2008-06-20 18:13:25 +0000 | [diff] [blame] | 1756 | break; |
| 1757 | } |
| 1758 | |
| 1759 | /* Opcode: Jump P1 P2 P3 * * |
| 1760 | ** |
| 1761 | ** Jump to the instruction at address P1, P2, or P3 depending on whether |
| 1762 | ** in the most recent OP_Compare instruction the P1 vector was less than |
| 1763 | ** equal to, or greater than the P2 vector, respectively. |
| 1764 | */ |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 1765 | case OP_Jump: { /* jump */ |
| 1766 | if( iCompare<0 ){ |
drh | 16ee60f | 2008-06-20 18:13:25 +0000 | [diff] [blame] | 1767 | pc = pOp->p1 - 1; |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 1768 | }else if( iCompare==0 ){ |
drh | 16ee60f | 2008-06-20 18:13:25 +0000 | [diff] [blame] | 1769 | pc = pOp->p2 - 1; |
| 1770 | }else{ |
| 1771 | pc = pOp->p3 - 1; |
| 1772 | } |
| 1773 | break; |
| 1774 | } |
| 1775 | |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1776 | /* Opcode: And P1 P2 P3 * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1777 | ** |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1778 | ** Take the logical AND of the values in registers P1 and P2 and |
| 1779 | ** write the result into register P3. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1780 | ** |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1781 | ** If either P1 or P2 is 0 (false) then the result is 0 even if |
| 1782 | ** the other input is NULL. A NULL and true or two NULLs give |
| 1783 | ** a NULL output. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1784 | */ |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1785 | /* Opcode: Or P1 P2 P3 * * |
| 1786 | ** |
| 1787 | ** Take the logical OR of the values in register P1 and P2 and |
| 1788 | ** store the answer in register P3. |
| 1789 | ** |
| 1790 | ** If either P1 or P2 is nonzero (true) then the result is 1 (true) |
| 1791 | ** even if the other input is NULL. A NULL and false or two NULLs |
| 1792 | ** give a NULL output. |
| 1793 | */ |
| 1794 | case OP_And: /* same as TK_AND, in1, in2, out3 */ |
| 1795 | case OP_Or: { /* same as TK_OR, in1, in2, out3 */ |
| 1796 | int v1, v2; /* 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */ |
drh | bb11351 | 2002-05-27 01:04:51 +0000 | [diff] [blame] | 1797 | |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1798 | if( pIn1->flags & MEM_Null ){ |
drh | bb11351 | 2002-05-27 01:04:51 +0000 | [diff] [blame] | 1799 | v1 = 2; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1800 | }else{ |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1801 | v1 = sqlite3VdbeIntValue(pIn1)!=0; |
drh | bb11351 | 2002-05-27 01:04:51 +0000 | [diff] [blame] | 1802 | } |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1803 | if( pIn2->flags & MEM_Null ){ |
drh | bb11351 | 2002-05-27 01:04:51 +0000 | [diff] [blame] | 1804 | v2 = 2; |
| 1805 | }else{ |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1806 | v2 = sqlite3VdbeIntValue(pIn2)!=0; |
drh | bb11351 | 2002-05-27 01:04:51 +0000 | [diff] [blame] | 1807 | } |
| 1808 | if( pOp->opcode==OP_And ){ |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1809 | 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] | 1810 | v1 = and_logic[v1*3+v2]; |
| 1811 | }else{ |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1812 | 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] | 1813 | v1 = or_logic[v1*3+v2]; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1814 | } |
drh | bb11351 | 2002-05-27 01:04:51 +0000 | [diff] [blame] | 1815 | if( v1==2 ){ |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1816 | MemSetTypeFlag(pOut, MEM_Null); |
drh | bb11351 | 2002-05-27 01:04:51 +0000 | [diff] [blame] | 1817 | }else{ |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1818 | pOut->u.i = v1; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1819 | MemSetTypeFlag(pOut, MEM_Int); |
drh | bb11351 | 2002-05-27 01:04:51 +0000 | [diff] [blame] | 1820 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1821 | break; |
| 1822 | } |
| 1823 | |
drh | e99fa2a | 2008-12-15 15:27:51 +0000 | [diff] [blame] | 1824 | /* Opcode: Not P1 P2 * * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1825 | ** |
drh | e99fa2a | 2008-12-15 15:27:51 +0000 | [diff] [blame] | 1826 | ** Interpret the value in register P1 as a boolean value. Store the |
| 1827 | ** boolean complement in register P2. If the value in register P1 is |
| 1828 | ** NULL, then a NULL is stored in P2. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1829 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1830 | case OP_Not: { /* same as TK_NOT, in1 */ |
drh | e99fa2a | 2008-12-15 15:27:51 +0000 | [diff] [blame] | 1831 | pOut = &p->aMem[pOp->p2]; |
| 1832 | if( pIn1->flags & MEM_Null ){ |
| 1833 | sqlite3VdbeMemSetNull(pOut); |
| 1834 | }else{ |
| 1835 | sqlite3VdbeMemSetInt64(pOut, !sqlite3VdbeIntValue(pIn1)); |
| 1836 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1837 | break; |
| 1838 | } |
| 1839 | |
drh | e99fa2a | 2008-12-15 15:27:51 +0000 | [diff] [blame] | 1840 | /* Opcode: BitNot P1 P2 * * * |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1841 | ** |
drh | e99fa2a | 2008-12-15 15:27:51 +0000 | [diff] [blame] | 1842 | ** Interpret the content of register P1 as an integer. Store the |
| 1843 | ** ones-complement of the P1 value into register P2. If P1 holds |
| 1844 | ** a NULL then store a NULL in P2. |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1845 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1846 | case OP_BitNot: { /* same as TK_BITNOT, in1 */ |
drh | e99fa2a | 2008-12-15 15:27:51 +0000 | [diff] [blame] | 1847 | pOut = &p->aMem[pOp->p2]; |
| 1848 | if( pIn1->flags & MEM_Null ){ |
| 1849 | sqlite3VdbeMemSetNull(pOut); |
| 1850 | }else{ |
| 1851 | sqlite3VdbeMemSetInt64(pOut, ~sqlite3VdbeIntValue(pIn1)); |
| 1852 | } |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1853 | break; |
| 1854 | } |
| 1855 | |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 1856 | /* Opcode: If P1 P2 P3 * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1857 | ** |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 1858 | ** Jump to P2 if the value in register P1 is true. The value is |
| 1859 | ** is considered true if it is numeric and non-zero. If the value |
| 1860 | ** in P1 is NULL then take the jump if P3 is true. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1861 | */ |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 1862 | /* Opcode: IfNot P1 P2 P3 * * |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1863 | ** |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 1864 | ** Jump to P2 if the value in register P1 is False. The value is |
| 1865 | ** is considered true if it has a numeric value of zero. If the value |
| 1866 | ** in P1 is NULL then take the jump if P3 is true. |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1867 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1868 | case OP_If: /* jump, in1 */ |
| 1869 | case OP_IfNot: { /* jump, in1 */ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1870 | int c; |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 1871 | if( pIn1->flags & MEM_Null ){ |
| 1872 | c = pOp->p3; |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1873 | }else{ |
drh | ba0232a | 2005-06-06 17:27:19 +0000 | [diff] [blame] | 1874 | #ifdef SQLITE_OMIT_FLOATING_POINT |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 1875 | c = sqlite3VdbeIntValue(pIn1); |
drh | ba0232a | 2005-06-06 17:27:19 +0000 | [diff] [blame] | 1876 | #else |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 1877 | c = sqlite3VdbeRealValue(pIn1)!=0.0; |
drh | ba0232a | 2005-06-06 17:27:19 +0000 | [diff] [blame] | 1878 | #endif |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1879 | if( pOp->opcode==OP_IfNot ) c = !c; |
| 1880 | } |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 1881 | if( c ){ |
| 1882 | pc = pOp->p2-1; |
| 1883 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1884 | break; |
| 1885 | } |
| 1886 | |
drh | 2d401ab | 2008-01-10 23:50:11 +0000 | [diff] [blame] | 1887 | /* Opcode: IsNull P1 P2 P3 * * |
drh | 477df4b | 2008-01-05 18:48:24 +0000 | [diff] [blame] | 1888 | ** |
drh | 2d401ab | 2008-01-10 23:50:11 +0000 | [diff] [blame] | 1889 | ** Jump to P2 if the value in register P1 is NULL. If P3 is greater |
| 1890 | ** than zero, then check all values reg(P1), reg(P1+1), |
| 1891 | ** reg(P1+2), ..., reg(P1+P3-1). |
drh | 477df4b | 2008-01-05 18:48:24 +0000 | [diff] [blame] | 1892 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1893 | case OP_IsNull: { /* same as TK_ISNULL, jump, in1 */ |
drh | 2d401ab | 2008-01-10 23:50:11 +0000 | [diff] [blame] | 1894 | int n = pOp->p3; |
| 1895 | assert( pOp->p3==0 || pOp->p1>0 ); |
| 1896 | do{ |
| 1897 | if( (pIn1->flags & MEM_Null)!=0 ){ |
| 1898 | pc = pOp->p2 - 1; |
| 1899 | break; |
| 1900 | } |
| 1901 | pIn1++; |
| 1902 | }while( --n > 0 ); |
drh | 477df4b | 2008-01-05 18:48:24 +0000 | [diff] [blame] | 1903 | break; |
| 1904 | } |
| 1905 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1906 | /* Opcode: NotNull P1 P2 * * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1907 | ** |
drh | 6a288a3 | 2008-01-07 19:20:24 +0000 | [diff] [blame] | 1908 | ** Jump to P2 if the value in register P1 is not NULL. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1909 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1910 | case OP_NotNull: { /* same as TK_NOTNULL, jump, in1 */ |
drh | 6a288a3 | 2008-01-07 19:20:24 +0000 | [diff] [blame] | 1911 | if( (pIn1->flags & MEM_Null)==0 ){ |
| 1912 | pc = pOp->p2 - 1; |
| 1913 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1914 | break; |
| 1915 | } |
| 1916 | |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 1917 | /* Opcode: SetNumColumns * P2 * * * |
danielk1977 | b4964b7 | 2004-05-18 01:23:38 +0000 | [diff] [blame] | 1918 | ** |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 1919 | ** This opcode sets the number of columns for the cursor opened by the |
| 1920 | ** following instruction to P2. |
danielk1977 | b4964b7 | 2004-05-18 01:23:38 +0000 | [diff] [blame] | 1921 | ** |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 1922 | ** An OP_SetNumColumns is only useful if it occurs immediately before |
| 1923 | ** one of the following opcodes: |
danielk1977 | ac17178 | 2005-02-05 06:49:54 +0000 | [diff] [blame] | 1924 | ** |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 1925 | ** OpenRead |
| 1926 | ** OpenWrite |
| 1927 | ** OpenPseudo |
| 1928 | ** |
| 1929 | ** If the OP_Column opcode is to be executed on a cursor, then |
| 1930 | ** this opcode must be present immediately before the opcode that |
| 1931 | ** opens the cursor. |
danielk1977 | b4964b7 | 2004-05-18 01:23:38 +0000 | [diff] [blame] | 1932 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1933 | case OP_SetNumColumns: { |
danielk1977 | b4964b7 | 2004-05-18 01:23:38 +0000 | [diff] [blame] | 1934 | break; |
| 1935 | } |
| 1936 | |
danielk1977 | 60585dd | 2008-01-03 08:08:40 +0000 | [diff] [blame] | 1937 | /* Opcode: Column P1 P2 P3 P4 * |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 1938 | ** |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1939 | ** Interpret the data that cursor P1 points to as a structure built using |
| 1940 | ** the MakeRecord instruction. (See the MakeRecord opcode for additional |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 1941 | ** information about the format of the data.) Extract the P2-th column |
| 1942 | ** from this record. If there are less that (P2+1) |
| 1943 | ** values in the record, extract a NULL. |
| 1944 | ** |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1945 | ** The value extracted is stored in register P3. |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 1946 | ** |
danielk1977 | 1f4aa33 | 2008-01-03 09:51:55 +0000 | [diff] [blame] | 1947 | ** If the column contains fewer than P2 fields, then extract a NULL. Or, |
| 1948 | ** if the P4 argument is a P4_MEM use the value of the P4 argument as |
| 1949 | ** the result. |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 1950 | */ |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1951 | case OP_Column: { |
danielk1977 | 64202cf | 2008-11-17 15:31:47 +0000 | [diff] [blame] | 1952 | int payloadSize; /* Number of bytes in the record */ |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 1953 | int p1 = pOp->p1; /* P1 value of the opcode */ |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1954 | int p2 = pOp->p2; /* column number to retrieve */ |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 1955 | VdbeCursor *pC = 0;/* The VDBE cursor */ |
drh | e61cffc | 2004-06-12 18:12:15 +0000 | [diff] [blame] | 1956 | char *zRec; /* Pointer to complete record-data */ |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 1957 | BtCursor *pCrsr; /* The BTree cursor */ |
| 1958 | u32 *aType; /* aType[i] holds the numeric type of the i-th column */ |
| 1959 | 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] | 1960 | int nField; /* number of fields in the record */ |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1961 | int len; /* The length of the serialized data for the column */ |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 1962 | int i; /* Loop counter */ |
| 1963 | char *zData; /* Part of the record being decoded */ |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 1964 | Mem *pDest; /* Where to write the extracted value */ |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 1965 | Mem sMem; /* For storing the record being decoded */ |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 1966 | |
drh | b27b7f5 | 2008-12-10 18:03:45 +0000 | [diff] [blame] | 1967 | memset(&sMem, 0, sizeof(sMem)); |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 1968 | assert( p1<p->nCursor ); |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1969 | assert( pOp->p3>0 && pOp->p3<=p->nMem ); |
| 1970 | pDest = &p->aMem[pOp->p3]; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1971 | MemSetTypeFlag(pDest, MEM_Null); |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1972 | |
drh | e61cffc | 2004-06-12 18:12:15 +0000 | [diff] [blame] | 1973 | /* This block sets the variable payloadSize to be the total number of |
| 1974 | ** bytes in the record. |
| 1975 | ** |
| 1976 | ** 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] | 1977 | ** The complete record text is always available for pseudo-tables |
| 1978 | ** If the record is stored in a cursor, the complete record text |
| 1979 | ** might be available in the pC->aRow cache. Or it might not be. |
| 1980 | ** If the data is unavailable, zRec is set to NULL. |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 1981 | ** |
| 1982 | ** We also compute the number of columns in the record. For cursors, |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 1983 | ** the number of columns is stored in the VdbeCursor.nField element. |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1984 | */ |
drh | b73857f | 2006-03-17 00:25:59 +0000 | [diff] [blame] | 1985 | pC = p->apCsr[p1]; |
danielk1977 | 6c92409 | 2007-11-12 08:09:34 +0000 | [diff] [blame] | 1986 | assert( pC!=0 ); |
danielk1977 | 0817d0d | 2007-02-14 09:19:36 +0000 | [diff] [blame] | 1987 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 1988 | assert( pC->pVtabCursor==0 ); |
| 1989 | #endif |
drh | b73857f | 2006-03-17 00:25:59 +0000 | [diff] [blame] | 1990 | if( pC->pCursor!=0 ){ |
drh | e61cffc | 2004-06-12 18:12:15 +0000 | [diff] [blame] | 1991 | /* The record is stored in a B-Tree */ |
drh | 536065a | 2005-01-26 21:55:31 +0000 | [diff] [blame] | 1992 | rc = sqlite3VdbeCursorMoveto(pC); |
drh | 52f159e | 2005-01-27 00:33:21 +0000 | [diff] [blame] | 1993 | if( rc ) goto abort_due_to_error; |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 1994 | zRec = 0; |
| 1995 | pCrsr = pC->pCursor; |
| 1996 | if( pC->nullRow ){ |
| 1997 | payloadSize = 0; |
drh | 76873ab | 2006-01-07 18:48:26 +0000 | [diff] [blame] | 1998 | }else if( pC->cacheStatus==p->cacheCtr ){ |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 1999 | payloadSize = pC->payloadSize; |
drh | 2646da7 | 2005-12-09 20:02:05 +0000 | [diff] [blame] | 2000 | zRec = (char*)pC->aRow; |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 2001 | }else if( pC->isIndex ){ |
danielk1977 | 96fc5fe | 2004-05-13 11:34:16 +0000 | [diff] [blame] | 2002 | i64 payloadSize64; |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2003 | sqlite3BtreeKeySize(pCrsr, &payloadSize64); |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 2004 | payloadSize = (int)payloadSize64; |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2005 | }else{ |
danielk1977 | 64202cf | 2008-11-17 15:31:47 +0000 | [diff] [blame] | 2006 | sqlite3BtreeDataSize(pCrsr, (u32 *)&payloadSize); |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2007 | } |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 2008 | nField = pC->nField; |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 2009 | }else{ |
| 2010 | assert( pC->pseudoTable ); |
drh | e61cffc | 2004-06-12 18:12:15 +0000 | [diff] [blame] | 2011 | /* The record is the sole entry of a pseudo-table */ |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2012 | payloadSize = pC->nData; |
| 2013 | zRec = pC->pData; |
drh | 76873ab | 2006-01-07 18:48:26 +0000 | [diff] [blame] | 2014 | pC->cacheStatus = CACHE_STALE; |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2015 | assert( payloadSize==0 || zRec!=0 ); |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 2016 | nField = pC->nField; |
danielk1977 | f7df9cc | 2004-06-16 12:02:47 +0000 | [diff] [blame] | 2017 | pCrsr = 0; |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2018 | } |
| 2019 | |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2020 | /* If payloadSize is 0, then just store a NULL */ |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2021 | if( payloadSize==0 ){ |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 2022 | assert( pDest->flags&MEM_Null ); |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 2023 | goto op_column_out; |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2024 | } |
drh | bb4957f | 2008-03-20 14:03:29 +0000 | [diff] [blame] | 2025 | if( payloadSize>db->aLimit[SQLITE_LIMIT_LENGTH] ){ |
drh | 023ae03 | 2007-05-08 12:12:16 +0000 | [diff] [blame] | 2026 | goto too_big; |
| 2027 | } |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2028 | |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 2029 | assert( p2<nField ); |
danielk1977 | b4964b7 | 2004-05-18 01:23:38 +0000 | [diff] [blame] | 2030 | |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2031 | /* Read and parse the table header. Store the results of the parse |
| 2032 | ** into the record header cache fields of the cursor. |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2033 | */ |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 2034 | aType = pC->aType; |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 2035 | if( pC->cacheStatus==p->cacheCtr ){ |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 2036 | aOffset = pC->aOffset; |
| 2037 | }else{ |
danielk1977 | dedf45b | 2006-01-13 17:12:01 +0000 | [diff] [blame] | 2038 | u8 *zIdx; /* Index into header */ |
| 2039 | u8 *zEndHdr; /* Pointer to first byte after the header */ |
danielk1977 | 64202cf | 2008-11-17 15:31:47 +0000 | [diff] [blame] | 2040 | int offset; /* Offset into the data */ |
drh | 0ac0719 | 2006-02-10 14:02:07 +0000 | [diff] [blame] | 2041 | int szHdrSz; /* Size of the header size field at start of record */ |
drh | b27b7f5 | 2008-12-10 18:03:45 +0000 | [diff] [blame] | 2042 | int avail = 0; /* Number of bytes of available data */ |
drh | b73857f | 2006-03-17 00:25:59 +0000 | [diff] [blame] | 2043 | |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 2044 | assert(aType); |
drh | b73857f | 2006-03-17 00:25:59 +0000 | [diff] [blame] | 2045 | pC->aOffset = aOffset = &aType[nField]; |
| 2046 | pC->payloadSize = payloadSize; |
| 2047 | pC->cacheStatus = p->cacheCtr; |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2048 | |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 2049 | /* Figure out how many bytes are in the header */ |
danielk1977 | 84ac9d0 | 2004-05-18 09:58:06 +0000 | [diff] [blame] | 2050 | if( zRec ){ |
| 2051 | zData = zRec; |
| 2052 | }else{ |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 2053 | if( pC->isIndex ){ |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 2054 | zData = (char*)sqlite3BtreeKeyFetch(pCrsr, &avail); |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 2055 | }else{ |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 2056 | zData = (char*)sqlite3BtreeDataFetch(pCrsr, &avail); |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2057 | } |
drh | e61cffc | 2004-06-12 18:12:15 +0000 | [diff] [blame] | 2058 | /* If KeyFetch()/DataFetch() managed to get the entire payload, |
| 2059 | ** save the payload in the pC->aRow cache. That will save us from |
| 2060 | ** having to make additional calls to fetch the content portion of |
| 2061 | ** the record. |
| 2062 | */ |
| 2063 | if( avail>=payloadSize ){ |
drh | 2646da7 | 2005-12-09 20:02:05 +0000 | [diff] [blame] | 2064 | zRec = zData; |
| 2065 | pC->aRow = (u8*)zData; |
drh | e61cffc | 2004-06-12 18:12:15 +0000 | [diff] [blame] | 2066 | }else{ |
| 2067 | pC->aRow = 0; |
| 2068 | } |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 2069 | } |
drh | 588f5bc | 2007-01-02 18:41:54 +0000 | [diff] [blame] | 2070 | /* The following assert is true in all cases accept when |
| 2071 | ** the database file has been corrupted externally. |
| 2072 | ** assert( zRec!=0 || avail>=payloadSize || avail>=9 ); */ |
shane | 3f8d5cf | 2008-04-24 19:15:09 +0000 | [diff] [blame] | 2073 | szHdrSz = getVarint32((u8*)zData, offset); |
drh | e61cffc | 2004-06-12 18:12:15 +0000 | [diff] [blame] | 2074 | |
| 2075 | /* The KeyFetch() or DataFetch() above are fast and will get the entire |
| 2076 | ** record header in most cases. But they will fail to get the complete |
| 2077 | ** record header if the record header does not fit on a single page |
| 2078 | ** in the B-Tree. When that happens, use sqlite3VdbeMemFromBtree() to |
| 2079 | ** acquire the complete header text. |
| 2080 | */ |
danielk1977 | dedf45b | 2006-01-13 17:12:01 +0000 | [diff] [blame] | 2081 | if( !zRec && avail<offset ){ |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 2082 | sMem.flags = 0; |
| 2083 | sMem.db = 0; |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 2084 | rc = sqlite3VdbeMemFromBtree(pCrsr, 0, offset, pC->isIndex, &sMem); |
danielk1977 | 84ac9d0 | 2004-05-18 09:58:06 +0000 | [diff] [blame] | 2085 | if( rc!=SQLITE_OK ){ |
danielk1977 | 3c9cc8d | 2005-01-17 03:40:08 +0000 | [diff] [blame] | 2086 | goto op_column_out; |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2087 | } |
drh | b6f5452 | 2004-05-20 02:42:16 +0000 | [diff] [blame] | 2088 | zData = sMem.z; |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2089 | } |
drh | 0ac0719 | 2006-02-10 14:02:07 +0000 | [diff] [blame] | 2090 | zEndHdr = (u8 *)&zData[offset]; |
| 2091 | zIdx = (u8 *)&zData[szHdrSz]; |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2092 | |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 2093 | /* Scan the header and use it to fill in the aType[] and aOffset[] |
| 2094 | ** arrays. aType[i] will contain the type integer for the i-th |
| 2095 | ** column and aOffset[i] will contain the offset from the beginning |
| 2096 | ** 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] | 2097 | */ |
danielk1977 | dedf45b | 2006-01-13 17:12:01 +0000 | [diff] [blame] | 2098 | for(i=0; i<nField; i++){ |
| 2099 | if( zIdx<zEndHdr ){ |
| 2100 | aOffset[i] = offset; |
shane | 3f8d5cf | 2008-04-24 19:15:09 +0000 | [diff] [blame] | 2101 | zIdx += getVarint32(zIdx, aType[i]); |
danielk1977 | dedf45b | 2006-01-13 17:12:01 +0000 | [diff] [blame] | 2102 | offset += sqlite3VdbeSerialTypeLen(aType[i]); |
| 2103 | }else{ |
| 2104 | /* If i is less that nField, then there are less fields in this |
| 2105 | ** record than SetNumColumns indicated there are columns in the |
| 2106 | ** table. Set the offset for any extra columns not present in |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2107 | ** the record to 0. This tells code below to store a NULL |
| 2108 | ** instead of deserializing a value from the record. |
danielk1977 | dedf45b | 2006-01-13 17:12:01 +0000 | [diff] [blame] | 2109 | */ |
| 2110 | aOffset[i] = 0; |
| 2111 | } |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2112 | } |
danielk1977 | 5f09613 | 2008-03-28 15:44:09 +0000 | [diff] [blame] | 2113 | sqlite3VdbeMemRelease(&sMem); |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 2114 | sMem.flags = MEM_Null; |
| 2115 | |
danielk1977 | 9792eef | 2006-01-13 15:58:43 +0000 | [diff] [blame] | 2116 | /* If we have read more header data than was contained in the header, |
| 2117 | ** 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] | 2118 | ** record, or if the end of the last field appears to be before the end |
| 2119 | ** of the record (when all fields present), then we must be dealing |
| 2120 | ** with a corrupt database. |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 2121 | */ |
danielk1977 | fb8f2e2 | 2008-09-22 06:13:31 +0000 | [diff] [blame] | 2122 | if( zIdx>zEndHdr || offset>payloadSize |
| 2123 | || (zIdx==zEndHdr && offset!=payloadSize) ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 2124 | rc = SQLITE_CORRUPT_BKPT; |
danielk1977 | 3c9cc8d | 2005-01-17 03:40:08 +0000 | [diff] [blame] | 2125 | goto op_column_out; |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 2126 | } |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 2127 | } |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2128 | |
danielk1977 | 36963fd | 2005-02-19 08:18:05 +0000 | [diff] [blame] | 2129 | /* Get the column information. If aOffset[p2] is non-zero, then |
| 2130 | ** deserialize the value from the record. If aOffset[p2] is zero, |
| 2131 | ** then there are not enough fields in the record to satisfy the |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 2132 | ** 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] | 2133 | ** a pointer to a Mem object. |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2134 | */ |
danielk1977 | 36963fd | 2005-02-19 08:18:05 +0000 | [diff] [blame] | 2135 | if( aOffset[p2] ){ |
| 2136 | assert( rc==SQLITE_OK ); |
| 2137 | if( zRec ){ |
danielk1977 | 808ec7c | 2008-07-29 10:18:57 +0000 | [diff] [blame] | 2138 | sqlite3VdbeMemReleaseExternal(pDest); |
| 2139 | sqlite3VdbeSerialGet((u8 *)&zRec[aOffset[p2]], aType[p2], pDest); |
danielk1977 | 36963fd | 2005-02-19 08:18:05 +0000 | [diff] [blame] | 2140 | }else{ |
| 2141 | len = sqlite3VdbeSerialTypeLen(aType[p2]); |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 2142 | sqlite3VdbeMemMove(&sMem, pDest); |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 2143 | rc = sqlite3VdbeMemFromBtree(pCrsr, aOffset[p2], len, pC->isIndex, &sMem); |
danielk1977 | 36963fd | 2005-02-19 08:18:05 +0000 | [diff] [blame] | 2144 | if( rc!=SQLITE_OK ){ |
| 2145 | goto op_column_out; |
| 2146 | } |
| 2147 | zData = sMem.z; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 2148 | sqlite3VdbeSerialGet((u8*)zData, aType[p2], pDest); |
danielk1977 | 7701e81 | 2005-01-10 12:59:51 +0000 | [diff] [blame] | 2149 | } |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 2150 | pDest->enc = encoding; |
danielk1977 | 36963fd | 2005-02-19 08:18:05 +0000 | [diff] [blame] | 2151 | }else{ |
danielk1977 | 60585dd | 2008-01-03 08:08:40 +0000 | [diff] [blame] | 2152 | if( pOp->p4type==P4_MEM ){ |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 2153 | sqlite3VdbeMemShallowCopy(pDest, pOp->p4.pMem, MEM_Static); |
danielk1977 | aee18ef | 2005-03-09 12:26:50 +0000 | [diff] [blame] | 2154 | }else{ |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 2155 | assert( pDest->flags&MEM_Null ); |
danielk1977 | aee18ef | 2005-03-09 12:26:50 +0000 | [diff] [blame] | 2156 | } |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 2157 | } |
drh | febe106 | 2004-08-28 18:17:48 +0000 | [diff] [blame] | 2158 | |
| 2159 | /* If we dynamically allocated space to hold the data (in the |
| 2160 | ** sqlite3VdbeMemFromBtree() call above) then transfer control of that |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 2161 | ** dynamically allocated space over to the pDest structure. |
drh | febe106 | 2004-08-28 18:17:48 +0000 | [diff] [blame] | 2162 | ** This prevents a memory copy. |
| 2163 | */ |
danielk1977 | 5f09613 | 2008-03-28 15:44:09 +0000 | [diff] [blame] | 2164 | if( sMem.zMalloc ){ |
| 2165 | assert( sMem.z==sMem.zMalloc ); |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 2166 | assert( !(pDest->flags & MEM_Dyn) ); |
| 2167 | assert( !(pDest->flags & (MEM_Blob|MEM_Str)) || pDest->z==sMem.z ); |
| 2168 | pDest->flags &= ~(MEM_Ephem|MEM_Static); |
danielk1977 | 5f09613 | 2008-03-28 15:44:09 +0000 | [diff] [blame] | 2169 | pDest->flags |= MEM_Term; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 2170 | pDest->z = sMem.z; |
danielk1977 | 5f09613 | 2008-03-28 15:44:09 +0000 | [diff] [blame] | 2171 | pDest->zMalloc = sMem.zMalloc; |
danielk1977 | b1bc953 | 2004-05-22 03:05:33 +0000 | [diff] [blame] | 2172 | } |
drh | febe106 | 2004-08-28 18:17:48 +0000 | [diff] [blame] | 2173 | |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 2174 | rc = sqlite3VdbeMemMakeWriteable(pDest); |
drh | d3194f5 | 2004-05-27 19:59:32 +0000 | [diff] [blame] | 2175 | |
danielk1977 | 3c9cc8d | 2005-01-17 03:40:08 +0000 | [diff] [blame] | 2176 | op_column_out: |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 2177 | UPDATE_MAX_BLOBSIZE(pDest); |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 2178 | REGISTER_TRACE(pOp->p3, pDest); |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2179 | break; |
| 2180 | } |
| 2181 | |
danielk1977 | 751de56 | 2008-04-18 09:01:15 +0000 | [diff] [blame] | 2182 | /* Opcode: Affinity P1 P2 * P4 * |
| 2183 | ** |
| 2184 | ** Apply affinities to a range of P2 registers starting with P1. |
| 2185 | ** |
| 2186 | ** P4 is a string that is P2 characters long. The nth character of the |
| 2187 | ** string indicates the column affinity that should be used for the nth |
| 2188 | ** memory cell in the range. |
| 2189 | */ |
| 2190 | case OP_Affinity: { |
| 2191 | char *zAffinity = pOp->p4.z; |
| 2192 | Mem *pData0 = &p->aMem[pOp->p1]; |
| 2193 | Mem *pLast = &pData0[pOp->p2-1]; |
| 2194 | Mem *pRec; |
| 2195 | |
| 2196 | for(pRec=pData0; pRec<=pLast; pRec++){ |
danielk1977 | b790c6c | 2008-04-18 10:25:24 +0000 | [diff] [blame] | 2197 | ExpandBlob(pRec); |
danielk1977 | 751de56 | 2008-04-18 09:01:15 +0000 | [diff] [blame] | 2198 | applyAffinity(pRec, zAffinity[pRec-pData0], encoding); |
| 2199 | } |
| 2200 | break; |
| 2201 | } |
| 2202 | |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2203 | /* Opcode: MakeRecord P1 P2 P3 P4 * |
drh | 7a224de | 2004-06-02 01:22:02 +0000 | [diff] [blame] | 2204 | ** |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2205 | ** Convert P2 registers beginning with P1 into a single entry |
drh | 7a224de | 2004-06-02 01:22:02 +0000 | [diff] [blame] | 2206 | ** 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] | 2207 | ** in an index. The details of the format are irrelevant as long as |
drh | 1e968a0 | 2008-03-25 00:22:21 +0000 | [diff] [blame] | 2208 | ** the OP_Column opcode can decode the record later. |
| 2209 | ** Refer to source code comments for the details of the record |
drh | 7a224de | 2004-06-02 01:22:02 +0000 | [diff] [blame] | 2210 | ** format. |
| 2211 | ** |
danielk1977 | 751de56 | 2008-04-18 09:01:15 +0000 | [diff] [blame] | 2212 | ** 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] | 2213 | ** string indicates the column affinity that should be used for the nth |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2214 | ** field of the index key. |
drh | 7a224de | 2004-06-02 01:22:02 +0000 | [diff] [blame] | 2215 | ** |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 2216 | ** The mapping from character to affinity is given by the SQLITE_AFF_ |
| 2217 | ** macros defined in sqliteInt.h. |
drh | 7a224de | 2004-06-02 01:22:02 +0000 | [diff] [blame] | 2218 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 2219 | ** If P4 is NULL then all index fields have the affinity NONE. |
drh | 7f057c9 | 2005-06-24 03:53:06 +0000 | [diff] [blame] | 2220 | */ |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2221 | case OP_MakeRecord: { |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 2222 | /* Assuming the record contains N fields, the record format looks |
| 2223 | ** like this: |
| 2224 | ** |
drh | 7a224de | 2004-06-02 01:22:02 +0000 | [diff] [blame] | 2225 | ** ------------------------------------------------------------------------ |
| 2226 | ** | hdr-size | type 0 | type 1 | ... | type N-1 | data0 | ... | data N-1 | |
| 2227 | ** ------------------------------------------------------------------------ |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 2228 | ** |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2229 | ** Data(0) is taken from register P1. Data(1) comes from register P1+1 |
| 2230 | ** and so froth. |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 2231 | ** |
| 2232 | ** Each type field is a varint representing the serial type of the |
| 2233 | ** corresponding data element (see sqlite3VdbeSerialType()). The |
drh | 7a224de | 2004-06-02 01:22:02 +0000 | [diff] [blame] | 2234 | ** hdr-size field is also a varint which is the offset from the beginning |
| 2235 | ** of the record to data0. |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 2236 | */ |
drh | fdf972a | 2007-05-02 13:30:27 +0000 | [diff] [blame] | 2237 | u8 *zNewRecord; /* A buffer to hold the data for the new record */ |
| 2238 | Mem *pRec; /* The new record */ |
drh | 023ae03 | 2007-05-08 12:12:16 +0000 | [diff] [blame] | 2239 | u64 nData = 0; /* Number of bytes of data space */ |
danielk1977 | ededfd5 | 2004-06-17 07:53:01 +0000 | [diff] [blame] | 2240 | int nHdr = 0; /* Number of bytes of header space */ |
danielk1977 | 64202cf | 2008-11-17 15:31:47 +0000 | [diff] [blame] | 2241 | i64 nByte = 0; /* Data space required for this record */ |
drh | fdf972a | 2007-05-02 13:30:27 +0000 | [diff] [blame] | 2242 | int nZero = 0; /* Number of zero bytes at the end of the record */ |
drh | cb9882a | 2005-03-17 03:15:40 +0000 | [diff] [blame] | 2243 | int nVarint; /* Number of bytes in a varint */ |
danielk1977 | ededfd5 | 2004-06-17 07:53:01 +0000 | [diff] [blame] | 2244 | u32 serial_type; /* Type field */ |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2245 | Mem *pData0; /* First field to be combined into the record */ |
| 2246 | Mem *pLast; /* Last field of the record */ |
danielk1977 | ededfd5 | 2004-06-17 07:53:01 +0000 | [diff] [blame] | 2247 | int nField; /* Number of fields in the record */ |
danielk1977 | ededfd5 | 2004-06-17 07:53:01 +0000 | [diff] [blame] | 2248 | char *zAffinity; /* The affinity string for the record */ |
drh | d946db0 | 2005-12-29 19:23:06 +0000 | [diff] [blame] | 2249 | int file_format; /* File format to use for encoding */ |
drh | fdf972a | 2007-05-02 13:30:27 +0000 | [diff] [blame] | 2250 | int i; /* Space used in zNewRecord[] */ |
danielk1977 | ededfd5 | 2004-06-17 07:53:01 +0000 | [diff] [blame] | 2251 | |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2252 | nField = pOp->p1; |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 2253 | zAffinity = pOp->p4.z; |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2254 | assert( nField>0 && pOp->p2>0 && pOp->p2+nField<=p->nMem ); |
| 2255 | pData0 = &p->aMem[nField]; |
| 2256 | nField = pOp->p2; |
| 2257 | pLast = &pData0[nField-1]; |
drh | d946db0 | 2005-12-29 19:23:06 +0000 | [diff] [blame] | 2258 | file_format = p->minWriteFileFormat; |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 2259 | |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 2260 | /* Loop through the elements that will make up the record to figure |
| 2261 | ** out how much space is required for the new record. |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 2262 | */ |
drh | a2a49dc | 2008-01-02 14:28:13 +0000 | [diff] [blame] | 2263 | for(pRec=pData0; pRec<=pLast; pRec++){ |
drh | ae7e151 | 2007-05-02 16:51:59 +0000 | [diff] [blame] | 2264 | int len; |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 2265 | if( zAffinity ){ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 2266 | applyAffinity(pRec, zAffinity[pRec-pData0], encoding); |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 2267 | } |
danielk1977 | d908f5a | 2007-05-11 07:08:28 +0000 | [diff] [blame] | 2268 | if( pRec->flags&MEM_Zero && pRec->n>0 ){ |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 2269 | sqlite3VdbeMemExpandBlob(pRec); |
danielk1977 | d908f5a | 2007-05-11 07:08:28 +0000 | [diff] [blame] | 2270 | } |
drh | d946db0 | 2005-12-29 19:23:06 +0000 | [diff] [blame] | 2271 | serial_type = sqlite3VdbeSerialType(pRec, file_format); |
drh | ae7e151 | 2007-05-02 16:51:59 +0000 | [diff] [blame] | 2272 | len = sqlite3VdbeSerialTypeLen(serial_type); |
| 2273 | nData += len; |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 2274 | nHdr += sqlite3VarintLen(serial_type); |
drh | fdf972a | 2007-05-02 13:30:27 +0000 | [diff] [blame] | 2275 | if( pRec->flags & MEM_Zero ){ |
| 2276 | /* Only pure zero-filled BLOBs can be input to this Opcode. |
| 2277 | ** We do not allow blobs with a prefix and a zero-filled tail. */ |
drh | 8df3284 | 2008-12-09 02:51:23 +0000 | [diff] [blame] | 2278 | nZero += pRec->u.nZero; |
drh | ae7e151 | 2007-05-02 16:51:59 +0000 | [diff] [blame] | 2279 | }else if( len ){ |
drh | fdf972a | 2007-05-02 13:30:27 +0000 | [diff] [blame] | 2280 | nZero = 0; |
| 2281 | } |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 2282 | } |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 2283 | |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 2284 | /* Add the initial header varint and total the size */ |
drh | cb9882a | 2005-03-17 03:15:40 +0000 | [diff] [blame] | 2285 | nHdr += nVarint = sqlite3VarintLen(nHdr); |
| 2286 | if( nVarint<sqlite3VarintLen(nHdr) ){ |
| 2287 | nHdr++; |
| 2288 | } |
drh | fdf972a | 2007-05-02 13:30:27 +0000 | [diff] [blame] | 2289 | nByte = nHdr+nData-nZero; |
drh | bb4957f | 2008-03-20 14:03:29 +0000 | [diff] [blame] | 2290 | if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){ |
drh | 023ae03 | 2007-05-08 12:12:16 +0000 | [diff] [blame] | 2291 | goto too_big; |
| 2292 | } |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 2293 | |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 2294 | /* Make sure the output register has a buffer large enough to store |
| 2295 | ** the new record. The output register (pOp->p3) is not allowed to |
| 2296 | ** be one of the input registers (because the following call to |
| 2297 | ** sqlite3VdbeMemGrow() could clobber the value before it is used). |
| 2298 | */ |
| 2299 | assert( pOp->p3<pOp->p1 || pOp->p3>=pOp->p1+pOp->p2 ); |
| 2300 | pOut = &p->aMem[pOp->p3]; |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 2301 | if( sqlite3VdbeMemGrow(pOut, (int)nByte, 0) ){ |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 2302 | goto no_mem; |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 2303 | } |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 2304 | zNewRecord = (u8 *)pOut->z; |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 2305 | |
| 2306 | /* Write the record */ |
shane | 3f8d5cf | 2008-04-24 19:15:09 +0000 | [diff] [blame] | 2307 | i = putVarint32(zNewRecord, nHdr); |
drh | a2a49dc | 2008-01-02 14:28:13 +0000 | [diff] [blame] | 2308 | for(pRec=pData0; pRec<=pLast; pRec++){ |
drh | d946db0 | 2005-12-29 19:23:06 +0000 | [diff] [blame] | 2309 | serial_type = sqlite3VdbeSerialType(pRec, file_format); |
shane | 3f8d5cf | 2008-04-24 19:15:09 +0000 | [diff] [blame] | 2310 | i += putVarint32(&zNewRecord[i], serial_type); /* serial type */ |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 2311 | } |
drh | a2a49dc | 2008-01-02 14:28:13 +0000 | [diff] [blame] | 2312 | for(pRec=pData0; pRec<=pLast; pRec++){ /* serial data */ |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 2313 | i += sqlite3VdbeSerialPut(&zNewRecord[i], (int)(nByte-i), pRec,file_format); |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 2314 | } |
drh | fdf972a | 2007-05-02 13:30:27 +0000 | [diff] [blame] | 2315 | assert( i==nByte ); |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 2316 | |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2317 | assert( pOp->p3>0 && pOp->p3<=p->nMem ); |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 2318 | pOut->n = (int)nByte; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 2319 | pOut->flags = MEM_Blob | MEM_Dyn; |
| 2320 | pOut->xDel = 0; |
drh | fdf972a | 2007-05-02 13:30:27 +0000 | [diff] [blame] | 2321 | if( nZero ){ |
drh | 8df3284 | 2008-12-09 02:51:23 +0000 | [diff] [blame] | 2322 | pOut->u.nZero = nZero; |
drh | 477df4b | 2008-01-05 18:48:24 +0000 | [diff] [blame] | 2323 | pOut->flags |= MEM_Zero; |
drh | fdf972a | 2007-05-02 13:30:27 +0000 | [diff] [blame] | 2324 | } |
drh | 477df4b | 2008-01-05 18:48:24 +0000 | [diff] [blame] | 2325 | pOut->enc = SQLITE_UTF8; /* In case the blob is ever converted to text */ |
drh | 1013c93 | 2008-01-06 00:25:21 +0000 | [diff] [blame] | 2326 | REGISTER_TRACE(pOp->p3, pOut); |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 2327 | UPDATE_MAX_BLOBSIZE(pOut); |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 2328 | break; |
| 2329 | } |
| 2330 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 2331 | /* Opcode: Statement P1 * * * * |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2332 | ** |
drh | 7f0f12e | 2004-05-21 13:39:50 +0000 | [diff] [blame] | 2333 | ** Begin an individual statement transaction which is part of a larger |
drh | 82ed1e5 | 2008-04-25 12:25:42 +0000 | [diff] [blame] | 2334 | ** transaction. This is needed so that the statement |
drh | 7f0f12e | 2004-05-21 13:39:50 +0000 | [diff] [blame] | 2335 | ** can be rolled back after an error without having to roll back the |
| 2336 | ** entire transaction. The statement transaction will automatically |
| 2337 | ** commit when the VDBE halts. |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2338 | ** |
drh | 82ed1e5 | 2008-04-25 12:25:42 +0000 | [diff] [blame] | 2339 | ** If the database connection is currently in autocommit mode (that |
| 2340 | ** is to say, if it is in between BEGIN and COMMIT) |
| 2341 | ** and if there are no other active statements on the same database |
| 2342 | ** connection, then this operation is a no-op. No statement transaction |
| 2343 | ** is needed since any error can use the normal ROLLBACK process to |
| 2344 | ** undo changes. |
| 2345 | ** |
| 2346 | ** If a statement transaction is started, then a statement journal file |
| 2347 | ** will be allocated and initialized. |
| 2348 | ** |
drh | 7f0f12e | 2004-05-21 13:39:50 +0000 | [diff] [blame] | 2349 | ** The statement is begun on the database file with index P1. The main |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2350 | ** database file has an index of 0 and the file used for temporary tables |
| 2351 | ** has an index of 1. |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2352 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2353 | case OP_Statement: { |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 2354 | if( db->autoCommit==0 || db->activeVdbeCnt>1 ){ |
| 2355 | int i = pOp->p1; |
| 2356 | Btree *pBt; |
| 2357 | assert( i>=0 && i<db->nDb ); |
| 2358 | assert( db->aDb[i].pBt!=0 ); |
| 2359 | pBt = db->aDb[i].pBt; |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 2360 | assert( sqlite3BtreeIsInTrans(pBt) ); |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 2361 | assert( (p->btreeMask & (1<<i))!=0 ); |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 2362 | if( !sqlite3BtreeIsInStmt(pBt) ){ |
| 2363 | rc = sqlite3BtreeBeginStmt(pBt); |
danielk1977 | 182c4ba | 2007-06-27 15:53:34 +0000 | [diff] [blame] | 2364 | p->openedStatement = 1; |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 2365 | } |
| 2366 | } |
| 2367 | break; |
| 2368 | } |
| 2369 | |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2370 | /* Opcode: Savepoint P1 * * P4 * |
| 2371 | ** |
| 2372 | ** Open, release or rollback the savepoint named by parameter P4, depending |
| 2373 | ** on the value of P1. To open a new savepoint, P1==0. To release (commit) an |
| 2374 | ** existing savepoint, P1==1, or to rollback an existing savepoint P1==2. |
| 2375 | */ |
| 2376 | case OP_Savepoint: { |
| 2377 | int p1 = pOp->p1; |
| 2378 | char *zName = pOp->p4.z; /* Name of savepoint */ |
| 2379 | |
| 2380 | /* Assert that the p1 parameter is valid. Also that if there is no open |
| 2381 | ** transaction, then there cannot be any savepoints. |
| 2382 | */ |
| 2383 | assert( db->pSavepoint==0 || db->autoCommit==0 ); |
| 2384 | assert( p1==SAVEPOINT_BEGIN||p1==SAVEPOINT_RELEASE||p1==SAVEPOINT_ROLLBACK ); |
| 2385 | assert( db->pSavepoint || db->isTransactionSavepoint==0 ); |
| 2386 | assert( checkSavepointCount(db) ); |
| 2387 | |
| 2388 | if( p1==SAVEPOINT_BEGIN ){ |
danielk1977 | 34cf35d | 2008-12-18 18:31:38 +0000 | [diff] [blame] | 2389 | if( db->writeVdbeCnt>0 ){ |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2390 | /* A new savepoint cannot be created if there are active write |
| 2391 | ** statements (i.e. open read/write incremental blob handles). |
| 2392 | */ |
| 2393 | sqlite3SetString(&p->zErrMsg, db, "cannot open savepoint - " |
| 2394 | "SQL statements in progress"); |
| 2395 | rc = SQLITE_BUSY; |
| 2396 | }else{ |
| 2397 | int nName = sqlite3Strlen30(zName); |
| 2398 | Savepoint *pNew; |
| 2399 | |
| 2400 | /* Create a new savepoint structure. */ |
| 2401 | pNew = sqlite3DbMallocRaw(db, sizeof(Savepoint)+nName+1); |
| 2402 | if( pNew ){ |
| 2403 | pNew->zName = (char *)&pNew[1]; |
| 2404 | memcpy(pNew->zName, zName, nName+1); |
| 2405 | |
| 2406 | /* If there is no open transaction, then mark this as a special |
| 2407 | ** "transaction savepoint". */ |
| 2408 | if( db->autoCommit ){ |
| 2409 | db->autoCommit = 0; |
| 2410 | db->isTransactionSavepoint = 1; |
| 2411 | }else{ |
| 2412 | db->nSavepoint++; |
| 2413 | } |
| 2414 | |
| 2415 | /* Link the new savepoint into the database handle's list. */ |
| 2416 | pNew->pNext = db->pSavepoint; |
| 2417 | db->pSavepoint = pNew; |
| 2418 | } |
| 2419 | } |
| 2420 | }else{ |
| 2421 | Savepoint *pSavepoint; |
| 2422 | int iSavepoint = 0; |
| 2423 | |
| 2424 | /* Find the named savepoint. If there is no such savepoint, then an |
| 2425 | ** an error is returned to the user. */ |
| 2426 | for( |
| 2427 | pSavepoint=db->pSavepoint; |
| 2428 | pSavepoint && sqlite3StrICmp(pSavepoint->zName, zName); |
| 2429 | pSavepoint=pSavepoint->pNext |
| 2430 | ){ |
| 2431 | iSavepoint++; |
| 2432 | } |
| 2433 | if( !pSavepoint ){ |
| 2434 | sqlite3SetString(&p->zErrMsg, db, "no such savepoint: %s", zName); |
| 2435 | rc = SQLITE_ERROR; |
| 2436 | }else if( |
| 2437 | db->writeVdbeCnt>0 || (p1==SAVEPOINT_ROLLBACK && db->activeVdbeCnt>1) |
| 2438 | ){ |
| 2439 | /* It is not possible to release (commit) a savepoint if there are |
| 2440 | ** active write statements. It is not possible to rollback a savepoint |
| 2441 | ** if there are any active statements at all. |
| 2442 | */ |
| 2443 | sqlite3SetString(&p->zErrMsg, db, |
| 2444 | "cannot %s savepoint - SQL statements in progress", |
| 2445 | (p1==SAVEPOINT_ROLLBACK ? "rollback": "release") |
| 2446 | ); |
| 2447 | rc = SQLITE_BUSY; |
| 2448 | }else{ |
| 2449 | |
| 2450 | /* Determine whether or not this is a transaction savepoint. If so, |
danielk1977 | 34cf35d | 2008-12-18 18:31:38 +0000 | [diff] [blame] | 2451 | ** and this is a RELEASE command, then the current transaction |
| 2452 | ** is committed. |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2453 | */ |
| 2454 | int isTransaction = pSavepoint->pNext==0 && db->isTransactionSavepoint; |
| 2455 | if( isTransaction && p1==SAVEPOINT_RELEASE ){ |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2456 | db->autoCommit = 1; |
| 2457 | if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){ |
| 2458 | p->pc = pc; |
| 2459 | db->autoCommit = 0; |
| 2460 | p->rc = rc = SQLITE_BUSY; |
| 2461 | goto vdbe_return; |
| 2462 | } |
danielk1977 | 34cf35d | 2008-12-18 18:31:38 +0000 | [diff] [blame] | 2463 | db->isTransactionSavepoint = 0; |
| 2464 | rc = p->rc; |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2465 | }else{ |
| 2466 | int ii; |
| 2467 | iSavepoint = db->nSavepoint - iSavepoint - 1; |
| 2468 | for(ii=0; ii<db->nDb; ii++){ |
| 2469 | rc = sqlite3BtreeSavepoint(db->aDb[ii].pBt, p1, iSavepoint); |
| 2470 | if( rc!=SQLITE_OK ){ |
| 2471 | goto abort_due_to_error; |
| 2472 | } |
| 2473 | } |
| 2474 | if( p1==SAVEPOINT_ROLLBACK && db->flags&SQLITE_InternChanges ){ |
| 2475 | sqlite3ExpirePreparedStatements(db); |
| 2476 | sqlite3ResetInternalSchema(db, 0); |
| 2477 | } |
| 2478 | } |
| 2479 | |
| 2480 | /* Regardless of whether this is a RELEASE or ROLLBACK, destroy all |
| 2481 | ** savepoints nested inside of the savepoint being operated on. */ |
| 2482 | while( db->pSavepoint!=pSavepoint ){ |
| 2483 | Savepoint *pTmp = db->pSavepoint; |
| 2484 | db->pSavepoint = pTmp->pNext; |
| 2485 | sqlite3DbFree(db, pTmp); |
| 2486 | db->nSavepoint--; |
| 2487 | } |
| 2488 | |
| 2489 | /* If it is a RELEASE, then destroy the savepoint being operated on too */ |
| 2490 | if( p1==SAVEPOINT_RELEASE ){ |
| 2491 | assert( pSavepoint==db->pSavepoint ); |
| 2492 | db->pSavepoint = pSavepoint->pNext; |
| 2493 | sqlite3DbFree(db, pSavepoint); |
| 2494 | if( !isTransaction ){ |
| 2495 | db->nSavepoint--; |
| 2496 | } |
| 2497 | } |
| 2498 | } |
| 2499 | } |
| 2500 | |
| 2501 | break; |
| 2502 | } |
| 2503 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 2504 | /* Opcode: AutoCommit P1 P2 * * * |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 2505 | ** |
| 2506 | ** 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] | 2507 | ** back any currently active btree transactions. If there are any active |
| 2508 | ** VMs (apart from this one), then the COMMIT or ROLLBACK statement fails. |
drh | 92f02c3 | 2004-09-02 14:57:08 +0000 | [diff] [blame] | 2509 | ** |
| 2510 | ** This instruction causes the VM to halt. |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 2511 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2512 | case OP_AutoCommit: { |
drh | ad4a4b8 | 2008-11-05 16:37:34 +0000 | [diff] [blame] | 2513 | int desiredAutoCommit = pOp->p1; |
| 2514 | int rollback = pOp->p2; |
| 2515 | int turnOnAC = desiredAutoCommit && !db->autoCommit; |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 2516 | |
drh | ad4a4b8 | 2008-11-05 16:37:34 +0000 | [diff] [blame] | 2517 | assert( desiredAutoCommit==1 || desiredAutoCommit==0 ); |
| 2518 | assert( desiredAutoCommit==1 || rollback==0 ); |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 2519 | |
drh | 92f02c3 | 2004-09-02 14:57:08 +0000 | [diff] [blame] | 2520 | assert( db->activeVdbeCnt>0 ); /* At least this one VM is active */ |
danielk1977 | 46c43ed | 2004-06-30 06:30:25 +0000 | [diff] [blame] | 2521 | |
drh | ad4a4b8 | 2008-11-05 16:37:34 +0000 | [diff] [blame] | 2522 | if( turnOnAC && rollback && db->activeVdbeCnt>1 ){ |
| 2523 | /* If this instruction implements a ROLLBACK and other VMs are |
danielk1977 | 46c43ed | 2004-06-30 06:30:25 +0000 | [diff] [blame] | 2524 | ** still running, and a transaction is active, return an error indicating |
| 2525 | ** that the other VMs must complete first. |
| 2526 | */ |
drh | ad4a4b8 | 2008-11-05 16:37:34 +0000 | [diff] [blame] | 2527 | sqlite3SetString(&p->zErrMsg, db, "cannot rollback transaction - " |
| 2528 | "SQL statements in progress"); |
drh | 99dfe5e | 2008-10-30 15:03:15 +0000 | [diff] [blame] | 2529 | rc = SQLITE_BUSY; |
drh | ad4a4b8 | 2008-11-05 16:37:34 +0000 | [diff] [blame] | 2530 | }else if( turnOnAC && !rollback && db->writeVdbeCnt>1 ){ |
| 2531 | /* If this instruction implements a COMMIT and other VMs are writing |
| 2532 | ** return an error indicating that the other VMs must complete first. |
| 2533 | */ |
| 2534 | sqlite3SetString(&p->zErrMsg, db, "cannot commit transaction - " |
| 2535 | "SQL statements in progress"); |
| 2536 | rc = SQLITE_BUSY; |
| 2537 | }else if( desiredAutoCommit!=db->autoCommit ){ |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2538 | if( rollback ){ |
drh | ad4a4b8 | 2008-11-05 16:37:34 +0000 | [diff] [blame] | 2539 | assert( desiredAutoCommit==1 ); |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 2540 | sqlite3RollbackAll(db); |
danielk1977 | f3f06bb | 2005-12-16 15:24:28 +0000 | [diff] [blame] | 2541 | db->autoCommit = 1; |
| 2542 | }else{ |
shane | 7d3846a | 2008-12-11 02:58:26 +0000 | [diff] [blame] | 2543 | db->autoCommit = (u8)desiredAutoCommit; |
danielk1977 | f3f06bb | 2005-12-16 15:24:28 +0000 | [diff] [blame] | 2544 | if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){ |
danielk1977 | f3f06bb | 2005-12-16 15:24:28 +0000 | [diff] [blame] | 2545 | p->pc = pc; |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 2546 | db->autoCommit = (u8)(1-desiredAutoCommit); |
drh | 900b31e | 2007-08-28 02:27:51 +0000 | [diff] [blame] | 2547 | p->rc = rc = SQLITE_BUSY; |
| 2548 | goto vdbe_return; |
danielk1977 | f3f06bb | 2005-12-16 15:24:28 +0000 | [diff] [blame] | 2549 | } |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 2550 | } |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2551 | sqlite3CloseSavepoints(db); |
drh | 83968c4 | 2007-04-18 16:45:24 +0000 | [diff] [blame] | 2552 | if( p->rc==SQLITE_OK ){ |
drh | 900b31e | 2007-08-28 02:27:51 +0000 | [diff] [blame] | 2553 | rc = SQLITE_DONE; |
drh | 83968c4 | 2007-04-18 16:45:24 +0000 | [diff] [blame] | 2554 | }else{ |
drh | 900b31e | 2007-08-28 02:27:51 +0000 | [diff] [blame] | 2555 | rc = SQLITE_ERROR; |
drh | 83968c4 | 2007-04-18 16:45:24 +0000 | [diff] [blame] | 2556 | } |
drh | 900b31e | 2007-08-28 02:27:51 +0000 | [diff] [blame] | 2557 | goto vdbe_return; |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 2558 | }else{ |
drh | f089aa4 | 2008-07-08 19:34:06 +0000 | [diff] [blame] | 2559 | sqlite3SetString(&p->zErrMsg, db, |
drh | ad4a4b8 | 2008-11-05 16:37:34 +0000 | [diff] [blame] | 2560 | (!desiredAutoCommit)?"cannot start a transaction within a transaction":( |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 2561 | (rollback)?"cannot rollback - no transaction is active": |
drh | f089aa4 | 2008-07-08 19:34:06 +0000 | [diff] [blame] | 2562 | "cannot commit - no transaction is active")); |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 2563 | |
| 2564 | rc = SQLITE_ERROR; |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2565 | } |
| 2566 | break; |
| 2567 | } |
| 2568 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 2569 | /* Opcode: Transaction P1 P2 * * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2570 | ** |
| 2571 | ** Begin a transaction. The transaction ends when a Commit or Rollback |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2572 | ** opcode is encountered. Depending on the ON CONFLICT setting, the |
| 2573 | ** transaction might also be rolled back if an error is encountered. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2574 | ** |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2575 | ** P1 is the index of the database file on which the transaction is |
| 2576 | ** started. Index 0 is the main database file and index 1 is the |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 2577 | ** file used for temporary tables. Indices of 2 or more are used for |
| 2578 | ** attached databases. |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 2579 | ** |
drh | 8024205 | 2004-06-09 00:48:12 +0000 | [diff] [blame] | 2580 | ** 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] | 2581 | ** obtained on the database file when a write-transaction is started. No |
drh | 8024205 | 2004-06-09 00:48:12 +0000 | [diff] [blame] | 2582 | ** other process can start another write transaction while this transaction is |
| 2583 | ** underway. Starting a write transaction also creates a rollback journal. A |
| 2584 | ** write transaction must be started before any changes can be made to the |
drh | 684917c | 2004-10-05 02:41:42 +0000 | [diff] [blame] | 2585 | ** database. If P2 is 2 or greater then an EXCLUSIVE lock is also obtained |
| 2586 | ** on the file. |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 2587 | ** |
| 2588 | ** 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] | 2589 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2590 | case OP_Transaction: { |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2591 | int i = pOp->p1; |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 2592 | Btree *pBt; |
| 2593 | |
drh | 8bf8dc9 | 2003-05-17 17:35:10 +0000 | [diff] [blame] | 2594 | assert( i>=0 && i<db->nDb ); |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 2595 | assert( (p->btreeMask & (1<<i))!=0 ); |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 2596 | pBt = db->aDb[i].pBt; |
| 2597 | |
danielk1977 | 24162fe | 2004-06-04 06:22:00 +0000 | [diff] [blame] | 2598 | if( pBt ){ |
danielk1977 | 40b38dc | 2004-06-26 08:38:24 +0000 | [diff] [blame] | 2599 | rc = sqlite3BtreeBeginTrans(pBt, pOp->p2); |
danielk1977 | 24162fe | 2004-06-04 06:22:00 +0000 | [diff] [blame] | 2600 | if( rc==SQLITE_BUSY ){ |
danielk1977 | 2a764eb | 2004-06-12 01:43:26 +0000 | [diff] [blame] | 2601 | p->pc = pc; |
drh | 900b31e | 2007-08-28 02:27:51 +0000 | [diff] [blame] | 2602 | p->rc = rc = SQLITE_BUSY; |
drh | 900b31e | 2007-08-28 02:27:51 +0000 | [diff] [blame] | 2603 | goto vdbe_return; |
danielk1977 | 24162fe | 2004-06-04 06:22:00 +0000 | [diff] [blame] | 2604 | } |
danielk1977 | 2ef6848 | 2008-07-07 17:13:08 +0000 | [diff] [blame] | 2605 | if( rc!=SQLITE_OK && rc!=SQLITE_READONLY /* && rc!=SQLITE_BUSY */ ){ |
danielk1977 | 24162fe | 2004-06-04 06:22:00 +0000 | [diff] [blame] | 2606 | goto abort_due_to_error; |
drh | 90bfcda | 2001-09-23 19:46:51 +0000 | [diff] [blame] | 2607 | } |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 2608 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2609 | break; |
| 2610 | } |
| 2611 | |
drh | b1fdb2a | 2008-01-05 04:06:03 +0000 | [diff] [blame] | 2612 | /* Opcode: ReadCookie P1 P2 P3 * * |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 2613 | ** |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2614 | ** Read cookie number P3 from database P1 and write it into register P2. |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 2615 | ** P3==0 is the schema version. P3==1 is the database format. |
| 2616 | ** P3==2 is the recommended pager cache size, and so forth. P1==0 is |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2617 | ** the main database file and P1==1 is the database file used to store |
| 2618 | ** temporary tables. |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 2619 | ** |
danielk1977 | 418899a | 2007-06-24 10:14:00 +0000 | [diff] [blame] | 2620 | ** If P1 is negative, then this is a request to read the size of a |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 2621 | ** databases free-list. P3 must be set to 1 in this case. The actual |
danielk1977 | 418899a | 2007-06-24 10:14:00 +0000 | [diff] [blame] | 2622 | ** database accessed is ((P1+1)*-1). For example, a P1 parameter of -1 |
danielk1977 | d62e76c | 2007-06-24 16:11:03 +0000 | [diff] [blame] | 2623 | ** corresponds to database 0 ("main"), a P1 of -2 is database 1 ("temp"). |
danielk1977 | 418899a | 2007-06-24 10:14:00 +0000 | [diff] [blame] | 2624 | ** |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 2625 | ** There must be a read-lock on the database (either a transaction |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2626 | ** must be started or there must be an open cursor) before |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 2627 | ** executing this instruction. |
| 2628 | */ |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 2629 | case OP_ReadCookie: { /* out2-prerelease */ |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 2630 | int iMeta; |
danielk1977 | 180b56a | 2007-06-24 08:00:42 +0000 | [diff] [blame] | 2631 | int iDb = pOp->p1; |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 2632 | int iCookie = pOp->p3; |
danielk1977 | 180b56a | 2007-06-24 08:00:42 +0000 | [diff] [blame] | 2633 | |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 2634 | assert( pOp->p3<SQLITE_N_BTREE_META ); |
danielk1977 | 180b56a | 2007-06-24 08:00:42 +0000 | [diff] [blame] | 2635 | if( iDb<0 ){ |
| 2636 | iDb = (-1*(iDb+1)); |
| 2637 | iCookie *= -1; |
| 2638 | } |
| 2639 | assert( iDb>=0 && iDb<db->nDb ); |
| 2640 | assert( db->aDb[iDb].pBt!=0 ); |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 2641 | assert( (p->btreeMask & (1<<iDb))!=0 ); |
drh | a3b321d | 2004-05-11 09:31:31 +0000 | [diff] [blame] | 2642 | /* The indexing of meta values at the schema layer is off by one from |
| 2643 | ** the indexing in the btree layer. The btree considers meta[0] to |
| 2644 | ** be the number of free pages in the database (a read-only value) |
| 2645 | ** and meta[1] to be the schema cookie. The schema layer considers |
| 2646 | ** meta[1] to be the schema cookie. So we have to shift the index |
| 2647 | ** by one in the following statement. |
| 2648 | */ |
danielk1977 | 180b56a | 2007-06-24 08:00:42 +0000 | [diff] [blame] | 2649 | rc = sqlite3BtreeGetMeta(db->aDb[iDb].pBt, 1 + iCookie, (u32 *)&iMeta); |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 2650 | pOut->u.i = iMeta; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 2651 | MemSetTypeFlag(pOut, MEM_Int); |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 2652 | break; |
| 2653 | } |
| 2654 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 2655 | /* Opcode: SetCookie P1 P2 P3 * * |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 2656 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 2657 | ** Write the content of register P3 (interpreted as an integer) |
| 2658 | ** into cookie number P2 of database P1. |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2659 | ** P2==0 is the schema version. P2==1 is the database format. |
| 2660 | ** P2==2 is the recommended pager cache size, and so forth. P1==0 is |
| 2661 | ** the main database file and P1==1 is the database file used to store |
| 2662 | ** temporary tables. |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 2663 | ** |
| 2664 | ** A transaction must be started before executing this opcode. |
| 2665 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2666 | case OP_SetCookie: { /* in3 */ |
drh | 3f7d4e4 | 2004-07-24 14:35:58 +0000 | [diff] [blame] | 2667 | Db *pDb; |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 2668 | assert( pOp->p2<SQLITE_N_BTREE_META ); |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2669 | assert( pOp->p1>=0 && pOp->p1<db->nDb ); |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 2670 | assert( (p->btreeMask & (1<<pOp->p1))!=0 ); |
drh | 3f7d4e4 | 2004-07-24 14:35:58 +0000 | [diff] [blame] | 2671 | pDb = &db->aDb[pOp->p1]; |
| 2672 | assert( pDb->pBt!=0 ); |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 2673 | sqlite3VdbeMemIntegerify(pIn3); |
drh | a3b321d | 2004-05-11 09:31:31 +0000 | [diff] [blame] | 2674 | /* See note about index shifting on OP_ReadCookie */ |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 2675 | rc = sqlite3BtreeUpdateMeta(pDb->pBt, 1+pOp->p2, (int)pIn3->u.i); |
drh | 3f7d4e4 | 2004-07-24 14:35:58 +0000 | [diff] [blame] | 2676 | if( pOp->p2==0 ){ |
| 2677 | /* When the schema cookie changes, record the new cookie internally */ |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 2678 | pDb->pSchema->schema_cookie = (int)pIn3->u.i; |
drh | 3f7d4e4 | 2004-07-24 14:35:58 +0000 | [diff] [blame] | 2679 | db->flags |= SQLITE_InternChanges; |
drh | d28bcb3 | 2005-12-21 14:43:11 +0000 | [diff] [blame] | 2680 | }else if( pOp->p2==1 ){ |
| 2681 | /* Record changes in the file format */ |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 2682 | pDb->pSchema->file_format = (u8)pIn3->u.i; |
drh | 3f7d4e4 | 2004-07-24 14:35:58 +0000 | [diff] [blame] | 2683 | } |
drh | fd426c6 | 2006-01-30 15:34:22 +0000 | [diff] [blame] | 2684 | if( pOp->p1==1 ){ |
| 2685 | /* Invalidate all prepared statements whenever the TEMP database |
| 2686 | ** schema is changed. Ticket #1644 */ |
| 2687 | sqlite3ExpirePreparedStatements(db); |
| 2688 | } |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 2689 | break; |
| 2690 | } |
| 2691 | |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 2692 | /* Opcode: VerifyCookie P1 P2 * |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 2693 | ** |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2694 | ** Check the value of global database parameter number 0 (the |
| 2695 | ** schema version) and make sure it is equal to P2. |
| 2696 | ** P1 is the database number which is 0 for the main database file |
| 2697 | ** and 1 for the file holding temporary tables and some higher number |
| 2698 | ** for auxiliary databases. |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 2699 | ** |
| 2700 | ** The cookie changes its value whenever the database schema changes. |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2701 | ** This operation is used to detect when that the cookie has changed |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 2702 | ** and that the current process needs to reread the schema. |
| 2703 | ** |
| 2704 | ** Either a transaction needs to have been started or an OP_Open needs |
| 2705 | ** to be executed (to establish a read lock) before this opcode is |
| 2706 | ** invoked. |
| 2707 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2708 | case OP_VerifyCookie: { |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 2709 | int iMeta; |
drh | c275b4e | 2004-07-19 17:25:24 +0000 | [diff] [blame] | 2710 | Btree *pBt; |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2711 | assert( pOp->p1>=0 && pOp->p1<db->nDb ); |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 2712 | assert( (p->btreeMask & (1<<pOp->p1))!=0 ); |
drh | c275b4e | 2004-07-19 17:25:24 +0000 | [diff] [blame] | 2713 | pBt = db->aDb[pOp->p1].pBt; |
| 2714 | if( pBt ){ |
| 2715 | rc = sqlite3BtreeGetMeta(pBt, 1, (u32 *)&iMeta); |
| 2716 | }else{ |
| 2717 | rc = SQLITE_OK; |
| 2718 | iMeta = 0; |
| 2719 | } |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 2720 | if( rc==SQLITE_OK && iMeta!=pOp->p2 ){ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 2721 | sqlite3DbFree(db, p->zErrMsg); |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 2722 | p->zErrMsg = sqlite3DbStrDup(db, "database schema has changed"); |
danielk1977 | 896e792 | 2007-04-17 08:32:33 +0000 | [diff] [blame] | 2723 | /* If the schema-cookie from the database file matches the cookie |
| 2724 | ** stored with the in-memory representation of the schema, do |
| 2725 | ** not reload the schema from the database file. |
| 2726 | ** |
shane | 21e7feb | 2008-05-30 15:59:49 +0000 | [diff] [blame] | 2727 | ** If virtual-tables are in use, this is not just an optimization. |
danielk1977 | 896e792 | 2007-04-17 08:32:33 +0000 | [diff] [blame] | 2728 | ** Often, v-tables store their data in other SQLite tables, which |
| 2729 | ** are queried from within xNext() and other v-table methods using |
| 2730 | ** prepared queries. If such a query is out-of-date, we do not want to |
| 2731 | ** discard the database schema, as the user code implementing the |
| 2732 | ** v-table would have to be ready for the sqlite3_vtab structure itself |
| 2733 | ** to be invalidated whenever sqlite3_step() is called from within |
| 2734 | ** a v-table method. |
| 2735 | */ |
| 2736 | if( db->aDb[pOp->p1].pSchema->schema_cookie!=iMeta ){ |
| 2737 | sqlite3ResetInternalSchema(db, pOp->p1); |
| 2738 | } |
| 2739 | |
drh | f6d8ab8 | 2007-01-12 23:43:42 +0000 | [diff] [blame] | 2740 | sqlite3ExpirePreparedStatements(db); |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 2741 | rc = SQLITE_SCHEMA; |
| 2742 | } |
| 2743 | break; |
| 2744 | } |
| 2745 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 2746 | /* Opcode: OpenRead P1 P2 P3 P4 P5 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2747 | ** |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2748 | ** Open a read-only cursor for the database table whose root page is |
danielk1977 | 207872a | 2008-01-03 07:54:23 +0000 | [diff] [blame] | 2749 | ** P2 in a database file. The database file is determined by P3. |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 2750 | ** P3==0 means the main database, P3==1 means the database used for |
| 2751 | ** temporary tables, and P3>1 means used the corresponding attached |
| 2752 | ** database. Give the new cursor an identifier of P1. The P1 |
danielk1977 | 207872a | 2008-01-03 07:54:23 +0000 | [diff] [blame] | 2753 | ** values need not be contiguous but all P1 values should be small integers. |
| 2754 | ** It is an error for P1 to be negative. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2755 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 2756 | ** If P5!=0 then use the content of register P2 as the root page, not |
| 2757 | ** the value of P2 itself. |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 2758 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2759 | ** There will be a read lock on the database whenever there is an |
| 2760 | ** open cursor. If the database was unlocked prior to this instruction |
| 2761 | ** then a read lock is acquired as part of this instruction. A read |
| 2762 | ** lock allows other processes to read the database but prohibits |
| 2763 | ** any other process from modifying the database. The read lock is |
| 2764 | ** released when all cursors are closed. If this instruction attempts |
| 2765 | ** to get a read lock but fails, the script terminates with an |
| 2766 | ** SQLITE_BUSY error code. |
| 2767 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 2768 | ** The P4 value is a pointer to a KeyInfo structure that defines the |
| 2769 | ** content and collating sequence of indices. P4 is NULL for cursors |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 2770 | ** that are not pointing to indices. |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 2771 | ** |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2772 | ** See also OpenWrite. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2773 | */ |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 2774 | /* Opcode: OpenWrite P1 P2 P3 P4 P5 |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2775 | ** |
| 2776 | ** 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] | 2777 | ** page is P2. Or if P5!=0 use the content of register P2 to find the |
| 2778 | ** root page. |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2779 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 2780 | ** The P4 value is a pointer to a KeyInfo structure that defines the |
| 2781 | ** content and collating sequence of indices. P4 is NULL for cursors |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 2782 | ** that are not pointing to indices. |
jplyon | 5a56422 | 2003-06-02 06:15:58 +0000 | [diff] [blame] | 2783 | ** |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2784 | ** This instruction works just like OpenRead except that it opens the cursor |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2785 | ** in read/write mode. For a given table, there can be one or more read-only |
| 2786 | ** cursors or a single read/write cursor but not both. |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 2787 | ** |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2788 | ** See also OpenRead. |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2789 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2790 | case OP_OpenRead: |
| 2791 | case OP_OpenWrite: { |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2792 | int i = pOp->p1; |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 2793 | int p2 = pOp->p2; |
danielk1977 | 207872a | 2008-01-03 07:54:23 +0000 | [diff] [blame] | 2794 | int iDb = pOp->p3; |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 2795 | int wrFlag; |
| 2796 | Btree *pX; |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 2797 | VdbeCursor *pCur; |
drh | d946db0 | 2005-12-29 19:23:06 +0000 | [diff] [blame] | 2798 | Db *pDb; |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2799 | |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 2800 | assert( iDb>=0 && iDb<db->nDb ); |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 2801 | assert( (p->btreeMask & (1<<iDb))!=0 ); |
drh | d946db0 | 2005-12-29 19:23:06 +0000 | [diff] [blame] | 2802 | pDb = &db->aDb[iDb]; |
| 2803 | pX = pDb->pBt; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 2804 | assert( pX!=0 ); |
drh | d946db0 | 2005-12-29 19:23:06 +0000 | [diff] [blame] | 2805 | if( pOp->opcode==OP_OpenWrite ){ |
| 2806 | wrFlag = 1; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2807 | if( pDb->pSchema->file_format < p->minWriteFileFormat ){ |
| 2808 | p->minWriteFileFormat = pDb->pSchema->file_format; |
drh | d946db0 | 2005-12-29 19:23:06 +0000 | [diff] [blame] | 2809 | } |
| 2810 | }else{ |
| 2811 | wrFlag = 0; |
| 2812 | } |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 2813 | if( pOp->p5 ){ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2814 | assert( p2>0 ); |
| 2815 | assert( p2<=p->nMem ); |
| 2816 | pIn2 = &p->aMem[p2]; |
| 2817 | sqlite3VdbeMemIntegerify(pIn2); |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 2818 | p2 = (int)pIn2->u.i; |
shane | dcc50b7 | 2008-11-13 18:29:50 +0000 | [diff] [blame] | 2819 | if( p2<2 ) { |
| 2820 | rc = SQLITE_CORRUPT_BKPT; |
| 2821 | goto abort_due_to_error; |
| 2822 | } |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 2823 | } |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 2824 | assert( i>=0 ); |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 2825 | pCur = allocateCursor(p, i, &pOp[-1], iDb, 1); |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 2826 | if( pCur==0 ) goto no_mem; |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 2827 | pCur->nullRow = 1; |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 2828 | rc = sqlite3BtreeCursor(pX, p2, wrFlag, pOp->p4.p, pCur->pCursor); |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 2829 | if( pOp->p4type==P4_KEYINFO ){ |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 2830 | pCur->pKeyInfo = pOp->p4.pKeyInfo; |
danielk1977 | 14db266 | 2006-01-09 16:12:04 +0000 | [diff] [blame] | 2831 | pCur->pKeyInfo->enc = ENC(p->db); |
danielk1977 | 24162fe | 2004-06-04 06:22:00 +0000 | [diff] [blame] | 2832 | }else{ |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 2833 | pCur->pKeyInfo = 0; |
danielk1977 | 24162fe | 2004-06-04 06:22:00 +0000 | [diff] [blame] | 2834 | } |
| 2835 | switch( rc ){ |
| 2836 | case SQLITE_BUSY: { |
danielk1977 | 2a764eb | 2004-06-12 01:43:26 +0000 | [diff] [blame] | 2837 | p->pc = pc; |
drh | 900b31e | 2007-08-28 02:27:51 +0000 | [diff] [blame] | 2838 | p->rc = rc = SQLITE_BUSY; |
drh | 900b31e | 2007-08-28 02:27:51 +0000 | [diff] [blame] | 2839 | goto vdbe_return; |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 2840 | } |
danielk1977 | 24162fe | 2004-06-04 06:22:00 +0000 | [diff] [blame] | 2841 | case SQLITE_OK: { |
| 2842 | int flags = sqlite3BtreeFlags(pCur->pCursor); |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 2843 | /* Sanity checking. Only the lower four bits of the flags byte should |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame] | 2844 | ** be used. Bit 3 (mask 0x08) is unpredictable. The lower 3 bits |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 2845 | ** (mask 0x07) should be either 5 (intkey+leafdata for tables) or |
| 2846 | ** 2 (zerodata for indices). If these conditions are not met it can |
| 2847 | ** only mean that we are dealing with a corrupt database file |
| 2848 | */ |
| 2849 | if( (flags & 0xf0)!=0 || ((flags & 0x07)!=5 && (flags & 0x07)!=2) ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 2850 | rc = SQLITE_CORRUPT_BKPT; |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 2851 | goto abort_due_to_error; |
| 2852 | } |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 2853 | pCur->isTable = (flags & BTREE_INTKEY)!=0 ?1:0; |
| 2854 | pCur->isIndex = (flags & BTREE_ZERODATA)!=0 ?1:0; |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 2855 | /* 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] | 2856 | ** we expect to be opening an index. If this is not what happened, |
| 2857 | ** then the database is corrupt |
| 2858 | */ |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 2859 | if( (pCur->isTable && pOp->p4type==P4_KEYINFO) |
| 2860 | || (pCur->isIndex && pOp->p4type!=P4_KEYINFO) ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 2861 | rc = SQLITE_CORRUPT_BKPT; |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 2862 | goto abort_due_to_error; |
| 2863 | } |
danielk1977 | 24162fe | 2004-06-04 06:22:00 +0000 | [diff] [blame] | 2864 | break; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2865 | } |
danielk1977 | 24162fe | 2004-06-04 06:22:00 +0000 | [diff] [blame] | 2866 | case SQLITE_EMPTY: { |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 2867 | pCur->isTable = pOp->p4type!=P4_KEYINFO; |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 2868 | pCur->isIndex = !pCur->isTable; |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 2869 | pCur->pCursor = 0; |
danielk1977 | 24162fe | 2004-06-04 06:22:00 +0000 | [diff] [blame] | 2870 | rc = SQLITE_OK; |
| 2871 | break; |
| 2872 | } |
| 2873 | default: { |
| 2874 | goto abort_due_to_error; |
| 2875 | } |
| 2876 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2877 | break; |
| 2878 | } |
| 2879 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 2880 | /* Opcode: OpenEphemeral P1 P2 * P4 * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2881 | ** |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 2882 | ** Open a new cursor P1 to a transient table. |
drh | 9170dd7 | 2005-07-08 17:13:46 +0000 | [diff] [blame] | 2883 | ** The cursor is always opened read/write even if |
| 2884 | ** the main database is read-only. The transient or virtual |
| 2885 | ** table is deleted automatically when the cursor is closed. |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 2886 | ** |
drh | 0342b1f | 2005-09-01 03:07:44 +0000 | [diff] [blame] | 2887 | ** P2 is the number of columns in the virtual table. |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 2888 | ** The cursor points to a BTree table if P4==0 and to a BTree index |
| 2889 | ** 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] | 2890 | ** that defines the format of keys in the index. |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 2891 | ** |
| 2892 | ** This opcode was once called OpenTemp. But that created |
| 2893 | ** confusion because the term "temp table", might refer either |
| 2894 | ** to a TEMP table at the SQL level, or to a table opened by |
| 2895 | ** this opcode. Then this opcode was call OpenVirtual. But |
| 2896 | ** that created confusion with the whole virtual-table idea. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2897 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2898 | case OP_OpenEphemeral: { |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2899 | int i = pOp->p1; |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 2900 | VdbeCursor *pCx; |
drh | 33f4e02 | 2007-09-03 15:19:34 +0000 | [diff] [blame] | 2901 | static const int openFlags = |
| 2902 | SQLITE_OPEN_READWRITE | |
| 2903 | SQLITE_OPEN_CREATE | |
| 2904 | SQLITE_OPEN_EXCLUSIVE | |
| 2905 | SQLITE_OPEN_DELETEONCLOSE | |
| 2906 | SQLITE_OPEN_TRANSIENT_DB; |
| 2907 | |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 2908 | assert( i>=0 ); |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 2909 | pCx = allocateCursor(p, i, pOp, -1, 1); |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 2910 | if( pCx==0 ) goto no_mem; |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 2911 | pCx->nullRow = 1; |
drh | 33f4e02 | 2007-09-03 15:19:34 +0000 | [diff] [blame] | 2912 | rc = sqlite3BtreeFactory(db, 0, 1, SQLITE_DEFAULT_TEMP_CACHE_SIZE, openFlags, |
| 2913 | &pCx->pBt); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2914 | if( rc==SQLITE_OK ){ |
danielk1977 | 40b38dc | 2004-06-26 08:38:24 +0000 | [diff] [blame] | 2915 | rc = sqlite3BtreeBeginTrans(pCx->pBt, 1); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2916 | } |
| 2917 | if( rc==SQLITE_OK ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2918 | /* If a transient index is required, create it by calling |
| 2919 | ** sqlite3BtreeCreateTable() with the BTREE_ZERODATA flag before |
| 2920 | ** opening it. If a transient table is required, just use the |
danielk1977 | 0dbe72b | 2004-05-11 04:54:49 +0000 | [diff] [blame] | 2921 | ** automatically created table with root-page 1 (an INTKEY table). |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2922 | */ |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 2923 | if( pOp->p4.pKeyInfo ){ |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 2924 | int pgno; |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 2925 | assert( pOp->p4type==P4_KEYINFO ); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2926 | rc = sqlite3BtreeCreateTable(pCx->pBt, &pgno, BTREE_ZERODATA); |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 2927 | if( rc==SQLITE_OK ){ |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 2928 | assert( pgno==MASTER_ROOT+1 ); |
drh | 1e968a0 | 2008-03-25 00:22:21 +0000 | [diff] [blame] | 2929 | rc = sqlite3BtreeCursor(pCx->pBt, pgno, 1, |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 2930 | (KeyInfo*)pOp->p4.z, pCx->pCursor); |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 2931 | pCx->pKeyInfo = pOp->p4.pKeyInfo; |
danielk1977 | 14db266 | 2006-01-09 16:12:04 +0000 | [diff] [blame] | 2932 | pCx->pKeyInfo->enc = ENC(p->db); |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 2933 | } |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 2934 | pCx->isTable = 0; |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 2935 | }else{ |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 2936 | rc = sqlite3BtreeCursor(pCx->pBt, MASTER_ROOT, 1, 0, pCx->pCursor); |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 2937 | pCx->isTable = 1; |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 2938 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2939 | } |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 2940 | pCx->isIndex = !pCx->isTable; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2941 | break; |
| 2942 | } |
| 2943 | |
danielk1977 | 9882d99 | 2008-03-27 17:59:01 +0000 | [diff] [blame] | 2944 | /* Opcode: OpenPseudo P1 P2 * * * |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 2945 | ** |
| 2946 | ** Open a new cursor that points to a fake table that contains a single |
| 2947 | ** row of data. Any attempt to write a second row of data causes the |
| 2948 | ** first row to be deleted. All data is deleted when the cursor is |
| 2949 | ** closed. |
| 2950 | ** |
| 2951 | ** A pseudo-table created by this opcode is useful for holding the |
drh | cdd536f | 2006-03-17 00:04:03 +0000 | [diff] [blame] | 2952 | ** NEW or OLD tables in a trigger. Also used to hold the a single |
| 2953 | ** row output from the sorter so that the row can be decomposed into |
| 2954 | ** individual columns using the OP_Column opcode. |
danielk1977 | 9882d99 | 2008-03-27 17:59:01 +0000 | [diff] [blame] | 2955 | ** |
| 2956 | ** When OP_Insert is executed to insert a row in to the pseudo table, |
| 2957 | ** the pseudo-table cursor may or may not make it's own copy of the |
| 2958 | ** original row data. If P2 is 0, then the pseudo-table will copy the |
| 2959 | ** original row data. Otherwise, a pointer to the original memory cell |
| 2960 | ** is stored. In this case, the vdbe program must ensure that the |
| 2961 | ** memory cell containing the row data is not overwritten until the |
| 2962 | ** pseudo table is closed (or a new row is inserted into it). |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 2963 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2964 | case OP_OpenPseudo: { |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 2965 | int i = pOp->p1; |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 2966 | VdbeCursor *pCx; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 2967 | assert( i>=0 ); |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 2968 | pCx = allocateCursor(p, i, &pOp[-1], -1, 0); |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 2969 | if( pCx==0 ) goto no_mem; |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 2970 | pCx->nullRow = 1; |
| 2971 | pCx->pseudoTable = 1; |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 2972 | pCx->ephemPseudoTable = (u8)pOp->p2; |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 2973 | pCx->isTable = 1; |
| 2974 | pCx->isIndex = 0; |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 2975 | break; |
| 2976 | } |
| 2977 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 2978 | /* Opcode: Close P1 * * * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2979 | ** |
| 2980 | ** Close a cursor previously opened as P1. If P1 is not |
| 2981 | ** currently open, this instruction is a no-op. |
| 2982 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2983 | case OP_Close: { |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2984 | int i = pOp->p1; |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 2985 | assert( i>=0 && i<p->nCursor ); |
| 2986 | sqlite3VdbeFreeCursor(p, p->apCsr[i]); |
| 2987 | p->apCsr[i] = 0; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2988 | break; |
| 2989 | } |
| 2990 | |
drh | 959403f | 2008-12-12 17:56:16 +0000 | [diff] [blame] | 2991 | /* Opcode: SeekGe P1 P2 P3 P4 * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2992 | ** |
danielk1977 | b790c6c | 2008-04-18 10:25:24 +0000 | [diff] [blame] | 2993 | ** 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] | 2994 | ** use the value in register P3 as the key. If cursor P1 refers |
danielk1977 | b790c6c | 2008-04-18 10:25:24 +0000 | [diff] [blame] | 2995 | ** to an SQL index, then P3 is the first in an array of P4 registers |
| 2996 | ** that are used as an unpacked index key. |
| 2997 | ** |
| 2998 | ** Reposition cursor P1 so that it points to the smallest entry that |
| 2999 | ** is greater than or equal to the key value. If there are no records |
| 3000 | ** 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] | 3001 | ** |
drh | 959403f | 2008-12-12 17:56:16 +0000 | [diff] [blame] | 3002 | ** See also: Found, NotFound, Distinct, SeekLt, SeekGt, SeekLe |
drh | 7cf6e4d | 2004-05-19 14:56:55 +0000 | [diff] [blame] | 3003 | */ |
drh | 959403f | 2008-12-12 17:56:16 +0000 | [diff] [blame] | 3004 | /* Opcode: SeekGt P1 P2 P3 P4 * |
drh | 7cf6e4d | 2004-05-19 14:56:55 +0000 | [diff] [blame] | 3005 | ** |
danielk1977 | b790c6c | 2008-04-18 10:25:24 +0000 | [diff] [blame] | 3006 | ** 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] | 3007 | ** use the value in register P3 as a key. If cursor P1 refers |
danielk1977 | b790c6c | 2008-04-18 10:25:24 +0000 | [diff] [blame] | 3008 | ** to an SQL index, then P3 is the first in an array of P4 registers |
| 3009 | ** that are used as an unpacked index key. |
| 3010 | ** |
| 3011 | ** Reposition cursor P1 so that it points to the smallest entry that |
| 3012 | ** is greater than the key value. If there are no records greater than |
| 3013 | ** the key and P2 is not zero, then jump to P2. |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 3014 | ** |
drh | 959403f | 2008-12-12 17:56:16 +0000 | [diff] [blame] | 3015 | ** See also: Found, NotFound, Distinct, SeekLt, SeekGe, SeekLe |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3016 | */ |
drh | 959403f | 2008-12-12 17:56:16 +0000 | [diff] [blame] | 3017 | /* Opcode: SeekLt P1 P2 P3 P4 * |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 3018 | ** |
danielk1977 | b790c6c | 2008-04-18 10:25:24 +0000 | [diff] [blame] | 3019 | ** 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] | 3020 | ** use the value in register P3 as a key. If cursor P1 refers |
danielk1977 | b790c6c | 2008-04-18 10:25:24 +0000 | [diff] [blame] | 3021 | ** to an SQL index, then P3 is the first in an array of P4 registers |
| 3022 | ** that are used as an unpacked index key. |
| 3023 | ** |
| 3024 | ** Reposition cursor P1 so that it points to the largest entry that |
| 3025 | ** is less than the key value. If there are no records less than |
| 3026 | ** the key and P2 is not zero, then jump to P2. |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 3027 | ** |
drh | 959403f | 2008-12-12 17:56:16 +0000 | [diff] [blame] | 3028 | ** See also: Found, NotFound, Distinct, SeekGt, SeekGe, SeekLe |
drh | 7cf6e4d | 2004-05-19 14:56:55 +0000 | [diff] [blame] | 3029 | */ |
drh | 959403f | 2008-12-12 17:56:16 +0000 | [diff] [blame] | 3030 | /* Opcode: SeekLe P1 P2 P3 P4 * |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 3031 | ** |
danielk1977 | b790c6c | 2008-04-18 10:25:24 +0000 | [diff] [blame] | 3032 | ** 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] | 3033 | ** use the value in register P3 as a key. If cursor P1 refers |
danielk1977 | b790c6c | 2008-04-18 10:25:24 +0000 | [diff] [blame] | 3034 | ** to an SQL index, then P3 is the first in an array of P4 registers |
| 3035 | ** that are used as an unpacked index key. |
danielk1977 | 751de56 | 2008-04-18 09:01:15 +0000 | [diff] [blame] | 3036 | ** |
danielk1977 | b790c6c | 2008-04-18 10:25:24 +0000 | [diff] [blame] | 3037 | ** Reposition cursor P1 so that it points to the largest entry that |
| 3038 | ** is less than or equal to the key value. If there are no records |
| 3039 | ** 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] | 3040 | ** |
drh | 959403f | 2008-12-12 17:56:16 +0000 | [diff] [blame] | 3041 | ** See also: Found, NotFound, Distinct, SeekGt, SeekGe, SeekLt |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 3042 | */ |
drh | 959403f | 2008-12-12 17:56:16 +0000 | [diff] [blame] | 3043 | case OP_SeekLt: /* jump, in3 */ |
| 3044 | case OP_SeekLe: /* jump, in3 */ |
| 3045 | case OP_SeekGe: /* jump, in3 */ |
| 3046 | case OP_SeekGt: { /* jump, in3 */ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3047 | int i = pOp->p1; |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 3048 | VdbeCursor *pC; |
drh | 80ff32f | 2001-11-04 18:32:46 +0000 | [diff] [blame] | 3049 | |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3050 | assert( i>=0 && i<p->nCursor ); |
drh | 959403f | 2008-12-12 17:56:16 +0000 | [diff] [blame] | 3051 | assert( pOp->p2!=0 ); |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame] | 3052 | pC = p->apCsr[i]; |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 3053 | assert( pC!=0 ); |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3054 | if( pC->pCursor!=0 ){ |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 3055 | int res, oc; |
drh | 7cf6e4d | 2004-05-19 14:56:55 +0000 | [diff] [blame] | 3056 | oc = pOp->opcode; |
drh | a11846b | 2004-01-07 18:52:56 +0000 | [diff] [blame] | 3057 | pC->nullRow = 0; |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3058 | if( pC->isTable ){ |
drh | 959403f | 2008-12-12 17:56:16 +0000 | [diff] [blame] | 3059 | i64 iKey; /* The rowid we are to seek to */ |
| 3060 | |
| 3061 | /* The input value in P3 might be of any type: integer, real, string, |
| 3062 | ** blob, or NULL. But it needs to be an integer before we can do |
| 3063 | ** the seek, so covert it. */ |
| 3064 | applyNumericAffinity(pIn3); |
| 3065 | iKey = sqlite3VdbeIntValue(pIn3); |
| 3066 | pC->rowidIsValid = 0; |
| 3067 | |
| 3068 | /* If the P3 value could not be converted into an integer without |
| 3069 | ** loss of information, then special processing is required... */ |
| 3070 | if( (pIn3->flags & MEM_Int)==0 ){ |
| 3071 | if( (pIn3->flags & MEM_Real)==0 ){ |
| 3072 | /* If the P3 value cannot be converted into any kind of a number, |
| 3073 | ** then the seek is not possible, so jump to P2 */ |
| 3074 | pc = pOp->p2 - 1; |
| 3075 | break; |
| 3076 | } |
| 3077 | /* If we reach this point, then the P3 value must be a floating |
| 3078 | ** point number. */ |
| 3079 | assert( (pIn3->flags & MEM_Real)!=0 ); |
| 3080 | |
| 3081 | if( iKey==SMALLEST_INT64 && (pIn3->r<(double)iKey || pIn3->r>0) ){ |
| 3082 | /* The P3 value is to large in magnitude to be expressed as an |
| 3083 | ** integer. */ |
| 3084 | res = 1; |
| 3085 | if( pIn3->r<0 ){ |
| 3086 | if( oc==OP_SeekGt || oc==OP_SeekGe ){ |
| 3087 | rc = sqlite3BtreeFirst(pC->pCursor, &res); |
| 3088 | if( rc!=SQLITE_OK ) goto abort_due_to_error; |
| 3089 | } |
| 3090 | }else{ |
| 3091 | if( oc==OP_SeekLt || oc==OP_SeekLe ){ |
| 3092 | rc = sqlite3BtreeLast(pC->pCursor, &res); |
| 3093 | if( rc!=SQLITE_OK ) goto abort_due_to_error; |
| 3094 | } |
| 3095 | } |
| 3096 | if( res ){ |
| 3097 | pc = pOp->p2 - 1; |
| 3098 | } |
| 3099 | break; |
| 3100 | }else if( oc==OP_SeekLt || oc==OP_SeekGe ){ |
| 3101 | /* Use the ceiling() function to convert real->int */ |
| 3102 | if( pIn3->r > (double)iKey ) iKey++; |
| 3103 | }else{ |
| 3104 | /* Use the floor() function to convert real->int */ |
| 3105 | assert( oc==OP_SeekLe || oc==OP_SeekGt ); |
| 3106 | if( pIn3->r < (double)iKey ) iKey--; |
| 3107 | } |
| 3108 | } |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 3109 | rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, 0, (u64)iKey, 0, &res); |
danielk1977 | 2812956 | 2005-01-11 10:25:06 +0000 | [diff] [blame] | 3110 | if( rc!=SQLITE_OK ){ |
| 3111 | goto abort_due_to_error; |
| 3112 | } |
drh | 959403f | 2008-12-12 17:56:16 +0000 | [diff] [blame] | 3113 | if( res==0 ){ |
| 3114 | pC->rowidIsValid = 1; |
| 3115 | pC->lastRowid = iKey; |
| 3116 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3117 | }else{ |
danielk1977 | b790c6c | 2008-04-18 10:25:24 +0000 | [diff] [blame] | 3118 | UnpackedRecord r; |
| 3119 | int nField = pOp->p4.i; |
| 3120 | assert( pOp->p4type==P4_INT32 ); |
| 3121 | assert( nField>0 ); |
| 3122 | r.pKeyInfo = pC->pKeyInfo; |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 3123 | r.nField = (u16)nField; |
drh | 959403f | 2008-12-12 17:56:16 +0000 | [diff] [blame] | 3124 | if( oc==OP_SeekGt || oc==OP_SeekLe ){ |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 3125 | r.flags = UNPACKED_INCRKEY; |
| 3126 | }else{ |
| 3127 | r.flags = 0; |
| 3128 | } |
danielk1977 | b790c6c | 2008-04-18 10:25:24 +0000 | [diff] [blame] | 3129 | r.aMem = &p->aMem[pOp->p3]; |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 3130 | rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, &r, 0, 0, &res); |
danielk1977 | 2812956 | 2005-01-11 10:25:06 +0000 | [diff] [blame] | 3131 | if( rc!=SQLITE_OK ){ |
| 3132 | goto abort_due_to_error; |
| 3133 | } |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3134 | pC->rowidIsValid = 0; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3135 | } |
drh | a11846b | 2004-01-07 18:52:56 +0000 | [diff] [blame] | 3136 | pC->deferredMoveto = 0; |
drh | 76873ab | 2006-01-07 18:48:26 +0000 | [diff] [blame] | 3137 | pC->cacheStatus = CACHE_STALE; |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 3138 | #ifdef SQLITE_TEST |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 3139 | sqlite3_search_count++; |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 3140 | #endif |
drh | 959403f | 2008-12-12 17:56:16 +0000 | [diff] [blame] | 3141 | if( oc==OP_SeekGe || oc==OP_SeekGt ){ |
| 3142 | if( res<0 || (res==0 && oc==OP_SeekGt) ){ |
danielk1977 | 2812956 | 2005-01-11 10:25:06 +0000 | [diff] [blame] | 3143 | rc = sqlite3BtreeNext(pC->pCursor, &res); |
danielk1977 | 01427a6 | 2005-01-11 13:02:33 +0000 | [diff] [blame] | 3144 | if( rc!=SQLITE_OK ) goto abort_due_to_error; |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3145 | pC->rowidIsValid = 0; |
drh | 1af3fdb | 2004-07-18 21:33:01 +0000 | [diff] [blame] | 3146 | }else{ |
| 3147 | res = 0; |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 3148 | } |
drh | 7cf6e4d | 2004-05-19 14:56:55 +0000 | [diff] [blame] | 3149 | }else{ |
drh | 959403f | 2008-12-12 17:56:16 +0000 | [diff] [blame] | 3150 | assert( oc==OP_SeekLt || oc==OP_SeekLe ); |
| 3151 | if( res>0 || (res==0 && oc==OP_SeekLt) ){ |
danielk1977 | 01427a6 | 2005-01-11 13:02:33 +0000 | [diff] [blame] | 3152 | rc = sqlite3BtreePrevious(pC->pCursor, &res); |
| 3153 | if( rc!=SQLITE_OK ) goto abort_due_to_error; |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3154 | pC->rowidIsValid = 0; |
drh | 1a844c3 | 2002-12-04 22:29:28 +0000 | [diff] [blame] | 3155 | }else{ |
| 3156 | /* res might be negative because the table is empty. Check to |
| 3157 | ** see if this is the case. |
| 3158 | */ |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 3159 | res = sqlite3BtreeEof(pC->pCursor); |
drh | 1a844c3 | 2002-12-04 22:29:28 +0000 | [diff] [blame] | 3160 | } |
drh | 1af3fdb | 2004-07-18 21:33:01 +0000 | [diff] [blame] | 3161 | } |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 3162 | assert( pOp->p2>0 ); |
drh | 1af3fdb | 2004-07-18 21:33:01 +0000 | [diff] [blame] | 3163 | if( res ){ |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 3164 | pc = pOp->p2 - 1; |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 3165 | } |
danielk1977 | f7b9d66 | 2008-06-23 18:49:43 +0000 | [diff] [blame] | 3166 | }else if( !pC->pseudoTable ){ |
| 3167 | /* This happens when attempting to open the sqlite3_master table |
| 3168 | ** for read access returns SQLITE_EMPTY. In this case always |
| 3169 | ** take the jump (since there are no records in the table). |
| 3170 | */ |
| 3171 | pc = pOp->p2 - 1; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3172 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3173 | break; |
| 3174 | } |
| 3175 | |
drh | 959403f | 2008-12-12 17:56:16 +0000 | [diff] [blame] | 3176 | /* Opcode: Seek P1 P2 * * * |
| 3177 | ** |
| 3178 | ** P1 is an open table cursor and P2 is a rowid integer. Arrange |
| 3179 | ** for P1 to move so that it points to the rowid given by P2. |
| 3180 | ** |
| 3181 | ** This is actually a deferred seek. Nothing actually happens until |
| 3182 | ** the cursor is used to read a record. That way, if no reads |
| 3183 | ** occur, no unnecessary I/O happens. |
| 3184 | */ |
| 3185 | case OP_Seek: { /* in2 */ |
| 3186 | int i = pOp->p1; |
| 3187 | VdbeCursor *pC; |
| 3188 | |
| 3189 | assert( i>=0 && i<p->nCursor ); |
| 3190 | pC = p->apCsr[i]; |
| 3191 | assert( pC!=0 ); |
| 3192 | if( pC->pCursor!=0 ){ |
| 3193 | assert( pC->isTable ); |
| 3194 | pC->nullRow = 0; |
| 3195 | pC->movetoTarget = sqlite3VdbeIntValue(pIn2); |
| 3196 | pC->rowidIsValid = 0; |
| 3197 | pC->deferredMoveto = 1; |
| 3198 | } |
| 3199 | break; |
| 3200 | } |
| 3201 | |
| 3202 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 3203 | /* Opcode: Found P1 P2 P3 * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3204 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 3205 | ** Register P3 holds a blob constructed by MakeRecord. P1 is an index. |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3206 | ** If an entry that matches the value in register p3 exists in P1 then |
| 3207 | ** 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] | 3208 | ** then fall thru. The P1 cursor is left pointing at the matching entry |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 3209 | ** if it exists. |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3210 | ** |
| 3211 | ** This instruction is used to implement the IN operator where the |
danielk1977 | 9a96b66 | 2007-11-29 17:05:18 +0000 | [diff] [blame] | 3212 | ** left-hand side is a SELECT statement. P1 may be a true index, or it |
| 3213 | ** may be a temporary index that holds the results of the SELECT |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 3214 | ** statement. This instruction is also used to implement the |
| 3215 | ** DISTINCT keyword in SELECT statements. |
danielk1977 | 9a96b66 | 2007-11-29 17:05:18 +0000 | [diff] [blame] | 3216 | ** |
| 3217 | ** This instruction checks if index P1 contains a record for which |
shane | 21e7feb | 2008-05-30 15:59:49 +0000 | [diff] [blame] | 3218 | ** the first N serialized values exactly match the N serialized values |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3219 | ** in the record in register P3, where N is the total number of values in |
| 3220 | ** the P3 record (the P3 record is a prefix of the P1 record). |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 3221 | ** |
drh | cb6d50e | 2008-08-21 19:28:30 +0000 | [diff] [blame] | 3222 | ** See also: NotFound, IsUnique, NotExists |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3223 | */ |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 3224 | /* Opcode: NotFound P1 P2 P3 * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3225 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 3226 | ** Register P3 holds a blob constructed by MakeRecord. P1 is |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3227 | ** 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] | 3228 | ** to P2. If an entry does existing, fall through. The cursor is left |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3229 | ** pointing to the entry that matches. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3230 | ** |
drh | cb6d50e | 2008-08-21 19:28:30 +0000 | [diff] [blame] | 3231 | ** See also: Found, NotExists, IsUnique |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3232 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3233 | case OP_NotFound: /* jump, in3 */ |
| 3234 | case OP_Found: { /* jump, in3 */ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3235 | int i = pOp->p1; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3236 | int alreadyExists = 0; |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 3237 | VdbeCursor *pC; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3238 | assert( i>=0 && i<p->nCursor ); |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 3239 | assert( p->apCsr[i]!=0 ); |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame] | 3240 | if( (pC = p->apCsr[i])->pCursor!=0 ){ |
danielk1977 | 7751940 | 2007-08-30 11:48:31 +0000 | [diff] [blame] | 3241 | int res; |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 3242 | UnpackedRecord *pIdxKey; |
| 3243 | |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3244 | assert( pC->isTable==0 ); |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 3245 | assert( pIn3->flags & MEM_Blob ); |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 3246 | pIdxKey = sqlite3VdbeRecordUnpack(pC->pKeyInfo, pIn3->n, pIn3->z, |
drh | 23f79d0 | 2008-08-20 22:06:47 +0000 | [diff] [blame] | 3247 | aTempRec, sizeof(aTempRec)); |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 3248 | if( pIdxKey==0 ){ |
| 3249 | goto no_mem; |
danielk1977 | 9a96b66 | 2007-11-29 17:05:18 +0000 | [diff] [blame] | 3250 | } |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 3251 | if( pOp->opcode==OP_Found ){ |
| 3252 | pIdxKey->flags |= UNPACKED_PREFIX_MATCH; |
| 3253 | } |
| 3254 | rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, pIdxKey, 0, 0, &res); |
| 3255 | sqlite3VdbeDeleteUnpackedRecord(pIdxKey); |
danielk1977 | 7751940 | 2007-08-30 11:48:31 +0000 | [diff] [blame] | 3256 | if( rc!=SQLITE_OK ){ |
| 3257 | break; |
| 3258 | } |
| 3259 | alreadyExists = (res==0); |
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 | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3262 | } |
| 3263 | if( pOp->opcode==OP_Found ){ |
| 3264 | if( alreadyExists ) pc = pOp->p2 - 1; |
| 3265 | }else{ |
| 3266 | if( !alreadyExists ) pc = pOp->p2 - 1; |
| 3267 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3268 | break; |
| 3269 | } |
| 3270 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 3271 | /* Opcode: IsUnique P1 P2 P3 P4 * |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 3272 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 3273 | ** The P3 register contains an integer record number. Call this |
| 3274 | ** record number R. The P4 register contains an index key created |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 3275 | ** using MakeRecord. Call it K. |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 3276 | ** |
drh | 7cf6e4d | 2004-05-19 14:56:55 +0000 | [diff] [blame] | 3277 | ** P1 is an index. So it has no data and its key consists of a |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3278 | ** record generated by OP_MakeRecord where the last field is the |
| 3279 | ** rowid of the entry that the index refers to. |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 3280 | ** |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 3281 | ** This instruction asks if there is an entry in P1 where the |
drh | 7cf6e4d | 2004-05-19 14:56:55 +0000 | [diff] [blame] | 3282 | ** fields matches K but the rowid is different from R. |
| 3283 | ** If there is no such entry, then there is an immediate |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 3284 | ** jump to P2. If any entry does exist where the index string |
| 3285 | ** matches K but the record number is not R, then the record |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 3286 | ** number for that entry is written into P3 and control |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 3287 | ** falls through to the next instruction. |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 3288 | ** |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3289 | ** See also: NotFound, NotExists, Found |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 3290 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3291 | case OP_IsUnique: { /* jump, in3 */ |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 3292 | int i = pOp->p1; |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 3293 | VdbeCursor *pCx; |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 3294 | BtCursor *pCrsr; |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 3295 | Mem *pK; |
danielk1977 | 452c989 | 2004-05-13 05:16:15 +0000 | [diff] [blame] | 3296 | i64 R; |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 3297 | |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 3298 | /* Pop the value R off the top of the stack |
| 3299 | */ |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 3300 | assert( pOp->p4type==P4_INT32 ); |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3301 | assert( pOp->p4.i>0 && pOp->p4.i<=p->nMem ); |
| 3302 | pK = &p->aMem[pOp->p4.i]; |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 3303 | sqlite3VdbeMemIntegerify(pIn3); |
| 3304 | R = pIn3->u.i; |
drh | 73bdf07 | 2006-08-15 14:21:16 +0000 | [diff] [blame] | 3305 | assert( i>=0 && i<p->nCursor ); |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame] | 3306 | pCx = p->apCsr[i]; |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 3307 | assert( pCx!=0 ); |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 3308 | pCrsr = pCx->pCursor; |
| 3309 | if( pCrsr!=0 ){ |
danielk1977 | f2fa831 | 2006-01-24 13:09:33 +0000 | [diff] [blame] | 3310 | int res; |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 3311 | i64 v; /* The record number that matches K */ |
| 3312 | UnpackedRecord *pIdxKey; /* Unpacked version of P4 */ |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 3313 | |
| 3314 | /* Make sure K is a string and make zKey point to K |
| 3315 | */ |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 3316 | assert( pK->flags & MEM_Blob ); |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 3317 | pIdxKey = sqlite3VdbeRecordUnpack(pCx->pKeyInfo, pK->n, pK->z, |
drh | 23f79d0 | 2008-08-20 22:06:47 +0000 | [diff] [blame] | 3318 | aTempRec, sizeof(aTempRec)); |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 3319 | if( pIdxKey==0 ){ |
| 3320 | goto no_mem; |
| 3321 | } |
| 3322 | pIdxKey->flags |= UNPACKED_IGNORE_ROWID; |
danielk1977 | 452c989 | 2004-05-13 05:16:15 +0000 | [diff] [blame] | 3323 | |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 3324 | /* Search for an entry in P1 where all but the last rowid match K |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 3325 | ** If there is no such entry, jump immediately to P2. |
| 3326 | */ |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 3327 | assert( pCx->deferredMoveto==0 ); |
drh | 76873ab | 2006-01-07 18:48:26 +0000 | [diff] [blame] | 3328 | pCx->cacheStatus = CACHE_STALE; |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 3329 | rc = sqlite3BtreeMovetoUnpacked(pCrsr, pIdxKey, 0, 0, &res); |
danielk1977 | f011300 | 2006-01-24 12:09:17 +0000 | [diff] [blame] | 3330 | if( rc!=SQLITE_OK ){ |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 3331 | sqlite3VdbeDeleteUnpackedRecord(pIdxKey); |
danielk1977 | f011300 | 2006-01-24 12:09:17 +0000 | [diff] [blame] | 3332 | goto abort_due_to_error; |
| 3333 | } |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 3334 | if( res<0 ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3335 | rc = sqlite3BtreeNext(pCrsr, &res); |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 3336 | if( res ){ |
| 3337 | pc = pOp->p2 - 1; |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 3338 | sqlite3VdbeDeleteUnpackedRecord(pIdxKey); |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 3339 | break; |
| 3340 | } |
| 3341 | } |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 3342 | rc = sqlite3VdbeIdxKeyCompare(pCx, pIdxKey, &res); |
| 3343 | sqlite3VdbeDeleteUnpackedRecord(pIdxKey); |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 3344 | if( rc!=SQLITE_OK ) goto abort_due_to_error; |
| 3345 | if( res>0 ){ |
| 3346 | pc = pOp->p2 - 1; |
| 3347 | break; |
| 3348 | } |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 3349 | |
| 3350 | /* At this point, pCrsr is pointing to an entry in P1 where all but |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 3351 | ** the final entry (the rowid) matches K. Check to see if the |
| 3352 | ** final rowid column is different from R. If it equals R then jump |
danielk1977 | 452c989 | 2004-05-13 05:16:15 +0000 | [diff] [blame] | 3353 | ** immediately to P2. |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 3354 | */ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 3355 | rc = sqlite3VdbeIdxRowid(pCrsr, &v); |
danielk1977 | 452c989 | 2004-05-13 05:16:15 +0000 | [diff] [blame] | 3356 | if( rc!=SQLITE_OK ){ |
| 3357 | goto abort_due_to_error; |
| 3358 | } |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 3359 | if( v==R ){ |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 3360 | pc = pOp->p2 - 1; |
| 3361 | break; |
| 3362 | } |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 3363 | |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3364 | /* The final varint of the key is different from R. Store it back |
| 3365 | ** into register R3. (The record number of an entry that violates |
| 3366 | ** a UNIQUE constraint.) |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 3367 | */ |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 3368 | pIn3->u.i = v; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 3369 | assert( pIn3->flags&MEM_Int ); |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 3370 | } |
| 3371 | break; |
| 3372 | } |
| 3373 | |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3374 | /* Opcode: NotExists P1 P2 P3 * * |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 3375 | ** |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3376 | ** Use the content of register P3 as a integer key. If a record |
danielk1977 | 96cb76f | 2008-01-04 13:24:28 +0000 | [diff] [blame] | 3377 | ** with that key does not exist in table of P1, then jump to P2. |
| 3378 | ** If the record does exist, then fall thru. The cursor is left |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3379 | ** pointing to the record if it exists. |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 3380 | ** |
| 3381 | ** The difference between this operation and NotFound is that this |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3382 | ** operation assumes the key is an integer and that P1 is a table whereas |
| 3383 | ** NotFound assumes key is a blob constructed from MakeRecord and |
| 3384 | ** P1 is an index. |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 3385 | ** |
drh | cb6d50e | 2008-08-21 19:28:30 +0000 | [diff] [blame] | 3386 | ** See also: Found, NotFound, IsUnique |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 3387 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3388 | case OP_NotExists: { /* jump, in3 */ |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 3389 | int i = pOp->p1; |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 3390 | VdbeCursor *pC; |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 3391 | BtCursor *pCrsr; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3392 | assert( i>=0 && i<p->nCursor ); |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 3393 | assert( p->apCsr[i]!=0 ); |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame] | 3394 | if( (pCrsr = (pC = p->apCsr[i])->pCursor)!=0 ){ |
drh | 03e1f51 | 2008-12-11 13:05:00 +0000 | [diff] [blame] | 3395 | int res = 0; |
danielk1977 | 36a3c70 | 2004-05-11 06:55:14 +0000 | [diff] [blame] | 3396 | u64 iKey; |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 3397 | assert( pIn3->flags & MEM_Int ); |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3398 | assert( p->apCsr[i]->isTable ); |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 3399 | iKey = intToKey(pIn3->u.i); |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 3400 | rc = sqlite3BtreeMovetoUnpacked(pCrsr, 0, iKey, 0,&res); |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 3401 | pC->lastRowid = pIn3->u.i; |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 3402 | pC->rowidIsValid = res==0 ?1:0; |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 3403 | pC->nullRow = 0; |
drh | 76873ab | 2006-01-07 18:48:26 +0000 | [diff] [blame] | 3404 | pC->cacheStatus = CACHE_STALE; |
danielk1977 | 2812956 | 2005-01-11 10:25:06 +0000 | [diff] [blame] | 3405 | if( res!=0 ){ |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 3406 | pc = pOp->p2 - 1; |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 3407 | assert( pC->rowidIsValid==0 ); |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 3408 | } |
danielk1977 | f7b9d66 | 2008-06-23 18:49:43 +0000 | [diff] [blame] | 3409 | }else if( !pC->pseudoTable ){ |
| 3410 | /* This happens when an attempt to open a read cursor on the |
| 3411 | ** sqlite_master table returns SQLITE_EMPTY. |
| 3412 | */ |
| 3413 | assert( pC->isTable ); |
| 3414 | pc = pOp->p2 - 1; |
| 3415 | assert( pC->rowidIsValid==0 ); |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 3416 | } |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 3417 | break; |
| 3418 | } |
| 3419 | |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 3420 | /* Opcode: Sequence P1 P2 * * * |
drh | 4db38a7 | 2005-09-01 12:16:28 +0000 | [diff] [blame] | 3421 | ** |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 3422 | ** Find the next available sequence number for cursor P1. |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3423 | ** Write the sequence number into register P2. |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 3424 | ** The sequence number on the cursor is incremented after this |
| 3425 | ** instruction. |
drh | 4db38a7 | 2005-09-01 12:16:28 +0000 | [diff] [blame] | 3426 | */ |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 3427 | case OP_Sequence: { /* out2-prerelease */ |
drh | 4db38a7 | 2005-09-01 12:16:28 +0000 | [diff] [blame] | 3428 | int i = pOp->p1; |
drh | 4db38a7 | 2005-09-01 12:16:28 +0000 | [diff] [blame] | 3429 | assert( i>=0 && i<p->nCursor ); |
| 3430 | assert( p->apCsr[i]!=0 ); |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 3431 | pOut->u.i = p->apCsr[i]->seqCount++; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 3432 | MemSetTypeFlag(pOut, MEM_Int); |
drh | 4db38a7 | 2005-09-01 12:16:28 +0000 | [diff] [blame] | 3433 | break; |
| 3434 | } |
| 3435 | |
| 3436 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 3437 | /* Opcode: NewRowid P1 P2 P3 * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3438 | ** |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3439 | ** 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] | 3440 | ** The record number is not previously used as a key in the database |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3441 | ** table that cursor P1 points to. The new record number is written |
| 3442 | ** written to register P2. |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 3443 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 3444 | ** If P3>0 then P3 is a register that holds the largest previously |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 3445 | ** generated record number. No new record numbers are allowed to be less |
drh | 2958a4e | 2004-11-12 03:56:15 +0000 | [diff] [blame] | 3446 | ** than this value. When this value reaches its maximum, a SQLITE_FULL |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 3447 | ** error is generated. The P3 register is updated with the generated |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 3448 | ** record number. This P3 mechanism is used to help implement the |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 3449 | ** AUTOINCREMENT feature. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3450 | */ |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 3451 | case OP_NewRowid: { /* out2-prerelease */ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3452 | int i = pOp->p1; |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 3453 | i64 v = 0; |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 3454 | VdbeCursor *pC; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3455 | assert( i>=0 && i<p->nCursor ); |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 3456 | assert( p->apCsr[i]!=0 ); |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame] | 3457 | if( (pC = p->apCsr[i])->pCursor==0 ){ |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 3458 | /* The zero initialization above is all that is needed */ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3459 | }else{ |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 3460 | /* The next rowid or record number (different terms for the same |
| 3461 | ** thing) is obtained in a two-step algorithm. |
| 3462 | ** |
| 3463 | ** First we attempt to find the largest existing rowid and add one |
| 3464 | ** to that. But if the largest existing rowid is already the maximum |
| 3465 | ** positive integer, we have to fall through to the second |
| 3466 | ** probabilistic algorithm |
| 3467 | ** |
| 3468 | ** The second algorithm is to select a rowid at random and see if |
| 3469 | ** it already exists in the table. If it does not exist, we have |
| 3470 | ** succeeded. If the random rowid does exist, we select a new one |
| 3471 | ** and try again, up to 1000 times. |
drh | db5ed6d | 2001-09-18 22:17:44 +0000 | [diff] [blame] | 3472 | ** |
| 3473 | ** For a table with less than 2 billion entries, the probability |
| 3474 | ** of not finding a unused rowid is about 1.0e-300. This is a |
| 3475 | ** non-zero probability, but it is still vanishingly small and should |
| 3476 | ** never cause a problem. You are much, much more likely to have a |
| 3477 | ** hardware failure than for this algorithm to fail. |
| 3478 | ** |
drh | af9ff33 | 2002-01-16 21:00:27 +0000 | [diff] [blame] | 3479 | ** The analysis in the previous paragraph assumes that you have a good |
| 3480 | ** source of random numbers. Is a library function like lrand48() |
| 3481 | ** good enough? Maybe. Maybe not. It's hard to know whether there |
| 3482 | ** might be subtle bugs is some implementations of lrand48() that |
| 3483 | ** could cause problems. To avoid uncertainty, SQLite uses its own |
| 3484 | ** random number generator based on the RC4 algorithm. |
| 3485 | ** |
drh | db5ed6d | 2001-09-18 22:17:44 +0000 | [diff] [blame] | 3486 | ** To promote locality of reference for repetitive inserts, the |
shane | 21e7feb | 2008-05-30 15:59:49 +0000 | [diff] [blame] | 3487 | ** first few attempts at choosing a random rowid pick values just a little |
drh | db5ed6d | 2001-09-18 22:17:44 +0000 | [diff] [blame] | 3488 | ** larger than the previous rowid. This has been shown experimentally |
| 3489 | ** to double the speed of the COPY operation. |
| 3490 | */ |
danielk1977 | f7df9cc | 2004-06-16 12:02:47 +0000 | [diff] [blame] | 3491 | int res, rx=SQLITE_OK, cnt; |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 3492 | i64 x; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3493 | cnt = 0; |
drh | 4e6083c | 2005-02-04 21:13:00 +0000 | [diff] [blame] | 3494 | if( (sqlite3BtreeFlags(pC->pCursor)&(BTREE_INTKEY|BTREE_ZERODATA)) != |
| 3495 | BTREE_INTKEY ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 3496 | rc = SQLITE_CORRUPT_BKPT; |
drh | 4e6083c | 2005-02-04 21:13:00 +0000 | [diff] [blame] | 3497 | goto abort_due_to_error; |
| 3498 | } |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 3499 | assert( (sqlite3BtreeFlags(pC->pCursor) & BTREE_INTKEY)!=0 ); |
| 3500 | assert( (sqlite3BtreeFlags(pC->pCursor) & BTREE_ZERODATA)==0 ); |
drh | fe2093d | 2005-01-20 22:48:47 +0000 | [diff] [blame] | 3501 | |
drh | 75f86a4 | 2005-02-17 00:03:06 +0000 | [diff] [blame] | 3502 | #ifdef SQLITE_32BIT_ROWID |
| 3503 | # define MAX_ROWID 0x7fffffff |
| 3504 | #else |
drh | fe2093d | 2005-01-20 22:48:47 +0000 | [diff] [blame] | 3505 | /* Some compilers complain about constants of the form 0x7fffffffffffffff. |
| 3506 | ** Others complain about 0x7ffffffffffffffffLL. The following macro seems |
| 3507 | ** to provide the constant while making all compilers happy. |
| 3508 | */ |
danielk1977 | 64202cf | 2008-11-17 15:31:47 +0000 | [diff] [blame] | 3509 | # define MAX_ROWID (i64)( (((u64)0x7fffffff)<<32) | (u64)0xffffffff ) |
drh | 75f86a4 | 2005-02-17 00:03:06 +0000 | [diff] [blame] | 3510 | #endif |
drh | fe2093d | 2005-01-20 22:48:47 +0000 | [diff] [blame] | 3511 | |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 3512 | if( !pC->useRandomRowid ){ |
drh | 32fbe34 | 2002-10-19 20:16:37 +0000 | [diff] [blame] | 3513 | if( pC->nextRowidValid ){ |
| 3514 | v = pC->nextRowid; |
drh | 3fc190c | 2001-09-14 03:24:23 +0000 | [diff] [blame] | 3515 | }else{ |
danielk1977 | 261919c | 2005-12-06 12:52:59 +0000 | [diff] [blame] | 3516 | rc = sqlite3BtreeLast(pC->pCursor, &res); |
| 3517 | if( rc!=SQLITE_OK ){ |
| 3518 | goto abort_due_to_error; |
| 3519 | } |
drh | 32fbe34 | 2002-10-19 20:16:37 +0000 | [diff] [blame] | 3520 | if( res ){ |
| 3521 | v = 1; |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 3522 | }else{ |
danielk1977 | e0d4b06 | 2004-06-28 01:11:46 +0000 | [diff] [blame] | 3523 | sqlite3BtreeKeySize(pC->pCursor, &v); |
drh | 32fbe34 | 2002-10-19 20:16:37 +0000 | [diff] [blame] | 3524 | v = keyToInt(v); |
drh | 75f86a4 | 2005-02-17 00:03:06 +0000 | [diff] [blame] | 3525 | if( v==MAX_ROWID ){ |
drh | 32fbe34 | 2002-10-19 20:16:37 +0000 | [diff] [blame] | 3526 | pC->useRandomRowid = 1; |
| 3527 | }else{ |
| 3528 | v++; |
| 3529 | } |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 3530 | } |
drh | 3fc190c | 2001-09-14 03:24:23 +0000 | [diff] [blame] | 3531 | } |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 3532 | |
| 3533 | #ifndef SQLITE_OMIT_AUTOINCREMENT |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 3534 | if( pOp->p3 ){ |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 3535 | Mem *pMem; |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 3536 | assert( pOp->p3>0 && pOp->p3<=p->nMem ); /* P3 is a valid memory cell */ |
| 3537 | pMem = &p->aMem[pOp->p3]; |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 3538 | REGISTER_TRACE(pOp->p3, pMem); |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 3539 | sqlite3VdbeMemIntegerify(pMem); |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 3540 | assert( (pMem->flags & MEM_Int)!=0 ); /* mem(P3) holds an integer */ |
drh | 3c024d6 | 2007-03-30 11:23:45 +0000 | [diff] [blame] | 3541 | if( pMem->u.i==MAX_ROWID || pC->useRandomRowid ){ |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 3542 | rc = SQLITE_FULL; |
| 3543 | goto abort_due_to_error; |
| 3544 | } |
drh | 3c024d6 | 2007-03-30 11:23:45 +0000 | [diff] [blame] | 3545 | if( v<pMem->u.i+1 ){ |
| 3546 | v = pMem->u.i + 1; |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 3547 | } |
drh | 3c024d6 | 2007-03-30 11:23:45 +0000 | [diff] [blame] | 3548 | pMem->u.i = v; |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 3549 | } |
| 3550 | #endif |
| 3551 | |
drh | 75f86a4 | 2005-02-17 00:03:06 +0000 | [diff] [blame] | 3552 | if( v<MAX_ROWID ){ |
drh | 32fbe34 | 2002-10-19 20:16:37 +0000 | [diff] [blame] | 3553 | pC->nextRowidValid = 1; |
| 3554 | pC->nextRowid = v+1; |
| 3555 | }else{ |
| 3556 | pC->nextRowidValid = 0; |
| 3557 | } |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 3558 | } |
| 3559 | if( pC->useRandomRowid ){ |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 3560 | assert( pOp->p3==0 ); /* SQLITE_FULL must have occurred prior to this */ |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 3561 | v = db->priorNewRowid; |
| 3562 | cnt = 0; |
| 3563 | do{ |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 3564 | if( cnt==0 && (v&0xffffff)==v ){ |
| 3565 | v++; |
| 3566 | }else{ |
drh | 2fa1868 | 2008-03-19 14:15:34 +0000 | [diff] [blame] | 3567 | sqlite3_randomness(sizeof(v), &v); |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 3568 | if( cnt<5 ) v &= 0xffffff; |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 3569 | } |
| 3570 | if( v==0 ) continue; |
| 3571 | x = intToKey(v); |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 3572 | rx = sqlite3BtreeMovetoUnpacked(pC->pCursor, 0, (u64)x, 0, &res); |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 3573 | cnt++; |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 3574 | }while( cnt<100 && rx==SQLITE_OK && res==0 ); |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 3575 | db->priorNewRowid = v; |
| 3576 | if( rx==SQLITE_OK && res==0 ){ |
| 3577 | rc = SQLITE_FULL; |
| 3578 | goto abort_due_to_error; |
| 3579 | } |
drh | 1eaa269 | 2001-09-18 02:02:23 +0000 | [diff] [blame] | 3580 | } |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3581 | pC->rowidIsValid = 0; |
drh | a11846b | 2004-01-07 18:52:56 +0000 | [diff] [blame] | 3582 | pC->deferredMoveto = 0; |
drh | 76873ab | 2006-01-07 18:48:26 +0000 | [diff] [blame] | 3583 | pC->cacheStatus = CACHE_STALE; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3584 | } |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 3585 | MemSetTypeFlag(pOut, MEM_Int); |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 3586 | pOut->u.i = v; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3587 | break; |
| 3588 | } |
| 3589 | |
danielk1977 | 1f4aa33 | 2008-01-03 09:51:55 +0000 | [diff] [blame] | 3590 | /* Opcode: Insert P1 P2 P3 P4 P5 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3591 | ** |
jplyon | 5a56422 | 2003-06-02 06:15:58 +0000 | [diff] [blame] | 3592 | ** Write an entry into the table of cursor P1. A new entry is |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 3593 | ** created if it doesn't already exist or the data for an existing |
danielk1977 | 1f4aa33 | 2008-01-03 09:51:55 +0000 | [diff] [blame] | 3594 | ** entry is overwritten. The data is the value stored register |
| 3595 | ** number P2. The key is stored in register P3. The key must |
| 3596 | ** be an integer. |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 3597 | ** |
danielk1977 | 1f4aa33 | 2008-01-03 09:51:55 +0000 | [diff] [blame] | 3598 | ** If the OPFLAG_NCHANGE flag of P5 is set, then the row change count is |
| 3599 | ** incremented (otherwise not). If the OPFLAG_LASTROWID flag of P5 is set, |
danielk1977 | b28af71 | 2004-06-21 06:50:26 +0000 | [diff] [blame] | 3600 | ** then rowid is stored for subsequent return by the |
drh | 85b623f | 2007-12-13 21:54:09 +0000 | [diff] [blame] | 3601 | ** sqlite3_last_insert_rowid() function (otherwise it is unmodified). |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 3602 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 3603 | ** Parameter P4 may point to a string containing the table-name, or |
danielk1977 | 1f6eec5 | 2006-06-16 06:17:47 +0000 | [diff] [blame] | 3604 | ** may be NULL. If it is not NULL, then the update-hook |
| 3605 | ** (sqlite3.xUpdateCallback) is invoked following a successful insert. |
| 3606 | ** |
drh | 93aed5a | 2008-01-16 17:46:38 +0000 | [diff] [blame] | 3607 | ** (WARNING/TODO: If P1 is a pseudo-cursor and P2 is dynamically |
| 3608 | ** allocated, then ownership of P2 is transferred to the pseudo-cursor |
| 3609 | ** and register P2 becomes ephemeral. If the cursor is changed, the |
| 3610 | ** value of register P2 will then change. Make sure this does not |
| 3611 | ** cause any problems.) |
| 3612 | ** |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3613 | ** This instruction only works on tables. The equivalent instruction |
| 3614 | ** for indices is OP_IdxInsert. |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 3615 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3616 | case OP_Insert: { |
danielk1977 | 1f4aa33 | 2008-01-03 09:51:55 +0000 | [diff] [blame] | 3617 | Mem *pData = &p->aMem[pOp->p2]; |
| 3618 | Mem *pKey = &p->aMem[pOp->p3]; |
| 3619 | |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 3620 | i64 iKey; /* The integer ROWID or key for the record to be inserted */ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3621 | int i = pOp->p1; |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 3622 | VdbeCursor *pC; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 3623 | assert( i>=0 && i<p->nCursor ); |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 3624 | pC = p->apCsr[i]; |
| 3625 | assert( pC!=0 ); |
| 3626 | assert( pC->pCursor!=0 || pC->pseudoTable ); |
| 3627 | assert( pKey->flags & MEM_Int ); |
| 3628 | assert( pC->isTable ); |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 3629 | REGISTER_TRACE(pOp->p2, pData); |
| 3630 | REGISTER_TRACE(pOp->p3, pKey); |
danielk1977 | 5f8d8a8 | 2004-05-11 00:28:42 +0000 | [diff] [blame] | 3631 | |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 3632 | iKey = intToKey(pKey->u.i); |
| 3633 | if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++; |
| 3634 | if( pOp->p5 & OPFLAG_LASTROWID ) db->lastRowid = pKey->u.i; |
| 3635 | if( pC->nextRowidValid && pKey->u.i>=pC->nextRowid ){ |
| 3636 | pC->nextRowidValid = 0; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3637 | } |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 3638 | if( pData->flags & MEM_Null ){ |
| 3639 | pData->z = 0; |
| 3640 | pData->n = 0; |
| 3641 | }else{ |
| 3642 | assert( pData->flags & (MEM_Blob|MEM_Str) ); |
| 3643 | } |
| 3644 | if( pC->pseudoTable ){ |
danielk1977 | 9882d99 | 2008-03-27 17:59:01 +0000 | [diff] [blame] | 3645 | if( !pC->ephemPseudoTable ){ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 3646 | sqlite3DbFree(db, pC->pData); |
danielk1977 | 9882d99 | 2008-03-27 17:59:01 +0000 | [diff] [blame] | 3647 | } |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 3648 | pC->iKey = iKey; |
| 3649 | pC->nData = pData->n; |
danielk1977 | 5f09613 | 2008-03-28 15:44:09 +0000 | [diff] [blame] | 3650 | if( pData->z==pData->zMalloc || pC->ephemPseudoTable ){ |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 3651 | pC->pData = pData->z; |
danielk1977 | 9882d99 | 2008-03-27 17:59:01 +0000 | [diff] [blame] | 3652 | if( !pC->ephemPseudoTable ){ |
| 3653 | pData->flags &= ~MEM_Dyn; |
| 3654 | pData->flags |= MEM_Ephem; |
danielk1977 | 5f09613 | 2008-03-28 15:44:09 +0000 | [diff] [blame] | 3655 | pData->zMalloc = 0; |
danielk1977 | 9882d99 | 2008-03-27 17:59:01 +0000 | [diff] [blame] | 3656 | } |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 3657 | }else{ |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 3658 | pC->pData = sqlite3Malloc( pC->nData+2 ); |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 3659 | if( !pC->pData ) goto no_mem; |
| 3660 | memcpy(pC->pData, pData->z, pC->nData); |
| 3661 | pC->pData[pC->nData] = 0; |
| 3662 | pC->pData[pC->nData+1] = 0; |
| 3663 | } |
| 3664 | pC->nullRow = 0; |
| 3665 | }else{ |
| 3666 | int nZero; |
| 3667 | if( pData->flags & MEM_Zero ){ |
drh | 8df3284 | 2008-12-09 02:51:23 +0000 | [diff] [blame] | 3668 | nZero = pData->u.nZero; |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 3669 | }else{ |
| 3670 | nZero = 0; |
| 3671 | } |
| 3672 | rc = sqlite3BtreeInsert(pC->pCursor, 0, iKey, |
| 3673 | pData->z, pData->n, nZero, |
| 3674 | pOp->p5 & OPFLAG_APPEND); |
| 3675 | } |
| 3676 | |
| 3677 | pC->rowidIsValid = 0; |
| 3678 | pC->deferredMoveto = 0; |
| 3679 | pC->cacheStatus = CACHE_STALE; |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 +0000 | [diff] [blame] | 3680 | |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 3681 | /* Invoke the update-hook if required. */ |
| 3682 | if( rc==SQLITE_OK && db->xUpdateCallback && pOp->p4.z ){ |
| 3683 | const char *zDb = db->aDb[pC->iDb].zName; |
| 3684 | const char *zTbl = pOp->p4.z; |
| 3685 | int op = ((pOp->p5 & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_INSERT); |
| 3686 | assert( pC->isTable ); |
| 3687 | db->xUpdateCallback(db->pUpdateArg, op, zDb, zTbl, iKey); |
| 3688 | assert( pC->iDb>=0 ); |
| 3689 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3690 | break; |
| 3691 | } |
| 3692 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 3693 | /* Opcode: Delete P1 P2 * P4 * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3694 | ** |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 3695 | ** Delete the record at which the P1 cursor is currently pointing. |
| 3696 | ** |
| 3697 | ** The cursor will be left pointing at either the next or the previous |
| 3698 | ** 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] | 3699 | ** the next Next instruction will be a no-op. Hence it is OK to delete |
| 3700 | ** a record from within an Next loop. |
drh | c8d30ac | 2002-04-12 10:08:59 +0000 | [diff] [blame] | 3701 | ** |
rdc | b0c374f | 2004-02-20 22:53:38 +0000 | [diff] [blame] | 3702 | ** 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] | 3703 | ** incremented (otherwise not). |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3704 | ** |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 3705 | ** P1 must not be pseudo-table. It has to be a real table with |
| 3706 | ** multiple rows. |
| 3707 | ** |
| 3708 | ** If P4 is not NULL, then it is the name of the table that P1 is |
| 3709 | ** pointing to. The update hook will be invoked, if it exists. |
| 3710 | ** If P4 is not NULL then the P1 cursor must have been positioned |
| 3711 | ** using OP_NotFound prior to invoking this opcode. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3712 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3713 | case OP_Delete: { |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3714 | int i = pOp->p1; |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 3715 | i64 iKey; |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 3716 | VdbeCursor *pC; |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 3717 | |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3718 | assert( i>=0 && i<p->nCursor ); |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame] | 3719 | pC = p->apCsr[i]; |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 3720 | assert( pC!=0 ); |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 3721 | assert( pC->pCursor!=0 ); /* Only valid for real tables, no pseudotables */ |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 +0000 | [diff] [blame] | 3722 | |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 3723 | /* If the update-hook will be invoked, set iKey to the rowid of the |
| 3724 | ** row being deleted. |
| 3725 | */ |
| 3726 | if( db->xUpdateCallback && pOp->p4.z ){ |
| 3727 | assert( pC->isTable ); |
| 3728 | assert( pC->rowidIsValid ); /* lastRowid set by previous OP_NotFound */ |
| 3729 | iKey = pC->lastRowid; |
| 3730 | } |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 +0000 | [diff] [blame] | 3731 | |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 3732 | rc = sqlite3VdbeCursorMoveto(pC); |
| 3733 | if( rc ) goto abort_due_to_error; |
| 3734 | rc = sqlite3BtreeDelete(pC->pCursor); |
| 3735 | pC->nextRowidValid = 0; |
| 3736 | pC->cacheStatus = CACHE_STALE; |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 +0000 | [diff] [blame] | 3737 | |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 3738 | /* Invoke the update-hook if required. */ |
| 3739 | if( rc==SQLITE_OK && db->xUpdateCallback && pOp->p4.z ){ |
| 3740 | const char *zDb = db->aDb[pC->iDb].zName; |
| 3741 | const char *zTbl = pOp->p4.z; |
| 3742 | db->xUpdateCallback(db->pUpdateArg, SQLITE_DELETE, zDb, zTbl, iKey); |
| 3743 | assert( pC->iDb>=0 ); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3744 | } |
danielk1977 | b28af71 | 2004-06-21 06:50:26 +0000 | [diff] [blame] | 3745 | if( pOp->p2 & OPFLAG_NCHANGE ) p->nChange++; |
rdc | b0c374f | 2004-02-20 22:53:38 +0000 | [diff] [blame] | 3746 | break; |
| 3747 | } |
| 3748 | |
danielk1977 | b28af71 | 2004-06-21 06:50:26 +0000 | [diff] [blame] | 3749 | /* Opcode: ResetCount P1 * * |
rdc | b0c374f | 2004-02-20 22:53:38 +0000 | [diff] [blame] | 3750 | ** |
danielk1977 | b28af71 | 2004-06-21 06:50:26 +0000 | [diff] [blame] | 3751 | ** This opcode resets the VMs internal change counter to 0. If P1 is true, |
| 3752 | ** then the value of the change counter is copied to the database handle |
| 3753 | ** change counter (returned by subsequent calls to sqlite3_changes()) |
| 3754 | ** before it is reset. This is used by trigger programs. |
rdc | b0c374f | 2004-02-20 22:53:38 +0000 | [diff] [blame] | 3755 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3756 | case OP_ResetCount: { |
danielk1977 | b28af71 | 2004-06-21 06:50:26 +0000 | [diff] [blame] | 3757 | if( pOp->p1 ){ |
drh | 344737f | 2004-09-19 00:50:20 +0000 | [diff] [blame] | 3758 | sqlite3VdbeSetChanges(db, p->nChange); |
danielk1977 | b28af71 | 2004-06-21 06:50:26 +0000 | [diff] [blame] | 3759 | } |
| 3760 | p->nChange = 0; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3761 | break; |
| 3762 | } |
| 3763 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 3764 | /* Opcode: RowData P1 P2 * * * |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3765 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 3766 | ** Write into register P2 the complete row data for cursor P1. |
| 3767 | ** There is no interpretation of the data. |
| 3768 | ** It is just copied onto the P2 register exactly as |
danielk1977 | 96cb76f | 2008-01-04 13:24:28 +0000 | [diff] [blame] | 3769 | ** it is found in the database file. |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3770 | ** |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 3771 | ** If the P1 cursor must be pointing to a valid row (not a NULL row) |
| 3772 | ** of a real table, not a pseudo-table. |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3773 | */ |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 3774 | /* Opcode: RowKey P1 P2 * * * |
drh | 143f3c4 | 2004-01-07 20:37:52 +0000 | [diff] [blame] | 3775 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 3776 | ** Write into register P2 the complete row key for cursor P1. |
| 3777 | ** There is no interpretation of the data. |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3778 | ** The key is copied onto the P3 register exactly as |
danielk1977 | 96cb76f | 2008-01-04 13:24:28 +0000 | [diff] [blame] | 3779 | ** it is found in the database file. |
drh | 143f3c4 | 2004-01-07 20:37:52 +0000 | [diff] [blame] | 3780 | ** |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 3781 | ** If the P1 cursor must be pointing to a valid row (not a NULL row) |
| 3782 | ** of a real table, not a pseudo-table. |
drh | 143f3c4 | 2004-01-07 20:37:52 +0000 | [diff] [blame] | 3783 | */ |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 3784 | case OP_RowKey: |
| 3785 | case OP_RowData: { |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3786 | int i = pOp->p1; |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 3787 | VdbeCursor *pC; |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 3788 | BtCursor *pCrsr; |
danielk1977 | e0d4b06 | 2004-06-28 01:11:46 +0000 | [diff] [blame] | 3789 | u32 n; |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3790 | |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 3791 | pOut = &p->aMem[pOp->p2]; |
| 3792 | |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3793 | /* Note that RowKey and RowData are really exactly the same instruction */ |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3794 | assert( i>=0 && i<p->nCursor ); |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame] | 3795 | pC = p->apCsr[i]; |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3796 | assert( pC->isTable || pOp->opcode==OP_RowKey ); |
| 3797 | assert( pC->isIndex || pOp->opcode==OP_RowData ); |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 3798 | assert( pC!=0 ); |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 3799 | assert( pC->nullRow==0 ); |
| 3800 | assert( pC->pseudoTable==0 ); |
| 3801 | assert( pC->pCursor!=0 ); |
| 3802 | pCrsr = pC->pCursor; |
| 3803 | rc = sqlite3VdbeCursorMoveto(pC); |
| 3804 | if( rc ) goto abort_due_to_error; |
| 3805 | if( pC->isIndex ){ |
| 3806 | i64 n64; |
| 3807 | assert( !pC->isTable ); |
| 3808 | sqlite3BtreeKeySize(pCrsr, &n64); |
drh | bb4957f | 2008-03-20 14:03:29 +0000 | [diff] [blame] | 3809 | if( n64>db->aLimit[SQLITE_LIMIT_LENGTH] ){ |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 3810 | goto too_big; |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3811 | } |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 3812 | n = (int)n64; |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 3813 | }else{ |
| 3814 | sqlite3BtreeDataSize(pCrsr, &n); |
danielk1977 | 64202cf | 2008-11-17 15:31:47 +0000 | [diff] [blame] | 3815 | if( (int)n>db->aLimit[SQLITE_LIMIT_LENGTH] ){ |
drh | 023ae03 | 2007-05-08 12:12:16 +0000 | [diff] [blame] | 3816 | goto too_big; |
| 3817 | } |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 3818 | } |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 3819 | if( sqlite3VdbeMemGrow(pOut, n, 0) ){ |
| 3820 | goto no_mem; |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 3821 | } |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 3822 | pOut->n = n; |
| 3823 | MemSetTypeFlag(pOut, MEM_Blob); |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 3824 | if( pC->isIndex ){ |
| 3825 | rc = sqlite3BtreeKey(pCrsr, 0, n, pOut->z); |
| 3826 | }else{ |
| 3827 | rc = sqlite3BtreeData(pCrsr, 0, n, pOut->z); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3828 | } |
danielk1977 | 96cb76f | 2008-01-04 13:24:28 +0000 | [diff] [blame] | 3829 | pOut->enc = SQLITE_UTF8; /* In case the blob is ever cast to text */ |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 3830 | UPDATE_MAX_BLOBSIZE(pOut); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3831 | break; |
| 3832 | } |
| 3833 | |
drh | 2133d82 | 2008-01-03 18:44:59 +0000 | [diff] [blame] | 3834 | /* Opcode: Rowid P1 P2 * * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3835 | ** |
drh | 2133d82 | 2008-01-03 18:44:59 +0000 | [diff] [blame] | 3836 | ** 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] | 3837 | ** P1 is currently point to. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3838 | */ |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 3839 | case OP_Rowid: { /* out2-prerelease */ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3840 | int i = pOp->p1; |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 3841 | VdbeCursor *pC; |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 3842 | i64 v; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3843 | |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3844 | assert( i>=0 && i<p->nCursor ); |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame] | 3845 | pC = p->apCsr[i]; |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 3846 | assert( pC!=0 ); |
drh | 536065a | 2005-01-26 21:55:31 +0000 | [diff] [blame] | 3847 | rc = sqlite3VdbeCursorMoveto(pC); |
drh | 52f159e | 2005-01-27 00:33:21 +0000 | [diff] [blame] | 3848 | if( rc ) goto abort_due_to_error; |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3849 | if( pC->rowidIsValid ){ |
| 3850 | v = pC->lastRowid; |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3851 | }else if( pC->pseudoTable ){ |
| 3852 | v = keyToInt(pC->iKey); |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 3853 | }else if( pC->nullRow ){ |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 3854 | /* Leave the rowid set to a NULL */ |
drh | d60ccc6 | 2003-06-24 10:39:46 +0000 | [diff] [blame] | 3855 | break; |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3856 | }else{ |
| 3857 | assert( pC->pCursor!=0 ); |
danielk1977 | e0d4b06 | 2004-06-28 01:11:46 +0000 | [diff] [blame] | 3858 | sqlite3BtreeKeySize(pC->pCursor, &v); |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3859 | v = keyToInt(v); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3860 | } |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 3861 | pOut->u.i = v; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 3862 | MemSetTypeFlag(pOut, MEM_Int); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3863 | break; |
| 3864 | } |
| 3865 | |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3866 | /* Opcode: NullRow P1 * * * * |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 3867 | ** |
| 3868 | ** Move the cursor P1 to a null row. Any OP_Column operations |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3869 | ** that occur while the cursor is on the null row will always |
| 3870 | ** write a NULL. |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 3871 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3872 | case OP_NullRow: { |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 3873 | int i = pOp->p1; |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 3874 | VdbeCursor *pC; |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 3875 | |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3876 | assert( i>=0 && i<p->nCursor ); |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame] | 3877 | pC = p->apCsr[i]; |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 3878 | assert( pC!=0 ); |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame] | 3879 | pC->nullRow = 1; |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3880 | pC->rowidIsValid = 0; |
danielk1977 | be51a65 | 2008-10-08 17:58:48 +0000 | [diff] [blame] | 3881 | if( pC->pCursor ){ |
| 3882 | sqlite3BtreeClearCursor(pC->pCursor); |
| 3883 | } |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 3884 | break; |
| 3885 | } |
| 3886 | |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3887 | /* Opcode: Last P1 P2 * * * |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 3888 | ** |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3889 | ** The next use of the Rowid or Column or Next instruction for P1 |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 3890 | ** will refer to the last entry in the database table or index. |
| 3891 | ** If the table or index is empty and P2>0, then jump immediately to P2. |
| 3892 | ** If P2 is 0 or if the table or index is not empty, fall through |
| 3893 | ** to the following instruction. |
| 3894 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3895 | case OP_Last: { /* jump */ |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 3896 | int i = pOp->p1; |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 3897 | VdbeCursor *pC; |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 3898 | BtCursor *pCrsr; |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 3899 | int res; |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 3900 | |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3901 | assert( i>=0 && i<p->nCursor ); |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame] | 3902 | pC = p->apCsr[i]; |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 3903 | assert( pC!=0 ); |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 3904 | pCrsr = pC->pCursor; |
| 3905 | assert( pCrsr!=0 ); |
| 3906 | rc = sqlite3BtreeLast(pCrsr, &res); |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 3907 | pC->nullRow = (u8)res; |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 3908 | pC->deferredMoveto = 0; |
| 3909 | pC->cacheStatus = CACHE_STALE; |
| 3910 | if( res && pOp->p2>0 ){ |
| 3911 | pc = pOp->p2 - 1; |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 3912 | } |
| 3913 | break; |
| 3914 | } |
| 3915 | |
drh | 0342b1f | 2005-09-01 03:07:44 +0000 | [diff] [blame] | 3916 | |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3917 | /* Opcode: Sort P1 P2 * * * |
drh | 0342b1f | 2005-09-01 03:07:44 +0000 | [diff] [blame] | 3918 | ** |
| 3919 | ** This opcode does exactly the same thing as OP_Rewind except that |
| 3920 | ** it increments an undocumented global variable used for testing. |
| 3921 | ** |
| 3922 | ** Sorting is accomplished by writing records into a sorting index, |
| 3923 | ** then rewinding that index and playing it back from beginning to |
| 3924 | ** end. We use the OP_Sort opcode instead of OP_Rewind to do the |
| 3925 | ** rewinding so that the global variable will be incremented and |
| 3926 | ** regression tests can determine whether or not the optimizer is |
| 3927 | ** correctly optimizing out sorts. |
| 3928 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3929 | case OP_Sort: { /* jump */ |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 3930 | #ifdef SQLITE_TEST |
drh | 0342b1f | 2005-09-01 03:07:44 +0000 | [diff] [blame] | 3931 | sqlite3_sort_count++; |
drh | 4db38a7 | 2005-09-01 12:16:28 +0000 | [diff] [blame] | 3932 | sqlite3_search_count--; |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 3933 | #endif |
drh | d1d3848 | 2008-10-07 23:46:38 +0000 | [diff] [blame] | 3934 | p->aCounter[SQLITE_STMTSTATUS_SORT-1]++; |
drh | 0342b1f | 2005-09-01 03:07:44 +0000 | [diff] [blame] | 3935 | /* Fall through into OP_Rewind */ |
| 3936 | } |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3937 | /* Opcode: Rewind P1 P2 * * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3938 | ** |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3939 | ** The next use of the Rowid or Column or Next instruction for P1 |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 3940 | ** will refer to the first entry in the database table or index. |
| 3941 | ** If the table or index is empty and P2>0, then jump immediately to P2. |
| 3942 | ** If P2 is 0 or if the table or index is not empty, fall through |
| 3943 | ** to the following instruction. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3944 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3945 | case OP_Rewind: { /* jump */ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3946 | int i = pOp->p1; |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 3947 | VdbeCursor *pC; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3948 | BtCursor *pCrsr; |
drh | f4dada7 | 2004-05-11 09:57:35 +0000 | [diff] [blame] | 3949 | int res; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3950 | |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3951 | assert( i>=0 && i<p->nCursor ); |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame] | 3952 | pC = p->apCsr[i]; |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 3953 | assert( pC!=0 ); |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3954 | if( (pCrsr = pC->pCursor)!=0 ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3955 | rc = sqlite3BtreeFirst(pCrsr, &res); |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 3956 | pC->atFirst = res==0 ?1:0; |
drh | a11846b | 2004-01-07 18:52:56 +0000 | [diff] [blame] | 3957 | pC->deferredMoveto = 0; |
drh | 76873ab | 2006-01-07 18:48:26 +0000 | [diff] [blame] | 3958 | pC->cacheStatus = CACHE_STALE; |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3959 | }else{ |
drh | f4dada7 | 2004-05-11 09:57:35 +0000 | [diff] [blame] | 3960 | res = 1; |
| 3961 | } |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 3962 | pC->nullRow = (u8)res; |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 3963 | assert( pOp->p2>0 && pOp->p2<p->nOp ); |
| 3964 | if( res ){ |
drh | f4dada7 | 2004-05-11 09:57:35 +0000 | [diff] [blame] | 3965 | pc = pOp->p2 - 1; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3966 | } |
| 3967 | break; |
| 3968 | } |
| 3969 | |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3970 | /* Opcode: Next P1 P2 * * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3971 | ** |
| 3972 | ** 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] | 3973 | ** table or index. If there are no more key/value pairs then fall through |
| 3974 | ** to the following instruction. But if the cursor advance was successful, |
| 3975 | ** jump immediately to P2. |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 3976 | ** |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 3977 | ** The P1 cursor must be for a real table, not a pseudo-table. |
| 3978 | ** |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 3979 | ** See also: Prev |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 3980 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3981 | /* Opcode: Prev P1 P2 * * * |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 3982 | ** |
| 3983 | ** Back up cursor P1 so that it points to the previous key/data pair in its |
| 3984 | ** table or index. If there is no previous key/value pairs then fall through |
| 3985 | ** to the following instruction. But if the cursor backup was successful, |
| 3986 | ** jump immediately to P2. |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 3987 | ** |
| 3988 | ** The P1 cursor must be for a real table, not a pseudo-table. |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 3989 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3990 | case OP_Prev: /* jump */ |
| 3991 | case OP_Next: { /* jump */ |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 3992 | VdbeCursor *pC; |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 3993 | BtCursor *pCrsr; |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 3994 | int res; |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 3995 | |
drh | caec2f1 | 2003-01-07 02:47:47 +0000 | [diff] [blame] | 3996 | CHECK_FOR_INTERRUPT; |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 3997 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame] | 3998 | pC = p->apCsr[pOp->p1]; |
drh | 72e8fa4 | 2007-03-28 14:30:06 +0000 | [diff] [blame] | 3999 | if( pC==0 ){ |
| 4000 | break; /* See ticket #2273 */ |
| 4001 | } |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 4002 | pCrsr = pC->pCursor; |
| 4003 | assert( pCrsr ); |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 4004 | res = 1; |
| 4005 | assert( pC->deferredMoveto==0 ); |
| 4006 | rc = pOp->opcode==OP_Next ? sqlite3BtreeNext(pCrsr, &res) : |
| 4007 | sqlite3BtreePrevious(pCrsr, &res); |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 4008 | pC->nullRow = (u8)res; |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 4009 | pC->cacheStatus = CACHE_STALE; |
| 4010 | if( res==0 ){ |
| 4011 | pc = pOp->p2 - 1; |
drh | d1d3848 | 2008-10-07 23:46:38 +0000 | [diff] [blame] | 4012 | if( pOp->p5 ) p->aCounter[pOp->p5-1]++; |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 4013 | #ifdef SQLITE_TEST |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 4014 | sqlite3_search_count++; |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 4015 | #endif |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 4016 | } |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 4017 | pC->rowidIsValid = 0; |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 4018 | break; |
| 4019 | } |
| 4020 | |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4021 | /* Opcode: IdxInsert P1 P2 P3 * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4022 | ** |
drh | aa9b896 | 2008-01-08 02:57:55 +0000 | [diff] [blame] | 4023 | ** Register P2 holds a SQL index key made using the |
| 4024 | ** MakeIdxRec instructions. This opcode writes that key |
drh | ee32e0a | 2006-01-10 19:45:49 +0000 | [diff] [blame] | 4025 | ** into the index P1. Data for the entry is nil. |
drh | 717e640 | 2001-09-27 03:22:32 +0000 | [diff] [blame] | 4026 | ** |
drh | aa9b896 | 2008-01-08 02:57:55 +0000 | [diff] [blame] | 4027 | ** 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] | 4028 | ** insert is likely to be an append. |
| 4029 | ** |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 4030 | ** This instruction only works for indices. The equivalent instruction |
| 4031 | ** for tables is OP_Insert. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4032 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4033 | case OP_IdxInsert: { /* in2 */ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4034 | int i = pOp->p1; |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 4035 | VdbeCursor *pC; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4036 | BtCursor *pCrsr; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 4037 | assert( i>=0 && i<p->nCursor ); |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 4038 | assert( p->apCsr[i]!=0 ); |
drh | aa9b896 | 2008-01-08 02:57:55 +0000 | [diff] [blame] | 4039 | assert( pIn2->flags & MEM_Blob ); |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame] | 4040 | if( (pCrsr = (pC = p->apCsr[i])->pCursor)!=0 ){ |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 4041 | assert( pC->isTable==0 ); |
drh | aa9b896 | 2008-01-08 02:57:55 +0000 | [diff] [blame] | 4042 | rc = ExpandBlob(pIn2); |
danielk1977 | d908f5a | 2007-05-11 07:08:28 +0000 | [diff] [blame] | 4043 | if( rc==SQLITE_OK ){ |
drh | aa9b896 | 2008-01-08 02:57:55 +0000 | [diff] [blame] | 4044 | int nKey = pIn2->n; |
| 4045 | const char *zKey = pIn2->z; |
| 4046 | rc = sqlite3BtreeInsert(pCrsr, zKey, nKey, "", 0, 0, pOp->p3); |
danielk1977 | d908f5a | 2007-05-11 07:08:28 +0000 | [diff] [blame] | 4047 | assert( pC->deferredMoveto==0 ); |
| 4048 | pC->cacheStatus = CACHE_STALE; |
| 4049 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4050 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4051 | break; |
| 4052 | } |
| 4053 | |
drh | d1d3848 | 2008-10-07 23:46:38 +0000 | [diff] [blame] | 4054 | /* Opcode: IdxDelete P1 P2 P3 * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4055 | ** |
drh | e14006d | 2008-03-25 17:23:32 +0000 | [diff] [blame] | 4056 | ** The content of P3 registers starting at register P2 form |
| 4057 | ** an unpacked index key. This opcode removes that entry from the |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 4058 | ** index opened by cursor P1. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4059 | */ |
drh | e14006d | 2008-03-25 17:23:32 +0000 | [diff] [blame] | 4060 | case OP_IdxDelete: { |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4061 | int i = pOp->p1; |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 4062 | VdbeCursor *pC; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4063 | BtCursor *pCrsr; |
drh | e14006d | 2008-03-25 17:23:32 +0000 | [diff] [blame] | 4064 | assert( pOp->p3>0 ); |
| 4065 | assert( pOp->p2>0 && pOp->p2+pOp->p3<=p->nMem ); |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 4066 | assert( i>=0 && i<p->nCursor ); |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 4067 | assert( p->apCsr[i]!=0 ); |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame] | 4068 | if( (pCrsr = (pC = p->apCsr[i])->pCursor)!=0 ){ |
danielk1977 | 75bab7d | 2006-01-23 13:09:45 +0000 | [diff] [blame] | 4069 | int res; |
drh | e14006d | 2008-03-25 17:23:32 +0000 | [diff] [blame] | 4070 | UnpackedRecord r; |
| 4071 | r.pKeyInfo = pC->pKeyInfo; |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 4072 | r.nField = (u16)pOp->p3; |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 4073 | r.flags = 0; |
drh | e14006d | 2008-03-25 17:23:32 +0000 | [diff] [blame] | 4074 | r.aMem = &p->aMem[pOp->p2]; |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 4075 | rc = sqlite3BtreeMovetoUnpacked(pCrsr, &r, 0, 0, &res); |
danielk1977 | 75bab7d | 2006-01-23 13:09:45 +0000 | [diff] [blame] | 4076 | if( rc==SQLITE_OK && res==0 ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 4077 | rc = sqlite3BtreeDelete(pCrsr); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4078 | } |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 4079 | assert( pC->deferredMoveto==0 ); |
drh | 76873ab | 2006-01-07 18:48:26 +0000 | [diff] [blame] | 4080 | pC->cacheStatus = CACHE_STALE; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4081 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4082 | break; |
| 4083 | } |
| 4084 | |
drh | 2133d82 | 2008-01-03 18:44:59 +0000 | [diff] [blame] | 4085 | /* Opcode: IdxRowid P1 P2 * * * |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 4086 | ** |
drh | 2133d82 | 2008-01-03 18:44:59 +0000 | [diff] [blame] | 4087 | ** 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] | 4088 | ** the end of the index key pointed to by cursor P1. This integer should be |
| 4089 | ** the rowid of the table entry to which this index entry points. |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 4090 | ** |
drh | 3c899a6 | 2006-01-10 18:44:08 +0000 | [diff] [blame] | 4091 | ** See also: Rowid, MakeIdxRec. |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 4092 | */ |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 4093 | case OP_IdxRowid: { /* out2-prerelease */ |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 4094 | int i = pOp->p1; |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 4095 | BtCursor *pCrsr; |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 4096 | VdbeCursor *pC; |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 4097 | |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 4098 | assert( i>=0 && i<p->nCursor ); |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 4099 | assert( p->apCsr[i]!=0 ); |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame] | 4100 | if( (pCrsr = (pC = p->apCsr[i])->pCursor)!=0 ){ |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 4101 | i64 rowid; |
danielk1977 | 452c989 | 2004-05-13 05:16:15 +0000 | [diff] [blame] | 4102 | |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame] | 4103 | assert( pC->deferredMoveto==0 ); |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 4104 | assert( pC->isTable==0 ); |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 4105 | if( !pC->nullRow ){ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 4106 | rc = sqlite3VdbeIdxRowid(pCrsr, &rowid); |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 4107 | if( rc!=SQLITE_OK ){ |
| 4108 | goto abort_due_to_error; |
| 4109 | } |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 4110 | MemSetTypeFlag(pOut, MEM_Int); |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 4111 | pOut->u.i = rowid; |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 4112 | } |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 4113 | } |
| 4114 | break; |
| 4115 | } |
| 4116 | |
danielk1977 | 61dd583 | 2008-04-18 11:31:12 +0000 | [diff] [blame] | 4117 | /* Opcode: IdxGE P1 P2 P3 P4 P5 |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 4118 | ** |
danielk1977 | 61dd583 | 2008-04-18 11:31:12 +0000 | [diff] [blame] | 4119 | ** The P4 register values beginning with P3 form an unpacked index |
| 4120 | ** key that omits the ROWID. Compare this key value against the index |
| 4121 | ** that P1 is currently pointing to, ignoring the ROWID on the P1 index. |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 4122 | ** |
danielk1977 | 61dd583 | 2008-04-18 11:31:12 +0000 | [diff] [blame] | 4123 | ** If the P1 index entry is greater than or equal to the key value |
| 4124 | ** then jump to P2. Otherwise fall through to the next instruction. |
drh | 772ae62 | 2004-05-19 13:13:08 +0000 | [diff] [blame] | 4125 | ** |
danielk1977 | 61dd583 | 2008-04-18 11:31:12 +0000 | [diff] [blame] | 4126 | ** If P5 is non-zero then the key value is increased by an epsilon |
| 4127 | ** prior to the comparison. This make the opcode work like IdxGT except |
| 4128 | ** that if the key from register P3 is a prefix of the key in the cursor, |
| 4129 | ** the result is false whereas it would be true with IdxGT. |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 4130 | */ |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4131 | /* Opcode: IdxLT P1 P2 P3 * P5 |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 4132 | ** |
danielk1977 | 61dd583 | 2008-04-18 11:31:12 +0000 | [diff] [blame] | 4133 | ** The P4 register values beginning with P3 form an unpacked index |
| 4134 | ** key that omits the ROWID. Compare this key value against the index |
| 4135 | ** that P1 is currently pointing to, ignoring the ROWID on the P1 index. |
drh | f3218fe | 2004-05-28 08:21:02 +0000 | [diff] [blame] | 4136 | ** |
danielk1977 | 61dd583 | 2008-04-18 11:31:12 +0000 | [diff] [blame] | 4137 | ** If the P1 index entry is less than the key value then jump to P2. |
| 4138 | ** Otherwise fall through to the next instruction. |
drh | 772ae62 | 2004-05-19 13:13:08 +0000 | [diff] [blame] | 4139 | ** |
danielk1977 | 61dd583 | 2008-04-18 11:31:12 +0000 | [diff] [blame] | 4140 | ** If P5 is non-zero then the key value is increased by an epsilon prior |
| 4141 | ** to the comparison. This makes the opcode work like IdxLE. |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 4142 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4143 | case OP_IdxLT: /* jump, in3 */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4144 | case OP_IdxGE: { /* jump, in3 */ |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 4145 | int i= pOp->p1; |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 4146 | VdbeCursor *pC; |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 4147 | |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 4148 | assert( i>=0 && i<p->nCursor ); |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 4149 | assert( p->apCsr[i]!=0 ); |
drh | 4f26bb6 | 2005-09-08 14:17:20 +0000 | [diff] [blame] | 4150 | if( (pC = p->apCsr[i])->pCursor!=0 ){ |
drh | 0850b53 | 2006-01-31 19:31:43 +0000 | [diff] [blame] | 4151 | int res; |
danielk1977 | 61dd583 | 2008-04-18 11:31:12 +0000 | [diff] [blame] | 4152 | UnpackedRecord r; |
drh | d7556d2 | 2004-05-14 21:59:40 +0000 | [diff] [blame] | 4153 | assert( pC->deferredMoveto==0 ); |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 4154 | assert( pOp->p5==0 || pOp->p5==1 ); |
danielk1977 | 61dd583 | 2008-04-18 11:31:12 +0000 | [diff] [blame] | 4155 | assert( pOp->p4type==P4_INT32 ); |
| 4156 | r.pKeyInfo = pC->pKeyInfo; |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 4157 | r.nField = (u16)pOp->p4.i; |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 4158 | if( pOp->p5 ){ |
| 4159 | r.flags = UNPACKED_INCRKEY | UNPACKED_IGNORE_ROWID; |
| 4160 | }else{ |
| 4161 | r.flags = UNPACKED_IGNORE_ROWID; |
| 4162 | } |
danielk1977 | 61dd583 | 2008-04-18 11:31:12 +0000 | [diff] [blame] | 4163 | r.aMem = &p->aMem[pOp->p3]; |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 4164 | rc = sqlite3VdbeIdxKeyCompare(pC, &r, &res); |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 4165 | if( pOp->opcode==OP_IdxLT ){ |
| 4166 | res = -res; |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 4167 | }else{ |
| 4168 | assert( pOp->opcode==OP_IdxGE ); |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 4169 | res++; |
| 4170 | } |
| 4171 | if( res>0 ){ |
| 4172 | pc = pOp->p2 - 1 ; |
| 4173 | } |
| 4174 | } |
| 4175 | break; |
| 4176 | } |
| 4177 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4178 | /* Opcode: Destroy P1 P2 P3 * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4179 | ** |
| 4180 | ** Delete an entire database table or index whose root page in the database |
| 4181 | ** file is given by P1. |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 4182 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4183 | ** The table being destroyed is in the main database file if P3==0. If |
| 4184 | ** 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] | 4185 | ** that is used to store tables create using CREATE TEMPORARY TABLE. |
| 4186 | ** |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 4187 | ** If AUTOVACUUM is enabled then it is possible that another root page |
| 4188 | ** might be moved into the newly deleted root page in order to keep all |
| 4189 | ** root pages contiguous at the beginning of the database. The former |
| 4190 | ** value of the root page that moved - its value before the move occurred - |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4191 | ** is stored in register P2. If no page |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4192 | ** movement was required (because the table being dropped was already |
| 4193 | ** the last one in the database) then a zero is stored in register P2. |
| 4194 | ** If AUTOVACUUM is disabled then a zero is stored in register P2. |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 4195 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 4196 | ** See also: Clear |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4197 | */ |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4198 | case OP_Destroy: { /* out2-prerelease */ |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 4199 | int iMoved; |
drh | 3765df4 | 2006-06-28 18:18:09 +0000 | [diff] [blame] | 4200 | int iCnt; |
danielk1977 | 212b218 | 2006-06-23 14:32:08 +0000 | [diff] [blame] | 4201 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | 5a91a53 | 2007-01-05 16:39:43 +0000 | [diff] [blame] | 4202 | Vdbe *pVdbe; |
danielk1977 | 212b218 | 2006-06-23 14:32:08 +0000 | [diff] [blame] | 4203 | iCnt = 0; |
| 4204 | for(pVdbe=db->pVdbe; pVdbe; pVdbe=pVdbe->pNext){ |
| 4205 | if( pVdbe->magic==VDBE_MAGIC_RUN && pVdbe->inVtabMethod<2 && pVdbe->pc>=0 ){ |
| 4206 | iCnt++; |
| 4207 | } |
| 4208 | } |
drh | 3765df4 | 2006-06-28 18:18:09 +0000 | [diff] [blame] | 4209 | #else |
| 4210 | iCnt = db->activeVdbeCnt; |
danielk1977 | 212b218 | 2006-06-23 14:32:08 +0000 | [diff] [blame] | 4211 | #endif |
| 4212 | if( iCnt>1 ){ |
danielk1977 | e6efa74 | 2004-11-10 11:55:10 +0000 | [diff] [blame] | 4213 | rc = SQLITE_LOCKED; |
drh | 77658e2 | 2007-12-04 16:54:52 +0000 | [diff] [blame] | 4214 | p->errorAction = OE_Abort; |
danielk1977 | e6efa74 | 2004-11-10 11:55:10 +0000 | [diff] [blame] | 4215 | }else{ |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4216 | int iDb = pOp->p3; |
danielk1977 | 212b218 | 2006-06-23 14:32:08 +0000 | [diff] [blame] | 4217 | assert( iCnt==1 ); |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4218 | assert( (p->btreeMask & (1<<iDb))!=0 ); |
| 4219 | rc = sqlite3BtreeDropTable(db->aDb[iDb].pBt, pOp->p1, &iMoved); |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 4220 | MemSetTypeFlag(pOut, MEM_Int); |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4221 | pOut->u.i = iMoved; |
drh | 3765df4 | 2006-06-28 18:18:09 +0000 | [diff] [blame] | 4222 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | e6efa74 | 2004-11-10 11:55:10 +0000 | [diff] [blame] | 4223 | if( rc==SQLITE_OK && iMoved!=0 ){ |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4224 | sqlite3RootPageMoved(&db->aDb[iDb], iMoved, pOp->p1); |
danielk1977 | e6efa74 | 2004-11-10 11:55:10 +0000 | [diff] [blame] | 4225 | } |
drh | 3765df4 | 2006-06-28 18:18:09 +0000 | [diff] [blame] | 4226 | #endif |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 4227 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4228 | break; |
| 4229 | } |
| 4230 | |
danielk1977 | c7af484 | 2008-10-27 13:59:33 +0000 | [diff] [blame] | 4231 | /* Opcode: Clear P1 P2 P3 |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 4232 | ** |
| 4233 | ** Delete all contents of the database table or index whose root page |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 4234 | ** in the database file is given by P1. But, unlike Destroy, do not |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 4235 | ** remove the table or index from the database file. |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 4236 | ** |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 4237 | ** The table being clear is in the main database file if P2==0. If |
| 4238 | ** P2==1 then the table to be clear is in the auxiliary database file |
| 4239 | ** that is used to store tables create using CREATE TEMPORARY TABLE. |
| 4240 | ** |
danielk1977 | c7af484 | 2008-10-27 13:59:33 +0000 | [diff] [blame] | 4241 | ** If the P3 value is non-zero, then the table refered to must be an |
| 4242 | ** intkey table (an SQL table, not an index). In this case the row change |
| 4243 | ** count is incremented by the number of rows in the table being cleared. |
| 4244 | ** If P3 is greater than zero, then the value stored in register P3 is |
| 4245 | ** also incremented by the number of rows in the table being cleared. |
| 4246 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 4247 | ** See also: Destroy |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 4248 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4249 | case OP_Clear: { |
danielk1977 | c7af484 | 2008-10-27 13:59:33 +0000 | [diff] [blame] | 4250 | int nChange = 0; |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 4251 | assert( (p->btreeMask & (1<<pOp->p2))!=0 ); |
danielk1977 | c7af484 | 2008-10-27 13:59:33 +0000 | [diff] [blame] | 4252 | rc = sqlite3BtreeClearTable( |
| 4253 | db->aDb[pOp->p2].pBt, pOp->p1, (pOp->p3 ? &nChange : 0) |
| 4254 | ); |
| 4255 | if( pOp->p3 ){ |
| 4256 | p->nChange += nChange; |
| 4257 | if( pOp->p3>0 ){ |
| 4258 | p->aMem[pOp->p3].u.i += nChange; |
| 4259 | } |
| 4260 | } |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 4261 | break; |
| 4262 | } |
| 4263 | |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 4264 | /* Opcode: CreateTable P1 P2 * * * |
drh | 5b2fd56 | 2001-09-13 15:21:31 +0000 | [diff] [blame] | 4265 | ** |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 4266 | ** Allocate a new table in the main database file if P1==0 or in the |
| 4267 | ** auxiliary database file if P1==1 or in an attached database if |
| 4268 | ** P1>1. Write the root page number of the new table into |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4269 | ** register P2 |
drh | 5b2fd56 | 2001-09-13 15:21:31 +0000 | [diff] [blame] | 4270 | ** |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 4271 | ** The difference between a table and an index is this: A table must |
| 4272 | ** have a 4-byte integer key and can have arbitrary data. An index |
| 4273 | ** has an arbitrary key but no data. |
| 4274 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 4275 | ** See also: CreateIndex |
drh | 5b2fd56 | 2001-09-13 15:21:31 +0000 | [diff] [blame] | 4276 | */ |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 4277 | /* Opcode: CreateIndex P1 P2 * * * |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 4278 | ** |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 4279 | ** Allocate a new index in the main database file if P1==0 or in the |
| 4280 | ** auxiliary database file if P1==1 or in an attached database if |
| 4281 | ** P1>1. Write the root page number of the new table into |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4282 | ** register P2. |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 4283 | ** |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 4284 | ** See documentation on OP_CreateTable for additional information. |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 4285 | */ |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 4286 | case OP_CreateIndex: /* out2-prerelease */ |
| 4287 | case OP_CreateTable: { /* out2-prerelease */ |
drh | 88a003e | 2008-12-11 16:17:03 +0000 | [diff] [blame] | 4288 | int pgno = 0; |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 4289 | int flags; |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 4290 | Db *pDb; |
| 4291 | assert( pOp->p1>=0 && pOp->p1<db->nDb ); |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 4292 | assert( (p->btreeMask & (1<<pOp->p1))!=0 ); |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 4293 | pDb = &db->aDb[pOp->p1]; |
| 4294 | assert( pDb->pBt!=0 ); |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 4295 | if( pOp->opcode==OP_CreateTable ){ |
danielk1977 | 9407625 | 2004-05-14 12:16:11 +0000 | [diff] [blame] | 4296 | /* flags = BTREE_INTKEY; */ |
| 4297 | flags = BTREE_LEAFDATA|BTREE_INTKEY; |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 4298 | }else{ |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 4299 | flags = BTREE_ZERODATA; |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 4300 | } |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 4301 | rc = sqlite3BtreeCreateTable(pDb->pBt, &pgno, flags); |
drh | 88a003e | 2008-12-11 16:17:03 +0000 | [diff] [blame] | 4302 | pOut->u.i = pgno; |
| 4303 | MemSetTypeFlag(pOut, MEM_Int); |
drh | 5b2fd56 | 2001-09-13 15:21:31 +0000 | [diff] [blame] | 4304 | break; |
| 4305 | } |
| 4306 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4307 | /* Opcode: ParseSchema P1 P2 * P4 * |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 4308 | ** |
| 4309 | ** Read and parse all entries from the SQLITE_MASTER table of database P1 |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 4310 | ** that match the WHERE clause P4. P2 is the "force" flag. Always do |
drh | 3c23a88 | 2007-01-09 14:01:13 +0000 | [diff] [blame] | 4311 | ** the parsing if P2 is true. If P2 is false, then this routine is a |
| 4312 | ** no-op if the schema is not currently loaded. In other words, if P2 |
| 4313 | ** is false, the SQLITE_MASTER table is only parsed if the rest of the |
| 4314 | ** schema is already loaded into the symbol table. |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 4315 | ** |
| 4316 | ** This opcode invokes the parser to create a new virtual machine, |
shane | 21e7feb | 2008-05-30 15:59:49 +0000 | [diff] [blame] | 4317 | ** then runs the new virtual machine. It is thus a re-entrant opcode. |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 4318 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4319 | case OP_ParseSchema: { |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 4320 | char *zSql; |
| 4321 | int iDb = pOp->p1; |
| 4322 | const char *zMaster; |
| 4323 | InitData initData; |
| 4324 | |
| 4325 | assert( iDb>=0 && iDb<db->nDb ); |
drh | 3c23a88 | 2007-01-09 14:01:13 +0000 | [diff] [blame] | 4326 | if( !pOp->p2 && !DbHasProperty(db, iDb, DB_SchemaLoaded) ){ |
| 4327 | break; |
| 4328 | } |
danielk1977 | 53c0f74 | 2005-03-29 03:10:59 +0000 | [diff] [blame] | 4329 | zMaster = SCHEMA_TABLE(iDb); |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 4330 | initData.db = db; |
drh | ece3c72 | 2006-09-23 20:36:01 +0000 | [diff] [blame] | 4331 | initData.iDb = pOp->p1; |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 4332 | initData.pzErrMsg = &p->zErrMsg; |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 4333 | zSql = sqlite3MPrintf(db, |
drh | ece3c72 | 2006-09-23 20:36:01 +0000 | [diff] [blame] | 4334 | "SELECT name, rootpage, sql FROM '%q'.%s WHERE %s", |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 4335 | db->aDb[iDb].zName, zMaster, pOp->p4.z); |
drh | 71c697e | 2004-08-08 23:39:19 +0000 | [diff] [blame] | 4336 | if( zSql==0 ) goto no_mem; |
drh | 7e8b848 | 2008-01-23 03:03:05 +0000 | [diff] [blame] | 4337 | (void)sqlite3SafetyOff(db); |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 4338 | assert( db->init.busy==0 ); |
| 4339 | db->init.busy = 1; |
drh | c456e57 | 2008-08-11 18:44:58 +0000 | [diff] [blame] | 4340 | initData.rc = SQLITE_OK; |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 4341 | assert( !db->mallocFailed ); |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 4342 | rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0); |
drh | c456e57 | 2008-08-11 18:44:58 +0000 | [diff] [blame] | 4343 | if( rc==SQLITE_OK ) rc = initData.rc; |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 4344 | sqlite3DbFree(db, zSql); |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 4345 | db->init.busy = 0; |
drh | 7e8b848 | 2008-01-23 03:03:05 +0000 | [diff] [blame] | 4346 | (void)sqlite3SafetyOn(db); |
danielk1977 | 261919c | 2005-12-06 12:52:59 +0000 | [diff] [blame] | 4347 | if( rc==SQLITE_NOMEM ){ |
danielk1977 | 261919c | 2005-12-06 12:52:59 +0000 | [diff] [blame] | 4348 | goto no_mem; |
| 4349 | } |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 4350 | break; |
| 4351 | } |
| 4352 | |
drh | cfed7bc | 2006-03-13 14:28:05 +0000 | [diff] [blame] | 4353 | #if !defined(SQLITE_OMIT_ANALYZE) && !defined(SQLITE_OMIT_PARSER) |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4354 | /* Opcode: LoadAnalysis P1 * * * * |
drh | 497e446 | 2005-07-23 03:18:40 +0000 | [diff] [blame] | 4355 | ** |
| 4356 | ** Read the sqlite_stat1 table for database P1 and load the content |
| 4357 | ** of that table into the internal index hash table. This will cause |
| 4358 | ** the analysis to be used when preparing all subsequent queries. |
| 4359 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4360 | case OP_LoadAnalysis: { |
drh | 497e446 | 2005-07-23 03:18:40 +0000 | [diff] [blame] | 4361 | int iDb = pOp->p1; |
| 4362 | assert( iDb>=0 && iDb<db->nDb ); |
drh | cf1be45 | 2007-05-12 12:08:51 +0000 | [diff] [blame] | 4363 | rc = sqlite3AnalysisLoad(db, iDb); |
drh | 497e446 | 2005-07-23 03:18:40 +0000 | [diff] [blame] | 4364 | break; |
| 4365 | } |
drh | cfed7bc | 2006-03-13 14:28:05 +0000 | [diff] [blame] | 4366 | #endif /* !defined(SQLITE_OMIT_ANALYZE) && !defined(SQLITE_OMIT_PARSER) */ |
drh | 497e446 | 2005-07-23 03:18:40 +0000 | [diff] [blame] | 4367 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4368 | /* Opcode: DropTable P1 * * P4 * |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 4369 | ** |
| 4370 | ** Remove the internal (in-memory) data structures that describe |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 4371 | ** the table named P4 in database P1. This is called after a table |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 4372 | ** is dropped in order to keep the internal representation of the |
| 4373 | ** schema consistent with what is on disk. |
| 4374 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4375 | case OP_DropTable: { |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 4376 | sqlite3UnlinkAndDeleteTable(db, pOp->p1, pOp->p4.z); |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 4377 | break; |
| 4378 | } |
| 4379 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4380 | /* Opcode: DropIndex P1 * * P4 * |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 4381 | ** |
| 4382 | ** Remove the internal (in-memory) data structures that describe |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 4383 | ** the index named P4 in database P1. This is called after an index |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 4384 | ** is dropped in order to keep the internal representation of the |
| 4385 | ** schema consistent with what is on disk. |
| 4386 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4387 | case OP_DropIndex: { |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 4388 | sqlite3UnlinkAndDeleteIndex(db, pOp->p1, pOp->p4.z); |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 4389 | break; |
| 4390 | } |
| 4391 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4392 | /* Opcode: DropTrigger P1 * * P4 * |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 4393 | ** |
| 4394 | ** Remove the internal (in-memory) data structures that describe |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 4395 | ** the trigger named P4 in database P1. This is called after a trigger |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 4396 | ** is dropped in order to keep the internal representation of the |
| 4397 | ** schema consistent with what is on disk. |
| 4398 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4399 | case OP_DropTrigger: { |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 4400 | sqlite3UnlinkAndDeleteTrigger(db, pOp->p1, pOp->p4.z); |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 4401 | break; |
| 4402 | } |
| 4403 | |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 4404 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 4405 | #ifndef SQLITE_OMIT_INTEGRITY_CHECK |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4406 | /* Opcode: IntegrityCk P1 P2 P3 * P5 |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4407 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4408 | ** Do an analysis of the currently open database. Store in |
| 4409 | ** register P1 the text of an error message describing any problems. |
| 4410 | ** If no problems are found, store a NULL in register P1. |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 4411 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4412 | ** The register P3 contains the maximum number of allowed errors. |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 4413 | ** At most reg(P3) errors will be reported. |
| 4414 | ** In other words, the analysis stops as soon as reg(P1) errors are |
| 4415 | ** seen. Reg(P1) is updated with the number of errors remaining. |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 4416 | ** |
drh | 7906975 | 2004-05-22 21:30:40 +0000 | [diff] [blame] | 4417 | ** The root page numbers of all tables in the database are integer |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 4418 | ** 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] | 4419 | ** total. |
drh | 2150432 | 2002-06-25 13:16:02 +0000 | [diff] [blame] | 4420 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4421 | ** If P5 is not zero, the check is done on the auxiliary database |
drh | 2150432 | 2002-06-25 13:16:02 +0000 | [diff] [blame] | 4422 | ** file, not the main database file. |
drh | 1dd397f | 2002-02-03 03:34:07 +0000 | [diff] [blame] | 4423 | ** |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 4424 | ** This opcode is used to implement the integrity_check pragma. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4425 | */ |
drh | aaab572 | 2002-02-19 13:39:21 +0000 | [diff] [blame] | 4426 | case OP_IntegrityCk: { |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4427 | int nRoot; /* Number of tables to check. (Number of root pages.) */ |
| 4428 | int *aRoot; /* Array of rootpage numbers for tables to be checked */ |
| 4429 | int j; /* Loop counter */ |
| 4430 | int nErr; /* Number of errors reported */ |
| 4431 | char *z; /* Text of the error report */ |
| 4432 | Mem *pnErr; /* Register keeping track of errors remaining */ |
| 4433 | |
| 4434 | nRoot = pOp->p2; |
drh | 7906975 | 2004-05-22 21:30:40 +0000 | [diff] [blame] | 4435 | assert( nRoot>0 ); |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 4436 | aRoot = sqlite3DbMallocRaw(db, sizeof(int)*(nRoot+1) ); |
drh | caec2f1 | 2003-01-07 02:47:47 +0000 | [diff] [blame] | 4437 | if( aRoot==0 ) goto no_mem; |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4438 | assert( pOp->p3>0 && pOp->p3<=p->nMem ); |
| 4439 | pnErr = &p->aMem[pOp->p3]; |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 4440 | assert( (pnErr->flags & MEM_Int)!=0 ); |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4441 | assert( (pnErr->flags & (MEM_Str|MEM_Blob))==0 ); |
| 4442 | pIn1 = &p->aMem[pOp->p1]; |
drh | 7906975 | 2004-05-22 21:30:40 +0000 | [diff] [blame] | 4443 | for(j=0; j<nRoot; j++){ |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 4444 | aRoot[j] = (int)sqlite3VdbeIntValue(&pIn1[j]); |
drh | 1dd397f | 2002-02-03 03:34:07 +0000 | [diff] [blame] | 4445 | } |
| 4446 | aRoot[j] = 0; |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4447 | assert( pOp->p5<db->nDb ); |
| 4448 | assert( (p->btreeMask & (1<<pOp->p5))!=0 ); |
| 4449 | z = sqlite3BtreeIntegrityCheck(db->aDb[pOp->p5].pBt, aRoot, nRoot, |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 4450 | (int)pnErr->u.i, &nErr); |
drh | c890fec | 2008-08-01 20:10:08 +0000 | [diff] [blame] | 4451 | sqlite3DbFree(db, aRoot); |
drh | 3c024d6 | 2007-03-30 11:23:45 +0000 | [diff] [blame] | 4452 | pnErr->u.i -= nErr; |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 4453 | sqlite3VdbeMemSetNull(pIn1); |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 4454 | if( nErr==0 ){ |
| 4455 | assert( z==0 ); |
drh | c890fec | 2008-08-01 20:10:08 +0000 | [diff] [blame] | 4456 | }else if( z==0 ){ |
| 4457 | goto no_mem; |
drh | 1dd397f | 2002-02-03 03:34:07 +0000 | [diff] [blame] | 4458 | }else{ |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 4459 | sqlite3VdbeMemSetStr(pIn1, z, -1, SQLITE_UTF8, sqlite3_free); |
danielk1977 | 8a6b541 | 2004-05-24 07:04:25 +0000 | [diff] [blame] | 4460 | } |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 4461 | UPDATE_MAX_BLOBSIZE(pIn1); |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4462 | sqlite3VdbeChangeEncoding(pIn1, encoding); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4463 | break; |
| 4464 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 4465 | #endif /* SQLITE_OMIT_INTEGRITY_CHECK */ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4466 | |
drh | 3d4501e | 2008-12-04 20:40:10 +0000 | [diff] [blame] | 4467 | /* Opcode: RowSetAdd P1 P2 * * * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4468 | ** |
drh | 3d4501e | 2008-12-04 20:40:10 +0000 | [diff] [blame] | 4469 | ** Insert the integer value held by register P2 into a boolean index |
| 4470 | ** held in register P1. |
| 4471 | ** |
| 4472 | ** An assertion fails if P2 is not an integer. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4473 | */ |
drh | 3d4501e | 2008-12-04 20:40:10 +0000 | [diff] [blame] | 4474 | case OP_RowSetAdd: { /* in2 */ |
| 4475 | Mem *pIdx; |
| 4476 | Mem *pVal; |
| 4477 | assert( pOp->p1>0 && pOp->p1<=p->nMem ); |
| 4478 | pIdx = &p->aMem[pOp->p1]; |
| 4479 | assert( pOp->p2>0 && pOp->p2<=p->nMem ); |
| 4480 | pVal = &p->aMem[pOp->p2]; |
| 4481 | assert( (pVal->flags & MEM_Int)!=0 ); |
| 4482 | if( (pIdx->flags & MEM_RowSet)==0 ){ |
| 4483 | sqlite3VdbeMemSetRowSet(pIdx); |
drh | 8d99363 | 2008-12-04 22:17:55 +0000 | [diff] [blame] | 4484 | if( (pIdx->flags & MEM_RowSet)==0 ) goto no_mem; |
drh | 3d4501e | 2008-12-04 20:40:10 +0000 | [diff] [blame] | 4485 | } |
| 4486 | sqlite3RowSetInsert(pIdx->u.pRowSet, pVal->u.i); |
| 4487 | break; |
| 4488 | } |
| 4489 | |
| 4490 | /* Opcode: RowSetRead P1 P2 P3 * * |
| 4491 | ** |
| 4492 | ** Extract the smallest value from boolean index P1 and put that value into |
| 4493 | ** register P3. Or, if boolean index P1 is initially empty, leave P3 |
| 4494 | ** unchanged and jump to instruction P2. |
| 4495 | */ |
| 4496 | case OP_RowSetRead: { /* jump, out3 */ |
| 4497 | Mem *pIdx; |
| 4498 | i64 val; |
| 4499 | assert( pOp->p1>0 && pOp->p1<=p->nMem ); |
| 4500 | CHECK_FOR_INTERRUPT; |
| 4501 | pIdx = &p->aMem[pOp->p1]; |
drh | dd5f5a6 | 2008-12-23 13:35:23 +0000 | [diff] [blame] | 4502 | pOut = &p->aMem[pOp->p3]; |
drh | 3d4501e | 2008-12-04 20:40:10 +0000 | [diff] [blame] | 4503 | if( (pIdx->flags & MEM_RowSet)==0 |
| 4504 | || sqlite3RowSetNext(pIdx->u.pRowSet, &val)==0 |
| 4505 | ){ |
| 4506 | /* The boolean index is empty */ |
| 4507 | sqlite3VdbeMemSetNull(pIdx); |
| 4508 | pc = pOp->p2 - 1; |
| 4509 | }else{ |
| 4510 | /* A value was pulled from the index */ |
| 4511 | assert( pOp->p3>0 && pOp->p3<=p->nMem ); |
drh | 3d4501e | 2008-12-04 20:40:10 +0000 | [diff] [blame] | 4512 | sqlite3VdbeMemSetInt64(pOut, val); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 4513 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4514 | break; |
| 4515 | } |
| 4516 | |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4517 | |
danielk1977 | 93758c8 | 2005-01-21 08:13:14 +0000 | [diff] [blame] | 4518 | #ifndef SQLITE_OMIT_TRIGGER |
rdc | b0c374f | 2004-02-20 22:53:38 +0000 | [diff] [blame] | 4519 | /* Opcode: ContextPush * * * |
| 4520 | ** |
| 4521 | ** Save the current Vdbe context such that it can be restored by a ContextPop |
| 4522 | ** opcode. The context stores the last insert row id, the last statement change |
| 4523 | ** count, and the current statement change count. |
| 4524 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4525 | case OP_ContextPush: { |
drh | 344737f | 2004-09-19 00:50:20 +0000 | [diff] [blame] | 4526 | int i = p->contextStackTop++; |
| 4527 | Context *pContext; |
danielk1977 | b28af71 | 2004-06-21 06:50:26 +0000 | [diff] [blame] | 4528 | |
drh | 344737f | 2004-09-19 00:50:20 +0000 | [diff] [blame] | 4529 | assert( i>=0 ); |
danielk1977 | b28af71 | 2004-06-21 06:50:26 +0000 | [diff] [blame] | 4530 | /* 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] | 4531 | if( i>=p->contextStackDepth ){ |
| 4532 | p->contextStackDepth = i+1; |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 4533 | p->contextStack = sqlite3DbReallocOrFree(db, p->contextStack, |
drh | cf64372 | 2007-03-27 13:36:37 +0000 | [diff] [blame] | 4534 | sizeof(Context)*(i+1)); |
drh | 344737f | 2004-09-19 00:50:20 +0000 | [diff] [blame] | 4535 | if( p->contextStack==0 ) goto no_mem; |
| 4536 | } |
| 4537 | pContext = &p->contextStack[i]; |
| 4538 | pContext->lastRowid = db->lastRowid; |
| 4539 | pContext->nChange = p->nChange; |
rdc | b0c374f | 2004-02-20 22:53:38 +0000 | [diff] [blame] | 4540 | break; |
| 4541 | } |
| 4542 | |
| 4543 | /* Opcode: ContextPop * * * |
| 4544 | ** |
| 4545 | ** Restore the Vdbe context to the state it was in when contextPush was last |
| 4546 | ** executed. The context stores the last insert row id, the last statement |
| 4547 | ** change count, and the current statement change count. |
| 4548 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4549 | case OP_ContextPop: { |
drh | 344737f | 2004-09-19 00:50:20 +0000 | [diff] [blame] | 4550 | Context *pContext = &p->contextStack[--p->contextStackTop]; |
| 4551 | assert( p->contextStackTop>=0 ); |
| 4552 | db->lastRowid = pContext->lastRowid; |
| 4553 | p->nChange = pContext->nChange; |
rdc | b0c374f | 2004-02-20 22:53:38 +0000 | [diff] [blame] | 4554 | break; |
| 4555 | } |
danielk1977 | 93758c8 | 2005-01-21 08:13:14 +0000 | [diff] [blame] | 4556 | #endif /* #ifndef SQLITE_OMIT_TRIGGER */ |
rdc | b0c374f | 2004-02-20 22:53:38 +0000 | [diff] [blame] | 4557 | |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 4558 | #ifndef SQLITE_OMIT_AUTOINCREMENT |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4559 | /* Opcode: MemMax P1 P2 * * * |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 4560 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4561 | ** Set the value of register P1 to the maximum of its current value |
| 4562 | ** and the value in register P2. |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 4563 | ** |
| 4564 | ** This instruction throws an error if the memory cell is not initially |
| 4565 | ** an integer. |
| 4566 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4567 | case OP_MemMax: { /* in1, in2 */ |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4568 | sqlite3VdbeMemIntegerify(pIn1); |
| 4569 | sqlite3VdbeMemIntegerify(pIn2); |
| 4570 | if( pIn1->u.i<pIn2->u.i){ |
| 4571 | pIn1->u.i = pIn2->u.i; |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 4572 | } |
| 4573 | break; |
| 4574 | } |
| 4575 | #endif /* SQLITE_OMIT_AUTOINCREMENT */ |
| 4576 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4577 | /* Opcode: IfPos P1 P2 * * * |
danielk1977 | a2dc3b1 | 2005-02-05 12:48:48 +0000 | [diff] [blame] | 4578 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4579 | ** If the value of register P1 is 1 or greater, jump to P2. |
drh | 6f58f70 | 2006-01-08 05:26:41 +0000 | [diff] [blame] | 4580 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4581 | ** It is illegal to use this instruction on a register that does |
drh | 6f58f70 | 2006-01-08 05:26:41 +0000 | [diff] [blame] | 4582 | ** not contain an integer. An assertion fault will result if you try. |
danielk1977 | a2dc3b1 | 2005-02-05 12:48:48 +0000 | [diff] [blame] | 4583 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4584 | case OP_IfPos: { /* jump, in1 */ |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 4585 | assert( pIn1->flags&MEM_Int ); |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 4586 | if( pIn1->u.i>0 ){ |
drh | ec7429a | 2005-10-06 16:53:14 +0000 | [diff] [blame] | 4587 | pc = pOp->p2 - 1; |
| 4588 | } |
| 4589 | break; |
| 4590 | } |
| 4591 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4592 | /* Opcode: IfNeg P1 P2 * * * |
drh | 15007a9 | 2006-01-08 18:10:17 +0000 | [diff] [blame] | 4593 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4594 | ** If the value of register P1 is less than zero, jump to P2. |
drh | 15007a9 | 2006-01-08 18:10:17 +0000 | [diff] [blame] | 4595 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4596 | ** It is illegal to use this instruction on a register that does |
drh | 15007a9 | 2006-01-08 18:10:17 +0000 | [diff] [blame] | 4597 | ** not contain an integer. An assertion fault will result if you try. |
| 4598 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4599 | case OP_IfNeg: { /* jump, in1 */ |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 4600 | assert( pIn1->flags&MEM_Int ); |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 4601 | if( pIn1->u.i<0 ){ |
drh | 15007a9 | 2006-01-08 18:10:17 +0000 | [diff] [blame] | 4602 | pc = pOp->p2 - 1; |
| 4603 | } |
| 4604 | break; |
| 4605 | } |
| 4606 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4607 | /* Opcode: IfZero P1 P2 * * * |
drh | ec7429a | 2005-10-06 16:53:14 +0000 | [diff] [blame] | 4608 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4609 | ** If the value of register P1 is exactly 0, jump to P2. |
drh | 6f58f70 | 2006-01-08 05:26:41 +0000 | [diff] [blame] | 4610 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4611 | ** It is illegal to use this instruction on a register that does |
drh | 6f58f70 | 2006-01-08 05:26:41 +0000 | [diff] [blame] | 4612 | ** not contain an integer. An assertion fault will result if you try. |
drh | ec7429a | 2005-10-06 16:53:14 +0000 | [diff] [blame] | 4613 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4614 | case OP_IfZero: { /* jump, in1 */ |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 4615 | assert( pIn1->flags&MEM_Int ); |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 4616 | if( pIn1->u.i==0 ){ |
drh | a2a49dc | 2008-01-02 14:28:13 +0000 | [diff] [blame] | 4617 | pc = pOp->p2 - 1; |
| 4618 | } |
| 4619 | break; |
| 4620 | } |
| 4621 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4622 | /* Opcode: AggStep * P2 P3 P4 P5 |
drh | e509535 | 2002-02-24 03:25:14 +0000 | [diff] [blame] | 4623 | ** |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 4624 | ** Execute the step function for an aggregate. The |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4625 | ** function has P5 arguments. P4 is a pointer to the FuncDef |
| 4626 | ** structure that specifies the function. Use register |
| 4627 | ** P3 as the accumulator. |
drh | e509535 | 2002-02-24 03:25:14 +0000 | [diff] [blame] | 4628 | ** |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4629 | ** The P5 arguments are taken from register P2 and its |
| 4630 | ** successors. |
drh | e509535 | 2002-02-24 03:25:14 +0000 | [diff] [blame] | 4631 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4632 | case OP_AggStep: { |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4633 | int n = pOp->p5; |
drh | e509535 | 2002-02-24 03:25:14 +0000 | [diff] [blame] | 4634 | int i; |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 4635 | Mem *pMem, *pRec; |
danielk1977 | 22322fd | 2004-05-25 23:35:17 +0000 | [diff] [blame] | 4636 | sqlite3_context ctx; |
danielk1977 | 6ddcca5 | 2004-05-24 23:48:25 +0000 | [diff] [blame] | 4637 | sqlite3_value **apVal; |
drh | e509535 | 2002-02-24 03:25:14 +0000 | [diff] [blame] | 4638 | |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 4639 | assert( n>=0 ); |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4640 | pRec = &p->aMem[pOp->p2]; |
danielk1977 | 6ddcca5 | 2004-05-24 23:48:25 +0000 | [diff] [blame] | 4641 | apVal = p->apArg; |
| 4642 | assert( apVal || n==0 ); |
drh | 6810ce6 | 2004-01-31 19:22:56 +0000 | [diff] [blame] | 4643 | for(i=0; i<n; i++, pRec++){ |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 4644 | apVal[i] = pRec; |
drh | 8079a0d | 2006-01-12 17:20:50 +0000 | [diff] [blame] | 4645 | storeTypeInfo(pRec, encoding); |
drh | e509535 | 2002-02-24 03:25:14 +0000 | [diff] [blame] | 4646 | } |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 4647 | ctx.pFunc = pOp->p4.pFunc; |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4648 | assert( pOp->p3>0 && pOp->p3<=p->nMem ); |
| 4649 | ctx.pMem = pMem = &p->aMem[pOp->p3]; |
drh | abfcea2 | 2005-09-06 20:36:48 +0000 | [diff] [blame] | 4650 | pMem->n++; |
drh | 90669c1 | 2006-01-20 15:45:36 +0000 | [diff] [blame] | 4651 | ctx.s.flags = MEM_Null; |
| 4652 | ctx.s.z = 0; |
danielk1977 | 5f09613 | 2008-03-28 15:44:09 +0000 | [diff] [blame] | 4653 | ctx.s.zMalloc = 0; |
drh | 90669c1 | 2006-01-20 15:45:36 +0000 | [diff] [blame] | 4654 | ctx.s.xDel = 0; |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 4655 | ctx.s.db = db; |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 4656 | ctx.isError = 0; |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 4657 | ctx.pColl = 0; |
drh | e82f5d0 | 2008-10-07 19:53:14 +0000 | [diff] [blame] | 4658 | if( ctx.pFunc->flags & SQLITE_FUNC_NEEDCOLL ){ |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 4659 | assert( pOp>p->aOp ); |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 4660 | assert( pOp[-1].p4type==P4_COLLSEQ ); |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 4661 | assert( pOp[-1].opcode==OP_CollSeq ); |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 4662 | ctx.pColl = pOp[-1].p4.pColl; |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 4663 | } |
danielk1977 | 6ddcca5 | 2004-05-24 23:48:25 +0000 | [diff] [blame] | 4664 | (ctx.pFunc->xStep)(&ctx, n, apVal); |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 4665 | if( ctx.isError ){ |
drh | f089aa4 | 2008-07-08 19:34:06 +0000 | [diff] [blame] | 4666 | sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(&ctx.s)); |
drh | 69544ec | 2008-02-06 14:11:34 +0000 | [diff] [blame] | 4667 | rc = ctx.isError; |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 4668 | } |
drh | 90669c1 | 2006-01-20 15:45:36 +0000 | [diff] [blame] | 4669 | sqlite3VdbeMemRelease(&ctx.s); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4670 | break; |
| 4671 | } |
| 4672 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4673 | /* Opcode: AggFinal P1 P2 * P4 * |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4674 | ** |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 4675 | ** Execute the finalizer function for an aggregate. P1 is |
| 4676 | ** the memory location that is the accumulator for the aggregate. |
drh | a10a34b | 2005-09-07 22:09:48 +0000 | [diff] [blame] | 4677 | ** |
| 4678 | ** P2 is the number of arguments that the step function takes and |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 4679 | ** P4 is a pointer to the FuncDef for this function. The P2 |
drh | a10a34b | 2005-09-07 22:09:48 +0000 | [diff] [blame] | 4680 | ** argument is not used by this opcode. It is only there to disambiguate |
| 4681 | ** functions that can take varying numbers of arguments. The |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 4682 | ** P4 argument is only needed for the degenerate case where |
drh | a10a34b | 2005-09-07 22:09:48 +0000 | [diff] [blame] | 4683 | ** the step function was not previously called. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4684 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4685 | case OP_AggFinal: { |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 4686 | Mem *pMem; |
drh | 0a07c10 | 2008-01-03 18:03:08 +0000 | [diff] [blame] | 4687 | assert( pOp->p1>0 && pOp->p1<=p->nMem ); |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 4688 | pMem = &p->aMem[pOp->p1]; |
drh | a10a34b | 2005-09-07 22:09:48 +0000 | [diff] [blame] | 4689 | assert( (pMem->flags & ~(MEM_Null|MEM_Agg))==0 ); |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 4690 | rc = sqlite3VdbeMemFinalize(pMem, pOp->p4.pFunc); |
drh | 90669c1 | 2006-01-20 15:45:36 +0000 | [diff] [blame] | 4691 | if( rc==SQLITE_ERROR ){ |
drh | f089aa4 | 2008-07-08 19:34:06 +0000 | [diff] [blame] | 4692 | sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(pMem)); |
drh | 90669c1 | 2006-01-20 15:45:36 +0000 | [diff] [blame] | 4693 | } |
drh | 2dca868 | 2008-03-21 17:13:13 +0000 | [diff] [blame] | 4694 | sqlite3VdbeChangeEncoding(pMem, encoding); |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 4695 | UPDATE_MAX_BLOBSIZE(pMem); |
drh | 023ae03 | 2007-05-08 12:12:16 +0000 | [diff] [blame] | 4696 | if( sqlite3VdbeMemTooBig(pMem) ){ |
| 4697 | goto too_big; |
| 4698 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4699 | break; |
| 4700 | } |
| 4701 | |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4702 | |
drh | fdbcdee | 2007-03-27 14:44:50 +0000 | [diff] [blame] | 4703 | #if !defined(SQLITE_OMIT_VACUUM) && !defined(SQLITE_OMIT_ATTACH) |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4704 | /* Opcode: Vacuum * * * * * |
drh | 6f8c91c | 2003-12-07 00:24:35 +0000 | [diff] [blame] | 4705 | ** |
| 4706 | ** Vacuum the entire database. This opcode will cause other virtual |
| 4707 | ** machines to be created and run. It may not be called from within |
| 4708 | ** a transaction. |
| 4709 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4710 | case OP_Vacuum: { |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 4711 | if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse; |
| 4712 | rc = sqlite3RunVacuum(&p->zErrMsg, db); |
| 4713 | if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse; |
drh | 6f8c91c | 2003-12-07 00:24:35 +0000 | [diff] [blame] | 4714 | break; |
| 4715 | } |
drh | 154d4b2 | 2006-09-21 11:02:16 +0000 | [diff] [blame] | 4716 | #endif |
drh | 6f8c91c | 2003-12-07 00:24:35 +0000 | [diff] [blame] | 4717 | |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 4718 | #if !defined(SQLITE_OMIT_AUTOVACUUM) |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4719 | /* Opcode: IncrVacuum P1 P2 * * * |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 4720 | ** |
| 4721 | ** Perform a single step of the incremental vacuum procedure on |
drh | ca5557f | 2007-05-04 18:30:40 +0000 | [diff] [blame] | 4722 | ** the P1 database. If the vacuum has finished, jump to instruction |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 4723 | ** P2. Otherwise, fall through to the next instruction. |
| 4724 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4725 | case OP_IncrVacuum: { /* jump */ |
drh | ca5557f | 2007-05-04 18:30:40 +0000 | [diff] [blame] | 4726 | Btree *pBt; |
| 4727 | |
| 4728 | assert( pOp->p1>=0 && pOp->p1<db->nDb ); |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 4729 | assert( (p->btreeMask & (1<<pOp->p1))!=0 ); |
drh | ca5557f | 2007-05-04 18:30:40 +0000 | [diff] [blame] | 4730 | pBt = db->aDb[pOp->p1].pBt; |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 4731 | rc = sqlite3BtreeIncrVacuum(pBt); |
| 4732 | if( rc==SQLITE_DONE ){ |
| 4733 | pc = pOp->p2 - 1; |
| 4734 | rc = SQLITE_OK; |
| 4735 | } |
| 4736 | break; |
| 4737 | } |
| 4738 | #endif |
| 4739 | |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4740 | /* Opcode: Expire P1 * * * * |
danielk1977 | a21c6b6 | 2005-01-24 10:25:59 +0000 | [diff] [blame] | 4741 | ** |
| 4742 | ** Cause precompiled statements to become expired. An expired statement |
| 4743 | ** fails with an error code of SQLITE_SCHEMA if it is ever executed |
| 4744 | ** (via sqlite3_step()). |
| 4745 | ** |
| 4746 | ** If P1 is 0, then all SQL statements become expired. If P1 is non-zero, |
| 4747 | ** then only the currently executing statement is affected. |
| 4748 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4749 | case OP_Expire: { |
danielk1977 | a21c6b6 | 2005-01-24 10:25:59 +0000 | [diff] [blame] | 4750 | if( !pOp->p1 ){ |
| 4751 | sqlite3ExpirePreparedStatements(db); |
| 4752 | }else{ |
| 4753 | p->expired = 1; |
| 4754 | } |
| 4755 | break; |
| 4756 | } |
| 4757 | |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 4758 | #ifndef SQLITE_OMIT_SHARED_CACHE |
drh | 6a9ad3d | 2008-04-02 16:29:30 +0000 | [diff] [blame] | 4759 | /* Opcode: TableLock P1 P2 P3 P4 * |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 4760 | ** |
| 4761 | ** Obtain a lock on a particular table. This instruction is only used when |
| 4762 | ** the shared-cache feature is enabled. |
| 4763 | ** |
drh | 6a9ad3d | 2008-04-02 16:29:30 +0000 | [diff] [blame] | 4764 | ** If P1 is the index of the database in sqlite3.aDb[] of the database |
| 4765 | ** on which the lock is acquired. A readlock is obtained if P3==0 or |
| 4766 | ** a write lock if P3==1. |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 4767 | ** |
| 4768 | ** P2 contains the root-page of the table to lock. |
| 4769 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 4770 | ** 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] | 4771 | ** used to generate an error message if the lock cannot be obtained. |
| 4772 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4773 | case OP_TableLock: { |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 4774 | int p1 = pOp->p1; |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 4775 | u8 isWriteLock = (u8)pOp->p3; |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 4776 | assert( p1>=0 && p1<db->nDb ); |
| 4777 | assert( (p->btreeMask & (1<<p1))!=0 ); |
drh | 6a9ad3d | 2008-04-02 16:29:30 +0000 | [diff] [blame] | 4778 | assert( isWriteLock==0 || isWriteLock==1 ); |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 4779 | rc = sqlite3BtreeLockTable(db->aDb[p1].pBt, pOp->p2, isWriteLock); |
| 4780 | if( rc==SQLITE_LOCKED ){ |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 4781 | const char *z = pOp->p4.z; |
drh | f089aa4 | 2008-07-08 19:34:06 +0000 | [diff] [blame] | 4782 | sqlite3SetString(&p->zErrMsg, db, "database table is locked: %s", z); |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 4783 | } |
| 4784 | break; |
| 4785 | } |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 4786 | #endif /* SQLITE_OMIT_SHARED_CACHE */ |
| 4787 | |
| 4788 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4789 | /* Opcode: VBegin * * * P4 * |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 4790 | ** |
danielk1977 | 3e3a84d | 2008-08-01 17:37:40 +0000 | [diff] [blame] | 4791 | ** P4 may be a pointer to an sqlite3_vtab structure. If so, call the |
| 4792 | ** xBegin method for that table. |
| 4793 | ** |
| 4794 | ** Also, whether or not P4 is set, check that this is not being called from |
| 4795 | ** within a callback to a virtual table xSync() method. If it is, set the |
| 4796 | ** error code to SQLITE_LOCKED. |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 4797 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4798 | case OP_VBegin: { |
danielk1977 | 3e3a84d | 2008-08-01 17:37:40 +0000 | [diff] [blame] | 4799 | sqlite3_vtab *pVtab = pOp->p4.pVtab; |
| 4800 | rc = sqlite3VtabBegin(db, pVtab); |
| 4801 | if( pVtab ){ |
| 4802 | sqlite3DbFree(db, p->zErrMsg); |
| 4803 | p->zErrMsg = pVtab->zErrMsg; |
| 4804 | pVtab->zErrMsg = 0; |
| 4805 | } |
danielk1977 | f9e7dda | 2006-06-16 16:08:53 +0000 | [diff] [blame] | 4806 | break; |
| 4807 | } |
| 4808 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 4809 | |
| 4810 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4811 | /* Opcode: VCreate P1 * * P4 * |
danielk1977 | f9e7dda | 2006-06-16 16:08:53 +0000 | [diff] [blame] | 4812 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 4813 | ** 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] | 4814 | ** for that table. |
| 4815 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4816 | case OP_VCreate: { |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 4817 | rc = sqlite3VtabCallCreate(db, pOp->p1, pOp->p4.z, &p->zErrMsg); |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 4818 | break; |
| 4819 | } |
| 4820 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 4821 | |
| 4822 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4823 | /* Opcode: VDestroy P1 * * P4 * |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 4824 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 4825 | ** 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] | 4826 | ** of that table. |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 4827 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4828 | case OP_VDestroy: { |
danielk1977 | 212b218 | 2006-06-23 14:32:08 +0000 | [diff] [blame] | 4829 | p->inVtabMethod = 2; |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 4830 | rc = sqlite3VtabCallDestroy(db, pOp->p1, pOp->p4.z); |
danielk1977 | 212b218 | 2006-06-23 14:32:08 +0000 | [diff] [blame] | 4831 | p->inVtabMethod = 0; |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 4832 | break; |
| 4833 | } |
| 4834 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 4835 | |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 4836 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4837 | /* Opcode: VOpen P1 * * P4 * |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 4838 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 4839 | ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure. |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 4840 | ** P1 is a cursor number. This opcode opens a cursor to the virtual |
| 4841 | ** table and stores that cursor in P1. |
| 4842 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4843 | case OP_VOpen: { |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 4844 | VdbeCursor *pCur = 0; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 4845 | sqlite3_vtab_cursor *pVtabCursor = 0; |
| 4846 | |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 4847 | sqlite3_vtab *pVtab = pOp->p4.pVtab; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 4848 | sqlite3_module *pModule = (sqlite3_module *)pVtab->pModule; |
| 4849 | |
| 4850 | assert(pVtab && pModule); |
| 4851 | if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse; |
| 4852 | rc = pModule->xOpen(pVtab, &pVtabCursor); |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 4853 | sqlite3DbFree(db, p->zErrMsg); |
drh | 80cc85b | 2008-07-23 21:07:25 +0000 | [diff] [blame] | 4854 | p->zErrMsg = pVtab->zErrMsg; |
| 4855 | pVtab->zErrMsg = 0; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 4856 | if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse; |
| 4857 | if( SQLITE_OK==rc ){ |
shane | 21e7feb | 2008-05-30 15:59:49 +0000 | [diff] [blame] | 4858 | /* Initialize sqlite3_vtab_cursor base class */ |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 4859 | pVtabCursor->pVtab = pVtab; |
| 4860 | |
| 4861 | /* Initialise vdbe cursor object */ |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 4862 | pCur = allocateCursor(p, pOp->p1, &pOp[-1], -1, 0); |
danielk1977 | be71889 | 2006-06-23 08:05:19 +0000 | [diff] [blame] | 4863 | if( pCur ){ |
| 4864 | pCur->pVtabCursor = pVtabCursor; |
| 4865 | pCur->pModule = pVtabCursor->pVtab->pModule; |
danielk1977 | b7a2f2e | 2006-06-23 11:34:54 +0000 | [diff] [blame] | 4866 | }else{ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 4867 | db->mallocFailed = 1; |
danielk1977 | b7a2f2e | 2006-06-23 11:34:54 +0000 | [diff] [blame] | 4868 | pModule->xClose(pVtabCursor); |
danielk1977 | be71889 | 2006-06-23 08:05:19 +0000 | [diff] [blame] | 4869 | } |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 4870 | } |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 4871 | break; |
| 4872 | } |
| 4873 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 4874 | |
| 4875 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
danielk1977 | 6dbee81 | 2008-01-03 18:39:41 +0000 | [diff] [blame] | 4876 | /* Opcode: VFilter P1 P2 P3 P4 * |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 4877 | ** |
| 4878 | ** P1 is a cursor opened using VOpen. P2 is an address to jump to if |
| 4879 | ** the filtered result set is empty. |
| 4880 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 4881 | ** P4 is either NULL or a string that was generated by the xBestIndex |
| 4882 | ** method of the module. The interpretation of the P4 string is left |
drh | 4be8b51 | 2006-06-13 23:51:34 +0000 | [diff] [blame] | 4883 | ** to the module implementation. |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 4884 | ** |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 4885 | ** This opcode invokes the xFilter method on the virtual table specified |
danielk1977 | 6dbee81 | 2008-01-03 18:39:41 +0000 | [diff] [blame] | 4886 | ** by P1. The integer query plan parameter to xFilter is stored in register |
| 4887 | ** P3. Register P3+1 stores the argc parameter to be passed to the |
drh | 174edc6 | 2008-05-29 05:23:41 +0000 | [diff] [blame] | 4888 | ** xFilter method. Registers P3+2..P3+1+argc are the argc |
| 4889 | ** additional parameters which are passed to |
danielk1977 | 6dbee81 | 2008-01-03 18:39:41 +0000 | [diff] [blame] | 4890 | ** xFilter as argv. Register P3+2 becomes argv[0] when passed to xFilter. |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 4891 | ** |
danielk1977 | 6dbee81 | 2008-01-03 18:39:41 +0000 | [diff] [blame] | 4892 | ** 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] | 4893 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4894 | case OP_VFilter: { /* jump */ |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 4895 | int nArg; |
danielk1977 | 6dbee81 | 2008-01-03 18:39:41 +0000 | [diff] [blame] | 4896 | int iQuery; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 4897 | const sqlite3_module *pModule; |
danielk1977 | 6dbee81 | 2008-01-03 18:39:41 +0000 | [diff] [blame] | 4898 | Mem *pQuery = &p->aMem[pOp->p3]; |
| 4899 | Mem *pArgc = &pQuery[1]; |
drh | 4dc754d | 2008-07-23 18:17:32 +0000 | [diff] [blame] | 4900 | sqlite3_vtab_cursor *pVtabCursor; |
| 4901 | sqlite3_vtab *pVtab; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 4902 | |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 4903 | VdbeCursor *pCur = p->apCsr[pOp->p1]; |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 4904 | |
| 4905 | REGISTER_TRACE(pOp->p3, pQuery); |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 4906 | assert( pCur->pVtabCursor ); |
drh | 4dc754d | 2008-07-23 18:17:32 +0000 | [diff] [blame] | 4907 | pVtabCursor = pCur->pVtabCursor; |
| 4908 | pVtab = pVtabCursor->pVtab; |
| 4909 | pModule = pVtab->pModule; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 4910 | |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4911 | /* Grab the index number and argc parameters */ |
danielk1977 | 6dbee81 | 2008-01-03 18:39:41 +0000 | [diff] [blame] | 4912 | assert( (pQuery->flags&MEM_Int)!=0 && pArgc->flags==MEM_Int ); |
drh | 9c1905f | 2008-12-10 22:32:56 +0000 | [diff] [blame] | 4913 | nArg = (int)pArgc->u.i; |
| 4914 | iQuery = (int)pQuery->u.i; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 4915 | |
drh | 644a529 | 2006-12-20 14:53:38 +0000 | [diff] [blame] | 4916 | /* Invoke the xFilter method */ |
| 4917 | { |
drh | 3f87d2a | 2006-12-20 14:31:24 +0000 | [diff] [blame] | 4918 | int res = 0; |
drh | 4be8b51 | 2006-06-13 23:51:34 +0000 | [diff] [blame] | 4919 | int i; |
| 4920 | Mem **apArg = p->apArg; |
| 4921 | for(i = 0; i<nArg; i++){ |
danielk1977 | 6dbee81 | 2008-01-03 18:39:41 +0000 | [diff] [blame] | 4922 | apArg[i] = &pArgc[i+1]; |
drh | 4be8b51 | 2006-06-13 23:51:34 +0000 | [diff] [blame] | 4923 | storeTypeInfo(apArg[i], 0); |
danielk1977 | 5fac9f8 | 2006-06-13 14:16:58 +0000 | [diff] [blame] | 4924 | } |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 4925 | |
| 4926 | if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse; |
danielk1977 | 5a114ca | 2008-08-02 15:10:08 +0000 | [diff] [blame] | 4927 | sqlite3VtabLock(pVtab); |
danielk1977 | be71889 | 2006-06-23 08:05:19 +0000 | [diff] [blame] | 4928 | p->inVtabMethod = 1; |
drh | 4dc754d | 2008-07-23 18:17:32 +0000 | [diff] [blame] | 4929 | rc = pModule->xFilter(pVtabCursor, iQuery, pOp->p4.z, nArg, apArg); |
danielk1977 | be71889 | 2006-06-23 08:05:19 +0000 | [diff] [blame] | 4930 | p->inVtabMethod = 0; |
danielk1977 | 3e3a84d | 2008-08-01 17:37:40 +0000 | [diff] [blame] | 4931 | sqlite3DbFree(db, p->zErrMsg); |
| 4932 | p->zErrMsg = pVtab->zErrMsg; |
| 4933 | pVtab->zErrMsg = 0; |
danielk1977 | 5a114ca | 2008-08-02 15:10:08 +0000 | [diff] [blame] | 4934 | sqlite3VtabUnlock(db, pVtab); |
danielk1977 | a298e90 | 2006-06-22 09:53:48 +0000 | [diff] [blame] | 4935 | if( rc==SQLITE_OK ){ |
drh | 4dc754d | 2008-07-23 18:17:32 +0000 | [diff] [blame] | 4936 | res = pModule->xEof(pVtabCursor); |
danielk1977 | a298e90 | 2006-06-22 09:53:48 +0000 | [diff] [blame] | 4937 | } |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 4938 | if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse; |
| 4939 | |
danielk1977 | a298e90 | 2006-06-22 09:53:48 +0000 | [diff] [blame] | 4940 | if( res ){ |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 4941 | pc = pOp->p2 - 1; |
| 4942 | } |
| 4943 | } |
drh | 1d454a3 | 2008-01-31 19:34:51 +0000 | [diff] [blame] | 4944 | pCur->nullRow = 0; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 4945 | |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 4946 | break; |
| 4947 | } |
| 4948 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 4949 | |
| 4950 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4951 | /* Opcode: VRowid P1 P2 * * * |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 4952 | ** |
drh | 2133d82 | 2008-01-03 18:44:59 +0000 | [diff] [blame] | 4953 | ** Store into register P2 the rowid of |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 4954 | ** the virtual-table that the P1 cursor is pointing to. |
| 4955 | */ |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 4956 | case OP_VRowid: { /* out2-prerelease */ |
danielk1977 | 3e3a84d | 2008-08-01 17:37:40 +0000 | [diff] [blame] | 4957 | sqlite3_vtab *pVtab; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 4958 | const sqlite3_module *pModule; |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 4959 | sqlite_int64 iRow; |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 4960 | VdbeCursor *pCur = p->apCsr[pOp->p1]; |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 4961 | |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 4962 | assert( pCur->pVtabCursor ); |
drh | 2945b4a | 2008-01-31 15:53:45 +0000 | [diff] [blame] | 4963 | if( pCur->nullRow ){ |
| 4964 | break; |
| 4965 | } |
danielk1977 | 3e3a84d | 2008-08-01 17:37:40 +0000 | [diff] [blame] | 4966 | pVtab = pCur->pVtabCursor->pVtab; |
| 4967 | pModule = pVtab->pModule; |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 4968 | assert( pModule->xRowid ); |
| 4969 | if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse; |
| 4970 | rc = pModule->xRowid(pCur->pVtabCursor, &iRow); |
danielk1977 | 3e3a84d | 2008-08-01 17:37:40 +0000 | [diff] [blame] | 4971 | sqlite3DbFree(db, p->zErrMsg); |
| 4972 | p->zErrMsg = pVtab->zErrMsg; |
| 4973 | pVtab->zErrMsg = 0; |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 4974 | if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 4975 | MemSetTypeFlag(pOut, MEM_Int); |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 4976 | pOut->u.i = iRow; |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 4977 | break; |
| 4978 | } |
| 4979 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 4980 | |
| 4981 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 4982 | /* Opcode: VColumn P1 P2 P3 * * |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 4983 | ** |
drh | 2133d82 | 2008-01-03 18:44:59 +0000 | [diff] [blame] | 4984 | ** Store the value of the P2-th column of |
| 4985 | ** the row of the virtual-table that the |
| 4986 | ** P1 cursor is pointing to into register P3. |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 4987 | */ |
| 4988 | case OP_VColumn: { |
danielk1977 | 3e3a84d | 2008-08-01 17:37:40 +0000 | [diff] [blame] | 4989 | sqlite3_vtab *pVtab; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 4990 | const sqlite3_module *pModule; |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 4991 | Mem *pDest; |
| 4992 | sqlite3_context sContext; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 4993 | |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 4994 | VdbeCursor *pCur = p->apCsr[pOp->p1]; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 4995 | assert( pCur->pVtabCursor ); |
drh | 2945b4a | 2008-01-31 15:53:45 +0000 | [diff] [blame] | 4996 | assert( pOp->p3>0 && pOp->p3<=p->nMem ); |
| 4997 | pDest = &p->aMem[pOp->p3]; |
| 4998 | if( pCur->nullRow ){ |
| 4999 | sqlite3VdbeMemSetNull(pDest); |
| 5000 | break; |
| 5001 | } |
danielk1977 | 3e3a84d | 2008-08-01 17:37:40 +0000 | [diff] [blame] | 5002 | pVtab = pCur->pVtabCursor->pVtab; |
| 5003 | pModule = pVtab->pModule; |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 5004 | assert( pModule->xColumn ); |
| 5005 | memset(&sContext, 0, sizeof(sContext)); |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 5006 | |
| 5007 | /* The output cell may already have a buffer allocated. Move |
| 5008 | ** the current contents to sContext.s so in case the user-function |
| 5009 | ** can use the already allocated buffer instead of allocating a |
| 5010 | ** new one. |
| 5011 | */ |
| 5012 | sqlite3VdbeMemMove(&sContext.s, pDest); |
| 5013 | MemSetTypeFlag(&sContext.s, MEM_Null); |
| 5014 | |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 5015 | if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse; |
| 5016 | rc = pModule->xColumn(pCur->pVtabCursor, &sContext, pOp->p2); |
danielk1977 | 3e3a84d | 2008-08-01 17:37:40 +0000 | [diff] [blame] | 5017 | sqlite3DbFree(db, p->zErrMsg); |
| 5018 | p->zErrMsg = pVtab->zErrMsg; |
| 5019 | pVtab->zErrMsg = 0; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 5020 | |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 5021 | /* Copy the result of the function to the P3 register. We |
| 5022 | ** do this regardless of whether or not an error occured to ensure any |
| 5023 | ** dynamic allocation in sContext.s (a Mem struct) is released. |
| 5024 | */ |
| 5025 | sqlite3VdbeChangeEncoding(&sContext.s, encoding); |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 5026 | REGISTER_TRACE(pOp->p3, pDest); |
| 5027 | sqlite3VdbeMemMove(pDest, &sContext.s); |
| 5028 | UPDATE_MAX_BLOBSIZE(pDest); |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 5029 | |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 5030 | if( sqlite3SafetyOn(db) ){ |
| 5031 | goto abort_due_to_misuse; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 5032 | } |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 5033 | if( sqlite3VdbeMemTooBig(pDest) ){ |
| 5034 | goto too_big; |
| 5035 | } |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 5036 | break; |
| 5037 | } |
| 5038 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 5039 | |
| 5040 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 5041 | /* Opcode: VNext P1 P2 * * * |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 5042 | ** |
| 5043 | ** Advance virtual table P1 to the next row in its result set and |
| 5044 | ** jump to instruction P2. Or, if the virtual table has reached |
| 5045 | ** the end of its result set, then fall through to the next instruction. |
| 5046 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 5047 | case OP_VNext: { /* jump */ |
danielk1977 | 3e3a84d | 2008-08-01 17:37:40 +0000 | [diff] [blame] | 5048 | sqlite3_vtab *pVtab; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 5049 | const sqlite3_module *pModule; |
| 5050 | int res = 0; |
| 5051 | |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 5052 | VdbeCursor *pCur = p->apCsr[pOp->p1]; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 5053 | assert( pCur->pVtabCursor ); |
drh | 2945b4a | 2008-01-31 15:53:45 +0000 | [diff] [blame] | 5054 | if( pCur->nullRow ){ |
| 5055 | break; |
| 5056 | } |
danielk1977 | 3e3a84d | 2008-08-01 17:37:40 +0000 | [diff] [blame] | 5057 | pVtab = pCur->pVtabCursor->pVtab; |
| 5058 | pModule = pVtab->pModule; |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 5059 | assert( pModule->xNext ); |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 5060 | |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 5061 | /* Invoke the xNext() method of the module. There is no way for the |
| 5062 | ** underlying implementation to return an error if one occurs during |
| 5063 | ** xNext(). Instead, if an error occurs, true is returned (indicating that |
| 5064 | ** data is available) and the error code returned when xColumn or |
| 5065 | ** some other method is next invoked on the save virtual table cursor. |
| 5066 | */ |
| 5067 | if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse; |
danielk1977 | 5a114ca | 2008-08-02 15:10:08 +0000 | [diff] [blame] | 5068 | sqlite3VtabLock(pVtab); |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 5069 | p->inVtabMethod = 1; |
| 5070 | rc = pModule->xNext(pCur->pVtabCursor); |
| 5071 | p->inVtabMethod = 0; |
danielk1977 | 3e3a84d | 2008-08-01 17:37:40 +0000 | [diff] [blame] | 5072 | sqlite3DbFree(db, p->zErrMsg); |
| 5073 | p->zErrMsg = pVtab->zErrMsg; |
| 5074 | pVtab->zErrMsg = 0; |
danielk1977 | 5a114ca | 2008-08-02 15:10:08 +0000 | [diff] [blame] | 5075 | sqlite3VtabUnlock(db, pVtab); |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 5076 | if( rc==SQLITE_OK ){ |
| 5077 | res = pModule->xEof(pCur->pVtabCursor); |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 5078 | } |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 5079 | if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse; |
danielk1977 | b7a7b9a | 2006-06-13 10:24:42 +0000 | [diff] [blame] | 5080 | |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 5081 | if( !res ){ |
| 5082 | /* If there is data, jump to P2 */ |
| 5083 | pc = pOp->p2 - 1; |
| 5084 | } |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 5085 | break; |
| 5086 | } |
| 5087 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 5088 | |
danielk1977 | 182c4ba | 2007-06-27 15:53:34 +0000 | [diff] [blame] | 5089 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 5090 | /* Opcode: VRename P1 * * P4 * |
danielk1977 | 182c4ba | 2007-06-27 15:53:34 +0000 | [diff] [blame] | 5091 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 5092 | ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure. |
danielk1977 | 182c4ba | 2007-06-27 15:53:34 +0000 | [diff] [blame] | 5093 | ** This opcode invokes the corresponding xRename method. The value |
danielk1977 | 6dbee81 | 2008-01-03 18:39:41 +0000 | [diff] [blame] | 5094 | ** in register P1 is passed as the zName argument to the xRename method. |
danielk1977 | 182c4ba | 2007-06-27 15:53:34 +0000 | [diff] [blame] | 5095 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 5096 | case OP_VRename: { |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 5097 | sqlite3_vtab *pVtab = pOp->p4.pVtab; |
danielk1977 | 6dbee81 | 2008-01-03 18:39:41 +0000 | [diff] [blame] | 5098 | Mem *pName = &p->aMem[pOp->p1]; |
danielk1977 | 182c4ba | 2007-06-27 15:53:34 +0000 | [diff] [blame] | 5099 | assert( pVtab->pModule->xRename ); |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 5100 | REGISTER_TRACE(pOp->p1, pName); |
danielk1977 | 182c4ba | 2007-06-27 15:53:34 +0000 | [diff] [blame] | 5101 | |
danielk1977 | 6dbee81 | 2008-01-03 18:39:41 +0000 | [diff] [blame] | 5102 | Stringify(pName, encoding); |
danielk1977 | 182c4ba | 2007-06-27 15:53:34 +0000 | [diff] [blame] | 5103 | |
| 5104 | if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse; |
| 5105 | sqlite3VtabLock(pVtab); |
danielk1977 | 6dbee81 | 2008-01-03 18:39:41 +0000 | [diff] [blame] | 5106 | rc = pVtab->pModule->xRename(pVtab, pName->z); |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 5107 | sqlite3DbFree(db, p->zErrMsg); |
drh | 80cc85b | 2008-07-23 21:07:25 +0000 | [diff] [blame] | 5108 | p->zErrMsg = pVtab->zErrMsg; |
| 5109 | pVtab->zErrMsg = 0; |
danielk1977 | 182c4ba | 2007-06-27 15:53:34 +0000 | [diff] [blame] | 5110 | sqlite3VtabUnlock(db, pVtab); |
| 5111 | if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse; |
| 5112 | |
danielk1977 | 182c4ba | 2007-06-27 15:53:34 +0000 | [diff] [blame] | 5113 | break; |
| 5114 | } |
| 5115 | #endif |
drh | 4cbdda9 | 2006-06-14 19:00:20 +0000 | [diff] [blame] | 5116 | |
| 5117 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 5118 | /* Opcode: VUpdate P1 P2 P3 P4 * |
danielk1977 | 399918f | 2006-06-14 13:03:23 +0000 | [diff] [blame] | 5119 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 5120 | ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure. |
danielk1977 | 399918f | 2006-06-14 13:03:23 +0000 | [diff] [blame] | 5121 | ** This opcode invokes the corresponding xUpdate method. P2 values |
danielk1977 | 2a339ff | 2008-01-03 17:31:44 +0000 | [diff] [blame] | 5122 | ** are contiguous memory cells starting at P3 to pass to the xUpdate |
| 5123 | ** invocation. The value in register (P3+P2-1) corresponds to the |
| 5124 | ** p2th element of the argv array passed to xUpdate. |
drh | 4cbdda9 | 2006-06-14 19:00:20 +0000 | [diff] [blame] | 5125 | ** |
| 5126 | ** The xUpdate method will do a DELETE or an INSERT or both. |
danielk1977 | 2a339ff | 2008-01-03 17:31:44 +0000 | [diff] [blame] | 5127 | ** The argv[0] element (which corresponds to memory cell P3) |
| 5128 | ** is the rowid of a row to delete. If argv[0] is NULL then no |
| 5129 | ** deletion occurs. The argv[1] element is the rowid of the new |
| 5130 | ** row. This can be NULL to have the virtual table select the new |
| 5131 | ** rowid for itself. The subsequent elements in the array are |
| 5132 | ** the values of columns in the new row. |
drh | 4cbdda9 | 2006-06-14 19:00:20 +0000 | [diff] [blame] | 5133 | ** |
| 5134 | ** If P2==1 then no insert is performed. argv[0] is the rowid of |
| 5135 | ** a row to delete. |
danielk1977 | 1f6eec5 | 2006-06-16 06:17:47 +0000 | [diff] [blame] | 5136 | ** |
| 5137 | ** P1 is a boolean flag. If it is set to true and the xUpdate call |
| 5138 | ** is successful, then the value returned by sqlite3_last_insert_rowid() |
| 5139 | ** is set to the value of the rowid for the row just inserted. |
danielk1977 | 399918f | 2006-06-14 13:03:23 +0000 | [diff] [blame] | 5140 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 5141 | case OP_VUpdate: { |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 5142 | sqlite3_vtab *pVtab = pOp->p4.pVtab; |
danielk1977 | 399918f | 2006-06-14 13:03:23 +0000 | [diff] [blame] | 5143 | sqlite3_module *pModule = (sqlite3_module *)pVtab->pModule; |
drh | 4cbdda9 | 2006-06-14 19:00:20 +0000 | [diff] [blame] | 5144 | int nArg = pOp->p2; |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 5145 | assert( pOp->p4type==P4_VTAB ); |
danielk1977 | 399918f | 2006-06-14 13:03:23 +0000 | [diff] [blame] | 5146 | if( pModule->xUpdate==0 ){ |
drh | f089aa4 | 2008-07-08 19:34:06 +0000 | [diff] [blame] | 5147 | sqlite3SetString(&p->zErrMsg, db, "read-only table"); |
danielk1977 | 399918f | 2006-06-14 13:03:23 +0000 | [diff] [blame] | 5148 | rc = SQLITE_ERROR; |
| 5149 | }else{ |
| 5150 | int i; |
danielk1977 | 1f6eec5 | 2006-06-16 06:17:47 +0000 | [diff] [blame] | 5151 | sqlite_int64 rowid; |
danielk1977 | 399918f | 2006-06-14 13:03:23 +0000 | [diff] [blame] | 5152 | Mem **apArg = p->apArg; |
danielk1977 | 2a339ff | 2008-01-03 17:31:44 +0000 | [diff] [blame] | 5153 | Mem *pX = &p->aMem[pOp->p3]; |
| 5154 | for(i=0; i<nArg; i++){ |
drh | 9c41938 | 2006-06-16 21:13:21 +0000 | [diff] [blame] | 5155 | storeTypeInfo(pX, 0); |
| 5156 | apArg[i] = pX; |
danielk1977 | 2a339ff | 2008-01-03 17:31:44 +0000 | [diff] [blame] | 5157 | pX++; |
danielk1977 | 399918f | 2006-06-14 13:03:23 +0000 | [diff] [blame] | 5158 | } |
danielk1977 | c7d5410 | 2006-06-15 07:29:00 +0000 | [diff] [blame] | 5159 | if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse; |
drh | 189d4af | 2006-09-02 20:57:52 +0000 | [diff] [blame] | 5160 | sqlite3VtabLock(pVtab); |
danielk1977 | 1f6eec5 | 2006-06-16 06:17:47 +0000 | [diff] [blame] | 5161 | rc = pModule->xUpdate(pVtab, nArg, apArg, &rowid); |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 5162 | sqlite3DbFree(db, p->zErrMsg); |
drh | 80cc85b | 2008-07-23 21:07:25 +0000 | [diff] [blame] | 5163 | p->zErrMsg = pVtab->zErrMsg; |
| 5164 | pVtab->zErrMsg = 0; |
danielk1977 | a04a34f | 2007-04-16 15:06:25 +0000 | [diff] [blame] | 5165 | sqlite3VtabUnlock(db, pVtab); |
danielk1977 | c7d5410 | 2006-06-15 07:29:00 +0000 | [diff] [blame] | 5166 | if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse; |
danielk1977 | 1f6eec5 | 2006-06-16 06:17:47 +0000 | [diff] [blame] | 5167 | if( pOp->p1 && rc==SQLITE_OK ){ |
| 5168 | assert( nArg>1 && apArg[0] && (apArg[0]->flags&MEM_Null) ); |
| 5169 | db->lastRowid = rowid; |
| 5170 | } |
drh | b5df144 | 2008-04-10 14:00:09 +0000 | [diff] [blame] | 5171 | p->nChange++; |
danielk1977 | 399918f | 2006-06-14 13:03:23 +0000 | [diff] [blame] | 5172 | } |
drh | 4cbdda9 | 2006-06-14 19:00:20 +0000 | [diff] [blame] | 5173 | break; |
danielk1977 | 399918f | 2006-06-14 13:03:23 +0000 | [diff] [blame] | 5174 | } |
| 5175 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 5176 | |
danielk1977 | 59a9379 | 2008-05-15 17:48:20 +0000 | [diff] [blame] | 5177 | #ifndef SQLITE_OMIT_PAGER_PRAGMAS |
| 5178 | /* Opcode: Pagecount P1 P2 * * * |
| 5179 | ** |
| 5180 | ** Write the current number of pages in database P1 to memory cell P2. |
| 5181 | */ |
| 5182 | case OP_Pagecount: { /* out2-prerelease */ |
| 5183 | int p1 = pOp->p1; |
| 5184 | int nPage; |
| 5185 | Pager *pPager = sqlite3BtreePager(db->aDb[p1].pBt); |
| 5186 | |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame] | 5187 | rc = sqlite3PagerPagecount(pPager, &nPage); |
| 5188 | if( rc==SQLITE_OK ){ |
danielk1977 | 59a9379 | 2008-05-15 17:48:20 +0000 | [diff] [blame] | 5189 | pOut->flags = MEM_Int; |
| 5190 | pOut->u.i = nPage; |
| 5191 | } |
| 5192 | break; |
| 5193 | } |
| 5194 | #endif |
| 5195 | |
drh | 949f9cd | 2008-01-12 21:35:57 +0000 | [diff] [blame] | 5196 | #ifndef SQLITE_OMIT_TRACE |
| 5197 | /* Opcode: Trace * * * P4 * |
| 5198 | ** |
| 5199 | ** If tracing is enabled (by the sqlite3_trace()) interface, then |
| 5200 | ** the UTF-8 string contained in P4 is emitted on the trace callback. |
| 5201 | */ |
| 5202 | case OP_Trace: { |
| 5203 | if( pOp->p4.z ){ |
| 5204 | if( db->xTrace ){ |
| 5205 | db->xTrace(db->pTraceArg, pOp->p4.z); |
| 5206 | } |
| 5207 | #ifdef SQLITE_DEBUG |
| 5208 | if( (db->flags & SQLITE_SqlTrace)!=0 ){ |
| 5209 | sqlite3DebugPrintf("SQL-trace: %s\n", pOp->p4.z); |
| 5210 | } |
| 5211 | #endif /* SQLITE_DEBUG */ |
| 5212 | } |
| 5213 | break; |
| 5214 | } |
| 5215 | #endif |
| 5216 | |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 5217 | |
| 5218 | /* Opcode: Noop * * * * * |
| 5219 | ** |
| 5220 | ** Do nothing. This instruction is often useful as a jump |
| 5221 | ** destination. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5222 | */ |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 5223 | /* |
| 5224 | ** The magic Explain opcode are only inserted when explain==2 (which |
| 5225 | ** is to say when the EXPLAIN QUERY PLAN syntax is used.) |
| 5226 | ** This opcode records information from the optimizer. It is the |
| 5227 | ** the same as a no-op. This opcodesnever appears in a real VM program. |
| 5228 | */ |
| 5229 | default: { /* This is really OP_Noop and OP_Explain */ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5230 | break; |
| 5231 | } |
| 5232 | |
| 5233 | /***************************************************************************** |
| 5234 | ** The cases of the switch statement above this line should all be indented |
| 5235 | ** by 6 spaces. But the left-most 6 spaces have been removed to improve the |
| 5236 | ** readability. From this point on down, the normal indentation rules are |
| 5237 | ** restored. |
| 5238 | *****************************************************************************/ |
| 5239 | } |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 5240 | |
drh | 7b39686 | 2003-01-01 23:06:20 +0000 | [diff] [blame] | 5241 | #ifdef VDBE_PROFILE |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 5242 | { |
shane | 9bcbdad | 2008-05-29 20:22:37 +0000 | [diff] [blame] | 5243 | u64 elapsed = sqlite3Hwtime() - start; |
| 5244 | pOp->cycles += elapsed; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 5245 | pOp->cnt++; |
| 5246 | #if 0 |
shane | 9bcbdad | 2008-05-29 20:22:37 +0000 | [diff] [blame] | 5247 | fprintf(stdout, "%10llu ", elapsed); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 5248 | sqlite3VdbePrintOp(stdout, origPc, &p->aOp[origPc]); |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 5249 | #endif |
| 5250 | } |
drh | 7b39686 | 2003-01-01 23:06:20 +0000 | [diff] [blame] | 5251 | #endif |
| 5252 | |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 5253 | /* The following code adds nothing to the actual functionality |
| 5254 | ** of the program. It is only here for testing and debugging. |
| 5255 | ** On the other hand, it does burn CPU cycles every time through |
| 5256 | ** the evaluator loop. So we can leave it out when NDEBUG is defined. |
| 5257 | */ |
| 5258 | #ifndef NDEBUG |
drh | a611040 | 2005-07-28 20:51:19 +0000 | [diff] [blame] | 5259 | assert( pc>=-1 && pc<p->nOp ); |
drh | ae7e151 | 2007-05-02 16:51:59 +0000 | [diff] [blame] | 5260 | |
drh | cf1023c | 2007-05-08 20:59:49 +0000 | [diff] [blame] | 5261 | #ifdef SQLITE_DEBUG |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 5262 | if( p->trace ){ |
| 5263 | if( rc!=0 ) fprintf(p->trace,"rc=%d\n",rc); |
drh | ca48c90 | 2008-01-18 14:08:24 +0000 | [diff] [blame] | 5264 | if( opProperty & OPFLG_OUT2_PRERELEASE ){ |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 5265 | registerTrace(p->trace, pOp->p2, pOut); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5266 | } |
drh | ca48c90 | 2008-01-18 14:08:24 +0000 | [diff] [blame] | 5267 | if( opProperty & OPFLG_OUT3 ){ |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 5268 | registerTrace(p->trace, pOp->p3, pOut); |
| 5269 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5270 | } |
danielk1977 | b5402fb | 2005-01-12 07:15:04 +0000 | [diff] [blame] | 5271 | #endif /* SQLITE_DEBUG */ |
| 5272 | #endif /* NDEBUG */ |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 5273 | } /* The end of the for(;;) loop the loops through opcodes */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5274 | |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 5275 | /* If we reach this point, it means that execution is finished with |
| 5276 | ** an error of some kind. |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 5277 | */ |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 5278 | vdbe_error_halt: |
| 5279 | assert( rc ); |
| 5280 | p->rc = rc; |
drh | 92f02c3 | 2004-09-02 14:57:08 +0000 | [diff] [blame] | 5281 | sqlite3VdbeHalt(p); |
danielk1977 | 7eaabcd | 2008-07-07 14:56:56 +0000 | [diff] [blame] | 5282 | if( rc==SQLITE_IOERR_NOMEM ) db->mallocFailed = 1; |
| 5283 | rc = SQLITE_ERROR; |
drh | 900b31e | 2007-08-28 02:27:51 +0000 | [diff] [blame] | 5284 | |
| 5285 | /* This is the only way out of this procedure. We have to |
| 5286 | ** release the mutexes on btrees that were acquired at the |
| 5287 | ** top. */ |
| 5288 | vdbe_return: |
drh | 4cf7c7f | 2007-08-28 23:28:07 +0000 | [diff] [blame] | 5289 | sqlite3BtreeMutexArrayLeave(&p->aMutex); |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 5290 | return rc; |
| 5291 | |
drh | 023ae03 | 2007-05-08 12:12:16 +0000 | [diff] [blame] | 5292 | /* Jump to here if a string or blob larger than SQLITE_MAX_LENGTH |
| 5293 | ** is encountered. |
| 5294 | */ |
| 5295 | too_big: |
drh | f089aa4 | 2008-07-08 19:34:06 +0000 | [diff] [blame] | 5296 | sqlite3SetString(&p->zErrMsg, db, "string or blob too big"); |
drh | 023ae03 | 2007-05-08 12:12:16 +0000 | [diff] [blame] | 5297 | rc = SQLITE_TOOBIG; |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 5298 | goto vdbe_error_halt; |
drh | 023ae03 | 2007-05-08 12:12:16 +0000 | [diff] [blame] | 5299 | |
drh | 98640a3 | 2007-06-07 19:08:32 +0000 | [diff] [blame] | 5300 | /* Jump to here if a malloc() fails. |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 5301 | */ |
| 5302 | no_mem: |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 5303 | db->mallocFailed = 1; |
drh | f089aa4 | 2008-07-08 19:34:06 +0000 | [diff] [blame] | 5304 | sqlite3SetString(&p->zErrMsg, db, "out of memory"); |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 5305 | rc = SQLITE_NOMEM; |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 5306 | goto vdbe_error_halt; |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 5307 | |
| 5308 | /* Jump to here for an SQLITE_MISUSE error. |
| 5309 | */ |
| 5310 | abort_due_to_misuse: |
| 5311 | rc = SQLITE_MISUSE; |
| 5312 | /* Fall thru into abort_due_to_error */ |
| 5313 | |
| 5314 | /* Jump to here for any other kind of fatal error. The "rc" variable |
| 5315 | ** should hold the error number. |
| 5316 | */ |
| 5317 | abort_due_to_error: |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 5318 | assert( p->zErrMsg==0 ); |
| 5319 | if( db->mallocFailed ) rc = SQLITE_NOMEM; |
danielk1977 | 7eaabcd | 2008-07-07 14:56:56 +0000 | [diff] [blame] | 5320 | if( rc!=SQLITE_IOERR_NOMEM ){ |
drh | f089aa4 | 2008-07-08 19:34:06 +0000 | [diff] [blame] | 5321 | sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3ErrStr(rc)); |
danielk1977 | 7eaabcd | 2008-07-07 14:56:56 +0000 | [diff] [blame] | 5322 | } |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 5323 | goto vdbe_error_halt; |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 5324 | |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 5325 | /* Jump to here if the sqlite3_interrupt() API sets the interrupt |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 5326 | ** flag. |
| 5327 | */ |
| 5328 | abort_due_to_interrupt: |
drh | 881feaa | 2006-07-26 01:39:30 +0000 | [diff] [blame] | 5329 | assert( db->u1.isInterrupted ); |
drh | 7e8b848 | 2008-01-23 03:03:05 +0000 | [diff] [blame] | 5330 | rc = SQLITE_INTERRUPT; |
danielk1977 | 026d270 | 2004-06-14 13:14:59 +0000 | [diff] [blame] | 5331 | p->rc = rc; |
drh | f089aa4 | 2008-07-08 19:34:06 +0000 | [diff] [blame] | 5332 | sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3ErrStr(rc)); |
drh | a05a722 | 2008-01-19 03:35:58 +0000 | [diff] [blame] | 5333 | goto vdbe_error_halt; |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 5334 | } |