use of io.micronaut.data.model.runtime.PreparedQuery in project micronaut-coherence by micronaut-projects.
the class DefaultCoherenceRepositoryOperations method createBindingMap.
/**
* Creates a map of binding parameters names and their values.
*
* @param preparedQuery the {@link PreparedQuery} to build the binding map from
*
* @return the parameters necessary to execute a CohQL statement
*/
protected Map<String, Object> createBindingMap(PreparedQuery preparedQuery) {
List<QueryParameterBinding> bindings = preparedQuery.getQueryBindings();
int bindingsLen = bindings.size();
String[] bindingNames = new String[bindingsLen];
Integer[] bindingIndexes = new Integer[bindingsLen];
for (int i = 0; i < bindingsLen; i++) {
QueryParameterBinding binding = bindings.get(i);
bindingNames[i] = binding.getName();
bindingIndexes[i] = binding.getParameterIndex();
}
Object[] bindingValues = preparedQuery.getParameterArray();
return IntStream.range(0, bindingNames.length).boxed().collect(Collectors.toMap(i -> bindingNames[i], i -> bindingValues[bindingIndexes[i]]));
}
use of io.micronaut.data.model.runtime.PreparedQuery in project micronaut-data by micronaut-projects.
the class HibernateJpaOperations method findAll.
@NonNull
@Override
public <T, R> Iterable<R> findAll(@NonNull PreparedQuery<T, R> preparedQuery) {
return transactionOperations.executeRead(status -> {
Session entityManager = sessionFactory.getCurrentSession();
String queryStr = preparedQuery.getQuery();
Pageable pageable = preparedQuery.getPageable();
if (pageable != Pageable.UNPAGED) {
Sort sort = pageable.getSort();
if (sort.isSorted()) {
queryStr += QUERY_BUILDER.buildOrderBy(queryStr, getEntity(preparedQuery.getRootEntity()), sort).getQuery();
}
}
if (preparedQuery.isDtoProjection()) {
Query<Tuple> q;
if (preparedQuery.isNative()) {
q = entityManager.createNativeQuery(queryStr, Tuple.class);
} else if (queryStr.toLowerCase(Locale.ENGLISH).startsWith("select new ")) {
Class<R> wrapperType = ReflectionUtils.getWrapperType(preparedQuery.getResultType());
Query<R> query = entityManager.createQuery(queryStr, wrapperType);
bindPreparedQuery(query, preparedQuery, entityManager, queryStr);
return query.list();
} else {
q = entityManager.createQuery(queryStr, Tuple.class);
}
bindPreparedQuery(q, preparedQuery, entityManager, queryStr);
return q.stream().map(tuple -> {
Set<String> properties = tuple.getElements().stream().map(TupleElement::getAlias).collect(Collectors.toCollection(() -> new TreeSet<>(String.CASE_INSENSITIVE_ORDER)));
return (new BeanIntrospectionMapper<Tuple, R>() {
@Override
public Object read(Tuple tuple1, String alias) {
if (!properties.contains(alias)) {
return null;
}
return tuple1.get(alias);
}
@Override
public ConversionService<?> getConversionService() {
return dataConversionService;
}
}).map(tuple, preparedQuery.getResultType());
}).collect(Collectors.toList());
} else {
Class<R> wrapperType = ReflectionUtils.getWrapperType(preparedQuery.getResultType());
Query<R> q;
if (preparedQuery.isNative()) {
if (DataType.ENTITY.equals(preparedQuery.getResultDataType())) {
q = entityManager.createNativeQuery(queryStr, wrapperType);
} else {
q = entityManager.createNativeQuery(queryStr);
}
} else {
q = entityManager.createQuery(queryStr, wrapperType);
}
bindPreparedQuery(q, preparedQuery, entityManager, queryStr);
return q.list();
}
});
}
use of io.micronaut.data.model.runtime.PreparedQuery in project micronaut-data by micronaut-projects.
the class DefaultFindSliceReactiveInterceptor method intercept.
@Override
public Object intercept(RepositoryMethodKey methodKey, MethodInvocationContext<Object, Object> context) {
if (context.hasAnnotation(Query.class)) {
PreparedQuery<Object, Object> preparedQuery = (PreparedQuery<Object, Object>) prepareQuery(methodKey, context);
Pageable pageable = preparedQuery.getPageable();
Mono<Slice<Object>> publisher = Flux.from(reactiveOperations.findAll(preparedQuery)).collectList().map(objects -> Slice.of(objects, pageable));
return Publishers.convertPublisher(publisher, context.getReturnType().getType());
} else {
PagedQuery<Object> pagedQuery = getPagedQuery(context);
Mono<? extends Slice<?>> result = Flux.from(reactiveOperations.findAll(pagedQuery)).collectList().map(objects -> Slice.of(objects, pagedQuery.getPageable()));
return Publishers.convertPublisher(result, context.getReturnType().getType());
}
}
use of io.micronaut.data.model.runtime.PreparedQuery in project micronaut-data by micronaut-projects.
the class DefaultMongoRepositoryOperations method getCount.
private <T, R> R getCount(ClientSession clientSession, MongoPreparedQuery<T, R, MongoDatabase> preparedQuery) {
Class<R> resultType = preparedQuery.getResultType();
RuntimePersistentEntity<T> persistentEntity = preparedQuery.getRuntimePersistentEntity();
MongoDatabase database = preparedQuery.getDatabase();
if (preparedQuery.isAggregate()) {
MongoAggregation aggregation = preparedQuery.getAggregation();
if (QUERY_LOG.isDebugEnabled()) {
QUERY_LOG.debug("Executing Mongo 'aggregate' with pipeline: {}", aggregation.getPipeline().stream().map(e -> e.toBsonDocument().toJson()).collect(Collectors.toList()));
}
R result = aggregate(clientSession, preparedQuery, BsonDocument.class).map(bsonDocument -> convertResult(database.getCodecRegistry(), resultType, bsonDocument, false)).first();
if (result == null) {
result = conversionService.convertRequired(0, resultType);
}
return result;
} else {
MongoFind find = preparedQuery.getFind();
MongoFindOptions options = find.getOptions();
Bson filter = options == null ? null : options.getFilter();
filter = filter == null ? new BsonDocument() : filter;
if (QUERY_LOG.isDebugEnabled()) {
QUERY_LOG.debug("Executing Mongo 'countDocuments' with filter: {}", filter.toBsonDocument().toJson());
}
long count = getCollection(database, persistentEntity, BsonDocument.class).countDocuments(clientSession, filter);
return conversionService.convertRequired(count, resultType);
}
}
use of io.micronaut.data.model.runtime.PreparedQuery in project micronaut-data by micronaut-projects.
the class DefaultMongoRepositoryOperations method findAllAggregated.
private <T, R> Iterable<R> findAllAggregated(ClientSession clientSession, MongoPreparedQuery<T, R, MongoDatabase> preparedQuery, boolean stream) {
Pageable pageable = preparedQuery.getPageable();
int limit = pageable == Pageable.UNPAGED ? -1 : pageable.getSize();
Class<T> type = preparedQuery.getRootEntity();
Class<R> resultType = preparedQuery.getResultType();
MongoIterable<R> aggregate;
if (!resultType.isAssignableFrom(type)) {
MongoDatabase database = preparedQuery.getDatabase();
aggregate = aggregate(clientSession, preparedQuery, BsonDocument.class).map(result -> convertResult(database.getCodecRegistry(), resultType, result, preparedQuery.isDtoProjection()));
} else {
aggregate = aggregate(clientSession, preparedQuery, resultType);
}
return stream ? aggregate : aggregate.into(new ArrayList<>(limit > 0 ? limit : 20));
}
Aggregations