Search in sources :

Example 1 with DocumentCondition

use of jakarta.nosql.document.DocumentCondition in project jnosql-diana by eclipse.

the class DeleteQueryParser method getQuery.

private DocumentDeleteQuery getQuery(String query, DocumentObserverParser observer) {
    DeleteQuery deleteQuery = deleteQueryProvider.apply(query);
    String collection = observer.fireEntity(deleteQuery.getEntity());
    List<String> documents = deleteQuery.getFields().stream().map(f -> observer.fireField(collection, f)).collect(Collectors.toList());
    DocumentCondition condition = null;
    Params params = Params.newParams();
    if (deleteQuery.getWhere().isPresent()) {
        condition = deleteQuery.getWhere().map(c -> Conditions.getCondition(c, params, observer, collection)).get();
    }
    if (params.isNotEmpty()) {
        throw new QueryException("To run a query with a parameter use a PrepareStatement instead.");
    }
    return new DefaultDocumentDeleteQuery(collection, condition, documents);
}
Also used : DocumentCollectionManager(jakarta.nosql.document.DocumentCollectionManager) DocumentCondition(jakarta.nosql.document.DocumentCondition) DocumentDeleteQuery(jakarta.nosql.document.DocumentDeleteQuery) DocumentPreparedStatement(jakarta.nosql.document.DocumentPreparedStatement) DocumentDeleteQueryParams(jakarta.nosql.document.DocumentDeleteQueryParams) DocumentEntity(jakarta.nosql.document.DocumentEntity) Collectors(java.util.stream.Collectors) ServiceLoaderProvider(jakarta.nosql.ServiceLoaderProvider) DeleteQueryConverter(jakarta.nosql.document.DeleteQueryConverter) DeleteQuery(jakarta.nosql.query.DeleteQuery) Objects(java.util.Objects) List(java.util.List) DeleteQueryProvider(jakarta.nosql.query.DeleteQuery.DeleteQueryProvider) Stream(java.util.stream.Stream) QueryException(jakarta.nosql.QueryException) DocumentObserverParser(jakarta.nosql.document.DocumentObserverParser) Params(jakarta.nosql.Params) QueryException(jakarta.nosql.QueryException) DocumentDeleteQueryParams(jakarta.nosql.document.DocumentDeleteQueryParams) Params(jakarta.nosql.Params) DocumentDeleteQuery(jakarta.nosql.document.DocumentDeleteQuery) DeleteQuery(jakarta.nosql.query.DeleteQuery) DocumentCondition(jakarta.nosql.document.DocumentCondition)

Example 2 with DocumentCondition

use of jakarta.nosql.document.DocumentCondition in project jnosql-diana by eclipse.

the class DefaultDocumentConditionTest method shouldCreateOrCondition.

@Test
public void shouldCreateOrCondition() {
    Document age = Document.of("age", 26);
    Document name = Document.of("name", "Otavio");
    DocumentCondition condition1 = DefaultDocumentCondition.of(name, Condition.EQUALS);
    DocumentCondition condition2 = DefaultDocumentCondition.of(age, Condition.GREATER_THAN);
    DocumentCondition and = condition1.or(condition2);
    Document andDocument = and.getDocument();
    assertEquals(Condition.OR, and.getCondition());
    assertEquals(Condition.OR.getNameField(), andDocument.getName());
    assertThat(andDocument.getValue().get(new TypeReference<List<DocumentCondition>>() {
    }), Matchers.containsInAnyOrder(condition1, condition2));
}
Also used : TypeReference(jakarta.nosql.TypeReference) Document(jakarta.nosql.document.Document) DocumentCondition(jakarta.nosql.document.DocumentCondition) Test(org.junit.jupiter.api.Test)

Example 3 with DocumentCondition

use of jakarta.nosql.document.DocumentCondition in project jnosql-diana by eclipse.

the class DefaultDocumentConditionTest method shouldAnd.

@Test
public void shouldAnd() {
    DocumentCondition eq = DocumentCondition.eq(Document.of("name", "otavio"));
    DocumentCondition gt = DocumentCondition.gt(Document.of("age", 10));
    DocumentCondition lte = DocumentCondition.lte(Document.of("salary", 10_000.00));
    DocumentCondition and = eq.and(gt);
    List<DocumentCondition> conditions = and.getDocument().get(new TypeReference<List<DocumentCondition>>() {
    });
    assertEquals(Condition.AND, and.getCondition());
    assertThat(conditions, Matchers.containsInAnyOrder(eq, gt));
    DocumentCondition result = and.and(lte);
    assertEquals(Condition.AND, result.getCondition());
    assertThat(result.getDocument().get(new TypeReference<List<DocumentCondition>>() {
    }), Matchers.containsInAnyOrder(eq, gt, lte));
}
Also used : List(java.util.List) TypeReference(jakarta.nosql.TypeReference) DocumentCondition(jakarta.nosql.document.DocumentCondition) Test(org.junit.jupiter.api.Test)

Example 4 with DocumentCondition

use of jakarta.nosql.document.DocumentCondition in project jnosql-diana by eclipse.

the class DefaultDocumentConditionTest method shouldAffirmDoubleNegate.

@Test
public void shouldAffirmDoubleNegate() {
    DocumentCondition eq = DocumentCondition.eq(Document.of("name", "otavio"));
    DocumentCondition affirm = eq.negate().negate();
    assertEquals(eq.getCondition(), affirm.getCondition());
}
Also used : DocumentCondition(jakarta.nosql.document.DocumentCondition) Test(org.junit.jupiter.api.Test)

Example 5 with DocumentCondition

use of jakarta.nosql.document.DocumentCondition in project jnosql-diana by eclipse.

the class DefaultDocumentConditionTest method shouldNegate.

@Test
public void shouldNegate() {
    DocumentCondition eq = DocumentCondition.eq(Document.of("name", "otavio"));
    DocumentCondition negate = eq.negate();
    assertEquals(Condition.NOT, negate.getCondition());
    DocumentCondition condition = negate.getDocument().get(DocumentCondition.class);
    assertEquals(eq, condition);
}
Also used : DocumentCondition(jakarta.nosql.document.DocumentCondition) Test(org.junit.jupiter.api.Test)

Aggregations

DocumentCondition (jakarta.nosql.document.DocumentCondition)132 Test (org.junit.jupiter.api.Test)68 DocumentQuery (jakarta.nosql.document.DocumentQuery)64 Document (jakarta.nosql.document.Document)60 List (java.util.List)37 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)37 ValueSource (org.junit.jupiter.params.provider.ValueSource)37 DocumentDeleteQuery (jakarta.nosql.document.DocumentDeleteQuery)33 Person (jakarta.nosql.tck.entities.Person)17 Pagination (jakarta.nosql.mapping.Pagination)13 DocumentPreparedStatement (jakarta.nosql.document.DocumentPreparedStatement)10 TypeReference (jakarta.nosql.TypeReference)7 Params (jakarta.nosql.Params)6 DocumentEntity (jakarta.nosql.document.DocumentEntity)6 DocumentObserverParser (jakarta.nosql.document.DocumentObserverParser)5 QueryException (jakarta.nosql.QueryException)4 ServiceLoaderProvider (jakarta.nosql.ServiceLoaderProvider)4 DocumentCollectionManager (jakarta.nosql.document.DocumentCollectionManager)4 DocumentQueryParams (jakarta.nosql.document.DocumentQueryParams)4 SelectQueryConverter (jakarta.nosql.document.SelectQueryConverter)4