View Javadoc
1   /*
2    *    Copyright 2016-2024 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;
17  
18  import java.sql.JDBCType;
19  import java.util.Objects;
20  import java.util.Optional;
21  import java.util.function.Supplier;
22  
23  import org.jetbrains.annotations.NotNull;
24  
25  public class SqlTable implements TableExpression {
26  
27      protected Supplier<String> nameSupplier;
28  
29      protected SqlTable(String tableName) {
30          Objects.requireNonNull(tableName);
31  
32          this.nameSupplier = () -> tableName;
33      }
34  
35      /**
36       * Creates an SqlTable whose name can be changed at runtime.
37       *
38       * @param tableNameSupplier table name supplier
39       * @deprecated please use {@link AliasableSqlTable} if you need to change the table name at runtime
40       */
41      @Deprecated
42      protected SqlTable(Supplier<String> tableNameSupplier) {
43          Objects.requireNonNull(tableNameSupplier);
44  
45          this.nameSupplier = tableNameSupplier;
46      }
47  
48      /**
49       * Creates an SqlTable whose name can be changed at runtime.
50       *
51       * @param schemaSupplier schema supplier
52       * @param tableName table name
53       * @deprecated please use {@link AliasableSqlTable} if you need to change the table name at runtime
54       */
55      @Deprecated
56      protected SqlTable(Supplier<Optional<String>> schemaSupplier, String tableName) {
57          this(Optional::empty, schemaSupplier, tableName);
58      }
59  
60      /**
61       * Creates an SqlTable whose name can be changed at runtime.
62       *
63       * @param catalogSupplier catalog supplier
64       * @param schemaSupplier schema supplier
65       * @param tableName table name
66       * @deprecated please use {@link AliasableSqlTable} if you need to change the table name at runtime
67       */
68      @Deprecated
69      protected SqlTable(Supplier<Optional<String>> catalogSupplier, Supplier<Optional<String>> schemaSupplier,
70              String tableName) {
71          Objects.requireNonNull(catalogSupplier);
72          Objects.requireNonNull(schemaSupplier);
73          Objects.requireNonNull(tableName);
74  
75          this.nameSupplier = () -> compose(catalogSupplier, schemaSupplier, tableName);
76      }
77  
78      private String compose(Supplier<Optional<String>> catalogSupplier, Supplier<Optional<String>> schemaSupplier,
79              String tableName) {
80          return catalogSupplier.get().map(c -> compose(c, schemaSupplier, tableName))
81                  .orElseGet(() -> compose(schemaSupplier, tableName));
82      }
83  
84      private String compose(String catalog, Supplier<Optional<String>> schemaSupplier, String tableName) {
85          return schemaSupplier.get().map(s -> composeCatalogSchemaAndTable(catalog, s, tableName))
86                  .orElseGet(() -> composeCatalogAndTable(catalog, tableName));
87      }
88  
89      private String compose(Supplier<Optional<String>> schemaSupplier, String tableName) {
90          return schemaSupplier.get().map(s -> composeSchemaAndTable(s, tableName))
91                  .orElse(tableName);
92      }
93  
94      private String composeCatalogAndTable(String catalog, String tableName) {
95          return catalog + ".." + tableName; //$NON-NLS-1$
96      }
97  
98      private String composeSchemaAndTable(String schema, String tableName) {
99          return schema + "." + tableName; //$NON-NLS-1$
100     }
101 
102     private String composeCatalogSchemaAndTable(String catalog, String schema, String tableName) {
103         return catalog + "." + schema + "." + tableName; //$NON-NLS-1$ //$NON-NLS-2$
104     }
105 
106     public String tableNameAtRuntime() {
107         return nameSupplier.get();
108     }
109 
110     public BasicColumn allColumns() {
111         return SqlColumn.of("*", this); //$NON-NLS-1$
112     }
113 
114     @NotNull
115     public <T> SqlColumn<T> column(String name) {
116         return SqlColumn.of(name, this);
117     }
118 
119     @NotNull
120     public <T> SqlColumn<T> column(String name, JDBCType jdbcType) {
121         return SqlColumn.of(name, this, jdbcType);
122     }
123 
124     @NotNull
125     public <T> SqlColumn<T> column(String name, JDBCType jdbcType, String typeHandler) {
126         SqlColumn<T> column = SqlColumn.of(name, this, jdbcType);
127         return column.withTypeHandler(typeHandler);
128     }
129 
130     @Override
131     public <R> R accept(TableExpressionVisitor<R> visitor) {
132         return visitor.visit(this);
133     }
134 
135     public Optional<String> tableAlias() {
136         return Optional.empty();
137     }
138 
139     public static SqlTable of(String name) {
140         return new SqlTable(name);
141     }
142 }