Skip to content

Commit

Permalink
Merge remote branch 'remotes/origin/nh-2500'
Browse files Browse the repository at this point in the history
  • Loading branch information
ayende committed Jan 29, 2012
2 parents 93030bc + 6ebd3e8 commit 3305470
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 1 deletion.
13 changes: 13 additions & 0 deletions src/NHibernate.Test/Linq/ByMethod/OrderByTests.cs
Expand Up @@ -6,6 +6,19 @@ namespace NHibernate.Test.Linq.ByMethod
[TestFixture]
public class OrderByTests : LinqTestCase
{
[Test]
public void GroupByThenOrderBy()
{
var query = from c in db.Customers
group c by c.Address.Country into g
orderby g.Key
select new { Country = g.Key, Count = g.Count() };

var ids = query.ToList();
Assert.NotNull(ids);
AssertOrderedBy.Ascending(ids, arg => arg.Country);
}

[Test]
public void AscendingOrderByClause()
{
Expand Down
96 changes: 96 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/NH2500/Fixture.cs
@@ -0,0 +1,96 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using NHibernate.Cfg.MappingSchema;
using NHibernate.Linq;
using NHibernate.Mapping.ByCode;

using NUnit.Framework;

namespace NHibernate.Test.NHSpecificTest.NH2500
{
public class Foo
{
public virtual Guid Id { get; set; }

public virtual string Name { get; set; }
}


[TestFixture]
public class Fixture : TestCaseMappingByCode
{
protected override HbmMapping GetMappings()
{
var mapper = new ConventionModelMapper();
mapper.BeforeMapClass += (mi, t, x) => x.Id(map => map.Generator(Generators.Guid));
return mapper.CompileMappingFor(new[] { typeof(Foo) });
}

protected override void OnSetUp()
{
base.OnSetUp();

using (ISession session = Sfi.OpenSession())
using (ITransaction transaction = session.BeginTransaction())
{
session.Persist(new Foo { Name = "Banana" });
session.Persist(new Foo { Name = "Egg" });
transaction.Commit();
}
}

protected override void OnTearDown()
{
using (ISession session = Sfi.OpenSession())
using (ITransaction transaction = session.BeginTransaction())
{
session.CreateQuery("delete from Foo").ExecuteUpdate();
transaction.Commit();
}

base.OnTearDown();
}

private int count;

[Test]
public void TestLinqProjectionExpressionDoesntCacheParameters()
{
using (ISession session = Sfi.OpenSession())
using (ITransaction transaction = session.BeginTransaction())
{
this.count = 1;

var foos1 = session.Query<Foo>()
.Where(x => x.Name == "Banana")
.Select(x => new
{
x.Name,
count,
User = "abc"
}).First();

this.count = 2;

var foos2 = session.Query<Foo>()
.Where(x => x.Name == "Egg")
.Select(x => new
{
x.Name,
count,
User = "def"
}).First();

Assert.AreEqual(1, foos1.count);
Assert.AreEqual(2, foos2.count);
Assert.AreEqual("abc", foos1.User);
Assert.AreEqual("def", foos2.User);

transaction.Commit();
}
}
}
}
1 change: 1 addition & 0 deletions src/NHibernate.Test/NHibernate.Test.csproj
Expand Up @@ -653,6 +653,7 @@
<Compile Include="NHSpecificTest\AccessAndCorrectPropertyName\Model.cs" />
<Compile Include="NHSpecificTest\BagWithLazyExtraAndFilter\Domain.cs" />
<Compile Include="NHSpecificTest\BagWithLazyExtraAndFilter\Fixture.cs" />
<Compile Include="NHSpecificTest\NH2500\Fixture.cs" />
<Compile Include="NHSpecificTest\NH2914\Fixture.cs" />
<Compile Include="NHSpecificTest\NH2914\Model.cs" />
<Compile Include="NHSpecificTest\NH2976\Employee.cs" />
Expand Down
20 changes: 19 additions & 1 deletion src/NHibernate/Linq/Visitors/ExpressionKeyVisitor.cs
Expand Up @@ -78,7 +78,7 @@ protected override Expression VisitConstantExpression(ConstantExpression express
{
NamedParameter param;

if (_constantToParameterMap.TryGetValue(expression, out param))
if (_constantToParameterMap.TryGetValue(expression, out param) && insideSelectClause == false)
{
// Nulls generate different query plans. X = variable generates a different query depending on if variable is null or not.
if (param.Value == null)
Expand Down Expand Up @@ -169,15 +169,33 @@ protected override MemberBinding VisitMemberMemberBinding(MemberMemberBinding bi
return base.VisitMemberMemberBinding(binding);
}

private bool insideSelectClause;
protected override Expression VisitMethodCallExpression(MethodCallExpression expression)
{
var old = insideSelectClause;

switch (expression.Method.Name)
{
case "First":
case "FirstOrDefault":
case "Single":
case "SingleOrDefault":
case "Select":
insideSelectClause = true;
break;
default:
insideSelectClause = false;
break;
}

VisitExpression(expression.Object);
_string.Append('.');
VisitMethod(expression.Method);
_string.Append('(');
VisitList(expression.Arguments, AppendCommas);
_string.Append(')');

insideSelectClause = old;
return expression;
}

Expand Down

0 comments on commit 3305470

Please sign in to comment.