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);
}
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;
}
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());
}
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);
}
}
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);
}
Aggregations