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

View File

@ -6,6 +6,12 @@
*/ */
package org.hibernate.dialect; 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; import org.hibernate.engine.jdbc.dialect.spi.DialectResolutionInfo;
/** /**
@ -94,7 +100,7 @@ public enum Database {
ENTERPRISEDB { ENTERPRISEDB {
@Override @Override
public Dialect createDialect(DialectResolutionInfo info) { public Dialect createDialect(DialectResolutionInfo info) {
return new PostgresPlusDialect(); return new PostgresPlusDialect( info );
} }
@Override @Override
public boolean productNameMatches(String databaseName) { public boolean productNameMatches(String databaseName) {
@ -311,6 +317,10 @@ public enum Database {
POSTGRESQL { POSTGRESQL {
@Override @Override
public Dialect createDialect(DialectResolutionInfo info) { public Dialect createDialect(DialectResolutionInfo info) {
final String version = getVersion( info.unwrap( DatabaseMetaData.class ) );
if ( version.startsWith( "Cockroach" ) ) {
return new CockroachDialect( info );
}
return new PostgreSQLDialect( info ); return new PostgreSQLDialect( info );
} }
@Override @Override
@ -321,6 +331,33 @@ public enum Database {
public String getDriverClassName(String jdbcUrl) { public String getDriverClassName(String jdbcUrl) {
return "org.postgresql.Driver"; 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 { SQLSERVER {

View File

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

View File

@ -87,4 +87,12 @@ public class DatabaseMetaDataDialectResolutionInfoAdapter implements DialectReso
public int getDriverMinorVersion() { public int getDriverMinorVersion() {
return interpretVersion( databaseMetaData.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(); 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;
}
} }