-
Notifications
You must be signed in to change notification settings - Fork 1
/
StringTransformCustomizations.cs
210 lines (196 loc) · 9.72 KB
/
StringTransformCustomizations.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
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
using Microsoft.SqlServer.Server;
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.Text.RegularExpressions;
namespace MySQLCLRFunctions
{
// Horrible collection of things I made to help that I haven't used.
public static class StringTransformCustomizations
{
[SqlFunction(DataAccess = DataAccessKind.None, IsDeterministic = true, IsPrecise = true)]
public static string CleanUpSQLServerNameDelimiters(string input)
{
if (string.IsNullOrWhiteSpace(input)) return input;
if (string.IsNullOrEmpty(input)) return input;
string[] nameparts = new string[4];
int howmanynameparts = 0;
string currentnamepart = "";
int currentnamepartidx = 0;
int howmanyleftbrackets = 0, howmanyrightbrackets = 0, howmanydots = 0;
int nestedintobracketslevel = 0;
int charactersinthispart = 0;
char? lastchar = null;
foreach (char c in input)
{
lastchar = c;
if (c == '.')
{
if (nestedintobracketslevel == 0)
{
nameparts[currentnamepartidx] = currentnamepart;
currentnamepartidx++;
currentnamepart = "";
charactersinthispart = 0;
howmanynameparts++;
}
}
else if (c == '[')
{
if (nestedintobracketslevel == 0)
{
if (charactersinthispart > 0)
{
return $"2:error while parsing {input}: on character {c} at namepart {currentnamepart} with number of characters in part = {charactersinthispart} and nested to {nestedintobracketslevel}";
}
else
{
nestedintobracketslevel++;
}
}
else
{
return $"2a:error while parsing {input}: on character {c} at namepart {currentnamepart} with number of characters in part = {charactersinthispart} and nested to {nestedintobracketslevel}";
}
}
else if (c == ']')
{
if (nestedintobracketslevel <= 0)
{
return $"3:error while parsing {input}: on character {c} at namepart {currentnamepart} with number of characters in part = {charactersinthispart} and nested to {nestedintobracketslevel}";
}
if (charactersinthispart == 0)
{
return $"5:error while parsing {input}: on character {c} at namepart {currentnamepart} with number of characters in part = {charactersinthispart} and nested to {nestedintobracketslevel}";
}
else
{
nestedintobracketslevel--;
}
}
// instance separator?
else
{
charactersinthispart++;
currentnamepart += c;
// if > 128, error.
// Check for legal characters
}
}
if (lastchar == '.')
{
return $"4:error while parsing {input}: on last character {lastchar} at namepart {currentnamepart} with number of characters in part = {charactersinthispart} and nested to {nestedintobracketslevel}";
}
if (charactersinthispart > 0) {
howmanynameparts++;
nameparts[currentnamepartidx] = currentnamepart;
}
string rebuiltname = "";
int partno = 0;
foreach (string namepart in nameparts)
{
if (partno >= howmanynameparts) break;
if (string.IsNullOrWhiteSpace(namepart))
{
if (howmanynameparts - partno == 2) rebuiltname += ".";
else
{
return $"6:error while parsing {input}: rebuiltname = {rebuiltname} with partno {partno} on last character {lastchar} at namepart {currentnamepart} with number of characters in part = {charactersinthispart} and nested to {nestedintobracketslevel}";
}
}
else
{
if (partno > 0)
{
rebuiltname += '.';
}
rebuiltname += "[" + namepart + "]";
}
partno++;
}
return rebuiltname;
}
[SqlFunction(DataAccess = DataAccessKind.None, IsDeterministic = true, IsPrecise = true)]
public static string RemoveSQLServerNameDelimiters(string input)
{
if (string.IsNullOrWhiteSpace(input)) return input;
if (string.IsNullOrEmpty(input)) return input;
return input.Trim().Trim(new char[] { '[', ']' }).Replace("]]", "]");
}
public static string ExpandSQLParameterString(string sqlwithparametersembedded, int parameterno, string newvalue)
{
// https://docs.microsoft.com/en-us/dotnet/standard/base-types/character-escapes-in-regular-expressions
newvalue = "'" + newvalue.Replace("'", "''") + "'";
return Regex.Replace(sqlwithparametersembedded, $@"\$\{parameterno}(^\d|$)", newvalue);
}
public static string ExpandSQLParameter(string sqlwithparametersembedded, int parameterno, string newvalue)
{
// https://docs.microsoft.com/en-us/dotnet/standard/base-types/character-escapes-in-regular-expressions
return Regex.Replace(sqlwithparametersembedded, $@"\$\{parameterno}(^\d|$)", newvalue);
}
/***************************************************************************************************************************************************************************************************
*
* SQL Modules are hard to read as a single line with all the header junk.
* Unsafe because uses pointers to improve performance the old fashioned C way for replacements that are 1 to 1 in length.
*
* Possibly move to StringReduce.
**************************************************************************************************************************************************************************************/
[SqlFunction(DataAccess = DataAccessKind.None, IsDeterministic = true, IsPrecise = true)]
public static string TrimSQL(string input, bool toSingleLine, bool dropFullLineComments)
{
if (input == null) return null;
if (string.IsNullOrWhiteSpace(input)) return input;
input = input.ToLowerInvariant(); // We loose pretty, but matching is better
input = input.Replace("\t", " ").Replace("[dbo].", "").Replace(" as ", " ").Replace("N'", "'");
input = StringTransform.ReplaceRecursiveS(input, " ", " ");
var lines = input.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
var cleanlines = new List<string>(100);
foreach (var line in lines)
{
if (Regex.IsMatch(line, "^--=+$")
|| Regex.IsMatch(line, "^---+$")
|| (dropFullLineComments && Regex.IsMatch(line.Trim(), "^--.*$")) // Dump comments
|| line.Trim().Equals("set nocount on", StringComparison.CurrentCultureIgnoreCase)
)
{
// Discard
}
else
{
cleanlines.Add(line.TrimEnd(null));
}
}
if (toSingleLine)
{
var temps = String.Join(" ", cleanlines).REPLACERECURSIVES(" ", " "); // After tabs newlines converted to spaces
return temps.Replace(", ", ",").Replace(" =", "=").Replace("= ", "=").Replace(" (", "(").Replace("[", "").Replace("]", "").Replace(" begin ", "{")
.Replace(" end ", "}").REPLACEMATCHX("\bend\b?$", "}");
}
return String.Join(Environment.NewLine, cleanlines);
}
/***************************************************************************************************************************************************************************************************
*
* Not using
*
**************************************************************************************************************************************************************************************/
#if false
[Obsolete]
[SqlFunction(DataAccess = DataAccessKind.None, IsDeterministic = true, IsPrecise = true)]
public static string BuildRaiserrorMessage(string message, string Param1 = null, string Param2 = null, string Param3 = null, string Param4 = null, string Param5 = null
, string Param6 = null, string Param7 = null, string Param8 = null, string Param9 = null, string Param10 = null)
{
StringBuilder cmd = new StringBuilder(2048);
cmd.Append("RAISERROR('").Append(message).Append("',");
if (Param1 != null)
{
string[] param1parts = Param1.Split(':');
string param1val = param1parts[0];
string param1type = param1parts[1];
//if (param1type == "string")
}
return null;
}
#endif
}
}