using System.Collections.Immutable;
using System.Linq.Expressions;
namespace Khaemhat.Data.Querying;
/// Starts queries: Query.For<Party>().Where(p => p.Name.Contains(text)).OrderBy(p => p.Name).Page(0, 50).
public static class Query
{
/// A query over all rows of .
/// The entity type.
/// The query.
public static Query For()
where T : class => Query.All;
}
///
/// An immutable query over the table of (SPEC ยง5.2): filters in the safe expression subset
/// (equality, comparisons, Contains/StartsWith/EndsWith, In as list.Contains(x.Prop),
/// &&, ||, !, == null), parameterized conditions, ordering and
/// paging. Each method returns a new query.
///
/// The entity type.
public sealed class Query
where T : class
{
private Query(
ImmutableList filters, ImmutableList conditions, ImmutableList order, int offset, int? limit)
{
Filters = filters;
Conditions = conditions;
Order = order;
Offset = offset;
Limit = limit;
}
internal static Query All { get; } = new([], [], [], 0, null);
/// The expression filters, combined with AND.
public ImmutableList Filters { get; }
/// The SQL conditions, combined with AND.
public ImmutableList Conditions { get; }
/// The ordering, in priority order.
public ImmutableList Order { get; }
/// Rows to skip.
public int Offset { get; }
/// Rows to return at most, or for all.
public int? Limit { get; }
/// Adds a filter.
/// The predicate, in the supported subset.
/// The new query.
public Query Where(Expression> predicate)
{
ArgumentNullException.ThrowIfNull(predicate);
return new(Filters.Add(predicate), Conditions, Order, Offset, Limit);
}
/// Adds a parameterized SQL condition, for what expressions cannot say.
/// The condition, for example SqlFragment.Of($"{SqlFragment.Identifier("total")} > {min}").
/// The new query.
public Query Where(SqlFragment condition)
{
ArgumentNullException.ThrowIfNull(condition);
return condition.IsEmpty ? this : new(Filters, Conditions.Add(condition), Order, Offset, Limit);
}
/// Orders by a property, ascending; further calls add secondary orderings.
/// The property type.
/// The property.
/// The new query.
public Query OrderBy(Expression> property) => AddOrder(property, descending: false);
/// Orders by a property, descending; further calls add secondary orderings.
/// The property type.
/// The property.
/// The new query.
public Query OrderByDescending(Expression> property) => AddOrder(property, descending: true);
/// Returns at most rows after skipping .
/// Rows to skip.
/// Rows to return.
/// The new query.
public Query Page(int offset, int limit)
{
ArgumentOutOfRangeException.ThrowIfNegative(offset);
ArgumentOutOfRangeException.ThrowIfNegative(limit);
return new(Filters, Conditions, Order, offset, limit);
}
/// Returns at most rows.
/// Rows to return.
/// The new query.
public Query Take(int limit) => Page(Offset, limit);
private Query AddOrder(LambdaExpression property, bool descending)
{
ArgumentNullException.ThrowIfNull(property);
return new(Filters, Conditions, Order.Add(new QueryOrder(property, descending)), Offset, Limit);
}
}
/// An ordering of a query.
/// The property selector.
/// Whether the order is descending.
public sealed record QueryOrder(LambdaExpression Property, bool Descending);