Search in sources :

Example 1 with SQLUpdateStatement

use of org.eclipse.persistence.internal.expressions.SQLUpdateStatement in project eclipselink by eclipse-ee4j.

the class HistoryPolicy method mappingLogicalDelete.

/**
 * INTERNAL:
 * Performs a logical delete (update) on the historical schema.  Direct
 * collections and many to many mappings are maintained through the session
 * events.
 */
public void mappingLogicalDelete(ModifyQuery originalQuery, AbstractRecord arguments, AbstractSession session) {
    SQLDeleteStatement originalStatement = (SQLDeleteStatement) originalQuery.getSQLStatement();
    DataModifyQuery historyQuery = new DataModifyQuery();
    SQLUpdateStatement historyStatement = new SQLUpdateStatement();
    DatabaseTable histTable = getHistoricalTables().get(0);
    historyStatement.setTable(histTable);
    Expression whereClause = (Expression) originalStatement.getWhereClause().clone();
    DatabaseField endField = getEnd();
    whereClause = whereClause.getBuilder().getField(endField).isNull().and(whereClause);
    historyStatement.setWhereClause(whereClause);
    AbstractRecord modifyRow = new DatabaseRecord();
    AbstractRecord translationRow = arguments.clone();
    Object time = getCurrentTime(session);
    modifyRow.add(getEnd(), time);
    translationRow.add(getEnd(), time);
    historyStatement.setModifyRow(modifyRow);
    historyQuery.setSQLStatement(historyStatement);
    historyQuery.setModifyRow(modifyRow);
    session.executeQuery(historyQuery, translationRow);
}
Also used : DatabaseRecord(org.eclipse.persistence.sessions.DatabaseRecord) SQLDeleteStatement(org.eclipse.persistence.internal.expressions.SQLDeleteStatement) 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) SQLUpdateStatement(org.eclipse.persistence.internal.expressions.SQLUpdateStatement) DatabaseField(org.eclipse.persistence.internal.helper.DatabaseField) AbstractRecord(org.eclipse.persistence.internal.sessions.AbstractRecord) DatabaseTable(org.eclipse.persistence.internal.helper.DatabaseTable) HistoricalDatabaseTable(org.eclipse.persistence.internal.history.HistoricalDatabaseTable) DataModifyQuery(org.eclipse.persistence.queries.DataModifyQuery)

Example 2 with SQLUpdateStatement

use of org.eclipse.persistence.internal.expressions.SQLUpdateStatement in project eclipselink by eclipse-ee4j.

the class OneToManyMapping method initializeChangeOrderTargetQuery.

/**
 * INTERNAL:
 * Initialize changeOrderTargetQuery.
 */
@Override
protected void initializeChangeOrderTargetQuery(AbstractSession session) {
    boolean hasChangeOrderTargetQuery = changeOrderTargetQuery != null;
    if (!hasChangeOrderTargetQuery) {
        changeOrderTargetQuery = new DataModifyQuery();
    }
    changeOrderTargetQuery = new DataModifyQuery();
    if (!changeOrderTargetQuery.hasSessionName()) {
        changeOrderTargetQuery.setSessionName(session.getName());
    }
    if (hasChangeOrderTargetQuery) {
        return;
    }
    DatabaseTable table = this.listOrderField.getTable();
    // Build where clause expression.
    Expression whereClause = null;
    Expression builder = new ExpressionBuilder();
    int size = targetPrimaryKeyFields.size();
    for (int index = 0; index < size; index++) {
        DatabaseField targetPrimaryKey = targetPrimaryKeyFields.get(index);
        Expression expression = builder.getField(targetPrimaryKey).equal(builder.getParameter(targetPrimaryKey));
        whereClause = expression.and(whereClause);
    }
    AbstractRecord modifyRow = new DatabaseRecord();
    modifyRow.add(this.listOrderField, null);
    SQLUpdateStatement statement = new SQLUpdateStatement();
    statement.setTable(table);
    statement.setWhereClause(whereClause);
    statement.setModifyRow(modifyRow);
    changeOrderTargetQuery.setSQLStatement(statement);
}
Also used : DatabaseRecord(org.eclipse.persistence.sessions.DatabaseRecord) FieldExpression(org.eclipse.persistence.internal.expressions.FieldExpression) ParameterExpression(org.eclipse.persistence.internal.expressions.ParameterExpression) Expression(org.eclipse.persistence.expressions.Expression) SQLUpdateStatement(org.eclipse.persistence.internal.expressions.SQLUpdateStatement) DatabaseField(org.eclipse.persistence.internal.helper.DatabaseField) AbstractRecord(org.eclipse.persistence.internal.sessions.AbstractRecord) DatabaseTable(org.eclipse.persistence.internal.helper.DatabaseTable) ExpressionBuilder(org.eclipse.persistence.expressions.ExpressionBuilder) DataModifyQuery(org.eclipse.persistence.queries.DataModifyQuery)

