forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBasicLoader.cs
113 lines (97 loc) · 3.07 KB
/
BasicLoader.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
using System.Collections.Generic;
using NHibernate.Engine;
using NHibernate.Persister.Collection;
using NHibernate.Persister.Entity;
using NHibernate.Type;
using NHibernate.Util;
namespace NHibernate.Loader
{
public abstract class BasicLoader : Loader
{
protected static readonly string[] NoSuffix = {string.Empty};
private IEntityAliases[] descriptors;
private ICollectionAliases[] collectionDescriptors;
public BasicLoader(ISessionFactoryImplementor factory) : base(factory) {}
protected override sealed IEntityAliases[] EntityAliases
{
get { return descriptors; }
}
protected override sealed ICollectionAliases[] CollectionAliases
{
get { return collectionDescriptors; }
}
protected abstract string[] Suffixes { get; }
protected abstract string[] CollectionSuffixes { get; }
protected override void PostInstantiate()
{
ILoadable[] persisters = EntityPersisters;
string[] suffixes = Suffixes;
descriptors = new IEntityAliases[persisters.Length];
for (int i = 0; i < descriptors.Length; i++)
{
descriptors[i] = new DefaultEntityAliases(persisters[i], suffixes[i]);
}
ICollectionPersister[] collectionPersisters = CollectionPersisters;
int bagCount = 0;
if (collectionPersisters != null)
{
string[] collectionSuffixes = CollectionSuffixes;
collectionDescriptors = new ICollectionAliases[collectionPersisters.Length];
for (int i = 0; i < collectionPersisters.Length; i++)
{
if (IsBag(collectionPersisters[i]))
{
bagCount++;
}
collectionDescriptors[i] = new GeneratedCollectionAliases(GetCollectionUserProvidedAlias(i), collectionPersisters[i], collectionSuffixes[i]);
}
}
else
{
collectionDescriptors = null;
}
// H3.2 : 14.3. Associations and joins
// Join fetching multiple collection roles also sometimes gives unexpected results for bag mappings,
// so be careful about how you formulate your queries in this case
if (bagCount > 1)
{
throw new QueryException($"Cannot simultaneously fetch multiple bags: {this}");
}
}
protected virtual IDictionary<string, string[]> GetCollectionUserProvidedAlias(int index)
{
return null;
}
private static bool IsBag(ICollectionPersister collectionPersister)
{
var type = collectionPersister.CollectionType.GetType();
return type.IsGenericType && type.GetGenericTypeDefinition() == typeof (GenericBagType<>);
}
/// <summary>
/// Utility method that generates 0_, 1_ suffixes. Subclasses don't
/// necessarily need to use this algorithm, but it is intended that
/// they will in most cases.
/// </summary>
public static string[] GenerateSuffixes(int length)
{
return GenerateSuffixes(0, length);
}
public static string[] GenerateSuffixes(int seed, int length)
{
if (length == 0)
{
return NoSuffix;
}
string[] suffixes = new string[length];
for (int i = 0; i < length; i++)
{
suffixes[i] = GenerateSuffix(i + seed);
}
return suffixes;
}
public static string GenerateSuffix(int index)
{
return index.ToString() + StringHelper.Underscore;
}
}
}