Search in sources :

Example 1 with Insert

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

the class NestedUdtIT method setup.

@BeforeClass
public static void setup() {
    CqlSession session = SESSION_RULE.session();
    for (String query : ImmutableList.of("CREATE TYPE type1(s1 text, s2 text)", "CREATE TYPE type2(i1 int, i2 int)", "CREATE TYPE type1_partial(s1 text)", "CREATE TYPE type2_partial(i1 int)", "CREATE TABLE container(id uuid PRIMARY KEY, " + "list frozen<list<type1>>, " + "map1 frozen<map<text, list<type1>>>, " + "map2 frozen<map<type1, set<list<type2>>>>," + "map3 frozen<map<type1, map<text, set<type2>>>>" + ")", "CREATE TABLE container_partial(id uuid PRIMARY KEY, " + "list frozen<list<type1_partial>>, " + "map1 frozen<map<text, list<type1_partial>>>, " + "map2 frozen<map<type1_partial, set<list<type2_partial>>>>," + "map3 frozen<map<type1_partial, map<text, set<type2_partial>>>>" + ")")) {
        session.execute(SimpleStatement.builder(query).setExecutionProfile(SESSION_RULE.slowProfile()).build());
    }
    UserDefinedType type1Partial = session.getKeyspace().flatMap(ks -> session.getMetadata().getKeyspace(ks)).flatMap(ks -> ks.getUserDefinedType("type1_partial")).orElseThrow(AssertionError::new);
    session.execute(SimpleStatement.newInstance("INSERT INTO container_partial (id, list) VALUES (?, ?)", SAMPLE_CONTAINER.getId(), Lists.newArrayList(type1Partial.newValue("a"), type1Partial.newValue("b"))));
    UdtsMapper udtsMapper = new NestedUdtIT_UdtsMapperBuilder(session).build();
    containerDao = udtsMapper.containerDao(SESSION_RULE.keyspace());
}
Also used : CqlIdentifier(com.datastax.oss.driver.api.core.CqlIdentifier) DaoFactory(com.datastax.oss.driver.api.mapper.annotations.DaoFactory) BeforeClass(org.junit.BeforeClass) DaoKeyspace(com.datastax.oss.driver.api.mapper.annotations.DaoKeyspace) ImmutableSet(com.datastax.oss.driver.shaded.guava.common.collect.ImmutableSet) TestRule(org.junit.rules.TestRule) Mapper(com.datastax.oss.driver.api.mapper.annotations.Mapper) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) CcmRule(com.datastax.oss.driver.api.testinfra.ccm.CcmRule) SimpleStatement(com.datastax.oss.driver.api.core.cql.SimpleStatement) SetEntity(com.datastax.oss.driver.api.mapper.annotations.SetEntity) ParallelizableTests(com.datastax.oss.driver.categories.ParallelizableTests) ImmutableMap(com.datastax.oss.driver.shaded.guava.common.collect.ImmutableMap) Entity(com.datastax.oss.driver.api.mapper.annotations.Entity) CqlSession(com.datastax.oss.driver.api.core.CqlSession) Map(java.util.Map) Select(com.datastax.oss.driver.api.mapper.annotations.Select) Assertions.catchThrowable(org.assertj.core.api.Assertions.catchThrowable) ClassRule(org.junit.ClassRule) Row(com.datastax.oss.driver.api.core.cql.Row) CassandraRequirement(com.datastax.oss.driver.api.testinfra.CassandraRequirement) Before(org.junit.Before) PartitionKey(com.datastax.oss.driver.api.mapper.annotations.PartitionKey) BoundStatementBuilder(com.datastax.oss.driver.api.core.cql.BoundStatementBuilder) ResultSet(com.datastax.oss.driver.api.core.cql.ResultSet) Set(java.util.Set) Test(org.junit.Test) Insert(com.datastax.oss.driver.api.mapper.annotations.Insert) UUID(java.util.UUID) PreparedStatement(com.datastax.oss.driver.api.core.cql.PreparedStatement) Category(org.junit.experimental.categories.Category) UdtValue(com.datastax.oss.driver.api.core.data.UdtValue) ImmutableList(com.datastax.oss.driver.shaded.guava.common.collect.ImmutableList) SessionRule(com.datastax.oss.driver.api.testinfra.session.SessionRule) Objects(java.util.Objects) RuleChain(org.junit.rules.RuleChain) UserDefinedType(com.datastax.oss.driver.api.core.type.UserDefinedType) GetEntity(com.datastax.oss.driver.api.mapper.annotations.GetEntity) List(java.util.List) Lists(org.assertj.core.util.Lists) NullSavingStrategy(com.datastax.oss.driver.api.mapper.entity.saving.NullSavingStrategy) Dao(com.datastax.oss.driver.api.mapper.annotations.Dao) UserDefinedType(com.datastax.oss.driver.api.core.type.UserDefinedType) CqlSession(com.datastax.oss.driver.api.core.CqlSession) BeforeClass(org.junit.BeforeClass)

