Search in sources :

Example 1 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.getColumns(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 2 with QueryException

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

the class DeleteQueryParser method getQuery.

private ColumnDeleteQuery getQuery(String query, ColumnObserverParser observer) {
    DeleteQuery deleteQuery = deleteQueryProvider.apply(query);
    String columnFamily = observer.fireEntity(deleteQuery.getEntity());
    List<String> columns = deleteQuery.getFields().stream().map(f -> observer.fireField(columnFamily, f)).collect(Collectors.toList());
    ColumnCondition condition = null;
    Params params = Params.newParams();
    if (deleteQuery.getWhere().isPresent()) {
        condition = deleteQuery.getWhere().map(c -> Conditions.getCondition(c, params, observer, columnFamily)).get();
    }
    if (params.isNotEmpty()) {
        throw new QueryException("To run a query with a parameter use a PrepareStatement instead.");
    }
    return new DefaultColumnDeleteQuery(columnFamily, condition, columns);
}
Also used : ColumnEntity(jakarta.nosql.column.ColumnEntity) ColumnDeleteQuery(jakarta.nosql.column.ColumnDeleteQuery) DeleteQueryConverter(jakarta.nosql.column.DeleteQueryConverter) ColumnFamilyManager(jakarta.nosql.column.ColumnFamilyManager) ColumnCondition(jakarta.nosql.column.ColumnCondition) Collectors(java.util.stream.Collectors) ServiceLoaderProvider(jakarta.nosql.ServiceLoaderProvider) DeleteQuery(jakarta.nosql.query.DeleteQuery) List(java.util.List) DeleteQueryProvider(jakarta.nosql.query.DeleteQuery.DeleteQueryProvider) Stream(java.util.stream.Stream) QueryException(jakarta.nosql.QueryException) Objects.requireNonNull(java.util.Objects.requireNonNull) ColumnDeleteQueryParams(jakarta.nosql.column.ColumnDeleteQueryParams) ColumnPreparedStatement(jakarta.nosql.column.ColumnPreparedStatement) ColumnObserverParser(jakarta.nosql.column.ColumnObserverParser) Params(jakarta.nosql.Params) QueryException(jakarta.nosql.QueryException) ColumnDeleteQueryParams(jakarta.nosql.column.ColumnDeleteQueryParams) Params(jakarta.nosql.Params) ColumnDeleteQuery(jakarta.nosql.column.ColumnDeleteQuery) DeleteQuery(jakarta.nosql.query.DeleteQuery) ColumnCondition(jakarta.nosql.column.ColumnCondition)

Example 3 with QueryException

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

the class SelectQueryParser method getColumnQuery.

private ColumnQuery getColumnQuery(String query, ColumnObserverParser observer) {
    SelectQuery selectQuery = selectQueryProvider.apply(query);
    String columnFamily = observer.fireEntity(selectQuery.getEntity());
    long limit = selectQuery.getLimit();
    long skip = selectQuery.getSkip();
    List<String> columns = selectQuery.getFields().stream().map(f -> observer.fireField(columnFamily, f)).collect(Collectors.toList());
    List<Sort> sorts = selectQuery.getOrderBy().stream().map(s -> toSort(s, observer, columnFamily)).collect(toList());
    ColumnCondition condition = null;
    Params params = Params.newParams();
    if (selectQuery.getWhere().isPresent()) {
        condition = selectQuery.getWhere().map(c -> Conditions.getCondition(c, params, observer, columnFamily)).get();
    }
    if (params.isNotEmpty()) {
        throw new QueryException("To run a query with a parameter use a PrepareStatement instead.");
    }
    return new DefaultColumnQuery(limit, skip, columnFamily, columns, sorts, condition);
}
Also used : SelectQuery(jakarta.nosql.query.SelectQuery) ColumnEntity(jakarta.nosql.column.ColumnEntity) SelectQuery(jakarta.nosql.query.SelectQuery) ColumnFamilyManager(jakarta.nosql.column.ColumnFamilyManager) ColumnQueryParams(jakarta.nosql.column.ColumnQueryParams) ColumnCondition(jakarta.nosql.column.ColumnCondition) Collectors(java.util.stream.Collectors) ServiceLoaderProvider(jakarta.nosql.ServiceLoaderProvider) SelectQueryConverter(jakarta.nosql.column.SelectQueryConverter) Objects(java.util.Objects) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) Stream(java.util.stream.Stream) QueryException(jakarta.nosql.QueryException) SelectQueryProvider(jakarta.nosql.query.SelectQuery.SelectQueryProvider) ColumnPreparedStatement(jakarta.nosql.column.ColumnPreparedStatement) ColumnQuery(jakarta.nosql.column.ColumnQuery) Sort(jakarta.nosql.Sort) ColumnObserverParser(jakarta.nosql.column.ColumnObserverParser) Params(jakarta.nosql.Params) QueryException(jakarta.nosql.QueryException) Sort(jakarta.nosql.Sort) ColumnQueryParams(jakarta.nosql.column.ColumnQueryParams) Params(jakarta.nosql.Params) ColumnCondition(jakarta.nosql.column.ColumnCondition)

Example 4 with QueryException

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

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

the class UpdateQueryParser method query.

Stream<DocumentEntity> query(String query, DocumentCollectionManager collectionManager, DocumentObserverParser observer) {
    UpdateQuery updateQuery = supplier.apply(query);
    Params params = Params.newParams();
    DocumentEntity entity = getEntity(params, updateQuery, observer);
    if (params.isNotEmpty()) {
        throw new QueryException("To run a query with a parameter use a PrepareStatement instead.");
    }
    return Stream.of(collectionManager.update(entity));
}
Also used : QueryException(jakarta.nosql.QueryException) DocumentEntity(jakarta.nosql.document.DocumentEntity) UpdateQuery(jakarta.nosql.query.UpdateQuery) Params(jakarta.nosql.Params)

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