Search in sources :

Example 1 with DocumentObserverParser

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

the class DeleteQueryParser method getQuery.

private DocumentDeleteQuery getQuery(String query, DocumentObserverParser observer) {
    DeleteQuery deleteQuery = deleteQueryProvider.apply(query);
    String collection = observer.fireEntity(deleteQuery.getEntity());
    List<String> documents = deleteQuery.getFields().stream().map(f -> observer.fireField(collection, f)).collect(Collectors.toList());
    DocumentCondition condition = null;
    Params params = Params.newParams();
    if (deleteQuery.getWhere().isPresent()) {
        condition = deleteQuery.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 DefaultDocumentDeleteQuery(collection, condition, documents);
}
Also used : DocumentCollectionManager(jakarta.nosql.document.DocumentCollectionManager) DocumentCondition(jakarta.nosql.document.DocumentCondition) DocumentDeleteQuery(jakarta.nosql.document.DocumentDeleteQuery) DocumentPreparedStatement(jakarta.nosql.document.DocumentPreparedStatement) DocumentDeleteQueryParams(jakarta.nosql.document.DocumentDeleteQueryParams) DocumentEntity(jakarta.nosql.document.DocumentEntity) Collectors(java.util.stream.Collectors) ServiceLoaderProvider(jakarta.nosql.ServiceLoaderProvider) DeleteQueryConverter(jakarta.nosql.document.DeleteQueryConverter) DeleteQuery(jakarta.nosql.query.DeleteQuery) Objects(java.util.Objects) List(java.util.List) DeleteQueryProvider(jakarta.nosql.query.DeleteQuery.DeleteQueryProvider) Stream(java.util.stream.Stream) QueryException(jakarta.nosql.QueryException) DocumentObserverParser(jakarta.nosql.document.DocumentObserverParser) Params(jakarta.nosql.Params) QueryException(jakarta.nosql.QueryException) DocumentDeleteQueryParams(jakarta.nosql.document.DocumentDeleteQueryParams) Params(jakarta.nosql.Params) DocumentDeleteQuery(jakarta.nosql.document.DocumentDeleteQuery) DeleteQuery(jakarta.nosql.query.DeleteQuery) DocumentCondition(jakarta.nosql.document.DocumentCondition)

Example 2 with DocumentObserverParser

use of jakarta.nosql.document.DocumentObserverParser 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);
}
Also used : SelectQuery(jakarta.nosql.query.SelectQuery) DocumentCollectionManager(jakarta.nosql.document.DocumentCollectionManager) SelectQuery(jakarta.nosql.query.SelectQuery) DocumentCondition(jakarta.nosql.document.DocumentCondition) DocumentPreparedStatement(jakarta.nosql.document.DocumentPreparedStatement) DocumentEntity(jakarta.nosql.document.DocumentEntity) Collectors(java.util.stream.Collectors) ServiceLoaderProvider(jakarta.nosql.ServiceLoaderProvider) DocumentQueryParams(jakarta.nosql.document.DocumentQueryParams) Objects(java.util.Objects) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) Stream(java.util.stream.Stream) DocumentQuery(jakarta.nosql.document.DocumentQuery) SelectQueryConverter(jakarta.nosql.document.SelectQueryConverter) QueryException(jakarta.nosql.QueryException) SelectQueryProvider(jakarta.nosql.query.SelectQuery.SelectQueryProvider) Sort(jakarta.nosql.Sort) DocumentObserverParser(jakarta.nosql.document.DocumentObserverParser) Params(jakarta.nosql.Params) QueryException(jakarta.nosql.QueryException) Sort(jakarta.nosql.Sort) DocumentQueryParams(jakarta.nosql.document.DocumentQueryParams) Params(jakarta.nosql.Params) DocumentCondition(jakarta.nosql.document.DocumentCondition)

Example 3 with DocumentObserverParser

