Search in sources :

Example 1 with StatementQueryMechanism

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);
    }
}
Also used : DeleteAllQuery(org.eclipse.persistence.queries.DeleteAllQuery) ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) DatabaseRecord(org.eclipse.persistence.sessions.DatabaseRecord) SQLUpdateStatement(org.eclipse.persistence.internal.expressions.SQLUpdateStatement) AbstractRecord(org.eclipse.persistence.internal.sessions.AbstractRecord) ExpressionBuilder(org.eclipse.persistence.expressions.ExpressionBuilder) StatementQueryMechanism(org.eclipse.persistence.internal.queries.StatementQueryMechanism) ObjectExpression(org.eclipse.persistence.internal.expressions.ObjectExpression) ConstantExpression(org.eclipse.persistence.internal.expressions.ConstantExpression) Expression(org.eclipse.persistence.expressions.Expression) TableExpression(org.eclipse.persistence.internal.expressions.TableExpression) DatabaseTable(org.eclipse.persistence.internal.helper.DatabaseTable) HistoricalDatabaseTable(org.eclipse.persistence.internal.history.HistoricalDatabaseTable)

Example 2 with StatementQueryMechanism

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();
    }
}
Also used : ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) StatementQueryMechanism(org.eclipse.persistence.internal.queries.StatementQueryMechanism) AbstractRecord(org.eclipse.persistence.internal.sessions.AbstractRecord) DatabaseTable(org.eclipse.persistence.internal.helper.DatabaseTable) HistoricalDatabaseTable(org.eclipse.persistence.internal.history.HistoricalDatabaseTable) SQLInsertStatement(org.eclipse.persistence.internal.expressions.SQLInsertStatement)

Aggregations

ClassDescriptor (org.eclipse.persistence.descriptors.ClassDescriptor)2 DatabaseTable (org.eclipse.persistence.internal.helper.DatabaseTable)2 HistoricalDatabaseTable (org.eclipse.persistence.internal.history.HistoricalDatabaseTable)2 StatementQueryMechanism (org.eclipse.persistence.internal.queries.StatementQueryMechanism)2 AbstractRecord (org.eclipse.persistence.internal.sessions.AbstractRecord)2 Expression (org.eclipse.persistence.expressions.Expression)1 ExpressionBuilder (org.eclipse.persistence.expressions.ExpressionBuilder)1 ConstantExpression (org.eclipse.persistence.internal.expressions.ConstantExpression)1 ObjectExpression (org.eclipse.persistence.internal.expressions.ObjectExpression)1 SQLInsertStatement (org.eclipse.persistence.internal.expressions.SQLInsertStatement)1 SQLUpdateStatement (org.eclipse.persistence.internal.expressions.SQLUpdateStatement)1 TableExpression (org.eclipse.persistence.internal.expressions.TableExpression)1 DeleteAllQuery (org.eclipse.persistence.queries.DeleteAllQuery)1 DatabaseRecord (org.eclipse.persistence.sessions.DatabaseRecord)1