@Override public FilterExpression visitNOT(ExpressionParser.NOTContext ctx) { FilterExpression expression = visit(ctx.expression()); if (Objects.equals(expression, TRUE_USER_CHECK_EXPRESSION)) { return FALSE_USER_CHECK_EXPRESSION; } else if (Objects.equals(expression, FALSE_USER_CHECK_EXPRESSION)) { return TRUE_USER_CHECK_EXPRESSION; } else if (Objects.equals(expression, NO_EVALUATION_EXPRESSION)) { return NO_EVALUATION_EXPRESSION; } return new NotFilterExpression(expression); }
@Override public FilterExpression visitNOT(ExpressionParser.NOTContext ctx) { FilterExpression expression = visit(ctx.expression()); if (Objects.equals(expression, TRUE_USER_CHECK_EXPRESSION)) { return FALSE_USER_CHECK_EXPRESSION; } else if (Objects.equals(expression, FALSE_USER_CHECK_EXPRESSION)) { return TRUE_USER_CHECK_EXPRESSION; } else if (Objects.equals(expression, NO_EVALUATION_EXPRESSION)) { return NO_EVALUATION_EXPRESSION; } return new NotFilterExpression(expression); }
@Override public FilterExpression visitNotExpression(NotFilterExpression expression) { return new NotFilterExpression(expression.getNegated().accept(this)); } }
@Override public FilterExpression visitNotExpression(NotFilterExpression expression) { return new NotFilterExpression(expression.getNegated().accept(this)); } }
@Override public FilterExpression visitNotExpression(NotFilterExpression fe) { FilterExpression inner = fe.getNegated(); if (inner instanceof AndFilterExpression) { AndFilterExpression and = (AndFilterExpression) inner; FilterExpression left = new NotFilterExpression(and.getLeft()).accept(this); FilterExpression right = new NotFilterExpression(and.getRight()).accept(this); return new OrFilterExpression(left, right); } if (inner instanceof OrFilterExpression) { OrFilterExpression or = (OrFilterExpression) inner; FilterExpression left = new NotFilterExpression(or.getLeft()).accept(this); FilterExpression right = new NotFilterExpression(or.getRight()).accept(this); return new AndFilterExpression(left, right); } if (inner instanceof NotFilterExpression) { NotFilterExpression not = (NotFilterExpression) inner; return (not.getNegated()).accept(this); } if (inner instanceof FilterPredicate) { FilterPredicate filter = (FilterPredicate) inner; return filter.negate(); } return inner; } }
return new NotFilterExpression(equalityExpression(arguments.get(0), path, values));
@Override public FilterExpression visitNotExpression(NotFilterExpression fe) { FilterExpression inner = fe.getNegated(); if (inner instanceof AndFilterExpression) { AndFilterExpression and = (AndFilterExpression) inner; FilterExpression left = new NotFilterExpression(and.getLeft()).accept(this); FilterExpression right = new NotFilterExpression(and.getRight()).accept(this); return new OrFilterExpression(left, right); } if (inner instanceof OrFilterExpression) { OrFilterExpression or = (OrFilterExpression) inner; FilterExpression left = new NotFilterExpression(or.getLeft()).accept(this); FilterExpression right = new NotFilterExpression(or.getRight()).accept(this); return new AndFilterExpression(left, right); } if (inner instanceof NotFilterExpression) { NotFilterExpression not = (NotFilterExpression) inner; return (not.getNegated()).accept(this); } if (inner instanceof FilterPredicate) { FilterPredicate filter = (FilterPredicate) inner; return filter.negate(); } return inner; } }
return new NotFilterExpression(equalityExpression(arguments.get(0), path, values));
@Test public void testHQLQueryVisitor() throws Exception { List<Path.PathElement> p1Path = Arrays.asList( new Path.PathElement(Book.class, Author.class, "authors"), new Path.PathElement(Author.class, String.class, "name") ); FilterPredicate p1 = new InPredicate(new Path(p1Path), "foo", "bar"); List<Path.PathElement> p2Path = Arrays.asList( new Path.PathElement(Book.class, String.class, "name") ); FilterPredicate p2 = new InPredicate(new Path(p2Path), "blah"); List<Path.PathElement> p3Path = Arrays.asList( new Path.PathElement(Book.class, String.class, "genre") ); FilterPredicate p3 = new InPredicate(new Path(p3Path), "scifi"); OrFilterExpression or = new OrFilterExpression(p2, p3); AndFilterExpression and = new AndFilterExpression(or, p1); NotFilterExpression not = new NotFilterExpression(and); HQLFilterOperation filterOp = new HQLFilterOperation(); String query = filterOp.apply(not, false); String p1Params = p1.getParameters().stream() .map(FilterPredicate.FilterParameter::getPlaceholder).collect(Collectors.joining(", ")); String p2Params = p2.getParameters().stream() .map(FilterPredicate.FilterParameter::getPlaceholder).collect(Collectors.joining(", ")); String p3Params = p3.getParameters().stream() .map(FilterPredicate.FilterParameter::getPlaceholder).collect(Collectors.joining(", ")); String expected = "WHERE NOT (((name IN (" + p2Params + ") OR genre IN (" + p3Params + ")) " + "AND authors.name IN (" + p1Params + ")))"; Assert.assertEquals(query, expected); }