Search in sources :

Example 31 with Dialect

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;
}
Also used : ScrollMode(org.hibernate.ScrollMode) LockOptions(org.hibernate.LockOptions) SQLException(java.sql.SQLException) HibernateException(org.hibernate.HibernateException) CallableStatement(java.sql.CallableStatement) Dialect(org.hibernate.dialect.Dialect) PreparedStatement(java.sql.PreparedStatement) RowSelection(org.hibernate.engine.spi.RowSelection)

Example 32 with Dialect

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));
}
Also used : OuterJoinLoadable(org.hibernate.persister.entity.OuterJoinLoadable) Dialect(org.hibernate.dialect.Dialect)

Example 33 with Dialect

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);
}
Also used : Dialect(org.hibernate.dialect.Dialect)

Example 34 with Dialect

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));
}
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 35 with Dialect

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;
}
Also used : HibernateException(org.hibernate.HibernateException) Dialect(org.hibernate.dialect.Dialect) DialectResolutionInfo(org.hibernate.engine.jdbc.dialect.spi.DialectResolutionInfo)

Aggregations

Dialect (org.hibernate.dialect.Dialect)45 HibernateException (org.hibernate.HibernateException)9 JdbcEnvironment (org.hibernate.engine.jdbc.env.spi.JdbcEnvironment)7 PostgreSQL81Dialect (org.hibernate.dialect.PostgreSQL81Dialect)4 RowSelection (org.hibernate.engine.spi.RowSelection)4 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)4 Connection (java.sql.Connection)3 SQLException (java.sql.SQLException)3 Iterator (java.util.Iterator)3 Map (java.util.Map)3 H2Dialect (org.hibernate.dialect.H2Dialect)3 DialectResolutionInfo (org.hibernate.engine.jdbc.dialect.spi.DialectResolutionInfo)3 Column (org.hibernate.mapping.Column)3 SpatialDialect (org.hibernate.spatial.SpatialDialect)3 AuditEventRecord (com.evolveum.midpoint.audit.api.AuditEventRecord)2 AuditEventStage (com.evolveum.midpoint.audit.api.AuditEventStage)2 AuditEventType (com.evolveum.midpoint.audit.api.AuditEventType)2 AuditResultHandler (com.evolveum.midpoint.audit.api.AuditResultHandler)2 AuditService (com.evolveum.midpoint.audit.api.AuditService)2 PrismObject (com.evolveum.midpoint.prism.PrismObject)2