|
1 | 1 | using System;
|
| 2 | +using System.Text.RegularExpressions; |
2 | 3 | using Microsoft.Search.Interop;
|
3 | 4 |
|
4 | 5 | namespace Flow.Launcher.Plugin.Explorer.Search.WindowsIndex
|
5 | 6 | {
|
6 | 7 | public class QueryConstructor
|
7 | 8 | {
|
| 9 | + private static Regex _specialCharacterMatcher = new(@"[\@\@\#\#\&\&*_;,\%\|\!\(\)\{\}\[\]\^\~\?\\""\/\:\=\-]+", RegexOptions.Compiled); |
| 10 | + private static Regex _multiWhiteSpacesMatcher = new(@"\s+", RegexOptions.Compiled); |
| 11 | + |
8 | 12 | private Settings settings { get; }
|
9 | 13 |
|
10 | 14 | private const string SystemIndex = "SystemIndex";
|
@@ -76,8 +80,39 @@ public string FilesAndFolders(ReadOnlySpan<char> userSearchString)
|
76 | 80 | if (userSearchString.IsWhiteSpace())
|
77 | 81 | userSearchString = "*";
|
78 | 82 |
|
| 83 | + // Remove any special characters that might cause issues with the query |
| 84 | + var replacedSearchString = ReplaceSpecialCharacterWithTwoSideWhiteSpace(userSearchString); |
| 85 | + |
79 | 86 | // Generate SQL from constructed parameters, converting the userSearchString from AQS->WHERE clause
|
80 |
| - return $"{CreateBaseQuery().GenerateSQLFromUserQuery(userSearchString.ToString())} AND {RestrictionsForAllFilesAndFoldersSearch} ORDER BY {FileName}"; |
| 87 | + return $"{CreateBaseQuery().GenerateSQLFromUserQuery(replacedSearchString)} AND {RestrictionsForAllFilesAndFoldersSearch} ORDER BY {FileName}"; |
| 88 | + } |
| 89 | + |
| 90 | + /// <summary> |
| 91 | + /// If one special character have white space on one side, replace it with one white space. |
| 92 | + /// So command will not have "[special character]+*" which will cause OLEDB exception. |
| 93 | + /// </summary> |
| 94 | + private static string ReplaceSpecialCharacterWithTwoSideWhiteSpace(ReadOnlySpan<char> input) |
| 95 | + { |
| 96 | + const string whiteSpace = " "; |
| 97 | + |
| 98 | + var inputString = input.ToString(); |
| 99 | + |
| 100 | + // Use regex to match special characters with whitespace on one side |
| 101 | + // and replace them with a single space |
| 102 | + var result = _specialCharacterMatcher.Replace(inputString, match => |
| 103 | + { |
| 104 | + // Check if the match has whitespace on one side |
| 105 | + bool hasLeadingWhitespace = match.Index > 0 && char.IsWhiteSpace(inputString[match.Index - 1]); |
| 106 | + bool hasTrailingWhitespace = match.Index + match.Length < inputString.Length && char.IsWhiteSpace(inputString[match.Index + match.Length]); |
| 107 | + if (hasLeadingWhitespace || hasTrailingWhitespace) |
| 108 | + { |
| 109 | + return whiteSpace; |
| 110 | + } |
| 111 | + return match.Value; |
| 112 | + }); |
| 113 | + |
| 114 | + // Remove any extra spaces that might have been introduced |
| 115 | + return _multiWhiteSpacesMatcher.Replace(result, whiteSpace).Trim(); |
81 | 116 | }
|
82 | 117 |
|
83 | 118 | ///<summary>
|
|
0 commit comments