kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 00fb99dbb7974596be2d9528c3b411f632fca4963a9c1a33d5b82a23af1d0285 |
|---|---|
| Author | Alexandre Dutra <alexandre.dutra@datastax.com> · 2016-09-06 18:54 +02:00 |
| Committer | olim7t <omichallat+github@gmail.com> · 2016-09-16 10:45 -07:00 |
| Parents | c0702f73685f |
2 files changed
modified driver-mapping/src/main/java/com/datastax/driver/mapping/MappedUDTCodec.java +26 −4
| @@ -87,13 +87,35 @@ | ||
| 87 | 87 | |
| 88 | 88 | @Override |
| 89 | 89 | protected String formatField(T source, String fieldName) { |
| 90 | - // This codec implementation is internal-use only, and its format method is never used | |
| 91 | - throw new UnsupportedOperationException(); | |
| 90 | + if (!fieldName.startsWith("\"")) | |
| 91 | + fieldName = Metadata.quote(fieldName); | |
| 92 | + | |
| 93 | + ColumnMapper<T> columnMapper = columnMappers.get(fieldName); | |
| 94 | + | |
| 95 | + if (columnMapper == null) | |
| 96 | + return null; | |
| 97 | + | |
| 98 | + Object value = columnMapper.getValue(source); | |
| 99 | + | |
| 100 | + TypeCodec<Object> codec = columnMapper.getCustomCodec(); | |
| 101 | + if (codec == null) | |
| 102 | + codec = codecRegistry.codecFor(cqlUserType.getFieldType(columnMapper.getColumnName()), columnMapper.getJavaType()); | |
| 103 | + | |
| 104 | + return codec.format(value); | |
| 92 | 105 | } |
| 93 | 106 | |
| 94 | 107 | @Override |
| 95 | 108 | protected T parseAndSetField(String input, T target, String fieldName) { |
| 96 | - // This codec implementation is internal-use only, and its parse method is never used | |
| 97 | - throw new UnsupportedOperationException(); | |
| 109 | + if (!fieldName.startsWith("\"")) | |
| 110 | + fieldName = Metadata.quote(fieldName); | |
| 111 | + | |
| 112 | + ColumnMapper<T> columnMapper = columnMappers.get(fieldName); | |
| 113 | + if (columnMapper != null) { | |
| 114 | + TypeCodec<Object> codec = columnMapper.getCustomCodec(); | |
| 115 | + if (codec == null) | |
| 116 | + codec = codecRegistry.codecFor(cqlUserType.getFieldType(columnMapper.getColumnName()), columnMapper.getJavaType()); | |
| 117 | + columnMapper.setValue(target, codec.parse(input)); | |
| 118 | + } | |
| 119 | + return target; | |
| 98 | 120 | } |
| 99 | 121 | } |
modified driver-mapping/src/test/java/com/datastax/driver/mapping/MapperUDTTest.java +45 −0
| @@ -18,6 +18,7 @@ | ||
| 18 | 18 | import com.datastax.driver.core.*; |
| 19 | 19 | import com.datastax.driver.core.exceptions.CodecNotFoundException; |
| 20 | 20 | import com.datastax.driver.core.exceptions.InvalidQueryException; |
| 21 | +import com.datastax.driver.core.querybuilder.BuiltStatement; | |
| 21 | 22 | import com.datastax.driver.core.utils.CassandraVersion; |
| 22 | 23 | import com.datastax.driver.core.utils.UUIDs; |
| 23 | 24 | import com.datastax.driver.mapping.annotations.*; |
| @@ -31,6 +32,7 @@ | ||
| 31 | 32 | import java.util.*; |
| 32 | 33 | |
| 33 | 34 | import static com.datastax.driver.core.CreateCCM.TestMode.PER_METHOD; |
| 35 | +import static com.datastax.driver.core.querybuilder.QueryBuilder.*; | |
| 34 | 36 | import static org.assertj.core.api.Assertions.assertThat; |
| 35 | 37 | import static org.assertj.core.api.Assertions.fail; |
| 36 | 38 | import static org.testng.Assert.*; |
| @@ -728,4 +730,47 @@ | ||
| 728 | 730 | manager.mapper(UserWithAddressUnknownField.class); |
| 729 | 731 | } |
| 730 | 732 | |
| 733 | + /** | |
| 734 | + * Ensures that MappedUDTCodec is able to properly format UDTs when printing the query string of a BuiltStatement. | |
| 735 | + * | |
| 736 | + * @jira_ticket JAVA-1272 | |
| 737 | + * @test_category object_mapper | |
| 738 | + */ | |
| 739 | + @Test(groups = "short") | |
| 740 | + public void should_format_mapped_udt() throws Exception { | |
| 741 | + MappingManager manager = new MappingManager(session()); | |
| 742 | + UUID uuid = UUIDs.random(); | |
| 743 | + BuiltStatement update = | |
| 744 | + update("users") | |
| 745 | + .with(set("mainaddress", new Address("12 4th Street", "Springfield", 12345, "12341343", "435423245"))) | |
| 746 | + .where(eq("user_id", uuid)); | |
| 747 | + CodecRegistry codecRegistry = cluster().getConfiguration().getCodecRegistry(); | |
| 748 | + codecRegistry.register(manager.udtCodec(Address.class)); | |
| 749 | + String queryString = update.getQueryString(codecRegistry); | |
| 750 | + assertThat(queryString) | |
| 751 | + .isEqualTo("UPDATE users SET mainaddress=? WHERE user_id=?;"); | |
| 752 | + update.setForceNoValues(true); | |
| 753 | + queryString = update.getQueryString(codecRegistry); | |
| 754 | + assertThat(queryString).isEqualTo( | |
| 755 | + "UPDATE users " + | |
| 756 | + "SET mainaddress={street:'12 4th Street',city:'Springfield',\"ZIP_code\":12345,phones:{'435423245','12341343'}} " + | |
| 757 | + "WHERE user_id=" + uuid + ";"); | |
| 758 | + // check that the query string is valid | |
| 759 | + session().execute(queryString); | |
| 760 | + } | |
| 761 | + | |
| 762 | + /** | |
| 763 | + * Ensures that MappedUDTCodec is able to properly parse UDTs. | |
| 764 | + * | |
| 765 | + * @jira_ticket JAVA-1272 | |
| 766 | + * @test_category object_mapper | |
| 767 | + */ | |
| 768 | + @Test(groups = "short") | |
| 769 | + public void should_parse_mapped_udt() throws Exception { | |
| 770 | + MappingManager manager = new MappingManager(session()); | |
| 771 | + TypeCodec<Address> codec = manager.udtCodec(Address.class); | |
| 772 | + Address actual = codec.parse("{street:'12 4th Street',city:'Springfield',\"ZIP_code\":12345,phones:{'435423245','12341343'}}"); | |
| 773 | + assertThat(actual).isEqualTo(new Address("12 4th Street", "Springfield", 12345, "12341343", "435423245")); | |
| 774 | + } | |
| 775 | + | |
| 731 | 776 | } |