Search in sources :

Example 16 with SQLSelectStatement

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

the class AbstractDirectMapping method buildSelectionQueryForDirectCollectionKeyMapping.

/**
 * INTERNAL:
 * Certain key mappings favor different types of selection query.  Return the appropriate
 * type of selectionQuery.
 */
@Override
public ReadQuery buildSelectionQueryForDirectCollectionKeyMapping(ContainerPolicy containerPolicy) {
    DataReadQuery query = new DataReadQuery();
    query.setSQLStatement(new SQLSelectStatement());
    query.setContainerPolicy(containerPolicy);
    return query;
}
Also used : SQLSelectStatement(org.eclipse.persistence.internal.expressions.SQLSelectStatement)

Example 17 with SQLSelectStatement

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

the class ObjectRelationalDataTypeDescriptor method getRef.

/**
 * INTERNAL:
 * Get the ref for the object.
 * This is required for use by Refs, there might be a better way to do it when objID are supported.
 * (i.e. getting it from the object or identity map).
 */
public Ref getRef(Object object, AbstractSession session) {
    SQLSelectStatement statement = new SQLSelectStatement();
    // Assumed only one for obj-rel descriptors.
    statement.addTable(getTables().firstElement());
    statement.getFields().addElement(new org.eclipse.persistence.expressions.ExpressionBuilder().ref());
    statement.setWhereClause(getObjectBuilder().buildPrimaryKeyExpressionFromObject(object, session));
    statement.setRequiresAliases(true);
    statement.normalize(session, this);
    ValueReadQuery valueQuery = new ValueReadQuery();
    valueQuery.setSQLStatement(statement);
    valueQuery.checkPrepare(session, new DatabaseRecord(), true);
    // Must return unwrapped Ref on WLS.
    valueQuery.getCall().setIsNativeConnectionRequired(true);
    Ref ref = (Ref) session.executeQuery(valueQuery);
    return ref;
}
Also used : Ref(java.sql.Ref) DatabaseRecord(org.eclipse.persistence.sessions.DatabaseRecord) ValueReadQuery(org.eclipse.persistence.queries.ValueReadQuery) SQLSelectStatement(org.eclipse.persistence.internal.expressions.SQLSelectStatement)

Example 18 with SQLSelectStatement

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

the class InheritancePolicy method buildViewSelectStatement.

/**
 * INTERNAL:
 * Build a select statement for all subclasses on the view using the same
 * selection criteria as the query.
 */
public SQLSelectStatement buildViewSelectStatement(ObjectLevelReadQuery query) {
    // 2612538 - the default size of Map (32) is appropriate
    Map clonedExpressions = new IdentityHashMap();
    ExpressionQueryMechanism mechanism = (ExpressionQueryMechanism) query.getQueryMechanism();
    // CR#3166555 - Have the mechanism build the statement to avoid duplicating code and ensure that lock-mode, hints, hierarchical, etc. are set.
    SQLSelectStatement selectStatement = mechanism.buildBaseSelectStatement(false, clonedExpressions);
    selectStatement.setTables(new ArrayList<>(1));
    selectStatement.addTable(getReadAllSubclassesView());
    // Case, normal read for branch inheritance class that reads subclasses all in its own table(s).
    if (getWithAllSubclassesExpression() != null) {
        Expression branchIndicator = (Expression) getWithAllSubclassesExpression().clone();
        if (branchIndicator != null) {
            selectStatement.setWhereClause(branchIndicator.and(selectStatement.getWhereClause()));
        }
    }
    selectStatement.setFields(mechanism.getSelectionFields(selectStatement, true));
    selectStatement.normalizeForView(query.getSession(), getDescriptor(), clonedExpressions);
    // Allow for joining indexes to be computed to ensure distinct rows
    if (query.hasJoining()) {
        query.getJoinedAttributeManager().computeJoiningMappingIndexes(false, query.getSession(), 0);
    }
    return selectStatement;
}
Also used : Expression(org.eclipse.persistence.expressions.Expression) IdentityHashMap(java.util.IdentityHashMap) SQLSelectStatement(org.eclipse.persistence.internal.expressions.SQLSelectStatement) ExpressionQueryMechanism(org.eclipse.persistence.internal.queries.ExpressionQueryMechanism) HashMap(java.util.HashMap) Map(java.util.Map) IdentityHashMap(java.util.IdentityHashMap)

Example 19 with SQLSelectStatement

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

the class InheritancePolicy method buildClassIndicatorSelectStatement.

/**
 * INTERNAL:
 * Return a select statement that will be used to query the class indicators required to query.
 * This is used in the abstract-multiple read.
 */
public SQLSelectStatement buildClassIndicatorSelectStatement(ObjectLevelReadQuery query) {
    SQLSelectStatement selectStatement;
    selectStatement = new SQLSelectStatement();
    selectStatement.useDistinct();
    selectStatement.addTable(classIndicatorField.getTable());
    selectStatement.addField(getClassIndicatorField());
    // 2612538 - the default size of Map (32) is appropriate
    Map clonedExpressions = new IdentityHashMap();
    selectStatement.setWhereClause(((ExpressionQueryMechanism) query.getQueryMechanism()).buildBaseSelectionCriteria(false, clonedExpressions));
    appendWithAllSubclassesExpression(selectStatement);
    selectStatement.setTranslationRow(query.getTranslationRow());
    if (query.isReadAllQuery() && ((ReadAllQuery) query).hasHierarchicalExpressions()) {
        ReadAllQuery readAllQuery = (ReadAllQuery) query;
        selectStatement.setHierarchicalQueryExpressions(readAllQuery.getStartWithExpression(), readAllQuery.getConnectByExpression(), readAllQuery.getOrderSiblingsByExpressions(), readAllQuery.getDirection());
    }
    selectStatement.setHintString(query.getHintString());
    selectStatement.normalize(query.getSession(), getDescriptor(), clonedExpressions);
    return selectStatement;
}
Also used : IdentityHashMap(java.util.IdentityHashMap) ReadAllQuery(org.eclipse.persistence.queries.ReadAllQuery) SQLSelectStatement(org.eclipse.persistence.internal.expressions.SQLSelectStatement) HashMap(java.util.HashMap) Map(java.util.Map) IdentityHashMap(java.util.IdentityHashMap)

Example 20 with SQLSelectStatement

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

the class MongoPlatform method buildCallFromStatement.

/**
 * INTERNAL:
 * Override this method to throw an exception by default.
 * Platforms that support dynamic querying can override this to generate an EISInteraction.
 */
@Override
public DatasourceCall buildCallFromStatement(SQLStatement statement, DatabaseQuery query, AbstractSession session) {
    if (query.isObjectLevelReadQuery()) {
        ObjectLevelReadQuery readQuery = (ObjectLevelReadQuery) query;
        MappedInteraction interaction = new MappedInteraction();
        interaction.setProperty(OPERATION, MongoOperation.FIND);
        interaction.setProperty(COLLECTION, ((EISDescriptor) query.getDescriptor()).getDataTypeName());
        if (readQuery.getFirstResult() > 0) {
            interaction.setProperty(SKIP, readQuery.getFirstResult());
        }
        if (readQuery.getMaxRows() > 0) {
            interaction.setProperty(LIMIT, readQuery.getMaxRows());
        }
        if (readQuery.getFetchSize() > 0) {
            interaction.setProperty(BATCH_SIZE, readQuery.getMaxRows());
        }
        DatabaseRecord row = new DatabaseRecord();
        if (statement.getWhereClause() != null) {
            appendExpressionToQueryRow(statement.getWhereClause(), row, query);
        }
        if (readQuery.hasOrderByExpressions()) {
            DatabaseRecord sort = new DatabaseRecord();
            for (Expression orderBy : readQuery.getOrderByExpressions()) {
                appendExpressionToSortRow(orderBy, sort, query);
            }
            row.put(MongoRecord.SORT, sort);
        }
        if (readQuery.isReportQuery()) {
            DatabaseRecord select = new DatabaseRecord();
            for (Object field : ((SQLSelectStatement) statement).getFields()) {
                if (field instanceof DatabaseField) {
                    select.put((DatabaseField) field, 1);
                } else if (field instanceof Expression) {
                    Object value = extractValueFromExpression((Expression) field, readQuery);
                    if (!(value instanceof DatabaseField)) {
                        throw new EISException("Query too complex for Mongo translation, only field selects are supported in query: " + query);
                    }
                    select.put((DatabaseField) value, 1);
                }
            }
            row.put("$select", select);
        }
        interaction.setInputRow(row);
        return interaction;
    }
    throw new EISException("Query too complex for Mongo translation, only select queries are supported in query: " + query);
}
Also used : ObjectLevelReadQuery(org.eclipse.persistence.queries.ObjectLevelReadQuery) MappedInteraction(org.eclipse.persistence.eis.interactions.MappedInteraction) DatabaseRecord(org.eclipse.persistence.sessions.DatabaseRecord) RelationExpression(org.eclipse.persistence.internal.expressions.RelationExpression) FieldExpression(org.eclipse.persistence.internal.expressions.FieldExpression) QueryKeyExpression(org.eclipse.persistence.internal.expressions.QueryKeyExpression) ParameterExpression(org.eclipse.persistence.internal.expressions.ParameterExpression) LogicalExpression(org.eclipse.persistence.internal.expressions.LogicalExpression) FunctionExpression(org.eclipse.persistence.internal.expressions.FunctionExpression) ConstantExpression(org.eclipse.persistence.internal.expressions.ConstantExpression) Expression(org.eclipse.persistence.expressions.Expression) DatabaseField(org.eclipse.persistence.internal.helper.DatabaseField) SQLSelectStatement(org.eclipse.persistence.internal.expressions.SQLSelectStatement) EISException(org.eclipse.persistence.eis.EISException)

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