use of org.hibernate.dialect.Dialect in project hibernate-orm by hibernate.
the class AbstractLoadPlanBasedLoader method prepareQueryStatement.
/**
* Obtain a <tt>PreparedStatement</tt> with all parameters pre-bound.
* Bind JDBC-style <tt>?</tt> parameters, named parameters, and
* limit parameters.
*/
protected final PreparedStatement prepareQueryStatement(final String sql, final QueryParameters queryParameters, final LimitHandler limitHandler, final boolean scroll, final SharedSessionContractImplementor session) throws SQLException, HibernateException {
final Dialect dialect = session.getJdbcServices().getJdbcEnvironment().getDialect();
final RowSelection selection = queryParameters.getRowSelection();
final boolean useLimit = LimitHelper.useLimit(limitHandler, selection);
final boolean hasFirstRow = LimitHelper.hasFirstRow(selection);
final boolean useLimitOffset = hasFirstRow && useLimit && limitHandler.supportsLimitOffset();
final boolean callable = queryParameters.isCallable();
final ScrollMode scrollMode = getScrollMode(scroll, hasFirstRow, useLimitOffset, queryParameters);
final PreparedStatement st = session.getJdbcCoordinator().getStatementPreparer().prepareQueryStatement(sql, callable, scrollMode);
try {
int col = 1;
//TODO: can we limit stored procedures ?!
col += limitHandler.bindLimitParametersAtStartOfQuery(selection, st, col);
if (callable) {
col = dialect.registerResultSetOutParameter((CallableStatement) st, col);
}
col += bindParameterValues(st, queryParameters, col, session);
col += limitHandler.bindLimitParametersAtEndOfQuery(selection, st, col);
limitHandler.setMaxRows(selection, st);
if (selection != null) {
if (selection.getTimeout() != null) {
st.setQueryTimeout(selection.getTimeout());
}
if (selection.getFetchSize() != null) {
st.setFetchSize(selection.getFetchSize());
}
}
// handle lock timeout...
final LockOptions lockOptions = queryParameters.getLockOptions();
if (lockOptions != null) {
if (lockOptions.getTimeOut() != LockOptions.WAIT_FOREVER) {
if (!dialect.supportsLockTimeouts()) {
if (log.isDebugEnabled()) {
log.debugf("Lock timeout [%s] requested but dialect reported to not support lock timeouts", lockOptions.getTimeOut());
}
} else if (dialect.isLockTimeoutParameterized()) {
st.setInt(col++, lockOptions.getTimeOut());
}
}
}
if (log.isTraceEnabled()) {
log.tracev("Bound [{0}] parameters total", col);
}
} catch (SQLException sqle) {
session.getJdbcCoordinator().getResourceRegistry().release(st);
session.getJdbcCoordinator().afterStatementExecution();
throw sqle;
} catch (HibernateException he) {
session.getJdbcCoordinator().getResourceRegistry().release(st);
session.getJdbcCoordinator().afterStatementExecution();
throw he;
}
return st;
}
use of org.hibernate.dialect.Dialect in project hibernate-orm by hibernate.
the class EntityLoadQueryDetails method applyRootReturnTableFragments.
/**
* Applies "table fragments" to the FROM-CLAUSE of the given SelectStatementBuilder for the given Loadable
*
* @param select The SELECT statement builder
*
* @see org.hibernate.persister.entity.OuterJoinLoadable#fromTableFragment(java.lang.String)
* @see org.hibernate.persister.entity.Joinable#fromJoinFragment(java.lang.String, boolean, boolean)
*/
protected void applyRootReturnTableFragments(SelectStatementBuilder select) {
final String fromTableFragment;
final String rootAlias = entityReferenceAliases.getTableAlias();
final OuterJoinLoadable outerJoinLoadable = (OuterJoinLoadable) getRootEntityReturn().getEntityPersister();
final Dialect dialect = getSessionFactory().getJdbcServices().getJdbcEnvironment().getDialect();
if (getQueryBuildingParameters().getLockOptions() != null) {
fromTableFragment = dialect.appendLockHint(getQueryBuildingParameters().getLockOptions(), outerJoinLoadable.fromTableFragment(rootAlias));
select.setLockOptions(getQueryBuildingParameters().getLockOptions());
} else if (getQueryBuildingParameters().getLockMode() != null) {
fromTableFragment = dialect.appendLockHint(getQueryBuildingParameters().getLockMode(), outerJoinLoadable.fromTableFragment(rootAlias));
select.setLockMode(getQueryBuildingParameters().getLockMode());
} else {
fromTableFragment = outerJoinLoadable.fromTableFragment(rootAlias);
}
select.appendFromClauseFragment(fromTableFragment + outerJoinLoadable.fromJoinFragment(rootAlias, true, true));
}
use of org.hibernate.dialect.Dialect in project hibernate-orm by hibernate.
the class DbTimestampType method getCurrentTimestamp.
private Date getCurrentTimestamp(SharedSessionContractImplementor session) {
Dialect dialect = session.getJdbcServices().getJdbcEnvironment().getDialect();
String timestampSelectString = dialect.getCurrentTimestampSelectString();
if (dialect.isCurrentTimestampSelectStringCallable()) {
return useCallableStatement(timestampSelectString, session);
}
return usePreparedStatement(timestampSelectString, session);
}
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 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