HHH-14208 simplify example naming strategy code in user guide
This commit is contained in:
parent
4499abd9a9
commit
97ea1d853d
|
@ -6,14 +6,15 @@
|
||||||
*/
|
*/
|
||||||
package org.hibernate.userguide.naming;
|
package org.hibernate.userguide.naming;
|
||||||
|
|
||||||
import java.util.LinkedList;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.TreeMap;
|
import java.util.TreeMap;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
import org.hibernate.boot.model.naming.Identifier;
|
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 org.hibernate.engine.jdbc.env.spi.JdbcEnvironment;
|
||||||
|
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
@ -27,40 +28,35 @@ import org.apache.commons.lang3.StringUtils;
|
||||||
* Additionally standards call for the replacement of certain words with abbreviations.
|
* Additionally standards call for the replacement of certain words with abbreviations.
|
||||||
*
|
*
|
||||||
* @author Steve Ebersole
|
* @author Steve Ebersole
|
||||||
|
* @author Nathan Xu
|
||||||
*/
|
*/
|
||||||
public class AcmeCorpPhysicalNamingStrategy implements PhysicalNamingStrategy {
|
public class AcmeCorpPhysicalNamingStrategy extends PhysicalNamingStrategyStandardImpl {
|
||||||
private static final Map<String,String> ABBREVIATIONS = buildAbbreviationMap();
|
private static final Map<String, String> ABBREVIATIONS;
|
||||||
|
|
||||||
@Override
|
static {
|
||||||
public Identifier toPhysicalCatalogName(Identifier name, JdbcEnvironment jdbcEnvironment) {
|
ABBREVIATIONS = new TreeMap<>( String.CASE_INSENSITIVE_ORDER );
|
||||||
// Acme naming standards do not apply to catalog names
|
ABBREVIATIONS.put( "account", "acct" );
|
||||||
return name;
|
ABBREVIATIONS.put( "number", "num" );
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Identifier toPhysicalSchemaName(Identifier name, JdbcEnvironment jdbcEnvironment) {
|
|
||||||
// Acme naming standards do not apply to schema names
|
|
||||||
return name;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Identifier toPhysicalTableName(Identifier name, JdbcEnvironment jdbcEnvironment) {
|
public Identifier toPhysicalTableName(Identifier name, JdbcEnvironment jdbcEnvironment) {
|
||||||
final List<String> parts = splitAndReplace( name.getText() );
|
final List<String> parts = splitAndReplace( name.getText() );
|
||||||
return jdbcEnvironment.getIdentifierHelper().toIdentifier(
|
return jdbcEnvironment.getIdentifierHelper().toIdentifier(
|
||||||
join( parts ),
|
StringUtils.join( parts, '_' ),
|
||||||
name.isQuoted()
|
name.isQuoted()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Identifier toPhysicalSequenceName(Identifier name, JdbcEnvironment jdbcEnvironment) {
|
public Identifier toPhysicalSequenceName(Identifier name, JdbcEnvironment jdbcEnvironment) {
|
||||||
final LinkedList<String> parts = splitAndReplace( name.getText() );
|
final List<String> parts = splitAndReplace( name.getText() );
|
||||||
// Acme Corp says all sequences should end with _seq
|
// Acme Corp says all sequences should end with _seq
|
||||||
if ( !"seq".equalsIgnoreCase( parts.getLast() ) ) {
|
if ( !"seq".equals( parts.get( parts.size() - 1 ) ) ) {
|
||||||
parts.add( "seq" );
|
parts.add( "seq" );
|
||||||
}
|
}
|
||||||
return jdbcEnvironment.getIdentifierHelper().toIdentifier(
|
return jdbcEnvironment.getIdentifierHelper().toIdentifier(
|
||||||
join( parts ),
|
StringUtils.join( parts, '_' ),
|
||||||
name.isQuoted()
|
name.isQuoted()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -69,50 +65,15 @@ public class AcmeCorpPhysicalNamingStrategy implements PhysicalNamingStrategy {
|
||||||
public Identifier toPhysicalColumnName(Identifier name, JdbcEnvironment jdbcEnvironment) {
|
public Identifier toPhysicalColumnName(Identifier name, JdbcEnvironment jdbcEnvironment) {
|
||||||
final List<String> parts = splitAndReplace( name.getText() );
|
final List<String> parts = splitAndReplace( name.getText() );
|
||||||
return jdbcEnvironment.getIdentifierHelper().toIdentifier(
|
return jdbcEnvironment.getIdentifierHelper().toIdentifier(
|
||||||
join( parts ),
|
StringUtils.join( parts, '_' ),
|
||||||
name.isQuoted()
|
name.isQuoted()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Map<String, String> buildAbbreviationMap() {
|
private List<String> splitAndReplace(String name) {
|
||||||
TreeMap<String,String> abbreviationMap = new TreeMap<> ( String.CASE_INSENSITIVE_ORDER );
|
return Arrays.stream( StringUtils.splitByCharacterTypeCamelCase( name ) )
|
||||||
abbreviationMap.put( "account", "acct" );
|
.filter( StringUtils::isNotBlank )
|
||||||
abbreviationMap.put( "number", "num" );
|
.map( p -> ABBREVIATIONS.getOrDefault( p, p ).toLowerCase( Locale.ROOT ) )
|
||||||
return abbreviationMap;
|
.collect( Collectors.toList() );
|
||||||
}
|
|
||||||
|
|
||||||
private LinkedList<String> splitAndReplace(String name) {
|
|
||||||
LinkedList<String> result = new LinkedList<>();
|
|
||||||
for ( String part : StringUtils.splitByCharacterTypeCamelCase( name ) ) {
|
|
||||||
if ( part == null || part.trim().isEmpty() ) {
|
|
||||||
// skip null and space
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
part = applyAbbreviationReplacement( part );
|
|
||||||
result.add( part.toLowerCase( Locale.ROOT ) );
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
private String applyAbbreviationReplacement(String word) {
|
|
||||||
if ( ABBREVIATIONS.containsKey( word ) ) {
|
|
||||||
return ABBREVIATIONS.get( word );
|
|
||||||
}
|
|
||||||
|
|
||||||
return word;
|
|
||||||
}
|
|
||||||
|
|
||||||
private String join(List<String> parts) {
|
|
||||||
boolean firstPass = true;
|
|
||||||
String separator = "";
|
|
||||||
StringBuilder joined = new StringBuilder();
|
|
||||||
for ( String part : parts ) {
|
|
||||||
joined.append( separator ).append( part );
|
|
||||||
if ( firstPass ) {
|
|
||||||
firstPass = false;
|
|
||||||
separator = "_";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return joined.toString();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue