forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDistinctRootEntityResultTransformer.cs
93 lines (77 loc) · 2.23 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
88
89
90
91
92
93
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 IInternalLogger log = LoggerProvider.LoggerFor(typeof(DistinctRootEntityResultTransformer));
private static readonly object Hasher = new object();
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(string.Format("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);
var transformer = new RootEntityResultTransformer();
return transformer.IncludeInTransform(aliases, tupleLength);
}
public bool IsTransformedValueATupleElement(String[] aliases, int tupleLength)
{
//return RootEntityResultTransformer.INSTANCE.isTransformedValueATupleElement(null, tupleLength);
var transformer = new RootEntityResultTransformer();
return transformer.IsTransformedValueATupleElement(null, tupleLength);
}
public override bool Equals(object obj)
{
if (obj == null)
{
return false;
}
return obj.GetHashCode() == Hasher.GetHashCode();
}
public override int GetHashCode()
{
return Hasher.GetHashCode();
}
}
}