Skip to content

Commit 02cf351

Browse files
committed
Move off RowMetadata.getColumnNames(…) in preparation for R2DBC 0.9.
Closes #683
1 parent 4d65223 commit 02cf351

File tree

5 files changed

+85
-28
lines changed

5 files changed

+85
-28
lines changed

src/main/java/org/springframework/data/r2dbc/convert/MappingR2dbcConverter.java

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@
1515
*/
1616
package org.springframework.data.r2dbc.convert;
1717

18+
import io.r2dbc.spi.ColumnMetadata;
1819
import io.r2dbc.spi.Row;
1920
import io.r2dbc.spi.RowMetadata;
2021

2122
import java.util.ArrayList;
2223
import java.util.Collection;
2324
import java.util.Collections;
25+
import java.util.Iterator;
2426
import java.util.List;
2527
import java.util.Optional;
2628
import java.util.function.BiFunction;
@@ -163,7 +165,7 @@ private Object readFrom(Row row, @Nullable RowMetadata metadata, RelationalPersi
163165
try {
164166

165167
Object value = null;
166-
if (metadata == null || metadata.getColumnNames().contains(identifier)) {
168+
if (metadata == null || RowMetadataUtils.containsColumn(metadata, identifier)) {
167169
value = row.get(identifier);
168170
}
169171

@@ -187,6 +189,7 @@ private Object readFrom(Row row, @Nullable RowMetadata metadata, RelationalPersi
187189
}
188190
}
189191

192+
190193
public Object readValue(@Nullable Object value, TypeInformation<?> type) {
191194

192195
if (null == value) {
@@ -624,17 +627,8 @@ public <T> BiFunction<Row, RowMetadata, T> populateIdIfNecessary(T object) {
624627
private boolean potentiallySetId(Row row, RowMetadata metadata, PersistentPropertyAccessor<?> propertyAccessor,
625628
RelationalPersistentProperty idProperty) {
626629

627-
Collection<String> columns = metadata.getColumnNames();
628-
Object generatedIdValue = null;
629630
String idColumnName = idProperty.getColumnName().getReference();
630-
631-
if (columns.contains(idColumnName)) {
632-
generatedIdValue = row.get(idColumnName);
633-
} else if (columns.size() == 1) {
634-
635-
String key = columns.iterator().next();
636-
generatedIdValue = row.get(key);
637-
}
631+
Object generatedIdValue = extractGeneratedIdentifier(row, metadata, idColumnName);
638632

639633
if (generatedIdValue == null) {
640634
return false;
@@ -646,6 +640,24 @@ private boolean potentiallySetId(Row row, RowMetadata metadata, PersistentProper
646640
return true;
647641
}
648642

643+
@Nullable
644+
private Object extractGeneratedIdentifier(Row row, RowMetadata metadata, String idColumnName) {
645+
646+
if (RowMetadataUtils.containsColumn(metadata, idColumnName)) {
647+
return row.get(idColumnName);
648+
}
649+
650+
Iterable<? extends ColumnMetadata> columns = metadata.getColumnMetadatas();
651+
Iterator<? extends ColumnMetadata> it = columns.iterator();
652+
653+
if (it.hasNext()) {
654+
ColumnMetadata column = it.next();
655+
return row.get(column.getName());
656+
}
657+
658+
return null;
659+
}
660+
649661
private <R> RelationalPersistentEntity<R> getRequiredPersistentEntity(Class<R> type) {
650662
return (RelationalPersistentEntity<R>) getMappingContext().getRequiredPersistentEntity(type);
651663
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright 2021 the original author or authors.
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+
* https://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 org.springframework.data.r2dbc.convert;
17+
18+
import io.r2dbc.spi.ColumnMetadata;
19+
import io.r2dbc.spi.RowMetadata;
20+
21+
/**
22+
* Utility methods for {@link io.r2dbc.spi.RowMetadata}
23+
*
24+
* @author Mark Paluch
25+
* @since 1.3.7
26+
*/
27+
class RowMetadataUtils {
28+
29+
/**
30+
* Check whether the column {@code name} is contained in {@link RowMetadata}. The check happens case-insensitive.
31+
*
32+
* @param metadata the metadata object to inspect.
33+
* @param name column name.
34+
* @return {@code true} if the metadata contains the column {@code name}.
35+
*/
36+
public static boolean containsColumn(RowMetadata metadata, String name) {
37+
38+
for (ColumnMetadata columnMetadata : metadata.getColumnMetadatas()) {
39+
if (name.equalsIgnoreCase(columnMetadata.getName())) {
40+
return true;
41+
}
42+
}
43+
44+
return false;
45+
}
46+
}

src/main/java/org/springframework/data/r2dbc/convert/RowPropertyAccessor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public Class<?>[] getSpecificTargetClasses() {
4444

4545
@Override
4646
public boolean canRead(EvaluationContext context, @Nullable Object target, String name) {
47-
return rowMetadata != null && target != null && rowMetadata.getColumnNames().contains(name);
47+
return rowMetadata != null && target != null && RowMetadataUtils.containsColumn(rowMetadata, name);
4848
}
4949

5050
@Override

src/test/java/org/springframework/data/r2dbc/convert/EntityRowMapperUnitTests.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55

66
import io.r2dbc.spi.Row;
77
import io.r2dbc.spi.RowMetadata;
8+
import io.r2dbc.spi.test.MockColumnMetadata;
9+
import io.r2dbc.spi.test.MockRowMetadata;
810
import lombok.RequiredArgsConstructor;
911

10-
import java.util.Collection;
1112
import java.util.EnumSet;
1213
import java.util.List;
1314
import java.util.Set;
1415

15-
import org.junit.jupiter.api.BeforeEach;
1616
import org.junit.jupiter.api.Test;
1717
import org.junit.jupiter.api.extension.ExtendWith;
1818
import org.mockito.junit.jupiter.MockitoExtension;
@@ -32,15 +32,15 @@ public class EntityRowMapperUnitTests {
3232
DefaultReactiveDataAccessStrategy strategy = new DefaultReactiveDataAccessStrategy(PostgresDialect.INSTANCE);
3333

3434
Row rowMock = mock(Row.class);
35-
RowMetadata metadata = mock(RowMetadata.class);
36-
Collection<String> columns = mock(Collection.class);
37-
38-
@BeforeEach
39-
public void before() {
40-
41-
when(columns.contains(anyString())).thenReturn(true);
42-
when(metadata.getColumnNames()).thenReturn(columns);
43-
}
35+
RowMetadata metadata = MockRowMetadata.builder()
36+
.columnMetadata(MockColumnMetadata.builder().name("integer_set").build())
37+
.columnMetadata(MockColumnMetadata.builder().name("boxed_integers").build())
38+
.columnMetadata(MockColumnMetadata.builder().name("primitive_integers").build())
39+
.columnMetadata(MockColumnMetadata.builder().name("enum_array").build())
40+
.columnMetadata(MockColumnMetadata.builder().name("set_of_enum").build())
41+
.columnMetadata(MockColumnMetadata.builder().name("enum_set").build())
42+
.columnMetadata(MockColumnMetadata.builder().name("id").build())
43+
.columnMetadata(MockColumnMetadata.builder().name("ids").build()).build();
4444

4545
@Test // gh-22
4646
public void shouldMapSimpleEntity() {

src/test/java/org/springframework/data/r2dbc/core/ReactiveDataAccessStrategyTestSupport.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020

2121
import io.r2dbc.spi.Row;
2222
import io.r2dbc.spi.RowMetadata;
23+
import io.r2dbc.spi.test.MockColumnMetadata;
24+
import io.r2dbc.spi.test.MockRowMetadata;
2325
import lombok.Data;
2426

2527
import java.math.BigDecimal;
@@ -29,7 +31,6 @@
2931
import java.time.LocalTime;
3032
import java.time.OffsetDateTime;
3133
import java.time.ZonedDateTime;
32-
import java.util.Collection;
3334
import java.util.UUID;
3435
import java.util.function.BiConsumer;
3536
import java.util.function.Function;
@@ -196,10 +197,8 @@ private <T> void testType(BiConsumer<PrimitiveTypes, T> setter, Function<Primiti
196197

197198
ReactiveDataAccessStrategy strategy = getStrategy();
198199
Row rowMock = mock(Row.class);
199-
RowMetadata metadataMock = mock(RowMetadata.class);
200-
Collection<String> columnNames = mock(Collection.class);
201-
when(metadataMock.getColumnNames()).thenReturn(columnNames);
202-
when(columnNames.contains(fieldname)).thenReturn(true);
200+
RowMetadata metadataMock = MockRowMetadata.builder()
201+
.columnMetadata(MockColumnMetadata.builder().name(fieldname).build()).build();
203202

204203
PrimitiveTypes toSave = new PrimitiveTypes();
205204
setter.accept(toSave, testValue);

0 commit comments

Comments
 (0)