|
From: <ric...@us...> - 2009-07-27 14:41:49
|
Revision: 4663
http://nhibernate.svn.sourceforge.net/nhibernate/?rev=4663&view=rev
Author: ricbrown
Date: 2009-07-27 14:41:38 +0000 (Mon, 27 Jul 2009)
Log Message:
-----------
Added integration test for IQueryOver projections.
Modified Paths:
--------------
trunk/nhibernate/src/NHibernate/Criterion/QueryOver.cs
trunk/nhibernate/src/NHibernate/IQueryOver.cs
trunk/nhibernate/src/NHibernate/ISession.cs
trunk/nhibernate/src/NHibernate/Impl/SessionImpl.cs
trunk/nhibernate/src/NHibernate.Test/Criteria/Lambda/IntegrationFixture.cs
Modified: trunk/nhibernate/src/NHibernate/Criterion/QueryOver.cs
===================================================================
--- trunk/nhibernate/src/NHibernate/Criterion/QueryOver.cs 2009-07-27 10:49:18 UTC (rev 4662)
+++ trunk/nhibernate/src/NHibernate/Criterion/QueryOver.cs 2009-07-27 14:41:38 UTC (rev 4663)
@@ -198,6 +198,11 @@
return _criteria.List<T>();
}
+ public IList<U> List<U>()
+ {
+ return _criteria.List<U>();
+ }
+
/// <summary>
/// Get an executable instance of <c>IQueryOver<T></c>,
/// to actually run the query.</summary>
@@ -310,6 +315,9 @@
IList<T> IQueryOver<T>.List()
{ return List(); }
+ IList<U> IQueryOver<T>.List<U>()
+ { return List<U>(); }
+
}
}
Modified: trunk/nhibernate/src/NHibernate/IQueryOver.cs
===================================================================
--- trunk/nhibernate/src/NHibernate/IQueryOver.cs 2009-07-27 10:49:18 UTC (rev 4662)
+++ trunk/nhibernate/src/NHibernate/IQueryOver.cs 2009-07-27 14:41:38 UTC (rev 4663)
@@ -195,6 +195,12 @@
/// <returns>The list filled with the results.</returns>
IList<T> List();
+ /// <summary>
+ /// Get the results of the root type and fill the <see cref="IList<T>"/>
+ /// </summary>
+ /// <returns>The list filled with the results.</returns>
+ IList<U> List<U>();
+
}
}
Modified: trunk/nhibernate/src/NHibernate/ISession.cs
===================================================================
--- trunk/nhibernate/src/NHibernate/ISession.cs 2009-07-27 10:49:18 UTC (rev 4662)
+++ trunk/nhibernate/src/NHibernate/ISession.cs 2009-07-27 14:41:38 UTC (rev 4663)
@@ -1,6 +1,7 @@
using System;
using System.Collections;
using System.Data;
+using System.Linq.Expressions;
using NHibernate.Engine;
using NHibernate.Stat;
using NHibernate.Type;
@@ -783,13 +784,20 @@
ICriteria CreateCriteria(string entityName, string alias);
/// <summary>
- /// Creates a new <c>ICriteria<T></c> for the entity class.
+ /// Creates a new <c>IQueryOver<T></c> for the entity class.
/// </summary>
/// <typeparam name="T">The entity class</typeparam>
/// <returns>An ICriteria<T> object</returns>
IQueryOver<T> QueryOver<T>() where T : class;
/// <summary>
+ /// Creates a new <c>IQueryOver<T></c> for the entity class.
+ /// </summary>
+ /// <typeparam name="T">The entity class</typeparam>
+ /// <returns>An ICriteria<T> object</returns>
+ IQueryOver<T> QueryOver<T>(Expression<Func<T>> alias) where T : class;
+
+ /// <summary>
/// Create a new instance of <c>Query</c> for the given query string
/// </summary>
/// <param name="queryString">A hibernate query string</param>
Modified: trunk/nhibernate/src/NHibernate/Impl/SessionImpl.cs
===================================================================
--- trunk/nhibernate/src/NHibernate/Impl/SessionImpl.cs 2009-07-27 10:49:18 UTC (rev 4662)
+++ trunk/nhibernate/src/NHibernate/Impl/SessionImpl.cs 2009-07-27 14:41:38 UTC (rev 4663)
@@ -2,6 +2,7 @@
using System.Collections;
using System.Collections.Generic;
using System.Data;
+using System.Linq.Expressions;
using System.Runtime.Serialization;
using System.Security.Permissions;
using Iesi.Collections;
@@ -1881,6 +1882,16 @@
}
}
+ public IQueryOver<T> QueryOver<T>(Expression<Func<T>> alias) where T : class
+ {
+ using (new SessionIdLoggingContext(SessionId))
+ {
+ CheckAndUpdateSessionStatus();
+ string aliasPath = ExpressionProcessor.FindMemberExpression(alias.Body);
+ return new QueryOver<T>(new CriteriaImpl(typeof(T), aliasPath, this));
+ }
+ }
+
public override IList List(CriteriaImpl criteria)
{
using (new SessionIdLoggingContext(SessionId))
Modified: trunk/nhibernate/src/NHibernate.Test/Criteria/Lambda/IntegrationFixture.cs
===================================================================
--- trunk/nhibernate/src/NHibernate.Test/Criteria/Lambda/IntegrationFixture.cs 2009-07-27 10:49:18 UTC (rev 4662)
+++ trunk/nhibernate/src/NHibernate.Test/Criteria/Lambda/IntegrationFixture.cs 2009-07-27 14:41:38 UTC (rev 4663)
@@ -1,6 +1,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
+using System.Linq;
using NUnit.Framework;
@@ -91,6 +92,59 @@
}
}
+ [Test]
+ public void Project_SingleProperty()
+ {
+ using (ISession s = OpenSession())
+ using (ITransaction t = s.BeginTransaction())
+ {
+ s.Save(new Person() { Name = "test person 1", Age = 20 });
+ s.Save(new Person() { Name = "test person 2", Age = 30 });
+ t.Commit();
+ }
+
+ using (ISession s = OpenSession())
+ {
+ var actual =
+ s.QueryOver<Person>()
+ .Select(p => p.Age)
+ .OrderBy(p => p.Age, Order.Asc)
+ .List<int>();
+
+ Assert.That(actual[0], Is.EqualTo(20));
+ }
+ }
+
+ [Test]
+ public void Project_MultipleProperties()
+ {
+ using (ISession s = OpenSession())
+ using (ITransaction t = s.BeginTransaction())
+ {
+ s.Save(new Person() { Name = "test person 1", Age = 20 });
+ s.Save(new Person() { Name = "test person 2", Age = 30 });
+ t.Commit();
+ }
+
+ using (ISession s = OpenSession())
+ {
+ Person personAlias = null;
+ var actual =
+ s.QueryOver<Person>(() => personAlias)
+ .Select(p => p.Name,
+ p => personAlias.Age)
+ .OrderBy(p => p.Age, Order.Asc)
+ .List<object[]>()
+ .Select(props => new {
+ TestName = (string)props[0],
+ TestAge = (int)props[1],
+ });
+
+ Assert.That(actual.ElementAt(0).TestName, Is.EqualTo("test person 1"));
+ Assert.That(actual.ElementAt(1).TestAge, Is.EqualTo(30));
+ }
+ }
+
}
}
\ No newline at end of file
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|