Search in sources :

Example 6 with ParameterExpression

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

the class JUnitJPQLComplexTestSuite method testCollectionQueryKeysFieldValueWithNoMapping.

// Bug# 413146 - Test query with collection as a condition in query for attribute with no mapping.
// In current model Employee.startTime has no explicit mapping so QueryKeyExpression.getMapping() returns null.
public void testCollectionQueryKeysFieldValueWithNoMapping() {
    EntityManager em = createEntityManager();
    // Run query to trigger code path related to Bug# 413146
    Query query = em.createQuery("Select e.startTime from Employee e where e.startTime in (:time1, :time2)");
    query.setParameter("time1", new java.util.Date());
    query.setParameter("time2", new java.util.Date());
    query.getResultList();
    // Verify that no mapping exists for Employee.startTime - test is not valid on server
    if (!isOnServer()) {
        ReportQuery rq = (ReportQuery) ((QueryImpl) query).getDatabaseQueryInternal();
        CallQueryMechanism qm = rq != null ? (CallQueryMechanism) rq.getQueryMechanism() : null;
        DatasourceCall sc = qm != null ? qm.getDatabaseCall() : null;
        List params = sc != null ? sc.getParameters() : null;
        ParameterExpression pe = params != null && params.size() > 0 ? (ParameterExpression) params.get(0) : null;
        QueryKeyExpression qke = pe != null ? (QueryKeyExpression) pe.getLocalBase() : null;
        if (qke != null) {
            DatabaseMapping mapping = qke.getMapping();
            assertNull("There shall be no mapping for Employee.startTime", mapping);
        } else {
            fail("Could not retrieve mapping from QueryKeyExpression.");
        }
    }
    closeEntityManager(em);
}
Also used : EntityManager(jakarta.persistence.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) Query(jakarta.persistence.Query) ReadObjectQuery(org.eclipse.persistence.queries.ReadObjectQuery) ReportQuery(org.eclipse.persistence.queries.ReportQuery) DatabaseQuery(org.eclipse.persistence.queries.DatabaseQuery) ReadAllQuery(org.eclipse.persistence.queries.ReadAllQuery) ReportQuery(org.eclipse.persistence.queries.ReportQuery) ParameterExpression(org.eclipse.persistence.internal.expressions.ParameterExpression) List(java.util.List) ArrayList(java.util.ArrayList) DatabaseMapping(org.eclipse.persistence.mappings.DatabaseMapping) DatasourceCall(org.eclipse.persistence.internal.databaseaccess.DatasourceCall) CallQueryMechanism(org.eclipse.persistence.internal.queries.CallQueryMechanism) QueryKeyExpression(org.eclipse.persistence.internal.expressions.QueryKeyExpression)

Example 7 with ParameterExpression

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

the class MongoPlatform method extractValueFromExpression.

/**
 * Extract the field or constant value from the comparison expression.
 */
protected Object extractValueFromExpression(Expression expression, DatabaseQuery query) {
    Object value = null;
    expression.getBuilder().setSession(query.getSession());
    if (expression.isQueryKeyExpression()) {
        QueryKeyExpression queryKeyExpression = (QueryKeyExpression) expression;
        value = queryKeyExpression.getField();
        if ((queryKeyExpression.getMapping() != null) && queryKeyExpression.getMapping().getDescriptor().isDescriptorTypeAggregate()) {
            String name = queryKeyExpression.getField().getName();
            while (queryKeyExpression.getBaseExpression().isQueryKeyExpression() && (((QueryKeyExpression) queryKeyExpression.getBaseExpression()).getMapping().isAbstractCompositeObjectMapping() || ((QueryKeyExpression) queryKeyExpression.getBaseExpression()).getMapping().isAbstractCompositeCollectionMapping() || ((QueryKeyExpression) queryKeyExpression.getBaseExpression()).getMapping().isAbstractCompositeDirectCollectionMapping())) {
                queryKeyExpression = (QueryKeyExpression) queryKeyExpression.getBaseExpression();
                if (queryKeyExpression.getMapping().isAbstractCompositeObjectMapping()) {
                    name = queryKeyExpression.getMapping().getField().getName() + "." + name;
                } else if (queryKeyExpression.getMapping().isAbstractCompositeCollectionMapping()) {
                    name = queryKeyExpression.getMapping().getField().getName() + "." + name;
                } else if (queryKeyExpression.getMapping().isAbstractCompositeDirectCollectionMapping()) {
                    name = queryKeyExpression.getMapping().getField().getName() + "." + name;
                }
            }
            DatabaseField field = new DatabaseField();
            field.setName(name);
            value = field;
        }
    } else if (expression.isFieldExpression()) {
        value = ((FieldExpression) expression).getField();
    } else if (expression.isConstantExpression()) {
        value = ((ConstantExpression) expression).getValue();
        if (((ConstantExpression) expression).getLocalBase() != null) {
            value = ((ConstantExpression) expression).getLocalBase().getFieldValue(value, query.getSession());
        }
    } else if (expression.isParameterExpression()) {
        value = query.getTranslationRow().get(((ParameterExpression) expression).getField());
        if (((ParameterExpression) expression).getLocalBase() != null) {
            value = ((ParameterExpression) expression).getLocalBase().getFieldValue(value, query.getSession());
        }
    } else {
        throw new EISException("Query too complex for Mongo translation, comparison of [" + expression + "] not supported in query: " + query);
    }
    if (value instanceof List) {
        @SuppressWarnings({ "unchecked" }) List<Object> values = (List<Object>) value;
        for (int index = 0; index < values.size(); index++) {
            Object element = values.get(index);
            if (element instanceof Expression) {
                element = extractValueFromExpression((Expression) element, query);
                values.set(index, element);
            }
        }
    }
    return value;
}
Also used : ConstantExpression(org.eclipse.persistence.internal.expressions.ConstantExpression) EISException(org.eclipse.persistence.eis.EISException) QueryKeyExpression(org.eclipse.persistence.internal.expressions.QueryKeyExpression) FieldExpression(org.eclipse.persistence.internal.expressions.FieldExpression) 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) ParameterExpression(org.eclipse.persistence.internal.expressions.ParameterExpression) List(java.util.List)

