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