/** * Extracts the JDBC value from protobuf-TypedValue representation. * * @param protoValue Protobuf TypedValue * @param calendar Instance of a calendar * @return The JDBC representation of this TypedValue */ public static Object protoToJdbc(Common.TypedValue protoValue, Calendar calendar) { Object o = getSerialFromProto(Objects.requireNonNull(protoValue)); // Shortcircuit the null if (null == o) { return o; } return serialToJdbc(Rep.fromProto(protoValue.getType()), null, o, calendar); }
/** * Extracts the JDBC value from protobuf-TypedValue representation. * * @param protoValue Protobuf TypedValue * @param calendar Instance of a calendar * @return The JDBC representation of this TypedValue */ public static Object protoToJdbc(Common.TypedValue protoValue, Calendar calendar) { Object o = getSerialFromProto(Objects.requireNonNull(protoValue)); // Shortcircuit the null if (null == o) { return o; } return serialToJdbc(Rep.fromProto(protoValue.getType()), null, o, calendar); }
/** * Constructs a {@link TypedValue} from the protocol buffer representation. * * @param proto The protobuf Typedvalue * @return A {@link TypedValue} instance */ public static TypedValue fromProto(Common.TypedValue proto) { ColumnMetaData.Rep rep = ColumnMetaData.Rep.fromProto(proto.getType()); ColumnMetaData.Rep componentRep = ColumnMetaData.Rep.fromProto(proto.getComponentType()); Object value = getSerialFromProto(proto); return new TypedValue(rep, componentRep, value); }
/** * Constructs a {@link TypedValue} from the protocol buffer representation. * * @param proto The protobuf Typedvalue * @return A {@link TypedValue} instance */ public static TypedValue fromProto(Common.TypedValue proto) { ColumnMetaData.Rep rep = ColumnMetaData.Rep.fromProto(proto.getType()); ColumnMetaData.Rep componentRep = ColumnMetaData.Rep.fromProto(proto.getComponentType()); Object value = getSerialFromProto(proto); return new TypedValue(rep, componentRep, value); }
@Test public void testBase64() { byte[] bytes = "qwertyasdf".getBytes(UTF_8); // Plain bytes get put into protobuf for simplicitly Common.TypedValue proto = Common.TypedValue.newBuilder().setBytesValue( com.google.protobuf.ByteString.copyFrom(bytes)) .setType(Common.Rep.BYTE_STRING).build(); // But we should get back a b64-string to make sure TypedValue doesn't get confused. Object deserializedObj = TypedValue.getSerialFromProto(proto); assertThat(deserializedObj, is(instanceOf(String.class))); assertEquals(new ByteString(bytes).toBase64String(), (String) deserializedObj); // But we should get a non-b64 byte array as the JDBC representation deserializedObj = TypedValue.protoToJdbc(proto, DateTimeUtils.calendar()); assertThat(deserializedObj, is(instanceOf(byte[].class))); assertArrayEquals(bytes, (byte[]) deserializedObj); }
@Test public void testBase64() { byte[] bytes = "qwertyasdf".getBytes(UTF_8); // Plain bytes get put into protobuf for simplicitly Common.TypedValue proto = Common.TypedValue.newBuilder().setBytesValue( com.google.protobuf.ByteString.copyFrom(bytes)) .setType(Common.Rep.BYTE_STRING).build(); // But we should get back a b64-string to make sure TypedValue doesn't get confused. Object deserializedObj = TypedValue.getSerialFromProto(proto); assertThat(deserializedObj, is(instanceOf(String.class))); assertEquals(new ByteString(bytes).toBase64String(), (String) deserializedObj); // But we should get a non-b64 byte array as the JDBC representation deserializedObj = TypedValue.protoToJdbc(proto, DateTimeUtils.calendar()); assertThat(deserializedObj, is(instanceOf(byte[].class))); assertArrayEquals(bytes, (byte[]) deserializedObj); }