4
4
import org .springframework .data .r2dbc .convert .R2dbcConverter ;
5
5
import org .springframework .data .r2dbc .core .DatabaseClient ;
6
6
import org .springframework .data .r2dbc .core .ReactiveDataAccessStrategy ;
7
- import org .springframework .data .relational .core .sql .Column ;
8
7
import org .springframework .data .relational .core .sql .Condition ;
8
+ import org .springframework .data .relational .core .sql .Expression ;
9
9
import org .springframework .data .relational .core .sql .SelectBuilder .SelectFromAndJoin ;
10
10
import org .springframework .data .relational .core .sql .StatementBuilder ;
11
11
import org .springframework .data .relational .core .sql .Table ;
12
+ import org .springframework .data .relational .core .sql .render .RenderContext ;
12
13
import org .springframework .data .relational .core .sql .render .SqlRenderer ;
13
14
import org .springframework .data .relational .repository .query .RelationalEntityMetadata ;
14
15
import org .springframework .data .repository .query .parser .AbstractQueryCreator ;
@@ -34,9 +35,9 @@ public class R2dbcQueryCreator extends AbstractQueryCreator<BindableQuery, Condi
34
35
* Creates new instance of this class with the given {@link PartTree}, {@link ReactiveDataAccessStrategy},
35
36
* {@link RelationalEntityMetadata} and {@link ParameterMetadataProvider}.
36
37
*
37
- * @param tree part tree (must not be {@literal null})
38
- * @param dataAccessStrategy data access strategy (must not be {@literal null})
39
- * @param entityMetadata relational entity metadata (must not be {@literal null})
38
+ * @param tree part tree (must not be {@literal null})
39
+ * @param dataAccessStrategy data access strategy (must not be {@literal null})
40
+ * @param entityMetadata relational entity metadata (must not be {@literal null})
40
41
* @param parameterMetadataProvider parameter metadata provider (must not be {@literal null})
41
42
*/
42
43
public R2dbcQueryCreator (PartTree tree ,
@@ -76,15 +77,15 @@ protected Condition or(Condition condition, Condition s1) {
76
77
* Creates {@link BindableQuery} applying the given {@link Condition} and {@link Sort} definition.
77
78
*
78
79
* @param condition condition to be applied to query
79
- * @param sort sort option to be applied to query (must not be {@literal null})
80
+ * @param sort sort option to be applied to query (must not be {@literal null})
80
81
* @return new instance of {@link BindableQuery}
81
82
*/
82
83
@ Override
83
84
protected BindableQuery complete (Condition condition , Sort sort ) {
84
85
Table fromTable = Table .create (entityMetadata .getTableName ());
85
- List <Column > columns = fromTable .columns (dataAccessStrategy .getAllColumns (entityMetadata .getJavaType ()));
86
+ List <? extends Expression > selectExpressions = getSelectionExpressions (fromTable );
87
+ SelectFromAndJoin selectBuilder = StatementBuilder .select (selectExpressions ).from (fromTable );
86
88
87
- SelectFromAndJoin selectBuilder = StatementBuilder .select (columns ).from (fromTable );
88
89
if (tree .isExistsProjection ()) {
89
90
selectBuilder .limit (1 );
90
91
}
@@ -93,7 +94,8 @@ protected BindableQuery complete(Condition condition, Sort sort) {
93
94
selectBuilder .where (condition );
94
95
}
95
96
96
- SqlRenderer sqlRenderer = SqlRenderer .create ();
97
+ RenderContext renderContext = dataAccessStrategy .getStatementMapper ().getRenderContext ();
98
+ SqlRenderer sqlRenderer = renderContext == null ? SqlRenderer .create () : SqlRenderer .create (renderContext );
97
99
String sql = sqlRenderer .render (selectBuilder .build ());
98
100
99
101
return new BindableQuery () {
@@ -108,4 +110,11 @@ public String get() {
108
110
}
109
111
};
110
112
}
113
+
114
+ private List <? extends Expression > getSelectionExpressions (Table fromTable ) {
115
+ if (tree .isExistsProjection ()) {
116
+ return fromTable .columns (dataAccessStrategy .getIdentifierColumns (entityMetadata .getJavaType ()));
117
+ }
118
+ return fromTable .columns (dataAccessStrategy .getAllColumns (entityMetadata .getJavaType ()));
119
+ }
111
120
}
0 commit comments