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