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));
}
}
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);
}
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());
}
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);
});
}
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());
}
Aggregations