Search in sources :

Example 6 with QueryException

use of jakarta.nosql.QueryException 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 QueryException

use of jakarta.nosql.QueryException 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 8 with QueryException

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

the class GetQueryParser method query.

Stream<Value> query(String query, BucketManager manager) {
    GetQuery getQuery = provider.apply(query);
    Params params = Params.newParams();
    List<Value> values = getQuery.getKeys().stream().map(k -> Values.getValue(k, params)).collect(toList());
    if (params.isNotEmpty()) {
        throw new QueryException("To run a query with a parameter use a PrepareStatement instead.");
    }
    return values.stream().map(Value::get).map(manager::get).filter(Optional::isPresent).map(Optional::get);
}
Also used : Value(jakarta.nosql.Value) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) Stream(java.util.stream.Stream) QueryException(jakarta.nosql.QueryException) GetQueryProvider(jakarta.nosql.query.GetQuery.GetQueryProvider) Optional(java.util.Optional) BucketManager(jakarta.nosql.keyvalue.BucketManager) KeyValuePreparedStatement(jakarta.nosql.keyvalue.KeyValuePreparedStatement) GetQuery(jakarta.nosql.query.GetQuery) ServiceLoaderProvider(jakarta.nosql.ServiceLoaderProvider) Params(jakarta.nosql.Params) QueryException(jakarta.nosql.QueryException) Optional(java.util.Optional) GetQuery(jakarta.nosql.query.GetQuery) Value(jakarta.nosql.Value) Params(jakarta.nosql.Params)

Example 9 with QueryException

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

the class Values method get.

static Object get(QueryValue<?> value, Params parameters) {
    ValueType type = value.getType();
    switch(type) {
        case NUMBER:
        case STRING:
            return value.get();
        case PARAMETER:
            return parameters.add(ParamQueryValue.class.cast(value).get());
        case ARRAY:
            return Stream.of(ArrayQueryValue.class.cast(value).get()).map(v -> get(v, parameters)).collect(toList());
        case FUNCTION:
            Function function = FunctionQueryValue.class.cast(value).get();
            String name = function.getName();
            Object[] params = function.getParams();
            if ("convert".equals(name)) {
                return jakarta.nosql.Value.of(get(QueryValue.class.cast(params[0]), parameters)).get((Class<?>) params[1]);
            }
            String message = String.format("There is not support to the function: %s with parameters %s", name, Arrays.toString(params));
            throw new QueryException(message);
        case JSON:
            return JsonObjects.getDocuments(JSONQueryValue.class.cast(value).get());
        case CONDITION:
        default:
            throw new QueryException("There is not support to the value: " + type);
    }
}
Also used : ParamQueryValue(jakarta.nosql.query.ParamQueryValue) QueryValue(jakarta.nosql.query.QueryValue) Arrays(java.util.Arrays) Collectors.toList(java.util.stream.Collectors.toList) Stream(java.util.stream.Stream) ValueType(jakarta.nosql.query.ValueType) ArrayQueryValue(jakarta.nosql.query.ArrayQueryValue) QueryException(jakarta.nosql.QueryException) Function(jakarta.nosql.query.Function) FunctionQueryValue(jakarta.nosql.query.FunctionQueryValue) JSONQueryValue(jakarta.nosql.query.JSONQueryValue) Params(jakarta.nosql.Params) Function(jakarta.nosql.query.Function) QueryException(jakarta.nosql.QueryException) ValueType(jakarta.nosql.query.ValueType) FunctionQueryValue(jakarta.nosql.query.FunctionQueryValue) ParamQueryValue(jakarta.nosql.query.ParamQueryValue) QueryValue(jakarta.nosql.query.QueryValue) ArrayQueryValue(jakarta.nosql.query.ArrayQueryValue) FunctionQueryValue(jakarta.nosql.query.FunctionQueryValue) JSONQueryValue(jakarta.nosql.query.JSONQueryValue)

Example 10 with QueryException

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

the class ConditionQueryParser method getEntity.

protected DocumentEntity getEntity(ConditionQuerySupplier query, String collection, Params params, DocumentObserverParser observer) {
    DocumentEntity entity = DocumentEntity.of(collection);
    if (query.useJSONCondition()) {
        JSONQueryValue jsonValue = query.getValue().orElseThrow(() -> new QueryException("It is an invalid state of" + " either Update or Insert."));
        List<Document> documents = JsonObjects.getDocuments(jsonValue.get());
        entity.addAll(documents);
        return entity;
    }
    query.getConditions().stream().map(c -> Conditions.getCondition(c, params, observer, collection)).map(DocumentCondition::getDocument).forEach(entity::add);
    return entity;
}
Also used : QueryException(jakarta.nosql.QueryException) DocumentEntity(jakarta.nosql.document.DocumentEntity) Document(jakarta.nosql.document.Document) JSONQueryValue(jakarta.nosql.query.JSONQueryValue)

Aggregations

QueryException (jakarta.nosql.QueryException)17 Params (jakarta.nosql.Params)14 Stream (java.util.stream.Stream)9 Collectors.toList (java.util.stream.Collectors.toList)7 ServiceLoaderProvider (jakarta.nosql.ServiceLoaderProvider)6 List (java.util.List)6 ColumnEntity (jakarta.nosql.column.ColumnEntity)5 DocumentEntity (jakarta.nosql.document.DocumentEntity)5 Value (jakarta.nosql.Value)4 Function (jakarta.nosql.query.Function)4 JSONQueryValue (jakarta.nosql.query.JSONQueryValue)4 Collectors (java.util.stream.Collectors)4 ArrayQueryValue (jakarta.nosql.query.ArrayQueryValue)3 Objects (java.util.Objects)3 Sort (jakarta.nosql.Sort)2 ColumnCondition (jakarta.nosql.column.ColumnCondition)2 ColumnFamilyManager (jakarta.nosql.column.ColumnFamilyManager)2 ColumnObserverParser (jakarta.nosql.column.ColumnObserverParser)2 ColumnPreparedStatement (jakarta.nosql.column.ColumnPreparedStatement)2 DocumentCollectionManager (jakarta.nosql.document.DocumentCollectionManager)2