FileDocCategorySizeDatePackage
JoinSequence.javaAPI DocHibernate 3.2.58265Fri Feb 24 15:12:14 GMT 2006org.hibernate.engine

JoinSequence

public class JoinSequence extends Object
author
Gavin King

Fields Summary
private final SessionFactoryImplementor
factory
private final List
joins
private boolean
useThetaStyle
private final StringBuffer
conditions
private String
rootAlias
private org.hibernate.persister.entity.Joinable
rootJoinable
private Selector
selector
private JoinSequence
next
private boolean
isFromPart
Constructors Summary
public JoinSequence(SessionFactoryImplementor factory)

		this.factory = factory;
	
Methods Summary
public org.hibernate.engine.JoinSequenceaddCondition(java.lang.String condition)

		if ( condition.trim().length() != 0 ) {
			if ( !condition.startsWith( " and " ) ) conditions.append( " and " );
			conditions.append( condition );
		}
		return this;
	
public org.hibernate.engine.JoinSequenceaddCondition(java.lang.String alias, java.lang.String[] columns, java.lang.String condition)

		for ( int i = 0; i < columns.length; i++ ) {
			conditions.append( " and " )
					.append( alias )
					.append( '." )
					.append( columns[i] )
					.append( condition );
		}
		return this;
	
private voidaddExtraJoins(org.hibernate.sql.JoinFragment joinFragment, java.lang.String alias, org.hibernate.persister.entity.Joinable joinable, boolean innerJoin)

		boolean include = isIncluded( alias );
		joinFragment.addJoins( joinable.fromJoinFragment( alias, innerJoin, include ),
				joinable.whereJoinFragment( alias, innerJoin, include ) );
	
public org.hibernate.engine.JoinSequenceaddJoin(org.hibernate.type.AssociationType associationType, java.lang.String alias, int joinType, java.lang.String[] referencingKey)

		joins.add( new Join( associationType, alias, joinType, referencingKey ) );
		return this;
	
public org.hibernate.engine.JoinSequencecopy()

		JoinSequence copy = new JoinSequence( factory );
		copy.joins.addAll( this.joins );
		copy.useThetaStyle = this.useThetaStyle;
		copy.rootAlias = this.rootAlias;
		copy.rootJoinable = this.rootJoinable;
		copy.selector = this.selector;
		copy.next = this.next == null ? null : this.next.copy();
		copy.isFromPart = this.isFromPart;
		copy.conditions.append( this.conditions.toString() );
		return copy;
	
public org.hibernate.engine.JoinSequencegetFromPart()

		JoinSequence fromPart = new JoinSequence( factory );
		fromPart.joins.addAll( this.joins );
		fromPart.useThetaStyle = this.useThetaStyle;
		fromPart.rootAlias = this.rootAlias;
		fromPart.rootJoinable = this.rootJoinable;
		fromPart.selector = this.selector;
		fromPart.next = this.next == null ? null : this.next.getFromPart();
		fromPart.isFromPart = true;
		return fromPart;
	
public intgetJoinCount()

		return joins.size();
	
private booleanisIncluded(java.lang.String alias)

		return selector != null && selector.includeSubclasses( alias );
	
private booleanisManyToManyRoot(org.hibernate.persister.entity.Joinable joinable)

		if ( joinable != null && joinable.isCollection() ) {
			QueryableCollection persister = ( QueryableCollection ) joinable;
			return persister.isManyToMany();
		}
		return false;
	
public booleanisThetaStyle()

		return useThetaStyle;
	
public org.hibernate.engine.JoinSequencesetNext(org.hibernate.engine.JoinSequence next)

		this.next = next;
		return this;
	
public org.hibernate.engine.JoinSequencesetRoot(org.hibernate.persister.entity.Joinable joinable, java.lang.String alias)

		this.rootAlias = alias;
		this.rootJoinable = joinable;
		return this;
	
public org.hibernate.engine.JoinSequencesetSelector(org.hibernate.engine.JoinSequence$Selector s)

		this.selector = s;
		return this;
	
public org.hibernate.engine.JoinSequencesetUseThetaStyle(boolean useThetaStyle)

		this.useThetaStyle = useThetaStyle;
		return this;
	
public org.hibernate.sql.JoinFragmenttoJoinFragment()

		return toJoinFragment( CollectionHelper.EMPTY_MAP, true );
	
public org.hibernate.sql.JoinFragmenttoJoinFragment(java.util.Map enabledFilters, boolean includeExtraJoins)

		return toJoinFragment( enabledFilters, includeExtraJoins, null, null );
	
public org.hibernate.sql.JoinFragmenttoJoinFragment(java.util.Map enabledFilters, boolean includeExtraJoins, java.lang.String withClauseFragment, java.lang.String withClauseJoinAlias)

		QueryJoinFragment joinFragment = new QueryJoinFragment( factory.getDialect(), useThetaStyle );
		if ( rootJoinable != null ) {
			joinFragment.addCrossJoin( rootJoinable.getTableName(), rootAlias );
			String filterCondition = rootJoinable.filterFragment( rootAlias, enabledFilters );
			// JoinProcessor needs to know if the where clause fragment came from a dynamic filter or not so it
			// can put the where clause fragment in the right place in the SQL AST.   'hasFilterCondition' keeps track
			// of that fact.
			joinFragment.setHasFilterCondition( joinFragment.addCondition( filterCondition ) );
			if (includeExtraJoins) { //TODO: not quite sure about the full implications of this!
				addExtraJoins( joinFragment, rootAlias, rootJoinable, true );
			}
		}

		Joinable last = rootJoinable;

		for ( int i = 0; i < joins.size(); i++ ) {
			Join join = ( Join ) joins.get( i );
			String on = join.getAssociationType().getOnCondition( join.getAlias(), factory, enabledFilters );
			String condition = null;
			if ( last != null &&
			        isManyToManyRoot( last ) &&
			        ( ( QueryableCollection ) last ).getElementType() == join.getAssociationType() ) {
				// the current join represents the join between a many-to-many association table
				// and its "target" table.  Here we need to apply any additional filters
				// defined specifically on the many-to-many
				String manyToManyFilter = ( ( QueryableCollection ) last )
				        .getManyToManyFilterFragment( join.getAlias(), enabledFilters );
				condition = "".equals( manyToManyFilter )
						? on
						: "".equals( on )
								? manyToManyFilter
								: on + " and " + manyToManyFilter;
			}
			else {
				condition = on;
			}
			if ( withClauseFragment != null ) {
				if ( join.getAlias().equals( withClauseJoinAlias ) ) {
					condition += " and " + withClauseFragment;
				}
			}
			joinFragment.addJoin(
			        join.getJoinable().getTableName(),
					join.getAlias(),
					join.getLHSColumns(),
					JoinHelper.getRHSColumnNames( join.getAssociationType(), factory ),
					join.joinType,
					condition
			);
			if (includeExtraJoins) { //TODO: not quite sure about the full implications of this!
				addExtraJoins( joinFragment, join.getAlias(), join.getJoinable(), join.joinType == JoinFragment.INNER_JOIN );
			}
			last = join.getJoinable();
		}
		if ( next != null ) {
			joinFragment.addFragment( next.toJoinFragment( enabledFilters, includeExtraJoins ) );
		}
		joinFragment.addCondition( conditions.toString() );
		if ( isFromPart ) joinFragment.clearWherePart();
		return joinFragment;
	
public java.lang.StringtoString()


	   
		StringBuffer buf = new StringBuffer();
		buf.append( "JoinSequence{" );
		if ( rootJoinable != null ) {
			buf.append( rootJoinable )
					.append( '[" )
					.append( rootAlias )
					.append( ']" );
		}
		for ( int i = 0; i < joins.size(); i++ ) {
			buf.append( "->" ).append( joins.get( i ) );
		}
		return buf.append( '}" ).toString();