1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
37
38
39
40
41 @Deprecated
42 protected SqlTable(Supplier<String> tableNameSupplier) {
43 Objects.requireNonNull(tableNameSupplier);
44
45 this.nameSupplier = tableNameSupplier;
46 }
47
48
49
50
51
52
53
54
55 @Deprecated
56 protected SqlTable(Supplier<Optional<String>> schemaSupplier, String tableName) {
57 this(Optional::empty, schemaSupplier, tableName);
58 }
59
60
61
62
63
64
65
66
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;
96 }
97
98 private String composeSchemaAndTable(String schema, String tableName) {
99 return schema + "." + tableName;
100 }
101
102 private String composeCatalogSchemaAndTable(String catalog, String schema, String tableName) {
103 return catalog + "." + schema + "." + tableName;
104 }
105
106 public String tableNameAtRuntime() {
107 return nameSupplier.get();
108 }
109
110 public BasicColumn allColumns() {
111 return SqlColumn.of("*", this);
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 }