39
39
import org .springframework .jdbc .core .namedparam .MapSqlParameterSource ;
40
40
import org .springframework .lang .Nullable ;
41
41
import org .springframework .util .Assert ;
42
+ import org .springframework .util .CollectionUtils ;
42
43
43
44
/**
44
45
* Generates SQL statements to be used by {@link SimpleJdbcRepository}
@@ -507,29 +508,40 @@ private String createFindAllSql() {
507
508
}
508
509
509
510
private SelectBuilder .SelectWhere selectBuilder () {
510
- return selectBuilder (Collections .emptyList (), null );
511
+ return selectBuilder (Collections .emptyList (), Query . empty () );
511
512
}
512
513
513
-
514
514
private SelectBuilder .SelectWhere selectBuilder (Query query ) {
515
515
return selectBuilder (Collections .emptyList (), query );
516
516
}
517
517
518
518
private SelectBuilder .SelectWhere selectBuilder (Collection <SqlIdentifier > keyColumns ) {
519
- return selectBuilder (keyColumns , null );
519
+ return selectBuilder (keyColumns , Query . empty () );
520
520
}
521
521
522
- private SelectBuilder .SelectWhere selectBuilder (Collection <SqlIdentifier > keyColumns , @ Nullable Query query ) {
522
+ private SelectBuilder .SelectWhere selectBuilder (Collection <SqlIdentifier > keyColumns , Query query ) {
523
523
524
524
Table table = getTable ();
525
525
526
- Set <Expression > columnExpressions = new LinkedHashSet <>();
526
+ Projection projection = getProjection (keyColumns , query , table );
527
+ SelectBuilder .SelectAndFrom selectBuilder = StatementBuilder .select (projection .columns ());
528
+ SelectBuilder .SelectJoin baseSelect = selectBuilder .from (table );
529
+
530
+ for (Join join : projection .joins ()) {
531
+ baseSelect = baseSelect .leftOuterJoin (join .joinTable ).on (join .joinColumn ).equals (join .parentId );
532
+ }
533
+
534
+ return (SelectBuilder .SelectWhere ) baseSelect ;
535
+ }
527
536
528
- List < Join > joinTables = new ArrayList <>();
537
+ private Projection getProjection ( Collection < SqlIdentifier > keyColumns , Query query , Table table ) {
529
538
530
- if (query != null && !query .getColumns ().isEmpty ()) {
539
+ Set <Expression > columns = new LinkedHashSet <>();
540
+ Set <Join > joins = new LinkedHashSet <>();
541
+
542
+ if (!CollectionUtils .isEmpty (query .getColumns ())) {
531
543
for (SqlIdentifier columnName : query .getColumns ()) {
532
- columnExpressions .add (Column .create (columnName , table ));
544
+ columns .add (Column .create (columnName , table ));
533
545
}
534
546
} else {
535
547
for (PersistentPropertyPath <RelationalPersistentProperty > path : mappingContext
@@ -540,29 +552,30 @@ private SelectBuilder.SelectWhere selectBuilder(Collection<SqlIdentifier> keyCol
540
552
// add a join if necessary
541
553
Join join = getJoin (extPath );
542
554
if (join != null ) {
543
- joinTables .add (join );
555
+ joins .add (join );
544
556
}
545
557
546
558
Column column = getColumn (extPath );
547
559
if (column != null ) {
548
- columnExpressions .add (column );
560
+ columns .add (column );
549
561
}
550
562
}
551
563
}
552
564
553
-
554
565
for (SqlIdentifier keyColumn : keyColumns ) {
555
- columnExpressions .add (table .column (keyColumn ).as (keyColumn ));
566
+ columns .add (table .column (keyColumn ).as (keyColumn ));
556
567
}
557
568
558
- SelectBuilder .SelectAndFrom selectBuilder = StatementBuilder .select (columnExpressions );
559
- SelectBuilder .SelectJoin baseSelect = selectBuilder .from (table );
560
-
561
- for (Join join : joinTables ) {
562
- baseSelect = baseSelect .leftOuterJoin (join .joinTable ).on (join .joinColumn ).equals (join .parentId );
563
- }
569
+ return new Projection (columns , joins );
570
+ }
564
571
565
- return (SelectBuilder .SelectWhere ) baseSelect ;
572
+ /**
573
+ * Projection including its source joins.
574
+ *
575
+ * @param columns
576
+ * @param joins
577
+ */
578
+ record Projection (Set <Expression > columns , Set <Join > joins ) {
566
579
}
567
580
568
581
private SelectBuilder .SelectOrdered selectBuilder (Collection <SqlIdentifier > keyColumns , Sort sort ,
@@ -901,7 +914,6 @@ public String selectByQuery(Query query, MapSqlParameterSource parameterSource)
901
914
return render (select );
902
915
}
903
916
904
-
905
917
/**
906
918
* Constructs a single sql query that performs select based on the provided query and pagination information.
907
919
* Additional the bindings for the where clause are stored after execution into the <code>parameterSource</code>
0 commit comments