• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mybatis / generator / 1602

26 Feb 2025 05:58PM UTC coverage: 88.328% (-0.01%) from 88.339%
1602

push

github

web-flow
Merge pull request #1297 from jeffgbutler/java-17

Code Cleanup after Java 17 Update

2518 of 3412 branches covered (73.8%)

163 of 180 new or added lines in 53 files covered. (90.56%)

12 existing lines in 5 files now uncovered.

11192 of 12671 relevant lines covered (88.33%)

0.88 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

80.77
/core/mybatis-generator-core/src/main/java/org/mybatis/generator/api/FullyQualifiedTable.java
1
/*
2
 *    Copyright 2006-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.generator.api;
17

18
import static org.mybatis.generator.internal.util.StringUtility.composeFullyQualifiedTableName;
19
import static org.mybatis.generator.internal.util.StringUtility.stringHasValue;
20

21
import java.util.Objects;
22
import java.util.regex.Matcher;
23
import java.util.regex.Pattern;
24

25
import org.mybatis.generator.config.Context;
26
import org.mybatis.generator.config.DomainObjectRenamingRule;
27
import org.mybatis.generator.internal.util.JavaBeansUtil;
28

29
public class FullyQualifiedTable {
30

31
    private final String introspectedCatalog;
32
    private final String introspectedSchema;
33
    private final String introspectedTableName;
34
    private final String runtimeCatalog;
35
    private final String runtimeSchema;
36
    private final String runtimeTableName;
37
    private String domainObjectName;
38
    private String domainObjectSubPackage;
39
    private final String alias;
40
    private final boolean ignoreQualifiersAtRuntime;
41
    private final String beginningDelimiter;
42
    private final String endingDelimiter;
43
    private final DomainObjectRenamingRule domainObjectRenamingRule;
44

45
    /**
46
     * This object is used to hold information related to the table itself, not the columns in the
47
     * table.
48
     *
49
     * @param introspectedCatalog
50
     *            the actual catalog of the table as returned from DatabaseMetaData. This value
51
     *            should only be set if the user configured a catalog. Otherwise, the
52
     *            DatabaseMetaData is reporting some database default that we don't want in the
53
     *            generated code.
54
     * @param introspectedSchema
55
     *            the actual schema of the table as returned from DatabaseMetaData. This value
56
     *            should only be set if the user configured a schema. Otherwise, the
57
     *            DatabaseMetaData is reporting some database default that we don't want in the
58
     *            generated code.
59
     * @param introspectedTableName
60
     *            the actual table name as returned from DatabaseMetaData
61
     * @param domainObjectName
62
     *            the configured domain object name for this table. If nothing is configured, we'll build the domain
63
     *            object named based on the tableName or runtimeTableName.
64
     * @param alias
65
     *            a configured alias for the table. This alias will be added to the table name in the SQL
66
     * @param ignoreQualifiersAtRuntime
67
     *            if true, then the catalog and schema qualifiers will be ignored when composing fully qualified names
68
     *            in the generated SQL. This is used, for example, when the user needs to specify a specific schema for
69
     *            generating code but does not want the schema in the generated SQL
70
     * @param runtimeCatalog
71
     *            this is used to "rename" the catalog in the generated SQL. This is useful, for example, when
72
     *            generating code against one catalog that should run with a different catalog.
73
     * @param runtimeSchema
74
     *            this is used to "rename" the schema in the generated SQL. This is useful, for example, when generating
75
     *            code against one schema that should run with a different schema.
76
     * @param runtimeTableName
77
     *            this is used to "rename" the table in the generated SQL. This is useful, for example, when generating
78
     *            code to run with an Oracle synonym. The user would have to specify the actual table name and schema
79
     *            for generation, but would want to use the synonym name in the generated SQL
80
     * @param delimitIdentifiers
81
     *            if true, then the table identifiers will be delimited at runtime. The delimiter characters are
82
     *            obtained from the Context.
83
     * @param domainObjectRenamingRule
84
     *            If domainObjectName is not configured, we'll build the domain object named based on the tableName
85
     *            or runtimeTableName.
86
     *            And then we use the domain object renaming rule to generate the final domain object name.
87
     * @param context
88
     *            the context
89
     */
90
    public FullyQualifiedTable(String introspectedCatalog,
91
            String introspectedSchema, String introspectedTableName,
92
            String domainObjectName, String alias,
93
            boolean ignoreQualifiersAtRuntime, String runtimeCatalog,
94
            String runtimeSchema, String runtimeTableName,
95
            boolean delimitIdentifiers, DomainObjectRenamingRule domainObjectRenamingRule,
96
            Context context) {
97
        super();
1✔
98
        this.introspectedCatalog = introspectedCatalog;
1✔
99
        this.introspectedSchema = introspectedSchema;
1✔
100
        this.introspectedTableName = introspectedTableName;
1✔
101
        this.ignoreQualifiersAtRuntime = ignoreQualifiersAtRuntime;
1✔
102
        this.runtimeCatalog = runtimeCatalog;
1✔
103
        this.runtimeSchema = runtimeSchema;
1✔
104
        this.runtimeTableName = runtimeTableName;
1✔
105
        this.domainObjectRenamingRule = domainObjectRenamingRule;
1✔
106

107
        if (stringHasValue(domainObjectName)) {
1✔
108
            int index = domainObjectName.lastIndexOf('.');
1✔
109
            if (index == -1) {
1✔
110
                this.domainObjectName = domainObjectName;
1✔
111
            } else {
112
                this.domainObjectName = domainObjectName.substring(index + 1);
1✔
113
                this.domainObjectSubPackage = domainObjectName.substring(0, index);
1✔
114
            }
115
        }
116

117
        if (alias == null) {
1✔
118
            this.alias = null;
1✔
119
        } else {
120
            this.alias = alias.trim();
1✔
121
        }
122

123
        beginningDelimiter = delimitIdentifiers ? context
1✔
124
                .getBeginningDelimiter() : ""; //$NON-NLS-1$
1✔
125
        endingDelimiter = delimitIdentifiers ? context.getEndingDelimiter()
1✔
126
                : ""; //$NON-NLS-1$
1✔
127
    }
1✔
128

129
    public String getIntrospectedCatalog() {
130
        return introspectedCatalog;
1✔
131
    }
132

133
    public String getIntrospectedSchema() {
134
        return introspectedSchema;
1✔
135
    }
136

137
    public String getIntrospectedTableName() {
138
        return introspectedTableName;
1✔
139
    }
140

141
    public String getFullyQualifiedTableNameAtRuntime() {
142
        StringBuilder localCatalog = new StringBuilder();
1✔
143
        if (!ignoreQualifiersAtRuntime) {
1!
144
            if (stringHasValue(runtimeCatalog)) {
1!
145
                localCatalog.append(runtimeCatalog);
×
146
            } else if (stringHasValue(introspectedCatalog)) {
1!
147
                localCatalog.append(introspectedCatalog);
×
148
            }
149
        }
150
        if (!localCatalog.isEmpty()) {
1!
151
            addDelimiters(localCatalog);
×
152
        }
153

154
        StringBuilder localSchema = new StringBuilder();
1✔
155
        if (!ignoreQualifiersAtRuntime) {
1!
156
            if (stringHasValue(runtimeSchema)) {
1!
157
                localSchema.append(runtimeSchema);
×
158
            } else if (stringHasValue(introspectedSchema)) {
1✔
159
                localSchema.append(introspectedSchema);
1✔
160
            }
161
        }
162
        if (!localSchema.isEmpty()) {
1✔
163
            addDelimiters(localSchema);
1✔
164
        }
165

166
        StringBuilder localTableName = new StringBuilder();
1✔
167
        if (stringHasValue(runtimeTableName)) {
1!
168
            localTableName.append(runtimeTableName);
×
169
        } else {
170
            localTableName.append(introspectedTableName);
1✔
171
        }
172
        addDelimiters(localTableName);
1✔
173

174
        return composeFullyQualifiedTableName(localCatalog
1✔
175
                .toString(), localSchema.toString(), localTableName.toString(),
1✔
176
                '.');
177
    }
178

179
    public String getAliasedFullyQualifiedTableNameAtRuntime() {
180
        StringBuilder sb = new StringBuilder();
1✔
181

182
        sb.append(getFullyQualifiedTableNameAtRuntime());
1✔
183

184
        if (stringHasValue(alias)) {
1✔
185
            sb.append(' ');
1✔
186
            sb.append(alias);
1✔
187
        }
188

189
        return sb.toString();
1✔
190
    }
191

192
    public String getDomainObjectName() {
193
        if (stringHasValue(domainObjectName)) {
1✔
194
            return domainObjectName;
1✔
195
        }
196

197
        String finalDomainObjectName;
198
        if (stringHasValue(runtimeTableName)) {
1!
199
            finalDomainObjectName = JavaBeansUtil.getCamelCaseString(runtimeTableName, true);
×
200
        } else {
201
            finalDomainObjectName = JavaBeansUtil.getCamelCaseString(introspectedTableName, true);
1✔
202
        }
203

204
        if (domainObjectRenamingRule != null) {
1✔
205
            Pattern pattern = Pattern.compile(domainObjectRenamingRule.getSearchString());
1✔
206
            String replaceString = domainObjectRenamingRule.getReplaceString();
1✔
207
            replaceString = replaceString == null ? "" : replaceString; //$NON-NLS-1$
1✔
208
            Matcher matcher = pattern.matcher(finalDomainObjectName);
1✔
209
            finalDomainObjectName = JavaBeansUtil.getFirstCharacterUppercase(matcher.replaceAll(replaceString));
1✔
210
        }
211
        return finalDomainObjectName;
1✔
212
    }
213

214
    @Override
215
    public boolean equals(Object obj) {
216
        if (this == obj) {
×
217
            return true;
×
218
        }
219

NEW
220
        if (!(obj instanceof FullyQualifiedTable other)) {
×
221
            return false;
×
222
        }
223

224
        return Objects.equals(this.introspectedTableName, other.introspectedTableName)
×
UNCOV
225
                && Objects.equals(this.introspectedCatalog, other.introspectedCatalog)
×
226
                && Objects.equals(this.introspectedSchema, other.introspectedSchema);
×
227
    }
228

229
    @Override
230
    public int hashCode() {
231
        return Objects.hash(introspectedTableName, introspectedCatalog, introspectedCatalog);
1✔
232
    }
233

234
    @Override
235
    public String toString() {
236
        return composeFullyQualifiedTableName(
1✔
237
                introspectedCatalog, introspectedSchema, introspectedTableName,
238
                '.');
239
    }
240

241
    public String getAlias() {
242
        return alias;
×
243
    }
244

245
    /**
246
     * Calculates a Java package fragment based on the table catalog and schema.
247
     * If qualifiers are ignored, then this method will return an empty string.
248
     *
249
     * <p>This method is used for determining the sub package for Java client and
250
     * SQL map (XML) objects.  It ignores any sub-package added to the
251
     * domain object name in the table configuration.
252
     *
253
     * @param isSubPackagesEnabled
254
     *            the is sub packages enabled
255
     * @return the subpackage for this table
256
     */
257
    public String getSubPackageForClientOrSqlMap(boolean isSubPackagesEnabled) {
258
        StringBuilder sb = new StringBuilder();
1✔
259
        if (!ignoreQualifiersAtRuntime && isSubPackagesEnabled) {
1!
260
            if (stringHasValue(runtimeCatalog)) {
1!
261
                sb.append('.');
×
262
                sb.append(runtimeCatalog.toLowerCase());
×
263
            } else if (stringHasValue(introspectedCatalog)) {
1!
264
                sb.append('.');
×
265
                sb.append(introspectedCatalog.toLowerCase());
×
266
            }
267

268
            if (stringHasValue(runtimeSchema)) {
1!
269
                sb.append('.');
×
270
                sb.append(runtimeSchema.toLowerCase());
×
271
            } else if (stringHasValue(introspectedSchema)) {
1✔
272
                sb.append('.');
1✔
273
                sb.append(introspectedSchema.toLowerCase());
1✔
274
            }
275
        }
276

277
        // TODO - strip characters that are not valid in package names
278
        return sb.toString();
1✔
279
    }
280

281
    /**
282
     * Calculates a Java package fragment based on the table catalog and schema.
283
     * If qualifiers are ignored, then this method will return an empty string.
284
     *
285
     * <p>This method is used for determining the sub package for Java model objects only.
286
     * It takes into account the possibility that a sub-package was added to the
287
     * domain object name in the table configuration.
288
     *
289
     * @param isSubPackagesEnabled
290
     *            the is sub packages enabled
291
     * @return the subpackage for this table
292
     */
293
    public String getSubPackageForModel(boolean isSubPackagesEnabled) {
294
        StringBuilder sb = new StringBuilder();
1✔
295
        sb.append(getSubPackageForClientOrSqlMap(isSubPackagesEnabled));
1✔
296

297
        if (stringHasValue(domainObjectSubPackage)) {
1✔
298
            sb.append('.');
1✔
299
            sb.append(domainObjectSubPackage);
1✔
300
        }
301

302
        return sb.toString();
1✔
303
    }
304

305
    private void addDelimiters(StringBuilder sb) {
306
        if (stringHasValue(beginningDelimiter)) {
1✔
307
            sb.insert(0, beginningDelimiter);
1✔
308
        }
309

310
        if (stringHasValue(endingDelimiter)) {
1✔
311
            sb.append(endingDelimiter);
1✔
312
        }
313
    }
1✔
314

315
    public String getDomainObjectSubPackage() {
316
        return domainObjectSubPackage;
1✔
317
    }
318
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc