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