kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 21d094a922a5e13b1b7fe5f7ac07251a57d858a5663df9aebee1ca2547854cab |
|---|---|
| Author | Kevin Gallardo <kevin.gallardo@datastax.com> · 2015-05-22 10:37 +01:00 |
| Committer | olim7t <omichallat+github@gmail.com> · 2015-07-01 15:18 +02:00 |
| Parents | 22494c9e09ae |
7 files changed
modified changelog/README.md +1 −0
| @@ -21,6 +21,7 @@ | ||
| 21 | 21 | - [improvement] Support CONTAINS / CONTAINS KEY in QueryBuilder (JAVA-677) |
| 22 | 22 | - [improvement] Add USING options in mapper for delete and save |
| 23 | 23 | operations (JAVA-477/JAVA-540) |
| 24 | +- [improvement] Add mapper option to configure whether to save null fields (JAVA-473) | |
| 24 | 25 | |
| 25 | 26 | Merged from 2.0 branch: |
| 26 | 27 |
modified driver-mapping/src/main/java/com/datastax/driver/mapping/Mapper.java +93 −15
| @@ -32,9 +32,12 @@ | ||
| 32 | 32 | import com.datastax.driver.core.querybuilder.Delete; |
| 33 | 33 | import com.datastax.driver.core.querybuilder.Insert; |
| 34 | 34 | import com.datastax.driver.core.querybuilder.QueryBuilder; |
| 35 | +import com.datastax.driver.mapping.Mapper.Option.SaveNullFields; | |
| 35 | 36 | import com.datastax.driver.mapping.annotations.Accessor; |
| 36 | 37 | import com.datastax.driver.mapping.annotations.Computed; |
| 37 | 38 | |
| 39 | +import static com.datastax.driver.mapping.Mapper.Option.Type.SAVE_NULL_FIELDS; | |
| 40 | + | |
| 38 | 41 | /** |
| 39 | 42 | * An object handling the mapping of a particular class. |
| 40 | 43 | * <p> |
| @@ -100,16 +103,16 @@ | ||
| 100 | 103 | return manager.getSession(); |
| 101 | 104 | } |
| 102 | 105 | |
| 103 | - PreparedStatement getPreparedQuery(QueryType type, EnumMap<Option.Type, Option> options) { | |
| 106 | + PreparedStatement getPreparedQuery(QueryType type, Set<ColumnMapper<?>> columns, EnumMap<Option.Type, Option> options) { | |
| 104 | 107 | |
| 105 | - MapperQueryKey pqk = new MapperQueryKey(type, options); | |
| 108 | + MapperQueryKey pqk = new MapperQueryKey(type, columns, options); | |
| 106 | 109 | |
| 107 | 110 | PreparedStatement stmt = preparedQueries.get(pqk); |
| 108 | 111 | if (stmt == null) { |
| 109 | 112 | synchronized (preparedQueries) { |
| 110 | 113 | stmt = preparedQueries.get(pqk); |
| 111 | 114 | if (stmt == null) { |
| 112 | - String queryString = type.makePreparedQueryString(tableMetadata, mapper, manager, options.values()); | |
| 115 | + String queryString = type.makePreparedQueryString(tableMetadata, mapper, manager, columns, options.values()); | |
| 113 | 116 | logger.debug("Preparing query {}", queryString); |
| 114 | 117 | stmt = session().prepare(queryString); |
| 115 | 118 | Map<MapperQueryKey, PreparedStatement> newQueries = new HashMap<MapperQueryKey, PreparedStatement>(preparedQueries); |
| @@ -121,6 +124,10 @@ | ||
| 121 | 124 | return stmt; |
| 122 | 125 | } |
| 123 | 126 | |
| 127 | + PreparedStatement getPreparedQuery(QueryType type, EnumMap<Option.Type, Option> options) { | |
| 128 | + return getPreparedQuery(type, Collections.<ColumnMapper<?>>emptySet(), options); | |
| 129 | + } | |
| 130 | + | |
| 124 | 131 | /** |
| 125 | 132 | * The {@code TableMetadata} for this mapper. |
| 126 | 133 | * |
| @@ -180,15 +187,24 @@ | ||
| 180 | 187 | } |
| 181 | 188 | |
| 182 | 189 | private Statement saveQuery(T entity, EnumMap<Option.Type, Option> options) { |
| 183 | - BoundStatement bs = getPreparedQuery(QueryType.SAVE, options).bind(); | |
| 184 | - int i = 0; | |
| 190 | + Map<ColumnMapper<?>, Object> values = new HashMap<ColumnMapper<?>, Object>(); | |
| 191 | + boolean saveNullFields = shouldSaveNullFields(options); | |
| 192 | + | |
| 185 | 193 | for (ColumnMapper<T> cm : mapper.allColumns()) { |
| 186 | - if (cm.kind != ColumnMapper.Kind.COMPUTED) { | |
| 187 | - Object value = cm.getValue(entity); | |
| 188 | - bs.setBytesUnsafe(i++, value == null ? null : cm.getDataType().serialize(value, protocolVersion)); | |
| 194 | + Object value = cm.getValue(entity); | |
| 195 | + if (cm.kind != ColumnMapper.Kind.COMPUTED && (saveNullFields || value != null)) { | |
| 196 | + values.put(cm, value); | |
| 189 | 197 | } |
| 190 | 198 | } |
| 191 | 199 | |
| 200 | + BoundStatement bs = getPreparedQuery(QueryType.SAVE, values.keySet(), options).bind(); | |
| 201 | + int i = 0; | |
| 202 | + for (Map.Entry<ColumnMapper<?>, Object> entry : values.entrySet()) { | |
| 203 | + DataType type = entry.getKey().getDataType(); | |
| 204 | + Object value = entry.getValue(); | |
| 205 | + bs.setBytesUnsafe(i++, value == null ? null : type.serialize(value, protocolVersion)); | |
| 206 | + } | |
| 207 | + | |
| 192 | 208 | if (mapper.writeConsistency != null) |
| 193 | 209 | bs.setConsistencyLevel(mapper.writeConsistency); |
| 194 | 210 | |
| @@ -200,6 +216,11 @@ | ||
| 200 | 216 | return bs; |
| 201 | 217 | } |
| 202 | 218 | |
| 219 | + private static boolean shouldSaveNullFields(EnumMap<Option.Type, Option> options) { | |
| 220 | + SaveNullFields option = (SaveNullFields)options.get(SAVE_NULL_FIELDS); | |
| 221 | + return option == null || option.saveNullFields; | |
| 222 | + } | |
| 223 | + | |
| 203 | 224 | /** |
| 204 | 225 | * Save an entity mapped by this mapper. |
| 205 | 226 | * <p> |
| @@ -307,8 +328,7 @@ | ||
| 307 | 328 | if (value == null) { |
| 308 | 329 | throw new IllegalArgumentException(String.format("Invalid null value for PRIMARY KEY column %s (argument %d)", column.getColumnName(), i)); |
| 309 | 330 | } |
| 310 | - bs.setBytesUnsafe(i, column.getDataType().serialize(value, protocolVersion)); | |
| 311 | - i++; | |
| 331 | + bs.setBytesUnsafe(i++, column.getDataType().serialize(value, protocolVersion)); | |
| 312 | 332 | } |
| 313 | 333 | |
| 314 | 334 | if (mapper.readConsistency != null) |
| @@ -709,7 +729,7 @@ | ||
| 709 | 729 | */ |
| 710 | 730 | public static abstract class Option { |
| 711 | 731 | |
| 712 | - enum Type {TTL, TIMESTAMP, CL, TRACING} | |
| 732 | + enum Type {TTL, TIMESTAMP, CL, TRACING, SAVE_NULL_FIELDS} | |
| 713 | 733 | |
| 714 | 734 | final Type type; |
| 715 | 735 | |
| @@ -760,7 +780,8 @@ | ||
| 760 | 780 | } |
| 761 | 781 | |
| 762 | 782 | /** |
| 763 | - * Creates a new Option object to enable query tracing for a mapper operation. | |
| 783 | + * Creates a new Option object to enable query tracing for a mapper operation. This | |
| 784 | + * is valid for save, delete and get operations. | |
| 764 | 785 | * |
| 765 | 786 | * @param enabled whether to enable tracing. |
| 766 | 787 | * @return the option. |
| @@ -769,6 +790,23 @@ | ||
| 769 | 790 | return new Tracing(enabled); |
| 770 | 791 | } |
| 771 | 792 | |
| 793 | + /** | |
| 794 | + * Creates a new Option object to specify whether null entity fields should be included in | |
| 795 | + * insert queries. This option is valid only for save operations. | |
| 796 | + * | |
| 797 | + * If this option is not specified, it defaults to {@code true} (null fields are saved). | |
| 798 | + * | |
| 799 | + * @param enabled whether to include null fields in queries. | |
| 800 | + * @return the option. | |
| 801 | + */ | |
| 802 | + public static Option saveNullFields(boolean enabled){ | |
| 803 | + return new SaveNullFields(enabled); | |
| 804 | + } | |
| 805 | + | |
| 806 | + public Type getType(){ | |
| 807 | + return this.type; | |
| 808 | + } | |
| 809 | + | |
| 772 | 810 | abstract void appendTo(Insert.Options usings); |
| 773 | 811 | |
| 774 | 812 | abstract void appendTo(Delete.Options usings); |
| @@ -908,16 +946,55 @@ | ||
| 908 | 946 | return false; |
| 909 | 947 | } |
| 910 | 948 | } |
| 949 | + | |
| 950 | + static class SaveNullFields extends Option { | |
| 951 | + | |
| 952 | + private boolean saveNullFields; | |
| 953 | + | |
| 954 | + SaveNullFields(boolean saveNullFields) { | |
| 955 | + super(SAVE_NULL_FIELDS); | |
| 956 | + this.saveNullFields = saveNullFields; | |
| 957 | + } | |
| 958 | + | |
| 959 | + @Override | |
| 960 | + void appendTo(Insert.Options usings) { | |
| 961 | + throw new UnsupportedOperationException("shouldn't be called"); | |
| 962 | + } | |
| 963 | + | |
| 964 | + @Override | |
| 965 | + void appendTo(Delete.Options usings) { | |
| 966 | + throw new UnsupportedOperationException("shouldn't be called"); | |
| 967 | + } | |
| 968 | + | |
| 969 | + @Override | |
| 970 | + void addToPreparedStatement(BoundStatement bs, int i) { | |
| 971 | + // nothing to do | |
| 972 | + } | |
| 973 | + | |
| 974 | + @Override | |
| 975 | + void checkValidFor(QueryType qt, MappingManager manager) { | |
| 976 | + checkArgument(qt == QueryType.SAVE, "SaveNullFields option is only allowed in save queries"); | |
| 977 | + } | |
| 978 | + | |
| 979 | + @Override | |
| 980 | + boolean isIncludedInQuery() { | |
| 981 | + return false; | |
| 982 | + } | |
| 983 | + } | |
| 984 | + | |
| 911 | 985 | } |
| 912 | 986 | |
| 913 | 987 | private static class MapperQueryKey { |
| 914 | 988 | private final QueryType queryType; |
| 915 | 989 | private final EnumSet<Option.Type> optionTypes; |
| 990 | + private final Set<ColumnMapper<?>> columns; | |
| 916 | 991 | |
| 917 | - MapperQueryKey(QueryType queryType, EnumMap<Option.Type, Option> options) { | |
| 992 | + MapperQueryKey(QueryType queryType, Set<ColumnMapper<?>> columnMappers, EnumMap<Option.Type, Option> options) { | |
| 918 | 993 | Preconditions.checkNotNull(queryType); |
| 919 | 994 | Preconditions.checkNotNull(options); |
| 995 | + Preconditions.checkNotNull(columnMappers); | |
| 920 | 996 | this.queryType = queryType; |
| 997 | + this.columns = columnMappers; | |
| 921 | 998 | this.optionTypes = EnumSet.noneOf(Option.Type.class); |
| 922 | 999 | for (Option opt : options.values()) { |
| 923 | 1000 | if (opt.isIncludedInQuery()) |
| @@ -932,13 +1009,14 @@ | ||
| 932 | 1009 | if (other instanceof MapperQueryKey) { |
| 933 | 1010 | MapperQueryKey that = (MapperQueryKey)other; |
| 934 | 1011 | return this.queryType.equals(that.queryType) |
| 935 | - && this.optionTypes.equals(that.optionTypes); | |
| 1012 | + && this.optionTypes.equals(that.optionTypes) | |
| 1013 | + && this.columns.equals(that.columns); | |
| 936 | 1014 | } |
| 937 | 1015 | return false; |
| 938 | 1016 | } |
| 939 | 1017 | |
| 940 | 1018 | public int hashCode() { |
| 941 | - return Objects.hashCode(queryType, optionTypes); | |
| 1019 | + return Objects.hashCode(queryType, optionTypes, columns); | |
| 942 | 1020 | } |
| 943 | 1021 | } |
| 944 | 1022 | } |
modified driver-mapping/src/main/java/com/datastax/driver/mapping/QueryType.java +3 −2
| @@ -18,6 +18,7 @@ | ||
| 18 | 18 | import java.util.ArrayList; |
| 19 | 19 | import java.util.Collection; |
| 20 | 20 | import java.util.List; |
| 21 | +import java.util.Set; | |
| 21 | 22 | |
| 22 | 23 | import com.google.common.base.Objects; |
| 23 | 24 | |
| @@ -60,13 +61,13 @@ | ||
| 60 | 61 | return new QueryType(reversed ? Kind.REVERSED_SLICE : Kind.SLICE, startBoundSize, startInclusive, endBoundSize, endInclusive); |
| 61 | 62 | } |
| 62 | 63 | |
| 63 | - String makePreparedQueryString(TableMetadata table, EntityMapper<?> mapper, MappingManager manager, Collection<Mapper.Option> options) { | |
| 64 | + String makePreparedQueryString(TableMetadata table, EntityMapper<?> mapper, MappingManager manager, Set<ColumnMapper<?>> columns, Collection<Mapper.Option> options) { | |
| 64 | 65 | switch (kind) { |
| 65 | 66 | case SAVE: { |
| 66 | 67 | Insert insert = table == null |
| 67 | 68 | ? insertInto(mapper.getKeyspace(), mapper.getTable()) |
| 68 | 69 | : insertInto(table); |
| 69 | - for (ColumnMapper<?> cm : mapper.allColumns()) | |
| 70 | + for (ColumnMapper<?> cm : columns) | |
| 70 | 71 | if (cm.kind != ColumnMapper.Kind.COMPUTED) |
| 71 | 72 | insert.value(cm.getColumnName(), bindMarker()); |
| 72 | 73 |
modified driver-mapping/src/test/java/com/datastax/driver/mapping/MapperComputedFieldsTest.java +19 −1
| @@ -18,9 +18,11 @@ | ||
| 18 | 18 | import java.util.Collection; |
| 19 | 19 | |
| 20 | 20 | import com.google.common.collect.Lists; |
| 21 | +import org.testng.SkipException; | |
| 21 | 22 | import org.testng.annotations.BeforeMethod; |
| 22 | 23 | import org.testng.annotations.Test; |
| 23 | 24 | |
| 25 | +import static com.datastax.driver.core.ProtocolVersion.V1; | |
| 24 | 26 | import static org.assertj.core.api.Assertions.fail; |
| 25 | 27 | |
| 26 | 28 | import com.datastax.driver.core.*; |
| @@ -35,7 +37,6 @@ | ||
| 35 | 37 | * annotation to map computed fields. |
| 36 | 38 | */ |
| 37 | 39 | @SuppressWarnings("unused") |
| 38 | -@CassandraVersion(major = 2.0) | |
| 39 | 40 | public class MapperComputedFieldsTest extends CCMBridge.PerClassSingleNodeCluster { |
| 40 | 41 | |
| 41 | 42 | @Override |
| @@ -45,16 +46,28 @@ | ||
| 45 | 46 | "INSERT INTO user (login, name) VALUES ('testlogin', 'test name')"); |
| 46 | 47 | } |
| 47 | 48 | |
| 49 | + ProtocolVersion protocolVersion; | |
| 48 | 50 | MappingManager mappingManager; |
| 49 | 51 | Mapper<User> userMapper; |
| 50 | 52 | |
| 51 | 53 | @BeforeMethod(groups = "short") |
| 52 | 54 | void setup() { |
| 53 | 55 | mappingManager = new MappingManager(session); |
| 56 | + protocolVersion = cluster.getConfiguration().getProtocolOptions().getProtocolVersionEnum(); | |
| 57 | + if (protocolVersion.compareTo(V1) > 0) | |
| 58 | + userMapper = mappingManager.mapper(User.class); | |
| 59 | + } | |
| 60 | + | |
| 61 | + @Test(groups = "short", expectedExceptions = { UnsupportedOperationException.class }) | |
| 62 | + void should_get_unsupported_operation_exception_on_v1() { | |
| 63 | + if (protocolVersion.compareTo(V1) > 0) | |
| 64 | + throw new SkipException("Skipped when protocol version > V1"); | |
| 65 | + | |
| 54 | 66 | userMapper = mappingManager.mapper(User.class); |
| 55 | 67 | } |
| 56 | 68 | |
| 57 | 69 | @Test(groups = "short") |
| 70 | + @CassandraVersion(major = 2.0) | |
| 58 | 71 | void should_save_and_get_entity_with_computed_fields() { |
| 59 | 72 | long writeTime = System.currentTimeMillis() * 1000; |
| 60 | 73 | User newUser = new User("testlogin2", "blah"); |
| @@ -80,6 +93,7 @@ | ||
| 80 | 93 | } |
| 81 | 94 | |
| 82 | 95 | @Test(groups = "short") |
| 96 | + @CassandraVersion(major = 2.0) | |
| 83 | 97 | void should_add_aliases_for_fields_in_select_queries() { |
| 84 | 98 | BoundStatement bs = (BoundStatement)userMapper.getQuery("test"); |
| 85 | 99 | assertThat(bs.preparedStatement().getQueryString()) |
| @@ -87,6 +101,7 @@ | ||
| 87 | 101 | } |
| 88 | 102 | |
| 89 | 103 | @Test(groups = "short") |
| 104 | + @CassandraVersion(major = 2.0) | |
| 90 | 105 | public void should_map_aliased_resultset_to_objects() { |
| 91 | 106 | Statement getQuery = userMapper.getQuery("testlogin"); |
| 92 | 107 | getQuery.setConsistencyLevel(ConsistencyLevel.QUORUM); |
| @@ -99,6 +114,7 @@ | ||
| 99 | 114 | } |
| 100 | 115 | |
| 101 | 116 | @Test(groups = "short") |
| 117 | + @CassandraVersion(major = 2.0) | |
| 102 | 118 | void should_map_unaliased_resultset_to_objects() { |
| 103 | 119 | UserAccessor userAccessor = mappingManager.createAccessor(UserAccessor.class); |
| 104 | 120 | ResultSet rs = userAccessor.all(); |
| @@ -110,11 +126,13 @@ | ||
| 110 | 126 | } |
| 111 | 127 | |
| 112 | 128 | @Test(groups = "short", expectedExceptions = IllegalArgumentException.class) |
| 129 | + @CassandraVersion(major = 2.0) | |
| 113 | 130 | void should_fail_if_computed_field_is_not_right_type() { |
| 114 | 131 | mappingManager.mapper(User_WrongComputedType.class); |
| 115 | 132 | } |
| 116 | 133 | |
| 117 | 134 | @Test(groups = "short") |
| 135 | + @CassandraVersion(major = 2.0) | |
| 118 | 136 | void should_fail_if_computed_field_marked_with_column_annotation() { |
| 119 | 137 | Mapper<User_WrongAnnotationForComputed> mapper = mappingManager.mapper(User_WrongAnnotationForComputed.class); |
| 120 | 138 | try { |
modified driver-mapping/src/test/java/com/datastax/driver/mapping/MapperOptionTest.java +36 −12
| @@ -23,6 +23,7 @@ | ||
| 23 | 23 | import org.testng.annotations.BeforeMethod; |
| 24 | 24 | import org.testng.annotations.Test; |
| 25 | 25 | |
| 26 | +import static com.datastax.driver.core.ConsistencyLevel.*; | |
| 26 | 27 | import static org.assertj.core.api.Assertions.assertThat; |
| 27 | 28 | import static org.assertj.core.api.Assertions.fail; |
| 28 | 29 | |
| @@ -33,7 +34,6 @@ | ||
| 33 | 34 | import com.datastax.driver.mapping.annotations.PartitionKey; |
| 34 | 35 | import com.datastax.driver.mapping.annotations.Table; |
| 35 | 36 | |
| 36 | -import static com.datastax.driver.core.ConsistencyLevel.TWO; | |
| 37 | 37 | import static com.datastax.driver.core.ProtocolVersion.V1; |
| 38 | 38 | import static com.datastax.driver.mapping.Mapper.Option; |
| 39 | 39 | |
| @@ -55,9 +55,9 @@ | ||
| 55 | 55 | |
| 56 | 56 | @Test(groups = "short") |
| 57 | 57 | @CassandraVersion(major = 2.0) |
| 58 | - void should_use_save_options() { | |
| 58 | + void should_use_save_options() throws Exception { | |
| 59 | 59 | Long tsValue = futureTimestamp(); |
| 60 | - mapper.saveAsync(new User(42, "helloworld"), Option.timestamp(tsValue), Option.tracing(true)); | |
| 60 | + mapper.save(new User(42, "helloworld"), Option.timestamp(tsValue), Option.tracing(true)); | |
| 61 | 61 | assertThat(mapper.get(42).getV()).isEqualTo("helloworld"); |
| 62 | 62 | Long tsReturned = session.execute("SELECT writetime(v) FROM user WHERE key=" + 42).one().getLong(0); |
| 63 | 63 | assertThat(tsReturned).isEqualTo(tsValue); |
| @@ -69,21 +69,24 @@ | ||
| 69 | 69 | User todelete = new User(45, "todelete"); |
| 70 | 70 | mapper.save(todelete); |
| 71 | 71 | Option opt = Option.timestamp(35); |
| 72 | - BoundStatement bs = (BoundStatement)mapper.deleteQuery(45, opt, Option.consistencyLevel(ConsistencyLevel.ALL)); | |
| 72 | + BoundStatement bs = (BoundStatement)mapper.deleteQuery(45, opt, Option.consistencyLevel(QUORUM)); | |
| 73 | 73 | assertThat(bs.preparedStatement().getQueryString()).contains("USING TIMESTAMP"); |
| 74 | + assertThat(bs.getConsistencyLevel()).isEqualTo(QUORUM); | |
| 74 | 75 | } |
| 75 | 76 | |
| 76 | 77 | @Test(groups = "short", expectedExceptions = {IllegalArgumentException.class}) |
| 77 | 78 | @CassandraVersion(major = 2.0) |
| 78 | 79 | void should_use_get_options() { |
| 79 | - User todelete = new User(45, "toget"); | |
| 80 | - mapper.save(todelete); | |
| 80 | + User user = new User(45, "toget"); | |
| 81 | + mapper.save(user); | |
| 81 | 82 | |
| 82 | - Option opt = Option.tracing(true); | |
| 83 | - BoundStatement bs = (BoundStatement)mapper.getQuery(45, opt); | |
| 83 | + BoundStatement bs = (BoundStatement)mapper.getQuery(45, Option.tracing(true), Option.consistencyLevel(ALL)); | |
| 84 | 84 | assertThat(bs.isTracing()).isTrue(); |
| 85 | - | |
| 86 | - User us = mapper.mapAliased(session.execute(bs)).one(); | |
| 85 | + assertThat(bs.getConsistencyLevel()).isEqualTo(ALL); | |
| 86 | + | |
| 87 | + ResultSet rs = session.execute(bs); | |
| 88 | + assertThat(rs.getExecutionInfo().getQueryTrace()).isNotNull(); | |
| 89 | + User us = mapper.mapAliased(rs).one(); | |
| 87 | 90 | assertThat(us.getV()).isEqualTo("toget"); |
| 88 | 91 | |
| 89 | 92 | mapper.getQuery(45, Option.timestamp(1337)); |
| @@ -133,6 +136,27 @@ | ||
| 133 | 136 | assertThat(bs.isTracing()).isFalse(); |
| 134 | 137 | } |
| 135 | 138 | |
| 139 | + @Test(groups = "short") | |
| 140 | + @CassandraVersion(major = 2.0) | |
| 141 | + void should_prioritize_option_over_model_consistency() { | |
| 142 | + // Generate Write Query and ensure User model writeConsistency is used. | |
| 143 | + User user = new User(1859, "Steve"); | |
| 144 | + Statement saveDefault = mapper.saveQuery(user); | |
| 145 | + assertThat(saveDefault.getConsistencyLevel()).isEqualTo(ONE); | |
| 146 | + | |
| 147 | + // Generate Write Query and ensure provided Option for consistencyLevel is used. | |
| 148 | + Statement saveProvidedCL = mapper.saveQuery(user, Option.consistencyLevel(QUORUM)); | |
| 149 | + assertThat(saveProvidedCL.getConsistencyLevel()).isEqualTo(QUORUM); | |
| 150 | + | |
| 151 | + // Generate Read Query and ensure User model readConsistency is used. | |
| 152 | + Statement readDefault = mapper.getQuery(1859); | |
| 153 | + assertThat(readDefault.getConsistencyLevel()).isEqualTo(LOCAL_ONE); | |
| 154 | + | |
| 155 | + // Generate Ready Query and ensure provided Option for consistencyLevel is used. | |
| 156 | + Statement readProvidedCL = mapper.getQuery(1859, Option.consistencyLevel(LOCAL_QUORUM)); | |
| 157 | + assertThat(readProvidedCL.getConsistencyLevel()).isEqualTo(LOCAL_QUORUM); | |
| 158 | + } | |
| 159 | + | |
| 136 | 160 | @Test(groups = "short") |
| 137 | 161 | @CassandraVersion(major = 2.0) |
| 138 | 162 | void should_use_explicit_options_over_default_options() { |
| @@ -333,14 +357,14 @@ | ||
| 333 | 357 | private static void assertFirstNodeUnavailable(ExecutionException e) { |
| 334 | 358 | Throwable cause = e.getCause(); |
| 335 | 359 | assertThat(cause).isInstanceOf(NoHostAvailableException.class); |
| 336 | - assertFirstNodeUnavailable((NoHostAvailableException)cause); | |
| 360 | + assertFirstNodeUnavailable((NoHostAvailableException) cause); | |
| 337 | 361 | } |
| 338 | 362 | |
| 339 | 363 | private static long futureTimestamp() { |
| 340 | 364 | return (System.currentTimeMillis() + 1000) * 1000; |
| 341 | 365 | } |
| 342 | 366 | |
| 343 | - @Table(name = "user", writeConsistency = "ONE") | |
| 367 | + @Table(name = "user", readConsistency = "LOCAL_ONE", writeConsistency = "ONE") | |
| 344 | 368 | public static class User { |
| 345 | 369 | @PartitionKey |
| 346 | 370 | private int key; |
added driver-mapping/src/test/java/com/datastax/driver/mapping/MapperSaveNullFieldsTest.java +151 −0
| @@ -0,0 +1,151 @@ | ||
| 1 | +/* | |
| 2 | + * Copyright (C) 2012-2015 DataStax Inc. | |
| 3 | + * | |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | |
| 5 | + * you may not use this file except in compliance with the License. | |
| 6 | + * You may obtain a copy of the License at | |
| 7 | + * | |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 | |
| 9 | + * | |
| 10 | + * Unless required by applicable law or agreed to in writing, software | |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, | |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 13 | + * See the License for the specific language governing permissions and | |
| 14 | + * limitations under the License. | |
| 15 | + */ | |
| 16 | +package com.datastax.driver.mapping; | |
| 17 | + | |
| 18 | +import java.util.Collection; | |
| 19 | + | |
| 20 | +import com.google.common.collect.Lists; | |
| 21 | +import org.testng.annotations.BeforeMethod; | |
| 22 | +import org.testng.annotations.Test; | |
| 23 | + | |
| 24 | +import static org.assertj.core.api.Assertions.assertThat; | |
| 25 | + | |
| 26 | +import com.datastax.driver.core.BoundStatement; | |
| 27 | +import com.datastax.driver.core.CCMBridge; | |
| 28 | +import com.datastax.driver.mapping.Mapper.Option; | |
| 29 | +import com.datastax.driver.mapping.annotations.PartitionKey; | |
| 30 | +import com.datastax.driver.mapping.annotations.Table; | |
| 31 | + | |
| 32 | +public class MapperSaveNullFieldsTest extends CCMBridge.PerClassSingleNodeCluster { | |
| 33 | + | |
| 34 | + Mapper<User> mapper; | |
| 35 | + | |
| 36 | + @Override | |
| 37 | + protected Collection<String> getTableDefinitions() { | |
| 38 | + return Lists.newArrayList("CREATE TABLE user (login text primary key, name text, phone text)"); | |
| 39 | + } | |
| 40 | + | |
| 41 | + @BeforeMethod(groups = "short") | |
| 42 | + public void setup() { | |
| 43 | + mapper = new MappingManager(session).mapper(User.class); | |
| 44 | + } | |
| 45 | + | |
| 46 | + @Test(groups = "short") | |
| 47 | + void should_save_null_fields_if_requested() { | |
| 48 | + should_save_null_fields(true, Option.saveNullFields(true)); | |
| 49 | + | |
| 50 | + mapper.setDefaultSaveOptions(Option.saveNullFields(true)); | |
| 51 | + should_save_null_fields(true); | |
| 52 | + } | |
| 53 | + | |
| 54 | + @Test(groups = "short") | |
| 55 | + void should_save_null_fields_by_default() { | |
| 56 | + should_save_null_fields(true); | |
| 57 | + } | |
| 58 | + | |
| 59 | + @Test(groups = "short") | |
| 60 | + void should_ignore_null_fields_if_requested() { | |
| 61 | + should_save_null_fields(false, Option.saveNullFields(false)); | |
| 62 | + | |
| 63 | + mapper.setDefaultSaveOptions(Option.saveNullFields(false)); | |
| 64 | + should_save_null_fields(false); | |
| 65 | + } | |
| 66 | + | |
| 67 | + private void should_save_null_fields(boolean saveExpected, Option... options) { | |
| 68 | + // Try different combinations of null fields | |
| 69 | + should_save_null_fields(true, true, saveExpected, options); | |
| 70 | + should_save_null_fields(true, false, saveExpected, options); | |
| 71 | + should_save_null_fields(false, true, saveExpected, options); | |
| 72 | + should_save_null_fields(false, false, saveExpected, options); | |
| 73 | + } | |
| 74 | + | |
| 75 | + private void should_save_null_fields(boolean nullName, boolean nullPhone, boolean saveExpected, Option... options) { | |
| 76 | + // Start with clean data | |
| 77 | + session.execute("insert into user(login, name, phone) " | |
| 78 | + + "values ('test_login', 'previous_name', 'previous_phone')"); | |
| 79 | + | |
| 80 | + String newName = nullName ? null : "new_name"; | |
| 81 | + String newPhone = nullPhone ? null : "new_phone"; | |
| 82 | + String description = String.format("update with name=%s, phone = %s", newName, newPhone); | |
| 83 | + User newUser = new User("test_login", newName, newPhone); | |
| 84 | + | |
| 85 | + // Check if null fields are included in generated statement: | |
| 86 | + BoundStatement bs = (BoundStatement)mapper.saveQuery(newUser, options); | |
| 87 | + String queryString = bs.preparedStatement().getQueryString(); | |
| 88 | + if (nullName && !saveExpected) | |
| 89 | + assertThat(queryString).as(description).doesNotContain("\"name\""); | |
| 90 | + else | |
| 91 | + assertThat(queryString).as(description).contains("\"name\""); | |
| 92 | + | |
| 93 | + if (nullPhone && !saveExpected) | |
| 94 | + assertThat(queryString).as(description).doesNotContain("\"phone\""); | |
| 95 | + else | |
| 96 | + assertThat(queryString).as(description).contains("\"phone\""); | |
| 97 | + | |
| 98 | + // Save entity and check the data | |
| 99 | + mapper.save(newUser, options); | |
| 100 | + User savedUser = mapper.get("test_login"); | |
| 101 | + String expectedName = nullName | |
| 102 | + ? (saveExpected ? null : "previous_name") | |
| 103 | + : "new_name"; | |
| 104 | + String expectedPhone = nullPhone | |
| 105 | + ? (saveExpected ? null : "previous_phone") | |
| 106 | + : "new_phone"; | |
| 107 | + assertThat(savedUser.getName()).as(description).isEqualTo(expectedName); | |
| 108 | + assertThat(savedUser.getPhone()).as(description).isEqualTo(expectedPhone); | |
| 109 | + } | |
| 110 | + | |
| 111 | + @Table(name = "user") | |
| 112 | + public static class User { | |
| 113 | + @PartitionKey | |
| 114 | + private String login; | |
| 115 | + private String name; | |
| 116 | + private String phone; | |
| 117 | + | |
| 118 | + public User() { | |
| 119 | + } | |
| 120 | + | |
| 121 | + public User(String login, String name, String phone) { | |
| 122 | + this.login = login; | |
| 123 | + this.name = name; | |
| 124 | + this.phone = phone; | |
| 125 | + } | |
| 126 | + | |
| 127 | + public String getLogin() { | |
| 128 | + return login; | |
| 129 | + } | |
| 130 | + | |
| 131 | + public void setLogin(String login) { | |
| 132 | + this.login = login; | |
| 133 | + } | |
| 134 | + | |
| 135 | + public String getName() { | |
| 136 | + return name; | |
| 137 | + } | |
| 138 | + | |
| 139 | + public void setName(String name) { | |
| 140 | + this.name = name; | |
| 141 | + } | |
| 142 | + | |
| 143 | + public String getPhone() { | |
| 144 | + return phone; | |
| 145 | + } | |
| 146 | + | |
| 147 | + public void setPhone(String phone) { | |
| 148 | + this.phone = phone; | |
| 149 | + } | |
| 150 | + } | |
| 151 | +} |
modified driver-mapping/src/test/java/com/datastax/driver/mapping/MapperTest.java +13 −0
| @@ -418,4 +418,17 @@ | ||
| 418 | 418 | // cleanup |
| 419 | 419 | session.execute("delete from posts where user_id = " + u1.getUserId()); |
| 420 | 420 | } |
| 421 | + | |
| 422 | + @Test(groups="short") | |
| 423 | + public void should_not_initialize_session_when_protocol_version_provided() { | |
| 424 | + Session newSession = cluster.newSession(); | |
| 425 | + | |
| 426 | + // Ensures that a Session is not initialized when a protocol version is provided. | |
| 427 | + MappingManager manager = new MappingManager(newSession, ProtocolVersion.V1); | |
| 428 | + assertThat(newSession.getState().getConnectedHosts()).hasSize(0); | |
| 429 | + | |
| 430 | + // Session should be initialized on first query. | |
| 431 | + newSession.execute("USE " + keyspace); | |
| 432 | + assertThat(newSession.getState().getConnectedHosts()).hasSize(1); | |
| 433 | + } | |
| 421 | 434 | } |