2021-06-08 14:56:45 -06:00
|
|
|
using System.Collections.Generic;
|
2017-05-12 14:49:44 +02:00
|
|
|
using System.Linq;
|
2013-12-19 18:33:25 +11:00
|
|
|
using System.Text.RegularExpressions;
|
2021-02-09 11:26:22 +01:00
|
|
|
using Umbraco.Extensions;
|
2013-12-18 17:22:00 +11:00
|
|
|
|
2022-01-13 23:46:21 +00:00
|
|
|
namespace Umbraco.Cms.Core.Persistence
|
2013-12-18 17:22:00 +11:00
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// String extension methods used specifically to translate into SQL
|
|
|
|
|
/// </summary>
|
2022-01-13 23:46:21 +00:00
|
|
|
public static class SqlExpressionExtensions
|
2013-12-18 17:22:00 +11:00
|
|
|
{
|
2018-12-06 01:14:06 +11:00
|
|
|
/// <summary>
|
|
|
|
|
/// Indicates whether two nullable values are equal, substituting a fallback value for nulls.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="T">The nullable type.</typeparam>
|
|
|
|
|
/// <param name="value">The value to compare.</param>
|
|
|
|
|
/// <param name="other">The value to compare to.</param>
|
|
|
|
|
/// <param name="fallbackValue">The value to use when any value is null.</param>
|
|
|
|
|
/// <remarks>Do not use outside of Sql expressions.</remarks>
|
|
|
|
|
// see usage in ExpressionVisitorBase
|
|
|
|
|
public static bool SqlNullableEquals<T>(this T? value, T? other, T fallbackValue)
|
|
|
|
|
where T : struct
|
|
|
|
|
{
|
|
|
|
|
return (value ?? fallbackValue).Equals(other ?? fallbackValue);
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-08 14:56:45 -06:00
|
|
|
public static bool SqlIn<T>(this IEnumerable<T> collection, T item) => collection.Contains(item);
|
2017-05-12 14:49:44 +02:00
|
|
|
|
2013-12-19 18:33:25 +11:00
|
|
|
public static bool SqlWildcard(this string str, string txt, TextColumnType columnType)
|
|
|
|
|
{
|
|
|
|
|
var wildcardmatch = new Regex("^" + Regex.Escape(txt).
|
2017-07-20 11:21:28 +02:00
|
|
|
//deal with any wildcard chars %
|
2013-12-19 18:33:25 +11:00
|
|
|
Replace(@"\%", ".*") + "$");
|
|
|
|
|
|
|
|
|
|
return wildcardmatch.IsMatch(str);
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-08 14:56:45 -06:00
|
|
|
#pragma warning disable IDE0060 // Remove unused parameter
|
|
|
|
|
public static bool SqlContains(this string str, string txt, TextColumnType columnType) => str.InvariantContains(txt);
|
2013-12-18 17:22:00 +11:00
|
|
|
|
2021-06-08 14:56:45 -06:00
|
|
|
public static bool SqlEquals(this string str, string txt, TextColumnType columnType) => str.InvariantEquals(txt);
|
2013-12-18 17:22:00 +11:00
|
|
|
|
2022-02-15 14:41:06 +01:00
|
|
|
public static bool SqlStartsWith(this string? str, string txt, TextColumnType columnType) => str?.InvariantStartsWith(txt) ?? false;
|
2013-12-18 17:22:00 +11:00
|
|
|
|
2021-06-08 14:56:45 -06:00
|
|
|
public static bool SqlEndsWith(this string str, string txt, TextColumnType columnType) => str.InvariantEndsWith(txt);
|
|
|
|
|
#pragma warning restore IDE0060 // Remove unused parameter
|
2013-12-18 17:22:00 +11:00
|
|
|
}
|
2017-07-20 11:21:28 +02:00
|
|
|
}
|