Search in sources :

Example 1 with BOOLEAN

use of com.datastax.oss.driver.internal.mapper.processor.dao.DefaultDaoReturnTypeKind.BOOLEAN in project java-driver by datastax.

the class DaoDeleteMethodGenerator method generate.

@Override
public Optional<MethodSpec> generate() {
    Delete annotation = methodElement.getAnnotation(Delete.class);
    assert annotation != null;
    if (annotation.ifExists() && !annotation.customIfClause().isEmpty()) {
        context.getMessager().error(methodElement, "Invalid annotation parameters: %s cannot have both ifExists and customIfClause", Delete.class.getSimpleName());
        return Optional.empty();
    }
    // Validate the arguments: either an entity instance, or the PK components (in the latter case,
    // the entity class has to be provided via the annotation).
    // In either case, a Function<BoundStatementBuilder, BoundStatementBuilder> can be added in last
    // position.
    List<? extends VariableElement> parameters = methodElement.getParameters();
    VariableElement boundStatementFunction = findBoundStatementFunction(methodElement);
    if (boundStatementFunction != null) {
        parameters = parameters.subList(0, parameters.size() - 1);
    }
    TypeElement entityElement;
    EntityDefinition entityDefinition;
    boolean hasEntityParameter;
    if (parameters.isEmpty()) {
        context.getMessager().error(methodElement, "Wrong number of parameters: %s methods with no custom clause " + "must take either an entity instance, or the primary key components", Delete.class.getSimpleName());
        return Optional.empty();
    }
    String customWhereClause = annotation.customWhereClause();
    String customIfClause = annotation.customIfClause();
    VariableElement firstParameter = parameters.get(0);
    entityElement = EntityUtils.asEntityElement(firstParameter, typeParameters);
    hasEntityParameter = (entityElement != null);
    // the number of primary key parameters provided, if -1 this implies a custom
    // where clause where number of parameters that are primary key are irrelevant.
    final int primaryKeyParameterCount;
    if (hasEntityParameter) {
        if (!customWhereClause.isEmpty()) {
            context.getMessager().error(methodElement, "Invalid parameter list: %s methods that have a custom where clause " + "must not take an Entity (%s) as a parameter", Delete.class.getSimpleName(), entityElement.getSimpleName());
        }
        entityDefinition = context.getEntityFactory().getDefinition(entityElement);
        primaryKeyParameterCount = entityDefinition.getPrimaryKey().size();
    } else {
        entityElement = getEntityClassFromAnnotation(Delete.class);
        if (entityElement == null) {
            context.getMessager().error(methodElement, "Missing entity class: %s methods that do not operate on an entity " + "instance must have an 'entityClass' argument", Delete.class.getSimpleName());
            return Optional.empty();
        } else {
            entityDefinition = context.getEntityFactory().getDefinition(entityElement);
        }
        if (customWhereClause.isEmpty()) {
            /* if a custom if clause is provided, the whole primary key must also be provided.
         * we only do this check if there is no custom where clause as the order of
         * keys may differ in that case.*/
            List<? extends VariableElement> primaryKeyParameters = parameters;
            if (!customIfClause.isEmpty()) {
                if (primaryKeyParameters.size() < entityDefinition.getPrimaryKey().size()) {
                    List<TypeName> primaryKeyTypes = entityDefinition.getPrimaryKey().stream().map(d -> d.getType().asTypeName()).collect(Collectors.toList());
                    context.getMessager().error(methodElement, "Invalid parameter list: %s methods that have a custom if clause" + "must specify the entire primary key (expected primary keys of %s: %s)", Delete.class.getSimpleName(), entityElement.getSimpleName(), primaryKeyTypes);
                    return Optional.empty();
                } else {
                    // restrict parameters to primary key length.
                    primaryKeyParameters = primaryKeyParameters.subList(0, entityDefinition.getPrimaryKey().size());
                }
            }
            primaryKeyParameterCount = primaryKeyParameters.size();
            if (!EntityUtils.areParametersValid(entityElement, entityDefinition, primaryKeyParameters, Delete.class, context, methodElement, processedType, "do not operate on an entity instance and lack a custom where clause")) {
                return Optional.empty();
            }
        } else {
            primaryKeyParameterCount = -1;
        }
    }
    // Validate the return type:
    DaoReturnType returnType = parseAndValidateReturnType(getSupportedReturnTypes(), Delete.class.getSimpleName());
    if (returnType == null) {
        return Optional.empty();
    }
    // Generate the method
    String helperFieldName = enclosingClass.addEntityHelperField(ClassName.get(entityElement));
    String statementName = enclosingClass.addPreparedStatement(methodElement, (methodBuilder, requestName) -> generatePrepareRequest(methodBuilder, requestName, helperFieldName, primaryKeyParameterCount));
    CodeBlock.Builder createStatementBlock = CodeBlock.builder();
    createStatementBlock.addStatement("$T boundStatementBuilder = $L.boundStatementBuilder()", BoundStatementBuilder.class, statementName);
    populateBuilderWithStatementAttributes(createStatementBlock, methodElement);
    populateBuilderWithFunction(createStatementBlock, boundStatementFunction);
    int nextParameterIndex = 0;
    if (hasEntityParameter) {
        warnIfCqlNamePresent(Collections.singletonList(firstParameter));
        // Bind entity's PK properties
        for (PropertyDefinition property : entityDefinition.getPrimaryKey()) {
            GeneratedCodePatterns.setValue(property.getCqlName(), property.getType(), CodeBlock.of("$L.$L()", firstParameter.getSimpleName(), property.getGetterName()), "boundStatementBuilder", createStatementBlock, enclosingClass);
        }
        nextParameterIndex = 1;
    } else if (customWhereClause.isEmpty()) {
        // The PK components are passed as arguments to the method (we've already checked that the
        // types match).
        List<CodeBlock> primaryKeyNames = entityDefinition.getPrimaryKey().stream().map(PropertyDefinition::getCqlName).collect(Collectors.toList()).subList(0, primaryKeyParameterCount);
        List<? extends VariableElement> bindMarkers = parameters.subList(0, primaryKeyParameterCount);
        warnIfCqlNamePresent(bindMarkers);
        GeneratedCodePatterns.bindParameters(bindMarkers, primaryKeyNames, createStatementBlock, enclosingClass, context, false);
        nextParameterIndex = primaryKeyNames.size();
    }
    // Bind any remaining parameters, assuming they are values for a custom WHERE or IF clause
    if (nextParameterIndex < parameters.size()) {
        if (customIfClause.isEmpty() && customWhereClause.isEmpty()) {
            context.getMessager().error(methodElement, "Wrong number of parameters: %s methods can only have additional " + "parameters if they specify a custom WHERE or IF clause", Delete.class.getSimpleName());
        }
        List<? extends VariableElement> bindMarkers = parameters.subList(nextParameterIndex, parameters.size());
        if (validateCqlNamesPresent(bindMarkers)) {
            GeneratedCodePatterns.bindParameters(bindMarkers, createStatementBlock, enclosingClass, context, false);
        } else {
            return Optional.empty();
        }
    }
    createStatementBlock.addStatement("$T boundStatement = boundStatementBuilder.build()", BoundStatement.class);
    return crudMethod(createStatementBlock, returnType, helperFieldName);
}
Also used : Delete(com.datastax.oss.driver.api.mapper.annotations.Delete) ProcessorContext(com.datastax.oss.driver.internal.mapper.processor.ProcessorContext) GeneratedCodePatterns(com.datastax.oss.driver.internal.mapper.processor.util.generation.GeneratedCodePatterns) ImmutableSet(com.datastax.oss.driver.shaded.guava.common.collect.ImmutableSet) VOID(com.datastax.oss.driver.internal.mapper.processor.dao.DefaultDaoReturnTypeKind.VOID) VariableElement(javax.lang.model.element.VariableElement) ClassName(com.squareup.javapoet.ClassName) PropertyDefinition(com.datastax.oss.driver.internal.mapper.processor.entity.PropertyDefinition) TypeElement(javax.lang.model.element.TypeElement) SimpleStatement(com.datastax.oss.driver.api.core.cql.SimpleStatement) FUTURE_OF_ASYNC_RESULT_SET(com.datastax.oss.driver.internal.mapper.processor.dao.DefaultDaoReturnTypeKind.FUTURE_OF_ASYNC_RESULT_SET) FUTURE_OF_BOOLEAN(com.datastax.oss.driver.internal.mapper.processor.dao.DefaultDaoReturnTypeKind.FUTURE_OF_BOOLEAN) Map(java.util.Map) BOOLEAN(com.datastax.oss.driver.internal.mapper.processor.dao.DefaultDaoReturnTypeKind.BOOLEAN) REACTIVE_RESULT_SET(com.datastax.oss.driver.internal.mapper.processor.dao.DefaultDaoReturnTypeKind.REACTIVE_RESULT_SET) EntityDefinition(com.datastax.oss.driver.internal.mapper.processor.entity.EntityDefinition) RESULT_SET(com.datastax.oss.driver.internal.mapper.processor.dao.DefaultDaoReturnTypeKind.RESULT_SET) CodeBlock(com.squareup.javapoet.CodeBlock) Name(javax.lang.model.element.Name) BoundStatementBuilder(com.datastax.oss.driver.api.core.cql.BoundStatementBuilder) MethodSpec(com.squareup.javapoet.MethodSpec) ExecutableElement(javax.lang.model.element.ExecutableElement) Set(java.util.Set) BoundStatement(com.datastax.oss.driver.api.core.cql.BoundStatement) Collectors(java.util.stream.Collectors) CUSTOM(com.datastax.oss.driver.internal.mapper.processor.dao.DefaultDaoReturnTypeKind.CUSTOM) List(java.util.List) Delete(com.datastax.oss.driver.api.mapper.annotations.Delete) FUTURE_OF_VOID(com.datastax.oss.driver.internal.mapper.processor.dao.DefaultDaoReturnTypeKind.FUTURE_OF_VOID) BOUND_STATEMENT(com.datastax.oss.driver.internal.mapper.processor.dao.DefaultDaoReturnTypeKind.BOUND_STATEMENT) TypeName(com.squareup.javapoet.TypeName) Optional(java.util.Optional) Collections(java.util.Collections) TypeName(com.squareup.javapoet.TypeName) TypeElement(javax.lang.model.element.TypeElement) CodeBlock(com.squareup.javapoet.CodeBlock) VariableElement(javax.lang.model.element.VariableElement) PropertyDefinition(com.datastax.oss.driver.internal.mapper.processor.entity.PropertyDefinition) EntityDefinition(com.datastax.oss.driver.internal.mapper.processor.entity.EntityDefinition) List(java.util.List)

