1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.mybatis.generator.api;
17
18 import static org.mybatis.generator.internal.util.EqualsUtil.areEqual;
19 import static org.mybatis.generator.internal.util.HashCodeUtil.SEED;
20 import static org.mybatis.generator.internal.util.HashCodeUtil.hash;
21 import static org.mybatis.generator.internal.util.JavaBeansUtil.getCamelCaseString;
22 import static org.mybatis.generator.internal.util.StringUtility.composeFullyQualifiedTableName;
23 import static org.mybatis.generator.internal.util.StringUtility.stringHasValue;
24
25 import org.mybatis.generator.config.Context;
26
27
28
29
30
31
32 public class FullyQualifiedTable {
33
34
35 private String introspectedCatalog;
36
37
38 private String introspectedSchema;
39
40
41 private String introspectedTableName;
42
43
44 private String runtimeCatalog;
45
46
47 private String runtimeSchema;
48
49
50 private String runtimeTableName;
51
52
53 private String domainObjectName;
54
55
56 private String domainObjectSubPackage;
57
58
59 private String alias;
60
61
62 private boolean ignoreQualifiersAtRuntime;
63
64
65 private String beginningDelimiter;
66
67
68 private String endingDelimiter;
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108 public FullyQualifiedTable(String introspectedCatalog,
109 String introspectedSchema, String introspectedTableName,
110 String domainObjectName, String alias,
111 boolean ignoreQualifiersAtRuntime, String runtimeCatalog,
112 String runtimeSchema, String runtimeTableName,
113 boolean delimitIdentifiers, Context context) {
114 super();
115 this.introspectedCatalog = introspectedCatalog;
116 this.introspectedSchema = introspectedSchema;
117 this.introspectedTableName = introspectedTableName;
118 this.ignoreQualifiersAtRuntime = ignoreQualifiersAtRuntime;
119 this.runtimeCatalog = runtimeCatalog;
120 this.runtimeSchema = runtimeSchema;
121 this.runtimeTableName = runtimeTableName;
122
123 if (stringHasValue(domainObjectName)) {
124 int index = domainObjectName.lastIndexOf('.');
125 if (index == -1) {
126 this.domainObjectName = domainObjectName;
127 } else {
128 this.domainObjectName = domainObjectName.substring(index + 1);
129 this.domainObjectSubPackage = domainObjectName.substring(0, index);
130 }
131 }
132
133 if (alias == null) {
134 this.alias = null;
135 } else {
136 this.alias = alias.trim();
137 }
138
139 beginningDelimiter = delimitIdentifiers ? context
140 .getBeginningDelimiter() : "";
141 endingDelimiter = delimitIdentifiers ? context.getEndingDelimiter()
142 : "";
143 }
144
145
146
147
148
149
150 public String getIntrospectedCatalog() {
151 return introspectedCatalog;
152 }
153
154
155
156
157
158
159 public String getIntrospectedSchema() {
160 return introspectedSchema;
161 }
162
163
164
165
166
167
168 public String getIntrospectedTableName() {
169 return introspectedTableName;
170 }
171
172
173
174
175
176
177 public String getFullyQualifiedTableNameAtRuntime() {
178 StringBuilder localCatalog = new StringBuilder();
179 if (!ignoreQualifiersAtRuntime) {
180 if (stringHasValue(runtimeCatalog)) {
181 localCatalog.append(runtimeCatalog);
182 } else if (stringHasValue(introspectedCatalog)) {
183 localCatalog.append(introspectedCatalog);
184 }
185 }
186 if (localCatalog.length() > 0) {
187 addDelimiters(localCatalog);
188 }
189
190 StringBuilder localSchema = new StringBuilder();
191 if (!ignoreQualifiersAtRuntime) {
192 if (stringHasValue(runtimeSchema)) {
193 localSchema.append(runtimeSchema);
194 } else if (stringHasValue(introspectedSchema)) {
195 localSchema.append(introspectedSchema);
196 }
197 }
198 if (localSchema.length() > 0) {
199 addDelimiters(localSchema);
200 }
201
202 StringBuilder localTableName = new StringBuilder();
203 if (stringHasValue(runtimeTableName)) {
204 localTableName.append(runtimeTableName);
205 } else {
206 localTableName.append(introspectedTableName);
207 }
208 addDelimiters(localTableName);
209
210 return composeFullyQualifiedTableName(localCatalog
211 .toString(), localSchema.toString(), localTableName.toString(),
212 '.');
213 }
214
215
216
217
218
219
220 public String getAliasedFullyQualifiedTableNameAtRuntime() {
221 StringBuilder sb = new StringBuilder();
222
223 sb.append(getFullyQualifiedTableNameAtRuntime());
224
225 if (stringHasValue(alias)) {
226 sb.append(' ');
227 sb.append(alias);
228 }
229
230 return sb.toString();
231 }
232
233
234
235
236
237
238
239 public String getIbatis2SqlMapNamespace() {
240 String localCatalog = stringHasValue(runtimeCatalog) ? runtimeCatalog
241 : introspectedCatalog;
242 String localSchema = stringHasValue(runtimeSchema) ? runtimeSchema
243 : introspectedSchema;
244 String localTable = stringHasValue(runtimeTableName) ? runtimeTableName
245 : introspectedTableName;
246
247 return composeFullyQualifiedTableName(
248 ignoreQualifiersAtRuntime ? null : localCatalog,
249 ignoreQualifiersAtRuntime ? null : localSchema,
250 localTable, '_');
251 }
252
253
254
255
256
257
258 public String getDomainObjectName() {
259 if (stringHasValue(domainObjectName)) {
260 return domainObjectName;
261 } else if (stringHasValue(runtimeTableName)) {
262 return getCamelCaseString(runtimeTableName, true);
263 } else {
264 return getCamelCaseString(introspectedTableName, true);
265 }
266 }
267
268
269
270
271 @Override
272 public boolean equals(Object obj) {
273 if (this == obj) {
274 return true;
275 }
276
277 if (!(obj instanceof FullyQualifiedTable)) {
278 return false;
279 }
280
281 FullyQualifiedTable other = (FullyQualifiedTable) obj;
282
283 return areEqual(this.introspectedTableName,
284 other.introspectedTableName)
285 && areEqual(this.introspectedCatalog,
286 other.introspectedCatalog)
287 && areEqual(this.introspectedSchema,
288 other.introspectedSchema);
289 }
290
291
292
293
294 @Override
295 public int hashCode() {
296 int result = SEED;
297 result = hash(result, introspectedTableName);
298 result = hash(result, introspectedCatalog);
299 result = hash(result, introspectedSchema);
300
301 return result;
302 }
303
304
305
306
307 @Override
308 public String toString() {
309 return composeFullyQualifiedTableName(
310 introspectedCatalog, introspectedSchema, introspectedTableName,
311 '.');
312 }
313
314
315
316
317
318
319 public String getAlias() {
320 return alias;
321 }
322
323
324
325
326
327
328
329
330
331 public String getSubPackage(boolean isSubPackagesEnabled) {
332 StringBuilder sb = new StringBuilder();
333 if (!ignoreQualifiersAtRuntime && isSubPackagesEnabled) {
334 if (stringHasValue(runtimeCatalog)) {
335 sb.append('.');
336 sb.append(runtimeCatalog.toLowerCase());
337 } else if (stringHasValue(introspectedCatalog)) {
338 sb.append('.');
339 sb.append(introspectedCatalog.toLowerCase());
340 }
341
342 if (stringHasValue(runtimeSchema)) {
343 sb.append('.');
344 sb.append(runtimeSchema.toLowerCase());
345 } else if (stringHasValue(introspectedSchema)) {
346 sb.append('.');
347 sb.append(introspectedSchema.toLowerCase());
348 }
349 }
350
351 if (stringHasValue(domainObjectSubPackage)) {
352 sb.append('.');
353 sb.append(domainObjectSubPackage);
354 }
355
356
357 return sb.toString();
358 }
359
360
361
362
363
364
365
366 private void addDelimiters(StringBuilder sb) {
367 if (stringHasValue(beginningDelimiter)) {
368 sb.insert(0, beginningDelimiter);
369 }
370
371 if (stringHasValue(endingDelimiter)) {
372 sb.append(endingDelimiter);
373 }
374 }
375 }