[hibernate-commits] Hibernate SVN: r16391 - in core/branches/antlr3/src/main/java/org/hibernate/sql/ast: tree and 1 other directory.

hibernate-commits at lists.jboss.org hibernate-commits at lists.jboss.org
Tue Apr 21 16:24:58 EDT 2009


Author: steve.ebersole at jboss.com
Date: 2009-04-21 16:24:58 -0400 (Tue, 21 Apr 2009)
New Revision: 16391

Added:
   core/branches/antlr3/src/main/java/org/hibernate/sql/ast/phase/hql/resolve/path/impl/FromClausePathResolutionStrategy.java
   core/branches/antlr3/src/main/java/org/hibernate/sql/ast/phase/hql/resolve/path/impl/OnFragmentPathResolutionStrategy.java
   core/branches/antlr3/src/main/java/org/hibernate/sql/ast/phase/hql/resolve/path/impl/SelectClausePathResolutionStrategy.java
   core/branches/antlr3/src/main/java/org/hibernate/sql/ast/phase/hql/resolve/path/impl/WithFragmentPathResolutionStrategy.java
Modified:
   core/branches/antlr3/src/main/java/org/hibernate/sql/ast/phase/hql/resolve/path/impl/AbstractPathResolutionStrategy.java
   core/branches/antlr3/src/main/java/org/hibernate/sql/ast/tree/Table.java
Log:
remainder of path resolution strategies

Modified: core/branches/antlr3/src/main/java/org/hibernate/sql/ast/phase/hql/resolve/path/impl/AbstractPathResolutionStrategy.java
===================================================================
--- core/branches/antlr3/src/main/java/org/hibernate/sql/ast/phase/hql/resolve/path/impl/AbstractPathResolutionStrategy.java	2009-04-21 19:41:54 UTC (rev 16390)
+++ core/branches/antlr3/src/main/java/org/hibernate/sql/ast/phase/hql/resolve/path/impl/AbstractPathResolutionStrategy.java	2009-04-21 20:24:58 UTC (rev 16391)
@@ -457,6 +457,18 @@
 		}
 	}
 
+	protected Table createJoin(PersisterSpace lhs, String propertyName, String alias) {
+		validateJoinable( lhs, propertyName );
+		Type propertyType = lhs.getPropertyType( propertyName );
+		if ( propertyType.isEntityType() ) {
+			return createJoin( lhs, resolveEntityPersister( lhs, propertyName ), alias );
+		}
+		else {
+			// assume collection because of validation of being joinable...
+			return createJoin( lhs, resolveCollectionPersister( lhs, propertyName ), alias, null );
+		}
+	}
+
 	protected Table createJoin(PersisterSpace lhs, Queryable entityPersister, String alias) {
 		EntityType entityType = entityPersister.getEntityMetamodel().getEntityType();
 
@@ -554,6 +566,16 @@
 		return collectionTableExpression;
 	}
 
