cz.jirutka.rsql.parser.ast.ComparisonOperator Java Examples

The following examples show how to use cz.jirutka.rsql.parser.ast.ComparisonOperator. 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: SearchFilterToQueryConverter.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
private SearchQuery createRangeQuery(Class<?> cls, String name, Object value, ComparisonOperator type) {

    boolean minInclusive = type == RSQLOperators.GREATER_THAN_OR_EQUAL || type == RSQLOperators.EQUAL;
    boolean maxInclusive = type == RSQLOperators.LESS_THAN_OR_EQUAL || type == RSQLOperators.EQUAL;

    if (Number.class.isAssignableFrom(cls)) {
      if (Double.class.isAssignableFrom(cls)) {
        return createDoubleRangeQuery(name, value, type, minInclusive, maxInclusive);
      } else if (Float.class.isAssignableFrom(cls)) {
        return createFloatRangeQuery(name, value, type, minInclusive, maxInclusive);
      } else if (Long.class.isAssignableFrom(cls)) {
        return createLongRangeQuery(name, value, type, minInclusive, maxInclusive);
      } else {
        return createIntRangeQuery(name, value, type, minInclusive, maxInclusive);
      }
    } else if (String.class.isAssignableFrom(cls)) {
      return createStringRangeQuery(name, value, type, minInclusive, maxInclusive);
    } else {
      throw new IllegalArgumentException(format("%s: Can not do range query on field %s of type %s" +
        "only long, int, double, float types are supported", visitorName, name, cls.getName()));
    }
  }
 
Example #2
Source File: QueryRsqlVisitorTest.java    From molgenis with GNU Lesser General Public License v3.0 6 votes vote down vote up
@ParameterizedTest
@MethodSource("testVisitComparisonNodeLessThanOrEqualProvider")
void testVisitComparisonNodeLessThanOrEqual(String symbol) {
  String selector = "age";
  String argument = "87";
  ComparisonOperator operator = new ComparisonOperator(symbol);
  ComparisonNode node = new ComparisonNode(operator, selector, singletonList(argument));

  Query query =
      Query.builder()
          .setItem(selector)
          .setOperator(LESS_THAN_OR_EQUAL_TO)
          .setValue(argument)
          .build();
  assertEquals(query, queryRsqlVisitor.visit(node));
}
 
Example #3
Source File: QueryRsqlVisitorTest.java    From molgenis with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Test
void testVisitAndNode() {
  String argument0 = "piet";
  ComparisonOperator operator0 = new ComparisonOperator("=q=");
  ComparisonNode node0 = new ComparisonNode(operator0, "*", singletonList(argument0));

  String argument1 = "jan";
  ComparisonOperator operator1 = new ComparisonOperator("=q=");
  ComparisonNode node1 = new ComparisonNode(operator1, "*", singletonList(argument1));

  AndNode andNode = new AndNode(asList(node0, node1));
  Query query =
      Query.builder()
          .setOperator(AND)
          .setValue(
              asList(
                  Query.builder().setOperator(MATCHES).setValue(argument0).build(),
                  Query.builder().setOperator(MATCHES).setValue(argument1).build()))
          .build();
  assertEquals(query, queryRsqlVisitor.visit(andNode));
}
 
Example #4
Source File: QueryRsqlVisitorTest.java    From molgenis with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Test
void testVisitOrNode() {
  String argument0 = "piet";
  ComparisonOperator operator0 = new ComparisonOperator("=q=");
  ComparisonNode node0 = new ComparisonNode(operator0, "*", singletonList(argument0));

  String argument1 = "jan";
  ComparisonOperator operator1 = new ComparisonOperator("=q=");
  ComparisonNode node1 = new ComparisonNode(operator1, "*", singletonList(argument1));

  OrNode orNode = new OrNode(asList(node0, node1));
  Query query =
      Query.builder()
          .setOperator(OR)
          .setValue(
              asList(
                  Query.builder().setOperator(MATCHES).setValue(argument0).build(),
                  Query.builder().setOperator(MATCHES).setValue(argument1).build()))
          .build();
  assertEquals(query, queryRsqlVisitor.visit(orNode));
}
 
