2N/A
2N/A#pragma ident "%Z%%M% %I% %E% SMI"
2N/A
2N/A/*
2N/A** 2001 September 15
2N/A**
2N/A** The author disclaims copyright to this source code. In place of
2N/A** a legal notice, here is a blessing:
2N/A**
2N/A** May you do good and not evil.
2N/A** May you find forgiveness for yourself and forgive others.
2N/A** May you share freely, never taking more than you give.
2N/A**
2N/A*************************************************************************
2N/A** This file contains C code routines that are called by the parser
2N/A** to handle UPDATE statements.
2N/A**
2N/A** $Id: update.c,v 1.70.2.1 2004/04/29 16:16:29 drh Exp $
2N/A*/
2N/A#include "sqliteInt.h"
2N/A
2N/A/*
2N/A** Process an UPDATE statement.
2N/A**
2N/A** UPDATE OR IGNORE table_wxyz SET a=b, c=d WHERE e<5 AND f NOT NULL;
2N/A** \_______/ \________/ \______/ \________________/
2N/A* onError pTabList pChanges pWhere
2N/A*/
2N/Avoid sqliteUpdate(
2N/A Parse *pParse, /* The parser context */
2N/A SrcList *pTabList, /* The table in which we should change things */
2N/A ExprList *pChanges, /* Things to be changed */
2N/A Expr *pWhere, /* The WHERE clause. May be null */
2N/A int onError /* How to handle constraint errors */
2N/A){
2N/A int i, j; /* Loop counters */
2N/A Table *pTab; /* The table to be updated */
2N/A int loopStart; /* VDBE instruction address of the start of the loop */
2N/A int jumpInst; /* Addr of VDBE instruction to jump out of loop */
2N/A WhereInfo *pWInfo; /* Information about the WHERE clause */
2N/A Vdbe *v; /* The virtual database engine */
2N/A Index *pIdx; /* For looping over indices */
2N/A int nIdx; /* Number of indices that need updating */
2N/A int nIdxTotal; /* Total number of indices */
2N/A int iCur; /* VDBE Cursor number of pTab */
2N/A sqlite *db; /* The database structure */
2N/A Index **apIdx = 0; /* An array of indices that need updating too */
2N/A char *aIdxUsed = 0; /* aIdxUsed[i]==1 if the i-th index is used */
2N/A int *aXRef = 0; /* aXRef[i] is the index in pChanges->a[] of the
2N/A ** an expression for the i-th column of the table.
2N/A ** aXRef[i]==-1 if the i-th column is not changed. */
2N/A int chngRecno; /* True if the record number is being changed */
2N/A Expr *pRecnoExpr; /* Expression defining the new record number */
2N/A int openAll; /* True if all indices need to be opened */
2N/A int isView; /* Trying to update a view */
2N/A int iStackDepth; /* Index of memory cell holding stack depth */
2N/A AuthContext sContext; /* The authorization context */
2N/A
2N/A int before_triggers; /* True if there are any BEFORE triggers */
2N/A int after_triggers; /* True if there are any AFTER triggers */
2N/A int row_triggers_exist = 0; /* True if any row triggers exist */
2N/A
2N/A int newIdx = -1; /* index of trigger "new" temp table */
2N/A int oldIdx = -1; /* index of trigger "old" temp table */
2N/A
2N/A sContext.pParse = 0;
2N/A if( pParse->nErr || sqlite_malloc_failed ) goto update_cleanup;
2N/A db = pParse->db;
2N/A assert( pTabList->nSrc==1 );
2N/A iStackDepth = pParse->nMem++;
2N/A
2N/A /* Locate the table which we want to update.
2N/A */
2N/A pTab = sqliteSrcListLookup(pParse, pTabList);
2N/A if( pTab==0 ) goto update_cleanup;
2N/A before_triggers = sqliteTriggersExist(pParse, pTab->pTrigger,
2N/A TK_UPDATE, TK_BEFORE, TK_ROW, pChanges);
2N/A after_triggers = sqliteTriggersExist(pParse, pTab->pTrigger,
2N/A TK_UPDATE, TK_AFTER, TK_ROW, pChanges);
2N/A row_triggers_exist = before_triggers || after_triggers;
2N/A isView = pTab->pSelect!=0;
2N/A if( sqliteIsReadOnly(pParse, pTab, before_triggers) ){
2N/A goto update_cleanup;
2N/A }
2N/A if( isView ){
2N/A if( sqliteViewGetColumnNames(pParse, pTab) ){
2N/A goto update_cleanup;
2N/A }
2N/A }
2N/A aXRef = sqliteMalloc( sizeof(int) * pTab->nCol );
2N/A if( aXRef==0 ) goto update_cleanup;
2N/A for(i=0; i<pTab->nCol; i++) aXRef[i] = -1;
2N/A
2N/A /* If there are FOR EACH ROW triggers, allocate cursors for the
2N/A ** special OLD and NEW tables
2N/A */
2N/A if( row_triggers_exist ){
2N/A newIdx = pParse->nTab++;
2N/A oldIdx = pParse->nTab++;
2N/A }
2N/A
2N/A /* Allocate a cursors for the main database table and for all indices.
2N/A ** The index cursors might not be used, but if they are used they
2N/A ** need to occur right after the database cursor. So go ahead and
2N/A ** allocate enough space, just in case.
2N/A */
2N/A pTabList->a[0].iCursor = iCur = pParse->nTab++;
2N/A for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
2N/A pParse->nTab++;
2N/A }
2N/A
2N/A /* Resolve the column names in all the expressions of the
2N/A ** of the UPDATE statement. Also find the column index
2N/A ** for each column to be updated in the pChanges array. For each
2N/A ** column to be updated, make sure we have authorization to change
2N/A ** that column.
2N/A */
2N/A chngRecno = 0;
2N/A for(i=0; i<pChanges->nExpr; i++){
2N/A if( sqliteExprResolveIds(pParse, pTabList, 0, pChanges->a[i].pExpr) ){
2N/A goto update_cleanup;
2N/A }
2N/A if( sqliteExprCheck(pParse, pChanges->a[i].pExpr, 0, 0) ){
2N/A goto update_cleanup;
2N/A }
2N/A for(j=0; j<pTab->nCol; j++){
2N/A if( sqliteStrICmp(pTab->aCol[j].zName, pChanges->a[i].zName)==0 ){
2N/A if( j==pTab->iPKey ){
2N/A chngRecno = 1;
2N/A pRecnoExpr = pChanges->a[i].pExpr;
2N/A }
2N/A aXRef[j] = i;
2N/A break;
2N/A }
2N/A }
2N/A if( j>=pTab->nCol ){
2N/A if( sqliteIsRowid(pChanges->a[i].zName) ){
2N/A chngRecno = 1;
2N/A pRecnoExpr = pChanges->a[i].pExpr;
2N/A }else{
2N/A sqliteErrorMsg(pParse, "no such column: %s", pChanges->a[i].zName);
2N/A goto update_cleanup;
2N/A }
2N/A }
2N/A#ifndef SQLITE_OMIT_AUTHORIZATION
2N/A {
2N/A int rc;
2N/A rc = sqliteAuthCheck(pParse, SQLITE_UPDATE, pTab->zName,
2N/A pTab->aCol[j].zName, db->aDb[pTab->iDb].zName);
2N/A if( rc==SQLITE_DENY ){
2N/A goto update_cleanup;
2N/A }else if( rc==SQLITE_IGNORE ){
2N/A aXRef[j] = -1;
2N/A }
2N/A }
2N/A#endif
2N/A }
2N/A
2N/A /* Allocate memory for the array apIdx[] and fill it with pointers to every
2N/A ** index that needs to be updated. Indices only need updating if their
2N/A ** key includes one of the columns named in pChanges or if the record
2N/A ** number of the original table entry is changing.
2N/A */
2N/A for(nIdx=nIdxTotal=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdxTotal++){
2N/A if( chngRecno ){
2N/A i = 0;
2N/A }else {
2N/A for(i=0; i<pIdx->nColumn; i++){
2N/A if( aXRef[pIdx->aiColumn[i]]>=0 ) break;
2N/A }
2N/A }
2N/A if( i<pIdx->nColumn ) nIdx++;
2N/A }
2N/A if( nIdxTotal>0 ){
2N/A apIdx = sqliteMalloc( sizeof(Index*) * nIdx + nIdxTotal );
2N/A if( apIdx==0 ) goto update_cleanup;
2N/A aIdxUsed = (char*)&apIdx[nIdx];
2N/A }
2N/A for(nIdx=j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
2N/A if( chngRecno ){
2N/A i = 0;
2N/A }else{
2N/A for(i=0; i<pIdx->nColumn; i++){
2N/A if( aXRef[pIdx->aiColumn[i]]>=0 ) break;
2N/A }
2N/A }
2N/A if( i<pIdx->nColumn ){
2N/A apIdx[nIdx++] = pIdx;
2N/A aIdxUsed[j] = 1;
2N/A }else{
2N/A aIdxUsed[j] = 0;
2N/A }
2N/A }
2N/A
2N/A /* Resolve the column names in all the expressions in the
2N/A ** WHERE clause.
2N/A */
2N/A if( pWhere ){
2N/A if( sqliteExprResolveIds(pParse, pTabList, 0, pWhere) ){
2N/A goto update_cleanup;
2N/A }
2N/A if( sqliteExprCheck(pParse, pWhere, 0, 0) ){
2N/A goto update_cleanup;
2N/A }
2N/A }
2N/A
2N/A /* Start the view context
2N/A */
2N/A if( isView ){
2N/A sqliteAuthContextPush(pParse, &sContext, pTab->zName);
2N/A }
2N/A
2N/A /* Begin generating code.
2N/A */
2N/A v = sqliteGetVdbe(pParse);
2N/A if( v==0 ) goto update_cleanup;
2N/A sqliteBeginWriteOperation(pParse, 1, pTab->iDb);
2N/A
2N/A /* If we are trying to update a view, construct that view into
2N/A ** a temporary table.
2N/A */
2N/A if( isView ){
2N/A Select *pView;
2N/A pView = sqliteSelectDup(pTab->pSelect);
2N/A sqliteSelect(pParse, pView, SRT_TempTable, iCur, 0, 0, 0);
2N/A sqliteSelectDelete(pView);
2N/A }
2N/A
2N/A /* Begin the database scan
2N/A */
2N/A pWInfo = sqliteWhereBegin(pParse, pTabList, pWhere, 1, 0);
2N/A if( pWInfo==0 ) goto update_cleanup;
2N/A
2N/A /* Remember the index of every item to be updated.
2N/A */
2N/A sqliteVdbeAddOp(v, OP_ListWrite, 0, 0);
2N/A
2N/A /* End the database scan loop.
2N/A */
2N/A sqliteWhereEnd(pWInfo);
2N/A
2N/A /* Initialize the count of updated rows
2N/A */
2N/A if( db->flags & SQLITE_CountRows && !pParse->trigStack ){
2N/A sqliteVdbeAddOp(v, OP_Integer, 0, 0);
2N/A }
2N/A
2N/A if( row_triggers_exist ){
2N/A /* Create pseudo-tables for NEW and OLD
2N/A */
2N/A sqliteVdbeAddOp(v, OP_OpenPseudo, oldIdx, 0);
2N/A sqliteVdbeAddOp(v, OP_OpenPseudo, newIdx, 0);
2N/A
2N/A /* The top of the update loop for when there are triggers.
2N/A */
2N/A sqliteVdbeAddOp(v, OP_ListRewind, 0, 0);
2N/A sqliteVdbeAddOp(v, OP_StackDepth, 0, 0);
2N/A sqliteVdbeAddOp(v, OP_MemStore, iStackDepth, 1);
2N/A loopStart = sqliteVdbeAddOp(v, OP_MemLoad, iStackDepth, 0);
2N/A sqliteVdbeAddOp(v, OP_StackReset, 0, 0);
2N/A jumpInst = sqliteVdbeAddOp(v, OP_ListRead, 0, 0);
2N/A sqliteVdbeAddOp(v, OP_Dup, 0, 0);
2N/A
2N/A /* Open a cursor and make it point to the record that is
2N/A ** being updated.
2N/A */
2N/A sqliteVdbeAddOp(v, OP_Dup, 0, 0);
2N/A if( !isView ){
2N/A sqliteVdbeAddOp(v, OP_Integer, pTab->iDb, 0);
2N/A sqliteVdbeAddOp(v, OP_OpenRead, iCur, pTab->tnum);
2N/A }
2N/A sqliteVdbeAddOp(v, OP_MoveTo, iCur, 0);
2N/A
2N/A /* Generate the OLD table
2N/A */
2N/A sqliteVdbeAddOp(v, OP_Recno, iCur, 0);
2N/A sqliteVdbeAddOp(v, OP_RowData, iCur, 0);
2N/A sqliteVdbeAddOp(v, OP_PutIntKey, oldIdx, 0);
2N/A
2N/A /* Generate the NEW table
2N/A */
2N/A if( chngRecno ){
2N/A sqliteExprCode(pParse, pRecnoExpr);
2N/A }else{
2N/A sqliteVdbeAddOp(v, OP_Recno, iCur, 0);
2N/A }
2N/A for(i=0; i<pTab->nCol; i++){
2N/A if( i==pTab->iPKey ){
2N/A sqliteVdbeAddOp(v, OP_String, 0, 0);
2N/A continue;
2N/A }
2N/A j = aXRef[i];
2N/A if( j<0 ){
2N/A sqliteVdbeAddOp(v, OP_Column, iCur, i);
2N/A }else{
2N/A sqliteExprCode(pParse, pChanges->a[j].pExpr);
2N/A }
2N/A }
2N/A sqliteVdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0);
2N/A sqliteVdbeAddOp(v, OP_PutIntKey, newIdx, 0);
2N/A if( !isView ){
2N/A sqliteVdbeAddOp(v, OP_Close, iCur, 0);
2N/A }
2N/A
2N/A /* Fire the BEFORE and INSTEAD OF triggers
2N/A */
2N/A if( sqliteCodeRowTrigger(pParse, TK_UPDATE, pChanges, TK_BEFORE, pTab,
2N/A newIdx, oldIdx, onError, loopStart) ){
2N/A goto update_cleanup;
2N/A }
2N/A }
2N/A
2N/A if( !isView ){
2N/A /*
2N/A ** Open every index that needs updating. Note that if any
2N/A ** index could potentially invoke a REPLACE conflict resolution
2N/A ** action, then we need to open all indices because we might need
2N/A ** to be deleting some records.
2N/A */
2N/A sqliteVdbeAddOp(v, OP_Integer, pTab->iDb, 0);
2N/A sqliteVdbeAddOp(v, OP_OpenWrite, iCur, pTab->tnum);
2N/A if( onError==OE_Replace ){
2N/A openAll = 1;
2N/A }else{
2N/A openAll = 0;
2N/A for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
2N/A if( pIdx->onError==OE_Replace ){
2N/A openAll = 1;
2N/A break;
2N/A }
2N/A }
2N/A }
2N/A for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
2N/A if( openAll || aIdxUsed[i] ){
2N/A sqliteVdbeAddOp(v, OP_Integer, pIdx->iDb, 0);
2N/A sqliteVdbeAddOp(v, OP_OpenWrite, iCur+i+1, pIdx->tnum);
2N/A assert( pParse->nTab>iCur+i+1 );
2N/A }
2N/A }
2N/A
2N/A /* Loop over every record that needs updating. We have to load
2N/A ** the old data for each record to be updated because some columns
2N/A ** might not change and we will need to copy the old value.
2N/A ** Also, the old data is needed to delete the old index entires.
2N/A ** So make the cursor point at the old record.
2N/A */
2N/A if( !row_triggers_exist ){
2N/A sqliteVdbeAddOp(v, OP_ListRewind, 0, 0);
2N/A jumpInst = loopStart = sqliteVdbeAddOp(v, OP_ListRead, 0, 0);
2N/A sqliteVdbeAddOp(v, OP_Dup, 0, 0);
2N/A }
2N/A sqliteVdbeAddOp(v, OP_NotExists, iCur, loopStart);
2N/A
2N/A /* If the record number will change, push the record number as it
2N/A ** will be after the update. (The old record number is currently
2N/A ** on top of the stack.)
2N/A */
2N/A if( chngRecno ){
2N/A sqliteExprCode(pParse, pRecnoExpr);
2N/A sqliteVdbeAddOp(v, OP_MustBeInt, 0, 0);
2N/A }
2N/A
2N/A /* Compute new data for this record.
2N/A */
2N/A for(i=0; i<pTab->nCol; i++){
2N/A if( i==pTab->iPKey ){
2N/A sqliteVdbeAddOp(v, OP_String, 0, 0);
2N/A continue;
2N/A }
2N/A j = aXRef[i];
2N/A if( j<0 ){
2N/A sqliteVdbeAddOp(v, OP_Column, iCur, i);
2N/A }else{
2N/A sqliteExprCode(pParse, pChanges->a[j].pExpr);
2N/A }
2N/A }
2N/A
2N/A /* Do constraint checks
2N/A */
2N/A sqliteGenerateConstraintChecks(pParse, pTab, iCur, aIdxUsed, chngRecno, 1,
2N/A onError, loopStart);
2N/A
2N/A /* Delete the old indices for the current record.
2N/A */
2N/A sqliteGenerateRowIndexDelete(db, v, pTab, iCur, aIdxUsed);
2N/A
2N/A /* If changing the record number, delete the old record.
2N/A */
2N/A if( chngRecno ){
2N/A sqliteVdbeAddOp(v, OP_Delete, iCur, 0);
2N/A }
2N/A
2N/A /* Create the new index entries and the new record.
2N/A */
2N/A sqliteCompleteInsertion(pParse, pTab, iCur, aIdxUsed, chngRecno, 1, -1);
2N/A }
2N/A
2N/A /* Increment the row counter
2N/A */
2N/A if( db->flags & SQLITE_CountRows && !pParse->trigStack){
2N/A sqliteVdbeAddOp(v, OP_AddImm, 1, 0);
2N/A }
2N/A
2N/A /* If there are triggers, close all the cursors after each iteration
2N/A ** through the loop. The fire the after triggers.
2N/A */
2N/A if( row_triggers_exist ){
2N/A if( !isView ){
2N/A for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
2N/A if( openAll || aIdxUsed[i] )
2N/A sqliteVdbeAddOp(v, OP_Close, iCur+i+1, 0);
2N/A }
2N/A sqliteVdbeAddOp(v, OP_Close, iCur, 0);
2N/A pParse->nTab = iCur;
2N/A }
2N/A if( sqliteCodeRowTrigger(pParse, TK_UPDATE, pChanges, TK_AFTER, pTab,
2N/A newIdx, oldIdx, onError, loopStart) ){
2N/A goto update_cleanup;
2N/A }
2N/A }
2N/A
2N/A /* Repeat the above with the next record to be updated, until
2N/A ** all record selected by the WHERE clause have been updated.
2N/A */
2N/A sqliteVdbeAddOp(v, OP_Goto, 0, loopStart);
2N/A sqliteVdbeChangeP2(v, jumpInst, sqliteVdbeCurrentAddr(v));
2N/A sqliteVdbeAddOp(v, OP_ListReset, 0, 0);
2N/A
2N/A /* Close all tables if there were no FOR EACH ROW triggers */
2N/A if( !row_triggers_exist ){
2N/A for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
2N/A if( openAll || aIdxUsed[i] ){
2N/A sqliteVdbeAddOp(v, OP_Close, iCur+i+1, 0);
2N/A }
2N/A }
2N/A sqliteVdbeAddOp(v, OP_Close, iCur, 0);
2N/A pParse->nTab = iCur;
2N/A }else{
2N/A sqliteVdbeAddOp(v, OP_Close, newIdx, 0);
2N/A sqliteVdbeAddOp(v, OP_Close, oldIdx, 0);
2N/A }
2N/A
2N/A sqliteVdbeAddOp(v, OP_SetCounts, 0, 0);
2N/A sqliteEndWriteOperation(pParse);
2N/A
2N/A /*
2N/A ** Return the number of rows that were changed.
2N/A */
2N/A if( db->flags & SQLITE_CountRows && !pParse->trigStack ){
2N/A sqliteVdbeOp3(v, OP_ColumnName, 0, 1, "rows updated", P3_STATIC);
2N/A sqliteVdbeAddOp(v, OP_Callback, 1, 0);
2N/A }
2N/A
2N/Aupdate_cleanup:
2N/A sqliteAuthContextPop(&sContext);
2N/A sqliteFree(apIdx);
2N/A sqliteFree(aXRef);
2N/A sqliteSrcListDelete(pTabList);
2N/A sqliteExprListDelete(pChanges);
2N/A sqliteExprDelete(pWhere);
2N/A return;
2N/A}