blob: 134b4815512ebf96de75f3e34207f2f8d760e4ff [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**
drh25303782004-12-07 15:41:48 +000015** $Id: update.c,v 1.99 2004/12/07 15:41:49 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 */
drh85e20962003-04-25 17:52:11 +000051 AuthContext sContext; /* The authorization context */
drhcce7d172000-05-31 15:34:51 +000052
drh798da522004-11-04 04:42:28 +000053#ifndef SQLITE_OMIT_TRIGGER
54 int isView; /* Trying to update a view */
drhdca76842004-12-07 14:06:13 +000055 int triggers_exist = 0; /* True if any row triggers exist */
drh798da522004-11-04 04:42:28 +000056#endif
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;
drhb7f91642004-10-31 02:22:47 +000070
71 /* Figure out if we have any triggers and if the table being
72 ** updated is a view
73 */
74#ifndef SQLITE_OMIT_TRIGGER
drhdca76842004-12-07 14:06:13 +000075 triggers_exist = sqlite3TriggersExist(pParse, pTab, TK_UPDATE, pChanges);
drh5cf590c2003-04-24 01:45:04 +000076 isView = pTab->pSelect!=0;
drhb7f91642004-10-31 02:22:47 +000077#else
drhdca76842004-12-07 14:06:13 +000078# define triggers_exist 0
drhb7f91642004-10-31 02:22:47 +000079# define isView 0
80#endif
81#ifdef SQLITE_OMIT_VIEW
82# undef isView
83# define isView 0
84#endif
85
drhdca76842004-12-07 14:06:13 +000086 if( sqlite3IsReadOnly(pParse, pTab, triggers_exist) ){
drh5cf590c2003-04-24 01:45:04 +000087 goto update_cleanup;
drha69d9162003-04-17 22:57:53 +000088 }
drh85e20962003-04-25 17:52:11 +000089 if( isView ){
danielk19774adee202004-05-08 08:23:19 +000090 if( sqlite3ViewGetColumnNames(pParse, pTab) ){
drh85e20962003-04-25 17:52:11 +000091 goto update_cleanup;
92 }
drh5cf590c2003-04-24 01:45:04 +000093 }
drheafe05b2004-06-13 00:54:01 +000094 aXRef = sqliteMallocRaw( sizeof(int) * pTab->nCol );
drhcce7d172000-05-31 15:34:51 +000095 if( aXRef==0 ) goto update_cleanup;
96 for(i=0; i<pTab->nCol; i++) aXRef[i] = -1;
97
drhb2fe7d82003-04-20 17:29:23 +000098 /* If there are FOR EACH ROW triggers, allocate cursors for the
99 ** special OLD and NEW tables
100 */
drhdca76842004-12-07 14:06:13 +0000101 if( triggers_exist ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000102 newIdx = pParse->nTab++;
103 oldIdx = pParse->nTab++;
104 }
105
drh53e3fc72002-07-16 17:22:50 +0000106 /* Allocate a cursors for the main database table and for all indices.
107 ** The index cursors might not be used, but if they are used they
108 ** need to occur right after the database cursor. So go ahead and
109 ** allocate enough space, just in case.
110 */
drh6a3ea0e2003-05-02 14:32:12 +0000111 pTabList->a[0].iCursor = iCur = pParse->nTab++;
drh53e3fc72002-07-16 17:22:50 +0000112 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
113 pParse->nTab++;
114 }
115
drh85e20962003-04-25 17:52:11 +0000116 /* Resolve the column names in all the expressions of the
117 ** of the UPDATE statement. Also find the column index
drhed6c8672003-01-12 18:02:16 +0000118 ** for each column to be updated in the pChanges array. For each
119 ** column to be updated, make sure we have authorization to change
120 ** that column.
drhcce7d172000-05-31 15:34:51 +0000121 */
drh4a324312001-12-21 14:30:42 +0000122 chngRecno = 0;
drhcce7d172000-05-31 15:34:51 +0000123 for(i=0; i<pChanges->nExpr; i++){
drh290c1942004-08-21 17:54:45 +0000124 if( sqlite3ExprResolveAndCheck(pParse, pTabList, 0,
125 pChanges->a[i].pExpr, 0, 0) ){
drhcce7d172000-05-31 15:34:51 +0000126 goto update_cleanup;
127 }
128 for(j=0; j<pTab->nCol; j++){
danielk19774adee202004-05-08 08:23:19 +0000129 if( sqlite3StrICmp(pTab->aCol[j].zName, pChanges->a[i].zName)==0 ){
drh9647ff82002-01-14 02:56:24 +0000130 if( j==pTab->iPKey ){
drh4a324312001-12-21 14:30:42 +0000131 chngRecno = 1;
132 pRecnoExpr = pChanges->a[i].pExpr;
133 }
drhcce7d172000-05-31 15:34:51 +0000134 aXRef[j] = i;
135 break;
136 }
137 }
138 if( j>=pTab->nCol ){
danielk19774adee202004-05-08 08:23:19 +0000139 if( sqlite3IsRowid(pChanges->a[i].zName) ){
drha0217ba2003-06-01 01:10:33 +0000140 chngRecno = 1;
141 pRecnoExpr = pChanges->a[i].pExpr;
142 }else{
danielk19774adee202004-05-08 08:23:19 +0000143 sqlite3ErrorMsg(pParse, "no such column: %s", pChanges->a[i].zName);
drha0217ba2003-06-01 01:10:33 +0000144 goto update_cleanup;
145 }
drhcce7d172000-05-31 15:34:51 +0000146 }
drhed6c8672003-01-12 18:02:16 +0000147#ifndef SQLITE_OMIT_AUTHORIZATION
drhe5f9c642003-01-13 23:27:31 +0000148 {
149 int rc;
danielk19774adee202004-05-08 08:23:19 +0000150 rc = sqlite3AuthCheck(pParse, SQLITE_UPDATE, pTab->zName,
drhe22a3342003-04-22 20:30:37 +0000151 pTab->aCol[j].zName, db->aDb[pTab->iDb].zName);
drhe5f9c642003-01-13 23:27:31 +0000152 if( rc==SQLITE_DENY ){
153 goto update_cleanup;
154 }else if( rc==SQLITE_IGNORE ){
155 aXRef[j] = -1;
156 }
drhed6c8672003-01-12 18:02:16 +0000157 }
158#endif
drhcce7d172000-05-31 15:34:51 +0000159 }
160
drh5a2c2c22001-11-21 02:21:11 +0000161 /* Allocate memory for the array apIdx[] and fill it with pointers to every
drhcce7d172000-05-31 15:34:51 +0000162 ** index that needs to be updated. Indices only need updating if their
drhc8392582001-12-31 02:48:51 +0000163 ** key includes one of the columns named in pChanges or if the record
164 ** number of the original table entry is changing.
drhcce7d172000-05-31 15:34:51 +0000165 */
drh0ca3e242002-01-29 23:07:02 +0000166 for(nIdx=nIdxTotal=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdxTotal++){
drh4a324312001-12-21 14:30:42 +0000167 if( chngRecno ){
168 i = 0;
169 }else {
170 for(i=0; i<pIdx->nColumn; i++){
171 if( aXRef[pIdx->aiColumn[i]]>=0 ) break;
172 }
drhcce7d172000-05-31 15:34:51 +0000173 }
drh967e8b72000-06-21 13:59:10 +0000174 if( i<pIdx->nColumn ) nIdx++;
drhcce7d172000-05-31 15:34:51 +0000175 }
drh0ca3e242002-01-29 23:07:02 +0000176 if( nIdxTotal>0 ){
drheafe05b2004-06-13 00:54:01 +0000177 apIdx = sqliteMallocRaw( sizeof(Index*) * nIdx + nIdxTotal );
drhb0729502001-03-14 12:35:57 +0000178 if( apIdx==0 ) goto update_cleanup;
drh0ca3e242002-01-29 23:07:02 +0000179 aIdxUsed = (char*)&apIdx[nIdx];
drhb0729502001-03-14 12:35:57 +0000180 }
drh0ca3e242002-01-29 23:07:02 +0000181 for(nIdx=j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
drh4a324312001-12-21 14:30:42 +0000182 if( chngRecno ){
183 i = 0;
184 }else{
185 for(i=0; i<pIdx->nColumn; i++){
186 if( aXRef[pIdx->aiColumn[i]]>=0 ) break;
187 }
drhcce7d172000-05-31 15:34:51 +0000188 }
drh0ca3e242002-01-29 23:07:02 +0000189 if( i<pIdx->nColumn ){
danielk19777cedc8d2004-06-10 10:50:08 +0000190 if( sqlite3CheckIndexCollSeq(pParse, pIdx) ) goto update_cleanup;
drh0ca3e242002-01-29 23:07:02 +0000191 apIdx[nIdx++] = pIdx;
192 aIdxUsed[j] = 1;
193 }else{
194 aIdxUsed[j] = 0;
195 }
drhcce7d172000-05-31 15:34:51 +0000196 }
197
drh85e20962003-04-25 17:52:11 +0000198 /* Resolve the column names in all the expressions in the
199 ** WHERE clause.
200 */
drh290c1942004-08-21 17:54:45 +0000201 if( sqlite3ExprResolveAndCheck(pParse, pTabList, 0, pWhere, 0, 0) ){
202 goto update_cleanup;
drh85e20962003-04-25 17:52:11 +0000203 }
204
205 /* Start the view context
206 */
207 if( isView ){
danielk19774adee202004-05-08 08:23:19 +0000208 sqlite3AuthContextPush(pParse, &sContext, pTab->zName);
drh85e20962003-04-25 17:52:11 +0000209 }
210
drhcce7d172000-05-31 15:34:51 +0000211 /* Begin generating code.
212 */
danielk19774adee202004-05-08 08:23:19 +0000213 v = sqlite3GetVdbe(pParse);
drhcce7d172000-05-31 15:34:51 +0000214 if( v==0 ) goto update_cleanup;
drh4794f732004-11-05 17:17:50 +0000215 if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
danielk19774adee202004-05-08 08:23:19 +0000216 sqlite3BeginWriteOperation(pParse, 1, pTab->iDb);
drhcce7d172000-05-31 15:34:51 +0000217
drh5cf590c2003-04-24 01:45:04 +0000218 /* If we are trying to update a view, construct that view into
219 ** a temporary table.
220 */
221 if( isView ){
drh85e20962003-04-25 17:52:11 +0000222 Select *pView;
danielk19774adee202004-05-08 08:23:19 +0000223 pView = sqlite3SelectDup(pTab->pSelect);
danielk197784ac9d02004-05-18 09:58:06 +0000224 sqlite3Select(pParse, pView, SRT_TempTable, iCur, 0, 0, 0, 0);
danielk19774adee202004-05-08 08:23:19 +0000225 sqlite3SelectDelete(pView);
drh5cf590c2003-04-24 01:45:04 +0000226 }
227
drhcce7d172000-05-31 15:34:51 +0000228 /* Begin the database scan
229 */
drhe4e72072004-11-23 01:47:30 +0000230 pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, 1, 0, 0);
drhcce7d172000-05-31 15:34:51 +0000231 if( pWInfo==0 ) goto update_cleanup;
232
233 /* Remember the index of every item to be updated.
234 */
danielk19774adee202004-05-08 08:23:19 +0000235 sqlite3VdbeAddOp(v, OP_ListWrite, 0, 0);
drhcce7d172000-05-31 15:34:51 +0000236
237 /* End the database scan loop.
238 */
danielk19774adee202004-05-08 08:23:19 +0000239 sqlite3WhereEnd(pWInfo);
drhcce7d172000-05-31 15:34:51 +0000240
drh1bee3d72001-10-15 00:44:35 +0000241 /* Initialize the count of updated rows
242 */
danielk1977c3f9bad2002-05-15 08:30:12 +0000243 if( db->flags & SQLITE_CountRows && !pParse->trigStack ){
danielk19774adee202004-05-08 08:23:19 +0000244 sqlite3VdbeAddOp(v, OP_Integer, 0, 0);
drh1bee3d72001-10-15 00:44:35 +0000245 }
246
drhdca76842004-12-07 14:06:13 +0000247 if( triggers_exist ){
drh70ce3f02003-04-15 19:22:22 +0000248 /* Create pseudo-tables for NEW and OLD
249 */
danielk19774adee202004-05-08 08:23:19 +0000250 sqlite3VdbeAddOp(v, OP_OpenPseudo, oldIdx, 0);
danielk197784ac9d02004-05-18 09:58:06 +0000251 sqlite3VdbeAddOp(v, OP_SetNumColumns, oldIdx, pTab->nCol);
danielk19774adee202004-05-08 08:23:19 +0000252 sqlite3VdbeAddOp(v, OP_OpenPseudo, newIdx, 0);
danielk197784ac9d02004-05-18 09:58:06 +0000253 sqlite3VdbeAddOp(v, OP_SetNumColumns, newIdx, pTab->nCol);
danielk1977c3f9bad2002-05-15 08:30:12 +0000254
drh70ce3f02003-04-15 19:22:22 +0000255 /* The top of the update loop for when there are triggers.
256 */
danielk19774adee202004-05-08 08:23:19 +0000257 sqlite3VdbeAddOp(v, OP_ListRewind, 0, 0);
258 addr = sqlite3VdbeAddOp(v, OP_ListRead, 0, 0);
259 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000260
drh70ce3f02003-04-15 19:22:22 +0000261 /* Open a cursor and make it point to the record that is
262 ** being updated.
263 */
danielk19774adee202004-05-08 08:23:19 +0000264 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drh5cf590c2003-04-24 01:45:04 +0000265 if( !isView ){
drhad6d9462004-09-19 02:15:24 +0000266 sqlite3OpenTableForReading(v, iCur, pTab);
drh5cf590c2003-04-24 01:45:04 +0000267 }
drh7cf6e4d2004-05-19 14:56:55 +0000268 sqlite3VdbeAddOp(v, OP_MoveGe, iCur, 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000269
drh70ce3f02003-04-15 19:22:22 +0000270 /* Generate the OLD table
271 */
danielk19774adee202004-05-08 08:23:19 +0000272 sqlite3VdbeAddOp(v, OP_Recno, iCur, 0);
273 sqlite3VdbeAddOp(v, OP_RowData, iCur, 0);
274 sqlite3VdbeAddOp(v, OP_PutIntKey, oldIdx, 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000275
drh70ce3f02003-04-15 19:22:22 +0000276 /* Generate the NEW table
277 */
278 if( chngRecno ){
drh25303782004-12-07 15:41:48 +0000279 sqlite3ExprCodeAndCache(pParse, pRecnoExpr);
drh70ce3f02003-04-15 19:22:22 +0000280 }else{
danielk19774adee202004-05-08 08:23:19 +0000281 sqlite3VdbeAddOp(v, OP_Recno, iCur, 0);
drh70ce3f02003-04-15 19:22:22 +0000282 }
drh25303782004-12-07 15:41:48 +0000283 for(i=0; i<pTab->nCol; i++){
drh70ce3f02003-04-15 19:22:22 +0000284 if( i==pTab->iPKey ){
danielk19770f69c1e2004-05-29 11:24:50 +0000285 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
drh70ce3f02003-04-15 19:22:22 +0000286 continue;
287 }
288 j = aXRef[i];
289 if( j<0 ){
danielk19774adee202004-05-08 08:23:19 +0000290 sqlite3VdbeAddOp(v, OP_Column, iCur, i);
danielk1977c3f9bad2002-05-15 08:30:12 +0000291 }else{
drh25303782004-12-07 15:41:48 +0000292 sqlite3ExprCodeAndCache(pParse, pChanges->a[j].pExpr);
danielk1977c3f9bad2002-05-15 08:30:12 +0000293 }
294 }
danielk19774adee202004-05-08 08:23:19 +0000295 sqlite3VdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0);
danielk1977a37cdde2004-05-16 11:15:36 +0000296 if( !isView ){
297 sqlite3TableAffinityStr(v, pTab);
298 }
299 if( pParse->nErr ) goto update_cleanup;
danielk19774adee202004-05-08 08:23:19 +0000300 sqlite3VdbeAddOp(v, OP_PutIntKey, newIdx, 0);
drh5cf590c2003-04-24 01:45:04 +0000301 if( !isView ){
danielk19774adee202004-05-08 08:23:19 +0000302 sqlite3VdbeAddOp(v, OP_Close, iCur, 0);
drh5cf590c2003-04-24 01:45:04 +0000303 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000304
drh5cf590c2003-04-24 01:45:04 +0000305 /* Fire the BEFORE and INSTEAD OF triggers
drh70ce3f02003-04-15 19:22:22 +0000306 */
drhdca76842004-12-07 14:06:13 +0000307 if( sqlite3CodeRowTrigger(pParse, TK_UPDATE, pChanges, TRIGGER_BEFORE, pTab,
danielk19776f349032002-06-11 02:25:40 +0000308 newIdx, oldIdx, onError, addr) ){
danielk1977f29ce552002-05-19 23:43:12 +0000309 goto update_cleanup;
310 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000311 }
312
drh5cf590c2003-04-24 01:45:04 +0000313 if( !isView ){
314 /*
315 ** Open every index that needs updating. Note that if any
316 ** index could potentially invoke a REPLACE conflict resolution
317 ** action, then we need to open all indices because we might need
318 ** to be deleting some records.
319 */
danielk19774adee202004-05-08 08:23:19 +0000320 sqlite3VdbeAddOp(v, OP_Integer, pTab->iDb, 0);
321 sqlite3VdbeAddOp(v, OP_OpenWrite, iCur, pTab->tnum);
danielk1977b4964b72004-05-18 01:23:38 +0000322 sqlite3VdbeAddOp(v, OP_SetNumColumns, iCur, pTab->nCol);
drh5cf590c2003-04-24 01:45:04 +0000323 if( onError==OE_Replace ){
324 openAll = 1;
325 }else{
326 openAll = 0;
327 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
328 if( pIdx->onError==OE_Replace ){
329 openAll = 1;
330 break;
331 }
drh0ca3e242002-01-29 23:07:02 +0000332 }
333 }
drh5cf590c2003-04-24 01:45:04 +0000334 for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
335 if( openAll || aIdxUsed[i] ){
danielk19774adee202004-05-08 08:23:19 +0000336 sqlite3VdbeAddOp(v, OP_Integer, pIdx->iDb, 0);
drhd3d39e92004-05-20 22:16:29 +0000337 sqlite3VdbeOp3(v, OP_OpenWrite, iCur+i+1, pIdx->tnum,
338 (char*)&pIdx->keyInfo, P3_KEYINFO);
drh6a3ea0e2003-05-02 14:32:12 +0000339 assert( pParse->nTab>iCur+i+1 );
drh5cf590c2003-04-24 01:45:04 +0000340 }
drh0ca3e242002-01-29 23:07:02 +0000341 }
drhcce7d172000-05-31 15:34:51 +0000342
drh5cf590c2003-04-24 01:45:04 +0000343 /* Loop over every record that needs updating. We have to load
344 ** the old data for each record to be updated because some columns
345 ** might not change and we will need to copy the old value.
346 ** Also, the old data is needed to delete the old index entires.
347 ** So make the cursor point at the old record.
348 */
drhdca76842004-12-07 14:06:13 +0000349 if( !triggers_exist ){
danielk19774adee202004-05-08 08:23:19 +0000350 sqlite3VdbeAddOp(v, OP_ListRewind, 0, 0);
351 addr = sqlite3VdbeAddOp(v, OP_ListRead, 0, 0);
352 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drh4a324312001-12-21 14:30:42 +0000353 }
danielk19774adee202004-05-08 08:23:19 +0000354 sqlite3VdbeAddOp(v, OP_NotExists, iCur, addr);
drh5cf590c2003-04-24 01:45:04 +0000355
356 /* If the record number will change, push the record number as it
357 ** will be after the update. (The old record number is currently
358 ** on top of the stack.)
359 */
360 if( chngRecno ){
danielk19774adee202004-05-08 08:23:19 +0000361 sqlite3ExprCode(pParse, pRecnoExpr);
362 sqlite3VdbeAddOp(v, OP_MustBeInt, 0, 0);
drhcce7d172000-05-31 15:34:51 +0000363 }
drh5cf590c2003-04-24 01:45:04 +0000364
365 /* Compute new data for this record.
366 */
367 for(i=0; i<pTab->nCol; i++){
368 if( i==pTab->iPKey ){
danielk19770f69c1e2004-05-29 11:24:50 +0000369 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
drh5cf590c2003-04-24 01:45:04 +0000370 continue;
371 }
372 j = aXRef[i];
373 if( j<0 ){
danielk19774adee202004-05-08 08:23:19 +0000374 sqlite3VdbeAddOp(v, OP_Column, iCur, i);
drh5cf590c2003-04-24 01:45:04 +0000375 }else{
danielk19774adee202004-05-08 08:23:19 +0000376 sqlite3ExprCode(pParse, pChanges->a[j].pExpr);
drh5cf590c2003-04-24 01:45:04 +0000377 }
378 }
379
380 /* Do constraint checks
381 */
danielk19774adee202004-05-08 08:23:19 +0000382 sqlite3GenerateConstraintChecks(pParse, pTab, iCur, aIdxUsed, chngRecno, 1,
drh5cf590c2003-04-24 01:45:04 +0000383 onError, addr);
384
385 /* Delete the old indices for the current record.
386 */
danielk19774adee202004-05-08 08:23:19 +0000387 sqlite3GenerateRowIndexDelete(db, v, pTab, iCur, aIdxUsed);
drh5cf590c2003-04-24 01:45:04 +0000388
389 /* If changing the record number, delete the old record.
390 */
391 if( chngRecno ){
danielk19774adee202004-05-08 08:23:19 +0000392 sqlite3VdbeAddOp(v, OP_Delete, iCur, 0);
drh5cf590c2003-04-24 01:45:04 +0000393 }
394
395 /* Create the new index entries and the new record.
396 */
danielk19774adee202004-05-08 08:23:19 +0000397 sqlite3CompleteInsertion(pParse, pTab, iCur, aIdxUsed, chngRecno, 1, -1);
drhcce7d172000-05-31 15:34:51 +0000398 }
399
drh0ca3e242002-01-29 23:07:02 +0000400 /* Increment the row counter
drh1bee3d72001-10-15 00:44:35 +0000401 */
danielk1977c3f9bad2002-05-15 08:30:12 +0000402 if( db->flags & SQLITE_CountRows && !pParse->trigStack){
danielk19774adee202004-05-08 08:23:19 +0000403 sqlite3VdbeAddOp(v, OP_AddImm, 1, 0);
drh1bee3d72001-10-15 00:44:35 +0000404 }
405
drh5cf590c2003-04-24 01:45:04 +0000406 /* If there are triggers, close all the cursors after each iteration
407 ** through the loop. The fire the after triggers.
408 */
drhdca76842004-12-07 14:06:13 +0000409 if( triggers_exist ){
drh5cf590c2003-04-24 01:45:04 +0000410 if( !isView ){
411 for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
412 if( openAll || aIdxUsed[i] )
danielk19774adee202004-05-08 08:23:19 +0000413 sqlite3VdbeAddOp(v, OP_Close, iCur+i+1, 0);
drh5cf590c2003-04-24 01:45:04 +0000414 }
danielk19774adee202004-05-08 08:23:19 +0000415 sqlite3VdbeAddOp(v, OP_Close, iCur, 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000416 }
drhdca76842004-12-07 14:06:13 +0000417 if( sqlite3CodeRowTrigger(pParse, TK_UPDATE, pChanges, TRIGGER_AFTER, pTab,
danielk19776f349032002-06-11 02:25:40 +0000418 newIdx, oldIdx, onError, addr) ){
danielk1977f29ce552002-05-19 23:43:12 +0000419 goto update_cleanup;
420 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000421 }
422
drhcce7d172000-05-31 15:34:51 +0000423 /* Repeat the above with the next record to be updated, until
424 ** all record selected by the WHERE clause have been updated.
425 */
danielk19774adee202004-05-08 08:23:19 +0000426 sqlite3VdbeAddOp(v, OP_Goto, 0, addr);
427 sqlite3VdbeChangeP2(v, addr, sqlite3VdbeCurrentAddr(v));
428 sqlite3VdbeAddOp(v, OP_ListReset, 0, 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000429
430 /* Close all tables if there were no FOR EACH ROW triggers */
drhdca76842004-12-07 14:06:13 +0000431 if( !triggers_exist ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000432 for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
433 if( openAll || aIdxUsed[i] ){
danielk19774adee202004-05-08 08:23:19 +0000434 sqlite3VdbeAddOp(v, OP_Close, iCur+i+1, 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000435 }
436 }
danielk19774adee202004-05-08 08:23:19 +0000437 sqlite3VdbeAddOp(v, OP_Close, iCur, 0);
danielk1977f29ce552002-05-19 23:43:12 +0000438 }else{
danielk19774adee202004-05-08 08:23:19 +0000439 sqlite3VdbeAddOp(v, OP_Close, newIdx, 0);
440 sqlite3VdbeAddOp(v, OP_Close, oldIdx, 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000441 }
442
drh1bee3d72001-10-15 00:44:35 +0000443 /*
danielk1977e7de6f22004-11-05 06:02:06 +0000444 ** Return the number of rows that were changed. If this routine is
445 ** generating code because of a call to sqlite3NestedParse(), do not
446 ** invoke the callback function.
drh1bee3d72001-10-15 00:44:35 +0000447 */
danielk1977e7de6f22004-11-05 06:02:06 +0000448 if( db->flags & SQLITE_CountRows && !pParse->trigStack && pParse->nested==0 ){
danielk19774adee202004-05-08 08:23:19 +0000449 sqlite3VdbeAddOp(v, OP_Callback, 1, 0);
danielk197722322fd2004-05-25 23:35:17 +0000450 sqlite3VdbeSetNumCols(v, 1);
danielk19773cf86062004-05-26 10:11:05 +0000451 sqlite3VdbeSetColName(v, 0, "rows updated", P3_STATIC);
drh1bee3d72001-10-15 00:44:35 +0000452 }
453
drhcce7d172000-05-31 15:34:51 +0000454update_cleanup:
danielk19774adee202004-05-08 08:23:19 +0000455 sqlite3AuthContextPop(&sContext);
drhcce7d172000-05-31 15:34:51 +0000456 sqliteFree(apIdx);
457 sqliteFree(aXRef);
danielk19774adee202004-05-08 08:23:19 +0000458 sqlite3SrcListDelete(pTabList);
459 sqlite3ExprListDelete(pChanges);
460 sqlite3ExprDelete(pWhere);
drhcce7d172000-05-31 15:34:51 +0000461 return;
462}