Search in sources :

Example 1 with UserDefinedType

use of com.datastax.oss.driver.api.core.type.UserDefinedType in project java-driver by datastax.

the class CachingCodecRegistry method createCodec.

// Try to create a codec when we haven't found it in the cache.
// Variant where the Java type is unknown.
@NonNull
protected TypeCodec<?> createCodec(@NonNull DataType cqlType) {
    if (cqlType instanceof ListType) {
        DataType elementType = ((ListType) cqlType).getElementType();
        TypeCodec<Object> elementCodec = codecFor(elementType);
        return TypeCodecs.listOf(elementCodec);
    } else if (cqlType instanceof SetType) {
        DataType elementType = ((SetType) cqlType).getElementType();
        TypeCodec<Object> elementCodec = codecFor(elementType);
        return TypeCodecs.setOf(elementCodec);
    } else if (cqlType instanceof MapType) {
        DataType keyType = ((MapType) cqlType).getKeyType();
        DataType valueType = ((MapType) cqlType).getValueType();
        TypeCodec<Object> keyCodec = codecFor(keyType);
        TypeCodec<Object> valueCodec = codecFor(valueType);
        return TypeCodecs.mapOf(keyCodec, valueCodec);
    } else if (cqlType instanceof TupleType) {
        return TypeCodecs.tupleOf((TupleType) cqlType);
    } else if (cqlType instanceof UserDefinedType) {
        return TypeCodecs.udtOf((UserDefinedType) cqlType);
    } else if (cqlType instanceof CustomType) {
        return TypeCodecs.custom(cqlType);
    }
    throw new CodecNotFoundException(cqlType, null);
}
Also used : CustomType(com.datastax.oss.driver.api.core.type.CustomType) SetType(com.datastax.oss.driver.api.core.type.SetType) TypeCodec(com.datastax.oss.driver.api.core.type.codec.TypeCodec) ListType(com.datastax.oss.driver.api.core.type.ListType) TupleType(com.datastax.oss.driver.api.core.type.TupleType) DataType(com.datastax.oss.driver.api.core.type.DataType) UserDefinedType(com.datastax.oss.driver.api.core.type.UserDefinedType) CodecNotFoundException(com.datastax.oss.driver.api.core.type.codec.CodecNotFoundException) MapType(com.datastax.oss.driver.api.core.type.MapType) NonNull(edu.umd.cs.findbugs.annotations.NonNull)

Example 2 with UserDefinedType

use of com.datastax.oss.driver.api.core.type.UserDefinedType in project java-driver by datastax.

the class CachingCodecRegistryTestDataProviders method emptyCollectionsWithCqlAndJavaTypes.

@DataProvider
public static Object[][] emptyCollectionsWithCqlAndJavaTypes() {
    TupleType tupleType = DataTypes.tupleOf(DataTypes.INT, DataTypes.listOf(DataTypes.TEXT));
    UserDefinedType userType = new UserDefinedTypeBuilder(CqlIdentifier.fromInternal("ks"), CqlIdentifier.fromInternal("type")).withField(CqlIdentifier.fromInternal("field1"), DataTypes.INT).withField(CqlIdentifier.fromInternal("field2"), DataTypes.listOf(DataTypes.TEXT)).build();
    return new Object[][] { // lists
    { DataTypes.listOf(DataTypes.INT), GenericType.listOf(Integer.class), DataTypes.listOf(DataTypes.BOOLEAN), GenericType.listOf(Boolean.class), Collections.emptyList() }, { DataTypes.listOf(DataTypes.TEXT), GenericType.listOf(String.class), DataTypes.listOf(DataTypes.BOOLEAN), GenericType.listOf(Boolean.class), Collections.emptyList() }, { DataTypes.listOf(DataTypes.BLOB), GenericType.listOf(ByteBuffer.class), DataTypes.listOf(DataTypes.BOOLEAN), GenericType.listOf(Boolean.class), Collections.emptyList() }, { DataTypes.listOf(DataTypes.INET), GenericType.listOf(InetAddress.class), DataTypes.listOf(DataTypes.BOOLEAN), GenericType.listOf(Boolean.class), Collections.emptyList() }, { DataTypes.listOf(tupleType), GenericType.listOf(TupleValue.class), DataTypes.listOf(DataTypes.BOOLEAN), GenericType.listOf(Boolean.class), Collections.emptyList() }, { DataTypes.listOf(userType), GenericType.listOf(UdtValue.class), DataTypes.listOf(DataTypes.BOOLEAN), GenericType.listOf(Boolean.class), Collections.emptyList() }, { DataTypes.listOf(DataTypes.listOf(DataTypes.INT)), GenericType.listOf(GenericType.listOf(Integer.class)), DataTypes.listOf(DataTypes.listOf(DataTypes.BOOLEAN)), GenericType.listOf(GenericType.listOf(Boolean.class)), ImmutableList.of(Collections.emptyList()) }, { DataTypes.listOf(DataTypes.listOf(tupleType)), GenericType.listOf(GenericType.listOf(TupleValue.class)), DataTypes.listOf(DataTypes.listOf(DataTypes.BOOLEAN)), GenericType.listOf(GenericType.listOf(Boolean.class)), ImmutableList.of(Collections.emptyList()) }, { DataTypes.listOf(DataTypes.listOf(userType)), GenericType.listOf(GenericType.listOf(UdtValue.class)), DataTypes.listOf(DataTypes.listOf(DataTypes.BOOLEAN)), GenericType.listOf(GenericType.listOf(Boolean.class)), ImmutableList.of(Collections.emptyList()) }, // sets
    { DataTypes.setOf(DataTypes.INT), GenericType.setOf(Integer.class), DataTypes.setOf(DataTypes.BOOLEAN), GenericType.setOf(Boolean.class), Collections.emptySet() }, { DataTypes.setOf(DataTypes.TEXT), GenericType.setOf(String.class), DataTypes.setOf(DataTypes.BOOLEAN), GenericType.setOf(Boolean.class), Collections.emptySet() }, { DataTypes.setOf(DataTypes.BLOB), GenericType.setOf(ByteBuffer.class), DataTypes.setOf(DataTypes.BOOLEAN), GenericType.setOf(Boolean.class), Collections.emptySet() }, { DataTypes.setOf(DataTypes.INET), GenericType.setOf(InetAddress.class), DataTypes.setOf(DataTypes.BOOLEAN), GenericType.setOf(Boolean.class), Collections.emptySet() }, { DataTypes.setOf(tupleType), GenericType.setOf(TupleValue.class), DataTypes.setOf(DataTypes.BOOLEAN), GenericType.setOf(Boolean.class), Collections.emptySet() }, { DataTypes.setOf(userType), GenericType.setOf(UdtValue.class), DataTypes.setOf(DataTypes.BOOLEAN), GenericType.setOf(Boolean.class), Collections.emptySet() }, { DataTypes.setOf(DataTypes.setOf(DataTypes.INT)), GenericType.setOf(GenericType.setOf(Integer.class)), DataTypes.setOf(DataTypes.setOf(DataTypes.BOOLEAN)), GenericType.setOf(GenericType.setOf(Boolean.class)), ImmutableSet.of(Collections.emptySet()) }, { DataTypes.setOf(DataTypes.setOf(tupleType)), GenericType.setOf(GenericType.setOf(TupleValue.class)), DataTypes.setOf(DataTypes.setOf(DataTypes.BOOLEAN)), GenericType.setOf(GenericType.setOf(Boolean.class)), ImmutableSet.of(Collections.emptySet()) }, { DataTypes.setOf(DataTypes.setOf(userType)), GenericType.setOf(GenericType.setOf(UdtValue.class)), DataTypes.setOf(DataTypes.setOf(DataTypes.BOOLEAN)), GenericType.setOf(GenericType.setOf(Boolean.class)), ImmutableSet.of(Collections.emptySet()) }, // maps
    { DataTypes.mapOf(DataTypes.INT, DataTypes.TEXT), GenericType.mapOf(Integer.class, String.class), DataTypes.mapOf(DataTypes.BOOLEAN, DataTypes.BOOLEAN), GenericType.mapOf(Boolean.class, Boolean.class), Collections.emptyMap() }, { DataTypes.mapOf(DataTypes.BLOB, DataTypes.INET), GenericType.mapOf(ByteBuffer.class, InetAddress.class), DataTypes.mapOf(DataTypes.BOOLEAN, DataTypes.BOOLEAN), GenericType.mapOf(Boolean.class, Boolean.class), Collections.emptyMap() }, { DataTypes.mapOf(tupleType, tupleType), GenericType.mapOf(TupleValue.class, TupleValue.class), DataTypes.mapOf(DataTypes.BOOLEAN, DataTypes.BOOLEAN), GenericType.mapOf(Boolean.class, Boolean.class), Collections.emptyMap() }, { DataTypes.mapOf(userType, userType), GenericType.mapOf(UdtValue.class, UdtValue.class), DataTypes.mapOf(DataTypes.BOOLEAN, DataTypes.BOOLEAN), GenericType.mapOf(Boolean.class, Boolean.class), Collections.emptyMap() }, { DataTypes.mapOf(DataTypes.UUID, DataTypes.mapOf(DataTypes.INT, DataTypes.TEXT)), GenericType.mapOf(GenericType.UUID, GenericType.mapOf(Integer.class, String.class)), DataTypes.mapOf(DataTypes.UUID, DataTypes.mapOf(DataTypes.BOOLEAN, DataTypes.BOOLEAN)), GenericType.mapOf(GenericType.UUID, GenericType.mapOf(Boolean.class, Boolean.class)), ImmutableMap.of(UUID.randomUUID(), Collections.emptyMap()) }, { DataTypes.mapOf(DataTypes.mapOf(DataTypes.INT, DataTypes.TEXT), DataTypes.UUID), GenericType.mapOf(GenericType.mapOf(Integer.class, String.class), GenericType.UUID), DataTypes.mapOf(DataTypes.mapOf(DataTypes.BOOLEAN, DataTypes.BOOLEAN), DataTypes.UUID), GenericType.mapOf(GenericType.mapOf(Boolean.class, Boolean.class), GenericType.UUID), ImmutableMap.of(Collections.emptyMap(), UUID.randomUUID()) }, { DataTypes.mapOf(DataTypes.mapOf(userType, userType), DataTypes.mapOf(tupleType, tupleType)), GenericType.mapOf(GenericType.mapOf(UdtValue.class, UdtValue.class), GenericType.mapOf(TupleValue.class, TupleValue.class)), DataTypes.mapOf(DataTypes.mapOf(DataTypes.BOOLEAN, DataTypes.BOOLEAN), DataTypes.mapOf(DataTypes.BOOLEAN, DataTypes.BOOLEAN)), GenericType.mapOf(GenericType.mapOf(Boolean.class, Boolean.class), GenericType.mapOf(Boolean.class, Boolean.class)), ImmutableMap.of(Collections.emptyMap(), Collections.emptyMap()) } };
}
Also used : BigInteger(java.math.BigInteger) UdtValue(com.datastax.oss.driver.api.core.data.UdtValue) DefaultUdtValue(com.datastax.oss.driver.internal.core.data.DefaultUdtValue) TupleType(com.datastax.oss.driver.api.core.type.TupleType) UserDefinedType(com.datastax.oss.driver.api.core.type.UserDefinedType) UserDefinedTypeBuilder(com.datastax.oss.driver.internal.core.type.UserDefinedTypeBuilder) ByteBuffer(java.nio.ByteBuffer) InetAddress(java.net.InetAddress) DefaultTupleValue(com.datastax.oss.driver.internal.core.data.DefaultTupleValue) TupleValue(com.datastax.oss.driver.api.core.data.TupleValue) DataProvider(com.tngtech.java.junit.dataprovider.DataProvider)

