Search in sources :

Example 1 with RelationExpression

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

the class ObjectPersistenceRuntimeXMLProject method buildRelationExpressionDescriptor.

protected ClassDescriptor buildRelationExpressionDescriptor() {
    XMLDescriptor descriptor = new XMLDescriptor();
    descriptor.setJavaClass(RelationExpression.class);
    descriptor.setDefaultRootElement("relation-expression");
    descriptor.getInheritancePolicy().setParentClass(Expression.class);
    // Child value expressions need their backpointer to their local base set,
    // this is not persisted so must be hooked back up after loading.
    descriptor.getEventManager().addListener(new DescriptorEventAdapter() {

        @Override
        public void postBuild(DescriptorEvent event) {
            RelationExpression expression = (RelationExpression) event.getObject();
            if ((expression.getFirstChild() != null) && (expression.getSecondChild() != null)) {
                if (expression.getSecondChild().isValueExpression()) {
                    expression.getSecondChild().setLocalBase(expression.getFirstChild());
                }
                if (expression.getFirstChild().isValueExpression()) {
                    expression.getFirstChild().setLocalBase(expression.getSecondChild());
                }
            }
        }
    });
    XMLDirectMapping operatorMapping = new XMLDirectMapping();
    operatorMapping.setAttributeName("operator");
    ObjectTypeConverter operatorConverter = new ObjectTypeConverter();
    operatorConverter.addConversionValue("equal", ExpressionOperator.getOperator(ExpressionOperator.Equal));
    operatorConverter.addConversionValue("notEqual", ExpressionOperator.getOperator(ExpressionOperator.NotEqual));
    operatorConverter.addConversionValue("like", ExpressionOperator.getOperator(ExpressionOperator.Like));
    operatorConverter.addConversionValue("notLike", ExpressionOperator.getOperator(ExpressionOperator.NotLike));
    operatorConverter.addConversionValue("greaterThan", ExpressionOperator.getOperator(ExpressionOperator.GreaterThan));
    operatorConverter.addConversionValue("greaterThanEqual", ExpressionOperator.getOperator(ExpressionOperator.GreaterThanEqual));
    operatorConverter.addConversionValue("lessThan", ExpressionOperator.getOperator(ExpressionOperator.LessThan));
    operatorConverter.addConversionValue("lessThanEqual", ExpressionOperator.getOperator(ExpressionOperator.LessThanEqual));
    operatorMapping.setConverter(operatorConverter);
    operatorMapping.setXPath("@operator");
    descriptor.addMapping(operatorMapping);
    XMLCompositeObjectMapping leftMapping = new XMLCompositeObjectMapping();
    leftMapping.setAttributeName("firstChild");
    leftMapping.setGetMethodName("getFirstChild");
    leftMapping.setSetMethodName("setFirstChild");
    leftMapping.setReferenceClass(Expression.class);
    leftMapping.setXPath(getPrimaryNamespaceXPath() + "left");
    descriptor.addMapping(leftMapping);
    XMLCompositeObjectMapping rightMapping = new XMLCompositeObjectMapping();
    rightMapping.setAttributeName("secondChild");
    rightMapping.setGetMethodName("getSecondChild");
    rightMapping.setSetMethodName("setSecondChild");
    rightMapping.setReferenceClass(Expression.class);
    rightMapping.setXPath(getPrimaryNamespaceXPath() + "right");
    descriptor.addMapping(rightMapping);
    return descriptor;
}
Also used : XMLDescriptor(org.eclipse.persistence.oxm.XMLDescriptor) XMLDirectMapping(org.eclipse.persistence.oxm.mappings.XMLDirectMapping) RelationExpression(org.eclipse.persistence.internal.expressions.RelationExpression) DescriptorEventAdapter(org.eclipse.persistence.descriptors.DescriptorEventAdapter) ObjectTypeConverter(org.eclipse.persistence.mappings.converters.ObjectTypeConverter) DescriptorEvent(org.eclipse.persistence.descriptors.DescriptorEvent) XMLCompositeObjectMapping(org.eclipse.persistence.oxm.mappings.XMLCompositeObjectMapping)

Example 2 with RelationExpression

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

the class MongoPlatform method appendExpressionToQueryRow.

/**
 * Append the expression and recursively to the query row.
 */
