Search in sources :

Example 26 with SQLSelectStatement

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

the class ExpressionQueryMechanism method prepareCursorSelectAllRows.

/**
 * Pre-build the SQL statement from the expression.
 */
@Override
public void prepareCursorSelectAllRows() {
    if (getQuery().isReportQuery()) {
        SQLSelectStatement statement = buildReportQuerySelectStatement(false);
        setSQLStatement(statement);
    // For bug 2718118 inheritance with cursors is supported provided there is a read all subclasses view.
    } else if (getDescriptor().hasInheritance() && getDescriptor().getInheritancePolicy().requiresMultipleTableSubclassRead() && getDescriptor().getInheritancePolicy().hasView()) {
        InheritancePolicy inheritancePolicy = getDescriptor().getInheritancePolicy();
        SQLSelectStatement statement = inheritancePolicy.buildViewSelectStatement((ObjectLevelReadQuery) getQuery());
        setSQLStatement(statement);
    } else {
        setSQLStatement(buildNormalSelectStatement());
    }
    super.prepareCursorSelectAllRows();
}
Also used : ObjectLevelReadQuery(org.eclipse.persistence.queries.ObjectLevelReadQuery) InheritancePolicy(org.eclipse.persistence.descriptors.InheritancePolicy) SQLSelectStatement(org.eclipse.persistence.internal.expressions.SQLSelectStatement)

Example 27 with SQLSelectStatement

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

the class ExpressionQueryMechanism method buildStatementsForUpdateAllForTempTables.

protected Vector buildStatementsForUpdateAllForTempTables(DatabaseTable table, HashMap databaseFieldsToValues, List<DatabaseField> primaryKeyFields) {
    Vector statements = org.eclipse.persistence.internal.helper.NonSynchronizedVector.newInstance(4);
    Vector allFields = org.eclipse.persistence.internal.helper.NonSynchronizedVector.newInstance();
    Iterator<DatabaseField> it = getDescriptor().getFields().iterator();
    while (it.hasNext()) {
        DatabaseField field = it.next();
        if (table.equals(field.getTable())) {
            allFields.add(field);
        }
    }
    Collection assignedFields = databaseFieldsToValues.keySet();
    HashMap databaseFieldsToValuesForInsert = databaseFieldsToValues;
    Collection assignedFieldsForInsert = assignedFields;
    // can do that because all fields initialized to null when temp. table created.
    if (!getExecutionSession().getPlatform().isNullAllowedInSelectClause()) {
        databaseFieldsToValuesForInsert = new HashMap(databaseFieldsToValues.size());
        Iterator itEntries = databaseFieldsToValues.entrySet().iterator();
        while (itEntries.hasNext()) {
            Map.Entry entry = (Map.Entry) itEntries.next();
            if (entry.getValue() instanceof ConstantExpression) {
                ConstantExpression constExp = (ConstantExpression) entry.getValue();
                if (constExp.getValue() == null) {
                    continue;
                }
            }
            databaseFieldsToValuesForInsert.put(entry.getKey(), entry.getValue());
        }
        assignedFieldsForInsert = databaseFieldsToValuesForInsert.keySet();
    }
    SQLUpdateAllStatementForTempTable createTempTableStatement = new SQLUpdateAllStatementForTempTable();
    createTempTableStatement.setMode(SQLModifyAllStatementForTempTable.CREATE_TEMP_TABLE);
    createTempTableStatement.setTable(table);
    createTempTableStatement.setAllFields(allFields);
    createTempTableStatement.setAssignedFields(assignedFields);
    createTempTableStatement.setPrimaryKeyFields(primaryKeyFields);
    statements.addElement(createTempTableStatement);
    SQLSelectStatement selectStatement = createSQLSelectStatementForModifyAllForTempTable(databaseFieldsToValuesForInsert);
    SQLCall selectCall = (SQLCall) selectStatement.buildCall(getSession(), getQuery());
    SQLUpdateAllStatementForTempTable insertStatement = new SQLUpdateAllStatementForTempTable();
    insertStatement.setMode(SQLModifyAllStatementForTempTable.INSERT_INTO_TEMP_TABLE);
    insertStatement.setTable(table);
    insertStatement.setTranslationRow(getTranslationRow());
    insertStatement.setSelectCall(selectCall);
    insertStatement.setAssignedFields(assignedFieldsForInsert);
    insertStatement.setPrimaryKeyFields(primaryKeyFields);
    statements.addElement(insertStatement);
    SQLUpdateAllStatementForTempTable updateStatement = new SQLUpdateAllStatementForTempTable();
    updateStatement.setMode(SQLModifyAllStatementForTempTable.UPDATE_ORIGINAL_TABLE);
    updateStatement.setTable(table);
    updateStatement.setTranslationRow(getTranslationRow());
    updateStatement.setAssignedFields(assignedFields);
    updateStatement.setPrimaryKeyFields(primaryKeyFields);
    statements.addElement(updateStatement);
    SQLUpdateAllStatementForTempTable cleanupStatement = new SQLUpdateAllStatementForTempTable();
    cleanupStatement.setMode(SQLModifyAllStatementForTempTable.CLEANUP_TEMP_TABLE);
    cleanupStatement.setTable(table);
    statements.addElement(cleanupStatement);
    return statements;
}
Also used : SQLCall(org.eclipse.persistence.queries.SQLCall) IdentityHashMap(java.util.IdentityHashMap) HashMap(java.util.HashMap) ConstantExpression(org.eclipse.persistence.internal.expressions.ConstantExpression) SQLUpdateAllStatementForTempTable(org.eclipse.persistence.internal.expressions.SQLUpdateAllStatementForTempTable) DatabaseField(org.eclipse.persistence.internal.helper.DatabaseField) Iterator(java.util.Iterator) ExpressionIterator(org.eclipse.persistence.internal.expressions.ExpressionIterator) Collection(java.util.Collection) SQLSelectStatement(org.eclipse.persistence.internal.expressions.SQLSelectStatement) Vector(java.util.Vector) NonSynchronizedVector(org.eclipse.persistence.internal.helper.NonSynchronizedVector) Map(java.util.Map) IdentityHashMap(java.util.IdentityHashMap) HashMap(java.util.HashMap)

