/// <summary> /// Converts <paramref name="filter"/> to <see cref="IWhereClause"/>. /// </summary> /// <param name="filter">The filter to convert</param> /// <returns>A <see cref="IWhereClause"/> equivalent of the given <paramref name="filter"/>.</returns> public static IWhereClause ToWhere(this IFilter filter) { IWhereClause clause = null; switch (filter) { case Filter f: { ClauseOperator clauseOperator; object value = f.Value; switch (f.Operator) { case FilterOperator.EqualTo: clauseOperator = ClauseOperator.EqualTo; break; case FilterOperator.NotEqualTo: clauseOperator = ClauseOperator.NotEqualTo; break; case FilterOperator.IsNull: clauseOperator = ClauseOperator.IsNull; break; case FilterOperator.IsNotNull: clauseOperator = ClauseOperator.IsNotNull; break; case FilterOperator.LessThan: clauseOperator = ClauseOperator.LessThan; break; case FilterOperator.LessThanOrEqualTo: clauseOperator = ClauseOperator.LessThanOrEqualTo; break; case FilterOperator.GreaterThan: clauseOperator = ClauseOperator.GreaterThan; break; case FilterOperator.GreaterThanOrEqual: clauseOperator = ClauseOperator.GreaterThanOrEqualTo; break; case FilterOperator.StartsWith: case FilterOperator.EndsWith: case FilterOperator.Contains: clauseOperator = ClauseOperator.Like; if (f.Operator == FilterOperator.StartsWith) { value = $"{value}%"; } else if (f.Operator == FilterOperator.EndsWith) { value = $"%{value}"; } else { value = $"%{value}%"; } break; case FilterOperator.NotStartsWith: case FilterOperator.NotEndsWith: case FilterOperator.NotContains: clauseOperator = ClauseOperator.NotLike; if (f.Operator == FilterOperator.NotStartsWith) { value = $"{value}%"; } else if (f.Operator == FilterOperator.NotEndsWith) { value = $"%{value}"; } else { value = $"%{value}%"; } break; default: throw new NotSupportedException($"Unsupported {f.Operator} operator"); } clause = new WhereClause(f.Field.Field(), clauseOperator, value switch { bool b => b.Literal(), DateTime date => date.Literal(), string s => s.Literal(), null => null, long l => l.Literal(), float floatValue => floatValue.Literal(), decimal decimalValue => decimalValue.Literal(), double doubleValue => doubleValue.Literal(), int intValue => intValue.Literal(), #if NET6_0_OR_GREATER DateOnly date => date.Literal(), TimeOnly time => time.Literal(), #endif _ => throw new NotSupportedException($"Unexpected '{value?.GetType().Name}' type when building WhereClause") } ); break; }
/// <summary> /// Builds a new <see cref="WhereClause"/> instance with a <see cref="DateOnly"/> constraint. /// </summary> /// <param name="column"><see cref="IColumn"/> where to apply the clause onto</param> /// <param name="operator"><see cref="ClauseOperator"/> to apply</param> /// <param name="constraint">constraint to apply to <paramref name="column"/>.</param> /// <exception cref="ArgumentNullException">if <paramref name="column"/> is <c>null</c>.</exception> public WhereClause(IColumn column, ClauseOperator @operator, DateOnly?constraint) : this(column, @operator, constraint?.Literal()) { }