use of org.hibernate.dialect.Dialect 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));
}
use of org.hibernate.dialect.Dialect in project hibernate-orm by hibernate.
the class AbstractTableBasedBulkIdHandler method generateIdInsertSelect.
/**
* Generate the {@code INSERT}-{@code SELECT} statement for holding matching ids. This is the
* {@code INSERT} used to populate the bulk-id table with ids matching the restrictions defined in the
* original {@code WHERE} clause
*
* @param tableAlias The table alias to use for the entity
* @param whereClause The processed representation for the user-defined {@code WHERE} clause.
*
* @return The {@code INSERT}-{@code SELECT} for populating the bulk-id table.
*/
protected String generateIdInsertSelect(String tableAlias, IdTableInfo idTableInfo, ProcessedWhereClause whereClause) {
final Dialect dialect = sessionFactory.getJdbcServices().getJdbcEnvironment().getDialect();
final Select select = generateIdSelect(tableAlias, whereClause);
InsertSelect insert = new InsertSelect(dialect);
if (sessionFactory.getSessionFactoryOptions().isCommentsEnabled()) {
insert.setComment("insert-select for " + getTargetedQueryable().getEntityName() + " ids");
}
insert.setTableName(idTableInfo.getQualifiedIdTableName());
insert.setSelect(select);
return insert.toStatementString();
}
use of org.hibernate.dialect.Dialect in project hibernate-orm by hibernate.
the class LiteralProcessor method setConstantValue.
private void setConstantValue(DotNode node, String text, Object value) {
if (LOG.isDebugEnabled()) {
LOG.debugf("setConstantValue() %s -> %s %s", text, value, value.getClass().getName());
}
// Chop off the rest of the tree.
node.setFirstChild(null);
if (value instanceof String) {
node.setType(SqlTokenTypes.QUOTED_STRING);
} else if (value instanceof Character) {
node.setType(SqlTokenTypes.QUOTED_STRING);
} else if (value instanceof Byte) {
node.setType(SqlTokenTypes.NUM_INT);
} else if (value instanceof Short) {
node.setType(SqlTokenTypes.NUM_INT);
} else if (value instanceof Integer) {
node.setType(SqlTokenTypes.NUM_INT);
} else if (value instanceof Long) {
node.setType(SqlTokenTypes.NUM_LONG);
} else if (value instanceof Double) {
node.setType(SqlTokenTypes.NUM_DOUBLE);
} else if (value instanceof Float) {
node.setType(SqlTokenTypes.NUM_FLOAT);
} else {
node.setType(SqlTokenTypes.CONSTANT);
}
Type type;
try {
type = walker.getSessionFactoryHelper().getFactory().getTypeResolver().heuristicType(value.getClass().getName());
} catch (MappingException me) {
throw new QueryException(me);
}
if (type == null) {
throw new QueryException(QueryTranslator.ERROR_CANNOT_DETERMINE_TYPE + node.getText());
}
try {
LiteralType literalType = (LiteralType) type;
Dialect dialect = walker.getSessionFactoryHelper().getFactory().getDialect();
// noinspection unchecked
node.setText(literalType.objectToSQLString(value, dialect));
} catch (Exception e) {
throw new QueryException(QueryTranslator.ERROR_CANNOT_FORMAT_LITERAL + node.getText(), e);
}
node.setDataType(type);
node.setResolvedConstant(text);
}
use of org.hibernate.dialect.Dialect in project hibernate-orm by hibernate.
the class MetadataBuildingProcess method handleTypes.
// todo (7.0) : buildJandexInitializer
// private static JandexInitManager buildJandexInitializer(
// MetadataBuildingOptions options,
// ClassLoaderAccess classLoaderAccess) {
// final boolean autoIndexMembers = ConfigurationHelper.getBoolean(
// org.hibernate.cfg.AvailableSettings.ENABLE_AUTO_INDEX_MEMBER_TYPES,
// options.getServiceRegistry().getService( ConfigurationService.class ).getSettings(),
// false
// );
//
// return new JandexInitManager( options.getJandexView(), classLoaderAccess, autoIndexMembers );
// }
private static void handleTypes(BootstrapContext bootstrapContext, MetadataBuildingOptions options) {
final ClassLoaderService classLoaderService = options.getServiceRegistry().getService(ClassLoaderService.class);
final TypeContributions typeContributions = new TypeContributions() {
@Override
public void contributeType(org.hibernate.type.BasicType type) {
getBasicTypeRegistry().register(type);
}
@Override
public void contributeType(BasicType type, String... keys) {
getBasicTypeRegistry().register(type, keys);
}
@Override
public void contributeType(UserType type, String[] keys) {
getBasicTypeRegistry().register(type, keys);
}
@Override
public void contributeType(CompositeUserType type, String[] keys) {
getBasicTypeRegistry().register(type, keys);
}
@Override
public void contributeJavaTypeDescriptor(JavaTypeDescriptor descriptor) {
bootstrapContext.getTypeConfiguration().getJavaTypeDescriptorRegistry().addDescriptor(descriptor);
}
@Override
public void contributeSqlTypeDescriptor(SqlTypeDescriptor descriptor) {
bootstrapContext.getTypeConfiguration().getSqlTypeDescriptorRegistry().addDescriptor(descriptor);
}
@Override
public TypeConfiguration getTypeConfiguration() {
return bootstrapContext.getTypeConfiguration();
}
final BasicTypeRegistry getBasicTypeRegistry() {
return bootstrapContext.getTypeConfiguration().getBasicTypeRegistry();
}
};
// add Dialect contributed types
final Dialect dialect = options.getServiceRegistry().getService(JdbcServices.class).getDialect();
dialect.contributeTypes(typeContributions, options.getServiceRegistry());
// add TypeContributor contributed types.
for (TypeContributor contributor : classLoaderService.loadJavaServices(TypeContributor.class)) {
contributor.contribute(typeContributions, options.getServiceRegistry());
}
// add explicit application registered types
for (BasicTypeRegistration basicTypeRegistration : options.getBasicTypeRegistrations()) {
bootstrapContext.getTypeConfiguration().getBasicTypeRegistry().register(basicTypeRegistration.getBasicType(), basicTypeRegistration.getRegistrationKeys());
}
}
use of org.hibernate.dialect.Dialect 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;
}
Aggregations