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.render;
17  
18  import java.util.Objects;
19  import java.util.Optional;
20  
21  import org.jspecify.annotations.Nullable;
22  import org.mybatis.dynamic.sql.SqlTable;
23  
24  public class TableAliasCalculatorWithParent implements TableAliasCalculator {
25      private final TableAliasCalculator parent;
26      private final TableAliasCalculator child;
27  
28      private TableAliasCalculatorWithParent(Builder builder) {
29          parent = Objects.requireNonNull(builder.parent);
30          child = Objects.requireNonNull(builder.child);
31      }
32  
33      @Override
34      public Optional<String> aliasForColumn(SqlTable table) {
35          Optional<String> answer = child.aliasForColumn(table);
36          if (answer.isPresent()) {
37              return answer;
38          }
39          return parent.aliasForColumn(table);
40      }
41  
42      @Override
43      public Optional<String> aliasForTable(SqlTable table) {
44          Optional<String> answer = child.aliasForTable(table);
45          if (answer.isPresent()) {
46              return answer;
47          }
48          return parent.aliasForTable(table);
49      }
50  
51      public static class Builder {
52          private @Nullable TableAliasCalculator parent;
53          private @Nullable TableAliasCalculator child;
54  
55          public Builder withParent(TableAliasCalculator parent) {
56              this.parent = parent;
57              return this;
58          }
59  
60          public Builder withChild(TableAliasCalculator child) {
61              this.child = child;
62              return this;
63          }
64  
65          public TableAliasCalculatorWithParent build() {
66              return new TableAliasCalculatorWithParent(this);
67          }
68      }
69  }