Example 3 with UserDefinedType

use of com.datastax.oss.driver.api.core.type.UserDefinedType in project java-driver by datastax.

the class UserDefinedTypesSimple method insertData.

private static void insertData(CqlSession session) {
    // prepare the INSERT statement
    PreparedStatement prepared = session.prepare("INSERT INTO examples.udts (k, c) VALUES (?, ?)");
    // retrieve the user-defined type metadata
    UserDefinedType coordinatesType = retrieveCoordinatesType(session);
    // bind the parameters in one pass
    UdtValue coordinates1 = coordinatesType.newValue(12, 34);
    BoundStatement boundStatement1 = prepared.bind(1, coordinates1);
    // execute the insertion
    session.execute(boundStatement1);
    // alternate method: bind the parameters one by one
    UdtValue coordinates2 = coordinatesType.newValue(56, 78);
    BoundStatement boundStatement2 = prepared.bind().setInt("k", 2).setUdtValue("c", coordinates2);
    // execute the insertion
    session.execute(boundStatement2);
}
Also used : UdtValue(com.datastax.oss.driver.api.core.data.UdtValue) UserDefinedType(com.datastax.oss.driver.api.core.type.UserDefinedType) PreparedStatement(com.datastax.oss.driver.api.core.cql.PreparedStatement) BoundStatement(com.datastax.oss.driver.api.core.cql.BoundStatement)

