private void checkSql(QueryTranslator oldQueryTranslator, QueryTranslator newQueryTranslator, String hql, boolean scalar, String sql) { String oldsql = oldQueryTranslator.getSQLString(); String newsql = newQueryTranslator.getSQLString(); System.out.println( "HQL : " + ASTPrinter.escapeMultibyteChars(hql) ); System.out.println( "OLD SQL: " + ASTPrinter.escapeMultibyteChars(oldsql) ); System.out.println( "NEW SQL: " + ASTPrinter.escapeMultibyteChars(newsql) ); if ( sql == null ) { // Check the generated SQL. ASTPrinter.escapeMultibyteChars( assertSQLEquals( "SQL is not the same as the old SQL (scalar=" + scalar + ")", oldsql, newsql ); } else { assertSQLEquals( "SQL is not the same as the expected SQL (scalar=" + scalar + ")", sql, newsql ); } }
@Test @TestForIssue(jiraKey = "HHH-8579") public void testAliasWithLocale() { // Without the HHH-8579 fix, this will generate non-ascii query aliases. String hql = "from IAmAFoo"; QueryTranslatorFactory ast = new ASTQueryTranslatorFactory(); QueryTranslator queryTranslator = ast.createQueryTranslator( hql, hql, Collections.EMPTY_MAP, sessionFactory(), null ); queryTranslator.compile( Collections.EMPTY_MAP, false ); String sql = queryTranslator.getSQLString(); assertTrue( sql.matches( asciiRegex ) ); }
@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 ); }
private void testMultiJoinAddition(String hql) { final HQLQueryPlan plan = sessionFactory().getQueryPlanCache().getHQLQueryPlan( hql, false, Collections.EMPTY_MAP ); assertEquals( 1, plan.getTranslators().length ); final QueryTranslator translator = plan.getTranslators()[0]; final String generatedSql = translator.getSQLString(); int sub1JoinColumnIndex = generatedSql.indexOf( ".base_sub_1" ); assertNotEquals( "Generated SQL doesn't contain a join for 'base' with 'PolymorphicSub1' via 'base_sub_1':\n" + generatedSql, -1, sub1JoinColumnIndex ); int sub2JoinColumnIndex = generatedSql.indexOf( ".base_sub_2" ); assertNotEquals( "Generated SQL doesn't contain a join for 'base' with 'PolymorphicSub2' via 'base_sub_2':\n" + generatedSql, -1, sub2JoinColumnIndex ); }
assertEquals( 1, plan.getTranslators().length ); final QueryTranslator translator = plan.getTranslators()[0]; final String generatedSql = translator.getSQLString(); System.out.println( "Generated SQL : " + generatedSql );
QueryTranslatorFactory translatorFactory = new ASTQueryTranslatorFactory(); SessionFactoryImplementor factory = (SessionFactoryImplementor) getSessionFactory(); QueryTranslator translator = translatorFactory. createQueryTranslator(hqlQueryText, hqlQueryText, Collections.EMPTY_MAP, factory); translator.compile(Collections.EMPTY_MAP, false); translator.getSQLString();
/** * 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(); } }