Example 28 with SQLSelectStatement

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

the class ExpressionQueryMechanism method createSQLSelectStatementForModifyAllForTempTable.

protected SQLSelectStatement createSQLSelectStatementForModifyAllForTempTable(HashMap databaseFieldsToValues) {
    ExpressionBuilder builder = ((ModifyAllQuery) getQuery()).getExpressionBuilder();
    Expression whereClause = getSelectionCriteria();
    ReportQuery reportQuery = new ReportQuery(getDescriptor().getJavaClass(), builder);
    reportQuery.setDescriptor(getDescriptor());
    reportQuery.setSelectionCriteria(whereClause);
    reportQuery.setSession(getSession());
    reportQuery.setShouldRetrievePrimaryKeys(true);
    if (databaseFieldsToValues != null) {
        Iterator itValues = databaseFieldsToValues.values().iterator();
        while (itValues.hasNext()) {
            reportQuery.addAttribute("", (Expression) itValues.next());
        }
    }
    SQLSelectStatement selectStatement = ((ExpressionQueryMechanism) reportQuery.getQueryMechanism()).buildReportQuerySelectStatement(false);
    reportQuery.setSession(null);
    return selectStatement;
}
Also used : FieldExpression(org.eclipse.persistence.internal.expressions.FieldExpression) ConstantExpression(org.eclipse.persistence.internal.expressions.ConstantExpression) QueryKeyExpression(org.eclipse.persistence.internal.expressions.QueryKeyExpression) ParameterExpression(org.eclipse.persistence.internal.expressions.ParameterExpression) ObjectExpression(org.eclipse.persistence.internal.expressions.ObjectExpression) DataExpression(org.eclipse.persistence.internal.expressions.DataExpression) Expression(org.eclipse.persistence.expressions.Expression) ModifyAllQuery(org.eclipse.persistence.queries.ModifyAllQuery) ReportQuery(org.eclipse.persistence.queries.ReportQuery) Iterator(java.util.Iterator) ExpressionIterator(org.eclipse.persistence.internal.expressions.ExpressionIterator) SQLSelectStatement(org.eclipse.persistence.internal.expressions.SQLSelectStatement) ExpressionBuilder(org.eclipse.persistence.expressions.ExpressionBuilder)

Example 29 with SQLSelectStatement

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

the class ContainerPolicy method buildSelectionQueryForDirectCollectionMapping.

/**
 * INTERNAL:
 * This
 * Certain key mappings favor different types of selection query.  Return the appropriate
 * type of selectionQuery
 */
public ReadQuery buildSelectionQueryForDirectCollectionMapping() {
    DirectReadQuery query = new DirectReadQuery();
    query.setSQLStatement(new SQLSelectStatement());
    query.setContainerPolicy(this);
    return query;
}
Also used : SQLSelectStatement(org.eclipse.persistence.internal.expressions.SQLSelectStatement) DirectReadQuery(org.eclipse.persistence.queries.DirectReadQuery)

Example 30 with SQLSelectStatement

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

the class ExpressionQueryMechanism method buildStatementsForDeleteAllForTempTables.

/**
 * Build SQLStatements for delete all using temporary table.
 * @return {@code Vector<SQLStatement>}
 */
