forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQueryCacheResultBuilder.cs
119 lines (102 loc) · 2.46 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NHibernate.Collection;
using NHibernate.Engine;
using NHibernate.Persister.Collection;
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 IType[] _cacheTypes;
private readonly List<int> _entityFetchIndexes = new List<int>();
private readonly List<int> _collectionFetchIndexes = new List<int>();
private readonly bool _hasFetches;
internal QueryCacheResultBuilder(Loader.Loader loader)
{
_resultTypes = loader.ResultTypes;
_cacheTypes = loader.CacheTypes;
if (loader.EntityFetches != null)
{
for (var i = 0; i < loader.EntityFetches.Length; i++)
{
if (loader.EntityFetches[i])
{
_entityFetchIndexes.Add(i);
}
}
_hasFetches = _entityFetchIndexes.Count > 0;
}
if (loader.CollectionFetches == null)
{
return;
}
for (var i = 0; i < loader.CollectionFetches.Length; i++)
{
if (loader.CollectionFetches[i])
{
_collectionFetchIndexes.Add(i);
}
}
_hasFetches = _hasFetches || _collectionFetchIndexes.Count > 0;
}
internal IList Result { get; } = new List<object>();
internal void AddRow(object result, object[] entities, IPersistentCollection[] collections)
{
if (!_hasFetches)
{
Result.Add(result);
return;
}
var row = new object[_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 _entityFetchIndexes)
{
row[i++] = entities[index];
}
foreach (var index in _collectionFetchIndexes)
{
row[i++] = collections[index];
}
Result.Add(row);
}
internal IList GetResultList(IList cacheList)
{
if (!_hasFetches)
{
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;
}
}
}