ds@1952: /* ds@1952: ** 2001 September 15 ds@1952: ** ds@1952: ** The author disclaims copyright to this source code. In place of ds@1952: ** a legal notice, here is a blessing: ds@1952: ** ds@1952: ** May you do good and not evil. ds@1952: ** May you find forgiveness for yourself and forgive others. ds@1952: ** May you share freely, never taking more than you give. ds@1952: ** ds@1952: ************************************************************************* ds@1952: ** This file contains C code routines that are called by the parser ds@1952: ** to handle UPDATE statements. ds@1952: ** ds@1952: ** $Id$ ds@1952: */ ds@1952: #include "sqliteInt.h" ds@1952: ds@1952: #ifndef SQLITE_OMIT_VIRTUALTABLE ds@1952: /* Forward declaration */ ds@1952: static void updateVirtualTable( ds@1952: Parse *pParse, /* The parsing context */ ds@1952: SrcList *pSrc, /* The virtual table to be modified */ ds@1952: Table *pTab, /* The virtual table */ ds@1952: ExprList *pChanges, /* The columns to change in the UPDATE statement */ ds@1952: Expr *pRowidExpr, /* Expression used to recompute the rowid */ ds@1952: int *aXRef, /* Mapping from columns of pTab to entries in pChanges */ ds@1952: Expr *pWhere /* WHERE clause of the UPDATE statement */ ds@1952: ); ds@1952: #endif /* SQLITE_OMIT_VIRTUALTABLE */ ds@1952: ds@1952: /* ds@1952: ** The most recently coded instruction was an OP_Column to retrieve the ds@1952: ** i-th column of table pTab. This routine sets the P3 parameter of the ds@1952: ** OP_Column to the default value, if any. ds@1952: ** ds@1952: ** The default value of a column is specified by a DEFAULT clause in the ds@1952: ** column definition. This was either supplied by the user when the table ds@1952: ** was created, or added later to the table definition by an ALTER TABLE ds@1952: ** command. If the latter, then the row-records in the table btree on disk ds@1952: ** may not contain a value for the column and the default value, taken ds@1952: ** from the P3 parameter of the OP_Column instruction, is returned instead. ds@1952: ** If the former, then all row-records are guaranteed to include a value ds@1952: ** for the column and the P3 value is not required. ds@1952: ** ds@1952: ** Column definitions created by an ALTER TABLE command may only have ds@1952: ** literal default values specified: a number, null or a string. (If a more ds@1952: ** complicated default expression value was provided, it is evaluated ds@1952: ** when the ALTER TABLE is executed and one of the literal values written ds@1952: ** into the sqlite_master table.) ds@1952: ** ds@1952: ** Therefore, the P3 parameter is only required if the default value for ds@1952: ** the column is a literal number, string or null. The sqlite3ValueFromExpr() ds@1952: ** function is capable of transforming these types of expressions into ds@1952: ** sqlite3_value objects. ds@1952: */ ds@1952: void sqlite3ColumnDefault(Vdbe *v, Table *pTab, int i){ ds@1952: if( pTab && !pTab->pSelect ){ ds@1952: sqlite3_value *pValue; ds@1952: u8 enc = ENC(sqlite3VdbeDb(v)); ds@1952: Column *pCol = &pTab->aCol[i]; ds@1952: assert( inCol ); ds@1952: sqlite3ValueFromExpr(sqlite3VdbeDb(v), pCol->pDflt, enc, pCol->affinity, &pValue); ds@1952: if( pValue ){ ds@1952: sqlite3VdbeChangeP3(v, -1, (const char *)pValue, P3_MEM); ds@1952: }else{ ds@1952: VdbeComment((v, "# %s.%s", pTab->zName, pCol->zName)); ds@1952: } ds@1952: } ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Process an UPDATE statement. ds@1952: ** ds@1952: ** UPDATE OR IGNORE table_wxyz SET a=b, c=d WHERE e<5 AND f NOT NULL; ds@1952: ** \_______/ \________/ \______/ \________________/ ds@1952: * onError pTabList pChanges pWhere ds@1952: */ ds@1952: void sqlite3Update( ds@1952: Parse *pParse, /* The parser context */ ds@1952: SrcList *pTabList, /* The table in which we should change things */ ds@1952: ExprList *pChanges, /* Things to be changed */ ds@1952: Expr *pWhere, /* The WHERE clause. May be null */ ds@1952: int onError /* How to handle constraint errors */ ds@1952: ){ ds@1952: int i, j; /* Loop counters */ ds@1952: Table *pTab; /* The table to be updated */ ds@1952: int addr = 0; /* VDBE instruction address of the start of the loop */ ds@1952: WhereInfo *pWInfo; /* Information about the WHERE clause */ ds@1952: Vdbe *v; /* The virtual database engine */ ds@1952: Index *pIdx; /* For looping over indices */ ds@1952: int nIdx; /* Number of indices that need updating */ ds@1952: int nIdxTotal; /* Total number of indices */ ds@1952: int iCur; /* VDBE Cursor number of pTab */ ds@1952: sqlite3 *db; /* The database structure */ ds@1952: Index **apIdx = 0; /* An array of indices that need updating too */ ds@1952: char *aIdxUsed = 0; /* aIdxUsed[i]==1 if the i-th index is used */ ds@1952: int *aXRef = 0; /* aXRef[i] is the index in pChanges->a[] of the ds@1952: ** an expression for the i-th column of the table. ds@1952: ** aXRef[i]==-1 if the i-th column is not changed. */ ds@1952: int chngRowid; /* True if the record number is being changed */ ds@1952: Expr *pRowidExpr = 0; /* Expression defining the new record number */ ds@1952: int openAll = 0; /* True if all indices need to be opened */ ds@1952: AuthContext sContext; /* The authorization context */ ds@1952: NameContext sNC; /* The name-context to resolve expressions in */ ds@1952: int iDb; /* Database containing the table being updated */ ds@1952: int memCnt = 0; /* Memory cell used for counting rows changed */ ds@1952: ds@1952: #ifndef SQLITE_OMIT_TRIGGER ds@1952: int isView; /* Trying to update a view */ ds@1952: int triggers_exist = 0; /* True if any row triggers exist */ ds@1952: #endif ds@1952: ds@1952: int newIdx = -1; /* index of trigger "new" temp table */ ds@1952: int oldIdx = -1; /* index of trigger "old" temp table */ ds@1952: ds@1952: sContext.pParse = 0; ds@1952: db = pParse->db; ds@1952: if( pParse->nErr || db->mallocFailed ){ ds@1952: goto update_cleanup; ds@1952: } ds@1952: assert( pTabList->nSrc==1 ); ds@1952: ds@1952: /* Locate the table which we want to update. ds@1952: */ ds@1952: pTab = sqlite3SrcListLookup(pParse, pTabList); ds@1952: if( pTab==0 ) goto update_cleanup; ds@1952: iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema); ds@1952: ds@1952: /* Figure out if we have any triggers and if the table being ds@1952: ** updated is a view ds@1952: */ ds@1952: #ifndef SQLITE_OMIT_TRIGGER ds@1952: triggers_exist = sqlite3TriggersExist(pParse, pTab, TK_UPDATE, pChanges); ds@1952: isView = pTab->pSelect!=0; ds@1952: #else ds@1952: # define triggers_exist 0 ds@1952: # define isView 0 ds@1952: #endif ds@1952: #ifdef SQLITE_OMIT_VIEW ds@1952: # undef isView ds@1952: # define isView 0 ds@1952: #endif ds@1952: ds@1952: if( sqlite3IsReadOnly(pParse, pTab, triggers_exist) ){ ds@1952: goto update_cleanup; ds@1952: } ds@1952: if( sqlite3ViewGetColumnNames(pParse, pTab) ){ ds@1952: goto update_cleanup; ds@1952: } ds@1952: aXRef = sqlite3DbMallocRaw(db, sizeof(int) * pTab->nCol ); ds@1952: if( aXRef==0 ) goto update_cleanup; ds@1952: for(i=0; inCol; i++) aXRef[i] = -1; ds@1952: ds@1952: /* If there are FOR EACH ROW triggers, allocate cursors for the ds@1952: ** special OLD and NEW tables ds@1952: */ ds@1952: if( triggers_exist ){ ds@1952: newIdx = pParse->nTab++; ds@1952: oldIdx = pParse->nTab++; ds@1952: } ds@1952: ds@1952: /* Allocate a cursors for the main database table and for all indices. ds@1952: ** The index cursors might not be used, but if they are used they ds@1952: ** need to occur right after the database cursor. So go ahead and ds@1952: ** allocate enough space, just in case. ds@1952: */ ds@1952: pTabList->a[0].iCursor = iCur = pParse->nTab++; ds@1952: for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ ds@1952: pParse->nTab++; ds@1952: } ds@1952: ds@1952: /* Initialize the name-context */ ds@1952: memset(&sNC, 0, sizeof(sNC)); ds@1952: sNC.pParse = pParse; ds@1952: sNC.pSrcList = pTabList; ds@1952: ds@1952: /* Resolve the column names in all the expressions of the ds@1952: ** of the UPDATE statement. Also find the column index ds@1952: ** for each column to be updated in the pChanges array. For each ds@1952: ** column to be updated, make sure we have authorization to change ds@1952: ** that column. ds@1952: */ ds@1952: chngRowid = 0; ds@1952: for(i=0; inExpr; i++){ ds@1952: if( sqlite3ExprResolveNames(&sNC, pChanges->a[i].pExpr) ){ ds@1952: goto update_cleanup; ds@1952: } ds@1952: for(j=0; jnCol; j++){ ds@1952: if( sqlite3StrICmp(pTab->aCol[j].zName, pChanges->a[i].zName)==0 ){ ds@1952: if( j==pTab->iPKey ){ ds@1952: chngRowid = 1; ds@1952: pRowidExpr = pChanges->a[i].pExpr; ds@1952: } ds@1952: aXRef[j] = i; ds@1952: break; ds@1952: } ds@1952: } ds@1952: if( j>=pTab->nCol ){ ds@1952: if( sqlite3IsRowid(pChanges->a[i].zName) ){ ds@1952: chngRowid = 1; ds@1952: pRowidExpr = pChanges->a[i].pExpr; ds@1952: }else{ ds@1952: sqlite3ErrorMsg(pParse, "no such column: %s", pChanges->a[i].zName); ds@1952: goto update_cleanup; ds@1952: } ds@1952: } ds@1952: #ifndef SQLITE_OMIT_AUTHORIZATION ds@1952: { ds@1952: int rc; ds@1952: rc = sqlite3AuthCheck(pParse, SQLITE_UPDATE, pTab->zName, ds@1952: pTab->aCol[j].zName, db->aDb[iDb].zName); ds@1952: if( rc==SQLITE_DENY ){ ds@1952: goto update_cleanup; ds@1952: }else if( rc==SQLITE_IGNORE ){ ds@1952: aXRef[j] = -1; ds@1952: } ds@1952: } ds@1952: #endif ds@1952: } ds@1952: ds@1952: /* Allocate memory for the array apIdx[] and fill it with pointers to every ds@1952: ** index that needs to be updated. Indices only need updating if their ds@1952: ** key includes one of the columns named in pChanges or if the record ds@1952: ** number of the original table entry is changing. ds@1952: */ ds@1952: for(nIdx=nIdxTotal=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdxTotal++){ ds@1952: if( chngRowid ){ ds@1952: i = 0; ds@1952: }else { ds@1952: for(i=0; inColumn; i++){ ds@1952: if( aXRef[pIdx->aiColumn[i]]>=0 ) break; ds@1952: } ds@1952: } ds@1952: if( inColumn ) nIdx++; ds@1952: } ds@1952: if( nIdxTotal>0 ){ ds@1952: apIdx = sqlite3DbMallocRaw(db, sizeof(Index*) * nIdx + nIdxTotal ); ds@1952: if( apIdx==0 ) goto update_cleanup; ds@1952: aIdxUsed = (char*)&apIdx[nIdx]; ds@1952: } ds@1952: for(nIdx=j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){ ds@1952: if( chngRowid ){ ds@1952: i = 0; ds@1952: }else{ ds@1952: for(i=0; inColumn; i++){ ds@1952: if( aXRef[pIdx->aiColumn[i]]>=0 ) break; ds@1952: } ds@1952: } ds@1952: if( inColumn ){ ds@1952: apIdx[nIdx++] = pIdx; ds@1952: aIdxUsed[j] = 1; ds@1952: }else{ ds@1952: aIdxUsed[j] = 0; ds@1952: } ds@1952: } ds@1952: ds@1952: /* Begin generating code. ds@1952: */ ds@1952: v = sqlite3GetVdbe(pParse); ds@1952: if( v==0 ) goto update_cleanup; ds@1952: if( pParse->nested==0 ) sqlite3VdbeCountChanges(v); ds@1952: sqlite3BeginWriteOperation(pParse, 1, iDb); ds@1952: ds@1952: #ifndef SQLITE_OMIT_VIRTUALTABLE ds@1952: /* Virtual tables must be handled separately */ ds@1952: if( IsVirtual(pTab) ){ ds@1952: updateVirtualTable(pParse, pTabList, pTab, pChanges, pRowidExpr, aXRef, ds@1952: pWhere); ds@1952: pWhere = 0; ds@1952: pTabList = 0; ds@1952: goto update_cleanup; ds@1952: } ds@1952: #endif ds@1952: ds@1952: /* Resolve the column names in all the expressions in the ds@1952: ** WHERE clause. ds@1952: */ ds@1952: if( sqlite3ExprResolveNames(&sNC, pWhere) ){ ds@1952: goto update_cleanup; ds@1952: } ds@1952: ds@1952: /* Start the view context ds@1952: */ ds@1952: if( isView ){ ds@1952: sqlite3AuthContextPush(pParse, &sContext, pTab->zName); ds@1952: } ds@1952: ds@1952: /* If we are trying to update a view, realize that view into ds@1952: ** a ephemeral table. ds@1952: */ ds@1952: if( isView ){ ds@1952: Select *pView; ds@1952: pView = sqlite3SelectDup(db, pTab->pSelect); ds@1952: sqlite3Select(pParse, pView, SRT_EphemTab, iCur, 0, 0, 0, 0); ds@1952: sqlite3SelectDelete(pView); ds@1952: } ds@1952: ds@1952: /* Begin the database scan ds@1952: */ ds@1952: pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, 0); ds@1952: if( pWInfo==0 ) goto update_cleanup; ds@1952: ds@1952: /* Remember the rowid of every item to be updated. ds@1952: */ ds@1952: sqlite3VdbeAddOp(v, IsVirtual(pTab) ? OP_VRowid : OP_Rowid, iCur, 0); ds@1952: sqlite3VdbeAddOp(v, OP_FifoWrite, 0, 0); ds@1952: ds@1952: /* End the database scan loop. ds@1952: */ ds@1952: sqlite3WhereEnd(pWInfo); ds@1952: ds@1952: /* Initialize the count of updated rows ds@1952: */ ds@1952: if( db->flags & SQLITE_CountRows && !pParse->trigStack ){ ds@1952: memCnt = pParse->nMem++; ds@1952: sqlite3VdbeAddOp(v, OP_MemInt, 0, memCnt); ds@1952: } ds@1952: ds@1952: if( triggers_exist ){ ds@1952: /* Create pseudo-tables for NEW and OLD ds@1952: */ ds@1952: sqlite3VdbeAddOp(v, OP_OpenPseudo, oldIdx, 0); ds@1952: sqlite3VdbeAddOp(v, OP_SetNumColumns, oldIdx, pTab->nCol); ds@1952: sqlite3VdbeAddOp(v, OP_OpenPseudo, newIdx, 0); ds@1952: sqlite3VdbeAddOp(v, OP_SetNumColumns, newIdx, pTab->nCol); ds@1952: ds@1952: /* The top of the update loop for when there are triggers. ds@1952: */ ds@1952: addr = sqlite3VdbeAddOp(v, OP_FifoRead, 0, 0); ds@1952: ds@1952: if( !isView ){ ds@1952: sqlite3VdbeAddOp(v, OP_Dup, 0, 0); ds@1952: sqlite3VdbeAddOp(v, OP_Dup, 0, 0); ds@1952: /* Open a cursor and make it point to the record that is ds@1952: ** being updated. ds@1952: */ ds@1952: sqlite3OpenTable(pParse, iCur, iDb, pTab, OP_OpenRead); ds@1952: } ds@1952: sqlite3VdbeAddOp(v, OP_MoveGe, iCur, 0); ds@1952: ds@1952: /* Generate the OLD table ds@1952: */ ds@1952: sqlite3VdbeAddOp(v, OP_Rowid, iCur, 0); ds@1952: sqlite3VdbeAddOp(v, OP_RowData, iCur, 0); ds@1952: sqlite3VdbeAddOp(v, OP_Insert, oldIdx, 0); ds@1952: ds@1952: /* Generate the NEW table ds@1952: */ ds@1952: if( chngRowid ){ ds@1952: sqlite3ExprCodeAndCache(pParse, pRowidExpr); ds@1952: }else{ ds@1952: sqlite3VdbeAddOp(v, OP_Rowid, iCur, 0); ds@1952: } ds@1952: for(i=0; inCol; i++){ ds@1952: if( i==pTab->iPKey ){ ds@1952: sqlite3VdbeAddOp(v, OP_Null, 0, 0); ds@1952: continue; ds@1952: } ds@1952: j = aXRef[i]; ds@1952: if( j<0 ){ ds@1952: sqlite3VdbeAddOp(v, OP_Column, iCur, i); ds@1952: sqlite3ColumnDefault(v, pTab, i); ds@1952: }else{ ds@1952: sqlite3ExprCodeAndCache(pParse, pChanges->a[j].pExpr); ds@1952: } ds@1952: } ds@1952: sqlite3VdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0); ds@1952: if( !isView ){ ds@1952: sqlite3TableAffinityStr(v, pTab); ds@1952: } ds@1952: if( pParse->nErr ) goto update_cleanup; ds@1952: sqlite3VdbeAddOp(v, OP_Insert, newIdx, 0); ds@1952: if( !isView ){ ds@1952: sqlite3VdbeAddOp(v, OP_Close, iCur, 0); ds@1952: } ds@1952: ds@1952: /* Fire the BEFORE and INSTEAD OF triggers ds@1952: */ ds@1952: if( sqlite3CodeRowTrigger(pParse, TK_UPDATE, pChanges, TRIGGER_BEFORE, pTab, ds@1952: newIdx, oldIdx, onError, addr) ){ ds@1952: goto update_cleanup; ds@1952: } ds@1952: } ds@1952: ds@1952: if( !isView && !IsVirtual(pTab) ){ ds@1952: /* ds@1952: ** Open every index that needs updating. Note that if any ds@1952: ** index could potentially invoke a REPLACE conflict resolution ds@1952: ** action, then we need to open all indices because we might need ds@1952: ** to be deleting some records. ds@1952: */ ds@1952: sqlite3OpenTable(pParse, iCur, iDb, pTab, OP_OpenWrite); ds@1952: if( onError==OE_Replace ){ ds@1952: openAll = 1; ds@1952: }else{ ds@1952: openAll = 0; ds@1952: for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ ds@1952: if( pIdx->onError==OE_Replace ){ ds@1952: openAll = 1; ds@1952: break; ds@1952: } ds@1952: } ds@1952: } ds@1952: for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){ ds@1952: if( openAll || aIdxUsed[i] ){ ds@1952: KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIdx); ds@1952: sqlite3VdbeAddOp(v, OP_Integer, iDb, 0); ds@1952: sqlite3VdbeOp3(v, OP_OpenWrite, iCur+i+1, pIdx->tnum, ds@1952: (char*)pKey, P3_KEYINFO_HANDOFF); ds@1952: assert( pParse->nTab>iCur+i+1 ); ds@1952: } ds@1952: } ds@1952: ds@1952: /* Loop over every record that needs updating. We have to load ds@1952: ** the old data for each record to be updated because some columns ds@1952: ** might not change and we will need to copy the old value. ds@1952: ** Also, the old data is needed to delete the old index entries. ds@1952: ** So make the cursor point at the old record. ds@1952: */ ds@1952: if( !triggers_exist ){ ds@1952: addr = sqlite3VdbeAddOp(v, OP_FifoRead, 0, 0); ds@1952: sqlite3VdbeAddOp(v, OP_Dup, 0, 0); ds@1952: } ds@1952: sqlite3VdbeAddOp(v, OP_NotExists, iCur, addr); ds@1952: ds@1952: /* If the record number will change, push the record number as it ds@1952: ** will be after the update. (The old record number is currently ds@1952: ** on top of the stack.) ds@1952: */ ds@1952: if( chngRowid ){ ds@1952: sqlite3ExprCode(pParse, pRowidExpr); ds@1952: sqlite3VdbeAddOp(v, OP_MustBeInt, 0, 0); ds@1952: } ds@1952: ds@1952: /* Compute new data for this record. ds@1952: */ ds@1952: for(i=0; inCol; i++){ ds@1952: if( i==pTab->iPKey ){ ds@1952: sqlite3VdbeAddOp(v, OP_Null, 0, 0); ds@1952: continue; ds@1952: } ds@1952: j = aXRef[i]; ds@1952: if( j<0 ){ ds@1952: sqlite3VdbeAddOp(v, OP_Column, iCur, i); ds@1952: sqlite3ColumnDefault(v, pTab, i); ds@1952: }else{ ds@1952: sqlite3ExprCode(pParse, pChanges->a[j].pExpr); ds@1952: } ds@1952: } ds@1952: ds@1952: /* Do constraint checks ds@1952: */ ds@1952: sqlite3GenerateConstraintChecks(pParse, pTab, iCur, aIdxUsed, chngRowid, 1, ds@1952: onError, addr); ds@1952: ds@1952: /* Delete the old indices for the current record. ds@1952: */ ds@1952: sqlite3GenerateRowIndexDelete(v, pTab, iCur, aIdxUsed); ds@1952: ds@1952: /* If changing the record number, delete the old record. ds@1952: */ ds@1952: if( chngRowid ){ ds@1952: sqlite3VdbeAddOp(v, OP_Delete, iCur, 0); ds@1952: } ds@1952: ds@1952: /* Create the new index entries and the new record. ds@1952: */ ds@1952: sqlite3CompleteInsertion(pParse, pTab, iCur, aIdxUsed, chngRowid, 1, -1, 0); ds@1952: } ds@1952: ds@1952: /* Increment the row counter ds@1952: */ ds@1952: if( db->flags & SQLITE_CountRows && !pParse->trigStack){ ds@1952: sqlite3VdbeAddOp(v, OP_MemIncr, 1, memCnt); ds@1952: } ds@1952: ds@1952: /* If there are triggers, close all the cursors after each iteration ds@1952: ** through the loop. The fire the after triggers. ds@1952: */ ds@1952: if( triggers_exist ){ ds@1952: if( !isView ){ ds@1952: for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){ ds@1952: if( openAll || aIdxUsed[i] ) ds@1952: sqlite3VdbeAddOp(v, OP_Close, iCur+i+1, 0); ds@1952: } ds@1952: sqlite3VdbeAddOp(v, OP_Close, iCur, 0); ds@1952: } ds@1952: if( sqlite3CodeRowTrigger(pParse, TK_UPDATE, pChanges, TRIGGER_AFTER, pTab, ds@1952: newIdx, oldIdx, onError, addr) ){ ds@1952: goto update_cleanup; ds@1952: } ds@1952: } ds@1952: ds@1952: /* Repeat the above with the next record to be updated, until ds@1952: ** all record selected by the WHERE clause have been updated. ds@1952: */ ds@1952: sqlite3VdbeAddOp(v, OP_Goto, 0, addr); ds@1952: sqlite3VdbeJumpHere(v, addr); ds@1952: ds@1952: /* Close all tables if there were no FOR EACH ROW triggers */ ds@1952: if( !triggers_exist ){ ds@1952: for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){ ds@1952: if( openAll || aIdxUsed[i] ){ ds@1952: sqlite3VdbeAddOp(v, OP_Close, iCur+i+1, 0); ds@1952: } ds@1952: } ds@1952: sqlite3VdbeAddOp(v, OP_Close, iCur, 0); ds@1952: }else{ ds@1952: sqlite3VdbeAddOp(v, OP_Close, newIdx, 0); ds@1952: sqlite3VdbeAddOp(v, OP_Close, oldIdx, 0); ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Return the number of rows that were changed. If this routine is ds@1952: ** generating code because of a call to sqlite3NestedParse(), do not ds@1952: ** invoke the callback function. ds@1952: */ ds@1952: if( db->flags & SQLITE_CountRows && !pParse->trigStack && pParse->nested==0 ){ ds@1952: sqlite3VdbeAddOp(v, OP_MemLoad, memCnt, 0); ds@1952: sqlite3VdbeAddOp(v, OP_Callback, 1, 0); ds@1952: sqlite3VdbeSetNumCols(v, 1); ds@1952: sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "rows updated", P3_STATIC); ds@1952: } ds@1952: ds@1952: update_cleanup: ds@1952: sqlite3AuthContextPop(&sContext); ds@1952: sqlite3_free(apIdx); ds@1952: sqlite3_free(aXRef); ds@1952: sqlite3SrcListDelete(pTabList); ds@1952: sqlite3ExprListDelete(pChanges); ds@1952: sqlite3ExprDelete(pWhere); ds@1952: return; ds@1952: } ds@1952: ds@1952: #ifndef SQLITE_OMIT_VIRTUALTABLE ds@1952: /* ds@1952: ** Generate code for an UPDATE of a virtual table. ds@1952: ** ds@1952: ** The strategy is that we create an ephemerial table that contains ds@1952: ** for each row to be changed: ds@1952: ** ds@1952: ** (A) The original rowid of that row. ds@1952: ** (B) The revised rowid for the row. (note1) ds@1952: ** (C) The content of every column in the row. ds@1952: ** ds@1952: ** Then we loop over this ephemeral table and for each row in ds@1952: ** the ephermeral table call VUpdate. ds@1952: ** ds@1952: ** When finished, drop the ephemeral table. ds@1952: ** ds@1952: ** (note1) Actually, if we know in advance that (A) is always the same ds@1952: ** as (B) we only store (A), then duplicate (A) when pulling ds@1952: ** it out of the ephemeral table before calling VUpdate. ds@1952: */ ds@1952: static void updateVirtualTable( ds@1952: Parse *pParse, /* The parsing context */ ds@1952: SrcList *pSrc, /* The virtual table to be modified */ ds@1952: Table *pTab, /* The virtual table */ ds@1952: ExprList *pChanges, /* The columns to change in the UPDATE statement */ ds@1952: Expr *pRowid, /* Expression used to recompute the rowid */ ds@1952: int *aXRef, /* Mapping from columns of pTab to entries in pChanges */ ds@1952: Expr *pWhere /* WHERE clause of the UPDATE statement */ ds@1952: ){ ds@1952: Vdbe *v = pParse->pVdbe; /* Virtual machine under construction */ ds@1952: ExprList *pEList = 0; /* The result set of the SELECT statement */ ds@1952: Select *pSelect = 0; /* The SELECT statement */ ds@1952: Expr *pExpr; /* Temporary expression */ ds@1952: int ephemTab; /* Table holding the result of the SELECT */ ds@1952: int i; /* Loop counter */ ds@1952: int addr; /* Address of top of loop */ ds@1952: sqlite3 *db = pParse->db; /* Database connection */ ds@1952: ds@1952: /* Construct the SELECT statement that will find the new values for ds@1952: ** all updated rows. ds@1952: */ ds@1952: pEList = sqlite3ExprListAppend(pParse, 0, ds@1952: sqlite3CreateIdExpr(pParse, "_rowid_"), 0); ds@1952: if( pRowid ){ ds@1952: pEList = sqlite3ExprListAppend(pParse, pEList, ds@1952: sqlite3ExprDup(db, pRowid), 0); ds@1952: } ds@1952: assert( pTab->iPKey<0 ); ds@1952: for(i=0; inCol; i++){ ds@1952: if( aXRef[i]>=0 ){ ds@1952: pExpr = sqlite3ExprDup(db, pChanges->a[aXRef[i]].pExpr); ds@1952: }else{ ds@1952: pExpr = sqlite3CreateIdExpr(pParse, pTab->aCol[i].zName); ds@1952: } ds@1952: pEList = sqlite3ExprListAppend(pParse, pEList, pExpr, 0); ds@1952: } ds@1952: pSelect = sqlite3SelectNew(pParse, pEList, pSrc, pWhere, 0, 0, 0, 0, 0, 0); ds@1952: ds@1952: /* Create the ephemeral table into which the update results will ds@1952: ** be stored. ds@1952: */ ds@1952: assert( v ); ds@1952: ephemTab = pParse->nTab++; ds@1952: sqlite3VdbeAddOp(v, OP_OpenEphemeral, ephemTab, pTab->nCol+1+(pRowid!=0)); ds@1952: ds@1952: /* fill the ephemeral table ds@1952: */ ds@1952: sqlite3Select(pParse, pSelect, SRT_Table, ephemTab, 0, 0, 0, 0); ds@1952: ds@1952: /* ds@1952: ** Generate code to scan the ephemeral table and call VDelete and ds@1952: ** VInsert ds@1952: */ ds@1952: sqlite3VdbeAddOp(v, OP_Rewind, ephemTab, 0); ds@1952: addr = sqlite3VdbeCurrentAddr(v); ds@1952: sqlite3VdbeAddOp(v, OP_Column, ephemTab, 0); ds@1952: if( pRowid ){ ds@1952: sqlite3VdbeAddOp(v, OP_Column, ephemTab, 1); ds@1952: }else{ ds@1952: sqlite3VdbeAddOp(v, OP_Dup, 0, 0); ds@1952: } ds@1952: for(i=0; inCol; i++){ ds@1952: sqlite3VdbeAddOp(v, OP_Column, ephemTab, i+1+(pRowid!=0)); ds@1952: } ds@1952: pParse->pVirtualLock = pTab; ds@1952: sqlite3VdbeOp3(v, OP_VUpdate, 0, pTab->nCol+2, ds@1952: (const char*)pTab->pVtab, P3_VTAB); ds@1952: sqlite3VdbeAddOp(v, OP_Next, ephemTab, addr); ds@1952: sqlite3VdbeJumpHere(v, addr-1); ds@1952: sqlite3VdbeAddOp(v, OP_Close, ephemTab, 0); ds@1952: ds@1952: /* Cleanup */ ds@1952: sqlite3SelectDelete(pSelect); ds@1952: } ds@1952: #endif /* SQLITE_OMIT_VIRTUALTABLE */