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 "vdbeInt.h" |
| 19 | |
| 20 | |
drh | 46c99e0 | 2007-08-27 23:26:59 +0000 | [diff] [blame] | 21 | |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 22 | /* |
| 23 | ** When debugging the code generator in a symbolic debugger, one can |
mlcreech | 3a00f90 | 2008-03-04 17:45:01 +0000 | [diff] [blame] | 24 | ** set the sqlite3VdbeAddopTrace to 1 and all opcodes will be printed |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 25 | ** as they are added to the instruction stream. |
| 26 | */ |
drh | 8d904f0 | 2005-06-14 17:47:58 +0000 | [diff] [blame] | 27 | #ifdef SQLITE_DEBUG |
mlcreech | 3a00f90 | 2008-03-04 17:45:01 +0000 | [diff] [blame] | 28 | int sqlite3VdbeAddopTrace = 0; |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 29 | #endif |
| 30 | |
| 31 | |
| 32 | /* |
| 33 | ** Create a new virtual database engine. |
| 34 | */ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 35 | Vdbe *sqlite3VdbeCreate(sqlite3 *db){ |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 36 | Vdbe *p; |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 37 | p = sqlite3DbMallocZero(db, sizeof(Vdbe) ); |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 38 | if( p==0 ) return 0; |
| 39 | p->db = db; |
| 40 | if( db->pVdbe ){ |
| 41 | db->pVdbe->pPrev = p; |
| 42 | } |
| 43 | p->pNext = db->pVdbe; |
| 44 | p->pPrev = 0; |
| 45 | db->pVdbe = p; |
| 46 | p->magic = VDBE_MAGIC_INIT; |
| 47 | return p; |
| 48 | } |
| 49 | |
| 50 | /* |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 51 | ** Remember the SQL string for a prepared statement. |
| 52 | */ |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 53 | void sqlite3VdbeSetSql(Vdbe *p, const char *z, int n, int isPrepareV2){ |
dan | 1d2ce4f | 2009-10-19 18:11:09 +0000 | [diff] [blame] | 54 | assert( isPrepareV2==1 || isPrepareV2==0 ); |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 55 | if( p==0 ) return; |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 56 | #ifdef SQLITE_OMIT_TRACE |
| 57 | if( !isPrepareV2 ) return; |
| 58 | #endif |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 59 | assert( p->zSql==0 ); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 60 | p->zSql = sqlite3DbStrNDup(p->db, z, n); |
shane | f639c40 | 2009-11-03 19:42:30 +0000 | [diff] [blame] | 61 | p->isPrepareV2 = (u8)isPrepareV2; |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | /* |
| 65 | ** Return the SQL associated with a prepared statement |
| 66 | */ |
danielk1977 | d0e2a85 | 2007-11-14 06:48:48 +0000 | [diff] [blame] | 67 | const char *sqlite3_sql(sqlite3_stmt *pStmt){ |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 68 | Vdbe *p = (Vdbe *)pStmt; |
drh | 87f5c5f | 2010-01-20 01:20:56 +0000 | [diff] [blame] | 69 | return (p && p->isPrepareV2) ? p->zSql : 0; |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | /* |
drh | c515525 | 2007-01-08 21:07:17 +0000 | [diff] [blame] | 73 | ** Swap all content between two VDBE structures. |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 74 | */ |
drh | c515525 | 2007-01-08 21:07:17 +0000 | [diff] [blame] | 75 | void sqlite3VdbeSwap(Vdbe *pA, Vdbe *pB){ |
| 76 | Vdbe tmp, *pTmp; |
| 77 | char *zTmp; |
drh | c515525 | 2007-01-08 21:07:17 +0000 | [diff] [blame] | 78 | tmp = *pA; |
| 79 | *pA = *pB; |
| 80 | *pB = tmp; |
| 81 | pTmp = pA->pNext; |
| 82 | pA->pNext = pB->pNext; |
| 83 | pB->pNext = pTmp; |
| 84 | pTmp = pA->pPrev; |
| 85 | pA->pPrev = pB->pPrev; |
| 86 | pB->pPrev = pTmp; |
| 87 | zTmp = pA->zSql; |
| 88 | pA->zSql = pB->zSql; |
| 89 | pB->zSql = zTmp; |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 90 | pB->isPrepareV2 = pA->isPrepareV2; |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 91 | } |
| 92 | |
drh | cf1023c | 2007-05-08 20:59:49 +0000 | [diff] [blame] | 93 | #ifdef SQLITE_DEBUG |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 94 | /* |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 95 | ** Turn tracing on or off |
| 96 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 97 | void sqlite3VdbeTrace(Vdbe *p, FILE *trace){ |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 98 | p->trace = trace; |
| 99 | } |
drh | cf1023c | 2007-05-08 20:59:49 +0000 | [diff] [blame] | 100 | #endif |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 101 | |
| 102 | /* |
danielk1977 | 00e1361 | 2008-11-17 19:18:54 +0000 | [diff] [blame] | 103 | ** Resize the Vdbe.aOp array so that it is at least one op larger than |
| 104 | ** it was. |
danielk1977 | ace3eb2 | 2006-01-26 10:35:04 +0000 | [diff] [blame] | 105 | ** |
danielk1977 | 00e1361 | 2008-11-17 19:18:54 +0000 | [diff] [blame] | 106 | ** If an out-of-memory error occurs while resizing the array, return |
| 107 | ** SQLITE_NOMEM. In this case Vdbe.aOp and Vdbe.nOpAlloc remain |
| 108 | ** unchanged (this is so that any opcodes already allocated can be |
| 109 | ** correctly deallocated along with the rest of the Vdbe). |
drh | 76ff3a0 | 2004-09-24 22:32:30 +0000 | [diff] [blame] | 110 | */ |
danielk1977 | 00e1361 | 2008-11-17 19:18:54 +0000 | [diff] [blame] | 111 | static int growOpArray(Vdbe *p){ |
drh | a4e5d58 | 2007-10-20 15:41:57 +0000 | [diff] [blame] | 112 | VdbeOp *pNew; |
danielk1977 | 00e1361 | 2008-11-17 19:18:54 +0000 | [diff] [blame] | 113 | int nNew = (p->nOpAlloc ? p->nOpAlloc*2 : (int)(1024/sizeof(Op))); |
| 114 | pNew = sqlite3DbRealloc(p->db, p->aOp, nNew*sizeof(Op)); |
drh | a4e5d58 | 2007-10-20 15:41:57 +0000 | [diff] [blame] | 115 | if( pNew ){ |
drh | b45f65d | 2009-03-01 19:42:11 +0000 | [diff] [blame] | 116 | p->nOpAlloc = sqlite3DbMallocSize(p->db, pNew)/sizeof(Op); |
drh | a4e5d58 | 2007-10-20 15:41:57 +0000 | [diff] [blame] | 117 | p->aOp = pNew; |
drh | 76ff3a0 | 2004-09-24 22:32:30 +0000 | [diff] [blame] | 118 | } |
danielk1977 | 00e1361 | 2008-11-17 19:18:54 +0000 | [diff] [blame] | 119 | return (pNew ? SQLITE_OK : SQLITE_NOMEM); |
drh | 76ff3a0 | 2004-09-24 22:32:30 +0000 | [diff] [blame] | 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 | 8df3284 | 2008-12-09 02:51:23 +0000 | [diff] [blame] | 144 | assert( op>0 && op<0xff ); |
drh | fd2d26b | 2006-03-15 22:44:36 +0000 | [diff] [blame] | 145 | if( p->nOpAlloc<=i ){ |
danielk1977 | 00e1361 | 2008-11-17 19:18:54 +0000 | [diff] [blame] | 146 | if( growOpArray(p) ){ |
drh | c42ed16 | 2009-06-26 14:04:51 +0000 | [diff] [blame] | 147 | return 1; |
drh | fd2d26b | 2006-03-15 22:44:36 +0000 | [diff] [blame] | 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]; |
drh | 8df3284 | 2008-12-09 02:51:23 +0000 | [diff] [blame] | 152 | pOp->opcode = (u8)op; |
drh | 26c9b5e | 2008-04-11 14:56:53 +0000 | [diff] [blame] | 153 | pOp->p5 = 0; |
drh | 701a0ae | 2004-02-22 20:05:00 +0000 | [diff] [blame] | 154 | pOp->p1 = p1; |
drh | 701a0ae | 2004-02-22 20:05:00 +0000 | [diff] [blame] | 155 | pOp->p2 = p2; |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 156 | pOp->p3 = p3; |
| 157 | pOp->p4.p = 0; |
| 158 | pOp->p4type = P4_NOTUSED; |
danielk1977 | 8b60e0f | 2005-01-12 09:10:39 +0000 | [diff] [blame] | 159 | #ifdef SQLITE_DEBUG |
drh | 26c9b5e | 2008-04-11 14:56:53 +0000 | [diff] [blame] | 160 | pOp->zComment = 0; |
mlcreech | 3a00f90 | 2008-03-04 17:45:01 +0000 | [diff] [blame] | 161 | if( sqlite3VdbeAddopTrace ) sqlite3VdbePrintOp(0, i, &p->aOp[i]); |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 162 | #endif |
drh | 26c9b5e | 2008-04-11 14:56:53 +0000 | [diff] [blame] | 163 | #ifdef VDBE_PROFILE |
| 164 | pOp->cycles = 0; |
| 165 | pOp->cnt = 0; |
| 166 | #endif |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 167 | return i; |
| 168 | } |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 169 | int sqlite3VdbeAddOp0(Vdbe *p, int op){ |
| 170 | return sqlite3VdbeAddOp3(p, op, 0, 0, 0); |
| 171 | } |
| 172 | int sqlite3VdbeAddOp1(Vdbe *p, int op, int p1){ |
| 173 | return sqlite3VdbeAddOp3(p, op, p1, 0, 0); |
| 174 | } |
| 175 | int sqlite3VdbeAddOp2(Vdbe *p, int op, int p1, int p2){ |
| 176 | return sqlite3VdbeAddOp3(p, op, p1, p2, 0); |
drh | 701a0ae | 2004-02-22 20:05:00 +0000 | [diff] [blame] | 177 | } |
| 178 | |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 179 | |
drh | 701a0ae | 2004-02-22 20:05:00 +0000 | [diff] [blame] | 180 | /* |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 181 | ** Add an opcode that includes the p4 value as a pointer. |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 182 | */ |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 183 | int sqlite3VdbeAddOp4( |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 184 | Vdbe *p, /* Add the opcode to this VM */ |
| 185 | int op, /* The new opcode */ |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 186 | int p1, /* The P1 operand */ |
| 187 | int p2, /* The P2 operand */ |
| 188 | int p3, /* The P3 operand */ |
| 189 | const char *zP4, /* The P4 operand */ |
| 190 | int p4type /* P4 operand type */ |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 191 | ){ |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 192 | int addr = sqlite3VdbeAddOp3(p, op, p1, p2, p3); |
| 193 | sqlite3VdbeChangeP4(p, addr, zP4, p4type); |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 194 | return addr; |
| 195 | } |
| 196 | |
| 197 | /* |
drh | 5d9c9da | 2011-06-03 20:11:17 +0000 | [diff] [blame] | 198 | ** Add an OP_ParseSchema opcode. This routine is broken out from |
| 199 | ** sqlite3VdbeAddOp4() since it needs to also local all btrees. |
| 200 | ** |
| 201 | ** The zWhere string must have been obtained from sqlite3_malloc(). |
| 202 | ** This routine will take ownership of the allocated memory. |
| 203 | */ |
| 204 | void sqlite3VdbeAddParseSchemaOp(Vdbe *p, int iDb, char *zWhere){ |
| 205 | int j; |
| 206 | int addr = sqlite3VdbeAddOp3(p, OP_ParseSchema, iDb, 0, 0); |
| 207 | sqlite3VdbeChangeP4(p, addr, zWhere, P4_DYNAMIC); |
| 208 | for(j=0; j<p->db->nDb; j++) sqlite3VdbeUsesBtree(p, j); |
| 209 | } |
| 210 | |
| 211 | /* |
drh | 8cff69d | 2009-11-12 19:59:44 +0000 | [diff] [blame] | 212 | ** Add an opcode that includes the p4 value as an integer. |
| 213 | */ |
| 214 | int sqlite3VdbeAddOp4Int( |
| 215 | Vdbe *p, /* Add the opcode to this VM */ |
| 216 | int op, /* The new opcode */ |
| 217 | int p1, /* The P1 operand */ |
| 218 | int p2, /* The P2 operand */ |
| 219 | int p3, /* The P3 operand */ |
| 220 | int p4 /* The P4 operand as an integer */ |
| 221 | ){ |
| 222 | int addr = sqlite3VdbeAddOp3(p, op, p1, p2, p3); |
| 223 | sqlite3VdbeChangeP4(p, addr, SQLITE_INT_TO_PTR(p4), P4_INT32); |
| 224 | return addr; |
| 225 | } |
| 226 | |
| 227 | /* |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 228 | ** Create a new symbolic label for an instruction that has yet to be |
| 229 | ** coded. The symbolic label is really just a negative number. The |
| 230 | ** label can be used as the P2 value of an operation. Later, when |
| 231 | ** the label is resolved to a specific address, the VDBE will scan |
| 232 | ** through its operation list and change all values of P2 which match |
| 233 | ** the label into the resolved address. |
| 234 | ** |
| 235 | ** The VDBE knows that a P2 value is a label because labels are |
| 236 | ** always negative and P2 values are suppose to be non-negative. |
| 237 | ** 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] | 238 | ** |
| 239 | ** Zero is returned if a malloc() fails. |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 240 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 241 | int sqlite3VdbeMakeLabel(Vdbe *p){ |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 242 | int i; |
| 243 | i = p->nLabel++; |
| 244 | assert( p->magic==VDBE_MAGIC_INIT ); |
| 245 | if( i>=p->nLabelAlloc ){ |
drh | 6a1e071 | 2008-12-05 15:24:15 +0000 | [diff] [blame] | 246 | int n = p->nLabelAlloc*2 + 5; |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 247 | p->aLabel = sqlite3DbReallocOrFree(p->db, p->aLabel, |
drh | 6a1e071 | 2008-12-05 15:24:15 +0000 | [diff] [blame] | 248 | n*sizeof(p->aLabel[0])); |
| 249 | p->nLabelAlloc = sqlite3DbMallocSize(p->db, p->aLabel)/sizeof(p->aLabel[0]); |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 250 | } |
drh | 76ff3a0 | 2004-09-24 22:32:30 +0000 | [diff] [blame] | 251 | if( p->aLabel ){ |
| 252 | p->aLabel[i] = -1; |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 253 | } |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 254 | return -1-i; |
| 255 | } |
| 256 | |
| 257 | /* |
| 258 | ** Resolve label "x" to be the address of the next instruction to |
| 259 | ** be inserted. The parameter "x" must have been obtained from |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 260 | ** a prior call to sqlite3VdbeMakeLabel(). |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 261 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 262 | void sqlite3VdbeResolveLabel(Vdbe *p, int x){ |
drh | 76ff3a0 | 2004-09-24 22:32:30 +0000 | [diff] [blame] | 263 | int j = -1-x; |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 264 | assert( p->magic==VDBE_MAGIC_INIT ); |
drh | 76ff3a0 | 2004-09-24 22:32:30 +0000 | [diff] [blame] | 265 | assert( j>=0 && j<p->nLabel ); |
| 266 | if( p->aLabel ){ |
| 267 | p->aLabel[j] = p->nOp; |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 268 | } |
| 269 | } |
| 270 | |
drh | 4611d92 | 2010-02-25 14:47:01 +0000 | [diff] [blame] | 271 | /* |
| 272 | ** Mark the VDBE as one that can only be run one time. |
| 273 | */ |
| 274 | void sqlite3VdbeRunOnlyOnce(Vdbe *p){ |
| 275 | p->runOnlyOnce = 1; |
| 276 | } |
| 277 | |
drh | ff738bc | 2009-09-24 00:09:58 +0000 | [diff] [blame] | 278 | #ifdef SQLITE_DEBUG /* sqlite3AssertMayAbort() logic */ |
dan | 144926d | 2009-09-09 11:37:20 +0000 | [diff] [blame] | 279 | |
| 280 | /* |
| 281 | ** The following type and function are used to iterate through all opcodes |
| 282 | ** in a Vdbe main program and each of the sub-programs (triggers) it may |
| 283 | ** invoke directly or indirectly. It should be used as follows: |
| 284 | ** |
| 285 | ** Op *pOp; |
| 286 | ** VdbeOpIter sIter; |
| 287 | ** |
| 288 | ** memset(&sIter, 0, sizeof(sIter)); |
| 289 | ** sIter.v = v; // v is of type Vdbe* |
| 290 | ** while( (pOp = opIterNext(&sIter)) ){ |
| 291 | ** // Do something with pOp |
| 292 | ** } |
| 293 | ** sqlite3DbFree(v->db, sIter.apSub); |
| 294 | ** |
| 295 | */ |
| 296 | typedef struct VdbeOpIter VdbeOpIter; |
| 297 | struct VdbeOpIter { |
| 298 | Vdbe *v; /* Vdbe to iterate through the opcodes of */ |
| 299 | SubProgram **apSub; /* Array of subprograms */ |
| 300 | int nSub; /* Number of entries in apSub */ |
| 301 | int iAddr; /* Address of next instruction to return */ |
| 302 | int iSub; /* 0 = main program, 1 = first sub-program etc. */ |
| 303 | }; |
| 304 | static Op *opIterNext(VdbeOpIter *p){ |
| 305 | Vdbe *v = p->v; |
| 306 | Op *pRet = 0; |
| 307 | Op *aOp; |
| 308 | int nOp; |
| 309 | |
| 310 | if( p->iSub<=p->nSub ){ |
| 311 | |
| 312 | if( p->iSub==0 ){ |
| 313 | aOp = v->aOp; |
| 314 | nOp = v->nOp; |
| 315 | }else{ |
| 316 | aOp = p->apSub[p->iSub-1]->aOp; |
| 317 | nOp = p->apSub[p->iSub-1]->nOp; |
| 318 | } |
| 319 | assert( p->iAddr<nOp ); |
| 320 | |
| 321 | pRet = &aOp[p->iAddr]; |
| 322 | p->iAddr++; |
| 323 | if( p->iAddr==nOp ){ |
| 324 | p->iSub++; |
| 325 | p->iAddr = 0; |
| 326 | } |
| 327 | |
| 328 | if( pRet->p4type==P4_SUBPROGRAM ){ |
| 329 | int nByte = (p->nSub+1)*sizeof(SubProgram*); |
| 330 | int j; |
| 331 | for(j=0; j<p->nSub; j++){ |
| 332 | if( p->apSub[j]==pRet->p4.pProgram ) break; |
| 333 | } |
| 334 | if( j==p->nSub ){ |
| 335 | p->apSub = sqlite3DbReallocOrFree(v->db, p->apSub, nByte); |
| 336 | if( !p->apSub ){ |
| 337 | pRet = 0; |
| 338 | }else{ |
| 339 | p->apSub[p->nSub++] = pRet->p4.pProgram; |
| 340 | } |
| 341 | } |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | return pRet; |
| 346 | } |
| 347 | |
| 348 | /* |
dan | f367721 | 2009-09-10 16:14:50 +0000 | [diff] [blame] | 349 | ** Check if the program stored in the VM associated with pParse may |
drh | ff738bc | 2009-09-24 00:09:58 +0000 | [diff] [blame] | 350 | ** throw an ABORT exception (causing the statement, but not entire transaction |
dan | 144926d | 2009-09-09 11:37:20 +0000 | [diff] [blame] | 351 | ** to be rolled back). This condition is true if the main program or any |
| 352 | ** sub-programs contains any of the following: |
| 353 | ** |
| 354 | ** * OP_Halt with P1=SQLITE_CONSTRAINT and P2=OE_Abort. |
| 355 | ** * OP_HaltIfNull with P1=SQLITE_CONSTRAINT and P2=OE_Abort. |
| 356 | ** * OP_Destroy |
| 357 | ** * OP_VUpdate |
| 358 | ** * OP_VRename |
dan | 32b09f2 | 2009-09-23 17:29:59 +0000 | [diff] [blame] | 359 | ** * OP_FkCounter with P2==0 (immediate foreign key constraint) |
dan | 144926d | 2009-09-09 11:37:20 +0000 | [diff] [blame] | 360 | ** |
dan | f367721 | 2009-09-10 16:14:50 +0000 | [diff] [blame] | 361 | ** Then check that the value of Parse.mayAbort is true if an |
| 362 | ** ABORT may be thrown, or false otherwise. Return true if it does |
| 363 | ** match, or false otherwise. This function is intended to be used as |
| 364 | ** part of an assert statement in the compiler. Similar to: |
| 365 | ** |
| 366 | ** assert( sqlite3VdbeAssertMayAbort(pParse->pVdbe, pParse->mayAbort) ); |
dan | 144926d | 2009-09-09 11:37:20 +0000 | [diff] [blame] | 367 | */ |
dan | f367721 | 2009-09-10 16:14:50 +0000 | [diff] [blame] | 368 | int sqlite3VdbeAssertMayAbort(Vdbe *v, int mayAbort){ |
| 369 | int hasAbort = 0; |
dan | 144926d | 2009-09-09 11:37:20 +0000 | [diff] [blame] | 370 | Op *pOp; |
| 371 | VdbeOpIter sIter; |
| 372 | memset(&sIter, 0, sizeof(sIter)); |
| 373 | sIter.v = v; |
| 374 | |
| 375 | while( (pOp = opIterNext(&sIter))!=0 ){ |
| 376 | int opcode = pOp->opcode; |
| 377 | if( opcode==OP_Destroy || opcode==OP_VUpdate || opcode==OP_VRename |
dan | 32b09f2 | 2009-09-23 17:29:59 +0000 | [diff] [blame] | 378 | #ifndef SQLITE_OMIT_FOREIGN_KEY |
dan | 0ff297e | 2009-09-25 17:03:14 +0000 | [diff] [blame] | 379 | || (opcode==OP_FkCounter && pOp->p1==0 && pOp->p2==1) |
dan | 32b09f2 | 2009-09-23 17:29:59 +0000 | [diff] [blame] | 380 | #endif |
dan | 144926d | 2009-09-09 11:37:20 +0000 | [diff] [blame] | 381 | || ((opcode==OP_Halt || opcode==OP_HaltIfNull) |
| 382 | && (pOp->p1==SQLITE_CONSTRAINT && pOp->p2==OE_Abort)) |
| 383 | ){ |
dan | f367721 | 2009-09-10 16:14:50 +0000 | [diff] [blame] | 384 | hasAbort = 1; |
dan | 144926d | 2009-09-09 11:37:20 +0000 | [diff] [blame] | 385 | break; |
| 386 | } |
| 387 | } |
dan | 144926d | 2009-09-09 11:37:20 +0000 | [diff] [blame] | 388 | sqlite3DbFree(v->db, sIter.apSub); |
dan | f367721 | 2009-09-10 16:14:50 +0000 | [diff] [blame] | 389 | |
| 390 | /* Return true if hasAbort==mayAbort. Or if a malloc failure occured. |
| 391 | ** If malloc failed, then the while() loop above may not have iterated |
| 392 | ** through all opcodes and hasAbort may be set incorrectly. Return |
| 393 | ** true for this case to prevent the assert() in the callers frame |
| 394 | ** from failing. */ |
| 395 | return ( v->db->mallocFailed || hasAbort==mayAbort ); |
dan | 144926d | 2009-09-09 11:37:20 +0000 | [diff] [blame] | 396 | } |
drh | ff738bc | 2009-09-24 00:09:58 +0000 | [diff] [blame] | 397 | #endif /* SQLITE_DEBUG - the sqlite3AssertMayAbort() function */ |
dan | 144926d | 2009-09-09 11:37:20 +0000 | [diff] [blame] | 398 | |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 399 | /* |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 400 | ** Loop through the program looking for P2 values that are negative |
| 401 | ** on jump instructions. Each such value is a label. Resolve the |
| 402 | ** label by setting the P2 value to its correct non-zero value. |
drh | 76ff3a0 | 2004-09-24 22:32:30 +0000 | [diff] [blame] | 403 | ** |
| 404 | ** This routine is called once after all opcodes have been inserted. |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 405 | ** |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 406 | ** Variable *pMaxFuncArgs is set to the maximum value of any P2 argument |
danielk1977 | 399918f | 2006-06-14 13:03:23 +0000 | [diff] [blame] | 407 | ** 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] | 408 | ** sqlite3VdbeMakeReady() to size the Vdbe.apArg[] array. |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 409 | ** |
| 410 | ** The Op.opflags field is set on all opcodes. |
drh | 76ff3a0 | 2004-09-24 22:32:30 +0000 | [diff] [blame] | 411 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 412 | static void resolveP2Values(Vdbe *p, int *pMaxFuncArgs){ |
drh | 76ff3a0 | 2004-09-24 22:32:30 +0000 | [diff] [blame] | 413 | int i; |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 414 | int nMaxArgs = *pMaxFuncArgs; |
drh | 76ff3a0 | 2004-09-24 22:32:30 +0000 | [diff] [blame] | 415 | Op *pOp; |
| 416 | int *aLabel = p->aLabel; |
drh | ad4a4b8 | 2008-11-05 16:37:34 +0000 | [diff] [blame] | 417 | p->readOnly = 1; |
drh | 76ff3a0 | 2004-09-24 22:32:30 +0000 | [diff] [blame] | 418 | for(pOp=p->aOp, i=p->nOp-1; i>=0; i--, pOp++){ |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 419 | u8 opcode = pOp->opcode; |
| 420 | |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 421 | pOp->opflags = sqlite3OpcodeProperty[opcode]; |
drh | a2baf3a | 2008-06-18 15:34:09 +0000 | [diff] [blame] | 422 | if( opcode==OP_Function || opcode==OP_AggStep ){ |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 423 | if( pOp->p5>nMaxArgs ) nMaxArgs = pOp->p5; |
drh | 10fc727 | 2010-12-08 18:30:19 +0000 | [diff] [blame] | 424 | }else if( (opcode==OP_Transaction && pOp->p2!=0) || opcode==OP_Vacuum ){ |
drh | ad4a4b8 | 2008-11-05 16:37:34 +0000 | [diff] [blame] | 425 | p->readOnly = 0; |
danielk1977 | 182c4ba | 2007-06-27 15:53:34 +0000 | [diff] [blame] | 426 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 427 | }else if( opcode==OP_VUpdate ){ |
| 428 | if( pOp->p2>nMaxArgs ) nMaxArgs = pOp->p2; |
drh | 4be8b51 | 2006-06-13 23:51:34 +0000 | [diff] [blame] | 429 | }else if( opcode==OP_VFilter ){ |
| 430 | int n; |
| 431 | assert( p->nOp - i >= 3 ); |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 432 | assert( pOp[-1].opcode==OP_Integer ); |
danielk1977 | 6dbee81 | 2008-01-03 18:39:41 +0000 | [diff] [blame] | 433 | n = pOp[-1].p1; |
drh | 4be8b51 | 2006-06-13 23:51:34 +0000 | [diff] [blame] | 434 | if( n>nMaxArgs ) nMaxArgs = n; |
danielk1977 | 182c4ba | 2007-06-27 15:53:34 +0000 | [diff] [blame] | 435 | #endif |
drh | c6aff30 | 2011-09-01 15:32:47 +0000 | [diff] [blame] | 436 | }else if( opcode==OP_Next || opcode==OP_SorterNext ){ |
dan | a205a48 | 2011-08-27 18:48:57 +0000 | [diff] [blame] | 437 | pOp->p4.xAdvance = sqlite3BtreeNext; |
| 438 | pOp->p4type = P4_ADVANCE; |
| 439 | }else if( opcode==OP_Prev ){ |
| 440 | pOp->p4.xAdvance = sqlite3BtreePrevious; |
| 441 | pOp->p4type = P4_ADVANCE; |
danielk1977 | bc04f85 | 2005-03-29 08:26:13 +0000 | [diff] [blame] | 442 | } |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 443 | |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 444 | if( (pOp->opflags & OPFLG_JUMP)!=0 && pOp->p2<0 ){ |
drh | d298151 | 2008-01-04 19:33:49 +0000 | [diff] [blame] | 445 | assert( -1-pOp->p2<p->nLabel ); |
| 446 | pOp->p2 = aLabel[-1-pOp->p2]; |
| 447 | } |
drh | 76ff3a0 | 2004-09-24 22:32:30 +0000 | [diff] [blame] | 448 | } |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 449 | sqlite3DbFree(p->db, p->aLabel); |
drh | 76ff3a0 | 2004-09-24 22:32:30 +0000 | [diff] [blame] | 450 | p->aLabel = 0; |
danielk1977 | bc04f85 | 2005-03-29 08:26:13 +0000 | [diff] [blame] | 451 | |
| 452 | *pMaxFuncArgs = nMaxArgs; |
drh | 76ff3a0 | 2004-09-24 22:32:30 +0000 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | /* |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 456 | ** Return the address of the next instruction to be inserted. |
| 457 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 458 | int sqlite3VdbeCurrentAddr(Vdbe *p){ |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 459 | assert( p->magic==VDBE_MAGIC_INIT ); |
| 460 | return p->nOp; |
| 461 | } |
| 462 | |
dan | 65a7cd1 | 2009-09-01 12:16:01 +0000 | [diff] [blame] | 463 | /* |
| 464 | ** This function returns a pointer to the array of opcodes associated with |
| 465 | ** the Vdbe passed as the first argument. It is the callers responsibility |
| 466 | ** to arrange for the returned array to be eventually freed using the |
| 467 | ** vdbeFreeOpArray() function. |
| 468 | ** |
| 469 | ** Before returning, *pnOp is set to the number of entries in the returned |
| 470 | ** array. Also, *pnMaxArg is set to the larger of its current value and |
| 471 | ** the number of entries in the Vdbe.apArg[] array required to execute the |
| 472 | ** returned program. |
| 473 | */ |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 474 | VdbeOp *sqlite3VdbeTakeOpArray(Vdbe *p, int *pnOp, int *pnMaxArg){ |
| 475 | VdbeOp *aOp = p->aOp; |
dan | 523a087 | 2009-08-31 05:23:32 +0000 | [diff] [blame] | 476 | assert( aOp && !p->db->mallocFailed ); |
dan | 65a7cd1 | 2009-09-01 12:16:01 +0000 | [diff] [blame] | 477 | |
| 478 | /* Check that sqlite3VdbeUsesBtree() was not called on this VM */ |
drh | bdaec52 | 2011-04-04 00:14:43 +0000 | [diff] [blame] | 479 | assert( p->btreeMask==0 ); |
dan | 65a7cd1 | 2009-09-01 12:16:01 +0000 | [diff] [blame] | 480 | |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 481 | resolveP2Values(p, pnMaxArg); |
| 482 | *pnOp = p->nOp; |
| 483 | p->aOp = 0; |
| 484 | return aOp; |
| 485 | } |
| 486 | |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 487 | /* |
| 488 | ** Add a whole list of operations to the operation stack. Return the |
| 489 | ** address of the first operation added. |
| 490 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 491 | int sqlite3VdbeAddOpList(Vdbe *p, int nOp, VdbeOpList const *aOp){ |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 492 | int addr; |
| 493 | assert( p->magic==VDBE_MAGIC_INIT ); |
danielk1977 | 00e1361 | 2008-11-17 19:18:54 +0000 | [diff] [blame] | 494 | if( p->nOp + nOp > p->nOpAlloc && growOpArray(p) ){ |
drh | 76ff3a0 | 2004-09-24 22:32:30 +0000 | [diff] [blame] | 495 | return 0; |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 496 | } |
| 497 | addr = p->nOp; |
drh | 7b74603 | 2009-06-26 12:15:22 +0000 | [diff] [blame] | 498 | if( ALWAYS(nOp>0) ){ |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 499 | int i; |
drh | 905793e | 2004-02-21 13:31:09 +0000 | [diff] [blame] | 500 | VdbeOpList const *pIn = aOp; |
| 501 | for(i=0; i<nOp; i++, pIn++){ |
| 502 | int p2 = pIn->p2; |
| 503 | VdbeOp *pOut = &p->aOp[i+addr]; |
| 504 | pOut->opcode = pIn->opcode; |
| 505 | pOut->p1 = pIn->p1; |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 506 | if( p2<0 && (sqlite3OpcodeProperty[pOut->opcode] & OPFLG_JUMP)!=0 ){ |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 507 | pOut->p2 = addr + ADDR(p2); |
| 508 | }else{ |
| 509 | pOut->p2 = p2; |
| 510 | } |
drh | 2400345 | 2008-01-03 01:28:59 +0000 | [diff] [blame] | 511 | pOut->p3 = pIn->p3; |
| 512 | pOut->p4type = P4_NOTUSED; |
| 513 | pOut->p4.p = 0; |
| 514 | pOut->p5 = 0; |
danielk1977 | 8b60e0f | 2005-01-12 09:10:39 +0000 | [diff] [blame] | 515 | #ifdef SQLITE_DEBUG |
drh | 26c9b5e | 2008-04-11 14:56:53 +0000 | [diff] [blame] | 516 | pOut->zComment = 0; |
mlcreech | 3a00f90 | 2008-03-04 17:45:01 +0000 | [diff] [blame] | 517 | if( sqlite3VdbeAddopTrace ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 518 | sqlite3VdbePrintOp(0, i+addr, &p->aOp[i+addr]); |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 519 | } |
| 520 | #endif |
| 521 | } |
| 522 | p->nOp += nOp; |
| 523 | } |
| 524 | return addr; |
| 525 | } |
| 526 | |
| 527 | /* |
| 528 | ** Change the value of the P1 operand for a specific instruction. |
| 529 | ** This routine is useful when a large program is loaded from a |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 530 | ** static array using sqlite3VdbeAddOpList but we want to make a |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 531 | ** few minor changes to the program. |
| 532 | */ |
drh | 88caeac | 2011-08-24 15:12:08 +0000 | [diff] [blame] | 533 | void sqlite3VdbeChangeP1(Vdbe *p, u32 addr, int val){ |
drh | 7b74603 | 2009-06-26 12:15:22 +0000 | [diff] [blame] | 534 | assert( p!=0 ); |
drh | 88caeac | 2011-08-24 15:12:08 +0000 | [diff] [blame] | 535 | if( ((u32)p->nOp)>addr ){ |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 536 | p->aOp[addr].p1 = val; |
| 537 | } |
| 538 | } |
| 539 | |
| 540 | /* |
| 541 | ** Change the value of the P2 operand for a specific instruction. |
| 542 | ** This routine is useful for setting a jump destination. |
| 543 | */ |
drh | 88caeac | 2011-08-24 15:12:08 +0000 | [diff] [blame] | 544 | void sqlite3VdbeChangeP2(Vdbe *p, u32 addr, int val){ |
drh | 7b74603 | 2009-06-26 12:15:22 +0000 | [diff] [blame] | 545 | assert( p!=0 ); |
drh | 88caeac | 2011-08-24 15:12:08 +0000 | [diff] [blame] | 546 | if( ((u32)p->nOp)>addr ){ |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 547 | p->aOp[addr].p2 = val; |
| 548 | } |
| 549 | } |
| 550 | |
drh | d654be8 | 2005-09-20 17:42:23 +0000 | [diff] [blame] | 551 | /* |
danielk1977 | 1f4aa33 | 2008-01-03 09:51:55 +0000 | [diff] [blame] | 552 | ** Change the value of the P3 operand for a specific instruction. |
danielk1977 | 207872a | 2008-01-03 07:54:23 +0000 | [diff] [blame] | 553 | */ |
drh | 88caeac | 2011-08-24 15:12:08 +0000 | [diff] [blame] | 554 | void sqlite3VdbeChangeP3(Vdbe *p, u32 addr, int val){ |
drh | 7b74603 | 2009-06-26 12:15:22 +0000 | [diff] [blame] | 555 | assert( p!=0 ); |
drh | 88caeac | 2011-08-24 15:12:08 +0000 | [diff] [blame] | 556 | if( ((u32)p->nOp)>addr ){ |
danielk1977 | 207872a | 2008-01-03 07:54:23 +0000 | [diff] [blame] | 557 | p->aOp[addr].p3 = val; |
| 558 | } |
| 559 | } |
| 560 | |
| 561 | /* |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 562 | ** Change the value of the P5 operand for the most recently |
| 563 | ** added operation. |
danielk1977 | 1f4aa33 | 2008-01-03 09:51:55 +0000 | [diff] [blame] | 564 | */ |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 565 | void sqlite3VdbeChangeP5(Vdbe *p, u8 val){ |
drh | 7b74603 | 2009-06-26 12:15:22 +0000 | [diff] [blame] | 566 | assert( p!=0 ); |
| 567 | if( p->aOp ){ |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 568 | assert( p->nOp>0 ); |
| 569 | p->aOp[p->nOp-1].p5 = val; |
danielk1977 | 1f4aa33 | 2008-01-03 09:51:55 +0000 | [diff] [blame] | 570 | } |
| 571 | } |
| 572 | |
| 573 | /* |
drh | f887540 | 2006-03-17 13:56:34 +0000 | [diff] [blame] | 574 | ** Change the P2 operand of instruction addr so that it points to |
drh | d654be8 | 2005-09-20 17:42:23 +0000 | [diff] [blame] | 575 | ** the address of the next instruction to be coded. |
| 576 | */ |
| 577 | void sqlite3VdbeJumpHere(Vdbe *p, int addr){ |
drh | 8c2cd5d | 2011-08-16 02:07:04 +0000 | [diff] [blame] | 578 | assert( addr>=0 || p->db->mallocFailed ); |
| 579 | if( addr>=0 ) sqlite3VdbeChangeP2(p, addr, p->nOp); |
drh | d654be8 | 2005-09-20 17:42:23 +0000 | [diff] [blame] | 580 | } |
drh | b38ad99 | 2005-09-16 00:27:01 +0000 | [diff] [blame] | 581 | |
drh | b7f6f68 | 2006-07-08 17:06:43 +0000 | [diff] [blame] | 582 | |
| 583 | /* |
| 584 | ** If the input FuncDef structure is ephemeral, then free it. If |
| 585 | ** the FuncDef is not ephermal, then do nothing. |
| 586 | */ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 587 | static void freeEphemeralFunction(sqlite3 *db, FuncDef *pDef){ |
drh | 7b74603 | 2009-06-26 12:15:22 +0000 | [diff] [blame] | 588 | if( ALWAYS(pDef) && (pDef->flags & SQLITE_FUNC_EPHEM)!=0 ){ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 589 | sqlite3DbFree(db, pDef); |
drh | b7f6f68 | 2006-07-08 17:06:43 +0000 | [diff] [blame] | 590 | } |
| 591 | } |
| 592 | |
dan | d46def7 | 2010-07-24 11:28:28 +0000 | [diff] [blame] | 593 | static void vdbeFreeOpArray(sqlite3 *, Op *, int); |
| 594 | |
drh | b38ad99 | 2005-09-16 00:27:01 +0000 | [diff] [blame] | 595 | /* |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 596 | ** Delete a P4 value if necessary. |
drh | b38ad99 | 2005-09-16 00:27:01 +0000 | [diff] [blame] | 597 | */ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 598 | static void freeP4(sqlite3 *db, int p4type, void *p4){ |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 599 | if( p4 ){ |
dan | d46def7 | 2010-07-24 11:28:28 +0000 | [diff] [blame] | 600 | assert( db ); |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 601 | switch( p4type ){ |
| 602 | case P4_REAL: |
| 603 | case P4_INT64: |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 604 | case P4_DYNAMIC: |
| 605 | case P4_KEYINFO: |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 606 | case P4_INTARRAY: |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 607 | case P4_KEYINFO_HANDOFF: { |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 608 | sqlite3DbFree(db, p4); |
drh | ac1733d | 2005-09-17 17:58:22 +0000 | [diff] [blame] | 609 | break; |
| 610 | } |
drh | b975598 | 2010-07-24 16:34:37 +0000 | [diff] [blame] | 611 | case P4_MPRINTF: { |
drh | 7043db9 | 2010-07-26 12:38:12 +0000 | [diff] [blame] | 612 | if( db->pnBytesFreed==0 ) sqlite3_free(p4); |
drh | b975598 | 2010-07-24 16:34:37 +0000 | [diff] [blame] | 613 | break; |
| 614 | } |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 615 | case P4_VDBEFUNC: { |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 616 | VdbeFunc *pVdbeFunc = (VdbeFunc *)p4; |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 617 | freeEphemeralFunction(db, pVdbeFunc->pFunc); |
dan | d46def7 | 2010-07-24 11:28:28 +0000 | [diff] [blame] | 618 | if( db->pnBytesFreed==0 ) sqlite3VdbeDeleteAuxData(pVdbeFunc, 0); |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 619 | sqlite3DbFree(db, pVdbeFunc); |
drh | ac1733d | 2005-09-17 17:58:22 +0000 | [diff] [blame] | 620 | break; |
| 621 | } |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 622 | case P4_FUNCDEF: { |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 623 | freeEphemeralFunction(db, (FuncDef*)p4); |
drh | b7f6f68 | 2006-07-08 17:06:43 +0000 | [diff] [blame] | 624 | break; |
| 625 | } |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 626 | case P4_MEM: { |
drh | c176c27 | 2010-07-26 13:57:59 +0000 | [diff] [blame] | 627 | if( db->pnBytesFreed==0 ){ |
| 628 | sqlite3ValueFree((sqlite3_value*)p4); |
| 629 | }else{ |
drh | f37c68e | 2010-07-26 14:20:06 +0000 | [diff] [blame] | 630 | Mem *p = (Mem*)p4; |
| 631 | sqlite3DbFree(db, p->zMalloc); |
| 632 | sqlite3DbFree(db, p); |
drh | c176c27 | 2010-07-26 13:57:59 +0000 | [diff] [blame] | 633 | } |
drh | ac1733d | 2005-09-17 17:58:22 +0000 | [diff] [blame] | 634 | break; |
| 635 | } |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 636 | case P4_VTAB : { |
dan | d46def7 | 2010-07-24 11:28:28 +0000 | [diff] [blame] | 637 | if( db->pnBytesFreed==0 ) sqlite3VtabUnlock((VTable *)p4); |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 638 | break; |
| 639 | } |
drh | b38ad99 | 2005-09-16 00:27:01 +0000 | [diff] [blame] | 640 | } |
| 641 | } |
| 642 | } |
| 643 | |
dan | 65a7cd1 | 2009-09-01 12:16:01 +0000 | [diff] [blame] | 644 | /* |
| 645 | ** Free the space allocated for aOp and any p4 values allocated for the |
| 646 | ** opcodes contained within. If aOp is not NULL it is assumed to contain |
| 647 | ** nOp entries. |
| 648 | */ |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 649 | static void vdbeFreeOpArray(sqlite3 *db, Op *aOp, int nOp){ |
| 650 | if( aOp ){ |
| 651 | Op *pOp; |
| 652 | for(pOp=aOp; pOp<&aOp[nOp]; pOp++){ |
| 653 | freeP4(db, pOp->p4type, pOp->p4.p); |
| 654 | #ifdef SQLITE_DEBUG |
| 655 | sqlite3DbFree(db, pOp->zComment); |
| 656 | #endif |
| 657 | } |
| 658 | } |
| 659 | sqlite3DbFree(db, aOp); |
| 660 | } |
| 661 | |
dan | 65a7cd1 | 2009-09-01 12:16:01 +0000 | [diff] [blame] | 662 | /* |
dan | d19c933 | 2010-07-26 12:05:17 +0000 | [diff] [blame] | 663 | ** Link the SubProgram object passed as the second argument into the linked |
| 664 | ** list at Vdbe.pSubProgram. This list is used to delete all sub-program |
| 665 | ** objects when the VM is no longer required. |
dan | 65a7cd1 | 2009-09-01 12:16:01 +0000 | [diff] [blame] | 666 | */ |
dan | d19c933 | 2010-07-26 12:05:17 +0000 | [diff] [blame] | 667 | void sqlite3VdbeLinkSubProgram(Vdbe *pVdbe, SubProgram *p){ |
| 668 | p->pNext = pVdbe->pProgram; |
| 669 | pVdbe->pProgram = p; |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 670 | } |
| 671 | |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 672 | /* |
drh | 48f2d3b | 2011-09-16 01:34:43 +0000 | [diff] [blame] | 673 | ** Change the opcode at addr into OP_Noop |
drh | f887540 | 2006-03-17 13:56:34 +0000 | [diff] [blame] | 674 | */ |
drh | 48f2d3b | 2011-09-16 01:34:43 +0000 | [diff] [blame] | 675 | void sqlite3VdbeChangeToNoop(Vdbe *p, int addr){ |
drh | 7b74603 | 2009-06-26 12:15:22 +0000 | [diff] [blame] | 676 | if( p->aOp ){ |
danielk1977 | 92d4d7a | 2007-05-04 12:05:56 +0000 | [diff] [blame] | 677 | VdbeOp *pOp = &p->aOp[addr]; |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 678 | sqlite3 *db = p->db; |
drh | 48f2d3b | 2011-09-16 01:34:43 +0000 | [diff] [blame] | 679 | freeP4(db, pOp->p4type, pOp->p4.p); |
| 680 | memset(pOp, 0, sizeof(pOp[0])); |
| 681 | pOp->opcode = OP_Noop; |
drh | f887540 | 2006-03-17 13:56:34 +0000 | [diff] [blame] | 682 | } |
| 683 | } |
| 684 | |
| 685 | /* |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 686 | ** Change the value of the P4 operand for a specific instruction. |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 687 | ** This routine is useful when a large program is loaded from a |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 688 | ** static array using sqlite3VdbeAddOpList but we want to make a |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 689 | ** few minor changes to the program. |
| 690 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 691 | ** 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] | 692 | ** the string is made into memory obtained from sqlite3_malloc(). |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 693 | ** A value of n==0 means copy bytes of zP4 up to and including the |
| 694 | ** first null byte. If n>0 then copy n+1 bytes of zP4. |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 695 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 696 | ** 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] | 697 | ** A copy is made of the KeyInfo structure into memory obtained from |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 698 | ** sqlite3_malloc, to be freed when the Vdbe is finalized. |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 699 | ** n==P4_KEYINFO_HANDOFF indicates that zP4 points to a KeyInfo structure |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 700 | ** stored in memory that the caller has obtained from sqlite3_malloc. The |
danielk1977 | 1f55c05 | 2005-05-19 08:42:59 +0000 | [diff] [blame] | 701 | ** caller should not free the allocation, it will be freed when the Vdbe is |
| 702 | ** finalized. |
| 703 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 704 | ** Other values of n (P4_STATIC, P4_COLLSEQ etc.) indicate that zP4 points |
danielk1977 | 1f55c05 | 2005-05-19 08:42:59 +0000 | [diff] [blame] | 705 | ** to a string or structure that is guaranteed to exist for the lifetime of |
| 706 | ** the Vdbe. In these cases we can just copy the pointer. |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 707 | ** |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 708 | ** If addr<0 then change P4 on the most recently inserted instruction. |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 709 | */ |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 710 | void sqlite3VdbeChangeP4(Vdbe *p, int addr, const char *zP4, int n){ |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 711 | Op *pOp; |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 712 | sqlite3 *db; |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 713 | assert( p!=0 ); |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 714 | db = p->db; |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 715 | assert( p->magic==VDBE_MAGIC_INIT ); |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 716 | if( p->aOp==0 || db->mallocFailed ){ |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 717 | if ( n!=P4_KEYINFO && n!=P4_VTAB ) { |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 718 | freeP4(db, n, (void*)*(char**)&zP4); |
danielk1977 | 261919c | 2005-12-06 12:52:59 +0000 | [diff] [blame] | 719 | } |
danielk1977 | d5d5652 | 2005-03-16 12:15:20 +0000 | [diff] [blame] | 720 | return; |
| 721 | } |
drh | 7b74603 | 2009-06-26 12:15:22 +0000 | [diff] [blame] | 722 | assert( p->nOp>0 ); |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 723 | assert( addr<p->nOp ); |
| 724 | if( addr<0 ){ |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 725 | addr = p->nOp - 1; |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 726 | } |
| 727 | pOp = &p->aOp[addr]; |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 728 | freeP4(db, pOp->p4type, pOp->p4.p); |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 729 | pOp->p4.p = 0; |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 730 | if( n==P4_INT32 ){ |
mlcreech | 12d4082 | 2008-03-06 07:35:21 +0000 | [diff] [blame] | 731 | /* Note: this cast is safe, because the origin data point was an int |
| 732 | ** that was cast to a (const char *). */ |
shane | 1fc4129 | 2008-07-08 22:28:48 +0000 | [diff] [blame] | 733 | pOp->p4.i = SQLITE_PTR_TO_INT(zP4); |
drh | 8df3284 | 2008-12-09 02:51:23 +0000 | [diff] [blame] | 734 | pOp->p4type = P4_INT32; |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 735 | }else if( zP4==0 ){ |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 736 | pOp->p4.p = 0; |
| 737 | pOp->p4type = P4_NOTUSED; |
| 738 | }else if( n==P4_KEYINFO ){ |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 739 | KeyInfo *pKeyInfo; |
| 740 | int nField, nByte; |
drh | 4db38a7 | 2005-09-01 12:16:28 +0000 | [diff] [blame] | 741 | |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 742 | nField = ((KeyInfo*)zP4)->nField; |
drh | fdd6e85 | 2005-12-16 01:06:16 +0000 | [diff] [blame] | 743 | nByte = sizeof(*pKeyInfo) + (nField-1)*sizeof(pKeyInfo->aColl[0]) + nField; |
drh | b975598 | 2010-07-24 16:34:37 +0000 | [diff] [blame] | 744 | pKeyInfo = sqlite3DbMallocRaw(0, nByte); |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 745 | pOp->p4.pKeyInfo = pKeyInfo; |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 746 | if( pKeyInfo ){ |
drh | b21e7c7 | 2008-06-22 12:37:57 +0000 | [diff] [blame] | 747 | u8 *aSortOrder; |
drh | a378c56 | 2010-04-02 12:55:38 +0000 | [diff] [blame] | 748 | memcpy((char*)pKeyInfo, zP4, nByte - nField); |
drh | fdd6e85 | 2005-12-16 01:06:16 +0000 | [diff] [blame] | 749 | aSortOrder = pKeyInfo->aSortOrder; |
| 750 | if( aSortOrder ){ |
danielk1977 | bab45c6 | 2006-01-16 15:14:27 +0000 | [diff] [blame] | 751 | pKeyInfo->aSortOrder = (unsigned char*)&pKeyInfo->aColl[nField]; |
drh | fdd6e85 | 2005-12-16 01:06:16 +0000 | [diff] [blame] | 752 | memcpy(pKeyInfo->aSortOrder, aSortOrder, nField); |
| 753 | } |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 754 | pOp->p4type = P4_KEYINFO; |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 755 | }else{ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 756 | p->db->mallocFailed = 1; |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 757 | pOp->p4type = P4_NOTUSED; |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 758 | } |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 759 | }else if( n==P4_KEYINFO_HANDOFF ){ |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 760 | pOp->p4.p = (void*)zP4; |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 761 | pOp->p4type = P4_KEYINFO; |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 762 | }else if( n==P4_VTAB ){ |
| 763 | pOp->p4.p = (void*)zP4; |
| 764 | pOp->p4type = P4_VTAB; |
| 765 | sqlite3VtabLock((VTable *)zP4); |
| 766 | assert( ((VTable *)zP4)->db==p->db ); |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 767 | }else if( n<0 ){ |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 768 | pOp->p4.p = (void*)zP4; |
drh | 8df3284 | 2008-12-09 02:51:23 +0000 | [diff] [blame] | 769 | pOp->p4type = (signed char)n; |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 770 | }else{ |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 771 | if( n==0 ) n = sqlite3Strlen30(zP4); |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 772 | pOp->p4.z = sqlite3DbStrNDup(p->db, zP4, n); |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 773 | pOp->p4type = P4_DYNAMIC; |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 774 | } |
| 775 | } |
| 776 | |
drh | ad6d946 | 2004-09-19 02:15:24 +0000 | [diff] [blame] | 777 | #ifndef NDEBUG |
| 778 | /* |
drh | 16ee60f | 2008-06-20 18:13:25 +0000 | [diff] [blame] | 779 | ** Change the comment on the the most recently coded instruction. Or |
| 780 | ** insert a No-op and add the comment to that new instruction. This |
| 781 | ** makes the code easier to read during debugging. None of this happens |
| 782 | ** in a production build. |
drh | ad6d946 | 2004-09-19 02:15:24 +0000 | [diff] [blame] | 783 | */ |
drh | b07028f | 2011-10-14 21:49:18 +0000 | [diff] [blame^] | 784 | static void vdbeVComment(Vdbe *p, const char *zFormat, va_list ap){ |
danielk1977 | 0125683 | 2007-04-18 14:24:32 +0000 | [diff] [blame] | 785 | assert( p->nOp>0 || p->aOp==0 ); |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 786 | 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] | 787 | if( p->nOp ){ |
drh | b07028f | 2011-10-14 21:49:18 +0000 | [diff] [blame^] | 788 | assert( p->aOp ); |
| 789 | sqlite3DbFree(p->db, p->aOp[p->nOp-1].zComment); |
| 790 | p->aOp[p->nOp-1].zComment = sqlite3VMPrintf(p->db, zFormat, ap); |
| 791 | } |
| 792 | } |
| 793 | void sqlite3VdbeComment(Vdbe *p, const char *zFormat, ...){ |
| 794 | va_list ap; |
| 795 | if( p ){ |
danielk1977 | dba0137 | 2008-01-05 18:44:29 +0000 | [diff] [blame] | 796 | va_start(ap, zFormat); |
drh | b07028f | 2011-10-14 21:49:18 +0000 | [diff] [blame^] | 797 | vdbeVComment(p, zFormat, ap); |
danielk1977 | dba0137 | 2008-01-05 18:44:29 +0000 | [diff] [blame] | 798 | va_end(ap); |
| 799 | } |
drh | ad6d946 | 2004-09-19 02:15:24 +0000 | [diff] [blame] | 800 | } |
drh | 16ee60f | 2008-06-20 18:13:25 +0000 | [diff] [blame] | 801 | void sqlite3VdbeNoopComment(Vdbe *p, const char *zFormat, ...){ |
| 802 | va_list ap; |
drh | b07028f | 2011-10-14 21:49:18 +0000 | [diff] [blame^] | 803 | if( p ){ |
| 804 | sqlite3VdbeAddOp0(p, OP_Noop); |
drh | 16ee60f | 2008-06-20 18:13:25 +0000 | [diff] [blame] | 805 | va_start(ap, zFormat); |
drh | b07028f | 2011-10-14 21:49:18 +0000 | [diff] [blame^] | 806 | vdbeVComment(p, zFormat, ap); |
drh | 16ee60f | 2008-06-20 18:13:25 +0000 | [diff] [blame] | 807 | va_end(ap); |
| 808 | } |
| 809 | } |
| 810 | #endif /* NDEBUG */ |
drh | ad6d946 | 2004-09-19 02:15:24 +0000 | [diff] [blame] | 811 | |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 812 | /* |
drh | 20411ea | 2009-05-29 19:00:12 +0000 | [diff] [blame] | 813 | ** Return the opcode for a given address. If the address is -1, then |
| 814 | ** return the most recently inserted opcode. |
| 815 | ** |
| 816 | ** If a memory allocation error has occurred prior to the calling of this |
| 817 | ** routine, then a pointer to a dummy VdbeOp will be returned. That opcode |
drh | f83dc1e | 2010-06-03 12:09:52 +0000 | [diff] [blame] | 818 | ** is readable but not writable, though it is cast to a writable value. |
| 819 | ** The return of a dummy opcode allows the call to continue functioning |
| 820 | ** after a OOM fault without having to check to see if the return from |
| 821 | ** this routine is a valid pointer. But because the dummy.opcode is 0, |
| 822 | ** dummy will never be written to. This is verified by code inspection and |
| 823 | ** by running with Valgrind. |
drh | 37b89a0 | 2009-06-19 00:33:31 +0000 | [diff] [blame] | 824 | ** |
| 825 | ** About the #ifdef SQLITE_OMIT_TRACE: Normally, this routine is never called |
| 826 | ** unless p->nOp>0. This is because in the absense of SQLITE_OMIT_TRACE, |
| 827 | ** an OP_Trace instruction is always inserted by sqlite3VdbeGet() as soon as |
| 828 | ** a new VDBE is created. So we are free to set addr to p->nOp-1 without |
| 829 | ** having to double-check to make sure that the result is non-negative. But |
| 830 | ** if SQLITE_OMIT_TRACE is defined, the OP_Trace is omitted and we do need to |
| 831 | ** check the value of p->nOp-1 before continuing. |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 832 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 833 | VdbeOp *sqlite3VdbeGetOp(Vdbe *p, int addr){ |
drh | a0b75da | 2010-07-02 18:44:37 +0000 | [diff] [blame] | 834 | /* C89 specifies that the constant "dummy" will be initialized to all |
| 835 | ** zeros, which is correct. MSVC generates a warning, nevertheless. */ |
mistachkin | 0fe5f95 | 2011-09-14 18:19:08 +0000 | [diff] [blame] | 836 | static VdbeOp dummy; /* Ignore the MSVC warning about no initializer */ |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 837 | assert( p->magic==VDBE_MAGIC_INIT ); |
drh | 37b89a0 | 2009-06-19 00:33:31 +0000 | [diff] [blame] | 838 | if( addr<0 ){ |
| 839 | #ifdef SQLITE_OMIT_TRACE |
drh | f83dc1e | 2010-06-03 12:09:52 +0000 | [diff] [blame] | 840 | if( p->nOp==0 ) return (VdbeOp*)&dummy; |
drh | 37b89a0 | 2009-06-19 00:33:31 +0000 | [diff] [blame] | 841 | #endif |
| 842 | addr = p->nOp - 1; |
| 843 | } |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 844 | assert( (addr>=0 && addr<p->nOp) || p->db->mallocFailed ); |
drh | 20411ea | 2009-05-29 19:00:12 +0000 | [diff] [blame] | 845 | if( p->db->mallocFailed ){ |
drh | f83dc1e | 2010-06-03 12:09:52 +0000 | [diff] [blame] | 846 | return (VdbeOp*)&dummy; |
drh | 20411ea | 2009-05-29 19:00:12 +0000 | [diff] [blame] | 847 | }else{ |
| 848 | return &p->aOp[addr]; |
| 849 | } |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 850 | } |
| 851 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 852 | #if !defined(SQLITE_OMIT_EXPLAIN) || !defined(NDEBUG) \ |
| 853 | || defined(VDBE_PROFILE) || defined(SQLITE_DEBUG) |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 854 | /* |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 855 | ** Compute a string that describes the P4 parameter for an opcode. |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 856 | ** Use zTemp for any required temporary buffer space. |
| 857 | */ |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 858 | static char *displayP4(Op *pOp, char *zTemp, int nTemp){ |
| 859 | char *zP4 = zTemp; |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 860 | assert( nTemp>=20 ); |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 861 | switch( pOp->p4type ){ |
drh | 16ee60f | 2008-06-20 18:13:25 +0000 | [diff] [blame] | 862 | case P4_KEYINFO_STATIC: |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 863 | case P4_KEYINFO: { |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 864 | int i, j; |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 865 | KeyInfo *pKeyInfo = pOp->p4.pKeyInfo; |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 866 | sqlite3_snprintf(nTemp, zTemp, "keyinfo(%d", pKeyInfo->nField); |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 867 | i = sqlite3Strlen30(zTemp); |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 868 | for(j=0; j<pKeyInfo->nField; j++){ |
| 869 | CollSeq *pColl = pKeyInfo->aColl[j]; |
| 870 | if( pColl ){ |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 871 | int n = sqlite3Strlen30(pColl->zName); |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 872 | if( i+n>nTemp-6 ){ |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 873 | memcpy(&zTemp[i],",...",4); |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 874 | break; |
| 875 | } |
| 876 | zTemp[i++] = ','; |
drh | ffbc308 | 2004-05-21 01:29:06 +0000 | [diff] [blame] | 877 | if( pKeyInfo->aSortOrder && pKeyInfo->aSortOrder[j] ){ |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 878 | zTemp[i++] = '-'; |
| 879 | } |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 880 | memcpy(&zTemp[i], pColl->zName,n+1); |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 881 | i += n; |
| 882 | }else if( i+4<nTemp-6 ){ |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 883 | memcpy(&zTemp[i],",nil",4); |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 884 | i += 4; |
| 885 | } |
| 886 | } |
| 887 | zTemp[i++] = ')'; |
| 888 | zTemp[i] = 0; |
| 889 | assert( i<nTemp ); |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 890 | break; |
| 891 | } |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 892 | case P4_COLLSEQ: { |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 893 | CollSeq *pColl = pOp->p4.pColl; |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 894 | sqlite3_snprintf(nTemp, zTemp, "collseq(%.20s)", pColl->zName); |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 895 | break; |
| 896 | } |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 897 | case P4_FUNCDEF: { |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 898 | FuncDef *pDef = pOp->p4.pFunc; |
drh | a967e88 | 2006-06-13 01:04:52 +0000 | [diff] [blame] | 899 | sqlite3_snprintf(nTemp, zTemp, "%s(%d)", pDef->zName, pDef->nArg); |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 900 | break; |
| 901 | } |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 902 | case P4_INT64: { |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 903 | sqlite3_snprintf(nTemp, zTemp, "%lld", *pOp->p4.pI64); |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 904 | break; |
| 905 | } |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 906 | case P4_INT32: { |
| 907 | sqlite3_snprintf(nTemp, zTemp, "%d", pOp->p4.i); |
drh | 598f134 | 2007-10-23 15:39:45 +0000 | [diff] [blame] | 908 | break; |
| 909 | } |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 910 | case P4_REAL: { |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 911 | sqlite3_snprintf(nTemp, zTemp, "%.16g", *pOp->p4.pReal); |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 912 | break; |
| 913 | } |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 914 | case P4_MEM: { |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 915 | Mem *pMem = pOp->p4.pMem; |
drh | c4dd3fd | 2008-01-22 01:48:05 +0000 | [diff] [blame] | 916 | assert( (pMem->flags & MEM_Null)==0 ); |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 917 | if( pMem->flags & MEM_Str ){ |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 918 | zP4 = pMem->z; |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 919 | }else if( pMem->flags & MEM_Int ){ |
| 920 | sqlite3_snprintf(nTemp, zTemp, "%lld", pMem->u.i); |
| 921 | }else if( pMem->flags & MEM_Real ){ |
| 922 | sqlite3_snprintf(nTemp, zTemp, "%.16g", pMem->r); |
drh | 5601689 | 2009-08-25 14:24:04 +0000 | [diff] [blame] | 923 | }else{ |
| 924 | assert( pMem->flags & MEM_Blob ); |
| 925 | zP4 = "(blob)"; |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 926 | } |
drh | 598f134 | 2007-10-23 15:39:45 +0000 | [diff] [blame] | 927 | break; |
| 928 | } |
drh | a967e88 | 2006-06-13 01:04:52 +0000 | [diff] [blame] | 929 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 930 | case P4_VTAB: { |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 931 | sqlite3_vtab *pVtab = pOp->p4.pVtab->pVtab; |
drh | 1914619 | 2006-06-26 19:10:32 +0000 | [diff] [blame] | 932 | sqlite3_snprintf(nTemp, zTemp, "vtab:%p:%p", pVtab, pVtab->pModule); |
drh | a967e88 | 2006-06-13 01:04:52 +0000 | [diff] [blame] | 933 | break; |
| 934 | } |
| 935 | #endif |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 936 | case P4_INTARRAY: { |
| 937 | sqlite3_snprintf(nTemp, zTemp, "intarray"); |
| 938 | break; |
| 939 | } |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 940 | case P4_SUBPROGRAM: { |
| 941 | sqlite3_snprintf(nTemp, zTemp, "program"); |
| 942 | break; |
| 943 | } |
drh | 4a6f3aa | 2011-08-28 00:19:26 +0000 | [diff] [blame] | 944 | case P4_ADVANCE: { |
| 945 | zTemp[0] = 0; |
| 946 | break; |
| 947 | } |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 948 | default: { |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 949 | zP4 = pOp->p4.z; |
drh | 949f9cd | 2008-01-12 21:35:57 +0000 | [diff] [blame] | 950 | if( zP4==0 ){ |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 951 | zP4 = zTemp; |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 952 | zTemp[0] = 0; |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 953 | } |
| 954 | } |
| 955 | } |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 956 | assert( zP4!=0 ); |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 957 | return zP4; |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 958 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 959 | #endif |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 960 | |
drh | 900b31e | 2007-08-28 02:27:51 +0000 | [diff] [blame] | 961 | /* |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 962 | ** Declare to the Vdbe that the BTree object at db->aDb[i] is used. |
drh | 3ebaee9 | 2010-05-06 21:37:22 +0000 | [diff] [blame] | 963 | ** |
drh | bdaec52 | 2011-04-04 00:14:43 +0000 | [diff] [blame] | 964 | ** The prepared statements need to know in advance the complete set of |
| 965 | ** attached databases that they will be using. A mask of these databases |
| 966 | ** is maintained in p->btreeMask and is used for locking and other purposes. |
drh | 900b31e | 2007-08-28 02:27:51 +0000 | [diff] [blame] | 967 | */ |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 968 | void sqlite3VdbeUsesBtree(Vdbe *p, int i){ |
drh | fcd71b6 | 2011-04-05 22:08:24 +0000 | [diff] [blame] | 969 | assert( i>=0 && i<p->db->nDb && i<(int)sizeof(yDbMask)*8 ); |
danielk1977 | 00e1361 | 2008-11-17 19:18:54 +0000 | [diff] [blame] | 970 | assert( i<(int)sizeof(p->btreeMask)*8 ); |
drh | bdaec52 | 2011-04-04 00:14:43 +0000 | [diff] [blame] | 971 | p->btreeMask |= ((yDbMask)1)<<i; |
drh | dc5b047 | 2011-04-06 22:05:53 +0000 | [diff] [blame] | 972 | if( i!=1 && sqlite3BtreeSharable(p->db->aDb[i].pBt) ){ |
| 973 | p->lockMask |= ((yDbMask)1)<<i; |
| 974 | } |
drh | 900b31e | 2007-08-28 02:27:51 +0000 | [diff] [blame] | 975 | } |
| 976 | |
drh | e54e051 | 2011-04-05 17:31:56 +0000 | [diff] [blame] | 977 | #if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE>0 |
drh | bdaec52 | 2011-04-04 00:14:43 +0000 | [diff] [blame] | 978 | /* |
| 979 | ** If SQLite is compiled to support shared-cache mode and to be threadsafe, |
| 980 | ** this routine obtains the mutex associated with each BtShared structure |
| 981 | ** that may be accessed by the VM passed as an argument. In doing so it also |
| 982 | ** sets the BtShared.db member of each of the BtShared structures, ensuring |
| 983 | ** that the correct busy-handler callback is invoked if required. |
| 984 | ** |
| 985 | ** If SQLite is not threadsafe but does support shared-cache mode, then |
| 986 | ** sqlite3BtreeEnter() is invoked to set the BtShared.db variables |
| 987 | ** of all of BtShared structures accessible via the database handle |
| 988 | ** associated with the VM. |
| 989 | ** |
| 990 | ** If SQLite is not threadsafe and does not support shared-cache mode, this |
| 991 | ** function is a no-op. |
| 992 | ** |
| 993 | ** The p->btreeMask field is a bitmask of all btrees that the prepared |
| 994 | ** statement p will ever use. Let N be the number of bits in p->btreeMask |
| 995 | ** corresponding to btrees that use shared cache. Then the runtime of |
| 996 | ** this routine is N*N. But as N is rarely more than 1, this should not |
| 997 | ** be a problem. |
| 998 | */ |
| 999 | void sqlite3VdbeEnter(Vdbe *p){ |
drh | bdaec52 | 2011-04-04 00:14:43 +0000 | [diff] [blame] | 1000 | int i; |
| 1001 | yDbMask mask; |
drh | dc5b047 | 2011-04-06 22:05:53 +0000 | [diff] [blame] | 1002 | sqlite3 *db; |
| 1003 | Db *aDb; |
| 1004 | int nDb; |
| 1005 | if( p->lockMask==0 ) return; /* The common case */ |
| 1006 | db = p->db; |
| 1007 | aDb = db->aDb; |
| 1008 | nDb = db->nDb; |
drh | bdaec52 | 2011-04-04 00:14:43 +0000 | [diff] [blame] | 1009 | for(i=0, mask=1; i<nDb; i++, mask += mask){ |
drh | dc5b047 | 2011-04-06 22:05:53 +0000 | [diff] [blame] | 1010 | if( i!=1 && (mask & p->lockMask)!=0 && ALWAYS(aDb[i].pBt!=0) ){ |
drh | bdaec52 | 2011-04-04 00:14:43 +0000 | [diff] [blame] | 1011 | sqlite3BtreeEnter(aDb[i].pBt); |
| 1012 | } |
| 1013 | } |
drh | bdaec52 | 2011-04-04 00:14:43 +0000 | [diff] [blame] | 1014 | } |
drh | e54e051 | 2011-04-05 17:31:56 +0000 | [diff] [blame] | 1015 | #endif |
drh | bdaec52 | 2011-04-04 00:14:43 +0000 | [diff] [blame] | 1016 | |
drh | e54e051 | 2011-04-05 17:31:56 +0000 | [diff] [blame] | 1017 | #if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE>0 |
drh | bdaec52 | 2011-04-04 00:14:43 +0000 | [diff] [blame] | 1018 | /* |
| 1019 | ** Unlock all of the btrees previously locked by a call to sqlite3VdbeEnter(). |
| 1020 | */ |
| 1021 | void sqlite3VdbeLeave(Vdbe *p){ |
drh | bdaec52 | 2011-04-04 00:14:43 +0000 | [diff] [blame] | 1022 | int i; |
| 1023 | yDbMask mask; |
drh | dc5b047 | 2011-04-06 22:05:53 +0000 | [diff] [blame] | 1024 | sqlite3 *db; |
| 1025 | Db *aDb; |
| 1026 | int nDb; |
| 1027 | if( p->lockMask==0 ) return; /* The common case */ |
| 1028 | db = p->db; |
| 1029 | aDb = db->aDb; |
| 1030 | nDb = db->nDb; |
drh | bdaec52 | 2011-04-04 00:14:43 +0000 | [diff] [blame] | 1031 | for(i=0, mask=1; i<nDb; i++, mask += mask){ |
drh | dc5b047 | 2011-04-06 22:05:53 +0000 | [diff] [blame] | 1032 | if( i!=1 && (mask & p->lockMask)!=0 && ALWAYS(aDb[i].pBt!=0) ){ |
drh | bdaec52 | 2011-04-04 00:14:43 +0000 | [diff] [blame] | 1033 | sqlite3BtreeLeave(aDb[i].pBt); |
| 1034 | } |
| 1035 | } |
drh | bdaec52 | 2011-04-04 00:14:43 +0000 | [diff] [blame] | 1036 | } |
drh | bdaec52 | 2011-04-04 00:14:43 +0000 | [diff] [blame] | 1037 | #endif |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 1038 | |
danielk1977 | 8b60e0f | 2005-01-12 09:10:39 +0000 | [diff] [blame] | 1039 | #if defined(VDBE_PROFILE) || defined(SQLITE_DEBUG) |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 1040 | /* |
| 1041 | ** Print a single opcode. This routine is used for debugging only. |
| 1042 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1043 | void sqlite3VdbePrintOp(FILE *pOut, int pc, Op *pOp){ |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 1044 | char *zP4; |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 1045 | char zPtr[50]; |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 1046 | static const char *zFormat1 = "%4d %-13s %4d %4d %4d %-4s %.2X %s\n"; |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 1047 | if( pOut==0 ) pOut = stdout; |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 1048 | zP4 = displayP4(pOp, zPtr, sizeof(zPtr)); |
danielk1977 | 11641c1 | 2008-01-03 08:18:30 +0000 | [diff] [blame] | 1049 | fprintf(pOut, zFormat1, pc, |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 1050 | sqlite3OpcodeName(pOp->opcode), pOp->p1, pOp->p2, pOp->p3, zP4, pOp->p5, |
| 1051 | #ifdef SQLITE_DEBUG |
| 1052 | pOp->zComment ? pOp->zComment : "" |
| 1053 | #else |
| 1054 | "" |
| 1055 | #endif |
| 1056 | ); |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 1057 | fflush(pOut); |
| 1058 | } |
| 1059 | #endif |
| 1060 | |
| 1061 | /* |
drh | 76ff3a0 | 2004-09-24 22:32:30 +0000 | [diff] [blame] | 1062 | ** Release an array of N Mem elements |
| 1063 | */ |
drh | c890fec | 2008-08-01 20:10:08 +0000 | [diff] [blame] | 1064 | static void releaseMemArray(Mem *p, int N){ |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1065 | if( p && N ){ |
danielk1977 | e972e03 | 2008-09-19 18:32:26 +0000 | [diff] [blame] | 1066 | Mem *pEnd; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1067 | sqlite3 *db = p->db; |
drh | 8df3284 | 2008-12-09 02:51:23 +0000 | [diff] [blame] | 1068 | u8 malloc_failed = db->mallocFailed; |
dan | d46def7 | 2010-07-24 11:28:28 +0000 | [diff] [blame] | 1069 | if( db->pnBytesFreed ){ |
| 1070 | for(pEnd=&p[N]; p<pEnd; p++){ |
| 1071 | sqlite3DbFree(db, p->zMalloc); |
| 1072 | } |
drh | c176c27 | 2010-07-26 13:57:59 +0000 | [diff] [blame] | 1073 | return; |
| 1074 | } |
danielk1977 | e972e03 | 2008-09-19 18:32:26 +0000 | [diff] [blame] | 1075 | for(pEnd=&p[N]; p<pEnd; p++){ |
| 1076 | assert( (&p[1])==pEnd || p[0].db==p[1].db ); |
| 1077 | |
| 1078 | /* This block is really an inlined version of sqlite3VdbeMemRelease() |
| 1079 | ** that takes advantage of the fact that the memory cell value is |
| 1080 | ** being set to NULL after releasing any dynamic resources. |
| 1081 | ** |
| 1082 | ** The justification for duplicating code is that according to |
| 1083 | ** callgrind, this causes a certain test case to hit the CPU 4.7 |
| 1084 | ** percent less (x86 linux, gcc version 4.1.2, -O6) than if |
| 1085 | ** sqlite3MemRelease() were called from here. With -O2, this jumps |
| 1086 | ** to 6.6 percent. The test case is inserting 1000 rows into a table |
| 1087 | ** with no indexes using a single prepared INSERT statement, bind() |
| 1088 | ** and reset(). Inserts are grouped into a transaction. |
| 1089 | */ |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 1090 | if( p->flags&(MEM_Agg|MEM_Dyn|MEM_Frame|MEM_RowSet) ){ |
danielk1977 | e972e03 | 2008-09-19 18:32:26 +0000 | [diff] [blame] | 1091 | sqlite3VdbeMemRelease(p); |
| 1092 | }else if( p->zMalloc ){ |
| 1093 | sqlite3DbFree(db, p->zMalloc); |
| 1094 | p->zMalloc = 0; |
| 1095 | } |
| 1096 | |
danielk1977 | 5f09613 | 2008-03-28 15:44:09 +0000 | [diff] [blame] | 1097 | p->flags = MEM_Null; |
drh | 76ff3a0 | 2004-09-24 22:32:30 +0000 | [diff] [blame] | 1098 | } |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1099 | db->mallocFailed = malloc_failed; |
drh | 76ff3a0 | 2004-09-24 22:32:30 +0000 | [diff] [blame] | 1100 | } |
| 1101 | } |
| 1102 | |
dan | 65a7cd1 | 2009-09-01 12:16:01 +0000 | [diff] [blame] | 1103 | /* |
| 1104 | ** Delete a VdbeFrame object and its contents. VdbeFrame objects are |
| 1105 | ** allocated by the OP_Program opcode in sqlite3VdbeExec(). |
| 1106 | */ |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 1107 | void sqlite3VdbeFrameDelete(VdbeFrame *p){ |
| 1108 | int i; |
| 1109 | Mem *aMem = VdbeFrameMem(p); |
| 1110 | VdbeCursor **apCsr = (VdbeCursor **)&aMem[p->nChildMem]; |
| 1111 | for(i=0; i<p->nChildCsr; i++){ |
| 1112 | sqlite3VdbeFreeCursor(p->v, apCsr[i]); |
| 1113 | } |
| 1114 | releaseMemArray(aMem, p->nChildMem); |
| 1115 | sqlite3DbFree(p->v->db, p); |
| 1116 | } |
| 1117 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 1118 | #ifndef SQLITE_OMIT_EXPLAIN |
drh | 76ff3a0 | 2004-09-24 22:32:30 +0000 | [diff] [blame] | 1119 | /* |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 1120 | ** Give a listing of the program in the virtual machine. |
| 1121 | ** |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1122 | ** The interface is the same as sqlite3VdbeExec(). But instead of |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 1123 | ** running the code, it invokes the callback once for each instruction. |
| 1124 | ** This feature is used to implement "EXPLAIN". |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1125 | ** |
| 1126 | ** When p->explain==1, each instruction is listed. When |
| 1127 | ** p->explain==2, only OP_Explain instructions are listed and these |
| 1128 | ** are shown in a different format. p->explain==2 is used to implement |
| 1129 | ** EXPLAIN QUERY PLAN. |
drh | 5cfa584 | 2009-12-31 20:35:08 +0000 | [diff] [blame] | 1130 | ** |
| 1131 | ** When p->explain==1, first the main program is listed, then each of |
| 1132 | ** the trigger subprograms are listed one by one. |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 1133 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1134 | int sqlite3VdbeList( |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 1135 | Vdbe *p /* The VDBE */ |
| 1136 | ){ |
drh | 5cfa584 | 2009-12-31 20:35:08 +0000 | [diff] [blame] | 1137 | int nRow; /* Stop when row count reaches this */ |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 1138 | int nSub = 0; /* Number of sub-vdbes seen so far */ |
| 1139 | SubProgram **apSub = 0; /* Array of sub-vdbes */ |
drh | 5cfa584 | 2009-12-31 20:35:08 +0000 | [diff] [blame] | 1140 | Mem *pSub = 0; /* Memory cell hold array of subprogs */ |
| 1141 | sqlite3 *db = p->db; /* The database connection */ |
| 1142 | int i; /* Loop counter */ |
| 1143 | int rc = SQLITE_OK; /* Return code */ |
drh | 9734e6e | 2011-10-07 18:24:25 +0000 | [diff] [blame] | 1144 | Mem *pMem = &p->aMem[1]; /* First Mem of result set */ |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 1145 | |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 1146 | assert( p->explain ); |
drh | 5f82e3c | 2009-07-06 00:44:08 +0000 | [diff] [blame] | 1147 | assert( p->magic==VDBE_MAGIC_RUN ); |
danielk1977 | 6c359f0 | 2008-11-21 16:58:03 +0000 | [diff] [blame] | 1148 | assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY || p->rc==SQLITE_NOMEM ); |
danielk1977 | 18f4189 | 2004-05-22 07:27:46 +0000 | [diff] [blame] | 1149 | |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1150 | /* Even though this opcode does not use dynamic strings for |
| 1151 | ** the result, result columns may become dynamic if the user calls |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 1152 | ** sqlite3_column_text16(), causing a translation to UTF-16 encoding. |
danielk1977 | 18f4189 | 2004-05-22 07:27:46 +0000 | [diff] [blame] | 1153 | */ |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 1154 | releaseMemArray(pMem, 8); |
drh | 9734e6e | 2011-10-07 18:24:25 +0000 | [diff] [blame] | 1155 | p->pResultSet = 0; |
danielk1977 | 18f4189 | 2004-05-22 07:27:46 +0000 | [diff] [blame] | 1156 | |
danielk1977 | 6c359f0 | 2008-11-21 16:58:03 +0000 | [diff] [blame] | 1157 | if( p->rc==SQLITE_NOMEM ){ |
| 1158 | /* This happens if a malloc() inside a call to sqlite3_column_text() or |
| 1159 | ** sqlite3_column_text16() failed. */ |
| 1160 | db->mallocFailed = 1; |
| 1161 | return SQLITE_ERROR; |
| 1162 | } |
| 1163 | |
drh | 5cfa584 | 2009-12-31 20:35:08 +0000 | [diff] [blame] | 1164 | /* When the number of output rows reaches nRow, that means the |
| 1165 | ** listing has finished and sqlite3_step() should return SQLITE_DONE. |
| 1166 | ** nRow is the sum of the number of rows in the main program, plus |
| 1167 | ** the sum of the number of rows in all trigger subprograms encountered |
| 1168 | ** so far. The nRow value will increase as new trigger subprograms are |
| 1169 | ** encountered, but p->pc will eventually catch up to nRow. |
| 1170 | */ |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 1171 | nRow = p->nOp; |
| 1172 | if( p->explain==1 ){ |
drh | 5cfa584 | 2009-12-31 20:35:08 +0000 | [diff] [blame] | 1173 | /* The first 8 memory cells are used for the result set. So we will |
| 1174 | ** commandeer the 9th cell to use as storage for an array of pointers |
| 1175 | ** to trigger subprograms. The VDBE is guaranteed to have at least 9 |
| 1176 | ** cells. */ |
| 1177 | assert( p->nMem>9 ); |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 1178 | pSub = &p->aMem[9]; |
| 1179 | if( pSub->flags&MEM_Blob ){ |
drh | 5cfa584 | 2009-12-31 20:35:08 +0000 | [diff] [blame] | 1180 | /* On the first call to sqlite3_step(), pSub will hold a NULL. It is |
| 1181 | ** initialized to a BLOB by the P4_SUBPROGRAM processing logic below */ |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 1182 | nSub = pSub->n/sizeof(Vdbe*); |
| 1183 | apSub = (SubProgram **)pSub->z; |
| 1184 | } |
| 1185 | for(i=0; i<nSub; i++){ |
| 1186 | nRow += apSub[i]->nOp; |
| 1187 | } |
| 1188 | } |
| 1189 | |
drh | ecc9242 | 2005-09-10 16:46:12 +0000 | [diff] [blame] | 1190 | do{ |
| 1191 | i = p->pc++; |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 1192 | }while( i<nRow && p->explain==2 && p->aOp[i].opcode!=OP_Explain ); |
| 1193 | if( i>=nRow ){ |
drh | 826fb5a | 2004-02-14 23:59:57 +0000 | [diff] [blame] | 1194 | p->rc = SQLITE_OK; |
| 1195 | rc = SQLITE_DONE; |
drh | 881feaa | 2006-07-26 01:39:30 +0000 | [diff] [blame] | 1196 | }else if( db->u1.isInterrupted ){ |
drh | c5cdca6 | 2005-01-11 16:54:14 +0000 | [diff] [blame] | 1197 | p->rc = SQLITE_INTERRUPT; |
drh | 826fb5a | 2004-02-14 23:59:57 +0000 | [diff] [blame] | 1198 | rc = SQLITE_ERROR; |
drh | f089aa4 | 2008-07-08 19:34:06 +0000 | [diff] [blame] | 1199 | sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3ErrStr(p->rc)); |
drh | 826fb5a | 2004-02-14 23:59:57 +0000 | [diff] [blame] | 1200 | }else{ |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1201 | char *z; |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 1202 | Op *pOp; |
| 1203 | if( i<p->nOp ){ |
drh | 5cfa584 | 2009-12-31 20:35:08 +0000 | [diff] [blame] | 1204 | /* The output line number is small enough that we are still in the |
| 1205 | ** main program. */ |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 1206 | pOp = &p->aOp[i]; |
| 1207 | }else{ |
drh | 5cfa584 | 2009-12-31 20:35:08 +0000 | [diff] [blame] | 1208 | /* We are currently listing subprograms. Figure out which one and |
| 1209 | ** pick up the appropriate opcode. */ |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 1210 | int j; |
| 1211 | i -= p->nOp; |
| 1212 | for(j=0; i>=apSub[j]->nOp; j++){ |
| 1213 | i -= apSub[j]->nOp; |
| 1214 | } |
| 1215 | pOp = &apSub[j]->aOp[i]; |
| 1216 | } |
danielk1977 | 0d78bae | 2008-01-03 07:09:48 +0000 | [diff] [blame] | 1217 | if( p->explain==1 ){ |
| 1218 | pMem->flags = MEM_Int; |
| 1219 | pMem->type = SQLITE_INTEGER; |
| 1220 | pMem->u.i = i; /* Program counter */ |
| 1221 | pMem++; |
| 1222 | |
| 1223 | pMem->flags = MEM_Static|MEM_Str|MEM_Term; |
| 1224 | pMem->z = (char*)sqlite3OpcodeName(pOp->opcode); /* Opcode */ |
| 1225 | assert( pMem->z!=0 ); |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 1226 | pMem->n = sqlite3Strlen30(pMem->z); |
danielk1977 | 0d78bae | 2008-01-03 07:09:48 +0000 | [diff] [blame] | 1227 | pMem->type = SQLITE_TEXT; |
| 1228 | pMem->enc = SQLITE_UTF8; |
| 1229 | pMem++; |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 1230 | |
drh | 5cfa584 | 2009-12-31 20:35:08 +0000 | [diff] [blame] | 1231 | /* When an OP_Program opcode is encounter (the only opcode that has |
| 1232 | ** a P4_SUBPROGRAM argument), expand the size of the array of subprograms |
| 1233 | ** kept in p->aMem[9].z to hold the new program - assuming this subprogram |
| 1234 | ** has not already been seen. |
| 1235 | */ |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 1236 | if( pOp->p4type==P4_SUBPROGRAM ){ |
| 1237 | int nByte = (nSub+1)*sizeof(SubProgram*); |
| 1238 | int j; |
| 1239 | for(j=0; j<nSub; j++){ |
| 1240 | if( apSub[j]==pOp->p4.pProgram ) break; |
| 1241 | } |
| 1242 | if( j==nSub && SQLITE_OK==sqlite3VdbeMemGrow(pSub, nByte, 1) ){ |
| 1243 | apSub = (SubProgram **)pSub->z; |
| 1244 | apSub[nSub++] = pOp->p4.pProgram; |
| 1245 | pSub->flags |= MEM_Blob; |
| 1246 | pSub->n = nSub*sizeof(SubProgram*); |
| 1247 | } |
| 1248 | } |
danielk1977 | 0d78bae | 2008-01-03 07:09:48 +0000 | [diff] [blame] | 1249 | } |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 1250 | |
| 1251 | pMem->flags = MEM_Int; |
drh | 3c024d6 | 2007-03-30 11:23:45 +0000 | [diff] [blame] | 1252 | pMem->u.i = pOp->p1; /* P1 */ |
drh | 9c05483 | 2004-05-31 18:51:57 +0000 | [diff] [blame] | 1253 | pMem->type = SQLITE_INTEGER; |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 1254 | pMem++; |
| 1255 | |
| 1256 | pMem->flags = MEM_Int; |
drh | 3c024d6 | 2007-03-30 11:23:45 +0000 | [diff] [blame] | 1257 | pMem->u.i = pOp->p2; /* P2 */ |
drh | 9c05483 | 2004-05-31 18:51:57 +0000 | [diff] [blame] | 1258 | pMem->type = SQLITE_INTEGER; |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 1259 | pMem++; |
| 1260 | |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 1261 | pMem->flags = MEM_Int; |
| 1262 | pMem->u.i = pOp->p3; /* P3 */ |
| 1263 | pMem->type = SQLITE_INTEGER; |
| 1264 | pMem++; |
danielk1977 | 0d78bae | 2008-01-03 07:09:48 +0000 | [diff] [blame] | 1265 | |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1266 | if( sqlite3VdbeMemGrow(pMem, 32, 0) ){ /* P4 */ |
danielk1977 | 357864e | 2009-03-25 15:43:08 +0000 | [diff] [blame] | 1267 | assert( p->db->mallocFailed ); |
| 1268 | return SQLITE_ERROR; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1269 | } |
| 1270 | pMem->flags = MEM_Dyn|MEM_Str|MEM_Term; |
| 1271 | z = displayP4(pOp, pMem->z, 32); |
| 1272 | if( z!=pMem->z ){ |
| 1273 | sqlite3VdbeMemSetStr(pMem, z, -1, SQLITE_UTF8, 0); |
| 1274 | }else{ |
| 1275 | assert( pMem->z!=0 ); |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 1276 | pMem->n = sqlite3Strlen30(pMem->z); |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1277 | pMem->enc = SQLITE_UTF8; |
| 1278 | } |
drh | 9c05483 | 2004-05-31 18:51:57 +0000 | [diff] [blame] | 1279 | pMem->type = SQLITE_TEXT; |
danielk1977 | 0d78bae | 2008-01-03 07:09:48 +0000 | [diff] [blame] | 1280 | pMem++; |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 1281 | |
danielk1977 | 0d78bae | 2008-01-03 07:09:48 +0000 | [diff] [blame] | 1282 | if( p->explain==1 ){ |
drh | 85e5f0d | 2008-02-19 18:28:13 +0000 | [diff] [blame] | 1283 | if( sqlite3VdbeMemGrow(pMem, 4, 0) ){ |
danielk1977 | 357864e | 2009-03-25 15:43:08 +0000 | [diff] [blame] | 1284 | assert( p->db->mallocFailed ); |
| 1285 | return SQLITE_ERROR; |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1286 | } |
| 1287 | pMem->flags = MEM_Dyn|MEM_Str|MEM_Term; |
drh | 85e5f0d | 2008-02-19 18:28:13 +0000 | [diff] [blame] | 1288 | pMem->n = 2; |
| 1289 | sqlite3_snprintf(3, pMem->z, "%.2x", pOp->p5); /* P5 */ |
danielk1977 | 0d78bae | 2008-01-03 07:09:48 +0000 | [diff] [blame] | 1290 | pMem->type = SQLITE_TEXT; |
| 1291 | pMem->enc = SQLITE_UTF8; |
| 1292 | pMem++; |
| 1293 | |
drh | aa9b896 | 2008-01-08 02:57:55 +0000 | [diff] [blame] | 1294 | #ifdef SQLITE_DEBUG |
danielk1977 | 0d78bae | 2008-01-03 07:09:48 +0000 | [diff] [blame] | 1295 | if( pOp->zComment ){ |
| 1296 | pMem->flags = MEM_Str|MEM_Term; |
| 1297 | pMem->z = pOp->zComment; |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 1298 | pMem->n = sqlite3Strlen30(pMem->z); |
danielk1977 | 0d78bae | 2008-01-03 07:09:48 +0000 | [diff] [blame] | 1299 | pMem->enc = SQLITE_UTF8; |
danielk1977 | 1e522b4 | 2008-09-16 09:09:19 +0000 | [diff] [blame] | 1300 | pMem->type = SQLITE_TEXT; |
drh | 52391cb | 2008-02-14 23:44:13 +0000 | [diff] [blame] | 1301 | }else |
drh | aa9b896 | 2008-01-08 02:57:55 +0000 | [diff] [blame] | 1302 | #endif |
drh | 52391cb | 2008-02-14 23:44:13 +0000 | [diff] [blame] | 1303 | { |
| 1304 | pMem->flags = MEM_Null; /* Comment */ |
| 1305 | pMem->type = SQLITE_NULL; |
| 1306 | } |
danielk1977 | 0d78bae | 2008-01-03 07:09:48 +0000 | [diff] [blame] | 1307 | } |
| 1308 | |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 1309 | p->nResColumn = 8 - 4*(p->explain-1); |
drh | 9734e6e | 2011-10-07 18:24:25 +0000 | [diff] [blame] | 1310 | p->pResultSet = &p->aMem[1]; |
drh | 826fb5a | 2004-02-14 23:59:57 +0000 | [diff] [blame] | 1311 | p->rc = SQLITE_OK; |
| 1312 | rc = SQLITE_ROW; |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 1313 | } |
drh | 826fb5a | 2004-02-14 23:59:57 +0000 | [diff] [blame] | 1314 | return rc; |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 1315 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 1316 | #endif /* SQLITE_OMIT_EXPLAIN */ |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 1317 | |
drh | 7c4ac0c | 2007-04-05 11:25:58 +0000 | [diff] [blame] | 1318 | #ifdef SQLITE_DEBUG |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 1319 | /* |
drh | 3f7d4e4 | 2004-07-24 14:35:58 +0000 | [diff] [blame] | 1320 | ** Print the SQL that was used to generate a VDBE program. |
| 1321 | */ |
| 1322 | void sqlite3VdbePrintSql(Vdbe *p){ |
drh | 3f7d4e4 | 2004-07-24 14:35:58 +0000 | [diff] [blame] | 1323 | int nOp = p->nOp; |
| 1324 | VdbeOp *pOp; |
drh | c16a03b | 2004-09-15 13:38:10 +0000 | [diff] [blame] | 1325 | if( nOp<1 ) return; |
drh | 949f9cd | 2008-01-12 21:35:57 +0000 | [diff] [blame] | 1326 | pOp = &p->aOp[0]; |
| 1327 | if( pOp->opcode==OP_Trace && pOp->p4.z!=0 ){ |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 1328 | const char *z = pOp->p4.z; |
danielk1977 | 78ca0e7 | 2009-01-20 16:53:39 +0000 | [diff] [blame] | 1329 | while( sqlite3Isspace(*z) ) z++; |
drh | 3f7d4e4 | 2004-07-24 14:35:58 +0000 | [diff] [blame] | 1330 | printf("SQL: [%s]\n", z); |
| 1331 | } |
drh | 3f7d4e4 | 2004-07-24 14:35:58 +0000 | [diff] [blame] | 1332 | } |
drh | 7c4ac0c | 2007-04-05 11:25:58 +0000 | [diff] [blame] | 1333 | #endif |
drh | 3f7d4e4 | 2004-07-24 14:35:58 +0000 | [diff] [blame] | 1334 | |
drh | 602c237 | 2007-03-01 00:29:13 +0000 | [diff] [blame] | 1335 | #if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE) |
| 1336 | /* |
| 1337 | ** Print an IOTRACE message showing SQL content. |
| 1338 | */ |
| 1339 | void sqlite3VdbeIOTraceSql(Vdbe *p){ |
| 1340 | int nOp = p->nOp; |
| 1341 | VdbeOp *pOp; |
mlcreech | 3a00f90 | 2008-03-04 17:45:01 +0000 | [diff] [blame] | 1342 | if( sqlite3IoTrace==0 ) return; |
drh | 602c237 | 2007-03-01 00:29:13 +0000 | [diff] [blame] | 1343 | if( nOp<1 ) return; |
drh | 949f9cd | 2008-01-12 21:35:57 +0000 | [diff] [blame] | 1344 | pOp = &p->aOp[0]; |
| 1345 | if( pOp->opcode==OP_Trace && pOp->p4.z!=0 ){ |
drh | 602c237 | 2007-03-01 00:29:13 +0000 | [diff] [blame] | 1346 | int i, j; |
drh | 00a18e4 | 2007-08-13 11:10:34 +0000 | [diff] [blame] | 1347 | char z[1000]; |
drh | 949f9cd | 2008-01-12 21:35:57 +0000 | [diff] [blame] | 1348 | sqlite3_snprintf(sizeof(z), z, "%s", pOp->p4.z); |
danielk1977 | 78ca0e7 | 2009-01-20 16:53:39 +0000 | [diff] [blame] | 1349 | for(i=0; sqlite3Isspace(z[i]); i++){} |
drh | 602c237 | 2007-03-01 00:29:13 +0000 | [diff] [blame] | 1350 | for(j=0; z[i]; i++){ |
danielk1977 | 78ca0e7 | 2009-01-20 16:53:39 +0000 | [diff] [blame] | 1351 | if( sqlite3Isspace(z[i]) ){ |
drh | 602c237 | 2007-03-01 00:29:13 +0000 | [diff] [blame] | 1352 | if( z[i-1]!=' ' ){ |
| 1353 | z[j++] = ' '; |
| 1354 | } |
| 1355 | }else{ |
| 1356 | z[j++] = z[i]; |
| 1357 | } |
| 1358 | } |
| 1359 | z[j] = 0; |
mlcreech | 3a00f90 | 2008-03-04 17:45:01 +0000 | [diff] [blame] | 1360 | sqlite3IoTrace("SQL %s\n", z); |
drh | 602c237 | 2007-03-01 00:29:13 +0000 | [diff] [blame] | 1361 | } |
| 1362 | } |
| 1363 | #endif /* !SQLITE_OMIT_TRACE && SQLITE_ENABLE_IOTRACE */ |
| 1364 | |
drh | b2771ce | 2009-02-20 01:28:59 +0000 | [diff] [blame] | 1365 | /* |
drh | 4800b2e | 2009-12-08 15:35:22 +0000 | [diff] [blame] | 1366 | ** Allocate space from a fixed size buffer and return a pointer to |
| 1367 | ** that space. If insufficient space is available, return NULL. |
| 1368 | ** |
| 1369 | ** The pBuf parameter is the initial value of a pointer which will |
| 1370 | ** receive the new memory. pBuf is normally NULL. If pBuf is not |
| 1371 | ** NULL, it means that memory space has already been allocated and that |
| 1372 | ** this routine should not allocate any new memory. When pBuf is not |
| 1373 | ** NULL simply return pBuf. Only allocate new memory space when pBuf |
| 1374 | ** is NULL. |
drh | b2771ce | 2009-02-20 01:28:59 +0000 | [diff] [blame] | 1375 | ** |
| 1376 | ** nByte is the number of bytes of space needed. |
| 1377 | ** |
drh | 19875c8 | 2009-12-08 19:58:19 +0000 | [diff] [blame] | 1378 | ** *ppFrom points to available space and pEnd points to the end of the |
| 1379 | ** available space. When space is allocated, *ppFrom is advanced past |
| 1380 | ** the end of the allocated space. |
drh | b2771ce | 2009-02-20 01:28:59 +0000 | [diff] [blame] | 1381 | ** |
| 1382 | ** *pnByte is a counter of the number of bytes of space that have failed |
| 1383 | ** to allocate. If there is insufficient space in *ppFrom to satisfy the |
danielk1977 | d336e22 | 2009-02-20 10:58:41 +0000 | [diff] [blame] | 1384 | ** request, then increment *pnByte by the amount of the request. |
drh | b2771ce | 2009-02-20 01:28:59 +0000 | [diff] [blame] | 1385 | */ |
drh | 4800b2e | 2009-12-08 15:35:22 +0000 | [diff] [blame] | 1386 | static void *allocSpace( |
| 1387 | void *pBuf, /* Where return pointer will be stored */ |
drh | b2771ce | 2009-02-20 01:28:59 +0000 | [diff] [blame] | 1388 | int nByte, /* Number of bytes to allocate */ |
| 1389 | u8 **ppFrom, /* IN/OUT: Allocate from *ppFrom */ |
danielk1977 | d336e22 | 2009-02-20 10:58:41 +0000 | [diff] [blame] | 1390 | u8 *pEnd, /* Pointer to 1 byte past the end of *ppFrom buffer */ |
drh | b2771ce | 2009-02-20 01:28:59 +0000 | [diff] [blame] | 1391 | int *pnByte /* If allocation cannot be made, increment *pnByte */ |
| 1392 | ){ |
drh | ea598cb | 2009-04-05 12:22:08 +0000 | [diff] [blame] | 1393 | assert( EIGHT_BYTE_ALIGNMENT(*ppFrom) ); |
drh | 4800b2e | 2009-12-08 15:35:22 +0000 | [diff] [blame] | 1394 | if( pBuf ) return pBuf; |
| 1395 | nByte = ROUND8(nByte); |
| 1396 | if( &(*ppFrom)[nByte] <= pEnd ){ |
| 1397 | pBuf = (void*)*ppFrom; |
| 1398 | *ppFrom += nByte; |
| 1399 | }else{ |
| 1400 | *pnByte += nByte; |
drh | b2771ce | 2009-02-20 01:28:59 +0000 | [diff] [blame] | 1401 | } |
drh | 4800b2e | 2009-12-08 15:35:22 +0000 | [diff] [blame] | 1402 | return pBuf; |
drh | b2771ce | 2009-02-20 01:28:59 +0000 | [diff] [blame] | 1403 | } |
drh | 602c237 | 2007-03-01 00:29:13 +0000 | [diff] [blame] | 1404 | |
drh | 3f7d4e4 | 2004-07-24 14:35:58 +0000 | [diff] [blame] | 1405 | /* |
drh | 124c0b4 | 2011-06-01 18:15:55 +0000 | [diff] [blame] | 1406 | ** Rewind the VDBE back to the beginning in preparation for |
| 1407 | ** running it. |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 1408 | */ |
drh | 124c0b4 | 2011-06-01 18:15:55 +0000 | [diff] [blame] | 1409 | void sqlite3VdbeRewind(Vdbe *p){ |
| 1410 | #if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE) |
| 1411 | int i; |
| 1412 | #endif |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 1413 | assert( p!=0 ); |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 1414 | assert( p->magic==VDBE_MAGIC_INIT ); |
| 1415 | |
drh | c16a03b | 2004-09-15 13:38:10 +0000 | [diff] [blame] | 1416 | /* There should be at least one opcode. |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 1417 | */ |
drh | c16a03b | 2004-09-15 13:38:10 +0000 | [diff] [blame] | 1418 | assert( p->nOp>0 ); |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 1419 | |
danielk1977 | 00e1361 | 2008-11-17 19:18:54 +0000 | [diff] [blame] | 1420 | /* Set the magic to VDBE_MAGIC_RUN sooner rather than later. */ |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 1421 | p->magic = VDBE_MAGIC_RUN; |
| 1422 | |
drh | 124c0b4 | 2011-06-01 18:15:55 +0000 | [diff] [blame] | 1423 | #ifdef SQLITE_DEBUG |
| 1424 | for(i=1; i<p->nMem; i++){ |
| 1425 | assert( p->aMem[i].db==p->db ); |
| 1426 | } |
| 1427 | #endif |
| 1428 | p->pc = -1; |
| 1429 | p->rc = SQLITE_OK; |
| 1430 | p->errorAction = OE_Abort; |
| 1431 | p->magic = VDBE_MAGIC_RUN; |
| 1432 | p->nChange = 0; |
| 1433 | p->cacheCtr = 1; |
| 1434 | p->minWriteFileFormat = 255; |
| 1435 | p->iStatement = 0; |
| 1436 | p->nFkConstraint = 0; |
| 1437 | #ifdef VDBE_PROFILE |
| 1438 | for(i=0; i<p->nOp; i++){ |
| 1439 | p->aOp[i].cnt = 0; |
| 1440 | p->aOp[i].cycles = 0; |
| 1441 | } |
| 1442 | #endif |
| 1443 | } |
| 1444 | |
| 1445 | /* |
| 1446 | ** Prepare a virtual machine for execution for the first time after |
| 1447 | ** creating the virtual machine. This involves things such |
| 1448 | ** as allocating stack space and initializing the program counter. |
| 1449 | ** After the VDBE has be prepped, it can be executed by one or more |
| 1450 | ** calls to sqlite3VdbeExec(). |
| 1451 | ** |
| 1452 | ** This function may be called exact once on a each virtual machine. |
| 1453 | ** After this routine is called the VM has been "packaged" and is ready |
| 1454 | ** to run. After this routine is called, futher calls to |
| 1455 | ** sqlite3VdbeAddOp() functions are prohibited. This routine disconnects |
| 1456 | ** the Vdbe from the Parse object that helped generate it so that the |
| 1457 | ** the Vdbe becomes an independent entity and the Parse object can be |
| 1458 | ** destroyed. |
| 1459 | ** |
| 1460 | ** Use the sqlite3VdbeRewind() procedure to restore a virtual machine back |
| 1461 | ** to its initial state after it has been run. |
| 1462 | */ |
| 1463 | void sqlite3VdbeMakeReady( |
| 1464 | Vdbe *p, /* The VDBE */ |
| 1465 | Parse *pParse /* Parsing context */ |
| 1466 | ){ |
| 1467 | sqlite3 *db; /* The database connection */ |
| 1468 | int nVar; /* Number of parameters */ |
| 1469 | int nMem; /* Number of VM memory registers */ |
| 1470 | int nCursor; /* Number of cursors required */ |
| 1471 | int nArg; /* Number of arguments in subprograms */ |
| 1472 | int n; /* Loop counter */ |
| 1473 | u8 *zCsr; /* Memory available for allocation */ |
| 1474 | u8 *zEnd; /* First byte past allocated memory */ |
| 1475 | int nByte; /* How much extra memory is needed */ |
| 1476 | |
| 1477 | assert( p!=0 ); |
| 1478 | assert( p->nOp>0 ); |
| 1479 | assert( pParse!=0 ); |
| 1480 | assert( p->magic==VDBE_MAGIC_INIT ); |
| 1481 | db = p->db; |
| 1482 | assert( db->mallocFailed==0 ); |
| 1483 | nVar = pParse->nVar; |
| 1484 | nMem = pParse->nMem; |
| 1485 | nCursor = pParse->nTab; |
| 1486 | nArg = pParse->nMaxArg; |
| 1487 | |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 1488 | /* For each cursor required, also allocate a memory cell. Memory |
| 1489 | ** cells (nMem+1-nCursor)..nMem, inclusive, will never be used by |
| 1490 | ** the vdbe program. Instead they are used to allocate space for |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 1491 | ** VdbeCursor/BtCursor structures. The blob of memory associated with |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 1492 | ** cursor 0 is stored in memory cell nMem. Memory cell (nMem-1) |
| 1493 | ** stores the blob of memory associated with cursor 1, etc. |
| 1494 | ** |
| 1495 | ** See also: allocateCursor(). |
| 1496 | */ |
| 1497 | nMem += nCursor; |
| 1498 | |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1499 | /* Allocate space for memory registers, SQL variables, VDBE cursors and |
drh | 124c0b4 | 2011-06-01 18:15:55 +0000 | [diff] [blame] | 1500 | ** an array to marshal SQL function arguments in. |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 1501 | */ |
drh | 124c0b4 | 2011-06-01 18:15:55 +0000 | [diff] [blame] | 1502 | zCsr = (u8*)&p->aOp[p->nOp]; /* Memory avaliable for allocation */ |
| 1503 | zEnd = (u8*)&p->aOp[p->nOpAlloc]; /* First byte past end of zCsr[] */ |
drh | 19875c8 | 2009-12-08 19:58:19 +0000 | [diff] [blame] | 1504 | |
drh | 124c0b4 | 2011-06-01 18:15:55 +0000 | [diff] [blame] | 1505 | resolveP2Values(p, &nArg); |
| 1506 | p->usesStmtJournal = (u8)(pParse->isMultiWrite && pParse->mayAbort); |
| 1507 | if( pParse->explain && nMem<10 ){ |
| 1508 | nMem = 10; |
| 1509 | } |
| 1510 | memset(zCsr, 0, zEnd-zCsr); |
| 1511 | zCsr += (zCsr - (u8*)0)&7; |
| 1512 | assert( EIGHT_BYTE_ALIGNMENT(zCsr) ); |
drh | aab910c | 2011-06-27 00:01:22 +0000 | [diff] [blame] | 1513 | p->expired = 0; |
drh | 124c0b4 | 2011-06-01 18:15:55 +0000 | [diff] [blame] | 1514 | |
| 1515 | /* Memory for registers, parameters, cursor, etc, is allocated in two |
| 1516 | ** passes. On the first pass, we try to reuse unused space at the |
| 1517 | ** end of the opcode array. If we are unable to satisfy all memory |
| 1518 | ** requirements by reusing the opcode array tail, then the second |
| 1519 | ** pass will fill in the rest using a fresh allocation. |
| 1520 | ** |
| 1521 | ** This two-pass approach that reuses as much memory as possible from |
| 1522 | ** the leftover space at the end of the opcode array can significantly |
| 1523 | ** reduce the amount of memory held by a prepared statement. |
| 1524 | */ |
| 1525 | do { |
| 1526 | nByte = 0; |
| 1527 | p->aMem = allocSpace(p->aMem, nMem*sizeof(Mem), &zCsr, zEnd, &nByte); |
| 1528 | p->aVar = allocSpace(p->aVar, nVar*sizeof(Mem), &zCsr, zEnd, &nByte); |
| 1529 | p->apArg = allocSpace(p->apArg, nArg*sizeof(Mem*), &zCsr, zEnd, &nByte); |
| 1530 | p->azVar = allocSpace(p->azVar, nVar*sizeof(char*), &zCsr, zEnd, &nByte); |
| 1531 | p->apCsr = allocSpace(p->apCsr, nCursor*sizeof(VdbeCursor*), |
| 1532 | &zCsr, zEnd, &nByte); |
| 1533 | if( nByte ){ |
| 1534 | p->pFree = sqlite3DbMallocZero(db, nByte); |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 1535 | } |
drh | 124c0b4 | 2011-06-01 18:15:55 +0000 | [diff] [blame] | 1536 | zCsr = p->pFree; |
| 1537 | zEnd = &zCsr[nByte]; |
| 1538 | }while( nByte && !db->mallocFailed ); |
drh | b2771ce | 2009-02-20 01:28:59 +0000 | [diff] [blame] | 1539 | |
drh | 124c0b4 | 2011-06-01 18:15:55 +0000 | [diff] [blame] | 1540 | p->nCursor = (u16)nCursor; |
| 1541 | if( p->aVar ){ |
| 1542 | p->nVar = (ynVar)nVar; |
| 1543 | for(n=0; n<nVar; n++){ |
| 1544 | p->aVar[n].flags = MEM_Null; |
| 1545 | p->aVar[n].db = db; |
danielk1977 | 54db47e | 2004-05-19 10:36:43 +0000 | [diff] [blame] | 1546 | } |
drh | 82a4851 | 2003-09-06 22:45:20 +0000 | [diff] [blame] | 1547 | } |
drh | 124c0b4 | 2011-06-01 18:15:55 +0000 | [diff] [blame] | 1548 | if( p->azVar ){ |
| 1549 | p->nzVar = pParse->nzVar; |
| 1550 | memcpy(p->azVar, pParse->azVar, p->nzVar*sizeof(p->azVar[0])); |
| 1551 | memset(pParse->azVar, 0, pParse->nzVar*sizeof(pParse->azVar[0])); |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 1552 | } |
drh | 124c0b4 | 2011-06-01 18:15:55 +0000 | [diff] [blame] | 1553 | if( p->aMem ){ |
| 1554 | p->aMem--; /* aMem[] goes from 1..nMem */ |
| 1555 | p->nMem = nMem; /* not from 0..nMem-1 */ |
| 1556 | for(n=1; n<=nMem; n++){ |
| 1557 | p->aMem[n].flags = MEM_Null; |
| 1558 | p->aMem[n].db = db; |
drh | cf64d8b | 2003-12-31 17:57:10 +0000 | [diff] [blame] | 1559 | } |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 1560 | } |
drh | 124c0b4 | 2011-06-01 18:15:55 +0000 | [diff] [blame] | 1561 | p->explain = pParse->explain; |
| 1562 | sqlite3VdbeRewind(p); |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 1563 | } |
| 1564 | |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 1565 | /* |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 1566 | ** Close a VDBE cursor and release all the resources that cursor |
| 1567 | ** happens to hold. |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 1568 | */ |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 1569 | void sqlite3VdbeFreeCursor(Vdbe *p, VdbeCursor *pCx){ |
drh | 4774b13 | 2004-06-12 20:12:51 +0000 | [diff] [blame] | 1570 | if( pCx==0 ){ |
| 1571 | return; |
| 1572 | } |
dan | a20fde6 | 2011-07-12 14:28:05 +0000 | [diff] [blame] | 1573 | sqlite3VdbeSorterClose(p->db, pCx); |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 1574 | if( pCx->pBt ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1575 | sqlite3BtreeClose(pCx->pBt); |
drh | 34004ce | 2008-07-11 16:15:17 +0000 | [diff] [blame] | 1576 | /* The pCx->pCursor will be close automatically, if it exists, by |
| 1577 | ** the call above. */ |
| 1578 | }else if( pCx->pCursor ){ |
| 1579 | sqlite3BtreeCloseCursor(pCx->pCursor); |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 1580 | } |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 1581 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 1582 | if( pCx->pVtabCursor ){ |
| 1583 | sqlite3_vtab_cursor *pVtabCursor = pCx->pVtabCursor; |
danielk1977 | be71889 | 2006-06-23 08:05:19 +0000 | [diff] [blame] | 1584 | const sqlite3_module *pModule = pCx->pModule; |
| 1585 | p->inVtabMethod = 1; |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 1586 | pModule->xClose(pVtabCursor); |
danielk1977 | be71889 | 2006-06-23 08:05:19 +0000 | [diff] [blame] | 1587 | p->inVtabMethod = 0; |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 1588 | } |
| 1589 | #endif |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 1590 | } |
| 1591 | |
dan | 65a7cd1 | 2009-09-01 12:16:01 +0000 | [diff] [blame] | 1592 | /* |
| 1593 | ** Copy the values stored in the VdbeFrame structure to its Vdbe. This |
| 1594 | ** is used, for example, when a trigger sub-program is halted to restore |
| 1595 | ** control to the main program. |
| 1596 | */ |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 1597 | int sqlite3VdbeFrameRestore(VdbeFrame *pFrame){ |
| 1598 | Vdbe *v = pFrame->v; |
| 1599 | v->aOp = pFrame->aOp; |
| 1600 | v->nOp = pFrame->nOp; |
| 1601 | v->aMem = pFrame->aMem; |
| 1602 | v->nMem = pFrame->nMem; |
| 1603 | v->apCsr = pFrame->apCsr; |
| 1604 | v->nCursor = pFrame->nCursor; |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 1605 | v->db->lastRowid = pFrame->lastRowid; |
| 1606 | v->nChange = pFrame->nChange; |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 1607 | return pFrame->pc; |
| 1608 | } |
| 1609 | |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 1610 | /* |
drh | 5f82e3c | 2009-07-06 00:44:08 +0000 | [diff] [blame] | 1611 | ** Close all cursors. |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 1612 | ** |
| 1613 | ** Also release any dynamic memory held by the VM in the Vdbe.aMem memory |
| 1614 | ** cell array. This is necessary as the memory cell array may contain |
| 1615 | ** pointers to VdbeFrame objects, which may in turn contain pointers to |
| 1616 | ** open cursors. |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 1617 | */ |
drh | 5f82e3c | 2009-07-06 00:44:08 +0000 | [diff] [blame] | 1618 | static void closeAllCursors(Vdbe *p){ |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 1619 | if( p->pFrame ){ |
drh | 2327275 | 2011-03-06 21:54:33 +0000 | [diff] [blame] | 1620 | VdbeFrame *pFrame; |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 1621 | for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent); |
| 1622 | sqlite3VdbeFrameRestore(pFrame); |
| 1623 | } |
| 1624 | p->pFrame = 0; |
| 1625 | p->nFrame = 0; |
| 1626 | |
dan | 523a087 | 2009-08-31 05:23:32 +0000 | [diff] [blame] | 1627 | if( p->apCsr ){ |
| 1628 | int i; |
| 1629 | for(i=0; i<p->nCursor; i++){ |
| 1630 | VdbeCursor *pC = p->apCsr[i]; |
| 1631 | if( pC ){ |
| 1632 | sqlite3VdbeFreeCursor(p, pC); |
| 1633 | p->apCsr[i] = 0; |
| 1634 | } |
danielk1977 | be71889 | 2006-06-23 08:05:19 +0000 | [diff] [blame] | 1635 | } |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 1636 | } |
dan | 523a087 | 2009-08-31 05:23:32 +0000 | [diff] [blame] | 1637 | if( p->aMem ){ |
| 1638 | releaseMemArray(&p->aMem[1], p->nMem); |
| 1639 | } |
dan | 2710657 | 2010-12-01 08:04:47 +0000 | [diff] [blame] | 1640 | while( p->pDelFrame ){ |
| 1641 | VdbeFrame *pDel = p->pDelFrame; |
| 1642 | p->pDelFrame = pDel->pParent; |
| 1643 | sqlite3VdbeFrameDelete(pDel); |
| 1644 | } |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 1645 | } |
| 1646 | |
| 1647 | /* |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 1648 | ** Clean up the VM after execution. |
| 1649 | ** |
| 1650 | ** This routine will automatically close any cursors, lists, and/or |
| 1651 | ** sorters that were left open. It also deletes the values of |
drh | 5a12e68 | 2004-05-19 11:24:25 +0000 | [diff] [blame] | 1652 | ** variables in the aVar[] array. |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 1653 | */ |
drh | c890fec | 2008-08-01 20:10:08 +0000 | [diff] [blame] | 1654 | static void Cleanup(Vdbe *p){ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 1655 | sqlite3 *db = p->db; |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 1656 | |
| 1657 | #ifdef SQLITE_DEBUG |
| 1658 | /* Execute assert() statements to ensure that the Vdbe.apCsr[] and |
| 1659 | ** Vdbe.aMem[] arrays have already been cleaned up. */ |
| 1660 | int i; |
dan | 523a087 | 2009-08-31 05:23:32 +0000 | [diff] [blame] | 1661 | for(i=0; i<p->nCursor; i++) assert( p->apCsr==0 || p->apCsr[i]==0 ); |
| 1662 | for(i=1; i<=p->nMem; i++) assert( p->aMem==0 || p->aMem[i].flags==MEM_Null ); |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 1663 | #endif |
| 1664 | |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 1665 | sqlite3DbFree(db, p->zErrMsg); |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 1666 | p->zErrMsg = 0; |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 1667 | p->pResultSet = 0; |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 1668 | } |
| 1669 | |
| 1670 | /* |
danielk1977 | 22322fd | 2004-05-25 23:35:17 +0000 | [diff] [blame] | 1671 | ** Set the number of result columns that will be returned by this SQL |
| 1672 | ** statement. This is now set at compile time, rather than during |
| 1673 | ** execution of the vdbe program so that sqlite3_column_count() can |
| 1674 | ** be called on an SQL statement before sqlite3_step(). |
| 1675 | */ |
| 1676 | void sqlite3VdbeSetNumCols(Vdbe *p, int nResColumn){ |
drh | 76ff3a0 | 2004-09-24 22:32:30 +0000 | [diff] [blame] | 1677 | Mem *pColName; |
| 1678 | int n; |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 1679 | sqlite3 *db = p->db; |
drh | 4a50aac | 2007-08-23 02:47:53 +0000 | [diff] [blame] | 1680 | |
drh | c890fec | 2008-08-01 20:10:08 +0000 | [diff] [blame] | 1681 | releaseMemArray(p->aColName, p->nResColumn*COLNAME_N); |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 1682 | sqlite3DbFree(db, p->aColName); |
danielk1977 | 955de52 | 2006-02-10 02:27:42 +0000 | [diff] [blame] | 1683 | n = nResColumn*COLNAME_N; |
shane | 36840fd | 2009-06-26 16:32:13 +0000 | [diff] [blame] | 1684 | p->nResColumn = (u16)nResColumn; |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 1685 | p->aColName = pColName = (Mem*)sqlite3DbMallocZero(db, sizeof(Mem)*n ); |
drh | 76ff3a0 | 2004-09-24 22:32:30 +0000 | [diff] [blame] | 1686 | if( p->aColName==0 ) return; |
| 1687 | while( n-- > 0 ){ |
drh | 4a50aac | 2007-08-23 02:47:53 +0000 | [diff] [blame] | 1688 | pColName->flags = MEM_Null; |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 1689 | pColName->db = p->db; |
drh | 4a50aac | 2007-08-23 02:47:53 +0000 | [diff] [blame] | 1690 | pColName++; |
drh | 76ff3a0 | 2004-09-24 22:32:30 +0000 | [diff] [blame] | 1691 | } |
danielk1977 | 22322fd | 2004-05-25 23:35:17 +0000 | [diff] [blame] | 1692 | } |
| 1693 | |
| 1694 | /* |
danielk1977 | 3cf8606 | 2004-05-26 10:11:05 +0000 | [diff] [blame] | 1695 | ** Set the name of the idx'th column to be returned by the SQL statement. |
| 1696 | ** zName must be a pointer to a nul terminated string. |
| 1697 | ** |
| 1698 | ** This call must be made after a call to sqlite3VdbeSetNumCols(). |
| 1699 | ** |
danielk1977 | 10fb749 | 2008-10-31 10:53:22 +0000 | [diff] [blame] | 1700 | ** The final parameter, xDel, must be one of SQLITE_DYNAMIC, SQLITE_STATIC |
| 1701 | ** or SQLITE_TRANSIENT. If it is SQLITE_DYNAMIC, then the buffer pointed |
| 1702 | ** to by zName will be freed by sqlite3DbFree() when the vdbe is destroyed. |
danielk1977 | 3cf8606 | 2004-05-26 10:11:05 +0000 | [diff] [blame] | 1703 | */ |
danielk1977 | 10fb749 | 2008-10-31 10:53:22 +0000 | [diff] [blame] | 1704 | int sqlite3VdbeSetColName( |
| 1705 | Vdbe *p, /* Vdbe being configured */ |
| 1706 | int idx, /* Index of column zName applies to */ |
| 1707 | int var, /* One of the COLNAME_* constants */ |
| 1708 | const char *zName, /* Pointer to buffer containing name */ |
| 1709 | void (*xDel)(void*) /* Memory management strategy for zName */ |
| 1710 | ){ |
danielk1977 | 3cf8606 | 2004-05-26 10:11:05 +0000 | [diff] [blame] | 1711 | int rc; |
| 1712 | Mem *pColName; |
danielk1977 | 955de52 | 2006-02-10 02:27:42 +0000 | [diff] [blame] | 1713 | assert( idx<p->nResColumn ); |
| 1714 | assert( var<COLNAME_N ); |
danielk1977 | 10fb749 | 2008-10-31 10:53:22 +0000 | [diff] [blame] | 1715 | if( p->db->mallocFailed ){ |
| 1716 | assert( !zName || xDel!=SQLITE_DYNAMIC ); |
| 1717 | return SQLITE_NOMEM; |
| 1718 | } |
drh | 76ff3a0 | 2004-09-24 22:32:30 +0000 | [diff] [blame] | 1719 | assert( p->aColName!=0 ); |
danielk1977 | 955de52 | 2006-02-10 02:27:42 +0000 | [diff] [blame] | 1720 | pColName = &(p->aColName[idx+var*p->nResColumn]); |
danielk1977 | 10fb749 | 2008-10-31 10:53:22 +0000 | [diff] [blame] | 1721 | rc = sqlite3VdbeMemSetStr(pColName, zName, -1, SQLITE_UTF8, xDel); |
drh | 0793f1b | 2008-11-05 17:41:19 +0000 | [diff] [blame] | 1722 | assert( rc!=0 || !zName || (pColName->flags&MEM_Term)!=0 ); |
danielk1977 | 3cf8606 | 2004-05-26 10:11:05 +0000 | [diff] [blame] | 1723 | return rc; |
| 1724 | } |
| 1725 | |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1726 | /* |
| 1727 | ** A read or write transaction may or may not be active on database handle |
| 1728 | ** db. If a transaction is active, commit it. If there is a |
| 1729 | ** write-transaction spanning more than one database file, this routine |
| 1730 | ** takes care of the master journal trickery. |
| 1731 | */ |
danielk1977 | 3e3a84d | 2008-08-01 17:37:40 +0000 | [diff] [blame] | 1732 | static int vdbeCommit(sqlite3 *db, Vdbe *p){ |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1733 | int i; |
| 1734 | int nTrans = 0; /* Number of databases with an active write-transaction */ |
| 1735 | int rc = SQLITE_OK; |
| 1736 | int needXcommit = 0; |
| 1737 | |
shane | 36840fd | 2009-06-26 16:32:13 +0000 | [diff] [blame] | 1738 | #ifdef SQLITE_OMIT_VIRTUALTABLE |
| 1739 | /* With this option, sqlite3VtabSync() is defined to be simply |
| 1740 | ** SQLITE_OK so p is not used. |
| 1741 | */ |
| 1742 | UNUSED_PARAMETER(p); |
| 1743 | #endif |
| 1744 | |
danielk1977 | 5bd270b | 2006-07-25 15:14:52 +0000 | [diff] [blame] | 1745 | /* Before doing anything else, call the xSync() callback for any |
| 1746 | ** virtual module tables written in this transaction. This has to |
| 1747 | ** be done before determining whether a master journal file is |
| 1748 | ** required, as an xSync() callback may add an attached database |
| 1749 | ** to the transaction. |
| 1750 | */ |
danielk1977 | 3e3a84d | 2008-08-01 17:37:40 +0000 | [diff] [blame] | 1751 | rc = sqlite3VtabSync(db, &p->zErrMsg); |
danielk1977 | 5bd270b | 2006-07-25 15:14:52 +0000 | [diff] [blame] | 1752 | |
| 1753 | /* This loop determines (a) if the commit hook should be invoked and |
| 1754 | ** (b) how many database files have open write transactions, not |
| 1755 | ** including the temp database. (b) is important because if more than |
| 1756 | ** one database file has an open write transaction, a master journal |
| 1757 | ** file is required for an atomic commit. |
| 1758 | */ |
drh | abfb62f | 2010-07-30 11:20:35 +0000 | [diff] [blame] | 1759 | for(i=0; rc==SQLITE_OK && i<db->nDb; i++){ |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1760 | Btree *pBt = db->aDb[i].pBt; |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 1761 | if( sqlite3BtreeIsInTrans(pBt) ){ |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1762 | needXcommit = 1; |
| 1763 | if( i!=1 ) nTrans++; |
drh | abfb62f | 2010-07-30 11:20:35 +0000 | [diff] [blame] | 1764 | rc = sqlite3PagerExclusiveLock(sqlite3BtreePager(pBt)); |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1765 | } |
| 1766 | } |
drh | abfb62f | 2010-07-30 11:20:35 +0000 | [diff] [blame] | 1767 | if( rc!=SQLITE_OK ){ |
| 1768 | return rc; |
| 1769 | } |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1770 | |
| 1771 | /* If there are any write-transactions at all, invoke the commit hook */ |
| 1772 | if( needXcommit && db->xCommitCallback ){ |
drh | 92f02c3 | 2004-09-02 14:57:08 +0000 | [diff] [blame] | 1773 | rc = db->xCommitCallback(db->pCommitArg); |
drh | 92f02c3 | 2004-09-02 14:57:08 +0000 | [diff] [blame] | 1774 | if( rc ){ |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1775 | return SQLITE_CONSTRAINT; |
| 1776 | } |
| 1777 | } |
| 1778 | |
danielk1977 | 40b38dc | 2004-06-26 08:38:24 +0000 | [diff] [blame] | 1779 | /* The simple case - no more than one database file (not counting the |
| 1780 | ** TEMP database) has a transaction active. There is no need for the |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1781 | ** master-journal. |
drh | c9e0686 | 2004-06-09 20:03:08 +0000 | [diff] [blame] | 1782 | ** |
danielk1977 | 40b38dc | 2004-06-26 08:38:24 +0000 | [diff] [blame] | 1783 | ** If the return value of sqlite3BtreeGetFilename() is a zero length |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 1784 | ** string, it means the main database is :memory: or a temp file. In |
| 1785 | ** that case we do not support atomic multi-file commits, so use the |
| 1786 | ** simple case then too. |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1787 | */ |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 1788 | if( 0==sqlite3Strlen30(sqlite3BtreeGetFilename(db->aDb[0].pBt)) |
| 1789 | || nTrans<=1 |
| 1790 | ){ |
danielk1977 | 0410302 | 2009-02-03 16:51:24 +0000 | [diff] [blame] | 1791 | for(i=0; rc==SQLITE_OK && i<db->nDb; i++){ |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1792 | Btree *pBt = db->aDb[i].pBt; |
| 1793 | if( pBt ){ |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 1794 | rc = sqlite3BtreeCommitPhaseOne(pBt, 0); |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1795 | } |
| 1796 | } |
| 1797 | |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 1798 | /* Do the commit only if all databases successfully complete phase 1. |
| 1799 | ** If one of the BtreeCommitPhaseOne() calls fails, this indicates an |
| 1800 | ** IO error while deleting or truncating a journal file. It is unlikely, |
| 1801 | ** but could happen. In this case abandon processing and return the error. |
danielk1977 | 979f38e | 2007-03-27 16:19:51 +0000 | [diff] [blame] | 1802 | */ |
| 1803 | for(i=0; rc==SQLITE_OK && i<db->nDb; i++){ |
| 1804 | Btree *pBt = db->aDb[i].pBt; |
| 1805 | if( pBt ){ |
dan | 60939d0 | 2011-03-29 15:40:55 +0000 | [diff] [blame] | 1806 | rc = sqlite3BtreeCommitPhaseTwo(pBt, 0); |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1807 | } |
danielk1977 | 979f38e | 2007-03-27 16:19:51 +0000 | [diff] [blame] | 1808 | } |
| 1809 | if( rc==SQLITE_OK ){ |
danielk1977 | f9e7dda | 2006-06-16 16:08:53 +0000 | [diff] [blame] | 1810 | sqlite3VtabCommit(db); |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1811 | } |
| 1812 | } |
| 1813 | |
| 1814 | /* The complex case - There is a multi-file write-transaction active. |
| 1815 | ** This requires a master journal file to ensure the transaction is |
| 1816 | ** committed atomicly. |
| 1817 | */ |
danielk1977 | 44ee5bf | 2005-05-27 09:41:12 +0000 | [diff] [blame] | 1818 | #ifndef SQLITE_OMIT_DISKIO |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1819 | else{ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 1820 | sqlite3_vfs *pVfs = db->pVfs; |
drh | 2c8997b | 2005-08-27 16:36:48 +0000 | [diff] [blame] | 1821 | int needSync = 0; |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1822 | char *zMaster = 0; /* File-name for the master journal */ |
| 1823 | char const *zMainFile = sqlite3BtreeGetFilename(db->aDb[0].pBt); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 1824 | sqlite3_file *pMaster = 0; |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 1825 | i64 offset = 0; |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 1826 | int res; |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1827 | |
| 1828 | /* Select a master journal file name */ |
| 1829 | do { |
drh | dc5ea5c | 2008-12-10 17:19:59 +0000 | [diff] [blame] | 1830 | u32 iRandom; |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 1831 | sqlite3DbFree(db, zMaster); |
drh | dc5ea5c | 2008-12-10 17:19:59 +0000 | [diff] [blame] | 1832 | sqlite3_randomness(sizeof(iRandom), &iRandom); |
| 1833 | zMaster = sqlite3MPrintf(db, "%s-mj%08X", zMainFile, iRandom&0x7fffffff); |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1834 | if( !zMaster ){ |
| 1835 | return SQLITE_NOMEM; |
| 1836 | } |
drh | 81cc516 | 2011-05-17 20:36:21 +0000 | [diff] [blame] | 1837 | sqlite3FileSuffix3(zMainFile, zMaster); |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 1838 | rc = sqlite3OsAccess(pVfs, zMaster, SQLITE_ACCESS_EXISTS, &res); |
| 1839 | }while( rc==SQLITE_OK && res ); |
| 1840 | if( rc==SQLITE_OK ){ |
drh | 19db935 | 2008-03-27 22:42:51 +0000 | [diff] [blame] | 1841 | /* Open the master journal. */ |
| 1842 | rc = sqlite3OsOpenMalloc(pVfs, zMaster, &pMaster, |
| 1843 | SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE| |
| 1844 | SQLITE_OPEN_EXCLUSIVE|SQLITE_OPEN_MASTER_JOURNAL, 0 |
| 1845 | ); |
| 1846 | } |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1847 | if( rc!=SQLITE_OK ){ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 1848 | sqlite3DbFree(db, zMaster); |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1849 | return rc; |
| 1850 | } |
| 1851 | |
| 1852 | /* Write the name of each database file in the transaction into the new |
| 1853 | ** master journal file. If an error occurs at this point close |
| 1854 | ** and delete the master journal file. All the individual journal files |
| 1855 | ** still have 'null' as the master journal pointer, so they will roll |
danielk1977 | aca790a | 2005-01-13 11:07:52 +0000 | [diff] [blame] | 1856 | ** back independently if a failure occurs. |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1857 | */ |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 1858 | for(i=0; i<db->nDb; i++){ |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1859 | Btree *pBt = db->aDb[i].pBt; |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 1860 | if( sqlite3BtreeIsInTrans(pBt) ){ |
danielk1977 | 5865e3d | 2004-06-14 06:03:57 +0000 | [diff] [blame] | 1861 | char const *zFile = sqlite3BtreeGetJournalname(pBt); |
drh | 8c96a6e | 2010-08-31 01:09:15 +0000 | [diff] [blame] | 1862 | if( zFile==0 ){ |
drh | b290e1c | 2009-12-08 13:36:55 +0000 | [diff] [blame] | 1863 | continue; /* Ignore TEMP and :memory: databases */ |
| 1864 | } |
drh | 8c96a6e | 2010-08-31 01:09:15 +0000 | [diff] [blame] | 1865 | assert( zFile[0]!=0 ); |
drh | 2c8997b | 2005-08-27 16:36:48 +0000 | [diff] [blame] | 1866 | if( !needSync && !sqlite3BtreeSyncDisabled(pBt) ){ |
| 1867 | needSync = 1; |
| 1868 | } |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 1869 | rc = sqlite3OsWrite(pMaster, zFile, sqlite3Strlen30(zFile)+1, offset); |
| 1870 | offset += sqlite3Strlen30(zFile)+1; |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1871 | if( rc!=SQLITE_OK ){ |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 1872 | sqlite3OsCloseFree(pMaster); |
| 1873 | sqlite3OsDelete(pVfs, zMaster, 0); |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 1874 | sqlite3DbFree(db, zMaster); |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1875 | return rc; |
| 1876 | } |
| 1877 | } |
| 1878 | } |
| 1879 | |
danielk1977 | 9663b8f | 2007-08-24 11:52:28 +0000 | [diff] [blame] | 1880 | /* Sync the master journal file. If the IOCAP_SEQUENTIAL device |
| 1881 | ** flag is set this is not required. |
| 1882 | */ |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 1883 | if( needSync |
| 1884 | && 0==(sqlite3OsDeviceCharacteristics(pMaster)&SQLITE_IOCAP_SEQUENTIAL) |
| 1885 | && SQLITE_OK!=(rc = sqlite3OsSync(pMaster, SQLITE_SYNC_NORMAL)) |
| 1886 | ){ |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 1887 | sqlite3OsCloseFree(pMaster); |
| 1888 | sqlite3OsDelete(pVfs, zMaster, 0); |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 1889 | sqlite3DbFree(db, zMaster); |
danielk1977 | 5865e3d | 2004-06-14 06:03:57 +0000 | [diff] [blame] | 1890 | return rc; |
| 1891 | } |
drh | c9e0686 | 2004-06-09 20:03:08 +0000 | [diff] [blame] | 1892 | |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1893 | /* Sync all the db files involved in the transaction. The same call |
| 1894 | ** sets the master journal pointer in each individual journal. If |
| 1895 | ** an error occurs here, do not delete the master journal file. |
| 1896 | ** |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 1897 | ** If the error occurs during the first call to |
| 1898 | ** sqlite3BtreeCommitPhaseOne(), then there is a chance that the |
| 1899 | ** master journal file will be orphaned. But we cannot delete it, |
| 1900 | ** in case the master journal file name was written into the journal |
shane | be21779 | 2009-03-05 04:20:31 +0000 | [diff] [blame] | 1901 | ** file before the failure occurred. |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1902 | */ |
danielk1977 | 5bd270b | 2006-07-25 15:14:52 +0000 | [diff] [blame] | 1903 | for(i=0; rc==SQLITE_OK && i<db->nDb; i++){ |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1904 | Btree *pBt = db->aDb[i].pBt; |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 1905 | if( pBt ){ |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 1906 | rc = sqlite3BtreeCommitPhaseOne(pBt, zMaster); |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1907 | } |
| 1908 | } |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 1909 | sqlite3OsCloseFree(pMaster); |
drh | abfb62f | 2010-07-30 11:20:35 +0000 | [diff] [blame] | 1910 | assert( rc!=SQLITE_BUSY ); |
danielk1977 | 5bd270b | 2006-07-25 15:14:52 +0000 | [diff] [blame] | 1911 | if( rc!=SQLITE_OK ){ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 1912 | sqlite3DbFree(db, zMaster); |
danielk1977 | 5bd270b | 2006-07-25 15:14:52 +0000 | [diff] [blame] | 1913 | return rc; |
| 1914 | } |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1915 | |
danielk1977 | 962398d | 2004-06-14 09:35:16 +0000 | [diff] [blame] | 1916 | /* Delete the master journal file. This commits the transaction. After |
| 1917 | ** doing this the directory is synced again before any individual |
| 1918 | ** transaction files are deleted. |
| 1919 | */ |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 1920 | rc = sqlite3OsDelete(pVfs, zMaster, 1); |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 1921 | sqlite3DbFree(db, zMaster); |
drh | c416ba9 | 2007-03-30 18:42:55 +0000 | [diff] [blame] | 1922 | zMaster = 0; |
drh | 29a0138 | 2006-08-13 19:04:18 +0000 | [diff] [blame] | 1923 | if( rc ){ |
| 1924 | return rc; |
| 1925 | } |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1926 | |
| 1927 | /* All files and directories have already been synced, so the following |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 1928 | ** calls to sqlite3BtreeCommitPhaseTwo() are only closing files and |
| 1929 | ** deleting or truncating journals. If something goes wrong while |
| 1930 | ** this is happening we don't really care. The integrity of the |
| 1931 | ** transaction is already guaranteed, but some stray 'cold' journals |
| 1932 | ** may be lying around. Returning an error code won't help matters. |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1933 | */ |
danielk1977 | 979f38e | 2007-03-27 16:19:51 +0000 | [diff] [blame] | 1934 | disable_simulated_io_errors(); |
danielk1977 | 2d1d86f | 2008-06-20 14:59:51 +0000 | [diff] [blame] | 1935 | sqlite3BeginBenignMalloc(); |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1936 | for(i=0; i<db->nDb; i++){ |
| 1937 | Btree *pBt = db->aDb[i].pBt; |
| 1938 | if( pBt ){ |
dan | 60939d0 | 2011-03-29 15:40:55 +0000 | [diff] [blame] | 1939 | sqlite3BtreeCommitPhaseTwo(pBt, 1); |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1940 | } |
| 1941 | } |
danielk1977 | 2d1d86f | 2008-06-20 14:59:51 +0000 | [diff] [blame] | 1942 | sqlite3EndBenignMalloc(); |
danielk1977 | 979f38e | 2007-03-27 16:19:51 +0000 | [diff] [blame] | 1943 | enable_simulated_io_errors(); |
| 1944 | |
danielk1977 | f9e7dda | 2006-06-16 16:08:53 +0000 | [diff] [blame] | 1945 | sqlite3VtabCommit(db); |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1946 | } |
danielk1977 | 44ee5bf | 2005-05-27 09:41:12 +0000 | [diff] [blame] | 1947 | #endif |
danielk1977 | 026d270 | 2004-06-14 13:14:59 +0000 | [diff] [blame] | 1948 | |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1949 | return rc; |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1950 | } |
| 1951 | |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 1952 | /* |
| 1953 | ** This routine checks that the sqlite3.activeVdbeCnt count variable |
| 1954 | ** matches the number of vdbe's in the list sqlite3.pVdbe that are |
| 1955 | ** currently active. An assertion fails if the two counts do not match. |
drh | 92f02c3 | 2004-09-02 14:57:08 +0000 | [diff] [blame] | 1956 | ** This is an internal self-check only - it is not an essential processing |
| 1957 | ** step. |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 1958 | ** |
| 1959 | ** This is a no-op if NDEBUG is defined. |
| 1960 | */ |
| 1961 | #ifndef NDEBUG |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 1962 | static void checkActiveVdbeCnt(sqlite3 *db){ |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 1963 | Vdbe *p; |
| 1964 | int cnt = 0; |
drh | ad4a4b8 | 2008-11-05 16:37:34 +0000 | [diff] [blame] | 1965 | int nWrite = 0; |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 1966 | p = db->pVdbe; |
| 1967 | while( p ){ |
drh | 92f02c3 | 2004-09-02 14:57:08 +0000 | [diff] [blame] | 1968 | if( p->magic==VDBE_MAGIC_RUN && p->pc>=0 ){ |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 1969 | cnt++; |
drh | ad4a4b8 | 2008-11-05 16:37:34 +0000 | [diff] [blame] | 1970 | if( p->readOnly==0 ) nWrite++; |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 1971 | } |
| 1972 | p = p->pNext; |
| 1973 | } |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 1974 | assert( cnt==db->activeVdbeCnt ); |
drh | ad4a4b8 | 2008-11-05 16:37:34 +0000 | [diff] [blame] | 1975 | assert( nWrite==db->writeVdbeCnt ); |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 1976 | } |
| 1977 | #else |
| 1978 | #define checkActiveVdbeCnt(x) |
| 1979 | #endif |
| 1980 | |
danielk1977 | 3cf8606 | 2004-05-26 10:11:05 +0000 | [diff] [blame] | 1981 | /* |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 1982 | ** For every Btree that in database connection db which |
| 1983 | ** has been modified, "trip" or invalidate each cursor in |
| 1984 | ** that Btree might have been modified so that the cursor |
| 1985 | ** can never be used again. This happens when a rollback |
| 1986 | *** occurs. We have to trip all the other cursors, even |
| 1987 | ** cursor from other VMs in different database connections, |
| 1988 | ** so that none of them try to use the data at which they |
| 1989 | ** were pointing and which now may have been changed due |
| 1990 | ** to the rollback. |
| 1991 | ** |
| 1992 | ** Remember that a rollback can delete tables complete and |
| 1993 | ** reorder rootpages. So it is not sufficient just to save |
| 1994 | ** the state of the cursor. We have to invalidate the cursor |
| 1995 | ** so that it is never used again. |
danielk1977 | be71889 | 2006-06-23 08:05:19 +0000 | [diff] [blame] | 1996 | */ |
drh | ade6c9c | 2007-11-24 10:23:44 +0000 | [diff] [blame] | 1997 | static void invalidateCursorsOnModifiedBtrees(sqlite3 *db){ |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 1998 | int i; |
| 1999 | for(i=0; i<db->nDb; i++){ |
| 2000 | Btree *p = db->aDb[i].pBt; |
| 2001 | if( p && sqlite3BtreeIsInTrans(p) ){ |
| 2002 | sqlite3BtreeTripAllCursors(p, SQLITE_ABORT); |
| 2003 | } |
danielk1977 | be71889 | 2006-06-23 08:05:19 +0000 | [diff] [blame] | 2004 | } |
| 2005 | } |
| 2006 | |
| 2007 | /* |
danielk1977 | bd43455 | 2009-03-18 10:33:00 +0000 | [diff] [blame] | 2008 | ** If the Vdbe passed as the first argument opened a statement-transaction, |
| 2009 | ** close it now. Argument eOp must be either SAVEPOINT_ROLLBACK or |
| 2010 | ** SAVEPOINT_RELEASE. If it is SAVEPOINT_ROLLBACK, then the statement |
| 2011 | ** transaction is rolled back. If eOp is SAVEPOINT_RELEASE, then the |
| 2012 | ** statement transaction is commtted. |
| 2013 | ** |
| 2014 | ** If an IO error occurs, an SQLITE_IOERR_XXX error code is returned. |
| 2015 | ** Otherwise SQLITE_OK. |
| 2016 | */ |
| 2017 | int sqlite3VdbeCloseStatement(Vdbe *p, int eOp){ |
danielk1977 | c926b6a | 2009-03-20 14:42:11 +0000 | [diff] [blame] | 2018 | sqlite3 *const db = p->db; |
danielk1977 | bd43455 | 2009-03-18 10:33:00 +0000 | [diff] [blame] | 2019 | int rc = SQLITE_OK; |
danielk1977 | ecaecf9 | 2009-07-08 08:05:35 +0000 | [diff] [blame] | 2020 | |
danielk1977 | e494817 | 2009-07-17 17:25:43 +0000 | [diff] [blame] | 2021 | /* If p->iStatement is greater than zero, then this Vdbe opened a |
| 2022 | ** statement transaction that should be closed here. The only exception |
| 2023 | ** is that an IO error may have occured, causing an emergency rollback. |
| 2024 | ** In this case (db->nStatement==0), and there is nothing to do. |
| 2025 | */ |
| 2026 | if( db->nStatement && p->iStatement ){ |
danielk1977 | bd43455 | 2009-03-18 10:33:00 +0000 | [diff] [blame] | 2027 | int i; |
| 2028 | const int iSavepoint = p->iStatement-1; |
danielk1977 | bd43455 | 2009-03-18 10:33:00 +0000 | [diff] [blame] | 2029 | |
| 2030 | assert( eOp==SAVEPOINT_ROLLBACK || eOp==SAVEPOINT_RELEASE); |
| 2031 | assert( db->nStatement>0 ); |
| 2032 | assert( p->iStatement==(db->nStatement+db->nSavepoint) ); |
| 2033 | |
| 2034 | for(i=0; i<db->nDb; i++){ |
| 2035 | int rc2 = SQLITE_OK; |
| 2036 | Btree *pBt = db->aDb[i].pBt; |
| 2037 | if( pBt ){ |
| 2038 | if( eOp==SAVEPOINT_ROLLBACK ){ |
| 2039 | rc2 = sqlite3BtreeSavepoint(pBt, SAVEPOINT_ROLLBACK, iSavepoint); |
| 2040 | } |
| 2041 | if( rc2==SQLITE_OK ){ |
| 2042 | rc2 = sqlite3BtreeSavepoint(pBt, SAVEPOINT_RELEASE, iSavepoint); |
| 2043 | } |
| 2044 | if( rc==SQLITE_OK ){ |
| 2045 | rc = rc2; |
| 2046 | } |
| 2047 | } |
| 2048 | } |
| 2049 | db->nStatement--; |
| 2050 | p->iStatement = 0; |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 2051 | |
dan | a311b80 | 2011-04-26 19:21:34 +0000 | [diff] [blame] | 2052 | if( rc==SQLITE_OK ){ |
| 2053 | if( eOp==SAVEPOINT_ROLLBACK ){ |
| 2054 | rc = sqlite3VtabSavepoint(db, SAVEPOINT_ROLLBACK, iSavepoint); |
| 2055 | } |
| 2056 | if( rc==SQLITE_OK ){ |
| 2057 | rc = sqlite3VtabSavepoint(db, SAVEPOINT_RELEASE, iSavepoint); |
| 2058 | } |
| 2059 | } |
| 2060 | |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 2061 | /* If the statement transaction is being rolled back, also restore the |
| 2062 | ** database handles deferred constraint counter to the value it had when |
| 2063 | ** the statement transaction was opened. */ |
| 2064 | if( eOp==SAVEPOINT_ROLLBACK ){ |
| 2065 | db->nDeferredCons = p->nStmtDefCons; |
| 2066 | } |
danielk1977 | bd43455 | 2009-03-18 10:33:00 +0000 | [diff] [blame] | 2067 | } |
| 2068 | return rc; |
| 2069 | } |
| 2070 | |
| 2071 | /* |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 2072 | ** This function is called when a transaction opened by the database |
| 2073 | ** handle associated with the VM passed as an argument is about to be |
| 2074 | ** committed. If there are outstanding deferred foreign key constraint |
| 2075 | ** violations, return SQLITE_ERROR. Otherwise, SQLITE_OK. |
| 2076 | ** |
| 2077 | ** If there are outstanding FK violations and this function returns |
| 2078 | ** SQLITE_ERROR, set the result of the VM to SQLITE_CONSTRAINT and write |
| 2079 | ** an error message to it. Then return SQLITE_ERROR. |
| 2080 | */ |
| 2081 | #ifndef SQLITE_OMIT_FOREIGN_KEY |
dan | 32b09f2 | 2009-09-23 17:29:59 +0000 | [diff] [blame] | 2082 | int sqlite3VdbeCheckFk(Vdbe *p, int deferred){ |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 2083 | sqlite3 *db = p->db; |
dan | 32b09f2 | 2009-09-23 17:29:59 +0000 | [diff] [blame] | 2084 | if( (deferred && db->nDeferredCons>0) || (!deferred && p->nFkConstraint>0) ){ |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 2085 | p->rc = SQLITE_CONSTRAINT; |
dan | 32b09f2 | 2009-09-23 17:29:59 +0000 | [diff] [blame] | 2086 | p->errorAction = OE_Abort; |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 2087 | sqlite3SetString(&p->zErrMsg, db, "foreign key constraint failed"); |
| 2088 | return SQLITE_ERROR; |
| 2089 | } |
| 2090 | return SQLITE_OK; |
| 2091 | } |
| 2092 | #endif |
| 2093 | |
| 2094 | /* |
drh | 92f02c3 | 2004-09-02 14:57:08 +0000 | [diff] [blame] | 2095 | ** This routine is called the when a VDBE tries to halt. If the VDBE |
| 2096 | ** has made changes and is in autocommit mode, then commit those |
| 2097 | ** changes. If a rollback is needed, then do the rollback. |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 2098 | ** |
drh | 92f02c3 | 2004-09-02 14:57:08 +0000 | [diff] [blame] | 2099 | ** 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] | 2100 | ** SQLITE_MAGIC_RUN to SQLITE_MAGIC_HALT. It is harmless to |
| 2101 | ** call this on a VM that is in the SQLITE_MAGIC_HALT state. |
drh | 92f02c3 | 2004-09-02 14:57:08 +0000 | [diff] [blame] | 2102 | ** |
| 2103 | ** Return an error code. If the commit could not complete because of |
| 2104 | ** lock contention, return SQLITE_BUSY. If SQLITE_BUSY is returned, it |
| 2105 | ** means the close did not happen and needs to be repeated. |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 2106 | */ |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 2107 | int sqlite3VdbeHalt(Vdbe *p){ |
danielk1977 | bd43455 | 2009-03-18 10:33:00 +0000 | [diff] [blame] | 2108 | int rc; /* Used to store transient return codes */ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 2109 | sqlite3 *db = p->db; |
danielk1977 | 07cb560 | 2006-01-20 10:55:05 +0000 | [diff] [blame] | 2110 | |
| 2111 | /* This function contains the logic that determines if a statement or |
| 2112 | ** transaction will be committed or rolled back as a result of the |
| 2113 | ** execution of this virtual machine. |
| 2114 | ** |
drh | 71b890a | 2007-10-03 15:30:52 +0000 | [diff] [blame] | 2115 | ** If any of the following errors occur: |
danielk1977 | 07cb560 | 2006-01-20 10:55:05 +0000 | [diff] [blame] | 2116 | ** |
drh | 71b890a | 2007-10-03 15:30:52 +0000 | [diff] [blame] | 2117 | ** SQLITE_NOMEM |
| 2118 | ** SQLITE_IOERR |
| 2119 | ** SQLITE_FULL |
| 2120 | ** SQLITE_INTERRUPT |
danielk1977 | 07cb560 | 2006-01-20 10:55:05 +0000 | [diff] [blame] | 2121 | ** |
drh | 71b890a | 2007-10-03 15:30:52 +0000 | [diff] [blame] | 2122 | ** Then the internal cache might have been left in an inconsistent |
| 2123 | ** state. We need to rollback the statement transaction, if there is |
| 2124 | ** one, or the complete transaction if there is no statement transaction. |
danielk1977 | 07cb560 | 2006-01-20 10:55:05 +0000 | [diff] [blame] | 2125 | */ |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 2126 | |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 2127 | if( p->db->mallocFailed ){ |
danielk1977 | 261919c | 2005-12-06 12:52:59 +0000 | [diff] [blame] | 2128 | p->rc = SQLITE_NOMEM; |
| 2129 | } |
drh | 5f82e3c | 2009-07-06 00:44:08 +0000 | [diff] [blame] | 2130 | closeAllCursors(p); |
drh | 92f02c3 | 2004-09-02 14:57:08 +0000 | [diff] [blame] | 2131 | if( p->magic!=VDBE_MAGIC_RUN ){ |
drh | 92f02c3 | 2004-09-02 14:57:08 +0000 | [diff] [blame] | 2132 | return SQLITE_OK; |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 2133 | } |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 2134 | checkActiveVdbeCnt(db); |
danielk1977 | 261919c | 2005-12-06 12:52:59 +0000 | [diff] [blame] | 2135 | |
danielk1977 | 07cb560 | 2006-01-20 10:55:05 +0000 | [diff] [blame] | 2136 | /* No commit or rollback needed if the program never started */ |
| 2137 | if( p->pc>=0 ){ |
drh | aac2f55 | 2006-09-23 21:44:23 +0000 | [diff] [blame] | 2138 | int mrc; /* Primary error code from p->rc */ |
danielk1977 | bd43455 | 2009-03-18 10:33:00 +0000 | [diff] [blame] | 2139 | int eStatementOp = 0; |
| 2140 | int isSpecialError; /* Set to true if a 'special' error */ |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 2141 | |
| 2142 | /* Lock all btrees used by the statement */ |
drh | bdaec52 | 2011-04-04 00:14:43 +0000 | [diff] [blame] | 2143 | sqlite3VdbeEnter(p); |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 2144 | |
drh | 71b890a | 2007-10-03 15:30:52 +0000 | [diff] [blame] | 2145 | /* Check for one of the special errors */ |
drh | aac2f55 | 2006-09-23 21:44:23 +0000 | [diff] [blame] | 2146 | mrc = p->rc & 0xff; |
drh | fa3be90 | 2009-07-07 02:44:07 +0000 | [diff] [blame] | 2147 | assert( p->rc!=SQLITE_IOERR_BLOCKED ); /* This error no longer exists */ |
drh | 71b890a | 2007-10-03 15:30:52 +0000 | [diff] [blame] | 2148 | isSpecialError = mrc==SQLITE_NOMEM || mrc==SQLITE_IOERR |
drh | 77658e2 | 2007-12-04 16:54:52 +0000 | [diff] [blame] | 2149 | || mrc==SQLITE_INTERRUPT || mrc==SQLITE_FULL; |
danielk1977 | 07cb560 | 2006-01-20 10:55:05 +0000 | [diff] [blame] | 2150 | if( isSpecialError ){ |
dan | 5653e4d | 2010-08-12 11:25:47 +0000 | [diff] [blame] | 2151 | /* If the query was read-only and the error code is SQLITE_INTERRUPT, |
| 2152 | ** no rollback is necessary. Otherwise, at least a savepoint |
| 2153 | ** transaction must be rolled back to restore the database to a |
| 2154 | ** consistent state. |
| 2155 | ** |
| 2156 | ** Even if the statement is read-only, it is important to perform |
| 2157 | ** a statement or transaction rollback operation. If the error |
| 2158 | ** occured while writing to the journal, sub-journal or database |
| 2159 | ** file as part of an effort to free up cache space (see function |
| 2160 | ** pagerStress() in pager.c), the rollback is required to restore |
| 2161 | ** the pager to a consistent state. |
danielk1977 | 07cb560 | 2006-01-20 10:55:05 +0000 | [diff] [blame] | 2162 | */ |
drh | ad4a4b8 | 2008-11-05 16:37:34 +0000 | [diff] [blame] | 2163 | if( !p->readOnly || mrc!=SQLITE_INTERRUPT ){ |
drh | fa3be90 | 2009-07-07 02:44:07 +0000 | [diff] [blame] | 2164 | if( (mrc==SQLITE_NOMEM || mrc==SQLITE_FULL) && p->usesStmtJournal ){ |
danielk1977 | bd43455 | 2009-03-18 10:33:00 +0000 | [diff] [blame] | 2165 | eStatementOp = SAVEPOINT_ROLLBACK; |
danielk1977 | 07cb560 | 2006-01-20 10:55:05 +0000 | [diff] [blame] | 2166 | }else{ |
| 2167 | /* We are forced to roll back the active transaction. Before doing |
| 2168 | ** so, abort any other statements this handle currently has active. |
| 2169 | */ |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 2170 | invalidateCursorsOnModifiedBtrees(db); |
danielk1977 | 97a227c | 2006-01-20 16:32:04 +0000 | [diff] [blame] | 2171 | sqlite3RollbackAll(db); |
danielk1977 | fc158bf | 2009-01-07 08:12:16 +0000 | [diff] [blame] | 2172 | sqlite3CloseSavepoints(db); |
danielk1977 | 07cb560 | 2006-01-20 10:55:05 +0000 | [diff] [blame] | 2173 | db->autoCommit = 1; |
| 2174 | } |
danielk1977 | 261919c | 2005-12-06 12:52:59 +0000 | [diff] [blame] | 2175 | } |
| 2176 | } |
dan | 32b09f2 | 2009-09-23 17:29:59 +0000 | [diff] [blame] | 2177 | |
| 2178 | /* Check for immediate foreign key violations. */ |
| 2179 | if( p->rc==SQLITE_OK ){ |
| 2180 | sqlite3VdbeCheckFk(p, 0); |
| 2181 | } |
danielk1977 | 07cb560 | 2006-01-20 10:55:05 +0000 | [diff] [blame] | 2182 | |
danielk1977 | bd43455 | 2009-03-18 10:33:00 +0000 | [diff] [blame] | 2183 | /* If the auto-commit flag is set and this is the only active writer |
| 2184 | ** VM, then we do either a commit or rollback of the current transaction. |
danielk1977 | 07cb560 | 2006-01-20 10:55:05 +0000 | [diff] [blame] | 2185 | ** |
| 2186 | ** Note: This block also runs if one of the special errors handled |
drh | ad4a4b8 | 2008-11-05 16:37:34 +0000 | [diff] [blame] | 2187 | ** above has occurred. |
danielk1977 | 07cb560 | 2006-01-20 10:55:05 +0000 | [diff] [blame] | 2188 | */ |
danielk1977 | 093e0f6 | 2008-11-13 18:00:14 +0000 | [diff] [blame] | 2189 | if( !sqlite3VtabInSync(db) |
| 2190 | && db->autoCommit |
| 2191 | && db->writeVdbeCnt==(p->readOnly==0) |
| 2192 | ){ |
danielk1977 | 07cb560 | 2006-01-20 10:55:05 +0000 | [diff] [blame] | 2193 | if( p->rc==SQLITE_OK || (p->errorAction==OE_Fail && !isSpecialError) ){ |
dan | 19611b1 | 2011-01-24 16:00:58 +0000 | [diff] [blame] | 2194 | rc = sqlite3VdbeCheckFk(p, 1); |
| 2195 | if( rc!=SQLITE_OK ){ |
drh | e9ce585 | 2011-02-11 22:54:28 +0000 | [diff] [blame] | 2196 | if( NEVER(p->readOnly) ){ |
drh | bdaec52 | 2011-04-04 00:14:43 +0000 | [diff] [blame] | 2197 | sqlite3VdbeLeave(p); |
dan | 19611b1 | 2011-01-24 16:00:58 +0000 | [diff] [blame] | 2198 | return SQLITE_ERROR; |
| 2199 | } |
| 2200 | rc = SQLITE_CONSTRAINT; |
| 2201 | }else{ |
| 2202 | /* The auto-commit flag is true, the vdbe program was successful |
| 2203 | ** or hit an 'OR FAIL' constraint and there are no deferred foreign |
| 2204 | ** key constraints to hold up the transaction. This means a commit |
| 2205 | ** is required. */ |
| 2206 | rc = vdbeCommit(db, p); |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 2207 | } |
dan | 19611b1 | 2011-01-24 16:00:58 +0000 | [diff] [blame] | 2208 | if( rc==SQLITE_BUSY && p->readOnly ){ |
drh | bdaec52 | 2011-04-04 00:14:43 +0000 | [diff] [blame] | 2209 | sqlite3VdbeLeave(p); |
danielk1977 | 07cb560 | 2006-01-20 10:55:05 +0000 | [diff] [blame] | 2210 | return SQLITE_BUSY; |
| 2211 | }else if( rc!=SQLITE_OK ){ |
| 2212 | p->rc = rc; |
danielk1977 | 97a227c | 2006-01-20 16:32:04 +0000 | [diff] [blame] | 2213 | sqlite3RollbackAll(db); |
danielk1977 | 07cb560 | 2006-01-20 10:55:05 +0000 | [diff] [blame] | 2214 | }else{ |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 2215 | db->nDeferredCons = 0; |
danielk1977 | 07cb560 | 2006-01-20 10:55:05 +0000 | [diff] [blame] | 2216 | sqlite3CommitInternalChanges(db); |
| 2217 | } |
| 2218 | }else{ |
danielk1977 | 97a227c | 2006-01-20 16:32:04 +0000 | [diff] [blame] | 2219 | sqlite3RollbackAll(db); |
danielk1977 | 07cb560 | 2006-01-20 10:55:05 +0000 | [diff] [blame] | 2220 | } |
danielk1977 | bd43455 | 2009-03-18 10:33:00 +0000 | [diff] [blame] | 2221 | db->nStatement = 0; |
| 2222 | }else if( eStatementOp==0 ){ |
danielk1977 | 07cb560 | 2006-01-20 10:55:05 +0000 | [diff] [blame] | 2223 | if( p->rc==SQLITE_OK || p->errorAction==OE_Fail ){ |
danielk1977 | bd43455 | 2009-03-18 10:33:00 +0000 | [diff] [blame] | 2224 | eStatementOp = SAVEPOINT_RELEASE; |
danielk1977 | 07cb560 | 2006-01-20 10:55:05 +0000 | [diff] [blame] | 2225 | }else if( p->errorAction==OE_Abort ){ |
danielk1977 | bd43455 | 2009-03-18 10:33:00 +0000 | [diff] [blame] | 2226 | eStatementOp = SAVEPOINT_ROLLBACK; |
danielk1977 | 07cb560 | 2006-01-20 10:55:05 +0000 | [diff] [blame] | 2227 | }else{ |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 2228 | invalidateCursorsOnModifiedBtrees(db); |
danielk1977 | 97a227c | 2006-01-20 16:32:04 +0000 | [diff] [blame] | 2229 | sqlite3RollbackAll(db); |
danielk1977 | fc158bf | 2009-01-07 08:12:16 +0000 | [diff] [blame] | 2230 | sqlite3CloseSavepoints(db); |
danielk1977 | 07cb560 | 2006-01-20 10:55:05 +0000 | [diff] [blame] | 2231 | db->autoCommit = 1; |
| 2232 | } |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 2233 | } |
danielk1977 | 07cb560 | 2006-01-20 10:55:05 +0000 | [diff] [blame] | 2234 | |
danielk1977 | bd43455 | 2009-03-18 10:33:00 +0000 | [diff] [blame] | 2235 | /* If eStatementOp is non-zero, then a statement transaction needs to |
| 2236 | ** be committed or rolled back. Call sqlite3VdbeCloseStatement() to |
| 2237 | ** do so. If this operation returns an error, and the current statement |
drh | 3517324 | 2010-03-08 21:40:13 +0000 | [diff] [blame] | 2238 | ** error code is SQLITE_OK or SQLITE_CONSTRAINT, then promote the |
| 2239 | ** current statement error code. |
danielk1977 | 07cb560 | 2006-01-20 10:55:05 +0000 | [diff] [blame] | 2240 | */ |
danielk1977 | bd43455 | 2009-03-18 10:33:00 +0000 | [diff] [blame] | 2241 | if( eStatementOp ){ |
| 2242 | rc = sqlite3VdbeCloseStatement(p, eStatementOp); |
dan | 40ad9d2 | 2010-06-03 09:17:38 +0000 | [diff] [blame] | 2243 | if( rc ){ |
drh | 346506f | 2011-05-25 01:16:42 +0000 | [diff] [blame] | 2244 | if( p->rc==SQLITE_OK || p->rc==SQLITE_CONSTRAINT ){ |
dan | 40ad9d2 | 2010-06-03 09:17:38 +0000 | [diff] [blame] | 2245 | p->rc = rc; |
| 2246 | sqlite3DbFree(db, p->zErrMsg); |
| 2247 | p->zErrMsg = 0; |
| 2248 | } |
| 2249 | invalidateCursorsOnModifiedBtrees(db); |
| 2250 | sqlite3RollbackAll(db); |
| 2251 | sqlite3CloseSavepoints(db); |
| 2252 | db->autoCommit = 1; |
danielk1977 | 07cb560 | 2006-01-20 10:55:05 +0000 | [diff] [blame] | 2253 | } |
danielk1977 | 77d83ba | 2004-05-31 10:08:14 +0000 | [diff] [blame] | 2254 | } |
danielk1977 | 07cb560 | 2006-01-20 10:55:05 +0000 | [diff] [blame] | 2255 | |
danielk1977 | bd43455 | 2009-03-18 10:33:00 +0000 | [diff] [blame] | 2256 | /* If this was an INSERT, UPDATE or DELETE and no statement transaction |
| 2257 | ** has been rolled back, update the database connection change-counter. |
danielk1977 | 07cb560 | 2006-01-20 10:55:05 +0000 | [diff] [blame] | 2258 | */ |
drh | 6be240e | 2009-07-14 02:33:02 +0000 | [diff] [blame] | 2259 | if( p->changeCntOn ){ |
danielk1977 | bd43455 | 2009-03-18 10:33:00 +0000 | [diff] [blame] | 2260 | if( eStatementOp!=SAVEPOINT_ROLLBACK ){ |
danielk1977 | 07cb560 | 2006-01-20 10:55:05 +0000 | [diff] [blame] | 2261 | sqlite3VdbeSetChanges(db, p->nChange); |
| 2262 | }else{ |
| 2263 | sqlite3VdbeSetChanges(db, 0); |
| 2264 | } |
| 2265 | p->nChange = 0; |
danielk1977 | b28af71 | 2004-06-21 06:50:26 +0000 | [diff] [blame] | 2266 | } |
danielk1977 | 07cb560 | 2006-01-20 10:55:05 +0000 | [diff] [blame] | 2267 | |
| 2268 | /* Rollback or commit any schema changes that occurred. */ |
| 2269 | if( p->rc!=SQLITE_OK && db->flags&SQLITE_InternChanges ){ |
drh | c7792fa | 2011-04-02 16:28:52 +0000 | [diff] [blame] | 2270 | sqlite3ResetInternalSchema(db, -1); |
danielk1977 | 07cb560 | 2006-01-20 10:55:05 +0000 | [diff] [blame] | 2271 | db->flags = (db->flags | SQLITE_InternChanges); |
| 2272 | } |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 2273 | |
| 2274 | /* Release the locks */ |
drh | bdaec52 | 2011-04-04 00:14:43 +0000 | [diff] [blame] | 2275 | sqlite3VdbeLeave(p); |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 2276 | } |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 2277 | |
danielk1977 | 65fd59f | 2006-06-24 11:51:33 +0000 | [diff] [blame] | 2278 | /* We have successfully halted and closed the VM. Record this fact. */ |
| 2279 | if( p->pc>=0 ){ |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 2280 | db->activeVdbeCnt--; |
drh | ad4a4b8 | 2008-11-05 16:37:34 +0000 | [diff] [blame] | 2281 | if( !p->readOnly ){ |
| 2282 | db->writeVdbeCnt--; |
| 2283 | } |
| 2284 | assert( db->activeVdbeCnt>=db->writeVdbeCnt ); |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 2285 | } |
drh | 92f02c3 | 2004-09-02 14:57:08 +0000 | [diff] [blame] | 2286 | p->magic = VDBE_MAGIC_HALT; |
| 2287 | checkActiveVdbeCnt(db); |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 2288 | if( p->db->mallocFailed ){ |
| 2289 | p->rc = SQLITE_NOMEM; |
| 2290 | } |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 2291 | |
danielk1977 | 404ca07 | 2009-03-16 13:19:36 +0000 | [diff] [blame] | 2292 | /* If the auto-commit flag is set to true, then any locks that were held |
| 2293 | ** by connection db have now been released. Call sqlite3ConnectionUnlocked() |
| 2294 | ** to invoke any required unlock-notify callbacks. |
| 2295 | */ |
| 2296 | if( db->autoCommit ){ |
| 2297 | sqlite3ConnectionUnlocked(db); |
| 2298 | } |
| 2299 | |
danielk1977 | bd43455 | 2009-03-18 10:33:00 +0000 | [diff] [blame] | 2300 | assert( db->activeVdbeCnt>0 || db->autoCommit==0 || db->nStatement==0 ); |
dan | 19611b1 | 2011-01-24 16:00:58 +0000 | [diff] [blame] | 2301 | return (p->rc==SQLITE_BUSY ? SQLITE_BUSY : SQLITE_OK); |
drh | 92f02c3 | 2004-09-02 14:57:08 +0000 | [diff] [blame] | 2302 | } |
drh | 4cf7c7f | 2007-08-28 23:28:07 +0000 | [diff] [blame] | 2303 | |
drh | 92f02c3 | 2004-09-02 14:57:08 +0000 | [diff] [blame] | 2304 | |
| 2305 | /* |
drh | 3c23a88 | 2007-01-09 14:01:13 +0000 | [diff] [blame] | 2306 | ** Each VDBE holds the result of the most recent sqlite3_step() call |
| 2307 | ** in p->rc. This routine sets that result back to SQLITE_OK. |
| 2308 | */ |
| 2309 | void sqlite3VdbeResetStepResult(Vdbe *p){ |
| 2310 | p->rc = SQLITE_OK; |
| 2311 | } |
| 2312 | |
| 2313 | /* |
drh | 92f02c3 | 2004-09-02 14:57:08 +0000 | [diff] [blame] | 2314 | ** Clean up a VDBE after execution but do not delete the VDBE just yet. |
| 2315 | ** Write any error messages into *pzErrMsg. Return the result code. |
| 2316 | ** |
| 2317 | ** After this routine is run, the VDBE should be ready to be executed |
| 2318 | ** again. |
| 2319 | ** |
| 2320 | ** To look at it another way, this routine resets the state of the |
| 2321 | ** virtual machine from VDBE_MAGIC_RUN or VDBE_MAGIC_HALT back to |
| 2322 | ** VDBE_MAGIC_INIT. |
| 2323 | */ |
drh | c890fec | 2008-08-01 20:10:08 +0000 | [diff] [blame] | 2324 | int sqlite3VdbeReset(Vdbe *p){ |
drh | 4ac285a | 2006-09-15 07:28:50 +0000 | [diff] [blame] | 2325 | sqlite3 *db; |
drh | 4ac285a | 2006-09-15 07:28:50 +0000 | [diff] [blame] | 2326 | db = p->db; |
drh | 92f02c3 | 2004-09-02 14:57:08 +0000 | [diff] [blame] | 2327 | |
| 2328 | /* If the VM did not run to completion or if it encountered an |
| 2329 | ** error, then it might not have been halted properly. So halt |
| 2330 | ** it now. |
| 2331 | */ |
| 2332 | sqlite3VdbeHalt(p); |
| 2333 | |
drh | fb7e765 | 2005-01-24 00:28:42 +0000 | [diff] [blame] | 2334 | /* If the VDBE has be run even partially, then transfer the error code |
| 2335 | ** and error message from the VDBE into the main database structure. But |
| 2336 | ** if the VDBE has just been set to run but has not actually executed any |
| 2337 | ** instructions yet, leave the main database error information unchanged. |
drh | 92f02c3 | 2004-09-02 14:57:08 +0000 | [diff] [blame] | 2338 | */ |
drh | fb7e765 | 2005-01-24 00:28:42 +0000 | [diff] [blame] | 2339 | if( p->pc>=0 ){ |
| 2340 | if( p->zErrMsg ){ |
danielk1977 | 9ff3f3f | 2008-10-11 17:51:38 +0000 | [diff] [blame] | 2341 | sqlite3BeginBenignMalloc(); |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 2342 | sqlite3ValueSetStr(db->pErr,-1,p->zErrMsg,SQLITE_UTF8,SQLITE_TRANSIENT); |
danielk1977 | 9ff3f3f | 2008-10-11 17:51:38 +0000 | [diff] [blame] | 2343 | sqlite3EndBenignMalloc(); |
danielk1977 | 97a227c | 2006-01-20 16:32:04 +0000 | [diff] [blame] | 2344 | db->errCode = p->rc; |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 2345 | sqlite3DbFree(db, p->zErrMsg); |
drh | fb7e765 | 2005-01-24 00:28:42 +0000 | [diff] [blame] | 2346 | p->zErrMsg = 0; |
| 2347 | }else if( p->rc ){ |
drh | 4ac285a | 2006-09-15 07:28:50 +0000 | [diff] [blame] | 2348 | sqlite3Error(db, p->rc, 0); |
drh | fb7e765 | 2005-01-24 00:28:42 +0000 | [diff] [blame] | 2349 | }else{ |
drh | 4ac285a | 2006-09-15 07:28:50 +0000 | [diff] [blame] | 2350 | sqlite3Error(db, SQLITE_OK, 0); |
drh | fb7e765 | 2005-01-24 00:28:42 +0000 | [diff] [blame] | 2351 | } |
drh | 4611d92 | 2010-02-25 14:47:01 +0000 | [diff] [blame] | 2352 | if( p->runOnlyOnce ) p->expired = 1; |
danielk1977 | a21c6b6 | 2005-01-24 10:25:59 +0000 | [diff] [blame] | 2353 | }else if( p->rc && p->expired ){ |
| 2354 | /* The expired flag was set on the VDBE before the first call |
| 2355 | ** to sqlite3_step(). For consistency (since sqlite3_step() was |
| 2356 | ** called), set the database error in this case as well. |
| 2357 | */ |
drh | 4ac285a | 2006-09-15 07:28:50 +0000 | [diff] [blame] | 2358 | sqlite3Error(db, p->rc, 0); |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 2359 | sqlite3ValueSetStr(db->pErr, -1, p->zErrMsg, SQLITE_UTF8, SQLITE_TRANSIENT); |
| 2360 | sqlite3DbFree(db, p->zErrMsg); |
danielk1977 | 8e55652 | 2007-11-13 10:30:24 +0000 | [diff] [blame] | 2361 | p->zErrMsg = 0; |
drh | 92f02c3 | 2004-09-02 14:57:08 +0000 | [diff] [blame] | 2362 | } |
| 2363 | |
| 2364 | /* Reclaim all memory used by the VDBE |
| 2365 | */ |
drh | c890fec | 2008-08-01 20:10:08 +0000 | [diff] [blame] | 2366 | Cleanup(p); |
drh | 92f02c3 | 2004-09-02 14:57:08 +0000 | [diff] [blame] | 2367 | |
| 2368 | /* Save profiling information from this VDBE run. |
| 2369 | */ |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 2370 | #ifdef VDBE_PROFILE |
| 2371 | { |
| 2372 | FILE *out = fopen("vdbe_profile.out", "a"); |
| 2373 | if( out ){ |
| 2374 | int i; |
| 2375 | fprintf(out, "---- "); |
| 2376 | for(i=0; i<p->nOp; i++){ |
| 2377 | fprintf(out, "%02x", p->aOp[i].opcode); |
| 2378 | } |
| 2379 | fprintf(out, "\n"); |
| 2380 | for(i=0; i<p->nOp; i++){ |
| 2381 | fprintf(out, "%6d %10lld %8lld ", |
| 2382 | p->aOp[i].cnt, |
| 2383 | p->aOp[i].cycles, |
| 2384 | p->aOp[i].cnt>0 ? p->aOp[i].cycles/p->aOp[i].cnt : 0 |
| 2385 | ); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2386 | sqlite3VdbePrintOp(out, i, &p->aOp[i]); |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 2387 | } |
| 2388 | fclose(out); |
| 2389 | } |
| 2390 | } |
| 2391 | #endif |
| 2392 | p->magic = VDBE_MAGIC_INIT; |
drh | 4ac285a | 2006-09-15 07:28:50 +0000 | [diff] [blame] | 2393 | return p->rc & db->errMask; |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 2394 | } |
drh | 92f02c3 | 2004-09-02 14:57:08 +0000 | [diff] [blame] | 2395 | |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 2396 | /* |
| 2397 | ** Clean up and delete a VDBE after execution. Return an integer which is |
| 2398 | ** the result code. Write any error message text into *pzErrMsg. |
| 2399 | */ |
danielk1977 | 9e6db7d | 2004-06-21 08:18:51 +0000 | [diff] [blame] | 2400 | int sqlite3VdbeFinalize(Vdbe *p){ |
danielk1977 | b5548a8 | 2004-06-26 13:51:33 +0000 | [diff] [blame] | 2401 | int rc = SQLITE_OK; |
danielk1977 | b5548a8 | 2004-06-26 13:51:33 +0000 | [diff] [blame] | 2402 | if( p->magic==VDBE_MAGIC_RUN || p->magic==VDBE_MAGIC_HALT ){ |
drh | c890fec | 2008-08-01 20:10:08 +0000 | [diff] [blame] | 2403 | rc = sqlite3VdbeReset(p); |
drh | 4ac285a | 2006-09-15 07:28:50 +0000 | [diff] [blame] | 2404 | assert( (rc & p->db->errMask)==rc ); |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 2405 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2406 | sqlite3VdbeDelete(p); |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 2407 | return rc; |
| 2408 | } |
| 2409 | |
| 2410 | /* |
drh | f92c7ff | 2004-06-19 15:40:23 +0000 | [diff] [blame] | 2411 | ** Call the destructor for each auxdata entry in pVdbeFunc for which |
danielk1977 | e159fdf | 2004-06-21 10:45:06 +0000 | [diff] [blame] | 2412 | ** the corresponding bit in mask is clear. Auxdata entries beyond 31 |
drh | f92c7ff | 2004-06-19 15:40:23 +0000 | [diff] [blame] | 2413 | ** are always destroyed. To destroy all auxdata entries, call this |
danielk1977 | e159fdf | 2004-06-21 10:45:06 +0000 | [diff] [blame] | 2414 | ** routine with mask==0. |
drh | f92c7ff | 2004-06-19 15:40:23 +0000 | [diff] [blame] | 2415 | */ |
| 2416 | void sqlite3VdbeDeleteAuxData(VdbeFunc *pVdbeFunc, int mask){ |
| 2417 | int i; |
| 2418 | for(i=0; i<pVdbeFunc->nAux; i++){ |
| 2419 | struct AuxData *pAux = &pVdbeFunc->apAux[i]; |
drh | 3500ed6 | 2009-05-05 15:46:43 +0000 | [diff] [blame] | 2420 | if( (i>31 || !(mask&(((u32)1)<<i))) && pAux->pAux ){ |
drh | f92c7ff | 2004-06-19 15:40:23 +0000 | [diff] [blame] | 2421 | if( pAux->xDelete ){ |
| 2422 | pAux->xDelete(pAux->pAux); |
| 2423 | } |
| 2424 | pAux->pAux = 0; |
| 2425 | } |
| 2426 | } |
| 2427 | } |
| 2428 | |
| 2429 | /* |
dan | d46def7 | 2010-07-24 11:28:28 +0000 | [diff] [blame] | 2430 | ** Free all memory associated with the Vdbe passed as the second argument. |
| 2431 | ** The difference between this function and sqlite3VdbeDelete() is that |
| 2432 | ** VdbeDelete() also unlinks the Vdbe from the list of VMs associated with |
| 2433 | ** the database connection. |
| 2434 | */ |
| 2435 | void sqlite3VdbeDeleteObject(sqlite3 *db, Vdbe *p){ |
dan | d19c933 | 2010-07-26 12:05:17 +0000 | [diff] [blame] | 2436 | SubProgram *pSub, *pNext; |
drh | 124c0b4 | 2011-06-01 18:15:55 +0000 | [diff] [blame] | 2437 | int i; |
dan | d46def7 | 2010-07-24 11:28:28 +0000 | [diff] [blame] | 2438 | assert( p->db==0 || p->db==db ); |
| 2439 | releaseMemArray(p->aVar, p->nVar); |
| 2440 | releaseMemArray(p->aColName, p->nResColumn*COLNAME_N); |
dan | d19c933 | 2010-07-26 12:05:17 +0000 | [diff] [blame] | 2441 | for(pSub=p->pProgram; pSub; pSub=pNext){ |
| 2442 | pNext = pSub->pNext; |
| 2443 | vdbeFreeOpArray(db, pSub->aOp, pSub->nOp); |
| 2444 | sqlite3DbFree(db, pSub); |
| 2445 | } |
drh | 124c0b4 | 2011-06-01 18:15:55 +0000 | [diff] [blame] | 2446 | for(i=p->nzVar-1; i>=0; i--) sqlite3DbFree(db, p->azVar[i]); |
dan | d46def7 | 2010-07-24 11:28:28 +0000 | [diff] [blame] | 2447 | vdbeFreeOpArray(db, p->aOp, p->nOp); |
| 2448 | sqlite3DbFree(db, p->aLabel); |
| 2449 | sqlite3DbFree(db, p->aColName); |
| 2450 | sqlite3DbFree(db, p->zSql); |
| 2451 | sqlite3DbFree(db, p->pFree); |
| 2452 | sqlite3DbFree(db, p); |
| 2453 | } |
| 2454 | |
| 2455 | /* |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 2456 | ** Delete an entire VDBE. |
| 2457 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2458 | void sqlite3VdbeDelete(Vdbe *p){ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 2459 | sqlite3 *db; |
| 2460 | |
drh | fa3be90 | 2009-07-07 02:44:07 +0000 | [diff] [blame] | 2461 | if( NEVER(p==0) ) return; |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 2462 | db = p->db; |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 2463 | if( p->pPrev ){ |
| 2464 | p->pPrev->pNext = p->pNext; |
| 2465 | }else{ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 2466 | assert( db->pVdbe==p ); |
| 2467 | db->pVdbe = p->pNext; |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 2468 | } |
| 2469 | if( p->pNext ){ |
| 2470 | p->pNext->pPrev = p->pPrev; |
| 2471 | } |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 2472 | p->magic = VDBE_MAGIC_DEAD; |
drh | 87f5c5f | 2010-01-20 01:20:56 +0000 | [diff] [blame] | 2473 | p->db = 0; |
dan | d46def7 | 2010-07-24 11:28:28 +0000 | [diff] [blame] | 2474 | sqlite3VdbeDeleteObject(db, p); |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 2475 | } |
drh | a11846b | 2004-01-07 18:52:56 +0000 | [diff] [blame] | 2476 | |
| 2477 | /* |
drh | 9a65f2c | 2009-06-22 19:05:40 +0000 | [diff] [blame] | 2478 | ** Make sure the cursor p is ready to read or write the row to which it |
| 2479 | ** was last positioned. Return an error code if an OOM fault or I/O error |
| 2480 | ** prevents us from positioning the cursor to its correct position. |
| 2481 | ** |
drh | a11846b | 2004-01-07 18:52:56 +0000 | [diff] [blame] | 2482 | ** If a MoveTo operation is pending on the given cursor, then do that |
drh | 9a65f2c | 2009-06-22 19:05:40 +0000 | [diff] [blame] | 2483 | ** MoveTo now. If no move is pending, check to see if the row has been |
| 2484 | ** deleted out from under the cursor and if it has, mark the row as |
| 2485 | ** a NULL row. |
| 2486 | ** |
| 2487 | ** If the cursor is already pointing to the correct row and that row has |
| 2488 | ** not been deleted out from under the cursor, then this routine is a no-op. |
drh | a11846b | 2004-01-07 18:52:56 +0000 | [diff] [blame] | 2489 | */ |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 2490 | int sqlite3VdbeCursorMoveto(VdbeCursor *p){ |
drh | a11846b | 2004-01-07 18:52:56 +0000 | [diff] [blame] | 2491 | if( p->deferredMoveto ){ |
drh | 536065a | 2005-01-26 21:55:31 +0000 | [diff] [blame] | 2492 | int res, rc; |
adamd | 4fc9308 | 2006-09-14 16:57:19 +0000 | [diff] [blame] | 2493 | #ifdef SQLITE_TEST |
danielk1977 | 132872b | 2004-05-10 10:37:18 +0000 | [diff] [blame] | 2494 | extern int sqlite3_search_count; |
adamd | 4fc9308 | 2006-09-14 16:57:19 +0000 | [diff] [blame] | 2495 | #endif |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 2496 | assert( p->isTable ); |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 2497 | rc = sqlite3BtreeMovetoUnpacked(p->pCursor, 0, p->movetoTarget, 0, &res); |
drh | 536065a | 2005-01-26 21:55:31 +0000 | [diff] [blame] | 2498 | if( rc ) return rc; |
drh | aa73609 | 2009-06-22 00:55:30 +0000 | [diff] [blame] | 2499 | p->lastRowid = p->movetoTarget; |
drh | be0b237 | 2010-07-30 18:40:55 +0000 | [diff] [blame] | 2500 | if( res!=0 ) return SQLITE_CORRUPT_BKPT; |
| 2501 | p->rowidIsValid = 1; |
drh | 10cfdd5 | 2006-08-08 15:42:59 +0000 | [diff] [blame] | 2502 | #ifdef SQLITE_TEST |
danielk1977 | 132872b | 2004-05-10 10:37:18 +0000 | [diff] [blame] | 2503 | sqlite3_search_count++; |
drh | 10cfdd5 | 2006-08-08 15:42:59 +0000 | [diff] [blame] | 2504 | #endif |
drh | a11846b | 2004-01-07 18:52:56 +0000 | [diff] [blame] | 2505 | p->deferredMoveto = 0; |
drh | 76873ab | 2006-01-07 18:48:26 +0000 | [diff] [blame] | 2506 | p->cacheStatus = CACHE_STALE; |
drh | 6be240e | 2009-07-14 02:33:02 +0000 | [diff] [blame] | 2507 | }else if( ALWAYS(p->pCursor) ){ |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 2508 | int hasMoved; |
| 2509 | int rc = sqlite3BtreeCursorHasMoved(p->pCursor, &hasMoved); |
| 2510 | if( rc ) return rc; |
| 2511 | if( hasMoved ){ |
| 2512 | p->cacheStatus = CACHE_STALE; |
| 2513 | p->nullRow = 1; |
| 2514 | } |
drh | a11846b | 2004-01-07 18:52:56 +0000 | [diff] [blame] | 2515 | } |
| 2516 | return SQLITE_OK; |
| 2517 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2518 | |
drh | ab9f7f1 | 2004-05-08 10:56:11 +0000 | [diff] [blame] | 2519 | /* |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 2520 | ** The following functions: |
danielk1977 | 90e4d95 | 2004-05-10 10:05:53 +0000 | [diff] [blame] | 2521 | ** |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 2522 | ** sqlite3VdbeSerialType() |
| 2523 | ** sqlite3VdbeSerialTypeLen() |
danielk1977 | 90e4d95 | 2004-05-10 10:05:53 +0000 | [diff] [blame] | 2524 | ** sqlite3VdbeSerialLen() |
shane | 9200309 | 2008-07-31 01:43:13 +0000 | [diff] [blame] | 2525 | ** sqlite3VdbeSerialPut() |
| 2526 | ** sqlite3VdbeSerialGet() |
danielk1977 | 90e4d95 | 2004-05-10 10:05:53 +0000 | [diff] [blame] | 2527 | ** |
| 2528 | ** encapsulate the code that serializes values for storage in SQLite |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 2529 | ** data and index records. Each serialized value consists of a |
| 2530 | ** 'serial-type' and a blob of data. The serial type is an 8-byte unsigned |
| 2531 | ** integer, stored as a varint. |
danielk1977 | 90e4d95 | 2004-05-10 10:05:53 +0000 | [diff] [blame] | 2532 | ** |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 2533 | ** In an SQLite index record, the serial type is stored directly before |
| 2534 | ** the blob of data that it corresponds to. In a table record, all serial |
| 2535 | ** types are stored at the start of the record, and the blobs of data at |
| 2536 | ** the end. Hence these functions allow the caller to handle the |
| 2537 | ** serial-type and data blob seperately. |
| 2538 | ** |
| 2539 | ** The following table describes the various storage classes for data: |
| 2540 | ** |
| 2541 | ** serial type bytes of data type |
danielk1977 | 90e4d95 | 2004-05-10 10:05:53 +0000 | [diff] [blame] | 2542 | ** -------------- --------------- --------------- |
drh | a19b775 | 2004-05-30 21:14:58 +0000 | [diff] [blame] | 2543 | ** 0 0 NULL |
danielk1977 | 90e4d95 | 2004-05-10 10:05:53 +0000 | [diff] [blame] | 2544 | ** 1 1 signed integer |
| 2545 | ** 2 2 signed integer |
drh | a19b775 | 2004-05-30 21:14:58 +0000 | [diff] [blame] | 2546 | ** 3 3 signed integer |
| 2547 | ** 4 4 signed integer |
| 2548 | ** 5 6 signed integer |
| 2549 | ** 6 8 signed integer |
| 2550 | ** 7 8 IEEE float |
drh | d946db0 | 2005-12-29 19:23:06 +0000 | [diff] [blame] | 2551 | ** 8 0 Integer constant 0 |
| 2552 | ** 9 0 Integer constant 1 |
| 2553 | ** 10,11 reserved for expansion |
danielk1977 | 90e4d95 | 2004-05-10 10:05:53 +0000 | [diff] [blame] | 2554 | ** N>=12 and even (N-12)/2 BLOB |
| 2555 | ** N>=13 and odd (N-13)/2 text |
| 2556 | ** |
drh | 35a5965 | 2006-01-02 18:24:40 +0000 | [diff] [blame] | 2557 | ** The 8 and 9 types were added in 3.3.0, file format 4. Prior versions |
| 2558 | ** of SQLite will not understand those serial types. |
danielk1977 | 90e4d95 | 2004-05-10 10:05:53 +0000 | [diff] [blame] | 2559 | */ |
| 2560 | |
| 2561 | /* |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 2562 | ** Return the serial-type for the value stored in pMem. |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2563 | */ |
drh | d946db0 | 2005-12-29 19:23:06 +0000 | [diff] [blame] | 2564 | u32 sqlite3VdbeSerialType(Mem *pMem, int file_format){ |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 2565 | int flags = pMem->flags; |
drh | fdf972a | 2007-05-02 13:30:27 +0000 | [diff] [blame] | 2566 | int n; |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 2567 | |
| 2568 | if( flags&MEM_Null ){ |
drh | a19b775 | 2004-05-30 21:14:58 +0000 | [diff] [blame] | 2569 | return 0; |
danielk1977 | 90e4d95 | 2004-05-10 10:05:53 +0000 | [diff] [blame] | 2570 | } |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 2571 | if( flags&MEM_Int ){ |
drh | fe2093d | 2005-01-20 22:48:47 +0000 | [diff] [blame] | 2572 | /* Figure out whether to use 1, 2, 4, 6 or 8 bytes. */ |
drh | 5284a05 | 2008-05-08 15:18:10 +0000 | [diff] [blame] | 2573 | # define MAX_6BYTE ((((i64)0x00008000)<<32)-1) |
drh | 3c024d6 | 2007-03-30 11:23:45 +0000 | [diff] [blame] | 2574 | i64 i = pMem->u.i; |
drh | d946db0 | 2005-12-29 19:23:06 +0000 | [diff] [blame] | 2575 | u64 u; |
| 2576 | if( file_format>=4 && (i&1)==i ){ |
drh | 8df3284 | 2008-12-09 02:51:23 +0000 | [diff] [blame] | 2577 | return 8+(u32)i; |
drh | d946db0 | 2005-12-29 19:23:06 +0000 | [diff] [blame] | 2578 | } |
drh | cfd654b | 2011-03-05 13:54:15 +0000 | [diff] [blame] | 2579 | if( i<0 ){ |
| 2580 | if( i<(-MAX_6BYTE) ) return 6; |
| 2581 | /* Previous test prevents: u = -(-9223372036854775808) */ |
| 2582 | u = -i; |
| 2583 | }else{ |
| 2584 | u = i; |
| 2585 | } |
drh | 5742b63 | 2005-01-26 17:47:02 +0000 | [diff] [blame] | 2586 | if( u<=127 ) return 1; |
| 2587 | if( u<=32767 ) return 2; |
| 2588 | if( u<=8388607 ) return 3; |
| 2589 | if( u<=2147483647 ) return 4; |
| 2590 | if( u<=MAX_6BYTE ) return 5; |
drh | a19b775 | 2004-05-30 21:14:58 +0000 | [diff] [blame] | 2591 | return 6; |
danielk1977 | 90e4d95 | 2004-05-10 10:05:53 +0000 | [diff] [blame] | 2592 | } |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 2593 | if( flags&MEM_Real ){ |
drh | a19b775 | 2004-05-30 21:14:58 +0000 | [diff] [blame] | 2594 | return 7; |
danielk1977 | 90e4d95 | 2004-05-10 10:05:53 +0000 | [diff] [blame] | 2595 | } |
danielk1977 | e435975 | 2008-11-03 09:39:45 +0000 | [diff] [blame] | 2596 | assert( pMem->db->mallocFailed || flags&(MEM_Str|MEM_Blob) ); |
drh | fdf972a | 2007-05-02 13:30:27 +0000 | [diff] [blame] | 2597 | n = pMem->n; |
| 2598 | if( flags & MEM_Zero ){ |
drh | 8df3284 | 2008-12-09 02:51:23 +0000 | [diff] [blame] | 2599 | n += pMem->u.nZero; |
danielk1977 | 90e4d95 | 2004-05-10 10:05:53 +0000 | [diff] [blame] | 2600 | } |
drh | fdf972a | 2007-05-02 13:30:27 +0000 | [diff] [blame] | 2601 | assert( n>=0 ); |
| 2602 | return ((n*2) + 12 + ((flags&MEM_Str)!=0)); |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2603 | } |
| 2604 | |
| 2605 | /* |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 2606 | ** Return the length of the data corresponding to the supplied serial-type. |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2607 | */ |
drh | 35cd643 | 2009-06-05 14:17:21 +0000 | [diff] [blame] | 2608 | u32 sqlite3VdbeSerialTypeLen(u32 serial_type){ |
drh | a19b775 | 2004-05-30 21:14:58 +0000 | [diff] [blame] | 2609 | if( serial_type>=12 ){ |
drh | 51846b5 | 2004-05-28 16:00:21 +0000 | [diff] [blame] | 2610 | return (serial_type-12)/2; |
| 2611 | }else{ |
drh | 5719628 | 2004-10-06 15:41:16 +0000 | [diff] [blame] | 2612 | 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] | 2613 | return aSize[serial_type]; |
| 2614 | } |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2615 | } |
| 2616 | |
| 2617 | /* |
drh | 110daac | 2007-05-04 11:59:31 +0000 | [diff] [blame] | 2618 | ** If we are on an architecture with mixed-endian floating |
drh | 7a4f502 | 2007-05-23 07:20:08 +0000 | [diff] [blame] | 2619 | ** points (ex: ARM7) then swap the lower 4 bytes with the |
drh | 110daac | 2007-05-04 11:59:31 +0000 | [diff] [blame] | 2620 | ** upper 4 bytes. Return the result. |
| 2621 | ** |
drh | 7a4f502 | 2007-05-23 07:20:08 +0000 | [diff] [blame] | 2622 | ** For most architectures, this is a no-op. |
| 2623 | ** |
| 2624 | ** (later): It is reported to me that the mixed-endian problem |
| 2625 | ** on ARM7 is an issue with GCC, not with the ARM7 chip. It seems |
| 2626 | ** that early versions of GCC stored the two words of a 64-bit |
| 2627 | ** float in the wrong order. And that error has been propagated |
| 2628 | ** ever since. The blame is not necessarily with GCC, though. |
| 2629 | ** GCC might have just copying the problem from a prior compiler. |
| 2630 | ** I am also told that newer versions of GCC that follow a different |
| 2631 | ** ABI get the byte order right. |
| 2632 | ** |
| 2633 | ** Developers using SQLite on an ARM7 should compile and run their |
| 2634 | ** application using -DSQLITE_DEBUG=1 at least once. With DEBUG |
| 2635 | ** enabled, some asserts below will ensure that the byte order of |
| 2636 | ** floating point values is correct. |
drh | 60d09a7 | 2007-08-30 15:05:08 +0000 | [diff] [blame] | 2637 | ** |
| 2638 | ** (2007-08-30) Frank van Vugt has studied this problem closely |
| 2639 | ** and has send his findings to the SQLite developers. Frank |
| 2640 | ** writes that some Linux kernels offer floating point hardware |
| 2641 | ** emulation that uses only 32-bit mantissas instead of a full |
| 2642 | ** 48-bits as required by the IEEE standard. (This is the |
| 2643 | ** CONFIG_FPE_FASTFPE option.) On such systems, floating point |
| 2644 | ** byte swapping becomes very complicated. To avoid problems, |
| 2645 | ** the necessary byte swapping is carried out using a 64-bit integer |
| 2646 | ** rather than a 64-bit float. Frank assures us that the code here |
| 2647 | ** works for him. We, the developers, have no way to independently |
| 2648 | ** verify this, but Frank seems to know what he is talking about |
| 2649 | ** so we trust him. |
drh | 110daac | 2007-05-04 11:59:31 +0000 | [diff] [blame] | 2650 | */ |
| 2651 | #ifdef SQLITE_MIXED_ENDIAN_64BIT_FLOAT |
drh | 60d09a7 | 2007-08-30 15:05:08 +0000 | [diff] [blame] | 2652 | static u64 floatSwap(u64 in){ |
drh | 110daac | 2007-05-04 11:59:31 +0000 | [diff] [blame] | 2653 | union { |
drh | 60d09a7 | 2007-08-30 15:05:08 +0000 | [diff] [blame] | 2654 | u64 r; |
drh | 110daac | 2007-05-04 11:59:31 +0000 | [diff] [blame] | 2655 | u32 i[2]; |
| 2656 | } u; |
| 2657 | u32 t; |
| 2658 | |
| 2659 | u.r = in; |
| 2660 | t = u.i[0]; |
| 2661 | u.i[0] = u.i[1]; |
| 2662 | u.i[1] = t; |
| 2663 | return u.r; |
| 2664 | } |
| 2665 | # define swapMixedEndianFloat(X) X = floatSwap(X) |
| 2666 | #else |
| 2667 | # define swapMixedEndianFloat(X) |
| 2668 | #endif |
| 2669 | |
| 2670 | /* |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 2671 | ** Write the serialized data blob for the value stored in pMem into |
| 2672 | ** buf. It is assumed that the caller has allocated sufficient space. |
| 2673 | ** Return the number of bytes written. |
drh | fdf972a | 2007-05-02 13:30:27 +0000 | [diff] [blame] | 2674 | ** |
| 2675 | ** nBuf is the amount of space left in buf[]. nBuf must always be |
| 2676 | ** large enough to hold the entire field. Except, if the field is |
| 2677 | ** a blob with a zero-filled tail, then buf[] might be just the right |
| 2678 | ** size to hold everything except for the zero-filled tail. If buf[] |
| 2679 | ** is only big enough to hold the non-zero prefix, then only write that |
| 2680 | ** prefix into buf[]. But if buf[] is large enough to hold both the |
| 2681 | ** prefix and the tail then write the prefix and set the tail to all |
| 2682 | ** zeros. |
| 2683 | ** |
| 2684 | ** Return the number of bytes actually written into buf[]. The number |
| 2685 | ** of bytes in the zero-filled tail is included in the return value only |
| 2686 | ** if those bytes were zeroed in buf[]. |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 2687 | */ |
drh | 35cd643 | 2009-06-05 14:17:21 +0000 | [diff] [blame] | 2688 | u32 sqlite3VdbeSerialPut(u8 *buf, int nBuf, Mem *pMem, int file_format){ |
drh | d946db0 | 2005-12-29 19:23:06 +0000 | [diff] [blame] | 2689 | u32 serial_type = sqlite3VdbeSerialType(pMem, file_format); |
drh | 35cd643 | 2009-06-05 14:17:21 +0000 | [diff] [blame] | 2690 | u32 len; |
danielk1977 | 183f9f7 | 2004-05-13 05:20:26 +0000 | [diff] [blame] | 2691 | |
drh | 1483e14 | 2004-05-21 21:12:42 +0000 | [diff] [blame] | 2692 | /* Integer and Real */ |
drh | d946db0 | 2005-12-29 19:23:06 +0000 | [diff] [blame] | 2693 | if( serial_type<=7 && serial_type>0 ){ |
drh | 1483e14 | 2004-05-21 21:12:42 +0000 | [diff] [blame] | 2694 | u64 v; |
drh | 35cd643 | 2009-06-05 14:17:21 +0000 | [diff] [blame] | 2695 | u32 i; |
drh | a19b775 | 2004-05-30 21:14:58 +0000 | [diff] [blame] | 2696 | if( serial_type==7 ){ |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 2697 | assert( sizeof(v)==sizeof(pMem->r) ); |
| 2698 | memcpy(&v, &pMem->r, sizeof(v)); |
drh | 60d09a7 | 2007-08-30 15:05:08 +0000 | [diff] [blame] | 2699 | swapMixedEndianFloat(v); |
drh | 1483e14 | 2004-05-21 21:12:42 +0000 | [diff] [blame] | 2700 | }else{ |
drh | 3c024d6 | 2007-03-30 11:23:45 +0000 | [diff] [blame] | 2701 | v = pMem->u.i; |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 2702 | } |
drh | 1483e14 | 2004-05-21 21:12:42 +0000 | [diff] [blame] | 2703 | len = i = sqlite3VdbeSerialTypeLen(serial_type); |
shane | 75ac1de | 2009-06-09 18:58:52 +0000 | [diff] [blame] | 2704 | assert( len<=(u32)nBuf ); |
drh | 1483e14 | 2004-05-21 21:12:42 +0000 | [diff] [blame] | 2705 | while( i-- ){ |
drh | 8df3284 | 2008-12-09 02:51:23 +0000 | [diff] [blame] | 2706 | buf[i] = (u8)(v&0xFF); |
drh | 1483e14 | 2004-05-21 21:12:42 +0000 | [diff] [blame] | 2707 | v >>= 8; |
| 2708 | } |
| 2709 | return len; |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 2710 | } |
drh | d946db0 | 2005-12-29 19:23:06 +0000 | [diff] [blame] | 2711 | |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 2712 | /* String or blob */ |
drh | d946db0 | 2005-12-29 19:23:06 +0000 | [diff] [blame] | 2713 | if( serial_type>=12 ){ |
drh | 8df3284 | 2008-12-09 02:51:23 +0000 | [diff] [blame] | 2714 | assert( pMem->n + ((pMem->flags & MEM_Zero)?pMem->u.nZero:0) |
shane | 75ac1de | 2009-06-09 18:58:52 +0000 | [diff] [blame] | 2715 | == (int)sqlite3VdbeSerialTypeLen(serial_type) ); |
drh | fdf972a | 2007-05-02 13:30:27 +0000 | [diff] [blame] | 2716 | assert( pMem->n<=nBuf ); |
| 2717 | len = pMem->n; |
drh | d946db0 | 2005-12-29 19:23:06 +0000 | [diff] [blame] | 2718 | memcpy(buf, pMem->z, len); |
drh | fdf972a | 2007-05-02 13:30:27 +0000 | [diff] [blame] | 2719 | if( pMem->flags & MEM_Zero ){ |
drh | 8df3284 | 2008-12-09 02:51:23 +0000 | [diff] [blame] | 2720 | len += pMem->u.nZero; |
drh | 35cd643 | 2009-06-05 14:17:21 +0000 | [diff] [blame] | 2721 | assert( nBuf>=0 ); |
| 2722 | if( len > (u32)nBuf ){ |
| 2723 | len = (u32)nBuf; |
drh | fdf972a | 2007-05-02 13:30:27 +0000 | [diff] [blame] | 2724 | } |
| 2725 | memset(&buf[pMem->n], 0, len-pMem->n); |
| 2726 | } |
drh | d946db0 | 2005-12-29 19:23:06 +0000 | [diff] [blame] | 2727 | return len; |
| 2728 | } |
| 2729 | |
| 2730 | /* NULL or constants 0 or 1 */ |
| 2731 | return 0; |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 2732 | } |
| 2733 | |
| 2734 | /* |
| 2735 | ** Deserialize the data blob pointed to by buf as serial type serial_type |
| 2736 | ** and store the result in pMem. Return the number of bytes read. |
| 2737 | */ |
drh | 35cd643 | 2009-06-05 14:17:21 +0000 | [diff] [blame] | 2738 | u32 sqlite3VdbeSerialGet( |
danielk1977 | 93d4675 | 2004-05-23 13:30:58 +0000 | [diff] [blame] | 2739 | const unsigned char *buf, /* Buffer to deserialize from */ |
drh | 25aa1b4 | 2004-05-28 01:39:01 +0000 | [diff] [blame] | 2740 | u32 serial_type, /* Serial type to deserialize */ |
| 2741 | Mem *pMem /* Memory cell to write value into */ |
danielk1977 | b1bc953 | 2004-05-22 03:05:33 +0000 | [diff] [blame] | 2742 | ){ |
drh | 3c68582 | 2005-05-21 18:32:18 +0000 | [diff] [blame] | 2743 | switch( serial_type ){ |
drh | 3c68582 | 2005-05-21 18:32:18 +0000 | [diff] [blame] | 2744 | case 10: /* Reserved for future use */ |
| 2745 | case 11: /* Reserved for future use */ |
| 2746 | case 0: { /* NULL */ |
| 2747 | pMem->flags = MEM_Null; |
| 2748 | break; |
| 2749 | } |
| 2750 | case 1: { /* 1-byte signed integer */ |
drh | 3c024d6 | 2007-03-30 11:23:45 +0000 | [diff] [blame] | 2751 | pMem->u.i = (signed char)buf[0]; |
drh | 1483e14 | 2004-05-21 21:12:42 +0000 | [diff] [blame] | 2752 | pMem->flags = MEM_Int; |
drh | 3c68582 | 2005-05-21 18:32:18 +0000 | [diff] [blame] | 2753 | return 1; |
drh | 1483e14 | 2004-05-21 21:12:42 +0000 | [diff] [blame] | 2754 | } |
drh | 3c68582 | 2005-05-21 18:32:18 +0000 | [diff] [blame] | 2755 | case 2: { /* 2-byte signed integer */ |
drh | 3c024d6 | 2007-03-30 11:23:45 +0000 | [diff] [blame] | 2756 | pMem->u.i = (((signed char)buf[0])<<8) | buf[1]; |
drh | 3c68582 | 2005-05-21 18:32:18 +0000 | [diff] [blame] | 2757 | pMem->flags = MEM_Int; |
| 2758 | return 2; |
| 2759 | } |
| 2760 | case 3: { /* 3-byte signed integer */ |
drh | 3c024d6 | 2007-03-30 11:23:45 +0000 | [diff] [blame] | 2761 | pMem->u.i = (((signed char)buf[0])<<16) | (buf[1]<<8) | buf[2]; |
drh | 3c68582 | 2005-05-21 18:32:18 +0000 | [diff] [blame] | 2762 | pMem->flags = MEM_Int; |
| 2763 | return 3; |
| 2764 | } |
| 2765 | case 4: { /* 4-byte signed integer */ |
drh | 3c024d6 | 2007-03-30 11:23:45 +0000 | [diff] [blame] | 2766 | 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] | 2767 | pMem->flags = MEM_Int; |
| 2768 | return 4; |
| 2769 | } |
| 2770 | case 5: { /* 6-byte signed integer */ |
| 2771 | u64 x = (((signed char)buf[0])<<8) | buf[1]; |
| 2772 | u32 y = (buf[2]<<24) | (buf[3]<<16) | (buf[4]<<8) | buf[5]; |
| 2773 | x = (x<<32) | y; |
drh | 3c024d6 | 2007-03-30 11:23:45 +0000 | [diff] [blame] | 2774 | pMem->u.i = *(i64*)&x; |
drh | 3c68582 | 2005-05-21 18:32:18 +0000 | [diff] [blame] | 2775 | pMem->flags = MEM_Int; |
| 2776 | return 6; |
| 2777 | } |
drh | 91124b3 | 2005-08-18 18:15:05 +0000 | [diff] [blame] | 2778 | case 6: /* 8-byte signed integer */ |
drh | 3c68582 | 2005-05-21 18:32:18 +0000 | [diff] [blame] | 2779 | case 7: { /* IEEE floating point */ |
drh | d81bd4e | 2005-09-05 20:06:49 +0000 | [diff] [blame] | 2780 | u64 x; |
| 2781 | u32 y; |
drh | 2a3e4a7 | 2006-01-23 21:44:53 +0000 | [diff] [blame] | 2782 | #if !defined(NDEBUG) && !defined(SQLITE_OMIT_FLOATING_POINT) |
drh | de941c6 | 2005-08-28 01:34:21 +0000 | [diff] [blame] | 2783 | /* Verify that integers and floating point values use the same |
drh | 110daac | 2007-05-04 11:59:31 +0000 | [diff] [blame] | 2784 | ** byte order. Or, that if SQLITE_MIXED_ENDIAN_64BIT_FLOAT is |
| 2785 | ** defined that 64-bit floating point values really are mixed |
| 2786 | ** endian. |
drh | bfd6b03 | 2005-08-28 01:38:44 +0000 | [diff] [blame] | 2787 | */ |
drh | de941c6 | 2005-08-28 01:34:21 +0000 | [diff] [blame] | 2788 | static const u64 t1 = ((u64)0x3ff00000)<<32; |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 2789 | static const double r1 = 1.0; |
drh | 60d09a7 | 2007-08-30 15:05:08 +0000 | [diff] [blame] | 2790 | u64 t2 = t1; |
| 2791 | swapMixedEndianFloat(t2); |
| 2792 | assert( sizeof(r1)==sizeof(t2) && memcmp(&r1, &t2, sizeof(r1))==0 ); |
drh | de941c6 | 2005-08-28 01:34:21 +0000 | [diff] [blame] | 2793 | #endif |
drh | bfd6b03 | 2005-08-28 01:38:44 +0000 | [diff] [blame] | 2794 | |
drh | d81bd4e | 2005-09-05 20:06:49 +0000 | [diff] [blame] | 2795 | x = (buf[0]<<24) | (buf[1]<<16) | (buf[2]<<8) | buf[3]; |
| 2796 | y = (buf[4]<<24) | (buf[5]<<16) | (buf[6]<<8) | buf[7]; |
drh | 3c68582 | 2005-05-21 18:32:18 +0000 | [diff] [blame] | 2797 | x = (x<<32) | y; |
| 2798 | if( serial_type==6 ){ |
drh | 3c024d6 | 2007-03-30 11:23:45 +0000 | [diff] [blame] | 2799 | pMem->u.i = *(i64*)&x; |
drh | 3c68582 | 2005-05-21 18:32:18 +0000 | [diff] [blame] | 2800 | pMem->flags = MEM_Int; |
| 2801 | }else{ |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 2802 | assert( sizeof(x)==8 && sizeof(pMem->r)==8 ); |
drh | 60d09a7 | 2007-08-30 15:05:08 +0000 | [diff] [blame] | 2803 | swapMixedEndianFloat(x); |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 2804 | memcpy(&pMem->r, &x, sizeof(x)); |
drh | 2eaf93d | 2008-04-29 00:15:20 +0000 | [diff] [blame] | 2805 | pMem->flags = sqlite3IsNaN(pMem->r) ? MEM_Null : MEM_Real; |
drh | 3c68582 | 2005-05-21 18:32:18 +0000 | [diff] [blame] | 2806 | } |
| 2807 | return 8; |
| 2808 | } |
drh | d946db0 | 2005-12-29 19:23:06 +0000 | [diff] [blame] | 2809 | case 8: /* Integer 0 */ |
| 2810 | case 9: { /* Integer 1 */ |
drh | 3c024d6 | 2007-03-30 11:23:45 +0000 | [diff] [blame] | 2811 | pMem->u.i = serial_type-8; |
drh | d946db0 | 2005-12-29 19:23:06 +0000 | [diff] [blame] | 2812 | pMem->flags = MEM_Int; |
| 2813 | return 0; |
| 2814 | } |
drh | 3c68582 | 2005-05-21 18:32:18 +0000 | [diff] [blame] | 2815 | default: { |
drh | 35cd643 | 2009-06-05 14:17:21 +0000 | [diff] [blame] | 2816 | u32 len = (serial_type-12)/2; |
drh | 3c68582 | 2005-05-21 18:32:18 +0000 | [diff] [blame] | 2817 | pMem->z = (char *)buf; |
| 2818 | pMem->n = len; |
| 2819 | pMem->xDel = 0; |
| 2820 | if( serial_type&0x01 ){ |
| 2821 | pMem->flags = MEM_Str | MEM_Ephem; |
| 2822 | }else{ |
| 2823 | pMem->flags = MEM_Blob | MEM_Ephem; |
| 2824 | } |
| 2825 | return len; |
drh | 696b32f | 2004-05-30 01:51:52 +0000 | [diff] [blame] | 2826 | } |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 2827 | } |
drh | 3c68582 | 2005-05-21 18:32:18 +0000 | [diff] [blame] | 2828 | return 0; |
danielk1977 | 192ac1d | 2004-05-10 07:17:30 +0000 | [diff] [blame] | 2829 | } |
| 2830 | |
drh | 1e968a0 | 2008-03-25 00:22:21 +0000 | [diff] [blame] | 2831 | /* |
dan | 03e9cfc | 2011-09-05 14:20:27 +0000 | [diff] [blame] | 2832 | ** This routine is used to allocate sufficient space for an UnpackedRecord |
| 2833 | ** structure large enough to be used with sqlite3VdbeRecordUnpack() if |
| 2834 | ** the first argument is a pointer to KeyInfo structure pKeyInfo. |
drh | 1e968a0 | 2008-03-25 00:22:21 +0000 | [diff] [blame] | 2835 | ** |
dan | 03e9cfc | 2011-09-05 14:20:27 +0000 | [diff] [blame] | 2836 | ** The space is either allocated using sqlite3DbMallocRaw() or from within |
| 2837 | ** the unaligned buffer passed via the second and third arguments (presumably |
| 2838 | ** stack space). If the former, then *ppFree is set to a pointer that should |
| 2839 | ** be eventually freed by the caller using sqlite3DbFree(). Or, if the |
| 2840 | ** allocation comes from the pSpace/szSpace buffer, *ppFree is set to NULL |
| 2841 | ** before returning. |
drh | 1e968a0 | 2008-03-25 00:22:21 +0000 | [diff] [blame] | 2842 | ** |
dan | 03e9cfc | 2011-09-05 14:20:27 +0000 | [diff] [blame] | 2843 | ** If an OOM error occurs, NULL is returned. |
| 2844 | */ |
| 2845 | UnpackedRecord *sqlite3VdbeAllocUnpackedRecord( |
| 2846 | KeyInfo *pKeyInfo, /* Description of the record */ |
| 2847 | char *pSpace, /* Unaligned space available */ |
| 2848 | int szSpace, /* Size of pSpace[] in bytes */ |
| 2849 | char **ppFree /* OUT: Caller should free this pointer */ |
drh | 1e968a0 | 2008-03-25 00:22:21 +0000 | [diff] [blame] | 2850 | ){ |
dan | 03e9cfc | 2011-09-05 14:20:27 +0000 | [diff] [blame] | 2851 | UnpackedRecord *p; /* Unpacked record to return */ |
| 2852 | int nOff; /* Increment pSpace by nOff to align it */ |
| 2853 | int nByte; /* Number of bytes required for *p */ |
| 2854 | |
| 2855 | /* We want to shift the pointer pSpace up such that it is 8-byte aligned. |
shane | 80167bf | 2009-04-10 15:42:36 +0000 | [diff] [blame] | 2856 | ** Thus, we need to calculate a value, nOff, between 0 and 7, to shift |
| 2857 | ** it by. If pSpace is already 8-byte aligned, nOff should be zero. |
| 2858 | */ |
| 2859 | nOff = (8 - (SQLITE_PTR_TO_INT(pSpace) & 7)) & 7; |
drh | 8c5d152 | 2009-04-10 00:56:28 +0000 | [diff] [blame] | 2860 | nByte = ROUND8(sizeof(UnpackedRecord)) + sizeof(Mem)*(pKeyInfo->nField+1); |
dan | 42acb3e | 2011-09-05 20:16:38 +0000 | [diff] [blame] | 2861 | if( nByte>szSpace+nOff ){ |
dan | 03e9cfc | 2011-09-05 14:20:27 +0000 | [diff] [blame] | 2862 | p = (UnpackedRecord *)sqlite3DbMallocRaw(pKeyInfo->db, nByte); |
| 2863 | *ppFree = (char *)p; |
dan | 42acb3e | 2011-09-05 20:16:38 +0000 | [diff] [blame] | 2864 | if( !p ) return 0; |
drh | 1e968a0 | 2008-03-25 00:22:21 +0000 | [diff] [blame] | 2865 | }else{ |
dan | 42acb3e | 2011-09-05 20:16:38 +0000 | [diff] [blame] | 2866 | p = (UnpackedRecord*)&pSpace[nOff]; |
dan | 03e9cfc | 2011-09-05 14:20:27 +0000 | [diff] [blame] | 2867 | *ppFree = 0; |
drh | 1e968a0 | 2008-03-25 00:22:21 +0000 | [diff] [blame] | 2868 | } |
dan | 42acb3e | 2011-09-05 20:16:38 +0000 | [diff] [blame] | 2869 | |
| 2870 | p->aMem = (Mem*)&((char*)p)[ROUND8(sizeof(UnpackedRecord))]; |
drh | 1e968a0 | 2008-03-25 00:22:21 +0000 | [diff] [blame] | 2871 | p->pKeyInfo = pKeyInfo; |
| 2872 | p->nField = pKeyInfo->nField + 1; |
dan | 03e9cfc | 2011-09-05 14:20:27 +0000 | [diff] [blame] | 2873 | return p; |
| 2874 | } |
| 2875 | |
| 2876 | /* |
| 2877 | ** Given the nKey-byte encoding of a record in pKey[], populate the |
| 2878 | ** UnpackedRecord structure indicated by the fourth argument with the |
| 2879 | ** contents of the decoded record. |
| 2880 | */ |
| 2881 | void sqlite3VdbeRecordUnpack( |
| 2882 | KeyInfo *pKeyInfo, /* Information about the record format */ |
| 2883 | int nKey, /* Size of the binary record */ |
| 2884 | const void *pKey, /* The binary record */ |
| 2885 | UnpackedRecord *p /* Populate this structure before returning. */ |
| 2886 | ){ |
| 2887 | const unsigned char *aKey = (const unsigned char *)pKey; |
| 2888 | int d; |
| 2889 | u32 idx; /* Offset in aKey[] to read from */ |
| 2890 | u16 u; /* Unsigned loop counter */ |
| 2891 | u32 szHdr; |
dan | 42acb3e | 2011-09-05 20:16:38 +0000 | [diff] [blame] | 2892 | Mem *pMem = p->aMem; |
dan | 03e9cfc | 2011-09-05 14:20:27 +0000 | [diff] [blame] | 2893 | |
| 2894 | p->flags = 0; |
drh | 8c5d152 | 2009-04-10 00:56:28 +0000 | [diff] [blame] | 2895 | assert( EIGHT_BYTE_ALIGNMENT(pMem) ); |
shane | 3f8d5cf | 2008-04-24 19:15:09 +0000 | [diff] [blame] | 2896 | idx = getVarint32(aKey, szHdr); |
drh | 1e968a0 | 2008-03-25 00:22:21 +0000 | [diff] [blame] | 2897 | d = szHdr; |
shane | 0b8d276 | 2008-07-22 05:18:00 +0000 | [diff] [blame] | 2898 | u = 0; |
drh | 2fa34d3 | 2009-07-15 16:30:50 +0000 | [diff] [blame] | 2899 | while( idx<szHdr && u<p->nField && d<=nKey ){ |
drh | 1e968a0 | 2008-03-25 00:22:21 +0000 | [diff] [blame] | 2900 | u32 serial_type; |
| 2901 | |
danielk1977 | 00e1361 | 2008-11-17 19:18:54 +0000 | [diff] [blame] | 2902 | idx += getVarint32(&aKey[idx], serial_type); |
drh | 1e968a0 | 2008-03-25 00:22:21 +0000 | [diff] [blame] | 2903 | pMem->enc = pKeyInfo->enc; |
| 2904 | pMem->db = pKeyInfo->db; |
drh | c3f1d5f | 2011-05-30 23:42:16 +0000 | [diff] [blame] | 2905 | /* pMem->flags = 0; // sqlite3VdbeSerialGet() will set this for us */ |
danielk1977 | 5f09613 | 2008-03-28 15:44:09 +0000 | [diff] [blame] | 2906 | pMem->zMalloc = 0; |
drh | 1e968a0 | 2008-03-25 00:22:21 +0000 | [diff] [blame] | 2907 | d += sqlite3VdbeSerialGet(&aKey[d], serial_type, pMem); |
drh | e14006d | 2008-03-25 17:23:32 +0000 | [diff] [blame] | 2908 | pMem++; |
shane | 0b8d276 | 2008-07-22 05:18:00 +0000 | [diff] [blame] | 2909 | u++; |
drh | 1e968a0 | 2008-03-25 00:22:21 +0000 | [diff] [blame] | 2910 | } |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 2911 | assert( u<=pKeyInfo->nField + 1 ); |
shane | 0b8d276 | 2008-07-22 05:18:00 +0000 | [diff] [blame] | 2912 | p->nField = u; |
drh | 1e968a0 | 2008-03-25 00:22:21 +0000 | [diff] [blame] | 2913 | } |
| 2914 | |
| 2915 | /* |
| 2916 | ** This function compares the two table rows or index records |
| 2917 | ** specified by {nKey1, pKey1} and pPKey2. It returns a negative, zero |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 2918 | ** or positive integer if key1 is less than, equal to or |
| 2919 | ** greater than key2. The {nKey1, pKey1} key must be a blob |
drh | 1e968a0 | 2008-03-25 00:22:21 +0000 | [diff] [blame] | 2920 | ** created by th OP_MakeRecord opcode of the VDBE. The pPKey2 |
| 2921 | ** key must be a parsed key such as obtained from |
| 2922 | ** sqlite3VdbeParseRecord. |
| 2923 | ** |
| 2924 | ** Key1 and Key2 do not have to contain the same number of fields. |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 2925 | ** The key with fewer fields is usually compares less than the |
| 2926 | ** longer key. However if the UNPACKED_INCRKEY flags in pPKey2 is set |
| 2927 | ** and the common prefixes are equal, then key1 is less than key2. |
| 2928 | ** Or if the UNPACKED_MATCH_PREFIX flag is set and the prefixes are |
| 2929 | ** equal, then the keys are considered to be equal and |
drh | ec1fc80 | 2008-08-13 14:07:40 +0000 | [diff] [blame] | 2930 | ** the parts beyond the common prefix are ignored. |
| 2931 | ** |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 2932 | ** If the UNPACKED_IGNORE_ROWID flag is set, then the last byte of |
| 2933 | ** the header of pKey1 is ignored. It is assumed that pKey1 is |
| 2934 | ** an index key, and thus ends with a rowid value. The last byte |
| 2935 | ** of the header will therefore be the serial type of the rowid: |
| 2936 | ** one of 1, 2, 3, 4, 5, 6, 8, or 9 - the integer serial types. |
| 2937 | ** The serial type of the final rowid will always be a single byte. |
| 2938 | ** By ignoring this last byte of the header, we force the comparison |
| 2939 | ** to ignore the rowid at the end of key1. |
drh | 1e968a0 | 2008-03-25 00:22:21 +0000 | [diff] [blame] | 2940 | */ |
drh | e14006d | 2008-03-25 17:23:32 +0000 | [diff] [blame] | 2941 | int sqlite3VdbeRecordCompare( |
drh | ec1fc80 | 2008-08-13 14:07:40 +0000 | [diff] [blame] | 2942 | int nKey1, const void *pKey1, /* Left key */ |
drh | ec1fc80 | 2008-08-13 14:07:40 +0000 | [diff] [blame] | 2943 | UnpackedRecord *pPKey2 /* Right key */ |
drh | 1e968a0 | 2008-03-25 00:22:21 +0000 | [diff] [blame] | 2944 | ){ |
danielk1977 | 00e1361 | 2008-11-17 19:18:54 +0000 | [diff] [blame] | 2945 | int d1; /* Offset into aKey[] of next data element */ |
drh | 1e968a0 | 2008-03-25 00:22:21 +0000 | [diff] [blame] | 2946 | u32 idx1; /* Offset into aKey[] of next header element */ |
| 2947 | u32 szHdr1; /* Number of bytes in header */ |
| 2948 | int i = 0; |
| 2949 | int nField; |
| 2950 | int rc = 0; |
| 2951 | const unsigned char *aKey1 = (const unsigned char *)pKey1; |
| 2952 | KeyInfo *pKeyInfo; |
| 2953 | Mem mem1; |
| 2954 | |
| 2955 | pKeyInfo = pPKey2->pKeyInfo; |
| 2956 | mem1.enc = pKeyInfo->enc; |
drh | 3727263 | 2009-11-16 21:28:45 +0000 | [diff] [blame] | 2957 | mem1.db = pKeyInfo->db; |
drh | d93a8b2 | 2009-11-16 03:13:40 +0000 | [diff] [blame] | 2958 | /* mem1.flags = 0; // Will be initialized by sqlite3VdbeSerialGet() */ |
| 2959 | VVA_ONLY( mem1.zMalloc = 0; ) /* Only needed by assert() statements */ |
drh | 8b249a8 | 2009-11-16 02:14:00 +0000 | [diff] [blame] | 2960 | |
| 2961 | /* Compilers may complain that mem1.u.i is potentially uninitialized. |
| 2962 | ** We could initialize it, as shown here, to silence those complaints. |
drh | 5275d2e | 2011-04-27 01:00:17 +0000 | [diff] [blame] | 2963 | ** But in fact, mem1.u.i will never actually be used uninitialized, and doing |
drh | 8b249a8 | 2009-11-16 02:14:00 +0000 | [diff] [blame] | 2964 | ** the unnecessary initialization has a measurable negative performance |
| 2965 | ** impact, since this routine is a very high runner. And so, we choose |
| 2966 | ** to ignore the compiler warnings and leave this variable uninitialized. |
| 2967 | */ |
| 2968 | /* mem1.u.i = 0; // not needed, here to silence compiler warning */ |
drh | 1e968a0 | 2008-03-25 00:22:21 +0000 | [diff] [blame] | 2969 | |
shane | 3f8d5cf | 2008-04-24 19:15:09 +0000 | [diff] [blame] | 2970 | idx1 = getVarint32(aKey1, szHdr1); |
drh | 1e968a0 | 2008-03-25 00:22:21 +0000 | [diff] [blame] | 2971 | d1 = szHdr1; |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 2972 | if( pPKey2->flags & UNPACKED_IGNORE_ROWID ){ |
| 2973 | szHdr1--; |
| 2974 | } |
drh | 1e968a0 | 2008-03-25 00:22:21 +0000 | [diff] [blame] | 2975 | nField = pKeyInfo->nField; |
| 2976 | while( idx1<szHdr1 && i<pPKey2->nField ){ |
| 2977 | u32 serial_type1; |
| 2978 | |
| 2979 | /* Read the serial types for the next element in each key. */ |
shane | 3f8d5cf | 2008-04-24 19:15:09 +0000 | [diff] [blame] | 2980 | idx1 += getVarint32( aKey1+idx1, serial_type1 ); |
drh | 1e968a0 | 2008-03-25 00:22:21 +0000 | [diff] [blame] | 2981 | if( d1>=nKey1 && sqlite3VdbeSerialTypeLen(serial_type1)>0 ) break; |
| 2982 | |
| 2983 | /* Extract the values to be compared. |
| 2984 | */ |
| 2985 | d1 += sqlite3VdbeSerialGet(&aKey1[d1], serial_type1, &mem1); |
| 2986 | |
| 2987 | /* Do the comparison |
| 2988 | */ |
drh | e14006d | 2008-03-25 17:23:32 +0000 | [diff] [blame] | 2989 | rc = sqlite3MemCompare(&mem1, &pPKey2->aMem[i], |
drh | 1e968a0 | 2008-03-25 00:22:21 +0000 | [diff] [blame] | 2990 | i<nField ? pKeyInfo->aColl[i] : 0); |
drh | 1e968a0 | 2008-03-25 00:22:21 +0000 | [diff] [blame] | 2991 | if( rc!=0 ){ |
drh | 8b249a8 | 2009-11-16 02:14:00 +0000 | [diff] [blame] | 2992 | assert( mem1.zMalloc==0 ); /* See comment below */ |
| 2993 | |
| 2994 | /* Invert the result if we are using DESC sort order. */ |
| 2995 | if( pKeyInfo->aSortOrder && i<nField && pKeyInfo->aSortOrder[i] ){ |
| 2996 | rc = -rc; |
| 2997 | } |
| 2998 | |
| 2999 | /* If the PREFIX_SEARCH flag is set and all fields except the final |
| 3000 | ** rowid field were equal, then clear the PREFIX_SEARCH flag and set |
| 3001 | ** pPKey2->rowid to the value of the rowid field in (pKey1, nKey1). |
| 3002 | ** This is used by the OP_IsUnique opcode. |
| 3003 | */ |
| 3004 | if( (pPKey2->flags & UNPACKED_PREFIX_SEARCH) && i==(pPKey2->nField-1) ){ |
| 3005 | assert( idx1==szHdr1 && rc ); |
| 3006 | assert( mem1.flags & MEM_Int ); |
| 3007 | pPKey2->flags &= ~UNPACKED_PREFIX_SEARCH; |
| 3008 | pPKey2->rowid = mem1.u.i; |
| 3009 | } |
| 3010 | |
| 3011 | return rc; |
drh | 1e968a0 | 2008-03-25 00:22:21 +0000 | [diff] [blame] | 3012 | } |
| 3013 | i++; |
| 3014 | } |
drh | 407414c | 2009-07-14 14:15:27 +0000 | [diff] [blame] | 3015 | |
drh | 8b249a8 | 2009-11-16 02:14:00 +0000 | [diff] [blame] | 3016 | /* No memory allocation is ever used on mem1. Prove this using |
| 3017 | ** the following assert(). If the assert() fails, it indicates a |
| 3018 | ** memory leak and a need to call sqlite3VdbeMemRelease(&mem1). |
danielk1977 | de63035 | 2009-05-04 11:42:29 +0000 | [diff] [blame] | 3019 | */ |
drh | 8b249a8 | 2009-11-16 02:14:00 +0000 | [diff] [blame] | 3020 | assert( mem1.zMalloc==0 ); |
danielk1977 | de63035 | 2009-05-04 11:42:29 +0000 | [diff] [blame] | 3021 | |
drh | 8b249a8 | 2009-11-16 02:14:00 +0000 | [diff] [blame] | 3022 | /* rc==0 here means that one of the keys ran out of fields and |
| 3023 | ** all the fields up to that point were equal. If the UNPACKED_INCRKEY |
| 3024 | ** flag is set, then break the tie by treating key2 as larger. |
| 3025 | ** If the UPACKED_PREFIX_MATCH flag is set, then keys with common prefixes |
| 3026 | ** are considered to be equal. Otherwise, the longer key is the |
| 3027 | ** larger. As it happens, the pPKey2 will always be the longer |
| 3028 | ** if there is a difference. |
| 3029 | */ |
| 3030 | assert( rc==0 ); |
| 3031 | if( pPKey2->flags & UNPACKED_INCRKEY ){ |
| 3032 | rc = -1; |
| 3033 | }else if( pPKey2->flags & UNPACKED_PREFIX_MATCH ){ |
| 3034 | /* Leave rc==0 */ |
| 3035 | }else if( idx1<szHdr1 ){ |
| 3036 | rc = 1; |
drh | 1e968a0 | 2008-03-25 00:22:21 +0000 | [diff] [blame] | 3037 | } |
drh | 1e968a0 | 2008-03-25 00:22:21 +0000 | [diff] [blame] | 3038 | return rc; |
| 3039 | } |
drh | ec1fc80 | 2008-08-13 14:07:40 +0000 | [diff] [blame] | 3040 | |
danielk1977 | eb015e0 | 2004-05-18 01:31:14 +0000 | [diff] [blame] | 3041 | |
| 3042 | /* |
drh | 7a224de | 2004-06-02 01:22:02 +0000 | [diff] [blame] | 3043 | ** pCur points at an index entry created using the OP_MakeRecord opcode. |
| 3044 | ** Read the rowid (the last field in the record) and store it in *rowid. |
| 3045 | ** Return SQLITE_OK if everything works, or an error code otherwise. |
drh | 88a003e | 2008-12-11 16:17:03 +0000 | [diff] [blame] | 3046 | ** |
| 3047 | ** pCur might be pointing to text obtained from a corrupt database file. |
| 3048 | ** So the content cannot be trusted. Do appropriate checks on the content. |
danielk1977 | 183f9f7 | 2004-05-13 05:20:26 +0000 | [diff] [blame] | 3049 | */ |
drh | 35f6b93 | 2009-06-23 14:15:04 +0000 | [diff] [blame] | 3050 | int sqlite3VdbeIdxRowid(sqlite3 *db, BtCursor *pCur, i64 *rowid){ |
drh | 61fc595 | 2007-04-01 23:49:51 +0000 | [diff] [blame] | 3051 | i64 nCellKey = 0; |
danielk1977 | 183f9f7 | 2004-05-13 05:20:26 +0000 | [diff] [blame] | 3052 | int rc; |
drh | d578820 | 2004-05-28 08:21:05 +0000 | [diff] [blame] | 3053 | u32 szHdr; /* Size of the header */ |
| 3054 | u32 typeRowid; /* Serial type of the rowid */ |
| 3055 | u32 lenRowid; /* Size of the rowid */ |
| 3056 | Mem m, v; |
danielk1977 | 183f9f7 | 2004-05-13 05:20:26 +0000 | [diff] [blame] | 3057 | |
shane | cea72b2 | 2009-09-07 04:38:36 +0000 | [diff] [blame] | 3058 | UNUSED_PARAMETER(db); |
| 3059 | |
drh | 88a003e | 2008-12-11 16:17:03 +0000 | [diff] [blame] | 3060 | /* Get the size of the index entry. Only indices entries of less |
drh | 7b74603 | 2009-06-26 12:15:22 +0000 | [diff] [blame] | 3061 | ** than 2GiB are support - anything large must be database corruption. |
| 3062 | ** Any corruption is detected in sqlite3BtreeParseCellPtr(), though, so |
drh | c27ae61 | 2009-07-14 18:35:44 +0000 | [diff] [blame] | 3063 | ** this code can safely assume that nCellKey is 32-bits |
| 3064 | */ |
drh | ea8ffdf | 2009-07-22 00:35:23 +0000 | [diff] [blame] | 3065 | assert( sqlite3BtreeCursorIsValid(pCur) ); |
drh | b07028f | 2011-10-14 21:49:18 +0000 | [diff] [blame^] | 3066 | VVA_ONLY(rc =) sqlite3BtreeKeySize(pCur, &nCellKey); |
drh | c27ae61 | 2009-07-14 18:35:44 +0000 | [diff] [blame] | 3067 | assert( rc==SQLITE_OK ); /* pCur is always valid so KeySize cannot fail */ |
drh | 7b74603 | 2009-06-26 12:15:22 +0000 | [diff] [blame] | 3068 | assert( (nCellKey & SQLITE_MAX_U32)==(u64)nCellKey ); |
drh | 88a003e | 2008-12-11 16:17:03 +0000 | [diff] [blame] | 3069 | |
| 3070 | /* Read in the complete content of the index entry */ |
drh | ff104c1 | 2009-08-25 13:10:27 +0000 | [diff] [blame] | 3071 | memset(&m, 0, sizeof(m)); |
drh | 8df3284 | 2008-12-09 02:51:23 +0000 | [diff] [blame] | 3072 | rc = sqlite3VdbeMemFromBtree(pCur, 0, (int)nCellKey, 1, &m); |
drh | d578820 | 2004-05-28 08:21:05 +0000 | [diff] [blame] | 3073 | if( rc ){ |
danielk1977 | 183f9f7 | 2004-05-13 05:20:26 +0000 | [diff] [blame] | 3074 | return rc; |
| 3075 | } |
drh | 88a003e | 2008-12-11 16:17:03 +0000 | [diff] [blame] | 3076 | |
| 3077 | /* The index entry must begin with a header size */ |
shane | 3f8d5cf | 2008-04-24 19:15:09 +0000 | [diff] [blame] | 3078 | (void)getVarint32((u8*)m.z, szHdr); |
drh | 7b74603 | 2009-06-26 12:15:22 +0000 | [diff] [blame] | 3079 | testcase( szHdr==3 ); |
drh | 88a003e | 2008-12-11 16:17:03 +0000 | [diff] [blame] | 3080 | testcase( szHdr==m.n ); |
drh | 7b74603 | 2009-06-26 12:15:22 +0000 | [diff] [blame] | 3081 | if( unlikely(szHdr<3 || (int)szHdr>m.n) ){ |
drh | 88a003e | 2008-12-11 16:17:03 +0000 | [diff] [blame] | 3082 | goto idx_rowid_corruption; |
| 3083 | } |
| 3084 | |
| 3085 | /* The last field of the index should be an integer - the ROWID. |
| 3086 | ** Verify that the last entry really is an integer. */ |
shane | 3f8d5cf | 2008-04-24 19:15:09 +0000 | [diff] [blame] | 3087 | (void)getVarint32((u8*)&m.z[szHdr-1], typeRowid); |
drh | 88a003e | 2008-12-11 16:17:03 +0000 | [diff] [blame] | 3088 | testcase( typeRowid==1 ); |
| 3089 | testcase( typeRowid==2 ); |
| 3090 | testcase( typeRowid==3 ); |
| 3091 | testcase( typeRowid==4 ); |
| 3092 | testcase( typeRowid==5 ); |
| 3093 | testcase( typeRowid==6 ); |
| 3094 | testcase( typeRowid==8 ); |
| 3095 | testcase( typeRowid==9 ); |
| 3096 | if( unlikely(typeRowid<1 || typeRowid>9 || typeRowid==7) ){ |
| 3097 | goto idx_rowid_corruption; |
| 3098 | } |
drh | d578820 | 2004-05-28 08:21:05 +0000 | [diff] [blame] | 3099 | lenRowid = sqlite3VdbeSerialTypeLen(typeRowid); |
drh | eeb844a | 2009-08-08 18:01:07 +0000 | [diff] [blame] | 3100 | testcase( (u32)m.n==szHdr+lenRowid ); |
| 3101 | if( unlikely((u32)m.n<szHdr+lenRowid) ){ |
drh | 88a003e | 2008-12-11 16:17:03 +0000 | [diff] [blame] | 3102 | goto idx_rowid_corruption; |
| 3103 | } |
| 3104 | |
| 3105 | /* Fetch the integer off the end of the index record */ |
drh | 2646da7 | 2005-12-09 20:02:05 +0000 | [diff] [blame] | 3106 | sqlite3VdbeSerialGet((u8*)&m.z[m.n-lenRowid], typeRowid, &v); |
drh | 3c024d6 | 2007-03-30 11:23:45 +0000 | [diff] [blame] | 3107 | *rowid = v.u.i; |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 3108 | sqlite3VdbeMemRelease(&m); |
danielk1977 | 183f9f7 | 2004-05-13 05:20:26 +0000 | [diff] [blame] | 3109 | return SQLITE_OK; |
drh | 88a003e | 2008-12-11 16:17:03 +0000 | [diff] [blame] | 3110 | |
| 3111 | /* Jump here if database corruption is detected after m has been |
| 3112 | ** allocated. Free the m object and return SQLITE_CORRUPT. */ |
| 3113 | idx_rowid_corruption: |
| 3114 | testcase( m.zMalloc!=0 ); |
| 3115 | sqlite3VdbeMemRelease(&m); |
| 3116 | return SQLITE_CORRUPT_BKPT; |
danielk1977 | 183f9f7 | 2004-05-13 05:20:26 +0000 | [diff] [blame] | 3117 | } |
| 3118 | |
drh | 7cf6e4d | 2004-05-19 14:56:55 +0000 | [diff] [blame] | 3119 | /* |
drh | 5f82e3c | 2009-07-06 00:44:08 +0000 | [diff] [blame] | 3120 | ** Compare the key of the index entry that cursor pC is pointing to against |
| 3121 | ** the key string in pUnpacked. Write into *pRes a number |
drh | 7cf6e4d | 2004-05-19 14:56:55 +0000 | [diff] [blame] | 3122 | ** that is negative, zero, or positive if pC is less than, equal to, |
drh | 5f82e3c | 2009-07-06 00:44:08 +0000 | [diff] [blame] | 3123 | ** or greater than pUnpacked. Return SQLITE_OK on success. |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 3124 | ** |
drh | 5f82e3c | 2009-07-06 00:44:08 +0000 | [diff] [blame] | 3125 | ** pUnpacked is either created without a rowid or is truncated so that it |
drh | d578820 | 2004-05-28 08:21:05 +0000 | [diff] [blame] | 3126 | ** omits the rowid at the end. The rowid at the end of the index entry |
drh | ec1fc80 | 2008-08-13 14:07:40 +0000 | [diff] [blame] | 3127 | ** is ignored as well. Hence, this routine only compares the prefixes |
| 3128 | ** of the keys prior to the final rowid, not the entire key. |
drh | 7cf6e4d | 2004-05-19 14:56:55 +0000 | [diff] [blame] | 3129 | */ |
danielk1977 | 183f9f7 | 2004-05-13 05:20:26 +0000 | [diff] [blame] | 3130 | int sqlite3VdbeIdxKeyCompare( |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 3131 | VdbeCursor *pC, /* The cursor to compare against */ |
drh | 5f82e3c | 2009-07-06 00:44:08 +0000 | [diff] [blame] | 3132 | UnpackedRecord *pUnpacked, /* Unpacked version of key to compare against */ |
drh | 7cf6e4d | 2004-05-19 14:56:55 +0000 | [diff] [blame] | 3133 | int *res /* Write the comparison result here */ |
danielk1977 | 183f9f7 | 2004-05-13 05:20:26 +0000 | [diff] [blame] | 3134 | ){ |
drh | 61fc595 | 2007-04-01 23:49:51 +0000 | [diff] [blame] | 3135 | i64 nCellKey = 0; |
danielk1977 | 183f9f7 | 2004-05-13 05:20:26 +0000 | [diff] [blame] | 3136 | int rc; |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 3137 | BtCursor *pCur = pC->pCursor; |
drh | d578820 | 2004-05-28 08:21:05 +0000 | [diff] [blame] | 3138 | Mem m; |
danielk1977 | 183f9f7 | 2004-05-13 05:20:26 +0000 | [diff] [blame] | 3139 | |
drh | ea8ffdf | 2009-07-22 00:35:23 +0000 | [diff] [blame] | 3140 | assert( sqlite3BtreeCursorIsValid(pCur) ); |
drh | b07028f | 2011-10-14 21:49:18 +0000 | [diff] [blame^] | 3141 | VVA_ONLY(rc =) sqlite3BtreeKeySize(pCur, &nCellKey); |
drh | c27ae61 | 2009-07-14 18:35:44 +0000 | [diff] [blame] | 3142 | assert( rc==SQLITE_OK ); /* pCur is always valid so KeySize cannot fail */ |
drh | 407414c | 2009-07-14 14:15:27 +0000 | [diff] [blame] | 3143 | /* nCellKey will always be between 0 and 0xffffffff because of the say |
| 3144 | ** that btreeParseCellPtr() and sqlite3GetVarint32() are implemented */ |
drh | c27ae61 | 2009-07-14 18:35:44 +0000 | [diff] [blame] | 3145 | if( nCellKey<=0 || nCellKey>0x7fffffff ){ |
danielk1977 | 183f9f7 | 2004-05-13 05:20:26 +0000 | [diff] [blame] | 3146 | *res = 0; |
drh | 9978c97 | 2010-02-23 17:36:32 +0000 | [diff] [blame] | 3147 | return SQLITE_CORRUPT_BKPT; |
danielk1977 | 183f9f7 | 2004-05-13 05:20:26 +0000 | [diff] [blame] | 3148 | } |
drh | fd3ca1c | 2009-08-25 12:11:00 +0000 | [diff] [blame] | 3149 | memset(&m, 0, sizeof(m)); |
drh | 8df3284 | 2008-12-09 02:51:23 +0000 | [diff] [blame] | 3150 | rc = sqlite3VdbeMemFromBtree(pC->pCursor, 0, (int)nCellKey, 1, &m); |
drh | ec1fc80 | 2008-08-13 14:07:40 +0000 | [diff] [blame] | 3151 | if( rc ){ |
drh | d578820 | 2004-05-28 08:21:05 +0000 | [diff] [blame] | 3152 | return rc; |
danielk1977 | 183f9f7 | 2004-05-13 05:20:26 +0000 | [diff] [blame] | 3153 | } |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 3154 | assert( pUnpacked->flags & UNPACKED_IGNORE_ROWID ); |
| 3155 | *res = sqlite3VdbeRecordCompare(m.n, m.z, pUnpacked); |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 3156 | sqlite3VdbeMemRelease(&m); |
danielk1977 | 183f9f7 | 2004-05-13 05:20:26 +0000 | [diff] [blame] | 3157 | return SQLITE_OK; |
| 3158 | } |
danielk1977 | b28af71 | 2004-06-21 06:50:26 +0000 | [diff] [blame] | 3159 | |
| 3160 | /* |
| 3161 | ** This routine sets the value to be returned by subsequent calls to |
| 3162 | ** sqlite3_changes() on the database handle 'db'. |
| 3163 | */ |
| 3164 | void sqlite3VdbeSetChanges(sqlite3 *db, int nChange){ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 3165 | assert( sqlite3_mutex_held(db->mutex) ); |
danielk1977 | b28af71 | 2004-06-21 06:50:26 +0000 | [diff] [blame] | 3166 | db->nChange = nChange; |
| 3167 | db->nTotalChange += nChange; |
| 3168 | } |
| 3169 | |
| 3170 | /* |
| 3171 | ** Set a flag in the vdbe to update the change counter when it is finalised |
| 3172 | ** or reset. |
| 3173 | */ |
drh | 4794f73 | 2004-11-05 17:17:50 +0000 | [diff] [blame] | 3174 | void sqlite3VdbeCountChanges(Vdbe *v){ |
| 3175 | v->changeCntOn = 1; |
danielk1977 | b28af71 | 2004-06-21 06:50:26 +0000 | [diff] [blame] | 3176 | } |
drh | d89bd00 | 2005-01-22 03:03:54 +0000 | [diff] [blame] | 3177 | |
| 3178 | /* |
| 3179 | ** Mark every prepared statement associated with a database connection |
| 3180 | ** as expired. |
| 3181 | ** |
| 3182 | ** An expired statement means that recompilation of the statement is |
| 3183 | ** recommend. Statements expire when things happen that make their |
| 3184 | ** programs obsolete. Removing user-defined functions or collating |
| 3185 | ** sequences, or changing an authorization function are the types of |
| 3186 | ** things that make prepared statements obsolete. |
| 3187 | */ |
| 3188 | void sqlite3ExpirePreparedStatements(sqlite3 *db){ |
| 3189 | Vdbe *p; |
| 3190 | for(p = db->pVdbe; p; p=p->pNext){ |
| 3191 | p->expired = 1; |
| 3192 | } |
| 3193 | } |
danielk1977 | aee18ef | 2005-03-09 12:26:50 +0000 | [diff] [blame] | 3194 | |
| 3195 | /* |
| 3196 | ** Return the database associated with the Vdbe. |
| 3197 | */ |
| 3198 | sqlite3 *sqlite3VdbeDb(Vdbe *v){ |
| 3199 | return v->db; |
| 3200 | } |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 3201 | |
| 3202 | /* |
| 3203 | ** Return a pointer to an sqlite3_value structure containing the value bound |
| 3204 | ** parameter iVar of VM v. Except, if the value is an SQL NULL, return |
| 3205 | ** 0 instead. Unless it is NULL, apply affinity aff (one of the SQLITE_AFF_* |
| 3206 | ** constants) to the value before returning it. |
| 3207 | ** |
| 3208 | ** The returned value must be freed by the caller using sqlite3ValueFree(). |
| 3209 | */ |
| 3210 | sqlite3_value *sqlite3VdbeGetValue(Vdbe *v, int iVar, u8 aff){ |
| 3211 | assert( iVar>0 ); |
| 3212 | if( v ){ |
| 3213 | Mem *pMem = &v->aVar[iVar-1]; |
| 3214 | if( 0==(pMem->flags & MEM_Null) ){ |
| 3215 | sqlite3_value *pRet = sqlite3ValueNew(v->db); |
| 3216 | if( pRet ){ |
| 3217 | sqlite3VdbeMemCopy((Mem *)pRet, pMem); |
| 3218 | sqlite3ValueApplyAffinity(pRet, aff, SQLITE_UTF8); |
| 3219 | sqlite3VdbeMemStoreType((Mem *)pRet); |
| 3220 | } |
| 3221 | return pRet; |
| 3222 | } |
| 3223 | } |
| 3224 | return 0; |
| 3225 | } |
| 3226 | |
| 3227 | /* |
| 3228 | ** Configure SQL variable iVar so that binding a new value to it signals |
| 3229 | ** to sqlite3_reoptimize() that re-preparing the statement may result |
| 3230 | ** in a better query plan. |
| 3231 | */ |
dan | 1d2ce4f | 2009-10-19 18:11:09 +0000 | [diff] [blame] | 3232 | void sqlite3VdbeSetVarmask(Vdbe *v, int iVar){ |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 3233 | assert( iVar>0 ); |
| 3234 | if( iVar>32 ){ |
dan | 1d2ce4f | 2009-10-19 18:11:09 +0000 | [diff] [blame] | 3235 | v->expmask = 0xffffffff; |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 3236 | }else{ |
dan | 1d2ce4f | 2009-10-19 18:11:09 +0000 | [diff] [blame] | 3237 | v->expmask |= ((u32)1 << (iVar-1)); |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 3238 | } |
| 3239 | } |