Example #5
Source File: QueryRsqlVisitorTest.java    From molgenis with GNU Lesser General Public License v3.0 6 votes vote down vote up
@ParameterizedTest
@MethodSource("testVisitComparisonNodeGreaterThanOrEqualProvider")
void testVisitComparisonNodeGreaterThanOrEqual(String symbol) {
  String selector = "age";
  String argument = "87";
  ComparisonOperator operator = new ComparisonOperator(symbol);
  ComparisonNode node = new ComparisonNode(operator, selector, singletonList(argument));

  Query query =
      Query.builder()
          .setItem(selector)
          .setOperator(GREATER_THAN_OR_EQUAL_TO)
          .setValue(argument)
          .build();
  assertEquals(query, queryRsqlVisitor.visit(node));
}
 
Example #6
Source File: QueryRsqlVisitorTest.java    From molgenis with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
void testVisitComparisonNodeSearchQuery() {
  String selector = "name";
  String argument = "piet";
  ComparisonOperator operator = new ComparisonOperator("=sq=");
  ComparisonNode node = new ComparisonNode(operator, selector, singletonList(argument));

  Query query =
      Query.builder().setItem(selector).setOperator(SEARCH_QUERY).setValue(argument).build();
  assertEquals(query, queryRsqlVisitor.visit(node));
}
 
Example #7
Source File: BasicTypeFilterVisitor.java    From gemini with Apache License 2.0 5 votes vote down vote up
private QueryWithParams handleSingleArgumentOperator(EntityField field, ComparisonOperator operator, List<String> arguments, FilterVisitorContext filterVisitorContext) {
    String argument = arguments.get(0);// always true
    String stOperator = singleArgumentOpertors.get(operator);
    if (stOperator == null)
        return null;
    Object resArgument = handleStringValueForField(field, argument);
    String parameterName = filterVisitorContext.parameterFor(fieldName(field, false));
    String sql = String.format(" \"%s\".\"%s\" %s :%s ", field.getEntity().getName().toLowerCase(), fieldName(field, false), stOperator, parameterName);
    return new QueryWithParams(sql, Map.of(parameterName, resArgument));
}
 
Example #8
Source File: QueryRsqlVisitorTest.java    From molgenis with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
void testVisitComparisonNodeSearchQueryNull() {
  String selector = "name";
  String argument = "";
  ComparisonOperator operator = new ComparisonOperator("=sq=");
  ComparisonNode node = new ComparisonNode(operator, selector, singletonList(argument));

  assertThrows(RuntimeException.class, () -> queryRsqlVisitor.visit(node));
}
 
Example #9
Source File: QueryRsqlVisitorTest.java    From molgenis with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
void testVisitComparisonNodeSearchQueryAllSelector() {
  String selector = "*";
  String argument = "piet";
  ComparisonOperator operator = new ComparisonOperator("=sq=");
  ComparisonNode node = new ComparisonNode(operator, selector, singletonList(argument));

  Query query =
      Query.builder().setItem(null).setOperator(SEARCH_QUERY).setValue(argument).build();
  assertEquals(query, queryRsqlVisitor.visit(node));
}
 
Example #10
Source File: QueryRsqlVisitorTest.java    From molgenis with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
void testVisitComparisonNodeContains() {
  String selector = "name";
  String argument = "piet";
  ComparisonOperator operator = new ComparisonOperator("=like=");
  ComparisonNode node = new ComparisonNode(operator, selector, singletonList(argument));

  Query query =
      Query.builder().setItem(selector).setOperator(CONTAINS).setValue(argument).build();
  assertEquals(query, queryRsqlVisitor.visit(node));
}
 
