Search in sources :

Example 1 with ClassicQueryTranslatorFactory

use of org.hibernate.hql.internal.classic.ClassicQueryTranslatorFactory 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 2 with ClassicQueryTranslatorFactory

use of org.hibernate.hql.internal.classic.ClassicQueryTranslatorFactory 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

ClassicQueryTranslatorFactory (org.hibernate.hql.internal.classic.ClassicQueryTranslatorFactory)2 QueryTranslator (org.hibernate.hql.spi.QueryTranslator)2 QueryTranslatorFactory (org.hibernate.hql.spi.QueryTranslatorFactory)2 HashMap (java.util.HashMap)1 MappingException (org.hibernate.MappingException)1 QueryException (org.hibernate.QueryException)1 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)1 ASTQueryTranslatorFactory (org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory)1 QueryTranslatorImpl (org.hibernate.hql.internal.ast.QueryTranslatorImpl)1 Test (org.junit.Test)1