|
8 | 8 |
|
9 | 9 | import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment; |
10 | 10 |
|
| 11 | +import static java.lang.Character.isDigit; |
| 12 | +import static java.lang.Character.isLowerCase; |
| 13 | +import static java.lang.Character.isUpperCase; |
| 14 | + |
11 | 15 | /** |
12 | | - * Originally copied from Spring Boot as this strategy is popular there |
13 | | - * (original name is SpringPhysicalNamingStrategy). |
| 16 | + * Converts {@code camelCase} or {@code MixedCase} logical names to {@code snake_case}. |
14 | 17 | * |
15 | 18 | * @author Phillip Webb |
16 | 19 | * @author Madhura Bhave |
17 | 20 | */ |
| 21 | +// Originally copied from Spring's SpringPhysicalNamingStrategy as this strategy is popular there. |
18 | 22 | public class CamelCaseToUnderscoresNamingStrategy implements PhysicalNamingStrategy { |
19 | 23 |
|
20 | 24 | @Override |
21 | 25 | public Identifier toPhysicalCatalogName(Identifier logicalName, JdbcEnvironment jdbcEnvironment) { |
22 | | - return apply( logicalName, jdbcEnvironment ); |
| 26 | + return apply( logicalName ); |
23 | 27 | } |
24 | 28 |
|
25 | 29 | @Override |
26 | 30 | public Identifier toPhysicalSchemaName(Identifier logicalName, JdbcEnvironment jdbcEnvironment) { |
27 | | - return apply( logicalName, jdbcEnvironment ); |
| 31 | + return apply( logicalName ); |
28 | 32 | } |
29 | 33 |
|
30 | 34 | @Override |
31 | 35 | public Identifier toPhysicalTableName(Identifier logicalName, JdbcEnvironment jdbcEnvironment) { |
32 | | - return apply( logicalName, jdbcEnvironment ); |
| 36 | + return apply( logicalName ); |
33 | 37 | } |
34 | 38 |
|
35 | 39 | @Override |
36 | 40 | public Identifier toPhysicalSequenceName(Identifier logicalName, JdbcEnvironment jdbcEnvironment) { |
37 | | - return apply( logicalName, jdbcEnvironment ); |
| 41 | + return apply( logicalName ); |
38 | 42 | } |
39 | 43 |
|
40 | 44 | @Override |
41 | 45 | public Identifier toPhysicalColumnName(Identifier logicalName, JdbcEnvironment jdbcEnvironment) { |
42 | | - return apply( logicalName, jdbcEnvironment ); |
| 46 | + return apply( logicalName ); |
43 | 47 | } |
44 | 48 |
|
45 | | - private Identifier apply(final Identifier name, final JdbcEnvironment jdbcEnvironment) { |
| 49 | + private Identifier apply(final Identifier name) { |
46 | 50 | if ( name == null ) { |
47 | 51 | return null; |
48 | 52 | } |
49 | | - StringBuilder builder = new StringBuilder( name.getText().replace( '.', '_' ) ); |
| 53 | + else if ( name.isQuoted() ) { |
| 54 | + return quotedIdentifier( name ); |
| 55 | + } |
| 56 | + else { |
| 57 | + return unquotedIdentifier( name ); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + private String camelCaseToSnakeCase(String name) { |
| 62 | + final StringBuilder builder = new StringBuilder( name.replace( '.', '_' ) ); |
50 | 63 | for ( int i = 1; i < builder.length() - 1; i++ ) { |
51 | 64 | if ( isUnderscoreRequired( builder.charAt( i - 1 ), builder.charAt( i ), builder.charAt( i + 1 ) ) ) { |
52 | 65 | builder.insert( i++, '_' ); |
53 | 66 | } |
54 | 67 | } |
55 | | - return getIdentifier( builder.toString(), name.isQuoted(), jdbcEnvironment ); |
| 68 | + return builder.toString(); |
56 | 69 | } |
57 | 70 |
|
58 | | - /** |
59 | | - * Get an identifier for the specified details. By default this method will return an identifier |
60 | | - * with the name adapted based on the result of {@link #isCaseInsensitive(JdbcEnvironment)} |
61 | | - * |
62 | | - * @param name the name of the identifier |
63 | | - * @param quoted if the identifier is quoted |
64 | | - * @param jdbcEnvironment the JDBC environment |
65 | | - * |
66 | | - * @return an identifier instance |
67 | | - */ |
68 | | - protected Identifier getIdentifier(String name, final boolean quoted, final JdbcEnvironment jdbcEnvironment) { |
69 | | - if ( isCaseInsensitive( jdbcEnvironment ) ) { |
70 | | - name = name.toLowerCase( Locale.ROOT ); |
71 | | - } |
72 | | - return new Identifier( name, quoted ); |
| 71 | + protected Identifier unquotedIdentifier(Identifier name) { |
| 72 | + return new Identifier( camelCaseToSnakeCase( name.getText() ).toLowerCase( Locale.ROOT ) ); |
73 | 73 | } |
74 | 74 |
|
75 | | - /** |
76 | | - * Specify whether the database is case sensitive. |
77 | | - * |
78 | | - * @param jdbcEnvironment the JDBC environment which can be used to determine case |
79 | | - * |
80 | | - * @return true if the database is case insensitive sensitivity |
81 | | - */ |
82 | | - protected boolean isCaseInsensitive(JdbcEnvironment jdbcEnvironment) { |
83 | | - return true; |
| 75 | + protected Identifier quotedIdentifier(Identifier quotedName) { |
| 76 | + return quotedName; |
84 | 77 | } |
85 | 78 |
|
86 | 79 | private boolean isUnderscoreRequired(final char before, final char current, final char after) { |
87 | | - return ( Character.isLowerCase( before ) || Character.isDigit( before ) ) && Character.isUpperCase( current ) && ( Character.isLowerCase( |
88 | | - after ) || Character.isDigit( after ) ); |
| 80 | + return ( isLowerCase( before ) || isDigit( before ) ) |
| 81 | + && isUpperCase( current ) |
| 82 | + && ( isLowerCase( after ) || isDigit( after ) ); |
89 | 83 | } |
90 | 84 |
|
91 | 85 | } |
0 commit comments