Search in sources :

Example 6 with Function

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

the class Values method get.

static Object get(QueryValue<?> value, Params parameters) {
    ValueType type = value.getType();
    switch(type) {
        case NUMBER:
        case STRING:
            return value.get();
        case PARAMETER:
            return parameters.add(ParamQueryValue.class.cast(value).get());
        case ARRAY:
            return Stream.of(ArrayQueryValue.class.cast(value).get()).map(v -> get(v, parameters)).collect(toList());
        case FUNCTION:
            Function function = FunctionQueryValue.class.cast(value).get();
            String name = function.getName();
            Object[] params = function.getParams();
            if ("convert".equals(name)) {
                return jakarta.nosql.Value.of(get(QueryValue.class.cast(params[0]), parameters)).get((Class<?>) params[1]);
            }
            String message = String.format("There is not support to the function: %s with parameters %s", name, Arrays.toString(params));
            throw new QueryException(message);
        case JSON:
            return JsonObjects.getDocuments(JSONQueryValue.class.cast(value).get());
        case CONDITION:
        default:
            throw new QueryException("There is not support to the value: " + type);
    }
}
Also used : ParamQueryValue(jakarta.nosql.query.ParamQueryValue) QueryValue(jakarta.nosql.query.QueryValue) Arrays(java.util.Arrays) Collectors.toList(java.util.stream.Collectors.toList) Stream(java.util.stream.Stream) ValueType(jakarta.nosql.query.ValueType) ArrayQueryValue(jakarta.nosql.query.ArrayQueryValue) QueryException(jakarta.nosql.QueryException) Function(jakarta.nosql.query.Function) FunctionQueryValue(jakarta.nosql.query.FunctionQueryValue) JSONQueryValue(jakarta.nosql.query.JSONQueryValue) Params(jakarta.nosql.Params) Function(jakarta.nosql.query.Function) QueryException(jakarta.nosql.QueryException) ValueType(jakarta.nosql.query.ValueType) FunctionQueryValue(jakarta.nosql.query.FunctionQueryValue) ParamQueryValue(jakarta.nosql.query.ParamQueryValue) QueryValue(jakarta.nosql.query.QueryValue) ArrayQueryValue(jakarta.nosql.query.ArrayQueryValue) FunctionQueryValue(jakarta.nosql.query.FunctionQueryValue) JSONQueryValue(jakarta.nosql.query.JSONQueryValue)

Example 7 with Function

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

the class SelectQueryProviderTest method shouldReturnParserQuery21.

@ParameterizedTest(name = "Should parser the query {0}")
@ValueSource(strings = { "select  * from God where age = convert(12, java.lang.Integer)" })
public void shouldReturnParserQuery21(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.EQUALS, condition.getOperator());
    assertEquals("age", condition.getName());
    assertTrue(value instanceof FunctionQueryValue);
    Function function = FunctionQueryValue.class.cast(value).get();
    assertEquals("convert", function.getName());
    Object[] params = function.getParams();
    assertEquals(12L, NumberQueryValue.class.cast(params[0]).get());
    assertEquals(Integer.class, params[1]);
}
Also used : SelectQuery(jakarta.nosql.query.SelectQuery) Condition(jakarta.nosql.query.Condition) Function(jakarta.nosql.query.Function) JsonObject(javax.json.JsonObject) FunctionQueryValue(jakarta.nosql.query.FunctionQueryValue) Where(jakarta.nosql.query.Where) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 8 with Function

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

the class SelectQueryProviderTest method shouldReturnParserQuery28.

@ParameterizedTest(name = "Should parser the query {0}")
@ValueSource(strings = { "select  * from God where name = \"Ada\" and age = 20 or" + " siblings = {\"apollo\": \"Brother\", \"Zeus\": \"Father\"} or birthday =" + " convert(\"2007-12-03\", java.time.LocalDate)" })
public void shouldReturnParserQuery28(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);
    Assertions.assertEquals(Operator.OR, condition.getOperator());
    conditions = ConditionQueryValue.class.cast(condition.getValue()).get();
    assertEquals(2, conditions.size());
    condition = conditions.get(0);
    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"));
    condition = conditions.get(1);
    value = condition.getValue();
    Assertions.assertEquals(Operator.EQUALS, condition.getOperator());
    assertEquals("birthday", condition.getName());
    assertTrue(value instanceof FunctionQueryValue);
    Function function = FunctionQueryValue.class.cast(value).get();
    assertEquals("convert", function.getName());
    Object[] params = function.getParams();
    assertEquals("2007-12-03", StringQueryValue.class.cast(params[0]).get());
    assertEquals(LocalDate.class, params[1]);
}
Also used : Condition(jakarta.nosql.query.Condition) JsonObject(javax.json.JsonObject) StringQueryValue(jakarta.nosql.query.StringQueryValue) JSONQueryValue(jakarta.nosql.query.JSONQueryValue) SelectQuery(jakarta.nosql.query.SelectQuery) NumberQueryValue(jakarta.nosql.query.NumberQueryValue) Function(jakarta.nosql.query.Function) ConditionQueryValue(jakarta.nosql.query.ConditionQueryValue) JsonObject(javax.json.JsonObject) FunctionQueryValue(jakarta.nosql.query.FunctionQueryValue) Where(jakarta.nosql.query.Where) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 9 with Function

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

