-
-
Notifications
You must be signed in to change notification settings - Fork 514
/
Copy pathHelper.cs
192 lines (159 loc) · 6.14 KB
/
Helper.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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace SqlKata
{
public static class Helper
{
public static bool IsArray(object value)
{
if (value is string)
{
return false;
}
if (value is byte[])
{
return false;
}
return value is IEnumerable;
}
/// <summary>
/// Flat IEnumerable one level down
/// </summary>
/// <param name="array"></param>
/// <returns></returns>
public static IEnumerable<object> Flatten(IEnumerable<object> array)
{
foreach (var item in array)
{
if (IsArray(item))
{
foreach (var sub in (item as IEnumerable))
{
yield return sub;
}
}
else
{
yield return item;
}
}
}
public static IEnumerable<object> FlattenDeep(IEnumerable<object> array)
{
return array.SelectMany(o => IsArray(o) ? FlattenDeep(o as IEnumerable<object>) : new[] { o });
}
public static IEnumerable<int> AllIndexesOf(string str, string value)
{
if (string.IsNullOrEmpty(value))
{
yield break;
}
var index = 0;
do
{
index = str.IndexOf(value, index, StringComparison.Ordinal);
if (index == -1)
{
yield break;
}
yield return index;
} while ((index += value.Length) < str.Length);
}
public static string ReplaceAll(string subject, string match, Func<int, string> callback)
{
if (string.IsNullOrWhiteSpace(subject) || !subject.Contains(match))
{
return subject;
}
var splitted = subject.Split(
new[] { match },
StringSplitOptions.None
);
return splitted.Skip(1)
.Select((item, index) => callback(index) + item)
.Aggregate(new StringBuilder(splitted.First()), (prev, right) => prev.Append(right))
.ToString();
}
public static string JoinArray(string glue, IEnumerable array)
{
var result = new List<string>();
foreach (var item in array)
{
result.Add(item.ToString());
}
return string.Join(glue, result);
}
public static string ExpandParameters(string sql, string placeholder, object[] bindings)
{
return ReplaceAll(sql, placeholder, i =>
{
var parameter = bindings[i];
if (IsArray(parameter))
{
var count = EnumerableCount(parameter as IEnumerable);
return string.Join(",", placeholder.Repeat(count));
}
return placeholder.ToString();
});
}
public static int EnumerableCount(IEnumerable obj)
{
int count = 0;
foreach (var item in obj)
{
count++;
}
return count;
}
public static List<string> ExpandExpression(string expression)
{
var regex = @"^(?:\w+\.){1,2}{(.*)}";
var match = Regex.Match(expression, regex);
if (!match.Success)
{
// we did not found a match return the string as is.
return new List<string> { expression };
}
var table = expression.Substring(0, expression.IndexOf(".{"));
var captures = match.Groups[1].Value;
var cols = Regex.Split(captures, @"\s*,\s*")
.Select(x => $"{table}.{x.Trim()}")
.ToList();
return cols;
}
public static IEnumerable<string> Repeat(this string str, int count)
{
return Enumerable.Repeat(str, count);
}
/// <summary>
/// Replace instances of the <paramref name="identifier"/> within the string with the <paramref name="newIdentifier"/>, unless the <paramref name="identifier"/> was escaped via the <paramref name="escapeCharacter"/>
/// </summary>
/// <param name="input">input string to modify</param>
/// <param name="escapeCharacter">escape character to search for within the string</param>
/// <param name="identifier">string to search for and replace with <paramref name="newIdentifier"/></param>
/// <param name="newIdentifier">string that will replace instances of <paramref name="identifier"/> that have not been escaped</param>
/// <returns>
///Example ( Not Escaped ) :
///<br/> Input = [ Test ] , <paramref name="escapeCharacter"/> = '\', <paramref name="identifier"/> = '[', <paramref name="newIdentifier"/> = '{'
///<br/> Result: { Test ]
///<para/>
///Example ( Escaped ) :
///<br/> Input = \[ Test ] , <paramref name="escapeCharacter"/> = '\', <paramref name="identifier"/> = '[', <paramref name="newIdentifier"/> = '{'
///<br/> Result: [ Test ]
///<br/>
/// </returns>
public static string ReplaceIdentifierUnlessEscaped(this string input, string escapeCharacter, string identifier, string newIdentifier)
{
//Replace standard, non-escaped identifiers first
var nonEscapedRegex = new Regex($@"(?<!{Regex.Escape(escapeCharacter)}){Regex.Escape(identifier)}");
var nonEscapedReplace = nonEscapedRegex.Replace(input, newIdentifier);
//Then replace escaped identifiers, by just removing the escape character
var escapedRegex = new Regex($@"{Regex.Escape(escapeCharacter)}{Regex.Escape(identifier)}");
return escapedRegex.Replace(nonEscapedReplace, identifier);
}
}
}