From: <hib...@li...> - 2006-03-31 17:48:28
|
Author: ste...@jb... Date: 2006-03-31 12:47:53 -0500 (Fri, 31 Mar 2006) New Revision: 9724 Modified: branches/Branch_3_1/Hibernate3/src/org/hibernate/hql/ast/HqlSqlWalker.java branches/Branch_3_1/Hibernate3/src/org/hibernate/hql/ast/tree/FromElementType.java Log: HHH-1419 : missed applying to 3.1 branch originally Modified: branches/Branch_3_1/Hibernate3/src/org/hibernate/hql/ast/HqlSqlWalker.java =================================================================== --- branches/Branch_3_1/Hibernate3/src/org/hibernate/hql/ast/HqlSqlWalker.java 2006-03-31 15:41:05 UTC (rev 9723) +++ branches/Branch_3_1/Hibernate3/src/org/hibernate/hql/ast/HqlSqlWalker.java 2006-03-31 17:47:53 UTC (rev 9724) @@ -301,7 +301,7 @@ AST fetchNode, AST propertyFetch, AST with) throws SemanticException { - boolean fetch = ( fetchNode != null ) ? true : false; + boolean fetch = ( fetchNode != null ); // The path AST should be a DotNode, and it should have been evaluated already. if ( path.getType() != SqlTokenTypes.DOT ) { throw new SemanticException( "Path expected for join!" ); @@ -765,8 +765,8 @@ } ParameterNode parameter = ( ParameterNode ) astFactory.create( PARAM, "?" ); PositionalParameterSpecification paramSpec = new PositionalParameterSpecification( - ( ( Node ) inputNode ).getLine(), - ( ( Node ) inputNode ).getColumn(), + inputNode.getLine(), + inputNode.getColumn(), positionalParameterCount++ ); parameter.setHqlParameterSpecification( paramSpec ); @@ -784,8 +784,8 @@ parameter.setText( "?" ); NamedParameterSpecification paramSpec = new NamedParameterSpecification( - ( ( Node ) delimiterNode ).getLine(), - ( ( Node ) delimiterNode ).getColumn(), + delimiterNode.getLine(), + delimiterNode.getColumn(), name ); parameter.setHqlParameterSpecification( paramSpec ); @@ -878,8 +878,11 @@ } public FromClause getFinalFromClause() { - //TODO: more correct implementation - return currentFromClause; + FromClause top = currentFromClause; + while ( top.getParentFromClause() != null ) { + top = top.getParentFromClause(); + } + return top; } public boolean isShallowQuery() { Modified: branches/Branch_3_1/Hibernate3/src/org/hibernate/hql/ast/tree/FromElementType.java =================================================================== --- branches/Branch_3_1/Hibernate3/src/org/hibernate/hql/ast/tree/FromElementType.java 2006-03-31 15:41:05 UTC (rev 9723) +++ branches/Branch_3_1/Hibernate3/src/org/hibernate/hql/ast/tree/FromElementType.java 2006-03-31 17:47:53 UTC (rev 9724) @@ -5,6 +5,7 @@ import org.hibernate.MappingException; import org.hibernate.QueryException; +import org.hibernate.util.ArrayHelper; import org.hibernate.engine.JoinSequence; import org.hibernate.hql.CollectionProperties; import org.hibernate.hql.CollectionSubqueryFactory; @@ -152,8 +153,7 @@ } private static String generateSuffix(int size, int k) { - String suffix = size == 1 ? "" : Integer.toString( k ) + '_'; - return suffix; + return size == 1 ? "" : Integer.toString( k ) + '_'; } private void checkInitialized() { @@ -308,20 +308,66 @@ return new String[]{"(" + subquery + ")"}; } else { - // decide if we need to use table-alias qualification - boolean useTableAlias = fromElement.getWalker().getStatementType() == HqlSqlTokenTypes.SELECT - || fromElement.getWalker().getCurrentClauseType() == HqlSqlTokenTypes.SELECT - || fromElement.getWalker().isSubQuery() - || forceAlias; - if ( useTableAlias ) { + if ( forceAlias ) { return propertyMapping.toColumns( tableAlias, path ); } + else if ( fromElement.getWalker().getStatementType() == HqlSqlTokenTypes.SELECT ) { + return propertyMapping.toColumns( tableAlias, path ); + } + else if ( fromElement.getWalker().getCurrentClauseType() == HqlSqlTokenTypes.SELECT ) { + return propertyMapping.toColumns( tableAlias, path ); + } + else if ( fromElement.getWalker().isSubQuery() ) { + // for a subquery, the alias to use depends on a few things (we + // already know this is not an overall SELECT): + // 1) if this FROM_ELEMENT represents a correlation to the + // outer-most query + // A) if the outer query represents a multi-table + // persister, we need to use the given alias + // in anticipation of one of the multi-table + // executors being used (as this subquery will + // actually be used in the "id select" phase + // of that multi-table executor) + // B) otherwise, we need to use the persister's + // table name as the column qualification + // 2) otherwise (not correlated), use the given alias + if ( isCorrelation() ) { + if ( isMultiTable() ) { + return propertyMapping.toColumns( tableAlias, path ); + } + else { + return propertyMapping.toColumns( extractTableName(), path ); + } + } + else { + return propertyMapping.toColumns( tableAlias, path ); + } + } else { - return propertyMapping.toColumns( path ); + String[] columns = propertyMapping.toColumns( path ); + log.warn( "Using non-qualified column reference [" + path + " -> (" + ArrayHelper.toString( columns ) + ")]" ); + return columns; } } } + private boolean isCorrelation() { + FromClause top = fromElement.getWalker().getFinalFromClause(); + return fromElement.getFromClause() != fromElement.getWalker().getCurrentFromClause() && + fromElement.getFromClause() == top; + } + + private boolean isMultiTable() { + // should be safe to only ever expect EntityPersister references here + return fromElement.getQueryable() != null && + fromElement.getQueryable().isMultiTable(); + } + + private String extractTableName() { + // should be safe to only ever expect EntityPersister references here + return fromElement.getQueryable().getTableName(); + } + PropertyMapping getPropertyMapping(String propertyName) { checkInitialized(); if ( queryableCollection == null ) { // Not a collection? |