3939import org .springframework .jdbc .core .namedparam .MapSqlParameterSource ;
4040import org .springframework .lang .Nullable ;
4141import org .springframework .util .Assert ;
42+ import org .springframework .util .CollectionUtils ;
4243
4344/**
4445 * Generates SQL statements to be used by {@link SimpleJdbcRepository}
@@ -507,29 +508,40 @@ private String createFindAllSql() {
507508 }
508509
509510 private SelectBuilder .SelectWhere selectBuilder () {
510- return selectBuilder (Collections .emptyList (), null );
511+ return selectBuilder (Collections .emptyList (), Query . empty () );
511512 }
512513
513-
514514 private SelectBuilder .SelectWhere selectBuilder (Query query ) {
515515 return selectBuilder (Collections .emptyList (), query );
516516 }
517517
518518 private SelectBuilder .SelectWhere selectBuilder (Collection <SqlIdentifier > keyColumns ) {
519- return selectBuilder (keyColumns , null );
519+ return selectBuilder (keyColumns , Query . empty () );
520520 }
521521
522- private SelectBuilder .SelectWhere selectBuilder (Collection <SqlIdentifier > keyColumns , @ Nullable Query query ) {
522+ private SelectBuilder .SelectWhere selectBuilder (Collection <SqlIdentifier > keyColumns , Query query ) {
523523
524524 Table table = getTable ();
525525
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+ }
527536
528- List < Join > joinTables = new ArrayList <>();
537+ private Projection getProjection ( Collection < SqlIdentifier > keyColumns , Query query , Table table ) {
529538
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 ())) {
531543 for (SqlIdentifier columnName : query .getColumns ()) {
532- columnExpressions .add (Column .create (columnName , table ));
544+ columns .add (Column .create (columnName , table ));
533545 }
534546 } else {
535547 for (PersistentPropertyPath <RelationalPersistentProperty > path : mappingContext
@@ -540,29 +552,30 @@ private SelectBuilder.SelectWhere selectBuilder(Collection<SqlIdentifier> keyCol
540552 // add a join if necessary
541553 Join join = getJoin (extPath );
542554 if (join != null ) {
543- joinTables .add (join );
555+ joins .add (join );
544556 }
545557
546558 Column column = getColumn (extPath );
547559 if (column != null ) {
548- columnExpressions .add (column );
560+ columns .add (column );
549561 }
550562 }
551563 }
552564
553-
554565 for (SqlIdentifier keyColumn : keyColumns ) {
555- columnExpressions .add (table .column (keyColumn ).as (keyColumn ));
566+ columns .add (table .column (keyColumn ).as (keyColumn ));
556567 }
557568
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+ }
564571
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 ) {
566579 }
567580
568581 private SelectBuilder .SelectOrdered selectBuilder (Collection <SqlIdentifier > keyColumns , Sort sort ,
@@ -901,7 +914,6 @@ public String selectByQuery(Query query, MapSqlParameterSource parameterSource)
901914 return render (select );
902915 }
903916
904-
905917 /**
906918 * Constructs a single sql query that performs select based on the provided query and pagination information.
907919 * Additional the bindings for the where clause are stored after execution into the <code>parameterSource</code>
0 commit comments