Search in sources :

Example 1 with DialectResolutionInfo

use of org.hibernate.engine.jdbc.dialect.spi.DialectResolutionInfo in project hibernate-orm by hibernate.

the class DialectFactoryTest method testDetermination.

private void testDetermination(final String databaseName, final int majorVersion, final int minorVersion, Class expected, DialectResolver resolver) {
    dialectFactory.setDialectResolver(resolver);
    Dialect resolved = dialectFactory.buildDialect(new Properties(), new DialectResolutionInfoSource() {

        @Override
        public DialectResolutionInfo getDialectResolutionInfo() {
            return TestingDialectResolutionInfo.forDatabaseInfo(databaseName, majorVersion, minorVersion);
        }
    });
    assertEquals(expected, resolved.getClass());
}
Also used : DialectResolutionInfoSource(org.hibernate.engine.jdbc.dialect.spi.DialectResolutionInfoSource) SybaseAnywhereDialect(org.hibernate.dialect.SybaseAnywhereDialect) DerbyTenSevenDialect(org.hibernate.dialect.DerbyTenSevenDialect) H2Dialect(org.hibernate.dialect.H2Dialect) IngresDialect(org.hibernate.dialect.IngresDialect) SybaseASE15Dialect(org.hibernate.dialect.SybaseASE15Dialect) Oracle8iDialect(org.hibernate.dialect.Oracle8iDialect) InformixDialect(org.hibernate.dialect.InformixDialect) DB2Dialect(org.hibernate.dialect.DB2Dialect) PostgreSQL81Dialect(org.hibernate.dialect.PostgreSQL81Dialect) PostgreSQL9Dialect(org.hibernate.dialect.PostgreSQL9Dialect) PostgreSQL82Dialect(org.hibernate.dialect.PostgreSQL82Dialect) MySQLDialect(org.hibernate.dialect.MySQLDialect) Oracle10gDialect(org.hibernate.dialect.Oracle10gDialect) Oracle9iDialect(org.hibernate.dialect.Oracle9iDialect) DerbyDialect(org.hibernate.dialect.DerbyDialect) HSQLDialect(org.hibernate.dialect.HSQLDialect) DerbyTenFiveDialect(org.hibernate.dialect.DerbyTenFiveDialect) PostgresPlusDialect(org.hibernate.dialect.PostgresPlusDialect) SQLServerDialect(org.hibernate.dialect.SQLServerDialect) DB2400Dialect(org.hibernate.dialect.DB2400Dialect) Dialect(org.hibernate.dialect.Dialect) MySQL5Dialect(org.hibernate.dialect.MySQL5Dialect) DerbyTenSixDialect(org.hibernate.dialect.DerbyTenSixDialect) Properties(java.util.Properties) DialectResolutionInfo(org.hibernate.engine.jdbc.dialect.spi.DialectResolutionInfo)

Example 2 with DialectResolutionInfo

use of org.hibernate.engine.jdbc.dialect.spi.DialectResolutionInfo in project hibernate-orm by hibernate.

the class HibernateSchemaManagementTool method resolveJdbcContext.

public JdbcContext resolveJdbcContext(Map configurationValues) {
    final JdbcContextBuilder jdbcContextBuilder = new JdbcContextBuilder(serviceRegistry);
    // see if a specific connection has been provided
    final Connection providedConnection = (Connection) configurationValues.get(HBM2DDL_CONNECTION);
    if (providedConnection != null) {
        jdbcContextBuilder.jdbcConnectionAccess = new JdbcConnectionAccessProvidedConnectionImpl(providedConnection);
    }
    // see if a specific Dialect override has been provided...
    final String explicitDbName = (String) configurationValues.get(AvailableSettings.HBM2DDL_DB_NAME);
    if (StringHelper.isNotEmpty(explicitDbName)) {
        final String explicitDbMajor = (String) configurationValues.get(AvailableSettings.HBM2DDL_DB_MAJOR_VERSION);
        final String explicitDbMinor = (String) configurationValues.get(AvailableSettings.HBM2DDL_DB_MINOR_VERSION);
        final Dialect indicatedDialect = serviceRegistry.getService(DialectResolver.class).resolveDialect(new DialectResolutionInfo() {

            @Override
            public String getDatabaseName() {
                return explicitDbName;
            }

            @Override
            public int getDatabaseMajorVersion() {
                return StringHelper.isEmpty(explicitDbMajor) ? NO_VERSION : Integer.parseInt(explicitDbMajor);
            }

            @Override
            public int getDatabaseMinorVersion() {
                return StringHelper.isEmpty(explicitDbMinor) ? NO_VERSION : Integer.parseInt(explicitDbMinor);
            }

            @Override
            public String getDriverName() {
                return null;
            }

            @Override
            public int getDriverMajorVersion() {
                return NO_VERSION;
            }

            @Override
            public int getDriverMinorVersion() {
                return NO_VERSION;
            }
        });
        if (indicatedDialect == null) {
            log.debugf("Unable to resolve indicated Dialect resolution info (%s, %s, %s)", explicitDbName, explicitDbMajor, explicitDbMinor);
        } else {
            jdbcContextBuilder.dialect = indicatedDialect;
        }
    }
    return jdbcContextBuilder.buildJdbcContext();
}
Also used : DialectResolver(org.hibernate.engine.jdbc.dialect.spi.DialectResolver) JdbcConnectionAccessProvidedConnectionImpl(org.hibernate.tool.schema.internal.exec.JdbcConnectionAccessProvidedConnectionImpl) Connection(java.sql.Connection) Dialect(org.hibernate.dialect.Dialect) DialectResolutionInfo(org.hibernate.engine.jdbc.dialect.spi.DialectResolutionInfo)

