forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQueryCacheResultBuilder.cs
101 lines (86 loc) · 2.08 KB
/
QueryCacheResultBuilder.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
94
95
96
97
98
99
100
101
using System;
using System.Collections;
using System.Collections.Generic;
using NHibernate.Collection;
using NHibernate.Loader;
using NHibernate.Type;
namespace NHibernate.Cache
{
/// <summary>
/// A builder that builds a list from a query that can be passed to <see cref="IBatchableQueryCache"/>.
/// </summary>
public sealed class QueryCacheResultBuilder
{
private readonly IType[] _resultTypes;
private readonly Loader.Loader.QueryCacheInfo _cacheInfo;
// Since 5.5.
[Obsolete("Use overload taking an ILoader instead.")]
public static bool IsCacheWithFetches(Loader.Loader loader)
=> IsCacheWithFetches((ILoader)loader);
public static bool IsCacheWithFetches(ILoader loader)
{
return loader.CacheTypes.Length > loader.ResultTypes.Length;
}
internal QueryCacheResultBuilder(ILoader loader)
{
_resultTypes = loader.ResultTypes;
if (IsCacheWithFetches(loader))
{
_cacheInfo = loader.CacheInfo;
}
}
internal IList Result { get; } = new List<object>();
internal void AddRow(object result, object[] entities, IPersistentCollection[] collections)
{
if (_cacheInfo == null)
{
Result.Add(result);
return;
}
var row = new object[_cacheInfo.CacheTypes.Length];
if (_resultTypes.Length == 1)
{
row[0] = result;
}
else
{
Array.Copy((object[]) result, 0, row, 0, _resultTypes.Length);
}
var i = _resultTypes.Length;
foreach (var index in _cacheInfo.AdditionalEntities)
{
row[i++] = entities[index];
}
if (collections != null)
{
foreach (var collection in collections)
{
row[i++] = collection;
}
}
Result.Add(row);
}
internal IList GetResultList(IList cacheList)
{
if (_cacheInfo == null)
{
return cacheList;
}
var result = new List<object>();
foreach (object[] cacheRow in cacheList)
{
if (_resultTypes.Length == 1)
{
result.Add(cacheRow[0]);
}
else
{
var row = new object[_resultTypes.Length];
Array.Copy(cacheRow, 0, row, 0, _resultTypes.Length);
result.Add(row);
}
}
return result;
}
}
}