use of org.eclipse.persistence.internal.queries.StatementQueryMechanism in project eclipselink by eclipse-ee4j.
the class HistoryPolicy method logicalDelete.
/**
* INTERNAL:
* Performs a logical delete (update) on the historical schema.
*/
public void logicalDelete(ModifyQuery writeQuery, boolean isUpdate, boolean isShallow) {
ClassDescriptor descriptor = writeQuery.getDescriptor();
AbstractRecord originalModifyRow = writeQuery.getModifyRow();
AbstractRecord modifyRow = new DatabaseRecord();
StatementQueryMechanism updateMechanism = new StatementQueryMechanism(writeQuery);
Object currentTime = getCurrentTime(writeQuery.getSession());
for (int i = 0; i < getHistoricalTables().size(); i++) {
DatabaseTable table = getHistoricalTables().get(i);
if (isUpdate && !checkWastedVersioning(originalModifyRow, table)) {
continue;
}
SQLUpdateStatement updateStatement = new SQLUpdateStatement();
updateStatement.setTable(table);
Expression whereClause = null;
if (writeQuery instanceof DeleteAllQuery) {
if (writeQuery.getSelectionCriteria() != null) {
whereClause = (Expression) writeQuery.getSelectionCriteria().clone();
}
} else {
whereClause = descriptor.getObjectBuilder().buildPrimaryKeyExpression(table);
}
ExpressionBuilder builder = ((whereClause == null) ? new ExpressionBuilder() : whereClause.getBuilder());
whereClause = builder.getField(getEnd(i)).isNull().and(whereClause);
updateStatement.setWhereClause(whereClause);
modifyRow.add(getEnd(i), currentTime);
// the start field in the logicalInsert.
if (isUpdate) {
if (isShallow) {
// Bug 319276 - increment the timestamp by 1 to avoid unique constraint violation potential
java.sql.Timestamp incrementedTime = (java.sql.Timestamp) currentTime;
incrementedTime.setTime(incrementedTime.getTime() + getMinimumTimeIncrement(writeQuery.getSession()));
originalModifyRow.add(getStart(i), incrementedTime);
} else {
originalModifyRow.add(getStart(i), currentTime);
}
}
updateMechanism.getSQLStatements().add(updateStatement);
}
if (updateMechanism.hasMultipleStatements()) {
writeQuery.setModifyRow(modifyRow);
updateMechanism.updateObject();
writeQuery.setModifyRow(originalModifyRow);
}
}
use of org.eclipse.persistence.internal.queries.StatementQueryMechanism in project eclipselink by eclipse-ee4j.
the class HistoryPolicy method logicalInsert.
/**
* INTERNAL:
* Perform a logical insert into the historical schema, creating a new version
* of an object.
* <p>Called by postInsert() and also postUpdate() (which first does a logicalDelete
* of the previous version).
*/
public void logicalInsert(ObjectLevelModifyQuery writeQuery, boolean isUpdate) {
ClassDescriptor descriptor = getDescriptor();
AbstractRecord modifyRow = null;
AbstractRecord originalModifyRow = writeQuery.getModifyRow();
Object currentTime = null;
if (isUpdate) {
// Bug 319276
modifyRow = descriptor.getObjectBuilder().buildRow(writeQuery.getObject(), writeQuery.getSession(), WriteType.UPDATE);
// If anyone added items to the modify row, then they should also be added here.
modifyRow.putAll(originalModifyRow);
} else {
modifyRow = originalModifyRow;
// If update would have already discovered timestamp to use.
currentTime = getCurrentTime(writeQuery.getSession());
}
StatementQueryMechanism insertMechanism = new StatementQueryMechanism(writeQuery);
for (int i = 0; i < getHistoricalTables().size(); i++) {
DatabaseTable table = getHistoricalTables().get(i);
if (isUpdate && !checkWastedVersioning(originalModifyRow, table)) {
continue;
}
if (!isUpdate) {
modifyRow.add(getStart(i), currentTime);
}
SQLInsertStatement insertStatement = new SQLInsertStatement();
insertStatement.setTable(table);
insertMechanism.getSQLStatements().add(insertStatement);
}
if (insertMechanism.hasMultipleStatements()) {
writeQuery.setTranslationRow(modifyRow);
writeQuery.setModifyRow(modifyRow);
insertMechanism.insertObject();
}
}
Aggregations