use of jakarta.nosql.document.DocumentQuery in project jnosql-diana by eclipse.
the class SelectQueryParser method getDocumentQuery.
private DocumentQuery getDocumentQuery(String query, DocumentObserverParser observer) {
SelectQuery selectQuery = selectQueryProvider.apply(query);
String collection = observer.fireEntity(selectQuery.getEntity());
long limit = selectQuery.getLimit();
long skip = selectQuery.getSkip();
List<String> documents = selectQuery.getFields().stream().map(f -> observer.fireField(collection, f)).collect(Collectors.toList());
List<Sort> sorts = selectQuery.getOrderBy().stream().map(s -> toSort(s, observer, collection)).collect(toList());
DocumentCondition condition = null;
Params params = Params.newParams();
if (selectQuery.getWhere().isPresent()) {
condition = selectQuery.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 DefaultDocumentQuery(limit, skip, collection, documents, sorts, condition);
}
use of jakarta.nosql.document.DocumentQuery in project jnosql-diana by eclipse.
the class SelectQueryParser method getDocumentQuery.
private DocumentQuery getDocumentQuery(Params params, SelectQuery selectQuery, DocumentObserverParser observer) {
String collection = observer.fireEntity(selectQuery.getEntity());
long limit = selectQuery.getLimit();
long skip = selectQuery.getSkip();
List<String> documents = selectQuery.getFields().stream().map(f -> observer.fireField(collection, f)).collect(Collectors.toList());
List<Sort> sorts = selectQuery.getOrderBy().stream().map(s -> toSort(s, observer, collection)).collect(toList());
DocumentCondition condition = null;
if (selectQuery.getWhere().isPresent()) {
condition = selectQuery.getWhere().map(c -> Conditions.getCondition(c, params, observer, collection)).get();
}
return new DefaultDocumentQuery(limit, skip, collection, documents, sorts, condition);
}
use of jakarta.nosql.document.DocumentQuery in project jnosql-diana by eclipse.
the class DefaultDocumentQueryParserTest method shouldExecutePrepareStatement2.
@ParameterizedTest(name = "Should parser the query {0}")
@ValueSource(strings = { "select * from God where age = @age" })
public void shouldExecutePrepareStatement2(String query) {
ArgumentCaptor<DocumentQuery> captor = ArgumentCaptor.forClass(DocumentQuery.class);
DocumentPreparedStatement prepare = parser.prepare(query, manager, DocumentObserverParser.EMPTY);
prepare.bind("age", 12);
prepare.getResult();
Mockito.verify(manager).select(captor.capture());
DocumentQuery documentQuery = captor.getValue();
DocumentCondition documentCondition = documentQuery.getCondition().get();
Document document = documentCondition.getDocument();
assertEquals(Condition.EQUALS, documentCondition.getCondition());
assertEquals("age", document.getName());
assertEquals(12, document.get());
}
use of jakarta.nosql.document.DocumentQuery in project jnosql-diana by eclipse.
the class DefaultDocumentQueryParserTest method shouldReturnParserQuery.
@ParameterizedTest(name = "Should parser the query {0}")
@ValueSource(strings = { "select * from God" })
public void shouldReturnParserQuery(String query) {
ArgumentCaptor<DocumentQuery> captor = ArgumentCaptor.forClass(DocumentQuery.class);
parser.query(query, manager, DocumentObserverParser.EMPTY);
Mockito.verify(manager).select(captor.capture());
DocumentQuery documentQuery = captor.getValue();
assertTrue(documentQuery.getDocuments().isEmpty());
assertTrue(documentQuery.getSorts().isEmpty());
assertEquals(0L, documentQuery.getLimit());
assertEquals(0L, documentQuery.getSkip());
assertEquals("God", documentQuery.getDocumentCollection());
assertFalse(documentQuery.getCondition().isPresent());
}
use of jakarta.nosql.document.DocumentQuery 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());
}
Aggregations