Example usage for org.hibernate.hql.spi QueryTranslator isManipulationStatement

List of usage examples for org.hibernate.hql.spi QueryTranslator isManipulationStatement

Introduction

In this page you can find the example usage for org.hibernate.hql.spi QueryTranslator isManipulationStatement.

Prototype

boolean isManipulationStatement();

Source Link

Usage

From source file:com.blazebit.persistence.integration.hibernate.base.HibernateExtendedQuerySupport.java

License:Apache License

@Override
public String getSql(EntityManager em, Query query) {
    SessionImplementor session = em.unwrap(SessionImplementor.class);
    HQLQueryPlan queryPlan = getOriginalQueryPlan(session, query);

    if (queryPlan.getTranslators().length > 1) {
        throw new IllegalArgumentException("No support for multiple translators yet!");
    }/*from   w  w  w .  j  ava 2s . c  o m*/
    QueryTranslator queryTranslator = queryPlan.getTranslators()[0];

    String[] sqls;
    if (queryTranslator.isManipulationStatement()) {
        StatementExecutor executor = getStatementExecutor(queryTranslator);
        if (!(executor instanceof BasicExecutor)) {
            throw new IllegalArgumentException(
                    "Using polymorphic deletes/updates with CTEs is not yet supported");
        }
        sqls = executor.getSqlStatements();
    } else {
        sqls = queryPlan.getSqlStrings();
    }
    // TODO: have to handle multiple sql strings which happens when having e.g. a polymorphic UPDATE/DELETE
    for (int i = 0; i < sqls.length; i++) {
        if (sqls[i] != null) {
            return sqls[i];
        }
    }

    return null;
}

From source file:org.jboss.tools.hibernate4_0.QueryHelper.java

License:Open Source License

static String generateSQL(ExecutionContext executionContext, final SessionFactory sessionFactory,
        final String query) {

    if (StringHelper.isEmpty(query))
        return ""; //$NON-NLS-1$

    String result = (String) executionContext.execute(new ExecutionContext.Command() {
        public Object execute() {
            try {
                SessionFactoryImpl sfimpl = (SessionFactoryImpl) sessionFactory; // hack - to get to the actual queries..
                StringBuffer str = new StringBuffer(256);
                HQLQueryPlan plan = new HQLQueryPlan(query, false, Collections.EMPTY_MAP, sfimpl);

                QueryTranslator[] translators = plan.getTranslators();
                for (int i = 0; i < translators.length; i++) {
                    QueryTranslator translator = translators[i];
                    if (translator.isManipulationStatement()) {
                        str.append(HibernateConsoleMessages.DynamicSQLPreviewView_manipulation_of + i + ":"); //$NON-NLS-1$
                        Iterator<?> iterator = translator.getQuerySpaces().iterator();
                        while (iterator.hasNext()) {
                            Object qspace = iterator.next();
                            str.append(qspace);
                            if (iterator.hasNext()) {
                                str.append(", "); //$NON-NLS-1$
                            }//from   w w  w  .ja  v  a  2  s . co  m
                        }

                    } else {
                        Type[] returnTypes = translator.getReturnTypes();
                        str.append(i + ": "); //$NON-NLS-1$
                        for (int j = 0; j < returnTypes.length; j++) {
                            Type returnType = returnTypes[j];
                            str.append(returnType.getName());
                            if (j < returnTypes.length - 1) {
                                str.append(", "); //$NON-NLS-1$
                            }
                        }
                    }
                    str.append("\n-----------------\n"); //$NON-NLS-1$
                    Iterator<?> sqls = translator.collectSqlStrings().iterator();
                    while (sqls.hasNext()) {
                        String sql = (String) sqls.next();
                        str.append(QLFormatHelper.formatForScreen(sql));
                        str.append("\n\n"); //$NON-NLS-1$
                    }
                }
                return str.toString();
            } catch (Throwable t) {
                StringBuffer msgs = new StringBuffer();

                Throwable cause = t;
                while (cause != null) {
                    msgs.append(t);
                    if (cause.getCause() == cause) {
                        cause = null;
                    } else {
                        cause = cause.getCause();
                        if (cause != null)
                            msgs.append(HibernateConsoleMessages.DynamicSQLPreviewView_caused_by);
                    }
                }
                return msgs.toString();
            }

        }
    });

    return result;
}