blob: 4c2bbd96b3602f68a6f380dfc3d19a5674a52bc1 [file] [log] [blame]
drhcce7d172000-05-31 15:34:51 +00001/*
drhb19a2bc2001-09-16 00:13:26 +00002** 2001 September 15
drhcce7d172000-05-31 15:34:51 +00003**
drhb19a2bc2001-09-16 00:13:26 +00004** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
drhcce7d172000-05-31 15:34:51 +00006**
drhb19a2bc2001-09-16 00:13:26 +00007** 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.
drhcce7d172000-05-31 15:34:51 +000010**
11*************************************************************************
12** This file contains C code routines that are called by the parser
13** to handle UPDATE statements.
14**
drh4a324312001-12-21 14:30:42 +000015** $Id: update.c,v 1.23 2001/12/21 14:30:43 drh Exp $
drhcce7d172000-05-31 15:34:51 +000016*/
17#include "sqliteInt.h"
18
19/*
20** Process an UPDATE statement.
21*/
22void 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 */
drh4794b982000-06-06 13:54:14 +000036 int base; /* Index of first available table cursor */
drhecdc7532001-09-23 02:35:53 +000037 sqlite *db; /* The database structure */
drhcce7d172000-05-31 15:34:51 +000038 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
drh967e8b72000-06-21 13:59:10 +000040 ** an expression for the i-th column of the table.
41 ** aXRef[i]==-1 if the i-th column is not changed. */
drhf57b3392001-10-08 13:22:32 +000042 int openOp; /* Opcode used to open tables */
drh4a324312001-12-21 14:30:42 +000043 int chngRecno; /* True if the record number is being changed */
44 Expr *pRecnoExpr; /* Expression defining the new record number */
drhcce7d172000-05-31 15:34:51 +000045
drhdaffd0e2001-04-11 14:28:42 +000046 if( pParse->nErr || sqlite_malloc_failed ) goto update_cleanup;
drhecdc7532001-09-23 02:35:53 +000047 db = pParse->db;
drhdaffd0e2001-04-11 14:28:42 +000048
drhcce7d172000-05-31 15:34:51 +000049 /* Locate the table which we want to update. This table has to be
drh1ccde152000-06-17 13:12:39 +000050 ** put in an IdList structure because some of the subroutines we
drhcce7d172000-05-31 15:34:51 +000051 ** will be calling are designed to work with multiple tables and expect
52 ** an IdList* parameter instead of just a Table* parameger.
53 */
54 pTabList = sqliteIdListAppend(0, pTableName);
drhdaffd0e2001-04-11 14:28:42 +000055 if( pTabList==0 ) goto update_cleanup;
drhcce7d172000-05-31 15:34:51 +000056 for(i=0; i<pTabList->nId; i++){
drhecdc7532001-09-23 02:35:53 +000057 pTabList->a[i].pTab = sqliteFindTable(db, pTabList->a[i].zName);
drhcce7d172000-05-31 15:34:51 +000058 if( pTabList->a[i].pTab==0 ){
59 sqliteSetString(&pParse->zErrMsg, "no such table: ",
60 pTabList->a[i].zName, 0);
61 pParse->nErr++;
62 goto update_cleanup;
63 }
64 if( pTabList->a[i].pTab->readOnly ){
65 sqliteSetString(&pParse->zErrMsg, "table ", pTabList->a[i].zName,
66 " may not be modified", 0);
67 pParse->nErr++;
68 goto update_cleanup;
69 }
70 }
71 pTab = pTabList->a[0].pTab;
72 aXRef = sqliteMalloc( sizeof(int) * pTab->nCol );
73 if( aXRef==0 ) goto update_cleanup;
74 for(i=0; i<pTab->nCol; i++) aXRef[i] = -1;
75
drh967e8b72000-06-21 13:59:10 +000076 /* Resolve the column names in all the expressions in both the
77 ** WHERE clause and in the new values. Also find the column index
78 ** for each column to be updated in the pChanges array.
drhcce7d172000-05-31 15:34:51 +000079 */
80 if( pWhere ){
drh4794b982000-06-06 13:54:14 +000081 sqliteExprResolveInSelect(pParse, pWhere);
82 }
83 for(i=0; i<pChanges->nExpr; i++){
84 sqliteExprResolveInSelect(pParse, pChanges->a[i].pExpr);
85 }
86 if( pWhere ){
drhcce7d172000-05-31 15:34:51 +000087 if( sqliteExprResolveIds(pParse, pTabList, pWhere) ){
88 goto update_cleanup;
89 }
90 if( sqliteExprCheck(pParse, pWhere, 0, 0) ){
91 goto update_cleanup;
92 }
93 }
drh4a324312001-12-21 14:30:42 +000094 chngRecno = 0;
drhcce7d172000-05-31 15:34:51 +000095 for(i=0; i<pChanges->nExpr; i++){
96 if( sqliteExprResolveIds(pParse, pTabList, pChanges->a[i].pExpr) ){
97 goto update_cleanup;
98 }
99 if( sqliteExprCheck(pParse, pChanges->a[i].pExpr, 0, 0) ){
100 goto update_cleanup;
101 }
102 for(j=0; j<pTab->nCol; j++){
drh6206d502000-06-19 19:09:08 +0000103 if( sqliteStrICmp(pTab->aCol[j].zName, pChanges->a[i].zName)==0 ){
drh4a324312001-12-21 14:30:42 +0000104 if( i==pTab->iPKey ){
105 chngRecno = 1;
106 pRecnoExpr = pChanges->a[i].pExpr;
107 }
drhcce7d172000-05-31 15:34:51 +0000108 aXRef[j] = i;
109 break;
110 }
111 }
112 if( j>=pTab->nCol ){
drh967e8b72000-06-21 13:59:10 +0000113 sqliteSetString(&pParse->zErrMsg, "no such column: ",
drhcce7d172000-05-31 15:34:51 +0000114 pChanges->a[i].zName, 0);
115 pParse->nErr++;
116 goto update_cleanup;
117 }
118 }
119
drh5a2c2c22001-11-21 02:21:11 +0000120 /* Allocate memory for the array apIdx[] and fill it with pointers to every
drhcce7d172000-05-31 15:34:51 +0000121 ** index that needs to be updated. Indices only need updating if their
drh967e8b72000-06-21 13:59:10 +0000122 ** key includes one of the columns named in pChanges.
drhcce7d172000-05-31 15:34:51 +0000123 */
124 for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
drh4a324312001-12-21 14:30:42 +0000125 if( chngRecno ){
126 i = 0;
127 }else {
128 for(i=0; i<pIdx->nColumn; i++){
129 if( aXRef[pIdx->aiColumn[i]]>=0 ) break;
130 }
drhcce7d172000-05-31 15:34:51 +0000131 }
drh967e8b72000-06-21 13:59:10 +0000132 if( i<pIdx->nColumn ) nIdx++;
drhcce7d172000-05-31 15:34:51 +0000133 }
drhb0729502001-03-14 12:35:57 +0000134 if( nIdx>0 ){
135 apIdx = sqliteMalloc( sizeof(Index*) * nIdx );
136 if( apIdx==0 ) goto update_cleanup;
137 }
drhcce7d172000-05-31 15:34:51 +0000138 for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
drh4a324312001-12-21 14:30:42 +0000139 if( chngRecno ){
140 i = 0;
141 }else{
142 for(i=0; i<pIdx->nColumn; i++){
143 if( aXRef[pIdx->aiColumn[i]]>=0 ) break;
144 }
drhcce7d172000-05-31 15:34:51 +0000145 }
drh967e8b72000-06-21 13:59:10 +0000146 if( i<pIdx->nColumn ) apIdx[nIdx++] = pIdx;
drhcce7d172000-05-31 15:34:51 +0000147 }
148
149 /* Begin generating code.
150 */
drhd8bc7082000-06-07 23:51:50 +0000151 v = sqliteGetVdbe(pParse);
drhcce7d172000-05-31 15:34:51 +0000152 if( v==0 ) goto update_cleanup;
drhecdc7532001-09-23 02:35:53 +0000153 if( (db->flags & SQLITE_InTrans)==0 ){
drh99fcd712001-10-13 01:06:47 +0000154 sqliteVdbeAddOp(v, OP_Transaction, 0, 0);
155 sqliteVdbeAddOp(v, OP_VerifyCookie, db->schema_cookie, 0);
drhecdc7532001-09-23 02:35:53 +0000156 pParse->schemaVerified = 1;
drh5e00f6c2001-09-13 13:46:56 +0000157 }
drhcce7d172000-05-31 15:34:51 +0000158
159 /* Begin the database scan
160 */
drhcce7d172000-05-31 15:34:51 +0000161 pWInfo = sqliteWhereBegin(pParse, pTabList, pWhere, 1);
162 if( pWInfo==0 ) goto update_cleanup;
163
164 /* Remember the index of every item to be updated.
165 */
drh99fcd712001-10-13 01:06:47 +0000166 sqliteVdbeAddOp(v, OP_ListWrite, 0, 0);
drhcce7d172000-05-31 15:34:51 +0000167
168 /* End the database scan loop.
169 */
170 sqliteWhereEnd(pWInfo);
171
drh1bee3d72001-10-15 00:44:35 +0000172 /* Initialize the count of updated rows
173 */
174 if( db->flags & SQLITE_CountRows ){
175 sqliteVdbeAddOp(v, OP_Integer, 0, 0);
176 }
177
drhcce7d172000-05-31 15:34:51 +0000178 /* Rewind the list of records that need to be updated and
179 ** open every index that needs updating.
180 */
drh99fcd712001-10-13 01:06:47 +0000181 sqliteVdbeAddOp(v, OP_ListRewind, 0, 0);
drh4794b982000-06-06 13:54:14 +0000182 base = pParse->nTab;
drhf57b3392001-10-08 13:22:32 +0000183 openOp = pTab->isTemp ? OP_OpenWrAux : OP_OpenWrite;
drh99fcd712001-10-13 01:06:47 +0000184 sqliteVdbeAddOp(v, openOp, base, pTab->tnum);
drhcce7d172000-05-31 15:34:51 +0000185 for(i=0; i<nIdx; i++){
drh99fcd712001-10-13 01:06:47 +0000186 sqliteVdbeAddOp(v, openOp, base+i+1, apIdx[i]->tnum);
drhcce7d172000-05-31 15:34:51 +0000187 }
188
189 /* Loop over every record that needs updating. We have to load
drh967e8b72000-06-21 13:59:10 +0000190 ** the old data for each record to be updated because some columns
drh1ccde152000-06-17 13:12:39 +0000191 ** might not change and we will need to copy the old value.
drhcce7d172000-05-31 15:34:51 +0000192 ** Also, the old data is needed to delete the old index entires.
drh4a324312001-12-21 14:30:42 +0000193 ** So make the cursor point at the old record.
drhcce7d172000-05-31 15:34:51 +0000194 */
195 end = sqliteVdbeMakeLabel(v);
drh99fcd712001-10-13 01:06:47 +0000196 addr = sqliteVdbeAddOp(v, OP_ListRead, 0, end);
197 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
198 sqliteVdbeAddOp(v, OP_MoveTo, base, 0);
drhcce7d172000-05-31 15:34:51 +0000199
200 /* Delete the old indices for the current record.
201 */
202 for(i=0; i<nIdx; i++){
drh99fcd712001-10-13 01:06:47 +0000203 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
drhcce7d172000-05-31 15:34:51 +0000204 pIdx = apIdx[i];
drh967e8b72000-06-21 13:59:10 +0000205 for(j=0; j<pIdx->nColumn; j++){
drh99fcd712001-10-13 01:06:47 +0000206 sqliteVdbeAddOp(v, OP_Column, base, pIdx->aiColumn[j]);
drhcce7d172000-05-31 15:34:51 +0000207 }
drh99fcd712001-10-13 01:06:47 +0000208 sqliteVdbeAddOp(v, OP_MakeIdxKey, pIdx->nColumn, 0);
drh8721ce42001-11-07 14:22:00 +0000209 sqliteVdbeAddOp(v, OP_IdxDelete, base+i+1, 0);
drhcce7d172000-05-31 15:34:51 +0000210 }
211
drh4a324312001-12-21 14:30:42 +0000212 /* If changing the record number, remove the old record number
213 ** from the top of the stack and replace it with the new one.
214 */
215 if( chngRecno ){
216 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
217 sqliteExprCode(pParse, pRecnoExpr);
218 sqliteVdbeAddOp(v, OP_AddImm, 0, 0);
219 }
220
drh5a2c2c22001-11-21 02:21:11 +0000221 /* Compute new data for this record.
drhcce7d172000-05-31 15:34:51 +0000222 */
223 for(i=0; i<pTab->nCol; i++){
drh4a324312001-12-21 14:30:42 +0000224 if( i==pTab->iPKey ){
225 sqliteVdbeAddOp(v, OP_Dup, i, 0);
226 continue;
227 }
drhcce7d172000-05-31 15:34:51 +0000228 j = aXRef[i];
229 if( j<0 ){
drh99fcd712001-10-13 01:06:47 +0000230 sqliteVdbeAddOp(v, OP_Column, base, i);
drhcce7d172000-05-31 15:34:51 +0000231 }else{
232 sqliteExprCode(pParse, pChanges->a[j].pExpr);
233 }
234 }
235
drh4a324312001-12-21 14:30:42 +0000236 /* If changing the record number, delete the hold record.
237 */
238 if( chngRecno ){
239 sqliteVdbeAddOp(v, OP_Delete, 0, 0);
240 }
241
drhcce7d172000-05-31 15:34:51 +0000242 /* Insert new index entries that correspond to the new data
243 */
244 for(i=0; i<nIdx; i++){
drh99fcd712001-10-13 01:06:47 +0000245 sqliteVdbeAddOp(v, OP_Dup, pTab->nCol, 0); /* The KEY */
drhcce7d172000-05-31 15:34:51 +0000246 pIdx = apIdx[i];
drh967e8b72000-06-21 13:59:10 +0000247 for(j=0; j<pIdx->nColumn; j++){
drh4a324312001-12-21 14:30:42 +0000248 int idx = pIdx->aiColumn[j];
249 if( idx==pTab->iPKey ){
250 sqliteVdbeAddOp(v, OP_Dup, j, 0);
251 }else{
252 sqliteVdbeAddOp(v, OP_Dup, j+pTab->nCol-idx, 0);
253 }
drhcce7d172000-05-31 15:34:51 +0000254 }
drh99fcd712001-10-13 01:06:47 +0000255 sqliteVdbeAddOp(v, OP_MakeIdxKey, pIdx->nColumn, 0);
drh8721ce42001-11-07 14:22:00 +0000256 sqliteVdbeAddOp(v, OP_IdxPut, base+i+1, pIdx->isUnique);
drhcce7d172000-05-31 15:34:51 +0000257 }
258
259 /* Write the new data back into the database.
260 */
drh99fcd712001-10-13 01:06:47 +0000261 sqliteVdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0);
262 sqliteVdbeAddOp(v, OP_Put, base, 0);
drhcce7d172000-05-31 15:34:51 +0000263
drh1bee3d72001-10-15 00:44:35 +0000264 /* Increment the count of rows affected by the update
265 */
266 if( db->flags & SQLITE_CountRows ){
267 sqliteVdbeAddOp(v, OP_AddImm, 1, 0);
268 }
269
drhcce7d172000-05-31 15:34:51 +0000270 /* Repeat the above with the next record to be updated, until
271 ** all record selected by the WHERE clause have been updated.
272 */
drh99fcd712001-10-13 01:06:47 +0000273 sqliteVdbeAddOp(v, OP_Goto, 0, addr);
274 sqliteVdbeResolveLabel(v, end);
drha8b38d22001-11-01 14:41:34 +0000275 sqliteVdbeAddOp(v, OP_ListReset, 0, 0);
drhecdc7532001-09-23 02:35:53 +0000276 if( (db->flags & SQLITE_InTrans)==0 ){
drh99fcd712001-10-13 01:06:47 +0000277 sqliteVdbeAddOp(v, OP_Commit, 0, 0);
drh5e00f6c2001-09-13 13:46:56 +0000278 }
drhcce7d172000-05-31 15:34:51 +0000279
drh1bee3d72001-10-15 00:44:35 +0000280 /*
281 ** Return the number of rows that were changed.
282 */
283 if( db->flags & SQLITE_CountRows ){
284 sqliteVdbeAddOp(v, OP_ColumnCount, 1, 0);
285 sqliteVdbeAddOp(v, OP_ColumnName, 0, 0);
286 sqliteVdbeChangeP3(v, -1, "rows updated", P3_STATIC);
287 sqliteVdbeAddOp(v, OP_Callback, 1, 0);
288 }
289
drhcce7d172000-05-31 15:34:51 +0000290update_cleanup:
291 sqliteFree(apIdx);
292 sqliteFree(aXRef);
293 sqliteIdListDelete(pTabList);
294 sqliteExprListDelete(pChanges);
295 sqliteExprDelete(pWhere);
296 return;
297}