blob: 7ddecaca1f3fe50f1a72a9d848a3bccd1cedc2d1 [file] [log] [blame]
drhcce7d172000-05-31 15:34:51 +00001/*
drhb19a2bc2001-09-16 00:13:26 +00002** 2001 September 15
drhcce7d172000-05-31 15:34:51 +00003**
drhb19a2bc2001-09-16 00:13:26 +00004** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
drhcce7d172000-05-31 15:34:51 +00006**
drhb19a2bc2001-09-16 00:13:26 +00007** 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.
drhcce7d172000-05-31 15:34:51 +000010**
11*************************************************************************
12** This file contains C code routines that are called by the parser
13** to handle UPDATE statements.
14**
drhed6c8672003-01-12 18:02:16 +000015** $Id: update.c,v 1.52 2003/01/12 18:02:19 drh Exp $
drhcce7d172000-05-31 15:34:51 +000016*/
17#include "sqliteInt.h"
18
19/*
20** Process an UPDATE statement.
21*/
22void 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 */
drh9cfcf5d2002-01-29 18:41:24 +000026 Expr *pWhere, /* The WHERE clause. May be null */
27 int onError /* How to handle constraint errors */
drhcce7d172000-05-31 15:34:51 +000028){
29 int i, j; /* Loop counters */
30 Table *pTab; /* The table to be updated */
drhad3cab52002-05-24 02:04:32 +000031 SrcList *pTabList = 0; /* Fake FROM clause containing only pTab */
drh0ca3e242002-01-29 23:07:02 +000032 int addr; /* VDBE instruction address of the start of the loop */
drhcce7d172000-05-31 15:34:51 +000033 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 */
drh0ca3e242002-01-29 23:07:02 +000037 int nIdxTotal; /* Total number of indices */
drh4794b982000-06-06 13:54:14 +000038 int base; /* Index of first available table cursor */
drhecdc7532001-09-23 02:35:53 +000039 sqlite *db; /* The database structure */
drhcce7d172000-05-31 15:34:51 +000040 Index **apIdx = 0; /* An array of indices that need updating too */
drh0ca3e242002-01-29 23:07:02 +000041 char *aIdxUsed = 0; /* aIdxUsed[i] if the i-th index is used */
drhcce7d172000-05-31 15:34:51 +000042 int *aXRef = 0; /* aXRef[i] is the index in pChanges->a[] of the
drh967e8b72000-06-21 13:59:10 +000043 ** an expression for the i-th column of the table.
44 ** aXRef[i]==-1 if the i-th column is not changed. */
drhf57b3392001-10-08 13:22:32 +000045 int openOp; /* Opcode used to open tables */
drh4a324312001-12-21 14:30:42 +000046 int chngRecno; /* True if the record number is being changed */
47 Expr *pRecnoExpr; /* Expression defining the new record number */
drh0ca3e242002-01-29 23:07:02 +000048 int openAll; /* True if all indices need to be opened */
drhcce7d172000-05-31 15:34:51 +000049
danielk1977c3f9bad2002-05-15 08:30:12 +000050 int row_triggers_exist = 0;
51
52 int newIdx = -1; /* index of trigger "new" temp table */
53 int oldIdx = -1; /* index of trigger "old" temp table */
54
drhdaffd0e2001-04-11 14:28:42 +000055 if( pParse->nErr || sqlite_malloc_failed ) goto update_cleanup;
drhed6c8672003-01-12 18:02:16 +000056 if( sqliteAuthCommand(pParse, "UPDATE", 0) ) goto update_cleanup;
drhecdc7532001-09-23 02:35:53 +000057 db = pParse->db;
drhdaffd0e2001-04-11 14:28:42 +000058
danielk1977c3f9bad2002-05-15 08:30:12 +000059 /* Check for the special case of a VIEW with one or more ON UPDATE triggers
60 * defined
61 */
62 {
danielk1977f29ce552002-05-19 23:43:12 +000063 char *zTab = sqliteTableNameFromToken(pTableName);
danielk1977c3f9bad2002-05-15 08:30:12 +000064
danielk1977f29ce552002-05-19 23:43:12 +000065 if( zTab != 0 ){
danielk1977c3f9bad2002-05-15 08:30:12 +000066 pTab = sqliteFindTable(pParse->db, zTab);
danielk1977f29ce552002-05-19 23:43:12 +000067 if( pTab ){
drh9adf9ac2002-05-15 11:44:13 +000068 row_triggers_exist =
69 sqliteTriggersExist(pParse, pTab->pTrigger,
70 TK_UPDATE, TK_BEFORE, TK_ROW, pChanges) ||
71 sqliteTriggersExist(pParse, pTab->pTrigger,
72 TK_UPDATE, TK_AFTER, TK_ROW, pChanges);
danielk1977c3f9bad2002-05-15 08:30:12 +000073 }
74 sqliteFree(zTab);
danielk1977f29ce552002-05-19 23:43:12 +000075 if( row_triggers_exist && pTab->pSelect ){
drh9adf9ac2002-05-15 11:44:13 +000076 /* Just fire VIEW triggers */
77 sqliteViewTriggers(pParse, pTab, pWhere, onError, pChanges);
78 return;
danielk1977c3f9bad2002-05-15 08:30:12 +000079 }
80 }
81 }
82
drhcce7d172000-05-31 15:34:51 +000083 /* Locate the table which we want to update. This table has to be
drhad3cab52002-05-24 02:04:32 +000084 ** put in an SrcList structure because some of the subroutines we
drhcce7d172000-05-31 15:34:51 +000085 ** will be calling are designed to work with multiple tables and expect
drhad3cab52002-05-24 02:04:32 +000086 ** an SrcList* parameter instead of just a Table* parameter.
drhcce7d172000-05-31 15:34:51 +000087 */
drhad3cab52002-05-24 02:04:32 +000088 pTabList = sqliteTableTokenToSrcList(pParse, pTableName);
drhdaffd0e2001-04-11 14:28:42 +000089 if( pTabList==0 ) goto update_cleanup;
drhcce7d172000-05-31 15:34:51 +000090 pTab = pTabList->a[0].pTab;
drh417be792002-03-03 18:59:40 +000091 assert( pTab->pSelect==0 ); /* This table is not a VIEW */
drhcce7d172000-05-31 15:34:51 +000092 aXRef = sqliteMalloc( sizeof(int) * pTab->nCol );
93 if( aXRef==0 ) goto update_cleanup;
94 for(i=0; i<pTab->nCol; i++) aXRef[i] = -1;
95
danielk1977f29ce552002-05-19 23:43:12 +000096 /* If there are FOR EACH ROW triggers, allocate temp tables */
97 if( row_triggers_exist ){
danielk1977c3f9bad2002-05-15 08:30:12 +000098 newIdx = pParse->nTab++;
99 oldIdx = pParse->nTab++;
100 }
101
drh53e3fc72002-07-16 17:22:50 +0000102 /* Allocate a cursors for the main database table and for all indices.
103 ** The index cursors might not be used, but if they are used they
104 ** need to occur right after the database cursor. So go ahead and
105 ** allocate enough space, just in case.
106 */
107 base = pParse->nTab++;
108 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
109 pParse->nTab++;
110 }
111
drh967e8b72000-06-21 13:59:10 +0000112 /* Resolve the column names in all the expressions in both the
113 ** WHERE clause and in the new values. Also find the column index
drhed6c8672003-01-12 18:02:16 +0000114 ** for each column to be updated in the pChanges array. For each
115 ** column to be updated, make sure we have authorization to change
116 ** that column.
drhcce7d172000-05-31 15:34:51 +0000117 */
118 if( pWhere ){
drh832508b2002-03-02 17:04:07 +0000119 if( sqliteExprResolveIds(pParse, base, pTabList, 0, pWhere) ){
drhcce7d172000-05-31 15:34:51 +0000120 goto update_cleanup;
121 }
122 if( sqliteExprCheck(pParse, pWhere, 0, 0) ){
123 goto update_cleanup;
124 }
125 }
drh4a324312001-12-21 14:30:42 +0000126 chngRecno = 0;
drhcce7d172000-05-31 15:34:51 +0000127 for(i=0; i<pChanges->nExpr; i++){
drh832508b2002-03-02 17:04:07 +0000128 if( sqliteExprResolveIds(pParse, base, pTabList, 0, pChanges->a[i].pExpr) ){
drhcce7d172000-05-31 15:34:51 +0000129 goto update_cleanup;
130 }
131 if( sqliteExprCheck(pParse, pChanges->a[i].pExpr, 0, 0) ){
132 goto update_cleanup;
133 }
134 for(j=0; j<pTab->nCol; j++){
drh6206d502000-06-19 19:09:08 +0000135 if( sqliteStrICmp(pTab->aCol[j].zName, pChanges->a[i].zName)==0 ){
drh9647ff82002-01-14 02:56:24 +0000136 if( j==pTab->iPKey ){
drh4a324312001-12-21 14:30:42 +0000137 chngRecno = 1;
138 pRecnoExpr = pChanges->a[i].pExpr;
139 }
drhcce7d172000-05-31 15:34:51 +0000140 aXRef[j] = i;
141 break;
142 }
143 }
144 if( j>=pTab->nCol ){
drh967e8b72000-06-21 13:59:10 +0000145 sqliteSetString(&pParse->zErrMsg, "no such column: ",
drhcce7d172000-05-31 15:34:51 +0000146 pChanges->a[i].zName, 0);
147 pParse->nErr++;
148 goto update_cleanup;
149 }
drhed6c8672003-01-12 18:02:16 +0000150#ifndef SQLITE_OMIT_AUTHORIZATION
151 if( sqliteAuthWrite(pParse, pTab, j)==SQLITE_IGNORE ){
152 aXRef[j] = -1;
153 }
154#endif
drhcce7d172000-05-31 15:34:51 +0000155 }
156
drh5a2c2c22001-11-21 02:21:11 +0000157 /* Allocate memory for the array apIdx[] and fill it with pointers to every
drhcce7d172000-05-31 15:34:51 +0000158 ** index that needs to be updated. Indices only need updating if their
drhc8392582001-12-31 02:48:51 +0000159 ** key includes one of the columns named in pChanges or if the record
160 ** number of the original table entry is changing.
drhcce7d172000-05-31 15:34:51 +0000161 */
drh0ca3e242002-01-29 23:07:02 +0000162 for(nIdx=nIdxTotal=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdxTotal++){
drh4a324312001-12-21 14:30:42 +0000163 if( chngRecno ){
164 i = 0;
165 }else {
166 for(i=0; i<pIdx->nColumn; i++){
167 if( aXRef[pIdx->aiColumn[i]]>=0 ) break;
168 }
drhcce7d172000-05-31 15:34:51 +0000169 }
drh967e8b72000-06-21 13:59:10 +0000170 if( i<pIdx->nColumn ) nIdx++;
drhcce7d172000-05-31 15:34:51 +0000171 }
drh0ca3e242002-01-29 23:07:02 +0000172 if( nIdxTotal>0 ){
173 apIdx = sqliteMalloc( sizeof(Index*) * nIdx + nIdxTotal );
drhb0729502001-03-14 12:35:57 +0000174 if( apIdx==0 ) goto update_cleanup;
drh0ca3e242002-01-29 23:07:02 +0000175 aIdxUsed = (char*)&apIdx[nIdx];
drhb0729502001-03-14 12:35:57 +0000176 }
drh0ca3e242002-01-29 23:07:02 +0000177 for(nIdx=j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
drh4a324312001-12-21 14:30:42 +0000178 if( chngRecno ){
179 i = 0;
180 }else{
181 for(i=0; i<pIdx->nColumn; i++){
182 if( aXRef[pIdx->aiColumn[i]]>=0 ) break;
183 }
drhcce7d172000-05-31 15:34:51 +0000184 }
drh0ca3e242002-01-29 23:07:02 +0000185 if( i<pIdx->nColumn ){
186 apIdx[nIdx++] = pIdx;
187 aIdxUsed[j] = 1;
188 }else{
189 aIdxUsed[j] = 0;
190 }
drhcce7d172000-05-31 15:34:51 +0000191 }
192
193 /* Begin generating code.
194 */
drhd8bc7082000-06-07 23:51:50 +0000195 v = sqliteGetVdbe(pParse);
drhcce7d172000-05-31 15:34:51 +0000196 if( v==0 ) goto update_cleanup;
drhcabb0812002-09-14 13:47:32 +0000197 sqliteBeginWriteOperation(pParse, 1, !row_triggers_exist && pTab->isTemp);
drhcce7d172000-05-31 15:34:51 +0000198
199 /* Begin the database scan
200 */
drhe3184742002-06-19 14:27:05 +0000201 pWInfo = sqliteWhereBegin(pParse, base, pTabList, pWhere, 1, 0);
drhcce7d172000-05-31 15:34:51 +0000202 if( pWInfo==0 ) goto update_cleanup;
203
204 /* Remember the index of every item to be updated.
205 */
drh99fcd712001-10-13 01:06:47 +0000206 sqliteVdbeAddOp(v, OP_ListWrite, 0, 0);
drhcce7d172000-05-31 15:34:51 +0000207
208 /* End the database scan loop.
209 */
210 sqliteWhereEnd(pWInfo);
211
drh1bee3d72001-10-15 00:44:35 +0000212 /* Initialize the count of updated rows
213 */
danielk1977c3f9bad2002-05-15 08:30:12 +0000214 if( db->flags & SQLITE_CountRows && !pParse->trigStack ){
drh1bee3d72001-10-15 00:44:35 +0000215 sqliteVdbeAddOp(v, OP_Integer, 0, 0);
216 }
217
danielk1977f29ce552002-05-19 23:43:12 +0000218 if( row_triggers_exist ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000219 int ii;
220
221 sqliteVdbeAddOp(v, OP_OpenTemp, oldIdx, 0);
222 sqliteVdbeAddOp(v, OP_OpenTemp, newIdx, 0);
223
224 sqliteVdbeAddOp(v, OP_ListRewind, 0, 0);
225 addr = sqliteVdbeAddOp(v, OP_ListRead, 0, 0);
226 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
227
228 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
danielk1977368c7f62002-07-21 23:09:55 +0000229 sqliteVdbeAddOp(v, (pTab->isTemp?OP_OpenAux:OP_Open), base, pTab->tnum);
danielk1977c3f9bad2002-05-15 08:30:12 +0000230 sqliteVdbeAddOp(v, OP_MoveTo, base, 0);
231
232 sqliteVdbeAddOp(v, OP_Integer, 13, 0);
danielk1977f29ce552002-05-19 23:43:12 +0000233 for(ii = 0; ii < pTab->nCol; ii++){
234 if( ii == pTab->iPKey ){
235 sqliteVdbeAddOp(v, OP_Recno, base, 0);
236 }else{
237 sqliteVdbeAddOp(v, OP_Column, base, ii);
238 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000239 }
240 sqliteVdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0);
241 sqliteVdbeAddOp(v, OP_PutIntKey, oldIdx, 0);
242
243 sqliteVdbeAddOp(v, OP_Integer, 13, 0);
danielk1977f29ce552002-05-19 23:43:12 +0000244 for(ii = 0; ii < pTab->nCol; ii++){
danielk1977c3f9bad2002-05-15 08:30:12 +0000245 if( aXRef[ii] < 0 ){
danielk1977f29ce552002-05-19 23:43:12 +0000246 if( ii == pTab->iPKey ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000247 sqliteVdbeAddOp(v, OP_Recno, base, 0);
danielk1977f29ce552002-05-19 23:43:12 +0000248 }else{
danielk1977c3f9bad2002-05-15 08:30:12 +0000249 sqliteVdbeAddOp(v, OP_Column, base, ii);
danielk1977f29ce552002-05-19 23:43:12 +0000250 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000251 }else{
252 sqliteExprCode(pParse, pChanges->a[aXRef[ii]].pExpr);
253 }
254 }
255 sqliteVdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0);
256 sqliteVdbeAddOp(v, OP_PutIntKey, newIdx, 0);
257 sqliteVdbeAddOp(v, OP_Close, base, 0);
258
259 sqliteVdbeAddOp(v, OP_Rewind, oldIdx, 0);
260 sqliteVdbeAddOp(v, OP_Rewind, newIdx, 0);
261
danielk1977f29ce552002-05-19 23:43:12 +0000262 if( sqliteCodeRowTrigger(pParse, TK_UPDATE, pChanges, TK_BEFORE, pTab,
danielk19776f349032002-06-11 02:25:40 +0000263 newIdx, oldIdx, onError, addr) ){
danielk1977f29ce552002-05-19 23:43:12 +0000264 goto update_cleanup;
265 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000266 }
267
drhcce7d172000-05-31 15:34:51 +0000268 /* Rewind the list of records that need to be updated and
drh0ca3e242002-01-29 23:07:02 +0000269 ** open every index that needs updating. Note that if any
270 ** index could potentially invoke a REPLACE conflict resolution
271 ** action, then we need to open all indices because we might need
272 ** to be deleting some records.
drhcce7d172000-05-31 15:34:51 +0000273 */
drhf57b3392001-10-08 13:22:32 +0000274 openOp = pTab->isTemp ? OP_OpenWrAux : OP_OpenWrite;
drh99fcd712001-10-13 01:06:47 +0000275 sqliteVdbeAddOp(v, openOp, base, pTab->tnum);
drh0ca3e242002-01-29 23:07:02 +0000276 if( onError==OE_Replace ){
277 openAll = 1;
278 }else{
279 openAll = 0;
280 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
281 if( pIdx->onError==OE_Replace ){
282 openAll = 1;
283 break;
284 }
285 }
286 }
287 for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
288 if( openAll || aIdxUsed[i] ){
289 sqliteVdbeAddOp(v, openOp, base+i+1, pIdx->tnum);
drh53e3fc72002-07-16 17:22:50 +0000290 assert( pParse->nTab>base+i+1 );
drh0ca3e242002-01-29 23:07:02 +0000291 }
drhcce7d172000-05-31 15:34:51 +0000292 }
293
294 /* Loop over every record that needs updating. We have to load
drh967e8b72000-06-21 13:59:10 +0000295 ** the old data for each record to be updated because some columns
drh1ccde152000-06-17 13:12:39 +0000296 ** might not change and we will need to copy the old value.
drhcce7d172000-05-31 15:34:51 +0000297 ** Also, the old data is needed to delete the old index entires.
drh4a324312001-12-21 14:30:42 +0000298 ** So make the cursor point at the old record.
drhcce7d172000-05-31 15:34:51 +0000299 */
danielk1977f29ce552002-05-19 23:43:12 +0000300 if( !row_triggers_exist ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000301 sqliteVdbeAddOp(v, OP_ListRewind, 0, 0);
302 addr = sqliteVdbeAddOp(v, OP_ListRead, 0, 0);
303 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
304 }
drh07d6e3a2002-05-23 12:50:18 +0000305 sqliteVdbeAddOp(v, OP_NotExists, base, addr);
drhcce7d172000-05-31 15:34:51 +0000306
drh0ca3e242002-01-29 23:07:02 +0000307 /* If the record number will change, push the record number as it
308 ** will be after the update. (The old record number is currently
309 ** on top of the stack.)
drh4a324312001-12-21 14:30:42 +0000310 */
311 if( chngRecno ){
drha9f9d1c2002-06-29 02:20:08 +0000312 sqliteExprCode(pParse, pRecnoExpr);
313 sqliteVdbeAddOp(v, OP_MustBeInt, 0, 0);
drh4a324312001-12-21 14:30:42 +0000314 }
315
drh5a2c2c22001-11-21 02:21:11 +0000316 /* Compute new data for this record.
drhcce7d172000-05-31 15:34:51 +0000317 */
318 for(i=0; i<pTab->nCol; i++){
drh4a324312001-12-21 14:30:42 +0000319 if( i==pTab->iPKey ){
drh0ca3e242002-01-29 23:07:02 +0000320 sqliteVdbeAddOp(v, OP_String, 0, 0);
drh4a324312001-12-21 14:30:42 +0000321 continue;
322 }
drhcce7d172000-05-31 15:34:51 +0000323 j = aXRef[i];
324 if( j<0 ){
drh99fcd712001-10-13 01:06:47 +0000325 sqliteVdbeAddOp(v, OP_Column, base, i);
drhcce7d172000-05-31 15:34:51 +0000326 }else{
327 sqliteExprCode(pParse, pChanges->a[j].pExpr);
328 }
329 }
330
drh0ca3e242002-01-29 23:07:02 +0000331 /* Do constraint checks
332 */
drhb419a922002-01-30 16:17:23 +0000333 sqliteGenerateConstraintChecks(pParse, pTab, base, aIdxUsed, chngRecno, 1,
334 onError, addr);
drh0ca3e242002-01-29 23:07:02 +0000335
336 /* Delete the old indices for the current record.
337 */
drh38640e12002-07-05 21:42:36 +0000338 sqliteGenerateRowIndexDelete(db, v, pTab, base, aIdxUsed);
drh0ca3e242002-01-29 23:07:02 +0000339
drhc8392582001-12-31 02:48:51 +0000340 /* If changing the record number, delete the old record.
drh4a324312001-12-21 14:30:42 +0000341 */
342 if( chngRecno ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000343 sqliteVdbeAddOp(v, OP_Delete, base, 0);
drh4a324312001-12-21 14:30:42 +0000344 }
345
drh0ca3e242002-01-29 23:07:02 +0000346 /* Create the new index entries and the new record.
drhcce7d172000-05-31 15:34:51 +0000347 */
drhb419a922002-01-30 16:17:23 +0000348 sqliteCompleteInsertion(pParse, pTab, base, aIdxUsed, chngRecno, 1);
drhcce7d172000-05-31 15:34:51 +0000349
drh0ca3e242002-01-29 23:07:02 +0000350 /* Increment the row counter
drh1bee3d72001-10-15 00:44:35 +0000351 */
danielk1977c3f9bad2002-05-15 08:30:12 +0000352 if( db->flags & SQLITE_CountRows && !pParse->trigStack){
drh1bee3d72001-10-15 00:44:35 +0000353 sqliteVdbeAddOp(v, OP_AddImm, 1, 0);
354 }
355
danielk1977f29ce552002-05-19 23:43:12 +0000356 if( row_triggers_exist ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000357 for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
358 if( openAll || aIdxUsed[i] )
drh9adf9ac2002-05-15 11:44:13 +0000359 sqliteVdbeAddOp(v, OP_Close, base+i+1, 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000360 }
361 sqliteVdbeAddOp(v, OP_Close, base, 0);
362 pParse->nTab = base;
363
danielk1977f29ce552002-05-19 23:43:12 +0000364 if( sqliteCodeRowTrigger(pParse, TK_UPDATE, pChanges, TK_AFTER, pTab,
danielk19776f349032002-06-11 02:25:40 +0000365 newIdx, oldIdx, onError, addr) ){
danielk1977f29ce552002-05-19 23:43:12 +0000366 goto update_cleanup;
367 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000368 }
369
drhcce7d172000-05-31 15:34:51 +0000370 /* Repeat the above with the next record to be updated, until
371 ** all record selected by the WHERE clause have been updated.
372 */
drh99fcd712001-10-13 01:06:47 +0000373 sqliteVdbeAddOp(v, OP_Goto, 0, addr);
drh0ca3e242002-01-29 23:07:02 +0000374 sqliteVdbeChangeP2(v, addr, sqliteVdbeCurrentAddr(v));
drha8b38d22001-11-01 14:41:34 +0000375 sqliteVdbeAddOp(v, OP_ListReset, 0, 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000376
377 /* Close all tables if there were no FOR EACH ROW triggers */
danielk1977f29ce552002-05-19 23:43:12 +0000378 if( !row_triggers_exist ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000379 for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
380 if( openAll || aIdxUsed[i] ){
drh9adf9ac2002-05-15 11:44:13 +0000381 sqliteVdbeAddOp(v, OP_Close, base+i+1, 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000382 }
383 }
384 sqliteVdbeAddOp(v, OP_Close, base, 0);
385 pParse->nTab = base;
danielk1977f29ce552002-05-19 23:43:12 +0000386 }else{
danielk1977c3f9bad2002-05-15 08:30:12 +0000387 sqliteVdbeAddOp(v, OP_Close, newIdx, 0);
388 sqliteVdbeAddOp(v, OP_Close, oldIdx, 0);
389 }
390
drh1c928532002-01-31 15:54:21 +0000391 sqliteEndWriteOperation(pParse);
drhcce7d172000-05-31 15:34:51 +0000392
drh1bee3d72001-10-15 00:44:35 +0000393 /*
394 ** Return the number of rows that were changed.
395 */
danielk1977c3f9bad2002-05-15 08:30:12 +0000396 if( db->flags & SQLITE_CountRows && !pParse->trigStack ){
drh1bee3d72001-10-15 00:44:35 +0000397 sqliteVdbeAddOp(v, OP_ColumnName, 0, 0);
398 sqliteVdbeChangeP3(v, -1, "rows updated", P3_STATIC);
399 sqliteVdbeAddOp(v, OP_Callback, 1, 0);
400 }
401
drhcce7d172000-05-31 15:34:51 +0000402update_cleanup:
403 sqliteFree(apIdx);
404 sqliteFree(aXRef);
drhad3cab52002-05-24 02:04:32 +0000405 sqliteSrcListDelete(pTabList);
drhcce7d172000-05-31 15:34:51 +0000406 sqliteExprListDelete(pChanges);
407 sqliteExprDelete(pWhere);
408 return;
409}