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