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 | ed6c867 | 2003-01-12 18:02:16 +0000 | [diff] [blame^] | 15 | ** $Id: update.c,v 1.52 2003/01/12 18:02:19 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. |
| 21 | */ |
| 22 | void sqliteUpdate( |
| 23 | Parse *pParse, /* The parser context */ |
| 24 | Token *pTableName, /* The table in which we should change things */ |
| 25 | ExprList *pChanges, /* Things to be changed */ |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 26 | Expr *pWhere, /* The WHERE clause. May be null */ |
| 27 | int onError /* How to handle constraint errors */ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 28 | ){ |
| 29 | int i, j; /* Loop counters */ |
| 30 | Table *pTab; /* The table to be updated */ |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 31 | SrcList *pTabList = 0; /* Fake FROM clause containing only pTab */ |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 32 | int addr; /* VDBE instruction address of the start of the loop */ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 33 | WhereInfo *pWInfo; /* Information about the WHERE clause */ |
| 34 | Vdbe *v; /* The virtual database engine */ |
| 35 | Index *pIdx; /* For looping over indices */ |
| 36 | int nIdx; /* Number of indices that need updating */ |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 37 | int nIdxTotal; /* Total number of indices */ |
drh | 4794b98 | 2000-06-06 13:54:14 +0000 | [diff] [blame] | 38 | int base; /* Index of first available table cursor */ |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 39 | sqlite *db; /* The database structure */ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 40 | Index **apIdx = 0; /* An array of indices that need updating too */ |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 41 | char *aIdxUsed = 0; /* aIdxUsed[i] if the i-th index is used */ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 42 | int *aXRef = 0; /* aXRef[i] is the index in pChanges->a[] of the |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 43 | ** an expression for the i-th column of the table. |
| 44 | ** aXRef[i]==-1 if the i-th column is not changed. */ |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 45 | int openOp; /* Opcode used to open tables */ |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 46 | int chngRecno; /* True if the record number is being changed */ |
| 47 | Expr *pRecnoExpr; /* Expression defining the new record number */ |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 48 | int openAll; /* True if all indices need to be opened */ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 49 | |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 50 | int row_triggers_exist = 0; |
| 51 | |
| 52 | int newIdx = -1; /* index of trigger "new" temp table */ |
| 53 | int oldIdx = -1; /* index of trigger "old" temp table */ |
| 54 | |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 55 | if( pParse->nErr || sqlite_malloc_failed ) goto update_cleanup; |
drh | ed6c867 | 2003-01-12 18:02:16 +0000 | [diff] [blame^] | 56 | if( sqliteAuthCommand(pParse, "UPDATE", 0) ) goto update_cleanup; |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 57 | db = pParse->db; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 58 | |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 59 | /* Check for the special case of a VIEW with one or more ON UPDATE triggers |
| 60 | * defined |
| 61 | */ |
| 62 | { |
danielk1977 | f29ce55 | 2002-05-19 23:43:12 +0000 | [diff] [blame] | 63 | char *zTab = sqliteTableNameFromToken(pTableName); |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 64 | |
danielk1977 | f29ce55 | 2002-05-19 23:43:12 +0000 | [diff] [blame] | 65 | if( zTab != 0 ){ |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 66 | pTab = sqliteFindTable(pParse->db, zTab); |
danielk1977 | f29ce55 | 2002-05-19 23:43:12 +0000 | [diff] [blame] | 67 | if( pTab ){ |
drh | 9adf9ac | 2002-05-15 11:44:13 +0000 | [diff] [blame] | 68 | row_triggers_exist = |
| 69 | sqliteTriggersExist(pParse, pTab->pTrigger, |
| 70 | TK_UPDATE, TK_BEFORE, TK_ROW, pChanges) || |
| 71 | sqliteTriggersExist(pParse, pTab->pTrigger, |
| 72 | TK_UPDATE, TK_AFTER, TK_ROW, pChanges); |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 73 | } |
| 74 | sqliteFree(zTab); |
danielk1977 | f29ce55 | 2002-05-19 23:43:12 +0000 | [diff] [blame] | 75 | if( row_triggers_exist && pTab->pSelect ){ |
drh | 9adf9ac | 2002-05-15 11:44:13 +0000 | [diff] [blame] | 76 | /* Just fire VIEW triggers */ |
| 77 | sqliteViewTriggers(pParse, pTab, pWhere, onError, pChanges); |
| 78 | return; |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 79 | } |
| 80 | } |
| 81 | } |
| 82 | |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 83 | /* Locate the table which we want to update. This table has to be |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 84 | ** put in an SrcList structure because some of the subroutines we |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 85 | ** will be calling are designed to work with multiple tables and expect |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 86 | ** an SrcList* parameter instead of just a Table* parameter. |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 87 | */ |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 88 | pTabList = sqliteTableTokenToSrcList(pParse, pTableName); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 89 | if( pTabList==0 ) goto update_cleanup; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 90 | pTab = pTabList->a[0].pTab; |
drh | 417be79 | 2002-03-03 18:59:40 +0000 | [diff] [blame] | 91 | assert( pTab->pSelect==0 ); /* This table is not a VIEW */ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 92 | aXRef = sqliteMalloc( sizeof(int) * pTab->nCol ); |
| 93 | if( aXRef==0 ) goto update_cleanup; |
| 94 | for(i=0; i<pTab->nCol; i++) aXRef[i] = -1; |
| 95 | |
danielk1977 | f29ce55 | 2002-05-19 23:43:12 +0000 | [diff] [blame] | 96 | /* If there are FOR EACH ROW triggers, allocate temp tables */ |
| 97 | if( row_triggers_exist ){ |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 98 | newIdx = pParse->nTab++; |
| 99 | oldIdx = pParse->nTab++; |
| 100 | } |
| 101 | |
drh | 53e3fc7 | 2002-07-16 17:22:50 +0000 | [diff] [blame] | 102 | /* Allocate a cursors for the main database table and for all indices. |
| 103 | ** The index cursors might not be used, but if they are used they |
| 104 | ** need to occur right after the database cursor. So go ahead and |
| 105 | ** allocate enough space, just in case. |
| 106 | */ |
| 107 | base = pParse->nTab++; |
| 108 | for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
| 109 | pParse->nTab++; |
| 110 | } |
| 111 | |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 112 | /* Resolve the column names in all the expressions in both the |
| 113 | ** WHERE clause and in the new values. Also find the column index |
drh | ed6c867 | 2003-01-12 18:02:16 +0000 | [diff] [blame^] | 114 | ** for each column to be updated in the pChanges array. For each |
| 115 | ** column to be updated, make sure we have authorization to change |
| 116 | ** that column. |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 117 | */ |
| 118 | if( pWhere ){ |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 119 | if( sqliteExprResolveIds(pParse, base, pTabList, 0, pWhere) ){ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 120 | goto update_cleanup; |
| 121 | } |
| 122 | if( sqliteExprCheck(pParse, pWhere, 0, 0) ){ |
| 123 | goto update_cleanup; |
| 124 | } |
| 125 | } |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 126 | chngRecno = 0; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 127 | for(i=0; i<pChanges->nExpr; i++){ |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 128 | if( sqliteExprResolveIds(pParse, base, pTabList, 0, pChanges->a[i].pExpr) ){ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 129 | goto update_cleanup; |
| 130 | } |
| 131 | if( sqliteExprCheck(pParse, pChanges->a[i].pExpr, 0, 0) ){ |
| 132 | goto update_cleanup; |
| 133 | } |
| 134 | for(j=0; j<pTab->nCol; j++){ |
drh | 6206d50 | 2000-06-19 19:09:08 +0000 | [diff] [blame] | 135 | if( sqliteStrICmp(pTab->aCol[j].zName, pChanges->a[i].zName)==0 ){ |
drh | 9647ff8 | 2002-01-14 02:56:24 +0000 | [diff] [blame] | 136 | if( j==pTab->iPKey ){ |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 137 | chngRecno = 1; |
| 138 | pRecnoExpr = pChanges->a[i].pExpr; |
| 139 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 140 | aXRef[j] = i; |
| 141 | break; |
| 142 | } |
| 143 | } |
| 144 | if( j>=pTab->nCol ){ |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 145 | sqliteSetString(&pParse->zErrMsg, "no such column: ", |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 146 | pChanges->a[i].zName, 0); |
| 147 | pParse->nErr++; |
| 148 | goto update_cleanup; |
| 149 | } |
drh | ed6c867 | 2003-01-12 18:02:16 +0000 | [diff] [blame^] | 150 | #ifndef SQLITE_OMIT_AUTHORIZATION |
| 151 | if( sqliteAuthWrite(pParse, pTab, j)==SQLITE_IGNORE ){ |
| 152 | aXRef[j] = -1; |
| 153 | } |
| 154 | #endif |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 155 | } |
| 156 | |
drh | 5a2c2c2 | 2001-11-21 02:21:11 +0000 | [diff] [blame] | 157 | /* Allocate memory for the array apIdx[] and fill it with pointers to every |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 158 | ** index that needs to be updated. Indices only need updating if their |
drh | c839258 | 2001-12-31 02:48:51 +0000 | [diff] [blame] | 159 | ** key includes one of the columns named in pChanges or if the record |
| 160 | ** number of the original table entry is changing. |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 161 | */ |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 162 | for(nIdx=nIdxTotal=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdxTotal++){ |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 163 | if( chngRecno ){ |
| 164 | i = 0; |
| 165 | }else { |
| 166 | for(i=0; i<pIdx->nColumn; i++){ |
| 167 | if( aXRef[pIdx->aiColumn[i]]>=0 ) break; |
| 168 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 169 | } |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 170 | if( i<pIdx->nColumn ) nIdx++; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 171 | } |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 172 | if( nIdxTotal>0 ){ |
| 173 | apIdx = sqliteMalloc( sizeof(Index*) * nIdx + nIdxTotal ); |
drh | b072950 | 2001-03-14 12:35:57 +0000 | [diff] [blame] | 174 | if( apIdx==0 ) goto update_cleanup; |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 175 | aIdxUsed = (char*)&apIdx[nIdx]; |
drh | b072950 | 2001-03-14 12:35:57 +0000 | [diff] [blame] | 176 | } |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 177 | for(nIdx=j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){ |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 178 | if( chngRecno ){ |
| 179 | i = 0; |
| 180 | }else{ |
| 181 | for(i=0; i<pIdx->nColumn; i++){ |
| 182 | if( aXRef[pIdx->aiColumn[i]]>=0 ) break; |
| 183 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 184 | } |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 185 | if( i<pIdx->nColumn ){ |
| 186 | apIdx[nIdx++] = pIdx; |
| 187 | aIdxUsed[j] = 1; |
| 188 | }else{ |
| 189 | aIdxUsed[j] = 0; |
| 190 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | /* Begin generating code. |
| 194 | */ |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 195 | v = sqliteGetVdbe(pParse); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 196 | if( v==0 ) goto update_cleanup; |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 197 | sqliteBeginWriteOperation(pParse, 1, !row_triggers_exist && pTab->isTemp); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 198 | |
| 199 | /* Begin the database scan |
| 200 | */ |
drh | e318474 | 2002-06-19 14:27:05 +0000 | [diff] [blame] | 201 | pWInfo = sqliteWhereBegin(pParse, base, pTabList, pWhere, 1, 0); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 202 | if( pWInfo==0 ) goto update_cleanup; |
| 203 | |
| 204 | /* Remember the index of every item to be updated. |
| 205 | */ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 206 | sqliteVdbeAddOp(v, OP_ListWrite, 0, 0); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 207 | |
| 208 | /* End the database scan loop. |
| 209 | */ |
| 210 | sqliteWhereEnd(pWInfo); |
| 211 | |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 212 | /* Initialize the count of updated rows |
| 213 | */ |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 214 | if( db->flags & SQLITE_CountRows && !pParse->trigStack ){ |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 215 | sqliteVdbeAddOp(v, OP_Integer, 0, 0); |
| 216 | } |
| 217 | |
danielk1977 | f29ce55 | 2002-05-19 23:43:12 +0000 | [diff] [blame] | 218 | if( row_triggers_exist ){ |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 219 | int ii; |
| 220 | |
| 221 | sqliteVdbeAddOp(v, OP_OpenTemp, oldIdx, 0); |
| 222 | sqliteVdbeAddOp(v, OP_OpenTemp, newIdx, 0); |
| 223 | |
| 224 | sqliteVdbeAddOp(v, OP_ListRewind, 0, 0); |
| 225 | addr = sqliteVdbeAddOp(v, OP_ListRead, 0, 0); |
| 226 | sqliteVdbeAddOp(v, OP_Dup, 0, 0); |
| 227 | |
| 228 | sqliteVdbeAddOp(v, OP_Dup, 0, 0); |
danielk1977 | 368c7f6 | 2002-07-21 23:09:55 +0000 | [diff] [blame] | 229 | sqliteVdbeAddOp(v, (pTab->isTemp?OP_OpenAux:OP_Open), base, pTab->tnum); |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 230 | sqliteVdbeAddOp(v, OP_MoveTo, base, 0); |
| 231 | |
| 232 | sqliteVdbeAddOp(v, OP_Integer, 13, 0); |
danielk1977 | f29ce55 | 2002-05-19 23:43:12 +0000 | [diff] [blame] | 233 | for(ii = 0; ii < pTab->nCol; ii++){ |
| 234 | if( ii == pTab->iPKey ){ |
| 235 | sqliteVdbeAddOp(v, OP_Recno, base, 0); |
| 236 | }else{ |
| 237 | sqliteVdbeAddOp(v, OP_Column, base, ii); |
| 238 | } |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 239 | } |
| 240 | sqliteVdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0); |
| 241 | sqliteVdbeAddOp(v, OP_PutIntKey, oldIdx, 0); |
| 242 | |
| 243 | sqliteVdbeAddOp(v, OP_Integer, 13, 0); |
danielk1977 | f29ce55 | 2002-05-19 23:43:12 +0000 | [diff] [blame] | 244 | for(ii = 0; ii < pTab->nCol; ii++){ |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 245 | if( aXRef[ii] < 0 ){ |
danielk1977 | f29ce55 | 2002-05-19 23:43:12 +0000 | [diff] [blame] | 246 | if( ii == pTab->iPKey ){ |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 247 | sqliteVdbeAddOp(v, OP_Recno, base, 0); |
danielk1977 | f29ce55 | 2002-05-19 23:43:12 +0000 | [diff] [blame] | 248 | }else{ |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 249 | sqliteVdbeAddOp(v, OP_Column, base, ii); |
danielk1977 | f29ce55 | 2002-05-19 23:43:12 +0000 | [diff] [blame] | 250 | } |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 251 | }else{ |
| 252 | sqliteExprCode(pParse, pChanges->a[aXRef[ii]].pExpr); |
| 253 | } |
| 254 | } |
| 255 | sqliteVdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0); |
| 256 | sqliteVdbeAddOp(v, OP_PutIntKey, newIdx, 0); |
| 257 | sqliteVdbeAddOp(v, OP_Close, base, 0); |
| 258 | |
| 259 | sqliteVdbeAddOp(v, OP_Rewind, oldIdx, 0); |
| 260 | sqliteVdbeAddOp(v, OP_Rewind, newIdx, 0); |
| 261 | |
danielk1977 | f29ce55 | 2002-05-19 23:43:12 +0000 | [diff] [blame] | 262 | if( sqliteCodeRowTrigger(pParse, TK_UPDATE, pChanges, TK_BEFORE, pTab, |
danielk1977 | 6f34903 | 2002-06-11 02:25:40 +0000 | [diff] [blame] | 263 | newIdx, oldIdx, onError, addr) ){ |
danielk1977 | f29ce55 | 2002-05-19 23:43:12 +0000 | [diff] [blame] | 264 | goto update_cleanup; |
| 265 | } |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 266 | } |
| 267 | |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 268 | /* Rewind the list of records that need to be updated and |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 269 | ** open every index that needs updating. Note that if any |
| 270 | ** index could potentially invoke a REPLACE conflict resolution |
| 271 | ** action, then we need to open all indices because we might need |
| 272 | ** to be deleting some records. |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 273 | */ |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 274 | openOp = pTab->isTemp ? OP_OpenWrAux : OP_OpenWrite; |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 275 | sqliteVdbeAddOp(v, openOp, base, pTab->tnum); |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 276 | if( onError==OE_Replace ){ |
| 277 | openAll = 1; |
| 278 | }else{ |
| 279 | openAll = 0; |
| 280 | for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
| 281 | if( pIdx->onError==OE_Replace ){ |
| 282 | openAll = 1; |
| 283 | break; |
| 284 | } |
| 285 | } |
| 286 | } |
| 287 | for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){ |
| 288 | if( openAll || aIdxUsed[i] ){ |
| 289 | sqliteVdbeAddOp(v, openOp, base+i+1, pIdx->tnum); |
drh | 53e3fc7 | 2002-07-16 17:22:50 +0000 | [diff] [blame] | 290 | assert( pParse->nTab>base+i+1 ); |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 291 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | /* Loop over every record that needs updating. We have to load |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 295 | ** the old data for each record to be updated because some columns |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 296 | ** might not change and we will need to copy the old value. |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 297 | ** Also, the old data is needed to delete the old index entires. |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 298 | ** So make the cursor point at the old record. |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 299 | */ |
danielk1977 | f29ce55 | 2002-05-19 23:43:12 +0000 | [diff] [blame] | 300 | if( !row_triggers_exist ){ |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 301 | sqliteVdbeAddOp(v, OP_ListRewind, 0, 0); |
| 302 | addr = sqliteVdbeAddOp(v, OP_ListRead, 0, 0); |
| 303 | sqliteVdbeAddOp(v, OP_Dup, 0, 0); |
| 304 | } |
drh | 07d6e3a | 2002-05-23 12:50:18 +0000 | [diff] [blame] | 305 | sqliteVdbeAddOp(v, OP_NotExists, base, addr); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 306 | |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 307 | /* If the record number will change, push the record number as it |
| 308 | ** will be after the update. (The old record number is currently |
| 309 | ** on top of the stack.) |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 310 | */ |
| 311 | if( chngRecno ){ |
drh | a9f9d1c | 2002-06-29 02:20:08 +0000 | [diff] [blame] | 312 | sqliteExprCode(pParse, pRecnoExpr); |
| 313 | sqliteVdbeAddOp(v, OP_MustBeInt, 0, 0); |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 314 | } |
| 315 | |
drh | 5a2c2c2 | 2001-11-21 02:21:11 +0000 | [diff] [blame] | 316 | /* Compute new data for this record. |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 317 | */ |
| 318 | for(i=0; i<pTab->nCol; i++){ |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 319 | if( i==pTab->iPKey ){ |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 320 | sqliteVdbeAddOp(v, OP_String, 0, 0); |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 321 | continue; |
| 322 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 323 | j = aXRef[i]; |
| 324 | if( j<0 ){ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 325 | sqliteVdbeAddOp(v, OP_Column, base, i); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 326 | }else{ |
| 327 | sqliteExprCode(pParse, pChanges->a[j].pExpr); |
| 328 | } |
| 329 | } |
| 330 | |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 331 | /* Do constraint checks |
| 332 | */ |
drh | b419a92 | 2002-01-30 16:17:23 +0000 | [diff] [blame] | 333 | sqliteGenerateConstraintChecks(pParse, pTab, base, aIdxUsed, chngRecno, 1, |
| 334 | onError, addr); |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 335 | |
| 336 | /* Delete the old indices for the current record. |
| 337 | */ |
drh | 38640e1 | 2002-07-05 21:42:36 +0000 | [diff] [blame] | 338 | sqliteGenerateRowIndexDelete(db, v, pTab, base, aIdxUsed); |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 339 | |
drh | c839258 | 2001-12-31 02:48:51 +0000 | [diff] [blame] | 340 | /* If changing the record number, delete the old record. |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 341 | */ |
| 342 | if( chngRecno ){ |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 343 | sqliteVdbeAddOp(v, OP_Delete, base, 0); |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 344 | } |
| 345 | |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 346 | /* Create the new index entries and the new record. |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 347 | */ |
drh | b419a92 | 2002-01-30 16:17:23 +0000 | [diff] [blame] | 348 | sqliteCompleteInsertion(pParse, pTab, base, aIdxUsed, chngRecno, 1); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 349 | |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 350 | /* Increment the row counter |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 351 | */ |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 352 | if( db->flags & SQLITE_CountRows && !pParse->trigStack){ |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 353 | sqliteVdbeAddOp(v, OP_AddImm, 1, 0); |
| 354 | } |
| 355 | |
danielk1977 | f29ce55 | 2002-05-19 23:43:12 +0000 | [diff] [blame] | 356 | if( row_triggers_exist ){ |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 357 | for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){ |
| 358 | if( openAll || aIdxUsed[i] ) |
drh | 9adf9ac | 2002-05-15 11:44:13 +0000 | [diff] [blame] | 359 | sqliteVdbeAddOp(v, OP_Close, base+i+1, 0); |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 360 | } |
| 361 | sqliteVdbeAddOp(v, OP_Close, base, 0); |
| 362 | pParse->nTab = base; |
| 363 | |
danielk1977 | f29ce55 | 2002-05-19 23:43:12 +0000 | [diff] [blame] | 364 | if( sqliteCodeRowTrigger(pParse, TK_UPDATE, pChanges, TK_AFTER, pTab, |
danielk1977 | 6f34903 | 2002-06-11 02:25:40 +0000 | [diff] [blame] | 365 | newIdx, oldIdx, onError, addr) ){ |
danielk1977 | f29ce55 | 2002-05-19 23:43:12 +0000 | [diff] [blame] | 366 | goto update_cleanup; |
| 367 | } |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 368 | } |
| 369 | |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 370 | /* Repeat the above with the next record to be updated, until |
| 371 | ** all record selected by the WHERE clause have been updated. |
| 372 | */ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 373 | sqliteVdbeAddOp(v, OP_Goto, 0, addr); |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 374 | sqliteVdbeChangeP2(v, addr, sqliteVdbeCurrentAddr(v)); |
drh | a8b38d2 | 2001-11-01 14:41:34 +0000 | [diff] [blame] | 375 | sqliteVdbeAddOp(v, OP_ListReset, 0, 0); |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 376 | |
| 377 | /* Close all tables if there were no FOR EACH ROW triggers */ |
danielk1977 | f29ce55 | 2002-05-19 23:43:12 +0000 | [diff] [blame] | 378 | if( !row_triggers_exist ){ |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 379 | for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){ |
| 380 | if( openAll || aIdxUsed[i] ){ |
drh | 9adf9ac | 2002-05-15 11:44:13 +0000 | [diff] [blame] | 381 | sqliteVdbeAddOp(v, OP_Close, base+i+1, 0); |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 382 | } |
| 383 | } |
| 384 | sqliteVdbeAddOp(v, OP_Close, base, 0); |
| 385 | pParse->nTab = base; |
danielk1977 | f29ce55 | 2002-05-19 23:43:12 +0000 | [diff] [blame] | 386 | }else{ |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 387 | sqliteVdbeAddOp(v, OP_Close, newIdx, 0); |
| 388 | sqliteVdbeAddOp(v, OP_Close, oldIdx, 0); |
| 389 | } |
| 390 | |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 391 | sqliteEndWriteOperation(pParse); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 392 | |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 393 | /* |
| 394 | ** Return the number of rows that were changed. |
| 395 | */ |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 396 | if( db->flags & SQLITE_CountRows && !pParse->trigStack ){ |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 397 | sqliteVdbeAddOp(v, OP_ColumnName, 0, 0); |
| 398 | sqliteVdbeChangeP3(v, -1, "rows updated", P3_STATIC); |
| 399 | sqliteVdbeAddOp(v, OP_Callback, 1, 0); |
| 400 | } |
| 401 | |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 402 | update_cleanup: |
| 403 | sqliteFree(apIdx); |
| 404 | sqliteFree(aXRef); |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 405 | sqliteSrcListDelete(pTabList); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 406 | sqliteExprListDelete(pChanges); |
| 407 | sqliteExprDelete(pWhere); |
| 408 | return; |
| 409 | } |