blob: 93a67448c4186f8a214b99c53db880a6658f2df3 [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**
drh113088e2003-03-20 01:16:58 +000015** $Id: update.c,v 1.55 2003/03/20 01:16:59 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 */
drh113088e2003-03-20 01:16:58 +000024 SrcList *pTabList, /* The table in which we should change things */
drhcce7d172000-05-31 15:34:51 +000025 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 */
drh113088e2003-03-20 01:16:58 +000030 char *zTab; /* Name of the table to be updated */
drhcce7d172000-05-31 15:34:51 +000031 Table *pTab; /* The table to be updated */
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. */
drh4a324312001-12-21 14:30:42 +000045 int chngRecno; /* True if the record number is being changed */
46 Expr *pRecnoExpr; /* Expression defining the new record number */
drh0ca3e242002-01-29 23:07:02 +000047 int openAll; /* True if all indices need to be opened */
drhcce7d172000-05-31 15:34:51 +000048
danielk1977c3f9bad2002-05-15 08:30:12 +000049 int row_triggers_exist = 0;
50
51 int newIdx = -1; /* index of trigger "new" temp table */
52 int oldIdx = -1; /* index of trigger "old" temp table */
53
drhdaffd0e2001-04-11 14:28:42 +000054 if( pParse->nErr || sqlite_malloc_failed ) goto update_cleanup;
drhecdc7532001-09-23 02:35:53 +000055 db = pParse->db;
drh113088e2003-03-20 01:16:58 +000056 assert( pTabList->nSrc==1 );
drhdaffd0e2001-04-11 14:28:42 +000057
danielk1977c3f9bad2002-05-15 08:30:12 +000058 /* Check for the special case of a VIEW with one or more ON UPDATE triggers
59 * defined
60 */
drh113088e2003-03-20 01:16:58 +000061 zTab = pTabList->a[0].zName;
62 if( zTab != 0 ){
63 pTab = sqliteFindTable(pParse->db, zTab);
64 if( pTab ){
65 row_triggers_exist =
66 sqliteTriggersExist(pParse, pTab->pTrigger,
67 TK_UPDATE, TK_BEFORE, TK_ROW, pChanges) ||
68 sqliteTriggersExist(pParse, pTab->pTrigger,
69 TK_UPDATE, TK_AFTER, TK_ROW, pChanges);
70 }
danielk1977c3f9bad2002-05-15 08:30:12 +000071
drh113088e2003-03-20 01:16:58 +000072 if( row_triggers_exist && pTab->pSelect ){
73 /* Just fire VIEW triggers */
74 sqliteSrcListDelete(pTabList);
75 sqliteViewTriggers(pParse, pTab, pWhere, onError, pChanges);
76 return;
danielk1977c3f9bad2002-05-15 08:30:12 +000077 }
78 }
79
drhcce7d172000-05-31 15:34:51 +000080 /* Locate the table which we want to update. This table has to be
drhad3cab52002-05-24 02:04:32 +000081 ** put in an SrcList structure because some of the subroutines we
drhcce7d172000-05-31 15:34:51 +000082 ** will be calling are designed to work with multiple tables and expect
drhad3cab52002-05-24 02:04:32 +000083 ** an SrcList* parameter instead of just a Table* parameter.
drhcce7d172000-05-31 15:34:51 +000084 */
drh113088e2003-03-20 01:16:58 +000085 pTab = pTabList->a[0].pTab = sqliteTableNameToTable(pParse, zTab);
86 if( pTab==0 ) goto update_cleanup;
drh417be792002-03-03 18:59:40 +000087 assert( pTab->pSelect==0 ); /* This table is not a VIEW */
drhcce7d172000-05-31 15:34:51 +000088 aXRef = sqliteMalloc( sizeof(int) * pTab->nCol );
89 if( aXRef==0 ) goto update_cleanup;
90 for(i=0; i<pTab->nCol; i++) aXRef[i] = -1;
91
danielk1977f29ce552002-05-19 23:43:12 +000092 /* If there are FOR EACH ROW triggers, allocate temp tables */
93 if( row_triggers_exist ){
danielk1977c3f9bad2002-05-15 08:30:12 +000094 newIdx = pParse->nTab++;
95 oldIdx = pParse->nTab++;
96 }
97
drh53e3fc72002-07-16 17:22:50 +000098 /* Allocate a cursors for the main database table and for all indices.
99 ** The index cursors might not be used, but if they are used they
100 ** need to occur right after the database cursor. So go ahead and
101 ** allocate enough space, just in case.
102 */
103 base = pParse->nTab++;
104 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
105 pParse->nTab++;
106 }
107
drh967e8b72000-06-21 13:59:10 +0000108 /* Resolve the column names in all the expressions in both the
109 ** WHERE clause and in the new values. Also find the column index
drhed6c8672003-01-12 18:02:16 +0000110 ** for each column to be updated in the pChanges array. For each
111 ** column to be updated, make sure we have authorization to change
112 ** that column.
drhcce7d172000-05-31 15:34:51 +0000113 */
114 if( pWhere ){
drh832508b2002-03-02 17:04:07 +0000115 if( sqliteExprResolveIds(pParse, base, pTabList, 0, pWhere) ){
drhcce7d172000-05-31 15:34:51 +0000116 goto update_cleanup;
117 }
118 if( sqliteExprCheck(pParse, pWhere, 0, 0) ){
119 goto update_cleanup;
120 }
121 }
drh4a324312001-12-21 14:30:42 +0000122 chngRecno = 0;
drhcce7d172000-05-31 15:34:51 +0000123 for(i=0; i<pChanges->nExpr; i++){
drh832508b2002-03-02 17:04:07 +0000124 if( sqliteExprResolveIds(pParse, base, pTabList, 0, pChanges->a[i].pExpr) ){
drhcce7d172000-05-31 15:34:51 +0000125 goto update_cleanup;
126 }
127 if( sqliteExprCheck(pParse, pChanges->a[i].pExpr, 0, 0) ){
128 goto update_cleanup;
129 }
130 for(j=0; j<pTab->nCol; j++){
drh6206d502000-06-19 19:09:08 +0000131 if( sqliteStrICmp(pTab->aCol[j].zName, pChanges->a[i].zName)==0 ){
drh9647ff82002-01-14 02:56:24 +0000132 if( j==pTab->iPKey ){
drh4a324312001-12-21 14:30:42 +0000133 chngRecno = 1;
134 pRecnoExpr = pChanges->a[i].pExpr;
135 }
drhcce7d172000-05-31 15:34:51 +0000136 aXRef[j] = i;
137 break;
138 }
139 }
140 if( j>=pTab->nCol ){
drh967e8b72000-06-21 13:59:10 +0000141 sqliteSetString(&pParse->zErrMsg, "no such column: ",
drhcce7d172000-05-31 15:34:51 +0000142 pChanges->a[i].zName, 0);
143 pParse->nErr++;
144 goto update_cleanup;
145 }
drhed6c8672003-01-12 18:02:16 +0000146#ifndef SQLITE_OMIT_AUTHORIZATION
drhe5f9c642003-01-13 23:27:31 +0000147 {
148 int rc;
149 rc = sqliteAuthCheck(pParse, SQLITE_UPDATE, pTab->zName,
150 pTab->aCol[j].zName);
151 if( rc==SQLITE_DENY ){
152 goto update_cleanup;
153 }else if( rc==SQLITE_IGNORE ){
154 aXRef[j] = -1;
155 }
drhed6c8672003-01-12 18:02:16 +0000156 }
157#endif
drhcce7d172000-05-31 15:34:51 +0000158 }
159
drh5a2c2c22001-11-21 02:21:11 +0000160 /* Allocate memory for the array apIdx[] and fill it with pointers to every
drhcce7d172000-05-31 15:34:51 +0000161 ** index that needs to be updated. Indices only need updating if their
drhc8392582001-12-31 02:48:51 +0000162 ** key includes one of the columns named in pChanges or if the record
163 ** number of the original table entry is changing.
drhcce7d172000-05-31 15:34:51 +0000164 */
drh0ca3e242002-01-29 23:07:02 +0000165 for(nIdx=nIdxTotal=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdxTotal++){
drh4a324312001-12-21 14:30:42 +0000166 if( chngRecno ){
167 i = 0;
168 }else {
169 for(i=0; i<pIdx->nColumn; i++){
170 if( aXRef[pIdx->aiColumn[i]]>=0 ) break;
171 }
drhcce7d172000-05-31 15:34:51 +0000172 }
drh967e8b72000-06-21 13:59:10 +0000173 if( i<pIdx->nColumn ) nIdx++;
drhcce7d172000-05-31 15:34:51 +0000174 }
drh0ca3e242002-01-29 23:07:02 +0000175 if( nIdxTotal>0 ){
176 apIdx = sqliteMalloc( sizeof(Index*) * nIdx + nIdxTotal );
drhb0729502001-03-14 12:35:57 +0000177 if( apIdx==0 ) goto update_cleanup;
drh0ca3e242002-01-29 23:07:02 +0000178 aIdxUsed = (char*)&apIdx[nIdx];
drhb0729502001-03-14 12:35:57 +0000179 }
drh0ca3e242002-01-29 23:07:02 +0000180 for(nIdx=j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
drh4a324312001-12-21 14:30:42 +0000181 if( chngRecno ){
182 i = 0;
183 }else{
184 for(i=0; i<pIdx->nColumn; i++){
185 if( aXRef[pIdx->aiColumn[i]]>=0 ) break;
186 }
drhcce7d172000-05-31 15:34:51 +0000187 }
drh0ca3e242002-01-29 23:07:02 +0000188 if( i<pIdx->nColumn ){
189 apIdx[nIdx++] = pIdx;
190 aIdxUsed[j] = 1;
191 }else{
192 aIdxUsed[j] = 0;
193 }
drhcce7d172000-05-31 15:34:51 +0000194 }
195
196 /* Begin generating code.
197 */
drhd8bc7082000-06-07 23:51:50 +0000198 v = sqliteGetVdbe(pParse);
drhcce7d172000-05-31 15:34:51 +0000199 if( v==0 ) goto update_cleanup;
drhcabb0812002-09-14 13:47:32 +0000200 sqliteBeginWriteOperation(pParse, 1, !row_triggers_exist && pTab->isTemp);
drhcce7d172000-05-31 15:34:51 +0000201
202 /* Begin the database scan
203 */
drhe3184742002-06-19 14:27:05 +0000204 pWInfo = sqliteWhereBegin(pParse, base, pTabList, pWhere, 1, 0);
drhcce7d172000-05-31 15:34:51 +0000205 if( pWInfo==0 ) goto update_cleanup;
206
207 /* Remember the index of every item to be updated.
208 */
drh99fcd712001-10-13 01:06:47 +0000209 sqliteVdbeAddOp(v, OP_ListWrite, 0, 0);
drhcce7d172000-05-31 15:34:51 +0000210
211 /* End the database scan loop.
212 */
213 sqliteWhereEnd(pWInfo);
214
drh1bee3d72001-10-15 00:44:35 +0000215 /* Initialize the count of updated rows
216 */
danielk1977c3f9bad2002-05-15 08:30:12 +0000217 if( db->flags & SQLITE_CountRows && !pParse->trigStack ){
drh1bee3d72001-10-15 00:44:35 +0000218 sqliteVdbeAddOp(v, OP_Integer, 0, 0);
219 }
220
danielk1977f29ce552002-05-19 23:43:12 +0000221 if( row_triggers_exist ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000222 int ii;
223
224 sqliteVdbeAddOp(v, OP_OpenTemp, oldIdx, 0);
225 sqliteVdbeAddOp(v, OP_OpenTemp, newIdx, 0);
226
227 sqliteVdbeAddOp(v, OP_ListRewind, 0, 0);
228 addr = sqliteVdbeAddOp(v, OP_ListRead, 0, 0);
229 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
230
231 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
drh001bbcb2003-03-19 03:14:00 +0000232 sqliteVdbeAddOp(v, OP_Integer, pTab->isTemp, 0);
233 sqliteVdbeAddOp(v, OP_OpenRead, base, pTab->tnum);
danielk1977c3f9bad2002-05-15 08:30:12 +0000234 sqliteVdbeAddOp(v, OP_MoveTo, base, 0);
235
236 sqliteVdbeAddOp(v, OP_Integer, 13, 0);
danielk1977f29ce552002-05-19 23:43:12 +0000237 for(ii = 0; ii < pTab->nCol; ii++){
238 if( ii == pTab->iPKey ){
239 sqliteVdbeAddOp(v, OP_Recno, base, 0);
240 }else{
241 sqliteVdbeAddOp(v, OP_Column, base, ii);
242 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000243 }
244 sqliteVdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0);
245 sqliteVdbeAddOp(v, OP_PutIntKey, oldIdx, 0);
246
247 sqliteVdbeAddOp(v, OP_Integer, 13, 0);
danielk1977f29ce552002-05-19 23:43:12 +0000248 for(ii = 0; ii < pTab->nCol; ii++){
danielk1977c3f9bad2002-05-15 08:30:12 +0000249 if( aXRef[ii] < 0 ){
danielk1977f29ce552002-05-19 23:43:12 +0000250 if( ii == pTab->iPKey ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000251 sqliteVdbeAddOp(v, OP_Recno, base, 0);
danielk1977f29ce552002-05-19 23:43:12 +0000252 }else{
danielk1977c3f9bad2002-05-15 08:30:12 +0000253 sqliteVdbeAddOp(v, OP_Column, base, ii);
danielk1977f29ce552002-05-19 23:43:12 +0000254 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000255 }else{
256 sqliteExprCode(pParse, pChanges->a[aXRef[ii]].pExpr);
257 }
258 }
259 sqliteVdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0);
260 sqliteVdbeAddOp(v, OP_PutIntKey, newIdx, 0);
261 sqliteVdbeAddOp(v, OP_Close, base, 0);
262
263 sqliteVdbeAddOp(v, OP_Rewind, oldIdx, 0);
264 sqliteVdbeAddOp(v, OP_Rewind, newIdx, 0);
265
danielk1977f29ce552002-05-19 23:43:12 +0000266 if( sqliteCodeRowTrigger(pParse, TK_UPDATE, pChanges, TK_BEFORE, pTab,
danielk19776f349032002-06-11 02:25:40 +0000267 newIdx, oldIdx, onError, addr) ){
danielk1977f29ce552002-05-19 23:43:12 +0000268 goto update_cleanup;
269 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000270 }
271
drhcce7d172000-05-31 15:34:51 +0000272 /* Rewind the list of records that need to be updated and
drh0ca3e242002-01-29 23:07:02 +0000273 ** open every index that needs updating. Note that if any
274 ** index could potentially invoke a REPLACE conflict resolution
275 ** action, then we need to open all indices because we might need
276 ** to be deleting some records.
drhcce7d172000-05-31 15:34:51 +0000277 */
drh001bbcb2003-03-19 03:14:00 +0000278 sqliteVdbeAddOp(v, OP_Integer, pTab->isTemp, 0);
279 sqliteVdbeAddOp(v, OP_OpenWrite, base, pTab->tnum);
drh0ca3e242002-01-29 23:07:02 +0000280 if( onError==OE_Replace ){
281 openAll = 1;
282 }else{
283 openAll = 0;
284 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
285 if( pIdx->onError==OE_Replace ){
286 openAll = 1;
287 break;
288 }
289 }
290 }
291 for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
292 if( openAll || aIdxUsed[i] ){
drh001bbcb2003-03-19 03:14:00 +0000293 sqliteVdbeAddOp(v, OP_Integer, pTab->isTemp, 0);
294 sqliteVdbeAddOp(v, OP_OpenWrite, base+i+1, pIdx->tnum);
drh53e3fc72002-07-16 17:22:50 +0000295 assert( pParse->nTab>base+i+1 );
drh0ca3e242002-01-29 23:07:02 +0000296 }
drhcce7d172000-05-31 15:34:51 +0000297 }
298
299 /* Loop over every record that needs updating. We have to load
drh967e8b72000-06-21 13:59:10 +0000300 ** the old data for each record to be updated because some columns
drh1ccde152000-06-17 13:12:39 +0000301 ** might not change and we will need to copy the old value.
drhcce7d172000-05-31 15:34:51 +0000302 ** Also, the old data is needed to delete the old index entires.
drh4a324312001-12-21 14:30:42 +0000303 ** So make the cursor point at the old record.
drhcce7d172000-05-31 15:34:51 +0000304 */
danielk1977f29ce552002-05-19 23:43:12 +0000305 if( !row_triggers_exist ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000306 sqliteVdbeAddOp(v, OP_ListRewind, 0, 0);
307 addr = sqliteVdbeAddOp(v, OP_ListRead, 0, 0);
308 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
309 }
drh07d6e3a2002-05-23 12:50:18 +0000310 sqliteVdbeAddOp(v, OP_NotExists, base, addr);
drhcce7d172000-05-31 15:34:51 +0000311
drh0ca3e242002-01-29 23:07:02 +0000312 /* If the record number will change, push the record number as it
313 ** will be after the update. (The old record number is currently
314 ** on top of the stack.)
drh4a324312001-12-21 14:30:42 +0000315 */
316 if( chngRecno ){
drha9f9d1c2002-06-29 02:20:08 +0000317 sqliteExprCode(pParse, pRecnoExpr);
318 sqliteVdbeAddOp(v, OP_MustBeInt, 0, 0);
drh4a324312001-12-21 14:30:42 +0000319 }
320
drh5a2c2c22001-11-21 02:21:11 +0000321 /* Compute new data for this record.
drhcce7d172000-05-31 15:34:51 +0000322 */
323 for(i=0; i<pTab->nCol; i++){
drh4a324312001-12-21 14:30:42 +0000324 if( i==pTab->iPKey ){
drh0ca3e242002-01-29 23:07:02 +0000325 sqliteVdbeAddOp(v, OP_String, 0, 0);
drh4a324312001-12-21 14:30:42 +0000326 continue;
327 }
drhcce7d172000-05-31 15:34:51 +0000328 j = aXRef[i];
329 if( j<0 ){
drh99fcd712001-10-13 01:06:47 +0000330 sqliteVdbeAddOp(v, OP_Column, base, i);
drhcce7d172000-05-31 15:34:51 +0000331 }else{
332 sqliteExprCode(pParse, pChanges->a[j].pExpr);
333 }
334 }
335
drh0ca3e242002-01-29 23:07:02 +0000336 /* Do constraint checks
337 */
drhb419a922002-01-30 16:17:23 +0000338 sqliteGenerateConstraintChecks(pParse, pTab, base, aIdxUsed, chngRecno, 1,
339 onError, addr);
drh0ca3e242002-01-29 23:07:02 +0000340
341 /* Delete the old indices for the current record.
342 */
drh38640e12002-07-05 21:42:36 +0000343 sqliteGenerateRowIndexDelete(db, v, pTab, base, aIdxUsed);
drh0ca3e242002-01-29 23:07:02 +0000344
drhc8392582001-12-31 02:48:51 +0000345 /* If changing the record number, delete the old record.
drh4a324312001-12-21 14:30:42 +0000346 */
347 if( chngRecno ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000348 sqliteVdbeAddOp(v, OP_Delete, base, 0);
drh4a324312001-12-21 14:30:42 +0000349 }
350
drh0ca3e242002-01-29 23:07:02 +0000351 /* Create the new index entries and the new record.
drhcce7d172000-05-31 15:34:51 +0000352 */
drhb419a922002-01-30 16:17:23 +0000353 sqliteCompleteInsertion(pParse, pTab, base, aIdxUsed, chngRecno, 1);
drhcce7d172000-05-31 15:34:51 +0000354
drh0ca3e242002-01-29 23:07:02 +0000355 /* Increment the row counter
drh1bee3d72001-10-15 00:44:35 +0000356 */
danielk1977c3f9bad2002-05-15 08:30:12 +0000357 if( db->flags & SQLITE_CountRows && !pParse->trigStack){
drh1bee3d72001-10-15 00:44:35 +0000358 sqliteVdbeAddOp(v, OP_AddImm, 1, 0);
359 }
360
danielk1977f29ce552002-05-19 23:43:12 +0000361 if( row_triggers_exist ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000362 for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
363 if( openAll || aIdxUsed[i] )
drh9adf9ac2002-05-15 11:44:13 +0000364 sqliteVdbeAddOp(v, OP_Close, base+i+1, 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000365 }
366 sqliteVdbeAddOp(v, OP_Close, base, 0);
367 pParse->nTab = base;
368
danielk1977f29ce552002-05-19 23:43:12 +0000369 if( sqliteCodeRowTrigger(pParse, TK_UPDATE, pChanges, TK_AFTER, pTab,
danielk19776f349032002-06-11 02:25:40 +0000370 newIdx, oldIdx, onError, addr) ){
danielk1977f29ce552002-05-19 23:43:12 +0000371 goto update_cleanup;
372 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000373 }
374
drhcce7d172000-05-31 15:34:51 +0000375 /* Repeat the above with the next record to be updated, until
376 ** all record selected by the WHERE clause have been updated.
377 */
drh99fcd712001-10-13 01:06:47 +0000378 sqliteVdbeAddOp(v, OP_Goto, 0, addr);
drh0ca3e242002-01-29 23:07:02 +0000379 sqliteVdbeChangeP2(v, addr, sqliteVdbeCurrentAddr(v));
drha8b38d22001-11-01 14:41:34 +0000380 sqliteVdbeAddOp(v, OP_ListReset, 0, 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000381
382 /* Close all tables if there were no FOR EACH ROW triggers */
danielk1977f29ce552002-05-19 23:43:12 +0000383 if( !row_triggers_exist ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000384 for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
385 if( openAll || aIdxUsed[i] ){
drh9adf9ac2002-05-15 11:44:13 +0000386 sqliteVdbeAddOp(v, OP_Close, base+i+1, 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000387 }
388 }
389 sqliteVdbeAddOp(v, OP_Close, base, 0);
390 pParse->nTab = base;
danielk1977f29ce552002-05-19 23:43:12 +0000391 }else{
danielk1977c3f9bad2002-05-15 08:30:12 +0000392 sqliteVdbeAddOp(v, OP_Close, newIdx, 0);
393 sqliteVdbeAddOp(v, OP_Close, oldIdx, 0);
394 }
395
drh1c928532002-01-31 15:54:21 +0000396 sqliteEndWriteOperation(pParse);
drhcce7d172000-05-31 15:34:51 +0000397
drh1bee3d72001-10-15 00:44:35 +0000398 /*
399 ** Return the number of rows that were changed.
400 */
danielk1977c3f9bad2002-05-15 08:30:12 +0000401 if( db->flags & SQLITE_CountRows && !pParse->trigStack ){
drh1bee3d72001-10-15 00:44:35 +0000402 sqliteVdbeAddOp(v, OP_ColumnName, 0, 0);
403 sqliteVdbeChangeP3(v, -1, "rows updated", P3_STATIC);
404 sqliteVdbeAddOp(v, OP_Callback, 1, 0);
405 }
406
drhcce7d172000-05-31 15:34:51 +0000407update_cleanup:
408 sqliteFree(apIdx);
409 sqliteFree(aXRef);
drhad3cab52002-05-24 02:04:32 +0000410 sqliteSrcListDelete(pTabList);
drhcce7d172000-05-31 15:34:51 +0000411 sqliteExprListDelete(pChanges);
412 sqliteExprDelete(pWhere);
413 return;
414}