use of io.micronaut.data.model.runtime.PreparedQuery in project micronaut-data by micronaut-projects.
the class DefaultDeleteAllReactiveInterceptor method intercept.
@Override
public Object intercept(RepositoryMethodKey methodKey, MethodInvocationContext<Object, Object> context) {
Argument<Object> arg = context.getReturnType().asArgument();
Optional<Iterable<Object>> deleteEntities = findEntitiesParameter(context, Object.class);
Optional<Object> deleteEntity = findEntityParameter(context, Object.class);
Publisher publisher;
if (!deleteEntity.isPresent() && !deleteEntities.isPresent()) {
PreparedQuery<?, Number> preparedQuery = (PreparedQuery<?, Number>) prepareQuery(methodKey, context);
publisher = reactiveOperations.executeDelete(preparedQuery);
} else if (deleteEntity.isPresent()) {
publisher = reactiveOperations.delete(getDeleteOperation(context, deleteEntity.get()));
} else {
publisher = reactiveOperations.deleteAll(getDeleteBatchOperation(context, deleteEntities.get()));
}
return Publishers.convertPublisher(publisher, arg.getType());
}
use of io.micronaut.data.model.runtime.PreparedQuery in project micronaut-data by micronaut-projects.
the class HibernateJpaOperations method findOne.
@Nullable
@Override
public <T, R> R findOne(@NonNull PreparedQuery<T, R> preparedQuery) {
return transactionOperations.executeRead(status -> {
Class<R> resultType = preparedQuery.getResultType();
String query = preparedQuery.getQuery();
Session currentSession = sessionFactory.getCurrentSession();
if (preparedQuery.isDtoProjection()) {
Query<Tuple> q;
if (preparedQuery.isNative()) {
q = currentSession.createNativeQuery(query, Tuple.class);
} else if (query.toLowerCase(Locale.ENGLISH).startsWith("select new ")) {
Query<R> dtoQuery = currentSession.createQuery(query, resultType);
bindParameters(dtoQuery, preparedQuery, query);
bindQueryHints(dtoQuery, preparedQuery, currentSession);
return dtoQuery.uniqueResult();
} else {
q = currentSession.createQuery(query, Tuple.class);
}
bindParameters(q, preparedQuery, query);
bindQueryHints(q, preparedQuery, currentSession);
Tuple tuple = first(q);
if (tuple != null) {
return (new BeanIntrospectionMapper<Tuple, R>() {
@Override
public Object read(Tuple tuple1, String alias) {
return tuple1.get(alias);
}
@Override
public ConversionService<?> getConversionService() {
return dataConversionService;
}
}).map(tuple, resultType);
}
return null;
} else {
Query<R> q;
if (preparedQuery.isNative()) {
if (DataType.ENTITY.equals(preparedQuery.getResultDataType())) {
q = currentSession.createNativeQuery(query, resultType);
} else {
q = currentSession.createNativeQuery(query);
}
} else {
q = currentSession.createQuery(query, resultType);
}
bindParameters(q, preparedQuery, query);
bindQueryHints(q, preparedQuery, currentSession);
return first(q);
}
});
}
use of io.micronaut.data.model.runtime.PreparedQuery in project micronaut-data by micronaut-projects.
the class DefaultMongoRepositoryOperations method findOneAggregated.
private <T, R> R findOneAggregated(ClientSession clientSession, MongoPreparedQuery<T, R, MongoDatabase> preparedQuery) {
MongoDatabase database = preparedQuery.getDatabase();
Class<T> type = preparedQuery.getRootEntity();
Class<R> resultType = preparedQuery.getResultType();
if (!resultType.isAssignableFrom(type)) {
BsonDocument result = aggregate(clientSession, preparedQuery, BsonDocument.class).first();
return convertResult(database.getCodecRegistry(), resultType, result, preparedQuery.isDtoProjection());
}
return aggregate(clientSession, preparedQuery).map(r -> {
RuntimePersistentEntity<T> persistentEntity = preparedQuery.getRuntimePersistentEntity();
if (type.isInstance(r)) {
return (R) triggerPostLoad(preparedQuery.getAnnotationMetadata(), persistentEntity, type.cast(r));
}
return r;
}).first();
}
use of io.micronaut.data.model.runtime.PreparedQuery in project micronaut-data by micronaut-projects.
the class DefaultMongoRepositoryOperations method findAllFiltered.
private <T, R> Iterable<R> findAllFiltered(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> findIterable;
if (!resultType.isAssignableFrom(type)) {
MongoDatabase database = preparedQuery.getDatabase();
findIterable = find(clientSession, preparedQuery, BsonDocument.class).map(result -> convertResult(database.getCodecRegistry(), resultType, result, preparedQuery.isDtoProjection()));
} else {
findIterable = find(clientSession, preparedQuery);
}
return stream ? findIterable : findIterable.into(new ArrayList<>(limit > 0 ? limit : 20));
}
use of io.micronaut.data.model.runtime.PreparedQuery in project micronaut-data by micronaut-projects.
the class DefaultReactiveMongoRepositoryOperations method getCount.
private <T, R> Mono<R> getCount(ClientSession clientSession, MongoPreparedQuery<T, R, MongoDatabase> preparedQuery) {
Class<R> resultType = preparedQuery.getResultType();
MongoDatabase database = preparedQuery.getDatabase();
RuntimePersistentEntity<T> persistentEntity = preparedQuery.getRuntimePersistentEntity();
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()));
}
return Mono.from(aggregate(clientSession, preparedQuery, BsonDocument.class).first()).map(bsonDocument -> convertResult(database.getCodecRegistry(), resultType, bsonDocument, false)).switchIfEmpty(Mono.defer(() -> Mono.just(conversionService.convertRequired(0, resultType))));
} 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());
}
return Mono.from(getCollection(database, persistentEntity, BsonDocument.class).countDocuments(clientSession, filter)).map(count -> conversionService.convertRequired(count, resultType));
}
}
Aggregations