Skip to content

Commit e187e95

Browse files
committed
NH-3237 - Port-back removing cast operator in max and min aggregates
1 parent e786c94 commit e187e95

File tree

6 files changed

+510
-12
lines changed

6 files changed

+510
-12
lines changed
Lines changed: 223 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,223 @@
1+
using System.Linq;
2+
using NHibernate.Cfg.MappingSchema;
3+
using NHibernate.Linq;
4+
using NHibernate.Mapping.ByCode;
5+
using NUnit.Framework;
6+
using System;
7+
8+
namespace NHibernate.Test.NHSpecificTest.NH3237
9+
{
10+
[TestFixture]
11+
public class ByCodeFixture : TestCaseMappingByCode
12+
{
13+
protected override HbmMapping GetMappings()
14+
{
15+
var mapper = new ModelMapper();
16+
mapper.Class<Entity>(rc =>
17+
{
18+
rc.Id(x => x.Id, m => m.Generator(Generators.GuidComb));
19+
rc.Property(x => x.DateTimeOffsetValue, m => m.Type(typeof(DateTimeOffsetUserType), new DateTimeOffsetUserType(TimeSpan.FromHours(10))));
20+
rc.Property(x => x.EnumValue, m => m.Type(typeof(EnumUserType), null));
21+
rc.Property(x => x.IntValue);
22+
rc.Property(x => x.LongValue);
23+
rc.Property(x => x.DecimalValue);
24+
rc.Property(x => x.DoubleValue);
25+
rc.Property(x => x.FloatValue);
26+
rc.Property(x => x.DateTimeValue);
27+
rc.Property(x => x.StringValue);
28+
});
29+
30+
return mapper.CompileMappingForAllExplicitlyAddedEntities();
31+
}
32+
33+
protected override void OnSetUp()
34+
{
35+
using (ISession session = OpenSession())
36+
using (ITransaction transaction = session.BeginTransaction())
37+
{
38+
var e1 = new Entity
39+
{
40+
DateTimeOffsetValue = new DateTimeOffset(2012, 08, 06, 11, 0, 0, TimeSpan.FromHours(10)),
41+
EnumValue = TestEnum.Zero,
42+
IntValue = 1,
43+
LongValue = 1L,
44+
DecimalValue = 1.2m,
45+
DoubleValue = 1.2d,
46+
FloatValue = 1.2f,
47+
DateTimeValue = new DateTime(2012, 08, 06, 11, 0, 0),
48+
StringValue = "a"
49+
};
50+
session.Save(e1);
51+
52+
var e2 = new Entity
53+
{
54+
DateTimeOffsetValue = new DateTimeOffset(2012, 08, 06, 12, 0, 0, TimeSpan.FromHours(10)),
55+
EnumValue = TestEnum.One,
56+
IntValue = 2,
57+
LongValue = 2L,
58+
DecimalValue = 2.2m,
59+
DoubleValue = 2.2d,
60+
FloatValue = 2.2f,
61+
DateTimeValue = new DateTime(2012, 08, 06, 12, 0, 0),
62+
StringValue = "b"
63+
};
64+
session.Save(e2);
65+
66+
var e3 = new Entity
67+
{
68+
DateTimeOffsetValue = new DateTimeOffset(2012, 08, 06, 13, 0, 0, TimeSpan.FromHours(10)),
69+
EnumValue = TestEnum.Two,
70+
IntValue = 3,
71+
LongValue = 3L,
72+
DecimalValue = 3.2m,
73+
DoubleValue = 3.2d,
74+
FloatValue = 3.2f,
75+
DateTimeValue = new DateTime(2012, 08, 06, 13, 0, 0),
76+
StringValue = "c"
77+
};
78+
session.Save(e3);
79+
80+
session.Flush();
81+
transaction.Commit();
82+
}
83+
}
84+
85+
protected override void OnTearDown()
86+
{
87+
using (ISession session = OpenSession())
88+
using (ITransaction transaction = session.BeginTransaction())
89+
{
90+
session.Delete("from System.Object");
91+
92+
session.Flush();
93+
transaction.Commit();
94+
}
95+
}
96+
97+
[Test]
98+
public void Test_That_DateTimeOffset_UserType_Can_Be_Used_For_Max_And_Min_Aggregates()
99+
{
100+
using (ISession session = OpenSession())
101+
using (session.BeginTransaction())
102+
{
103+
var min = session.Query<Entity>().Min(e => e.DateTimeOffsetValue);
104+
Assert.AreEqual(new DateTimeOffset(2012, 08, 06, 11, 0, 0, TimeSpan.FromHours(10)), min);
105+
106+
var max = session.Query<Entity>().Max(e => e.DateTimeOffsetValue);
107+
Assert.AreEqual(new DateTimeOffset(2012, 08, 06, 13, 0, 0, TimeSpan.FromHours(10)), max);
108+
}
109+
}
110+
111+
[Test]
112+
public void Test_That_Enum_Type_Can_Be_Used_For_Max_And_Min_Aggregates()
113+
{
114+
using (ISession session = OpenSession())
115+
using (session.BeginTransaction())
116+
{
117+
var min = session.Query<Entity>().Min(e => e.EnumValue);
118+
Assert.AreEqual(TestEnum.Zero, min);
119+
120+
var max = session.Query<Entity>().Max(e => e.EnumValue);
121+
Assert.AreEqual(TestEnum.Two, max);
122+
}
123+
}
124+
125+
[Test]
126+
public void Test_Max_And_Min_Aggregates_Work_For_Ints()
127+
{
128+
using (ISession session = OpenSession())
129+
using (session.BeginTransaction())
130+
{
131+
var min = session.Query<Entity>().Min(e => e.IntValue);
132+
Assert.AreEqual(1, min);
133+
134+
var max = session.Query<Entity>().Max(e => e.IntValue);
135+
Assert.AreEqual(3, max);
136+
}
137+
}
138+
139+
[Test]
140+
public void Test_Max_And_Min_Aggregates_Work_For_Longs()
141+
{
142+
using (ISession session = OpenSession())
143+
using (session.BeginTransaction())
144+
{
145+
var min = session.Query<Entity>().Min(e => e.LongValue);
146+
Assert.AreEqual(1L, min);
147+
148+
var max = session.Query<Entity>().Max(e => e.LongValue);
149+
Assert.AreEqual(3L, max);
150+
}
151+
}
152+
153+
[Test]
154+
public void Test_Max_And_Min_Aggregates_Work_For_Decimals()
155+
{
156+
using (ISession session = OpenSession())
157+
using (session.BeginTransaction())
158+
{
159+
var min = session.Query<Entity>().Min(e => e.DecimalValue);
160+
Assert.AreEqual(1.2m, min);
161+
162+
var max = session.Query<Entity>().Max(e => e.DecimalValue);
163+
Assert.AreEqual(3.2m, max);
164+
}
165+
}
166+
167+
[Test]
168+
public void Test_Max_And_Min_Aggregates_Work_For_Doubles()
169+
{
170+
using (ISession session = OpenSession())
171+
using (session.BeginTransaction())
172+
{
173+
var min = session.Query<Entity>().Min(e => e.DoubleValue);
174+
Assert.AreEqual(1.2d, min);
175+
176+
var max = session.Query<Entity>().Max(e => e.DoubleValue);
177+
Assert.AreEqual(3.2d, max);
178+
}
179+
}
180+
181+
[Test]
182+
public void Test_Max_And_Min_Aggregates_Work_For_Floats()
183+
{
184+
using (ISession session = OpenSession())
185+
using (session.BeginTransaction())
186+
{
187+
var min = session.Query<Entity>().Min(e => e.FloatValue);
188+
Assert.AreEqual(1.2f, min);
189+
190+
var max = session.Query<Entity>().Max(e => e.FloatValue);
191+
Assert.AreEqual(3.2f, max);
192+
}
193+
}
194+
195+
[Test]
196+
public void Test_Max_And_Min_Aggregates_Work_For_DateTimes()
197+
{
198+
using (ISession session = OpenSession())
199+
using (session.BeginTransaction())
200+
{
201+
var min = session.Query<Entity>().Min(e => e.DateTimeValue);
202+
Assert.AreEqual(new DateTime(2012, 08, 06, 11, 0, 0), min);
203+
204+
var max = session.Query<Entity>().Max(e => e.DateTimeValue);
205+
Assert.AreEqual(new DateTime(2012, 08, 06, 13, 0, 0), max);
206+
}
207+
}
208+
209+
[Test]
210+
public void Test_Max_And_Min_Aggregates_Work_For_Strings()
211+
{
212+
using (ISession session = OpenSession())
213+
using (session.BeginTransaction())
214+
{
215+
var min = session.Query<Entity>().Min(e => e.StringValue);
216+
Assert.AreEqual("a", min);
217+
218+
var max = session.Query<Entity>().Max(e => e.StringValue);
219+
Assert.AreEqual("c", max);
220+
}
221+
}
222+
}
223+
}
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
using System.Linq;
2+
using System;
3+
using System.Data;
4+
using NHibernate.SqlTypes;
5+
using NHibernate.UserTypes;
6+
7+
namespace NHibernate.Test.NHSpecificTest.NH3237
8+
{
9+
public class DateTimeOffsetUserType : IUserType, IParameterizedType
10+
{
11+
public TimeSpan Offset { get; private set; }
12+
13+
public DateTimeOffsetUserType()
14+
{
15+
16+
}
17+
public DateTimeOffsetUserType(TimeSpan offset)
18+
{
19+
Offset = offset;
20+
}
21+
22+
public System.Type ReturnedType
23+
{
24+
get { return typeof(DateTimeOffset); }
25+
}
26+
27+
public SqlType[] SqlTypes
28+
{
29+
get { return new[] { new SqlType(DbType.DateTime) }; }
30+
}
31+
32+
public object NullSafeGet(IDataReader dr, string[] names, object owner)
33+
{
34+
var name = names[0];
35+
int index = dr.GetOrdinal(name);
36+
37+
if (dr.IsDBNull(index))
38+
{
39+
return null;
40+
}
41+
try
42+
{
43+
DateTime storedTime;
44+
try
45+
{
46+
DateTime dbValue = Convert.ToDateTime(dr[index]);
47+
storedTime = new DateTime(dbValue.Year, dbValue.Month, dbValue.Day, dbValue.Hour, dbValue.Minute, dbValue.Second);
48+
}
49+
catch (Exception ex)
50+
{
51+
throw new FormatException(string.Format("Input string '{0}' was not in the correct format.", dr[index]), ex);
52+
}
53+
54+
return new DateTimeOffset(storedTime, Offset);
55+
}
56+
catch (InvalidCastException ice)
57+
{
58+
throw new ADOException(
59+
string.Format(
60+
"Could not cast the value in field {0} of type {1} to the Type {2}. Please check to make sure that the mapping is correct and that your DataProvider supports this Data Type.",
61+
names[0], dr[index].GetType().Name, GetType().Name), ice);
62+
}
63+
}
64+
65+
public void NullSafeSet(IDbCommand cmd, object value, int index)
66+
{
67+
if (value == null)
68+
{
69+
NHibernateUtil.DateTime.NullSafeSet(cmd, null, index);
70+
}
71+
else
72+
{
73+
DateTimeOffset dateTimeOffset = (DateTimeOffset)value;
74+
DateTime paramVal = dateTimeOffset.ToOffset(Offset).DateTime;
75+
76+
IDataParameter parameter = (IDataParameter)cmd.Parameters[index];
77+
parameter.Value = paramVal;
78+
}
79+
}
80+
81+
public object Assemble(object cached, object owner)
82+
{
83+
return cached;
84+
}
85+
86+
public object DeepCopy(object value)
87+
{
88+
return value;
89+
}
90+
91+
public object Disassemble(object value)
92+
{
93+
return value;
94+
}
95+
96+
public new bool Equals(object x, object y)
97+
{
98+
if (ReferenceEquals(x, null))
99+
{
100+
return ReferenceEquals(y, null);
101+
}
102+
return x.Equals(y);
103+
}
104+
105+
public int GetHashCode(object x)
106+
{
107+
if (ReferenceEquals(x, null))
108+
{
109+
return 0;
110+
}
111+
return x.GetHashCode();
112+
}
113+
114+
public bool IsMutable
115+
{
116+
get { return false; }
117+
}
118+
119+
public object Replace(object original, object target, object owner)
120+
{
121+
return original;
122+
}
123+
124+
public int Compare(object x, object y)
125+
{
126+
return ((DateTimeOffset)x).CompareTo((DateTimeOffset)y);
127+
}
128+
129+
public void SetParameterValues(System.Collections.Generic.IDictionary<string, string> parameters)
130+
{
131+
string offset;
132+
if (parameters.TryGetValue("Offset", out offset))
133+
{
134+
Offset = TimeSpan.Parse(offset);
135+
}
136+
}
137+
}
138+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using System;
2+
3+
namespace NHibernate.Test.NHSpecificTest.NH3237
4+
{
5+
class Entity
6+
{
7+
public virtual Guid Id { get; set; }
8+
public virtual DateTimeOffset DateTimeOffsetValue { get; set; }
9+
public virtual TestEnum EnumValue { get; set; }
10+
public virtual int IntValue { get; set; }
11+
public virtual long LongValue { get; set; }
12+
public virtual decimal DecimalValue { get; set; }
13+
public virtual double DoubleValue { get; set; }
14+
public virtual float FloatValue { get; set; }
15+
public virtual string StringValue { get; set; }
16+
public virtual DateTime DateTimeValue { get; set; }
17+
}
18+
19+
public enum TestEnum
20+
{
21+
Zero = 0,
22+
One = 1,
23+
Two = 2
24+
}
25+
}

0 commit comments

Comments
 (0)