From: <ric...@us...> - 2009-07-08 17:07:08
|
Revision: 4592 http://nhibernate.svn.sourceforge.net/nhibernate/?rev=4592&view=rev Author: ricbrown Date: 2009-07-08 17:07:02 +0000 (Wed, 08 Jul 2009) Log Message: ----------- Added overloads for creating sub-criteria with alias and join types. Modified Paths: -------------- trunk/nhibernate/src/NHibernate/Criterion/QueryOver.cs trunk/nhibernate/src/NHibernate/Criterion/QueryOverJoinBuilder.cs trunk/nhibernate/src/NHibernate/IQueryOver.cs trunk/nhibernate/src/NHibernate.Test/Criteria/Lambda/LambdaFixtureBase.cs trunk/nhibernate/src/NHibernate.Test/Criteria/Lambda/Model.cs trunk/nhibernate/src/NHibernate.Test/Criteria/Lambda/QueryOverFixture.cs Modified: trunk/nhibernate/src/NHibernate/Criterion/QueryOver.cs =================================================================== --- trunk/nhibernate/src/NHibernate/Criterion/QueryOver.cs 2009-07-08 15:34:48 UTC (rev 4591) +++ trunk/nhibernate/src/NHibernate/Criterion/QueryOver.cs 2009-07-08 17:07:02 UTC (rev 4592) @@ -89,6 +89,39 @@ ExpressionProcessor.FindMemberExpression(path.Body))); } + public QueryOver<U> JoinQueryOver<U>(Expression<Func<T, U>> path, Expression<Func<U>> alias) + { + return new QueryOver<U>(_impl, + _criteria.CreateCriteria( + ExpressionProcessor.FindMemberExpression(path.Body), + ExpressionProcessor.FindMemberExpression(alias.Body))); + } + + public QueryOver<U> JoinQueryOver<U>(Expression<Func<T, U>> path, JoinType joinType) + { + return new QueryOver<U>(_impl, + _criteria.CreateCriteria( + ExpressionProcessor.FindMemberExpression(path.Body), + joinType)); + } + + public QueryOver<U> JoinQueryOver<U>(Expression<Func<T, U>> path, Expression<Func<U>> alias, JoinType joinType) + { + return new QueryOver<U>(_impl, + _criteria.CreateCriteria( + ExpressionProcessor.FindMemberExpression(path.Body), + ExpressionProcessor.FindMemberExpression(alias.Body), + joinType)); + } + + public QueryOver<U> JoinQueryOver<U>(Expression<Func<T, IEnumerable<U>>> path, Expression<Func<U>> alias) + { + return new QueryOver<U>(_impl, + _criteria.CreateCriteria( + ExpressionProcessor.FindMemberExpression(path.Body), + ExpressionProcessor.FindMemberExpression(alias.Body))); + } + public QueryOver<U> JoinQueryOver<U>(Expression<Func<T, IEnumerable<U>>> path) { return new QueryOver<U>(_impl, @@ -96,6 +129,23 @@ ExpressionProcessor.FindMemberExpression(path.Body))); } + public QueryOver<U> JoinQueryOver<U>(Expression<Func<T, IEnumerable<U>>> path, Expression<Func<U>> alias, JoinType joinType) + { + return new QueryOver<U>(_impl, + _criteria.CreateCriteria( + ExpressionProcessor.FindMemberExpression(path.Body), + ExpressionProcessor.FindMemberExpression(alias.Body), + joinType)); + } + + public QueryOver<U> JoinQueryOver<U>(Expression<Func<T, IEnumerable<U>>> path, JoinType joinType) + { + return new QueryOver<U>(_impl, + _criteria.CreateCriteria( + ExpressionProcessor.FindMemberExpression(path.Body), + joinType)); + } + public QueryOver<T> Join(Expression<Func<T, object>> path, Expression<Func<object>> alias) { return AddAlias( @@ -204,9 +254,27 @@ IQueryOver<U> IQueryOver<T>.JoinQueryOver<U>(Expression<Func<T, U>> path) { return JoinQueryOver(path); } + IQueryOver<U> IQueryOver<T>.JoinQueryOver<U>(Expression<Func<T, U>> path, Expression<Func<U>> alias) + { return JoinQueryOver(path, alias); } + + IQueryOver<U> IQueryOver<T>.JoinQueryOver<U>(Expression<Func<T, U>> path, JoinType joinType) + { return JoinQueryOver(path, joinType); } + + IQueryOver<U> IQueryOver<T>.JoinQueryOver<U>(Expression<Func<T, U>> path, Expression<Func<U>> alias, JoinType joinType) + { return JoinQueryOver(path, alias, joinType); } + IQueryOver<U> IQueryOver<T>.JoinQueryOver<U>(Expression<Func<T, IEnumerable<U>>> path) { return JoinQueryOver(path); } + IQueryOver<U> IQueryOver<T>.JoinQueryOver<U>(Expression<Func<T, IEnumerable<U>>> path, Expression<Func<U>> alias) + { return JoinQueryOver(path, alias); } + + IQueryOver<U> IQueryOver<T>.JoinQueryOver<U>(Expression<Func<T, IEnumerable<U>>> path, JoinType joinType) + { return JoinQueryOver(path, joinType); } + + IQueryOver<U> IQueryOver<T>.JoinQueryOver<U>(Expression<Func<T, IEnumerable<U>>> path, Expression<Func<U>> alias, JoinType joinType) + { return JoinQueryOver(path, alias, joinType); } + IQueryOver<T> IQueryOver<T>.Join(Expression<Func<T, object>> path, Expression<Func<object>> alias) { return Join(path, alias); } Modified: trunk/nhibernate/src/NHibernate/Criterion/QueryOverJoinBuilder.cs =================================================================== --- trunk/nhibernate/src/NHibernate/Criterion/QueryOverJoinBuilder.cs 2009-07-08 15:34:48 UTC (rev 4591) +++ trunk/nhibernate/src/NHibernate/Criterion/QueryOverJoinBuilder.cs 2009-07-08 17:07:02 UTC (rev 4592) @@ -12,28 +12,70 @@ public class QueryOverJoinBuilder<T> : QueryOverJoinBuilderBase<QueryOver<T>, T> { public QueryOverJoinBuilder(QueryOver<T> root, JoinType joinType) : base(root, joinType) { } + + public QueryOver<U> JoinQueryOver<U>(Expression<Func<T, U>> path) + { + return root.JoinQueryOver<U>(path, joinType); + } + + public QueryOver<U> JoinQueryOver<U>(Expression<Func<T, U>> path, Expression<Func<U>> alias) + { + return root.JoinQueryOver<U>(path, alias, joinType); + } + + public QueryOver<U> JoinQueryOver<U>(Expression<Func<T, IEnumerable<U>>> path) + { + return root.JoinQueryOver<U>(path, joinType); + } + + public QueryOver<U> JoinQueryOver<U>(Expression<Func<T, IEnumerable<U>>> path, Expression<Func<U>> alias) + { + return root.JoinQueryOver<U>(path, alias, joinType); + } + } public class IQueryOverJoinBuilder<T> : QueryOverJoinBuilderBase<IQueryOver<T>, T> { public IQueryOverJoinBuilder(IQueryOver<T> root, JoinType joinType) : base(root, joinType) { } + + public IQueryOver<U> JoinQueryOver<U>(Expression<Func<T, U>> path) + { + return root.JoinQueryOver<U>(path, joinType); + } + + public IQueryOver<U> JoinQueryOver<U>(Expression<Func<T, U>> path, Expression<Func<U>> alias) + { + return root.JoinQueryOver<U>(path, alias, joinType); + } + + public IQueryOver<U> JoinQueryOver<U>(Expression<Func<T, IEnumerable<U>>> path) + { + return root.JoinQueryOver<U>(path, joinType); + } + + public IQueryOver<U> JoinQueryOver<U>(Expression<Func<T, IEnumerable<U>>> path, Expression<Func<U>> alias) + { + return root.JoinQueryOver<U>(path, alias, joinType); + } + } public class QueryOverJoinBuilderBase<R, T> where R : IQueryOver<T> { - private R _root; - private JoinType _joinType; + protected R root; + protected JoinType joinType; public QueryOverJoinBuilderBase(R root, JoinType joinType) { - _root = root; - _joinType = joinType; + this.root = root; + this.joinType = joinType; } public R Join(Expression<Func<T, object>> path, Expression<Func<object>> alias) { - return (R)_root.Join(path, alias, _joinType); + return (R)root.Join(path, alias, joinType); } } Modified: trunk/nhibernate/src/NHibernate/IQueryOver.cs =================================================================== --- trunk/nhibernate/src/NHibernate/IQueryOver.cs 2009-07-08 15:34:48 UTC (rev 4591) +++ trunk/nhibernate/src/NHibernate/IQueryOver.cs 2009-07-08 17:07:02 UTC (rev 4592) @@ -98,6 +98,32 @@ /// <summary> /// Creates a new NHibernate.ICriteria<T>, "rooted" at the associated entity + /// </summary> + /// <typeparam name="U">Type of sub-criteria</typeparam> + /// <param name="path">Lambda expression returning association path</param> + /// <param name="alias">Lambda expression returning alias reference</param> + /// <returns>The created "sub criteria"</returns> + IQueryOver<U> JoinQueryOver<U>(Expression<Func<T, U>> path, Expression<Func<U>> alias); + + /// <summary> + /// Creates a new NHibernate.ICriteria<T>, "rooted" at the associated entity + /// </summary> + /// <typeparam name="U">Type of sub-criteria</typeparam> + /// <param name="path">Lambda expression returning association path</param> + /// <returns>The created "sub criteria"</returns> + IQueryOver<U> JoinQueryOver<U>(Expression<Func<T, U>> path, JoinType joinType); + + /// <summary> + /// Creates a new NHibernate.ICriteria<T>, "rooted" at the associated entity + /// </summary> + /// <typeparam name="U">Type of sub-criteria</typeparam> + /// <param name="path">Lambda expression returning association path</param> + /// <param name="alias">Lambda expression returning alias reference</param> + /// <returns>The created "sub criteria"</returns> + IQueryOver<U> JoinQueryOver<U>(Expression<Func<T, U>> path, Expression<Func<U>> alias, JoinType joinType); + + /// <summary> + /// Creates a new NHibernate.ICriteria<T>, "rooted" at the associated entity /// specifying a collection for the join. /// </summary> /// <typeparam name="U">Type of sub-criteria (type of the collection)</typeparam> @@ -106,6 +132,35 @@ IQueryOver<U> JoinQueryOver<U>(Expression<Func<T, IEnumerable<U>>> path); /// <summary> + /// Creates a new NHibernate.ICriteria<T>, "rooted" at the associated entity + /// specifying a collection for the join. + /// </summary> + /// <typeparam name="U">Type of sub-criteria (type of the collection)</typeparam> + /// <param name="path">Lambda expression returning association path</param> + /// <param name="alias">Lambda expression returning alias reference</param> + /// <returns>The created "sub criteria"</returns> + IQueryOver<U> JoinQueryOver<U>(Expression<Func<T, IEnumerable<U>>> path, Expression<Func<U>> alias); + + /// <summary> + /// Creates a new NHibernate.ICriteria<T>, "rooted" at the associated entity + /// specifying a collection for the join. + /// </summary> + /// <typeparam name="U">Type of sub-criteria (type of the collection)</typeparam> + /// <param name="path">Lambda expression returning association path</param> + /// <returns>The created "sub criteria"</returns> + IQueryOver<U> JoinQueryOver<U>(Expression<Func<T, IEnumerable<U>>> path, JoinType joinType); + + /// <summary> + /// Creates a new NHibernate.ICriteria<T>, "rooted" at the associated entity + /// specifying a collection for the join. + /// </summary> + /// <typeparam name="U">Type of sub-criteria (type of the collection)</typeparam> + /// <param name="path">Lambda expression returning association path</param> + /// <param name="alias">Lambda expression returning alias reference</param> + /// <returns>The created "sub criteria"</returns> + IQueryOver<U> JoinQueryOver<U>(Expression<Func<T, IEnumerable<U>>> path, Expression<Func<U>> alias, JoinType joinType); + + /// <summary> /// Join an association, assigning an alias to the joined entity /// </summary> /// <param name="path">Lambda expression returning association path</param> Modified: trunk/nhibernate/src/NHibernate.Test/Criteria/Lambda/LambdaFixtureBase.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Test/Criteria/Lambda/LambdaFixtureBase.cs 2009-07-08 15:34:48 UTC (rev 4591) +++ trunk/nhibernate/src/NHibernate.Test/Criteria/Lambda/LambdaFixtureBase.cs 2009-07-08 17:07:02 UTC (rev 4592) @@ -94,7 +94,7 @@ PushName(name); string fieldPath = _fieldPath.Peek(); - if (expected == null) + if (expected == null || actual == null) { Assert.AreEqual(expected, actual, fieldPath); _fieldPath.Pop(); Modified: trunk/nhibernate/src/NHibernate.Test/Criteria/Lambda/Model.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Test/Criteria/Lambda/Model.cs 2009-07-08 15:34:48 UTC (rev 4591) +++ trunk/nhibernate/src/NHibernate.Test/Criteria/Lambda/Model.cs 2009-07-08 17:07:02 UTC (rev 4592) @@ -36,6 +36,8 @@ public virtual IEnumerable<Relation> Collection3 { get; set; } public virtual IEnumerable<Relation> Collection4 { get; set; } + public virtual IEnumerable<Person> People { get; set; } + } } Modified: trunk/nhibernate/src/NHibernate.Test/Criteria/Lambda/QueryOverFixture.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Test/Criteria/Lambda/QueryOverFixture.cs 2009-07-08 15:34:48 UTC (rev 4591) +++ trunk/nhibernate/src/NHibernate.Test/Criteria/Lambda/QueryOverFixture.cs 2009-07-08 17:07:02 UTC (rev 4592) @@ -154,13 +154,43 @@ IQueryOver<Child> actual = CreateTestQueryOver<Person>() - .JoinQueryOver<Child>(p => p.Children) // sub-criteria + .JoinQueryOver(p => p.Children) // sub-criteria .Where(c => c.Nickname == "test name"); AssertCriteriaAreEqual(expected, actual); } [Test] + public void SubCriteria_JoinQueryOverCombinations() + { + ICriteria expected = + CreateTestCriteria(typeof(Relation)) + .CreateCriteria("Related1") + .CreateCriteria("Related2", JoinType.LeftOuterJoin) + .CreateCriteria("Related3", JoinType.RightOuterJoin) + .CreateCriteria("Related4", JoinType.FullJoin) + .CreateCriteria("Collection1", "collection1Alias") + .CreateCriteria("Collection2", "collection2Alias", JoinType.LeftOuterJoin) + .CreateCriteria("Collection3", JoinType.RightOuterJoin) + .CreateCriteria("People", "personAlias", JoinType.FullJoin); + + Relation collection1Alias = null, collection2Alias = null; + Person personAlias = null; + IQueryOver<Person> actual = + CreateTestQueryOver<Relation>() + .Inner.JoinQueryOver(r => r.Related1) + .Left.JoinQueryOver(r => r.Related2) + .Right.JoinQueryOver(r => r.Related3) + .Full.JoinQueryOver(r => r.Related4) + .JoinQueryOver(r => r.Collection1, () => collection1Alias) + .Left.JoinQueryOver(r => r.Collection2, () => collection2Alias) + .Right.JoinQueryOver(r => r.Collection3) + .Full.JoinQueryOver(r => r.People, () => personAlias); + + AssertCriteriaAreEqual(expected, actual); + } + + [Test] public void Alias_Join() { ICriteria expected = This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |