blob: 60c049d68a0697d827d552cf96d7f3cf4d68883a [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**
drh9bb575f2004-09-06 17:24:11 +000015** $Id: update.c,v 1.88 2004/09/06 17:24:13 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*/
danielk19774adee202004-05-08 08:23:19 +000026void sqlite3Update(
drhcce7d172000-05-31 15:34:51 +000027 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 */
danielk1977742f9472004-06-16 12:02:43 +000035 int addr = 0; /* 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 */
drh6a3ea0e2003-05-02 14:32:12 +000041 int iCur; /* VDBE Cursor number of pTab */
drh9bb575f2004-09-06 17:24:11 +000042 sqlite3 *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 */
danielk1977742f9472004-06-16 12:02:43 +000049 Expr *pRecnoExpr = 0; /* Expression defining the new record number */
50 int openAll = 0; /* True if all indices need to be opened */
drh5cf590c2003-04-24 01:45:04 +000051 int isView; /* Trying to update a view */
drh85e20962003-04-25 17:52:11 +000052 AuthContext sContext; /* The authorization context */
drhcce7d172000-05-31 15:34:51 +000053
drh70ce3f02003-04-15 19:22:22 +000054 int before_triggers; /* True if there are any BEFORE triggers */
55 int after_triggers; /* True if there are any AFTER triggers */
56 int row_triggers_exist = 0; /* True if any row triggers exist */
danielk1977c3f9bad2002-05-15 08:30:12 +000057
58 int newIdx = -1; /* index of trigger "new" temp table */
59 int oldIdx = -1; /* index of trigger "old" temp table */
60
drh85e20962003-04-25 17:52:11 +000061 sContext.pParse = 0;
danielk19776f8a5032004-05-10 10:34:51 +000062 if( pParse->nErr || sqlite3_malloc_failed ) goto update_cleanup;
drhecdc7532001-09-23 02:35:53 +000063 db = pParse->db;
drh113088e2003-03-20 01:16:58 +000064 assert( pTabList->nSrc==1 );
drhdaffd0e2001-04-11 14:28:42 +000065
drhb2fe7d82003-04-20 17:29:23 +000066 /* Locate the table which we want to update.
drhcce7d172000-05-31 15:34:51 +000067 */
danielk19774adee202004-05-08 08:23:19 +000068 pTab = sqlite3SrcListLookup(pParse, pTabList);
drha69d9162003-04-17 22:57:53 +000069 if( pTab==0 ) goto update_cleanup;
danielk19774adee202004-05-08 08:23:19 +000070 before_triggers = sqlite3TriggersExist(pParse, pTab->pTrigger,
drha69d9162003-04-17 22:57:53 +000071 TK_UPDATE, TK_BEFORE, TK_ROW, pChanges);
danielk19774adee202004-05-08 08:23:19 +000072 after_triggers = sqlite3TriggersExist(pParse, pTab->pTrigger,
drha69d9162003-04-17 22:57:53 +000073 TK_UPDATE, TK_AFTER, TK_ROW, pChanges);
74 row_triggers_exist = before_triggers || after_triggers;
drh5cf590c2003-04-24 01:45:04 +000075 isView = pTab->pSelect!=0;
danielk19774adee202004-05-08 08:23:19 +000076 if( sqlite3IsReadOnly(pParse, pTab, before_triggers) ){
drh5cf590c2003-04-24 01:45:04 +000077 goto update_cleanup;
drha69d9162003-04-17 22:57:53 +000078 }
drh85e20962003-04-25 17:52:11 +000079 if( isView ){
danielk19774adee202004-05-08 08:23:19 +000080 if( sqlite3ViewGetColumnNames(pParse, pTab) ){
drh85e20962003-04-25 17:52:11 +000081 goto update_cleanup;
82 }
drh5cf590c2003-04-24 01:45:04 +000083 }
drheafe05b2004-06-13 00:54:01 +000084 aXRef = sqliteMallocRaw( sizeof(int) * pTab->nCol );
drhcce7d172000-05-31 15:34:51 +000085 if( aXRef==0 ) goto update_cleanup;
86 for(i=0; i<pTab->nCol; i++) aXRef[i] = -1;
87
drhb2fe7d82003-04-20 17:29:23 +000088 /* If there are FOR EACH ROW triggers, allocate cursors for the
89 ** special OLD and NEW tables
90 */
danielk1977f29ce552002-05-19 23:43:12 +000091 if( row_triggers_exist ){
danielk1977c3f9bad2002-05-15 08:30:12 +000092 newIdx = pParse->nTab++;
93 oldIdx = pParse->nTab++;
94 }
95
drh53e3fc72002-07-16 17:22:50 +000096 /* Allocate a cursors for the main database table and for all indices.
97 ** The index cursors might not be used, but if they are used they
98 ** need to occur right after the database cursor. So go ahead and
99 ** allocate enough space, just in case.
100 */
drh6a3ea0e2003-05-02 14:32:12 +0000101 pTabList->a[0].iCursor = iCur = pParse->nTab++;
drh53e3fc72002-07-16 17:22:50 +0000102 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
103 pParse->nTab++;
104 }
105
drh85e20962003-04-25 17:52:11 +0000106 /* Resolve the column names in all the expressions of the
107 ** of the UPDATE statement. Also find the column index
drhed6c8672003-01-12 18:02:16 +0000108 ** for each column to be updated in the pChanges array. For each
109 ** column to be updated, make sure we have authorization to change
110 ** that column.
drhcce7d172000-05-31 15:34:51 +0000111 */
drh4a324312001-12-21 14:30:42 +0000112 chngRecno = 0;
drhcce7d172000-05-31 15:34:51 +0000113 for(i=0; i<pChanges->nExpr; i++){
drh290c1942004-08-21 17:54:45 +0000114 if( sqlite3ExprResolveAndCheck(pParse, pTabList, 0,
115 pChanges->a[i].pExpr, 0, 0) ){
drhcce7d172000-05-31 15:34:51 +0000116 goto update_cleanup;
117 }
118 for(j=0; j<pTab->nCol; j++){
danielk19774adee202004-05-08 08:23:19 +0000119 if( sqlite3StrICmp(pTab->aCol[j].zName, pChanges->a[i].zName)==0 ){
drh9647ff82002-01-14 02:56:24 +0000120 if( j==pTab->iPKey ){
drh4a324312001-12-21 14:30:42 +0000121 chngRecno = 1;
122 pRecnoExpr = pChanges->a[i].pExpr;
123 }
drhcce7d172000-05-31 15:34:51 +0000124 aXRef[j] = i;
125 break;
126 }
127 }
128 if( j>=pTab->nCol ){
danielk19774adee202004-05-08 08:23:19 +0000129 if( sqlite3IsRowid(pChanges->a[i].zName) ){
drha0217ba2003-06-01 01:10:33 +0000130 chngRecno = 1;
131 pRecnoExpr = pChanges->a[i].pExpr;
132 }else{
danielk19774adee202004-05-08 08:23:19 +0000133 sqlite3ErrorMsg(pParse, "no such column: %s", pChanges->a[i].zName);
drha0217ba2003-06-01 01:10:33 +0000134 goto update_cleanup;
135 }
drhcce7d172000-05-31 15:34:51 +0000136 }
drhed6c8672003-01-12 18:02:16 +0000137#ifndef SQLITE_OMIT_AUTHORIZATION
drhe5f9c642003-01-13 23:27:31 +0000138 {
139 int rc;
danielk19774adee202004-05-08 08:23:19 +0000140 rc = sqlite3AuthCheck(pParse, SQLITE_UPDATE, pTab->zName,
drhe22a3342003-04-22 20:30:37 +0000141 pTab->aCol[j].zName, db->aDb[pTab->iDb].zName);
drhe5f9c642003-01-13 23:27:31 +0000142 if( rc==SQLITE_DENY ){
143 goto update_cleanup;
144 }else if( rc==SQLITE_IGNORE ){
145 aXRef[j] = -1;
146 }
drhed6c8672003-01-12 18:02:16 +0000147 }
148#endif
drhcce7d172000-05-31 15:34:51 +0000149 }
150
drh5a2c2c22001-11-21 02:21:11 +0000151 /* Allocate memory for the array apIdx[] and fill it with pointers to every
drhcce7d172000-05-31 15:34:51 +0000152 ** index that needs to be updated. Indices only need updating if their
drhc8392582001-12-31 02:48:51 +0000153 ** key includes one of the columns named in pChanges or if the record
154 ** number of the original table entry is changing.
drhcce7d172000-05-31 15:34:51 +0000155 */
drh0ca3e242002-01-29 23:07:02 +0000156 for(nIdx=nIdxTotal=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdxTotal++){
drh4a324312001-12-21 14:30:42 +0000157 if( chngRecno ){
158 i = 0;
159 }else {
160 for(i=0; i<pIdx->nColumn; i++){
161 if( aXRef[pIdx->aiColumn[i]]>=0 ) break;
162 }
drhcce7d172000-05-31 15:34:51 +0000163 }
drh967e8b72000-06-21 13:59:10 +0000164 if( i<pIdx->nColumn ) nIdx++;
drhcce7d172000-05-31 15:34:51 +0000165 }
drh0ca3e242002-01-29 23:07:02 +0000166 if( nIdxTotal>0 ){
drheafe05b2004-06-13 00:54:01 +0000167 apIdx = sqliteMallocRaw( sizeof(Index*) * nIdx + nIdxTotal );
drhb0729502001-03-14 12:35:57 +0000168 if( apIdx==0 ) goto update_cleanup;
drh0ca3e242002-01-29 23:07:02 +0000169 aIdxUsed = (char*)&apIdx[nIdx];
drhb0729502001-03-14 12:35:57 +0000170 }
drh0ca3e242002-01-29 23:07:02 +0000171 for(nIdx=j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
drh4a324312001-12-21 14:30:42 +0000172 if( chngRecno ){
173 i = 0;
174 }else{
175 for(i=0; i<pIdx->nColumn; i++){
176 if( aXRef[pIdx->aiColumn[i]]>=0 ) break;
177 }
drhcce7d172000-05-31 15:34:51 +0000178 }
drh0ca3e242002-01-29 23:07:02 +0000179 if( i<pIdx->nColumn ){
danielk19777cedc8d2004-06-10 10:50:08 +0000180 if( sqlite3CheckIndexCollSeq(pParse, pIdx) ) goto update_cleanup;
drh0ca3e242002-01-29 23:07:02 +0000181 apIdx[nIdx++] = pIdx;
182 aIdxUsed[j] = 1;
183 }else{
184 aIdxUsed[j] = 0;
185 }
drhcce7d172000-05-31 15:34:51 +0000186 }
187
drh85e20962003-04-25 17:52:11 +0000188 /* Resolve the column names in all the expressions in the
189 ** WHERE clause.
190 */
drh290c1942004-08-21 17:54:45 +0000191 if( sqlite3ExprResolveAndCheck(pParse, pTabList, 0, pWhere, 0, 0) ){
192 goto update_cleanup;
drh85e20962003-04-25 17:52:11 +0000193 }
194
195 /* Start the view context
196 */
197 if( isView ){
danielk19774adee202004-05-08 08:23:19 +0000198 sqlite3AuthContextPush(pParse, &sContext, pTab->zName);
drh85e20962003-04-25 17:52:11 +0000199 }
200
drhcce7d172000-05-31 15:34:51 +0000201 /* Begin generating code.
202 */
danielk19774adee202004-05-08 08:23:19 +0000203 v = sqlite3GetVdbe(pParse);
drhcce7d172000-05-31 15:34:51 +0000204 if( v==0 ) goto update_cleanup;
danielk1977b28af712004-06-21 06:50:26 +0000205 sqlite3VdbeCountChanges(v);
danielk19774adee202004-05-08 08:23:19 +0000206 sqlite3BeginWriteOperation(pParse, 1, pTab->iDb);
drhcce7d172000-05-31 15:34:51 +0000207
drh5cf590c2003-04-24 01:45:04 +0000208 /* If we are trying to update a view, construct that view into
209 ** a temporary table.
210 */
211 if( isView ){
drh85e20962003-04-25 17:52:11 +0000212 Select *pView;
danielk19774adee202004-05-08 08:23:19 +0000213 pView = sqlite3SelectDup(pTab->pSelect);
danielk197784ac9d02004-05-18 09:58:06 +0000214 sqlite3Select(pParse, pView, SRT_TempTable, iCur, 0, 0, 0, 0);
danielk19774adee202004-05-08 08:23:19 +0000215 sqlite3SelectDelete(pView);
drh5cf590c2003-04-24 01:45:04 +0000216 }
217
drhcce7d172000-05-31 15:34:51 +0000218 /* Begin the database scan
219 */
danielk19774adee202004-05-08 08:23:19 +0000220 pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, 1, 0);
drhcce7d172000-05-31 15:34:51 +0000221 if( pWInfo==0 ) goto update_cleanup;
222
223 /* Remember the index of every item to be updated.
224 */
danielk19774adee202004-05-08 08:23:19 +0000225 sqlite3VdbeAddOp(v, OP_ListWrite, 0, 0);
drhcce7d172000-05-31 15:34:51 +0000226
227 /* End the database scan loop.
228 */
danielk19774adee202004-05-08 08:23:19 +0000229 sqlite3WhereEnd(pWInfo);
drhcce7d172000-05-31 15:34:51 +0000230
drh1bee3d72001-10-15 00:44:35 +0000231 /* Initialize the count of updated rows
232 */
danielk1977c3f9bad2002-05-15 08:30:12 +0000233 if( db->flags & SQLITE_CountRows && !pParse->trigStack ){
danielk19774adee202004-05-08 08:23:19 +0000234 sqlite3VdbeAddOp(v, OP_Integer, 0, 0);
drh1bee3d72001-10-15 00:44:35 +0000235 }
236
danielk1977f29ce552002-05-19 23:43:12 +0000237 if( row_triggers_exist ){
drh70ce3f02003-04-15 19:22:22 +0000238 /* Create pseudo-tables for NEW and OLD
239 */
danielk19774adee202004-05-08 08:23:19 +0000240 sqlite3VdbeAddOp(v, OP_OpenPseudo, oldIdx, 0);
danielk197784ac9d02004-05-18 09:58:06 +0000241 sqlite3VdbeAddOp(v, OP_SetNumColumns, oldIdx, pTab->nCol);
danielk19774adee202004-05-08 08:23:19 +0000242 sqlite3VdbeAddOp(v, OP_OpenPseudo, newIdx, 0);
danielk197784ac9d02004-05-18 09:58:06 +0000243 sqlite3VdbeAddOp(v, OP_SetNumColumns, newIdx, pTab->nCol);
danielk1977c3f9bad2002-05-15 08:30:12 +0000244
drh70ce3f02003-04-15 19:22:22 +0000245 /* The top of the update loop for when there are triggers.
246 */
danielk19774adee202004-05-08 08:23:19 +0000247 sqlite3VdbeAddOp(v, OP_ListRewind, 0, 0);
248 addr = sqlite3VdbeAddOp(v, OP_ListRead, 0, 0);
249 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000250
drh70ce3f02003-04-15 19:22:22 +0000251 /* Open a cursor and make it point to the record that is
252 ** being updated.
253 */
danielk19774adee202004-05-08 08:23:19 +0000254 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drh5cf590c2003-04-24 01:45:04 +0000255 if( !isView ){
danielk19774adee202004-05-08 08:23:19 +0000256 sqlite3VdbeAddOp(v, OP_Integer, pTab->iDb, 0);
257 sqlite3VdbeAddOp(v, OP_OpenRead, iCur, pTab->tnum);
danielk1977b4964b72004-05-18 01:23:38 +0000258 sqlite3VdbeAddOp(v, OP_SetNumColumns, iCur, pTab->nCol);
drh5cf590c2003-04-24 01:45:04 +0000259 }
drh7cf6e4d2004-05-19 14:56:55 +0000260 sqlite3VdbeAddOp(v, OP_MoveGe, iCur, 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000261
drh70ce3f02003-04-15 19:22:22 +0000262 /* Generate the OLD table
263 */
danielk19774adee202004-05-08 08:23:19 +0000264 sqlite3VdbeAddOp(v, OP_Recno, iCur, 0);
265 sqlite3VdbeAddOp(v, OP_RowData, iCur, 0);
266 sqlite3VdbeAddOp(v, OP_PutIntKey, oldIdx, 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000267
drh70ce3f02003-04-15 19:22:22 +0000268 /* Generate the NEW table
269 */
270 if( chngRecno ){
danielk19774adee202004-05-08 08:23:19 +0000271 sqlite3ExprCode(pParse, pRecnoExpr);
drh70ce3f02003-04-15 19:22:22 +0000272 }else{
danielk19774adee202004-05-08 08:23:19 +0000273 sqlite3VdbeAddOp(v, OP_Recno, iCur, 0);
drh70ce3f02003-04-15 19:22:22 +0000274 }
drh855eb1c2004-08-31 13:45:11 +0000275 for(i=0; i<pTab->nCol; i++){ /* TODO: Factor out this loop as common code */
drh70ce3f02003-04-15 19:22:22 +0000276 if( i==pTab->iPKey ){
danielk19770f69c1e2004-05-29 11:24:50 +0000277 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
drh70ce3f02003-04-15 19:22:22 +0000278 continue;
279 }
280 j = aXRef[i];
281 if( j<0 ){
danielk19774adee202004-05-08 08:23:19 +0000282 sqlite3VdbeAddOp(v, OP_Column, iCur, i);
danielk1977c3f9bad2002-05-15 08:30:12 +0000283 }else{
danielk19774adee202004-05-08 08:23:19 +0000284 sqlite3ExprCode(pParse, pChanges->a[j].pExpr);
danielk1977c3f9bad2002-05-15 08:30:12 +0000285 }
286 }
danielk19774adee202004-05-08 08:23:19 +0000287 sqlite3VdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0);
danielk1977a37cdde2004-05-16 11:15:36 +0000288 if( !isView ){
289 sqlite3TableAffinityStr(v, pTab);
290 }
291 if( pParse->nErr ) goto update_cleanup;
danielk19774adee202004-05-08 08:23:19 +0000292 sqlite3VdbeAddOp(v, OP_PutIntKey, newIdx, 0);
drh5cf590c2003-04-24 01:45:04 +0000293 if( !isView ){
danielk19774adee202004-05-08 08:23:19 +0000294 sqlite3VdbeAddOp(v, OP_Close, iCur, 0);
drh5cf590c2003-04-24 01:45:04 +0000295 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000296
drh5cf590c2003-04-24 01:45:04 +0000297 /* Fire the BEFORE and INSTEAD OF triggers
drh70ce3f02003-04-15 19:22:22 +0000298 */
danielk19774adee202004-05-08 08:23:19 +0000299 if( sqlite3CodeRowTrigger(pParse, TK_UPDATE, pChanges, TK_BEFORE, pTab,
danielk19776f349032002-06-11 02:25:40 +0000300 newIdx, oldIdx, onError, addr) ){
danielk1977f29ce552002-05-19 23:43:12 +0000301 goto update_cleanup;
302 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000303 }
304
drh5cf590c2003-04-24 01:45:04 +0000305 if( !isView ){
306 /*
307 ** Open every index that needs updating. Note that if any
308 ** index could potentially invoke a REPLACE conflict resolution
309 ** action, then we need to open all indices because we might need
310 ** to be deleting some records.
311 */
danielk19774adee202004-05-08 08:23:19 +0000312 sqlite3VdbeAddOp(v, OP_Integer, pTab->iDb, 0);
313 sqlite3VdbeAddOp(v, OP_OpenWrite, iCur, pTab->tnum);
danielk1977b4964b72004-05-18 01:23:38 +0000314 sqlite3VdbeAddOp(v, OP_SetNumColumns, iCur, pTab->nCol);
drh5cf590c2003-04-24 01:45:04 +0000315 if( onError==OE_Replace ){
316 openAll = 1;
317 }else{
318 openAll = 0;
319 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
320 if( pIdx->onError==OE_Replace ){
321 openAll = 1;
322 break;
323 }
drh0ca3e242002-01-29 23:07:02 +0000324 }
325 }
drh5cf590c2003-04-24 01:45:04 +0000326 for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
327 if( openAll || aIdxUsed[i] ){
danielk19774adee202004-05-08 08:23:19 +0000328 sqlite3VdbeAddOp(v, OP_Integer, pIdx->iDb, 0);
drhd3d39e92004-05-20 22:16:29 +0000329 sqlite3VdbeOp3(v, OP_OpenWrite, iCur+i+1, pIdx->tnum,
330 (char*)&pIdx->keyInfo, P3_KEYINFO);
drh6a3ea0e2003-05-02 14:32:12 +0000331 assert( pParse->nTab>iCur+i+1 );
drh5cf590c2003-04-24 01:45:04 +0000332 }
drh0ca3e242002-01-29 23:07:02 +0000333 }
drhcce7d172000-05-31 15:34:51 +0000334
drh5cf590c2003-04-24 01:45:04 +0000335 /* Loop over every record that needs updating. We have to load
336 ** the old data for each record to be updated because some columns
337 ** might not change and we will need to copy the old value.
338 ** Also, the old data is needed to delete the old index entires.
339 ** So make the cursor point at the old record.
340 */
341 if( !row_triggers_exist ){
danielk19774adee202004-05-08 08:23:19 +0000342 sqlite3VdbeAddOp(v, OP_ListRewind, 0, 0);
343 addr = sqlite3VdbeAddOp(v, OP_ListRead, 0, 0);
344 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drh4a324312001-12-21 14:30:42 +0000345 }
danielk19774adee202004-05-08 08:23:19 +0000346 sqlite3VdbeAddOp(v, OP_NotExists, iCur, addr);
drh5cf590c2003-04-24 01:45:04 +0000347
348 /* If the record number will change, push the record number as it
349 ** will be after the update. (The old record number is currently
350 ** on top of the stack.)
351 */
352 if( chngRecno ){
danielk19774adee202004-05-08 08:23:19 +0000353 sqlite3ExprCode(pParse, pRecnoExpr);
354 sqlite3VdbeAddOp(v, OP_MustBeInt, 0, 0);
drhcce7d172000-05-31 15:34:51 +0000355 }
drh5cf590c2003-04-24 01:45:04 +0000356
357 /* Compute new data for this record.
358 */
359 for(i=0; i<pTab->nCol; i++){
360 if( i==pTab->iPKey ){
danielk19770f69c1e2004-05-29 11:24:50 +0000361 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
drh5cf590c2003-04-24 01:45:04 +0000362 continue;
363 }
364 j = aXRef[i];
365 if( j<0 ){
danielk19774adee202004-05-08 08:23:19 +0000366 sqlite3VdbeAddOp(v, OP_Column, iCur, i);
drh5cf590c2003-04-24 01:45:04 +0000367 }else{
danielk19774adee202004-05-08 08:23:19 +0000368 sqlite3ExprCode(pParse, pChanges->a[j].pExpr);
drh5cf590c2003-04-24 01:45:04 +0000369 }
370 }
371
372 /* Do constraint checks
373 */
danielk19774adee202004-05-08 08:23:19 +0000374 sqlite3GenerateConstraintChecks(pParse, pTab, iCur, aIdxUsed, chngRecno, 1,
drh5cf590c2003-04-24 01:45:04 +0000375 onError, addr);
376
377 /* Delete the old indices for the current record.
378 */
danielk19774adee202004-05-08 08:23:19 +0000379 sqlite3GenerateRowIndexDelete(db, v, pTab, iCur, aIdxUsed);
drh5cf590c2003-04-24 01:45:04 +0000380
381 /* If changing the record number, delete the old record.
382 */
383 if( chngRecno ){
danielk19774adee202004-05-08 08:23:19 +0000384 sqlite3VdbeAddOp(v, OP_Delete, iCur, 0);
drh5cf590c2003-04-24 01:45:04 +0000385 }
386
387 /* Create the new index entries and the new record.
388 */
danielk19774adee202004-05-08 08:23:19 +0000389 sqlite3CompleteInsertion(pParse, pTab, iCur, aIdxUsed, chngRecno, 1, -1);
drhcce7d172000-05-31 15:34:51 +0000390 }
391
drh0ca3e242002-01-29 23:07:02 +0000392 /* Increment the row counter
drh1bee3d72001-10-15 00:44:35 +0000393 */
danielk1977c3f9bad2002-05-15 08:30:12 +0000394 if( db->flags & SQLITE_CountRows && !pParse->trigStack){
danielk19774adee202004-05-08 08:23:19 +0000395 sqlite3VdbeAddOp(v, OP_AddImm, 1, 0);
drh1bee3d72001-10-15 00:44:35 +0000396 }
397
drh5cf590c2003-04-24 01:45:04 +0000398 /* If there are triggers, close all the cursors after each iteration
399 ** through the loop. The fire the after triggers.
400 */
danielk1977f29ce552002-05-19 23:43:12 +0000401 if( row_triggers_exist ){
drh5cf590c2003-04-24 01:45:04 +0000402 if( !isView ){
403 for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
404 if( openAll || aIdxUsed[i] )
danielk19774adee202004-05-08 08:23:19 +0000405 sqlite3VdbeAddOp(v, OP_Close, iCur+i+1, 0);
drh5cf590c2003-04-24 01:45:04 +0000406 }
danielk19774adee202004-05-08 08:23:19 +0000407 sqlite3VdbeAddOp(v, OP_Close, iCur, 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000408 }
danielk19774adee202004-05-08 08:23:19 +0000409 if( sqlite3CodeRowTrigger(pParse, TK_UPDATE, pChanges, TK_AFTER, pTab,
danielk19776f349032002-06-11 02:25:40 +0000410 newIdx, oldIdx, onError, addr) ){
danielk1977f29ce552002-05-19 23:43:12 +0000411 goto update_cleanup;
412 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000413 }
414
drhcce7d172000-05-31 15:34:51 +0000415 /* Repeat the above with the next record to be updated, until
416 ** all record selected by the WHERE clause have been updated.
417 */
danielk19774adee202004-05-08 08:23:19 +0000418 sqlite3VdbeAddOp(v, OP_Goto, 0, addr);
419 sqlite3VdbeChangeP2(v, addr, sqlite3VdbeCurrentAddr(v));
420 sqlite3VdbeAddOp(v, OP_ListReset, 0, 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000421
422 /* Close all tables if there were no FOR EACH ROW triggers */
danielk1977f29ce552002-05-19 23:43:12 +0000423 if( !row_triggers_exist ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000424 for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
425 if( openAll || aIdxUsed[i] ){
danielk19774adee202004-05-08 08:23:19 +0000426 sqlite3VdbeAddOp(v, OP_Close, iCur+i+1, 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000427 }
428 }
danielk19774adee202004-05-08 08:23:19 +0000429 sqlite3VdbeAddOp(v, OP_Close, iCur, 0);
danielk1977f29ce552002-05-19 23:43:12 +0000430 }else{
danielk19774adee202004-05-08 08:23:19 +0000431 sqlite3VdbeAddOp(v, OP_Close, newIdx, 0);
432 sqlite3VdbeAddOp(v, OP_Close, oldIdx, 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000433 }
434
danielk19774adee202004-05-08 08:23:19 +0000435 sqlite3EndWriteOperation(pParse);
drhcce7d172000-05-31 15:34:51 +0000436
drh1bee3d72001-10-15 00:44:35 +0000437 /*
438 ** Return the number of rows that were changed.
439 */
danielk1977c3f9bad2002-05-15 08:30:12 +0000440 if( db->flags & SQLITE_CountRows && !pParse->trigStack ){
danielk19774adee202004-05-08 08:23:19 +0000441 sqlite3VdbeAddOp(v, OP_Callback, 1, 0);
danielk197722322fd2004-05-25 23:35:17 +0000442 sqlite3VdbeSetNumCols(v, 1);
danielk19773cf86062004-05-26 10:11:05 +0000443 sqlite3VdbeSetColName(v, 0, "rows updated", P3_STATIC);
drh1bee3d72001-10-15 00:44:35 +0000444 }
445
drhcce7d172000-05-31 15:34:51 +0000446update_cleanup:
danielk19774adee202004-05-08 08:23:19 +0000447 sqlite3AuthContextPop(&sContext);
drhcce7d172000-05-31 15:34:51 +0000448 sqliteFree(apIdx);
449 sqliteFree(aXRef);
danielk19774adee202004-05-08 08:23:19 +0000450 sqlite3SrcListDelete(pTabList);
451 sqlite3ExprListDelete(pChanges);
452 sqlite3ExprDelete(pWhere);
drhcce7d172000-05-31 15:34:51 +0000453 return;
454}