📄 updateresultset.java
字号:
/* Derby - Class org.apache.derby.impl.sql.execute.UpdateResultSet Copyright 1997, 2004 The Apache Software Foundation or its licensors, as applicable. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */package org.apache.derby.impl.sql.execute;import org.apache.derby.iapi.services.loader.GeneratedMethod;import org.apache.derby.iapi.services.monitor.Monitor;import org.apache.derby.iapi.services.sanity.SanityManager;import org.apache.derby.iapi.services.stream.HeaderPrintWriter;import org.apache.derby.iapi.services.stream.InfoStreams;import org.apache.derby.iapi.services.io.StreamStorable;import org.apache.derby.iapi.error.StandardException;import org.apache.derby.iapi.sql.conn.LanguageConnectionContext;import org.apache.derby.iapi.sql.dictionary.ConglomerateDescriptor;import org.apache.derby.iapi.sql.dictionary.DataDictionary;import org.apache.derby.iapi.sql.dictionary.DataDictionaryContext;import org.apache.derby.iapi.sql.dictionary.TableDescriptor;import org.apache.derby.iapi.types.BooleanDataValue;import org.apache.derby.iapi.types.DataValueDescriptor;import org.apache.derby.iapi.types.RowLocation;import org.apache.derby.iapi.sql.execute.ConstantAction;import org.apache.derby.iapi.sql.execute.CursorResultSet;import org.apache.derby.iapi.sql.execute.ExecRow;import org.apache.derby.iapi.sql.execute.ExecutionContext;import org.apache.derby.iapi.sql.execute.RowChanger;import org.apache.derby.iapi.sql.execute.NoPutResultSet;import org.apache.derby.iapi.types.DataValueDescriptor;import org.apache.derby.iapi.sql.Activation;import org.apache.derby.iapi.sql.ResultDescription;import org.apache.derby.iapi.sql.ResultSet;import org.apache.derby.iapi.store.access.ConglomerateController;import org.apache.derby.iapi.store.access.DynamicCompiledOpenConglomInfo;import org.apache.derby.iapi.store.access.ScanController;import org.apache.derby.iapi.store.access.StaticCompiledOpenConglomInfo;import org.apache.derby.iapi.store.access.TransactionController;import org.apache.derby.iapi.reference.SQLState;import org.apache.derby.iapi.db.TriggerExecutionContext;import org.apache.derby.iapi.services.io.FormatableBitSet;import java.util.Properties;import java.util.Hashtable;/** * Update the rows from the specified * base table. This will cause constraints to be checked * and triggers to be executed based on the c's and t's * compiled into the update plan. * * @author ames */public class UpdateResultSet extends DMLWriteResultSet{ public TransactionController tc; public ExecRow newBaseRow; public ExecRow row; public ExecRow deferredSparseRow; public UpdateConstantAction constants; private ResultDescription resultDescription; private NoPutResultSet source; public NoPutResultSet savedSource; private RowChanger rowChanger; protected ConglomerateController deferredBaseCC; protected long[] deferredUniqueCIDs; protected boolean[] deferredUniqueCreated; protected ConglomerateController deferredUniqueCC[]; protected ScanController[] deferredUniqueScans; public LanguageConnectionContext lcc; private TemporaryRowHolderImpl deletedRowHolder; private TemporaryRowHolderImpl insertedRowHolder; // cached private RISetChecker riChecker; private TriggerInfo triggerInfo; private TriggerEventActivator triggerActivator; private boolean updatingReferencedKey; private boolean updatingForeignKey; private int numOpens; private long heapConglom; private FKInfo[] fkInfoArray; private FormatableBitSet baseRowReadList; private GeneratedMethod checkGM; private int resultWidth; private int numberOfBaseColumns; private ExecRow deferredTempRow; private ExecRow deferredBaseRow; private ExecRow oldDeletedRow; private ResultDescription triggerResultDescription; int lockMode; boolean deferred; boolean beforeUpdateCopyRequired = false; /** * Returns the description of the updated rows. * REVISIT: Do we want this to return NULL instead? */ public ResultDescription getResultDescription() { return resultDescription; } /* * class interface * */ /** * @param source update rows come from source * @param checkGM Generated method for enforcing check constraints * @param compiledConstants constantAction for the update * @exception StandardException thrown on error */ public UpdateResultSet(NoPutResultSet source, GeneratedMethod checkGM, Activation activation) throws StandardException { this(source, checkGM , activation, activation.getConstantAction(),null); } /* * class interface * */ /** * @param source update rows come from source * @param checkGM Generated method for enforcing check constraints * @param activation Activation * @param constantActionItem id of the update constant action saved objec * @param rsdItem id of the Result Description saved object * @exception StandardException thrown on error */ public UpdateResultSet(NoPutResultSet source, GeneratedMethod checkGM, Activation activation, int constantActionItem, int rsdItem) throws StandardException { this(source, checkGM , activation, ((ConstantAction)activation.getPreparedStatement().getSavedObject(constantActionItem)), (ResultDescription) activation.getPreparedStatement().getSavedObject(rsdItem)); // In case of referential action update, we do a deferred updates deferred = true; } /* * class interface * */ /** * @param source update rows come from source * @param checkGM Generated method for enforcing check constraints * @param compiledConstants constantAction for the update * @exception StandardException thrown on error */ public UpdateResultSet(NoPutResultSet source, GeneratedMethod checkGM, Activation activation, ConstantAction passedInConstantAction, ResultDescription passedInRsd) throws StandardException { super(activation, passedInConstantAction); // find the language context. lcc = activation.getLanguageConnectionContext(); // Get the current transaction controller tc = activation.getTransactionController(); this.source = source; this.checkGM = checkGM; constants = (UpdateConstantAction) constantAction; fkInfoArray = constants.getFKInfo( lcc.getExecutionContext() ); triggerInfo = constants.getTriggerInfo(lcc.getExecutionContext()); heapConglom = constants.conglomId; baseRowReadList = constants.getBaseRowReadList(); if(passedInRsd ==null) resultDescription = source.getResultDescription(); else resultDescription = passedInRsd; /* ** We NEED a result description when we are going to ** to have to kick off a trigger. In a replicated environment ** we don't get a result description when we are replaying ** source xacts on the target, which should never be the ** case for an UpdateResultSet. */ if (SanityManager.DEBUG) { if (resultDescription == null) { SanityManager.ASSERT(triggerInfo == null, "triggers need a result description to pass to result sets given to users"); } } if (fkInfoArray != null) { for (int i = 0; i < fkInfoArray.length; i++) { if (fkInfoArray[i].type == FKInfo.REFERENCED_KEY) { updatingReferencedKey = true; if (SanityManager.DEBUG) { SanityManager.ASSERT(constants.deferred, "updating referenced key but update not deferred, wuzzup?"); } } else { updatingForeignKey = true; } } } /* Get the # of columns in the ResultSet */ resultWidth = resultDescription.getColumnCount(); /* ** Calculate the # of columns in the base table. The result set ** contains the before columns, the after columns, and the RowLocation, ** so the number of base columns is half of the number of result set ** columns, after subtracting one for the row location column. */ numberOfBaseColumns = (resultWidth - 1) / 2; /* Get the new base row */ newBaseRow = RowUtil.getEmptyValueRow(numberOfBaseColumns, lcc); /* decode lock mode */ lockMode = decodeLockMode(lcc, constants.lockMode); deferred = constants.deferred; //update can be marked for deferred mode because the scan is being done //using index. But it is not necesary to keep the before copy //of the row in the temporary row holder (deletedRowHolder) unless //there are RI constraint or Triggers.(beetle:5301) if(triggerInfo != null || fkInfoArray !=null){ beforeUpdateCopyRequired = true; } } /** @exception StandardException Standard Cloudscape error policy */ public void open() throws StandardException { setup(); collectAffectedRows(); /* ** If this is a deferred update, read the new rows and RowLocations ** from the temporary conglomerate and update the base table using ** the RowChanger. */ if (deferred) { runChecker(true); //check for only RESTRICT referential action rule violations fireBeforeTriggers(); updateDeferredRows(); /* Apply deferred inserts to unique indexes */ rowChanger.finish(); runChecker(false); //check for all violations fireAfterTriggers(); } else{ /* Apply deferred inserts to unique indexes */ rowChanger.finish(); } cleanUp(); } /** @exception StandardException Standard Cloudscape error policy */ void setup() throws StandardException { boolean firstOpen = (rowChanger == null); rowCount = 0; /* Cache query plan text for source, before it gets blown away */ if (lcc.getRunTimeStatisticsMode()) { /* savedSource nulled after run time statistics generation */ savedSource = source; } /* Get or re-use the row changer. * NOTE: We need to set ourself as the top result set * if this is not the 1st execution. (Done in constructor * for 1st execution.) */ if (firstOpen) { rowChanger = lcc.getLanguageConnectionFactory().getExecutionFactory() .getRowChanger( heapConglom, constants.heapSCOCI, heapDCOCI, constants.irgs, constants.indexCIDS, constants.indexSCOCIs, indexDCOCIs, constants.numColumns, tc, constants.changedColumnIds, constants.getBaseRowReadList(), constants.getBaseRowReadMap(), constants.getStreamStorableHeapColIds(), activation); rowChanger.setIndexNames(constants.indexNames); } else { lcc.getStatementContext().setTopResultSet(this, subqueryTrackingArray); } /* Open the RowChanger before the source ResultSet so that * the store will see the RowChanger's lock as a covering lock * if it is a table lock. */ rowChanger.open(lockMode); if (numOpens++ == 0) { source.openCore(); } else { source.reopenCore(); } /* The source does not know whether or not we are doing a * deferred mode update. If we are, then we must clear the * index scan info from the activation so that the row changer * does not re-use that information (which won't be valid for * a deferred mode update). */ if (deferred) { activation.clearIndexScanInfo(); } if (fkInfoArray != null) { if (riChecker == null) { riChecker = new RISetChecker(tc, fkInfoArray); } else { riChecker.reopen(); } } if (deferred) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -