Search in sources :

Example 6 with QueryTranslator

use of org.hibernate.hql.spi.QueryTranslator in project hibernate-orm by hibernate.

the class CompositeIdTest method testDistinctCountOfEntityWithCompositeId.

@Test
@SkipForDialect(value = Oracle8iDialect.class, comment = "Cannot count distinct over multiple columns in Oracle")
@SkipForDialect(value = SQLServerDialect.class, comment = "Cannot count distinct over multiple columns in SQL Server")
public void testDistinctCountOfEntityWithCompositeId() {
    // today we do not account for Dialects supportsTupleDistinctCounts() is false.  though really the only
    // "option" there is to throw an error.
    final HQLQueryPlan plan = sessionFactory().getQueryPlanCache().getHQLQueryPlan("select count(distinct o) from Order o", false, Collections.EMPTY_MAP);
    assertEquals(1, plan.getTranslators().length);
    final QueryTranslator translator = plan.getTranslators()[0];
    final String generatedSql = translator.getSQLString();
    System.out.println("Generated SQL : " + generatedSql);
    final int countExpressionListStart = generatedSql.indexOf("count(");
    final int countExpressionListEnd = generatedSql.indexOf(")", countExpressionListStart);
    final String countExpressionFragment = generatedSql.substring(countExpressionListStart + 6, countExpressionListEnd + 1);
    assertTrue(countExpressionFragment.startsWith("distinct"));
    assertTrue(countExpressionFragment.contains(","));
    Session s = openSession();
    s.beginTransaction();
    Customer c = new Customer();
    c.setCustomerId("1");
    c.setAddress("123 somewhere");
    c.setName("Brett");
    Order o1 = new Order(c);
    o1.setOrderDate(Calendar.getInstance());
    Order o2 = new Order(c);
    o2.setOrderDate(Calendar.getInstance());
    s.persist(c);
    s.persist(o1);
    s.persist(o2);
    s.getTransaction().commit();
    s.clear();
    s.beginTransaction();
    try {
        long count = (Long) s.createQuery("select count(distinct o) FROM Order o").uniqueResult();
        if (!getDialect().supportsTupleDistinctCounts()) {
            fail("expected SQLGrammarException");
        }
        assertEquals(2l, count);
    } catch (SQLGrammarException e) {
        if (getDialect().supportsTupleDistinctCounts()) {
            throw e;
        }
    }
    s.getTransaction().commit();
    s.close();
    s = openSession();
    s.beginTransaction();
    s.createQuery("delete from Order").executeUpdate();
    s.createQuery("delete from Customer").executeUpdate();
    s.getTransaction().commit();
    s.close();
}
Also used : HQLQueryPlan(org.hibernate.engine.query.spi.HQLQueryPlan) SQLGrammarException(org.hibernate.exception.SQLGrammarException) QueryTranslator(org.hibernate.hql.spi.QueryTranslator) Session(org.hibernate.Session) SkipForDialect(org.hibernate.testing.SkipForDialect) Test(org.junit.Test)

Example 7 with QueryTranslator

use of org.hibernate.hql.spi.QueryTranslator in project hibernate-orm by hibernate.

the class HQLQueryPlan method performExecuteUpdate.

/**
 * Coordinates the efforts to perform an execution across all the included query translators.
 *
 * @param queryParameters The query parameters
 * @param session The session
 *
 * @return The aggregated "affected row" count
 *
 * @throws HibernateException Indicates a problem performing the execution
 */
public int performExecuteUpdate(QueryParameters queryParameters, SharedSessionContractImplementor session) throws HibernateException {
    if (traceEnabled) {
        LOG.tracev("Execute update: {0}", getSourceQuery());
        queryParameters.traceParameters(session.getFactory());
    }
    if (translators.length != 1) {
        LOG.splitQueries(getSourceQuery(), translators.length);
    }
    int result = 0;
    for (QueryTranslator translator : translators) {
        result += translator.executeUpdate(queryParameters, session);
    }
    return result;
}
Also used : QueryTranslator(org.hibernate.hql.spi.QueryTranslator)

Example 8 with QueryTranslator

use of org.hibernate.hql.spi.QueryTranslator in project hibernate-orm by hibernate.

the class QueryTranslatorTestCase method assertTranslation.

protected void assertTranslation(String hql, Map replacements, boolean scalar, String sql) {
    SessionFactoryImplementor factory = sessionFactory();
    // Create an empty replacements map if we don't have one.
    if (replacements == null) {
        replacements = new HashMap();
    }
    // steve -> note that the empty maps here represent the currently enabled filters...
    QueryTranslator oldQueryTranslator = null;
    Exception oldException = null;
    try {
        System.out.println("Compiling with classic QueryTranslator...");
        QueryTranslatorFactory classic = new ClassicQueryTranslatorFactory();
        oldQueryTranslator = classic.createQueryTranslator(hql, hql, Collections.EMPTY_MAP, factory, null);
        oldQueryTranslator.compile(replacements, scalar);
    } catch (QueryException e) {
        oldException = e;
    } catch (MappingException e) {
        oldException = e;
    }
    QueryTranslator newQueryTranslator = null;
    Exception newException = null;
    try {
        System.out.println("Compiling with AST QueryTranslator...");
        newQueryTranslator = createNewQueryTranslator(hql, replacements, scalar);
    } catch (QueryException e) {
        newException = e;
    } catch (MappingException e) {
        newException = e;
    }
    // If the old QT threw an exception, the new one should too.
    if (oldException != null) {
        assertNotNull("New query translator did *NOT* throw an exception, the old one did : " + oldException, newException);
        assertEquals(oldException.getMessage(), newException.getMessage());
        // Don't bother with the rest of the assertions.
        return;
    } else if (newException != null) {
        newException.printStackTrace();
        assertNull("Old query translator did not throw an exception, the new one did", newException);
    }
    // -- check all of the outputs --
    checkSql(oldQueryTranslator, newQueryTranslator, hql, scalar, sql);
    checkQuerySpaces(oldQueryTranslator, newQueryTranslator);
    checkReturnedTypes(oldQueryTranslator, newQueryTranslator);
}
Also used : ClassicQueryTranslatorFactory(org.hibernate.hql.internal.classic.ClassicQueryTranslatorFactory) ASTQueryTranslatorFactory(org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory) QueryTranslatorFactory(org.hibernate.hql.spi.QueryTranslatorFactory) QueryException(org.hibernate.QueryException) HashMap(java.util.HashMap) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) ClassicQueryTranslatorFactory(org.hibernate.hql.internal.classic.ClassicQueryTranslatorFactory) QueryTranslator(org.hibernate.hql.spi.QueryTranslator) MappingException(org.hibernate.MappingException) QueryException(org.hibernate.QueryException) MappingException(org.hibernate.MappingException)

Example 9 with QueryTranslator

use of org.hibernate.hql.spi.QueryTranslator in project hibernate-orm by hibernate.

the class QueryTranslatorTestCase method compileBadHql.

protected Exception compileBadHql(String hql, boolean scalar) {
    QueryTranslator newQueryTranslator;
    Map replacements = null;
    Exception newException = null;
    SessionFactoryImplementor factory = sessionFactory();
    try {
        QueryTranslatorFactory ast = new ASTQueryTranslatorFactory();
        newQueryTranslator = ast.createQueryTranslator(hql, hql, Collections.EMPTY_MAP, factory, null);
        newQueryTranslator.compile(replacements, scalar);
    } catch (QueryException e) {
        newException = e;
    } catch (MappingException e) {
        newException = e;
    }
    assertNotNull("Expected exception from compilation of '" + hql + "'!", newException);
    return newException;
}
Also used : ClassicQueryTranslatorFactory(org.hibernate.hql.internal.classic.ClassicQueryTranslatorFactory) ASTQueryTranslatorFactory(org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory) QueryTranslatorFactory(org.hibernate.hql.spi.QueryTranslatorFactory) QueryException(org.hibernate.QueryException) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) QueryTranslator(org.hibernate.hql.spi.QueryTranslator) HashMap(java.util.HashMap) Map(java.util.Map) TreeMap(java.util.TreeMap) MappingException(org.hibernate.MappingException) QueryException(org.hibernate.QueryException) ASTQueryTranslatorFactory(org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory) MappingException(org.hibernate.MappingException)

Example 10 with QueryTranslator

use of org.hibernate.hql.spi.QueryTranslator in project hibernate-orm by hibernate.

the class CompositeIdTest method testNonDistinctCountOfEntityWithCompositeId.

@Test
public void testNonDistinctCountOfEntityWithCompositeId() {
    // the check here is all based on whether we had commas in the expressions inside the count
    final HQLQueryPlan plan = sessionFactory().getQueryPlanCache().getHQLQueryPlan("select count(o) from Order o", false, Collections.EMPTY_MAP);
    assertEquals(1, plan.getTranslators().length);
    final QueryTranslator translator = plan.getTranslators()[0];
    final String generatedSql = translator.getSQLString();
    final int countExpressionListStart = generatedSql.indexOf("count(");
    final int countExpressionListEnd = generatedSql.indexOf(")", countExpressionListStart);
    final String countExpressionFragment = generatedSql.substring(countExpressionListStart + 6, countExpressionListEnd + 1);
    final boolean hadCommas = countExpressionFragment.contains(",");
    // set up the expectation based on Dialect...
    final boolean expectCommas = sessionFactory().getDialect().supportsTupleCounts();
    assertEquals(expectCommas, hadCommas);
}
Also used : HQLQueryPlan(org.hibernate.engine.query.spi.HQLQueryPlan) QueryTranslator(org.hibernate.hql.spi.QueryTranslator) Test(org.junit.Test)

Aggregations

QueryTranslator (org.hibernate.hql.spi.QueryTranslator)21 Test (org.junit.Test)11 QueryTranslatorFactory (org.hibernate.hql.spi.QueryTranslatorFactory)9 ASTQueryTranslatorFactory (org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory)8 PersistentClass (org.hibernate.mapping.PersistentClass)6 RootClass (org.hibernate.mapping.RootClass)6 POJOClass (org.hibernate.tool.hbm2x.pojo.POJOClass)6 IPOJOClass (org.jboss.tools.hibernate.runtime.spi.IPOJOClass)6 IPersistentClass (org.jboss.tools.hibernate.runtime.spi.IPersistentClass)6 IQueryTranslator (org.jboss.tools.hibernate.runtime.spi.IQueryTranslator)6 HQLQueryPlan (org.hibernate.engine.query.spi.HQLQueryPlan)4 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)4 HashMap (java.util.HashMap)3 ClassicQueryTranslatorFactory (org.hibernate.hql.internal.classic.ClassicQueryTranslatorFactory)3 Map (java.util.Map)2 MappingException (org.hibernate.MappingException)2 QueryException (org.hibernate.QueryException)2 QueryTranslatorImpl (org.hibernate.hql.internal.ast.QueryTranslatorImpl)2 TestForIssue (org.hibernate.testing.TestForIssue)2 ArrayList (java.util.ArrayList)1