Example 8 with ParameterExpression

use of org.eclipse.persistence.internal.expressions.ParameterExpression in project jmix by jmix-framework.

the class ValueHoldersSupport method getEntityIdFromValueHolder.

public static Object getEntityIdFromValueHolder(QueryBasedValueHolder queryBasedValueHolder) {
    AtomicReference<String> fieldName = new AtomicReference<>();
    ExpressionIterator iterator = new ExpressionIterator() {

        @Override
        public void iterate(Expression each) {
            if (each instanceof ParameterExpression) {
                fieldName.set(((ParameterExpression) each).getField().getQualifiedName());
            }
        }
    };
    iterator.iterateOn(queryBasedValueHolder.getQuery().getSelectionCriteria());
    return queryBasedValueHolder.getRow().get(fieldName.get());
}
Also used : ExpressionIterator(org.eclipse.persistence.internal.expressions.ExpressionIterator) Expression(org.eclipse.persistence.expressions.Expression) ParameterExpression(org.eclipse.persistence.internal.expressions.ParameterExpression) ParameterExpression(org.eclipse.persistence.internal.expressions.ParameterExpression) AtomicReference(java.util.concurrent.atomic.AtomicReference)

Example 9 with ParameterExpression

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

the class DescriptorQueryManager method updatePropertyParameterExpression.

/**
 * INTERNAL:
 * This method will walk the given expression and mark any parameter
 * expressions as property expressions. This is done when additional
 * criteria has been specified and parameter values must be resolved
 * through session properties.
 *
 * @see #postInitialize
 */
protected void updatePropertyParameterExpression(Expression exp) {
    if (exp.isCompoundExpression()) {
        updatePropertyParameterExpression(((CompoundExpression) exp).getFirstChild());
        updatePropertyParameterExpression(((CompoundExpression) exp).getSecondChild());
    } else if (exp.isFunctionExpression()) {
        for (Expression e : ((FunctionExpression) exp).getChildren()) {
            updatePropertyParameterExpression(e);
        }
    } else if (exp.isSubSelectExpression()) {
        ReportQuery subSelectQuery = ((SubSelectExpression) exp).getSubQuery();
        for (ReportItem item : subSelectQuery.getItems()) {
            updatePropertyParameterExpression(item.getAttributeExpression());
        }
    }
    if (exp.isParameterExpression()) {
        ((ParameterExpression) exp).setIsProperty(true);
    }
}
Also used : SubSelectExpression(org.eclipse.persistence.internal.expressions.SubSelectExpression) SubSelectExpression(org.eclipse.persistence.internal.expressions.SubSelectExpression) ParameterExpression(org.eclipse.persistence.internal.expressions.ParameterExpression) CompoundExpression(org.eclipse.persistence.internal.expressions.CompoundExpression) FunctionExpression(org.eclipse.persistence.internal.expressions.FunctionExpression) Expression(org.eclipse.persistence.expressions.Expression) ReportQuery(org.eclipse.persistence.queries.ReportQuery) ParameterExpression(org.eclipse.persistence.internal.expressions.ParameterExpression) ReportItem(org.eclipse.persistence.internal.queries.ReportItem)

Example 10 with ParameterExpression

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

the class DatabaseCall method translate.

/**
 * INTERNAL:
 * Allow the call to translate from the translation for predefined calls.
 */
