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