Search in sources :

Example 1 with Update

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

the class DaoUpdateMethodGenerator method generatePrepareRequest.

private void generatePrepareRequest(MethodSpec.Builder methodBuilder, String requestName, String helperFieldName) {
    Update annotation = methodElement.getAnnotation(Update.class);
    maybeAddWhereClause(methodBuilder, requestName, helperFieldName, annotation.customWhereClause());
    maybeAddTtl(annotation.ttl(), methodBuilder);
    maybeAddTimestamp(annotation.timestamp(), methodBuilder);
    methodBuilder.addCode(")");
    maybeAddIfClause(methodBuilder, annotation);
    methodBuilder.addCode(".asCql()");
    methodBuilder.addCode(")$];\n");
}
Also used : DefaultUpdate(com.datastax.oss.driver.internal.querybuilder.update.DefaultUpdate) Update(com.datastax.oss.driver.api.mapper.annotations.Update)

Example 2 with Update

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

the class DaoUpdateMethodGenerator method generate.

@Override
public Optional<MethodSpec> generate() {
    // Validate the parameters:
    // - the first one must be the entity.
    // - the others are completely free-form (they'll be used as additional bind variables)
    // 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 = parameters.isEmpty() ? null : EntityUtils.asEntityElement(parameters.get(0), typeParameters);
    if (entityElement == null) {
        context.getMessager().error(methodElement, "%s methods must take the entity to update as the first parameter", Update.class.getSimpleName());
        return Optional.empty();
    }
    warnIfCqlNamePresent(parameters.subList(0, 1));
    EntityDefinition entityDefinition = context.getEntityFactory().getDefinition(entityElement);
    // Validate the return type:
    DaoReturnType returnType = parseAndValidateReturnType(getSupportedReturnTypes(), Update.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));
    CodeBlock.Builder createStatementBlock = CodeBlock.builder();
    createStatementBlock.addStatement("$T boundStatementBuilder = $L.boundStatementBuilder()", BoundStatementBuilder.class, statementName);
    populateBuilderWithStatementAttributes(createStatementBlock, methodElement);
    populateBuilderWithFunction(createStatementBlock, boundStatementFunction);
    String entityParameterName = parameters.get(0).getSimpleName().toString();
    Update annotation = methodElement.getAnnotation(Update.class);
    String customWhereClause = annotation.customWhereClause();
    NullSavingStrategy nullSavingStrategy = nullSavingStrategyValidation.getNullSavingStrategy(Update.class, Update::nullSavingStrategy, methodElement, enclosingClass);
    if (customWhereClause.isEmpty()) {
        // We generated an update by primary key (see maybeAddWhereClause), all entity properties are
        // present as placeholders.
        createStatementBlock.addStatement("$1L.set($2L, boundStatementBuilder, $3T.$4L, false)", helperFieldName, entityParameterName, NullSavingStrategy.class, nullSavingStrategy);
    } else {
        createStatementBlock.addStatement("$1T nullSavingStrategy = $1T.$2L", NullSavingStrategy.class, nullSavingStrategy);
        // (if the custom clause has custom placeholders, this will be addressed below)
        for (PropertyDefinition property : entityDefinition.getRegularColumns()) {
            GeneratedCodePatterns.setValue(property.getCqlName(), property.getType(), CodeBlock.of("$L.$L()", entityParameterName, property.getGetterName()), "boundStatementBuilder", createStatementBlock, enclosingClass, true, false);
        }
    }
    // customIfClause
    if (parameters.size() > 1) {
        List<? extends VariableElement> bindMarkers = parameters.subList(1, 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 : EntityDefinition(com.datastax.oss.driver.internal.mapper.processor.entity.EntityDefinition) 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) DefaultUpdate(com.datastax.oss.driver.internal.querybuilder.update.DefaultUpdate) Update(com.datastax.oss.driver.api.mapper.annotations.Update) PropertyDefinition(com.datastax.oss.driver.internal.mapper.processor.entity.PropertyDefinition)

Aggregations

Update (com.datastax.oss.driver.api.mapper.annotations.Update)2 DefaultUpdate (com.datastax.oss.driver.internal.querybuilder.update.DefaultUpdate)2 NullSavingStrategy (com.datastax.oss.driver.api.mapper.entity.saving.NullSavingStrategy)1 EntityDefinition (com.datastax.oss.driver.internal.mapper.processor.entity.EntityDefinition)1 PropertyDefinition (com.datastax.oss.driver.internal.mapper.processor.entity.PropertyDefinition)1 CodeBlock (com.squareup.javapoet.CodeBlock)1 TypeElement (javax.lang.model.element.TypeElement)1 VariableElement (javax.lang.model.element.VariableElement)1