Skip to content

Commit 7565262

Browse files
committed
HBX-3183: Improve the implementation of AbstractMetaDataDialect
Signed-off-by: Koen Aers <koen.aers@gmail.com>
1 parent c822657 commit 7565262

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
@@ -25,149 +25,152 @@
2525
public abstract class AbstractMetaDataDialect implements RevengDialect {
2626

2727
protected final Logger log = Logger.getLogger(this.getClass());
28-
28+
2929
private Connection connection;
3030
private DatabaseMetaData metaData;
31-
31+
3232
private ConnectionProvider connectionProvider = null;
3333

3434
public void configure(
3535
ConnectionProvider connectionProvider) {
36-
this.connectionProvider = connectionProvider;
36+
this.connectionProvider = connectionProvider;
3737
}
38-
38+
3939
public void close() {
4040
metaData = null;
4141
if(connection != null) {
4242
try {
43-
connectionProvider.closeConnection(connection);
43+
connectionProvider.closeConnection(connection);
4444
}
4545
catch (SQLException e) {
4646
throw new RuntimeException("Problem while closing connection", e);
4747
} finally {
4848
connection = null;
4949
}
5050
}
51-
connectionProvider = null;
51+
connectionProvider = null;
5252
}
53-
53+
5454
protected DatabaseMetaData getMetaData() {
5555
if (metaData == null) {
5656
try {
57-
metaData = getConnection().getMetaData();
58-
}
57+
metaData = getConnection().getMetaData();
58+
}
5959
catch (SQLException e) {
6060
throw new RuntimeException("Getting database metadata", e);
6161
}
6262
}
6363
return metaData;
6464
}
65-
65+
6666
protected String getDatabaseStructure(String catalog, String schema) {
67-
ResultSet schemaRs = null;
68-
ResultSet catalogRs = null;
69-
String nl = System.getProperty("line.separator");
70-
StringBuffer sb = new StringBuffer(nl);
71-
// Let's give the user some feedback. The exception
72-
// is probably related to incorrect schema configuration.
73-
sb.append("Configured schema:").append(schema).append(nl);
74-
sb.append("Configured catalog:").append(catalog ).append(nl);
75-
76-
try {
77-
schemaRs = getMetaData().getSchemas();
78-
sb.append("Available schemas:").append(nl);
79-
while (schemaRs.next() ) {
80-
sb.append(" ").append(schemaRs.getString("TABLE_SCHEM") ).append(nl);
81-
}
82-
}
83-
catch (SQLException e2) {
84-
log.warn("Could not get schemas", e2);
85-
sb.append(" <SQLException while getting schemas>").append(nl);
86-
}
87-
finally {
88-
try {
89-
schemaRs.close();
90-
}
91-
catch (Exception ignore) {
92-
}
93-
}
94-
95-
try {
96-
catalogRs = getMetaData().getCatalogs();
97-
sb.append("Available catalogs:").append(nl);
98-
while (catalogRs.next() ) {
99-
sb.append(" ").append(catalogRs.getString("TABLE_CAT") ).append(nl);
100-
}
101-
}
102-
catch (SQLException e2) {
103-
log.warn("Could not get catalogs", e2);
104-
sb.append(" <SQLException while getting catalogs>").append(nl);
105-
}
106-
finally {
107-
try {
108-
catalogRs.close();
109-
}
110-
catch (Exception ignore) {
111-
}
112-
}
113-
return sb.toString();
114-
}
67+
ResultSet schemaRs = null;
68+
ResultSet catalogRs = null;
69+
String nl = System.lineSeparator();
70+
StringBuilder sb = new StringBuilder(nl);
71+
// Let's give the user some feedback. The exception
72+
// is probably related to incorrect schema configuration.
73+
sb.append("Configured schema:").append(schema).append(nl);
74+
sb.append("Configured catalog:").append(catalog ).append(nl);
75+
76+
try {
77+
schemaRs = getMetaData().getSchemas();
78+
sb.append("Available schemas:").append(nl);
79+
while (schemaRs.next() ) {
80+
sb.append(" ").append(schemaRs.getString("TABLE_SCHEM") ).append(nl);
81+
}
82+
}
83+
catch (SQLException e2) {
84+
log.warn("Could not get schemas", e2);
85+
sb.append(" <SQLException while getting schemas>").append(nl);
86+
}
87+
finally {
88+
try {
89+
if (schemaRs != null) {
90+
schemaRs.close();
91+
}
92+
}
93+
catch (Exception ignore) {
94+
}
95+
}
96+
97+
try {
98+
catalogRs = getMetaData().getCatalogs();
99+
sb.append("Available catalogs:").append(nl);
100+
while (catalogRs.next() ) {
101+
sb.append(" ").append(catalogRs.getString("TABLE_CAT") ).append(nl);
102+
}
103+
}
104+
catch (SQLException e2) {
105+
log.warn("Could not get catalogs", e2);
106+
sb.append(" <SQLException while getting catalogs>").append(nl);
107+
}
108+
finally {
109+
try {
110+
if (catalogRs != null) {
111+
catalogRs.close();
112+
}
113+
}
114+
catch (Exception ignore) {
115+
}
116+
}
117+
return sb.toString();
118+
}
115119

