forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDistinctRootEntityResultTransformer.cs
87 lines (72 loc) · 2.06 KB
/
DistinctRootEntityResultTransformer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
namespace NHibernate.Transform
{
[Serializable]
public class DistinctRootEntityResultTransformer : IResultTransformer, ITupleSubsetResultTransformer
{
private static readonly INHibernateLogger log = NHibernateLogger.For(typeof(DistinctRootEntityResultTransformer));
internal static readonly DistinctRootEntityResultTransformer Instance = new DistinctRootEntityResultTransformer();
internal sealed class Identity
{
internal readonly object entity;
internal Identity(object entity)
{
this.entity = entity;
}
public override bool Equals(object other)
{
Identity that = (Identity) other;
return ReferenceEquals(entity, that.entity);
}
public override int GetHashCode()
{
return RuntimeHelpers.GetHashCode(entity);
}
}
public object TransformTuple(object[] tuple, string[] aliases)
{
return tuple[tuple.Length - 1];
}
public IList TransformList(IList list)
{
IList result = (IList)Activator.CreateInstance(list.GetType());
var distinct = new HashSet<Identity>();
for (int i = 0; i < list.Count; i++)
{
object entity = list[i];
if (distinct.Add(new Identity(entity)))
{
result.Add(entity);
}
}
if (log.IsDebugEnabled())
{
log.Debug("transformed: {0} rows to: {1} distinct results", list.Count, result.Count);
}
return result;
}
public bool[] IncludeInTransform(String[] aliases, int tupleLength)
{
return RootEntityResultTransformer.Instance.IncludeInTransform(aliases, tupleLength);
}
public bool IsTransformedValueATupleElement(String[] aliases, int tupleLength)
{
return RootEntityResultTransformer.Instance.IsTransformedValueATupleElement(null, tupleLength);
}
public override bool Equals(object obj)
{
if (ReferenceEquals(obj, this))
return true;
if (obj == null)
return false;
return obj.GetType() == GetType();
}
public override int GetHashCode()
{
return RuntimeHelpers.GetHashCode(Instance);
}
}
}