HHH-14837 - Move to Jakarta EE

improved support for schema tooling settings
This commit is contained in:
Steve Ebersole 2021-09-24 12:43:26 -05:00
parent 679a919203
commit e422d913db
3 changed files with 102 additions and 46 deletions

View File

@ -6,6 +6,7 @@
*/ */
package org.hibernate.internal.util; package org.hibernate.internal.util;
import java.util.function.Function;
import java.util.function.Supplier; import java.util.function.Supplier;
/** /**
@ -66,20 +67,36 @@ public class NullnessHelper {
*/ */
@SafeVarargs @SafeVarargs
public static <T> T coalesceSuppliedValues(Supplier<T>... valueSuppliers) { public static <T> T coalesceSuppliedValues(Supplier<T>... valueSuppliers) {
return coalesceSuppliedValues(
(value) -> ( value instanceof String && StringHelper.isNotEmpty( (String) value ) )
|| value != null,
valueSuppliers
);
}
/**
* Operates like SQL coalesce expression, returning the first non-empty value
*
* @implNote This impl treats empty strings (`""`) as null.
*
* @param valueSuppliers List of value Suppliers
* @param <T> Generic type of values to coalesce
*
* @return The first non-empty value, or null if all values were empty
*/
@SafeVarargs
public static <T> T coalesceSuppliedValues(Function<T,Boolean> checker, Supplier<T>... valueSuppliers) {
if ( valueSuppliers == null ) { if ( valueSuppliers == null ) {
return null; return null;
} }
for ( Supplier<T> valueSupplier : valueSuppliers ) { for ( Supplier<T> valueSupplier : valueSuppliers ) {
if ( valueSupplier != null ) { if ( valueSupplier != null ) {
final T value = valueSupplier.get(); final T value = valueSupplier.get();
if ( value instanceof String ) { if ( checker.apply( value ) ) {
if ( StringHelper.isNotEmpty( (String) value ) ) {
return value;
}
}
else if ( value != null ) {
return value; return value;
} }
} }
} }

View File

@ -51,9 +51,19 @@ import org.hibernate.tool.schema.spi.TargetDescriptor;
import org.jboss.logging.Logger; import org.jboss.logging.Logger;
import static org.hibernate.cfg.AvailableSettings.DIALECT_DB_NAME;
import static org.hibernate.cfg.AvailableSettings.DIALECT_DB_VERSION;
import static org.hibernate.cfg.AvailableSettings.HBM2DDL_CONNECTION; import static org.hibernate.cfg.AvailableSettings.HBM2DDL_CONNECTION;
import static org.hibernate.cfg.AvailableSettings.HBM2DDL_DB_MAJOR_VERSION;
import static org.hibernate.cfg.AvailableSettings.HBM2DDL_DB_MINOR_VERSION;
import static org.hibernate.cfg.AvailableSettings.HBM2DDL_DELIMITER; import static org.hibernate.cfg.AvailableSettings.HBM2DDL_DELIMITER;
import static org.hibernate.cfg.AvailableSettings.JAKARTA_DIALECT_DB_VERSION;
import static org.hibernate.cfg.AvailableSettings.JAKARTA_HBM2DDL_CONNECTION; import static org.hibernate.cfg.AvailableSettings.JAKARTA_HBM2DDL_CONNECTION;
import static org.hibernate.cfg.AvailableSettings.JAKARTA_HBM2DDL_DB_MAJOR_VERSION;
import static org.hibernate.cfg.AvailableSettings.JAKARTA_HBM2DDL_DB_MINOR_VERSION;
import static org.hibernate.cfg.AvailableSettings.JAKARTA_HBM2DDL_DB_NAME;
import static org.hibernate.internal.log.DeprecationLogger.DEPRECATION_LOGGER;
import static org.hibernate.internal.util.NullnessHelper.coalesceSuppliedValues;
/** /**
* The standard Hibernate implementation for performing schema management. * The standard Hibernate implementation for performing schema management.
@ -208,66 +218,91 @@ public class HibernateSchemaManagementTool implements SchemaManagementTool, Serv
final JdbcContextBuilder jdbcContextBuilder = new JdbcContextBuilder( serviceRegistry ); final JdbcContextBuilder jdbcContextBuilder = new JdbcContextBuilder( serviceRegistry );
// see if a specific connection has been provided // see if a specific connection has been provided
final Connection providedConnection = (Connection) configurationValues.get( HBM2DDL_CONNECTION ); final Connection providedConnection = (Connection) coalesceSuppliedValues(
() -> configurationValues.get( JAKARTA_HBM2DDL_CONNECTION ),
() -> {
final Object value = configurationValues.get( HBM2DDL_CONNECTION );
if ( value != null ) {
DEPRECATION_LOGGER.deprecatedSetting( HBM2DDL_CONNECTION, JAKARTA_HBM2DDL_CONNECTION );
}
return value;
}
);
if ( providedConnection != null ) { if ( providedConnection != null ) {
jdbcContextBuilder.jdbcConnectionAccess = new JdbcConnectionAccessProvidedConnectionImpl( providedConnection ); jdbcContextBuilder.jdbcConnectionAccess = new JdbcConnectionAccessProvidedConnectionImpl( providedConnection );
} }
else {
final Connection jakartaProvidedConnection = (Connection) configurationValues.get( JAKARTA_HBM2DDL_CONNECTION );
if ( jakartaProvidedConnection != null ) {
jdbcContextBuilder.jdbcConnectionAccess = new JdbcConnectionAccessProvidedConnectionImpl( jakartaProvidedConnection );
}
}
// see if a specific Dialect override has been provided... // see if a specific Dialect override has been provided through database name, version, etc
String dbName = (String) configurationValues.get( AvailableSettings.DIALECT_DB_NAME ); final String dbName = (String) coalesceSuppliedValues(
if ( dbName == null ) { () -> configurationValues.get( JAKARTA_HBM2DDL_DB_NAME ),
dbName = (String) configurationValues.get( AvailableSettings.JAKARTA_HBM2DDL_DB_NAME ); () -> {
} final String name = (String) configurationValues.get( DIALECT_DB_NAME );
final String explicitDbName = dbName; if ( StringHelper.isNotEmpty( name ) ) {
if ( StringHelper.isNotEmpty( explicitDbName ) ) { DEPRECATION_LOGGER.deprecatedSetting( DIALECT_DB_NAME, JAKARTA_HBM2DDL_DB_NAME );
String dbVersion = (String) configurationValues.get( AvailableSettings.DIALECT_DB_VERSION ); }
if ( dbVersion == null ) { return name;
dbVersion = (String) configurationValues.get( AvailableSettings.JAKARTA_DIALECT_DB_VERSION ); }
} );
String dbMajor = (String) configurationValues.get( AvailableSettings.DIALECT_DB_MAJOR_VERSION ); if ( dbName != null ) {
if ( dbMajor == null ) { final String dbVersion = (String) coalesceSuppliedValues(
dbMajor = (String) configurationValues.get( AvailableSettings.JAKARTA_HBM2DDL_DB_MAJOR_VERSION ); () -> configurationValues.get( JAKARTA_DIALECT_DB_VERSION ),
} () -> {
String dbMinor = (String) configurationValues.get( AvailableSettings.DIALECT_DB_MINOR_VERSION ); final String name = (String) configurationValues.get( DIALECT_DB_VERSION );
if ( dbMinor == null ) { if ( StringHelper.isNotEmpty( name ) ) {
dbMinor = (String) configurationValues.get( AvailableSettings.JAKARTA_HBM2DDL_DB_MINOR_VERSION ); DEPRECATION_LOGGER.deprecatedSetting( DIALECT_DB_VERSION, JAKARTA_DIALECT_DB_VERSION );
} }
final String explicitDbVersion = dbVersion; return name;
final String explicitDbMajor = dbMajor; }
final String explicitDbMinor = dbMinor; );
final String dbMajor = (String) coalesceSuppliedValues(
() -> configurationValues.get( JAKARTA_HBM2DDL_DB_MAJOR_VERSION ),
() -> {
final String name = (String) configurationValues.get( HBM2DDL_DB_MAJOR_VERSION );
if ( StringHelper.isNotEmpty( name ) ) {
DEPRECATION_LOGGER.deprecatedSetting( HBM2DDL_DB_MAJOR_VERSION, JAKARTA_HBM2DDL_DB_MAJOR_VERSION );
}
return name;
}
);
final String dbMinor = (String) coalesceSuppliedValues(
() -> configurationValues.get( JAKARTA_HBM2DDL_DB_MINOR_VERSION ),
() -> {
final String name = (String) configurationValues.get( HBM2DDL_DB_MINOR_VERSION );
if ( StringHelper.isNotEmpty( name ) ) {
DEPRECATION_LOGGER.deprecatedSetting( HBM2DDL_DB_MINOR_VERSION, JAKARTA_HBM2DDL_DB_MINOR_VERSION );
}
return name;
}
);
final Dialect indicatedDialect = serviceRegistry.getService( DialectResolver.class ).resolveDialect( final Dialect indicatedDialect = serviceRegistry.getService( DialectResolver.class ).resolveDialect(
new DialectResolutionInfo() { new DialectResolutionInfo() {
@Override @Override
public String getDatabaseName() { public String getDatabaseName() {
return explicitDbName; return dbName;
} }
@Override @Override
public String getDatabaseVersion() { public String getDatabaseVersion() {
return explicitDbVersion == null return dbVersion == null
? String.valueOf( NO_VERSION ) : ? String.valueOf( NO_VERSION ) :
explicitDbVersion; dbVersion;
} }
@Override @Override
public int getDatabaseMajorVersion() { public int getDatabaseMajorVersion() {
return StringHelper.isEmpty( explicitDbMajor ) return StringHelper.isEmpty( dbMajor )
? NO_VERSION ? NO_VERSION
: Integer.parseInt( explicitDbMajor ); : Integer.parseInt( dbMajor );
} }
@Override @Override
public int getDatabaseMinorVersion() { public int getDatabaseMinorVersion() {
return StringHelper.isEmpty( explicitDbMinor ) return StringHelper.isEmpty( dbMinor )
? NO_VERSION ? NO_VERSION
: Integer.parseInt( explicitDbMinor ); : Integer.parseInt( dbMinor );
} }
@Override @Override
@ -290,9 +325,9 @@ public class HibernateSchemaManagementTool implements SchemaManagementTool, Serv
if ( indicatedDialect == null ) { if ( indicatedDialect == null ) {
log.debugf( log.debugf(
"Unable to resolve indicated Dialect resolution info (%s, %s, %s)", "Unable to resolve indicated Dialect resolution info (%s, %s, %s)",
explicitDbName, dbName,
explicitDbMajor, dbMajor,
explicitDbMinor dbMinor
); );
} }
else { else {

View File

@ -90,9 +90,13 @@ public class JakartaSchemaToolingTests {
JAKARTA_HBM2DDL_DATABASE_ACTION, Action.CREATE_DROP, JAKARTA_HBM2DDL_DATABASE_ACTION, Action.CREATE_DROP,
HBM2DDL_DATABASE_ACTION, Action.NONE, HBM2DDL_DATABASE_ACTION, Action.NONE,
JAKARTA_JPA_JDBC_DRIVER, Environment.getProperties().get( AvailableSettings.DRIVER ), JAKARTA_JPA_JDBC_DRIVER, Environment.getProperties().get( AvailableSettings.DRIVER ),
JPA_JDBC_DRIVER, "does.not.exist",
JAKARTA_JPA_JDBC_URL, Environment.getProperties().get( AvailableSettings.URL ), JAKARTA_JPA_JDBC_URL, Environment.getProperties().get( AvailableSettings.URL ),
JPA_JDBC_URL, "jdbc:na:nowhere",
JAKARTA_JPA_JDBC_USER, Environment.getProperties().get( AvailableSettings.USER ), JAKARTA_JPA_JDBC_USER, Environment.getProperties().get( AvailableSettings.USER ),
JAKARTA_JPA_JDBC_PASSWORD, Environment.getProperties().get( AvailableSettings.PASS ) JPA_JDBC_USER, "goofy",
JAKARTA_JPA_JDBC_PASSWORD, Environment.getProperties().get( AvailableSettings.PASS ),
JPA_JDBC_PASSWORD, "goober"
) ) { ) ) {
tryQuery( sessionFactory ); tryQuery( sessionFactory );
} }