35
35
import org .springframework .data .relational .core .mapping .RelationalPersistentEntity ;
36
36
import org .springframework .data .relational .core .mapping .RelationalPersistentProperty ;
37
37
import org .springframework .data .relational .core .sql .*;
38
+ import org .springframework .data .relational .core .sql .render .RenderContext ;
38
39
import org .springframework .data .relational .core .sql .render .SqlRenderer ;
39
40
import org .springframework .data .relational .domain .Identifier ;
40
41
import org .springframework .data .util .Lazy ;
@@ -64,7 +65,7 @@ class SqlGenerator {
64
65
private final JdbcConverter converter ;
65
66
private final RelationalPersistentEntity <?> entity ;
66
67
private final MappingContext <RelationalPersistentEntity <?>, RelationalPersistentProperty > mappingContext ;
67
- private final IdentifierProcessing identifierProcessing ;
68
+ private final RenderContext renderContext ;
68
69
69
70
private final SqlContext sqlContext ;
70
71
private final SqlRenderer sqlRenderer ;
@@ -92,16 +93,15 @@ class SqlGenerator {
92
93
* @param entity must not be {@literal null}.
93
94
* @param dialect must not be {@literal null}.
94
95
*/
95
- SqlGenerator (RelationalMappingContext mappingContext , JdbcConverter converter , RelationalPersistentEntity <?> entity ,
96
- Dialect dialect ) {
96
+ SqlGenerator (RelationalMappingContext mappingContext , JdbcConverter converter ,RelationalPersistentEntity <?> entity , Dialect dialect ) {
97
97
98
98
this .mappingContext = mappingContext ;
99
99
this .converter = converter ;
100
100
this .entity = entity ;
101
- this .identifierProcessing = dialect .getIdentifierProcessing ();
102
- this .sqlContext = new SqlContext (entity , this .identifierProcessing );
101
+ this .sqlContext = new SqlContext (entity );
103
102
this .sqlRenderer = SqlRenderer .create (new RenderContextFactory (dialect ).createRenderContext ());
104
103
this .columns = new Columns (entity , mappingContext , converter );
104
+ this .renderContext = new RenderContextFactory (dialect ).createRenderContext ();
105
105
}
106
106
107
107
/**
@@ -125,10 +125,9 @@ private Condition getSubselectCondition(PersistentPropertyPathExtension path,
125
125
return rootCondition .apply (filterColumn );
126
126
}
127
127
128
- Table subSelectTable = SQL .table (parentPath .getTableName ().toSql (identifierProcessing ));
129
- Column idColumn = subSelectTable .column (parentPath .getIdColumnName ().toSql (identifierProcessing ));
130
- Column selectFilterColumn = subSelectTable
131
- .column (parentPath .getEffectiveIdColumnName ().toSql (identifierProcessing ));
128
+ Table subSelectTable = Table .create (parentPath .getTableName ());
129
+ Column idColumn = subSelectTable .column (parentPath .getIdColumnName ());
130
+ Column selectFilterColumn = subSelectTable .column (parentPath .getEffectiveIdColumnName ());
132
131
133
132
Condition innerCondition ;
134
133
@@ -151,7 +150,7 @@ private Condition getSubselectCondition(PersistentPropertyPathExtension path,
151
150
}
152
151
153
152
private BindMarker getBindMarker (SqlIdentifier columnName ) {
154
- return SQL .bindMarker (":" + parameterPattern .matcher (columnName . getReference ( identifierProcessing )).replaceAll ("" ));
153
+ return SQL .bindMarker (":" + parameterPattern .matcher (renderReference ( columnName )).replaceAll ("" ));
155
154
}
156
155
157
156
/**
@@ -210,21 +209,19 @@ String getFindAllByProperty(Identifier parentIdentifier, @Nullable SqlIdentifier
210
209
Assert .isTrue (keyColumn != null || !ordered ,
211
210
"If the SQL statement should be ordered a keyColumn to order by must be provided." );
212
211
212
+ Table table = getTable ();
213
+
213
214
SelectBuilder .SelectWhere builder = selectBuilder ( //
214
215
keyColumn == null //
215
216
? Collections .emptyList () //
216
- : Collections .singleton (keyColumn . toSql ( identifierProcessing ) ) //
217
+ : Collections .singleton (keyColumn ) //
217
218
);
218
219
219
- Table table = getTable ();
220
-
221
220
Condition condition = buildConditionForBackReference (parentIdentifier , table );
222
221
SelectBuilder .SelectWhereAndOr withWhereClause = builder .where (condition );
223
222
224
223
Select select = ordered //
225
- ? withWhereClause
226
- .orderBy (table .column (keyColumn .toSql (identifierProcessing )).as (keyColumn .toSql (identifierProcessing )))
227
- .build () //
224
+ ? withWhereClause .orderBy (table .column (keyColumn ).as (keyColumn )).build () //
228
225
: withWhereClause .build ();
229
226
230
227
return render (select );
@@ -235,8 +232,7 @@ private Condition buildConditionForBackReference(Identifier parentIdentifier, Ta
235
232
Condition condition = null ;
236
233
for (SqlIdentifier backReferenceColumn : parentIdentifier .toMap ().keySet ()) {
237
234
238
- Condition newCondition = table .column (backReferenceColumn .toSql (identifierProcessing ))
239
- .isEqualTo (getBindMarker (backReferenceColumn ));
235
+ Condition newCondition = table .column (backReferenceColumn ).isEqualTo (getBindMarker (backReferenceColumn ));
240
236
condition = condition == null ? newCondition : condition .and (newCondition );
241
237
}
242
238
@@ -372,7 +368,7 @@ private SelectBuilder.SelectWhere selectBuilder() {
372
368
return selectBuilder (Collections .emptyList ());
373
369
}
374
370
375
- private SelectBuilder .SelectWhere selectBuilder (Collection <String > keyColumns ) {
371
+ private SelectBuilder .SelectWhere selectBuilder (Collection <SqlIdentifier > keyColumns ) {
376
372
377
373
Table table = getTable ();
378
374
@@ -396,7 +392,7 @@ private SelectBuilder.SelectWhere selectBuilder(Collection<String> keyColumns) {
396
392
}
397
393
}
398
394
399
- for (String keyColumn : keyColumns ) {
395
+ for (SqlIdentifier keyColumn : keyColumns ) {
400
396
columnExpressions .add (table .column (keyColumn ).as (keyColumn ));
401
397
}
402
398
@@ -485,8 +481,8 @@ Join getJoin(PersistentPropertyPathExtension path) {
485
481
486
482
return new Join ( //
487
483
currentTable , //
488
- currentTable .column (path .getReverseColumnName (). toSql ( identifierProcessing ) ), //
489
- parentTable .column (idDefiningParentPath .getIdColumnName (). toSql ( identifierProcessing ) ) //
484
+ currentTable .column (path .getReverseColumnName ()), //
485
+ parentTable .column (idDefiningParentPath .getIdColumnName ()) //
490
486
);
491
487
}
492
488
@@ -526,14 +522,14 @@ private String createInsertSql(Set<SqlIdentifier> additionalColumns) {
526
522
527
523
Table table = getTable ();
528
524
529
- Set <SqlIdentifier > columnNamesForInsert = new TreeSet <>(Comparator .comparing (id -> id . toSql ( identifierProcessing ) ));
525
+ Set <SqlIdentifier > columnNamesForInsert = new TreeSet <>(Comparator .comparing (SqlIdentifier :: getReference ));
530
526
columnNamesForInsert .addAll (columns .getInsertableColumns ());
531
527
columnNamesForInsert .addAll (additionalColumns );
532
528
533
529
InsertBuilder .InsertIntoColumnsAndValuesWithBuild insert = Insert .builder ().into (table );
534
530
535
531
for (SqlIdentifier cn : columnNamesForInsert ) {
536
- insert = insert .column (table .column (cn . toSql ( identifierProcessing ) ));
532
+ insert = insert .column (table .column (cn ));
537
533
}
538
534
539
535
InsertBuilder .InsertValuesWithBuild insertWithValues = null ;
@@ -551,8 +547,7 @@ private String createUpdateSql() {
551
547
private String createUpdateWithVersionSql () {
552
548
553
549
Update update = createBaseUpdate () //
554
- .and (getVersionColumn ()
555
- .isEqualTo (SQL .bindMarker (":" + VERSION_SQL_PARAMETER .getReference (identifierProcessing )))) //
550
+ .and (getVersionColumn ().isEqualTo (SQL .bindMarker (":" + renderReference (VERSION_SQL_PARAMETER )))) //
556
551
.build ();
557
552
558
553
return render (update );
@@ -565,7 +560,7 @@ private UpdateBuilder.UpdateWhereAndOr createBaseUpdate() {
565
560
List <AssignValue > assignments = columns .getUpdateableColumns () //
566
561
.stream () //
567
562
.map (columnName -> Assignments .value ( //
568
- table .column (columnName . toSql ( identifierProcessing ) ), //
563
+ table .column (columnName ), //
569
564
getBindMarker (columnName ))) //
570
565
.collect (Collectors .toList ());
571
566
@@ -582,28 +577,27 @@ private String createDeleteSql() {
582
577
private String createDeleteByIdAndVersionSql () {
583
578
584
579
Delete delete = createBaseDeleteById (getTable ()) //
585
- .and (getVersionColumn ()
586
- .isEqualTo (SQL .bindMarker (":" + VERSION_SQL_PARAMETER .getReference (identifierProcessing )))) //
580
+ .and (getVersionColumn ().isEqualTo (SQL .bindMarker (":" + renderReference (VERSION_SQL_PARAMETER )))) //
587
581
.build ();
588
582
589
583
return render (delete );
590
584
}
591
585
592
586
private DeleteBuilder .DeleteWhereAndOr createBaseDeleteById (Table table ) {
593
587
return Delete .builder ().from (table )
594
- .where (getIdColumn ().isEqualTo (SQL .bindMarker (":" + ID_SQL_PARAMETER . getReference ( identifierProcessing ))));
588
+ .where (getIdColumn ().isEqualTo (SQL .bindMarker (":" + renderReference ( ID_SQL_PARAMETER ))));
595
589
}
596
590
597
591
private String createDeleteByPathAndCriteria (PersistentPropertyPathExtension path ,
598
592
Function <Column , Condition > rootCondition ) {
599
593
600
- Table table = SQL . table (path .getTableName (). toSql ( identifierProcessing ));
594
+ Table table = Table . create (path .getTableName ());
601
595
602
596
DeleteBuilder .DeleteWhere builder = Delete .builder () //
603
597
.from (table );
604
598
Delete delete ;
605
599
606
- Column filterColumn = table .column (path .getReverseColumnName (). toSql ( identifierProcessing ) );
600
+ Column filterColumn = table .column (path .getReverseColumnName ());
607
601
608
602
if (path .getLength () == 1 ) {
609
603
@@ -659,6 +653,10 @@ private Column getVersionColumn() {
659
653
return sqlContext .getVersionColumn ();
660
654
}
661
655
656
+ private String renderReference (SqlIdentifier identifier ) {
657
+ return identifier .getReference (renderContext .getIdentifierProcessing ());
658
+ }
659
+
662
660
private List <OrderByField > extractOrderByFields (Sort sort ) {
663
661
return sort .stream ()
664
662
.map (order -> OrderByField .from (Column .create (order .getProperty (), this .getTable ()), order .getDirection ()))
0 commit comments