1
1
package org .springframework .data .r2dbc .repository .query ;
2
2
3
3
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 ;
4
12
import org .springframework .data .repository .query .parser .AbstractQueryCreator ;
5
13
import org .springframework .data .repository .query .parser .Part ;
6
14
import org .springframework .data .repository .query .parser .PartTree ;
15
+ import org .springframework .util .Assert ;
7
16
8
17
import java .util .Iterator ;
18
+ import java .util .List ;
9
19
import java .util .concurrent .locks .Condition ;
10
20
11
21
/**
14
24
* @author Roman Chigvintsev
15
25
*/
16
26
class R2dbcQueryCreator extends AbstractQueryCreator <BindableQuery , Condition > {
27
+ private final PartTree tree ;
28
+ private final RelationalEntityMetadata <?> entityMetadata ;
29
+ private final ReactiveDataAccessStrategy dataAccessStrategy ;
30
+
17
31
/**
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}.
19
34
*
20
35
* @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})
21
38
*/
22
- public R2dbcQueryCreator (PartTree tree ) {
39
+ public R2dbcQueryCreator (PartTree tree ,
40
+ RelationalEntityMetadata <?> entityMetadata ,
41
+ ReactiveDataAccessStrategy dataAccessStrategy ) {
23
42
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 ;
24
50
}
25
51
26
52
@ Override
@@ -38,8 +64,36 @@ protected Condition or(Condition condition, Condition s1) {
38
64
throw new UnsupportedOperationException ();
39
65
}
40
66
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
+ */
41
74
@ Override
42
75
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
+ };
44
98
}
45
99
}
0 commit comments