Search in sources :

Example 1 with Query

use of com.datastax.oss.driver.api.mapper.annotations.Query in project java-driver by datastax.

the class DaoQueryMethodGenerator method generate.

@Override
public Optional<MethodSpec> generate() {
    // Validate the return type:
    DaoReturnType returnType = parseAndValidateReturnType(getSupportedReturnTypes(), Query.class.getSimpleName());
    if (returnType == null) {
        return Optional.empty();
    }
    // Generate the method:
    TypeElement entityElement = returnType.getEntityElement();
    String helperFieldName = (entityElement == null) ? null : enclosingClass.addEntityHelperField(ClassName.get(entityElement));
    String statementName = enclosingClass.addPreparedStatement(methodElement, (methodBuilder, requestName) -> generatePrepareRequest(methodBuilder, requestName, helperFieldName));
    CodeBlock.Builder createStatementBlock = CodeBlock.builder();
    List<? extends VariableElement> parameters = methodElement.getParameters();
    VariableElement boundStatementFunction = findBoundStatementFunction(methodElement);
    if (boundStatementFunction != null) {
        parameters = parameters.subList(0, methodElement.getParameters().size() - 1);
    }
    createStatementBlock.addStatement("$T boundStatementBuilder = $L.boundStatementBuilder()", BoundStatementBuilder.class, statementName);
    NullSavingStrategy nullSavingStrategy = nullSavingStrategyValidation.getNullSavingStrategy(Query.class, Query::nullSavingStrategy, methodElement, enclosingClass);
    createStatementBlock.addStatement("$1T nullSavingStrategy = $1T.$2L", NullSavingStrategy.class, nullSavingStrategy);
    populateBuilderWithStatementAttributes(createStatementBlock, methodElement);
    populateBuilderWithFunction(createStatementBlock, boundStatementFunction);
    if (validateCqlNamesPresent(parameters)) {
        GeneratedCodePatterns.bindParameters(parameters, createStatementBlock, enclosingClass, context, true);
        createStatementBlock.addStatement("$T boundStatement = boundStatementBuilder.build()", BoundStatement.class);
        return crudMethod(createStatementBlock, returnType, helperFieldName);
    } else {
        return Optional.empty();
    }
}
Also used : Query(com.datastax.oss.driver.api.mapper.annotations.Query) TypeElement(javax.lang.model.element.TypeElement) CodeBlock(com.squareup.javapoet.CodeBlock) NullSavingStrategy(com.datastax.oss.driver.api.mapper.entity.saving.NullSavingStrategy) VariableElement(javax.lang.model.element.VariableElement)

Aggregations

Query (com.datastax.oss.driver.api.mapper.annotations.Query)1 NullSavingStrategy (com.datastax.oss.driver.api.mapper.entity.saving.NullSavingStrategy)1 CodeBlock (com.squareup.javapoet.CodeBlock)1 TypeElement (javax.lang.model.element.TypeElement)1 VariableElement (javax.lang.model.element.VariableElement)1