use of jakarta.nosql.document.DocumentObserverParser 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);
}
Also used : DocumentCollectionManager(jakarta.nosql.document.DocumentCollectionManager) SelectQuery(jakarta.nosql.query.SelectQuery) DocumentCondition(jakarta.nosql.document.DocumentCondition) DocumentPreparedStatement(jakarta.nosql.document.DocumentPreparedStatement) DocumentEntity(jakarta.nosql.document.DocumentEntity) Collectors(java.util.stream.Collectors) ServiceLoaderProvider(jakarta.nosql.ServiceLoaderProvider) DocumentQueryParams(jakarta.nosql.document.DocumentQueryParams) Objects(java.util.Objects) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) Stream(java.util.stream.Stream) DocumentQuery(jakarta.nosql.document.DocumentQuery) SelectQueryConverter(jakarta.nosql.document.SelectQueryConverter) QueryException(jakarta.nosql.QueryException) SelectQueryProvider(jakarta.nosql.query.SelectQuery.SelectQueryProvider) Sort(jakarta.nosql.Sort) DocumentObserverParser(jakarta.nosql.document.DocumentObserverParser) Params(jakarta.nosql.Params) Sort(jakarta.nosql.Sort) DocumentCondition(jakarta.nosql.document.DocumentCondition)

Example 4 with DocumentObserverParser

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

the class SelectQueryParserTest method shouldReturnParserQuery12.

@ParameterizedTest(name = "Should parser the query {0}")
@ValueSource(strings = { "select  * from God where stamina >= -10.23" })
public void shouldReturnParserQuery12(String query) {
    ArgumentCaptor<DocumentQuery> captor = ArgumentCaptor.forClass(DocumentQuery.class);
    DocumentObserverParser observer = new DocumentObserverParser() {
    };
    parser.query(query, documentCollection, observer);
    Mockito.verify(documentCollection).select(captor.capture());
    DocumentQuery documentQuery = captor.getValue();
    checkBaseQuery(documentQuery, 0L, 0L);
    assertTrue(documentQuery.getCondition().isPresent());
    DocumentCondition condition = documentQuery.getCondition().get();
    assertEquals(Condition.GREATER_EQUALS_THAN, condition.getCondition());
    assertEquals(Document.of("stamina", -10.23), condition.getDocument());
}
Also used : DocumentQuery(jakarta.nosql.document.DocumentQuery) DocumentObserverParser(jakarta.nosql.document.DocumentObserverParser) DocumentCondition(jakarta.nosql.document.DocumentCondition) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 5 with DocumentObserverParser

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

the class SelectQueryParserTest method shouldReturnParserQuery8.

@ParameterizedTest(name = "Should parser the query {0}")
@ValueSource(strings = { "select * from God limit 12" })
public void shouldReturnParserQuery8(String query) {
    ArgumentCaptor<DocumentQuery> captor = ArgumentCaptor.forClass(DocumentQuery.class);
    DocumentObserverParser observer = new DocumentObserverParser() {
    };
    parser.query(query, documentCollection, observer);
    Mockito.verify(documentCollection).select(captor.capture());
    DocumentQuery documentQuery = captor.getValue();
    checkBaseQuery(documentQuery, 12L, 0L);
    assertFalse(documentQuery.getCondition().isPresent());
}
Also used : DocumentQuery(jakarta.nosql.document.DocumentQuery) DocumentObserverParser(jakarta.nosql.document.DocumentObserverParser) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

DocumentObserverParser (jakarta.nosql.document.DocumentObserverParser)7 DocumentCondition (jakarta.nosql.document.DocumentCondition)5 DocumentQuery (jakarta.nosql.document.DocumentQuery)5 Params (jakarta.nosql.Params)4 QueryException (jakarta.nosql.QueryException)4 ServiceLoaderProvider (jakarta.nosql.ServiceLoaderProvider)4 DocumentCollectionManager (jakarta.nosql.document.DocumentCollectionManager)4 DocumentEntity (jakarta.nosql.document.DocumentEntity)4 DocumentPreparedStatement (jakarta.nosql.document.DocumentPreparedStatement)4 List (java.util.List)4 Objects (java.util.Objects)4 Collectors (java.util.stream.Collectors)4 Stream (java.util.stream.Stream)4 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)3 ValueSource (org.junit.jupiter.params.provider.ValueSource)3 Sort (jakarta.nosql.Sort)2 DeleteQueryConverter (jakarta.nosql.document.DeleteQueryConverter)2 DocumentDeleteQuery (jakarta.nosql.document.DocumentDeleteQuery)2 DocumentDeleteQueryParams (jakarta.nosql.document.DocumentDeleteQueryParams)2 DocumentQueryParams (jakarta.nosql.document.DocumentQueryParams)2