Example 3 with DialectResolutionInfo

use of org.hibernate.engine.jdbc.dialect.spi.DialectResolutionInfo in project hibernate-orm by hibernate.

the class DialectFactoryTest method testDialectNotFound.

@Test
public void testDialectNotFound() {
    Map properties = Collections.EMPTY_MAP;
    try {
        dialectFactory.buildDialect(properties, new DialectResolutionInfoSource() {

            @Override
            public DialectResolutionInfo getDialectResolutionInfo() {
                return TestingDialectResolutionInfo.forDatabaseInfo("NoSuchDatabase", 666);
            }
        });
        fail();
    } catch (HibernateException e) {
        assertNull(e.getCause());
    }
}
Also used : DialectResolutionInfoSource(org.hibernate.engine.jdbc.dialect.spi.DialectResolutionInfoSource) HibernateException(org.hibernate.HibernateException) HashMap(java.util.HashMap) Map(java.util.Map) DialectResolutionInfo(org.hibernate.engine.jdbc.dialect.spi.DialectResolutionInfo) Test(org.junit.Test)

Example 4 with DialectResolutionInfo

use of org.hibernate.engine.jdbc.dialect.spi.DialectResolutionInfo in project hibernate-orm by hibernate.

the class JdbcEnvironmentInitiator method initiateService.

@Override
public JdbcEnvironment initiateService(Map configurationValues, ServiceRegistryImplementor registry) {
    final DialectFactory dialectFactory = registry.getService(DialectFactory.class);
    // 'hibernate.temp.use_jdbc_metadata_defaults' is a temporary magic value.
    // The need for it is intended to be alleviated with future development, thus it is
    // not defined as an Environment constant...
    //
    // it is used to control whether we should consult the JDBC metadata to determine
    // certain Settings default values; it is useful to *not* do this when the database
    // may not be available (mainly in tools usage).
    boolean useJdbcMetadata = ConfigurationHelper.getBoolean("hibernate.temp.use_jdbc_metadata_defaults", configurationValues, true);
    if (useJdbcMetadata) {
        final JdbcConnectionAccess jdbcConnectionAccess = buildJdbcConnectionAccess(configurationValues, registry);
        try {
            final Connection connection = jdbcConnectionAccess.obtainConnection();
            try {
                final DatabaseMetaData dbmd = connection.getMetaData();
                if (log.isDebugEnabled()) {
                    log.debugf("Database ->\n" + "       name : %s\n" + "    version : %s\n" + "      major : %s\n" + "      minor : %s", dbmd.getDatabaseProductName(), dbmd.getDatabaseProductVersion(), dbmd.getDatabaseMajorVersion(), dbmd.getDatabaseMinorVersion());
                    log.debugf("Driver ->\n" + "       name : %s\n" + "    version : %s\n" + "      major : %s\n" + "      minor : %s", dbmd.getDriverName(), dbmd.getDriverVersion(), dbmd.getDriverMajorVersion(), dbmd.getDriverMinorVersion());
                    log.debugf("JDBC version : %s.%s", dbmd.getJDBCMajorVersion(), dbmd.getJDBCMinorVersion());
                }
                Dialect dialect = dialectFactory.buildDialect(configurationValues, new DialectResolutionInfoSource() {

                    @Override
                    public DialectResolutionInfo getDialectResolutionInfo() {
                        try {
                            return new DatabaseMetaDataDialectResolutionInfoAdapter(connection.getMetaData());
                        } catch (SQLException sqlException) {
                            throw new HibernateException("Unable to access java.sql.DatabaseMetaData to determine appropriate Dialect to use", sqlException);
                        }
                    }
                });
                return new JdbcEnvironmentImpl(registry, dialect, dbmd);
            } catch (SQLException e) {
                log.unableToObtainConnectionMetadata(e.getMessage());
            } finally {
                try {
                    jdbcConnectionAccess.releaseConnection(connection);
                } catch (SQLException ignore) {
                }
            }
        } catch (Exception e) {
            log.unableToObtainConnectionToQueryMetadata(e.getMessage());
        }
    }
    // if we get here, either we were asked to not use JDBC metadata or accessing the JDBC metadata failed.
    return new JdbcEnvironmentImpl(registry, dialectFactory.buildDialect(configurationValues, null));
}
Also used : SQLException(java.sql.SQLException) HibernateException(org.hibernate.HibernateException) Connection(java.sql.Connection) DatabaseMetaDataDialectResolutionInfoAdapter(org.hibernate.engine.jdbc.dialect.spi.DatabaseMetaDataDialectResolutionInfoAdapter) DialectFactory(org.hibernate.engine.jdbc.dialect.spi.DialectFactory) DatabaseMetaData(java.sql.DatabaseMetaData) SQLException(java.sql.SQLException) HibernateException(org.hibernate.HibernateException) DialectResolutionInfoSource(org.hibernate.engine.jdbc.dialect.spi.DialectResolutionInfoSource) JdbcConnectionAccess(org.hibernate.engine.jdbc.connections.spi.JdbcConnectionAccess) Dialect(org.hibernate.dialect.Dialect) DialectResolutionInfo(org.hibernate.engine.jdbc.dialect.spi.DialectResolutionInfo)

Example 5 with DialectResolutionInfo

use of org.hibernate.engine.jdbc.dialect.spi.DialectResolutionInfo in project hibernate-orm by hibernate.

the class DialectFactoryImpl method determineDialect.

/**
	 * Determine the appropriate Dialect to use given the connection.
	 *
	 * @param resolutionInfoSource Access to DialectResolutionInfo used to resolve the Dialect.
	 *
	 * @return The appropriate dialect instance.
	 *
	 * @throws HibernateException No connection given or no resolver could make
	 * the determination from the given connection.
	 */
private Dialect determineDialect(DialectResolutionInfoSource resolutionInfoSource) {
    if (resolutionInfoSource == null) {
        throw new HibernateException("Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set");
    }
    final DialectResolutionInfo info = resolutionInfoSource.getDialectResolutionInfo();
    final Dialect dialect = dialectResolver.resolveDialect(info);
    if (dialect == null) {
        throw new HibernateException("Unable to determine Dialect to use [name=" + info.getDatabaseName() + ", majorVersion=" + info.getDatabaseMajorVersion() + "]; user must register resolver or explicitly set 'hibernate.dialect'");
    }
    return dialect;
}
Also used : HibernateException(org.hibernate.HibernateException) Dialect(org.hibernate.dialect.Dialect) DialectResolutionInfo(org.hibernate.engine.jdbc.dialect.spi.DialectResolutionInfo)

Aggregations

DialectResolutionInfo (org.hibernate.engine.jdbc.dialect.spi.DialectResolutionInfo)5 Dialect (org.hibernate.dialect.Dialect)4 HibernateException (org.hibernate.HibernateException)3 DialectResolutionInfoSource (org.hibernate.engine.jdbc.dialect.spi.DialectResolutionInfoSource)3 Connection (java.sql.Connection)2 DatabaseMetaData (java.sql.DatabaseMetaData)1 SQLException (java.sql.SQLException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Properties (java.util.Properties)1 DB2400Dialect (org.hibernate.dialect.DB2400Dialect)1 DB2Dialect (org.hibernate.dialect.DB2Dialect)1 DerbyDialect (org.hibernate.dialect.DerbyDialect)1 DerbyTenFiveDialect (org.hibernate.dialect.DerbyTenFiveDialect)1 DerbyTenSevenDialect (org.hibernate.dialect.DerbyTenSevenDialect)1 DerbyTenSixDialect (org.hibernate.dialect.DerbyTenSixDialect)1 H2Dialect (org.hibernate.dialect.H2Dialect)1 HSQLDialect (org.hibernate.dialect.HSQLDialect)1 InformixDialect (org.hibernate.dialect.InformixDialect)1 IngresDialect (org.hibernate.dialect.IngresDialect)1