Search in sources :

Example 21 with DocumentDeleteQuery

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

the class AbstractDocumentTemplate method delete.

@Override
public <T, K> void delete(Class<T> entityClass, K id) {
    requireNonNull(entityClass, "entityClass is required");
    requireNonNull(id, "id is required");
    ClassMapping classMapping = getClassMappings().get(entityClass);
    FieldMapping idField = classMapping.getId().orElseThrow(() -> IdNotFoundException.newInstance(entityClass));
    Object value = ConverterUtil.getValue(id, classMapping, idField.getFieldName(), getConverters());
    DocumentDeleteQuery query = DocumentDeleteQuery.delete().from(classMapping.getName()).where(idField.getName()).eq(value).build();
    delete(query);
}
Also used : ClassMapping(org.eclipse.jnosql.mapping.reflection.ClassMapping) FieldMapping(org.eclipse.jnosql.mapping.reflection.FieldMapping) DocumentDeleteQuery(jakarta.nosql.document.DocumentDeleteQuery)

Example 22 with DocumentDeleteQuery

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

the class AbstractReactiveDocumentRepositoryProxy method invoke.

@Override
public Object invoke(Object instance, Method method, Object[] args) throws Throwable {
    RepositoryType type = ReactiveRepositoryType.of(method);
    Class<?> typeClass = getClassMapping().getClassInstance();
    switch(type) {
        case DEFAULT:
            return method.invoke(getRepository(), args);
        case FIND_BY:
            DocumentQuery query = getQuery(method, args);
            return executeQuery(method, args, typeClass, query);
        case FIND_ALL:
            DocumentQuery queryFindAll = select().from(getClassMapping().getName()).build();
            return executeQuery(method, args, typeClass, getQuerySorts(args, queryFindAll));
        case DELETE_BY:
            DocumentDeleteQuery documentDeleteQuery = getDeleteQuery(method, args);
            Iterable<Void> iterable = () -> {
                getTemplate().delete(documentDeleteQuery);
                return Collections.emptyIterator();
            };
            return ReactiveStreams.fromIterable(iterable).buildRs();
        case OBJECT_METHOD:
            return method.invoke(this, args);
        case JNOSQL_QUERY:
            DynamicQueryMethodReturn methodReturn = DynamicQueryMethodReturn.builder().withArgs(args).withMethod(method).withTypeClass(typeClass).withPrepareConverter(q -> getTemplate().prepare(q)).withQueryConverter(q -> getTemplate().query(q)).build();
            return methodReturn.execute();
        default:
            return Void.class;
    }
}
Also used : DynamicQueryMethodReturn(org.eclipse.jnosql.mapping.repository.DynamicQueryMethodReturn) RepositoryType(org.eclipse.jnosql.mapping.query.RepositoryType) ReactiveRepositoryType(org.eclipse.jnosql.mapping.reactive.ReactiveRepositoryType) DocumentQuery.select(jakarta.nosql.document.DocumentQuery.select) DocumentDeleteQuery(jakarta.nosql.document.DocumentDeleteQuery) DocumentTemplate(jakarta.nosql.mapping.document.DocumentTemplate) ReactiveRepository(org.eclipse.jnosql.mapping.reactive.ReactiveRepository) DocumentQuery(jakarta.nosql.document.DocumentQuery) BaseDocumentRepository(org.eclipse.jnosql.mapping.document.query.BaseDocumentRepository) DynamicQueryMethodReturn(org.eclipse.jnosql.mapping.repository.DynamicQueryMethodReturn) InvocationHandler(java.lang.reflect.InvocationHandler) ReactiveStreams(org.eclipse.microprofile.reactive.streams.operators.ReactiveStreams) Method(java.lang.reflect.Method) Collections(java.util.Collections) DocumentQuery(jakarta.nosql.document.DocumentQuery) RepositoryType(org.eclipse.jnosql.mapping.query.RepositoryType) ReactiveRepositoryType(org.eclipse.jnosql.mapping.reactive.ReactiveRepositoryType) DocumentDeleteQuery(jakarta.nosql.document.DocumentDeleteQuery)

Example 23 with DocumentDeleteQuery

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

the class DefaultReactiveDocumentTemplateTest method shouldDelete.

@Test
public void shouldDelete() {
    AtomicLong atomicLong = new AtomicLong();
    DocumentDeleteQuery query = Mockito.mock(DocumentDeleteQuery.class);
    final Observable<Void> observable = manager.delete(query);
    Mockito.verify(template, Mockito.never()).delete(query);
    CompletionStage<Optional<Void>> completion = observable.getFirst();
    completion.thenAccept(o -> atomicLong.incrementAndGet());
    Mockito.verify(template).delete(query);
    assertEquals(1L, atomicLong.get());
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) Optional(java.util.Optional) DocumentDeleteQuery(jakarta.nosql.document.DocumentDeleteQuery) Test(org.junit.jupiter.api.Test)

Example 24 with DocumentDeleteQuery

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

the class DefaultDocumentEventPersistManagerTest method shouldFirePreDeleteQuery.

@Test
public void shouldFirePreDeleteQuery() {
    DocumentDeleteQuery query = DocumentDeleteQuery.delete().from("collection").build();
    subject.firePreDeleteQuery(query);
    ArgumentCaptor<DocumentDeleteQueryExecute> captor = ArgumentCaptor.forClass(DocumentDeleteQueryExecute.class);
    verify(documentDeleteQueryExecute).fire(captor.capture());
    assertEquals(query, captor.getValue().getQuery());
}
Also used : DocumentDeleteQueryExecute(jakarta.nosql.mapping.document.DocumentDeleteQueryExecute) DocumentDeleteQuery(jakarta.nosql.document.DocumentDeleteQuery) Test(org.junit.jupiter.api.Test)

Example 25 with DocumentDeleteQuery

use of jakarta.nosql.document.DocumentDeleteQuery 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)

Aggregations

DocumentDeleteQuery (jakarta.nosql.document.DocumentDeleteQuery)114 Test (org.junit.jupiter.api.Test)79 DocumentEntity (jakarta.nosql.document.DocumentEntity)45 DocumentQuery (jakarta.nosql.document.DocumentQuery)43 Document (jakarta.nosql.document.Document)37 DocumentCondition (jakarta.nosql.document.DocumentCondition)33 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)20 ValueSource (org.junit.jupiter.params.provider.ValueSource)20 List (java.util.List)15 Person (jakarta.nosql.tck.entities.Person)12 Collectors (java.util.stream.Collectors)7 Params (jakarta.nosql.Params)5 DocumentDeleteQueryParams (jakarta.nosql.document.DocumentDeleteQueryParams)5 Duration (java.time.Duration)5 HashMap (java.util.HashMap)5 Optional (java.util.Optional)5 StreamSupport (java.util.stream.StreamSupport)5 DocumentCollectionManager (jakarta.nosql.document.DocumentCollectionManager)4 DocumentQuery.select (jakarta.nosql.document.DocumentQuery.select)4 Map (java.util.Map)4