|
17 | 17 |
|
18 | 18 | import io.r2dbc.spi.ConnectionFactory;
|
19 | 19 |
|
| 20 | +import java.util.Collections; |
20 | 21 | import java.util.Optional;
|
21 | 22 |
|
22 | 23 | import org.springframework.context.annotation.Bean;
|
23 | 24 | import org.springframework.context.annotation.Configuration;
|
| 25 | +import org.springframework.core.convert.converter.Converter; |
| 26 | +import org.springframework.data.convert.CustomConversions; |
| 27 | +import org.springframework.data.convert.CustomConversions.StoreConversions; |
24 | 28 | import org.springframework.data.r2dbc.dialect.Database;
|
25 | 29 | import org.springframework.data.r2dbc.dialect.Dialect;
|
26 | 30 | import org.springframework.data.r2dbc.function.DatabaseClient;
|
27 | 31 | import org.springframework.data.r2dbc.function.DefaultReactiveDataAccessStrategy;
|
28 | 32 | import org.springframework.data.r2dbc.function.ReactiveDataAccessStrategy;
|
| 33 | +import org.springframework.data.r2dbc.function.convert.R2dbcCustomConversions; |
29 | 34 | import org.springframework.data.r2dbc.support.R2dbcExceptionTranslator;
|
30 | 35 | import org.springframework.data.r2dbc.support.SqlErrorCodeR2dbcExceptionTranslator;
|
31 | 36 | import org.springframework.data.relational.core.conversion.BasicRelationalConverter;
|
@@ -95,33 +100,59 @@ public DatabaseClient databaseClient(ReactiveDataAccessStrategy dataAccessStrate
|
95 | 100 | * Register a {@link RelationalMappingContext} and apply an optional {@link NamingStrategy}.
|
96 | 101 | *
|
97 | 102 | * @param namingStrategy optional {@link NamingStrategy}. Use {@link NamingStrategy#INSTANCE} as fallback.
|
| 103 | + * @param r2dbcCustomConversions customized R2DBC conversions. |
98 | 104 | * @return must not be {@literal null}.
|
99 | 105 | * @throws IllegalArgumentException if any of the required args is {@literal null}.
|
100 | 106 | */
|
101 | 107 | @Bean
|
102 |
| - public RelationalMappingContext r2dbcMappingContext(Optional<NamingStrategy> namingStrategy) { |
| 108 | + public RelationalMappingContext r2dbcMappingContext(Optional<NamingStrategy> namingStrategy, |
| 109 | + R2dbcCustomConversions r2dbcCustomConversions) { |
103 | 110 |
|
104 | 111 | Assert.notNull(namingStrategy, "NamingStrategy must not be null!");
|
105 | 112 |
|
106 |
| - return new RelationalMappingContext(namingStrategy.orElse(NamingStrategy.INSTANCE)); |
| 113 | + RelationalMappingContext relationalMappingContext = new RelationalMappingContext( |
| 114 | + namingStrategy.orElse(NamingStrategy.INSTANCE)); |
| 115 | + relationalMappingContext.setSimpleTypeHolder(r2dbcCustomConversions.getSimpleTypeHolder()); |
| 116 | + |
| 117 | + return relationalMappingContext; |
107 | 118 | }
|
108 | 119 |
|
109 | 120 | /**
|
110 | 121 | * Creates a {@link ReactiveDataAccessStrategy} using the configured {@link #r2dbcMappingContext(Optional)
|
111 | 122 | * RelationalMappingContext}.
|
112 | 123 | *
|
113 | 124 | * @param mappingContext the configured {@link RelationalMappingContext}.
|
| 125 | + * @param r2dbcCustomConversions customized R2DBC conversions. |
114 | 126 | * @return must not be {@literal null}.
|
115 |
| - * @see #r2dbcMappingContext(Optional) |
| 127 | + * @see #r2dbcMappingContext(Optional, R2dbcCustomConversions) |
116 | 128 | * @see #getDialect(ConnectionFactory)
|
117 | 129 | * @throws IllegalArgumentException if any of the {@literal mappingContext} is {@literal null}.
|
118 | 130 | */
|
119 | 131 | @Bean
|
120 |
| - public ReactiveDataAccessStrategy reactiveDataAccessStrategy(RelationalMappingContext mappingContext) { |
| 132 | + public ReactiveDataAccessStrategy reactiveDataAccessStrategy(RelationalMappingContext mappingContext, |
| 133 | + R2dbcCustomConversions r2dbcCustomConversions) { |
121 | 134 |
|
122 | 135 | Assert.notNull(mappingContext, "MappingContext must not be null!");
|
123 |
| - return new DefaultReactiveDataAccessStrategy(getDialect(connectionFactory()), |
124 |
| - new BasicRelationalConverter(mappingContext)); |
| 136 | + |
| 137 | + BasicRelationalConverter converter = new BasicRelationalConverter(mappingContext, r2dbcCustomConversions); |
| 138 | + |
| 139 | + return new DefaultReactiveDataAccessStrategy(getDialect(connectionFactory()), converter); |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * Register custom {@link Converter}s in a {@link CustomConversions} object if required. These |
| 144 | + * {@link CustomConversions} will be registered with the {@link BasicRelationalConverter} and |
| 145 | + * {@link #r2dbcMappingContext(Optional, R2dbcCustomConversions)}. Returns an empty {@link R2dbcCustomConversions} |
| 146 | + * instance by default. |
| 147 | + * |
| 148 | + * @return must not be {@literal null}. |
| 149 | + */ |
| 150 | + @Bean |
| 151 | + public R2dbcCustomConversions r2dbcCustomConversions() { |
| 152 | + |
| 153 | + Dialect dialect = getDialect(connectionFactory()); |
| 154 | + StoreConversions storeConversions = StoreConversions.of(dialect.getSimpleTypeHolder()); |
| 155 | + return new R2dbcCustomConversions(storeConversions, Collections.emptyList()); |
125 | 156 | }
|
126 | 157 |
|
127 | 158 | /**
|
|
0 commit comments