DATAJDBC-207 - Default NamingStrategy is now Snake Case.
Moved the implementation from the DelimiterNamingStrategy into the NamingStrategy. Dropped the support for different separators, since there is no good way to support it in the default implementations of an interface. A getSeparator() method would bleed into the public API. Also the added value of that flexibility seems limited. During migration of the various test it became obvious that SqlGeneratorUnitTests was broken since test failures happend on a worker thread not on the main test thread. This is fixed as well with this commit.
This commit is contained in:
committed by
Greg Turnquist
parent
0460f2f509
commit
b4b6625d50
@@ -1,71 +0,0 @@
|
||||
/*
|
||||
* Copyright 2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.jdbc.core.mapping;
|
||||
|
||||
import org.springframework.data.util.ParsingUtils;
|
||||
|
||||
/**
|
||||
* The delimiter character implementation of {@link NamingStrategy} with no schema, table based on {@link Class} and
|
||||
* column name based on {@link JdbcPersistentProperty}. The default delimiter is '_', resulting in snake case.
|
||||
*
|
||||
* @author Kazuki Shimizu
|
||||
* @author Jens Schauder
|
||||
* @since 1.0
|
||||
*/
|
||||
public class DelimiterNamingStrategy implements NamingStrategy {
|
||||
|
||||
private final String delimiter;
|
||||
|
||||
/**
|
||||
* Construct a instance with '_' as delimiter. This results in a snake case naming strategy.
|
||||
*/
|
||||
public DelimiterNamingStrategy() {
|
||||
this("_");
|
||||
}
|
||||
|
||||
/**
|
||||
* Construct a instance with specified delimiter.
|
||||
*
|
||||
* @param delimiter a delimiter character
|
||||
*/
|
||||
public DelimiterNamingStrategy(String delimiter) {
|
||||
this.delimiter = delimiter;
|
||||
}
|
||||
|
||||
/**
|
||||
* Look up the {@link Class}'s simple name after converting to separated word using with {@code delimiter}.
|
||||
*/
|
||||
@Override
|
||||
public String getTableName(Class<?> type) {
|
||||
return ParsingUtils.reconcatenateCamelCase(NamingStrategy.super.getTableName(type), delimiter);
|
||||
}
|
||||
|
||||
/**
|
||||
* Look up the {@link JdbcPersistentProperty}'s name after converting to separated word using with {@code delimiter}.
|
||||
*/
|
||||
@Override
|
||||
public String getColumnName(JdbcPersistentProperty property) {
|
||||
return ParsingUtils.reconcatenateCamelCase(NamingStrategy.super.getColumnName(property), delimiter);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the value that adding {@code delimiter} + 'key' for returned value of {@link #getReverseColumnName}.
|
||||
*/
|
||||
@Override
|
||||
public String getKeyColumn(JdbcPersistentProperty property) {
|
||||
return getReverseColumnName(property) + delimiter + "key";
|
||||
}
|
||||
}
|
||||
@@ -15,9 +15,12 @@
|
||||
*/
|
||||
package org.springframework.data.jdbc.core.mapping;
|
||||
|
||||
import org.springframework.data.util.ParsingUtils;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* Interface and default implementation of a naming strategy. Defaults to no schema, table name based on {@link Class}
|
||||
* and column name based on {@link JdbcPersistentProperty}.
|
||||
* and column name based on {@link JdbcPersistentProperty} with name parts of both separated by '_'.
|
||||
* <p>
|
||||
* NOTE: Can also be used as an adapter. Create a lambda or an anonymous subclass and override any settings to implement
|
||||
* a different strategy on the fly.
|
||||
@@ -25,6 +28,7 @@ package org.springframework.data.jdbc.core.mapping;
|
||||
* @author Greg Turnquist
|
||||
* @author Michael Simons
|
||||
* @author Kazuki Shimizu
|
||||
* @author Jens Schauder
|
||||
* @author Oliver Gierke
|
||||
* @since 1.0
|
||||
*/
|
||||
@@ -47,17 +51,24 @@ public interface NamingStrategy {
|
||||
}
|
||||
|
||||
/**
|
||||
* Defaults to returning the given type's simple name.
|
||||
* The name of the table to be used for persisting entities having the type passed as an argument. The default
|
||||
* implementation takes the {@code type.getSimpleName()} and separates camel case parts with '_'.
|
||||
*/
|
||||
default String getTableName(Class<?> type) {
|
||||
return type.getSimpleName();
|
||||
|
||||
Assert.notNull(type, "Type must not be null.");
|
||||
|
||||
return ParsingUtils.reconcatenateCamelCase(type.getSimpleName(), "_");
|
||||
}
|
||||
|
||||
/**
|
||||
* Defaults to return the given {@link JdbcPersistentProperty}'s name;
|
||||
* Defaults to return the given {@link JdbcPersistentProperty}'s name with the parts of a camel case name separated by '_';
|
||||
*/
|
||||
default String getColumnName(JdbcPersistentProperty property) {
|
||||
return property.getName();
|
||||
|
||||
Assert.notNull(property, "Property must not be null.");
|
||||
|
||||
return ParsingUtils.reconcatenateCamelCase(property.getName(), "_");
|
||||
}
|
||||
|
||||
default String getQualifiedTableName(Class<?> type) {
|
||||
@@ -71,6 +82,9 @@ public interface NamingStrategy {
|
||||
* @return a column name. Must not be {@code null}.
|
||||
*/
|
||||
default String getReverseColumnName(JdbcPersistentProperty property) {
|
||||
|
||||
Assert.notNull(property,"Property must not be null.");
|
||||
|
||||
return property.getOwner().getTableName();
|
||||
}
|
||||
|
||||
@@ -81,6 +95,9 @@ public interface NamingStrategy {
|
||||
* @return name of the key column. Must not be {@code null}.
|
||||
*/
|
||||
default String getKeyColumn(JdbcPersistentProperty property) {
|
||||
|
||||
Assert.notNull(property, "Property must not be null.");
|
||||
|
||||
return getReverseColumnName(property) + "_key";
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user