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,195 +88,147 @@ 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 ); return Cache71Dialect.class;
case 'F': return caseF( name ); case "Cockroach":
case 'H': return caseH( name ); return CockroachDialect.class;
case 'I': return caseI( name ); case "CUBRID":
case 'M': return caseM( name ); return CUBRIDDialect.class;
case 'O': return caseO( name ); case "DB2":
case 'P': return caseP( name ); return DB2Dialect.class;
case 'S': return caseS( name ); case "DB2i":
case 'T': return caseT( name ); return DB2iDialect.class;
} case "DB2z":
return null; return DB2zDialect.class;
} case "DB297":
return DB297Dialect.class;
private static Class<? extends Dialect> caseC(final String name) { case "DB2390":
if ( name.equals( "Cache71" ) ) { return DB2390Dialect.class;
return Cache71Dialect.class; case "DB2390V8":
} return DB2390V8Dialect.class;
// todo (6.0): add CockroachDB case "DB2400":
// if ( name.equals( "CockroachDB192" ) ) { return DB2400Dialect.class;
// return CockroachDB192Dialect.class; case "DB2400V7R3":
// } return DB2400V7R3Dialect.class;
// if ( name.equals( "CockroachDB201" ) ) { case "Derby":
// return CockroachDB201Dialect.class; return DerbyDialect.class;
// } case "DerbyTenFive":
if ( name.equals( "CUBRID" ) ) { return DerbyTenFiveDialect.class;
return CUBRIDDialect.class; case "DerbyTenSix":
} return DerbyTenSixDialect.class;
return null; case "DerbyTenSeven":
} return DerbyTenSevenDialect.class;
case "Firebird":
private static Class<? extends Dialect> caseD(final String name) { return FirebirdDialect.class;
if ( name.equals( "DB2" ) ) { case "H2":
return DB2Dialect.class; return H2Dialect.class;
} case "HANACloudColumnStore":
if ( name.equals( "DB2390" ) ) { return HANACloudColumnStoreDialect.class;
return DB2390Dialect.class; case "HANAColumnStore":
} return HANAColumnStoreDialect.class;
if ( name.equals( "DB2390V8" ) ) { case "HANARowStore":
return DB2390V8Dialect.class; return HANARowStoreDialect.class;
} case "HSQL":
if ( name.equals( "DB2400" ) ) { return HSQLDialect.class;
return DB2400Dialect.class; case "Informix":
} return InformixDialect.class;
if ( name.equals( "DB2400V7R3" ) ) { case "Informix10":
return DB2400V7R3Dialect.class; return Informix10Dialect.class;
} case "Ingres":
if ( name.equals( "DerbyTenFive" ) ) { return IngresDialect.class;
return DerbyTenFiveDialect.class; case "Ingres9":
} return Ingres9Dialect.class;
if ( name.equals( "DerbyTenSix" ) ) { case "Ingres10":
return DerbyTenSixDialect.class; return Ingres10Dialect.class;
} case "MariaDB":
if ( name.equals( "DerbyTenSeven" ) ) { return MariaDBDialect.class;
return DerbyTenSevenDialect.class; case "MariaDB53":
} return MariaDB53Dialect.class;
return null; case "MariaDB10":
} return MariaDB10Dialect.class;
case "MariaDB102":
private static Class<? extends Dialect> caseF(final String name) { return MariaDB102Dialect.class;
if ( name.equals( "Firebird" ) ) { case "MariaDB103":
return FirebirdDialect.class; return MariaDB103Dialect.class;
} case "MaxDB":
return null; return MaxDBDialect.class;
} case "MimerSQL":
return MimerSQLDialect.class;
private static Class<? extends Dialect> caseH(final String name) { case "MySQL":
if ( name.equals( "H2" ) ) { return MySQLDialect.class;
return H2Dialect.class; case "MySQL5":
} return MySQL5Dialect.class;
if ( name.equals( "HANACloudColumnStore" ) ) { case "MySQL55":
return HANACloudColumnStoreDialect.class; return MySQL55Dialect.class;
} case "MySQL57":
if ( name.equals( "HANAColumnStore" ) ) { return MySQL57Dialect.class;
return HANAColumnStoreDialect.class; case "MySQL8":
} return MySQL8Dialect.class;
if ( name.equals( "HANARowStore" ) ) { case "Oracle":
return HANARowStoreDialect.class; return OracleDialect.class;
} case "Oracle8i":
if ( name.equals( "HSQL" ) ) { return Oracle8iDialect.class;
return HSQLDialect.class; case "Oracle9i":
} return Oracle9iDialect.class;
return null; case "Oracle10g":
} return Oracle10gDialect.class;
case "Oracle12c":
private static Class<? extends Dialect> caseI(final String name) { return Oracle12cDialect.class;
if ( name.equals( "Informix" ) ) { case "PostgresPlus":
return InformixDialect.class; return PostgresPlusDialect.class;
} case "PostgreSQL":
if ( name.equals( "Ingres" ) ) { return PostgreSQLDialect.class;
return IngresDialect.class; case "PostgreSQL81":
} return PostgreSQL81Dialect.class;
if ( name.equals( "Ingres9" ) ) { case "PostgreSQL82":
return Ingres9Dialect.class; return PostgreSQL82Dialect.class;
} case "PostgreSQL9":
if ( name.equals( "Ingres10" ) ) { return PostgreSQL9Dialect.class;
return Ingres10Dialect.class; case "PostgreSQL91":
} return PostgreSQL91Dialect.class;
return null; case "PostgreSQL92":
} return PostgreSQL92Dialect.class;
case "PostgreSQL93":
private static Class<? extends Dialect> caseM(final String name) { return PostgreSQL93Dialect.class;
if ( name.equals( "MimerSQL" ) ) { case "PostgreSQL94":
return MimerSQLDialect.class; return PostgreSQL94Dialect.class;
} case "PostgreSQL95":
if ( name.equals( "MySQL5" ) ) { return PostgreSQL95Dialect.class;
return MySQL5Dialect.class; case "RDMSOS2200":
} return RDMSOS2200Dialect.class;
if ( name.equals( "MySQL57" ) ) { case "SAPDB":
return MySQL57Dialect.class; return SAPDBDialect.class;
} case "Spanner":
if ( name.equals( "MySQL8" ) ) { return SpannerDialect.class;
return MySQL8Dialect.class; case "SQLServer":
} return SQLServerDialect.class;
return null; case "SQLServer2005":
} return SQLServer2005Dialect.class;
case "SQLServer2008":
private static Class<? extends Dialect> caseO(final String name) { return SQLServer2008Dialect.class;
if ( name.equals( "Oracle8i" ) ) { case "SQLServer2012":
return Oracle8iDialect.class; return SQLServer2012Dialect.class;
} case "Sybase":
if ( name.equals( "Oracle9i" ) ) { return SybaseDialect.class;
return Oracle9iDialect.class; case "Sybase11":
} return Sybase11Dialect.class;
if ( name.equals( "Oracle10g" ) ) { case "SybaseAnywhere":
return Oracle10gDialect.class; return SybaseAnywhereDialect.class;
} case "SybaseASE":
return null; return SybaseASEDialect.class;
} case "SybaseASE15":
return SybaseASE15Dialect.class;
private static Class<? extends Dialect> caseP(final String name) { case "SybaseASE157":
if ( name.equals( "PostgresPlus" ) ) { return SybaseASE157Dialect.class;
return PostgresPlusDialect.class; case "Teradata":
} return TeradataDialect.class;
if ( name.equals( "PostgreSQL81" ) ) { case "Teradata14":
return PostgreSQL81Dialect.class; return Teradata14Dialect.class;
} case "TimesTen":
if ( name.equals( "PostgreSQL82" ) ) { return TimesTenDialect.class;
return PostgreSQL82Dialect.class;
}
if ( name.equals( "PostgreSQL9" ) ) {
return PostgreSQL9Dialect.class;
}
return null;
}
private static Class<? extends Dialect> caseS(final String name) {
if ( name.equals( "SAPDB" ) ) {
return SAPDBDialect.class;
}
if ( name.equals( "SQLServer" ) ) {
return SQLServerDialect.class;
}
if ( name.equals( "SQLServer2005" ) ) {
return SQLServer2005Dialect.class;
}
if ( name.equals( "SQLServer2008" ) ) {
return SQLServer2008Dialect.class;
}
if ( name.equals( "Sybase11" ) ) {
return Sybase11Dialect.class;
}
if ( name.equals( "SybaseAnywhere" ) ) {
return SybaseAnywhereDialect.class;
}
if ( name.equals( "Sybase11" ) ) {
return Sybase11Dialect.class;
}
if ( name.equals( "SybaseAnywhere" ) ) {
return SybaseAnywhereDialect.class;
}
if ( name.equals( "SybaseASE15" ) ) {
return SybaseASE15Dialect.class;
}
if ( name.equals( "SybaseASE157" ) ) {
return SybaseASE157Dialect.class;
}
return null;
}
private static Class<? extends Dialect> caseT(final String name) {
if ( name.equals( "Teradata" ) ) {
return TeradataDialect.class;
}
if ( name.equals( "TimesTen" ) ) {
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;
/** /**
@ -77,7 +83,7 @@ public enum Database {
DERBY { DERBY {
@Override @Override
public Dialect createDialect(DialectResolutionInfo info) { public Dialect createDialect(DialectResolutionInfo info) {
return new DerbyDialect(info); return new DerbyDialect( info );
} }
@Override @Override
public boolean productNameMatches(String databaseName) { public boolean productNameMatches(String databaseName) {
@ -85,7 +91,7 @@ public enum Database {
} }
@Override @Override
public String getDriverClassName(String jdbcUrl) { public String getDriverClassName(String jdbcUrl) {
return jdbcUrl.startsWith("jdbc:derby://") return jdbcUrl.startsWith( "jdbc:derby://" )
? "org.apache.derby.jdbc.ClientDriver" ? "org.apache.derby.jdbc.ClientDriver"
: "org.apache.derby.jdbc.EmbeddedDriver"; : "org.apache.derby.jdbc.EmbeddedDriver";
} }
@ -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) {
@ -113,7 +119,7 @@ public enum Database {
FIREBIRD { FIREBIRD {
@Override @Override
public Dialect createDialect(DialectResolutionInfo info) { public Dialect createDialect(DialectResolutionInfo info) {
return new FirebirdDialect(info); return new FirebirdDialect( info );
} }
@Override @Override
public boolean productNameMatches(String databaseName) { public boolean productNameMatches(String databaseName) {
@ -132,7 +138,7 @@ public enum Database {
H2 { H2 {
@Override @Override
public Dialect createDialect(DialectResolutionInfo info) { public Dialect createDialect(DialectResolutionInfo info) {
return new H2Dialect(info); return new H2Dialect( info );
} }
@Override @Override
public boolean productNameMatches(String databaseName) { public boolean productNameMatches(String databaseName) {
@ -147,7 +153,7 @@ public enum Database {
HANA { HANA {
@Override @Override
public Dialect createDialect(DialectResolutionInfo info) { public Dialect createDialect(DialectResolutionInfo info) {
return new HANAColumnStoreDialect(info); return new HANAColumnStoreDialect( info );
} }
@Override @Override
public boolean productNameMatches(String databaseName) { public boolean productNameMatches(String databaseName) {
@ -166,7 +172,7 @@ public enum Database {
HSQL { HSQL {
@Override @Override
public Dialect createDialect(DialectResolutionInfo info) { public Dialect createDialect(DialectResolutionInfo info) {
return new HSQLDialect(info); return new HSQLDialect( info );
} }
@Override @Override
public boolean productNameMatches(String databaseName) { public boolean productNameMatches(String databaseName) {
@ -185,12 +191,12 @@ public enum Database {
INFORMIX { INFORMIX {
@Override @Override
public Dialect createDialect(DialectResolutionInfo info) { public Dialect createDialect(DialectResolutionInfo info) {
return new InformixDialect(info); return new InformixDialect( info );
} }
@Override @Override
public boolean productNameMatches(String databaseName) { public boolean productNameMatches(String databaseName) {
//usually "Informix Dynamic Server" //usually "Informix Dynamic Server"
return databaseName.toLowerCase().startsWith("informix"); return databaseName.toLowerCase().startsWith( "informix" );
} }
@Override @Override
public String getDriverClassName(String jdbcUrl) { public String getDriverClassName(String jdbcUrl) {
@ -205,11 +211,11 @@ public enum Database {
INGRES { INGRES {
@Override @Override
public Dialect createDialect(DialectResolutionInfo info) { public Dialect createDialect(DialectResolutionInfo info) {
return new IngresDialect(info); return new IngresDialect( info );
} }
@Override @Override
public boolean productNameMatches(String databaseName) { public boolean productNameMatches(String databaseName) {
return databaseName.toLowerCase().startsWith("ingres"); return databaseName.toLowerCase().startsWith( "ingres" );
} }
@Override @Override
public String getDriverClassName(String jdbcUrl) { public String getDriverClassName(String jdbcUrl) {
@ -226,12 +232,12 @@ public enum Database {
else { else {
//in case the product name has been set to MySQL //in case the product name has been set to MySQL
String driverName = info.getDriverName(); String driverName = info.getDriverName();
return driverName != null && driverName.startsWith("MariaDB"); return driverName != null && driverName.startsWith( "MariaDB" );
} }
} }
@Override @Override
public Dialect createDialect(DialectResolutionInfo info) { public Dialect createDialect(DialectResolutionInfo info) {
return new MariaDBDialect(info); return new MariaDBDialect( info );
} }
@Override @Override
public boolean productNameMatches(String productName) { public boolean productNameMatches(String productName) {
@ -250,8 +256,8 @@ public enum Database {
} }
@Override @Override
public boolean productNameMatches(String databaseName) { public boolean productNameMatches(String databaseName) {
return databaseName.toLowerCase().startsWith("sap db") return databaseName.toLowerCase().startsWith( "sap db" )
|| databaseName.toLowerCase().startsWith("maxdb"); || databaseName.toLowerCase().startsWith( "maxdb" );
} }
@Override @Override
public String getDriverClassName(String jdbcUrl) { public String getDriverClassName(String jdbcUrl) {
@ -270,7 +276,7 @@ public enum Database {
} }
@Override @Override
public boolean productNameMatches(String databaseName) { public boolean productNameMatches(String databaseName) {
return databaseName.startsWith("Mimer SQL"); return databaseName.startsWith( "Mimer SQL" );
} }
@Override @Override
public String getDriverClassName(String jdbcUrl) { public String getDriverClassName(String jdbcUrl) {
@ -281,7 +287,7 @@ public enum Database {
MYSQL { MYSQL {
@Override @Override
public Dialect createDialect(DialectResolutionInfo info) { public Dialect createDialect(DialectResolutionInfo info) {
return new MySQLDialect(info); return new MySQLDialect( info );
} }
@Override @Override
public boolean productNameMatches(String databaseName) { public boolean productNameMatches(String databaseName) {
@ -296,7 +302,7 @@ public enum Database {
ORACLE { ORACLE {
@Override @Override
public Dialect createDialect(DialectResolutionInfo info) { public Dialect createDialect(DialectResolutionInfo info) {
return new OracleDialect(info); return new OracleDialect( info );
} }
@Override @Override
public boolean productNameMatches(String databaseName) { public boolean productNameMatches(String databaseName) {
@ -311,7 +317,11 @@ public enum Database {
POSTGRESQL { POSTGRESQL {
@Override @Override
public Dialect createDialect(DialectResolutionInfo info) { 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 @Override
public boolean productNameMatches(String databaseName) { public boolean productNameMatches(String databaseName) {
@ -321,12 +331,39 @@ 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 {
@Override @Override
public Dialect createDialect(DialectResolutionInfo info) { public Dialect createDialect(DialectResolutionInfo info) {
return new SQLServerDialect(info); return new SQLServerDialect( info );
} }
@Override @Override
public boolean productNameMatches(String databaseName) { public boolean productNameMatches(String databaseName) {
@ -343,7 +380,7 @@ public enum Database {
public Dialect createDialect(DialectResolutionInfo info) { public Dialect createDialect(DialectResolutionInfo info) {
final String databaseName = info.getDatabaseName(); final String databaseName = info.getDatabaseName();
if ( isASE( databaseName ) ) { if ( isASE( databaseName ) ) {
return new SybaseASEDialect(info); return new SybaseASEDialect( info );
} }
if ( isASA( databaseName ) ) { if ( isASA( databaseName ) ) {
return new SybaseAnywhereDialect(); return new SybaseAnywhereDialect();
@ -364,15 +401,15 @@ public enum Database {
} }
@Override @Override
public boolean matchesUrl(String jdbcUrl) { public boolean matchesUrl(String jdbcUrl) {
return jdbcUrl.startsWith("jdbc:sybase:") return jdbcUrl.startsWith( "jdbc:sybase:" )
|| jdbcUrl.startsWith("jdbc:sqlanywhere:"); || jdbcUrl.startsWith( "jdbc:sqlanywhere:" );
} }
}, },
TERADATA { TERADATA {
@Override @Override
public Dialect createDialect(DialectResolutionInfo info) { public Dialect createDialect(DialectResolutionInfo info) {
return new TeradataDialect(info); return new TeradataDialect( info );
} }
@Override @Override
public boolean productNameMatches(String databaseName) { public boolean productNameMatches(String databaseName) {
@ -391,7 +428,7 @@ public enum Database {
} }
@Override @Override
public boolean productNameMatches(String databaseName) { 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() { public PostgreSQLDialect() {
this(800); this( 800 );
} }
public PostgreSQLDialect(int version) { public PostgreSQLDialect(int version) {

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

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

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;
}
} }