Skip to content

Commit

Permalink
HHH-7692 Use Postgres82Dialect for 9 and 9.1.
Browse files Browse the repository at this point in the history
  • Loading branch information
Bryan Turner authored and brmeyer committed Oct 22, 2012
1 parent 189a8d3 commit 1333725
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 11 deletions.
Expand Up @@ -63,7 +63,7 @@
public class StandardDialectResolver extends AbstractDialectResolver {

private static final CoreMessageLogger LOG = Logger.getMessageLogger(CoreMessageLogger.class,
StandardDialectResolver.class.getName());
StandardDialectResolver.class.getName());

@Override
protected Dialect resolveDialectInternal(DatabaseMetaData metaData) throws SQLException {
Expand All @@ -88,7 +88,7 @@ protected Dialect resolveDialectInternal(DatabaseMetaData metaData) throws SQLEx

if ( "PostgreSQL".equals( databaseName ) ) {
final int databaseMinorVersion = metaData.getDatabaseMinorVersion();
if (databaseMajorVersion >= 8 && databaseMinorVersion >= 2) {
if ( databaseMajorVersion > 8 || ( databaseMajorVersion == 8 && databaseMinorVersion >= 2 ) ) {
return new PostgreSQL82Dialect();
}
return new PostgreSQL81Dialect();
Expand All @@ -111,7 +111,7 @@ else if ( databaseMajorVersion == 10 && databaseMinorVersion == 5 ) {
}

if ( "ingres".equalsIgnoreCase( databaseName ) ) {
switch( databaseMajorVersion ) {
switch ( databaseMajorVersion ) {
case 9:
int databaseMinorVersion = metaData.getDatabaseMinorVersion();
if (databaseMinorVersion > 2) {
Expand Down
Expand Up @@ -24,21 +24,23 @@
*/
package org.hibernate.engine.jdbc.dialect.internal;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.sql.DatabaseMetaData;
import java.sql.SQLException;

import org.hibernate.dialect.Dialect;
import org.hibernate.dialect.PostgreSQL81Dialect;
import org.hibernate.dialect.PostgreSQL82Dialect;
import org.hibernate.dialect.SQLServer2005Dialect;
import org.hibernate.dialect.SQLServer2008Dialect;
import org.hibernate.dialect.SQLServerDialect;
import org.hibernate.testing.junit4.BaseUnitTestCase;
import org.junit.Test;

import java.sql.DatabaseMetaData;
import java.sql.SQLException;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

/**
* Unit test of the {@link StandardDialectResolver} class.
*
Expand Down Expand Up @@ -71,6 +73,41 @@ public void testResolveDialectInternalForUnknownSQLServerVersion() throws SQLExc
runSQLServerDialectTest(7, SQLServerDialect.class);
}

@Test
public void testResolveDialectInternalForPostgres81() throws SQLException {
runPostgresDialectTest(8, 1, PostgreSQL81Dialect.class);
}

@Test
public void testResolveDialectInternalForPostgres82() throws SQLException {
runPostgresDialectTest(8, 2, PostgreSQL82Dialect.class);
}

@Test
public void testResolveDialectInternalForPostgres83() throws SQLException {
runPostgresDialectTest(8, 3, PostgreSQL82Dialect.class);
}

@Test
public void testResolveDialectInternalForPostgres84() throws SQLException {
runPostgresDialectTest(8, 4, PostgreSQL82Dialect.class);
}

@Test
public void testResolveDialectInternalForPostgres9() throws SQLException {
runPostgresDialectTest(9, 0, PostgreSQL82Dialect.class);
}

@Test
public void testResolveDialectInternalForPostgres91() throws SQLException {
runPostgresDialectTest(9, 1, PostgreSQL82Dialect.class);
}

@Test
public void testResolveDialectInternalForPostgres92() throws SQLException {
runPostgresDialectTest(9, 2, PostgreSQL82Dialect.class);
}

private static void runSQLServerDialectTest(int version, Class<? extends SQLServerDialect> expectedDialect)
throws SQLException {
DatabaseMetaData metaData = mock(DatabaseMetaData.class);
Expand All @@ -82,4 +119,31 @@ private static void runSQLServerDialectTest(int version, Class<? extends SQLServ
assertTrue("Dialect for SQL Server version " + version + " should be " + expectedDialect.getSimpleName(),
expectedDialect.isInstance(dialect));
}

private static void runPostgresDialectTest(int majorVersion, int minorVersion,
Class<? extends Dialect> expectedDialect) throws SQLException {
runDialectTest("PostgreSQL", majorVersion, minorVersion, expectedDialect);
}

private static void runDialectTest(String productName, int majorVersion, int minorVersion,
Class<? extends Dialect> expectedDialect) throws SQLException {
DatabaseMetaData metaData = mock(DatabaseMetaData.class);
when(metaData.getDatabaseProductName()).thenReturn(productName);
when(metaData.getDatabaseMajorVersion()).thenReturn(majorVersion);
when(metaData.getDatabaseMinorVersion()).thenReturn(minorVersion);

Dialect dialect = new StandardDialectResolver().resolveDialectInternal(metaData);

StringBuilder builder = new StringBuilder(productName)
.append(" ")
.append(majorVersion);
if (minorVersion > 0) {
builder.append(".").append(minorVersion);
}
String dbms = builder.toString();

assertNotNull("Dialect for " + dbms + " should not be null", dialect);
assertTrue("Dialect for " + dbms + " should be " + expectedDialect.getSimpleName(),
expectedDialect.isInstance(dialect));
}
}

0 comments on commit 1333725

Please sign in to comment.