forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuerySourceNamer.cs
64 lines (53 loc) · 1.55 KB
/
QuerySourceNamer.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
using System;
using System.Collections.Generic;
using Remotion.Linq.Clauses;
namespace NHibernate.Linq
{
/// <summary>
/// Associate unique names to query sources. The HQL AST parser will rename them anyway, but we need to
/// ensure uniqueness that is not provided by IQuerySource.ItemName.
/// </summary>
public class QuerySourceNamer
{
private readonly Dictionary<IQuerySource, string> _map = new Dictionary<IQuerySource, string>();
private readonly List<string> _names = new List<string>();
private int _differentiator = 1;
public void Add(IQuerySource querySource)
{
if (_map.ContainsKey(querySource))
return;
_map.Add(querySource, CreateUniqueName(querySource.ItemName));
}
internal void Add(IQuerySource querySource, string name)
{
if (_map.ContainsKey(querySource))
return;
_map.Add(querySource, name);
}
public string GetName(IQuerySource querySource)
{
string result;
if (!_map.TryGetValue(querySource, out result))
{
throw new HibernateException(
String.Format("Query Source could not be identified: ItemName = {0}, ItemType = {1}, Expression = {2}",
querySource.ItemName,
querySource.ItemType,
querySource));
}
return result;
}
private string CreateUniqueName(string proposedName)
{
string uniqueName = proposedName;
if (_names.Contains(proposedName))
{
// make the name unique
uniqueName = string.Format("{0}{1:000}", proposedName, _differentiator);
_differentiator++;
}
_names.Add(uniqueName);
return uniqueName;
}
}
}