Search in sources :

Example 66 with Dialect

use of org.hibernate.dialect.Dialect in project hibernate-orm by hibernate.

the class IdentNode method resolveAsAlias.

private boolean resolveAsAlias() {
    final String alias = getText();
    // This is not actually a constant, but a reference to FROM element.
    final FromElement element = getWalker().getCurrentFromClause().getFromElement(alias);
    if (element == null) {
        return false;
    }
    element.applyTreatAsDeclarations(getWalker().getTreatAsDeclarationsByPath(alias));
    setType(SqlTokenTypes.ALIAS_REF);
    setFromElement(element);
    String[] columnExpressions = element.getIdentityColumns();
    // determine whether to apply qualification (table alias) to the column(s)...
    if (!isFromElementUpdateOrDeleteRoot(element)) {
        if (StringHelper.isNotEmpty(element.getTableAlias())) {
            // apparently we also need to check that they are not already qualified.  Ugh!
            columnExpressions = StringHelper.qualifyIfNot(element.getTableAlias(), columnExpressions);
        }
    }
    final Dialect dialect = getWalker().getSessionFactoryHelper().getFactory().getDialect();
    final boolean isInCount = getWalker().isInCount();
    final boolean isInDistinctCount = isInCount && getWalker().isInCountDistinct();
    final boolean isInNonDistinctCount = isInCount && !getWalker().isInCountDistinct();
    final boolean isCompositeValue = columnExpressions.length > 1;
    if (isCompositeValue) {
        if (isInNonDistinctCount && !dialect.supportsTupleCounts()) {
            // TODO: #supportsTupleCounts currently false for all Dialects -- could this be cleaned up?
            setText(columnExpressions[0]);
        } else {
            String joinedFragment = String.join(", ", columnExpressions);
            // avoid wrapping in parenthesis (explicit tuple treatment) if possible due to varied support for
            // tuple syntax across databases..
            final boolean shouldSkipWrappingInParenthesis = (isInDistinctCount && !dialect.requiresParensForTupleDistinctCounts()) || isInNonDistinctCount || getWalker().getCurrentTopLevelClauseType() == HqlSqlTokenTypes.ORDER || getWalker().getCurrentTopLevelClauseType() == HqlSqlTokenTypes.GROUP;
            if (!shouldSkipWrappingInParenthesis) {
                joinedFragment = "(" + joinedFragment + ")";
            }
            setText(joinedFragment);
        }
        return true;
    } else if (columnExpressions.length > 0) {
        setText(columnExpressions[0]);
        return true;
    }
    return false;
}
Also used : Dialect(org.hibernate.dialect.Dialect)

Example 67 with Dialect

use of org.hibernate.dialect.Dialect in project hibernate-orm by hibernate.

the class SchemaDropperImpl method generateDropCommands.

/**
 * For testing...
 *
 * @param metadata The metadata for which to generate the creation commands.
 *
 * @return The generation commands
 */
public List<String> generateDropCommands(Metadata metadata, final boolean manageNamespaces) {
    final JournalingGenerationTarget target = new JournalingGenerationTarget();
    final ServiceRegistry serviceRegistry = ((MetadataImplementor) metadata).getMetadataBuildingOptions().getServiceRegistry();
    final Dialect dialect = serviceRegistry.getService(JdbcEnvironment.class).getDialect();
    final ExecutionOptions options = new ExecutionOptions() {

        @Override
        public boolean shouldManageNamespaces() {
            return manageNamespaces;
        }

        @Override
        public Map getConfigurationValues() {
            return Collections.emptyMap();
        }

        @Override
        public ExceptionHandler getExceptionHandler() {
            return ExceptionHandlerHaltImpl.INSTANCE;
        }
    };
    dropFromMetadata(metadata, options, dialect, FormatStyle.NONE.getFormatter(), target);
    return target.commands;
}
Also used : ExecutionOptions(org.hibernate.tool.schema.spi.ExecutionOptions) Dialect(org.hibernate.dialect.Dialect) ServiceRegistry(org.hibernate.service.ServiceRegistry) JdbcEnvironment(org.hibernate.engine.jdbc.env.spi.JdbcEnvironment)

Example 68 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 69 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 70 with Dialect

use of org.hibernate.dialect.Dialect in project jbosstools-hibernate by jbosstools.

the class DummyMetadataImplementorTest method testInstance.

@Test
public void testInstance() {
    assertNotNull(DummyMetadataImplementor.INSTANCE);
    StandardServiceRegistry serviceRegistry = DummyMetadataImplementor.INSTANCE.getMetadataBuildingOptions().getServiceRegistry();
    JdbcServices jdbcServices = serviceRegistry.getService(JdbcServices.class);
    Dialect dialect = jdbcServices.getDialect();
    assertTrue(dialect instanceof DummyMetadataImplementor.DummyDialect);
}
Also used : Dialect(org.hibernate.dialect.Dialect) JdbcServices(org.hibernate.engine.jdbc.spi.JdbcServices) StandardServiceRegistry(org.hibernate.boot.registry.StandardServiceRegistry) Test(org.junit.jupiter.api.Test)

Aggregations

Dialect (org.hibernate.dialect.Dialect)80 DialectFactory (org.hibernate.engine.jdbc.dialect.spi.DialectFactory)19 StandardServiceRegistry (org.hibernate.boot.registry.StandardServiceRegistry)18 Column (org.hibernate.mapping.Column)14 SQLException (java.sql.SQLException)13 DialectResolutionInfo (org.hibernate.engine.jdbc.dialect.spi.DialectResolutionInfo)12 JdbcServices (org.hibernate.engine.jdbc.spi.JdbcServices)12 Configuration (org.hibernate.cfg.Configuration)11 ServiceRegistry (org.hibernate.service.ServiceRegistry)11 Properties (java.util.Properties)10 HibernateException (org.hibernate.HibernateException)10 IConfiguration (org.jboss.tools.hibernate.runtime.spi.IConfiguration)10 StandardServiceRegistryBuilder (org.hibernate.boot.registry.StandardServiceRegistryBuilder)9 DatabaseMetaDataDialectResolutionInfoAdapter (org.hibernate.engine.jdbc.dialect.spi.DatabaseMetaDataDialectResolutionInfoAdapter)9 DialectResolutionInfoSource (org.hibernate.engine.jdbc.dialect.spi.DialectResolutionInfoSource)9 HibernateException (org.jboss.tools.hibernate.runtime.spi.HibernateException)9 MetaDataDialect (org.hibernate.cfg.reveng.dialect.MetaDataDialect)8 Test (org.junit.jupiter.api.Test)8 JdbcEnvironment (org.hibernate.engine.jdbc.env.spi.JdbcEnvironment)7 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)6