translators[i] = queryTranslatorFactory .createQueryTranslator( hql, concreteQueryStrings[i], enabledFilters, factory, entityGraphQueryHint ); translators[i].compile( querySubstitutions, shallow ); ( (FilterTranslator) translators[i] ).compile( collectionRole, querySubstitutions, shallow ); combinedQuerySpaces.addAll( translators[i].getQuerySpaces() ); this.parameterMetadata = buildParameterMetadata( translators[0].getParameterTranslations(), hql ); if ( translators[0].isManipulationStatement() ) { returnMetadata = null; final Type[] types = ( length > 1 ) ? new Type[translators[0].getReturnTypes().length] : translators[0].getReturnTypes(); returnMetadata = new ReturnMetadata( translators[0].getReturnAliases(), types );
/** * This method should only be called for debugging purposes as it regenerates a new array every time. */ public String[] getSqlStrings() { List<String> sqlStrings = new ArrayList<>(); for ( int i = 0; i < translators.length; i++ ) { sqlStrings.addAll( translators[i].collectSqlStrings() ); } return ArrayHelper.toStringArray( sqlStrings ); }
public Class getDynamicInstantiationResultType() { return translators[0].getDynamicInstantiationResultType(); }
getLoadQueryInfluencers().getEnabledFilters() ); if ( queryPlan.getTranslators()[0].isManipulationStatement() ) { throw new IllegalArgumentException( "Update/delete queries cannot be typed" ); else if ( queryPlan.getTranslators()[0].getReturnTypes().length == 1 ) { final Type queryResultType = queryPlan.getTranslators()[0].getReturnTypes()[0]; if ( !resultClass.isAssignableFrom( queryResultType.getReturnedClass() ) ) { throw new IllegalArgumentException(
@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 ) ); }
/** * Coordinates the efforts to perform a scroll across all the included query translators. * * @param queryParameters The query parameters * @param session The session * * @return The query result iterator * * @throws HibernateException Indicates a problem performing the query */ public ScrollableResultsImplementor performScroll( QueryParameters queryParameters, SharedSessionContractImplementor session) throws HibernateException { if ( traceEnabled ) { LOG.tracev( "Iterate: {0}", getSourceQuery() ); queryParameters.traceParameters( session.getFactory() ); } if ( translators.length != 1 ) { throw new QueryException( "implicit polymorphism not supported for scroll() queries" ); } if ( queryParameters.getRowSelection().definesLimits() && translators[0].containsCollectionFetches() ) { throw new QueryException( "firstResult/maxResults not supported in conjunction with scroll() of a query containing collection fetches" ); } return translators[0].scroll( queryParameters, session ); }
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] );
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; }
public boolean isSelect() { return !translators[0].isManipulationStatement(); }
/** * 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; }
private void checkQuerySpaces(QueryTranslator oldQueryTranslator, QueryTranslator newQueryTranslator) { // Check the query spaces for a regression. Set<Serializable> oldQuerySpaces = oldQueryTranslator.getQuerySpaces(); Set<Serializable> querySpaces = newQueryTranslator.getQuerySpaces(); assertEquals( "Query spaces is not the right size!", oldQuerySpaces.size(), querySpaces.size() ); for ( Object o : oldQuerySpaces ) { assertTrue( "New query space does not contain " + o + "!", querySpaces.contains( o ) ); } }
private void checkReturnedTypes(QueryTranslator oldQueryTranslator, QueryTranslator newQueryTranslator) { // Check the returned types for a regression. Type[] oldReturnTypes = oldQueryTranslator.getReturnTypes(); Type[] returnTypes = newQueryTranslator.getReturnTypes(); assertEquals( "Return types array is not the right length!", oldReturnTypes.length, returnTypes.length ); for ( int i = 0; i < returnTypes.length; i++ ) { assertNotNull( returnTypes[i] ); assertNotNull( oldReturnTypes[i] ); assertEquals( "Returned types did not match!", oldReturnTypes[i].getReturnedClass(), returnTypes[i].getReturnedClass() ); System.out.println("returnedType[" + i + "] = " + returnTypes[i] + " oldReturnTypes[" + i + "] = " + oldReturnTypes[i]); } }
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();
public ScrollableResults performScroll( QueryParameters queryParameters, SessionImplementor session) throws HibernateException { if ( LOG.isTraceEnabled() ) { LOG.tracev( "Iterate: {0}", getSourceQuery() ); queryParameters.traceParameters( session.getFactory() ); } if ( translators.length != 1 ) { throw new QueryException( "implicit polymorphism not supported for scroll() queries" ); } if ( queryParameters.getRowSelection().definesLimits() && translators[0].containsCollectionFetches() ) { throw new QueryException( "firstResult/maxResults not supported in conjunction with scroll() of a query containing collection fetches" ); } return translators[0].scroll( queryParameters, session ); }
QueryTranslatorFactory classic = new ClassicQueryTranslatorFactory(); oldQueryTranslator = classic.createQueryTranslator( hql, hql, Collections.EMPTY_MAP, factory, null ); oldQueryTranslator.compile( replacements, scalar );
@Override public String getSql(EntityManager em, Query query) { SessionImplementor session = em.unwrap(SessionImplementor.class); HQLQueryPlan queryPlan = getOriginalQueryPlan(session, query); if (queryPlan.getTranslators().length > 1) { throw new IllegalArgumentException("No support for multiple translators yet!"); } QueryTranslator queryTranslator = queryPlan.getTranslators()[0]; String[] sqls; if (queryTranslator.isManipulationStatement()) { StatementExecutor executor = getStatementExecutor(queryTranslator); if (!(executor instanceof BasicExecutor)) { throw new IllegalArgumentException("Using polymorphic deletes/updates with CTEs is not yet supported"); } sqls = executor.getSqlStatements(); } else { sqls = queryPlan.getSqlStrings(); } // TODO: have to handle multiple sql strings which happens when having e.g. a polymorphic UPDATE/DELETE for (int i = 0; i < sqls.length; i++) { if (sqls[i] != null) { return sqls[i]; } } return null; }
public int performExecuteUpdate(QueryParameters queryParameters, SessionImplementor session) throws HibernateException { if ( LOG.isTraceEnabled() ) { LOG.tracev( "Execute update: {0}", getSourceQuery() ); queryParameters.traceParameters( session.getFactory() ); } if ( translators.length != 1 ) { LOG.splitQueries( getSourceQuery(), translators.length ); } int result = 0; for ( int i = 0; i < translators.length; i++ ) { result += translators[i].executeUpdate( queryParameters, session ); } return result; }
.getQueryTranslatorFactory() .createQueryTranslator( hql, concreteQueryStrings[i], enabledFilters, factory ); translators[i].compile( factory.getSettings().getQuerySubstitutions(), shallow ); ( ( FilterTranslator ) translators[i] ).compile( collectionRole, factory.getSettings().getQuerySubstitutions(), shallow ); combinedQuerySpaces.addAll( translators[i].getQuerySpaces() ); sqlStringList.addAll( translators[i].collectSqlStrings() ); this.parameterMetadata = buildParameterMetadata( translators[0].getParameterTranslations(), hql ); if ( translators[0].isManipulationStatement() ) { returnMetadata = null; final int returns = translators[0].getReturnTypes().length; returnMetadata = new ReturnMetadata( translators[0].getReturnAliases(), new Type[returns] ); returnMetadata = new ReturnMetadata( translators[0].getReturnAliases(), translators[0].getReturnTypes() );
/** * 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(); } }
public ScrollableResults performScroll( QueryParameters queryParameters, SessionImplementor session) throws HibernateException { if ( LOG.isTraceEnabled() ) { LOG.tracev( "Iterate: {0}", getSourceQuery() ); queryParameters.traceParameters( session.getFactory() ); } if ( translators.length != 1 ) { throw new QueryException( "implicit polymorphism not supported for scroll() queries" ); } if ( queryParameters.getRowSelection().definesLimits() && translators[0].containsCollectionFetches() ) { throw new QueryException( "firstResult/maxResults not supported in conjunction with scroll() of a query containing collection fetches" ); } return translators[0].scroll( queryParameters, session ); }