Search in sources :

Example 6 with ConstantExpression

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
}
Also used : TrimExpression(org.eclipse.persistence.jpa.jpql.parser.TrimExpression) AbsExpression(org.eclipse.persistence.jpa.jpql.parser.AbsExpression) CoalesceExpression(org.eclipse.persistence.jpa.jpql.parser.CoalesceExpression) ConcatExpression(org.eclipse.persistence.jpa.jpql.parser.ConcatExpression) SubstringExpression(org.eclipse.persistence.jpa.jpql.parser.SubstringExpression) SizeExpression(org.eclipse.persistence.jpa.jpql.parser.SizeExpression) CastExpression(org.eclipse.persistence.jpa.jpql.parser.CastExpression) SubExpression(org.eclipse.persistence.jpa.jpql.parser.SubExpression) AndExpression(org.eclipse.persistence.jpa.jpql.parser.AndExpression) JPQLExpression(org.eclipse.persistence.jpa.jpql.parser.JPQLExpression) ExtractExpression(org.eclipse.persistence.jpa.jpql.parser.ExtractExpression) MapEntryExpression(org.eclipse.persistence.internal.expressions.MapEntryExpression) IndexExpression(org.eclipse.persistence.jpa.jpql.parser.IndexExpression) StateFieldPathExpression(org.eclipse.persistence.jpa.jpql.parser.StateFieldPathExpression) UpperExpression(org.eclipse.persistence.jpa.jpql.parser.UpperExpression) ExistsExpression(org.eclipse.persistence.jpa.jpql.parser.ExistsExpression) KeyExpression(org.eclipse.persistence.jpa.jpql.parser.KeyExpression) AllOrAnyExpression(org.eclipse.persistence.jpa.jpql.parser.AllOrAnyExpression) ModExpression(org.eclipse.persistence.jpa.jpql.parser.ModExpression) Expression(org.eclipse.persistence.expressions.Expression) LikeExpression(org.eclipse.persistence.jpa.jpql.parser.LikeExpression) CaseExpression(org.eclipse.persistence.jpa.jpql.parser.CaseExpression) LengthExpression(org.eclipse.persistence.jpa.jpql.parser.LengthExpression) MultiplicationExpression(org.eclipse.persistence.jpa.jpql.parser.MultiplicationExpression) UnknownExpression(org.eclipse.persistence.jpa.jpql.parser.UnknownExpression) NullComparisonExpression(org.eclipse.persistence.jpa.jpql.parser.NullComparisonExpression) ValueExpression(org.eclipse.persistence.jpa.jpql.parser.ValueExpression) ComparisonExpression(org.eclipse.persistence.jpa.jpql.parser.ComparisonExpression) InExpression(org.eclipse.persistence.jpa.jpql.parser.InExpression) NotExpression(org.eclipse.persistence.jpa.jpql.parser.NotExpression) DateConstantExpression(org.eclipse.persistence.internal.expressions.DateConstantExpression) BadExpression(org.eclipse.persistence.jpa.jpql.parser.BadExpression) BetweenExpression(org.eclipse.persistence.jpa.jpql.parser.BetweenExpression) SqrtExpression(org.eclipse.persistence.jpa.jpql.parser.SqrtExpression) FunctionExpression(org.eclipse.persistence.jpa.jpql.parser.FunctionExpression) ConstantExpression(org.eclipse.persistence.internal.expressions.ConstantExpression) TableExpression(org.eclipse.persistence.jpa.jpql.parser.TableExpression) CollectionMemberExpression(org.eclipse.persistence.jpa.jpql.parser.CollectionMemberExpression) AbstractPathExpression(org.eclipse.persistence.jpa.jpql.parser.AbstractPathExpression) NullExpression(org.eclipse.persistence.jpa.jpql.parser.NullExpression) ConstructorExpression(org.eclipse.persistence.jpa.jpql.parser.ConstructorExpression) NullIfExpression(org.eclipse.persistence.jpa.jpql.parser.NullIfExpression) CollectionValuedPathExpression(org.eclipse.persistence.jpa.jpql.parser.CollectionValuedPathExpression) LocateExpression(org.eclipse.persistence.jpa.jpql.parser.LocateExpression) AdditionExpression(org.eclipse.persistence.jpa.jpql.parser.AdditionExpression) ObjectExpression(org.eclipse.persistence.jpa.jpql.parser.ObjectExpression) CollectionExpression(org.eclipse.persistence.jpa.jpql.parser.CollectionExpression) RegexpExpression(org.eclipse.persistence.jpa.jpql.parser.RegexpExpression) ParameterExpression(org.eclipse.persistence.internal.expressions.ParameterExpression) LowerExpression(org.eclipse.persistence.jpa.jpql.parser.LowerExpression) OrExpression(org.eclipse.persistence.jpa.jpql.parser.OrExpression) TreatExpression(org.eclipse.persistence.jpa.jpql.parser.TreatExpression) TypeExpression(org.eclipse.persistence.jpa.jpql.parser.TypeExpression) EmptyCollectionComparisonExpression(org.eclipse.persistence.jpa.jpql.parser.EmptyCollectionComparisonExpression) SubtractionExpression(org.eclipse.persistence.jpa.jpql.parser.SubtractionExpression) EntryExpression(org.eclipse.persistence.jpa.jpql.parser.EntryExpression) DivisionExpression(org.eclipse.persistence.jpa.jpql.parser.DivisionExpression) KeywordExpression(org.eclipse.persistence.jpa.jpql.parser.KeywordExpression) DateConstantExpression(org.eclipse.persistence.internal.expressions.DateConstantExpression) ConstantExpression(org.eclipse.persistence.internal.expressions.ConstantExpression) ExpressionBuilder(org.eclipse.persistence.expressions.ExpressionBuilder)

