Search in sources :

Example 26 with UserDefinedType

use of com.datastax.oss.driver.api.core.type.UserDefinedType in project jnosql-diana-driver by eclipse.

the class CassandraConverter method getUDT.

private static UDT getUDT(ColumnDefinition definition, UdtValue udtValue) {
    String name = definition.getName().asInternal();
    final UserDefinedType type = udtValue.getType();
    List<Column> columns = new ArrayList<>();
    List<String> names = type.getFieldNames().stream().map(CqlIdentifier::asInternal).collect(toList());
    for (CqlIdentifier fieldName : type.getFieldNames()) {
        final int index = names.indexOf(fieldName.asInternal());
        DataType fieldType = type.getFieldTypes().get(index);
        Object elementValue = udtValue.get(fieldName, CodecRegistry.DEFAULT.codecFor(fieldType));
        if (elementValue != null) {
            columns.add(Column.of(fieldName.asInternal(), elementValue));
        }
    }
    return UDT.builder(type.getName().asInternal()).withName(name).addUDT(columns).build();
}
Also used : Column(jakarta.nosql.column.Column) ArrayList(java.util.ArrayList) UserDefinedType(com.datastax.oss.driver.api.core.type.UserDefinedType) DataType(com.datastax.oss.driver.api.core.type.DataType) CqlIdentifier(com.datastax.oss.driver.api.core.CqlIdentifier)

Example 27 with UserDefinedType

use of com.datastax.oss.driver.api.core.type.UserDefinedType in project jnosql-diana-driver by eclipse.

the class QueryUtils method insertUDT.

private static void insertUDT(UDT udt, String keyspace, String columnFamily, CqlSession session, Map<String, Term> values) {
    final Optional<KeyspaceMetadata> keyspaceMetadata = session.getMetadata().getKeyspace(keyspace);
    UserDefinedType userType = keyspaceMetadata.flatMap(ks -> ks.getUserDefinedType(udt.getUserType())).orElseThrow(() -> new IllegalArgumentException("Missing UDT definition"));
    final TableMetadata tableMetadata = keyspaceMetadata.flatMap(k -> k.getTable(columnFamily)).orElseThrow(() -> new IllegalArgumentException("Missing Table definition"));
    final ColumnMetadata columnMetadata = tableMetadata.getColumn(getName(udt)).orElseThrow(() -> new IllegalArgumentException("Missing the column definition"));
    final DataType type = columnMetadata.getType();
    Iterable elements = Iterable.class.cast(udt.get());
    Object udtValue = getUdtValue(userType, elements, type);
    values.put(getName(udt), QueryBuilder.literal(udtValue));
}
Also used : QueryBuilder(com.datastax.oss.driver.api.querybuilder.QueryBuilder) SortType(jakarta.nosql.SortType) CqlIdentifier(com.datastax.oss.driver.api.core.CqlIdentifier) ClusteringOrder(com.datastax.oss.driver.api.core.metadata.schema.ClusteringOrder) Insert(com.datastax.oss.driver.api.querybuilder.insert.Insert) HashMap(java.util.HashMap) Function(java.util.function.Function) ValueUtil(org.eclipse.jnosql.communication.driver.ValueUtil) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) CqlSession(com.datastax.oss.driver.api.core.CqlSession) Duration(java.time.Duration) Map(java.util.Map) InsertInto(com.datastax.oss.driver.api.querybuilder.insert.InsertInto) Select(com.datastax.oss.driver.api.querybuilder.select.Select) RegularInsert(com.datastax.oss.driver.api.querybuilder.insert.RegularInsert) TableMetadata(com.datastax.oss.driver.api.core.metadata.schema.TableMetadata) ColumnEntity(jakarta.nosql.column.ColumnEntity) CodecRegistry(com.datastax.oss.driver.api.core.type.codec.registry.CodecRegistry) Collection(java.util.Collection) DataType(com.datastax.oss.driver.api.core.type.DataType) Collectors(java.util.stream.Collectors) UdtValue(com.datastax.oss.driver.api.core.data.UdtValue) ColumnMetadata(com.datastax.oss.driver.api.core.metadata.schema.ColumnMetadata) ProtocolConstants(com.datastax.oss.protocol.internal.ProtocolConstants) UserDefinedType(com.datastax.oss.driver.api.core.type.UserDefinedType) KeyspaceMetadata(com.datastax.oss.driver.api.core.metadata.schema.KeyspaceMetadata) Value(jakarta.nosql.Value) List(java.util.List) Column(jakarta.nosql.column.Column) CodecNotFoundException(com.datastax.oss.driver.api.core.type.codec.CodecNotFoundException) TypeCodec(com.datastax.oss.driver.api.core.type.codec.TypeCodec) Optional(java.util.Optional) SetType(com.datastax.oss.driver.api.core.type.SetType) Term(com.datastax.oss.driver.api.querybuilder.term.Term) ColumnQuery(jakarta.nosql.column.ColumnQuery) Sort(jakarta.nosql.Sort) CommunicationException(jakarta.nosql.CommunicationException) TableMetadata(com.datastax.oss.driver.api.core.metadata.schema.TableMetadata) ColumnMetadata(com.datastax.oss.driver.api.core.metadata.schema.ColumnMetadata) UserDefinedType(com.datastax.oss.driver.api.core.type.UserDefinedType) DataType(com.datastax.oss.driver.api.core.type.DataType) KeyspaceMetadata(com.datastax.oss.driver.api.core.metadata.schema.KeyspaceMetadata)

