kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | d3ae2055778c573498c7dd1089fd9518ac8d41d916e7c7dd83c9895f33b8fc9c |
|---|---|
| Author | Olivier Michallat <olim7t@users.noreply.github.com> · 2015-06-26 18:30 +02:00 |
| Committer | Olivier Michallat <olim7t@users.noreply.github.com> · 2015-06-26 18:30 +02:00 |
| Parents | e2d5e12472e5, dfc0c00933c1 |
16 files changed
modified changelog/README.md +2 −0
| @@ -17,6 +17,8 @@ | ||
| 17 | 17 | - [bug] Prevent QueryBuilder from trying to serialize raw string (JAVA-688) |
| 18 | 18 | - [improvement] Allow @Enumerated in Accessor method parameters (JAVA-589) |
| 19 | 19 | - [improvement] Allow access to table metadata from Mapper (JAVA-554) |
| 20 | +- [improvement] Provide a way to map computed fields (JAVA-661) | |
| 21 | +- [improvement] Ignore missing columns in mapper (JAVA-824) | |
| 20 | 22 | |
| 21 | 23 | |
| 22 | 24 | ### 2.1.6 |
modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/Select.java +13 −0
| @@ -347,6 +347,14 @@ | ||
| 347 | 347 | */ |
| 348 | 348 | public abstract SelectionOrAlias column(String name); |
| 349 | 349 | |
| 350 | + /** | |
| 351 | + * Selects the provided raw expression. | |
| 352 | + * | |
| 353 | + * @param rawString the raw expression to add. | |
| 354 | + * @return this in-build SELECT statement | |
| 355 | + */ | |
| 356 | + public abstract SelectionOrAlias raw(String rawString); | |
| 357 | + | |
| 350 | 358 | /** |
| 351 | 359 | * Selects the write time of provided column. |
| 352 | 360 | * <p> |
| @@ -470,6 +478,11 @@ | ||
| 470 | 478 | return queueName(name); |
| 471 | 479 | } |
| 472 | 480 | |
| 481 | + @Override | |
| 482 | + public SelectionOrAlias raw(String rawString) { | |
| 483 | + return queueName(QueryBuilder.raw(rawString)); | |
| 484 | + } | |
| 485 | + | |
| 473 | 486 | /** |
| 474 | 487 | * Selects the write time of provided column. |
| 475 | 488 | * <p> |
modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/Utils.java +2 −0
| @@ -307,6 +307,8 @@ | ||
| 307 | 307 | Alias alias = (Alias)name; |
| 308 | 308 | appendName(alias.column, sb); |
| 309 | 309 | sb.append(" AS ").append(alias.alias); |
| 310 | + } else if (name instanceof RawString) { | |
| 311 | + sb.append(((RawString)name).str); | |
| 310 | 312 | } else { |
| 311 | 313 | throw new IllegalArgumentException(String.format("Invalid column %s of type unknown of the query builder", name)); |
| 312 | 314 | } |
modified driver-mapping/src/main/java/com/datastax/driver/mapping/AnnotationChecks.java +8 −0
| @@ -75,6 +75,7 @@ | ||
| 75 | 75 | field.getDeclaringClass().getName(), e.getMessage())); |
| 76 | 76 | } |
| 77 | 77 | } |
| 78 | + checkValidComputed(field); | |
| 78 | 79 | } |
| 79 | 80 | |
| 80 | 81 | // Returns the offending annotation if there is one |
| @@ -100,6 +101,13 @@ | ||
| 100 | 101 | checkFrozenTypes(javaType, declaredFrozenType); |
| 101 | 102 | } |
| 102 | 103 | |
| 104 | + static void checkValidComputed(Field field) { | |
| 105 | + Computed computed = field.getAnnotation(Computed.class); | |
| 106 | + if (computed != null && computed.formula().isEmpty()){ | |
| 107 | + throw new IllegalArgumentException(String.format("Field %s: attribute 'formula' of annotation @Computed is mandatory for computed fields", field.getName())); | |
| 108 | + } | |
| 109 | + } | |
| 110 | + | |
| 103 | 111 | // Builds a DeclaredFrozenType hierarchy based on the @Frozen* annotations on a field. |
| 104 | 112 | private static DeclaredFrozenType getDeclaredFrozenType(Field field) { |
| 105 | 113 | Frozen frozen = field.getAnnotation(Frozen.class); |
modified driver-mapping/src/main/java/com/datastax/driver/mapping/AnnotationParser.java +34 −10
| @@ -22,6 +22,7 @@ | ||
| 22 | 22 | import java.util.Collections; |
| 23 | 23 | import java.util.Comparator; |
| 24 | 24 | import java.util.List; |
| 25 | +import java.util.concurrent.atomic.AtomicInteger; | |
| 25 | 26 | |
| 26 | 27 | import com.google.common.base.Strings; |
| 27 | 28 | |
| @@ -76,7 +77,7 @@ | ||
| 76 | 77 | |
| 77 | 78 | AnnotationChecks.validateAnnotations(field, "entity", |
| 78 | 79 | Column.class, ClusteringColumn.class, Enumerated.class, Frozen.class, FrozenKey.class, |
| 79 | - FrozenValue.class, PartitionKey.class, Transient.class); | |
| 80 | + FrozenValue.class, PartitionKey.class, Transient.class, Computed.class); | |
| 80 | 81 | |
| 81 | 82 | if (field.getAnnotation(Transient.class) != null) |
| 82 | 83 | continue; |
| @@ -94,15 +95,17 @@ | ||
| 94 | 95 | } |
| 95 | 96 | } |
| 96 | 97 | |
| 98 | + AtomicInteger columnCounter = new AtomicInteger(0); | |
| 99 | + | |
| 97 | 100 | Collections.sort(pks, fieldComparator); |
| 98 | 101 | Collections.sort(ccs, fieldComparator); |
| 99 | 102 | |
| 100 | 103 | validateOrder(pks, "@PartitionKey"); |
| 101 | 104 | validateOrder(ccs, "@ClusteringColumn"); |
| 102 | 105 | |
| 103 | - mapper.addColumns(convert(pks, factory, mapper.entityClass, mappingManager), | |
| 104 | - convert(ccs, factory, mapper.entityClass, mappingManager), | |
| 105 | - convert(rgs, factory, mapper.entityClass, mappingManager)); | |
| 106 | + mapper.addColumns(convert(pks, factory, mapper.entityClass, mappingManager, columnCounter), | |
| 107 | + convert(ccs, factory, mapper.entityClass, mappingManager, columnCounter), | |
| 108 | + convert(rgs, factory, mapper.entityClass, mappingManager, columnCounter)); | |
| 106 | 109 | return mapper; |
| 107 | 110 | } |
| 108 | 111 | |
| @@ -148,16 +151,16 @@ | ||
| 148 | 151 | } |
| 149 | 152 | } |
| 150 | 153 | |
| 151 | - mapper.addColumns(convert(columns, factory, udtClass, mappingManager)); | |
| 154 | + mapper.addColumns(convert(columns, factory, udtClass, mappingManager, null)); | |
| 152 | 155 | return mapper; |
| 153 | 156 | } |
| 154 | 157 | |
| 155 | - private static <T> List<ColumnMapper<T>> convert(List<Field> fields, EntityMapper.Factory factory, Class<T> klass, MappingManager mappingManager) { | |
| 158 | + private static <T> List<ColumnMapper<T>> convert(List<Field> fields, EntityMapper.Factory factory, Class<T> klass, MappingManager mappingManager, AtomicInteger columnCounter) { | |
| 156 | 159 | List<ColumnMapper<T>> mappers = new ArrayList<ColumnMapper<T>>(fields.size()); |
| 157 | 160 | for (int i = 0; i < fields.size(); i++) { |
| 158 | 161 | Field field = fields.get(i); |
| 159 | 162 | int pos = position(field); |
| 160 | - mappers.add(factory.createColumnMapper(klass, field, pos < 0 ? i : pos, mappingManager)); | |
| 163 | + mappers.add(factory.createColumnMapper(klass, field, pos < 0 ? i : pos, mappingManager, columnCounter)); | |
| 161 | 164 | } |
| 162 | 165 | return mappers; |
| 163 | 166 | } |
| @@ -186,12 +189,20 @@ | ||
| 186 | 189 | public static ColumnMapper.Kind kind(Field field) { |
| 187 | 190 | PartitionKey pk = field.getAnnotation(PartitionKey.class); |
| 188 | 191 | ClusteringColumn cc = field.getAnnotation(ClusteringColumn.class); |
| 192 | + Computed comp = field.getAnnotation(Computed.class); | |
| 189 | 193 | if (pk != null && cc != null) |
| 190 | 194 | throw new IllegalArgumentException("Field " + field.getName() + " cannot have both the @PartitionKey and @ClusteringColumn annotations"); |
| 191 | 195 | |
| 192 | - return pk != null | |
| 193 | - ? ColumnMapper.Kind.PARTITION_KEY | |
| 194 | - : (cc != null ? ColumnMapper.Kind.CLUSTERING_COLUMN : ColumnMapper.Kind.REGULAR); | |
| 196 | + if (pk != null) { | |
| 197 | + return ColumnMapper.Kind.PARTITION_KEY; | |
| 198 | + } | |
| 199 | + if (cc != null) { | |
| 200 | + return ColumnMapper.Kind.CLUSTERING_COLUMN; | |
| 201 | + } | |
| 202 | + if (comp != null) { | |
| 203 | + return ColumnMapper.Kind.COMPUTED; | |
| 204 | + } | |
| 205 | + return ColumnMapper.Kind.REGULAR; | |
| 195 | 206 | } |
| 196 | 207 | |
| 197 | 208 | public static EnumType enumType(Field field) { |
| @@ -205,7 +216,11 @@ | ||
| 205 | 216 | |
| 206 | 217 | public static String columnName(Field field) { |
| 207 | 218 | Column column = field.getAnnotation(Column.class); |
| 219 | + Computed computedField = field.getAnnotation(Computed.class); | |
| 208 | 220 | if (column != null && !column.name().isEmpty()) { |
| 221 | + if (computedField != null){ | |
| 222 | + throw new IllegalArgumentException("Cannot use @Column and @Computed on the same field"); | |
| 223 | + } | |
| 209 | 224 | return column.caseSensitive() ? column.name() : column.name().toLowerCase(); |
| 210 | 225 | } |
| 211 | 226 | |
| @@ -214,9 +229,18 @@ | ||
| 214 | 229 | return udtField.caseSensitive() ? udtField.name() : udtField.name().toLowerCase(); |
| 215 | 230 | } |
| 216 | 231 | |
| 232 | + if (computedField != null) { | |
| 233 | + return computedField.formula(); | |
| 234 | + } | |
| 235 | + | |
| 217 | 236 | return field.getName().toLowerCase(); |
| 218 | 237 | } |
| 219 | 238 | |
| 239 | + public static String newAlias(Field field, int columnNumber) { | |
| 240 | + return "col" + columnNumber; | |
| 241 | + | |
| 242 | + } | |
| 243 | + | |
| 220 | 244 | public static <T> AccessorMapper<T> parseAccessor(Class<T> accClass, AccessorMapper.Factory factory, MappingManager mappingManager) { |
| 221 | 245 | if (!accClass.isInterface()) |
| 222 | 246 | throw new IllegalArgumentException("@Accessor annotation is only allowed on interfaces"); |
modified driver-mapping/src/main/java/com/datastax/driver/mapping/ColumnMapper.java +20 −11
| @@ -16,15 +16,18 @@ | ||
| 16 | 16 | package com.datastax.driver.mapping; |
| 17 | 17 | |
| 18 | 18 | import java.lang.reflect.Field; |
| 19 | +import java.util.concurrent.atomic.AtomicInteger; | |
| 19 | 20 | |
| 20 | 21 | import com.datastax.driver.core.DataType; |
| 22 | + | |
| 21 | 23 | import static com.datastax.driver.core.querybuilder.QueryBuilder.quote; |
| 22 | 24 | |
| 23 | 25 | abstract class ColumnMapper<T> { |
| 24 | 26 | |
| 25 | - public enum Kind { PARTITION_KEY, CLUSTERING_COLUMN, REGULAR }; | |
| 27 | + public enum Kind {PARTITION_KEY, CLUSTERING_COLUMN, REGULAR, COMPUTED}; | |
| 26 | 28 | |
| 27 | 29 | private final String columnName; |
| 30 | + private final String alias; | |
| 28 | 31 | protected final String fieldName; |
| 29 | 32 | protected final Class<?> javaType; |
| 30 | 33 | // Note: dataType is not guaranteed to be exact. Typically, it will be uuid even if the underlying |
| @@ -34,24 +37,30 @@ | ||
| 34 | 37 | protected final Kind kind; |
| 35 | 38 | protected final int position; |
| 36 | 39 | |
| 37 | - protected ColumnMapper(Field field, DataType dataType, int position) { | |
| 38 | - this(AnnotationParser.columnName(field), field.getName(), field.getType(), dataType, AnnotationParser.kind(field), position); | |
| 39 | - } | |
| 40 | - | |
| 41 | - private ColumnMapper(String columnName, String fieldName, Class<?> javaType, DataType dataType, Kind kind, int position) { | |
| 42 | - this.columnName = columnName; | |
| 43 | - this.fieldName = fieldName; | |
| 44 | - this.javaType = javaType; | |
| 40 | + protected ColumnMapper(Field field, DataType dataType, int position, AtomicInteger columnCounter) { | |
| 41 | + this.columnName = AnnotationParser.columnName(field); | |
| 42 | + this.alias = (columnCounter != null) | |
| 43 | + ? AnnotationParser.newAlias(field, columnCounter.incrementAndGet()) | |
| 44 | + : null; | |
| 45 | + this.fieldName = field.getName(); | |
| 46 | + this.javaType = field.getType(); | |
| 45 | 47 | this.dataType = dataType; |
| 46 | - this.kind = kind; | |
| 48 | + this.kind = AnnotationParser.kind(field); | |
| 47 | 49 | this.position = position; |
| 48 | 50 | } |
| 49 | 51 | |
| 50 | 52 | public abstract Object getValue(T entity); |
| 53 | + | |
| 51 | 54 | public abstract void setValue(T entity, Object value); |
| 52 | 55 | |
| 53 | 56 | public String getColumnName() { |
| 54 | - return quote(columnName); | |
| 57 | + return kind == Kind.COMPUTED | |
| 58 | + ? columnName | |
| 59 | + : quote(columnName); | |
| 60 | + } | |
| 61 | + | |
| 62 | + public String getAlias() { | |
| 63 | + return alias; | |
| 55 | 64 | } |
| 56 | 65 | |
| 57 | 66 | public DataType getDataType() { |
modified driver-mapping/src/main/java/com/datastax/driver/mapping/EntityMapper.java +2 −1
| @@ -17,6 +17,7 @@ | ||
| 17 | 17 | |
| 18 | 18 | import java.lang.reflect.Field; |
| 19 | 19 | import java.util.*; |
| 20 | +import java.util.concurrent.atomic.AtomicInteger; | |
| 20 | 21 | |
| 21 | 22 | import com.datastax.driver.core.ConsistencyLevel; |
| 22 | 23 | import static com.datastax.driver.core.querybuilder.QueryBuilder.quote; |
| @@ -83,6 +84,6 @@ | ||
| 83 | 84 | |
| 84 | 85 | interface Factory { |
| 85 | 86 | public <T> EntityMapper<T> create(Class<T> entityClass, String keyspace, String table, ConsistencyLevel writeConsistency, ConsistencyLevel readConsistency); |
| 86 | - public <T> ColumnMapper<T> createColumnMapper(Class<T> componentClass, Field field, int position, MappingManager mappingManager); | |
| 87 | + public <T> ColumnMapper<T> createColumnMapper(Class<T> componentClass, Field field, int position, MappingManager mappingManager, AtomicInteger columnCounter); | |
| 87 | 88 | } |
| 88 | 89 | } |
modified driver-mapping/src/main/java/com/datastax/driver/mapping/Mapper.java +41 −7
| @@ -23,11 +23,12 @@ | ||
| 23 | 23 | import com.google.common.base.Functions; |
| 24 | 24 | import com.google.common.util.concurrent.Futures; |
| 25 | 25 | import com.google.common.util.concurrent.ListenableFuture; |
| 26 | - | |
| 27 | 26 | import org.slf4j.Logger; |
| 28 | 27 | import org.slf4j.LoggerFactory; |
| 29 | 28 | |
| 30 | 29 | import com.datastax.driver.core.*; |
| 30 | +import com.datastax.driver.mapping.annotations.Computed; | |
| 31 | +import com.datastax.driver.mapping.annotations.Accessor; | |
| 31 | 32 | |
| 32 | 33 | /** |
| 33 | 34 | * An object handling the mapping of a particular class. |
| @@ -51,7 +52,8 @@ | ||
| 51 | 52 | private static final Function<Object, Void> NOOP = Functions.<Void>constant(null); |
| 52 | 53 | |
| 53 | 54 | final Function<ResultSet, T> mapOneFunction; |
| 54 | - final Function<ResultSet, Result<T>> mapAllFunction; | |
| 55 | + final Function<ResultSet, T> mapOneFunctionWithoutAliases; | |
| 56 | + final Function<ResultSet, Result<T>> mapAllFunctionWithoutAliases; | |
| 55 | 57 | |
| 56 | 58 | Mapper(MappingManager manager, Class<T> klass, EntityMapper<T> mapper) { |
| 57 | 59 | this.manager = manager; |
| @@ -63,11 +65,16 @@ | ||
| 63 | 65 | |
| 64 | 66 | this.protocolVersion = manager.getSession().getCluster().getConfiguration().getProtocolOptions().getProtocolVersionEnum(); |
| 65 | 67 | this.mapOneFunction = new Function<ResultSet, T>() { |
| 68 | + public T apply(ResultSet rs) { | |
| 69 | + return Mapper.this.mapAliased(rs).one(); | |
| 70 | + } | |
| 71 | + }; | |
| 72 | + this.mapOneFunctionWithoutAliases = new Function<ResultSet, T>() { | |
| 66 | 73 | public T apply(ResultSet rs) { |
| 67 | 74 | return Mapper.this.map(rs).one(); |
| 68 | 75 | } |
| 69 | 76 | }; |
| 70 | - this.mapAllFunction = new Function<ResultSet, Result<T>>() { | |
| 77 | + this.mapAllFunctionWithoutAliases = new Function<ResultSet, Result<T>>() { | |
| 71 | 78 | public Result<T> apply(ResultSet rs) { |
| 72 | 79 | return Mapper.this.map(rs); |
| 73 | 80 | } |
| @@ -132,8 +139,10 @@ | ||
| 132 | 139 | BoundStatement bs = ps.bind(); |
| 133 | 140 | int i = 0; |
| 134 | 141 | for (ColumnMapper<T> cm : mapper.allColumns()) { |
| 135 | - Object value = cm.getValue(entity); | |
| 136 | - bs.setBytesUnsafe(i++, value == null ? null : cm.getDataType().serialize(value, protocolVersion)); | |
| 142 | + if (cm.kind != ColumnMapper.Kind.COMPUTED) { | |
| 143 | + Object value = cm.getValue(entity); | |
| 144 | + bs.setBytesUnsafe(i++, value == null ? null : cm.getDataType().serialize(value, protocolVersion)); | |
| 145 | + } | |
| 137 | 146 | } |
| 138 | 147 | |
| 139 | 148 | if (mapper.writeConsistency != null) |
| @@ -284,18 +293,43 @@ | ||
| 284 | 293 | } |
| 285 | 294 | |
| 286 | 295 | /** |
| 287 | - * Map the rows from a {@code ResultSet} into the class this is mapper of. | |
| 296 | + * Maps the rows from a {@code ResultSet} into the class this is a mapper of. | |
| 297 | + * <p> | |
| 298 | + * Use this method to map a result set that was not generated by the mapper (e.g. a result set coming from a | |
| 299 | + * manual query or an {@link Accessor method}. | |
| 300 | + * It expects that the result set contains all column mapped in the target class, | |
| 301 | + * and that they are not aliased. {@link Computed} fields will not be filled in mapped objects. | |
| 288 | 302 | * |
| 289 | 303 | * @param resultSet the {@code ResultSet} to map. |
| 290 | 304 | * @return the mapped result set. Note that the returned mapped result set |
| 291 | 305 | * will encapsulate {@code resultSet} and so consuming results from this |
| 292 | 306 | * returned mapped result set will consume results from {@code resultSet} |
| 293 | 307 | * and vice-versa. |
| 308 | + * | |
| 309 | + * @see #mapAliased(ResultSet) | |
| 294 | 310 | */ |
| 295 | 311 | public Result<T> map(ResultSet resultSet) { |
| 296 | 312 | return new Result<T>(resultSet, mapper, protocolVersion); |
| 297 | 313 | } |
| 298 | 314 | |
| 315 | + /** | |
| 316 | + * Maps the rows from a {@code ResultSet} into the class this is a mapper of. | |
| 317 | + * <p> | |
| 318 | + * Use this method to map a result set coming from the execution of the {@code Statement} returned by | |
| 319 | + * {@link #getQuery(Object...)}. {@link Computed} fields will be filled in the mapped object. | |
| 320 | + * | |
| 321 | + * @param resultSet the {@code ResultSet} to map. | |
| 322 | + * @return the mapped result set. Note that the returned mapped result set | |
| 323 | + * will encapsulate {@code resultSet} and so consuming results from this | |
| 324 | + * returned mapped result set will consume results from {@code resultSet} | |
| 325 | + * and vice-versa. | |
| 326 | + * | |
| 327 | + * @see #map(ResultSet) | |
| 328 | + */ | |
| 329 | + public Result<T> mapAliased(ResultSet resultSet) { | |
| 330 | + return new Result<T>(resultSet, mapper, protocolVersion, true); | |
| 331 | + } | |
| 332 | + | |
| 299 | 333 | /** |
| 300 | 334 | * Creates a query to fetch entity given its PRIMARY KEY. |
| 301 | 335 | * <p> |
| @@ -348,7 +382,7 @@ | ||
| 348 | 382 | * at least one of those values is {@code null}. |
| 349 | 383 | */ |
| 350 | 384 | public T get(Object... primaryKey) { |
| 351 | - return map(session().execute(getQuery(primaryKey))).one(); | |
| 385 | + return mapAliased(session().execute(getQuery(primaryKey))).one(); | |
| 352 | 386 | } |
| 353 | 387 | |
| 354 | 388 | /** |
modified driver-mapping/src/main/java/com/datastax/driver/mapping/MethodMapper.java +2 −2
| @@ -142,8 +142,8 @@ | ||
| 142 | 142 | return future; |
| 143 | 143 | |
| 144 | 144 | return mapOne |
| 145 | - ? Futures.transform(future, returnMapper.mapOneFunction) | |
| 146 | - : Futures.transform(future, returnMapper.mapAllFunction); | |
| 145 | + ? Futures.transform(future, returnMapper.mapOneFunctionWithoutAliases) | |
| 146 | + : Futures.transform(future, returnMapper.mapAllFunctionWithoutAliases); | |
| 147 | 147 | } else { |
| 148 | 148 | ResultSet rs = session.execute(bs); |
| 149 | 149 | if (returnMapper == null) |
modified driver-mapping/src/main/java/com/datastax/driver/mapping/QueryType.java +23 −5
| @@ -15,7 +15,8 @@ | ||
| 15 | 15 | */ |
| 16 | 16 | package com.datastax.driver.mapping; |
| 17 | 17 | |
| 18 | -import java.util.*; | |
| 18 | +import java.util.ArrayList; | |
| 19 | +import java.util.List; | |
| 19 | 20 | |
| 20 | 21 | import com.google.common.base.Objects; |
| 21 | 22 | |
| @@ -23,6 +24,7 @@ | ||
| 23 | 24 | import com.datastax.driver.core.querybuilder.Delete; |
| 24 | 25 | import com.datastax.driver.core.querybuilder.Insert; |
| 25 | 26 | import com.datastax.driver.core.querybuilder.Select; |
| 27 | + | |
| 26 | 28 | import static com.datastax.driver.core.querybuilder.QueryBuilder.*; |
| 27 | 29 | |
| 28 | 30 | class QueryType { |
| @@ -64,14 +66,30 @@ | ||
| 64 | 66 | ? insertInto(mapper.getKeyspace(), mapper.getTable()) |
| 65 | 67 | : insertInto(table); |
| 66 | 68 | for (ColumnMapper<?> cm : mapper.allColumns()) |
| 67 | - insert.value(cm.getColumnName(), bindMarker()); | |
| 69 | + if (cm.kind != ColumnMapper.Kind.COMPUTED) | |
| 70 | + insert.value(cm.getColumnName(), bindMarker()); | |
| 68 | 71 | return insert.toString(); |
| 69 | 72 | } |
| 70 | 73 | case GET: |
| 71 | 74 | { |
| 72 | - Select select = table == null | |
| 73 | - ? select().all().from(mapper.getKeyspace(), mapper.getTable()) | |
| 74 | - : select().all().from(table); | |
| 75 | + Select.Selection selection = select(); | |
| 76 | + for (ColumnMapper cm : mapper.allColumns()) { | |
| 77 | + Select.SelectionOrAlias column = (cm.kind == ColumnMapper.Kind.COMPUTED) | |
| 78 | + ? selection.raw(cm.getColumnName()) | |
| 79 | + : selection.column(cm.getColumnName()); | |
| 80 | + | |
| 81 | + if (cm.getAlias() == null) { | |
| 82 | + selection = column; | |
| 83 | + } else { | |
| 84 | + selection = column.as(cm.getAlias()); | |
| 85 | + } | |
| 86 | + } | |
| 87 | + Select select; | |
| 88 | + if (table == null) { | |
| 89 | + select = selection.from(mapper.getKeyspace(), mapper.getTable()); | |
| 90 | + } else { | |
| 91 | + select = selection.from(table); | |
| 92 | + } | |
| 75 | 93 | Select.Where where = select.where(); |
| 76 | 94 | for (int i = 0; i < mapper.primaryKeySize(); i++) |
| 77 | 95 | where.and(eq(mapper.getPrimaryKeyColumn(i).getColumnName(), bindMarker())); |
modified driver-mapping/src/main/java/com/datastax/driver/mapping/ReflectionMapper.java +18 −16
| @@ -22,8 +22,10 @@ | ||
| 22 | 22 | import java.lang.reflect.Type; |
| 23 | 23 | import java.lang.reflect.Method; |
| 24 | 24 | import java.util.*; |
| 25 | +import java.util.concurrent.atomic.AtomicInteger; | |
| 25 | 26 | |
| 26 | 27 | import com.datastax.driver.core.*; |
| 28 | +import com.datastax.driver.mapping.annotations.Computed; | |
| 27 | 29 | |
| 28 | 30 | /** |
| 29 | 31 | * An {@link EntityMapper} implementation that use reflection to read and write fields |
| @@ -55,12 +57,12 @@ | ||
| 55 | 57 | private final Method readMethod; |
| 56 | 58 | private final Method writeMethod; |
| 57 | 59 | |
| 58 | - private LiteralMapper(Field field, int position, PropertyDescriptor pd) { | |
| 59 | - this(field, extractSimpleType(field), position, pd); | |
| 60 | + private LiteralMapper(Field field, int position, PropertyDescriptor pd, AtomicInteger columnNumber) { | |
| 61 | + this(field, extractSimpleType(field), position, pd, columnNumber); | |
| 60 | 62 | } |
| 61 | 63 | |
| 62 | - private LiteralMapper(Field field, DataType type, int position, PropertyDescriptor pd) { | |
| 63 | - super(field, type, position); | |
| 64 | + private LiteralMapper(Field field, DataType type, int position, PropertyDescriptor pd, AtomicInteger columnCounter) { | |
| 65 | + super(field, type, position, columnCounter); | |
| 64 | 66 | this.readMethod = pd.getReadMethod(); |
| 65 | 67 | this.writeMethod = pd.getWriteMethod(); |
| 66 | 68 | } |
| @@ -93,8 +95,8 @@ | ||
| 93 | 95 | private final EnumType enumType; |
| 94 | 96 | private final Map<String, Object> fromString; |
| 95 | 97 | |
| 96 | - private EnumMapper(Field field, int position, PropertyDescriptor pd, EnumType enumType) { | |
| 97 | - super(field, enumType == EnumType.STRING ? DataType.text() : DataType.cint(), position, pd); | |
| 98 | + private EnumMapper(Field field, int position, PropertyDescriptor pd, EnumType enumType, AtomicInteger columnCounter) { | |
| 99 | + super(field, enumType == EnumType.STRING ? DataType.text() : DataType.cint(), position, pd, columnCounter); | |
| 98 | 100 | this.enumType = enumType; |
| 99 | 101 | |
| 100 | 102 | if (enumType == EnumType.STRING) { |
| @@ -138,8 +140,8 @@ | ||
| 138 | 140 | private static class UDTColumnMapper<T, U> extends LiteralMapper<T> { |
| 139 | 141 | private final UDTMapper<U> udtMapper; |
| 140 | 142 | |
| 141 | - private UDTColumnMapper(Field field, int position, PropertyDescriptor pd, UDTMapper<U> udtMapper) { | |
| 142 | - super(field, udtMapper.getUserType(), position, pd); | |
| 143 | + private UDTColumnMapper(Field field, int position, PropertyDescriptor pd, UDTMapper<U> udtMapper, AtomicInteger columnCounter) { | |
| 144 | + super(field, udtMapper.getUserType(), position, pd, columnCounter); | |
| 143 | 145 | this.udtMapper = udtMapper; |
| 144 | 146 | } |
| 145 | 147 | |
| @@ -163,8 +165,8 @@ | ||
| 163 | 165 | private static class NestedUDTMapper<T> extends LiteralMapper<T> { |
| 164 | 166 | private final InferredCQLType inferredCQLType; |
| 165 | 167 | |
| 166 | - public NestedUDTMapper(Field field, int position, PropertyDescriptor pd, InferredCQLType inferredCQLType) { | |
| 167 | - super(field, inferredCQLType.dataType, position, pd); | |
| 168 | + public NestedUDTMapper(Field field, int position, PropertyDescriptor pd, InferredCQLType inferredCQLType, AtomicInteger columnCounter) { | |
| 169 | + super(field, inferredCQLType.dataType, position, pd, columnCounter); | |
| 168 | 170 | this.inferredCQLType = inferredCQLType; |
| 169 | 171 | } |
| 170 | 172 | |
| @@ -199,32 +201,32 @@ | ||
| 199 | 201 | } |
| 200 | 202 | |
| 201 | 203 | @SuppressWarnings({ "unchecked", "rawtypes" }) |
| 202 | - public <T> ColumnMapper<T> createColumnMapper(Class<T> entityClass, Field field, int position, MappingManager mappingManager) { | |
| 204 | + public <T> ColumnMapper<T> createColumnMapper(Class<T> entityClass, Field field, int position, MappingManager mappingManager, AtomicInteger columnCounter) { | |
| 203 | 205 | String fieldName = field.getName(); |
| 204 | 206 | try { |
| 205 | 207 | PropertyDescriptor pd = new PropertyDescriptor(fieldName, field.getDeclaringClass()); |
| 206 | 208 | |
| 207 | 209 | if (field.getType().isEnum()) { |
| 208 | - return new EnumMapper<T>(field, position, pd, AnnotationParser.enumType(field)); | |
| 210 | + return new EnumMapper<T>(field, position, pd, AnnotationParser.enumType(field), columnCounter); | |
| 209 | 211 | } |
| 210 | 212 | |
| 211 | 213 | if (TypeMappings.isMappedUDT(field.getType())) { |
| 212 | 214 | UDTMapper<?> udtMapper = mappingManager.getUDTMapper(field.getType()); |
| 213 | - return (ColumnMapper<T>) new UDTColumnMapper(field, position, pd, udtMapper); | |
| 215 | + return (ColumnMapper<T>) new UDTColumnMapper(field, position, pd, udtMapper, columnCounter); | |
| 214 | 216 | } |
| 215 | 217 | |
| 216 | 218 | if (field.getGenericType() instanceof ParameterizedType) { |
| 217 | 219 | InferredCQLType inferredCQLType = InferredCQLType.from(field, mappingManager); |
| 218 | 220 | if (inferredCQLType.containsMappedUDT) { |
| 219 | 221 | // We need a specialized mapper to convert UDT instances in the hierarchy. |
| 220 | - return (ColumnMapper<T>)new NestedUDTMapper(field, position, pd, inferredCQLType); | |
| 222 | + return (ColumnMapper<T>)new NestedUDTMapper(field, position, pd, inferredCQLType, columnCounter); | |
| 221 | 223 | } else { |
| 222 | 224 | // The default codecs will know how to handle the extracted datatype. |
| 223 | - return new LiteralMapper<T>(field, inferredCQLType.dataType, position, pd); | |
| 225 | + return new LiteralMapper<T>(field, inferredCQLType.dataType, position, pd, columnCounter); | |
| 224 | 226 | } |
| 225 | 227 | } |
| 226 | 228 | |
| 227 | - return new LiteralMapper<T>(field, position, pd); | |
| 229 | + return new LiteralMapper<T>(field, position, pd, columnCounter); | |
| 228 | 230 | |
| 229 | 231 | } catch (IntrospectionException e) { |
| 230 | 232 | throw new IllegalArgumentException("Cannot find matching getter and setter for field '" + fieldName + "'"); |
modified driver-mapping/src/main/java/com/datastax/driver/mapping/Result.java +10 −1
| @@ -28,17 +28,26 @@ | ||
| 28 | 28 | private final ResultSet rs; |
| 29 | 29 | private final EntityMapper<T> mapper; |
| 30 | 30 | private final ProtocolVersion protocolVersion; |
| 31 | + private final boolean useAlias; | |
| 31 | 32 | |
| 32 | 33 | Result(ResultSet rs, EntityMapper<T> mapper, ProtocolVersion protocolVersion) { |
| 34 | + this(rs, mapper, protocolVersion, false); | |
| 35 | + } | |
| 36 | + | |
| 37 | + Result(ResultSet rs, EntityMapper<T> mapper, ProtocolVersion protocolVersion, boolean useAlias) { | |
| 33 | 38 | this.rs = rs; |
| 34 | 39 | this.mapper = mapper; |
| 35 | 40 | this.protocolVersion = protocolVersion; |
| 41 | + this.useAlias = useAlias; | |
| 36 | 42 | } |
| 37 | 43 | |
| 38 | 44 | private T map(Row row) { |
| 39 | 45 | T entity = mapper.newEntity(); |
| 40 | 46 | for (ColumnMapper<T> cm : mapper.allColumns()) { |
| 41 | - ByteBuffer bytes = row.getBytesUnsafe(cm.getColumnName()); | |
| 47 | + String name = cm.getAlias() != null && this.useAlias ? cm.getAlias() : cm.getColumnName(); | |
| 48 | + if (!row.getColumnDefinitions().contains(name)) | |
| 49 | + continue; | |
| 50 | + ByteBuffer bytes = row.getBytesUnsafe(name); | |
| 42 | 51 | if (bytes != null) |
| 43 | 52 | cm.setValue(entity, cm.getDataType().deserialize(bytes, protocolVersion)); |
| 44 | 53 | } |
added driver-mapping/src/main/java/com/datastax/driver/mapping/annotations/Computed.java +50 −0
| @@ -0,0 +1,50 @@ | ||
| 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.annotations; | |
| 17 | + | |
| 18 | + | |
| 19 | +import java.lang.annotation.ElementType; | |
| 20 | +import com.datastax.driver.mapping.Mapper; | |
| 21 | +import java.lang.annotation.Retention; | |
| 22 | +import java.lang.annotation.RetentionPolicy; | |
| 23 | +import java.lang.annotation.Target; | |
| 24 | + | |
| 25 | +/** | |
| 26 | + * Annotates a "computed" field, which gets filled from a CQL expression in the SELECT query | |
| 27 | + * used to retrieve an entity. | |
| 28 | + * <p> | |
| 29 | + * Note that such fields are only filled by default mapper operations ({@link Mapper#get(Object...)}), | |
| 30 | + * they will be ignored in accessor queries and save operations. | |
| 31 | + * <p> | |
| 32 | + * As opposed to other mapper annotation, this one doesn't handle case sensitivity. If your expression | |
| 33 | + * contains case-sensitive names, you'll need to quote them explicitly: | |
| 34 | + * <pre> | |
| 35 | + * {@code @Computed("\"caseSensitiveFunction\"(v)")} | |
| 36 | + * </pre> | |
| 37 | + */ | |
| 38 | +@Target(ElementType.FIELD) | |
| 39 | +@Retention(RetentionPolicy.RUNTIME) | |
| 40 | +public @interface Computed { | |
| 41 | + /** | |
| 42 | + * The formula used to compute the field. | |
| 43 | + * <p> | |
| 44 | + * This is a CQL expression like you would use directly in a query, for instance | |
| 45 | + * "writetime(v)". | |
| 46 | + * | |
| 47 | + * @return the formula. | |
| 48 | + */ | |
| 49 | + String formula(); | |
| 50 | +} |
added driver-mapping/src/test/java/com/datastax/driver/mapping/MapperComputedFieldsTest.java +318 −0
| @@ -0,0 +1,318 @@ | ||
| 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.fail; | |
| 25 | + | |
| 26 | +import com.datastax.driver.core.*; | |
| 27 | +import com.datastax.driver.core.exceptions.InvalidQueryException; | |
| 28 | +import com.datastax.driver.mapping.annotations.*; | |
| 29 | + | |
| 30 | +import static com.datastax.driver.core.Assertions.assertThat; | |
| 31 | + | |
| 32 | +/** | |
| 33 | + * Tests to ensure validity of {@link com.datastax.driver.mapping.annotations.Computed} | |
| 34 | + * annotation to map computed fields. | |
| 35 | + */ | |
| 36 | +@SuppressWarnings("unused") | |
| 37 | +public class MapperComputedFieldsTest extends CCMBridge.PerClassSingleNodeCluster { | |
| 38 | + | |
| 39 | + @Override | |
| 40 | + protected Collection<String> getTableDefinitions() { | |
| 41 | + return Lists.newArrayList( | |
| 42 | + "CREATE TABLE user (login text primary key, name text)", | |
| 43 | + "INSERT INTO user (login, name) VALUES ('testlogin', 'test name')"); | |
| 44 | + } | |
| 45 | + | |
| 46 | + MappingManager mappingManager; | |
| 47 | + Mapper<User> userMapper; | |
| 48 | + | |
| 49 | + @BeforeMethod(groups = "short") | |
| 50 | + void setup() { | |
| 51 | + mappingManager = new MappingManager(session); | |
| 52 | + userMapper = mappingManager.mapper(User.class); | |
| 53 | + } | |
| 54 | + | |
| 55 | + @Test(groups = "short") | |
| 56 | + void should_save_and_get_entity_with_computed_fields() { | |
| 57 | + long writeTime = System.currentTimeMillis() * 1000; | |
| 58 | + User newUser = new User("testlogin2", "blah"); | |
| 59 | + newUser.setWriteTime(1); // will be ignored | |
| 60 | + | |
| 61 | + userMapper.save(newUser); | |
| 62 | + | |
| 63 | + User fetched = userMapper.get("testlogin2"); | |
| 64 | + assertThat(fetched.getLogin()).isEqualTo("testlogin2"); | |
| 65 | + assertThat(fetched.getName()).isEqualTo("blah"); | |
| 66 | + // write time should be within 30 seconds. | |
| 67 | + assertThat(fetched.getWriteTime()).isGreaterThanOrEqualTo(writeTime).isLessThan(writeTime + 30000000L); | |
| 68 | + assertThat(fetched.getTtl()).isNull(); // TTL should be null since it was not set. | |
| 69 | + | |
| 70 | + // Overwrite with TTL | |
| 71 | + session.execute("insert into user (login, name) values ('testlogin2', 'blah') using TTL 600"); | |
| 72 | + fetched = userMapper.get("testlogin2"); | |
| 73 | + assertThat(fetched.getWriteTime()).isGreaterThanOrEqualTo(writeTime).isLessThan(writeTime + 30000000L); | |
| 74 | + assertThat(fetched.getTtl()).isBetween(570, 600); // TTL should be within 30 secs. | |
| 75 | + | |
| 76 | + // cleanup | |
| 77 | + userMapper.delete(newUser); | |
| 78 | + } | |
| 79 | + | |
| 80 | + @Test(groups = "short") | |
| 81 | + void should_add_aliases_for_fields_in_select_queries() { | |
| 82 | + BoundStatement bs = (BoundStatement)userMapper.getQuery("test"); | |
| 83 | + assertThat(bs.preparedStatement().getQueryString()) | |
| 84 | + .contains("SELECT", "\"login\" AS col1", "\"name\" AS col2", "writetime(\"name\") AS col3"); | |
| 85 | + } | |
| 86 | + | |
| 87 | + @Test(groups = "short") | |
| 88 | + public void should_map_aliased_resultset_to_objects() { | |
| 89 | + Statement getQuery = userMapper.getQuery("testlogin"); | |
| 90 | + getQuery.setConsistencyLevel(ConsistencyLevel.QUORUM); | |
| 91 | + ResultSet rs = session.execute(getQuery); | |
| 92 | + | |
| 93 | + Result<User> result = userMapper.mapAliased(rs); | |
| 94 | + User user = result.one(); | |
| 95 | + | |
| 96 | + assertThat(user.getLogin()).isEqualTo("testlogin"); | |
| 97 | + } | |
| 98 | + | |
| 99 | + @Test(groups = "short") | |
| 100 | + void should_map_unaliased_resultset_to_objects() { | |
| 101 | + UserAccessor userAccessor = mappingManager.createAccessor(UserAccessor.class); | |
| 102 | + ResultSet rs = userAccessor.all(); | |
| 103 | + | |
| 104 | + Result<User> result = userMapper.map(rs); | |
| 105 | + User user = result.one(); | |
| 106 | + assertThat(user.getLogin()).isEqualTo("testlogin"); | |
| 107 | + assertThat(user.getWriteTime()).isEqualTo(0); | |
| 108 | + } | |
| 109 | + | |
| 110 | + @Test(groups = "short", expectedExceptions = IllegalArgumentException.class) | |
| 111 | + void should_fail_if_computed_field_is_not_right_type() { | |
| 112 | + mappingManager.mapper(User_WrongComputedType.class); | |
| 113 | + } | |
| 114 | + | |
| 115 | + @Test(groups = "short") | |
| 116 | + void should_fail_if_computed_field_marked_with_column_annotation() { | |
| 117 | + Mapper<User_WrongAnnotationForComputed> mapper = mappingManager.mapper(User_WrongAnnotationForComputed.class); | |
| 118 | + try { | |
| 119 | + mapper.save(new User_WrongAnnotationForComputed("test", "foo")); | |
| 120 | + fail("Expected an InvalidQueryException"); | |
| 121 | + } catch (InvalidQueryException e) {/*expected*/} | |
| 122 | + | |
| 123 | + try { | |
| 124 | + User_WrongAnnotationForComputed saved = mapper.get(42); | |
| 125 | + fail("Expected an InvalidQueryException"); | |
| 126 | + } catch (InvalidQueryException e) {/*expected*/} | |
| 127 | + } | |
| 128 | + | |
| 129 | + @Table(name = "user") | |
| 130 | + public static class User { | |
| 131 | + @PartitionKey | |
| 132 | + private String login; | |
| 133 | + private String name; | |
| 134 | + | |
| 135 | + public User() { | |
| 136 | + } | |
| 137 | + | |
| 138 | + public User(String login, String name) { | |
| 139 | + this.login = login; | |
| 140 | + this.name = name; | |
| 141 | + } | |
| 142 | + | |
| 143 | + // quotes in the column name inserted on purpose | |
| 144 | + // to test the alias generation mechanism | |
| 145 | + @Computed(formula = "writetime(\"name\")") | |
| 146 | + long writeTime; | |
| 147 | + | |
| 148 | + @Computed(formula = "ttl(name)") | |
| 149 | + Integer ttl; | |
| 150 | + | |
| 151 | + public String getLogin() { | |
| 152 | + return login; | |
| 153 | + } | |
| 154 | + | |
| 155 | + public void setLogin(String login) { | |
| 156 | + this.login = login; | |
| 157 | + } | |
| 158 | + | |
| 159 | + public String getName() { | |
| 160 | + return name; | |
| 161 | + } | |
| 162 | + | |
| 163 | + public void setName(String name) { | |
| 164 | + this.name = name; | |
| 165 | + } | |
| 166 | + | |
| 167 | + public long getWriteTime() { | |
| 168 | + return writeTime; | |
| 169 | + } | |
| 170 | + | |
| 171 | + public void setWriteTime(long writeTime) { | |
| 172 | + this.writeTime = writeTime; | |
| 173 | + } | |
| 174 | + | |
| 175 | + public Integer getTtl() { | |
| 176 | + return ttl; | |
| 177 | + } | |
| 178 | + | |
| 179 | + public void setTtl(Integer ttl) { | |
| 180 | + this.ttl = ttl; | |
| 181 | + } | |
| 182 | + } | |
| 183 | + | |
| 184 | + @Accessor | |
| 185 | + interface UserAccessor { | |
| 186 | + @Query("select * from user") | |
| 187 | + ResultSet all(); | |
| 188 | + } | |
| 189 | + | |
| 190 | + @Table(name = "key_value") | |
| 191 | + public static class KeyValue { | |
| 192 | + @PartitionKey | |
| 193 | + private int key; | |
| 194 | + private String v; | |
| 195 | + | |
| 196 | + @Computed(formula = "writetime(\"v\")") | |
| 197 | + long writeTime; | |
| 198 | + | |
| 199 | + @Computed(formula = "ttl(v)") | |
| 200 | + Integer ttl; | |
| 201 | + | |
| 202 | + public KeyValue() { | |
| 203 | + } | |
| 204 | + | |
| 205 | + public KeyValue(int k, String val) { | |
| 206 | + this.key = k; | |
| 207 | + this.v = val; | |
| 208 | + } | |
| 209 | + | |
| 210 | + public int getKey() { | |
| 211 | + return this.key; | |
| 212 | + } | |
| 213 | + | |
| 214 | + public void setKey(int pk) { | |
| 215 | + this.key = pk; | |
| 216 | + } | |
| 217 | + | |
| 218 | + public String getV() { | |
| 219 | + return this.v; | |
| 220 | + } | |
| 221 | + | |
| 222 | + public void setV(String val) { | |
| 223 | + this.v = val; | |
| 224 | + } | |
| 225 | + | |
| 226 | + public long getWriteTime() { | |
| 227 | + return this.writeTime; | |
| 228 | + } | |
| 229 | + | |
| 230 | + public void setWriteTime(long pk) { | |
| 231 | + this.writeTime = pk; | |
| 232 | + } | |
| 233 | + | |
| 234 | + public Integer getTtl() { | |
| 235 | + return this.ttl; | |
| 236 | + } | |
| 237 | + | |
| 238 | + public void setTtl(Integer ttl) { | |
| 239 | + this.ttl = ttl; | |
| 240 | + } | |
| 241 | + } | |
| 242 | + | |
| 243 | + @Table(name = "user") | |
| 244 | + public static class User_WrongComputedType { | |
| 245 | + @PartitionKey | |
| 246 | + private String login; | |
| 247 | + private String name; | |
| 248 | + | |
| 249 | + @Column(name = "writetime(v)") | |
| 250 | + byte writeTime; | |
| 251 | + | |
| 252 | + public String getLogin() { | |
| 253 | + return login; | |
| 254 | + } | |
| 255 | + | |
| 256 | + public void setLogin(String login) { | |
| 257 | + this.login = login; | |
| 258 | + } | |
| 259 | + | |
| 260 | + public String getName() { | |
| 261 | + return name; | |
| 262 | + } | |
| 263 | + | |
| 264 | + public void setName(String name) { | |
| 265 | + this.name = name; | |
| 266 | + } | |
| 267 | + | |
| 268 | + public byte getWriteTime() { | |
| 269 | + return writeTime; | |
| 270 | + } | |
| 271 | + | |
| 272 | + public void setWriteTime(byte writeTime) { | |
| 273 | + this.writeTime = writeTime; | |
| 274 | + } | |
| 275 | + } | |
| 276 | + | |
| 277 | + @Table(name = "user") | |
| 278 | + public static class User_WrongAnnotationForComputed { | |
| 279 | + @PartitionKey | |
| 280 | + private String login; | |
| 281 | + private String name; | |
| 282 | + | |
| 283 | + @Column(name = "writetime(v)") | |
| 284 | + long writeTime; | |
| 285 | + | |
| 286 | + public User_WrongAnnotationForComputed() { | |
| 287 | + } | |
| 288 | + | |
| 289 | + public User_WrongAnnotationForComputed(String login, String name) { | |
| 290 | + this.login = login; | |
| 291 | + this.name = name; | |
| 292 | + } | |
| 293 | + | |
| 294 | + public String getLogin() { | |
| 295 | + return login; | |
| 296 | + } | |
| 297 | + | |
| 298 | + public void setLogin(String login) { | |
| 299 | + this.login = login; | |
| 300 | + } | |
| 301 | + | |
| 302 | + public String getName() { | |
| 303 | + return name; | |
| 304 | + } | |
| 305 | + | |
| 306 | + public void setName(String name) { | |
| 307 | + this.name = name; | |
| 308 | + } | |
| 309 | + | |
| 310 | + public long getWriteTime() { | |
| 311 | + return writeTime; | |
| 312 | + } | |
| 313 | + | |
| 314 | + public void setWriteTime(long writeTime) { | |
| 315 | + this.writeTime = writeTime; | |
| 316 | + } | |
| 317 | + } | |
| 318 | +} |
modified driver-mapping/src/test/java/com/datastax/driver/mapping/MapperTest.java +38 −0
| @@ -22,6 +22,8 @@ | ||
| 22 | 22 | import com.google.common.base.Objects; |
| 23 | 23 | import com.google.common.util.concurrent.ListenableFuture; |
| 24 | 24 | import org.testng.annotations.Test; |
| 25 | + | |
| 26 | +import static org.assertj.core.api.Assertions.assertThat; | |
| 25 | 27 | import static org.testng.Assert.*; |
| 26 | 28 | |
| 27 | 29 | import com.datastax.driver.mapping.MapperTest.User.Gender; |
| @@ -377,4 +379,40 @@ | ||
| 377 | 379 | userAccessor.updateNameAndGender("Paule", null, u1.getUserId()); |
| 378 | 380 | assertEquals(userMapper.get(u1.getUserId()).getGender(), null); |
| 379 | 381 | } |
| 382 | + | |
| 383 | + @Test(groups="short") | |
| 384 | + public void should_map_objects_from_partial_queries() throws Exception { | |
| 385 | + MappingManager manager = new MappingManager(session); | |
| 386 | + | |
| 387 | + Mapper<Post> m = manager.mapper(Post.class); | |
| 388 | + | |
| 389 | + // Insert a few posts | |
| 390 | + User u1 = new User("Paul", "paul@gmail.com", User.Gender.MALE); | |
| 391 | + Post p1 = new Post(u1, "Something about mapping"); | |
| 392 | + Post p2 = new Post(u1, "Something else"); | |
| 393 | + Post p3 = new Post(u1, "Something more"); | |
| 394 | + | |
| 395 | + p1.setDevice(InetAddress.getLocalHost()); | |
| 396 | + p2.setTags(new HashSet<String>(Arrays.asList("important", "keeper"))); | |
| 397 | + | |
| 398 | + m.save(p1); | |
| 399 | + m.save(p2); | |
| 400 | + m.save(p3); | |
| 401 | + | |
| 402 | + // Retrieve posts with a projection query that only retrieves some of the fields | |
| 403 | + ResultSet rs = session.execute("select user_id, post_id, title from posts where user_id = ?", u1.getUserId()); | |
| 404 | + | |
| 405 | + Result<Post> result = m.map(rs); | |
| 406 | + for (Post post : result) { | |
| 407 | + assertThat(post.getUserId()).isEqualTo(u1.getUserId()); | |
| 408 | + assertThat(post.getPostId()).isNotNull(); | |
| 409 | + assertThat(post.getTitle()).isNotNull(); | |
| 410 | + | |
| 411 | + assertThat(post.getDevice()).isNull(); | |
| 412 | + assertThat(post.getTags()).isNull(); | |
| 413 | + } | |
| 414 | + | |
| 415 | + // cleanup | |
| 416 | + session.execute("delete from posts where user_id = ?", u1.getUserId()); | |
| 417 | + } | |
| 380 | 418 | } |
added driver-mapping/src/test/resources/log4j.properties +28 −0
| @@ -0,0 +1,28 @@ | ||
| 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 | + | |
| 17 | +# Set root logger level to DEBUG and its only appender to A1. | |
| 18 | +log4j.rootLogger=INFO, A1 | |
| 19 | + | |
| 20 | +# Scassandra's info log is a bit verbose | |
| 21 | +log4j.logger.org.scassandra=WARN | |
| 22 | + | |
| 23 | +# A1 is set to be a ConsoleAppender. | |
| 24 | +log4j.appender.A1=org.apache.log4j.ConsoleAppender | |
| 25 | + | |
| 26 | +# A1 uses PatternLayout. | |
| 27 | +log4j.appender.A1.layout=org.apache.log4j.PatternLayout | |
| 28 | +log4j.appender.A1.layout.ConversionPattern=\ %-6r [%t] %-5p %c %x - %m%n |