Skip to content

Commit d29b55a

Browse files
committed
HHH-9849 - Fix Duplicate column name for mixed case column name on schema update
1 parent 9024ff5 commit d29b55a

File tree

9 files changed

+40
-228
lines changed

9 files changed

+40
-228
lines changed

hibernate-core/src/main/java/org/hibernate/engine/jdbc/env/internal/NormalizingIdentifierHelperImpl.java

Lines changed: 0 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -188,79 +188,4 @@ public String toMetaDataObjectName(Identifier identifier) {
188188
}
189189
return toMetaDataText( identifier );
190190
}
191-
192-
@Override
193-
public Identifier fromMetaDataCatalogName(String catalogName) {
194-
if ( catalogName == null || !nameQualifierSupport.supportsCatalogs() ) {
195-
return null;
196-
}
197-
198-
// if ( jdbcEnvironment.getCurrentCatalog() == null
199-
// || catalogName.equals( jdbcEnvironment.getCurrentCatalog().getText() ) ) {
200-
// return null;
201-
// }
202-
203-
return toIdentifierFromMetaData( catalogName );
204-
}
205-
206-
public Identifier toIdentifierFromMetaData(String text) {
207-
log.tracef( "Interpreting return value [%s] from DatabaseMetaData as identifier", text );
208-
209-
if ( globallyQuoteIdentifiers ) {
210-
log.trace( "Forcing DatabaseMetaData return value as quoted due to global quoting" );
211-
return Identifier.toIdentifier( text, true );
212-
}
213-
214-
if ( autoQuoteKeywords && isReservedWord( text ) ) {
215-
// unequivocally it needs to be quoted...
216-
log.trace( "Forcing DatabaseMetaData return value as quoted as it was recognized as a reserved word" );
217-
return Identifier.toIdentifier( text, true );
218-
}
219-
220-
// lovely decipher of whether the incoming value represents a quoted identifier...
221-
final boolean isUpperCase = text.toUpperCase( Locale.ROOT ).equals( text );
222-
final boolean isLowerCase = text.toLowerCase( Locale.ROOT ).equals( text );
223-
final boolean isMixedCase = ! isLowerCase && ! isUpperCase;
224-
225-
if ( quotedCaseStrategy == IdentifierCaseStrategy.MIXED && isMixedCase ) {
226-
log.trace( "Interpreting return value as quoted due to case strategy" );
227-
return Identifier.toIdentifier( text, true );
228-
}
229-
230-
if ( quotedCaseStrategy == IdentifierCaseStrategy.LOWER && isLowerCase ) {
231-
log.trace( "Interpreting return value as quoted due to case strategy" );
232-
return Identifier.toIdentifier( text, true );
233-
}
234-
235-
if ( quotedCaseStrategy == IdentifierCaseStrategy.UPPER && isUpperCase ) {
236-
log.trace( "Interpreting return value as quoted due to case strategy" );
237-
return Identifier.toIdentifier( text, true );
238-
}
239-
240-
log.trace( "Interpreting return value as unquoted due to case strategy" );
241-
return Identifier.toIdentifier( text );
242-
}
243-
244-
@Override
245-
public Identifier fromMetaDataSchemaName(String schemaName) {
246-
if ( schemaName == null || !nameQualifierSupport.supportsSchemas() ) {
247-
return null;
248-
}
249-
250-
// if ( jdbcEnvironment.getCurrentSchema() == null
251-
// || schemaName.equals( jdbcEnvironment.getCurrentSchema().getText() ) ) {
252-
// return null;
253-
// }
254-
255-
return toIdentifierFromMetaData( schemaName );
256-
}
257-
258-
@Override
259-
public Identifier fromMetaDataObjectName(String objectName) {
260-
if ( objectName == null ) {
261-
return null;
262-
}
263-
264-
return toIdentifierFromMetaData( objectName );
265-
}
266191
}

hibernate-core/src/main/java/org/hibernate/engine/jdbc/env/spi/IdentifierHelper.java

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -101,34 +101,4 @@ public interface IdentifierHelper {
101101
* @return The String representation of the given object name
102102
*/
103103
String toMetaDataObjectName(Identifier identifier);
104-
105-
/**
106-
* Parse an Identifier representation from the String representation of a catalog name
107-
* as obtained from {@link java.sql.DatabaseMetaData} calls.
108-
*
109-
* @param catalogName The String representation of a catalog name
110-
*
111-
* @return The parsed Identifier representation of the given catalog name
112-
*/
113-
Identifier fromMetaDataCatalogName(String catalogName);
114-
115-
/**
116-
* Parse an Identifier representation from the String representation of a schema name
117-
* as obtained from {@link java.sql.DatabaseMetaData} calls.
118-
*
119-
* @param schemaName The String representation of a schema name
120-
*
121-
* @return The parsed Identifier representation of the given schema name
122-
*/
123-
Identifier fromMetaDataSchemaName(String schemaName);
124-
125-
/**
126-
* Parse an Identifier representation from the String representation of an object name
127-
* as obtained from {@link java.sql.DatabaseMetaData} calls.
128-
*
129-
* @param name The String representation of an object name
130-
*
131-
* @return The parsed Identifier representation of the given object name
132-
*/
133-
Identifier fromMetaDataObjectName(String name);
134104
}