Example 28 with UserDefinedType

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

the class DataTypeCqlNameParser method parse.

private DataType parse(String toParse, CqlIdentifier keyspaceId, boolean frozen, Map<CqlIdentifier, UserDefinedType> userTypes, InternalDriverContext context) {
    if (toParse.startsWith("'")) {
        return DataTypes.custom(toParse.substring(1, toParse.length() - 1));
    }
    Parser parser = new Parser(toParse, 0);
    String type = parser.parseTypeName();
    if (type.equalsIgnoreCase(RawColumn.THRIFT_EMPTY_TYPE)) {
        return DataTypes.custom(type);
    }
    DataType nativeType = NATIVE_TYPES_BY_NAME.get(type.toLowerCase(Locale.ROOT));
    if (nativeType != null) {
        return nativeType;
    }
    if (parser.isEOS()) {
        // No parameters => it's a UDT
        CqlIdentifier name = CqlIdentifier.fromCql(type);
        if (userTypes != null) {
            UserDefinedType userType = userTypes.get(name);
            if (userType == null) {
                throw new IllegalStateException(String.format("Can't find referenced user type %s", type));
            }
            return userType.copy(frozen);
        } else {
            return new ShallowUserDefinedType(keyspaceId, name, frozen);
        }
    }
    List<String> parameters = parser.parseTypeParameters();
    if (type.equalsIgnoreCase("list")) {
        if (parameters.size() != 1) {
            throw new IllegalArgumentException(String.format("Expecting single parameter for list, got %s", parameters));
        }
        DataType elementType = parse(parameters.get(0), keyspaceId, false, userTypes, context);
        return DataTypes.listOf(elementType, frozen);
    }
    if (type.equalsIgnoreCase("set")) {
        if (parameters.size() != 1) {
            throw new IllegalArgumentException(String.format("Expecting single parameter for set, got %s", parameters));
        }
        DataType elementType = parse(parameters.get(0), keyspaceId, false, userTypes, context);
        return DataTypes.setOf(elementType, frozen);
    }
    if (type.equalsIgnoreCase("map")) {
        if (parameters.size() != 2) {
            throw new IllegalArgumentException(String.format("Expecting two parameters for map, got %s", parameters));
        }
        DataType keyType = parse(parameters.get(0), keyspaceId, false, userTypes, context);
        DataType valueType = parse(parameters.get(1), keyspaceId, false, userTypes, context);
        return DataTypes.mapOf(keyType, valueType, frozen);
    }
    if (type.equalsIgnoreCase("frozen")) {
        if (parameters.size() != 1) {
            throw new IllegalArgumentException(String.format("Expecting single parameter for frozen keyword, got %s", parameters));
        }
        return parse(parameters.get(0), keyspaceId, true, userTypes, context);
    }
    if (type.equalsIgnoreCase("tuple")) {
        if (parameters.isEmpty()) {
            throw new IllegalArgumentException("Expecting at list one parameter for tuple, got none");
        }
        ImmutableList.Builder<DataType> componentTypesBuilder = ImmutableList.builder();
        for (String rawType : parameters) {
            componentTypesBuilder.add(parse(rawType, keyspaceId, false, userTypes, context));
        }
        return new DefaultTupleType(componentTypesBuilder.build(), context);
    }
    throw new IllegalArgumentException("Could not parse type name " + toParse);
}
Also used : DefaultTupleType(com.datastax.oss.driver.internal.core.type.DefaultTupleType) ShallowUserDefinedType(com.datastax.oss.driver.internal.core.metadata.schema.ShallowUserDefinedType) ImmutableList(com.datastax.oss.driver.shaded.guava.common.collect.ImmutableList) DataType(com.datastax.oss.driver.api.core.type.DataType) UserDefinedType(com.datastax.oss.driver.api.core.type.UserDefinedType) ShallowUserDefinedType(com.datastax.oss.driver.internal.core.metadata.schema.ShallowUserDefinedType) CqlIdentifier(com.datastax.oss.driver.api.core.CqlIdentifier)

