Skip to content

Commit 046a707

Browse files
committed
introduce and use new toColumnList method
1 parent ae0546b commit 046a707

File tree

6 files changed

+48
-36
lines changed

6 files changed

+48
-36
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ private Condition getSubselectCondition(AggregatePath path,
186186
innerCondition = getSubselectCondition(parentPath, conditionFunction, selectFilterColumns);
187187
}
188188

189-
List<Expression> idColumns = parentPathTableInfo.idColumnInfos().toList(ci -> subSelectTable.column(ci.name()));
189+
List<Column> idColumns = parentPathTableInfo.idColumnInfos().toColumnList(subSelectTable);
190190

191191
Select select = Select.builder() //
192192
.select(idColumns) //

spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/query/JdbcDeleteQueryCreator.java

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,6 @@
1515
*/
1616
package org.springframework.data.jdbc.repository.query;
1717

18-
import java.util.ArrayList;
19-
import java.util.List;
20-
2118
import org.springframework.data.domain.Sort;
2219
import org.springframework.data.jdbc.core.convert.JdbcConverter;
2320
import org.springframework.data.jdbc.core.convert.QueryMapper;
@@ -29,9 +26,17 @@
2926
import org.springframework.data.relational.core.mapping.RelationalPersistentEntity;
3027
import org.springframework.data.relational.core.mapping.RelationalPersistentProperty;
3128
import org.springframework.data.relational.core.query.Criteria;
32-
import org.springframework.data.relational.core.sql.*;
29+
import org.springframework.data.relational.core.sql.Column;
30+
import org.springframework.data.relational.core.sql.Condition;
31+
import org.springframework.data.relational.core.sql.Conditions;
32+
import org.springframework.data.relational.core.sql.Delete;
3333
import org.springframework.data.relational.core.sql.DeleteBuilder.DeleteWhere;
34+
import org.springframework.data.relational.core.sql.Expression;
35+
import org.springframework.data.relational.core.sql.Expressions;
36+
import org.springframework.data.relational.core.sql.Select;
3437
import org.springframework.data.relational.core.sql.SelectBuilder.SelectWhere;
38+
import org.springframework.data.relational.core.sql.StatementBuilder;
39+
import org.springframework.data.relational.core.sql.Table;
3540
import org.springframework.data.relational.core.sql.render.SqlRenderer;
3641
import org.springframework.data.relational.repository.query.RelationalEntityMetadata;
3742
import org.springframework.data.relational.repository.query.RelationalParameterAccessor;
@@ -41,6 +46,9 @@
4146
import org.springframework.lang.Nullable;
4247
import org.springframework.util.Assert;
4348