@Override
public void translate(AbstractRecord translationRow, AbstractRecord modifyRow, AbstractSession session) {
    if (!isPrepared()) {
        throw ValidationException.cannotTranslateUnpreparedCall(toString());
    }
    if (usesBinding(session) && (this.parameters != null)) {
        boolean hasParameterizedIN = false;
        List parameters = getParameters();
        List<Integer> parameterTypes = getParameterTypes();
        int size = parameters.size();
        List parametersValues = new ArrayList(size);
        for (int index = 0; index < size; index++) {
            Object parameter = parameters.get(index);
            Object parameterType = parameterTypes.get(index);
            if (parameterType == MODIFY) {
                DatabaseField field = (DatabaseField) parameter;
                Object value = modifyRow.get(field);
                // If the value is null, the field is passed as the value so the type can be obtained from the field.
                if (value == null) {
                    // The field from the modify row is used, as the calls field may not have the type,
                    // but if the field is missing the calls field may also have the type.
                    value = modifyRow.getField(field);
                    if (value == null) {
                        value = field;
                    }
                }
                parametersValues.add(value);
            } else if (parameterType == CUSTOM_MODIFY) {
                DatabaseField field = (DatabaseField) parameter;
                Object value = modifyRow.get(field);
                value = session.getPlatform().getCustomModifyValueForCall(this, value, field, true);
                // Bug#8200836 needs use unwrapped connection
                if ((value != null) && (value instanceof BindCallCustomParameter) && (((BindCallCustomParameter) value).shouldUseUnwrappedConnection())) {
                    this.isNativeConnectionRequired = true;
                }
                // If the value is null, the field is passed as the value so the type can be obtained from the field.
                if (value == null) {
                    // The field from the modify row is used, as the calls field may not have the type,
                    // but if the field is missing the calls field may also have the type.
                    value = modifyRow.getField(field);
                    if (value == null) {
                        value = field;
                    }
                }
                parametersValues.add(value);
            } else if (parameterType == TRANSLATION) {
                Object value = null;
                DatabaseField field = null;
                if (parameter instanceof ParameterExpression) {
                    field = ((ParameterExpression) parameter).getField();
                    value = ((ParameterExpression) parameter).getValue(translationRow, query, session);
                } else {
                    field = (DatabaseField) parameter;
                    value = translationRow.get(field);
                    if (value == null) {
                        // Backward compatibility double check.
                        value = modifyRow.get(field);
                    }
                }
                if (value instanceof Collection) {
                    // Must re-translate IN parameters.
                    hasParameterizedIN = true;
                }
                // If the value is null, the field is passed as the value so the type can be obtained from the field.
                if ((value == null) && (field != null)) {
                    if (!this.query.hasNullableArguments() || !this.query.getNullableArguments().contains(field)) {
                        value = translationRow.getField(field);
                        // but if the field is missing the calls field may also have the type.
                        if (value == null) {
                            value = field;
                        }
                        parametersValues.add(value);
                    }
                } else {
                    parametersValues.add(value);
                }
            } else if (parameterType == LITERAL) {
                parametersValues.add(parameter);
            } else if (parameterType == IN) {
                Object value = getValueForInParameter(parameter, translationRow, modifyRow, session, true);
                // Returning this means the parameter was optional and should not be included.
                if (value != this) {
                    parametersValues.add(value);
                }
            } else if (parameterType == INOUT) {
                Object value = getValueForInOutParameter(parameter, translationRow, modifyRow, session);
                parametersValues.add(value);
            } else if (parameterType == OUT || parameterType == OUT_CURSOR) {
                if (parameter != null) {
                    ((OutputParameterForCallableStatement) parameter).getOutputField().setIndex(index);
                }
                parametersValues.add(parameter);
            }
        }
        setParameters(parametersValues);
        // If an IN parameter was found must translate SQL.
        if (hasParameterizedIN) {
            translateQueryStringForParameterizedIN(translationRow, modifyRow, session);
        }
        return;
    }
    translateQueryString(translationRow, modifyRow, session);
}
Also used : ObjectRelationalDatabaseField(org.eclipse.persistence.mappings.structures.ObjectRelationalDatabaseField) DatabaseField(org.eclipse.persistence.internal.helper.DatabaseField) ParameterExpression(org.eclipse.persistence.internal.expressions.ParameterExpression) ArrayList(java.util.ArrayList) Collection(java.util.Collection) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

ParameterExpression (org.eclipse.persistence.internal.expressions.ParameterExpression)15 DatabaseField (org.eclipse.persistence.internal.helper.DatabaseField)8 Expression (org.eclipse.persistence.expressions.Expression)7 ArrayList (java.util.ArrayList)5 List (java.util.List)5 ConstantExpression (org.eclipse.persistence.internal.expressions.ConstantExpression)4 FieldExpression (org.eclipse.persistence.internal.expressions.FieldExpression)4 QueryKeyExpression (org.eclipse.persistence.internal.expressions.QueryKeyExpression)4 Collection (java.util.Collection)3 ExpressionIterator (org.eclipse.persistence.internal.expressions.ExpressionIterator)3 HashSet (java.util.HashSet)2 Map (java.util.Map)2 ClassDescriptor (org.eclipse.persistence.descriptors.ClassDescriptor)2 ObjectBuilder (org.eclipse.persistence.internal.descriptors.ObjectBuilder)2 DataExpression (org.eclipse.persistence.internal.expressions.DataExpression)2 FunctionExpression (org.eclipse.persistence.internal.expressions.FunctionExpression)2 ObjectExpression (org.eclipse.persistence.internal.expressions.ObjectExpression)2 DatabaseTable (org.eclipse.persistence.internal.helper.DatabaseTable)2 ObjectRelationalDatabaseField (org.eclipse.persistence.mappings.structures.ObjectRelationalDatabaseField)2 EntityManager (jakarta.persistence.EntityManager)1