Created
July 12, 2015 13:44
-
-
Save knalli/bbf83af3aad2320f3aab to your computer and use it in GitHub Desktop.
Hibernate 5 PhysicalNamingStrategy (untested), Java8
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package de.knallisworld.hibernate5; | |
import org.hibernate.boot.model.naming.Identifier; | |
import org.hibernate.boot.model.naming.PhysicalNamingStrategy; | |
import org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl; | |
import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment; | |
import java.util.Optional; | |
import java.util.StringTokenizer; | |
/** | |
* TODO: link to origin/reference of "OracleNamingStrategy" | |
*/ | |
public class OraclePhysicalNamingStrategy extends PhysicalNamingStrategyStandardImpl implements PhysicalNamingStrategy { | |
private static final int MAX_LENGTH = 30; | |
private static final String VOWELS = "AEIOUaeiou"; | |
private static String abbreviate(final String token) { | |
boolean vowelFound = false; | |
for (int i = token.length() - 1; i >= 0; i--) { | |
if (!vowelFound) { | |
vowelFound = VOWELS.contains(String.valueOf(token.charAt(i))); | |
} else if (!VOWELS.contains(String.valueOf(token.charAt(i)))) { | |
return token.substring(0, i + 1); | |
} | |
} | |
return ""; | |
} | |
private static String abbreviateName(final String someName) { | |
if (someName.length() <= MAX_LENGTH) { | |
return someName; | |
} | |
final String[] tokens = splitName(someName); | |
shortenName(someName, tokens); | |
return assembleResults(tokens); | |
} | |
public static Identifier abbreviateName(final Identifier identifier) { | |
return Optional.ofNullable(identifier) | |
.map(_identifier -> Identifier.toIdentifier(abbreviateName(_identifier.render()), identifier.isQuoted())) | |
.orElse(null); | |
} | |
private static String assembleResults(final String[] tokens) { | |
final StringBuilder result = new StringBuilder(tokens[0]); | |
for (int j = 1; j < tokens.length; j++) { | |
result.append('_').append(tokens[j]); | |
} | |
return result.toString(); | |
} | |
private static int getIndexOfLongest(final String[] tokens) { | |
int maxLength = 0; | |
int index = -1; | |
for (int i = 0; i < tokens.length; i++) { | |
final String string = tokens[i]; | |
if (maxLength < string.length()) { | |
maxLength = string.length(); | |
index = i; | |
} | |
} | |
return index; | |
} | |
private static void shortenName(final String someName, final String[] tokens) { | |
int currentLength = someName.length(); | |
while (currentLength > MAX_LENGTH) { | |
final int tokenIndex = getIndexOfLongest(tokens); | |
final String oldToken = tokens[tokenIndex]; | |
tokens[tokenIndex] = abbreviate(oldToken); | |
// Reduce size about saved/removed chars. | |
currentLength -= oldToken.length() - tokens[tokenIndex].length(); | |
} | |
} | |
private static String[] splitName(final String someName) { | |
final StringTokenizer toki = new StringTokenizer(someName, "_"); | |
final String[] tokens = new String[toki.countTokens()]; | |
int i = 0; | |
while (toki.hasMoreTokens()) { | |
tokens[i] = toki.nextToken(); | |
i++; | |
} | |
return tokens; | |
} | |
@Override | |
public Identifier toPhysicalColumnName(final Identifier name, final JdbcEnvironment context) { | |
return abbreviateName(super.toPhysicalColumnName(name, context)); | |
} | |
@Override | |
public Identifier toPhysicalCatalogName(final Identifier name, final JdbcEnvironment context) { | |
return abbreviateName(super.toPhysicalCatalogName(name, context)); | |
} | |
@Override | |
public Identifier toPhysicalSchemaName(final Identifier name, final JdbcEnvironment context) { | |
return abbreviateName(super.toPhysicalSchemaName(name, context)); | |
} | |
@Override | |
public Identifier toPhysicalSequenceName(final Identifier name, final JdbcEnvironment context) { | |
return abbreviateName(super.toPhysicalSequenceName(name, context)); | |
} | |
@Override | |
public Identifier toPhysicalTableName(final Identifier name, final JdbcEnvironment context) { | |
return abbreviateName(super.toPhysicalTableName(name, context)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment