Search in sources :

Example 6 with Query

use of io.micronaut.data.annotation.Query in project micronaut-data by micronaut-projects.

the class RawQueryMethodMatcher method buildRawQuery.

/**
 * Builds a raw query for the given match context. Should be called for methods annotated with {@link Query} explicitly.
 */
private void buildRawQuery(@NonNull MethodMatchContext matchContext, MethodMatchInfo methodMatchInfo, ParameterElement entityParameter, ParameterElement entitiesParameter, DataMethod.OperationType operationType) {
    MethodElement methodElement = matchContext.getMethodElement();
    String queryString = methodElement.stringValue(Query.class).orElseThrow(() -> new IllegalStateException("Should only be called if Query has value!"));
    List<ParameterElement> parameters = Arrays.asList(matchContext.getParameters());
    boolean namedParameters = matchContext.getRepositoryClass().booleanValue(RepositoryConfiguration.class, "namedParameters").orElse(true);
    ParameterElement entityParam = null;
    SourcePersistentEntity persistentEntity = null;
    if (entityParameter != null) {
        entityParam = entityParameter;
        persistentEntity = matchContext.getEntity(entityParameter.getGenericType());
    } else if (entitiesParameter != null) {
        entityParam = entitiesParameter;
        persistentEntity = matchContext.getEntity(entitiesParameter.getGenericType().getFirstTypeArgument().orElseThrow(IllegalStateException::new));
    }
    QueryResult queryResult = getQueryResult(matchContext, queryString, parameters, namedParameters, entityParam, persistentEntity);
    String cq = matchContext.getAnnotationMetadata().stringValue(Query.class, "countQuery").orElse(null);
    QueryResult countQueryResult = cq == null ? null : getQueryResult(matchContext, cq, parameters, namedParameters, entityParam, persistentEntity);
    boolean encodeEntityParameters = persistentEntity != null || operationType == DataMethod.OperationType.INSERT;
    methodMatchInfo.isRawQuery(true).encodeEntityParameters(encodeEntityParameters).queryResult(queryResult).countQueryResult(countQueryResult);
}
Also used : QueryResult(io.micronaut.data.model.query.builder.QueryResult) Query(io.micronaut.data.annotation.Query) SourcePersistentEntity(io.micronaut.data.processor.model.SourcePersistentEntity) MethodElement(io.micronaut.inject.ast.MethodElement) ParameterElement(io.micronaut.inject.ast.ParameterElement) RepositoryConfiguration(io.micronaut.data.annotation.RepositoryConfiguration)

Aggregations

Query (io.micronaut.data.annotation.Query)6 ParameterElement (io.micronaut.inject.ast.ParameterElement)6 TypeRole (io.micronaut.data.annotation.TypeRole)5 DataMethod (io.micronaut.data.intercept.annotation.DataMethod)5 QueryResult (io.micronaut.data.model.query.builder.QueryResult)5 SourcePersistentEntity (io.micronaut.data.processor.model.SourcePersistentEntity)5 ClassElement (io.micronaut.inject.ast.ClassElement)5 MethodElement (io.micronaut.inject.ast.MethodElement)5 Map (java.util.Map)5 Parameter (io.micronaut.context.annotation.Parameter)4 NonNull (io.micronaut.core.annotation.NonNull)4 RepositoryConfiguration (io.micronaut.data.annotation.RepositoryConfiguration)4 DataInterceptor (io.micronaut.data.intercept.DataInterceptor)4 PersistentPropertyPath (io.micronaut.data.model.PersistentPropertyPath)4 QueryParameterBinding (io.micronaut.data.model.query.builder.QueryParameterBinding)4 SourceParameterExpressionImpl (io.micronaut.data.processor.model.criteria.impl.SourceParameterExpressionImpl)4 MethodMatchContext (io.micronaut.data.processor.visitors.MethodMatchContext)4 ArrayList (java.util.ArrayList)4 Arrays (java.util.Arrays)4 Collections (java.util.Collections)4