Skip to content

Commit ec8f540

Browse files
committed
#166 - Polishing.
Consider Dialect-specific converters also in DatabaseClient.create(…) factory. Reduce constant visibility. Add tests. Reformat code. Original pull request: #168.
1 parent 19f572f commit ec8f540

File tree

5 files changed

+76
-20
lines changed

5 files changed

+76
-20
lines changed

src/main/java/org/springframework/data/r2dbc/config/AbstractR2dbcConfiguration.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717

1818
import io.r2dbc.spi.ConnectionFactory;
1919

20+
import java.util.ArrayList;
2021
import java.util.Collections;
22+
import java.util.List;
2123
import java.util.Optional;
2224

2325
import org.springframework.beans.BeansException;
@@ -174,7 +176,11 @@ public R2dbcCustomConversions r2dbcCustomConversions() {
174176
protected StoreConversions getStoreConversions() {
175177

176178
R2dbcDialect dialect = getDialect(lookupConnectionFactory());
177-
return StoreConversions.of(dialect.getSimpleTypeHolder(), dialect.getConverters(), R2dbcCustomConversions.STORE_CONVERTERS);
179+
180+
List<Object> converters = new ArrayList<>(dialect.getConverters());
181+
converters.addAll(R2dbcCustomConversions.STORE_CONVERTERS);
182+
183+
return StoreConversions.of(dialect.getSimpleTypeHolder(), converters);
178184
}
179185

180186
/**

src/main/java/org/springframework/data/r2dbc/core/DefaultReactiveDataAccessStrategy.java

+11-1
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,23 @@ public DefaultReactiveDataAccessStrategy(R2dbcDialect dialect, Collection<?> con
8282
this(dialect, createConverter(dialect, converters));
8383
}
8484

85+
/**
86+
* Creates a new {@link R2dbcConverter} given {@link R2dbcDialect} and custom {@code converters}.
87+
*
88+
* @param dialect must not be {@literal null}.
89+
* @param converters must not be {@literal null}.
90+
* @return the {@link R2dbcConverter}.
91+
*/
8592
public static R2dbcConverter createConverter(R2dbcDialect dialect, Collection<?> converters) {
8693

8794
Assert.notNull(dialect, "Dialect must not be null");
8895
Assert.notNull(converters, "Converters must not be null");
8996

97+
List<Object> storeConverters = new ArrayList<>(dialect.getConverters());
98+
storeConverters.addAll(R2dbcCustomConversions.STORE_CONVERTERS);
99+
90100
R2dbcCustomConversions customConversions = new R2dbcCustomConversions(
91-
StoreConversions.of(dialect.getSimpleTypeHolder(), R2dbcCustomConversions.STORE_CONVERTERS), converters);
101+
StoreConversions.of(dialect.getSimpleTypeHolder(), storeConverters), storeConverters);
92102

93103
RelationalMappingContext context = new RelationalMappingContext();
94104
context.setSimpleTypeHolder(customConversions.getSimpleTypeHolder());

src/main/java/org/springframework/data/r2dbc/dialect/MySqlDialect.java

+10-17
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@
1818
import java.net.InetAddress;
1919
import java.net.URI;
2020
import java.net.URL;
21-
import java.util.ArrayList;
2221
import java.util.Arrays;
2322
import java.util.Collection;
2423
import java.util.Collections;
2524
import java.util.HashSet;
2625
import java.util.List;
2726
import java.util.Set;
2827
import java.util.UUID;
28+
2929
import org.springframework.core.convert.converter.Converter;
3030

3131
/**
@@ -45,19 +45,11 @@ public class MySqlDialect extends org.springframework.data.relational.core.diale
4545
public static final MySqlDialect INSTANCE = new MySqlDialect();
4646

4747
private static final BindMarkersFactory ANONYMOUS = BindMarkersFactory.anonymous("?");
48-
48+
4949
/**
50-
* MySql specific converters.
50+
* MySQL specific converters.
5151
*/
52-
public static final List<Object> CONVERTERS;
53-
54-
static {
55-
List<Object> converters = new ArrayList<>();
56-
57-
converters.add(ByteToBooleanConverter.INSTANCE);
58-
59-
CONVERTERS = Collections.unmodifiableList(converters);
60-
}
52+
private static final List<Object> CONVERTERS = Collections.singletonList(ByteToBooleanConverter.INSTANCE);
6153

6254
/*
6355
* (non-Javadoc)
@@ -76,7 +68,7 @@ public BindMarkersFactory getBindMarkersFactory() {
7668
public Collection<? extends Class<?>> getSimpleTypes() {
7769
return SIMPLE_TYPES;
7870
}
79-
71+
8072
/*
8173
* (non-Javadoc)
8274
* @see org.springframework.data.r2dbc.dialect.R2dbcDialect#getConverters()
@@ -85,11 +77,10 @@ public Collection<? extends Class<?>> getSimpleTypes() {
8577
public Collection<Object> getConverters() {
8678
return CONVERTERS;
8779
}
88-
80+
8981
/**
90-
* Simple singleton to convert {@link Byte}s to their {@link Boolean}
91-
* representation. MySQL does not have a built in boolean type by default,
92-
* so relies on using a byte instead. Non-zero values represent true.
82+
* Simple singleton to convert {@link Byte}s to their {@link Boolean} representation. MySQL does not have a built-in
83+
* boolean type by default, so relies on using a byte instead. Non-zero values represent {@literal true}.
9384
*
9485
* @author Michael Berry
9586
*/
@@ -99,9 +90,11 @@ public enum ByteToBooleanConverter implements Converter<Byte, Boolean> {
9990

10091
@Override
10192
public Boolean convert(Byte s) {
93+
10294
if (s == null) {
10395
return null;
10496
}
97+
10598
return s != 0;
10699
}
107100
}

src/main/java/org/springframework/data/r2dbc/dialect/R2dbcDialect.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
*
1515
* @author Mark Paluch
1616
* @author Jens Schauder
17+
* @author Michael Berry
1718
*/
1819
public interface R2dbcDialect extends Dialect {
1920

@@ -51,7 +52,7 @@ default SimpleTypeHolder getSimpleTypeHolder() {
5152

5253
/**
5354
* Return a collection of converters for this dialect.
54-
*
55+
*
5556
* @return a collection of converters for this dialect.
5657
*/
5758
default Collection<Object> getConverters() {

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

+46
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,22 @@
1515
*/
1616
package org.springframework.data.r2dbc.core;
1717

18+
import static org.assertj.core.api.Assertions.*;
19+
1820
import io.r2dbc.spi.ConnectionFactory;
21+
import lombok.Data;
22+
import reactor.test.StepVerifier;
1923

2024
import javax.sql.DataSource;
2125

2226
import org.junit.ClassRule;
27+
import org.junit.Test;
2328

29+
import org.springframework.dao.DataAccessException;
2430
import org.springframework.data.r2dbc.testing.ExternalDatabase;
2531
import org.springframework.data.r2dbc.testing.MySqlTestSupport;
32+
import org.springframework.data.relational.core.mapping.Table;
33+
import org.springframework.jdbc.core.JdbcTemplate;
2634

2735
/**
2836
* Integration tests for {@link DatabaseClient} against MySQL.
@@ -47,4 +55,42 @@ protected ConnectionFactory createConnectionFactory() {
4755
protected String getCreateTableStatement() {
4856
return MySqlTestSupport.CREATE_TABLE_LEGOSET;
4957
}
58+
59+
@Test // gh-166
60+
public void considersBuiltInConverters() {
61+
62+
ConnectionFactory connectionFactory = createConnectionFactory();
63+
JdbcTemplate jdbc = createJdbcTemplate(createDataSource());
64+
65+
try {
66+
jdbc.execute("DROP TABLE boolean_mapping");
67+
} catch (DataAccessException e) {}
68+
jdbc.execute("CREATE TABLE boolean_mapping (id int, flag1 TINYINT, flag2 TINYINT)");
69+
70+
BooleanMapping mapping = new BooleanMapping();
71+
mapping.setId(42);
72+
mapping.setFlag1(true);
73+
74+
DatabaseClient databaseClient = DatabaseClient.create(connectionFactory);
75+
76+
databaseClient.insert().into(BooleanMapping.class).using(mapping).then() //
77+
.as(StepVerifier::create) //
78+
.verifyComplete();
79+
80+
databaseClient.select().from(BooleanMapping.class).fetch().first() //
81+
.as(StepVerifier::create) //
82+
.consumeNextWith(actual -> assertThat(actual.isFlag1()).isTrue()) //
83+
.verifyComplete();
84+
}
85+
86+
@Table("boolean_mapping")
87+
@Data
88+
static class BooleanMapping {
89+
90+
int id;
91+
boolean flag1;
92+
boolean flag2;
93+
94+
}
95+
5096
}

0 commit comments

Comments
 (0)