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