Aggregations

BoundStatement (com.datastax.oss.driver.api.core.cql.BoundStatement)1 BoundStatementBuilder (com.datastax.oss.driver.api.core.cql.BoundStatementBuilder)1 SimpleStatement (com.datastax.oss.driver.api.core.cql.SimpleStatement)1 Delete (com.datastax.oss.driver.api.mapper.annotations.Delete)1 ProcessorContext (com.datastax.oss.driver.internal.mapper.processor.ProcessorContext)1 BOOLEAN (com.datastax.oss.driver.internal.mapper.processor.dao.DefaultDaoReturnTypeKind.BOOLEAN)1 BOUND_STATEMENT (com.datastax.oss.driver.internal.mapper.processor.dao.DefaultDaoReturnTypeKind.BOUND_STATEMENT)1 CUSTOM (com.datastax.oss.driver.internal.mapper.processor.dao.DefaultDaoReturnTypeKind.CUSTOM)1 FUTURE_OF_ASYNC_RESULT_SET (com.datastax.oss.driver.internal.mapper.processor.dao.DefaultDaoReturnTypeKind.FUTURE_OF_ASYNC_RESULT_SET)1 FUTURE_OF_BOOLEAN (com.datastax.oss.driver.internal.mapper.processor.dao.DefaultDaoReturnTypeKind.FUTURE_OF_BOOLEAN)1 FUTURE_OF_VOID (com.datastax.oss.driver.internal.mapper.processor.dao.DefaultDaoReturnTypeKind.FUTURE_OF_VOID)1 REACTIVE_RESULT_SET (com.datastax.oss.driver.internal.mapper.processor.dao.DefaultDaoReturnTypeKind.REACTIVE_RESULT_SET)1 RESULT_SET (com.datastax.oss.driver.internal.mapper.processor.dao.DefaultDaoReturnTypeKind.RESULT_SET)1 VOID (com.datastax.oss.driver.internal.mapper.processor.dao.DefaultDaoReturnTypeKind.VOID)1 EntityDefinition (com.datastax.oss.driver.internal.mapper.processor.entity.EntityDefinition)1 PropertyDefinition (com.datastax.oss.driver.internal.mapper.processor.entity.PropertyDefinition)1 GeneratedCodePatterns (com.datastax.oss.driver.internal.mapper.processor.util.generation.GeneratedCodePatterns)1 ImmutableSet (com.datastax.oss.driver.shaded.guava.common.collect.ImmutableSet)1 ClassName (com.squareup.javapoet.ClassName)1 CodeBlock (com.squareup.javapoet.CodeBlock)1