Example 2 with Insert

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

the class ImmutableEntityIT method setup.

@BeforeClass
public static void setup() {
    CqlSession session = SESSION_RULE.session();
    for (String query : createStatements(CCM_RULE)) {
        session.execute(SimpleStatement.builder(query).setExecutionProfile(SESSION_RULE.slowProfile()).build());
    }
    UserDefinedType dimensions2d = session.getKeyspace().flatMap(ks -> session.getMetadata().getKeyspace(ks)).flatMap(ks -> ks.getUserDefinedType("dimensions2d")).orElseThrow(AssertionError::new);
    session.execute("INSERT INTO product2d (id, description, dimensions) VALUES (?, ?, ?)", PRODUCT_2D_ID, "2D product", dimensions2d.newValue(12, 34));
    InventoryMapper mapper = InventoryMapper.builder(session).build();
    dao = mapper.immutableProductDao(SESSION_RULE.keyspace());
}
Also used : CqlIdentifier(com.datastax.oss.driver.api.core.CqlIdentifier) DaoFactory(com.datastax.oss.driver.api.mapper.annotations.DaoFactory) BeforeClass(org.junit.BeforeClass) DaoKeyspace(com.datastax.oss.driver.api.mapper.annotations.DaoKeyspace) TestRule(org.junit.rules.TestRule) Mapper(com.datastax.oss.driver.api.mapper.annotations.Mapper) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) CcmRule(com.datastax.oss.driver.api.testinfra.ccm.CcmRule) SimpleStatement(com.datastax.oss.driver.api.core.cql.SimpleStatement) ParallelizableTests(com.datastax.oss.driver.categories.ParallelizableTests) FLUENT(com.datastax.oss.driver.api.mapper.entity.naming.GetterStyle.FLUENT) Computed(com.datastax.oss.driver.api.mapper.annotations.Computed) Entity(com.datastax.oss.driver.api.mapper.annotations.Entity) CqlSession(com.datastax.oss.driver.api.core.CqlSession) MapperBuilder(com.datastax.oss.driver.api.mapper.MapperBuilder) Select(com.datastax.oss.driver.api.mapper.annotations.Select) PropertyStrategy(com.datastax.oss.driver.api.mapper.annotations.PropertyStrategy) ClassRule(org.junit.ClassRule) Row(com.datastax.oss.driver.api.core.cql.Row) PartitionKey(com.datastax.oss.driver.api.mapper.annotations.PartitionKey) DefaultNullSavingStrategy(com.datastax.oss.driver.api.mapper.annotations.DefaultNullSavingStrategy) Test(org.junit.Test) CqlName(com.datastax.oss.driver.api.mapper.annotations.CqlName) Insert(com.datastax.oss.driver.api.mapper.annotations.Insert) UUID(java.util.UUID) Category(org.junit.experimental.categories.Category) UdtValue(com.datastax.oss.driver.api.core.data.UdtValue) SessionRule(com.datastax.oss.driver.api.testinfra.session.SessionRule) Objects(java.util.Objects) RuleChain(org.junit.rules.RuleChain) UserDefinedType(com.datastax.oss.driver.api.core.type.UserDefinedType) GetEntity(com.datastax.oss.driver.api.mapper.annotations.GetEntity) NullSavingStrategy(com.datastax.oss.driver.api.mapper.entity.saving.NullSavingStrategy) Dao(com.datastax.oss.driver.api.mapper.annotations.Dao) UserDefinedType(com.datastax.oss.driver.api.core.type.UserDefinedType) CqlSession(com.datastax.oss.driver.api.core.CqlSession) BeforeClass(org.junit.BeforeClass)

