use of com.datastax.oss.driver.api.core.type.UserDefinedType in project java-driver by datastax.
the class DataTypeIT method typeFor.
private static String typeFor(DataType dataType) {
String typeName = dataType.asCql(true, true);
if (dataType instanceof UserDefinedType) {
UserDefinedType udt = (UserDefinedType) dataType;
// Create type if it doesn't already exist.
List<String> fieldParts = new ArrayList<>();
for (int i = 0; i < udt.getFieldNames().size(); i++) {
String fieldName = udt.getFieldNames().get(i).asCql(false);
String fieldType = typeFor(udt.getFieldTypes().get(i));
fieldParts.add(fieldName + " " + fieldType);
}
SESSION_RULE.session().execute(SimpleStatement.builder(String.format("CREATE TYPE IF NOT EXISTS %s (%s)", udt.getName().asCql(false), String.join(",", fieldParts))).setExecutionProfile(SESSION_RULE.slowProfile()).build());
// older versions of C* don't support non-frozen UDTs.
if (!udt.isFrozen()) {
typeName = "frozen<" + typeName + ">";
}
}
return typeName;
}
use of com.datastax.oss.driver.api.core.type.UserDefinedType in project java-driver by datastax.
the class CodecRegistryIT method should_register_and_use_custom_codec_for_user_defined_type.
@Test
public void should_register_and_use_custom_codec_for_user_defined_type() {
Map<String, Coordinates> coordinatesMap = ImmutableMap.of("home", new Coordinates(12, 34));
GenericType<Map<String, Coordinates>> coordinatesMapType = GenericType.mapOf(String.class, Coordinates.class);
// Still create a separate session because we don't want to interfere with other tests
try (CqlSession session = SessionUtils.newSession(CCM_RULE, SESSION_RULE.keyspace())) {
// register the mapping codec for UDT coordinates
UserDefinedType coordinatesUdt = session.getMetadata().getKeyspace(SESSION_RULE.keyspace()).flatMap(ks -> ks.getUserDefinedType("coordinates")).orElseThrow(IllegalStateException::new);
MutableCodecRegistry codecRegistry = (MutableCodecRegistry) session.getContext().getCodecRegistry();
// Retrieve the inner codec
TypeCodec<UdtValue> innerCodec = codecRegistry.codecFor(coordinatesUdt);
assertThat(innerCodec).isInstanceOf(UdtCodec.class);
// Create the "outer" codec and register it
CoordinatesCodec coordinatesCodec = new CoordinatesCodec(innerCodec);
codecRegistry.register(coordinatesCodec);
// Test that the codec will be used to create on-the-fly codecs
assertThat(codecRegistry.codecFor(Coordinates.class)).isSameAs(coordinatesCodec);
assertThat(codecRegistry.codecFor(coordinatesMapType).accepts(coordinatesMap)).isTrue();
// test insertion
PreparedStatement prepared = session.prepare("INSERT INTO test3 (k0, k1, v) values (?, ?, ?)");
BoundStatement insert = prepared.boundStatementBuilder().setString(0, name.getMethodName()).setInt(1, 0).set(2, coordinatesMap, // use java type so has to be looked up in registry.
coordinatesMapType).build();
session.execute(insert);
// test retrieval
ResultSet result = session.execute(SimpleStatement.builder("SELECT v from test3 where k0 = ? AND k1 = ?").addPositionalValues(name.getMethodName(), 0).build());
List<Row> rows = result.all();
assertThat(rows).hasSize(1);
Row row = rows.get(0);
assertThat(row.get(0, coordinatesMapType)).isEqualTo(coordinatesMap);
assertThat(row.getMap(0, String.class, Coordinates.class)).isEqualTo(coordinatesMap);
}
}
use of com.datastax.oss.driver.api.core.type.UserDefinedType in project java-driver by datastax.
the class TermTest method should_generate_literal_terms.
@Test
public void should_generate_literal_terms() {
assertThat(literal(1)).hasCql("1");
assertThat(literal("foo")).hasCql("'foo'");
assertThat(literal(ImmutableList.of(1, 2, 3))).hasCql("[1,2,3]");
TupleType tupleType = DataTypes.tupleOf(DataTypes.INT, DataTypes.TEXT);
TupleValue tupleValue = tupleType.newValue().setInt(0, 1).setString(1, "foo");
assertThat(literal(tupleValue)).hasCql("(1,'foo')");
UserDefinedType udtType = new UserDefinedTypeBuilder(CqlIdentifier.fromCql("ks"), CqlIdentifier.fromCql("user")).withField(CqlIdentifier.fromCql("first_name"), DataTypes.TEXT).withField(CqlIdentifier.fromCql("last_name"), DataTypes.TEXT).build();
UdtValue udtValue = udtType.newValue().setString("first_name", "Jane").setString("last_name", "Doe");
assertThat(literal(udtValue)).hasCql("{first_name:'Jane',last_name:'Doe'}");
assertThat(literal(null)).hasCql("NULL");
assertThat(literal(Charsets.UTF_8, new CharsetCodec())).hasCql("'UTF-8'");
assertThat(literal(Charsets.UTF_8, CharsetCodec.TEST_REGISTRY)).hasCql("'UTF-8'");
}
Aggregations