Skip to content

Commit 1091e4a

Browse files
Alexandre Dutraolim7t
authored andcommitted
Add tests for case-sensitivity in the mapper.
1 parent a1704f2 commit 1091e4a

11 files changed

Lines changed: 239 additions & 69 deletions

File tree

driver-mapping/src/main/java/com/datastax/driver/mapping/AnnotationParser.java

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package com.datastax.driver.mapping;
1717

1818
import com.datastax.driver.core.ConsistencyLevel;
19+
import com.datastax.driver.core.Metadata;
1920
import com.datastax.driver.core.TableMetadata;
2021
import com.datastax.driver.core.TypeCodec;
2122
import com.datastax.driver.core.UserType;
@@ -34,8 +35,6 @@
3435
import java.util.*;
3536
import java.util.concurrent.atomic.AtomicInteger;
3637

37-
import static com.datastax.driver.core.Metadata.quote;
38-
3938
/**
4039
* Static methods that facilitates parsing:
4140
* - {@link #parseEntity(Class, MappingManager)}: entity classes into {@link EntityMapper} instances
@@ -48,13 +47,13 @@ class AnnotationParser {
4847
@VisibleForTesting
4948
static final Set<Class<? extends Annotation>> VALID_PROPERTY_ANNOTATIONS = ImmutableSet.of(
5049
Column.class,
50+
Computed.class,
5151
ClusteringColumn.class,
5252
Frozen.class,
5353
FrozenKey.class,
5454
FrozenValue.class,
5555
PartitionKey.class,
56-
Transient.class,
57-
Computed.class);
56+
Transient.class);
5857

5958
@VisibleForTesting
6059
static final Set<Class<? extends Annotation>> VALID_FIELD_ANNOTATIONS = ImmutableSet.of(
@@ -77,20 +76,20 @@ private AnnotationParser() {
7776
static <T> EntityMapper<T> parseEntity(final Class<T> entityClass, MappingManager mappingManager) {
7877
Table table = AnnotationChecks.getTypeAnnotation(Table.class, entityClass);
7978

80-
String ksName = table.caseSensitiveKeyspace() ? table.keyspace() : table.keyspace().toLowerCase();
81-
String tableName = table.caseSensitiveTable() ? table.name() : table.name().toLowerCase();
79+
String ksName = table.caseSensitiveKeyspace() ? Metadata.quote(table.keyspace()) : table.keyspace().toLowerCase();
80+
String tableName = table.caseSensitiveTable() ? Metadata.quote(table.name()) : table.name().toLowerCase();
8281

8382
ConsistencyLevel writeConsistency = table.writeConsistency().isEmpty() ? null : ConsistencyLevel.valueOf(table.writeConsistency().toUpperCase());
8483
ConsistencyLevel readConsistency = table.readConsistency().isEmpty() ? null : ConsistencyLevel.valueOf(table.readConsistency().toUpperCase());
8584

8685
if (Strings.isNullOrEmpty(table.keyspace())) {
87-
ksName = mappingManager.getSession().getLoggedKeyspace();
88-
if (Strings.isNullOrEmpty(ksName))
86+
String loggedKeyspace = mappingManager.getSession().getLoggedKeyspace();
87+
if (Strings.isNullOrEmpty(loggedKeyspace))
8988
throw new IllegalArgumentException(String.format(
90-
"Error creating mapper for class %s, the @%s annotation declares no default keyspace, and the session is not currently logged to any keyspace",
91-
entityClass.getSimpleName(),
92-
Table.class.getSimpleName()
89+
"Error creating mapper for %s, the @Table annotation declares no default keyspace, and the session is not currently logged to any keyspace",
90+
entityClass
9391
));
92+
ksName = Metadata.quote(loggedKeyspace);
9493
}
9594

9695
EntityMapper<T> mapper = new EntityMapper<T>(entityClass, ksName, tableName, writeConsistency, readConsistency);
@@ -154,17 +153,17 @@ else if (propertyMapper.isClusteringColumn())
154153
static <T> MappedUDTCodec<T> parseUDT(Class<T> udtClass, MappingManager mappingManager) {
155154
UDT udt = AnnotationChecks.getTypeAnnotation(UDT.class, udtClass);
156155

157-
String ksName = udt.caseSensitiveKeyspace() ? udt.keyspace() : udt.keyspace().toLowerCase();
158-
String udtName = udt.caseSensitiveType() ? quote(udt.name()) : udt.name().toLowerCase();
156+
String ksName = udt.caseSensitiveKeyspace() ? Metadata.quote(udt.keyspace()) : udt.keyspace().toLowerCase();
157+
String udtName = udt.caseSensitiveType() ? Metadata.quote(udt.name()) : udt.name().toLowerCase();
159158

160159
if (Strings.isNullOrEmpty(udt.keyspace())) {
161-
ksName = mappingManager.getSession().getLoggedKeyspace();
162-
if (Strings.isNullOrEmpty(ksName))
160+
String loggedKeyspace = mappingManager.getSession().getLoggedKeyspace();
161+
if (Strings.isNullOrEmpty(loggedKeyspace))
163162
throw new IllegalArgumentException(String.format(
164-
"Error creating UDT codec for class %s, the @%s annotation declares no default keyspace, and the session is not currently logged to any keyspace",
165-
udtClass.getSimpleName(),
166-
UDT.class.getSimpleName()
163+
"Error creating UDT codec for %s, the @UDT annotation declares no default keyspace, and the session is not currently logged to any keyspace",
164+
udtClass
167165
));
166+
ksName = Metadata.quote(loggedKeyspace);
168167
}
169168

170169
UserType userType = mappingManager.getSession().getCluster().getMetadata().getKeyspace(ksName).getUserType(udtName);

driver-mapping/src/main/java/com/datastax/driver/mapping/EntityMapper.java

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,11 @@
2020
import java.util.ArrayList;
2121
import java.util.List;
2222

23-
import static com.datastax.driver.core.querybuilder.QueryBuilder.quote;
24-
2523
class EntityMapper<T> {
2624

2725
private final Class<T> entityClass;
28-
private final String keyspace;
29-
private final String table;
26+
final String keyspace;
27+
final String table;
3028

3129
final ConsistencyLevel writeConsistency;
3230
final ConsistencyLevel readConsistency;
@@ -44,14 +42,6 @@ class EntityMapper<T> {
4442
this.readConsistency = readConsistency;
4543
}
4644

47-
String getKeyspace() {
48-
return quote(keyspace);
49-
}
50-
51-
String getTable() {
52-
return quote(table);
53-
}
54-
5545
int primaryKeySize() {
5646
return partitionKeys.size() + clusteringColumns.size();
5747
}

driver-mapping/src/main/java/com/datastax/driver/mapping/MappedUDTCodec.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@
1515
*/
1616
package com.datastax.driver.mapping;
1717

