Search in sources :

Example 6 with QueryTranslatorFactory

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

the class CriteriaHQLAlignmentTest method testHQLAggregationReturnType.

@Test
public void testHQLAggregationReturnType() {
    // EJB3: COUNT returns Long
    QueryTranslatorImpl translator = createNewQueryTranslator("select count(*) from Human h");
    assertEquals("incorrect return type count", 1, translator.getReturnTypes().length);
    assertEquals("incorrect return type", LongType.INSTANCE, translator.getReturnTypes()[0]);
    translator = createNewQueryTranslator("select count(h.heightInches) from Human h");
    assertEquals("incorrect return type count", 1, translator.getReturnTypes().length);
    assertEquals("incorrect return type", LongType.INSTANCE, translator.getReturnTypes()[0]);
    // MAX, MIN return the type of the state-field to which they are applied. 
    translator = createNewQueryTranslator("select max(h.heightInches) from Human h");
    assertEquals("incorrect return type count", 1, translator.getReturnTypes().length);
    assertEquals("incorrect return type", DoubleType.INSTANCE, translator.getReturnTypes()[0]);
    translator = createNewQueryTranslator("select max(h.id) from Human h");
    assertEquals("incorrect return type count", 1, translator.getReturnTypes().length);
    assertEquals("incorrect return type", LongType.INSTANCE, translator.getReturnTypes()[0]);
    // AVG returns Double.
    translator = createNewQueryTranslator("select avg(h.heightInches) from Human h");
    assertEquals("incorrect return type count", 1, translator.getReturnTypes().length);
    assertEquals("incorrect return type", DoubleType.INSTANCE, translator.getReturnTypes()[0]);
    translator = createNewQueryTranslator("select avg(h.id) from Human h");
    assertEquals("incorrect return type count", 1, translator.getReturnTypes().length);
    assertEquals("incorrect return type", DoubleType.INSTANCE, translator.getReturnTypes()[0]);
    translator = createNewQueryTranslator("select avg(h.bigIntegerValue) from Human h");
    assertEquals("incorrect return type count", 1, translator.getReturnTypes().length);
    assertEquals("incorrect return type", DoubleType.INSTANCE, translator.getReturnTypes()[0]);
    // SUM returns Long when applied to state-fields of integral types (other than BigInteger);
    translator = createNewQueryTranslator("select sum(h.id) from Human h");
    assertEquals("incorrect return type count", 1, translator.getReturnTypes().length);
    assertEquals("incorrect return type", LongType.INSTANCE, translator.getReturnTypes()[0]);
    translator = createNewQueryTranslator("select sum(h.intValue) from Human h");
    assertEquals("incorrect return type count", 1, translator.getReturnTypes().length);
    assertEquals("incorrect return type", LongType.INSTANCE, translator.getReturnTypes()[0]);
    // SUM returns Double when applied to state-fields of floating point types; 
    translator = createNewQueryTranslator("select sum(h.heightInches) from Human h");
    assertEquals("incorrect return type count", 1, translator.getReturnTypes().length);
    assertEquals("incorrect return type", DoubleType.INSTANCE, translator.getReturnTypes()[0]);
    translator = createNewQueryTranslator("select sum(h.floatValue) from Human h");
    assertEquals("incorrect return type count", 1, translator.getReturnTypes().length);
    assertEquals("incorrect return type", DoubleType.INSTANCE, translator.getReturnTypes()[0]);
    // SUM returns BigInteger when applied to state-fields of type BigInteger 
    translator = createNewQueryTranslator("select sum(h.bigIntegerValue) from Human h");
    assertEquals("incorrect return type count", 1, translator.getReturnTypes().length);
    assertEquals("incorrect return type", BigIntegerType.INSTANCE, translator.getReturnTypes()[0]);
    // SUM and BigDecimal when applied to state-fields of type BigDecimal.
    translator = createNewQueryTranslator("select sum(h.bigDecimalValue) from Human h");
    assertEquals("incorrect return type count", 1, translator.getReturnTypes().length);
    assertEquals("incorrect return type", BigDecimalType.INSTANCE, translator.getReturnTypes()[0]);
    // special case to test classicquery special case handling of count(*)
    String hql = "select count(*) from Human h";
    QueryTranslatorFactory classic = new ClassicQueryTranslatorFactory();
    QueryTranslator oldQueryTranslator = classic.createQueryTranslator(hql, hql, Collections.EMPTY_MAP, sessionFactory(), null);
    oldQueryTranslator.compile(Collections.EMPTY_MAP, true);
    assertEquals("incorrect return type count", 1, oldQueryTranslator.getReturnTypes().length);
    assertEquals("incorrect return type", LongType.INSTANCE, oldQueryTranslator.getReturnTypes()[0]);
}
Also used : ClassicQueryTranslatorFactory(org.hibernate.hql.internal.classic.ClassicQueryTranslatorFactory) QueryTranslatorFactory(org.hibernate.hql.spi.QueryTranslatorFactory) ClassicQueryTranslatorFactory(org.hibernate.hql.internal.classic.ClassicQueryTranslatorFactory) QueryTranslator(org.hibernate.hql.spi.QueryTranslator) QueryTranslatorImpl(org.hibernate.hql.internal.ast.QueryTranslatorImpl) Test(org.junit.Test)

