/// <summary> /// Test if left is between right-array. Returns true or false. Right value must be an array. Support multiple values /// </summary> public static BsonValue BETWEEN(Collation collation, BsonValue left, BsonValue right) { if (!right.IsArray) { throw new InvalidOperationException("BETWEEN expression need an array with 2 values"); } var arr = right.AsArray; if (arr.Count != 2) { throw new InvalidOperationException("BETWEEN expression need an array with 2 values"); } var start = arr[0]; var end = arr[1]; //return left >= start && right <= end; return(collation.Compare(left, start) >= 0 && collation.Compare(left, end) <= 0); }
public static BsonValue GT_ANY(Collation collation, IEnumerable <BsonValue> left, BsonValue right) => left.Any(x => collation.Compare(x, right) > 0);
public static BsonValue LTE_ALL(Collation collation, IEnumerable <BsonValue> left, BsonValue right) => left.Any(x => collation.Compare(x, right) <= 0);
/// <summary> /// Implement SqlLike in C# string - based on /// https://stackoverflow.com/a/8583383/3286260 /// I remove support for [ and ] to avoid missing close brackets /// </summary> public static bool SqlLike(this string str, string pattern, Collation collation) { var isMatch = true; var isWildCardOn = false; var isCharWildCardOn = false; var isCharSetOn = false; var isNotCharSetOn = false; var endOfPattern = false; var lastWildCard = -1; var patternIndex = 0; var p = '\0'; for (var i = 0; i < str.Length; i++) { var c = str[i]; endOfPattern = (patternIndex >= pattern.Length); if (!endOfPattern) { p = pattern[patternIndex]; if (!isWildCardOn && p == '%') { lastWildCard = patternIndex; isWildCardOn = true; while (patternIndex < pattern.Length && pattern[patternIndex] == '%') { patternIndex++; } if (patternIndex >= pattern.Length) { p = '\0'; } else { p = pattern[patternIndex]; } } else if (p == '_') { isCharWildCardOn = true; patternIndex++; } } if (isWildCardOn) { if (char.ToUpper(c) == char.ToUpper(p)) { isWildCardOn = false; patternIndex++; } } else if (isCharWildCardOn) { isCharWildCardOn = false; } else if (isCharSetOn || isNotCharSetOn) { //var charMatch = (set.Contains(char.ToUpper(c))); // -- always "false" - remove [abc] support //if ((isNotCharSetOn && charMatch) || (isCharSetOn && !charMatch)) if (isCharSetOn) { if (lastWildCard >= 0) { patternIndex = lastWildCard; } else { isMatch = false; break; } } isNotCharSetOn = isCharSetOn = false; } else { if (collation.Compare(c, p) == 0) { patternIndex++; } else { if (lastWildCard >= 0) { patternIndex = lastWildCard; } else { isMatch = false; break; } } } } endOfPattern = (patternIndex >= pattern.Length); if (isMatch && !endOfPattern) { var isOnlyWildCards = true; for (var i = patternIndex; i < pattern.Length; i++) { if (pattern[i] != '%') { isOnlyWildCards = false; break; } } if (isOnlyWildCards) { endOfPattern = true; } } return(isMatch && endOfPattern); }