protected void appendExpressionToQueryRow(Expression expression, AbstractRecord row, DatabaseQuery query) {
    if (expression.isRelationExpression()) {
        RelationExpression relation = (RelationExpression) expression;
        Object left = extractValueFromExpression(relation.getFirstChild(), query);
        Object right = extractValueFromExpression(relation.getSecondChild(), query);
        if (relation.getOperator().getSelector() == ExpressionOperator.Equal) {
            row.put(left, right);
        } else {
            DatabaseRecord nested = new DatabaseRecord();
            if (relation.getOperator().getSelector() == ExpressionOperator.GreaterThan) {
                nested.put("$gt", right);
            } else if (relation.getOperator().getSelector() == ExpressionOperator.LessThan) {
                nested.put("$lt", right);
            } else if (relation.getOperator().getSelector() == ExpressionOperator.LessThanEqual) {
                nested.put("$lte", right);
            } else if (relation.getOperator().getSelector() == ExpressionOperator.GreaterThanEqual) {
                nested.put("$gte", right);
            } else if (relation.getOperator().getSelector() == ExpressionOperator.NotEqual) {
                nested.put("$ne", right);
            } else if (relation.getOperator().getSelector() == ExpressionOperator.In) {
                nested.put("$in", right);
                row.put(left, nested);
            } else if (relation.getOperator().getSelector() == ExpressionOperator.NotIn) {
                nested.put("$nin", right);
                row.put(left, nested);
            } else {
                throw new EISException("Query too complex for Mongo translation, relation [" + expression + "] not supported in query: " + query);
            }
            row.put(left, nested);
        }
    } else if (expression.isLogicalExpression()) {
        LogicalExpression logic = (LogicalExpression) expression;
        DatabaseRecord first = new DatabaseRecord();
        DatabaseRecord second = new DatabaseRecord();
        appendExpressionToQueryRow(logic.getFirstChild(), first, query);
        appendExpressionToQueryRow(logic.getSecondChild(), second, query);
        List<DatabaseRecord> nested = new Vector<>();
        nested.add(first);
        nested.add(second);
        if (logic.getOperator().getSelector() == ExpressionOperator.And) {
            row.put("$and", nested);
        } else if (logic.getOperator().getSelector() == ExpressionOperator.Or) {
            row.put("$or", nested);
        } else {
            throw new EISException("Query too complex for Mongo translation, logic [" + expression + "] not supported in query: " + query);
        }
    } else if (expression.isFunctionExpression()) {
        FunctionExpression function = (FunctionExpression) expression;
        if (function.getOperator().getSelector() == ExpressionOperator.Like) {
            Object left = extractValueFromExpression(function.getChildren().get(0), query);
            Object right = extractValueFromExpression(function.getChildren().get(1), query);
            if (!(right instanceof String)) {
                throw new EISException("Query too complex for Mongo translation, like with [" + right + "] not supported in query: " + query);
            }
            String pattern = (String) right;
            DatabaseRecord nested = new DatabaseRecord();
            if (!this.isLikeRegex) {
                pattern = Helper.convertLikeToRegex(pattern);
            }
            nested.put("$regex", pattern);
            row.put(left, nested);
        } else if (function.getOperator().getSelector() == ExpressionOperator.Not) {
            // Detect situation 'not(a = b)' and change it to '(a != b)'
            Expression expr = function.getChildren().get(0);
            if (expr.isRelationExpression()) {
                RelationExpression relation = (RelationExpression) expr;
                Object left = extractValueFromExpression(relation.getFirstChild(), query);
                Object right = extractValueFromExpression(relation.getSecondChild(), query);
                DatabaseRecord nested = new DatabaseRecord();
                if (expr.getOperator().getSelector() == ExpressionOperator.Equal) {
                    nested.put("$ne", right);
                } else {
                    nested.put("not", right);
                }
                row.put(left, nested);
            } else {
                throw new EISException("Query too complex for Mongo translation, function [" + expression + "] not supported in query: " + query);
            }
        } else {
            throw new EISException("Query too complex for Mongo translation, function [" + expression + "] not supported in query: " + query);
        }
    } else {
        throw new EISException("Query too complex for Mongo translation, expression [" + expression + "] not supported in query: " + query);
    }
}
Also used : LogicalExpression(org.eclipse.persistence.internal.expressions.LogicalExpression) FunctionExpression(org.eclipse.persistence.internal.expressions.FunctionExpression) DatabaseRecord(org.eclipse.persistence.sessions.DatabaseRecord) RelationExpression(org.eclipse.persistence.internal.expressions.RelationExpression) 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) EISException(org.eclipse.persistence.eis.EISException) List(java.util.List)