Example 7 with QueryTranslatorFactory

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

the class EJBQLTest method assertEjbqlEqualsHql.

private void assertEjbqlEqualsHql(String ejbql, String hql) {
    QueryTranslatorFactory ast = new ASTQueryTranslatorFactory();
    QueryTranslator queryTranslator = ast.createQueryTranslator(hql, hql, Collections.EMPTY_MAP, sessionFactory(), null);
    queryTranslator.compile(Collections.EMPTY_MAP, true);
    String hqlSql = queryTranslator.getSQLString();
    queryTranslator = ast.createQueryTranslator(ejbql, ejbql, Collections.EMPTY_MAP, sessionFactory(), null);
    queryTranslator.compile(Collections.EMPTY_MAP, true);
    String ejbqlSql = queryTranslator.getSQLString();
    assertEquals(hqlSql, ejbqlSql);
}
Also used : QueryTranslatorFactory(org.hibernate.hql.spi.QueryTranslatorFactory) ASTQueryTranslatorFactory(org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory) QueryTranslator(org.hibernate.hql.spi.QueryTranslator) ASTQueryTranslatorFactory(org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory)

Example 8 with QueryTranslatorFactory

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

the class EJBQLTest method compile.

private QueryTranslatorImpl compile(String input) {
    QueryTranslatorFactory ast = new ASTQueryTranslatorFactory();
    QueryTranslator queryTranslator = ast.createQueryTranslator(input, input, Collections.EMPTY_MAP, sessionFactory(), null);
    queryTranslator.compile(Collections.EMPTY_MAP, true);
    return (QueryTranslatorImpl) queryTranslator;
}
Also used : QueryTranslatorFactory(org.hibernate.hql.spi.QueryTranslatorFactory) ASTQueryTranslatorFactory(org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory) QueryTranslator(org.hibernate.hql.spi.QueryTranslator) ASTQueryTranslatorFactory(org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory) QueryTranslatorImpl(org.hibernate.hql.internal.ast.QueryTranslatorImpl)

Example 9 with QueryTranslatorFactory

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

the class HQLTest method compileWithAstQueryTranslator.

private void compileWithAstQueryTranslator(String hql, boolean scalar) {
    Map replacements = new HashMap();
    QueryTranslatorFactory ast = new ASTQueryTranslatorFactory();
    SessionFactoryImplementor factory = getSessionFactoryImplementor();
    QueryTranslator newQueryTranslator = ast.createQueryTranslator(hql, hql, Collections.EMPTY_MAP, factory, null);
    newQueryTranslator.compile(replacements, scalar);
}
Also used : ASTQueryTranslatorFactory(org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory) QueryTranslatorFactory(org.hibernate.hql.spi.QueryTranslatorFactory) HashMap(java.util.HashMap) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) QueryTranslator(org.hibernate.hql.spi.QueryTranslator) Map(java.util.Map) HashMap(java.util.HashMap) ASTQueryTranslatorFactory(org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory)

Example 10 with QueryTranslatorFactory

use of org.hibernate.hql.spi.QueryTranslatorFactory 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)

Aggregations

QueryTranslatorFactory (org.hibernate.hql.spi.QueryTranslatorFactory)12 ASTQueryTranslatorFactory (org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory)11 QueryTranslator (org.hibernate.hql.spi.QueryTranslator)9 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)4 QueryTranslatorImpl (org.hibernate.hql.internal.ast.QueryTranslatorImpl)4 ClassicQueryTranslatorFactory (org.hibernate.hql.internal.classic.ClassicQueryTranslatorFactory)4 HashMap (java.util.HashMap)3 Map (java.util.Map)2 MappingException (org.hibernate.MappingException)2 QueryException (org.hibernate.QueryException)2 Test (org.junit.Test)2 TreeMap (java.util.TreeMap)1 StrategySelector (org.hibernate.boot.registry.selector.spi.StrategySelector)1 TestForIssue (org.hibernate.testing.TestForIssue)1