use of org.hibernate.hql.spi.QueryTranslator 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());
}
use of org.hibernate.hql.spi.QueryTranslator 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());
}
use of org.hibernate.hql.spi.QueryTranslator in project midpoint by Evolveum.
the class HibernateToSqlTranslator method toSql.
/**
* Do not use in production code! Only for testing purposes only. Used for example during query engine upgrade.
* Method provides translation from hibernate HQL query to plain SQL string query.
*
* @param sessionFactory
* @param hqlQueryText
* @return SQL string, null if hqlQueryText parameter is empty.
*/
public static String toSql(SessionFactory sessionFactory, String hqlQueryText) {
Validate.notNull(sessionFactory, "Session factory must not be null.");
if (StringUtils.isEmpty(hqlQueryText)) {
return null;
}
final QueryTranslatorFactory translatorFactory = new ASTQueryTranslatorFactory();
final SessionFactoryImplementor factory = (SessionFactoryImplementor) sessionFactory;
final QueryTranslator translator = translatorFactory.createQueryTranslator(hqlQueryText, hqlQueryText, Collections.EMPTY_MAP, factory, null);
translator.compile(Collections.EMPTY_MAP, false);
return translator.getSQLString();
}
use of org.hibernate.hql.spi.QueryTranslator in project hibernate-orm by hibernate.
the class HQLQueryPlan method performList.
/**
* Coordinates the efforts to perform a list across all the included query translators.
*
* @param queryParameters The query parameters
* @param session The session
*
* @return The query result list
*
* @throws HibernateException Indicates a problem performing the query
*/
@SuppressWarnings("unchecked")
public List performList(QueryParameters queryParameters, SharedSessionContractImplementor session) throws HibernateException {
if (traceEnabled) {
LOG.tracev("Find: {0}", getSourceQuery());
queryParameters.traceParameters(session.getFactory());
}
final RowSelection rowSelection = queryParameters.getRowSelection();
final boolean hasLimit = rowSelection != null && rowSelection.definesLimits();
final boolean needsLimit = hasLimit && translators.length > 1;
final QueryParameters queryParametersToUse;
if (needsLimit) {
LOG.needsLimit();
final RowSelection selection = new RowSelection();
selection.setFetchSize(queryParameters.getRowSelection().getFetchSize());
selection.setTimeout(queryParameters.getRowSelection().getTimeout());
queryParametersToUse = queryParameters.createCopyUsing(selection);
} else {
queryParametersToUse = queryParameters;
}
// fast path to avoid unnecessary allocation and copying
if (translators.length == 1) {
return translators[0].list(session, queryParametersToUse);
}
final int guessedResultSize = guessResultSize(rowSelection);
final List combinedResults = new ArrayList(guessedResultSize);
final IdentitySet distinction;
if (needsLimit) {
distinction = new IdentitySet(guessedResultSize);
} else {
distinction = null;
}
int includedCount = -1;
translator_loop: for (QueryTranslator translator : translators) {
final List tmp = translator.list(session, queryParametersToUse);
if (needsLimit) {
// NOTE : firstRow is zero-based
final int first = queryParameters.getRowSelection().getFirstRow() == null ? 0 : queryParameters.getRowSelection().getFirstRow();
final int max = queryParameters.getRowSelection().getMaxRows() == null ? -1 : queryParameters.getRowSelection().getMaxRows();
for (final Object result : tmp) {
if (!distinction.add(result)) {
continue;
}
includedCount++;
if (includedCount < first) {
continue;
}
combinedResults.add(result);
if (max >= 0 && includedCount > max) {
// break the outer loop !!!
break translator_loop;
}
}
} else {
combinedResults.addAll(tmp);
}
}
return combinedResults;
}
use of org.hibernate.hql.spi.QueryTranslator 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]);
}
Aggregations