116120
protected Connection getConnection() throws SQLException {
117121
if(connection==null) {
118122
connection = connectionProvider.getConnection();
119123
}
120124
return connection;
121125
}
122-
126+
123127
public void close(Iterator<?> iterator) {
124128
if(iterator instanceof ResultSetIterator) {
125129
((ResultSetIterator)iterator).close();
126130
}
127131
}
128-
132+
129133
public boolean needQuote(String name) {
130-
134+
131135
if(name==null) return false;
132-
133-
// TODO: use jdbc metadata to decide on this. but for now we just handle the most typical cases.
136+
137+
// TODO: use jdbc metadata to decide on this. but for now we just handle the most typical cases.
134138
if(name.indexOf('-')>0) return true;
135139
if(name.indexOf(' ')>0) return true;
136-
if(name.indexOf('.')>0) return true;
137-
return false;
140+
return name.indexOf( '.' ) > 0;
138141
}
139-
142+
140143
protected String caseForSearch(String value) throws SQLException {
141144
// TODO: handle quoted requests (just strip it ?)
142145
if(needQuote(value)) {
143146
if ( getMetaData().storesMixedCaseQuotedIdentifiers() ) {
144147
return value;
145-
} else if ( getMetaData().storesUpperCaseQuotedIdentifiers() ) {
146-
return toUpperCase( value );
148+
} else if ( getMetaData().storesUpperCaseQuotedIdentifiers() ) {
149+
return toUpperCase( value );
147150
} else if( getMetaData().storesLowerCaseQuotedIdentifiers() ) {
148151
return toLowerCase( value );
149152
} else {
150153
return value;
151154
}
152155
} else if ( getMetaData().storesMixedCaseQuotedIdentifiers() ) {
153156
return value;
154-
} else if ( getMetaData().storesUpperCaseIdentifiers() ) {
155-
return toUpperCase( value );
157+
} else if ( getMetaData().storesUpperCaseIdentifiers() ) {
158+
return toUpperCase( value );
156159
} else if( getMetaData().storesLowerCaseIdentifiers() ) {
157160
return toLowerCase( value );
158161
} else {
159162
return value;
160-
}
163+
}
161164
}
162165

163166
private String toUpperCase(String str) {
164167
return str==null ? null : str.toUpperCase();
165168
}
166-
169+
167170
private String toLowerCase(String str) {
168171
return str == null ? null : str.toLowerCase(Locale.ENGLISH);
169172
}
170-
173+
171174
public Iterator<Map<String, Object>> getSuggestedPrimaryKeyStrategyName(String catalog, String schema, String table) {
172175
Map<String, Object> m = new HashMap<String, Object>();
173176
m.put( "TABLE_CAT", catalog );

0 commit comments

Comments
 (0)