blob: b77e1bb0ce75e9cd1b22c99aee744fb7674e6597 [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**
drhb2fe7d82003-04-20 17:29:23 +000015** $Id: update.c,v 1.61 2003/04/20 17:29:24 drh Exp $
drhcce7d172000-05-31 15:34:51 +000016*/
17#include "sqliteInt.h"
18
19/*
20** Process an UPDATE statement.
drhb2fe7d82003-04-20 17:29:23 +000021**
22** UPDATE OR IGNORE table_wxyz SET a=b, c=d WHERE e<5 AND f NOT NULL;
23** \_______/ \________/ \______/ \________________/
24* onError pTabList pChanges pWhere
drhcce7d172000-05-31 15:34:51 +000025*/
26void sqliteUpdate(
27 Parse *pParse, /* The parser context */
drh113088e2003-03-20 01:16:58 +000028 SrcList *pTabList, /* The table in which we should change things */
drhcce7d172000-05-31 15:34:51 +000029 ExprList *pChanges, /* Things to be changed */
drh9cfcf5d2002-01-29 18:41:24 +000030 Expr *pWhere, /* The WHERE clause. May be null */
31 int onError /* How to handle constraint errors */
drhcce7d172000-05-31 15:34:51 +000032){
33 int i, j; /* Loop counters */
34 Table *pTab; /* The table to be updated */
drh0ca3e242002-01-29 23:07:02 +000035 int addr; /* VDBE instruction address of the start of the loop */
drhcce7d172000-05-31 15:34:51 +000036 WhereInfo *pWInfo; /* Information about the WHERE clause */
37 Vdbe *v; /* The virtual database engine */
38 Index *pIdx; /* For looping over indices */
39 int nIdx; /* Number of indices that need updating */
drh0ca3e242002-01-29 23:07:02 +000040 int nIdxTotal; /* Total number of indices */
drh4794b982000-06-06 13:54:14 +000041 int base; /* Index of first available table cursor */
drhecdc7532001-09-23 02:35:53 +000042 sqlite *db; /* The database structure */
drhcce7d172000-05-31 15:34:51 +000043 Index **apIdx = 0; /* An array of indices that need updating too */
drhb2fe7d82003-04-20 17:29:23 +000044 char *aIdxUsed = 0; /* aIdxUsed[i]==1 if the i-th index is used */
drhcce7d172000-05-31 15:34:51 +000045 int *aXRef = 0; /* aXRef[i] is the index in pChanges->a[] of the
drh967e8b72000-06-21 13:59:10 +000046 ** an expression for the i-th column of the table.
47 ** aXRef[i]==-1 if the i-th column is not changed. */
drh4a324312001-12-21 14:30:42 +000048 int chngRecno; /* True if the record number is being changed */
49 Expr *pRecnoExpr; /* Expression defining the new record number */
drh0ca3e242002-01-29 23:07:02 +000050 int openAll; /* True if all indices need to be opened */
drhcce7d172000-05-31 15:34:51 +000051
drh70ce3f02003-04-15 19:22:22 +000052 int before_triggers; /* True if there are any BEFORE triggers */
53 int after_triggers; /* True if there are any AFTER triggers */
54 int row_triggers_exist = 0; /* True if any row triggers exist */
danielk1977c3f9bad2002-05-15 08:30:12 +000055
56 int newIdx = -1; /* index of trigger "new" temp table */
57 int oldIdx = -1; /* index of trigger "old" temp table */
58
drhdaffd0e2001-04-11 14:28:42 +000059 if( pParse->nErr || sqlite_malloc_failed ) goto update_cleanup;
drhecdc7532001-09-23 02:35:53 +000060 db = pParse->db;
drh113088e2003-03-20 01:16:58 +000061 assert( pTabList->nSrc==1 );
drhdaffd0e2001-04-11 14:28:42 +000062
drhb2fe7d82003-04-20 17:29:23 +000063 /* Locate the table which we want to update.
drhcce7d172000-05-31 15:34:51 +000064 */
drh812d7a22003-03-27 13:50:00 +000065 pTab = sqliteSrcListLookup(pParse, pTabList);
drha69d9162003-04-17 22:57:53 +000066 if( pTab==0 ) goto update_cleanup;
67 before_triggers = sqliteTriggersExist(pParse, pTab->pTrigger,
68 TK_UPDATE, TK_BEFORE, TK_ROW, pChanges);
69 after_triggers = sqliteTriggersExist(pParse, pTab->pTrigger,
70 TK_UPDATE, TK_AFTER, TK_ROW, pChanges);
71 row_triggers_exist = before_triggers || after_triggers;
72 if( row_triggers_exist && pTab->pSelect ){
73 /* Just fire VIEW triggers */
74 sqliteSrcListDelete(pTabList);
75 sqliteViewTriggers(pParse, pTab, pWhere, onError, pChanges);
76 return;
77 }
78 if( sqliteIsReadOnly(pParse, pTab) ) goto update_cleanup;
drh417be792002-03-03 18:59:40 +000079 assert( pTab->pSelect==0 ); /* This table is not a VIEW */
drhcce7d172000-05-31 15:34:51 +000080 aXRef = sqliteMalloc( sizeof(int) * pTab->nCol );
81 if( aXRef==0 ) goto update_cleanup;
82 for(i=0; i<pTab->nCol; i++) aXRef[i] = -1;
83
drhb2fe7d82003-04-20 17:29:23 +000084 /* If there are FOR EACH ROW triggers, allocate cursors for the
85 ** special OLD and NEW tables
86 */
danielk1977f29ce552002-05-19 23:43:12 +000087 if( row_triggers_exist ){
danielk1977c3f9bad2002-05-15 08:30:12 +000088 newIdx = pParse->nTab++;
89 oldIdx = pParse->nTab++;
90 }
91
drh53e3fc72002-07-16 17:22:50 +000092 /* Allocate a cursors for the main database table and for all indices.
93 ** The index cursors might not be used, but if they are used they
94 ** need to occur right after the database cursor. So go ahead and
95 ** allocate enough space, just in case.
96 */
97 base = pParse->nTab++;
98 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
99 pParse->nTab++;
100 }
101
drh967e8b72000-06-21 13:59:10 +0000102 /* Resolve the column names in all the expressions in both the
103 ** WHERE clause and in the new values. Also find the column index
drhed6c8672003-01-12 18:02:16 +0000104 ** for each column to be updated in the pChanges array. For each
105 ** column to be updated, make sure we have authorization to change
106 ** that column.
drhcce7d172000-05-31 15:34:51 +0000107 */
108 if( pWhere ){
drh832508b2002-03-02 17:04:07 +0000109 if( sqliteExprResolveIds(pParse, base, pTabList, 0, pWhere) ){
drhcce7d172000-05-31 15:34:51 +0000110 goto update_cleanup;
111 }
112 if( sqliteExprCheck(pParse, pWhere, 0, 0) ){
113 goto update_cleanup;
114 }
115 }
drh4a324312001-12-21 14:30:42 +0000116 chngRecno = 0;
drhcce7d172000-05-31 15:34:51 +0000117 for(i=0; i<pChanges->nExpr; i++){
drh832508b2002-03-02 17:04:07 +0000118 if( sqliteExprResolveIds(pParse, base, pTabList, 0, pChanges->a[i].pExpr) ){
drhcce7d172000-05-31 15:34:51 +0000119 goto update_cleanup;
120 }
121 if( sqliteExprCheck(pParse, pChanges->a[i].pExpr, 0, 0) ){
122 goto update_cleanup;
123 }
124 for(j=0; j<pTab->nCol; j++){
drh6206d502000-06-19 19:09:08 +0000125 if( sqliteStrICmp(pTab->aCol[j].zName, pChanges->a[i].zName)==0 ){
drh9647ff82002-01-14 02:56:24 +0000126 if( j==pTab->iPKey ){
drh4a324312001-12-21 14:30:42 +0000127 chngRecno = 1;
128 pRecnoExpr = pChanges->a[i].pExpr;
129 }
drhcce7d172000-05-31 15:34:51 +0000130 aXRef[j] = i;
131 break;
132 }
133 }
134 if( j>=pTab->nCol ){
drhda93d232003-03-31 02:12:46 +0000135 sqliteErrorMsg(pParse, "no such column: %s", pChanges->a[i].zName);
drhcce7d172000-05-31 15:34:51 +0000136 goto update_cleanup;
137 }
drhed6c8672003-01-12 18:02:16 +0000138#ifndef SQLITE_OMIT_AUTHORIZATION
drhe5f9c642003-01-13 23:27:31 +0000139 {
140 int rc;
141 rc = sqliteAuthCheck(pParse, SQLITE_UPDATE, pTab->zName,
142 pTab->aCol[j].zName);
143 if( rc==SQLITE_DENY ){
144 goto update_cleanup;
145 }else if( rc==SQLITE_IGNORE ){
146 aXRef[j] = -1;
147 }
drhed6c8672003-01-12 18:02:16 +0000148 }
149#endif
drhcce7d172000-05-31 15:34:51 +0000150 }
151
drh5a2c2c22001-11-21 02:21:11 +0000152 /* Allocate memory for the array apIdx[] and fill it with pointers to every
drhcce7d172000-05-31 15:34:51 +0000153 ** index that needs to be updated. Indices only need updating if their
drhc8392582001-12-31 02:48:51 +0000154 ** key includes one of the columns named in pChanges or if the record
155 ** number of the original table entry is changing.
drhcce7d172000-05-31 15:34:51 +0000156 */
drh0ca3e242002-01-29 23:07:02 +0000157 for(nIdx=nIdxTotal=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdxTotal++){
drh4a324312001-12-21 14:30:42 +0000158 if( chngRecno ){
159 i = 0;
160 }else {
161 for(i=0; i<pIdx->nColumn; i++){
162 if( aXRef[pIdx->aiColumn[i]]>=0 ) break;
163 }
drhcce7d172000-05-31 15:34:51 +0000164 }
drh967e8b72000-06-21 13:59:10 +0000165 if( i<pIdx->nColumn ) nIdx++;
drhcce7d172000-05-31 15:34:51 +0000166 }
drh0ca3e242002-01-29 23:07:02 +0000167 if( nIdxTotal>0 ){
168 apIdx = sqliteMalloc( sizeof(Index*) * nIdx + nIdxTotal );
drhb0729502001-03-14 12:35:57 +0000169 if( apIdx==0 ) goto update_cleanup;
drh0ca3e242002-01-29 23:07:02 +0000170 aIdxUsed = (char*)&apIdx[nIdx];
drhb0729502001-03-14 12:35:57 +0000171 }
drh0ca3e242002-01-29 23:07:02 +0000172 for(nIdx=j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
drh4a324312001-12-21 14:30:42 +0000173 if( chngRecno ){
174 i = 0;
175 }else{
176 for(i=0; i<pIdx->nColumn; i++){
177 if( aXRef[pIdx->aiColumn[i]]>=0 ) break;
178 }
drhcce7d172000-05-31 15:34:51 +0000179 }
drh0ca3e242002-01-29 23:07:02 +0000180 if( i<pIdx->nColumn ){
181 apIdx[nIdx++] = pIdx;
182 aIdxUsed[j] = 1;
183 }else{
184 aIdxUsed[j] = 0;
185 }
drhcce7d172000-05-31 15:34:51 +0000186 }
187
188 /* Begin generating code.
189 */
drhd8bc7082000-06-07 23:51:50 +0000190 v = sqliteGetVdbe(pParse);
drhcce7d172000-05-31 15:34:51 +0000191 if( v==0 ) goto update_cleanup;
drhd24cc422003-03-27 12:51:24 +0000192 sqliteBeginWriteOperation(pParse, 1, !row_triggers_exist && pTab->iDb==1);
drhcce7d172000-05-31 15:34:51 +0000193
194 /* Begin the database scan
195 */
drhe3184742002-06-19 14:27:05 +0000196 pWInfo = sqliteWhereBegin(pParse, base, pTabList, pWhere, 1, 0);
drhcce7d172000-05-31 15:34:51 +0000197 if( pWInfo==0 ) goto update_cleanup;
198
199 /* Remember the index of every item to be updated.
200 */
drh99fcd712001-10-13 01:06:47 +0000201 sqliteVdbeAddOp(v, OP_ListWrite, 0, 0);
drhcce7d172000-05-31 15:34:51 +0000202
203 /* End the database scan loop.
204 */
205 sqliteWhereEnd(pWInfo);
206
drh1bee3d72001-10-15 00:44:35 +0000207 /* Initialize the count of updated rows
208 */
danielk1977c3f9bad2002-05-15 08:30:12 +0000209 if( db->flags & SQLITE_CountRows && !pParse->trigStack ){
drh1bee3d72001-10-15 00:44:35 +0000210 sqliteVdbeAddOp(v, OP_Integer, 0, 0);
211 }
212
danielk1977f29ce552002-05-19 23:43:12 +0000213 if( row_triggers_exist ){
drh70ce3f02003-04-15 19:22:22 +0000214 /* Create pseudo-tables for NEW and OLD
215 */
216 sqliteVdbeAddOp(v, OP_OpenPseudo, oldIdx, 0);
217 sqliteVdbeAddOp(v, OP_OpenPseudo, newIdx, 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000218
drh70ce3f02003-04-15 19:22:22 +0000219 /* The top of the update loop for when there are triggers.
220 */
danielk1977c3f9bad2002-05-15 08:30:12 +0000221 sqliteVdbeAddOp(v, OP_ListRewind, 0, 0);
222 addr = sqliteVdbeAddOp(v, OP_ListRead, 0, 0);
223 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
224
drh70ce3f02003-04-15 19:22:22 +0000225 /* Open a cursor and make it point to the record that is
226 ** being updated.
227 */
danielk1977c3f9bad2002-05-15 08:30:12 +0000228 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
drhd24cc422003-03-27 12:51:24 +0000229 sqliteVdbeAddOp(v, OP_Integer, pTab->iDb, 0);
drh001bbcb2003-03-19 03:14:00 +0000230 sqliteVdbeAddOp(v, OP_OpenRead, base, pTab->tnum);
danielk1977c3f9bad2002-05-15 08:30:12 +0000231 sqliteVdbeAddOp(v, OP_MoveTo, base, 0);
232
drh70ce3f02003-04-15 19:22:22 +0000233 /* Generate the OLD table
234 */
235 sqliteVdbeAddOp(v, OP_Recno, base, 0);
236 sqliteVdbeAddOp(v, OP_RowData, base, 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000237 sqliteVdbeAddOp(v, OP_PutIntKey, oldIdx, 0);
238
drh70ce3f02003-04-15 19:22:22 +0000239 /* Generate the NEW table
240 */
241 if( chngRecno ){
242 sqliteExprCode(pParse, pRecnoExpr);
243 }else{
244 sqliteVdbeAddOp(v, OP_Recno, base, 0);
245 }
246 for(i=0; i<pTab->nCol; i++){
247 if( i==pTab->iPKey ){
248 sqliteVdbeAddOp(v, OP_String, 0, 0);
249 continue;
250 }
251 j = aXRef[i];
252 if( j<0 ){
253 sqliteVdbeAddOp(v, OP_Column, base, i);
danielk1977c3f9bad2002-05-15 08:30:12 +0000254 }else{
drh70ce3f02003-04-15 19:22:22 +0000255 sqliteExprCode(pParse, pChanges->a[j].pExpr);
danielk1977c3f9bad2002-05-15 08:30:12 +0000256 }
257 }
258 sqliteVdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0);
259 sqliteVdbeAddOp(v, OP_PutIntKey, newIdx, 0);
260 sqliteVdbeAddOp(v, OP_Close, base, 0);
261
drh70ce3f02003-04-15 19:22:22 +0000262 /* Fire the BEFORE triggers
263 */
danielk1977f29ce552002-05-19 23:43:12 +0000264 if( sqliteCodeRowTrigger(pParse, TK_UPDATE, pChanges, TK_BEFORE, pTab,
danielk19776f349032002-06-11 02:25:40 +0000265 newIdx, oldIdx, onError, addr) ){
danielk1977f29ce552002-05-19 23:43:12 +0000266 goto update_cleanup;
267 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000268 }
269
drhcce7d172000-05-31 15:34:51 +0000270 /* Rewind the list of records that need to be updated and
drh0ca3e242002-01-29 23:07:02 +0000271 ** open every index that needs updating. Note that if any
272 ** index could potentially invoke a REPLACE conflict resolution
273 ** action, then we need to open all indices because we might need
274 ** to be deleting some records.
drhcce7d172000-05-31 15:34:51 +0000275 */
drhd24cc422003-03-27 12:51:24 +0000276 sqliteVdbeAddOp(v, OP_Integer, pTab->iDb, 0);
drh001bbcb2003-03-19 03:14:00 +0000277 sqliteVdbeAddOp(v, OP_OpenWrite, base, pTab->tnum);
drh0ca3e242002-01-29 23:07:02 +0000278 if( onError==OE_Replace ){
279 openAll = 1;
280 }else{
281 openAll = 0;
282 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
283 if( pIdx->onError==OE_Replace ){
284 openAll = 1;
285 break;
286 }
287 }
288 }
289 for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
290 if( openAll || aIdxUsed[i] ){
drhd24cc422003-03-27 12:51:24 +0000291 sqliteVdbeAddOp(v, OP_Integer, pIdx->iDb, 0);
drh001bbcb2003-03-19 03:14:00 +0000292 sqliteVdbeAddOp(v, OP_OpenWrite, base+i+1, pIdx->tnum);
drh53e3fc72002-07-16 17:22:50 +0000293 assert( pParse->nTab>base+i+1 );
drh0ca3e242002-01-29 23:07:02 +0000294 }
drhcce7d172000-05-31 15:34:51 +0000295 }
296
297 /* Loop over every record that needs updating. We have to load
drh967e8b72000-06-21 13:59:10 +0000298 ** the old data for each record to be updated because some columns
drh1ccde152000-06-17 13:12:39 +0000299 ** might not change and we will need to copy the old value.
drhcce7d172000-05-31 15:34:51 +0000300 ** Also, the old data is needed to delete the old index entires.
drh4a324312001-12-21 14:30:42 +0000301 ** So make the cursor point at the old record.
drhcce7d172000-05-31 15:34:51 +0000302 */
danielk1977f29ce552002-05-19 23:43:12 +0000303 if( !row_triggers_exist ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000304 sqliteVdbeAddOp(v, OP_ListRewind, 0, 0);
305 addr = sqliteVdbeAddOp(v, OP_ListRead, 0, 0);
306 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
307 }
drh07d6e3a2002-05-23 12:50:18 +0000308 sqliteVdbeAddOp(v, OP_NotExists, base, addr);
drhcce7d172000-05-31 15:34:51 +0000309
drh0ca3e242002-01-29 23:07:02 +0000310 /* If the record number will change, push the record number as it
311 ** will be after the update. (The old record number is currently
312 ** on top of the stack.)
drh4a324312001-12-21 14:30:42 +0000313 */
314 if( chngRecno ){
drha9f9d1c2002-06-29 02:20:08 +0000315 sqliteExprCode(pParse, pRecnoExpr);
316 sqliteVdbeAddOp(v, OP_MustBeInt, 0, 0);
drh4a324312001-12-21 14:30:42 +0000317 }
318
drh5a2c2c22001-11-21 02:21:11 +0000319 /* Compute new data for this record.
drhcce7d172000-05-31 15:34:51 +0000320 */
321 for(i=0; i<pTab->nCol; i++){
drh4a324312001-12-21 14:30:42 +0000322 if( i==pTab->iPKey ){
drh0ca3e242002-01-29 23:07:02 +0000323 sqliteVdbeAddOp(v, OP_String, 0, 0);
drh4a324312001-12-21 14:30:42 +0000324 continue;
325 }
drhcce7d172000-05-31 15:34:51 +0000326 j = aXRef[i];
327 if( j<0 ){
drh99fcd712001-10-13 01:06:47 +0000328 sqliteVdbeAddOp(v, OP_Column, base, i);
drhcce7d172000-05-31 15:34:51 +0000329 }else{
330 sqliteExprCode(pParse, pChanges->a[j].pExpr);
331 }
332 }
333
drh0ca3e242002-01-29 23:07:02 +0000334 /* Do constraint checks
335 */
drhb419a922002-01-30 16:17:23 +0000336 sqliteGenerateConstraintChecks(pParse, pTab, base, aIdxUsed, chngRecno, 1,
337 onError, addr);
drh0ca3e242002-01-29 23:07:02 +0000338
339 /* Delete the old indices for the current record.
340 */
drh38640e12002-07-05 21:42:36 +0000341 sqliteGenerateRowIndexDelete(db, v, pTab, base, aIdxUsed);
drh0ca3e242002-01-29 23:07:02 +0000342
drhc8392582001-12-31 02:48:51 +0000343 /* If changing the record number, delete the old record.
drh4a324312001-12-21 14:30:42 +0000344 */
345 if( chngRecno ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000346 sqliteVdbeAddOp(v, OP_Delete, base, 0);
drh4a324312001-12-21 14:30:42 +0000347 }
348
drh0ca3e242002-01-29 23:07:02 +0000349 /* Create the new index entries and the new record.
drhcce7d172000-05-31 15:34:51 +0000350 */
drh70ce3f02003-04-15 19:22:22 +0000351 sqliteCompleteInsertion(pParse, pTab, base, aIdxUsed, chngRecno, 1, -1);
drhcce7d172000-05-31 15:34:51 +0000352
drh0ca3e242002-01-29 23:07:02 +0000353 /* Increment the row counter
drh1bee3d72001-10-15 00:44:35 +0000354 */
danielk1977c3f9bad2002-05-15 08:30:12 +0000355 if( db->flags & SQLITE_CountRows && !pParse->trigStack){
drh1bee3d72001-10-15 00:44:35 +0000356 sqliteVdbeAddOp(v, OP_AddImm, 1, 0);
357 }
358
danielk1977f29ce552002-05-19 23:43:12 +0000359 if( row_triggers_exist ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000360 for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
361 if( openAll || aIdxUsed[i] )
drh9adf9ac2002-05-15 11:44:13 +0000362 sqliteVdbeAddOp(v, OP_Close, base+i+1, 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000363 }
364 sqliteVdbeAddOp(v, OP_Close, base, 0);
365 pParse->nTab = base;
366
danielk1977f29ce552002-05-19 23:43:12 +0000367 if( sqliteCodeRowTrigger(pParse, TK_UPDATE, pChanges, TK_AFTER, pTab,
danielk19776f349032002-06-11 02:25:40 +0000368 newIdx, oldIdx, onError, addr) ){
danielk1977f29ce552002-05-19 23:43:12 +0000369 goto update_cleanup;
370 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000371 }
372
drhcce7d172000-05-31 15:34:51 +0000373 /* Repeat the above with the next record to be updated, until
374 ** all record selected by the WHERE clause have been updated.
375 */
drh99fcd712001-10-13 01:06:47 +0000376 sqliteVdbeAddOp(v, OP_Goto, 0, addr);
drh0ca3e242002-01-29 23:07:02 +0000377 sqliteVdbeChangeP2(v, addr, sqliteVdbeCurrentAddr(v));
drha8b38d22001-11-01 14:41:34 +0000378 sqliteVdbeAddOp(v, OP_ListReset, 0, 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000379
380 /* Close all tables if there were no FOR EACH ROW triggers */
danielk1977f29ce552002-05-19 23:43:12 +0000381 if( !row_triggers_exist ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000382 for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
383 if( openAll || aIdxUsed[i] ){
drh9adf9ac2002-05-15 11:44:13 +0000384 sqliteVdbeAddOp(v, OP_Close, base+i+1, 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000385 }
386 }
387 sqliteVdbeAddOp(v, OP_Close, base, 0);
388 pParse->nTab = base;
danielk1977f29ce552002-05-19 23:43:12 +0000389 }else{
danielk1977c3f9bad2002-05-15 08:30:12 +0000390 sqliteVdbeAddOp(v, OP_Close, newIdx, 0);
391 sqliteVdbeAddOp(v, OP_Close, oldIdx, 0);
392 }
393
drh1c928532002-01-31 15:54:21 +0000394 sqliteEndWriteOperation(pParse);
drhcce7d172000-05-31 15:34:51 +0000395
drh1bee3d72001-10-15 00:44:35 +0000396 /*
397 ** Return the number of rows that were changed.
398 */
danielk1977c3f9bad2002-05-15 08:30:12 +0000399 if( db->flags & SQLITE_CountRows && !pParse->trigStack ){
drh1bee3d72001-10-15 00:44:35 +0000400 sqliteVdbeAddOp(v, OP_ColumnName, 0, 0);
401 sqliteVdbeChangeP3(v, -1, "rows updated", P3_STATIC);
402 sqliteVdbeAddOp(v, OP_Callback, 1, 0);
403 }
404
drhcce7d172000-05-31 15:34:51 +0000405update_cleanup:
406 sqliteFree(apIdx);
407 sqliteFree(aXRef);
drhad3cab52002-05-24 02:04:32 +0000408 sqliteSrcListDelete(pTabList);
drhcce7d172000-05-31 15:34:51 +0000409 sqliteExprListDelete(pChanges);
410 sqliteExprDelete(pWhere);
411 return;
412}