Example 3 with SQLUpdateStatement

use of org.eclipse.persistence.internal.expressions.SQLUpdateStatement in project eclipselink by eclipse-ee4j.

the class OneToManyMapping method initializeRemoveAllTargetsQuery.

/**
 * INTERNAL:
 * Initialize removeAllTargetsQuery.
 */
protected void initializeRemoveAllTargetsQuery(AbstractSession session) {
    if (!removeAllTargetsQuery.hasSessionName()) {
        removeAllTargetsQuery.setSessionName(session.getName());
    }
    if (hasCustomRemoveAllTargetsQuery) {
        return;
    }
    // All targetForeignKeys should have the same table
    DatabaseTable table = targetForeignKeyFields.get(0).getTable();
    // Build where clause expression.
    Expression whereClause = null;
    Expression builder = new ExpressionBuilder();
    AbstractRecord modifyRow = new DatabaseRecord();
    int size = targetForeignKeyFields.size();
    for (int index = 0; index < size; index++) {
        DatabaseField targetForeignKey = targetForeignKeyFields.get(index);
        if (shouldRemoveTargetQueryModifyTargetForeignKey()) {
            modifyRow.put(targetForeignKey, null);
        }
        Expression expression = builder.getField(targetForeignKey).equal(builder.getParameter(targetForeignKey));
        whereClause = expression.and(whereClause);
    }
    if (this.listOrderField != null) {
        // targetForeignKeys and listOrderField should have the same table
        modifyRow.add(this.listOrderField, null);
    }
    SQLUpdateStatement statement = new SQLUpdateStatement();
    statement.setTable(table);
    statement.setWhereClause(whereClause);
    statement.setModifyRow(modifyRow);
    removeAllTargetsQuery.setSQLStatement(statement);
}
Also used : DatabaseRecord(org.eclipse.persistence.sessions.DatabaseRecord) FieldExpression(org.eclipse.persistence.internal.expressions.FieldExpression) ParameterExpression(org.eclipse.persistence.internal.expressions.ParameterExpression) Expression(org.eclipse.persistence.expressions.Expression) SQLUpdateStatement(org.eclipse.persistence.internal.expressions.SQLUpdateStatement) DatabaseField(org.eclipse.persistence.internal.helper.DatabaseField) AbstractRecord(org.eclipse.persistence.internal.sessions.AbstractRecord) DatabaseTable(org.eclipse.persistence.internal.helper.DatabaseTable) ExpressionBuilder(org.eclipse.persistence.expressions.ExpressionBuilder)

Example 4 with SQLUpdateStatement

use of org.eclipse.persistence.internal.expressions.SQLUpdateStatement in project eclipselink by eclipse-ee4j.

the class OneToManyMapping method initializeAddTargetQuery.

/**
 * INTERNAL:
 * Initialize addTargetQuery.
 */
protected void initializeAddTargetQuery(AbstractSession session) {
    AbstractRecord modifyRow = createModifyRowForAddTargetQuery();
    if (modifyRow.isEmpty()) {
        return;
    }
    if (!hasCustomAddTargetQuery) {
        addTargetQuery = new DataModifyQuery();
    }
    if (!addTargetQuery.hasSessionName()) {
        addTargetQuery.setSessionName(session.getName());
    }
    if (hasCustomAddTargetQuery) {
        return;
    }
    // all fields in modifyRow must have the same table
    DatabaseTable table = (modifyRow.getFields().get(0)).getTable();
    // Build where clause expression.
    Expression whereClause = null;
    Expression builder = new ExpressionBuilder();
    int size = targetPrimaryKeyFields.size();
    for (int index = 0; index < size; index++) {
        DatabaseField targetPrimaryKey = targetPrimaryKeyFields.get(index);
        Expression expression = builder.getField(targetPrimaryKey).equal(builder.getParameter(targetPrimaryKey));
        whereClause = expression.and(whereClause);
    }
    SQLUpdateStatement statement = new SQLUpdateStatement();
    statement.setTable(table);
    statement.setWhereClause(whereClause);
    statement.setModifyRow(modifyRow);
    addTargetQuery.setSQLStatement(statement);
}
Also used : FieldExpression(org.eclipse.persistence.internal.expressions.FieldExpression) ParameterExpression(org.eclipse.persistence.internal.expressions.ParameterExpression) Expression(org.eclipse.persistence.expressions.Expression) SQLUpdateStatement(org.eclipse.persistence.internal.expressions.SQLUpdateStatement) DatabaseField(org.eclipse.persistence.internal.helper.DatabaseField) AbstractRecord(org.eclipse.persistence.internal.sessions.AbstractRecord) DatabaseTable(org.eclipse.persistence.internal.helper.DatabaseTable) ExpressionBuilder(org.eclipse.persistence.expressions.ExpressionBuilder) DataModifyQuery(org.eclipse.persistence.queries.DataModifyQuery)

