org.apache.olingo.odata2.api.uri.expression.BinaryExpression Java Examples
The following examples show how to use
org.apache.olingo.odata2.api.uri.expression.BinaryExpression.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JsonVisitor.java From olingo-odata2 with Apache License 2.0 | 6 votes |
@Override public Object visitBinary(final BinaryExpression binaryExpression, final BinaryOperator operator, final Object leftSide, final Object rightSide) { try { StringWriter writer = new StringWriter(); JsonStreamWriter jsonStreamWriter = new JsonStreamWriter(writer); jsonStreamWriter.beginObject().namedStringValueRaw("nodeType", binaryExpression.getKind().toString()).separator() .namedStringValue("operator", operator.toUriLiteral()).separator().namedStringValueRaw("type", getType(binaryExpression)).separator().name("left").unquotedValue(leftSide.toString()).separator().name( "right").unquotedValue(rightSide.toString()).endObject(); writer.flush(); return writer.toString(); } catch (final IOException e) { return null; } }
Example #2
Source File: SQLVisitor.java From DataHubSystem with GNU Affero General Public License v3.0 | 5 votes |
@Override public Object visitBinary(BinaryExpression binary_expression, BinaryOperator operator, Object left_side, Object right_side) { Criterion criterion; switch (operator) { case EQ: case NE: case GT: case GE: case LT: case LE: { criterion = getCriterionComparative(operator, left_side, right_side); break; } case AND: case OR: { Criterion left = (Criterion) left_side; Criterion right = (Criterion) right_side; criterion = getCriterionLogical(operator, left, right); break; } default: // Other operators are not supported for SQL Statements throw new UnsupportedOperationException("Unsupported operator: " + operator.toUriLiteral()); } // return the binary statement return criterion; }
Example #3
Source File: FunctionalVisitor.java From DataHubSystem with GNU Affero General Public License v3.0 | 5 votes |
@Override public Object visitBinary(BinaryExpression be, BinaryOperator op, Object left, Object right) { // `left` and `right` are instances of Node<?> (Expression Tree) // Returns a BiTransformer (functional Java) as a Node<?> (Expression Tree) Transformer res; switch(op) { case EQ: res = Transformers.<Object>eq(); break; case NE: res = Transformers.<Object>ne(); break; case AND: res = Transformers.and(); break; case OR: res = Transformers.or(); break; case GE: res = Transformers.ge(); break; case GT: res = Transformers.gt(); break; case LE: res = Transformers.le(); break; case LT: res = Transformers.lt(); break; case ADD: res = Transformers.add(); break; case SUB: res = Transformers.sub(); break; case MUL: res = Transformers.mul(); break; case DIV: res = Transformers.div(); break; case MODULO: res = Transformers.mod(); break; default: throw new UnsupportedOperationException("Unsupported operator: " + op.toUriLiteral()); } ExecutableExpressionTree.Node nleft = ExecutableExpressionTree.Node.class.cast(left); ExecutableExpressionTree.Node nright = ExecutableExpressionTree.Node.class.cast(right); return ExecutableExpressionTree.Node.createNode(res, nleft, nright); }
Example #4
Source File: ODataExpressionParser.java From olingo-odata2 with Apache License 2.0 | 5 votes |
private static EdmMapping getEdmMapping(BinaryExpression binaryExpression) throws EdmException { if(binaryExpression!=null && binaryExpression.getLeftOperand() instanceof PropertyExpression){ PropertyExpression left = (PropertyExpression)binaryExpression.getLeftOperand(); if(left != null && left.getEdmProperty() instanceof EdmElement){ EdmElement property = (EdmElement)left.getEdmProperty(); return property.getMapping(); } } return null; }
Example #5
Source File: DebugInfoUri.java From olingo-odata2 with Apache License 2.0 | 5 votes |
private void appendExpression(final CommonExpression expression, final Writer writer) throws IOException { final ExpressionKind kind = expression.getKind(); writer.append("<span class=\"kind\">") .append(kind.toString()) .append("</span> <span class=\"literal\">") .append(kind == ExpressionKind.MEMBER ? ((MemberExpression) expression).getProperty().getUriLiteral() : expression.getUriLiteral()) .append("</span>, type <span class=\"type\">") .append(expression.getEdmType().toString()) .append("</span>"); if (kind == ExpressionKind.UNARY) { writer.append("<ul class=\"expr\"><li>"); appendExpression(((UnaryExpression) expression).getOperand(), writer); writer.append("</li></ul>"); } else if (kind == ExpressionKind.BINARY) { writer.append("<ol class=\"expr\"><li>"); appendExpression(((BinaryExpression) expression).getLeftOperand(), writer); writer.append("</li><li>"); appendExpression(((BinaryExpression) expression).getRightOperand(), writer); writer.append("</li></ol>"); } else if (kind == ExpressionKind.METHOD) { final MethodExpression methodExpression = (MethodExpression) expression; if (methodExpression.getParameterCount() > 0) { writer.append("<ol class=\"expr\">"); for (final CommonExpression parameter : methodExpression.getParameters()) { writer.append("<li>"); appendExpression(parameter, writer); writer.append("</li>"); } writer.append("</ol>"); } } else if (kind == ExpressionKind.MEMBER) { writer.append("<ul class=\"expr\"><li>"); appendExpression(((MemberExpression) expression).getPath(), writer); writer.append("</li></ul>"); } }
Example #6
Source File: FilterParserImpl.java From olingo-odata2 with Apache License 2.0 | 5 votes |
protected void validateBinaryOperatorTypes(final BinaryExpression binaryExpression) throws ExpressionParserException, ExpressionParserInternalError { InfoBinaryOperator binOpt = availableBinaryOperators.get(binaryExpression.getOperator().toUriLiteral()); List<EdmType> actualParameterTypes = new ArrayList<EdmType>(); final EdmType leftType = binaryExpression.getLeftOperand().getEdmType(); if (leftType == null && resourceEntityType == null) { return; } actualParameterTypes.add(leftType); final EdmType rightType = binaryExpression.getRightOperand().getEdmType(); if (rightType == null && resourceEntityType == null) { return; } actualParameterTypes.add(rightType); // special case for navigation property (non-)equality comparison with null if ("Equality".equals(binOpt.getCategory()) && (leftType != null && leftType.getKind() == EdmTypeKind.ENTITY && rightType == EdmSimpleTypeFacadeImpl.getEdmSimpleType(EdmSimpleTypeKind.Null) || leftType == EdmSimpleTypeFacadeImpl.getEdmSimpleType(EdmSimpleTypeKind.Null) && rightType != null && rightType.getKind() == EdmTypeKind.ENTITY)) { binaryExpression.setEdmType(EdmSimpleTypeFacadeImpl.getEdmSimpleType(EdmSimpleTypeKind.Boolean)); return; } final ParameterSet parameterSet = binOpt.validateParameterSet(actualParameterTypes); if (parameterSet == null) { BinaryExpressionImpl binaryExpressionImpl = (BinaryExpressionImpl) binaryExpression; // Tested with TestParserExceptions.TestPMvalidateBinaryOperator throw FilterParserExceptionImpl.createINVALID_TYPES_FOR_BINARY_OPERATOR(binaryExpression.getOperator(), binaryExpression.getLeftOperand().getEdmType(), binaryExpression.getRightOperand().getEdmType(), binaryExpressionImpl.getToken(), curExpression); } binaryExpression.setEdmType(parameterSet.getReturnType()); }
Example #7
Source File: JPAQueryBuilderTest.java From olingo-odata2 with Apache License 2.0 | 4 votes |
private UriInfo mockURIInfoWithKeyNavSegAndFilter(EdmMapping mapping) throws EdmException { UriInfo uriInfo = EasyMock.createMock(UriInfo.class); EdmEntityType startEntityType = EasyMock.createMock(EdmEntityType.class); EasyMock.expect(startEntityType.getMapping()).andStubReturn(mapping); EdmEntitySet startEntitySet = EasyMock.createMock(EdmEntitySet.class); EasyMock.expect(startEntitySet.getEntityType()).andStubReturn(startEntityType); EasyMock.expect(uriInfo.getStartEntitySet()).andStubReturn(startEntitySet); EdmEntityType targetEntityType = EasyMock.createMock(EdmEntityType.class); EasyMock.expect(targetEntityType.getMapping()).andStubReturn((EdmMapping) mockNavEdmMappingForProperty()); EdmEntitySet targetEntitySet = EasyMock.createMock(EdmEntitySet.class); EasyMock.expect(targetEntitySet.getEntityType()).andStubReturn(targetEntityType); EasyMock.expect(uriInfo.getTargetEntitySet()).andStubReturn(targetEntitySet); List<KeyPredicate> keyPreds = new ArrayList<KeyPredicate>(); EdmProperty edmProperty = mockEdmProperty((EdmMapping) mockMappingWithType("uuid"), "uuid"); keyPreds.add(mockKeyPredicate(edmProperty, "56fe79b1-1c88-465b-b309-33bf8b8f6800")); EasyMock.expect(uriInfo.getKeyPredicates()).andStubReturn(keyPreds); List<NavigationSegment> navSegments = new ArrayList<NavigationSegment>(); EasyMock.expect(uriInfo.getNavigationSegments()).andStubReturn(navSegments); NavigationSegment navSegment = EasyMock.createMock(NavigationSegment.class); EasyMock.expect(navSegment.getEntitySet()).andStubReturn(targetEntitySet); List<KeyPredicate> navKeyPreds = new ArrayList<KeyPredicate>(); EasyMock.expect(navSegment.getKeyPredicates()).andStubReturn(navKeyPreds); EdmNavigationProperty navEdmProperty = EasyMock.createMock(EdmNavigationProperty.class); EasyMock.expect(navSegment.getNavigationProperty()).andStubReturn(navEdmProperty); EasyMock.expect(navEdmProperty.getMapping()).andStubReturn((EdmMapping)mockNavEdmMappingForProperty()); EasyMock.expect(navEdmProperty.getFromRole()).andStubReturn("Customers"); EasyMock.expect(navEdmProperty.getToRole()).andStubReturn("SalesOrderHeader"); EdmAssociation association = EasyMock.createMock(EdmAssociation.class); EasyMock.expect(navEdmProperty.getRelationship()).andStubReturn(association); EdmAssociationEnd associationEnd = EasyMock.createMock(EdmAssociationEnd.class); EasyMock.expect(associationEnd.getEntityType()).andStubReturn(startEntityType); EasyMock.expect(association.getEnd("Customers")).andStubReturn(associationEnd); navSegments.add(navSegment); FilterExpression filterExpression = EasyMock.createMock(FilterExpression.class); PropertyExpression propExp = EasyMock.createMock(PropertyExpression.class); LiteralExpression literalExp = EasyMock.createMock(LiteralExpression.class); EasyMock.expect(uriInfo.getFilter()).andStubReturn(filterExpression); BinaryExpression commonExpression = EasyMock.createMock(BinaryExpression.class); EasyMock.expect(commonExpression.getOperator()).andStubReturn(BinaryOperator.EQ); EasyMock.expect(commonExpression.getKind()).andStubReturn(ExpressionKind.BINARY); EasyMock.expect(filterExpression.getExpression()).andStubReturn(commonExpression); EasyMock.expect(filterExpression.getKind()).andStubReturn(ExpressionKind.FILTER); EasyMock.expect(filterExpression.getExpressionString()).andStubReturn( "Customer eq '56fe79b1-1c88-465b-b309-32bf8b8f7800'"); EasyMock.expect(commonExpression.getLeftOperand()).andStubReturn(propExp); EasyMock.expect(propExp.getEdmProperty()).andStubReturn(mockEdmProperty( (EdmMapping) mockMappingWithType("uuid"), "uuid")); EasyMock.expect(propExp.getKind()).andStubReturn(ExpressionKind.PROPERTY); EasyMock.expect(propExp.getEdmType()).andStubReturn(EdmSimpleTypeKind.Guid.getEdmSimpleTypeInstance()); EasyMock.expect(commonExpression.getRightOperand()).andStubReturn(literalExp); EasyMock.expect(literalExp.getUriLiteral()).andStubReturn("guid'56fe79b1-1c88-465b-b309-32bf8b8f7800'"); EasyMock.expect(literalExp.getKind()).andStubReturn(ExpressionKind.LITERAL); EasyMock.expect(literalExp.getEdmType()).andStubReturn(EdmSimpleTypeKind.Guid.getEdmSimpleTypeInstance()); EasyMock.expect(uriInfo.getOrderBy()).andStubReturn(null); EasyMock.expect(uriInfo.getTop()).andStubReturn(null); EasyMock.expect(uriInfo.getSkip()).andStubReturn(null); EasyMock.replay(startEntityType, targetEntityType, startEntitySet, targetEntitySet, uriInfo, filterExpression, propExp, literalExp, navSegment, navEdmProperty, commonExpression, association, associationEnd); return uriInfo; }
Example #8
Source File: JPAQueryBuilderTest.java From olingo-odata2 with Apache License 2.0 | 4 votes |
private UriInfo mockURIInfoForEntitySetWithBinaryFilterExpression (EdmMapping mapping, String methodName) throws EdmException { UriInfo uriInfo = EasyMock.createMock(UriInfo.class); List<NavigationSegment> navSegments = new ArrayList<NavigationSegment>(); EasyMock.expect(uriInfo.getNavigationSegments()).andStubReturn(navSegments); EdmEntityType edmEntityType = EasyMock.createMock(EdmEntityType.class); EasyMock.expect(edmEntityType.getMapping()).andStubReturn(mapping); EdmEntitySet edmEntitySet = EasyMock.createMock(EdmEntitySet.class); EasyMock.expect(edmEntitySet.getEntityType()).andStubReturn(edmEntityType); EasyMock.expect(uriInfo.getTargetEntitySet()).andStubReturn(edmEntitySet); EdmProperty edmProperty = EasyMock.createMock(EdmProperty.class); EasyMock.expect(edmProperty.getMapping()).andStubReturn((EdmMapping)mockEdmMappingForProperty()); OrderByExpression orderbyExpression = EasyMock.createMock(OrderByExpression.class); List<OrderExpression> orders = new ArrayList<OrderExpression>(); EasyMock.expect(orderbyExpression.getOrders()).andStubReturn(orders); EasyMock.expect(uriInfo.getOrderBy()).andStubReturn(orderbyExpression); FilterExpression filterExpression = EasyMock.createMock(FilterExpression.class); BinaryExpression commonExpression = EasyMock.createMock(BinaryExpression.class); EasyMock.expect(commonExpression.getOperator()).andStubReturn(BinaryOperator.EQ); EasyMock.expect(commonExpression.getKind()).andStubReturn(ExpressionKind.BINARY); MethodExpression methodExp = EasyMock.createMock(MethodExpression.class); EasyMock.expect(commonExpression.getLeftOperand()).andStubReturn(methodExp); EdmSimpleType type = EasyMock.createMock(EdmSimpleType.class);; EasyMock.expect(methodExp.getEdmType()).andStubReturn(type ); LiteralExpression literalValueExp = EasyMock.createMock(LiteralExpression.class); EasyMock.expect(commonExpression.getRightOperand()).andStubReturn(literalValueExp); EasyMock.expect(literalValueExp.getUriLiteral()).andStubReturn("'a%.b*.c'"); EasyMock.expect(literalValueExp.getKind()).andStubReturn(ExpressionKind.LITERAL); EasyMock.expect(literalValueExp.getEdmType()).andStubReturn(EdmSimpleTypeKind.String.getEdmSimpleTypeInstance()); PropertyExpression propExp = EasyMock.createMock(PropertyExpression.class); LiteralExpression literalExp1 = EasyMock.createMock(LiteralExpression.class); LiteralExpression literalExp2 = EasyMock.createMock(LiteralExpression.class); List<CommonExpression> parameterList = new ArrayList<CommonExpression>(); if ("substring".equals(methodName)) { EasyMock.expect(methodExp.getMethod()).andStubReturn(MethodOperator.SUBSTRING); EasyMock.expect(methodExp.getKind()).andStubReturn(ExpressionKind.METHOD); EasyMock.expect(methodExp.getParameterCount()).andStubReturn(3); EasyMock.expect(propExp.getEdmProperty()).andStubReturn(edmProperty); EasyMock.expect(propExp.getKind()).andStubReturn(ExpressionKind.PROPERTY); parameterList.add(propExp); EasyMock.expect(literalExp1.getUriLiteral()).andStubReturn("1"); EasyMock.expect(literalExp1.getKind()).andStubReturn(ExpressionKind.LITERAL); EasyMock.expect(literalExp1.getEdmType()).andStubReturn(EdmSimpleTypeKind.Int16.getEdmSimpleTypeInstance()); parameterList.add(literalExp1); EasyMock.expect(literalExp2.getUriLiteral()).andStubReturn("2"); EasyMock.expect(literalExp2.getKind()).andStubReturn(ExpressionKind.LITERAL); EasyMock.expect(literalExp2.getEdmType()).andStubReturn(EdmSimpleTypeKind.Int16.getEdmSimpleTypeInstance()); parameterList.add(literalExp2); EasyMock.expect(methodExp.getParameters()).andStubReturn(parameterList); } else if ("toLower".equals(methodName)) { EasyMock.expect(methodExp.getMethod()).andStubReturn(MethodOperator.TOLOWER); EasyMock.expect(methodExp.getKind()).andStubReturn(ExpressionKind.METHOD); EasyMock.expect(methodExp.getParameterCount()).andStubReturn(1); EasyMock.expect(propExp.getEdmProperty()).andStubReturn(edmProperty); EasyMock.expect(propExp.getKind()).andStubReturn(ExpressionKind.PROPERTY); parameterList.add(propExp); EasyMock.expect(methodExp.getParameters()).andStubReturn(parameterList); } else if ("toUpper".equals(methodName)) { EasyMock.expect(methodExp.getMethod()).andStubReturn(MethodOperator.TOUPPER); EasyMock.expect(methodExp.getKind()).andStubReturn(ExpressionKind.METHOD); EasyMock.expect(methodExp.getParameterCount()).andStubReturn(1); EasyMock.expect(propExp.getEdmProperty()).andStubReturn(edmProperty); EasyMock.expect(propExp.getKind()).andStubReturn(ExpressionKind.PROPERTY); parameterList.add(propExp); EasyMock.expect(methodExp.getParameters()).andStubReturn(parameterList); } EasyMock.expect(filterExpression.getExpression()).andStubReturn(commonExpression); EasyMock.expect(filterExpression.getKind()).andStubReturn(ExpressionKind.FILTER); EasyMock.expect(filterExpression.getExpressionString()).andStubReturn("substring(CompanyName,1,2) eq 'a.b.c'"); EasyMock.expect(uriInfo.getFilter()).andStubReturn(filterExpression); EasyMock.replay(edmEntityType, edmEntitySet, orderbyExpression, filterExpression, commonExpression, literalExp1, literalExp2, propExp, uriInfo, edmProperty, methodExp, literalValueExp); return uriInfo; }
Example #9
Source File: FilterParserImpl.java From olingo-odata2 with Apache License 2.0 | 4 votes |
protected CommonExpression readElements(final CommonExpression leftExpression, final int priority) throws ExpressionParserException, ExpressionParserInternalError { CommonExpression leftNode = leftExpression; CommonExpression rightNode; BinaryExpression binaryNode; ActualBinaryOperator operator = readBinaryOperator(); ActualBinaryOperator nextOperator; while ((operator != null) && (operator.getOP().getPriority() >= priority)) { tokenList.next(); // eat the operator rightNode = readElement(leftNode, operator); // throws FilterParserException, FilterParserInternalError if (rightNode == null) { // Tested with TestParserExceptions.testAdditionalStuff CASE 10 throw FilterParserExceptionImpl.createEXPRESSION_EXPECTED_AFTER_POS(operator.getToken().getPosition() + operator.getToken().getUriLiteral().length(), curExpression); } nextOperator = readBinaryOperator(); // It must be "while" because for example in "Filter=a or c eq d and e eq f" // after reading the "eq" operator the "and" operator must be consumed too. This is due to the fact that "and" has // a higher priority than "or" while ((nextOperator != null) && (nextOperator.getOP().getPriority() > operator.getOP().getPriority())) { // recurse until the a binary operator with a lower priority is detected rightNode = readElements(rightNode, nextOperator.getOP().getPriority()); nextOperator = readBinaryOperator(); } // Although the member operator is also a binary operator, there is some special handling in the filterTree if (operator.getOP().getOperator() == BinaryOperator.PROPERTY_ACCESS) { binaryNode = new MemberExpressionImpl(leftNode, rightNode); } else { binaryNode = new BinaryExpressionImpl(operator.getOP(), leftNode, rightNode, operator.getToken()); } try { validateBinaryOperatorTypes(binaryNode); } catch (ExpressionParserException expressionException) { // Extend the error information // Tested for original throw point expressionException.setFilterTree(binaryNode); throw expressionException; } leftNode = binaryNode; operator = readBinaryOperator(); } // Add special handling for expressions like $filter=notsupportedfunction('a') // If this special handling is not in place the error text would be // -->Invalid token "(" detected after parsing at position 21 in "notsupportedfunction('a')". // with this special handling we ensure that the error text would be Token token = tokenList.lookToken(); if (token != null) { if ((leftNode.getKind() == ExpressionKind.PROPERTY) && (tokenList.lookToken().getKind() == TokenKind.OPENPAREN)) { // Tested with TestParserExceptions.testAdditionalStuff CASE 2 throw FilterParserExceptionImpl.createINVALID_METHOD_CALL(leftNode, tokenList.lookPrevToken(), curExpression); } } return leftNode; }
Example #10
Source File: VisitorTool.java From olingo-odata2 with Apache License 2.0 | 4 votes |
@Override public Object visitBinary(final BinaryExpression binaryExpression, final BinaryOperator operator, final Object leftSide, final Object rightSide) { return "{" + leftSide.toString() + " " + operator.toUriLiteral() + " " + rightSide.toString() + "}"; }
Example #11
Source File: ODataParser.java From cxf with Apache License 2.0 | 4 votes |
@Override @SuppressWarnings("unchecked") public Object visitBinary(BinaryExpression binaryExpression, BinaryOperator operator, Object leftSide, Object rightSide) { // AND / OR operate on search conditions if (operator == BinaryOperator.AND || operator == BinaryOperator.OR) { if (leftSide instanceof SearchCondition && rightSide instanceof SearchCondition) { final List< SearchCondition< T > > conditions = new ArrayList<>(2); conditions.add((SearchCondition< T >)leftSide); conditions.add((SearchCondition< T >)rightSide); if (operator == BinaryOperator.AND) { return new AndSearchCondition< T >(conditions); } else if (operator == BinaryOperator.OR) { return new OrSearchCondition< T >(conditions); } } else { throw new SearchParseException( "Unsupported binary operation arguments (SearchCondition expected): " + leftSide + ", " + rightSide); } } // Property could be either on left side (Name eq 'Tom') or // right side ('Tom' eq Name) TypedValue value = null; TypedProperty property = null; if (leftSide instanceof TypedProperty && rightSide instanceof TypedValue) { property = (TypedProperty)leftSide; value = (TypedValue)rightSide; } else if (rightSide instanceof TypedProperty && leftSide instanceof TypedValue) { property = (TypedProperty)rightSide; value = (TypedValue)leftSide; } else { throw new SearchParseException( "Unsupported binary operation arguments (TypedValue or TypedProperty expected): " + leftSide + ", " + rightSide); } ConditionType conditionType = null; switch (operator) { case EQ: conditionType = ConditionType.EQUALS; break; case NE: conditionType = ConditionType.NOT_EQUALS; break; case LT: conditionType = ConditionType.LESS_THAN; break; case LE: conditionType = ConditionType.LESS_OR_EQUALS; break; case GT: conditionType = ConditionType.GREATER_THAN; break; case GE: conditionType = ConditionType.GREATER_OR_EQUALS; break; default: throw new SearchParseException("Unsupported binary operation: " + operator); } Object typedValue = null; // If property type and value type are compatible, just use them if (property.typeInfo.getWrappedTypeClass().isAssignableFrom(value.typeClass)) { typedValue = value.value; } else { // Property type and value type are not compatible and convert / cast are required String valueStr = value.literal; if (isDecodeQueryValues()) { valueStr = UrlUtils.urlDecode(valueStr); } typedValue = parseType(property.propertyName, null, null, property.propertyName, property.typeInfo, valueStr); } final CollectionCheckInfo checkInfo = property.typeInfo.getCollectionCheckInfo(); if (checkInfo != null) { return new CollectionCheckCondition< T >(property.propertyName, typedValue, property.typeInfo.getGenericType(), conditionType, condition, checkInfo); } return new PrimitiveSearchCondition< T >(property.propertyName, typedValue, property.typeInfo.getGenericType(), conditionType, condition); }