Example 4 with UserDefinedType

use of com.datastax.oss.driver.api.core.type.UserDefinedType 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 5 with UserDefinedType

use of com.datastax.oss.driver.api.core.type.UserDefinedType in project java-driver by datastax.

the class GraphDataTypesTest method complexTypesAndGeoTests.

@Test
public void complexTypesAndGeoTests() throws IOException {
    TupleType tuple = tupleOf(DseDataTypes.POINT, DseDataTypes.LINE_STRING, DseDataTypes.POLYGON);
    tuple.attach(context);
    verifySerDeBinary(tuple.newValue(Point.fromCoordinates(3.3, 4.4), LineString.fromPoints(Point.fromCoordinates(1, 1), Point.fromCoordinates(2, 2), Point.fromCoordinates(3, 3)), Polygon.fromPoints(Point.fromCoordinates(3, 4), Point.fromCoordinates(5, 4), Point.fromCoordinates(6, 6))));
    UserDefinedType udt = new UserDefinedTypeBuilder("ks", "udt1").withField("a", DseDataTypes.POINT).withField("b", DseDataTypes.LINE_STRING).withField("c", DseDataTypes.POLYGON).build();
    udt.attach(context);
    verifySerDeBinary(udt.newValue(Point.fromCoordinates(3.3, 4.4), LineString.fromPoints(Point.fromCoordinates(1, 1), Point.fromCoordinates(2, 2), Point.fromCoordinates(3, 3)), Polygon.fromPoints(Point.fromCoordinates(3, 4), Point.fromCoordinates(5, 4), Point.fromCoordinates(6, 6))));
}
Also used : TupleType(com.datastax.oss.driver.api.core.type.TupleType) UserDefinedType(com.datastax.oss.driver.api.core.type.UserDefinedType) UserDefinedTypeBuilder(com.datastax.oss.driver.internal.core.type.UserDefinedTypeBuilder) Test(org.junit.Test)

Aggregations

UserDefinedType (com.datastax.oss.driver.api.core.type.UserDefinedType)53 Test (org.junit.Test)33 CqlIdentifier (com.datastax.oss.driver.api.core.CqlIdentifier)21 UdtValue (com.datastax.oss.driver.api.core.data.UdtValue)20 UserDefinedTypeBuilder (com.datastax.oss.driver.internal.core.type.UserDefinedTypeBuilder)15 TupleType (com.datastax.oss.driver.api.core.type.TupleType)13 CqlSession (com.datastax.oss.driver.api.core.CqlSession)9 DataType (com.datastax.oss.driver.api.core.type.DataType)9 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)7 ListType (com.datastax.oss.driver.api.core.type.ListType)6 SetType (com.datastax.oss.driver.api.core.type.SetType)6 SessionRule (com.datastax.oss.driver.api.testinfra.session.SessionRule)6 ParallelizableTests (com.datastax.oss.driver.categories.ParallelizableTests)6 Category (org.junit.experimental.categories.Category)6 Row (com.datastax.oss.driver.api.core.cql.Row)5 SimpleStatement (com.datastax.oss.driver.api.core.cql.SimpleStatement)5 TupleValue (com.datastax.oss.driver.api.core.data.TupleValue)5 TypeCodec (com.datastax.oss.driver.api.core.type.codec.TypeCodec)5 CcmRule (com.datastax.oss.driver.api.testinfra.ccm.CcmRule)5 ResultSet (com.datastax.oss.driver.api.core.cql.ResultSet)4