Search in sources :

Example 1 with Dao

use of com.datastax.oss.driver.api.mapper.annotations.Dao 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 2 with Dao

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

the class MapperDaoFactoryMethodGenerator method generate.

@Override
public Optional<MethodSpec> generate() {
    // Validate the return type, which tells us what DAO to build, and whether the method should be
    // async.
    ClassName daoImplementationName = null;
    boolean isAsync = false;
    TypeMirror returnTypeMirror = methodElement.getReturnType();
    if (returnTypeMirror.getKind() == TypeKind.DECLARED) {
        DeclaredType declaredReturnType = (DeclaredType) returnTypeMirror;
        if (declaredReturnType.getTypeArguments().isEmpty()) {
            Element returnTypeElement = declaredReturnType.asElement();
            if (returnTypeElement.getAnnotation(Dao.class) != null) {
                daoImplementationName = GeneratedNames.daoImplementation(((TypeElement) returnTypeElement));
            }
        } else if (context.getClassUtils().isFuture(declaredReturnType)) {
            TypeMirror typeArgument = declaredReturnType.getTypeArguments().get(0);
            if (typeArgument.getKind() == TypeKind.DECLARED) {
                Element typeArgumentElement = ((DeclaredType) typeArgument).asElement();
                if (typeArgumentElement.getAnnotation(Dao.class) != null) {
                    daoImplementationName = GeneratedNames.daoImplementation(((TypeElement) typeArgumentElement));
                    isAsync = true;
                }
            }
        }
    }
    if (daoImplementationName == null) {
        context.getMessager().error(methodElement, "Invalid return type: %s methods must return a %s-annotated interface, " + "or future thereof", DaoFactory.class.getSimpleName(), Dao.class.getSimpleName());
        return Optional.empty();
    }
    // Validate the arguments
    String keyspaceArgumentName = null;
    String tableArgumentName = null;
    String profileArgumentName = null;
    boolean profileIsClass = false;
    for (VariableElement parameterElement : methodElement.getParameters()) {
        if (parameterElement.getAnnotation(DaoKeyspace.class) != null) {
            keyspaceArgumentName = validateKeyspaceOrTableParameter(parameterElement, keyspaceArgumentName, DaoKeyspace.class, context);
            if (keyspaceArgumentName == null) {
                return Optional.empty();
            }
        } else if (parameterElement.getAnnotation(DaoTable.class) != null) {
            tableArgumentName = validateKeyspaceOrTableParameter(parameterElement, tableArgumentName, DaoTable.class, context);
            if (tableArgumentName == null) {
                return Optional.empty();
            }
        } else if (parameterElement.getAnnotation(DaoProfile.class) != null) {
            profileArgumentName = validateExecutionProfile(parameterElement, profileArgumentName, context);
            profileIsClass = context.getClassUtils().isSame(parameterElement.asType(), DriverExecutionProfile.class);
            if (profileArgumentName == null) {
                return Optional.empty();
            }
        } else {
            context.getMessager().error(methodElement, "Invalid parameter annotations: " + "%s method parameters must be annotated with @%s, @%s or @%s", DaoFactory.class.getSimpleName(), DaoKeyspace.class.getSimpleName(), DaoTable.class.getSimpleName(), DaoProfile.class.getSimpleName());
            return Optional.empty();
        }
    }
    boolean isCachedByMethodArguments = (keyspaceArgumentName != null || tableArgumentName != null || profileArgumentName != null);
    TypeName returnTypeName = ClassName.get(methodElement.getReturnType());
    String suggestedFieldName = methodElement.getSimpleName() + "Cache";
    String fieldName = isCachedByMethodArguments ? enclosingClass.addDaoMapField(suggestedFieldName, returnTypeName) : enclosingClass.addDaoSimpleField(suggestedFieldName, returnTypeName, daoImplementationName, isAsync);
    MethodSpec.Builder overridingMethodBuilder = GeneratedCodePatterns.override(methodElement);
    if (isCachedByMethodArguments) {
        // DaoCacheKey key = new DaoCacheKey(<ks>, <table>, <profileName>, <profile>)
        // where <ks>, <table> is either the name of the parameter or "(CqlIdentifier)null"
        overridingMethodBuilder.addCode("$1T key = new $1T(", DaoCacheKey.class);
        if (keyspaceArgumentName == null) {
            overridingMethodBuilder.addCode("($T)null", CqlIdentifier.class);
        } else {
            overridingMethodBuilder.addCode("$L", keyspaceArgumentName);
        }
        overridingMethodBuilder.addCode(", ");
        if (tableArgumentName == null) {
            overridingMethodBuilder.addCode("($T)null", CqlIdentifier.class);
        } else {
            overridingMethodBuilder.addCode("$L", tableArgumentName);
        }
        overridingMethodBuilder.addCode(", ");
        if (profileArgumentName == null) {
            overridingMethodBuilder.addCode("null, null);\n");
        } else {
            if (profileIsClass) {
                overridingMethodBuilder.addCode("null, $L);\n", profileArgumentName);
            } else {
                overridingMethodBuilder.addCode("$L, null);\n", profileArgumentName);
            }
        }
        overridingMethodBuilder.addStatement("return $L.computeIfAbsent(key, " + "k -> $T.$L(context.withDaoParameters(k.getKeyspaceId(), k.getTableId(), " + "k.getExecutionProfileName(), k.getExecutionProfile())))", fieldName, daoImplementationName, isAsync ? "initAsync" : "init");
    } else {
        overridingMethodBuilder.addStatement("return $L.get()", fieldName);
    }
    return Optional.of(overridingMethodBuilder.build());
}
Also used : TypeName(com.squareup.javapoet.TypeName) DriverExecutionProfile(com.datastax.oss.driver.api.core.config.DriverExecutionProfile) MethodSpec(com.squareup.javapoet.MethodSpec) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) VariableElement(javax.lang.model.element.VariableElement) Element(javax.lang.model.element.Element) TypeElement(javax.lang.model.element.TypeElement) DaoKeyspace(com.datastax.oss.driver.api.mapper.annotations.DaoKeyspace) VariableElement(javax.lang.model.element.VariableElement) DaoFactory(com.datastax.oss.driver.api.mapper.annotations.DaoFactory) DaoProfile(com.datastax.oss.driver.api.mapper.annotations.DaoProfile) Dao(com.datastax.oss.driver.api.mapper.annotations.Dao) TypeMirror(javax.lang.model.type.TypeMirror) ClassName(com.squareup.javapoet.ClassName) DeclaredType(javax.lang.model.type.DeclaredType) DaoTable(com.datastax.oss.driver.api.mapper.annotations.DaoTable)

Example 3 with Dao

use of com.datastax.oss.driver.api.mapper.annotations.Dao 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)

Example 4 with Dao

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

the class SetEntityIT 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());
    }
    InventoryMapper inventoryMapper = new SetEntityIT_InventoryMapperBuilder(session).build();
    dao = inventoryMapper.productDao(SESSION_RULE.keyspace());
    dimensions2d = session.getKeyspace().flatMap(ks -> session.getMetadata().getKeyspace(ks)).flatMap(ks -> ks.getUserDefinedType("dimensions2d")).orElseThrow(AssertionError::new);
}
Also used : CqlIdentifier(com.datastax.oss.driver.api.core.CqlIdentifier) GettableByName(com.datastax.oss.driver.api.core.data.GettableByName) 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) SetEntity(com.datastax.oss.driver.api.mapper.annotations.SetEntity) ParallelizableTests(com.datastax.oss.driver.categories.ParallelizableTests) CqlSession(com.datastax.oss.driver.api.core.CqlSession) Assertions.catchThrowable(org.assertj.core.api.Assertions.catchThrowable) ClassRule(org.junit.ClassRule) BoundStatementBuilder(com.datastax.oss.driver.api.core.cql.BoundStatementBuilder) BoundStatement(com.datastax.oss.driver.api.core.cql.BoundStatement) Test(org.junit.Test) PreparedStatement(com.datastax.oss.driver.api.core.cql.PreparedStatement) 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) NullSavingStrategy(com.datastax.oss.driver.api.mapper.entity.saving.NullSavingStrategy) Dao(com.datastax.oss.driver.api.mapper.annotations.Dao) CqlSession(com.datastax.oss.driver.api.core.CqlSession) BeforeClass(org.junit.BeforeClass)

Aggregations

Dao (com.datastax.oss.driver.api.mapper.annotations.Dao)4 DaoFactory (com.datastax.oss.driver.api.mapper.annotations.DaoFactory)4 DaoKeyspace (com.datastax.oss.driver.api.mapper.annotations.DaoKeyspace)4 CqlIdentifier (com.datastax.oss.driver.api.core.CqlIdentifier)3 CqlSession (com.datastax.oss.driver.api.core.CqlSession)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 Mapper (com.datastax.oss.driver.api.mapper.annotations.Mapper)3 NullSavingStrategy (com.datastax.oss.driver.api.mapper.entity.saving.NullSavingStrategy)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 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)3 BeforeClass (org.junit.BeforeClass)3 ClassRule (org.junit.ClassRule)3 Test (org.junit.Test)3 Category (org.junit.experimental.categories.Category)3 RuleChain (org.junit.rules.RuleChain)3 TestRule (org.junit.rules.TestRule)3