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