Search in sources :

Example 1 with IQueryTranslator

use of org.jboss.tools.hibernate.runtime.spi.IQueryTranslator in project jbosstools-hibernate by jbosstools.

the class QueryHelper method generateSQL.

public static String generateSQL(ExecutionContext executionContext, final ISessionFactory sessionFactory, final String query, final IService service) {
    // $NON-NLS-1$
    if (StringHelper.isEmpty(query))
        return "";
    String result = (String) executionContext.execute(new ExecutionContext.Command() {

        public Object execute() {
            try {
                StringBuffer str = new StringBuffer(256);
                IHQLQueryPlan plan = service.newHQLQueryPlan(query, false, sessionFactory);
                IQueryTranslator[] translators = plan.getTranslators();
                for (int i = 0; i < translators.length; i++) {
                    IQueryTranslator translator = translators[i];
                    if (translator.isManipulationStatement()) {
                        // $NON-NLS-1$
                        str.append(ConsoleMessages.DynamicSQLPreviewView_manipulation_of + i + ":");
                        Iterator<?> iterator = translator.getQuerySpaces().iterator();
                        while (iterator.hasNext()) {
                            Object qspace = iterator.next();
                            str.append(qspace);
                            // $NON-NLS-1$
                            if (iterator.hasNext()) {
                                str.append(", ");
                            }
                        }
                    } else {
                        IType[] returnTypes = translator.getReturnTypes();
                        // $NON-NLS-1$
                        str.append(i + ": ");
                        for (int j = 0; j < returnTypes.length; j++) {
                            IType returnType = returnTypes[j];
                            str.append(returnType.getName());
                            // $NON-NLS-1$
                            if (j < returnTypes.length - 1) {
                                str.append(", ");
                            }
                        }
                    }
                    // $NON-NLS-1$
                    str.append("\n-----------------\n");
                    Iterator<?> sqls = translator.collectSqlStrings().iterator();
                    while (sqls.hasNext()) {
                        String sql = (String) sqls.next();
                        str.append(QLFormatHelper.formatForScreen(sql));
                        // $NON-NLS-1$
                        str.append("\n\n");
                    }
                }
                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(ConsoleMessages.DynamicSQLPreviewView_caused_by);
                    }
                }
                return msgs.toString();
            }
        }
    });
    return result;
}
Also used : IHQLQueryPlan(org.jboss.tools.hibernate.runtime.spi.IHQLQueryPlan) IQueryTranslator(org.jboss.tools.hibernate.runtime.spi.IQueryTranslator) IType(org.jboss.tools.hibernate.runtime.spi.IType)

Example 2 with IQueryTranslator

use of org.jboss.tools.hibernate.runtime.spi.IQueryTranslator in project jbosstools-hibernate by jbosstools.

the class FacadeFactoryTest method testCreateQueryTranslator.

@Test
public void testCreateQueryTranslator() {
    QueryTranslator queryTranslator = (QueryTranslator) Proxy.newProxyInstance(facadeFactory.getClassLoader(), new Class[] { QueryTranslator.class }, new TestInvocationHandler());
    IQueryTranslator facade = facadeFactory.createQueryTranslator(queryTranslator);
    Assert.assertSame(queryTranslator, ((IFacade) facade).getTarget());
}
Also used : PersistentClass(org.hibernate.mapping.PersistentClass) RootClass(org.hibernate.mapping.RootClass) IPersistentClass(org.jboss.tools.hibernate.runtime.spi.IPersistentClass) POJOClass(org.hibernate.tool.hbm2x.pojo.POJOClass) IPOJOClass(org.jboss.tools.hibernate.runtime.spi.IPOJOClass) IQueryTranslator(org.jboss.tools.hibernate.runtime.spi.IQueryTranslator) QueryTranslator(org.hibernate.hql.QueryTranslator) IQueryTranslator(org.jboss.tools.hibernate.runtime.spi.IQueryTranslator) Test(org.junit.Test)

Example 3 with IQueryTranslator

use of org.jboss.tools.hibernate.runtime.spi.IQueryTranslator in project jbosstools-hibernate by jbosstools.

the class FacadeFactoryTest method testCreateQueryTranslator.