Example #11
Source File: QueryRsqlVisitorTest.java    From molgenis with GNU Lesser General Public License v3.0 5 votes vote down vote up
@ParameterizedTest
@MethodSource("testVisitComparisonNodeLessThanProvider")
void testVisitComparisonNodeLessThan(String symbol) {
  String selector = "age";
  String argument = "87";
  ComparisonOperator operator = new ComparisonOperator(symbol);
  ComparisonNode node = new ComparisonNode(operator, selector, singletonList(argument));

  Query query =
      Query.builder().setItem(selector).setOperator(LESS_THAN).setValue(argument).build();
  assertEquals(query, queryRsqlVisitor.visit(node));
}
 
Example #12
Source File: QueryRsqlVisitorTest.java    From molgenis with GNU Lesser General Public License v3.0 5 votes vote down vote up
@ParameterizedTest
@MethodSource("testVisitComparisonNodeGreaterThanProvider")
void testVisitComparisonNodeGreaterThan(String symbol) {
  String selector = "age";
  String argument = "87";
  ComparisonOperator operator = new ComparisonOperator(symbol);
  ComparisonNode node = new ComparisonNode(operator, selector, singletonList(argument));

  Query query =
      Query.builder().setItem(selector).setOperator(GREATER_THAN).setValue(argument).build();
  assertEquals(query, queryRsqlVisitor.visit(node));
}
 
Example #13
Source File: QueryRsqlVisitorTest.java    From molgenis with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
void testVisitComparisonNodeIllegalOperator() {
  String selector = "name";
  String argument = "piet";
  ComparisonOperator operator = new ComparisonOperator("=illegal=");
  ComparisonNode node = new ComparisonNode(operator, selector, singletonList(argument));

  assertThrows(RuntimeException.class, () -> queryRsqlVisitor.visit(node));
}
 
Example #14
Source File: QueryConverterTest.java    From molgenis with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
void testCreateQuery() {
  String rsqlQuery = "item==value";
  ComparisonNode node =
      new ComparisonNode(new ComparisonOperator("=="), "item", singletonList("value"));
  Query query = Query.builder().setItem("item").setOperator(Operator.EQUALS).build();
  when(node.accept(rsqlVisitor)).thenReturn(query);
  assertEquals(query, queryConverter.convert(rsqlQuery));
  verify(rsqlVisitor).visit(node, null);
}
 
Example #15
Source File: PermissionRsqlVisitorTest.java    From molgenis with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
void testVisit() {
  ComparisonNode comparisonNode =
      new ComparisonNode(
          new ComparisonOperator("=="), "user", Collections.singletonList("user1"));
  assertEquals(new PermissionsQuery(asList("user1"), emptyList()), visitor.visit(comparisonNode));
}
 
Example #16
Source File: PermissionRsqlVisitorTest.java    From molgenis with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
void testVisit2() {
  ComparisonNode comparisonNode =
      new ComparisonNode(
          new ComparisonOperator("=in=", true), "role", Arrays.asList("role1", "role2"));
  assertEquals(
      new PermissionsQuery(emptyList(), asList("role1", "role2")), visitor.visit(comparisonNode));
}
 
Example #17
Source File: PermissionRsqlVisitorTest.java    From molgenis with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
void testVisitOr() {
  ComparisonNode comparisonNodeUser =
      new ComparisonNode(
          new ComparisonOperator("=="), "user", Collections.singletonList("user1"));
  ComparisonNode comparisonNodeRole =
      new ComparisonNode(
          new ComparisonOperator("=in=", true), "role", Arrays.asList("role1", "role2"));
  OrNode orNode = new OrNode(Arrays.asList(comparisonNodeUser, comparisonNodeRole));
  assertEquals(
      new PermissionsQuery(asList("user1"), asList("role1", "role2")), visitor.visit(orNode));
}
 
Example #18
Source File: RsqlConfig.java    From molgenis with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Bean
public RSQLParser rsqlParser() {
  Set<ComparisonOperator> operators = RSQLOperators.defaultOperators();
  operators.add(new ComparisonOperator("=q=", false));
  operators.add(new ComparisonOperator("=sq=", false));
  operators.add(new ComparisonOperator("=notlike=", false));
  operators.add(new ComparisonOperator("=rng=", true));
  operators.add(new ComparisonOperator("=like=", false));
  return new RSQLParser(operators);
}
 
