Search in sources :

Example 11 with SQLSelectStatement

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

the class ExpressionQueryMechanism method buildBaseSelectStatement.

/**
 * Return the appropriate select statement containing the fields in the table.
 * Ability to switch off AdditionalJoinExpression is required for DeleteAllQuery.
 */
public SQLSelectStatement buildBaseSelectStatement(boolean isSubSelect, Map clonedExpressions, boolean shouldUseAdditionalJoinExpression) {
    SQLSelectStatement selectStatement = new SQLSelectStatement();
    ObjectLevelReadQuery query = (ObjectLevelReadQuery) getQuery();
    selectStatement.setQuery(query);
    selectStatement.setLockingClause(query.getLockingClause());
    selectStatement.setDistinctState(query.getDistinctState());
    selectStatement.setTables((Vector) getDescriptor().getTables().clone());
    selectStatement.setWhereClause(buildBaseSelectionCriteria(isSubSelect, clonedExpressions, shouldUseAdditionalJoinExpression));
    // instead of the 'left' we will build the SQL using the wrong builder.
    if (query.hasDefaultBuilder() && !query.getExpressionBuilder().wasQueryClassSetInternally()) {
        selectStatement.setBuilder((ExpressionBuilder) query.getExpressionBuilder().copiedVersionFrom(clonedExpressions));
    }
    // of table alias always be correct
    if (query.hasOrderByExpressions()) {
        selectStatement.setOrderByExpressions(cloneExpressions(query.getOrderByExpressions(), clonedExpressions));
    }
    if (query.hasNonFetchJoinedAttributeExpressions()) {
        selectStatement.setNonSelectFields(cloneExpressions(query.getNonFetchJoinAttributeExpressions(), clonedExpressions));
    }
    if (query.hasUnionExpressions()) {
        selectStatement.setUnionExpressions(cloneExpressions(query.getUnionExpressions(), clonedExpressions));
    }
    if (getQuery().isReadAllQuery() && ((ReadAllQuery) getQuery()).hasHierarchicalExpressions()) {
        ReadAllQuery readAllquery = (ReadAllQuery) query;
        Expression startsWith = readAllquery.getStartWithExpression();
        if (startsWith != null) {
            startsWith.copiedVersionFrom(clonedExpressions);
        }
        selectStatement.setHierarchicalQueryExpressions(startsWith, readAllquery.getConnectByExpression().copiedVersionFrom(clonedExpressions), cloneExpressions(readAllquery.getOrderSiblingsByExpressions(), clonedExpressions), readAllquery.getDirection());
    }
    selectStatement.setHintString(query.getHintString());
    selectStatement.setTranslationRow(getTranslationRow());
    return selectStatement;
}
Also used : ObjectLevelReadQuery(org.eclipse.persistence.queries.ObjectLevelReadQuery) 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) ReadAllQuery(org.eclipse.persistence.queries.ReadAllQuery) SQLSelectStatement(org.eclipse.persistence.internal.expressions.SQLSelectStatement)

Example 12 with SQLSelectStatement

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

the class ExpressionQueryMechanism method buildUpdateAllStatement.

protected SQLUpdateAllStatement buildUpdateAllStatement(DatabaseTable table, HashMap databaseFieldsToValues, SQLCall selectCallForExist, SQLSelectStatement selectStatementForExist, Collection primaryKeyFields) {
    SQLUpdateAllStatement updateAllStatement = new SQLUpdateAllStatement();
    updateAllStatement.setTable(table);
    updateAllStatement.setTranslationRow(getTranslationRow());
    HashMap databaseFieldsToValuesCopy = new HashMap(databaseFieldsToValues.size());
    HashMap databaseFieldsToTableAliases = null;
    Iterator it = databaseFieldsToValues.entrySet().iterator();
    while (it.hasNext()) {
        Map.Entry entry = (Map.Entry) it.next();
        // for each table to be updated
        DatabaseField field = (DatabaseField) entry.getKey();
        // here's a Map of left hand fields to right hand expressions
        Object value = entry.getValue();
        if (value instanceof SQLSelectStatement) {
            SQLSelectStatement selStatement = (SQLSelectStatement) value;
            SQLCall selCall = (SQLCall) selStatement.buildCall(getSession());
            databaseFieldsToValuesCopy.put(field, selCall);
            if (databaseFieldsToTableAliases == null) {
                databaseFieldsToTableAliases = new HashMap();
                updateAllStatement.setPrimaryKeyFieldsForAutoJoin(primaryKeyFields);
            }
            databaseFieldsToTableAliases.put(field, getAliasTableName(selStatement, table, getExecutionSession().getPlatform()));
        } else {
            // should be Expression
            databaseFieldsToValuesCopy.put(field, value);
        }
    }
    updateAllStatement.setUpdateClauses(databaseFieldsToValuesCopy);
    updateAllStatement.setDatabaseFieldsToTableAliases(databaseFieldsToTableAliases);
    updateAllStatement.setSelectCallForExist(selectCallForExist);
    updateAllStatement.setShouldExtractWhereClauseFromSelectCallForExist(!selectStatementForExist.requiresAliases() && table.equals(selectStatementForExist.getTables().get(0)));
    updateAllStatement.setTableAliasInSelectCallForExist(getAliasTableName(selectStatementForExist, table, getExecutionSession().getPlatform()));
    updateAllStatement.setPrimaryKeyFieldsForAutoJoin(primaryKeyFields);
    return updateAllStatement;
}
Also used : SQLCall(org.eclipse.persistence.queries.SQLCall) IdentityHashMap(java.util.IdentityHashMap) HashMap(java.util.HashMap) DatabaseField(org.eclipse.persistence.internal.helper.DatabaseField) Iterator(java.util.Iterator) ExpressionIterator(org.eclipse.persistence.internal.expressions.ExpressionIterator) SQLUpdateAllStatement(org.eclipse.persistence.internal.expressions.SQLUpdateAllStatement) SQLSelectStatement(org.eclipse.persistence.internal.expressions.SQLSelectStatement) InvalidObject(org.eclipse.persistence.internal.helper.InvalidObject) Map(java.util.Map) IdentityHashMap(java.util.IdentityHashMap) HashMap(java.util.HashMap)

