com.gs.fw.common.mithra.transaction.MultiUpdateOperation Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of reladomo Show documentation
Show all versions of reladomo Show documentation
Reladomo is an object-relational mapping framework.
/*
Copyright 2016 Goldman Sachs.
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 com.gs.fw.common.mithra.transaction;
import com.gs.collections.impl.list.mutable.FastList;
import com.gs.fw.common.mithra.*;
import com.gs.fw.common.mithra.attribute.*;
import com.gs.fw.common.mithra.attribute.update.AttributeUpdateWrapper;
import com.gs.fw.common.mithra.behavior.txparticipation.MithraOptimisticLockException;
import com.gs.fw.common.mithra.cache.FullUniqueIndex;
import com.gs.fw.common.mithra.databasetype.DatabaseType;
import com.gs.fw.common.mithra.finder.RelatedFinder;
import com.gs.fw.common.mithra.util.MithraFastList;
import org.slf4j.Logger;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.*;
public class MultiUpdateOperation extends TransactionOperation
{
private final List updates;
private final List mithraObjects;
private final boolean isDated;
private List singleValuedPrimaryKeys;
private final VersionAttribute versionAttribute;
private MithraDataObject[] dataObjects;
private int startIndex = 0;
private int endIndex = 0;
private String firstPartSql;
private FullUniqueIndex index;
private final Attribute diffPk;
public MultiUpdateOperation(UpdateOperation first, UpdateOperation second, Attribute diffPk)
{
super(first.getMithraObject(), first.getPortal());
this.diffPk = diffPk;
mithraObjects = new FastList();
addObject(first);
addObject(second);
this.updates = first.getUpdates();
this.isDated = this.getPortal().getFinder().getAsOfAttributes() != null;
if (this.getPortal().getTxParticipationMode().isOptimisticLocking())
{
this.versionAttribute = first.getPortal().getFinder().getVersionAttribute();
}
else
{
this.versionAttribute = null;
}
}
public MultiUpdateOperation(List updates, List mithraObjects)
{
this(updates, mithraObjects, findDiffPk(mithraObjects, ((AttributeUpdateWrapper) updates.get(0)).getAttribute().getOwnerPortal()));
}
private static Attribute findDiffPk(List mithraObjects, MithraObjectPortal ownerPortal)
{
return UpdateOperation.findDifferentPk(ownerPortal, mithraObjects.get(0), mithraObjects.get(1));
}
protected MultiUpdateOperation(List updates, List mithraObjects, Attribute diffPk)
{
super((MithraTransactionalObject) mithraObjects.get(0), ((AttributeUpdateWrapper) updates.get(0)).getAttribute().getOwnerPortal());
this.diffPk = diffPk;
isDated = this.getPortal().getFinder().getAsOfAttributes() != null;
this.updates = updates;
this.mithraObjects = mithraObjects;
if (this.getPortal().getTxParticipationMode().isOptimisticLocking())
{
this.versionAttribute = this.getPortal().getFinder().getVersionAttribute();
}
else
{
this.versionAttribute = null;
}
}
private void addObject(UpdateOperation updateOperation)
{
mithraObjects.add(updateOperation.getMithraObject());
if (index != null)
{
index.put(updateOperation.getMithraObject());
}
}
@Override
protected boolean isAsOfAttributeToOnlyUpdate()
{
for(int i=0;i getUpdates()
{
return updates;
}
@Override
public void execute() throws MithraDatabaseException
{
this.getPortal().getMithraObjectPersister().multiUpdate(this);
setUpdated();
}
public void setUpdated()
{
for(int i=0;i 0) buf.append(", ");
buf.append(upd.getSetAttributeSql());
}
buf.append(" where ");
if (this.versionAttribute != null)
{
buf.append((this.versionAttribute).getColumnName());
buf.append(" = ? ");
buf.append(" and ");
}
for(int i=0;i dataObjects.length)
{
batchSize = todo;
}
if (this.endIndex + batchSize == dataObjects.length - 1)
{
batchSize++;
}
String sql = this.firstPartSql + diffPk.getColumnName()+
" in (";
sql += createQuestionMarks(batchSize) + ")";
this.startIndex = this.endIndex;
this.endIndex += batchSize;
return sql;
}
protected String createQuestionMarks(int numberOfQuestions)
{
int questionLength = ((numberOfQuestions - 1) * 2) + 1;
StringBuilder bunchOfQuestionMarks = new StringBuilder(questionLength);
bunchOfQuestionMarks.append('?');
for (int k = 1; k < numberOfQuestions; k++)
{
bunchOfQuestionMarks.append(",?");
}
return bunchOfQuestionMarks.toString();
}
public void setSqlParameters(PreparedStatement stm, TimeZone databaseTimeZone, DatabaseType databaseType) throws SQLException
{
int pos = setUpdateParameters(stm, databaseTimeZone, databaseType);
pos = setSingleValuedKeyParameters(stm, databaseTimeZone, pos, dataObjects[startIndex], databaseType);
setInClauseParameters(stm, databaseTimeZone, pos, databaseType);
}
private void setInClauseParameters(PreparedStatement stm, TimeZone databaseTimeZone, int pos, DatabaseType databaseType) throws SQLException
{
SingleColumnAttribute attr = (SingleColumnAttribute) diffPk;
for(int i=startIndex; i < endIndex; i++)
{
attr.setSqlParameters(stm, dataObjects[i], pos, databaseTimeZone, databaseType);
pos++;
}
}
private int setSingleValuedKeyParameters(PreparedStatement stm, TimeZone databaseTimeZone,
int pos, MithraDataObject data, DatabaseType databaseType) throws SQLException
{
if (this.versionAttribute != null)
{
this.versionAttribute.setVersionAttributeSqlParameters(stm, data, pos, databaseTimeZone);
pos++;
}
for(int i=0;i