use of org.eclipse.persistence.internal.expressions.ConstantExpression in project eclipselink by eclipse-ee4j.
the class ExpressionBuilderVisitor method visit.
@Override
public void visit(ArithmeticFactor expression) {
// First create the Expression that is prepended with the unary sign
expression.getExpression().accept(this);
Expression arithmeticFactor = queryExpression;
// Create an expression for the constant 0 (zero)
queryExpression = new ConstantExpression(0, new ExpressionBuilder());
// "- <something>" is really "0 - <something>"
queryExpression = ExpressionMath.subtract(queryExpression, arithmeticFactor);
// Note: The type will be calculated when traversing the sub-expression
}
use of org.eclipse.persistence.internal.expressions.ConstantExpression in project eclipselink by eclipse-ee4j.
the class ExpressionBuilderVisitor method visit.
@Override
public void visit(NumericLiteral expression) {
// Instantiate a Number object with the value
type[0] = queryContext.getType(expression);
// Special case for a long number, Long.parseLong() does not handle 'l|L'
// but Double.parseDouble() and Float.parseFloat() do handle 'd|D' and 'f|F', respectively
String text = expression.getText();
if ((type[0] == Long.class) && (text.endsWith("L") || text.endsWith("l"))) {
text = text.substring(0, text.length() - 1);
}
@SuppressWarnings({ "unchecked" }) Number number = queryContext.newInstance((Class<? extends Number>) type[0], String.class, text);
// Now create the numeric expression
queryExpression = new ConstantExpression(number, queryContext.getBaseExpression());
}
use of org.eclipse.persistence.internal.expressions.ConstantExpression in project eclipselink by eclipse-ee4j.
the class VariableNode method generateExpression.
@Override
public Expression generateExpression(GenerationContext generationContext) {
Expression myExpression = null;
String name = getCanonicalVariableName();
// is there a cached Expression?
myExpression = generationContext.expressionFor(name);
if (myExpression != null) {
return myExpression;
}
// Either I have an alias type, or I'm an IN declaration
if (classConstant != null) {
myExpression = new ConstantExpression(classConstant, generationContext.getBaseExpression());
} else if (generationContext.getParseTreeContext().isRangeVariable(name)) {
myExpression = generateBaseBuilderExpression(generationContext);
} else {
myExpression = generateExpressionForAlias(generationContext);
}
generationContext.addExpression(myExpression, name);
return myExpression;
}
use of org.eclipse.persistence.internal.expressions.ConstantExpression in project eclipselink by eclipse-ee4j.
the class JUnitJPQLExamplesTestSuite method testExistsExpression.
public void testExistsExpression() {
EntityManager em = createEntityManager();
boolean testPass = false;
ExpressionBuilder employeeBuilder = new ExpressionBuilder(Employee.class);
ExpressionBuilder managerBuilder = new ExpressionBuilder(Employee.class);
ReportQuery mainQuery = new ReportQuery();
ReportQuery subQuery = new ReportQuery();
subQuery.setReferenceClass(Employee.class);
Expression managerExpression = employeeBuilder.get("manager").get("id").equal(managerBuilder.get("id"));
subQuery.addAttribute("one", new ConstantExpression(1, subQuery.getExpressionBuilder()));
subQuery.setSelectionCriteria(managerExpression);
Expression employeeExpression = employeeBuilder.exists(subQuery);
mainQuery.setReferenceClass(Employee.class);
mainQuery.setSelectionCriteria(employeeExpression);
mainQuery.addAttribute("id");
mainQuery.returnWithoutReportQueryResult();
List expectedResult = (List) getServerSession().executeQuery(mainQuery);
String ejbqlString = "SELECT DISTINCT emp.id FROM Employee emp WHERE EXISTS ( SELECT managedEmp.id FROM Employee managedEmp WHERE managedEmp = emp.manager)";
List result = em.createQuery(ejbqlString).getResultList();
if (result.containsAll(expectedResult) && expectedResult.containsAll(result))
testPass = true;
// 8 employees with managers
Assert.assertEquals("Exists Expression test failed: data validation error", result.size(), 8);
Assert.assertTrue("Exists Expression test failed", testPass);
}
use of org.eclipse.persistence.internal.expressions.ConstantExpression 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;
}
Aggregations