Example 13 with SQLSelectStatement

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

the class ExpressionQueryMechanism method createSQLSelectStatementForModifyAll.

/**
 * Customary inheritance expression is required for DeleteAllQuery and UpdateAllQuery preparation.
 * Ability to switch off AdditionalJoinExpression is required for DeleteAllQuery.
 */
protected SQLSelectStatement createSQLSelectStatementForModifyAll(Expression whereClause, Expression inheritanceExpression, ClassDescriptor desc, boolean useCustomaryInheritanceExpression, boolean shouldUseAdditionalJoinExpression) {
    ExpressionBuilder builder;
    if (whereClause != null) {
        whereClause = (Expression) whereClause.clone();
        builder = whereClause.getBuilder();
    } else {
        builder = new ExpressionBuilder();
    }
    ReportQuery reportQuery = new ReportQuery(desc.getJavaClass(), builder);
    reportQuery.setDescriptor(desc);
    reportQuery.setShouldRetrieveFirstPrimaryKey(true);
    reportQuery.setSelectionCriteria(whereClause);
    reportQuery.setSession(getSession());
    SQLSelectStatement selectStatement = ((ExpressionQueryMechanism) reportQuery.getQueryMechanism()).buildReportQuerySelectStatement(false, useCustomaryInheritanceExpression, inheritanceExpression, shouldUseAdditionalJoinExpression);
    reportQuery.setSession(null);
    return selectStatement;
}
Also used : ReportQuery(org.eclipse.persistence.queries.ReportQuery) SQLSelectStatement(org.eclipse.persistence.internal.expressions.SQLSelectStatement) ExpressionBuilder(org.eclipse.persistence.expressions.ExpressionBuilder)

Example 14 with SQLSelectStatement

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

the class ExpressionQueryMechanism method buildUpdateAllStatementForOracleAnonymousBlock.

protected SQLModifyStatement buildUpdateAllStatementForOracleAnonymousBlock(HashMap tables_databaseFieldsToValues, HashMap tablesToPrimaryKeyFields) {
    SQLSelectStatement selectStatement = createSQLSelectStatementForUpdateAllForOracleAnonymousBlock(tables_databaseFieldsToValues);
    SQLCall selectCall = (SQLCall) selectStatement.buildCall(getSession());
    SQLUpdateAllStatementForOracleAnonymousBlock updateAllStatement = new SQLUpdateAllStatementForOracleAnonymousBlock();
    updateAllStatement.setTranslationRow(getTranslationRow());
    updateAllStatement.setSelectCall(selectCall);
    updateAllStatement.setTables_databaseFieldsToValues(tables_databaseFieldsToValues);
    updateAllStatement.setTablesToPrimaryKeyFields(tablesToPrimaryKeyFields);
    updateAllStatement.setTable(getDescriptor().getTables().firstElement());
    return updateAllStatement;
}
Also used : SQLCall(org.eclipse.persistence.queries.SQLCall) SQLUpdateAllStatementForOracleAnonymousBlock(org.eclipse.persistence.internal.expressions.SQLUpdateAllStatementForOracleAnonymousBlock) SQLSelectStatement(org.eclipse.persistence.internal.expressions.SQLSelectStatement)

Example 15 with SQLSelectStatement

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

the class ExpressionQueryMechanism method buildSelectStatementForDoesExist.

/**
 * Return the appropriate select statement to perform a does exist check
 * @param field fields for does exist check.
 */
protected SQLSelectStatement buildSelectStatementForDoesExist(DatabaseField field) {
    // Build appropriate select statement
    SQLSelectStatement selectStatement;
    selectStatement = new SQLSelectStatement();
    selectStatement.addField(field);
    selectStatement.setWhereClause(((Expression) getDescriptor().getObjectBuilder().getPrimaryKeyExpression().clone()).and(getDescriptor().getQueryManager().getAdditionalJoinExpression()));
    selectStatement.setTranslationRow(getTranslationRow());
    selectStatement.normalize(getSession(), getQuery().getDescriptor());
    selectStatement.setHintString(getQuery().getHintString());
    return selectStatement;
}
Also used : SQLSelectStatement(org.eclipse.persistence.internal.expressions.SQLSelectStatement)

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