AryaWu/sqlite
0
1/*2** 2001 September 153**4** The author disclaims copyright to this source code. In place of5** a legal notice, here is a blessing:6**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.10**11*************************************************************************12** This file contains C code routines that are called by the parser13** to handle UPDATE statements.14*/15#include "sqliteInt.h"16 17#ifndef SQLITE_OMIT_VIRTUALTABLE18/* Forward declaration */19static void updateVirtualTable(20 Parse *pParse, /* The parsing context */21 SrcList *pSrc, /* The virtual table to be modified */22 Table *pTab, /* The virtual table */23 ExprList *pChanges, /* The columns to change in the UPDATE statement */24 Expr *pRowidExpr, /* Expression used to recompute the rowid */25 int *aXRef, /* Mapping from columns of pTab to entries in pChanges */26 Expr *pWhere, /* WHERE clause of the UPDATE statement */27 int onError /* ON CONFLICT strategy */28);29#endif /* SQLITE_OMIT_VIRTUALTABLE */30 31/*32** The most recently coded instruction was an OP_Column to retrieve the33** i-th column of table pTab. This routine sets the P4 parameter of the34** OP_Column to the default value, if any.35**36** The default value of a column is specified by a DEFAULT clause in the37** column definition. This was either supplied by the user when the table38** was created, or added later to the table definition by an ALTER TABLE39** command. If the latter, then the row-records in the table btree on disk40** may not contain a value for the column and the default value, taken41** from the P4 parameter of the OP_Column instruction, is returned instead.42** If the former, then all row-records are guaranteed to include a value43** for the column and the P4 value is not required.44**45** Column definitions created by an ALTER TABLE command may only have46** literal default values specified: a number, null or a string. (If a more47** complicated default expression value was provided, it is evaluated48** when the ALTER TABLE is executed and one of the literal values written49** into the sqlite_schema table.)50**51** Therefore, the P4 parameter is only required if the default value for52** the column is a literal number, string or null. The sqlite3ValueFromExpr()53** function is capable of transforming these types of expressions into54** sqlite3_value objects.55**56** If column as REAL affinity and the table is an ordinary b-tree table57** (not a virtual table) then the value might have been stored as an58** integer. In that case, add an OP_RealAffinity opcode to make sure59** it has been converted into REAL.60*/61void sqlite3ColumnDefault(Vdbe *v, Table *pTab, int i, int iReg){62 Column *pCol;63 assert( pTab!=0 );64 assert( pTab->nCol>i );65 pCol = &pTab->aCol[i];66 if( pCol->iDflt ){67 sqlite3_value *pValue = 0;68 u8 enc = ENC(sqlite3VdbeDb(v));69 assert( !IsView(pTab) );70 VdbeComment((v, "%s.%s", pTab->zName, pCol->zCnName));71 assert( i<pTab->nCol );72 sqlite3ValueFromExpr(sqlite3VdbeDb(v),73 sqlite3ColumnExpr(pTab,pCol), enc,74 pCol->affinity, &pValue);75 if( pValue ){76 sqlite3VdbeAppendP4(v, pValue, P4_MEM);77 }78 }79#ifndef SQLITE_OMIT_FLOATING_POINT80 if( pCol->affinity==SQLITE_AFF_REAL && !IsVirtual(pTab) ){81 sqlite3VdbeAddOp1(v, OP_RealAffinity, iReg);82 }83#endif84}85 86/*87** Check to see if column iCol of index pIdx references any of the88** columns defined by aXRef and chngRowid. Return true if it does89** and false if not. This is an optimization. False-positives are a90** performance degradation, but false-negatives can result in a corrupt91** index and incorrect answers.92**93** aXRef[j] will be non-negative if column j of the original table is94** being updated. chngRowid will be true if the rowid of the table is95** being updated.96*/97static int indexColumnIsBeingUpdated(98 Index *pIdx, /* The index to check */99 int iCol, /* Which column of the index to check */100 int *aXRef, /* aXRef[j]>=0 if column j is being updated */101 int chngRowid /* true if the rowid is being updated */102){103 i16 iIdxCol = pIdx->aiColumn[iCol];104 assert( iIdxCol!=XN_ROWID ); /* Cannot index rowid */105 if( iIdxCol>=0 ){106 return aXRef[iIdxCol]>=0;107 }108 assert( iIdxCol==XN_EXPR );109 assert( pIdx->aColExpr!=0 );110 assert( pIdx->aColExpr->a[iCol].pExpr!=0 );111 return sqlite3ExprReferencesUpdatedColumn(pIdx->aColExpr->a[iCol].pExpr,112 aXRef,chngRowid);113}114 115/*116** Check to see if index pIdx is a partial index whose conditional117** expression might change values due to an UPDATE. Return true if118** the index is subject to change and false if the index is guaranteed119** to be unchanged. This is an optimization. False-positives are a120** performance degradation, but false-negatives can result in a corrupt121** index and incorrect answers.122**123** aXRef[j] will be non-negative if column j of the original table is124** being updated. chngRowid will be true if the rowid of the table is125** being updated.126*/127static int indexWhereClauseMightChange(128 Index *pIdx, /* The index to check */129 int *aXRef, /* aXRef[j]>=0 if column j is being updated */130 int chngRowid /* true if the rowid is being updated */131){132 if( pIdx->pPartIdxWhere==0 ) return 0;133 return sqlite3ExprReferencesUpdatedColumn(pIdx->pPartIdxWhere,134 aXRef, chngRowid);135}136 137/*138** Allocate and return a pointer to an expression of type TK_ROW with139** Expr.iColumn set to value (iCol+1). The resolver will modify the140** expression to be a TK_COLUMN reading column iCol of the first141** table in the source-list (pSrc->a[0]).142*/143static Expr *exprRowColumn(Parse *pParse, int iCol){144 Expr *pRet = sqlite3PExpr(pParse, TK_ROW, 0, 0);145 if( pRet ) pRet->iColumn = iCol+1;146 return pRet;147}148 149/*150** Assuming both the pLimit and pOrderBy parameters are NULL, this function151** generates VM code to run the query:152**153** SELECT <other-columns>, pChanges FROM pTabList WHERE pWhere154**155** and write the results to the ephemeral table already opened as cursor156** iEph. None of pChanges, pTabList or pWhere are modified or consumed by157** this function, they must be deleted by the caller.158**159** Or, if pLimit and pOrderBy are not NULL, and pTab is not a view:160**161** SELECT <other-columns>, pChanges FROM pTabList162** WHERE pWhere163** GROUP BY <other-columns>164** ORDER BY pOrderBy LIMIT pLimit165**166** If pTab is a view, the GROUP BY clause is omitted.167**168** Exactly how results are written to table iEph, and exactly what169** the <other-columns> in the query above are is determined by the type170** of table pTabList->a[0].pTab.171**172** If the table is a WITHOUT ROWID table, then argument pPk must be its173** PRIMARY KEY. In this case <other-columns> are the primary key columns174** of the table, in order. The results of the query are written to ephemeral175** table iEph as index keys, using OP_IdxInsert.176**177** If the table is actually a view, then <other-columns> are all columns of178** the view. The results are written to the ephemeral table iEph as records179** with automatically assigned integer keys.180**181** If the table is a virtual or ordinary intkey table, then <other-columns>182** is its rowid. For a virtual table, the results are written to iEph as183** records with automatically assigned integer keys For intkey tables, the184** rowid value in <other-columns> is used as the integer key, and the185** remaining fields make up the table record.186*/187static void updateFromSelect(188 Parse *pParse, /* Parse context */189 int iEph, /* Cursor for open eph. table */190 Index *pPk, /* PK if table 0 is WITHOUT ROWID */191 ExprList *pChanges, /* List of expressions to return */192 SrcList *pTabList, /* List of tables to select from */193 Expr *pWhere, /* WHERE clause for query */194 ExprList *pOrderBy, /* ORDER BY clause */195 Expr *pLimit /* LIMIT clause */196){197 int i;198 SelectDest dest;199 Select *pSelect = 0;200 ExprList *pList = 0;201 ExprList *pGrp = 0;202 Expr *pLimit2 = 0;203 ExprList *pOrderBy2 = 0;204 sqlite3 *db = pParse->db;205 Table *pTab = pTabList->a[0].pSTab;206 SrcList *pSrc;207 Expr *pWhere2;208 int eDest;209 210#ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT211 if( pOrderBy && pLimit==0 ) {212 sqlite3ErrorMsg(pParse, "ORDER BY without LIMIT on UPDATE");213 return;214 }215 pOrderBy2 = sqlite3ExprListDup(db, pOrderBy, 0);216 pLimit2 = sqlite3ExprDup(db, pLimit, 0);217#else218 UNUSED_PARAMETER(pOrderBy);219 UNUSED_PARAMETER(pLimit);220#endif221 222 pSrc = sqlite3SrcListDup(db, pTabList, 0);223 pWhere2 = sqlite3ExprDup(db, pWhere, 0);224 225 assert( pTabList->nSrc>1 );226 if( pSrc ){227 assert( pSrc->a[0].fg.notCte );228 pSrc->a[0].iCursor = -1;229 pSrc->a[0].pSTab->nTabRef--;230 pSrc->a[0].pSTab = 0;231 }232 if( pPk ){233 for(i=0; i<pPk->nKeyCol; i++){234 Expr *pNew = exprRowColumn(pParse, pPk->aiColumn[i]);235#ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT236 if( pLimit ){237 pGrp = sqlite3ExprListAppend(pParse, pGrp, sqlite3ExprDup(db, pNew, 0));238 }239#endif240 pList = sqlite3ExprListAppend(pParse, pList, pNew);241 }242 eDest = IsVirtual(pTab) ? SRT_Table : SRT_Upfrom;243 }else if( IsView(pTab) ){244 for(i=0; i<pTab->nCol; i++){245 pList = sqlite3ExprListAppend(pParse, pList, exprRowColumn(pParse, i));246 }247 eDest = SRT_Table;248 }else{249 eDest = IsVirtual(pTab) ? SRT_Table : SRT_Upfrom;250 pList = sqlite3ExprListAppend(pParse, 0, sqlite3PExpr(pParse,TK_ROW,0,0));251#ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT252 if( pLimit ){253 pGrp = sqlite3ExprListAppend(pParse, 0, sqlite3PExpr(pParse,TK_ROW,0,0));254 }255#endif256 }257 assert( pChanges!=0 || pParse->db->mallocFailed );258 if( pChanges ){259 for(i=0; i<pChanges->nExpr; i++){260 pList = sqlite3ExprListAppend(pParse, pList,261 sqlite3ExprDup(db, pChanges->a[i].pExpr, 0)262 );263 }264 }265 pSelect = sqlite3SelectNew(pParse, pList,266 pSrc, pWhere2, pGrp, 0, pOrderBy2,267 SF_UFSrcCheck|SF_IncludeHidden|SF_UpdateFrom, pLimit2268 );269 if( pSelect ) pSelect->selFlags |= SF_OrderByReqd;270 sqlite3SelectDestInit(&dest, eDest, iEph);271 dest.iSDParm2 = (pPk ? pPk->nKeyCol : -1);272 sqlite3Select(pParse, pSelect, &dest);273 sqlite3SelectDelete(db, pSelect);274}275 276/*277** Process an UPDATE statement.278**279** UPDATE OR IGNORE tbl SET a=b, c=d FROM tbl2... WHERE e<5 AND f NOT NULL;280** \_______/ \_/ \______/ \_____/ \________________/281** onError | pChanges | pWhere282** \_______________________/283** pTabList284*/285void sqlite3Update(286 Parse *pParse, /* The parser context */287 SrcList *pTabList, /* The table in which we should change things */288 ExprList *pChanges, /* Things to be changed */289 Expr *pWhere, /* The WHERE clause. May be null */290 int onError, /* How to handle constraint errors */291 ExprList *pOrderBy, /* ORDER BY clause. May be null */292 Expr *pLimit, /* LIMIT clause. May be null */293 Upsert *pUpsert /* ON CONFLICT clause, or null */294){295 int i, j, k; /* Loop counters */296 Table *pTab; /* The table to be updated */297 int addrTop = 0; /* VDBE instruction address of the start of the loop */298 WhereInfo *pWInfo = 0; /* Information about the WHERE clause */299 Vdbe *v; /* The virtual database engine */300 Index *pIdx; /* For looping over indices */301 Index *pPk; /* The PRIMARY KEY index for WITHOUT ROWID tables */302 int nIdx; /* Number of indices that need updating */303 int nAllIdx; /* Total number of indexes */304 int iBaseCur; /* Base cursor number */305 int iDataCur; /* Cursor for the canonical data btree */306 int iIdxCur; /* Cursor for the first index */307 sqlite3 *db; /* The database structure */308 int *aRegIdx = 0; /* Registers for to each index and the main table */309 int *aXRef = 0; /* aXRef[i] is the index in pChanges->a[] of the310 ** an expression for the i-th column of the table.311 ** aXRef[i]==-1 if the i-th column is not changed. */312 u8 *aToOpen; /* 1 for tables and indices to be opened */313 u8 chngPk; /* PRIMARY KEY changed in a WITHOUT ROWID table */314 u8 chngRowid; /* Rowid changed in a normal table */315 u8 chngKey; /* Either chngPk or chngRowid */316 Expr *pRowidExpr = 0; /* Expression defining the new record number */317 int iRowidExpr = -1; /* Index of "rowid=" (or IPK) assignment in pChanges */318 AuthContext sContext; /* The authorization context */319 NameContext sNC; /* The name-context to resolve expressions in */320 int iDb; /* Database containing the table being updated */321 int eOnePass; /* ONEPASS_XXX value from where.c */322 int hasFK; /* True if foreign key processing is required */323 int labelBreak; /* Jump here to break out of UPDATE loop */324 int labelContinue; /* Jump here to continue next step of UPDATE loop */325 int flags; /* Flags for sqlite3WhereBegin() */326 327#ifndef SQLITE_OMIT_TRIGGER328 int isView; /* True when updating a view (INSTEAD OF trigger) */329 Trigger *pTrigger; /* List of triggers on pTab, if required */330 int tmask; /* Mask of TRIGGER_BEFORE|TRIGGER_AFTER */331#endif332 int newmask; /* Mask of NEW.* columns accessed by BEFORE triggers */333 int iEph = 0; /* Ephemeral table holding all primary key values */334 int nKey = 0; /* Number of elements in regKey for WITHOUT ROWID */335 int aiCurOnePass[2]; /* The write cursors opened by WHERE_ONEPASS */336 int addrOpen = 0; /* Address of OP_OpenEphemeral */337 int iPk = 0; /* First of nPk cells holding PRIMARY KEY value */338 i16 nPk = 0; /* Number of components of the PRIMARY KEY */339 int bReplace = 0; /* True if REPLACE conflict resolution might happen */340 int bFinishSeek = 1; /* The OP_FinishSeek opcode is needed */341 int nChangeFrom = 0; /* If there is a FROM, pChanges->nExpr, else 0 */342 343 /* Register Allocations */344 int regRowCount = 0; /* A count of rows changed */345 int regOldRowid = 0; /* The old rowid */346 int regNewRowid = 0; /* The new rowid */347 int regNew = 0; /* Content of the NEW.* table in triggers */348 int regOld = 0; /* Content of OLD.* table in triggers */349 int regRowSet = 0; /* Rowset of rows to be updated */350 int regKey = 0; /* composite PRIMARY KEY value */351 352 memset(&sContext, 0, sizeof(sContext));353 db = pParse->db;354 assert( db->pParse==pParse );355 if( pParse->nErr ){356 goto update_cleanup;357 }358 assert( db->mallocFailed==0 );359 360 /* Locate the table which we want to update.361 */362 pTab = sqlite3SrcListLookup(pParse, pTabList);363 if( pTab==0 ) goto update_cleanup;364 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);365 366 /* Figure out if we have any triggers and if the table being367 ** updated is a view.368 */369#ifndef SQLITE_OMIT_TRIGGER370 pTrigger = sqlite3TriggersExist(pParse, pTab, TK_UPDATE, pChanges, &tmask);371 isView = IsView(pTab);372 assert( pTrigger || tmask==0 );373#else374# define pTrigger 0375# define isView 0376# define tmask 0377#endif378#ifdef SQLITE_OMIT_VIEW379# undef isView380# define isView 0381#endif382 383#if TREETRACE_ENABLED384 if( sqlite3TreeTrace & 0x10000 ){385 sqlite3TreeViewLine(0, "In sqlite3Update() at %s:%d", __FILE__, __LINE__);386 sqlite3TreeViewUpdate(pParse->pWith, pTabList, pChanges, pWhere,387 onError, pOrderBy, pLimit, pUpsert, pTrigger);388 }389#endif390 391 /* If there was a FROM clause, set nChangeFrom to the number of expressions392 ** in the change-list. Otherwise, set it to 0. There cannot be a FROM393 ** clause if this function is being called to generate code for part of394 ** an UPSERT statement. */395 nChangeFrom = (pTabList->nSrc>1) ? pChanges->nExpr : 0;396 assert( nChangeFrom==0 || pUpsert==0 );397 398#ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT399 if( !isView && nChangeFrom==0 ){400 pWhere = sqlite3LimitWhere(401 pParse, pTabList, pWhere, pOrderBy, pLimit, "UPDATE"402 );403 pOrderBy = 0;404 pLimit = 0;405 }406#endif407 408 if( sqlite3ViewGetColumnNames(pParse, pTab) ){409 goto update_cleanup;410 }411 if( sqlite3IsReadOnly(pParse, pTab, pTrigger) ){412 goto update_cleanup;413 }414 415 /* Allocate a cursors for the main database table and for all indices.416 ** The index cursors might not be used, but if they are used they417 ** need to occur right after the database cursor. So go ahead and418 ** allocate enough space, just in case.419 */420 iBaseCur = iDataCur = pParse->nTab++;421 iIdxCur = iDataCur+1;422 pPk = HasRowid(pTab) ? 0 : sqlite3PrimaryKeyIndex(pTab);423 testcase( pPk!=0 && pPk!=pTab->pIndex );424 for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){425 if( pPk==pIdx ){426 iDataCur = pParse->nTab;427 }428 pParse->nTab++;429 }430 if( pUpsert ){431 /* On an UPSERT, reuse the same cursors already opened by INSERT */432 iDataCur = pUpsert->iDataCur;433 iIdxCur = pUpsert->iIdxCur;434 pParse->nTab = iBaseCur;435 }436 pTabList->a[0].iCursor = iDataCur;437 438 /* Allocate space for aXRef[], aRegIdx[], and aToOpen[].439 ** Initialize aXRef[] and aToOpen[] to their default values.440 */441 aXRef = sqlite3DbMallocRawNN(db, sizeof(int) * (pTab->nCol+nIdx+1) + nIdx+2 );442 if( aXRef==0 ) goto update_cleanup;443 aRegIdx = aXRef+pTab->nCol;444 aToOpen = (u8*)(aRegIdx+nIdx+1);445 memset(aToOpen, 1, nIdx+1);446 aToOpen[nIdx+1] = 0;447 for(i=0; i<pTab->nCol; i++) aXRef[i] = -1;448 449 /* Initialize the name-context */450 memset(&sNC, 0, sizeof(sNC));451 sNC.pParse = pParse;452 sNC.pSrcList = pTabList;453 sNC.uNC.pUpsert = pUpsert;454 sNC.ncFlags = NC_UUpsert;455 456 /* Begin generating code. */457 v = sqlite3GetVdbe(pParse);458 if( v==0 ) goto update_cleanup;459 460 /* Resolve the column names in all the expressions of the461 ** of the UPDATE statement. Also find the column index462 ** for each column to be updated in the pChanges array. For each463 ** column to be updated, make sure we have authorization to change464 ** that column.465 */466 chngRowid = chngPk = 0;467 for(i=0; i<pChanges->nExpr; i++){468 /* If this is an UPDATE with a FROM clause, do not resolve expressions469 ** here. The call to sqlite3Select() below will do that. */470 if( nChangeFrom==0 && sqlite3ResolveExprNames(&sNC, pChanges->a[i].pExpr) ){471 goto update_cleanup;472 }473 j = sqlite3ColumnIndex(pTab, pChanges->a[i].zEName);474 if( j>=0 ){475 if( j==pTab->iPKey ){476 chngRowid = 1;477 pRowidExpr = pChanges->a[i].pExpr;478 iRowidExpr = i;479 }else if( pPk && (pTab->aCol[j].colFlags & COLFLAG_PRIMKEY)!=0 ){480 chngPk = 1;481 }482#ifndef SQLITE_OMIT_GENERATED_COLUMNS483 else if( pTab->aCol[j].colFlags & COLFLAG_GENERATED ){484 testcase( pTab->aCol[j].colFlags & COLFLAG_VIRTUAL );485 testcase( pTab->aCol[j].colFlags & COLFLAG_STORED );486 sqlite3ErrorMsg(pParse,487 "cannot UPDATE generated column \"%s\"",488 pTab->aCol[j].zCnName);489 goto update_cleanup;490 }491#endif492 aXRef[j] = i;493 }else{494 if( pPk==0 && sqlite3IsRowid(pChanges->a[i].zEName) ){495 j = -1;496 chngRowid = 1;497 pRowidExpr = pChanges->a[i].pExpr;498 iRowidExpr = i;499 }else{500 sqlite3ErrorMsg(pParse, "no such column: %s", pChanges->a[i].zEName);501 pParse->checkSchema = 1;502 goto update_cleanup;503 }504 }505#ifndef SQLITE_OMIT_AUTHORIZATION506 {507 int rc;508 rc = sqlite3AuthCheck(pParse, SQLITE_UPDATE, pTab->zName,509 j<0 ? "ROWID" : pTab->aCol[j].zCnName,510 db->aDb[iDb].zDbSName);511 if( rc==SQLITE_DENY ){512 goto update_cleanup;513 }else if( rc==SQLITE_IGNORE ){514 aXRef[j] = -1;515 }516 }517#endif518 }519 assert( (chngRowid & chngPk)==0 );520 assert( chngRowid==0 || chngRowid==1 );521 assert( chngPk==0 || chngPk==1 );522 chngKey = chngRowid + chngPk;523 524#ifndef SQLITE_OMIT_GENERATED_COLUMNS525 /* Mark generated columns as changing if their generator expressions526 ** reference any changing column. The actual aXRef[] value for527 ** generated expressions is not used, other than to check to see that it528 ** is non-negative, so the value of aXRef[] for generated columns can be529 ** set to any non-negative number. We use 99999 so that the value is530 ** obvious when looking at aXRef[] in a symbolic debugger.531 */532 if( pTab->tabFlags & TF_HasGenerated ){533 int bProgress;534 testcase( pTab->tabFlags & TF_HasVirtual );535 testcase( pTab->tabFlags & TF_HasStored );536 do{537 bProgress = 0;538 for(i=0; i<pTab->nCol; i++){539 if( aXRef[i]>=0 ) continue;540 if( (pTab->aCol[i].colFlags & COLFLAG_GENERATED)==0 ) continue;541 if( sqlite3ExprReferencesUpdatedColumn(542 sqlite3ColumnExpr(pTab, &pTab->aCol[i]),543 aXRef, chngRowid)544 ){545 aXRef[i] = 99999;546 bProgress = 1;547 }548 }549 }while( bProgress );550 }551#endif552 553 /* The SET expressions are not actually used inside the WHERE loop.554 ** So reset the colUsed mask. Unless this is a virtual table. In that555 ** case, set all bits of the colUsed mask (to ensure that the virtual556 ** table implementation makes all columns available).557 */558 pTabList->a[0].colUsed = IsVirtual(pTab) ? ALLBITS : 0;559 560 hasFK = sqlite3FkRequired(pParse, pTab, aXRef, chngKey);561 562 /* There is one entry in the aRegIdx[] array for each index on the table563 ** being updated. Fill in aRegIdx[] with a register number that will hold564 ** the key for accessing each index.565 */566 if( onError==OE_Replace ) bReplace = 1;567 for(nAllIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nAllIdx++){568 int reg;569 if( chngKey || hasFK>1 || pIdx==pPk570 || indexWhereClauseMightChange(pIdx,aXRef,chngRowid)571 ){572 reg = ++pParse->nMem;573 pParse->nMem += pIdx->nColumn;574 }else{575 reg = 0;576 for(i=0; i<pIdx->nKeyCol; i++){577 if( indexColumnIsBeingUpdated(pIdx, i, aXRef, chngRowid) ){578 reg = ++pParse->nMem;579 pParse->nMem += pIdx->nColumn;580 if( onError==OE_Default && pIdx->onError==OE_Replace ){581 bReplace = 1;582 }583 break;584 }585 }586 }587 if( reg==0 ) aToOpen[nAllIdx+1] = 0;588 aRegIdx[nAllIdx] = reg;589 }590 aRegIdx[nAllIdx] = ++pParse->nMem; /* Register storing the table record */591 if( bReplace ){592 /* If REPLACE conflict resolution might be invoked, open cursors on all593 ** indexes in case they are needed to delete records. */594 memset(aToOpen, 1, nIdx+1);595 }596 597 if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);598 sqlite3BeginWriteOperation(pParse, pTrigger || hasFK, iDb);599 600 /* Allocate required registers. */601 if( !IsVirtual(pTab) ){602 /* For now, regRowSet and aRegIdx[nAllIdx] share the same register.603 ** If regRowSet turns out to be needed, then aRegIdx[nAllIdx] will be604 ** reallocated. aRegIdx[nAllIdx] is the register in which the main605 ** table record is written. regRowSet holds the RowSet for the606 ** two-pass update algorithm. */607 assert( aRegIdx[nAllIdx]==pParse->nMem );608 regRowSet = aRegIdx[nAllIdx];609 regOldRowid = regNewRowid = ++pParse->nMem;610 if( chngPk || pTrigger || hasFK ){611 regOld = pParse->nMem + 1;612 pParse->nMem += pTab->nCol;613 }614 if( chngKey || pTrigger || hasFK ){615 regNewRowid = ++pParse->nMem;616 }617 regNew = pParse->nMem + 1;618 pParse->nMem += pTab->nCol;619 }620 621 /* Start the view context. */622 if( isView ){623 sqlite3AuthContextPush(pParse, &sContext, pTab->zName);624 }625 626 /* If we are trying to update a view, realize that view into627 ** an ephemeral table.628 */629#if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER)630 if( nChangeFrom==0 && isView ){631 sqlite3MaterializeView(pParse, pTab,632 pWhere, pOrderBy, pLimit, iDataCur633 );634 pOrderBy = 0;635 pLimit = 0;636 }637#endif638 639 /* Resolve the column names in all the expressions in the640 ** WHERE clause.641 */642 if( nChangeFrom==0 && sqlite3ResolveExprNames(&sNC, pWhere) ){643 goto update_cleanup;644 }645 646#ifndef SQLITE_OMIT_VIRTUALTABLE647 /* Virtual tables must be handled separately */648 if( IsVirtual(pTab) ){649 updateVirtualTable(pParse, pTabList, pTab, pChanges, pRowidExpr, aXRef,650 pWhere, onError);651 goto update_cleanup;652 }653#endif654 655 /* Jump to labelBreak to abandon further processing of this UPDATE */656 labelContinue = labelBreak = sqlite3VdbeMakeLabel(pParse);657 658 /* Not an UPSERT. Normal processing. Begin by659 ** initialize the count of updated rows */660 if( (db->flags&SQLITE_CountRows)!=0661 && !pParse->pTriggerTab662 && !pParse->nested663 && !pParse->bReturning664 && pUpsert==0665 ){666 regRowCount = ++pParse->nMem;667 sqlite3VdbeAddOp2(v, OP_Integer, 0, regRowCount);668 }669 670 if( nChangeFrom==0 && HasRowid(pTab) ){671 sqlite3VdbeAddOp3(v, OP_Null, 0, regRowSet, regOldRowid);672 iEph = pParse->nTab++;673 addrOpen = sqlite3VdbeAddOp3(v, OP_OpenEphemeral, iEph, 0, regRowSet);674 }else{675 assert( pPk!=0 || HasRowid(pTab) );676 nPk = pPk ? pPk->nKeyCol : 0;677 iPk = pParse->nMem+1;678 pParse->nMem += nPk;679 pParse->nMem += nChangeFrom;680 regKey = ++pParse->nMem;681 if( pUpsert==0 ){682 int nEphCol = nPk + nChangeFrom + (isView ? pTab->nCol : 0);683 iEph = pParse->nTab++;684 if( pPk ) sqlite3VdbeAddOp3(v, OP_Null, 0, iPk, iPk+nPk-1);685 addrOpen = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, iEph, nEphCol);686 if( pPk ){687 KeyInfo *pKeyInfo = sqlite3KeyInfoOfIndex(pParse, pPk);688 if( pKeyInfo ){689 pKeyInfo->nAllField = nEphCol;690 sqlite3VdbeAppendP4(v, pKeyInfo, P4_KEYINFO);691 }692 }693 if( nChangeFrom ){694 updateFromSelect(695 pParse, iEph, pPk, pChanges, pTabList, pWhere, pOrderBy, pLimit696 );697#ifndef SQLITE_OMIT_SUBQUERY698 if( isView ) iDataCur = iEph;699#endif700 }701 }702 }703 704 if( nChangeFrom ){705 sqlite3MultiWrite(pParse);706 eOnePass = ONEPASS_OFF;707 nKey = nPk;708 regKey = iPk;709 }else{710 if( pUpsert ){711 /* If this is an UPSERT, then all cursors have already been opened by712 ** the outer INSERT and the data cursor should be pointing at the row713 ** that is to be updated. So bypass the code that searches for the714 ** row(s) to be updated.715 */716 pWInfo = 0;717 eOnePass = ONEPASS_SINGLE;718 sqlite3ExprIfFalse(pParse, pWhere, labelBreak, SQLITE_JUMPIFNULL);719 bFinishSeek = 0;720 }else{721 /* Begin the database scan.722 **723 ** Do not consider a single-pass strategy for a multi-row update if724 ** there is anything that might disrupt the cursor being used to do725 ** the UPDATE:726 ** (1) This is a nested UPDATE727 ** (2) There are triggers728 ** (3) There are FOREIGN KEY constraints729 ** (4) There are REPLACE conflict handlers730 ** (5) There are subqueries in the WHERE clause731 */732 flags = WHERE_ONEPASS_DESIRED;733 if( !pParse->nested734 && !pTrigger735 && !hasFK736 && !chngKey737 && !bReplace738 && (pWhere==0 || !ExprHasProperty(pWhere, EP_Subquery))739 ){740 flags |= WHERE_ONEPASS_MULTIROW;741 }742 pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere,0,0,0,flags,iIdxCur);743 if( pWInfo==0 ) goto update_cleanup;744 745 /* A one-pass strategy that might update more than one row may not746 ** be used if any column of the index used for the scan is being747 ** updated. Otherwise, if there is an index on "b", statements like748 ** the following could create an infinite loop:749 **750 ** UPDATE t1 SET b=b+1 WHERE b>?751 **752 ** Fall back to ONEPASS_OFF if where.c has selected a ONEPASS_MULTI753 ** strategy that uses an index for which one or more columns are being754 ** updated. */755 eOnePass = sqlite3WhereOkOnePass(pWInfo, aiCurOnePass);756 bFinishSeek = sqlite3WhereUsesDeferredSeek(pWInfo);757 if( eOnePass!=ONEPASS_SINGLE ){758 sqlite3MultiWrite(pParse);759 if( eOnePass==ONEPASS_MULTI ){760 int iCur = aiCurOnePass[1];761 if( iCur>=0 && iCur!=iDataCur && aToOpen[iCur-iBaseCur] ){762 eOnePass = ONEPASS_OFF;763 }764 assert( iCur!=iDataCur || !HasRowid(pTab) );765 }766 }767 }768 769 if( HasRowid(pTab) ){770 /* Read the rowid of the current row of the WHERE scan. In ONEPASS_OFF771 ** mode, write the rowid into the FIFO. In either of the one-pass modes,772 ** leave it in register regOldRowid. */773 sqlite3VdbeAddOp2(v, OP_Rowid, iDataCur, regOldRowid);774 if( eOnePass==ONEPASS_OFF ){775 aRegIdx[nAllIdx] = ++pParse->nMem;776 sqlite3VdbeAddOp3(v, OP_Insert, iEph, regRowSet, regOldRowid);777 }else{778 if( ALWAYS(addrOpen) ) sqlite3VdbeChangeToNoop(v, addrOpen);779 }780 }else{781 /* Read the PK of the current row into an array of registers. In782 ** ONEPASS_OFF mode, serialize the array into a record and store it in783 ** the ephemeral table. Or, in ONEPASS_SINGLE or MULTI mode, change784 ** the OP_OpenEphemeral instruction to a Noop (the ephemeral table785 ** is not required) and leave the PK fields in the array of registers. */786 for(i=0; i<nPk; i++){787 assert( pPk->aiColumn[i]>=0 );788 sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur,789 pPk->aiColumn[i], iPk+i);790 }791 if( eOnePass ){792 if( addrOpen ) sqlite3VdbeChangeToNoop(v, addrOpen);793 nKey = nPk;794 regKey = iPk;795 }else{796 sqlite3VdbeAddOp4(v, OP_MakeRecord, iPk, nPk, regKey,797 sqlite3IndexAffinityStr(db, pPk), nPk);798 sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iEph, regKey, iPk, nPk);799 }800 }801 }802 803 if( pUpsert==0 ){804 if( nChangeFrom==0 && eOnePass!=ONEPASS_MULTI ){805 sqlite3WhereEnd(pWInfo);806 }807 808 if( !isView ){809 int addrOnce = 0;810 int iNotUsed1 = 0;811 int iNotUsed2 = 0;812 813 /* Open every index that needs updating. */814 if( eOnePass!=ONEPASS_OFF ){815 if( aiCurOnePass[0]>=0 ) aToOpen[aiCurOnePass[0]-iBaseCur] = 0;816 if( aiCurOnePass[1]>=0 ) aToOpen[aiCurOnePass[1]-iBaseCur] = 0;817 }818 819 if( eOnePass==ONEPASS_MULTI && (nIdx-(aiCurOnePass[1]>=0))>0 ){820 addrOnce = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v);821 }822 sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite, 0, iBaseCur,823 aToOpen, &iNotUsed1, &iNotUsed2);824 if( addrOnce ){825 sqlite3VdbeJumpHereOrPopInst(v, addrOnce);826 }827 }828 829 /* Top of the update loop */830 if( eOnePass!=ONEPASS_OFF ){831 if( aiCurOnePass[0]!=iDataCur832 && aiCurOnePass[1]!=iDataCur833#ifdef SQLITE_ALLOW_ROWID_IN_VIEW834 && !isView835#endif836 ){837 assert( pPk );838 sqlite3VdbeAddOp4Int(v, OP_NotFound, iDataCur, labelBreak, regKey,nKey);839 VdbeCoverage(v);840 }841 if( eOnePass!=ONEPASS_SINGLE ){842 labelContinue = sqlite3VdbeMakeLabel(pParse);843 }844 sqlite3VdbeAddOp2(v, OP_IsNull, pPk ? regKey : regOldRowid, labelBreak);845 VdbeCoverageIf(v, pPk==0);846 VdbeCoverageIf(v, pPk!=0);847 }else if( pPk || nChangeFrom ){848 labelContinue = sqlite3VdbeMakeLabel(pParse);849 sqlite3VdbeAddOp2(v, OP_Rewind, iEph, labelBreak); VdbeCoverage(v);850 addrTop = sqlite3VdbeCurrentAddr(v);851 if( nChangeFrom ){852 if( !isView ){853 if( pPk ){854 for(i=0; i<nPk; i++){855 sqlite3VdbeAddOp3(v, OP_Column, iEph, i, iPk+i);856 }857 sqlite3VdbeAddOp4Int(858 v, OP_NotFound, iDataCur, labelContinue, iPk, nPk859 ); VdbeCoverage(v);860 }else{861 sqlite3VdbeAddOp2(v, OP_Rowid, iEph, regOldRowid);862 sqlite3VdbeAddOp3(863 v, OP_NotExists, iDataCur, labelContinue, regOldRowid864 ); VdbeCoverage(v);865 }866 }867 }else{868 sqlite3VdbeAddOp2(v, OP_RowData, iEph, regKey);869 sqlite3VdbeAddOp4Int(v, OP_NotFound, iDataCur, labelContinue, regKey,0);870 VdbeCoverage(v);871 }872 }else{873 sqlite3VdbeAddOp2(v, OP_Rewind, iEph, labelBreak); VdbeCoverage(v);874 labelContinue = sqlite3VdbeMakeLabel(pParse);875 addrTop = sqlite3VdbeAddOp2(v, OP_Rowid, iEph, regOldRowid);876 VdbeCoverage(v);877 sqlite3VdbeAddOp3(v, OP_NotExists, iDataCur, labelContinue, regOldRowid);878 VdbeCoverage(v);879 }880 }881 882 /* If the rowid value will change, set register regNewRowid to883 ** contain the new value. If the rowid is not being modified,884 ** then regNewRowid is the same register as regOldRowid, which is885 ** already populated. */886 assert( chngKey || pTrigger || hasFK || regOldRowid==regNewRowid );887 if( chngRowid ){888 assert( iRowidExpr>=0 );889 if( nChangeFrom==0 ){890 sqlite3ExprCode(pParse, pRowidExpr, regNewRowid);891 }else{892 sqlite3VdbeAddOp3(v, OP_Column, iEph, iRowidExpr, regNewRowid);893 }894 sqlite3VdbeAddOp1(v, OP_MustBeInt, regNewRowid); VdbeCoverage(v);895 }896 897 /* Compute the old pre-UPDATE content of the row being changed, if that898 ** information is needed */899 if( chngPk || hasFK || pTrigger ){900 u32 oldmask = (hasFK ? sqlite3FkOldmask(pParse, pTab) : 0);901 oldmask |= sqlite3TriggerColmask(pParse,902 pTrigger, pChanges, 0, TRIGGER_BEFORE|TRIGGER_AFTER, pTab, onError903 );904 for(i=0; i<pTab->nCol; i++){905 u32 colFlags = pTab->aCol[i].colFlags;906 k = sqlite3TableColumnToStorage(pTab, i) + regOld;907 if( oldmask==0xffffffff908 || (i<32 && (oldmask & MASKBIT32(i))!=0)909 || (colFlags & COLFLAG_PRIMKEY)!=0910 ){911 testcase( oldmask!=0xffffffff && i==31 );912 sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, i, k);913 }else{914 sqlite3VdbeAddOp2(v, OP_Null, 0, k);915 }916 }917 if( chngRowid==0 && pPk==0 ){918#ifdef SQLITE_ALLOW_ROWID_IN_VIEW919 if( isView ) sqlite3VdbeAddOp2(v, OP_Null, 0, regOldRowid);920#endif921 sqlite3VdbeAddOp2(v, OP_Copy, regOldRowid, regNewRowid);922 }923 }924 925 /* Populate the array of registers beginning at regNew with the new926 ** row data. This array is used to check constants, create the new927 ** table and index records, and as the values for any new.* references928 ** made by triggers.929 **930 ** If there are one or more BEFORE triggers, then do not populate the931 ** registers associated with columns that are (a) not modified by932 ** this UPDATE statement and (b) not accessed by new.* references. The933 ** values for registers not modified by the UPDATE must be reloaded from934 ** the database after the BEFORE triggers are fired anyway (as the trigger935 ** may have modified them). So not loading those that are not going to936 ** be used eliminates some redundant opcodes.937 */938 newmask = sqlite3TriggerColmask(939 pParse, pTrigger, pChanges, 1, TRIGGER_BEFORE, pTab, onError940 );941 for(i=0, k=regNew; i<pTab->nCol; i++, k++){942 if( i==pTab->iPKey ){943 sqlite3VdbeAddOp2(v, OP_Null, 0, k);944 }else if( (pTab->aCol[i].colFlags & COLFLAG_GENERATED)!=0 ){945 if( pTab->aCol[i].colFlags & COLFLAG_VIRTUAL ) k--;946 }else{947 j = aXRef[i];948 if( j>=0 ){949 if( nChangeFrom ){950 int nOff = (isView ? pTab->nCol : nPk);951 assert( eOnePass==ONEPASS_OFF );952 sqlite3VdbeAddOp3(v, OP_Column, iEph, nOff+j, k);953 }else{954 sqlite3ExprCode(pParse, pChanges->a[j].pExpr, k);955 }956 }else if( 0==(tmask&TRIGGER_BEFORE) || i>31 || (newmask & MASKBIT32(i)) ){957 /* This branch loads the value of a column that will not be changed958 ** into a register. This is done if there are no BEFORE triggers, or959 ** if there are one or more BEFORE triggers that use this value via960 ** a new.* reference in a trigger program.961 */962 testcase( i==31 );963 testcase( i==32 );964 sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, i, k);965 bFinishSeek = 0;966 }else{967 sqlite3VdbeAddOp2(v, OP_Null, 0, k);968 }969 }970 }971#ifndef SQLITE_OMIT_GENERATED_COLUMNS972 if( pTab->tabFlags & TF_HasGenerated ){973 testcase( pTab->tabFlags & TF_HasVirtual );974 testcase( pTab->tabFlags & TF_HasStored );975 sqlite3ComputeGeneratedColumns(pParse, regNew, pTab);976 }977#endif978 979 /* Fire any BEFORE UPDATE triggers. This happens before constraints are980 ** verified. One could argue that this is wrong.981 */982 if( tmask&TRIGGER_BEFORE ){983 sqlite3TableAffinity(v, pTab, regNew);984 sqlite3CodeRowTrigger(pParse, pTrigger, TK_UPDATE, pChanges,985 TRIGGER_BEFORE, pTab, regOldRowid, onError, labelContinue);986 987 if( !isView ){988 /* The row-trigger may have deleted the row being updated. In this989 ** case, jump to the next row. No updates or AFTER triggers are990 ** required. This behavior - what happens when the row being updated991 ** is deleted or renamed by a BEFORE trigger - is left undefined in the992 ** documentation.993 */994 if( pPk ){995 sqlite3VdbeAddOp4Int(v, OP_NotFound,iDataCur,labelContinue,regKey,nKey);996 VdbeCoverage(v);997 }else{998 sqlite3VdbeAddOp3(v, OP_NotExists, iDataCur, labelContinue,regOldRowid);999 VdbeCoverage(v);1000 }1001 1002 /* After-BEFORE-trigger-reload-loop:1003 ** If it did not delete it, the BEFORE trigger may still have modified1004 ** some of the columns of the row being updated. Load the values for1005 ** all columns not modified by the update statement into their registers1006 ** in case this has happened. Only unmodified columns are reloaded.1007 ** The values computed for modified columns use the values before the1008 ** BEFORE trigger runs. See test case trigger1-18.0 (added 2018-04-26)1009 ** for an example.1010 */1011 for(i=0, k=regNew; i<pTab->nCol; i++, k++){1012 if( pTab->aCol[i].colFlags & COLFLAG_GENERATED ){1013 if( pTab->aCol[i].colFlags & COLFLAG_VIRTUAL ) k--;1014 }else if( aXRef[i]<0 && i!=pTab->iPKey ){1015 sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, i, k);1016 }1017 }1018#ifndef SQLITE_OMIT_GENERATED_COLUMNS1019 if( pTab->tabFlags & TF_HasGenerated ){1020 testcase( pTab->tabFlags & TF_HasVirtual );1021 testcase( pTab->tabFlags & TF_HasStored );1022 sqlite3ComputeGeneratedColumns(pParse, regNew, pTab);1023 }1024#endif1025 }1026 }1027 1028 if( !isView ){1029 /* Do constraint checks. */1030 assert( regOldRowid>0 );1031 sqlite3GenerateConstraintChecks(pParse, pTab, aRegIdx, iDataCur, iIdxCur,1032 regNewRowid, regOldRowid, chngKey, onError, labelContinue, &bReplace,1033 aXRef, 0);1034 1035 /* If REPLACE conflict handling may have been used, or if the PK of the1036 ** row is changing, then the GenerateConstraintChecks() above may have1037 ** moved cursor iDataCur. Reseek it. */1038 if( bReplace || chngKey ){1039 if( pPk ){1040 sqlite3VdbeAddOp4Int(v, OP_NotFound,iDataCur,labelContinue,regKey,nKey);1041 }else{1042 sqlite3VdbeAddOp3(v, OP_NotExists, iDataCur, labelContinue,regOldRowid);1043 }1044 VdbeCoverage(v);1045 }1046 1047 /* Do FK constraint checks. */1048 if( hasFK ){1049 sqlite3FkCheck(pParse, pTab, regOldRowid, 0, aXRef, chngKey);1050 }1051 1052 /* Delete the index entries associated with the current record. */1053 sqlite3GenerateRowIndexDelete(pParse, pTab, iDataCur, iIdxCur, aRegIdx, -1);1054 1055 /* We must run the OP_FinishSeek opcode to resolve a prior1056 ** OP_DeferredSeek if there is any possibility that there have been1057 ** no OP_Column opcodes since the OP_DeferredSeek was issued. But1058 ** we want to avoid the OP_FinishSeek if possible, as running it1059 ** costs CPU cycles. */1060 if( bFinishSeek ){1061 sqlite3VdbeAddOp1(v, OP_FinishSeek, iDataCur);1062 }1063 1064 /* If changing the rowid value, or if there are foreign key constraints1065 ** to process, delete the old record. Otherwise, add a noop OP_Delete1066 ** to invoke the pre-update hook.1067 **1068 ** That (regNew==regnewRowid+1) is true is also important for the1069 ** pre-update hook. If the caller invokes preupdate_new(), the returned1070 ** value is copied from memory cell (regNewRowid+1+iCol), where iCol1071 ** is the column index supplied by the user.1072 */1073 assert( regNew==regNewRowid+1 );1074#ifdef SQLITE_ENABLE_PREUPDATE_HOOK1075 sqlite3VdbeAddOp3(v, OP_Delete, iDataCur,1076 OPFLAG_ISUPDATE | ((hasFK>1 || chngKey) ? 0 : OPFLAG_ISNOOP),1077 regNewRowid1078 );1079 if( eOnePass==ONEPASS_MULTI ){1080 assert( hasFK==0 && chngKey==0 );1081 sqlite3VdbeChangeP5(v, OPFLAG_SAVEPOSITION);1082 }1083 if( !pParse->nested ){1084 sqlite3VdbeAppendP4(v, pTab, P4_TABLE);1085 }1086#else1087 if( hasFK>1 || chngKey ){1088 sqlite3VdbeAddOp2(v, OP_Delete, iDataCur, 0);1089 }1090#endif1091 1092 if( hasFK ){1093 sqlite3FkCheck(pParse, pTab, 0, regNewRowid, aXRef, chngKey);1094 }1095 1096 /* Insert the new index entries and the new record. */1097 sqlite3CompleteInsertion(1098 pParse, pTab, iDataCur, iIdxCur, regNewRowid, aRegIdx,1099 OPFLAG_ISUPDATE | (eOnePass==ONEPASS_MULTI ? OPFLAG_SAVEPOSITION : 0),1100 0, 01101 );1102 1103 /* Do any ON CASCADE, SET NULL or SET DEFAULT operations required to1104 ** handle rows (possibly in other tables) that refer via a foreign key1105 ** to the row just updated. */1106 if( hasFK ){1107 sqlite3FkActions(pParse, pTab, pChanges, regOldRowid, aXRef, chngKey);1108 }1109 }1110 1111 /* Increment the row counter1112 */1113 if( regRowCount ){1114 sqlite3VdbeAddOp2(v, OP_AddImm, regRowCount, 1);1115 }1116 1117 if( pTrigger ){1118 sqlite3CodeRowTrigger(pParse, pTrigger, TK_UPDATE, pChanges,1119 TRIGGER_AFTER, pTab, regOldRowid, onError, labelContinue);1120 }1121 1122 /* Repeat the above with the next record to be updated, until1123 ** all record selected by the WHERE clause have been updated.1124 */1125 if( eOnePass==ONEPASS_SINGLE ){1126 /* Nothing to do at end-of-loop for a single-pass */1127 }else if( eOnePass==ONEPASS_MULTI ){1128 sqlite3VdbeResolveLabel(v, labelContinue);1129 sqlite3WhereEnd(pWInfo);1130 }else{1131 sqlite3VdbeResolveLabel(v, labelContinue);1132 sqlite3VdbeAddOp2(v, OP_Next, iEph, addrTop); VdbeCoverage(v);1133 }1134 sqlite3VdbeResolveLabel(v, labelBreak);1135 1136 /* Update the sqlite_sequence table by storing the content of the1137 ** maximum rowid counter values recorded while inserting into1138 ** autoincrement tables.1139 */1140 if( pParse->nested==0 && pParse->pTriggerTab==0 && pUpsert==0 ){1141 sqlite3AutoincrementEnd(pParse);1142 }1143 1144 /*1145 ** Return the number of rows that were changed, if we are tracking1146 ** that information.1147 */1148 if( regRowCount ){1149 sqlite3CodeChangeCount(v, regRowCount, "rows updated");1150 }1151 1152update_cleanup:1153 sqlite3AuthContextPop(&sContext);1154 sqlite3DbFree(db, aXRef); /* Also frees aRegIdx[] and aToOpen[] */1155 sqlite3SrcListDelete(db, pTabList);1156 sqlite3ExprListDelete(db, pChanges);1157 sqlite3ExprDelete(db, pWhere);1158#if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT)1159 sqlite3ExprListDelete(db, pOrderBy);1160 sqlite3ExprDelete(db, pLimit);1161#endif1162 return;1163}1164/* Make sure "isView" and other macros defined above are undefined. Otherwise1165** they may interfere with compilation of other functions in this file1166** (or in another file, if this file becomes part of the amalgamation). */1167#ifdef isView1168 #undef isView1169#endif1170#ifdef pTrigger1171 #undef pTrigger1172#endif1173 1174#ifndef SQLITE_OMIT_VIRTUALTABLE1175/*1176** Generate code for an UPDATE of a virtual table.1177**1178** There are two possible strategies - the default and the special1179** "onepass" strategy. Onepass is only used if the virtual table1180** implementation indicates that pWhere may match at most one row.1181**1182** The default strategy is to create an ephemeral table that contains1183** for each row to be changed:1184**1185** (A) The original rowid of that row.1186** (B) The revised rowid for the row.1187** (C) The content of every column in the row.1188**1189** Then loop through the contents of this ephemeral table executing a1190** VUpdate for each row. When finished, drop the ephemeral table.1191**1192** The "onepass" strategy does not use an ephemeral table. Instead, it1193** stores the same values (A, B and C above) in a register array and1194** makes a single invocation of VUpdate.1195*/1196static void updateVirtualTable(1197 Parse *pParse, /* The parsing context */1198 SrcList *pSrc, /* The virtual table to be modified */1199 Table *pTab, /* The virtual table */1200 ExprList *pChanges, /* The columns to change in the UPDATE statement */