Skip to content

Commit d5ba816

Browse files
committed
HBX-3208: Improve the implementation of the 'dialect' classes
Signed-off-by: Koen Aers <[email protected]>
1 parent ea60c83 commit d5ba816

File tree

8 files changed

+1554
-1525
lines changed

8 files changed

+1554
-1525
lines changed

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

Lines changed: 165 additions & 157 deletions
Original file line numberDiff line numberDiff line change
@@ -41,161 +41,169 @@
4141
*/
4242
public abstract class AbstractMetaDataDialect implements RevengDialect {
4343

44-
protected final Logger log = Logger.getLogger(this.getClass());
45-
46-
private Connection connection;
47-
private DatabaseMetaData metaData;
48-
49-
private ConnectionProvider connectionProvider = null;
50-
51-
public void configure(
52-
ConnectionProvider connectionProvider) {
53-
this.connectionProvider = connectionProvider;
54-
}
55-
56-
public void close() {
57-
metaData = null;
58-
if(connection != null) {
59-
try {
60-
connectionProvider.closeConnection(connection);
61-
}
62-
catch (SQLException e) {
63-
throw new RuntimeException("Problem while closing connection", e);
64-
} finally {
65-
connection = null;
66-
}
67-
}
68-
connectionProvider = null;
69-
}
70-
71-
protected DatabaseMetaData getMetaData() {
72-
if (metaData == null) {
73-
try {
74-
metaData = getConnection().getMetaData();
75-
}
76-
catch (SQLException e) {
77-
throw new RuntimeException("Getting database metadata", e);
78-
}
79-
}
80-
return metaData;
81-
}
82-
83-
protected String getDatabaseStructure(String catalog, String schema) {
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-
}
136-
137-
protected Connection getConnection() throws SQLException {
138-
if(connection==null) {
139-
connection = connectionProvider.getConnection();
140-
}
141-
return connection;
142-
}
143-
144-
public void close(Iterator<?> iterator) {
145-
if(iterator instanceof ResultSetIterator) {
146-
((ResultSetIterator)iterator).close();
147-
}
148-
}
149-
150-
public boolean needQuote(String name) {
151-
152-
if(name==null) return false;
153-
154-
// TODO: use jdbc metadata to decide on this. but for now we just handle the most typical cases.
155-
if(name.indexOf('-')>0) return true;
156-
if(name.indexOf(' ')>0) return true;
157-
return name.indexOf( '.' ) > 0;
158-
}
159-
160-
protected String caseForSearch(String value) throws SQLException {
161-
// TODO: handle quoted requests (just strip it ?)
162-
if(needQuote(value)) {
163-
if ( getMetaData().storesMixedCaseQuotedIdentifiers() ) {
164-
return value;
165-
} else if ( getMetaData().storesUpperCaseQuotedIdentifiers() ) {
166-
return toUpperCase( value );
167-
} else if( getMetaData().storesLowerCaseQuotedIdentifiers() ) {
168-
return toLowerCase( value );
169-
} else {
170-
return value;
171-
}
172-
} else if ( getMetaData().storesMixedCaseQuotedIdentifiers() ) {
173-
return value;
174-
} else if ( getMetaData().storesUpperCaseIdentifiers() ) {
175-
return toUpperCase( value );
176-
} else if( getMetaData().storesLowerCaseIdentifiers() ) {
177-
return toLowerCase( value );
178-
} else {
179-
return value;
180-
}
181-
}
182-
183-
private String toUpperCase(String str) {
184-
return str==null ? null : str.toUpperCase();
185-
}
186-
187-
private String toLowerCase(String str) {
188-
return str == null ? null : str.toLowerCase(Locale.ENGLISH);
189-
}
190-
191-
public Iterator<Map<String, Object>> getSuggestedPrimaryKeyStrategyName(String catalog, String schema, String table) {
192-
Map<String, Object> m = new HashMap<String, Object>();
193-
m.put( "TABLE_CAT", catalog );
194-
m.put( "TABLE_SCHEMA", schema );
195-
m.put( "TABLE_NAME", table );
196-
m.put( "HIBERNATE_STRATEGY", null );
197-
List<Map<String, Object>> l = new ArrayList<Map<String, Object>>();
198-
l.add(m);
199-
return l.iterator();
200-
}
44+
protected final Logger log = Logger.getLogger(this.getClass());
45+
46+
private Connection connection;
47+
private DatabaseMetaData metaData;
48+
49+
private ConnectionProvider connectionProvider = null;
50+
51+
public void configure(
52+
ConnectionProvider connectionProvider) {
53+
this.connectionProvider = connectionProvider;
54+
}
55+
56+
public void close() {
57+
metaData = null;
58+
if(connection != null) {
59+
try {
60+
connectionProvider.closeConnection(connection);
61+
}
62+
catch (SQLException e) {
63+
throw new RuntimeException("Problem while closing connection", e);
64+
}
65+
finally {
66+
connection = null;
67+
}
68+
}
69+
connectionProvider = null;
70+
}
71+
72+
protected DatabaseMetaData getMetaData() {
73+
if (metaData == null) {
74+
try {
75+
metaData = getConnection().getMetaData();
76+
}
77+
catch (SQLException e) {
78+
throw new RuntimeException("Getting database metadata", e);
79+
}
80+
}
81+
return metaData;
82+
}
83+
84+
protected String getDatabaseStructure(String catalog, String schema) {
85+
ResultSet schemaRs = null;
86+
ResultSet catalogRs = null;
87+
String nl = System.lineSeparator();
88+
StringBuilder sb = new StringBuilder(nl);
89+
// Let's give the user some feedback. The exception
90+
// is probably related to incorrect schema configuration.
91+
sb.append("Configured schema:").append(schema).append(nl);
92+
sb.append("Configured catalog:").append(catalog ).append(nl);
93+
94+
try {
95+
schemaRs = getMetaData().getSchemas();
96+
sb.append("Available schemas:").append(nl);
97+
while (schemaRs.next() ) {
98+
sb.append(" ").append(schemaRs.getString("TABLE_SCHEM") ).append(nl);
99+
}
100+
}
101+
catch (SQLException e2) {
102+
log.warn("Could not get schemas", e2);
103+
sb.append(" <SQLException while getting schemas>").append(nl);
104+
}
105+
finally {
106+
try {
107+
if (schemaRs != null) {
108+
schemaRs.close();
109+
}
110+
}
111+
catch (Exception ignore) {
112+
}
113+
}
114+
115+
try {
116+
catalogRs = getMetaData().getCatalogs();
117+
sb.append("Available catalogs:").append(nl);
118+
while (catalogRs.next() ) {
119+
sb.append(" ").append(catalogRs.getString("TABLE_CAT") ).append(nl);
120+
}
121+
}
122+
catch (SQLException e2) {
123+
log.warn("Could not get catalogs", e2);
124+
sb.append(" <SQLException while getting catalogs>").append(nl);
125+
}
126+
finally {
127+
try {
128+
if (catalogRs != null) {
129+
catalogRs.close();
130+
}
131+
}
132+
catch (Exception ignore) {
133+
}
134+
}
135+
return sb.toString();
136+
}
137+
138+
protected Connection getConnection() throws SQLException {
139+
if(connection==null) {
140+
connection = connectionProvider.getConnection();
141+
}
142+
return connection;
143+
}
144+
145+
public void close(Iterator<?> iterator) {
146+
if(iterator instanceof ResultSetIterator) {
147+
((ResultSetIterator)iterator).close();
148+
}
149+
}
150+
151+
public boolean needQuote(String name) {
152+
153+
if(name==null) return false;
154+
155+
// TODO: use jdbc metadata to decide on this. but for now we just handle the most typical cases.
156+
if(name.indexOf('-')>0) return true;
157+
if(name.indexOf(' ')>0) return true;
158+
return name.indexOf( '.' ) > 0;
159+
}
160+
161+
protected String caseForSearch(String value) throws SQLException {
162+
// TODO: handle quoted requests (just strip it ?)
163+
if(needQuote(value)) {
164+
if ( getMetaData().storesMixedCaseQuotedIdentifiers() ) {
165+
return value;
166+
}
167+
else if ( getMetaData().storesUpperCaseQuotedIdentifiers() ) {
168+
return toUpperCase( value );
169+
}
170+
else if( getMetaData().storesLowerCaseQuotedIdentifiers() ) {
171+
return toLowerCase( value );
172+
}
173+
else {
174+
return value;
175+
}
176+
}
177+
else if ( getMetaData().storesMixedCaseQuotedIdentifiers() ) {
178+
return value;
179+
}
180+
else if ( getMetaData().storesUpperCaseIdentifiers() ) {
181+
return toUpperCase( value );
182+
}
183+
else if( getMetaData().storesLowerCaseIdentifiers() ) {
184+
return toLowerCase( value );
185+
}
186+
else {
187+
return value;
188+
}
189+
}
190+
191+
private String toUpperCase(String str) {
192+
return str==null ? null : str.toUpperCase();
193+
}
194+
195+
private String toLowerCase(String str) {
196+
return str == null ? null : str.toLowerCase(Locale.ENGLISH);
197+
}
198+
199+
public Iterator<Map<String, Object>> getSuggestedPrimaryKeyStrategyName(String catalog, String schema, String table) {
200+
Map<String, Object> m = new HashMap<>();
201+
m.put( "TABLE_CAT", catalog );
202+
m.put( "TABLE_SCHEMA", schema );
203+
m.put( "TABLE_NAME", table );
204+
m.put( "HIBERNATE_STRATEGY", null );
205+
List<Map<String, Object>> l = new ArrayList<>();
206+
l.add(m);
207+
return l.iterator();
208+
}
201209
}

0 commit comments

Comments
 (0)