use of io.micronaut.data.annotation.Query in project micronaut-data by micronaut-projects.
the class MongoExecutorQueryMethodMatcher method match.
@Override
public MethodMatch match(MethodMatchContext matchContext) {
Optional<ClassElement> executor = matchContext.getVisitorContext().getClassElement(MongoAnnotations.EXECUTOR_REPOSITORY);
if (executor.isPresent() && executor.get().isAssignable(matchContext.getRepositoryClass())) {
return null;
}
String methodName = matchContext.getMethodElement().getName();
if ("findAll".equals(methodName) || "findOne".equals(methodName)) {
ParameterElement[] parameters = matchContext.getParameters();
switch(parameters.length) {
case 1:
ParameterElement parameter = parameters[0];
if (isBson(parameter)) {
return new MongoQueryExecutorMatch(DataMethod.OperationType.QUERY) {
@Override
protected void apply(MethodMatchInfo matchInfo) {
matchInfo.addParameterRole(MongoAnnotations.FILTER_ROLE, parameter.getName());
}
};
} else if (isPipeline(parameter)) {
return new MongoQueryExecutorMatch(DataMethod.OperationType.QUERY) {
@Override
protected void apply(MethodMatchInfo matchInfo) {
matchInfo.addParameterRole(MongoAnnotations.PIPELINE_ROLE, parameter.getName());
}
};
} else if (parameter.getType().isAssignable(MongoAnnotations.FIND_OPTIONS_BEAN)) {
return new MongoQueryExecutorMatch(DataMethod.OperationType.QUERY) {
@Override
protected void apply(MethodMatchInfo matchInfo) {
matchInfo.addParameterRole(MongoAnnotations.FIND_OPTIONS_ROLE, parameter.getName());
}
};
}
break;
case 2:
ParameterElement parameter1 = parameters[0];
ParameterElement parameter2 = parameters[1];
if (isBson(parameter1) && parameter2.getType().isAssignable(MongoAnnotations.FIND_OPTIONS_BEAN)) {
return new MongoQueryExecutorMatch(DataMethod.OperationType.QUERY) {
@Override
protected void apply(MethodMatchInfo matchInfo) {
matchInfo.addParameterRole(MongoAnnotations.FILTER_ROLE, parameter1.getName());
matchInfo.addParameterRole(MongoAnnotations.FIND_OPTIONS_ROLE, parameter2.getName());
}
};
} else if (isPipeline(parameter1) && parameter2.getType().isAssignable(MongoAnnotations.AGGREGATION_OPTIONS_BEAN)) {
return new MongoQueryExecutorMatch(DataMethod.OperationType.QUERY) {
@Override
protected void apply(MethodMatchInfo matchInfo) {
matchInfo.addParameterRole(MongoAnnotations.PIPELINE_ROLE, parameter1.getName());
matchInfo.addParameterRole(MongoAnnotations.AGGREGATE_OPTIONS_ROLE, parameter2.getName());
}
};
}
if ("findOne".equals(methodName)) {
break;
}
ParameterElement p1 = parameters[0];
ParameterElement p2 = parameters[1];
if (isBson(p1) && p2.getType().isAssignable(MongoAnnotations.PAGEABLE_BEAN)) {
return new MongoQueryExecutorMatch(DataMethod.OperationType.QUERY) {
@Override
protected void apply(MethodMatchInfo matchInfo) {
matchInfo.addParameterRole(MongoAnnotations.FILTER_ROLE, p1.getName());
matchInfo.addParameterRole(TypeRole.PAGEABLE, p2.getName());
// Fake query to have stored query
matchContext.getMethodElement().annotate(Query.class, builder -> {
builder.member(DataMethod.META_MEMBER_COUNT_QUERY, "{}");
});
}
};
} else if (p1.getType().isAssignable(MongoAnnotations.FIND_OPTIONS_BEAN) && p2.getType().isAssignable(MongoAnnotations.PAGEABLE_BEAN)) {
return new MongoQueryExecutorMatch(DataMethod.OperationType.QUERY) {
@Override
protected void apply(MethodMatchInfo matchInfo) {
matchInfo.addParameterRole(MongoAnnotations.FIND_OPTIONS_ROLE, p1.getName());
matchInfo.addParameterRole(TypeRole.PAGEABLE, p2.getName());
// Fake query to have stored query
matchContext.getMethodElement().annotate(Query.class, builder -> {
builder.member(DataMethod.META_MEMBER_COUNT_QUERY, "{}");
});
}
};
}
break;
default:
return null;
}
}
if ("count".equals(methodName)) {
ParameterElement[] parameters = matchContext.getParameters();
if (parameters.length == 1) {
ParameterElement parameter = parameters[0];
if (isBson(parameter)) {
return new MongoQueryExecutorMatch(DataMethod.OperationType.COUNT) {
@Override
protected void apply(MethodMatchInfo matchInfo) {
matchInfo.addParameterRole(MongoAnnotations.FILTER_ROLE, parameter.getName());
}
};
}
}
return null;
}
if ("deleteAll".equals(methodName)) {
ParameterElement[] parameters = matchContext.getParameters();
switch(parameters.length) {
case 1:
ParameterElement parameter = parameters[0];
if (isBson(parameter)) {
return new MongoQueryExecutorMatch(DataMethod.OperationType.DELETE) {
@Override
protected void apply(MethodMatchInfo matchInfo) {
matchInfo.addParameterRole(MongoAnnotations.FILTER_ROLE, parameter.getName());
}
};
}
break;
case 2:
ParameterElement parameter1 = parameters[0];
ParameterElement parameter2 = parameters[1];
if (isBson(parameter1) && parameter2.getType().isAssignable(MongoAnnotations.DELETE_OPTIONS_BEAN)) {
return new MongoQueryExecutorMatch(DataMethod.OperationType.DELETE) {
@Override
protected void apply(MethodMatchInfo matchInfo) {
matchInfo.addParameterRole(MongoAnnotations.FILTER_ROLE, parameter1.getName());
matchInfo.addParameterRole(MongoAnnotations.DELETE_OPTIONS_ROLE, parameter2.getName());
}
};
}
break;
default:
return null;
}
}
if ("updateAll".equals(methodName)) {
ParameterElement[] parameters = matchContext.getParameters();
switch(parameters.length) {
case 2:
ParameterElement parameter1 = parameters[0];
ParameterElement parameter2 = parameters[1];
if (isBson(parameter1) && isBson(parameter2)) {
return new MongoQueryExecutorMatch(DataMethod.OperationType.UPDATE) {
@Override
protected void apply(MethodMatchInfo matchInfo) {
matchInfo.addParameterRole(MongoAnnotations.FILTER_ROLE, parameter1.getName());
matchInfo.addParameterRole(MongoAnnotations.UPDATE_ROLE, parameter2.getName());
}
};
}
break;
case 3:
ParameterElement filter = parameters[0];
ParameterElement update = parameters[1];
ParameterElement options = parameters[2];
if (isBson(filter) && isBson(update) && options.getType().isAssignable(MongoAnnotations.UPDATE_OPTIONS_BEAN)) {
return new MongoQueryExecutorMatch(DataMethod.OperationType.UPDATE) {
@Override
protected void apply(MethodMatchInfo matchInfo) {
matchInfo.addParameterRole(MongoAnnotations.FILTER_ROLE, filter.getName());
matchInfo.addParameterRole(MongoAnnotations.UPDATE_ROLE, update.getName());
matchInfo.addParameterRole(MongoAnnotations.UPDATE_OPTIONS_ROLE, options.getName());
}
};
}
break;
default:
return null;
}
}
return null;
}
use of io.micronaut.data.annotation.Query in project micronaut-data by micronaut-projects.
the class RepositoryTypeElementVisitor method processMethodInfo.
private void processMethodInfo(MethodMatchContext methodMatchContext, MethodMatchInfo methodInfo) {
QueryBuilder queryEncoder = methodMatchContext.getQueryBuilder();
MethodElement element = methodMatchContext.getMethodElement();
SourcePersistentEntity entity = methodMatchContext.getRootEntity();
ParameterElement[] parameters = methodMatchContext.getParameters();
// populate parameter roles
for (Map.Entry<String, Element> entry : methodMatchContext.getParametersInRole().entrySet()) {
methodInfo.addParameterRole(entry.getKey(), entry.getValue().getName());
}
List<QueryParameterBinding> parameterBinding = null;
boolean encodeEntityParameters = false;
boolean supportsImplicitQueries = methodMatchContext.supportsImplicitQueries();
QueryResult queryResult = methodInfo.getQueryResult();
if (queryResult != null) {
if (methodInfo.isRawQuery()) {
// no need to annotation since already annotated, just replace the
// the computed parameter names
parameterBinding = queryResult.getParameterBindings();
element.annotate(Query.class, (builder) -> builder.member(DataMethod.META_MEMBER_RAW_QUERY, element.stringValue(Query.class).map(q -> addRawQueryParameterPlaceholders(queryEncoder, queryResult.getQuery(), queryResult.getQueryParts())).orElse(null)));
ClassElement genericReturnType = methodMatchContext.getReturnType();
if (methodMatchContext.isTypeInRole(genericReturnType, TypeRole.PAGE) || element.isPresent(Query.class, "countQuery")) {
QueryResult countQueryResult = methodInfo.getCountQueryResult();
if (countQueryResult == null) {
throw new MatchFailedException("Query returns a Page and does not specify a 'countQuery' member.", element);
} else {
element.annotate(Query.class, (builder) -> builder.member(DataMethod.META_MEMBER_RAW_COUNT_QUERY, addRawQueryParameterPlaceholders(queryEncoder, countQueryResult.getQuery(), countQueryResult.getQueryParts())));
}
}
encodeEntityParameters = methodInfo.isEncodeEntityParameters();
} else {
encodeEntityParameters = methodInfo.isEncodeEntityParameters();
parameterBinding = queryResult.getParameterBindings();
bindAdditionalParameters(methodMatchContext, entity, parameterBinding, parameters, queryResult.getAdditionalRequiredParameters());
QueryResult preparedCount = methodInfo.getCountQueryResult();
if (preparedCount != null) {
element.annotate(Query.class, annotationBuilder -> {
annotationBuilder.value(queryResult.getQuery());
annotationBuilder.member(DataMethod.META_MEMBER_COUNT_QUERY, preparedCount.getQuery());
});
} else {
element.annotate(Query.class, annotationBuilder -> {
annotationBuilder.value(queryResult.getQuery());
String update = queryResult.getUpdate();
if (StringUtils.isNotEmpty(update)) {
annotationBuilder.member("update", update);
}
});
}
}
}
ClassElement runtimeInterceptor = methodInfo.getRuntimeInterceptor();
if (runtimeInterceptor == null) {
throw new MatchFailedException("Unable to implement Repository method: " + currentRepository.getSimpleName() + "." + element.getName() + "(..). No possible runtime implementations found.", element);
}
boolean finalEncodeEntityParameters = encodeEntityParameters;
List<QueryParameterBinding> finalParameterBinding = parameterBinding;
element.annotate(DataMethod.class, annotationBuilder -> {
annotationBuilder.member(DataMethod.META_MEMBER_OPERATION_TYPE, methodInfo.getOperationType());
annotationBuilder.member(DataMethod.META_MEMBER_ROOT_ENTITY, new AnnotationClassValue<>(entity.getName()));
// include the roles
methodInfo.getParameterRoles().forEach(annotationBuilder::member);
if (methodInfo.isDto()) {
annotationBuilder.member(DataMethod.META_MEMBER_DTO, true);
}
if (methodInfo.isOptimisticLock()) {
annotationBuilder.member(DataMethod.META_MEMBER_OPTIMISTIC_LOCK, true);
}
TypedElement resultType = methodInfo.getResultType();
if (resultType != null) {
annotationBuilder.member(DataMethod.META_MEMBER_RESULT_TYPE, new AnnotationClassValue<>(resultType.getName()));
ClassElement type = resultType.getType();
if (!TypeUtils.isVoid(type)) {
annotationBuilder.member(DataMethod.META_MEMBER_RESULT_DATA_TYPE, TypeUtils.resolveDataType(type, dataTypes));
}
}
String idType = resolveIdType(entity);
if (idType != null) {
annotationBuilder.member(DataMethod.META_MEMBER_ID_TYPE, idType);
}
annotationBuilder.member(DataMethod.META_MEMBER_INTERCEPTOR, new AnnotationClassValue<>(runtimeInterceptor.getName()));
if (queryResult != null) {
if (finalParameterBinding.stream().anyMatch(QueryParameterBinding::isExpandable)) {
annotationBuilder.member(DataMethod.META_MEMBER_EXPANDABLE_QUERY, queryResult.getQueryParts().toArray(new String[0]));
QueryResult preparedCount = methodInfo.getCountQueryResult();
if (preparedCount != null) {
annotationBuilder.member(DataMethod.META_MEMBER_EXPANDABLE_COUNT_QUERY, preparedCount.getQueryParts().toArray(new String[0]));
}
}
int max = queryResult.getMax();
if (max > -1) {
annotationBuilder.member(DataMethod.META_MEMBER_PAGE_SIZE, max);
}
long offset = queryResult.getOffset();
if (offset > 0) {
annotationBuilder.member(DataMethod.META_MEMBER_PAGE_INDEX, offset);
}
}
Arrays.stream(parameters).filter(p -> p.getGenericType().isAssignable(entity.getName())).findFirst().ifPresent(parameterElement -> annotationBuilder.member(DataMethod.META_MEMBER_ENTITY, parameterElement.getName()));
if (CollectionUtils.isNotEmpty(finalParameterBinding)) {
bindParameters(supportsImplicitQueries, finalParameterBinding, finalEncodeEntityParameters, annotationBuilder);
}
});
}
use of io.micronaut.data.annotation.Query in project micronaut-data by micronaut-projects.
the class RawQueryMethodMatcher method match.
@Override
public MethodMatch match(MethodMatchContext matchContext) {
if (matchContext.getMethodElement().stringValue(Query.class).isPresent()) {
return new MethodMatch() {
@Override
public MethodMatchInfo buildMatchInfo(MethodMatchContext matchContext) {
MethodElement methodElement = matchContext.getMethodElement();
ParameterElement[] parameters = matchContext.getParameters();
ParameterElement entityParameter;
ParameterElement entitiesParameter;
if (parameters.length > 1) {
entityParameter = null;
entitiesParameter = null;
} else {
entityParameter = Arrays.stream(parameters).filter(p -> TypeUtils.isEntity(p.getGenericType())).findFirst().orElse(null);
entitiesParameter = Arrays.stream(parameters).filter(p -> TypeUtils.isIterableOfEntity(p.getGenericType())).findFirst().orElse(null);
}
boolean readOnly = matchContext.getAnnotationMetadata().booleanValue(Query.class, "readOnly").orElse(true);
String query = matchContext.getAnnotationMetadata().stringValue(Query.class).orElseThrow(IllegalStateException::new);
DataMethod.OperationType operationType = findOperationType(methodElement.getName(), query, readOnly);
Map.Entry<ClassElement, Class<? extends DataInterceptor>> entry = FindersUtils.resolveInterceptorTypeByOperationType(entityParameter != null, entitiesParameter != null, operationType, matchContext);
ClassElement resultType = entry.getKey();
Class<? extends DataInterceptor> interceptorType = entry.getValue();
if (interceptorType.getSimpleName().startsWith("SaveOne")) {
// Use `executeUpdate` operation for "insert(String a, String b)" style queries
// - custom query doesn't need to use root entity
// - we would like to know how many rows were updated
operationType = DataMethod.OperationType.UPDATE;
Map.Entry<ClassElement, Class<? extends DataInterceptor>> e = FindersUtils.pickUpdateInterceptor(matchContext, matchContext.getReturnType());
resultType = e.getKey();
interceptorType = e.getValue();
}
if (operationType == DataMethod.OperationType.QUERY) {
// Entity parameter/parameters only make sense if the operation is based on entity
entityParameter = null;
entitiesParameter = null;
}
boolean isDto = false;
if (resultType == null) {
resultType = matchContext.getRootEntity().getType();
} else {
if (operationType == DataMethod.OperationType.QUERY) {
if (resultType.hasAnnotation(Introspected.class)) {
if (!resultType.hasAnnotation(MappedEntity.class)) {
isDto = true;
}
}
} else if (!isValidReturnType(resultType, operationType)) {
throw new MatchFailedException("Invalid result type: " + resultType.getName() + " for '" + operationType + "' operation");
}
}
MethodMatchInfo methodMatchInfo = new MethodMatchInfo(operationType, resultType, FindersUtils.getInterceptorElement(matchContext, interceptorType));
methodMatchInfo.dto(isDto);
buildRawQuery(matchContext, methodMatchInfo, entityParameter, entitiesParameter, operationType);
if (entityParameter != null) {
methodMatchInfo.addParameterRole(TypeRole.ENTITY, entityParameter.getName());
} else if (entitiesParameter != null) {
methodMatchInfo.addParameterRole(TypeRole.ENTITIES, entitiesParameter.getName());
}
return methodMatchInfo;
}
};
}
return null;
}
use of io.micronaut.data.annotation.Query in project micronaut-data by micronaut-projects.
the class RawQueryMethodMatcher method getQueryResult.
private QueryResult getQueryResult(MethodMatchContext matchContext, String queryString, List<ParameterElement> parameters, boolean namedParameters, ParameterElement entityParam, SourcePersistentEntity persistentEntity) {
java.util.regex.Matcher matcher = VARIABLE_PATTERN.matcher(queryString.replace("\\:", ""));
List<QueryParameterBinding> parameterBindings = new ArrayList<>(parameters.size());
List<String> queryParts = new ArrayList<>();
int index = 1;
int lastOffset = 0;
while (matcher.find()) {
String prefix = queryString.substring(lastOffset, matcher.start(3) - 1);
if (!prefix.isEmpty()) {
queryParts.add(prefix);
}
lastOffset = matcher.end(3);
String name = matcher.group(3);
if (namedParameters) {
Optional<ParameterElement> element = parameters.stream().filter(p -> p.stringValue(Parameter.class).orElse(p.getName()).equals(name)).findFirst();
if (element.isPresent()) {
PersistentPropertyPath propertyPath = matchContext.getRootEntity().getPropertyPath(name);
BindingContext bindingContext = BindingContext.create().name(name).incomingMethodParameterProperty(propertyPath).outgoingQueryParameterProperty(propertyPath);
parameterBindings.add(bindingParameter(matchContext, element.get()).bind(bindingContext));
} else if (persistentEntity != null) {
PersistentPropertyPath propertyPath = persistentEntity.getPropertyPath(name);
if (propertyPath == null) {
throw new MatchFailedException("Cannot update non-existent property: " + name);
} else {
BindingContext bindingContext = BindingContext.create().name(name).incomingMethodParameterProperty(propertyPath).outgoingQueryParameterProperty(propertyPath);
parameterBindings.add(bindingParameter(matchContext, entityParam, true).bind(bindingContext));
}
} else {
throw new MatchFailedException("No method parameter found for named Query parameter: " + name);
}
} else {
Optional<ParameterElement> element = parameters.stream().filter(p -> p.stringValue(Parameter.class).orElse(p.getName()).equals(name)).findFirst();
if (element.isPresent()) {
PersistentPropertyPath propertyPath = matchContext.getRootEntity().getPropertyPath(name);
BindingContext bindingContext = BindingContext.create().index(index++).incomingMethodParameterProperty(propertyPath).outgoingQueryParameterProperty(propertyPath);
parameterBindings.add(bindingParameter(matchContext, element.get()).bind(bindingContext));
} else if (persistentEntity != null) {
PersistentPropertyPath propertyPath = persistentEntity.getPropertyPath(name);
if (propertyPath == null) {
matchContext.fail("Cannot update non-existent property: " + name);
} else {
BindingContext bindingContext = BindingContext.create().index(index++).incomingMethodParameterProperty(propertyPath).outgoingQueryParameterProperty(propertyPath);
parameterBindings.add(bindingParameter(matchContext, entityParam, true).bind(bindingContext));
}
} else {
throw new MatchFailedException("No method parameter found for named Query parameter: " + name);
}
}
}
queryString = queryString.replace("\\:", ":");
if (queryParts.isEmpty()) {
queryParts.add(queryString);
} else if (lastOffset > 0) {
queryParts.add(queryString.substring(lastOffset));
}
String finalQueryString = queryString;
return new QueryResult() {
@Override
public String getQuery() {
return finalQueryString;
}
@Override
public List<String> getQueryParts() {
return queryParts;
}
@Override
public List<QueryParameterBinding> getParameterBindings() {
return parameterBindings;
}
@Override
public Map<String, String> getAdditionalRequiredParameters() {
return Collections.emptyMap();
}
};
}
use of io.micronaut.data.annotation.Query in project micronaut-data by micronaut-projects.
the class MongoRawQueryMethodMatcher method processCustomQuery.
private String processCustomQuery(MethodMatchContext matchContext, String queryString, List<ParameterElement> parameters, ParameterElement entityParam, SourcePersistentEntity persistentEntity, List<QueryParameterBinding> parameterBindings) {
java.util.regex.Matcher matcher = VARIABLE_PATTERN.matcher(queryString);
List<String> queryParts = new ArrayList<>();
int lastOffset = 0;
while (matcher.find()) {
int matcherStart = matcher.start(3);
String start = queryString.substring(lastOffset, matcherStart - 1);
lastOffset = matcher.end(3);
if (!start.isEmpty()) {
queryParts.add(start);
}
String name = matcher.group(3);
Optional<ParameterElement> element = parameters.stream().filter(p -> p.stringValue(Parameter.class).orElse(p.getName()).equals(name)).findFirst();
if (element.isPresent()) {
PersistentPropertyPath propertyPath = matchContext.getRootEntity().getPropertyPath(name);
BindingContext bindingContext = BindingContext.create().name(name).incomingMethodParameterProperty(propertyPath).outgoingQueryParameterProperty(propertyPath);
parameterBindings.add(bindingParameter(matchContext, element.get()).bind(bindingContext));
} else if (persistentEntity != null) {
PersistentPropertyPath propertyPath = persistentEntity.getPropertyPath(name);
if (propertyPath == null) {
throw new MatchFailedException("Cannot update non-existent property: " + name);
} else {
BindingContext bindingContext = BindingContext.create().name(name).incomingMethodParameterProperty(propertyPath).outgoingQueryParameterProperty(propertyPath);
parameterBindings.add(bindingParameter(matchContext, entityParam, true).bind(bindingContext));
}
} else {
throw new MatchFailedException("No method parameter found for named Query parameter: " + name);
}
int ind = parameterBindings.size() - 1;
queryParts.add("{$mn_qp:" + ind + "}");
}
String end = queryString.substring(lastOffset);
if (!end.isEmpty()) {
queryParts.add(end);
}
return String.join("", queryParts);
}
Aggregations