Example #19
Source File: MolgenisRSQLVisitorTest.java    From molgenis with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
void testVisitComparisonNodeFieldSearchQuery() {
  ComparisonOperator searchQuery = new ComparisonOperator("=sq=");
  ComparisonNode node = new ComparisonNode(searchQuery, "version", List.of("8.3.*"));

  Query actual = visitor.visit(node);

  Query expected = new QueryImpl().searchQuery("version", "8.3.*");
  assertEquals(expected, actual);
}
 
Example #20
Source File: MolgenisRSQLVisitorTest.java    From molgenis with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
void testVisitComparisonNodeAllFieldsSearchQuery() {
  ComparisonOperator searchQuery = new ComparisonOperator("=sq=");
  ComparisonNode node = new ComparisonNode(searchQuery, "*", List.of("8.3.*"));

  Query actual = visitor.visit(node);

  Query expected = new QueryImpl().searchQuery("8.3.*");
  assertEquals(expected, actual);
}
 
Example #21
Source File: QueryRsqlVisitorTest.java    From molgenis with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
void testVisitComparisonNodeMatchesNull() {
  String selector = "name";
  String argument = "";
  ComparisonOperator operator = new ComparisonOperator("=q=");
  ComparisonNode node = new ComparisonNode(operator, selector, singletonList(argument));

  assertThrows(RuntimeException.class, () -> queryRsqlVisitor.visit(node));
}
 
Example #22
Source File: BasicTypeFilterVisitor.java    From gemini with Apache License 2.0 5 votes vote down vote up
public QueryWithParams visit(EntityField field, ComparisonNode node, FilterVisitorContext filterVisitorContext) {
    ComparisonOperator operator = node.getOperator();
    List<String> arguments = node.getArguments();
    QueryWithParams singleArg = handleSingleArgumentOperator(field, operator, arguments, filterVisitorContext);
    if (singleArg != null)
        return singleArg;
    QueryWithParams multipleArg = handleMultipleArgumentOperator(field, operator, arguments, filterVisitorContext);
    if (multipleArg == null) {
        throw new GeminiRuntimeException(String.format("Filter not impemented for type %s", field.getType()));
    }
    return multipleArg;
}
 
Example #23
Source File: BasicTypeFilterVisitor.java    From gemini with Apache License 2.0 5 votes vote down vote up
private QueryWithParams handleMultipleArgumentOperator(EntityField field, ComparisonOperator operator, List<String> arguments, FilterVisitorContext filterVisitorContext) {
    String stOperator = multipleArgumentsOperators.get(operator);
    if (stOperator == null) {
        return null;
    }
    List<Object> parameters = arguments.stream().map(a -> handleStringValueForField(field, a)).collect(Collectors.toList());
    String parameterName = filterVisitorContext.parameterFor(fieldName(field, false));

    String sql = String.format(" \"%s\".\"%s\" %s (:%s) ", field.getEntity().getName().toLowerCase(), fieldName(field, false), stOperator, parameterName);
    return new QueryWithParams(sql, Map.of(parameterName, parameters));
}
 
Example #24
Source File: RSQLComplexConverter.java    From rsql-jpa-specification with MIT License 5 votes vote down vote up
@Override
public Void visit(ComparisonNode node, Map<String, MultiValueMap<String, String>> map) {
	log.debug("visit(node:{},map:{})", node, map);
	String key = node.getSelector();
	ComparisonOperator operator = node.getOperator();
	MultiValueMap<String, String> operatorMap = map.computeIfAbsent(key, k -> CollectionUtils.toMultiValueMap(new HashMap<>()));
	for (String ops : operator.getSymbols()) {
		operatorMap.addAll(ops, node.getArguments());
	}
	return null;
}
 
