18
18
import java .util .ArrayList ;
19
19
import java .util .List ;
20
20
21
+ import org .jetbrains .annotations .NotNull ;
21
22
import org .springframework .data .mapping .context .MappingContext ;
22
23
import org .springframework .data .r2dbc .convert .R2dbcConverter ;
23
24
import org .springframework .data .r2dbc .dialect .R2dbcDialect ;
24
25
import org .springframework .data .r2dbc .query .BoundAssignments ;
25
26
import org .springframework .data .r2dbc .query .BoundCondition ;
26
27
import org .springframework .data .r2dbc .query .UpdateMapper ;
27
28
import org .springframework .data .relational .core .dialect .RenderContextFactory ;
29
+ import org .springframework .data .relational .core .mapping .RelationalMappingContext ;
28
30
import org .springframework .data .relational .core .mapping .RelationalPersistentEntity ;
29
31
import org .springframework .data .relational .core .mapping .RelationalPersistentProperty ;
30
32
import org .springframework .data .relational .core .query .CriteriaDefinition ;
@@ -53,7 +55,7 @@ class DefaultStatementMapper implements StatementMapper {
53
55
private final RenderContext renderContext ;
54
56
private final UpdateMapper updateMapper ;
55
57
private final MappingContext <? extends RelationalPersistentEntity <?>, ? extends RelationalPersistentProperty > mappingContext ;
56
-
58
+ private boolean forceQuote ;
57
59
DefaultStatementMapper (R2dbcDialect dialect , R2dbcConverter converter ) {
58
60
59
61
RenderContextFactory factory = new RenderContextFactory (dialect );
@@ -62,6 +64,9 @@ class DefaultStatementMapper implements StatementMapper {
62
64
this .renderContext = factory .createRenderContext ();
63
65
this .updateMapper = new UpdateMapper (dialect , converter );
64
66
this .mappingContext = converter .getMappingContext ();
67
+ if (mappingContext instanceof RelationalMappingContext relationalMappingContext ){
68
+ forceQuote = relationalMappingContext .isForceQuote ();
69
+ }
65
70
}
66
71
67
72
DefaultStatementMapper (R2dbcDialect dialect , RenderContext renderContext , UpdateMapper updateMapper ,
@@ -70,6 +75,9 @@ class DefaultStatementMapper implements StatementMapper {
70
75
this .renderContext = renderContext ;
71
76
this .updateMapper = updateMapper ;
72
77
this .mappingContext = mappingContext ;
78
+ if (mappingContext instanceof RelationalMappingContext relationalMappingContext ){
79
+ forceQuote = relationalMappingContext .isForceQuote ();
80
+ }
73
81
}
74
82
75
83
@ Override
@@ -90,7 +98,8 @@ public PreparedOperation<?> getMappedObject(SelectSpec selectSpec) {
90
98
private PreparedOperation <Select > getMappedObject (SelectSpec selectSpec ,
91
99
@ Nullable RelationalPersistentEntity <?> entity ) {
92
100
93
- Table table = selectSpec .getTable ();
101
+ String tableName = selectSpec .getTable ().getName ().getReference ();
102
+ Table table = getTable (tableName );
94
103
SelectBuilder .SelectAndFrom selectAndFrom = StatementBuilder .select (getSelectList (selectSpec , entity ));
95
104
96
105
if (selectSpec .isDistinct ()) {
@@ -158,7 +167,7 @@ private PreparedOperation<Insert> getMappedObject(InsertSpec insertSpec,
158
167
@ Nullable RelationalPersistentEntity <?> entity ) {
159
168
160
169
BindMarkers bindMarkers = this .dialect .getBindMarkersFactory ().create ();
161
- Table table = Table . create ( toSql ( insertSpec .getTable ()));
170
+ Table table = getTable ( insertSpec .getTable (). getReference ( ));
162
171
163
172
BoundAssignments boundAssignments = this .updateMapper .getMappedObject (bindMarkers , insertSpec .getAssignments (),
164
173
table , entity );
@@ -191,7 +200,7 @@ private PreparedOperation<Update> getMappedObject(UpdateSpec updateSpec,
191
200
@ Nullable RelationalPersistentEntity <?> entity ) {
192
201
193
202
BindMarkers bindMarkers = this .dialect .getBindMarkersFactory ().create ();
194
- Table table = Table . create ( toSql ( updateSpec .getTable ()));
203
+ Table table = getTable ( updateSpec .getTable (). getReference ( ));
195
204
196
205
if (updateSpec .getUpdate () == null || updateSpec .getUpdate ().getAssignments ().isEmpty ()) {
197
206
throw new IllegalArgumentException ("UPDATE contains no assignments" );
@@ -210,7 +219,6 @@ private PreparedOperation<Update> getMappedObject(UpdateSpec updateSpec,
210
219
211
220
CriteriaDefinition criteria = updateSpec .getCriteria ();
212
221
if (criteria != null && !criteria .isEmpty ()) {
213
-
214
222
BoundCondition boundCondition = this .updateMapper .getMappedObject (bindMarkers , criteria , table , entity );
215
223
216
224
bindings = bindings .and (boundCondition .getBindings ());
@@ -222,6 +230,10 @@ private PreparedOperation<Update> getMappedObject(UpdateSpec updateSpec,
222
230
return new DefaultPreparedOperation <>(update , this .renderContext , bindings );
223
231
}
224
232
233
+ private Table getTable (String tableName ) {
234
+ return forceQuote ? Table .create (SqlIdentifier .quoted (tableName )) : Table .create (SqlIdentifier .unquoted (tableName ));
235
+ }
236
+
225
237
@ Override
226
238
public PreparedOperation <Delete > getMappedObject (DeleteSpec deleteSpec ) {
227
239
return getMappedObject (deleteSpec , null );
@@ -236,7 +248,7 @@ private PreparedOperation<Delete> getMappedObject(DeleteSpec deleteSpec,
236
248
@ Nullable RelationalPersistentEntity <?> entity ) {
237
249
238
250
BindMarkers bindMarkers = this .dialect .getBindMarkersFactory ().create ();
239
- Table table = Table . create ( toSql ( deleteSpec .getTable ()));
251
+ Table table = getTable ( deleteSpec .getTable (). getReference ( ));
240
252
241
253
DeleteBuilder .DeleteWhere deleteBuilder = StatementBuilder .delete (table );
242
254
0 commit comments