the class SelectQueryProviderTest method shouldReturnParserQuery29.

@ParameterizedTest(name = "Should parser the query {0}")
@ValueSource(strings = { "select  * from God where name = \"Ada\" and age = 20 or" + " siblings = {\"apollo\": \"Brother\", \"Zeus\": \"Father\"} and birthday =" + " convert(\"2007-12-03\", java.time.LocalDate)" })
public void shouldReturnParserQuery29(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(4, 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);
    Assertions.assertEquals(Operator.OR, condition.getOperator());
    assertEquals(1, ConditionQueryValue.class.cast(condition.getValue()).get().size());
    Condition c = ConditionQueryValue.class.cast(condition.getValue()).get().get(0);
    value = c.getValue();
    Assertions.assertEquals(Operator.EQUALS, c.getOperator());
    assertEquals("siblings", c.getName());
    assertTrue(value instanceof JSONQueryValue);
    JsonObject jsonObject = JSONQueryValue.class.cast(value).get();
    assertEquals("Brother", jsonObject.getString("apollo"));
    assertEquals("Father", jsonObject.getString("Zeus"));
    condition = conditions.get(3);
    value = condition.getValue();
    Assertions.assertEquals(Operator.EQUALS, condition.getOperator());
    assertEquals("birthday", condition.getName());
    assertTrue(value instanceof FunctionQueryValue);
    Function function = FunctionQueryValue.class.cast(value).get();
    assertEquals("convert", function.getName());
    Object[] params = function.getParams();
    assertEquals("2007-12-03", StringQueryValue.class.cast(params[0]).get());
    assertEquals(LocalDate.class, params[1]);
}
Also used : Condition(jakarta.nosql.query.Condition) JsonObject(javax.json.JsonObject) StringQueryValue(jakarta.nosql.query.StringQueryValue) JSONQueryValue(jakarta.nosql.query.JSONQueryValue) SelectQuery(jakarta.nosql.query.SelectQuery) NumberQueryValue(jakarta.nosql.query.NumberQueryValue) Function(jakarta.nosql.query.Function) ConditionQueryValue(jakarta.nosql.query.ConditionQueryValue) JsonObject(javax.json.JsonObject) FunctionQueryValue(jakarta.nosql.query.FunctionQueryValue) Where(jakarta.nosql.query.Where) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 10 with Function

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

the class UpdateQueryProviderTest method shouldReturnParserQuery5.

@ParameterizedTest(name = "Should parser the query {0}")
@ValueSource(strings = { "update God (birthday = convert(\"1988-01-01\", java.time.LocalDate))" })
public void shouldReturnParserQuery5(String query) {
    UpdateQuery updateQuery = checkUpdateFromStart(query);
    List<Condition> conditions = updateQuery.getConditions();
    assertEquals(1, conditions.size());
    Condition condition = conditions.get(0);
    assertEquals("birthday", condition.getName());
    assertEquals(Operator.EQUALS, condition.getOperator());
    QueryValue<?> value = condition.getValue();
    assertTrue(value instanceof FunctionQueryValue);
    Function function = FunctionQueryValue.class.cast(value).get();
    assertEquals("convert", function.getName());
    Object[] params = function.getParams();
    assertEquals(2, params.length);
    assertEquals("1988-01-01", StringQueryValue.class.cast(params[0]).get());
    assertEquals(LocalDate.class, params[1]);
}
Also used : Condition(jakarta.nosql.query.Condition) Function(jakarta.nosql.query.Function) UpdateQuery(jakarta.nosql.query.UpdateQuery) JsonObject(javax.json.JsonObject) FunctionQueryValue(jakarta.nosql.query.FunctionQueryValue) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

Function (jakarta.nosql.query.Function)12 FunctionQueryValue (jakarta.nosql.query.FunctionQueryValue)11 Condition (jakarta.nosql.query.Condition)8 JsonObject (javax.json.JsonObject)8 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)8 ValueSource (org.junit.jupiter.params.provider.ValueSource)8 JSONQueryValue (jakarta.nosql.query.JSONQueryValue)7 Where (jakarta.nosql.query.Where)6 QueryException (jakarta.nosql.QueryException)4 ConditionQueryValue (jakarta.nosql.query.ConditionQueryValue)4 NumberQueryValue (jakarta.nosql.query.NumberQueryValue)4 StringQueryValue (jakarta.nosql.query.StringQueryValue)4 Params (jakarta.nosql.Params)3 ArrayQueryValue (jakarta.nosql.query.ArrayQueryValue)3 DeleteQuery (jakarta.nosql.query.DeleteQuery)3 ParamQueryValue (jakarta.nosql.query.ParamQueryValue)3 QueryValue (jakarta.nosql.query.QueryValue)3 SelectQuery (jakarta.nosql.query.SelectQuery)3 ValueType (jakarta.nosql.query.ValueType)3 Arrays (java.util.Arrays)3