drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 1 | /* |
| 2 | ** 2003 September 6 |
| 3 | ** |
| 4 | ** The author disclaims copyright to this source code. In place of |
| 5 | ** a legal notice, here is a blessing: |
| 6 | ** |
| 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. |
| 10 | ** |
| 11 | ************************************************************************* |
| 12 | ** This file contains code used for creating, destroying, and populating |
danielk1977 | fc57d7b | 2004-05-26 02:04:57 +0000 | [diff] [blame] | 13 | ** a VDBE (or an "sqlite3_stmt" as it is known to the outside world.) Prior |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 14 | ** to version 2.8.7, all this code was combined into the vdbe.c source file. |
| 15 | ** But that file was getting too big so this subroutines were split out. |
danielk1977 | 9a9b156 | 2008-04-24 08:31:51 +0000 | [diff] [blame] | 16 | ** |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame^] | 17 | ** $Id: vdbeaux.c,v 1.393 2008/06/25 00:12:42 drh Exp $ |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 18 | */ |
| 19 | #include "sqliteInt.h" |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 20 | #include <ctype.h> |
| 21 | #include "vdbeInt.h" |
| 22 | |
| 23 | |
drh | 46c99e0 | 2007-08-27 23:26:59 +0000 | [diff] [blame] | 24 | |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 25 | /* |
| 26 | ** When debugging the code generator in a symbolic debugger, one can |
mlcreech | 3a00f90 | 2008-03-04 17:45:01 +0000 | [diff] [blame] | 27 | ** set the sqlite3VdbeAddopTrace to 1 and all opcodes will be printed |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 28 | ** as they are added to the instruction stream. |
| 29 | */ |
drh | 8d904f0 | 2005-06-14 17:47:58 +0000 | [diff] [blame] | 30 | #ifdef SQLITE_DEBUG |
mlcreech | 3a00f90 | 2008-03-04 17:45:01 +0000 | [diff] [blame] | 31 | int sqlite3VdbeAddopTrace = 0; |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 32 | #endif |
| 33 | |
| 34 | |
| 35 | /* |
| 36 | ** Create a new virtual database engine. |
| 37 | */ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 38 | Vdbe *sqlite3VdbeCreate(sqlite3 *db){ |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 39 | Vdbe *p; |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 40 | p = sqlite3DbMallocZero(db, sizeof(Vdbe) ); |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 41 | if( p==0 ) return 0; |
| 42 | p->db = db; |
| 43 | if( db->pVdbe ){ |
| 44 | db->pVdbe->pPrev = p; |
| 45 | } |
| 46 | p->pNext = db->pVdbe; |
| 47 | p->pPrev = 0; |
| 48 | db->pVdbe = p; |
| 49 | p->magic = VDBE_MAGIC_INIT; |
| 50 | return p; |
| 51 | } |
| 52 | |
| 53 | /* |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 54 | ** Remember the SQL string for a prepared statement. |
| 55 | */ |
| 56 | void sqlite3VdbeSetSql(Vdbe *p, const char *z, int n){ |
| 57 | if( p==0 ) return; |
| 58 | assert( p->zSql==0 ); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 59 | p->zSql = sqlite3DbStrNDup(p->db, z, n); |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | /* |
| 63 | ** Return the SQL associated with a prepared statement |
| 64 | */ |
danielk1977 | d0e2a85 | 2007-11-14 06:48:48 +0000 | [diff] [blame] | 65 | const char *sqlite3_sql(sqlite3_stmt *pStmt){ |
| 66 | return ((Vdbe *)pStmt)->zSql; |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | /* |
drh | c515525 | 2007-01-08 21:07:17 +0000 | [diff] [blame] | 70 | ** Swap all content between two VDBE structures. |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 71 | */ |
drh | c515525 | 2007-01-08 21:07:17 +0000 | [diff] [blame] | 72 | void sqlite3VdbeSwap(Vdbe *pA, Vdbe *pB){ |
| 73 | Vdbe tmp, *pTmp; |
| 74 | char *zTmp; |
| 75 | int nTmp; |
| 76 | tmp = *pA; |
| 77 | *pA = *pB; |
| 78 | *pB = tmp; |
| 79 | pTmp = pA->pNext; |
| 80 | pA->pNext = pB->pNext; |
| 81 | pB->pNext = pTmp; |
| 82 | pTmp = pA->pPrev; |
| 83 | pA->pPrev = pB->pPrev; |
| 84 | pB->pPrev = pTmp; |
| 85 | zTmp = pA->zSql; |
| 86 | pA->zSql = pB->zSql; |
| 87 | pB->zSql = zTmp; |
| 88 | nTmp = pA->nSql; |
| 89 | pA->nSql = pB->nSql; |
| 90 | pB->nSql = nTmp; |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 91 | } |
| 92 | |
drh | cf1023c | 2007-05-08 20:59:49 +0000 | [diff] [blame] | 93 | #ifdef SQLITE_DEBUG |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 94 | /* |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 95 | ** Turn tracing on or off |
| 96 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 97 | void sqlite3VdbeTrace(Vdbe *p, FILE *trace){ |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 98 | p->trace = trace; |
| 99 | } |
drh | cf1023c | 2007-05-08 20:59:49 +0000 | [diff] [blame] | 100 | #endif |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 101 | |
| 102 | /* |
drh | 76ff3a0 | 2004-09-24 22:32:30 +0000 | [diff] [blame] | 103 | ** Resize the Vdbe.aOp array so that it contains at least N |
drh | a4e5d58 | 2007-10-20 15:41:57 +0000 | [diff] [blame] | 104 | ** elements. |
danielk1977 | ace3eb2 | 2006-01-26 10:35:04 +0000 | [diff] [blame] | 105 | ** |
| 106 | ** If an out-of-memory error occurs while resizing the array, |
| 107 | ** Vdbe.aOp and Vdbe.nOpAlloc remain unchanged (this is so that |
| 108 | ** any opcodes already allocated can be correctly deallocated |
| 109 | ** along with the rest of the Vdbe). |
drh | 76ff3a0 | 2004-09-24 22:32:30 +0000 | [diff] [blame] | 110 | */ |
| 111 | static void resizeOpArray(Vdbe *p, int N){ |
drh | a4e5d58 | 2007-10-20 15:41:57 +0000 | [diff] [blame] | 112 | VdbeOp *pNew; |
drh | a4e5d58 | 2007-10-20 15:41:57 +0000 | [diff] [blame] | 113 | pNew = sqlite3DbRealloc(p->db, p->aOp, N*sizeof(Op)); |
| 114 | if( pNew ){ |
| 115 | p->nOpAlloc = N; |
| 116 | p->aOp = pNew; |
drh | 76ff3a0 | 2004-09-24 22:32:30 +0000 | [diff] [blame] | 117 | } |
| 118 | } |
| 119 | |
| 120 | /* |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 121 | ** Add a new instruction to the list of instructions current in the |
| 122 | ** VDBE. Return the address of the new instruction. |
| 123 | ** |
| 124 | ** Parameters: |
| 125 | ** |
| 126 | ** p Pointer to the VDBE |
| 127 | ** |
| 128 | ** op The opcode for this instruction |
| 129 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 130 | ** p1, p2, p3 Operands |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 131 | ** |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 132 | ** Use the sqlite3VdbeResolveLabel() function to fix an address and |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 133 | ** the sqlite3VdbeChangeP4() function to change the value of the P4 |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 134 | ** operand. |
| 135 | */ |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 136 | int sqlite3VdbeAddOp3(Vdbe *p, int op, int p1, int p2, int p3){ |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 137 | int i; |
drh | 701a0ae | 2004-02-22 20:05:00 +0000 | [diff] [blame] | 138 | VdbeOp *pOp; |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 139 | |
| 140 | i = p->nOp; |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 141 | assert( p->magic==VDBE_MAGIC_INIT ); |
drh | fd2d26b | 2006-03-15 22:44:36 +0000 | [diff] [blame] | 142 | if( p->nOpAlloc<=i ){ |
drh | eee4c8c | 2008-02-18 22:24:57 +0000 | [diff] [blame] | 143 | resizeOpArray(p, p->nOpAlloc ? p->nOpAlloc*2 : 1024/sizeof(Op)); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 144 | if( p->db->mallocFailed ){ |
drh | fd2d26b | 2006-03-15 22:44:36 +0000 | [diff] [blame] | 145 | return 0; |
| 146 | } |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 147 | } |
danielk1977 | 0125683 | 2007-04-18 14:24:32 +0000 | [diff] [blame] | 148 | p->nOp++; |
drh | 701a0ae | 2004-02-22 20:05:00 +0000 | [diff] [blame] | 149 | pOp = &p->aOp[i]; |
| 150 | pOp->opcode = op; |
drh | 26c9b5e | 2008-04-11 14:56:53 +0000 | [diff] [blame] | 151 | pOp->p5 = 0; |
drh | 701a0ae | 2004-02-22 20:05:00 +0000 | [diff] [blame] | 152 | pOp->p1 = p1; |
drh | 701a0ae | 2004-02-22 20:05:00 +0000 | [diff] [blame] | 153 | pOp->p2 = p2; |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 154 | pOp->p3 = p3; |
| 155 | pOp->p4.p = 0; |
| 156 | pOp->p4type = P4_NOTUSED; |
drh | 55ef4d9 | 2005-08-14 01:20:37 +0000 | [diff] [blame] | 157 | p->expired = 0; |
danielk1977 | 8b60e0f | 2005-01-12 09:10:39 +0000 | [diff] [blame] | 158 | #ifdef SQLITE_DEBUG |
drh | 26c9b5e | 2008-04-11 14:56:53 +0000 | [diff] [blame] | 159 | pOp->zComment = 0; |
mlcreech | 3a00f90 | 2008-03-04 17:45:01 +0000 | [diff] [blame] | 160 | if( sqlite3VdbeAddopTrace ) sqlite3VdbePrintOp(0, i, &p->aOp[i]); |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 161 | #endif |
drh | 26c9b5e | 2008-04-11 14:56:53 +0000 | [diff] [blame] | 162 | #ifdef VDBE_PROFILE |
| 163 | pOp->cycles = 0; |
| 164 | pOp->cnt = 0; |
| 165 | #endif |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 166 | return i; |
| 167 | } |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 168 | int sqlite3VdbeAddOp0(Vdbe *p, int op){ |
| 169 | return sqlite3VdbeAddOp3(p, op, 0, 0, 0); |
| 170 | } |
| 171 | int sqlite3VdbeAddOp1(Vdbe *p, int op, int p1){ |
| 172 | return sqlite3VdbeAddOp3(p, op, p1, 0, 0); |
| 173 | } |
| 174 | int sqlite3VdbeAddOp2(Vdbe *p, int op, int p1, int p2){ |
| 175 | return sqlite3VdbeAddOp3(p, op, p1, p2, 0); |
drh | 701a0ae | 2004-02-22 20:05:00 +0000 | [diff] [blame] | 176 | } |
| 177 | |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 178 | |
drh | 701a0ae | 2004-02-22 20:05:00 +0000 | [diff] [blame] | 179 | /* |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 180 | ** Add an opcode that includes the p4 value as a pointer. |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 181 | */ |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 182 | int sqlite3VdbeAddOp4( |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 183 | Vdbe *p, /* Add the opcode to this VM */ |
| 184 | int op, /* The new opcode */ |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 185 | int p1, /* The P1 operand */ |
| 186 | int p2, /* The P2 operand */ |
| 187 | int p3, /* The P3 operand */ |
| 188 | const char *zP4, /* The P4 operand */ |
| 189 | int p4type /* P4 operand type */ |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 190 | ){ |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 191 | int addr = sqlite3VdbeAddOp3(p, op, p1, p2, p3); |
| 192 | sqlite3VdbeChangeP4(p, addr, zP4, p4type); |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 193 | return addr; |
| 194 | } |
| 195 | |
| 196 | /* |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 197 | ** Create a new symbolic label for an instruction that has yet to be |
| 198 | ** coded. The symbolic label is really just a negative number. The |
| 199 | ** label can be used as the P2 value of an operation. Later, when |
| 200 | ** the label is resolved to a specific address, the VDBE will scan |
| 201 | ** through its operation list and change all values of P2 which match |
| 202 | ** the label into the resolved address. |
| 203 | ** |
| 204 | ** The VDBE knows that a P2 value is a label because labels are |
| 205 | ** always negative and P2 values are suppose to be non-negative. |
| 206 | ** Hence, a negative P2 value is a label that has yet to be resolved. |
danielk1977 | b5548a8 | 2004-06-26 13:51:33 +0000 | [diff] [blame] | 207 | ** |
| 208 | ** Zero is returned if a malloc() fails. |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 209 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 210 | int sqlite3VdbeMakeLabel(Vdbe *p){ |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 211 | int i; |
| 212 | i = p->nLabel++; |
| 213 | assert( p->magic==VDBE_MAGIC_INIT ); |
| 214 | if( i>=p->nLabelAlloc ){ |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 215 | p->nLabelAlloc = p->nLabelAlloc*2 + 10; |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 216 | p->aLabel = sqlite3DbReallocOrFree(p->db, p->aLabel, |
drh | cf64372 | 2007-03-27 13:36:37 +0000 | [diff] [blame] | 217 | p->nLabelAlloc*sizeof(p->aLabel[0])); |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 218 | } |
drh | 76ff3a0 | 2004-09-24 22:32:30 +0000 | [diff] [blame] | 219 | if( p->aLabel ){ |
| 220 | p->aLabel[i] = -1; |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 221 | } |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 222 | return -1-i; |
| 223 | } |
| 224 | |
| 225 | /* |
| 226 | ** Resolve label "x" to be the address of the next instruction to |
| 227 | ** be inserted. The parameter "x" must have been obtained from |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 228 | ** a prior call to sqlite3VdbeMakeLabel(). |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 229 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 230 | void sqlite3VdbeResolveLabel(Vdbe *p, int x){ |
drh | 76ff3a0 | 2004-09-24 22:32:30 +0000 | [diff] [blame] | 231 | int j = -1-x; |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 232 | assert( p->magic==VDBE_MAGIC_INIT ); |
drh | 76ff3a0 | 2004-09-24 22:32:30 +0000 | [diff] [blame] | 233 | assert( j>=0 && j<p->nLabel ); |
| 234 | if( p->aLabel ){ |
| 235 | p->aLabel[j] = p->nOp; |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 236 | } |
| 237 | } |
| 238 | |
| 239 | /* |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 240 | ** Loop through the program looking for P2 values that are negative |
| 241 | ** on jump instructions. Each such value is a label. Resolve the |
| 242 | ** label by setting the P2 value to its correct non-zero value. |
drh | 76ff3a0 | 2004-09-24 22:32:30 +0000 | [diff] [blame] | 243 | ** |
| 244 | ** This routine is called once after all opcodes have been inserted. |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 245 | ** |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 246 | ** Variable *pMaxFuncArgs is set to the maximum value of any P2 argument |
danielk1977 | 399918f | 2006-06-14 13:03:23 +0000 | [diff] [blame] | 247 | ** to an OP_Function, OP_AggStep or OP_VFilter opcode. This is used by |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 248 | ** sqlite3VdbeMakeReady() to size the Vdbe.apArg[] array. |
danielk1977 | bc04f85 | 2005-03-29 08:26:13 +0000 | [diff] [blame] | 249 | ** |
drh | 3844990 | 2005-06-07 01:43:41 +0000 | [diff] [blame] | 250 | ** This routine also does the following optimization: It scans for |
drh | 77658e2 | 2007-12-04 16:54:52 +0000 | [diff] [blame] | 251 | ** instructions that might cause a statement rollback. Such instructions |
| 252 | ** are: |
| 253 | ** |
| 254 | ** * OP_Halt with P1=SQLITE_CONSTRAINT and P2=OE_Abort. |
| 255 | ** * OP_Destroy |
| 256 | ** * OP_VUpdate |
| 257 | ** * OP_VRename |
| 258 | ** |
| 259 | ** If no such instruction is found, then every Statement instruction |
| 260 | ** is changed to a Noop. In this way, we avoid creating the statement |
| 261 | ** journal file unnecessarily. |
drh | 76ff3a0 | 2004-09-24 22:32:30 +0000 | [diff] [blame] | 262 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 263 | static void resolveP2Values(Vdbe *p, int *pMaxFuncArgs){ |
drh | 76ff3a0 | 2004-09-24 22:32:30 +0000 | [diff] [blame] | 264 | int i; |
danielk1977 | bc04f85 | 2005-03-29 08:26:13 +0000 | [diff] [blame] | 265 | int nMaxArgs = 0; |
drh | 76ff3a0 | 2004-09-24 22:32:30 +0000 | [diff] [blame] | 266 | Op *pOp; |
| 267 | int *aLabel = p->aLabel; |
drh | 3844990 | 2005-06-07 01:43:41 +0000 | [diff] [blame] | 268 | int doesStatementRollback = 0; |
| 269 | int hasStatementBegin = 0; |
drh | 76ff3a0 | 2004-09-24 22:32:30 +0000 | [diff] [blame] | 270 | for(pOp=p->aOp, i=p->nOp-1; i>=0; i--, pOp++){ |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 271 | u8 opcode = pOp->opcode; |
| 272 | |
drh | a2baf3a | 2008-06-18 15:34:09 +0000 | [diff] [blame] | 273 | if( opcode==OP_Function || opcode==OP_AggStep ){ |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 274 | if( pOp->p5>nMaxArgs ) nMaxArgs = pOp->p5; |
danielk1977 | 399918f | 2006-06-14 13:03:23 +0000 | [diff] [blame] | 275 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | a2baf3a | 2008-06-18 15:34:09 +0000 | [diff] [blame] | 276 | }else if( opcode==OP_VUpdate ){ |
danielk1977 | bc04f85 | 2005-03-29 08:26:13 +0000 | [diff] [blame] | 277 | if( pOp->p2>nMaxArgs ) nMaxArgs = pOp->p2; |
drh | a2baf3a | 2008-06-18 15:34:09 +0000 | [diff] [blame] | 278 | #endif |
danielk1977 | 5dfecba | 2008-06-23 13:57:21 +0000 | [diff] [blame] | 279 | } |
danielk1977 | 182c4ba | 2007-06-27 15:53:34 +0000 | [diff] [blame] | 280 | if( opcode==OP_Halt ){ |
drh | 3844990 | 2005-06-07 01:43:41 +0000 | [diff] [blame] | 281 | if( pOp->p1==SQLITE_CONSTRAINT && pOp->p2==OE_Abort ){ |
| 282 | doesStatementRollback = 1; |
| 283 | } |
drh | 3844990 | 2005-06-07 01:43:41 +0000 | [diff] [blame] | 284 | }else if( opcode==OP_Statement ){ |
| 285 | hasStatementBegin = 1; |
drh | 77658e2 | 2007-12-04 16:54:52 +0000 | [diff] [blame] | 286 | }else if( opcode==OP_Destroy ){ |
| 287 | doesStatementRollback = 1; |
danielk1977 | 182c4ba | 2007-06-27 15:53:34 +0000 | [diff] [blame] | 288 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 289 | }else if( opcode==OP_VUpdate || opcode==OP_VRename ){ |
| 290 | doesStatementRollback = 1; |
drh | 4be8b51 | 2006-06-13 23:51:34 +0000 | [diff] [blame] | 291 | }else if( opcode==OP_VFilter ){ |
| 292 | int n; |
| 293 | assert( p->nOp - i >= 3 ); |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 294 | assert( pOp[-1].opcode==OP_Integer ); |
danielk1977 | 6dbee81 | 2008-01-03 18:39:41 +0000 | [diff] [blame] | 295 | n = pOp[-1].p1; |
drh | 4be8b51 | 2006-06-13 23:51:34 +0000 | [diff] [blame] | 296 | if( n>nMaxArgs ) nMaxArgs = n; |
danielk1977 | 182c4ba | 2007-06-27 15:53:34 +0000 | [diff] [blame] | 297 | #endif |
danielk1977 | bc04f85 | 2005-03-29 08:26:13 +0000 | [diff] [blame] | 298 | } |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 299 | |
drh | d298151 | 2008-01-04 19:33:49 +0000 | [diff] [blame] | 300 | if( sqlite3VdbeOpcodeHasProperty(opcode, OPFLG_JUMP) && pOp->p2<0 ){ |
| 301 | assert( -1-pOp->p2<p->nLabel ); |
| 302 | pOp->p2 = aLabel[-1-pOp->p2]; |
| 303 | } |
drh | 76ff3a0 | 2004-09-24 22:32:30 +0000 | [diff] [blame] | 304 | } |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 305 | sqlite3_free(p->aLabel); |
drh | 76ff3a0 | 2004-09-24 22:32:30 +0000 | [diff] [blame] | 306 | p->aLabel = 0; |
danielk1977 | bc04f85 | 2005-03-29 08:26:13 +0000 | [diff] [blame] | 307 | |
| 308 | *pMaxFuncArgs = nMaxArgs; |
drh | 3844990 | 2005-06-07 01:43:41 +0000 | [diff] [blame] | 309 | |
| 310 | /* If we never rollback a statement transaction, then statement |
| 311 | ** transactions are not needed. So change every OP_Statement |
drh | 66560ad | 2006-01-06 14:32:19 +0000 | [diff] [blame] | 312 | ** opcode into an OP_Noop. This avoid a call to sqlite3OsOpenExclusive() |
drh | 3844990 | 2005-06-07 01:43:41 +0000 | [diff] [blame] | 313 | ** which can be expensive on some platforms. |
| 314 | */ |
| 315 | if( hasStatementBegin && !doesStatementRollback ){ |
| 316 | for(pOp=p->aOp, i=p->nOp-1; i>=0; i--, pOp++){ |
| 317 | if( pOp->opcode==OP_Statement ){ |
| 318 | pOp->opcode = OP_Noop; |
| 319 | } |
| 320 | } |
| 321 | } |
drh | 76ff3a0 | 2004-09-24 22:32:30 +0000 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | /* |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 325 | ** Return the address of the next instruction to be inserted. |
| 326 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 327 | int sqlite3VdbeCurrentAddr(Vdbe *p){ |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 328 | assert( p->magic==VDBE_MAGIC_INIT ); |
| 329 | return p->nOp; |
| 330 | } |
| 331 | |
| 332 | /* |
| 333 | ** Add a whole list of operations to the operation stack. Return the |
| 334 | ** address of the first operation added. |
| 335 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 336 | int sqlite3VdbeAddOpList(Vdbe *p, int nOp, VdbeOpList const *aOp){ |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 337 | int addr; |
| 338 | assert( p->magic==VDBE_MAGIC_INIT ); |
drh | a4e5d58 | 2007-10-20 15:41:57 +0000 | [diff] [blame] | 339 | if( p->nOp + nOp > p->nOpAlloc ){ |
drh | eee4c8c | 2008-02-18 22:24:57 +0000 | [diff] [blame] | 340 | resizeOpArray(p, p->nOpAlloc ? p->nOpAlloc*2 : 1024/sizeof(Op)); |
| 341 | assert( p->nOp+nOp<=p->nOpAlloc || p->db->mallocFailed ); |
drh | a4e5d58 | 2007-10-20 15:41:57 +0000 | [diff] [blame] | 342 | } |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 343 | if( p->db->mallocFailed ){ |
drh | 76ff3a0 | 2004-09-24 22:32:30 +0000 | [diff] [blame] | 344 | return 0; |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 345 | } |
| 346 | addr = p->nOp; |
| 347 | if( nOp>0 ){ |
| 348 | int i; |
drh | 905793e | 2004-02-21 13:31:09 +0000 | [diff] [blame] | 349 | VdbeOpList const *pIn = aOp; |
| 350 | for(i=0; i<nOp; i++, pIn++){ |
| 351 | int p2 = pIn->p2; |
| 352 | VdbeOp *pOut = &p->aOp[i+addr]; |
| 353 | pOut->opcode = pIn->opcode; |
| 354 | pOut->p1 = pIn->p1; |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 355 | if( p2<0 && sqlite3VdbeOpcodeHasProperty(pOut->opcode, OPFLG_JUMP) ){ |
| 356 | pOut->p2 = addr + ADDR(p2); |
| 357 | }else{ |
| 358 | pOut->p2 = p2; |
| 359 | } |
drh | 2400345 | 2008-01-03 01:28:59 +0000 | [diff] [blame] | 360 | pOut->p3 = pIn->p3; |
| 361 | pOut->p4type = P4_NOTUSED; |
| 362 | pOut->p4.p = 0; |
| 363 | pOut->p5 = 0; |
danielk1977 | 8b60e0f | 2005-01-12 09:10:39 +0000 | [diff] [blame] | 364 | #ifdef SQLITE_DEBUG |
drh | 26c9b5e | 2008-04-11 14:56:53 +0000 | [diff] [blame] | 365 | pOut->zComment = 0; |
mlcreech | 3a00f90 | 2008-03-04 17:45:01 +0000 | [diff] [blame] | 366 | if( sqlite3VdbeAddopTrace ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 367 | sqlite3VdbePrintOp(0, i+addr, &p->aOp[i+addr]); |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 368 | } |
| 369 | #endif |
| 370 | } |
| 371 | p->nOp += nOp; |
| 372 | } |
| 373 | return addr; |
| 374 | } |
| 375 | |
| 376 | /* |
| 377 | ** Change the value of the P1 operand for a specific instruction. |
| 378 | ** This routine is useful when a large program is loaded from a |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 379 | ** static array using sqlite3VdbeAddOpList but we want to make a |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 380 | ** few minor changes to the program. |
| 381 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 382 | void sqlite3VdbeChangeP1(Vdbe *p, int addr, int val){ |
drh | 8aa34ae | 2006-03-13 12:54:09 +0000 | [diff] [blame] | 383 | assert( p==0 || p->magic==VDBE_MAGIC_INIT ); |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 384 | if( p && addr>=0 && p->nOp>addr && p->aOp ){ |
| 385 | p->aOp[addr].p1 = val; |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | /* |
| 390 | ** Change the value of the P2 operand for a specific instruction. |
| 391 | ** This routine is useful for setting a jump destination. |
| 392 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 393 | void sqlite3VdbeChangeP2(Vdbe *p, int addr, int val){ |
drh | 8aa34ae | 2006-03-13 12:54:09 +0000 | [diff] [blame] | 394 | assert( p==0 || p->magic==VDBE_MAGIC_INIT ); |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 395 | if( p && addr>=0 && p->nOp>addr && p->aOp ){ |
| 396 | p->aOp[addr].p2 = val; |
| 397 | } |
| 398 | } |
| 399 | |
drh | d654be8 | 2005-09-20 17:42:23 +0000 | [diff] [blame] | 400 | /* |
danielk1977 | 1f4aa33 | 2008-01-03 09:51:55 +0000 | [diff] [blame] | 401 | ** Change the value of the P3 operand for a specific instruction. |
danielk1977 | 207872a | 2008-01-03 07:54:23 +0000 | [diff] [blame] | 402 | */ |
| 403 | void sqlite3VdbeChangeP3(Vdbe *p, int addr, int val){ |
| 404 | assert( p==0 || p->magic==VDBE_MAGIC_INIT ); |
| 405 | if( p && addr>=0 && p->nOp>addr && p->aOp ){ |
| 406 | p->aOp[addr].p3 = val; |
| 407 | } |
| 408 | } |
| 409 | |
| 410 | /* |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 411 | ** Change the value of the P5 operand for the most recently |
| 412 | ** added operation. |
danielk1977 | 1f4aa33 | 2008-01-03 09:51:55 +0000 | [diff] [blame] | 413 | */ |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 414 | void sqlite3VdbeChangeP5(Vdbe *p, u8 val){ |
danielk1977 | 1f4aa33 | 2008-01-03 09:51:55 +0000 | [diff] [blame] | 415 | assert( p==0 || p->magic==VDBE_MAGIC_INIT ); |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 416 | if( p && p->aOp ){ |
| 417 | assert( p->nOp>0 ); |
| 418 | p->aOp[p->nOp-1].p5 = val; |
danielk1977 | 1f4aa33 | 2008-01-03 09:51:55 +0000 | [diff] [blame] | 419 | } |
| 420 | } |
| 421 | |
| 422 | /* |
drh | f887540 | 2006-03-17 13:56:34 +0000 | [diff] [blame] | 423 | ** Change the P2 operand of instruction addr so that it points to |
drh | d654be8 | 2005-09-20 17:42:23 +0000 | [diff] [blame] | 424 | ** the address of the next instruction to be coded. |
| 425 | */ |
| 426 | void sqlite3VdbeJumpHere(Vdbe *p, int addr){ |
| 427 | sqlite3VdbeChangeP2(p, addr, p->nOp); |
| 428 | } |
drh | b38ad99 | 2005-09-16 00:27:01 +0000 | [diff] [blame] | 429 | |
drh | b7f6f68 | 2006-07-08 17:06:43 +0000 | [diff] [blame] | 430 | |
| 431 | /* |
| 432 | ** If the input FuncDef structure is ephemeral, then free it. If |
| 433 | ** the FuncDef is not ephermal, then do nothing. |
| 434 | */ |
| 435 | static void freeEphemeralFunction(FuncDef *pDef){ |
| 436 | if( pDef && (pDef->flags & SQLITE_FUNC_EPHEM)!=0 ){ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 437 | sqlite3_free(pDef); |
drh | b7f6f68 | 2006-07-08 17:06:43 +0000 | [diff] [blame] | 438 | } |
| 439 | } |
| 440 | |
drh | b38ad99 | 2005-09-16 00:27:01 +0000 | [diff] [blame] | 441 | /* |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 442 | ** Delete a P4 value if necessary. |
drh | b38ad99 | 2005-09-16 00:27:01 +0000 | [diff] [blame] | 443 | */ |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame^] | 444 | static void freeP4(int p4type, void *p4){ |
| 445 | if( p4 ){ |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 446 | switch( p4type ){ |
| 447 | case P4_REAL: |
| 448 | case P4_INT64: |
| 449 | case P4_MPRINTF: |
| 450 | case P4_DYNAMIC: |
| 451 | case P4_KEYINFO: |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame^] | 452 | case P4_INTARRAY: |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 453 | case P4_KEYINFO_HANDOFF: { |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame^] | 454 | sqlite3_free(p4); |
drh | ac1733d | 2005-09-17 17:58:22 +0000 | [diff] [blame] | 455 | break; |
| 456 | } |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 457 | case P4_VDBEFUNC: { |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame^] | 458 | VdbeFunc *pVdbeFunc = (VdbeFunc *)p4; |
drh | b7f6f68 | 2006-07-08 17:06:43 +0000 | [diff] [blame] | 459 | freeEphemeralFunction(pVdbeFunc->pFunc); |
drh | ac1733d | 2005-09-17 17:58:22 +0000 | [diff] [blame] | 460 | sqlite3VdbeDeleteAuxData(pVdbeFunc, 0); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 461 | sqlite3_free(pVdbeFunc); |
drh | ac1733d | 2005-09-17 17:58:22 +0000 | [diff] [blame] | 462 | break; |
| 463 | } |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 464 | case P4_FUNCDEF: { |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame^] | 465 | freeEphemeralFunction((FuncDef*)p4); |
drh | b7f6f68 | 2006-07-08 17:06:43 +0000 | [diff] [blame] | 466 | break; |
| 467 | } |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 468 | case P4_MEM: { |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame^] | 469 | sqlite3ValueFree((sqlite3_value*)p4); |
drh | ac1733d | 2005-09-17 17:58:22 +0000 | [diff] [blame] | 470 | break; |
| 471 | } |
drh | b38ad99 | 2005-09-16 00:27:01 +0000 | [diff] [blame] | 472 | } |
| 473 | } |
| 474 | } |
| 475 | |
| 476 | |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 477 | /* |
drh | f887540 | 2006-03-17 13:56:34 +0000 | [diff] [blame] | 478 | ** Change N opcodes starting at addr to No-ops. |
| 479 | */ |
| 480 | void sqlite3VdbeChangeToNoop(Vdbe *p, int addr, int N){ |
danielk1977 | 92d4d7a | 2007-05-04 12:05:56 +0000 | [diff] [blame] | 481 | if( p && p->aOp ){ |
| 482 | VdbeOp *pOp = &p->aOp[addr]; |
| 483 | while( N-- ){ |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 484 | freeP4(pOp->p4type, pOp->p4.p); |
danielk1977 | 92d4d7a | 2007-05-04 12:05:56 +0000 | [diff] [blame] | 485 | memset(pOp, 0, sizeof(pOp[0])); |
| 486 | pOp->opcode = OP_Noop; |
| 487 | pOp++; |
| 488 | } |
drh | f887540 | 2006-03-17 13:56:34 +0000 | [diff] [blame] | 489 | } |
| 490 | } |
| 491 | |
| 492 | /* |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 493 | ** Change the value of the P4 operand for a specific instruction. |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 494 | ** This routine is useful when a large program is loaded from a |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 495 | ** static array using sqlite3VdbeAddOpList but we want to make a |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 496 | ** few minor changes to the program. |
| 497 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 498 | ** If n>=0 then the P4 operand is dynamic, meaning that a copy of |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 499 | ** the string is made into memory obtained from sqlite3_malloc(). |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 500 | ** A value of n==0 means copy bytes of zP4 up to and including the |
| 501 | ** first null byte. If n>0 then copy n+1 bytes of zP4. |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 502 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 503 | ** If n==P4_KEYINFO it means that zP4 is a pointer to a KeyInfo structure. |
danielk1977 | 1f55c05 | 2005-05-19 08:42:59 +0000 | [diff] [blame] | 504 | ** A copy is made of the KeyInfo structure into memory obtained from |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 505 | ** sqlite3_malloc, to be freed when the Vdbe is finalized. |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 506 | ** n==P4_KEYINFO_HANDOFF indicates that zP4 points to a KeyInfo structure |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 507 | ** stored in memory that the caller has obtained from sqlite3_malloc. The |
danielk1977 | 1f55c05 | 2005-05-19 08:42:59 +0000 | [diff] [blame] | 508 | ** caller should not free the allocation, it will be freed when the Vdbe is |
| 509 | ** finalized. |
| 510 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 511 | ** Other values of n (P4_STATIC, P4_COLLSEQ etc.) indicate that zP4 points |
danielk1977 | 1f55c05 | 2005-05-19 08:42:59 +0000 | [diff] [blame] | 512 | ** to a string or structure that is guaranteed to exist for the lifetime of |
| 513 | ** the Vdbe. In these cases we can just copy the pointer. |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 514 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 515 | ** If addr<0 then change P4 on the most recently inserted instruction. |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 516 | */ |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 517 | void sqlite3VdbeChangeP4(Vdbe *p, int addr, const char *zP4, int n){ |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 518 | Op *pOp; |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 519 | assert( p!=0 ); |
| 520 | assert( p->magic==VDBE_MAGIC_INIT ); |
| 521 | if( p->aOp==0 || p->db->mallocFailed ){ |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 522 | if (n != P4_KEYINFO) { |
| 523 | freeP4(n, (void*)*(char**)&zP4); |
danielk1977 | 261919c | 2005-12-06 12:52:59 +0000 | [diff] [blame] | 524 | } |
danielk1977 | d5d5652 | 2005-03-16 12:15:20 +0000 | [diff] [blame] | 525 | return; |
| 526 | } |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 527 | assert( addr<p->nOp ); |
| 528 | if( addr<0 ){ |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 529 | addr = p->nOp - 1; |
| 530 | if( addr<0 ) return; |
| 531 | } |
| 532 | pOp = &p->aOp[addr]; |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 533 | freeP4(pOp->p4type, pOp->p4.p); |
| 534 | pOp->p4.p = 0; |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 535 | if( n==P4_INT32 ){ |
mlcreech | 12d4082 | 2008-03-06 07:35:21 +0000 | [diff] [blame] | 536 | /* Note: this cast is safe, because the origin data point was an int |
| 537 | ** that was cast to a (const char *). */ |
drh | 7209c69 | 2008-04-27 18:40:11 +0000 | [diff] [blame] | 538 | pOp->p4.i = (int)zP4; |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 539 | pOp->p4type = n; |
| 540 | }else if( zP4==0 ){ |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 541 | pOp->p4.p = 0; |
| 542 | pOp->p4type = P4_NOTUSED; |
| 543 | }else if( n==P4_KEYINFO ){ |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 544 | KeyInfo *pKeyInfo; |
| 545 | int nField, nByte; |
drh | 4db38a7 | 2005-09-01 12:16:28 +0000 | [diff] [blame] | 546 | |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 547 | nField = ((KeyInfo*)zP4)->nField; |
drh | fdd6e85 | 2005-12-16 01:06:16 +0000 | [diff] [blame] | 548 | nByte = sizeof(*pKeyInfo) + (nField-1)*sizeof(pKeyInfo->aColl[0]) + nField; |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 549 | pKeyInfo = sqlite3Malloc( nByte ); |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 550 | pOp->p4.pKeyInfo = pKeyInfo; |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 551 | if( pKeyInfo ){ |
drh | b21e7c7 | 2008-06-22 12:37:57 +0000 | [diff] [blame] | 552 | u8 *aSortOrder; |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 553 | memcpy(pKeyInfo, zP4, nByte); |
drh | fdd6e85 | 2005-12-16 01:06:16 +0000 | [diff] [blame] | 554 | aSortOrder = pKeyInfo->aSortOrder; |
| 555 | if( aSortOrder ){ |
danielk1977 | bab45c6 | 2006-01-16 15:14:27 +0000 | [diff] [blame] | 556 | pKeyInfo->aSortOrder = (unsigned char*)&pKeyInfo->aColl[nField]; |
drh | fdd6e85 | 2005-12-16 01:06:16 +0000 | [diff] [blame] | 557 | memcpy(pKeyInfo->aSortOrder, aSortOrder, nField); |
| 558 | } |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 559 | pOp->p4type = P4_KEYINFO; |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 560 | }else{ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 561 | p->db->mallocFailed = 1; |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 562 | pOp->p4type = P4_NOTUSED; |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 563 | } |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 564 | }else if( n==P4_KEYINFO_HANDOFF ){ |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 565 | pOp->p4.p = (void*)zP4; |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 566 | pOp->p4type = P4_KEYINFO; |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 567 | }else if( n<0 ){ |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 568 | pOp->p4.p = (void*)zP4; |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 569 | pOp->p4type = n; |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 570 | }else{ |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 571 | if( n==0 ) n = strlen(zP4); |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 572 | pOp->p4.z = sqlite3DbStrNDup(p->db, zP4, n); |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 573 | pOp->p4type = P4_DYNAMIC; |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 574 | } |
| 575 | } |
| 576 | |
drh | ad6d946 | 2004-09-19 02:15:24 +0000 | [diff] [blame] | 577 | #ifndef NDEBUG |
| 578 | /* |
drh | 16ee60f | 2008-06-20 18:13:25 +0000 | [diff] [blame] | 579 | ** Change the comment on the the most recently coded instruction. Or |
| 580 | ** insert a No-op and add the comment to that new instruction. This |
| 581 | ** makes the code easier to read during debugging. None of this happens |
| 582 | ** in a production build. |
drh | ad6d946 | 2004-09-19 02:15:24 +0000 | [diff] [blame] | 583 | */ |
| 584 | void sqlite3VdbeComment(Vdbe *p, const char *zFormat, ...){ |
| 585 | va_list ap; |
danielk1977 | 0125683 | 2007-04-18 14:24:32 +0000 | [diff] [blame] | 586 | assert( p->nOp>0 || p->aOp==0 ); |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 587 | assert( p->aOp==0 || p->aOp[p->nOp-1].zComment==0 || p->db->mallocFailed ); |
danielk1977 | dba0137 | 2008-01-05 18:44:29 +0000 | [diff] [blame] | 588 | if( p->nOp ){ |
drh | 8cc7432 | 2008-01-15 02:22:24 +0000 | [diff] [blame] | 589 | char **pz = &p->aOp[p->nOp-1].zComment; |
danielk1977 | dba0137 | 2008-01-05 18:44:29 +0000 | [diff] [blame] | 590 | va_start(ap, zFormat); |
drh | 8cc7432 | 2008-01-15 02:22:24 +0000 | [diff] [blame] | 591 | sqlite3_free(*pz); |
| 592 | *pz = sqlite3VMPrintf(p->db, zFormat, ap); |
danielk1977 | dba0137 | 2008-01-05 18:44:29 +0000 | [diff] [blame] | 593 | va_end(ap); |
| 594 | } |
drh | ad6d946 | 2004-09-19 02:15:24 +0000 | [diff] [blame] | 595 | } |
drh | 16ee60f | 2008-06-20 18:13:25 +0000 | [diff] [blame] | 596 | void sqlite3VdbeNoopComment(Vdbe *p, const char *zFormat, ...){ |
| 597 | va_list ap; |
| 598 | sqlite3VdbeAddOp0(p, OP_Noop); |
| 599 | assert( p->nOp>0 || p->aOp==0 ); |
| 600 | assert( p->aOp==0 || p->aOp[p->nOp-1].zComment==0 || p->db->mallocFailed ); |
| 601 | if( p->nOp ){ |
| 602 | char **pz = &p->aOp[p->nOp-1].zComment; |
| 603 | va_start(ap, zFormat); |
| 604 | sqlite3_free(*pz); |
| 605 | *pz = sqlite3VMPrintf(p->db, zFormat, ap); |
| 606 | va_end(ap); |
| 607 | } |
| 608 | } |
| 609 | #endif /* NDEBUG */ |
drh | ad6d946 | 2004-09-19 02:15:24 +0000 | [diff] [blame] | 610 | |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 611 | /* |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 612 | ** Return the opcode for a given address. |
| 613 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 614 | VdbeOp *sqlite3VdbeGetOp(Vdbe *p, int addr){ |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 615 | assert( p->magic==VDBE_MAGIC_INIT ); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 616 | assert( (addr>=0 && addr<p->nOp) || p->db->mallocFailed ); |
danielk1977 | 0125683 | 2007-04-18 14:24:32 +0000 | [diff] [blame] | 617 | return ((addr>=0 && addr<p->nOp)?(&p->aOp[addr]):0); |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 618 | } |
| 619 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 620 | #if !defined(SQLITE_OMIT_EXPLAIN) || !defined(NDEBUG) \ |
| 621 | || defined(VDBE_PROFILE) || defined(SQLITE_DEBUG) |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 622 | /* |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 623 | ** Compute a string that describes the P4 parameter for an opcode. |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 624 | ** Use zTemp for any required temporary buffer space. |
| 625 | */ |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 626 | static char *displayP4(Op *pOp, char *zTemp, int nTemp){ |
| 627 | char *zP4 = zTemp; |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 628 | assert( nTemp>=20 ); |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 629 | switch( pOp->p4type ){ |
drh | 16ee60f | 2008-06-20 18:13:25 +0000 | [diff] [blame] | 630 | case P4_KEYINFO_STATIC: |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 631 | case P4_KEYINFO: { |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 632 | int i, j; |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 633 | KeyInfo *pKeyInfo = pOp->p4.pKeyInfo; |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 634 | sqlite3_snprintf(nTemp, zTemp, "keyinfo(%d", pKeyInfo->nField); |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 635 | i = strlen(zTemp); |
| 636 | for(j=0; j<pKeyInfo->nField; j++){ |
| 637 | CollSeq *pColl = pKeyInfo->aColl[j]; |
| 638 | if( pColl ){ |
| 639 | int n = strlen(pColl->zName); |
| 640 | if( i+n>nTemp-6 ){ |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 641 | memcpy(&zTemp[i],",...",4); |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 642 | break; |
| 643 | } |
| 644 | zTemp[i++] = ','; |
drh | ffbc308 | 2004-05-21 01:29:06 +0000 | [diff] [blame] | 645 | if( pKeyInfo->aSortOrder && pKeyInfo->aSortOrder[j] ){ |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 646 | zTemp[i++] = '-'; |
| 647 | } |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 648 | memcpy(&zTemp[i], pColl->zName,n+1); |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 649 | i += n; |
| 650 | }else if( i+4<nTemp-6 ){ |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 651 | memcpy(&zTemp[i],",nil",4); |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 652 | i += 4; |
| 653 | } |
| 654 | } |
| 655 | zTemp[i++] = ')'; |
| 656 | zTemp[i] = 0; |
| 657 | assert( i<nTemp ); |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 658 | break; |
| 659 | } |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 660 | case P4_COLLSEQ: { |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 661 | CollSeq *pColl = pOp->p4.pColl; |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 662 | sqlite3_snprintf(nTemp, zTemp, "collseq(%.20s)", pColl->zName); |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 663 | break; |
| 664 | } |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 665 | case P4_FUNCDEF: { |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 666 | FuncDef *pDef = pOp->p4.pFunc; |
drh | a967e88 | 2006-06-13 01:04:52 +0000 | [diff] [blame] | 667 | sqlite3_snprintf(nTemp, zTemp, "%s(%d)", pDef->zName, pDef->nArg); |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 668 | break; |
| 669 | } |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 670 | case P4_INT64: { |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 671 | sqlite3_snprintf(nTemp, zTemp, "%lld", *pOp->p4.pI64); |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 672 | break; |
| 673 | } |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 674 | case P4_INT32: { |
| 675 | sqlite3_snprintf(nTemp, zTemp, "%d", pOp->p4.i); |
drh | 598f134 | 2007-10-23 15:39:45 +0000 | [diff] [blame] | 676 | break; |
| 677 | } |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 678 | case P4_REAL: { |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 679 | sqlite3_snprintf(nTemp, zTemp, "%.16g", *pOp->p4.pReal); |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 680 | break; |
| 681 | } |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 682 | case P4_MEM: { |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 683 | Mem *pMem = pOp->p4.pMem; |
drh | c4dd3fd | 2008-01-22 01:48:05 +0000 | [diff] [blame] | 684 | assert( (pMem->flags & MEM_Null)==0 ); |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 685 | if( pMem->flags & MEM_Str ){ |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 686 | zP4 = pMem->z; |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 687 | }else if( pMem->flags & MEM_Int ){ |
| 688 | sqlite3_snprintf(nTemp, zTemp, "%lld", pMem->u.i); |
| 689 | }else if( pMem->flags & MEM_Real ){ |
| 690 | sqlite3_snprintf(nTemp, zTemp, "%.16g", pMem->r); |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 691 | } |
drh | 598f134 | 2007-10-23 15:39:45 +0000 | [diff] [blame] | 692 | break; |
| 693 | } |
drh | a967e88 | 2006-06-13 01:04:52 +0000 | [diff] [blame] | 694 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 695 | case P4_VTAB: { |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 696 | sqlite3_vtab *pVtab = pOp->p4.pVtab; |
drh | 1914619 | 2006-06-26 19:10:32 +0000 | [diff] [blame] | 697 | sqlite3_snprintf(nTemp, zTemp, "vtab:%p:%p", pVtab, pVtab->pModule); |
drh | a967e88 | 2006-06-13 01:04:52 +0000 | [diff] [blame] | 698 | break; |
| 699 | } |
| 700 | #endif |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame^] | 701 | case P4_INTARRAY: { |
| 702 | sqlite3_snprintf(nTemp, zTemp, "intarray"); |
| 703 | break; |
| 704 | } |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 705 | default: { |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 706 | zP4 = pOp->p4.z; |
drh | 949f9cd | 2008-01-12 21:35:57 +0000 | [diff] [blame] | 707 | if( zP4==0 ){ |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 708 | zP4 = zTemp; |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 709 | zTemp[0] = 0; |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 710 | } |
| 711 | } |
| 712 | } |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 713 | assert( zP4!=0 ); |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 714 | return zP4; |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 715 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 716 | #endif |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 717 | |
drh | 900b31e | 2007-08-28 02:27:51 +0000 | [diff] [blame] | 718 | /* |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 719 | ** Declare to the Vdbe that the BTree object at db->aDb[i] is used. |
| 720 | ** |
drh | 900b31e | 2007-08-28 02:27:51 +0000 | [diff] [blame] | 721 | */ |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 722 | void sqlite3VdbeUsesBtree(Vdbe *p, int i){ |
| 723 | int mask; |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 724 | assert( i>=0 && i<p->db->nDb ); |
| 725 | assert( i<sizeof(p->btreeMask)*8 ); |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 726 | mask = 1<<i; |
| 727 | if( (p->btreeMask & mask)==0 ){ |
| 728 | p->btreeMask |= mask; |
| 729 | sqlite3BtreeMutexArrayInsert(&p->aMutex, p->db->aDb[i].pBt); |
| 730 | } |
drh | 900b31e | 2007-08-28 02:27:51 +0000 | [diff] [blame] | 731 | } |
| 732 | |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 733 | |
danielk1977 | 8b60e0f | 2005-01-12 09:10:39 +0000 | [diff] [blame] | 734 | #if defined(VDBE_PROFILE) || defined(SQLITE_DEBUG) |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 735 | /* |
| 736 | ** Print a single opcode. This routine is used for debugging only. |
| 737 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 738 | void sqlite3VdbePrintOp(FILE *pOut, int pc, Op *pOp){ |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 739 | char *zP4; |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 740 | char zPtr[50]; |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 741 | static const char *zFormat1 = "%4d %-13s %4d %4d %4d %-4s %.2X %s\n"; |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 742 | if( pOut==0 ) pOut = stdout; |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 743 | zP4 = displayP4(pOp, zPtr, sizeof(zPtr)); |
danielk1977 | 11641c1 | 2008-01-03 08:18:30 +0000 | [diff] [blame] | 744 | fprintf(pOut, zFormat1, pc, |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 745 | sqlite3OpcodeName(pOp->opcode), pOp->p1, pOp->p2, pOp->p3, zP4, pOp->p5, |
| 746 | #ifdef SQLITE_DEBUG |
| 747 | pOp->zComment ? pOp->zComment : "" |
| 748 | #else |
| 749 | "" |
| 750 | #endif |
| 751 | ); |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 752 | fflush(pOut); |
| 753 | } |
| 754 | #endif |
| 755 | |
| 756 | /* |
drh | 76ff3a0 | 2004-09-24 22:32:30 +0000 | [diff] [blame] | 757 | ** Release an array of N Mem elements |
| 758 | */ |
danielk1977 | dfb316d | 2008-03-26 18:34:43 +0000 | [diff] [blame] | 759 | static void releaseMemArray(Mem *p, int N, int freebuffers){ |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 760 | if( p && N ){ |
| 761 | sqlite3 *db = p->db; |
| 762 | int malloc_failed = db->mallocFailed; |
drh | 76ff3a0 | 2004-09-24 22:32:30 +0000 | [diff] [blame] | 763 | while( N-->0 ){ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 764 | assert( N<2 || p[0].db==p[1].db ); |
danielk1977 | 5f09613 | 2008-03-28 15:44:09 +0000 | [diff] [blame] | 765 | if( freebuffers ){ |
danielk1977 | dfb316d | 2008-03-26 18:34:43 +0000 | [diff] [blame] | 766 | sqlite3VdbeMemRelease(p); |
danielk1977 | 5f09613 | 2008-03-28 15:44:09 +0000 | [diff] [blame] | 767 | }else{ |
| 768 | sqlite3VdbeMemReleaseExternal(p); |
danielk1977 | dfb316d | 2008-03-26 18:34:43 +0000 | [diff] [blame] | 769 | } |
danielk1977 | 5f09613 | 2008-03-28 15:44:09 +0000 | [diff] [blame] | 770 | p->flags = MEM_Null; |
danielk1977 | dfb316d | 2008-03-26 18:34:43 +0000 | [diff] [blame] | 771 | p++; |
drh | 76ff3a0 | 2004-09-24 22:32:30 +0000 | [diff] [blame] | 772 | } |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 773 | db->mallocFailed = malloc_failed; |
drh | 76ff3a0 | 2004-09-24 22:32:30 +0000 | [diff] [blame] | 774 | } |
| 775 | } |
| 776 | |
danielk1977 | dfb316d | 2008-03-26 18:34:43 +0000 | [diff] [blame] | 777 | #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT |
| 778 | int sqlite3VdbeReleaseBuffers(Vdbe *p){ |
| 779 | int ii; |
| 780 | int nFree = 0; |
| 781 | assert( sqlite3_mutex_held(p->db->mutex) ); |
| 782 | for(ii=1; ii<=p->nMem; ii++){ |
| 783 | Mem *pMem = &p->aMem[ii]; |
| 784 | if( pMem->z && pMem->flags&MEM_Dyn ){ |
| 785 | assert( !pMem->xDel ); |
| 786 | nFree += sqlite3MallocSize(pMem->z); |
| 787 | sqlite3VdbeMemRelease(pMem); |
| 788 | } |
| 789 | } |
| 790 | return nFree; |
| 791 | } |
| 792 | #endif |
| 793 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 794 | #ifndef SQLITE_OMIT_EXPLAIN |
drh | 76ff3a0 | 2004-09-24 22:32:30 +0000 | [diff] [blame] | 795 | /* |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 796 | ** Give a listing of the program in the virtual machine. |
| 797 | ** |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 798 | ** The interface is the same as sqlite3VdbeExec(). But instead of |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 799 | ** running the code, it invokes the callback once for each instruction. |
| 800 | ** This feature is used to implement "EXPLAIN". |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 801 | ** |
| 802 | ** When p->explain==1, each instruction is listed. When |
| 803 | ** p->explain==2, only OP_Explain instructions are listed and these |
| 804 | ** are shown in a different format. p->explain==2 is used to implement |
| 805 | ** EXPLAIN QUERY PLAN. |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 806 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 807 | int sqlite3VdbeList( |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 808 | Vdbe *p /* The VDBE */ |
| 809 | ){ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 810 | sqlite3 *db = p->db; |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 811 | int i; |
drh | 826fb5a | 2004-02-14 23:59:57 +0000 | [diff] [blame] | 812 | int rc = SQLITE_OK; |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 813 | Mem *pMem = p->pResultSet = &p->aMem[1]; |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 814 | |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 815 | assert( p->explain ); |
drh | c5cdca6 | 2005-01-11 16:54:14 +0000 | [diff] [blame] | 816 | if( p->magic!=VDBE_MAGIC_RUN ) return SQLITE_MISUSE; |
| 817 | assert( db->magic==SQLITE_MAGIC_BUSY ); |
| 818 | assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY ); |
danielk1977 | 18f4189 | 2004-05-22 07:27:46 +0000 | [diff] [blame] | 819 | |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 820 | /* Even though this opcode does not use dynamic strings for |
| 821 | ** the result, result columns may become dynamic if the user calls |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 822 | ** sqlite3_column_text16(), causing a translation to UTF-16 encoding. |
danielk1977 | 18f4189 | 2004-05-22 07:27:46 +0000 | [diff] [blame] | 823 | */ |
danielk1977 | dfb316d | 2008-03-26 18:34:43 +0000 | [diff] [blame] | 824 | releaseMemArray(pMem, p->nMem, 1); |
danielk1977 | 18f4189 | 2004-05-22 07:27:46 +0000 | [diff] [blame] | 825 | |
drh | ecc9242 | 2005-09-10 16:46:12 +0000 | [diff] [blame] | 826 | do{ |
| 827 | i = p->pc++; |
| 828 | }while( i<p->nOp && p->explain==2 && p->aOp[i].opcode!=OP_Explain ); |
drh | 826fb5a | 2004-02-14 23:59:57 +0000 | [diff] [blame] | 829 | if( i>=p->nOp ){ |
| 830 | p->rc = SQLITE_OK; |
| 831 | rc = SQLITE_DONE; |
drh | 881feaa | 2006-07-26 01:39:30 +0000 | [diff] [blame] | 832 | }else if( db->u1.isInterrupted ){ |
drh | c5cdca6 | 2005-01-11 16:54:14 +0000 | [diff] [blame] | 833 | p->rc = SQLITE_INTERRUPT; |
drh | 826fb5a | 2004-02-14 23:59:57 +0000 | [diff] [blame] | 834 | rc = SQLITE_ERROR; |
danielk1977 | f20b21c | 2004-05-31 23:56:42 +0000 | [diff] [blame] | 835 | sqlite3SetString(&p->zErrMsg, sqlite3ErrStr(p->rc), (char*)0); |
drh | 826fb5a | 2004-02-14 23:59:57 +0000 | [diff] [blame] | 836 | }else{ |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 837 | char *z; |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 838 | Op *pOp = &p->aOp[i]; |
danielk1977 | 0d78bae | 2008-01-03 07:09:48 +0000 | [diff] [blame] | 839 | if( p->explain==1 ){ |
| 840 | pMem->flags = MEM_Int; |
| 841 | pMem->type = SQLITE_INTEGER; |
| 842 | pMem->u.i = i; /* Program counter */ |
| 843 | pMem++; |
| 844 | |
| 845 | pMem->flags = MEM_Static|MEM_Str|MEM_Term; |
| 846 | pMem->z = (char*)sqlite3OpcodeName(pOp->opcode); /* Opcode */ |
| 847 | assert( pMem->z!=0 ); |
| 848 | pMem->n = strlen(pMem->z); |
| 849 | pMem->type = SQLITE_TEXT; |
| 850 | pMem->enc = SQLITE_UTF8; |
| 851 | pMem++; |
| 852 | } |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 853 | |
| 854 | pMem->flags = MEM_Int; |
drh | 3c024d6 | 2007-03-30 11:23:45 +0000 | [diff] [blame] | 855 | pMem->u.i = pOp->p1; /* P1 */ |
drh | 9c05483 | 2004-05-31 18:51:57 +0000 | [diff] [blame] | 856 | pMem->type = SQLITE_INTEGER; |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 857 | pMem++; |
| 858 | |
| 859 | pMem->flags = MEM_Int; |
drh | 3c024d6 | 2007-03-30 11:23:45 +0000 | [diff] [blame] | 860 | pMem->u.i = pOp->p2; /* P2 */ |
drh | 9c05483 | 2004-05-31 18:51:57 +0000 | [diff] [blame] | 861 | pMem->type = SQLITE_INTEGER; |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 862 | pMem++; |
| 863 | |
danielk1977 | 0d78bae | 2008-01-03 07:09:48 +0000 | [diff] [blame] | 864 | if( p->explain==1 ){ |
| 865 | pMem->flags = MEM_Int; |
| 866 | pMem->u.i = pOp->p3; /* P3 */ |
| 867 | pMem->type = SQLITE_INTEGER; |
| 868 | pMem++; |
| 869 | } |
| 870 | |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 871 | if( sqlite3VdbeMemGrow(pMem, 32, 0) ){ /* P4 */ |
| 872 | p->db->mallocFailed = 1; |
| 873 | return SQLITE_NOMEM; |
| 874 | } |
| 875 | pMem->flags = MEM_Dyn|MEM_Str|MEM_Term; |
| 876 | z = displayP4(pOp, pMem->z, 32); |
| 877 | if( z!=pMem->z ){ |
| 878 | sqlite3VdbeMemSetStr(pMem, z, -1, SQLITE_UTF8, 0); |
| 879 | }else{ |
| 880 | assert( pMem->z!=0 ); |
| 881 | pMem->n = strlen(pMem->z); |
| 882 | pMem->enc = SQLITE_UTF8; |
| 883 | } |
drh | 9c05483 | 2004-05-31 18:51:57 +0000 | [diff] [blame] | 884 | pMem->type = SQLITE_TEXT; |
danielk1977 | 0d78bae | 2008-01-03 07:09:48 +0000 | [diff] [blame] | 885 | pMem++; |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 886 | |
danielk1977 | 0d78bae | 2008-01-03 07:09:48 +0000 | [diff] [blame] | 887 | if( p->explain==1 ){ |
drh | 85e5f0d | 2008-02-19 18:28:13 +0000 | [diff] [blame] | 888 | if( sqlite3VdbeMemGrow(pMem, 4, 0) ){ |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 889 | p->db->mallocFailed = 1; |
| 890 | return SQLITE_NOMEM; |
| 891 | } |
| 892 | pMem->flags = MEM_Dyn|MEM_Str|MEM_Term; |
drh | 85e5f0d | 2008-02-19 18:28:13 +0000 | [diff] [blame] | 893 | pMem->n = 2; |
| 894 | sqlite3_snprintf(3, pMem->z, "%.2x", pOp->p5); /* P5 */ |
danielk1977 | 0d78bae | 2008-01-03 07:09:48 +0000 | [diff] [blame] | 895 | pMem->type = SQLITE_TEXT; |
| 896 | pMem->enc = SQLITE_UTF8; |
| 897 | pMem++; |
| 898 | |
drh | aa9b896 | 2008-01-08 02:57:55 +0000 | [diff] [blame] | 899 | #ifdef SQLITE_DEBUG |
danielk1977 | 0d78bae | 2008-01-03 07:09:48 +0000 | [diff] [blame] | 900 | if( pOp->zComment ){ |
| 901 | pMem->flags = MEM_Str|MEM_Term; |
| 902 | pMem->z = pOp->zComment; |
| 903 | pMem->n = strlen(pMem->z); |
| 904 | pMem->enc = SQLITE_UTF8; |
drh | 52391cb | 2008-02-14 23:44:13 +0000 | [diff] [blame] | 905 | }else |
drh | aa9b896 | 2008-01-08 02:57:55 +0000 | [diff] [blame] | 906 | #endif |
drh | 52391cb | 2008-02-14 23:44:13 +0000 | [diff] [blame] | 907 | { |
| 908 | pMem->flags = MEM_Null; /* Comment */ |
| 909 | pMem->type = SQLITE_NULL; |
| 910 | } |
danielk1977 | 0d78bae | 2008-01-03 07:09:48 +0000 | [diff] [blame] | 911 | } |
| 912 | |
| 913 | p->nResColumn = 8 - 5*(p->explain-1); |
drh | 826fb5a | 2004-02-14 23:59:57 +0000 | [diff] [blame] | 914 | p->rc = SQLITE_OK; |
| 915 | rc = SQLITE_ROW; |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 916 | } |
drh | 826fb5a | 2004-02-14 23:59:57 +0000 | [diff] [blame] | 917 | return rc; |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 918 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 919 | #endif /* SQLITE_OMIT_EXPLAIN */ |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 920 | |
drh | 7c4ac0c | 2007-04-05 11:25:58 +0000 | [diff] [blame] | 921 | #ifdef SQLITE_DEBUG |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 922 | /* |
drh | 3f7d4e4 | 2004-07-24 14:35:58 +0000 | [diff] [blame] | 923 | ** Print the SQL that was used to generate a VDBE program. |
| 924 | */ |
| 925 | void sqlite3VdbePrintSql(Vdbe *p){ |
drh | 3f7d4e4 | 2004-07-24 14:35:58 +0000 | [diff] [blame] | 926 | int nOp = p->nOp; |
| 927 | VdbeOp *pOp; |
drh | c16a03b | 2004-09-15 13:38:10 +0000 | [diff] [blame] | 928 | if( nOp<1 ) return; |
drh | 949f9cd | 2008-01-12 21:35:57 +0000 | [diff] [blame] | 929 | pOp = &p->aOp[0]; |
| 930 | if( pOp->opcode==OP_Trace && pOp->p4.z!=0 ){ |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 931 | const char *z = pOp->p4.z; |
drh | 4c755c0 | 2004-08-08 20:22:17 +0000 | [diff] [blame] | 932 | while( isspace(*(u8*)z) ) z++; |
drh | 3f7d4e4 | 2004-07-24 14:35:58 +0000 | [diff] [blame] | 933 | printf("SQL: [%s]\n", z); |
| 934 | } |
drh | 3f7d4e4 | 2004-07-24 14:35:58 +0000 | [diff] [blame] | 935 | } |
drh | 7c4ac0c | 2007-04-05 11:25:58 +0000 | [diff] [blame] | 936 | #endif |
drh | 3f7d4e4 | 2004-07-24 14:35:58 +0000 | [diff] [blame] | 937 | |
drh | 602c237 | 2007-03-01 00:29:13 +0000 | [diff] [blame] | 938 | #if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE) |
| 939 | /* |
| 940 | ** Print an IOTRACE message showing SQL content. |
| 941 | */ |
| 942 | void sqlite3VdbeIOTraceSql(Vdbe *p){ |
| 943 | int nOp = p->nOp; |
| 944 | VdbeOp *pOp; |
mlcreech | 3a00f90 | 2008-03-04 17:45:01 +0000 | [diff] [blame] | 945 | if( sqlite3IoTrace==0 ) return; |
drh | 602c237 | 2007-03-01 00:29:13 +0000 | [diff] [blame] | 946 | if( nOp<1 ) return; |
drh | 949f9cd | 2008-01-12 21:35:57 +0000 | [diff] [blame] | 947 | pOp = &p->aOp[0]; |
| 948 | if( pOp->opcode==OP_Trace && pOp->p4.z!=0 ){ |
drh | 602c237 | 2007-03-01 00:29:13 +0000 | [diff] [blame] | 949 | int i, j; |
drh | 00a18e4 | 2007-08-13 11:10:34 +0000 | [diff] [blame] | 950 | char z[1000]; |
drh | 949f9cd | 2008-01-12 21:35:57 +0000 | [diff] [blame] | 951 | sqlite3_snprintf(sizeof(z), z, "%s", pOp->p4.z); |
danielk1977 | 2be2be9 | 2007-05-16 17:50:45 +0000 | [diff] [blame] | 952 | for(i=0; isspace((unsigned char)z[i]); i++){} |
drh | 602c237 | 2007-03-01 00:29:13 +0000 | [diff] [blame] | 953 | for(j=0; z[i]; i++){ |
danielk1977 | 2be2be9 | 2007-05-16 17:50:45 +0000 | [diff] [blame] | 954 | if( isspace((unsigned char)z[i]) ){ |
drh | 602c237 | 2007-03-01 00:29:13 +0000 | [diff] [blame] | 955 | if( z[i-1]!=' ' ){ |
| 956 | z[j++] = ' '; |
| 957 | } |
| 958 | }else{ |
| 959 | z[j++] = z[i]; |
| 960 | } |
| 961 | } |
| 962 | z[j] = 0; |
mlcreech | 3a00f90 | 2008-03-04 17:45:01 +0000 | [diff] [blame] | 963 | sqlite3IoTrace("SQL %s\n", z); |
drh | 602c237 | 2007-03-01 00:29:13 +0000 | [diff] [blame] | 964 | } |
| 965 | } |
| 966 | #endif /* !SQLITE_OMIT_TRACE && SQLITE_ENABLE_IOTRACE */ |
| 967 | |
| 968 | |
drh | 3f7d4e4 | 2004-07-24 14:35:58 +0000 | [diff] [blame] | 969 | /* |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 970 | ** Prepare a virtual machine for execution. This involves things such |
| 971 | ** as allocating stack space and initializing the program counter. |
| 972 | ** After the VDBE has be prepped, it can be executed by one or more |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 973 | ** calls to sqlite3VdbeExec(). |
drh | 92f02c3 | 2004-09-02 14:57:08 +0000 | [diff] [blame] | 974 | ** |
| 975 | ** This is the only way to move a VDBE from VDBE_MAGIC_INIT to |
| 976 | ** VDBE_MAGIC_RUN. |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 977 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 978 | void sqlite3VdbeMakeReady( |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 979 | Vdbe *p, /* The VDBE */ |
drh | 7c972de | 2003-09-06 22:18:07 +0000 | [diff] [blame] | 980 | int nVar, /* Number of '?' see in the SQL statement */ |
drh | 290c194 | 2004-08-21 17:54:45 +0000 | [diff] [blame] | 981 | int nMem, /* Number of memory cells to allocate */ |
| 982 | int nCursor, /* Number of cursors to allocate */ |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 983 | int isExplain /* True if the EXPLAIN keywords is present */ |
| 984 | ){ |
| 985 | int n; |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 986 | sqlite3 *db = p->db; |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 987 | |
| 988 | assert( p!=0 ); |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 989 | assert( p->magic==VDBE_MAGIC_INIT ); |
| 990 | |
drh | c16a03b | 2004-09-15 13:38:10 +0000 | [diff] [blame] | 991 | /* There should be at least one opcode. |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 992 | */ |
drh | c16a03b | 2004-09-15 13:38:10 +0000 | [diff] [blame] | 993 | assert( p->nOp>0 ); |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 994 | |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 995 | /* Set the magic to VDBE_MAGIC_RUN sooner rather than later. This |
| 996 | * is because the call to resizeOpArray() below may shrink the |
| 997 | * p->aOp[] array to save memory if called when in VDBE_MAGIC_RUN |
| 998 | * state. |
| 999 | */ |
| 1000 | p->magic = VDBE_MAGIC_RUN; |
| 1001 | |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 1002 | /* For each cursor required, also allocate a memory cell. Memory |
| 1003 | ** cells (nMem+1-nCursor)..nMem, inclusive, will never be used by |
| 1004 | ** the vdbe program. Instead they are used to allocate space for |
| 1005 | ** Cursor/BtCursor structures. The blob of memory associated with |
| 1006 | ** cursor 0 is stored in memory cell nMem. Memory cell (nMem-1) |
| 1007 | ** stores the blob of memory associated with cursor 1, etc. |
| 1008 | ** |
| 1009 | ** See also: allocateCursor(). |
| 1010 | */ |
| 1011 | nMem += nCursor; |
| 1012 | |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1013 | /* |
| 1014 | ** Allocation space for registers. |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 1015 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1016 | if( p->aMem==0 ){ |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 1017 | int nArg; /* Maximum number of args passed to a user function. */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1018 | resolveP2Values(p, &nArg); |
drh | 26c9b5e | 2008-04-11 14:56:53 +0000 | [diff] [blame] | 1019 | /*resizeOpArray(p, p->nOp);*/ |
drh | 82a4851 | 2003-09-06 22:45:20 +0000 | [diff] [blame] | 1020 | assert( nVar>=0 ); |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1021 | if( isExplain && nMem<10 ){ |
| 1022 | p->nMem = nMem = 10; |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 1023 | } |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1024 | p->aMem = sqlite3DbMallocZero(db, |
| 1025 | nMem*sizeof(Mem) /* aMem */ |
drh | 86f4330 | 2004-10-05 17:37:36 +0000 | [diff] [blame] | 1026 | + nVar*sizeof(Mem) /* aVar */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1027 | + nArg*sizeof(Mem*) /* apArg */ |
drh | 86f4330 | 2004-10-05 17:37:36 +0000 | [diff] [blame] | 1028 | + nVar*sizeof(char*) /* azVar */ |
drh | 0a07c10 | 2008-01-03 18:03:08 +0000 | [diff] [blame] | 1029 | + nCursor*sizeof(Cursor*) + 1 /* apCsr */ |
drh | 82a4851 | 2003-09-06 22:45:20 +0000 | [diff] [blame] | 1030 | ); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 1031 | if( !db->mallocFailed ){ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1032 | p->aMem--; /* aMem[] goes from 1..nMem */ |
| 1033 | p->nMem = nMem; /* not from 0..nMem-1 */ |
drh | 0a07c10 | 2008-01-03 18:03:08 +0000 | [diff] [blame] | 1034 | p->aVar = &p->aMem[nMem+1]; |
drh | 86f4330 | 2004-10-05 17:37:36 +0000 | [diff] [blame] | 1035 | p->nVar = nVar; |
| 1036 | p->okVar = 0; |
| 1037 | p->apArg = (Mem**)&p->aVar[nVar]; |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 1038 | p->azVar = (char**)&p->apArg[nArg]; |
drh | 86f4330 | 2004-10-05 17:37:36 +0000 | [diff] [blame] | 1039 | p->apCsr = (Cursor**)&p->azVar[nVar]; |
drh | 290c194 | 2004-08-21 17:54:45 +0000 | [diff] [blame] | 1040 | p->nCursor = nCursor; |
| 1041 | for(n=0; n<nVar; n++){ |
| 1042 | p->aVar[n].flags = MEM_Null; |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 1043 | p->aVar[n].db = db; |
| 1044 | } |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1045 | for(n=1; n<=nMem; n++){ |
| 1046 | p->aMem[n].flags = MEM_Null; |
| 1047 | p->aMem[n].db = db; |
drh | 290c194 | 2004-08-21 17:54:45 +0000 | [diff] [blame] | 1048 | } |
danielk1977 | 54db47e | 2004-05-19 10:36:43 +0000 | [diff] [blame] | 1049 | } |
drh | 82a4851 | 2003-09-06 22:45:20 +0000 | [diff] [blame] | 1050 | } |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1051 | #ifdef SQLITE_DEBUG |
| 1052 | for(n=1; n<p->nMem; n++){ |
| 1053 | assert( p->aMem[n].db==db ); |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 1054 | } |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1055 | #endif |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 1056 | |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 1057 | p->pc = -1; |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 1058 | p->rc = SQLITE_OK; |
| 1059 | p->uniqueCnt = 0; |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 1060 | p->errorAction = OE_Abort; |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 1061 | p->explain |= isExplain; |
| 1062 | p->magic = VDBE_MAGIC_RUN; |
danielk1977 | b28af71 | 2004-06-21 06:50:26 +0000 | [diff] [blame] | 1063 | p->nChange = 0; |
drh | 76873ab | 2006-01-07 18:48:26 +0000 | [diff] [blame] | 1064 | p->cacheCtr = 1; |
drh | d946db0 | 2005-12-29 19:23:06 +0000 | [diff] [blame] | 1065 | p->minWriteFileFormat = 255; |
danielk1977 | 182c4ba | 2007-06-27 15:53:34 +0000 | [diff] [blame] | 1066 | p->openedStatement = 0; |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 1067 | #ifdef VDBE_PROFILE |
drh | cf64d8b | 2003-12-31 17:57:10 +0000 | [diff] [blame] | 1068 | { |
| 1069 | int i; |
| 1070 | for(i=0; i<p->nOp; i++){ |
| 1071 | p->aOp[i].cnt = 0; |
| 1072 | p->aOp[i].cycles = 0; |
| 1073 | } |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 1074 | } |
| 1075 | #endif |
| 1076 | } |
| 1077 | |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 1078 | /* |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 1079 | ** Close a VDBE cursor and release all the resources that cursor |
| 1080 | ** happens to hold. |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 1081 | */ |
danielk1977 | be71889 | 2006-06-23 08:05:19 +0000 | [diff] [blame] | 1082 | void sqlite3VdbeFreeCursor(Vdbe *p, Cursor *pCx){ |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 1083 | if( pCx==0 ){ |
| 1084 | return; |
| 1085 | } |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 1086 | if( pCx->pCursor ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1087 | sqlite3BtreeCloseCursor(pCx->pCursor); |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 1088 | } |
| 1089 | if( pCx->pBt ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1090 | sqlite3BtreeClose(pCx->pBt); |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 1091 | } |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 1092 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 1093 | if( pCx->pVtabCursor ){ |
| 1094 | sqlite3_vtab_cursor *pVtabCursor = pCx->pVtabCursor; |
danielk1977 | be71889 | 2006-06-23 08:05:19 +0000 | [diff] [blame] | 1095 | const sqlite3_module *pModule = pCx->pModule; |
| 1096 | p->inVtabMethod = 1; |
drh | 7e8b848 | 2008-01-23 03:03:05 +0000 | [diff] [blame] | 1097 | (void)sqlite3SafetyOff(p->db); |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 1098 | pModule->xClose(pVtabCursor); |
drh | 7e8b848 | 2008-01-23 03:03:05 +0000 | [diff] [blame] | 1099 | (void)sqlite3SafetyOn(p->db); |
danielk1977 | be71889 | 2006-06-23 08:05:19 +0000 | [diff] [blame] | 1100 | p->inVtabMethod = 0; |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 1101 | } |
| 1102 | #endif |
danielk1977 | 9882d99 | 2008-03-27 17:59:01 +0000 | [diff] [blame] | 1103 | if( !pCx->ephemPseudoTable ){ |
| 1104 | sqlite3_free(pCx->pData); |
| 1105 | } |
drh | 5c07053 | 2008-04-11 15:36:03 +0000 | [diff] [blame] | 1106 | /* memset(pCx, 0, sizeof(Cursor)); */ |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 1107 | /* sqlite3_free(pCx->aType); */ |
| 1108 | /* sqlite3_free(pCx); */ |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 1109 | } |
| 1110 | |
| 1111 | /* |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 1112 | ** Close all cursors except for VTab cursors that are currently |
| 1113 | ** in use. |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 1114 | */ |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 1115 | static void closeAllCursorsExceptActiveVtabs(Vdbe *p){ |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 1116 | int i; |
drh | 290c194 | 2004-08-21 17:54:45 +0000 | [diff] [blame] | 1117 | if( p->apCsr==0 ) return; |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 1118 | for(i=0; i<p->nCursor; i++){ |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 1119 | Cursor *pC = p->apCsr[i]; |
| 1120 | if( pC && (!p->inVtabMethod || !pC->pVtabCursor) ){ |
| 1121 | sqlite3VdbeFreeCursor(p, pC); |
danielk1977 | b7a2f2e | 2006-06-23 11:34:54 +0000 | [diff] [blame] | 1122 | p->apCsr[i] = 0; |
danielk1977 | be71889 | 2006-06-23 08:05:19 +0000 | [diff] [blame] | 1123 | } |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 1124 | } |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 1125 | } |
| 1126 | |
| 1127 | /* |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 1128 | ** Clean up the VM after execution. |
| 1129 | ** |
| 1130 | ** This routine will automatically close any cursors, lists, and/or |
| 1131 | ** sorters that were left open. It also deletes the values of |
drh | 5a12e68 | 2004-05-19 11:24:25 +0000 | [diff] [blame] | 1132 | ** variables in the aVar[] array. |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 1133 | */ |
danielk1977 | dfb316d | 2008-03-26 18:34:43 +0000 | [diff] [blame] | 1134 | static void Cleanup(Vdbe *p, int freebuffers){ |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 1135 | int i; |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 1136 | closeAllCursorsExceptActiveVtabs(p); |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1137 | for(i=1; i<=p->nMem; i++){ |
| 1138 | MemSetTypeFlag(&p->aMem[i], MEM_Null); |
| 1139 | } |
danielk1977 | dfb316d | 2008-03-26 18:34:43 +0000 | [diff] [blame] | 1140 | releaseMemArray(&p->aMem[1], p->nMem, freebuffers); |
drh | a01f79d | 2005-07-08 13:07:59 +0000 | [diff] [blame] | 1141 | sqlite3VdbeFifoClear(&p->sFifo); |
drh | 76ff3a0 | 2004-09-24 22:32:30 +0000 | [diff] [blame] | 1142 | if( p->contextStack ){ |
| 1143 | for(i=0; i<p->contextStackTop; i++){ |
drh | a01f79d | 2005-07-08 13:07:59 +0000 | [diff] [blame] | 1144 | sqlite3VdbeFifoClear(&p->contextStack[i].sFifo); |
drh | 76ff3a0 | 2004-09-24 22:32:30 +0000 | [diff] [blame] | 1145 | } |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 1146 | sqlite3_free(p->contextStack); |
drh | 344737f | 2004-09-19 00:50:20 +0000 | [diff] [blame] | 1147 | } |
drh | 5f96843 | 2004-02-21 19:02:30 +0000 | [diff] [blame] | 1148 | p->contextStack = 0; |
drh | 344737f | 2004-09-19 00:50:20 +0000 | [diff] [blame] | 1149 | p->contextStackDepth = 0; |
| 1150 | p->contextStackTop = 0; |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 1151 | sqlite3_free(p->zErrMsg); |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 1152 | p->zErrMsg = 0; |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 1153 | p->pResultSet = 0; |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 1154 | } |
| 1155 | |
| 1156 | /* |
danielk1977 | 22322fd | 2004-05-25 23:35:17 +0000 | [diff] [blame] | 1157 | ** Set the number of result columns that will be returned by this SQL |
| 1158 | ** statement. This is now set at compile time, rather than during |
| 1159 | ** execution of the vdbe program so that sqlite3_column_count() can |
| 1160 | ** be called on an SQL statement before sqlite3_step(). |
| 1161 | */ |
| 1162 | void sqlite3VdbeSetNumCols(Vdbe *p, int nResColumn){ |
drh | 76ff3a0 | 2004-09-24 22:32:30 +0000 | [diff] [blame] | 1163 | Mem *pColName; |
| 1164 | int n; |
drh | 4a50aac | 2007-08-23 02:47:53 +0000 | [diff] [blame] | 1165 | |
danielk1977 | dfb316d | 2008-03-26 18:34:43 +0000 | [diff] [blame] | 1166 | releaseMemArray(p->aColName, p->nResColumn*COLNAME_N, 1); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 1167 | sqlite3_free(p->aColName); |
danielk1977 | 955de52 | 2006-02-10 02:27:42 +0000 | [diff] [blame] | 1168 | n = nResColumn*COLNAME_N; |
drh | cc43cab | 2005-10-05 11:35:09 +0000 | [diff] [blame] | 1169 | p->nResColumn = nResColumn; |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 1170 | p->aColName = pColName = (Mem*)sqlite3DbMallocZero(p->db, sizeof(Mem)*n ); |
drh | 76ff3a0 | 2004-09-24 22:32:30 +0000 | [diff] [blame] | 1171 | if( p->aColName==0 ) return; |
| 1172 | while( n-- > 0 ){ |
drh | 4a50aac | 2007-08-23 02:47:53 +0000 | [diff] [blame] | 1173 | pColName->flags = MEM_Null; |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 1174 | pColName->db = p->db; |
drh | 4a50aac | 2007-08-23 02:47:53 +0000 | [diff] [blame] | 1175 | pColName++; |
drh | 76ff3a0 | 2004-09-24 22:32:30 +0000 | [diff] [blame] | 1176 | } |
danielk1977 | 22322fd | 2004-05-25 23:35:17 +0000 | [diff] [blame] | 1177 | } |
| 1178 | |
| 1179 | /* |
danielk1977 | 3cf8606 | 2004-05-26 10:11:05 +0000 | [diff] [blame] | 1180 | ** Set the name of the idx'th column to be returned by the SQL statement. |
| 1181 | ** zName must be a pointer to a nul terminated string. |
| 1182 | ** |
| 1183 | ** This call must be made after a call to sqlite3VdbeSetNumCols(). |
| 1184 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 1185 | ** If N==P4_STATIC it means that zName is a pointer to a constant static |
| 1186 | ** string and we can just copy the pointer. If it is P4_DYNAMIC, then |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 1187 | ** the string is freed using sqlite3_free() when the vdbe is finished with |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 1188 | ** it. Otherwise, N bytes of zName are copied. |
danielk1977 | 3cf8606 | 2004-05-26 10:11:05 +0000 | [diff] [blame] | 1189 | */ |
danielk1977 | 955de52 | 2006-02-10 02:27:42 +0000 | [diff] [blame] | 1190 | int sqlite3VdbeSetColName(Vdbe *p, int idx, int var, const char *zName, int N){ |
danielk1977 | 3cf8606 | 2004-05-26 10:11:05 +0000 | [diff] [blame] | 1191 | int rc; |
| 1192 | Mem *pColName; |
danielk1977 | 955de52 | 2006-02-10 02:27:42 +0000 | [diff] [blame] | 1193 | assert( idx<p->nResColumn ); |
| 1194 | assert( var<COLNAME_N ); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 1195 | if( p->db->mallocFailed ) return SQLITE_NOMEM; |
drh | 76ff3a0 | 2004-09-24 22:32:30 +0000 | [diff] [blame] | 1196 | assert( p->aColName!=0 ); |
danielk1977 | 955de52 | 2006-02-10 02:27:42 +0000 | [diff] [blame] | 1197 | pColName = &(p->aColName[idx+var*p->nResColumn]); |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 1198 | if( N==P4_DYNAMIC || N==P4_STATIC ){ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 1199 | rc = sqlite3VdbeMemSetStr(pColName, zName, -1, SQLITE_UTF8, SQLITE_STATIC); |
danielk1977 | 3cf8606 | 2004-05-26 10:11:05 +0000 | [diff] [blame] | 1200 | }else{ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 1201 | rc = sqlite3VdbeMemSetStr(pColName, zName, N, SQLITE_UTF8,SQLITE_TRANSIENT); |
danielk1977 | 3cf8606 | 2004-05-26 10:11:05 +0000 | [diff] [blame] | 1202 | } |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 1203 | if( rc==SQLITE_OK && N==P4_DYNAMIC ){ |
danielk1977 | 5f09613 | 2008-03-28 15:44:09 +0000 | [diff] [blame] | 1204 | pColName->flags &= (~MEM_Static); |
| 1205 | pColName->zMalloc = pColName->z; |
danielk1977 | 3cf8606 | 2004-05-26 10:11:05 +0000 | [diff] [blame] | 1206 | } |
| 1207 | return rc; |
| 1208 | } |
| 1209 | |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1210 | /* |
| 1211 | ** A read or write transaction may or may not be active on database handle |
| 1212 | ** db. If a transaction is active, commit it. If there is a |
| 1213 | ** write-transaction spanning more than one database file, this routine |
| 1214 | ** takes care of the master journal trickery. |
| 1215 | */ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 1216 | static int vdbeCommit(sqlite3 *db){ |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1217 | int i; |
| 1218 | int nTrans = 0; /* Number of databases with an active write-transaction */ |
| 1219 | int rc = SQLITE_OK; |
| 1220 | int needXcommit = 0; |
| 1221 | |
danielk1977 | 5bd270b | 2006-07-25 15:14:52 +0000 | [diff] [blame] | 1222 | /* Before doing anything else, call the xSync() callback for any |
| 1223 | ** virtual module tables written in this transaction. This has to |
| 1224 | ** be done before determining whether a master journal file is |
| 1225 | ** required, as an xSync() callback may add an attached database |
| 1226 | ** to the transaction. |
| 1227 | */ |
| 1228 | rc = sqlite3VtabSync(db, rc); |
| 1229 | if( rc!=SQLITE_OK ){ |
| 1230 | return rc; |
| 1231 | } |
| 1232 | |
| 1233 | /* This loop determines (a) if the commit hook should be invoked and |
| 1234 | ** (b) how many database files have open write transactions, not |
| 1235 | ** including the temp database. (b) is important because if more than |
| 1236 | ** one database file has an open write transaction, a master journal |
| 1237 | ** file is required for an atomic commit. |
| 1238 | */ |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1239 | for(i=0; i<db->nDb; i++){ |
| 1240 | Btree *pBt = db->aDb[i].pBt; |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 1241 | if( sqlite3BtreeIsInTrans(pBt) ){ |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1242 | needXcommit = 1; |
| 1243 | if( i!=1 ) nTrans++; |
| 1244 | } |
| 1245 | } |
| 1246 | |
| 1247 | /* If there are any write-transactions at all, invoke the commit hook */ |
| 1248 | if( needXcommit && db->xCommitCallback ){ |
drh | 7e8b848 | 2008-01-23 03:03:05 +0000 | [diff] [blame] | 1249 | (void)sqlite3SafetyOff(db); |
drh | 92f02c3 | 2004-09-02 14:57:08 +0000 | [diff] [blame] | 1250 | rc = db->xCommitCallback(db->pCommitArg); |
drh | 7e8b848 | 2008-01-23 03:03:05 +0000 | [diff] [blame] | 1251 | (void)sqlite3SafetyOn(db); |
drh | 92f02c3 | 2004-09-02 14:57:08 +0000 | [diff] [blame] | 1252 | if( rc ){ |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1253 | return SQLITE_CONSTRAINT; |
| 1254 | } |
| 1255 | } |
| 1256 | |
danielk1977 | 40b38dc | 2004-06-26 08:38:24 +0000 | [diff] [blame] | 1257 | /* The simple case - no more than one database file (not counting the |
| 1258 | ** TEMP database) has a transaction active. There is no need for the |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1259 | ** master-journal. |
drh | c9e0686 | 2004-06-09 20:03:08 +0000 | [diff] [blame] | 1260 | ** |
danielk1977 | 40b38dc | 2004-06-26 08:38:24 +0000 | [diff] [blame] | 1261 | ** If the return value of sqlite3BtreeGetFilename() is a zero length |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 1262 | ** string, it means the main database is :memory: or a temp file. In |
| 1263 | ** that case we do not support atomic multi-file commits, so use the |
| 1264 | ** simple case then too. |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1265 | */ |
danielk1977 | 40b38dc | 2004-06-26 08:38:24 +0000 | [diff] [blame] | 1266 | if( 0==strlen(sqlite3BtreeGetFilename(db->aDb[0].pBt)) || nTrans<=1 ){ |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1267 | for(i=0; rc==SQLITE_OK && i<db->nDb; i++){ |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1268 | Btree *pBt = db->aDb[i].pBt; |
| 1269 | if( pBt ){ |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 1270 | rc = sqlite3BtreeCommitPhaseOne(pBt, 0); |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1271 | } |
| 1272 | } |
| 1273 | |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 1274 | /* Do the commit only if all databases successfully complete phase 1. |
| 1275 | ** If one of the BtreeCommitPhaseOne() calls fails, this indicates an |
| 1276 | ** IO error while deleting or truncating a journal file. It is unlikely, |
| 1277 | ** but could happen. In this case abandon processing and return the error. |
danielk1977 | 979f38e | 2007-03-27 16:19:51 +0000 | [diff] [blame] | 1278 | */ |
| 1279 | for(i=0; rc==SQLITE_OK && i<db->nDb; i++){ |
| 1280 | Btree *pBt = db->aDb[i].pBt; |
| 1281 | if( pBt ){ |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 1282 | rc = sqlite3BtreeCommitPhaseTwo(pBt); |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1283 | } |
danielk1977 | 979f38e | 2007-03-27 16:19:51 +0000 | [diff] [blame] | 1284 | } |
| 1285 | if( rc==SQLITE_OK ){ |
danielk1977 | f9e7dda | 2006-06-16 16:08:53 +0000 | [diff] [blame] | 1286 | sqlite3VtabCommit(db); |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1287 | } |
| 1288 | } |
| 1289 | |
| 1290 | /* The complex case - There is a multi-file write-transaction active. |
| 1291 | ** This requires a master journal file to ensure the transaction is |
| 1292 | ** committed atomicly. |
| 1293 | */ |
danielk1977 | 44ee5bf | 2005-05-27 09:41:12 +0000 | [diff] [blame] | 1294 | #ifndef SQLITE_OMIT_DISKIO |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1295 | else{ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 1296 | sqlite3_vfs *pVfs = db->pVfs; |
drh | 2c8997b | 2005-08-27 16:36:48 +0000 | [diff] [blame] | 1297 | int needSync = 0; |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1298 | char *zMaster = 0; /* File-name for the master journal */ |
| 1299 | char const *zMainFile = sqlite3BtreeGetFilename(db->aDb[0].pBt); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 1300 | sqlite3_file *pMaster = 0; |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 1301 | i64 offset = 0; |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 1302 | int res; |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1303 | |
| 1304 | /* Select a master journal file name */ |
| 1305 | do { |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1306 | u32 random; |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 1307 | sqlite3_free(zMaster); |
drh | 2fa1868 | 2008-03-19 14:15:34 +0000 | [diff] [blame] | 1308 | sqlite3_randomness(sizeof(random), &random); |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 1309 | zMaster = sqlite3MPrintf(db, "%s-mj%08X", zMainFile, random&0x7fffffff); |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1310 | if( !zMaster ){ |
| 1311 | return SQLITE_NOMEM; |
| 1312 | } |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 1313 | rc = sqlite3OsAccess(pVfs, zMaster, SQLITE_ACCESS_EXISTS, &res); |
| 1314 | }while( rc==SQLITE_OK && res ); |
| 1315 | if( rc==SQLITE_OK ){ |
drh | 19db935 | 2008-03-27 22:42:51 +0000 | [diff] [blame] | 1316 | /* Open the master journal. */ |
| 1317 | rc = sqlite3OsOpenMalloc(pVfs, zMaster, &pMaster, |
| 1318 | SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE| |
| 1319 | SQLITE_OPEN_EXCLUSIVE|SQLITE_OPEN_MASTER_JOURNAL, 0 |
| 1320 | ); |
| 1321 | } |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1322 | if( rc!=SQLITE_OK ){ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 1323 | sqlite3_free(zMaster); |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1324 | return rc; |
| 1325 | } |
| 1326 | |
| 1327 | /* Write the name of each database file in the transaction into the new |
| 1328 | ** master journal file. If an error occurs at this point close |
| 1329 | ** and delete the master journal file. All the individual journal files |
| 1330 | ** still have 'null' as the master journal pointer, so they will roll |
danielk1977 | aca790a | 2005-01-13 11:07:52 +0000 | [diff] [blame] | 1331 | ** back independently if a failure occurs. |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1332 | */ |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 1333 | for(i=0; i<db->nDb; i++){ |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1334 | Btree *pBt = db->aDb[i].pBt; |
drh | c9e0686 | 2004-06-09 20:03:08 +0000 | [diff] [blame] | 1335 | if( i==1 ) continue; /* Ignore the TEMP database */ |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 1336 | if( sqlite3BtreeIsInTrans(pBt) ){ |
danielk1977 | 5865e3d | 2004-06-14 06:03:57 +0000 | [diff] [blame] | 1337 | char const *zFile = sqlite3BtreeGetJournalname(pBt); |
drh | c9e0686 | 2004-06-09 20:03:08 +0000 | [diff] [blame] | 1338 | if( zFile[0]==0 ) continue; /* Ignore :memory: databases */ |
drh | 2c8997b | 2005-08-27 16:36:48 +0000 | [diff] [blame] | 1339 | if( !needSync && !sqlite3BtreeSyncDisabled(pBt) ){ |
| 1340 | needSync = 1; |
| 1341 | } |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 1342 | rc = sqlite3OsWrite(pMaster, zFile, strlen(zFile)+1, offset); |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 1343 | offset += strlen(zFile)+1; |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1344 | if( rc!=SQLITE_OK ){ |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 1345 | sqlite3OsCloseFree(pMaster); |
| 1346 | sqlite3OsDelete(pVfs, zMaster, 0); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 1347 | sqlite3_free(zMaster); |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1348 | return rc; |
| 1349 | } |
| 1350 | } |
| 1351 | } |
| 1352 | |
danielk1977 | 9663b8f | 2007-08-24 11:52:28 +0000 | [diff] [blame] | 1353 | /* Sync the master journal file. If the IOCAP_SEQUENTIAL device |
| 1354 | ** flag is set this is not required. |
| 1355 | */ |
danielk1977 | 5865e3d | 2004-06-14 06:03:57 +0000 | [diff] [blame] | 1356 | zMainFile = sqlite3BtreeGetDirname(db->aDb[0].pBt); |
danielk1977 | f036aef | 2007-08-20 05:36:51 +0000 | [diff] [blame] | 1357 | if( (needSync |
danielk1977 | 9663b8f | 2007-08-24 11:52:28 +0000 | [diff] [blame] | 1358 | && (0==(sqlite3OsDeviceCharacteristics(pMaster)&SQLITE_IOCAP_SEQUENTIAL)) |
danielk1977 | f036aef | 2007-08-20 05:36:51 +0000 | [diff] [blame] | 1359 | && (rc=sqlite3OsSync(pMaster, SQLITE_SYNC_NORMAL))!=SQLITE_OK) ){ |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 1360 | sqlite3OsCloseFree(pMaster); |
| 1361 | sqlite3OsDelete(pVfs, zMaster, 0); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 1362 | sqlite3_free(zMaster); |
danielk1977 | 5865e3d | 2004-06-14 06:03:57 +0000 | [diff] [blame] | 1363 | return rc; |
| 1364 | } |
drh | c9e0686 | 2004-06-09 20:03:08 +0000 | [diff] [blame] | 1365 | |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1366 | /* Sync all the db files involved in the transaction. The same call |
| 1367 | ** sets the master journal pointer in each individual journal. If |
| 1368 | ** an error occurs here, do not delete the master journal file. |
| 1369 | ** |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 1370 | ** If the error occurs during the first call to |
| 1371 | ** sqlite3BtreeCommitPhaseOne(), then there is a chance that the |
| 1372 | ** master journal file will be orphaned. But we cannot delete it, |
| 1373 | ** in case the master journal file name was written into the journal |
| 1374 | ** file before the failure occured. |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1375 | */ |
danielk1977 | 5bd270b | 2006-07-25 15:14:52 +0000 | [diff] [blame] | 1376 | for(i=0; rc==SQLITE_OK && i<db->nDb; i++){ |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1377 | Btree *pBt = db->aDb[i].pBt; |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 1378 | if( pBt ){ |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 1379 | rc = sqlite3BtreeCommitPhaseOne(pBt, zMaster); |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1380 | } |
| 1381 | } |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 1382 | sqlite3OsCloseFree(pMaster); |
danielk1977 | 5bd270b | 2006-07-25 15:14:52 +0000 | [diff] [blame] | 1383 | if( rc!=SQLITE_OK ){ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 1384 | sqlite3_free(zMaster); |
danielk1977 | 5bd270b | 2006-07-25 15:14:52 +0000 | [diff] [blame] | 1385 | return rc; |
| 1386 | } |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1387 | |
danielk1977 | 962398d | 2004-06-14 09:35:16 +0000 | [diff] [blame] | 1388 | /* Delete the master journal file. This commits the transaction. After |
| 1389 | ** doing this the directory is synced again before any individual |
| 1390 | ** transaction files are deleted. |
| 1391 | */ |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 1392 | rc = sqlite3OsDelete(pVfs, zMaster, 1); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 1393 | sqlite3_free(zMaster); |
drh | c416ba9 | 2007-03-30 18:42:55 +0000 | [diff] [blame] | 1394 | zMaster = 0; |
drh | 29a0138 | 2006-08-13 19:04:18 +0000 | [diff] [blame] | 1395 | if( rc ){ |
| 1396 | return rc; |
| 1397 | } |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1398 | |
| 1399 | /* All files and directories have already been synced, so the following |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 1400 | ** calls to sqlite3BtreeCommitPhaseTwo() are only closing files and |
| 1401 | ** deleting or truncating journals. If something goes wrong while |
| 1402 | ** this is happening we don't really care. The integrity of the |
| 1403 | ** transaction is already guaranteed, but some stray 'cold' journals |
| 1404 | ** may be lying around. Returning an error code won't help matters. |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1405 | */ |
danielk1977 | 979f38e | 2007-03-27 16:19:51 +0000 | [diff] [blame] | 1406 | disable_simulated_io_errors(); |
danielk1977 | 2d1d86f | 2008-06-20 14:59:51 +0000 | [diff] [blame] | 1407 | sqlite3BeginBenignMalloc(); |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1408 | for(i=0; i<db->nDb; i++){ |
| 1409 | Btree *pBt = db->aDb[i].pBt; |
| 1410 | if( pBt ){ |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 1411 | sqlite3BtreeCommitPhaseTwo(pBt); |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1412 | } |
| 1413 | } |
danielk1977 | 2d1d86f | 2008-06-20 14:59:51 +0000 | [diff] [blame] | 1414 | sqlite3EndBenignMalloc(); |
danielk1977 | 979f38e | 2007-03-27 16:19:51 +0000 | [diff] [blame] | 1415 | enable_simulated_io_errors(); |
| 1416 | |
danielk1977 | f9e7dda | 2006-06-16 16:08:53 +0000 | [diff] [blame] | 1417 | sqlite3VtabCommit(db); |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1418 | } |
danielk1977 | 44ee5bf | 2005-05-27 09:41:12 +0000 | [diff] [blame] | 1419 | #endif |
danielk1977 | 026d270 | 2004-06-14 13:14:59 +0000 | [diff] [blame] | 1420 | |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1421 | return rc; |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1422 | } |
| 1423 | |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 1424 | /* |
| 1425 | ** This routine checks that the sqlite3.activeVdbeCnt count variable |
| 1426 | ** matches the number of vdbe's in the list sqlite3.pVdbe that are |
| 1427 | ** currently active. An assertion fails if the two counts do not match. |
drh | 92f02c3 | 2004-09-02 14:57:08 +0000 | [diff] [blame] | 1428 | ** This is an internal self-check only - it is not an essential processing |
| 1429 | ** step. |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 1430 | ** |
| 1431 | ** This is a no-op if NDEBUG is defined. |
| 1432 | */ |
| 1433 | #ifndef NDEBUG |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 1434 | static void checkActiveVdbeCnt(sqlite3 *db){ |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 1435 | Vdbe *p; |
| 1436 | int cnt = 0; |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 1437 | p = db->pVdbe; |
| 1438 | while( p ){ |
drh | 92f02c3 | 2004-09-02 14:57:08 +0000 | [diff] [blame] | 1439 | if( p->magic==VDBE_MAGIC_RUN && p->pc>=0 ){ |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 1440 | cnt++; |
| 1441 | } |
| 1442 | p = p->pNext; |
| 1443 | } |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 1444 | assert( cnt==db->activeVdbeCnt ); |
| 1445 | } |
| 1446 | #else |
| 1447 | #define checkActiveVdbeCnt(x) |
| 1448 | #endif |
| 1449 | |
danielk1977 | 3cf8606 | 2004-05-26 10:11:05 +0000 | [diff] [blame] | 1450 | /* |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 1451 | ** For every Btree that in database connection db which |
| 1452 | ** has been modified, "trip" or invalidate each cursor in |
| 1453 | ** that Btree might have been modified so that the cursor |
| 1454 | ** can never be used again. This happens when a rollback |
| 1455 | *** occurs. We have to trip all the other cursors, even |
| 1456 | ** cursor from other VMs in different database connections, |
| 1457 | ** so that none of them try to use the data at which they |
| 1458 | ** were pointing and which now may have been changed due |
| 1459 | ** to the rollback. |
| 1460 | ** |
| 1461 | ** Remember that a rollback can delete tables complete and |
| 1462 | ** reorder rootpages. So it is not sufficient just to save |
| 1463 | ** the state of the cursor. We have to invalidate the cursor |
| 1464 | ** so that it is never used again. |
danielk1977 | be71889 | 2006-06-23 08:05:19 +0000 | [diff] [blame] | 1465 | */ |
drh | ade6c9c | 2007-11-24 10:23:44 +0000 | [diff] [blame] | 1466 | static void invalidateCursorsOnModifiedBtrees(sqlite3 *db){ |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 1467 | int i; |
| 1468 | for(i=0; i<db->nDb; i++){ |
| 1469 | Btree *p = db->aDb[i].pBt; |
| 1470 | if( p && sqlite3BtreeIsInTrans(p) ){ |
| 1471 | sqlite3BtreeTripAllCursors(p, SQLITE_ABORT); |
| 1472 | } |
danielk1977 | be71889 | 2006-06-23 08:05:19 +0000 | [diff] [blame] | 1473 | } |
| 1474 | } |
| 1475 | |
| 1476 | /* |
drh | 92f02c3 | 2004-09-02 14:57:08 +0000 | [diff] [blame] | 1477 | ** This routine is called the when a VDBE tries to halt. If the VDBE |
| 1478 | ** has made changes and is in autocommit mode, then commit those |
| 1479 | ** changes. If a rollback is needed, then do the rollback. |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 1480 | ** |
drh | 92f02c3 | 2004-09-02 14:57:08 +0000 | [diff] [blame] | 1481 | ** This routine is the only way to move the state of a VM from |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 1482 | ** SQLITE_MAGIC_RUN to SQLITE_MAGIC_HALT. It is harmless to |
| 1483 | ** call this on a VM that is in the SQLITE_MAGIC_HALT state. |
drh | 92f02c3 | 2004-09-02 14:57:08 +0000 | [diff] [blame] | 1484 | ** |
| 1485 | ** Return an error code. If the commit could not complete because of |
| 1486 | ** lock contention, return SQLITE_BUSY. If SQLITE_BUSY is returned, it |
| 1487 | ** means the close did not happen and needs to be repeated. |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 1488 | */ |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 1489 | int sqlite3VdbeHalt(Vdbe *p){ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 1490 | sqlite3 *db = p->db; |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 1491 | int i; |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 1492 | int (*xFunc)(Btree *pBt) = 0; /* Function to call on each btree backend */ |
danielk1977 | 07cb560 | 2006-01-20 10:55:05 +0000 | [diff] [blame] | 1493 | int isSpecialError; /* Set to true if SQLITE_NOMEM or IOERR */ |
| 1494 | |
| 1495 | /* This function contains the logic that determines if a statement or |
| 1496 | ** transaction will be committed or rolled back as a result of the |
| 1497 | ** execution of this virtual machine. |
| 1498 | ** |
drh | 71b890a | 2007-10-03 15:30:52 +0000 | [diff] [blame] | 1499 | ** If any of the following errors occur: |
danielk1977 | 07cb560 | 2006-01-20 10:55:05 +0000 | [diff] [blame] | 1500 | ** |
drh | 71b890a | 2007-10-03 15:30:52 +0000 | [diff] [blame] | 1501 | ** SQLITE_NOMEM |
| 1502 | ** SQLITE_IOERR |
| 1503 | ** SQLITE_FULL |
| 1504 | ** SQLITE_INTERRUPT |
danielk1977 | 07cb560 | 2006-01-20 10:55:05 +0000 | [diff] [blame] | 1505 | ** |
drh | 71b890a | 2007-10-03 15:30:52 +0000 | [diff] [blame] | 1506 | ** Then the internal cache might have been left in an inconsistent |
| 1507 | ** state. We need to rollback the statement transaction, if there is |
| 1508 | ** one, or the complete transaction if there is no statement transaction. |
danielk1977 | 07cb560 | 2006-01-20 10:55:05 +0000 | [diff] [blame] | 1509 | */ |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 1510 | |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 1511 | if( p->db->mallocFailed ){ |
danielk1977 | 261919c | 2005-12-06 12:52:59 +0000 | [diff] [blame] | 1512 | p->rc = SQLITE_NOMEM; |
| 1513 | } |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 1514 | closeAllCursorsExceptActiveVtabs(p); |
drh | 92f02c3 | 2004-09-02 14:57:08 +0000 | [diff] [blame] | 1515 | if( p->magic!=VDBE_MAGIC_RUN ){ |
drh | 92f02c3 | 2004-09-02 14:57:08 +0000 | [diff] [blame] | 1516 | return SQLITE_OK; |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 1517 | } |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 1518 | checkActiveVdbeCnt(db); |
danielk1977 | 261919c | 2005-12-06 12:52:59 +0000 | [diff] [blame] | 1519 | |
danielk1977 | 07cb560 | 2006-01-20 10:55:05 +0000 | [diff] [blame] | 1520 | /* No commit or rollback needed if the program never started */ |
| 1521 | if( p->pc>=0 ){ |
drh | aac2f55 | 2006-09-23 21:44:23 +0000 | [diff] [blame] | 1522 | int mrc; /* Primary error code from p->rc */ |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 1523 | |
| 1524 | /* Lock all btrees used by the statement */ |
| 1525 | sqlite3BtreeMutexArrayEnter(&p->aMutex); |
| 1526 | |
drh | 71b890a | 2007-10-03 15:30:52 +0000 | [diff] [blame] | 1527 | /* Check for one of the special errors */ |
drh | aac2f55 | 2006-09-23 21:44:23 +0000 | [diff] [blame] | 1528 | mrc = p->rc & 0xff; |
drh | 71b890a | 2007-10-03 15:30:52 +0000 | [diff] [blame] | 1529 | isSpecialError = mrc==SQLITE_NOMEM || mrc==SQLITE_IOERR |
drh | 77658e2 | 2007-12-04 16:54:52 +0000 | [diff] [blame] | 1530 | || mrc==SQLITE_INTERRUPT || mrc==SQLITE_FULL; |
danielk1977 | 07cb560 | 2006-01-20 10:55:05 +0000 | [diff] [blame] | 1531 | if( isSpecialError ){ |
danielk1977 | 261919c | 2005-12-06 12:52:59 +0000 | [diff] [blame] | 1532 | /* This loop does static analysis of the query to see which of the |
| 1533 | ** following three categories it falls into: |
| 1534 | ** |
| 1535 | ** Read-only |
danielk1977 | 07cb560 | 2006-01-20 10:55:05 +0000 | [diff] [blame] | 1536 | ** Query with statement journal |
| 1537 | ** Query without statement journal |
danielk1977 | 261919c | 2005-12-06 12:52:59 +0000 | [diff] [blame] | 1538 | ** |
| 1539 | ** We could do something more elegant than this static analysis (i.e. |
| 1540 | ** store the type of query as part of the compliation phase), but |
danielk1977 | 07cb560 | 2006-01-20 10:55:05 +0000 | [diff] [blame] | 1541 | ** handling malloc() or IO failure is a fairly obscure edge case so |
| 1542 | ** this is probably easier. Todo: Might be an opportunity to reduce |
| 1543 | ** code size a very small amount though... |
danielk1977 | 261919c | 2005-12-06 12:52:59 +0000 | [diff] [blame] | 1544 | */ |
drh | d181704 | 2007-10-03 18:45:04 +0000 | [diff] [blame] | 1545 | int notReadOnly = 0; |
danielk1977 | 261919c | 2005-12-06 12:52:59 +0000 | [diff] [blame] | 1546 | int isStatement = 0; |
| 1547 | assert(p->aOp || p->nOp==0); |
| 1548 | for(i=0; i<p->nOp; i++){ |
| 1549 | switch( p->aOp[i].opcode ){ |
| 1550 | case OP_Transaction: |
drh | d181704 | 2007-10-03 18:45:04 +0000 | [diff] [blame] | 1551 | notReadOnly |= p->aOp[i].p2; |
danielk1977 | 261919c | 2005-12-06 12:52:59 +0000 | [diff] [blame] | 1552 | break; |
| 1553 | case OP_Statement: |
| 1554 | isStatement = 1; |
| 1555 | break; |
| 1556 | } |
| 1557 | } |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 1558 | |
| 1559 | |
danielk1977 | 07cb560 | 2006-01-20 10:55:05 +0000 | [diff] [blame] | 1560 | /* If the query was read-only, we need do no rollback at all. Otherwise, |
| 1561 | ** proceed with the special handling. |
| 1562 | */ |
drh | d181704 | 2007-10-03 18:45:04 +0000 | [diff] [blame] | 1563 | if( notReadOnly || mrc!=SQLITE_INTERRUPT ){ |
danielk1977 | e965ac7 | 2007-06-13 15:22:28 +0000 | [diff] [blame] | 1564 | if( p->rc==SQLITE_IOERR_BLOCKED && isStatement ){ |
| 1565 | xFunc = sqlite3BtreeRollbackStmt; |
| 1566 | p->rc = SQLITE_BUSY; |
drh | d181704 | 2007-10-03 18:45:04 +0000 | [diff] [blame] | 1567 | } else if( (mrc==SQLITE_NOMEM || mrc==SQLITE_FULL) && isStatement ){ |
danielk1977 | 07cb560 | 2006-01-20 10:55:05 +0000 | [diff] [blame] | 1568 | xFunc = sqlite3BtreeRollbackStmt; |
| 1569 | }else{ |
| 1570 | /* We are forced to roll back the active transaction. Before doing |
| 1571 | ** so, abort any other statements this handle currently has active. |
| 1572 | */ |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 1573 | invalidateCursorsOnModifiedBtrees(db); |
danielk1977 | 97a227c | 2006-01-20 16:32:04 +0000 | [diff] [blame] | 1574 | sqlite3RollbackAll(db); |
danielk1977 | 07cb560 | 2006-01-20 10:55:05 +0000 | [diff] [blame] | 1575 | db->autoCommit = 1; |
| 1576 | } |
danielk1977 | 261919c | 2005-12-06 12:52:59 +0000 | [diff] [blame] | 1577 | } |
| 1578 | } |
danielk1977 | 07cb560 | 2006-01-20 10:55:05 +0000 | [diff] [blame] | 1579 | |
| 1580 | /* If the auto-commit flag is set and this is the only active vdbe, then |
| 1581 | ** we do either a commit or rollback of the current transaction. |
| 1582 | ** |
| 1583 | ** Note: This block also runs if one of the special errors handled |
| 1584 | ** above has occured. |
| 1585 | */ |
| 1586 | if( db->autoCommit && db->activeVdbeCnt==1 ){ |
| 1587 | if( p->rc==SQLITE_OK || (p->errorAction==OE_Fail && !isSpecialError) ){ |
drh | fd131da | 2007-08-07 17:13:03 +0000 | [diff] [blame] | 1588 | /* The auto-commit flag is true, and the vdbe program was |
danielk1977 | 07cb560 | 2006-01-20 10:55:05 +0000 | [diff] [blame] | 1589 | ** successful or hit an 'OR FAIL' constraint. This means a commit |
| 1590 | ** is required. |
| 1591 | */ |
| 1592 | int rc = vdbeCommit(db); |
| 1593 | if( rc==SQLITE_BUSY ){ |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 1594 | sqlite3BtreeMutexArrayLeave(&p->aMutex); |
danielk1977 | 07cb560 | 2006-01-20 10:55:05 +0000 | [diff] [blame] | 1595 | return SQLITE_BUSY; |
| 1596 | }else if( rc!=SQLITE_OK ){ |
| 1597 | p->rc = rc; |
danielk1977 | 97a227c | 2006-01-20 16:32:04 +0000 | [diff] [blame] | 1598 | sqlite3RollbackAll(db); |
danielk1977 | 07cb560 | 2006-01-20 10:55:05 +0000 | [diff] [blame] | 1599 | }else{ |
| 1600 | sqlite3CommitInternalChanges(db); |
| 1601 | } |
| 1602 | }else{ |
danielk1977 | 97a227c | 2006-01-20 16:32:04 +0000 | [diff] [blame] | 1603 | sqlite3RollbackAll(db); |
danielk1977 | 07cb560 | 2006-01-20 10:55:05 +0000 | [diff] [blame] | 1604 | } |
| 1605 | }else if( !xFunc ){ |
| 1606 | if( p->rc==SQLITE_OK || p->errorAction==OE_Fail ){ |
danielk1977 | 182c4ba | 2007-06-27 15:53:34 +0000 | [diff] [blame] | 1607 | if( p->openedStatement ){ |
| 1608 | xFunc = sqlite3BtreeCommitStmt; |
| 1609 | } |
danielk1977 | 07cb560 | 2006-01-20 10:55:05 +0000 | [diff] [blame] | 1610 | }else if( p->errorAction==OE_Abort ){ |
| 1611 | xFunc = sqlite3BtreeRollbackStmt; |
| 1612 | }else{ |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 1613 | invalidateCursorsOnModifiedBtrees(db); |
danielk1977 | 97a227c | 2006-01-20 16:32:04 +0000 | [diff] [blame] | 1614 | sqlite3RollbackAll(db); |
danielk1977 | 07cb560 | 2006-01-20 10:55:05 +0000 | [diff] [blame] | 1615 | db->autoCommit = 1; |
| 1616 | } |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 1617 | } |
danielk1977 | 07cb560 | 2006-01-20 10:55:05 +0000 | [diff] [blame] | 1618 | |
| 1619 | /* If xFunc is not NULL, then it is one of sqlite3BtreeRollbackStmt or |
| 1620 | ** sqlite3BtreeCommitStmt. Call it once on each backend. If an error occurs |
| 1621 | ** and the return code is still SQLITE_OK, set the return code to the new |
| 1622 | ** error value. |
| 1623 | */ |
| 1624 | assert(!xFunc || |
| 1625 | xFunc==sqlite3BtreeCommitStmt || |
| 1626 | xFunc==sqlite3BtreeRollbackStmt |
| 1627 | ); |
| 1628 | for(i=0; xFunc && i<db->nDb; i++){ |
| 1629 | int rc; |
| 1630 | Btree *pBt = db->aDb[i].pBt; |
| 1631 | if( pBt ){ |
| 1632 | rc = xFunc(pBt); |
danielk1977 | 8a7aea3 | 2006-01-23 15:25:48 +0000 | [diff] [blame] | 1633 | if( rc && (p->rc==SQLITE_OK || p->rc==SQLITE_CONSTRAINT) ){ |
| 1634 | p->rc = rc; |
| 1635 | sqlite3SetString(&p->zErrMsg, 0); |
| 1636 | } |
danielk1977 | 07cb560 | 2006-01-20 10:55:05 +0000 | [diff] [blame] | 1637 | } |
danielk1977 | 77d83ba | 2004-05-31 10:08:14 +0000 | [diff] [blame] | 1638 | } |
danielk1977 | 07cb560 | 2006-01-20 10:55:05 +0000 | [diff] [blame] | 1639 | |
| 1640 | /* If this was an INSERT, UPDATE or DELETE and the statement was committed, |
| 1641 | ** set the change counter. |
| 1642 | */ |
| 1643 | if( p->changeCntOn && p->pc>=0 ){ |
| 1644 | if( !xFunc || xFunc==sqlite3BtreeCommitStmt ){ |
| 1645 | sqlite3VdbeSetChanges(db, p->nChange); |
| 1646 | }else{ |
| 1647 | sqlite3VdbeSetChanges(db, 0); |
| 1648 | } |
| 1649 | p->nChange = 0; |
danielk1977 | b28af71 | 2004-06-21 06:50:26 +0000 | [diff] [blame] | 1650 | } |
danielk1977 | 07cb560 | 2006-01-20 10:55:05 +0000 | [diff] [blame] | 1651 | |
| 1652 | /* Rollback or commit any schema changes that occurred. */ |
| 1653 | if( p->rc!=SQLITE_OK && db->flags&SQLITE_InternChanges ){ |
| 1654 | sqlite3ResetInternalSchema(db, 0); |
| 1655 | db->flags = (db->flags | SQLITE_InternChanges); |
| 1656 | } |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 1657 | |
| 1658 | /* Release the locks */ |
| 1659 | sqlite3BtreeMutexArrayLeave(&p->aMutex); |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 1660 | } |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 1661 | |
danielk1977 | 65fd59f | 2006-06-24 11:51:33 +0000 | [diff] [blame] | 1662 | /* We have successfully halted and closed the VM. Record this fact. */ |
| 1663 | if( p->pc>=0 ){ |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 1664 | db->activeVdbeCnt--; |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 1665 | } |
drh | 92f02c3 | 2004-09-02 14:57:08 +0000 | [diff] [blame] | 1666 | p->magic = VDBE_MAGIC_HALT; |
| 1667 | checkActiveVdbeCnt(db); |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 1668 | if( p->db->mallocFailed ){ |
| 1669 | p->rc = SQLITE_NOMEM; |
| 1670 | } |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 1671 | |
drh | 92f02c3 | 2004-09-02 14:57:08 +0000 | [diff] [blame] | 1672 | return SQLITE_OK; |
| 1673 | } |
drh | 4cf7c7f | 2007-08-28 23:28:07 +0000 | [diff] [blame] | 1674 | |
drh | 92f02c3 | 2004-09-02 14:57:08 +0000 | [diff] [blame] | 1675 | |
| 1676 | /* |
drh | 3c23a88 | 2007-01-09 14:01:13 +0000 | [diff] [blame] | 1677 | ** Each VDBE holds the result of the most recent sqlite3_step() call |
| 1678 | ** in p->rc. This routine sets that result back to SQLITE_OK. |
| 1679 | */ |
| 1680 | void sqlite3VdbeResetStepResult(Vdbe *p){ |
| 1681 | p->rc = SQLITE_OK; |
| 1682 | } |
| 1683 | |
| 1684 | /* |
drh | 92f02c3 | 2004-09-02 14:57:08 +0000 | [diff] [blame] | 1685 | ** Clean up a VDBE after execution but do not delete the VDBE just yet. |
| 1686 | ** Write any error messages into *pzErrMsg. Return the result code. |
| 1687 | ** |
| 1688 | ** After this routine is run, the VDBE should be ready to be executed |
| 1689 | ** again. |
| 1690 | ** |
| 1691 | ** To look at it another way, this routine resets the state of the |
| 1692 | ** virtual machine from VDBE_MAGIC_RUN or VDBE_MAGIC_HALT back to |
| 1693 | ** VDBE_MAGIC_INIT. |
| 1694 | */ |
danielk1977 | dfb316d | 2008-03-26 18:34:43 +0000 | [diff] [blame] | 1695 | int sqlite3VdbeReset(Vdbe *p, int freebuffers){ |
drh | 4ac285a | 2006-09-15 07:28:50 +0000 | [diff] [blame] | 1696 | sqlite3 *db; |
drh | 4ac285a | 2006-09-15 07:28:50 +0000 | [diff] [blame] | 1697 | db = p->db; |
drh | 92f02c3 | 2004-09-02 14:57:08 +0000 | [diff] [blame] | 1698 | |
| 1699 | /* If the VM did not run to completion or if it encountered an |
| 1700 | ** error, then it might not have been halted properly. So halt |
| 1701 | ** it now. |
| 1702 | */ |
drh | 7e8b848 | 2008-01-23 03:03:05 +0000 | [diff] [blame] | 1703 | (void)sqlite3SafetyOn(db); |
drh | 92f02c3 | 2004-09-02 14:57:08 +0000 | [diff] [blame] | 1704 | sqlite3VdbeHalt(p); |
drh | 7e8b848 | 2008-01-23 03:03:05 +0000 | [diff] [blame] | 1705 | (void)sqlite3SafetyOff(db); |
drh | 92f02c3 | 2004-09-02 14:57:08 +0000 | [diff] [blame] | 1706 | |
drh | fb7e765 | 2005-01-24 00:28:42 +0000 | [diff] [blame] | 1707 | /* If the VDBE has be run even partially, then transfer the error code |
| 1708 | ** and error message from the VDBE into the main database structure. But |
| 1709 | ** if the VDBE has just been set to run but has not actually executed any |
| 1710 | ** instructions yet, leave the main database error information unchanged. |
drh | 92f02c3 | 2004-09-02 14:57:08 +0000 | [diff] [blame] | 1711 | */ |
drh | fb7e765 | 2005-01-24 00:28:42 +0000 | [diff] [blame] | 1712 | if( p->pc>=0 ){ |
| 1713 | if( p->zErrMsg ){ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 1714 | sqlite3ValueSetStr(db->pErr,-1,p->zErrMsg,SQLITE_UTF8,sqlite3_free); |
danielk1977 | 97a227c | 2006-01-20 16:32:04 +0000 | [diff] [blame] | 1715 | db->errCode = p->rc; |
drh | fb7e765 | 2005-01-24 00:28:42 +0000 | [diff] [blame] | 1716 | p->zErrMsg = 0; |
| 1717 | }else if( p->rc ){ |
drh | 4ac285a | 2006-09-15 07:28:50 +0000 | [diff] [blame] | 1718 | sqlite3Error(db, p->rc, 0); |
drh | fb7e765 | 2005-01-24 00:28:42 +0000 | [diff] [blame] | 1719 | }else{ |
drh | 4ac285a | 2006-09-15 07:28:50 +0000 | [diff] [blame] | 1720 | sqlite3Error(db, SQLITE_OK, 0); |
drh | fb7e765 | 2005-01-24 00:28:42 +0000 | [diff] [blame] | 1721 | } |
danielk1977 | a21c6b6 | 2005-01-24 10:25:59 +0000 | [diff] [blame] | 1722 | }else if( p->rc && p->expired ){ |
| 1723 | /* The expired flag was set on the VDBE before the first call |
| 1724 | ** to sqlite3_step(). For consistency (since sqlite3_step() was |
| 1725 | ** called), set the database error in this case as well. |
| 1726 | */ |
drh | 4ac285a | 2006-09-15 07:28:50 +0000 | [diff] [blame] | 1727 | sqlite3Error(db, p->rc, 0); |
danielk1977 | 8e55652 | 2007-11-13 10:30:24 +0000 | [diff] [blame] | 1728 | sqlite3ValueSetStr(db->pErr, -1, p->zErrMsg, SQLITE_UTF8, sqlite3_free); |
| 1729 | p->zErrMsg = 0; |
drh | 92f02c3 | 2004-09-02 14:57:08 +0000 | [diff] [blame] | 1730 | } |
| 1731 | |
| 1732 | /* Reclaim all memory used by the VDBE |
| 1733 | */ |
danielk1977 | dfb316d | 2008-03-26 18:34:43 +0000 | [diff] [blame] | 1734 | Cleanup(p, freebuffers); |
drh | 92f02c3 | 2004-09-02 14:57:08 +0000 | [diff] [blame] | 1735 | |
| 1736 | /* Save profiling information from this VDBE run. |
| 1737 | */ |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 1738 | #ifdef VDBE_PROFILE |
| 1739 | { |
| 1740 | FILE *out = fopen("vdbe_profile.out", "a"); |
| 1741 | if( out ){ |
| 1742 | int i; |
| 1743 | fprintf(out, "---- "); |
| 1744 | for(i=0; i<p->nOp; i++){ |
| 1745 | fprintf(out, "%02x", p->aOp[i].opcode); |
| 1746 | } |
| 1747 | fprintf(out, "\n"); |
| 1748 | for(i=0; i<p->nOp; i++){ |
| 1749 | fprintf(out, "%6d %10lld %8lld ", |
| 1750 | p->aOp[i].cnt, |
| 1751 | p->aOp[i].cycles, |
| 1752 | p->aOp[i].cnt>0 ? p->aOp[i].cycles/p->aOp[i].cnt : 0 |
| 1753 | ); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1754 | sqlite3VdbePrintOp(out, i, &p->aOp[i]); |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 1755 | } |
| 1756 | fclose(out); |
| 1757 | } |
| 1758 | } |
| 1759 | #endif |
| 1760 | p->magic = VDBE_MAGIC_INIT; |
drh | 91b48aa | 2004-06-30 11:14:18 +0000 | [diff] [blame] | 1761 | p->aborted = 0; |
drh | 4ac285a | 2006-09-15 07:28:50 +0000 | [diff] [blame] | 1762 | return p->rc & db->errMask; |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 1763 | } |
drh | 92f02c3 | 2004-09-02 14:57:08 +0000 | [diff] [blame] | 1764 | |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 1765 | /* |
| 1766 | ** Clean up and delete a VDBE after execution. Return an integer which is |
| 1767 | ** the result code. Write any error message text into *pzErrMsg. |
| 1768 | */ |
danielk1977 | 9e6db7d | 2004-06-21 08:18:51 +0000 | [diff] [blame] | 1769 | int sqlite3VdbeFinalize(Vdbe *p){ |
danielk1977 | b5548a8 | 2004-06-26 13:51:33 +0000 | [diff] [blame] | 1770 | int rc = SQLITE_OK; |
danielk1977 | b5548a8 | 2004-06-26 13:51:33 +0000 | [diff] [blame] | 1771 | if( p->magic==VDBE_MAGIC_RUN || p->magic==VDBE_MAGIC_HALT ){ |
danielk1977 | dfb316d | 2008-03-26 18:34:43 +0000 | [diff] [blame] | 1772 | rc = sqlite3VdbeReset(p, 1); |
drh | 4ac285a | 2006-09-15 07:28:50 +0000 | [diff] [blame] | 1773 | assert( (rc & p->db->errMask)==rc ); |
danielk1977 | b5548a8 | 2004-06-26 13:51:33 +0000 | [diff] [blame] | 1774 | }else if( p->magic!=VDBE_MAGIC_INIT ){ |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 1775 | return SQLITE_MISUSE; |
| 1776 | } |
danielk1977 | dfb316d | 2008-03-26 18:34:43 +0000 | [diff] [blame] | 1777 | releaseMemArray(&p->aMem[1], p->nMem, 1); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1778 | sqlite3VdbeDelete(p); |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 1779 | return rc; |
| 1780 | } |
| 1781 | |
| 1782 | /* |
drh | f92c7ff | 2004-06-19 15:40:23 +0000 | [diff] [blame] | 1783 | ** Call the destructor for each auxdata entry in pVdbeFunc for which |
danielk1977 | e159fdf | 2004-06-21 10:45:06 +0000 | [diff] [blame] | 1784 | ** the corresponding bit in mask is clear. Auxdata entries beyond 31 |
drh | f92c7ff | 2004-06-19 15:40:23 +0000 | [diff] [blame] | 1785 | ** are always destroyed. To destroy all auxdata entries, call this |
danielk1977 | e159fdf | 2004-06-21 10:45:06 +0000 | [diff] [blame] | 1786 | ** routine with mask==0. |
drh | f92c7ff | 2004-06-19 15:40:23 +0000 | [diff] [blame] | 1787 | */ |
| 1788 | void sqlite3VdbeDeleteAuxData(VdbeFunc *pVdbeFunc, int mask){ |
| 1789 | int i; |
| 1790 | for(i=0; i<pVdbeFunc->nAux; i++){ |
| 1791 | struct AuxData *pAux = &pVdbeFunc->apAux[i]; |
| 1792 | if( (i>31 || !(mask&(1<<i))) && pAux->pAux ){ |
| 1793 | if( pAux->xDelete ){ |
| 1794 | pAux->xDelete(pAux->pAux); |
| 1795 | } |
| 1796 | pAux->pAux = 0; |
| 1797 | } |
| 1798 | } |
| 1799 | } |
| 1800 | |
| 1801 | /* |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 1802 | ** Delete an entire VDBE. |
| 1803 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1804 | void sqlite3VdbeDelete(Vdbe *p){ |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 1805 | int i; |
| 1806 | if( p==0 ) return; |
danielk1977 | dfb316d | 2008-03-26 18:34:43 +0000 | [diff] [blame] | 1807 | Cleanup(p, 1); |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 1808 | if( p->pPrev ){ |
| 1809 | p->pPrev->pNext = p->pNext; |
| 1810 | }else{ |
| 1811 | assert( p->db->pVdbe==p ); |
| 1812 | p->db->pVdbe = p->pNext; |
| 1813 | } |
| 1814 | if( p->pNext ){ |
| 1815 | p->pNext->pPrev = p->pPrev; |
| 1816 | } |
drh | 76ff3a0 | 2004-09-24 22:32:30 +0000 | [diff] [blame] | 1817 | if( p->aOp ){ |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 1818 | Op *pOp = p->aOp; |
| 1819 | for(i=0; i<p->nOp; i++, pOp++){ |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 1820 | freeP4(pOp->p4type, pOp->p4.p); |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 1821 | #ifdef SQLITE_DEBUG |
| 1822 | sqlite3_free(pOp->zComment); |
| 1823 | #endif |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 1824 | } |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 1825 | sqlite3_free(p->aOp); |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 1826 | } |
danielk1977 | dfb316d | 2008-03-26 18:34:43 +0000 | [diff] [blame] | 1827 | releaseMemArray(p->aVar, p->nVar, 1); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 1828 | sqlite3_free(p->aLabel); |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1829 | if( p->aMem ){ |
| 1830 | sqlite3_free(&p->aMem[1]); |
| 1831 | } |
danielk1977 | dfb316d | 2008-03-26 18:34:43 +0000 | [diff] [blame] | 1832 | releaseMemArray(p->aColName, p->nResColumn*COLNAME_N, 1); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 1833 | sqlite3_free(p->aColName); |
| 1834 | sqlite3_free(p->zSql); |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 1835 | p->magic = VDBE_MAGIC_DEAD; |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 1836 | sqlite3_free(p); |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 1837 | } |
drh | a11846b | 2004-01-07 18:52:56 +0000 | [diff] [blame] | 1838 | |
| 1839 | /* |
drh | a11846b | 2004-01-07 18:52:56 +0000 | [diff] [blame] | 1840 | ** If a MoveTo operation is pending on the given cursor, then do that |
| 1841 | ** MoveTo now. Return an error code. If no MoveTo is pending, this |
| 1842 | ** routine does nothing and returns SQLITE_OK. |
| 1843 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1844 | int sqlite3VdbeCursorMoveto(Cursor *p){ |
drh | a11846b | 2004-01-07 18:52:56 +0000 | [diff] [blame] | 1845 | if( p->deferredMoveto ){ |
drh | 536065a | 2005-01-26 21:55:31 +0000 | [diff] [blame] | 1846 | int res, rc; |
adamd | 4fc9308 | 2006-09-14 16:57:19 +0000 | [diff] [blame] | 1847 | #ifdef SQLITE_TEST |
danielk1977 | 132872b | 2004-05-10 10:37:18 +0000 | [diff] [blame] | 1848 | extern int sqlite3_search_count; |
adamd | 4fc9308 | 2006-09-14 16:57:19 +0000 | [diff] [blame] | 1849 | #endif |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 1850 | assert( p->isTable ); |
drh | e14006d | 2008-03-25 17:23:32 +0000 | [diff] [blame] | 1851 | rc = sqlite3BtreeMoveto(p->pCursor, 0, 0, p->movetoTarget, 0, &res); |
drh | 536065a | 2005-01-26 21:55:31 +0000 | [diff] [blame] | 1852 | if( rc ) return rc; |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 1853 | *p->pIncrKey = 0; |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 1854 | p->lastRowid = keyToInt(p->movetoTarget); |
| 1855 | p->rowidIsValid = res==0; |
drh | a11846b | 2004-01-07 18:52:56 +0000 | [diff] [blame] | 1856 | if( res<0 ){ |
drh | 536065a | 2005-01-26 21:55:31 +0000 | [diff] [blame] | 1857 | rc = sqlite3BtreeNext(p->pCursor, &res); |
| 1858 | if( rc ) return rc; |
drh | a11846b | 2004-01-07 18:52:56 +0000 | [diff] [blame] | 1859 | } |
drh | 10cfdd5 | 2006-08-08 15:42:59 +0000 | [diff] [blame] | 1860 | #ifdef SQLITE_TEST |
danielk1977 | 132872b | 2004-05-10 10:37:18 +0000 | [diff] [blame] | 1861 | sqlite3_search_count++; |
drh | 10cfdd5 | 2006-08-08 15:42:59 +0000 | [diff] [blame] | 1862 | #endif |
drh | a11846b | 2004-01-07 18:52:56 +0000 | [diff] [blame] | 1863 | p->deferredMoveto = 0; |
drh | 76873ab | 2006-01-07 18:48:26 +0000 | [diff] [blame] | 1864 | p->cacheStatus = CACHE_STALE; |
drh | a11846b | 2004-01-07 18:52:56 +0000 | [diff] [blame] | 1865 | } |
| 1866 | return SQLITE_OK; |
| 1867 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1868 | |
drh | ab9f7f1 | 2004-05-08 10:56:11 +0000 | [diff] [blame] | 1869 | /* |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1870 | ** The following functions: |
danielk1977 | 90e4d95 | 2004-05-10 10:05:53 +0000 | [diff] [blame] | 1871 | ** |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1872 | ** sqlite3VdbeSerialType() |
| 1873 | ** sqlite3VdbeSerialTypeLen() |
| 1874 | ** sqlite3VdbeSerialRead() |
danielk1977 | 90e4d95 | 2004-05-10 10:05:53 +0000 | [diff] [blame] | 1875 | ** sqlite3VdbeSerialLen() |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1876 | ** sqlite3VdbeSerialWrite() |
danielk1977 | 90e4d95 | 2004-05-10 10:05:53 +0000 | [diff] [blame] | 1877 | ** |
| 1878 | ** encapsulate the code that serializes values for storage in SQLite |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1879 | ** data and index records. Each serialized value consists of a |
| 1880 | ** 'serial-type' and a blob of data. The serial type is an 8-byte unsigned |
| 1881 | ** integer, stored as a varint. |
danielk1977 | 90e4d95 | 2004-05-10 10:05:53 +0000 | [diff] [blame] | 1882 | ** |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1883 | ** In an SQLite index record, the serial type is stored directly before |
| 1884 | ** the blob of data that it corresponds to. In a table record, all serial |
| 1885 | ** types are stored at the start of the record, and the blobs of data at |
| 1886 | ** the end. Hence these functions allow the caller to handle the |
| 1887 | ** serial-type and data blob seperately. |
| 1888 | ** |
| 1889 | ** The following table describes the various storage classes for data: |
| 1890 | ** |
| 1891 | ** serial type bytes of data type |
danielk1977 | 90e4d95 | 2004-05-10 10:05:53 +0000 | [diff] [blame] | 1892 | ** -------------- --------------- --------------- |
drh | a19b775 | 2004-05-30 21:14:58 +0000 | [diff] [blame] | 1893 | ** 0 0 NULL |
danielk1977 | 90e4d95 | 2004-05-10 10:05:53 +0000 | [diff] [blame] | 1894 | ** 1 1 signed integer |
| 1895 | ** 2 2 signed integer |
drh | a19b775 | 2004-05-30 21:14:58 +0000 | [diff] [blame] | 1896 | ** 3 3 signed integer |
| 1897 | ** 4 4 signed integer |
| 1898 | ** 5 6 signed integer |
| 1899 | ** 6 8 signed integer |
| 1900 | ** 7 8 IEEE float |
drh | d946db0 | 2005-12-29 19:23:06 +0000 | [diff] [blame] | 1901 | ** 8 0 Integer constant 0 |
| 1902 | ** 9 0 Integer constant 1 |
| 1903 | ** 10,11 reserved for expansion |
danielk1977 | 90e4d95 | 2004-05-10 10:05:53 +0000 | [diff] [blame] | 1904 | ** N>=12 and even (N-12)/2 BLOB |
| 1905 | ** N>=13 and odd (N-13)/2 text |
| 1906 | ** |
drh | 35a5965 | 2006-01-02 18:24:40 +0000 | [diff] [blame] | 1907 | ** The 8 and 9 types were added in 3.3.0, file format 4. Prior versions |
| 1908 | ** of SQLite will not understand those serial types. |
danielk1977 | 90e4d95 | 2004-05-10 10:05:53 +0000 | [diff] [blame] | 1909 | */ |
| 1910 | |
| 1911 | /* |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1912 | ** Return the serial-type for the value stored in pMem. |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 1913 | */ |
drh | d946db0 | 2005-12-29 19:23:06 +0000 | [diff] [blame] | 1914 | u32 sqlite3VdbeSerialType(Mem *pMem, int file_format){ |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1915 | int flags = pMem->flags; |
drh | fdf972a | 2007-05-02 13:30:27 +0000 | [diff] [blame] | 1916 | int n; |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1917 | |
| 1918 | if( flags&MEM_Null ){ |
drh | a19b775 | 2004-05-30 21:14:58 +0000 | [diff] [blame] | 1919 | return 0; |
danielk1977 | 90e4d95 | 2004-05-10 10:05:53 +0000 | [diff] [blame] | 1920 | } |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1921 | if( flags&MEM_Int ){ |
drh | fe2093d | 2005-01-20 22:48:47 +0000 | [diff] [blame] | 1922 | /* Figure out whether to use 1, 2, 4, 6 or 8 bytes. */ |
drh | 5284a05 | 2008-05-08 15:18:10 +0000 | [diff] [blame] | 1923 | # define MAX_6BYTE ((((i64)0x00008000)<<32)-1) |
drh | 3c024d6 | 2007-03-30 11:23:45 +0000 | [diff] [blame] | 1924 | i64 i = pMem->u.i; |
drh | d946db0 | 2005-12-29 19:23:06 +0000 | [diff] [blame] | 1925 | u64 u; |
| 1926 | if( file_format>=4 && (i&1)==i ){ |
| 1927 | return 8+i; |
| 1928 | } |
| 1929 | u = i<0 ? -i : i; |
drh | 5742b63 | 2005-01-26 17:47:02 +0000 | [diff] [blame] | 1930 | if( u<=127 ) return 1; |
| 1931 | if( u<=32767 ) return 2; |
| 1932 | if( u<=8388607 ) return 3; |
| 1933 | if( u<=2147483647 ) return 4; |
| 1934 | if( u<=MAX_6BYTE ) return 5; |
drh | a19b775 | 2004-05-30 21:14:58 +0000 | [diff] [blame] | 1935 | return 6; |
danielk1977 | 90e4d95 | 2004-05-10 10:05:53 +0000 | [diff] [blame] | 1936 | } |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1937 | if( flags&MEM_Real ){ |
drh | a19b775 | 2004-05-30 21:14:58 +0000 | [diff] [blame] | 1938 | return 7; |
danielk1977 | 90e4d95 | 2004-05-10 10:05:53 +0000 | [diff] [blame] | 1939 | } |
drh | fdf972a | 2007-05-02 13:30:27 +0000 | [diff] [blame] | 1940 | assert( flags&(MEM_Str|MEM_Blob) ); |
| 1941 | n = pMem->n; |
| 1942 | if( flags & MEM_Zero ){ |
| 1943 | n += pMem->u.i; |
danielk1977 | 90e4d95 | 2004-05-10 10:05:53 +0000 | [diff] [blame] | 1944 | } |
drh | fdf972a | 2007-05-02 13:30:27 +0000 | [diff] [blame] | 1945 | assert( n>=0 ); |
| 1946 | return ((n*2) + 12 + ((flags&MEM_Str)!=0)); |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 1947 | } |
| 1948 | |
| 1949 | /* |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 1950 | ** Return the length of the data corresponding to the supplied serial-type. |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 1951 | */ |
drh | 25aa1b4 | 2004-05-28 01:39:01 +0000 | [diff] [blame] | 1952 | int sqlite3VdbeSerialTypeLen(u32 serial_type){ |
drh | a19b775 | 2004-05-30 21:14:58 +0000 | [diff] [blame] | 1953 | if( serial_type>=12 ){ |
drh | 51846b5 | 2004-05-28 16:00:21 +0000 | [diff] [blame] | 1954 | return (serial_type-12)/2; |
| 1955 | }else{ |
drh | 5719628 | 2004-10-06 15:41:16 +0000 | [diff] [blame] | 1956 | static const u8 aSize[] = { 0, 1, 2, 3, 4, 6, 8, 8, 0, 0, 0, 0 }; |
drh | 51846b5 | 2004-05-28 16:00:21 +0000 | [diff] [blame] | 1957 | return aSize[serial_type]; |
| 1958 | } |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 1959 | } |
| 1960 | |
| 1961 | /* |
drh | 110daac | 2007-05-04 11:59:31 +0000 | [diff] [blame] | 1962 | ** If we are on an architecture with mixed-endian floating |
drh | 7a4f502 | 2007-05-23 07:20:08 +0000 | [diff] [blame] | 1963 | ** points (ex: ARM7) then swap the lower 4 bytes with the |
drh | 110daac | 2007-05-04 11:59:31 +0000 | [diff] [blame] | 1964 | ** upper 4 bytes. Return the result. |
| 1965 | ** |
drh | 7a4f502 | 2007-05-23 07:20:08 +0000 | [diff] [blame] | 1966 | ** For most architectures, this is a no-op. |
| 1967 | ** |
| 1968 | ** (later): It is reported to me that the mixed-endian problem |
| 1969 | ** on ARM7 is an issue with GCC, not with the ARM7 chip. It seems |
| 1970 | ** that early versions of GCC stored the two words of a 64-bit |
| 1971 | ** float in the wrong order. And that error has been propagated |
| 1972 | ** ever since. The blame is not necessarily with GCC, though. |
| 1973 | ** GCC might have just copying the problem from a prior compiler. |
| 1974 | ** I am also told that newer versions of GCC that follow a different |
| 1975 | ** ABI get the byte order right. |
| 1976 | ** |
| 1977 | ** Developers using SQLite on an ARM7 should compile and run their |
| 1978 | ** application using -DSQLITE_DEBUG=1 at least once. With DEBUG |
| 1979 | ** enabled, some asserts below will ensure that the byte order of |
| 1980 | ** floating point values is correct. |
drh | 60d09a7 | 2007-08-30 15:05:08 +0000 | [diff] [blame] | 1981 | ** |
| 1982 | ** (2007-08-30) Frank van Vugt has studied this problem closely |
| 1983 | ** and has send his findings to the SQLite developers. Frank |
| 1984 | ** writes that some Linux kernels offer floating point hardware |
| 1985 | ** emulation that uses only 32-bit mantissas instead of a full |
| 1986 | ** 48-bits as required by the IEEE standard. (This is the |
| 1987 | ** CONFIG_FPE_FASTFPE option.) On such systems, floating point |
| 1988 | ** byte swapping becomes very complicated. To avoid problems, |
| 1989 | ** the necessary byte swapping is carried out using a 64-bit integer |
| 1990 | ** rather than a 64-bit float. Frank assures us that the code here |
| 1991 | ** works for him. We, the developers, have no way to independently |
| 1992 | ** verify this, but Frank seems to know what he is talking about |
| 1993 | ** so we trust him. |
drh | 110daac | 2007-05-04 11:59:31 +0000 | [diff] [blame] | 1994 | */ |
| 1995 | #ifdef SQLITE_MIXED_ENDIAN_64BIT_FLOAT |
drh | 60d09a7 | 2007-08-30 15:05:08 +0000 | [diff] [blame] | 1996 | static u64 floatSwap(u64 in){ |
drh | 110daac | 2007-05-04 11:59:31 +0000 | [diff] [blame] | 1997 | union { |
drh | 60d09a7 | 2007-08-30 15:05:08 +0000 | [diff] [blame] | 1998 | u64 r; |
drh | 110daac | 2007-05-04 11:59:31 +0000 | [diff] [blame] | 1999 | u32 i[2]; |
| 2000 | } u; |
| 2001 | u32 t; |
| 2002 | |
| 2003 | u.r = in; |
| 2004 | t = u.i[0]; |
| 2005 | u.i[0] = u.i[1]; |
| 2006 | u.i[1] = t; |
| 2007 | return u.r; |
| 2008 | } |
| 2009 | # define swapMixedEndianFloat(X) X = floatSwap(X) |
| 2010 | #else |
| 2011 | # define swapMixedEndianFloat(X) |
| 2012 | #endif |
| 2013 | |
| 2014 | /* |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 2015 | ** Write the serialized data blob for the value stored in pMem into |
| 2016 | ** buf. It is assumed that the caller has allocated sufficient space. |
| 2017 | ** Return the number of bytes written. |
drh | fdf972a | 2007-05-02 13:30:27 +0000 | [diff] [blame] | 2018 | ** |
| 2019 | ** nBuf is the amount of space left in buf[]. nBuf must always be |
| 2020 | ** large enough to hold the entire field. Except, if the field is |
| 2021 | ** a blob with a zero-filled tail, then buf[] might be just the right |
| 2022 | ** size to hold everything except for the zero-filled tail. If buf[] |
| 2023 | ** is only big enough to hold the non-zero prefix, then only write that |
| 2024 | ** prefix into buf[]. But if buf[] is large enough to hold both the |
| 2025 | ** prefix and the tail then write the prefix and set the tail to all |
| 2026 | ** zeros. |
| 2027 | ** |
| 2028 | ** Return the number of bytes actually written into buf[]. The number |
| 2029 | ** of bytes in the zero-filled tail is included in the return value only |
| 2030 | ** if those bytes were zeroed in buf[]. |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 2031 | */ |
drh | fdf972a | 2007-05-02 13:30:27 +0000 | [diff] [blame] | 2032 | int sqlite3VdbeSerialPut(u8 *buf, int nBuf, Mem *pMem, int file_format){ |
drh | d946db0 | 2005-12-29 19:23:06 +0000 | [diff] [blame] | 2033 | u32 serial_type = sqlite3VdbeSerialType(pMem, file_format); |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 2034 | int len; |
danielk1977 | 183f9f7 | 2004-05-13 05:20:26 +0000 | [diff] [blame] | 2035 | |
drh | 1483e14 | 2004-05-21 21:12:42 +0000 | [diff] [blame] | 2036 | /* Integer and Real */ |
drh | d946db0 | 2005-12-29 19:23:06 +0000 | [diff] [blame] | 2037 | if( serial_type<=7 && serial_type>0 ){ |
drh | 1483e14 | 2004-05-21 21:12:42 +0000 | [diff] [blame] | 2038 | u64 v; |
| 2039 | int i; |
drh | a19b775 | 2004-05-30 21:14:58 +0000 | [diff] [blame] | 2040 | if( serial_type==7 ){ |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 2041 | assert( sizeof(v)==sizeof(pMem->r) ); |
| 2042 | memcpy(&v, &pMem->r, sizeof(v)); |
drh | 60d09a7 | 2007-08-30 15:05:08 +0000 | [diff] [blame] | 2043 | swapMixedEndianFloat(v); |
drh | 1483e14 | 2004-05-21 21:12:42 +0000 | [diff] [blame] | 2044 | }else{ |
drh | 3c024d6 | 2007-03-30 11:23:45 +0000 | [diff] [blame] | 2045 | v = pMem->u.i; |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 2046 | } |
drh | 1483e14 | 2004-05-21 21:12:42 +0000 | [diff] [blame] | 2047 | len = i = sqlite3VdbeSerialTypeLen(serial_type); |
drh | fdf972a | 2007-05-02 13:30:27 +0000 | [diff] [blame] | 2048 | assert( len<=nBuf ); |
drh | 1483e14 | 2004-05-21 21:12:42 +0000 | [diff] [blame] | 2049 | while( i-- ){ |
| 2050 | buf[i] = (v&0xFF); |
| 2051 | v >>= 8; |
| 2052 | } |
| 2053 | return len; |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 2054 | } |
drh | d946db0 | 2005-12-29 19:23:06 +0000 | [diff] [blame] | 2055 | |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 2056 | /* String or blob */ |
drh | d946db0 | 2005-12-29 19:23:06 +0000 | [diff] [blame] | 2057 | if( serial_type>=12 ){ |
drh | fdf972a | 2007-05-02 13:30:27 +0000 | [diff] [blame] | 2058 | assert( pMem->n + ((pMem->flags & MEM_Zero)?pMem->u.i:0) |
| 2059 | == sqlite3VdbeSerialTypeLen(serial_type) ); |
| 2060 | assert( pMem->n<=nBuf ); |
| 2061 | len = pMem->n; |
drh | d946db0 | 2005-12-29 19:23:06 +0000 | [diff] [blame] | 2062 | memcpy(buf, pMem->z, len); |
drh | fdf972a | 2007-05-02 13:30:27 +0000 | [diff] [blame] | 2063 | if( pMem->flags & MEM_Zero ){ |
| 2064 | len += pMem->u.i; |
| 2065 | if( len>nBuf ){ |
| 2066 | len = nBuf; |
| 2067 | } |
| 2068 | memset(&buf[pMem->n], 0, len-pMem->n); |
| 2069 | } |
drh | d946db0 | 2005-12-29 19:23:06 +0000 | [diff] [blame] | 2070 | return len; |
| 2071 | } |
| 2072 | |
| 2073 | /* NULL or constants 0 or 1 */ |
| 2074 | return 0; |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 2075 | } |
| 2076 | |
| 2077 | /* |
| 2078 | ** Deserialize the data blob pointed to by buf as serial type serial_type |
| 2079 | ** and store the result in pMem. Return the number of bytes read. |
| 2080 | */ |
danielk1977 | b1bc953 | 2004-05-22 03:05:33 +0000 | [diff] [blame] | 2081 | int sqlite3VdbeSerialGet( |
danielk1977 | 93d4675 | 2004-05-23 13:30:58 +0000 | [diff] [blame] | 2082 | const unsigned char *buf, /* Buffer to deserialize from */ |
drh | 25aa1b4 | 2004-05-28 01:39:01 +0000 | [diff] [blame] | 2083 | u32 serial_type, /* Serial type to deserialize */ |
| 2084 | Mem *pMem /* Memory cell to write value into */ |
danielk1977 | b1bc953 | 2004-05-22 03:05:33 +0000 | [diff] [blame] | 2085 | ){ |
drh | 3c68582 | 2005-05-21 18:32:18 +0000 | [diff] [blame] | 2086 | switch( serial_type ){ |
drh | 3c68582 | 2005-05-21 18:32:18 +0000 | [diff] [blame] | 2087 | case 10: /* Reserved for future use */ |
| 2088 | case 11: /* Reserved for future use */ |
| 2089 | case 0: { /* NULL */ |
| 2090 | pMem->flags = MEM_Null; |
| 2091 | break; |
| 2092 | } |
| 2093 | case 1: { /* 1-byte signed integer */ |
drh | 3c024d6 | 2007-03-30 11:23:45 +0000 | [diff] [blame] | 2094 | pMem->u.i = (signed char)buf[0]; |
drh | 1483e14 | 2004-05-21 21:12:42 +0000 | [diff] [blame] | 2095 | pMem->flags = MEM_Int; |
drh | 3c68582 | 2005-05-21 18:32:18 +0000 | [diff] [blame] | 2096 | return 1; |
drh | 1483e14 | 2004-05-21 21:12:42 +0000 | [diff] [blame] | 2097 | } |
drh | 3c68582 | 2005-05-21 18:32:18 +0000 | [diff] [blame] | 2098 | case 2: { /* 2-byte signed integer */ |
drh | 3c024d6 | 2007-03-30 11:23:45 +0000 | [diff] [blame] | 2099 | pMem->u.i = (((signed char)buf[0])<<8) | buf[1]; |
drh | 3c68582 | 2005-05-21 18:32:18 +0000 | [diff] [blame] | 2100 | pMem->flags = MEM_Int; |
| 2101 | return 2; |
| 2102 | } |
| 2103 | case 3: { /* 3-byte signed integer */ |
drh | 3c024d6 | 2007-03-30 11:23:45 +0000 | [diff] [blame] | 2104 | pMem->u.i = (((signed char)buf[0])<<16) | (buf[1]<<8) | buf[2]; |
drh | 3c68582 | 2005-05-21 18:32:18 +0000 | [diff] [blame] | 2105 | pMem->flags = MEM_Int; |
| 2106 | return 3; |
| 2107 | } |
| 2108 | case 4: { /* 4-byte signed integer */ |
drh | 3c024d6 | 2007-03-30 11:23:45 +0000 | [diff] [blame] | 2109 | pMem->u.i = (buf[0]<<24) | (buf[1]<<16) | (buf[2]<<8) | buf[3]; |
drh | 3c68582 | 2005-05-21 18:32:18 +0000 | [diff] [blame] | 2110 | pMem->flags = MEM_Int; |
| 2111 | return 4; |
| 2112 | } |
| 2113 | case 5: { /* 6-byte signed integer */ |
| 2114 | u64 x = (((signed char)buf[0])<<8) | buf[1]; |
| 2115 | u32 y = (buf[2]<<24) | (buf[3]<<16) | (buf[4]<<8) | buf[5]; |
| 2116 | x = (x<<32) | y; |
drh | 3c024d6 | 2007-03-30 11:23:45 +0000 | [diff] [blame] | 2117 | pMem->u.i = *(i64*)&x; |
drh | 3c68582 | 2005-05-21 18:32:18 +0000 | [diff] [blame] | 2118 | pMem->flags = MEM_Int; |
| 2119 | return 6; |
| 2120 | } |
drh | 91124b3 | 2005-08-18 18:15:05 +0000 | [diff] [blame] | 2121 | case 6: /* 8-byte signed integer */ |
drh | 3c68582 | 2005-05-21 18:32:18 +0000 | [diff] [blame] | 2122 | case 7: { /* IEEE floating point */ |
drh | d81bd4e | 2005-09-05 20:06:49 +0000 | [diff] [blame] | 2123 | u64 x; |
| 2124 | u32 y; |
drh | 2a3e4a7 | 2006-01-23 21:44:53 +0000 | [diff] [blame] | 2125 | #if !defined(NDEBUG) && !defined(SQLITE_OMIT_FLOATING_POINT) |
drh | de941c6 | 2005-08-28 01:34:21 +0000 | [diff] [blame] | 2126 | /* Verify that integers and floating point values use the same |
drh | 110daac | 2007-05-04 11:59:31 +0000 | [diff] [blame] | 2127 | ** byte order. Or, that if SQLITE_MIXED_ENDIAN_64BIT_FLOAT is |
| 2128 | ** defined that 64-bit floating point values really are mixed |
| 2129 | ** endian. |
drh | bfd6b03 | 2005-08-28 01:38:44 +0000 | [diff] [blame] | 2130 | */ |
drh | de941c6 | 2005-08-28 01:34:21 +0000 | [diff] [blame] | 2131 | static const u64 t1 = ((u64)0x3ff00000)<<32; |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 2132 | static const double r1 = 1.0; |
drh | 60d09a7 | 2007-08-30 15:05:08 +0000 | [diff] [blame] | 2133 | u64 t2 = t1; |
| 2134 | swapMixedEndianFloat(t2); |
| 2135 | assert( sizeof(r1)==sizeof(t2) && memcmp(&r1, &t2, sizeof(r1))==0 ); |
drh | de941c6 | 2005-08-28 01:34:21 +0000 | [diff] [blame] | 2136 | #endif |
drh | bfd6b03 | 2005-08-28 01:38:44 +0000 | [diff] [blame] | 2137 | |
drh | d81bd4e | 2005-09-05 20:06:49 +0000 | [diff] [blame] | 2138 | x = (buf[0]<<24) | (buf[1]<<16) | (buf[2]<<8) | buf[3]; |
| 2139 | y = (buf[4]<<24) | (buf[5]<<16) | (buf[6]<<8) | buf[7]; |
drh | 3c68582 | 2005-05-21 18:32:18 +0000 | [diff] [blame] | 2140 | x = (x<<32) | y; |
| 2141 | if( serial_type==6 ){ |
drh | 3c024d6 | 2007-03-30 11:23:45 +0000 | [diff] [blame] | 2142 | pMem->u.i = *(i64*)&x; |
drh | 3c68582 | 2005-05-21 18:32:18 +0000 | [diff] [blame] | 2143 | pMem->flags = MEM_Int; |
| 2144 | }else{ |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 2145 | assert( sizeof(x)==8 && sizeof(pMem->r)==8 ); |
drh | 60d09a7 | 2007-08-30 15:05:08 +0000 | [diff] [blame] | 2146 | swapMixedEndianFloat(x); |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 2147 | memcpy(&pMem->r, &x, sizeof(x)); |
drh | 2eaf93d | 2008-04-29 00:15:20 +0000 | [diff] [blame] | 2148 | pMem->flags = sqlite3IsNaN(pMem->r) ? MEM_Null : MEM_Real; |
drh | 3c68582 | 2005-05-21 18:32:18 +0000 | [diff] [blame] | 2149 | } |
| 2150 | return 8; |
| 2151 | } |
drh | d946db0 | 2005-12-29 19:23:06 +0000 | [diff] [blame] | 2152 | case 8: /* Integer 0 */ |
| 2153 | case 9: { /* Integer 1 */ |
drh | 3c024d6 | 2007-03-30 11:23:45 +0000 | [diff] [blame] | 2154 | pMem->u.i = serial_type-8; |
drh | d946db0 | 2005-12-29 19:23:06 +0000 | [diff] [blame] | 2155 | pMem->flags = MEM_Int; |
| 2156 | return 0; |
| 2157 | } |
drh | 3c68582 | 2005-05-21 18:32:18 +0000 | [diff] [blame] | 2158 | default: { |
| 2159 | int len = (serial_type-12)/2; |
| 2160 | pMem->z = (char *)buf; |
| 2161 | pMem->n = len; |
| 2162 | pMem->xDel = 0; |
| 2163 | if( serial_type&0x01 ){ |
| 2164 | pMem->flags = MEM_Str | MEM_Ephem; |
| 2165 | }else{ |
| 2166 | pMem->flags = MEM_Blob | MEM_Ephem; |
| 2167 | } |
| 2168 | return len; |
drh | 696b32f | 2004-05-30 01:51:52 +0000 | [diff] [blame] | 2169 | } |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 2170 | } |
drh | 3c68582 | 2005-05-21 18:32:18 +0000 | [diff] [blame] | 2171 | return 0; |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2172 | } |
| 2173 | |
drh | 0e6082e | 2006-01-12 20:28:35 +0000 | [diff] [blame] | 2174 | |
drh | 1e968a0 | 2008-03-25 00:22:21 +0000 | [diff] [blame] | 2175 | /* |
| 2176 | ** Given the nKey-byte encoding of a record in pKey[], parse the |
drh | e14006d | 2008-03-25 17:23:32 +0000 | [diff] [blame] | 2177 | ** record into a UnpackedRecord structure. Return a pointer to |
drh | 1e968a0 | 2008-03-25 00:22:21 +0000 | [diff] [blame] | 2178 | ** that structure. |
| 2179 | ** |
| 2180 | ** The calling function might provide szSpace bytes of memory |
| 2181 | ** space at pSpace. This space can be used to hold the returned |
| 2182 | ** VDbeParsedRecord structure if it is large enough. If it is |
| 2183 | ** not big enough, space is obtained from sqlite3_malloc(). |
| 2184 | ** |
| 2185 | ** The returned structure should be closed by a call to |
drh | e14006d | 2008-03-25 17:23:32 +0000 | [diff] [blame] | 2186 | ** sqlite3VdbeDeleteUnpackedRecord(). |
drh | 1e968a0 | 2008-03-25 00:22:21 +0000 | [diff] [blame] | 2187 | */ |
drh | e14006d | 2008-03-25 17:23:32 +0000 | [diff] [blame] | 2188 | UnpackedRecord *sqlite3VdbeRecordUnpack( |
drh | 1e968a0 | 2008-03-25 00:22:21 +0000 | [diff] [blame] | 2189 | KeyInfo *pKeyInfo, /* Information about the record format */ |
| 2190 | int nKey, /* Size of the binary record */ |
| 2191 | const void *pKey, /* The binary record */ |
| 2192 | void *pSpace, /* Space available to hold resulting object */ |
| 2193 | int szSpace /* Size of pSpace[] in bytes */ |
| 2194 | ){ |
| 2195 | const unsigned char *aKey = (const unsigned char *)pKey; |
drh | e14006d | 2008-03-25 17:23:32 +0000 | [diff] [blame] | 2196 | UnpackedRecord *p; |
drh | 1e968a0 | 2008-03-25 00:22:21 +0000 | [diff] [blame] | 2197 | int nByte; |
| 2198 | int i, idx, d; |
| 2199 | u32 szHdr; |
| 2200 | Mem *pMem; |
| 2201 | |
drh | fab6959 | 2008-04-10 14:57:24 +0000 | [diff] [blame] | 2202 | assert( sizeof(Mem)>sizeof(*p) ); |
| 2203 | nByte = sizeof(Mem)*(pKeyInfo->nField+2); |
drh | 1e968a0 | 2008-03-25 00:22:21 +0000 | [diff] [blame] | 2204 | if( nByte>szSpace ){ |
| 2205 | p = sqlite3DbMallocRaw(pKeyInfo->db, nByte); |
| 2206 | if( p==0 ) return 0; |
| 2207 | p->needFree = 1; |
| 2208 | }else{ |
| 2209 | p = pSpace; |
| 2210 | p->needFree = 0; |
| 2211 | } |
| 2212 | p->pKeyInfo = pKeyInfo; |
| 2213 | p->nField = pKeyInfo->nField + 1; |
| 2214 | p->needDestroy = 1; |
drh | fab6959 | 2008-04-10 14:57:24 +0000 | [diff] [blame] | 2215 | p->aMem = pMem = &((Mem*)p)[1]; |
shane | 3f8d5cf | 2008-04-24 19:15:09 +0000 | [diff] [blame] | 2216 | idx = getVarint32(aKey, szHdr); |
drh | 1e968a0 | 2008-03-25 00:22:21 +0000 | [diff] [blame] | 2217 | d = szHdr; |
| 2218 | i = 0; |
| 2219 | while( idx<szHdr && i<p->nField ){ |
| 2220 | u32 serial_type; |
| 2221 | |
shane | 3f8d5cf | 2008-04-24 19:15:09 +0000 | [diff] [blame] | 2222 | idx += getVarint32( aKey+idx, serial_type); |
drh | 1e968a0 | 2008-03-25 00:22:21 +0000 | [diff] [blame] | 2223 | if( d>=nKey && sqlite3VdbeSerialTypeLen(serial_type)>0 ) break; |
| 2224 | pMem->enc = pKeyInfo->enc; |
| 2225 | pMem->db = pKeyInfo->db; |
| 2226 | pMem->flags = 0; |
danielk1977 | 5f09613 | 2008-03-28 15:44:09 +0000 | [diff] [blame] | 2227 | pMem->zMalloc = 0; |
drh | 1e968a0 | 2008-03-25 00:22:21 +0000 | [diff] [blame] | 2228 | d += sqlite3VdbeSerialGet(&aKey[d], serial_type, pMem); |
drh | e14006d | 2008-03-25 17:23:32 +0000 | [diff] [blame] | 2229 | pMem++; |
| 2230 | i++; |
drh | 1e968a0 | 2008-03-25 00:22:21 +0000 | [diff] [blame] | 2231 | } |
| 2232 | p->nField = i; |
| 2233 | return (void*)p; |
| 2234 | } |
| 2235 | |
| 2236 | /* |
drh | e14006d | 2008-03-25 17:23:32 +0000 | [diff] [blame] | 2237 | ** This routine destroys a UnpackedRecord object |
drh | 1e968a0 | 2008-03-25 00:22:21 +0000 | [diff] [blame] | 2238 | */ |
drh | e14006d | 2008-03-25 17:23:32 +0000 | [diff] [blame] | 2239 | void sqlite3VdbeDeleteUnpackedRecord(UnpackedRecord *p){ |
drh | 1e968a0 | 2008-03-25 00:22:21 +0000 | [diff] [blame] | 2240 | if( p ){ |
| 2241 | if( p->needDestroy ){ |
| 2242 | int i; |
drh | e14006d | 2008-03-25 17:23:32 +0000 | [diff] [blame] | 2243 | Mem *pMem; |
| 2244 | for(i=0, pMem=p->aMem; i<p->nField; i++, pMem++){ |
danielk1977 | 5f09613 | 2008-03-28 15:44:09 +0000 | [diff] [blame] | 2245 | if( pMem->zMalloc ){ |
drh | e14006d | 2008-03-25 17:23:32 +0000 | [diff] [blame] | 2246 | sqlite3VdbeMemRelease(pMem); |
drh | 1e968a0 | 2008-03-25 00:22:21 +0000 | [diff] [blame] | 2247 | } |
| 2248 | } |
| 2249 | } |
| 2250 | if( p->needFree ){ |
| 2251 | sqlite3_free(p); |
| 2252 | } |
| 2253 | } |
| 2254 | } |
| 2255 | |
| 2256 | /* |
| 2257 | ** This function compares the two table rows or index records |
| 2258 | ** specified by {nKey1, pKey1} and pPKey2. It returns a negative, zero |
| 2259 | ** or positive integer if {nKey1, pKey1} is less than, equal to or |
| 2260 | ** greater than pPKey2. The {nKey1, pKey1} key must be a blob |
| 2261 | ** created by th OP_MakeRecord opcode of the VDBE. The pPKey2 |
| 2262 | ** key must be a parsed key such as obtained from |
| 2263 | ** sqlite3VdbeParseRecord. |
| 2264 | ** |
| 2265 | ** Key1 and Key2 do not have to contain the same number of fields. |
| 2266 | ** But if the lengths differ, Key2 must be the shorter of the two. |
| 2267 | ** |
| 2268 | ** Historical note: In earlier versions of this routine both Key1 |
| 2269 | ** and Key2 were blobs obtained from OP_MakeRecord. But we found |
| 2270 | ** that in typical use the same Key2 would be submitted multiple times |
| 2271 | ** in a row. So an optimization was added to parse the Key2 key |
| 2272 | ** separately and submit the parsed version. In this way, we avoid |
| 2273 | ** parsing the same Key2 multiple times in a row. |
| 2274 | */ |
drh | e14006d | 2008-03-25 17:23:32 +0000 | [diff] [blame] | 2275 | int sqlite3VdbeRecordCompare( |
drh | 1e968a0 | 2008-03-25 00:22:21 +0000 | [diff] [blame] | 2276 | int nKey1, const void *pKey1, |
drh | e14006d | 2008-03-25 17:23:32 +0000 | [diff] [blame] | 2277 | UnpackedRecord *pPKey2 |
drh | 1e968a0 | 2008-03-25 00:22:21 +0000 | [diff] [blame] | 2278 | ){ |
| 2279 | u32 d1; /* Offset into aKey[] of next data element */ |
| 2280 | u32 idx1; /* Offset into aKey[] of next header element */ |
| 2281 | u32 szHdr1; /* Number of bytes in header */ |
| 2282 | int i = 0; |
| 2283 | int nField; |
| 2284 | int rc = 0; |
| 2285 | const unsigned char *aKey1 = (const unsigned char *)pKey1; |
| 2286 | KeyInfo *pKeyInfo; |
| 2287 | Mem mem1; |
| 2288 | |
| 2289 | pKeyInfo = pPKey2->pKeyInfo; |
| 2290 | mem1.enc = pKeyInfo->enc; |
| 2291 | mem1.db = pKeyInfo->db; |
| 2292 | mem1.flags = 0; |
danielk1977 | 5f09613 | 2008-03-28 15:44:09 +0000 | [diff] [blame] | 2293 | mem1.zMalloc = 0; |
drh | 1e968a0 | 2008-03-25 00:22:21 +0000 | [diff] [blame] | 2294 | |
shane | 3f8d5cf | 2008-04-24 19:15:09 +0000 | [diff] [blame] | 2295 | idx1 = getVarint32(aKey1, szHdr1); |
drh | 1e968a0 | 2008-03-25 00:22:21 +0000 | [diff] [blame] | 2296 | d1 = szHdr1; |
| 2297 | nField = pKeyInfo->nField; |
| 2298 | while( idx1<szHdr1 && i<pPKey2->nField ){ |
| 2299 | u32 serial_type1; |
| 2300 | |
| 2301 | /* Read the serial types for the next element in each key. */ |
shane | 3f8d5cf | 2008-04-24 19:15:09 +0000 | [diff] [blame] | 2302 | idx1 += getVarint32( aKey1+idx1, serial_type1 ); |
drh | 1e968a0 | 2008-03-25 00:22:21 +0000 | [diff] [blame] | 2303 | if( d1>=nKey1 && sqlite3VdbeSerialTypeLen(serial_type1)>0 ) break; |
| 2304 | |
| 2305 | /* Extract the values to be compared. |
| 2306 | */ |
| 2307 | d1 += sqlite3VdbeSerialGet(&aKey1[d1], serial_type1, &mem1); |
| 2308 | |
| 2309 | /* Do the comparison |
| 2310 | */ |
drh | e14006d | 2008-03-25 17:23:32 +0000 | [diff] [blame] | 2311 | rc = sqlite3MemCompare(&mem1, &pPKey2->aMem[i], |
drh | 1e968a0 | 2008-03-25 00:22:21 +0000 | [diff] [blame] | 2312 | i<nField ? pKeyInfo->aColl[i] : 0); |
drh | 1e968a0 | 2008-03-25 00:22:21 +0000 | [diff] [blame] | 2313 | if( rc!=0 ){ |
| 2314 | break; |
| 2315 | } |
| 2316 | i++; |
| 2317 | } |
danielk1977 | 5f09613 | 2008-03-28 15:44:09 +0000 | [diff] [blame] | 2318 | if( mem1.zMalloc ) sqlite3VdbeMemRelease(&mem1); |
drh | 1e968a0 | 2008-03-25 00:22:21 +0000 | [diff] [blame] | 2319 | |
| 2320 | /* One of the keys ran out of fields, but all the fields up to that point |
| 2321 | ** were equal. If the incrKey flag is true, then the second key is |
| 2322 | ** treated as larger. |
| 2323 | */ |
| 2324 | if( rc==0 ){ |
| 2325 | if( pKeyInfo->incrKey ){ |
| 2326 | rc = -1; |
| 2327 | }else if( !pKeyInfo->prefixIsEqual ){ |
| 2328 | if( d1<nKey1 ){ |
| 2329 | rc = 1; |
| 2330 | } |
| 2331 | } |
| 2332 | }else if( pKeyInfo->aSortOrder && i<pKeyInfo->nField |
| 2333 | && pKeyInfo->aSortOrder[i] ){ |
| 2334 | rc = -rc; |
| 2335 | } |
| 2336 | |
| 2337 | return rc; |
| 2338 | } |
drh | d578820 | 2004-05-28 08:21:05 +0000 | [diff] [blame] | 2339 | |
| 2340 | /* |
drh | 7a224de | 2004-06-02 01:22:02 +0000 | [diff] [blame] | 2341 | ** The argument is an index entry composed using the OP_MakeRecord opcode. |
| 2342 | ** The last entry in this record should be an integer (specifically |
| 2343 | ** an integer rowid). This routine returns the number of bytes in |
| 2344 | ** that integer. |
drh | d578820 | 2004-05-28 08:21:05 +0000 | [diff] [blame] | 2345 | */ |
drh | 7416170 | 2006-02-24 02:53:49 +0000 | [diff] [blame] | 2346 | int sqlite3VdbeIdxRowidLen(const u8 *aKey){ |
drh | d578820 | 2004-05-28 08:21:05 +0000 | [diff] [blame] | 2347 | u32 szHdr; /* Size of the header */ |
| 2348 | u32 typeRowid; /* Serial type of the rowid */ |
| 2349 | |
shane | 3f8d5cf | 2008-04-24 19:15:09 +0000 | [diff] [blame] | 2350 | (void)getVarint32(aKey, szHdr); |
| 2351 | (void)getVarint32(&aKey[szHdr-1], typeRowid); |
drh | d578820 | 2004-05-28 08:21:05 +0000 | [diff] [blame] | 2352 | return sqlite3VdbeSerialTypeLen(typeRowid); |
| 2353 | } |
danielk1977 | eb015e0 | 2004-05-18 01:31:14 +0000 | [diff] [blame] | 2354 | |
| 2355 | |
| 2356 | /* |
drh | 7a224de | 2004-06-02 01:22:02 +0000 | [diff] [blame] | 2357 | ** pCur points at an index entry created using the OP_MakeRecord opcode. |
| 2358 | ** Read the rowid (the last field in the record) and store it in *rowid. |
| 2359 | ** Return SQLITE_OK if everything works, or an error code otherwise. |
danielk1977 | 183f9f7 | 2004-05-13 05:20:26 +0000 | [diff] [blame] | 2360 | */ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 2361 | int sqlite3VdbeIdxRowid(BtCursor *pCur, i64 *rowid){ |
drh | 61fc595 | 2007-04-01 23:49:51 +0000 | [diff] [blame] | 2362 | i64 nCellKey = 0; |
danielk1977 | 183f9f7 | 2004-05-13 05:20:26 +0000 | [diff] [blame] | 2363 | int rc; |
drh | d578820 | 2004-05-28 08:21:05 +0000 | [diff] [blame] | 2364 | u32 szHdr; /* Size of the header */ |
| 2365 | u32 typeRowid; /* Serial type of the rowid */ |
| 2366 | u32 lenRowid; /* Size of the rowid */ |
| 2367 | Mem m, v; |
danielk1977 | 183f9f7 | 2004-05-13 05:20:26 +0000 | [diff] [blame] | 2368 | |
drh | d578820 | 2004-05-28 08:21:05 +0000 | [diff] [blame] | 2369 | sqlite3BtreeKeySize(pCur, &nCellKey); |
| 2370 | if( nCellKey<=0 ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 2371 | return SQLITE_CORRUPT_BKPT; |
drh | d578820 | 2004-05-28 08:21:05 +0000 | [diff] [blame] | 2372 | } |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 2373 | m.flags = 0; |
| 2374 | m.db = 0; |
danielk1977 | 5f09613 | 2008-03-28 15:44:09 +0000 | [diff] [blame] | 2375 | m.zMalloc = 0; |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 2376 | rc = sqlite3VdbeMemFromBtree(pCur, 0, nCellKey, 1, &m); |
drh | d578820 | 2004-05-28 08:21:05 +0000 | [diff] [blame] | 2377 | if( rc ){ |
danielk1977 | 183f9f7 | 2004-05-13 05:20:26 +0000 | [diff] [blame] | 2378 | return rc; |
| 2379 | } |
shane | 3f8d5cf | 2008-04-24 19:15:09 +0000 | [diff] [blame] | 2380 | (void)getVarint32((u8*)m.z, szHdr); |
| 2381 | (void)getVarint32((u8*)&m.z[szHdr-1], typeRowid); |
drh | d578820 | 2004-05-28 08:21:05 +0000 | [diff] [blame] | 2382 | lenRowid = sqlite3VdbeSerialTypeLen(typeRowid); |
drh | 2646da7 | 2005-12-09 20:02:05 +0000 | [diff] [blame] | 2383 | sqlite3VdbeSerialGet((u8*)&m.z[m.n-lenRowid], typeRowid, &v); |
drh | 3c024d6 | 2007-03-30 11:23:45 +0000 | [diff] [blame] | 2384 | *rowid = v.u.i; |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 2385 | sqlite3VdbeMemRelease(&m); |
danielk1977 | 183f9f7 | 2004-05-13 05:20:26 +0000 | [diff] [blame] | 2386 | return SQLITE_OK; |
| 2387 | } |
| 2388 | |
drh | 7cf6e4d | 2004-05-19 14:56:55 +0000 | [diff] [blame] | 2389 | /* |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 2390 | ** Compare the key of the index entry that cursor pC is point to against |
drh | 7cf6e4d | 2004-05-19 14:56:55 +0000 | [diff] [blame] | 2391 | ** the key string in pKey (of length nKey). Write into *pRes a number |
| 2392 | ** that is negative, zero, or positive if pC is less than, equal to, |
| 2393 | ** or greater than pKey. Return SQLITE_OK on success. |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 2394 | ** |
drh | d578820 | 2004-05-28 08:21:05 +0000 | [diff] [blame] | 2395 | ** pKey is either created without a rowid or is truncated so that it |
| 2396 | ** omits the rowid at the end. The rowid at the end of the index entry |
| 2397 | ** is ignored as well. |
drh | 7cf6e4d | 2004-05-19 14:56:55 +0000 | [diff] [blame] | 2398 | */ |
danielk1977 | 183f9f7 | 2004-05-13 05:20:26 +0000 | [diff] [blame] | 2399 | int sqlite3VdbeIdxKeyCompare( |
drh | 7cf6e4d | 2004-05-19 14:56:55 +0000 | [diff] [blame] | 2400 | Cursor *pC, /* The cursor to compare against */ |
danielk1977 | 751de56 | 2008-04-18 09:01:15 +0000 | [diff] [blame] | 2401 | UnpackedRecord *pUnpacked, |
drh | 7cf6e4d | 2004-05-19 14:56:55 +0000 | [diff] [blame] | 2402 | int nKey, const u8 *pKey, /* The key to compare */ |
| 2403 | int *res /* Write the comparison result here */ |
danielk1977 | 183f9f7 | 2004-05-13 05:20:26 +0000 | [diff] [blame] | 2404 | ){ |
drh | 61fc595 | 2007-04-01 23:49:51 +0000 | [diff] [blame] | 2405 | i64 nCellKey = 0; |
danielk1977 | 183f9f7 | 2004-05-13 05:20:26 +0000 | [diff] [blame] | 2406 | int rc; |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 2407 | BtCursor *pCur = pC->pCursor; |
drh | d578820 | 2004-05-28 08:21:05 +0000 | [diff] [blame] | 2408 | int lenRowid; |
| 2409 | Mem m; |
drh | e14006d | 2008-03-25 17:23:32 +0000 | [diff] [blame] | 2410 | UnpackedRecord *pRec; |
drh | 1e968a0 | 2008-03-25 00:22:21 +0000 | [diff] [blame] | 2411 | char zSpace[200]; |
danielk1977 | 183f9f7 | 2004-05-13 05:20:26 +0000 | [diff] [blame] | 2412 | |
| 2413 | sqlite3BtreeKeySize(pCur, &nCellKey); |
| 2414 | if( nCellKey<=0 ){ |
| 2415 | *res = 0; |
| 2416 | return SQLITE_OK; |
| 2417 | } |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 2418 | m.db = 0; |
| 2419 | m.flags = 0; |
danielk1977 | 5f09613 | 2008-03-28 15:44:09 +0000 | [diff] [blame] | 2420 | m.zMalloc = 0; |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 2421 | rc = sqlite3VdbeMemFromBtree(pC->pCursor, 0, nCellKey, 1, &m); |
drh | d578820 | 2004-05-28 08:21:05 +0000 | [diff] [blame] | 2422 | if( rc ){ |
| 2423 | return rc; |
danielk1977 | 183f9f7 | 2004-05-13 05:20:26 +0000 | [diff] [blame] | 2424 | } |
drh | 7416170 | 2006-02-24 02:53:49 +0000 | [diff] [blame] | 2425 | lenRowid = sqlite3VdbeIdxRowidLen((u8*)m.z); |
danielk1977 | 751de56 | 2008-04-18 09:01:15 +0000 | [diff] [blame] | 2426 | if( !pUnpacked ){ |
| 2427 | pRec = sqlite3VdbeRecordUnpack(pC->pKeyInfo, nKey, pKey, |
drh | 1e968a0 | 2008-03-25 00:22:21 +0000 | [diff] [blame] | 2428 | zSpace, sizeof(zSpace)); |
danielk1977 | 751de56 | 2008-04-18 09:01:15 +0000 | [diff] [blame] | 2429 | }else{ |
| 2430 | pRec = pUnpacked; |
| 2431 | } |
drh | 1e968a0 | 2008-03-25 00:22:21 +0000 | [diff] [blame] | 2432 | if( pRec==0 ){ |
| 2433 | return SQLITE_NOMEM; |
| 2434 | } |
drh | e14006d | 2008-03-25 17:23:32 +0000 | [diff] [blame] | 2435 | *res = sqlite3VdbeRecordCompare(m.n-lenRowid, m.z, pRec); |
danielk1977 | 751de56 | 2008-04-18 09:01:15 +0000 | [diff] [blame] | 2436 | if( !pUnpacked ){ |
| 2437 | sqlite3VdbeDeleteUnpackedRecord(pRec); |
| 2438 | } |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 2439 | sqlite3VdbeMemRelease(&m); |
danielk1977 | 183f9f7 | 2004-05-13 05:20:26 +0000 | [diff] [blame] | 2440 | return SQLITE_OK; |
| 2441 | } |
danielk1977 | b28af71 | 2004-06-21 06:50:26 +0000 | [diff] [blame] | 2442 | |
| 2443 | /* |
| 2444 | ** This routine sets the value to be returned by subsequent calls to |
| 2445 | ** sqlite3_changes() on the database handle 'db'. |
| 2446 | */ |
| 2447 | void sqlite3VdbeSetChanges(sqlite3 *db, int nChange){ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 2448 | assert( sqlite3_mutex_held(db->mutex) ); |
danielk1977 | b28af71 | 2004-06-21 06:50:26 +0000 | [diff] [blame] | 2449 | db->nChange = nChange; |
| 2450 | db->nTotalChange += nChange; |
| 2451 | } |
| 2452 | |
| 2453 | /* |
| 2454 | ** Set a flag in the vdbe to update the change counter when it is finalised |
| 2455 | ** or reset. |
| 2456 | */ |
drh | 4794f73 | 2004-11-05 17:17:50 +0000 | [diff] [blame] | 2457 | void sqlite3VdbeCountChanges(Vdbe *v){ |
| 2458 | v->changeCntOn = 1; |
danielk1977 | b28af71 | 2004-06-21 06:50:26 +0000 | [diff] [blame] | 2459 | } |
drh | d89bd00 | 2005-01-22 03:03:54 +0000 | [diff] [blame] | 2460 | |
| 2461 | /* |
| 2462 | ** Mark every prepared statement associated with a database connection |
| 2463 | ** as expired. |
| 2464 | ** |
| 2465 | ** An expired statement means that recompilation of the statement is |
| 2466 | ** recommend. Statements expire when things happen that make their |
| 2467 | ** programs obsolete. Removing user-defined functions or collating |
| 2468 | ** sequences, or changing an authorization function are the types of |
| 2469 | ** things that make prepared statements obsolete. |
| 2470 | */ |
| 2471 | void sqlite3ExpirePreparedStatements(sqlite3 *db){ |
| 2472 | Vdbe *p; |
| 2473 | for(p = db->pVdbe; p; p=p->pNext){ |
| 2474 | p->expired = 1; |
| 2475 | } |
| 2476 | } |
danielk1977 | aee18ef | 2005-03-09 12:26:50 +0000 | [diff] [blame] | 2477 | |
| 2478 | /* |
| 2479 | ** Return the database associated with the Vdbe. |
| 2480 | */ |
| 2481 | sqlite3 *sqlite3VdbeDb(Vdbe *v){ |
| 2482 | return v->db; |
| 2483 | } |