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