Search in sources :

Example 1 with LogicalExpression

use of org.eclipse.persistence.internal.expressions.LogicalExpression 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 2 with LogicalExpression

use of org.eclipse.persistence.internal.expressions.LogicalExpression 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

FunctionExpression (org.eclipse.persistence.internal.expressions.FunctionExpression)2 LogicalExpression (org.eclipse.persistence.internal.expressions.LogicalExpression)2 RelationExpression (org.eclipse.persistence.internal.expressions.RelationExpression)2 Collection (java.util.Collection)1 List (java.util.List)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 DatabaseRecord (org.eclipse.persistence.sessions.DatabaseRecord)1