1 //---------------------------------------------------------------------
2 // <copyright file="CqlIdentifiers.cs" company="Microsoft">
3 // Copyright (c) Microsoft Corporation. All rights reserved.
7 // @backupOwner Microsoft
8 //---------------------------------------------------------------------
11 using System.Data.Common.Utils;
13 using System.Collections.Generic;
14 using System.Diagnostics;
15 using System.Globalization;
16 namespace System.Data.Mapping.ViewGeneration.Structures
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
25 internal CqlIdentifiers()
27 m_identifiers = new Set<string>(StringComparer.Ordinal);
32 private Set<string> m_identifiers;
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)
41 return GetNonConflictingName("_from", num);
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)
49 return GetNonConflictingName("T", num);
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()
57 return GetNonConflictingName("T", -1);
60 [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Globalization", "CA1308:NormalizeStringsToUppercase")]
61 internal void AddIdentifier(string identifier)
63 m_identifiers.Add(identifier.ToLower(CultureInfo.InvariantCulture));
66 [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Globalization", "CA1308:NormalizeStringsToUppercase")]
67 private string GetNonConflictingName(string prefix, int number)
69 // Do a case sensitive search but return the string that uses the
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)
78 // Go through integers and find the first one that does not clash
79 for (int count = 0; count < int.MaxValue; count++)
83 result = StringUtil.FormatInvariant("{0}_{1}", prefix, count);
87 result = StringUtil.FormatInvariant("{0}_{1}_{2}", prefix, count, number);
89 if (m_identifiers.Contains(result.ToLower(CultureInfo.InvariantCulture)) == false)
94 Debug.Fail("Found no unique _from till MaxValue?");
98 internal override void ToCompactString(StringBuilder builder)
100 m_identifiers.ToCompactString(builder);