+	protected void validateJoinable(PersisterSpace lhs, String propertyName) {
+		if ( ! isAssociation( lhs.getPropertyType( propertyName ) ) ) {
+			throw new InvalidPropertyJoinException( getPathThusFar(), lhs.getName(), propertyName );
+		}
+	}
+
+	protected boolean isAssociation(Type propertyType) {
+		return propertyType.isAssociationType();
+	}
+
 	protected void validateCollectionReference(PersisterSpace lhs, String propertyName) {
 		if ( ! isCollectionReference( lhs.getPropertyType( propertyName ) ) ) {
 			throw new CollectionExpectedException( getPathThusFar(), lhs.getName(), propertyName );

Added: core/branches/antlr3/src/main/java/org/hibernate/sql/ast/phase/hql/resolve/path/impl/FromClausePathResolutionStrategy.java
===================================================================
--- core/branches/antlr3/src/main/java/org/hibernate/sql/ast/phase/hql/resolve/path/impl/FromClausePathResolutionStrategy.java	                        (rev 0)
+++ core/branches/antlr3/src/main/java/org/hibernate/sql/ast/phase/hql/resolve/path/impl/FromClausePathResolutionStrategy.java	2009-04-21 20:24:58 UTC (rev 16391)
@@ -0,0 +1,125 @@
+/*
+ * Hibernate, Relational Persistence for Idiomatic Java
+ *
+ * Copyright (c) 2009, Red Hat Middleware LLC or third-party
+ * contributors as
+ * indicated by the @author tags or express copyright attribution
+ * statements applied by the authors.  All third-party contributions are
+ * distributed under license by Red Hat Middleware LLC.
+ *
+ * This copyrighted material is made available to anyone wishing to use,
+ * modify,
+ * copy, or redistribute it subject to the terms and conditions of the GNU
+ * Lesser General Public License, as published by the Free Software
+ * Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
+ * License
+ * for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this distribution; if not, write to:
+ * Free Software Foundation, Inc.
+ * 51 Franklin Street, Fifth Floor
+ * Boston, MA  02110-1301  USA
+ */
+package org.hibernate.sql.ast.phase.hql.resolve.path.impl;
+
+import org.hibernate.QueryException;
+import org.hibernate.sql.ast.common.HibernateTree;
+import org.hibernate.sql.ast.common.JoinType;
+import org.hibernate.sql.ast.phase.hql.parse.HQLParser;
+import org.hibernate.sql.ast.phase.hql.resolve.PersisterSpace;
+import org.hibernate.sql.ast.phase.hql.resolve.ResolutionContext;
+import org.hibernate.sql.ast.phase.hql.resolve.path.PathedPropertyReferenceSource;
+import org.hibernate.sql.ast.tree.Table;
+
+/**
+ *
+ * {@link org.hibernate.sql.ast.phase.hql.resolve.path.PathResolutionStrategy} for dealing with path expressions
+ * occuring in the <tt>FROM</tt> clause of a query.
+ *
+ * @author Steve Ebersole
+ */
+public class FromClausePathResolutionStrategy extends AbstractPathResolutionStrategy {
+	private final JoinType joinType;
+	private final boolean associationFetch;
+	private final boolean propertyFetch;
+	private final String alias;
+
+	/**
+	 * Instantiate a normalization strategy for handling path expressions in a <tt>FROM</tt> clause.
+	 *
+	 * @param resolutionContext The context for resolution.
+	 * @param joinType The type of explicit-join specified at the root of this path expression.
+	 * @param associationFetch Was association fetching indicated on this path expression?
+	 * @param propertyFetch Was property fetching indicated on this path expression?
+	 * @param alias The alias (if one) specified on this joined path expression.
+	 */
+	public FromClausePathResolutionStrategy(
+			ResolutionContext resolutionContext,
+			JoinType joinType,
+			boolean associationFetch,
+			boolean propertyFetch,
+			String alias) {
+		super( resolutionContext );
+		this.joinType = joinType;
+		this.associationFetch = associationFetch;
+		this.propertyFetch = propertyFetch;
+		this.alias = alias;
+	}
+
+	protected PathedPropertyReferenceSource internalHandleRoot(PersisterSpace persisterSpace) {
+		return new SourceImpl( persisterSpace );
+	}
+
+	private class SourceImpl extends AbstractPathedPropertyReferenceSource {
+		private final PersisterSpace lhs;
+
+		private SourceImpl(PersisterSpace lhs) {
+			this.lhs = lhs;
+		}
+
+		public PathedPropertyReferenceSource handleIntermediatePathPart(String name) {
+			// TODO : still need to account for paths including component dereferences...
+			Table joinedTable = buildPropertyJoinedTable( lhs, name, null, false, associationFetch );
+			return new SourceImpl( joinedTable.getTableSpace().getPersisterSpace() );
+		}
+
+		public HibernateTree handleTerminalPathPart(String propertyName) {
+			validateJoinable( lhs, propertyName );
+
+			Table joinedTable = buildPropertyJoinedTable( lhs, propertyName, alias, propertyFetch, associationFetch );
+			return joinedTable.getTableSpace().buildIdentifierColumnReferences();
+		}
+
+		public PathedPropertyReferenceSource handleIntermediateIndexAccess(String name, HibernateTree selector) {
+			throw new UnsupportedOperationException( "index operation not supported in from clause" );
+		}
+
+		public HibernateTree handleTerminalIndexAccess(String name, HibernateTree selector) {
+			throw new UnsupportedOperationException( "index operation not supported in from clause" );
+		}
+	}
+
+	protected HibernateTree buildJoinTypeNode() {
+		if ( joinType == JoinType.INNER ) {
+			return new HibernateTree( HQLParser.INNER );
+		}
+		else if ( joinType == JoinType.LEFT ) {
+			return new HibernateTree( HQLParser.LEFT );
+		}
+		else if ( joinType == JoinType.RIGHT ) {
+			return new HibernateTree( HQLParser.RIGHT );
+		}
+		// if no match found, throw exception
+		throw new QueryException( "Unrecognized join type [" + joinType.toString() + "]" );
+	}
+
+	protected boolean areJoinsReusable() {
+		return false;
+	}
+}

Added: core/branches/antlr3/src/main/java/org/hibernate/sql/ast/phase/hql/resolve/path/impl/OnFragmentPathResolutionStrategy.java
===================================================================
--- core/branches/antlr3/src/main/java/org/hibernate/sql/ast/phase/hql/resolve/path/impl/OnFragmentPathResolutionStrategy.java	                        (rev 0)
+++ core/branches/antlr3/src/main/java/org/hibernate/sql/ast/phase/hql/resolve/path/impl/OnFragmentPathResolutionStrategy.java	2009-04-21 20:24:58 UTC (rev 16391)
@@ -0,0 +1,74 @@
+/*
+ * Hibernate, Relational Persistence for Idiomatic Java
+ *
+ * Copyright (c) 2009, Red Hat Middleware LLC or third-party
+ * contributors as
+ * indicated by the @author tags or express copyright attribution
+ * statements applied by the authors.  All third-party contributions are
+ * distributed under license by Red Hat Middleware LLC.
+ *
+ * This copyrighted material is made available to anyone wishing to use,
+ * modify,
+ * copy, or redistribute it subject to the terms and conditions of the GNU
+ * Lesser General Public License, as published by the Free Software
+ * Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
+ * License
+ * for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this distribution; if not, write to:
+ * Free Software Foundation, Inc.
+ * 51 Franklin Street, Fifth Floor
+ * Boston, MA  02110-1301  USA
+ */
+package org.hibernate.sql.ast.phase.hql.resolve.path.impl;
+
+import org.hibernate.HibernateException;
+import org.hibernate.sql.ast.phase.hql.resolve.PersisterSpace;
+import org.hibernate.sql.ast.phase.hql.resolve.ResolutionContext;
+import org.hibernate.sql.ast.phase.hql.resolve.path.PathedPropertyReferenceSource;
+
+/**
+ * {@link org.hibernate.sql.ast.phase.hql.resolve.path.PathResolutionStrategy} for dealing with path expressions
+ * occuring in the <tt>ON</tt> clause of a persister join.
+ *
+ * @author Steve Ebersole
+ */
+public class OnFragmentPathResolutionStrategy extends BasicPathResolutionStrategySupport {
+	private final PersisterSpace joinRhs;
+	private PersisterSpace joinLhs;
+
+	public OnFragmentPathResolutionStrategy(ResolutionContext resolutionContext, PersisterSpace joinRhs) {
+		super( resolutionContext );
+		this.joinRhs = joinRhs;
+	}
+
+	public PersisterSpace getDiscoveredLHS() {
+		return joinLhs;
+	}
+
+	protected PathedPropertyReferenceSource internalHandleRoot(PersisterSpace persisterSpace) {
+		// persisterSpace must either refer to our LHS or RHS...
+		if ( persisterSpace == joinRhs ) {
+			// nothing to do...
+		}
+		else if ( joinLhs != null ) {
+			if ( persisterSpace != joinLhs ) {
+				throw new HibernateException(
+						"path root not resolveable against either left-hand-side [" +
+						joinLhs.getSourceAlias() + "] nor right-hand-side [" +
+						joinRhs.getSourceAlias() + "] of the join"
+				);
+			}
+		}
+		else {
+			joinLhs = persisterSpace;
+		}
+		return super.internalHandleRoot( persisterSpace );
+	}
+}

Added: core/branches/antlr3/src/main/java/org/hibernate/sql/ast/phase/hql/resolve/path/impl/SelectClausePathResolutionStrategy.java
===================================================================
--- core/branches/antlr3/src/main/java/org/hibernate/sql/ast/phase/hql/resolve/path/impl/SelectClausePathResolutionStrategy.java	                        (rev 0)
+++ core/branches/antlr3/src/main/java/org/hibernate/sql/ast/phase/hql/resolve/path/impl/SelectClausePathResolutionStrategy.java	2009-04-21 20:24:58 UTC (rev 16391)
@@ -0,0 +1,51 @@
+/*
+ * Hibernate, Relational Persistence for Idiomatic Java
+ *
+ * Copyright (c) 2009, Red Hat Middleware LLC or third-party
+ * contributors as
+ * indicated by the @author tags or express copyright attribution
+ * statements applied by the authors.  All third-party contributions are
+ * distributed under license by Red Hat Middleware LLC.
+ *
+ * This copyrighted material is made available to anyone wishing to use,
+ * modify,
+ * copy, or redistribute it subject to the terms and conditions of the GNU
+ * Lesser General Public License, as published by the Free Software
+ * Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
+ * License
+ * for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this distribution; if not, write to:
+ * Free Software Foundation, Inc.
+ * 51 Franklin Street, Fifth Floor
+ * Boston, MA  02110-1301  USA
+ */
+
+package org.hibernate.sql.ast.phase.hql.resolve.path.impl;
+
+import org.hibernate.sql.ast.phase.hql.resolve.ResolutionContext;
+
+/**
+ * Essentially the same stragety as {@link BasicPathResolutionStrategySupport} expcept that in the select clause
+ * an entity encountered as the terminal part must force a join...
+ * <p/>
+ * todo : the above is true as long as the path is not a function argument...
+ *
+ * @author Steve Ebersole
+ */
+public class SelectClausePathResolutionStrategy extends BasicPathResolutionStrategySupport {
+	public SelectClausePathResolutionStrategy(ResolutionContext resolutionContext) {
+		super( resolutionContext );
+	}
+
+	protected boolean shouldTerminalEntityPropertyForceJoin() {
+		// here we should *as long as* we are not part of a function processing
+		return ! resolutionContext().isCurrentlyProcessingFunction();
+	}
+}

Added: core/branches/antlr3/src/main/java/org/hibernate/sql/ast/phase/hql/resolve/path/impl/WithFragmentPathResolutionStrategy.java
===================================================================
--- core/branches/antlr3/src/main/java/org/hibernate/sql/ast/phase/hql/resolve/path/impl/WithFragmentPathResolutionStrategy.java	                        (rev 0)
+++ core/branches/antlr3/src/main/java/org/hibernate/sql/ast/phase/hql/resolve/path/impl/WithFragmentPathResolutionStrategy.java	2009-04-21 20:24:58 UTC (rev 16391)
@@ -0,0 +1,125 @@
+/*
+ * Hibernate, Relational Persistence for Idiomatic Java
+ *
+ * Copyright (c) 2009, Red Hat Middleware LLC or third-party
+ * contributors as
+ * indicated by the @author tags or express copyright attribution
+ * statements applied by the authors.  All third-party contributions are
+ * distributed under license by Red Hat Middleware LLC.
+ *
+ * This copyrighted material is made available to anyone wishing to use,
+ * modify,
+ * copy, or redistribute it subject to the terms and conditions of the GNU
+ * Lesser General Public License, as published by the Free Software
+ * Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
+ * License
+ * for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this distribution; if not, write to:
+ * Free Software Foundation, Inc.
+ * 51 Franklin Street, Fifth Floor
+ * Boston, MA  02110-1301  USA
+ */
+
+package org.hibernate.sql.ast.phase.hql.resolve.path.impl;
+
+import org.hibernate.QueryException;
+import org.hibernate.HibernateException;
+import org.hibernate.sql.ast.phase.hql.resolve.PersisterSpace;
+import org.hibernate.sql.ast.phase.hql.resolve.ResolutionContext;
+import org.hibernate.sql.ast.phase.hql.resolve.path.PathedPropertyReferenceSource;
+import org.hibernate.sql.ast.common.HibernateTree;
+import org.hibernate.sql.ast.tree.Table;
+
+/**
+ * {@link org.hibernate.sql.ast.phase.hql.resolve.path.PathResolutionStrategy} for dealing with <tt>WITH</tt>
+ * fragments used to supply addition join conditions to property joins.
+ *
+ * @author Steve Ebersole
+ */
+public class WithFragmentPathResolutionStrategy extends BasicPathResolutionStrategySupport {
+	private final PersisterSpace lhs;
+
+	private String baseRhsPropertyName;
+	private PersisterSpace rhs;
+
+	public WithFragmentPathResolutionStrategy(ResolutionContext resolutionContext, PersisterSpace lhs) {
+		super( resolutionContext );
+		this.lhs = lhs;
+	}
+
+	@Override
+	protected PathedPropertyReferenceSource internalHandleRoot(PersisterSpace persisterSpace) {
+		rhs = persisterSpace;
+		return super.internalHandleRoot( persisterSpace );
+	}
+
+	@Override
+	protected PathedPropertyReferenceSource internalResolveIntermediatePathPart(PathedPropertyReferenceSource source, String pathPart) {
+		if ( baseRhsPropertyName == null ) {
+			baseRhsPropertyName = pathPart;
+		}
+		return super.internalResolveIntermediatePathPart( source, pathPart );
+	}
+
+	@Override
+	protected HibernateTree internalResolveTerminalPathPart(PathedPropertyReferenceSource source, String pathPart) {
+		if ( baseRhsPropertyName == null ) {
+			baseRhsPropertyName = pathPart;
+		}
+		return super.internalResolveTerminalPathPart( source, pathPart );
+	}
+
+	@Override
+	protected PathedPropertyReferenceSource internalHandleIntermediateIndexAccess(PathedPropertyReferenceSource source, String pathPart, HibernateTree selector) {
+		if ( baseRhsPropertyName == null ) {
+			baseRhsPropertyName = pathPart;
+		}
+		return super.internalHandleIntermediateIndexAccess( source, pathPart, selector );
+	}
+
+	@Override
+	protected HibernateTree internalHandleTerminalIndexAccess(PathedPropertyReferenceSource source, String pathPart, HibernateTree selector) {
+		if ( baseRhsPropertyName == null ) {
+			baseRhsPropertyName = pathPart;
+		}
+		return super.internalHandleTerminalIndexAccess( source, pathPart, selector );
+	}
+
+	public void applyWithFragment(HibernateTree withFragment) {
+		// first, locate the actual Join node which links the lhs and rhs...
+		Table lhsTable = lhs.getTableSpace().getContainingTable( baseRhsPropertyName );
+
+		// todo : implement...
+
+		// as simple as finding the table under lhsTable which contains a join to a table from the table-space associated with
+		// the rhs persister-space???
+
+//		Join join = null;
+//		AST nextPossible = lhs.getFirstChild();
+//		while ( nextPossible != null ) {
+//			if ( nextPossible instanceof Join ) {
+//				if ( ( ( Join ) nextPossible ).locateRhs() == rhs ) {
+//					join = ( Join ) nextPossible;
+//					break;
+//				}
+//			}
+//			nextPossible = nextPossible.getNextSibling();
+//		}
+//		if ( join == null ) {
+//			throw new QueryException( "could not locate specific join node to which to apply with fragment [" + withFragment + "]" );
+//		}
+//		join.addChild( withFragment );
+	}
+
+	protected void validateJoinCreation(PersisterSpace origin, String property) {
+		// todo : why not???
+		throw new HibernateException( "Path expressions [" + origin.getSourceAlias() + "." + property + "] within 'with clause' cannot result in physical join" );
+	}
+}

Modified: core/branches/antlr3/src/main/java/org/hibernate/sql/ast/tree/Table.java
===================================================================
--- core/branches/antlr3/src/main/java/org/hibernate/sql/ast/tree/Table.java	2009-04-21 19:41:54 UTC (rev 16390)
+++ core/branches/antlr3/src/main/java/org/hibernate/sql/ast/tree/Table.java	2009-04-21 20:24:58 UTC (rev 16391)
@@ -180,6 +180,10 @@
 		public void registerReusablePropertyJoinedTable(String propertyName, Table table) {
 			propertyToJoinedTableMap.put( propertyName, table );
 		}
+
+		public boolean contansProperty(String propertyName) {
+			return getPropertyType( propertyName ) != null;
+		}
 	}
 
 	public static class EntityTableSpace extends AbstractTableSpace {




More information about the hibernate-commits mailing list