-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathScriptGeneratorSupporter.cs
More file actions
231 lines (207 loc) · 9.41 KB
/
ScriptGeneratorSupporter.cs
File metadata and controls
231 lines (207 loc) · 9.41 KB
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
//------------------------------------------------------------------------------
// <copyright file="ScriptGeneratorSupporter.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
//------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using System.Text;
using antlr;
using Microsoft.SqlServer.TransactSql.ScriptDom;
namespace Microsoft.SqlServer.TransactSql.ScriptDom.ScriptGenerator
{
/// <summary>
/// Converts token type id numbers to string representations using a lookup
/// array
/// </summary>
internal static partial class ScriptGeneratorSupporter
{
#region Constants
internal const string EscapedRSquareParen = "]]";
internal const string EscapedQuote = "\"\"";
internal const string Quote = "\"";
#endregion
#region GetNNNCase Methods
public static Int32 TokenTypeCount
{
get
{
return _typeStrings.Length;
}
}
/// <summary>
/// Retrieves a version of the specified string, in the keyword casing format specified
/// </summary>
/// <param name="str">The string to get a specially cased version of</param>
/// <param name="casing">The keyword casing method to use</param>
/// <returns>A version of the string in the casing format specified in <paramref name="casing"/></returns>
[SuppressMessage("Microsoft.Globalization", "CA1308:NormalizeStringsToUppercase")]
public static string GetCasedString(string str, KeywordCasing casing)
{
switch (casing)
{
case KeywordCasing.Lowercase:
return str.ToLowerInvariant();
case KeywordCasing.Uppercase:
return str.ToUpperInvariant();
case KeywordCasing.PascalCase:
return GetPascalCase(str);
default:
Debug.Fail("Invalid KeywordCasing value");
break;
}
return String.Empty;
}
/// <summary>
/// Retrieves a version of the specified string, in the identifier casing format specified
/// </summary>
/// <param name="str">The string to get a specially cased version of</param>
/// <param name="casing">The identifier casing method to use</param>
/// <returns>A version of the string in the casing format specified in <paramref name="casing"/></returns>
[SuppressMessage("Microsoft.Globalization", "CA1308:NormalizeStringsToUppercase")]
public static string GetCasedString(string str, IdentifierCasing casing)
{
switch (casing)
{
case IdentifierCasing.PreserveOriginal:
// No transformation - preserve original casing
return str;
case IdentifierCasing.Lowercase:
return str.ToLowerInvariant();
case IdentifierCasing.Uppercase:
return str.ToUpperInvariant();
case IdentifierCasing.PascalCase:
return GetPascalCase(str);
default:
Debug.Fail("Invalid IdentifierCasing value");
break;
}
return String.Empty;
}
/// <summary>
/// Retrieves a Pascal Cased version of the string
/// </summary>
/// <param name="str">The string to pascal case</param>
/// <returns>A Pascal Cased version of the string</returns>
[SuppressMessage("Microsoft.Globalization", "CA1308:NormalizeStringsToUppercase")]
public static string GetPascalCase(string str)
{
// Make the first letter upper case
str = str.ToLowerInvariant();
char firstLetter = Char.ToUpperInvariant(str[0]);
// Return a new string
StringBuilder sb = new StringBuilder();
sb.Append(firstLetter);
sb.Append(str.Substring(1));
return sb.ToString();
}
/// <summary>
/// Retrieves a string representation of the token, in lower case.
/// </summary>
/// <param name="tokenType">The type of token to retrieve a string representation of</param>
/// <returns>A lower-cased string representation of the token</returns>
/// <exception cref="System.InvalidOperationException">
/// The specified token type does not have a single string representation (such as Identifier),
/// it must be accompanied by a string representation.
/// </exception>
public static string GetLowerCase(TSqlTokenType tokenType)
{
// The array contains lower case representations already
if (tokenType < 0 || (int)tokenType >= _typeStrings.Length)
{
throw new ArgumentOutOfRangeException("tokenType");
}
string typeString = _typeStrings[(int)tokenType];
if (String.IsNullOrEmpty(typeString))
{
throw new InvalidOperationException(
String.Format(CultureInfo.CurrentCulture, SqlScriptGeneratorResource.TokenTypeDoesNotHaveStringRepresentation, tokenType));
}
return typeString;
}
/// <summary>
/// Retrieves a string representation of the token, in upper case.
/// </summary>
/// <param name="tokenType">The type of token to retrieve a string representation of</param>
/// <returns>A upper-cased string representation of the token</returns>
/// <exception cref="System.InvalidOperationException">
/// The specified token type does not have a single string representation (such as Identifier),
/// it must be accompanied by a string representation.
/// </exception>
public static string GetUpperCase(TSqlTokenType tokenType)
{
// TODO: Consider a separate Upper Case array, for performance
// Get the lower case representation, and convert to upper case
return GetLowerCase(tokenType).ToUpperInvariant();
}
/// <summary>
/// Retrieves a string representation of the token, in pascal case (first letter capitalized,
/// remaining letters lower case).
/// </summary>
/// <param name="tokenType">The type of token to retrieve a string representation of</param>
/// <returns>A pascal-cased string representation of the token</returns>
/// <exception cref="System.InvalidOperationException">
/// The specified token type does not have a single string representation (such as Identifier),
/// it must be accompanied by a string representation.
/// </exception>
public static string GetPascalCase(TSqlTokenType tokenType)
{
// TODO: Consider a separate Pascal Cased array, to cover some of the special cases
// such as RowGuidCol, IdentityCol, and other keywords with "internal words"
// Get the lower case string and make it Pascal Cased
return GetPascalCase(GetLowerCase(tokenType));
}
#endregion
#region helper methods
/// <summary>
/// Retrieves a string representation of the token, in the casing format specified
/// </summary>
/// <param name="tokenType">The type of token to retrieve a string representation of</param>
/// <param name="casing">The casing method to use</param>
/// <returns>A string representation of the token</returns>
/// <exception cref="System.InvalidOperationException">
/// The specified token type does not have a single string representation (such as Identifier),
/// it must be accompanied by a string representation.
/// </exception>
public static string GetTokenString(TSqlTokenType tokenType, KeywordCasing casing)
{
switch (casing)
{
case KeywordCasing.Lowercase:
return GetLowerCase(tokenType);
case KeywordCasing.Uppercase:
return GetUpperCase(tokenType);
case KeywordCasing.PascalCase:
return GetPascalCase(tokenType);
default:
Debug.Fail("Invalid KeywordCasing value");
break;
}
return String.Empty;
}
/// <summary>
/// Create a white space token
/// </summary>
/// <param name="count">number of white space characters</param>
/// <returns></returns>
public static TSqlParserToken CreateWhitespaceToken(Int32 count)
{
// Build the whitespace
String ws = new String(' ', count);
// Create a whitespace token and add it to the layout table
return new TSqlParserToken(TSqlTokenType.WhiteSpace, ws);
}
internal static void CheckForNullReference(object variable, string variableName)
{
if (variableName == null)
throw new ArgumentNullException("variableName");
if (variable == null)
throw new ArgumentNullException(variableName);
}
#endregion
}
}