drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1 | /* |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2 | ** 2001 September 15 |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 3 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 4 | ** The author disclaims copyright to this source code. In place of |
| 5 | ** a legal notice, here is a blessing: |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 6 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 7 | ** 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. |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 10 | ** |
| 11 | ************************************************************************* |
| 12 | ** This file contains C code routines that are called by the parser |
| 13 | ** to handle UPDATE statements. |
| 14 | ** |
drh | eafe05b | 2004-06-13 00:54:01 +0000 | [diff] [blame^] | 15 | ** $Id: update.c,v 1.83 2004/06/13 00:54:02 drh Exp $ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 16 | */ |
| 17 | #include "sqliteInt.h" |
| 18 | |
| 19 | /* |
| 20 | ** Process an UPDATE statement. |
drh | b2fe7d8 | 2003-04-20 17:29:23 +0000 | [diff] [blame] | 21 | ** |
| 22 | ** UPDATE OR IGNORE table_wxyz SET a=b, c=d WHERE e<5 AND f NOT NULL; |
| 23 | ** \_______/ \________/ \______/ \________________/ |
| 24 | * onError pTabList pChanges pWhere |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 25 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 26 | void sqlite3Update( |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 27 | Parse *pParse, /* The parser context */ |
drh | 113088e | 2003-03-20 01:16:58 +0000 | [diff] [blame] | 28 | SrcList *pTabList, /* The table in which we should change things */ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 29 | ExprList *pChanges, /* Things to be changed */ |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 30 | Expr *pWhere, /* The WHERE clause. May be null */ |
| 31 | int onError /* How to handle constraint errors */ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 32 | ){ |
| 33 | int i, j; /* Loop counters */ |
| 34 | Table *pTab; /* The table to be updated */ |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 35 | int addr; /* VDBE instruction address of the start of the loop */ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 36 | 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 */ |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 40 | int nIdxTotal; /* Total number of indices */ |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 41 | int iCur; /* VDBE Cursor number of pTab */ |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 42 | sqlite *db; /* The database structure */ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 43 | Index **apIdx = 0; /* An array of indices that need updating too */ |
drh | b2fe7d8 | 2003-04-20 17:29:23 +0000 | [diff] [blame] | 44 | char *aIdxUsed = 0; /* aIdxUsed[i]==1 if the i-th index is used */ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 45 | int *aXRef = 0; /* aXRef[i] is the index in pChanges->a[] of the |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 46 | ** an expression for the i-th column of the table. |
| 47 | ** aXRef[i]==-1 if the i-th column is not changed. */ |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 48 | int chngRecno; /* True if the record number is being changed */ |
| 49 | Expr *pRecnoExpr; /* Expression defining the new record number */ |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 50 | int openAll; /* True if all indices need to be opened */ |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 51 | int isView; /* Trying to update a view */ |
drh | 85e2096 | 2003-04-25 17:52:11 +0000 | [diff] [blame] | 52 | AuthContext sContext; /* The authorization context */ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 53 | |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 54 | 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 */ |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 57 | |
| 58 | int newIdx = -1; /* index of trigger "new" temp table */ |
| 59 | int oldIdx = -1; /* index of trigger "old" temp table */ |
| 60 | |
drh | 85e2096 | 2003-04-25 17:52:11 +0000 | [diff] [blame] | 61 | sContext.pParse = 0; |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 62 | if( pParse->nErr || sqlite3_malloc_failed ) goto update_cleanup; |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 63 | db = pParse->db; |
drh | 113088e | 2003-03-20 01:16:58 +0000 | [diff] [blame] | 64 | assert( pTabList->nSrc==1 ); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 65 | |
drh | b2fe7d8 | 2003-04-20 17:29:23 +0000 | [diff] [blame] | 66 | /* Locate the table which we want to update. |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 67 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 68 | pTab = sqlite3SrcListLookup(pParse, pTabList); |
drh | a69d916 | 2003-04-17 22:57:53 +0000 | [diff] [blame] | 69 | if( pTab==0 ) goto update_cleanup; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 70 | before_triggers = sqlite3TriggersExist(pParse, pTab->pTrigger, |
drh | a69d916 | 2003-04-17 22:57:53 +0000 | [diff] [blame] | 71 | TK_UPDATE, TK_BEFORE, TK_ROW, pChanges); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 72 | after_triggers = sqlite3TriggersExist(pParse, pTab->pTrigger, |
drh | a69d916 | 2003-04-17 22:57:53 +0000 | [diff] [blame] | 73 | TK_UPDATE, TK_AFTER, TK_ROW, pChanges); |
| 74 | row_triggers_exist = before_triggers || after_triggers; |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 75 | isView = pTab->pSelect!=0; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 76 | if( sqlite3IsReadOnly(pParse, pTab, before_triggers) ){ |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 77 | goto update_cleanup; |
drh | a69d916 | 2003-04-17 22:57:53 +0000 | [diff] [blame] | 78 | } |
drh | 85e2096 | 2003-04-25 17:52:11 +0000 | [diff] [blame] | 79 | if( isView ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 80 | if( sqlite3ViewGetColumnNames(pParse, pTab) ){ |
drh | 85e2096 | 2003-04-25 17:52:11 +0000 | [diff] [blame] | 81 | goto update_cleanup; |
| 82 | } |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 83 | } |
drh | eafe05b | 2004-06-13 00:54:01 +0000 | [diff] [blame^] | 84 | aXRef = sqliteMallocRaw( sizeof(int) * pTab->nCol ); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 85 | if( aXRef==0 ) goto update_cleanup; |
| 86 | for(i=0; i<pTab->nCol; i++) aXRef[i] = -1; |
| 87 | |
drh | b2fe7d8 | 2003-04-20 17:29:23 +0000 | [diff] [blame] | 88 | /* If there are FOR EACH ROW triggers, allocate cursors for the |
| 89 | ** special OLD and NEW tables |
| 90 | */ |
danielk1977 | f29ce55 | 2002-05-19 23:43:12 +0000 | [diff] [blame] | 91 | if( row_triggers_exist ){ |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 92 | newIdx = pParse->nTab++; |
| 93 | oldIdx = pParse->nTab++; |
| 94 | } |
| 95 | |
drh | 53e3fc7 | 2002-07-16 17:22:50 +0000 | [diff] [blame] | 96 | /* 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 | */ |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 101 | pTabList->a[0].iCursor = iCur = pParse->nTab++; |
drh | 53e3fc7 | 2002-07-16 17:22:50 +0000 | [diff] [blame] | 102 | for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
| 103 | pParse->nTab++; |
| 104 | } |
| 105 | |
drh | 85e2096 | 2003-04-25 17:52:11 +0000 | [diff] [blame] | 106 | /* Resolve the column names in all the expressions of the |
| 107 | ** of the UPDATE statement. Also find the column index |
drh | ed6c867 | 2003-01-12 18:02:16 +0000 | [diff] [blame] | 108 | ** 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. |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 111 | */ |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 112 | chngRecno = 0; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 113 | for(i=0; i<pChanges->nExpr; i++){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 114 | if( sqlite3ExprResolveIds(pParse, pTabList, 0, pChanges->a[i].pExpr) ){ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 115 | goto update_cleanup; |
| 116 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 117 | if( sqlite3ExprCheck(pParse, pChanges->a[i].pExpr, 0, 0) ){ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 118 | goto update_cleanup; |
| 119 | } |
| 120 | for(j=0; j<pTab->nCol; j++){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 121 | if( sqlite3StrICmp(pTab->aCol[j].zName, pChanges->a[i].zName)==0 ){ |
drh | 9647ff8 | 2002-01-14 02:56:24 +0000 | [diff] [blame] | 122 | if( j==pTab->iPKey ){ |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 123 | chngRecno = 1; |
| 124 | pRecnoExpr = pChanges->a[i].pExpr; |
| 125 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 126 | aXRef[j] = i; |
| 127 | break; |
| 128 | } |
| 129 | } |
| 130 | if( j>=pTab->nCol ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 131 | if( sqlite3IsRowid(pChanges->a[i].zName) ){ |
drh | a0217ba | 2003-06-01 01:10:33 +0000 | [diff] [blame] | 132 | chngRecno = 1; |
| 133 | pRecnoExpr = pChanges->a[i].pExpr; |
| 134 | }else{ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 135 | sqlite3ErrorMsg(pParse, "no such column: %s", pChanges->a[i].zName); |
drh | a0217ba | 2003-06-01 01:10:33 +0000 | [diff] [blame] | 136 | goto update_cleanup; |
| 137 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 138 | } |
drh | ed6c867 | 2003-01-12 18:02:16 +0000 | [diff] [blame] | 139 | #ifndef SQLITE_OMIT_AUTHORIZATION |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 140 | { |
| 141 | int rc; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 142 | rc = sqlite3AuthCheck(pParse, SQLITE_UPDATE, pTab->zName, |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame] | 143 | pTab->aCol[j].zName, db->aDb[pTab->iDb].zName); |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 144 | if( rc==SQLITE_DENY ){ |
| 145 | goto update_cleanup; |
| 146 | }else if( rc==SQLITE_IGNORE ){ |
| 147 | aXRef[j] = -1; |
| 148 | } |
drh | ed6c867 | 2003-01-12 18:02:16 +0000 | [diff] [blame] | 149 | } |
| 150 | #endif |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 151 | } |
| 152 | |
drh | 5a2c2c2 | 2001-11-21 02:21:11 +0000 | [diff] [blame] | 153 | /* Allocate memory for the array apIdx[] and fill it with pointers to every |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 154 | ** index that needs to be updated. Indices only need updating if their |
drh | c839258 | 2001-12-31 02:48:51 +0000 | [diff] [blame] | 155 | ** key includes one of the columns named in pChanges or if the record |
| 156 | ** number of the original table entry is changing. |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 157 | */ |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 158 | for(nIdx=nIdxTotal=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdxTotal++){ |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 159 | if( chngRecno ){ |
| 160 | i = 0; |
| 161 | }else { |
| 162 | for(i=0; i<pIdx->nColumn; i++){ |
| 163 | if( aXRef[pIdx->aiColumn[i]]>=0 ) break; |
| 164 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 165 | } |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 166 | if( i<pIdx->nColumn ) nIdx++; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 167 | } |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 168 | if( nIdxTotal>0 ){ |
drh | eafe05b | 2004-06-13 00:54:01 +0000 | [diff] [blame^] | 169 | apIdx = sqliteMallocRaw( sizeof(Index*) * nIdx + nIdxTotal ); |
drh | b072950 | 2001-03-14 12:35:57 +0000 | [diff] [blame] | 170 | if( apIdx==0 ) goto update_cleanup; |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 171 | aIdxUsed = (char*)&apIdx[nIdx]; |
drh | b072950 | 2001-03-14 12:35:57 +0000 | [diff] [blame] | 172 | } |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 173 | for(nIdx=j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){ |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 174 | if( chngRecno ){ |
| 175 | i = 0; |
| 176 | }else{ |
| 177 | for(i=0; i<pIdx->nColumn; i++){ |
| 178 | if( aXRef[pIdx->aiColumn[i]]>=0 ) break; |
| 179 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 180 | } |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 181 | if( i<pIdx->nColumn ){ |
danielk1977 | 7cedc8d | 2004-06-10 10:50:08 +0000 | [diff] [blame] | 182 | if( sqlite3CheckIndexCollSeq(pParse, pIdx) ) goto update_cleanup; |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 183 | apIdx[nIdx++] = pIdx; |
| 184 | aIdxUsed[j] = 1; |
| 185 | }else{ |
| 186 | aIdxUsed[j] = 0; |
| 187 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 188 | } |
| 189 | |
drh | 85e2096 | 2003-04-25 17:52:11 +0000 | [diff] [blame] | 190 | /* Resolve the column names in all the expressions in the |
| 191 | ** WHERE clause. |
| 192 | */ |
| 193 | if( pWhere ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 194 | if( sqlite3ExprResolveIds(pParse, pTabList, 0, pWhere) ){ |
drh | 85e2096 | 2003-04-25 17:52:11 +0000 | [diff] [blame] | 195 | goto update_cleanup; |
| 196 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 197 | if( sqlite3ExprCheck(pParse, pWhere, 0, 0) ){ |
drh | 85e2096 | 2003-04-25 17:52:11 +0000 | [diff] [blame] | 198 | goto update_cleanup; |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | /* Start the view context |
| 203 | */ |
| 204 | if( isView ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 205 | sqlite3AuthContextPush(pParse, &sContext, pTab->zName); |
drh | 85e2096 | 2003-04-25 17:52:11 +0000 | [diff] [blame] | 206 | } |
| 207 | |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 208 | /* Begin generating code. |
| 209 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 210 | v = sqlite3GetVdbe(pParse); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 211 | if( v==0 ) goto update_cleanup; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 212 | sqlite3BeginWriteOperation(pParse, 1, pTab->iDb); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 213 | |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 214 | /* If we are trying to update a view, construct that view into |
| 215 | ** a temporary table. |
| 216 | */ |
| 217 | if( isView ){ |
drh | 85e2096 | 2003-04-25 17:52:11 +0000 | [diff] [blame] | 218 | Select *pView; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 219 | pView = sqlite3SelectDup(pTab->pSelect); |
danielk1977 | 84ac9d0 | 2004-05-18 09:58:06 +0000 | [diff] [blame] | 220 | sqlite3Select(pParse, pView, SRT_TempTable, iCur, 0, 0, 0, 0); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 221 | sqlite3SelectDelete(pView); |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 222 | } |
| 223 | |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 224 | /* Begin the database scan |
| 225 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 226 | pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, 1, 0); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 227 | if( pWInfo==0 ) goto update_cleanup; |
| 228 | |
| 229 | /* Remember the index of every item to be updated. |
| 230 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 231 | sqlite3VdbeAddOp(v, OP_ListWrite, 0, 0); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 232 | |
| 233 | /* End the database scan loop. |
| 234 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 235 | sqlite3WhereEnd(pWInfo); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 236 | |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 237 | /* Initialize the count of updated rows |
| 238 | */ |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 239 | if( db->flags & SQLITE_CountRows && !pParse->trigStack ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 240 | sqlite3VdbeAddOp(v, OP_Integer, 0, 0); |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 241 | } |
| 242 | |
danielk1977 | f29ce55 | 2002-05-19 23:43:12 +0000 | [diff] [blame] | 243 | if( row_triggers_exist ){ |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 244 | /* Create pseudo-tables for NEW and OLD |
| 245 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 246 | sqlite3VdbeAddOp(v, OP_OpenPseudo, oldIdx, 0); |
danielk1977 | 84ac9d0 | 2004-05-18 09:58:06 +0000 | [diff] [blame] | 247 | sqlite3VdbeAddOp(v, OP_SetNumColumns, oldIdx, pTab->nCol); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 248 | sqlite3VdbeAddOp(v, OP_OpenPseudo, newIdx, 0); |
danielk1977 | 84ac9d0 | 2004-05-18 09:58:06 +0000 | [diff] [blame] | 249 | sqlite3VdbeAddOp(v, OP_SetNumColumns, newIdx, pTab->nCol); |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 250 | |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 251 | /* The top of the update loop for when there are triggers. |
| 252 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 253 | sqlite3VdbeAddOp(v, OP_ListRewind, 0, 0); |
| 254 | addr = sqlite3VdbeAddOp(v, OP_ListRead, 0, 0); |
| 255 | sqlite3VdbeAddOp(v, OP_Dup, 0, 0); |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 256 | |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 257 | /* Open a cursor and make it point to the record that is |
| 258 | ** being updated. |
| 259 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 260 | sqlite3VdbeAddOp(v, OP_Dup, 0, 0); |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 261 | if( !isView ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 262 | sqlite3VdbeAddOp(v, OP_Integer, pTab->iDb, 0); |
| 263 | sqlite3VdbeAddOp(v, OP_OpenRead, iCur, pTab->tnum); |
danielk1977 | b4964b7 | 2004-05-18 01:23:38 +0000 | [diff] [blame] | 264 | sqlite3VdbeAddOp(v, OP_SetNumColumns, iCur, pTab->nCol); |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 265 | } |
drh | 7cf6e4d | 2004-05-19 14:56:55 +0000 | [diff] [blame] | 266 | sqlite3VdbeAddOp(v, OP_MoveGe, iCur, 0); |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 267 | |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 268 | /* Generate the OLD table |
| 269 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 270 | sqlite3VdbeAddOp(v, OP_Recno, iCur, 0); |
| 271 | sqlite3VdbeAddOp(v, OP_RowData, iCur, 0); |
| 272 | sqlite3VdbeAddOp(v, OP_PutIntKey, oldIdx, 0); |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 273 | |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 274 | /* Generate the NEW table |
| 275 | */ |
| 276 | if( chngRecno ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 277 | sqlite3ExprCode(pParse, pRecnoExpr); |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 278 | }else{ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 279 | sqlite3VdbeAddOp(v, OP_Recno, iCur, 0); |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 280 | } |
| 281 | for(i=0; i<pTab->nCol; i++){ |
| 282 | if( i==pTab->iPKey ){ |
danielk1977 | 0f69c1e | 2004-05-29 11:24:50 +0000 | [diff] [blame] | 283 | sqlite3VdbeAddOp(v, OP_String8, 0, 0); |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 284 | continue; |
| 285 | } |
| 286 | j = aXRef[i]; |
| 287 | if( j<0 ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 288 | sqlite3VdbeAddOp(v, OP_Column, iCur, i); |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 289 | }else{ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 290 | sqlite3ExprCode(pParse, pChanges->a[j].pExpr); |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 291 | } |
| 292 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 293 | sqlite3VdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0); |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 294 | if( !isView ){ |
| 295 | sqlite3TableAffinityStr(v, pTab); |
| 296 | } |
| 297 | if( pParse->nErr ) goto update_cleanup; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 298 | sqlite3VdbeAddOp(v, OP_PutIntKey, newIdx, 0); |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 299 | if( !isView ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 300 | sqlite3VdbeAddOp(v, OP_Close, iCur, 0); |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 301 | } |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 302 | |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 303 | /* Fire the BEFORE and INSTEAD OF triggers |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 304 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 305 | if( sqlite3CodeRowTrigger(pParse, TK_UPDATE, pChanges, TK_BEFORE, pTab, |
danielk1977 | 6f34903 | 2002-06-11 02:25:40 +0000 | [diff] [blame] | 306 | newIdx, oldIdx, onError, addr) ){ |
danielk1977 | f29ce55 | 2002-05-19 23:43:12 +0000 | [diff] [blame] | 307 | goto update_cleanup; |
| 308 | } |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 309 | } |
| 310 | |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 311 | if( !isView ){ |
| 312 | /* |
| 313 | ** Open every index that needs updating. Note that if any |
| 314 | ** index could potentially invoke a REPLACE conflict resolution |
| 315 | ** action, then we need to open all indices because we might need |
| 316 | ** to be deleting some records. |
| 317 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 318 | sqlite3VdbeAddOp(v, OP_Integer, pTab->iDb, 0); |
| 319 | sqlite3VdbeAddOp(v, OP_OpenWrite, iCur, pTab->tnum); |
danielk1977 | b4964b7 | 2004-05-18 01:23:38 +0000 | [diff] [blame] | 320 | sqlite3VdbeAddOp(v, OP_SetNumColumns, iCur, pTab->nCol); |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 321 | if( onError==OE_Replace ){ |
| 322 | openAll = 1; |
| 323 | }else{ |
| 324 | openAll = 0; |
| 325 | for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
| 326 | if( pIdx->onError==OE_Replace ){ |
| 327 | openAll = 1; |
| 328 | break; |
| 329 | } |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 330 | } |
| 331 | } |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 332 | for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){ |
| 333 | if( openAll || aIdxUsed[i] ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 334 | sqlite3VdbeAddOp(v, OP_Integer, pIdx->iDb, 0); |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 335 | sqlite3VdbeOp3(v, OP_OpenWrite, iCur+i+1, pIdx->tnum, |
| 336 | (char*)&pIdx->keyInfo, P3_KEYINFO); |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 337 | assert( pParse->nTab>iCur+i+1 ); |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 338 | } |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 339 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 340 | |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 341 | /* Loop over every record that needs updating. We have to load |
| 342 | ** the old data for each record to be updated because some columns |
| 343 | ** might not change and we will need to copy the old value. |
| 344 | ** Also, the old data is needed to delete the old index entires. |
| 345 | ** So make the cursor point at the old record. |
| 346 | */ |
| 347 | if( !row_triggers_exist ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 348 | sqlite3VdbeAddOp(v, OP_ListRewind, 0, 0); |
| 349 | addr = sqlite3VdbeAddOp(v, OP_ListRead, 0, 0); |
| 350 | sqlite3VdbeAddOp(v, OP_Dup, 0, 0); |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 351 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 352 | sqlite3VdbeAddOp(v, OP_NotExists, iCur, addr); |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 353 | |
| 354 | /* If the record number will change, push the record number as it |
| 355 | ** will be after the update. (The old record number is currently |
| 356 | ** on top of the stack.) |
| 357 | */ |
| 358 | if( chngRecno ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 359 | sqlite3ExprCode(pParse, pRecnoExpr); |
| 360 | sqlite3VdbeAddOp(v, OP_MustBeInt, 0, 0); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 361 | } |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 362 | |
| 363 | /* Compute new data for this record. |
| 364 | */ |
| 365 | for(i=0; i<pTab->nCol; i++){ |
| 366 | if( i==pTab->iPKey ){ |
danielk1977 | 0f69c1e | 2004-05-29 11:24:50 +0000 | [diff] [blame] | 367 | sqlite3VdbeAddOp(v, OP_String8, 0, 0); |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 368 | continue; |
| 369 | } |
| 370 | j = aXRef[i]; |
| 371 | if( j<0 ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 372 | sqlite3VdbeAddOp(v, OP_Column, iCur, i); |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 373 | }else{ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 374 | sqlite3ExprCode(pParse, pChanges->a[j].pExpr); |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 375 | } |
| 376 | } |
| 377 | |
| 378 | /* Do constraint checks |
| 379 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 380 | sqlite3GenerateConstraintChecks(pParse, pTab, iCur, aIdxUsed, chngRecno, 1, |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 381 | onError, addr); |
| 382 | |
| 383 | /* Delete the old indices for the current record. |
| 384 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 385 | sqlite3GenerateRowIndexDelete(db, v, pTab, iCur, aIdxUsed); |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 386 | |
| 387 | /* If changing the record number, delete the old record. |
| 388 | */ |
| 389 | if( chngRecno ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 390 | sqlite3VdbeAddOp(v, OP_Delete, iCur, 0); |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | /* Create the new index entries and the new record. |
| 394 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 395 | sqlite3CompleteInsertion(pParse, pTab, iCur, aIdxUsed, chngRecno, 1, -1); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 396 | } |
| 397 | |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 398 | /* Increment the row counter |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 399 | */ |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 400 | if( db->flags & SQLITE_CountRows && !pParse->trigStack){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 401 | sqlite3VdbeAddOp(v, OP_AddImm, 1, 0); |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 402 | } |
| 403 | |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 404 | /* If there are triggers, close all the cursors after each iteration |
| 405 | ** through the loop. The fire the after triggers. |
| 406 | */ |
danielk1977 | f29ce55 | 2002-05-19 23:43:12 +0000 | [diff] [blame] | 407 | if( row_triggers_exist ){ |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 408 | if( !isView ){ |
| 409 | for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){ |
| 410 | if( openAll || aIdxUsed[i] ) |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 411 | sqlite3VdbeAddOp(v, OP_Close, iCur+i+1, 0); |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 412 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 413 | sqlite3VdbeAddOp(v, OP_Close, iCur, 0); |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 414 | pParse->nTab = iCur; |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 415 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 416 | if( sqlite3CodeRowTrigger(pParse, TK_UPDATE, pChanges, TK_AFTER, pTab, |
danielk1977 | 6f34903 | 2002-06-11 02:25:40 +0000 | [diff] [blame] | 417 | newIdx, oldIdx, onError, addr) ){ |
danielk1977 | f29ce55 | 2002-05-19 23:43:12 +0000 | [diff] [blame] | 418 | goto update_cleanup; |
| 419 | } |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 420 | } |
| 421 | |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 422 | /* Repeat the above with the next record to be updated, until |
| 423 | ** all record selected by the WHERE clause have been updated. |
| 424 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 425 | sqlite3VdbeAddOp(v, OP_Goto, 0, addr); |
| 426 | sqlite3VdbeChangeP2(v, addr, sqlite3VdbeCurrentAddr(v)); |
| 427 | sqlite3VdbeAddOp(v, OP_ListReset, 0, 0); |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 428 | |
| 429 | /* Close all tables if there were no FOR EACH ROW triggers */ |
danielk1977 | f29ce55 | 2002-05-19 23:43:12 +0000 | [diff] [blame] | 430 | if( !row_triggers_exist ){ |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 431 | for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){ |
| 432 | if( openAll || aIdxUsed[i] ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 433 | sqlite3VdbeAddOp(v, OP_Close, iCur+i+1, 0); |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 434 | } |
| 435 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 436 | sqlite3VdbeAddOp(v, OP_Close, iCur, 0); |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 437 | pParse->nTab = iCur; |
danielk1977 | f29ce55 | 2002-05-19 23:43:12 +0000 | [diff] [blame] | 438 | }else{ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 439 | sqlite3VdbeAddOp(v, OP_Close, newIdx, 0); |
| 440 | sqlite3VdbeAddOp(v, OP_Close, oldIdx, 0); |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 441 | } |
| 442 | |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 443 | sqlite3VdbeAddOp(v, OP_SetCounts, 0, 0); |
| 444 | sqlite3EndWriteOperation(pParse); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 445 | |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 446 | /* |
| 447 | ** Return the number of rows that were changed. |
| 448 | */ |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 449 | if( db->flags & SQLITE_CountRows && !pParse->trigStack ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 450 | sqlite3VdbeAddOp(v, OP_Callback, 1, 0); |
danielk1977 | 22322fd | 2004-05-25 23:35:17 +0000 | [diff] [blame] | 451 | sqlite3VdbeSetNumCols(v, 1); |
danielk1977 | 3cf8606 | 2004-05-26 10:11:05 +0000 | [diff] [blame] | 452 | sqlite3VdbeSetColName(v, 0, "rows updated", P3_STATIC); |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 453 | } |
| 454 | |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 455 | update_cleanup: |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 456 | sqlite3AuthContextPop(&sContext); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 457 | sqliteFree(apIdx); |
| 458 | sqliteFree(aXRef); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 459 | sqlite3SrcListDelete(pTabList); |
| 460 | sqlite3ExprListDelete(pChanges); |
| 461 | sqlite3ExprDelete(pWhere); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 462 | return; |
| 463 | } |