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