HHH-7965 - Redesign DialectResolver contract
This commit is contained in:
parent
5c453fee8d
commit
6599f710bb
|
@ -261,7 +261,7 @@
|
|||
<para>
|
||||
The standard resolver implementation acts as a chain, delegating to a series of individual
|
||||
resolvers. The standard Hibernate resolution behavior is contained in
|
||||
<classname>org.hibernate.engine.jdbc.dialect.internal.StandardDialectResolver</classname>.
|
||||
<classname>org.hibernate.engine.jdbc.dialect.internal.StandardDatabaseMetaDataDialectResolver</classname>.
|
||||
<classname>org.hibernate.engine.jdbc.dialect.internal.DialectResolverInitiator</classname>
|
||||
also consults with the <property>hibernate.dialect_resolvers</property> setting for any
|
||||
custom resolvers.
|
||||
|
|
|
@ -28,15 +28,18 @@ import java.sql.SQLException;
|
|||
|
||||
import org.hibernate.HibernateException;
|
||||
import org.hibernate.dialect.Dialect;
|
||||
import org.hibernate.engine.jdbc.dialect.spi.AbstractDatabaseMetaDataDialectResolver;
|
||||
|
||||
/**
|
||||
* Intended as support for custom resolvers.
|
||||
*
|
||||
* @author Steve Ebersole
|
||||
*
|
||||
* @deprecated Purpose has shifted to new {@link org.hibernate.engine.jdbc.dialect.spi.DatabaseInfoDialectResolver}
|
||||
* contract. See <a href="https://hibernate.onjira.com/browse/HHH-7965">HHH-7965</a> for details.
|
||||
*/
|
||||
public class BasicDialectResolver extends AbstractDialectResolver {
|
||||
// TODO: should this disappear???
|
||||
|
||||
@Deprecated
|
||||
public class BasicDialectResolver extends AbstractDatabaseMetaDataDialectResolver {
|
||||
public static final int VERSION_INSENSITIVE_VERSION = -9999;
|
||||
|
||||
private final String matchingName;
|
||||
|
|
|
@ -0,0 +1,79 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* Copyright (c) 2013, Red Hat Inc. or third-party contributors as
|
||||
* indicated by the @author tags or express copyright attribution
|
||||
* statements applied by the authors. All third-party contributions are
|
||||
* distributed under license by Red Hat Inc.
|
||||
*
|
||||
* This copyrighted material is made available to anyone wishing to use, modify,
|
||||
* copy, or redistribute it subject to the terms and conditions of the GNU
|
||||
* Lesser General Public License, as published by the Free Software Foundation.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
|
||||
* for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this distribution; if not, write to:
|
||||
* Free Software Foundation, Inc.
|
||||
* 51 Franklin Street, Fifth Floor
|
||||
* Boston, MA 02110-1301 USA
|
||||
*/
|
||||
package org.hibernate.engine.jdbc.dialect.internal;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import org.hibernate.HibernateException;
|
||||
import org.hibernate.boot.registry.StandardServiceInitiator;
|
||||
import org.hibernate.boot.registry.classloading.spi.ClassLoaderService;
|
||||
import org.hibernate.cfg.AvailableSettings;
|
||||
import org.hibernate.engine.jdbc.dialect.spi.DatabaseInfoDialectResolver;
|
||||
import org.hibernate.internal.util.StringHelper;
|
||||
import org.hibernate.service.spi.ServiceException;
|
||||
import org.hibernate.service.spi.ServiceRegistryImplementor;
|
||||
|
||||
/**
|
||||
* @author Steve Ebersole
|
||||
*/
|
||||
public class DatabaseInfoDialectResolverInitiator implements StandardServiceInitiator<DatabaseInfoDialectResolver> {
|
||||
public static final DatabaseInfoDialectResolverInitiator INSTANCE = new DatabaseInfoDialectResolverInitiator();
|
||||
|
||||
@Override
|
||||
public Class<DatabaseInfoDialectResolver> getServiceInitiated() {
|
||||
return DatabaseInfoDialectResolver.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
public DatabaseInfoDialectResolver initiateService(Map configurationValues, ServiceRegistryImplementor registry) {
|
||||
final DatabaseInfoDialectResolverSet resolver = new DatabaseInfoDialectResolverSet();
|
||||
applyCustomReslvers( resolver, configurationValues, registry );
|
||||
resolver.addResolver( StandardDatabaseInfoDialectResolver.INSTANCE );
|
||||
return resolver;
|
||||
}
|
||||
|
||||
private void applyCustomReslvers(
|
||||
DatabaseInfoDialectResolverSet resolver,
|
||||
Map configurationValues,
|
||||
ServiceRegistryImplementor registry) {
|
||||
final String resolverImplNames = (String) configurationValues.get( AvailableSettings.DIALECT_RESOLVERS );
|
||||
|
||||
if ( StringHelper.isNotEmpty( resolverImplNames ) ) {
|
||||
final ClassLoaderService classLoaderService = registry.getService( ClassLoaderService.class );
|
||||
for ( String resolverImplName : StringHelper.split( ", \n\r\f\t", resolverImplNames ) ) {
|
||||
try {
|
||||
resolver.addResolver(
|
||||
(DatabaseInfoDialectResolver) classLoaderService.classForName( resolverImplName ).newInstance()
|
||||
);
|
||||
}
|
||||
catch (HibernateException e) {
|
||||
throw e;
|
||||
}
|
||||
catch (Exception e) {
|
||||
throw new ServiceException( "Unable to instantiate named dialect resolver [" + resolverImplName + "]", e );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,81 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* Copyright (c) 2013, Red Hat Inc. or third-party contributors as
|
||||
* indicated by the @author tags or express copyright attribution
|
||||
* statements applied by the authors. All third-party contributions are
|
||||
* distributed under license by Red Hat Inc.
|
||||
*
|
||||
* This copyrighted material is made available to anyone wishing to use, modify,
|
||||
* copy, or redistribute it subject to the terms and conditions of the GNU
|
||||
* Lesser General Public License, as published by the Free Software Foundation.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
|
||||
* for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this distribution; if not, write to:
|
||||
* Free Software Foundation, Inc.
|
||||
* 51 Franklin Street, Fifth Floor
|
||||
* Boston, MA 02110-1301 USA
|
||||
*/
|
||||
package org.hibernate.engine.jdbc.dialect.internal;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.hibernate.dialect.Dialect;
|
||||
import org.hibernate.engine.jdbc.dialect.spi.DatabaseInfoDialectResolver;
|
||||
|
||||
/**
|
||||
* @author Steve Ebersole
|
||||
*/
|
||||
public class DatabaseInfoDialectResolverSet implements DatabaseInfoDialectResolver {
|
||||
private List<DatabaseInfoDialectResolver> delegateResolvers;
|
||||
|
||||
public DatabaseInfoDialectResolverSet() {
|
||||
this( new ArrayList<DatabaseInfoDialectResolver>() );
|
||||
}
|
||||
|
||||
public DatabaseInfoDialectResolverSet(List<DatabaseInfoDialectResolver> delegateResolvers) {
|
||||
this.delegateResolvers = delegateResolvers;
|
||||
}
|
||||
|
||||
public DatabaseInfoDialectResolverSet(DatabaseInfoDialectResolver... delegateResolvers) {
|
||||
this( Arrays.asList( delegateResolvers ) );
|
||||
}
|
||||
|
||||
@Override
|
||||
public Dialect resolve(DatabaseInfo databaseInfo) {
|
||||
for ( DatabaseInfoDialectResolver resolver : delegateResolvers ) {
|
||||
Dialect dialect = resolver.resolve( databaseInfo );
|
||||
if ( dialect != null ) {
|
||||
return dialect;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a resolver at the end of the underlying resolver list. The resolver added by this method is at lower
|
||||
* priority than any other existing resolvers.
|
||||
*
|
||||
* @param resolver The resolver to add.
|
||||
*/
|
||||
public void addResolver(DatabaseInfoDialectResolver resolver) {
|
||||
delegateResolvers.add( resolver );
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a resolver at the beginning of the underlying resolver list. The resolver added by this method is at higher
|
||||
* priority than any other existing resolvers.
|
||||
*
|
||||
* @param resolver The resolver to add.
|
||||
*/
|
||||
public void addResolverAtFirst(DatabaseInfoDialectResolver resolver) {
|
||||
delegateResolvers.add( 0, resolver );
|
||||
}
|
||||
}
|
|
@ -23,16 +23,15 @@
|
|||
*/
|
||||
package org.hibernate.engine.jdbc.dialect.internal;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.hibernate.HibernateException;
|
||||
import org.hibernate.cfg.AvailableSettings;
|
||||
import org.hibernate.internal.util.StringHelper;
|
||||
import org.hibernate.boot.registry.classloading.spi.ClassLoaderService;
|
||||
import org.hibernate.engine.jdbc.dialect.spi.DialectResolver;
|
||||
import org.hibernate.boot.registry.StandardServiceInitiator;
|
||||
import org.hibernate.boot.registry.classloading.spi.ClassLoaderService;
|
||||
import org.hibernate.cfg.AvailableSettings;
|
||||
import org.hibernate.engine.jdbc.dialect.spi.DatabaseInfoDialectResolver;
|
||||
import org.hibernate.engine.jdbc.dialect.spi.DialectResolver;
|
||||
import org.hibernate.internal.util.StringHelper;
|
||||
import org.hibernate.service.spi.ServiceException;
|
||||
import org.hibernate.service.spi.ServiceRegistryImplementor;
|
||||
|
||||
|
@ -51,19 +50,29 @@ public class DialectResolverInitiator implements StandardServiceInitiator<Dialec
|
|||
|
||||
@Override
|
||||
public DialectResolver initiateService(Map configurationValues, ServiceRegistryImplementor registry) {
|
||||
return new DialectResolverSet( determineResolvers( configurationValues, registry ) );
|
||||
final DialectResolverSet resolver = new DialectResolverSet();
|
||||
applyCustomerResolvers( resolver, registry, configurationValues );
|
||||
resolver.addResolver(
|
||||
new StandardDatabaseMetaDataDialectResolver(
|
||||
registry.getService( DatabaseInfoDialectResolver.class )
|
||||
)
|
||||
);
|
||||
return resolver;
|
||||
}
|
||||
|
||||
private List<DialectResolver> determineResolvers(Map configurationValues, ServiceRegistryImplementor registry) {
|
||||
final List<DialectResolver> resolvers = new ArrayList<DialectResolver>();
|
||||
|
||||
private void applyCustomerResolvers(
|
||||
DialectResolverSet resolver,
|
||||
ServiceRegistryImplementor registry,
|
||||
Map configurationValues) {
|
||||
final String resolverImplNames = (String) configurationValues.get( AvailableSettings.DIALECT_RESOLVERS );
|
||||
|
||||
if ( StringHelper.isNotEmpty( resolverImplNames ) ) {
|
||||
final ClassLoaderService classLoaderService = registry.getService( ClassLoaderService.class );
|
||||
for ( String resolverImplName : StringHelper.split( ", \n\r\f\t", resolverImplNames ) ) {
|
||||
try {
|
||||
resolvers.add( (DialectResolver) classLoaderService.classForName( resolverImplName ).newInstance() );
|
||||
resolver.addResolver(
|
||||
(DialectResolver) classLoaderService.classForName( resolverImplName ).newInstance()
|
||||
);
|
||||
}
|
||||
catch (HibernateException e) {
|
||||
throw e;
|
||||
|
@ -73,8 +82,5 @@ public class DialectResolverInitiator implements StandardServiceInitiator<Dialec
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
resolvers.add( new StandardDialectResolver() );
|
||||
return resolvers;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* Copyright (c) 2010, Red Hat Inc. or third-party contributors as
|
||||
* Copyright (c) 2013, Red Hat Inc. or third-party contributors as
|
||||
* indicated by the @author tags or express copyright attribution
|
||||
* statements applied by the authors. All third-party contributions are
|
||||
* distributed under license by Red Hat Inc.
|
||||
|
@ -23,9 +23,6 @@
|
|||
*/
|
||||
package org.hibernate.engine.jdbc.dialect.internal;
|
||||
|
||||
import java.sql.DatabaseMetaData;
|
||||
import java.sql.SQLException;
|
||||
|
||||
import org.jboss.logging.Logger;
|
||||
|
||||
import org.hibernate.dialect.CUBRIDDialect;
|
||||
|
@ -53,22 +50,23 @@ import org.hibernate.dialect.SQLServer2008Dialect;
|
|||
import org.hibernate.dialect.SQLServerDialect;
|
||||
import org.hibernate.dialect.SybaseASE15Dialect;
|
||||
import org.hibernate.dialect.SybaseAnywhereDialect;
|
||||
import org.hibernate.engine.jdbc.dialect.spi.DatabaseInfoDialectResolver;
|
||||
import org.hibernate.internal.CoreMessageLogger;
|
||||
|
||||
/**
|
||||
* The standard Hibernate Dialect resolver.
|
||||
*
|
||||
* @author Steve Ebersole
|
||||
*/
|
||||
public class StandardDialectResolver extends AbstractDialectResolver {
|
||||
public class StandardDatabaseInfoDialectResolver implements DatabaseInfoDialectResolver {
|
||||
public static final StandardDatabaseInfoDialectResolver INSTANCE = new StandardDatabaseInfoDialectResolver();
|
||||
|
||||
private static final CoreMessageLogger LOG = Logger.getMessageLogger(CoreMessageLogger.class,
|
||||
StandardDialectResolver.class.getName());
|
||||
private static final CoreMessageLogger LOG = Logger.getMessageLogger(
|
||||
CoreMessageLogger.class,
|
||||
StandardDatabaseInfoDialectResolver.class.getName()
|
||||
);
|
||||
|
||||
@Override
|
||||
protected Dialect resolveDialectInternal(DatabaseMetaData metaData) throws SQLException {
|
||||
String databaseName = metaData.getDatabaseProductName();
|
||||
int databaseMajorVersion = metaData.getDatabaseMajorVersion();
|
||||
public Dialect resolve(DatabaseInfo databaseInfo) {
|
||||
final String databaseName = databaseInfo.getDatabaseName();
|
||||
|
||||
if ( "CUBRID".equalsIgnoreCase( databaseName ) ) {
|
||||
return new CUBRIDDialect();
|
||||
|
@ -87,22 +85,26 @@ public class StandardDialectResolver extends AbstractDialectResolver {
|
|||
}
|
||||
|
||||
if ( "PostgreSQL".equals( databaseName ) ) {
|
||||
final int databaseMinorVersion = metaData.getDatabaseMinorVersion();
|
||||
if ( databaseMajorVersion > 8 || ( databaseMajorVersion == 8 && databaseMinorVersion >= 2 ) ) {
|
||||
final int majorVersion = databaseInfo.getDatabaseMajorVersion();
|
||||
final int minorVersion = databaseInfo.getDatabaseMinorVersion();
|
||||
|
||||
if ( majorVersion > 8 || ( majorVersion == 8 && minorVersion >= 2 ) ) {
|
||||
return new PostgreSQL82Dialect();
|
||||
}
|
||||
return new PostgreSQL81Dialect();
|
||||
}
|
||||
|
||||
if ( "Apache Derby".equals( databaseName ) ) {
|
||||
final int databaseMinorVersion = metaData.getDatabaseMinorVersion();
|
||||
if ( databaseMajorVersion > 10 || ( databaseMajorVersion == 10 && databaseMinorVersion >= 7 ) ) {
|
||||
final int majorVersion = databaseInfo.getDatabaseMajorVersion();
|
||||
final int minorVersion = databaseInfo.getDatabaseMinorVersion();
|
||||
|
||||
if ( majorVersion > 10 || ( majorVersion == 10 && minorVersion >= 7 ) ) {
|
||||
return new DerbyTenSevenDialect();
|
||||
}
|
||||
else if ( databaseMajorVersion == 10 && databaseMinorVersion == 6 ) {
|
||||
else if ( majorVersion == 10 && minorVersion == 6 ) {
|
||||
return new DerbyTenSixDialect();
|
||||
}
|
||||
else if ( databaseMajorVersion == 10 && databaseMinorVersion == 5 ) {
|
||||
else if ( majorVersion == 10 && minorVersion == 5 ) {
|
||||
return new DerbyTenFiveDialect();
|
||||
}
|
||||
else {
|
||||
|
@ -111,23 +113,27 @@ public class StandardDialectResolver extends AbstractDialectResolver {
|
|||
}
|
||||
|
||||
if ( "ingres".equalsIgnoreCase( databaseName ) ) {
|
||||
switch ( databaseMajorVersion ) {
|
||||
final int majorVersion = databaseInfo.getDatabaseMajorVersion();
|
||||
final int minorVersion = databaseInfo.getDatabaseMinorVersion();
|
||||
|
||||
switch ( majorVersion ) {
|
||||
case 9:
|
||||
int databaseMinorVersion = metaData.getDatabaseMinorVersion();
|
||||
if (databaseMinorVersion > 2) {
|
||||
if (minorVersion > 2) {
|
||||
return new Ingres9Dialect();
|
||||
}
|
||||
return new IngresDialect();
|
||||
case 10:
|
||||
return new Ingres10Dialect();
|
||||
default:
|
||||
LOG.unknownIngresVersion(databaseMajorVersion);
|
||||
LOG.unknownIngresVersion( majorVersion );
|
||||
}
|
||||
return new IngresDialect();
|
||||
}
|
||||
|
||||
if ( databaseName.startsWith( "Microsoft SQL Server" ) ) {
|
||||
switch ( databaseMajorVersion ) {
|
||||
final int majorVersion = databaseInfo.getDatabaseMajorVersion();
|
||||
|
||||
switch ( majorVersion ) {
|
||||
case 8:
|
||||
return new SQLServerDialect();
|
||||
case 9:
|
||||
|
@ -136,7 +142,7 @@ public class StandardDialectResolver extends AbstractDialectResolver {
|
|||
case 11:
|
||||
return new SQLServer2008Dialect();
|
||||
default:
|
||||
LOG.unknownSqlServerVersion(databaseMajorVersion);
|
||||
LOG.unknownSqlServerVersion( majorVersion );
|
||||
}
|
||||
return new SQLServerDialect();
|
||||
}
|
||||
|
@ -162,7 +168,9 @@ public class StandardDialectResolver extends AbstractDialectResolver {
|
|||
}
|
||||
|
||||
if ( "Oracle".equals( databaseName ) ) {
|
||||
switch ( databaseMajorVersion ) {
|
||||
final int majorVersion = databaseInfo.getDatabaseMajorVersion();
|
||||
|
||||
switch ( majorVersion ) {
|
||||
case 11:
|
||||
return new Oracle10gDialect();
|
||||
case 10:
|
||||
|
@ -172,7 +180,7 @@ public class StandardDialectResolver extends AbstractDialectResolver {
|
|||
case 8:
|
||||
return new Oracle8iDialect();
|
||||
default:
|
||||
LOG.unknownOracleVersion(databaseMajorVersion);
|
||||
LOG.unknownOracleVersion( majorVersion );
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,92 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* Copyright (c) 2010, Red Hat Inc. or third-party contributors as
|
||||
* indicated by the @author tags or express copyright attribution
|
||||
* statements applied by the authors. All third-party contributions are
|
||||
* distributed under license by Red Hat Inc.
|
||||
*
|
||||
* This copyrighted material is made available to anyone wishing to use, modify,
|
||||
* copy, or redistribute it subject to the terms and conditions of the GNU
|
||||
* Lesser General Public License, as published by the Free Software Foundation.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
|
||||
* for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this distribution; if not, write to:
|
||||
* Free Software Foundation, Inc.
|
||||
* 51 Franklin Street, Fifth Floor
|
||||
* Boston, MA 02110-1301 USA
|
||||
*/
|
||||
package org.hibernate.engine.jdbc.dialect.internal;
|
||||
|
||||
import java.sql.DatabaseMetaData;
|
||||
import java.sql.SQLException;
|
||||
|
||||
import org.hibernate.dialect.Dialect;
|
||||
import org.hibernate.dialect.resolver.BasicSQLExceptionConverter;
|
||||
import org.hibernate.engine.jdbc.dialect.spi.AbstractDatabaseMetaDataDialectResolver;
|
||||
import org.hibernate.engine.jdbc.dialect.spi.DatabaseInfoDialectResolver;
|
||||
|
||||
/**
|
||||
* The standard Hibernate Dialect resolver.
|
||||
*
|
||||
* @author Steve Ebersole
|
||||
*/
|
||||
public class StandardDatabaseMetaDataDialectResolver extends AbstractDatabaseMetaDataDialectResolver {
|
||||
private final DatabaseInfoDialectResolver infoResolver;
|
||||
|
||||
public StandardDatabaseMetaDataDialectResolver(DatabaseInfoDialectResolver infoResolver) {
|
||||
this.infoResolver = infoResolver;
|
||||
}
|
||||
|
||||
public static final class DatabaseInfoImpl implements DatabaseInfoDialectResolver.DatabaseInfo {
|
||||
private final DatabaseMetaData databaseMetaData;
|
||||
|
||||
public DatabaseInfoImpl(DatabaseMetaData databaseMetaData) {
|
||||
this.databaseMetaData = databaseMetaData;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDatabaseName() {
|
||||
try {
|
||||
return databaseMetaData.getDatabaseProductName();
|
||||
}
|
||||
catch (SQLException e) {
|
||||
throw BasicSQLExceptionConverter.INSTANCE.convert( e );
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getDatabaseMajorVersion() {
|
||||
try {
|
||||
return databaseMetaData.getDatabaseMajorVersion();
|
||||
}
|
||||
catch (SQLException e) {
|
||||
throw BasicSQLExceptionConverter.INSTANCE.convert( e );
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getDatabaseMinorVersion() {
|
||||
try {
|
||||
return databaseMetaData.getDatabaseMinorVersion();
|
||||
}
|
||||
catch (SQLException e) {
|
||||
throw BasicSQLExceptionConverter.INSTANCE.convert( e );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Dialect resolveDialectInternal(DatabaseMetaData metaData) throws SQLException {
|
||||
if ( infoResolver == null ) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return infoResolver.resolve( new DatabaseInfoImpl( metaData ) );
|
||||
}
|
||||
}
|
|
@ -21,7 +21,7 @@
|
|||
* 51 Franklin Street, Fifth Floor
|
||||
* Boston, MA 02110-1301 USA
|
||||
*/
|
||||
package org.hibernate.engine.jdbc.dialect.internal;
|
||||
package org.hibernate.engine.jdbc.dialect.spi;
|
||||
|
||||
import java.sql.DatabaseMetaData;
|
||||
import java.sql.SQLException;
|
||||
|
@ -33,7 +33,6 @@ import org.hibernate.dialect.Dialect;
|
|||
import org.hibernate.dialect.resolver.BasicSQLExceptionConverter;
|
||||
import org.hibernate.exception.JDBCConnectionException;
|
||||
import org.hibernate.internal.CoreMessageLogger;
|
||||
import org.hibernate.engine.jdbc.dialect.spi.DialectResolver;
|
||||
|
||||
/**
|
||||
* A templated resolver impl which delegates to the {@link #resolveDialectInternal} method
|
||||
|
@ -41,10 +40,12 @@ import org.hibernate.engine.jdbc.dialect.spi.DialectResolver;
|
|||
*
|
||||
* @author Steve Ebersole
|
||||
*/
|
||||
public abstract class AbstractDialectResolver implements DialectResolver {
|
||||
public abstract class AbstractDatabaseMetaDataDialectResolver implements DialectResolver {
|
||||
|
||||
private static final CoreMessageLogger LOG = Logger.getMessageLogger(CoreMessageLogger.class,
|
||||
AbstractDialectResolver.class.getName());
|
||||
private static final CoreMessageLogger LOG = Logger.getMessageLogger(
|
||||
CoreMessageLogger.class,
|
||||
AbstractDatabaseMetaDataDialectResolver.class.getName()
|
||||
);
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
|
@ -58,7 +59,10 @@ public abstract class AbstractDialectResolver implements DialectResolver {
|
|||
}
|
||||
catch ( SQLException sqlException ) {
|
||||
JDBCException jdbcException = BasicSQLExceptionConverter.INSTANCE.convert( sqlException );
|
||||
if (jdbcException instanceof JDBCConnectionException) throw jdbcException;
|
||||
if (jdbcException instanceof JDBCConnectionException) {
|
||||
throw jdbcException;
|
||||
}
|
||||
|
||||
LOG.warnf( "%s : %s", BasicSQLExceptionConverter.MSG, sqlException.getMessage() );
|
||||
return null;
|
||||
}
|
|
@ -0,0 +1,78 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* Copyright (c) 2013, Red Hat Inc. or third-party contributors as
|
||||
* indicated by the @author tags or express copyright attribution
|
||||
* statements applied by the authors. All third-party contributions are
|
||||
* distributed under license by Red Hat Inc.
|
||||
*
|
||||
* This copyrighted material is made available to anyone wishing to use, modify,
|
||||
* copy, or redistribute it subject to the terms and conditions of the GNU
|
||||
* Lesser General Public License, as published by the Free Software Foundation.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
|
||||
* for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this distribution; if not, write to:
|
||||
* Free Software Foundation, Inc.
|
||||
* 51 Franklin Street, Fifth Floor
|
||||
* Boston, MA 02110-1301 USA
|
||||
*/
|
||||
package org.hibernate.engine.jdbc.dialect.spi;
|
||||
|
||||
import org.hibernate.dialect.Dialect;
|
||||
import org.hibernate.service.Service;
|
||||
|
||||
/**
|
||||
* A contract for resolving database name, major version and minor version to Dialect
|
||||
*
|
||||
* @author Steve Ebersole
|
||||
*/
|
||||
public interface DatabaseInfoDialectResolver extends Service {
|
||||
public static final int NO_VERSION = -9999;
|
||||
|
||||
/**
|
||||
* Determine the {@link Dialect} to use based on the given information. Implementations are
|
||||
* expected to return the {@link Dialect} instance to use, or {@code null} if the they did not locate a match.
|
||||
*
|
||||
* @param databaseInfo Access to the needed database information
|
||||
*
|
||||
* @return The dialect to use, or null.
|
||||
*/
|
||||
public Dialect resolve(DatabaseInfo databaseInfo);
|
||||
|
||||
public static interface DatabaseInfo {
|
||||
/**
|
||||
* Obtain access to the database name, as returned from {@link java.sql.DatabaseMetaData#getDatabaseProductName()}
|
||||
* for the target database
|
||||
*
|
||||
* @return The database name
|
||||
*/
|
||||
public String getDatabaseName();
|
||||
|
||||
/**
|
||||
* Obtain access to the database major version, as returned from
|
||||
* {@link java.sql.DatabaseMetaData#getDatabaseMajorVersion()} for the target database; {@value #NO_VERSION}
|
||||
* indicates no version information was supplied
|
||||
*
|
||||
* @return The major version
|
||||
*
|
||||
* @see #NO_VERSION
|
||||
*/
|
||||
public int getDatabaseMajorVersion();
|
||||
|
||||
/**
|
||||
* Obtain access to the database minor version, as returned from
|
||||
* {@link java.sql.DatabaseMetaData#getDatabaseMinorVersion()} for the target database; {@value #NO_VERSION}
|
||||
* indicates no version information was supplied
|
||||
*
|
||||
* @return The minor version
|
||||
*
|
||||
* @see #NO_VERSION
|
||||
*/
|
||||
public int getDatabaseMinorVersion();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,52 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* Copyright (c) 2013, Red Hat Inc. or third-party contributors as
|
||||
* indicated by the @author tags or express copyright attribution
|
||||
* statements applied by the authors. All third-party contributions are
|
||||
* distributed under license by Red Hat Inc.
|
||||
*
|
||||
* This copyrighted material is made available to anyone wishing to use, modify,
|
||||
* copy, or redistribute it subject to the terms and conditions of the GNU
|
||||
* Lesser General Public License, as published by the Free Software Foundation.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
|
||||
* for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this distribution; if not, write to:
|
||||
* Free Software Foundation, Inc.
|
||||
* 51 Franklin Street, Fifth Floor
|
||||
* Boston, MA 02110-1301 USA
|
||||
*/
|
||||
package org.hibernate.engine.jdbc.dialect.spi;
|
||||
|
||||
import java.sql.DatabaseMetaData;
|
||||
|
||||
import org.hibernate.dialect.Dialect;
|
||||
import org.hibernate.exception.JDBCConnectionException;
|
||||
import org.hibernate.service.Service;
|
||||
|
||||
/**
|
||||
* Contract for determining the {@link Dialect} to use based on a JDBC {@link java.sql.Connection}.
|
||||
*
|
||||
* @author Tomoto Shimizu Washio
|
||||
* @author Steve Ebersole
|
||||
*/
|
||||
public interface DatabaseMetaDataDialectResolver extends Service {
|
||||
/**
|
||||
* Determine the {@link org.hibernate.dialect.Dialect} to use based on the given JDBC {@link java.sql.DatabaseMetaData}. Implementations are
|
||||
* expected to return the {@link org.hibernate.dialect.Dialect} instance to use, or null if the {@link java.sql.DatabaseMetaData} does not match
|
||||
* the criteria handled by this impl.
|
||||
*
|
||||
* @param metaData The JDBC metadata.
|
||||
*
|
||||
* @return The dialect to use, or null.
|
||||
*
|
||||
* @throws org.hibernate.exception.JDBCConnectionException Indicates a 'non transient connection problem', which indicates that
|
||||
* we should stop resolution attempts.
|
||||
*/
|
||||
public Dialect resolveDialect(DatabaseMetaData metaData) throws JDBCConnectionException;
|
||||
}
|
|
@ -23,30 +23,10 @@
|
|||
*/
|
||||
package org.hibernate.engine.jdbc.dialect.spi;
|
||||
|
||||
import java.sql.DatabaseMetaData;
|
||||
|
||||
import org.hibernate.dialect.Dialect;
|
||||
import org.hibernate.exception.JDBCConnectionException;
|
||||
import org.hibernate.service.Service;
|
||||
|
||||
/**
|
||||
* Contract for determining the {@link Dialect} to use based on a JDBC {@link java.sql.Connection}.
|
||||
*
|
||||
* @author Tomoto Shimizu Washio
|
||||
* @author Steve Ebersole
|
||||
* @deprecated Deprecated in favor of {@link DatabaseMetaDataDialectResolver} to account for resolving by name versus
|
||||
* by DatabaseMetaData
|
||||
*/
|
||||
public interface DialectResolver extends Service {
|
||||
/**
|
||||
* Determine the {@link Dialect} to use based on the given JDBC {@link DatabaseMetaData}. Implementations are
|
||||
* expected to return the {@link Dialect} instance to use, or null if the {@link DatabaseMetaData} does not match
|
||||
* the criteria handled by this impl.
|
||||
*
|
||||
* @param metaData The JDBC metadata.
|
||||
*
|
||||
* @return The dialect to use, or null.
|
||||
*
|
||||
* @throws JDBCConnectionException Indicates a 'non transient connection problem', which indicates that
|
||||
* we should stop resolution attempts.
|
||||
*/
|
||||
public Dialect resolveDialect(DatabaseMetaData metaData) throws JDBCConnectionException;
|
||||
@Deprecated
|
||||
public interface DialectResolver extends DatabaseMetaDataDialectResolver {
|
||||
}
|
||||
|
|
|
@ -48,12 +48,12 @@ import org.hibernate.HibernateException;
|
|||
import org.hibernate.LockMode;
|
||||
import org.hibernate.cache.CacheException;
|
||||
import org.hibernate.dialect.Dialect;
|
||||
import org.hibernate.engine.jdbc.dialect.spi.DialectResolver;
|
||||
import org.hibernate.engine.loading.internal.CollectionLoadContext;
|
||||
import org.hibernate.engine.loading.internal.EntityLoadContext;
|
||||
import org.hibernate.engine.spi.CollectionKey;
|
||||
import org.hibernate.engine.spi.SessionFactoryImplementor;
|
||||
import org.hibernate.id.IntegralDataTypeHolder;
|
||||
import org.hibernate.engine.jdbc.dialect.internal.AbstractDialectResolver;
|
||||
import org.hibernate.engine.jndi.JndiException;
|
||||
import org.hibernate.engine.jndi.JndiNameException;
|
||||
import org.hibernate.type.BasicType;
|
||||
|
@ -1076,7 +1076,7 @@ public interface CoreMessageLogger extends BasicLogger {
|
|||
|
||||
@LogMessage(level = WARN)
|
||||
@Message(value = "Error executing resolver [%s] : %s", id = 316)
|
||||
void unableToExecuteResolver(AbstractDialectResolver abstractDialectResolver,
|
||||
void unableToExecuteResolver(DialectResolver abstractDialectResolver,
|
||||
String message);
|
||||
|
||||
@LogMessage(level = INFO)
|
||||
|
|
|
@ -30,6 +30,7 @@ import java.util.List;
|
|||
import org.hibernate.boot.registry.StandardServiceInitiator;
|
||||
import org.hibernate.cache.internal.RegionFactoryInitiator;
|
||||
import org.hibernate.engine.jdbc.batch.internal.BatchBuilderInitiator;
|
||||
import org.hibernate.engine.jdbc.dialect.internal.DatabaseInfoDialectResolverInitiator;
|
||||
import org.hibernate.engine.jdbc.internal.JdbcServicesInitiator;
|
||||
import org.hibernate.engine.transaction.internal.TransactionFactoryInitiator;
|
||||
import org.hibernate.id.factory.internal.MutableIdentifierGeneratorFactoryInitiator;
|
||||
|
@ -69,6 +70,7 @@ public class StandardServiceInitiators {
|
|||
|
||||
serviceInitiators.add( ConnectionProviderInitiator.INSTANCE );
|
||||
serviceInitiators.add( MultiTenantConnectionProviderInitiator.INSTANCE );
|
||||
serviceInitiators.add( DatabaseInfoDialectResolverInitiator.INSTANCE );
|
||||
serviceInitiators.add( DialectResolverInitiator.INSTANCE );
|
||||
serviceInitiators.add( DialectFactoryInitiator.INSTANCE );
|
||||
serviceInitiators.add( BatchBuilderInitiator.INSTANCE );
|
||||
|
|
|
@ -26,8 +26,8 @@ import java.sql.DatabaseMetaData;
|
|||
import java.sql.SQLException;
|
||||
|
||||
import org.hibernate.HibernateException;
|
||||
import org.hibernate.engine.jdbc.dialect.internal.AbstractDialectResolver;
|
||||
import org.hibernate.engine.jdbc.dialect.internal.BasicDialectResolver;
|
||||
import org.hibernate.engine.jdbc.dialect.spi.AbstractDatabaseMetaDataDialectResolver;
|
||||
|
||||
/**
|
||||
* @author Steve Ebersole
|
||||
|
@ -46,7 +46,7 @@ public class TestingDialects {
|
|||
public static class MySpecialDB2Dialect extends Dialect {
|
||||
}
|
||||
|
||||
public static class MyDialectResolver1 extends AbstractDialectResolver {
|
||||
public static class MyDialectResolver1 extends AbstractDatabaseMetaDataDialectResolver {
|
||||
protected Dialect resolveDialectInternal(DatabaseMetaData metaData) throws SQLException {
|
||||
String databaseName = metaData.getDatabaseProductName();
|
||||
int databaseMajorVersion = metaData.getDatabaseMajorVersion();
|
||||
|
@ -71,7 +71,7 @@ public class TestingDialects {
|
|||
}
|
||||
}
|
||||
|
||||
public static class ErrorDialectResolver1 extends AbstractDialectResolver {
|
||||
public static class ErrorDialectResolver1 extends AbstractDatabaseMetaDataDialectResolver {
|
||||
public Dialect resolveDialectInternal(DatabaseMetaData metaData) throws SQLException {
|
||||
String databaseName = metaData.getDatabaseProductName();
|
||||
if ( databaseName.equals( "ConnectionErrorDatabase1" ) ) {
|
||||
|
@ -83,7 +83,7 @@ public class TestingDialects {
|
|||
}
|
||||
}
|
||||
|
||||
public static class ErrorDialectResolver2 extends AbstractDialectResolver {
|
||||
public static class ErrorDialectResolver2 extends AbstractDatabaseMetaDataDialectResolver {
|
||||
public Dialect resolveDialectInternal(DatabaseMetaData metaData) throws SQLException {
|
||||
String databaseName = metaData.getDatabaseProductName();
|
||||
if ( databaseName.equals( "ErrorDatabase1" ) ) {
|
||||
|
|
|
@ -31,7 +31,6 @@ import java.util.Properties;
|
|||
|
||||
import org.hibernate.HibernateException;
|
||||
import org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl;
|
||||
import org.hibernate.boot.registry.classloading.spi.ClassLoadingException;
|
||||
import org.hibernate.boot.registry.selector.internal.StrategySelectorBuilder;
|
||||
import org.hibernate.boot.registry.selector.spi.StrategySelectionException;
|
||||
import org.hibernate.cfg.Environment;
|
||||
|
@ -58,7 +57,8 @@ import org.hibernate.dialect.SybaseAnywhereDialect;
|
|||
import org.hibernate.dialect.TestingDialects;
|
||||
import org.hibernate.engine.jdbc.dialect.internal.DialectFactoryImpl;
|
||||
import org.hibernate.engine.jdbc.dialect.internal.DialectResolverSet;
|
||||
import org.hibernate.engine.jdbc.dialect.internal.StandardDialectResolver;
|
||||
import org.hibernate.engine.jdbc.dialect.internal.StandardDatabaseInfoDialectResolver;
|
||||
import org.hibernate.engine.jdbc.dialect.internal.StandardDatabaseMetaDataDialectResolver;
|
||||
import org.hibernate.engine.jdbc.dialect.spi.DialectResolver;
|
||||
|
||||
import org.junit.Before;
|
||||
|
@ -82,7 +82,9 @@ public class DialectFactoryTest extends BaseUnitTestCase {
|
|||
dialectFactory.setStrategySelector(
|
||||
new StrategySelectorBuilder().buildSelector( new ClassLoaderServiceImpl( getClass().getClassLoader() ) )
|
||||
);
|
||||
dialectFactory.setDialectResolver( new StandardDialectResolver() );
|
||||
dialectFactory.setDialectResolver(
|
||||
new StandardDatabaseMetaDataDialectResolver( StandardDatabaseInfoDialectResolver.INSTANCE )
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -140,7 +142,7 @@ public class DialectFactoryTest extends BaseUnitTestCase {
|
|||
|
||||
@Test
|
||||
public void testPreregisteredDialects() {
|
||||
DialectResolver resolver = new StandardDialectResolver();
|
||||
DialectResolver resolver = new StandardDatabaseMetaDataDialectResolver( StandardDatabaseInfoDialectResolver.INSTANCE );
|
||||
testDetermination( "HSQL Database Engine", HSQLDialect.class, resolver );
|
||||
testDetermination( "H2", H2Dialect.class, resolver );
|
||||
testDetermination( "MySQL", MySQLDialect.class, resolver );
|
||||
|
|
|
@ -42,11 +42,11 @@ import org.hibernate.testing.junit4.BaseUnitTestCase;
|
|||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Unit test of the {@link StandardDialectResolver} class.
|
||||
* Unit test of the {@link StandardDatabaseMetaDataDialectResolver} class.
|
||||
*
|
||||
* @author Bryan Turner
|
||||
*/
|
||||
public class StandardDialectResolverTest extends BaseUnitTestCase {
|
||||
public class StandardDatabaseMetaDataDialectResolverTest extends BaseUnitTestCase {
|
||||
|
||||
@Test
|
||||
public void testResolveDialectInternalForSQLServer2000()
|
||||
|
@ -137,8 +137,8 @@ public class StandardDialectResolverTest extends BaseUnitTestCase {
|
|||
when( metaData.getDatabaseMajorVersion() ).thenReturn( majorVersion );
|
||||
when( metaData.getDatabaseMinorVersion() ).thenReturn( minorVersion );
|
||||
|
||||
Dialect dialect = new StandardDialectResolver().resolveDialectInternal(
|
||||
metaData );
|
||||
Dialect dialect = new StandardDatabaseMetaDataDialectResolver( StandardDatabaseInfoDialectResolver.INSTANCE )
|
||||
.resolveDialectInternal( metaData );
|
||||
|
||||
StringBuilder builder = new StringBuilder( productName ).append( " " )
|
||||
.append( majorVersion );
|
Loading…
Reference in New Issue