Search in sources :

Example 6 with DocumentEntity

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

the class InsertQueryParser method query.

Stream<DocumentEntity> query(String query, DocumentCollectionManager collectionManager, DocumentObserverParser observer) {
    InsertQuery insertQuery = insertQueryProvider.apply(query);
    String collection = insertQuery.getEntity();
    Params params = Params.newParams();
    DocumentEntity entity = getEntity(insertQuery, collection, params, observer);
    Optional<Duration> ttl = insertQuery.getTtl();
    if (params.isNotEmpty()) {
        throw new QueryException("To run a query with a parameter use a PrepareStatement instead.");
    }
    if (ttl.isPresent()) {
        return Stream.of(collectionManager.insert(entity, ttl.get()));
    } else {
        return Stream.of(collectionManager.insert(entity));
    }
}
Also used : InsertQuery(jakarta.nosql.query.InsertQuery) QueryException(jakarta.nosql.QueryException) DocumentEntity(jakarta.nosql.document.DocumentEntity) Params(jakarta.nosql.Params) Duration(java.time.Duration)

Example 7 with DocumentEntity

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

the class DefaultDocumentPreparedStatement method getSingleResult.

@Override
public Optional<DocumentEntity> getSingleResult() {
    Stream<DocumentEntity> entities = getResult();
    final Iterator<DocumentEntity> iterator = entities.iterator();
    if (!iterator.hasNext()) {
        return Optional.empty();
    }
    final DocumentEntity next = iterator.next();
    if (!iterator.hasNext()) {
        return Optional.of(next);
    }
    throw new NonUniqueResultException("The select returns more than one entity, select: " + query);
}
Also used : NonUniqueResultException(jakarta.nosql.NonUniqueResultException) DocumentEntity(jakarta.nosql.document.DocumentEntity)

Example 8 with DocumentEntity

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

the class DefaultDocumentQueryParserTest method shouldSingleResult.

@ParameterizedTest(name = "Should parser the query {0}")
@ValueSource(strings = { "select  * from God where age = @age" })
public void shouldSingleResult(String query) {
    ArgumentCaptor<DocumentQuery> captor = ArgumentCaptor.forClass(DocumentQuery.class);
    Mockito.when(manager.select(Mockito.any(DocumentQuery.class))).thenReturn(Stream.of(mock(DocumentEntity.class)));
    DocumentPreparedStatement prepare = parser.prepare(query, manager, DocumentObserverParser.EMPTY);
    prepare.bind("age", 12);
    final Optional<DocumentEntity> result = prepare.getSingleResult();
    Mockito.verify(manager).select(captor.capture());
    DocumentQuery columnQuery = captor.getValue();
    DocumentCondition columnCondition = columnQuery.getCondition().get();
    Document column = columnCondition.getDocument();
    assertEquals(Condition.EQUALS, columnCondition.getCondition());
    assertEquals("age", column.getName());
    assertEquals(12, column.get());
    assertTrue(result.isPresent());
}
Also used : DocumentQuery(jakarta.nosql.document.DocumentQuery) DocumentEntity(jakarta.nosql.document.DocumentEntity) DocumentPreparedStatement(jakarta.nosql.document.DocumentPreparedStatement) Document(jakarta.nosql.document.Document) DocumentCondition(jakarta.nosql.document.DocumentCondition) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 9 with DocumentEntity

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

the class DocumentEntityTest method shouldReturnAnErrorWhenAddANullDocument.

@Test
public void shouldReturnAnErrorWhenAddANullDocument() {
    Assertions.assertThrows(NullPointerException.class, () -> {
        DocumentEntity entity = new DefaultDocumentEntity("name");
        entity.add(null);
    });
}
Also used : DocumentEntity(jakarta.nosql.document.DocumentEntity) Test(org.junit.jupiter.api.Test)

Example 10 with DocumentEntity

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

the class DocumentEntityTest method shouldRemoveAllElementsWhenUseClearMethod.

@Test
public void shouldRemoveAllElementsWhenUseClearMethod() {
    List<Document> documents = asList(Document.of("name", 10), Document.of("name2", 11), Document.of("name3", 12), Document.of("name4", 13), Document.of("name5", 14), Document.of("name5", 16));
    DocumentEntity collection = DocumentEntity.of("documentCollection", documents);
    assertFalse(collection.isEmpty());
    collection.clear();
    assertTrue(collection.isEmpty());
}
Also used : DocumentEntity(jakarta.nosql.document.DocumentEntity) Document(jakarta.nosql.document.Document) Test(org.junit.jupiter.api.Test)

Aggregations

DocumentEntity (jakarta.nosql.document.DocumentEntity)312 Test (org.junit.jupiter.api.Test)238 DocumentQuery (jakarta.nosql.document.DocumentQuery)131 Document (jakarta.nosql.document.Document)129 DocumentDeleteQuery (jakarta.nosql.document.DocumentDeleteQuery)62 List (java.util.List)54 Arrays.asList (java.util.Arrays.asList)51 ArrayList (java.util.ArrayList)46 Duration (java.time.Duration)31 HashMap (java.util.HashMap)30 Map (java.util.Map)27 Collectors (java.util.stream.Collectors)27 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)25 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)25 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)23 ValueSource (org.junit.jupiter.params.provider.ValueSource)23 TypeReference (jakarta.nosql.TypeReference)21 DocumentQuery.select (jakarta.nosql.document.DocumentQuery.select)21 Matchers.contains (org.hamcrest.Matchers.contains)21 Assertions.assertFalse (org.junit.jupiter.api.Assertions.assertFalse)21