c4db94301f88ad828f354a0f495c94fde2e94c04
[mono.git] / mcs / class / referencesource / System.Data.Entity / System / Data / Mapping / ViewGeneration / CqlGeneration / CqlIdentifiers.cs
1 //---------------------------------------------------------------------
2 // <copyright file="CqlIdentifiers.cs" company="Microsoft">
3 //      Copyright (c) Microsoft Corporation.  All rights reserved.
4 // </copyright>
5 //
6 // @owner Microsoft
7 // @backupOwner Microsoft
8 //---------------------------------------------------------------------
9
10
11 using System.Data.Common.Utils;
12 using System.Text;
13 using System.Collections.Generic;
14 using System.Diagnostics;
15 using System.Globalization;
16 namespace System.Data.Mapping.ViewGeneration.Structures
17 {
18
19     // This class is responsible for ensuring unique aliases for _from0, etc
20     // and block aliases T, T0, T1, etc
21     internal class CqlIdentifiers : InternalBase
22     {
23
24         #region Constructor
25         internal CqlIdentifiers()
26         {
27             m_identifiers = new Set<string>(StringComparer.Ordinal);
28         }
29         #endregion
30
31         #region Fields
32         private Set<string> m_identifiers;
33         #endregion
34
35         #region Methods
36         // effects: Given a number, returns _from<num> if it does not clashes with
37         // any identifier, else returns _from_<next>_<num> where <next> is the first number from 0
38         // where there is no clash
39         internal string GetFromVariable(int num)
40         {
41             return GetNonConflictingName("_from", num);
42         }
43
44         // effects: Given a number, returns T<num> if it does not clashes with
45         // any identifier, else returns T_<next>_<num> where <next> is the first number from 0
46         // where there is no clash
47         internal string GetBlockAlias(int num)
48         {
49             return GetNonConflictingName("T", num);
50         }
51
52         // effects: Given a number, returns T if it does not clashes with
53         // any identifier, else returns T_<next> where <next> is the first number from 0
54         // where there is no clash
55         internal string GetBlockAlias()
56         {
57             return GetNonConflictingName("T", -1);
58         }
59
60         [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Globalization", "CA1308:NormalizeStringsToUppercase")]
61         internal void AddIdentifier(string identifier)
62         {
63             m_identifiers.Add(identifier.ToLower(CultureInfo.InvariantCulture));
64         }
65
66         [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Globalization", "CA1308:NormalizeStringsToUppercase")]
67         private string GetNonConflictingName(string prefix, int number)
68         {
69             // Do a case sensitive search but return the string that uses the
70             // original prefix
71             string result = number < 0 ? prefix : StringUtil.FormatInvariant("{0}{1}", prefix, number);
72             // Check if the prefix exists or not
73             if (m_identifiers.Contains(result.ToLower(CultureInfo.InvariantCulture)) == false)
74             {
75                 return result;
76             }
77
78             // Go through integers and find the first one that does not clash
79             for (int count = 0; count < int.MaxValue; count++)
80             {
81                 if (number < 0)
82                 {
83                     result = StringUtil.FormatInvariant("{0}_{1}", prefix, count);
84                 }
85                 else
86                 {
87                     result = StringUtil.FormatInvariant("{0}_{1}_{2}", prefix, count, number);
88                 }
89                 if (m_identifiers.Contains(result.ToLower(CultureInfo.InvariantCulture)) == false)
90                 {
91                     return result;
92                 }
93             }
94             Debug.Fail("Found no unique _from till MaxValue?");
95             return null;
96         }
97
98         internal override void ToCompactString(StringBuilder builder)
99         {
100             m_identifiers.ToCompactString(builder);
101         }
102
103         #endregion
104     }
105 }