Allow overriding case in SpringPhysicalNamingStrategy

Fixes gh-7410
pull/7634/head
Madhura Bhave 8 years ago
parent 5d7453a635
commit b29620f2ad

@ -27,6 +27,7 @@ import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment;
* conventions. * conventions.
* *
* @author Phillip Webb * @author Phillip Webb
* @author Madhura Bhave
* @since 1.4.0 * @since 1.4.0
*/ */
public class SpringPhysicalNamingStrategy implements PhysicalNamingStrategy { public class SpringPhysicalNamingStrategy implements PhysicalNamingStrategy {
@ -34,45 +35,59 @@ public class SpringPhysicalNamingStrategy implements PhysicalNamingStrategy {
@Override @Override
public Identifier toPhysicalCatalogName(Identifier name, public Identifier toPhysicalCatalogName(Identifier name,
JdbcEnvironment jdbcEnvironment) { JdbcEnvironment jdbcEnvironment) {
return apply(name); return apply(name, jdbcEnvironment);
} }
@Override @Override
public Identifier toPhysicalSchemaName(Identifier name, public Identifier toPhysicalSchemaName(Identifier name,
JdbcEnvironment jdbcEnvironment) { JdbcEnvironment jdbcEnvironment) {
return apply(name); return apply(name, jdbcEnvironment);
} }
@Override @Override
public Identifier toPhysicalTableName(Identifier name, public Identifier toPhysicalTableName(Identifier name,
JdbcEnvironment jdbcEnvironment) { JdbcEnvironment jdbcEnvironment) {
return apply(name); return apply(name, jdbcEnvironment);
} }
@Override @Override
public Identifier toPhysicalSequenceName(Identifier name, public Identifier toPhysicalSequenceName(Identifier name,
JdbcEnvironment jdbcEnvironment) { JdbcEnvironment jdbcEnvironment) {
return apply(name); return apply(name, jdbcEnvironment);
} }
@Override @Override
public Identifier toPhysicalColumnName(Identifier name, public Identifier toPhysicalColumnName(Identifier name,
JdbcEnvironment jdbcEnvironment) { JdbcEnvironment jdbcEnvironment) {
return apply(name); return apply(name, jdbcEnvironment);
} }
private Identifier apply(Identifier name) { private Identifier apply(Identifier name, JdbcEnvironment jdbcEnvironment) {
if (name == null) { if (name == null) {
return null; return null;
} }
StringBuilder text = new StringBuilder(name.getText().replace('.', '_')); StringBuilder builder = new StringBuilder(name.getText().replace('.', '_'));
for (int i = 1; i < text.length() - 1; i++) { for (int i = 1; i < builder.length() - 1; i++) {
if (isUnderscoreRequired(text.charAt(i - 1), text.charAt(i), if (isUnderscoreRequired(builder.charAt(i - 1), builder.charAt(i),
text.charAt(i + 1))) { builder.charAt(i + 1))) {
text.insert(i++, '_'); builder.insert(i++, '_');
} }
} }
return new Identifier(text.toString().toLowerCase(Locale.ROOT), name.isQuoted());
String text = builder.toString();
String finalText = isCaseInsensitive(jdbcEnvironment) ? text.toLowerCase(Locale.ROOT)
: text;
return new Identifier(finalText, name.isQuoted());
}
/**
* Specify whether the database is case sensitive.
* @param jdbcEnvironment The JDBC environment which can be used to determine case
* @return true if the database is case insensitive
* sensitivity
*/
protected boolean isCaseInsensitive(JdbcEnvironment jdbcEnvironment) {
return true;
} }
private boolean isUnderscoreRequired(char before, char current, char after) { private boolean isUnderscoreRequired(char before, char current, char after) {

@ -23,6 +23,7 @@ import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.AvailableSettings; import org.hibernate.cfg.AvailableSettings;
import org.hibernate.dialect.H2Dialect; import org.hibernate.dialect.H2Dialect;
import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment;
import org.hibernate.mapping.PersistentClass; import org.hibernate.mapping.PersistentClass;
import org.hibernate.service.ServiceRegistry; import org.hibernate.service.ServiceRegistry;
import org.junit.Before; import org.junit.Before;
@ -34,17 +35,22 @@ import static org.assertj.core.api.Assertions.assertThat;
* Tests for {@link SpringPhysicalNamingStrategy}. * Tests for {@link SpringPhysicalNamingStrategy}.
* *
* @author Phillip Webb * @author Phillip Webb
* @author Madhura Bhave
*/ */
public class SpringPhysicalNamingStrategyTests { public class SpringPhysicalNamingStrategyTests {
private Metadata metadata; private Metadata metadata;
private MetadataSources metadataSources;
private StandardServiceRegistry serviceRegistry;
@Before @Before
public void setup() throws Exception { public void setup() throws Exception {
MetadataSources metadataSources = new MetadataSources(); this.metadataSources = new MetadataSources();
metadataSources.addAnnotatedClass(TelephoneNumber.class); this.metadataSources.addAnnotatedClass(TelephoneNumber.class);
StandardServiceRegistry serviceRegistry = getServiceRegistry(metadataSources); this.serviceRegistry = getServiceRegistry(this.metadataSources);
this.metadata = metadataSources.getMetadataBuilder(serviceRegistry) this.metadata = this.metadataSources.getMetadataBuilder(this.serviceRegistry)
.applyPhysicalNamingStrategy(new SpringPhysicalNamingStrategy()).build(); .applyPhysicalNamingStrategy(new SpringPhysicalNamingStrategy()).build();
} }
@ -61,4 +67,21 @@ public class SpringPhysicalNamingStrategyTests {
assertThat(binding.getTable().getQuotedName()).isEqualTo("telephone_number"); assertThat(binding.getTable().getQuotedName()).isEqualTo("telephone_number");
} }
@Test
public void tableNameShouldNotBeLowerCaseIfCaseSensitive() throws Exception {
this.metadata = this.metadataSources.getMetadataBuilder(this.serviceRegistry)
.applyPhysicalNamingStrategy(new TestSpringPhysicalNamingStrategy()).build();
PersistentClass binding = this.metadata
.getEntityBinding(TelephoneNumber.class.getName());
assertThat(binding.getTable().getQuotedName()).isEqualTo("Telephone_Number");
}
private class TestSpringPhysicalNamingStrategy extends SpringPhysicalNamingStrategy {
@Override
protected boolean isCaseInsensitive(JdbcEnvironment jdbcEnvironment) {
return false;
}
}
} }

Loading…
Cancel
Save