Skip to content

Commit 19f572f

Browse files
berry120mp911de
authored andcommitted
#166 - Add the ability to define dialect-specific converters in R2dbcDialect for MySQL.
Add a MySql specific dialect converter for converting from byte to boolean. Original pull request: #168.
1 parent fee4437 commit 19f572f

File tree

3 files changed

+56
-1
lines changed

3 files changed

+56
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ public R2dbcCustomConversions r2dbcCustomConversions() {
174174
protected StoreConversions getStoreConversions() {
175175

176176
R2dbcDialect dialect = getDialect(lookupConnectionFactory());
177-
return StoreConversions.of(dialect.getSimpleTypeHolder(), R2dbcCustomConversions.STORE_CONVERTERS);
177+
return StoreConversions.of(dialect.getSimpleTypeHolder(), dialect.getConverters(), R2dbcCustomConversions.STORE_CONVERTERS);
178178
}
179179

180180
/**

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

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,15 @@
1818
import java.net.InetAddress;
1919
import java.net.URI;
2020
import java.net.URL;
21+
import java.util.ArrayList;
2122
import java.util.Arrays;
2223
import java.util.Collection;
24+
import java.util.Collections;
2325
import java.util.HashSet;
26+
import java.util.List;
2427
import java.util.Set;
2528
import java.util.UUID;
29+
import org.springframework.core.convert.converter.Converter;
2630

2731
/**
2832
* An SQL dialect for MySQL.
@@ -41,6 +45,19 @@ public class MySqlDialect extends org.springframework.data.relational.core.diale
4145
public static final MySqlDialect INSTANCE = new MySqlDialect();
4246

4347
private static final BindMarkersFactory ANONYMOUS = BindMarkersFactory.anonymous("?");
48+
49+
/**
50+
* MySql specific converters.
51+
*/
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+
}
4461

4562
/*
4663
* (non-Javadoc)
@@ -59,4 +76,33 @@ public BindMarkersFactory getBindMarkersFactory() {
5976
public Collection<? extends Class<?>> getSimpleTypes() {
6077
return SIMPLE_TYPES;
6178
}
79+
80+
/*
81+
* (non-Javadoc)
82+
* @see org.springframework.data.r2dbc.dialect.R2dbcDialect#getConverters()
83+
*/
84+
@Override
85+
public Collection<Object> getConverters() {
86+
return CONVERTERS;
87+
}
88+
89+
/**
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.
93+
*
94+
* @author Michael Berry
95+
*/
96+
public enum ByteToBooleanConverter implements Converter<Byte, Boolean> {
97+
98+
INSTANCE;
99+
100+
@Override
101+
public Boolean convert(Byte s) {
102+
if (s == null) {
103+
return null;
104+
}
105+
return s != 0;
106+
}
107+
}
62108
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,13 @@ default SimpleTypeHolder getSimpleTypeHolder() {
4848

4949
return new SimpleTypeHolder(simpleTypes, true);
5050
}
51+
52+
/**
53+
* Return a collection of converters for this dialect.
54+
*
55+
* @return a collection of converters for this dialect.
56+
*/
57+
default Collection<Object> getConverters() {
58+
return Collections.emptySet();
59+
}
5160
}

0 commit comments

Comments
 (0)