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();
}
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));
}
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);
}
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));
}
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);
}
Aggregations