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

The following examples show how to use cz.jirutka.rsql.parser.ast.ComparisonOperator. These examples are extracted from open source projects. 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 Project: dremio-oss   Source File: SearchFilterToQueryConverter.java    License: 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
@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
@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 4
@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
@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 6
Source Project: gemini   Source File: BasicTypeFilterVisitor.java    License: 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 7
Source Project: gemini   Source File: BasicTypeFilterVisitor.java    License: 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 Project: gemini   Source File: BasicTypeFilterVisitor.java    License: 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 9
Source Project: rsql-jpa-specification   Source File: RSQLComplexConverter.java    License: 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 10
Source Project: dremio-oss   Source File: SearchFilterToQueryConverter.java    License: 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 11
Source Project: dremio-oss   Source File: SearchFilterToQueryConverter.java    License: 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 12
Source Project: dremio-oss   Source File: SearchFilterToQueryConverter.java    License: 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 13
Source Project: dremio-oss   Source File: SearchFilterToQueryConverter.java    License: 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 14
Source Project: dremio-oss   Source File: SearchFilterToQueryConverter.java    License: 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 15
Source Project: pnc   Source File: RSQLProducerImpl.java    License: 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);
}
 
Example 16
Source Project: molgenis   Source File: ApiConfig.java    License: GNU Lesser General Public License v3.0 5 votes vote down vote up
private RSQLParser rsqlParser() {
  Set<ComparisonOperator> operators = new HashSet<>(RSQLOperators.defaultOperators());
  operators.add(new ComparisonOperator("=q=", false));
  operators.add(new ComparisonOperator("=sq=", false));
  operators.add(new ComparisonOperator("=like=", false));
  operators.add(new ComparisonOperator("=notlike=", false));
  return new RSQLParser(operators);
}
 
Example 17
@Test
void testVisitComparisonNodeEquals() {
  String selector = "name";
  String argument = "piet";
  ComparisonOperator operator = new ComparisonOperator("==");
  ComparisonNode node = new ComparisonNode(operator, selector, singletonList(argument));

  Query query = Query.builder().setItem(selector).setOperator(EQUALS).setValue(argument).build();
  assertEquals(query, queryRsqlVisitor.visit(node));
}
 
Example 18
@Test
void testVisitComparisonNodeEqualsNull() {
  String selector = "name";
  String argument = "";
  ComparisonOperator operator = new ComparisonOperator("==");
  ComparisonNode node = new ComparisonNode(operator, selector, singletonList(argument));

  Query query = Query.builder().setItem(selector).setOperator(EQUALS).setValue(null).build();
  assertEquals(query, queryRsqlVisitor.visit(node));
}
 
Example 19
@Test
void testVisitComparisonNodeNotEquals() {
  String selector = "name";
  String argument = "piet";
  ComparisonOperator operator = new ComparisonOperator("!=");
  ComparisonNode node = new ComparisonNode(operator, selector, singletonList(argument));

  Query query =
      Query.builder().setItem(selector).setOperator(NOT_EQUALS).setValue(argument).build();
  assertEquals(query, queryRsqlVisitor.visit(node));
}
 
Example 20
@Test
void testVisitComparisonNodeIn() {
  String selector = "name";
  List<String> arguments = asList("jan", "piet");
  ComparisonOperator operator = new ComparisonOperator("=in=", true);
  ComparisonNode node = new ComparisonNode(operator, selector, arguments);

  Query query = Query.builder().setItem(selector).setOperator(IN).setValue(arguments).build();
  assertEquals(query, queryRsqlVisitor.visit(node));
}
 
Example 21
@Test
void testVisitComparisonNodeNotIn() {
  String selector = "name";
  List<String> arguments = asList("jan", "piet");
  ComparisonOperator operator = new ComparisonOperator("=out=", true);
  ComparisonNode node = new ComparisonNode(operator, selector, arguments);

  Query query = Query.builder().setItem(selector).setOperator(NOT_IN).setValue(arguments).build();
  assertEquals(query, queryRsqlVisitor.visit(node));
}
 
Example 22
@Test
void testVisitComparisonNodeMatches() {
  String selector = "name";
  String argument = "piet";
  ComparisonOperator operator = new ComparisonOperator("=q=");
  ComparisonNode node = new ComparisonNode(operator, selector, singletonList(argument));

  Query query = Query.builder().setItem(selector).setOperator(MATCHES).setValue(argument).build();
  assertEquals(query, queryRsqlVisitor.visit(node));
}
 
Example 23
@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 24
@Test
void testVisitComparisonNodeMatchesAllSelector() {
  String selector = "*";
  String argument = "piet";
  ComparisonOperator operator = new ComparisonOperator("=q=");
  ComparisonNode node = new ComparisonNode(operator, selector, singletonList(argument));

  Query query = Query.builder().setItem(null).setOperator(MATCHES).setValue(argument).build();
  assertEquals(query, queryRsqlVisitor.visit(node));
}
 
Example 25
@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 26
@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 27
@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 28
@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 29
@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 30
@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));
}