18-
import com.datastax.driver.core.*;
18+
import com.datastax.driver.core.CodecRegistry;
19+
import com.datastax.driver.core.ProtocolVersion;
20+
import com.datastax.driver.core.TypeCodec;
21+
import com.datastax.driver.core.UserType;
1922

2023
import java.nio.ByteBuffer;
2124
import java.util.Map;
@@ -48,10 +51,6 @@ Class<T> getUdtClass() {
4851

4952
@Override
5053
protected ByteBuffer serializeField(T source, String fieldName, ProtocolVersion protocolVersion) {
51-
// The parent class passes lowercase names unquoted, but in our internal map of mappers they are always quoted
52-
if (!fieldName.startsWith("\""))
53-
fieldName = Metadata.quote(fieldName);
54-
5554
PropertyMapper propertyMapper = columnMappers.get(fieldName);
5655

5756
if (propertyMapper == null)
@@ -68,9 +67,6 @@ protected ByteBuffer serializeField(T source, String fieldName, ProtocolVersion
6867

6968
@Override
7069
protected T deserializeAndSetField(ByteBuffer input, T target, String fieldName, ProtocolVersion protocolVersion) {
71-
if (!fieldName.startsWith("\""))
72-
fieldName = Metadata.quote(fieldName);
73-
7470
PropertyMapper propertyMapper = columnMappers.get(fieldName);
7571
if (propertyMapper != null) {
7672
TypeCodec<Object> codec = propertyMapper.customCodec;

driver-mapping/src/main/java/com/datastax/driver/mapping/Mapper.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ public class Mapper<T> {
7272
this.klass = klass;
7373
this.mapper = mapper;
7474

75-
KeyspaceMetadata keyspace = session().getCluster().getMetadata().getKeyspace(mapper.getKeyspace());
76-
this.tableMetadata = keyspace == null ? null : keyspace.getTable(mapper.getTable());
75+
KeyspaceMetadata keyspace = session().getCluster().getMetadata().getKeyspace(mapper.keyspace);
76+
this.tableMetadata = keyspace == null ? null : keyspace.getTable(mapper.table);
7777

7878
this.mapOneFunction = new Function<ResultSet, T>() {
7979
@Override

driver-mapping/src/main/java/com/datastax/driver/mapping/PropertyMapper.java

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -130,18 +130,23 @@ boolean isClusteringColumn() {
130130
}
131131

132132
private String inferColumnName() {
133-
Column column = annotation(Column.class);
134-
if (column != null && !column.name().isEmpty()) {
135-
return Metadata.quote(column.caseSensitive() ? column.name() : column.name().toLowerCase());
136-
}
137-
com.datastax.driver.mapping.annotations.Field udtField = annotation(com.datastax.driver.mapping.annotations.Field.class);
138-
if (udtField != null && !udtField.name().isEmpty()) {
139-
return Metadata.quote(udtField.caseSensitive() ? udtField.name() : udtField.name().toLowerCase());
140-
}
141133
if (isComputed()) {
142134
return annotation(Computed.class).value();
143135
}
144-
return Metadata.quote(propertyName.toLowerCase());
136+
boolean caseSensitive = false;
137+
String columnName = propertyName;
138+
if (hasAnnotation(Column.class)) {
139+
Column column = annotation(Column.class);
140+
caseSensitive = column.caseSensitive();
141+
if (!column.name().isEmpty())
142+
columnName = column.name();
143+
} else if (hasAnnotation(com.datastax.driver.mapping.annotations.Field.class)) {
144+
com.datastax.driver.mapping.annotations.Field udtField = annotation(com.datastax.driver.mapping.annotations.Field.class);
145+
caseSensitive = udtField.caseSensitive();
146+
if (!udtField.name().isEmpty())
147+
columnName = udtField.name();
148+
}
149+
return caseSensitive ? Metadata.quote(columnName) : columnName.toLowerCase();
145150
}
146151

147152
@SuppressWarnings("unchecked")

driver-mapping/src/main/java/com/datastax/driver/mapping/QueryType.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ enum QueryType {
3131
@Override
3232
String makePreparedQueryString(TableMetadata table, EntityMapper<?> mapper, MappingManager manager, Set<PropertyMapper> columns, Collection<Mapper.Option> options) {
3333
Insert insert = table == null
34-
? insertInto(mapper.getKeyspace(), mapper.getTable())
34+
? insertInto(mapper.keyspace, mapper.table)
3535
: insertInto(table);
3636
for (PropertyMapper col : columns)
3737
if (!col.isComputed())
@@ -65,7 +65,7 @@ String makePreparedQueryString(TableMetadata table, EntityMapper<?> mapper, Mapp
6565
}
6666
Select select;
6767
if (table == null) {
68-
select = selection.from(mapper.getKeyspace(), mapper.getTable());
68+
select = selection.from(mapper.keyspace, mapper.table);
6969
} else {
7070
select = selection.from(table);
7171
}
@@ -83,7 +83,7 @@ String makePreparedQueryString(TableMetadata table, EntityMapper<?> mapper, Mapp
8383
@Override
8484
String makePreparedQueryString(TableMetadata table, EntityMapper<?> mapper, MappingManager manager, Set<PropertyMapper> columns, Collection<Mapper.Option> options) {
8585
Delete delete = table == null
86-
? delete().all().from(mapper.getKeyspace(), mapper.getTable())
86+
? delete().all().from(mapper.keyspace, mapper.table)
8787
: delete().all().from(table);
8888
Delete.Where where = delete.where();
8989
for (int i = 0; i < mapper.primaryKeySize(); i++)

0 commit comments

Comments
 (0)