Skip to content

Commit 46bae29

Browse files
committed
spring-projects#282 - Generate simplest SQL without condition in R2dbcQueryCreator
1 parent e266d0d commit 46bae29

File tree

2 files changed

+58
-8
lines changed

2 files changed

+58
-8
lines changed

Diff for: src/main/java/org/springframework/data/r2dbc/repository/query/PartTreeR2dbcQuery.java

+1-5
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import org.springframework.data.repository.query.parser.Part;
1111
import org.springframework.data.repository.query.parser.PartTree;
1212
import org.springframework.data.util.Streamable;
13-
import org.springframework.util.Assert;
1413

1514
/**
1615
* An {@link AbstractR2dbcQuery} implementation based on a {@link PartTree}.
@@ -21,7 +20,6 @@
2120
* @author Roman Chigvintsev
2221
*/
2322
public class PartTreeR2dbcQuery extends AbstractR2dbcQuery {
24-
private final ReactiveDataAccessStrategy dataAccessStrategy;
2523
private final RelationalParameters parameters;
2624
private final PartTree tree;
2725
private final R2dbcQueryCreator queryCreator;
@@ -39,16 +37,14 @@ public PartTreeR2dbcQuery(R2dbcQueryMethod method,
3937
R2dbcConverter converter,
4038
ReactiveDataAccessStrategy dataAccessStrategy) {
4139
super(method, databaseClient, converter);
42-
Assert.notNull(dataAccessStrategy, "Data access strategy must not be null");
43-
this.dataAccessStrategy = dataAccessStrategy;
4440
this.parameters = method.getParameters();
4541

4642
RelationalEntityMetadata<?> entityMetadata = method.getEntityInformation();
4743

4844
try {
4945
this.tree = new PartTree(method.getName(), entityMetadata.getJavaType());
5046
validate(tree, parameters, method.getName());
51-
this.queryCreator = new R2dbcQueryCreator(tree);
47+
this.queryCreator = new R2dbcQueryCreator(tree, entityMetadata, dataAccessStrategy);
5248
} catch (Exception e) {
5349
String message = String.format("Failed to create query for method %s! %s", method, e.getMessage());
5450
throw new IllegalArgumentException(message, e);

Diff for: src/main/java/org/springframework/data/r2dbc/repository/query/R2dbcQueryCreator.java

+57-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
11
package org.springframework.data.r2dbc.repository.query;
22

33
import org.springframework.data.domain.Sort;
4+
import org.springframework.data.r2dbc.core.DatabaseClient;
5+
import org.springframework.data.r2dbc.core.ReactiveDataAccessStrategy;
6+
import org.springframework.data.relational.core.sql.Column;
7+
import org.springframework.data.relational.core.sql.SelectBuilder.SelectFromAndJoin;
8+
import org.springframework.data.relational.core.sql.StatementBuilder;
9+
import org.springframework.data.relational.core.sql.Table;
10+
import org.springframework.data.relational.core.sql.render.SqlRenderer;
11+
import org.springframework.data.relational.repository.query.RelationalEntityMetadata;
412
import org.springframework.data.repository.query.parser.AbstractQueryCreator;
513
import org.springframework.data.repository.query.parser.Part;
614
import org.springframework.data.repository.query.parser.PartTree;
15+
import org.springframework.util.Assert;
716

817
import java.util.Iterator;
18+
import java.util.List;
919
import java.util.concurrent.locks.Condition;
1020

1121
/**
@@ -14,13 +24,29 @@
1424
* @author Roman Chigvintsev
1525
*/
1626
class R2dbcQueryCreator extends AbstractQueryCreator<BindableQuery, Condition> {
27+
private final PartTree tree;
28+
private final RelationalEntityMetadata<?> entityMetadata;
29+
private final ReactiveDataAccessStrategy dataAccessStrategy;
30+
1731
/**
18-
* Creates new instance of this class with the given {@link PartTree}.
32+
* Creates new instance of this class with the given {@link PartTree}, {@link RelationalEntityMetadata} and
33+
* {@link ReactiveDataAccessStrategy}.
1934
*
2035
* @param tree part tree (must not be {@literal null})
36+
* @param entityMetadata relational entity metadata (must not be {@literal null})
37+
* @param dataAccessStrategy data access strategy (must not be {@literal null})
2138
*/
22-
public R2dbcQueryCreator(PartTree tree) {
39+
public R2dbcQueryCreator(PartTree tree,
40+
RelationalEntityMetadata<?> entityMetadata,
41+
ReactiveDataAccessStrategy dataAccessStrategy) {
2342
super(tree);
43+
this.tree = tree;
44+
45+
Assert.notNull(entityMetadata, "Relational entity metadata must not be null");
46+
Assert.notNull(dataAccessStrategy, "Data access strategy must not be null");
47+
48+
this.entityMetadata = entityMetadata;
49+
this.dataAccessStrategy = dataAccessStrategy;
2450
}
2551

2652
@Override
@@ -38,8 +64,36 @@ protected Condition or(Condition condition, Condition s1) {
3864
throw new UnsupportedOperationException();
3965
}
4066

67+
/**
68+
* Creates {@link BindableQuery} applying the given {@link Condition} and {@link Sort} definition.
69+
*
70+
* @param condition condition to be applied to query
71+
* @param sort sort option to be applied to query (must not be {@literal null})
72+
* @return new instance of {@link BindableQuery}
73+
*/
4174
@Override
4275
protected BindableQuery complete(Condition condition, Sort sort) {
43-
throw new UnsupportedOperationException();
76+
Table fromTable = Table.create(entityMetadata.getTableName());
77+
List<Column> columns = fromTable.columns(dataAccessStrategy.getAllColumns(entityMetadata.getJavaType()));
78+
79+
SelectFromAndJoin selectBuilder = StatementBuilder.select(columns).from(fromTable);
80+
if (tree.isExistsProjection()) {
81+
selectBuilder.limit(1);
82+
}
83+
84+
SqlRenderer sqlRenderer = SqlRenderer.create();
85+
String sql = sqlRenderer.render(selectBuilder.build());
86+
87+
return new BindableQuery() {
88+
@Override
89+
public <T extends DatabaseClient.BindSpec<T>> T bind(T bindSpec) {
90+
return bindSpec;
91+
}
92+
93+
@Override
94+
public String get() {
95+
return sql;
96+
}
97+
};
4498
}
4599
}

0 commit comments

Comments
 (0)