Skip to content

Commit ac871b3

Browse files
leonardlabathazzik
authored andcommitted
Add support for Convert.ToInt32 / int.Parse
Also support for decimal and double. NH-2865 : Test case added NH-3377 : Test case added
1 parent 14a0f27 commit ac871b3

File tree

7 files changed

+271
-0
lines changed

7 files changed

+271
-0
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using System;
2+
3+
namespace NHibernate.Test.NHSpecificTest.NH2865
4+
{
5+
class OrderLine
6+
{
7+
public virtual Guid Id { get; set; }
8+
public virtual string Count { get; set; }
9+
}
10+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using System.Linq;
2+
using NHibernate.Linq;
3+
using NUnit.Framework;
4+
using System;
5+
6+
namespace NHibernate.Test.NHSpecificTest.NH2865
7+
{
8+
[TestFixture]
9+
public class Fixture : BugTestCase
10+
{
11+
protected override void OnSetUp()
12+
{
13+
using (ISession session = OpenSession())
14+
using (ITransaction transaction = session.BeginTransaction())
15+
{
16+
var e1 = new OrderLine {Count = "2"};
17+
session.Save(e1);
18+
19+
var e2 = new OrderLine { Count = "3" };
20+
session.Save(e2);
21+
22+
session.Flush();
23+
transaction.Commit();
24+
}
25+
}
26+
27+
protected override void OnTearDown()
28+
{
29+
using (ISession session = OpenSession())
30+
using (ITransaction transaction = session.BeginTransaction())
31+
{
32+
session.Delete("from System.Object");
33+
34+
session.Flush();
35+
transaction.Commit();
36+
}
37+
}
38+
39+
[Test]
40+
public void UsingConvertToInt32InSumExpressionShouldNotThrowException()
41+
{
42+
using (ISession session = OpenSession())
43+
using (session.BeginTransaction())
44+
{
45+
var result = session.Query<OrderLine>().Sum(l => int.Parse(l.Count));
46+
47+
Assert.AreEqual(5, result);
48+
}
49+
}
50+
}
51+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="NHibernate.Test" namespace="NHibernate.Test.NHSpecificTest.NH2865">
3+
4+
<class name="OrderLine">
5+
<id name="Id" generator="guid.comb" />
6+
<property name="Count" />
7+
</class>
8+
9+
</hibernate-mapping>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System;
2+
3+
namespace NHibernate.Test.NHSpecificTest.NH3377
4+
{
5+
class Entity
6+
{
7+
public virtual Guid Id { get; set; }
8+
public virtual string Name { get; set; }
9+
public virtual string Age { get; set; }
10+
public virtual string Solde { get; set; }
11+
}
12+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
using System.Linq;
2+
using NHibernate.Linq;
3+
using NUnit.Framework;
4+
using System;
5+
6+
namespace NHibernate.Test.NHSpecificTest.NH3377
7+
{
8+
[TestFixture]
9+
public class Fixture : BugTestCase
10+
{
11+
protected override void OnSetUp()
12+
{
13+
using (ISession session = OpenSession())
14+
using (ITransaction transaction = session.BeginTransaction())
15+
{
16+
var e1 = new Entity
17+
{
18+
Name = "Bob",
19+
Age = "17",
20+
Solde = "5.4"
21+
};
22+
session.Save(e1);
23+
24+
var e2 = new Entity
25+
{
26+
Name = "Sally",
27+
Age = "16"
28+
};
29+
session.Save(e2);
30+
31+
session.Flush();
32+
transaction.Commit();
33+
}
34+
}
35+
36+
protected override void OnTearDown()
37+
{
38+
using (ISession session = OpenSession())
39+
using (ITransaction transaction = session.BeginTransaction())
40+
{
41+
session.Delete("from System.Object");
42+
43+
session.Flush();
44+
transaction.Commit();
45+
}
46+
}
47+
48+
[Test]
49+
public void ShouldBeAbleToCallConvertToInt32FromStringParameter()
50+
{
51+
using (ISession session = OpenSession())
52+
using (session.BeginTransaction())
53+
{
54+
var result = from e in session.Query<Entity>()
55+
where e.Name == "Bob"
56+
select Convert.ToInt32(e.Age);
57+
58+
Assert.AreEqual(1, result.Count());
59+
Assert.AreEqual(17, result.First());
60+
}
61+
}
62+
63+
[Test]
64+
public void ShouldBeAbleToCallConvertToInt32FromStringParameterInMax()
65+
{
66+
using (ISession session = OpenSession())
67+
using (session.BeginTransaction())
68+
{
69+
var result = session.Query<Entity>().Max(e => Convert.ToInt32(e.Age));
70+
71+
Assert.AreEqual(17, result);
72+
}
73+
}
74+
}
75+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="NHibernate.Test" namespace="NHibernate.Test.NHSpecificTest.NH3377">
3+
4+
<class name="Entity">
5+
<id name="Id" generator="guid.comb" />
6+
<property name="Name" />
7+
<property name="Age" />
8+
<property name="Solde" />
9+
</class>
10+
11+
</hibernate-mapping>
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Linq.Expressions;
5+
using System.Reflection;
6+
using System.Text;
7+
using NHibernate.Hql.Ast;
8+
using NHibernate.Linq.Visitors;
9+
using System.Collections.ObjectModel;
10+
11+
namespace NHibernate.Linq.Functions
12+
{
13+
public class ConvertToInt32Generator : BaseHqlGeneratorForMethod
14+
{
15+
public ConvertToInt32Generator()
16+
{
17+
SupportedMethods = new[]
18+
{
19+
ReflectionHelper.GetMethodDefinition<string>(s => int.Parse(s)),
20+
ReflectionHelper.GetMethodDefinition<bool>(o => Convert.ToInt32(o)),
21+
ReflectionHelper.GetMethodDefinition<byte>(o => Convert.ToInt32(o)),
22+
ReflectionHelper.GetMethodDefinition<char>(o => Convert.ToInt32(o)),
23+
ReflectionHelper.GetMethodDefinition<decimal>(o => Convert.ToInt32(o)),
24+
ReflectionHelper.GetMethodDefinition<double>(o => Convert.ToInt32(o)),
25+
ReflectionHelper.GetMethodDefinition<float>(o => Convert.ToInt32(o)),
26+
ReflectionHelper.GetMethodDefinition<int>(o => Convert.ToInt32(o)),
27+
ReflectionHelper.GetMethodDefinition<long>(o => Convert.ToInt32(o)),
28+
ReflectionHelper.GetMethodDefinition<object>(o => Convert.ToInt32(o)),
29+
ReflectionHelper.GetMethodDefinition<sbyte>(o => Convert.ToInt32(o)),
30+
ReflectionHelper.GetMethodDefinition<short>(o => Convert.ToInt32(o)),
31+
ReflectionHelper.GetMethodDefinition<string>(o => Convert.ToInt32(o)),
32+
ReflectionHelper.GetMethodDefinition<uint>(o => Convert.ToInt32(o)),
33+
ReflectionHelper.GetMethodDefinition<ulong>(o => Convert.ToInt32(o)),
34+
ReflectionHelper.GetMethodDefinition<ushort>(o => Convert.ToInt32(o))
35+
};
36+
}
37+
38+
public override HqlTreeNode BuildHql(MethodInfo method, Expression targetObject, ReadOnlyCollection<Expression> arguments, HqlTreeBuilder treeBuilder, IHqlExpressionVisitor visitor)
39+
{
40+
return treeBuilder.Cast(visitor.Visit(arguments[0]).AsExpression(), typeof (int));
41+
}
42+
}
43+
44+
public class ConvertToDecimalGenerator : BaseHqlGeneratorForMethod
45+
{
46+
public ConvertToDecimalGenerator()
47+
{
48+
SupportedMethods = new[]
49+
{
50+
ReflectionHelper.GetMethodDefinition<string>(s => decimal.Parse(s)),
51+
ReflectionHelper.GetMethodDefinition<bool>(o => Convert.ToDecimal(o)),
52+
ReflectionHelper.GetMethodDefinition<byte>(o => Convert.ToDecimal(o)),
53+
ReflectionHelper.GetMethodDefinition<decimal>(o => Convert.ToDecimal(o)),
54+
ReflectionHelper.GetMethodDefinition<double>(o => Convert.ToDecimal(o)),
55+
ReflectionHelper.GetMethodDefinition<float>(o => Convert.ToDecimal(o)),
56+
ReflectionHelper.GetMethodDefinition<int>(o => Convert.ToDecimal(o)),
57+
ReflectionHelper.GetMethodDefinition<long>(o => Convert.ToDecimal(o)),
58+
ReflectionHelper.GetMethodDefinition<object>(o => Convert.ToDecimal(o)),
59+
ReflectionHelper.GetMethodDefinition<sbyte>(o => Convert.ToDecimal(o)),
60+
ReflectionHelper.GetMethodDefinition<short>(o => Convert.ToDecimal(o)),
61+
ReflectionHelper.GetMethodDefinition<string>(o => Convert.ToDecimal(o)),
62+
ReflectionHelper.GetMethodDefinition<uint>(o => Convert.ToDecimal(o)),
63+
ReflectionHelper.GetMethodDefinition<ulong>(o => Convert.ToDecimal(o)),
64+
ReflectionHelper.GetMethodDefinition<ushort>(o => Convert.ToDecimal(o))
65+
};
66+
}
67+
68+
public override HqlTreeNode BuildHql(MethodInfo method, Expression targetObject, ReadOnlyCollection<Expression> arguments, HqlTreeBuilder treeBuilder, IHqlExpressionVisitor visitor)
69+
{
70+
return treeBuilder.Cast(visitor.Visit(arguments[0]).AsExpression(), typeof(decimal));
71+
}
72+
}
73+
74+
public class ConvertToDoubleGenerator : BaseHqlGeneratorForMethod
75+
{
76+
public ConvertToDoubleGenerator()
77+
{
78+
SupportedMethods = new[]
79+
{
80+
ReflectionHelper.GetMethodDefinition<string>(s => double.Parse(s)),
81+
ReflectionHelper.GetMethodDefinition<bool>(o => Convert.ToDouble(o)),
82+
ReflectionHelper.GetMethodDefinition<byte>(o => Convert.ToDouble(o)),
83+
ReflectionHelper.GetMethodDefinition<decimal>(o => Convert.ToDouble(o)),
84+
ReflectionHelper.GetMethodDefinition<double>(o => Convert.ToDouble(o)),
85+
ReflectionHelper.GetMethodDefinition<float>(o => Convert.ToDouble(o)),
86+
ReflectionHelper.GetMethodDefinition<int>(o => Convert.ToDouble(o)),
87+
ReflectionHelper.GetMethodDefinition<long>(o => Convert.ToDouble(o)),
88+
ReflectionHelper.GetMethodDefinition<object>(o => Convert.ToDouble(o)),
89+
ReflectionHelper.GetMethodDefinition<sbyte>(o => Convert.ToDouble(o)),
90+
ReflectionHelper.GetMethodDefinition<short>(o => Convert.ToDouble(o)),
91+
ReflectionHelper.GetMethodDefinition<string>(o => Convert.ToDouble(o)),
92+
ReflectionHelper.GetMethodDefinition<uint>(o => Convert.ToDouble(o)),
93+
ReflectionHelper.GetMethodDefinition<ulong>(o => Convert.ToDouble(o)),
94+
ReflectionHelper.GetMethodDefinition<ushort>(o => Convert.ToDouble(o))
95+
};
96+
}
97+
98+
public override HqlTreeNode BuildHql(MethodInfo method, Expression targetObject, ReadOnlyCollection<Expression> arguments, HqlTreeBuilder treeBuilder, IHqlExpressionVisitor visitor)
99+
{
100+
return treeBuilder.Cast(visitor.Visit(arguments[0]).AsExpression(), typeof(double));
101+
}
102+
}
103+
}

0 commit comments

Comments
 (0)