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