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

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

Introduction

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

Prototype

Type[] getReturnTypes();

Source Link

Document

Returns an array of Types represented in the query result.

Usage

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  ww .  j  av 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;
}