Skip to content

Commit 75cfb1c

Browse files
committed
removed function indirection for condition construction
1 parent e8fb5e4 commit 75cfb1c

File tree

1 file changed

+29
-47
lines changed
  • spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/convert

1 file changed

+29
-47
lines changed

spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/convert/SqlGenerator.java

Lines changed: 29 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
import org.springframework.data.relational.core.query.CriteriaDefinition;
3434
import org.springframework.data.relational.core.query.Query;
3535
import org.springframework.data.relational.core.sql.*;
36-
import org.springframework.data.relational.core.sql.render.RenderContext;
3736
import org.springframework.data.relational.core.sql.render.SqlRenderer;
3837
import org.springframework.data.util.Lazy;
3938
import org.springframework.data.util.Pair;
@@ -94,10 +93,6 @@ class SqlGenerator {
9493
private final QueryMapper queryMapper;
9594
private final Dialect dialect;
9695

97-
private final Function<Map<AggregatePath, Column>, Condition> inCondition;
98-
private final Function<Map<AggregatePath, Column>, Condition> equalityCondition;
99-
private final Function<Map<AggregatePath, Column>, Condition> notNullCondition;
100-
10196
/**
10297
* Create a new {@link SqlGenerator} given {@link RelationalMappingContext} and {@link RelationalPersistentEntity}.
10398
*
@@ -116,11 +111,6 @@ class SqlGenerator {
116111
this.columns = new Columns(entity, mappingContext, converter);
117112
this.queryMapper = new QueryMapper(converter);
118113
this.dialect = dialect;
119-
120-
inCondition = inCondition();
121-
equalityCondition = equalityCondition();
122-
notNullCondition = isNotNullCondition();
123-
124114
}
125115

126116
/**
@@ -208,7 +198,7 @@ private Condition getSubselectCondition(AggregatePath path,
208198
}
209199

210200
private Expression toExpression(Map<AggregatePath, Column> columnsMap) {
211-
return TupleExpression.maybeWrap(new ArrayList<>(columnsMap.values()));
201+
return TupleExpression.maybeWrap(new ArrayList<>(columnsMap.values()));
212202
}
213203

214204
private BindMarker getBindMarker(SqlIdentifier columnName) {
@@ -454,7 +444,7 @@ String createDeleteAllSql(@Nullable PersistentPropertyPath<RelationalPersistentP
454444
return render(deleteAll.build());
455445
}
456446

457-
return createDeleteByPathAndCriteria(mappingContext.getAggregatePath(path), notNullCondition);
447+
return createDeleteByPathAndCriteria(mappingContext.getAggregatePath(path), this::isNotNullCondition);
458448
}
459449

460450
/**
@@ -465,7 +455,7 @@ String createDeleteAllSql(@Nullable PersistentPropertyPath<RelationalPersistentP
465455
* @return the statement as a {@link String}. Guaranteed to be not {@literal null}.
466456
*/
467457
String createDeleteByPath(PersistentPropertyPath<RelationalPersistentProperty> path) {
468-
return createDeleteByPathAndCriteria(mappingContext.getAggregatePath(path), equalityCondition);
458+
return createDeleteByPathAndCriteria(mappingContext.getAggregatePath(path), this::equalityCondition);
469459
}
470460

471461
/**
@@ -476,63 +466,55 @@ String createDeleteByPath(PersistentPropertyPath<RelationalPersistentProperty> p
476466
* @return the statement as a {@link String}. Guaranteed to be not {@literal null}.
477467
*/
478468
String createDeleteInByPath(PersistentPropertyPath<RelationalPersistentProperty> path) {
479-
return createDeleteByPathAndCriteria(mappingContext.getAggregatePath(path), inCondition);
469+
return createDeleteByPathAndCriteria(mappingContext.getAggregatePath(path), this::inCondition);
480470
}
481471

482472
/**
483-
* Constructs a function for constructing a where condition. The where condition will be of the form
484-
* {@literal <columns> IN :bind-marker}
473+
* Constructs a where condition. The where condition will be of the form {@literal <columns> IN :bind-marker}
485474
*/
486-
private Function<Map<AggregatePath, Column>, Condition> inCondition() {
487-
488-
return columnMap -> {
475+
private Condition inCondition(Map<AggregatePath, Column> columnMap) {
489476

490-
List<Column> columns = List.copyOf(columnMap.values());
477+
List<Column> columns = List.copyOf(columnMap.values());
491478

492-
if (columns.size() == 1) {
493-
return Conditions.in(columns.get(0), getBindMarker(IDS_SQL_PARAMETER));
494-
}
495-
return Conditions.in(TupleExpression.create(columns), getBindMarker(IDS_SQL_PARAMETER));
496-
};
479+
if (columns.size() == 1) {
480+
return Conditions.in(columns.get(0), getBindMarker(IDS_SQL_PARAMETER));
481+
}
482+
return Conditions.in(TupleExpression.create(columns), getBindMarker(IDS_SQL_PARAMETER));
497483
}
498484

499485
/**
500-
* Constructs a function for constructing a where. The where condition will be of the form
486+
* Constructs a where-condition. The where condition will be of the form
501487
* {@literal <column-a> = :bind-marker-a AND <column-b> = :bind-marker-b ...}
502488
*/
503-
private Function<Map<AggregatePath, Column>, Condition> equalityCondition() {
489+
private Condition equalityCondition(Map<AggregatePath, Column> columnMap) {
504490

505491
AggregatePath.ColumnInfos idColumnInfos = mappingContext.getAggregatePath(entity).getTableInfo().idColumnInfos();
506492

507-
return columnMap -> {
508-
509-
Condition result = null;
510-
for (Map.Entry<AggregatePath, Column> entry : columnMap.entrySet()) {
511-
BindMarker bindMarker = getBindMarker(idColumnInfos.get(entry.getKey()).name());
512-
Comparison singleCondition = entry.getValue().isEqualTo(bindMarker);
493+
Condition result = null;
494+
for (Map.Entry<AggregatePath, Column> entry : columnMap.entrySet()) {
495+
BindMarker bindMarker = getBindMarker(idColumnInfos.get(entry.getKey()).name());
496+
Comparison singleCondition = entry.getValue().isEqualTo(bindMarker);
513497

514-
result = result == null ? singleCondition : result.and(singleCondition);
515-
}
516-
return result;
517-
};
498+
result = result == null ? singleCondition : result.and(singleCondition);
499+
}
500+
Assert.state(result != null, "We need at least one condition");
501+
return result;
518502
}
519503

520504
/**
521505
* Constructs a function for constructing where a condition. The where condition will be of the form
522506
* {@literal <column-a> IS NOT NULL AND <column-b> IS NOT NULL ... }
523507
*/
524-
private Function<Map<AggregatePath, Column>, Condition> isNotNullCondition() {
525-
526-
return columnMap -> {
508+
private Condition isNotNullCondition(Map<AggregatePath, Column> columnMap) {
527509

528-
Condition result = null;
529-
for (Column column : columnMap.values()) {
530-
Condition singleCondition = column.isNotNull();
510+
Condition result = null;
511+
for (Column column : columnMap.values()) {
512+
Condition singleCondition = column.isNotNull();
531513

532-
result = result == null ? singleCondition : result.and(singleCondition);
533-
}
534-
return result;
535-
};
514+
result = result == null ? singleCondition : result.and(singleCondition);
515+
}
516+
Assert.state(result != null, "We need at least one condition");
517+
return result;
536518
}
537519

538520
private String createFindOneSql() {

0 commit comments

Comments
 (0)