Search in sources :

Example 61 with DocumentCondition

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

the class DocumentRepositoryProxyTest method shouldFindByNameLike.

@Test
public void shouldFindByNameLike() {
    Person ada = Person.builder().withAge(20).withName("Ada").build();
    when(template.select(any(DocumentQuery.class))).thenReturn(Stream.of(ada));
    personRepository.findByNameLike("Ada");
    ArgumentCaptor<DocumentQuery> captor = ArgumentCaptor.forClass(DocumentQuery.class);
    verify(template).select(captor.capture());
    DocumentQuery query = captor.getValue();
    DocumentCondition condition = query.getCondition().get();
    assertEquals("Person", query.getDocumentCollection());
    assertEquals(LIKE, condition.getCondition());
    assertEquals(Document.of("name", "Ada"), condition.getDocument());
}
Also used : DocumentQuery(jakarta.nosql.document.DocumentQuery) Person(jakarta.nosql.tck.entities.Person) DocumentCondition(jakarta.nosql.document.DocumentCondition) Test(org.junit.jupiter.api.Test)

Example 62 with DocumentCondition

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

the class DefaultDocumentTemplateTest method shouldDeleteEntity.

@Test
public void shouldDeleteEntity() {
    subject.delete(Person.class, "10");
    ArgumentCaptor<DocumentDeleteQuery> queryCaptor = ArgumentCaptor.forClass(DocumentDeleteQuery.class);
    verify(managerMock).delete(queryCaptor.capture());
    DocumentDeleteQuery query = queryCaptor.getValue();
    DocumentCondition condition = query.getCondition().get();
    assertEquals("Person", query.getDocumentCollection());
    assertEquals(DocumentCondition.eq(Document.of("_id", 10L)), condition);
}
Also used : DocumentCondition(jakarta.nosql.document.DocumentCondition) DocumentDeleteQuery(jakarta.nosql.document.DocumentDeleteQuery) Test(org.junit.jupiter.api.Test)

Example 63 with DocumentCondition

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

the class ParamsBinderTest method shouldConvert2.

@Test
public void shouldConvert2() {
    Method method = Stream.of(PersonRepository.class.getMethods()).filter(m -> m.getName().equals("findByAgeAndName")).findFirst().get();
    ClassMapping classMapping = mappings.get(Person.class);
    RepositoryDocumentObserverParser parser = new RepositoryDocumentObserverParser(classMapping);
    paramsBinder = new ParamsBinder(classMapping, converters);
    SelectMethodProvider selectMethodFactory = SelectMethodProvider.get();
    SelectQuery selectQuery = selectMethodFactory.apply(method, classMapping.getName());
    SelectQueryConverter converter = ServiceLoaderProvider.get(SelectQueryConverter.class);
    DocumentQueryParams queryParams = converter.apply(selectQuery, parser);
    Params params = queryParams.getParams();
    paramsBinder.bind(params, new Object[] { 10L, "Ada" }, method);
    DocumentQuery query = queryParams.getQuery();
    DocumentCondition columnCondition = query.getCondition().get();
    List<DocumentCondition> conditions = columnCondition.getDocument().get(new TypeReference<List<DocumentCondition>>() {
    });
    List<Object> values = conditions.stream().map(DocumentCondition::getDocument).map(Document::getValue).map(Value::get).collect(Collectors.toList());
    assertEquals(10, values.get(0));
    assertEquals("Ada", values.get(1));
}
Also used : ClassMapping(org.eclipse.jnosql.mapping.reflection.ClassMapping) ParamsBinder(org.eclipse.jnosql.mapping.util.ParamsBinder) DocumentQueryParams(jakarta.nosql.document.DocumentQueryParams) Params(jakarta.nosql.Params) Method(java.lang.reflect.Method) Document(jakarta.nosql.document.Document) SelectQuery(jakarta.nosql.query.SelectQuery) DocumentQuery(jakarta.nosql.document.DocumentQuery) SelectQueryConverter(jakarta.nosql.document.SelectQueryConverter) SelectMethodProvider(org.eclipse.jnosql.communication.query.method.SelectMethodProvider) DocumentQueryParams(jakarta.nosql.document.DocumentQueryParams) List(java.util.List) DocumentCondition(jakarta.nosql.document.DocumentCondition) Test(org.junit.jupiter.api.Test)

Example 64 with DocumentCondition

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

the class QueryAQLConverter method definesCondition.

private static void definesCondition(DocumentCondition condition, StringBuilder aql, Map<String, Object> params, char entity, int counter) {
    Document document = condition.getDocument();
    switch(condition.getCondition()) {
        case IN:
            appendCondition(aql, params, entity, document, IN);
            return;
        case EQUALS:
            appendCondition(aql, params, entity, document, EQUALS);
            return;
        case GREATER_EQUALS_THAN:
            appendCondition(aql, params, entity, document, GREATER_EQUALS_THAN);
            return;
        case GREATER_THAN:
            appendCondition(aql, params, entity, document, GREATER_THAN);
            return;
        case LESSER_THAN:
            appendCondition(aql, params, entity, document, LESSER_THAN);
            return;
        case LESSER_EQUALS_THAN:
            appendCondition(aql, params, entity, document, LESSER_EQUALS_THAN);
            return;
        case LIKE:
            appendCondition(aql, params, entity, document, LIKE);
            return;
        case AND:
            for (DocumentCondition dc : document.get(new TypeReference<List<DocumentCondition>>() {
            })) {
                if (isFirstCondition(aql, counter)) {
                    aql.append(AND);
                }
                definesCondition(dc, aql, params, entity, ++counter);
            }
            return;
        case OR:
            for (DocumentCondition dc : document.get(new TypeReference<List<DocumentCondition>>() {
            })) {
                if (isFirstCondition(aql, counter)) {
                    aql.append(OR);
                }
                definesCondition(dc, aql, params, entity, ++counter);
            }
            return;
        case NOT:
            DocumentCondition documentCondition = document.get(DocumentCondition.class);
            aql.append(NOT);
            definesCondition(documentCondition, aql, params, entity, ++counter);
            return;
        default:
            throw new IllegalArgumentException("The condition does not support in AQL: " + condition.getCondition());
    }
}
Also used : List(java.util.List) Document(jakarta.nosql.document.Document) DocumentCondition(jakarta.nosql.document.DocumentCondition)

Example 65 with DocumentCondition

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

the class DocumentQueryConversor method convert.

public static Bson convert(DocumentCondition condition) {
    Document document = condition.getDocument();
    Object value = ValueUtil.convert(document.getValue());
    switch(condition.getCondition()) {
        case EQUALS:
            return Filters.eq(document.getName(), value);
        case GREATER_THAN:
            return Filters.gt(document.getName(), value);
        case GREATER_EQUALS_THAN:
            return Filters.gte(document.getName(), value);
        case LESSER_THAN:
            return Filters.lt(document.getName(), value);
        case LESSER_EQUALS_THAN:
            return Filters.lte(document.getName(), value);
        case IN:
            List<Object> inList = ValueUtil.convertToList(document.getValue());
            return Filters.in(document.getName(), inList.toArray());
        case NOT:
            return Filters.not(convert(document.get(DocumentCondition.class)));
        case LIKE:
            return Filters.regex(document.getName(), value.toString());
        case AND:
            List<DocumentCondition> andList = condition.getDocument().getValue().get(new TypeReference<List<DocumentCondition>>() {
            });
            return Filters.and(andList.stream().map(DocumentQueryConversor::convert).collect(Collectors.toList()));
        case OR:
            List<DocumentCondition> orList = condition.getDocument().getValue().get(new TypeReference<List<DocumentCondition>>() {
            });
            return Filters.or(orList.stream().map(DocumentQueryConversor::convert).collect(Collectors.toList()));
        default:
            throw new UnsupportedOperationException("The condition " + condition.getCondition() + " is not supported from mongoDB diana driver");
    }
}
Also used : List(java.util.List) Document(jakarta.nosql.document.Document) DocumentCondition(jakarta.nosql.document.DocumentCondition)

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