dotnet-dynamic-linq/PoweredSoft.DynamicLinq/Fluent/QueryBuilder.cs

230 lines
7.5 KiB
C#
Raw Normal View History

2018-02-11 20:55:29 -05:00
using PoweredSoft.DynamicLinq.Helpers;
using System;
using System.Collections.Generic;
2018-02-12 05:18:44 -05:00
using System.Data.SqlClient;
2018-02-11 20:55:29 -05:00
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
namespace PoweredSoft.DynamicLinq.Fluent
{
public class QueryBuilder<T>
{
public IQueryable<T> Query { get; set; }
public Type QueryableType { get; set; }
public bool IsNullCheckingEnabled { get; protected set; } = false;
2018-02-12 05:18:44 -05:00
public List<QueryBuilderFilter> Filters { get; protected set; } = new List<QueryBuilderFilter>();
public List<QueryBuilderSort> Sorts { get; protected set; } = new List<QueryBuilderSort>();
2018-02-11 20:55:29 -05:00
public QueryBuilder(IQueryable<T> query)
{
Query = query;
}
public QueryBuilder<T> NullChecking(bool check = true)
{
IsNullCheckingEnabled = check;
return this;
}
2018-02-12 04:30:55 -05:00
public virtual QueryBuilder<T> Compare(string path, ConditionOperators conditionOperators, object value,
QueryConvertStrategy convertStrategy = QueryConvertStrategy.ConvertConstantToComparedPropertyOrField,
bool and = true, QueryCollectionHandling collectionHandling = QueryCollectionHandling.Any)
2018-02-11 20:55:29 -05:00
{
2018-02-12 05:18:44 -05:00
Filters.Add(new QueryBuilderFilter
2018-02-11 20:55:29 -05:00
{
And = and,
ConditionOperator = conditionOperators,
Path = path,
Value = value,
ConvertStrategy = convertStrategy,
CollectionHandling = collectionHandling
2018-02-11 20:55:29 -05:00
});
return this;
}
2018-02-12 05:18:44 -05:00
public virtual QueryBuilder<T> Sort(string path, SortOrder sortOrder, bool appendSort)
{
Sorts.Add(new QueryBuilderSort
{
Path = path,
SortOrder = sortOrder,
AppendSort = appendSort
});
return this;
}
2018-02-12 04:30:55 -05:00
public virtual QueryBuilder<T> SubQuery(Action<QueryBuilder<T>> subQuery, bool and = true)
2018-02-11 20:55:29 -05:00
{
// create query builder for same type.
var qb = new QueryBuilder<T>(Query);
qb.NullChecking(IsNullCheckingEnabled);
2018-02-11 20:55:29 -05:00
// callback.
subQuery(qb);
// create a query part.
2018-02-12 04:35:27 -05:00
var part = new QueryBuilderFilter();
2018-02-11 20:55:29 -05:00
part.And = and;
part.Filters = qb.Filters;
2018-02-12 05:18:44 -05:00
Filters.Add(part);
2018-02-11 20:55:29 -05:00
//return self.
return this;
}
public QueryBuilder<T> And(string path, ConditionOperators conditionOperator, object value,
QueryConvertStrategy convertStrategy = QueryConvertStrategy.ConvertConstantToComparedPropertyOrField, QueryCollectionHandling collectionHandling = QueryCollectionHandling.Any)
=> Compare(path, conditionOperator, value, convertStrategy: convertStrategy, collectionHandling: collectionHandling, and: true);
2018-02-11 20:55:29 -05:00
public QueryBuilder<T> Or(string path, ConditionOperators conditionOperator, object value,
QueryConvertStrategy convertStrategy = QueryConvertStrategy.ConvertConstantToComparedPropertyOrField, QueryCollectionHandling collectionHandling = QueryCollectionHandling.Any)
=> Compare(path, conditionOperator, value, convertStrategy: convertStrategy, collectionHandling: collectionHandling, and: false);
2018-02-11 20:55:29 -05:00
public QueryBuilder<T> And(Action<QueryBuilder<T>> subQuery)
=> SubQuery(subQuery, true);
public QueryBuilder<T> Or(Action<QueryBuilder<T>> subQuery)
=> SubQuery(subQuery, false);
2018-02-12 04:30:55 -05:00
public virtual IQueryable<T> Build()
{
// the query.
var query = Query;
2018-02-12 05:18:44 -05:00
// build the filters.
2018-02-12 04:33:51 -05:00
query = BuildFilters(query);
2018-02-12 04:30:55 -05:00
2018-02-12 05:18:44 -05:00
// build the sorts
query = BuildSorts(query);
2018-02-12 04:30:55 -05:00
return query;
}
2018-02-12 05:18:44 -05:00
public virtual QueryBuilder<T> OrderBy(string path)
{
Sorts.Clear();
Sorts.Add(new QueryBuilderSort
{
Path = path,
SortOrder = SortOrder.Ascending,
AppendSort = false
});
return this;
}
public virtual QueryBuilder<T> OrderByDescending(string path)
{
Sorts.Clear();
Sorts.Add(new QueryBuilderSort
{
Path = path,
SortOrder = SortOrder.Descending,
AppendSort = false
});
return this;
}
public virtual QueryBuilder<T> ThenBy(string path)
{
Sorts.Add(new QueryBuilderSort
{
Path = path,
SortOrder = SortOrder.Ascending,
AppendSort = true
});
return this;
}
public virtual QueryBuilder<T> ThenByDescending(string path)
{
Sorts.Add(new QueryBuilderSort
{
Path = path,
SortOrder = SortOrder.Descending,
AppendSort = true
});
return this;
}
protected virtual IQueryable<T> BuildSorts(IQueryable<T> query)
{
Sorts.ForEach(sort =>
{
query = QueryableHelpers.CreateSortExpression(query, sort.Path, sort.SortOrder, sort.AppendSort);
});
return query;
}
2018-02-12 04:33:51 -05:00
protected virtual IQueryable<T> BuildFilters(IQueryable<T> query)
2018-02-11 20:55:29 -05:00
{
2018-02-12 05:18:44 -05:00
if (Filters == null || Filters?.Count() == 0)
return query;
// shared parameter.
var sharedParameter = Expression.Parameter(typeof(T), "t");
// build the expression.
var filterExpressionMerged = BuildFilterExpression(sharedParameter, Filters);
// make changes on the query.
query = query.Where(filterExpressionMerged);
2018-02-12 05:18:44 -05:00
2018-02-11 20:55:29 -05:00
return query;
}
protected virtual Expression<Func<T, bool>> BuildFilterExpression(ParameterExpression parameter, List<QueryBuilderFilter> filters)
2018-02-11 20:55:29 -05:00
{
Expression<Func<T, bool>> temp = null;
2018-02-11 20:55:29 -05:00
2018-02-12 04:35:27 -05:00
filters.ForEach(filter =>
2018-02-11 20:55:29 -05:00
{
Expression<Func<T, bool>> innerExpression;
if (filter.Filters?.Any() == true)
innerExpression = BuildFilterExpression(parameter, filter.Filters);
2018-02-11 20:55:29 -05:00
else
2018-02-12 04:35:27 -05:00
innerExpression = BuildFilterExpression(parameter, filter);
2018-02-11 20:55:29 -05:00
if (temp == null)
{
temp = innerExpression;
}
2018-02-11 20:55:29 -05:00
else
{
if (filter.And)
temp = Expression.Lambda<Func<T, bool>>(Expression.AndAlso(temp.Body, innerExpression.Body), parameter);
else
temp = Expression.Lambda<Func<T, bool>>(Expression.OrElse(temp.Body, innerExpression.Body), parameter);
}
2018-02-11 20:55:29 -05:00
});
return temp;
2018-02-11 20:55:29 -05:00
}
protected virtual Expression<Func<T, bool>> BuildFilterExpression(ParameterExpression parameter, QueryBuilderFilter filter)
2018-02-11 20:55:29 -05:00
{
var ret = QueryableHelpers.CreateFilterExpression<T>(
filter.Path,
filter.ConditionOperator,
filter.Value,
filter.ConvertStrategy,
filter.CollectionHandling,
parameter: parameter,
nullChecking: IsNullCheckingEnabled
);
return ret;
2018-02-11 20:55:29 -05:00
}
}
}