Skip to content

Commit 1d0103a

Browse files
Merge pull request #163 from TobiasMarklund/optimizeremovefilter
Optimize removal of filter disabled conditions
2 parents b0a021b + 279d0fa commit 1d0103a

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

src/EntityFramework.DynamicFilters.Shared/DynamicFilterExtensions.cs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -934,10 +934,13 @@ private static void RemoveFilterDisabledConditionFromQuery(DbCommand command, Db
934934
int paramIdx;
935935
// Must include the "IS NOT NULL" part here. When switched to using CSpace, the SQL was slightly different
936936
// and found a case (covered by test case "AccountAndBlogEntries") where an embedded select was returning these parameters!
937-
while ((paramIdx = command.CommandText.IndexOf(param.ParameterName + " IS NOT NULL", StringComparison.CurrentCultureIgnoreCase)) != -1)
937+
StringBuilder b = new StringBuilder();
938+
var curIdx = 0;
939+
while ((curIdx < command.CommandText.Length) &&
940+
(paramIdx = command.CommandText.IndexOf(param.ParameterName + " IS NOT NULL", curIdx, StringComparison.OrdinalIgnoreCase)) != -1)
938941
{
939-
int startIdx = command.CommandText.LastIndexOf("or", paramIdx, StringComparison.CurrentCultureIgnoreCase);
940-
int endIdx = command.CommandText.IndexOf(")", paramIdx);
942+
int startIdx = command.CommandText.LastIndexOf("or", paramIdx, StringComparison.OrdinalIgnoreCase);
943+
int endIdx = command.CommandText.IndexOf(')', paramIdx);
941944

942945
if (endIdx == -1)
943946
{
@@ -952,7 +955,7 @@ private static void RemoveFilterDisabledConditionFromQuery(DbCommand command, Db
952955
// (note the extra ()'s which are not present in PostgreSQL).
953956
// So while we found the ending ")", we may or may not want to actually remove it.
954957
// Determine that by checking for the presence of an opening "(" in between the "or" and the parameter name
955-
var openingParenIndex = command.CommandText.IndexOf("(", startIdx);
958+
var openingParenIndex = command.CommandText.IndexOf('(', startIdx);
956959
if ((openingParenIndex < startIdx) || (openingParenIndex > paramIdx))
957960
endIdx--; // Do not have opening paren so do not remove the trailing ")"!
958961
}
@@ -962,12 +965,18 @@ private static void RemoveFilterDisabledConditionFromQuery(DbCommand command, Db
962965
#if (DEBUG)
963966
throw new ApplicationException(string.Format("Failed to find start or end index of remove filter clause for parameter name {0}", param.ParameterName));
964967
#else
965-
return;
968+
break;
966969
#endif
967970
}
968971

969-
command.CommandText = command.CommandText.Remove(startIdx, endIdx - startIdx + 1);
972+
b.Append(command.CommandText.Substring(curIdx, startIdx - curIdx));
973+
974+
curIdx = endIdx + 1;
970975
}
976+
if (curIdx < command.CommandText.Length)
977+
b.Append(command.CommandText.Substring(curIdx, command.CommandText.Length - curIdx));
978+
979+
command.CommandText = b.ToString();
971980
}
972981

973982
/// <summary>

0 commit comments

Comments
 (0)