Example 3 with RelationExpression

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

the class InImpl method value.

/**
 *  Add to list of values to be tested against.
 *  @param value expression
 *  @return in predicate
 */
@Override
public In<T> value(Expression<? extends T> value) {
    if (!(((InternalExpression) value).isLiteral() || ((InternalExpression) value).isParameter())) {
        RelationExpression baseIn = (RelationExpression) this.currentNode;
        this.currentNode = baseIn.getFirstChild().in(((SubQueryImpl) value).subQuery);
        if (this.parentNode != null) {
            if (this.parentNode.isCompoundExpression()) {
                CompoundExpression logExp = (LogicalExpression) this.parentNode;
                if (logExp.getFirstChild() == baseIn) {
                    logExp.create(this.currentNode, logExp.getSecondChild(), logExp.getOperator());
                } else {
                    logExp.create(logExp.getFirstChild(), this.currentNode, logExp.getOperator());
                }
            } else {
                FunctionExpression funcExp = (FunctionExpression) this.parentNode;
                funcExp.getChildren().set(funcExp.getChildren().indexOf(baseIn), this.currentNode);
            }
        }
    } else {
        if (this.currentNode.isRelationExpression()) {
            RelationExpression baseIn = (RelationExpression) this.currentNode;
            org.eclipse.persistence.expressions.Expression resultExp = ((InternalSelection) value).getCurrentNode();
            resultExp = org.eclipse.persistence.expressions.Expression.from(resultExp, baseIn.getFirstChild());
            ((Collection) ((CollectionExpression) baseIn.getSecondChild()).getValue()).add(resultExp);
            // Add to the expression list for #findRootAndParameters()
            this.expressions.add(value);
        } else {
            throw new IllegalStateException(ExceptionLocalization.buildMessage("CANNOT_ADD_CONSTANTS_TO_SUBQUERY_IN"));
        }
    }
    return this;
}
Also used : LogicalExpression(org.eclipse.persistence.internal.expressions.LogicalExpression) FunctionExpression(org.eclipse.persistence.internal.expressions.FunctionExpression) RelationExpression(org.eclipse.persistence.internal.expressions.RelationExpression) CompoundExpression(org.eclipse.persistence.internal.expressions.CompoundExpression) Collection(java.util.Collection)

Aggregations

RelationExpression (org.eclipse.persistence.internal.expressions.RelationExpression)3 FunctionExpression (org.eclipse.persistence.internal.expressions.FunctionExpression)2 LogicalExpression (org.eclipse.persistence.internal.expressions.LogicalExpression)2 Collection (java.util.Collection)1 List (java.util.List)1 DescriptorEvent (org.eclipse.persistence.descriptors.DescriptorEvent)1 DescriptorEventAdapter (org.eclipse.persistence.descriptors.DescriptorEventAdapter)1 EISException (org.eclipse.persistence.eis.EISException)1 Expression (org.eclipse.persistence.expressions.Expression)1 CompoundExpression (org.eclipse.persistence.internal.expressions.CompoundExpression)1 ConstantExpression (org.eclipse.persistence.internal.expressions.ConstantExpression)1 FieldExpression (org.eclipse.persistence.internal.expressions.FieldExpression)1 ParameterExpression (org.eclipse.persistence.internal.expressions.ParameterExpression)1 QueryKeyExpression (org.eclipse.persistence.internal.expressions.QueryKeyExpression)1 ObjectTypeConverter (org.eclipse.persistence.mappings.converters.ObjectTypeConverter)1 XMLDescriptor (org.eclipse.persistence.oxm.XMLDescriptor)1 XMLCompositeObjectMapping (org.eclipse.persistence.oxm.mappings.XMLCompositeObjectMapping)1 XMLDirectMapping (org.eclipse.persistence.oxm.mappings.XMLDirectMapping)1 DatabaseRecord (org.eclipse.persistence.sessions.DatabaseRecord)1