forked from dotnet/efcore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDbFunctionsExtensions.cs
More file actions
151 lines (131 loc) · 5.84 KB
/
Copy pathDbFunctionsExtensions.cs
File metadata and controls
151 lines (131 loc) · 5.84 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
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using Microsoft.EntityFrameworkCore.Utilities;
using JetBrains.Annotations;
namespace Microsoft.EntityFrameworkCore
{
/// <summary>
/// Provides CLR methods that get translated to database functions when used in LINQ to Entities queries.
/// The methods on this class are accessed via <see cref="EF.Functions" />.
/// </summary>
// ReSharper disable once InconsistentNaming
public static class DbFunctionsExtensions
{
/// <summary>
/// An implementation of the SQL LIKE operation. On relational databases this is usually directly
/// translated to SQL.
/// </summary>
/// <param name="_">The DbFunctions instance.</param>
/// <param name="matchExpression">The string that is to be matched.</param>
/// <param name="pattern">The pattern which may involve wildcards %,_,[,],^.</param>
/// <returns>true if there is a match.</returns>
public static bool Like(
[CanBeNull] this DbFunctions _,
[CanBeNull] string matchExpression,
[CanBeNull] string pattern)
=> LikeCore(matchExpression, pattern, escapeCharacter: null);
/// <summary>
/// An implementation of the SQL LIKE operation. On relational databases this is usually directly
/// translated to SQL.
/// </summary>
/// <param name="_">The DbFunctions instance.</param>
/// <param name="matchExpression">The string that is to be matched.</param>
/// <param name="pattern">The pattern which may involve wildcards %,_,[,],^.</param>
/// <param name="escapeCharacter">
/// The escape character (as a single character string) to use in front of %,_,[,],^
/// if they are not used as wildcards.
/// </param>
/// <returns>true if there is a match.</returns>
public static bool Like(
[CanBeNull] this DbFunctions _,
[CanBeNull] string matchExpression,
[CanBeNull] string pattern,
[CanBeNull] string escapeCharacter)
=> LikeCore(matchExpression, pattern, escapeCharacter);
// Regex special chars defined here:
// https://msdn.microsoft.com/en-us/library/4edbef7e(v=vs.110).aspx
private static readonly char[] _regexSpecialChars
= { '.', '$', '^', '{', '[', '(', '|', ')', '*', '+', '?', '\\' };
private static readonly string _defaultEscapeRegexCharsPattern
= BuildEscapeRegexCharsPattern(_regexSpecialChars);
private static readonly TimeSpan _regexTimeout = TimeSpan.FromMilliseconds(value: 1000.0);
private static string BuildEscapeRegexCharsPattern(IEnumerable<char> regexSpecialChars)
{
return string.Join("|", regexSpecialChars.Select(c => @"\" + c));
}
private static bool LikeCore(string matchExpression, string pattern, string escapeCharacter)
{
//TODO: this fixes https://github.com/aspnet/EntityFramework/issues/8656 by insisting that
// the "escape character" is a string but just using the first character of that string,
// but we may later want to allow the complete string as the "escape character"
// in which case we need to change the way we construct the regex below.
var singleEscapeCharacter =
(escapeCharacter == null || escapeCharacter.Length == 0)
? (char?)null
: escapeCharacter.First();
if (matchExpression == null
|| pattern == null)
{
return false;
}
if (matchExpression.Equals(pattern, StringComparison.OrdinalIgnoreCase))
{
return true;
}
if (matchExpression.Length == 0
|| pattern.Length == 0)
{
return false;
}
var escapeRegexCharsPattern
= singleEscapeCharacter == null
? _defaultEscapeRegexCharsPattern
: BuildEscapeRegexCharsPattern(_regexSpecialChars.Where(c => c != singleEscapeCharacter));
var regexPattern
= Regex.Replace(
pattern,
escapeRegexCharsPattern,
c => @"\" + c,
default(RegexOptions),
_regexTimeout);
var stringBuilder = new StringBuilder();
for (var i = 0; i < regexPattern.Length; i++)
{
var c = regexPattern[i];
var escaped = i > 0 && regexPattern[i - 1] == singleEscapeCharacter;
switch (c)
{
case '_':
{
stringBuilder.Append(escaped ? '_' : '.');
break;
}
case '%':
{
stringBuilder.Append(escaped ? "%" : ".*");
break;
}
default:
{
if (c != singleEscapeCharacter)
{
stringBuilder.Append(c);
}
break;
}
}
}
regexPattern = stringBuilder.ToString();
return Regex.IsMatch(
matchExpression,
@"\A" + regexPattern + @"\s*\z",
RegexOptions.IgnoreCase | RegexOptions.Singleline,
_regexTimeout);
}
}
}