Example 3 with Insert

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

the class DaoInsertMethodGenerator 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 insert as the first parameter", Insert.class.getSimpleName());
        return Optional.empty();
    }
    // Validate the return type:
    DaoReturnType returnType = parseAndValidateReturnType(getSupportedReturnTypes(), Insert.class.getSimpleName());
    if (returnType == null) {
        return Optional.empty();
    }
    if (returnType.getEntityElement() != null && !returnType.getEntityElement().equals(entityElement)) {
        context.getMessager().error(methodElement, "Invalid return type: %s methods must return the same entity as their argument ", Insert.class.getSimpleName());
        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);
    warnIfCqlNamePresent(parameters.subList(0, 1));
    String entityParameterName = parameters.get(0).getSimpleName().toString();
    NullSavingStrategy nullSavingStrategy = nullSavingStrategyValidation.getNullSavingStrategy(Insert.class, Insert::nullSavingStrategy, methodElement, enclosingClass);
    createStatementBlock.addStatement("$1L.set($2L, boundStatementBuilder, $3T.$4L, false)", helperFieldName, entityParameterName, NullSavingStrategy.class, nullSavingStrategy);
    // Handle all remaining parameters as additional bound values
    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 : 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) Insert(com.datastax.oss.driver.api.mapper.annotations.Insert)

Example 4 with Insert

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

the class DaoInsertMethodGenerator method generatePrepareRequest.

private void generatePrepareRequest(MethodSpec.Builder methodBuilder, String requestName, String helperFieldName) {
    methodBuilder.addCode("$[$T $L = $L.insert()", SimpleStatement.class, requestName, helperFieldName);
    Insert annotation = methodElement.getAnnotation(Insert.class);
    if (annotation.ifNotExists()) {
        methodBuilder.addCode(".ifNotExists()");
    }
    maybeAddTtl(annotation.ttl(), methodBuilder);
    maybeAddTimestamp(annotation.timestamp(), methodBuilder);
    methodBuilder.addCode(".build()$];\n");
}
Also used : Insert(com.datastax.oss.driver.api.mapper.annotations.Insert)

Example 5 with Insert

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

the class GetEntityIT method setup.