Example 5 with SQLUpdateStatement

use of org.eclipse.persistence.internal.expressions.SQLUpdateStatement in project eclipselink by eclipse-ee4j.

the class OneToManyMapping method initializeRemoveTargetQuery.

/**
 * INTERNAL:
 * Initialize removeTargetQuery.
 */
protected void initializeRemoveTargetQuery(AbstractSession session) {
    if (!removeTargetQuery.hasSessionName()) {
        removeTargetQuery.setSessionName(session.getName());
    }
    if (hasCustomRemoveTargetQuery) {
        return;
    }
    // All targetForeignKeys should have the same table
    DatabaseTable table = targetForeignKeyFields.get(0).getTable();
    // Build where clause expression.
    Expression whereClause = null;
    Expression builder = new ExpressionBuilder();
    int size = targetPrimaryKeyFields.size();
    for (int index = 0; index < size; index++) {
        DatabaseField targetPrimaryKey = targetPrimaryKeyFields.get(index);
        Expression expression = builder.getField(targetPrimaryKey).equal(builder.getParameter(targetPrimaryKey));
        whereClause = expression.and(whereClause);
    }
    AbstractRecord modifyRow = new DatabaseRecord();
    if (shouldRemoveTargetQueryModifyTargetForeignKey()) {
        size = targetForeignKeyFields.size();
        for (int index = 0; index < size; index++) {
            DatabaseField targetForeignKey = targetForeignKeyFields.get(index);
            modifyRow.put(targetForeignKey, null);
            Expression expression = builder.getField(targetForeignKey).equal(builder.getParameter(targetForeignKey));
            whereClause = expression.and(whereClause);
        }
    }
    if (listOrderField != null) {
        modifyRow.add(listOrderField, null);
    }
    SQLUpdateStatement statement = new SQLUpdateStatement();
    statement.setTable(table);
    statement.setWhereClause(whereClause);
    statement.setModifyRow(modifyRow);
    removeTargetQuery.setSQLStatement(statement);
}
Also used : DatabaseRecord(org.eclipse.persistence.sessions.DatabaseRecord) FieldExpression(org.eclipse.persistence.internal.expressions.FieldExpression) ParameterExpression(org.eclipse.persistence.internal.expressions.ParameterExpression) Expression(org.eclipse.persistence.expressions.Expression) SQLUpdateStatement(org.eclipse.persistence.internal.expressions.SQLUpdateStatement) DatabaseField(org.eclipse.persistence.internal.helper.DatabaseField) AbstractRecord(org.eclipse.persistence.internal.sessions.AbstractRecord) DatabaseTable(org.eclipse.persistence.internal.helper.DatabaseTable) ExpressionBuilder(org.eclipse.persistence.expressions.ExpressionBuilder)

Aggregations

SQLUpdateStatement (org.eclipse.persistence.internal.expressions.SQLUpdateStatement)11 AbstractRecord (org.eclipse.persistence.internal.sessions.AbstractRecord)10 Expression (org.eclipse.persistence.expressions.Expression)9 ExpressionBuilder (org.eclipse.persistence.expressions.ExpressionBuilder)8 DatabaseField (org.eclipse.persistence.internal.helper.DatabaseField)8 DatabaseRecord (org.eclipse.persistence.sessions.DatabaseRecord)8 DatabaseTable (org.eclipse.persistence.internal.helper.DatabaseTable)7 DataModifyQuery (org.eclipse.persistence.queries.DataModifyQuery)5 FieldExpression (org.eclipse.persistence.internal.expressions.FieldExpression)4 ParameterExpression (org.eclipse.persistence.internal.expressions.ParameterExpression)4 ObjectExpression (org.eclipse.persistence.internal.expressions.ObjectExpression)3 TableExpression (org.eclipse.persistence.internal.expressions.TableExpression)3 ConstantExpression (org.eclipse.persistence.internal.expressions.ConstantExpression)2 HistoricalDatabaseTable (org.eclipse.persistence.internal.history.HistoricalDatabaseTable)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1 Vector (java.util.Vector)1 ClassDescriptor (org.eclipse.persistence.descriptors.ClassDescriptor)1