Skip to content

Commit dcc46de

Browse files
committed
#64 - Migrate Select to StatementMapper.
1 parent baf8d1d commit dcc46de

13 files changed

+362
-870
lines changed

src/main/java/org/springframework/data/r2dbc/function/DefaultDatabaseClient.java

+14-28
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@
6060
import org.springframework.data.r2dbc.function.query.Criteria;
6161
import org.springframework.data.r2dbc.function.query.Update;
6262
import org.springframework.data.r2dbc.support.R2dbcExceptionTranslator;
63-
import org.springframework.data.relational.core.sql.Select;
6463
import org.springframework.lang.Nullable;
6564
import org.springframework.util.Assert;
6665
import org.springframework.util.StringUtils;
@@ -749,20 +748,16 @@ public FetchSpec<Map<String, Object>> fetch() {
749748

750749
private <R> FetchSpec<R> exchange(BiFunction<Row, RowMetadata, R> mappingFunction) {
751750

752-
PreparedOperation<Select> operation = dataAccessStrategy.getStatements().select(this.table, this.projectedFields,
753-
(t, configurer) -> {
754-
755-
configurer.withPageRequest(this.page).withSort(this.sort);
756-
757-
/*if (criteria != null) {
751+
StatementMapper mapper = dataAccessStrategy.getStatementMapper();
758752

759-
BoundCondition boundCondition = dataAccessStrategy.createCriteriaUpdateMapper()
760-
.getMappedCriteria(criteria, t);
761-
configurer.withWhere(boundCondition.getCondition()).withBindings(boundCondition.getBindings());
762-
} */
753+
StatementMapper.SelectSpec selectSpec = mapper.createSelect(this.table).withProjection(this.projectedFields)
754+
.withSort(this.sort).withPage(this.page);
763755

764-
});
756+
if (this.criteria != null) {
757+
selectSpec = selectSpec.withCriteria(this.criteria);
758+
}
765759

760+
PreparedOperation<?> operation = mapper.getMappedObject(selectSpec);
766761
return execute(operation, mappingFunction);
767762
}
768763

@@ -846,31 +841,22 @@ public FetchSpec<T> fetch() {
846841
private <R> FetchSpec<R> exchange(BiFunction<Row, RowMetadata, R> mappingFunction) {
847842

848843
List<String> columns;
844+
StatementMapper mapper = dataAccessStrategy.getStatementMapper().forType(this.typeToRead);
849845

850846
if (this.projectedFields.isEmpty()) {
851847
columns = dataAccessStrategy.getAllColumns(this.typeToRead);
852848
} else {
853849
columns = this.projectedFields;
854850
}
855851

856-
PreparedOperation<Select> operation = dataAccessStrategy.getStatements().select(this.table, columns,
857-
(table, configurer) -> {
858-
859-
Sort sortToUse;
860-
if (this.sort.isSorted()) {
861-
sortToUse = dataAccessStrategy.getMappedSort(this.sort, this.typeToRead);
862-
} else {
863-
sortToUse = this.sort;
864-
}
852+
StatementMapper.SelectSpec selectSpec = mapper.createSelect(this.table).withProjection(columns)
853+
.withPage(this.page).withSort(this.sort);
865854

866-
configurer.withPageRequest(this.page).withSort(sortToUse);
855+
if (this.criteria != null) {
856+
selectSpec = selectSpec.withCriteria(this.criteria);
857+
}
867858

868-
/*if (criteria != null) {
869-
BoundCondition boundCondition = dataAccessStrategy.createCriteriaUpdateMapper(this.typeToRead)
870-
.getMappedCriteria(criteria, table);
871-
configurer.withWhere(boundCondition.getCondition()).withBindings(boundCondition.getBindings());
872-
} */
873-
});
859+
PreparedOperation<?> operation = mapper.getMappedObject(selectSpec);
874860

875861
return execute(operation, mappingFunction);
876862
}

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

+14-51
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@
2626

2727
import org.springframework.dao.InvalidDataAccessResourceUsageException;
2828
import org.springframework.data.convert.CustomConversions.StoreConversions;
29-
import org.springframework.data.domain.Sort;
30-
import org.springframework.data.domain.Sort.Order;
3129
import org.springframework.data.mapping.context.MappingContext;
3230
import org.springframework.data.r2dbc.dialect.ArrayColumns;
3331
import org.springframework.data.r2dbc.dialect.BindMarkersFactory;
@@ -62,7 +60,6 @@ public class DefaultReactiveDataAccessStrategy implements ReactiveDataAccessStra
6260
private final R2dbcConverter converter;
6361
private final UpdateMapper updateMapper;
6462
private final MappingContext<RelationalPersistentEntity<?>, ? extends RelationalPersistentProperty> mappingContext;
65-
private final StatementFactory statements;
6663
private final StatementMapper statementMapper;
6764

6865
/**
@@ -127,11 +124,10 @@ public SelectRenderContext getSelect() {
127124
}
128125
};
129126

130-
this.statements = new DefaultStatementFactory(this.dialect, renderContext);
131-
this.statementMapper = new DefaultStatementMapper(dialect, renderContext, updateMapper, mappingContext);
127+
this.statementMapper = new DefaultStatementMapper(dialect, renderContext, this.updateMapper, this.mappingContext);
132128
}
133129

134-
/*
130+
/*
135131
* (non-Javadoc)
136132
* @see org.springframework.data.r2dbc.function.ReactiveDataAccessStrategy#getAllColumns(java.lang.Class)
137133
*/
@@ -152,7 +148,7 @@ public List<String> getAllColumns(Class<?> entityType) {
152148
return columnNames;
153149
}
154150

155-
/*
151+
/*
156152
* (non-Javadoc)
157153
* @see org.springframework.data.r2dbc.function.ReactiveDataAccessStrategy#getIdentifierColumns(java.lang.Class)
158154
*/
@@ -182,7 +178,7 @@ public OutboundRow getOutboundRow(Object object) {
182178

183179
OutboundRow row = new OutboundRow();
184180

185-
converter.write(object, row);
181+
this.converter.write(object, row);
186182

187183
RelationalPersistentEntity<?> entity = getRequiredPersistentEntity(ClassUtils.getUserClass(object));
188184

@@ -205,53 +201,25 @@ private boolean shouldConvertArrayValue(RelationalPersistentProperty property, S
205201

206202
private SettableValue getArrayValue(SettableValue value, RelationalPersistentProperty property) {
207203

208-
ArrayColumns arrayColumns = dialect.getArraySupport();
204+
ArrayColumns arrayColumns = this.dialect.getArraySupport();
209205

210206
if (!arrayColumns.isSupported()) {
211207

212208
throw new InvalidDataAccessResourceUsageException(
213-
"Dialect " + dialect.getClass().getName() + " does not support array columns");
209+
"Dialect " + this.dialect.getClass().getName() + " does not support array columns");
214210
}
215211

216-
return SettableValue.fromOrEmpty(converter.getArrayValue(arrayColumns, property, value.getValue()),
212+
return SettableValue.fromOrEmpty(this.converter.getArrayValue(arrayColumns, property, value.getValue()),
217213
property.getActualType());
218214
}
219215

220-
/*
221-
* (non-Javadoc)
222-
* @see org.springframework.data.r2dbc.function.ReactiveDataAccessStrategy#getMappedSort(java.lang.Class, org.springframework.data.domain.Sort)
223-
*/
224-
@Override
225-
public Sort getMappedSort(Sort sort, Class<?> typeToRead) {
226-
227-
RelationalPersistentEntity<?> entity = getPersistentEntity(typeToRead);
228-
if (entity == null) {
229-
return sort;
230-
}
231-
232-
List<Order> mappedOrder = new ArrayList<>();
233-
234-
for (Order order : sort) {
235-
236-
RelationalPersistentProperty persistentProperty = entity.getPersistentProperty(order.getProperty());
237-
if (persistentProperty == null) {
238-
mappedOrder.add(order);
239-
} else {
240-
mappedOrder
241-
.add(Order.by(persistentProperty.getColumnName()).with(order.getNullHandling()).with(order.getDirection()));
242-
}
243-
}
244-
245-
return Sort.by(mappedOrder);
246-
}
247-
248216
/*
249217
* (non-Javadoc)
250218
* @see org.springframework.data.r2dbc.function.ReactiveDataAccessStrategy#getRowMapper(java.lang.Class)
251219
*/
252220
@Override
253221
public <T> BiFunction<Row, RowMetadata, T> getRowMapper(Class<T> typeToRead) {
254-
return new EntityRowMapper<>(typeToRead, converter);
222+
return new EntityRowMapper<>(typeToRead, this.converter);
255223
}
256224

257225
/*
@@ -265,13 +233,8 @@ public String getTableName(Class<?> type) {
265233

266234
/*
267235
* (non-Javadoc)
268-
* @see org.springframework.data.r2dbc.function.ReactiveDataAccessStrategy#getStatements()
236+
* @see org.springframework.data.r2dbc.function.ReactiveDataAccessStrategy#getStatementMapper()
269237
*/
270-
@Override
271-
public StatementFactory getStatements() {
272-
return this.statements;
273-
}
274-
275238
@Override
276239
public StatementMapper getStatementMapper() {
277240
return this.statementMapper;
@@ -283,27 +246,27 @@ public StatementMapper getStatementMapper() {
283246
*/
284247
@Override
285248
public BindMarkersFactory getBindMarkersFactory() {
286-
return dialect.getBindMarkersFactory();
249+
return this.dialect.getBindMarkersFactory();
287250
}
288251

289252
/*
290253
* (non-Javadoc)
291254
* @see org.springframework.data.r2dbc.function.ReactiveDataAccessStrategy#getConverter()
292255
*/
293256
public R2dbcConverter getConverter() {
294-
return converter;
257+
return this.converter;
295258
}
296259

297260
public MappingContext<RelationalPersistentEntity<?>, ? extends RelationalPersistentProperty> getMappingContext() {
298-
return mappingContext;
261+
return this.mappingContext;
299262
}
300263

301264
private RelationalPersistentEntity<?> getRequiredPersistentEntity(Class<?> typeToRead) {
302-
return mappingContext.getRequiredPersistentEntity(typeToRead);
265+
return this.mappingContext.getRequiredPersistentEntity(typeToRead);
303266
}
304267

305268
@Nullable
306269
private RelationalPersistentEntity<?> getPersistentEntity(Class<?> typeToRead) {
307-
return mappingContext.getPersistentEntity(typeToRead);
270+
return this.mappingContext.getPersistentEntity(typeToRead);
308271
}
309272
}

0 commit comments

Comments
 (0)