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

120 lines
3.6 KiB
C#
Raw Normal View History

using System;
using System.CodeDom;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PoweredSoft.DynamicLinq.Fluent
{
2018-02-28 18:07:34 -05:00
public abstract partial class QueryBuilderBase
{
public bool IsNullCheckingEnabled { get; protected set; } = false;
public List<QueryBuilderFilter> Filters { get; protected set; } = new List<QueryBuilderFilter>();
public List<QueryBuilderSort> Sorts { get; protected set; } = new List<QueryBuilderSort>();
public virtual QueryBuilderBase NullChecking(bool check = true)
{
IsNullCheckingEnabled = check;
return this;
}
public virtual QueryBuilderBase Compare(string path, ConditionOperators conditionOperators, object value,
QueryConvertStrategy convertStrategy = QueryConvertStrategy.ConvertConstantToComparedPropertyOrField,
bool and = true, QueryCollectionHandling collectionHandling = QueryCollectionHandling.Any)
{
Filters.Add(new QueryBuilderFilter
{
And = and,
ConditionOperator = conditionOperators,
Path = path,
Value = value,
ConvertStrategy = convertStrategy,
CollectionHandling = collectionHandling
});
return this;
}
2018-02-20 22:21:23 -05:00
public virtual QueryBuilderBase Sort(string path, QuerySortDirection sortDirection, bool appendSort)
{
Sorts.Add(new QueryBuilderSort
{
Path = path,
2018-02-20 22:21:23 -05:00
sortDirection = sortDirection,
AppendSort = appendSort
});
return this;
}
protected abstract QueryBuilderBase GetSubQueryBuilder();
public virtual QueryBuilderBase SubQuery(Action<QueryBuilderBase> subQuery, bool and = true)
{
// create query builder for same type.
var qb = GetSubQueryBuilder();
qb.NullChecking(IsNullCheckingEnabled);
// callback.
subQuery(qb);
// create a query part.
var part = new QueryBuilderFilter();
part.And = and;
part.Filters = qb.Filters;
Filters.Add(part);
//return self.
return this;
2018-02-28 18:07:34 -05:00
}
2018-02-14 08:49:49 -05:00
public virtual QueryBuilderBase OrderBy(string path)
{
Sorts.Clear();
Sorts.Add(new QueryBuilderSort
{
Path = path,
2018-02-20 22:21:23 -05:00
sortDirection = QuerySortDirection.Ascending,
2018-02-14 08:49:49 -05:00
AppendSort = false
});
return this;
}
public virtual QueryBuilderBase OrderByDescending(string path)
{
Sorts.Clear();
Sorts.Add(new QueryBuilderSort
{
Path = path,
2018-02-20 22:21:23 -05:00
sortDirection = QuerySortDirection.Descending,
2018-02-14 08:49:49 -05:00
AppendSort = false
});
return this;
}
public virtual QueryBuilderBase ThenBy(string path)
{
Sorts.Add(new QueryBuilderSort
{
Path = path,
2018-02-20 22:21:23 -05:00
sortDirection = QuerySortDirection.Ascending,
2018-02-14 08:49:49 -05:00
AppendSort = true
});
return this;
}
public virtual QueryBuilderBase ThenByDescending(string path)
{
Sorts.Add(new QueryBuilderSort
{
Path = path,
2018-02-20 22:21:23 -05:00
sortDirection = QuerySortDirection.Descending,
2018-02-14 08:49:49 -05:00
AppendSort = true
});
return this;
}
}
}