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 | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame^] | 15 | ** $Id: update.c,v 1.18 2001/10/13 01:06:48 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 */ |
| 26 | Expr *pWhere /* The WHERE clause. May be null */ |
| 27 | ){ |
| 28 | int i, j; /* Loop counters */ |
| 29 | Table *pTab; /* The table to be updated */ |
| 30 | IdList *pTabList = 0; /* List containing only pTab */ |
| 31 | int end, addr; /* A couple of addresses in the generated code */ |
| 32 | WhereInfo *pWInfo; /* Information about the WHERE clause */ |
| 33 | Vdbe *v; /* The virtual database engine */ |
| 34 | Index *pIdx; /* For looping over indices */ |
| 35 | int nIdx; /* Number of indices that need updating */ |
drh | 4794b98 | 2000-06-06 13:54:14 +0000 | [diff] [blame] | 36 | int base; /* Index of first available table cursor */ |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 37 | sqlite *db; /* The database structure */ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 38 | Index **apIdx = 0; /* An array of indices that need updating too */ |
| 39 | int *aXRef = 0; /* aXRef[i] is the index in pChanges->a[] of the |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 40 | ** an expression for the i-th column of the table. |
| 41 | ** aXRef[i]==-1 if the i-th column is not changed. */ |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 42 | int openOp; /* Opcode used to open tables */ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 43 | |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 44 | if( pParse->nErr || sqlite_malloc_failed ) goto update_cleanup; |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 45 | db = pParse->db; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 46 | |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 47 | /* Locate the table which we want to update. This table has to be |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 48 | ** put in an IdList structure because some of the subroutines we |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 49 | ** will be calling are designed to work with multiple tables and expect |
| 50 | ** an IdList* parameter instead of just a Table* parameger. |
| 51 | */ |
| 52 | pTabList = sqliteIdListAppend(0, pTableName); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 53 | if( pTabList==0 ) goto update_cleanup; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 54 | for(i=0; i<pTabList->nId; i++){ |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 55 | pTabList->a[i].pTab = sqliteFindTable(db, pTabList->a[i].zName); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 56 | if( pTabList->a[i].pTab==0 ){ |
| 57 | sqliteSetString(&pParse->zErrMsg, "no such table: ", |
| 58 | pTabList->a[i].zName, 0); |
| 59 | pParse->nErr++; |
| 60 | goto update_cleanup; |
| 61 | } |
| 62 | if( pTabList->a[i].pTab->readOnly ){ |
| 63 | sqliteSetString(&pParse->zErrMsg, "table ", pTabList->a[i].zName, |
| 64 | " may not be modified", 0); |
| 65 | pParse->nErr++; |
| 66 | goto update_cleanup; |
| 67 | } |
| 68 | } |
| 69 | pTab = pTabList->a[0].pTab; |
| 70 | aXRef = sqliteMalloc( sizeof(int) * pTab->nCol ); |
| 71 | if( aXRef==0 ) goto update_cleanup; |
| 72 | for(i=0; i<pTab->nCol; i++) aXRef[i] = -1; |
| 73 | |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 74 | /* Resolve the column names in all the expressions in both the |
| 75 | ** WHERE clause and in the new values. Also find the column index |
| 76 | ** for each column to be updated in the pChanges array. |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 77 | */ |
| 78 | if( pWhere ){ |
drh | 4794b98 | 2000-06-06 13:54:14 +0000 | [diff] [blame] | 79 | sqliteExprResolveInSelect(pParse, pWhere); |
| 80 | } |
| 81 | for(i=0; i<pChanges->nExpr; i++){ |
| 82 | sqliteExprResolveInSelect(pParse, pChanges->a[i].pExpr); |
| 83 | } |
| 84 | if( pWhere ){ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 85 | if( sqliteExprResolveIds(pParse, pTabList, pWhere) ){ |
| 86 | goto update_cleanup; |
| 87 | } |
| 88 | if( sqliteExprCheck(pParse, pWhere, 0, 0) ){ |
| 89 | goto update_cleanup; |
| 90 | } |
| 91 | } |
| 92 | for(i=0; i<pChanges->nExpr; i++){ |
| 93 | if( sqliteExprResolveIds(pParse, pTabList, pChanges->a[i].pExpr) ){ |
| 94 | goto update_cleanup; |
| 95 | } |
| 96 | if( sqliteExprCheck(pParse, pChanges->a[i].pExpr, 0, 0) ){ |
| 97 | goto update_cleanup; |
| 98 | } |
| 99 | for(j=0; j<pTab->nCol; j++){ |
drh | 6206d50 | 2000-06-19 19:09:08 +0000 | [diff] [blame] | 100 | if( sqliteStrICmp(pTab->aCol[j].zName, pChanges->a[i].zName)==0 ){ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 101 | aXRef[j] = i; |
| 102 | break; |
| 103 | } |
| 104 | } |
| 105 | if( j>=pTab->nCol ){ |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 106 | sqliteSetString(&pParse->zErrMsg, "no such column: ", |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 107 | pChanges->a[i].zName, 0); |
| 108 | pParse->nErr++; |
| 109 | goto update_cleanup; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | /* Allocate memory for the array apIdx[] and fill it pointers to every |
| 114 | ** index that needs to be updated. Indices only need updating if their |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 115 | ** key includes one of the columns named in pChanges. |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 116 | */ |
| 117 | for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 118 | for(i=0; i<pIdx->nColumn; i++){ |
| 119 | if( aXRef[pIdx->aiColumn[i]]>=0 ) break; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 120 | } |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 121 | if( i<pIdx->nColumn ) nIdx++; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 122 | } |
drh | b072950 | 2001-03-14 12:35:57 +0000 | [diff] [blame] | 123 | if( nIdx>0 ){ |
| 124 | apIdx = sqliteMalloc( sizeof(Index*) * nIdx ); |
| 125 | if( apIdx==0 ) goto update_cleanup; |
| 126 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 127 | for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 128 | for(i=0; i<pIdx->nColumn; i++){ |
| 129 | if( aXRef[pIdx->aiColumn[i]]>=0 ) break; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 130 | } |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 131 | if( i<pIdx->nColumn ) apIdx[nIdx++] = pIdx; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | /* Begin generating code. |
| 135 | */ |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 136 | v = sqliteGetVdbe(pParse); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 137 | if( v==0 ) goto update_cleanup; |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 138 | if( (db->flags & SQLITE_InTrans)==0 ){ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame^] | 139 | sqliteVdbeAddOp(v, OP_Transaction, 0, 0); |
| 140 | sqliteVdbeAddOp(v, OP_VerifyCookie, db->schema_cookie, 0); |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 141 | pParse->schemaVerified = 1; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 142 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 143 | |
| 144 | /* Begin the database scan |
| 145 | */ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame^] | 146 | sqliteVdbeAddOp(v, OP_ListOpen, 0, 0); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 147 | pWInfo = sqliteWhereBegin(pParse, pTabList, pWhere, 1); |
| 148 | if( pWInfo==0 ) goto update_cleanup; |
| 149 | |
| 150 | /* Remember the index of every item to be updated. |
| 151 | */ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame^] | 152 | sqliteVdbeAddOp(v, OP_ListWrite, 0, 0); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 153 | |
| 154 | /* End the database scan loop. |
| 155 | */ |
| 156 | sqliteWhereEnd(pWInfo); |
| 157 | |
| 158 | /* Rewind the list of records that need to be updated and |
| 159 | ** open every index that needs updating. |
| 160 | */ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame^] | 161 | sqliteVdbeAddOp(v, OP_ListRewind, 0, 0); |
drh | 4794b98 | 2000-06-06 13:54:14 +0000 | [diff] [blame] | 162 | base = pParse->nTab; |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 163 | openOp = pTab->isTemp ? OP_OpenWrAux : OP_OpenWrite; |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame^] | 164 | sqliteVdbeAddOp(v, openOp, base, pTab->tnum); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 165 | for(i=0; i<nIdx; i++){ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame^] | 166 | sqliteVdbeAddOp(v, openOp, base+i+1, apIdx[i]->tnum); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | /* Loop over every record that needs updating. We have to load |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 170 | ** the old data for each record to be updated because some columns |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 171 | ** might not change and we will need to copy the old value. |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 172 | ** Also, the old data is needed to delete the old index entires. |
| 173 | */ |
| 174 | end = sqliteVdbeMakeLabel(v); |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame^] | 175 | addr = sqliteVdbeAddOp(v, OP_ListRead, 0, end); |
| 176 | sqliteVdbeAddOp(v, OP_Dup, 0, 0); |
| 177 | sqliteVdbeAddOp(v, OP_MoveTo, base, 0); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 178 | |
| 179 | /* Delete the old indices for the current record. |
| 180 | */ |
| 181 | for(i=0; i<nIdx; i++){ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame^] | 182 | sqliteVdbeAddOp(v, OP_Dup, 0, 0); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 183 | pIdx = apIdx[i]; |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 184 | for(j=0; j<pIdx->nColumn; j++){ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame^] | 185 | sqliteVdbeAddOp(v, OP_Column, base, pIdx->aiColumn[j]); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 186 | } |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame^] | 187 | sqliteVdbeAddOp(v, OP_MakeIdxKey, pIdx->nColumn, 0); |
| 188 | sqliteVdbeAddOp(v, OP_DeleteIdx, base+i+1, 0); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | /* Compute a completely new data for this record. |
| 192 | */ |
| 193 | for(i=0; i<pTab->nCol; i++){ |
| 194 | j = aXRef[i]; |
| 195 | if( j<0 ){ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame^] | 196 | sqliteVdbeAddOp(v, OP_Column, base, i); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 197 | }else{ |
| 198 | sqliteExprCode(pParse, pChanges->a[j].pExpr); |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | /* Insert new index entries that correspond to the new data |
| 203 | */ |
| 204 | for(i=0; i<nIdx; i++){ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame^] | 205 | sqliteVdbeAddOp(v, OP_Dup, pTab->nCol, 0); /* The KEY */ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 206 | pIdx = apIdx[i]; |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 207 | for(j=0; j<pIdx->nColumn; j++){ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame^] | 208 | sqliteVdbeAddOp(v, OP_Dup, j+pTab->nCol-pIdx->aiColumn[j], 0); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 209 | } |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame^] | 210 | sqliteVdbeAddOp(v, OP_MakeIdxKey, pIdx->nColumn, 0); |
| 211 | sqliteVdbeAddOp(v, OP_PutIdx, base+i+1, pIdx->isUnique); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | /* Write the new data back into the database. |
| 215 | */ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame^] | 216 | sqliteVdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0); |
| 217 | sqliteVdbeAddOp(v, OP_Put, base, 0); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 218 | |
| 219 | /* Repeat the above with the next record to be updated, until |
| 220 | ** all record selected by the WHERE clause have been updated. |
| 221 | */ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame^] | 222 | sqliteVdbeAddOp(v, OP_Goto, 0, addr); |
| 223 | sqliteVdbeResolveLabel(v, end); |
| 224 | sqliteVdbeAddOp(v, OP_ListClose, 0, 0); |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 225 | if( (db->flags & SQLITE_InTrans)==0 ){ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame^] | 226 | sqliteVdbeAddOp(v, OP_Commit, 0, 0); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 227 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 228 | |
| 229 | update_cleanup: |
| 230 | sqliteFree(apIdx); |
| 231 | sqliteFree(aXRef); |
| 232 | sqliteIdListDelete(pTabList); |
| 233 | sqliteExprListDelete(pChanges); |
| 234 | sqliteExprDelete(pWhere); |
| 235 | return; |
| 236 | } |