Example 7 with ConstantExpression

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());
}
Also used : DateConstantExpression(org.eclipse.persistence.internal.expressions.DateConstantExpression) ConstantExpression(org.eclipse.persistence.internal.expressions.ConstantExpression)

Example 8 with ConstantExpression

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;
}
Also used : ConstantExpression(org.eclipse.persistence.internal.expressions.ConstantExpression) Expression(org.eclipse.persistence.expressions.Expression) ConstantExpression(org.eclipse.persistence.internal.expressions.ConstantExpression)

Example 9 with ConstantExpression

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);
}
Also used : EntityManager(jakarta.persistence.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) ConstantExpression(org.eclipse.persistence.internal.expressions.ConstantExpression) Expression(org.eclipse.persistence.expressions.Expression) ReportQuery(org.eclipse.persistence.queries.ReportQuery) ConstantExpression(org.eclipse.persistence.internal.expressions.ConstantExpression) List(java.util.List) ExpressionBuilder(org.eclipse.persistence.expressions.ExpressionBuilder)

Example 10 with ConstantExpression

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

Aggregations

ConstantExpression (org.eclipse.persistence.internal.expressions.ConstantExpression)19 Expression (org.eclipse.persistence.expressions.Expression)7 DateConstantExpression (org.eclipse.persistence.internal.expressions.DateConstantExpression)6 ParameterExpression (org.eclipse.persistence.internal.expressions.ParameterExpression)5 ReportQuery (org.eclipse.persistence.queries.ReportQuery)5 ExpressionBuilder (org.eclipse.persistence.expressions.ExpressionBuilder)4 MapEntryExpression (org.eclipse.persistence.internal.expressions.MapEntryExpression)4 ReportItem (org.eclipse.persistence.internal.queries.ReportItem)4 List (java.util.List)3 FieldExpression (org.eclipse.persistence.internal.expressions.FieldExpression)3 ObjectExpression (org.eclipse.persistence.internal.expressions.ObjectExpression)3 QueryKeyExpression (org.eclipse.persistence.internal.expressions.QueryKeyExpression)3 DatabaseField (org.eclipse.persistence.internal.helper.DatabaseField)3 Collection (java.util.Collection)2 HashMap (java.util.HashMap)2 IdentityHashMap (java.util.IdentityHashMap)2 Iterator (java.util.Iterator)2 Map (java.util.Map)2 Vector (java.util.Vector)2 ExpressionIterator (org.eclipse.persistence.internal.expressions.ExpressionIterator)2