View Javadoc
1   /*
2    *    Copyright 2016-2025 the original author or authors.
3    *
4    *    Licensed under the Apache License, Version 2.0 (the "License");
5    *    you may not use this file except in compliance with the License.
6    *    You may obtain a copy of the License at
7    *
8    *       https://www.apache.org/licenses/LICENSE-2.0
9    *
10   *    Unless required by applicable law or agreed to in writing, software
11   *    distributed under the License is distributed on an "AS IS" BASIS,
12   *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *    See the License for the specific language governing permissions and
14   *    limitations under the License.
15   */
16  package org.mybatis.dynamic.sql.select.render;
17  
18  import java.util.Objects;
19  import java.util.stream.Collectors;
20  
21  import org.jspecify.annotations.Nullable;
22  import org.mybatis.dynamic.sql.exception.InvalidSqlException;
23  import org.mybatis.dynamic.sql.render.RenderingContext;
24  import org.mybatis.dynamic.sql.select.join.JoinModel;
25  import org.mybatis.dynamic.sql.select.join.JoinSpecification;
26  import org.mybatis.dynamic.sql.util.FragmentAndParameters;
27  import org.mybatis.dynamic.sql.util.FragmentCollector;
28  import org.mybatis.dynamic.sql.util.Messages;
29  
30  public class JoinRenderer {
31      private final JoinModel joinModel;
32      private final TableExpressionRenderer tableExpressionRenderer;
33      private final RenderingContext renderingContext;
34  
35      private JoinRenderer(Builder builder) {
36          joinModel = Objects.requireNonNull(builder.joinModel);
37          tableExpressionRenderer = Objects.requireNonNull(builder.tableExpressionRenderer);
38          renderingContext = Objects.requireNonNull(builder.renderingContext);
39      }
40  
41      public FragmentAndParameters render() {
42          return joinModel.joinSpecifications()
43                  .map(this::renderJoinSpecification)
44                  .collect(FragmentCollector.collect())
45                  .toFragmentAndParameters(Collectors.joining(" ")); //$NON-NLS-1$
46      }
47  
48      private FragmentAndParameters renderJoinSpecification(JoinSpecification joinSpecification) {
49          FragmentCollector fc = new FragmentCollector();
50          fc.add(FragmentAndParameters.fromFragment(joinSpecification.joinType().type()));
51          fc.add(joinSpecification.table().accept(tableExpressionRenderer));
52          fc.add(JoinSpecificationRenderer
53                  .withJoinSpecification(joinSpecification)
54                  .withRenderingContext(renderingContext)
55                  .build()
56                  .render()
57                  .orElseThrow(() -> new InvalidSqlException(Messages.getString("ERROR.46")))); //$NON-NLS-1$
58  
59          return fc.toFragmentAndParameters(Collectors.joining(" ")); //$NON-NLS-1$
60      }
61  
62      public static Builder withJoinModel(JoinModel joinModel) {
63          return new Builder().withJoinModel(joinModel);
64      }
65  
66      public static class Builder {
67          private @Nullable JoinModel joinModel;
68          private @Nullable TableExpressionRenderer tableExpressionRenderer;
69          private @Nullable RenderingContext renderingContext;
70  
71          public Builder withJoinModel(JoinModel joinModel) {
72              this.joinModel = joinModel;
73              return this;
74          }
75  
76          public Builder withTableExpressionRenderer(TableExpressionRenderer tableExpressionRenderer) {
77              this.tableExpressionRenderer = tableExpressionRenderer;
78              return this;
79          }
80  
81          public Builder withRenderingContext(RenderingContext renderingContext) {
82              this.renderingContext = renderingContext;
83              return this;
84          }
85  
86          public JoinRenderer build() {
87              return new JoinRenderer(this);
88          }
89      }
90  }