View Javadoc
1   /*
2    *    Copyright 2016-2026 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.dsl;
17  
18  import java.util.ArrayList;
19  import java.util.HashMap;
20  import java.util.List;
21  import java.util.Map;
22  
23  import org.jspecify.annotations.Nullable;
24  import org.mybatis.dynamic.sql.SqlTable;
25  import org.mybatis.dynamic.sql.TableExpression;
26  import org.mybatis.dynamic.sql.exception.DuplicateTableAliasException;
27  import org.mybatis.dynamic.sql.select.SelectModel;
28  import org.mybatis.dynamic.sql.select.SubQuery;
29  import org.mybatis.dynamic.sql.select.join.JoinModel;
30  import org.mybatis.dynamic.sql.util.Buildable;
31  import org.mybatis.dynamic.sql.util.Validator;
32  
33  /**
34   * Abstract base class for query DSL implementations.
35   *
36   * <p>This class does not implement any specific interface. That is an intentional choice to allow for flexibility
37   * in composing a DSL based on the interfaces that DSL needs to implement. This class is simply a landing ground
38   * for common functionality that can be shared across multiple query DSL implementations.</p>
39   */
40  public abstract class AbstractQueryingDSL {
41      private static final String ERROR_27 = "ERROR.27"; //$NON-NLS-1$
42  
43      private final Map<SqlTable, String> tableAliases = new HashMap<>();
44      private @Nullable TableExpression table;
45      private final List<AbstractJoinSupport<?, ?>> joinSpecifications = new ArrayList<>();
46  
47      protected void addTableAlias(SqlTable table, String tableAlias) {
48          if (tableAliases.containsKey(table)) {
49              throw new DuplicateTableAliasException(table, tableAlias, tableAliases.get(table));
50          }
51  
52          tableAliases.put(table, tableAlias);
53      }
54  
55      private SubQuery buildSubQuery(Buildable<SelectModel> selectModel) {
56          return new SubQuery.Builder()
57                  .withSelectModel(selectModel.build())
58                  .build();
59      }
60  
61      private SubQuery buildSubQuery(Buildable<SelectModel> selectModel, @Nullable String alias) {
62          return new SubQuery.Builder()
63                  .withSelectModel(selectModel.build())
64                  .withAlias(alias)
65                  .build();
66      }
67  
68      protected Map<SqlTable, String> tableAliases() {
69          return tableAliases;
70      }
71  
72      protected TableExpression table() {
73          Validator.assertTrue(table != null, ERROR_27);
74          return table;
75      }
76  
77      protected void setTable(SqlTable table) {
78          Validator.assertNull(this.table, ERROR_27);
79          this.table = table;
80      }
81  
82      protected void setTable(SqlTable table, String tableAlias) {
83          Validator.assertNull(this.table, ERROR_27);
84          this.table = table;
85          addTableAlias(table, tableAlias);
86      }
87  
88      protected void setTable(Buildable<SelectModel> select) {
89          Validator.assertNull(this.table, ERROR_27);
90          table = buildSubQuery(select);
91      }
92  
93      protected void setTable(Buildable<SelectModel> select, String tableAlias) {
94          Validator.assertNull(this.table, ERROR_27);
95          table = buildSubQuery(select, tableAlias);
96      }
97  
98      protected void addJoinSpecification(AbstractJoinSupport<?, ?> joinSpecification) {
99          joinSpecifications.add(joinSpecification);
100     }
101 
102     protected @Nullable JoinModel buildJoinModel() {
103         if (joinSpecifications.isEmpty()) {
104             return null;
105         }
106 
107         return JoinModel.of(joinSpecifications.stream()
108                 .map(AbstractJoinSupport::toJoinSpecification)
109                 .toList());
110     }
111 }