protected Vector buildStatementsForDeleteAllForTempTables() {
    Vector statements = org.eclipse.persistence.internal.helper.NonSynchronizedVector.newInstance();
    // retrieve rootTable and its primary key fields for composing temporary table
    DatabaseTable rootTable = getDescriptor().getMultipleTableInsertOrder().get(0);
    List<DatabaseField> rootTablePrimaryKeyFields = getPrimaryKeyFieldsForTable(rootTable);
    ClassDescriptor rootDescriptor = getDescriptor();
    if (getDescriptor().hasInheritance()) {
        rootDescriptor = rootDescriptor.getInheritancePolicy().getRootParentDescriptor();
    }
    Vector allFields = org.eclipse.persistence.internal.helper.NonSynchronizedVector.newInstance();
    Iterator<DatabaseField> it = rootDescriptor.getFields().iterator();
    while (it.hasNext()) {
        DatabaseField field = it.next();
        if (rootTable.equals(field.getTable())) {
            allFields.add(field);
        }
    }
    // statements will be executed in reverse order
    // statement for temporary table cleanup (Drop table or Delete from temp_table)
    SQLDeleteAllStatementForTempTable cleanupStatement = new SQLDeleteAllStatementForTempTable();
    cleanupStatement.setMode(SQLModifyAllStatementForTempTable.CLEANUP_TEMP_TABLE);
    cleanupStatement.setTable(rootTable);
    statements.addElement(cleanupStatement);
    // delete statements using temporary table
    Vector deleteStatements = buildDeleteAllStatementsForTempTable(getDescriptor(), rootTable, rootTablePrimaryKeyFields, null);
    statements.addAll(deleteStatements);
    // Insert statement populating temporary table with criteria
    SQLSelectStatement selectStatement = createSQLSelectStatementForModifyAllForTempTable(null);
    SQLCall selectCall = (SQLCall) selectStatement.buildCall(getSession());
    SQLDeleteAllStatementForTempTable insertStatement = new SQLDeleteAllStatementForTempTable();
    insertStatement.setMode(SQLModifyAllStatementForTempTable.INSERT_INTO_TEMP_TABLE);
    insertStatement.setTable(rootTable);
    insertStatement.setTranslationRow(getTranslationRow());
    insertStatement.setSelectCall(selectCall);
    insertStatement.setPrimaryKeyFields(rootTablePrimaryKeyFields);
    statements.addElement(insertStatement);
    // Create temporary table statement
    SQLDeleteAllStatementForTempTable createTempTableStatement = new SQLDeleteAllStatementForTempTable();
    createTempTableStatement.setMode(SQLModifyAllStatementForTempTable.CREATE_TEMP_TABLE);
    createTempTableStatement.setTable(rootTable);
    createTempTableStatement.setAllFields(allFields);
    createTempTableStatement.setPrimaryKeyFields(rootTablePrimaryKeyFields);
    statements.addElement(createTempTableStatement);
    return statements;
}
Also used : SQLCall(org.eclipse.persistence.queries.SQLCall) ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) DatabaseField(org.eclipse.persistence.internal.helper.DatabaseField) DatabaseTable(org.eclipse.persistence.internal.helper.DatabaseTable) SQLSelectStatement(org.eclipse.persistence.internal.expressions.SQLSelectStatement) Vector(java.util.Vector) NonSynchronizedVector(org.eclipse.persistence.internal.helper.NonSynchronizedVector) SQLDeleteAllStatementForTempTable(org.eclipse.persistence.internal.expressions.SQLDeleteAllStatementForTempTable)

Aggregations

SQLSelectStatement (org.eclipse.persistence.internal.expressions.SQLSelectStatement)35 Expression (org.eclipse.persistence.expressions.Expression)13 IdentityHashMap (java.util.IdentityHashMap)10 DatabaseField (org.eclipse.persistence.internal.helper.DatabaseField)10 HashMap (java.util.HashMap)9 Map (java.util.Map)8 ConstantExpression (org.eclipse.persistence.internal.expressions.ConstantExpression)8 FieldExpression (org.eclipse.persistence.internal.expressions.FieldExpression)8 ObjectExpression (org.eclipse.persistence.internal.expressions.ObjectExpression)8 ExpressionBuilder (org.eclipse.persistence.expressions.ExpressionBuilder)7 ParameterExpression (org.eclipse.persistence.internal.expressions.ParameterExpression)7 QueryKeyExpression (org.eclipse.persistence.internal.expressions.QueryKeyExpression)7 DataExpression (org.eclipse.persistence.internal.expressions.DataExpression)6 ObjectLevelReadQuery (org.eclipse.persistence.queries.ObjectLevelReadQuery)6 SQLCall (org.eclipse.persistence.queries.SQLCall)6 Iterator (java.util.Iterator)5 ExpressionIterator (org.eclipse.persistence.internal.expressions.ExpressionIterator)5 DirectReadQuery (org.eclipse.persistence.queries.DirectReadQuery)5 ReportQuery (org.eclipse.persistence.queries.ReportQuery)5 ArrayList (java.util.ArrayList)4