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 | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame^] | 15 | ** $Id: update.c,v 1.21 2001/11/07 14:22:00 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 | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 146 | pWInfo = sqliteWhereBegin(pParse, pTabList, pWhere, 1); |
| 147 | if( pWInfo==0 ) goto update_cleanup; |
| 148 | |
| 149 | /* Remember the index of every item to be updated. |
| 150 | */ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 151 | sqliteVdbeAddOp(v, OP_ListWrite, 0, 0); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 152 | |
| 153 | /* End the database scan loop. |
| 154 | */ |
| 155 | sqliteWhereEnd(pWInfo); |
| 156 | |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 157 | /* Initialize the count of updated rows |
| 158 | */ |
| 159 | if( db->flags & SQLITE_CountRows ){ |
| 160 | sqliteVdbeAddOp(v, OP_Integer, 0, 0); |
| 161 | } |
| 162 | |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 163 | /* Rewind the list of records that need to be updated and |
| 164 | ** open every index that needs updating. |
| 165 | */ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 166 | sqliteVdbeAddOp(v, OP_ListRewind, 0, 0); |
drh | 4794b98 | 2000-06-06 13:54:14 +0000 | [diff] [blame] | 167 | base = pParse->nTab; |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 168 | openOp = pTab->isTemp ? OP_OpenWrAux : OP_OpenWrite; |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 169 | sqliteVdbeAddOp(v, openOp, base, pTab->tnum); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 170 | for(i=0; i<nIdx; i++){ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 171 | sqliteVdbeAddOp(v, openOp, base+i+1, apIdx[i]->tnum); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | /* Loop over every record that needs updating. We have to load |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 175 | ** the old data for each record to be updated because some columns |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 176 | ** might not change and we will need to copy the old value. |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 177 | ** Also, the old data is needed to delete the old index entires. |
| 178 | */ |
| 179 | end = sqliteVdbeMakeLabel(v); |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 180 | addr = sqliteVdbeAddOp(v, OP_ListRead, 0, end); |
| 181 | sqliteVdbeAddOp(v, OP_Dup, 0, 0); |
| 182 | sqliteVdbeAddOp(v, OP_MoveTo, base, 0); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 183 | |
| 184 | /* Delete the old indices for the current record. |
| 185 | */ |
| 186 | for(i=0; i<nIdx; i++){ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 187 | sqliteVdbeAddOp(v, OP_Dup, 0, 0); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 188 | pIdx = apIdx[i]; |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 189 | for(j=0; j<pIdx->nColumn; j++){ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 190 | sqliteVdbeAddOp(v, OP_Column, base, pIdx->aiColumn[j]); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 191 | } |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 192 | sqliteVdbeAddOp(v, OP_MakeIdxKey, pIdx->nColumn, 0); |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame^] | 193 | sqliteVdbeAddOp(v, OP_IdxDelete, base+i+1, 0); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | /* Compute a completely new data for this record. |
| 197 | */ |
| 198 | for(i=0; i<pTab->nCol; i++){ |
| 199 | j = aXRef[i]; |
| 200 | if( j<0 ){ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 201 | sqliteVdbeAddOp(v, OP_Column, base, i); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 202 | }else{ |
| 203 | sqliteExprCode(pParse, pChanges->a[j].pExpr); |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | /* Insert new index entries that correspond to the new data |
| 208 | */ |
| 209 | for(i=0; i<nIdx; i++){ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 210 | sqliteVdbeAddOp(v, OP_Dup, pTab->nCol, 0); /* The KEY */ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 211 | pIdx = apIdx[i]; |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 212 | for(j=0; j<pIdx->nColumn; j++){ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 213 | sqliteVdbeAddOp(v, OP_Dup, j+pTab->nCol-pIdx->aiColumn[j], 0); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 214 | } |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 215 | sqliteVdbeAddOp(v, OP_MakeIdxKey, pIdx->nColumn, 0); |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame^] | 216 | sqliteVdbeAddOp(v, OP_IdxPut, base+i+1, pIdx->isUnique); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | /* Write the new data back into the database. |
| 220 | */ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 221 | sqliteVdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0); |
| 222 | sqliteVdbeAddOp(v, OP_Put, base, 0); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 223 | |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 224 | /* Increment the count of rows affected by the update |
| 225 | */ |
| 226 | if( db->flags & SQLITE_CountRows ){ |
| 227 | sqliteVdbeAddOp(v, OP_AddImm, 1, 0); |
| 228 | } |
| 229 | |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 230 | /* Repeat the above with the next record to be updated, until |
| 231 | ** all record selected by the WHERE clause have been updated. |
| 232 | */ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 233 | sqliteVdbeAddOp(v, OP_Goto, 0, addr); |
| 234 | sqliteVdbeResolveLabel(v, end); |
drh | a8b38d2 | 2001-11-01 14:41:34 +0000 | [diff] [blame] | 235 | sqliteVdbeAddOp(v, OP_ListReset, 0, 0); |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 236 | if( (db->flags & SQLITE_InTrans)==0 ){ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 237 | sqliteVdbeAddOp(v, OP_Commit, 0, 0); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 238 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 239 | |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 240 | /* |
| 241 | ** Return the number of rows that were changed. |
| 242 | */ |
| 243 | if( db->flags & SQLITE_CountRows ){ |
| 244 | sqliteVdbeAddOp(v, OP_ColumnCount, 1, 0); |
| 245 | sqliteVdbeAddOp(v, OP_ColumnName, 0, 0); |
| 246 | sqliteVdbeChangeP3(v, -1, "rows updated", P3_STATIC); |
| 247 | sqliteVdbeAddOp(v, OP_Callback, 1, 0); |
| 248 | } |
| 249 | |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 250 | update_cleanup: |
| 251 | sqliteFree(apIdx); |
| 252 | sqliteFree(aXRef); |
| 253 | sqliteIdListDelete(pTabList); |
| 254 | sqliteExprListDelete(pChanges); |
| 255 | sqliteExprDelete(pWhere); |
| 256 | return; |
| 257 | } |