drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1 | /* |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2 | ** 2001 September 15 |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 3 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 4 | ** The author disclaims copyright to this source code. In place of |
| 5 | ** a legal notice, here is a blessing: |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 6 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 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. |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 10 | ** |
| 11 | ************************************************************************* |
| 12 | ** This file contains C code routines that are called by the parser |
| 13 | ** to handle UPDATE statements. |
| 14 | ** |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame^] | 15 | ** $Id: update.c,v 1.30 2002/01/29 23:07:02 drh Exp $ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 16 | */ |
| 17 | #include "sqliteInt.h" |
| 18 | |
| 19 | /* |
| 20 | ** Process an UPDATE statement. |
| 21 | */ |
| 22 | void sqliteUpdate( |
| 23 | Parse *pParse, /* The parser context */ |
| 24 | Token *pTableName, /* The table in which we should change things */ |
| 25 | ExprList *pChanges, /* Things to be changed */ |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 26 | Expr *pWhere, /* The WHERE clause. May be null */ |
| 27 | int onError /* How to handle constraint errors */ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 28 | ){ |
| 29 | int i, j; /* Loop counters */ |
| 30 | Table *pTab; /* The table to be updated */ |
| 31 | IdList *pTabList = 0; /* List containing only pTab */ |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame^] | 32 | int addr; /* VDBE instruction address of the start of the loop */ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 33 | WhereInfo *pWInfo; /* Information about the WHERE clause */ |
| 34 | Vdbe *v; /* The virtual database engine */ |
| 35 | Index *pIdx; /* For looping over indices */ |
| 36 | int nIdx; /* Number of indices that need updating */ |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame^] | 37 | int nIdxTotal; /* Total number of indices */ |
drh | 4794b98 | 2000-06-06 13:54:14 +0000 | [diff] [blame] | 38 | int base; /* Index of first available table cursor */ |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 39 | sqlite *db; /* The database structure */ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 40 | Index **apIdx = 0; /* An array of indices that need updating too */ |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame^] | 41 | char *aIdxUsed = 0; /* aIdxUsed[i] if the i-th index is used */ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 42 | int *aXRef = 0; /* aXRef[i] is the index in pChanges->a[] of the |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 43 | ** an expression for the i-th column of the table. |
| 44 | ** aXRef[i]==-1 if the i-th column is not changed. */ |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 45 | int openOp; /* Opcode used to open tables */ |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 46 | int chngRecno; /* True if the record number is being changed */ |
| 47 | Expr *pRecnoExpr; /* Expression defining the new record number */ |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame^] | 48 | int openAll; /* True if all indices need to be opened */ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 49 | |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 50 | if( pParse->nErr || sqlite_malloc_failed ) goto update_cleanup; |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 51 | db = pParse->db; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 52 | |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 53 | /* Locate the table which we want to update. This table has to be |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 54 | ** put in an IdList structure because some of the subroutines we |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 55 | ** will be calling are designed to work with multiple tables and expect |
drh | c839258 | 2001-12-31 02:48:51 +0000 | [diff] [blame] | 56 | ** an IdList* parameter instead of just a Table* parameter. |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 57 | */ |
| 58 | pTabList = sqliteIdListAppend(0, pTableName); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 59 | if( pTabList==0 ) goto update_cleanup; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 60 | for(i=0; i<pTabList->nId; i++){ |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 61 | pTabList->a[i].pTab = sqliteFindTable(db, pTabList->a[i].zName); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 62 | if( pTabList->a[i].pTab==0 ){ |
| 63 | sqliteSetString(&pParse->zErrMsg, "no such table: ", |
| 64 | pTabList->a[i].zName, 0); |
| 65 | pParse->nErr++; |
| 66 | goto update_cleanup; |
| 67 | } |
| 68 | if( pTabList->a[i].pTab->readOnly ){ |
| 69 | sqliteSetString(&pParse->zErrMsg, "table ", pTabList->a[i].zName, |
| 70 | " may not be modified", 0); |
| 71 | pParse->nErr++; |
| 72 | goto update_cleanup; |
| 73 | } |
| 74 | } |
| 75 | pTab = pTabList->a[0].pTab; |
| 76 | aXRef = sqliteMalloc( sizeof(int) * pTab->nCol ); |
| 77 | if( aXRef==0 ) goto update_cleanup; |
| 78 | for(i=0; i<pTab->nCol; i++) aXRef[i] = -1; |
| 79 | |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 80 | /* Resolve the column names in all the expressions in both the |
| 81 | ** WHERE clause and in the new values. Also find the column index |
| 82 | ** for each column to be updated in the pChanges array. |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 83 | */ |
| 84 | if( pWhere ){ |
drh | 4794b98 | 2000-06-06 13:54:14 +0000 | [diff] [blame] | 85 | sqliteExprResolveInSelect(pParse, pWhere); |
| 86 | } |
| 87 | for(i=0; i<pChanges->nExpr; i++){ |
| 88 | sqliteExprResolveInSelect(pParse, pChanges->a[i].pExpr); |
| 89 | } |
| 90 | if( pWhere ){ |
drh | a2e0004 | 2002-01-22 03:13:42 +0000 | [diff] [blame] | 91 | if( sqliteExprResolveIds(pParse, pTabList, 0, pWhere) ){ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 92 | goto update_cleanup; |
| 93 | } |
| 94 | if( sqliteExprCheck(pParse, pWhere, 0, 0) ){ |
| 95 | goto update_cleanup; |
| 96 | } |
| 97 | } |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 98 | chngRecno = 0; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 99 | for(i=0; i<pChanges->nExpr; i++){ |
drh | a2e0004 | 2002-01-22 03:13:42 +0000 | [diff] [blame] | 100 | if( sqliteExprResolveIds(pParse, pTabList, 0, pChanges->a[i].pExpr) ){ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 101 | goto update_cleanup; |
| 102 | } |
| 103 | if( sqliteExprCheck(pParse, pChanges->a[i].pExpr, 0, 0) ){ |
| 104 | goto update_cleanup; |
| 105 | } |
| 106 | for(j=0; j<pTab->nCol; j++){ |
drh | 6206d50 | 2000-06-19 19:09:08 +0000 | [diff] [blame] | 107 | if( sqliteStrICmp(pTab->aCol[j].zName, pChanges->a[i].zName)==0 ){ |
drh | 9647ff8 | 2002-01-14 02:56:24 +0000 | [diff] [blame] | 108 | if( j==pTab->iPKey ){ |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 109 | chngRecno = 1; |
| 110 | pRecnoExpr = pChanges->a[i].pExpr; |
| 111 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 112 | aXRef[j] = i; |
| 113 | break; |
| 114 | } |
| 115 | } |
| 116 | if( j>=pTab->nCol ){ |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 117 | sqliteSetString(&pParse->zErrMsg, "no such column: ", |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 118 | pChanges->a[i].zName, 0); |
| 119 | pParse->nErr++; |
| 120 | goto update_cleanup; |
| 121 | } |
| 122 | } |
| 123 | |
drh | 5a2c2c2 | 2001-11-21 02:21:11 +0000 | [diff] [blame] | 124 | /* Allocate memory for the array apIdx[] and fill it with pointers to every |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 125 | ** index that needs to be updated. Indices only need updating if their |
drh | c839258 | 2001-12-31 02:48:51 +0000 | [diff] [blame] | 126 | ** key includes one of the columns named in pChanges or if the record |
| 127 | ** number of the original table entry is changing. |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 128 | */ |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame^] | 129 | for(nIdx=nIdxTotal=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdxTotal++){ |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 130 | if( chngRecno ){ |
| 131 | i = 0; |
| 132 | }else { |
| 133 | for(i=0; i<pIdx->nColumn; i++){ |
| 134 | if( aXRef[pIdx->aiColumn[i]]>=0 ) break; |
| 135 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 136 | } |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 137 | if( i<pIdx->nColumn ) nIdx++; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 138 | } |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame^] | 139 | if( nIdxTotal>0 ){ |
| 140 | apIdx = sqliteMalloc( sizeof(Index*) * nIdx + nIdxTotal ); |
drh | b072950 | 2001-03-14 12:35:57 +0000 | [diff] [blame] | 141 | if( apIdx==0 ) goto update_cleanup; |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame^] | 142 | aIdxUsed = (char*)&apIdx[nIdx]; |
drh | b072950 | 2001-03-14 12:35:57 +0000 | [diff] [blame] | 143 | } |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame^] | 144 | for(nIdx=j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){ |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 145 | if( chngRecno ){ |
| 146 | i = 0; |
| 147 | }else{ |
| 148 | for(i=0; i<pIdx->nColumn; i++){ |
| 149 | if( aXRef[pIdx->aiColumn[i]]>=0 ) break; |
| 150 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 151 | } |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame^] | 152 | if( i<pIdx->nColumn ){ |
| 153 | apIdx[nIdx++] = pIdx; |
| 154 | aIdxUsed[j] = 1; |
| 155 | }else{ |
| 156 | aIdxUsed[j] = 0; |
| 157 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | /* Begin generating code. |
| 161 | */ |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 162 | v = sqliteGetVdbe(pParse); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 163 | if( v==0 ) goto update_cleanup; |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 164 | if( (db->flags & SQLITE_InTrans)==0 ){ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 165 | sqliteVdbeAddOp(v, OP_Transaction, 0, 0); |
| 166 | sqliteVdbeAddOp(v, OP_VerifyCookie, db->schema_cookie, 0); |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 167 | pParse->schemaVerified = 1; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 168 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 169 | |
| 170 | /* Begin the database scan |
| 171 | */ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 172 | pWInfo = sqliteWhereBegin(pParse, pTabList, pWhere, 1); |
| 173 | if( pWInfo==0 ) goto update_cleanup; |
| 174 | |
| 175 | /* Remember the index of every item to be updated. |
| 176 | */ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 177 | sqliteVdbeAddOp(v, OP_ListWrite, 0, 0); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 178 | |
| 179 | /* End the database scan loop. |
| 180 | */ |
| 181 | sqliteWhereEnd(pWInfo); |
| 182 | |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 183 | /* Initialize the count of updated rows |
| 184 | */ |
| 185 | if( db->flags & SQLITE_CountRows ){ |
| 186 | sqliteVdbeAddOp(v, OP_Integer, 0, 0); |
| 187 | } |
| 188 | |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 189 | /* Rewind the list of records that need to be updated and |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame^] | 190 | ** open every index that needs updating. Note that if any |
| 191 | ** index could potentially invoke a REPLACE conflict resolution |
| 192 | ** action, then we need to open all indices because we might need |
| 193 | ** to be deleting some records. |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 194 | */ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 195 | sqliteVdbeAddOp(v, OP_ListRewind, 0, 0); |
drh | 4794b98 | 2000-06-06 13:54:14 +0000 | [diff] [blame] | 196 | base = pParse->nTab; |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 197 | openOp = pTab->isTemp ? OP_OpenWrAux : OP_OpenWrite; |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 198 | sqliteVdbeAddOp(v, openOp, base, pTab->tnum); |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame^] | 199 | if( onError==OE_Replace ){ |
| 200 | openAll = 1; |
| 201 | }else{ |
| 202 | openAll = 0; |
| 203 | for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
| 204 | if( pIdx->onError==OE_Replace ){ |
| 205 | openAll = 1; |
| 206 | break; |
| 207 | } |
| 208 | } |
| 209 | } |
| 210 | for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){ |
| 211 | if( openAll || aIdxUsed[i] ){ |
| 212 | sqliteVdbeAddOp(v, openOp, base+i+1, pIdx->tnum); |
| 213 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | /* Loop over every record that needs updating. We have to load |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 217 | ** the old data for each record to be updated because some columns |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 218 | ** might not change and we will need to copy the old value. |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 219 | ** Also, the old data is needed to delete the old index entires. |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 220 | ** So make the cursor point at the old record. |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 221 | */ |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame^] | 222 | addr = sqliteVdbeAddOp(v, OP_ListRead, 0, 0); |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 223 | sqliteVdbeAddOp(v, OP_Dup, 0, 0); |
| 224 | sqliteVdbeAddOp(v, OP_MoveTo, base, 0); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 225 | |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame^] | 226 | /* If the record number will change, push the record number as it |
| 227 | ** will be after the update. (The old record number is currently |
| 228 | ** on top of the stack.) |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 229 | */ |
| 230 | if( chngRecno ){ |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame^] | 231 | if( pTab->iPKey<0 || (j = aXRef[pTab->iPKey])<0 ){ |
| 232 | sqliteVdbeAddOp(v, OP_Dup, 0, 0); |
| 233 | }else{ |
| 234 | sqliteExprCode(pParse, pChanges->a[j].pExpr); |
| 235 | sqliteVdbeAddOp(v, OP_MustBeInt, 0, 0); |
| 236 | } |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 237 | } |
| 238 | |
drh | 5a2c2c2 | 2001-11-21 02:21:11 +0000 | [diff] [blame] | 239 | /* Compute new data for this record. |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 240 | */ |
| 241 | for(i=0; i<pTab->nCol; i++){ |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 242 | if( i==pTab->iPKey ){ |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame^] | 243 | sqliteVdbeAddOp(v, OP_String, 0, 0); |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 244 | continue; |
| 245 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 246 | j = aXRef[i]; |
| 247 | if( j<0 ){ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 248 | sqliteVdbeAddOp(v, OP_Column, base, i); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 249 | }else{ |
| 250 | sqliteExprCode(pParse, pChanges->a[j].pExpr); |
| 251 | } |
| 252 | } |
| 253 | |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame^] | 254 | /* Do constraint checks |
| 255 | */ |
| 256 | sqliteGenerateConstraintChecks(pParse, pTab, base, aIdxUsed, chngRecno, |
| 257 | onError, addr,1); |
| 258 | |
| 259 | /* Delete the old indices for the current record. |
| 260 | */ |
| 261 | sqliteGenerateRowIndexDelete(v, pTab, base, aIdxUsed); |
| 262 | |
drh | c839258 | 2001-12-31 02:48:51 +0000 | [diff] [blame] | 263 | /* If changing the record number, delete the old record. |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 264 | */ |
| 265 | if( chngRecno ){ |
| 266 | sqliteVdbeAddOp(v, OP_Delete, 0, 0); |
| 267 | } |
| 268 | |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame^] | 269 | /* Create the new index entries and the new record. |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 270 | */ |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame^] | 271 | sqliteCompleteInsertion(pParse, pTab, base, aIdxUsed, chngRecno); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 272 | |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame^] | 273 | /* Increment the row counter |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 274 | */ |
| 275 | if( db->flags & SQLITE_CountRows ){ |
| 276 | sqliteVdbeAddOp(v, OP_AddImm, 1, 0); |
| 277 | } |
| 278 | |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 279 | /* Repeat the above with the next record to be updated, until |
| 280 | ** all record selected by the WHERE clause have been updated. |
| 281 | */ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 282 | sqliteVdbeAddOp(v, OP_Goto, 0, addr); |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame^] | 283 | sqliteVdbeChangeP2(v, addr, sqliteVdbeCurrentAddr(v)); |
drh | a8b38d2 | 2001-11-01 14:41:34 +0000 | [diff] [blame] | 284 | sqliteVdbeAddOp(v, OP_ListReset, 0, 0); |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 285 | if( (db->flags & SQLITE_InTrans)==0 ){ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 286 | sqliteVdbeAddOp(v, OP_Commit, 0, 0); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 287 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 288 | |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 289 | /* |
| 290 | ** Return the number of rows that were changed. |
| 291 | */ |
| 292 | if( db->flags & SQLITE_CountRows ){ |
| 293 | sqliteVdbeAddOp(v, OP_ColumnCount, 1, 0); |
| 294 | sqliteVdbeAddOp(v, OP_ColumnName, 0, 0); |
| 295 | sqliteVdbeChangeP3(v, -1, "rows updated", P3_STATIC); |
| 296 | sqliteVdbeAddOp(v, OP_Callback, 1, 0); |
| 297 | } |
| 298 | |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 299 | update_cleanup: |
| 300 | sqliteFree(apIdx); |
| 301 | sqliteFree(aXRef); |
| 302 | sqliteIdListDelete(pTabList); |
| 303 | sqliteExprListDelete(pChanges); |
| 304 | sqliteExprDelete(pWhere); |
| 305 | return; |
| 306 | } |