Commit 40ae5b61 authored by thienvo's avatar thienvo

optimize sieve

parent 808939c9
...@@ -159,21 +159,139 @@ namespace Sieve.Services ...@@ -159,21 +159,139 @@ namespace Sieve.Services
} }
} }
public Expression<Func<TEntity, bool>> GetFilterExpressionQuery<TEntity>( public string GetExtendFilterString<TEntity>(
TSieveModel model, ref string dynamicQuery1, TSieveModel model,
bool bCaseSensitive = false) bool bCaseSensitive = false)
{ {
string fullQueryString = "";
//string dynamicQuery = ""; //string dynamicQuery = "";
if (model?.GetFiltersParsed() == null) if (model?.GetFiltersParsed() == null)
{ {
return null; return " id > 0";
}
//Expression outerExpression = null;
//var parameterExpression = Expression.Parameter(typeof(TEntity), "e");
foreach (var filterTerm in model.GetFiltersParsed())
{
//Expression innerExpression = null;
foreach (var filterTermName in filterTerm.Names)
{
string searchPart = "";
string searchProperty = ""; ////////-----------------
var (fullName, property) = GetSieveProperty<TEntity>(false, true, filterTermName);
if (property != null)
{
var converter = TypeDescriptor.GetConverter(property.PropertyType);
//dynamic propertyValue = parameterExpression;
foreach (object attrib in property.GetCustomAttributes(true))
{
searchProperty = attrib.GetType().GetProperty("StringValue").GetValue(attrib, null).ToString();
}
//foreach (var part in fullName.Split('.'))
//{
// propertyValue = Expression.PropertyOrField(propertyValue, part);
//}
if (filterTerm.Values == null) continue;
foreach (var filterTermValue in filterTerm.Values)
{
string dynamicQuery = ""; //////----------------
dynamic constantVal = converter.CanConvertFrom(typeof(string))
? converter.ConvertFrom(filterTermValue)
: Convert.ChangeType(filterTermValue, property.PropertyType);
Expression filterValue = GetClosureOverConstant(constantVal, property.PropertyType);
if (!string.IsNullOrEmpty(searchProperty))
{
dynamicQuery = GetDynamicQueryString(filterTerm, searchProperty.ToString(), filterValue.ToString());
if (string.IsNullOrEmpty(fullQueryString))
{
searchPart = "(" + dynamicQuery + ")";
}
else
{
searchPart += " Or " + "(" + dynamicQuery + ")";
}
}
#region advance
//if (filterTerm.OperatorIsCaseInsensitive)
//{
// propertyValue = Expression.Call(propertyValue,
// typeof(string).GetMethods()
// .First(m => m.Name == "ToUpper" && m.GetParameters().Length == 0));
// filterValue = Expression.Call(filterValue,
// typeof(string).GetMethods()
// .First(m => m.Name == "ToUpper" && m.GetParameters().Length == 0));
//}
//var expression = GetExpression(filterTerm, filterValue, propertyValue, bCaseSensitive);
//if (filterTerm.OperatorIsNegated)
//{
// expression = Expression.Not(expression);
//}
//if (innerExpression == null)
//{
// innerExpression = expression;
//}
//else
//{
// innerExpression = Expression.Or(innerExpression, expression);
//}
#endregion
}
}
if (!string.IsNullOrEmpty(searchPart))
{
if (string.IsNullOrEmpty(fullQueryString))
{
fullQueryString = "(" + searchPart + ")";
}
else
{
fullQueryString += " And " + "(" + searchPart + ")";
}
}
}
}
if (string.IsNullOrEmpty(fullQueryString))
{
fullQueryString = " id > 0";
}
return fullQueryString;
} }
public Expression<Func<TEntity, bool>> GetFilterExpressionQuery<TEntity>(
TSieveModel model,
bool bCaseSensitive = false)
{
Expression outerExpression = null; Expression outerExpression = null;
var parameterExpression = Expression.Parameter(typeof(TEntity), "e"); var parameterExpression = Expression.Parameter(typeof(TEntity), "e");
//string dynamicQuery = "";
if (model?.GetFiltersParsed() == null)
{
var property = Expression.Property(parameterExpression, "id");
ConstantExpression constant = Expression.Constant(0, typeof(int));
var rst = Expression.GreaterThan(property, constant);
return Expression.Lambda<Func<TEntity, bool>>(rst, parameterExpression);
}
foreach (var filterTerm in model.GetFiltersParsed()) foreach (var filterTerm in model.GetFiltersParsed())
{ {
string dynamicQuery = "";
Expression innerExpression = null; Expression innerExpression = null;
foreach (var filterTermName in filterTerm.Names) foreach (var filterTermName in filterTerm.Names)
{ {
...@@ -183,11 +301,7 @@ namespace Sieve.Services ...@@ -183,11 +301,7 @@ namespace Sieve.Services
var converter = TypeDescriptor.GetConverter(property.PropertyType); var converter = TypeDescriptor.GetConverter(property.PropertyType);
dynamic propertyValue = parameterExpression; dynamic propertyValue = parameterExpression;
foreach (object attrib in property.GetCustomAttributes(true))
{
dynamicQuery = attrib.GetType().GetProperty("StringValue").GetValue(attrib, null).ToString();
// Console.WriteLine(attrib);
}
foreach (var part in fullName.Split('.')) foreach (var part in fullName.Split('.'))
{ {
propertyValue = Expression.PropertyOrField(propertyValue, part); propertyValue = Expression.PropertyOrField(propertyValue, part);
...@@ -204,10 +318,7 @@ namespace Sieve.Services ...@@ -204,10 +318,7 @@ namespace Sieve.Services
Expression filterValue = GetClosureOverConstant(constantVal, property.PropertyType); Expression filterValue = GetClosureOverConstant(constantVal, property.PropertyType);
if (!string.IsNullOrEmpty(dynamicQuery))
{
dynamicQuery = GetDynamicQueryString(filterTerm, dynamicQuery.ToString(), filterValue.ToString());
}
if (filterTerm.OperatorIsCaseInsensitive) if (filterTerm.OperatorIsCaseInsensitive)
{ {
propertyValue = Expression.Call(propertyValue, propertyValue = Expression.Call(propertyValue,
...@@ -219,8 +330,6 @@ namespace Sieve.Services ...@@ -219,8 +330,6 @@ namespace Sieve.Services
.First(m => m.Name == "ToUpper" && m.GetParameters().Length == 0)); .First(m => m.Name == "ToUpper" && m.GetParameters().Length == 0));
} }
var expression = GetExpression(filterTerm, filterValue, propertyValue, bCaseSensitive); var expression = GetExpression(filterTerm, filterValue, propertyValue, bCaseSensitive);
if (filterTerm.OperatorIsNegated) if (filterTerm.OperatorIsNegated)
...@@ -236,27 +345,10 @@ namespace Sieve.Services ...@@ -236,27 +345,10 @@ namespace Sieve.Services
{ {
innerExpression = Expression.Or(innerExpression, expression); innerExpression = Expression.Or(innerExpression, expression);
} }
}
} }
} }
if (!string.IsNullOrEmpty(dynamicQuery))
{
if (string.IsNullOrEmpty(dynamicQuery1))
{
dynamicQuery1 = "(" + dynamicQuery + ")";
} }
else
{
dynamicQuery1 += " or " + "(" + dynamicQuery + ")";
}
}
if (outerExpression == null) if (outerExpression == null)
{ {
outerExpression = innerExpression; outerExpression = innerExpression;
...@@ -268,12 +360,16 @@ namespace Sieve.Services ...@@ -268,12 +360,16 @@ namespace Sieve.Services
} }
outerExpression = Expression.And(outerExpression, innerExpression); outerExpression = Expression.And(outerExpression, innerExpression);
} }
if (outerExpression==null) if (outerExpression == null)
{ {
return null; var property = Expression.Property(parameterExpression, "id");
ConstantExpression constant = Expression.Constant(0, typeof(int));
var rst = Expression.GreaterThan(property, constant);
return Expression.Lambda<Func<TEntity, bool>>(rst, parameterExpression);
} }
return Expression.Lambda<Func<TEntity, bool>>(outerExpression, parameterExpression); return Expression.Lambda<Func<TEntity, bool>>(outerExpression, parameterExpression);
} }
private IQueryable<TEntity> ApplyFiltering<TEntity>( private IQueryable<TEntity> ApplyFiltering<TEntity>(
TSieveModel model, TSieveModel model,
IQueryable<TEntity> result, IQueryable<TEntity> result,
...@@ -469,7 +565,7 @@ namespace Sieve.Services ...@@ -469,7 +565,7 @@ namespace Sieve.Services
return result; return result;
} }
public int ResultCountBeForeApplyPagination = 0; public int ResultCountBeForeApplyPagination = 0;
public void CaculateSkipAndTake( TSieveModel model, ref int skip, ref int take) public void GetSkipAndTake( TSieveModel model, ref int skip, ref int take)
{ {
var page = model?.Page ?? 1; var page = model?.Page ?? 1;
var pageSize = model?.PageSize ?? _options.Value.DefaultPageSize; var pageSize = model?.PageSize ?? _options.Value.DefaultPageSize;
...@@ -478,16 +574,10 @@ namespace Sieve.Services ...@@ -478,16 +574,10 @@ namespace Sieve.Services
take = Math.Min(pageSize, maxPageSize); take = Math.Min(pageSize, maxPageSize);
} }
public bool enablePagination = true;
private IQueryable<TEntity> ApplyPagination<TEntity>( private IQueryable<TEntity> ApplyPagination<TEntity>(
TSieveModel model, TSieveModel model,
IQueryable<TEntity> result) IQueryable<TEntity> result)
{ {
if (!enablePagination)
{
return result;
}
var page = model?.Page ?? 1; var page = model?.Page ?? 1;
var pageSize = model?.PageSize ?? _options.Value.DefaultPageSize; var pageSize = model?.PageSize ?? _options.Value.DefaultPageSize;
var maxPageSize = _options.Value.MaxPageSize > 0 ? _options.Value.MaxPageSize : pageSize; var maxPageSize = _options.Value.MaxPageSize > 0 ? _options.Value.MaxPageSize : pageSize;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment