Skip to content

Commit dfb22db

Browse files
committed
HBX-3183: Improve the implementation of AbstractMetaDataDialect
Signed-off-by: Koen Aers <[email protected]>
1 parent b848048 commit dfb22db

File tree

1 file changed

+76
-73
lines changed

1 file changed

+76
-73
lines changed

orm/src/main/java/org/hibernate/tool/internal/reveng/dialect/AbstractMetaDataDialect.java

Lines changed: 76 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -42,149 +42,152 @@
4242
public abstract class AbstractMetaDataDialect implements RevengDialect {
4343

4444
protected final Logger log = Logger.getLogger(this.getClass());
45-
45+
4646
private Connection connection;
4747
private DatabaseMetaData metaData;
48-
48+
4949
private ConnectionProvider connectionProvider = null;
5050

5151
public void configure(
5252
ConnectionProvider connectionProvider) {
53-
this.connectionProvider = connectionProvider;
53+
this.connectionProvider = connectionProvider;
5454
}
55-
55+
5656
public void close() {
5757
metaData = null;
5858
if(connection != null) {
5959
try {
60-
connectionProvider.closeConnection(connection);
60+
connectionProvider.closeConnection(connection);
6161
}
6262
catch (SQLException e) {
6363
throw new RuntimeException("Problem while closing connection", e);
6464
} finally {
6565
connection = null;
6666
}
6767
}
68-
connectionProvider = null;
68+
connectionProvider = null;
6969
}
70-
70+
7171
protected DatabaseMetaData getMetaData() {
7272
if (metaData == null) {
7373
try {
74-
metaData = getConnection().getMetaData();
75-
}
74+
metaData = getConnection().getMetaData();
75+
}
7676
catch (SQLException e) {
7777
throw new RuntimeException("Getting database metadata", e);
7878
}
7979
}
8080
return metaData;
8181
}
82-
82+
8383
protected String getDatabaseStructure(String catalog, String schema) {
84-
ResultSet schemaRs = null;
85-
ResultSet catalogRs = null;
86-
String nl = System.getProperty("line.separator");
87-
StringBuffer sb = new StringBuffer(nl);
88-
// Let's give the user some feedback. The exception
89-
// is probably related to incorrect schema configuration.
90-
sb.append("Configured schema:").append(schema).append(nl);
91-
sb.append("Configured catalog:").append(catalog ).append(nl);
92-
93-
try {
94-
schemaRs = getMetaData().getSchemas();
95-
sb.append("Available schemas:").append(nl);
96-
while (schemaRs.next() ) {
97-
sb.append(" ").append(schemaRs.getString("TABLE_SCHEM") ).append(nl);
98-
}
99-
}
100-
catch (SQLException e2) {
101-
log.warn("Could not get schemas", e2);
102-
sb.append(" <SQLException while getting schemas>").append(nl);
103-
}
104-
finally {
105-
try {
106-
schemaRs.close();
107-
}
108-
catch (Exception ignore) {
109-
}
110-
}
111-
112-
try {
113-
catalogRs = getMetaData().getCatalogs();
114-
sb.append("Available catalogs:").append(nl);
115-
while (catalogRs.next() ) {
116-
sb.append(" ").append(catalogRs.getString("TABLE_CAT") ).append(nl);
117-
}
118-
}
119-
catch (SQLException e2) {
120-
log.warn("Could not get catalogs", e2);
121-
sb.append(" <SQLException while getting catalogs>").append(nl);
122-
}
123-
finally {
124-
try {
125-
catalogRs.close();
126-
}
127-
catch (Exception ignore) {
128-
}
129-
}
130-
return sb.toString();
131-
}
84+
ResultSet schemaRs = null;
85+
ResultSet catalogRs = null;
86+
String nl = System.lineSeparator();
87+
StringBuilder sb = new StringBuilder(nl);
88+
// Let's give the user some feedback. The exception
89+
// is probably related to incorrect schema configuration.
90+
sb.append("Configured schema:").append(schema).append(nl);
91+
sb.append("Configured catalog:").append(catalog ).append(nl);
92+
93+
try {
94+
schemaRs = getMetaData().getSchemas();
95+
sb.append("Available schemas:").append(nl);
96+
while (schemaRs.next() ) {
97+
sb.append(" ").append(schemaRs.getString("TABLE_SCHEM") ).append(nl);
98+
}
99+
}
100+
catch (SQLException e2) {
101+
log.warn("Could not get schemas", e2);
102+
sb.append(" <SQLException while getting schemas>").append(nl);
103+
}
104+
finally {
105+
try {
106+
if (schemaRs != null) {
107+
schemaRs.close();
108+
}
109+
}
110+
catch (Exception ignore) {
111+
}
112+
}
113+
114+
try {
115+
catalogRs = getMetaData().getCatalogs();
116+
sb.append("Available catalogs:").append(nl);
117+
while (catalogRs.next() ) {
118+
sb.append(" ").append(catalogRs.getString("TABLE_CAT") ).append(nl);
119+
}
120+
}
121+
catch (SQLException e2) {
122+
log.warn("Could not get catalogs", e2);
123+
sb.append(" <SQLException while getting catalogs>").append(nl);
124+
}
125+
finally {
126+
try {
127+
if (catalogRs != null) {
128+
catalogRs.close();
129+
}
130+
}
131+
catch (Exception ignore) {
132+
}
133+
}
134+
return sb.toString();
135+
}
132136

133137
protected Connection getConnection() throws SQLException {
134138
if(connection==null) {
135139
connection = connectionProvider.getConnection();
136140
}
137141
return connection;
138142
}
139-
143+
140144
public void close(Iterator<?> iterator) {
141145
if(iterator instanceof ResultSetIterator) {
142146
((ResultSetIterator)iterator).close();
143147
}
144148
}
145-
149+
146150
public boolean needQuote(String name) {
147-
151+
148152
if(name==null) return false;
149-
150-
// TODO: use jdbc metadata to decide on this. but for now we just handle the most typical cases.
153+
154+
// TODO: use jdbc metadata to decide on this. but for now we just handle the most typical cases.
151155
if(name.indexOf('-')>0) return true;
152156
if(name.indexOf(' ')>0) return true;
153-
if(name.indexOf('.')>0) return true;
154-
return false;
157+
return name.indexOf( '.' ) > 0;
155158
}
156-
159+
157160
protected String caseForSearch(String value) throws SQLException {
158161
// TODO: handle quoted requests (just strip it ?)
159162
if(needQuote(value)) {
160163
if ( getMetaData().storesMixedCaseQuotedIdentifiers() ) {
161164
return value;
162-
} else if ( getMetaData().storesUpperCaseQuotedIdentifiers() ) {
163-
return toUpperCase( value );
165+
} else if ( getMetaData().storesUpperCaseQuotedIdentifiers() ) {
166+
return toUpperCase( value );
164167
} else if( getMetaData().storesLowerCaseQuotedIdentifiers() ) {
165168
return toLowerCase( value );
166169
} else {
167170
return value;
168171
}
169172
} else if ( getMetaData().storesMixedCaseQuotedIdentifiers() ) {
170173
return value;
171-
} else if ( getMetaData().storesUpperCaseIdentifiers() ) {
172-
return toUpperCase( value );
174+
} else if ( getMetaData().storesUpperCaseIdentifiers() ) {
175+
return toUpperCase( value );
173176
} else if( getMetaData().storesLowerCaseIdentifiers() ) {
174177
return toLowerCase( value );
175178
} else {
176179
return value;
177-
}
180+
}
178181
}
179182

180183
private String toUpperCase(String str) {
181184
return str==null ? null : str.toUpperCase();
182185
}
183-
186+
184187
private String toLowerCase(String str) {
185188
return str == null ? null : str.toLowerCase(Locale.ENGLISH);
186189
}
187-
190+
188191
public Iterator<Map<String, Object>> getSuggestedPrimaryKeyStrategyName(String catalog, String schema, String table) {
189192
Map<String, Object> m = new HashMap<String, Object>();
190193
m.put( "TABLE_CAT", catalog );

0 commit comments

Comments
 (0)