28
28
import org .springframework .data .r2dbc .repository .query .PartTreeR2dbcQuery ;
29
29
import org .springframework .data .r2dbc .repository .query .R2dbcQueryMethod ;
30
30
import org .springframework .data .r2dbc .repository .query .StringBasedR2dbcQuery ;
31
+ import org .springframework .data .relational .core .dialect .AnsiDialect ;
32
+ import org .springframework .data .relational .core .dialect .Dialect ;
31
33
import org .springframework .data .relational .core .mapping .RelationalPersistentEntity ;
32
34
import org .springframework .data .relational .core .mapping .RelationalPersistentProperty ;
35
+ import org .springframework .data .relational .core .sql .SqlIdentifier ;
36
+ import org .springframework .data .relational .repository .query .QueryPreprocessor ;
33
37
import org .springframework .data .relational .repository .query .RelationalEntityInformation ;
38
+ import org .springframework .data .relational .repository .query .TableNameQueryPreprocessor ;
34
39
import org .springframework .data .relational .repository .support .MappingRelationalEntityInformation ;
35
40
import org .springframework .data .repository .core .NamedQueries ;
36
41
import org .springframework .data .repository .core .RepositoryInformation ;
@@ -61,6 +66,7 @@ public class R2dbcRepositoryFactory extends ReactiveRepositoryFactorySupport {
61
66
private final MappingContext <? extends RelationalPersistentEntity <?>, ? extends RelationalPersistentProperty > mappingContext ;
62
67
private final R2dbcConverter converter ;
63
68
private final R2dbcEntityOperations operations ;
69
+ private final Dialect dialect ;
64
70
65
71
/**
66
72
* Creates a new {@link R2dbcRepositoryFactory} given {@link DatabaseClient} and {@link MappingContext}.
@@ -69,6 +75,17 @@ public class R2dbcRepositoryFactory extends ReactiveRepositoryFactorySupport {
69
75
* @param dataAccessStrategy must not be {@literal null}.
70
76
*/
71
77
public R2dbcRepositoryFactory (DatabaseClient databaseClient , ReactiveDataAccessStrategy dataAccessStrategy ) {
78
+ this (databaseClient , dataAccessStrategy , AnsiDialect .INSTANCE );
79
+ }
80
+
81
+ /**
82
+ * Creates a new {@link R2dbcRepositoryFactory} given {@link DatabaseClient} and {@link MappingContext}.
83
+ *
84
+ * @param databaseClient must not be {@literal null}.
85
+ * @param dataAccessStrategy must not be {@literal null}.
86
+ */
87
+ public R2dbcRepositoryFactory (DatabaseClient databaseClient , ReactiveDataAccessStrategy dataAccessStrategy ,
88
+ Dialect dialect ) {
72
89
73
90
Assert .notNull (databaseClient , "DatabaseClient must not be null" );
74
91
Assert .notNull (dataAccessStrategy , "ReactiveDataAccessStrategy must not be null" );
@@ -78,6 +95,7 @@ public R2dbcRepositoryFactory(DatabaseClient databaseClient, ReactiveDataAccessS
78
95
this .converter = dataAccessStrategy .getConverter ();
79
96
this .mappingContext = this .converter .getMappingContext ();
80
97
this .operations = new R2dbcEntityTemplate (this .databaseClient , this .dataAccessStrategy );
98
+ this .dialect = dialect ;
81
99
setEvaluationContextProvider (ReactiveQueryMethodEvaluationContextProvider .DEFAULT );
82
100
}
83
101
@@ -88,6 +106,16 @@ public R2dbcRepositoryFactory(DatabaseClient databaseClient, ReactiveDataAccessS
88
106
* @since 1.1.3
89
107
*/
90
108
public R2dbcRepositoryFactory (R2dbcEntityOperations operations ) {
109
+ this (operations , AnsiDialect .INSTANCE );
110
+ }
111
+
112
+ /**
113
+ * Creates a new {@link R2dbcRepositoryFactory} given {@link R2dbcEntityOperations}.
114
+ *
115
+ * @param operations must not be {@literal null}.
116
+ * @since 1.1.3
117
+ */
118
+ public R2dbcRepositoryFactory (R2dbcEntityOperations operations , Dialect dialect ) {
91
119
92
120
Assert .notNull (operations , "R2dbcEntityOperations must not be null" );
93
121
@@ -96,6 +124,7 @@ public R2dbcRepositoryFactory(R2dbcEntityOperations operations) {
96
124
this .converter = dataAccessStrategy .getConverter ();
97
125
this .mappingContext = this .converter .getMappingContext ();
98
126
this .operations = operations ;
127
+ this .dialect = dialect ;
99
128
setEvaluationContextProvider (ReactiveQueryMethodEvaluationContextProvider .DEFAULT );
100
129
}
101
130
@@ -110,16 +139,15 @@ protected Object getTargetRepository(RepositoryInformation information) {
110
139
RelationalEntityInformation <?, ?> entityInformation = getEntityInformation (information .getDomainType (),
111
140
information );
112
141
113
- return getTargetRepositoryViaReflection (information , entityInformation ,
114
- operations , this .converter );
142
+ return getTargetRepositoryViaReflection (information , entityInformation , operations , this .converter );
115
143
}
116
144
117
145
@ Override
118
146
protected Optional <QueryLookupStrategy > getQueryLookupStrategy (@ Nullable Key key ,
119
147
QueryMethodEvaluationContextProvider evaluationContextProvider ) {
120
148
return Optional .of (new R2dbcQueryLookupStrategy (this .operations ,
121
149
(ReactiveQueryMethodEvaluationContextProvider ) evaluationContextProvider , this .converter ,
122
- this .dataAccessStrategy ));
150
+ this .dataAccessStrategy , this . dialect ));
123
151
}
124
152
125
153
public <T , ID > RelationalEntityInformation <T , ID > getEntityInformation (Class <T > domainClass ) {
@@ -146,35 +174,45 @@ private static class R2dbcQueryLookupStrategy implements QueryLookupStrategy {
146
174
private final ReactiveQueryMethodEvaluationContextProvider evaluationContextProvider ;
147
175
private final R2dbcConverter converter ;
148
176
private final ReactiveDataAccessStrategy dataAccessStrategy ;
177
+ private final Dialect dialect ;
149
178
private final ExpressionParser parser = new CachingExpressionParser (EXPRESSION_PARSER );
150
179
151
180
R2dbcQueryLookupStrategy (R2dbcEntityOperations entityOperations ,
152
181
ReactiveQueryMethodEvaluationContextProvider evaluationContextProvider , R2dbcConverter converter ,
153
- ReactiveDataAccessStrategy dataAccessStrategy ) {
182
+ ReactiveDataAccessStrategy dataAccessStrategy , Dialect dialect ) {
183
+
154
184
this .entityOperations = entityOperations ;
155
185
this .evaluationContextProvider = evaluationContextProvider ;
156
186
this .converter = converter ;
157
187
this .dataAccessStrategy = dataAccessStrategy ;
158
-
188
+ this . dialect = dialect ;
159
189
}
160
190
161
191
@ Override
162
192
public RepositoryQuery resolveQuery (Method method , RepositoryMetadata metadata , ProjectionFactory factory ,
163
193
NamedQueries namedQueries ) {
164
194
195
+ MappingContext <? extends RelationalPersistentEntity <?>, ? extends RelationalPersistentProperty > mappingContext = this .converter .getMappingContext ();
165
196
R2dbcQueryMethod queryMethod = new R2dbcQueryMethod (method , metadata , factory ,
166
- this . converter . getMappingContext () );
197
+ mappingContext );
167
198
String namedQueryName = queryMethod .getNamedQueryName ();
168
199
169
- if (namedQueries .hasQuery (namedQueryName )) {
170
- String namedQuery = namedQueries .getQuery (namedQueryName );
171
- return new StringBasedR2dbcQuery (namedQuery , queryMethod , this .entityOperations , this .converter ,
200
+ Class <?> domainType = metadata .getDomainType ();
201
+ RelationalPersistentEntity <?> entity = mappingContext .getRequiredPersistentEntity (domainType );
202
+ SqlIdentifier tableName = entity .getTableName ();
203
+ SqlIdentifier qualifiedTableName = entity .getQualifiedTableName ();
204
+
205
+ if (namedQueries .hasQuery (namedQueryName ) || queryMethod .hasAnnotatedQuery ()) {
206
+
207
+ QueryPreprocessor queryPreprocessor = new TableNameQueryPreprocessor (tableName , qualifiedTableName , dialect );
208
+
209
+ String query = namedQueries .hasQuery (namedQueryName ) ? namedQueries .getQuery (namedQueryName ) : queryMethod .getRequiredAnnotatedQuery ();
210
+ query = queryPreprocessor .transform (query );
211
+
212
+ return new StringBasedR2dbcQuery (query , queryMethod , this .entityOperations , this .converter ,
172
213
this .dataAccessStrategy ,
173
214
parser , this .evaluationContextProvider );
174
- } else if (queryMethod .hasAnnotatedQuery ()) {
175
- return new StringBasedR2dbcQuery (queryMethod , this .entityOperations , this .converter , this .dataAccessStrategy ,
176
- this .parser ,
177
- this .evaluationContextProvider );
215
+
178
216
} else {
179
217
return new PartTreeR2dbcQuery (queryMethod , this .entityOperations , this .converter , this .dataAccessStrategy );
180
218
}
0 commit comments