use of jakarta.nosql.query.ConditionQueryValue in project jnosql-diana by eclipse.
the class SelectQueryProviderTest method shouldReturnParserQuery24.
@ParameterizedTest(name = "Should parser the query {0}")
@ValueSource(strings = { "select * from God where name not like \"Ada\"" })
public void shouldReturnParserQuery24(String query) {
SelectQuery selectQuery = checkSelectFromStart(query);
assertTrue(selectQuery.getWhere().isPresent());
Where where = selectQuery.getWhere().get();
Condition condition = where.getCondition();
QueryValue<?> value = condition.getValue();
Assertions.assertEquals(Operator.NOT, condition.getOperator());
assertEquals("_NOT", condition.getName());
assertTrue(value instanceof ConditionQueryValue);
List<Condition> conditions = ConditionQueryValue.class.cast(value).get();
assertEquals(1, conditions.size());
condition = conditions.get(0);
value = condition.getValue();
Assertions.assertEquals(Operator.LIKE, condition.getOperator());
assertEquals("name", condition.getName());
assertTrue(value instanceof StringQueryValue);
assertEquals("Ada", StringQueryValue.class.cast(value).get());
}
use of jakarta.nosql.query.ConditionQueryValue in project jnosql-diana by eclipse.
the class SelectQueryProviderTest method shouldReturnParserQuery25.
@ParameterizedTest(name = "Should parser the query {0}")
@ValueSource(strings = { "select * from God where name = \"Ada\" and age = 20 and" + " siblings = {\"apollo\": \"Brother\", \"Zeus\": \"Father\"}" })
public void shouldReturnParserQuery25(String query) {
SelectQuery selectQuery = checkSelectFromStart(query);
assertTrue(selectQuery.getWhere().isPresent());
Where where = selectQuery.getWhere().get();
Condition condition = where.getCondition();
QueryValue<?> value = condition.getValue();
Assertions.assertEquals(Operator.AND, condition.getOperator());
assertEquals("_AND", condition.getName());
assertTrue(value instanceof ConditionQueryValue);
List<Condition> conditions = ConditionQueryValue.class.cast(value).get();
assertEquals(3, conditions.size());
condition = conditions.get(0);
value = condition.getValue();
Assertions.assertEquals(Operator.EQUALS, condition.getOperator());
assertEquals("name", condition.getName());
assertTrue(value instanceof StringQueryValue);
assertEquals("Ada", StringQueryValue.class.cast(value).get());
condition = conditions.get(1);
value = condition.getValue();
Assertions.assertEquals(Operator.EQUALS, condition.getOperator());
assertEquals("age", condition.getName());
assertTrue(value instanceof NumberQueryValue);
assertEquals(20L, NumberQueryValue.class.cast(value).get());
condition = conditions.get(2);
value = condition.getValue();
Assertions.assertEquals(Operator.EQUALS, condition.getOperator());
assertEquals("siblings", condition.getName());
assertTrue(value instanceof JSONQueryValue);
JsonObject jsonObject = JSONQueryValue.class.cast(value).get();
assertEquals("Brother", jsonObject.getString("apollo"));
assertEquals("Father", jsonObject.getString("Zeus"));
}
use of jakarta.nosql.query.ConditionQueryValue in project jnosql-diana by eclipse.
the class AbstractWhereSupplier method appendCondition.
private void appendCondition(Operator operator, Condition newCondition) {
if (operator.equals(this.condition.getOperator())) {
ConditionQueryValue conditionValue = ConditionQueryValue.class.cast(this.condition.getValue());
List<Condition> conditions = new ArrayList<>(conditionValue.get());
conditions.add(newCondition);
this.condition = new DefaultCondition("_" + operator.name(), operator, DefaultConditionValue.of(conditions));
} else if (isNotAppendable()) {
List<Condition> conditions = Arrays.asList(this.condition, newCondition);
this.condition = new DefaultCondition("_" + operator.name(), operator, DefaultConditionValue.of(conditions));
} else {
List<Condition> conditions = ConditionQueryValue.class.cast(this.condition.getValue()).get();
Condition lastCondition = conditions.get(conditions.size() - 1);
if (isAppendable(lastCondition) && operator.equals(lastCondition.getOperator())) {
List<Condition> lastConditions = new ArrayList<>(ConditionQueryValue.class.cast(lastCondition.getValue()).get());
lastConditions.add(newCondition);
Condition newAppendable = new DefaultCondition("_" + operator.name(), operator, DefaultConditionValue.of(lastConditions));
List<Condition> newConditions = new ArrayList<>(conditions.subList(0, conditions.size() - 1));
newConditions.add(newAppendable);
this.condition = new DefaultCondition(this.condition.getName(), this.condition.getOperator(), DefaultConditionValue.of(newConditions));
} else {
Condition newAppendable = new DefaultCondition("_" + operator.name(), operator, DefaultConditionValue.of(Collections.singletonList(newCondition)));
List<Condition> newConditions = new ArrayList<>(conditions);
newConditions.add(newAppendable);
this.condition = new DefaultCondition(this.condition.getName(), this.condition.getOperator(), DefaultConditionValue.of(newConditions));
}
}
}
use of jakarta.nosql.query.ConditionQueryValue in project jnosql-diana by eclipse.
the class AbstractMethodQueryProvider method appendCondition.
private void appendCondition(Operator operator, Condition newCondition) {
if (operator.equals(this.condition.getOperator())) {
ConditionQueryValue conditionValue = ConditionQueryValue.class.cast(this.condition.getValue());
List<Condition> conditions = new ArrayList<>(conditionValue.get());
conditions.add(newCondition);
this.condition = new MethodCondition(SUB_ENTITY_FLAG + operator.name(), operator, MethodConditionValue.of(conditions));
} else if (isNotAppendable()) {
List<Condition> conditions = Arrays.asList(this.condition, newCondition);
this.condition = new MethodCondition(SUB_ENTITY_FLAG + operator.name(), operator, MethodConditionValue.of(conditions));
} else {
List<Condition> conditions = ConditionQueryValue.class.cast(this.condition.getValue()).get();
Condition lastCondition = conditions.get(conditions.size() - 1);
if (isAppendable(lastCondition) && operator.equals(lastCondition.getOperator())) {
List<Condition> lastConditions = new ArrayList<>(ConditionQueryValue.class.cast(lastCondition.getValue()).get());
lastConditions.add(newCondition);
Condition newAppendable = new MethodCondition(SUB_ENTITY_FLAG + operator.name(), operator, MethodConditionValue.of(lastConditions));
List<Condition> newConditions = new ArrayList<>(conditions.subList(0, conditions.size() - 1));
newConditions.add(newAppendable);
this.condition = new MethodCondition(this.condition.getName(), this.condition.getOperator(), MethodConditionValue.of(newConditions));
} else {
Condition newAppendable = new MethodCondition(SUB_ENTITY_FLAG + operator.name(), operator, MethodConditionValue.of(Collections.singletonList(newCondition)));
List<Condition> newConditions = new ArrayList<>(conditions);
newConditions.add(newAppendable);
this.condition = new MethodCondition(this.condition.getName(), this.condition.getOperator(), MethodConditionValue.of(newConditions));
}
}
}
use of jakarta.nosql.query.ConditionQueryValue in project jnosql-diana by eclipse.
the class AbstractQueryConvert method getPredicate.
protected GraphTraversal<Vertex, Vertex> getPredicate(GraphQueryMethod graphQuery, Condition condition, ClassMapping mapping) {
Operator operator = condition.getOperator();
String name = condition.getName();
String nativeName = mapping.getColumnField(name);
switch(operator) {
case EQUALS:
return __.has(nativeName, P.eq(graphQuery.getValue(name)));
case GREATER_THAN:
return __.has(nativeName, P.gt(graphQuery.getValue(name)));
case GREATER_EQUALS_THAN:
return __.has(nativeName, P.gte(graphQuery.getValue(name)));
case LESSER_THAN:
return __.has(nativeName, P.lt(graphQuery.getValue(name)));
case LESSER_EQUALS_THAN:
return __.has(nativeName, P.lte(graphQuery.getValue(name)));
case BETWEEN:
return __.has(nativeName, P.between(graphQuery.getValue(name), graphQuery.getValue(name)));
case IN:
return __.has(nativeName, P.within(graphQuery.getInValue(name)));
case NOT:
Condition notCondition = ((ConditionQueryValue) condition.getValue()).get().get(0);
return __.not(getPredicate(graphQuery, notCondition, mapping));
case AND:
return ((ConditionQueryValue) condition.getValue()).get().stream().map(c -> getPredicate(graphQuery, c, mapping)).reduce(GraphTraversal::and).orElseThrow(() -> new UnsupportedOperationException("There is an inconsistency at the AND operator"));
case OR:
return ((ConditionQueryValue) condition.getValue()).get().stream().map(c -> getPredicate(graphQuery, c, mapping)).reduce(GraphTraversal::or).orElseThrow(() -> new UnsupportedOperationException("There is an inconsistency at the OR operator"));
default:
throw new UnsupportedOperationException("There is not support to the type " + operator + " in graph");
}
}
Aggregations