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();
}
}
Aggregations