Skip to content

Commit

Permalink
miscellaneous code cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
gavinking committed Nov 4, 2022
1 parent 0d2aa57 commit 3d9bf07
Show file tree
Hide file tree
Showing 4 changed files with 199 additions and 240 deletions.
58 changes: 24 additions & 34 deletions hibernate-core/src/main/java/org/hibernate/mapping/Constraint.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import org.hibernate.HibernateException;
import org.hibernate.MappingException;
import org.hibernate.Remove;
import org.hibernate.boot.model.relational.Exportable;
import org.hibernate.boot.model.relational.SqlStringGenerationContext;
import org.hibernate.dialect.Dialect;
Expand Down Expand Up @@ -52,17 +53,15 @@ public void setName(String name) {
public static String generateName(String prefix, Table table, Column... columns) {
// Use a concatenation that guarantees uniqueness, even if identical names
// exist between all table and column identifiers.

StringBuilder sb = new StringBuilder( "table`" + table.getName() + "`" );

final StringBuilder sb = new StringBuilder( "table`" + table.getName() + "`" );
// Ensure a consistent ordering of columns, regardless of the order
// they were bound.
// Clone the list, as sometimes a set of order-dependent Column
// bindings are given.
Column[] alphabeticalColumns = columns.clone();
Arrays.sort( alphabeticalColumns, ColumnComparator.INSTANCE );
final Column[] alphabeticalColumns = columns.clone();
Arrays.sort( alphabeticalColumns, Comparator.comparing( Column::getName ) );
for ( Column column : alphabeticalColumns ) {
String columnName = column == null ? "" : column.getName();
final String columnName = column == null ? "" : column.getName();
sb.append( "column`" ).append( columnName ).append( "`" );
}
return prefix + hashedName( sb.toString() );
Expand All @@ -74,17 +73,14 @@ public static String generateName(String prefix, Table table, Column... columns)
* @return String The generated name
*/
public static String generateName(String prefix, Table table, List<Column> columns) {
//N.B. legacy APIs are involved: can't trust that the columns List is actually
//containing Column instances - the generic type isn't consistently enforced.
ArrayList<Column> defensive = new ArrayList<>( columns.size() );
for ( Object o : columns ) {
if ( o instanceof Column ) {
defensive.add( (Column) o );
}
// else: others might be Formula instances.
// They don't need to be part of the name generation.
}
return generateName( prefix, table, defensive.toArray( new Column[0] ) );
// N.B. legacy APIs are involved: can't trust that the columns List is actually
// containing Column instances - the generic type isn't consistently enforced.
// So some elements might be Formula instances, but they don't need to be part
// of the name generation.
final Column[] defensive = columns.stream()
.filter( (Object thing) -> thing instanceof Column )
.toArray( Column[]::new );
return generateName( prefix, table, defensive);
}

/**
Expand All @@ -93,17 +89,16 @@ public static String generateName(String prefix, Table table, List<Column> colum
* that the length of the name will always be smaller than the 30
* character identifier restriction enforced by a few dialects.
*
* @param s
* The name to be hashed.
* @param name The name to be hashed.
* @return String The hashed name.
*/
public static String hashedName(String s) {
public static String hashedName(String name) {
try {
MessageDigest md = MessageDigest.getInstance( "MD5" );
final MessageDigest md = MessageDigest.getInstance( "MD5" );
md.reset();
md.update( s.getBytes() );
byte[] digest = md.digest();
BigInteger bigInt = new BigInteger( 1, digest );
md.update( name.getBytes() );
final byte[] digest = md.digest();
final BigInteger bigInt = new BigInteger( 1, digest );
// By converting to base 35 (full alphanumeric), we guarantee
// that the length of the name will always be smaller than the 30
// character identifier restriction enforced by a few dialects.
Expand All @@ -114,14 +109,6 @@ public static String hashedName(String s) {
}
}

private static class ColumnComparator implements Comparator<Column> {
public static ColumnComparator INSTANCE = new ColumnComparator();

public int compare(Column col1, Column col2) {
return col1.getName().compareTo( col2.getName() );
}
}

public void addColumn(Column column) {
if ( !columns.contains( column ) ) {
columns.add( column );
Expand Down Expand Up @@ -151,7 +138,7 @@ public int getColumnSpan() {
}

public Column getColumn(int i) {
return columns.get( i );
return columns.get( i );
}

@Deprecated(since = "6.0")
Expand All @@ -175,7 +162,10 @@ public List<Column> getColumns() {
return columns;
}

@Deprecated(since="6.2")
/**
* @deprecated this method is no longer called
*/
@Deprecated(since="6.2") @Remove
public abstract String sqlConstraintString(
SqlStringGenerationContext context,
String constraintName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,18 +88,12 @@ public Column getColumn(Identifier name) {

@Override @Deprecated
public Iterator<Column> getColumnIterator() {
return new JoinedIterator<>(
includedTable.getColumnIterator(),
super.getColumnIterator()
);
return new JoinedIterator<>( includedTable.getColumnIterator(), super.getColumnIterator() );
}

@Override
public Collection<Column> getColumns() {
return new JoinedList<>(
new ArrayList<>( includedTable.getColumns() ),
new ArrayList<>( super.getColumns() )
);
return new JoinedList<>( new ArrayList<>( includedTable.getColumns() ), new ArrayList<>( super.getColumns() ) );
}

@Override
Expand All @@ -112,7 +106,7 @@ public PrimaryKey getPrimaryKey() {
return includedTable.getPrimaryKey();
}

@Override
@Override @Deprecated
public Iterator<UniqueKey> getUniqueKeyIterator() {
if ( !includedTable.isPhysicalTable() ) {
for ( UniqueKey uniqueKey : includedTable.getUniqueKeys().values() ) {
Expand All @@ -122,7 +116,7 @@ public Iterator<UniqueKey> getUniqueKeyIterator() {
return getUniqueKeys().values().iterator();
}

@Override
@Override @Deprecated
public Iterator<Index> getIndexIterator() {
final List<Index> indexes = new ArrayList<>();
for ( Index parentIndex : includedTable.getIndexes().values() ) {
Expand All @@ -132,10 +126,7 @@ public Iterator<Index> getIndexIterator() {
index.addColumns( parentIndex.getColumns() );
indexes.add( index );
}
return new JoinedIterator<>(
indexes.iterator(),
super.getIndexIterator()
);
return new JoinedIterator<>( indexes.iterator(), super.getIndexIterator() );
}

public Table getIncludedTable() {
Expand Down

0 comments on commit 3d9bf07

Please sign in to comment.