Example 29 with UserDefinedType

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

the class CassandraSchemaParser method parseKeyspace.

private KeyspaceMetadata parseKeyspace(AdminRow keyspaceRow) {
    // Cassandra <= 2.2
    // CREATE TABLE system.schema_keyspaces (
    // keyspace_name text PRIMARY KEY,
    // durable_writes boolean,
    // strategy_class text,
    // strategy_options text
    // )
    // 
    // Cassandra >= 3.0:
    // CREATE TABLE system_schema.keyspaces (
    // keyspace_name text PRIMARY KEY,
    // durable_writes boolean,
    // replication frozen<map<text, text>>
    // )
    CqlIdentifier keyspaceId = CqlIdentifier.fromInternal(keyspaceRow.getString("keyspace_name"));
    boolean durableWrites = MoreObjects.firstNonNull(keyspaceRow.getBoolean("durable_writes"), false);
    Map<String, String> replicationOptions;
    if (keyspaceRow.contains("strategy_class")) {
        String strategyClass = keyspaceRow.getString("strategy_class");
        Map<String, String> strategyOptions = SimpleJsonParser.parseStringMap(keyspaceRow.getString("strategy_options"));
        replicationOptions = ImmutableMap.<String, String>builder().putAll(strategyOptions).put("class", strategyClass).build();
    } else {
        replicationOptions = keyspaceRow.getMapOfStringToString("replication");
    }
    Map<CqlIdentifier, UserDefinedType> types = parseTypes(keyspaceId);
    return new DefaultKeyspaceMetadata(keyspaceId, durableWrites, false, replicationOptions, types, parseTables(keyspaceId, types), parseViews(keyspaceId, types), parseFunctions(keyspaceId, types), parseAggregates(keyspaceId, types));
}
Also used : DefaultKeyspaceMetadata(com.datastax.oss.driver.internal.core.metadata.schema.DefaultKeyspaceMetadata) UserDefinedType(com.datastax.oss.driver.api.core.type.UserDefinedType) CqlIdentifier(com.datastax.oss.driver.api.core.CqlIdentifier)

Example 30 with UserDefinedType

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

the class DefaultUdtValueTest method should_equate_instances_with_same_values_but_different_binary_representations.

@Test
public void should_equate_instances_with_same_values_but_different_binary_representations() {
    UserDefinedType type = new UserDefinedTypeBuilder(CqlIdentifier.fromInternal("ks"), CqlIdentifier.fromInternal("type")).withField(CqlIdentifier.fromInternal("f"), DataTypes.VARINT).build();
    UdtValue udt1 = type.newValue().setBytesUnsafe(0, Bytes.fromHexString("0x01"));
    UdtValue udt2 = type.newValue().setBytesUnsafe(0, Bytes.fromHexString("0x0001"));
    assertThat(udt1).isEqualTo(udt2);
}
Also used : UdtValue(com.datastax.oss.driver.api.core.data.UdtValue) 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