Example #25
Source File: SearchFilterToQueryConverter.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
private SearchQuery createIntRangeQuery(final String name, final Object value,
                                  final ComparisonOperator type, final boolean minInclusive, final boolean maxInclusive) {
  final Integer intValue = (Integer) value;

  final Integer minValue = (type != RSQLOperators.LESS_THAN && type != RSQLOperators.LESS_THAN_OR_EQUAL)
      ? intValue : null;
  final Integer maxValue = (type != RSQLOperators.GREATER_THAN && type != RSQLOperators.GREATER_THAN_OR_EQUAL)
      ? intValue : null;

  return SearchQueryUtils.newRangeInt(name, minValue, maxValue, minInclusive, maxInclusive);
}
 
Example #26
Source File: SearchFilterToQueryConverter.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
private SearchQuery createLongRangeQuery(final String name, final Object value,
                                   final ComparisonOperator type, final boolean minInclusive, final boolean maxInclusive) {
  final Long longValue = (Long) value;

  final Long minValue = (type != RSQLOperators.LESS_THAN && type != RSQLOperators.LESS_THAN_OR_EQUAL)
      ? longValue : null;
  final Long maxValue = (type != RSQLOperators.GREATER_THAN && type != RSQLOperators.GREATER_THAN_OR_EQUAL)
      ? longValue : null;

  return SearchQueryUtils.newRangeLong(name, minValue, maxValue, minInclusive, maxInclusive);
}
 
Example #27
Source File: SearchFilterToQueryConverter.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
private SearchQuery createDoubleRangeQuery(final String name, final Object value,
                                     final ComparisonOperator type, final boolean minInclusive, final boolean maxInclusive) {
  final Double doubleValue = (Double) value;

  final Double minValue = (type != RSQLOperators.LESS_THAN && type != RSQLOperators.LESS_THAN_OR_EQUAL)
      ? doubleValue : null;
  final Double maxValue = (type != RSQLOperators.GREATER_THAN && type != RSQLOperators.GREATER_THAN_OR_EQUAL)
      ? doubleValue : null;

  return SearchQueryUtils.newRangeDouble(name, minValue, maxValue, minInclusive, maxInclusive);
}
 
Example #28
Source File: SearchFilterToQueryConverter.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
private SearchQuery createFloatRangeQuery(final String name, final Object value,
                                    final ComparisonOperator type, final boolean minInclusive, final boolean maxInclusive) {
  final Float floatValue = (Float) value;

  final Float minValue = (type != RSQLOperators.LESS_THAN && type != RSQLOperators.LESS_THAN_OR_EQUAL)
      ? floatValue : null;
  final Float maxValue = (type != RSQLOperators.GREATER_THAN && type != RSQLOperators.GREATER_THAN_OR_EQUAL)
      ? floatValue : null;

  return SearchQueryUtils.newRangeFloat(name, minValue, maxValue, minInclusive, maxInclusive);
}
 
Example #29
Source File: SearchFilterToQueryConverter.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
private SearchQuery createStringRangeQuery(final String name, final Object value,
                                     final ComparisonOperator type, final boolean minInclusive, final boolean maxInclusive) {
  final String stringValue = (String)value;

  final String minValue = (type != RSQLOperators.LESS_THAN && type != RSQLOperators.LESS_THAN_OR_EQUAL)
      ? stringValue : null;
  final String maxValue = (type != RSQLOperators.GREATER_THAN && type != RSQLOperators.GREATER_THAN_OR_EQUAL)
      ? stringValue : null;

  return SearchQueryUtils.newRangeTerm(name, minValue, maxValue, minInclusive, maxInclusive);
}
 
Example #30
Source File: RSQLProducerImpl.java    From pnc with Apache License 2.0 5 votes vote down vote up
public RSQLProducerImpl() {
    Set<ComparisonOperator> predicateOperators = RSQLOperators.defaultOperators();
    predicateOperators.add(LIKE);
    predicateOperators.add(NOT_LIKE);
    predicateOperators.add(IS_NULL);

    predicateParser = new RSQLParser(predicateOperators);

    Set<ComparisonOperator> sortOperators = new HashSet<>();
    sortOperators.add(ASC);
    sortOperators.add(DESC);

    sortParser = new RSQLParser(sortOperators);
}