hibernate-core/src/main/java/org/hibernate/tool/schema/extract/internal/InformationExtractorJdbcDatabaseMetaDataImpl.java

Lines changed: 29 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import java.sql.ResultSet;
1111
import java.sql.SQLException;
1212
import java.util.ArrayList;
13-
import java.util.Collection;
1413
import java.util.HashMap;
1514
import java.util.List;
1615
import java.util.Map;
@@ -106,46 +105,6 @@ public boolean schemaExists(Identifier catalog, Identifier schema) {
106105
}
107106
}
108107

109-
@Override
110-
public Collection<TableInformation> getTables(Identifier catalog, Identifier schema) {
111-
try {
112-
final String catalogFilter = determineCatalogFilter( catalog );
113-
final String schemaFilter = determineSchemaFilter( schema );
114-
115-
final List<TableInformation> results = new ArrayList<TableInformation>();
116-
117-
ResultSet resultSet = extractionContext.getJdbcDatabaseMetaData().getTables(
118-
catalogFilter,
119-
schemaFilter,
120-
null,
121-
tableTypes
122-
);
123-
try {
124-
while ( resultSet.next() ) {
125-
final TableInformation tableInformation = extractTableInformation(
126-
catalog,
127-
schema,
128-
null,
129-
resultSet
130-
);
131-
results.add( tableInformation );
132-
}
133-
}
134-
finally {
135-
try {
136-
resultSet.close();
137-
}
138-
catch (SQLException ignore) {
139-
}
140-
}
141-
142-
return results;
143-
}
144-
catch (SQLException sqlException) {
145-
throw convertSQLException( sqlException, "Error accessing table metadata" );
146-
}
147-
}
148-
149108
private String determineCatalogFilter(Identifier catalog) throws SQLException {
150109
Identifier identifierToUse = catalog;
151110
if ( identifierToUse == null ) {
@@ -170,13 +129,13 @@ public TableInformation extractTableInformation(
170129
Identifier name,
171130
ResultSet resultSet) throws SQLException {
172131
if ( catalog == null ) {
173-
catalog = identifierHelper().fromMetaDataCatalogName( resultSet.getString( "TABLE_CAT" ) );
132+
catalog = identifierHelper().toIdentifier( resultSet.getString( "TABLE_CAT" ) );
174133
}
175134
if ( schema == null ) {
176-
schema = identifierHelper().fromMetaDataSchemaName( resultSet.getString( "TABLE_SCHEM" ) );
135+
schema = identifierHelper().toIdentifier( resultSet.getString( "TABLE_SCHEM" ) );
177136
}
178137
if ( name == null ) {
179-
name = identifierHelper().fromMetaDataObjectName( resultSet.getString( "TABLE_NAME" ) );
138+
name = identifierHelper().toIdentifier( resultSet.getString( "TABLE_NAME" ) );
180139
}
181140

182141
final QualifiedTableName tableName = new QualifiedTableName( catalog, schema, name );
@@ -239,36 +198,31 @@ protected boolean isPhysicalTableType(String tableType) {
239198
}
240199

241200
@Override
242-
public Iterable<ColumnInformation> getColumns(TableInformation tableInformation) {
243-
final List<ColumnInformation> results = new ArrayList<ColumnInformation>();
244-
201+
public ColumnInformation getColumn(TableInformation tableInformation, Identifier columnIdentifier) {
245202
try {
246203
ResultSet resultSet = extractionContext.getJdbcDatabaseMetaData().getColumns(
247204
identifierHelper().toMetaDataCatalogName( tableInformation.getName().getCatalogName() ),
248205
identifierHelper().toMetaDataSchemaName( tableInformation.getName().getSchemaName() ),
249206
identifierHelper().toMetaDataObjectName( tableInformation.getName().getTableName() ),
250-
"%"
207+
extractionContext.getJdbcEnvironment()
208+
.getIdentifierHelper()
209+
.toMetaDataObjectName( columnIdentifier )
251210
);
252211

253212
try {
254-
while ( resultSet.next() ) {
255-
final String columnName = resultSet.getString( "COLUMN_NAME" );
256-
if ( columnName == null ) {
257-
continue;
258-
}
259-
260-
results.add(
261-
new ColumnInformationImpl(
262-
tableInformation,
263-
identifierHelper().fromMetaDataObjectName( columnName ),
264-
resultSet.getInt( "DATA_TYPE" ),
265-
new StringTokenizer( resultSet.getString( "TYPE_NAME" ), "() " ).nextToken(),
266-
resultSet.getInt( "COLUMN_SIZE" ),
267-
resultSet.getInt("DECIMAL_DIGITS"),
268-
interpretTruthValue( resultSet.getString( "IS_NULLABLE" ) )
269-
)
270-
);
213+
if ( !resultSet.next() ) {
214+
return null;
271215
}
216+
return new ColumnInformationImpl(
217+
tableInformation,
218+
identifierHelper().toIdentifier( resultSet.getString( "COLUMN_NAME" ) ),
219+
resultSet.getInt( "DATA_TYPE" ),
220+
new StringTokenizer( resultSet.getString( "TYPE_NAME" ), "() " ).nextToken(),
221+
resultSet.getInt( "COLUMN_SIZE" ),
222+
resultSet.getInt( "DECIMAL_DIGITS" ),
223+
interpretTruthValue( resultSet.getString( "IS_NULLABLE" ) )
224+
);
225+
272226
}
273227
finally {
274228
resultSet.close();
@@ -277,8 +231,6 @@ public Iterable<ColumnInformation> getColumns(TableInformation tableInformation)
277231
catch (SQLException e) {
278232
throw convertSQLException( e, "Error accessing column metadata: " + tableInformation.getName().toString() );
279233
}
280-
281-
return results;
282234
}
283235

284236
private TruthValue interpretTruthValue(String nullable) {
@@ -309,7 +261,7 @@ public PrimaryKeyInformation getPrimaryKey(TableInformationImpl tableInformation
309261
final String currentPkName = resultSet.getString( "PK_NAME" );
310262
final Identifier currentPkIdentifier = currentPkName == null
311263
? null
312-
: identifierHelper().fromMetaDataObjectName( currentPkName );
264+
: identifierHelper().toIdentifier( currentPkName );
313265
if ( firstPass ) {
314266
pkIdentifier = currentPkIdentifier;
315267
firstPass = false;
@@ -328,7 +280,7 @@ public PrimaryKeyInformation getPrimaryKey(TableInformationImpl tableInformation
328280
final int columnPosition = resultSet.getInt( "KEY_SEQ" );
329281
final String columnName = resultSet.getString( "COLUMN_NAME" );
330282

331-
final Identifier columnIdentifier = identifierHelper().fromMetaDataObjectName( columnName );
283+
final Identifier columnIdentifier = identifierHelper().toIdentifier( columnName );
332284
final ColumnInformation column = tableInformation.getColumn( columnIdentifier );
333285
pkColumns.add( columnPosition-1, column );
334286
}
@@ -377,7 +329,7 @@ public Iterable<IndexInformation> getIndexes(TableInformation tableInformation)
377329
continue;
378330
}
379331

380-
final Identifier indexIdentifier = identifierHelper().fromMetaDataObjectName(
332+
final Identifier indexIdentifier = identifierHelper().toIdentifier(
381333
resultSet.getString(
382334
"INDEX_NAME"
383335
)
@@ -388,7 +340,7 @@ public Iterable<IndexInformation> getIndexes(TableInformation tableInformation)
388340
builders.put( indexIdentifier, builder );
389341
}
390342

391-
final Identifier columnIdentifier = identifierHelper().fromMetaDataObjectName( resultSet.getString( "COLUMN_NAME" ) );
343+
final Identifier columnIdentifier = identifierHelper().toIdentifier( resultSet.getString( "COLUMN_NAME" ) );
392344
final ColumnInformation columnInformation = tableInformation.getColumn( columnIdentifier );
393345
if ( columnInformation == null ) {
394346
throw new SchemaManagementException(
@@ -433,7 +385,7 @@ public Iterable<ForeignKeyInformation> getForeignKeys(TableInformation tableInfo
433385
try {
434386
while ( resultSet.next() ) {
435387
// IMPL NOTE : The builder is mainly used to collect the column reference mappings
436-
final Identifier fkIdentifier = identifierHelper().fromMetaDataObjectName(
388+
final Identifier fkIdentifier = identifierHelper().toIdentifier(
437389
resultSet.getString( "FK_NAME" )
438390
);
439391
ForeignKeyBuilder fkBuilder = fkBuilders.get( fkIdentifier );
@@ -455,10 +407,10 @@ public Iterable<ForeignKeyInformation> getForeignKeys(TableInformation tableInfo
455407
continue;
456408
}
457409

458-
final Identifier fkColumnIdentifier = identifierHelper().fromMetaDataObjectName(
410+
final Identifier fkColumnIdentifier = identifierHelper().toIdentifier(
459411
resultSet.getString( "FKCOLUMN_NAME" )
460412
);
461-
final Identifier pkColumnIdentifier = identifierHelper().fromMetaDataObjectName(
413+
final Identifier pkColumnIdentifier = identifierHelper().toIdentifier(
462414
resultSet.getString( "PKCOLUMN_NAME" )
463415
);
464416

@@ -529,9 +481,9 @@ private QualifiedTableName extractKeyTableName(ResultSet resultSet, String prefi
529481
final String incomingTableName = resultSet.getString( prefix + "TABLE_NAME" );
530482

531483
return new QualifiedTableName(
532-
identifierHelper().fromMetaDataCatalogName( incomingCatalogName ),
533-
identifierHelper().fromMetaDataSchemaName( incomingSchemaName ),
534-
identifierHelper().fromMetaDataObjectName( incomingTableName )
484+
identifierHelper().toIdentifier( incomingCatalogName ),
485+
identifierHelper().toIdentifier( incomingSchemaName ),
486+
identifierHelper().toIdentifier( incomingTableName )
535487
);
536488
}
537489
}

hibernate-core/src/main/java/org/hibernate/tool/schema/extract/internal/SequenceInformationExtractorH2DatabaseImpl.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,13 @@ public Iterable<SequenceInformation> extractMetadata(ExtractionContext extractio
4444
sequenceInformationList.add(
4545
new SequenceInformationImpl(
4646
new QualifiedSequenceName(
47-
identifierHelper.fromMetaDataCatalogName(
47+
identifierHelper.toIdentifier(
4848
resultSet.getString( "SEQUENCE_CATALOG" )
4949
),
50-
identifierHelper.fromMetaDataSchemaName(
50+
identifierHelper.toIdentifier(
5151
resultSet.getString( "SEQUENCE_SCHEMA" )
5252
),
53-
identifierHelper.fromMetaDataObjectName(
53+
identifierHelper.toIdentifier(
5454
resultSet.getString( "SEQUENCE_NAME" )
5555
)
5656
),

hibernate-core/src/main/java/org/hibernate/tool/schema/extract/internal/SequenceInformationExtractorLegacyImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public Iterable<SequenceInformation> extractMetadata(ExtractionContext extractio
4848
new QualifiedSequenceName(
4949
null,
5050
null,
51-
identifierHelper.fromMetaDataObjectName(
51+
identifierHelper.toIdentifier(
5252
resultSet.getString( 1 )
5353
)
5454
),

hibernate-core/src/main/java/org/hibernate/tool/schema/extract/internal/TableInformationImpl.java

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ public class TableInformationImpl implements TableInformation {
3131
private final boolean physicalTable;
3232
private final String comment;
3333

34-
private Map<Identifier, ColumnInformation> columns;
3534
private PrimaryKeyInformation primaryKey;
3635
private Map<Identifier, ForeignKeyInformation> foreignKeys;
3736
private Map<Identifier, IndexInformation> indexes;
@@ -64,26 +63,9 @@ public String getComment() {
6463
return comment;
6564
}
6665

67-
@Override
68-
public Iterable<ColumnInformation> getColumns() {
69-
return columns().values();
70-
}
71-
72-
protected Map<Identifier, ColumnInformation> columns() {
73-
if ( this.columns == null ) {
74-
final Map<Identifier, ColumnInformation> columnMap = new HashMap<Identifier, ColumnInformation>();
75-
final Iterable<ColumnInformation> columnInformationItr = extractor.getColumns( this );
76-
for ( ColumnInformation columnInformation : columnInformationItr ) {
77-
columnMap.put( columnInformation.getColumnIdentifier(), columnInformation );
78-
}
79-
this.columns = columnMap;
80-
}
81-
return this.columns;
82-
}
83-
8466
@Override
8567
public ColumnInformation getColumn(Identifier columnIdentifier) {
86-
return columns().get( columnIdentifier );
68+
return extractor.getColumn( this, columnIdentifier );
8769
}
8870

8971
@Override

0 commit comments

Comments
 (0)