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]);
}
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);
}
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;
}
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);
}
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);
}
Aggregations