Search in sources :

Example 1 with ConditionQueryValue

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());
}
Also used : SelectQuery(jakarta.nosql.query.SelectQuery) Condition(jakarta.nosql.query.Condition) ConditionQueryValue(jakarta.nosql.query.ConditionQueryValue) StringQueryValue(jakarta.nosql.query.StringQueryValue) Where(jakarta.nosql.query.Where) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 2 with ConditionQueryValue

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"));
}
Also used : SelectQuery(jakarta.nosql.query.SelectQuery) Condition(jakarta.nosql.query.Condition) NumberQueryValue(jakarta.nosql.query.NumberQueryValue) ConditionQueryValue(jakarta.nosql.query.ConditionQueryValue) JsonObject(javax.json.JsonObject) StringQueryValue(jakarta.nosql.query.StringQueryValue) Where(jakarta.nosql.query.Where) JSONQueryValue(jakarta.nosql.query.JSONQueryValue) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 3 with ConditionQueryValue

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));
        }
    }
}
Also used : Condition(jakarta.nosql.query.Condition) ConditionQueryValue(jakarta.nosql.query.ConditionQueryValue) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List)

Example 4 with ConditionQueryValue

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));
        }
    }
}
Also used : Condition(jakarta.nosql.query.Condition) ConditionQueryValue(jakarta.nosql.query.ConditionQueryValue) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List)

Example 5 with ConditionQueryValue

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");
    }
}
Also used : Operator(jakarta.nosql.query.Operator) Condition(jakarta.nosql.query.Condition) ConditionQueryValue(jakarta.nosql.query.ConditionQueryValue)

Aggregations

Condition (jakarta.nosql.query.Condition)19 ConditionQueryValue (jakarta.nosql.query.ConditionQueryValue)19 Where (jakarta.nosql.query.Where)16 StringQueryValue (jakarta.nosql.query.StringQueryValue)12 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)12 ValueSource (org.junit.jupiter.params.provider.ValueSource)12 JSONQueryValue (jakarta.nosql.query.JSONQueryValue)10 NumberQueryValue (jakarta.nosql.query.NumberQueryValue)10 JsonObject (javax.json.JsonObject)10 DeleteQuery (jakarta.nosql.query.DeleteQuery)8 SelectQuery (jakarta.nosql.query.SelectQuery)8 Function (jakarta.nosql.query.Function)4 FunctionQueryValue (jakarta.nosql.query.FunctionQueryValue)4 ParamQueryValue (jakarta.nosql.query.ParamQueryValue)4 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Operator (jakarta.nosql.query.Operator)1