blob: 7647d4a7e2e6f275bf718cf20799460c78d71102 [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**
drh633e6d52008-07-28 19:34:53 +000015** $Id: update.c,v 1.181 2008/07/28 19:34:54 drh Exp $
drhcce7d172000-05-31 15:34:51 +000016*/
17#include "sqliteInt.h"
18
drhedb193b2006-06-27 13:20:21 +000019#ifndef SQLITE_OMIT_VIRTUALTABLE
drh9c419382006-06-16 21:13:21 +000020/* Forward declaration */
21static void updateVirtualTable(
22 Parse *pParse, /* The parsing context */
23 SrcList *pSrc, /* The virtual table to be modified */
24 Table *pTab, /* The virtual table */
25 ExprList *pChanges, /* The columns to change in the UPDATE statement */
26 Expr *pRowidExpr, /* Expression used to recompute the rowid */
27 int *aXRef, /* Mapping from columns of pTab to entries in pChanges */
28 Expr *pWhere /* WHERE clause of the UPDATE statement */
29);
drhedb193b2006-06-27 13:20:21 +000030#endif /* SQLITE_OMIT_VIRTUALTABLE */
drh9c419382006-06-16 21:13:21 +000031
drhcce7d172000-05-31 15:34:51 +000032/*
drh8a512562005-11-14 22:29:05 +000033** The most recently coded instruction was an OP_Column to retrieve the
drh66a51672008-01-03 00:01:23 +000034** i-th column of table pTab. This routine sets the P4 parameter of the
danielk1977aee18ef2005-03-09 12:26:50 +000035** OP_Column to the default value, if any.
36**
37** The default value of a column is specified by a DEFAULT clause in the
38** column definition. This was either supplied by the user when the table
39** was created, or added later to the table definition by an ALTER TABLE
40** command. If the latter, then the row-records in the table btree on disk
41** may not contain a value for the column and the default value, taken
drh66a51672008-01-03 00:01:23 +000042** from the P4 parameter of the OP_Column instruction, is returned instead.
danielk1977aee18ef2005-03-09 12:26:50 +000043** If the former, then all row-records are guaranteed to include a value
drh66a51672008-01-03 00:01:23 +000044** for the column and the P4 value is not required.
danielk1977aee18ef2005-03-09 12:26:50 +000045**
46** Column definitions created by an ALTER TABLE command may only have
47** literal default values specified: a number, null or a string. (If a more
48** complicated default expression value was provided, it is evaluated
49** when the ALTER TABLE is executed and one of the literal values written
50** into the sqlite_master table.)
51**
drh66a51672008-01-03 00:01:23 +000052** Therefore, the P4 parameter is only required if the default value for
danielk1977aee18ef2005-03-09 12:26:50 +000053** the column is a literal number, string or null. The sqlite3ValueFromExpr()
54** function is capable of transforming these types of expressions into
55** sqlite3_value objects.
56*/
57void sqlite3ColumnDefault(Vdbe *v, Table *pTab, int i){
58 if( pTab && !pTab->pSelect ){
59 sqlite3_value *pValue;
danielk197714db2662006-01-09 16:12:04 +000060 u8 enc = ENC(sqlite3VdbeDb(v));
danielk1977aee18ef2005-03-09 12:26:50 +000061 Column *pCol = &pTab->aCol[i];
drhd4e70eb2008-01-02 00:34:36 +000062 VdbeComment((v, "%s.%s", pTab->zName, pCol->zName));
danielk1977c9cf6e32007-06-25 16:29:33 +000063 assert( i<pTab->nCol );
drhd4e70eb2008-01-02 00:34:36 +000064 sqlite3ValueFromExpr(sqlite3VdbeDb(v), pCol->pDflt, enc,
65 pCol->affinity, &pValue);
drh29dda4a2005-07-21 18:23:20 +000066 if( pValue ){
drh66a51672008-01-03 00:01:23 +000067 sqlite3VdbeChangeP4(v, -1, (const char *)pValue, P4_MEM);
drh29dda4a2005-07-21 18:23:20 +000068 }
danielk1977aee18ef2005-03-09 12:26:50 +000069 }
70}
71
72/*
drhcce7d172000-05-31 15:34:51 +000073** Process an UPDATE statement.
drhb2fe7d82003-04-20 17:29:23 +000074**
75** UPDATE OR IGNORE table_wxyz SET a=b, c=d WHERE e<5 AND f NOT NULL;
76** \_______/ \________/ \______/ \________________/
77* onError pTabList pChanges pWhere
drhcce7d172000-05-31 15:34:51 +000078*/
danielk19774adee202004-05-08 08:23:19 +000079void sqlite3Update(
drhcce7d172000-05-31 15:34:51 +000080 Parse *pParse, /* The parser context */
drh113088e2003-03-20 01:16:58 +000081 SrcList *pTabList, /* The table in which we should change things */
drhcce7d172000-05-31 15:34:51 +000082 ExprList *pChanges, /* Things to be changed */
drh9cfcf5d2002-01-29 18:41:24 +000083 Expr *pWhere, /* The WHERE clause. May be null */
84 int onError /* How to handle constraint errors */
drhcce7d172000-05-31 15:34:51 +000085){
86 int i, j; /* Loop counters */
87 Table *pTab; /* The table to be updated */
danielk1977742f9472004-06-16 12:02:43 +000088 int addr = 0; /* VDBE instruction address of the start of the loop */
drhcce7d172000-05-31 15:34:51 +000089 WhereInfo *pWInfo; /* Information about the WHERE clause */
90 Vdbe *v; /* The virtual database engine */
91 Index *pIdx; /* For looping over indices */
92 int nIdx; /* Number of indices that need updating */
drh6a3ea0e2003-05-02 14:32:12 +000093 int iCur; /* VDBE Cursor number of pTab */
drh9bb575f2004-09-06 17:24:11 +000094 sqlite3 *db; /* The database structure */
drhaa9b8962008-01-08 02:57:55 +000095 int *aRegIdx = 0; /* One register assigned to each index to be updated */
drhcce7d172000-05-31 15:34:51 +000096 int *aXRef = 0; /* aXRef[i] is the index in pChanges->a[] of the
drh967e8b72000-06-21 13:59:10 +000097 ** an expression for the i-th column of the table.
98 ** aXRef[i]==-1 if the i-th column is not changed. */
drhf0863fe2005-06-12 21:35:51 +000099 int chngRowid; /* True if the record number is being changed */
100 Expr *pRowidExpr = 0; /* Expression defining the new record number */
danielk1977742f9472004-06-16 12:02:43 +0000101 int openAll = 0; /* True if all indices need to be opened */
drh85e20962003-04-25 17:52:11 +0000102 AuthContext sContext; /* The authorization context */
danielk1977b3bce662005-01-29 08:32:43 +0000103 NameContext sNC; /* The name-context to resolve expressions in */
danielk1977da184232006-01-05 11:34:32 +0000104 int iDb; /* Database containing the table being updated */
drha05a7222008-01-19 03:35:58 +0000105 int j1; /* Addresses of jump instructions */
drh08c88eb2008-04-10 13:33:18 +0000106 int okOnePass; /* True for one-pass algorithm without the FIFO */
drhcce7d172000-05-31 15:34:51 +0000107
drh798da522004-11-04 04:42:28 +0000108#ifndef SQLITE_OMIT_TRIGGER
109 int isView; /* Trying to update a view */
drhdca76842004-12-07 14:06:13 +0000110 int triggers_exist = 0; /* True if any row triggers exist */
drh798da522004-11-04 04:42:28 +0000111#endif
danielk19778f2c54e2008-01-01 19:02:09 +0000112 int iBeginAfterTrigger; /* Address of after trigger program */
113 int iEndAfterTrigger; /* Exit of after trigger program */
114 int iBeginBeforeTrigger; /* Address of before trigger program */
115 int iEndBeforeTrigger; /* Exit of before trigger program */
116 u32 old_col_mask = 0; /* Mask of OLD.* columns in use */
117 u32 new_col_mask = 0; /* Mask of NEW.* columns in use */
danielk1977c3f9bad2002-05-15 08:30:12 +0000118
119 int newIdx = -1; /* index of trigger "new" temp table */
120 int oldIdx = -1; /* index of trigger "old" temp table */
121
drh04adf412008-01-08 18:57:50 +0000122 /* Register Allocations */
123 int regRowCount = 0; /* A count of rows changed */
124 int regOldRowid; /* The old rowid */
125 int regNewRowid; /* The new rowid */
126 int regData; /* New data for the row */
127
drh85e20962003-04-25 17:52:11 +0000128 sContext.pParse = 0;
drh17435752007-08-16 04:30:38 +0000129 db = pParse->db;
130 if( pParse->nErr || db->mallocFailed ){
drh6f7adc82006-01-11 21:41:20 +0000131 goto update_cleanup;
132 }
drh113088e2003-03-20 01:16:58 +0000133 assert( pTabList->nSrc==1 );
drhdaffd0e2001-04-11 14:28:42 +0000134
drhb2fe7d82003-04-20 17:29:23 +0000135 /* Locate the table which we want to update.
drhcce7d172000-05-31 15:34:51 +0000136 */
danielk19774adee202004-05-08 08:23:19 +0000137 pTab = sqlite3SrcListLookup(pParse, pTabList);
drha69d9162003-04-17 22:57:53 +0000138 if( pTab==0 ) goto update_cleanup;
danielk1977da184232006-01-05 11:34:32 +0000139 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
drhb7f91642004-10-31 02:22:47 +0000140
141 /* Figure out if we have any triggers and if the table being
142 ** updated is a view
143 */
144#ifndef SQLITE_OMIT_TRIGGER
drhdca76842004-12-07 14:06:13 +0000145 triggers_exist = sqlite3TriggersExist(pParse, pTab, TK_UPDATE, pChanges);
drh5cf590c2003-04-24 01:45:04 +0000146 isView = pTab->pSelect!=0;
drhb7f91642004-10-31 02:22:47 +0000147#else
drhdca76842004-12-07 14:06:13 +0000148# define triggers_exist 0
drhb7f91642004-10-31 02:22:47 +0000149# define isView 0
150#endif
151#ifdef SQLITE_OMIT_VIEW
152# undef isView
153# define isView 0
154#endif
155
drhdca76842004-12-07 14:06:13 +0000156 if( sqlite3IsReadOnly(pParse, pTab, triggers_exist) ){
drh5cf590c2003-04-24 01:45:04 +0000157 goto update_cleanup;
drha69d9162003-04-17 22:57:53 +0000158 }
danielk1977b3d24bf2006-06-19 03:05:10 +0000159 if( sqlite3ViewGetColumnNames(pParse, pTab) ){
160 goto update_cleanup;
drh5cf590c2003-04-24 01:45:04 +0000161 }
drh17435752007-08-16 04:30:38 +0000162 aXRef = sqlite3DbMallocRaw(db, sizeof(int) * pTab->nCol );
drhcce7d172000-05-31 15:34:51 +0000163 if( aXRef==0 ) goto update_cleanup;
164 for(i=0; i<pTab->nCol; i++) aXRef[i] = -1;
165
drhb2fe7d82003-04-20 17:29:23 +0000166 /* If there are FOR EACH ROW triggers, allocate cursors for the
167 ** special OLD and NEW tables
168 */
drhdca76842004-12-07 14:06:13 +0000169 if( triggers_exist ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000170 newIdx = pParse->nTab++;
171 oldIdx = pParse->nTab++;
172 }
173
drh53e3fc72002-07-16 17:22:50 +0000174 /* Allocate a cursors for the main database table and for all indices.
175 ** The index cursors might not be used, but if they are used they
176 ** need to occur right after the database cursor. So go ahead and
177 ** allocate enough space, just in case.
178 */
drh6a3ea0e2003-05-02 14:32:12 +0000179 pTabList->a[0].iCursor = iCur = pParse->nTab++;
drh53e3fc72002-07-16 17:22:50 +0000180 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
181 pParse->nTab++;
182 }
183
danielk1977b3bce662005-01-29 08:32:43 +0000184 /* Initialize the name-context */
185 memset(&sNC, 0, sizeof(sNC));
186 sNC.pParse = pParse;
187 sNC.pSrcList = pTabList;
188
drh85e20962003-04-25 17:52:11 +0000189 /* Resolve the column names in all the expressions of the
190 ** of the UPDATE statement. Also find the column index
drhed6c8672003-01-12 18:02:16 +0000191 ** for each column to be updated in the pChanges array. For each
192 ** column to be updated, make sure we have authorization to change
193 ** that column.
drhcce7d172000-05-31 15:34:51 +0000194 */
drhf0863fe2005-06-12 21:35:51 +0000195 chngRowid = 0;
drhcce7d172000-05-31 15:34:51 +0000196 for(i=0; i<pChanges->nExpr; i++){
danielk1977b3bce662005-01-29 08:32:43 +0000197 if( sqlite3ExprResolveNames(&sNC, pChanges->a[i].pExpr) ){
drhcce7d172000-05-31 15:34:51 +0000198 goto update_cleanup;
199 }
200 for(j=0; j<pTab->nCol; j++){
danielk19774adee202004-05-08 08:23:19 +0000201 if( sqlite3StrICmp(pTab->aCol[j].zName, pChanges->a[i].zName)==0 ){
drh9647ff82002-01-14 02:56:24 +0000202 if( j==pTab->iPKey ){
drhf0863fe2005-06-12 21:35:51 +0000203 chngRowid = 1;
204 pRowidExpr = pChanges->a[i].pExpr;
drh4a324312001-12-21 14:30:42 +0000205 }
drhcce7d172000-05-31 15:34:51 +0000206 aXRef[j] = i;
207 break;
208 }
209 }
210 if( j>=pTab->nCol ){
danielk19774adee202004-05-08 08:23:19 +0000211 if( sqlite3IsRowid(pChanges->a[i].zName) ){
drhf0863fe2005-06-12 21:35:51 +0000212 chngRowid = 1;
213 pRowidExpr = pChanges->a[i].pExpr;
drha0217ba2003-06-01 01:10:33 +0000214 }else{
danielk19774adee202004-05-08 08:23:19 +0000215 sqlite3ErrorMsg(pParse, "no such column: %s", pChanges->a[i].zName);
drha0217ba2003-06-01 01:10:33 +0000216 goto update_cleanup;
217 }
drhcce7d172000-05-31 15:34:51 +0000218 }
drhed6c8672003-01-12 18:02:16 +0000219#ifndef SQLITE_OMIT_AUTHORIZATION
drhe5f9c642003-01-13 23:27:31 +0000220 {
221 int rc;
danielk19774adee202004-05-08 08:23:19 +0000222 rc = sqlite3AuthCheck(pParse, SQLITE_UPDATE, pTab->zName,
danielk1977da184232006-01-05 11:34:32 +0000223 pTab->aCol[j].zName, db->aDb[iDb].zName);
drhe5f9c642003-01-13 23:27:31 +0000224 if( rc==SQLITE_DENY ){
225 goto update_cleanup;
226 }else if( rc==SQLITE_IGNORE ){
227 aXRef[j] = -1;
228 }
drhed6c8672003-01-12 18:02:16 +0000229 }
230#endif
drhcce7d172000-05-31 15:34:51 +0000231 }
232
drhaa9b8962008-01-08 02:57:55 +0000233 /* Allocate memory for the array aRegIdx[]. There is one entry in the
234 ** array for each index associated with table being updated. Fill in
235 ** the value with a register number for indices that are to be used
236 ** and with zero for unused indices.
drhcce7d172000-05-31 15:34:51 +0000237 */
drhaa9b8962008-01-08 02:57:55 +0000238 for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){}
239 if( nIdx>0 ){
240 aRegIdx = sqlite3DbMallocRaw(db, sizeof(Index*) * nIdx );
241 if( aRegIdx==0 ) goto update_cleanup;
242 }
243 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
244 int reg;
drhf0863fe2005-06-12 21:35:51 +0000245 if( chngRowid ){
drhaa9b8962008-01-08 02:57:55 +0000246 reg = ++pParse->nMem;
247 }else{
248 reg = 0;
drh4a324312001-12-21 14:30:42 +0000249 for(i=0; i<pIdx->nColumn; i++){
drhaa9b8962008-01-08 02:57:55 +0000250 if( aXRef[pIdx->aiColumn[i]]>=0 ){
251 reg = ++pParse->nMem;
252 break;
253 }
drh4a324312001-12-21 14:30:42 +0000254 }
drhcce7d172000-05-31 15:34:51 +0000255 }
drhaa9b8962008-01-08 02:57:55 +0000256 aRegIdx[j] = reg;
drhcce7d172000-05-31 15:34:51 +0000257 }
258
drh04adf412008-01-08 18:57:50 +0000259 /* Allocate a block of register used to store the change record
260 ** sent to sqlite3GenerateConstraintChecks(). There are either
261 ** one or two registers for holding the rowid. One rowid register
262 ** is used if chngRowid is false and two are used if chngRowid is
263 ** true. Following these are pTab->nCol register holding column
264 ** data.
265 */
266 regOldRowid = regNewRowid = pParse->nMem + 1;
267 pParse->nMem += pTab->nCol + 1;
268 if( chngRowid ){
269 regNewRowid++;
270 pParse->nMem++;
271 }
272 regData = regNewRowid+1;
273
274
drhcce7d172000-05-31 15:34:51 +0000275 /* Begin generating code.
276 */
danielk19774adee202004-05-08 08:23:19 +0000277 v = sqlite3GetVdbe(pParse);
drhcce7d172000-05-31 15:34:51 +0000278 if( v==0 ) goto update_cleanup;
drh4794f732004-11-05 17:17:50 +0000279 if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
danielk1977da184232006-01-05 11:34:32 +0000280 sqlite3BeginWriteOperation(pParse, 1, iDb);
drhcce7d172000-05-31 15:34:51 +0000281
drh9c419382006-06-16 21:13:21 +0000282#ifndef SQLITE_OMIT_VIRTUALTABLE
283 /* Virtual tables must be handled separately */
284 if( IsVirtual(pTab) ){
285 updateVirtualTable(pParse, pTabList, pTab, pChanges, pRowidExpr, aXRef,
286 pWhere);
287 pWhere = 0;
288 pTabList = 0;
289 goto update_cleanup;
290 }
291#endif
292
danielk19774273dea2006-06-17 06:31:18 +0000293 /* Start the view context
294 */
295 if( isView ){
296 sqlite3AuthContextPush(pParse, &sContext, pTab->zName);
297 }
298
danielk19778f2c54e2008-01-01 19:02:09 +0000299 /* Generate the code for triggers.
300 */
301 if( triggers_exist ){
danielk1977e448dc42008-01-02 11:50:51 +0000302 int iGoto;
303
304 /* Create pseudo-tables for NEW and OLD
305 */
danielk1977cd3e8f72008-03-25 09:47:35 +0000306 sqlite3VdbeAddOp2(v, OP_SetNumColumns, 0, pTab->nCol);
drh66a51672008-01-03 00:01:23 +0000307 sqlite3VdbeAddOp2(v, OP_OpenPseudo, oldIdx, 0);
danielk1977cd3e8f72008-03-25 09:47:35 +0000308 sqlite3VdbeAddOp2(v, OP_SetNumColumns, 0, pTab->nCol);
drh66a51672008-01-03 00:01:23 +0000309 sqlite3VdbeAddOp2(v, OP_OpenPseudo, newIdx, 0);
danielk1977e448dc42008-01-02 11:50:51 +0000310
drh66a51672008-01-03 00:01:23 +0000311 iGoto = sqlite3VdbeAddOp2(v, OP_Goto, 0, 0);
danielk19778f2c54e2008-01-01 19:02:09 +0000312 addr = sqlite3VdbeMakeLabel(v);
313 iBeginBeforeTrigger = sqlite3VdbeCurrentAddr(v);
314 if( sqlite3CodeRowTrigger(pParse, TK_UPDATE, pChanges, TRIGGER_BEFORE, pTab,
315 newIdx, oldIdx, onError, addr, &old_col_mask, &new_col_mask) ){
316 goto update_cleanup;
317 }
drh66a51672008-01-03 00:01:23 +0000318 iEndBeforeTrigger = sqlite3VdbeAddOp2(v, OP_Goto, 0, 0);
danielk19778f2c54e2008-01-01 19:02:09 +0000319 iBeginAfterTrigger = sqlite3VdbeCurrentAddr(v);
320 if( sqlite3CodeRowTrigger(pParse, TK_UPDATE, pChanges, TRIGGER_AFTER, pTab,
321 newIdx, oldIdx, onError, addr, &old_col_mask, &new_col_mask) ){
322 goto update_cleanup;
323 }
drh66a51672008-01-03 00:01:23 +0000324 iEndAfterTrigger = sqlite3VdbeAddOp2(v, OP_Goto, 0, 0);
danielk19778f2c54e2008-01-01 19:02:09 +0000325 sqlite3VdbeJumpHere(v, iGoto);
326 }
327
drh9d2985c2005-09-08 01:58:42 +0000328 /* If we are trying to update a view, realize that view into
329 ** a ephemeral table.
drh5cf590c2003-04-24 01:45:04 +0000330 */
331 if( isView ){
drhf93d9992008-04-15 14:36:42 +0000332 sqlite3MaterializeView(pParse, pTab->pSelect, pWhere, iCur);
drh0f35a6b2008-02-12 16:52:14 +0000333 }
drh1013c932008-01-06 00:25:21 +0000334
drh0f35a6b2008-02-12 16:52:14 +0000335 /* Resolve the column names in all the expressions in the
336 ** WHERE clause.
337 */
338 if( sqlite3ExprResolveNames(&sNC, pWhere) ){
339 goto update_cleanup;
drh5cf590c2003-04-24 01:45:04 +0000340 }
341
drhcce7d172000-05-31 15:34:51 +0000342 /* Begin the database scan
343 */
drh08c88eb2008-04-10 13:33:18 +0000344 sqlite3VdbeAddOp2(v, OP_Null, 0, regOldRowid);
345 pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, 0,
346 WHERE_ONEPASS_DESIRED);
drhcce7d172000-05-31 15:34:51 +0000347 if( pWInfo==0 ) goto update_cleanup;
drh08c88eb2008-04-10 13:33:18 +0000348 okOnePass = pWInfo->okOnePass;
drhcce7d172000-05-31 15:34:51 +0000349
drh4cbdda92006-06-14 19:00:20 +0000350 /* Remember the rowid of every item to be updated.
drhcce7d172000-05-31 15:34:51 +0000351 */
drh08c88eb2008-04-10 13:33:18 +0000352 sqlite3VdbeAddOp2(v, IsVirtual(pTab)?OP_VRowid:OP_Rowid, iCur, regOldRowid);
353 if( !okOnePass ) sqlite3VdbeAddOp2(v, OP_FifoWrite, regOldRowid, 0);
drhcce7d172000-05-31 15:34:51 +0000354
355 /* End the database scan loop.
356 */
danielk19774adee202004-05-08 08:23:19 +0000357 sqlite3WhereEnd(pWInfo);
drhcce7d172000-05-31 15:34:51 +0000358
drh1bee3d72001-10-15 00:44:35 +0000359 /* Initialize the count of updated rows
360 */
danielk1977c3f9bad2002-05-15 08:30:12 +0000361 if( db->flags & SQLITE_CountRows && !pParse->trigStack ){
drh04adf412008-01-08 18:57:50 +0000362 regRowCount = ++pParse->nMem;
363 sqlite3VdbeAddOp2(v, OP_Integer, 0, regRowCount);
drh1bee3d72001-10-15 00:44:35 +0000364 }
365
danielk1977e448dc42008-01-02 11:50:51 +0000366 if( !isView && !IsVirtual(pTab) ){
367 /*
368 ** Open every index that needs updating. Note that if any
369 ** index could potentially invoke a REPLACE conflict resolution
370 ** action, then we need to open all indices because we might need
371 ** to be deleting some records.
drh70ce3f02003-04-15 19:22:22 +0000372 */
drh08c88eb2008-04-10 13:33:18 +0000373 if( !okOnePass ) sqlite3OpenTable(pParse, iCur, iDb, pTab, OP_OpenWrite);
danielk1977e448dc42008-01-02 11:50:51 +0000374 if( onError==OE_Replace ){
375 openAll = 1;
376 }else{
377 openAll = 0;
378 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
379 if( pIdx->onError==OE_Replace ){
380 openAll = 1;
381 break;
382 }
383 }
drh5cf590c2003-04-24 01:45:04 +0000384 }
danielk1977e448dc42008-01-02 11:50:51 +0000385 for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
drhaa9b8962008-01-08 02:57:55 +0000386 if( openAll || aRegIdx[i]>0 ){
danielk1977e448dc42008-01-02 11:50:51 +0000387 KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIdx);
danielk1977207872a2008-01-03 07:54:23 +0000388 sqlite3VdbeAddOp4(v, OP_OpenWrite, iCur+i+1, pIdx->tnum, iDb,
drh66a51672008-01-03 00:01:23 +0000389 (char*)pKey, P4_KEYINFO_HANDOFF);
danielk1977e448dc42008-01-02 11:50:51 +0000390 assert( pParse->nTab>iCur+i+1 );
391 }
392 }
danielk1977e448dc42008-01-02 11:50:51 +0000393 }
394
395 /* Jump back to this point if a trigger encounters an IGNORE constraint. */
396 if( triggers_exist ){
397 sqlite3VdbeResolveLabel(v, addr);
398 }
399
400 /* Top of the update loop */
drh08c88eb2008-04-10 13:33:18 +0000401 if( okOnePass ){
402 int a1 = sqlite3VdbeAddOp1(v, OP_NotNull, regOldRowid);
403 addr = sqlite3VdbeAddOp0(v, OP_Goto);
404 sqlite3VdbeJumpHere(v, a1);
405 }else{
406 addr = sqlite3VdbeAddOp2(v, OP_FifoRead, regOldRowid, 0);
407 }
danielk1977e448dc42008-01-02 11:50:51 +0000408
409 if( triggers_exist ){
drh2d401ab2008-01-10 23:50:11 +0000410 int regRowid;
411 int regRow;
412 int regCols;
413
danielk1977e448dc42008-01-02 11:50:51 +0000414 /* Make cursor iCur point to the record that is being updated.
415 */
drh04adf412008-01-08 18:57:50 +0000416 sqlite3VdbeAddOp3(v, OP_NotExists, iCur, addr, regOldRowid);
danielk1977c3f9bad2002-05-15 08:30:12 +0000417
drh70ce3f02003-04-15 19:22:22 +0000418 /* Generate the OLD table
419 */
drh2d401ab2008-01-10 23:50:11 +0000420 regRowid = sqlite3GetTempReg(pParse);
421 regRow = sqlite3GetTempReg(pParse);
422 sqlite3VdbeAddOp2(v, OP_Rowid, iCur, regRowid);
danielk19778f2c54e2008-01-01 19:02:09 +0000423 if( !old_col_mask ){
drh2d401ab2008-01-10 23:50:11 +0000424 sqlite3VdbeAddOp2(v, OP_Null, 0, regRow);
danielk19778f2c54e2008-01-01 19:02:09 +0000425 }else{
drh2d401ab2008-01-10 23:50:11 +0000426 sqlite3VdbeAddOp2(v, OP_RowData, iCur, regRow);
danielk19778f2c54e2008-01-01 19:02:09 +0000427 }
drh2d401ab2008-01-10 23:50:11 +0000428 sqlite3VdbeAddOp3(v, OP_Insert, oldIdx, regRow, regRowid);
danielk1977c3f9bad2002-05-15 08:30:12 +0000429
drh70ce3f02003-04-15 19:22:22 +0000430 /* Generate the NEW table
431 */
drhf0863fe2005-06-12 21:35:51 +0000432 if( chngRowid ){
drh2d401ab2008-01-10 23:50:11 +0000433 sqlite3ExprCodeAndCache(pParse, pRowidExpr, regRowid);
drh70ce3f02003-04-15 19:22:22 +0000434 }else{
drh2d401ab2008-01-10 23:50:11 +0000435 sqlite3VdbeAddOp2(v, OP_Rowid, iCur, regRowid);
drh70ce3f02003-04-15 19:22:22 +0000436 }
drh2d401ab2008-01-10 23:50:11 +0000437 regCols = sqlite3GetTempRange(pParse, pTab->nCol);
drh25303782004-12-07 15:41:48 +0000438 for(i=0; i<pTab->nCol; i++){
drh70ce3f02003-04-15 19:22:22 +0000439 if( i==pTab->iPKey ){
drh2d401ab2008-01-10 23:50:11 +0000440 sqlite3VdbeAddOp2(v, OP_Null, 0, regCols+i);
drh70ce3f02003-04-15 19:22:22 +0000441 continue;
442 }
443 j = aXRef[i];
danielk19778f2c54e2008-01-01 19:02:09 +0000444 if( new_col_mask&((u32)1<<i) || new_col_mask==0xffffffff ){
445 if( j<0 ){
drh2d401ab2008-01-10 23:50:11 +0000446 sqlite3VdbeAddOp3(v, OP_Column, iCur, i, regCols+i);
danielk19778f2c54e2008-01-01 19:02:09 +0000447 sqlite3ColumnDefault(v, pTab, i);
448 }else{
drh2d401ab2008-01-10 23:50:11 +0000449 sqlite3ExprCodeAndCache(pParse, pChanges->a[j].pExpr, regCols+i);
danielk19778f2c54e2008-01-01 19:02:09 +0000450 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000451 }else{
drh2d401ab2008-01-10 23:50:11 +0000452 sqlite3VdbeAddOp2(v, OP_Null, 0, regCols+i);
danielk1977c3f9bad2002-05-15 08:30:12 +0000453 }
454 }
drh1db639c2008-01-17 02:36:28 +0000455 sqlite3VdbeAddOp3(v, OP_MakeRecord, regCols, pTab->nCol, regRow);
danielk1977a37cdde2004-05-16 11:15:36 +0000456 if( !isView ){
457 sqlite3TableAffinityStr(v, pTab);
drhda250ea2008-04-01 05:07:14 +0000458 sqlite3ExprCacheAffinityChange(pParse, regCols, pTab->nCol);
danielk1977a37cdde2004-05-16 11:15:36 +0000459 }
drh2d401ab2008-01-10 23:50:11 +0000460 sqlite3ReleaseTempRange(pParse, regCols, pTab->nCol);
drhaf005fb2008-07-09 16:51:51 +0000461 /* if( pParse->nErr ) goto update_cleanup; */
drh2d401ab2008-01-10 23:50:11 +0000462 sqlite3VdbeAddOp3(v, OP_Insert, newIdx, regRow, regRowid);
463 sqlite3ReleaseTempReg(pParse, regRowid);
464 sqlite3ReleaseTempReg(pParse, regRow);
danielk1977c3f9bad2002-05-15 08:30:12 +0000465
drh66a51672008-01-03 00:01:23 +0000466 sqlite3VdbeAddOp2(v, OP_Goto, 0, iBeginBeforeTrigger);
danielk19778f2c54e2008-01-01 19:02:09 +0000467 sqlite3VdbeJumpHere(v, iEndBeforeTrigger);
danielk1977c3f9bad2002-05-15 08:30:12 +0000468 }
469
drh4cbdda92006-06-14 19:00:20 +0000470 if( !isView && !IsVirtual(pTab) ){
drh5cf590c2003-04-24 01:45:04 +0000471 /* Loop over every record that needs updating. We have to load
472 ** the old data for each record to be updated because some columns
473 ** might not change and we will need to copy the old value.
drh4cbdda92006-06-14 19:00:20 +0000474 ** Also, the old data is needed to delete the old index entries.
drh5cf590c2003-04-24 01:45:04 +0000475 ** So make the cursor point at the old record.
476 */
drh04adf412008-01-08 18:57:50 +0000477 sqlite3VdbeAddOp3(v, OP_NotExists, iCur, addr, regOldRowid);
drh5cf590c2003-04-24 01:45:04 +0000478
479 /* If the record number will change, push the record number as it
480 ** will be after the update. (The old record number is currently
481 ** on top of the stack.)
482 */
drhf0863fe2005-06-12 21:35:51 +0000483 if( chngRowid ){
drh04adf412008-01-08 18:57:50 +0000484 sqlite3ExprCode(pParse, pRowidExpr, regNewRowid);
drh3c84ddf2008-01-09 02:15:38 +0000485 sqlite3VdbeAddOp1(v, OP_MustBeInt, regNewRowid);
drhcce7d172000-05-31 15:34:51 +0000486 }
drh5cf590c2003-04-24 01:45:04 +0000487
488 /* Compute new data for this record.
489 */
490 for(i=0; i<pTab->nCol; i++){
491 if( i==pTab->iPKey ){
drh04adf412008-01-08 18:57:50 +0000492 sqlite3VdbeAddOp2(v, OP_Null, 0, regData+i);
drh5cf590c2003-04-24 01:45:04 +0000493 continue;
494 }
495 j = aXRef[i];
496 if( j<0 ){
drh04adf412008-01-08 18:57:50 +0000497 sqlite3VdbeAddOp3(v, OP_Column, iCur, i, regData+i);
danielk1977aee18ef2005-03-09 12:26:50 +0000498 sqlite3ColumnDefault(v, pTab, i);
drh5cf590c2003-04-24 01:45:04 +0000499 }else{
drh04adf412008-01-08 18:57:50 +0000500 sqlite3ExprCode(pParse, pChanges->a[j].pExpr, regData+i);
drh5cf590c2003-04-24 01:45:04 +0000501 }
502 }
503
504 /* Do constraint checks
505 */
drh04adf412008-01-08 18:57:50 +0000506 sqlite3GenerateConstraintChecks(pParse, pTab, iCur, regNewRowid,
507 aRegIdx, chngRowid, 1,
508 onError, addr);
drh5cf590c2003-04-24 01:45:04 +0000509
510 /* Delete the old indices for the current record.
511 */
drha05a7222008-01-19 03:35:58 +0000512 j1 = sqlite3VdbeAddOp3(v, OP_NotExists, iCur, 0, regOldRowid);
drh2d401ab2008-01-10 23:50:11 +0000513 sqlite3GenerateRowIndexDelete(pParse, pTab, iCur, aRegIdx);
drh5cf590c2003-04-24 01:45:04 +0000514
515 /* If changing the record number, delete the old record.
516 */
drhf0863fe2005-06-12 21:35:51 +0000517 if( chngRowid ){
drh66a51672008-01-03 00:01:23 +0000518 sqlite3VdbeAddOp2(v, OP_Delete, iCur, 0);
drh5cf590c2003-04-24 01:45:04 +0000519 }
drha05a7222008-01-19 03:35:58 +0000520 sqlite3VdbeJumpHere(v, j1);
drh5cf590c2003-04-24 01:45:04 +0000521
522 /* Create the new index entries and the new record.
523 */
drh04adf412008-01-08 18:57:50 +0000524 sqlite3CompleteInsertion(pParse, pTab, iCur, regNewRowid,
525 aRegIdx, chngRowid, 1, -1, 0);
drhcce7d172000-05-31 15:34:51 +0000526 }
527
drh0ca3e242002-01-29 23:07:02 +0000528 /* Increment the row counter
drh1bee3d72001-10-15 00:44:35 +0000529 */
danielk1977c3f9bad2002-05-15 08:30:12 +0000530 if( db->flags & SQLITE_CountRows && !pParse->trigStack){
drh04adf412008-01-08 18:57:50 +0000531 sqlite3VdbeAddOp2(v, OP_AddImm, regRowCount, 1);
drh1bee3d72001-10-15 00:44:35 +0000532 }
533
drh5cf590c2003-04-24 01:45:04 +0000534 /* If there are triggers, close all the cursors after each iteration
535 ** through the loop. The fire the after triggers.
536 */
drhdca76842004-12-07 14:06:13 +0000537 if( triggers_exist ){
drh66a51672008-01-03 00:01:23 +0000538 sqlite3VdbeAddOp2(v, OP_Goto, 0, iBeginAfterTrigger);
danielk19778f2c54e2008-01-01 19:02:09 +0000539 sqlite3VdbeJumpHere(v, iEndAfterTrigger);
danielk1977c3f9bad2002-05-15 08:30:12 +0000540 }
541
drhcce7d172000-05-31 15:34:51 +0000542 /* Repeat the above with the next record to be updated, until
543 ** all record selected by the WHERE clause have been updated.
544 */
drh66a51672008-01-03 00:01:23 +0000545 sqlite3VdbeAddOp2(v, OP_Goto, 0, addr);
drhd654be82005-09-20 17:42:23 +0000546 sqlite3VdbeJumpHere(v, addr);
danielk1977c3f9bad2002-05-15 08:30:12 +0000547
danielk1977e448dc42008-01-02 11:50:51 +0000548 /* Close all tables */
549 for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
drhaa9b8962008-01-08 02:57:55 +0000550 if( openAll || aRegIdx[i]>0 ){
drh66a51672008-01-03 00:01:23 +0000551 sqlite3VdbeAddOp2(v, OP_Close, iCur+i+1, 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000552 }
danielk1977e448dc42008-01-02 11:50:51 +0000553 }
drh66a51672008-01-03 00:01:23 +0000554 sqlite3VdbeAddOp2(v, OP_Close, iCur, 0);
danielk1977e448dc42008-01-02 11:50:51 +0000555 if( triggers_exist ){
drh66a51672008-01-03 00:01:23 +0000556 sqlite3VdbeAddOp2(v, OP_Close, newIdx, 0);
557 sqlite3VdbeAddOp2(v, OP_Close, oldIdx, 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000558 }
559
drh1bee3d72001-10-15 00:44:35 +0000560 /*
danielk1977e7de6f22004-11-05 06:02:06 +0000561 ** Return the number of rows that were changed. If this routine is
562 ** generating code because of a call to sqlite3NestedParse(), do not
563 ** invoke the callback function.
drh1bee3d72001-10-15 00:44:35 +0000564 */
danielk1977e7de6f22004-11-05 06:02:06 +0000565 if( db->flags & SQLITE_CountRows && !pParse->trigStack && pParse->nested==0 ){
drh04adf412008-01-08 18:57:50 +0000566 sqlite3VdbeAddOp2(v, OP_ResultRow, regRowCount, 1);
danielk197722322fd2004-05-25 23:35:17 +0000567 sqlite3VdbeSetNumCols(v, 1);
drh66a51672008-01-03 00:01:23 +0000568 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "rows updated", P4_STATIC);
drh1bee3d72001-10-15 00:44:35 +0000569 }
570
drhcce7d172000-05-31 15:34:51 +0000571update_cleanup:
danielk19774adee202004-05-08 08:23:19 +0000572 sqlite3AuthContextPop(&sContext);
drh633e6d52008-07-28 19:34:53 +0000573 sqlite3DbFree(db, aRegIdx);
574 sqlite3DbFree(db, aXRef);
575 sqlite3SrcListDelete(db, pTabList);
576 sqlite3ExprListDelete(db, pChanges);
577 sqlite3ExprDelete(db, pWhere);
drhcce7d172000-05-31 15:34:51 +0000578 return;
579}
drh9c419382006-06-16 21:13:21 +0000580
581#ifndef SQLITE_OMIT_VIRTUALTABLE
582/*
583** Generate code for an UPDATE of a virtual table.
584**
585** The strategy is that we create an ephemerial table that contains
586** for each row to be changed:
587**
588** (A) The original rowid of that row.
589** (B) The revised rowid for the row. (note1)
590** (C) The content of every column in the row.
591**
592** Then we loop over this ephemeral table and for each row in
593** the ephermeral table call VUpdate.
594**
595** When finished, drop the ephemeral table.
596**
597** (note1) Actually, if we know in advance that (A) is always the same
598** as (B) we only store (A), then duplicate (A) when pulling
599** it out of the ephemeral table before calling VUpdate.
600*/
601static void updateVirtualTable(
602 Parse *pParse, /* The parsing context */
603 SrcList *pSrc, /* The virtual table to be modified */
604 Table *pTab, /* The virtual table */
605 ExprList *pChanges, /* The columns to change in the UPDATE statement */
606 Expr *pRowid, /* Expression used to recompute the rowid */
607 int *aXRef, /* Mapping from columns of pTab to entries in pChanges */
608 Expr *pWhere /* WHERE clause of the UPDATE statement */
609){
610 Vdbe *v = pParse->pVdbe; /* Virtual machine under construction */
611 ExprList *pEList = 0; /* The result set of the SELECT statement */
612 Select *pSelect = 0; /* The SELECT statement */
613 Expr *pExpr; /* Temporary expression */
614 int ephemTab; /* Table holding the result of the SELECT */
615 int i; /* Loop counter */
616 int addr; /* Address of top of loop */
danielk1977287fb612008-01-04 19:10:28 +0000617 int iReg; /* First register in set passed to OP_VUpdate */
drh17435752007-08-16 04:30:38 +0000618 sqlite3 *db = pParse->db; /* Database connection */
danielk1977287fb612008-01-04 19:10:28 +0000619 const char *pVtab = (const char*)pTab->pVtab;
drh1013c932008-01-06 00:25:21 +0000620 SelectDest dest;
drh9c419382006-06-16 21:13:21 +0000621
622 /* Construct the SELECT statement that will find the new values for
623 ** all updated rows.
624 */
drh17435752007-08-16 04:30:38 +0000625 pEList = sqlite3ExprListAppend(pParse, 0,
626 sqlite3CreateIdExpr(pParse, "_rowid_"), 0);
drh9c419382006-06-16 21:13:21 +0000627 if( pRowid ){
drh17435752007-08-16 04:30:38 +0000628 pEList = sqlite3ExprListAppend(pParse, pEList,
629 sqlite3ExprDup(db, pRowid), 0);
drh9c419382006-06-16 21:13:21 +0000630 }
danielk197765fd59f2006-06-24 11:51:33 +0000631 assert( pTab->iPKey<0 );
drh9c419382006-06-16 21:13:21 +0000632 for(i=0; i<pTab->nCol; i++){
danielk197765fd59f2006-06-24 11:51:33 +0000633 if( aXRef[i]>=0 ){
drh17435752007-08-16 04:30:38 +0000634 pExpr = sqlite3ExprDup(db, pChanges->a[aXRef[i]].pExpr);
drh9c419382006-06-16 21:13:21 +0000635 }else{
drh17435752007-08-16 04:30:38 +0000636 pExpr = sqlite3CreateIdExpr(pParse, pTab->aCol[i].zName);
drh9c419382006-06-16 21:13:21 +0000637 }
drh17435752007-08-16 04:30:38 +0000638 pEList = sqlite3ExprListAppend(pParse, pEList, pExpr, 0);
drh9c419382006-06-16 21:13:21 +0000639 }
drh17435752007-08-16 04:30:38 +0000640 pSelect = sqlite3SelectNew(pParse, pEList, pSrc, pWhere, 0, 0, 0, 0, 0, 0);
drh9c419382006-06-16 21:13:21 +0000641
642 /* Create the ephemeral table into which the update results will
643 ** be stored.
644 */
645 assert( v );
646 ephemTab = pParse->nTab++;
drh66a51672008-01-03 00:01:23 +0000647 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, ephemTab, pTab->nCol+1+(pRowid!=0));
drh9c419382006-06-16 21:13:21 +0000648
649 /* fill the ephemeral table
650 */
drh1013c932008-01-06 00:25:21 +0000651 sqlite3SelectDestInit(&dest, SRT_Table, ephemTab);
drha9671a22008-07-08 23:40:20 +0000652 sqlite3Select(pParse, pSelect, &dest, 0, 0, 0);
drh9c419382006-06-16 21:13:21 +0000653
danielk1977287fb612008-01-04 19:10:28 +0000654 /* Generate code to scan the ephemeral table and call VUpdate. */
655 iReg = ++pParse->nMem;
656 pParse->nMem += pTab->nCol+1;
drh66a51672008-01-03 00:01:23 +0000657 sqlite3VdbeAddOp2(v, OP_Rewind, ephemTab, 0);
drh9c419382006-06-16 21:13:21 +0000658 addr = sqlite3VdbeCurrentAddr(v);
danielk1977287fb612008-01-04 19:10:28 +0000659 sqlite3VdbeAddOp3(v, OP_Column, ephemTab, 0, iReg);
660 sqlite3VdbeAddOp3(v, OP_Column, ephemTab, (pRowid?1:0), iReg+1);
drh9c419382006-06-16 21:13:21 +0000661 for(i=0; i<pTab->nCol; i++){
danielk1977287fb612008-01-04 19:10:28 +0000662 sqlite3VdbeAddOp3(v, OP_Column, ephemTab, i+1+(pRowid!=0), iReg+2+i);
drh9c419382006-06-16 21:13:21 +0000663 }
drh4f3dd152008-04-28 18:46:43 +0000664 sqlite3VtabMakeWritable(pParse, pTab);
danielk1977287fb612008-01-04 19:10:28 +0000665 sqlite3VdbeAddOp4(v, OP_VUpdate, 0, pTab->nCol+2, iReg, pVtab, P4_VTAB);
drh66a51672008-01-03 00:01:23 +0000666 sqlite3VdbeAddOp2(v, OP_Next, ephemTab, addr);
danielk197707fd0592007-02-21 17:04:04 +0000667 sqlite3VdbeJumpHere(v, addr-1);
drh66a51672008-01-03 00:01:23 +0000668 sqlite3VdbeAddOp2(v, OP_Close, ephemTab, 0);
drh9c419382006-06-16 21:13:21 +0000669
670 /* Cleanup */
drh633e6d52008-07-28 19:34:53 +0000671 sqlite3SelectDelete(db, pSelect);
drh9c419382006-06-16 21:13:21 +0000672}
673#endif /* SQLITE_OMIT_VIRTUALTABLE */
drhf39d9582008-03-19 20:42:13 +0000674
675/* Make sure "isView" gets undefined in case this file becomes part of
676** the amalgamation - so that subsequent files do not see isView as a
677** macro. */
678#undef isView