Search in sources :

Example 6 with Condition

use of jakarta.nosql.query.Condition in project jnosql-diana by eclipse.

the class SelectQueryProviderTest method shouldReturnParserQuery12.

@ParameterizedTest(name = "Should parser the query {0}")
@ValueSource(strings = { "select  * from God where stamina <= 10.23" })
public void shouldReturnParserQuery12(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.LESSER_EQUALS_THAN, condition.getOperator());
    assertEquals("stamina", condition.getName());
    assertTrue(value instanceof NumberQueryValue);
    assertEquals(10.23, value.get());
}
Also used : SelectQuery(jakarta.nosql.query.SelectQuery) Condition(jakarta.nosql.query.Condition) NumberQueryValue(jakarta.nosql.query.NumberQueryValue) Where(jakarta.nosql.query.Where) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 7 with Condition

use of jakarta.nosql.query.Condition 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 8 with Condition

use of jakarta.nosql.query.Condition 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 9 with Condition

use of jakarta.nosql.query.Condition 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)

Example 10 with Condition

use of jakarta.nosql.query.Condition in project jnosql-diana by eclipse.

the class AbstractQueryConvert method getGraphTraversal.

protected GraphTraversal<Vertex, Vertex> getGraphTraversal(GraphQueryMethod graphQuery, Supplier<Optional<Where>> whereSupplier, ClassMapping mapping) {
    GraphTraversal<Vertex, Vertex> traversal = graphQuery.getTraversal();
    Optional<Where> whereOptional = whereSupplier.get();
    if (whereOptional.isPresent()) {
        Where where = whereOptional.get();
        Condition condition = where.getCondition();
        traversal.filter(getPredicate(graphQuery, condition, mapping));
    }
    return traversal;
}
Also used : Condition(jakarta.nosql.query.Condition) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) Where(jakarta.nosql.query.Where)

Aggregations

Condition (jakarta.nosql.query.Condition)79 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)66 ValueSource (org.junit.jupiter.params.provider.ValueSource)66 Where (jakarta.nosql.query.Where)61 SelectQuery (jakarta.nosql.query.SelectQuery)31 DeleteQuery (jakarta.nosql.query.DeleteQuery)29 NumberQueryValue (jakarta.nosql.query.NumberQueryValue)28 ConditionQueryValue (jakarta.nosql.query.ConditionQueryValue)25 StringQueryValue (jakarta.nosql.query.StringQueryValue)24 ParamQueryValue (jakarta.nosql.query.ParamQueryValue)18 JsonObject (javax.json.JsonObject)18 JSONQueryValue (jakarta.nosql.query.JSONQueryValue)16 FunctionQueryValue (jakarta.nosql.query.FunctionQueryValue)10 ArrayQueryValue (jakarta.nosql.query.ArrayQueryValue)8 Function (jakarta.nosql.query.Function)8 InsertQuery (jakarta.nosql.query.InsertQuery)7 UpdateQuery (jakarta.nosql.query.UpdateQuery)7 QueryValue (jakarta.nosql.query.QueryValue)6 Operator (jakarta.nosql.query.Operator)3 ArrayList (java.util.ArrayList)2