Search in sources :

Example 6 with PutQuery

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

the class PutProviderTest method shouldReturnParserQuery6.

@ParameterizedTest(name = "Should parser the query {0}")
@ValueSource(strings = { "put {@name, @value, 10 millisecond}\n" })
public void shouldReturnParserQuery6(String query) {
    PutQuery putQuery = provider.apply(query);
    QueryValue<?> key = putQuery.getKey();
    QueryValue<?> value = putQuery.getValue();
    Optional<Duration> ttl = putQuery.getTtl();
    assertTrue(key instanceof ParamQueryValue);
    assertEquals("name", ParamQueryValue.class.cast(key).get());
    assertTrue(value instanceof ParamQueryValue);
    assertEquals("value", ParamQueryValue.class.cast(value).get());
    assertTrue(ttl.isPresent());
    assertEquals(Duration.ofMillis(10L), ttl.get());
}
Also used : ParamQueryValue(jakarta.nosql.query.ParamQueryValue) Duration(java.time.Duration) PutQuery(jakarta.nosql.query.PutQuery) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 7 with PutQuery

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

the class PutProviderTest method shouldReturnParserQuery.

@ParameterizedTest(name = "Should parser the query {0}")
@ValueSource(strings = { "put {\"Ada\", \"Hunt\"}\n" })
public void shouldReturnParserQuery(String query) {
    PutQuery putQuery = provider.apply(query);
    QueryValue<?> key = putQuery.getKey();
    QueryValue<?> value = putQuery.getValue();
    Optional<Duration> ttl = putQuery.getTtl();
    assertTrue(key instanceof StringQueryValue);
    assertEquals("Ada", StringQueryValue.class.cast(key).get());
    assertFalse(ttl.isPresent());
    assertTrue(value instanceof StringQueryValue);
    assertEquals("Hunt", StringQueryValue.class.cast(value).get());
    assertFalse(ttl.isPresent());
}
Also used : Duration(java.time.Duration) PutQuery(jakarta.nosql.query.PutQuery) StringQueryValue(jakarta.nosql.query.StringQueryValue) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 8 with PutQuery

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

the class PutProviderTest method shouldReturnParserQuery5.

@ParameterizedTest(name = "Should parser the query {0}")
@ValueSource(strings = { "put {@name, @value, 10 second}\n" })
public void shouldReturnParserQuery5(String query) {
    PutQuery putQuery = provider.apply(query);
    QueryValue<?> key = putQuery.getKey();
    QueryValue<?> value = putQuery.getValue();
    Optional<Duration> ttl = putQuery.getTtl();
    assertTrue(key instanceof ParamQueryValue);
    assertEquals("name", ParamQueryValue.class.cast(key).get());
    assertTrue(value instanceof ParamQueryValue);
    assertEquals("value", ParamQueryValue.class.cast(value).get());
    assertTrue(ttl.isPresent());
    assertEquals(Duration.ofSeconds(10L), ttl.get());
}
Also used : ParamQueryValue(jakarta.nosql.query.ParamQueryValue) Duration(java.time.Duration) PutQuery(jakarta.nosql.query.PutQuery) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 9 with PutQuery

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

the class PutQueryParser method query.

Stream<Value> query(String query, BucketManager manager) {
    PutQuery putQuery = provider.apply(query);
    Params params = Params.newParams();
    Value key = Values.getValue(putQuery.getKey(), params);
    Value value = Values.getValue(putQuery.getValue(), params);
    Optional<Duration> ttl = putQuery.getTtl();
    if (params.isNotEmpty()) {
        throw new QueryException("To run a query with a parameter use a PrepareStatement instead.");
    }
    KeyValueEntity entity = KeyValueEntity.of(key.get(), value.get());
    if (ttl.isPresent()) {
        manager.put(entity, ttl.get());
    } else {
        manager.put(entity);
    }
    return Stream.empty();
}
Also used : QueryException(jakarta.nosql.QueryException) Value(jakarta.nosql.Value) Params(jakarta.nosql.Params) Duration(java.time.Duration) PutQuery(jakarta.nosql.query.PutQuery) KeyValueEntity(jakarta.nosql.keyvalue.KeyValueEntity)

Example 10 with PutQuery

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

the class PutQueryParser method prepare.

public KeyValuePreparedStatement prepare(String query, BucketManager manager) {
    PutQuery putQuery = provider.apply(query);
    Params params = Params.newParams();
    Value key = Values.getValue(putQuery.getKey(), params);
    Value value = Values.getValue(putQuery.getValue(), params);
    Optional<Duration> ttl = putQuery.getTtl();
    return DefaultKeyValuePreparedStatement.put(key, value, manager, params, ttl.orElse(null), query);
}
Also used : Value(jakarta.nosql.Value) Params(jakarta.nosql.Params) Duration(java.time.Duration) PutQuery(jakarta.nosql.query.PutQuery)

Aggregations

PutQuery (jakarta.nosql.query.PutQuery)10 Duration (java.time.Duration)10 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)8 ValueSource (org.junit.jupiter.params.provider.ValueSource)8 ParamQueryValue (jakarta.nosql.query.ParamQueryValue)5 Params (jakarta.nosql.Params)2 Value (jakarta.nosql.Value)2 NumberQueryValue (jakarta.nosql.query.NumberQueryValue)2 StringQueryValue (jakarta.nosql.query.StringQueryValue)2 QueryException (jakarta.nosql.QueryException)1 KeyValueEntity (jakarta.nosql.keyvalue.KeyValueEntity)1