HHH-14456 Add DialectResolutionInfo#unwrap to be able to detect Cockroach in dialect resultion. Cleanup dialect resolution code.

This commit is contained in:
Christian Beikov 2021-02-12 17:04:42 +01:00
parent 130733af17
commit dc884e077b
7 changed files with 268 additions and 222 deletions

View File

@ -10,11 +10,17 @@ import java.util.Objects;
import org.hibernate.dialect.CUBRIDDialect;
import org.hibernate.dialect.Cache71Dialect;
import org.hibernate.dialect.CacheDialect;
import org.hibernate.dialect.CockroachDialect;
import org.hibernate.dialect.DB2390Dialect;
import org.hibernate.dialect.DB2390V8Dialect;
import org.hibernate.dialect.DB2400Dialect;
import org.hibernate.dialect.DB2400V7R3Dialect;
import org.hibernate.dialect.DB297Dialect;
import org.hibernate.dialect.DB2Dialect;
import org.hibernate.dialect.DB2iDialect;
import org.hibernate.dialect.DB2zDialect;
import org.hibernate.dialect.DerbyDialect;
import org.hibernate.dialect.DerbyTenFiveDialect;
import org.hibernate.dialect.DerbyTenSevenDialect;
import org.hibernate.dialect.DerbyTenSixDialect;
@ -25,29 +31,52 @@ import org.hibernate.dialect.HANACloudColumnStoreDialect;
import org.hibernate.dialect.HANAColumnStoreDialect;
import org.hibernate.dialect.HANARowStoreDialect;
import org.hibernate.dialect.HSQLDialect;
import org.hibernate.dialect.Informix10Dialect;
import org.hibernate.dialect.InformixDialect;
import org.hibernate.dialect.Ingres10Dialect;
import org.hibernate.dialect.Ingres9Dialect;
import org.hibernate.dialect.IngresDialect;
import org.hibernate.dialect.MariaDB102Dialect;
import org.hibernate.dialect.MariaDB103Dialect;
import org.hibernate.dialect.MariaDB10Dialect;
import org.hibernate.dialect.MariaDB53Dialect;
import org.hibernate.dialect.MariaDBDialect;
import org.hibernate.dialect.MaxDBDialect;
import org.hibernate.dialect.MimerSQLDialect;
import org.hibernate.dialect.MySQL55Dialect;
import org.hibernate.dialect.MySQL57Dialect;
import org.hibernate.dialect.MySQL5Dialect;
import org.hibernate.dialect.MySQL8Dialect;
import org.hibernate.dialect.MySQLDialect;
import org.hibernate.dialect.Oracle10gDialect;
import org.hibernate.dialect.Oracle12cDialect;
import org.hibernate.dialect.Oracle8iDialect;
import org.hibernate.dialect.Oracle9iDialect;
import org.hibernate.dialect.OracleDialect;
import org.hibernate.dialect.PostgreSQL81Dialect;
import org.hibernate.dialect.PostgreSQL82Dialect;
import org.hibernate.dialect.PostgreSQL91Dialect;
import org.hibernate.dialect.PostgreSQL92Dialect;
import org.hibernate.dialect.PostgreSQL93Dialect;
import org.hibernate.dialect.PostgreSQL94Dialect;
import org.hibernate.dialect.PostgreSQL95Dialect;
import org.hibernate.dialect.PostgreSQL9Dialect;
import org.hibernate.dialect.PostgreSQLDialect;
import org.hibernate.dialect.PostgresPlusDialect;
import org.hibernate.dialect.RDMSOS2200Dialect;
import org.hibernate.dialect.SAPDBDialect;
import org.hibernate.dialect.SQLServer2005Dialect;
import org.hibernate.dialect.SQLServer2008Dialect;
import org.hibernate.dialect.SQLServer2012Dialect;
import org.hibernate.dialect.SQLServerDialect;
import org.hibernate.dialect.SpannerDialect;
import org.hibernate.dialect.Sybase11Dialect;
import org.hibernate.dialect.SybaseASE157Dialect;
import org.hibernate.dialect.SybaseASE15Dialect;
import org.hibernate.dialect.SybaseASEDialect;
import org.hibernate.dialect.SybaseAnywhereDialect;
import org.hibernate.dialect.SybaseDialect;
import org.hibernate.dialect.Teradata14Dialect;
import org.hibernate.dialect.TeradataDialect;
import org.hibernate.dialect.TimesTenDialect;
@ -59,194 +88,146 @@ public class DefaultDialectSelector implements LazyServiceResolver<Dialect> {
if ( name.isEmpty() ) {
return null;
}
//Let's organize all string matches in groups by first letter:
final char n = name.charAt( 0 );
switch ( n ) {
case 'C': return caseC( name );
case 'D': return caseD( name );
case 'F': return caseF( name );
case 'H': return caseH( name );
case 'I': return caseI( name );
case 'M': return caseM( name );
case 'O': return caseO( name );
case 'P': return caseP( name );
case 'S': return caseS( name );
case 'T': return caseT( name );
}
return null;
}
private static Class<? extends Dialect> caseC(final String name) {
if ( name.equals( "Cache71" ) ) {
switch ( name ) {
case "Cache":
return CacheDialect.class;
case "Cache71":
return Cache71Dialect.class;
}
// todo (6.0): add CockroachDB
// if ( name.equals( "CockroachDB192" ) ) {
// return CockroachDB192Dialect.class;
// }
// if ( name.equals( "CockroachDB201" ) ) {
// return CockroachDB201Dialect.class;
// }
if ( name.equals( "CUBRID" ) ) {
case "Cockroach":
return CockroachDialect.class;
case "CUBRID":
return CUBRIDDialect.class;
}
return null;
}
private static Class<? extends Dialect> caseD(final String name) {
if ( name.equals( "DB2" ) ) {
case "DB2":
return DB2Dialect.class;
}
if ( name.equals( "DB2390" ) ) {
case "DB2i":
return DB2iDialect.class;
case "DB2z":
return DB2zDialect.class;
case "DB297":
return DB297Dialect.class;
case "DB2390":
return DB2390Dialect.class;
}
if ( name.equals( "DB2390V8" ) ) {
case "DB2390V8":
return DB2390V8Dialect.class;
}
if ( name.equals( "DB2400" ) ) {
case "DB2400":
return DB2400Dialect.class;
}
if ( name.equals( "DB2400V7R3" ) ) {
case "DB2400V7R3":
return DB2400V7R3Dialect.class;
}
if ( name.equals( "DerbyTenFive" ) ) {
case "Derby":
return DerbyDialect.class;
case "DerbyTenFive":
return DerbyTenFiveDialect.class;
}
if ( name.equals( "DerbyTenSix" ) ) {
case "DerbyTenSix":
return DerbyTenSixDialect.class;
}
if ( name.equals( "DerbyTenSeven" ) ) {
case "DerbyTenSeven":
return DerbyTenSevenDialect.class;
}
return null;
}
private static Class<? extends Dialect> caseF(final String name) {
if ( name.equals( "Firebird" ) ) {
case "Firebird":
return FirebirdDialect.class;
}
return null;
}
private static Class<? extends Dialect> caseH(final String name) {
if ( name.equals( "H2" ) ) {
case "H2":
return H2Dialect.class;
}
if ( name.equals( "HANACloudColumnStore" ) ) {
case "HANACloudColumnStore":
return HANACloudColumnStoreDialect.class;
}
if ( name.equals( "HANAColumnStore" ) ) {
case "HANAColumnStore":
return HANAColumnStoreDialect.class;
}
if ( name.equals( "HANARowStore" ) ) {
case "HANARowStore":
return HANARowStoreDialect.class;
}
if ( name.equals( "HSQL" ) ) {
case "HSQL":
return HSQLDialect.class;
}
return null;
}
private static Class<? extends Dialect> caseI(final String name) {
if ( name.equals( "Informix" ) ) {
case "Informix":
return InformixDialect.class;
}
if ( name.equals( "Ingres" ) ) {
case "Informix10":
return Informix10Dialect.class;
case "Ingres":
return IngresDialect.class;
}
if ( name.equals( "Ingres9" ) ) {
case "Ingres9":
return Ingres9Dialect.class;
}
if ( name.equals( "Ingres10" ) ) {
case "Ingres10":
return Ingres10Dialect.class;
}
return null;
}
private static Class<? extends Dialect> caseM(final String name) {
if ( name.equals( "MimerSQL" ) ) {
case "MariaDB":
return MariaDBDialect.class;
case "MariaDB53":
return MariaDB53Dialect.class;
case "MariaDB10":
return MariaDB10Dialect.class;
case "MariaDB102":
return MariaDB102Dialect.class;
case "MariaDB103":
return MariaDB103Dialect.class;
case "MaxDB":
return MaxDBDialect.class;
case "MimerSQL":
return MimerSQLDialect.class;
}
if ( name.equals( "MySQL5" ) ) {
case "MySQL":
return MySQLDialect.class;
case "MySQL5":
return MySQL5Dialect.class;
}
if ( name.equals( "MySQL57" ) ) {
case "MySQL55":
return MySQL55Dialect.class;
case "MySQL57":
return MySQL57Dialect.class;
}
if ( name.equals( "MySQL8" ) ) {
case "MySQL8":
return MySQL8Dialect.class;
}
return null;
}
private static Class<? extends Dialect> caseO(final String name) {
if ( name.equals( "Oracle8i" ) ) {
case "Oracle":
return OracleDialect.class;
case "Oracle8i":
return Oracle8iDialect.class;
}
if ( name.equals( "Oracle9i" ) ) {
case "Oracle9i":
return Oracle9iDialect.class;
}
if ( name.equals( "Oracle10g" ) ) {
case "Oracle10g":
return Oracle10gDialect.class;
}
return null;
}
private static Class<? extends Dialect> caseP(final String name) {
if ( name.equals( "PostgresPlus" ) ) {
case "Oracle12c":
return Oracle12cDialect.class;
case "PostgresPlus":
return PostgresPlusDialect.class;
}
if ( name.equals( "PostgreSQL81" ) ) {
case "PostgreSQL":
return PostgreSQLDialect.class;
case "PostgreSQL81":
return PostgreSQL81Dialect.class;
}
if ( name.equals( "PostgreSQL82" ) ) {
case "PostgreSQL82":
return PostgreSQL82Dialect.class;
}
if ( name.equals( "PostgreSQL9" ) ) {
case "PostgreSQL9":
return PostgreSQL9Dialect.class;
}
return null;
}
private static Class<? extends Dialect> caseS(final String name) {
if ( name.equals( "SAPDB" ) ) {
case "PostgreSQL91":
return PostgreSQL91Dialect.class;
case "PostgreSQL92":
return PostgreSQL92Dialect.class;
case "PostgreSQL93":
return PostgreSQL93Dialect.class;
case "PostgreSQL94":
return PostgreSQL94Dialect.class;
case "PostgreSQL95":
return PostgreSQL95Dialect.class;
case "RDMSOS2200":
return RDMSOS2200Dialect.class;
case "SAPDB":
return SAPDBDialect.class;
}
if ( name.equals( "SQLServer" ) ) {
case "Spanner":
return SpannerDialect.class;
case "SQLServer":
return SQLServerDialect.class;
}
if ( name.equals( "SQLServer2005" ) ) {
case "SQLServer2005":
return SQLServer2005Dialect.class;
}
if ( name.equals( "SQLServer2008" ) ) {
case "SQLServer2008":
return SQLServer2008Dialect.class;
}
if ( name.equals( "Sybase11" ) ) {
case "SQLServer2012":
return SQLServer2012Dialect.class;
case "Sybase":
return SybaseDialect.class;
case "Sybase11":
return Sybase11Dialect.class;
}
if ( name.equals( "SybaseAnywhere" ) ) {
case "SybaseAnywhere":
return SybaseAnywhereDialect.class;
}
if ( name.equals( "Sybase11" ) ) {
return Sybase11Dialect.class;
}
if ( name.equals( "SybaseAnywhere" ) ) {
return SybaseAnywhereDialect.class;
}
if ( name.equals( "SybaseASE15" ) ) {
case "SybaseASE":
return SybaseASEDialect.class;
case "SybaseASE15":
return SybaseASE15Dialect.class;
}
if ( name.equals( "SybaseASE157" ) ) {
case "SybaseASE157":
return SybaseASE157Dialect.class;
}
return null;
}
private static Class<? extends Dialect> caseT(final String name) {
if ( name.equals( "Teradata" ) ) {
case "Teradata":
return TeradataDialect.class;
}
if ( name.equals( "TimesTen" ) ) {
case "Teradata14":
return Teradata14Dialect.class;
case "TimesTen":
return TimesTenDialect.class;
}
return null;

View File

@ -6,6 +6,12 @@
*/
package org.hibernate.dialect;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.hibernate.engine.jdbc.dialect.spi.BasicSQLExceptionConverter;
import org.hibernate.engine.jdbc.dialect.spi.DialectResolutionInfo;
/**
@ -77,7 +83,7 @@ public enum Database {
DERBY {
@Override
public Dialect createDialect(DialectResolutionInfo info) {
return new DerbyDialect(info);
return new DerbyDialect( info );
}
@Override
public boolean productNameMatches(String databaseName) {
@ -85,7 +91,7 @@ public enum Database {
}
@Override
public String getDriverClassName(String jdbcUrl) {
return jdbcUrl.startsWith("jdbc:derby://")
return jdbcUrl.startsWith( "jdbc:derby://" )
? "org.apache.derby.jdbc.ClientDriver"
: "org.apache.derby.jdbc.EmbeddedDriver";
}
@ -94,7 +100,7 @@ public enum Database {
ENTERPRISEDB {
@Override
public Dialect createDialect(DialectResolutionInfo info) {
return new PostgresPlusDialect();
return new PostgresPlusDialect( info );
}
@Override
public boolean productNameMatches(String databaseName) {
@ -113,7 +119,7 @@ public enum Database {
FIREBIRD {
@Override
public Dialect createDialect(DialectResolutionInfo info) {
return new FirebirdDialect(info);
return new FirebirdDialect( info );
}
@Override
public boolean productNameMatches(String databaseName) {
@ -132,7 +138,7 @@ public enum Database {
H2 {
@Override
public Dialect createDialect(DialectResolutionInfo info) {
return new H2Dialect(info);
return new H2Dialect( info );
}
@Override
public boolean productNameMatches(String databaseName) {
@ -147,7 +153,7 @@ public enum Database {
HANA {
@Override
public Dialect createDialect(DialectResolutionInfo info) {
return new HANAColumnStoreDialect(info);
return new HANAColumnStoreDialect( info );
}
@Override
public boolean productNameMatches(String databaseName) {
@ -166,7 +172,7 @@ public enum Database {
HSQL {
@Override
public Dialect createDialect(DialectResolutionInfo info) {
return new HSQLDialect(info);
return new HSQLDialect( info );
}
@Override
public boolean productNameMatches(String databaseName) {
@ -185,12 +191,12 @@ public enum Database {
INFORMIX {
@Override
public Dialect createDialect(DialectResolutionInfo info) {
return new InformixDialect(info);
return new InformixDialect( info );
}
@Override
public boolean productNameMatches(String databaseName) {
//usually "Informix Dynamic Server"
return databaseName.toLowerCase().startsWith("informix");
return databaseName.toLowerCase().startsWith( "informix" );
}
@Override
public String getDriverClassName(String jdbcUrl) {
@ -205,11 +211,11 @@ public enum Database {
INGRES {
@Override
public Dialect createDialect(DialectResolutionInfo info) {
return new IngresDialect(info);
return new IngresDialect( info );
}
@Override
public boolean productNameMatches(String databaseName) {
return databaseName.toLowerCase().startsWith("ingres");
return databaseName.toLowerCase().startsWith( "ingres" );
}
@Override
public String getDriverClassName(String jdbcUrl) {
@ -226,12 +232,12 @@ public enum Database {
else {
//in case the product name has been set to MySQL
String driverName = info.getDriverName();
return driverName != null && driverName.startsWith("MariaDB");
return driverName != null && driverName.startsWith( "MariaDB" );
}
}
@Override
public Dialect createDialect(DialectResolutionInfo info) {
return new MariaDBDialect(info);
return new MariaDBDialect( info );
}
@Override
public boolean productNameMatches(String productName) {
@ -250,8 +256,8 @@ public enum Database {
}
@Override
public boolean productNameMatches(String databaseName) {
return databaseName.toLowerCase().startsWith("sap db")
|| databaseName.toLowerCase().startsWith("maxdb");
return databaseName.toLowerCase().startsWith( "sap db" )
|| databaseName.toLowerCase().startsWith( "maxdb" );
}
@Override
public String getDriverClassName(String jdbcUrl) {
@ -270,7 +276,7 @@ public enum Database {
}
@Override
public boolean productNameMatches(String databaseName) {
return databaseName.startsWith("Mimer SQL");
return databaseName.startsWith( "Mimer SQL" );
}
@Override
public String getDriverClassName(String jdbcUrl) {
@ -281,7 +287,7 @@ public enum Database {
MYSQL {
@Override
public Dialect createDialect(DialectResolutionInfo info) {
return new MySQLDialect(info);
return new MySQLDialect( info );
}
@Override
public boolean productNameMatches(String databaseName) {
@ -296,7 +302,7 @@ public enum Database {
ORACLE {
@Override
public Dialect createDialect(DialectResolutionInfo info) {
return new OracleDialect(info);
return new OracleDialect( info );
}
@Override
public boolean productNameMatches(String databaseName) {
@ -311,7 +317,11 @@ public enum Database {
POSTGRESQL {
@Override
public Dialect createDialect(DialectResolutionInfo info) {
return new PostgreSQLDialect(info);
final String version = getVersion( info.unwrap( DatabaseMetaData.class ) );
if ( version.startsWith( "Cockroach" ) ) {
return new CockroachDialect( info );
}
return new PostgreSQLDialect( info );
}
@Override
public boolean productNameMatches(String databaseName) {
@ -321,12 +331,39 @@ public enum Database {
public String getDriverClassName(String jdbcUrl) {
return "org.postgresql.Driver";
}
private String getVersion(DatabaseMetaData databaseMetaData) {
try (Statement statement = databaseMetaData.getConnection().createStatement() ) {
final ResultSet rs = statement.executeQuery( "select version()" );
if ( rs.next() ) {
return rs.getString( 1 );
}
}
catch (SQLException e) {
throw BasicSQLExceptionConverter.INSTANCE.convert( e );
}
return "";
}
},
SPANNER {
@Override
public Dialect createDialect(DialectResolutionInfo info) {
return new SpannerDialect();
}
@Override
public boolean productNameMatches(String databaseName) {
return databaseName.startsWith( "Google Cloud Spanner" );
}
@Override
public String getDriverClassName(String jdbcUrl) {
return "com.google.cloud.spanner.jdbc.JdbcDriver";
}
},
SQLSERVER {
@Override
public Dialect createDialect(DialectResolutionInfo info) {
return new SQLServerDialect(info);
return new SQLServerDialect( info );
}
@Override
public boolean productNameMatches(String databaseName) {
@ -343,7 +380,7 @@ public enum Database {
public Dialect createDialect(DialectResolutionInfo info) {
final String databaseName = info.getDatabaseName();
if ( isASE( databaseName ) ) {
return new SybaseASEDialect(info);
return new SybaseASEDialect( info );
}
if ( isASA( databaseName ) ) {
return new SybaseAnywhereDialect();
@ -364,15 +401,15 @@ public enum Database {
}
@Override
public boolean matchesUrl(String jdbcUrl) {
return jdbcUrl.startsWith("jdbc:sybase:")
|| jdbcUrl.startsWith("jdbc:sqlanywhere:");
return jdbcUrl.startsWith( "jdbc:sybase:" )
|| jdbcUrl.startsWith( "jdbc:sqlanywhere:" );
}
},
TERADATA {
@Override
public Dialect createDialect(DialectResolutionInfo info) {
return new TeradataDialect(info);
return new TeradataDialect( info );
}
@Override
public boolean productNameMatches(String databaseName) {
@ -391,7 +428,7 @@ public enum Database {
}
@Override
public boolean productNameMatches(String databaseName) {
return databaseName.toLowerCase().startsWith("timesten");
return databaseName.toLowerCase().startsWith( "timesten" );
}
};

View File

@ -82,7 +82,7 @@ public class PostgreSQLDialect extends Dialect {
}
public PostgreSQLDialect() {
this(800);
this( 800 );
}
public PostgreSQLDialect(int version) {

View File

@ -11,6 +11,7 @@ import java.sql.SQLException;
import java.sql.Types;
import org.hibernate.dialect.function.CommonFunctionFactory;
import org.hibernate.engine.jdbc.dialect.spi.DialectResolutionInfo;
import org.hibernate.query.spi.QueryEngine;
/**
@ -19,6 +20,7 @@ import org.hibernate.query.spi.QueryEngine;
* @author Jim Mlodgenski
*/
public class PostgresPlusDialect extends PostgreSQLDialect {
/**
* Constructs a PostgresPlusDialect
*/
@ -26,6 +28,14 @@ public class PostgresPlusDialect extends PostgreSQLDialect {
super();
}
public PostgresPlusDialect(DialectResolutionInfo info) {
super( info );
}
public PostgresPlusDialect(int version) {
super( version );
}
@Override
public void initializeFunctionRegistry(QueryEngine queryEngine) {
super.initializeFunctionRegistry( queryEngine );

View File

@ -55,9 +55,9 @@ public class DialectFactoryImpl implements DialectFactory, ServiceRegistryAwareS
@Override
public Dialect buildDialect(Map configValues, DialectResolutionInfoSource resolutionInfoSource) throws HibernateException {
final Object dialectReference = configValues.get( AvailableSettings.DIALECT );
Dialect dialect = !isEmpty(dialectReference) ?
constructDialect(dialectReference, resolutionInfoSource) :
determineDialect(resolutionInfoSource);
Dialect dialect = !isEmpty( dialectReference ) ?
constructDialect( dialectReference, resolutionInfoSource ) :
determineDialect( resolutionInfoSource );
logSelectedDialect( dialect );
return dialect;
}
@ -66,10 +66,10 @@ public class DialectFactoryImpl implements DialectFactory, ServiceRegistryAwareS
LOG.usingDialect( dialect );
Class<? extends Dialect> dialectClass = dialect.getClass();
if ( dialectClass.isAnnotationPresent(Deprecated.class) ) {
if ( dialectClass.isAnnotationPresent( Deprecated.class ) ) {
Class<?> superDialectClass = dialectClass.getSuperclass();
if ( !superDialectClass.isAnnotationPresent(Deprecated.class)
&& !superDialectClass.equals(Dialect.class) ) {
if ( !superDialectClass.isAnnotationPresent( Deprecated.class )
&& !superDialectClass.equals( Dialect.class ) ) {
DEPRECATION_LOGGER.deprecatedDialect( dialectClass.getSimpleName(), superDialectClass.getName() );
}
else {
@ -101,7 +101,7 @@ public class DialectFactoryImpl implements DialectFactory, ServiceRegistryAwareS
try {
try {
if (resolutionInfoSource != null) {
return dialectClass.getConstructor(DialectResolutionInfo.class).newInstance(
return dialectClass.getConstructor( DialectResolutionInfo.class ).newInstance(
resolutionInfoSource.getDialectResolutionInfo()
);
}

View File

@ -87,4 +87,12 @@ public class DatabaseMetaDataDialectResolutionInfoAdapter implements DialectReso
public int getDriverMinorVersion() {
return interpretVersion( databaseMetaData.getDriverMinorVersion() );
}
@Override
public <T> T unwrap(Class<T> clazz) {
if ( clazz.isInstance( databaseMetaData ) ) {
return clazz.cast( databaseMetaData );
}
return null;
}
}

View File

@ -91,5 +91,15 @@ public interface DialectResolutionInfo {
*/
public int getDriverMinorVersion();
/**
* Obtain access to the underlying object of the given type.
*
* Return <code>null</code> if the underlying object is not of the given type.
*
* @return The unwrapped object or <code>null</code>
*/
default <T> T unwrap(Class<T> clazz) {
return null;
}
}