@Test
public void testCreateQueryTranslator() {
    QueryTranslator queryTranslator = (QueryTranslator) Proxy.newProxyInstance(facadeFactory.getClassLoader(), new Class[] { QueryTranslator.class }, new TestInvocationHandler());
    IQueryTranslator facade = facadeFactory.createQueryTranslator(queryTranslator);
    Assert.assertSame(queryTranslator, ((IFacade) facade).getTarget());
}
Also used : PersistentClass(org.hibernate.mapping.PersistentClass) RootClass(org.hibernate.mapping.RootClass) IPersistentClass(org.jboss.tools.hibernate.runtime.spi.IPersistentClass) POJOClass(org.hibernate.tool.hbm2x.pojo.POJOClass) IPOJOClass(org.jboss.tools.hibernate.runtime.spi.IPOJOClass) IQueryTranslator(org.jboss.tools.hibernate.runtime.spi.IQueryTranslator) QueryTranslator(org.hibernate.hql.QueryTranslator) IQueryTranslator(org.jboss.tools.hibernate.runtime.spi.IQueryTranslator) Test(org.junit.Test)

Example 4 with IQueryTranslator

use of org.jboss.tools.hibernate.runtime.spi.IQueryTranslator in project jbosstools-hibernate by jbosstools.

the class FacadeFactoryTest method testCreateQueryTranslator.

@Test
public void testCreateQueryTranslator() {
    QueryTranslator queryTranslator = (QueryTranslator) Proxy.newProxyInstance(facadeFactory.getClassLoader(), new Class[] { QueryTranslator.class }, new TestInvocationHandler());
    IQueryTranslator facade = facadeFactory.createQueryTranslator(queryTranslator);
    Assert.assertSame(queryTranslator, ((IFacade) facade).getTarget());
}
Also used : PersistentClass(org.hibernate.mapping.PersistentClass) IPOJOClass(org.jboss.tools.hibernate.runtime.spi.IPOJOClass) RootClass(org.hibernate.mapping.RootClass) IPersistentClass(org.jboss.tools.hibernate.runtime.spi.IPersistentClass) POJOClass(org.hibernate.tool.hbm2x.pojo.POJOClass) IQueryTranslator(org.jboss.tools.hibernate.runtime.spi.IQueryTranslator) QueryTranslator(org.hibernate.hql.spi.QueryTranslator) IQueryTranslator(org.jboss.tools.hibernate.runtime.spi.IQueryTranslator) Test(org.junit.Test)

Example 5 with IQueryTranslator

use of org.jboss.tools.hibernate.runtime.spi.IQueryTranslator in project jbosstools-hibernate by jbosstools.

the class FacadeFactoryTest method testCreateQueryTranslator.

@Test
public void testCreateQueryTranslator() {
    QueryTranslator queryTranslator = (QueryTranslator) Proxy.newProxyInstance(facadeFactory.getClassLoader(), new Class[] { QueryTranslator.class }, new TestInvocationHandler());
    IQueryTranslator facade = facadeFactory.createQueryTranslator(queryTranslator);
    Assert.assertSame(queryTranslator, ((IFacade) facade).getTarget());
}
Also used : PersistentClass(org.hibernate.mapping.PersistentClass) RootClass(org.hibernate.mapping.RootClass) IPersistentClass(org.jboss.tools.hibernate.runtime.spi.IPersistentClass) POJOClass(org.hibernate.tool.hbm2x.pojo.POJOClass) IPOJOClass(org.jboss.tools.hibernate.runtime.spi.IPOJOClass) IQueryTranslator(org.jboss.tools.hibernate.runtime.spi.IQueryTranslator) QueryTranslator(org.hibernate.hql.spi.QueryTranslator) IQueryTranslator(org.jboss.tools.hibernate.runtime.spi.IQueryTranslator) Test(org.junit.Test)

Aggregations

IQueryTranslator (org.jboss.tools.hibernate.runtime.spi.IQueryTranslator)9 PersistentClass (org.hibernate.mapping.PersistentClass)8 RootClass (org.hibernate.mapping.RootClass)8 POJOClass (org.hibernate.tool.hbm2x.pojo.POJOClass)8 IPOJOClass (org.jboss.tools.hibernate.runtime.spi.IPOJOClass)8 IPersistentClass (org.jboss.tools.hibernate.runtime.spi.IPersistentClass)8 Test (org.junit.Test)8 QueryTranslator (org.hibernate.hql.spi.QueryTranslator)6 QueryTranslator (org.hibernate.hql.QueryTranslator)2 IHQLQueryPlan (org.jboss.tools.hibernate.runtime.spi.IHQLQueryPlan)1 IType (org.jboss.tools.hibernate.runtime.spi.IType)1