@BeforeClass
public static void setup() {
    CqlSession session = SESSION_RULE.session();
    for (String query : createStatements(CCM_RULE)) {
        session.execute(SimpleStatement.builder(query).setExecutionProfile(SESSION_RULE.slowProfile()).build());
    }
    UserDefinedType dimensions2d = session.getKeyspace().flatMap(ks -> session.getMetadata().getKeyspace(ks)).flatMap(ks -> ks.getUserDefinedType("dimensions2d")).orElseThrow(AssertionError::new);
    session.execute("INSERT INTO product2d (id, description, dimensions) VALUES (?, ?, ?)", PRODUCT_2D_ID, "2D product", dimensions2d.newValue(12, 34));
    InventoryMapper inventoryMapper = new GetEntityIT_InventoryMapperBuilder(session).build();
    dao = inventoryMapper.productDao(SESSION_RULE.keyspace());
    dao.save(FLAMETHROWER);
    dao.save(MP3_DOWNLOAD);
}
Also used : CqlIdentifier(com.datastax.oss.driver.api.core.CqlIdentifier) DaoFactory(com.datastax.oss.driver.api.mapper.annotations.DaoFactory) BeforeClass(org.junit.BeforeClass) DaoKeyspace(com.datastax.oss.driver.api.mapper.annotations.DaoKeyspace) TestRule(org.junit.rules.TestRule) Mapper(com.datastax.oss.driver.api.mapper.annotations.Mapper) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) CcmRule(com.datastax.oss.driver.api.testinfra.ccm.CcmRule) SimpleStatement(com.datastax.oss.driver.api.core.cql.SimpleStatement) MappedAsyncPagingIterable(com.datastax.oss.driver.api.core.MappedAsyncPagingIterable) ParallelizableTests(com.datastax.oss.driver.categories.ParallelizableTests) PagingIterable(com.datastax.oss.driver.api.core.PagingIterable) AsyncResultSet(com.datastax.oss.driver.api.core.cql.AsyncResultSet) CqlSession(com.datastax.oss.driver.api.core.CqlSession) Assertions.catchThrowable(org.assertj.core.api.Assertions.catchThrowable) ClassRule(org.junit.ClassRule) Row(com.datastax.oss.driver.api.core.cql.Row) CompletableFutures(com.datastax.oss.driver.internal.core.util.concurrent.CompletableFutures) Sets(com.datastax.oss.driver.shaded.guava.common.collect.Sets) ResultSet(com.datastax.oss.driver.api.core.cql.ResultSet) DefaultNullSavingStrategy(com.datastax.oss.driver.api.mapper.annotations.DefaultNullSavingStrategy) Test(org.junit.Test) Insert(com.datastax.oss.driver.api.mapper.annotations.Insert) UUID(java.util.UUID) Category(org.junit.experimental.categories.Category) UdtValue(com.datastax.oss.driver.api.core.data.UdtValue) SessionRule(com.datastax.oss.driver.api.testinfra.session.SessionRule) RuleChain(org.junit.rules.RuleChain) UserDefinedType(com.datastax.oss.driver.api.core.type.UserDefinedType) GetEntity(com.datastax.oss.driver.api.mapper.annotations.GetEntity) Stream(java.util.stream.Stream) NullSavingStrategy(com.datastax.oss.driver.api.mapper.entity.saving.NullSavingStrategy) Dao(com.datastax.oss.driver.api.mapper.annotations.Dao) UserDefinedType(com.datastax.oss.driver.api.core.type.UserDefinedType) CqlSession(com.datastax.oss.driver.api.core.CqlSession) BeforeClass(org.junit.BeforeClass)

Aggregations

Insert (com.datastax.oss.driver.api.mapper.annotations.Insert)5 NullSavingStrategy (com.datastax.oss.driver.api.mapper.entity.saving.NullSavingStrategy)4 CqlIdentifier (com.datastax.oss.driver.api.core.CqlIdentifier)3 CqlSession (com.datastax.oss.driver.api.core.CqlSession)3 Row (com.datastax.oss.driver.api.core.cql.Row)3 SimpleStatement (com.datastax.oss.driver.api.core.cql.SimpleStatement)3 UdtValue (com.datastax.oss.driver.api.core.data.UdtValue)3 UserDefinedType (com.datastax.oss.driver.api.core.type.UserDefinedType)3 Dao (com.datastax.oss.driver.api.mapper.annotations.Dao)3 DaoFactory (com.datastax.oss.driver.api.mapper.annotations.DaoFactory)3 DaoKeyspace (com.datastax.oss.driver.api.mapper.annotations.DaoKeyspace)3 GetEntity (com.datastax.oss.driver.api.mapper.annotations.GetEntity)3 Mapper (com.datastax.oss.driver.api.mapper.annotations.Mapper)3 CcmRule (com.datastax.oss.driver.api.testinfra.ccm.CcmRule)3 SessionRule (com.datastax.oss.driver.api.testinfra.session.SessionRule)3 ParallelizableTests (com.datastax.oss.driver.categories.ParallelizableTests)3 UUID (java.util.UUID)3 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)3 BeforeClass (org.junit.BeforeClass)3 ClassRule (org.junit.ClassRule)3