blob: ce5d5b34671c2ebd7124e8a147984398b43c81c8 [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**
drh5a2c2c22001-11-21 02:21:11 +000015** $Id: update.c,v 1.22 2001/11/21 02:21:12 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 */
drhcce7d172000-05-31 15:34:51 +000043
drhdaffd0e2001-04-11 14:28:42 +000044 if( pParse->nErr || sqlite_malloc_failed ) goto update_cleanup;
drhecdc7532001-09-23 02:35:53 +000045 db = pParse->db;
drhdaffd0e2001-04-11 14:28:42 +000046
drhcce7d172000-05-31 15:34:51 +000047 /* Locate the table which we want to update. This table has to be
drh1ccde152000-06-17 13:12:39 +000048 ** put in an IdList structure because some of the subroutines we
drhcce7d172000-05-31 15:34:51 +000049 ** 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);
drhdaffd0e2001-04-11 14:28:42 +000053 if( pTabList==0 ) goto update_cleanup;
drhcce7d172000-05-31 15:34:51 +000054 for(i=0; i<pTabList->nId; i++){
drhecdc7532001-09-23 02:35:53 +000055 pTabList->a[i].pTab = sqliteFindTable(db, pTabList->a[i].zName);
drhcce7d172000-05-31 15:34:51 +000056 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
drh967e8b72000-06-21 13:59:10 +000074 /* 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.
drhcce7d172000-05-31 15:34:51 +000077 */
78 if( pWhere ){
drh4794b982000-06-06 13:54:14 +000079 sqliteExprResolveInSelect(pParse, pWhere);
80 }
81 for(i=0; i<pChanges->nExpr; i++){
82 sqliteExprResolveInSelect(pParse, pChanges->a[i].pExpr);
83 }
84 if( pWhere ){
drhcce7d172000-05-31 15:34:51 +000085 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++){
drh6206d502000-06-19 19:09:08 +0000100 if( sqliteStrICmp(pTab->aCol[j].zName, pChanges->a[i].zName)==0 ){
drhcce7d172000-05-31 15:34:51 +0000101 aXRef[j] = i;
102 break;
103 }
104 }
105 if( j>=pTab->nCol ){
drh967e8b72000-06-21 13:59:10 +0000106 sqliteSetString(&pParse->zErrMsg, "no such column: ",
drhcce7d172000-05-31 15:34:51 +0000107 pChanges->a[i].zName, 0);
108 pParse->nErr++;
109 goto update_cleanup;
110 }
111 }
112
drh5a2c2c22001-11-21 02:21:11 +0000113 /* Allocate memory for the array apIdx[] and fill it with pointers to every
drhcce7d172000-05-31 15:34:51 +0000114 ** index that needs to be updated. Indices only need updating if their
drh967e8b72000-06-21 13:59:10 +0000115 ** key includes one of the columns named in pChanges.
drhcce7d172000-05-31 15:34:51 +0000116 */
117 for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
drh967e8b72000-06-21 13:59:10 +0000118 for(i=0; i<pIdx->nColumn; i++){
119 if( aXRef[pIdx->aiColumn[i]]>=0 ) break;
drhcce7d172000-05-31 15:34:51 +0000120 }
drh967e8b72000-06-21 13:59:10 +0000121 if( i<pIdx->nColumn ) nIdx++;
drhcce7d172000-05-31 15:34:51 +0000122 }
drhb0729502001-03-14 12:35:57 +0000123 if( nIdx>0 ){
124 apIdx = sqliteMalloc( sizeof(Index*) * nIdx );
125 if( apIdx==0 ) goto update_cleanup;
126 }
drhcce7d172000-05-31 15:34:51 +0000127 for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
drh967e8b72000-06-21 13:59:10 +0000128 for(i=0; i<pIdx->nColumn; i++){
129 if( aXRef[pIdx->aiColumn[i]]>=0 ) break;
drhcce7d172000-05-31 15:34:51 +0000130 }
drh967e8b72000-06-21 13:59:10 +0000131 if( i<pIdx->nColumn ) apIdx[nIdx++] = pIdx;
drhcce7d172000-05-31 15:34:51 +0000132 }
133
134 /* Begin generating code.
135 */
drhd8bc7082000-06-07 23:51:50 +0000136 v = sqliteGetVdbe(pParse);
drhcce7d172000-05-31 15:34:51 +0000137 if( v==0 ) goto update_cleanup;
drhecdc7532001-09-23 02:35:53 +0000138 if( (db->flags & SQLITE_InTrans)==0 ){
drh99fcd712001-10-13 01:06:47 +0000139 sqliteVdbeAddOp(v, OP_Transaction, 0, 0);
140 sqliteVdbeAddOp(v, OP_VerifyCookie, db->schema_cookie, 0);
drhecdc7532001-09-23 02:35:53 +0000141 pParse->schemaVerified = 1;
drh5e00f6c2001-09-13 13:46:56 +0000142 }
drhcce7d172000-05-31 15:34:51 +0000143
144 /* Begin the database scan
145 */
drhcce7d172000-05-31 15:34:51 +0000146 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 */
drh99fcd712001-10-13 01:06:47 +0000151 sqliteVdbeAddOp(v, OP_ListWrite, 0, 0);
drhcce7d172000-05-31 15:34:51 +0000152
153 /* End the database scan loop.
154 */
155 sqliteWhereEnd(pWInfo);
156
drh1bee3d72001-10-15 00:44:35 +0000157 /* Initialize the count of updated rows
158 */
159 if( db->flags & SQLITE_CountRows ){
160 sqliteVdbeAddOp(v, OP_Integer, 0, 0);
161 }
162
drhcce7d172000-05-31 15:34:51 +0000163 /* Rewind the list of records that need to be updated and
164 ** open every index that needs updating.
165 */
drh99fcd712001-10-13 01:06:47 +0000166 sqliteVdbeAddOp(v, OP_ListRewind, 0, 0);
drh4794b982000-06-06 13:54:14 +0000167 base = pParse->nTab;
drhf57b3392001-10-08 13:22:32 +0000168 openOp = pTab->isTemp ? OP_OpenWrAux : OP_OpenWrite;
drh99fcd712001-10-13 01:06:47 +0000169 sqliteVdbeAddOp(v, openOp, base, pTab->tnum);
drhcce7d172000-05-31 15:34:51 +0000170 for(i=0; i<nIdx; i++){
drh99fcd712001-10-13 01:06:47 +0000171 sqliteVdbeAddOp(v, openOp, base+i+1, apIdx[i]->tnum);
drhcce7d172000-05-31 15:34:51 +0000172 }
173
174 /* Loop over every record that needs updating. We have to load
drh967e8b72000-06-21 13:59:10 +0000175 ** the old data for each record to be updated because some columns
drh1ccde152000-06-17 13:12:39 +0000176 ** might not change and we will need to copy the old value.
drhcce7d172000-05-31 15:34:51 +0000177 ** Also, the old data is needed to delete the old index entires.
178 */
179 end = sqliteVdbeMakeLabel(v);
drh99fcd712001-10-13 01:06:47 +0000180 addr = sqliteVdbeAddOp(v, OP_ListRead, 0, end);
181 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
182 sqliteVdbeAddOp(v, OP_MoveTo, base, 0);
drhcce7d172000-05-31 15:34:51 +0000183
184 /* Delete the old indices for the current record.
185 */
186 for(i=0; i<nIdx; i++){
drh99fcd712001-10-13 01:06:47 +0000187 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
drhcce7d172000-05-31 15:34:51 +0000188 pIdx = apIdx[i];
drh967e8b72000-06-21 13:59:10 +0000189 for(j=0; j<pIdx->nColumn; j++){
drh99fcd712001-10-13 01:06:47 +0000190 sqliteVdbeAddOp(v, OP_Column, base, pIdx->aiColumn[j]);
drhcce7d172000-05-31 15:34:51 +0000191 }
drh99fcd712001-10-13 01:06:47 +0000192 sqliteVdbeAddOp(v, OP_MakeIdxKey, pIdx->nColumn, 0);
drh8721ce42001-11-07 14:22:00 +0000193 sqliteVdbeAddOp(v, OP_IdxDelete, base+i+1, 0);
drhcce7d172000-05-31 15:34:51 +0000194 }
195
drh5a2c2c22001-11-21 02:21:11 +0000196 /* Compute new data for this record.
drhcce7d172000-05-31 15:34:51 +0000197 */
198 for(i=0; i<pTab->nCol; i++){
199 j = aXRef[i];
200 if( j<0 ){
drh99fcd712001-10-13 01:06:47 +0000201 sqliteVdbeAddOp(v, OP_Column, base, i);
drhcce7d172000-05-31 15:34:51 +0000202 }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++){
drh99fcd712001-10-13 01:06:47 +0000210 sqliteVdbeAddOp(v, OP_Dup, pTab->nCol, 0); /* The KEY */
drhcce7d172000-05-31 15:34:51 +0000211 pIdx = apIdx[i];
drh967e8b72000-06-21 13:59:10 +0000212 for(j=0; j<pIdx->nColumn; j++){
drh99fcd712001-10-13 01:06:47 +0000213 sqliteVdbeAddOp(v, OP_Dup, j+pTab->nCol-pIdx->aiColumn[j], 0);
drhcce7d172000-05-31 15:34:51 +0000214 }
drh99fcd712001-10-13 01:06:47 +0000215 sqliteVdbeAddOp(v, OP_MakeIdxKey, pIdx->nColumn, 0);
drh8721ce42001-11-07 14:22:00 +0000216 sqliteVdbeAddOp(v, OP_IdxPut, base+i+1, pIdx->isUnique);
drhcce7d172000-05-31 15:34:51 +0000217 }
218
219 /* Write the new data back into the database.
220 */
drh99fcd712001-10-13 01:06:47 +0000221 sqliteVdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0);
222 sqliteVdbeAddOp(v, OP_Put, base, 0);
drhcce7d172000-05-31 15:34:51 +0000223
drh1bee3d72001-10-15 00:44:35 +0000224 /* 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
drhcce7d172000-05-31 15:34:51 +0000230 /* Repeat the above with the next record to be updated, until
231 ** all record selected by the WHERE clause have been updated.
232 */
drh99fcd712001-10-13 01:06:47 +0000233 sqliteVdbeAddOp(v, OP_Goto, 0, addr);
234 sqliteVdbeResolveLabel(v, end);
drha8b38d22001-11-01 14:41:34 +0000235 sqliteVdbeAddOp(v, OP_ListReset, 0, 0);
drhecdc7532001-09-23 02:35:53 +0000236 if( (db->flags & SQLITE_InTrans)==0 ){
drh99fcd712001-10-13 01:06:47 +0000237 sqliteVdbeAddOp(v, OP_Commit, 0, 0);
drh5e00f6c2001-09-13 13:46:56 +0000238 }
drhcce7d172000-05-31 15:34:51 +0000239
drh1bee3d72001-10-15 00:44:35 +0000240 /*
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
drhcce7d172000-05-31 15:34:51 +0000250update_cleanup:
251 sqliteFree(apIdx);
252 sqliteFree(aXRef);
253 sqliteIdListDelete(pTabList);
254 sqliteExprListDelete(pChanges);
255 sqliteExprDelete(pWhere);
256 return;
257}