1 //---------------------------------------------------------------------
2 // <copyright file="CqlWriter.cs" company="Microsoft">
3 //      Copyright (c) Microsoft Corporation.  All rights reserved.
4 // </copyright>
5 //
6 // @owner Microsoft
7 // @backupOwner Microsoft
8 //---------------------------------------------------------------------
9 
10 using System.Text.RegularExpressions;
11 using System.Text;
12 using System.Data.Common.Utils;
13 using System.Data.Mapping.ViewGeneration.Utils;
14 using System.Data.Metadata.Edm;
15 
16 namespace System.Data.Mapping.ViewGeneration.CqlGeneration
17 {
18 
19     // This class contains helper methods needed for generating Cql
20     internal static class CqlWriter
21     {
22 
23         #region Fields
24         private static readonly Regex s_wordIdentifierRegex = new Regex(@"^[_A-Za-z]\w*$", RegexOptions.ECMAScript | RegexOptions.Compiled);
25         #endregion
26 
27         #region Helper Methods
28         // effects: Given a block name and a field in it -- returns a string
29         // of form "blockName.field". Does not perform any escaping
GetQualifiedName(string blockName, string field)30         internal static string GetQualifiedName(string blockName, string field)
31         {
32             string result = StringUtil.FormatInvariant("{0}.{1}", blockName, field);
33             return result;
34         }
35 
36         // effects: Modifies builder to contain an escaped version of type's name as "[namespace.typename]"
AppendEscapedTypeName(StringBuilder builder, EdmType type)37         internal static void AppendEscapedTypeName(StringBuilder builder, EdmType type)
38         {
39             AppendEscapedName(builder, GetQualifiedName(type.NamespaceName, type.Name));
40         }
41 
42         // effects: Modifies builder to contain an escaped version of "name1.name2" as "[name1].[name2]"
AppendEscapedQualifiedName(StringBuilder builder, string name1, string name2)43         internal static void AppendEscapedQualifiedName(StringBuilder builder, string name1, string name2)
44         {
45             AppendEscapedName(builder, name1);
46             builder.Append('.');
47             AppendEscapedName(builder, name2);
48         }
49 
50         // effects: Modifies builder to contain an escaped version of "name"
AppendEscapedName(StringBuilder builder, string name)51         internal static void AppendEscapedName(StringBuilder builder, string name)
52         {
53             if (s_wordIdentifierRegex.IsMatch(name) && false == ExternalCalls.IsReservedKeyword(name))
54             {
55                 // We do not need to escape the name if it is a simple name and it is not a keyword
56                 builder.Append(name);
57             }
58             else
59             {
60                 string newName = name.Replace("]", "]]");
61                 builder.Append('[')
62                        .Append(newName)
63                        .Append(']');
64             }
65         }
66         #endregion
67     }
68 }
69