49+
import java.util.ArrayList;
50+
import java.util.List;
51+
4452
/**
4553
* Implementation of {@link RelationalQueryCreator} that creates {@link List} of deletion {@link ParametrizedQuery} from
4654
* a {@link PartTree}.
@@ -93,7 +101,7 @@ protected List<ParametrizedQuery> complete(@Nullable Criteria criteria, Sort sor
93101
: queryMapper.getMappedObject(parameterSource, criteria, table, entity);
94102

95103
List<Column> idColumns = context.getAggregatePath(entity).getTableInfo().idColumnInfos()
96-
.toList(ci -> table.column(ci.name()));
104+
.toColumnList(table);
97105

98106
// create select criteria query for subselect
99107
SelectWhere selectBuilder = StatementBuilder.select(idColumns).from(table);
@@ -139,13 +147,13 @@ private void deleteRelations(List<Delete> deleteChain, RelationalPersistentEntit
139147
Table table = sqlContext.getUnaliasedTable(aggregatePath);
140148

141149
List<Column> reverseColumns = aggregatePath.getTableInfo().backReferenceColumnInfos()
142-
.toList(ci -> table.column(ci.name()));
150+
.toColumnList(table);
143151
Expression expression = Expressions.of(reverseColumns);
144152

145153
Condition inCondition = Conditions.in(expression, parentSelect);
146154

147155
List<Column> parentIdColumns = aggregatePath.getIdDefiningParentPath().getTableInfo().idColumnInfos()
148-
.toList(ci -> table.column(ci.name()));
156+
.toColumnList(table);
149157

150158
Select select = StatementBuilder.select( //
151159
parentIdColumns //

spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/query/JdbcQueryCreator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -325,9 +325,9 @@ Join getJoin(SqlContext sqlContext, AggregatePath path) {
325325
AggregatePath idDefiningParentPath = path.getIdDefiningParentPath();
326326
Table parentTable = sqlContext.getTable(idDefiningParentPath);
327327

328-
List<Column> reverseColumns = path.getTableInfo().backReferenceColumnInfos().toList(ci -> currentTable.column(ci.name()));
328+
List<Column> reverseColumns = path.getTableInfo().backReferenceColumnInfos().toColumnList(currentTable);
329329
List<Column> idColumns = idDefiningParentPath.getTableInfo().idColumnInfos()
330-
.toList(ci -> parentTable.column(ci.name()));
330+
.toColumnList(parentTable);
331331
return new Join( //
332332
currentTable, //
333333
reverseColumns, //

spring-data-relational/src/main/java/org/springframework/data/relational/core/mapping/AggregatePath.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,17 @@
3131
import org.springframework.data.mapping.PersistentProperty;
3232
import org.springframework.data.mapping.PersistentPropertyPath;
3333
import org.springframework.data.mapping.PropertyHandler;
34+
import org.springframework.data.relational.core.sql.Column;
3435
import org.springframework.data.relational.core.sql.SqlIdentifier;
36+
import org.springframework.data.relational.core.sql.Table;
3537
import org.springframework.lang.NonNull;
3638
import org.springframework.lang.Nullable;
3739
import org.springframework.util.Assert;
3840

3941
/**
4042
* Represents a path within an aggregate starting from the aggregate root. The path can be iterated from the leaf to its
4143
* root.
42-
*
44+
* <p>
4345
* It implements {@link Comparable} so that collections of {@code AggregatePath} instances can be sorted in a consistent way.
4446
*
4547
* @author Jens Schauder
@@ -547,6 +549,10 @@ public <T> List<T> toList(Function<ColumnInfo, T> mapper) {
547549
return columnInfos.values().stream().map(mapper).toList();
548550
}
549551

552+
public List<Column> toColumnList(Table table) {
553+
return toList(columnInfo -> table.column(columnInfo.name));
554+
}
555+
550556
/**
551557
* Performs a {@link Stream#reduce(Object, BiFunction, BinaryOperator)} on {@link ColumnInfo} and
552558
* {@link AggregatePath} to reduce the results into a single {@code T} return value.

spring-data-relational/src/main/java/org/springframework/data/relational/core/sql/AnalyticFunction.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public AnalyticFunction partitionBy(Expression... partitionBy) {
6464
* previously present.
6565
* @since 3.5
6666
*/
67-
public AnalyticFunction partitionBy(Collection<Expression> partitionBy) {
67+
public AnalyticFunction partitionBy(Collection<? extends Expression> partitionBy) {
6868
return partitionBy(partitionBy.toArray(new Expression[0]));
6969
}
7070

@@ -87,7 +87,7 @@ public AnalyticFunction orderBy(OrderByField... orderBy) {
8787
* previously present.
8888
* @since 3.5
8989
*/
90-
public AnalyticFunction orderBy(Collection<Expression> orderBy) {
90+
public AnalyticFunction orderBy(Collection<? extends Expression> orderBy) {
9191
return orderBy(orderBy.toArray(new Expression[0]));
9292
}
9393

spring-data-relational/src/main/java/org/springframework/data/relational/core/sqlgeneration/SingleQuerySqlGenerator.java

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ private static List<Expression> getSelectList(QueryMeta queryMeta, List<QueryMet
113113
}
114114

115115
private InlineQuery createMainSelect(List<Expression> columns, AggregatePath rootPath, InlineQuery rootQuery,
116-
List<QueryMeta> inlineQueries) {
116+
List<QueryMeta> inlineQueries) {
117117

118118
SelectBuilder.SelectJoin select = StatementBuilder.select(columns).from(rootQuery);
119119
select = applyJoins(rootPath, inlineQueries, select);
@@ -127,7 +127,7 @@ private InlineQuery createMainSelect(List<Expression> columns, AggregatePath roo
127127
*
128128
* @param paths the paths to consider.
129129
* @return a {@link Map} that contains all the inline queries indexed by the path to the entity that gets loaded by
130-
* the subquery.
130+
* the subquery.
131131
*/
132132
private List<QueryMeta> createInlineQueries(PersistentPropertyPaths<?, RelationalPersistentProperty> paths) {
133133

@@ -149,7 +149,7 @@ private List<QueryMeta> createInlineQueries(PersistentPropertyPaths<?, Relationa
149149
* that are not unique across tables and also the generated SQL doesn't contain quotes and funny column names, making
150150
* them easier to understand and also potentially shorter.
151151
*
152-
* @param basePath the path for which to create the inline query.
152+
* @param basePath the path for which to create the inline query.
153153
* @param condition a condition that is to be applied to the query. May be {@literal null}.
154154
* @return an inline query for the given path.
155155
*/
@@ -169,11 +169,8 @@ private QueryMeta createInlineQuery(AggregatePath basePath, @Nullable Condition
169169
String rowCountAlias = aliases.getRowCountAlias(basePath);
170170
Expression count = basePath.isRoot() ? new AliasedExpression(SQL.literalOf(1), rowCountAlias) //
171171
: AnalyticFunction.create("count", Expressions.just("*")) //
172-
.partitionBy( //
173-
basePath.getTableInfo().backReferenceColumnInfos().toList( //
174-
ci -> table.column(ci.name()) //
175-
) //
176-
).as(rowCountAlias);
172+
.partitionBy(basePath.getTableInfo().backReferenceColumnInfos().toColumnList(table) //
173+
).as(rowCountAlias);
177174
columns.add(count);
178175

179176
String backReferenceAlias = null;
@@ -241,25 +238,25 @@ private String getIdentifierProperty(List<AggregatePath> paths) {
241238
}
242239

243240
private static AnalyticFunction createRowNumberExpression(AggregatePath basePath, Table table,
244-
String rowNumberAlias) {
241+
String rowNumberAlias) {
245242
AggregatePath.ColumnInfos reverseColumnInfos = basePath.getTableInfo().backReferenceColumnInfos();
246243
return AnalyticFunction.create("row_number") //
247-
.partitionBy(reverseColumnInfos.toList(ci -> table.column(ci.name()))) //
248-
.orderBy(reverseColumnInfos.toList(ci -> table.column(ci.name()))) //
244+
.partitionBy(reverseColumnInfos.toColumnList(table)) //
245+
.orderBy(reverseColumnInfos.toColumnList(table)) //
249246
.as(rowNumberAlias);
250247
}
251248

252249
/**
253250
* Adds joins to a select.
254251
*
255-
* @param rootPath the AggregatePath that gets selected by the select in question.
252+
* @param rootPath the AggregatePath that gets selected by the select in question.
256253
* @param inlineQueries all the inline queries to added as joins as returned by
257-
* {@link #createInlineQueries(PersistentPropertyPaths)}
258-
* @param select the select to modify.
254+
* {@link #createInlineQueries(PersistentPropertyPaths)}
255+
* @param select the select to modify.
259256
* @return the original select but with added joins
260257
*/
261258
private SelectBuilder.SelectJoin applyJoins(AggregatePath rootPath, List<QueryMeta> inlineQueries,
262-
SelectBuilder.SelectJoin select) {
259+
SelectBuilder.SelectJoin select) {
263260

264261
RelationalPersistentProperty rootIdProperty = rootPath.getRequiredIdProperty();
265262
AggregatePath rootIdPath = rootPath.append(rootIdProperty);
@@ -286,12 +283,12 @@ private SelectBuilder.SelectJoin applyJoins(AggregatePath rootPath, List<QueryMe
286283
* </ol>
287284
*
288285
* @param inlineQueries all in the inline queries for all the children, as returned by
289-
* {@link #createInlineQueries(PersistentPropertyPaths)}
290-
* @param select the select to which the where clause gets added.
286+
* {@link #createInlineQueries(PersistentPropertyPaths)}
287+
* @param select the select to which the where clause gets added.
291288
* @return the modified select.
292289
*/
293290
private SelectBuilder.SelectOrdered applyWhereCondition(List<QueryMeta> inlineQueries,
294-
SelectBuilder.SelectJoin select) {
291+
SelectBuilder.SelectJoin select) {
295292

296293
SelectBuilder.SelectWhere selectWhere = (SelectBuilder.SelectWhere) select;
297294

@@ -389,8 +386,8 @@ public AliasFactory getAliasFactory() {
389386
* </table>
390387
*
391388
* @param rowNumberAlias the alias of the rownumber column of the subselect under consideration. This determines if
392-
* the other value is replaced by null or not.
393-
* @param alias the column potentially to be replaced by null
389+
* the other value is replaced by null or not.
390+
* @param alias the column potentially to be replaced by null
394391
* @return a SQL expression.
395392
*/
396393
private static Expression filteredColumnExpression(String rowNumberAlias, String alias) {
@@ -422,11 +419,12 @@ private static SimpleFunction greatest(List<Expression> expressions) {
422419
}
423420

424421
record QueryMeta(AggregatePath basePath, InlineQuery inlineQuery, Collection<Expression> simpleColumns,
425-
Collection<Expression> selectableExpressions, Expression id, Expression backReference, Expression key,
426-
Expression rowNumber, Expression rowCount) {
422+
Collection<Expression> selectableExpressions, Expression id, Expression backReference,
423+
Expression key,
424+
Expression rowNumber, Expression rowCount) {
427425

428426
static QueryMeta of(AggregatePath basePath, InlineQuery inlineQuery, Collection<Expression> simpleColumns,
429-
Expression id, Expression backReference, Expression key, Expression rowNumber, Expression rowCount) {
427+
Expression id, Expression backReference, Expression key, Expression rowNumber, Expression rowCount) {
430428

431429
List<Expression> selectableExpressions = new ArrayList<>(simpleColumns);
432430
selectableExpressions.add(rowNumber);

0 commit comments

Comments
 (0)