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